Daily Shaarli

All links of one day in a single page.

December 9, 2021

Quick 📸 on 🐘 active SQL from pg_stat_statements - DEV Community
thumbnail
with 
b as ( -- begin snapshot
select clock_timestamp() as "snap_time",* 
 from pg_stat_statements , pg_sleep(30) --> 30 second snapshots
),
e as ( -- end snapshot
 select clock_timestamp() as "snap_time",* 
 from pg_stat_statements , pg_sleep(0) 
),
u as ( -- union all as timeseries
 select * from e union all select * from b
),
d as ( -- delta values from cumulative metrics
 select
  snap_time
  ,extract( epoch from snap_time-lag(snap_time)
   over(query_cumulative_stats)) as seconds
  ,total_time-lag(total_time)
   over(query_cumulative_stats ) as total_time
  ,calls-lag(calls)
   over(query_cumulative_stats ) as calls
  ,query
 from u 
 window query_cumulative_stats as (
  partition by userid,dbid,queryid,query order by snap_time
  )
) select -- final output
  round((calls/seconds)::numeric,2) as "call/sec",
  round(((total_time/1000)/seconds)::numeric,1) as "AAS",
  date_trunc('second',(total_time/1000)* interval '1 second')
   as total_time,
  calls as "total calls", query 
 from d where calls>0 and total_time >0
 order by "AAS" desc fetch first 15 rows only;