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.
- At
14:00:01, an internal dashboard or background job starts aSELECToverorders(ACCESS SHARElock) that takes9 seconds. - At
14:00:02, the deploy job runsALTER TABLE orders ADD COLUMN ..., requestingACCESS EXCLUSIVE. BecauseACCESS SHAREis currently held by the dashboard query,ALTER TABLEenters the wait queue. - At
14:00:02.005, 400 API requests/second runSELECT * FROM orders WHERE id = $1. To prevent writer starvation, PostgreSQL queues every newACCESS SHARErequest behind the waitingACCESS EXCLUSIVEDDL session.
For the next eight seconds, zero reads or writes on orders return.
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:
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.
Everything looks calm at 25% CPU until one batch worker opens 65+ savepoints alongside a long transaction and freezes every reader on SubtransSLRU. Here is the exact PGPROC mechanism, how to spot it in pg_stat_activity, and how to fix the application loop.