Skip to content

fix: report Backfilling on a CREATING GSI and refuse its reads by phase - #454

Open
LeeroyHannigan wants to merge 34 commits into
mainfrom
fix/pg-gsi-create-race
Open

LeeroyHannigan wants to merge 34 commits into
mainfrom
fix/pg-gsi-create-race

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

What

A GSI that UpdateTable adds goes through the two CREATING phases the service has. DescribeTable reports Backfilling false and then true, and a Query, Scan or PartiQL SELECT naming the index gets the refusal for its phase.

Why

The PR closes no issue. Refs #1081, which asks for TableStatus UPDATING and the new index CREATING with Backfilling false in the UpdateTable response and in the DescribeTable after it. This PR adds the member. On PostgreSQL and SQLite the response still reports the table and the index ACTIVE, because the build runs inside the request. The response's TableStatus and IndexStatus remain for #1081.

What the service does

Measured on the service on 2026-10-26 in us-east-1, in two runs. A GSI added by UpdateTable to a 3,000-item table stayed CREATING for 512 s on one run and 519 s on the other.

Phase IndexStatus Backfilling Query or Scan naming the index
About the first 30 s CREATING false ValidationException: The table does not have the specified index: <name>
The rest of the build CREATING true ValidationException: Cannot read from backfilling global secondary index: <name>
Once the index is built ACTIVE absent served

While the index is CREATING, DescribeTable lists nine members for it: Backfilling, IndexArn, IndexName, IndexSizeBytes, IndexStatus, ItemCount, KeySchema, Projection and ProvisionedThroughput.

Those runs read the index every 30 s, so the table is right at that granularity. #708's measurement of 31 October read it more often. A Query sent 0 and 2 s after Backfilling turned true still got the not-found text. One sent about 45 s after it got the backfilling text. ExtendDB switches the refusal with the member. The wire test reads DescribeTable before and after each read and counts a reading only when both agree.

What main does

GsiDescription has no Backfilling member, so no backend reports one. PostgreSQL and SQLite answer a Query or Scan of a CREATING GSI with "The table does not have the specified index" for the whole build. MongoDB and Cassandra serve the read from the partial index. On Cassandra the recovery worker drops and recreates the index's data table before it backfills (crates/storage-cassandra/src/workers.rs). Main's lookup serves a CREATING index. A read that lands between the drop and the create queries a table that does not exist and answers InternalServerError.

#401 already made PostgreSQL GSI creation converge under concurrent writers: the build is state-based, and a recovery worker finishes builds left CREATING. This change adds the two CREATING phases on top of that design. It changes neither how the build orders its writes nor when the recovery worker runs. This PR's earlier commits fixed the same convergence bug with a design of their own. Their two convergence tests are ported here.

The change

GsiDescription gains Backfilling, absent when it is None. Each backend answers one question, whether the backfill of a CREATING index has started. extenddb_storage::gsi_backfill derives both the member and the refusal from that answer, so DescribeTable and a read report the same phase.

  • PostgreSQL and SQLite build the index in the server process, so the engine keeps the ids of indexes whose backfill has started. On PostgreSQL an id stays recorded until the index leaves CREATING. A build that fails for a passing reason (a conflict, a timeout, a lost connection) leaves the index CREATING for the recovery worker. The index stays in its second phase until the retry publishes it. DescribeTable and the read path copy the set before they read the catalog row. Within one process a call therefore never reports an earlier phase than the call before it. On SQLite the record lasts as long as the build inside UpdateTable. That leaves two exceptions, both after a failure. When the flip to ACTIVE fails, or the cleanup DELETE after a failed build fails, the record ends while the row stays CREATING. The index then reports the first phase until the startup reconciler rebuilds it.
  • MongoDB's backfill worker sets backfilling on the index document before its first batch, and the flip to ACTIVE removes it. A job resumed after a restart finds the mark and goes straight to its scan.
  • Cassandra's recovery worker writes backfill_heartbeat when it takes the index up, before it rebuilds the data table. A later scan finds the heartbeat fresh and leaves the index to the build that is running. The worker scans the catalog once per account keyspace, and each scan returns the CREATING indexes of every account. A scan takes up only the indexes whose base table belongs to the keyspace's account. The heartbeat is therefore written only by the scan that builds the index. The read lookup reads index_status with get_column, as it reads the other columns of the row. A row whose status cannot be read therefore fails the read with an internal error instead of being served.

The read path shares its index lookup with PartiQL. partiql/select.rs, partiql/batch.rs and partiql/authorize.rs resolve an index through index_info_by_table_id. An ExecuteStatement SELECT naming a CREATING GSI therefore gets the refusal of its phase on every backend. In a BatchExecuteStatement, that refusal is the error of the statement that names the index, and the other statements run. The authorization of the statement finds no key schema for the index in either phase. Those answers are ExtendDB's. In the first phase it is the PartiQL text for an index the table does not have: "The table does not have the specified index". That text carries no index name. In the second it is the backfilling sentence. The service's answer to a PartiQL read of a CREATING index is not measured (see Not tested).

A test setting, gsi_creating_phase_delay_ms:<table>, lengthens each phase by the given milliseconds so a client can observe both phases on a small table. It is writable through the management API and with extenddb settings set, from 0 to 60000. The admin guide lists it with the other test settings. table_scoped_gate_table_name, the helper of the two MongoDB test-gate keys, parses the key. It accepts a table name of 3 to 255 characters from a-z, A-Z, 0-9, ., _ and -.

Three changes to existing tests and CI. gsi_can_be_added_to_a_table_with_no_sort_key in tests/rust now waits for its new index to be ACTIVE before it queries it, because a CREATING index refuses reads on MongoDB and Cassandra. The wait_for_index_active helper it uses moves from gsi_update_table_attr_defs.rs to test_base.rs, where both files call it. devtools/run-mongodb-tests now runs the live_gsi_ storage tests with the records_live ones, so CI selects them.

One test-only entry point stays beside an existing one. recover_stale_gsi_backfills_of_table in the Cassandra workers.rs runs one recovery scan limited to one table. It returns while the backfills it started run, which the claim test needs to run a second scan during the phase hold. recover_stale_gsi_backfills_for_tests scans an account's keyspace and waits for its backfills. Both are compiled only for tests.

Overlaps with other PRs

Tests

Test What it checks Fails on
extenddb-storage gsi_backfill::tests (6 unit tests) The member and the refusal for each phase, the record's lifetime, the snapshot Main has no code for them to run against
PostgreSQL a_gsi_added_by_update_table_walks_both_creating_phases Phases 1, 2 and ACTIVE in order, each with its refusal Main: ("CREATING", None)
PostgreSQL a_passing_build_failure_keeps_the_index_backfilling_until_the_retry_publishes_it A passing failure produced in the database (the flip is locked, then cancelled with SQLSTATE 57014), then the recovery pass: Backfilling true and the backfilling refusal throughout A record that ends with the failed build: the UpdateTable response reports ("CREATING", false)
PostgreSQL the_index_stays_backfilling_from_the_end_of_its_backfill_to_the_flip From the end of the backfill to the flip, the index reads as phase 2 A build that drops its record before the flip: "phase 1 after the backfill wrote every item"
PostgreSQL gsi_create_converge (2 tests) The index equals the base projection after ten builds under four writers Passes on main by design: it pins the convergence #401 delivered
SQLite gsi_backfilling_tests::a_gsi_added_by_update_table_walks_both_creating_phases The three states in order Main: phases [3] instead of [1, 2, 3]. A build that drops its record before the flip: [1, 3]
MongoDB live_gsi_added_by_update_table_walks_both_creating_phases The three states in order Main: ("CREATING", None)
MongoDB live_gsi_job_resumed_after_a_restart_keeps_its_second_phase A job left marked by a dead process reports phase 2 and finishes without waiting the phase hold A resumed job that starts its first phase again
Cassandra a_creating_gsi_reports_each_phase_and_refuses_reads_by_phase The three catalog states the worker leaves, with their refusals, and a row whose index_status cannot be read fails the read Main: ("CREATING", None). A status read that treats an unreadable column as not CREATING: the read is served
Cassandra the_recovery_claim_starts_the_second_phase_and_a_later_scan_leaves_the_build The heartbeat is written before the data table is rebuilt, and a second scan leaves the running build alone A claim that writes no heartbeat
ops_settings a_gsi_phase_hold_is_writable_for_one_valid_table_name The management API accepts the hold key only with a table name the service would accept Main has no such key. A helper without the length bound: it accepts a two-character name
cmd_settings a_table_gsi_phase_hold_is_writable_from_the_cli extenddb settings set accepts the hold key within the bound and refuses an invalid table name A CLI that looks keys up in its fixed list alone: "Unknown setting". A helper without the length bound: it accepts a two-character name
Wire tests/python/test_gsi_backfilling.py The CREATING member set, both refusals byte for byte for Query, Scan and ExecuteStatement, the same refusals as the error of the index read in a BatchExecuteStatement whose read of a base item returns the item, and ACTIVE Main's settings API refuses the hold key. PartiQL lookups that answer the first-phase text for every error: the second phase's statement assertion

One run from the branch before the 30 October rebase is not repeated by CI. Against a Cassandra server built from that branch, a script added an index with UpdateTable in each of two accounts, 8 s apart. It ran both orders, and both indexes became ACTIVE each time. One of the orders adds the first index to the account whose keyspace the recovery worker lists last.

Merge with #395

Rebased on main at 3051e500 (#395) on 30 October.

DeleteTable reads the backfill set only for the description in its response. When and how the table's data is dropped is #395's code, unchanged.

Testing done

On 2a75a22, merged with main at c8e66fb, on one host:

  • cargo +1.97.0 fmt --all -- --check is clean. The two clippy commands of .github/workflows/clippy.yml with -D warnings on the seven crates this PR touches: clean. cargo +1.94.0 check --all-targets on the same crates: clean.
  • Lib tests: storage 296 passed, storage-postgres 94, storage-sqlite 336 with 1 ignored, storage-mongodb 199 with 86 ignored, storage-cassandra 192, engine 981 and server 522. On this host, the two write_lock_tests of extenddb-storage-sqlite and five backup_store tests of extenddb-server fail, as on main (storage-sqlite: the two write_lock_tests fail when an ancestor of the Cargo target directory is owned by another user #1414).
  • cargo test -p extenddb-wasm-adapter, with its parity corpus test: 4 passed.
  • PostgreSQL gsi_backfilling (3) and gsi_create_converge (2) against PostgreSQL 16 (pgvector/pgvector:pg16) with EXTENDDB_TEST_PG_REQUIRED=1: 5 passed.
  • tests/python/test_gsi_backfilling.py and tests/test_partiql_batch.py against a SQLite server and a PostgreSQL server built from 2a75a22: 171 passed on each.
  • Control: against a SQLite server built with the two PartiQL files of deda762 and the rest of 2a75a22, tests/python/test_gsi_backfilling.py fails. The BatchExecuteStatement of the second phase answers 400 ValidationException: Cannot read from backfilling global secondary index: gsi_slow for the whole call.
  • cargo test -p extenddb-storage-postgres --test doc_version_literals: passed.
  • The clippy step of the WebAssembly workflow is clean on the branch.
  • python3 -m pytest --collect-only -q collects 7305 tests in tests/ and 821 in tests/python, with no error.

The runs below are from earlier rounds.

CI at 3cb02a7ce, the head before the commits of 24 November, ran on 14 November, and all 13 workflow runs were green. Among them: Test 43599472817, Integration Tests 43599472857, MongoDB Integration Tests 43599472914 and Cassandra Integration Tests 43599472860. tests/python/test_gsi_backfilling.py passed in run-integration, run-integration-sqlite, mongodb-pytest and cassandra-pytest. The PostgreSQL gsi_backfilling and gsi_create_converge tests passed in run-postgres-storage-tests, the two live_gsi_ tests in mongodb-rust, and the Cassandra gsi_backfilling tests in cassandra-storage. The tests/rust suite passed in cassandra-rust and mongodb-rust.

On ece2581d0, on one host:

  • cargo fmt --all -- --check, and clippy with -D warnings as CI runs it, with both of its feature sets, on the whole workspace: clean.
  • Lib tests: engine 890 passed, storage 296, server 515 and SQLite 328 with 1 ignored, apart from the seven host failures named below. gsi_backfilling_tests passes.
  • tests/python/test_gsi_backfilling.py against a SQLite server built from ece2581d0: passed.
  • The clippy step of the WebAssembly workflow (devtools/run-wasm-step clippy): clean. At 832afe71c, the merge before the fix, it failed on the two tokio::time::sleep calls. It ran with the C compile of the bundled SQLite skipped, because the host has no clang, and clippy does not link.
  • python3 -m pytest --collect-only -q collects tests/ (6,638 tests) and tests/python (673) with no error.
  • feat(wasm): [Add WebAssembly browser support for ExtendDB #1009 changes no file of the PostgreSQL, MongoDB or Cassandra crates, so the tests of those backends below were not run again. CI at 3ab93e85b, the head before 832afe71c, passed all 65 checks.

On the commits of 24 November up to 14f7d0136, on one host:

  • cargo fmt --all -- --check. Clippy with -D warnings as CI runs it, with both of its feature sets, on the nine crates this PR touches. cargo +1.91.1 check --all-targets on the same crates. cargo test --no-run in tests/rust, with no warning.
  • Lib tests: core 1,146 passed, engine 885, storage 292, server 515 and SQLite 326. Five backup_store tests in extenddb-server and two write_lock_tests in extenddb-storage-sqlite fail on that host. The SQLite two are storage-sqlite: the two write_lock_tests fail when an ancestor of the Cargo target directory is owned by another user #1414, a file-ownership condition of the host. CI's Test run at 3cb02a7ce ran all seven green.
  • PostgreSQL gsi_backfilling (3) and gsi_create_converge (2) against PostgreSQL 16 (pgvector/pgvector:pg16, CI's image) with EXTENDDB_TEST_PG_REQUIRED=1: 5 passed. With the connection string unset and the variable set, the 5 fail with the new assert. With the previous base_conn, they passed as skips.
  • Cassandra gsi_backfilling (2) and index::recovery_for_one_account_leaves_the_indexes_of_other_accounts_alone against Cassandra 4.1 with PasswordAuthenticator: 3 passed. With the previous index.rs, the phase test fails at its new assertion: the read is served.
  • tests/rust gsi_on_hash_only_table and gsi_update_table_attr_defs (6 tests) against a SQLite server built from the branch, with --test-threads=1 as devtools/run-tests runs them: 6 passed.
  • The wire test against the same server: passed. Against a server whose PartiQL lookups answer the first-phase text for every error, it fails at the second phase's statement assertion.
  • With the helper's length bound set back to a non-empty check, a_gsi_phase_hold_is_writable_for_one_valid_table_name and a_table_gsi_phase_hold_is_writable_from_the_cli fail on the two-character name.
  • python3 -m pytest --collect-only -q collects tests/ (6,638 tests) and tests/python (673) with no error.

Not tested

  • A PostgreSQL restart in the middle of a build. The record lives in the process, so a restarted process reports Backfilling false until its recovery pass takes the build up again. By the code, that is after one GRACE_PUBLISH wait (1 s) and the write barrier.
  • The two SQLite failure paths above. One is a flip to ACTIVE that fails after a successful build. The other is a cleanup DELETE that fails after a failed build. No test produces either failure.
  • Where the backfilling refusal sits among other Query and Scan validation on the service. The requests that would measure it: a Query with ConsistentRead true on the backfilling index, and a Query whose KeyConditionExpression names a non-key attribute of it.
  • ExecuteStatement on a CREATING GSI on the service, in either phase. The request that would measure it: {"Statement": "SELECT * FROM \"<table>\".\"<index>\" WHERE g = 'v'"} right after UpdateTable and again after Backfilling turns true, and BatchExecuteStatement with the same statement. The wire test pins ExtendDB's answers.
  • The seconds between Backfilling turning true and the refusal switching on the service (see What the service does). A Query every second from UpdateTable on, with a DescribeTable between each pair, would measure it.
  • Whether Backfilling ever returns to false after true on the service. A DescribeTable every second for the whole build would measure it.
  • A second index change while an index is CREATING on MongoDB and Cassandra, which both accept. That is unchanged from main.
  • The wire tests ran on debug builds of the server.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a. Backfilling is a member of the service's DescribeTable response, and the setting follows vector_allocation_phase_delay_ms, the test setting of the same shape. The Storage trait gains no method. GsiDescription gains one optional field with a serde default, so descriptions stored by earlier releases deserialise.

Breaking changes

A client of an existing deployment sees four differences after the upgrade. DescribeTable lists Backfilling on a GSI that UpdateTable added, while the index is CREATING. On MongoDB and Cassandra a Query, Scan or PartiQL SELECT naming such an index gets a ValidationException where main served the partial index. On PostgreSQL and SQLite the refusal changes once the backfill has started. It was "The table does not have the specified index: ", and it is now "Cannot read from backfilling global secondary index: ". In a BatchExecuteStatement, a SELECT of such an index gets the refusal as its own error, and the other statements run. The upgrade manual lists these changes under v1.2.0.


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

if idx.index_status != "ACTIVE" || effective_delay(idx, system_default) != 0 {
return Ok(true);
}
index_has_queued_rows(tx, &idx.index_id).await

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be invoked on every update to an active, sync GSI. How much overhead does it introduce?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is no longer in the PR. On 29 October I replaced the branch: #401 fixed the convergence bug that these commits addressed, and the PR now adds only the two CREATING phases. The branch does not change data/index.rs, where this queue probe ran, so the PR adds no work to a write to an ACTIVE GSI.

Comment thread crates/storage-postgres/src/data/ddl.rs Outdated
if index_type == IndexType::Gsi && index_status != "ACTIVE" {
return Err(StorageError::Validation(format!(
"{}{index_name}",
extenddb_core::types::VECTOR_INDEX_BACKFILLING_SCAN_PREFIX

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mildly confusing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is gone with the earlier commits. A Query or Scan that names a CREATING GSI now gets the service's refusal for its phase, as measured on 2026-10-26. Before the backfill starts, the refusal is "The table does not have the specified index: ". After it starts, the refusal is "Cannot read from backfilling global secondary index: ". extenddb_storage::gsi_backfill builds both refusals, and the PR body has the table.

jcshepherd
jcshepherd previously approved these changes Oct 26, 2026

@jcshepherd jcshepherd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about performance for synchronous GSIs and nit on error messaging, but overall looks reasonable.

LeeroyHannigan added a commit that referenced this pull request Oct 29, 2026
On DynamoDB a GSI added by UpdateTable is refused to Query and Scan
while it is CREATING and served once its backfill is done and it is
ACTIVE (measured 2026-10-26 in us-east-1 on a 3000-item table). The
table takes writes while the index builds, and the ACTIVE index has to
include them.

These two tests check that for the PostgreSQL build #401 made
state-based. Each seeds 3000 rows, adds a KEYS_ONLY GSI with UpdateTable
while four writers move index keys, insert rows and delete rows, waits
for the propagation queue to drain, and checks that the index table
equals the base projection row for row. Each runs ten iterations, one
test with the propagation delay at 5 ms and the other at 0, where an
ACTIVE index is maintained inline.

Ported from #454 (gsi_creation_converges_under_concurrent_writers and
its _zero_delay variant, with the helpers they use) into a new file.
The UpdateTable input is built from JSON, so the file does not depend on
the struct's field list.
@LeeroyHannigan LeeroyHannigan changed the title Fix PostgreSQL global secondary index creation under concurrent writers fix: report Backfilling on a CREATING GSI and refuse its reads by phase Oct 29, 2026
Two runs against DynamoDB on 2026-10-26 (us-east-1) added a GSI to a
3000-item table with UpdateTable. The index stayed CREATING for 512 s and
519 s. For about the first 30 s DescribeTable reported Backfilling false,
and a Query or Scan naming the index got the ValidationException "The
table does not have the specified index: <name>". For the rest of the
build it reported Backfilling true, and both reads got the
ValidationException "Cannot read from backfilling global secondary
index: <name>". Once the index was ACTIVE the member was absent and both
reads were served.

Before this change no backend reported the member. PostgreSQL and SQLite
answered a read of a CREATING GSI with the first sentence for the whole
build, and MongoDB and Cassandra served the read from the partial index.

GsiDescription gains a Backfilling member that is absent when None. Each
backend answers one question, whether the backfill has started, and the
storage crate's gsi_backfill module derives both the member and the
refusal from that answer, so DescribeTable and a read report the same
phase. Each backend takes the answer from its build:

- PostgreSQL and SQLite build in the server process and record no
  backfill progress in the catalog, so the engine keeps the index ids
  whose backfill has started. PostgreSQL records an id when the build
  task reaches its backfill, after the publish wait and the write
  barrier. SQLite records it before UpdateTable opens the transaction
  that creates the index's data table and backfills it. The record is
  dropped after the ACTIVE flip or when the build gives up.
- MongoDB's backfill worker sets backfilling on the index document before
  its first batch, and the ACTIVE flip removes it. Restored indexes
  report no member.
- On Cassandra the recovery worker writes backfill_heartbeat when it
  starts work on the index, after checking the row is still CREATING and
  before it rebuilds the data table. A later scan of the worker then
  finds a fresh heartbeat and leaves the index to the running build.

A new test setting, gsi_creating_phase_delay_ms:<table>, lengthens each
phase by the given milliseconds, so a client can observe both on a small
table. It is writable through the settings API (0 to 60000) and
documented with the other test settings in the admin guide.

Tests: a storage test per backend walks the three states and checks the
refusal in each (Cassandra stages the catalog row, since its direct tests
run no worker), and tests/python/test_gsi_backfilling.py checks the
member set, both refusals byte for byte, and the ACTIVE state over the
wire.
On DynamoDB a GSI added by UpdateTable is refused to Query and Scan
while it is CREATING and served once its backfill is done and it is
ACTIVE (measured 2026-10-26 in us-east-1 on a 3000-item table). The
table takes writes while the index builds, and the ACTIVE index has to
include them.

These two tests check that for the PostgreSQL build #401 made
state-based. Each seeds 3000 rows, adds a KEYS_ONLY GSI with UpdateTable
while four writers move index keys, insert rows and delete rows, waits
for the propagation queue to drain, and checks that the index table
equals the base projection row for row. Each runs ten iterations, one
test with the propagation delay at 5 ms and the other at 0, where an
ACTIVE index is maintained inline.

Ported from #454 (gsi_creation_converges_under_concurrent_writers and
its _zero_delay variant, with the helpers they use) into a new file.
The UpdateTable input is built from JSON, so the file does not depend on
the struct's field list.
gsi_can_be_added_to_a_table_with_no_sort_key waited for the table to be
ACTIVE after UpdateTable and then queried the new index. On MongoDB and
Cassandra UpdateTable returns while the index is still CREATING, and a
CREATING index now refuses reads as the service does. On MongoDB the
first Query got "ValidationException: The table does not have the
specified index: hashOnlyIdx" and the test failed. Before the CREATING
phases were added, MongoDB served that read from the partial index and
the test's loop polled until the row appeared.

The test now waits for the index itself to be ACTIVE, the way the second
test in the file and gsi_update_table_attr_defs.rs already do. No other
test in tests/rust or tests/ reads a GSI that UpdateTable created
before waiting for that index to be ACTIVE.
… failure

A PostgreSQL GSI build that fails for a passing reason (a conflict, a
timeout, a lost connection) leaves its index CREATING for the recovery
worker, and UpdateTable still succeeds. The backfill record was a guard
local to the build loop, so that failure dropped it: the index went back
from Backfilling true to false, and a Query or Scan went back from
"Cannot read from backfilling global secondary index: <name>" to "The
table does not have the specified index: <name>", until the recovery
worker's next pass, which runs every minute and backs off up to an hour
after another failure. The service is not known to go back a phase.

The record now stays until the index leaves CREATING. The build removes
it when it publishes the index, finds the index deleted or already
published, or when remove_failed_build deletes the index after a failure
that would recur. A passing failure returns with the record in place, and
the retry records the same id again. The set no longer counts builds of
one index: an id is removed only once no build of it is needed. SQLite
keeps a record scoped to its build, because a failed SQLite build deletes
its row in the same call.

Test: a_passing_build_failure_keeps_the_index_backfilling_until_the_retry_publishes_it
locks the index's catalog row so the flip to ACTIVE waits, cancels the
waiting statement (SQLSTATE 57014, which the build treats as passing),
then checks that the UpdateTable response, ten readings before the
retry, and the readings while reconcile_creating_gsis runs until ACTIVE
report Backfilling true with the backfilling refusal. On the previous
commit it fails at its first check: the UpdateTable response reports
("CREATING", false). The test file's setup moves into a helper shared by
both tests.
The admin guide lists gsi_creating_phase_delay_ms:<table> among the
settings managed with `extenddb settings set`, but the command looked
keys up in KNOWN_KEYS alone and refused it with "Unknown setting". Only
the management API accepted the key, through its own check for a valid
table name.

ops_settings now has one lookup, writable_key_validator, that returns the
validator for a KNOWN_KEYS entry or for a table's phase hold. The
management API's set_setting and the CLI both use it, so they accept the
same keys with the same bound (0 to 60000 ms); set_setting also accepts
the MongoDB test-gate keys in builds with mongodb-test-hooks, as before.
The CLI's checks move into a validate function so they can be tested
without a store.

Test: a_table_gsi_phase_hold_is_writable_from_the_cli accepts the key with
1000 and 0, refuses 60001 as an invalid value and a two-character table
name as an unknown setting. It fails when validate looks keys up in
KNOWN_KEYS alone, as the command did before.
PostgreSQL and SQLite keep the ids of GSIs whose backfill has started in
the process, and DescribeTable and the read path paired that set with the
index's catalog row. They read the row first and the set second. A build
removes its id right after it flips the row to ACTIVE, so a call whose row
read landed before the flip and whose set read landed after the removal
saw a CREATING row with no record, and reported Backfilling false, or the
first-phase refusal "The table does not have the specified index", for an
index whose backfill had finished.

GsiBackfills gains snapshot(), a copy of the set. DescribeTable,
DeleteTable's response and the read path on both backends take it before
they read the rows they pair it with. An id is removed only after its row
has left CREATING, so a set read first can be paired only with the same
row or a newer one, and a call never reports an earlier phase than the
call before it.

On PostgreSQL the test hold inside the second phase now runs between the
backfill and the flip instead of before the backfill, so a test can read
the index in the window at the end of the build. The second phase is as
long as before: the hold plus the backfill.

Test: the_index_stays_backfilling_from_the_end_of_its_backfill_to_the_flip
waits until the index table holds the table's three items, then reads the
index until it is ACTIVE and fails on any reading other than the second
phase. It
fails when the build removes its record before the flip (the mutant that
inserts backfills.finish right after the backfill): "phase 1 after the
backfill wrote every item". The walk test and the passing-failure test
fail under that mutant too.
The MongoDB storage tests for the phases of a GSI that UpdateTable adds
need a live replica set, so they are #[ignore]d like the other live tests
and their names start with live_gsi_. devtools/run-mongodb-tests runs the
crate's live tests before the Rust suite, the one place in CI with a
replica set, but its filter was records_live alone, so CI never selected
them.

The filter is now `records_live live_gsi_`; libtest runs the tests whose
names contain either. With the old filter, `--list` on that command shows
none of the live_gsi_ tests; with the new one it lists them.
The Cassandra recovery claim and a MongoDB backfill resumed after a
restart had no test.

Cassandra: the recovery worker's claim writes backfill_heartbeat before it
rebuilds the index's data table and starts the backfill, which is the
start of the second phase, and a later scan that finds the heartbeat
fresh leaves the index to the build already running. The direct tests
run no worker, so workers.rs gains a hidden entry point,
recover_stale_gsi_backfills_of_table, that runs one scan limited to one
table's CREATING indexes; the limit keeps the scan off the rows the tests
beside it stage. The worker itself passes no limit.

the_recovery_claim_starts_the_second_phase_and_a_later_scan_leaves_the_build
stages the row as UpdateTable inserts it, with the table's phase hold at
3000 ms, and runs one scan. It checks that the index then reports
Backfilling true with the backfilling refusal, that the heartbeat's
write time is earlier than the write time of the rebuilt data table's
schema row, that a second scan during the hold leaves that table in place
(same table id), and that the running build publishes the index. It
fails when the claim writes no heartbeat.

MongoDB: a job resumed after a restart finds the backfilling mark the
dead process set (ttl_worker.rs) and goes straight to its scan.
live_gsi_job_resumed_after_a_restart_keeps_its_second_phase adds the
index with no worker running and a 30 s phase hold, sets the mark as a
worker that died would have, then starts the worker. The test fails on
any reading other than the second phase before and during the resumed
job, and requires the index to be ACTIVE within 15 s. It fails when the
resumed job starts its first phase again, which waits the hold twice. The
live test file's setup and phase reading move into helpers shared with
the existing test.
… keyspace

The Cassandra recovery worker runs its catalog scan once per account
keyspace, and the scan returns the CREATING indexes of every account.
Each scan rebuilt and backfilled every stale row in the keyspace it was
given. The claim this branch added writes backfill_heartbeat on the
first attempt, so when the worker listed another account's keyspace
first, that scan claimed the index, created its data table in the other
account's keyspace, and failed the backfill there ("table items_...
does not exist"). The scan of the index's own keyspace then found the
heartbeat fresh and skipped the index. Each later pass repeated this
once the heartbeat was older than the stale threshold, so the index
stayed CREATING with Backfilling true. Main has no claim, so every scan
tried the index and the one in its own keyspace built it.

The scan now reads the account that owns the base table from its tables
row, and leaves the row alone when that account's keyspace is not the
one being scanned. The check comes before the claim, so a scan of
another keyspace no longer writes the heartbeat, creates a data table or
takes a propagation hold for the index.

The other fix would build each row in its own account's keyspace from
whichever scan reaches it first. That makes the first scan of a pass do
all the work while the other scans repeat the catalog read and build
nothing, and when the claim's UPDATE fails, a later scan in the same pass
that reads the row before the backfill's first page writes a heartbeat
starts a second build of the index. With the skip, only the scan of the index's
own keyspace can take it up, once per pass, and the claim is written
only by the attempt that builds the index.

Test: a_recovery_pass_builds_each_index_in_its_own_account_keyspace
creates a table in each of two accounts, puts three items in each, adds
gidx to both with UpdateTable, and runs one pass limited to the two
tables: the first account's keyspace, then the second's. After the
first scan the second account's index must still report Backfilling
false, with no data table for it in the first keyspace. After the
second scan both indexes must become ACTIVE with three rows in their
data tables. On the previous commit it fails at the first check: the
second account's index reports ("CREATING", true). The claim test's
index id query moves into a helper the new test shares.
…the flip

The SQLite phase-walk test held the second CREATING phase before the
build, so no test read the index between the build's commit and the flip
to ACTIVE. A build that ended its record there, with the row still
CREATING, passed the test, although DescribeTable then reported the first
phase. The second hold now runs after the build commits and before the
flip, as on PostgreSQL, and a record dropped before the flip fails the
walk test.

The snapshot() doc comment and the build's comment said the answer never
goes back to the first phase. On SQLite it does on two failure paths: when
the flip to ACTIVE fails, or the cleanup DELETE after a failed build fails,
the record ends while the row stays CREATING until the startup reconciler
rebuilds it. Both comments now say so.
CodeQL reported the two-account recovery test as cleartext logging of
sensitive information: the assertion message printed the table name of a
helper built from the account id. The loop now names each account
"first" or "second", and the message prints that label.
a_recovery_pass_builds_each_index_in_its_own_account_keyspace, with its
helpers activate, add_gsi, rows_in and scan, checked two things. A recovery
pass over another account's keyspace leaves an index unclaimed and creates
no data table there. The pass over the owner's keyspace builds it.
recovery_for_one_account_leaves_the_indexes_of_other_accounts_alone in
direct/index.rs, which main carries since #868, checks the same rows. The
phase tests in this file check the Backfilling member after a claim.

The first-phase read check in the phase test now checks the index name in
the IndexNotFound error, as the PostgreSQL test does.
recover_stale_gsi_backfills_of_table is now compiled only for tests, like
the two worker entry points beside it. It stays separate from
recover_stale_gsi_backfills_for_tests because it detaches the backfills it
starts. The claim test runs a second scan while the first scan's build
waits out the phase hold.
…parse

fetch_index_by_name read index_status with get_by_name and treated a
column that failed to read as "not CREATING". It then served the read from
an index that may be partial. The other columns of the same row go through
get_column, which fails the read with Internal, and DescribeTable reads
index_status that way. The status read now does the same.

Test: the phase test sets the row's index_status to null after the ACTIVE
step and checks that the read fails with Internal. On the previous commit
it resolves the index.
The PartiQL SELECT path resolves an index through the lookup this branch
changes. A statement naming a CREATING GSI now gets the refusal of its
phase on every backend. The wire test sends one SELECT on the index beside
its Query and Scan in each phase. The answers it pins are ExtendDB's. In
the first phase it is the PartiQL text for an index the table does not
have, which carries no index name. In the second it is the backfilling
sentence, and once the index is ACTIVE it is the three items. The
service's answer to a PartiQL read of a CREATING index is not measured.
No conflicts. Main (#690, #765, #774) changed two of the files this branch
touches, in lines this branch does not change. It adds an undefined_table
helper in crates/storage-postgres/src/data/ddl.rs, and it changes the
diagnostics store connection and adds a test-hooks re-export in
crates/storage-postgres/src/lib.rs.
… read

Since #945 the reads run at LOCAL_QUORUM, and a debug build asserts that a
SELECT from system_schema goes through system_read, which reads at ONE. The
claim test's data_table helper read system_schema.tables with
query_optional, so the test panicked on the merged tree. It now uses
system_read, as the two-account test in index.rs does.
Main is at 89fc971, one commit after the last merge (#1280). It
changes crates/storage-mongodb/tests/migrations_guards.rs, which this
branch does not change. No file conflicted.
Main is at 3534c9e, one commit after the last merge (#1009, the
WebAssembly build). One file conflicted:
crates/storage-sqlite/src/lib.rs. In the module list, this branch adds
the test module gsi_backfilling_tests after delete_table, and #1009
puts a wasm32 cfg and its comment on hooks, the next module. The merge
keeps both.

Main changed four other files that this branch changes, and git merged
them with no overlap: crates/storage-sqlite/src/store.rs,
table_helpers.rs and update_table.rs, and
crates/engine/src/backup/tests.rs.
…int allows

The WebAssembly job lints the browser build with -D warnings and a list
of calls that trap or hang there. That list holds tokio::time::sleep.
update_table_impl, which the browser build compiles, slept twice
for the gsi_creating_phase_delay_ms test hold. So after the merge of
main the job fails on this branch.

Only the server's settings API, the settings command and tests write
the hold's row, and the browser build has none of them. So there the
hold is zero and the sleep never runs. Both waits now call
wait_creating_phase_hold. It carries the allow of
clippy::disallowed_methods with that reason, as build_vector_index does
on main.

@LJMart11 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at ece2581. Workspace tests ran on 1.91.1, including the new PostgreSQL, SQLite and settings phase tests, plus a wire run of BatchExecuteStatement through each CREATING phase on PostgreSQL. The review centres on how the backfilling refusal reaches PartiQL batch reads.

1 finding is left inline.

#[must_use]
pub fn creating_read_refusal(index_name: &str, backfill_started: bool) -> StorageError {
if backfill_started {
StorageError::Validation(format!(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A BatchExecuteStatement that names a GSI in its second CREATING phase now fails as a whole call, because check_read (crates/engine/src/partiql/batch.rs:384) treats only IndexNotFound as a per-statement answer and returns this Validation error as a request fault. On a PostgreSQL server built from this head, a two-statement batch (a SELECT on the index and a SELECT of a base item by key) returned 200 with a per-statement ValidationError and the item while Backfilling was false and once ACTIVE, but answered 400 ValidationException: Cannot read from backfilling global secondary index: gsi_slow while Backfilling was true. Mapping the backfilling refusal to a per-statement error in check_read, plus a BatchExecuteStatement step in test_gsi_backfilling.py, would close this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f9831b5 makes the backfilling refusal the answer of the statement in a BatchExecuteStatement. check_read in crates/engine/src/partiql/batch.rs now gives that statement the backfilling sentence, as it gives a missing index the not-found sentence. The authorization of the statement read the index's key schema through target_schema in crates/engine/src/partiql/authorize.rs, which failed the call in the same way. It now finds no key schema for a backfilling GSI, as for a missing one. is_backfilling_refusal in crates/storage/src/gsi_backfill.rs tells the refusal from the other lookup errors.

tests/python/test_gsi_backfilling.py now sends your two-statement batch in each phase. The index read gets the not-found sentence, then the backfilling sentence, then the batch primary-key sentence once the index is ACTIVE. The read of the base item returns the item each time. On a SQLite server and a PostgreSQL server built from 2a75a22, it passes, beside tests/test_partiql_batch.py (171 passed on each). On a server built with the two PartiQL files of the merge before f9831b5, it fails at the batch of the second phase. That batch answers 400 ValidationException: Cannot read from backfilling global secondary index: gsi_slow. The service's answer to a PartiQL read of a CREATING index is not measured, so these answers are ExtendDB's.

#708 sends these two lookups and the other PartiQL index lookups through common::statement_index, which reads a backfilling GSI as a missing index. The PR body names that overlap.

Main is at c8e66fb. This branch last had main at 3534c9e. Since
then, 42 PRs merged into main, #1482 among them, which fixes the
WebAssembly build.

Seven files conflicted. The merge keeps both sides in each:

- crates/core/src/settings_keys.rs: #1449 adds the
  search_inequality_filters keys where this branch adds the GSI phase
  hold key. Both blocks stay, the hold first.
- crates/app/src/cmd_settings.rs: #1449 imports server_setting_default
  and this branch imports writable_key_validator from the same module.
  The import names both.
- crates/server/src/management/ops_settings.rs: #1449 adds the
  search_tests module where this branch adds its tests module. Both
  stay.
- crates/storage-mongodb/src/lib.rs: #486 adds mod error_class where
  this branch adds its two GSI modules. All three stay.
- crates/storage-postgres/src/delete_table.rs: #1411 refuses a table
  that is already DELETING before the indexes are read, where this
  branch built its description with the backfill set. The merge keeps
  the refusal of #1411 and reads the backfill set after it, before the
  index rows, as this branch does.
- crates/storage-sqlite/src/delete_table.rs: the same change of #1411.
  The second hunk keeps the backfill set before the index rows and the
  sqlx::AssertSqlSafe query of #1166.
- crates/storage-sqlite/src/table_helpers.rs: the backfill set before
  the catalog rows, and the sqlx::AssertSqlSafe query of #1166.
…atch

In a BatchExecuteStatement, a SELECT that names a GSI whose backfill has
started failed the whole call with 400 ValidationException. check_read
in crates/engine/src/partiql/batch.rs answered a missing index per
statement and passed every other error of the index lookup up as a
request fault. The authorization of each statement read the index's key
schema through target_schema in crates/engine/src/partiql/authorize.rs,
which did the same.

Both now take the backfilling refusal as they take a missing index.
check_read gives the statement the backfilling sentence, as
ExecuteStatement gets it, and target_schema finds no key schema. The
other statements of the batch run. is_backfilling_refusal in
crates/storage/src/gsi_backfill.rs tells that refusal from the other
lookup errors.

tests/python/test_gsi_backfilling.py now sends a BatchExecuteStatement
of a read of the index and a read of a base item in each phase. The
service's answer to a PartiQL read of a CREATING index is not measured,
so the answers are ExtendDB's.
The Breaking changes section of this PR lists what a client of an
existing deployment sees after the upgrade. The upgrade manual had no
entry for it. The new v1.2.0 entry comes after the two v1.2.0 entries
of main. It names the Backfilling member, the refusal of each CREATING
phase on each backend against 1.1.0, and the per-statement refusal in a
BatchExecuteStatement.
Main moved to sqlx 0.9.1 in #1166, which refuses a query string built at
run time unless AssertSqlSafe wraps it. The two PostgreSQL GSI test files
of this branch built their SQL with format! and passed it by reference,
so after the merge of main their test targets did not compile (E0277).
gsi_backfilling.rs and gsi_create_converge.rs now wrap each built string
in sqlx::AssertSqlSafe, as the other test files of the crate do on main.

@LJMart11 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked at 2a75a22 against the list from ece2581.

The follow-up makes a BatchExecuteStatement read of a GSI whose backfill has started a per-statement error, in check_read and target_schema. Confirmed: on PostgreSQL and SQLite the batch of an index read plus a base-item read now answers 200 in each CREATING phase, with the backfilling sentence on the index statement and the item returned; removing either new arm makes the wire test fail with the whole-call 400. cargo test -p extenddb-engine -p extenddb-storage passed (981 + 296 + 1) under 1.94.0, and the head already contains the base tip.

Previous items: 1 fixed.

Main is at 44a7b79, six commits past c8e66fb: #1446, #1531, #1466,
#1087, #768 and #1296. In docs/manuals/07-upgrade-manual.md, #1446 and
#1531 add v1.2.0 entries at the top of "Behavior Changes by Release",
and git merged them with no conflict. #1087 adds its v1.2.0 entry after
the other v1.2.0 entries of main, where this branch has its entry. That
is the one conflict. The merge keeps both entries without change: the
entry of #1087 first, then the entry of this branch, before the v1.1.0
entries.

Git merged five other files with no conflict:

- crates/storage-cassandra/tests/direct_integration.rs: #1531 declares
  the update_item_retry module, and this branch declares
  gsi_backfilling.
- crates/storage-mongodb/src/table_engine.rs: #1446 changes DeleteTable.
  It marks the table DELETING by table_id, and it removes the tags and
  the table document in one transaction. This branch changes
  create_table_impl_inner, index_info_by_table_id_impl and
  doc_to_table_description_in.
- crates/storage-sqlite/src/lib.rs: #1087 declares the
  scan_segment_tests module, and this branch declares
  gsi_backfilling_tests.
- crates/storage-sqlite/src/store.rs: #1087 registers
  extenddb_scan_segment on each connection of the pool. This branch
  adds the gsi_backfills field and gsi_creating_phase_delay.
- crates/storage/src/lib.rs: #1087 rewrites the doc comment of
  scan_key_in_segment, and this branch declares the gsi_backfill
  module.
@LJMart11

LJMart11 commented Dec 4, 2026

Copy link
Copy Markdown

Checked at 2a75a22 against the list from ece2581. The head moved again to d2bfa44; this reviewer stops after two rounds and leaves the rest to a person.

Fixed:

  • item 1

Disputed: none.

Needs-human: none.

This branch has not been deployed

No deployments
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.

4 participants