From cb73f199c00ea96006c4aad3fe6020ee5e459ec2 Mon Sep 17 00:00:00 2001 From: VigneshViggu Date: Mon, 11 Mar 2024 20:29:38 +0530 Subject: [PATCH] Update the PLPGSQL queries to resolve duplicate queries Update the PLPGSQL queries to resolve duplicate queries Signed-off-by: VigneshViggu --- collector/pg_stat_statements.go | 76 ++++++++++++++++----------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/collector/pg_stat_statements.go b/collector/pg_stat_statements.go index c03e78b92..7af5ae153 100644 --- a/collector/pg_stat_statements.go +++ b/collector/pg_stat_statements.go @@ -71,47 +71,43 @@ var ( prometheus.Labels{}, ) - pgStatStatementsQuery = `SELECT - pg_get_userbyid(userid) as user, - pg_database.datname, - pg_stat_statements.queryid, - pg_stat_statements.calls as calls_total, - pg_stat_statements.total_time / 1000.0 as seconds_total, - pg_stat_statements.rows as rows_total, - pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total, - pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total - FROM pg_stat_statements - JOIN pg_database - ON pg_database.oid = pg_stat_statements.dbid - WHERE - total_time > ( - SELECT percentile_cont(0.1) - WITHIN GROUP (ORDER BY total_time) - FROM pg_stat_statements + pgStatStatementsQuery = `WITH percentiles AS ( + SELECT percentile_cont(0.1) WITHIN GROUP (ORDER BY total_time) AS percentile + FROM pg_stat_statements ) - ORDER BY seconds_total DESC - LIMIT 100;` - - pgStatStatementsNewQuery = `SELECT - pg_get_userbyid(userid) as user, - pg_database.datname, - pg_stat_statements.queryid, - pg_stat_statements.calls as calls_total, - pg_stat_statements.total_exec_time / 1000.0 as seconds_total, - pg_stat_statements.rows as rows_total, - pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total, - pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total - FROM pg_stat_statements - JOIN pg_database - ON pg_database.oid = pg_stat_statements.dbid - WHERE - total_exec_time > ( - SELECT percentile_cont(0.1) - WITHIN GROUP (ORDER BY total_exec_time) - FROM pg_stat_statements - ) - ORDER BY seconds_total DESC - LIMIT 100;` + SELECT DISTINCT ON (pss.queryid, pg_get_userbyid(pss.userid), pg_database.datname) + pg_get_userbyid(pss.userid) as user, + pg_database.datname, + pss.queryid, + pss.calls as calls_total, + pss.total_time / 1000.0 as seconds_total, + pss.rows as rows_total, + pss.blk_read_time / 1000.0 as block_read_seconds_total, + pss.blk_write_time / 1000.0 as block_write_seconds_total + FROM pg_stat_statements pss + JOIN pg_database ON pg_database.oid = pss.dbid + CROSS JOIN percentiles + WHERE pss.total_time > (SELECT percentile FROM percentiles) + ORDER BY pss.queryid, pg_get_userbyid(pss.userid) DESC, pg_database.datname + LIMIT 100;` + + pgStatStatementsNewQuery = `SELECT DISTINCT ON (pss.queryid, pg_get_userbyid(pss.userid), pg_database.datname) + pg_get_userbyid(pss.userid) AS user, + pg_database.datname AS database_name, + pss.queryid, + pss.calls AS calls_total, + pss.total_exec_time / 1000.0 AS seconds_total, + pss.rows AS rows_total, + pss.blk_read_time / 1000.0 AS block_read_seconds_total, + pss.blk_write_time / 1000.0 AS block_write_seconds_total + FROM pg_stat_statements pss + JOIN pg_database ON pg_database.oid = pss.dbid + JOIN ( + SELECT percentile_cont(0.1) WITHIN GROUP (ORDER BY total_exec_time) AS percentile_val + FROM pg_stat_statements + ) AS perc ON pss.total_exec_time > perc.percentile_val + ORDER BY pss.queryid, pg_get_userbyid(pss.userid) DESC, pg_database.datname + LIMIT 100;` ) func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {