PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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-2025, 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 "catalog/pg_class.h"
18#include "executor/executor.h"
19#include "executor/nodeAgg.h"
20#include "executor/nodeAppend.h"
26#include "executor/nodeCustom.h"
29#include "executor/nodeGather.h"
31#include "executor/nodeGroup.h"
32#include "executor/nodeHash.h"
37#include "executor/nodeLimit.h"
48#include "executor/nodeResult.h"
51#include "executor/nodeSetOp.h"
52#include "executor/nodeSort.h"
58#include "executor/nodeUnique.h"
62#include "nodes/extensible.h"
63#include "nodes/pathnodes.h"
64#include "utils/syscache.h"
65
66static bool IndexSupportsBackwardScan(Oid indexid);
67
68
69/*
70 * ExecReScan
71 * Reset a plan node so that its output can be re-scanned.
72 *
73 * Note that if the plan node has parameters that have changed value,
74 * the output might be different from last time.
75 */
76void
78{
79 /* If collecting timing stats, update them */
80 if (node->instrument)
82
83 /*
84 * If we have changed parameters, propagate that info.
85 *
86 * Note: ExecReScanSetParamPlan() can add bits to node->chgParam,
87 * corresponding to the output param(s) that the InitPlan will update.
88 * Since we make only one pass over the list, that means that an InitPlan
89 * can depend on the output param(s) of a sibling InitPlan only if that
90 * sibling appears earlier in the list. This is workable for now given
91 * the limited ways in which one InitPlan could depend on another, but
92 * eventually we might need to work harder (or else make the planner
93 * enlarge the extParam/allParam sets to include the params of depended-on
94 * InitPlans).
95 */
96 if (node->chgParam != NULL)
97 {
98 ListCell *l;
99
100 foreach(l, node->initPlan)
101 {
102 SubPlanState *sstate = (SubPlanState *) lfirst(l);
103 PlanState *splan = sstate->planstate;
104
105 if (splan->plan->extParam != NULL) /* don't care about child
106 * local Params */
107 UpdateChangedParamSet(splan, node->chgParam);
108 if (splan->chgParam != NULL)
109 ExecReScanSetParamPlan(sstate, node);
110 }
111 foreach(l, node->subPlan)
112 {
113 SubPlanState *sstate = (SubPlanState *) lfirst(l);
114 PlanState *splan = sstate->planstate;
115
116 if (splan->plan->extParam != NULL)
117 UpdateChangedParamSet(splan, node->chgParam);
118 }
119 /* Well. Now set chgParam for child trees. */
120 if (outerPlanState(node) != NULL)
122 if (innerPlanState(node) != NULL)
124 }
125
126 /* Call expression callbacks */
127 if (node->ps_ExprContext)
129
130 /* And do node-type-specific processing */
131 switch (nodeTag(node))
132 {
133 case T_ResultState:
135 break;
136
137 case T_ProjectSetState:
139 break;
140
141 case T_ModifyTableState:
143 break;
144
145 case T_AppendState:
147 break;
148
149 case T_MergeAppendState:
151 break;
152
153 case T_RecursiveUnionState:
155 break;
156
157 case T_BitmapAndState:
159 break;
160
161 case T_BitmapOrState:
163 break;
164
165 case T_SeqScanState:
167 break;
168
169 case T_SampleScanState:
171 break;
172
173 case T_GatherState:
175 break;
176
177 case T_GatherMergeState:
179 break;
180
181 case T_IndexScanState:
183 break;
184
185 case T_IndexOnlyScanState:
187 break;
188
189 case T_BitmapIndexScanState:
191 break;
192
193 case T_BitmapHeapScanState:
195 break;
196
197 case T_TidScanState:
199 break;
200
201 case T_TidRangeScanState:
203 break;
204
205 case T_SubqueryScanState:
207 break;
208
209 case T_FunctionScanState:
211 break;
212
213 case T_TableFuncScanState:
215 break;
216
217 case T_ValuesScanState:
219 break;
220
221 case T_CteScanState:
223 break;
224
225 case T_NamedTuplestoreScanState:
227 break;
228
229 case T_WorkTableScanState:
231 break;
232
233 case T_ForeignScanState:
235 break;
236
237 case T_CustomScanState:
239 break;
240
241 case T_NestLoopState:
243 break;
244
245 case T_MergeJoinState:
247 break;
248
249 case T_HashJoinState:
251 break;
252
253 case T_MaterialState:
255 break;
256
257 case T_MemoizeState:
259 break;
260
261 case T_SortState:
262 ExecReScanSort((SortState *) node);
263 break;
264
265 case T_IncrementalSortState:
267 break;
268
269 case T_GroupState:
270 ExecReScanGroup((GroupState *) node);
271 break;
272
273 case T_AggState:
274 ExecReScanAgg((AggState *) node);
275 break;
276
277 case T_WindowAggState:
279 break;
280
281 case T_UniqueState:
283 break;
284
285 case T_HashState:
286 ExecReScanHash((HashState *) node);
287 break;
288
289 case T_SetOpState:
290 ExecReScanSetOp((SetOpState *) node);
291 break;
292
293 case T_LockRowsState:
295 break;
296
297 case T_LimitState:
298 ExecReScanLimit((LimitState *) node);
299 break;
300
301 default:
302 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
303 break;
304 }
305
306 if (node->chgParam != NULL)
307 {
308 bms_free(node->chgParam);
309 node->chgParam = NULL;
310 }
311}
312
313/*
314 * ExecMarkPos
315 *
316 * Marks the current scan position.
317 *
318 * NOTE: mark/restore capability is currently needed only for plan nodes
319 * that are the immediate inner child of a MergeJoin node. Since MergeJoin
320 * requires sorted input, there is never any need to support mark/restore in
321 * node types that cannot produce sorted output. There are some cases in
322 * which a node can pass through sorted data from its child; if we don't
323 * implement mark/restore for such a node type, the planner compensates by
324 * inserting a Material node above that node.
325 */
326void
328{
329 switch (nodeTag(node))
330 {
331 case T_IndexScanState:
333 break;
334
335 case T_IndexOnlyScanState:
337 break;
338
339 case T_CustomScanState:
341 break;
342
343 case T_MaterialState:
345 break;
346
347 case T_SortState:
348 ExecSortMarkPos((SortState *) node);
349 break;
350
351 case T_ResultState:
353 break;
354
355 default:
356 /* don't make hard error unless caller asks to restore... */
357 elog(DEBUG2, "unrecognized node type: %d", (int) nodeTag(node));
358 break;
359 }
360}
361
362/*
363 * ExecRestrPos
364 *
365 * restores the scan position previously saved with ExecMarkPos()
366 *
367 * NOTE: the semantics of this are that the first ExecProcNode following
368 * the restore operation will yield the same tuple as the first one following
369 * the mark operation. It is unspecified what happens to the plan node's
370 * result TupleTableSlot. (In most cases the result slot is unchanged by
371 * a restore, but the node may choose to clear it or to load it with the
372 * restored-to tuple.) Hence the caller should discard any previously
373 * returned TupleTableSlot after doing a restore.
374 */
375void
377{
378 switch (nodeTag(node))
379 {
380 case T_IndexScanState:
382 break;
383
384 case T_IndexOnlyScanState:
386 break;
387
388 case T_CustomScanState:
390 break;
391
392 case T_MaterialState:
394 break;
395
396 case T_SortState:
397 ExecSortRestrPos((SortState *) node);
398 break;
399
400 case T_ResultState:
402 break;
403
404 default:
405 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
406 break;
407 }
408}
409
410/*
411 * ExecSupportsMarkRestore - does a Path support mark/restore?
412 *
413 * This is used during planning and so must accept a Path, not a Plan.
414 * We keep it here to be adjacent to the routines above, which also must
415 * know which plan types support mark/restore.
416 */
417bool
419{
420 /*
421 * For consistency with the routines above, we do not examine the nodeTag
422 * but rather the pathtype, which is the Plan node type the Path would
423 * produce.
424 */
425 switch (pathnode->pathtype)
426 {
427 case T_IndexScan:
428 case T_IndexOnlyScan:
429
430 /*
431 * Not all index types support mark/restore.
432 */
433 return castNode(IndexPath, pathnode)->indexinfo->amcanmarkpos;
434
435 case T_Material:
436 case T_Sort:
437 return true;
438
439 case T_CustomScan:
441 return true;
442 return false;
443
444 case T_Result:
445
446 /*
447 * Result supports mark/restore iff it has a child plan that does.
448 *
449 * We have to be careful here because there is more than one Path
450 * type that can produce a Result plan node.
451 */
452 if (IsA(pathnode, ProjectionPath))
453 return ExecSupportsMarkRestore(((ProjectionPath *) pathnode)->subpath);
454 else if (IsA(pathnode, MinMaxAggPath))
455 return false; /* childless Result */
456 else if (IsA(pathnode, GroupResultPath))
457 return false; /* childless Result */
458 else
459 {
460 /* Simple RTE_RESULT base relation */
461 Assert(IsA(pathnode, Path));
462 return false; /* childless Result */
463 }
464
465 case T_Append:
466 {
467 AppendPath *appendPath = castNode(AppendPath, pathnode);
468
469 /*
470 * If there's exactly one child, then there will be no Append
471 * in the final plan, so we can handle mark/restore if the
472 * child plan node can.
473 */
474 if (list_length(appendPath->subpaths) == 1)
475 return ExecSupportsMarkRestore((Path *) linitial(appendPath->subpaths));
476 /* Otherwise, Append can't handle it */
477 return false;
478 }
479
480 case T_MergeAppend:
481 {
482 MergeAppendPath *mapath = castNode(MergeAppendPath, pathnode);
483
484 /*
485 * Like the Append case above, single-subpath MergeAppends
486 * won't be in the final plan, so just return the child's
487 * mark/restore ability.
488 */
489 if (list_length(mapath->subpaths) == 1)
490 return ExecSupportsMarkRestore((Path *) linitial(mapath->subpaths));
491 /* Otherwise, MergeAppend can't handle it */
492 return false;
493 }
494
495 default:
496 break;
497 }
498
499 return false;
500}
501
502/*
503 * ExecSupportsBackwardScan - does a plan type support backwards scanning?
504 *
505 * Ideally, all plan types would support backwards scan, but that seems
506 * unlikely to happen soon. In some cases, a plan node passes the backwards
507 * scan down to its children, and so supports backwards scan only if its
508 * children do. Therefore, this routine must be passed a complete plan tree.
509 */
510bool
512{
513 if (node == NULL)
514 return false;
515
516 /*
517 * Parallel-aware nodes return a subset of the tuples in each worker, and
518 * in general we can't expect to have enough bookkeeping state to know
519 * which ones we returned in this worker as opposed to some other worker.
520 */
521 if (node->parallel_aware)
522 return false;
523
524 switch (nodeTag(node))
525 {
526 case T_Result:
527 if (outerPlan(node) != NULL)
529 else
530 return false;
531
532 case T_Append:
533 {
534 ListCell *l;
535
536 /* With async, tuples may be interleaved, so can't back up. */
537 if (((Append *) node)->nasyncplans > 0)
538 return false;
539
540 foreach(l, ((Append *) node)->appendplans)
541 {
543 return false;
544 }
545 /* need not check tlist because Append doesn't evaluate it */
546 return true;
547 }
548
549 case T_SampleScan:
550 /* Simplify life for tablesample methods by disallowing this */
551 return false;
552
553 case T_Gather:
554 return false;
555
556 case T_IndexScan:
557 return IndexSupportsBackwardScan(((IndexScan *) node)->indexid);
558
559 case T_IndexOnlyScan:
560 return IndexSupportsBackwardScan(((IndexOnlyScan *) node)->indexid);
561
562 case T_SubqueryScan:
563 return ExecSupportsBackwardScan(((SubqueryScan *) node)->subplan);
564
565 case T_CustomScan:
566 if (((CustomScan *) node)->flags & CUSTOMPATH_SUPPORT_BACKWARD_SCAN)
567 return true;
568 return false;
569
570 case T_SeqScan:
571 case T_TidScan:
572 case T_TidRangeScan:
573 case T_FunctionScan:
574 case T_ValuesScan:
575 case T_CteScan:
576 case T_Material:
577 case T_Sort:
578 /* these don't evaluate tlist */
579 return true;
580
581 case T_IncrementalSort:
582
583 /*
584 * Unlike full sort, incremental sort keeps only a single group of
585 * tuples in memory, so it can't scan backwards.
586 */
587 return false;
588
589 case T_LockRows:
590 case T_Limit:
592
593 default:
594 return false;
595 }
596}
597
598/*
599 * An IndexScan or IndexOnlyScan node supports backward scan only if the
600 * index's AM does.
601 */
602static bool
604{
605 bool result;
606 HeapTuple ht_idxrel;
607 Form_pg_class idxrelrec;
608 IndexAmRoutine *amroutine;
609
610 /* Fetch the pg_class tuple of the index relation */
611 ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexid));
612 if (!HeapTupleIsValid(ht_idxrel))
613 elog(ERROR, "cache lookup failed for relation %u", indexid);
614 idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
615
616 /* Fetch the index AM's API struct */
617 amroutine = GetIndexAmRoutineByAmId(idxrelrec->relam, false);
618
619 result = amroutine->amcanbackward;
620
621 pfree(amroutine);
622 ReleaseSysCache(ht_idxrel);
623
624 return result;
625}
626
627/*
628 * ExecMaterializesOutput - does a plan type materialize its output?
629 *
630 * Returns true if the plan node type is one that automatically materializes
631 * its output (typically by keeping it in a tuplestore). For such plans,
632 * a rescan without any parameter change will have zero startup cost and
633 * very low per-tuple cost.
634 */
635bool
637{
638 switch (plantype)
639 {
640 case T_Material:
641 case T_FunctionScan:
642 case T_TableFuncScan:
643 case T_CteScan:
644 case T_NamedTuplestoreScan:
645 case T_WorkTableScan:
646 case T_Sort:
647 return true;
648
649 default:
650 break;
651 }
652
653 return false;
654}
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
#define DEBUG2
Definition: elog.h:29
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
void ExecMarkPos(PlanState *node)
Definition: execAmi.c:327
bool ExecSupportsMarkRestore(Path *pathnode)
Definition: execAmi.c:418
static bool IndexSupportsBackwardScan(Oid indexid)
Definition: execAmi.c:603
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:636
bool ExecSupportsBackwardScan(Plan *node)
Definition: execAmi.c:511
void ExecReScan(PlanState *node)
Definition: execAmi.c:77
void ExecRestrPos(PlanState *node)
Definition: execAmi.c:376
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:444
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:911
#define outerPlanState(node)
Definition: execnodes.h:1255
#define innerPlanState(node)
Definition: execnodes.h:1254
#define CUSTOMPATH_SUPPORT_BACKWARD_SCAN
Definition: extensible.h:84
#define CUSTOMPATH_SUPPORT_MARK_RESTORE
Definition: extensible.h:85
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:140
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:311
void pfree(void *pointer)
Definition: mcxt.c:2147
void ExecReScanAgg(AggState *node)
Definition: nodeAgg.c:4466
void ExecReScanAppend(AppendState *node)
Definition: nodeAppend.c:421
void ExecReScanBitmapAnd(BitmapAndState *node)
void ExecReScanBitmapHeapScan(BitmapHeapScanState *node)
void ExecReScanBitmapIndexScan(BitmapIndexScanState *node)
void ExecReScanBitmapOr(BitmapOrState *node)
Definition: nodeBitmapOr.c:219
void ExecReScanCteScan(CteScanState *node)
Definition: nodeCtescan.c:307
void ExecCustomRestrPos(CustomScanState *node)
Definition: nodeCustom.c:150
void ExecReScanCustomScan(CustomScanState *node)
Definition: nodeCustom.c:132
void ExecCustomMarkPos(CustomScanState *node)
Definition: nodeCustom.c:139
void ExecReScanForeignScan(ForeignScanState *node)
void ExecReScanFunctionScan(FunctionScanState *node)
void ExecReScanGatherMerge(GatherMergeState *node)
void ExecReScanGather(GatherState *node)
Definition: nodeGather.c:442
void ExecReScanGroup(GroupState *node)
Definition: nodeGroup.c:235
void ExecReScanHash(HashState *node)
Definition: nodeHash.c:2375
void ExecReScanHashJoin(HashJoinState *node)
void ExecReScanIncrementalSort(IncrementalSortState *node)
void ExecReScanIndexOnlyScan(IndexOnlyScanState *node)
void ExecIndexOnlyRestrPos(IndexOnlyScanState *node)
void ExecIndexOnlyMarkPos(IndexOnlyScanState *node)
void ExecReScanIndexScan(IndexScanState *node)
void ExecIndexRestrPos(IndexScanState *node)
void ExecIndexMarkPos(IndexScanState *node)
void ExecReScanLimit(LimitState *node)
Definition: nodeLimit.c:541
void ExecReScanLockRows(LockRowsState *node)
Definition: nodeLockRows.c:399
void ExecReScanMaterial(MaterialState *node)
Definition: nodeMaterial.c:313
void ExecMaterialMarkPos(MaterialState *node)
Definition: nodeMaterial.c:262
void ExecMaterialRestrPos(MaterialState *node)
Definition: nodeMaterial.c:290
void ExecReScanMemoize(MemoizeState *node)
Definition: nodeMemoize.c:1139
void ExecReScanMergeAppend(MergeAppendState *node)
void ExecReScanMergeJoin(MergeJoinState *node)
void ExecReScanModifyTable(ModifyTableState *node)
void ExecReScanNamedTuplestoreScan(NamedTuplestoreScanState *node)
void ExecReScanNestLoop(NestLoopState *node)
Definition: nodeNestloop.c:381
void ExecReScanProjectSet(ProjectSetState *node)
void ExecReScanRecursiveUnion(RecursiveUnionState *node)
void ExecReScanResult(ResultState *node)
Definition: nodeResult.c:249
void ExecResultRestrPos(ResultState *node)
Definition: nodeResult.c:161
void ExecResultMarkPos(ResultState *node)
Definition: nodeResult.c:146
void ExecReScanSampleScan(SampleScanState *node)
void ExecReScanSeqScan(SeqScanState *node)
Definition: nodeSeqscan.c:317
void ExecReScanSetOp(SetOpState *node)
Definition: nodeSetOp.c:693
void ExecSortMarkPos(SortState *node)
Definition: nodeSort.c:329
void ExecReScanSort(SortState *node)
Definition: nodeSort.c:362
void ExecSortRestrPos(SortState *node)
Definition: nodeSort.c:347
void ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
Definition: nodeSubplan.c:1299
void ExecReScanSubqueryScan(SubqueryScanState *node)
void ExecReScanTableFuncScan(TableFuncScanState *node)
void ExecReScanTidRangeScan(TidRangeScanState *node)
void ExecReScanTidScan(TidScanState *node)
Definition: nodeTidscan.c:447
void ExecReScanUnique(UniqueState *node)
Definition: nodeUnique.c:175
void ExecReScanValuesScan(ValuesScanState *node)
void ExecReScanWindowAgg(WindowAggState *node)
void ExecReScanWorkTableScan(WorkTableScanState *node)
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define nodeTag(nodeptr)
Definition: nodes.h:139
NodeTag
Definition: nodes.h:27
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
FormData_pg_class * Form_pg_class
Definition: pg_class.h:156
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define outerPlan(node)
Definition: plannodes.h:241
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:30
List * subpaths
Definition: pathnodes.h:2069
bool amcanbackward
Definition: amapi.h:254
NodeTag pathtype
Definition: pathnodes.h:1762
Instrumentation * instrument
Definition: execnodes.h:1169
Plan * plan
Definition: execnodes.h:1159
List * subPlan
Definition: execnodes.h:1186
List * initPlan
Definition: execnodes.h:1184
Bitmapset * chgParam
Definition: execnodes.h:1191
ExprContext * ps_ExprContext
Definition: execnodes.h:1198
Bitmapset * extParam
Definition: plannodes.h:229
bool parallel_aware
Definition: plannodes.h:193
struct PlanState * planstate
Definition: execnodes.h:1006
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221