PostgreSQL Source Code git master
explain_state.h File Reference
#include "nodes/parsenodes.h"
#include "nodes/plannodes.h"
#include "parser/parse_node.h"
Include dependency graph for explain_state.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ExplainWorkersState
 
struct  ExplainState
 

Typedefs

typedef enum ExplainSerializeOption ExplainSerializeOption
 
typedef enum ExplainFormat ExplainFormat
 
typedef struct ExplainWorkersState ExplainWorkersState
 
typedef struct ExplainState ExplainState
 
typedef void(* ExplainOptionHandler) (ExplainState *, DefElem *, ParseState *)
 
typedef void(* explain_validate_options_hook_type) (ExplainState *es, List *options, ParseState *pstate)
 

Enumerations

enum  ExplainSerializeOption { EXPLAIN_SERIALIZE_NONE , EXPLAIN_SERIALIZE_TEXT , EXPLAIN_SERIALIZE_BINARY }
 
enum  ExplainFormat { EXPLAIN_FORMAT_TEXT , EXPLAIN_FORMAT_XML , EXPLAIN_FORMAT_JSON , EXPLAIN_FORMAT_YAML }
 

Functions

ExplainStateNewExplainState (void)
 
void ParseExplainOptionList (ExplainState *es, List *options, ParseState *pstate)
 
int GetExplainExtensionId (const char *extension_name)
 
void * GetExplainExtensionState (ExplainState *es, int extension_id)
 
void SetExplainExtensionState (ExplainState *es, int extension_id, void *opaque)
 
void RegisterExtensionExplainOption (const char *option_name, ExplainOptionHandler handler)
 
bool ApplyExtensionExplainOption (ExplainState *es, DefElem *opt, ParseState *pstate)
 

Variables

PGDLLIMPORT explain_validate_options_hook_type explain_validate_options_hook
 

Typedef Documentation

◆ explain_validate_options_hook_type

typedef void(* explain_validate_options_hook_type) (ExplainState *es, List *options, ParseState *pstate)

Definition at line 82 of file explain_state.h.

◆ ExplainFormat

◆ ExplainOptionHandler

typedef void(* ExplainOptionHandler) (ExplainState *, DefElem *, ParseState *)

Definition at line 79 of file explain_state.h.

◆ ExplainSerializeOption

◆ ExplainState

typedef struct ExplainState ExplainState

◆ ExplainWorkersState

Enumeration Type Documentation

◆ ExplainFormat

Enumerator
EXPLAIN_FORMAT_TEXT 
EXPLAIN_FORMAT_XML 
EXPLAIN_FORMAT_JSON 
EXPLAIN_FORMAT_YAML 

Definition at line 27 of file explain_state.h.

