From “The Simpsons” trailer for record-breaking episode No. 636. (Fox TV)

“WHY DOES the show endure?” legendary comedy writer Mike Reiss asks of “The Simpsons,” the show he used to run, in his forthcoming book “Springfield Confidential.” “Because it’s based on two fundamental principles: family and folly.”

With that in mind, it only seems fitting that “The Simpsons” draws upon both principles to dramatize its act of taking down “Gunsmoke” as the new sheriff of television. On Sunday night, “The Simpsons” will pass the classic midcentury western by airing show No. 636 — the most scripted episodes broadcast by any prime-time series in TV history.

In a new video to mark the moment, “Gunsmoke” Marshal Matt Dillon challenges Maggie Simpson to a duel. “Can’t let you do it,” the 6-foot-7 lawman intones to baby Maggie. “Can’t let you break my record.” But there’s no pacifying Maggie, who’s faster on the draw.

The video then parodies the opening to “Gunsmoke” (1955-1975), from cameo-style images to musical theme.

So what does “Simpsons” executive producer Matt Selman think of the TV-history showdown?

“I say reboot ‘Gunsmoke’ and keep TV’s hottest rivalry going strong,” Selman tells The Washington Post’s Comic Riffs, holstered tongue in cheek.

“The Simpsons” is in its record-setting 29th season and has been renewed to return for its 30th.

“The record-holder for most seasons was of course ‘Gunsmoke’ at 20,” “Simpsons” producer David Silverman tells The Post. “Once we broke that, I had a feeling we would go to at least 30 seasons.  And I was looking towards breaking the 635 [episode] mark — it seemed a strong possibility.

“But I never thought that I’d be writing the tying episode,” continues Silverman, who co-wrote last week’s Episode No. 635, titled “Lisa Gets the Blues,” with Brian Kelley. “That was completely happenstance.”

In that episode, Lisa goes to Jazz Fest and must face her musical failures once in the Big Easy.

“I had pitched the idea for the New Orleans episode to [showrunner] Al Jean in late March 2017,” says Silverman, who directed “The Simpsons Movie.” “It was a notion Matt Groening and I talked about in 2002, while we were both at New Orleans Jazz Fest. … It was only at the table read [last] July that I discovered it would tie with ‘Gunsmoke.’ ”

“ ‘Gunsmoke’ lasted twenty years, ‘Lassie’ did nineteen,” Reiss writes in “Springfield Confidential.” “ ‘Ozzie and Harriet’ went fourteen seasons … it’s a little embarrassing what boring shows we’re competing with.”

“We’re not tapped out just yet,” he adds.

So how much longer can “The Simpsons” go on?

Until “the day people all over the world start treating each other with love, respect and intelligence,” Reiss writes.

“I hope that day never comes.”

The record-breaking episode, titled “Forgive and Regret” — in which Grampa Simpson offers a stunning deathbed confession — airs Sunday night at 8 on Fox.

Read more:

Justice Ivanka? How ‘The Simpsons’ swiftly turned around a timely ‘Trump’s first 100 days’ spoof.

‘The Simpsons’ predicted a Trump presidency years ago. The writer explains why.