SAN FRANCISCO (AP) -- Pablo Sandoval hit a game-ending solo homer with one out in the ninth inning, lifting the Giants over the San Diego Padres 5-4 on Sunday in what may have been Johnny Cueto's final start with San Francisco.
Cueto had a rocky outing on the last day of the season, allowing four runs and 12 hits in five innings. Cueto can opt out of the $130 million, six-year contract he signed before the 2016 season and become a free agent.
Sandoval hit a 3-2 fastball from rookie Phil Maton (3-2). It was his fifth homer in 47 games since returning to San Francisco this summer.
Giants reliever Hunter Strickland (4-3) pitched 1 1/3 scoreless innings.
After the game, San Francisco honored retiring pitcher Matt Cain on his 33rd birthday. Cain made the final appearances of his 13-year career Saturday. Manager Bruce Bochy and catcher Buster Posey both spoke, and Bochy also thanked the fans for supporting the Giants through a 64-98 season.
The Giants lost 23 more games than they did last season in their worst year since 1985, when they lost 100 games.
The Padres (71-91) lost for the sixth time in seven games as they concluded their seventh straight losing season with a modest improvement of three more wins than last season.
The Giants hadn't won a series with the Padres since the 2016 All-Star break, losing seven of eight with one series tie over that stretch.
Padres starter Luis Perdomo, who's expected to compete for a spot in the rotation next spring, gave up four runs, seven hits and one walk in seven innings. The 24-year-old right-hander threw a season-high 108 pitches.
Hunter Renfroe was 2 for 4 with a home run. He connected on his 26th homer off Cueto in the third inning.
The Giants rallied from a 4-1 deficit when Brandon Crawford's run-scoring double highlighted a three-run inning fourth inning.
Giants right fielder Hunter Pence made a diving backhand catch on a ball in the gap, robbing Renfroe of extra bases for the final out in the seventh. Pence left the field to a standing ovation.
Perdomo tripled leading off the fourth inning, his fifth of the year and his second against the Giants. Perdomo scored when Travis Jankowski followed with an infield hit to give the Padres a 4-1 lead.