Jan Danielsson <jan.danielsson@gmail.com> writes:
> I have seen a few "currval", "last_value", tricks and such, but they
> aren't good enough, AFAICT, since they only work when I can guarantee
> that no one else will insert a new record between the time that I insert
> one, and query for the last_value.
You evidently do not understand how currval works. It is session-local,
so the race condition you are imagining does not exist.
regards, tom lane