28{
ExplainFormat
Definition: explain_state.h:28
@ EXPLAIN_FORMAT_XML
Definition: explain_state.h:30
@ EXPLAIN_FORMAT_YAML
Definition: explain_state.h:32
@ EXPLAIN_FORMAT_TEXT
Definition: explain_state.h:29
@ EXPLAIN_FORMAT_JSON
Definition: explain_state.h:31

◆ ExplainSerializeOption

Enumerator
EXPLAIN_SERIALIZE_NONE 
EXPLAIN_SERIALIZE_TEXT 
EXPLAIN_SERIALIZE_BINARY 

Definition at line 20 of file explain_state.h.

21{
ExplainSerializeOption
Definition: explain_state.h:21
@ EXPLAIN_SERIALIZE_TEXT
Definition: explain_state.h:23
@ EXPLAIN_SERIALIZE_NONE
Definition: explain_state.h:22
@ EXPLAIN_SERIALIZE_BINARY
Definition: explain_state.h:24

Function Documentation

◆ ApplyExtensionExplainOption()

bool ApplyExtensionExplainOption ( ExplainState es,
DefElem opt,
ParseState pstate 
)

Definition at line 364 of file explain_state.c.

365{
366 for (int i = 0; i < ExplainExtensionOptionsAssigned; ++i)
367 {
368 if (strcmp(ExplainExtensionOptionArray[i].option_name,
369 opt->defname) == 0)
370 {
372 return true;
373 }
374 }
375
376 return false;
377}
static ExplainExtensionOption * ExplainExtensionOptionArray
Definition: explain_state.c:53
static int ExplainExtensionOptionsAssigned
Definition: explain_state.c:54
int i
Definition: isn.c:77
char * defname
Definition: parsenodes.h:843
ExplainOptionHandler option_handler
Definition: explain_state.c:46

References DefElem::defname, ExplainExtensionOptionArray, ExplainExtensionOptionsAssigned, i, and ExplainExtensionOption::option_handler.

Referenced by ParseExplainOptionList().

◆ GetExplainExtensionId()

int GetExplainExtensionId ( const char *  extension_name)

Definition at line 221 of file explain_state.c.

222{
223 /* Search for an existing extension by this name; if found, return ID. */
224 for (int i = 0; i < ExplainExtensionNamesAssigned; ++i)
225 if (strcmp(ExplainExtensionNameArray[i], extension_name) == 0)
226 return i;
227
228 /* If there is no array yet, create one. */
229 if (ExplainExtensionNameArray == NULL)
230 {
232 ExplainExtensionNameArray = (const char **)
235 * sizeof(char *));
236 }
237
238 /* If there's an array but it's currently full, expand it. */
240 {
242
243 ExplainExtensionNameArray = (const char **)
244 repalloc(ExplainExtensionNameArray, i * sizeof(char *));
246 }
247
248 /* Assign and return new ID. */
251}
static const char ** ExplainExtensionNameArray
Definition: explain_state.c:49
static int ExplainExtensionNamesAssigned
Definition: explain_state.c:50
static int ExplainExtensionNamesAllocated
Definition: explain_state.c:51
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1610
MemoryContext TopMemoryContext
Definition: mcxt.c:166
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:189

References ExplainExtensionNameArray, ExplainExtensionNamesAllocated, ExplainExtensionNamesAssigned, i, MemoryContextAlloc(), pg_nextpower2_32(), repalloc(), and TopMemoryContext.

Referenced by _PG_init().

◆ GetExplainExtensionState()

void * GetExplainExtensionState ( ExplainState es,
int  extension_id 
)

Definition at line 259 of file explain_state.c.

260{
261 Assert(extension_id >= 0);
262
263 if (extension_id >= es->extension_state_allocated)
264 return NULL;
265
266 return es->extension_state[extension_id];
267}
Assert(PointerIsAligned(start, uint64))
int extension_state_allocated
Definition: explain_state.h:76
void ** extension_state
Definition: explain_state.h:75

References Assert(), ExplainState::extension_state, and ExplainState::extension_state_allocated.

Referenced by overexplain_ensure_options(), overexplain_per_node_hook(), and overexplain_per_plan_hook().

◆ NewExplainState()

ExplainState * NewExplainState ( void  )

Definition at line 61 of file explain_state.c.

62{
64
65 /* Set default options (most fields can be left as zeroes). */
66 es->costs = true;
67 /* Prepare output buffer. */
68 es->str = makeStringInfo();
69
70 return es;
71}
#define palloc0_object(type)
Definition: fe_memutils.h:75
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
StringInfo str
Definition: explain_state.h:46

References ExplainState::costs, makeStringInfo(), palloc0_object, and ExplainState::str.

Referenced by explain_ExecutorEnd(), and ExplainQuery().

◆ ParseExplainOptionList()

void ParseExplainOptionList ( ExplainState es,
List options,
ParseState pstate 
)

Definition at line 77 of file explain_state.c.

78{
79 ListCell *lc;
80 bool timing_set = false;
81 bool buffers_set = false;
82 bool summary_set = false;
83
84 /* Parse options list. */
85 foreach(lc, options)
86 {
87 DefElem *opt = (DefElem *) lfirst(lc);
88
89 if (strcmp(opt->defname, "analyze") == 0)
90 es->analyze = defGetBoolean(opt);
91 else if (strcmp(opt->defname, "verbose") == 0)
92 es->verbose = defGetBoolean(opt);
93 else if (strcmp(opt->defname, "costs") == 0)
94 es->costs = defGetBoolean(opt);
95 else if (strcmp(opt->defname, "buffers") == 0)
96 {
97 buffers_set = true;
98 es->buffers = defGetBoolean(opt);
99 }
100 else if (strcmp(opt->defname, "wal") == 0)
101 es->wal = defGetBoolean(opt);
102 else if (strcmp(opt->defname, "settings") == 0)
103 es->settings = defGetBoolean(opt);
104 else if (strcmp(opt->defname, "generic_plan") == 0)
105 es->generic = defGetBoolean(opt);
106 else if (strcmp(opt->defname, "timing") == 0)
107 {
108 timing_set = true;
109 es->timing = defGetBoolean(opt);
110 }
111 else if (strcmp(opt->defname, "summary") == 0)
112 {
113 summary_set = true;
114 es->summary = defGetBoolean(opt);
115 }
116 else if (strcmp(opt->defname, "memory") == 0)
117 es->memory = defGetBoolean(opt);
118 else if (strcmp(opt->defname, "serialize") == 0)
119 {
120 if (opt->arg)
121 {
122 char *p = defGetString(opt);
123
124 if (strcmp(p, "off") == 0 || strcmp(p, "none") == 0)
126 else if (strcmp(p, "text") == 0)
128 else if (strcmp(p, "binary") == 0)
130 else
132 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
133 errmsg("unrecognized value for %s option \"%s\": \"%s\"",
134 "EXPLAIN", opt->defname, p),
135 parser_errposition(pstate, opt->location)));
136 }
137 else
138 {
139 /* SERIALIZE without an argument is taken as 'text' */
141 }
142 }
143 else if (strcmp(opt->defname, "format") == 0)
144 {
145 char *p = defGetString(opt);
146
147 if (strcmp(p, "text") == 0)
149 else if (strcmp(p, "xml") == 0)
151 else if (strcmp(p, "json") == 0)
153 else if (strcmp(p, "yaml") == 0)
155 else
157 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
158 errmsg("unrecognized value for %s option \"%s\": \"%s\"",
159 "EXPLAIN", opt->defname, p),
160 parser_errposition(pstate, opt->location)));
161 }
162 else if (!ApplyExtensionExplainOption(es, opt, pstate))
164 (errcode(ERRCODE_SYNTAX_ERROR),
165 errmsg("unrecognized %s option \"%s\"",
166 "EXPLAIN", opt->defname),
167 parser_errposition(pstate, opt->location)));
168 }
169
170 /* check that WAL is used with EXPLAIN ANALYZE */
171 if (es->wal && !es->analyze)
173 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
174 errmsg("EXPLAIN option %s requires ANALYZE", "WAL")));
175
176 /* if the timing was not set explicitly, set default value */
177 es->timing = (timing_set) ? es->timing : es->analyze;
178
179 /* if the buffers was not set explicitly, set default value */
180 es->buffers = (buffers_set) ? es->buffers : es->analyze;
181
182 /* check that timing is used with EXPLAIN ANALYZE */
183 if (es->timing && !es->analyze)
185 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
186 errmsg("EXPLAIN option %s requires ANALYZE", "TIMING")));
187
188 /* check that serialize is used with EXPLAIN ANALYZE */
189 if (es->serialize != EXPLAIN_SERIALIZE_NONE && !es->analyze)
191 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
192 errmsg("EXPLAIN option %s requires ANALYZE", "SERIALIZE")));
193
194 /* check that GENERIC_PLAN is not used with EXPLAIN ANALYZE */
195 if (es->generic && es->analyze)
197 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
198 errmsg("%s options %s and %s cannot be used together",
199 "EXPLAIN", "ANALYZE", "GENERIC_PLAN")));
200
201 /* if the summary was not set explicitly, set default value */
202 es->summary = (summary_set) ? es->summary : es->analyze;
203
204 /* plugin specific option validation */
206 (*explain_validate_options_hook) (es, options, pstate);
207}
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
explain_validate_options_hook_type explain_validate_options_hook
Definition: explain_state.c:41
bool ApplyExtensionExplainOption(ExplainState *es, DefElem *opt, ParseState *pstate)
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
#define lfirst(lc)
Definition: pg_list.h:172
static char ** options
ParseLoc location
Definition: parsenodes.h:847
Node * arg
Definition: parsenodes.h:844
ExplainFormat format
Definition: explain_state.h:59
ExplainSerializeOption serialize
Definition: explain_state.h:58

