]> granicus.if.org Git - postgresql/blob - src/backend/executor/execMain.c
Replace the array-style TupleTable data structure with a simple List of
[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.329 2009/09/27 20:09: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         IncrRetrieved();
1708         (estate->es_processed)++;
1709 }
1710
1711 /* ----------------------------------------------------------------
1712  *              ExecInsert
1713  *
1714  *              INSERTs are trickier.. we have to insert the tuple into
1715  *              the base relation and insert appropriate tuples into the
1716  *              index relations.
1717  * ----------------------------------------------------------------
1718  */
1719 static void
1720 ExecInsert(TupleTableSlot *slot,
1721                    ItemPointer tupleid,
1722                    TupleTableSlot *planSlot,
1723                    DestReceiver *dest,
1724                    EState *estate)
1725 {
1726         HeapTuple       tuple;
1727         ResultRelInfo *resultRelInfo;
1728         Relation        resultRelationDesc;
1729         Oid                     newId;
1730         List       *recheckIndexes = NIL;
1731
1732         /*
1733          * get the heap tuple out of the tuple table slot, making sure we have a
1734          * writable copy
1735          */
1736         tuple = ExecMaterializeSlot(slot);
1737
1738         /*
1739          * get information on the (current) result relation
1740          */
1741         resultRelInfo = estate->es_result_relation_info;
1742         resultRelationDesc = resultRelInfo->ri_RelationDesc;
1743
1744         /*
1745          * If the result relation has OIDs, force the tuple's OID to zero so that
1746          * heap_insert will assign a fresh OID.  Usually the OID already will be
1747          * zero at this point, but there are corner cases where the plan tree can
1748          * return a tuple extracted literally from some table with the same
1749          * rowtype.
1750          *
1751          * XXX if we ever wanted to allow users to assign their own OIDs to new
1752          * rows, this'd be the place to do it.  For the moment, we make a point of
1753          * doing this before calling triggers, so that a user-supplied trigger
1754          * could hack the OID if desired.
1755          */
1756         if (resultRelationDesc->rd_rel->relhasoids)
1757                 HeapTupleSetOid(tuple, InvalidOid);
1758
1759         /* BEFORE ROW INSERT Triggers */
1760         if (resultRelInfo->ri_TrigDesc &&
1761                 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1762         {
1763                 HeapTuple       newtuple;
1764
1765                 newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1766
1767                 if (newtuple == NULL)   /* "do nothing" */
1768                         return;
1769
1770                 if (newtuple != tuple)  /* modified by Trigger(s) */
1771                 {
1772                         /*
1773                          * Put the modified tuple into a slot for convenience of routines
1774                          * below.  We assume the tuple was allocated in per-tuple memory
1775                          * context, and therefore will go away by itself. The tuple table
1776                          * slot should not try to clear it.
1777                          */
1778                         TupleTableSlot *newslot = estate->es_trig_tuple_slot;
1779
1780                         if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
1781                                 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
1782                         ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
1783                         slot = newslot;
1784                         tuple = newtuple;
1785                 }
1786         }
1787
1788         /*
1789          * Check the constraints of the tuple
1790          */
1791         if (resultRelationDesc->rd_att->constr)
1792                 ExecConstraints(resultRelInfo, slot, estate);
1793
1794         /*
1795          * insert the tuple
1796          *
1797          * Note: heap_insert returns the tid (location) of the new tuple in the
1798          * t_self field.
1799          */
1800         newId = heap_insert(resultRelationDesc, tuple,
1801                                                 estate->es_output_cid, 0, NULL);
1802
1803         IncrAppended();
1804         (estate->es_processed)++;
1805         estate->es_lastoid = newId;
1806         setLastTid(&(tuple->t_self));
1807
1808         /*
1809          * insert index entries for tuple
1810          */
1811         if (resultRelInfo->ri_NumIndices > 0)
1812                 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
1813                                                                                            estate, false);
1814
1815         /* AFTER ROW INSERT Triggers */
1816         ExecARInsertTriggers(estate, resultRelInfo, tuple, recheckIndexes);
1817
1818         /* Process RETURNING if present */
1819         if (resultRelInfo->ri_projectReturning)
1820                 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1821                                                          slot, planSlot, dest);
1822 }
1823
1824 /* ----------------------------------------------------------------
1825  *              ExecDelete
1826  *
1827  *              DELETE is like UPDATE, except that we delete the tuple and no
1828  *              index modifications are needed
1829  * ----------------------------------------------------------------
1830  */
1831 static void
1832 ExecDelete(ItemPointer tupleid,
1833                    TupleTableSlot *planSlot,
1834                    DestReceiver *dest,
1835                    EState *estate)
1836 {
1837         ResultRelInfo *resultRelInfo;
1838         Relation        resultRelationDesc;
1839         HTSU_Result result;
1840         ItemPointerData update_ctid;
1841         TransactionId update_xmax;
1842
1843         /*
1844          * get information on the (current) result relation
1845          */
1846         resultRelInfo = estate->es_result_relation_info;
1847         resultRelationDesc = resultRelInfo->ri_RelationDesc;
1848
1849         /* BEFORE ROW DELETE Triggers */
1850         if (resultRelInfo->ri_TrigDesc &&
1851                 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
1852         {
1853                 bool            dodelete;
1854
1855                 dodelete = ExecBRDeleteTriggers(estate, resultRelInfo, tupleid);
1856
1857                 if (!dodelete)                  /* "do nothing" */
1858                         return;
1859         }
1860
1861         /*
1862          * delete the tuple
1863          *
1864          * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
1865          * the row to be deleted is visible to that snapshot, and throw a can't-
1866          * serialize error if not.      This is a special-case behavior needed for
1867          * referential integrity updates in serializable transactions.
1868          */
1869 ldelete:;
1870         result = heap_delete(resultRelationDesc, tupleid,
1871                                                  &update_ctid, &update_xmax,
1872                                                  estate->es_output_cid,
1873                                                  estate->es_crosscheck_snapshot,
1874                                                  true /* wait for commit */ );
1875         switch (result)
1876         {
1877                 case HeapTupleSelfUpdated:
1878                         /* already deleted by self; nothing to do */
1879                         return;
1880
1881                 case HeapTupleMayBeUpdated:
1882                         break;
1883
1884                 case HeapTupleUpdated:
1885                         if (IsXactIsoLevelSerializable)
1886                                 ereport(ERROR,
1887                                                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1888                                                  errmsg("could not serialize access due to concurrent update")));
1889                         else if (!ItemPointerEquals(tupleid, &update_ctid))
1890                         {
1891                                 TupleTableSlot *epqslot;
1892
1893                                 epqslot = EvalPlanQual(estate,
1894                                                                            resultRelInfo->ri_RangeTableIndex,
1895                                                                            &update_ctid,
1896                                                                            update_xmax);
1897                                 if (!TupIsNull(epqslot))
1898                                 {
1899                                         *tupleid = update_ctid;
1900                                         goto ldelete;
1901                                 }
1902                         }
1903                         /* tuple already deleted; nothing to do */
1904                         return;
1905
1906                 default:
1907                         elog(ERROR, "unrecognized heap_delete status: %u", result);
1908                         return;
1909         }
1910
1911         IncrDeleted();
1912         (estate->es_processed)++;
1913
1914         /*
1915          * Note: Normally one would think that we have to delete index tuples
1916          * associated with the heap tuple now...
1917          *
1918          * ... but in POSTGRES, we have no need to do this because VACUUM will
1919          * take care of it later.  We can't delete index tuples immediately
1920          * anyway, since the tuple is still visible to other transactions.
1921          */
1922
1923         /* AFTER ROW DELETE Triggers */
1924         ExecARDeleteTriggers(estate, resultRelInfo, tupleid);
1925
1926         /* Process RETURNING if present */
1927         if (resultRelInfo->ri_projectReturning)
1928         {
1929                 /*
1930                  * We have to put the target tuple into a slot, which means first we
1931                  * gotta fetch it.      We can use the trigger tuple slot.
1932                  */
1933                 TupleTableSlot *slot = estate->es_trig_tuple_slot;
1934                 HeapTupleData deltuple;
1935                 Buffer          delbuffer;
1936
1937                 deltuple.t_self = *tupleid;
1938                 if (!heap_fetch(resultRelationDesc, SnapshotAny,
1939                                                 &deltuple, &delbuffer, false, NULL))
1940                         elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
1941
1942                 if (slot->tts_tupleDescriptor != RelationGetDescr(resultRelationDesc))
1943                         ExecSetSlotDescriptor(slot, RelationGetDescr(resultRelationDesc));
1944                 ExecStoreTuple(&deltuple, slot, InvalidBuffer, false);
1945
1946                 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1947                                                          slot, planSlot, dest);
1948
1949                 ExecClearTuple(slot);
1950                 ReleaseBuffer(delbuffer);
1951         }
1952 }
1953
1954 /* ----------------------------------------------------------------
1955  *              ExecUpdate
1956  *
1957  *              note: we can't run UPDATE queries with transactions
1958  *              off because UPDATEs are actually INSERTs and our
1959  *              scan will mistakenly loop forever, updating the tuple
1960  *              it just inserted..      This should be fixed but until it
1961  *              is, we don't want to get stuck in an infinite loop
1962  *              which corrupts your database..
1963  * ----------------------------------------------------------------
1964  */
1965 static void
1966 ExecUpdate(TupleTableSlot *slot,
1967                    ItemPointer tupleid,
1968                    TupleTableSlot *planSlot,
1969                    DestReceiver *dest,
1970                    EState *estate)
1971 {
1972         HeapTuple       tuple;
1973         ResultRelInfo *resultRelInfo;
1974         Relation        resultRelationDesc;
1975         HTSU_Result result;
1976         ItemPointerData update_ctid;
1977         TransactionId update_xmax;
1978         List *recheckIndexes = NIL;
1979
1980         /*
1981          * abort the operation if not running transactions
1982          */
1983         if (IsBootstrapProcessingMode())
1984                 elog(ERROR, "cannot UPDATE during bootstrap");
1985
1986         /*
1987          * get the heap tuple out of the tuple table slot, making sure we have a
1988          * writable copy
1989          */
1990         tuple = ExecMaterializeSlot(slot);
1991
1992         /*
1993          * get information on the (current) result relation
1994          */
1995         resultRelInfo = estate->es_result_relation_info;
1996         resultRelationDesc = resultRelInfo->ri_RelationDesc;
1997
1998         /* BEFORE ROW UPDATE Triggers */
1999         if (resultRelInfo->ri_TrigDesc &&
2000                 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
2001         {
2002                 HeapTuple       newtuple;
2003
2004                 newtuple = ExecBRUpdateTriggers(estate, resultRelInfo,
2005                                                                                 tupleid, tuple);
2006
2007                 if (newtuple == NULL)   /* "do nothing" */
2008                         return;
2009
2010                 if (newtuple != tuple)  /* modified by Trigger(s) */
2011                 {
2012                         /*
2013                          * Put the modified tuple into a slot for convenience of routines
2014                          * below.  We assume the tuple was allocated in per-tuple memory
2015                          * context, and therefore will go away by itself. The tuple table
2016                          * slot should not try to clear it.
2017                          */
2018                         TupleTableSlot *newslot = estate->es_trig_tuple_slot;
2019
2020                         if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
2021                                 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
2022                         ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
2023                         slot = newslot;
2024                         tuple = newtuple;
2025                 }
2026         }
2027
2028         /*
2029          * Check the constraints of the tuple
2030          *
2031          * If we generate a new candidate tuple after EvalPlanQual testing, we
2032          * must loop back here and recheck constraints.  (We don't need to redo
2033          * triggers, however.  If there are any BEFORE triggers then trigger.c
2034          * will have done heap_lock_tuple to lock the correct tuple, so there's no
2035          * need to do them again.)
2036          */
2037 lreplace:;
2038         if (resultRelationDesc->rd_att->constr)
2039                 ExecConstraints(resultRelInfo, slot, estate);
2040
2041         /*
2042          * replace the heap tuple
2043          *
2044          * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
2045          * the row to be updated is visible to that snapshot, and throw a can't-
2046          * serialize error if not.      This is a special-case behavior needed for
2047          * referential integrity updates in serializable transactions.
2048          */
2049         result = heap_update(resultRelationDesc, tupleid, tuple,
2050                                                  &update_ctid, &update_xmax,
2051                                                  estate->es_output_cid,
2052                                                  estate->es_crosscheck_snapshot,
2053                                                  true /* wait for commit */ );
2054         switch (result)
2055         {
2056                 case HeapTupleSelfUpdated:
2057                         /* already deleted by self; nothing to do */
2058                         return;
2059
2060                 case HeapTupleMayBeUpdated:
2061                         break;
2062
2063                 case HeapTupleUpdated:
2064                         if (IsXactIsoLevelSerializable)
2065                                 ereport(ERROR,
2066                                                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2067                                                  errmsg("could not serialize access due to concurrent update")));
2068                         else if (!ItemPointerEquals(tupleid, &update_ctid))
2069                         {
2070                                 TupleTableSlot *epqslot;
2071
2072                                 epqslot = EvalPlanQual(estate,
2073                                                                            resultRelInfo->ri_RangeTableIndex,
2074                                                                            &update_ctid,
2075                                                                            update_xmax);
2076                                 if (!TupIsNull(epqslot))
2077                                 {
2078                                         *tupleid = update_ctid;
2079                                         slot = ExecFilterJunk(estate->es_junkFilter, epqslot);
2080                                         tuple = ExecMaterializeSlot(slot);
2081                                         goto lreplace;
2082                                 }
2083                         }
2084                         /* tuple already deleted; nothing to do */
2085                         return;
2086
2087                 default:
2088                         elog(ERROR, "unrecognized heap_update status: %u", result);
2089                         return;
2090         }
2091
2092         IncrReplaced();
2093         (estate->es_processed)++;
2094
2095         /*
2096          * Note: instead of having to update the old index tuples associated with
2097          * the heap tuple, all we do is form and insert new index tuples. This is
2098          * because UPDATEs are actually DELETEs and INSERTs, and index tuple
2099          * deletion is done later by VACUUM (see notes in ExecDelete).  All we do
2100          * here is insert new index tuples.  -cim 9/27/89
2101          */
2102
2103         /*
2104          * insert index entries for tuple
2105          *
2106          * Note: heap_update returns the tid (location) of the new tuple in the
2107          * t_self field.
2108          *
2109          * If it's a HOT update, we mustn't insert new index entries.
2110          */
2111         if (resultRelInfo->ri_NumIndices > 0 && !HeapTupleIsHeapOnly(tuple))
2112                 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
2113                                                                                            estate, false);
2114
2115         /* AFTER ROW UPDATE Triggers */
2116         ExecARUpdateTriggers(estate, resultRelInfo, tupleid, tuple,
2117                                                  recheckIndexes);
2118
2119         /* Process RETURNING if present */
2120         if (resultRelInfo->ri_projectReturning)
2121                 ExecProcessReturning(resultRelInfo->ri_projectReturning,
2122                                                          slot, planSlot, dest);
2123 }
2124
2125 /*
2126  * ExecRelCheck --- check that tuple meets constraints for result relation
2127  */
2128 static const char *
2129 ExecRelCheck(ResultRelInfo *resultRelInfo,
2130                          TupleTableSlot *slot, EState *estate)
2131 {
2132         Relation        rel = resultRelInfo->ri_RelationDesc;
2133         int                     ncheck = rel->rd_att->constr->num_check;
2134         ConstrCheck *check = rel->rd_att->constr->check;
2135         ExprContext *econtext;
2136         MemoryContext oldContext;
2137         List       *qual;
2138         int                     i;
2139
2140         /*
2141          * If first time through for this result relation, build expression
2142          * nodetrees for rel's constraint expressions.  Keep them in the per-query
2143          * memory context so they'll survive throughout the query.
2144          */
2145         if (resultRelInfo->ri_ConstraintExprs == NULL)
2146         {
2147                 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
2148                 resultRelInfo->ri_ConstraintExprs =
2149                         (List **) palloc(ncheck * sizeof(List *));
2150                 for (i = 0; i < ncheck; i++)
2151                 {
2152                         /* ExecQual wants implicit-AND form */
2153                         qual = make_ands_implicit(stringToNode(check[i].ccbin));
2154                         resultRelInfo->ri_ConstraintExprs[i] = (List *)
2155                                 ExecPrepareExpr((Expr *) qual, estate);
2156                 }
2157                 MemoryContextSwitchTo(oldContext);
2158         }
2159
2160         /*
2161          * We will use the EState's per-tuple context for evaluating constraint
2162          * expressions (creating it if it's not already there).
2163          */
2164         econtext = GetPerTupleExprContext(estate);
2165
2166         /* Arrange for econtext's scan tuple to be the tuple under test */
2167         econtext->ecxt_scantuple = slot;
2168
2169         /* And evaluate the constraints */
2170         for (i = 0; i < ncheck; i++)
2171         {
2172                 qual = resultRelInfo->ri_ConstraintExprs[i];
2173
2174                 /*
2175                  * NOTE: SQL92 specifies that a NULL result from a constraint
2176                  * expression is not to be treated as a failure.  Therefore, tell
2177                  * ExecQual to return TRUE for NULL.
2178                  */
2179                 if (!ExecQual(qual, econtext, true))
2180                         return check[i].ccname;
2181         }
2182
2183         /* NULL result means no error */
2184         return NULL;
2185 }
2186
2187 void
2188 ExecConstraints(ResultRelInfo *resultRelInfo,
2189                                 TupleTableSlot *slot, EState *estate)
2190 {
2191         Relation        rel = resultRelInfo->ri_RelationDesc;
2192         TupleConstr *constr = rel->rd_att->constr;
2193
2194         Assert(constr);
2195
2196         if (constr->has_not_null)
2197         {
2198                 int                     natts = rel->rd_att->natts;
2199                 int                     attrChk;
2200
2201                 for (attrChk = 1; attrChk <= natts; attrChk++)
2202                 {
2203                         if (rel->rd_att->attrs[attrChk - 1]->attnotnull &&
2204                                 slot_attisnull(slot, attrChk))
2205                                 ereport(ERROR,
2206                                                 (errcode(ERRCODE_NOT_NULL_VIOLATION),
2207                                                  errmsg("null value in column \"%s\" violates not-null constraint",
2208                                                 NameStr(rel->rd_att->attrs[attrChk - 1]->attname))));
2209                 }
2210         }
2211
2212         if (constr->num_check > 0)
2213         {
2214                 const char *failed;
2215
2216                 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
2217                         ereport(ERROR,
2218                                         (errcode(ERRCODE_CHECK_VIOLATION),
2219                                          errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2220                                                         RelationGetRelationName(rel), failed)));
2221         }
2222 }
2223
2224 /*
2225  * ExecProcessReturning --- evaluate a RETURNING list and send to dest
2226  *
2227  * projectReturning: RETURNING projection info for current result rel
2228  * tupleSlot: slot holding tuple actually inserted/updated/deleted
2229  * planSlot: slot holding tuple returned by top plan node
2230  * dest: where to send the output
2231  */
2232 static void
2233 ExecProcessReturning(ProjectionInfo *projectReturning,
2234                                          TupleTableSlot *tupleSlot,
2235                                          TupleTableSlot *planSlot,
2236                                          DestReceiver *dest)
2237 {
2238         ExprContext *econtext = projectReturning->pi_exprContext;
2239         TupleTableSlot *retSlot;
2240
2241         /*
2242          * Reset per-tuple memory context to free any expression evaluation
2243          * storage allocated in the previous cycle.
2244          */
2245         ResetExprContext(econtext);
2246
2247         /* Make tuple and any needed join variables available to ExecProject */
2248         econtext->ecxt_scantuple = tupleSlot;
2249         econtext->ecxt_outertuple = planSlot;
2250
2251         /* Compute the RETURNING expressions */
2252         retSlot = ExecProject(projectReturning, NULL);
2253
2254         /* Send to dest */
2255         (*dest->receiveSlot) (retSlot, dest);
2256
2257         ExecClearTuple(retSlot);
2258 }
2259
2260 /*
2261  * Check a modified tuple to see if we want to process its updated version
2262  * under READ COMMITTED rules.
2263  *
2264  * See backend/executor/README for some info about how this works.
2265  *
2266  *      estate - executor state data
2267  *      rti - rangetable index of table containing tuple
2268  *      *tid - t_ctid from the outdated tuple (ie, next updated version)
2269  *      priorXmax - t_xmax from the outdated tuple
2270  *
2271  * *tid is also an output parameter: it's modified to hold the TID of the
2272  * latest version of the tuple (note this may be changed even on failure)
2273  *
2274  * Returns a slot containing the new candidate update/delete tuple, or
2275  * NULL if we determine we shouldn't process the row.
2276  */
2277 TupleTableSlot *
2278 EvalPlanQual(EState *estate, Index rti,
2279                          ItemPointer tid, TransactionId priorXmax)
2280 {
2281         evalPlanQual *epq;
2282         EState     *epqstate;
2283         Relation        relation;
2284         HeapTupleData tuple;
2285         HeapTuple       copyTuple = NULL;
2286         SnapshotData SnapshotDirty;
2287         bool            endNode;
2288
2289         Assert(rti != 0);
2290
2291         /*
2292          * find relation containing target tuple
2293          */
2294         if (estate->es_result_relation_info != NULL &&
2295                 estate->es_result_relation_info->ri_RangeTableIndex == rti)
2296                 relation = estate->es_result_relation_info->ri_RelationDesc;
2297         else
2298         {
2299                 ListCell   *l;
2300
2301                 relation = NULL;
2302                 foreach(l, estate->es_rowMarks)
2303                 {
2304                         ExecRowMark *erm = lfirst(l);
2305
2306                         if (erm->rti == rti)
2307                         {
2308                                 relation = erm->relation;
2309                                 break;
2310                         }
2311                 }
2312                 if (relation == NULL)
2313                         elog(ERROR, "could not find RowMark for RT index %u", rti);
2314         }
2315
2316         /*
2317          * fetch tid tuple
2318          *
2319          * Loop here to deal with updated or busy tuples
2320          */
2321         InitDirtySnapshot(SnapshotDirty);
2322         tuple.t_self = *tid;
2323         for (;;)
2324         {
2325                 Buffer          buffer;
2326
2327                 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
2328                 {
2329                         /*
2330                          * If xmin isn't what we're expecting, the slot must have been
2331                          * recycled and reused for an unrelated tuple.  This implies that
2332                          * the latest version of the row was deleted, so we need do
2333                          * nothing.  (Should be safe to examine xmin without getting
2334                          * buffer's content lock, since xmin never changes in an existing
2335                          * tuple.)
2336                          */
2337                         if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2338                                                                          priorXmax))
2339                         {
2340                                 ReleaseBuffer(buffer);
2341                                 return NULL;
2342                         }
2343
2344                         /* otherwise xmin should not be dirty... */
2345                         if (TransactionIdIsValid(SnapshotDirty.xmin))
2346                                 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
2347
2348                         /*
2349                          * If tuple is being updated by other transaction then we have to
2350                          * wait for its commit/abort.
2351                          */
2352                         if (TransactionIdIsValid(SnapshotDirty.xmax))
2353                         {
2354                                 ReleaseBuffer(buffer);
2355                                 XactLockTableWait(SnapshotDirty.xmax);
2356                                 continue;               /* loop back to repeat heap_fetch */
2357                         }
2358
2359                         /*
2360                          * If tuple was inserted by our own transaction, we have to check
2361                          * cmin against es_output_cid: cmin >= current CID means our
2362                          * command cannot see the tuple, so we should ignore it.  Without
2363                          * this we are open to the "Halloween problem" of indefinitely
2364                          * re-updating the same tuple. (We need not check cmax because
2365                          * HeapTupleSatisfiesDirty will consider a tuple deleted by our
2366                          * transaction dead, regardless of cmax.)  We just checked that
2367                          * priorXmax == xmin, so we can test that variable instead of
2368                          * doing HeapTupleHeaderGetXmin again.
2369                          */
2370                         if (TransactionIdIsCurrentTransactionId(priorXmax) &&
2371                                 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
2372                         {
2373                                 ReleaseBuffer(buffer);
2374                                 return NULL;
2375                         }
2376
2377                         /*
2378                          * We got tuple - now copy it for use by recheck query.
2379                          */
2380                         copyTuple = heap_copytuple(&tuple);
2381                         ReleaseBuffer(buffer);
2382                         break;
2383                 }
2384
2385                 /*
2386                  * If the referenced slot was actually empty, the latest version of
2387                  * the row must have been deleted, so we need do nothing.
2388                  */
2389                 if (tuple.t_data == NULL)
2390                 {
2391                         ReleaseBuffer(buffer);
2392                         return NULL;
2393                 }
2394
2395                 /*
2396                  * As above, if xmin isn't what we're expecting, do nothing.
2397                  */
2398                 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2399                                                                  priorXmax))
2400                 {
2401                         ReleaseBuffer(buffer);
2402                         return NULL;
2403                 }
2404
2405                 /*
2406                  * If we get here, the tuple was found but failed SnapshotDirty.
2407                  * Assuming the xmin is either a committed xact or our own xact (as it
2408                  * certainly should be if we're trying to modify the tuple), this must
2409                  * mean that the row was updated or deleted by either a committed xact
2410                  * or our own xact.  If it was deleted, we can ignore it; if it was
2411                  * updated then chain up to the next version and repeat the whole
2412                  * test.
2413                  *
2414                  * As above, it should be safe to examine xmax and t_ctid without the
2415                  * buffer content lock, because they can't be changing.
2416                  */
2417                 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2418                 {
2419                         /* deleted, so forget about it */
2420                         ReleaseBuffer(buffer);
2421                         return NULL;
2422                 }
2423
2424                 /* updated, so look at the updated row */
2425                 tuple.t_self = tuple.t_data->t_ctid;
2426                 /* updated row should have xmin matching this xmax */
2427                 priorXmax = HeapTupleHeaderGetXmax(tuple.t_data);
2428                 ReleaseBuffer(buffer);
2429                 /* loop back to fetch next in chain */
2430         }
2431
2432         /*
2433          * For UPDATE/DELETE we have to return tid of actual row we're executing
2434          * PQ for.
2435          */
2436         *tid = tuple.t_self;
2437
2438         /*
2439          * Need to run a recheck subquery.      Find or create a PQ stack entry.
2440          */
2441         epq = estate->es_evalPlanQual;
2442         endNode = true;
2443
2444         if (epq != NULL && epq->rti == 0)
2445         {
2446                 /* Top PQ stack entry is idle, so re-use it */
2447                 Assert(!(estate->es_useEvalPlan) && epq->next == NULL);
2448                 epq->rti = rti;
2449                 endNode = false;
2450         }
2451
2452         /*
2453          * If this is request for another RTE - Ra, - then we have to check wasn't
2454          * PlanQual requested for Ra already and if so then Ra' row was updated
2455          * again and we have to re-start old execution for Ra and forget all what
2456          * we done after Ra was suspended. Cool? -:))
2457          */
2458         if (epq != NULL && epq->rti != rti &&
2459                 epq->estate->es_evTuple[rti - 1] != NULL)
2460         {
2461                 do
2462                 {
2463                         evalPlanQual *oldepq;
2464
2465                         /* stop execution */
2466                         EvalPlanQualStop(epq);
2467                         /* pop previous PlanQual from the stack */
2468                         oldepq = epq->next;
2469                         Assert(oldepq && oldepq->rti != 0);
2470                         /* push current PQ to freePQ stack */
2471                         oldepq->free = epq;
2472                         epq = oldepq;
2473                         estate->es_evalPlanQual = epq;
2474                 } while (epq->rti != rti);
2475         }
2476
2477         /*
2478          * If we are requested for another RTE then we have to suspend execution
2479          * of current PlanQual and start execution for new one.
2480          */
2481         if (epq == NULL || epq->rti != rti)
2482         {
2483                 /* try to reuse plan used previously */
2484                 evalPlanQual *newepq = (epq != NULL) ? epq->free : NULL;
2485
2486                 if (newepq == NULL)             /* first call or freePQ stack is empty */
2487                 {
2488                         newepq = (evalPlanQual *) palloc0(sizeof(evalPlanQual));
2489                         newepq->free = NULL;
2490                         newepq->estate = NULL;
2491                         newepq->planstate = NULL;
2492                 }
2493                 else
2494                 {
2495                         /* recycle previously used PlanQual */
2496                         Assert(newepq->estate == NULL);
2497                         epq->free = NULL;
2498                 }
2499                 /* push current PQ to the stack */
2500                 newepq->next = epq;
2501                 epq = newepq;
2502                 estate->es_evalPlanQual = epq;
2503                 epq->rti = rti;
2504                 endNode = false;
2505         }
2506
2507         Assert(epq->rti == rti);
2508
2509         /*
2510          * Ok - we're requested for the same RTE.  Unfortunately we still have to
2511          * end and restart execution of the plan, because ExecReScan wouldn't
2512          * ensure that upper plan nodes would reset themselves.  We could make
2513          * that work if insertion of the target tuple were integrated with the
2514          * Param mechanism somehow, so that the upper plan nodes know that their
2515          * children's outputs have changed.
2516          *
2517          * Note that the stack of free evalPlanQual nodes is quite useless at the
2518          * moment, since it only saves us from pallocing/releasing the
2519          * evalPlanQual nodes themselves.  But it will be useful once we implement
2520          * ReScan instead of end/restart for re-using PlanQual nodes.
2521          */
2522         if (endNode)
2523         {
2524                 /* stop execution */
2525                 EvalPlanQualStop(epq);
2526         }
2527
2528         /*
2529          * Initialize new recheck query.
2530          *
2531          * Note: if we were re-using PlanQual plans via ExecReScan, we'd need to
2532          * instead copy down changeable state from the top plan (including
2533          * es_result_relation_info, es_junkFilter) and reset locally changeable
2534          * state in the epq (including es_param_exec_vals, es_evTupleNull).
2535          */
2536         EvalPlanQualStart(epq, estate, epq->next);
2537
2538         /*
2539          * free old RTE' tuple, if any, and store target tuple where relation's
2540          * scan node will see it
2541          */
2542         epqstate = epq->estate;
2543         if (epqstate->es_evTuple[rti - 1] != NULL)
2544                 heap_freetuple(epqstate->es_evTuple[rti - 1]);
2545         epqstate->es_evTuple[rti - 1] = copyTuple;
2546
2547         return EvalPlanQualNext(estate);
2548 }
2549
2550 static TupleTableSlot *
2551 EvalPlanQualNext(EState *estate)
2552 {
2553         evalPlanQual *epq = estate->es_evalPlanQual;
2554         MemoryContext oldcontext;
2555         TupleTableSlot *slot;
2556
2557         Assert(epq->rti != 0);
2558
2559 lpqnext:;
2560         oldcontext = MemoryContextSwitchTo(epq->estate->es_query_cxt);
2561         slot = ExecProcNode(epq->planstate);
2562         MemoryContextSwitchTo(oldcontext);
2563
2564         /*
2565          * No more tuples for this PQ. Continue previous one.
2566          */
2567         if (TupIsNull(slot))
2568         {
2569                 evalPlanQual *oldepq;
2570
2571                 /* stop execution */
2572                 EvalPlanQualStop(epq);
2573                 /* pop old PQ from the stack */
2574                 oldepq = epq->next;
2575                 if (oldepq == NULL)
2576                 {
2577                         /* this is the first (oldest) PQ - mark as free */
2578                         epq->rti = 0;
2579                         estate->es_useEvalPlan = false;
2580                         /* and continue Query execution */
2581                         return NULL;
2582                 }
2583                 Assert(oldepq->rti != 0);
2584                 /* push current PQ to freePQ stack */
2585                 oldepq->free = epq;
2586                 epq = oldepq;
2587                 estate->es_evalPlanQual = epq;
2588                 goto lpqnext;
2589         }
2590
2591         return slot;
2592 }
2593
2594 static void
2595 EndEvalPlanQual(EState *estate)
2596 {
2597         evalPlanQual *epq = estate->es_evalPlanQual;
2598
2599         if (epq->rti == 0)                      /* plans already shutdowned */
2600         {
2601                 Assert(epq->next == NULL);
2602                 return;
2603         }
2604
2605         for (;;)
2606         {
2607                 evalPlanQual *oldepq;
2608
2609                 /* stop execution */
2610                 EvalPlanQualStop(epq);
2611                 /* pop old PQ from the stack */
2612                 oldepq = epq->next;
2613                 if (oldepq == NULL)
2614                 {
2615                         /* this is the first (oldest) PQ - mark as free */
2616                         epq->rti = 0;
2617                         estate->es_useEvalPlan = false;
2618                         break;
2619                 }
2620                 Assert(oldepq->rti != 0);
2621                 /* push current PQ to freePQ stack */
2622                 oldepq->free = epq;
2623                 epq = oldepq;
2624                 estate->es_evalPlanQual = epq;
2625         }
2626 }
2627
2628 /*
2629  * Start execution of one level of PlanQual.
2630  *
2631  * This is a cut-down version of ExecutorStart(): we copy some state from
2632  * the top-level estate rather than initializing it fresh.
2633  */
2634 static void
2635 EvalPlanQualStart(evalPlanQual *epq, EState *estate, evalPlanQual *priorepq)
2636 {
2637         EState     *epqstate;
2638         int                     rtsize;
2639         MemoryContext oldcontext;
2640         ListCell   *l;
2641
2642         rtsize = list_length(estate->es_range_table);
2643
2644         epq->estate = epqstate = CreateExecutorState();
2645
2646         oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2647
2648         /*
2649          * The epqstates share the top query's copy of unchanging state such as
2650          * the snapshot, rangetable, result-rel info, and external Param info.
2651          * They need their own copies of local state, including a tuple table,
2652          * es_param_exec_vals, etc.
2653          */
2654         epqstate->es_direction = ForwardScanDirection;
2655         epqstate->es_snapshot = estate->es_snapshot;
2656         epqstate->es_crosscheck_snapshot = estate->es_crosscheck_snapshot;
2657         epqstate->es_range_table = estate->es_range_table;
2658         epqstate->es_output_cid = estate->es_output_cid;
2659         epqstate->es_result_relations = estate->es_result_relations;
2660         epqstate->es_num_result_relations = estate->es_num_result_relations;
2661         epqstate->es_result_relation_info = estate->es_result_relation_info;
2662         epqstate->es_junkFilter = estate->es_junkFilter;
2663         /* es_trig_target_relations must NOT be copied */
2664         epqstate->es_param_list_info = estate->es_param_list_info;
2665         if (estate->es_plannedstmt->nParamExec > 0)
2666                 epqstate->es_param_exec_vals = (ParamExecData *)
2667                         palloc0(estate->es_plannedstmt->nParamExec * sizeof(ParamExecData));
2668         epqstate->es_rowMarks = estate->es_rowMarks;
2669         epqstate->es_instrument = estate->es_instrument;
2670         epqstate->es_select_into = estate->es_select_into;
2671         epqstate->es_into_oids = estate->es_into_oids;
2672         epqstate->es_plannedstmt = estate->es_plannedstmt;
2673
2674         /*
2675          * Each epqstate must have its own es_evTupleNull state, but all the stack
2676          * entries share es_evTuple state.      This allows sub-rechecks to inherit
2677          * the value being examined by an outer recheck.
2678          */
2679         epqstate->es_evTupleNull = (bool *) palloc0(rtsize * sizeof(bool));
2680         if (priorepq == NULL)
2681                 /* first PQ stack entry */
2682                 epqstate->es_evTuple = (HeapTuple *)
2683                         palloc0(rtsize * sizeof(HeapTuple));
2684         else
2685                 /* later stack entries share the same storage */
2686                 epqstate->es_evTuple = priorepq->estate->es_evTuple;
2687
2688         /*
2689          * Each epqstate also has its own tuple table.
2690          */
2691         epqstate->es_tupleTable = NIL;
2692
2693         /*
2694          * Initialize private state information for each SubPlan.  We must do this
2695          * before running ExecInitNode on the main query tree, since
2696          * ExecInitSubPlan expects to be able to find these entries.
2697          */
2698         Assert(epqstate->es_subplanstates == NIL);
2699         foreach(l, estate->es_plannedstmt->subplans)
2700         {
2701                 Plan       *subplan = (Plan *) lfirst(l);
2702                 PlanState  *subplanstate;
2703
2704                 subplanstate = ExecInitNode(subplan, epqstate, 0);
2705
2706                 epqstate->es_subplanstates = lappend(epqstate->es_subplanstates,
2707                                                                                          subplanstate);
2708         }
2709
2710         /*
2711          * Initialize the private state information for all the nodes in the query
2712          * tree.  This opens files, allocates storage and leaves us ready to start
2713          * processing tuples.
2714          */
2715         epq->planstate = ExecInitNode(estate->es_plannedstmt->planTree, epqstate, 0);
2716
2717         MemoryContextSwitchTo(oldcontext);
2718 }
2719
2720 /*
2721  * End execution of one level of PlanQual.
2722  *
2723  * This is a cut-down version of ExecutorEnd(); basically we want to do most
2724  * of the normal cleanup, but *not* close result relations (which we are
2725  * just sharing from the outer query).  We do, however, have to close any
2726  * trigger target relations that got opened, since those are not shared.
2727  */
2728 static void
2729 EvalPlanQualStop(evalPlanQual *epq)
2730 {
2731         EState     *epqstate = epq->estate;
2732         MemoryContext oldcontext;
2733         ListCell   *l;
2734
2735         oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2736
2737         ExecEndNode(epq->planstate);
2738
2739         foreach(l, epqstate->es_subplanstates)
2740         {
2741                 PlanState  *subplanstate = (PlanState *) lfirst(l);
2742
2743                 ExecEndNode(subplanstate);
2744         }
2745
2746         /* throw away the per-epqstate tuple table completely */
2747         ExecResetTupleTable(epqstate->es_tupleTable, true);
2748         epqstate->es_tupleTable = NIL;
2749
2750         if (epqstate->es_evTuple[epq->rti - 1] != NULL)
2751         {
2752                 heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
2753                 epqstate->es_evTuple[epq->rti - 1] = NULL;
2754         }
2755
2756         foreach(l, epqstate->es_trig_target_relations)
2757         {
2758                 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
2759
2760                 /* Close indices and then the relation itself */
2761                 ExecCloseIndices(resultRelInfo);
2762                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2763         }
2764
2765         MemoryContextSwitchTo(oldcontext);
2766
2767         FreeExecutorState(epqstate);
2768
2769         epq->estate = NULL;
2770         epq->planstate = NULL;
2771 }
2772
2773 /*
2774  * ExecGetActivePlanTree --- get the active PlanState tree from a QueryDesc
2775  *
2776  * Ordinarily this is just the one mentioned in the QueryDesc, but if we
2777  * are looking at a row returned by the EvalPlanQual machinery, we need
2778  * to look at the subsidiary state instead.
2779  */
2780 PlanState *
2781 ExecGetActivePlanTree(QueryDesc *queryDesc)
2782 {
2783         EState     *estate = queryDesc->estate;
2784
2785         if (estate && estate->es_useEvalPlan && estate->es_evalPlanQual != NULL)
2786                 return estate->es_evalPlanQual->planstate;
2787         else
2788                 return queryDesc->planstate;
2789 }
2790
2791
2792 /*
2793  * Support for SELECT INTO (a/k/a CREATE TABLE AS)
2794  *
2795  * We implement SELECT INTO by diverting SELECT's normal output with
2796  * a specialized DestReceiver type.
2797  */
2798
2799 typedef struct
2800 {
2801         DestReceiver pub;                       /* publicly-known function pointers */
2802         EState     *estate;                     /* EState we are working with */
2803         Relation        rel;                    /* Relation to write to */
2804         int                     hi_options;             /* heap_insert performance options */
2805         BulkInsertState bistate;        /* bulk insert state */
2806 } DR_intorel;
2807
2808 /*
2809  * OpenIntoRel --- actually create the SELECT INTO target relation
2810  *
2811  * This also replaces QueryDesc->dest with the special DestReceiver for
2812  * SELECT INTO.  We assume that the correct result tuple type has already
2813  * been placed in queryDesc->tupDesc.
2814  */
2815 static void
2816 OpenIntoRel(QueryDesc *queryDesc)
2817 {
2818         IntoClause *into = queryDesc->plannedstmt->intoClause;
2819         EState     *estate = queryDesc->estate;
2820         Relation        intoRelationDesc;
2821         char       *intoName;
2822         Oid                     namespaceId;
2823         Oid                     tablespaceId;
2824         Datum           reloptions;
2825         AclResult       aclresult;
2826         Oid                     intoRelationId;
2827         TupleDesc       tupdesc;
2828         DR_intorel *myState;
2829         static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
2830
2831         Assert(into);
2832
2833         /*
2834          * Check consistency of arguments
2835          */
2836         if (into->onCommit != ONCOMMIT_NOOP && !into->rel->istemp)
2837                 ereport(ERROR,
2838                                 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
2839                                  errmsg("ON COMMIT can only be used on temporary tables")));
2840
2841         /*
2842          * Find namespace to create in, check its permissions
2843          */
2844         intoName = into->rel->relname;
2845         namespaceId = RangeVarGetCreationNamespace(into->rel);
2846
2847         aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
2848                                                                           ACL_CREATE);
2849         if (aclresult != ACLCHECK_OK)
2850                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2851                                            get_namespace_name(namespaceId));
2852
2853         /*
2854          * Select tablespace to use.  If not specified, use default tablespace
2855          * (which may in turn default to database's default).
2856          */
2857         if (into->tableSpaceName)
2858         {
2859                 tablespaceId = get_tablespace_oid(into->tableSpaceName);
2860                 if (!OidIsValid(tablespaceId))
2861                         ereport(ERROR,
2862                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
2863                                          errmsg("tablespace \"%s\" does not exist",
2864                                                         into->tableSpaceName)));
2865         }
2866         else
2867         {
2868                 tablespaceId = GetDefaultTablespace(into->rel->istemp);
2869                 /* note InvalidOid is OK in this case */
2870         }
2871
2872         /* Check permissions except when using the database's default space */
2873         if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
2874         {
2875                 AclResult       aclresult;
2876
2877                 aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
2878                                                                                    ACL_CREATE);
2879
2880                 if (aclresult != ACLCHECK_OK)
2881                         aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
2882                                                    get_tablespace_name(tablespaceId));
2883         }
2884
2885         /* Parse and validate any reloptions */
2886         reloptions = transformRelOptions((Datum) 0,
2887                                                                          into->options,
2888                                                                          NULL,
2889                                                                          validnsps,
2890                                                                          true,
2891                                                                          false);
2892         (void) heap_reloptions(RELKIND_RELATION, reloptions, true);
2893
2894         /* Copy the tupdesc because heap_create_with_catalog modifies it */
2895         tupdesc = CreateTupleDescCopy(queryDesc->tupDesc);
2896
2897         /* Now we can actually create the new relation */
2898         intoRelationId = heap_create_with_catalog(intoName,
2899                                                                                           namespaceId,
2900                                                                                           tablespaceId,
2901                                                                                           InvalidOid,
2902                                                                                           InvalidOid,
2903                                                                                           GetUserId(),
2904                                                                                           tupdesc,
2905                                                                                           NIL,
2906                                                                                           RELKIND_RELATION,
2907                                                                                           false,
2908                                                                                           true,
2909                                                                                           0,
2910                                                                                           into->onCommit,
2911                                                                                           reloptions,
2912                                                                                           allowSystemTableMods);
2913
2914         FreeTupleDesc(tupdesc);
2915
2916         /*
2917          * Advance command counter so that the newly-created relation's catalog
2918          * tuples will be visible to heap_open.
2919          */
2920         CommandCounterIncrement();
2921
2922         /*
2923          * If necessary, create a TOAST table for the INTO relation. Note that
2924          * AlterTableCreateToastTable ends with CommandCounterIncrement(), so that
2925          * the TOAST table will be visible for insertion.
2926          */
2927         reloptions = transformRelOptions((Datum) 0,
2928                                                                          into->options,
2929                                                                          "toast",
2930                                                                          validnsps,
2931                                                                          true,
2932                                                                          false);
2933
2934         (void) heap_reloptions(RELKIND_TOASTVALUE, reloptions, true);
2935
2936         AlterTableCreateToastTable(intoRelationId, InvalidOid, reloptions, false);
2937
2938         /*
2939          * And open the constructed table for writing.
2940          */
2941         intoRelationDesc = heap_open(intoRelationId, AccessExclusiveLock);
2942
2943         /*
2944          * Now replace the query's DestReceiver with one for SELECT INTO
2945          */
2946         queryDesc->dest = CreateDestReceiver(DestIntoRel);
2947         myState = (DR_intorel *) queryDesc->dest;
2948         Assert(myState->pub.mydest == DestIntoRel);
2949         myState->estate = estate;
2950         myState->rel = intoRelationDesc;
2951
2952         /*
2953          * We can skip WAL-logging the insertions, unless PITR is in use.  We can
2954          * skip the FSM in any case.
2955          */
2956         myState->hi_options = HEAP_INSERT_SKIP_FSM |
2957                 (XLogArchivingActive() ? 0 : HEAP_INSERT_SKIP_WAL);
2958         myState->bistate = GetBulkInsertState();
2959
2960         /* Not using WAL requires rd_targblock be initially invalid */
2961         Assert(intoRelationDesc->rd_targblock == InvalidBlockNumber);
2962 }
2963
2964 /*
2965  * CloseIntoRel --- clean up SELECT INTO at ExecutorEnd time
2966  */
2967 static void
2968 CloseIntoRel(QueryDesc *queryDesc)
2969 {
2970         DR_intorel *myState = (DR_intorel *) queryDesc->dest;
2971
2972         /* OpenIntoRel might never have gotten called */
2973         if (myState && myState->pub.mydest == DestIntoRel && myState->rel)
2974         {
2975                 FreeBulkInsertState(myState->bistate);
2976
2977                 /* If we skipped using WAL, must heap_sync before commit */
2978                 if (myState->hi_options & HEAP_INSERT_SKIP_WAL)
2979                         heap_sync(myState->rel);
2980
2981                 /* close rel, but keep lock until commit */
2982                 heap_close(myState->rel, NoLock);
2983
2984                 myState->rel = NULL;
2985         }
2986 }
2987
2988 /*
2989  * CreateIntoRelDestReceiver -- create a suitable DestReceiver object
2990  */
2991 DestReceiver *
2992 CreateIntoRelDestReceiver(void)
2993 {
2994         DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
2995
2996         self->pub.receiveSlot = intorel_receive;
2997         self->pub.rStartup = intorel_startup;
2998         self->pub.rShutdown = intorel_shutdown;
2999         self->pub.rDestroy = intorel_destroy;
3000         self->pub.mydest = DestIntoRel;
3001
3002         /* private fields will be set by OpenIntoRel */
3003
3004         return (DestReceiver *) self;
3005 }
3006
3007 /*
3008  * intorel_startup --- executor startup
3009  */
3010 static void
3011 intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
3012 {
3013         /* no-op */
3014 }
3015
3016 /*
3017  * intorel_receive --- receive one tuple
3018  */
3019 static void
3020 intorel_receive(TupleTableSlot *slot, DestReceiver *self)
3021 {
3022         DR_intorel *myState = (DR_intorel *) self;
3023         HeapTuple       tuple;
3024
3025         /*
3026          * get the heap tuple out of the tuple table slot, making sure we have a
3027          * writable copy
3028          */
3029         tuple = ExecMaterializeSlot(slot);
3030
3031         /*
3032          * force assignment of new OID (see comments in ExecInsert)
3033          */
3034         if (myState->rel->rd_rel->relhasoids)
3035                 HeapTupleSetOid(tuple, InvalidOid);
3036
3037         heap_insert(myState->rel,
3038                                 tuple,
3039                                 myState->estate->es_output_cid,
3040                                 myState->hi_options,
3041                                 myState->bistate);
3042
3043         /* We know this is a newly created relation, so there are no indexes */
3044
3045         IncrAppended();
3046 }
3047
3048 /*
3049  * intorel_shutdown --- executor end
3050  */
3051 static void
3052 intorel_shutdown(DestReceiver *self)
3053 {
3054         /* no-op */
3055 }
3056
3057 /*
3058  * intorel_destroy --- release DestReceiver object
3059  */
3060 static void
3061 intorel_destroy(DestReceiver *self)
3062 {
3063         pfree(self);
3064 }