Goal: trace a live lock dependency and choose the right way to release it.
Start a labelled client holding ACCESS EXCLUSIVE, which conflicts even with readers, then sleeping for up to an hour so inspection is not a 45-second race. The final steps cancel it. PGAPPNAME labels this connection; & backgrounds the shell job, $! saves its shell PID for wait, and > file 2>&1 redirects output/errors. A shell PID is not the database backend PID. The bounded poll waits for the granted lock, then the query proves readiness. These named jobs belong only to this lab.
All hints run at the Linux shell. psql -c executes quoted SQL and exits; each command uses a new connection. -U postgres explicitly selects the administrative SQL role, and -v ON_ERROR_STOP=1 stops on SQL errors. The monitoring tables are separate provided fixtures, not the baked business datasets.
Goal: create a second connection that cannot read until the lock is released.
Background a COUNT(*) with a different application name and save its shell job PID. Wait until the reader is blocked. Expect active, Lock, and has_blocker=true: active means a statement is in progress, not that it is consuming CPU.
Goal: distinguish an active sleeper from an active lock waiter.
Inspect only the named clients. Expect the holder at Timeout/PgSleep and the reader at Lock/relation. query_start measures elapsed wall time including waits; it is not CPU time. Backend PIDs and elapsed seconds vary. pg_backend_pid() identifies the inspecting connection, which is excluded here by application name.
Goal: prove which named session blocks the reader, without copying a PID by hand.
pg_blocking_pids() returns immediate blockers, including conflicting holders and earlier conflicting waiters. unnest makes one row per returned PID so it can be joined to activity. Expect one edge from lab24_waiter to lab24_blocker. This two-session experiment is not a recursive traversal of every ancestor in a longer chain.
Goal: interrupt the sleeping statement and wait for this one-shot client to exit.
Target the labelled holder in this database, not every session containing similar SQL. cancel_sent=true confirms the signal was sent; it does not itself prove lock release. wait uses the saved shell PID and a nonzero exit is expected after cancellation. Because this client exits on the SQL error, its connection closes and transaction rolls back. A different interactive client could remain connected after a cancellation and needs separate transaction handling.
Goal: prove the waiting reader finished because the holder was cancelled.
Wait for the reader to finish successfully, then inspect its 5,000-row count and the holder’s specific cancellation error. The holder’s error is intentional; unrelated errors are not success. Client log files are separate from PostgreSQL’s server log.
Goal: hold the same lock without an executing statement.
Bash coproc starts a background client with a pipe kept open for more input; unlike a short sleep, this leaves time to read the instructions. ${LAB_IDLE[1]} is its input descriptor and $LAB_IDLE_PID its shell job PID. printf sends BEGIN and LOCK, but no commit. Save both values because Bash may unset the coprocess variables when it exits. Wait for idle in transaction and the granted lock: the backend is now waiting for client input while retaining its transaction lock.
Goal: show that an idle transaction needs its connection ended to release the lock.
First send cancel and verify the idle session still holds the lock: there is no executing query to interrupt. Then terminate only lab24_idle, wait for its client to exit, and close the saved pipe descriptor. Expect no named lab sessions or exclusive locks, and a normal 5,000-row read. A sent signal is not a substitute for inspecting the resulting state.
The idle psql client is still waiting for input even after its backend ends. Send one final query so it notices the closed connection, close the pipe, then wait for the failed client. Closing the pipe before waiting avoids hanging on an input reader.