 'stat_statements',
 9.4,
 $sql$
-with q_data as (
-  select
-    (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
-    queryid::text as tag_queryid,
-    max(ltrim(regexp_replace(query, E'[ \\t\\n\\r]+' , ' ', 'g')))::varchar(16000) as tag_query,
-    array_to_string(array_agg(distinct quote_ident(pg_get_userbyid(userid))), ',') as users,
-    sum(s.calls)::int8 as calls,
-    round(sum(s.total_time)::numeric, 3)::double precision as total_time,
-    sum(shared_blks_hit)::int8 as shared_blks_hit,
-    sum(shared_blks_read)::int8 as shared_blks_read,
-    sum(shared_blks_written)::int8 as shared_blks_written,
-    sum(shared_blks_dirtied)::int8 as shared_blks_dirtied,
-    sum(temp_blks_read)::int8 as temp_blks_read,
-    sum(temp_blks_written)::int8 as temp_blks_written,
-    round(sum(blk_read_time)::numeric, 3)::double precision as blk_read_time,
-    round(sum(blk_write_time)::numeric, 3)::double precision as blk_write_time
-  from
-    get_stat_statements() s
-  where
-    calls > 5
-    and total_time > 0
-    and dbid = (select oid from pg_database where datname = current_database())
-    and not upper(s.query) like any (array['DEALLOCATE%', 'SET %', 'RESET %', 'BEGIN%', 'BEGIN;',
-      'COMMIT%', 'END%', 'ROLLBACK%', 'SHOW%'])
-  group by
-    queryid
-)
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    total_time > 0
-  order by
-    total_time desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  order by
-    calls desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    shared_blks_read > 0
-  order by
-    shared_blks_read desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    shared_blks_written > 0
-  order by
-    shared_blks_written desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    temp_blks_read > 0
-  order by
-    temp_blks_read desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    temp_blks_written > 0
-  order by
-    temp_blks_written desc
-  limit 100
-) a;
-$sql$,
-$sql$
-with q_data as (
-  select
-    (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
-    queryid::text as tag_queryid,
-    max(ltrim(regexp_replace(query, E'[ \\t\\n\\r]+' , ' ', 'g')))::varchar(16000) as tag_query,
-    array_to_string(array_agg(distinct quote_ident(pg_get_userbyid(userid))), ',') as users,
-    sum(s.calls)::int8 as calls,
-    round(sum(s.total_time)::numeric, 3)::double precision as total_time,
-    sum(shared_blks_hit)::int8 as shared_blks_hit,
-    sum(shared_blks_read)::int8 as shared_blks_read,
-    sum(shared_blks_written)::int8 as shared_blks_written,
-    sum(shared_blks_dirtied)::int8 as shared_blks_dirtied,
-    sum(temp_blks_read)::int8 as temp_blks_read,
-    sum(temp_blks_written)::int8 as temp_blks_written,
-    round(sum(blk_read_time)::numeric, 3)::double precision as blk_read_time,
-    round(sum(blk_write_time)::numeric, 3)::double precision as blk_write_time
-  from
-    pg_stat_statements s
-  where
-    calls > 5
-    and total_time > 0
-    and dbid = (select oid from pg_database where datname = current_database())
-    and not upper(s.query) like any (array['DEALLOCATE%', 'SET %', 'RESET %', 'BEGIN%', 'BEGIN;',
-      'COMMIT%', 'END%', 'ROLLBACK%', 'SHOW%'])
-  group by
-    queryid
-)
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    total_time > 0
-  order by
-    total_time desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  order by
-    calls desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    shared_blks_read > 0
-  order by
-    shared_blks_read desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    shared_blks_written > 0
-  order by
-    shared_blks_written desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    temp_blks_read > 0
-  order by
-    temp_blks_read desc
-  limit 100
-) a
-union
-select * from (
-  select
-    *
-  from
-    q_data
-  where
-    temp_blks_written > 0
-  order by
-    temp_blks_written desc
-  limit 100
-) a;
+WITH q_data AS (
+    SELECT
+        (extract(epoch FROM now()) * 1e9)::int8 AS epoch_ns,
+        queryid::text AS tag_queryid,
+        /*
+         NB! if security conscious about exposing query texts replace the below expression with a dash ('-') OR
+         use the stat_statements_no_query_text metric instead, created specifically for this use case.
+         */
+        array_to_string(array_agg(DISTINCT quote_ident(pg_get_userbyid(userid))), ',') AS users,
+        sum(s.calls)::int8 AS calls,
+        round(sum(s.total_time)::numeric, 3)::double precision AS total_time,
+        sum(shared_blks_hit)::int8 AS shared_blks_hit,
+        sum(shared_blks_read)::int8 AS shared_blks_read,
+        sum(shared_blks_written)::int8 AS shared_blks_written,
+        sum(shared_blks_dirtied)::int8 AS shared_blks_dirtied,
+        sum(temp_blks_read)::int8 AS temp_blks_read,
+        sum(temp_blks_written)::int8 AS temp_blks_written,
+        round(sum(blk_read_time)::numeric, 3)::double precision AS blk_read_time,
+        round(sum(blk_write_time)::numeric, 3)::double precision AS blk_write_time
+    FROM
+        get_stat_statements() s
+    WHERE
+        calls > 5
+        AND total_time > 5
+        AND dbid = (
+            SELECT
+                oid
+            FROM
+                pg_database
+            WHERE
+                datname = current_database())
+            AND NOT upper(s.query::varchar(50))
+            LIKE ANY (ARRAY['DEALLOCATE%',
+                'SET %',
+                'RESET %',
+                'BEGIN%',
+                'BEGIN;',
+                'COMMIT%',
+                'END%',
+                'ROLLBACK%',
+                'SHOW%'])
+        GROUP BY
+            queryid
+),
+q_queryid_text AS (
+    SELECT
+        queryid::text,
+        query::varchar(8000)
+    FROM
+        get_stat_statements()
+    WHERE
+        dbid = (
+            SELECT
+                oid
+            FROM
+                pg_database
+            WHERE
+                datname = current_database()))
+SELECT
+    b.*,
+    (
+        SELECT
+            ltrim(regexp_replace(query, E'[ \\t\\n\\r]+', ' ', 'g'))
+        FROM
+            q_queryid_text
+        WHERE
+            q_queryid_text.queryid = b.tag_queryid
+        LIMIT 1) AS tag_query
+FROM (
+    SELECT
+        *
+    FROM (
+        SELECT
+            *
+        FROM
+            q_data
+        WHERE
+            total_time > 0
+        ORDER BY
+            total_time DESC
+        LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    ORDER BY
+        calls DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        shared_blks_read > 0
+    ORDER BY
+        shared_blks_read DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        shared_blks_written > 0
+    ORDER BY
+        shared_blks_written DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        temp_blks_read > 0
+    ORDER BY
+        temp_blks_read DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        temp_blks_written > 0
+    ORDER BY
+        temp_blks_written DESC
+    LIMIT 100) a) b;
+$sql$,
+$sql$
+WITH q_data AS (
+    SELECT
+        (extract(epoch FROM now()) * 1e9)::int8 AS epoch_ns,
+        queryid::text AS tag_queryid,
+        /*
+         NB! if security conscious about exposing query texts replace the below expression with a dash ('-') OR
+         use the stat_statements_no_query_text metric instead, created specifically for this use case.
+         */
+        array_to_string(array_agg(DISTINCT quote_ident(pg_get_userbyid(userid))), ',') AS users,
+        sum(s.calls)::int8 AS calls,
+        round(sum(s.total_time)::numeric, 3)::double precision AS total_time,
+        sum(shared_blks_hit)::int8 AS shared_blks_hit,
+        sum(shared_blks_read)::int8 AS shared_blks_read,
+        sum(shared_blks_written)::int8 AS shared_blks_written,
+        sum(shared_blks_dirtied)::int8 AS shared_blks_dirtied,
+        sum(temp_blks_read)::int8 AS temp_blks_read,
+        sum(temp_blks_written)::int8 AS temp_blks_written,
+        round(sum(blk_read_time)::numeric, 3)::double precision AS blk_read_time,
+        round(sum(blk_write_time)::numeric, 3)::double precision AS blk_write_time
+    FROM
+        pg_stat_statements s
+    WHERE
+        calls > 5
+        AND total_time > 5
+        AND dbid = (
+            SELECT
+                oid
+            FROM
+                pg_database
+            WHERE
+                datname = current_database())
+            AND NOT upper(s.query::varchar(50))
+            LIKE ANY (ARRAY['DEALLOCATE%',
+                'SET %',
+                'RESET %',
+                'BEGIN%',
+                'BEGIN;',
+                'COMMIT%',
+                'END%',
+                'ROLLBACK%',
+                'SHOW%'])
+        GROUP BY
+            queryid
+),
+q_queryid_text AS (
+    SELECT
+        queryid::text,
+        query::varchar(8000)
+    FROM
+        pg_stat_statements
+    WHERE
+        dbid = (
+            SELECT
+                oid
+            FROM
+                pg_database
+            WHERE
+                datname = current_database()))
+SELECT
+    b.*,
+    (
+        SELECT
+            ltrim(regexp_replace(query, E'[ \\t\\n\\r]+', ' ', 'g'))
+        FROM
+            q_queryid_text
+        WHERE
+            q_queryid_text.queryid = b.tag_queryid
+        LIMIT 1) AS tag_query
+FROM (
+    SELECT
+        *
+    FROM (
+        SELECT
+            *
+        FROM
+            q_data
+        WHERE
+            total_time > 0
+        ORDER BY
+            total_time DESC
+        LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    ORDER BY
+        calls DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        shared_blks_read > 0
+    ORDER BY
+        shared_blks_read DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        shared_blks_written > 0
+    ORDER BY
+        shared_blks_written DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        temp_blks_read > 0
+    ORDER BY
+        temp_blks_read DESC
+    LIMIT 100) a
+UNION
+SELECT
+    *
+FROM (
+    SELECT
+        *
+    FROM
+        q_data
+    WHERE
+        temp_blks_written > 0
+    ORDER BY
+        temp_blks_written DESC
+    LIMIT 100) a) b;
 $sql$
 );
 

