HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Is there a way to see wait pid details in pg_stat_activity

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
waitwayseepg_stat_activitydetailspidthere

Problem

To see what's happening in the DB I can use a query on pg_stat_activity to see what all the processes are currently doing.

When I get a log like process 1019106 still waiting for AccessShareLock on relation 18314 of database 16402 after 1000.127 ms at character 2859 Processes holding the lock: 1013210, 1009626. Wait queue: 1019040, 1019105, 1019106. is there a way to get the process id's holding the lock into that pg_stat_activity query output?

I can make pg_stat_activity give me a table of pid, user, state, wait event type, wait event, state change time, and sql.

What I want is to add a column to that table so I can see the pid's it's waiting on.

Solution

You can use pg_blocking_pids() to obtain a list of PIDs that are blocking that process:

select pid, usename, state, wait_event_type, wait_event, 
       pg_blocking_pids(pid) as blocked_by
from pg_stat_activity

Code Snippets

select pid, usename, state, wait_event_type, wait_event, 
       pg_blocking_pids(pid) as blocked_by
from pg_stat_activity

Context

StackExchange Database Administrators Q#319762, answer score: 4

Revisions (0)

No revisions yet.