This is straight from Stack Overflow
As it was pointed out by @a_horse_with_no_name and @Jack Douglas, currval works only with the current session. So if you are ok with the fact that the result might be affected by an uncommitted transaction of another session, and you still want something that will work across sessions, you can use this:
SELECT last_value FROM your_sequence_name;
Use the link to SO for more information.
From Postgres documentation though, it is clearly stated that
It is an error to call lastval if nextval has not yet been called in the current session.
So I guess strictly speaking in order to properly use currval or last_value for a sequence across sessions, you would need to do something like that?
SELECT setval('serial_id_seq',nextval('serial_id_seq')-1);
Assuming, of course, that you will not have an insert or any other way of using the serial field in the current session.
currval()
is most definitely not discouraged.