PostgreSQL Source Code  git master
nodeSubplan.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeSubplan.c
4  * routines to support sub-selects appearing in expressions
5  *
6  * This module is concerned with executing SubPlan expression nodes, which
7  * should not be confused with sub-SELECTs appearing in FROM. SubPlans are
8  * divided into "initplans", which are those that need only one evaluation per
9  * query (among other restrictions, this requires that they don't use any
10  * direct correlation variables from the parent plan level), and "regular"
11  * subplans, which are re-evaluated every time their result is required.
12  *
13  *
14  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  * IDENTIFICATION
18  * src/backend/executor/nodeSubplan.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 /*
23  * INTERFACE ROUTINES
24  * ExecSubPlan - process a subselect
25  * ExecInitSubPlan - initialize a subselect
26  */
27 #include "postgres.h"
28 
29 #include <math.h>
30 
31 #include "access/htup_details.h"
32 #include "executor/executor.h"
33 #include "executor/nodeSubplan.h"
34 #include "miscadmin.h"
35 #include "nodes/makefuncs.h"
36 #include "nodes/nodeFuncs.h"
37 #include "optimizer/optimizer.h"
38 #include "utils/array.h"
39 #include "utils/lsyscache.h"
40 #include "utils/memutils.h"
41 
43  ExprContext *econtext,
44  bool *isNull);
46  ExprContext *econtext,
47  bool *isNull);
48 static void buildSubPlanHash(SubPlanState *node, ExprContext *econtext);
49 static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot,
50  FmgrInfo *eqfunctions);
51 static bool slotAllNulls(TupleTableSlot *slot);
52 static bool slotNoNulls(TupleTableSlot *slot);
53 
54 
55 /* ----------------------------------------------------------------
56  * ExecSubPlan
57  *
58  * This is the main entry point for execution of a regular SubPlan.
59  * ----------------------------------------------------------------
60  */
61 Datum
63  ExprContext *econtext,
64  bool *isNull)
65 {
66  SubPlan *subplan = node->subplan;
67  EState *estate = node->planstate->state;
68  ScanDirection dir = estate->es_direction;
69  Datum retval;
70 
72 
73  /* Set non-null as default */
74  *isNull = false;
75 
76  /* Sanity checks */
77  if (subplan->subLinkType == CTE_SUBLINK)
78  elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
79  if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
80  elog(ERROR, "cannot set parent params from subquery");
81 
82  /* Force forward-scan mode for evaluation */
84 
85  /* Select appropriate evaluation strategy */
86  if (subplan->useHashTable)
87  retval = ExecHashSubPlan(node, econtext, isNull);
88  else
89  retval = ExecScanSubPlan(node, econtext, isNull);
90 
91  /* restore scan direction */
92  estate->es_direction = dir;
93 
94  return retval;
95 }
96 
97 /*
98  * ExecHashSubPlan: store subselect result in an in-memory hash table
99  */
100 static Datum
102  ExprContext *econtext,
103  bool *isNull)
104 {
105  SubPlan *subplan = node->subplan;
106  PlanState *planstate = node->planstate;
107  TupleTableSlot *slot;
108 
109  /* Shouldn't have any direct correlation Vars */
110  if (subplan->parParam != NIL || node->args != NIL)
111  elog(ERROR, "hashed subplan with direct correlation not supported");
112 
113  /*
114  * If first time through or we need to rescan the subplan, build the hash
115  * table.
116  */
117  if (node->hashtable == NULL || planstate->chgParam != NULL)
118  buildSubPlanHash(node, econtext);
119 
120  /*
121  * The result for an empty subplan is always FALSE; no need to evaluate
122  * lefthand side.
123  */
124  *isNull = false;
125  if (!node->havehashrows && !node->havenullrows)
126  return BoolGetDatum(false);
127 
128  /*
129  * Evaluate lefthand expressions and form a projection tuple. First we
130  * have to set the econtext to use (hack alert!).
131  */
132  node->projLeft->pi_exprContext = econtext;
133  slot = ExecProject(node->projLeft);
134 
135  /*
136  * Note: because we are typically called in a per-tuple context, we have
137  * to explicitly clear the projected tuple before returning. Otherwise,
138  * we'll have a double-free situation: the per-tuple context will probably
139  * be reset before we're called again, and then the tuple slot will think
140  * it still needs to free the tuple.
141  */
142 
143  /*
144  * If the LHS is all non-null, probe for an exact match in the main hash
145  * table. If we find one, the result is TRUE. Otherwise, scan the
146  * partly-null table to see if there are any rows that aren't provably
147  * unequal to the LHS; if so, the result is UNKNOWN. (We skip that part
148  * if we don't care about UNKNOWN.) Otherwise, the result is FALSE.
149  *
150  * Note: the reason we can avoid a full scan of the main hash table is
151  * that the combining operators are assumed never to yield NULL when both
152  * inputs are non-null. If they were to do so, we might need to produce
153  * UNKNOWN instead of FALSE because of an UNKNOWN result in comparing the
154  * LHS to some main-table entry --- which is a comparison we will not even
155  * make, unless there's a chance match of hash keys.
156  */
157  if (slotNoNulls(slot))
158  {
159  if (node->havehashrows &&
161  slot,
162  node->cur_eq_comp,
163  node->lhs_hash_funcs) != NULL)
164  {
165  ExecClearTuple(slot);
166  return BoolGetDatum(true);
167  }
168  if (node->havenullrows &&
169  findPartialMatch(node->hashnulls, slot, node->cur_eq_funcs))
170  {
171  ExecClearTuple(slot);
172  *isNull = true;
173  return BoolGetDatum(false);
174  }
175  ExecClearTuple(slot);
176  return BoolGetDatum(false);
177  }
178 
179  /*
180  * When the LHS is partly or wholly NULL, we can never return TRUE. If we
181  * don't care about UNKNOWN, just return FALSE. Otherwise, if the LHS is
182  * wholly NULL, immediately return UNKNOWN. (Since the combining
183  * operators are strict, the result could only be FALSE if the sub-select
184  * were empty, but we already handled that case.) Otherwise, we must scan
185  * both the main and partly-null tables to see if there are any rows that
186  * aren't provably unequal to the LHS; if so, the result is UNKNOWN.
187  * Otherwise, the result is FALSE.
188  */
189  if (node->hashnulls == NULL)
190  {
191  ExecClearTuple(slot);
192  return BoolGetDatum(false);
193  }
194  if (slotAllNulls(slot))
195  {
196  ExecClearTuple(slot);
197  *isNull = true;
198  return BoolGetDatum(false);
199  }
200  /* Scan partly-null table first, since more likely to get a match */
201  if (node->havenullrows &&
202  findPartialMatch(node->hashnulls, slot, node->cur_eq_funcs))
203  {
204  ExecClearTuple(slot);
205  *isNull = true;
206  return BoolGetDatum(false);
207  }
208  if (node->havehashrows &&
209  findPartialMatch(node->hashtable, slot, node->cur_eq_funcs))
210  {
211  ExecClearTuple(slot);
212  *isNull = true;
213  return BoolGetDatum(false);
214  }
215  ExecClearTuple(slot);
216  return BoolGetDatum(false);
217 }
218 
219 /*
220  * ExecScanSubPlan: default case where we have to rescan subplan each time
221  */
222 static Datum
224  ExprContext *econtext,
225  bool *isNull)
226 {
227  SubPlan *subplan = node->subplan;
228  PlanState *planstate = node->planstate;
229  SubLinkType subLinkType = subplan->subLinkType;
230  MemoryContext oldcontext;
231  TupleTableSlot *slot;
232  Datum result;
233  bool found = false; /* true if got at least one subplan tuple */
234  ListCell *pvar;
235  ListCell *l;
236  ArrayBuildStateAny *astate = NULL;
237 
238  /*
239  * MULTIEXPR subplans, when "executed", just return NULL; but first we
240  * mark the subplan's output parameters as needing recalculation. (This
241  * is a bit of a hack: it relies on the subplan appearing later in its
242  * targetlist than any of the referencing Params, so that all the Params
243  * have been evaluated before we re-mark them for the next evaluation
244  * cycle. But in general resjunk tlist items appear after non-resjunk
245  * ones, so this should be safe.) Unlike ExecReScanSetParamPlan, we do
246  * *not* set bits in the parent plan node's chgParam, because we don't
247  * want to cause a rescan of the parent.
248  *
249  * Note: we are also relying on MULTIEXPR SubPlans not sharing any output
250  * parameters with other SubPlans, because if one does then it is unclear
251  * which SubPlanState node the parameter's execPlan field will be pointing
252  * to when we come to evaluate the parameter. We can allow plain initplan
253  * SubPlans to share output parameters, because it doesn't actually matter
254  * which initplan SubPlan we reference as long as they all point to the
255  * same underlying subplan. However, that fails to hold for MULTIEXPRs
256  * because they can have non-empty args lists, and the "same" args might
257  * have mutated into different forms in different parts of a plan tree.
258  * There is currently no problem because MULTIEXPR can appear only in an
259  * UPDATE's top-level target list, so it won't get duplicated anyplace.
260  * Postgres versions before v14 had to make concrete efforts to avoid
261  * sharing output parameters across different clones of a MULTIEXPR, and
262  * the problem could recur someday.
263  */
264  if (subLinkType == MULTIEXPR_SUBLINK)
265  {
266  EState *estate = node->parent->state;
267 
268  foreach(l, subplan->setParam)
269  {
270  int paramid = lfirst_int(l);
271  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
272 
273  prm->execPlan = node;
274  }
275  *isNull = true;
276  return (Datum) 0;
277  }
278 
279  /* Initialize ArrayBuildStateAny in caller's context, if needed */
280  if (subLinkType == ARRAY_SUBLINK)
281  astate = initArrayResultAny(subplan->firstColType,
282  CurrentMemoryContext, true);
283 
284  /*
285  * We are probably in a short-lived expression-evaluation context. Switch
286  * to the per-query context for manipulating the child plan's chgParam,
287  * calling ExecProcNode on it, etc.
288  */
289  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
290 
291  /*
292  * Set Params of this plan from parent plan correlation values. (Any
293  * calculation we have to do is done in the parent econtext, since the
294  * Param values don't need to have per-query lifetime.)
295  */
296  Assert(list_length(subplan->parParam) == list_length(node->args));
297 
298  forboth(l, subplan->parParam, pvar, node->args)
299  {
300  int paramid = lfirst_int(l);
301  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
302 
304  econtext,
305  &(prm->isnull));
306  planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
307  }
308 
309  /*
310  * Now that we've set up its parameters, we can reset the subplan.
311  */
312  ExecReScan(planstate);
313 
314  /*
315  * For all sublink types except EXPR_SUBLINK and ARRAY_SUBLINK, the result
316  * is boolean as are the results of the combining operators. We combine
317  * results across tuples (if the subplan produces more than one) using OR
318  * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
319  * (ROWCOMPARE_SUBLINK doesn't allow multiple tuples from the subplan.)
320  * NULL results from the combining operators are handled according to the
321  * usual SQL semantics for OR and AND. The result for no input tuples is
322  * FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for
323  * ROWCOMPARE_SUBLINK.
324  *
325  * For EXPR_SUBLINK we require the subplan to produce no more than one
326  * tuple, else an error is raised. If zero tuples are produced, we return
327  * NULL. Assuming we get a tuple, we just use its first column (there can
328  * be only one non-junk column in this case).
329  *
330  * For ARRAY_SUBLINK we allow the subplan to produce any number of tuples,
331  * and form an array of the first column's values. Note in particular
332  * that we produce a zero-element array if no tuples are produced (this is
333  * a change from pre-8.3 behavior of returning NULL).
334  */
335  result = BoolGetDatum(subLinkType == ALL_SUBLINK);
336  *isNull = false;
337 
338  for (slot = ExecProcNode(planstate);
339  !TupIsNull(slot);
340  slot = ExecProcNode(planstate))
341  {
342  TupleDesc tdesc = slot->tts_tupleDescriptor;
343  Datum rowresult;
344  bool rownull;
345  int col;
346  ListCell *plst;
347 
348  if (subLinkType == EXISTS_SUBLINK)
349  {
350  found = true;
351  result = BoolGetDatum(true);
352  break;
353  }
354 
355  if (subLinkType == EXPR_SUBLINK)
356  {
357  /* cannot allow multiple input tuples for EXPR sublink */
358  if (found)
359  ereport(ERROR,
360  (errcode(ERRCODE_CARDINALITY_VIOLATION),
361  errmsg("more than one row returned by a subquery used as an expression")));
362  found = true;
363 
364  /*
365  * We need to copy the subplan's tuple in case the result is of
366  * pass-by-ref type --- our return value will point into this
367  * copied tuple! Can't use the subplan's instance of the tuple
368  * since it won't still be valid after next ExecProcNode() call.
369  * node->curTuple keeps track of the copied tuple for eventual
370  * freeing.
371  */
372  if (node->curTuple)
373  heap_freetuple(node->curTuple);
374  node->curTuple = ExecCopySlotHeapTuple(slot);
375 
376  result = heap_getattr(node->curTuple, 1, tdesc, isNull);
377  /* keep scanning subplan to make sure there's only one tuple */
378  continue;
379  }
380 
381  if (subLinkType == ARRAY_SUBLINK)
382  {
383  Datum dvalue;
384  bool disnull;
385 
386  found = true;
387  /* stash away current value */
388  Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
389  dvalue = slot_getattr(slot, 1, &disnull);
390  astate = accumArrayResultAny(astate, dvalue, disnull,
391  subplan->firstColType, oldcontext);
392  /* keep scanning subplan to collect all values */
393  continue;
394  }
395 
396  /* cannot allow multiple input tuples for ROWCOMPARE sublink either */
397  if (subLinkType == ROWCOMPARE_SUBLINK && found)
398  ereport(ERROR,
399  (errcode(ERRCODE_CARDINALITY_VIOLATION),
400  errmsg("more than one row returned by a subquery used as an expression")));
401 
402  found = true;
403 
404  /*
405  * For ALL, ANY, and ROWCOMPARE sublinks, load up the Params
406  * representing the columns of the sub-select, and then evaluate the
407  * combining expression.
408  */
409  col = 1;
410  foreach(plst, subplan->paramIds)
411  {
412  int paramid = lfirst_int(plst);
413  ParamExecData *prmdata;
414 
415  prmdata = &(econtext->ecxt_param_exec_vals[paramid]);
416  Assert(prmdata->execPlan == NULL);
417  prmdata->value = slot_getattr(slot, col, &(prmdata->isnull));
418  col++;
419  }
420 
421  rowresult = ExecEvalExprSwitchContext(node->testexpr, econtext,
422  &rownull);
423 
424  if (subLinkType == ANY_SUBLINK)
425  {
426  /* combine across rows per OR semantics */
427  if (rownull)
428  *isNull = true;
429  else if (DatumGetBool(rowresult))
430  {
431  result = BoolGetDatum(true);
432  *isNull = false;
433  break; /* needn't look at any more rows */
434  }
435  }
436  else if (subLinkType == ALL_SUBLINK)
437  {
438  /* combine across rows per AND semantics */
439  if (rownull)
440  *isNull = true;
441  else if (!DatumGetBool(rowresult))
442  {
443  result = BoolGetDatum(false);
444  *isNull = false;
445  break; /* needn't look at any more rows */
446  }
447  }
448  else
449  {
450  /* must be ROWCOMPARE_SUBLINK */
451  result = rowresult;
452  *isNull = rownull;
453  }
454  }
455 
456  MemoryContextSwitchTo(oldcontext);
457 
458  if (subLinkType == ARRAY_SUBLINK)
459  {
460  /* We return the result in the caller's context */
461  result = makeArrayResultAny(astate, oldcontext, true);
462  }
463  else if (!found)
464  {
465  /*
466  * deal with empty subplan result. result/isNull were previously
467  * initialized correctly for all sublink types except EXPR and
468  * ROWCOMPARE; for those, return NULL.
469  */
470  if (subLinkType == EXPR_SUBLINK ||
471  subLinkType == ROWCOMPARE_SUBLINK)
472  {
473  result = (Datum) 0;
474  *isNull = true;
475  }
476  }
477 
478  return result;
479 }
480 
481 /*
482  * buildSubPlanHash: load hash table by scanning subplan output.
483  */
484 static void
486 {
487  SubPlan *subplan = node->subplan;
488  PlanState *planstate = node->planstate;
489  int ncols = node->numCols;
490  ExprContext *innerecontext = node->innerecontext;
491  MemoryContext oldcontext;
492  long nbuckets;
493  TupleTableSlot *slot;
494 
495  Assert(subplan->subLinkType == ANY_SUBLINK);
496 
497  /*
498  * If we already had any hash tables, reset 'em; otherwise create empty
499  * hash table(s).
500  *
501  * If we need to distinguish accurately between FALSE and UNKNOWN (i.e.,
502  * NULL) results of the IN operation, then we have to store subplan output
503  * rows that are partly or wholly NULL. We store such rows in a separate
504  * hash table that we expect will be much smaller than the main table. (We
505  * can use hashing to eliminate partly-null rows that are not distinct. We
506  * keep them separate to minimize the cost of the inevitable full-table
507  * searches; see findPartialMatch.)
508  *
509  * If it's not necessary to distinguish FALSE and UNKNOWN, then we don't
510  * need to store subplan output rows that contain NULL.
511  */
513  node->havehashrows = false;
514  node->havenullrows = false;
515 
516  nbuckets = clamp_cardinality_to_long(planstate->plan->plan_rows);
517  if (nbuckets < 1)
518  nbuckets = 1;
519 
520  if (node->hashtable)
522  else
524  node->descRight,
525  ncols,
526  node->keyColIdx,
527  node->tab_eq_funcoids,
528  node->tab_hash_funcs,
529  node->tab_collations,
530  nbuckets,
531  0,
532  node->planstate->state->es_query_cxt,
533  node->hashtablecxt,
534  node->hashtempcxt,
535  false);
536 
537  if (!subplan->unknownEqFalse)
538  {
539  if (ncols == 1)
540  nbuckets = 1; /* there can only be one entry */
541  else
542  {
543  nbuckets /= 16;
544  if (nbuckets < 1)
545  nbuckets = 1;
546  }
547 
548  if (node->hashnulls)
550  else
552  node->descRight,
553  ncols,
554  node->keyColIdx,
555  node->tab_eq_funcoids,
556  node->tab_hash_funcs,
557  node->tab_collations,
558  nbuckets,
559  0,
560  node->planstate->state->es_query_cxt,
561  node->hashtablecxt,
562  node->hashtempcxt,
563  false);
564  }
565  else
566  node->hashnulls = NULL;
567 
568  /*
569  * We are probably in a short-lived expression-evaluation context. Switch
570  * to the per-query context for manipulating the child plan.
571  */
572  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
573 
574  /*
575  * Reset subplan to start.
576  */
577  ExecReScan(planstate);
578 
579  /*
580  * Scan the subplan and load the hash table(s). Note that when there are
581  * duplicate rows coming out of the sub-select, only one copy is stored.
582  */
583  for (slot = ExecProcNode(planstate);
584  !TupIsNull(slot);
585  slot = ExecProcNode(planstate))
586  {
587  int col = 1;
588  ListCell *plst;
589  bool isnew;
590 
591  /*
592  * Load up the Params representing the raw sub-select outputs, then
593  * form the projection tuple to store in the hashtable.
594  */
595  foreach(plst, subplan->paramIds)
596  {
597  int paramid = lfirst_int(plst);
598  ParamExecData *prmdata;
599 
600  prmdata = &(innerecontext->ecxt_param_exec_vals[paramid]);
601  Assert(prmdata->execPlan == NULL);
602  prmdata->value = slot_getattr(slot, col,
603  &(prmdata->isnull));
604  col++;
605  }
606  slot = ExecProject(node->projRight);
607 
608  /*
609  * If result contains any nulls, store separately or not at all.
610  */
611  if (slotNoNulls(slot))
612  {
613  (void) LookupTupleHashEntry(node->hashtable, slot, &isnew, NULL);
614  node->havehashrows = true;
615  }
616  else if (node->hashnulls)
617  {
618  (void) LookupTupleHashEntry(node->hashnulls, slot, &isnew, NULL);
619  node->havenullrows = true;
620  }
621 
622  /*
623  * Reset innerecontext after each inner tuple to free any memory used
624  * during ExecProject.
625  */
626  ResetExprContext(innerecontext);
627  }
628 
629  /*
630  * Since the projected tuples are in the sub-query's context and not the
631  * main context, we'd better clear the tuple slot before there's any
632  * chance of a reset of the sub-query's context. Else we will have the
633  * potential for a double free attempt. (XXX possibly no longer needed,
634  * but can't hurt.)
635  */
637 
638  MemoryContextSwitchTo(oldcontext);
639 }
640 
641 /*
642  * execTuplesUnequal
643  * Return true if two tuples are definitely unequal in the indicated
644  * fields.
645  *
646  * Nulls are neither equal nor unequal to anything else. A true result
647  * is obtained only if there are non-null fields that compare not-equal.
648  *
649  * slot1, slot2: the tuples to compare (must have same columns!)
650  * numCols: the number of attributes to be examined
651  * matchColIdx: array of attribute column numbers
652  * eqFunctions: array of fmgr lookup info for the equality functions to use
653  * evalContext: short-term memory context for executing the functions
654  */
655 static bool
657  TupleTableSlot *slot2,
658  int numCols,
659  AttrNumber *matchColIdx,
660  FmgrInfo *eqfunctions,
661  const Oid *collations,
662  MemoryContext evalContext)
663 {
664  MemoryContext oldContext;
665  bool result;
666  int i;
667 
668  /* Reset and switch into the temp context. */
669  MemoryContextReset(evalContext);
670  oldContext = MemoryContextSwitchTo(evalContext);
671 
672  /*
673  * We cannot report a match without checking all the fields, but we can
674  * report a non-match as soon as we find unequal fields. So, start
675  * comparing at the last field (least significant sort key). That's the
676  * most likely to be different if we are dealing with sorted input.
677  */
678  result = false;
679 
680  for (i = numCols; --i >= 0;)
681  {
682  AttrNumber att = matchColIdx[i];
683  Datum attr1,
684  attr2;
685  bool isNull1,
686  isNull2;
687 
688  attr1 = slot_getattr(slot1, att, &isNull1);
689 
690  if (isNull1)
691  continue; /* can't prove anything here */
692 
693  attr2 = slot_getattr(slot2, att, &isNull2);
694 
695  if (isNull2)
696  continue; /* can't prove anything here */
697 
698  /* Apply the type-specific equality function */
699  if (!DatumGetBool(FunctionCall2Coll(&eqfunctions[i],
700  collations[i],
701  attr1, attr2)))
702  {
703  result = true; /* they are unequal */
704  break;
705  }
706  }
707 
708  MemoryContextSwitchTo(oldContext);
709 
710  return result;
711 }
712 
713 /*
714  * findPartialMatch: does the hashtable contain an entry that is not
715  * provably distinct from the tuple?
716  *
717  * We have to scan the whole hashtable; we can't usefully use hashkeys
718  * to guide probing, since we might get partial matches on tuples with
719  * hashkeys quite unrelated to what we'd get from the given tuple.
720  *
721  * Caller must provide the equality functions to use, since in cross-type
722  * cases these are different from the hashtable's internal functions.
723  */
724 static bool
726  FmgrInfo *eqfunctions)
727 {
728  int numCols = hashtable->numCols;
729  AttrNumber *keyColIdx = hashtable->keyColIdx;
730  TupleHashIterator hashiter;
731  TupleHashEntry entry;
732 
733  InitTupleHashIterator(hashtable, &hashiter);
734  while ((entry = ScanTupleHashTable(hashtable, &hashiter)) != NULL)
735  {
737 
738  ExecStoreMinimalTuple(entry->firstTuple, hashtable->tableslot, false);
739  if (!execTuplesUnequal(slot, hashtable->tableslot,
740  numCols, keyColIdx,
741  eqfunctions,
742  hashtable->tab_collations,
743  hashtable->tempcxt))
744  {
745  TermTupleHashIterator(&hashiter);
746  return true;
747  }
748  }
749  /* No TermTupleHashIterator call needed here */
750  return false;
751 }
752 
753 /*
754  * slotAllNulls: is the slot completely NULL?
755  *
756  * This does not test for dropped columns, which is OK because we only
757  * use it on projected tuples.
758  */
759 static bool
761 {
762  int ncols = slot->tts_tupleDescriptor->natts;
763  int i;
764 
765  for (i = 1; i <= ncols; i++)
766  {
767  if (!slot_attisnull(slot, i))
768  return false;
769  }
770  return true;
771 }
772 
773 /*
774  * slotNoNulls: is the slot entirely not NULL?
775  *
776  * This does not test for dropped columns, which is OK because we only
777  * use it on projected tuples.
778  */
779 static bool
781 {
782  int ncols = slot->tts_tupleDescriptor->natts;
783  int i;
784 
785  for (i = 1; i <= ncols; i++)
786  {
787  if (slot_attisnull(slot, i))
788  return false;
789  }
790  return true;
791 }
792 
793 /* ----------------------------------------------------------------
794  * ExecInitSubPlan
795  *
796  * Create a SubPlanState for a SubPlan; this is the SubPlan-specific part
797  * of ExecInitExpr(). We split it out so that it can be used for InitPlans
798  * as well as regular SubPlans. Note that we don't link the SubPlan into
799  * the parent's subPlan list, because that shouldn't happen for InitPlans.
800  * Instead, ExecInitExpr() does that one part.
801  * ----------------------------------------------------------------
802  */
803 SubPlanState *
805 {
807  EState *estate = parent->state;
808 
809  sstate->subplan = subplan;
810 
811  /* Link the SubPlanState to already-initialized subplan */
812  sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
813  subplan->plan_id - 1);
814 
815  /*
816  * This check can fail if the planner mistakenly puts a parallel-unsafe
817  * subplan into a parallelized subquery; see ExecSerializePlan.
818  */
819  if (sstate->planstate == NULL)
820  elog(ERROR, "subplan \"%s\" was not initialized",
821  subplan->plan_name);
822 
823  /* Link to parent's state, too */
824  sstate->parent = parent;
825 
826  /* Initialize subexpressions */
827  sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
828  sstate->args = ExecInitExprList(subplan->args, parent);
829 
830  /*
831  * initialize my state
832  */
833  sstate->curTuple = NULL;
834  sstate->curArray = PointerGetDatum(NULL);
835  sstate->projLeft = NULL;
836  sstate->projRight = NULL;
837  sstate->hashtable = NULL;
838  sstate->hashnulls = NULL;
839  sstate->hashtablecxt = NULL;
840  sstate->hashtempcxt = NULL;
841  sstate->innerecontext = NULL;
842  sstate->keyColIdx = NULL;
843  sstate->tab_eq_funcoids = NULL;
844  sstate->tab_hash_funcs = NULL;
845  sstate->tab_eq_funcs = NULL;
846  sstate->tab_collations = NULL;
847  sstate->lhs_hash_funcs = NULL;
848  sstate->cur_eq_funcs = NULL;
849 
850  /*
851  * If this is an initplan or MULTIEXPR subplan, it has output parameters
852  * that the parent plan will use, so mark those parameters as needing
853  * evaluation. We don't actually run the subplan until we first need one
854  * of its outputs.
855  *
856  * A CTE subplan's output parameter is never to be evaluated in the normal
857  * way, so skip this in that case.
858  *
859  * Note that we don't set parent->chgParam here: the parent plan hasn't
860  * been run yet, so no need to force it to re-run.
861  */
862  if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
863  {
864  ListCell *lst;
865 
866  foreach(lst, subplan->setParam)
867  {
868  int paramid = lfirst_int(lst);
869  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
870 
871  prm->execPlan = sstate;
872  }
873  }
874 
875  /*
876  * If we are going to hash the subquery output, initialize relevant stuff.
877  * (We don't create the hashtable until needed, though.)
878  */
879  if (subplan->useHashTable)
880  {
881  int ncols,
882  i;
883  TupleDesc tupDescLeft;
884  TupleDesc tupDescRight;
885  Oid *cross_eq_funcoids;
886  TupleTableSlot *slot;
887  List *oplist,
888  *lefttlist,
889  *righttlist;
890  ListCell *l;
891 
892  /* We need a memory context to hold the hash table(s) */
893  sstate->hashtablecxt =
895  "Subplan HashTable Context",
897  /* and a small one for the hash tables to use as temp storage */
898  sstate->hashtempcxt =
900  "Subplan HashTable Temp Context",
902  /* and a short-lived exprcontext for function evaluation */
903  sstate->innerecontext = CreateExprContext(estate);
904 
905  /*
906  * We use ExecProject to evaluate the lefthand and righthand
907  * expression lists and form tuples. (You might think that we could
908  * use the sub-select's output tuples directly, but that is not the
909  * case if we had to insert any run-time coercions of the sub-select's
910  * output datatypes; anyway this avoids storing any resjunk columns
911  * that might be in the sub-select's output.) Run through the
912  * combining expressions to build tlists for the lefthand and
913  * righthand sides.
914  *
915  * We also extract the combining operators themselves to initialize
916  * the equality and hashing functions for the hash tables.
917  */
918  if (IsA(subplan->testexpr, OpExpr))
919  {
920  /* single combining operator */
921  oplist = list_make1(subplan->testexpr);
922  }
923  else if (is_andclause(subplan->testexpr))
924  {
925  /* multiple combining operators */
926  oplist = castNode(BoolExpr, subplan->testexpr)->args;
927  }
928  else
929  {
930  /* shouldn't see anything else in a hashable subplan */
931  elog(ERROR, "unrecognized testexpr type: %d",
932  (int) nodeTag(subplan->testexpr));
933  oplist = NIL; /* keep compiler quiet */
934  }
935  ncols = list_length(oplist);
936 
937  lefttlist = righttlist = NIL;
938  sstate->numCols = ncols;
939  sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
940  sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
941  sstate->tab_collations = (Oid *) palloc(ncols * sizeof(Oid));
942  sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
943  sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
944  sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
945  sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
946  /* we'll need the cross-type equality fns below, but not in sstate */
947  cross_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
948 
949  i = 1;
950  foreach(l, oplist)
951  {
952  OpExpr *opexpr = lfirst_node(OpExpr, l);
953  Expr *expr;
954  TargetEntry *tle;
955  Oid rhs_eq_oper;
956  Oid left_hashfn;
957  Oid right_hashfn;
958 
959  Assert(list_length(opexpr->args) == 2);
960 
961  /* Process lefthand argument */
962  expr = (Expr *) linitial(opexpr->args);
963  tle = makeTargetEntry(expr,
964  i,
965  NULL,
966  false);
967  lefttlist = lappend(lefttlist, tle);
968 
969  /* Process righthand argument */
970  expr = (Expr *) lsecond(opexpr->args);
971  tle = makeTargetEntry(expr,
972  i,
973  NULL,
974  false);
975  righttlist = lappend(righttlist, tle);
976 
977  /* Lookup the equality function (potentially cross-type) */
978  cross_eq_funcoids[i - 1] = opexpr->opfuncid;
979  fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
980  fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
981 
982  /* Look up the equality function for the RHS type */
983  if (!get_compatible_hash_operators(opexpr->opno,
984  NULL, &rhs_eq_oper))
985  elog(ERROR, "could not find compatible hash operator for operator %u",
986  opexpr->opno);
987  sstate->tab_eq_funcoids[i - 1] = get_opcode(rhs_eq_oper);
988  fmgr_info(sstate->tab_eq_funcoids[i - 1],
989  &sstate->tab_eq_funcs[i - 1]);
990 
991  /* Lookup the associated hash functions */
992  if (!get_op_hash_functions(opexpr->opno,
993  &left_hashfn, &right_hashfn))
994  elog(ERROR, "could not find hash function for hash operator %u",
995  opexpr->opno);
996  fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
997  fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
998 
999  /* Set collation */
1000  sstate->tab_collations[i - 1] = opexpr->inputcollid;
1001 
1002  /* keyColIdx is just column numbers 1..n */
1003  sstate->keyColIdx[i - 1] = i;
1004 
1005  i++;
1006  }
1007 
1008  /*
1009  * Construct tupdescs, slots and projection nodes for left and right
1010  * sides. The lefthand expressions will be evaluated in the parent
1011  * plan node's exprcontext, which we don't have access to here.
1012  * Fortunately we can just pass NULL for now and fill it in later
1013  * (hack alert!). The righthand expressions will be evaluated in our
1014  * own innerecontext.
1015  */
1016  tupDescLeft = ExecTypeFromTL(lefttlist);
1017  slot = ExecInitExtraTupleSlot(estate, tupDescLeft, &TTSOpsVirtual);
1018  sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
1019  NULL,
1020  slot,
1021  parent,
1022  NULL);
1023 
1024  sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist);
1025  slot = ExecInitExtraTupleSlot(estate, tupDescRight, &TTSOpsVirtual);
1026  sstate->projRight = ExecBuildProjectionInfo(righttlist,
1027  sstate->innerecontext,
1028  slot,
1029  sstate->planstate,
1030  NULL);
1031 
1032  /*
1033  * Create comparator for lookups of rows in the table (potentially
1034  * cross-type comparisons).
1035  */
1036  sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight,
1038  ncols,
1039  sstate->keyColIdx,
1040  cross_eq_funcoids,
1041  sstate->tab_collations,
1042  parent);
1043  }
1044 
1045  return sstate;
1046 }
1047 
1048 /* ----------------------------------------------------------------
1049  * ExecSetParamPlan
1050  *
1051  * Executes a subplan and sets its output parameters.
1052  *
1053  * This is called from ExecEvalParamExec() when the value of a PARAM_EXEC
1054  * parameter is requested and the param's execPlan field is set (indicating
1055  * that the param has not yet been evaluated). This allows lazy evaluation
1056  * of initplans: we don't run the subplan until/unless we need its output.
1057  * Note that this routine MUST clear the execPlan fields of the plan's
1058  * output parameters after evaluating them!
1059  *
1060  * The results of this function are stored in the EState associated with the
1061  * ExprContext (particularly, its ecxt_param_exec_vals); any pass-by-ref
1062  * result Datums are allocated in the EState's per-query memory. The passed
1063  * econtext can be any ExprContext belonging to that EState; which one is
1064  * important only to the extent that the ExprContext's per-tuple memory
1065  * context is used to evaluate any parameters passed down to the subplan.
1066  * (Thus in principle, the shorter-lived the ExprContext the better, since
1067  * that data isn't needed after we return. In practice, because initplan
1068  * parameters are never more complex than Vars, Aggrefs, etc, evaluating them
1069  * currently never leaks any memory anyway.)
1070  * ----------------------------------------------------------------
1071  */
1072 void
1074 {
1075  SubPlan *subplan = node->subplan;
1076  PlanState *planstate = node->planstate;
1077  SubLinkType subLinkType = subplan->subLinkType;
1078  EState *estate = planstate->state;
1079  ScanDirection dir = estate->es_direction;
1080  MemoryContext oldcontext;
1081  TupleTableSlot *slot;
1082  ListCell *pvar;
1083  ListCell *l;
1084  bool found = false;
1085  ArrayBuildStateAny *astate = NULL;
1086 
1087  if (subLinkType == ANY_SUBLINK ||
1088  subLinkType == ALL_SUBLINK)
1089  elog(ERROR, "ANY/ALL subselect unsupported as initplan");
1090  if (subLinkType == CTE_SUBLINK)
1091  elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
1092 
1093  /*
1094  * Enforce forward scan direction regardless of caller. It's hard but not
1095  * impossible to get here in backward scan, so make it work anyway.
1096  */
1098 
1099  /* Initialize ArrayBuildStateAny in caller's context, if needed */
1100  if (subLinkType == ARRAY_SUBLINK)
1101  astate = initArrayResultAny(subplan->firstColType,
1102  CurrentMemoryContext, true);
1103 
1104  /*
1105  * Must switch to per-query memory context.
1106  */
1107  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1108 
1109  /*
1110  * Set Params of this plan from parent plan correlation values. (Any
1111  * calculation we have to do is done in the parent econtext, since the
1112  * Param values don't need to have per-query lifetime.) Currently, we
1113  * expect only MULTIEXPR_SUBLINK plans to have any correlation values.
1114  */
1115  Assert(subplan->parParam == NIL || subLinkType == MULTIEXPR_SUBLINK);
1116  Assert(list_length(subplan->parParam) == list_length(node->args));
1117 
1118  forboth(l, subplan->parParam, pvar, node->args)
1119  {
1120  int paramid = lfirst_int(l);
1121  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1122 
1124  econtext,
1125  &(prm->isnull));
1126  planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
1127  }
1128 
1129  /*
1130  * Run the plan. (If it needs to be rescanned, the first ExecProcNode
1131  * call will take care of that.)
1132  */
1133  for (slot = ExecProcNode(planstate);
1134  !TupIsNull(slot);
1135  slot = ExecProcNode(planstate))
1136  {
1137  TupleDesc tdesc = slot->tts_tupleDescriptor;
1138  int i = 1;
1139 
1140  if (subLinkType == EXISTS_SUBLINK)
1141  {
1142  /* There can be only one setParam... */
1143  int paramid = linitial_int(subplan->setParam);
1144  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1145 
1146  prm->execPlan = NULL;
1147  prm->value = BoolGetDatum(true);
1148  prm->isnull = false;
1149  found = true;
1150  break;
1151  }
1152 
1153  if (subLinkType == ARRAY_SUBLINK)
1154  {
1155  Datum dvalue;
1156  bool disnull;
1157 
1158  found = true;
1159  /* stash away current value */
1160  Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
1161  dvalue = slot_getattr(slot, 1, &disnull);
1162  astate = accumArrayResultAny(astate, dvalue, disnull,
1163  subplan->firstColType, oldcontext);
1164  /* keep scanning subplan to collect all values */
1165  continue;
1166  }
1167 
1168  if (found &&
1169  (subLinkType == EXPR_SUBLINK ||
1170  subLinkType == MULTIEXPR_SUBLINK ||
1171  subLinkType == ROWCOMPARE_SUBLINK))
1172  ereport(ERROR,
1173  (errcode(ERRCODE_CARDINALITY_VIOLATION),
1174  errmsg("more than one row returned by a subquery used as an expression")));
1175 
1176  found = true;
1177 
1178  /*
1179  * We need to copy the subplan's tuple into our own context, in case
1180  * any of the params are pass-by-ref type --- the pointers stored in
1181  * the param structs will point at this copied tuple! node->curTuple
1182  * keeps track of the copied tuple for eventual freeing.
1183  */
1184  if (node->curTuple)
1185  heap_freetuple(node->curTuple);
1186  node->curTuple = ExecCopySlotHeapTuple(slot);
1187 
1188  /*
1189  * Now set all the setParam params from the columns of the tuple
1190  */
1191  foreach(l, subplan->setParam)
1192  {
1193  int paramid = lfirst_int(l);
1194  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1195 
1196  prm->execPlan = NULL;
1197  prm->value = heap_getattr(node->curTuple, i, tdesc,
1198  &(prm->isnull));
1199  i++;
1200  }
1201  }
1202 
1203  if (subLinkType == ARRAY_SUBLINK)
1204  {
1205  /* There can be only one setParam... */
1206  int paramid = linitial_int(subplan->setParam);
1207  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1208 
1209  /*
1210  * We build the result array in query context so it won't disappear;
1211  * to avoid leaking memory across repeated calls, we have to remember
1212  * the latest value, much as for curTuple above.
1213  */
1214  if (node->curArray != PointerGetDatum(NULL))
1215  pfree(DatumGetPointer(node->curArray));
1216  node->curArray = makeArrayResultAny(astate,
1217  econtext->ecxt_per_query_memory,
1218  true);
1219  prm->execPlan = NULL;
1220  prm->value = node->curArray;
1221  prm->isnull = false;
1222  }
1223  else if (!found)
1224  {
1225  if (subLinkType == EXISTS_SUBLINK)
1226  {
1227  /* There can be only one setParam... */
1228  int paramid = linitial_int(subplan->setParam);
1229  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1230 
1231  prm->execPlan = NULL;
1232  prm->value = BoolGetDatum(false);
1233  prm->isnull = false;
1234  }
1235  else
1236  {
1237  /* For other sublink types, set all the output params to NULL */
1238  foreach(l, subplan->setParam)
1239  {
1240  int paramid = lfirst_int(l);
1241  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1242 
1243  prm->execPlan = NULL;
1244  prm->value = (Datum) 0;
1245  prm->isnull = true;
1246  }
1247  }
1248  }
1249 
1250  MemoryContextSwitchTo(oldcontext);
1251 
1252  /* restore scan direction */
1253  estate->es_direction = dir;
1254 }
1255 
1256 /*
1257  * ExecSetParamPlanMulti
1258  *
1259  * Apply ExecSetParamPlan to evaluate any not-yet-evaluated initplan output
1260  * parameters whose ParamIDs are listed in "params". Any listed params that
1261  * are not initplan outputs are ignored.
1262  *
1263  * As with ExecSetParamPlan, any ExprContext belonging to the current EState
1264  * can be used, but in principle a shorter-lived ExprContext is better than a
1265  * longer-lived one.
1266  */
1267 void
1269 {
1270  int paramid;
1271 
1272  paramid = -1;
1273  while ((paramid = bms_next_member(params, paramid)) >= 0)
1274  {
1275  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1276 
1277  if (prm->execPlan != NULL)
1278  {
1279  /* Parameter not evaluated yet, so go do it */
1280  ExecSetParamPlan(prm->execPlan, econtext);
1281  /* ExecSetParamPlan should have processed this param... */
1282  Assert(prm->execPlan == NULL);
1283  }
1284  }
1285 }
1286 
1287 /*
1288  * Mark an initplan as needing recalculation
1289  */
1290 void
1292 {
1293  PlanState *planstate = node->planstate;
1294  SubPlan *subplan = node->subplan;
1295  EState *estate = parent->state;
1296  ListCell *l;
1297 
1298  /* sanity checks */
1299  if (subplan->parParam != NIL)
1300  elog(ERROR, "direct correlated subquery unsupported as initplan");
1301  if (subplan->setParam == NIL)
1302  elog(ERROR, "setParam list of initplan is empty");
1303  if (bms_is_empty(planstate->plan->extParam))
1304  elog(ERROR, "extParam set of initplan is empty");
1305 
1306  /*
1307  * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1308  */
1309 
1310  /*
1311  * Mark this subplan's output parameters as needing recalculation.
1312  *
1313  * CTE subplans are never executed via parameter recalculation; instead
1314  * they get run when called by nodeCtescan.c. So don't mark the output
1315  * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1316  * so that dependent plan nodes will get told to rescan.
1317  */
1318  foreach(l, subplan->setParam)
1319  {
1320  int paramid = lfirst_int(l);
1321  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1322 
1323  if (subplan->subLinkType != CTE_SUBLINK)
1324  prm->execPlan = node;
1325 
1326  parent->chgParam = bms_add_member(parent->chgParam, paramid);
1327  }
1328 }
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5703
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5748
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5776
int16 AttrNumber
Definition: attnum.h:21
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1047
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
long clamp_cardinality_to_long(Cardinality x)
Definition: costsize.c:224
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:319
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:124
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:354
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, int numCols, const AttrNumber *keyColIdx, const Oid *eqfunctions, const Oid *collations, PlanState *parent)
Definition: execExpr.c:3719
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 *hash)
Definition: execGrouping.c:306
TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, ExprState *eqcomp, FmgrInfo *hashfunctions)
Definition: execGrouping.c:393
TupleHashTable BuildTupleHashTableExt(PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, long nbuckets, Size additionalsize, MemoryContext metacxt, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:154
void ResetTupleHashTable(TupleHashTable hashtable)
Definition: execGrouping.c:285
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1446
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1831
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1938
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:302
#define ScanTupleHashTable(htable, iter)
Definition: execnodes.h:827
#define TermTupleHashIterator(iter)
Definition: execnodes.h:823
#define InitTupleHashIterator(htable, iter)
Definition: execnodes.h:821
tuplehash_iterator TupleHashIterator
Definition: execnodes.h:814
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:361
#define ResetExprContext(econtext)
Definition: executor.h:529
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:333
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:254
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1134
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:788
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
bool get_compatible_hash_operators(Oid opno, Oid *lhs_opno, Oid *rhs_opno)
Definition: lsyscache.c:409
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1267
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:509
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:303
void pfree(void *pointer)
Definition: mcxt.c:1306
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
void * palloc(Size size)
Definition: mcxt.c:1199
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:101
static void buildSubPlanHash(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:485
static bool execTuplesUnequal(TupleTableSlot *slot1, TupleTableSlot *slot2, int numCols, AttrNumber *matchColIdx, FmgrInfo *eqfunctions, const Oid *collations, MemoryContext evalContext)
Definition: nodeSubplan.c:656
void ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
Definition: nodeSubplan.c:1291
static bool slotNoNulls(TupleTableSlot *slot)
Definition: nodeSubplan.c:780
static Datum ExecScanSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:223
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1073
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1268
SubPlanState * ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
Definition: nodeSubplan.c:804
static bool slotAllNulls(TupleTableSlot *slot)
Definition: nodeSubplan.c:760
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:62
static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot, FmgrInfo *eqfunctions)
Definition: nodeSubplan.c:725
#define IsA(nodeptr, _type_)
Definition: nodes.h:162
#define nodeTag(nodeptr)
Definition: nodes.h:116
#define makeNode(_type_)
Definition: nodes.h:159
#define castNode(_type_, nodeptr)
Definition: nodes.h:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
#define lfirst(lc)
Definition: pg_list.h:170
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:150
#define NIL
Definition: pg_list.h:66
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:465
#define lfirst_int(lc)
Definition: pg_list.h:171
#define list_make1(x1)
Definition: pg_list.h:210
#define linitial_int(l)
Definition: pg_list.h:177
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
uintptr_t Datum
Definition: postgres.h:412
static Datum BoolGetDatum(bool X)
Definition: postgres.h:450
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660
unsigned int Oid
Definition: postgres_ext.h:31
SubLinkType
Definition: primnodes.h:823
@ ARRAY_SUBLINK
Definition: primnodes.h:830
@ ANY_SUBLINK
Definition: primnodes.h:826
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:829
@ CTE_SUBLINK
Definition: primnodes.h:831
@ EXPR_SUBLINK
Definition: primnodes.h:828
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:827
@ ALL_SUBLINK
Definition: primnodes.h:825
@ EXISTS_SUBLINK
Definition: primnodes.h:824
ScanDirection
Definition: sdir.h:23
@ ForwardScanDirection
Definition: sdir.h:26
ParamExecData * es_param_exec_vals
Definition: execnodes.h:645
MemoryContext es_query_cxt
Definition: execnodes.h:650
ScanDirection es_direction
Definition: execnodes.h:607
List * es_subplanstates
Definition: execnodes.h:662
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:258
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:254
TupleTableSlot * resultslot
Definition: execnodes.h:96
Definition: fmgr.h:57
Definition: pg_list.h:52
Definition: nodes.h:112
Oid opno
Definition: primnodes.h:648
List * args
Definition: primnodes.h:666
Oid inputcollid
Definition: primnodes.h:663
bool isnull
Definition: params.h:150
Datum value
Definition: params.h:149
void * execPlan
Definition: params.h:148
Plan * plan
Definition: execnodes.h:1024
EState * state
Definition: execnodes.h:1026
Bitmapset * chgParam
Definition: execnodes.h:1056
Bitmapset * extParam
Definition: plannodes.h:168
Cardinality plan_rows
Definition: plannodes.h:132
ExprState pi_state
Definition: execnodes.h:354
ExprContext * pi_exprContext
Definition: execnodes.h:356
TupleHashTable hashtable
Definition: execnodes.h:950
ExprState * cur_eq_comp
Definition: execnodes.h:967
FmgrInfo * tab_eq_funcs
Definition: execnodes.h:964
MemoryContext hashtablecxt
Definition: execnodes.h:954
Oid * tab_eq_funcoids
Definition: execnodes.h:960
ExprContext * innerecontext
Definition: execnodes.h:956
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:963
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:966
List * args
Definition: execnodes.h:943
MemoryContext hashtempcxt
Definition: execnodes.h:955
HeapTuple curTuple
Definition: execnodes.h:944
FmgrInfo * lhs_hash_funcs
Definition: execnodes.h:965
AttrNumber * keyColIdx
Definition: execnodes.h:959
struct PlanState * planstate
Definition: execnodes.h:940
TupleDesc descRight
Definition: execnodes.h:947
SubPlan * subplan
Definition: execnodes.h:939
ProjectionInfo * projLeft
Definition: execnodes.h:948
ProjectionInfo * projRight
Definition: execnodes.h:949
bool havenullrows
Definition: execnodes.h:953
ExprState * testexpr
Definition: execnodes.h:942
struct PlanState * parent
Definition: execnodes.h:941
Oid * tab_collations
Definition: execnodes.h:962
TupleHashTable hashnulls
Definition: execnodes.h:951
bool havehashrows
Definition: execnodes.h:952
Datum curArray
Definition: execnodes.h:945
int plan_id
Definition: primnodes.h:893
char * plan_name
Definition: primnodes.h:895
List * args
Definition: primnodes.h:914
List * paramIds
Definition: primnodes.h:891
bool useHashTable
Definition: primnodes.h:902
Node * testexpr
Definition: primnodes.h:890
List * parParam
Definition: primnodes.h:913
List * setParam
Definition: primnodes.h:911
bool unknownEqFalse
Definition: primnodes.h:904
SubLinkType subLinkType
Definition: primnodes.h:888
Oid firstColType
Definition: primnodes.h:897
MinimalTuple firstTuple
Definition: execnodes.h:780
AttrNumber * keyColIdx
Definition: execnodes.h:798
MemoryContext tempcxt
Definition: execnodes.h:803
TupleTableSlot * tableslot
Definition: execnodes.h:805
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433
static HeapTuple ExecCopySlotHeapTuple(TupleTableSlot *slot)
Definition: tuptable.h:460
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:389
#define TupIsNull(slot)
Definition: tuptable.h:300
static bool slot_attisnull(TupleTableSlot *slot, int attnum)
Definition: tuptable.h:375