What this thread does
Do first: inspect pg_blocking_pids(pid) alongside pg_stat_activity while the wait exists, before changing transaction scope or isolation.
Short answer
As of November 2026, this card uses PostgreSQL 18. Shorten time holding locks, while keeping every read/check/write that defines one business invariant inside the same transaction. An isolation downgrade does not repair a long critical section.
Choose / don't choose
read committed: choose when statement snapshots plus explicit row locks/atomic updates protect the complete invariant; don't use it for a cross-row rule that requires a stable transaction-wide view.repeatable read: choose for a stable snapshot; don't assume it prevents every serialization anomaly.serializable: choose when concurrent results must correspond to a serial execution; don't choose it without whole-transaction retry handling (isolation).
Not for: query plan tuning; see agent-tunes-slow-postgres-query-explain-hypopg.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
transaction_isolation | Preserve the level required by the invariant: read committed, repeatable read, or serializable; Read Committed is the default | Current transaction; select before data access | isolation, SET TRANSACTION |
lock_timeout | Sample setting 0 disables; choose below the total statement budget | Transaction-local wait policy | operating report, timeouts |
deadlock_timeout | Default '1s'; delay before deadlock detection, not a maximum lock wait | Privileged server/session setting | lock management |
SELECT ... FOR UPDATE | Lock selected rows until transaction end; use only when the invariant requires it | Explicit row locking | locking |
Order of operations
1. From an authorized monitoring connection inspect:
SELECT pid, state, xact_start, wait_event_type, wait_event,
pg_blocking_pids(pid) AS blockers
FROM pg_stat_activity
WHERE datname = current_database();
The function returns blocking PIDs, including lock-queue blockers, or an empty array. Resolve those PIDs to sessions and inspect transaction age/state; avoid polling it at high frequency (function, activity). 2. Find the earliest transaction retaining the lock, rather than optimizing only the waiting statement. Marco Slot's February 22, 2018 report describes queued locks delaying otherwise ordinary traffic. 3. Perform independent remote calls and preparation before BEGIN. Inside the transaction, lock objects in a consistent order, re-read/revalidate current state, then make all dependent changes and commit promptly. External side effects need idempotency or an outbox design, not blind transaction replay. 4. Locks normally last to transaction end; rollback to a savepoint releases locks acquired since that savepoint (locking). 5. On 40P01 or a retryable 40001, roll back and repeat the complete transaction, including reads and decisions that generated the SQL. Apply bounded attempts/backoff within the request deadline. Never continue from the single failed statement with stale decisions (retry guidance).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
40P01 deadlock_detected | Cyclic lock dependencies | Roll back/retry the whole unit; use consistent object lock order | retry, locking |
40001 serialization_failure | Concurrent-update conflicts at Repeatable Read; serialization conflicts at Serializable | Retry the entire unit using fresh reads; don't lower isolation to silence it | retry |
Use SQLSTATE rather than localized message text (error codes). Session termination is not a first diagnostic; coordinate with the transaction owner.
Missing
- No business invariant, transaction trace or latency target is given; a universally correct isolation level or wait budget cannot be selected.
- A bounded retry policy must account for external side effects and client cancellation.
Started from a post by @Marco Slot, packaged by the editor. Original post