Repository navigation
fix(postgres): refuse UpdateTable on a non-ACTIVE table without the row lock - #569
LeeroyHannigan wants to merge 7 commits into
Conversation
…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
639ec4d to
3a78aad
Compare
.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
left a comment
There was a problem hiding this comment.
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:
-
The READ COMMITTED pin has no test. With
begin_with("BEGIN ISOLATION LEVEL READ COMMITTED")replaced bybegin(), all three tests inupdate_table_deleting_wait.rspass. The pin does change the answer: with the database'sdefault_transaction_isolationatrepeatable read, a second transaction holding the ACTIVE rowFOR UPDATE, and that transaction updatingitem_countand committing whileupdate_tablewaits, this branch returnsOk(Active)and the branch without the pin returnsInternal("... could not serialize access due to concurrent update")(2 of 2). Please add a test of that shape. -
The lock survey in the description does not hold. With the DELETING row held by the poller's claim statement (
poller_holdin 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_table3.006 s 3.003 s open_table_snapshot(CreateBackup's open)0.8 ms 3.002 s delete_table.rs:55locks rows withtable_status IN ('ACTIVE', 'CREATING', 'DELETING') FOR UPDATE, andsnapshot_engine.rs:416takes the rowFOR SHAREbefore 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. -
The pin's comment says the pre-read under REPEATABLE READ turns a concurrent update into a 40001 "where
mainserved the request". In the shape above, main's code also answers 40001 (2 of 2), so the pin improves on main there. -
base_conn()in the new file does not honorEXTENDDB_TEST_PG_REQUIRED, asbackup_restore.rsand 18 other PostgreSQL test files do, so a job without the connection string passes it as skipped. -
The #550 merge note says both branches append to the CI
--testlist. 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.
|
All five are in 80b2c28 and the description, on top of a merge of main (08f6a8d).
9980dcc also fixes the comment on the pre-read in |
hitesh96db
left a comment
There was a problem hiding this comment.
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.
- The new test
update_table_applies_after_a_concurrent_rowpasses (15 of 15 alone, 8 parallel runs of the file). With the pin replaced bybegin()it fails withcould not serialize access due to concurrent updateand the other three tests pass. With main'supdate_table.rsit fails the same way. The refusal test fails without the pre-read (3.001 s against the 500 ms budget). delete_tabletook 3.004 s under the poller's hold.open_table_snapshottook 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.- 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 readdefault, 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. - With
EXTENDDB_TEST_PG_REQUIRED=1and the connection string unset or empty, all four tests fail with the fixture's message. Without the variable they skip. integration.yml:790runs--testsand has no per-file--testentry. A trial merge with #550's head9cbb9fedconflicts only inupdate_table.rs. The naive resolution compiles but fails 1 of #550's 7deleting_windowtests. 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.
navareamol
left a comment
There was a problem hiding this comment.
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.
Changes since the last review
Three commits follow 5130dd7, the head of the reviews of 21 and 23 Nov:
origin/mainat b0d1c64. The merge has no conflict, andmainchanges neither file of this branch.update_table_applies_after_a_concurrent_row_update_under_a_repeatable_read_default, the test of theREAD COMMITTEDpin. The test file now uses the shared scratch fixture intests/common/stream_disable_fixture.rsand keeps no copy of it, soEXTENDDB_TEST_PG_REQUIREDturns a missing connection string into a failure.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 plainSELECT table_statusruns first on the request's transaction and refuses a non-ACTIVErow before theFOR UPDATEread. 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 aCreateTablethat 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'sSKIP LOCKEDclaim.The transaction opens with
BEGIN ISOLATION LEVEL READ COMMITTEDrather than inheriting the server default. The pre-read is the first statement, and underREPEATABLE READit 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, wheremainserved the request. The pin also changes one ofmain's answers. On a database whose default isREPEATABLE READ, anUpdateTablethat waits for the row lock while another transaction updates the row now applies. Onmain, the locked read failed withcould 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:
UpdateTablethat arrives while aCREATINGtoACTIVEactivation is uncommitted getsResourceInUseExceptionTable <name> is not in ACTIVE state.mainwaited for the lock and applied it. The window is the activating transaction's duration, needs a nonzerocontrol_plane_delay_secondsor a restore target, and no client that has observedACTIVEcan reach it.UpdateTablethat arrives while the removal of aDELETINGrow is uncommitted, as in a pass of the delete poller, gets the same refusal at once.mainwaited for the lock and answeredResourceNotFoundException. No client that has observed the removal can reach it.DeleteTableand, with separate catalog and data databases,CreateBackupalso wait behind the poller's hold.delete_tablelocks theDELETINGrowFOR UPDATEto return its description (3.01 s under the hold), and the backup open takes the rowFOR SHAREbefore it reads the status. Both waits are onmainand outside #556, and #1450 tracks them.Why
update_tableon PostgreSQL took the catalog rowFOR UPDATEbefore reading its status. The delete poller claims aDELETINGrowFOR UPDATE SKIP LOCKEDand holds that lock while its data drop waits out a 3 slock_timeout. So anUpdateTablelanding in that window waited for the attempt to end, then readDELETINGand 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 callsrefuse_unless_activeand #550 adds aDELETINGarm. Resolution for whichever lands second: giverefuse_unless_activea"DELETING"arm returningTableBeingDeleted, and change the named assertion inupdate_table_deleting_wait.rsfromTableNotActivetoTableBeingDeleted. With that resolution on a trial merge of 9980dcc and #550's head 9cbb9fe, #550's sevendeleting_windowtests and this branch's four pass together. A resolution without the arm compiles and is wrong: the pre-read answersTableNotActivebefore #550's arm runs, and #550'supdate_table_during_the_window_is_table_being_deletedfails.git merge-treeof 9980dcc conflicts incrates/storage-postgres/src/update_table.rswith #550, with #598 and #599 (both on #550's branch) and with #1207 (on #1093's branch). It merges clean withmain. The other open PRs that changeupdate_table.rsadd 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
querytakes animpl SqlSafeStr, which a&'static stror anAssertSqlSafevalue provides and a&Stringdoes not. So the new test'sALTER DATABASEstatement, built withformat!, then needssqlx::AssertSqlSafe, as #1166 gives the fixture'sCREATE DATABASE.Found and filed separately
put_create_racefails 9 of 9 onmainagainst a server whosedefault_transaction_isolationisrepeatable read: the data path's transactions assume READ COMMITTED without pinning it. Filed as #570.Testing done
UpdateTableduring a held windowResourceInUseExceptionTable <name> is not in ACTIVE stateREPEATABLE READdefault, row updated during the lock waitcould not serialize access due to concurrent update(500)crates/storage-postgres/tests/update_table_deleting_wait.rsis new. The PostgreSQL storage-level job runs it throughcargo test --tests(.github/workflows/integration.ymlline 790). In the refusal test,delete_tableitself opens the window withcontrol_plane_delay_seconds = 1. A second transaction holds the row with the poller's own claim statement and releases it after the poller'slock_timeout. The test asserts that the refusal isTableNotActivenaming the table, arrives in under 500 ms, and equals the unheld refusal.update_table_applies_after_a_concurrent_row_update_under_a_repeatable_read_defaultsets the scratch database's default torepeatable readand holds theACTIVErow until the call queues on it. Then it updates the row, commits, and requires the update to apply. WithEXTENDDB_TEST_PG_REQUIRED=1and no connection string, each of the four tests fails instead of reporting a skip.Controls, each a change to
update_table.rswith the tests kept:TableNotFound: the named assertion fails.begin(): the new test fails withcould not serialize access due to concurrent update, and the other three pass.main'supdate_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, withEXTENDDB_TEST_PG_REQUIRED=1andEXTENDDB_EXPECT_VECTORS=1as 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 therun-postgres-storage-testsjob, which CI builds with--release: 41 targets, 482 passed, 2 ignored, 5 failed. The five are threestore_identitytests inbackup_restore.rsand two tests incatalog_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, andcargo +1.91.1 check -p extenddb-storage-postgres --all-targets: clean.cargo +1.97.0 clippy -p extenddb-storage-postgres --all-targets -- -W clippy::pedanticgives no warning on a line this branch adds.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, 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
UpdateTableanswers on PostgreSQL change from 1.0.0:default_transaction_isolationisrepeatable read, anUpdateTablethat waits for the table's row lock while another transaction updates that row now applies. 1.0.0 answered 500Internal server error.UpdateTablethat arrives while an uncommittedCREATINGtoACTIVEactivation holds the row lock getsResourceInUseExceptionTable <name> is not in ACTIVE state. 1.0.0 waited for the activation and applied the request.UpdateTablethat arrives while the removal of aDELETINGrow is uncommitted gets the sameResourceInUseExceptionat once. 1.0.0 waited for the removal and answeredResourceNotFoundException.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.