The government is breathing a sign of relief after retaining a seat of Upper Hunter in a by-election.