'stat_statements',
9.2,
$sql$
with q_data as (
  select
    (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
    (regexp_replace(md5(query), E'\\D', '', 'g'))::varchar(10)::int8 as tag_queryid,
    max(ltrim(regexp_replace(query, E'[ \\t\\n\\r]+' , ' ', 'g')))::varchar(16000) as tag_query,
    array_to_string(array_agg(distinct quote_ident(pg_get_userbyid(userid))), ',') as users,
    sum(s.calls)::int8 as calls,
    round(sum(s.total_time)::numeric, 3)::double precision as total_time,
    sum(shared_blks_hit)::int8 as shared_blks_hit,
    sum(shared_blks_read)::int8 as shared_blks_read,
    sum(shared_blks_written)::int8 as shared_blks_written,
    sum(shared_blks_dirtied)::int8 as shared_blks_dirtied,
    sum(temp_blks_read)::int8 as temp_blks_read,
    sum(temp_blks_written)::int8 as temp_blks_written,
    round(sum(blk_read_time)::numeric, 3)::double precision as blk_read_time,
    round(sum(blk_write_time)::numeric, 3)::double precision as blk_write_time
  from
    get_stat_statements() s
  where
    calls > 5
    and total_time > 0
    and dbid = (select oid from pg_database where datname = current_database())
    and not upper(s.query) like any (array['DEALLOCATE%', 'SET %', 'RESET %', 'BEGIN%', 'BEGIN;',
      'COMMIT%', 'END%', 'ROLLBACK%', 'SHOW%'])
  group by
    tag_queryid
)
select * from (
  select
    *
  from
    q_data
  where
    total_time > 0
  order by
    total_time desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  order by
    calls desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    shared_blks_read > 0
  order by
    shared_blks_read desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    shared_blks_written > 0
  order by
    shared_blks_written desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    temp_blks_read > 0
  order by
    temp_blks_read desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    temp_blks_written > 0
  order by
    temp_blks_written desc
  limit 100
) a;
$sql$,
$sql$
with q_data as (
  select
    (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
    (regexp_replace(md5(query), E'\\D', '', 'g'))::varchar(10)::int8 as tag_queryid,
    max(ltrim(regexp_replace(query, E'[ \\t\\n\\r]+' , ' ', 'g')))::varchar(16000) as tag_query,
    array_to_string(array_agg(distinct quote_ident(pg_get_userbyid(userid))), ',') as users,
    sum(s.calls)::int8 as calls,
    round(sum(s.total_time)::numeric, 3)::double precision as total_time,
    sum(shared_blks_hit)::int8 as shared_blks_hit,
    sum(shared_blks_read)::int8 as shared_blks_read,
    sum(shared_blks_written)::int8 as shared_blks_written,
    sum(shared_blks_dirtied)::int8 as shared_blks_dirtied,
    sum(temp_blks_read)::int8 as temp_blks_read,
    sum(temp_blks_written)::int8 as temp_blks_written,
    round(sum(blk_read_time)::numeric, 3)::double precision as blk_read_time,
    round(sum(blk_write_time)::numeric, 3)::double precision as blk_write_time
  from
    pg_stat_statements s
  where
    calls > 5
    and total_time > 0
    and dbid = (select oid from pg_database where datname = current_database())
    and not upper(s.query) like any (array['DEALLOCATE%', 'SET %', 'RESET %', 'BEGIN%', 'BEGIN;',
      'COMMIT%', 'END%', 'ROLLBACK%', 'SHOW%'])
  group by
    tag_queryid
)
select * from (
  select
    *
  from
    q_data
  where
    total_time > 0
  order by
    total_time desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  order by
    calls desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    shared_blks_read > 0
  order by
    shared_blks_read desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    shared_blks_written > 0
  order by
    shared_blks_written desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    temp_blks_read > 0
  order by
    temp_blks_read desc
  limit 100
) a
union
select * from (
  select
    *
  from
    q_data
  where
    temp_blks_written > 0
  order by
    temp_blks_written desc
  limit 100
) a;
$sql$
);


'stat_statements',
13,
$sql$
WITH q_data AS (
    SELECT
        (extract(epoch FROM now()) * 1e9)::int8 AS epoch_ns,
        queryid::text AS tag_queryid,
        /*
         NB! if security conscious about exposing query texts replace the below expression with a dash ('-') OR
         use the stat_statements_no_query_text metric instead, created specifically for this use case.
         */
        array_to_string(array_agg(DISTINCT quote_ident(pg_get_userbyid(userid))), ',') AS users,
        sum(s.calls)::int8 AS calls,
        round(sum(s.total_exec_time)::numeric, 3)::double precision AS total_time,
        sum(shared_blks_hit)::int8 AS shared_blks_hit,
        sum(shared_blks_read)::int8 AS shared_blks_read,
        sum(shared_blks_written)::int8 AS shared_blks_written,
        sum(shared_blks_dirtied)::int8 AS shared_blks_dirtied,
        sum(temp_blks_read)::int8 AS temp_blks_read,
        sum(temp_blks_written)::int8 AS temp_blks_written,
        round(sum(blk_read_time)::numeric, 3)::double precision AS blk_read_time,
        round(sum(blk_write_time)::numeric, 3)::double precision AS blk_write_time,
        sum(wal_fpi)::int8 AS wal_fpi,
        sum(wal_bytes)::int8 AS wal_bytes,
        round(sum(s.total_plan_time)::numeric, 3)::double precision AS total_plan_time
    FROM
        get_stat_statements() s
    WHERE
        calls > 5
        AND total_exec_time > 5
        AND dbid = (
            SELECT
                oid
            FROM
                pg_database
            WHERE
                datname = current_database())
            AND NOT upper(s.query::varchar(50))
            LIKE ANY (ARRAY['DEALLOCATE%',
                'SET %',
                'RESET %',
                'BEGIN%',
                'BEGIN;',
                'COMMIT%',
                'END%',
                'ROLLBACK%',
                'SHOW%'])
        GROUP BY
            queryid
),
q_queryid_text AS (
    SELECT
        queryid::text,
        query::varchar(8000)
    FROM
        get_stat_statements()
    WHERE
        dbid = (
            SELECT
                oid
            FROM
                pg_database
            WHERE
                datname = current_database()))
SELECT
    b.*,
    (
        SELECT
            ltrim(regexp_replace(query, E'[ \\t\\n\\r]+', ' ', 'g'))
        FROM
            q_queryid_text
        WHERE
            q_queryid_text.queryid = b.tag_queryid
        LIMIT 1) AS tag_query
FROM (
    SELECT
        *
    FROM (
        SELECT
            *
        FROM
            q_data
        WHERE
            total_time > 0
        ORDER BY
            total_time DESC
        LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    ORDER BY
        calls DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        shared_blks_read > 0
    ORDER BY
        shared_blks_read DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        shared_blks_written > 0
    ORDER BY
        shared_blks_written DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        temp_blks_read > 0
    ORDER BY
        temp_blks_read DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        temp_blks_written > 0
    ORDER BY
        temp_blks_written DESC
    LIMIT 100) a) b;
$sql$,
$sql$
WITH q_data AS (
    SELECT
        (extract(epoch FROM now()) * 1e9)::int8 AS epoch_ns,
        queryid::text AS tag_queryid,
        /*
         NB! if security conscious about exposing query texts replace the below expression with a dash ('-') OR
         use the stat_statements_no_query_text metric instead, created specifically for this use case.
         */
        array_to_string(array_agg(DISTINCT quote_ident(pg_get_userbyid(userid))), ',') AS users,
        sum(s.calls)::int8 AS calls,
        round(sum(s.total_exec_time)::numeric, 3)::double precision AS total_time,
        sum(shared_blks_hit)::int8 AS shared_blks_hit,
        sum(shared_blks_read)::int8 AS shared_blks_read,
        sum(shared_blks_written)::int8 AS shared_blks_written,
        sum(shared_blks_dirtied)::int8 AS shared_blks_dirtied,
        sum(temp_blks_read)::int8 AS temp_blks_read,
        sum(temp_blks_written)::int8 AS temp_blks_written,
        round(sum(blk_read_time)::numeric, 3)::double precision AS blk_read_time,
        round(sum(blk_write_time)::numeric, 3)::double precision AS blk_write_time,
        sum(wal_fpi)::int8 AS wal_fpi,
        sum(wal_bytes)::int8 AS wal_bytes,
        round(sum(s.total_plan_time)::numeric, 3)::double precision AS total_plan_time
    FROM
        pg_stat_statements s
    WHERE
        calls > 5
        AND total_exec_time > 5
        AND dbid = (
            SELECT
                oid
            FROM
                pg_database
            WHERE
                datname = current_database())
            AND NOT upper(s.query::varchar(50))
            LIKE ANY (ARRAY['DEALLOCATE%',
                'SET %',
                'RESET %',
                'BEGIN%',
                'BEGIN;',
                'COMMIT%',
                'END%',
                'ROLLBACK%',
                'SHOW%'])
        GROUP BY
            queryid
),
q_queryid_text AS (
    SELECT
        queryid::text,
        query::varchar(8000)
    FROM
        pg_stat_statements
    WHERE
        dbid = (
            SELECT
                oid
            FROM
                pg_database
            WHERE
                datname = current_database()))
SELECT
    b.*,
    (
        SELECT
            ltrim(regexp_replace(query, E'[ \\t\\n\\r]+', ' ', 'g'))
        FROM
            q_queryid_text
        WHERE
            q_queryid_text.queryid = b.tag_queryid
        LIMIT 1) AS tag_query
FROM (
    SELECT
        *
    FROM (
        SELECT
            *
        FROM
            q_data
        WHERE
            total_time > 0
        ORDER BY
            total_time DESC
        LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    ORDER BY
        calls DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        shared_blks_read > 0
    ORDER BY
        shared_blks_read DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        shared_blks_written > 0
    ORDER BY
        shared_blks_written DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        temp_blks_read > 0
    ORDER BY
        temp_blks_read DESC
    LIMIT 100) a
UNION
SELECT
    *
FROM (
    SELECT
        *
    FROM
        q_data
    WHERE
        temp_blks_written > 0
    ORDER BY
        temp_blks_written DESC
    LIMIT 100) a) b;
$sql$
);

