Stopping Lock-Queue Pileups in PostgreSQL Migrations

A metadata-only ALTER TABLE takes 12 milliseconds on staging and still takes down production when it queues behind one 8-second query. Here is how PostgreSQL lock queues form and how to block unsafe DDL in GitHub Actions.

Almost every backend team learns PostgreSQL’s lock-queue rule the hard way at least once.

An engineer writes ALTER TABLE orders ADD COLUMN retry_count integer; in a migration, tests it against a staging copy with 500,000 rows, sees it finish in 9 ms (no table rewrite in modern PostgreSQL), and merges the pull request.

During deploy, production connection pools hit 100% saturation and health checks fail across every API pod.

The ACCESS EXCLUSIVE lock-queue sandwich

The migration itself did not rewrite orders. What took down the API was lock-queue fairness.

  1. At 14:00:01, an internal dashboard or background job starts a SELECT over orders (ACCESS SHARE lock) that takes 9 seconds.
  2. At 14:00:02, the deploy job runs ALTER TABLE orders ADD COLUMN ..., requesting ACCESS EXCLUSIVE. Because ACCESS SHARE is currently held by the dashboard query, ALTER TABLE enters the wait queue.
  3. At 14:00:02.005, 400 API requests/second run SELECT * FROM orders WHERE id = $1. To prevent writer starvation, PostgreSQL queues every new ACCESS SHARE request behind the waiting ACCESS EXCLUSIVE DDL session.

For the next eight seconds, zero reads or writes on orders return.

SET lock_timeout Aborts the DDL attempt in 2s instead of queuing API traffic behind slow readers
NOT VALID Splits ADD CONSTRAINT into a fast catalog entry plus SHARE UPDATE EXCLUSIVE validation
SARIF 2.1.0 Inline GitHub PR annotations on MIG001..MIG006 via culpritsql/culprit-ci

Four rewrites that keep hot PostgreSQL tables online

1. Always bound DDL wait time with lock_timeout

Never let a migration wait indefinitely in the lock queue. Prefixing DDL with SET LOCAL lock_timeout = '2s' (or session SET lock_timeout = '2s') fails the migration cleanly if a long reader is active so your deploy runner can retry seconds later without piling up application connections:

SET lock_timeout = '2s';
ALTER TABLE public.orders ADD COLUMN retry_count integer;
RESET lock_timeout;

2. Build and drop indexes CONCURRENTLY outside transaction blocks

Standard CREATE INDEX takes a SHARE lock, blocking every INSERT, UPDATE, and DELETE for the entire index build. CREATE INDEX CONCURRENTLY uses SHARE UPDATE EXCLUSIVE over two table scans so reads and writes continue uninterrupted, provided your migration runner disables its automatic BEGIN ... COMMIT wrapper (MIG002):

-- disable_ddl_transaction! (Rails) / atomic = False (Django) / -- migrate:no-transaction
CREATE INDEX CONCURRENTLY IF NOT EXISTS orders_customer_status_idx
  ON public.orders (customer_id, status);

3. Add FOREIGN KEY and CHECK constraints in two phases (NOT VALID)

ALTER TABLE child ADD CONSTRAINT ... FOREIGN KEY ... validates every existing row while holding SHARE ROW EXCLUSIVE, blocking writes on both tables. Splitting creation from validation downgrades the full scan to SHARE UPDATE EXCLUSIVE:

SET lock_timeout = '2s';
ALTER TABLE public.webhook_deliveries
  ADD CONSTRAINT webhook_deliveries_workspace_fk
  FOREIGN KEY (workspace_id) REFERENCES public.workspaces (id)
  ON DELETE CASCADE NOT VALID;
RESET lock_timeout;

-- Scans existing rows under SHARE UPDATE EXCLUSIVE (concurrent reads & writes continue):
ALTER TABLE public.webhook_deliveries
  VALIDATE CONSTRAINT webhook_deliveries_workspace_fk;

4. Index referencing FOREIGN KEY columns (MIG004)

PostgreSQL automatically creates a unique index on workspaces(id), never on webhook_deliveries(workspace_id). The day a customer deletes a workspace or a retention job prunes expired accounts, ON DELETE CASCADE (or referential integrity verification) executes a full sequential scan across webhook_deliveries for every deleted parent row.

