this is an old stack overflow, so maybe things have changed: insert - PostgreSQL function for last inserted ID - Stack Overflow
this is basically saying to use sequences directly since that is what postgresql is really doing anyway.
Using sequences directly is some extra code, but it will allow you to get the sequence prior to saving the rest of it. If not, you would get it after using one of the following:
currval()
(needs the sequence name) only works after an INSERT
(which has executed nextval()
), in the same session.
LASTVAL();
This is similar to the previous, only that you don't need to specify the sequence name: it looks for the most recent modified sequence (always inside your session, same caveat as above).