NEW YORK (AP) — Alex Rodriguez’s record-breaking grand slam was a big one for the Yankees, who can only hope it came in time for a last-gasp run at the playoffs.

Rodriguez set a major league mark with his 24th career slam, passing Lou Gehrig with a tiebreaking shot in the seventh inning that sent New York to a 5-1 victory over the San Francisco Giants on Friday night.

“It means I’m getting old,” said Rodriguez, 38. “It’s hard to think about things like that right now. We’re really on a sprint to the end here and every win is huge for us.”

More importantly for the Yankees, it carried them to their second win in seven games as they cling to faint playoff hopes.

New York, which has eight games remaining, pulled within three of the second AL wild card — with three other teams in between. The Indians’ 2-1 rain-shortened win over Houston, moved them a half-game ahead of Texas, which began the day tied with Tampa Bay atop the standings but lost 2-1 to Kansas City. The Rays beat Baltimore in 18 innings to keep the lead.

“It’s an unbelievable accomplishment and it’s a big one for us,” Yankees manager Joe Girardi said. “I don’t know if we can afford to lose any more games.”

Alfonso Soriano also homered and New York opened its final homestand with a much-needed victory, hours after pitcher Andy Pettitte announced his plans to retire — again — following this season.

The news about Pettitte hardly came as a surprise, but the 41-year-old lefty put to rest any speculation he might be back next season. Pettitte, who stepped off the mound for a year before returning in 2012, is scheduled to start the series finale Sunday — the same day the Yankees will honor retiring closer Mariano Rivera in a pregame ceremony.

Rookie outfielder Juan Perez, back home in the Bronx, hit an RBI double for the Giants. The defending World Series champions have spent all week in New York — they took two of three from the Mets at Citi Field, where a large throng of vocal San Francisco fans made them feel right at home.

Giants rooters came out in the Bronx, too, easy to spot in their orange or black jerseys. An audible chant of “Let’s go Giants!” quickly drew boos from Yankees fans in the crowd of 41,734.

With the score tied at 1, Eduardo Nunez grounded a single through the right side to start the seventh. Lincecum (10-14) hit Brendan Ryan with a pitch and walked Ichiro Suzuki with two outs, loading the bases and ending the right-hander’s night.

Rodriguez, hampered by a sore leg lately, reached out and lofted a 2-1 pitch from George Kontos toward the hitter-friendly dimensions in right field for his 654th career homer and seventh this season.

“Just tough to watch and tough to put somebody in that kind of situation,” Lincecum said. “Just wasted a lot of pitches that inning, didn’t attack the zone as much as the previous six, and I felt like I could’ve gotten out of that better than what happened.”

The hit snapped a 1-for-25 slide for A-Rod, who returned Aug. 5 from hip surgery. It was his 14th slam for the Yankees, moving him ahead of Joe DiMaggio for second place on the franchise list behind Gehrig.

“I’m a huge fan of Lou Gehrig, everything he’s done, going back to his college days in New York,” Rodriguez said. “He’s kind of the gold standard for a Yankee. Special moment. I’ll think about it someday.”

Sabathia was pulled after a leadoff single in the eighth and walked off to a warm ovation. David Robertson got Perez to ground into a double play, and Rivera worked a perfect ninth.

But it was Rodriguez who delivered the big blow.

“Unbelievable — especially at that moment. He came up big for us again,” Sabathia said. “Hopefully we can just keep getting big hits like that and keep winning games.”

Lincecum, who had won four straight decisions, was looking for his first five-game winning streak since early in the 2010 season. He was charged with four runs and five hits in 6 2-3 innings. He threw 66 of 121 pitches for strikes.

“He pitched great,” manager Bruce Bochy said. “It’s some tough luck there in that last inning.”

NOTES: Giants CF Angel Pagan was rested in favor of Perez. … DH Buster Posey singled in the sixth to end a 1-for-23 skid on the road trip. … Rodriguez’s previous slam came in Atlanta on June 12 last year. … Looking to spark his struggling lineup, Girardi moved a slumping Suzuki into the leadoff spot, partly because he had faced Lincecum several times in the past few years. … LHP Boone Logan (elbow) was available, Girardi said. … After the Yankees got in late following Thursday night’s loss at Toronto, C Chris Stewart was rested in favor of rookie J.R. Murphy. “Physically, I just think it’s too hard,” Girardi said.