Skip to content

executor: bound the partitioned FK parent step and type its deadlock - #657

Draft
Kiran01bm wants to merge 1 commit into
mainfrom
kiran01bm/pd102-partitioned-fk-parent-step
Draft

Kiran01bm wants to merge 1 commit into
mainfrom
kiran01bm/pd102-partitioned-fk-parent-step

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

The last step of adding a foreign key to a partitioned table online now holds its locks for a fixed number of statements however many partitions there are, and a deadlock there gets its own retryable code.

Why

pg-sprite adds a foreign key to a partitioned table one partition at a time, then finishes with a short step on the parent that locks the referenced table and every partition while it checks each partition's key. That check ran about three catalog queries per partition, so the lock was held longer the bigger the table (about 231 ms at 400 partitions), and if the step deadlocked with another session it was reported as a generic failure, though simply running it again would have worked.

What

  • The parent step's proof and read-back read the whole tree in one query each, with the partition OIDs as an array, mirroring the partitioned key build: column numbers through the key build's readKeyColumnsOf, foreign keys through a new readForeignKeysOf, and the adopted keys through a new readAdoptedLeafFKs. The proof rules (ST-7) are unchanged; a partition the batch returns nothing for fails closed.
  • A 40P01 from any statement in the parent step returns ErrPartitionedForeignKeyDeadlocked, code partitioned-foreign-key-deadlocked, Permanent() false, with the server's error kept in the chain. The rerun adopts every partition.
  • Tests: the parent step sends 12 statements under its locks at 3 and at 30 partitions, on PG 14 and 16; a real deadlock (a reader holding ACCESS SHARE on the referenced table, then inserting into the tree) returns the typed code, leaves the parent without the key and each partition's key unadopted, and a rerun adopts all of them. The deadlock test passed 10 runs under -race.
  • Docs: partitioned foreign key design, SchemaBot integration outcomes, execution model codes, LK-2 and ST-7 notes.

Before / after

The same parent step on a 400-partition events table referencing accounts, once in the normal case and once meeting another session's transaction that read accounts and then writes to events.

ALTER TABLE events ADD CONSTRAINT events_account_fk FOREIGN KEY (account_id) REFERENCES accounts (id)

Before, parent step (accounts ACCESS EXCLUSIVE, every partition SHARE ROW EXCLUSIVE)
  proof + read-back   ≈ 3 queries × 400 partitions        ≈ 231 ms under the locks
  deadlock (40P01)    → failed, execution-failed           (looks like "inspect first")

After, same locks
  proof + read-back   12 statements at 3 or 30 or 400 partitions
  deadlock (40P01)    → failed, partitioned-foreign-key-deadlocked, retryable
  rerun               → every partition's key found and adopted

The parent step ran about three catalog reads per partition while it held
the tree's locks, and a deadlock there failed as execution-failed. It now
reads the whole tree in a fixed number of statements, and a 40P01 gets
the retryable partitioned-foreign-key-deadlocked code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant