Skip to content

3.x.x: let migrate --offline-indexes build 013's and 024's indexes with a plain parallel CREATE INDEX in one transaction when no instance is live - #745

Merged
RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/rc5-offline-index-builds
Oct 8, 2026
Merged

RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/rc5-offline-index-builds

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Summary

PR-B of PRISM rc.5 (design: section B of rc5-plan-u3.md, "(d2) plain parallel CREATE INDEX for 013 (and 024) when no instance is live").

The W1 cutover's migrate step runs with every frontend stopped, yet 013 and 024 build their indexes the online way: CREATE INDEX CONCURRENTLY reads the share ledger twice and waits out every older snapshot, and DROP INDEX CONCURRENTLY runs statement by statement. This adds an opt-in offline build for that case:

qbit-prism-server migrate --offline-indexes [--index-build-workers N] [--index-build-memory SIZE]
# N defaults to 4. SIZE defaults to 2GB, or the server's maintenance_work_mem when that is higher.

With the flag, each index migration (013, then 024) runs its usual plan in one transaction, with a plain parallel CREATE INDEX of each rendered definition and a plain DROP INDEX, and records its version as that transaction commits. Without it, nothing changes: migrate and every frontend build concurrently as before.

It composes with #743's --defer-share-hashes. The W1 cutover's migrate step runs qbit-prism-server migrate --defer-share-hashes --offline-indexes:

  • the recent range is mapped first and serving permitted (fence 2), before 013 drops the index that serves the range's bounds;
  • 013 and 024 then build offline, with the backfill pending.

A gated test runs exactly that. The plan expects the outage to drop from about 3.7 min to 1.5 to 2 min. That is unmeasured; the pair rehearsal should measure it (below).

No migration number is taken. Migration 013's SQL and its registration (NATIVE_MIGRATIONS, ONLINE_MIGRATIONS, derive_online) are byte-identical.

Design

The opt-in is the operator CLI's alone. Command::Migrate { offline_indexes, index_build_workers, index_build_memory }. There is no environment variable, because compose frontends run the same online-migration loop with PRISM_POSTGRES_INIT_SCHEMA=1, and they must stay on CONCURRENTLY.

  • --index-build-workers (default 4) is limited to 0..=1024, PostgreSQL's limit for max_parallel_maintenance_workers.

  • --index-build-memory takes a size as PostgreSQL writes one, parsed and bounded as PostgreSQL parses maintenance_work_mem:

    • a number, whole or not, then optionally B, kB, MB, GB or TB (case-sensitive; kB without one);
    • rounded to whole kilobytes, and refused outside 1MB..2147483647kB.

    So migrate refuses a bad size before it connects, not after the migrations ahead of the build. PostgreSQL also takes an exponent such as 1e3MB; the parser refuses that, which is only stricter.

  • Both options requires = "offline_indexes".

Plumbing, composed with #743. pub struct MigrateOptions { pub share_hashes: ShareHashBackfill, pub index_build: IndexBuildMode }:

migrate's CLI builds the options from --defer-share-hashes (Defer, otherwise Finish) and --offline-indexes/--index-build-*. IndexBuildMode { Concurrent, Offline { workers: u16, memory_kb: Option<u32> } } lives in online.rs. Both types are pub, re-exported from ledger, because the gated integration tests call connect_migrate.

online.rs. apply keeps the session setup (statement and lock timeouts 0), the runner lock and the recorded() short-circuit, then dispatches on the mode. Both modes share the rest:

  • plan(), the preflight factored out of apply;
  • run_plan(), the one Keep/Build/Rebuild/Drop/Dropped step loop. Only a concurrent step's changes go into the progress the refusals report; an offline step's roll back with its transaction;
  • record(): verify_declared, the version's insert and the commit.

The modes differ only in their setup and statements:

  • apply_concurrently runs the plan on the session, then records in a fresh transaction under MIGRATION_LOCK.
  • apply_offline does everything in one transaction:
    1. It takes MIGRATION_LOCK. That wait has no timeout, as in the online runners' record step.
    2. It sets, for the transaction only: lock_timeout 5 s, max_parallel_maintenance_workers to the requested workers, and maintenance_work_mem to --index-build-memory. Without that flag it is 2GB, unless the server's is higher, in which case the server's is kept.
    3. Before any DDL, it refuses an instance that has not reported drained or stopped (refuse_unquiesced_instances, which keeps qbit_prism_instances locked until commit) and a live legacy writer lease (refuse_live_legacy_lease, factored out of migrate_schema unchanged).
      • Only those refusals carry the "no instance or legacy writer may be live" context. Its remedy names plain qbit-prism-server migrate, which builds CONCURRENTLY and records without stopping frontends.
      • A database error from the guards' own statements, such as a lock timeout behind a heartbeat in flight, surfaces as itself under a neutral "could not check for live instances and legacy writers" context.
    4. It runs LOCK TABLE … IN ACCESS EXCLUSIVE MODE on the distinct tables, in name order.
      • Only a lock timeout (55P03) gets the "a transaction still uses the table … nothing was changed" text (lock_failure).
      • A deadlock, cancel, termination or missing table surfaces as itself under "could not lock for migration N's offline index build".
    5. It runs the shared plan: the definition verbatim behind a CREATE INDEX prefix check, and a plain DROP INDEX after drop_planned's re-lookup.
    6. It records.

The run logs the workers it asked for, maintenance_work_mem, max_parallel_workers and max_worker_processes before it builds. Each build's index built line carries its elapsed_ms.

Anything that ends an offline migration's transaction before its commit rolls that migration back whole: no invalid index, no partial drop, no record. The migrations the same migrate recorded before it stay recorded, and the messages say so. A rerun without the flag falls back to CONCURRENTLY. 016/017 and the fingerprint don't notice the build mode: the swap checks only to_regclass and indrelid, and pg_get_indexdef renders a plain build the same way as a concurrent one. 024 gets the mode for free.

Memory. A build's sort takes about maintenance_work_mem in all, not that much per worker. PostgreSQL treats it as the limit of the whole parallel utility command and divides it among the leader and the workers, each needing at least 32 MB or the build launches fewer workers. I checked this on PG16 with trace_sort: with 64MB and 4 workers requested, the build launched 1 worker, each participant's sort got workMem = 32768 kB, and the leader's merge used 65536 kB. The docs say so and ask the operator to size --index-build-memory to the memory the database host has free.

Docs.

  • docs/prism-rust-migration.md:
    • The migration overview mentions the flag.
    • The 013 section gains two paragraphs:
      • what the offline run does and refuses, and starting frontends only after migrate returns;
      • plain migrate as the way out of a refusal or of a built-but-unrecorded leftover while frontends serve;
      • the worker-slot needs, --index-build-memory and the memory need.
    • The isolated-restore rehearsal's time qbit-prism-server migrate gains --offline-indexes.
  • docs/prism-ledger-ops.md:
    • The 024 upgrade paragraph mentions the flag.
    • The runbook's repair-forward time qbit-prism-server migrate gains --offline-indexes.
  • Doc comments: the online.rs module doc (header and offline paragraph), OnlineMigration::Indexes, ONLINE_MIGRATIONS, IndexBuildMode, MigrateOptions and connect_migrate, plus a call-site comment in connect_inner.

Tests

New gated tests in tests/support/index_trim.rs (listed in test/prism-gated-tests.txt). They watch an offline run through a probe of two event triggers in the fixture database. The probe records each index the test schema creates or drops, with current_query() and the build settings in force, in a table whose rows commit or roll back with the statement's transaction. It can hold a statement, still inside the run's transaction, until the test releases it.

  • T1 migration_013_offline_builds_plain_indexes_in_one_transaction_with_every_instance_stopped
    • Setup: 013 and 024 undone, and an interrupted concurrent build's invalid index left under one of the names. Every instance is stopped.
    • While the run is held in its first build, the sampler sees the rendered definition verbatim and the run holding ACCESS EXCLUSIVE on qbit_share_ledger. An append waits on it (pg_blocking_pids).
    • After release, the index set is the trimmed set and the append landed.
    • Every statement logged for 13 shares the xmin of 13's record, in plan order with the expected text and the settings 5s 3 2GB: build, the rebuild's drop and build, and four plain drops. 024's build shares 24's record.
    • No logged statement is concurrent.
  • T2 migration_013_offline_refuses_a_live_instance_or_legacy_lease_before_any_ddl
    • A registered instance is refused, then, with it stopped, a live legacy lease.
    • Both refusals come while an open writer holds the ledger, so a run that locked the table first would fail with 55P03 instead.
    • Nothing is changed (indexes with OIDs, the record). Then the run goes through.
  • T3 migration_013_offline_fails_fast_behind_an_open_writer_and_changes_nothing
    • Behind an open writer, then behind an open reader, the run fails with 55P03 and the lock-timeout text, after at least the 5 s lock timeout and within the test's 60 s cap.
    • Nothing is changed and the writer commits. Then the run goes through.
  • T4 migration_013_offline_rolls_back_a_partial_run_whole
    • The run is held at its second drop, with both builds and the first drop done, and its backend is terminated.
    • The index set equals the one before, OIDs included, 13 is unrecorded and the probe's rows are gone.
    • A start without the flag then builds concurrently.
  • T7 migration_013_offline_takes_the_operators_build_memory_and_otherwise_never_lowers_the_servers
    • The database's default maintenance_work_mem is set to 3GB.
    • Without the flag the builds see 3GB. With memory_kb 64MB they see 64MB.
  • T8 migration_013_offline_reports_a_lock_timeout_in_its_guards_as_itself
    • A heartbeat in flight holds qbit_prism_instances. The run fails with 55P03 under "could not check for live instances and legacy writers", never as a live instance.
    • Nothing is changed. Then the run goes through.

Unit tests:

  • T5 an_offline_build_runs_the_rendered_definition_verbatim pins the offline and concurrent statements, the drop statements and the default mode.
  • only_a_lock_timeout_reads_as_a_transaction_holding_the_table covers 55P03 against 40P01, 57014, 57P01, 42P01 and none.
  • index_build_memory_is_parsed_and_bounded_as_postgres_does (in tools.rs): sizes in kilobytes with fractions, whitespace and every unit; case-sensitive units refused; the 1MB..2147483647kB bounds.

config_cli.rs:

  • T6:
    • migrate --offline-indexes --index-build-workers 8 and migrate --offline-indexes --index-build-memory 1.5GB reach PostgreSQL (database_only_cli_commands_reach_postgres_without_reading_seeds).
    • offline_indexes_is_a_switch_of_migrate_alone pins the switch's refusals and help.
  • index_build_memory_is_checked_before_connecting:
    • refuses the size without the switch, 2gb, and 512kB;
    • migrate --help documents the flag.

The cutover's migrate step, in tests/support/share_hash_backfill.rs, built on #743's 2.x.x fixtures and the probe: migrate_defer_share_hashes_with_offline_indexes_permits_serving_in_one_run.

  • It runs MigrateOptions { share_hashes: Defer, index_build: Offline } once on a populated 2.x.x source.
  • The recent range is mapped exactly, serving is permitted (fence 2), and the cursor stays where it was planned. Every migration but 2 is recorded.
  • 013's two builds (verbatim) and four plain drops share 13's recording transaction, and 024's build shares 24's. No statement anywhere is a concurrent build or drop.
  • A frontend started with initialize then leaves the backfill alone, passes the start gate and appends a share.

Also:

  • a unit test, migrate_takes_the_deferred_backfill_and_the_offline_indexes_together, parses both flags into one command;
  • config_cli reaches PostgreSQL with migrate --defer-share-hashes --offline-indexes.

T1 to T6 pass as they were. The only change to them is mechanical: the offline() helper and T5's constructor gained memory_kb: None for the new field. T1, T4 and T7 create event triggers, so like the failover tests (ALTER SYSTEM) they need the test role to be a superuser, which CI's prism_test is.

Existing tests are unchanged and stay green, since the default stays CONCURRENTLY: index_trim, fanout_lane_plan, config_cli and the online.rs unit tests.

Local gates on the rebased commit:

GATES3

Mutation checks, one per safety property (each restored, git diff verified identical afterwards):

