Skip to content

Commit

Permalink
Add pg_show_plans_q view
Browse files Browse the repository at this point in the history
Same as pg_show_plans view but allows you to see the corresponding
query strings.
  • Loading branch information
Ivan Kovmir committed Nov 7, 2023
1 parent 9a77b73 commit 4f090b1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions pg_show_plans--2.0--2.1.sql
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 29 additions & 0 deletions pg_show_plans--2.1.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion pg_show_plans.control
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f090b1

Please sign in to comment.