Skip to content

fix(storage-postgres): set READ COMMITTED on the server's and the stores' sessions - #1340

Open
LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/570-postgres-read-committed
Open

LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/570-postgres-read-committed

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

What

The PostgreSQL backend sets default_transaction_isolation to read committed on each new connection of the server's and the stores' pools. pool_options() in crates/storage-postgres/src/lib.rs gives a pool an after_connect hook that runs the SET. 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, so try_claim_with sets the level on it too.

Transactions that need one snapshot (TransactGetItems, the reads of a backup and of a table snapshot) still name REPEATABLE READ in their BEGIN, 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_isolation as 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 ROLE or the connection string's options set. I ran main 8c64333 with REPEATABLE READ on a role, on the server and in the connection string's options, one source per run. In each run all 9 tests of put_create_race.rs failed with SQLSTATE 40001, which the engine answers as 500. 4 of the 8 tests of twi_conflict.rs failed with a TransactionConflict cancellation. 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 COMMITTED does 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 SET reaches one server connection only, and stays on it for the pool's later clients. The docs ask for read 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.rs runs its first race test again with REPEATABLE READ as the database's default. It keeps its own copy of the ALTER DATABASE ... SET default_transaction_isolation setup. The three test files that run it share no module, and a shared helper would change stream_commit_order.rs, which this fix does not touch.
  • The new test and the new race test fail on main's source. Each of these changes also fails at least one of them:
    • the hook removed from any one of the five pools
    • the claim session's SET removed
    • REPEATABLE READ in the hook
    • the SET moved from after_connect to before_acquire
  • put_create_race and twi_conflict pass on this branch under each of the three REPEATABLE READ sources: a role, the server and the connection string's options. They also pass under a SERIALIZABLE role default.
  • cargo test -p extenddb-storage-postgres --tests against pgvector/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 --check passes. cargo clippy -p extenddb-storage-postgres --all-targets -- -D warnings passes, plain and with --features extenddb-engine/backup-test-hooks as in the second clippy.yml command. The first clippy.yml command turns on test hooks of the Cassandra and MongoDB crates only. cargo +1.91.1 check -p extenddb-storage-postgres --all-targets passes.
  • clippy::pedantic reports one warning on the changed lines: too_many_lines on the new test, where the lint counts 137 lines against its limit of 100.

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


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.

…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 demohan 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.

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.

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

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.

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.

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.

storage-postgres: the data path assumes READ COMMITTED and does not pin it; put_create_race fails under a repeatable read default

2 participants