I have run pg_stat_activity I found there are log of session running. One of session using a function. In that function there are several blocks or dml select operations are being performed. How can I find the select or dml operation causing issue. Because directly it is not available in the query section of pg_stat_activity. Please help. Thank you.
I have tried to find out solution but unable to get. As in Oracle it is readily available in v$ession even any procedure is running.
2
Answers
Enable query logging in your PostgreSQL setup by setting
log_statement = 'all'
to identify the specific SQL statement that is creating problems with a PostgreSQL function. Next, use thepg_stat_activity
command to locate the session that is now running the function, note its backend PID, and look for items pertaining to that PID in the PostgreSQL log files. Consider the scenario where you are tracking a function calledmy_function()
in a session with a PID of 12345. You may discover:You may identify and examine the issue SQL statements that were performed within the function by looking at the log entries connected to the PID of the session.
Hope this helps, do let me know if this helps.
You can see the slow statement with
pg_stat_statements
(if you enable the extension and setpg_stat_statements.track = all
), or you can log the slow statement with auto_explain (which I recommend).In
postgresql.conf
, setThen restart PostgreSQL.