NEW YORK -- Thousands of police officers, state troopers, sheriff's deputies and others from law enforcement agencies big and small across the U.S. gathered at the New York City funeral of a slain officer killed with his partner in a brazen daytime ambush a week ago.

The sea of blue uniforms stretched for blocks outside the Christ Tabernacle Church in Queens on Saturday, as police helicopters flew above in a missing-man formation and Officer Rafael Ramos' body was carried by pallbearers in a casket draped in the New York Police Department flag. The NYPD estimates more than 20,000 officers attended.

"When an assassin's bullet targeted two officers, it targeted this city and it touched the soul of an entire nation," Vice-President Joe Biden said in his eulogy.

But the sombre day, reminiscent of the bond New Yorkers shared after the Sept. 11 attacks and Superstorm Sandy, was not without tension.

During the eulogy, a few hundred officers outside the church turned away from giant screens showing New York Mayor Bill de Blasio, who has been harshly criticized by New York police union officials as a contributor to a climate of mistrust that preceded the killings of Ramos and his partner, Wenjian Liu.

Sgt. Myron Joseph of the New Rochelle Police Department said he and fellow officers turned their backs spontaneously to "support our brothers in the NYPD."

In a statement, de Blasio's spokesman said: "The Ramos and Liu families, our police department and our city are dealing with an unconscionable tragedy. Our sole focus is unifying this city and honouring the lives of our two police officers."

In his eulogy, the mayor said hearts citywide were broken after the Dec. 20 shootings.

Police Commissioner William Bratton, who said Ramos and Liu were targeted because they wore a uniform, was scheduled to appear on CBS' "Face the Nation" on Sunday. On Friday he made Ramos -- who was studying to become a pastor and kept Bible study books in his locker -- an honorary chaplain of his Brooklyn precinct.

Police union officials in contentious contract negotiations with the city have faulted de Blasio for showing sympathy to protesters angry over the police deaths of Michael Brown in Ferguson, Missouri, and Eric Garner on Staten Island.

At a hospital after the officers' slayings, the police union's president, Patrick Lynch, and others turned their backs on de Blasio. Lynch said the mayor had "blood on his hands."

After the funeral, Lynch and de Blasio exchanged nods as they exited the church. Lynch refused to answer reporters' questions about officers turning their backs.

Weeks before the shooting, Lynch had suggested officers sign a petition requesting that the mayor not attend their funerals were they to die in the line of duty.

Since the Dec. 20 shootings, de Blasio has stood firmly by police, calling on demonstrators to temporarily halt protests and praising the department after it arrested several men charged with threatening police.

After the officers' deaths, the gunman, Ismaaiyl Brinsley killed himself. Police said he was troubled and had shot and wounded an ex-girlfriend in Baltimore earlier that day.

In online posts shortly before the attack, Brinsley referenced the killings of Brown and Garner, both of whom were black, by white police officers.

Ramos and Liu were the first officers to die in the line of duty in New York since 2011. Funeral plans for Liu haven't been announced.

They have both been posthumously promoted to first-grade detective. Ramos, a married father of two, was buried at Cypress Hills Cemetery after the service.

Officer Dustin Lindaman of the Waterloo Police Department flew from Iowa to attend Ramos' funeral.

"He's one of our brothers, and when this happens, it affects everyone in law enforcement -- it absolutely affects everyone," he said.