ADR-0040 — Include thread_id in the listing index
- Status: Accepted
- Date: 2026-11-30
- Supersedes: ADR-0029
- Authors: project maintainers
Context
ADR-0029 created the partial index opc_checkpoints_root_latest on
checkpoints (checkpoint_id) WHERE checkpoint_ns = '' in OPC_MIGRATIONS version 7. Thread
listing (src/open_postgres_checkpointer/_sql.py, list_threads_sql) orders root checkpoints by
checkpoint_id DESC, thread_id DESC and runs a NOT EXISTS probe keyed on c.thread_id to drop
older root checkpoints of the same thread.
With only checkpoint_id in the index, every candidate root checkpoint requires a heap fetch to
read thread_id for the NOT EXISTS check (and for tie-breaking on equal checkpoint ids, such as
copied threads), and a 3-column keyset cursor (checkpoint_id, thread_id, checkpoint_ns) < (...)
cannot push the full cursor comparison into the index condition.
Decision
We will index (checkpoint_id DESC, thread_id DESC) WHERE checkpoint_ns = '' on
opc_checkpoints_root_latest and pair it with a 2-column keyset cursor
(checkpoint_id, thread_id) < (%(after_id)s, %(after_thread)s) (_sql.LIST_THREADS_AFTER).
New migration version 8 (OPC_HEAD = 8) rebuilds opc_checkpoints_root_latest whenever
pg_get_indexdef lacks the (checkpoint_id DESC, thread_id DESC) key (or the index is invalid),
so databases already at version 7 upgrade cleanly in place. Version 7 becomes a no-op: a database
already past it never re-runs it, and a database below it (a new one, or one adopted from upstream)
then builds the index once, in version 8, instead of building the one-column shape and rebuilding
it.
Alternatives considered
- Keep the 1-column
(checkpoint_id)index — rejected because addingthread_id DESCaligns the index directly withlist_threads_sql'sORDER BYand 2-column keyset cursor and lets unfiltered scans readthread_iddirectly from the index for non-latest checkpoints (inferredfrom the PostgreSQL query plan). - Change version 7 to build the new index — rejected because
setup()returns early whenopc_migrationsalready records version 7, leaving existing databases stuck on the one-column index without detection. - Keep version 7 building the one-column index — rejected because every database below
version 7, including an upstream one being adopted, would then build the index twice over the
whole
checkpointstable.
Consequences
list_threadskeyset pages push(checkpoint_id, thread_id) < (...)directly into the index condition without incremental sorting on ties (measuredbytests/integration/test_read_plans.py).- Unfiltered
list_threadscan satisfyc.thread_idfrom the index duringNOT EXISTSchecks on non-latest root checkpoints (inferredfrom the index key columns). - Existing version-7 databases upgrade automatically on the next
setup()call, with a brief drop and concurrent rebuild ofopc_checkpoints_root_latestduring migration 8. - The partial index stores
thread_idalongsidecheckpoint_idfor root checkpoints, slightly increasing index size (inferred, not yet re-benchmarked on an uncontended host; confirm withjust bench-case listingandjust bench-case four-armbefore the general release).
References
- ADR-0029, ADR-0028
- thread-listing.md,
benchmarks/list_index.py