SAN DIEGO — A victory for Carl DeMaio, who is locked in a tight mayoral race, would make San Diego the second-largest city in the country to elect an openly gay mayor, and by far the largest to elect a gay Republican.

Yet, perhaps no group has opposed Mr. DeMaio as loudly as this city’s sizable gay and lesbian population.

Angered by his reticence on gay issues and his acceptance of campaign donations from backers of Proposition 8, California’s 2008 ban on same-sex marriage, parts of the crowd booed Mr. DeMaio at a mayoral debate at the gay and lesbian community center here. He was booed again as he walked hand in hand with his partner in this year’s gay pride parade.

And many gay and lesbian leaders here have lined up behind Bob Filner, 70, a Democratic congressman and Mr. DeMaio’s opponent in the Nov. 6 election.