]> granicus.if.org Git - postgresql/blob - src/backend/executor/execMain.c
Remove very ancient tuple-counting infrastructure (IncrRetrieved() and
[postgresql] / src / backend / executor / execMain.c
1 /*-------------------------------------------------------------------------
2  *
3  * execMain.c
4  *        top level executor interface routines
5  *
6  * INTERFACE ROUTINES
7  *      ExecutorStart()
8  *      ExecutorRun()
9  *      ExecutorEnd()
10  *
11  *      The old ExecutorMain() has been replaced by ExecutorStart(),
12  *      ExecutorRun() and ExecutorEnd()
13  *
14  *      These three procedures are the external interfaces to the executor.
15  *      In each case, the query descriptor is required as an argument.
16  *
17  *      ExecutorStart() must be called at the beginning of execution of any
18  *      query plan and ExecutorEnd() should always be called at the end of
19  *      execution of a plan.
20  *
21  *      ExecutorRun accepts direction and count arguments that specify whether
22  *      the plan is to be executed forwards, backwards, and for how many tuples.
23  *
24  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
25  * Portions Copyright (c) 1994, Regents of the University of California
26  *
27  *
28  * IDENTIFICATION
29  *        $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.331 2009/10/08 22:34:57 tgl Exp $
30  *
31  *-------------------------------------------------------------------------
32  */
33 #include "postgres.h"
34
35 #include "access/heapam.h"
36 #include "access/reloptions.h"
37 #include "access/sysattr.h"
38 #include "access/transam.h"
39 #include "access/xact.h"
40 #include "catalog/heap.h"
41 #include "catalog/namespace.h"
42 #include "catalog/toasting.h"
43 #include "commands/tablespace.h"
44 #include "commands/trigger.h"
45 #include "executor/execdebug.h"
46 #include "executor/instrument.h"
47 #include "executor/nodeSubplan.h"
48 #include "miscadmin.h"
49 #include "nodes/nodeFuncs.h"
50 #include "optimizer/clauses.h"
51 #include "parser/parse_clause.h"
52 #include "parser/parsetree.h"
53 #include "storage/bufmgr.h"
54 #include "storage/lmgr.h"
55 #include "storage/smgr.h"
56 #include "utils/acl.h"
57 #include "utils/builtins.h"
58 #include "utils/lsyscache.h"
59 #include "utils/memutils.h"
60 #include "utils/snapmgr.h"
61 #include "utils/tqual.h"
62
63
64 /* Hooks for plugins to get control in ExecutorStart/Run/End() */
65 ExecutorStart_hook_type ExecutorStart_hook = NULL;
66 ExecutorRun_hook_type ExecutorRun_hook = NULL;
67 ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
68
69 typedef struct evalPlanQual
70 {
71         Index           rti;
72         EState     *estate;
73         PlanState  *planstate;
74         struct evalPlanQual *next;      /* stack of active PlanQual plans */
75         struct evalPlanQual *free;      /* list of free PlanQual plans */
76 } evalPlanQual;
77
78 /* decls for local routines only used within this module */
79 static void InitPlan(QueryDesc *queryDesc, int eflags);
80 static void ExecCheckPlanOutput(Relation resultRel, List *targetList);
81 static void ExecEndPlan(PlanState *planstate, EState *estate);
82 static void ExecutePlan(EState *estate, PlanState *planstate,
83                         CmdType operation,
84                         long numberTuples,
85                         ScanDirection direction,
86                         DestReceiver *dest);
87 static void ExecSelect(TupleTableSlot *slot,
88                    DestReceiver *dest, EState *estate);
89 static void ExecInsert(TupleTableSlot *slot, ItemPointer tupleid,
90                    TupleTableSlot *planSlot,
91                    DestReceiver *dest, EState *estate);
92 static void ExecDelete(ItemPointer tupleid,
93                    TupleTableSlot *planSlot,
94                    DestReceiver *dest, EState *estate);
95 static void ExecUpdate(TupleTableSlot *slot, ItemPointer tupleid,
96                    TupleTableSlot *planSlot,
97                    DestReceiver *dest, EState *estate);
98 static void ExecProcessReturning(ProjectionInfo *projectReturning,
99                                          TupleTableSlot *tupleSlot,
100                                          TupleTableSlot *planSlot,
101                                          DestReceiver *dest);
102 static TupleTableSlot *EvalPlanQualNext(EState *estate);
103 static void EndEvalPlanQual(EState *estate);
104 static void ExecCheckRTPerms(List *rangeTable);
105 static void ExecCheckRTEPerms(RangeTblEntry *rte);
106 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
107 static void EvalPlanQualStart(evalPlanQual *epq, EState *estate,
108                                   evalPlanQual *priorepq);
109 static void EvalPlanQualStop(evalPlanQual *epq);
110 static void OpenIntoRel(QueryDesc *queryDesc);
111 static void CloseIntoRel(QueryDesc *queryDesc);
112 static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
113 static void intorel_receive(TupleTableSlot *slot, DestReceiver *self);
114 static void intorel_shutdown(DestReceiver *self);
115 static void intorel_destroy(DestReceiver *self);
116
117 /* end of local decls */
118
119
120 /* ----------------------------------------------------------------
121  *              ExecutorStart
122  *
123  *              This routine must be called at the beginning of any execution of any
124  *              query plan
125  *
126  * Takes a QueryDesc previously created by CreateQueryDesc (it's not real
127  * clear why we bother to separate the two functions, but...).  The tupDesc
128  * field of the QueryDesc is filled in to describe the tuples that will be
129  * returned, and the internal fields (estate and planstate) are set up.
130  *
131  * eflags contains flag bits as described in executor.h.
132  *
133  * NB: the CurrentMemoryContext when this is called will become the parent
134  * of the per-query context used for this Executor invocation.
135  *
136  * We provide a function hook variable that lets loadable plugins
137  * get control when ExecutorStart is called.  Such a plugin would
138  * normally call standard_ExecutorStart().
139  *
140  * ----------------------------------------------------------------
141  */
142 void
143 ExecutorStart(QueryDesc *queryDesc, int eflags)
144 {
145         if (ExecutorStart_hook)
146                 (*ExecutorStart_hook) (queryDesc, eflags);
147         else
148                 standard_ExecutorStart(queryDesc, eflags);
149 }
150
151 void
152 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
153 {
154         EState     *estate;
155         MemoryContext oldcontext;
156
157         /* sanity checks: queryDesc must not be started already */
158         Assert(queryDesc != NULL);
159         Assert(queryDesc->estate == NULL);
160
161         /*
162          * If the transaction is read-only, we need to check if any writes are
163          * planned to non-temporary tables.  EXPLAIN is considered read-only.
164          */
165         if (XactReadOnly && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
166                 ExecCheckXactReadOnly(queryDesc->plannedstmt);
167
168         /*
169          * Build EState, switch into per-query memory context for startup.
170          */
171         estate = CreateExecutorState();
172         queryDesc->estate = estate;
173
174         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
175
176         /*
177          * Fill in parameters, if any, from queryDesc
178          */
179         estate->es_param_list_info = queryDesc->params;
180
181         if (queryDesc->plannedstmt->nParamExec > 0)
182                 estate->es_param_exec_vals = (ParamExecData *)
183                         palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData));
184
185         /*
186          * If non-read-only query, set the command ID to mark output tuples with
187          */
188         switch (queryDesc->operation)
189         {
190                 case CMD_SELECT:
191                         /* SELECT INTO and SELECT FOR UPDATE/SHARE need to mark tuples */
192                         if (queryDesc->plannedstmt->intoClause != NULL ||
193                                 queryDesc->plannedstmt->rowMarks != NIL)
194                                 estate->es_output_cid = GetCurrentCommandId(true);
195                         break;
196
197                 case CMD_INSERT:
198                 case CMD_DELETE:
199                 case CMD_UPDATE:
200                         estate->es_output_cid = GetCurrentCommandId(true);
201                         break;
202
203                 default:
204                         elog(ERROR, "unrecognized operation code: %d",
205                                  (int) queryDesc->operation);
206                         break;
207         }
208
209         /*
210          * Copy other important information into the EState
211          */
212         estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
213         estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
214         estate->es_instrument = queryDesc->doInstrument;
215
216         /*
217          * Initialize the plan state tree
218          */
219         InitPlan(queryDesc, eflags);
220
221         MemoryContextSwitchTo(oldcontext);
222 }
223
224 /* ----------------------------------------------------------------
225  *              ExecutorRun
226  *
227  *              This is the main routine of the executor module. It accepts
228  *              the query descriptor from the traffic cop and executes the
229  *              query plan.
230  *
231  *              ExecutorStart must have been called already.
232  *
233  *              If direction is NoMovementScanDirection then nothing is done
234  *              except to start up/shut down the destination.  Otherwise,
235  *              we retrieve up to 'count' tuples in the specified direction.
236  *
237  *              Note: count = 0 is interpreted as no portal limit, i.e., run to
238  *              completion.
239  *
240  *              There is no return value, but output tuples (if any) are sent to
241  *              the destination receiver specified in the QueryDesc; and the number
242  *              of tuples processed at the top level can be found in
243  *              estate->es_processed.
244  *
245  *              We provide a function hook variable that lets loadable plugins
246  *              get control when ExecutorRun is called.  Such a plugin would
247  *              normally call standard_ExecutorRun().
248  *
249  * ----------------------------------------------------------------
250  */
251 void
252 ExecutorRun(QueryDesc *queryDesc,
253                         ScanDirection direction, long count)
254 {
255         if (ExecutorRun_hook)
256                 (*ExecutorRun_hook) (queryDesc, direction, count);
257         else
258                 standard_ExecutorRun(queryDesc, direction, count);
259 }
260
261 void
262 standard_ExecutorRun(QueryDesc *queryDesc,
263                                          ScanDirection direction, long count)
264 {
265         EState     *estate;
266         CmdType         operation;
267         DestReceiver *dest;
268         bool            sendTuples;
269         MemoryContext oldcontext;
270
271         /* sanity checks */
272         Assert(queryDesc != NULL);
273
274         estate = queryDesc->estate;
275
276         Assert(estate != NULL);
277
278         /*
279          * Switch into per-query memory context
280          */
281         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
282
283         /* Allow instrumentation of ExecutorRun overall runtime */
284         if (queryDesc->totaltime)
285                 InstrStartNode(queryDesc->totaltime);
286
287         /*
288          * extract information from the query descriptor and the query feature.
289          */
290         operation = queryDesc->operation;
291         dest = queryDesc->dest;
292
293         /*
294          * startup tuple receiver, if we will be emitting tuples
295          */
296         estate->es_processed = 0;
297         estate->es_lastoid = InvalidOid;
298
299         sendTuples = (operation == CMD_SELECT ||
300                                   queryDesc->plannedstmt->returningLists);
301
302         if (sendTuples)
303                 (*dest->rStartup) (dest, operation, queryDesc->tupDesc);
304
305         /*
306          * run plan
307          */
308         if (!ScanDirectionIsNoMovement(direction))
309                 ExecutePlan(estate,
310                                         queryDesc->planstate,
311                                         operation,
312                                         count,
313                                         direction,
314                                         dest);
315
316         /*
317          * shutdown tuple receiver, if we started it
318          */
319         if (sendTuples)
320                 (*dest->rShutdown) (dest);
321
322         if (queryDesc->totaltime)
323                 InstrStopNode(queryDesc->totaltime, estate->es_processed);
324
325         MemoryContextSwitchTo(oldcontext);
326 }
327
328 /* ----------------------------------------------------------------
329  *              ExecutorEnd
330  *
331  *              This routine must be called at the end of execution of any
332  *              query plan
333  *
334  *              We provide a function hook variable that lets loadable plugins
335  *              get control when ExecutorEnd is called.  Such a plugin would
336  *              normally call standard_ExecutorEnd().
337  *
338  * ----------------------------------------------------------------
339  */
340 void
341 ExecutorEnd(QueryDesc *queryDesc)
342 {
343         if (ExecutorEnd_hook)
344                 (*ExecutorEnd_hook) (queryDesc);
345         else
346                 standard_ExecutorEnd(queryDesc);
347 }
348
349 void
350 standard_ExecutorEnd(QueryDesc *queryDesc)
351 {
352         EState     *estate;
353         MemoryContext oldcontext;
354
355         /* sanity checks */
356         Assert(queryDesc != NULL);
357
358         estate = queryDesc->estate;
359
360         Assert(estate != NULL);
361
362         /*
363          * Switch into per-query memory context to run ExecEndPlan
364          */
365         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
366
367         ExecEndPlan(queryDesc->planstate, estate);
368
369         /*
370          * Close the SELECT INTO relation if any
371          */
372         if (estate->es_select_into)
373                 CloseIntoRel(queryDesc);
374
375         /* do away with our snapshots */
376         UnregisterSnapshot(estate->es_snapshot);
377         UnregisterSnapshot(estate->es_crosscheck_snapshot);
378
379         /*
380          * Must switch out of context before destroying it
381          */
382         MemoryContextSwitchTo(oldcontext);
383
384         /*
385          * Release EState and per-query memory context.  This should release
386          * everything the executor has allocated.
387          */
388         FreeExecutorState(estate);
389
390         /* Reset queryDesc fields that no longer point to anything */
391         queryDesc->tupDesc = NULL;
392         queryDesc->estate = NULL;
393         queryDesc->planstate = NULL;
394         queryDesc->totaltime = NULL;
395 }
396
397 /* ----------------------------------------------------------------
398  *              ExecutorRewind
399  *
400  *              This routine may be called on an open queryDesc to rewind it
401  *              to the start.
402  * ----------------------------------------------------------------
403  */
404 void
405 ExecutorRewind(QueryDesc *queryDesc)
406 {
407         EState     *estate;
408         MemoryContext oldcontext;
409
410         /* sanity checks */
411         Assert(queryDesc != NULL);
412
413         estate = queryDesc->estate;
414
415         Assert(estate != NULL);
416
417         /* It's probably not sensible to rescan updating queries */
418         Assert(queryDesc->operation == CMD_SELECT);
419
420         /*
421          * Switch into per-query memory context
422          */
423         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
424
425         /*
426          * rescan plan
427          */
428         ExecReScan(queryDesc->planstate, NULL);
429
430         MemoryContextSwitchTo(oldcontext);
431 }
432
433
434 /*
435  * ExecCheckRTPerms
436  *              Check access permissions for all relations listed in a range table.
437  */
438 static void
439 ExecCheckRTPerms(List *rangeTable)
440 {
441         ListCell   *l;
442
443         foreach(l, rangeTable)
444         {
445                 ExecCheckRTEPerms((RangeTblEntry *) lfirst(l));
446         }
447 }
448
449 /*
450  * ExecCheckRTEPerms
451  *              Check access permissions for a single RTE.
452  */
453 static void
454 ExecCheckRTEPerms(RangeTblEntry *rte)
455 {
456         AclMode         requiredPerms;
457         AclMode         relPerms;
458         AclMode         remainingPerms;
459         Oid                     relOid;
460         Oid                     userid;
461         Bitmapset  *tmpset;
462         int                     col;
463
464         /*
465          * Only plain-relation RTEs need to be checked here.  Function RTEs are
466          * checked by init_fcache when the function is prepared for execution.
467          * Join, subquery, and special RTEs need no checks.
468          */
469         if (rte->rtekind != RTE_RELATION)
470                 return;
471
472         /*
473          * No work if requiredPerms is empty.
474          */
475         requiredPerms = rte->requiredPerms;
476         if (requiredPerms == 0)
477                 return;
478
479         relOid = rte->relid;
480
481         /*
482          * userid to check as: current user unless we have a setuid indication.
483          *
484          * Note: GetUserId() is presently fast enough that there's no harm in
485          * calling it separately for each RTE.  If that stops being true, we could
486          * call it once in ExecCheckRTPerms and pass the userid down from there.
487          * But for now, no need for the extra clutter.
488          */
489         userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
490
491         /*
492          * We must have *all* the requiredPerms bits, but some of the bits can be
493          * satisfied from column-level rather than relation-level permissions.
494          * First, remove any bits that are satisfied by relation permissions.
495          */
496         relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
497         remainingPerms = requiredPerms & ~relPerms;
498         if (remainingPerms != 0)
499         {
500                 /*
501                  * If we lack any permissions that exist only as relation permissions,
502                  * we can fail straight away.
503                  */
504                 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
505                         aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
506                                                    get_rel_name(relOid));
507
508                 /*
509                  * Check to see if we have the needed privileges at column level.
510                  *
511                  * Note: failures just report a table-level error; it would be nicer
512                  * to report a column-level error if we have some but not all of the
513                  * column privileges.
514                  */
515                 if (remainingPerms & ACL_SELECT)
516                 {
517                         /*
518                          * When the query doesn't explicitly reference any columns (for
519                          * example, SELECT COUNT(*) FROM table), allow the query if we
520                          * have SELECT on any column of the rel, as per SQL spec.
521                          */
522                         if (bms_is_empty(rte->selectedCols))
523                         {
524                                 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
525                                                                                           ACLMASK_ANY) != ACLCHECK_OK)
526                                         aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
527                                                                    get_rel_name(relOid));
528                         }
529
530                         tmpset = bms_copy(rte->selectedCols);
531                         while ((col = bms_first_member(tmpset)) >= 0)
532                         {
533                                 /* remove the column number offset */
534                                 col += FirstLowInvalidHeapAttributeNumber;
535                                 if (col == InvalidAttrNumber)
536                                 {
537                                         /* Whole-row reference, must have priv on all cols */
538                                         if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
539                                                                                                   ACLMASK_ALL) != ACLCHECK_OK)
540                                                 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
541                                                                            get_rel_name(relOid));
542                                 }
543                                 else
544                                 {
545                                         if (pg_attribute_aclcheck(relOid, col, userid, ACL_SELECT)
546                                                 != ACLCHECK_OK)
547                                                 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
548                                                                            get_rel_name(relOid));
549                                 }
550                         }
551                         bms_free(tmpset);
552                 }
553
554                 /*
555                  * Basically the same for the mod columns, with either INSERT or
556                  * UPDATE privilege as specified by remainingPerms.
557                  */
558                 remainingPerms &= ~ACL_SELECT;
559                 if (remainingPerms != 0)
560                 {
561                         /*
562                          * When the query doesn't explicitly change any columns, allow the
563                          * query if we have permission on any column of the rel.  This is
564                          * to handle SELECT FOR UPDATE as well as possible corner cases in
565                          * INSERT and UPDATE.
566                          */
567                         if (bms_is_empty(rte->modifiedCols))
568                         {
569                                 if (pg_attribute_aclcheck_all(relOid, userid, remainingPerms,
570                                                                                           ACLMASK_ANY) != ACLCHECK_OK)
571                                         aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
572                                                                    get_rel_name(relOid));
573                         }
574
575                         tmpset = bms_copy(rte->modifiedCols);
576                         while ((col = bms_first_member(tmpset)) >= 0)
577                         {
578                                 /* remove the column number offset */
579                                 col += FirstLowInvalidHeapAttributeNumber;
580                                 if (col == InvalidAttrNumber)
581                                 {
582                                         /* whole-row reference can't happen here */
583                                         elog(ERROR, "whole-row update is not implemented");
584                                 }
585                                 else
586                                 {
587                                         if (pg_attribute_aclcheck(relOid, col, userid, remainingPerms)
588                                                 != ACLCHECK_OK)
589                                                 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
590                                                                            get_rel_name(relOid));
591                                 }
592                         }
593                         bms_free(tmpset);
594                 }
595         }
596 }
597
598 /*
599  * Check that the query does not imply any writes to non-temp tables.
600  */
601 static void
602 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
603 {
604         ListCell   *l;
605
606         /*
607          * CREATE TABLE AS or SELECT INTO?
608          *
609          * XXX should we allow this if the destination is temp?
610          */
611         if (plannedstmt->intoClause != NULL)
612                 goto fail;
613
614         /* Fail if write permissions are requested on any non-temp table */
615         foreach(l, plannedstmt->rtable)
616         {
617                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
618
619                 if (rte->rtekind != RTE_RELATION)
620                         continue;
621
622                 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
623                         continue;
624
625                 if (isTempNamespace(get_rel_namespace(rte->relid)))
626                         continue;
627
628                 goto fail;
629         }
630
631         return;
632
633 fail:
634         ereport(ERROR,
635                         (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
636                          errmsg("transaction is read-only")));
637 }
638
639
640 /* ----------------------------------------------------------------
641  *              InitPlan
642  *
643  *              Initializes the query plan: open files, allocate storage
644  *              and start up the rule manager
645  * ----------------------------------------------------------------
646  */
647 static void
648 InitPlan(QueryDesc *queryDesc, int eflags)
649 {
650         CmdType         operation = queryDesc->operation;
651         PlannedStmt *plannedstmt = queryDesc->plannedstmt;
652         Plan       *plan = plannedstmt->planTree;
653         List       *rangeTable = plannedstmt->rtable;
654         EState     *estate = queryDesc->estate;
655         PlanState  *planstate;
656         TupleDesc       tupType;
657         ListCell   *l;
658         int                     i;
659
660         /*
661          * Do permissions checks
662          */
663         ExecCheckRTPerms(rangeTable);
664
665         /*
666          * initialize the node's execution state
667          */
668         estate->es_range_table = rangeTable;
669
670         /*
671          * initialize result relation stuff
672          */
673         if (plannedstmt->resultRelations)
674         {
675                 List       *resultRelations = plannedstmt->resultRelations;
676                 int                     numResultRelations = list_length(resultRelations);
677                 ResultRelInfo *resultRelInfos;
678                 ResultRelInfo *resultRelInfo;
679
680                 resultRelInfos = (ResultRelInfo *)
681                         palloc(numResultRelations * sizeof(ResultRelInfo));
682                 resultRelInfo = resultRelInfos;
683                 foreach(l, resultRelations)
684                 {
685                         Index           resultRelationIndex = lfirst_int(l);
686                         Oid                     resultRelationOid;
687                         Relation        resultRelation;
688
689                         resultRelationOid = getrelid(resultRelationIndex, rangeTable);
690                         resultRelation = heap_open(resultRelationOid, RowExclusiveLock);
691                         InitResultRelInfo(resultRelInfo,
692                                                           resultRelation,
693                                                           resultRelationIndex,
694                                                           operation,
695                                                           estate->es_instrument);
696                         resultRelInfo++;
697                 }
698                 estate->es_result_relations = resultRelInfos;
699                 estate->es_num_result_relations = numResultRelations;
700                 /* Initialize to first or only result rel */
701                 estate->es_result_relation_info = resultRelInfos;
702         }
703         else
704         {
705                 /*
706                  * if no result relation, then set state appropriately
707                  */
708                 estate->es_result_relations = NULL;
709                 estate->es_num_result_relations = 0;
710                 estate->es_result_relation_info = NULL;
711         }
712
713         /*
714          * Detect whether we're doing SELECT INTO.  If so, set the es_into_oids
715          * flag appropriately so that the plan tree will be initialized with the
716          * correct tuple descriptors.  (Other SELECT INTO stuff comes later.)
717          */
718         estate->es_select_into = false;
719         if (operation == CMD_SELECT && plannedstmt->intoClause != NULL)
720         {
721                 estate->es_select_into = true;
722                 estate->es_into_oids = interpretOidsOption(plannedstmt->intoClause->options);
723         }
724
725         /*
726          * Have to lock relations selected FOR UPDATE/FOR SHARE before we
727          * initialize the plan tree, else we'd be doing a lock upgrade. While we
728          * are at it, build the ExecRowMark list.
729          */
730         estate->es_rowMarks = NIL;
731         foreach(l, plannedstmt->rowMarks)
732         {
733                 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
734                 Oid                     relid;
735                 Relation        relation;
736                 ExecRowMark *erm;
737
738                 /* ignore "parent" rowmarks; they are irrelevant at runtime */
739                 if (rc->isParent)
740                         continue;
741
742                 relid = getrelid(rc->rti, rangeTable);
743                 relation = heap_open(relid, RowShareLock);
744                 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
745                 erm->relation = relation;
746                 erm->rti = rc->rti;
747                 erm->prti = rc->prti;
748                 erm->forUpdate = rc->forUpdate;
749                 erm->noWait = rc->noWait;
750                 /* We'll locate the junk attrs below */
751                 erm->ctidAttNo = InvalidAttrNumber;
752                 erm->toidAttNo = InvalidAttrNumber;
753                 ItemPointerSetInvalid(&(erm->curCtid));
754                 estate->es_rowMarks = lappend(estate->es_rowMarks, erm);
755         }
756
757         /*
758          * Initialize the executor's tuple table.  Also, if it's not a SELECT,
759          * set up a tuple table slot for use for trigger output tuples.
760          */
761         estate->es_tupleTable = NIL;
762         if (operation != CMD_SELECT)
763                 estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
764
765         /* mark EvalPlanQual not active */
766         estate->es_plannedstmt = plannedstmt;
767         estate->es_evalPlanQual = NULL;
768         estate->es_evTupleNull = NULL;
769         estate->es_evTuple = NULL;
770         estate->es_useEvalPlan = false;
771
772         /*
773          * Initialize private state information for each SubPlan.  We must do this
774          * before running ExecInitNode on the main query tree, since
775          * ExecInitSubPlan expects to be able to find these entries.
776          */
777         Assert(estate->es_subplanstates == NIL);
778         i = 1;                                          /* subplan indices count from 1 */
779         foreach(l, plannedstmt->subplans)
780         {
781                 Plan       *subplan = (Plan *) lfirst(l);
782                 PlanState  *subplanstate;
783                 int                     sp_eflags;
784
785                 /*
786                  * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
787                  * it is a parameterless subplan (not initplan), we suggest that it be
788                  * prepared to handle REWIND efficiently; otherwise there is no need.
789                  */
790                 sp_eflags = eflags & EXEC_FLAG_EXPLAIN_ONLY;
791                 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
792                         sp_eflags |= EXEC_FLAG_REWIND;
793
794                 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
795
796                 estate->es_subplanstates = lappend(estate->es_subplanstates,
797                                                                                    subplanstate);
798
799                 i++;
800         }
801
802         /*
803          * Initialize the private state information for all the nodes in the query
804          * tree.  This opens files, allocates storage and leaves us ready to start
805          * processing tuples.
806          */
807         planstate = ExecInitNode(plan, estate, eflags);
808
809         /*
810          * Get the tuple descriptor describing the type of tuples to return. (this
811          * is especially important if we are creating a relation with "SELECT
812          * INTO")
813          */
814         tupType = ExecGetResultType(planstate);
815
816         /*
817          * Initialize the junk filter if needed.  SELECT and INSERT queries need a
818          * filter if there are any junk attrs in the tlist.  UPDATE and DELETE
819          * always need a filter, since there's always a junk 'ctid' attribute
820          * present --- no need to look first.
821          *
822          * This section of code is also a convenient place to verify that the
823          * output of an INSERT or UPDATE matches the target table(s).
824          */
825         {
826                 bool            junk_filter_needed = false;
827                 ListCell   *tlist;
828
829                 switch (operation)
830                 {
831                         case CMD_SELECT:
832                         case CMD_INSERT:
833                                 foreach(tlist, plan->targetlist)
834                                 {
835                                         TargetEntry *tle = (TargetEntry *) lfirst(tlist);
836
837                                         if (tle->resjunk)
838                                         {
839                                                 junk_filter_needed = true;
840                                                 break;
841                                         }
842                                 }
843                                 break;
844                         case CMD_UPDATE:
845                         case CMD_DELETE:
846                                 junk_filter_needed = true;
847                                 break;
848                         default:
849                                 break;
850                 }
851
852                 if (junk_filter_needed)
853                 {
854                         /*
855                          * If there are multiple result relations, each one needs its own
856                          * junk filter.  Note this is only possible for UPDATE/DELETE, so
857                          * we can't be fooled by some needing a filter and some not.
858                          */
859                         if (list_length(plannedstmt->resultRelations) > 1)
860                         {
861                                 PlanState **appendplans;
862                                 int                     as_nplans;
863                                 ResultRelInfo *resultRelInfo;
864
865                                 /* Top plan had better be an Append here. */
866                                 Assert(IsA(plan, Append));
867                                 Assert(((Append *) plan)->isTarget);
868                                 Assert(IsA(planstate, AppendState));
869                                 appendplans = ((AppendState *) planstate)->appendplans;
870                                 as_nplans = ((AppendState *) planstate)->as_nplans;
871                                 Assert(as_nplans == estate->es_num_result_relations);
872                                 resultRelInfo = estate->es_result_relations;
873                                 for (i = 0; i < as_nplans; i++)
874                                 {
875                                         PlanState  *subplan = appendplans[i];
876                                         JunkFilter *j;
877
878                                         if (operation == CMD_UPDATE)
879                                                 ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
880                                                                                         subplan->plan->targetlist);
881
882                                         j = ExecInitJunkFilter(subplan->plan->targetlist,
883                                                         resultRelInfo->ri_RelationDesc->rd_att->tdhasoid,
884                                                                                    ExecInitExtraTupleSlot(estate));
885
886                                         /*
887                                          * Since it must be UPDATE/DELETE, there had better be a
888                                          * "ctid" junk attribute in the tlist ... but ctid could
889                                          * be at a different resno for each result relation. We
890                                          * look up the ctid resnos now and save them in the
891                                          * junkfilters.
892                                          */
893                                         j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
894                                         if (!AttributeNumberIsValid(j->jf_junkAttNo))
895                                                 elog(ERROR, "could not find junk ctid column");
896                                         resultRelInfo->ri_junkFilter = j;
897                                         resultRelInfo++;
898                                 }
899
900                                 /*
901                                  * Set active junkfilter too; at this point ExecInitAppend has
902                                  * already selected an active result relation...
903                                  */
904                                 estate->es_junkFilter =
905                                         estate->es_result_relation_info->ri_junkFilter;
906
907                                 /*
908                                  * We currently can't support rowmarks in this case, because
909                                  * the associated junk CTIDs might have different resnos in
910                                  * different subplans.
911                                  */
912                                 if (estate->es_rowMarks)
913                                         ereport(ERROR,
914                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
915                                                          errmsg("SELECT FOR UPDATE/SHARE is not supported within a query with multiple result relations")));
916                         }
917                         else
918                         {
919                                 /* Normal case with just one JunkFilter */
920                                 JunkFilter *j;
921
922                                 if (operation == CMD_INSERT || operation == CMD_UPDATE)
923                                         ExecCheckPlanOutput(estate->es_result_relation_info->ri_RelationDesc,
924                                                                                 planstate->plan->targetlist);
925
926                                 j = ExecInitJunkFilter(planstate->plan->targetlist,
927                                                                            tupType->tdhasoid,
928                                                                            ExecInitExtraTupleSlot(estate));
929                                 estate->es_junkFilter = j;
930                                 if (estate->es_result_relation_info)
931                                         estate->es_result_relation_info->ri_junkFilter = j;
932
933                                 if (operation == CMD_SELECT)
934                                 {
935                                         /* For SELECT, want to return the cleaned tuple type */
936                                         tupType = j->jf_cleanTupType;
937                                 }
938                                 else if (operation == CMD_UPDATE || operation == CMD_DELETE)
939                                 {
940                                         /* For UPDATE/DELETE, find the ctid junk attr now */
941                                         j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
942                                         if (!AttributeNumberIsValid(j->jf_junkAttNo))
943                                                 elog(ERROR, "could not find junk ctid column");
944                                 }
945
946                                 /* For SELECT FOR UPDATE/SHARE, find the junk attrs now */
947                                 foreach(l, estate->es_rowMarks)
948                                 {
949                                         ExecRowMark *erm = (ExecRowMark *) lfirst(l);
950                                         char            resname[32];
951
952                                         /* always need the ctid */
953                                         snprintf(resname, sizeof(resname), "ctid%u",
954                                                          erm->prti);
955                                         erm->ctidAttNo = ExecFindJunkAttribute(j, resname);
956                                         if (!AttributeNumberIsValid(erm->ctidAttNo))
957                                                 elog(ERROR, "could not find junk \"%s\" column",
958                                                          resname);
959                                         /* if child relation, need tableoid too */
960                                         if (erm->rti != erm->prti)
961                                         {
962                                                 snprintf(resname, sizeof(resname), "tableoid%u",
963                                                                  erm->prti);
964                                                 erm->toidAttNo = ExecFindJunkAttribute(j, resname);
965                                                 if (!AttributeNumberIsValid(erm->toidAttNo))
966                                                         elog(ERROR, "could not find junk \"%s\" column",
967                                                                  resname);
968                                         }
969                                 }
970                         }
971                 }
972                 else
973                 {
974                         if (operation == CMD_INSERT)
975                                 ExecCheckPlanOutput(estate->es_result_relation_info->ri_RelationDesc,
976                                                                         planstate->plan->targetlist);
977
978                         estate->es_junkFilter = NULL;
979                         if (estate->es_rowMarks)
980                                 elog(ERROR, "SELECT FOR UPDATE/SHARE, but no junk columns");
981                 }
982         }
983
984         /*
985          * Initialize RETURNING projections if needed.
986          */
987         if (plannedstmt->returningLists)
988         {
989                 TupleTableSlot *slot;
990                 ExprContext *econtext;
991                 ResultRelInfo *resultRelInfo;
992
993                 /*
994                  * We set QueryDesc.tupDesc to be the RETURNING rowtype in this case.
995                  * We assume all the sublists will generate the same output tupdesc.
996                  */
997                 tupType = ExecTypeFromTL((List *) linitial(plannedstmt->returningLists),
998                                                                  false);
999
1000                 /* Set up a slot for the output of the RETURNING projection(s) */
1001                 slot = ExecInitExtraTupleSlot(estate);
1002                 ExecSetSlotDescriptor(slot, tupType);
1003                 /* Need an econtext too */
1004                 econtext = CreateExprContext(estate);
1005
1006                 /*
1007                  * Build a projection for each result rel.      Note that any SubPlans in
1008                  * the RETURNING lists get attached to the topmost plan node.
1009                  */
1010                 Assert(list_length(plannedstmt->returningLists) == estate->es_num_result_relations);
1011                 resultRelInfo = estate->es_result_relations;
1012                 foreach(l, plannedstmt->returningLists)
1013                 {
1014                         List       *rlist = (List *) lfirst(l);
1015                         List       *rliststate;
1016
1017                         rliststate = (List *) ExecInitExpr((Expr *) rlist, planstate);
1018                         resultRelInfo->ri_projectReturning =
1019                                 ExecBuildProjectionInfo(rliststate, econtext, slot,
1020                                                                          resultRelInfo->ri_RelationDesc->rd_att);
1021                         resultRelInfo++;
1022                 }
1023         }
1024
1025         queryDesc->tupDesc = tupType;
1026         queryDesc->planstate = planstate;
1027
1028         /*
1029          * If doing SELECT INTO, initialize the "into" relation.  We must wait
1030          * till now so we have the "clean" result tuple type to create the new
1031          * table from.
1032          *
1033          * If EXPLAIN, skip creating the "into" relation.
1034          */
1035         if (estate->es_select_into && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
1036                 OpenIntoRel(queryDesc);
1037 }
1038
1039 /*
1040  * Initialize ResultRelInfo data for one result relation
1041  */
1042 void
1043 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1044                                   Relation resultRelationDesc,
1045                                   Index resultRelationIndex,
1046                                   CmdType operation,
1047                                   bool doInstrument)
1048 {
1049         /*
1050          * Check valid relkind ... parser and/or planner should have noticed this
1051          * already, but let's make sure.
1052          */
1053         switch (resultRelationDesc->rd_rel->relkind)
1054         {
1055                 case RELKIND_RELATION:
1056                         /* OK */
1057                         break;
1058                 case RELKIND_SEQUENCE:
1059                         ereport(ERROR,
1060                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1061                                          errmsg("cannot change sequence \"%s\"",
1062                                                         RelationGetRelationName(resultRelationDesc))));
1063                         break;
1064                 case RELKIND_TOASTVALUE:
1065                         ereport(ERROR,
1066                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1067                                          errmsg("cannot change TOAST relation \"%s\"",
1068                                                         RelationGetRelationName(resultRelationDesc))));
1069                         break;
1070                 case RELKIND_VIEW:
1071                         ereport(ERROR,
1072                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1073                                          errmsg("cannot change view \"%s\"",
1074                                                         RelationGetRelationName(resultRelationDesc))));
1075                         break;
1076                 default:
1077                         ereport(ERROR,
1078                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1079                                          errmsg("cannot change relation \"%s\"",
1080                                                         RelationGetRelationName(resultRelationDesc))));
1081                         break;
1082         }
1083
1084         /* OK, fill in the node */
1085         MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1086         resultRelInfo->type = T_ResultRelInfo;
1087         resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1088         resultRelInfo->ri_RelationDesc = resultRelationDesc;
1089         resultRelInfo->ri_NumIndices = 0;
1090         resultRelInfo->ri_IndexRelationDescs = NULL;
1091         resultRelInfo->ri_IndexRelationInfo = NULL;
1092         /* make a copy so as not to depend on relcache info not changing... */
1093         resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1094         if (resultRelInfo->ri_TrigDesc)
1095         {
1096                 int                     n = resultRelInfo->ri_TrigDesc->numtriggers;
1097
1098                 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1099                         palloc0(n * sizeof(FmgrInfo));
1100                 if (doInstrument)
1101                         resultRelInfo->ri_TrigInstrument = InstrAlloc(n);
1102                 else
1103                         resultRelInfo->ri_TrigInstrument = NULL;
1104         }
1105         else
1106         {
1107                 resultRelInfo->ri_TrigFunctions = NULL;
1108                 resultRelInfo->ri_TrigInstrument = NULL;
1109         }
1110         resultRelInfo->ri_ConstraintExprs = NULL;
1111         resultRelInfo->ri_junkFilter = NULL;
1112         resultRelInfo->ri_projectReturning = NULL;
1113
1114         /*
1115          * If there are indices on the result relation, open them and save
1116          * descriptors in the result relation info, so that we can add new index
1117          * entries for the tuples we add/update.  We need not do this for a
1118          * DELETE, however, since deletion doesn't affect indexes.
1119          */
1120         if (resultRelationDesc->rd_rel->relhasindex &&
1121                 operation != CMD_DELETE)
1122                 ExecOpenIndices(resultRelInfo);
1123 }
1124
1125 /*
1126  * Verify that the tuples to be produced by INSERT or UPDATE match the
1127  * target relation's rowtype
1128  *
1129  * We do this to guard against stale plans.  If plan invalidation is
1130  * functioning properly then we should never get a failure here, but better
1131  * safe than sorry.  Note that this is called after we have obtained lock
1132  * on the target rel, so the rowtype can't change underneath us.
1133  *
1134  * The plan output is represented by its targetlist, because that makes
1135  * handling the dropped-column case easier.
1136  */
1137 static void
1138 ExecCheckPlanOutput(Relation resultRel, List *targetList)
1139 {
1140         TupleDesc       resultDesc = RelationGetDescr(resultRel);
1141         int                     attno = 0;
1142         ListCell   *lc;
1143
1144         foreach(lc, targetList)
1145         {
1146                 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1147                 Form_pg_attribute attr;
1148
1149                 if (tle->resjunk)
1150                         continue;                       /* ignore junk tlist items */
1151
1152                 if (attno >= resultDesc->natts)
1153                         ereport(ERROR,
1154                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1155                                          errmsg("table row type and query-specified row type do not match"),
1156                                          errdetail("Query has too many columns.")));
1157                 attr = resultDesc->attrs[attno++];
1158
1159                 if (!attr->attisdropped)
1160                 {
1161                         /* Normal case: demand type match */
1162                         if (exprType((Node *) tle->expr) != attr->atttypid)
1163                                 ereport(ERROR,
1164                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1165                                                  errmsg("table row type and query-specified row type do not match"),
1166                                                  errdetail("Table has type %s at ordinal position %d, but query expects %s.",
1167                                                                    format_type_be(attr->atttypid),
1168                                                                    attno,
1169                                                          format_type_be(exprType((Node *) tle->expr)))));
1170                 }
1171                 else
1172                 {
1173                         /*
1174                          * For a dropped column, we can't check atttypid (it's likely 0).
1175                          * In any case the planner has most likely inserted an INT4 null.
1176                          * What we insist on is just *some* NULL constant.
1177                          */
1178                         if (!IsA(tle->expr, Const) ||
1179                                 !((Const *) tle->expr)->constisnull)
1180                                 ereport(ERROR,
1181                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1182                                                  errmsg("table row type and query-specified row type do not match"),
1183                                                  errdetail("Query provides a value for a dropped column at ordinal position %d.",
1184                                                                    attno)));
1185                 }
1186         }
1187         if (attno != resultDesc->natts)
1188                 ereport(ERROR,
1189                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1190                   errmsg("table row type and query-specified row type do not match"),
1191                                  errdetail("Query has too few columns.")));
1192 }
1193
1194 /*
1195  *              ExecGetTriggerResultRel
1196  *
1197  * Get a ResultRelInfo for a trigger target relation.  Most of the time,
1198  * triggers are fired on one of the result relations of the query, and so
1199  * we can just return a member of the es_result_relations array.  (Note: in
1200  * self-join situations there might be multiple members with the same OID;
1201  * if so it doesn't matter which one we pick.)  However, it is sometimes
1202  * necessary to fire triggers on other relations; this happens mainly when an
1203  * RI update trigger queues additional triggers on other relations, which will
1204  * be processed in the context of the outer query.      For efficiency's sake,
1205  * we want to have a ResultRelInfo for those triggers too; that can avoid
1206  * repeated re-opening of the relation.  (It also provides a way for EXPLAIN
1207  * ANALYZE to report the runtimes of such triggers.)  So we make additional
1208  * ResultRelInfo's as needed, and save them in es_trig_target_relations.
1209  */
1210 ResultRelInfo *
1211 ExecGetTriggerResultRel(EState *estate, Oid relid)
1212 {
1213         ResultRelInfo *rInfo;
1214         int                     nr;
1215         ListCell   *l;
1216         Relation        rel;
1217         MemoryContext oldcontext;
1218
1219         /* First, search through the query result relations */
1220         rInfo = estate->es_result_relations;
1221         nr = estate->es_num_result_relations;
1222         while (nr > 0)
1223         {
1224                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1225                         return rInfo;
1226                 rInfo++;
1227                 nr--;
1228         }
1229         /* Nope, but maybe we already made an extra ResultRelInfo for it */
1230         foreach(l, estate->es_trig_target_relations)
1231         {
1232                 rInfo = (ResultRelInfo *) lfirst(l);
1233                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1234                         return rInfo;
1235         }
1236         /* Nope, so we need a new one */
1237
1238         /*
1239          * Open the target relation's relcache entry.  We assume that an
1240          * appropriate lock is still held by the backend from whenever the trigger
1241          * event got queued, so we need take no new lock here.
1242          */
1243         rel = heap_open(relid, NoLock);
1244
1245         /*
1246          * Make the new entry in the right context.  Currently, we don't need any
1247          * index information in ResultRelInfos used only for triggers, so tell
1248          * InitResultRelInfo it's a DELETE.
1249          */
1250         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1251         rInfo = makeNode(ResultRelInfo);
1252         InitResultRelInfo(rInfo,
1253                                           rel,
1254                                           0,            /* dummy rangetable index */
1255                                           CMD_DELETE,
1256                                           estate->es_instrument);
1257         estate->es_trig_target_relations =
1258                 lappend(estate->es_trig_target_relations, rInfo);
1259         MemoryContextSwitchTo(oldcontext);
1260
1261         return rInfo;
1262 }
1263
1264 /*
1265  *              ExecContextForcesOids
1266  *
1267  * This is pretty grotty: when doing INSERT, UPDATE, or SELECT INTO,
1268  * we need to ensure that result tuples have space for an OID iff they are
1269  * going to be stored into a relation that has OIDs.  In other contexts
1270  * we are free to choose whether to leave space for OIDs in result tuples
1271  * (we generally don't want to, but we do if a physical-tlist optimization
1272  * is possible).  This routine checks the plan context and returns TRUE if the
1273  * choice is forced, FALSE if the choice is not forced.  In the TRUE case,
1274  * *hasoids is set to the required value.
1275  *
1276  * One reason this is ugly is that all plan nodes in the plan tree will emit
1277  * tuples with space for an OID, though we really only need the topmost node
1278  * to do so.  However, node types like Sort don't project new tuples but just
1279  * return their inputs, and in those cases the requirement propagates down
1280  * to the input node.  Eventually we might make this code smart enough to
1281  * recognize how far down the requirement really goes, but for now we just
1282  * make all plan nodes do the same thing if the top level forces the choice.
1283  *
1284  * We assume that estate->es_result_relation_info is already set up to
1285  * describe the target relation.  Note that in an UPDATE that spans an
1286  * inheritance tree, some of the target relations may have OIDs and some not.
1287  * We have to make the decisions on a per-relation basis as we initialize
1288  * each of the child plans of the topmost Append plan.
1289  *
1290  * SELECT INTO is even uglier, because we don't have the INTO relation's
1291  * descriptor available when this code runs; we have to look aside at a
1292  * flag set by InitPlan().
1293  */
1294 bool
1295 ExecContextForcesOids(PlanState *planstate, bool *hasoids)
1296 {
1297         if (planstate->state->es_select_into)
1298         {
1299                 *hasoids = planstate->state->es_into_oids;
1300                 return true;
1301         }
1302         else
1303         {
1304                 ResultRelInfo *ri = planstate->state->es_result_relation_info;
1305
1306                 if (ri != NULL)
1307                 {
1308                         Relation        rel = ri->ri_RelationDesc;
1309
1310                         if (rel != NULL)
1311                         {
1312                                 *hasoids = rel->rd_rel->relhasoids;
1313                                 return true;
1314                         }
1315                 }
1316         }
1317
1318         return false;
1319 }
1320
1321 /* ----------------------------------------------------------------
1322  *              ExecEndPlan
1323  *
1324  *              Cleans up the query plan -- closes files and frees up storage
1325  *
1326  * NOTE: we are no longer very worried about freeing storage per se
1327  * in this code; FreeExecutorState should be guaranteed to release all
1328  * memory that needs to be released.  What we are worried about doing
1329  * is closing relations and dropping buffer pins.  Thus, for example,
1330  * tuple tables must be cleared or dropped to ensure pins are released.
1331  * ----------------------------------------------------------------
1332  */
1333 static void
1334 ExecEndPlan(PlanState *planstate, EState *estate)
1335 {
1336         ResultRelInfo *resultRelInfo;
1337         int                     i;
1338         ListCell   *l;
1339
1340         /*
1341          * shut down any PlanQual processing we were doing
1342          */
1343         if (estate->es_evalPlanQual != NULL)
1344                 EndEvalPlanQual(estate);
1345
1346         /*
1347          * shut down the node-type-specific query processing
1348          */
1349         ExecEndNode(planstate);
1350
1351         /*
1352          * for subplans too
1353          */
1354         foreach(l, estate->es_subplanstates)
1355         {
1356                 PlanState  *subplanstate = (PlanState *) lfirst(l);
1357
1358                 ExecEndNode(subplanstate);
1359         }
1360
1361         /*
1362          * destroy the executor's tuple table.  Actually we only care about
1363          * releasing buffer pins and tupdesc refcounts; there's no need to
1364          * pfree the TupleTableSlots, since the containing memory context
1365          * is about to go away anyway.
1366          */
1367         ExecResetTupleTable(estate->es_tupleTable, false);
1368
1369         /*
1370          * close the result relation(s) if any, but hold locks until xact commit.
1371          */
1372         resultRelInfo = estate->es_result_relations;
1373         for (i = estate->es_num_result_relations; i > 0; i--)
1374         {
1375                 /* Close indices and then the relation itself */
1376                 ExecCloseIndices(resultRelInfo);
1377                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1378                 resultRelInfo++;
1379         }
1380
1381         /*
1382          * likewise close any trigger target relations
1383          */
1384         foreach(l, estate->es_trig_target_relations)
1385         {
1386                 resultRelInfo = (ResultRelInfo *) lfirst(l);
1387                 /* Close indices and then the relation itself */
1388                 ExecCloseIndices(resultRelInfo);
1389                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1390         }
1391
1392         /*
1393          * close any relations selected FOR UPDATE/FOR SHARE, again keeping locks
1394          */
1395         foreach(l, estate->es_rowMarks)
1396         {
1397                 ExecRowMark *erm = lfirst(l);
1398
1399                 heap_close(erm->relation, NoLock);
1400         }
1401 }
1402
1403 /* ----------------------------------------------------------------
1404  *              ExecutePlan
1405  *
1406  *              Processes the query plan until we have processed 'numberTuples' tuples,
1407  *              moving in the specified direction.
1408  *
1409  *              Runs to completion if numberTuples is 0
1410  *
1411  * Note: the ctid attribute is a 'junk' attribute that is removed before the
1412  * user can see it
1413  * ----------------------------------------------------------------
1414  */
1415 static void
1416 ExecutePlan(EState *estate,
1417                         PlanState *planstate,
1418                         CmdType operation,
1419                         long numberTuples,
1420                         ScanDirection direction,
1421                         DestReceiver *dest)
1422 {
1423         JunkFilter *junkfilter;
1424         TupleTableSlot *planSlot;
1425         TupleTableSlot *slot;
1426         ItemPointer tupleid = NULL;
1427         ItemPointerData tuple_ctid;
1428         long            current_tuple_count;
1429
1430         /*
1431          * initialize local variables
1432          */
1433         current_tuple_count = 0;
1434
1435         /*
1436          * Set the direction.
1437          */
1438         estate->es_direction = direction;
1439
1440         /*
1441          * Process BEFORE EACH STATEMENT triggers
1442          */
1443         switch (operation)
1444         {
1445                 case CMD_UPDATE:
1446                         ExecBSUpdateTriggers(estate, estate->es_result_relation_info);
1447                         break;
1448                 case CMD_DELETE:
1449                         ExecBSDeleteTriggers(estate, estate->es_result_relation_info);
1450                         break;
1451                 case CMD_INSERT:
1452                         ExecBSInsertTriggers(estate, estate->es_result_relation_info);
1453                         break;
1454                 default:
1455                         /* do nothing */
1456                         break;
1457         }
1458
1459         /*
1460          * Loop until we've processed the proper number of tuples from the plan.
1461          */
1462         for (;;)
1463         {
1464                 /* Reset the per-output-tuple exprcontext */
1465                 ResetPerTupleExprContext(estate);
1466
1467                 /*
1468                  * Execute the plan and obtain a tuple
1469                  */
1470 lnext:  ;
1471                 if (estate->es_useEvalPlan)
1472                 {
1473                         planSlot = EvalPlanQualNext(estate);
1474                         if (TupIsNull(planSlot))
1475                                 planSlot = ExecProcNode(planstate);
1476                 }
1477                 else
1478                         planSlot = ExecProcNode(planstate);
1479
1480                 /*
1481                  * if the tuple is null, then we assume there is nothing more to
1482                  * process so we just end the loop...
1483                  */
1484                 if (TupIsNull(planSlot))
1485                         break;
1486                 slot = planSlot;
1487
1488                 /*
1489                  * If we have a junk filter, then project a new tuple with the junk
1490                  * removed.
1491                  *
1492                  * Store this new "clean" tuple in the junkfilter's resultSlot.
1493                  * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1494                  * because that tuple slot has the wrong descriptor.)
1495                  *
1496                  * But first, extract all the junk information we need.
1497                  */
1498                 if ((junkfilter = estate->es_junkFilter) != NULL)
1499                 {
1500                         /*
1501                          * Process any FOR UPDATE or FOR SHARE locking requested.
1502                          */
1503                         if (estate->es_rowMarks != NIL)
1504                         {
1505                                 ListCell   *l;
1506
1507                 lmark:  ;
1508                                 foreach(l, estate->es_rowMarks)
1509                                 {
1510                                         ExecRowMark *erm = lfirst(l);
1511                                         Datum           datum;
1512                                         bool            isNull;
1513                                         HeapTupleData tuple;
1514                                         Buffer          buffer;
1515                                         ItemPointerData update_ctid;
1516                                         TransactionId update_xmax;
1517                                         TupleTableSlot *newSlot;
1518                                         LockTupleMode lockmode;
1519                                         HTSU_Result test;
1520
1521                                         /* if child rel, must check whether it produced this row */
1522                                         if (erm->rti != erm->prti)
1523                                         {
1524                                                 Oid                     tableoid;
1525
1526                                                 datum = ExecGetJunkAttribute(slot,
1527                                                                                                          erm->toidAttNo,
1528                                                                                                          &isNull);
1529                                                 /* shouldn't ever get a null result... */
1530                                                 if (isNull)
1531                                                         elog(ERROR, "tableoid is NULL");
1532                                                 tableoid = DatumGetObjectId(datum);
1533
1534                                                 if (tableoid != RelationGetRelid(erm->relation))
1535                                                 {
1536                                                         /* this child is inactive right now */
1537                                                         ItemPointerSetInvalid(&(erm->curCtid));
1538                                                         continue;
1539                                                 }
1540                                         }
1541
1542                                         /* okay, fetch the tuple by ctid */
1543                                         datum = ExecGetJunkAttribute(slot,
1544                                                                                                  erm->ctidAttNo,
1545                                                                                                  &isNull);
1546                                         /* shouldn't ever get a null result... */
1547                                         if (isNull)
1548                                                 elog(ERROR, "ctid is NULL");
1549                                         tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
1550
1551                                         if (erm->forUpdate)
1552                                                 lockmode = LockTupleExclusive;
1553                                         else
1554                                                 lockmode = LockTupleShared;
1555
1556                                         test = heap_lock_tuple(erm->relation, &tuple, &buffer,
1557                                                                                    &update_ctid, &update_xmax,
1558                                                                                    estate->es_output_cid,
1559                                                                                    lockmode, erm->noWait);
1560                                         ReleaseBuffer(buffer);
1561                                         switch (test)
1562                                         {
1563                                                 case HeapTupleSelfUpdated:
1564                                                         /* treat it as deleted; do not process */
1565                                                         goto lnext;
1566
1567                                                 case HeapTupleMayBeUpdated:
1568                                                         break;
1569
1570                                                 case HeapTupleUpdated:
1571                                                         if (IsXactIsoLevelSerializable)
1572                                                                 ereport(ERROR,
1573                                                                  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1574                                                                   errmsg("could not serialize access due to concurrent update")));
1575                                                         if (!ItemPointerEquals(&update_ctid,
1576                                                                                                    &tuple.t_self))
1577                                                         {
1578                                                                 /* updated, so look at updated version */
1579                                                                 newSlot = EvalPlanQual(estate,
1580                                                                                                            erm->rti,
1581                                                                                                            &update_ctid,
1582                                                                                                            update_xmax);
1583                                                                 if (!TupIsNull(newSlot))
1584                                                                 {
1585                                                                         slot = planSlot = newSlot;
1586                                                                         estate->es_useEvalPlan = true;
1587                                                                         goto lmark;
1588                                                                 }
1589                                                         }
1590
1591                                                         /*
1592                                                          * if tuple was deleted or PlanQual failed for
1593                                                          * updated tuple - we must not return this tuple!
1594                                                          */
1595                                                         goto lnext;
1596
1597                                                 default:
1598                                                         elog(ERROR, "unrecognized heap_lock_tuple status: %u",
1599                                                                  test);
1600                                         }
1601
1602                                         /* Remember tuple TID for WHERE CURRENT OF */
1603                                         erm->curCtid = tuple.t_self;
1604                                 }
1605                         }
1606
1607                         /*
1608                          * extract the 'ctid' junk attribute.
1609                          */
1610                         if (operation == CMD_UPDATE || operation == CMD_DELETE)
1611                         {
1612                                 Datum           datum;
1613                                 bool            isNull;
1614
1615                                 datum = ExecGetJunkAttribute(slot, junkfilter->jf_junkAttNo,
1616                                                                                          &isNull);
1617                                 /* shouldn't ever get a null result... */
1618                                 if (isNull)
1619                                         elog(ERROR, "ctid is NULL");
1620
1621                                 tupleid = (ItemPointer) DatumGetPointer(datum);
1622                                 tuple_ctid = *tupleid;  /* make sure we don't free the ctid!! */
1623                                 tupleid = &tuple_ctid;
1624                         }
1625
1626                         /*
1627                          * Create a new "clean" tuple with all junk attributes removed. We
1628                          * don't need to do this for DELETE, however (there will in fact
1629                          * be no non-junk attributes in a DELETE!)
1630                          */
1631                         if (operation != CMD_DELETE)
1632                                 slot = ExecFilterJunk(junkfilter, slot);
1633                 }
1634
1635                 /*
1636                  * now that we have a tuple, do the appropriate thing with it.. either
1637                  * send it to the output destination, add it to a relation someplace,
1638                  * delete it from a relation, or modify some of its attributes.
1639                  */
1640                 switch (operation)
1641                 {
1642                         case CMD_SELECT:
1643                                 ExecSelect(slot, dest, estate);
1644                                 break;
1645
1646                         case CMD_INSERT:
1647                                 ExecInsert(slot, tupleid, planSlot, dest, estate);
1648                                 break;
1649
1650                         case CMD_DELETE:
1651                                 ExecDelete(tupleid, planSlot, dest, estate);
1652                                 break;
1653
1654                         case CMD_UPDATE:
1655                                 ExecUpdate(slot, tupleid, planSlot, dest, estate);
1656                                 break;
1657
1658                         default:
1659                                 elog(ERROR, "unrecognized operation code: %d",
1660                                          (int) operation);
1661                                 break;
1662                 }
1663
1664                 /*
1665                  * check our tuple count.. if we've processed the proper number then
1666                  * quit, else loop again and process more tuples.  Zero numberTuples
1667                  * means no limit.
1668                  */
1669                 current_tuple_count++;
1670                 if (numberTuples && numberTuples == current_tuple_count)
1671                         break;
1672         }
1673
1674         /*
1675          * Process AFTER EACH STATEMENT triggers
1676          */
1677         switch (operation)
1678         {
1679                 case CMD_UPDATE:
1680                         ExecASUpdateTriggers(estate, estate->es_result_relation_info);
1681                         break;
1682                 case CMD_DELETE:
1683                         ExecASDeleteTriggers(estate, estate->es_result_relation_info);
1684                         break;
1685                 case CMD_INSERT:
1686                         ExecASInsertTriggers(estate, estate->es_result_relation_info);
1687                         break;
1688                 default:
1689                         /* do nothing */
1690                         break;
1691         }
1692 }
1693
1694 /* ----------------------------------------------------------------
1695  *              ExecSelect
1696  *
1697  *              SELECTs are easy.. we just pass the tuple to the appropriate
1698  *              output function.
1699  * ----------------------------------------------------------------
1700  */
1701 static void
1702 ExecSelect(TupleTableSlot *slot,
1703                    DestReceiver *dest,
1704                    EState *estate)
1705 {
1706         (*dest->receiveSlot) (slot, dest);
1707         (estate->es_processed)++;
1708 }
1709
1710 /* ----------------------------------------------------------------
1711  *              ExecInsert
1712  *
1713  *              INSERTs are trickier.. we have to insert the tuple into
1714  *              the base relation and insert appropriate tuples into the
1715  *              index relations.
1716  * ----------------------------------------------------------------
1717  */
1718 static void
1719 ExecInsert(TupleTableSlot *slot,
1720                    ItemPointer tupleid,
1721                    TupleTableSlot *planSlot,
1722                    DestReceiver *dest,
1723                    EState *estate)
1724 {
1725         HeapTuple       tuple;
1726         ResultRelInfo *resultRelInfo;
1727         Relation        resultRelationDesc;
1728         Oid                     newId;
1729         List       *recheckIndexes = NIL;
1730
1731         /*
1732          * get the heap tuple out of the tuple table slot, making sure we have a
1733          * writable copy
1734          */
1735         tuple = ExecMaterializeSlot(slot);
1736
1737         /*
1738          * get information on the (current) result relation
1739          */
1740         resultRelInfo = estate->es_result_relation_info;
1741         resultRelationDesc = resultRelInfo->ri_RelationDesc;
1742
1743         /*
1744          * If the result relation has OIDs, force the tuple's OID to zero so that
1745          * heap_insert will assign a fresh OID.  Usually the OID already will be
1746          * zero at this point, but there are corner cases where the plan tree can
1747          * return a tuple extracted literally from some table with the same
1748          * rowtype.
1749          *
1750          * XXX if we ever wanted to allow users to assign their own OIDs to new
1751          * rows, this'd be the place to do it.  For the moment, we make a point of
1752          * doing this before calling triggers, so that a user-supplied trigger
1753          * could hack the OID if desired.
1754          */
1755         if (resultRelationDesc->rd_rel->relhasoids)
1756                 HeapTupleSetOid(tuple, InvalidOid);
1757
1758         /* BEFORE ROW INSERT Triggers */
1759         if (resultRelInfo->ri_TrigDesc &&
1760                 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1761         {
1762                 HeapTuple       newtuple;
1763
1764                 newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1765
1766                 if (newtuple == NULL)   /* "do nothing" */
1767                         return;
1768
1769                 if (newtuple != tuple)  /* modified by Trigger(s) */
1770                 {
1771                         /*
1772                          * Put the modified tuple into a slot for convenience of routines
1773                          * below.  We assume the tuple was allocated in per-tuple memory
1774                          * context, and therefore will go away by itself. The tuple table
1775                          * slot should not try to clear it.
1776                          */
1777                         TupleTableSlot *newslot = estate->es_trig_tuple_slot;
1778
1779                         if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
1780                                 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
1781                         ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
1782                         slot = newslot;
1783                         tuple = newtuple;
1784                 }
1785         }
1786
1787         /*
1788          * Check the constraints of the tuple
1789          */
1790         if (resultRelationDesc->rd_att->constr)
1791                 ExecConstraints(resultRelInfo, slot, estate);
1792
1793         /*
1794          * insert the tuple
1795          *
1796          * Note: heap_insert returns the tid (location) of the new tuple in the
1797          * t_self field.
1798          */
1799         newId = heap_insert(resultRelationDesc, tuple,
1800                                                 estate->es_output_cid, 0, NULL);
1801
1802         (estate->es_processed)++;
1803         estate->es_lastoid = newId;
1804         setLastTid(&(tuple->t_self));
1805
1806         /*
1807          * insert index entries for tuple
1808          */
1809         if (resultRelInfo->ri_NumIndices > 0)
1810                 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
1811                                                                                            estate, false);
1812
1813         /* AFTER ROW INSERT Triggers */
1814         ExecARInsertTriggers(estate, resultRelInfo, tuple, recheckIndexes);
1815
1816         /* Process RETURNING if present */
1817         if (resultRelInfo->ri_projectReturning)
1818                 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1819                                                          slot, planSlot, dest);
1820 }
1821
1822 /* ----------------------------------------------------------------
1823  *              ExecDelete
1824  *
1825  *              DELETE is like UPDATE, except that we delete the tuple and no
1826  *              index modifications are needed
1827  * ----------------------------------------------------------------
1828  */
1829 static void
1830 ExecDelete(ItemPointer tupleid,
1831                    TupleTableSlot *planSlot,
1832                    DestReceiver *dest,
1833                    EState *estate)
1834 {
1835         ResultRelInfo *resultRelInfo;
1836         Relation        resultRelationDesc;
1837         HTSU_Result result;
1838         ItemPointerData update_ctid;
1839         TransactionId update_xmax;
1840
1841         /*
1842          * get information on the (current) result relation
1843          */
1844         resultRelInfo = estate->es_result_relation_info;
1845         resultRelationDesc = resultRelInfo->ri_RelationDesc;
1846
1847         /* BEFORE ROW DELETE Triggers */
1848         if (resultRelInfo->ri_TrigDesc &&
1849                 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
1850         {
1851                 bool            dodelete;
1852
1853                 dodelete = ExecBRDeleteTriggers(estate, resultRelInfo, tupleid);
1854
1855                 if (!dodelete)                  /* "do nothing" */
1856                         return;
1857         }
1858
1859         /*
1860          * delete the tuple
1861          *
1862          * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
1863          * the row to be deleted is visible to that snapshot, and throw a can't-
1864          * serialize error if not.      This is a special-case behavior needed for
1865          * referential integrity updates in serializable transactions.
1866          */
1867 ldelete:;
1868         result = heap_delete(resultRelationDesc, tupleid,
1869                                                  &update_ctid, &update_xmax,
1870                                                  estate->es_output_cid,
1871                                                  estate->es_crosscheck_snapshot,
1872                                                  true /* wait for commit */ );
1873         switch (result)
1874         {
1875                 case HeapTupleSelfUpdated:
1876                         /* already deleted by self; nothing to do */
1877                         return;
1878
1879                 case HeapTupleMayBeUpdated:
1880                         break;
1881
1882                 case HeapTupleUpdated:
1883                         if (IsXactIsoLevelSerializable)
1884                                 ereport(ERROR,
1885                                                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1886                                                  errmsg("could not serialize access due to concurrent update")));
1887                         else if (!ItemPointerEquals(tupleid, &update_ctid))
1888                         {
1889                                 TupleTableSlot *epqslot;
1890
1891                                 epqslot = EvalPlanQual(estate,
1892                                                                            resultRelInfo->ri_RangeTableIndex,
1893                                                                            &update_ctid,
1894                                                                            update_xmax);
1895                                 if (!TupIsNull(epqslot))
1896                                 {
1897                                         *tupleid = update_ctid;
1898                                         goto ldelete;
1899                                 }
1900                         }
1901                         /* tuple already deleted; nothing to do */
1902                         return;
1903
1904                 default:
1905                         elog(ERROR, "unrecognized heap_delete status: %u", result);
1906                         return;
1907         }
1908
1909         (estate->es_processed)++;
1910
1911         /*
1912          * Note: Normally one would think that we have to delete index tuples
1913          * associated with the heap tuple now...
1914          *
1915          * ... but in POSTGRES, we have no need to do this because VACUUM will
1916          * take care of it later.  We can't delete index tuples immediately
1917          * anyway, since the tuple is still visible to other transactions.
1918          */
1919
1920         /* AFTER ROW DELETE Triggers */
1921         ExecARDeleteTriggers(estate, resultRelInfo, tupleid);
1922
1923         /* Process RETURNING if present */
1924         if (resultRelInfo->ri_projectReturning)
1925         {
1926                 /*
1927                  * We have to put the target tuple into a slot, which means first we
1928                  * gotta fetch it.      We can use the trigger tuple slot.
1929                  */
1930                 TupleTableSlot *slot = estate->es_trig_tuple_slot;
1931                 HeapTupleData deltuple;
1932                 Buffer          delbuffer;
1933
1934                 deltuple.t_self = *tupleid;
1935                 if (!heap_fetch(resultRelationDesc, SnapshotAny,
1936                                                 &deltuple, &delbuffer, false, NULL))
1937                         elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
1938
1939                 if (slot->tts_tupleDescriptor != RelationGetDescr(resultRelationDesc))
1940                         ExecSetSlotDescriptor(slot, RelationGetDescr(resultRelationDesc));
1941                 ExecStoreTuple(&deltuple, slot, InvalidBuffer, false);
1942
1943                 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1944                                                          slot, planSlot, dest);
1945
1946                 ExecClearTuple(slot);
1947                 ReleaseBuffer(delbuffer);
1948         }
1949 }
1950
1951 /* ----------------------------------------------------------------
1952  *              ExecUpdate
1953  *
1954  *              note: we can't run UPDATE queries with transactions
1955  *              off because UPDATEs are actually INSERTs and our
1956  *              scan will mistakenly loop forever, updating the tuple
1957  *              it just inserted..      This should be fixed but until it
1958  *              is, we don't want to get stuck in an infinite loop
1959  *              which corrupts your database..
1960  * ----------------------------------------------------------------
1961  */
1962 static void
1963 ExecUpdate(TupleTableSlot *slot,
1964                    ItemPointer tupleid,
1965                    TupleTableSlot *planSlot,
1966                    DestReceiver *dest,
1967                    EState *estate)
1968 {
1969         HeapTuple       tuple;
1970         ResultRelInfo *resultRelInfo;
1971         Relation        resultRelationDesc;
1972         HTSU_Result result;
1973         ItemPointerData update_ctid;
1974         TransactionId update_xmax;
1975         List *recheckIndexes = NIL;
1976
1977         /*
1978          * abort the operation if not running transactions
1979          */
1980         if (IsBootstrapProcessingMode())
1981                 elog(ERROR, "cannot UPDATE during bootstrap");
1982
1983         /*
1984          * get the heap tuple out of the tuple table slot, making sure we have a
1985          * writable copy
1986          */
1987         tuple = ExecMaterializeSlot(slot);
1988
1989         /*
1990          * get information on the (current) result relation
1991          */
1992         resultRelInfo = estate->es_result_relation_info;
1993         resultRelationDesc = resultRelInfo->ri_RelationDesc;
1994
1995         /* BEFORE ROW UPDATE Triggers */
1996         if (resultRelInfo->ri_TrigDesc &&
1997                 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
1998         {
1999                 HeapTuple       newtuple;
2000
2001                 newtuple = ExecBRUpdateTriggers(estate, resultRelInfo,
2002                                                                                 tupleid, tuple);
2003
2004                 if (newtuple == NULL)   /* "do nothing" */
2005                         return;
2006
2007                 if (newtuple != tuple)  /* modified by Trigger(s) */
2008                 {
2009                         /*
2010                          * Put the modified tuple into a slot for convenience of routines
2011                          * below.  We assume the tuple was allocated in per-tuple memory
2012                          * context, and therefore will go away by itself. The tuple table
2013                          * slot should not try to clear it.
2014                          */
2015                         TupleTableSlot *newslot = estate->es_trig_tuple_slot;
2016
2017                         if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
2018                                 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
2019                         ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
2020                         slot = newslot;
2021                         tuple = newtuple;
2022                 }
2023         }
2024
2025         /*
2026          * Check the constraints of the tuple
2027          *
2028          * If we generate a new candidate tuple after EvalPlanQual testing, we
2029          * must loop back here and recheck constraints.  (We don't need to redo
2030          * triggers, however.  If there are any BEFORE triggers then trigger.c
2031          * will have done heap_lock_tuple to lock the correct tuple, so there's no
2032          * need to do them again.)
2033          */
2034 lreplace:;
2035         if (resultRelationDesc->rd_att->constr)
2036                 ExecConstraints(resultRelInfo, slot, estate);
2037
2038         /*
2039          * replace the heap tuple
2040          *
2041          * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
2042          * the row to be updated is visible to that snapshot, and throw a can't-
2043          * serialize error if not.      This is a special-case behavior needed for
2044          * referential integrity updates in serializable transactions.
2045          */
2046         result = heap_update(resultRelationDesc, tupleid, tuple,
2047                                                  &update_ctid, &update_xmax,
2048                                                  estate->es_output_cid,
2049                                                  estate->es_crosscheck_snapshot,
2050                                                  true /* wait for commit */ );
2051         switch (result)
2052         {
2053                 case HeapTupleSelfUpdated:
2054                         /* already deleted by self; nothing to do */
2055                         return;
2056
2057                 case HeapTupleMayBeUpdated:
2058                         break;
2059
2060                 case HeapTupleUpdated:
2061                         if (IsXactIsoLevelSerializable)
2062                                 ereport(ERROR,
2063                                                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2064                                                  errmsg("could not serialize access due to concurrent update")));
2065                         else if (!ItemPointerEquals(tupleid, &update_ctid))
2066                         {
2067                                 TupleTableSlot *epqslot;
2068
2069                                 epqslot = EvalPlanQual(estate,
2070                                                                            resultRelInfo->ri_RangeTableIndex,
2071                                                                            &update_ctid,
2072                                                                            update_xmax);
2073                                 if (!TupIsNull(epqslot))
2074                                 {
2075                                         *tupleid = update_ctid;
2076                                         slot = ExecFilterJunk(estate->es_junkFilter, epqslot);
2077                                         tuple = ExecMaterializeSlot(slot);
2078                                         goto lreplace;
2079                                 }
2080                         }
2081                         /* tuple already deleted; nothing to do */
2082                         return;
2083
2084                 default:
2085                         elog(ERROR, "unrecognized heap_update status: %u", result);
2086                         return;
2087         }
2088
2089         (estate->es_processed)++;
2090
2091         /*
2092          * Note: instead of having to update the old index tuples associated with
2093          * the heap tuple, all we do is form and insert new index tuples. This is
2094          * because UPDATEs are actually DELETEs and INSERTs, and index tuple
2095          * deletion is done later by VACUUM (see notes in ExecDelete).  All we do
2096          * here is insert new index tuples.  -cim 9/27/89
2097          */
2098
2099         /*
2100          * insert index entries for tuple
2101          *
2102          * Note: heap_update returns the tid (location) of the new tuple in the
2103          * t_self field.
2104          *
2105          * If it's a HOT update, we mustn't insert new index entries.
2106          */
2107         if (resultRelInfo->ri_NumIndices > 0 && !HeapTupleIsHeapOnly(tuple))
2108                 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
2109                                                                                            estate, false);
2110
2111         /* AFTER ROW UPDATE Triggers */
2112         ExecARUpdateTriggers(estate, resultRelInfo, tupleid, tuple,
2113                                                  recheckIndexes);
2114
2115         /* Process RETURNING if present */
2116         if (resultRelInfo->ri_projectReturning)
2117                 ExecProcessReturning(resultRelInfo->ri_projectReturning,
2118                                                          slot, planSlot, dest);
2119 }
2120
2121 /*
2122  * ExecRelCheck --- check that tuple meets constraints for result relation
2123  */
2124 static const char *
2125 ExecRelCheck(ResultRelInfo *resultRelInfo,
2126                          TupleTableSlot *slot, EState *estate)
2127 {
2128         Relation        rel = resultRelInfo->ri_RelationDesc;
2129         int                     ncheck = rel->rd_att->constr->num_check;
2130         ConstrCheck *check = rel->rd_att->constr->check;
2131         ExprContext *econtext;
2132         MemoryContext oldContext;
2133         List       *qual;
2134         int                     i;
2135
2136         /*
2137          * If first time through for this result relation, build expression
2138          * nodetrees for rel's constraint expressions.  Keep them in the per-query
2139          * memory context so they'll survive throughout the query.
2140          */
2141         if (resultRelInfo->ri_ConstraintExprs == NULL)
2142         {
2143                 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
2144                 resultRelInfo->ri_ConstraintExprs =
2145                         (List **) palloc(ncheck * sizeof(List *));
2146                 for (i = 0; i < ncheck; i++)
2147                 {
2148                         /* ExecQual wants implicit-AND form */
2149                         qual = make_ands_implicit(stringToNode(check[i].ccbin));
2150                         resultRelInfo->ri_ConstraintExprs[i] = (List *)
2151                                 ExecPrepareExpr((Expr *) qual, estate);
2152                 }
2153                 MemoryContextSwitchTo(oldContext);
2154         }
2155
2156         /*
2157          * We will use the EState's per-tuple context for evaluating constraint
2158          * expressions (creating it if it's not already there).
2159          */
2160         econtext = GetPerTupleExprContext(estate);
2161
2162         /* Arrange for econtext's scan tuple to be the tuple under test */
2163         econtext->ecxt_scantuple = slot;
2164
2165         /* And evaluate the constraints */
2166         for (i = 0; i < ncheck; i++)
2167         {
2168                 qual = resultRelInfo->ri_ConstraintExprs[i];
2169
2170                 /*
2171                  * NOTE: SQL92 specifies that a NULL result from a constraint
2172                  * expression is not to be treated as a failure.  Therefore, tell
2173                  * ExecQual to return TRUE for NULL.
2174                  */
2175                 if (!ExecQual(qual, econtext, true))
2176                         return check[i].ccname;
2177         }
2178
2179         /* NULL result means no error */
2180         return NULL;
2181 }
2182
2183 void
2184 ExecConstraints(ResultRelInfo *resultRelInfo,
2185                                 TupleTableSlot *slot, EState *estate)
2186 {
2187         Relation        rel = resultRelInfo->ri_RelationDesc;
2188         TupleConstr *constr = rel->rd_att->constr;
2189
2190         Assert(constr);
2191
2192         if (constr->has_not_null)
2193         {
2194                 int                     natts = rel->rd_att->natts;
2195                 int                     attrChk;
2196
2197                 for (attrChk = 1; attrChk <= natts; attrChk++)
2198                 {
2199                         if (rel->rd_att->attrs[attrChk - 1]->attnotnull &&
2200                                 slot_attisnull(slot, attrChk))
2201                                 ereport(ERROR,
2202                                                 (errcode(ERRCODE_NOT_NULL_VIOLATION),
2203                                                  errmsg("null value in column \"%s\" violates not-null constraint",
2204                                                 NameStr(rel->rd_att->attrs[attrChk - 1]->attname))));
2205                 }
2206         }
2207
2208         if (constr->num_check > 0)
2209         {
2210                 const char *failed;
2211
2212                 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
2213                         ereport(ERROR,
2214                                         (errcode(ERRCODE_CHECK_VIOLATION),
2215                                          errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2216                                                         RelationGetRelationName(rel), failed)));
2217         }
2218 }
2219
2220 /*
2221  * ExecProcessReturning --- evaluate a RETURNING list and send to dest
2222  *
2223  * projectReturning: RETURNING projection info for current result rel
2224  * tupleSlot: slot holding tuple actually inserted/updated/deleted
2225  * planSlot: slot holding tuple returned by top plan node
2226  * dest: where to send the output
2227  */
2228 static void
2229 ExecProcessReturning(ProjectionInfo *projectReturning,
2230                                          TupleTableSlot *tupleSlot,
2231                                          TupleTableSlot *planSlot,
2232                                          DestReceiver *dest)
2233 {
2234         ExprContext *econtext = projectReturning->pi_exprContext;
2235         TupleTableSlot *retSlot;
2236
2237         /*
2238          * Reset per-tuple memory context to free any expression evaluation
2239          * storage allocated in the previous cycle.
2240          */
2241         ResetExprContext(econtext);
2242
2243         /* Make tuple and any needed join variables available to ExecProject */
2244         econtext->ecxt_scantuple = tupleSlot;
2245         econtext->ecxt_outertuple = planSlot;
2246
2247         /* Compute the RETURNING expressions */
2248         retSlot = ExecProject(projectReturning, NULL);
2249
2250         /* Send to dest */
2251         (*dest->receiveSlot) (retSlot, dest);
2252
2253         ExecClearTuple(retSlot);
2254 }
2255
2256 /*
2257  * Check a modified tuple to see if we want to process its updated version
2258  * under READ COMMITTED rules.
2259  *
2260  * See backend/executor/README for some info about how this works.
2261  *
2262  *      estate - executor state data
2263  *      rti - rangetable index of table containing tuple
2264  *      *tid - t_ctid from the outdated tuple (ie, next updated version)
2265  *      priorXmax - t_xmax from the outdated tuple
2266  *
2267  * *tid is also an output parameter: it's modified to hold the TID of the
2268  * latest version of the tuple (note this may be changed even on failure)
2269  *
2270  * Returns a slot containing the new candidate update/delete tuple, or
2271  * NULL if we determine we shouldn't process the row.
2272  */
2273 TupleTableSlot *
2274 EvalPlanQual(EState *estate, Index rti,
2275                          ItemPointer tid, TransactionId priorXmax)
2276 {
2277         evalPlanQual *epq;
2278         EState     *epqstate;
2279         Relation        relation;
2280         HeapTupleData tuple;
2281         HeapTuple       copyTuple = NULL;
2282         SnapshotData SnapshotDirty;
2283         bool            endNode;
2284
2285         Assert(rti != 0);
2286
2287         /*
2288          * find relation containing target tuple
2289          */
2290         if (estate->es_result_relation_info != NULL &&
2291                 estate->es_result_relation_info->ri_RangeTableIndex == rti)
2292                 relation = estate->es_result_relation_info->ri_RelationDesc;
2293         else
2294         {
2295                 ListCell   *l;
2296
2297                 relation = NULL;
2298                 foreach(l, estate->es_rowMarks)
2299                 {
2300                         ExecRowMark *erm = lfirst(l);
2301
2302                         if (erm->rti == rti)
2303                         {
2304                                 relation = erm->relation;
2305                                 break;
2306                         }
2307                 }
2308                 if (relation == NULL)
2309                         elog(ERROR, "could not find RowMark for RT index %u", rti);
2310         }
2311
2312         /*
2313          * fetch tid tuple
2314          *
2315          * Loop here to deal with updated or busy tuples
2316          */
2317         InitDirtySnapshot(SnapshotDirty);
2318         tuple.t_self = *tid;
2319         for (;;)
2320         {
2321                 Buffer          buffer;
2322
2323                 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
2324                 {
2325                         /*
2326                          * If xmin isn't what we're expecting, the slot must have been
2327                          * recycled and reused for an unrelated tuple.  This implies that
2328                          * the latest version of the row was deleted, so we need do
2329                          * nothing.  (Should be safe to examine xmin without getting
2330                          * buffer's content lock, since xmin never changes in an existing
2331                          * tuple.)
2332                          */
2333                         if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2334                                                                          priorXmax))
2335                         {
2336                                 ReleaseBuffer(buffer);
2337                                 return NULL;
2338                         }
2339
2340                         /* otherwise xmin should not be dirty... */
2341                         if (TransactionIdIsValid(SnapshotDirty.xmin))
2342                                 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
2343
2344                         /*
2345                          * If tuple is being updated by other transaction then we have to
2346                          * wait for its commit/abort.
2347                          */
2348                         if (TransactionIdIsValid(SnapshotDirty.xmax))
2349                         {
2350                                 ReleaseBuffer(buffer);
2351                                 XactLockTableWait(SnapshotDirty.xmax);
2352                                 continue;               /* loop back to repeat heap_fetch */
2353                         }
2354
2355                         /*
2356                          * If tuple was inserted by our own transaction, we have to check
2357                          * cmin against es_output_cid: cmin >= current CID means our
2358                          * command cannot see the tuple, so we should ignore it.  Without
2359                          * this we are open to the "Halloween problem" of indefinitely
2360                          * re-updating the same tuple. (We need not check cmax because
2361                          * HeapTupleSatisfiesDirty will consider a tuple deleted by our
2362                          * transaction dead, regardless of cmax.)  We just checked that
2363                          * priorXmax == xmin, so we can test that variable instead of
2364                          * doing HeapTupleHeaderGetXmin again.
2365                          */
2366                         if (TransactionIdIsCurrentTransactionId(priorXmax) &&
2367                                 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
2368                         {
2369                                 ReleaseBuffer(buffer);
2370                                 return NULL;
2371                         }
2372
2373                         /*
2374                          * We got tuple - now copy it for use by recheck query.
2375                          */
2376                         copyTuple = heap_copytuple(&tuple);
2377                         ReleaseBuffer(buffer);
2378                         break;
2379                 }
2380
2381                 /*
2382                  * If the referenced slot was actually empty, the latest version of
2383                  * the row must have been deleted, so we need do nothing.
2384                  */
2385                 if (tuple.t_data == NULL)
2386                 {
2387                         ReleaseBuffer(buffer);
2388                         return NULL;
2389                 }
2390
2391                 /*
2392                  * As above, if xmin isn't what we're expecting, do nothing.
2393                  */
2394                 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2395                                                                  priorXmax))
2396                 {
2397                         ReleaseBuffer(buffer);
2398                         return NULL;
2399                 }
2400
2401                 /*
2402                  * If we get here, the tuple was found but failed SnapshotDirty.
2403                  * Assuming the xmin is either a committed xact or our own xact (as it
2404                  * certainly should be if we're trying to modify the tuple), this must
2405                  * mean that the row was updated or deleted by either a committed xact
2406                  * or our own xact.  If it was deleted, we can ignore it; if it was
2407                  * updated then chain up to the next version and repeat the whole
2408                  * test.
2409                  *
2410                  * As above, it should be safe to examine xmax and t_ctid without the
2411                  * buffer content lock, because they can't be changing.
2412                  */
2413                 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2414                 {
2415                         /* deleted, so forget about it */
2416                         ReleaseBuffer(buffer);
2417                         return NULL;
2418                 }
2419
2420                 /* updated, so look at the updated row */
2421                 tuple.t_self = tuple.t_data->t_ctid;
2422                 /* updated row should have xmin matching this xmax */
2423                 priorXmax = HeapTupleHeaderGetXmax(tuple.t_data);
2424                 ReleaseBuffer(buffer);
2425                 /* loop back to fetch next in chain */
2426         }
2427
2428         /*
2429          * For UPDATE/DELETE we have to return tid of actual row we're executing
2430          * PQ for.
2431          */
2432         *tid = tuple.t_self;
2433
2434         /*
2435          * Need to run a recheck subquery.      Find or create a PQ stack entry.
2436          */
2437         epq = estate->es_evalPlanQual;
2438         endNode = true;
2439
2440         if (epq != NULL && epq->rti == 0)
2441         {
2442                 /* Top PQ stack entry is idle, so re-use it */
2443                 Assert(!(estate->es_useEvalPlan) && epq->next == NULL);
2444                 epq->rti = rti;
2445                 endNode = false;
2446         }
2447
2448         /*
2449          * If this is request for another RTE - Ra, - then we have to check wasn't
2450          * PlanQual requested for Ra already and if so then Ra' row was updated
2451          * again and we have to re-start old execution for Ra and forget all what
2452          * we done after Ra was suspended. Cool? -:))
2453          */
2454         if (epq != NULL && epq->rti != rti &&
2455                 epq->estate->es_evTuple[rti - 1] != NULL)
2456         {
2457                 do
2458                 {
2459                         evalPlanQual *oldepq;
2460
2461                         /* stop execution */
2462                         EvalPlanQualStop(epq);
2463                         /* pop previous PlanQual from the stack */
2464                         oldepq = epq->next;
2465                         Assert(oldepq && oldepq->rti != 0);
2466                         /* push current PQ to freePQ stack */
2467                         oldepq->free = epq;
2468                         epq = oldepq;
2469                         estate->es_evalPlanQual = epq;
2470                 } while (epq->rti != rti);
2471         }
2472
2473         /*
2474          * If we are requested for another RTE then we have to suspend execution
2475          * of current PlanQual and start execution for new one.
2476          */
2477         if (epq == NULL || epq->rti != rti)
2478         {
2479                 /* try to reuse plan used previously */
2480                 evalPlanQual *newepq = (epq != NULL) ? epq->free : NULL;
2481
2482                 if (newepq == NULL)             /* first call or freePQ stack is empty */
2483                 {
2484                         newepq = (evalPlanQual *) palloc0(sizeof(evalPlanQual));
2485                         newepq->free = NULL;
2486                         newepq->estate = NULL;
2487                         newepq->planstate = NULL;
2488                 }
2489                 else
2490                 {
2491                         /* recycle previously used PlanQual */
2492                         Assert(newepq->estate == NULL);
2493                         epq->free = NULL;
2494                 }
2495                 /* push current PQ to the stack */
2496                 newepq->next = epq;
2497                 epq = newepq;
2498                 estate->es_evalPlanQual = epq;
2499                 epq->rti = rti;
2500                 endNode = false;
2501         }
2502
2503         Assert(epq->rti == rti);
2504
2505         /*
2506          * Ok - we're requested for the same RTE.  Unfortunately we still have to
2507          * end and restart execution of the plan, because ExecReScan wouldn't
2508          * ensure that upper plan nodes would reset themselves.  We could make
2509          * that work if insertion of the target tuple were integrated with the
2510          * Param mechanism somehow, so that the upper plan nodes know that their
2511          * children's outputs have changed.
2512          *
2513          * Note that the stack of free evalPlanQual nodes is quite useless at the
2514          * moment, since it only saves us from pallocing/releasing the
2515          * evalPlanQual nodes themselves.  But it will be useful once we implement
2516          * ReScan instead of end/restart for re-using PlanQual nodes.
2517          */
2518         if (endNode)
2519         {
2520                 /* stop execution */
2521                 EvalPlanQualStop(epq);
2522         }
2523
2524         /*
2525          * Initialize new recheck query.
2526          *
2527          * Note: if we were re-using PlanQual plans via ExecReScan, we'd need to
2528          * instead copy down changeable state from the top plan (including
2529          * es_result_relation_info, es_junkFilter) and reset locally changeable
2530          * state in the epq (including es_param_exec_vals, es_evTupleNull).
2531          */
2532         EvalPlanQualStart(epq, estate, epq->next);
2533
2534         /*
2535          * free old RTE' tuple, if any, and store target tuple where relation's
2536          * scan node will see it
2537          */
2538         epqstate = epq->estate;
2539         if (epqstate->es_evTuple[rti - 1] != NULL)
2540                 heap_freetuple(epqstate->es_evTuple[rti - 1]);
2541         epqstate->es_evTuple[rti - 1] = copyTuple;
2542
2543         return EvalPlanQualNext(estate);
2544 }
2545
2546 static TupleTableSlot *
2547 EvalPlanQualNext(EState *estate)
2548 {
2549         evalPlanQual *epq = estate->es_evalPlanQual;
2550         MemoryContext oldcontext;
2551         TupleTableSlot *slot;
2552
2553         Assert(epq->rti != 0);
2554
2555 lpqnext:;
2556         oldcontext = MemoryContextSwitchTo(epq->estate->es_query_cxt);
2557         slot = ExecProcNode(epq->planstate);
2558         MemoryContextSwitchTo(oldcontext);
2559
2560         /*
2561          * No more tuples for this PQ. Continue previous one.
2562          */
2563         if (TupIsNull(slot))
2564         {
2565                 evalPlanQual *oldepq;
2566
2567                 /* stop execution */
2568                 EvalPlanQualStop(epq);
2569                 /* pop old PQ from the stack */
2570                 oldepq = epq->next;
2571                 if (oldepq == NULL)
2572                 {
2573                         /* this is the first (oldest) PQ - mark as free */
2574                         epq->rti = 0;
2575                         estate->es_useEvalPlan = false;
2576                         /* and continue Query execution */
2577                         return NULL;
2578                 }
2579                 Assert(oldepq->rti != 0);
2580                 /* push current PQ to freePQ stack */
2581                 oldepq->free = epq;
2582                 epq = oldepq;
2583                 estate->es_evalPlanQual = epq;
2584                 goto lpqnext;
2585         }
2586
2587         return slot;
2588 }
2589
2590 static void
2591 EndEvalPlanQual(EState *estate)
2592 {
2593         evalPlanQual *epq = estate->es_evalPlanQual;
2594
2595         if (epq->rti == 0)                      /* plans already shutdowned */
2596         {
2597                 Assert(epq->next == NULL);
2598                 return;
2599         }
2600
2601         for (;;)
2602         {
2603                 evalPlanQual *oldepq;
2604
2605                 /* stop execution */
2606                 EvalPlanQualStop(epq);
2607                 /* pop old PQ from the stack */
2608                 oldepq = epq->next;
2609                 if (oldepq == NULL)
2610                 {
2611                         /* this is the first (oldest) PQ - mark as free */
2612                         epq->rti = 0;
2613                         estate->es_useEvalPlan = false;
2614                         break;
2615                 }
2616                 Assert(oldepq->rti != 0);
2617                 /* push current PQ to freePQ stack */
2618                 oldepq->free = epq;
2619                 epq = oldepq;
2620                 estate->es_evalPlanQual = epq;
2621         }
2622 }
2623
2624 /*
2625  * Start execution of one level of PlanQual.
2626  *
2627  * This is a cut-down version of ExecutorStart(): we copy some state from
2628  * the top-level estate rather than initializing it fresh.
2629  */
2630 static void
2631 EvalPlanQualStart(evalPlanQual *epq, EState *estate, evalPlanQual *priorepq)
2632 {
2633         EState     *epqstate;
2634         int                     rtsize;
2635         MemoryContext oldcontext;
2636         ListCell   *l;
2637
2638         rtsize = list_length(estate->es_range_table);
2639
2640         epq->estate = epqstate = CreateExecutorState();
2641
2642         oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2643
2644         /*
2645          * The epqstates share the top query's copy of unchanging state such as
2646          * the snapshot, rangetable, result-rel info, and external Param info.
2647          * They need their own copies of local state, including a tuple table,
2648          * es_param_exec_vals, etc.
2649          */
2650         epqstate->es_direction = ForwardScanDirection;
2651         epqstate->es_snapshot = estate->es_snapshot;
2652         epqstate->es_crosscheck_snapshot = estate->es_crosscheck_snapshot;
2653         epqstate->es_range_table = estate->es_range_table;
2654         epqstate->es_output_cid = estate->es_output_cid;
2655         epqstate->es_result_relations = estate->es_result_relations;
2656         epqstate->es_num_result_relations = estate->es_num_result_relations;
2657         epqstate->es_result_relation_info = estate->es_result_relation_info;
2658         epqstate->es_junkFilter = estate->es_junkFilter;
2659         /* es_trig_target_relations must NOT be copied */
2660         epqstate->es_param_list_info = estate->es_param_list_info;
2661         if (estate->es_plannedstmt->nParamExec > 0)
2662                 epqstate->es_param_exec_vals = (ParamExecData *)
2663                         palloc0(estate->es_plannedstmt->nParamExec * sizeof(ParamExecData));
2664         epqstate->es_rowMarks = estate->es_rowMarks;
2665         epqstate->es_instrument = estate->es_instrument;
2666         epqstate->es_select_into = estate->es_select_into;
2667         epqstate->es_into_oids = estate->es_into_oids;
2668         epqstate->es_plannedstmt = estate->es_plannedstmt;
2669
2670         /*
2671          * Each epqstate must have its own es_evTupleNull state, but all the stack
2672          * entries share es_evTuple state.      This allows sub-rechecks to inherit
2673          * the value being examined by an outer recheck.
2674          */
2675         epqstate->es_evTupleNull = (bool *) palloc0(rtsize * sizeof(bool));
2676         if (priorepq == NULL)
2677                 /* first PQ stack entry */
2678                 epqstate->es_evTuple = (HeapTuple *)
2679                         palloc0(rtsize * sizeof(HeapTuple));
2680         else
2681                 /* later stack entries share the same storage */
2682                 epqstate->es_evTuple = priorepq->estate->es_evTuple;
2683
2684         /*
2685          * Each epqstate also has its own tuple table.
2686          */
2687         epqstate->es_tupleTable = NIL;
2688
2689         /*
2690          * Initialize private state information for each SubPlan.  We must do this
2691          * before running ExecInitNode on the main query tree, since
2692          * ExecInitSubPlan expects to be able to find these entries.
2693          */
2694         Assert(epqstate->es_subplanstates == NIL);
2695         foreach(l, estate->es_plannedstmt->subplans)
2696         {
2697                 Plan       *subplan = (Plan *) lfirst(l);
2698                 PlanState  *subplanstate;
2699
2700                 subplanstate = ExecInitNode(subplan, epqstate, 0);
2701
2702                 epqstate->es_subplanstates = lappend(epqstate->es_subplanstates,
2703                                                                                          subplanstate);
2704         }
2705
2706         /*
2707          * Initialize the private state information for all the nodes in the query
2708          * tree.  This opens files, allocates storage and leaves us ready to start
2709          * processing tuples.
2710          */
2711         epq->planstate = ExecInitNode(estate->es_plannedstmt->planTree, epqstate, 0);
2712
2713         MemoryContextSwitchTo(oldcontext);
2714 }
2715
2716 /*
2717  * End execution of one level of PlanQual.
2718  *
2719  * This is a cut-down version of ExecutorEnd(); basically we want to do most
2720  * of the normal cleanup, but *not* close result relations (which we are
2721  * just sharing from the outer query).  We do, however, have to close any
2722  * trigger target relations that got opened, since those are not shared.
2723  */
2724 static void
2725 EvalPlanQualStop(evalPlanQual *epq)
2726 {
2727         EState     *epqstate = epq->estate;
2728         MemoryContext oldcontext;
2729         ListCell   *l;
2730
2731         oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2732
2733         ExecEndNode(epq->planstate);
2734
2735         foreach(l, epqstate->es_subplanstates)
2736         {
2737                 PlanState  *subplanstate = (PlanState *) lfirst(l);
2738
2739                 ExecEndNode(subplanstate);
2740         }
2741
2742         /* throw away the per-epqstate tuple table completely */
2743         ExecResetTupleTable(epqstate->es_tupleTable, true);
2744         epqstate->es_tupleTable = NIL;
2745
2746         if (epqstate->es_evTuple[epq->rti - 1] != NULL)
2747         {
2748                 heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
2749                 epqstate->es_evTuple[epq->rti - 1] = NULL;
2750         }
2751
2752         foreach(l, epqstate->es_trig_target_relations)
2753         {
2754                 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
2755
2756                 /* Close indices and then the relation itself */
2757                 ExecCloseIndices(resultRelInfo);
2758                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2759         }
2760
2761         MemoryContextSwitchTo(oldcontext);
2762
2763         FreeExecutorState(epqstate);
2764
2765         epq->estate = NULL;
2766         epq->planstate = NULL;
2767 }
2768
2769 /*
2770  * ExecGetActivePlanTree --- get the active PlanState tree from a QueryDesc
2771  *
2772  * Ordinarily this is just the one mentioned in the QueryDesc, but if we
2773  * are looking at a row returned by the EvalPlanQual machinery, we need
2774  * to look at the subsidiary state instead.
2775  */
2776 PlanState *
2777 ExecGetActivePlanTree(QueryDesc *queryDesc)
2778 {
2779         EState     *estate = queryDesc->estate;
2780
2781         if (estate && estate->es_useEvalPlan && estate->es_evalPlanQual != NULL)
2782                 return estate->es_evalPlanQual->planstate;
2783         else
2784                 return queryDesc->planstate;
2785 }
2786
2787
2788 /*
2789  * Support for SELECT INTO (a/k/a CREATE TABLE AS)
2790  *
2791  * We implement SELECT INTO by diverting SELECT's normal output with
2792  * a specialized DestReceiver type.
2793  */
2794
2795 typedef struct
2796 {
2797         DestReceiver pub;                       /* publicly-known function pointers */
2798         EState     *estate;                     /* EState we are working with */
2799         Relation        rel;                    /* Relation to write to */
2800         int                     hi_options;             /* heap_insert performance options */
2801         BulkInsertState bistate;        /* bulk insert state */
2802 } DR_intorel;
2803
2804 /*
2805  * OpenIntoRel --- actually create the SELECT INTO target relation
2806  *
2807  * This also replaces QueryDesc->dest with the special DestReceiver for
2808  * SELECT INTO.  We assume that the correct result tuple type has already
2809  * been placed in queryDesc->tupDesc.
2810  */
2811 static void
2812 OpenIntoRel(QueryDesc *queryDesc)
2813 {
2814         IntoClause *into = queryDesc->plannedstmt->intoClause;
2815         EState     *estate = queryDesc->estate;
2816         Relation        intoRelationDesc;
2817         char       *intoName;
2818         Oid                     namespaceId;
2819         Oid                     tablespaceId;
2820         Datum           reloptions;
2821         AclResult       aclresult;
2822         Oid                     intoRelationId;
2823         TupleDesc       tupdesc;
2824         DR_intorel *myState;
2825         static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
2826
2827         Assert(into);
2828
2829         /*
2830          * Check consistency of arguments
2831          */
2832         if (into->onCommit != ONCOMMIT_NOOP && !into->rel->istemp)
2833                 ereport(ERROR,
2834                                 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
2835                                  errmsg("ON COMMIT can only be used on temporary tables")));
2836
2837         /*
2838          * Find namespace to create in, check its permissions
2839          */
2840         intoName = into->rel->relname;
2841         namespaceId = RangeVarGetCreationNamespace(into->rel);
2842
2843         aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
2844                                                                           ACL_CREATE);
2845         if (aclresult != ACLCHECK_OK)
2846                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2847                                            get_namespace_name(namespaceId));
2848
2849         /*
2850          * Select tablespace to use.  If not specified, use default tablespace
2851          * (which may in turn default to database's default).
2852          */
2853         if (into->tableSpaceName)
2854         {
2855                 tablespaceId = get_tablespace_oid(into->tableSpaceName);
2856                 if (!OidIsValid(tablespaceId))
2857                         ereport(ERROR,
2858                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
2859                                          errmsg("tablespace \"%s\" does not exist",
2860                                                         into->tableSpaceName)));
2861         }
2862         else
2863         {
2864                 tablespaceId = GetDefaultTablespace(into->rel->istemp);
2865                 /* note InvalidOid is OK in this case */
2866         }
2867
2868         /* Check permissions except when using the database's default space */
2869         if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
2870         {
2871                 AclResult       aclresult;
2872
2873                 aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
2874                                                                                    ACL_CREATE);
2875
2876                 if (aclresult != ACLCHECK_OK)
2877                         aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
2878                                                    get_tablespace_name(tablespaceId));
2879         }
2880
2881         /* Parse and validate any reloptions */
2882         reloptions = transformRelOptions((Datum) 0,
2883                                                                          into->options,
2884                                                                          NULL,
2885                                                                          validnsps,
2886                                                                          true,
2887                                                                          false);
2888         (void) heap_reloptions(RELKIND_RELATION, reloptions, true);
2889
2890         /* Copy the tupdesc because heap_create_with_catalog modifies it */
2891         tupdesc = CreateTupleDescCopy(queryDesc->tupDesc);
2892
2893         /* Now we can actually create the new relation */
2894         intoRelationId = heap_create_with_catalog(intoName,
2895                                                                                           namespaceId,
2896                                                                                           tablespaceId,
2897                                                                                           InvalidOid,
2898                                                                                           InvalidOid,
2899                                                                                           GetUserId(),
2900                                                                                           tupdesc,
2901                                                                                           NIL,
2902                                                                                           RELKIND_RELATION,
2903                                                                                           false,
2904                                                                                           true,
2905                                                                                           0,
2906                                                                                           into->onCommit,
2907                                                                                           reloptions,
2908                                                                                           true,
2909                                                                                           allowSystemTableMods);
2910
2911         FreeTupleDesc(tupdesc);
2912
2913         /*
2914          * Advance command counter so that the newly-created relation's catalog
2915          * tuples will be visible to heap_open.
2916          */
2917         CommandCounterIncrement();
2918
2919         /*
2920          * If necessary, create a TOAST table for the INTO relation. Note that
2921          * AlterTableCreateToastTable ends with CommandCounterIncrement(), so that
2922          * the TOAST table will be visible for insertion.
2923          */
2924         reloptions = transformRelOptions((Datum) 0,
2925                                                                          into->options,
2926                                                                          "toast",
2927                                                                          validnsps,
2928                                                                          true,
2929                                                                          false);
2930
2931         (void) heap_reloptions(RELKIND_TOASTVALUE, reloptions, true);
2932
2933         AlterTableCreateToastTable(intoRelationId, InvalidOid, reloptions, false);
2934
2935         /*
2936          * And open the constructed table for writing.
2937          */
2938         intoRelationDesc = heap_open(intoRelationId, AccessExclusiveLock);
2939
2940         /*
2941          * Now replace the query's DestReceiver with one for SELECT INTO
2942          */
2943         queryDesc->dest = CreateDestReceiver(DestIntoRel);
2944         myState = (DR_intorel *) queryDesc->dest;
2945         Assert(myState->pub.mydest == DestIntoRel);
2946         myState->estate = estate;
2947         myState->rel = intoRelationDesc;
2948
2949         /*
2950          * We can skip WAL-logging the insertions, unless PITR is in use.  We can
2951          * skip the FSM in any case.
2952          */
2953         myState->hi_options = HEAP_INSERT_SKIP_FSM |
2954                 (XLogArchivingActive() ? 0 : HEAP_INSERT_SKIP_WAL);
2955         myState->bistate = GetBulkInsertState();
2956
2957         /* Not using WAL requires rd_targblock be initially invalid */
2958         Assert(intoRelationDesc->rd_targblock == InvalidBlockNumber);
2959 }
2960
2961 /*
2962  * CloseIntoRel --- clean up SELECT INTO at ExecutorEnd time
2963  */
2964 static void
2965 CloseIntoRel(QueryDesc *queryDesc)
2966 {
2967         DR_intorel *myState = (DR_intorel *) queryDesc->dest;
2968
2969         /* OpenIntoRel might never have gotten called */
2970         if (myState && myState->pub.mydest == DestIntoRel && myState->rel)
2971         {
2972                 FreeBulkInsertState(myState->bistate);
2973
2974                 /* If we skipped using WAL, must heap_sync before commit */
2975                 if (myState->hi_options & HEAP_INSERT_SKIP_WAL)
2976                         heap_sync(myState->rel);
2977
2978                 /* close rel, but keep lock until commit */
2979                 heap_close(myState->rel, NoLock);
2980
2981                 myState->rel = NULL;
2982         }
2983 }
2984
2985 /*
2986  * CreateIntoRelDestReceiver -- create a suitable DestReceiver object
2987  */
2988 DestReceiver *
2989 CreateIntoRelDestReceiver(void)
2990 {
2991         DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
2992
2993         self->pub.receiveSlot = intorel_receive;
2994         self->pub.rStartup = intorel_startup;
2995         self->pub.rShutdown = intorel_shutdown;
2996         self->pub.rDestroy = intorel_destroy;
2997         self->pub.mydest = DestIntoRel;
2998
2999         /* private fields will be set by OpenIntoRel */
3000
3001         return (DestReceiver *) self;
3002 }
3003
3004 /*
3005  * intorel_startup --- executor startup
3006  */
3007 static void
3008 intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
3009 {
3010         /* no-op */
3011 }
3012
3013 /*
3014  * intorel_receive --- receive one tuple
3015  */
3016 static void
3017 intorel_receive(TupleTableSlot *slot, DestReceiver *self)
3018 {
3019         DR_intorel *myState = (DR_intorel *) self;
3020         HeapTuple       tuple;
3021
3022         /*
3023          * get the heap tuple out of the tuple table slot, making sure we have a
3024          * writable copy
3025          */
3026         tuple = ExecMaterializeSlot(slot);
3027
3028         /*
3029          * force assignment of new OID (see comments in ExecInsert)
3030          */
3031         if (myState->rel->rd_rel->relhasoids)
3032                 HeapTupleSetOid(tuple, InvalidOid);
3033
3034         heap_insert(myState->rel,
3035                                 tuple,
3036                                 myState->estate->es_output_cid,
3037                                 myState->hi_options,
3038                                 myState->bistate);
3039
3040         /* We know this is a newly created relation, so there are no indexes */
3041 }
3042
3043 /*
3044  * intorel_shutdown --- executor end
3045  */
3046 static void
3047 intorel_shutdown(DestReceiver *self)
3048 {
3049         /* no-op */
3050 }
3051
3052 /*
3053  * intorel_destroy --- release DestReceiver object
3054  */
3055 static void
3056 intorel_destroy(DestReceiver *self)
3057 {
3058         pfree(self);
3059 }