Skip to content

fix(postgres): refuse UpdateTable on a non-ACTIVE table without the row lock - #569

Open
LeeroyHannigan wants to merge 7 commits into
mainfrom
fix/556-pg-update-table-deleting-lock-wait
Open

LeeroyHannigan wants to merge 7 commits into
mainfrom
fix/556-pg-update-table-deleting-lock-wait

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

Three commits follow 5130dd7, the head of the reviews of 21 and 23 Nov:

  • 08f6a8d merges origin/main at b0d1c64. The merge has no conflict, and main changes neither file of this branch.
  • 80b2c28 adds update_table_applies_after_a_concurrent_row_update_under_a_repeatable_read_default, the test of the READ COMMITTED pin. The test file now uses the shared scratch fixture in tests/common/stream_disable_fixture.rs and keeps no copy of it, so EXTENDDB_TEST_PG_REQUIRED turns a missing connection string into a failure.
  • 9980dcc corrects the comment on the status pre-read in update_table.rs. The comment said that the pre-read gives the answer the locked read would give. It now names the two answers that change at the boundary.

This body now corrects the lock survey (#1450). It also lists the second answer the pin changes and the two boundary answers. It updates the merge notes, names #570 and gives the counts at this head.

Two sentences of the 3a78aad message are no longer true at this head. "The new test binary is in the PostgreSQL storage-level CI list": the branch adds no list entry, and the job runs the file through cargo test --tests. "One behaviour changes at a boundary": What lists two boundary answers and one more answer the pin changes. The earlier commits stay as the reviewers saw them, and the message of 9980dcc corrects both sentences.

What

crates/storage-postgres/src/update_table.rs: a plain SELECT table_status runs first on the request's transaction and refuses a non-ACTIVE row before the FOR UPDATE read. The locked read and its own status check stay, so the locked read still catches a status that changes between the two reads. The pre-read leaves an absent row to the locked read, which then sees a CreateTable that commits in between. Both reads refuse through one function, refuse_unless_active, so a status that gets its own sentence gets it from both. This branch does not change the poller's SKIP LOCKED claim.

The transaction opens with BEGIN ISOLATION LEVEL READ COMMITTED rather than inheriting the server default. The pre-read is the first statement, and under REPEATABLE READ it would set the snapshot the locked read then has to use. A row updated in between would then make the locked read fail with 40001, where main served the request. The pin also changes one of main's answers. On a database whose default is REPEATABLE READ, an UpdateTable that waits for the row lock while another transaction updates the row now applies. On main, the locked read failed with could not serialize access due to concurrent update, and the request answered 500.

Two answers change at a boundary, when the row changes after the pre-read and before the locked read returns. A storage-level probe measured each case 2 of 2 on PostgreSQL 16.15, with a second transaction that held the change open for 400 ms:

  • An UpdateTable that arrives while a CREATING to ACTIVE activation is uncommitted gets ResourceInUseException Table <name> is not in ACTIVE state. main waited for the lock and applied it. The window is the activating transaction's duration, needs a nonzero control_plane_delay_seconds or a restore target, and no client that has observed ACTIVE can reach it.
  • An UpdateTable that arrives while the removal of a DELETING row is uncommitted, as in a pass of the delete poller, gets the same refusal at once. main waited for the lock and answered ResourceNotFoundException. No client that has observed the removal can reach it.

DeleteTable and, with separate catalog and data databases, CreateBackup also wait behind the poller's hold. delete_table locks the DELETING row FOR UPDATE to return its description (3.01 s under the hold), and the backup open takes the row FOR SHARE before it reads the status. Both waits are on main and outside #556, and #1450 tracks them.

Why

update_table on PostgreSQL took the catalog row FOR UPDATE before reading its status. The delete poller claims a DELETING row FOR UPDATE SKIP LOCKED and holds that lock while its data drop waits out a 3 s lock_timeout. So an UpdateTable landing in that window waited for the attempt to end, then read DELETING and refused. The answer was right every time, and it took up to 3 s to arrive. Measured on #550's branch: twelve calls at 0.5 s spacing, ten refused in 2 to 5 ms, two in 1256 and 2922 ms.

Closes #556

Merging with #550

Both branches change update_table.rs, and the merge conflicts in one place by design. In the locked read's status check, this branch calls refuse_unless_active and #550 adds a DELETING arm. Resolution for whichever lands second: give refuse_unless_active a "DELETING" arm returning TableBeingDeleted, and change the named assertion in update_table_deleting_wait.rs from TableNotActive to TableBeingDeleted. With that resolution on a trial merge of 9980dcc and #550's head 9cbb9fe, #550's seven deleting_window tests and this branch's four pass together. A resolution without the arm compiles and is wrong: the pre-read answers TableNotActive before #550's arm runs, and #550's update_table_during_the_window_is_table_being_deleted fails.

git merge-tree of 9980dcc conflicts in crates/storage-postgres/src/update_table.rs with #550, with #598 and #599 (both on #550's branch) and with #1207 (on #1093's branch). It merges clean with main. The other open PRs that change update_table.rs add no conflict with this branch.

#1166 (sqlx 0.9.1) merges with this branch without a conflict in this branch's two files. sqlx 0.9.1's query takes an impl SqlSafeStr, which a &'static str or an AssertSqlSafe value provides and a &String does not. So the new test's ALTER DATABASE statement, built with format!, then needs sqlx::AssertSqlSafe, as #1166 gives the fixture's CREATE DATABASE.

Found and filed separately

put_create_race fails 9 of 9 on main against a server whose default_transaction_isolation is repeatable read: the data path's transactions assume READ COMMITTED without pinning it. Filed as #570.

Testing done

Shape Before After
storage test, refusal under the poller's hold 3.00 s 0.9 to 1.4 ms idle, 19 to 22 ms at host load 27
wire, twelve UpdateTable during a held window two of twelve at 2953 and 3012 ms all twelve 3.8 to 5.8 ms
answer ResourceInUseException Table <name> is not in ACTIVE state unchanged
storage test, REPEATABLE READ default, row updated during the lock wait could not serialize access due to concurrent update (500) applies

crates/storage-postgres/tests/update_table_deleting_wait.rs is new. The PostgreSQL storage-level job runs it through cargo test --tests (.github/workflows/integration.yml line 790). In the refusal test, delete_table itself opens the window with control_plane_delay_seconds = 1. A second transaction holds the row with the poller's own claim statement and releases it after the poller's lock_timeout. The test asserts that the refusal is TableNotActive naming the table, arrives in under 500 ms, and equals the unheld refusal. update_table_applies_after_a_concurrent_row_update_under_a_repeatable_read_default sets the scratch database's default to repeatable read and holds the ACTIVE row until the call queues on it. Then it updates the row, commits, and requires the update to apply. With EXTENDDB_TEST_PG_REQUIRED=1 and no connection string, each of the four tests fails instead of reporting a skip.

Controls, each a change to update_table.rs with the tests kept:

  • Without the pre-read: the refusal takes 3.00 s.
  • Pre-read moved below the locked read: 3.00 s.
  • Pre-read refusing TableNotFound: the named assertion fails.
  • Pin replaced by begin(): the new test fails with could not serialize access due to concurrent update, and the other three pass.
  • main's update_table.rs: the new test fails the same way, and the refusal takes 3.0023 s against the 500 ms budget.

Counts at 9980dcc, against PostgreSQL 16.15 (pgvector/pgvector:pg16, the image of CI's job) on 127.0.0.1, with EXTENDDB_TEST_PG_REQUIRED=1 and EXTENDDB_EXPECT_VECTORS=1 as in CI's job, in the dev profile:

  • cargo test -p extenddb-storage-postgres --test update_table_deleting_wait: 4 passed.
  • cargo test -p extenddb-storage-postgres --lib, with no connection string: 87 passed.
  • cargo test -p extenddb-storage-postgres --tests --no-fail-fast, the command of the run-postgres-storage-tests job, which CI builds with --release: 41 targets, 482 passed, 2 ignored, 5 failed. The five are three store_identity tests in backup_restore.rs and two tests in catalog_nul_backup_jobs.rs, files this branch does not change. Each one fails because the filesystem backup store refuses a root whose parent directory another user owns. Every temporary directory on the test host has such a parent.
  • cargo +1.97.0 fmt --all -- --check, cargo +1.97.0 clippy -p extenddb-storage-postgres --all-targets -- -D warnings, the same with --features extenddb-engine/backup-test-hooks, and cargo +1.91.1 check -p extenddb-storage-postgres --all-targets: clean. cargo +1.97.0 clippy -p extenddb-storage-postgres --all-targets -- -W clippy::pedantic gives no warning on a line this branch adds.

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

Breaking changes

Three UpdateTable answers on PostgreSQL change from 1.0.0:

  • On a server whose default_transaction_isolation is repeatable read, an UpdateTable that waits for the table's row lock while another transaction updates that row now applies. 1.0.0 answered 500 Internal server error.
  • An UpdateTable that arrives while an uncommitted CREATING to ACTIVE activation holds the row lock gets ResourceInUseException Table <name> is not in ACTIVE state. 1.0.0 waited for the activation and applied the request.
  • An UpdateTable that arrives while the removal of a DELETING row is uncommitted gets the same ResourceInUseException at once. 1.0.0 waited for the removal and answered ResourceNotFoundException.

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.

…ow lock

update_table took the catalog row FOR UPDATE before it read the status. The
delete poller claims a DELETING row FOR UPDATE SKIP LOCKED and holds that lock
while its data drop waits out a 3 s lock_timeout, so an UpdateTable that landed
in that window waited for the poller's attempt to end before it read DELETING
and refused. The answer was already the right one; it took up to 3 s to
arrive, and with several DELETING tables claimed in one pass the catalog row
lock is held for the whole pass, so the wait grows with their number.

update_table now reads table_status without a row lock and refuses a
non-ACTIVE row before it takes FOR UPDATE. The locked read and its status check
are unchanged, so a status that changes between the two reads is caught under
the lock, and a row that is absent at the pre-read is left to the locked read,
which reports TableNotFound or sees a CreateTable that committed in between.
Both reads refuse through one function, refuse_unless_active, so a status that
gets its own sentence gets it from both reads. The poller's lock discipline is
untouched: SKIP LOCKED is what keeps two pollers off one row.

The transaction opens with BEGIN ISOLATION LEVEL READ COMMITTED rather than
inheriting the server default. The pre-read is the transaction's first
statement; under REPEATABLE READ it would fix the snapshot the FOR UPDATE read
then has to use, and a row updated after that snapshot would fail the locked
read with 40001 where main served the request.

One behaviour changes at a boundary: an UpdateTable that arrives while a
CREATING to ACTIVE activation is uncommitted is refused with the CREATING
sentence, where main waited for the lock and applied it. The window is the
activation statement's own duration, needs a nonzero control_plane_delay_seconds
or a restore target, and no client that has observed ACTIVE can reach it.

The storage tests hold the DELETING row with the poller's own claim statement,
release it after the poller's own lock_timeout, and assert the refusal is
TableNotActive naming the table, equals the refusal the call gives with nothing
holding the row, and arrives under 500 ms (measured 0.9 to 1.4 ms idle, 19 to
22 ms under a host load average of 27). With the pre-read removed the same call
takes 3.00 s. On the wire, twelve UpdateTable calls during a held window answer
in 3.8 to 5.8 ms; without the pre-read two of twelve take 2953 and 3012 ms, the
issue's distribution. The new test binary is in the PostgreSQL storage-level CI
list.

Closes #556
@LeeroyHannigan
LeeroyHannigan force-pushed the fix/556-pg-update-table-deleting-lock-wait branch from 639ec4d to 3a78aad Compare October 26, 2026 09:21
@navareamol navareamol added the v1 Needed for the 1.0 release label Nov 3, 2026
@navareamol navareamol added this to the 1.0 milestone Nov 3, 2026
.github/workflows/integration.yml keeps the PostgreSQL storage-level test job from main. Its cargo test --tests command runs every test target of the crate, which includes the update_table_deleting_wait target this branch added to the removed per-test list.
…able fields

Main's catalog baseline seeds no control_plane_delay_seconds row, so the UPDATE in set_delay changed nothing. Under the default delay of 0.25 s, create_table inserted the table as CREATING and delete_table took its synchronous path. The helper now upserts the row, as the other storage-level tests do.

UpdateTableInput gained warm_throughput and sse_specification on main. The request literal in update_input sets both to None.

@hitesh96db hitesh96db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The pre-read fixes the wait. Under the poller's hold the refusal took 2.05 ms at 5130dd7 and 2.43 ms merged with current main, and with the pre-read refusal removed the test fails at 3.0 s.

Findings:

  1. The READ COMMITTED pin has no test. With begin_with("BEGIN ISOLATION LEVEL READ COMMITTED") replaced by begin(), all three tests in update_table_deleting_wait.rs pass. The pin does change the answer: with the database's default_transaction_isolation at repeatable read, a second transaction holding the ACTIVE row FOR UPDATE, and that transaction updating item_count and committing while update_table waits, this branch returns Ok(Active) and the branch without the pin returns Internal("... could not serialize access due to concurrent update") (2 of 2). Please add a test of that shape.

  2. The lock survey in the description does not hold. With the DELETING row held by the poller's claim statement (poller_hold in the new test):

    Call on the DELETING table One database Separate catalog and data databases
    update_table (this branch) 2.3 ms 1.2 ms
    delete_table 3.006 s 3.003 s
    open_table_snapshot (CreateBackup's open) 0.8 ms 3.002 s

    delete_table.rs:55 locks rows with table_status IN ('ACTIVE', 'CREATING', 'DELETING') FOR UPDATE, and snapshot_engine.rs:416 takes the row FOR SHARE before it checks the status. Both are on main at this branch's merge base. Please correct the sentence in the description; the two waits are outside #556 and can be filed separately.

  3. The pin's comment says the pre-read under REPEATABLE READ turns a concurrent update into a 40001 "where main served the request". In the shape above, main's code also answers 40001 (2 of 2), so the pin improves on main there.

  4. base_conn() in the new file does not honor EXTENDDB_TEST_PG_REQUIRED, as backup_restore.rs and 18 other PostgreSQL test files do, so a job without the connection string passes it as skipped.

  5. The #550 merge note says both branches append to the CI --test list. This branch adds none; the file runs through --tests (integration.yml:782).

…h fixture

update_table opens its transaction with BEGIN ISOLATION LEVEL READ COMMITTED, and no test covered that line. With the pin replaced by begin(), the three tests in update_table_deleting_wait.rs passed.

The new test sets the scratch database's default_transaction_isolation to repeatable read and opens a second engine on it. A second transaction holds the ACTIVE row FOR UPDATE and waits until the call queues on that lock. Then it updates the row and commits, and the call must apply. Without the pin, the locked read fails with "could not serialize access due to concurrent update". Main's update_table fails the same way.

So the pin also changes one of main's answers. On a database whose default is REPEATABLE READ, an UpdateTable that waits for the row lock while another transaction updates the row now applies. On main, the locked read failed with "could not serialize access due to concurrent update" and the request answered 500.

The file now uses the scratch fixture in tests/common/stream_disable_fixture.rs and its table input, and keeps no copy of its own. The fixture's base_conn fails a test when the environment sets EXTENDDB_TEST_PG_REQUIRED but no connection string. A job that must reach PostgreSQL no longer passes these tests as skipped. The scratch databases are now named eddb_sdis_*.
The comment on update_table's status pre-read said that the pre-read only shortens a refusal and gives the answer the locked read would give. That is not true when the row changes after the pre-read and before the locked read returns. An uncommitted activation or delete that holds the row lock while the pre-read runs is one such case.

For a CREATING row whose activation then commits, the pre-read returns TableNotActive, where main waited for the lock and applied the request. For a DELETING row whose removal then commits, the pre-read also returns TableNotActive, where main waited for the lock and returned TableNotFound. UpdateTable answers TableNotActive as ResourceInUseException "Table <name> is not in ACTIVE state", and TableNotFound as ResourceNotFoundException. On PostgreSQL 16, each case gave these results 2 of 2 times, with a second transaction that held the change open for 400 ms.

The comment now names both cases. This commit changes no code.

Corrections to the message of 3a78aad. The branch adds no entry to the PostgreSQL storage-level CI list: the job runs the new file through cargo test --tests. More than one behaviour changes: the two answers above, and the answer of the READ COMMITTED pin that 80b2c28 names.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

All five are in 80b2c28 and the description, on top of a merge of main (08f6a8d).

  1. Added update_table_applies_after_a_concurrent_row_update_under_a_repeatable_read_default in 80b2c28, in your shape. The scratch database's default is repeatable read, and a second transaction holds the ACTIVE row FOR UPDATE, then updates it and commits while update_table waits. The test passes with the pin. With begin() in place of the pin it fails with could not serialize access due to concurrent update, and main's update_table.rs fails it the same way.

  2. The description's lock survey now says that DeleteTable and, with separate catalog and data databases, CreateBackup also wait behind the poller's hold. Here delete_table took 3.01 s under the claim. storage-postgres: DeleteTable and CreateBackup wait up to 3 s behind the delete poller's row lock #1450 tracks both waits.

  3. Confirmed: main's update_table.rs fails that test with the same 40001, and this branch applies the update. The description's What section and the message of 80b2c28 now list it as a second answer the pin changes. The sentence about main serving the request stays. It covers the other case, a row updated between the two reads with no lock wait, where main reads the row once.

  4. Fixed in 80b2c28. The file now uses the shared fixture in tests/common/stream_disable_fixture.rs and keeps no copy of the scratch setup. The fixture's base_conn fails the test when EXTENDDB_TEST_PG_REQUIRED is set and the connection string is not. With EXTENDDB_TEST_PG_REQUIRED=1 and no connection string, all four tests now fail with the fixture's message.

  5. The branch adds no CI entry. The job runs every test target of the crate with --tests (integration.yml:790 at this head), and the only conflict with fix: answer requests during a table's DELETING window as the service does #550 is in update_table.rs. The merge note now says so. On a trial merge with fix: answer requests during a table's DELETING window as the service does #550's current head, fix: answer requests during a table's DELETING window as the service does #550's seven deleting_window tests and this branch's four pass together.

9980dcc also fixes the comment on the pre-read in update_table.rs, which said the pre-read gives the answer the locked read would give. Two answers differ when the row changes between the reads: a CREATING row whose activation commits, and a DELETING row whose removal commits. The comment and the description now name both.

@hitesh96db hitesh96db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All five items from my first review are fixed or answered. Checked at 9980dcc2 on PostgreSQL 17.11. I have no open requests on this PR.

  1. The new test update_table_applies_after_a_concurrent_row passes (15 of 15 alone, 8 parallel runs of the file). With the pin replaced by begin() it fails with could not serialize access due to concurrent update and the other three tests pass. With main's update_table.rs it fails the same way. The refusal test fails without the pre-read (3.001 s against the 500 ms budget).
  2. delete_table took 3.004 s under the poller's hold. open_table_snapshot took 3.002 s with separate catalog and data databases and 0.6 ms with one. The corrected sentence in the description matches, and #1450 exists.
  3. Answered. Main also returns 40001 in the lock-wait shape, as the author says. With a loop of committed row updates running while 400 UpdateTables ran under a repeatable read default, the head had 0 errors, the variant with the pin removed 294, and main 266. The retained sentence about a commit landing between the two reads without a lock wait needs a hook between two statements, and I could not reproduce it.
  4. With EXTENDDB_TEST_PG_REQUIRED=1 and the connection string unset or empty, all four tests fail with the fixture's message. Without the variable they skip.
  5. integration.yml:790 runs --tests and has no per-file --test entry. A trial merge with #550's head 9cbb9fed conflicts only in update_table.rs. The naive resolution compiles but fails 1 of #550's 7 deleting_window tests. The resolution in the description passes those 7 plus this PR's 4.

New in the delta: an UpdateTable that arrives during an uncommitted CREATING to ACTIVE activation is now refused in 1.0 ms, where main waited 409 ms and applied it. DynamoDB refuses an UpdateTable on a CREATING table with ResourceInUseException (Attempt to change a resource which is still in use: Table is being created: <name>, 3 of 3), so the head moves toward the service. Two concurrent UpdateTables under a repeatable read default gave 10 of 10 OK on the head and 4 failures out of 10 on main.

Optional: no test pins that a missing row is left to the locked read. A pre-read that answers TableNotFound itself passes all four tests.

CI: every check at 9980dcc2 passes. The branch merges cleanly onto current main.

Not measured: the DELETING window on DynamoDB (I used the recorded measurement from #599), and SQLite, MongoDB and Cassandra.

@hitesh96db
hitesh96db added this pull request to the merge queue Nov 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 26, 2026
@hitesh96db
hitesh96db added this pull request to the merge queue Nov 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 26, 2026
@LeeroyHannigan
LeeroyHannigan added this pull request to the merge queue Nov 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 26, 2026
@hitesh96db
hitesh96db added this pull request to the merge queue Nov 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 30, 2026

@navareamol navareamol 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.

The new test in update_table_deleting_wait.rs builds its ALTER DATABASE statement with sqlx::query(&format!(...)). Main is now on sqlx 0.9.1, and main's own fixture wraps such statements in sqlx::AssertSqlSafe. I think this file will not compile on a branch that has main merged. Could you merge main and wrap that call in sqlx::AssertSqlSafe?

The description says main changes neither file of this branch. Main now changes update_table.rs in the GSI delete path. The merge is clean, so only the sentence needs updating.

The pin applies READ COMMITTED to every UpdateTable. Has anyone checked a database whose default is SERIALIZABLE? I think the explicit level makes the answer the same as on a READ COMMITTED default, but I have not measured it.

I will approve once the test compiles on a branch with main merged, CI is green on it, and the description matches main.

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

v1 Needed for the 1.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage-postgres: UpdateTable during the DELETING window waits up to 3 s behind the delete poller's lock

3 participants