From 4f090b187fff7204957596f7c4e989038155c350 Mon Sep 17 00:00:00 2001 From: Ivan Kovmir Date: Tue, 7 Nov 2023 11:22:10 +0100 Subject: [PATCH] Add pg_show_plans_q view Same as pg_show_plans view but allows you to see the corresponding query strings. --- Makefile | 3 ++- pg_show_plans--2.0--2.1.sql | 8 ++++++++ pg_show_plans--2.1.sql | 29 +++++++++++++++++++++++++++++ pg_show_plans.control | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 pg_show_plans--2.0--2.1.sql create mode 100644 pg_show_plans--2.1.sql diff --git a/Makefile b/Makefile index d911c19..e46b844 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ OBJS = pg_show_plans.o EXTENSION = pg_show_plans DATA = pg_show_plans--1.0--1.1.sql \ pg_show_plans--1.1--2.0.sql \ - pg_show_plans--2.0.sql + pg_show_plans--2.0--2.1.sql \ + pg_show_plans--2.1.sql REGRESS = pg_show_plans formats DOCS = pg_show_plans.md diff --git a/pg_show_plans--2.0--2.1.sql b/pg_show_plans--2.0--2.1.sql new file mode 100644 index 0000000..7e4c8a7 --- /dev/null +++ b/pg_show_plans--2.0--2.1.sql @@ -0,0 +1,8 @@ +-- Register a view to see query plans along with the corresponding queries. +CREATE VIEW pg_show_plans_q AS + SELECT p.pid, p.level, p.plan, a.query + FROM pg_show_plans p + LEFT JOIN pg_stat_activity a + ON p.pid = a.pid AND p.level = 0 ORDER BY p.pid, p.level; + +GRANT SELECT ON pg_show_plans_q TO PUBLIC; diff --git a/pg_show_plans--2.1.sql b/pg_show_plans--2.1.sql new file mode 100644 index 0000000..9fff321 --- /dev/null +++ b/pg_show_plans--2.1.sql @@ -0,0 +1,29 @@ +/* pg_show_plans/pg_show_plans--2.1.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION pg_show_plans" to load this file. \quit + +CREATE FUNCTION pg_show_plans( + OUT pid int, + OUT level int, + OUT userid oid, + OUT dbid oid, + OUT plan text +) +RETURNS SETOF record +AS 'MODULE_PATHNAME' +LANGUAGE C; + +-- Register a view on the function for ease of use. +CREATE VIEW pg_show_plans AS + SELECT * FROM pg_show_plans(); + +-- Register a view to see query plans along with the corresponding queries. +CREATE VIEW pg_show_plans_q AS + SELECT p.pid, p.level, p.plan, a.query + FROM pg_show_plans p + LEFT JOIN pg_stat_activity a + ON p.pid = a.pid AND p.level = 0 ORDER BY p.pid, p.level; + +GRANT SELECT ON pg_show_plans TO PUBLIC; +GRANT SELECT ON pg_show_plans_q TO PUBLIC; diff --git a/pg_show_plans.control b/pg_show_plans.control index 1ebf426..6bb4989 100644 --- a/pg_show_plans.control +++ b/pg_show_plans.control @@ -1,5 +1,5 @@ # pg_show_plans extension comment = 'show query plans of all currently running SQL statements' -default_version = '2.0' +default_version = '2.1' module_pathname = '$libdir/pg_show_plans' relocatable = true