Inspecting PostgreSQL Locks

Inspecting PostgreSQL Locks

Inspecting PostgreSQL locks can be done using the pg_locks view in the system catalog. This view displays information about all currently held and awaited locks. The columns in this view include the lock type, the table or index that the lock is on, the transaction that is holding the lock, and the mode in which the lock is held. You can also use the pg_stat_activity view to see which queries are currently running and if they are holding any locks. Additionally, the pg_stat_activity view can also be used to see the current state of a running query or transaction, including information about locks held or awaited. To get the detailed information about a particular lock, you can use the pg_lock_status() function, which will give you the current state of the lock and information about the transaction that holds the lock.


How locks are implemented in PostgreSQL?

Locks in PostgreSQL are implemented using a multi-version concurrency control (MVCC) system. This system allows multiple transactions to access the same data simultaneously, while still maintaining consistency and preventing conflicts.
PostgreSQL uses several types of locks, including table-level locks, row-level locks, and advisory locks.
Table-level locks are used to control access to an entire table. They come in two forms: exclusive locks, which prevent other transactions from accessing the table, and shared locks, which allow multiple transactions to access the table simultaneously but prevent any of them from modifying the data.
Row-level locks are used to control access to specific rows within a table. They can be either shared or exclusive, depending on the type of operation being performed.
Advisory locks are used to implement application-level locking, where the application can explicitly lock a specific resource and release it later.
When a transaction requests a lock, it may have to wait for the lock to be available if it is currently held by another transaction. The lock manager in PostgreSQL uses a priority-based algorithm to determine which transaction should be granted the lock next, and to resolve any conflicts that may arise.

Python script to monitor PostgreSQL Lock Activity

Here is an example of a Python script that uses the psycopg2 library to connect to a PostgreSQL database and retrieve information about active locks:


This script will retrieve the information of all the locks in the database where granted is false and prints it. You can modify the query to only retrieve the information you need and also you can schedule the script to run at a specific interval to get real-time lock activity.

About Shiv Iyer 455 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.