Closed markpyb closed 1 day ago
It turns out to be a server side cache id conflict.
Server uses sequence Oid as a cache id, it is unique in the database, but not in the cluster.
But cache entry is for whole node, hence other clients connected to the same node may end up sharing same cache entry, regardless the database they are connected to, if they happen to have same Oid in their respective databases.
Simplest repro, one session connects to freshly created database:
seqtest1=# create sequence foo;
CREATE SEQUENCE
seqtest1=# select oid, relname from pg_class where relkind = 'S';
oid | relname
-------+---------
16384 | foo
(1 row)
seqtest1=# select nextval('foo');
nextval
---------
1
(1 row)
seqtest1=# select nextval('foo');
nextval
---------
2
(1 row)
seqtest1=# select nextval('foo');
nextval
---------
3
(1 row)
other session, connected to the same node, but other database creates brand new sequence, but it looks used:
seqtest2=# create sequence bar;
CREATE SEQUENCE
seqtest2=# select oid, relname from pg_class where relkind = 'S';
oid | relname
-------+---------
16384 | bar
(1 row)
seqtest2=# select nextval('bar');
nextval
---------
4
(1 row)
The problem affects clusters with gflag ysql_sequence_cache_method=server
.
By default ysql_sequence_cache_method=connection
.
Related to that, sequence cache may have stale entries from dropped sequences and dropped databases, and they may conflict with active sessions.
Jira Link: DB-11851
Description
When dropping a database with a sequence in it with ysql_sequence_cache_method=server, it is possible that the sequence cache is not invalidated, when the database is re-created with the application object (with a sequence) it leads to a situation where subsequent inserts can error with duplicate key value violation, on the identity key column
this seems present in 2024.1 and also 20.4
I observed it with a more complex test case , to observe the duplicate key value violation
but a much simpler repro is possible to observe the behaviour when dropping the database
Issue Type
kind/bug
Warning: Please confirm that this issue does not contain any sensitive information