Skip to content

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 adding thread_id DESC aligns the index directly with list_threads_sql's ORDER BY and 2-column keyset cursor and lets unfiltered scans read thread_id directly from the index for non-latest checkpoints (inferred from the PostgreSQL query plan).
  • Change version 7 to build the new index — rejected because setup() returns early when opc_migrations already 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 checkpoints table.

Consequences

  • list_threads keyset pages push (checkpoint_id, thread_id) < (...) directly into the index condition without incremental sorting on ties (measured by tests/integration/test_read_plans.py).
  • Unfiltered list_threads can satisfy c.thread_id from the index during NOT EXISTS checks on non-latest root checkpoints (inferred from the index key columns).
  • Existing version-7 databases upgrade automatically on the next setup() call, with a brief drop and concurrent rebuild of opc_checkpoints_root_latest during migration 8.
  • The partial index stores thread_id alongside checkpoint_id for root checkpoints, slightly increasing index size (inferred, not yet re-benchmarked on an uncontended host; confirm with just bench-case listing and just bench-case four-arm before the general release).

References