Skip to content

fix(storage-postgres): unlock a refused maintenance claim before closing it - #1194

Merged
LeeroyHannigan merged 3 commits into
mainfrom
fix/1094-maintenance-claim-unlock
Nov 23, 2026
Merged

LeeroyHannigan merged 3 commits into
mainfrom
fix/1094-maintenance-claim-unlock

Conversation

@hitesh96db

@hitesh96db hitesh96db commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What

On PostgreSQL, a maintenance take that is not kept now releases the maintenance advisory lock on its claim session before it closes the session. That covers a take refused because the previous holder's row is still younger than the stale window, and a take stopped by an error in the young-row read or the INSERT. The unlock statement is now one method, ClaimSession::unlock, called by release and by end_unkept_maintenance_take.

Why

maintenance_claim takes the maintenance advisory lock on a dedicated session. When the row it then finds is still young, it refuses with AlreadyExists and closes the session. The close did not unlock first. The server frees a closed session's lock when its backend exits, and that can happen after close returns. Until it does, check_maintenance_claim finds the lock held and answers that a claim is live. That check runs in every CreateBackup and RestoreTableFromBackup request, so those requests could see a claim that is not there in the moment after a refused maintenance command. The two error returns dropped the session with the lock held, which leaves the same window.

The same window made catalog_index::the_maintenance_claim_excludes_a_second_holder_and_expires_when_stale fail once in CI at crates/storage-postgres/tests/backup_restore.rs (a stale claim is not held). The release path already unlocked before it closed; every path that ends a take now does the same. A failed unlock on a take that is not kept is logged, and the close still frees the lock, only later.

Closes #1094

Testing done

catalog_index::a_refused_maintenance_take_leaves_no_lock_behind is new. It writes a young maintenance row, then makes the take through a second engine whose connections pass through the test proxy with a 100 ms delay on every chunk from client to server. The close's Terminate then reaches the server 100 ms after the take returns. Right after the take, check_maintenance_claim on the direct engine, with a 1 ms window so the row alone cannot answer held, must find the lock free. For it, the PostgreSQL test proxy (tests/migrations_support/proxy.rs) gains to_server_delay_ms.

Against a local PostgreSQL 17 with EXTENDDB_TEST_PG_REQUIRED=1:

Run Result
The new test, refused path closing without the unlock fails 10 of 10 runs: the refused take's lock is still held after it returned
The new test, with the change passes 50 of 50 runs, about 3.5 s each
The four maintenance tests, with the change pass 10 of 10 runs
--test backup_restore 76 of 76 pass
--test migrations_guards, the other user of the proxy its proxy tests pass

Two more tests cover the error-path unlocks. a_maintenance_take_whose_insert_fails_leaves_no_lock_behind installs a BEFORE INSERT trigger that raises in the scratch catalog, so the INSERT fails after the young-row read finds no row. a_maintenance_take_whose_young_row_read_fails_leaves_no_lock_behind renames heartbeat_at for the take, so the young-row read fails on its own, and renames it back before the check. Both make the take through the same 100 ms proxy and check the lock right after it returns. With the unlock in end_unkept_maintenance_take disabled, all three take tests fail in 3 of 3 runs; with it, all three pass, and the six maintenance tests passed in 3 of 3 runs. --test migrations_guards passes apart from a_role_that_does_not_own_the_tables_is_told_what_to_run, which fails on my host because the catalog tables are owned by my local role.

cargo +1.97.0 test --release -p extenddb-storage-postgres --test backup_restore
cargo +1.97.0 fmt --all -- --check
cargo +1.97.0 clippy -p extenddb-storage-postgres --all-targets -- -D warnings

All clean.

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

None.


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.

…ing it

maintenance_claim takes the maintenance advisory lock on a dedicated
session, finds the previous holder's row still younger than the stale
window, and refuses with AlreadyExists. It closed the session without
unlocking. The server frees a closed session's lock when its backend
exits, which can be after close returns. Until then
check_maintenance_claim, which runs in every CreateBackup and
RestoreTableFromBackup request, found the lock held and answered that a
claim was live.

The refused path now runs pg_advisory_unlock on the session before
closing it, as the release path does. A failed unlock is logged and the
close still frees the lock, only later.

