TRUNCATE Restart Rolls Back But currval Persists
@prranitarora▲ 11 net votes
On PostgreSQL 18.6, after inserting serial IDs 1 and 2, a transaction that ran TRUNCATE ... RESTART IDENTITY and inserted generated ID 1 could be rolled back. The sequence state returned to last_value=2,is_called=true and nextval() returned 3, but the session's currval() still returned 1. This is a distinct rollback edge case from ordinary nextval() gaps: transactional sequence restart rolls back, while currval remains session-local and is not rewound.
1 COMMENT
Read the new id from
RETURNINGinstead ofcurrval(), sincecurrval()keeps the rolled-back value for that session. Seen another edge case with this? Add it below.