Skip to content

Commit 8da61bb

Browse files
committed
Control extension state assigning to GUC variable
1 parent 89c3f3a commit 8da61bb

File tree

3 files changed

+12
-40
lines changed

3 files changed

+12
-40
lines changed

pg_show_plans--1.1--2.0.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ DROP FUNCTION gsp_format_text;
22
DROP FUNCTION gsp_format_json;
33
DROP FUNCTION gsp_format_yaml;
44
DROP FUNCTION gsp_format_xml;
5+
DROP FUNCTION pg_show_plans_enable;
6+
DROP FUNCTION pg_show_plans_disable;

pg_show_plans--2.0.sql

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
44
\echo Use "CREATE EXTENSION pg_show_plans" to load this file. \quit
55

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-
176
CREATE FUNCTION pg_show_plans(
187
OUT pid int,
198
OUT level int,
@@ -30,7 +19,3 @@ CREATE VIEW pg_show_plans AS
3019
SELECT * FROM pg_show_plans();
3120

3221
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: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void append_query_plan(ExplainState *es);
8585
/* on_shmem_exit() callback to delete hash entry on client disconnect. */
8686
static void cleanup(int code, Datum arg);
8787
/* Set extension state, either enable or disable. */
88-
static void set_state(const bool state);
88+
static void set_state(bool state, void *extra);
8989
static const char *show_state(void);
9090
/* Set query plan output format: text, json, ... */
9191
static void set_format(const int format);
@@ -108,14 +108,9 @@ static void pgsp_ExecutorStart(QueryDesc *queryDesc, int eflags);
108108
static void pgsp_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
109109
uint64 count, bool execute_once);
110110

111-
/* Enables/Disables the extension. */
112-
Datum pg_show_plans_enable(PG_FUNCTION_ARGS);
113-
Datum pg_show_plans_disable(PG_FUNCTION_ARGS);
114111
/* Show query plans of all the currently running statements. */
115112
Datum pg_show_plans(PG_FUNCTION_ARGS);
116113

117-
PG_FUNCTION_INFO_V1(pg_show_plans_enable);
118-
PG_FUNCTION_INFO_V1(pg_show_plans_disable);
119114
PG_FUNCTION_INFO_V1(pg_show_plans);
120115

121116
/* Global Variables */
@@ -164,9 +159,9 @@ _PG_init(void)
164159
NULL,
165160
&start_enabled,
166161
true,
167-
PGC_POSTMASTER,
162+
PGC_USERSET,
168163
0,
169-
NULL, NULL, show_state);
164+
NULL, set_state, show_state);
170165
DefineCustomIntVariable("pg_show_plans.max_plan_length",
171166
gettext_noop("Set the maximum plan length. "
172167
"Note that this module allocates (max_plan_length*max_connections) "
@@ -318,10 +313,14 @@ cleanup(int code, Datum arg)
318313
}
319314

320315
static void
321-
set_state(const bool state)
316+
set_state(bool state, void *extra)
322317
{
323-
shmem_safety_check();
324-
if (is_allowed_role())
318+
/* Shared memory may not be fully available at server start, so we do not
319+
* check for pgsp_hash availability here. That is why the following line is
320+
* commented out. */
321+
/* shmem_safety_check(); */
322+
323+
if (pgsp != NULL && is_allowed_role())
325324
pgsp->is_enabled = state;
326325
}
327326

@@ -510,20 +509,6 @@ pgsp_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
510509
PG_END_TRY();
511510
}
512511

513-
Datum
514-
pg_show_plans_enable(PG_FUNCTION_ARGS)
515-
{
516-
set_state(true);
517-
PG_RETURN_VOID();
518-
}
519-
520-
Datum
521-
pg_show_plans_disable(PG_FUNCTION_ARGS)
522-
{
523-
set_state(false);
524-
PG_RETURN_VOID();
525-
}
526-
527512
Datum
528513
pg_show_plans(PG_FUNCTION_ARGS)
529514
{

0 commit comments

Comments
 (0)