2016-11-12 14:01:48 +01:00
|
|
|
CREATE EXTENSION pg_stat_statements;
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
--
|
|
|
|
-- simple and compound statements
|
|
|
|
--
|
|
|
|
SET pg_stat_statements.track_utility = FALSE;
|
2020-07-03 04:35:22 +02:00
|
|
|
SET pg_stat_statements.track_planning = TRUE;
|
2017-01-14 22:17:30 +01:00
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
SELECT 1 AS "int";
|
|
|
|
|
|
|
|
SELECT 'hello'
|
|
|
|
-- multiline
|
|
|
|
AS "text";
|
|
|
|
|
|
|
|
SELECT 'world' AS "text";
|
|
|
|
|
|
|
|
-- transaction
|
|
|
|
BEGIN;
|
|
|
|
SELECT 1 AS "int";
|
|
|
|
SELECT 'hello' AS "text";
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
-- compound transaction
|
|
|
|
BEGIN \;
|
|
|
|
SELECT 2.0 AS "float" \;
|
|
|
|
SELECT 'world' AS "text" \;
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
-- compound with empty statements and spurious leading spacing
|
|
|
|
\;\; SELECT 3 + 3 \;\;\; SELECT ' ' || ' !' \;\; SELECT 1 + 4 \;;
|
|
|
|
|
|
|
|
-- non ;-terminated statements
|
|
|
|
SELECT 1 + 1 + 1 AS "add" \gset
|
|
|
|
SELECT :add + 1 + 1 AS "add" \;
|
|
|
|
SELECT :add + 1 + 1 AS "add" \gset
|
|
|
|
|
|
|
|
-- set operator
|
|
|
|
SELECT 1 AS i UNION SELECT 2 ORDER BY i;
|
|
|
|
|
2017-03-28 02:14:36 +02:00
|
|
|
-- ? operator
|
|
|
|
select '{"a":1, "b":2}'::jsonb ? 'b';
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
-- cte
|
|
|
|
WITH t(f) AS (
|
|
|
|
VALUES (1.0), (2.0)
|
|
|
|
)
|
|
|
|
SELECT f FROM t ORDER BY f;
|
2016-11-12 14:01:48 +01:00
|
|
|
|
2017-03-28 02:14:36 +02:00
|
|
|
-- prepared statement with parameter
|
|
|
|
PREPARE pgss_test (int) AS SELECT $1, 'test' LIMIT 1;
|
|
|
|
EXECUTE pgss_test(1);
|
|
|
|
DEALLOCATE pgss_test;
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- CRUD: INSERT SELECT UPDATE DELETE on test table
|
|
|
|
--
|
2016-11-12 14:01:48 +01:00
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
-- utility "create table" should not be shown
|
2017-03-28 02:14:36 +02:00
|
|
|
CREATE TEMP TABLE test (a int, b char(20));
|
2017-01-14 22:17:30 +01:00
|
|
|
|
2016-11-12 14:01:48 +01:00
|
|
|
INSERT INTO test VALUES(generate_series(1, 10), 'aaa');
|
2017-01-14 22:17:30 +01:00
|
|
|
UPDATE test SET b = 'bbb' WHERE a > 7;
|
|
|
|
DELETE FROM test WHERE a > 9;
|
|
|
|
|
|
|
|
-- explicit transaction
|
|
|
|
BEGIN;
|
|
|
|
UPDATE test SET b = '111' WHERE a = 1 ;
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
BEGIN \;
|
|
|
|
UPDATE test SET b = '222' WHERE a = 2 \;
|
|
|
|
COMMIT ;
|
|
|
|
|
|
|
|
UPDATE test SET b = '333' WHERE a = 3 \;
|
|
|
|
UPDATE test SET b = '444' WHERE a = 4 ;
|
|
|
|
|
|
|
|
BEGIN \;
|
|
|
|
UPDATE test SET b = '555' WHERE a = 5 \;
|
|
|
|
UPDATE test SET b = '666' WHERE a = 6 \;
|
|
|
|
COMMIT ;
|
|
|
|
|
2017-03-28 02:14:36 +02:00
|
|
|
-- many INSERT values
|
|
|
|
INSERT INTO test (a, b) VALUES (1, 'a'), (2, 'b'), (3, 'c');
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
-- SELECT with constants
|
|
|
|
SELECT * FROM test WHERE a > 5 ORDER BY a ;
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
FROM test
|
|
|
|
WHERE a > 9
|
|
|
|
ORDER BY a ;
|
|
|
|
|
|
|
|
-- SELECT without constants
|
|
|
|
SELECT * FROM test ORDER BY a;
|
|
|
|
|
2017-03-28 02:14:36 +02:00
|
|
|
-- SELECT with IN clause
|
|
|
|
SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5);
|
|
|
|
|
2022-09-27 10:44:42 +02:00
|
|
|
-- MERGE
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a AND st.a >= 4)
|
|
|
|
WHEN MATCHED THEN UPDATE SET b = st.b || st.a::text;
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a AND st.a >= 4)
|
|
|
|
WHEN MATCHED THEN UPDATE SET b = test.b || st.a::text;
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a AND st.a >= 4)
|
|
|
|
WHEN MATCHED AND length(st.b) > 1 THEN UPDATE SET b = test.b || st.a::text;
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a)
|
|
|
|
WHEN NOT MATCHED THEN INSERT (a, b) VALUES (0, NULL);
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a)
|
|
|
|
WHEN NOT MATCHED THEN INSERT VALUES (0, NULL); -- same as above
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a)
|
|
|
|
WHEN NOT MATCHED THEN INSERT (b, a) VALUES (NULL, 0);
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a)
|
|
|
|
WHEN NOT MATCHED THEN INSERT (a) VALUES (0);
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a AND st.a >= 4)
|
|
|
|
WHEN MATCHED THEN DELETE;
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a AND st.a >= 4)
|
|
|
|
WHEN MATCHED THEN DO NOTHING;
|
|
|
|
MERGE INTO test USING test st ON (st.a = test.a AND st.a >= 4)
|
|
|
|
WHEN NOT MATCHED THEN DO NOTHING;
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
2020-04-05 04:04:04 +02:00
|
|
|
--
|
|
|
|
-- INSERT, UPDATE, DELETE on test table to validate WAL generation metrics
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
-- utility "create table" should not be shown
|
|
|
|
CREATE TABLE pgss_test (a int, b char(20));
|
|
|
|
|
|
|
|
INSERT INTO pgss_test VALUES(generate_series(1, 10), 'aaa');
|
|
|
|
UPDATE pgss_test SET b = 'bbb' WHERE a > 7;
|
|
|
|
DELETE FROM pgss_test WHERE a > 9;
|
|
|
|
-- DROP test table
|
|
|
|
SET pg_stat_statements.track_utility = TRUE;
|
|
|
|
DROP TABLE pgss_test;
|
|
|
|
SET pg_stat_statements.track_utility = FALSE;
|
|
|
|
|
|
|
|
-- Check WAL is generated for the above statements
|
|
|
|
SELECT query, calls, rows,
|
|
|
|
wal_bytes > 0 as wal_bytes_generated,
|
|
|
|
wal_records > 0 as wal_records_generated,
|
|
|
|
wal_records = rows as wal_records_as_rows
|
|
|
|
FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
--
|
|
|
|
-- pg_stat_statements.track = none
|
|
|
|
--
|
|
|
|
SET pg_stat_statements.track = 'none';
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
SELECT 1 AS "one";
|
|
|
|
SELECT 1 + 1 AS "two";
|
|
|
|
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- pg_stat_statements.track = top
|
|
|
|
--
|
|
|
|
SET pg_stat_statements.track = 'top';
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
DO LANGUAGE plpgsql $$
|
|
|
|
BEGIN
|
|
|
|
-- this is a SELECT
|
|
|
|
PERFORM 'hello world'::TEXT;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
-- PL/pgSQL function
|
|
|
|
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$
|
|
|
|
DECLARE
|
|
|
|
r INTEGER;
|
|
|
|
BEGIN
|
|
|
|
SELECT (i + 1 + 1.0)::INTEGER INTO r;
|
|
|
|
RETURN r;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
SELECT PLUS_TWO(3);
|
|
|
|
SELECT PLUS_TWO(7);
|
|
|
|
|
|
|
|
-- SQL function --- use LIMIT to keep it from being inlined
|
|
|
|
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS
|
|
|
|
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL;
|
|
|
|
|
|
|
|
SELECT PLUS_ONE(8);
|
|
|
|
SELECT PLUS_ONE(10);
|
|
|
|
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- pg_stat_statements.track = all
|
|
|
|
--
|
|
|
|
SET pg_stat_statements.track = 'all';
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
-- we drop and recreate the functions to avoid any caching funnies
|
|
|
|
DROP FUNCTION PLUS_ONE(INTEGER);
|
|
|
|
DROP FUNCTION PLUS_TWO(INTEGER);
|
|
|
|
|
|
|
|
-- PL/pgSQL function
|
|
|
|
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$
|
|
|
|
DECLARE
|
|
|
|
r INTEGER;
|
|
|
|
BEGIN
|
|
|
|
SELECT (i + 1 + 1.0)::INTEGER INTO r;
|
|
|
|
RETURN r;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
SELECT PLUS_TWO(-1);
|
|
|
|
SELECT PLUS_TWO(2);
|
|
|
|
|
|
|
|
-- SQL function --- use LIMIT to keep it from being inlined
|
|
|
|
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS
|
|
|
|
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL;
|
|
|
|
|
|
|
|
SELECT PLUS_ONE(3);
|
|
|
|
SELECT PLUS_ONE(1);
|
|
|
|
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
2019-07-14 13:07:40 +02:00
|
|
|
--
|
|
|
|
-- queries with locking clauses
|
|
|
|
--
|
|
|
|
CREATE TABLE pgss_a (id integer PRIMARY KEY);
|
|
|
|
CREATE TABLE pgss_b (id integer PRIMARY KEY, a_id integer REFERENCES pgss_a);
|
|
|
|
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
-- control query
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id;
|
|
|
|
|
|
|
|
-- test range tables
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE;
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_a;
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_b;
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_a, pgss_b; -- matches plain "FOR UPDATE"
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_b, pgss_a;
|
|
|
|
|
|
|
|
-- test strengths
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR NO KEY UPDATE;
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR SHARE;
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR KEY SHARE;
|
|
|
|
|
|
|
|
-- test wait policies
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE NOWAIT;
|
|
|
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE SKIP LOCKED;
|
|
|
|
|
|
|
|
SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
DROP TABLE pgss_a, pgss_b CASCADE;
|
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
--
|
|
|
|
-- utility commands
|
|
|
|
--
|
|
|
|
SET pg_stat_statements.track_utility = TRUE;
|
|
|
|
SELECT pg_stat_statements_reset();
|
2016-11-12 14:01:48 +01:00
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
SELECT 1;
|
|
|
|
CREATE INDEX test_b ON test(b);
|
|
|
|
DROP TABLE test \;
|
|
|
|
DROP TABLE IF EXISTS test \;
|
|
|
|
DROP FUNCTION PLUS_ONE(INTEGER);
|
|
|
|
DROP TABLE IF EXISTS test \;
|
|
|
|
DROP TABLE IF EXISTS test \;
|
|
|
|
DROP FUNCTION IF EXISTS PLUS_ONE(INTEGER);
|
|
|
|
DROP FUNCTION PLUS_TWO(INTEGER);
|
2016-11-12 14:01:48 +01:00
|
|
|
|
2017-01-14 22:17:30 +01:00
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
2016-11-12 14:01:48 +01:00
|
|
|
|
2020-07-29 16:21:55 +02:00
|
|
|
--
|
|
|
|
-- Track the total number of rows retrieved or affected by the utility
|
2020-11-12 03:26:55 +01:00
|
|
|
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
|
|
|
|
-- REFRESH MATERIALIZED VIEW and SELECT INTO
|
2020-07-29 16:21:55 +02:00
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
|
|
|
|
CREATE TABLE pgss_ctas AS SELECT a, 'ctas' b FROM generate_series(1, 10) a;
|
|
|
|
SELECT generate_series(1, 10) c INTO pgss_select_into;
|
|
|
|
COPY pgss_ctas (a, b) FROM STDIN;
|
|
|
|
11 copy
|
|
|
|
12 copy
|
|
|
|
13 copy
|
|
|
|
\.
|
|
|
|
CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
|
2020-11-12 03:26:55 +01:00
|
|
|
REFRESH MATERIALIZED VIEW pgss_matv;
|
2020-07-29 16:21:55 +02:00
|
|
|
BEGIN;
|
|
|
|
DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
|
|
|
|
FETCH NEXT pgss_cursor;
|
|
|
|
FETCH FORWARD 5 pgss_cursor;
|
|
|
|
FETCH FORWARD ALL pgss_cursor;
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
2019-01-11 04:20:09 +01:00
|
|
|
--
|
|
|
|
-- Track user activity and reset them
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
CREATE ROLE regress_stats_user1;
|
|
|
|
CREATE ROLE regress_stats_user2;
|
|
|
|
|
|
|
|
SET ROLE regress_stats_user1;
|
|
|
|
|
|
|
|
SELECT 1 AS "ONE";
|
|
|
|
SELECT 1+1 AS "TWO";
|
|
|
|
|
|
|
|
RESET ROLE;
|
|
|
|
SET ROLE regress_stats_user2;
|
|
|
|
|
|
|
|
SELECT 1 AS "ONE";
|
|
|
|
SELECT 1+1 AS "TWO";
|
|
|
|
|
|
|
|
RESET ROLE;
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Don't reset anything if any of the parameter is NULL
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset(NULL);
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- remove query ('SELECT $1+$2 AS "TWO"') executed by regress_stats_user2
|
|
|
|
-- in the current_database
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset(
|
|
|
|
(SELECT r.oid FROM pg_roles AS r WHERE r.rolname = 'regress_stats_user2'),
|
|
|
|
(SELECT d.oid FROM pg_database As d where datname = current_database()),
|
|
|
|
(SELECT s.queryid FROM pg_stat_statements AS s
|
|
|
|
WHERE s.query = 'SELECT $1+$2 AS "TWO"' LIMIT 1));
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- remove query ('SELECT $1 AS "ONE"') executed by two users
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset(0,0,s.queryid)
|
|
|
|
FROM pg_stat_statements AS s WHERE s.query = 'SELECT $1 AS "ONE"';
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- remove query of a user (regress_stats_user1)
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset(r.oid)
|
|
|
|
FROM pg_roles AS r WHERE r.rolname = 'regress_stats_user1';
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- reset all
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset(0,0,0);
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
|
|
|
|
|
|
--
|
|
|
|
-- cleanup
|
|
|
|
--
|
|
|
|
DROP ROLE regress_stats_user1;
|
|
|
|
DROP ROLE regress_stats_user2;
|
2020-07-29 16:21:55 +02:00
|
|
|
DROP MATERIALIZED VIEW pgss_matv;
|
|
|
|
DROP TABLE pgss_ctas;
|
|
|
|
DROP TABLE pgss_select_into;
|
2019-01-11 04:20:09 +01:00
|
|
|
|
Allow pg_stat_statements to track planning statistics.
This commit makes pg_stat_statements support new GUC
pg_stat_statements.track_planning. If this option is enabled,
pg_stat_statements tracks the planning statistics of the statements,
e.g., the number of times the statement was planned, the total time
spent planning the statement, etc. This feature is useful to check
the statements that it takes a long time to plan. Previously since
pg_stat_statements tracked only the execution statistics, we could
not use that for the purpose.
The planning and execution statistics are stored at the end of
each phase separately. So there are not always one-to-one relationship
between them. For example, if the statement is successfully planned
but fails in the execution phase, only its planning statistics are stored.
This may cause the users to be able to see different pg_stat_statements
results from the previous version. To avoid this,
pg_stat_statements.track_planning needs to be disabled.
This commit bumps the version of pg_stat_statements to 1.8
since it changes the definition of pg_stat_statements function.
Author: Julien Rouhaud, Pascal Legrand, Thomas Munro, Fujii Masao
Reviewed-by: Sergei Kornilov, Tomas Vondra, Yoshikazu Imai, Haribabu Kommi, Tom Lane
Discussion: https://postgr.es/m/CAHGQGwFx_=DO-Gu-MfPW3VQ4qC7TfVdH2zHmvZfrGv6fQ3D-Tw@mail.gmail.com
Discussion: https://postgr.es/m/CAEepm=0e59Y_6Q_YXYCTHZkqOc6H2pJ54C_Xe=VFu50Aqqp_sA@mail.gmail.com
Discussion: https://postgr.es/m/DB6PR0301MB21352F6210E3B11934B0DCC790B00@DB6PR0301MB2135.eurprd03.prod.outlook.com
2020-04-02 04:20:19 +02:00
|
|
|
--
|
|
|
|
-- [re]plan counting
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
CREATE TABLE test ();
|
|
|
|
PREPARE prep1 AS SELECT COUNT(*) FROM test;
|
|
|
|
EXECUTE prep1;
|
|
|
|
EXECUTE prep1;
|
|
|
|
EXECUTE prep1;
|
|
|
|
ALTER TABLE test ADD COLUMN x int;
|
|
|
|
EXECUTE prep1;
|
|
|
|
SELECT 42;
|
|
|
|
SELECT 42;
|
|
|
|
SELECT 42;
|
2021-07-26 05:25:15 +02:00
|
|
|
SELECT query, plans, calls, rows FROM pg_stat_statements
|
|
|
|
WHERE query NOT LIKE 'PREPARE%' ORDER BY query COLLATE "C";
|
|
|
|
-- for the prepared statement we expect at least one replan, but cache
|
|
|
|
-- invalidations could force more
|
|
|
|
SELECT query, plans >= 2 AND plans <= calls AS plans_ok, calls, rows FROM pg_stat_statements
|
|
|
|
WHERE query LIKE 'PREPARE%' ORDER BY query COLLATE "C";
|
Allow pg_stat_statements to track planning statistics.
This commit makes pg_stat_statements support new GUC
pg_stat_statements.track_planning. If this option is enabled,
pg_stat_statements tracks the planning statistics of the statements,
e.g., the number of times the statement was planned, the total time
spent planning the statement, etc. This feature is useful to check
the statements that it takes a long time to plan. Previously since
pg_stat_statements tracked only the execution statistics, we could
not use that for the purpose.
The planning and execution statistics are stored at the end of
each phase separately. So there are not always one-to-one relationship
between them. For example, if the statement is successfully planned
but fails in the execution phase, only its planning statistics are stored.
This may cause the users to be able to see different pg_stat_statements
results from the previous version. To avoid this,
pg_stat_statements.track_planning needs to be disabled.
This commit bumps the version of pg_stat_statements to 1.8
since it changes the definition of pg_stat_statements function.
Author: Julien Rouhaud, Pascal Legrand, Thomas Munro, Fujii Masao
Reviewed-by: Sergei Kornilov, Tomas Vondra, Yoshikazu Imai, Haribabu Kommi, Tom Lane
Discussion: https://postgr.es/m/CAHGQGwFx_=DO-Gu-MfPW3VQ4qC7TfVdH2zHmvZfrGv6fQ3D-Tw@mail.gmail.com
Discussion: https://postgr.es/m/CAEepm=0e59Y_6Q_YXYCTHZkqOc6H2pJ54C_Xe=VFu50Aqqp_sA@mail.gmail.com
Discussion: https://postgr.es/m/DB6PR0301MB21352F6210E3B11934B0DCC790B00@DB6PR0301MB2135.eurprd03.prod.outlook.com
2020-04-02 04:20:19 +02:00
|
|
|
|
2020-11-26 13:18:05 +01:00
|
|
|
--
|
|
|
|
-- access to pg_stat_statements_info view
|
|
|
|
--
|
|
|
|
SELECT pg_stat_statements_reset();
|
|
|
|
SELECT dealloc FROM pg_stat_statements_info;
|
|
|
|
|
2021-04-08 10:23:10 +02:00
|
|
|
--
|
|
|
|
-- top level handling
|
|
|
|
--
|
|
|
|
SET pg_stat_statements.track = 'top';
|
|
|
|
DELETE FROM test;
|
|
|
|
DO $$
|
|
|
|
BEGIN
|
|
|
|
DELETE FROM test;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
SELECT query, toplevel, plans, calls FROM pg_stat_statements WHERE query LIKE '%DELETE%' ORDER BY query COLLATE "C", toplevel;
|
|
|
|
|
|
|
|
SET pg_stat_statements.track = 'all';
|
|
|
|
DELETE FROM test;
|
|
|
|
DO $$
|
|
|
|
BEGIN
|
|
|
|
DELETE FROM test;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
SELECT query, toplevel, plans, calls FROM pg_stat_statements WHERE query LIKE '%DELETE%' ORDER BY query COLLATE "C", toplevel;
|
|
|
|
|
2021-05-03 20:59:30 +02:00
|
|
|
-- FROM [ONLY]
|
|
|
|
CREATE TABLE tbl_inh(id integer);
|
|
|
|
CREATE TABLE tbl_inh_1() INHERITS (tbl_inh);
|
|
|
|
INSERT INTO tbl_inh_1 SELECT 1;
|
|
|
|
|
|
|
|
SELECT * FROM tbl_inh;
|
|
|
|
SELECT * FROM ONLY tbl_inh;
|
|
|
|
|
|
|
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%FROM%tbl_inh%';
|
|
|
|
|
|
|
|
-- WITH TIES
|
|
|
|
CREATE TABLE limitoption AS SELECT 0 AS val FROM generate_series(1, 10);
|
|
|
|
SELECT *
|
|
|
|
FROM limitoption
|
|
|
|
WHERE val < 2
|
|
|
|
ORDER BY val
|
|
|
|
FETCH FIRST 2 ROWS WITH TIES;
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
FROM limitoption
|
|
|
|
WHERE val < 2
|
|
|
|
ORDER BY val
|
|
|
|
FETCH FIRST 2 ROW ONLY;
|
|
|
|
|
|
|
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%FETCH FIRST%';
|
|
|
|
|
|
|
|
-- GROUP BY [DISTINCT]
|
|
|
|
SELECT a, b, c
|
|
|
|
FROM (VALUES (1, 2, 3), (4, NULL, 6), (7, 8, 9)) AS t (a, b, c)
|
|
|
|
GROUP BY ROLLUP(a, b), rollup(a, c)
|
|
|
|
ORDER BY a, b, c;
|
|
|
|
SELECT a, b, c
|
|
|
|
FROM (VALUES (1, 2, 3), (4, NULL, 6), (7, 8, 9)) AS t (a, b, c)
|
|
|
|
GROUP BY DISTINCT ROLLUP(a, b), rollup(a, c)
|
|
|
|
ORDER BY a, b, c;
|
|
|
|
|
|
|
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%GROUP BY%ROLLUP%';
|
|
|
|
|
|
|
|
-- GROUPING SET agglevelsup
|
|
|
|
SELECT (
|
|
|
|
SELECT (
|
|
|
|
SELECT GROUPING(a,b) FROM (VALUES (1)) v2(c)
|
|
|
|
) FROM (VALUES (1,2)) v1(a,b) GROUP BY (a,b)
|
|
|
|
) FROM (VALUES(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
|
|
|
|
SELECT (
|
|
|
|
SELECT (
|
|
|
|
SELECT GROUPING(e,f) FROM (VALUES (1)) v2(c)
|
|
|
|
) FROM (VALUES (1,2)) v1(a,b) GROUP BY (a,b)
|
|
|
|
) FROM (VALUES(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
|
|
|
|
|
|
|
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%SELECT GROUPING%';
|
|
|
|
|
2016-11-12 14:01:48 +01:00
|
|
|
DROP EXTENSION pg_stat_statements;
|