Skip to content

Commit

Permalink
Fix powa_statements_src()
Browse files Browse the repository at this point in the history
The newly added toplevel field has to be put at the end of the various tables,
to ensure an identical layout whether you install or upgrade the extension.
Unfortunately this was missed in powa_statements_src() leading to failure
during snapshot.

Thanks to Thomas Reiss for the report.
  • Loading branch information
rjuju committed Nov 10, 2023
1 parent 77ab13f commit d61e829
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 4.2.2 (WIP)

- Bugfixes
- Fix the toplevel field position in powa_statements_src function (Julien
Rouhaud, thanks to Thomas Reiss for the report)

## 4.2.1

- Bugfixes
Expand Down
117 changes: 115 additions & 2 deletions powa--4.2.1--4.2.2.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,118 @@
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "ALTER EXTENSION powa" to load this file. \quit

-- Nothing to do, 4.2.0 and 4.2.1 are identical, the new version was only
-- needed to fix a bug with the 4.1.4 <-> 4.2.0 upgrade script.
DROP FUNCTION powa_statements_src(integer);
CREATE OR REPLACE FUNCTION powa_statements_src(IN _srvid integer,
OUT ts timestamp with time zone,
OUT userid oid,
OUT dbid oid,
OUT queryid bigint,
OUT query text,
OUT calls bigint,
OUT total_exec_time double precision,
OUT rows bigint,
OUT shared_blks_hit bigint,
OUT shared_blks_read bigint,
OUT shared_blks_dirtied bigint,
OUT shared_blks_written bigint,
OUT local_blks_hit bigint,
OUT local_blks_read bigint,
OUT local_blks_dirtied bigint,
OUT local_blks_written bigint,
OUT temp_blks_read bigint,
OUT temp_blks_written bigint,
OUT blk_read_time double precision,
OUT blk_write_time double precision,
OUT plans bigint,
OUT total_plan_time float8,
OUT wal_records bigint,
OUT wal_fpi bigint,
OUT wal_bytes numeric,
OUT toplevel boolean
)
RETURNS SETOF record
STABLE
AS $PROC$
DECLARE
v_pgss integer[];
BEGIN
IF (_srvid = 0) THEN
SELECT regexp_split_to_array(extversion, E'\\.') INTO STRICT v_pgss
FROM pg_extension
WHERE extname = 'pg_stat_statements';

IF (v_pgss[1] = 1 AND v_pgss[2] >= 10) THEN
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes, pgss.toplevel
FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
WHERE pgss.query !~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)'
AND NOT (r.rolname = ANY (string_to_array(
powa_get_guc('powa.ignored_users', ''),
',')));
ELSIF (v_pgss[1] = 1 AND v_pgss[2] >= 8) THEN
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes, true::boolean
FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
WHERE pgss.query !~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)'
AND NOT (r.rolname = ANY (string_to_array(
powa_get_guc('powa.ignored_users', ''),
',')));
ELSE
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time,pgss.blk_write_time,
0::bigint, 0::double precision,
0::bigint, 0::bigint, 0::numeric, true::boolean

FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
WHERE pgss.query !~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)'
AND NOT (r.rolname = ANY (string_to_array(
powa_get_guc('powa.ignored_users', ''),
',')));
END IF;
ELSE
RETURN QUERY SELECT pgss.ts,
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes, pgss.toplevel
FROM powa_statements_src_tmp pgss WHERE srvid = _srvid;
END IF;
END;
$PROC$ LANGUAGE plpgsql; /* end of powa_statements_src */
20 changes: 10 additions & 10 deletions powa--4.2.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2420,7 +2420,6 @@ CREATE OR REPLACE FUNCTION powa_statements_src(IN _srvid integer,
OUT ts timestamp with time zone,
OUT userid oid,
OUT dbid oid,
OUT toplevel boolean,
OUT queryid bigint,
OUT query text,
OUT calls bigint,
Expand All @@ -2442,7 +2441,8 @@ CREATE OR REPLACE FUNCTION powa_statements_src(IN _srvid integer,
OUT total_plan_time float8,
OUT wal_records bigint,
OUT wal_fpi bigint,
OUT wal_bytes numeric
OUT wal_bytes numeric,
OUT toplevel boolean
)
RETURNS SETOF record
STABLE
Expand All @@ -2457,7 +2457,7 @@ BEGIN

IF (v_pgss[1] = 1 AND v_pgss[2] >= 10) THEN
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, pgss.toplevel, pgss.queryid, pgss.query,
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
Expand All @@ -2466,7 +2466,7 @@ BEGIN
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes, pgss.toplevel
FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
Expand All @@ -2476,7 +2476,7 @@ BEGIN
',')));
ELSIF (v_pgss[1] = 1 AND v_pgss[2] >= 8) THEN
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, true::boolean, pgss.queryid, pgss.query,
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
Expand All @@ -2485,7 +2485,7 @@ BEGIN
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes, true::boolean
FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
Expand All @@ -2495,7 +2495,7 @@ BEGIN
',')));
ELSE
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, true::boolean, pgss.queryid, pgss.query,
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
Expand All @@ -2504,7 +2504,7 @@ BEGIN
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time,pgss.blk_write_time,
0::bigint, 0::double precision,
0::bigint, 0::bigint, 0::numeric
0::bigint, 0::bigint, 0::numeric, true::boolean

FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
Expand All @@ -2516,7 +2516,7 @@ BEGIN
END IF;
ELSE
RETURN QUERY SELECT pgss.ts,
pgss.userid, pgss.dbid, pgss.toplevel, pgss.queryid, pgss.query,
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
Expand All @@ -2525,7 +2525,7 @@ BEGIN
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes, pgss.toplevel
FROM powa_statements_src_tmp pgss WHERE srvid = _srvid;
END IF;
END;
Expand Down

0 comments on commit d61e829

Please sign in to comment.