The setup for a fairy tale ending was all too perfect Sunday afternoon at Fenway Park.

The Red Sox trailed by a run heading into the bottom of the ninth inning of Game 162. MVP candidate Mookie Betts was due up for Boston, followed by David Ortiz.

Ortiz, of course, made a career off coming up big in the biggest moments. That’s one of the many reasons why on Sunday the Red Sox and their fans celebrated the designated hitter before the game, the final regular-season contest of a brilliant career.

In a perfect world, Betts would have reach and set the table for one more (regular-season) walk-off from Ortiz. Instead, Betts lined out to shortstop to start the inning.

Still, Ortiz could have at least tied the game with what would have been career home run No. 542. Instead, he hit a dribbler that went no more than 3 feet in front of home plate and the Toronto Blue Jays easily retired him in his last regular-season at-bat before retirement.

Sunday didn’t come with a perfect ending for Ortiz and the Red Sox. But for anyone who’s watched Ortiz strut to the plate and deliver in the clutch time and time again over his 14 years in Boston knows it’s never too crazy to expect perfect when Ortiz has a bat in his hand.

And that will be Ortiz’s longest-lasting on-field legacy. Go back 15, 16 years. Go back to the pre-Ortiz years. Red Sox fans would have sat there and watched Boston try to tie the game in the ninth inning, but deep down, they feared — they knew — the Red Sox would falter. And if for some reason they did pull it out, that only was setting up a bigger and more painful collapse down the road. That anticipation of doom always was there, and it usually didn’t take long for it to actualize.

But now Red Sox fans expect the best to happen. Fourteen seasons of Ortiz with the three World Series titles and countless clutch home runs later, it’s hard to imagine this is the same team that was, it seemed, perpetually cursed.

Ortiz changed that thinking. He obviously had plenty of help along the way, but no one embodies the golden years of the Red Sox more than No. 34.

The Minnesota Twins cast-off wasted little time in his making his impact felt in Boston. It’s laughable in hindsight that Ortiz struggled to get at-bats from Jeremy Giambi early in 2003, but his production in his first season with Boston often goes overlooked. Alongside Manny Ramirez in the middle of a Boston batting order that produced one of the most potent offenses in club history, Ortiz clubbed 31 home runs and 101 RBIs in just 128 games in 2003.

And while he struggled for much of the American League Division Series against Oakland, he really made his mark in Game 4 against Keith Foulke, which was fittingly his first playoff hit with the Red Sox.

Ortiz then hit two home runs in the American League Championship Series against the Yankees, including one in Game 7, as it looked like Boston was World Series-bound. Then, of course, disaster struck, Grady Little happened, so did Aaron Boone, and Ortiz got a firsthand look at what it meant to be a part of the Red Sox.

“That’s what it meant — not just for the fanbase — but to play for the Red Sox,” said NESN’s Red Sox studio host Tom Caron, who covered that team as a sideline reporter. “They knew that history. They were all part of that, but he erased it. They erased it the following year.”

Did they ever.

No Red Sox fan can ever forget what Ortiz did in 2004. It’s the stuff of legend. He put an entire fanbase at its absolute lowest on his back and almost singlehandedly helped the Red Sox exorcise nearly a century of demons.

First, it was Game 4.

Then, when everyone expected him to deliver again a night later, he did it again.

A little more than a week later, the Boston Red Sox were World Series champions, something multiple generations probably still can’t believe.

Had Ortiz walked away then — if for some reason, he called it quits at 28 — he’d still be considered one of the most important figures in Boston sports history. It didn’t end there, though. He was just getting started.

Ortiz had a monster postseason in 2007 — including reaching base in 11 of his 13 plate appearances in the ALDS — helping Boston to another World Series.

And as a 37-year-old in 2013, he put on a show for the ages. He came to the plate in Game 2 of the ALCS against the Detroit Tigers with the bases loaded and the Red Sox trailing by four runs in the bottom of the eighth.

Again, it seemed to perfectly set up to actually happen. Again, he delivered.

All he did from there was hit .688 in the World Series, reaching base in 19 of 25 plate appearances. The Red Sox won the World Series, and Ortiz earned MVP honors.

If he walked away after that, no one would have blamed him. There would be a statue outside Fenway Park by now.

But he came back, and here in 2016, his final season, he looks like he’s saving his best for last. It’s fitting that in the ninth inning of his career, Ortiz led the league in doubles and RBIs. Still, he and Red Sox fans are hoping there’s one last legendary run left in those creaky feet and ankles.

Given what we’ve seen from Ortiz for the last decade and a half, it’s hard to bet against him.

But even if Ortiz and the Red Sox fall short, he’ll still walk away as arguably the most influential player in franchise history. He’ll walk away as one of the most influential athletes in the history of this city and region.

He changed the mindset from that of hoping to win but expecting the worst to expecting to win. Every time.

A lot of people had a hand in that, but no hand carried more power than Ortiz’s.

“We changed things around here,” Ortiz said on the recent NESN documentary “The David Ortiz Era.” “We do things that a lot of great players didn’t get to accomplish. I say ‘we’ because I didn’t win a World Series myself. We won the World Series as a team. That era, those players combined with myself, I think it was a good era for this organization, the Red Sox fans, but it’s going to be a challenge for the next generation because now people are going to be expecting that.”

Thumbnail photo via Robert Deutsch/USA TODAY Sports Images