Skip to content

Commit 0eb8995

Browse files
committed
Control plan format via GUC variable
Now you can select plan format by assigning to 'pg_show_plans.plan_format' GUC variable. Close #31.
1 parent 48a585d commit 0eb8995

File tree

11 files changed

+67
-206
lines changed

11 files changed

+67
-206
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ OBJS = pg_show_plans.o
33

44
EXTENSION = pg_show_plans
55
DATA = pg_show_plans--1.0--1.1.sql \
6-
pg_show_plans--1.1.sql
6+
pg_show_plans--1.1--2.0.sql \
7+
pg_show_plans--2.0.sql
78
REGRESS = pg_show_plans formats
89
DOCS = pg_show_plans.md
910

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,3 @@ query |
136136
- `plan`: query plan.
137137
* `pg_show_plans_disable()`: disable extension.
138138
* `pg_show_plans_enable()`: enable extension.
139-
* `pgsp_format_text()`: changes query format to `json`.
140-
* `pgsp_format_json()`: changes query format to `json`.
141-
* `pgsp_format_yaml()`: changes query format to `yaml`.
142-
* `pgsp_format_xml()`: changes query format to `xml`.

expected/formats.out

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ select setting::int < 140000 as pg12_13 from pg_settings where name = 'server_ve
66
(1 row)
77

88
-- json output
9-
select pgsp_format_json();
10-
pgsp_format_json
11-
------------------
12-
13-
(1 row)
14-
9+
set pg_show_plans.plan_format = 'json';
1510
show pg_show_plans.plan_format;
1611
pg_show_plans.plan_format
1712
---------------------------
@@ -51,12 +46,7 @@ select * from nest();
5146
(2 rows)
5247

5348
-- yaml output
54-
select pgsp_format_yaml();
55-
pgsp_format_yaml
56-
------------------
57-
58-
(1 row)
59-
49+
set pg_show_plans.plan_format = 'yaml';
6050
show pg_show_plans.plan_format;
6151
pg_show_plans.plan_format
6252
---------------------------
@@ -90,12 +80,7 @@ select * from nest();
9080
(2 rows)
9181

9282
-- xml output
93-
select pgsp_format_xml();
94-
pgsp_format_xml
95-
-----------------
96-
97-
(1 row)
98-
83+
set pg_show_plans.plan_format = 'xml';
9984
show pg_show_plans.plan_format;
10085
pg_show_plans.plan_format
10186
---------------------------

expected/formats_1.out

Lines changed: 0 additions & 138 deletions
This file was deleted.

expected/pg_show_plans.out

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ as $$
2222
end;
2323
$$;
2424
-- text output
25+
set pg_show_plans.plan_format = 'text';
2526
show pg_show_plans.plan_format;
2627
pg_show_plans.plan_format
2728
---------------------------
@@ -42,5 +43,3 @@ select * from nest();
4243
| Filter: (level >= 0)
4344
(2 rows)
4445

45-
set pg_show_plans.plan_format = 'json'; -- fails
46-
ERROR: parameter "pg_show_plans.plan_format" cannot be changed without restarting the server

pg_show_plans--1.1--2.0.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DROP FUNCTION gsp_format_text;
2+
DROP FUNCTION gsp_format_json;
3+
DROP FUNCTION gsp_format_yaml;
4+
DROP FUNCTION gsp_format_xml;

pg_show_plans--2.0.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* pg_show_plans/pg_show_plans--2.0.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "CREATE EXTENSION pg_show_plans" to load this file. \quit
5+
6+
-- Register functions.
7+
CREATE FUNCTION pg_show_plans_enable()
8+
RETURNS void
9+
AS 'MODULE_PATHNAME'
10+
LANGUAGE C;
11+
12+
CREATE FUNCTION pg_show_plans_disable()
13+
RETURNS void
14+
AS 'MODULE_PATHNAME'
15+
LANGUAGE C;
16+
17+
CREATE FUNCTION pg_show_plans(
18+
OUT pid int,
19+
OUT level int,
20+
OUT userid oid,
21+
OUT dbid oid,
22+
OUT plan text
23+
)
24+
RETURNS SETOF record
25+
AS 'MODULE_PATHNAME'
26+
LANGUAGE C;
27+
28+
-- Register a view on the function for ease of use.
29+
CREATE VIEW pg_show_plans AS
30+
SELECT * FROM pg_show_plans();
31+
32+
GRANT SELECT ON pg_show_plans TO PUBLIC;
33+
34+
-- Don't want this to be available to non-superusers.
35+
REVOKE ALL ON FUNCTION pg_show_plans_enable() FROM PUBLIC;
36+
REVOKE ALL ON FUNCTION pg_show_plans_disable() FROM PUBLIC;

