oh, there is one other possible answer on bug tracker
which is that since there is a constraint:
constraint bt_bugs_bug_number_un
unique (project_id, bug_number)
you could do the insert, and loop until the bt_bugs_bug_number_un constraint stopped firing
(which would generally be one time through).
I am not sure how you do that with postgresql though
and if that works in a transaction or not.