/usr/share/postgresql/9.6/extension/powa--3.0.0--3.0.1.sql is in postgresql-9.6-powa 3.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | -- complain if script is sourced in psql, rather than via CREATE EXTENSION
--\echo Use "ALTER EXTENSION powa" to load this file. \quit
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET search_path = public, pg_catalog;
CREATE TABLE powa_databases(
oid oid PRIMARY KEY,
datname name,
dropped timestamp with time zone
);
CREATE OR REPLACE FUNCTION powa_statements_snapshot() RETURNS void AS $PROC$
DECLARE
result boolean;
ignore_regexp text:='^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)';
BEGIN
-- In this function, we capture statements, and also aggregate counters by database
-- so that the first screens of powa stay reactive even though there may be thousands
-- of different statements
RAISE DEBUG 'running powa_statements_snapshot';
WITH capture AS(
SELECT pgss.*
FROM pg_stat_statements pgss
JOIN pg_roles r ON pgss.userid = r.oid
WHERE pgss.query !~* ignore_regexp
AND NOT (r.rolname = ANY (string_to_array(current_setting('powa.ignored_users'),',')))
),
missing_statements AS(
INSERT INTO powa_statements (queryid, dbid, userid, query)
SELECT queryid, dbid, userid, query
FROM capture c
WHERE NOT EXISTS (SELECT 1
FROM powa_statements ps
WHERE ps.queryid = c.queryid
AND ps.dbid = c.dbid
AND ps.userid = c.userid
)
),
by_query AS (
INSERT INTO powa_statements_history_current
SELECT queryid, dbid, userid,
ROW(
now(), calls, total_time, rows, shared_blks_hit, shared_blks_read,
shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read,
local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written,
blk_read_time, blk_write_time
)::powa_statements_history_record AS record
FROM capture
),
by_database AS (
INSERT INTO powa_statements_history_current_db
SELECT dbid,
ROW(
now(), sum(calls), sum(total_time), sum(rows), sum(shared_blks_hit), sum(shared_blks_read),
sum(shared_blks_dirtied), sum(shared_blks_written), sum(local_blks_hit), sum(local_blks_read),
sum(local_blks_dirtied), sum(local_blks_written), sum(temp_blks_read), sum(temp_blks_written),
sum(blk_read_time), sum(blk_write_time)
)::powa_statements_history_record AS record
FROM capture
GROUP BY dbid
)
SELECT true::boolean INTO result; -- For now we don't care. What could we do on error except crash anyway?
END;
$PROC$ language plpgsql;
CREATE OR REPLACE FUNCTION powa_take_snapshot() RETURNS void AS $PROC$
DECLARE
purgets timestamp with time zone;
purge_seq bigint;
funcname text;
v_state text;
v_msg text;
v_detail text;
v_hint text;
v_context text;
BEGIN
-- Keep track of existing databases
WITH missing AS (
SELECT d.oid, d.datname
FROM pg_database d
LEFT JOIN powa_databases p ON d.oid = p.oid
WHERE p.oid IS NULL
)
INSERT INTO powa_databases
SELECT * FROM missing;
-- Keep track of renamed databases
WITH renamed AS (
SELECT d.oid, d.datname
FROM pg_database AS d
JOIN powa_databases AS p ON d.oid = p.oid
WHERE d.datname != p.datname
)
UPDATE powa_databases AS p
SET datname = r.datname
FROM renamed AS r
WHERE p.oid = r.oid;
-- Keep track of when databases are dropped
WITH dropped AS (
SELECT p.oid
FROM powa_databases p
LEFT JOIN pg_database d ON p.oid = d.oid
WHERE d.oid IS NULL
AND p.dropped IS NULL)
UPDATE powa_databases p
SET dropped = now()
FROM dropped d
WHERE p.oid = d.oid;
-- For all enabled snapshot functions in the powa_functions table, execute
FOR funcname IN SELECT function_name
FROM powa_functions
WHERE operation='snapshot' AND enabled LOOP
-- Call all of them, with no parameter
RAISE debug 'fonction: %',funcname;
BEGIN
EXECUTE 'SELECT ' || quote_ident(funcname)||'()';
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS
v_state = RETURNED_SQLSTATE,
v_msg = MESSAGE_TEXT,
v_detail = PG_EXCEPTION_DETAIL,
v_hint = PG_EXCEPTION_HINT,
v_context = PG_EXCEPTION_CONTEXT;
RAISE warning 'powa_take_snapshot(): function "%" failed:
state : %
message: %
detail : %
hint : %
context: %', funcname, v_state, v_msg, v_detail, v_hint, v_context;
END;
END LOOP;
-- Coalesce datas if needed
SELECT nextval('powa_coalesce_sequence'::regclass) INTO purge_seq;
IF ( purge_seq
% current_setting('powa.coalesce')::bigint ) = 0
THEN
FOR funcname IN SELECT function_name
FROM powa_functions
WHERE operation='aggregate' AND enabled LOOP
-- Call all of them, with no parameter
BEGIN
EXECUTE 'SELECT ' || quote_ident(funcname)||'()';
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS
v_state = RETURNED_SQLSTATE,
v_msg = MESSAGE_TEXT,
v_detail = PG_EXCEPTION_DETAIL,
v_hint = PG_EXCEPTION_HINT,
v_context = PG_EXCEPTION_CONTEXT;
RAISE warning 'powa_take_snapshot(): function "%" failed:
state : %
message: %
detail : %
hint : %
context: %', funcname, v_state, v_msg, v_detail, v_hint, v_context;
END;
END LOOP;
UPDATE powa_last_aggregation SET aggts = now();
END IF;
-- Once every 10 packs, we also purge
IF ( purge_seq
% (current_setting('powa.coalesce')::bigint *10) ) = 0
THEN
FOR funcname IN SELECT function_name
FROM powa_functions
WHERE operation='purge' AND enabled LOOP
-- Call all of them, with no parameter
BEGIN
EXECUTE 'SELECT ' || quote_ident(funcname)||'()';
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS
v_state = RETURNED_SQLSTATE,
v_msg = MESSAGE_TEXT,
v_detail = PG_EXCEPTION_DETAIL,
v_hint = PG_EXCEPTION_HINT,
v_context = PG_EXCEPTION_CONTEXT;
RAISE warning 'powa_take_snapshot(): function "%" failed:
state : %
message: %
detail : %
hint : %
context: %', funcname, v_state, v_msg, v_detail, v_hint, v_context;
END;
END LOOP;
UPDATE powa_last_purge SET purgets = now();
END IF;
END;
$PROC$ LANGUAGE plpgsql;
-- remove entries that should not have been stored
DELETE FROM powa_statements_history_current pshc
USING powa_statements ps
WHERE pshc.queryid = ps.queryid
AND pshc.dbid = ps.dbid
AND pshc.userid = ps.userid
AND ps.query ~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)';
DELETE FROM powa_statements_history psh
USING powa_statements ps
WHERE psh.queryid = ps.queryid
AND psh.dbid = ps.dbid
AND psh.userid = ps.userid
AND ps.query ~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)';
DELETE FROM powa_statements
WHERE query ~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)';
|