pg_show_plans.c

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ static void set_state(const bool state);
8989
static const char *show_state(void);
9090
/* Set query plan output format: text, json, ... */
9191
static void set_format(const int format);
92+
/* Propagate GUC variable value to shared memory (assign hook). */
93+
static void prop_format_to_shmem(int newval, void *extra);
9294
static const char *show_format(void);
9395
/* Check the extension has been properly loaded. */
9496
static inline void shmem_safety_check(void);
@@ -109,11 +111,6 @@ static void pgsp_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
109111
/* Enables/Disables the extension. */
110112
Datum pg_show_plans_enable(PG_FUNCTION_ARGS);
111113
Datum pg_show_plans_disable(PG_FUNCTION_ARGS);
112-
/* Sets query plan output format. */
113-
Datum pgsp_format_text(PG_FUNCTION_ARGS);
114-
Datum pgsp_format_json(PG_FUNCTION_ARGS);
115-
Datum pgsp_format_yaml(PG_FUNCTION_ARGS);
116-
Datum pgsp_format_xml(PG_FUNCTION_ARGS);
117114
/* Show query plans of all the currently running statements. */
118115
Datum pg_show_plans(PG_FUNCTION_ARGS);
119116

@@ -195,9 +192,9 @@ _PG_init(void)
195192
&plan_format,
196193
EXPLAIN_FORMAT_TEXT,
197194
plan_formats,
198-
PGC_POSTMASTER,
195+
PGC_USERSET,
199196
0,
200-
NULL, NULL, show_format);
197+
NULL, prop_format_to_shmem, show_format);
201198

202199
/* Save old hooks, and install new ones. */
203200
#if PG_VERSION_NUM >= 150000
@@ -346,11 +343,21 @@ show_state()
346343
static void
347344
set_format(const int format)
348345
{
349-
shmem_safety_check();
350-
if (is_allowed_role())
346+
/* Shared memory may not be fully available at server start, so we do not
347+
* check for pgsp_hash availability here. That is why the following line is
348+
* commented out. */
349+
/* shmem_safety_check(); */
350+
351+
if (pgsp != NULL && is_allowed_role())
351352
pgsp->plan_format = format;
352353
}
353354

355+
static void
356+
prop_format_to_shmem(int newval, void *extra)
357+
{
358+
set_format(newval);
359+
}
360+
354361
static const char *
355362
show_format()
356363
{
@@ -521,34 +528,6 @@ pg_show_plans_disable(PG_FUNCTION_ARGS)
521528
PG_RETURN_VOID();
522529
}
523530

524-
Datum
525-
pgsp_format_text(PG_FUNCTION_ARGS)
526-
{
527-
set_format(EXPLAIN_FORMAT_TEXT);
528-
PG_RETURN_VOID();
529-
}
530-
531-
Datum
532-
pgsp_format_json(PG_FUNCTION_ARGS)
533-
{
534-
set_format(EXPLAIN_FORMAT_JSON);
535-
PG_RETURN_VOID();
536-
}
537-
538-
Datum
539-
pgsp_format_yaml(PG_FUNCTION_ARGS)
540-
{
541-
set_format(EXPLAIN_FORMAT_YAML);
542-
PG_RETURN_VOID();
543-
}
544-
545-
Datum
546-
pgsp_format_xml(PG_FUNCTION_ARGS)
547-
{
548-
set_format(EXPLAIN_FORMAT_XML);
549-
PG_RETURN_VOID();
550-
}
551-
552531
Datum
553532
pg_show_plans(PG_FUNCTION_ARGS)
554533
{

pg_show_plans.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_show_plans extension
22
comment = 'show query plans of all currently running SQL statements'
3-
default_version = '1.1'
3+
default_version = '2.0'
44
module_pathname = '$libdir/pg_show_plans'
55
relocatable = true

sql/formats.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
select setting::int < 140000 as pg12_13 from pg_settings where name = 'server_version_num';
33

44
-- json output
5-
select pgsp_format_json();
5+
set pg_show_plans.plan_format = 'json';
66
show pg_show_plans.plan_format;
77
select * from nest();
88

99
-- yaml output
10-
select pgsp_format_yaml();
10+
set pg_show_plans.plan_format = 'yaml';
1111
show pg_show_plans.plan_format;
1212
select * from nest();
1313

1414
-- xml output
15-
select pgsp_format_xml();
15+
set pg_show_plans.plan_format = 'xml';
1616
show pg_show_plans.plan_format;
1717
select * from nest();
1818

0 commit comments

Comments
 (0)