Skip to content

Migrations: a configurable session preamble (for example lock_timeout) applied to every migration file #7565

Open

Description

@luizfneves404

Existing issues

  • I have searched the existing issues.

Affected area

Migrations

Problem to solve

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:

[db.migrations]
preamble = "SET lock_timeout = '5s'"

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.

Additional context

  • The PostgreSQL documentation for SET says that LOCAL issued outside a transaction block "emits a warning and otherwise has no effect" (https://www.postgresql.org/docs/current/sql-set.html).
  • 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.

Activity

  1. sudhi76 commented on Nov 25, 2026

    @sudhi76

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions