Skip to content

fix: a blocking detach locks the parent first, so a reader cannot deadlock it - #87

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/detach-locks-parent-first
Oct 1, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/detach-locks-parent-first

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Found while measuring a pg-partsmith recipe for Outpost's docs: retention under a live reader failed with deadlock detected.

Process 261: SELECT (SELECT count(*) FROM events WHERE time < '2026-10-01'), ...
Process 267: ALTER TABLE "public"."events" DETACH PARTITION "public"."events__2026_01"

The blocking detach took ACCESS EXCLUSIVE on the partition (_lock_partition, for the identity check and the marker) and only then asked for the parent in DETACH. A query through the parent locks the parent and then its partitions. A reader that got the parent in between waits for the partition, the detach waits for the parent, and after deadlock_timeout PostgreSQL fails one of them. Retention on a table with a DEFAULT partition always takes the blocking path, since DETACH CONCURRENTLY is refused there, so any table like Outpost's hits it under steady reads.

The detach now takes ACCESS EXCLUSIVE on the parent alone (LOCK TABLE ONLY) first, the lock DETACH takes anyway, and then the partition. That is the order readers use and the order PostgreSQL's own DETACH uses, and the order the attach path already follows (_lock_for_attach). The parent's lock is held a few catalog queries longer than before; readers and writers queue behind it exactly as they did behind the DETACH.

The test holds the parent with a reader that has yet to reach the partition, starts a blocking detach, waits until it is queued, then lets the reader read the partition and commit. Before the fix the detach fails with DeadlockDetectedError; after it, both finish.

  • unit: 2861 passed
  • integration (PG 17): 572 passed
  • ruff, mypy clean

…dlock it

The blocking detach took ACCESS EXCLUSIVE on the partition and then
waited for the parent in DETACH. A query through the parent locks the
parent and then its partitions, so a reader that got the parent in
between waited for the partition while the detach waited for the
parent, and PostgreSQL ended one of them with a deadlock. Retention on
a table with a DEFAULT partition takes this path every time, and with
steady reads the deadlock was likely, not rare.

The detach now takes ACCESS EXCLUSIVE on the parent alone first, the
lock DETACH takes anyway, then the partition: the order readers and
PostgreSQL's own DETACH use.
@codecov

codecov Bot commented Oct 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AlexeyShalaev
AlexeyShalaev merged commit 660e005 into master Oct 1, 2026
32 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/detach-locks-parent-first branch October 1, 2026 19:51
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