References ExplainState::analyze, ApplyExtensionExplainOption(), DefElem::arg, ExplainState::buffers, ExplainState::costs, defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, EXPLAIN_SERIALIZE_BINARY, EXPLAIN_SERIALIZE_NONE, EXPLAIN_SERIALIZE_TEXT, explain_validate_options_hook, ExplainState::format, ExplainState::generic, if(), lfirst, DefElem::location, ExplainState::memory, options, parser_errposition(), ExplainState::serialize, ExplainState::settings, ExplainState::summary, ExplainState::timing, ExplainState::verbose, and ExplainState::wal.

Referenced by ExplainQuery().

◆ RegisterExtensionExplainOption()

void RegisterExtensionExplainOption ( const char *  option_name,
ExplainOptionHandler  handler 
)

Definition at line 315 of file explain_state.c.

317{
319
320 /* Search for an existing option by this name; if found, update handler. */
321 for (int i = 0; i < ExplainExtensionOptionsAssigned; ++i)
322 {
323 if (strcmp(ExplainExtensionOptionArray[i].option_name,
324 option_name) == 0)
325 {
327 return;
328 }
329 }
330
331 /* If there is no array yet, create one. */
332 if (ExplainExtensionOptionArray == NULL)
333 {
338 * sizeof(char *));
339 }
340
341 /* If there's an array but it's currently full, expand it. */
343 {
345
347 repalloc(ExplainExtensionOptionArray, i * sizeof(char *));
349 }
350
351 /* Assign and return new ID. */
353 exopt->option_name = option_name;
354 exopt->option_handler = handler;
355}
static int ExplainExtensionOptionsAllocated
Definition: explain_state.c:55
const char * option_name
Definition: explain_state.c:45

