Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/installcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
strategy:
matrix:
pgversion:
- 18
- 17
- 16
- 15
Expand Down
18 changes: 15 additions & 3 deletions expected/formats_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
select setting::int < 140000 as pg12_13 from pg_settings where name = 'server_version_num';
pg12_13
---------
t
f
(1 row)

-- json output
Expand All @@ -20,24 +20,28 @@ select * from nest();
| "Plan": { +
| "Node Type": "Function Scan", +
| "Parallel Aware": false, +
| "Async Capable": false, +
| "Function Name": "nest", +
| "Alias": "nest", +
| "Startup Cost": 0.25, +
| "Total Cost": 10.25, +
| "Plan Rows": 1000, +
| "Plan Width": 36 +
| "Plan Width": 36, +
| "Disabled": false +
| } +
| ]
1 | [ +
| "Plan": { +
| "Node Type": "Function Scan", +
| "Parallel Aware": false, +
| "Async Capable": false, +
| "Function Name": "pg_show_plans",+
| "Alias": "pg_show_plans", +
| "Startup Cost": 0.00, +
| "Total Cost": 12.50, +
| "Plan Rows": 333, +
| "Plan Width": 36, +
| "Disabled": false, +
| "Filter": "(level >= 0)" +
| } +
| ]
Expand All @@ -57,21 +61,25 @@ select * from nest();
0 | Plan: +
| Node Type: "Function Scan" +
| Parallel Aware: false +
| Async Capable: false +
| Function Name: "nest" +
| Alias: "nest" +
| Startup Cost: 0.25 +
| Total Cost: 10.25 +
| Plan Rows: 1000 +
| Plan Width: 36
| Plan Width: 36 +
| Disabled: false
1 | Plan: +
| Node Type: "Function Scan" +
| Parallel Aware: false +
| Async Capable: false +
| Function Name: "pg_show_plans"+
| Alias: "pg_show_plans" +
| Startup Cost: 0.00 +
| Total Cost: 12.50 +
| Plan Rows: 333 +
| Plan Width: 36 +
| Disabled: false +
| Filter: "(level >= 0)"
(2 rows)

Expand All @@ -90,24 +98,28 @@ select * from nest();
| <Plan> +
| <Node-Type>Function Scan</Node-Type> +
| <Parallel-Aware>false</Parallel-Aware> +
| <Async-Capable>false</Async-Capable> +
| <Function-Name>nest</Function-Name> +
| <Alias>nest</Alias> +
| <Startup-Cost>0.25</Startup-Cost> +
| <Total-Cost>10.25</Total-Cost> +
| <Plan-Rows>1000</Plan-Rows> +
| <Plan-Width>36</Plan-Width> +
| <Disabled>false</Disabled> +
| </Plan> +
| </explain>
1 | <explain xmlns="http://www.postgresql.org/2009/explain">+
| <Plan> +
| <Node-Type>Function Scan</Node-Type> +
| <Parallel-Aware>false</Parallel-Aware> +
| <Async-Capable>false</Async-Capable> +
| <Function-Name>pg_show_plans</Function-Name> +
| <Alias>pg_show_plans</Alias> +
| <Startup-Cost>0.00</Startup-Cost> +
| <Total-Cost>12.50</Total-Cost> +
| <Plan-Rows>333</Plan-Rows> +
| <Plan-Width>36</Plan-Width> +
| <Disabled>false</Disabled> +
| <Filter>(level &gt;= 0)</Filter> +
| </Plan> +
| </explain>
Expand Down
77 changes: 69 additions & 8 deletions pg_show_plans.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include "catalog/pg_authid.h"
#include "commands/explain.h"
#if PG_VERSION_NUM >= 180000
#include "commands/explain_state.h"
#include "commands/explain_format.h"
#endif
#include "fmgr.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
Expand Down Expand Up @@ -103,10 +107,20 @@ static void pgsp_shmem_request(void);
#endif
static void pgsp_shmem_startup(void);
/* Saves query plans to the shared hash table. */
static void pgsp_ExecutorStart(QueryDesc *queryDesc, int eflags);
static
#if PG_VERSION_NUM < 180000
void
#else
bool
#endif
pgsp_ExecutorStart(QueryDesc *queryDesc, int eflags);
/* Keeps track of the nest level. */
static void pgsp_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
uint64 count, bool execute_once);
uint64 count
#if PG_VERSION_NUM < 180000
, bool execute_once
#endif
);

/* Show query plans of all the currently running statements. */
Datum pg_show_plans(PG_FUNCTION_ARGS);
Expand Down Expand Up @@ -455,26 +469,54 @@ pgsp_shmem_startup(void)
LWLockRelease(AddinShmemInitLock);
}

static void
static
#if PG_VERSION_NUM < 180000
void
#else
bool
#endif
pgsp_ExecutorStart(QueryDesc *queryDesc, int eflags)
{
ExplainState *es;
#if PG_VERSION_NUM >= 180000
bool ret_val;
#endif

if (prev_ExecutorStart)
{
#if PG_VERSION_NUM >= 180000
ret_val =
#endif
prev_ExecutorStart(queryDesc, eflags);
}
else
{
#if PG_VERSION_NUM >= 180000
ret_val =
#endif
standard_ExecutorStart(queryDesc, eflags);
}

if (!ensure_cached()) {
ereport(WARNING,
errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("not enough memory to append new query plans"),
errhint("Try increasing 'pg_show_plans.max_plan_length'."));
return;
return
#if PG_VERSION_NUM >= 180000
ret_val
#endif
;
}

if (!pgsp->is_enabled)
return;
{
return
#if PG_VERSION_NUM >= 180000
ret_val
#endif
;
}

es = NewExplainState();
es->format = pgsp->plan_format;
Expand All @@ -484,20 +526,39 @@ pgsp_ExecutorStart(QueryDesc *queryDesc, int eflags)

append_query_plan(es);
pfree(es->str->data);

return
#if PG_VERSION_NUM >= 180000
ret_val
#endif
;
}

static void
pgsp_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
uint64 count, bool execute_once)
uint64 count
#if PG_VERSION_NUM < 180000
, bool execute_once
#endif
)
{
nest_level++;
PG_TRY();
{
/* These functions return *after* the nested quries do. */
if (prev_ExecutorRun)
prev_ExecutorRun(queryDesc, direction, count, execute_once);
prev_ExecutorRun(queryDesc, direction, count
#if PG_VERSION_NUM < 180000
, execute_once);
#else
);
#endif
else
standard_ExecutorRun(queryDesc, direction, count, execute_once);
standard_ExecutorRun(queryDesc, direction, count
#if PG_VERSION_NUM < 180000
, execute_once
#endif
);

nest_level--;
/* Wait for reading to complete, then delete. */
Expand Down