A loop of a refused take followed at once by the check, against a row
older than the check's window, read the closing session's lock as a live
claim 3 times and 2 times in two runs of 2,000 rounds. With the unlock
it did so 0 times in 3 runs of 2,000 rounds.

Closes #1094

Signed-off-by: Hitesh Sharma <hts@amazon.com>

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

Thanks, the diagnosis holds. ClaimSession::close returns before the server's backend exits, and until then check_maintenance_claim reads the lock as held. I ran your loop against PostgreSQL 16, 2,000 rounds per run. Each round writes a young row, waits 20 ms, makes a refused take, then checks with a 1 ms window. Without the unlock, 70 to 84 rounds read the lock as held in each of three runs. With it, 0 in each of four runs. That's about 4 percent of rounds on this loaded host, against the 0.1 percent in the description.

Before it merges:

  1. Please add a test. A deterministic one takes about 2 s. In it, a second engine reaches the server through a proxy that holds each chunk from client to server for 100 ms on its way. The refused take's Terminate then reaches the server 100 ms after close returns. A check_maintenance_claim on the direct engine right after the take still finds the lock. That test failed 10 of 10 runs without the unlock and passed 50 of 50 with it. crates/storage-postgres/tests/migrations_support/proxy.rs already has a Proxy that can pause and cut a connection. Please give it a client-to-server delay, as #1181 does for MongoDB's PausingProxy, rather than adding a second proxy.
  2. The new unlock is a second copy of the one in release (backup_catalog.rs:1697-1702), and its comment repeats release's doc. Please put the statement in one method next to ClaimSession::close, for example async fn unlock(&mut self, key: &str) -> Result<(), sqlx::Error>, and call it from both places.

You could also unlock on the two error returns. The young-row read (backup_catalog.rs:1331) and the INSERT (:1365) return on an error with the lock held, and the dropped session frees it only when its backend exits. If you add the method, both returns can call it first.

catalog_index::the_maintenance_claim_excludes_a_second_holder_and_expires_when_stale passed 100 of 100 runs. --test backup_restore passed apart from 3 store_identity tests, which fail on this host because of the owner of an ancestor directory and pass in CI. #1166 (sqlx 0.9.1) leaves release's unlock as it is, so whichever of the two merges second still builds.

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

Thanks, both are in, and so are the unlocks on the two error returns. With the refused path closing without the unlock, the new test failed 10 of 10 runs. With the unlock it passed 50 of 50, at 4.0 to 5.6 s per run against PostgreSQL 16 on a loaded host. pg_advisory_unlock now appears only in ClaimSession::unlock. --test migrations_guards, the other user of the proxy, passed 7 of 7. --test backup_restore passed apart from the 3 store_identity tests that fail on this host, also with main at ca768ed merged in. fmt and both of CI's clippy commands are clean.

Before it merges, please fix three sentences:

  1. The description says that neither the young-row read nor the INSERT can be made to fail on its own. Both can fail on their own. In psql against PostgreSQL 16, one session took the maintenance lock and ran the take's two statements. A BEFORE INSERT trigger that raises made the INSERT fail after the young-row read returned no row, and pg_advisory_unlock on that session then returned true. With heartbeat_at renamed, the young-row read failed on its own. So a test can install the trigger in the scratch catalog and make the take through the proxy, as the refused-take test does. Please add that test, or say that the error-path unlocks have no test.
  2. The new commit message says "so no path leaves the lock to the close". A failed unlock still leaves it to the close, as the doc of end_unkept_maintenance_take and the description say. "so no path skips the unlock" would be true. The commit message goes into main's history when the PR merges.
  3. The doc of ClaimSession::unlock says that a caller that ends the session runs it before close. backup_catalog.rs ends a ClaimSession with close alone at lines 916, 995, 1133 and 1199. Please limit the sentence to a caller whose lock another connection can check right after the close.

The new commit also has no Signed-off-by line, where 9b7ae17 has one.

Hitesh Sharma added 2 commits November 20, 2026 16:59
Move the advisory unlock into ClaimSession::unlock, called by release
and by end_unkept_maintenance_take. A take now unlocks before it closes
its session when it is refused for a young row and when the young-row
read or the INSERT fails, so no path skips the unlock. A failed unlock
is logged, and the close then frees the lock once the server's backend
exits.

