ADR-0043 — Migration statements wait one second for a table lock, then retry
- Status: Accepted
- Date: 2026-12-05
- Supersedes: none
- Authors: project maintainers
Context
Adopting in place, setup() runs on a database that is serving traffic. Our version 1 creates
opc_blob_patch with two foreign keys to checkpoint_blobs (_migrations.py, OPC_MIGRATIONS),
and two of upstream's versions are ALTER TABLE on checkpoint_blobs and checkpoint_writes.
Each needs a table lock that conflicts with transactions that wrote to the table, so it waits for
every such open transaction, and PostgreSQL makes every later statement on the table wait behind
it.
Nothing bounded that wait. With one transaction left open after an insert into
checkpoint_blobs, a later insert was cancelled by its own 3-second lock_timeout while
setup() waited, and setup() never returned: its 120-second deadline covered the migration
lock and the retries, not a statement waiting inside PostgreSQL
(tests/integration/test_setup_and_pool.py, both tests failed before this decision).
Decision
Each migration statement that is not CREATE INDEX CONCURRENTLY runs in its own transaction
with SET LOCAL lock_timeout = '1s'. On LockNotAvailable the pass pauses 2 seconds and
retries, until the existing 120-second deadline, then raises MigrationError naming the cause.
CREATE INDEX CONCURRENTLY statements run as before, with no limit.
Alternatives considered
- A limit on
CREATE INDEX CONCURRENTLYtoo — rejected: it takes no lock that blocks writers, and it must outwait every transaction older than it. A limit would cancel the build on any database with a transaction longer than the limit, leave an invalid index, and repeat the build on each retry. - A session
SET lock_timeout, reset afterwards — rejected: the connection is pooled, and a failed reset would leave the limit on it.SET LOCALends with the statement's transaction. - A caller-set limit — rejected: no caller has asked for another value, and
setup()takes no arguments today. - Document it and leave
setup()as it was — rejected: the stall is on the adopter's live tables and the hang has no bound.
Consequences
- While a transaction holds a table, later statements on it wait behind
setup()for at most 1 second in every 3, not without bound, andsetup()ends within its deadline. - A database whose tables are never free for one statement within 120 seconds now gets a
MigrationErrorwheresetup()used to wait until the table was free. Callingsetup()again continues: every statement is idempotent and recorded after it runs. - Each migration statement and its ledger row remain separate commits, as before.
References
src/open_postgres_checkpointer/_migrations.py(_apply,migrate)tests/integration/test_setup_and_pool.py- PostgreSQL:
lock_timeout