A now-fatherless 4-year-old boy saluted the flag-draped coffin carrying an NYPD detective and airman killed by a Taliban suicide bomb as thousands outside St. Patrick’s Cathedral joined Wednesday to mourn.

Sitting on the shoulders of a uniformed police officer, Ryan Lemm paid his final respects to his father, Joseph G. Lemm, an Air National Guard technical sergeant who died in Afghanistan last week.

“Today we say farewell to a hero of our time and a hero for all ages,” said NYPD Commissioner William Bratton, one of several delivering eulogies.

The Rev. Christopher Monturo, Lemm’s pastor at Saint Anthony of Padua Church in West Harrison, gave the homily, calling the fallen officer a “Superman” and his death serving the nation a “final heroic act.”

Monturo, turning to Lemm’s widow, Christine, and the couple’s two children, said, “We want to help you dry your tears.”

Little Ryan — wearing a red necktie and dark suit, a police shield pinned to his lapel — played on a cop’s shoulders, doffing a police cap with his dad’s coffin a few feet away.

The boy, startled, had put his hands over his hears when the loud voice of the NYPD ceremonial unit’s announcer came over the loudspeaker to “present arms!” As pallbearers carried the casket down the cathedral steps, he joined the officers solemnly arrayed around them in a final salute. Lemm was buried at Gate of Heaven Cemetery in Hawthorne.

In a Mass overseen by the Archbishop of New York, Cardinal Timothy Dolan, Lemm’s daughter, Brooke, 17, gave a reading from Ecclesiastes — passages speaking of “a time to be born and a time to die,” and “a time to kill and a time to heal.”

Lemm, 45, is one of six American service members who died Dec. 21 in the attack. An assailant on a motorcycle packed with explosives crashed into a patrol convoy near Bagram Air Field. The Taliban has claimed responsibility.

“Joe dedicated himself to the highest ideals of the city, this profession, this department, this nation. He dedicated himself to freedom,” Bratton said, posthumously promoting Lemm to first-grade detective, an elevation common after line-of-duty deaths. The move increases the benefits a family receives.

Officials recalled how Lemm, whose roots are in Nebraska and Iowa, enlisted in the Air Force in 1988, and fulfilled his dream to move to New York City by becoming an NYPD cop in 2000.

“Our city is heartbroken. There are people mourning in every part of this great metropolis,” said Mayor Bill de Blasio, who attended with his wife, Chirlane McCray.

As police motorcycles roared past the church and a hearse pulled up to St. Patrick’s with Lemm’s casket, pedestrians watched from cordoned-off streets in front of Fifth Avenue shops; office workers looked from windows overlooking the church to watch the rites.

Along with police officers from the New York area and as far away as Chicago who came to pay respects, there were airmen and airwomen in blue dress uniforms and members of all branches of the military.

Lemm, of West Harrison in Westchester County, was a member of the 105th Airlift Wing based in Newburgh. He had been deployed twice to Afghanistan and once to Iraq since he joined the NYPD. One of his early police assignments was picking through the rubble of the Twin Towers to search for survivors.

Bratton noted that 1,110 reservists are in the NYPD’s ranks, including 130 on active duty.

Lemm’s eulogizers paid tribute to the five others killed in the Dec. 21 attack, including the Long Island airman, Staff Sgt. Louis Bonacasa of Coram, whose funeral is Saturday at New Beginnings Christian Center in the hamlet.

Had be come home safe, the mourners in Coram Saturday would all but certainly have included Joseph Lemm, who went to every police funeral he could, said Monturo. “He never missed one.”

With Gary Dymski