Supabase and PostgresDatabase and Postgresreference
Postgres lock queue behind ACCESS EXCLUSIVE, lock_timeout and PgBouncer
An ALTER TABLE needs an ACCESS EXCLUSIVE lock, which conflicts with every other lock mode, including the ACCESS SHARE that a SELECT takes.1 The migration therefore waits for every reader that is already running, and Postgres puts the waiting request in the lock’s queue, where requests that conflict with it are granted in order of arrival.2 This page measures what that queue does to the readers behind it, to PostgREST and to a transaction-mode PgBouncer, what lock_timeout bounds when a migration has several statements, whether the stack recovers by itself afterwards, and which migration shape keeps the damage small.
TL;DR
- A reader holding ACCESS SHARE blocked nobody: in the 5 s blocker-only window every request answered 200 in all three
hold20repetitions. Once theALTERwas waiting behind it, every later reader on both paths queued, with 0 answers per second ended inside the 20 s wait (LQ02). - Both PgBouncer pools of 10 filled within a second:
cl_waiting10 andsv_active10 for both login roles,maxwaitup to 19 s, and 21 Postgres backends waiting for the lock (theALTERplus 10 per role). PostgREST answered 504PGRST003after its 10 s pool acquisition timeout, 181 to 182 times in the 20 s wait; the first 504 came 11 to 11.1 s after theALTERstarted (LQ02). - Everything answered again within about a second of the commit: requests sent in the second the
ALTERcommitted had a p95 of 295 ms (PostgREST) and 5 ms (the role path), and none of the 600 PostgREST and 300 role requests sent after the end failed (LQ02). lock_timeoutT limits each statement’s wait for its lock, not the migration. In a transaction of KALTERstatements on K tables, the first table’s lock is held to the end, so readers of the first table were blocked about K x T: 5,100 ms at K = 3, T = 2,000 ms, with waits staged at 0.85 T and no timeout firing (LQ01).- A 250 ms
lock_timeoutwith a retry loop committed about when one shot did (5, 3 and 1 s against 5, 2.5 and 1 s) and held the readers’ longest latency to 222, 212 and 217 ms on PostgREST against 4,963, 2,480 and 960 ms for the one shot (LQ04). - No wedge reproduced: every arm recovered with no restart. The longest lingering failure was PostgREST’s 503
PGRST002for 12.3 to 12.7 s after the lock cleared, with PgBouncer’squery_timeoutat 5 s and Postgres atmax_connections40 (LQ03). SET lock_timeoutoutside a transaction through a transaction-mode pool with one server connection reached the next client (3 of 3 read1234ms);SET LOCALdid not (3 of 3 read0) (LQ04).
The same chain as text:
- A long reader holds ACCESS SHARE and blocks nobody.
- The
ALTERasks for ACCESS EXCLUSIVE and waits for the reader. - Every later reader conflicts with the waiting request and queues behind it.
- The PgBouncer pools fill with queued readers, 10 per login role.
- PostgREST waits 10 s for a pool connection, then answers 504
PGRST003. - When the reader ends the
ALTERcommits and the queue drains in about a second.
Which do I pick
Section titled “Which do I pick”| Situation | Choice | Why |
|---|---|---|
| A single-statement migration on a table that is read all the time | SET LOCAL lock_timeout of a few hundred milliseconds in a retry loop | 250 ms with retries held the longest reader stall to about the timeout; one shot stalled readers for the whole wait (LQ04) |
| A migration of K statements in one transaction | Size the blocking as K x T, or split into one transaction per statement | Readers of the first table waited 5,100 ms at K = 3, T = 2 s with no timeout firing (LQ01) |
| You cannot bound the wait | Put a statement_timeout on the reader roles to bound how long one reader holds a connection | It cut each reader after 3 s but the pools still saturated and PostgREST still returned PGRST003 (LQ02) |
SET of a session setting through a transaction pooler | SET LOCAL inside the transaction | SET reached the next client on the same server connection 3 of 3 times (LQ04) |
| The pooler seems stuck after a lock cleared | Wait about 13 s before restarting | Every arm recovered with no restart; the slowest took 12.3 to 12.7 s (LQ03) |
What lock_timeout bounds (LQ01)
Section titled “What lock_timeout bounds (LQ01)”A migration of K ALTER TABLE ... ADD COLUMN statements in one transaction, each on its own table, with lock_timeout T. Per cell: Postgres 17.11 alone with no pooler and no load, K tables, K blockers (each an open transaction that read its table, so it holds ACCESS SHARE), K probe sessions (one per table, select count(*) every 20 ms), and the migration on its own connection. The stagger pattern releases blocker i at 0.85 x T x i after the migration starts, so statement i waits about 0.85 T and no timeout can fire; last does the same for blockers 1 to K-1 and never releases blocker K, so statement K times out. The 0.85 factor is this module’s choice, a margin of 15% of T against jitter. “Longest reader block” is the largest probe latency in the repetition, a lower bound when the probe loop itself stalled. Selected rows (the artifact has all 30 cells), three repetitions each:
| Variant | Pattern | K | T ms | Outcome | Migration wall ms | Longest reader block on table 1, ms | On table K, ms |
|---|---|---|---|---|---|---|---|
session (SET) | stagger | 1 | 2000 | committed x3 | 1704 / 1705 / 1703 | 1699 / 1694 / 1678 | 1699 / 1694 / 1678 |
session | stagger | 2 | 2000 | committed x3 | 3406 / 3414 / 3421 | 3401 / 3395 / 3382 | 1695 / 1707 / 1694 |
session | stagger | 3 | 2000 | committed x3 | 5104 / 5132 / 5104 | 5100 / 5113 / 5080 | 1695 / 1712 / 1670 |
local (SET LOCAL) | stagger | 3 | 2000 | committed x3 | 5102 / 5112 / 5103 | 5082 / 5109 / 5068 | 1660 / 1705 / 1688 |
multi-lock | stagger | 3 | 1000 | committed x3 | 2567 / 2554 / 2552 | 2542 / 2538 / 2527 | 851 / 818 / 845 |
stmt (statement_timeout) | stagger | 3 | 1000 | 57014, committed, committed | 3150 / 2561 / 2555 | 3109 / 2546 / 2534 | 1397 / 854 / 804 |
multi-stmt | stagger | 3 | 1000 | committed x3 | 2561 / 2554 / 2555 | 2551 / 2540 / 2543 | 826 / 850 / 829 |
local | last | 2 | 1000 | 55P03 x3 | 1856 / 1861 / 1854 | 1845 / 1853 / 1848 | 984 / 987 / 989 |
local | last | 3 | 2000 | 55P03 x3 | 5405 / 5406 / 5407 | 5384 / 5392 / 5404 | 1997 / 1977 / 1997 |
stmt | last | 3 | 2000 | 57014 x3 | 5406 / 5409 / 5422 | 5389 / 5391 / 5350 | 1977 / 1992 / 2000 |
lt-lt-st (lock_timeout 1000, statement_timeout 3000) | last | 1 | 1000 | 55P03 x3 | 1007 / 1010 / 1005 | 980 / 985 / 997 | 980 / 985 / 997 |
lt-st-lt (statement_timeout 1000, lock_timeout 3000) | last | 1 | 1000 | 57014 x3 | 1077 / 1032 / 1488 | 975 / 976 / 1316 | 975 / 976 / 1316 |
Variants: session is SET lock_timeout then the statements one by one; local is BEGIN; SET LOCAL lock_timeout; ...; stmt is SET statement_timeout only; multi-lock and multi-stmt send one simple-query message begin; alter; alter; ...; commit after the SET.
- Nothing in the
staggerrows timed out, and readers of the first table were blocked about as long as the whole migration. K = 3, T = 2,000 ms committed after 5104 / 5132 / 5104 ms withSETand 5102 / 5112 / 5103 ms withSET LOCAL; the first table’s probe saw 5100 / 5113 / 5080 ms and 5082 / 5109 / 5068 ms. The last table’s readers saw 1695 / 1712 / 1670 ms. Alock_timeoutof 2 s that never fired left readers of table 1 waiting about 2.5 times its value, and the figure grows with K. - When the last statement does time out, the readers of table 1 were blocked for almost the whole migration, not for T. K = 3, T = 2,000 ms,
SET LOCAL:55P03after 5405 / 5406 / 5407 ms, readers of table 1 blocked 5384 / 5392 / 5404 ms, readers of table 3 blocked 1997 / 1977 / 1997 ms (the last statement’s own wait, against T = 2,000 ms). statement_timeoutwithout anylock_timeoutbehaves the same per statement and fails with SQLSTATE57014instead of55P03. Sent as one simple-query message, K = 3 statements under a 1,000 msstatement_timeoutcommitted after 2561 / 2554 / 2555 ms, so the timeout was not applied to the message as a whole, which matches the documented wording on server 17.11.3lock_timeoutsent as one multi-statement message behaved like the statement-by-statementSET LOCALcells (K = 3, T = 1,000 ms: 2567 / 2554 / 2552 ms).- After
COMMITthe session still readlock_timeout1sor2swhere it had been set withSET(3 of 3 in eachsessioncell) and0where it had been set withSET LOCAL(3 of 3 in eachlocalcell). - With
lock_timeout1,000 ms andstatement_timeout3,000 ms the error was55P03after 1007 / 1010 / 1005 ms; withstatement_timeout1,000 ms andlock_timeout3,000 ms it was57014after 1077 / 1032 / 1488 ms. The documentation says alock_timeoutequal to or above a nonzerostatement_timeoutis rather pointless.3
One cell failed: stmt stagger K = 3, T = 1,000 ms, repetition 1 ended in 57014 after 3150 ms where the cell expected a commit (repetitions 2 and 3 committed after 2561 and 2555 ms). Statement 3 started at about 1,704 ms, so its timeout was due at about 2,704 ms; its blocker was due to release at 2,550 ms, 154 ms earlier, and the error reached the client at 3,150 ms, 446 ms after the timeout was due. The reading, an inference not separated: the client process stalled, the release timer ran more than 154 ms late and the error was read late, on a host at load average 15 to 40. Other cells show smaller stalls of the same kind, for example local K = 1, T = 2,000 ms, repetitions 2 and 3 committing after 2,050 and 2,111 ms where 1,700 was expected. Not measured in LQ01: row-level locks, DDL (data definition language) other than ADD COLUMN, a statement that waits for locks on more than one object, a table rewrite, the hosted platform’s role settings, any pooler.
The queue behind a waiting ACCESS EXCLUSIVE (LQ02)
Section titled “The queue behind a waiting ACCESS EXCLUSIVE (LQ02)”Timeline per arm, in seconds from the start of the load: load alone to 12; a blocker (select count(*), pg_sleep(25) from lq_items, ACCESS SHARE for 25 s) from 12; the ALTER (alter table lq_items add column, metadata only) at 17; the blocker ends at 37 and the ALTER commits then; 30 s of load after the ALTER ends. Arms: control (no blocker, no ALTER), hold20 (no lock_timeout, the ALTER waits 20 s), lt2s (SET LOCAL lock_timeout 2 s, the blocker keeps running), and hold20-st3 (as hold20, with statement_timeout 3 s on the roles anon and app_reader; anon 3 s is the hosted default recorded in the lab’s data-api-capacity DC03a). Three repetitions per figure, run B.
- Blocker only. In the 5 s blocker-only window every request answered 200 in all three
hold20repetitions (PostgREST200:101x 3, PgBouncer role200:51x 3), and the same in the other arms. - With the
ALTERwaiting, readers queued on both paths. Inhold20, 0 / 0 / 0 PostgREST answers per second and 0 / 0 / 0 PgBouncer-role answers per second ended inside the 20 s wait. PostgREST requests sent during the wait:200:219 504 PGRST003:181,200:218 504 PGRST003:182,200:218 504 PGRST003:182. The 200s ended when theALTERcommitted, after up to 19982 / 19989 / 19984 ms. The first 504 ended 11.1 / 11.1 / 11 s after theALTERstarted (acquisition timeout 10 s). PgBouncer-role requests sent during the wait:200:200x 3, p50 9939 / 9944 / 9955 ms, longest 19933 / 19929 / 19935 ms; none failed, they waited. The control arm in the same run answered 200 for every request, longest 50 / 55 / 27 ms (PostgREST) and 19 / 27 / 18 ms (role). - PostgREST’s answer for a request that could not get a pool connection was HTTP (Hypertext Transfer Protocol) 504 with code
PGRST003and the message “Timed out acquiring connection from connection pool.” The v14 errors page listsPGRST003as 504 andPGRST001andPGRST002as 503, so a pool timeout is a 504 on 14.18.4 - Pool saturation in
hold20: PgBouncerSHOW POOLS(1 s samples) readcl_waiting10 / 10 / 10 andsv_active10 / 10 / 10 for both login roles,maxwaitup to 19 / 19 / 19 s (authenticator) and 18 / 18 / 18 s (app_reader); Postgres had 21 backends waiting for the lock in every repetition (theALTERand 10 per role) of 22 client backends. PostgREST’s own gaugepgrst_db_pool_waitingread at most 5 / 3 / 4 while about 180 requests were waiting, so on this PostgREST it did not show the queue (inhold20-st3it read 123 / 1 / 120). - Recovery with no restart: requests sent in the second the
ALTERcommitted had a p95 of 295 ms (PostgREST) and 5 ms (role), and 6 ms and 3 ms in the next second.cl_waitingwas 0 at 10 and 30 s, lock waiters 0, and no request failed among the 600 PostgREST and 300 role requests sent after the end. The 20 requests per path at the end all answered. The same held in the other arms and repetitions. lt2s: theALTERfailed55P03after 2 s (alter_wall_s2 x 3). Readers sent in those 2 s all answered 200, PostgREST p50 966 / 969 / 970 ms and longest 1957 / 1958 / 1977 ms, role longest 1907 / 1904 / 1905 ms, no errors. The pools still filled inside the 2 s (authenticatorcl_waiting10 / 10 / 10;app_reader10 / 10 / 1), with 20 / 20 / 20 backends waiting for the lock. The blocker kept running to 37 s and readers were unaffected by it.hold20-st3: withstatement_timeout3 s the readers holding a server connection were cancelled:500 5701460 x 3 on PostgREST andpg 5701460 x 3 on the role path (message “canceling statement due to statement timeout”), the first one 3 / 3 / 3 s (PostgREST) and 3.1 s (role) after theALTERstarted. The pools still saturated (cl_waiting10 for both roles, 21 lock waiters), PostgREST still returned504 PGRST003137 / 137 / 138 times against 181 to 182 without the statement timeout, and requests waited up to 14908 / 14082 / 13324 ms (PostgREST) and 13930 / 13935 / 13930 ms (role). The reading, with the order of hand-over not traced: each cancelled statement frees a server connection that the next queued client takes and waits on in turn.
A lock_timeout of 2 s bounded the reader stall to 2 s; a statement_timeout on the reader roles bounded how long one reader holds a connection and did not stop the queue. This repeats on a local stack what the lab’s data-api-capacity experiment recorded on a managed project with a lock held by a session (DC18, 2026-10-27, managed Small at 500 requests a second and 4XL at 1,500, a 30 s ACCESS EXCLUSIVE lock: 0 successful responses a second for its first 25 s and recovery 44.9 s and 19.8 s after it ended, derived from the backlog).5 The differences are the cause (a waiting ALTER here, a held LOCK there), the pool sizes (10 and 20 here) and the rates (20 and 10 a second here), so the recovery times are not compared. The same page records a 12.6 s lock giving 60 x 500 57014 on anon reads and no aborted pooled connection (DC06); see Supabase Data API (application programming interface) connection pool and capacity.
Not measured in LQ02: writes through PostgREST, other lock modes, a waiting ALTER that is granted and then runs for seconds (ADD COLUMN here holds ACCESS EXCLUSIVE for milliseconds), pool sizes other than 20 and 10, and a hosted project.
Does the pooler recover (LQ03)
Section titled “Does the pooler recover (LQ03)”Same stack, load and timeline as LQ02 with a 30 s ALTER wait (blocker 33 s), PgBouncer query_wait_timeout 10 s (SHOW CONFIG read back per arm), Postgres max_connections 40, and per-arm client behaviour. Nothing is restarted inside an arm; after the load stops each arm sends 20 requests per path through fresh connections and reads PgBouncer’s SHOW SERVERS and SHOW CLIENTS and the Postgres backend count. Arms: qwt10-patient (client timeout 60 s, 90 s of load after the ALTER), qwt10-impatient3s (client timeout 3 s: an HTTP request is aborted, a pool client destroys its connection with the query still running; 45 s after), qwt10-impatient3s-login1 (the same with server_login_retry 1 s, default 15), and qt5-patient (PgBouncer query_timeout 5 s, patient clients, 45 s after).6 Three repetitions each; “after” means requests sent after the ALTER ended.
| Arm | PostgREST during the wait | PgBouncer-role path during the wait | After the ALTER ended |
|---|---|---|---|
qwt10-patient | 504 PGRST003 366 / 365 / 366, 503 PGRST001 17 / 19 / 19, 200 217 / 216 / 215; first error 10.5 / 10.9 / 10.9 s after the ALTER started | pg 08P01 query_wait_timeout 20 x 3, 200 280 x 3 | PostgREST 200:1800 x 3 and role 200:900 x 3: no failure |
qwt10-impatient3s | timeout 546 / 546 / 545, 200 54 / 54 / 55 | pg 53300 too many clients 257 / 257 / 247, timeout 43 / 43 / 53 | PostgREST no failure; role 53300 45 / 45 / 45 sent after, last 4.5 / 4.5 / 4.5 s after the end |
qwt10-impatient3s-login1 | timeout 544 / 548 / 546 | 53300 275 x 3, timeout 25 x 3 | PostgREST no failure; role 53300 9 / 1 / 8, last 0.9 / 0.1 / 0.8 s after the end |
qt5-patient | 503 PGRST002 377 / 368 / 369, 503 PGRST001 219 / 229 / 226 | pg 53300 258 / 263 / 257, pg 08P01 query_timeout 26 / 22 / 28, pg 08P01 16 / 15 / 15 | PostgREST 503 PGRST002 246 / 254 / 253 sent after, last 12.3 / 12.7 / 12.6 s after the end; role 53300 59 / 60 / 58, last 5.9 / 6 / 5.8 s after |
- In every arm and repetition PgBouncer
cl_waitingwas 0 for both roles 10 s and 30 s after theALTERended, Postgres had 0 backends waiting for a lock, and the 20 final requests per path all answered (final_rest_ok_of_20andfinal_pool_ok_of_20are 20 in all 12 results). No arm needed a restart to answer again. qwt10-patient: PgBouncer disconnected waiting clients after 10 s, 20 per role per repetition (log linesclosing: query_wait_timeout20 per role, matchingpg 08P01 query_wait_timeout20 on the role path); PostgREST’sPGRST001(“Database client error. Retrying the connection.”) 17 to 19 times is the same cut-off seen from PostgREST’s pool.- Abandoned queries piled up on Postgres. Backends waiting for the lock peaked at 35 and total backends at 36 in all 6 impatient repetitions and in the 3
qt5-patientrepetitions, where PgBouncer, not the client, gives up on the query (max_connections40, 3 reserved for superusers), against 21 and 22 with patient clients (pool 10 per role plus theALTER). In the impatient arm the surplus 14 is derived from 35 - 1 - 10 - 10 and fits PgBouncer’s log, 24 lines ofclosing: client disconnect while server was not readyin each of the 6 impatient repetitions: PgBouncer closes the server connection of an abandoned client and opens a replacement, and the abandoned query keeps waiting in Postgres. Its login errors then readserver login has been failing, cached error: remaining connection slots are reserved for roles with the SUPERUSER attribute, 302 / 302 / 292 log lines inqwt10-impatient3s. After the lock cleared, backends read 11 at 10 s and 11 to 12 at 30 s in that arm (20 with patient clients, 2 and 3 inqt5-patient). server_login_retry: with the default 15 s the role path kept failing 4.5 s after theALTERended in 3 of 3 (45 failed requests sent after, the same count each time); with 1 s it failed 0.1 to 0.9 s after in 3 of 3 (1 to 9 requests). PgBouncer’s log showsserver login failed2 / 2 / 2 times with the default and 25 / 24 / 25 times with 1 s during the arm. The lever moves the post-lock error window; why the default’s window is 4.5 s and not 15 s was not separated.qt5-patientis the longest lingering failure: PostgREST kept answering503 PGRST002(“Could not query the database for the schema cache. Retrying.”) for 12.3 / 12.7 / 12.6 s after theALTERended, each answer within tens of milliseconds, then answered 200 with no action taken (all 20 requests a second failing until second 42, all ok from 43). The PostgREST connection-pool page says it “will retry forever with exponential backoff, with a maximum backoff time of 32 seconds between retries”.7 The reading, a hypothesis only: the schema-cache retry schedule spanned the end of the lock. PostgREST’s log (levelerrorhere), the retry times anddb-channel-enabledtrue were not measured.
One result failed: LQ03-qwt10-impatient3s-login1-r1, “PgBouncer clients still waiting in the last 5 s”. Its samples read cl_waiting 0 for every second from 31 to 70 s, 1 at second 71 (authenticator, sv_active 1) and 0 at 72 to 74, with PostgREST’s per-second p95 at 790 ms in the same stretch and host load average 7.8 before and 12.7 after. The reading is a one-second host or harness stall that the end-of-run check caught, not state left behind. Recorded as a fail and not retried.
Wedge: not reproduced. Tried, all with no restart: a lock held 30 s against PgBouncer cutting waiting clients (query_wait_timeout 10 s); clients that abandon queries (3 s) so that Postgres reaches max_connections; PgBouncer’s own query_timeout 5 s against both paths with Postgres at max_connections; and the default server_login_retry 15 s against 1 s. The report that motivated the arm lasted until a restart; nothing here lasted past 12.7 s. Not tried: Supavisor (the hosted pooler’s code is not PgBouncer’s), a lock held for minutes, server_lifetime or server_idle_timeout changes, max_client_conn exhaustion, PgBouncer session mode, an application that retries failed requests, any hosted project.
A short lock_timeout with a retry loop (LQ04)
Section titled “A short lock_timeout with a retry loop (LQ04)”Long readers (select count(*), pg_sleep(6) from lq_items, 2 s gap, so an ACCESS SHARE holder 75% of the time) run under the LQ02 load. The migration starts when the current long reader has run 1, 3.5 and 5 s (repetitions 1, 2 and 3: 5, 2.5 and 1 s still to go). The same three phases in every arm; the phases were chosen, not sampled.
| Arm | Attempts | Time to commit, s | Each failed attempt waited, ms | Longest reader latency, PostgREST / role, ms | Requests slow (over 1.5 s) or failed in the window |
|---|---|---|---|---|---|
oneshot | 1 / 1 / 1 | 5 / 2.5 / 1 | none; the one attempt waited 5008 / 2509 / 1006 | 4963 / 2480 / 960 and 4913 / 2413 / 910 | PostgREST 71 / 21 / 0, role 35 / 10 / 0 |
retry-250ms | 6 / 4 / 2 | 5 / 3 / 1 | 251 to 252 (55P03) | 222 / 212 / 217 and 171 / 163 / 165 | 0 / 0 / 0 on both paths |
retry-1000ms | 3 / 2 / 2 | 5 / 2.5 / 2 | 1001 to 1007 (55P03) | 975 / 965 / 967 and 923 / 913 / 915 | 0 / 0 / 0 on both paths |
- One shot stalled readers for the whole wait: PostgREST requests sent during it had p50 2507 / 1249 / 460 ms, none failed, and both PgBouncer pools filled (
cl_waiting10 / 10 / 9 for theauthenticatorrole, 10 / 10 / 0 for the other). - The retry loop got the lock about when the long reader ended: 5 / 3 / 1 s (250 ms timeout) and 5 / 2.5 / 2 s (1,000 ms) after the first attempt, against the one shot’s 5 / 2.5 / 1 s. Readers’ longest latency stayed within about the timeout (222 ms against 250 ms; 975 ms against 1,000 ms) and readers kept being answered during the retry window (PostgREST
rest_ok_ended_per_s_in_block19.9 x 3 at 250 ms; 16 / 15.9 / 19.9 at 1,000 ms; the arrival rate is 20). - The 1,000 ms timeout filled the
authenticatorpool in repetitions 1 and 3 (cl_waitingmax 10 and 9); the 250 ms timeout did not show waiting clients in the 1 s samples (0 x 3), which is not evidence that none waited, since a 250 ms wait can fall between two samples. LQ04-set-leak(PgBouncer pool size 1 for the role, so two clients share one server connection): a client that ranSET lock_timeout = '1234ms'outside a transaction and disconnected left the next client reading1234ms(3 of 3); afterBEGIN; SET LOCAL lock_timeout = '1234ms'; COMMITthe next client read0(3 of 3). The PgBouncer documentation saysserver_reset_queryis not used with transaction pooling.6- The retry loop cost nothing in time to commit beyond the next attempt boundary (repetition 2 of
retry-250ms: 3 s against 2.5 s) and cut the readers’ longest stall from the long reader’s remainder (5, 2.5 and 1 s) to about the timeout. Each failed attempt is a55P03that the migration runner must treat as retryable.
Not measured in LQ04: jittered backoff, a loop with a total deadline, a long reader that does not end inside the attempt budget (40 attempts), DDL that needs a table rewrite, SET through Supavisor, and a hosted project.
What was not measured
Section titled “What was not measured”- Any hosted project, Supavisor, and the platform’s role settings. The 8 s
lock_timeoutandstatement_timeoutthat the lab recorded on the hostedauthenticatorrole were not set in this rig. - Writes through PostgREST, other lock modes, row-level locks, and DDL other than
ADD COLUMN. - A lock held for minutes, other pool sizes than 10 per role and 20 in PostgREST, and PostgREST’s log and retry times during recovery.
- Application-side retries of failed requests during the stall.
Reading the numbers
Section titled “Reading the numbers”- Latencies below a second sit inside host noise: load average 8.4 to 40.3 in run A and 1.5 to 13.7 in run B on the same 10-CPU VM. Each arm’s figures are read against its own run, not across runs.
- “Longest reader block” and “longest latency” are the maxima over a repetition, so one stalled probe loop moves them; the failed LQ01 cell and the failed LQ03 result are those stalls, recorded as fails.
- Request counts such as
504 PGRST003:181are requests sent during the 20 s wait, by the open-loop generator at 20 a second; they scale with the rate, the wait and the 10 s acquisition timeout. - Three repetitions per arm; the phases in LQ04 were chosen, not sampled, so the retry arms are demonstrations of the shape rather than an estimate of its distribution.
What to do about it
Section titled “What to do about it”| Practice | Evidence | Module |
|---|---|---|
Retry DDL under a short SET LOCAL lock_timeout | 250 ms with retries: readers’ longest latency 222, 212 and 217 ms against 4,963, 2,480 and 960 ms for one shot; 0 slow or failed requests against up to 71 | LQ04 |
Treat 55P03 from the migration as retryable | Each failed attempt waited 251 to 252 ms and then failed 55P03; the loop committed about when the long reader ended | LQ04 |
| Budget K x T of blocking for a K-statement migration, or split it | K = 3, T = 2,000 ms: readers of table 1 blocked 5,100 ms with no timeout firing | LQ01 |
Use SET LOCAL, not SET, through a transaction pooler | SET lock_timeout left 1234ms for the next client 3 of 3; SET LOCAL left 0 3 of 3 | LQ04, LQ01 |
Do not rely on a reader statement_timeout to protect the pools from a waiting ALTER | It cancelled 60 readers per path in 3 s and the pools still saturated: cl_waiting 10, 21 lock waiters, PGRST003 137 to 138 | LQ02 |
Look for the long reader behind the waiting ALTER | A holder of ACCESS SHARE blocked nobody (every request 200 in the blocker-only window); once the ALTER queued, 0 answers per second ended inside the wait | LQ02 |
Read PGRST003 after 10 s as a pool wait, not a database outage | 504 PGRST003 first appeared 11 to 11.1 s after the ALTER started, with acquisition timeout 10 s | LQ02 |
Wait out about 13 s of PGRST002 after a lock clears before restarting the pooler | qt5-patient: 12.3 to 12.7 s of 503 PGRST002, then 200 with no action; no arm needed a restart | LQ03 |
| Set client timeouts above the longest expected lock wait | 3 s client timeouts left 35 backends waiting of 36 and 53300 too many clients 257 / 257 / 247 on the role path, against 21 and 22 backends and no 53300 with patient clients (query_wait_timeout 10 s in both) | LQ03 |
Lower server_login_retry to shorten the post-lock error window | 1 s ended the post-lock 53300 window 0.1 to 0.9 s after the lock against 4.5 s with the default 15 s, at 25 server login failed log lines against 2 | LQ03 |
Reproducing
Section titled “Reproducing”The experiment is experiments/lock-queue in supabase-lab. It needs Docker, bun, and the three images (pinned by digest in lib/stack.ts).
make probe # all four modules, about 45 minutes, writes evidence/<ts>/make probe ONLY=LQ01 # one moduleLQ_TIME_SCALE=0.3 LQ_REPS=1 make probe ONLY=LQ02 # a smoke run of shortened phasesmake facts # measurements of the newest run as markdownEvidence
Section titled “Evidence”| Claim | Status | How it was checked |
|---|---|---|
lock_timeout bounds each statement; readers of table 1 blocked about K x T | measured, 30 cells x 3 repetitions, run A, one failed cell | LQ01, run A facts |
A waiting ALTER queues every later reader on PostgREST and PgBouncer; recovery within about 1 s of commit | measured, 4 arms x 3 repetitions, run B | LQ02, run B facts |
No wedge; PostgREST PGRST002 for 12.3 to 12.7 s in the query_timeout arm | measured, 4 arms x 3 repetitions, one result failed on a host stall; the schema-cache retry reading is a hypothesis | LQ03, run B facts |
250 ms retry loop against one shot; SET leaks, SET LOCAL does not | measured, 3 arms x 3 repetitions plus the leak probe; the phases were chosen | LQ04, run B facts |
Waiting requests join a queue and conflicting requests are granted in order of arrival; ACCESS EXCLUSIVE is the only mode that blocks a plain SELECT | documented21 | doc-read 2026-12-03 and again for this page; the lock manager README at REL_17_STABLE |
lock_timeout applies to each lock acquisition attempt; statement_timeout applies to each statement of a simple-query message | documented3; consistent with LQ01 | doc-read 2026-12-03 and again for this page |
| The hosted pooler’s behaviour | not measured | Supavisor not in the rig |
No docs-versus-runtime disagreement was found, so nothing was filed upstream.
Modules
Section titled “Modules”| Module | Experiment | Test | Artifact |
|---|---|---|---|
| LQ01 | lock-queue | lq01-lock-timeout.ts | run-2026-12-03T06-14-25-696Z |
| LQ02 | lock-queue | lq02-queue-behind.ts | run-2026-12-03T07-02-34-708Z |
| LQ03 | lock-queue | lq03-pooler-recovery.ts | run-2026-12-03T07-02-34-708Z |
| LQ04 | lock-queue | lq04-safe-migration.ts | run-2026-12-03T07-02-34-708Z |
| DC18 (cited, not run here) | data-api-capacity | not re-read for this page | RUNLOG |
Related docs
Section titled “Related docs”- Postgres lock table capacity and out of shared memory - the other lock failure: one transaction touching too many objects, rather than many waiting on one.
- Foreign keys to auth.users block sign-in during validation - an idle-in-transaction reader blocking an
ADD COLUMNon a hot table, and thelock_timeoutthose probes carried. - Supabase Data API connection pool and capacity - the hosted pool sizes, role timeouts and the 12.6 s lock run this page’s LQ02 repeats locally.
- pgjdbc and HikariCP on Supabase connection paths - the pooler modes a client behind PgBouncer or Supavisor meets.
- PostgREST retries of an RPC (remote procedure call) that raises 40001 - another case where a client-visible error code decides whether PostgREST retries.
References
Section titled “References”-
PostgreSQL Global Development Group, “Explicit Locking,” PostgreSQL 17 Documentation. https://www.postgresql.org/docs/17/explicit-locking.html ↩ ↩2
-
PostgreSQL Global Development Group, “src/backend/storage/lmgr/README,” postgres/postgres, tag
REL_17_STABLE. https://github.com/postgres/postgres/blob/REL_17_STABLE/src/backend/storage/lmgr/README ↩ ↩2 -
PostgreSQL Global Development Group, “Client Connection Defaults,” PostgreSQL 17 Documentation,
lock_timeoutandstatement_timeout. https://www.postgresql.org/docs/17/runtime-config-client.html ↩ ↩2 ↩3 -
PostgREST, “Errors,” PostgREST v14 Documentation. https://docs.postgrest.org/en/v14/references/errors.html ↩
-
erfianugrah, “supabase-lab, data-api-capacity RUNLOG (DC18, 2026-10-27),” GitHub, commit aa4ccf8e. https://github.com/erfianugrah/supabase-lab/blob/aa4ccf8e1f5d57b306173bb976e2575da1db429f/experiments/data-api-capacity/RUNLOG.md ↩
-
PgBouncer, “Configuration,” PgBouncer documentation,
query_wait_timeout,query_timeout,server_login_retryandserver_reset_query. https://www.pgbouncer.org/config.html ↩ ↩2 -
PostgREST, “Connection Pool,” PostgREST v14 Documentation. https://docs.postgrest.org/en/v14/references/connection_pool.html ↩
Linked from
- Foreign keys to auth.users block sign-in during validation
- Postgres lock table capacity and out of shared memory (53200)
- Postgres MultiXact SLRU contention from foreign-key inserts into a hot parent row
- PostgREST retries of an RPC that raises 40001
- Supabase Data API connection pool and capacity
- Supabase incident classes and client workarounds