Add a test that makes the refused take through a proxy holding each
chunk to the server for 100 ms, so the close's Terminate arrives after
the take returns, and checks the lock is free right after the take.
The proxy gains a client-to-server delay for it.

Signed-off-by: Hitesh Sharma <hts@amazon.com>
… take

Add tests for a take stopped by a failing INSERT (a BEFORE INSERT
trigger that raises) and by a failing young-row read (heartbeat_at
renamed for the take). Each makes the take through the 100 ms proxy and
checks the lock is free right after it returns. Without the unlock in
end_unkept_maintenance_take all three maintenance-take tests fail.

Limit the doc of ClaimSession::unlock to a caller whose lock another
connection can check right after the close.

Signed-off-by: Hitesh Sharma <hts@amazon.com>
@hitesh96db
hitesh96db force-pushed the fix/1094-maintenance-claim-unlock branch from a2d6663 to 40d60b5 Compare November 21, 2026 00:59
@hitesh96db

Copy link
Copy Markdown
Contributor Author

Thanks, all three sentences are fixed, and the error-path unlocks now have tests.

  1. Error-path tests. Added a_maintenance_take_whose_insert_fails_leaves_no_lock_behind (a BEFORE INSERT trigger that raises in the scratch catalog) and a_maintenance_take_whose_young_row_read_fails_leaves_no_lock_behind (heartbeat_at renamed for the take, renamed back before the check, since the check reads it too). Both take through the 100 ms proxy, like the refused-take test, and I moved the proxy setup and the lock check into two small helpers the three tests share. With the unlock in end_unkept_maintenance_take disabled, all three tests failed in 3 of 3 runs; with it, they pass, and the six maintenance tests passed in 3 of 3 runs. The description now says this instead of "cannot be made to fail".
  2. Commit message. 951cfae6 now reads "so no path skips the unlock" and says a failed unlock is logged and the close frees the lock later. This needed a force-push to the PR branch (8fc056a4 and a2d6663f are replaced by 951cfae6 and 40d60b53; the tree is identical to what you reviewed apart from the message and sign-off).
  3. ClaimSession::unlock doc. It now says a caller whose lock another connection can check right after the close runs it before close, so it no longer claims that for the callers that end with close alone.

Both rewritten commits and the new one carry a Signed-off-by line.

fmt and clippy -p extenddb-storage-postgres --all-targets -- -D warnings are clean. --test migrations_guards passes 6 of 7 here. a_role_that_does_not_own_the_tables_is_told_what_to_run fails on my host because the catalog tables are owned by my local role. I did not rerun the full --test backup_restore after this change, only the maintenance tests, so CI covers the rest.

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

Thanks, all three are in. With end_unkept_maintenance_take reduced to a close, each of the three take tests failed at its lock assertion in 3 of 3 runs. With the unlock, each passed 20 of 20. Each test's catalog is its own database, so the trigger and the rename stay out of the other tests. --test backup_restore passed apart from the 3 store_identity tests that fail on this host. --test migrations_guards passed 7 of 7, including a_role_that_does_not_own_the_tables_is_told_what_to_run. The take tests also passed with main at f0170bd merged in.

The two new tests accept any error from the take. A take that fails before it holds the lock, for example on the proxy's connection, would also pass them. Matching the error they provoke, a StorageError::Internal with "insert refused" or "heartbeat_at" in it, would rule that out. The description's --test backup_restore row still says 76 of 76, and the binary has 78 tests now.

@LeeroyHannigan
LeeroyHannigan added this pull request to the merge queue Nov 23, 2026
Merged via the queue into main with commit 1d3be63 Nov 23, 2026
67 of 72 checks passed
@LeeroyHannigan
LeeroyHannigan deleted the fix/1094-maintenance-claim-unlock branch November 23, 2026 14:41
LeeroyHannigan added a commit that referenced this pull request Nov 24, 2026
This merge brings in 13 commits of main through 44cd436. One of them,
#1194, changes two files of this branch: it adds ClaimSession::unlock to
crates/storage-postgres/src/backup_engine.rs, and it adds the
migrations_support module and a test to
crates/storage-postgres/tests/backup_restore.rs. Both files merge without a
conflict.
@robinnsc robinnsc mentioned this pull request Nov 25, 2026
6 of 8 tasks
robinnsc added a commit that referenced this pull request Nov 26, 2026
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that
carries it: the root Cargo.toml version and its 13 exact pins, the 15
workspace entries in Cargo.lock (no third-party entry changes),
packaging/npm/package.json (the version and the five platform pins),
the four license-notice files (regenerated with cargo-about 0.9.0), and
the sample `extenddb version` and `serve` output in
docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md.

