PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
execUtils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execUtils.c
4  * miscellaneous executor utility routines
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/executor/execUtils.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
17  * CreateExecutorState Create/delete executor working state
18  * FreeExecutorState
19  * CreateExprContext
20  * CreateStandaloneExprContext
21  * FreeExprContext
22  * ReScanExprContext
23  *
24  * ExecAssignExprContext Common code for plan node init routines.
25  * ExecAssignResultType
26  * etc
27  *
28  * ExecOpenScanRelation Common code for scan node init routines.
29  * ExecCloseScanRelation
30  *
31  * RegisterExprContextCallback Register function shutdown callback
32  * UnregisterExprContextCallback Deregister function shutdown callback
33  *
34  * NOTES
35  * This file has traditionally been the place to stick misc.
36  * executor support stuff that doesn't really go anyplace else.
37  */
38 
39 #include "postgres.h"
40 
41 #include "access/relscan.h"
42 #include "access/transam.h"
43 #include "executor/executor.h"
44 #include "nodes/nodeFuncs.h"
45 #include "parser/parsetree.h"
46 #include "utils/memutils.h"
47 #include "utils/rel.h"
48 
49 
50 static bool get_last_attnums(Node *node, ProjectionInfo *projInfo);
51 static void ShutdownExprContext(ExprContext *econtext, bool isCommit);
52 
53 
54 /* ----------------------------------------------------------------
55  * Executor state and memory management functions
56  * ----------------------------------------------------------------
57  */
58 
59 /* ----------------
60  * CreateExecutorState
61  *
62  * Create and initialize an EState node, which is the root of
63  * working storage for an entire Executor invocation.
64  *
65  * Principally, this creates the per-query memory context that will be
66  * used to hold all working data that lives till the end of the query.
67  * Note that the per-query context will become a child of the caller's
68  * CurrentMemoryContext.
69  * ----------------
70  */
71 EState *
73 {
74  EState *estate;
75  MemoryContext qcontext;
76  MemoryContext oldcontext;
77 
78  /*
79  * Create the per-query context for this Executor run.
80  */
82  "ExecutorState",
84 
85  /*
86  * Make the EState node within the per-query context. This way, we don't
87  * need a separate pfree() operation for it at shutdown.
88  */
89  oldcontext = MemoryContextSwitchTo(qcontext);
90 
91  estate = makeNode(EState);
92 
93  /*
94  * Initialize all fields of the Executor State structure
95  */
97  estate->es_snapshot = InvalidSnapshot; /* caller must initialize this */
98  estate->es_crosscheck_snapshot = InvalidSnapshot; /* no crosscheck */
99  estate->es_range_table = NIL;
100  estate->es_plannedstmt = NULL;
101 
102  estate->es_junkFilter = NULL;
103 
104  estate->es_output_cid = (CommandId) 0;
105 
106  estate->es_result_relations = NULL;
107  estate->es_num_result_relations = 0;
108  estate->es_result_relation_info = NULL;
109 
110  estate->es_trig_target_relations = NIL;
111  estate->es_trig_tuple_slot = NULL;
112  estate->es_trig_oldtup_slot = NULL;
113  estate->es_trig_newtup_slot = NULL;
114 
115  estate->es_param_list_info = NULL;
116  estate->es_param_exec_vals = NULL;
117 
118  estate->es_query_cxt = qcontext;
119 
120  estate->es_tupleTable = NIL;
121 
122  estate->es_rowMarks = NIL;
123 
124  estate->es_processed = 0;
125  estate->es_lastoid = InvalidOid;
126 
127  estate->es_top_eflags = 0;
128  estate->es_instrument = 0;
129  estate->es_finished = false;
130 
131  estate->es_exprcontexts = NIL;
132 
133  estate->es_subplanstates = NIL;
134 
135  estate->es_auxmodifytables = NIL;
136 
137  estate->es_per_tuple_exprcontext = NULL;
138 
139  estate->es_epqTuple = NULL;
140  estate->es_epqTupleSet = NULL;
141  estate->es_epqScanDone = NULL;
142  estate->es_sourceText = NULL;
143 
144  /*
145  * Return the executor state structure
146  */
147  MemoryContextSwitchTo(oldcontext);
148 
149  return estate;
150 }
151 
152 /* ----------------
153  * FreeExecutorState
154  *
155  * Release an EState along with all remaining working storage.
156  *
157  * Note: this is not responsible for releasing non-memory resources,
158  * such as open relations or buffer pins. But it will shut down any
159  * still-active ExprContexts within the EState. That is sufficient
160  * cleanup for situations where the EState has only been used for expression
161  * evaluation, and not to run a complete Plan.
162  *
163  * This can be called in any memory context ... so long as it's not one
164  * of the ones to be freed.
165  * ----------------
166  */
167 void
169 {
170  /*
171  * Shut down and free any remaining ExprContexts. We do this explicitly
172  * to ensure that any remaining shutdown callbacks get called (since they
173  * might need to release resources that aren't simply memory within the
174  * per-query memory context).
175  */
176  while (estate->es_exprcontexts)
177  {
178  /*
179  * XXX: seems there ought to be a faster way to implement this than
180  * repeated list_delete(), no?
181  */
183  true);
184  /* FreeExprContext removed the list link for us */
185  }
186 
187  /*
188  * Free the per-query memory context, thereby releasing all working
189  * memory, including the EState node itself.
190  */
192 }
193 
194 /* ----------------
195  * CreateExprContext
196  *
197  * Create a context for expression evaluation within an EState.
198  *
199  * An executor run may require multiple ExprContexts (we usually make one
200  * for each Plan node, and a separate one for per-output-tuple processing
201  * such as constraint checking). Each ExprContext has its own "per-tuple"
202  * memory context.
203  *
204  * Note we make no assumption about the caller's memory context.
205  * ----------------
206  */
207 ExprContext *
209 {
210  ExprContext *econtext;
211  MemoryContext oldcontext;
212 
213  /* Create the ExprContext node within the per-query memory context */
214  oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
215 
216  econtext = makeNode(ExprContext);
217 
218  /* Initialize fields of ExprContext */
219  econtext->ecxt_scantuple = NULL;
220  econtext->ecxt_innertuple = NULL;
221  econtext->ecxt_outertuple = NULL;
222 
223  econtext->ecxt_per_query_memory = estate->es_query_cxt;
224 
225  /*
226  * Create working memory for expression evaluation in this context.
227  */
228  econtext->ecxt_per_tuple_memory =
230  "ExprContext",
232 
233  econtext->ecxt_param_exec_vals = estate->es_param_exec_vals;
234  econtext->ecxt_param_list_info = estate->es_param_list_info;
235 
236  econtext->ecxt_aggvalues = NULL;
237  econtext->ecxt_aggnulls = NULL;
238 
239  econtext->caseValue_datum = (Datum) 0;
240  econtext->caseValue_isNull = true;
241 
242  econtext->domainValue_datum = (Datum) 0;
243  econtext->domainValue_isNull = true;
244 
245  econtext->ecxt_estate = estate;
246 
247  econtext->ecxt_callbacks = NULL;
248 
249  /*
250  * Link the ExprContext into the EState to ensure it is shut down when the
251  * EState is freed. Because we use lcons(), shutdowns will occur in
252  * reverse order of creation, which may not be essential but can't hurt.
253  */
254  estate->es_exprcontexts = lcons(econtext, estate->es_exprcontexts);
255 
256  MemoryContextSwitchTo(oldcontext);
257 
258  return econtext;
259 }
260 
261 /* ----------------
262  * CreateStandaloneExprContext
263  *
264  * Create a context for standalone expression evaluation.
265  *
266  * An ExprContext made this way can be used for evaluation of expressions
267  * that contain no Params, subplans, or Var references (it might work to
268  * put tuple references into the scantuple field, but it seems unwise).
269  *
270  * The ExprContext struct is allocated in the caller's current memory
271  * context, which also becomes its "per query" context.
272  *
273  * It is caller's responsibility to free the ExprContext when done,
274  * or at least ensure that any shutdown callbacks have been called
275  * (ReScanExprContext() is suitable). Otherwise, non-memory resources
276  * might be leaked.
277  * ----------------
278  */
279 ExprContext *
281 {
282  ExprContext *econtext;
283 
284  /* Create the ExprContext node within the caller's memory context */
285  econtext = makeNode(ExprContext);
286 
287  /* Initialize fields of ExprContext */
288  econtext->ecxt_scantuple = NULL;
289  econtext->ecxt_innertuple = NULL;
290  econtext->ecxt_outertuple = NULL;
291 
293 
294  /*
295  * Create working memory for expression evaluation in this context.
296  */
297  econtext->ecxt_per_tuple_memory =
299  "ExprContext",
301 
302  econtext->ecxt_param_exec_vals = NULL;
303  econtext->ecxt_param_list_info = NULL;
304 
305  econtext->ecxt_aggvalues = NULL;
306  econtext->ecxt_aggnulls = NULL;
307 
308  econtext->caseValue_datum = (Datum) 0;
309  econtext->caseValue_isNull = true;
310 
311  econtext->domainValue_datum = (Datum) 0;
312  econtext->domainValue_isNull = true;
313 
314  econtext->ecxt_estate = NULL;
315 
316  econtext->ecxt_callbacks = NULL;
317 
318  return econtext;
319 }
320 
321 /* ----------------
322  * FreeExprContext
323  *
324  * Free an expression context, including calling any remaining
325  * shutdown callbacks.
326  *
327  * Since we free the temporary context used for expression evaluation,
328  * any previously computed pass-by-reference expression result will go away!
329  *
330  * If isCommit is false, we are being called in error cleanup, and should
331  * not call callbacks but only release memory. (It might be better to call
332  * the callbacks and pass the isCommit flag to them, but that would require
333  * more invasive code changes than currently seems justified.)
334  *
335  * Note we make no assumption about the caller's memory context.
336  * ----------------
337  */
338 void
339 FreeExprContext(ExprContext *econtext, bool isCommit)
340 {
341  EState *estate;
342 
343  /* Call any registered callbacks */
344  ShutdownExprContext(econtext, isCommit);
345  /* And clean up the memory used */
347  /* Unlink self from owning EState, if any */
348  estate = econtext->ecxt_estate;
349  if (estate)
351  econtext);
352  /* And delete the ExprContext node */
353  pfree(econtext);
354 }
355 
356 /*
357  * ReScanExprContext
358  *
359  * Reset an expression context in preparation for a rescan of its
360  * plan node. This requires calling any registered shutdown callbacks,
361  * since any partially complete set-returning-functions must be canceled.
362  *
363  * Note we make no assumption about the caller's memory context.
364  */
365 void
367 {
368  /* Call any registered callbacks */
369  ShutdownExprContext(econtext, true);
370  /* And clean up the memory used */
372 }
373 
374 /*
375  * Build a per-output-tuple ExprContext for an EState.
376  *
377  * This is normally invoked via GetPerTupleExprContext() macro,
378  * not directly.
379  */
380 ExprContext *
382 {
383  if (estate->es_per_tuple_exprcontext == NULL)
385 
386  return estate->es_per_tuple_exprcontext;
387 }
388 
389 
390 /* ----------------------------------------------------------------
391  * miscellaneous node-init support functions
392  *
393  * Note: all of these are expected to be called with CurrentMemoryContext
394  * equal to the per-query memory context.
395  * ----------------------------------------------------------------
396  */
397 
398 /* ----------------
399  * ExecAssignExprContext
400  *
401  * This initializes the ps_ExprContext field. It is only necessary
402  * to do this for nodes which use ExecQual or ExecProject
403  * because those routines require an econtext. Other nodes that
404  * don't have to evaluate expressions don't need to do this.
405  * ----------------
406  */
407 void
409 {
410  planstate->ps_ExprContext = CreateExprContext(estate);
411 }
412 
413 /* ----------------
414  * ExecAssignResultType
415  * ----------------
416  */
417 void
419 {
420  TupleTableSlot *slot = planstate->ps_ResultTupleSlot;
421 
422  ExecSetSlotDescriptor(slot, tupDesc);
423 }
424 
425 /* ----------------
426  * ExecAssignResultTypeFromTL
427  * ----------------
428  */
429 void
431 {
432  bool hasoid;
433  TupleDesc tupDesc;
434 
435  if (ExecContextForcesOids(planstate, &hasoid))
436  {
437  /* context forces OID choice; hasoid is now set correctly */
438  }
439  else
440  {
441  /* given free choice, don't leave space for OIDs in result tuples */
442  hasoid = false;
443  }
444 
445  /*
446  * ExecTypeFromTL needs the parse-time representation of the tlist, not a
447  * list of ExprStates. This is good because some plan nodes don't bother
448  * to set up planstate->targetlist ...
449  */
450  tupDesc = ExecTypeFromTL(planstate->plan->targetlist, hasoid);
451  ExecAssignResultType(planstate, tupDesc);
452 }
453 
454 /* ----------------
455  * ExecGetResultType
456  * ----------------
457  */
458 TupleDesc
460 {
461  TupleTableSlot *slot = planstate->ps_ResultTupleSlot;
462 
463  return slot->tts_tupleDescriptor;
464 }
465 
466 /* ----------------
467  * ExecBuildProjectionInfo
468  *
469  * Build a ProjectionInfo node for evaluating the given tlist in the given
470  * econtext, and storing the result into the tuple slot. (Caller must have
471  * ensured that tuple slot has a descriptor matching the tlist!) Note that
472  * the given tlist should be a list of ExprState nodes, not Expr nodes.
473  *
474  * inputDesc can be NULL, but if it is not, we check to see whether simple
475  * Vars in the tlist match the descriptor. It is important to provide
476  * inputDesc for relation-scan plan nodes, as a cross check that the relation
477  * hasn't been changed since the plan was made. At higher levels of a plan,
478  * there is no need to recheck.
479  * ----------------
480  */
483  ExprContext *econtext,
484  TupleTableSlot *slot,
485  TupleDesc inputDesc)
486 {
488  int len = ExecTargetListLength(targetList);
489  int *workspace;
490  int *varSlotOffsets;
491  int *varNumbers;
492  int *varOutputCols;
493  List *exprlist;
494  int numSimpleVars;
495  bool directMap;
496  ListCell *tl;
497 
498  projInfo->pi_exprContext = econtext;
499  projInfo->pi_slot = slot;
500  /* since these are all int arrays, we need do just one palloc */
501  workspace = (int *) palloc(len * 3 * sizeof(int));
502  projInfo->pi_varSlotOffsets = varSlotOffsets = workspace;
503  projInfo->pi_varNumbers = varNumbers = workspace + len;
504  projInfo->pi_varOutputCols = varOutputCols = workspace + len * 2;
505  projInfo->pi_lastInnerVar = 0;
506  projInfo->pi_lastOuterVar = 0;
507  projInfo->pi_lastScanVar = 0;
508 
509  /*
510  * We separate the target list elements into simple Var references and
511  * expressions which require the full ExecTargetList machinery. To be a
512  * simple Var, a Var has to be a user attribute and not mismatch the
513  * inputDesc. (Note: if there is a type mismatch then ExecEvalScalarVar
514  * will probably throw an error at runtime, but we leave that to it.)
515  */
516  exprlist = NIL;
517  numSimpleVars = 0;
518  directMap = true;
519  foreach(tl, targetList)
520  {
521  GenericExprState *gstate = (GenericExprState *) lfirst(tl);
522  Var *variable = (Var *) gstate->arg->expr;
523  bool isSimpleVar = false;
524 
525  if (variable != NULL &&
526  IsA(variable, Var) &&
527  variable->varattno > 0)
528  {
529  if (!inputDesc)
530  isSimpleVar = true; /* can't check type, assume OK */
531  else if (variable->varattno <= inputDesc->natts)
532  {
533  Form_pg_attribute attr;
534 
535  attr = inputDesc->attrs[variable->varattno - 1];
536  if (!attr->attisdropped && variable->vartype == attr->atttypid)
537  isSimpleVar = true;
538  }
539  }
540 
541  if (isSimpleVar)
542  {
543  TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
544  AttrNumber attnum = variable->varattno;
545 
546  varNumbers[numSimpleVars] = attnum;
547  varOutputCols[numSimpleVars] = tle->resno;
548  if (tle->resno != numSimpleVars + 1)
549  directMap = false;
550 
551  switch (variable->varno)
552  {
553  case INNER_VAR:
554  varSlotOffsets[numSimpleVars] = offsetof(ExprContext,
555  ecxt_innertuple);
556  if (projInfo->pi_lastInnerVar < attnum)
557  projInfo->pi_lastInnerVar = attnum;
558  break;
559 
560  case OUTER_VAR:
561  varSlotOffsets[numSimpleVars] = offsetof(ExprContext,
562  ecxt_outertuple);
563  if (projInfo->pi_lastOuterVar < attnum)
564  projInfo->pi_lastOuterVar = attnum;
565  break;
566 
567  /* INDEX_VAR is handled by default case */
568 
569  default:
570  varSlotOffsets[numSimpleVars] = offsetof(ExprContext,
571  ecxt_scantuple);
572  if (projInfo->pi_lastScanVar < attnum)
573  projInfo->pi_lastScanVar = attnum;
574  break;
575  }
576  numSimpleVars++;
577  }
578  else
579  {
580  /* Not a simple variable, add it to generic targetlist */
581  exprlist = lappend(exprlist, gstate);
582  /* Examine expr to include contained Vars in lastXXXVar counts */
583  get_last_attnums((Node *) variable, projInfo);
584  }
585  }
586  projInfo->pi_targetlist = exprlist;
587  projInfo->pi_numSimpleVars = numSimpleVars;
588  projInfo->pi_directMap = directMap;
589 
590  return projInfo;
591 }
592 
593 /*
594  * get_last_attnums: expression walker for ExecBuildProjectionInfo
595  *
596  * Update the lastXXXVar counts to be at least as large as the largest
597  * attribute numbers found in the expression
598  */
599 static bool
601 {
602  if (node == NULL)
603  return false;
604  if (IsA(node, Var))
605  {
606  Var *variable = (Var *) node;
607  AttrNumber attnum = variable->varattno;
608 
609  switch (variable->varno)
610  {
611  case INNER_VAR:
612  if (projInfo->pi_lastInnerVar < attnum)
613  projInfo->pi_lastInnerVar = attnum;
614  break;
615 
616  case OUTER_VAR:
617  if (projInfo->pi_lastOuterVar < attnum)
618  projInfo->pi_lastOuterVar = attnum;
619  break;
620 
621  /* INDEX_VAR is handled by default case */
622 
623  default:
624  if (projInfo->pi_lastScanVar < attnum)
625  projInfo->pi_lastScanVar = attnum;
626  break;
627  }
628  return false;
629  }
630 
631  /*
632  * Don't examine the arguments or filters of Aggrefs or WindowFuncs,
633  * because those do not represent expressions to be evaluated within the
634  * overall targetlist's econtext. GroupingFunc arguments are never
635  * evaluated at all.
636  */
637  if (IsA(node, Aggref))
638  return false;
639  if (IsA(node, WindowFunc))
640  return false;
641  if (IsA(node, GroupingFunc))
642  return false;
644  (void *) projInfo);
645 }
646 
647 /* ----------------
648  * ExecAssignProjectionInfo
649  *
650  * forms the projection information from the node's targetlist
651  *
652  * Notes for inputDesc are same as for ExecBuildProjectionInfo: supply it
653  * for a relation-scan node, can pass NULL for upper-level nodes
654  * ----------------
655  */
656 void
658  TupleDesc inputDesc)
659 {
660  planstate->ps_ProjInfo =
662  planstate->ps_ExprContext,
663  planstate->ps_ResultTupleSlot,
664  inputDesc);
665 }
666 
667 
668 /* ----------------
669  * ExecFreeExprContext
670  *
671  * A plan node's ExprContext should be freed explicitly during executor
672  * shutdown because there may be shutdown callbacks to call. (Other resources
673  * made by the above routines, such as projection info, don't need to be freed
674  * explicitly because they're just memory in the per-query memory context.)
675  *
676  * However ... there is no particular need to do it during ExecEndNode,
677  * because FreeExecutorState will free any remaining ExprContexts within
678  * the EState. Letting FreeExecutorState do it allows the ExprContexts to
679  * be freed in reverse order of creation, rather than order of creation as
680  * will happen if we delete them here, which saves O(N^2) work in the list
681  * cleanup inside FreeExprContext.
682  * ----------------
683  */
684 void
686 {
687  /*
688  * Per above discussion, don't actually delete the ExprContext. We do
689  * unlink it from the plan node, though.
690  */
691  planstate->ps_ExprContext = NULL;
692 }
693 
694 /* ----------------------------------------------------------------
695  * the following scan type support functions are for
696  * those nodes which are stubborn and return tuples in
697  * their Scan tuple slot instead of their Result tuple
698  * slot.. luck fur us, these nodes do not do projections
699  * so we don't have to worry about getting the ProjectionInfo
700  * right for them... -cim 6/3/91
701  * ----------------------------------------------------------------
702  */
703 
704 /* ----------------
705  * ExecAssignScanType
706  * ----------------
707  */
708 void
710 {
711  TupleTableSlot *slot = scanstate->ss_ScanTupleSlot;
712 
713  ExecSetSlotDescriptor(slot, tupDesc);
714 }
715 
716 /* ----------------
717  * ExecAssignScanTypeFromOuterPlan
718  * ----------------
719  */
720 void
722 {
724  TupleDesc tupDesc;
725 
726  outerPlan = outerPlanState(scanstate);
727  tupDesc = ExecGetResultType(outerPlan);
728 
729  ExecAssignScanType(scanstate, tupDesc);
730 }
731 
732 
733 /* ----------------------------------------------------------------
734  * Scan node support
735  * ----------------------------------------------------------------
736  */
737 
738 /* ----------------------------------------------------------------
739  * ExecRelationIsTargetRelation
740  *
741  * Detect whether a relation (identified by rangetable index)
742  * is one of the target relations of the query.
743  * ----------------------------------------------------------------
744  */
745 bool
747 {
748  ResultRelInfo *resultRelInfos;
749  int i;
750 
751  resultRelInfos = estate->es_result_relations;
752  for (i = 0; i < estate->es_num_result_relations; i++)
753  {
754  if (resultRelInfos[i].ri_RangeTableIndex == scanrelid)
755  return true;
756  }
757  return false;
758 }
759 
760 /* ----------------------------------------------------------------
761  * ExecOpenScanRelation
762  *
763  * Open the heap relation to be scanned by a base-level scan plan node.
764  * This should be called during the node's ExecInit routine.
765  *
766  * By default, this acquires AccessShareLock on the relation. However,
767  * if the relation was already locked by InitPlan, we don't need to acquire
768  * any additional lock. This saves trips to the shared lock manager.
769  * ----------------------------------------------------------------
770  */
771 Relation
772 ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
773 {
774  Relation rel;
775  Oid reloid;
776  LOCKMODE lockmode;
777 
778  /*
779  * Determine the lock type we need. First, scan to see if target relation
780  * is a result relation. If not, check if it's a FOR UPDATE/FOR SHARE
781  * relation. In either of those cases, we got the lock already.
782  */
783  lockmode = AccessShareLock;
784  if (ExecRelationIsTargetRelation(estate, scanrelid))
785  lockmode = NoLock;
786  else
787  {
788  /* Keep this check in sync with InitPlan! */
789  ExecRowMark *erm = ExecFindRowMark(estate, scanrelid, true);
790 
791  if (erm != NULL && erm->relation != NULL)
792  lockmode = NoLock;
793  }
794 
795  /* Open the relation and acquire lock as needed */
796  reloid = getrelid(scanrelid, estate->es_range_table);
797  rel = heap_open(reloid, lockmode);
798 
799  /*
800  * Complain if we're attempting a scan of an unscannable relation, except
801  * when the query won't actually be run. This is a slightly klugy place
802  * to do this, perhaps, but there is no better place.
803  */
804  if ((eflags & (EXEC_FLAG_EXPLAIN_ONLY | EXEC_FLAG_WITH_NO_DATA)) == 0 &&
805  !RelationIsScannable(rel))
806  ereport(ERROR,
807  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
808  errmsg("materialized view \"%s\" has not been populated",
810  errhint("Use the REFRESH MATERIALIZED VIEW command.")));
811 
812  return rel;
813 }
814 
815 /* ----------------------------------------------------------------
816  * ExecCloseScanRelation
817  *
818  * Close the heap relation scanned by a base-level scan plan node.
819  * This should be called during the node's ExecEnd routine.
820  *
821  * Currently, we do not release the lock acquired by ExecOpenScanRelation.
822  * This lock should be held till end of transaction. (There is a faction
823  * that considers this too much locking, however.)
824  *
825  * If we did want to release the lock, we'd have to repeat the logic in
826  * ExecOpenScanRelation in order to figure out what to release.
827  * ----------------------------------------------------------------
828  */
829 void
831 {
832  heap_close(scanrel, NoLock);
833 }
834 
835 /*
836  * UpdateChangedParamSet
837  * Add changed parameters to a plan node's chgParam set
838  */
839 void
841 {
842  Bitmapset *parmset;
843 
844  /*
845  * The plan node only depends on params listed in its allParam set. Don't
846  * include anything else into its chgParam set.
847  */
848  parmset = bms_intersect(node->plan->allParam, newchg);
849 
850  /*
851  * Keep node->chgParam == NULL if there's not actually any members; this
852  * allows the simplest possible tests in executor node files.
853  */
854  if (!bms_is_empty(parmset))
855  node->chgParam = bms_join(node->chgParam, parmset);
856  else
857  bms_free(parmset);
858 }
859 
860 /*
861  * Register a shutdown callback in an ExprContext.
862  *
863  * Shutdown callbacks will be called (in reverse order of registration)
864  * when the ExprContext is deleted or rescanned. This provides a hook
865  * for functions called in the context to do any cleanup needed --- it's
866  * particularly useful for functions returning sets. Note that the
867  * callback will *not* be called in the event that execution is aborted
868  * by an error.
869  */
870 void
873  Datum arg)
874 {
875  ExprContext_CB *ecxt_callback;
876 
877  /* Save the info in appropriate memory context */
878  ecxt_callback = (ExprContext_CB *)
880  sizeof(ExprContext_CB));
881 
882  ecxt_callback->function = function;
883  ecxt_callback->arg = arg;
884 
885  /* link to front of list for appropriate execution order */
886  ecxt_callback->next = econtext->ecxt_callbacks;
887  econtext->ecxt_callbacks = ecxt_callback;
888 }
889 
890 /*
891  * Deregister a shutdown callback in an ExprContext.
892  *
893  * Any list entries matching the function and arg will be removed.
894  * This can be used if it's no longer necessary to call the callback.
895  */
896 void
899  Datum arg)
900 {
901  ExprContext_CB **prev_callback;
902  ExprContext_CB *ecxt_callback;
903 
904  prev_callback = &econtext->ecxt_callbacks;
905 
906  while ((ecxt_callback = *prev_callback) != NULL)
907  {
908  if (ecxt_callback->function == function && ecxt_callback->arg == arg)
909  {
910  *prev_callback = ecxt_callback->next;
911  pfree(ecxt_callback);
912  }
913  else
914  prev_callback = &ecxt_callback->next;
915  }
916 }
917 
918 /*
919  * Call all the shutdown callbacks registered in an ExprContext.
920  *
921  * The callback list is emptied (important in case this is only a rescan
922  * reset, and not deletion of the ExprContext).
923  *
924  * If isCommit is false, just clean the callback list but don't call 'em.
925  * (See comment for FreeExprContext.)
926  */
927 static void
928 ShutdownExprContext(ExprContext *econtext, bool isCommit)
929 {
930  ExprContext_CB *ecxt_callback;
931  MemoryContext oldcontext;
932 
933  /* Fast path in normal case where there's nothing to do. */
934  if (econtext->ecxt_callbacks == NULL)
935  return;
936 
937  /*
938  * Call the callbacks in econtext's per-tuple context. This ensures that
939  * any memory they might leak will get cleaned up.
940  */
941  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
942 
943  /*
944  * Call each callback function in reverse registration order.
945  */
946  while ((ecxt_callback = econtext->ecxt_callbacks) != NULL)
947  {
948  econtext->ecxt_callbacks = ecxt_callback->next;
949  if (isCommit)
950  (*ecxt_callback->function) (ecxt_callback->arg);
951  pfree(ecxt_callback);
952  }
953 
954  MemoryContextSwitchTo(oldcontext);
955 }
#define NIL
Definition: pg_list.h:69
uint32 CommandId
Definition: c.h:408
ExprContext * CreateStandaloneExprContext(void)
Definition: execUtils.c:280
HeapTuple * es_epqTuple
Definition: execnodes.h:432
JunkFilter * es_junkFilter
Definition: execnodes.h:376
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
Datum * ecxt_aggvalues
Definition: execnodes.h:144
int errhint(const char *fmt,...)
Definition: elog.c:987
ExprState xprstate
Definition: execnodes.h:611
int ExecTargetListLength(List *targetlist)
Definition: execQual.c:5122
CommandId es_output_cid
Definition: execnodes.h:379
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1079
int LOCKMODE
Definition: lockdefs.h:26
TupleTableSlot * es_trig_newtup_slot
Definition: execnodes.h:390
Oid es_lastoid
Definition: execnodes.h:404
#define RelationIsScannable(relation)
Definition: rel.h:541
Relation relation
Definition: execnodes.h:460
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:897
ExprContext * ps_ExprContext
Definition: execnodes.h:1078
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:134
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
PlannedStmt * es_plannedstmt
Definition: execnodes.h:373
Definition: nodes.h:508
Snapshot es_crosscheck_snapshot
Definition: execnodes.h:371
int errcode(int sqlerrcode)
Definition: elog.c:575
ExprContext * es_per_tuple_exprcontext
Definition: execnodes.h:421
AttrNumber varattno
Definition: primnodes.h:146
List * targetlist
Definition: execnodes.h:1061
Snapshot es_snapshot
Definition: execnodes.h:370
#define EXEC_FLAG_WITH_NO_DATA
Definition: executor.h:65
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1291
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
#define heap_close(r, l)
Definition: heapam.h:97
int * pi_varSlotOffsets
Definition: execnodes.h:254
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
List * es_range_table
Definition: execnodes.h:372
unsigned int Oid
Definition: postgres_ext.h:31
Datum domainValue_datum
Definition: execnodes.h:152
Definition: primnodes.h:141
int natts
Definition: tupdesc.h:73
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:685
void ExecAssignResultType(PlanState *planstate, TupleDesc tupDesc)
Definition: execUtils.c:418
ScanDirection es_direction
Definition: execnodes.h:369
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:430
void FreeExecutorState(EState *estate)
Definition: execUtils.c:168
const char * es_sourceText
Definition: execnodes.h:374
int pi_lastScanVar
Definition: execnodes.h:259
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1077
ParamExecData * es_param_exec_vals
Definition: execnodes.h:394
void pfree(void *pointer)
Definition: mcxt.c:992
MemoryContext es_query_cxt
Definition: execnodes.h:397
ExprContextCallbackFunction function
Definition: execnodes.h:96
List * pi_targetlist
Definition: execnodes.h:249
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
Expr * expr
Definition: execnodes.h:598
Oid vartype
Definition: primnodes.h:148
Datum caseValue_datum
Definition: execnodes.h:148
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:808
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:772
ExprContext_CB * ecxt_callbacks
Definition: execnodes.h:159
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define outerPlanState(node)
Definition: execnodes.h:1090
#define NoLock
Definition: lockdefs.h:34
TupleTableSlot * pi_slot
Definition: execnodes.h:251
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:657
ResultRelInfo * es_result_relations
Definition: execnodes.h:382
AttrNumber resno
Definition: primnodes.h:1331
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, TupleDesc inputDesc)
Definition: execUtils.c:482
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:129
TupleTableSlot * es_trig_oldtup_slot
Definition: execnodes.h:389
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:137
#define RelationGetRelationName(relation)
Definition: rel.h:433
struct EState * ecxt_estate
Definition: execnodes.h:156
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
Bitmapset * allParam
Definition: plannodes.h:148
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
int es_instrument
Definition: execnodes.h:407
TupleTableSlot * es_trig_tuple_slot
Definition: execnodes.h:388
#define ereport(elevel, rest)
Definition: elog.h:122
EState * CreateExecutorState(void)
Definition: execUtils.c:72
int * pi_varNumbers
Definition: execnodes.h:255
Bitmapset * chgParam
Definition: execnodes.h:1072
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
struct ExprContext_CB * next
Definition: execnodes.h:95
#define outerPlan(node)
Definition: plannodes.h:159
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
static void ShutdownExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:928
Index varno
Definition: primnodes.h:144
ExprState * arg
Definition: execnodes.h:612
bool domainValue_isNull
Definition: execnodes.h:153
#define InvalidSnapshot
Definition: snapshot.h:25
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
bool * ecxt_aggnulls
Definition: execnodes.h:145
List * es_trig_target_relations
Definition: execnodes.h:387
List * es_tupleTable
Definition: execnodes.h:399
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
List * es_auxmodifytables
Definition: execnodes.h:414
uintptr_t Datum
Definition: postgres.h:374
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
void FreeExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:339
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
int es_num_result_relations
Definition: execnodes.h:383
unsigned int Index
Definition: c.h:362
Plan * plan
Definition: execnodes.h:1047
#define InvalidOid
Definition: postgres_ext.h:36
bool es_finished
Definition: execnodes.h:408
int * pi_varOutputCols
Definition: execnodes.h:256
static bool get_last_attnums(Node *node, ProjectionInfo *projInfo)
Definition: execUtils.c:600
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:840
#define INNER_VAR
Definition: primnodes.h:131
List * lcons(void *datum, List *list)
Definition: list.c:259
void bms_free(Bitmapset *a)
Definition: bitmapset.c:200
#define makeNode(_type_)
Definition: nodes.h:556
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:130
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:871
uint64 es_processed
Definition: execnodes.h:403
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:408
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1852
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:830
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:128
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:366
bool * es_epqTupleSet
Definition: execnodes.h:433
List * es_subplanstates
Definition: execnodes.h:412
List * es_rowMarks
Definition: execnodes.h:401
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:133
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:459
bool pi_directMap
Definition: execnodes.h:252
List * targetlist
Definition: plannodes.h:129
bool caseValue_isNull
Definition: execnodes.h:149
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
Definition: execUtils.c:721
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define getrelid(rangeindex, rangetable)
Definition: parsetree.h:41
int es_top_eflags
Definition: execnodes.h:406
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
ExprContext * MakePerTupleExprContext(EState *estate)
Definition: execUtils.c:381
int i
bool * es_epqScanDone
Definition: execnodes.h:434
int pi_lastInnerVar
Definition: execnodes.h:257
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:208
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:709
void * arg
ParamListInfo es_param_list_info
Definition: execnodes.h:393
List * es_exprcontexts
Definition: execnodes.h:410
int pi_lastOuterVar
Definition: execnodes.h:258
bool ExecContextForcesOids(PlanState *planstate, bool *hasoids)
Definition: execMain.c:1406
void(* ExprContextCallbackFunction)(Datum arg)
Definition: execnodes.h:91
ParamListInfo ecxt_param_list_info
Definition: execnodes.h:138
ExprContext * pi_exprContext
Definition: execnodes.h:250
Definition: pg_list.h:45
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:58
int16 AttrNumber
Definition: attnum.h:21
#define OUTER_VAR
Definition: primnodes.h:132
int pi_numSimpleVars
Definition: execnodes.h:253
bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid)
Definition: execUtils.c:746
#define offsetof(type, field)
Definition: c.h:551
ExecRowMark * ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
Definition: execMain.c:2208
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:384