Display individual query in pg_stat_activity

From: "Drouvot, Bertrand" <bdrouvot(at)amazon(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Cc: "Schneider (AWS), Jeremy" <schnjere(at)amazon(dot)com>
Subject: Display individual query in pg_stat_activity
Date: 2020-07-27 07:36:59
Message-ID: 030a4123-550a-9dc1-d326-3cd5c46bcc59@amazon.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I've attached a patch to display individual query in the
pg_stat_activity query field when multiple SQL statements are currently
displayed.

_Motivation:_

When multiple statements are displayed then we don’t know which one is
currently running.

For example:

psql -c "select pg_sleep(10);select pg_sleep(20);" is currently
displayed as:

postgres=# select backend_type,query from pg_stat_activity;
backend_type | query
------------------------------+--------------------------------------------------
client backend | select pg_sleep(10);select pg_sleep(20);

Showing which statement is currently being executed would be more helpful.

_Technical context and proposal:_

There is 2 points in this patch:

* modifying the current behavior in “exec_simple_query”
* modifying the current behavior in “ExecInitParallelPlan”

So that we could see for example:

backend_type | query
------------------------------+--------------------------------------------------
client backend | select pg_sleep(10);

and then

backend_type | query
------------------------------+--------------------------------------------------
client backend | select pg_sleep(20);

instead of the multiple sql statement described in the “motivation” section.

Another example: parallel worker being triggered while executing a function:

create or replace function test()
returns void as $$select count(/) as "first" from foo;select pg_sleep(10);select count(/) as "second" from foo;select pg_sleep(11);select pg_sleep(10)
$$
language sql;

We currently see:

backend_type | query
------------------------------+--------------------------------------------------------------------------------------------------------------------------------------
client backend | select test();
parallel worker | select count(*) as "first" from foo;select pg_sleep(10);select count(*) as "second" from foo;select pg_sleep(11);select pg_sleep(10)+
|
parallel worker | select count(*) as "first" from foo;select pg_sleep(10);select count(*) as "second" from foo;select pg_sleep(11);select pg_sleep(10)+
|

while the attached patch would provide:


backend_type | query [217/1938]
------------------------------+--------------------------------------------------
client backend | select test();
parallel worker | select count(*) as "first" from foo;
parallel worker | select count(*) as "first" from foo;

and then:

backend_type | query
------------------------------+--------------------------------------------------
client backend | select test();
parallel worker | select count(*) as "second" from foo;
parallel worker | select count(*) as "second" from foo;

I will add this patch to the next commitfest. I look forward to your
feedback about the idea and/or implementation.

Regards,
Bertrand

Attachment Content-Type Size
v1-0001-pg_stat_activity_individual_query.patch text/plain 3.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Mahendra Singh Thalor 2020-07-27 07:45:09 Re: display offset along with block number in vacuum errors
Previous Message Masahiko Sawada 2020-07-27 07:34:34 Re: display offset along with block number in vacuum errors