Report
Two concurrent CREATE INDEX commands on the same partitioned table that both match one unattached partition index fail with a duplicate key error on pg_inherits_relid_seqno_index instead of the second creating its own partition index
- Severity
- 25
- Blamed commit
- 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99
On 10 branches
REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
CREATE INDEX on a partitioned table takes ShareLock on the table, its partitions and any existing partition index it reuses. ShareLock does not conflict with itself, so two CREATE INDEX commands on the same partitioned table can run at the same time. If a partition has an unattached index that matches both new parent indexes, both commands pick that same index and try to attach it. The second one fails with a raw unique violation on the pg_inherits catalog index.
The recent fix for ALTER INDEX ... ATTACH PARTITION racing with CREATE INDEX (rechecking has_superclass() after locking the child index) does not cover this case. The lock taken there is ShareLock, which a concurrent CREATE INDEX does not block, so the recheck still sees the index as unattached.
Reproduction
Setup:
CREATE TABLE p (a int) PARTITION BY LIST (a);
CREATE TABLE c1 PARTITION OF p FOR VALUES IN (1);
CREATE INDEX c1_a ON c1 (a);
Session 1:
BEGIN;
CREATE INDEX pi1 ON p (a);
-- do not commit yet
Session 2 (does not block on locks; it waits on the uncommitted pg_inherits row):
CREATE INDEX pi2 ON p (a);
Session 1:
COMMIT;
Session 2 then fails:
ERROR: duplicate key value violates unique constraint
"pg_inherits_relid_seqno_index"
DETAIL: Key (inhrelid, inhseqno)=(16390, 1) already exists.
Expected: pi2 is created, with a new index built on c1 (the same as when CREATE INDEX pi2 runs after session 1 has committed, which creates c1_a_idx and attaches it to pi2).
Analysis
DefineIndex() (indexcmds.c), when recursing to partitions, looks for an existing index on each partition that is not yet attached (has_superclass()) and matches the new index definition. It opens that index with the same lockmode as the parent (ShareLock for a non-concurrent build) and calls IndexSetParentIndex(). Two backends holding ShareLock on the same child index can both pass the attached check, both under the locked recheck too, because neither sees the other's uncommitted pg_inherits row. IndexSetParentIndex() then calls StoreSingleInheritance(), and the second insert blocks on the unique index pg_inherits_relid_seqno_index and errors once the first transaction commits.
The reuse of existing partition indexes, and this unlocked check-then-attach under a self-compatible lock, was introduced by commit 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ("Local partitioned indexes").
Notes
Tested on master 026b3b282a4a18801d22b85509dbf6868f48168e. Two CREATE INDEX commands on the same plain table can run concurrently; on a partitioned table they can't when a partition has a reusable index, and the error users see is an internal catalog unique violation.
Branches
Tested on master only. Blame 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 predates every supported branch, so REL_14 through master are expected to be affected (untested).
Related
- c5a0f8332ecd415182ae0db13615db56e29326f8 ("Recheck partition index attachment after locking it", BUG #19723) fixed the ALTER INDEX ... ATTACH PARTITION race with CREATE INDEX. It does not cover two concurrent CREATE INDEX commands.
- 01a1fb0b-ecd6-7769-8851-101a00f002ea: partitioned index validation lock-order deadlock (different mechanism).
- 01a1cbeb-fc5f-75e6-9767-03f967054166: ATTACH PARTITION leaves a parent index valid over an invalid subpartition index (different mechanism).