Cookbook recipe

Online schema changes without long locks

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

An ALTER TABLE blocks all reads/writes because it waits for (and then holds) an ACCESS EXCLUSIVE lock behind a long query. Do it safely. Diagnose it See what the DDL would wait behind: SELECT pid, now()-query_start…

Investigation Path

An ALTER TABLE blocks all reads/writes because it waits for (and then holds) an ACCESS EXCLUSIVE lock behind a long query. Do it safely.

Diagnose it

See what the DDL would wait behind:

SELECT pid, now()-query_start AS runtime, left(query,60)
FROM pg_stat_activity WHERE state='active' ORDER BY runtime DESC;

Why it happens

Most ALTERs need a brief ACCESS EXCLUSIVE lock, but they must first wait for conflicting transactions — and while waiting they queue everyone behind them. A long-running query can turn a fast ALTER into a system-wide stall.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes