Skip to content

Commit 85794a1

Browse files
committed
Add ability to set query plan formats on the run
1 parent 0c14f48 commit 85794a1

2 files changed

Lines changed: 60 additions & 11 deletions

File tree

pg_show_plans--1.0.sql

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,47 @@ RETURNS void
1414
AS 'MODULE_PATHNAME'
1515
LANGUAGE C;
1616

17+
CREATE FUNCTION pgsp_format_text()
18+
RETURNS void
19+
AS 'MODULE_PATHNAME'
20+
LANGUAGE C;
21+
1722
CREATE FUNCTION pgsp_format_json()
1823
RETURNS void
1924
AS 'MODULE_PATHNAME'
2025
LANGUAGE C;
2126

22-
CREATE FUNCTION pgsp_format_text()
27+
CREATE FUNCTION pgsp_format_yaml()
28+
RETURNS void
29+
AS 'MODULE_PATHNAME'
30+
LANGUAGE C;
31+
32+
CREATE FUNCTION pgsp_format_xml()
2333
RETURNS void
2434
AS 'MODULE_PATHNAME'
2535
LANGUAGE C;
2636

2737
CREATE FUNCTION pg_show_plans(
28-
OUT pid int8,
29-
OUT level int8,
30-
OUT userid oid,
31-
OUT dbid oid,
32-
OUT plan text
38+
OUT pid int8,
39+
OUT level int8,
40+
OUT userid oid,
41+
OUT dbid oid,
42+
OUT plan text
3343
)
3444
RETURNS SETOF record
3545
AS 'MODULE_PATHNAME'
3646
LANGUAGE C;
3747

3848
-- Register a view on the function for ease of use.
3949
CREATE VIEW pg_show_plans AS
40-
SELECT * FROM pg_show_plans();
50+
SELECT * FROM pg_show_plans();
4151

4252
GRANT SELECT ON pg_show_plans TO PUBLIC;
4353

4454
-- Don't want this to be available to non-superusers.
4555
REVOKE ALL ON FUNCTION pg_show_plans_enable() FROM PUBLIC;
4656
REVOKE ALL ON FUNCTION pg_show_plans_disable() FROM PUBLIC;
47-
REVOKE ALL ON FUNCTION pgsp_format_json() FROM PUBLIC;
4857
REVOKE ALL ON FUNCTION pgsp_format_text() FROM PUBLIC;
58+
REVOKE ALL ON FUNCTION pgsp_format_json() FROM PUBLIC;
59+
REVOKE ALL ON FUNCTION pgsp_format_yaml() FROM PUBLIC;
60+
REVOKE ALL ON FUNCTION pgsp_format_xml() FROM PUBLIC;

pg_show_plans.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,24 @@ static void pgsp_ExecutorStart(QueryDesc *queryDesc, int eflags);
9595
static void pgsp_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
9696
uint64 count, bool execute_once);
9797

98-
/* Enables the extension. */
98+
/* Enables/Disables the extension. */
9999
Datum pg_show_plans_enable(PG_FUNCTION_ARGS);
100-
/* Disables the extension. */
101100
Datum pg_show_plans_disable(PG_FUNCTION_ARGS);
101+
/* Sets query plan output format. */
102+
Datum pgsp_format_text(PG_FUNCTION_ARGS);
103+
Datum pgsp_format_json(PG_FUNCTION_ARGS);
104+
Datum pgsp_format_yaml(PG_FUNCTION_ARGS);
105+
Datum pgsp_format_xml(PG_FUNCTION_ARGS);
102106
/* Show query plans of all the currently running statements. */
103107
Datum pg_show_plans(PG_FUNCTION_ARGS);
104108

105-
PG_FUNCTION_INFO_V1(pg_show_plans);
106109
PG_FUNCTION_INFO_V1(pg_show_plans_enable);
107110
PG_FUNCTION_INFO_V1(pg_show_plans_disable);
111+
PG_FUNCTION_INFO_V1(pgsp_format_text);
112+
PG_FUNCTION_INFO_V1(pgsp_format_json);
113+
PG_FUNCTION_INFO_V1(pgsp_format_yaml);
114+
PG_FUNCTION_INFO_V1(pgsp_format_xml);
115+
PG_FUNCTION_INFO_V1(pg_show_plans);
108116

109117
/* Global Variables */
110118
/* Shared extension state. */
@@ -433,6 +441,35 @@ pg_show_plans_disable(PG_FUNCTION_ARGS)
433441
pgsp->is_enabled = false;
434442
PG_RETURN_VOID();
435443
}
444+
445+
Datum
446+
pgsp_format_text(PG_FUNCTION_ARGS)
447+
{
448+
pgsp->plan_format = EXPLAIN_FORMAT_TEXT;
449+
PG_RETURN_VOID();
450+
}
451+
452+
Datum
453+
pgsp_format_json(PG_FUNCTION_ARGS)
454+
{
455+
pgsp->plan_format = EXPLAIN_FORMAT_JSON;
456+
PG_RETURN_VOID();
457+
}
458+
459+
Datum
460+
pgsp_format_yaml(PG_FUNCTION_ARGS)
461+
{
462+
pgsp->plan_format = EXPLAIN_FORMAT_YAML;
463+
PG_RETURN_VOID();
464+
}
465+
466+
Datum
467+
pgsp_format_xml(PG_FUNCTION_ARGS)
468+
{
469+
pgsp->plan_format = EXPLAIN_FORMAT_XML;
470+
PG_RETURN_VOID();
471+
}
472+
436473
Datum
437474
pg_show_plans(PG_FUNCTION_ARGS)
438475
{

0 commit comments

Comments
 (0)