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