SAN FRANCISCO (AP) — Less than 24 hours after blowing a six-run lead in an extra-innings loss, Mark Melancon prevented the San Francisco Giants from letting another big advantage slip away.

The former closer did it on a day when the Giants celebrated their 2010 World Series championship, a team that had more than its fair share of tight finishes.

READ MORE: 4 Interceptions Including A Pick 6 Help Deliver 27-17 Victory For Cowboys Over Saints

Melancon got pinch-hitter Carlos Perez to fly out with the bases loaded in the ninth inning, preserving a 5-3 win over Texas Rangers on Saturday.

“It’s really incredible how similar the two games were but we found a way to get the last out today,” San Francisco manager Bruce Bochy said. “Torture was back.”

Andrew Suarez pitched seven scoreless innings of three-hit ball to end a four-start winless drought. Brandon Crawford and Hunter Pence homered while Nick Hundley added two walks and scored.

San Francisco began the day nine games behind Milwaukee for the second NL wild card.

“It’s very important to continue to send that message of relentless attack,” Pence said. “Even where we are and as clouded as it may seem, you still never know. When there’s still a chance in this game of baseball, things can get hot in an instant.”

Hours after Bochy confirmed that six-time All-Star catcher Buster Posey is headed for season-ending hip surgery, San Francisco did the bulk of its scoring in the first inning then held on behind Suarez and three relievers for a rare win at AT&T Park.

Suarez (5-9) struck out five and walked three to win for the first time since July 29. The left-hander induced 12 groundouts and two double plays.

READ MORE: Government Shutdown Averted: US Senate Passes Stopgap Funding Bill

“This whole week I looked at old video just to see what could get me going again,” Suarez said. “Once I know I’m getting a lot of groundballs I know that’s when I’m in my game.”

The Giants have the fourth-best winning percentage in the NL at home but had lost 10 of the previous 13 at their waterfront ballpark before edging the Rangers.

Rougned Odor homered and had three hits for Texas.

“The difference is we didn’t get the big hit when we needed to there late,” Rangers manager Jeff Banister said. “We felt good about our situation once we got some baserunners there in the last inning. We were unable to come up with the big hit.”

Hunter Strickland and Will Smith combined to retire five batters before Melancon closed the hectic ninth.

Crawford homered off Martin Perez (2-6) in the first, a three-run blast to center. The Giants shortstop entered the day batting .178 since July 1, the lowest average in the majors among players with at least 150 plate appearances.

Pence had a pinch-hit home run off Matt Moore in the seventh.

Odor, who hit a tying two-run home run in the ninth Friday, trimmed the lead to 5-3 with a three-run shot off Hunter Strickland in the eighth. Two batters earlier, a two-out fielding error by left fielder Austin Slater extended the inning.

MORE NEWS: Fantasy Football Start Or Sit Week 13: Elijah Mitchell Looks To Take Advantage Of Seahawks' Defense

(© Copyright 2018 The Associated Press. All Rights Reserved. This material may not be published, broadcast, rewritten or redistributed.)