References ExplainExtensionOptionArray, ExplainExtensionOptionsAllocated, ExplainExtensionOptionsAssigned, i, MemoryContextAlloc(), ExplainExtensionOption::option_handler, ExplainExtensionOption::option_name, pg_nextpower2_32(), repalloc(), and TopMemoryContext.

Referenced by _PG_init().

◆ SetExplainExtensionState()

void SetExplainExtensionState ( ExplainState es,
int  extension_id,
void *  opaque 
)

Definition at line 278 of file explain_state.c.

279{
280 Assert(extension_id >= 0);
281
282 /* If there is no array yet, create one. */
283 if (es->extension_state == NULL)
284 {
286 Max(16, pg_nextpower2_32(extension_id + 1));
287 es->extension_state =
288 palloc0(es->extension_state_allocated * sizeof(void *));
289 }
290
291 /* If there's an array but it's currently full, expand it. */
292 if (extension_id >= es->extension_state_allocated)
293 {
294 int i;
295
296 i = pg_nextpower2_32(extension_id + 1);
299 }
300
301 es->extension_state[extension_id] = opaque;
302}
#define Max(x, y)
Definition: c.h:1010
void * palloc0(Size size)
Definition: mcxt.c:1395
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109

References Assert(), ExplainState::extension_state, ExplainState::extension_state_allocated, i, Max, palloc0(), pg_nextpower2_32(), and repalloc0_array.

Referenced by overexplain_ensure_options().

Variable Documentation

◆ explain_validate_options_hook

PGDLLIMPORT explain_validate_options_hook_type explain_validate_options_hook
extern

Definition at line 41 of file explain_state.c.

Referenced by ParseExplainOptionList().