Thread: query execution time for last 24 hours in PostgreSQL

query execution time for last 24 hours in PostgreSQL

From
nikhil kumar
Date:
Hi Team,

Can you please let me know anyone how to get each query execution time from last 24 hours in PostgreSQL 

I tried the below query but giving the wrong information.

SELECT sa.client_addr,
sa.usename,
sa.datname,
sa.state,
sa.query,
sa.query_start,
EXTRACT (EPOCH FROM (current_timestamp sa.query_start)) AS execution_time_seconds
FROM pg_stat_activity sa
WHERE sa.query_start >= current_timestamp interval '24 hours';

Thanks,
Nikhil,
PostgreSQL DBA,
8074430856.
Hello,

you can get this infos with pg_stat_statement extension.


nikhil kumar <nikhil.dba97@gmail.com>, 23 May 2024 Per, 21:08 tarihinde şunu yazdı:
Hi Team,

Can you please let me know anyone how to get each query execution time from last 24 hours in PostgreSQL 

I tried the below query but giving the wrong information.

SELECT sa.client_addr,
sa.usename,
sa.datname,
sa.state,
sa.query,
sa.query_start,
EXTRACT (EPOCH FROM (current_timestamp sa.query_start)) AS execution_time_seconds
FROM pg_stat_activity sa
WHERE sa.query_start >= current_timestamp interval '24 hours';

Thanks,
Nikhil,
PostgreSQL DBA,
8074430856.

Re: query execution time for last 24 hours in PostgreSQL

From
Holger Jakobs
Date:
Am 23.05.24 um 20:07 schrieb nikhil kumar:
> Hi Team,
>
> Can you please let me know anyone how to get each query execution time 
> from last 24 hours in PostgreSQL
>
> I tried the below query but giving the wrong information.
>
> SELECT sa.client_addr,
> sa.usename,
> sa.datname,
> sa.state,
> sa.query,
> sa.query_start,
> EXTRACT (EPOCH FROM (current_timestamp sa.query_start)) AS 
> execution_time_seconds
> FROM pg_stat_activity sa
> WHERE sa.query_start >= current_timestamp interval '24 hours';
>
> Thanks,
> Nikhil,
> PostgreSQL DBA,
> 8074430856.

pg_stat_activity shows current activity, not historic activity.

Unless you use an extension like pg_stat_statements, or log your 
statements, the information won't be available.

Regards,
Holger

-- 

Holger Jakobs, Bergisch Gladbach


Attachment

Re: query execution time for last 24 hours in PostgreSQL

From
Tom Lane
Date:
Holger Jakobs <holger@jakobs.com> writes:
> Am 23.05.24 um 20:07 schrieb nikhil kumar:
>> Can you please let me know anyone how to get each query execution time 
>> from last 24 hours in PostgreSQL

> pg_stat_activity shows current activity, not historic activity.
> Unless you use an extension like pg_stat_statements, or log your 
> statements, the information won't be available.

pg_stat_statements only collects cumulative runtime.  The OP seems
to be asking for the runtime of each execution of each statement.
The only way to get that is by logging the durations (see log_duration
and related settings) and then scraping the postmaster log.  You
might want to use pgbadger or a similar tool to help with the scraping
and subsequent data analysis.

            regards, tom lane