Skip to content
select * from depesz;·

PostgreSQL 20 Adds Backend-Level Lock Statistics

Waiting for PostgreSQL 20 – Add backend-level lock statistics

Key Takeaway

Michael Paquier committed a patch on June 30, 2026 adding per-backend lock statistics to PostgreSQL 20, covering lock wait counts, wait times, and fastpath_exceeded counts.

Why It Matters

Per-backend lock statistics break down aggregate waits by process and can be joined with pg_stat_activity to diagnose contention in running sessions.

Technical Breakdown · Distilled from the original source

Querying lock statistics by process

  • pg_stat_get_backend_lock(pid) takes a backend PID and returns one row per lock type. It exposes the same metrics as pg_stat_lock: wait counts, cumulative wait times, and fastpath_exceeded counts.
  • The function can be called from another connection. Like pg_stat_get_backend_io(), it can be joined with pg_stat_activity to examine lock statistics alongside activity information for each running backend.

Breaking down waits across three sessions

  • The article demonstrates the feature on a development build using three psql sessions. The first holds an ACCESS EXCLUSIVE lock on table t inside a transaction. The other two run SELECT * FROM t at different times and remain blocked until the transaction holding the lock ends.
  • The relation row in pg_stat_lock records two waits and a wait_time of 13525.1. Queries by PID show one wait per reader, with wait_time values of 8849.524 and 4675.576. Both per-backend rows show NULL for stats_reset.

Flushing and persistence

  • The patch builds on the backend statistics infrastructure introduced by commit 9aea73fc61d4 and adds a flag to pgstat_flush_backend() to control flushing of lock statistics. It bumps the catalog version but leaves PGSTAT_FILE_FORMAT_ID unchanged because backend statistics are never written to disk.
Source: select * from depesz;· depesz.com

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

Read Official Source

Related PostgreSQL Reference