SAN FRANCISCO -- As full-scale, nationally televised meltdowns go, this one will be difficult to top.

With two out and the go-ahead run on second base in the bottom of the eighth inning here Sunday night, Phillies manager Gabe Kapler turned to converted starter Nick Pivetta to keep the game tied and send it into the ninth inning. And Pivetta promptly gave up three hits to the next four batters -- including a two-run single to a pitcher -- in an eventual 9-6 loss to the San Francisco Giants.

OK, let's pause and digest that for a moment: With the outcome in the balance, in a game that could have wild-card implications, Kapler's best option out of the bullpen was a pitcher with nine career relief appearances and a 5.36 earned-run average this season.

"It was the matchup that we looked for," Kapler said of pitting Pivetta against Kevin Pillar. "We talked about it prior to the inning. We were going to set up Pillar with fastballs up and out, hammers [curveballs] down. We weren't able to execute."

Indeed, Pivetta gave up a triple to Pillar that put the Giants ahead, 7-6. After Brandon Crawford walked intentionally and stole second base uncontested, Pivetta gave up the first hit of Giants closer Will Smith’s career -- in his first career plate appearance -- which effectively drove a stake through the Phillies’ heart.

"It's a humbling game," Pivetta said.

The Phillies, who left 15 runners on base, limped home after the finale of a western road trip in which they went 2-5. They are 60-58 overall and have fallen to fourth place in the National League East and fifth place in the wild-card race, two games behind the St. Louis Cardinals for the last NL playoff spot.

"Obviously I need to do a much better job getting ahead of guys with my breaking ball. I didn't establish that at all," Pivetta said. "Went for a four-seamer up in the 3-1 count, left it too much middle to Pillar. I need to get Pillar. That's my job. My job was to get that one guy out, and I didn't do that."

And as Pivetta made the slow walk to the dugout after the eighth inning, Smith jogged into the third-base dugout and received a round of high-fives.

Then, he shut down the Phillies in the ninth inning.

In the Phillies’ best-laid plans, it would’ve been David Robertson or Tommy Hunter or Pat Neshek or Seranthony Dominguez in that eighth inning. But all of them are on the disabled list, leaving Kapler to search for solutions.

“This was definitely a tough road trip, a disappointing road trip,” Kapler said. “We didn’t execute enough on offense or on the mound. But we go back home, and we know that we’re right in the hunt and we continue to fight, and that’s it. That’s what we have control over, how we respond to getting knocked down.”

It has become next to impossible to list all of the Phillies’ crushing losses in a season that feels as though it’s slipping away. But this one has to be at the top of the list.

Jake Arrieta, who continues to pitch with a bone spur in his right elbow, put the Phillies in a 2-0 hole in the first inning and barely survived a three-run third. The Phillies blew a 5-2 lead, then rallied to tie the game, 6-6, before coughing it up.

Arrieta had hoped to pitch deeper into this game, especially after feeling as though he could've gone six innings in his previous start. And now, after his latest clunker, he said he will have to at least consider shutting it down for the season and having surgery to remove the painful bone spur.

“I don’t necessarily want to make a decision right now,” he said. “We’ll have the off-day [Monday] and maybe have a conversation on Tuesday.”

Trailing, 6-5, in the eighth inning, and having squandered a big opportunity one inning earlier when slumping Rhys Hoskins popped out with the bases loaded, the Phillies rallied to tie the game against the Giants’ two best relievers.

J.T. Realmuto got it started with a one-out single against lefty Tony Watson. Scott Kingery followed with a double into the right-field corner. The Giants brought in Smith, their closer, and Corey Dickerson lifted a sacrifice fly to score Realmuto with the tying run.

But Pivetta couldn’t hold it.

“[It’s] a heavy heart in losing like that because it’s a lot to put on me,” Pivetta said. “But I know that when I get the opportunity in the next couple days, I’m going to go out and do what I’ve been doing and just be consistent. It’s not the right time [to falter]. We need to be stacking on wins. That’s on me today, especially in that eighth inning. Just got to move forward.”

What choice do the Phillies have?