F.20. pgrowlocks

The pgrowlocks module provides a function to show row locking information for a specified table.

F.20.1. Overview

pgrowlocks(text) RETURNS pgrowlocks_type
  

The parameter is a name of table. And pgrowlocks_type is defined as:

CREATE TYPE pgrowlocks_type AS (
        locked_row TID,         -- row TID
        lock_type TEXT,         -- lock type
        locker XID,             -- locking XID
        multi bool,             -- multi XID?
        xids xid[],             -- multi XIDs
        pids INTEGER[]          -- locker's process id
);
  

Table F-17. pgrowlocks_type

locked_rowtuple ID(TID) of each locked rows
lock_type"Shared" for shared lock, "Exclusive" for exclusive lock
lockertransaction ID of locker (Note 1)
multi"t" if locker is a multi transaction, otherwise "f"
xidsXIDs of lockers (Note 2)
pidsprocess ids of locking backends

Note1: If the locker is multi transaction, it represents the multi ID.

Note2: If the locker is multi, multiple data are shown.

The calling sequence for pgrowlocks is as follows: pgrowlocks grabs AccessShareLock for the target table and reads each row one by one to get the row locking information. You should notice that:

  1. if the table is exclusive locked by someone else, pgrowlocks will be blocked.

  2. pgrowlocks may show incorrect information if there's a new lock or a lock is freeed while its execution.

pgrowlocks does not show the contents of locked rows. If you want to take a look at the row contents at the same time, you could do something like this:

SELECT * FROM accounts AS a, pgrowlocks('accounts') AS p WHERE p.locked_ row = a.ctid;
  

F.20.2. Example

pgrowlocks returns the following data type:

Here is a sample execution of pgrowlocks:

test=# SELECT * FROM pgrowlocks('t1');
 locked_row | lock_type | locker | multi |   xids    |     pids      
------------+-----------+--------+-------+-----------+---------------
      (0,1) | Shared    |     19 | t     | {804,805} | {29066,29068}
      (0,2) | Shared    |     19 | t     | {804,805} | {29066,29068}
      (0,3) | Exclusive |    804 | f     | {804}     | {29066}
      (0,4) | Exclusive |    804 | f     | {804}     | {29066}
(4 rows)