PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
execAmi.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execAmi.c
4  * miscellaneous executor access method 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  * src/backend/executor/execAmi.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include "access/amapi.h"
16 #include "access/htup_details.h"
17 #include "executor/execdebug.h"
18 #include "executor/nodeAgg.h"
19 #include "executor/nodeAppend.h"
20 #include "executor/nodeBitmapAnd.h"
23 #include "executor/nodeBitmapOr.h"
24 #include "executor/nodeCtescan.h"
25 #include "executor/nodeCustom.h"
28 #include "executor/nodeGather.h"
29 #include "executor/nodeGroup.h"
30 #include "executor/nodeGroup.h"
31 #include "executor/nodeHash.h"
32 #include "executor/nodeHashjoin.h"
34 #include "executor/nodeIndexscan.h"
35 #include "executor/nodeLimit.h"
36 #include "executor/nodeLockRows.h"
37 #include "executor/nodeMaterial.h"
39 #include "executor/nodeMergejoin.h"
42 #include "executor/nodeNestloop.h"
45 #include "executor/nodeResult.h"
47 #include "executor/nodeSeqscan.h"
48 #include "executor/nodeSetOp.h"
49 #include "executor/nodeSort.h"
50 #include "executor/nodeSubplan.h"
53 #include "executor/nodeTidscan.h"
54 #include "executor/nodeUnique.h"
56 #include "executor/nodeWindowAgg.h"
58 #include "nodes/nodeFuncs.h"
59 #include "nodes/relation.h"
60 #include "utils/rel.h"
61 #include "utils/syscache.h"
62 
63 
64 static bool IndexSupportsBackwardScan(Oid indexid);
65 
66 
67 /*
68  * ExecReScan
69  * Reset a plan node so that its output can be re-scanned.
70  *
71  * Note that if the plan node has parameters that have changed value,
72  * the output might be different from last time.
73  */
74 void
76 {
77  /* If collecting timing stats, update them */
78  if (node->instrument)
79  InstrEndLoop(node->instrument);
80 
81  /*
82  * If we have changed parameters, propagate that info.
83  *
84  * Note: ExecReScanSetParamPlan() can add bits to node->chgParam,
85  * corresponding to the output param(s) that the InitPlan will update.
86  * Since we make only one pass over the list, that means that an InitPlan
87  * can depend on the output param(s) of a sibling InitPlan only if that
88  * sibling appears earlier in the list. This is workable for now given
89  * the limited ways in which one InitPlan could depend on another, but
90  * eventually we might need to work harder (or else make the planner
91  * enlarge the extParam/allParam sets to include the params of depended-on
92  * InitPlans).
93  */
94  if (node->chgParam != NULL)
95  {
96  ListCell *l;
97 
98  foreach(l, node->initPlan)
99  {
100  SubPlanState *sstate = (SubPlanState *) lfirst(l);
101  PlanState *splan = sstate->planstate;
102 
103  if (splan->plan->extParam != NULL) /* don't care about child
104  * local Params */
105  UpdateChangedParamSet(splan, node->chgParam);
106  if (splan->chgParam != NULL)
107  ExecReScanSetParamPlan(sstate, node);
108  }
109  foreach(l, node->subPlan)
110  {
111  SubPlanState *sstate = (SubPlanState *) lfirst(l);
112  PlanState *splan = sstate->planstate;
113 
114  if (splan->plan->extParam != NULL)
115  UpdateChangedParamSet(splan, node->chgParam);
116  }
117  /* Well. Now set chgParam for left/right trees. */
118  if (node->lefttree != NULL)
120  if (node->righttree != NULL)
122  }
123 
124  /* Call expression callbacks */
125  if (node->ps_ExprContext)
127 
128  /* And do node-type-specific processing */
129  switch (nodeTag(node))
130  {
131  case T_ResultState:
132  ExecReScanResult((ResultState *) node);
133  break;
134 
135  case T_ProjectSetState:
137  break;
138 
139  case T_ModifyTableState:
141  break;
142 
143  case T_AppendState:
144  ExecReScanAppend((AppendState *) node);
145  break;
146 
147  case T_MergeAppendState:
149  break;
150 
153  break;
154 
155  case T_BitmapAndState:
157  break;
158 
159  case T_BitmapOrState:
161  break;
162 
163  case T_SeqScanState:
165  break;
166 
167  case T_SampleScanState:
169  break;
170 
171  case T_GatherState:
172  ExecReScanGather((GatherState *) node);
173  break;
174 
175  case T_IndexScanState:
177  break;
178 
181  break;
182 
185  break;
186 
189  break;
190 
191  case T_TidScanState:
193  break;
194 
195  case T_SubqueryScanState:
197  break;
198 
199  case T_FunctionScanState:
201  break;
202 
205  break;
206 
207  case T_ValuesScanState:
209  break;
210 
211  case T_CteScanState:
213  break;
214 
217  break;
218 
221  break;
222 
223  case T_ForeignScanState:
225  break;
226 
227  case T_CustomScanState:
229  break;
230 
231  case T_NestLoopState:
233  break;
234 
235  case T_MergeJoinState:
237  break;
238 
239  case T_HashJoinState:
241  break;
242 
243  case T_MaterialState:
245  break;
246 
247  case T_SortState:
248  ExecReScanSort((SortState *) node);
249  break;
250 
251  case T_GroupState:
252  ExecReScanGroup((GroupState *) node);
253  break;
254 
255  case T_AggState:
256  ExecReScanAgg((AggState *) node);
257  break;
258 
259  case T_WindowAggState:
261  break;
262 
263  case T_UniqueState:
264  ExecReScanUnique((UniqueState *) node);
265  break;
266 
267  case T_HashState:
268  ExecReScanHash((HashState *) node);
269  break;
270 
271  case T_SetOpState:
272  ExecReScanSetOp((SetOpState *) node);
273  break;
274 
275  case T_LockRowsState:
277  break;
278 
279  case T_LimitState:
280  ExecReScanLimit((LimitState *) node);
281  break;
282 
283  default:
284  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
285  break;
286  }
287 
288  if (node->chgParam != NULL)
289  {
290  bms_free(node->chgParam);
291  node->chgParam = NULL;
292  }
293 }
294 
295 /*
296  * ExecMarkPos
297  *
298  * Marks the current scan position.
299  *
300  * NOTE: mark/restore capability is currently needed only for plan nodes
301  * that are the immediate inner child of a MergeJoin node. Since MergeJoin
302  * requires sorted input, there is never any need to support mark/restore in
303  * node types that cannot produce sorted output. There are some cases in
304  * which a node can pass through sorted data from its child; if we don't
305  * implement mark/restore for such a node type, the planner compensates by
306  * inserting a Material node above that node.
307  */
308 void
310 {
311  switch (nodeTag(node))
312  {
313  case T_IndexScanState:
315  break;
316 
319  break;
320 
321  case T_CustomScanState:
323  break;
324 
325  case T_MaterialState:
327  break;
328 
329  case T_SortState:
330  ExecSortMarkPos((SortState *) node);
331  break;
332 
333  case T_ResultState:
334  ExecResultMarkPos((ResultState *) node);
335  break;
336 
337  default:
338  /* don't make hard error unless caller asks to restore... */
339  elog(DEBUG2, "unrecognized node type: %d", (int) nodeTag(node));
340  break;
341  }
342 }
343 
344 /*
345  * ExecRestrPos
346  *
347  * restores the scan position previously saved with ExecMarkPos()
348  *
349  * NOTE: the semantics of this are that the first ExecProcNode following
350  * the restore operation will yield the same tuple as the first one following
351  * the mark operation. It is unspecified what happens to the plan node's
352  * result TupleTableSlot. (In most cases the result slot is unchanged by
353  * a restore, but the node may choose to clear it or to load it with the
354  * restored-to tuple.) Hence the caller should discard any previously
355  * returned TupleTableSlot after doing a restore.
356  */
357 void
359 {
360  switch (nodeTag(node))
361  {
362  case T_IndexScanState:
364  break;
365 
368  break;
369 
370  case T_CustomScanState:
372  break;
373 
374  case T_MaterialState:
376  break;
377 
378  case T_SortState:
379  ExecSortRestrPos((SortState *) node);
380  break;
381 
382  case T_ResultState:
384  break;
385 
386  default:
387  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
388  break;
389  }
390 }
391 
392 /*
393  * ExecSupportsMarkRestore - does a Path support mark/restore?
394  *
395  * This is used during planning and so must accept a Path, not a Plan.
396  * We keep it here to be adjacent to the routines above, which also must
397  * know which plan types support mark/restore.
398  */
399 bool
401 {
402  /*
403  * For consistency with the routines above, we do not examine the nodeTag
404  * but rather the pathtype, which is the Plan node type the Path would
405  * produce.
406  */
407  switch (pathnode->pathtype)
408  {
409  case T_IndexScan:
410  case T_IndexOnlyScan:
411  case T_Material:
412  case T_Sort:
413  return true;
414 
415  case T_CustomScan:
416  {
417  CustomPath *customPath = castNode(CustomPath, pathnode);
418 
419  if (customPath->flags & CUSTOMPATH_SUPPORT_MARK_RESTORE)
420  return true;
421  return false;
422  }
423  case T_Result:
424 
425  /*
426  * Result supports mark/restore iff it has a child plan that does.
427  *
428  * We have to be careful here because there is more than one Path
429  * type that can produce a Result plan node.
430  */
431  if (IsA(pathnode, ProjectionPath))
432  return ExecSupportsMarkRestore(((ProjectionPath *) pathnode)->subpath);
433  else if (IsA(pathnode, MinMaxAggPath))
434  return false; /* childless Result */
435  else
436  {
437  Assert(IsA(pathnode, ResultPath));
438  return false; /* childless Result */
439  }
440 
441  default:
442  break;
443  }
444 
445  return false;
446 }
447 
448 /*
449  * ExecSupportsBackwardScan - does a plan type support backwards scanning?
450  *
451  * Ideally, all plan types would support backwards scan, but that seems
452  * unlikely to happen soon. In some cases, a plan node passes the backwards
453  * scan down to its children, and so supports backwards scan only if its
454  * children do. Therefore, this routine must be passed a complete plan tree.
455  */
456 bool
458 {
459  if (node == NULL)
460  return false;
461 
462  /*
463  * Parallel-aware nodes return a subset of the tuples in each worker, and
464  * in general we can't expect to have enough bookkeeping state to know
465  * which ones we returned in this worker as opposed to some other worker.
466  */
467  if (node->parallel_aware)
468  return false;
469 
470  switch (nodeTag(node))
471  {
472  case T_Result:
473  if (outerPlan(node) != NULL)
474  return ExecSupportsBackwardScan(outerPlan(node));
475  else
476  return false;
477 
478  case T_Append:
479  {
480  ListCell *l;
481 
482  foreach(l, ((Append *) node)->appendplans)
483  {
484  if (!ExecSupportsBackwardScan((Plan *) lfirst(l)))
485  return false;
486  }
487  /* need not check tlist because Append doesn't evaluate it */
488  return true;
489  }
490 
491  case T_SampleScan:
492  /* Simplify life for tablesample methods by disallowing this */
493  return false;
494 
495  case T_Gather:
496  return false;
497 
498  case T_IndexScan:
499  return IndexSupportsBackwardScan(((IndexScan *) node)->indexid);
500 
501  case T_IndexOnlyScan:
502  return IndexSupportsBackwardScan(((IndexOnlyScan *) node)->indexid);
503 
504  case T_SubqueryScan:
505  return ExecSupportsBackwardScan(((SubqueryScan *) node)->subplan);
506 
507  case T_CustomScan:
508  {
509  uint32 flags = ((CustomScan *) node)->flags;
510 
512  return true;
513  }
514  return false;
515 
516  case T_SeqScan:
517  case T_TidScan:
518  case T_FunctionScan:
519  case T_ValuesScan:
520  case T_CteScan:
521  case T_Material:
522  case T_Sort:
523  return true;
524 
525  case T_LockRows:
526  case T_Limit:
527  return ExecSupportsBackwardScan(outerPlan(node));
528 
529  default:
530  return false;
531  }
532 }
533 
534 /*
535  * An IndexScan or IndexOnlyScan node supports backward scan only if the
536  * index's AM does.
537  */
538 static bool
540 {
541  bool result;
542  HeapTuple ht_idxrel;
543  Form_pg_class idxrelrec;
544  IndexAmRoutine *amroutine;
545 
546  /* Fetch the pg_class tuple of the index relation */
547  ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexid));
548  if (!HeapTupleIsValid(ht_idxrel))
549  elog(ERROR, "cache lookup failed for relation %u", indexid);
550  idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
551 
552  /* Fetch the index AM's API struct */
553  amroutine = GetIndexAmRoutineByAmId(idxrelrec->relam, false);
554 
555  result = amroutine->amcanbackward;
556 
557  pfree(amroutine);
558  ReleaseSysCache(ht_idxrel);
559 
560  return result;
561 }
562 
563 /*
564  * ExecMaterializesOutput - does a plan type materialize its output?
565  *
566  * Returns true if the plan node type is one that automatically materializes
567  * its output (typically by keeping it in a tuplestore). For such plans,
568  * a rescan without any parameter change will have zero startup cost and
569  * very low per-tuple cost.
570  */
571 bool
573 {
574  switch (plantype)
575  {
576  case T_Material:
577  case T_FunctionScan:
578  case T_TableFuncScan:
579  case T_CteScan:
581  case T_WorkTableScan:
582  case T_Sort:
583  return true;
584 
585  default:
586  break;
587  }
588 
589  return false;
590 }
void ExecReScanBitmapHeapScan(BitmapHeapScanState *node)
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
void ExecReScanGroup(GroupState *node)
Definition: nodeGroup.c:240
bool ExecSupportsMarkRestore(Path *pathnode)
Definition: execAmi.c:400
void ExecMaterialRestrPos(MaterialState *node)
Definition: nodeMaterial.c:295
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define CUSTOMPATH_SUPPORT_BACKWARD_SCAN
Definition: extensible.h:81
void ExecReScanModifyTable(ModifyTableState *node)
Instrumentation * instrument
Definition: execnodes.h:838
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
#define CUSTOMPATH_SUPPORT_MARK_RESTORE
Definition: extensible.h:82
List * initPlan
Definition: execnodes.h:849
void ExecReScanMaterial(MaterialState *node)
Definition: nodeMaterial.c:318
void ExecIndexOnlyRestrPos(IndexOnlyScanState *node)
ExprContext * ps_ExprContext
Definition: execnodes.h:862
void ExecReScanLockRows(LockRowsState *node)
Definition: nodeLockRows.c:454
void ExecReScan(PlanState *node)
Definition: execAmi.c:75
void ExecReScanFunctionScan(FunctionScanState *node)
List * subPlan
Definition: execnodes.h:851
Definition: nodes.h:48
return result
Definition: formatting.c:1633
void ExecReScanIndexOnlyScan(IndexOnlyScanState *node)
uint32 flags
Definition: relation.h:1159
Definition: nodes.h:75
void ExecReScanLimit(LimitState *node)
Definition: nodeLimit.c:434
void ExecReScanWorkTableScan(WorkTableScanState *node)
struct PlanState * righttree
Definition: execnodes.h:848
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
void ExecReScanResult(ResultState *node)
Definition: nodeResult.c:260
void ExecResultRestrPos(ResultState *node)
Definition: nodeResult.c:158
void ExecReScanSetOp(SetOpState *node)
Definition: nodeSetOp.c:590
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
static bool IndexSupportsBackwardScan(Oid indexid)
Definition: execAmi.c:539
void ExecReScanSort(SortState *node)
Definition: nodeSort.c:292
void ExecReScanForeignScan(ForeignScanState *node)
struct PlanState * lefttree
Definition: execnodes.h:847
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:114
NodeTag pathtype
Definition: relation.h:952
void ExecCustomRestrPos(CustomScanState *node)
Definition: nodeCustom.c:148
void ExecRestrPos(PlanState *node)
Definition: execAmi.c:358
void ExecReScanHash(HashState *node)
Definition: nodeHash.c:1263
void pfree(void *pointer)
Definition: mcxt.c:950
void ExecIndexOnlyMarkPos(IndexOnlyScanState *node)
void ExecReScanProjectSet(ProjectSetState *node)
Definition: nodes.h:45
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:755
void ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
Definition: nodeSubplan.c:1087
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
#define DEBUG2
Definition: elog.h:24
void ExecSortMarkPos(SortState *node)
Definition: nodeSort.c:259
void ExecReScanBitmapIndexScan(BitmapIndexScanState *node)
bool amcanbackward
Definition: amapi.h:175
void ExecIndexMarkPos(IndexScanState *node)
void ExecReScanUnique(UniqueState *node)
Definition: nodeUnique.c:185
void ExecReScanIndexScan(IndexScanState *node)
bool parallel_aware
Definition: plannodes.h:137
void ExecReScanMergeAppend(MergeAppendState *node)
unsigned int uint32
Definition: c.h:268
void ExecReScanNestLoop(NestLoopState *node)
Definition: nodeNestloop.c:391
static SPIPlanPtr splan
Definition: regress.c:451
Bitmapset * chgParam
Definition: execnodes.h:856
#define outerPlan(node)
Definition: plannodes.h:174
void ExecReScanGather(GatherState *node)
Definition: nodeGather.c:424
void ExecReScanNamedTuplestoreScan(NamedTuplestoreScanState *node)
void ExecReScanAppend(AppendState *node)
Definition: nodeAppend.c:272
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
bool ExecSupportsBackwardScan(Plan *node)
Definition: execAmi.c:457
void ExecReScanSubqueryScan(SubqueryScanState *node)
void ExecCustomMarkPos(CustomScanState *node)
Definition: nodeCustom.c:137
Plan * plan
Definition: execnodes.h:832
void ExecMarkPos(PlanState *node)
Definition: execAmi.c:309
void ExecReScanBitmapOr(BitmapOrState *node)
Definition: nodeBitmapOr.c:205
void ExecReScanRecursiveUnion(RecursiveUnionState *node)
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:671
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
void ExecReScanBitmapAnd(BitmapAndState *node)
void ExecReScanAgg(AggState *node)
Definition: nodeAgg.c:3882
void ExecSortRestrPos(SortState *node)
Definition: nodeSort.c:277
void ExecResultMarkPos(ResultState *node)
Definition: nodeResult.c:143
Bitmapset * extParam
Definition: plannodes.h:162
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:376
void ExecIndexRestrPos(IndexScanState *node)
#define nodeTag(nodeptr)
Definition: nodes.h:514
void ExecReScanValuesScan(ValuesScanState *node)
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
Definition: nodes.h:80
void ExecReScanSampleScan(SampleScanState *node)
void ExecReScanCteScan(CteScanState *node)
Definition: nodeCtescan.c:307
void ExecMaterialMarkPos(MaterialState *node)
Definition: nodeMaterial.c:267
void ExecReScanWindowAgg(WindowAggState *node)
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:572
#define elog
Definition: elog.h:219
void ExecReScanTidScan(TidScanState *node)
Definition: nodeTidscan.c:458
void ExecReScanMergeJoin(MergeJoinState *node)
void ExecReScanCustomScan(CustomScanState *node)
Definition: nodeCustom.c:130
Definition: relation.h:948
void ExecReScanTableFuncScan(TableFuncScanState *node)
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
void ExecReScanSeqScan(SeqScanState *node)
Definition: nodeSeqscan.c:267
Definition: nodes.h:85
void ExecReScanHashJoin(HashJoinState *node)
Definition: nodeHashjoin.c:895