Mutation Fails
M1: drop the instance refusal T2: the run falls through to the table lock and times out behind the open writer instead of refusing
M2: build CONCURRENTLY offline T5 (statement not verbatim) and T1 (CREATE INDEX CONCURRENTLY cannot run inside a transaction block)
M3: drop the 5 s lock timeout T3 (waits behind the open writer past its 60 s cap) and T1 (the probe sees lock_timeout 0)
M4: run the plan outside the migration's transaction T4 (the first drop had already committed) and T1 (the build does not hold the share ledger)
M5: drop the legacy lease refusal T2
M6: refuse only after the table lock T2 (55P03 behind the writer instead of the refusal before any lock)
M7: drop requires = "offline_indexes" from --index-build-workers T6 (--index-build-workers 8 alone gets past clap)
M8: map every LOCK TABLE failure to the lock-timeout text (fix 2) only_a_lock_timeout_reads_as_a_transaction_holding_the_table (40P01 read as a lock timeout)
M9: give the guards' database errors the live-instance refusal (fix 3) T8 (the heartbeat's lock timeout read as a live instance)
M10: never lower maintenance_work_mem, even for --index-build-memory (fix 1) T7 (the build saw 3GB, not the operator's 64MB)
M11: connect_inner drops the index build mode (always concurrent) the cutover test (013's builds outside 13's transaction) and T1
M12: connect_inner drops the share-hash mode (always Finish) the cutover test (the whole backfill mapped, not the recent range)

M1 to M10 ran on the review round's commit, 69955066. The rebase changed none of the code they mutate. M1, M3, M5 and M6 are the checks the fixes touch, and M8 is the new one for fix 2. M2 and M4 were re-targeted at the refactored code. M9 and M10 cover fixes 3 and 1. M11 and M12 ran on the rebased commit and check that the composed options reach both runners.

Review

A /code-review high of the first version found nine items.

Fixed:

  1. maintenance_work_mem could not be lowered. --index-build-memory (above) sets it. The "never lower a larger server setting" rule now applies only when the flag is absent (T7, the parser unit test, the config_cli test). The memory need is documented as about maintenance_work_mem per build in all; see Memory above for why it is not × (workers + 1).
  2. Every LOCK TABLE failure read as a lock timeout. Only 55P03 does now; anything else surfaces as itself under a neutral context (lock_failure, its unit test, mutation M8).
  3. The guards' "live instance" context covered database errors too. It covers only their refusals now; their database errors surface as themselves (T8, mutation M9).
  4. The rollback message claimed nothing of the run stays. It now says this migration's changes roll back and the migrations this run recorded before it stay recorded (also in the docs).
  5. Two copies of the step loop. Both modes run one run_plan and one record, behaviour-identical: the index_trim and fanout_lane_plan tests and T1–T6 pass as they were.
  6. Stale docs and a throwaway allocation. OnlineMigration::Indexes' doc and the module header mention the offline mode, and the offline path checks the CREATE INDEX prefix instead of building and discarding a concurrent statement.

Declined:

Risks

  • ACCESS EXCLUSIVE on qbit_share_ledger for the whole build. Every append and read waits for it, so it is safe only behind the guard. The guard refuses unquiesced instances and a live legacy lease before any DDL. The 5 s lock timeout turns a missed writer, a running export or a reader into a fast failure that changes nothing.
  • Tools register no instance. A running import-audits or self-check is not refused by name. Its open transaction on the table ends the run at the lock timeout. If it is between transactions, its next statement waits for the build or fails on its own timeouts. The docs and the flag's help say to stop every frontend and tool.
  • Starting frontends. A frontend started during the run fails its startup. With PRISM_POSTGRES_INIT_SCHEMA=1 it waits on the migration lock until its 5 s lock timeout. Otherwise the start gate finds 13 unrecorded. So start frontends only after migrate returns (documented). A drained instance that is still running blocks on qbit_prism_instances for the run's duration, so its heartbeats fail meanwhile.
  • Memory and parallelism.
    • A build's sort takes about maintenance_work_mem: 2GB by default, or the server's when that is higher. Size it to the host's free memory with --index-build-memory.
    • Workers come out of max_worker_processes and max_parallel_workers, at least 32 MB of the memory each. Otherwise the build silently uses fewer workers or none: slower, not wrong.
    • Both are documented, and the settings are logged.
  • Runner lock waits. migrate --offline-indexes still takes the session runner lock first. If a frontend's concurrent runner is mid-build, the offline run waits for it, then finds the version recorded, as plain migrate does today.

What the pair rehearsal should measure

  • time qbit-prism-server migrate --offline-indexes against plain migrate on the same restore. That gives the migrate total and the outage delta the plan expects, about 3.7 min down to 1.5 to 2 min.
  • Each build's index built elapsed_ms for 013's two indexes and 024's, and the dropped replaced index lines.
  • The logged workers, max_parallel_workers, max_worker_processes and maintenance_work_mem, to confirm the parallel workers were really granted. If they weren't, compare with --index-build-workers 2, 4 and 8, the server's slots, and --index-build-memory (it needs at least 32 MB per participant).
  • The database host's free memory during the builds and the disk I/O. Choose --index-build-memory from that for the production run.
  • That frontends started after migrate returned come up normally, and that self-check and the recovery evidence cmp pass afterwards (the index set and fingerprint are unchanged).
  • Optionally, the lock-timeout path: with one writer deliberately left open, the run should fail in about 5 s with nothing changed.

Merge order and local suite

FULL_SUITE_RESULTS

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T23:10:34.533313Z 118b512 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…th a plain parallel CREATE INDEX in one transaction when no instance is live

The W1 cutover's migrate step runs with every frontend stopped, yet 013
and 024 build their indexes the online way: CREATE INDEX CONCURRENTLY
reads the share ledger twice and waits out every older snapshot, and
DROP INDEX CONCURRENTLY runs statement by statement. The plan puts the
gain of building them plainly at about 3.7 down to 1.5 to 2 minutes of
outage, to be measured in the pair rehearsal.

`qbit-prism-server migrate --offline-indexes [--index-build-workers N]
[--index-build-memory SIZE]` runs each index migration's plan in one
transaction instead. N defaults to 4, at most PostgreSQL's 1024. SIZE
is a maintenance_work_mem as PostgreSQL writes it, checked before
connecting; without it the run takes 2GB, or the server's setting when
that is higher. A build's sort takes about that much in all, divided
among its leader and workers.

The transaction takes the migration lock and sets a 5 s lock timeout,
the workers and the memory for itself. Before any DDL it refuses an
instance that has not reported drained or stopped and a live legacy
writer lease, naming plain migrate, which builds CONCURRENTLY and
records without stopping frontends, as the way out. It then locks the
migration's tables ACCESS EXCLUSIVE in name order, runs each rendered
definition verbatim as a plain build, drops with a plain DROP INDEX,
verifies the declared set and records the version as it commits. Only a
lock timeout reads as a transaction still holding a table; any other
failure, in the guards or the table lock, surfaces as itself. An
interrupted migration rolls back whole, so no invalid index and no
partial drop remain while the migrations recorded before it stay
recorded, and a rerun without the flag builds concurrently.

The switch is the operator CLI's alone. MigrateOptions carries it beside
#743's share-hash backfill mode, so migrate takes --defer-share-hashes
and --offline-indexes together, as the W1 cutover's migrate step runs
them: the recent range is mapped first, before 013 drops the index that
serves it, and 013 and 024 then build offline with the backfill pending.
Ledger::connect_migrate takes the options, or a ShareHashBackfill alone
as before, through #743's shared operator connect; every other connect
passes the default, so frontends with PRISM_POSTGRES_INIT_SCHEMA=1 run
the same loop and stay on CONCURRENTLY. 013's SQL and registration are
unchanged and no migration number is taken. Both modes share the
preflight plan, the step loop and the record, and the legacy lease
refusal is factored out of migrate_schema.

Six gated index_trim tests watch offline runs through event triggers:
the rendered definitions run verbatim in the transaction that records
the version while an append waits; a live instance or legacy lease is
refused before any DDL; an open writer or reader ends the run with
55P03 and nothing changed; a terminated run rolls back whole, after
which a start without the flag builds concurrently; the operator's
build memory is taken even below the server's, which is otherwise
never lowered; and a lock timeout inside the guards reads as itself.
A gated share_hash_backfill test runs the cutover's migrate step, both
flags in one run on a populated 2.x.x source: the recent range is mapped
at fence 2, 013 and 024 build offline with nothing concurrent, and a
frontend then starts and serves.
Unit tests pin the statements, the lock failure classification and the
memory parser, and config_cli pins the flags. The docs describe the
mode, its worker and memory needs and starting frontends only after
migrate returns, and the isolated-restore runbooks pass the flag.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RobertClarke
RobertClarke force-pushed the RobertClarke/rc5-offline-index-builds branch from 6995506 to 118b512 Compare October 7, 2026 23:08
@RobertClarke
RobertClarke merged commit 2d103c3 into 3.x.x Oct 8, 2026
73 of 79 checks passed
@RobertClarke
RobertClarke deleted the RobertClarke/rc5-offline-index-builds branch October 8, 2026 00:36
RobertClarke added a commit that referenced this pull request Oct 8, 2026
…n throttled batches, and let the evidence export and self-check read it pending

migrate --defer-share-hashes leaves migration 2's share-hash backfill
pending at share_hash_backfill_pending = 2 while frontends serve. The
new qbit-prism-server backfill-share-hashes maps the rest and records
2. Plain migrate on such a database now takes the same path, at the
default throttle, instead of rc.4's unthrottled batches, with or
without #745's --offline-indexes, which changes only how 013 and 024
build.

A Throttle bounds every batch: at most --max-batch share_seq values
(5,000), one statement in a transaction of its own, cancelled at
--statement-timeout-ms (2 s, at most 5 s) and retried at half its size,
and a rest of took * (1 - d) / d after each, where d is --duty-cycle
(0.5). Every native append updates the cluster row under ORDER_LOCK,
and a snapshot held on the primary keeps that row's dead versions
(#738), so no statement holds one longer than its timeout, and none is
held between batches. Each bound is checked in one place, which the
flags' parsers call before anything connects. The chunked double-credit
check that runs before 2 is recorded keeps to the same throttle. A
batch or chunk that times out at its smallest size says to run
backfill-share-hashes with a smaller --max-batch, or else a larger
--statement-timeout-ms up to the cap; plain migrate's error names that
command, as migrate takes no throttle flags.

While frontends serve, the transaction that records 2 waits at most
2 s for the migration lock or the cursor table's lock, and its
statements keep to the throttle's timeout. An attempt that waits longer
is rolled back, holding nothing, logged, and tried again after a
backoff from 2 s doubling to 30 s, 20 times, about ten minutes in all.
The run then stops, naming what held it and to run
backfill-share-hashes again, which goes straight to the double-credit
check and the record. The record of a backfill before anything serves
is unchanged.

The command refuses, before it maps anything, a backfill that does not
permit serving, and it shares A1's checks under the runners' lock: the
fence and the claim, 017's conversion bound, the frozen end and the
double-credit refusal. On a database whose backfill has finished it
maps nothing and succeeds, so a retry after a lost reply is safe. It
prints what it did as JSON, already_complete included.

The recovery evidence export now reads a database at fence 2, as every
start does. It exports the cursor as one share_hashes_deferred record
in place of the partial mapping, and the summarizer marks the
share_hashes kind deferred with it and says so in a note. The preamble,
which every parallel session runs, reads only the catalog, and the
cursor is read last, after the integrity report, so an export holds the
cursor table's lock only until it commits, right after. The refusal of
a cursor at fence 1 names migrate --defer-share-hashes to serve and
plain migrate to finish; one that a build before #669 started, with no
fence, names plain migrate alone. A complete export's summary is
unchanged, and the parallel export and summarizer stay byte-identical
to the serial ones. The export checks each capability's value against
the highest that startup accepts, from one declaration that a unit test
compares with NATIVE_CAPABILITIES.

self-check samples the backfill beside the heartbeats, in one read of
the cursor and its fence under one 5 s deadline, and reports it in a
share_hash_backfill field with a warning line, never as a failure:
"pending" with the fence and the cursor, or "unknown" with why it could
not be read, a connection failure's text withheld. The field is absent
only when the read succeeded and nothing is pending. The start gate's
warning, migrate --defer-share-hashes's output and the share-archive
refusals while the backfill is pending name backfill-share-hashes as the
way to finish it. The migration guide and the ledger runbook describe
when and how to run it after go-live, its throttle, how to watch it,
the record's retries, and how to compare evidence while it is pending.

The recovery test helpers now refuse an export script edited after the
test binary was built. The serial export runs the copy compiled in,
while the parallel exporter and the summarizer read the file, so a full
suite whose script changed under it compared two scripts' exports: the
deferred export test once failed so, at the line the edit had moved.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 8, 2026
…n throttled batches, and let the evidence export and self-check read it pending (#746)

migrate --defer-share-hashes leaves migration 2's share-hash backfill
pending at share_hash_backfill_pending = 2 while frontends serve. The
new qbit-prism-server backfill-share-hashes maps the rest and records
2. Plain migrate on such a database now takes the same path, at the
default throttle, instead of rc.4's unthrottled batches, with or
without #745's --offline-indexes, which changes only how 013 and 024
build.

A Throttle bounds every batch: at most --max-batch share_seq values
(5,000), one statement in a transaction of its own, cancelled at
--statement-timeout-ms (2 s, at most 5 s) and retried at half its size,
and a rest of took * (1 - d) / d after each, where d is --duty-cycle
(0.5). Every native append updates the cluster row under ORDER_LOCK,
and a snapshot held on the primary keeps that row's dead versions
(#738), so no statement holds one longer than its timeout, and none is
held between batches. Each bound is checked in one place, which the
flags' parsers call before anything connects. The chunked double-credit
check that runs before 2 is recorded keeps to the same throttle. A
batch or chunk that times out at its smallest size says to run
backfill-share-hashes with a smaller --max-batch, or else a larger
--statement-timeout-ms up to the cap; plain migrate's error names that
command, as migrate takes no throttle flags.

While frontends serve, the transaction that records 2 waits at most
2 s for the migration lock or the cursor table's lock, and its
statements keep to the throttle's timeout. An attempt that waits longer
is rolled back, holding nothing, logged, and tried again after a
backoff from 2 s doubling to 30 s, 20 times, about ten minutes in all.
The run then stops, naming what held it and to run
backfill-share-hashes again, which goes straight to the double-credit
check and the record. The record of a backfill before anything serves
is unchanged.

The command refuses, before it maps anything, a backfill that does not
permit serving, and it shares A1's checks under the runners' lock: the
fence and the claim, 017's conversion bound, the frozen end and the
double-credit refusal. On a database whose backfill has finished it
maps nothing and succeeds, so a retry after a lost reply is safe. It
prints what it did as JSON, already_complete included.

The recovery evidence export now reads a database at fence 2, as every
start does. It exports the cursor as one share_hashes_deferred record
in place of the partial mapping, and the summarizer marks the
share_hashes kind deferred with it and says so in a note. The preamble,
which every parallel session runs, reads only the catalog, and the
cursor is read last, after the integrity report, so an export holds the
cursor table's lock only until it commits, right after. The refusal of
a cursor at fence 1 names migrate --defer-share-hashes to serve and
plain migrate to finish; one that a build before #669 started, with no
fence, names plain migrate alone. A complete export's summary is
unchanged, and the parallel export and summarizer stay byte-identical
to the serial ones. The export checks each capability's value against
the highest that startup accepts, from one declaration that a unit test
compares with NATIVE_CAPABILITIES.

self-check samples the backfill beside the heartbeats, in one read of
the cursor and its fence under one 5 s deadline, and reports it in a
share_hash_backfill field with a warning line, never as a failure:
"pending" with the fence and the cursor, or "unknown" with why it could
not be read, a connection failure's text withheld. The field is absent
only when the read succeeded and nothing is pending. The start gate's
warning, migrate --defer-share-hashes's output and the share-archive
refusals while the backfill is pending name backfill-share-hashes as the
way to finish it. The migration guide and the ledger runbook describe
when and how to run it after go-live, its throttle, how to watch it,
the record's retries, and how to compare evidence while it is pending.

The recovery test helpers now refuse an export script edited after the
test binary was built. The serial export runs the copy compiled in,
while the parallel exporter and the summarizer read the file, so a full
suite whose script changed under it compared two scripts' exports: the
deferred export test once failed so, at the line the edit had moved.

The record of 2 while frontends serve retried a failed attempt without
asking whether it had committed. An attempt can commit and still fail:
its COMMIT's reply lost with the connection, or cut short after the
commit was written. A retry then met the cursor that commit had dropped,
failed on the missing table, and reported a finished backfill as failed.

Now every failed attempt is followed, before anything else, by a read of
what it left, in one statement of a fresh transaction under the record's
own timeouts: whether 2 is recorded, and whether the cursor table
exists. With 2 recorded and the cursor gone the backfill is done: the run
logs that the attempt's reply was lost or cancelled but 2 is recorded,
and succeeds as any finished run does. With 2 unrecorded and the cursor
in place nothing committed, and a wait past a lock or statement timeout
is retried as before. Either other state was made by hand, since the one
transaction that records 2 drops the cursor in the same commit, and is
refused loudly rather than retried. A read that fails too stops the run,
naming backfill-share-hashes, which finds 2 recorded or records it.

The statement timeout still covers the COMMIT. Under synchronous
replication a COMMIT waits for the standby with its xid running and its
locks held, which #738 rules out for long, and a cancelled wait leaves
the commit local, which PostgreSQL reports as done with a warning. The
re-check covers every way an attempt can fail after its commit, and a
comment by the record's transaction says why.

A gated lib test injects the failure through a test-only fault hook, in
the pattern of the candidate claims' faults: keyed by schema, it lets the
first record attempt commit and then return PostgreSQL's 57014. The run
reports the backfill done, with 2 recorded and the cursor and the fence
gone. A unit test pins the four states the read can find. The migration
guide and the ledger runbook say what the run does after a lost reply.

Every read of the share-hash cursor took any 42P01 for the cursor
dropped since its look-up, by the transaction that records 2, and read
it as nothing pending. But the read can miss another relation it needs:
self-check's reads the fence from qbit_prism_schema_capabilities, so a
database with its cursor beside a missing capability table, a damaged
one self-check should diagnose, read as one whose backfill had finished,
and self-check omitted both the field and the warning.

Now a 42P01 reads as nothing pending only if the cursor is gone when it
is looked up again, in one short statement. Otherwise the read's own
error goes on, and self-check reports the backfill as unknown with it.
Both readers of the cursor, self-check's and the one every start and
migrate makes, go through that one place. The record of 2 has no such
shortcut: its re-check after a failed attempt reads the catalog and the
migration history, and a failure there stops the run.

self-check's backfill test in ledger_postgres renames the capability
table beside a pending cursor and requires state unknown with the
missing relation named, and the warning. A gated lib test drops the
cursor between its look-up and its read, through a test-only fault the
record test's hook now shares, and requires both readers to read nothing
pending. The lib tests move to share_hashes/postgres_tests.rs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 8, 2026
Set VERSION and [workspace.package].version to 3.0.0-rc.5: rc.4 plus
#737's fixes in #742 (fatal-state clear accepts a tip that only grew,
takes --timeout-seconds, and runs the integrity report under its own
statement timeout) and #744 (migration 026: the report computes each
set once); the deferred share-hash backfill in #743 and #746 (migrate
--defer-share-hashes, then the throttled backfill-share-hashes after
go-live); #745 (migrate --offline-indexes, which composes with
--defer-share-hashes); the cutover tooling merged after rc.4's tag
(#728, #732, #736, #740); and #747, a test-only fix for #724. Both
Cargo.lock files move only their workspace-member entries.

doc/release-notes-3.0.0-rc.5.md, dated 2026-10-08, covers:
- the changes, with the report's estimated 57 to 29 s since 026, the
  migrate --defer-share-hashes command measured on the pair at
  5 min 06 s (539d6c9, before #745), backfill-share-hashes' throttle,
  record of 2 and safe rerun, the evidence scripts' changes, and the
  offline index builds' estimated gain;
- upgrading: migrate is needed again (a binary-only upgrade refuses
  "missing migration(s) 26", and this export refuses a database without
  026), the cutover's migrate step with both flags, and finishing the
  backfill after go-live under #738's snapshot rule, with self-check
  only at low share rates. Rolling back to rc.4 at fence 2 goes only by
  finishing the backfill first or from the pre-migrate backup or
  physical copy, and each rc.4 refusal is quoted where it applies: a
  start meets the cursor first and says to run migrate, which rc.4's
  migrate refuses. The rules still in force: #737's statement timeout
  for rc.4's tools, 026 or not, #738's snapshot rule, and fatal-state
  clear --timeout-seconds, whose bound ends before the commit;
- the same evidence set as rc.4's, with each PR's merge SHA, the pair's
  serving evidence carried, and the rehearsal's full measurements left
  to the cutover's go/no-go record;
- the known issues: rc.4's, minus #737's F1, F2, F4 and F5, plus
  #737's open part, #738 (its fix planned for rc.6), #739, #741, #731
  and #734, with #582 closed and #712 and #724 updated.

docs/prism-rust-migration.md now says how earlier candidates refuse a
database at fence 2: a start with the backfill's own refusal, whose
advice to run migrate holds only for this release, and their migrate,
or a start with PRISM_POSTGRES_INIT_SCHEMA=1, with the fence. The
development-line notes name 3.0.0-rc.5, and they and the 3.0.0 notes
link all five candidates' notes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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