SAN DIEGO - The San Francisco Giants probably are looking forward to leaving Petco Park, even though their next stop is Dodger Stadium.

Andrew Cashner pitched brilliantly into the ninth inning and the San Diego Padres beat San Francisco 3-2 Saturday night to keep the Giants from gaining ground on the NL West-leading Los Angeles Dodgers.

The Giants remain 3 1/2 games behind the Dodgers, who lost 8-7 at the Chicago Cubs. San Francisco also is one game ahead of Pittsburgh in the race for the top wild-card spot.

Cashner (5-7) took a two-hit shutout into the ninth before allowing Hunter Pence's 20th home run, a leadoff drive to left on a 1-0 pitch. Joe Panik followed with a single to left to chase Cashner, who threw 106 pitches. He struck out seven and walked one.

"Cashner was good," Giants manager Bruce Bochy said. "I don't know who would have hit him tonight. He was coming off a two-hit shutout. We were facing a really good pitcher tonight on the top of his game."

On Friday night, the Padres got seven strong innings from rookie Odrisamer Despaigne and beat San Francisco 5-0.

"We ran into two really well-pitched games," said Bochy, who managed the Padres during their first three years at Petco Park. "That's going to happen in this ballpark. We've had a tough go. They have really good pitching. They've done a good job on us. These are two tough losses at this time of year. There's nothing we can do but come out tomorrow ready and focused."

Cashner beat the Philadelphia Phillies 1-0 in his previous start on Monday night. Manager Bud Black let him start the ninth in pursuit of another shutout.

"I wanted to try and finish," the tall right-hander said. "You don't get the opportunity a lot of times to take the game into the ninth with pinch hitters and game situations. I thought it was pretty cool him letting me go back out there."

The home run pitch "was just a changeup middle down. I thought I could have executed a little bit better on that. I pitched him a little bit different all night. I just missed location. It was just down the middle instead of throwing to a location."

Cashner retired 15 straight before Pence's homer. Kevin Quackenbush came on and got the first out before allowing Pablo Sandoval's single. Quackenbush struck out Brandon Belt before Gregor Blanco hit an RBI single. Brandon Crawford flied out to left with runners on the corners, giving Quackenbush his fifth save in six chances.

San Diego, closing out another miserable year, improved to 8-6 against the Giants this season. The series concludes on Sunday, and the teams finish the season with a four-game set at San Francisco.

Cashner hit his first career triple with one out in the fifth, the first triple by a big league pitcher this season, and bunted for a single in the sixth.

Yusmeiro Petit (5-5), who has taken Tim Lincecum's spot in the Giants' rotation, allowed three runs, two earned, and four hits in 5 1-3 innings. He struck out seven and walked three.

The Padres scored with one out in the third on second baseman Panik's throwing error on a potential double-play ball. The Padres loaded the bases before Petit struck out Jedd Gyorko and Yasmani Grandal.

Petit had faced 70 straight batters without going to a three-ball count before he walked Cashner with one out in the third.

Petit was chased by Rene Rivera's two-run double with one in the sixth. Grandal was aboard on a double and Seth Smith on an intentional walk.

HUNTER PENCE

The Giants' outfielder has 179 hits this season, the most in the NL. He's also scored 105 runs and collected 288 total bases.

TRAINER'S ROOM

Giants: CF Angel Pagan was out of the lineup for the fourth time in five games due to a bulging disk in his back. Pagan "was pretty sore" and was to be examined by a team doctor, Bochy said. Also, INF-OF Michael Morse on Friday night aggravated a strained side muscle that had kept him out for 15 games.

Padres: Black said Despaigne reported a sore latissimus dorsi muscle late in his strong effort in Friday night's win.

UP NEXT

Giants: RHP Ryan Vogelsong (8-11) is 1-3 with a 9.37 ERA in five games at Petco Park, including four starts.

Padres: RHP Ian Kennedy (11-13) is going for his second straight win after losing two in a row.