PHOENIX -- Khalil Greene knows how to get ready for a stretch run.
The San Diego shortstop homered and drove in three runs, and the Padres beat the Arizona Diamondbacks 5-3 on Friday night, maintaining their five-game lead over San Francisco in the NL West with nine games to go and reducing their magic number to five.
"Struggling somewhat earlier in the season or what have you, there's always something you could take from knowing it is a long season," said Greene, who has four homers, including his first career grand slam, and 12 RBI in the last seven games. "One game could kind of turn it around for you, and now is when it matters more than any other time, I guess, if you look at it that way."
Ramon Hernandez had an RBI single and Brian Giles added an RBI double off Arizona reliever Oscar Villarreal in San Diego's two-run eighth inning. The Padres (77-76) moved back over .500 with their sixth win in nine games after a four-game losing streak.
The Giants kept pace with a 7-6 victory at Colorado.
"It's in our hands," Padres manager Bruce Bochy said. "We like to win ballgames. That's what we need to do -- take care of business."
Pedro Astacio (3-2) allowed two runs and six hits in six-plus innings. Signed as a minor league free agent on June 30, Astacio is 3-0 in his last six starts.
"He was awesome," first baseman Robert Fick said. "He's been doing it for us since he got here. I think he had one bad outing, and since then he's been lights out."
Astacio retired seven straight hitters after Royce Clayton was thrown out trying to stretch a double into a triple in the fourth inning. The relay went from center fielder Dave Roberts to Greene to Joe Randa at third for the second out. Astacio got Kelly Stinnett for the last of his four strikeouts to end the inning.
"They have a great lineup," Astacio said. "A really good lineup. I go there and try to give the team some chance to come back and win the game."
Shawn Green led off the seventh with a single and Scott Linebrink relieved. Linebrink started a 1-6-3 double play to erase Green and then struck out Stinnett.
Craig Counsell had a one-out single in the eighth, stole second, went to third on catcher Hernandez's throwing error and scored on a wild pitch by Linebrink. Akinori Otsuka came on with two outs and struck out Tony Clark. Trevor Hoffman pitched the ninth for his 38th consecutive save and 41st overall.
Hoffman walked Royce Clayton with two outs, but pinch-hitter Alex Cintron, representing the tying run, lined out to Mark Loretta at second.
"I thought we had some pretty decent approaches on him," Diamondbacks manager Bob Melvin said of Hoffman. "You know, Alex hits the ball hard right there at the end. But when he comes in the game, he's finished the deal quite a few times."
Claudio Vargas (9-9) gave up three runs and a career-high seven walks in six innings. He allowed seven hits, including Greene's two-run homer in the fifth that gave the Padres a 3-2 lead.
Vargas walked five in the first three innings, but allowed only one run in that span as the Padres stranded seven runners. Greene got his 66th RBI, surpassing his career high set last year, when Vargas walked him with the bases loaded in the first inning.
"We had tough innings all day," Stinnett said. "It seemed like we had the leadoff guy on every inning, so it led to a tough night trying to battle through that. If you can't throw your fastball for a strike, you're working behind."
Arizona took a 2-1 lead in the third on three consecutive two-out hits -- doubles by Luis Gonzalez and Chad Tracy, and Clark's single.
1B Clark banged into the guardrail in front of a photographers well catching Randa's popup in the third inning, but kept his balance. ... Astacio is 5-10 in 21 starts this year, 12 with Texas before the Rangers released him on June 21. ... Counsell's unearned run ended Linebrink's streak of 20 2/3 scoreless innings over 19 appearances. ... Randa had his career-high 42nd double after Greene cleared the bases in the fifth.