Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parameters for currently running queries in PostgreSQL

Tags:

We wrote a small tool which displays all currently running queries. We get the currently running queries from pg_stat_activity.

The problem is: We dont know the parameters which were given to the query. We can only see the placeholders $1, $2, etc.

Is there any way to get the parameters for a currently running query?

The only workaround could be to enable the query log and parse the parameters from the query log, but this would be a very dirty and slow solution.

like image 394
Hendrik Avatar asked Sep 24 '11 10:09

Hendrik


People also ask

How do you check which query is running in Postgres?

Identify the current activity of the session by running the following command: SELECT * FROM pg_stat_activity WHERE pid = PID; Note: Replace PID with the pid that you identified in the step 1.

How do you analyze a query performance in PostgreSQL?

The most powerful tool at our disposal for understanding and optimizing SQL queries is EXPLAIN ANALYZE , which is a Postgres command that accepts a statement such as SELECT ... , UPDATE ... , or DELETE ... , executes the statement, and instead of returning the data provides a query plan detailing what approach the ...

What is PID in Pg_stat_activity?

pid: Process ID of the server process holding/waiting for this lock. Joining this column onto the pg_stat_activity view can provide all of the information above on the query / user / time etc.


2 Answers

I don't think it's possible. I have faced the same issue.

like image 194
Peter Eisentraut Avatar answered Sep 30 '22 01:09

Peter Eisentraut


I use to run all the queries through stored procedures/functions. This way you can add code to make a log at the start of every function.

like image 22
DavidEG Avatar answered Sep 29 '22 23:09

DavidEG