1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*-------------------------------------------------------------------------
*
* pquery.c--
* POSTGRES process query command code
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/pquery.c,v 1.15 1998/02/26 04:36:32 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include "postgres.h"
#include "tcop/tcopdebug.h"
#include "utils/palloc.h"
#include "nodes/nodes.h"
#include "utils/mcxt.h"
#include "miscadmin.h"
#include "utils/portal.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "nodes/plannodes.h"
#include "nodes/execnodes.h"
#include "nodes/memnodes.h"
#include "tcop/dest.h"
#include "executor/execdefs.h"
#include "executor/execdesc.h"
#include "executor/executor.h"
#include "tcop/pquery.h"
#include "commands/command.h"
static char *CreateOperationTag(int operationType);
static void ProcessQueryDesc(QueryDesc *queryDesc);
/* ----------------------------------------------------------------
* CreateQueryDesc
* ----------------------------------------------------------------
*/
QueryDesc *
CreateQueryDesc(Query *parsetree,
Plan *plantree,
CommandDest dest)
{
QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
qd->operation = parsetree->commandType; /* operation */
qd->parsetree = parsetree; /* parse tree */
qd->plantree = plantree; /* plan */
qd->dest = dest; /* output dest */
return qd;
}
/* ----------------------------------------------------------------
* CreateExecutorState
*
* Note: this may someday take parameters -cim 9/18/89
* ----------------------------------------------------------------
*/
EState *
CreateExecutorState(void)
{
EState *state;
extern int NBuffers;
long *refcount;
/* ----------------
* create a new executor state
* ----------------
*/
state = makeNode(EState);
/* ----------------
* initialize the Executor State structure
* ----------------
*/
state->es_direction = ForwardScanDirection;
state->es_range_table = NIL;
state->es_into_relation_descriptor = NULL;
state->es_result_relation_info = NULL;
state->es_param_list_info = NULL;
state->es_param_exec_vals = NULL;
state->es_BaseId = 0;
state->es_tupleTable = NULL;
state->es_junkFilter = NULL;
refcount = (long *) palloc(NBuffers * sizeof(long));
MemSet((char *) refcount, 0, NBuffers * sizeof(long));
state->es_refcount = (int *) refcount;
/* ----------------
* return the executor state structure
* ----------------
*/
return state;
}
/* ----------------------------------------------------------------
* CreateOperationTag
*
* utility to get a string representation of the
* query operation.
* ----------------------------------------------------------------
*/
static char *
CreateOperationTag(int operationType)
{
char *tag;
switch (operationType)
{
case CMD_SELECT:
tag = "SELECT";
break;
case CMD_INSERT:
tag = "INSERT";
break;
case CMD_DELETE:
tag = "DELETE";
break;
case CMD_UPDATE:
tag = "UPDATE";
break;
default:
elog(DEBUG, "CreateOperationTag: unknown operation type %d",
operationType);
tag = NULL;
break;
}
return tag;
}
/* ----------------
* ProcessPortal
* ----------------
*/
void
ProcessPortal(char *portalName,
Query *parseTree,
Plan *plan,
EState *state,
TupleDesc attinfo,
CommandDest dest)
{
Portal portal;
MemoryContext portalContext;
/* ----------------
* convert the current blank portal into the user-specified
* portal and initialize the state and query descriptor.
* ----------------
*/
if (PortalNameIsSpecial(portalName))
elog(ERROR,
"The portal name %s is reserved for internal use",
portalName);
portal = BlankPortalAssignName(portalName);
PortalSetQuery(portal,
CreateQueryDesc(parseTree, plan, dest),
attinfo,
state,
PortalCleanup);
/* ----------------
* now create a new blank portal and switch to it.
* Otherwise, the new named portal will be cleaned.
*
* Note: portals will only be supported within a BEGIN...END
* block in the near future. Later, someone will fix it to
* do what is possible across transaction boundries. -hirohama
* ----------------
*/
portalContext = (MemoryContext)
PortalGetHeapMemory(GetPortalByName(NULL));
MemoryContextSwitchTo(portalContext);
StartPortalAllocMode(DefaultAllocMode, 0);
}
/* ----------------------------------------------------------------
* ProcessQueryDesc
*
* Read the comments for ProcessQuery() below...
* ----------------------------------------------------------------
*/
static void
ProcessQueryDesc(QueryDesc *queryDesc)
{
Query *parseTree;
Plan *plan;
int operation;
char *tag;
EState *state;
TupleDesc attinfo;
bool isRetrieveIntoPortal;
bool isRetrieveIntoRelation;
char *intoName = NULL;
CommandDest dest;
/* ----------------
* get info from the query desc
* ----------------
*/
parseTree = queryDesc->parsetree;
plan = queryDesc->plantree;
operation = queryDesc->operation;
tag = CreateOperationTag(operation);
dest = queryDesc->dest;
/* ----------------
* initialize portal/into relation status
* ----------------
*/
isRetrieveIntoPortal = false;
isRetrieveIntoRelation = false;
if (operation == CMD_SELECT)
{
if (parseTree->isPortal)
{
isRetrieveIntoPortal = true;
intoName = parseTree->into;
if (parseTree->isBinary)
{
/*
* For internal format portals, we change Remote
* (externalized form) to RemoteInternal (internalized
* form)
*/
dest = queryDesc->dest = RemoteInternal;
}
}
else if (parseTree->into != NULL)
{
/* select into table */
isRetrieveIntoRelation = true;
}
}
/* ----------------
* when performing a retrieve into, we override the normal
* communication destination during the processing of the
* the query. This only affects the tuple-output function
* - the correct destination will still see BeginCommand()
* and EndCommand() messages.
* ----------------
*/
if (isRetrieveIntoRelation)
queryDesc->dest = (int) None;
/* ----------------
* create a default executor state..
* ----------------
*/
state = CreateExecutorState();
/* ----------------
* call ExecStart to prepare the plan for execution
* ----------------
*/
attinfo = ExecutorStart(queryDesc, state);
/* ----------------
* report the query's result type information
* back to the front end or to whatever destination
* we're dealing with.
* ----------------
*/
BeginCommand(NULL,
operation,
attinfo,
isRetrieveIntoRelation,
isRetrieveIntoPortal,
tag,
dest);
/* ----------------
* Named portals do not do a "fetch all" initially, so now
* we return since ExecMain has been called with EXEC_START
* to initialize the query plan.
*
* Note: ProcessPortal transforms the current "blank" portal
* into a named portal and creates a new blank portal so
* everything we allocated in the current "blank" memory
* context will be preserved across queries. -cim 2/22/91
* ----------------
*/
if (isRetrieveIntoPortal)
{
PortalExecutorHeapMemory = NULL;
ProcessPortal(intoName,
parseTree,
plan,
state,
attinfo,
dest);
EndCommand(tag, dest);
return;
}
/* ----------------
* Now we get to the important call to ExecutorRun() where we
* actually run the plan..
* ----------------
*/
ExecutorRun(queryDesc, state, EXEC_RUN, 0);
/* save infos for EndCommand */
UpdateCommandInfo(operation, state->es_lastoid, state->es_processed);
/* ----------------
* now, we close down all the scans and free allocated resources...
* with ExecutorEnd()
* ----------------
*/
ExecutorEnd(queryDesc, state);
/* ----------------
* Notify the destination of end of processing.
* ----------------
*/
EndCommand(tag, dest);
}
/* ----------------------------------------------------------------
* ProcessQuery
*
* Execute a plan, the non-parallel version
* ----------------------------------------------------------------
*/
void
ProcessQuery(Query *parsetree,
Plan *plan,
char *argv[],
Oid *typev,
int nargs,
CommandDest dest)
{
QueryDesc *queryDesc;
extern int dontExecute; /* from postgres.c */
extern void print_plan(Plan *p, Query *parsetree); /* from print.c */
queryDesc = CreateQueryDesc(parsetree, plan, dest);
if (dontExecute)
{
/* don't execute it, just show the query plan */
print_plan(plan, parsetree);
}
else
ProcessQueryDesc(queryDesc);
}
|