@@ -125,45 +125,42 @@ void
125
125
ExplainQuery (ExplainStmt * stmt , const char * queryString ,
126
126
ParamListInfo params , DestReceiver * dest )
127
127
{
128
- ExplainState es ;
128
+ ExplainState * es = NewExplainState () ;
129
129
TupOutputState * tstate ;
130
130
List * rewritten ;
131
131
ListCell * lc ;
132
132
bool timing_set = false;
133
133
134
- /* Initialize ExplainState. */
135
- ExplainInitState (& es );
136
-
137
134
/* Parse options list. */
138
135
foreach (lc , stmt -> options )
139
136
{
140
137
DefElem * opt = (DefElem * ) lfirst (lc );
141
138
142
139
if (strcmp (opt -> defname , "analyze" ) == 0 )
143
- es . analyze = defGetBoolean (opt );
140
+ es -> analyze = defGetBoolean (opt );
144
141
else if (strcmp (opt -> defname , "verbose" ) == 0 )
145
- es . verbose = defGetBoolean (opt );
142
+ es -> verbose = defGetBoolean (opt );
146
143
else if (strcmp (opt -> defname , "costs" ) == 0 )
147
- es . costs = defGetBoolean (opt );
144
+ es -> costs = defGetBoolean (opt );
148
145
else if (strcmp (opt -> defname , "buffers" ) == 0 )
149
- es . buffers = defGetBoolean (opt );
146
+ es -> buffers = defGetBoolean (opt );
150
147
else if (strcmp (opt -> defname , "timing" ) == 0 )
151
148
{
152
149
timing_set = true;
153
- es . timing = defGetBoolean (opt );
150
+ es -> timing = defGetBoolean (opt );
154
151
}
155
152
else if (strcmp (opt -> defname , "format" ) == 0 )
156
153
{
157
154
char * p = defGetString (opt );
158
155
159
156
if (strcmp (p , "text" ) == 0 )
160
- es . format = EXPLAIN_FORMAT_TEXT ;
157
+ es -> format = EXPLAIN_FORMAT_TEXT ;
161
158
else if (strcmp (p , "xml" ) == 0 )
162
- es . format = EXPLAIN_FORMAT_XML ;
159
+ es -> format = EXPLAIN_FORMAT_XML ;
163
160
else if (strcmp (p , "json" ) == 0 )
164
- es . format = EXPLAIN_FORMAT_JSON ;
161
+ es -> format = EXPLAIN_FORMAT_JSON ;
165
162
else if (strcmp (p , "yaml" ) == 0 )
166
- es . format = EXPLAIN_FORMAT_YAML ;
163
+ es -> format = EXPLAIN_FORMAT_YAML ;
167
164
else
168
165
ereport (ERROR ,
169
166
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -177,22 +174,22 @@ ExplainQuery(ExplainStmt *stmt, const char *queryString,
177
174
opt -> defname )));
178
175
}
179
176
180
- if (es . buffers && !es . analyze )
177
+ if (es -> buffers && !es -> analyze )
181
178
ereport (ERROR ,
182
179
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
183
180
errmsg ("EXPLAIN option BUFFERS requires ANALYZE" )));
184
181
185
182
/* if the timing was not set explicitly, set default value */
186
- es . timing = (timing_set ) ? es . timing : es . analyze ;
183
+ es -> timing = (timing_set ) ? es -> timing : es -> analyze ;
187
184
188
185
/* check that timing is used with EXPLAIN ANALYZE */
189
- if (es . timing && !es . analyze )
186
+ if (es -> timing && !es -> analyze )
190
187
ereport (ERROR ,
191
188
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
192
189
errmsg ("EXPLAIN option TIMING requires ANALYZE" )));
193
190
194
191
/* currently, summary option is not exposed to users; just set it */
195
- es . summary = es . analyze ;
192
+ es -> summary = es -> analyze ;
196
193
197
194
/*
198
195
* Parse analysis was done already, but we still have to run the rule
@@ -210,16 +207,16 @@ ExplainQuery(ExplainStmt *stmt, const char *queryString,
210
207
rewritten = QueryRewrite ((Query * ) copyObject (stmt -> query ));
211
208
212
209
/* emit opening boilerplate */
213
- ExplainBeginOutput (& es );
210
+ ExplainBeginOutput (es );
214
211
215
212
if (rewritten == NIL )
216
213
{
217
214
/*
218
215
* In the case of an INSTEAD NOTHING, tell at least that. But in
219
216
* non-text format, the output is delimited, so this isn't necessary.
220
217
*/
221
- if (es . format == EXPLAIN_FORMAT_TEXT )
222
- appendStringInfoString (es . str , "Query rewrites to nothing\n" );
218
+ if (es -> format == EXPLAIN_FORMAT_TEXT )
219
+ appendStringInfoString (es -> str , "Query rewrites to nothing\n" );
223
220
}
224
221
else
225
222
{
@@ -228,41 +225,44 @@ ExplainQuery(ExplainStmt *stmt, const char *queryString,
228
225
/* Explain every plan */
229
226
foreach (l , rewritten )
230
227
{
231
- ExplainOneQuery ((Query * ) lfirst (l ), NULL , & es ,
228
+ ExplainOneQuery ((Query * ) lfirst (l ), NULL , es ,
232
229
queryString , params );
233
230
234
231
/* Separate plans with an appropriate separator */
235
232
if (lnext (l ) != NULL )
236
- ExplainSeparatePlans (& es );
233
+ ExplainSeparatePlans (es );
237
234
}
238
235
}
239
236
240
237
/* emit closing boilerplate */
241
- ExplainEndOutput (& es );
242
- Assert (es . indent == 0 );
238
+ ExplainEndOutput (es );
239
+ Assert (es -> indent == 0 );
243
240
244
241
/* output tuples */
245
242
tstate = begin_tup_output_tupdesc (dest , ExplainResultDesc (stmt ));
246
- if (es . format == EXPLAIN_FORMAT_TEXT )
247
- do_text_output_multiline (tstate , es . str -> data );
243
+ if (es -> format == EXPLAIN_FORMAT_TEXT )
244
+ do_text_output_multiline (tstate , es -> str -> data );
248
245
else
249
- do_text_output_oneline (tstate , es . str -> data );
246
+ do_text_output_oneline (tstate , es -> str -> data );
250
247
end_tup_output (tstate );
251
248
252
- pfree (es . str -> data );
249
+ pfree (es -> str -> data );
253
250
}
254
251
255
252
/*
256
- * Initialize ExplainState.
253
+ * Create a new ExplainState struct initialized with default options .
257
254
*/
258
- void
259
- ExplainInitState ( ExplainState * es )
255
+ ExplainState *
256
+ NewExplainState ( void )
260
257
{
261
- /* Set default options. */
262
- memset (es , 0 , sizeof (ExplainState ));
258
+ ExplainState * es = (ExplainState * ) palloc0 (sizeof (ExplainState ));
259
+
260
+ /* Set default options (most fields can be left as zeroes). */
263
261
es -> costs = true;
264
262
/* Prepare output buffer. */
265
263
es -> str = makeStringInfo ();
264
+
265
+ return es ;
266
266
}
267
267
268
268
/*
0 commit comments