The 4-year-old son of slain NYPD Detective and Air National Guardsman Joseph Lemm saluted his father’s casket as it was carried into St. Patrick’s Cathedral on Wednesday morning for the fallen hero’s funeral.

The heartbreaking image of Ryan Lemm paying respect to his dad echoed the 1963 photograph of John Kennedy Jr. saluting his assassinated father’s casket in November 1963.

Little Ryan, wearing his father’s gold shield and clutching his police hat, sat on the shoulders of Lemm’s partner, Detective John McCrossen.

“The city is heartbroken today,” Mayor de Blasio said. “It’s a solemn moment and it’s a moment where we remember someone who died fighting for all of us.”

The mayor called Lemm, 45, a “real-life New York success story” — coming from a small Nebraska town with fewer than 1,000 residents to become a member of New York’s Finest.

Police Commissioner Bill Bratton said he first met Lemm — nicknamed “Superman” for his chiseled looks and 6-foot-5 frame — two years ago, when the top cop promoted him to detective third grade.

Bratton noted that Lemm, who was promoted posthumously Wednesday to detective first grade, joined the NYPD only a year before 9/11.

“Joseph Lemm was a rookie NYPD cop then, and he was in the war from the start, sifting through the smoldering debris in the heartbreaking search for survivors,” Bratton said in his eulogy.

“Now [war] has taken him farther than miles. It has taken him to his new post in eternity. Today, we say farewell to a hero for all time and a hero for the ages.”

Lemm was among six American service members killed on Dec. 21 when a Taliban suicide bomber on a motorcycle attacked a joint US-Afghan patrol near the military base in Bagram.

He’s also survived by his wife, Christine, and stepdaughter, Brooke, 17, who read a Bible passage from Corinthians.

A sea of Lemm’s brothers and sisters in blue streamed into the cathedral along with his comrades from the Air National Guard and Air Force.

Guard Maj. Gen. Pat Murphy told mourners that Lemm was “open, personable, helpful, solid, down to earth, competent with a healthy respect for the virtue of hard work.

“Take these characteristics and blend them with a healthy dose of patriotism and sense of duty and you find a true blue American hero like Tech Sgt. Joseph Lemm,” he said.

About 100 NYPD motorcycles led a procession down Fifth Avenue followed by thousands of fellow cops.

The Rev. Christopher Monturo, pastor of St. Anthony of Padua Church in the family’s West­chester hometown of West Harrison, recalled Lemm as a jokester who loved fishing, basketball and the Nebraska Cornhuskers.

Lemm started his 15-year NYPD career at the 48th Precinct before moving to the Bronx Warrant Squad five years ago. He was deployed overseas three times, twice in Afghanistan and once in Iraq, and was due back home in March.

“God, whose nature is always to show mercy, we humbly implore you for your servant Joseph, whom you’ve called to journey back to you,” Timothy Cardinal Dolan told mourners.

With Shawn Cohen