This is a minor release. Since v1.0.0, main has two features (#468,
#1009) and changes that make ExtendDB answer as DynamoDB does, which
docs/versioning.md ships in a minor release. It adds no catalog
migration (RELEASED_THROUGH stays 12, the last id), no configuration
key and no backup format version.

Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468
(AT_TIMESTAMP, and the GetShardIterator answers it changes), #503,
#544, #567, #765 and #774, #588, #596, #690, #1144, #1173 and #1194,
next to the existing entries for #398 and #526. The #526 entry now
gives the old TransactWriteItems mismatch message, and the entries are
in PR order. Current Status, the General Upgrade Procedure and the
Rollback Procedure cover 1.0.0 to 1.1.0: no migrate step, and a
rollback runs the 1.0.0 binary on the same catalog, with the two
limits the manual names (#526 tokens, and 1.0.0's catalog-check
--fix). The rollback note of the KEK-file switch says that 1.0.0
serves a switched catalog.

docs/manuals/11-deployment-guide.md: a sentence about the release
before the one that carries #398 is gone.

Signed-off-by: Scott Robinson <robinnsc@amazon.com>
robinnsc added a commit that referenced this pull request Nov 26, 2026
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that
carries it: the root Cargo.toml version and its 13 exact pins, the 15
workspace entries in Cargo.lock (no third-party entry changes),
packaging/npm/package.json (the version and the five platform pins),
the four license-notice files (regenerated with cargo-about 0.9.0), and
the sample `extenddb version` and `serve` output in
docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md.

This is a minor release. Since v1.0.0, main has two features (#468,
#1009) and changes that make ExtendDB answer as DynamoDB does, which
docs/versioning.md ships in a minor release. It adds no catalog
migration (RELEASED_THROUGH stays 12, the last id), no configuration
key and no backup format version.

Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468
(AT_TIMESTAMP, and the GetShardIterator answers it changes), #486,
#503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173,
#1194 and #1375, next to the existing entries for #398, #526 and
#1166. The #526 entry now gives the old TransactWriteItems mismatch
message, and the entries are in PR order. Current Status, the General
Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no
migrate step, the #1166 checks before a PostgreSQL upgrade, and a
rollback runs the 1.0.0 binary on the same catalog, with the two
limits the manual names (#526 tokens, and 1.0.0's catalog-check
--fix). The rollback note of the KEK-file switch says that 1.0.0
serves a switched catalog.

docs/manuals/11-deployment-guide.md: a sentence about the release
before the one that carries #398 is gone.
robinnsc added a commit that referenced this pull request Nov 27, 2026
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that
carries it: the root Cargo.toml version and its 13 exact pins, the 15
workspace entries in Cargo.lock (no third-party entry changes),
packaging/npm/package.json (the version and the five platform pins),
the four license-notice files (regenerated with cargo-about 0.9.0), and
the sample `extenddb version` and `serve` output in
docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md.

This is a minor release. Since v1.0.0, main has three features (#468,
#1009, #1449) and changes that make ExtendDB answer as DynamoDB does, which
docs/versioning.md ships in a minor release. It adds no catalog
migration (RELEASED_THROUGH stays 12, the last id), no configuration
key and no backup format version.

Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468
(AT_TIMESTAMP, and the GetShardIterator answers it changes), #486,
#503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173,
#1194, #1375 and #1522, next to the existing entries for #398, #526,
#1166, #1449 and #1523. The #526 entry now gives the old
TransactWriteItems mismatch message, the #1523 heading takes the
v1.1.0: prefix, and the entries are in PR order. Current Status, the General
Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no
migrate step, the #1166 checks before a PostgreSQL upgrade, and a
rollback runs the 1.0.0 binary on the same catalog, with the two
limits the manual names (#526 tokens, and 1.0.0's catalog-check
--fix). The rollback note of the KEK-file switch says that 1.0.0
serves a switched catalog.

docs/manuals/11-deployment-guide.md: a sentence about the release
before the one that carries #398 is gone.
robinnsc added a commit that referenced this pull request Nov 27, 2026
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that
carries it: the root Cargo.toml version and its 13 exact pins, the 15
workspace entries in Cargo.lock (no third-party entry changes),
packaging/npm/package.json (the version and the five platform pins),
the four license-notice files (regenerated with cargo-about 0.9.0), and
the sample `extenddb version` and `serve` output in
docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md.

This is a minor release. Since v1.0.0, main has three features (#468,
#1009, #1449) and changes that make ExtendDB answer as DynamoDB does, which
docs/versioning.md ships in a minor release. It adds no catalog
migration (RELEASED_THROUGH stays 12, the last id), no configuration
key and no backup format version.

Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468
(AT_TIMESTAMP, and the GetShardIterator answers it changes), #486,
#503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173,
#1194, #1375 and #1522, next to the existing entries for #398, #526,
#1166, #1449 and #1523. The #526 entry now gives the old
TransactWriteItems mismatch message, the #1523 heading takes the
v1.1.0: prefix, and the entries are in PR order. Current Status, the General
Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no
migrate step, the #1166 checks before a PostgreSQL upgrade, and a
rollback runs the 1.0.0 binary on the same catalog, with the two
limits the manual names (#526 tokens, and 1.0.0's catalog-check
--fix). The rollback note of the KEK-file switch says that 1.0.0
serves a switched catalog.

docs/manuals/11-deployment-guide.md: a sentence about the release
before the one that carries #398 is gone.

docs/troubleshooting.md: the bus error entry names 1.0.0 as the release
that has the bug, so its line carries a version-literal-ok marker.
LeeroyHannigan added a commit that referenced this pull request Nov 27, 2026
* chore(release): bump version to 1.1.0

Moves the workspace version from 1.0.0 to 1.1.0 on every surface that
carries it: the root Cargo.toml version and its 13 exact pins, the 15
workspace entries in Cargo.lock (no third-party entry changes),
packaging/npm/package.json (the version and the five platform pins),
the four license-notice files (regenerated with cargo-about 0.9.0), and
the sample `extenddb version` and `serve` output in
docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md.

This is a minor release. Since v1.0.0, main has three features (#468,
#1009, #1449) and changes that make ExtendDB answer as DynamoDB does, which
docs/versioning.md ships in a minor release. It adds no catalog
migration (RELEASED_THROUGH stays 12, the last id), no configuration
key and no backup format version.

Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468
(AT_TIMESTAMP, and the GetShardIterator answers it changes), #486,
#503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173,
#1194, #1375 and #1522, next to the existing entries for #398, #526,
#1166, #1449 and #1523. The #526 entry now gives the old
TransactWriteItems mismatch message, the #1523 heading takes the
v1.1.0: prefix, and the entries are in PR order. Current Status, the General
Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no
migrate step, the #1166 checks before a PostgreSQL upgrade, and a
rollback runs the 1.0.0 binary on the same catalog, with the two
limits the manual names (#526 tokens, and 1.0.0's catalog-check
--fix). The rollback note of the KEK-file switch says that 1.0.0
serves a switched catalog.

docs/manuals/11-deployment-guide.md: a sentence about the release
before the one that carries #398 is gone.

docs/troubleshooting.md: the bus error entry names 1.0.0 as the release
that has the bug, so its line carries a version-literal-ok marker.

* docs(upgrade): add the v1.1.0 entry of #1290, put #1349's in order

#1290 merged after this branch was rebased. It changes the answer to a
request with no Authorization header, so it gets a v1.1.0 entry. The
merge of main put the #1349 entry after the #1523 entry. It moves to
the place of its number, beside the new #1290 entry.

---------

Co-authored-by: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: the_maintenance_claim_excludes_a_second_holder_and_expires_when_stale sees a refused take's lock as a live claim

2 participants