Repository navigation
fix(storage-postgres): set READ COMMITTED on the server's and the stores' sessions - #1340
LeeroyHannigan wants to merge 1 commit into
Conversation
…res' sessions The backend's statements expect READ COMMITTED, but its pools took the default_transaction_isolation that postgresql.conf, ALTER DATABASE, ALTER ROLE or the connection string's options set. Under REPEATABLE READ or SERIALIZABLE, a write that waited for another write to the same item failed. A PutItem or DeleteItem answered HTTP 500 (InternalServerError), and a TransactWriteItems was canceled with a TransactionConflict reason. pool_options() gives a pool an after_connect hook that sets default_transaction_isolation to read committed. The engine's catalog and data pools, the server's catalog store pool and the settings and management store pools use it. The backend opens the session that holds a backup or restore job's claim outside the pool, so that session sets the level itself. A transaction that needs one snapshot keeps its own BEGIN level. The backend sets the level per session. A BEGIN ISOLATION LEVEL does not reach the statements that run outside a transaction. A PutItem or DeleteItem with no condition, index or stream runs one such statement. Under REPEATABLE READ such a statement failed with SQLSTATE 40001 when another write committed while it waited. Behind a pooler in transaction mode, the session setting reaches one server connection only, and stays there for the pool's later clients. The deployment guide and the upgrade manual therefore ask for read committed as the role's and the database's default there. Fixes #570
demohan
left a comment
There was a problem hiding this comment.
Concurrent writes to one item answer as the service does at fc98878 whatever the operator's default isolation. With 16 writers for 5 rounds on each of nine non-transaction write shapes, twice, under a role default of REPEATABLE READ, a database default of REPEATABLE READ, a role default of SERIALIZABLE and the connection string's options, this head matched the service in us-east-1 on every shape, with a GSI and a stream on the table too, where main answered 500 on 34 to 70 of every 80 writes. Under REPEATABLE READ, GSI backfill under load, streams, backup, restore and a TTL sweep ran with no PostgreSQL error, no path relies on a stronger level, and the storage-postgres suite passes 460 of 460 at that default. All nine negative controls fail read_committed.rs, fmt, clippy and the 1.91.1 check are clean, and CI is green on 58 of 58.
Merged with main at 222d80a, check -p extenddb passes, but the tests do not build: sqlx 0.9.1 (#1166) refuses the dynamic SQL at read_committed.rs:75 and :98 and put_create_race.rs:167 with E0277, and wrapping those three in sqlx::AssertSqlSafe makes the three files pass under REPEATABLE READ with clippy and the 1.94.0 check clean. The upgrade manual conflicts, since both sides add v1.1.0 entries, and v1.1.0 is already tagged without this change.
I will approve once main is merged and the tests build, and the serving registration session runs at READ COMMITTED or the guide names it.
|
|
||
| ### Transaction isolation | ||
|
|
||
| ExtendDB's PostgreSQL statements are written for `READ COMMITTED`. The server sets `default_transaction_isolation` to `read committed` before the first statement of each connection of the pools that serve requests and run background work, and of the session that holds a backup or restore job's claim. That setting replaces a default from `postgresql.conf`, `ALTER DATABASE`, `ALTER ROLE` or the connection string's `options`. The `settings`, `encryption-key` and `backup` commands set it on the pools they read and write settings, accounts and items through. `verify` sets it on the connection it checks the catalog with. The migration runner's sessions, `init`, `destroy` and `catalog-check` keep the server's default. A transaction that needs one snapshot, such as TransactGetItems or the read of a backup, names its own level. |
There was a problem hiding this comment.
The serving registration session (migrations/locks.rs:432) is not in this list and keeps the default. It holds one transaction open for the server's life, so under a role default of REPEATABLE READ it held backend_xmin 65661 as long as the server ran, and VACUUM (VERBOSE) settings reported "2 are dead but not yet removable" with the removable cutoff fixed at 65661; at READ COMMITTED it holds no xmin. The catalog database, and the item tables when they share it, cannot be vacuumed for that time. Main does the same, and no answer was wrong. Setting READ COMMITTED on that connection before its BEGIN, with a case in read_committed.rs, would close it; otherwise this list could name it and its effect on vacuum.
|
|
||
| ### v1.1.0: PostgreSQL sets `READ COMMITTED` on the server's and the stores' sessions | ||
|
|
||
| **What changed.** The PostgreSQL backend sets `default_transaction_isolation` to `read committed` before the first statement of each connection of the pools that serve requests and run background work, and of the session that holds a backup or restore job's claim. [Transaction isolation](11-deployment-guide.md#transaction-isolation) in the deployment guide lists the commands that set it too. A default from `postgresql.conf`, `ALTER DATABASE`, `ALTER ROLE` or the connection string's `options` no longer applies to them. 1.0.0 ran them at that default. Under `repeatable read` or `serializable`, a write that waited for another write to the same item failed. A PutItem or DeleteItem answered HTTP 500 (`InternalServerError`), and a TransactWriteItems was canceled with a `TransactionConflict` reason. Transactions that need one snapshot (TransactGetItems, the reads of a backup and of a table snapshot) name their own level, as before. |
There was a problem hiding this comment.
On main under REPEATABLE READ, UpdateItem with ADD also answered 500 (68 of 80), and conditional writes answered 500 on 55 to 60 of 80 where ConditionalCheckFailedException was due, so "a PutItem or DeleteItem" leaves out writes a reader may have seen fail.
What
The PostgreSQL backend sets
default_transaction_isolationtoread committedon each new connection of the server's and the stores' pools.pool_options()incrates/storage-postgres/src/lib.rsgives a pool anafter_connecthook that runs theSET. The engine's catalog and data pools, the server's catalog store pool, and the settings and management store pools use it. The session that holds a backup or restore job's claim opens outside the pool, sotry_claim_withsets the level on it too.Transactions that need one snapshot (TransactGetItems, the reads of a backup and of a table snapshot) still name
REPEATABLE READin theirBEGIN, which overrides the session default.The deployment guide gets a "Transaction isolation" section. It lists the commands that set the level and the sessions that keep the server's default. The upgrade manual gets a v1.1.0 entry, and its PgBouncer paragraph names
default_transaction_isolationas a setting the pools send outside a transaction.Why
Fixes #570
The backend's statements expect READ COMMITTED, but the pools took the default that
postgresql.conf,ALTER DATABASE,ALTER ROLEor the connection string'soptionsset. I ran main 8c64333 with REPEATABLE READ on a role, on the server and in the connection string'soptions, one source per run. In each run all 9 tests ofput_create_race.rsfailed with SQLSTATE 40001, which the engine answers as 500. 4 of the 8 tests oftwi_conflict.rsfailed with aTransactionConflictcancellation. A SERIALIZABLE role default gave the same counts. With REPEATABLE READ set on the database, the new race test below fails the same way on main.The fix sets the level per session because a
BEGIN ISOLATION LEVEL READ COMMITTEDdoes not reach statements that run outside a transaction. A PutItem or DeleteItem with no condition, index or stream runs one such statement. On main under REPEATABLE READ, both failed with 40001 when another write committed while they waited.Known issue: behind PgBouncer in transaction mode, the session
SETreaches one server connection only, and stays on it for the pool's later clients. The docs ask forread committed, PostgreSQL's default, as the role's and the database's default there. #1337 proposes a startup warning for that layout.Testing done
crates/storage-postgres/tests/read_committed.rs(new) migrates a catalog and a data database and sets REPEATABLE READ as their default. A trigger records the isolation of each statement that writes a probed table. The file's one test checks CreateTable on the engine's catalog pool, and a conditional PutItem and a PutItem outside a transaction on the data pool. It also checks CreateAccount through the server's catalog store and through the management store, a setting through the settings store, and a job's claim session. Its last step opens a transaction on each of the data pool's 10 connections.put_create_race.rsruns its first race test again with REPEATABLE READ as the database's default. It keeps its own copy of theALTER DATABASE ... SET default_transaction_isolationsetup. The three test files that run it share no module, and a shared helper would changestream_commit_order.rs, which this fix does not touch.SETremovedSETmoved fromafter_connecttobefore_acquireput_create_raceandtwi_conflictpass on this branch under each of the three REPEATABLE READ sources: a role, the server and the connection string'soptions. They also pass under a SERIALIZABLE role default.cargo test -p extenddb-storage-postgres --testsagainstpgvector/pgvector:pg16: 455 passed and 2 ignored. Five filesystem backup store tests failed on this branch and on main in the same way. The local host's directories belong to another user, so the store refuses the backup root there. CI runs those tests.cargo +1.97.0 fmt --checkpasses.cargo clippy -p extenddb-storage-postgres --all-targets -- -D warningspasses, plain and with--features extenddb-engine/backup-test-hooksas in the secondclippy.ymlcommand. The firstclippy.ymlcommand turns on test hooks of the Cassandra and MongoDB crates only.cargo +1.91.1 check -p extenddb-storage-postgres --all-targetspasses.clippy::pedanticreports one warning on the changed lines:too_many_lineson the new test, where the lint counts 137 lines against its limit of 100.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
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.