You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migrations: a configurable session preamble (for example lock_timeout) applied to every migration file #7565
Migrations that take ACCESS EXCLUSIVE locks, such as those generated by supabase db schema declarative sync, can queue behind a long-running read. While they wait, every later query on the table queues behind them. A lock_timeout makes that wait fail fast. Today there is no way to set one for every migration short of editing each file.
The only per-file control is the first-line directive -- pg-delta: transaction=false, which makes a file non-transactional. It is not a preamble.
Proposed solution
A config setting, for example under [db.migrations] or [experimental.pgdelta], whose SQL the CLI runs before each migration file's statements:
The CLI would apply it per batch, not once per file, for the two reasons under Additional context.
Alternatives considered
Write SET LOCAL lock_timeout = '5s'; by hand at the top of each migration file. This works for a transactional file with no CREATE INDEX CONCURRENTLY. It does not work for a non-transactional file, where SET LOCAL runs outside any transaction block, and it is lost after a CONCURRENTLY statement splits the file.
Edit the generated file by hand. Regeneration overwrites it.
At v3.1.0, a transactional file is sent as one batch. A pipeline-incompatible statement such as CREATE INDEX CONCURRENTLY flushes the open batch, runs alone, and then a new batch starts (apps/cli/src/command-internal/migration-apply.ts: isPipelineIncompatible at line 64, the statement loop at line 553). Each flushed batch is its own transaction, so a SET LOCAL in the first batch does not cover the batches after the split.
A file whose first line is -- pg-delta: transaction=false runs every statement without a transaction block (migration-apply.ts, line 479). The CLI then issues RESET ALL. A preamble for such a file has to be session-level and reset afterwards.
Related: the pre-flight discussion (supabase/supabase discussion #45217) proposes SET lock_timeout as a suggestion for a lock-risk check. This request is about applying the setting, not detecting the risk.
I tested the lock behaviour behind this on a throwaway Postgres 17 (plain psql, not through the CLI), in case it helps the design.
The problem reproduces. One transaction holds a read lock on a table, an ALTER TABLE ... ADD COLUMN waits behind it, then a plain SELECT count(*) arrives:
Migration session
How long the SELECT waited
no lock_timeout
9.9 s (until the blocker finished)
lock_timeout = '2s'
0.9 s (until the ALTER gave up)
A session-level SET survives the batch split. With SET lock_timeout outside a transaction, the value is still 2s after a CREATE INDEX CONCURRENTLY. With SET LOCAL it is back to 0. So running the preamble once per file, after the existing RESET ALL, may be enough, without repeating it per batch.
CREATE INDEX CONCURRENTLY needs care. When lock_timeout cancels it, an invalid index is left behind (indisvalid = false). A retry with IF NOT EXISTS then skips it with a notice and succeeds, so the migration would be recorded with an unusable index. It may be worth not applying the preamble to standalone CONCURRENTLY statements, or documenting this.
Activity
sudhi76 commentedon Nov 25, 2026
I tested the lock behaviour behind this on a throwaway Postgres 17 (plain
psql, not through the CLI), in case it helps the design.The problem reproduces. One transaction holds a read lock on a table, an
ALTER TABLE ... ADD COLUMNwaits behind it, then a plainSELECT count(*)arrives:SELECTwaitedlock_timeoutlock_timeout = '2s'ALTERgave up)A session-level
SETsurvives the batch split. WithSET lock_timeoutoutside a transaction, the value is still2safter aCREATE INDEX CONCURRENTLY. WithSET LOCALit is back to0. So running the preamble once per file, after the existingRESET ALL, may be enough, without repeating it per batch.CREATE INDEX CONCURRENTLYneeds care. Whenlock_timeoutcancels it, an invalid index is left behind (indisvalid = false). A retry withIF NOT EXISTSthen skips it with a notice and succeeds, so the migration would be recorded with an unusable index. It may be worth not applying the preamble to standaloneCONCURRENTLYstatements, or documenting this.