SAN DIEGO -- The Giants relied on five relievers and 17 hits while enduring 12 innings that spanned four hours and seven minutes and included a last-ditch San Diego rally.

In other words, the Giants needed virtually everything they had Tuesday night to defeat the Padres, who have become their chief nemesis. But San Francisco indeed prevailed, 7-6, to secure its first victory over the Padres in eight games this season.

The Giants broke a 4-4 tie with a two-run double in the 12th inning from Matt Downs, who could be relegated to the bench when Freddy Sanchez is activated from the 15-day disabled list Wednesday.

Eugenio Velez's subsequent RBI single seemed superfluous but proved essential, because Brian Wilson yielded a pair of runs in the Padres' half of the 12th on David Eckstein's two-out double. The Giants actually put the potential winning run on base by intentionally walking Adrian Gonzalez, but Wilson escaped by striking out Chase Headley to record his 10th save.

"He'll go by my gravesite [and say], 'I helped put you there,'" manager Bruce Bochy jokingly said of Wilson, who has stranded seven runners while converting his last three save opportunities.

Had the Giants lost, they would have matched their longest losing streak against an opponent to begin a season. They dropped eight games in a row to the Chicago Cubs in 1977.

Instead, the Giants trimmed their National League West deficit to a half-game behind first-place San Diego. They did so with major contributions from relievers Brandon Medders, Jeremy Affeldt, Guillermo Mota and Dan Runzler (2-0), who combined to blank San Diego for five innings, and left fielder Andres Torres, whose two-run, eighth-inning homer erased a 4-2 deficit.

"It saves a little sanity around here," Bochy said of the coveted triumph. "That was a tough situation we were in and there was a good chance it would happen again. I'll say that about the guys -- they were determined to end the streak."

Before the game, the Giants showed their resolve the way ballplayers often do -- by maintaining a carefree attitude. A few of them were literally howling with laughter during stretching exercises. Broadcaster Duane Kuiper placed a photograph of former Giants left-hander Kirk Rueter in the dugout to spread good karma. Shortstop Edgar Renteria, stuck on the disabled list, took the lineup card to home plate to change the Giants' luck.

"It was in the back of everybody's head," Downs said of the Giants' 0-7 record against San Diego. "Everybody's talking about it, the media's talking about it, we hadn't beat them yet. It's a burden off our backs to finally get it going against these guys. I guess it's like a little curse."

The spell began lifting with one out in the 12th as Padres left-hander Cesar Ramos (0-1) relieved Joe Thatcher, whose side stiffened. Ramos retired Juan Uribe before pinch-hitter Ryan Rohlinger singled and moved to third on Eli Whiteside's bloop single. Downs then drilled a changeup into the left-field corner, chasing home Rohlinger and Whiteside. Velez, who entered the game in the eighth inning as part of a double-switch, delivered his hit to score Downs and lengthen the Giants' lead to 7-4.

Before that, the Giants' biggest hit belonged to Torres, whose second homer of the season restored hope with San Diego needing only six outs to seal another triumph. Facing Mike Adams, who was unscored upon in his previous four appearances this year against San Francisco, Aaron Rowand doubled leading off the inning before Torres found the right-field seats.

"It's a great feeling," Torres said. "If we keep going 100 percent, good things are going to happen."

Asked to describe the mood in the Giants' dugout when Torres' ball vanished, Downs said, "Oh, wow. It fired everybody back up."

Earlier, the Giants refused to be dominated by actually mounting some offense against San Diego starter Mat Latos, who had shut out the Giants in 16 innings spanning two previous starts this season. He extended that streak to 19 innings until San Francisco scored twice in the fourth on two-out RBI singles by Nate Schierholtz and Whiteside.

"Everything was going good, but the fastball was up a bit," Latos said. "They hit a couple of mistakes over the plate ... a couple of solid hits and a couple of bleeders."

Giants starter Jonathan Sanchez maintained a perfect game for four innings before San Diego scored four runs in the fifth. Consecutive singles by Headley, Nick Hundley, Oscar Salazar and Chris Denorfia generated one run. After Everth Cabrera's grounder forced Hundley at home, Sanchez fired a wild pitch that scored Salazar. Jerry Hairston Jr.'s two-out single delivered Denorfia and Cabrera.