Repository navigation
3.x.x: tell statement timeouts from cancels by SQLSTATE and time, not PostgreSQL's English message (#824) - #852
Merged
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
… PostgreSQL's English message (#824) The pre-migrate, the share-hash backfill and `fatal-state clear` now tell a statement timeout from an operator's cancel by SQLSTATE and elapsed time. They no longer depend on PostgreSQL's English message. Refs #824. ## The problem - `statement_timed_out` (`ledger/migration.rs`) required SQLSTATE 57014 **and** a message containing "statement timeout". `fatal_state.rs` had its own copy of the same check. - PostgreSQL translates that message under `lc_messages`, and nothing in the server pins it to English. - **The pre-migrate's retry (#791, #821):** - Each attempt set `lock_timeout` and `statement_timeout` both to 100 ms. The statement timer starts first, so a blocked `LOCK TABLE` nearly always ended with 57014. - On a non-English server, `gave_up` treated that 57014 as fatal. - `migrate`, or a start with `PRISM_POSTGRES_INIT_SCHEMA=1`, then failed at its first brief contention instead of retrying within the 600 s budget. - **Other paths that lost their timeout handling on such a server:** - the share-hash backfill's batch and double-credit halving; - its record-of-2 retry; - `fatal-state clear`'s "raise --timeout-seconds" advice. ## The change - **Pre-migrate (`table_locks.rs`):** - The up-front requests now run under `lock_timeout` = `LOCK_WAIT` (80 ms) and `statement_timeout` = `ATTEMPT` (100 ms). - A request blocked on one table, the usual contention, ends with 55P03 (`lock_not_available`), which is unambiguous. The statement gets a 20 ms margin before its first wait. - These are unchanged: - the documented 100 ms bound on the primary (the statement timeout is still the total); - #821's `DEADLOCK_CHECK` (50 ms, still within any one table's wait); - the backoff; - `BUDGET`. - `LockContention` now carries the SQLSTATE, and the retry warning logs it as `sqlstate=`. - The refusal now says each attempt "waited up to 100 ms for <tables>, 80 ms for any one". - The module docs and the `BUDGET` reasoning (about 290 attempts in 600 s) are updated. - **Everywhere else: `timed(timeout, statement)` in `ledger/migration.rs`.** - It runs one statement under the `statement_timeout` the caller set for it, and times it on this side's clock. - It marks the error `StatementTimedOut` only for a 57014 that arrived after at least nine tenths of that timeout. - Why a timeout qualifies: PostgreSQL starts its timer after the client starts its clock, and the error arrives after the timer fires. - Why the tenth of slack: PostgreSQL times the statement by the server's wall clock, which can run fast or step forward against the client's monotonic clock. Taking a timeout for a cancel would stop the run, the failure #824 is about; taking a cancel for a timeout only retries the statement. - Any 57014 sooner is a cancel request. sqlx 0.8 never sends one, so it is an operator's, and it stops the run as before. - `timed_out(&anyhow::Error)` reads the mark through any added context. - The only ambiguity: a cancel that lands in the last tenth of the timeout, or within the client's own overhead before it, is taken for the timeout and retried, as the timeout would have been. - **Callers converted:** - `gave_up`; - the backfill's batches and double-credit chunks, plus `smallest_timed_out`; - every statement of the record of 2, each timed on its own, with `waited_too_long`; - `integrity_report_bounded`, plus the `fatal-state clear` advice. - The unthrottled backfill batches run under the session's timeout, which `map_batches` reads once from `pg_settings`. - **Why not `lc_messages = 'C'`?** It is superuser-only (or needs `SET` on the parameter from PostgreSQL 15), so production migrate roles would need a fallback anyway. It also changes the session's server-log language. Timing works for every role. - **Docs:** - `docs/prism-rust-migration.md` (pre-migrate section): the 80/100 ms bounds, the SQLSTATE classification and the pre-3.1.2 behaviour; - `docs/prism-ledger-ops.md`: the backfill throttle and the fatal-state report timeout. ## Tests On minidev-alex1, against a private PostgreSQL 16 with fsync on: - **Unit tests (German messages from PostgreSQL 16's own `de` catalog):** - `ledger::migration::tests::a_statement_timeout_is_told_by_its_sqlstate_and_its_time_not_its_message` - `ledger::migration::tests::timed_marks_a_statement_its_timeout_cancelled_and_nothing_else` - `table_locks::tests::an_attempt_gives_up_by_sqlstate_and_time_whatever_the_language` - `share_hashes::tests::a_record_attempt_waited_too_long_by_its_sqlstate_and_time` - **New gated tests** (all four added to `test/prism-gated-tests.txt`): - `table_locks::postgres_tests::a_request_blocked_on_one_table_ends_with_the_lock_timeout_and_is_tried_again`: a held read makes the request end with 55P03 after at least `LOCK_WAIT`. `committed_within` warns `sqlstate=55P03` and never `57014`, then applies 27 and 28 once the read ends. - `table_locks::postgres_tests::on_a_server_whose_messages_are_german_a_timeout_is_still_told_from_a_cancel`: - Setup: a private cluster started with `LANGUAGE=de`, and `lc_messages` set to a host locale other than C or POSIX. The test checks that the server really answers in German. - Through `timed`, a real statement timeout is marked and a real `pg_cancel_backend` is not. - Through `lock_within_attempt`, the statement timeout (57014) and a lock timeout (55P03) give the attempt up, and the cancel does not. - The cancel is the statement's own `pg_cancel_backend(pg_backend_pid())`, which makes the test deterministic. - The German cluster needs a non-C locale on the host (`en_US.UTF-8` here) and PostgreSQL's German catalog (`postgres-16.mo`, shipped with `postgresql-16`). The test fails, naming which is missing, rather than pass without translation. - `share_hashes::postgres_tests::an_unthrottled_batch_past_the_sessions_statement_timeout_is_halved_and_a_cancel_stops_it`: - Runs on a minimal schema, so it migrates nothing and never takes the migration lock. - The unthrottled `map_batches` reads its 300 ms session timeout from the server and halves twice behind a slow trigger. - A batch that cancels its own backend stops the run, unmarked. - `share_hashes::postgres_tests::a_record_attempt_cut_by_its_statement_timeout_is_tried_again`: - An open read of the cursor holds the record's `DROP TABLE`. - The throttle's 300 ms statement timeout cuts each attempt, and the attempt is retried (the warning carries "cancelled by its 300 ms statement timeout"). - Once the read ends, 2 is recorded. - **The gated tests on a loaded host:** - A request whose backend is held up for the 20 ms margin before its wait gives up with 57014 instead of 55P03, which is still contention. - So `blocked_until_the_lock_timeout` requests again, up to five times, until it sees the 55P03. - In the German test, the self-cancelling statement is prepared first, so the measured request is a single round trip. - **The existing `deadlock_is_contention`** now sets its unlowered deadlock check to 60 ms. That is as far below `LOCK_WAIT` as #791's 80 ms check was below its 100 ms timeouts. The test asserts SQLSTATE 40P01 rather than the English message. Commands, every one through the host gate with `CARGO_PROFILE_DEV_DEBUG=line-tables-only`: - `cargo test --locked -p qbit-prism-server --lib -- --test-threads=4 ledger::migration::tests::a_statement_timeout ledger::migration::tests::timed_marks table_locks share_hashes fatal_state api::integrity` with `PRISM_TEST_DATABASE_URL`, `PRISM_TEST_PG_BIN_DIR` and `PRISM_TEST_REQUIRE_INTEGRATION=1`, on the tree merged with `3.x.x` (#833, #834): **56 passed**. The gate manifest shows all four new ids `executed`. - `cargo test --locked -p qbit-prism-server --test ledger_postgres --test api_database -- --test-threads=4 <the tests below>`, on the merged tree: - `a_batch_past_the_throttles_statement_timeout_is_retried_at_half_its_size` - `the_double_credit_check_runs_under_the_throttles_statement_timeout` - `backfill_share_hashes_runs_each_batch_under_its_statement_timeout_and_rests_between_batches` - `the_record_of_2_waits_out_a_reader_of_the_cursor_in_short_attempts` - `the_record_of_2_stops_cleanly_behind_a_held_migration_lock_and_records_once_released` - `clear_report_statement_ends_within_the_bound` - `migration_027_backs_off_instead_of_closing_a_lock_cycle_with_a_serving_frontend` - `clear_and_self_check_run_the_report_under_their_own_timeout` - #833's `api_database::integrity_routes_run_one_report_at_a_time_under_their_own_timeout`, which asserts `cancelled (canceling statement due to statement timeout)`. It still finds the SQLSTATE through the new `StatementTimedOut` context. - All passed except `clear_report_statement_ends_within_the_bound`. It failed only because it ran beside `clear_and_self_check_run_the_report_under_their_own_timeout`: - its last check counts every active `qbit_carry_forward_integrity_report` query in the whole database, and caught the other test's report; - alone, and with the two run serially, both pass; - both tests predate this change. - `cargo fmt --all -- --check`: clean. - `cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings`: clean, apart from the 3 known vendored sqlx-core warnings. - `python3 scripts/check_prism_pool_acquires.py` and `python3 scripts/check_gate_env_reads.py`: clean. An observation from the local runs: - An earlier version of the German test migrated a full ledger on its own cluster. Run beside the other schema-migrating tests on a loaded host (load average about 12), it slowed the shared database's migrations. - The result was two failures, neither seen when the test runs alone or on base: - `share_hashes::postgres_tests::a_record_whose_commit_reply_never_comes_is_read_afresh_at_its_deadline` hit its 100 s bound under the record's patient backoff; - `a_cursor_dropped_after_its_look_up_reads_as_nothing_pending` hit its 5 s lock timeout on the migration lock. - The German test now locks a table of its own instead of migrating a ledger. The same set then passes in 33 s, the same as base. - Even so, `a_record_whose_commit_reply_never_comes_is_read_afresh_at_its_deadline` varies between 30 and 83 s, within its 100 s bound, in this concentrated set at load average 7 to 10: - its record attempts' 300 ms timeout backs off up to 30 s whenever another test's fresh-schema migration holds the migration lock; - it was 83 s with this PR's record test skipped, so that test is not the cause. - This is pre-existing; the test's bound already allows a minute for it. ## Review Two local `/code-review xhigh` rounds (Opus). Most of what they found is fixed above. These are deliberate: - **Timing per call site:** `timed` wraps each statement where it runs, rather than in an executor wrapper. - `record_transaction`'s `BEGIN`/`set_config` and the double-credit check's first read are not timed. - Neither can meet the throttle's timeout, and that first read's failure stops the run, as before. - **The record transaction's timeouts stay equal:** its default statement timeout (2 s) still equals `RECORD_LOCK_TIMEOUT` (2 s), so a blocked record attempt usually ends with a timed 57014 rather than 55P03. - Its lock bounds are documented and feed the record's deadlines. - The tenth of slack covers the clock, and a gated test covers the retry. - **The German test fails rather than skips** when the host has no locale but C/POSIX or no German PostgreSQL catalog, so it can never pass without translation. ## What CI must cover - The full lib suite. - `ledger_postgres`. - The gated-test manifest proof, for the two new ids. - The dual-sim `r01-rc6-premigrate` scenario, which runs the pre-migrate with its new 80 ms lock wait beside rc.6 frontends. ## Not changed here - `ledger/window.rs` `refused_for_want_of_a_partition` also reads PostgreSQL's English message: 23514 plus "no partition of relation". That is the same class of problem and is outside this change's paths. - `api.rs` and `api/public_service.rs` classify 57014 by SQLSTATE alone, which this change does not touch. Refs #824 🤖 Generated with [Claude Code](https://claude.com/claude-code) Squashed commits: 6da4065 3.x.x: tell statement timeouts from cancels by SQLSTATE and time, not PostgreSQL's English message (#824) 9f3e558 3.x.x: review fixes for the #824 timeout classification dac583d 3.x.x: second review round for the #824 timeout classification f79ccb8 Merge remote-tracking branch 'origin/3.x.x' into orca/824-timeout-classification-lc-messages a8d40c3 3.x.x: the share-hash throttle's #738 comments name frontends before 3.1.1 Co-Authored-By: Claude Opus 5.5 <[email protected]>
kiwidream
force-pushed
the
orca/824-timeout-classification-lc-messages
branch
from
November 12, 2026 21:43
a8d40c3 to
42a9030
Compare
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The pre-migrate, the share-hash backfill and
fatal-state clearnow tell a statement timeout from an operator's cancel by SQLSTATE and elapsed time. They no longer depend on PostgreSQL's English message. Refs #824.The problem
statement_timed_out(ledger/migration.rs) required SQLSTATE 57014 and a message containing "statement timeout".fatal_state.rshad its own copy of the same check.lc_messages, and nothing in the server pins it to English.lock_timeoutandstatement_timeoutboth to 100 ms. The statement timer starts first, so a blockedLOCK TABLEnearly always ended with 57014.gave_uptreated that 57014 as fatal.migrate, or a start withPRISM_POSTGRES_INIT_SCHEMA=1, then failed at its first brief contention instead of retrying within the 600 s budget.fatal-state clear's "raise --timeout-seconds" advice.The change
table_locks.rs):lock_timeout=LOCK_WAIT(80 ms) andstatement_timeout=ATTEMPT(100 ms).lock_not_available), which is unambiguous. The statement gets a 20 ms margin before its first wait.DEADLOCK_CHECK(50 ms, still within any one table's wait);BUDGET.LockContentionnow carries the SQLSTATE, and the retry warning logs it assqlstate=.BUDGETreasoning (about 290 attempts in 600 s) are updated.timed(timeout, statement)inledger/migration.rs.statement_timeoutthe caller set for it, and times it on this side's clock.StatementTimedOutonly for a 57014 that arrived after at least nine tenths of that timeout.timed_out(&anyhow::Error)reads the mark through any added context.gave_up;smallest_timed_out;waited_too_long;integrity_report_bounded, plus thefatal-state clearadvice.map_batchesreads once frompg_settings.lc_messages = 'C'? It is superuser-only (or needsSETon the parameter from PostgreSQL 15), so production migrate roles would need a fallback anyway. It also changes the session's server-log language. Timing works for every role.docs/prism-rust-migration.md(pre-migrate section): the 80/100 ms bounds, the SQLSTATE classification and the pre-3.1.2 behaviour;docs/prism-ledger-ops.md: the backfill throttle and the fatal-state report timeout.Tests
On minidev-alex1, against a private PostgreSQL 16 with fsync on:
decatalog):ledger::migration::tests::a_statement_timeout_is_told_by_its_sqlstate_and_its_time_not_its_messageledger::migration::tests::timed_marks_a_statement_its_timeout_cancelled_and_nothing_elsetable_locks::tests::an_attempt_gives_up_by_sqlstate_and_time_whatever_the_languageshare_hashes::tests::a_record_attempt_waited_too_long_by_its_sqlstate_and_timetest/prism-gated-tests.txt):table_locks::postgres_tests::a_request_blocked_on_one_table_ends_with_the_lock_timeout_and_is_tried_again: a held read makes the request end with 55P03 after at leastLOCK_WAIT.committed_withinwarnssqlstate=55P03and never57014, then applies 27 and 28 once the read ends.table_locks::postgres_tests::on_a_server_whose_messages_are_german_a_timeout_is_still_told_from_a_cancel:LANGUAGE=de, andlc_messagesset to a host locale other than C or POSIX. The test checks that the server really answers in German.timed, a real statement timeout is marked and a realpg_cancel_backendis not.lock_within_attempt, the statement timeout (57014) and a lock timeout (55P03) give the attempt up, and the cancel does not.pg_cancel_backend(pg_backend_pid()), which makes the test deterministic.en_US.UTF-8here) and PostgreSQL's German catalog (postgres-16.mo, shipped withpostgresql-16). The test fails, naming which is missing, rather than pass without translation.share_hashes::postgres_tests::an_unthrottled_batch_past_the_sessions_statement_timeout_is_halved_and_a_cancel_stops_it:map_batchesreads its 300 ms session timeout from the server and halves twice behind a slow trigger.share_hashes::postgres_tests::a_record_attempt_cut_by_its_statement_timeout_is_tried_again:DROP TABLE.blocked_until_the_lock_timeoutrequests again, up to five times, until it sees the 55P03.deadlock_is_contentionnow sets its unlowered deadlock check to 60 ms. That is as far belowLOCK_WAITas 3.x.x: lock 027's and 028's tables at once so the pre-migrate cannot deadlock (#789) #791's 80 ms check was below its 100 ms timeouts. The test asserts SQLSTATE 40P01 rather than the English message.Commands, every one through the host gate with
CARGO_PROFILE_DEV_DEBUG=line-tables-only:cargo test --locked -p qbit-prism-server --lib -- --test-threads=4 ledger::migration::tests::a_statement_timeout ledger::migration::tests::timed_marks table_locks share_hashes fatal_state api::integritywithPRISM_TEST_DATABASE_URL,PRISM_TEST_PG_BIN_DIRandPRISM_TEST_REQUIRE_INTEGRATION=1, on the tree merged with3.x.x(3.x.x: the /audit integrity routes run one report at a time under their own timeout and serve a dated result; fatal-state clear asks the node once per height (#737, #690) #833, 3.x.x: a submitblock hyper hands back unwritten counts as not sent and is offered again, and the payout-revision listener reports its health (#816) #834): 56 passed. The gate manifest shows all four new idsexecuted.cargo test --locked -p qbit-prism-server --test ledger_postgres --test api_database -- --test-threads=4 <the tests below>, on the merged tree:a_batch_past_the_throttles_statement_timeout_is_retried_at_half_its_sizethe_double_credit_check_runs_under_the_throttles_statement_timeoutbackfill_share_hashes_runs_each_batch_under_its_statement_timeout_and_rests_between_batchesthe_record_of_2_waits_out_a_reader_of_the_cursor_in_short_attemptsthe_record_of_2_stops_cleanly_behind_a_held_migration_lock_and_records_once_releasedclear_report_statement_ends_within_the_boundmigration_027_backs_off_instead_of_closing_a_lock_cycle_with_a_serving_frontendclear_and_self_check_run_the_report_under_their_own_timeoutapi_database::integrity_routes_run_one_report_at_a_time_under_their_own_timeout, which assertscancelled (canceling statement due to statement timeout). It still finds the SQLSTATE through the newStatementTimedOutcontext.clear_report_statement_ends_within_the_bound. It failed only because it ran besideclear_and_self_check_run_the_report_under_their_own_timeout:qbit_carry_forward_integrity_reportquery in the whole database, and caught the other test's report;cargo fmt --all -- --check: clean.cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings: clean, apart from the 3 known vendored sqlx-core warnings.python3 scripts/check_prism_pool_acquires.pyandpython3 scripts/check_gate_env_reads.py: clean.An observation from the local runs:
share_hashes::postgres_tests::a_record_whose_commit_reply_never_comes_is_read_afresh_at_its_deadlinehit its 100 s bound under the record's patient backoff;a_cursor_dropped_after_its_look_up_reads_as_nothing_pendinghit its 5 s lock timeout on the migration lock.a_record_whose_commit_reply_never_comes_is_read_afresh_at_its_deadlinevaries between 30 and 83 s, within its 100 s bound, in this concentrated set at load average 7 to 10:Review
Two local
/code-review xhighrounds (Opus). Most of what they found is fixed above. These are deliberate:timedwraps each statement where it runs, rather than in an executor wrapper.record_transaction'sBEGIN/set_configand the double-credit check's first read are not timed.RECORD_LOCK_TIMEOUT(2 s), so a blocked record attempt usually ends with a timed 57014 rather than 55P03.What CI must cover
ledger_postgres.r01-rc6-premigratescenario, which runs the pre-migrate with its new 80 ms lock wait beside rc.6 frontends.Not changed here
ledger/window.rsrefused_for_want_of_a_partitionalso reads PostgreSQL's English message: 23514 plus "no partition of relation". That is the same class of problem and is outside this change's paths.api.rsandapi/public_service.rsclassify 57014 by SQLSTATE alone, which this change does not touch.Refs #824
🤖 Generated with Claude Code
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.