Report
Partitioned index validation locks the parent index before its table, deadlocking with foreign key creation and queries (pg_restore -j loses FKs or leaves indexes invalid)
- Severity
- 45
- 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
When ALTER INDEX ... ATTACH PARTITION attaches the last missing
partition of a partitioned index that is itself a partition of a
higher-level index, validatePartitionedIndex() marks the
intermediate index valid and then recurses to the parent. The
recursion takes AccessExclusiveLock on the parent index first and
then on the parent table. Everywhere else, including the
ATTACH PARTITION code itself, locks the table before its index.
A session that already holds any lock on the parent table and then needs one of its indexes therefore deadlocks with the attach. Adding a foreign key that references the partitioned table does exactly that, and so does an ordinary query in a transaction that has already locked the table.
Parallel pg_restore runs these steps concurrently: INDEX ATTACH
entries have no table dependencies, and FK CONSTRAINT entries
reference the partitioned table. When the deadlock hits, either the
foreign key is not created, or the attach is rolled back and the
partitioned unique index stays invalid at that level and every level
above it. pg_restore reports the error. Commit
2ebd684974375e9c68b43577b7e46a83597c8d95 recently fixed a related
race in this function by keeping the locks until commit, but not the
lock order.
Reproduction
CREATE TABLE pk (a int NOT NULL, b int NOT NULL)
PARTITION BY RANGE (a);
CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (0) TO (100)
PARTITION BY LIST (b);
CREATE TABLE pk1_0 PARTITION OF pk1 FOR VALUES IN (0);
ALTER TABLE pk ADD PRIMARY KEY (a, b);
CREATE UNIQUE INDEX pk_ba ON ONLY pk (b, a);
CREATE UNIQUE INDEX pk1_ba ON ONLY pk1 (b, a);
ALTER INDEX pk_ba ATTACH PARTITION pk1_ba;
CREATE UNIQUE INDEX pk1_0_ba ON pk1_0 (b, a);
CREATE TABLE fk (a int, b int);
Session 1:
BEGIN;
LOCK TABLE pk IN SHARE ROW EXCLUSIVE MODE; -- what ADD FOREIGN KEY
-- takes first
Session 2:
ALTER INDEX pk1_ba ATTACH PARTITION pk1_0_ba; -- blocks
Session 1:
ALTER TABLE fk ADD FOREIGN KEY (a, b) REFERENCES pk;
ERROR: deadlock detected
DETAIL: Process 1 waits for AccessShareLock on relation pk_ba;
blocked by process 2.
Process 2 waits for AccessExclusiveLock on relation pk; blocked by
process 1.
Using LOCK TABLE pk IN ACCESS SHARE MODE followed by
SELECT count(*) FROM pk WHERE b = 0 AND a = 1 in session 1 gives
the same deadlock, because planning opens pk_ba.
With pg_restore: create a three-level partitioned table with a
primary key and a second unique index (8 x 3 leaf partitions, 800k
rows), plus four tables with foreign keys referencing the primary
key. Dump with -Fd and restore with pg_restore -j8. Three out of
four runs reported, for example:
pg_restore: error: could not execute query: ERROR: deadlock detected
Command was: ALTER INDEX public.pk4_c_a_b_idx ATTACH PARTITION
public.pk4_2_c_a_b_idx;
The restored database then has an invalid mid-level index and an
invalid top-level unique index on the partitioned table. In one
-j4 run the foreign key entry was also a victim and the constraint
is missing.
Analysis
ATExecAttachPartitionIdx() documents its lock order:
To avoid deadlocks, obtain lock on the table before doing so on the index.
The recursion at the end of validatePartitionedIndex() does the
opposite:
parentIdx = relation_open(parentIdxId, AccessExclusiveLock);
parentTbl = relation_open(parentTblId, AccessExclusiveLock);
It holds the parent index while waiting for the parent table. Any
backend that holds a lock on the parent table and then opens that
index closes the cycle. ALTER TABLE ... ADD FOREIGN KEY locks the
referenced table with ShareRowExclusiveLock and later opens its
indexes, both while checking for a matching unique index and while
planning the validation query. Ordinary queries do the same in
AccessShareLock mode.
The recursion has been written this way since partitioned indexes were added in 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ("Local partitioned indexes"). Before 2ebd684974375e9c68b43577b7e46a83597c8d95 the locks were released at the end of the recursion, but the cycle already forms while the attach waits for the table lock, so releases without that change deadlock too.
pg_restore can't avoid this on its own.
identify_locking_dependencies() only considers TABLE and TABLE DATA
dependencies, and an INDEX ATTACH entry depends on two indexes,
not on the grandparent table it ends up locking.
A likely fix is to lock the parent table before the parent index in the recursion. The attach then waits for the table without holding the index, and the other session can finish.
Notes
Serial restore and pg_upgrade (which restores each database without
-j) are not affected. Hitting this needs at least three partition
levels, so that attaching a leaf validates an intermediate index that
is already attached to a higher one. With two levels the recursion
does not happen. Which side loses is a matter of timing: a lost
foreign key leaves referential integrity unenforced until it is
re-added, and a lost attach leaves the partitioned unique index
invalid.
Tested on master at 207b5293b8d9a217bb918b5b44774b3b4f1e2f89. The same index-before-table lock order in the recursion is present on all supported branches (2ebd684974375e9c68b43577b7e46a83597c8d95 was backpatched through REL_14_STABLE); back branches were not tested.