]> granicus.if.org Git - postgresql/blob - src/backend/executor/execMain.c
Initial pgindent run for v12.
[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-2019, 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/heapam.h"
41 #include "access/htup_details.h"
42 #include "access/sysattr.h"
43 #include "access/tableam.h"
44 #include "access/transam.h"
45 #include "access/xact.h"
46 #include "catalog/namespace.h"
47 #include "catalog/pg_publication.h"
48 #include "commands/matview.h"
49 #include "commands/trigger.h"
50 #include "executor/execdebug.h"
51 #include "executor/nodeSubplan.h"
52 #include "foreign/fdwapi.h"
53 #include "jit/jit.h"
54 #include "mb/pg_wchar.h"
55 #include "miscadmin.h"
56 #include "parser/parsetree.h"
57 #include "storage/bufmgr.h"
58 #include "storage/lmgr.h"
59 #include "tcop/utility.h"
60 #include "utils/acl.h"
61 #include "utils/lsyscache.h"
62 #include "utils/memutils.h"
63 #include "utils/partcache.h"
64 #include "utils/rls.h"
65 #include "utils/ruleutils.h"
66 #include "utils/snapmgr.h"
67
68
69 /* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
70 ExecutorStart_hook_type ExecutorStart_hook = NULL;
71 ExecutorRun_hook_type ExecutorRun_hook = NULL;
72 ExecutorFinish_hook_type ExecutorFinish_hook = NULL;
73 ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
74
75 /* Hook for plugin to get control in ExecCheckRTPerms() */
76 ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;
77
78 /* decls for local routines only used within this module */
79 static void InitPlan(QueryDesc *queryDesc, int eflags);
80 static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
81 static void ExecPostprocessPlan(EState *estate);
82 static void ExecEndPlan(PlanState *planstate, EState *estate);
83 static void ExecutePlan(EState *estate, PlanState *planstate,
84                         bool use_parallel_mode,
85                         CmdType operation,
86                         bool sendTuples,
87                         uint64 numberTuples,
88                         ScanDirection direction,
89                         DestReceiver *dest,
90                         bool execute_once);
91 static bool ExecCheckRTEPerms(RangeTblEntry *rte);
92 static bool ExecCheckRTEPermsModified(Oid relOid, Oid userid,
93                                                   Bitmapset *modifiedCols,
94                                                   AclMode requiredPerms);
95 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
96 static char *ExecBuildSlotValueDescription(Oid reloid,
97                                                           TupleTableSlot *slot,
98                                                           TupleDesc tupdesc,
99                                                           Bitmapset *modifiedCols,
100                                                           int maxfieldlen);
101 static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate,
102                                   Plan *planTree);
103
104 /*
105  * Note that GetAllUpdatedColumns() also exists in commands/trigger.c.  There does
106  * not appear to be any good header to put it into, given the structures that
107  * it uses, so we let them be duplicated.  Be sure to update both if one needs
108  * to be changed, however.
109  */
110 #define GetInsertedColumns(relinfo, estate) \
111         (exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->insertedCols)
112 #define GetUpdatedColumns(relinfo, estate) \
113         (exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->updatedCols)
114 #define GetAllUpdatedColumns(relinfo, estate) \
115         (bms_union(exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->updatedCols, \
116                            exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->extraUpdatedCols))
117
118 /* end of local decls */
119
120
121 /* ----------------------------------------------------------------
122  *              ExecutorStart
123  *
124  *              This routine must be called at the beginning of any execution of any
125  *              query plan
126  *
127  * Takes a QueryDesc previously created by CreateQueryDesc (which is separate
128  * only because some places use QueryDescs for utility commands).  The tupDesc
129  * field of the QueryDesc is filled in to describe the tuples that will be
130  * returned, and the internal fields (estate and planstate) are set up.
131  *
132  * eflags contains flag bits as described in executor.h.
133  *
134  * NB: the CurrentMemoryContext when this is called will become the parent
135  * of the per-query context used for this Executor invocation.
136  *
137  * We provide a function hook variable that lets loadable plugins
138  * get control when ExecutorStart is called.  Such a plugin would
139  * normally call standard_ExecutorStart().
140  *
141  * ----------------------------------------------------------------
142  */
143 void
144 ExecutorStart(QueryDesc *queryDesc, int eflags)
145 {
146         if (ExecutorStart_hook)
147                 (*ExecutorStart_hook) (queryDesc, eflags);
148         else
149                 standard_ExecutorStart(queryDesc, eflags);
150 }
151
152 void
153 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
154 {
155         EState     *estate;
156         MemoryContext oldcontext;
157
158         /* sanity checks: queryDesc must not be started already */
159         Assert(queryDesc != NULL);
160         Assert(queryDesc->estate == NULL);
161
162         /*
163          * If the transaction is read-only, we need to check if any writes are
164          * planned to non-temporary tables.  EXPLAIN is considered read-only.
165          *
166          * Don't allow writes in parallel mode.  Supporting UPDATE and DELETE
167          * would require (a) storing the combocid hash in shared memory, rather
168          * than synchronizing it just once at the start of parallelism, and (b) an
169          * alternative to heap_update()'s reliance on xmax for mutual exclusion.
170          * INSERT may have no such troubles, but we forbid it to simplify the
171          * checks.
172          *
173          * We have lower-level defenses in CommandCounterIncrement and elsewhere
174          * against performing unsafe operations in parallel mode, but this gives a
175          * more user-friendly error message.
176          */
177         if ((XactReadOnly || IsInParallelMode()) &&
178                 !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
179                 ExecCheckXactReadOnly(queryDesc->plannedstmt);
180
181         /*
182          * Build EState, switch into per-query memory context for startup.
183          */
184         estate = CreateExecutorState();
185         queryDesc->estate = estate;
186
187         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
188
189         /*
190          * Fill in external parameters, if any, from queryDesc; and allocate
191          * workspace for internal parameters
192          */
193         estate->es_param_list_info = queryDesc->params;
194
195         if (queryDesc->plannedstmt->paramExecTypes != NIL)
196         {
197                 int                     nParamExec;
198
199                 nParamExec = list_length(queryDesc->plannedstmt->paramExecTypes);
200                 estate->es_param_exec_vals = (ParamExecData *)
201                         palloc0(nParamExec * sizeof(ParamExecData));
202         }
203
204         estate->es_sourceText = queryDesc->sourceText;
205
206         /*
207          * Fill in the query environment, if any, from queryDesc.
208          */
209         estate->es_queryEnv = queryDesc->queryEnv;
210
211         /*
212          * If non-read-only query, set the command ID to mark output tuples with
213          */
214         switch (queryDesc->operation)
215         {
216                 case CMD_SELECT:
217
218                         /*
219                          * SELECT FOR [KEY] UPDATE/SHARE and modifying CTEs need to mark
220                          * tuples
221                          */
222                         if (queryDesc->plannedstmt->rowMarks != NIL ||
223                                 queryDesc->plannedstmt->hasModifyingCTE)
224                                 estate->es_output_cid = GetCurrentCommandId(true);
225
226                         /*
227                          * A SELECT without modifying CTEs can't possibly queue triggers,
228                          * so force skip-triggers mode. This is just a marginal efficiency
229                          * hack, since AfterTriggerBeginQuery/AfterTriggerEndQuery aren't
230                          * all that expensive, but we might as well do it.
231                          */
232                         if (!queryDesc->plannedstmt->hasModifyingCTE)
233                                 eflags |= EXEC_FLAG_SKIP_TRIGGERS;
234                         break;
235
236                 case CMD_INSERT:
237                 case CMD_DELETE:
238                 case CMD_UPDATE:
239                         estate->es_output_cid = GetCurrentCommandId(true);
240                         break;
241
242                 default:
243                         elog(ERROR, "unrecognized operation code: %d",
244                                  (int) queryDesc->operation);
245                         break;
246         }
247
248         /*
249          * Copy other important information into the EState
250          */
251         estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
252         estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
253         estate->es_top_eflags = eflags;
254         estate->es_instrument = queryDesc->instrument_options;
255         estate->es_jit_flags = queryDesc->plannedstmt->jitFlags;
256
257         /*
258          * Set up an AFTER-trigger statement context, unless told not to, or
259          * unless it's EXPLAIN-only mode (when ExecutorFinish won't be called).
260          */
261         if (!(eflags & (EXEC_FLAG_SKIP_TRIGGERS | EXEC_FLAG_EXPLAIN_ONLY)))
262                 AfterTriggerBeginQuery();
263
264         /*
265          * Initialize the plan state tree
266          */
267         InitPlan(queryDesc, eflags);
268
269         MemoryContextSwitchTo(oldcontext);
270 }
271
272 /* ----------------------------------------------------------------
273  *              ExecutorRun
274  *
275  *              This is the main routine of the executor module. It accepts
276  *              the query descriptor from the traffic cop and executes the
277  *              query plan.
278  *
279  *              ExecutorStart must have been called already.
280  *
281  *              If direction is NoMovementScanDirection then nothing is done
282  *              except to start up/shut down the destination.  Otherwise,
283  *              we retrieve up to 'count' tuples in the specified direction.
284  *
285  *              Note: count = 0 is interpreted as no portal limit, i.e., run to
286  *              completion.  Also note that the count limit is only applied to
287  *              retrieved tuples, not for instance to those inserted/updated/deleted
288  *              by a ModifyTable plan node.
289  *
290  *              There is no return value, but output tuples (if any) are sent to
291  *              the destination receiver specified in the QueryDesc; and the number
292  *              of tuples processed at the top level can be found in
293  *              estate->es_processed.
294  *
295  *              We provide a function hook variable that lets loadable plugins
296  *              get control when ExecutorRun is called.  Such a plugin would
297  *              normally call standard_ExecutorRun().
298  *
299  * ----------------------------------------------------------------
300  */
301 void
302 ExecutorRun(QueryDesc *queryDesc,
303                         ScanDirection direction, uint64 count,
304                         bool execute_once)
305 {
306         if (ExecutorRun_hook)
307                 (*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
308         else
309                 standard_ExecutorRun(queryDesc, direction, count, execute_once);
310 }
311
312 void
313 standard_ExecutorRun(QueryDesc *queryDesc,
314                                          ScanDirection direction, uint64 count, bool execute_once)
315 {
316         EState     *estate;
317         CmdType         operation;
318         DestReceiver *dest;
319         bool            sendTuples;
320         MemoryContext oldcontext;
321
322         /* sanity checks */
323         Assert(queryDesc != NULL);
324
325         estate = queryDesc->estate;
326
327         Assert(estate != NULL);
328         Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
329
330         /*
331          * Switch into per-query memory context
332          */
333         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
334
335         /* Allow instrumentation of Executor overall runtime */
336         if (queryDesc->totaltime)
337                 InstrStartNode(queryDesc->totaltime);
338
339         /*
340          * extract information from the query descriptor and the query feature.
341          */
342         operation = queryDesc->operation;
343         dest = queryDesc->dest;
344
345         /*
346          * startup tuple receiver, if we will be emitting tuples
347          */
348         estate->es_processed = 0;
349
350         sendTuples = (operation == CMD_SELECT ||
351                                   queryDesc->plannedstmt->hasReturning);
352
353         if (sendTuples)
354                 dest->rStartup(dest, operation, queryDesc->tupDesc);
355
356         /*
357          * run plan
358          */
359         if (!ScanDirectionIsNoMovement(direction))
360         {
361                 if (execute_once && queryDesc->already_executed)
362                         elog(ERROR, "can't re-execute query flagged for single execution");
363                 queryDesc->already_executed = true;
364
365                 ExecutePlan(estate,
366                                         queryDesc->planstate,
367                                         queryDesc->plannedstmt->parallelModeNeeded,
368                                         operation,
369                                         sendTuples,
370                                         count,
371                                         direction,
372                                         dest,
373                                         execute_once);
374         }
375
376         /*
377          * shutdown tuple receiver, if we started it
378          */
379         if (sendTuples)
380                 dest->rShutdown(dest);
381
382         if (queryDesc->totaltime)
383                 InstrStopNode(queryDesc->totaltime, estate->es_processed);
384
385         MemoryContextSwitchTo(oldcontext);
386 }
387
388 /* ----------------------------------------------------------------
389  *              ExecutorFinish
390  *
391  *              This routine must be called after the last ExecutorRun call.
392  *              It performs cleanup such as firing AFTER triggers.  It is
393  *              separate from ExecutorEnd because EXPLAIN ANALYZE needs to
394  *              include these actions in the total runtime.
395  *
396  *              We provide a function hook variable that lets loadable plugins
397  *              get control when ExecutorFinish is called.  Such a plugin would
398  *              normally call standard_ExecutorFinish().
399  *
400  * ----------------------------------------------------------------
401  */
402 void
403 ExecutorFinish(QueryDesc *queryDesc)
404 {
405         if (ExecutorFinish_hook)
406                 (*ExecutorFinish_hook) (queryDesc);
407         else
408                 standard_ExecutorFinish(queryDesc);
409 }
410
411 void
412 standard_ExecutorFinish(QueryDesc *queryDesc)
413 {
414         EState     *estate;
415         MemoryContext oldcontext;
416
417         /* sanity checks */
418         Assert(queryDesc != NULL);
419
420         estate = queryDesc->estate;
421
422         Assert(estate != NULL);
423         Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
424
425         /* This should be run once and only once per Executor instance */
426         Assert(!estate->es_finished);
427
428         /* Switch into per-query memory context */
429         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
430
431         /* Allow instrumentation of Executor overall runtime */
432         if (queryDesc->totaltime)
433                 InstrStartNode(queryDesc->totaltime);
434
435         /* Run ModifyTable nodes to completion */
436         ExecPostprocessPlan(estate);
437
438         /* Execute queued AFTER triggers, unless told not to */
439         if (!(estate->es_top_eflags & EXEC_FLAG_SKIP_TRIGGERS))
440                 AfterTriggerEndQuery(estate);
441
442         if (queryDesc->totaltime)
443                 InstrStopNode(queryDesc->totaltime, 0);
444
445         MemoryContextSwitchTo(oldcontext);
446
447         estate->es_finished = true;
448 }
449
450 /* ----------------------------------------------------------------
451  *              ExecutorEnd
452  *
453  *              This routine must be called at the end of execution of any
454  *              query plan
455  *
456  *              We provide a function hook variable that lets loadable plugins
457  *              get control when ExecutorEnd is called.  Such a plugin would
458  *              normally call standard_ExecutorEnd().
459  *
460  * ----------------------------------------------------------------
461  */
462 void
463 ExecutorEnd(QueryDesc *queryDesc)
464 {
465         if (ExecutorEnd_hook)
466                 (*ExecutorEnd_hook) (queryDesc);
467         else
468                 standard_ExecutorEnd(queryDesc);
469 }
470
471 void
472 standard_ExecutorEnd(QueryDesc *queryDesc)
473 {
474         EState     *estate;
475         MemoryContext oldcontext;
476
477         /* sanity checks */
478         Assert(queryDesc != NULL);
479
480         estate = queryDesc->estate;
481
482         Assert(estate != NULL);
483
484         /*
485          * Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
486          * Assert is needed because ExecutorFinish is new as of 9.1, and callers
487          * might forget to call it.
488          */
489         Assert(estate->es_finished ||
490                    (estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
491
492         /*
493          * Switch into per-query memory context to run ExecEndPlan
494          */
495         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
496
497         ExecEndPlan(queryDesc->planstate, estate);
498
499         /* do away with our snapshots */
500         UnregisterSnapshot(estate->es_snapshot);
501         UnregisterSnapshot(estate->es_crosscheck_snapshot);
502
503         /*
504          * Must switch out of context before destroying it
505          */
506         MemoryContextSwitchTo(oldcontext);
507
508         /*
509          * Release EState and per-query memory context.  This should release
510          * everything the executor has allocated.
511          */
512         FreeExecutorState(estate);
513
514         /* Reset queryDesc fields that no longer point to anything */
515         queryDesc->tupDesc = NULL;
516         queryDesc->estate = NULL;
517         queryDesc->planstate = NULL;
518         queryDesc->totaltime = NULL;
519 }
520
521 /* ----------------------------------------------------------------
522  *              ExecutorRewind
523  *
524  *              This routine may be called on an open queryDesc to rewind it
525  *              to the start.
526  * ----------------------------------------------------------------
527  */
528 void
529 ExecutorRewind(QueryDesc *queryDesc)
530 {
531         EState     *estate;
532         MemoryContext oldcontext;
533
534         /* sanity checks */
535         Assert(queryDesc != NULL);
536
537         estate = queryDesc->estate;
538
539         Assert(estate != NULL);
540
541         /* It's probably not sensible to rescan updating queries */
542         Assert(queryDesc->operation == CMD_SELECT);
543
544         /*
545          * Switch into per-query memory context
546          */
547         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
548
549         /*
550          * rescan plan
551          */
552         ExecReScan(queryDesc->planstate);
553
554         MemoryContextSwitchTo(oldcontext);
555 }
556
557
558 /*
559  * ExecCheckRTPerms
560  *              Check access permissions for all relations listed in a range table.
561  *
562  * Returns true if permissions are adequate.  Otherwise, throws an appropriate
563  * error if ereport_on_violation is true, or simply returns false otherwise.
564  *
565  * Note that this does NOT address row level security policies (aka: RLS).  If
566  * rows will be returned to the user as a result of this permission check
567  * passing, then RLS also needs to be consulted (and check_enable_rls()).
568  *
569  * See rewrite/rowsecurity.c.
570  */
571 bool
572 ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
573 {
574         ListCell   *l;
575         bool            result = true;
576
577         foreach(l, rangeTable)
578         {
579                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
580
581                 result = ExecCheckRTEPerms(rte);
582                 if (!result)
583                 {
584                         Assert(rte->rtekind == RTE_RELATION);
585                         if (ereport_on_violation)
586                                 aclcheck_error(ACLCHECK_NO_PRIV, get_relkind_objtype(get_rel_relkind(rte->relid)),
587                                                            get_rel_name(rte->relid));
588                         return false;
589                 }
590         }
591
592         if (ExecutorCheckPerms_hook)
593                 result = (*ExecutorCheckPerms_hook) (rangeTable,
594                                                                                          ereport_on_violation);
595         return result;
596 }
597
598 /*
599  * ExecCheckRTEPerms
600  *              Check access permissions for a single RTE.
601  */
602 static bool
603 ExecCheckRTEPerms(RangeTblEntry *rte)
604 {
605         AclMode         requiredPerms;
606         AclMode         relPerms;
607         AclMode         remainingPerms;
608         Oid                     relOid;
609         Oid                     userid;
610
611         /*
612          * Only plain-relation RTEs need to be checked here.  Function RTEs are
613          * checked when the function is prepared for execution.  Join, subquery,
614          * and special RTEs need no checks.
615          */
616         if (rte->rtekind != RTE_RELATION)
617                 return true;
618
619         /*
620          * No work if requiredPerms is empty.
621          */
622         requiredPerms = rte->requiredPerms;
623         if (requiredPerms == 0)
624                 return true;
625
626         relOid = rte->relid;
627
628         /*
629          * userid to check as: current user unless we have a setuid indication.
630          *
631          * Note: GetUserId() is presently fast enough that there's no harm in
632          * calling it separately for each RTE.  If that stops being true, we could
633          * call it once in ExecCheckRTPerms and pass the userid down from there.
634          * But for now, no need for the extra clutter.
635          */
636         userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
637
638         /*
639          * We must have *all* the requiredPerms bits, but some of the bits can be
640          * satisfied from column-level rather than relation-level permissions.
641          * First, remove any bits that are satisfied by relation permissions.
642          */
643         relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
644         remainingPerms = requiredPerms & ~relPerms;
645         if (remainingPerms != 0)
646         {
647                 int                     col = -1;
648
649                 /*
650                  * If we lack any permissions that exist only as relation permissions,
651                  * we can fail straight away.
652                  */
653                 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
654                         return false;
655
656                 /*
657                  * Check to see if we have the needed privileges at column level.
658                  *
659                  * Note: failures just report a table-level error; it would be nicer
660                  * to report a column-level error if we have some but not all of the
661                  * column privileges.
662                  */
663                 if (remainingPerms & ACL_SELECT)
664                 {
665                         /*
666                          * When the query doesn't explicitly reference any columns (for
667                          * example, SELECT COUNT(*) FROM table), allow the query if we
668                          * have SELECT on any column of the rel, as per SQL spec.
669                          */
670                         if (bms_is_empty(rte->selectedCols))
671                         {
672                                 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
673                                                                                           ACLMASK_ANY) != ACLCHECK_OK)
674                                         return false;
675                         }
676
677                         while ((col = bms_next_member(rte->selectedCols, col)) >= 0)
678                         {
679                                 /* bit #s are offset by FirstLowInvalidHeapAttributeNumber */
680                                 AttrNumber      attno = col + FirstLowInvalidHeapAttributeNumber;
681
682                                 if (attno == InvalidAttrNumber)
683                                 {
684                                         /* Whole-row reference, must have priv on all cols */
685                                         if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
686                                                                                                   ACLMASK_ALL) != ACLCHECK_OK)
687                                                 return false;
688                                 }
689                                 else
690                                 {
691                                         if (pg_attribute_aclcheck(relOid, attno, userid,
692                                                                                           ACL_SELECT) != ACLCHECK_OK)
693                                                 return false;
694                                 }
695                         }
696                 }
697
698                 /*
699                  * Basically the same for the mod columns, for both INSERT and UPDATE
700                  * privilege as specified by remainingPerms.
701                  */
702                 if (remainingPerms & ACL_INSERT && !ExecCheckRTEPermsModified(relOid,
703                                                                                                                                           userid,
704                                                                                                                                           rte->insertedCols,
705                                                                                                                                           ACL_INSERT))
706                         return false;
707
708                 if (remainingPerms & ACL_UPDATE && !ExecCheckRTEPermsModified(relOid,
709                                                                                                                                           userid,
710                                                                                                                                           rte->updatedCols,
711                                                                                                                                           ACL_UPDATE))
712                         return false;
713         }
714         return true;
715 }
716
717 /*
718  * ExecCheckRTEPermsModified
719  *              Check INSERT or UPDATE access permissions for a single RTE (these
720  *              are processed uniformly).
721  */
722 static bool
723 ExecCheckRTEPermsModified(Oid relOid, Oid userid, Bitmapset *modifiedCols,
724                                                   AclMode requiredPerms)
725 {
726         int                     col = -1;
727
728         /*
729          * When the query doesn't explicitly update any columns, allow the query
730          * if we have permission on any column of the rel.  This is to handle
731          * SELECT FOR UPDATE as well as possible corner cases in UPDATE.
732          */
733         if (bms_is_empty(modifiedCols))
734         {
735                 if (pg_attribute_aclcheck_all(relOid, userid, requiredPerms,
736                                                                           ACLMASK_ANY) != ACLCHECK_OK)
737                         return false;
738         }
739
740         while ((col = bms_next_member(modifiedCols, col)) >= 0)
741         {
742                 /* bit #s are offset by FirstLowInvalidHeapAttributeNumber */
743                 AttrNumber      attno = col + FirstLowInvalidHeapAttributeNumber;
744
745                 if (attno == InvalidAttrNumber)
746                 {
747                         /* whole-row reference can't happen here */
748                         elog(ERROR, "whole-row update is not implemented");
749                 }
750                 else
751                 {
752                         if (pg_attribute_aclcheck(relOid, attno, userid,
753                                                                           requiredPerms) != ACLCHECK_OK)
754                                 return false;
755                 }
756         }
757         return true;
758 }
759
760 /*
761  * Check that the query does not imply any writes to non-temp tables;
762  * unless we're in parallel mode, in which case don't even allow writes
763  * to temp tables.
764  *
765  * Note: in a Hot Standby this would need to reject writes to temp
766  * tables just as we do in parallel mode; but an HS standby can't have created
767  * any temp tables in the first place, so no need to check that.
768  */
769 static void
770 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
771 {
772         ListCell   *l;
773
774         /*
775          * Fail if write permissions are requested in parallel mode for table
776          * (temp or non-temp), otherwise fail for any non-temp table.
777          */
778         foreach(l, plannedstmt->rtable)
779         {
780                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
781
782                 if (rte->rtekind != RTE_RELATION)
783                         continue;
784
785                 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
786                         continue;
787
788                 if (isTempNamespace(get_rel_namespace(rte->relid)))
789                         continue;
790
791                 PreventCommandIfReadOnly(CreateCommandTag((Node *) plannedstmt));
792         }
793
794         if (plannedstmt->commandType != CMD_SELECT || plannedstmt->hasModifyingCTE)
795                 PreventCommandIfParallelMode(CreateCommandTag((Node *) plannedstmt));
796 }
797
798
799 /* ----------------------------------------------------------------
800  *              InitPlan
801  *
802  *              Initializes the query plan: open files, allocate storage
803  *              and start up the rule manager
804  * ----------------------------------------------------------------
805  */
806 static void
807 InitPlan(QueryDesc *queryDesc, int eflags)
808 {
809         CmdType         operation = queryDesc->operation;
810         PlannedStmt *plannedstmt = queryDesc->plannedstmt;
811         Plan       *plan = plannedstmt->planTree;
812         List       *rangeTable = plannedstmt->rtable;
813         EState     *estate = queryDesc->estate;
814         PlanState  *planstate;
815         TupleDesc       tupType;
816         ListCell   *l;
817         int                     i;
818
819         /*
820          * Do permissions checks
821          */
822         ExecCheckRTPerms(rangeTable, true);
823
824         /*
825          * initialize the node's execution state
826          */
827         ExecInitRangeTable(estate, rangeTable);
828
829         estate->es_plannedstmt = plannedstmt;
830
831         /*
832          * Initialize ResultRelInfo data structures, and open the result rels.
833          */
834         if (plannedstmt->resultRelations)
835         {
836                 List       *resultRelations = plannedstmt->resultRelations;
837                 int                     numResultRelations = list_length(resultRelations);
838                 ResultRelInfo *resultRelInfos;
839                 ResultRelInfo *resultRelInfo;
840
841                 resultRelInfos = (ResultRelInfo *)
842                         palloc(numResultRelations * sizeof(ResultRelInfo));
843                 resultRelInfo = resultRelInfos;
844                 foreach(l, resultRelations)
845                 {
846                         Index           resultRelationIndex = lfirst_int(l);
847                         Relation        resultRelation;
848
849                         resultRelation = ExecGetRangeTableRelation(estate,
850                                                                                                            resultRelationIndex);
851                         InitResultRelInfo(resultRelInfo,
852                                                           resultRelation,
853                                                           resultRelationIndex,
854                                                           NULL,
855                                                           estate->es_instrument);
856                         resultRelInfo++;
857                 }
858                 estate->es_result_relations = resultRelInfos;
859                 estate->es_num_result_relations = numResultRelations;
860
861                 /* es_result_relation_info is NULL except when within ModifyTable */
862                 estate->es_result_relation_info = NULL;
863
864                 /*
865                  * In the partitioned result relation case, also build ResultRelInfos
866                  * for all the partitioned table roots, because we will need them to
867                  * fire statement-level triggers, if any.
868                  */
869                 if (plannedstmt->rootResultRelations)
870                 {
871                         int                     num_roots = list_length(plannedstmt->rootResultRelations);
872
873                         resultRelInfos = (ResultRelInfo *)
874                                 palloc(num_roots * sizeof(ResultRelInfo));
875                         resultRelInfo = resultRelInfos;
876                         foreach(l, plannedstmt->rootResultRelations)
877                         {
878                                 Index           resultRelIndex = lfirst_int(l);
879                                 Relation        resultRelDesc;
880
881                                 resultRelDesc = ExecGetRangeTableRelation(estate,
882                                                                                                                   resultRelIndex);
883                                 InitResultRelInfo(resultRelInfo,
884                                                                   resultRelDesc,
885                                                                   resultRelIndex,
886                                                                   NULL,
887                                                                   estate->es_instrument);
888                                 resultRelInfo++;
889                         }
890
891                         estate->es_root_result_relations = resultRelInfos;
892                         estate->es_num_root_result_relations = num_roots;
893                 }
894                 else
895                 {
896                         estate->es_root_result_relations = NULL;
897                         estate->es_num_root_result_relations = 0;
898                 }
899         }
900         else
901         {
902                 /*
903                  * if no result relation, then set state appropriately
904                  */
905                 estate->es_result_relations = NULL;
906                 estate->es_num_result_relations = 0;
907                 estate->es_result_relation_info = NULL;
908                 estate->es_root_result_relations = NULL;
909                 estate->es_num_root_result_relations = 0;
910         }
911
912         /*
913          * Next, build the ExecRowMark array from the PlanRowMark(s), if any.
914          */
915         if (plannedstmt->rowMarks)
916         {
917                 estate->es_rowmarks = (ExecRowMark **)
918                         palloc0(estate->es_range_table_size * sizeof(ExecRowMark *));
919                 foreach(l, plannedstmt->rowMarks)
920                 {
921                         PlanRowMark *rc = (PlanRowMark *) lfirst(l);
922                         Oid                     relid;
923                         Relation        relation;
924                         ExecRowMark *erm;
925
926                         /* ignore "parent" rowmarks; they are irrelevant at runtime */
927                         if (rc->isParent)
928                                 continue;
929
930                         /* get relation's OID (will produce InvalidOid if subquery) */
931                         relid = exec_rt_fetch(rc->rti, estate)->relid;
932
933                         /* open relation, if we need to access it for this mark type */
934                         switch (rc->markType)
935                         {
936                                 case ROW_MARK_EXCLUSIVE:
937                                 case ROW_MARK_NOKEYEXCLUSIVE:
938                                 case ROW_MARK_SHARE:
939                                 case ROW_MARK_KEYSHARE:
940                                 case ROW_MARK_REFERENCE:
941                                         relation = ExecGetRangeTableRelation(estate, rc->rti);
942                                         break;
943                                 case ROW_MARK_COPY:
944                                         /* no physical table access is required */
945                                         relation = NULL;
946                                         break;
947                                 default:
948                                         elog(ERROR, "unrecognized markType: %d", rc->markType);
949                                         relation = NULL;        /* keep compiler quiet */
950                                         break;
951                         }
952
953                         /* Check that relation is a legal target for marking */
954                         if (relation)
955                                 CheckValidRowMarkRel(relation, rc->markType);
956
957                         erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
958                         erm->relation = relation;
959                         erm->relid = relid;
960                         erm->rti = rc->rti;
961                         erm->prti = rc->prti;
962                         erm->rowmarkId = rc->rowmarkId;
963                         erm->markType = rc->markType;
964                         erm->strength = rc->strength;
965                         erm->waitPolicy = rc->waitPolicy;
966                         erm->ermActive = false;
967                         ItemPointerSetInvalid(&(erm->curCtid));
968                         erm->ermExtra = NULL;
969
970                         Assert(erm->rti > 0 && erm->rti <= estate->es_range_table_size &&
971                                    estate->es_rowmarks[erm->rti - 1] == NULL);
972
973                         estate->es_rowmarks[erm->rti - 1] = erm;
974                 }
975         }
976
977         /*
978          * Initialize the executor's tuple table to empty.
979          */
980         estate->es_tupleTable = NIL;
981
982         /* mark EvalPlanQual not active */
983         estate->es_epqTupleSlot = NULL;
984         estate->es_epqScanDone = NULL;
985
986         /*
987          * Initialize private state information for each SubPlan.  We must do this
988          * before running ExecInitNode on the main query tree, since
989          * ExecInitSubPlan expects to be able to find these entries.
990          */
991         Assert(estate->es_subplanstates == NIL);
992         i = 1;                                          /* subplan indices count from 1 */
993         foreach(l, plannedstmt->subplans)
994         {
995                 Plan       *subplan = (Plan *) lfirst(l);
996                 PlanState  *subplanstate;
997                 int                     sp_eflags;
998
999                 /*
1000                  * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
1001                  * it is a parameterless subplan (not initplan), we suggest that it be
1002                  * prepared to handle REWIND efficiently; otherwise there is no need.
1003                  */
1004                 sp_eflags = eflags
1005                         & (EXEC_FLAG_EXPLAIN_ONLY | EXEC_FLAG_WITH_NO_DATA);
1006                 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
1007                         sp_eflags |= EXEC_FLAG_REWIND;
1008
1009                 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
1010
1011                 estate->es_subplanstates = lappend(estate->es_subplanstates,
1012                                                                                    subplanstate);
1013
1014                 i++;
1015         }
1016
1017         /*
1018          * Initialize the private state information for all the nodes in the query
1019          * tree.  This opens files, allocates storage and leaves us ready to start
1020          * processing tuples.
1021          */
1022         planstate = ExecInitNode(plan, estate, eflags);
1023
1024         /*
1025          * Get the tuple descriptor describing the type of tuples to return.
1026          */
1027         tupType = ExecGetResultType(planstate);
1028
1029         /*
1030          * Initialize the junk filter if needed.  SELECT queries need a filter if
1031          * there are any junk attrs in the top-level tlist.
1032          */
1033         if (operation == CMD_SELECT)
1034         {
1035                 bool            junk_filter_needed = false;
1036                 ListCell   *tlist;
1037
1038                 foreach(tlist, plan->targetlist)
1039                 {
1040                         TargetEntry *tle = (TargetEntry *) lfirst(tlist);
1041
1042                         if (tle->resjunk)
1043                         {
1044                                 junk_filter_needed = true;
1045                                 break;
1046                         }
1047                 }
1048
1049                 if (junk_filter_needed)
1050                 {
1051                         JunkFilter *j;
1052                         TupleTableSlot *slot;
1053
1054                         slot = ExecInitExtraTupleSlot(estate, NULL, &TTSOpsVirtual);
1055                         j = ExecInitJunkFilter(planstate->plan->targetlist,
1056                                                                    slot);
1057                         estate->es_junkFilter = j;
1058
1059                         /* Want to return the cleaned tuple type */
1060                         tupType = j->jf_cleanTupType;
1061                 }
1062         }
1063
1064         queryDesc->tupDesc = tupType;
1065         queryDesc->planstate = planstate;
1066 }
1067
1068 /*
1069  * Check that a proposed result relation is a legal target for the operation
1070  *
1071  * Generally the parser and/or planner should have noticed any such mistake
1072  * already, but let's make sure.
1073  *
1074  * Note: when changing this function, you probably also need to look at
1075  * CheckValidRowMarkRel.
1076  */
1077 void
1078 CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
1079 {
1080         Relation        resultRel = resultRelInfo->ri_RelationDesc;
1081         TriggerDesc *trigDesc = resultRel->trigdesc;
1082         FdwRoutine *fdwroutine;
1083
1084         switch (resultRel->rd_rel->relkind)
1085         {
1086                 case RELKIND_RELATION:
1087                 case RELKIND_PARTITIONED_TABLE:
1088                         CheckCmdReplicaIdentity(resultRel, operation);
1089                         break;
1090                 case RELKIND_SEQUENCE:
1091                         ereport(ERROR,
1092                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1093                                          errmsg("cannot change sequence \"%s\"",
1094                                                         RelationGetRelationName(resultRel))));
1095                         break;
1096                 case RELKIND_TOASTVALUE:
1097                         ereport(ERROR,
1098                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1099                                          errmsg("cannot change TOAST relation \"%s\"",
1100                                                         RelationGetRelationName(resultRel))));
1101                         break;
1102                 case RELKIND_VIEW:
1103
1104                         /*
1105                          * Okay only if there's a suitable INSTEAD OF trigger.  Messages
1106                          * here should match rewriteHandler.c's rewriteTargetView, except
1107                          * that we omit errdetail because we haven't got the information
1108                          * handy (and given that we really shouldn't get here anyway, it's
1109                          * not worth great exertion to get).
1110                          */
1111                         switch (operation)
1112                         {
1113                                 case CMD_INSERT:
1114                                         if (!trigDesc || !trigDesc->trig_insert_instead_row)
1115                                                 ereport(ERROR,
1116                                                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1117                                                                  errmsg("cannot insert into view \"%s\"",
1118                                                                                 RelationGetRelationName(resultRel)),
1119                                                                  errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.")));
1120                                         break;
1121                                 case CMD_UPDATE:
1122                                         if (!trigDesc || !trigDesc->trig_update_instead_row)
1123                                                 ereport(ERROR,
1124                                                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1125                                                                  errmsg("cannot update view \"%s\"",
1126                                                                                 RelationGetRelationName(resultRel)),
1127                                                                  errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.")));
1128                                         break;
1129                                 case CMD_DELETE:
1130                                         if (!trigDesc || !trigDesc->trig_delete_instead_row)
1131                                                 ereport(ERROR,
1132                                                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1133                                                                  errmsg("cannot delete from view \"%s\"",
1134                                                                                 RelationGetRelationName(resultRel)),
1135                                                                  errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.")));
1136                                         break;
1137                                 default:
1138                                         elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1139                                         break;
1140                         }
1141                         break;
1142                 case RELKIND_MATVIEW:
1143                         if (!MatViewIncrementalMaintenanceIsEnabled())
1144                                 ereport(ERROR,
1145                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1146                                                  errmsg("cannot change materialized view \"%s\"",
1147                                                                 RelationGetRelationName(resultRel))));
1148                         break;
1149                 case RELKIND_FOREIGN_TABLE:
1150                         /* Okay only if the FDW supports it */
1151                         fdwroutine = resultRelInfo->ri_FdwRoutine;
1152                         switch (operation)
1153                         {
1154                                 case CMD_INSERT:
1155                                         if (fdwroutine->ExecForeignInsert == NULL)
1156                                                 ereport(ERROR,
1157                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1158                                                                  errmsg("cannot insert into foreign table \"%s\"",
1159                                                                                 RelationGetRelationName(resultRel))));
1160                                         if (fdwroutine->IsForeignRelUpdatable != NULL &&
1161                                                 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_INSERT)) == 0)
1162                                                 ereport(ERROR,
1163                                                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1164                                                                  errmsg("foreign table \"%s\" does not allow inserts",
1165                                                                                 RelationGetRelationName(resultRel))));
1166                                         break;
1167                                 case CMD_UPDATE:
1168                                         if (fdwroutine->ExecForeignUpdate == NULL)
1169                                                 ereport(ERROR,
1170                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1171                                                                  errmsg("cannot update foreign table \"%s\"",
1172                                                                                 RelationGetRelationName(resultRel))));
1173                                         if (fdwroutine->IsForeignRelUpdatable != NULL &&
1174                                                 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_UPDATE)) == 0)
1175                                                 ereport(ERROR,
1176                                                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1177                                                                  errmsg("foreign table \"%s\" does not allow updates",
1178                                                                                 RelationGetRelationName(resultRel))));
1179                                         break;
1180                                 case CMD_DELETE:
1181                                         if (fdwroutine->ExecForeignDelete == NULL)
1182                                                 ereport(ERROR,
1183                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1184                                                                  errmsg("cannot delete from foreign table \"%s\"",
1185                                                                                 RelationGetRelationName(resultRel))));
1186                                         if (fdwroutine->IsForeignRelUpdatable != NULL &&
1187                                                 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_DELETE)) == 0)
1188                                                 ereport(ERROR,
1189                                                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1190                                                                  errmsg("foreign table \"%s\" does not allow deletes",
1191                                                                                 RelationGetRelationName(resultRel))));
1192                                         break;
1193                                 default:
1194                                         elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1195                                         break;
1196                         }
1197                         break;
1198                 default:
1199                         ereport(ERROR,
1200                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1201                                          errmsg("cannot change relation \"%s\"",
1202                                                         RelationGetRelationName(resultRel))));
1203                         break;
1204         }
1205 }
1206
1207 /*
1208  * Check that a proposed rowmark target relation is a legal target
1209  *
1210  * In most cases parser and/or planner should have noticed this already, but
1211  * they don't cover all cases.
1212  */
1213 static void
1214 CheckValidRowMarkRel(Relation rel, RowMarkType markType)
1215 {
1216         FdwRoutine *fdwroutine;
1217
1218         switch (rel->rd_rel->relkind)
1219         {
1220                 case RELKIND_RELATION:
1221                 case RELKIND_PARTITIONED_TABLE:
1222                         /* OK */
1223                         break;
1224                 case RELKIND_SEQUENCE:
1225                         /* Must disallow this because we don't vacuum sequences */
1226                         ereport(ERROR,
1227                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1228                                          errmsg("cannot lock rows in sequence \"%s\"",
1229                                                         RelationGetRelationName(rel))));
1230                         break;
1231                 case RELKIND_TOASTVALUE:
1232                         /* We could allow this, but there seems no good reason to */
1233                         ereport(ERROR,
1234                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1235                                          errmsg("cannot lock rows in TOAST relation \"%s\"",
1236                                                         RelationGetRelationName(rel))));
1237                         break;
1238                 case RELKIND_VIEW:
1239                         /* Should not get here; planner should have expanded the view */
1240                         ereport(ERROR,
1241                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1242                                          errmsg("cannot lock rows in view \"%s\"",
1243                                                         RelationGetRelationName(rel))));
1244                         break;
1245                 case RELKIND_MATVIEW:
1246                         /* Allow referencing a matview, but not actual locking clauses */
1247                         if (markType != ROW_MARK_REFERENCE)
1248                                 ereport(ERROR,
1249                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1250                                                  errmsg("cannot lock rows in materialized view \"%s\"",
1251                                                                 RelationGetRelationName(rel))));
1252                         break;
1253                 case RELKIND_FOREIGN_TABLE:
1254                         /* Okay only if the FDW supports it */
1255                         fdwroutine = GetFdwRoutineForRelation(rel, false);
1256                         if (fdwroutine->RefetchForeignRow == NULL)
1257                                 ereport(ERROR,
1258                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1259                                                  errmsg("cannot lock rows in foreign table \"%s\"",
1260                                                                 RelationGetRelationName(rel))));
1261                         break;
1262                 default:
1263                         ereport(ERROR,
1264                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1265                                          errmsg("cannot lock rows in relation \"%s\"",
1266                                                         RelationGetRelationName(rel))));
1267                         break;
1268         }
1269 }
1270
1271 /*
1272  * Initialize ResultRelInfo data for one result relation
1273  *
1274  * Caution: before Postgres 9.1, this function included the relkind checking
1275  * that's now in CheckValidResultRel, and it also did ExecOpenIndices if
1276  * appropriate.  Be sure callers cover those needs.
1277  */
1278 void
1279 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1280                                   Relation resultRelationDesc,
1281                                   Index resultRelationIndex,
1282                                   Relation partition_root,
1283                                   int instrument_options)
1284 {
1285         List       *partition_check = NIL;
1286
1287         MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1288         resultRelInfo->type = T_ResultRelInfo;
1289         resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1290         resultRelInfo->ri_RelationDesc = resultRelationDesc;
1291         resultRelInfo->ri_NumIndices = 0;
1292         resultRelInfo->ri_IndexRelationDescs = NULL;
1293         resultRelInfo->ri_IndexRelationInfo = NULL;
1294         /* make a copy so as not to depend on relcache info not changing... */
1295         resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1296         if (resultRelInfo->ri_TrigDesc)
1297         {
1298                 int                     n = resultRelInfo->ri_TrigDesc->numtriggers;
1299
1300                 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1301                         palloc0(n * sizeof(FmgrInfo));
1302                 resultRelInfo->ri_TrigWhenExprs = (ExprState **)
1303                         palloc0(n * sizeof(ExprState *));
1304                 if (instrument_options)
1305                         resultRelInfo->ri_TrigInstrument = InstrAlloc(n, instrument_options);
1306         }
1307         else
1308         {
1309                 resultRelInfo->ri_TrigFunctions = NULL;
1310                 resultRelInfo->ri_TrigWhenExprs = NULL;
1311                 resultRelInfo->ri_TrigInstrument = NULL;
1312         }
1313         if (resultRelationDesc->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1314                 resultRelInfo->ri_FdwRoutine = GetFdwRoutineForRelation(resultRelationDesc, true);
1315         else
1316                 resultRelInfo->ri_FdwRoutine = NULL;
1317
1318         /* The following fields are set later if needed */
1319         resultRelInfo->ri_FdwState = NULL;
1320         resultRelInfo->ri_usesFdwDirectModify = false;
1321         resultRelInfo->ri_ConstraintExprs = NULL;
1322         resultRelInfo->ri_GeneratedExprs = NULL;
1323         resultRelInfo->ri_junkFilter = NULL;
1324         resultRelInfo->ri_projectReturning = NULL;
1325         resultRelInfo->ri_onConflictArbiterIndexes = NIL;
1326         resultRelInfo->ri_onConflict = NULL;
1327         resultRelInfo->ri_ReturningSlot = NULL;
1328         resultRelInfo->ri_TrigOldSlot = NULL;
1329         resultRelInfo->ri_TrigNewSlot = NULL;
1330
1331         /*
1332          * Partition constraint, which also includes the partition constraint of
1333          * all the ancestors that are partitions.  Note that it will be checked
1334          * even in the case of tuple-routing where this table is the target leaf
1335          * partition, if there any BR triggers defined on the table.  Although
1336          * tuple-routing implicitly preserves the partition constraint of the
1337          * target partition for a given row, the BR triggers may change the row
1338          * such that the constraint is no longer satisfied, which we must fail for
1339          * by checking it explicitly.
1340          *
1341          * If this is a partitioned table, the partition constraint (if any) of a
1342          * given row will be checked just before performing tuple-routing.
1343          */
1344         partition_check = RelationGetPartitionQual(resultRelationDesc);
1345
1346         resultRelInfo->ri_PartitionCheck = partition_check;
1347         resultRelInfo->ri_PartitionRoot = partition_root;
1348         resultRelInfo->ri_PartitionInfo = NULL; /* may be set later */
1349         resultRelInfo->ri_CopyMultiInsertBuffer = NULL;
1350 }
1351
1352 /*
1353  * ExecGetTriggerResultRel
1354  *              Get a ResultRelInfo for a trigger target relation.
1355  *
1356  * Most of the time, triggers are fired on one of the result relations of the
1357  * query, and so we can just return a member of the es_result_relations array,
1358  * or the es_root_result_relations array (if any), or the
1359  * es_tuple_routing_result_relations list (if any).  (Note: in self-join
1360  * situations there might be multiple members with the same OID; if so it
1361  * doesn't matter which one we pick.)
1362  *
1363  * However, it is sometimes necessary to fire triggers on other relations;
1364  * this happens mainly when an RI update trigger queues additional triggers
1365  * on other relations, which will be processed in the context of the outer
1366  * query.  For efficiency's sake, we want to have a ResultRelInfo for those
1367  * triggers too; that can avoid repeated re-opening of the relation.  (It
1368  * also provides a way for EXPLAIN ANALYZE to report the runtimes of such
1369  * triggers.)  So we make additional ResultRelInfo's as needed, and save them
1370  * in es_trig_target_relations.
1371  */
1372 ResultRelInfo *
1373 ExecGetTriggerResultRel(EState *estate, Oid relid)
1374 {
1375         ResultRelInfo *rInfo;
1376         int                     nr;
1377         ListCell   *l;
1378         Relation        rel;
1379         MemoryContext oldcontext;
1380
1381         /* First, search through the query result relations */
1382         rInfo = estate->es_result_relations;
1383         nr = estate->es_num_result_relations;
1384         while (nr > 0)
1385         {
1386                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1387                         return rInfo;
1388                 rInfo++;
1389                 nr--;
1390         }
1391         /* Second, search through the root result relations, if any */
1392         rInfo = estate->es_root_result_relations;
1393         nr = estate->es_num_root_result_relations;
1394         while (nr > 0)
1395         {
1396                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1397                         return rInfo;
1398                 rInfo++;
1399                 nr--;
1400         }
1401
1402         /*
1403          * Third, search through the result relations that were created during
1404          * tuple routing, if any.
1405          */
1406         foreach(l, estate->es_tuple_routing_result_relations)
1407         {
1408                 rInfo = (ResultRelInfo *) lfirst(l);
1409                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1410                         return rInfo;
1411         }
1412
1413         /* Nope, but maybe we already made an extra ResultRelInfo for it */
1414         foreach(l, estate->es_trig_target_relations)
1415         {
1416                 rInfo = (ResultRelInfo *) lfirst(l);
1417                 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1418                         return rInfo;
1419         }
1420         /* Nope, so we need a new one */
1421
1422         /*
1423          * Open the target relation's relcache entry.  We assume that an
1424          * appropriate lock is still held by the backend from whenever the trigger
1425          * event got queued, so we need take no new lock here.  Also, we need not
1426          * recheck the relkind, so no need for CheckValidResultRel.
1427          */
1428         rel = table_open(relid, NoLock);
1429
1430         /*
1431          * Make the new entry in the right context.
1432          */
1433         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1434         rInfo = makeNode(ResultRelInfo);
1435         InitResultRelInfo(rInfo,
1436                                           rel,
1437                                           0,            /* dummy rangetable index */
1438                                           NULL,
1439                                           estate->es_instrument);
1440         estate->es_trig_target_relations =
1441                 lappend(estate->es_trig_target_relations, rInfo);
1442         MemoryContextSwitchTo(oldcontext);
1443
1444         /*
1445          * Currently, we don't need any index information in ResultRelInfos used
1446          * only for triggers, so no need to call ExecOpenIndices.
1447          */
1448
1449         return rInfo;
1450 }
1451
1452 /*
1453  * Close any relations that have been opened by ExecGetTriggerResultRel().
1454  */
1455 void
1456 ExecCleanUpTriggerState(EState *estate)
1457 {
1458         ListCell   *l;
1459
1460         foreach(l, estate->es_trig_target_relations)
1461         {
1462                 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
1463
1464                 /*
1465                  * Assert this is a "dummy" ResultRelInfo, see above.  Otherwise we
1466                  * might be issuing a duplicate close against a Relation opened by
1467                  * ExecGetRangeTableRelation.
1468                  */
1469                 Assert(resultRelInfo->ri_RangeTableIndex == 0);
1470
1471                 /*
1472                  * Since ExecGetTriggerResultRel doesn't call ExecOpenIndices for
1473                  * these rels, we needn't call ExecCloseIndices either.
1474                  */
1475                 Assert(resultRelInfo->ri_NumIndices == 0);
1476
1477                 table_close(resultRelInfo->ri_RelationDesc, NoLock);
1478         }
1479 }
1480
1481 /* ----------------------------------------------------------------
1482  *              ExecPostprocessPlan
1483  *
1484  *              Give plan nodes a final chance to execute before shutdown
1485  * ----------------------------------------------------------------
1486  */
1487 static void
1488 ExecPostprocessPlan(EState *estate)
1489 {
1490         ListCell   *lc;
1491
1492         /*
1493          * Make sure nodes run forward.
1494          */
1495         estate->es_direction = ForwardScanDirection;
1496
1497         /*
1498          * Run any secondary ModifyTable nodes to completion, in case the main
1499          * query did not fetch all rows from them.  (We do this to ensure that
1500          * such nodes have predictable results.)
1501          */
1502         foreach(lc, estate->es_auxmodifytables)
1503         {
1504                 PlanState  *ps = (PlanState *) lfirst(lc);
1505
1506                 for (;;)
1507                 {
1508                         TupleTableSlot *slot;
1509
1510                         /* Reset the per-output-tuple exprcontext each time */
1511                         ResetPerTupleExprContext(estate);
1512
1513                         slot = ExecProcNode(ps);
1514
1515                         if (TupIsNull(slot))
1516                                 break;
1517                 }
1518         }
1519 }
1520
1521 /* ----------------------------------------------------------------
1522  *              ExecEndPlan
1523  *
1524  *              Cleans up the query plan -- closes files and frees up storage
1525  *
1526  * NOTE: we are no longer very worried about freeing storage per se
1527  * in this code; FreeExecutorState should be guaranteed to release all
1528  * memory that needs to be released.  What we are worried about doing
1529  * is closing relations and dropping buffer pins.  Thus, for example,
1530  * tuple tables must be cleared or dropped to ensure pins are released.
1531  * ----------------------------------------------------------------
1532  */
1533 static void
1534 ExecEndPlan(PlanState *planstate, EState *estate)
1535 {
1536         ResultRelInfo *resultRelInfo;
1537         Index           num_relations;
1538         Index           i;
1539         ListCell   *l;
1540
1541         /*
1542          * shut down the node-type-specific query processing
1543          */
1544         ExecEndNode(planstate);
1545
1546         /*
1547          * for subplans too
1548          */
1549         foreach(l, estate->es_subplanstates)
1550         {
1551                 PlanState  *subplanstate = (PlanState *) lfirst(l);
1552
1553                 ExecEndNode(subplanstate);
1554         }
1555
1556         /*
1557          * destroy the executor's tuple table.  Actually we only care about
1558          * releasing buffer pins and tupdesc refcounts; there's no need to pfree
1559          * the TupleTableSlots, since the containing memory context is about to go
1560          * away anyway.
1561          */
1562         ExecResetTupleTable(estate->es_tupleTable, false);
1563
1564         /*
1565          * close indexes of result relation(s) if any.  (Rels themselves get
1566          * closed next.)
1567          */
1568         resultRelInfo = estate->es_result_relations;
1569         for (i = estate->es_num_result_relations; i > 0; i--)
1570         {
1571                 ExecCloseIndices(resultRelInfo);
1572                 resultRelInfo++;
1573         }
1574
1575         /*
1576          * close whatever rangetable Relations have been opened.  We do not
1577          * release any locks we might hold on those rels.
1578          */
1579         num_relations = estate->es_range_table_size;
1580         for (i = 0; i < num_relations; i++)
1581         {
1582                 if (estate->es_relations[i])
1583                         table_close(estate->es_relations[i], NoLock);
1584         }
1585
1586         /* likewise close any trigger target relations */
1587         ExecCleanUpTriggerState(estate);
1588 }
1589
1590 /* ----------------------------------------------------------------
1591  *              ExecutePlan
1592  *
1593  *              Processes the query plan until we have retrieved 'numberTuples' tuples,
1594  *              moving in the specified direction.
1595  *
1596  *              Runs to completion if numberTuples is 0
1597  *
1598  * Note: the ctid attribute is a 'junk' attribute that is removed before the
1599  * user can see it
1600  * ----------------------------------------------------------------
1601  */
1602 static void
1603 ExecutePlan(EState *estate,
1604                         PlanState *planstate,
1605                         bool use_parallel_mode,
1606                         CmdType operation,
1607                         bool sendTuples,
1608                         uint64 numberTuples,
1609                         ScanDirection direction,
1610                         DestReceiver *dest,
1611                         bool execute_once)
1612 {
1613         TupleTableSlot *slot;
1614         uint64          current_tuple_count;
1615
1616         /*
1617          * initialize local variables
1618          */
1619         current_tuple_count = 0;
1620
1621         /*
1622          * Set the direction.
1623          */
1624         estate->es_direction = direction;
1625
1626         /*
1627          * If the plan might potentially be executed multiple times, we must force
1628          * it to run without parallelism, because we might exit early.
1629          */
1630         if (!execute_once)
1631                 use_parallel_mode = false;
1632
1633         estate->es_use_parallel_mode = use_parallel_mode;
1634         if (use_parallel_mode)
1635                 EnterParallelMode();
1636
1637         /*
1638          * Loop until we've processed the proper number of tuples from the plan.
1639          */
1640         for (;;)
1641         {
1642                 /* Reset the per-output-tuple exprcontext */
1643                 ResetPerTupleExprContext(estate);
1644
1645                 /*
1646                  * Execute the plan and obtain a tuple
1647                  */
1648                 slot = ExecProcNode(planstate);
1649
1650                 /*
1651                  * if the tuple is null, then we assume there is nothing more to
1652                  * process so we just end the loop...
1653                  */
1654                 if (TupIsNull(slot))
1655                 {
1656                         /*
1657                          * If we know we won't need to back up, we can release resources
1658                          * at this point.
1659                          */
1660                         if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
1661                                 (void) ExecShutdownNode(planstate);
1662                         break;
1663                 }
1664
1665                 /*
1666                  * If we have a junk filter, then project a new tuple with the junk
1667                  * removed.
1668                  *
1669                  * Store this new "clean" tuple in the junkfilter's resultSlot.
1670                  * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1671                  * because that tuple slot has the wrong descriptor.)
1672                  */
1673                 if (estate->es_junkFilter != NULL)
1674                         slot = ExecFilterJunk(estate->es_junkFilter, slot);
1675
1676                 /*
1677                  * If we are supposed to send the tuple somewhere, do so. (In
1678                  * practice, this is probably always the case at this point.)
1679                  */
1680                 if (sendTuples)
1681                 {
1682                         /*
1683                          * If we are not able to send the tuple, we assume the destination
1684                          * has closed and no more tuples can be sent. If that's the case,
1685                          * end the loop.
1686                          */
1687                         if (!dest->receiveSlot(slot, dest))
1688                                 break;
1689                 }
1690
1691                 /*
1692                  * Count tuples processed, if this is a SELECT.  (For other operation
1693                  * types, the ModifyTable plan node must count the appropriate
1694                  * events.)
1695                  */
1696                 if (operation == CMD_SELECT)
1697                         (estate->es_processed)++;
1698
1699                 /*
1700                  * check our tuple count.. if we've processed the proper number then
1701                  * quit, else loop again and process more tuples.  Zero numberTuples
1702                  * means no limit.
1703                  */
1704                 current_tuple_count++;
1705                 if (numberTuples && numberTuples == current_tuple_count)
1706                 {
1707                         /*
1708                          * If we know we won't need to back up, we can release resources
1709                          * at this point.
1710                          */
1711                         if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
1712                                 (void) ExecShutdownNode(planstate);
1713                         break;
1714                 }
1715         }
1716
1717         if (use_parallel_mode)
1718                 ExitParallelMode();
1719 }
1720
1721
1722 /*
1723  * ExecRelCheck --- check that tuple meets constraints for result relation
1724  *
1725  * Returns NULL if OK, else name of failed check constraint
1726  */
1727 static const char *
1728 ExecRelCheck(ResultRelInfo *resultRelInfo,
1729                          TupleTableSlot *slot, EState *estate)
1730 {
1731         Relation        rel = resultRelInfo->ri_RelationDesc;
1732         int                     ncheck = rel->rd_att->constr->num_check;
1733         ConstrCheck *check = rel->rd_att->constr->check;
1734         ExprContext *econtext;
1735         MemoryContext oldContext;
1736         int                     i;
1737
1738         /*
1739          * If first time through for this result relation, build expression
1740          * nodetrees for rel's constraint expressions.  Keep them in the per-query
1741          * memory context so they'll survive throughout the query.
1742          */
1743         if (resultRelInfo->ri_ConstraintExprs == NULL)
1744         {
1745                 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1746                 resultRelInfo->ri_ConstraintExprs =
1747                         (ExprState **) palloc(ncheck * sizeof(ExprState *));
1748                 for (i = 0; i < ncheck; i++)
1749                 {
1750                         Expr       *checkconstr;
1751
1752                         checkconstr = stringToNode(check[i].ccbin);
1753                         resultRelInfo->ri_ConstraintExprs[i] =
1754                                 ExecPrepareExpr(checkconstr, estate);
1755                 }
1756                 MemoryContextSwitchTo(oldContext);
1757         }
1758
1759         /*
1760          * We will use the EState's per-tuple context for evaluating constraint
1761          * expressions (creating it if it's not already there).
1762          */
1763         econtext = GetPerTupleExprContext(estate);
1764
1765         /* Arrange for econtext's scan tuple to be the tuple under test */
1766         econtext->ecxt_scantuple = slot;
1767
1768         /* And evaluate the constraints */
1769         for (i = 0; i < ncheck; i++)
1770         {
1771                 ExprState  *checkconstr = resultRelInfo->ri_ConstraintExprs[i];
1772
1773                 /*
1774                  * NOTE: SQL specifies that a NULL result from a constraint expression
1775                  * is not to be treated as a failure.  Therefore, use ExecCheck not
1776                  * ExecQual.
1777                  */
1778                 if (!ExecCheck(checkconstr, econtext))
1779                         return check[i].ccname;
1780         }
1781
1782         /* NULL result means no error */
1783         return NULL;
1784 }
1785
1786 /*
1787  * ExecPartitionCheck --- check that tuple meets the partition constraint.
1788  *
1789  * Returns true if it meets the partition constraint.  If the constraint
1790  * fails and we're asked to emit to error, do so and don't return; otherwise
1791  * return false.
1792  */
1793 bool
1794 ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot,
1795                                    EState *estate, bool emitError)
1796 {
1797         ExprContext *econtext;
1798         bool            success;
1799
1800         /*
1801          * If first time through, build expression state tree for the partition
1802          * check expression.  Keep it in the per-query memory context so they'll
1803          * survive throughout the query.
1804          */
1805         if (resultRelInfo->ri_PartitionCheckExpr == NULL)
1806         {
1807                 List       *qual = resultRelInfo->ri_PartitionCheck;
1808
1809                 resultRelInfo->ri_PartitionCheckExpr = ExecPrepareCheck(qual, estate);
1810         }
1811
1812         /*
1813          * We will use the EState's per-tuple context for evaluating constraint
1814          * expressions (creating it if it's not already there).
1815          */
1816         econtext = GetPerTupleExprContext(estate);
1817
1818         /* Arrange for econtext's scan tuple to be the tuple under test */
1819         econtext->ecxt_scantuple = slot;
1820
1821         /*
1822          * As in case of the catalogued constraints, we treat a NULL result as
1823          * success here, not a failure.
1824          */
1825         success = ExecCheck(resultRelInfo->ri_PartitionCheckExpr, econtext);
1826
1827         /* if asked to emit error, don't actually return on failure */
1828         if (!success && emitError)
1829                 ExecPartitionCheckEmitError(resultRelInfo, slot, estate);
1830
1831         return success;
1832 }
1833
1834 /*
1835  * ExecPartitionCheckEmitError - Form and emit an error message after a failed
1836  * partition constraint check.
1837  */
1838 void
1839 ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
1840                                                         TupleTableSlot *slot,
1841                                                         EState *estate)
1842 {
1843         Oid                     root_relid;
1844         TupleDesc       tupdesc;
1845         char       *val_desc;
1846         Bitmapset  *modifiedCols;
1847
1848         /*
1849          * If the tuple has been routed, it's been converted to the partition's
1850          * rowtype, which might differ from the root table's.  We must convert it
1851          * back to the root table's rowtype so that val_desc in the error message
1852          * matches the input tuple.
1853          */
1854         if (resultRelInfo->ri_PartitionRoot)
1855         {
1856                 TupleDesc       old_tupdesc;
1857                 AttrNumber *map;
1858
1859                 root_relid = RelationGetRelid(resultRelInfo->ri_PartitionRoot);
1860                 tupdesc = RelationGetDescr(resultRelInfo->ri_PartitionRoot);
1861
1862                 old_tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
1863                 /* a reverse map */
1864                 map = convert_tuples_by_name_map_if_req(old_tupdesc, tupdesc,
1865                                                                                                 gettext_noop("could not convert row type"));
1866
1867                 /*
1868                  * Partition-specific slot's tupdesc can't be changed, so allocate a
1869                  * new one.
1870                  */
1871                 if (map != NULL)
1872                         slot = execute_attr_map_slot(map, slot,
1873                                                                                  MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
1874         }
1875         else
1876         {
1877                 root_relid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
1878                 tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
1879         }
1880
1881         modifiedCols = bms_union(GetInsertedColumns(resultRelInfo, estate),
1882                                                          GetUpdatedColumns(resultRelInfo, estate));
1883
1884         val_desc = ExecBuildSlotValueDescription(root_relid,
1885                                                                                          slot,
1886                                                                                          tupdesc,
1887                                                                                          modifiedCols,
1888                                                                                          64);
1889         ereport(ERROR,
1890                         (errcode(ERRCODE_CHECK_VIOLATION),
1891                          errmsg("new row for relation \"%s\" violates partition constraint",
1892                                         RelationGetRelationName(resultRelInfo->ri_RelationDesc)),
1893                          val_desc ? errdetail("Failing row contains %s.", val_desc) : 0));
1894 }
1895
1896 /*
1897  * ExecConstraints - check constraints of the tuple in 'slot'
1898  *
1899  * This checks the traditional NOT NULL and check constraints.
1900  *
1901  * The partition constraint is *NOT* checked.
1902  *
1903  * Note: 'slot' contains the tuple to check the constraints of, which may
1904  * have been converted from the original input tuple after tuple routing.
1905  * 'resultRelInfo' is the final result relation, after tuple routing.
1906  */
1907 void
1908 ExecConstraints(ResultRelInfo *resultRelInfo,
1909                                 TupleTableSlot *slot, EState *estate)
1910 {
1911         Relation        rel = resultRelInfo->ri_RelationDesc;
1912         TupleDesc       tupdesc = RelationGetDescr(rel);
1913         TupleConstr *constr = tupdesc->constr;
1914         Bitmapset  *modifiedCols;
1915         Bitmapset  *insertedCols;
1916         Bitmapset  *updatedCols;
1917
1918         Assert(constr || resultRelInfo->ri_PartitionCheck);
1919
1920         if (constr && constr->has_not_null)
1921         {
1922                 int                     natts = tupdesc->natts;
1923                 int                     attrChk;
1924
1925                 for (attrChk = 1; attrChk <= natts; attrChk++)
1926                 {
1927                         Form_pg_attribute att = TupleDescAttr(tupdesc, attrChk - 1);
1928
1929                         if (att->attnotnull && slot_attisnull(slot, attrChk))
1930                         {
1931                                 char       *val_desc;
1932                                 Relation        orig_rel = rel;
1933                                 TupleDesc       orig_tupdesc = RelationGetDescr(rel);
1934
1935                                 /*
1936                                  * If the tuple has been routed, it's been converted to the
1937                                  * partition's rowtype, which might differ from the root
1938                                  * table's.  We must convert it back to the root table's
1939                                  * rowtype so that val_desc shown error message matches the
1940                                  * input tuple.
1941                                  */
1942                                 if (resultRelInfo->ri_PartitionRoot)
1943                                 {
1944                                         AttrNumber *map;
1945
1946                                         rel = resultRelInfo->ri_PartitionRoot;
1947                                         tupdesc = RelationGetDescr(rel);
1948                                         /* a reverse map */
1949                                         map = convert_tuples_by_name_map_if_req(orig_tupdesc,
1950                                                                                                                         tupdesc,
1951                                                                                                                         gettext_noop("could not convert row type"));
1952
1953                                         /*
1954                                          * Partition-specific slot's tupdesc can't be changed, so
1955                                          * allocate a new one.
1956                                          */
1957                                         if (map != NULL)
1958                                                 slot = execute_attr_map_slot(map, slot,
1959                                                                                                          MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
1960                                 }
1961
1962                                 insertedCols = GetInsertedColumns(resultRelInfo, estate);
1963                                 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
1964                                 modifiedCols = bms_union(insertedCols, updatedCols);
1965                                 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
1966                                                                                                                  slot,
1967                                                                                                                  tupdesc,
1968                                                                                                                  modifiedCols,
1969                                                                                                                  64);
1970
1971                                 ereport(ERROR,
1972                                                 (errcode(ERRCODE_NOT_NULL_VIOLATION),
1973                                                  errmsg("null value in column \"%s\" violates not-null constraint",
1974                                                                 NameStr(att->attname)),
1975                                                  val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
1976                                                  errtablecol(orig_rel, attrChk)));
1977                         }
1978                 }
1979         }
1980
1981         if (constr && constr->num_check > 0)
1982         {
1983                 const char *failed;
1984
1985                 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
1986                 {
1987                         char       *val_desc;
1988                         Relation        orig_rel = rel;
1989
1990                         /* See the comment above. */
1991                         if (resultRelInfo->ri_PartitionRoot)
1992                         {
1993                                 TupleDesc       old_tupdesc = RelationGetDescr(rel);
1994                                 AttrNumber *map;
1995
1996                                 rel = resultRelInfo->ri_PartitionRoot;
1997                                 tupdesc = RelationGetDescr(rel);
1998                                 /* a reverse map */
1999                                 map = convert_tuples_by_name_map_if_req(old_tupdesc,
2000                                                                                                                 tupdesc,
2001                                                                                                                 gettext_noop("could not convert row type"));
2002
2003                                 /*
2004                                  * Partition-specific slot's tupdesc can't be changed, so
2005                                  * allocate a new one.
2006                                  */
2007                                 if (map != NULL)
2008                                         slot = execute_attr_map_slot(map, slot,
2009                                                                                                  MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
2010                         }
2011
2012                         insertedCols = GetInsertedColumns(resultRelInfo, estate);
2013                         updatedCols = GetUpdatedColumns(resultRelInfo, estate);
2014                         modifiedCols = bms_union(insertedCols, updatedCols);
2015                         val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2016                                                                                                          slot,
2017                                                                                                          tupdesc,
2018                                                                                                          modifiedCols,
2019                                                                                                          64);
2020                         ereport(ERROR,
2021                                         (errcode(ERRCODE_CHECK_VIOLATION),
2022                                          errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2023                                                         RelationGetRelationName(orig_rel), failed),
2024                                          val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
2025                                          errtableconstraint(orig_rel, failed)));
2026                 }
2027         }
2028 }
2029
2030 /*
2031  * ExecWithCheckOptions -- check that tuple satisfies any WITH CHECK OPTIONs
2032  * of the specified kind.
2033  *
2034  * Note that this needs to be called multiple times to ensure that all kinds of
2035  * WITH CHECK OPTIONs are handled (both those from views which have the WITH
2036  * CHECK OPTION set and from row level security policies).  See ExecInsert()
2037  * and ExecUpdate().
2038  */
2039 void
2040 ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
2041                                          TupleTableSlot *slot, EState *estate)
2042 {
2043         Relation        rel = resultRelInfo->ri_RelationDesc;
2044         TupleDesc       tupdesc = RelationGetDescr(rel);
2045         ExprContext *econtext;
2046         ListCell   *l1,
2047                            *l2;
2048
2049         /*
2050          * We will use the EState's per-tuple context for evaluating constraint
2051          * expressions (creating it if it's not already there).
2052          */
2053         econtext = GetPerTupleExprContext(estate);
2054
2055         /* Arrange for econtext's scan tuple to be the tuple under test */
2056         econtext->ecxt_scantuple = slot;
2057
2058         /* Check each of the constraints */
2059         forboth(l1, resultRelInfo->ri_WithCheckOptions,
2060                         l2, resultRelInfo->ri_WithCheckOptionExprs)
2061         {
2062                 WithCheckOption *wco = (WithCheckOption *) lfirst(l1);
2063                 ExprState  *wcoExpr = (ExprState *) lfirst(l2);
2064
2065                 /*
2066                  * Skip any WCOs which are not the kind we are looking for at this
2067                  * time.
2068                  */
2069                 if (wco->kind != kind)
2070                         continue;
2071
2072                 /*
2073                  * WITH CHECK OPTION checks are intended to ensure that the new tuple
2074                  * is visible (in the case of a view) or that it passes the
2075                  * 'with-check' policy (in the case of row security). If the qual
2076                  * evaluates to NULL or FALSE, then the new tuple won't be included in
2077                  * the view or doesn't pass the 'with-check' policy for the table.
2078                  */
2079                 if (!ExecQual(wcoExpr, econtext))
2080                 {
2081                         char       *val_desc;
2082                         Bitmapset  *modifiedCols;
2083                         Bitmapset  *insertedCols;
2084                         Bitmapset  *updatedCols;
2085
2086                         switch (wco->kind)
2087                         {
2088                                         /*
2089                                          * For WITH CHECK OPTIONs coming from views, we might be
2090                                          * able to provide the details on the row, depending on
2091                                          * the permissions on the relation (that is, if the user
2092                                          * could view it directly anyway).  For RLS violations, we
2093                                          * don't include the data since we don't know if the user
2094                                          * should be able to view the tuple as that depends on the
2095                                          * USING policy.
2096                                          */
2097                                 case WCO_VIEW_CHECK:
2098                                         /* See the comment in ExecConstraints(). */
2099                                         if (resultRelInfo->ri_PartitionRoot)
2100                                         {
2101                                                 TupleDesc       old_tupdesc = RelationGetDescr(rel);
2102                                                 AttrNumber *map;
2103
2104                                                 rel = resultRelInfo->ri_PartitionRoot;
2105                                                 tupdesc = RelationGetDescr(rel);
2106                                                 /* a reverse map */
2107                                                 map = convert_tuples_by_name_map_if_req(old_tupdesc,
2108                                                                                                                                 tupdesc,
2109                                                                                                                                 gettext_noop("could not convert row type"));
2110
2111                                                 /*
2112                                                  * Partition-specific slot's tupdesc can't be changed,
2113                                                  * so allocate a new one.
2114                                                  */
2115                                                 if (map != NULL)
2116                                                         slot = execute_attr_map_slot(map, slot,
2117                                                                                                                  MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
2118                                         }
2119
2120                                         insertedCols = GetInsertedColumns(resultRelInfo, estate);
2121                                         updatedCols = GetUpdatedColumns(resultRelInfo, estate);
2122                                         modifiedCols = bms_union(insertedCols, updatedCols);
2123                                         val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2124                                                                                                                          slot,
2125                                                                                                                          tupdesc,
2126                                                                                                                          modifiedCols,
2127                                                                                                                          64);
2128
2129                                         ereport(ERROR,
2130                                                         (errcode(ERRCODE_WITH_CHECK_OPTION_VIOLATION),
2131                                                          errmsg("new row violates check option for view \"%s\"",
2132                                                                         wco->relname),
2133                                                          val_desc ? errdetail("Failing row contains %s.",
2134                                                                                                   val_desc) : 0));
2135                                         break;
2136                                 case WCO_RLS_INSERT_CHECK:
2137                                 case WCO_RLS_UPDATE_CHECK:
2138                                         if (wco->polname != NULL)
2139                                                 ereport(ERROR,
2140                                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2141                                                                  errmsg("new row violates row-level security policy \"%s\" for table \"%s\"",
2142                                                                                 wco->polname, wco->relname)));
2143                                         else
2144                                                 ereport(ERROR,
2145                                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2146                                                                  errmsg("new row violates row-level security policy for table \"%s\"",
2147                                                                                 wco->relname)));
2148                                         break;
2149                                 case WCO_RLS_CONFLICT_CHECK:
2150                                         if (wco->polname != NULL)
2151                                                 ereport(ERROR,
2152                                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2153                                                                  errmsg("new row violates row-level security policy \"%s\" (USING expression) for table \"%s\"",
2154                                                                                 wco->polname, wco->relname)));
2155                                         else
2156                                                 ereport(ERROR,
2157                                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2158                                                                  errmsg("new row violates row-level security policy (USING expression) for table \"%s\"",
2159                                                                                 wco->relname)));
2160                                         break;
2161                                 default:
2162                                         elog(ERROR, "unrecognized WCO kind: %u", wco->kind);
2163                                         break;
2164                         }
2165                 }
2166         }
2167 }
2168
2169 /*
2170  * ExecBuildSlotValueDescription -- construct a string representing a tuple
2171  *
2172  * This is intentionally very similar to BuildIndexValueDescription, but
2173  * unlike that function, we truncate long field values (to at most maxfieldlen
2174  * bytes).  That seems necessary here since heap field values could be very
2175  * long, whereas index entries typically aren't so wide.
2176  *
2177  * Also, unlike the case with index entries, we need to be prepared to ignore
2178  * dropped columns.  We used to use the slot's tuple descriptor to decode the
2179  * data, but the slot's descriptor doesn't identify dropped columns, so we
2180  * now need to be passed the relation's descriptor.
2181  *
2182  * Note that, like BuildIndexValueDescription, if the user does not have
2183  * permission to view any of the columns involved, a NULL is returned.  Unlike
2184  * BuildIndexValueDescription, if the user has access to view a subset of the
2185  * column involved, that subset will be returned with a key identifying which
2186  * columns they are.
2187  */
2188 static char *
2189 ExecBuildSlotValueDescription(Oid reloid,
2190                                                           TupleTableSlot *slot,
2191                                                           TupleDesc tupdesc,
2192                                                           Bitmapset *modifiedCols,
2193                                                           int maxfieldlen)
2194 {
2195         StringInfoData buf;
2196         StringInfoData collist;
2197         bool            write_comma = false;
2198         bool            write_comma_collist = false;
2199         int                     i;
2200         AclResult       aclresult;
2201         bool            table_perm = false;
2202         bool            any_perm = false;
2203
2204         /*
2205          * Check if RLS is enabled and should be active for the relation; if so,
2206          * then don't return anything.  Otherwise, go through normal permission
2207          * checks.
2208          */
2209         if (check_enable_rls(reloid, InvalidOid, true) == RLS_ENABLED)
2210                 return NULL;
2211
2212         initStringInfo(&buf);
2213
2214         appendStringInfoChar(&buf, '(');
2215
2216         /*
2217          * Check if the user has permissions to see the row.  Table-level SELECT
2218          * allows access to all columns.  If the user does not have table-level
2219          * SELECT then we check each column and include those the user has SELECT
2220          * rights on.  Additionally, we always include columns the user provided
2221          * data for.
2222          */
2223         aclresult = pg_class_aclcheck(reloid, GetUserId(), ACL_SELECT);
2224         if (aclresult != ACLCHECK_OK)
2225         {
2226                 /* Set up the buffer for the column list */
2227                 initStringInfo(&collist);
2228                 appendStringInfoChar(&collist, '(');
2229         }
2230         else
2231                 table_perm = any_perm = true;
2232
2233         /* Make sure the tuple is fully deconstructed */
2234         slot_getallattrs(slot);
2235
2236         for (i = 0; i < tupdesc->natts; i++)
2237         {
2238                 bool            column_perm = false;
2239                 char       *val;
2240                 int                     vallen;
2241                 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2242
2243                 /* ignore dropped columns */
2244                 if (att->attisdropped)
2245                         continue;
2246
2247                 if (!table_perm)
2248                 {
2249                         /*
2250                          * No table-level SELECT, so need to make sure they either have
2251                          * SELECT rights on the column or that they have provided the data
2252                          * for the column.  If not, omit this column from the error
2253                          * message.
2254                          */
2255                         aclresult = pg_attribute_aclcheck(reloid, att->attnum,
2256                                                                                           GetUserId(), ACL_SELECT);
2257                         if (bms_is_member(att->attnum - FirstLowInvalidHeapAttributeNumber,
2258                                                           modifiedCols) || aclresult == ACLCHECK_OK)
2259                         {
2260                                 column_perm = any_perm = true;
2261
2262                                 if (write_comma_collist)
2263                                         appendStringInfoString(&collist, ", ");
2264                                 else
2265                                         write_comma_collist = true;
2266
2267                                 appendStringInfoString(&collist, NameStr(att->attname));
2268                         }
2269                 }
2270
2271                 if (table_perm || column_perm)
2272                 {
2273                         if (slot->tts_isnull[i])
2274                                 val = "null";
2275                         else
2276                         {
2277                                 Oid                     foutoid;
2278                                 bool            typisvarlena;
2279
2280                                 getTypeOutputInfo(att->atttypid,
2281                                                                   &foutoid, &typisvarlena);
2282                                 val = OidOutputFunctionCall(foutoid, slot->tts_values[i]);
2283                         }
2284
2285                         if (write_comma)
2286                                 appendStringInfoString(&buf, ", ");
2287                         else
2288                                 write_comma = true;
2289
2290                         /* truncate if needed */
2291                         vallen = strlen(val);
2292                         if (vallen <= maxfieldlen)
2293                                 appendStringInfoString(&buf, val);
2294                         else
2295                         {
2296                                 vallen = pg_mbcliplen(val, vallen, maxfieldlen);
2297                                 appendBinaryStringInfo(&buf, val, vallen);
2298                                 appendStringInfoString(&buf, "...");
2299                         }
2300                 }
2301         }
2302
2303         /* If we end up with zero columns being returned, then return NULL. */
2304         if (!any_perm)
2305                 return NULL;
2306
2307         appendStringInfoChar(&buf, ')');
2308
2309         if (!table_perm)
2310         {
2311                 appendStringInfoString(&collist, ") = ");
2312                 appendStringInfoString(&collist, buf.data);
2313
2314                 return collist.data;
2315         }
2316
2317         return buf.data;
2318 }
2319
2320
2321 /*
2322  * ExecUpdateLockMode -- find the appropriate UPDATE tuple lock mode for a
2323  * given ResultRelInfo
2324  */
2325 LockTupleMode
2326 ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo)
2327 {
2328         Bitmapset  *keyCols;
2329         Bitmapset  *updatedCols;
2330
2331         /*
2332          * Compute lock mode to use.  If columns that are part of the key have not
2333          * been modified, then we can use a weaker lock, allowing for better
2334          * concurrency.
2335          */
2336         updatedCols = GetAllUpdatedColumns(relinfo, estate);
2337         keyCols = RelationGetIndexAttrBitmap(relinfo->ri_RelationDesc,
2338                                                                                  INDEX_ATTR_BITMAP_KEY);
2339
2340         if (bms_overlap(keyCols, updatedCols))
2341                 return LockTupleExclusive;
2342
2343         return LockTupleNoKeyExclusive;
2344 }
2345
2346 /*
2347  * ExecFindRowMark -- find the ExecRowMark struct for given rangetable index
2348  *
2349  * If no such struct, either return NULL or throw error depending on missing_ok
2350  */
2351 ExecRowMark *
2352 ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
2353 {
2354         if (rti > 0 && rti <= estate->es_range_table_size &&
2355                 estate->es_rowmarks != NULL)
2356         {
2357                 ExecRowMark *erm = estate->es_rowmarks[rti - 1];
2358
2359                 if (erm)
2360                         return erm;
2361         }
2362         if (!missing_ok)
2363                 elog(ERROR, "failed to find ExecRowMark for rangetable index %u", rti);
2364         return NULL;
2365 }
2366
2367 /*
2368  * ExecBuildAuxRowMark -- create an ExecAuxRowMark struct
2369  *
2370  * Inputs are the underlying ExecRowMark struct and the targetlist of the
2371  * input plan node (not planstate node!).  We need the latter to find out
2372  * the column numbers of the resjunk columns.
2373  */
2374 ExecAuxRowMark *
2375 ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
2376 {
2377         ExecAuxRowMark *aerm = (ExecAuxRowMark *) palloc0(sizeof(ExecAuxRowMark));
2378         char            resname[32];
2379
2380         aerm->rowmark = erm;
2381
2382         /* Look up the resjunk columns associated with this rowmark */
2383         if (erm->markType != ROW_MARK_COPY)
2384         {
2385                 /* need ctid for all methods other than COPY */
2386                 snprintf(resname, sizeof(resname), "ctid%u", erm->rowmarkId);
2387                 aerm->ctidAttNo = ExecFindJunkAttributeInTlist(targetlist,
2388                                                                                                            resname);
2389                 if (!AttributeNumberIsValid(aerm->ctidAttNo))
2390                         elog(ERROR, "could not find junk %s column", resname);
2391         }
2392         else
2393         {
2394                 /* need wholerow if COPY */
2395                 snprintf(resname, sizeof(resname), "wholerow%u", erm->rowmarkId);
2396                 aerm->wholeAttNo = ExecFindJunkAttributeInTlist(targetlist,
2397                                                                                                                 resname);
2398                 if (!AttributeNumberIsValid(aerm->wholeAttNo))
2399                         elog(ERROR, "could not find junk %s column", resname);
2400         }
2401
2402         /* if child rel, need tableoid */
2403         if (erm->rti != erm->prti)
2404         {
2405                 snprintf(resname, sizeof(resname), "tableoid%u", erm->rowmarkId);
2406                 aerm->toidAttNo = ExecFindJunkAttributeInTlist(targetlist,
2407                                                                                                            resname);
2408                 if (!AttributeNumberIsValid(aerm->toidAttNo))
2409                         elog(ERROR, "could not find junk %s column", resname);
2410         }
2411
2412         return aerm;
2413 }
2414
2415
2416 /*
2417  * EvalPlanQual logic --- recheck modified tuple(s) to see if we want to
2418  * process the updated version under READ COMMITTED rules.
2419  *
2420  * See backend/executor/README for some info about how this works.
2421  */
2422
2423
2424 /*
2425  * Check the updated version of a tuple to see if we want to process it under
2426  * READ COMMITTED rules.
2427  *
2428  *      estate - outer executor state data
2429  *      epqstate - state for EvalPlanQual rechecking
2430  *      relation - table containing tuple
2431  *      rti - rangetable index of table containing tuple
2432  *      inputslot - tuple for processing - this can be the slot from
2433  *              EvalPlanQualSlot(), for the increased efficiency.
2434  *
2435  * This tests whether the tuple in inputslot still matches the relvant
2436  * quals. For that result to be useful, typically the input tuple has to be
2437  * last row version (otherwise the result isn't particularly useful) and
2438  * locked (otherwise the result might be out of date). That's typically
2439  * achieved by using table_lock_tuple() with the
2440  * TUPLE_LOCK_FLAG_FIND_LAST_VERSION flag.
2441  *
2442  * Returns a slot containing the new candidate update/delete tuple, or
2443  * NULL if we determine we shouldn't process the row.
2444  */
2445 TupleTableSlot *
2446 EvalPlanQual(EState *estate, EPQState *epqstate,
2447                          Relation relation, Index rti, TupleTableSlot *inputslot)
2448 {
2449         TupleTableSlot *slot;
2450         TupleTableSlot *testslot;
2451
2452         Assert(rti > 0);
2453
2454         /*
2455          * Need to run a recheck subquery.  Initialize or reinitialize EPQ state.
2456          */
2457         EvalPlanQualBegin(epqstate, estate);
2458
2459         /*
2460          * Callers will often use the EvalPlanQualSlot to store the tuple to avoid
2461          * an unnecessary copy.
2462          */
2463         testslot = EvalPlanQualSlot(epqstate, relation, rti);
2464         if (testslot != inputslot)
2465                 ExecCopySlot(testslot, inputslot);
2466
2467         /*
2468          * Fetch any non-locked source rows
2469          */
2470         EvalPlanQualFetchRowMarks(epqstate);
2471
2472         /*
2473          * Run the EPQ query.  We assume it will return at most one tuple.
2474          */
2475         slot = EvalPlanQualNext(epqstate);
2476
2477         /*
2478          * If we got a tuple, force the slot to materialize the tuple so that it
2479          * is not dependent on any local state in the EPQ query (in particular,
2480          * it's highly likely that the slot contains references to any pass-by-ref
2481          * datums that may be present in copyTuple).  As with the next step, this
2482          * is to guard against early re-use of the EPQ query.
2483          */
2484         if (!TupIsNull(slot))
2485                 ExecMaterializeSlot(slot);
2486
2487         /*
2488          * Clear out the test tuple.  This is needed in case the EPQ query is
2489          * re-used to test a tuple for a different relation.  (Not clear that can
2490          * really happen, but let's be safe.)
2491          */
2492         ExecClearTuple(testslot);
2493
2494         return slot;
2495 }
2496
2497 /*
2498  * EvalPlanQualInit -- initialize during creation of a plan state node
2499  * that might need to invoke EPQ processing.
2500  *
2501  * Note: subplan/auxrowmarks can be NULL/NIL if they will be set later
2502  * with EvalPlanQualSetPlan.
2503  */
2504 void
2505 EvalPlanQualInit(EPQState *epqstate, EState *estate,
2506                                  Plan *subplan, List *auxrowmarks, int epqParam)
2507 {
2508         /* Mark the EPQ state inactive */
2509         epqstate->estate = NULL;
2510         epqstate->planstate = NULL;
2511         epqstate->origslot = NULL;
2512         /* ... and remember data that EvalPlanQualBegin will need */
2513         epqstate->plan = subplan;
2514         epqstate->arowMarks = auxrowmarks;
2515         epqstate->epqParam = epqParam;
2516 }
2517
2518 /*
2519  * EvalPlanQualSetPlan -- set or change subplan of an EPQState.
2520  *
2521  * We need this so that ModifyTable can deal with multiple subplans.
2522  */
2523 void
2524 EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
2525 {
2526         /* If we have a live EPQ query, shut it down */
2527         EvalPlanQualEnd(epqstate);
2528         /* And set/change the plan pointer */
2529         epqstate->plan = subplan;
2530         /* The rowmarks depend on the plan, too */
2531         epqstate->arowMarks = auxrowmarks;
2532 }
2533
2534 /*
2535  * Return, and create if necessary, a slot for an EPQ test tuple.
2536  */
2537 TupleTableSlot *
2538 EvalPlanQualSlot(EPQState *epqstate,
2539                                  Relation relation, Index rti)
2540 {
2541         TupleTableSlot **slot;
2542
2543         Assert(rti > 0 && rti <= epqstate->estate->es_range_table_size);
2544         slot = &epqstate->estate->es_epqTupleSlot[rti - 1];
2545
2546         if (*slot == NULL)
2547         {
2548                 MemoryContext oldcontext;
2549
2550                 oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
2551
2552                 if (relation)
2553                         *slot = table_slot_create(relation,
2554                                                                           &epqstate->estate->es_tupleTable);
2555                 else
2556                         *slot = ExecAllocTableSlot(&epqstate->estate->es_tupleTable,
2557                                                                            epqstate->origslot->tts_tupleDescriptor,
2558                                                                            &TTSOpsVirtual);
2559
2560                 MemoryContextSwitchTo(oldcontext);
2561         }
2562
2563         return *slot;
2564 }
2565
2566 /*
2567  * Fetch the current row values for any non-locked relations that need
2568  * to be scanned by an EvalPlanQual operation.  origslot must have been set
2569  * to contain the current result row (top-level row) that we need to recheck.
2570  */
2571 void
2572 EvalPlanQualFetchRowMarks(EPQState *epqstate)
2573 {
2574         ListCell   *l;
2575
2576         Assert(epqstate->origslot != NULL);
2577
2578         foreach(l, epqstate->arowMarks)
2579         {
2580                 ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(l);
2581                 ExecRowMark *erm = aerm->rowmark;
2582                 Datum           datum;
2583                 bool            isNull;
2584                 TupleTableSlot *slot;
2585
2586                 if (RowMarkRequiresRowShareLock(erm->markType))
2587                         elog(ERROR, "EvalPlanQual doesn't support locking rowmarks");
2588
2589                 /* clear any leftover test tuple for this rel */
2590                 slot = EvalPlanQualSlot(epqstate, erm->relation, erm->rti);
2591                 ExecClearTuple(slot);
2592
2593                 /* if child rel, must check whether it produced this row */
2594                 if (erm->rti != erm->prti)
2595                 {
2596                         Oid                     tableoid;
2597
2598                         datum = ExecGetJunkAttribute(epqstate->origslot,
2599                                                                                  aerm->toidAttNo,
2600                                                                                  &isNull);
2601                         /* non-locked rels could be on the inside of outer joins */
2602                         if (isNull)
2603                                 continue;
2604                         tableoid = DatumGetObjectId(datum);
2605
2606                         Assert(OidIsValid(erm->relid));
2607                         if (tableoid != erm->relid)
2608                         {
2609                                 /* this child is inactive right now */
2610                                 continue;
2611                         }
2612                 }
2613
2614                 if (erm->markType == ROW_MARK_REFERENCE)
2615                 {
2616                         Assert(erm->relation != NULL);
2617
2618                         /* fetch the tuple's ctid */
2619                         datum = ExecGetJunkAttribute(epqstate->origslot,
2620                                                                                  aerm->ctidAttNo,
2621                                                                                  &isNull);
2622                         /* non-locked rels could be on the inside of outer joins */
2623                         if (isNull)
2624                                 continue;
2625
2626                         /* fetch requests on foreign tables must be passed to their FDW */
2627                         if (erm->relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2628                         {
2629                                 FdwRoutine *fdwroutine;
2630                                 bool            updated = false;
2631
2632                                 fdwroutine = GetFdwRoutineForRelation(erm->relation, false);
2633                                 /* this should have been checked already, but let's be safe */
2634                                 if (fdwroutine->RefetchForeignRow == NULL)
2635                                         ereport(ERROR,
2636                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2637                                                          errmsg("cannot lock rows in foreign table \"%s\"",
2638                                                                         RelationGetRelationName(erm->relation))));
2639
2640                                 fdwroutine->RefetchForeignRow(epqstate->estate,
2641                                                                                           erm,
2642                                                                                           datum,
2643                                                                                           slot,
2644                                                                                           &updated);
2645                                 if (TupIsNull(slot))
2646                                         elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2647
2648                                 /*
2649                                  * Ideally we'd insist on updated == false here, but that
2650                                  * assumes that FDWs can track that exactly, which they might
2651                                  * not be able to.  So just ignore the flag.
2652                                  */
2653                         }
2654                         else
2655                         {
2656                                 /* ordinary table, fetch the tuple */
2657                                 if (!table_fetch_row_version(erm->relation,
2658                                                                                          (ItemPointer) DatumGetPointer(datum),
2659                                                                                          SnapshotAny, slot))
2660                                         elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2661                         }
2662                 }
2663                 else
2664                 {
2665                         Assert(erm->markType == ROW_MARK_COPY);
2666
2667                         /* fetch the whole-row Var for the relation */
2668                         datum = ExecGetJunkAttribute(epqstate->origslot,
2669                                                                                  aerm->wholeAttNo,
2670                                                                                  &isNull);
2671                         /* non-locked rels could be on the inside of outer joins */
2672                         if (isNull)
2673                                 continue;
2674
2675                         ExecStoreHeapTupleDatum(datum, slot);
2676                 }
2677         }
2678 }
2679
2680 /*
2681  * Fetch the next row (if any) from EvalPlanQual testing
2682  *
2683  * (In practice, there should never be more than one row...)
2684  */
2685 TupleTableSlot *
2686 EvalPlanQualNext(EPQState *epqstate)
2687 {
2688         MemoryContext oldcontext;
2689         TupleTableSlot *slot;
2690
2691         oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
2692         slot = ExecProcNode(epqstate->planstate);
2693         MemoryContextSwitchTo(oldcontext);
2694
2695         return slot;
2696 }
2697
2698 /*
2699  * Initialize or reset an EvalPlanQual state tree
2700  */
2701 void
2702 EvalPlanQualBegin(EPQState *epqstate, EState *parentestate)
2703 {
2704         EState     *estate = epqstate->estate;
2705
2706         if (estate == NULL)
2707         {
2708                 /* First time through, so create a child EState */
2709                 EvalPlanQualStart(epqstate, parentestate, epqstate->plan);
2710         }
2711         else
2712         {
2713                 /*
2714                  * We already have a suitable child EPQ tree, so just reset it.
2715                  */
2716                 Index           rtsize = parentestate->es_range_table_size;
2717                 PlanState  *planstate = epqstate->planstate;
2718
2719                 MemSet(estate->es_epqScanDone, 0, rtsize * sizeof(bool));
2720
2721                 /* Recopy current values of parent parameters */
2722                 if (parentestate->es_plannedstmt->paramExecTypes != NIL)
2723                 {
2724                         int                     i;
2725
2726                         /*
2727                          * Force evaluation of any InitPlan outputs that could be needed
2728                          * by the subplan, just in case they got reset since
2729                          * EvalPlanQualStart (see comments therein).
2730                          */
2731                         ExecSetParamPlanMulti(planstate->plan->extParam,
2732                                                                   GetPerTupleExprContext(parentestate));
2733
2734                         i = list_length(parentestate->es_plannedstmt->paramExecTypes);
2735
2736                         while (--i >= 0)
2737                         {
2738                                 /* copy value if any, but not execPlan link */
2739                                 estate->es_param_exec_vals[i].value =
2740                                         parentestate->es_param_exec_vals[i].value;
2741                                 estate->es_param_exec_vals[i].isnull =
2742                                         parentestate->es_param_exec_vals[i].isnull;
2743                         }
2744                 }
2745
2746                 /*
2747                  * Mark child plan tree as needing rescan at all scan nodes.  The
2748                  * first ExecProcNode will take care of actually doing the rescan.
2749                  */
2750                 planstate->chgParam = bms_add_member(planstate->chgParam,
2751                                                                                          epqstate->epqParam);
2752         }
2753 }
2754
2755 /*
2756  * Start execution of an EvalPlanQual plan tree.
2757  *
2758  * This is a cut-down version of ExecutorStart(): we copy some state from
2759  * the top-level estate rather than initializing it fresh.
2760  */
2761 static void
2762 EvalPlanQualStart(EPQState *epqstate, EState *parentestate, Plan *planTree)
2763 {
2764         EState     *estate;
2765         Index           rtsize;
2766         MemoryContext oldcontext;
2767         ListCell   *l;
2768
2769         rtsize = parentestate->es_range_table_size;
2770
2771         epqstate->estate = estate = CreateExecutorState();
2772
2773         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2774
2775         /*
2776          * Child EPQ EStates share the parent's copy of unchanging state such as
2777          * the snapshot, rangetable, result-rel info, and external Param info.
2778          * They need their own copies of local state, including a tuple table,
2779          * es_param_exec_vals, etc.
2780          *
2781          * The ResultRelInfo array management is trickier than it looks.  We
2782          * create fresh arrays for the child but copy all the content from the
2783          * parent.  This is because it's okay for the child to share any
2784          * per-relation state the parent has already created --- but if the child
2785          * sets up any ResultRelInfo fields, such as its own junkfilter, that
2786          * state must *not* propagate back to the parent.  (For one thing, the
2787          * pointed-to data is in a memory context that won't last long enough.)
2788          */
2789         estate->es_direction = ForwardScanDirection;
2790         estate->es_snapshot = parentestate->es_snapshot;
2791         estate->es_crosscheck_snapshot = parentestate->es_crosscheck_snapshot;
2792         estate->es_range_table = parentestate->es_range_table;
2793         estate->es_range_table_array = parentestate->es_range_table_array;
2794         estate->es_range_table_size = parentestate->es_range_table_size;
2795         estate->es_relations = parentestate->es_relations;
2796         estate->es_rowmarks = parentestate->es_rowmarks;
2797         estate->es_plannedstmt = parentestate->es_plannedstmt;
2798         estate->es_junkFilter = parentestate->es_junkFilter;
2799         estate->es_output_cid = parentestate->es_output_cid;
2800         if (parentestate->es_num_result_relations > 0)
2801         {
2802                 int                     numResultRelations = parentestate->es_num_result_relations;
2803                 int                     numRootResultRels = parentestate->es_num_root_result_relations;
2804                 ResultRelInfo *resultRelInfos;
2805
2806                 resultRelInfos = (ResultRelInfo *)
2807                         palloc(numResultRelations * sizeof(ResultRelInfo));
2808                 memcpy(resultRelInfos, parentestate->es_result_relations,
2809                            numResultRelations * sizeof(ResultRelInfo));
2810                 estate->es_result_relations = resultRelInfos;
2811                 estate->es_num_result_relations = numResultRelations;
2812
2813                 /* Also transfer partitioned root result relations. */
2814                 if (numRootResultRels > 0)
2815                 {
2816                         resultRelInfos = (ResultRelInfo *)
2817                                 palloc(numRootResultRels * sizeof(ResultRelInfo));
2818                         memcpy(resultRelInfos, parentestate->es_root_result_relations,
2819                                    numRootResultRels * sizeof(ResultRelInfo));
2820                         estate->es_root_result_relations = resultRelInfos;
2821                         estate->es_num_root_result_relations = numRootResultRels;
2822                 }
2823         }
2824         /* es_result_relation_info must NOT be copied */
2825         /* es_trig_target_relations must NOT be copied */
2826         estate->es_top_eflags = parentestate->es_top_eflags;
2827         estate->es_instrument = parentestate->es_instrument;
2828         /* es_auxmodifytables must NOT be copied */
2829
2830         /*
2831          * The external param list is simply shared from parent.  The internal
2832          * param workspace has to be local state, but we copy the initial values
2833          * from the parent, so as to have access to any param values that were
2834          * already set from other parts of the parent's plan tree.
2835          */
2836         estate->es_param_list_info = parentestate->es_param_list_info;
2837         if (parentestate->es_plannedstmt->paramExecTypes != NIL)
2838         {
2839                 int                     i;
2840
2841                 /*
2842                  * Force evaluation of any InitPlan outputs that could be needed by
2843                  * the subplan.  (With more complexity, maybe we could postpone this
2844                  * till the subplan actually demands them, but it doesn't seem worth
2845                  * the trouble; this is a corner case already, since usually the
2846                  * InitPlans would have been evaluated before reaching EvalPlanQual.)
2847                  *
2848                  * This will not touch output params of InitPlans that occur somewhere
2849                  * within the subplan tree, only those that are attached to the
2850                  * ModifyTable node or above it and are referenced within the subplan.
2851                  * That's OK though, because the planner would only attach such
2852                  * InitPlans to a lower-level SubqueryScan node, and EPQ execution
2853                  * will not descend into a SubqueryScan.
2854                  *
2855                  * The EState's per-output-tuple econtext is sufficiently short-lived
2856                  * for this, since it should get reset before there is any chance of
2857                  * doing EvalPlanQual again.
2858                  */
2859                 ExecSetParamPlanMulti(planTree->extParam,
2860                                                           GetPerTupleExprContext(parentestate));
2861
2862                 /* now make the internal param workspace ... */
2863                 i = list_length(parentestate->es_plannedstmt->paramExecTypes);
2864                 estate->es_param_exec_vals = (ParamExecData *)
2865                         palloc0(i * sizeof(ParamExecData));
2866                 /* ... and copy down all values, whether really needed or not */
2867                 while (--i >= 0)
2868                 {
2869                         /* copy value if any, but not execPlan link */
2870                         estate->es_param_exec_vals[i].value =
2871                                 parentestate->es_param_exec_vals[i].value;
2872                         estate->es_param_exec_vals[i].isnull =
2873                                 parentestate->es_param_exec_vals[i].isnull;
2874                 }
2875         }
2876
2877         /*
2878          * Each EState must have its own es_epqScanDone state, but if we have
2879          * nested EPQ checks they should share es_epqTuple arrays.  This allows
2880          * sub-rechecks to inherit the values being examined by an outer recheck.
2881          */
2882         estate->es_epqScanDone = (bool *) palloc0(rtsize * sizeof(bool));
2883         if (parentestate->es_epqTupleSlot != NULL)
2884         {
2885                 estate->es_epqTupleSlot = parentestate->es_epqTupleSlot;
2886         }
2887         else
2888         {
2889                 estate->es_epqTupleSlot = (TupleTableSlot **)
2890                         palloc0(rtsize * sizeof(TupleTableSlot *));
2891         }
2892
2893         /*
2894          * Each estate also has its own tuple table.
2895          */
2896         estate->es_tupleTable = NIL;
2897
2898         /*
2899          * Initialize private state information for each SubPlan.  We must do this
2900          * before running ExecInitNode on the main query tree, since
2901          * ExecInitSubPlan expects to be able to find these entries. Some of the
2902          * SubPlans might not be used in the part of the plan tree we intend to
2903          * run, but since it's not easy to tell which, we just initialize them
2904          * all.
2905          */
2906         Assert(estate->es_subplanstates == NIL);
2907         foreach(l, parentestate->es_plannedstmt->subplans)
2908         {
2909                 Plan       *subplan = (Plan *) lfirst(l);
2910                 PlanState  *subplanstate;
2911
2912                 subplanstate = ExecInitNode(subplan, estate, 0);
2913                 estate->es_subplanstates = lappend(estate->es_subplanstates,
2914                                                                                    subplanstate);
2915         }
2916
2917         /*
2918          * Initialize the private state information for all the nodes in the part
2919          * of the plan tree we need to run.  This opens files, allocates storage
2920          * and leaves us ready to start processing tuples.
2921          */
2922         epqstate->planstate = ExecInitNode(planTree, estate, 0);
2923
2924         MemoryContextSwitchTo(oldcontext);
2925 }
2926
2927 /*
2928  * EvalPlanQualEnd -- shut down at termination of parent plan state node,
2929  * or if we are done with the current EPQ child.
2930  *
2931  * This is a cut-down version of ExecutorEnd(); basically we want to do most
2932  * of the normal cleanup, but *not* close result relations (which we are
2933  * just sharing from the outer query).  We do, however, have to close any
2934  * trigger target relations that got opened, since those are not shared.
2935  * (There probably shouldn't be any of the latter, but just in case...)
2936  */
2937 void
2938 EvalPlanQualEnd(EPQState *epqstate)
2939 {
2940         EState     *estate = epqstate->estate;
2941         MemoryContext oldcontext;
2942         ListCell   *l;
2943
2944         if (estate == NULL)
2945                 return;                                 /* idle, so nothing to do */
2946
2947         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2948
2949         ExecEndNode(epqstate->planstate);
2950
2951         foreach(l, estate->es_subplanstates)
2952         {
2953                 PlanState  *subplanstate = (PlanState *) lfirst(l);
2954
2955                 ExecEndNode(subplanstate);
2956         }
2957
2958         /* throw away the per-estate tuple table */
2959         ExecResetTupleTable(estate->es_tupleTable, false);
2960
2961         /* close any trigger target relations attached to this EState */
2962         ExecCleanUpTriggerState(estate);
2963
2964         MemoryContextSwitchTo(oldcontext);
2965
2966         FreeExecutorState(estate);
2967
2968         /* Mark EPQState idle */
2969         epqstate->estate = NULL;
2970         epqstate->planstate = NULL;
2971         epqstate->origslot = NULL;
2972 }