]> granicus.if.org Git - postgresql/blob - src/backend/executor/execMain.c
Incidental cleanup of matviews code.
[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  *      ExecutorFinish()
10  *      ExecutorEnd()
11  *
12  *      These four procedures are the external interface to the executor.
13  *      In each case, the query descriptor is required as an argument.
14  *
15  *      ExecutorStart must be called at the beginning of execution of any
16  *      query plan and ExecutorEnd must always be called at the end of
17  *      execution of a plan (unless it is aborted due to error).
18  *
19  *      ExecutorRun accepts direction and count arguments that specify whether
20  *      the plan is to be executed forwards, backwards, and for how many tuples.
21  *      In some cases ExecutorRun may be called multiple times to process all
22  *      the tuples for a plan.  It is also acceptable to stop short of executing
23  *      the whole plan (but only if it is a SELECT).
24  *
25  *      ExecutorFinish must be called after the final ExecutorRun call and
26  *      before ExecutorEnd.  This can be omitted only in case of EXPLAIN,
27  *      which should also omit ExecutorRun.
28  *
29  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
30  * Portions Copyright (c) 1994, Regents of the University of California
31  *
32  *
33  * IDENTIFICATION
34  *        src/backend/executor/execMain.c
35  *
36  *-------------------------------------------------------------------------
37  */
38 #include "postgres.h"
39
40 #include "access/htup_details.h"
41 #include "access/sysattr.h"
42 #include "access/transam.h"
43 #include "access/xact.h"
44 #include "catalog/namespace.h"
45 #include "commands/trigger.h"
46 #include "executor/execdebug.h"
47 #include "foreign/fdwapi.h"
48 #include "mb/pg_wchar.h"
49 #include "miscadmin.h"
50 #include "optimizer/clauses.h"
51 #include "parser/parsetree.h"
52 #include "storage/bufmgr.h"
53 #include "storage/lmgr.h"
54 #include "tcop/utility.h"
55 #include "utils/acl.h"
56 #include "utils/lsyscache.h"
57 #include "utils/memutils.h"
58 #include "utils/snapmgr.h"
59 #include "utils/tqual.h"
60
61
62 /* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
63 ExecutorStart_hook_type ExecutorStart_hook = NULL;
64 ExecutorRun_hook_type ExecutorRun_hook = NULL;
65 ExecutorFinish_hook_type ExecutorFinish_hook = NULL;
66 ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
67
68 /* Hook for plugin to get control in ExecCheckRTPerms() */
69 ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;
70
71 /* decls for local routines only used within this module */
72 static void InitPlan(QueryDesc *queryDesc, int eflags);
73 static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
74 static void ExecPostprocessPlan(EState *estate);
75 static void ExecEndPlan(PlanState *planstate, EState *estate);
76 static void ExecutePlan(EState *estate, PlanState *planstate,
77                         CmdType operation,
78                         bool sendTuples,
79                         long numberTuples,
80                         ScanDirection direction,
81                         DestReceiver *dest);
82 static bool ExecCheckRTEPerms(RangeTblEntry *rte);
83 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
84 static char *ExecBuildSlotValueDescription(TupleTableSlot *slot,
85                                                           int maxfieldlen);
86 static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate,
87                                   Plan *planTree);
88
89 /* end of local decls */
90
91
92 /* ----------------------------------------------------------------
93  *              ExecutorStart
94  *
95  *              This routine must be called at the beginning of any execution of any
96  *              query plan
97  *
98  * Takes a QueryDesc previously created by CreateQueryDesc (which is separate
99  * only because some places use QueryDescs for utility commands).  The tupDesc
100  * field of the QueryDesc is filled in to describe the tuples that will be
101  * returned, and the internal fields (estate and planstate) are set up.
102  *
103  * eflags contains flag bits as described in executor.h.
104  *
105  * NB: the CurrentMemoryContext when this is called will become the parent
106  * of the per-query context used for this Executor invocation.
107  *
108  * We provide a function hook variable that lets loadable plugins
109  * get control when ExecutorStart is called.  Such a plugin would
110  * normally call standard_ExecutorStart().
111  *
112  * ----------------------------------------------------------------
113  */
114 void
115 ExecutorStart(QueryDesc *queryDesc, int eflags)
116 {
117         if (ExecutorStart_hook)
118                 (*ExecutorStart_hook) (queryDesc, eflags);
119         else
120                 standard_ExecutorStart(queryDesc, eflags);
121 }
122
123 void
124 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
125 {
126         EState     *estate;
127         MemoryContext oldcontext;
128
129         /* sanity checks: queryDesc must not be started already */
130         Assert(queryDesc != NULL);
131         Assert(queryDesc->estate == NULL);
132
133         /*
134          * If the transaction is read-only, we need to check if any writes are
135          * planned to non-temporary tables.  EXPLAIN is considered read-only.
136          */
137         if (XactReadOnly && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
138                 ExecCheckXactReadOnly(queryDesc->plannedstmt);
139
140         /*
141          * Build EState, switch into per-query memory context for startup.
142          */
143         estate = CreateExecutorState();
144         queryDesc->estate = estate;
145
146         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
147
148         /*
149          * Fill in external parameters, if any, from queryDesc; and allocate
150          * workspace for internal parameters
151          */
152         estate->es_param_list_info = queryDesc->params;
153
154         if (queryDesc->plannedstmt->nParamExec > 0)
155                 estate->es_param_exec_vals = (ParamExecData *)
156                         palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData));
157
158         /*
159          * If non-read-only query, set the command ID to mark output tuples with
160          */
161         switch (queryDesc->operation)
162         {
163                 case CMD_SELECT:
164
165                         /*
166                          * SELECT FOR [KEY] UPDATE/SHARE and modifying CTEs need to mark
167                          * tuples
168                          */
169                         if (queryDesc->plannedstmt->rowMarks != NIL ||
170                                 queryDesc->plannedstmt->hasModifyingCTE)
171                                 estate->es_output_cid = GetCurrentCommandId(true);
172
173                         /*
174                          * A SELECT without modifying CTEs can't possibly queue triggers,
175                          * so force skip-triggers mode. This is just a marginal efficiency
176                          * hack, since AfterTriggerBeginQuery/AfterTriggerEndQuery aren't
177                          * all that expensive, but we might as well do it.
178                          */
179                         if (!queryDesc->plannedstmt->hasModifyingCTE)
180                                 eflags |= EXEC_FLAG_SKIP_TRIGGERS;
181                         break;
182
183                 case CMD_INSERT:
184                 case CMD_DELETE:
185                 case CMD_UPDATE:
186                         estate->es_output_cid = GetCurrentCommandId(true);
187                         break;
188
189                 default:
190                         elog(ERROR, "unrecognized operation code: %d",
191                                  (int) queryDesc->operation);
192                         break;
193         }
194
195         /*
196          * Copy other important information into the EState
197          */
198         estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
199         estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
200         estate->es_top_eflags = eflags;
201         estate->es_instrument = queryDesc->instrument_options;
202
203         /*
204          * Initialize the plan state tree
205          */
206         InitPlan(queryDesc, eflags);
207
208         /*
209          * Set up an AFTER-trigger statement context, unless told not to, or
210          * unless it's EXPLAIN-only mode (when ExecutorFinish won't be called).
211          */
212         if (!(eflags & (EXEC_FLAG_SKIP_TRIGGERS | EXEC_FLAG_EXPLAIN_ONLY)))
213                 AfterTriggerBeginQuery();
214
215         MemoryContextSwitchTo(oldcontext);
216 }
217
218 /* ----------------------------------------------------------------
219  *              ExecutorRun
220  *
221  *              This is the main routine of the executor module. It accepts
222  *              the query descriptor from the traffic cop and executes the
223  *              query plan.
224  *
225  *              ExecutorStart must have been called already.
226  *
227  *              If direction is NoMovementScanDirection then nothing is done
228  *              except to start up/shut down the destination.  Otherwise,
229  *              we retrieve up to 'count' tuples in the specified direction.
230  *
231  *              Note: count = 0 is interpreted as no portal limit, i.e., run to
232  *              completion.  Also note that the count limit is only applied to
233  *              retrieved tuples, not for instance to those inserted/updated/deleted
234  *              by a ModifyTable plan node.
235  *
236  *              There is no return value, but output tuples (if any) are sent to
237  *              the destination receiver specified in the QueryDesc; and the number
238  *              of tuples processed at the top level can be found in
239  *              estate->es_processed.
240  *
241  *              We provide a function hook variable that lets loadable plugins
242  *              get control when ExecutorRun is called.  Such a plugin would
243  *              normally call standard_ExecutorRun().
244  *
245  * ----------------------------------------------------------------
246  */
247 void
248 ExecutorRun(QueryDesc *queryDesc,
249                         ScanDirection direction, long count)
250 {
251         if (ExecutorRun_hook)
252                 (*ExecutorRun_hook) (queryDesc, direction, count);
253         else
254                 standard_ExecutorRun(queryDesc, direction, count);
255 }
256
257 void
258 standard_ExecutorRun(QueryDesc *queryDesc,
259                                          ScanDirection direction, long count)
260 {
261         EState     *estate;
262         CmdType         operation;
263         DestReceiver *dest;
264         bool            sendTuples;
265         MemoryContext oldcontext;
266
267         /* sanity checks */
268         Assert(queryDesc != NULL);
269
270         estate = queryDesc->estate;
271
272         Assert(estate != NULL);
273         Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
274
275         /*
276          * Switch into per-query memory context
277          */
278         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
279
280         /* Allow instrumentation of Executor overall runtime */
281         if (queryDesc->totaltime)
282                 InstrStartNode(queryDesc->totaltime);
283
284         /*
285          * extract information from the query descriptor and the query feature.
286          */
287         operation = queryDesc->operation;
288         dest = queryDesc->dest;
289
290         /*
291          * startup tuple receiver, if we will be emitting tuples
292          */
293         estate->es_processed = 0;
294         estate->es_lastoid = InvalidOid;
295
296         sendTuples = (operation == CMD_SELECT ||
297                                   queryDesc->plannedstmt->hasReturning);
298
299         if (sendTuples)
300                 (*dest->rStartup) (dest, operation, queryDesc->tupDesc);
301
302         /*
303          * run plan
304          */
305         if (!ScanDirectionIsNoMovement(direction))
306                 ExecutePlan(estate,
307                                         queryDesc->planstate,
308                                         operation,
309                                         sendTuples,
310                                         count,
311                                         direction,
312                                         dest);
313
314         /*
315          * shutdown tuple receiver, if we started it
316          */
317         if (sendTuples)
318                 (*dest->rShutdown) (dest);
319
320         if (queryDesc->totaltime)
321                 InstrStopNode(queryDesc->totaltime, estate->es_processed);
322
323         MemoryContextSwitchTo(oldcontext);
324 }
325
326 /* ----------------------------------------------------------------
327  *              ExecutorFinish
328  *
329  *              This routine must be called after the last ExecutorRun call.
330  *              It performs cleanup such as firing AFTER triggers.      It is
331  *              separate from ExecutorEnd because EXPLAIN ANALYZE needs to
332  *              include these actions in the total runtime.
333  *
334  *              We provide a function hook variable that lets loadable plugins
335  *              get control when ExecutorFinish is called.      Such a plugin would
336  *              normally call standard_ExecutorFinish().
337  *
338  * ----------------------------------------------------------------
339  */
340 void
341 ExecutorFinish(QueryDesc *queryDesc)
342 {
343         if (ExecutorFinish_hook)
344                 (*ExecutorFinish_hook) (queryDesc);
345         else
346                 standard_ExecutorFinish(queryDesc);
347 }
348
349 void
350 standard_ExecutorFinish(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         Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
362
363         /* This should be run once and only once per Executor instance */
364         Assert(!estate->es_finished);
365
366         /* Switch into per-query memory context */
367         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
368
369         /* Allow instrumentation of Executor overall runtime */
370         if (queryDesc->totaltime)
371                 InstrStartNode(queryDesc->totaltime);
372
373         /* Run ModifyTable nodes to completion */
374         ExecPostprocessPlan(estate);
375
376         /* Execute queued AFTER triggers, unless told not to */
377         if (!(estate->es_top_eflags & EXEC_FLAG_SKIP_TRIGGERS))
378                 AfterTriggerEndQuery(estate);
379
380         if (queryDesc->totaltime)
381                 InstrStopNode(queryDesc->totaltime, 0);
382
383         MemoryContextSwitchTo(oldcontext);
384
385         estate->es_finished = true;
386 }
387
388 /* ----------------------------------------------------------------
389  *              ExecutorEnd
390  *
391  *              This routine must be called at the end of execution of any
392  *              query plan
393  *
394  *              We provide a function hook variable that lets loadable plugins
395  *              get control when ExecutorEnd is called.  Such a plugin would
396  *              normally call standard_ExecutorEnd().
397  *
398  * ----------------------------------------------------------------
399  */
400 void
401 ExecutorEnd(QueryDesc *queryDesc)
402 {
403         if (ExecutorEnd_hook)
404                 (*ExecutorEnd_hook) (queryDesc);
405         else
406                 standard_ExecutorEnd(queryDesc);
407 }
408
409 void
410 standard_ExecutorEnd(QueryDesc *queryDesc)
411 {
412         EState     *estate;
413         MemoryContext oldcontext;
414
415         /* sanity checks */
416         Assert(queryDesc != NULL);
417
418         estate = queryDesc->estate;
419
420         Assert(estate != NULL);
421
422         /*
423          * Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
424          * Assert is needed because ExecutorFinish is new as of 9.1, and callers
425          * might forget to call it.
426          */
427         Assert(estate->es_finished ||
428                    (estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
429
430         /*
431          * Switch into per-query memory context to run ExecEndPlan
432          */
433         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
434
435         ExecEndPlan(queryDesc->planstate, estate);
436
437         /* do away with our snapshots */
438         UnregisterSnapshot(estate->es_snapshot);
439         UnregisterSnapshot(estate->es_crosscheck_snapshot);
440
441         /*
442          * Must switch out of context before destroying it
443          */
444         MemoryContextSwitchTo(oldcontext);
445
446         /*
447          * Release EState and per-query memory context.  This should release
448          * everything the executor has allocated.
449          */
450         FreeExecutorState(estate);
451
452         /* Reset queryDesc fields that no longer point to anything */
453         queryDesc->tupDesc = NULL;
454         queryDesc->estate = NULL;
455         queryDesc->planstate = NULL;
456         queryDesc->totaltime = NULL;
457 }
458
459 /* ----------------------------------------------------------------
460  *              ExecutorRewind
461  *
462  *              This routine may be called on an open queryDesc to rewind it
463  *              to the start.
464  * ----------------------------------------------------------------
465  */
466 void
467 ExecutorRewind(QueryDesc *queryDesc)
468 {
469         EState     *estate;
470         MemoryContext oldcontext;
471
472         /* sanity checks */
473         Assert(queryDesc != NULL);
474
475         estate = queryDesc->estate;
476
477         Assert(estate != NULL);
478
479         /* It's probably not sensible to rescan updating queries */
480         Assert(queryDesc->operation == CMD_SELECT);
481
482         /*
483          * Switch into per-query memory context
484          */
485         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
486
487         /*
488          * rescan plan
489          */
490         ExecReScan(queryDesc->planstate);
491
492         MemoryContextSwitchTo(oldcontext);
493 }
494
495
496 /*
497  * ExecCheckRTPerms
498  *              Check access permissions for all relations listed in a range table.
499  *
500  * Returns true if permissions are adequate.  Otherwise, throws an appropriate
501  * error if ereport_on_violation is true, or simply returns false otherwise.
502  */
503 bool
504 ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
505 {
506         ListCell   *l;
507         bool            result = true;
508
509         foreach(l, rangeTable)
510         {
511                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
512
513                 result = ExecCheckRTEPerms(rte);
514                 if (!result)
515                 {
516                         Assert(rte->rtekind == RTE_RELATION);
517                         if (ereport_on_violation)
518                                 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
519                                                            get_rel_name(rte->relid));
520                         return false;
521                 }
522         }
523
524         if (ExecutorCheckPerms_hook)
525                 result = (*ExecutorCheckPerms_hook) (rangeTable,
526                                                                                          ereport_on_violation);
527         return result;
528 }
529
530 /*
531  * ExecCheckRTEPerms
532  *              Check access permissions for a single RTE.
533  */
534 static bool
535 ExecCheckRTEPerms(RangeTblEntry *rte)
536 {
537         AclMode         requiredPerms;
538         AclMode         relPerms;
539         AclMode         remainingPerms;
540         Oid                     relOid;
541         Oid                     userid;
542         Bitmapset  *tmpset;
543         int                     col;
544
545         /*
546          * Only plain-relation RTEs need to be checked here.  Function RTEs are
547          * checked by init_fcache when the function is prepared for execution.
548          * Join, subquery, and special RTEs need no checks.
549          */
550         if (rte->rtekind != RTE_RELATION)
551                 return true;
552
553         /*
554          * No work if requiredPerms is empty.
555          */
556         requiredPerms = rte->requiredPerms;
557         if (requiredPerms == 0)
558                 return true;
559
560         relOid = rte->relid;
561
562         /*
563          * userid to check as: current user unless we have a setuid indication.
564          *
565          * Note: GetUserId() is presently fast enough that there's no harm in
566          * calling it separately for each RTE.  If that stops being true, we could
567          * call it once in ExecCheckRTPerms and pass the userid down from there.
568          * But for now, no need for the extra clutter.
569          */
570         userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
571
572         /*
573          * We must have *all* the requiredPerms bits, but some of the bits can be
574          * satisfied from column-level rather than relation-level permissions.
575          * First, remove any bits that are satisfied by relation permissions.
576          */
577         relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
578         remainingPerms = requiredPerms & ~relPerms;
579         if (remainingPerms != 0)
580         {
581                 /*
582                  * If we lack any permissions that exist only as relation permissions,
583                  * we can fail straight away.
584                  */
585                 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
586                         return false;
587
588                 /*
589                  * Check to see if we have the needed privileges at column level.
590                  *
591                  * Note: failures just report a table-level error; it would be nicer
592                  * to report a column-level error if we have some but not all of the
593                  * column privileges.
594                  */
595                 if (remainingPerms & ACL_SELECT)
596                 {
597                         /*
598                          * When the query doesn't explicitly reference any columns (for
599                          * example, SELECT COUNT(*) FROM table), allow the query if we
600                          * have SELECT on any column of the rel, as per SQL spec.
601                          */
602                         if (bms_is_empty(rte->selectedCols))
603                         {
604                                 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
605                                                                                           ACLMASK_ANY) != ACLCHECK_OK)
606                                         return false;
607                         }
608
609                         tmpset = bms_copy(rte->selectedCols);
610                         while ((col = bms_first_member(tmpset)) >= 0)
611                         {
612                                 /* remove the column number offset */
613                                 col += FirstLowInvalidHeapAttributeNumber;
614                                 if (col == InvalidAttrNumber)
615                                 {
616                                         /* Whole-row reference, must have priv on all cols */
617                                         if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
618                                                                                                   ACLMASK_ALL) != ACLCHECK_OK)
619                                                 return false;
620                                 }
621                                 else
622                                 {
623                                         if (pg_attribute_aclcheck(relOid, col, userid,
624                                                                                           ACL_SELECT) != ACLCHECK_OK)
625                                                 return false;
626                                 }
627                         }
628                         bms_free(tmpset);
629                 }
630
631                 /*
632                  * Basically the same for the mod columns, with either INSERT or
633                  * UPDATE privilege as specified by remainingPerms.
634                  */
635                 remainingPerms &= ~ACL_SELECT;
636                 if (remainingPerms != 0)
637                 {
638                         /*
639                          * When the query doesn't explicitly change any columns, allow the
640                          * query if we have permission on any column of the rel.  This is
641                          * to handle SELECT FOR UPDATE as well as possible corner cases in
642                          * INSERT and UPDATE.
643                          */
644                         if (bms_is_empty(rte->modifiedCols))
645                         {
646                                 if (pg_attribute_aclcheck_all(relOid, userid, remainingPerms,
647                                                                                           ACLMASK_ANY) != ACLCHECK_OK)
648                                         return false;
649                         }
650
651                         tmpset = bms_copy(rte->modifiedCols);
652                         while ((col = bms_first_member(tmpset)) >= 0)
653                         {
654                                 /* remove the column number offset */
655                                 col += FirstLowInvalidHeapAttributeNumber;
656                                 if (col == InvalidAttrNumber)
657                                 {
658                                         /* whole-row reference can't happen here */
659                                         elog(ERROR, "whole-row update is not implemented");
660                                 }
661                                 else
662                                 {
663                                         if (pg_attribute_aclcheck(relOid, col, userid,
664                                                                                           remainingPerms) != ACLCHECK_OK)
665                                                 return false;
666                                 }
667                         }
668                         bms_free(tmpset);
669                 }
670         }
671         return true;
672 }
673
674 /*
675  * Check that the query does not imply any writes to non-temp tables.
676  *
677  * Note: in a Hot Standby slave this would need to reject writes to temp
678  * tables as well; but an HS slave can't have created any temp tables
679  * in the first place, so no need to check that.
680  */
681 static void
682 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
683 {
684         ListCell   *l;
685
686         /* Fail if write permissions are requested on any non-temp table */
687         foreach(l, plannedstmt->rtable)
688         {
689                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
690
691                 if (rte->rtekind != RTE_RELATION)
692                         continue;
693
694                 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
695                         continue;
696
697                 if (isTempNamespace(get_rel_namespace(rte->relid)))
698                         continue;
699
700                 PreventCommandIfReadOnly(CreateCommandTag((Node *) plannedstmt));
701         }
702 }
703
704
705 /* ----------------------------------------------------------------
706  *              InitPlan
707  *
708  *              Initializes the query plan: open files, allocate storage
709  *              and start up the rule manager
710  * ----------------------------------------------------------------
711  */
712 static void
713 InitPlan(QueryDesc *queryDesc, int eflags)
714 {
715         CmdType         operation = queryDesc->operation;
716         PlannedStmt *plannedstmt = queryDesc->plannedstmt;
717         Plan       *plan = plannedstmt->planTree;
718         List       *rangeTable = plannedstmt->rtable;
719         EState     *estate = queryDesc->estate;
720         PlanState  *planstate;
721         TupleDesc       tupType;
722         ListCell   *l;
723         int                     i;
724
725         /*
726          * Do permissions checks
727          */
728         ExecCheckRTPerms(rangeTable, true);
729
730         /*
731          * initialize the node's execution state
732          */
733         estate->es_range_table = rangeTable;
734         estate->es_plannedstmt = plannedstmt;
735
736         /*
737          * initialize result relation stuff, and open/lock the result rels.
738          *
739          * We must do this before initializing the plan tree, else we might try to
740          * do a lock upgrade if a result rel is also a source rel.
741          */
742         if (plannedstmt->resultRelations)
743         {
744                 List       *resultRelations = plannedstmt->resultRelations;
745                 int                     numResultRelations = list_length(resultRelations);
746                 ResultRelInfo *resultRelInfos;
747                 ResultRelInfo *resultRelInfo;
748
749                 resultRelInfos = (ResultRelInfo *)
750                         palloc(numResultRelations * sizeof(ResultRelInfo));
751                 resultRelInfo = resultRelInfos;
752                 foreach(l, resultRelations)
753                 {
754                         Index           resultRelationIndex = lfirst_int(l);
755                         Oid                     resultRelationOid;
756                         Relation        resultRelation;
757
758                         resultRelationOid = getrelid(resultRelationIndex, rangeTable);
759                         resultRelation = heap_open(resultRelationOid, RowExclusiveLock);
760                         InitResultRelInfo(resultRelInfo,
761                                                           resultRelation,
762                                                           resultRelationIndex,
763                                                           estate->es_instrument);
764                         resultRelInfo++;
765                 }
766                 estate->es_result_relations = resultRelInfos;
767                 estate->es_num_result_relations = numResultRelations;
768                 /* es_result_relation_info is NULL except when within ModifyTable */
769                 estate->es_result_relation_info = NULL;
770         }
771         else
772         {
773                 /*
774                  * if no result relation, then set state appropriately
775                  */
776                 estate->es_result_relations = NULL;
777                 estate->es_num_result_relations = 0;
778                 estate->es_result_relation_info = NULL;
779         }
780
781         /*
782          * Similarly, we have to lock relations selected FOR [KEY] UPDATE/SHARE
783          * before we initialize the plan tree, else we'd be risking lock upgrades.
784          * While we are at it, build the ExecRowMark list.
785          */
786         estate->es_rowMarks = NIL;
787         foreach(l, plannedstmt->rowMarks)
788         {
789                 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
790                 Oid                     relid;
791                 Relation        relation;
792                 ExecRowMark *erm;
793
794                 /* ignore "parent" rowmarks; they are irrelevant at runtime */
795                 if (rc->isParent)
796                         continue;
797
798                 switch (rc->markType)
799                 {
800                         case ROW_MARK_EXCLUSIVE:
801                         case ROW_MARK_NOKEYEXCLUSIVE:
802                         case ROW_MARK_SHARE:
803                         case ROW_MARK_KEYSHARE:
804                                 relid = getrelid(rc->rti, rangeTable);
805                                 relation = heap_open(relid, RowShareLock);
806                                 break;
807                         case ROW_MARK_REFERENCE:
808                                 relid = getrelid(rc->rti, rangeTable);
809                                 relation = heap_open(relid, AccessShareLock);
810                                 break;
811                         case ROW_MARK_COPY:
812                                 /* there's no real table here ... */
813                                 relation = NULL;
814                                 break;
815                         default:
816                                 elog(ERROR, "unrecognized markType: %d", rc->markType);
817                                 relation = NULL;        /* keep compiler quiet */
818                                 break;
819                 }
820
821                 /* Check that relation is a legal target for marking */
822                 if (relation)
823                         CheckValidRowMarkRel(relation, rc->markType);
824
825                 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
826                 erm->relation = relation;
827                 erm->rti = rc->rti;
828                 erm->prti = rc->prti;
829                 erm->rowmarkId = rc->rowmarkId;
830                 erm->markType = rc->markType;
831                 erm->noWait = rc->noWait;
832                 ItemPointerSetInvalid(&(erm->curCtid));
833                 estate->es_rowMarks = lappend(estate->es_rowMarks, erm);
834         }
835
836         /*
837          * Initialize the executor's tuple table to empty.
838          */
839         estate->es_tupleTable = NIL;
840         estate->es_trig_tuple_slot = NULL;
841         estate->es_trig_oldtup_slot = NULL;
842         estate->es_trig_newtup_slot = NULL;
843
844         /* mark EvalPlanQual not active */
845         estate->es_epqTuple = NULL;
846         estate->es_epqTupleSet = NULL;
847         estate->es_epqScanDone = NULL;
848
849         /*
850          * Initialize private state information for each SubPlan.  We must do this
851          * before running ExecInitNode on the main query tree, since
852          * ExecInitSubPlan expects to be able to find these entries.
853          */
854         Assert(estate->es_subplanstates == NIL);
855         i = 1;                                          /* subplan indices count from 1 */
856         foreach(l, plannedstmt->subplans)
857         {
858                 Plan       *subplan = (Plan *) lfirst(l);
859                 PlanState  *subplanstate;
860                 int                     sp_eflags;
861
862                 /*
863                  * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
864                  * it is a parameterless subplan (not initplan), we suggest that it be
865                  * prepared to handle REWIND efficiently; otherwise there is no need.
866                  */
867                 sp_eflags = eflags & EXEC_FLAG_EXPLAIN_ONLY;
868                 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
869                         sp_eflags |= EXEC_FLAG_REWIND;
870
871                 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
872
873                 estate->es_subplanstates = lappend(estate->es_subplanstates,
874                                                                                    subplanstate);
875
876                 i++;
877         }
878
879         /*
880          * Initialize the private state information for all the nodes in the query
881          * tree.  This opens files, allocates storage and leaves us ready to start
882          * processing tuples.
883          */
884         planstate = ExecInitNode(plan, estate, eflags);
885
886         /*
887          * Get the tuple descriptor describing the type of tuples to return.
888          */
889         tupType = ExecGetResultType(planstate);
890
891         /*
892          * Initialize the junk filter if needed.  SELECT queries need a filter if
893          * there are any junk attrs in the top-level tlist.
894          */
895         if (operation == CMD_SELECT)
896         {
897                 bool            junk_filter_needed = false;
898                 ListCell   *tlist;
899
900                 foreach(tlist, plan->targetlist)
901                 {
902                         TargetEntry *tle = (TargetEntry *) lfirst(tlist);
903
904                         if (tle->resjunk)
905                         {
906                                 junk_filter_needed = true;
907                                 break;
908                         }
909                 }
910
911                 if (junk_filter_needed)
912                 {
913                         JunkFilter *j;
914
915                         j = ExecInitJunkFilter(planstate->plan->targetlist,
916                                                                    tupType->tdhasoid,
917                                                                    ExecInitExtraTupleSlot(estate));
918                         estate->es_junkFilter = j;
919
920                         /* Want to return the cleaned tuple type */
921                         tupType = j->jf_cleanTupType;
922                 }
923         }
924
925         queryDesc->tupDesc = tupType;
926         queryDesc->planstate = planstate;
927 }
928
929 /*
930  * Check that a proposed result relation is a legal target for the operation
931  *
932  * Generally the parser and/or planner should have noticed any such mistake
933  * already, but let's make sure.
934  *
935  * Note: when changing this function, you probably also need to look at
936  * CheckValidRowMarkRel.
937  */
938 void
939 CheckValidResultRel(Relation resultRel, CmdType operation)
940 {
941         TriggerDesc *trigDesc = resultRel->trigdesc;
942         FdwRoutine *fdwroutine;
943
944         switch (resultRel->rd_rel->relkind)
945         {
946                 case RELKIND_RELATION:
947                         /* OK */
948                         break;
949                 case RELKIND_SEQUENCE:
950                         ereport(ERROR,
951                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
952                                          errmsg("cannot change sequence \"%s\"",
953                                                         RelationGetRelationName(resultRel))));
954                         break;
955                 case RELKIND_TOASTVALUE:
956                         ereport(ERROR,
957                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
958                                          errmsg("cannot change TOAST relation \"%s\"",
959                                                         RelationGetRelationName(resultRel))));
960                         break;
961                 case RELKIND_VIEW:
962                         /*
963                          * Okay only if there's a suitable INSTEAD OF trigger.  Messages
964                          * here should match rewriteHandler.c's rewriteTargetView, except
965                          * that we omit errdetail because we haven't got the information
966                          * handy (and given that we really shouldn't get here anyway,
967                          * it's not worth great exertion to get).
968                          */
969                         switch (operation)
970                         {
971                                 case CMD_INSERT:
972                                         if (!trigDesc || !trigDesc->trig_insert_instead_row)
973                                                 ereport(ERROR,
974                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
975                                                    errmsg("cannot insert into view \"%s\"",
976                                                                   RelationGetRelationName(resultRel)),
977                                                    errhint("To make the view insertable, provide an unconditional ON INSERT DO INSTEAD rule or an INSTEAD OF INSERT trigger.")));
978                                         break;
979                                 case CMD_UPDATE:
980                                         if (!trigDesc || !trigDesc->trig_update_instead_row)
981                                                 ereport(ERROR,
982                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
983                                                    errmsg("cannot update view \"%s\"",
984                                                                   RelationGetRelationName(resultRel)),
985                                                    errhint("To make the view updatable, provide an unconditional ON UPDATE DO INSTEAD rule or an INSTEAD OF UPDATE trigger.")));
986                                         break;
987                                 case CMD_DELETE:
988                                         if (!trigDesc || !trigDesc->trig_delete_instead_row)
989                                                 ereport(ERROR,
990                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
991                                                    errmsg("cannot delete from view \"%s\"",
992                                                                   RelationGetRelationName(resultRel)),
993                                                    errhint("To make the view updatable, provide an unconditional ON DELETE DO INSTEAD rule or an INSTEAD OF DELETE trigger.")));
994                                         break;
995                                 default:
996                                         elog(ERROR, "unrecognized CmdType: %d", (int) operation);
997                                         break;
998                         }
999                         break;
1000                 case RELKIND_MATVIEW:
1001                         ereport(ERROR,
1002                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1003                                          errmsg("cannot change materialized view \"%s\"",
1004                                                         RelationGetRelationName(resultRel))));
1005                         break;
1006                 case RELKIND_FOREIGN_TABLE:
1007                         /* Okay only if the FDW supports it */
1008                         fdwroutine = GetFdwRoutineForRelation(resultRel, false);
1009                         switch (operation)
1010                         {
1011                                 case CMD_INSERT:
1012                                         if (fdwroutine->ExecForeignInsert == NULL)
1013                                                 ereport(ERROR,
1014                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1015                                                                  errmsg("cannot insert into foreign table \"%s\"",
1016                                                                                 RelationGetRelationName(resultRel))));
1017                                         break;
1018                                 case CMD_UPDATE:
1019                                         if (fdwroutine->ExecForeignUpdate == NULL)
1020                                                 ereport(ERROR,
1021                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1022                                                                  errmsg("cannot update foreign table \"%s\"",
1023                                                                                 RelationGetRelationName(resultRel))));
1024                                         break;
1025                                 case CMD_DELETE:
1026                                         if (fdwroutine->ExecForeignDelete == NULL)
1027                                                 ereport(ERROR,
1028                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1029                                                                  errmsg("cannot delete from foreign table \"%s\"",
1030                                                                                 RelationGetRelationName(resultRel))));
1031                                         break;
1032                                 default:
1033                                         elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1034                                         break;
1035                         }
1036                         break;
1037                 default:
1038                         ereport(ERROR,
1039                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1040                                          errmsg("cannot change relation \"%s\"",
1041                                                         RelationGetRelationName(resultRel))));
1042                         break;
1043         }
1044 }
1045
1046 /*
1047  * Check that a proposed rowmark target relation is a legal target
1048  *
1049  * In most cases parser and/or planner should have noticed this already, but
1050  * they don't cover all cases.
1051  */
1052 static void
1053 CheckValidRowMarkRel(Relation rel, RowMarkType markType)
1054 {
1055         switch (rel->rd_rel->relkind)
1056         {
1057                 case RELKIND_RELATION:
1058                         /* OK */
1059                         break;
1060                 case RELKIND_SEQUENCE:
1061                         /* Must disallow this because we don't vacuum sequences */
1062                         ereport(ERROR,
1063                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1064                                          errmsg("cannot lock rows in sequence \"%s\"",
1065                                                         RelationGetRelationName(rel))));
1066                         break;
1067                 case RELKIND_TOASTVALUE:
1068                         /* We could allow this, but there seems no good reason to */
1069                         ereport(ERROR,
1070                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1071                                          errmsg("cannot lock rows in TOAST relation \"%s\"",
1072                                                         RelationGetRelationName(rel))));
1073                         break;
1074                 case RELKIND_VIEW:
1075                         /* Should not get here; planner should have expanded the view */
1076                         ereport(ERROR,
1077                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1078                                          errmsg("cannot lock rows in view \"%s\"",
1079                                                         RelationGetRelationName(rel))));
1080                         break;
1081                 case RELKIND_MATVIEW:
1082                         /* Should not get here */
1083                         ereport(ERROR,
1084                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1085                                          errmsg("cannot lock rows in materialized view \"%s\"",
1086                                                         RelationGetRelationName(rel))));
1087                         break;
1088                 case RELKIND_FOREIGN_TABLE:
1089                         /* Should not get here */
1090                         ereport(ERROR,
1091                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1092                                          errmsg("cannot lock rows in foreign table \"%s\"",
1093                                                         RelationGetRelationName(rel))));
1094                         break;
1095                 default:
1096                         ereport(ERROR,
1097                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1098                                          errmsg("cannot lock rows in relation \"%s\"",
1099                                                         RelationGetRelationName(rel))));
1100                         break;
1101         }
1102 }
1103
1104 /*
1105  * Initialize ResultRelInfo data for one result relation
1106  *
1107  * Caution: before Postgres 9.1, this function included the relkind checking
1108  * that's now in CheckValidResultRel, and it also did ExecOpenIndices if
1109  * appropriate.  Be sure callers cover those needs.
1110  */
1111 void
1112 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1113                                   Relation resultRelationDesc,
1114                                   Index resultRelationIndex,
1115                                   int instrument_options)
1116 {
1117         MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1118         resultRelInfo->type = T_ResultRelInfo;
1119         resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1120         resultRelInfo->ri_RelationDesc = resultRelationDesc;
1121         resultRelInfo->ri_NumIndices = 0;
1122         resultRelInfo->ri_IndexRelationDescs = NULL;
1123         resultRelInfo->ri_IndexRelationInfo = NULL;
1124         /* make a copy so as not to depend on relcache info not changing... */
1125         resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1126         if (resultRelInfo->ri_TrigDesc)
1127         {
1128                 int                     n = resultRelInfo->ri_TrigDesc->numtriggers;
1129
1130                 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1131                         palloc0(n * sizeof(FmgrInfo));
1132                 resultRelInfo->ri_TrigWhenExprs = (List **)
1133                         palloc0(n * sizeof(List *));
1134                 if (instrument_options)
1135                         resultRelInfo->ri_TrigInstrument = InstrAlloc(n, instrument_options);
1136         }
1137         else
1138         {
1139                 resultRelInfo->ri_TrigFunctions = NULL;
1140                 resultRelInfo->ri_TrigWhenExprs = NULL;
1141                 resultRelInfo->ri_TrigInstrument = NULL;
1142         }
1143         if (resultRelationDesc->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1144                 resultRelInfo->ri_FdwRoutine = GetFdwRoutineForRelation(resultRelationDesc, true);
1145         else
1146                 resultRelInfo->ri_FdwRoutine = NULL;
1147         resultRelInfo->ri_FdwState = NULL;
1148         resultRelInfo->ri_ConstraintExprs = NULL;
1149         resultRelInfo->ri_junkFilter = NULL;
1150         resultRelInfo->ri_projectReturning = NULL;
1151 }
1152
1153 /*
1154  *              ExecGetTriggerResultRel
1155  *
1156  * Get a ResultRelInfo for a trigger target relation.  Most of the time,
1157  * triggers are fired on one of the result relations of the query, and so
1158  * we can just return a member of the es_result_relations array.  (Note: in
1159  * self-join situations there might be multiple members with the same OID;
1160  * if so it doesn't matter which one we pick.)  However, it is sometimes
1161  * necessary to fire triggers on other relations; this happens mainly when an
1162  * RI update trigger queues additional triggers on other relations, which will
1163  * be processed in the context of the outer query.      For efficiency's sake,
1164  * we want to have a ResultRelInfo for those triggers too; that can avoid
1165  * repeated re-opening of the relation.  (It also provides a way for EXPLAIN
1166  * ANALYZE to report the runtimes of such triggers.)  So we make additional
1167  * ResultRelInfo's as needed, and save them in es_trig_target_relations.
1168  */
1169 ResultRelInfo *
1170 ExecGetTriggerResultRel(EState *estate, Oid relid)
1171 {
1172         ResultRelInfo *rInfo;
1173         int                     nr;
1174         ListCell   *l;
1175         Relation        rel;
1176         MemoryContext oldcontext;
1177
1178         /* First, search through the query result relations */
1179         rInfo = estate->es_result_relations;
1180         nr = estate->es_num_result_relations;
1181         while (nr > 0)
1182         {
1183                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1184                         return rInfo;
1185                 rInfo++;
1186                 nr--;
1187         }
1188         /* Nope, but maybe we already made an extra ResultRelInfo for it */
1189         foreach(l, estate->es_trig_target_relations)
1190         {
1191                 rInfo = (ResultRelInfo *) lfirst(l);
1192                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1193                         return rInfo;
1194         }
1195         /* Nope, so we need a new one */
1196
1197         /*
1198          * Open the target relation's relcache entry.  We assume that an
1199          * appropriate lock is still held by the backend from whenever the trigger
1200          * event got queued, so we need take no new lock here.  Also, we need not
1201          * recheck the relkind, so no need for CheckValidResultRel.
1202          */
1203         rel = heap_open(relid, NoLock);
1204
1205         /*
1206          * Make the new entry in the right context.
1207          */
1208         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1209         rInfo = makeNode(ResultRelInfo);
1210         InitResultRelInfo(rInfo,
1211                                           rel,
1212                                           0,            /* dummy rangetable index */
1213                                           estate->es_instrument);
1214         estate->es_trig_target_relations =
1215                 lappend(estate->es_trig_target_relations, rInfo);
1216         MemoryContextSwitchTo(oldcontext);
1217
1218         /*
1219          * Currently, we don't need any index information in ResultRelInfos used
1220          * only for triggers, so no need to call ExecOpenIndices.
1221          */
1222
1223         return rInfo;
1224 }
1225
1226 /*
1227  *              ExecContextForcesOids
1228  *
1229  * This is pretty grotty: when doing INSERT, UPDATE, or CREATE TABLE AS,
1230  * we need to ensure that result tuples have space for an OID iff they are
1231  * going to be stored into a relation that has OIDs.  In other contexts
1232  * we are free to choose whether to leave space for OIDs in result tuples
1233  * (we generally don't want to, but we do if a physical-tlist optimization
1234  * is possible).  This routine checks the plan context and returns TRUE if the
1235  * choice is forced, FALSE if the choice is not forced.  In the TRUE case,
1236  * *hasoids is set to the required value.
1237  *
1238  * One reason this is ugly is that all plan nodes in the plan tree will emit
1239  * tuples with space for an OID, though we really only need the topmost node
1240  * to do so.  However, node types like Sort don't project new tuples but just
1241  * return their inputs, and in those cases the requirement propagates down
1242  * to the input node.  Eventually we might make this code smart enough to
1243  * recognize how far down the requirement really goes, but for now we just
1244  * make all plan nodes do the same thing if the top level forces the choice.
1245  *
1246  * We assume that if we are generating tuples for INSERT or UPDATE,
1247  * estate->es_result_relation_info is already set up to describe the target
1248  * relation.  Note that in an UPDATE that spans an inheritance tree, some of
1249  * the target relations may have OIDs and some not.  We have to make the
1250  * decisions on a per-relation basis as we initialize each of the subplans of
1251  * the ModifyTable node, so ModifyTable has to set es_result_relation_info
1252  * while initializing each subplan.
1253  *
1254  * CREATE TABLE AS is even uglier, because we don't have the target relation's
1255  * descriptor available when this code runs; we have to look aside at the
1256  * flags passed to ExecutorStart().
1257  */
1258 bool
1259 ExecContextForcesOids(PlanState *planstate, bool *hasoids)
1260 {
1261         ResultRelInfo *ri = planstate->state->es_result_relation_info;
1262
1263         if (ri != NULL)
1264         {
1265                 Relation        rel = ri->ri_RelationDesc;
1266
1267                 if (rel != NULL)
1268                 {
1269                         *hasoids = rel->rd_rel->relhasoids;
1270                         return true;
1271                 }
1272         }
1273
1274         if (planstate->state->es_top_eflags & EXEC_FLAG_WITH_OIDS)
1275         {
1276                 *hasoids = true;
1277                 return true;
1278         }
1279         if (planstate->state->es_top_eflags & EXEC_FLAG_WITHOUT_OIDS)
1280         {
1281                 *hasoids = false;
1282                 return true;
1283         }
1284
1285         return false;
1286 }
1287
1288 /* ----------------------------------------------------------------
1289  *              ExecPostprocessPlan
1290  *
1291  *              Give plan nodes a final chance to execute before shutdown
1292  * ----------------------------------------------------------------
1293  */
1294 static void
1295 ExecPostprocessPlan(EState *estate)
1296 {
1297         ListCell   *lc;
1298
1299         /*
1300          * Make sure nodes run forward.
1301          */
1302         estate->es_direction = ForwardScanDirection;
1303
1304         /*
1305          * Run any secondary ModifyTable nodes to completion, in case the main
1306          * query did not fetch all rows from them.      (We do this to ensure that
1307          * such nodes have predictable results.)
1308          */
1309         foreach(lc, estate->es_auxmodifytables)
1310         {
1311                 PlanState  *ps = (PlanState *) lfirst(lc);
1312
1313                 for (;;)
1314                 {
1315                         TupleTableSlot *slot;
1316
1317                         /* Reset the per-output-tuple exprcontext each time */
1318                         ResetPerTupleExprContext(estate);
1319
1320                         slot = ExecProcNode(ps);
1321
1322                         if (TupIsNull(slot))
1323                                 break;
1324                 }
1325         }
1326 }
1327
1328 /* ----------------------------------------------------------------
1329  *              ExecEndPlan
1330  *
1331  *              Cleans up the query plan -- closes files and frees up storage
1332  *
1333  * NOTE: we are no longer very worried about freeing storage per se
1334  * in this code; FreeExecutorState should be guaranteed to release all
1335  * memory that needs to be released.  What we are worried about doing
1336  * is closing relations and dropping buffer pins.  Thus, for example,
1337  * tuple tables must be cleared or dropped to ensure pins are released.
1338  * ----------------------------------------------------------------
1339  */
1340 static void
1341 ExecEndPlan(PlanState *planstate, EState *estate)
1342 {
1343         ResultRelInfo *resultRelInfo;
1344         int                     i;
1345         ListCell   *l;
1346
1347         /*
1348          * shut down the node-type-specific query processing
1349          */
1350         ExecEndNode(planstate);
1351
1352         /*
1353          * for subplans too
1354          */
1355         foreach(l, estate->es_subplanstates)
1356         {
1357                 PlanState  *subplanstate = (PlanState *) lfirst(l);
1358
1359                 ExecEndNode(subplanstate);
1360         }
1361
1362         /*
1363          * destroy the executor's tuple table.  Actually we only care about
1364          * releasing buffer pins and tupdesc refcounts; there's no need to pfree
1365          * the TupleTableSlots, since the containing memory context is about to go
1366          * away anyway.
1367          */
1368         ExecResetTupleTable(estate->es_tupleTable, false);
1369
1370         /*
1371          * close the result relation(s) if any, but hold locks until xact commit.
1372          */
1373         resultRelInfo = estate->es_result_relations;
1374         for (i = estate->es_num_result_relations; i > 0; i--)
1375         {
1376                 /* Close indices and then the relation itself */
1377                 ExecCloseIndices(resultRelInfo);
1378                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1379                 resultRelInfo++;
1380         }
1381
1382         /*
1383          * likewise close any trigger target relations
1384          */
1385         foreach(l, estate->es_trig_target_relations)
1386         {
1387                 resultRelInfo = (ResultRelInfo *) lfirst(l);
1388                 /* Close indices and then the relation itself */
1389                 ExecCloseIndices(resultRelInfo);
1390                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1391         }
1392
1393         /*
1394          * close any relations selected FOR [KEY] UPDATE/SHARE, again keeping locks
1395          */
1396         foreach(l, estate->es_rowMarks)
1397         {
1398                 ExecRowMark *erm = (ExecRowMark *) lfirst(l);
1399
1400                 if (erm->relation)
1401                         heap_close(erm->relation, NoLock);
1402         }
1403 }
1404
1405 /* ----------------------------------------------------------------
1406  *              ExecutePlan
1407  *
1408  *              Processes the query plan until we have retrieved 'numberTuples' tuples,
1409  *              moving in the specified direction.
1410  *
1411  *              Runs to completion if numberTuples is 0
1412  *
1413  * Note: the ctid attribute is a 'junk' attribute that is removed before the
1414  * user can see it
1415  * ----------------------------------------------------------------
1416  */
1417 static void
1418 ExecutePlan(EState *estate,
1419                         PlanState *planstate,
1420                         CmdType operation,
1421                         bool sendTuples,
1422                         long numberTuples,
1423                         ScanDirection direction,
1424                         DestReceiver *dest)
1425 {
1426         TupleTableSlot *slot;
1427         long            current_tuple_count;
1428
1429         /*
1430          * initialize local variables
1431          */
1432         current_tuple_count = 0;
1433
1434         /*
1435          * Set the direction.
1436          */
1437         estate->es_direction = direction;
1438
1439         /*
1440          * Loop until we've processed the proper number of tuples from the plan.
1441          */
1442         for (;;)
1443         {
1444                 /* Reset the per-output-tuple exprcontext */
1445                 ResetPerTupleExprContext(estate);
1446
1447                 /*
1448                  * Execute the plan and obtain a tuple
1449                  */
1450                 slot = ExecProcNode(planstate);
1451
1452                 /*
1453                  * if the tuple is null, then we assume there is nothing more to
1454                  * process so we just end the loop...
1455                  */
1456                 if (TupIsNull(slot))
1457                         break;
1458
1459                 /*
1460                  * If we have a junk filter, then project a new tuple with the junk
1461                  * removed.
1462                  *
1463                  * Store this new "clean" tuple in the junkfilter's resultSlot.
1464                  * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1465                  * because that tuple slot has the wrong descriptor.)
1466                  */
1467                 if (estate->es_junkFilter != NULL)
1468                         slot = ExecFilterJunk(estate->es_junkFilter, slot);
1469
1470                 /*
1471                  * If we are supposed to send the tuple somewhere, do so. (In
1472                  * practice, this is probably always the case at this point.)
1473                  */
1474                 if (sendTuples)
1475                         (*dest->receiveSlot) (slot, dest);
1476
1477                 /*
1478                  * Count tuples processed, if this is a SELECT.  (For other operation
1479                  * types, the ModifyTable plan node must count the appropriate
1480                  * events.)
1481                  */
1482                 if (operation == CMD_SELECT)
1483                         (estate->es_processed)++;
1484
1485                 /*
1486                  * check our tuple count.. if we've processed the proper number then
1487                  * quit, else loop again and process more tuples.  Zero numberTuples
1488                  * means no limit.
1489                  */
1490                 current_tuple_count++;
1491                 if (numberTuples && numberTuples == current_tuple_count)
1492                         break;
1493         }
1494 }
1495
1496
1497 /*
1498  * ExecRelCheck --- check that tuple meets constraints for result relation
1499  *
1500  * Returns NULL if OK, else name of failed check constraint
1501  */
1502 static const char *
1503 ExecRelCheck(ResultRelInfo *resultRelInfo,
1504                          TupleTableSlot *slot, EState *estate)
1505 {
1506         Relation        rel = resultRelInfo->ri_RelationDesc;
1507         int                     ncheck = rel->rd_att->constr->num_check;
1508         ConstrCheck *check = rel->rd_att->constr->check;
1509         ExprContext *econtext;
1510         MemoryContext oldContext;
1511         List       *qual;
1512         int                     i;
1513
1514         /*
1515          * If first time through for this result relation, build expression
1516          * nodetrees for rel's constraint expressions.  Keep them in the per-query
1517          * memory context so they'll survive throughout the query.
1518          */
1519         if (resultRelInfo->ri_ConstraintExprs == NULL)
1520         {
1521                 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1522                 resultRelInfo->ri_ConstraintExprs =
1523                         (List **) palloc(ncheck * sizeof(List *));
1524                 for (i = 0; i < ncheck; i++)
1525                 {
1526                         /* ExecQual wants implicit-AND form */
1527                         qual = make_ands_implicit(stringToNode(check[i].ccbin));
1528                         resultRelInfo->ri_ConstraintExprs[i] = (List *)
1529                                 ExecPrepareExpr((Expr *) qual, estate);
1530                 }
1531                 MemoryContextSwitchTo(oldContext);
1532         }
1533
1534         /*
1535          * We will use the EState's per-tuple context for evaluating constraint
1536          * expressions (creating it if it's not already there).
1537          */
1538         econtext = GetPerTupleExprContext(estate);
1539
1540         /* Arrange for econtext's scan tuple to be the tuple under test */
1541         econtext->ecxt_scantuple = slot;
1542
1543         /* And evaluate the constraints */
1544         for (i = 0; i < ncheck; i++)
1545         {
1546                 qual = resultRelInfo->ri_ConstraintExprs[i];
1547
1548                 /*
1549                  * NOTE: SQL specifies that a NULL result from a constraint
1550                  * expression is not to be treated as a failure.  Therefore, tell
1551                  * ExecQual to return TRUE for NULL.
1552                  */
1553                 if (!ExecQual(qual, econtext, true))
1554                         return check[i].ccname;
1555         }
1556
1557         /* NULL result means no error */
1558         return NULL;
1559 }
1560
1561 void
1562 ExecConstraints(ResultRelInfo *resultRelInfo,
1563                                 TupleTableSlot *slot, EState *estate)
1564 {
1565         Relation        rel = resultRelInfo->ri_RelationDesc;
1566         TupleConstr *constr = rel->rd_att->constr;
1567
1568         Assert(constr);
1569
1570         if (constr->has_not_null)
1571         {
1572                 int                     natts = rel->rd_att->natts;
1573                 int                     attrChk;
1574
1575                 for (attrChk = 1; attrChk <= natts; attrChk++)
1576                 {
1577                         if (rel->rd_att->attrs[attrChk - 1]->attnotnull &&
1578                                 slot_attisnull(slot, attrChk))
1579                                 ereport(ERROR,
1580                                                 (errcode(ERRCODE_NOT_NULL_VIOLATION),
1581                                                  errmsg("null value in column \"%s\" violates not-null constraint",
1582                                                   NameStr(rel->rd_att->attrs[attrChk - 1]->attname)),
1583                                                  errdetail("Failing row contains %s.",
1584                                                                    ExecBuildSlotValueDescription(slot, 64)),
1585                                                  errtablecol(rel, attrChk)));
1586                 }
1587         }
1588
1589         if (constr->num_check > 0)
1590         {
1591                 const char *failed;
1592
1593                 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
1594                         ereport(ERROR,
1595                                         (errcode(ERRCODE_CHECK_VIOLATION),
1596                                          errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
1597                                                         RelationGetRelationName(rel), failed),
1598                                          errdetail("Failing row contains %s.",
1599                                                            ExecBuildSlotValueDescription(slot, 64)),
1600                                          errtableconstraint(rel, failed)));
1601         }
1602 }
1603
1604 /*
1605  * ExecBuildSlotValueDescription -- construct a string representing a tuple
1606  *
1607  * This is intentionally very similar to BuildIndexValueDescription, but
1608  * unlike that function, we truncate long field values.  That seems necessary
1609  * here since heap field values could be very long, whereas index entries
1610  * typically aren't so wide.
1611  */
1612 static char *
1613 ExecBuildSlotValueDescription(TupleTableSlot *slot, int maxfieldlen)
1614 {
1615         StringInfoData buf;
1616         TupleDesc       tupdesc = slot->tts_tupleDescriptor;
1617         int                     i;
1618
1619         /* Make sure the tuple is fully deconstructed */
1620         slot_getallattrs(slot);
1621
1622         initStringInfo(&buf);
1623
1624         appendStringInfoChar(&buf, '(');
1625
1626         for (i = 0; i < tupdesc->natts; i++)
1627         {
1628                 char       *val;
1629                 int                     vallen;
1630
1631                 if (slot->tts_isnull[i])
1632                         val = "null";
1633                 else
1634                 {
1635                         Oid                     foutoid;
1636                         bool            typisvarlena;
1637
1638                         getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
1639                                                           &foutoid, &typisvarlena);
1640                         val = OidOutputFunctionCall(foutoid, slot->tts_values[i]);
1641                 }
1642
1643                 if (i > 0)
1644                         appendStringInfoString(&buf, ", ");
1645
1646                 /* truncate if needed */
1647                 vallen = strlen(val);
1648                 if (vallen <= maxfieldlen)
1649                         appendStringInfoString(&buf, val);
1650                 else
1651                 {
1652                         vallen = pg_mbcliplen(val, vallen, maxfieldlen);
1653                         appendBinaryStringInfo(&buf, val, vallen);
1654                         appendStringInfoString(&buf, "...");
1655                 }
1656         }
1657
1658         appendStringInfoChar(&buf, ')');
1659
1660         return buf.data;
1661 }
1662
1663
1664 /*
1665  * ExecFindRowMark -- find the ExecRowMark struct for given rangetable index
1666  */
1667 ExecRowMark *
1668 ExecFindRowMark(EState *estate, Index rti)
1669 {
1670         ListCell   *lc;
1671
1672         foreach(lc, estate->es_rowMarks)
1673         {
1674                 ExecRowMark *erm = (ExecRowMark *) lfirst(lc);
1675
1676                 if (erm->rti == rti)
1677                         return erm;
1678         }
1679         elog(ERROR, "failed to find ExecRowMark for rangetable index %u", rti);
1680         return NULL;                            /* keep compiler quiet */
1681 }
1682
1683 /*
1684  * ExecBuildAuxRowMark -- create an ExecAuxRowMark struct
1685  *
1686  * Inputs are the underlying ExecRowMark struct and the targetlist of the
1687  * input plan node (not planstate node!).  We need the latter to find out
1688  * the column numbers of the resjunk columns.
1689  */
1690 ExecAuxRowMark *
1691 ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
1692 {
1693         ExecAuxRowMark *aerm = (ExecAuxRowMark *) palloc0(sizeof(ExecAuxRowMark));
1694         char            resname[32];
1695
1696         aerm->rowmark = erm;
1697
1698         /* Look up the resjunk columns associated with this rowmark */
1699         if (erm->relation)
1700         {
1701                 Assert(erm->markType != ROW_MARK_COPY);
1702
1703                 /* if child rel, need tableoid */
1704                 if (erm->rti != erm->prti)
1705                 {
1706                         snprintf(resname, sizeof(resname), "tableoid%u", erm->rowmarkId);
1707                         aerm->toidAttNo = ExecFindJunkAttributeInTlist(targetlist,
1708                                                                                                                    resname);
1709                         if (!AttributeNumberIsValid(aerm->toidAttNo))
1710                                 elog(ERROR, "could not find junk %s column", resname);
1711                 }
1712
1713                 /* always need ctid for real relations */
1714                 snprintf(resname, sizeof(resname), "ctid%u", erm->rowmarkId);
1715                 aerm->ctidAttNo = ExecFindJunkAttributeInTlist(targetlist,
1716                                                                                                            resname);
1717                 if (!AttributeNumberIsValid(aerm->ctidAttNo))
1718                         elog(ERROR, "could not find junk %s column", resname);
1719         }
1720         else
1721         {
1722                 Assert(erm->markType == ROW_MARK_COPY);
1723
1724                 snprintf(resname, sizeof(resname), "wholerow%u", erm->rowmarkId);
1725                 aerm->wholeAttNo = ExecFindJunkAttributeInTlist(targetlist,
1726                                                                                                                 resname);
1727                 if (!AttributeNumberIsValid(aerm->wholeAttNo))
1728                         elog(ERROR, "could not find junk %s column", resname);
1729         }
1730
1731         return aerm;
1732 }
1733
1734
1735 /*
1736  * EvalPlanQual logic --- recheck modified tuple(s) to see if we want to
1737  * process the updated version under READ COMMITTED rules.
1738  *
1739  * See backend/executor/README for some info about how this works.
1740  */
1741
1742
1743 /*
1744  * Check a modified tuple to see if we want to process its updated version
1745  * under READ COMMITTED rules.
1746  *
1747  *      estate - outer executor state data
1748  *      epqstate - state for EvalPlanQual rechecking
1749  *      relation - table containing tuple
1750  *      rti - rangetable index of table containing tuple
1751  *      lockmode - requested tuple lock mode
1752  *      *tid - t_ctid from the outdated tuple (ie, next updated version)
1753  *      priorXmax - t_xmax from the outdated tuple
1754  *
1755  * *tid is also an output parameter: it's modified to hold the TID of the
1756  * latest version of the tuple (note this may be changed even on failure)
1757  *
1758  * Returns a slot containing the new candidate update/delete tuple, or
1759  * NULL if we determine we shouldn't process the row.
1760  *
1761  * Note: properly, lockmode should be declared as enum LockTupleMode,
1762  * but we use "int" to avoid having to include heapam.h in executor.h.
1763  */
1764 TupleTableSlot *
1765 EvalPlanQual(EState *estate, EPQState *epqstate,
1766                          Relation relation, Index rti, int lockmode,
1767                          ItemPointer tid, TransactionId priorXmax)
1768 {
1769         TupleTableSlot *slot;
1770         HeapTuple       copyTuple;
1771
1772         Assert(rti > 0);
1773
1774         /*
1775          * Get and lock the updated version of the row; if fail, return NULL.
1776          */
1777         copyTuple = EvalPlanQualFetch(estate, relation, lockmode,
1778                                                                   tid, priorXmax);
1779
1780         if (copyTuple == NULL)
1781                 return NULL;
1782
1783         /*
1784          * For UPDATE/DELETE we have to return tid of actual row we're executing
1785          * PQ for.
1786          */
1787         *tid = copyTuple->t_self;
1788
1789         /*
1790          * Need to run a recheck subquery.      Initialize or reinitialize EPQ state.
1791          */
1792         EvalPlanQualBegin(epqstate, estate);
1793
1794         /*
1795          * Free old test tuple, if any, and store new tuple where relation's scan
1796          * node will see it
1797          */
1798         EvalPlanQualSetTuple(epqstate, rti, copyTuple);
1799
1800         /*
1801          * Fetch any non-locked source rows
1802          */
1803         EvalPlanQualFetchRowMarks(epqstate);
1804
1805         /*
1806          * Run the EPQ query.  We assume it will return at most one tuple.
1807          */
1808         slot = EvalPlanQualNext(epqstate);
1809
1810         /*
1811          * If we got a tuple, force the slot to materialize the tuple so that it
1812          * is not dependent on any local state in the EPQ query (in particular,
1813          * it's highly likely that the slot contains references to any pass-by-ref
1814          * datums that may be present in copyTuple).  As with the next step, this
1815          * is to guard against early re-use of the EPQ query.
1816          */
1817         if (!TupIsNull(slot))
1818                 (void) ExecMaterializeSlot(slot);
1819
1820         /*
1821          * Clear out the test tuple.  This is needed in case the EPQ query is
1822          * re-used to test a tuple for a different relation.  (Not clear that can
1823          * really happen, but let's be safe.)
1824          */
1825         EvalPlanQualSetTuple(epqstate, rti, NULL);
1826
1827         return slot;
1828 }
1829
1830 /*
1831  * Fetch a copy of the newest version of an outdated tuple
1832  *
1833  *      estate - executor state data
1834  *      relation - table containing tuple
1835  *      lockmode - requested tuple lock mode
1836  *      *tid - t_ctid from the outdated tuple (ie, next updated version)
1837  *      priorXmax - t_xmax from the outdated tuple
1838  *
1839  * Returns a palloc'd copy of the newest tuple version, or NULL if we find
1840  * that there is no newest version (ie, the row was deleted not updated).
1841  * If successful, we have locked the newest tuple version, so caller does not
1842  * need to worry about it changing anymore.
1843  *
1844  * Note: properly, lockmode should be declared as enum LockTupleMode,
1845  * but we use "int" to avoid having to include heapam.h in executor.h.
1846  */
1847 HeapTuple
1848 EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
1849                                   ItemPointer tid, TransactionId priorXmax)
1850 {
1851         HeapTuple       copyTuple = NULL;
1852         HeapTupleData tuple;
1853         SnapshotData SnapshotDirty;
1854
1855         /*
1856          * fetch target tuple
1857          *
1858          * Loop here to deal with updated or busy tuples
1859          */
1860         InitDirtySnapshot(SnapshotDirty);
1861         tuple.t_self = *tid;
1862         for (;;)
1863         {
1864                 Buffer          buffer;
1865
1866                 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
1867                 {
1868                         HTSU_Result test;
1869                         HeapUpdateFailureData hufd;
1870
1871                         /*
1872                          * If xmin isn't what we're expecting, the slot must have been
1873                          * recycled and reused for an unrelated tuple.  This implies that
1874                          * the latest version of the row was deleted, so we need do
1875                          * nothing.  (Should be safe to examine xmin without getting
1876                          * buffer's content lock, since xmin never changes in an existing
1877                          * tuple.)
1878                          */
1879                         if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
1880                                                                          priorXmax))
1881                         {
1882                                 ReleaseBuffer(buffer);
1883                                 return NULL;
1884                         }
1885
1886                         /* otherwise xmin should not be dirty... */
1887                         if (TransactionIdIsValid(SnapshotDirty.xmin))
1888                                 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
1889
1890                         /*
1891                          * If tuple is being updated by other transaction then we have to
1892                          * wait for its commit/abort.
1893                          */
1894                         if (TransactionIdIsValid(SnapshotDirty.xmax))
1895                         {
1896                                 ReleaseBuffer(buffer);
1897                                 XactLockTableWait(SnapshotDirty.xmax);
1898                                 continue;               /* loop back to repeat heap_fetch */
1899                         }
1900
1901                         /*
1902                          * If tuple was inserted by our own transaction, we have to check
1903                          * cmin against es_output_cid: cmin >= current CID means our
1904                          * command cannot see the tuple, so we should ignore it.
1905                          * Otherwise heap_lock_tuple() will throw an error, and so would
1906                          * any later attempt to update or delete the tuple.  (We need not
1907                          * check cmax because HeapTupleSatisfiesDirty will consider a
1908                          * tuple deleted by our transaction dead, regardless of cmax.)
1909                          * Wee just checked that priorXmax == xmin, so we can test that
1910                          * variable instead of doing HeapTupleHeaderGetXmin again.
1911                          */
1912                         if (TransactionIdIsCurrentTransactionId(priorXmax) &&
1913                                 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
1914                         {
1915                                 ReleaseBuffer(buffer);
1916                                 return NULL;
1917                         }
1918
1919                         /*
1920                          * This is a live tuple, so now try to lock it.
1921                          */
1922                         test = heap_lock_tuple(relation, &tuple,
1923                                                                    estate->es_output_cid,
1924                                                                    lockmode, false /* wait */,
1925                                                                    false, &buffer, &hufd);
1926                         /* We now have two pins on the buffer, get rid of one */
1927                         ReleaseBuffer(buffer);
1928
1929                         switch (test)
1930                         {
1931                                 case HeapTupleSelfUpdated:
1932                                         /*
1933                                          * The target tuple was already updated or deleted by the
1934                                          * current command, or by a later command in the current
1935                                          * transaction.  We *must* ignore the tuple in the former
1936                                          * case, so as to avoid the "Halloween problem" of
1937                                          * repeated update attempts.  In the latter case it might
1938                                          * be sensible to fetch the updated tuple instead, but
1939                                          * doing so would require changing heap_lock_tuple as well
1940                                          * as heap_update and heap_delete to not complain about
1941                                          * updating "invisible" tuples, which seems pretty scary.
1942                                          * So for now, treat the tuple as deleted and do not
1943                                          * process.
1944                                          */
1945                                         ReleaseBuffer(buffer);
1946                                         return NULL;
1947
1948                                 case HeapTupleMayBeUpdated:
1949                                         /* successfully locked */
1950                                         break;
1951
1952                                 case HeapTupleUpdated:
1953                                         ReleaseBuffer(buffer);
1954                                         if (IsolationUsesXactSnapshot())
1955                                                 ereport(ERROR,
1956                                                                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1957                                                                  errmsg("could not serialize access due to concurrent update")));
1958                                         if (!ItemPointerEquals(&hufd.ctid, &tuple.t_self))
1959                                         {
1960                                                 /* it was updated, so look at the updated version */
1961                                                 tuple.t_self = hufd.ctid;
1962                                                 /* updated row should have xmin matching this xmax */
1963                                                 priorXmax = hufd.xmax;
1964                                                 continue;
1965                                         }
1966                                         /* tuple was deleted, so give up */
1967                                         return NULL;
1968
1969                                 default:
1970                                         ReleaseBuffer(buffer);
1971                                         elog(ERROR, "unrecognized heap_lock_tuple status: %u",
1972                                                  test);
1973                                         return NULL;    /* keep compiler quiet */
1974                         }
1975
1976                         /*
1977                          * We got tuple - now copy it for use by recheck query.
1978                          */
1979                         copyTuple = heap_copytuple(&tuple);
1980                         ReleaseBuffer(buffer);
1981                         break;
1982                 }
1983
1984                 /*
1985                  * If the referenced slot was actually empty, the latest version of
1986                  * the row must have been deleted, so we need do nothing.
1987                  */
1988                 if (tuple.t_data == NULL)
1989                 {
1990                         ReleaseBuffer(buffer);
1991                         return NULL;
1992                 }
1993
1994                 /*
1995                  * As above, if xmin isn't what we're expecting, do nothing.
1996                  */
1997                 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
1998                                                                  priorXmax))
1999                 {
2000                         ReleaseBuffer(buffer);
2001                         return NULL;
2002                 }
2003
2004                 /*
2005                  * If we get here, the tuple was found but failed SnapshotDirty.
2006                  * Assuming the xmin is either a committed xact or our own xact (as it
2007                  * certainly should be if we're trying to modify the tuple), this must
2008                  * mean that the row was updated or deleted by either a committed xact
2009                  * or our own xact.  If it was deleted, we can ignore it; if it was
2010                  * updated then chain up to the next version and repeat the whole
2011                  * process.
2012                  *
2013                  * As above, it should be safe to examine xmax and t_ctid without the
2014                  * buffer content lock, because they can't be changing.
2015                  */
2016                 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2017                 {
2018                         /* deleted, so forget about it */
2019                         ReleaseBuffer(buffer);
2020                         return NULL;
2021                 }
2022
2023                 /* updated, so look at the updated row */
2024                 tuple.t_self = tuple.t_data->t_ctid;
2025                 /* updated row should have xmin matching this xmax */
2026                 priorXmax = HeapTupleHeaderGetUpdateXid(tuple.t_data);
2027                 ReleaseBuffer(buffer);
2028                 /* loop back to fetch next in chain */
2029         }
2030
2031         /*
2032          * Return the copied tuple
2033          */
2034         return copyTuple;
2035 }
2036
2037 /*
2038  * EvalPlanQualInit -- initialize during creation of a plan state node
2039  * that might need to invoke EPQ processing.
2040  *
2041  * Note: subplan/auxrowmarks can be NULL/NIL if they will be set later
2042  * with EvalPlanQualSetPlan.
2043  */
2044 void
2045 EvalPlanQualInit(EPQState *epqstate, EState *estate,
2046                                  Plan *subplan, List *auxrowmarks, int epqParam)
2047 {
2048         /* Mark the EPQ state inactive */
2049         epqstate->estate = NULL;
2050         epqstate->planstate = NULL;
2051         epqstate->origslot = NULL;
2052         /* ... and remember data that EvalPlanQualBegin will need */
2053         epqstate->plan = subplan;
2054         epqstate->arowMarks = auxrowmarks;
2055         epqstate->epqParam = epqParam;
2056 }
2057
2058 /*
2059  * EvalPlanQualSetPlan -- set or change subplan of an EPQState.
2060  *
2061  * We need this so that ModifyTuple can deal with multiple subplans.
2062  */
2063 void
2064 EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
2065 {
2066         /* If we have a live EPQ query, shut it down */
2067         EvalPlanQualEnd(epqstate);
2068         /* And set/change the plan pointer */
2069         epqstate->plan = subplan;
2070         /* The rowmarks depend on the plan, too */
2071         epqstate->arowMarks = auxrowmarks;
2072 }
2073
2074 /*
2075  * Install one test tuple into EPQ state, or clear test tuple if tuple == NULL
2076  *
2077  * NB: passed tuple must be palloc'd; it may get freed later
2078  */
2079 void
2080 EvalPlanQualSetTuple(EPQState *epqstate, Index rti, HeapTuple tuple)
2081 {
2082         EState     *estate = epqstate->estate;
2083
2084         Assert(rti > 0);
2085
2086         /*
2087          * free old test tuple, if any, and store new tuple where relation's scan
2088          * node will see it
2089          */
2090         if (estate->es_epqTuple[rti - 1] != NULL)
2091                 heap_freetuple(estate->es_epqTuple[rti - 1]);
2092         estate->es_epqTuple[rti - 1] = tuple;
2093         estate->es_epqTupleSet[rti - 1] = true;
2094 }
2095
2096 /*
2097  * Fetch back the current test tuple (if any) for the specified RTI
2098  */
2099 HeapTuple
2100 EvalPlanQualGetTuple(EPQState *epqstate, Index rti)
2101 {
2102         EState     *estate = epqstate->estate;
2103
2104         Assert(rti > 0);
2105
2106         return estate->es_epqTuple[rti - 1];
2107 }
2108
2109 /*
2110  * Fetch the current row values for any non-locked relations that need
2111  * to be scanned by an EvalPlanQual operation.  origslot must have been set
2112  * to contain the current result row (top-level row) that we need to recheck.
2113  */
2114 void
2115 EvalPlanQualFetchRowMarks(EPQState *epqstate)
2116 {
2117         ListCell   *l;
2118
2119         Assert(epqstate->origslot != NULL);
2120
2121         foreach(l, epqstate->arowMarks)
2122         {
2123                 ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(l);
2124                 ExecRowMark *erm = aerm->rowmark;
2125                 Datum           datum;
2126                 bool            isNull;
2127                 HeapTupleData tuple;
2128
2129                 if (RowMarkRequiresRowShareLock(erm->markType))
2130                         elog(ERROR, "EvalPlanQual doesn't support locking rowmarks");
2131
2132                 /* clear any leftover test tuple for this rel */
2133                 EvalPlanQualSetTuple(epqstate, erm->rti, NULL);
2134
2135                 if (erm->relation)
2136                 {
2137                         Buffer          buffer;
2138
2139                         Assert(erm->markType == ROW_MARK_REFERENCE);
2140
2141                         /* if child rel, must check whether it produced this row */
2142                         if (erm->rti != erm->prti)
2143                         {
2144                                 Oid                     tableoid;
2145
2146                                 datum = ExecGetJunkAttribute(epqstate->origslot,
2147                                                                                          aerm->toidAttNo,
2148                                                                                          &isNull);
2149                                 /* non-locked rels could be on the inside of outer joins */
2150                                 if (isNull)
2151                                         continue;
2152                                 tableoid = DatumGetObjectId(datum);
2153
2154                                 if (tableoid != RelationGetRelid(erm->relation))
2155                                 {
2156                                         /* this child is inactive right now */
2157                                         continue;
2158                                 }
2159                         }
2160
2161                         /* fetch the tuple's ctid */
2162                         datum = ExecGetJunkAttribute(epqstate->origslot,
2163                                                                                  aerm->ctidAttNo,
2164                                                                                  &isNull);
2165                         /* non-locked rels could be on the inside of outer joins */
2166                         if (isNull)
2167                                 continue;
2168                         tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
2169
2170                         /* okay, fetch the tuple */
2171                         if (!heap_fetch(erm->relation, SnapshotAny, &tuple, &buffer,
2172                                                         false, NULL))
2173                                 elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2174
2175                         /* successful, copy and store tuple */
2176                         EvalPlanQualSetTuple(epqstate, erm->rti,
2177                                                                  heap_copytuple(&tuple));
2178                         ReleaseBuffer(buffer);
2179                 }
2180                 else
2181                 {
2182                         HeapTupleHeader td;
2183
2184                         Assert(erm->markType == ROW_MARK_COPY);
2185
2186                         /* fetch the whole-row Var for the relation */
2187                         datum = ExecGetJunkAttribute(epqstate->origslot,
2188                                                                                  aerm->wholeAttNo,
2189                                                                                  &isNull);
2190                         /* non-locked rels could be on the inside of outer joins */
2191                         if (isNull)
2192                                 continue;
2193                         td = DatumGetHeapTupleHeader(datum);
2194
2195                         /* build a temporary HeapTuple control structure */
2196                         tuple.t_len = HeapTupleHeaderGetDatumLength(td);
2197                         ItemPointerSetInvalid(&(tuple.t_self));
2198                         tuple.t_tableOid = InvalidOid;
2199                         tuple.t_data = td;
2200
2201                         /* copy and store tuple */
2202                         EvalPlanQualSetTuple(epqstate, erm->rti,
2203                                                                  heap_copytuple(&tuple));
2204                 }
2205         }
2206 }
2207
2208 /*
2209  * Fetch the next row (if any) from EvalPlanQual testing
2210  *
2211  * (In practice, there should never be more than one row...)
2212  */
2213 TupleTableSlot *
2214 EvalPlanQualNext(EPQState *epqstate)
2215 {
2216         MemoryContext oldcontext;
2217         TupleTableSlot *slot;
2218
2219         oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
2220         slot = ExecProcNode(epqstate->planstate);
2221         MemoryContextSwitchTo(oldcontext);
2222
2223         return slot;
2224 }
2225
2226 /*
2227  * Initialize or reset an EvalPlanQual state tree
2228  */
2229 void
2230 EvalPlanQualBegin(EPQState *epqstate, EState *parentestate)
2231 {
2232         EState     *estate = epqstate->estate;
2233
2234         if (estate == NULL)
2235         {
2236                 /* First time through, so create a child EState */
2237                 EvalPlanQualStart(epqstate, parentestate, epqstate->plan);
2238         }
2239         else
2240         {
2241                 /*
2242                  * We already have a suitable child EPQ tree, so just reset it.
2243                  */
2244                 int                     rtsize = list_length(parentestate->es_range_table);
2245                 PlanState  *planstate = epqstate->planstate;
2246
2247                 MemSet(estate->es_epqScanDone, 0, rtsize * sizeof(bool));
2248
2249                 /* Recopy current values of parent parameters */
2250                 if (parentestate->es_plannedstmt->nParamExec > 0)
2251                 {
2252                         int                     i = parentestate->es_plannedstmt->nParamExec;
2253
2254                         while (--i >= 0)
2255                         {
2256                                 /* copy value if any, but not execPlan link */
2257                                 estate->es_param_exec_vals[i].value =
2258                                         parentestate->es_param_exec_vals[i].value;
2259                                 estate->es_param_exec_vals[i].isnull =
2260                                         parentestate->es_param_exec_vals[i].isnull;
2261                         }
2262                 }
2263
2264                 /*
2265                  * Mark child plan tree as needing rescan at all scan nodes.  The
2266                  * first ExecProcNode will take care of actually doing the rescan.
2267                  */
2268                 planstate->chgParam = bms_add_member(planstate->chgParam,
2269                                                                                          epqstate->epqParam);
2270         }
2271 }
2272
2273 /*
2274  * Start execution of an EvalPlanQual plan tree.
2275  *
2276  * This is a cut-down version of ExecutorStart(): we copy some state from
2277  * the top-level estate rather than initializing it fresh.
2278  */
2279 static void
2280 EvalPlanQualStart(EPQState *epqstate, EState *parentestate, Plan *planTree)
2281 {
2282         EState     *estate;
2283         int                     rtsize;
2284         MemoryContext oldcontext;
2285         ListCell   *l;
2286
2287         rtsize = list_length(parentestate->es_range_table);
2288
2289         epqstate->estate = estate = CreateExecutorState();
2290
2291         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2292
2293         /*
2294          * Child EPQ EStates share the parent's copy of unchanging state such as
2295          * the snapshot, rangetable, result-rel info, and external Param info.
2296          * They need their own copies of local state, including a tuple table,
2297          * es_param_exec_vals, etc.
2298          */
2299         estate->es_direction = ForwardScanDirection;
2300         estate->es_snapshot = parentestate->es_snapshot;
2301         estate->es_crosscheck_snapshot = parentestate->es_crosscheck_snapshot;
2302         estate->es_range_table = parentestate->es_range_table;
2303         estate->es_plannedstmt = parentestate->es_plannedstmt;
2304         estate->es_junkFilter = parentestate->es_junkFilter;
2305         estate->es_output_cid = parentestate->es_output_cid;
2306         estate->es_result_relations = parentestate->es_result_relations;
2307         estate->es_num_result_relations = parentestate->es_num_result_relations;
2308         estate->es_result_relation_info = parentestate->es_result_relation_info;
2309         /* es_trig_target_relations must NOT be copied */
2310         estate->es_rowMarks = parentestate->es_rowMarks;
2311         estate->es_top_eflags = parentestate->es_top_eflags;
2312         estate->es_instrument = parentestate->es_instrument;
2313         /* es_auxmodifytables must NOT be copied */
2314
2315         /*
2316          * The external param list is simply shared from parent.  The internal
2317          * param workspace has to be local state, but we copy the initial values
2318          * from the parent, so as to have access to any param values that were
2319          * already set from other parts of the parent's plan tree.
2320          */
2321         estate->es_param_list_info = parentestate->es_param_list_info;
2322         if (parentestate->es_plannedstmt->nParamExec > 0)
2323         {
2324                 int                     i = parentestate->es_plannedstmt->nParamExec;
2325
2326                 estate->es_param_exec_vals = (ParamExecData *)
2327                         palloc0(i * sizeof(ParamExecData));
2328                 while (--i >= 0)
2329                 {
2330                         /* copy value if any, but not execPlan link */
2331                         estate->es_param_exec_vals[i].value =
2332                                 parentestate->es_param_exec_vals[i].value;
2333                         estate->es_param_exec_vals[i].isnull =
2334                                 parentestate->es_param_exec_vals[i].isnull;
2335                 }
2336         }
2337
2338         /*
2339          * Each EState must have its own es_epqScanDone state, but if we have
2340          * nested EPQ checks they should share es_epqTuple arrays.      This allows
2341          * sub-rechecks to inherit the values being examined by an outer recheck.
2342          */
2343         estate->es_epqScanDone = (bool *) palloc0(rtsize * sizeof(bool));
2344         if (parentestate->es_epqTuple != NULL)
2345         {
2346                 estate->es_epqTuple = parentestate->es_epqTuple;
2347                 estate->es_epqTupleSet = parentestate->es_epqTupleSet;
2348         }
2349         else
2350         {
2351                 estate->es_epqTuple = (HeapTuple *)
2352                         palloc0(rtsize * sizeof(HeapTuple));
2353                 estate->es_epqTupleSet = (bool *)
2354                         palloc0(rtsize * sizeof(bool));
2355         }
2356
2357         /*
2358          * Each estate also has its own tuple table.
2359          */
2360         estate->es_tupleTable = NIL;
2361
2362         /*
2363          * Initialize private state information for each SubPlan.  We must do this
2364          * before running ExecInitNode on the main query tree, since
2365          * ExecInitSubPlan expects to be able to find these entries. Some of the
2366          * SubPlans might not be used in the part of the plan tree we intend to
2367          * run, but since it's not easy to tell which, we just initialize them
2368          * all.
2369          */
2370         Assert(estate->es_subplanstates == NIL);
2371         foreach(l, parentestate->es_plannedstmt->subplans)
2372         {
2373                 Plan       *subplan = (Plan *) lfirst(l);
2374                 PlanState  *subplanstate;
2375
2376                 subplanstate = ExecInitNode(subplan, estate, 0);
2377                 estate->es_subplanstates = lappend(estate->es_subplanstates,
2378                                                                                    subplanstate);
2379         }
2380
2381         /*
2382          * Initialize the private state information for all the nodes in the part
2383          * of the plan tree we need to run.  This opens files, allocates storage
2384          * and leaves us ready to start processing tuples.
2385          */
2386         epqstate->planstate = ExecInitNode(planTree, estate, 0);
2387
2388         MemoryContextSwitchTo(oldcontext);
2389 }
2390
2391 /*
2392  * EvalPlanQualEnd -- shut down at termination of parent plan state node,
2393  * or if we are done with the current EPQ child.
2394  *
2395  * This is a cut-down version of ExecutorEnd(); basically we want to do most
2396  * of the normal cleanup, but *not* close result relations (which we are
2397  * just sharing from the outer query).  We do, however, have to close any
2398  * trigger target relations that got opened, since those are not shared.
2399  * (There probably shouldn't be any of the latter, but just in case...)
2400  */
2401 void
2402 EvalPlanQualEnd(EPQState *epqstate)
2403 {
2404         EState     *estate = epqstate->estate;
2405         MemoryContext oldcontext;
2406         ListCell   *l;
2407
2408         if (estate == NULL)
2409                 return;                                 /* idle, so nothing to do */
2410
2411         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2412
2413         ExecEndNode(epqstate->planstate);
2414
2415         foreach(l, estate->es_subplanstates)
2416         {
2417                 PlanState  *subplanstate = (PlanState *) lfirst(l);
2418
2419                 ExecEndNode(subplanstate);
2420         }
2421
2422         /* throw away the per-estate tuple table */
2423         ExecResetTupleTable(estate->es_tupleTable, false);
2424
2425         /* close any trigger target relations attached to this EState */
2426         foreach(l, estate->es_trig_target_relations)
2427         {
2428                 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
2429
2430                 /* Close indices and then the relation itself */
2431                 ExecCloseIndices(resultRelInfo);
2432                 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2433         }
2434
2435         MemoryContextSwitchTo(oldcontext);
2436
2437         FreeExecutorState(estate);
2438
2439         /* Mark EPQState idle */
2440         epqstate->estate = NULL;
2441         epqstate->planstate = NULL;
2442         epqstate->origslot = NULL;
2443 }