Skip to main content

pg-lock-tracer logo

Lock tracing tools for PostgreSQL

Build Status PyPI PyPI - Downloads Release date License GitHub Repo stars

A set of eBPF-based tools designed to observe and visualize PostgreSQL 🐘 locking activity. These tools help users understand lock timing and contention behavior.

Key components:

  • pg_lock_tracer: heavy lock tracer (e.g., table level locks)
  • pg_lw_lock_tracer: lightweight lock (LWLock) tracer
  • pg_row_lock_tracer: row-level lock tracer
  • pg_spinlock_delay_tracer: spinlock delay tracer
  • animate_lock_graph: render animated lock graphs from pg_lock_tracer tracer output

Which tool do I need?

Tool Traces Typical question it answers
pg_lock_tracer Table/relation locks (heavy locks) Which tables are being locked, and how long do lock waits take?
pg_lw_lock_tracer Lightweight locks (LWLocks) Is a backend stuck waiting on an internal PostgreSQL lock (e.g. WALWrite, ProcArray)?
pg_row_lock_tracer Row-level locks Which rows are being locked (e.g. SELECT ... FOR UPDATE), and did the lock block?
pg_spinlock_delay_tracer Spinlock delays Is a backend busy-spinning on a low-level spinlock for too long?

Contents

Comparison with Built-in PostgreSQL Tools

PostgreSQL includes several lock-related tools, but none of them offer a continuous, timestamped trace.

  • pg_locks — a snapshot of locks currently held or waited for, but it does not track the history of acquisitions, releases, or their durations.
  • pg_stat_activity.wait_event / wait_event_type — shows what a backend is currently waiting on (such as Lock, LWLock, or BufferPin), but this is also just a snapshot.
  • log_lock_waits + deadlock_timeout record heavy-lock waits that go over a set threshold, but they do not cover LWLocks, row locks, or spinlock delays.

pg-lock-tracer fills this gap by providing a continuous, nanosecond-timestamped event stream with statistics and stack traces for heavy locks, LWLocks, row locks, and spinlock delays, all without changing the running server.

Quickstart

  1. Install the pg-lock-tracer tools:
pip install pg-lock-tracer
  1. Identify the PostgreSQL server binary used by your instance (e.g., /usr/lib/postgresql/X/bin/postgres).
  2. Start tracing a running Postgres PID (requires root privileges):
sudo pg_lock_tracer -x /path/to/postgres

This shows the locking activity of all processes running this PostgreSQL binary.

pg_lock_tracer

pg_lock_tracer observes the locking activity of a running PostgreSQL process (using eBPF and UProbes), giving you a continuous view of table-level lock activity, timings, and statistics — unlike pg_locks, which only shows locks held right now.

pg_lock_tracer -x /usr/lib/postgresql/18/bin/postgres -p 1234

📖 Full documentation — usage examples, JSON output, statistics, event filtering, stack traces, and animated lock graphs.

pg_lw_lock_tracer

pg_lw_lock_trace allows to trace lightweight locks (LWLocks) in a PostgreSQL process via Userland Statically Defined Tracing (USDT).

pg_lw_lock_tracer -p 1234

📖 Full documentation — usage examples, output samples, and statistics.

pg_row_lock_tracer

pg_row_lock_tracer allows to trace row locks (see the PostgreSQL documentation) of a PostgreSQL process using eBPF and UProbes.

pg_row_lock_tracer -x /usr/lib/postgresql/18/bin/postgres

📖 Full documentation — usage examples, output samples, and statistics.

pg_spinlock_delay_tracer

pg_spinlock_delay_tracer allows tracing spinlock delays in a PostgreSQL process. Spin locks protect short critical sections in the code; if another process already holds one, the requesting process repeatedly checks (i.e., "spins") until it becomes available, and after a while PostgreSQL performs a "spin delay" and yields the CPU to avoid busy-waiting. pg_spinlock_delay_tracer reports these delays, including the spin/delay counts and the source location where the delay occurred.

pg_spinlock_delay_tracer -x /usr/lib/postgresql/17/bin/postgres

📖 Full documentation — usage examples and a step-by-step guide to reproduce a spinlock delay.

Additional Information

Installation

The PostgreSQL lock tracing tools are available as a Python package and require Python 3.11 or newer. These tools depend on the Python package for BPF. Unfortunately, this package is currently not available via pip (the Python package manager). Therefore, the package of the Linux distribution needs to be installed to provide this dependency. On Debian and Ubuntu, this can be done by executing the following command:

apt install python3-bpfcc

The tracing tools can be installed system-wide or in a dedicated virtual environment. To create and install the tools in such a virtual environment, the following steps must be performed. To install the tools system-wide, these steps can be skipped.

cd <installation directory>
python3 -m venv .venv
source .venv/bin/activate

# Copy the distribution Python BCC packages into this environment
cp -av /usr/lib/python3/dist-packages/bcc* $(python -c "import sysconfig; print(sysconfig.get_path('platlib'))")

Now, the tracing tools can be installed directly via pip by executing:

pip install pg-lock-tracer

The tools are now installed and can be invoked by calling pg_lock_tracer or pg_lw_lock_tracer.