Inspect the exact ModifyTable (Delete) execution plan diff below:

Live PostgreSQL EXPLAIN diffClick any operator or switch Diagram / Tree
Open in Culprit Explain ↗
A Unindexed FK cascade (3.8M Seq Scan)vsB With CONCURRENTLY FK index

1.3K× fasterWith CONCURRENTLY FK index took 1.3K× less time because it seeks webhook_deliveries_workspace_id_idx where Unindexed FK cascade (3.8M Seq Scan) scans webhook_deliveries (100% of Unindexed FK cascade (3.8M Seq Scan)'s cost).

96K
cost · Unindexed FK cascade (3.8M Seq Scan)
14.8
cost · With CONCURRENTLY FK index
612 ms
time · Unindexed FK cascade (3.8M Seq Scan)
0.46 ms
time · With CONCURRENTLY FK index
2 → 2
operators
1
identical regions
AUnindexed FK cascade (3.8M Seq Scan)PostgreSQL · actual
Actual plan2 operators · cost 96K80%
Measured run612 ms61Kreads · 43K from disk0rows
Where the 612 ms went
result
BWith CONCURRENTLY FK indexPostgreSQL · actual
Actual plan2 operators · cost 14.880%
Measured run0.48 ms17reads0rows
Where the 0.46 ms went
result
Live @culpritsql/plan-viewer PostgreSQL EXPLAIN diff: deleting one parent workspace cascades into a 3.8M-row Seq Scan on webhook_deliveries (612.4 ms, 61,000 buffers) until rule MIG004 adds webhook_deliveries_workspace_id_idx CONCURRENTLY (0.48 ms, 1,275x faster).

Adding webhook_deliveries_workspace_id_idx drops parent deletion from 612.4 ms (61,012 buffer reads scanning 3.8M rows) to 0.48 ms (17 buffer hits).

Enforcing migration rules automatically in GitHub Actions

Code review catches unsafe DDL when a senior engineer happens to spot it; automated PR gates catch it every time. Drop culpritsql/culprit-ci into .github/workflows/sql-ci.yml:

name: SQL Migration Gate

on:
  pull_request:
    paths:
      - "migrations/**"
      - "db/migrate/**"

permissions:
  contents: read
  pull-requests: write
  security-events: write

jobs:
  lint-migrations:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Lint PostgreSQL migrations & emit SARIF
        id: sql-gate
        uses: culpritsql/culprit-ci@v0.5.11
        continue-on-error: true
        with:
          sql: "migrations/*.sql"
          engine: "postgres"
          fail-on: "high"
          sarif-file: "culprit.sarif"
          post-comment: "true"

      - name: Annotate PR diff via GitHub Code Scanning
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: culprit.sarif

      - name: Enforce gate
        if: steps.sql-gate.outputs.passed != 'true'
        run: exit 1

Notice one detail that keeps developers from muting the linter: @culpritsql/ci tracks CREATE TABLE <name> statements within the same migration file. Indexing or altering a brand-new empty table created two lines earlier never triggers false MIG001 or MIG006 alarms.

When you also connect Culprit SQL Studio or MCP coding agents, culprit lint-sql prod-snapshot.json --file migrations/ --write weights every migration against your real table row counts, write rates, and recorded query workload, and rewrites supported DDL into concurrent operations automatically.

Frequently asked questions

Why does a fast metadata-only ALTER TABLE block SELECT queries in PostgreSQL?

ALTER TABLE acquires ACCESS EXCLUSIVE, which conflicts with every lock mode including ACCESS SHARE (taken by plain SELECT). If a 10-second reporting SELECT is already running when ALTER TABLE arrives, ALTER TABLE waits in the lock queue, and PostgreSQL's fair lock queue forces every new 2 ms SELECT behind the waiting ALTER TABLE until the 10-second query finishes.

Does PostgreSQL automatically create an index on FOREIGN KEY columns?

PostgreSQL indexes the referenced PRIMARY KEY or UNIQUE parent column automatically, not the referencing child column. Without an explicit index on the child foreign-key column, every DELETE or key UPDATE on the parent table runs a full Seq Scan across the child table while holding row locks.