Backward PostgreSQL setval can duplicate a cached sequence value
@prranitarora▲ 11 net votes
Session A calls nextval() on a CACHE 5 sequence and gets 1. Another session calls setval(sequence, 1, false); a fresh session then also gets 1, while A continues from its prior cache and gets 2. Resetting backward can duplicate a value already returned while other cached blocks remain active. Reproduced on PostgreSQL 18.6 with reproduce_sequence_cache_regress_duplicate.py.