If you want to install the latest development snapshot of the tools, the following command needs to be executed:

pip install git+https://github.com/jnidzwetzki/pg-lock-tracer

Want to work on the tools themselves? See CONTRIBUTING.md for the development setup.

PostgreSQL Build

The software is tested with PostgreSQL versions 15, 16, 17, 18, and 19. In order to be able to attach the uprobes to the functions, they should not to be optimized away (e.g., inlined) during the compilation of PostgreSQL. Otherwise errors like Unable to locate function XXX will occur when pg_lock_tracer is started. For optimized builds, see the experimental support for production builds below.

It is recommended to compile PostgreSQL with the following CFLAGS: CFLAGS="-ggdb -Og -g3 -fno-omit-frame-pointer".

Production Builds (Experimental)

The following table shows which tools work with which PostgreSQL builds:

Tool Traced via Debug build Production build (e.g., -O2, LTO)
pg_lock_tracer uprobes on lock, table, transaction, and query functions ✅ ⚠️ Experimental (x86_64), needs the debug symbols to trace inlined functions
pg_lw_lock_tracer USDT probes ✅ (with --enable-dtrace) ✅ with --enable-dtrace (the apt.postgresql.org packages are built with it)
pg_row_lock_tracer uprobe on heapam_tuple_lock ✅ ✅ (the function is called via the table access method and is never inlined)
pg_spinlock_delay_tracer uprobe on perform_spin_delay ✅ ⚠️ Experimental (x86_64), needs the debug symbols to trace the inlined copies; without them, only calls of the out-of-line copy are traced (with LTO, almost none)

If DWARF debug information is available, pg_lock_tracer and pg_spinlock_delay_tracer use it to find all inlined copies of the traced functions and attach a probe to each one. They read function parameters, such as the lock mode, from the locations given by the debug information, like registers. The tools search for debug information in the binary itself (if PostgreSQL is compiled with -g), in /usr/lib/debug/.build-id/, and in the file referenced by the .gnu_debuglink section. To install debug symbols from apt.postgresql.org, use the following command:

apt install postgresql-17-dbgsym

When you start one of these tools, it lists the inlined copies being probed. If you use the -v option, it shows more details about these copies and where the parameters are located.

===> Function FastPathGrantRelationLock not found in the binary (probably inlined), falling back to the DWARF debug information; this may take a while
===> GrantLock: probed on its symbol and at 5 inlined copies
===> FastPathGrantRelationLock: no symbol, probed at 1 inlined copy

Limitations:

  • Only x86_64 is supported.
  • Reading the debug information takes a few seconds at startup. The packages of the Linux distributions are built with link time optimization (LTO), which inlines functions across source files. For these packages, reading the debug information takes up to a minute, and several hundred inlined copies are probed (e.g., table_close alone is inlined at more than 600 places).
  • Parameters that are optimized out at an inlined copy are shown as Unknown (e.g., the lock mode).
  • The return of a function (QUERY_END for exec_simple_query and LOCK_RELATION_OID_END for LockRelationOid) is traced only on the function symbol, which includes symbols renamed by LTO (e.g., exec_simple_query.lto_priv.0). Returns from inlined copies of these functions are not traced. If a build inlines one of these functions completely, its return events are missing, and pg_lock_tracer prints a warning at startup.
  • Stack traces (-s) require frame pointers (-fno-omit-frame-pointer), which are omitted by many production builds.
  • If neither the function symbol nor debug information is available, pg_lock_tracer stops with an error that asks to install the debug symbols.

pg_lw_lock_trace uses USDT probes. Therefore, PostgreSQL has to be compiled with --enable-dtrace to use this script.

License

This project is licensed under the Apache License 2.0.

Metadata

Release files for pg-lock-tracer 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pg-lock-tracer 0.8.0
File Size Uploaded Provenance Actions
pg_lock_tracer-0.8.0.tar.gz 55.7 kB Without provenance Details for pg_lock_tracer-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pg-lock-tracer 0.8.0
File Interpreter ABI Platform Actions
pg_lock_tracer-0.8.0-py3-none-any.whl Python 3 none any Details for pg_lock_tracer-0.8.0-py3-none-any.whl

Total release size: 108.5 kB

Release files / pg_lock_tracer-0.8.0.tar.gz

Download URL pg_lock_tracer-0.8.0.tar.gz
Size 55.7 kB
Tags Source
SHA-256 checksum
How to use checksums
10f7ba91df6884ba63c6f789c2a96e764ca15387acce32dd0bc6df5ed95b9b81
BLAKE2b-256 checksum
How to use checksums
88d19e0c980b4d51f7706a3a41e02483748a8b2dee28802ac36b66b408a7e5f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / pg_lock_tracer-0.8.0-py3-none-any.whl

Download URL pg_lock_tracer-0.8.0-py3-none-any.whl
Size 52.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36c5c9b1c744cc90f487894c3b6321bd783b6586502252b6d16b7819d8020c19
BLAKE2b-256 checksum
How to use checksums
c83cf652269f2f48f0207f7a69eeb124e5a348b388ad821f2986d20196619bdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for pg-lock-tracer 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page