Repository navigation
Description
Measured on 1.5.0 against PostgreSQL 17.11: a plain events table with two million rows over the last year, swapped under a new parent as its DEFAULT partition the way guide/partition-existing-table.md says, with one writer inserting now() rows through events every 5 ms the whole time. Nothing else is running.
The first tick plans CREATE events__2026_09 / __2026_10 / __2026_11 (create_ahead). The two future months attach; the current month never does:
--- 3. the first tick: this month and two ahead, this month's rows move out of DEFAULT
Failed to attach after reconciliation retries
...
sqlalchemy.exc.IntegrityError: ... CheckViolationError: updated partition constraint for default partition "events_legacy" would be violated by some row
[SQL: ALTER TABLE "public"."events" ATTACH PARTITION "public"."events__2026_09" FOR VALUES FROM ('2026-09-01') TO ('2026-10-01')]
Attach failed after reconciliation; returned rows to DEFAULT partition
created 0, issues 0, error 'IntegrityError: ...'
live rows: 0 in this month's partition, 87 left in DEFAULT
writer: 19 inserts, p50 0.5 ms, longest 604 ms, 2 over 100 ms
The cause is the shape of _attach_with_reconcile (aio/services/execution.py, and the sync twin): the attach fails on the DEFAULT conflict, the window's rows are moved out of DEFAULT in their own transaction, the attach is retried in another. Between the move's commit and the attach's scan the writer has inserted one more row for the window, so the scan finds it and the attach fails again, DEFAULT_CONFLICT_MAX_RETRIES times in a row, and the rows are moved back. Under any steady write rate the current month cannot be attached, which is the one window every live table is writing into, and the state after the tick is the state before it: every insert keeps landing in DEFAULT. The run reports error set and issues empty; each retry costs the writer a full scan of the two-million-row DEFAULT under ACCESS EXCLUSIVE (the 604 ms above).
partition_data reaches the same window last and does worse: it fills the detached events__2026_09, fails the attach the same way, moves the rows back, and then raises the IntegrityError out of partition_data instead of reporting a move issue with complete=False, which is what the guide promises for a window it cannot handle. The eleven older months drained fine before that (lowest count 29764 of 129764 on the oldest month while it was mid-move, as the guide warns).
What I think the fix is: do the move and the attach in one transaction, under the lock the attach takes anyway. ATTACH already needs ACCESS EXCLUSIVE on the DEFAULT sibling for its scan, so BEGIN; LOCK TABLE <default> IN ACCESS EXCLUSIVE MODE; DELETE ... RETURNING / INSERT; ALTER TABLE ... ATTACH PARTITION ...; COMMIT holds the same lock for the move plus the scan and cannot lose the race; a writer waits for that transaction and is then routed to the new partition. The rows also stop needing a compensating move-back for this path, since a failed attach rolls the move back with it. For a large window the move itself can be long, so it may be worth draining most of the window in batches first (which partition_data already does) and taking the locked move-and-attach only for what is left, so the exclusive window is a batch and a scan, not the whole month. partition_data should report an attach it still cannot do as a move issue and return complete=False, never raise. The DEFAULT reconciliation section of concepts/execution.md and the guide's numbered list follow.
Lab: migrate_lab.py in https://github.com/bedrock-python/bedrock-python.github.io/tree/docs/production-python-series/docs/blog/lab/2026-09-07-partition-existing-table (the writer is Load._writer; steps 3 and 4 are the ones above).
Activity
AlexeyShalaev commentedon Sep 7, 2026
Reproduced on master with the lab,
postgres:17-alpine, 2M rows, the writer running: step 3 printscreated 0, issues 0, error 'IntegrityError: ... updated partition constraint for default partition "events_legacy" would be violated by some row'and leaves the current month unattached, and step 4 ends with that sameIntegrityErrorcoming out ofpartition_data, so the run never reaches the drain's end.The cause
The one you name.
_attach_with_reconcilemoves the window's rows out of DEFAULT inreconcile_default_rows' own transaction and retries theATTACHin another. That move holdsSHARE ROW EXCLUSIVEon the DEFAULT partition, so writers queue behind it — and are let in the moment it commits, which is exactly the gap before theATTACHtakes itsACCESS EXCLUSIVEand scans. One row is enough. WithDEFAULT_CONFLICT_MAX_RETRIES = 2the window then buys two full scans of a two-million-row DEFAULT underACCESS EXCLUSIVEand a compensating move-back, and converges on nothing.The design
The shape you propose, put where the library already puts a lock-and-then-several-statements step (the drop path).
A new repository operation,
reconcile_and_attach, doing in one transaction:LOCK TABLE ONLY <parent> IN SHARE UPDATE EXCLUSIVE MODE,LOCK TABLE <partition>andLOCK TABLE <default> IN ACCESS EXCLUSIVE MODE— the locksATTACHtakes anyway, parent first, so a concurrentATTACHon the same parent queues there rather than deadlocking against this one — then the sameDELETE … RETURNING/INSERTthe reconcile already uses, thenALTER TABLE … ATTACH PARTITION, then the identity re-checks and the orphan-marker clearing the plain attach does. Nothing can put a row into DEFAULT between the move and the scan, because nothing has been able to touch DEFAULT since before the move. And a failed attach rolls the move back with it, so this path needs no compensating move-back at all._attach_with_reconcilekeeps its shape. Attempt 1 is still the plain, cheapattach_partition. On a DEFAULT conflict it still does the bulkreconcile_default_rowsexactly as today — underSHARE ROW EXCLUSIVE, which leaves readers of DEFAULT alone — and attempt 2 isreconcile_and_attach, which takes only what the writer added in the meantime, plus the scan. That is your "drain most of it in batches first, take the locked move-and-attach only for what is left": the exclusive window is a tail and a scan, not a month.partition_datagets it for nothing, since itsfillhas already drained the window in batches and the tail is whatever landed during the last one.The second half of the report is the other half of the fix: a window that still cannot be attached has to be an issue, not an exception. On the last retry the executor re-raises the driver's
IntegrityErrortoday; it will raisePartitionTopologyErrorwithdefault_holds_rowsinstead — whichapply()already records inresult.issuesand goes on from, which is what the failures table inconcepts/execution.mdhas always promised — andpartition_datawill catch it and return amoveissue withcomplete=Falserather than let it out. That also covers the one case the lock cannot reach: a foreign DEFAULT partition, whichLOCK TABLErefuses outright.What I rejected
ACCESS EXCLUSIVE.ACCESS EXCLUSIVEon DEFAULT for the length of a month's rows, which blocks readers of DEFAULT too — where today'sSHARE ROW EXCLUSIVEbulk move leaves them alone.reconcile_and_attachis an addition to thePartitionRepositoryprotocol, so an implementation written from scratch gains one method; subclasses ofPostgresPartitionRepositoryare untouched. Both mirrors get the change and the same test — an integration test with a writer inserting into the window's hot end while the attach runs, which fails on master and passes with this.concepts/execution.md,guide/partition-existing-table.mdandagents.mdfollow.AlexeyShalaev commentedon Sep 7, 2026
Shipped in #65.
The move and the attach now share a transaction. A new repository operation,
reconcile_and_attach, takesEXCLUSIVEon the parent andACCESS EXCLUSIVEon the partition and on the DEFAULT sibling, moves what is left of the window, and attaches. Nothing can put a row into DEFAULT between the move and the scan, and a failed attach rolls the move back with it — so no compensating move-back on that path either. The bulk of the window still moves first in the reconcile's own transaction, under the lighterSHARE ROW EXCLUSIVEthat leaves readers alone, so what the exclusive lock covers is a tail and a scan, not a month. That is your "drain most of it in batches first, take the locked move-and-attach only for what is left";partition_datagets it for nothing, since itsfillhas already done the batches.One thing came out different from the sketch, and it is worth knowing about. I first took
SHARE UPDATE EXCLUSIVEon the parent — whatATTACHitself takes — and the new test failed, but on the writer's insert rather than on the attach:An INSERT picks its partition from the set it saw when it took
ROW EXCLUSIVEon the parent.SHARE UPDATE EXCLUSIVEdoes not conflict with that, so an insert can route to DEFAULT, queue on the lock the attach holds over it, and come out of the wait still aimed at DEFAULT — where the constraint the attach just narrowed rejects it. It does not re-route; the write is lost. That is what a plainATTACHdoes to a live writer today, and it only became visible here because the attach finally succeeds.EXCLUSIVEon the parent — one level up, conflicting withROW EXCLUSIVEbut not withACCESS SHARE— makes the insert wait before it chooses, so it re-plans and lands in the new partition with its statement unchanged, while readers of the other partitions carry on. It is written up indesign/postgresql-semantics.md.And the second half of your report: a window that still cannot be attached is now a
default_holds_rowsfinding rather than the driver's exception.apply()records it inresult.issuesand the run goes on — what the failures table promised all along — andpartition_datareturns amoveissue withcomplete=Falseinstead of raising. That also covers the one case the lock cannot reach: a foreign DEFAULT partition, whichLOCK TABLErefuses outright.Your lab, unmodified, against the branch:
No writer errors in either step, no issues, nothing raised. The tests that hold this are integration tests in both mirrors with a writer inserting into the window while the attach runs — a task in the aio suite, a thread in
tests/integration/sync/test_concurrency.py. Against master's library sources all four fail with theCheckViolationyou reported; against the branch they pass.Nothing to change on your side. The
PartitionRepositoryprotocol gainsreconcile_and_attach, so only an implementation written from scratch (rather than a subclass ofPostgresPartitionRepository) has one method to add;guide/extending.mddescribes its contract.One more thing the lab turned up on the way, unrelated to this: step 6 dies on
DROP TABLE events_legacybecauseBIGSERIALleaves the sequence owned by the old table while every partition'siddefault draws from it.ALTER SEQUENCE events_id_seq OWNED BY events.idbefore the drop, and it goes through with the ids intact —CASCADEtakes the sequence and every default with it, and the next insert fails onNOT NULL. That is now a note in the guide's last step.AlexeyShalaev commentedon Sep 7, 2026
Shipped in #65, which is merged. The lab's step 3 now attaches the current month on the first tick with a live writer inserting into it, and
partition_datano longer raises on that window. A release goes out shortly; I will re-measure the lab against it.