Juan Perez's RBI single in the bottom of the eighth inning lifted the San Francisco Giants over the San Diego Padres, 5-4, in the middle test of a three-game series.
The Padres scored twice in the top of the eighth to take a 4-3 lead. San Diego then sent Dale Thayer (0-3) to the mound in the bottom half. Buster Posey singled and was replaced by Andres Torres, who raced home on Brandon Belt's double off the wall in right-center field. Nick Noonan was intentionally walked before Joaquin Arias lined out. Perez followed with the game-winning hit to right.
"He saved the game for us, he won the game for us," Giants manager Bruce Bochy said of Perez.
Sergio Romo tossed a scoreless ninth to pick up his 17th save of the season for the Giants, who snapped a three-game skid. Matt Cain got the start and gave up two runs on six hits while striking out 10 over seven innings. Jean Machi (2-0) recorded the final out in the eighth to get the win.
Belt went 3-for-4 with two runs scored for San Francisco. Gregor Blanco and Posey added an RBI apiece.
Jesus Guzman, Nick Hundley and Logan Forsythe homered for the Padres, who had a seven-game winning streak snapped. Andrew Cashner allowed three runs on 10 hits while striking out six over 6 2/3 frames.
"That was a great baseball game," said Padres manager Bud Black.
The Giants jumped out to a 1-0 lead in the first. Blanco walked, stole second and scored on Posey's single to right.
San Francisco added another run in the fourth. Belt and Noonan hit back-to- back singles to start things. A run scored when Arias grounded into a double play.
Hundley and Forsythe hit solo homers off Cain in the sixth to tie the game at two.
The Giants went back in front in the seventh. Arias singled, advanced to second on a sacrifice bunt and scored on Blanco's base hit up the middle.
San Diego, however, took its first lead of the game in the eighth. Jeremy Affeldt walked Forsythe before giving up a homer to pinch-hitter Guzman.
The Giants signed shortstop Christian Arroyo, their first-round selection (25th overall) in the 2013 First-Year Player Draft ... San Francisco shortstop Brandon Crawford left Tuesday's game with a sprained middle and index finger on his right hand.