Reputation: 498
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.
Upvotes: 43
Views: 10552
Reputation: 31
How I solved this for numerical (BIG)INT values:
timeout 60s tcpdump -w db-$(date +%Y%m%d-%H%M).pcap
gzip db-$(date +%Y%m%d-%H%M).pcap
(pgsql.query || pgsql.type == "Row description" || pgsql.type == "Data row")
for h in $(seq 1 $(grep Parameter fileB.txt | cut -d ':' -f 2 | tr -d ' ')); do Q="$(echo $((16#$(grep -m${h} Data fileB.txt | tail -1 | cut -d ':' -f 2 | tr -d ' '))))" sed -i "s/\$$h/$Q/" fileA.txt done; cat fileA.txt
According to the docs, parameter-ized queries use the Extended Query protocol and has multiple parts:
Coming from MySQL-land, it seemed very strange to me that I can't tell what values are being passed to the database! After puzzling over it a while, I thought the values had to be communicated somehow and that would happen over the network, so I tried a packet capture and there they are! In my case, we aren't using stored procedures or prepared statements and we're on PosgreSQL version 12 and we don't use SSL (this makes it much easier to read the capture).
Upvotes: 2
Reputation: 5947
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.
Upvotes: 1
Reputation: 36719
I don't think it's possible. I have faced the same issue.
Upvotes: 12