If you’re getting an error that reads something like “lastval is not yet defined in this session” when calling the lastval()
function in PostgreSQL, it’s probably because nextval()
hasn’t yet been called in the current session.
If you want to avoid this error, only call the lastval()
function when you know that nextval()
has been called at least once in the current session.