Skip to content
WebsiteHosting.com

troubleshooting Operations & procedures

DDL lock queues and migration outage risk

A schema change can delay reads before it acquires a lock. Follow one PostgreSQL-like queue to separate the time a reader spends behind a waiting request from the time it spends behind a held lock.

Latest recorded source check · P0193

What it means

A schema change that holds an exclusive lock for two seconds can delay a read for much longer. The schema change, or DDL operation, may first have to wait for an existing transaction to release a lock. A later read can then wait behind the DDL request, even though the two reads could have run together.

Try it

Simulation: follow three requests on one table. The existing read holds its lock until 60 s. The DDL requests an exclusive lock at 10 s and needs it for 2 s once granted. Another read arrives at 20 s. With the DDL lock wait timeout disabled (0), the DDL gets the lock at 60 s and releases it at 62 s. The later read starts at 62 s: its wait is 42 s.

DDL lock queues and migration outage risk

Show how a seemingly short DDL can block a growing queue behind a long transaction

Simulation
Modeled outcome: Queue

An exclusive DDL waiter blocks the following reader until release in the stipulated queue.

DDL lock queues and migration outage risk: synthetic scenario inputs
Replaces all inputs with the documented starting example and the indicated change. Reset restores the starting example.
Lock-holder, DDL and later query

Query outcome

  1. Start (s)62
  2. Wait (s)42
  3. Outcomequeue
Existing holderUntil 60 s
DDL request10 s
10 s
DDL lock held60–62 s
Later query waiting20–62 s
062 s

Equal request times are ordered DDL first in this fixture. A timeout cancels the DDL before acquisition only when expiry is strictly earlier than the potential start.

Evaluated inputs

Required scenario evidence
provided
Later compatible read arrives at
20 s
DDL requested at
10 s
Existing read transaction releases lock
60 s
DDL lock wait timeout (0 = disabled)
0 s
Exclusive lock hold time
2 s

PostgreSQL-like simplified lock queue: one ACCESS SHARE holder, one ACCESS EXCLUSIVE waiter, one later ACCESS SHARE request. No overtaking while exclusive waiter queued; grant wins on same-tick timeout by explicit toy convention. No multi-lock deadlock, actual cancellation or SQL. Inspect authorized activity/locks before any separately approved change.

About this model

The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.

Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.

Change only the DDL lock wait timeout from 0 to 15 s. Watch which request leaves the queue at 25 s. The DDL has waited 15 s since its request at 10 s, so it times out without ever holding the lock. The later read can now start at 25 s, after waiting 5 s. The original transaction still holds its lock until 60 s; it didn't have to finish for the later read to proceed. Reset returns to the original timings, disabled timeout and provided evidence.

Where the 42 seconds go

The lock modes explain why the delay starts before the schema change does any work. In PostgreSQL 18, the ACCESS SHARE table locks taken by ordinary reads are compatible with each other. An ACCESS EXCLUSIVE lock conflicts with both. The simulation assumes the DDL needs that mode and the later read cannot overtake its request.

Time in the initial exampleWhat stops the later read?
20 s to 60 sThe DDL is ahead of the read, waiting for the existing transaction. It holds no exclusive lock yet.
60 s to 62 sThe DDL now holds the exclusive lock. The read conflicts with that granted lock.
At 62 sThe DDL releases the lock and the read can start.

PostgreSQL calls a holder of a conflicting lock a hard blocker and an earlier waiter for a conflicting lock a soft blocker. Here the DDL is a soft blocker of the later read for 40 s and a hard blocker for 2 s. Looking only for a granted exclusive lock during those first 40 s would miss why the read is waiting.

Arrival time matters, too. If the later read arrives at 70 s instead of 20 s, it starts immediately: the DDL still waited from 10 s to 60 s, but it has released its lock by the time the read needs one. A DDL wait and a reader's wait are different intervals. Neither, on its own, measures a site-wide outage.

Use the timeout boundary in seconds. With the default holder ending at 60 s and DDL requested at 10 s, a 20 s timeout cancels at 30 s; the later query at 20 s waits 10 s. At a 50 s timeout the expiry equals the modeled acquisition time, so the fixture permits acquisition and includes the DDL hold interval. That equality convention is not a prediction of PostgreSQL timer races.

In practice

If reads stall during a migration, start with a read-only observation in an environment you're authorized to inspect. Record the time, the affected table, the waiting read and the DDL transaction. In PostgreSQL 18, pg_blocking_pids can identify a blocker that is waiting ahead of a request as well as one that already holds a conflicting lock. Use that information alongside the requested and granted lock modes:

  • If the DDL blocks the read but is itself waiting, check what blocks the DDL. A conflicting holder on the same table would explain the chain in the first interval.
  • If the DDL already holds the conflicting lock, the read is waiting for release. That observation explains the current wait, not how long the DDL spent waiting before the lock was granted.
  • If you have only coincident timestamps, the cause is unknown. Stop before changing a timeout or terminating a session. Establish the blocker relationship, transaction context and applicable database version first.

Before a migration, check the lock mode documented for the exact operation. PostgreSQL 18 ALTER TABLE subcommands do not all require ACCESS EXCLUSIVE. Also check the effective timeout policy: lock_timeout bounds each lock acquisition attempt, not the full migration. The simulation's one queue and its rule for simultaneous grant and timeout are not scheduling guarantees. It inspects no database and models neither deadlocks nor real cancellations.

When estimating disruption, keep two intervals separate: how long the DDL can remain queued and how long it will hold the lock once granted. For an affected read, the useful question is how much of each interval falls after the read arrives.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Can a read transaction block DDL?

Yes. For example, a transaction holding a lock that conflicts with the required DDL lock can make the DDL wait. The exact lock modes and lifecycle matter.

Does DDL always take exclusive locks?

No universal lock mode applies to all DDL. Consult the operation’s documented locks, engine version and actual transaction context before predicting blocking.

Why use a lock timeout?

A lock timeout bounds waiting for acquisition under the applicable PostgreSQL setting, not the hold time after acquisition. This toy cancels only if expiry is strictly before acquisition; its equality rule is not a real timer-race guarantee.

What is safe to inspect first?

Inspect actual transactions and locks in an authorized environment. PostgreSQL 18 pg_blocking_pids can identify both lock holders and earlier conflicting waiters; combine that observation with lock mode, transaction context and the planned DDL. Do not terminate a session based on the toy timeline.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. PostgreSQL explicit locking

    §13.3.1, table-level lock modes and conflicts. In PostgreSQL 18, ACCESS EXCLUSIVE conflicts with all table-level lock modes, including ACCESS SHARE held by ordinary SELECT. Actual duration and blocking depend on the command and transaction lifecycle.

    Checked .

  2. PostgreSQL ALTER TABLE

    ALTER TABLE locking. ALTER TABLE lock modes vary by subcommand; default exclusive mode is subject to documented exceptions.

    Checked .

  3. PostgreSQL 18: System Information Functions and Operators

    §9.27.1 pg_blocking_pids. pg_blocking_pids identifies hard blockers holding conflicting locks and soft blockers waiting for conflicting locks ahead in the queue. This is real PostgreSQL diagnostic behavior, not a universal FIFO scheduling guarantee.

    Checked .

  4. PostgreSQL client connection defaults

    Section 19.11.1 (lock_timeout). PostgreSQL lock_timeout applies to each lock acquisition attempt; zero disables it. It is not an end-to-end migration deadline.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.