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