SkillVaultskills Browse all 500 skills

Data · Version 1.0.0 · Reviewed 2026-08-02

PostgreSQL Lock Contention Analyst

Make data systems more correct and operable for blocking chain analysis and Safe DDL sequencing with evidence, explicit trade-offs, and a verification plan.

4 method steps 6 documented failure modes 5 diagnostic checks 7 quality gates

Diagnoses blocking chains, lock escalation, long transactions, and the DDL patterns that stall a live PostgreSQL workload.

₹149 one-time

Get this skill archive

What this skill helps you do

  • Blocking chain analysis
  • Safe DDL sequencing
  • Idle-in-transaction diagnosis

How PostgreSQL Lock Contention Analyst works

You provide

pg_stat_activity, pg_locks, and the stall window

It inspects

Blocking chain root, transaction age, and lock modes

It decides

Which session to terminate and which DDL to resequence

You verify

Blocking tree empties and lock waits return to zero

What it checks first

PostgreSQL Lock Contention Analyst diagnoses blocking chains, lock escalation, long transactions, and the DDL patterns that stall a live PostgreSQL workload. Use it when the work involves Blocking chain analysis, Safe DDL sequencing, Idle-in-transaction diagnosis.

  1. The blocking chain root rather than the blocked query: `pg_blocking_pids()` identifies the transaction everyone is waiting on.
  2. Whether any session is `idle in transaction`, which holds locks indefinitely while doing no work.
  3. Lock mode conflicts specifically — `ACCESS EXCLUSIVE` conflicts with everything including plain reads.
  4. Transaction age (`xact_start`), since a long transaction also blocks vacuum and inflates bloat.
  5. Whether stalls correlate with a deployment or migration window, which points to DDL rather than workload.

Failure modes it recognizes

  • A short DDL statement queuing behind a long read, then blocking every subsequent query behind it — the queue, not the DDL, causes the outage.
  • An `idle in transaction` session from a connection pool that opened a transaction and never committed.
  • A foreign key insert taking a lock on the referenced row and serializing writes to a hot parent record.
  • `CREATE INDEX` without `CONCURRENTLY` blocking all writes for the duration of the build.
  • Autovacuum unable to reclaim tuples because an old transaction holds the snapshot horizon, causing gradual slowdown.
  • Deadlock from two transactions updating the same rows in different order, resolved by an automatic abort that surfaces as a random failure.

Answers it will reject

  • Killing the blocked queries instead of the blocker, which frees nothing and loses work.
  • Adding `statement_timeout` alone, which limits damage but leaves the contention mechanism unchanged.
  • Raising connection pool size during a lock stall, which increases the number of waiters rather than throughput.
  • Attributing low-CPU stalls to disk or network before checking `pg_locks`.

Decision rules it applies

  • When CPU and I/O are low but latency is high, treat it as a lock or wait problem until proven otherwise.
  • Always set a short `lock_timeout` before DDL so a migration fails fast instead of queuing behind a reader.
  • Use `CONCURRENTLY` for index creation and drops on live tables, accepting the longer build for the absent lock.
  • Add constraints as `NOT VALID` first and validate separately, which takes a weaker lock.

Evidence it asks for

  • Query `pg_stat_activity` joined with `pg_blocking_pids(pid)` to render the full blocking tree during the stall.
  • Watch `pg_locks` for `granted = false` rows to see what is queued and in which mode.
  • Track `max(now() - xact_start)` as a monitored metric so long transactions alert before they cause damage.

The method inside

  1. Establish what is actually true about blocking chain analysis from the supplied evidence, and mark what is missing.
  2. Identify the mechanism behind Safe DDL sequencing rather than restating the symptom.
  3. Choose the smallest defensible change for idle-in-transaction diagnosis, weighing impact, confidence, effort, and reversibility.
  4. Plan migration and observability before cutover

Deliverables

  • Blocking chain analysis assessment
  • Safe DDL sequencing decision and action plan
  • Idle-in-transaction diagnosis verification checklist

Evidence requirements

  • Schema, access patterns, query plans, or event contracts
  • Volume, cardinality, retention, and freshness
  • Consistency, latency, and migration constraints

Quality gates

  • Every material claim traces to supplied evidence or is labeled as a hypothesis.
  • The response follows the declared deliverable contract.
  • No execution, access, measurement, or verification is invented.
  • Secrets and personal data are redacted rather than repeated.
  • The user receives a concrete independent verification step.
  • The relevant failure modes in this domain were considered rather than only the reported symptom.
  • No listed anti-pattern was recommended as a solution.

Example task

Input

Writes to our orders table freeze for 30 seconds several times a day, but CPU and I/O stay low the whole time.

Expected output

Low CPU with stalled writes points to lock waiting rather than capacity. The pattern matches an ACCESS EXCLUSIVE lock queued behind a long-running read: once it queues, every subsequent query blocks behind it even though the read is harmless. Inspect pg_locks joined to pg_stat_activity for the blocking PID chain before changing any index...

Boundaries and compatibility

Ideal for

  • Blocking chain analysis: produce a decision or artifact grounded in supplied evidence.
  • Safe DDL sequencing: produce a decision or artifact grounded in supplied evidence.
  • Idle-in-transaction diagnosis: produce a decision or artifact grounded in supplied evidence.

Out of scope

  • Recommending indexes without a workload
  • Treating eventual consistency as universally acceptable

Agent compatibility

  • GitHub Copilot custom agents
  • Claude Agent Skills / SKILL.md
  • Any instruction-following chat model

Tool policy: Advisory by default. No tools are assumed. If the host provides tools, use read-only evidence gathering unless the user explicitly approves a scoped write or execution action.