PostgreSQL Source Code git master
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/nodeAgg.h"
19#include "executor/nodeAppend.h"
25#include "executor/nodeCustom.h"
28#include "executor/nodeGather.h"
30#include "executor/nodeGroup.h"
31#include "executor/nodeHash.h"
36#include "executor/nodeLimit.h"
47#include "executor/nodeResult.h"
50#include "executor/nodeSetOp.h"
51#include "executor/nodeSort.h"
57#include "executor/nodeUnique.h"
61#include "nodes/extensible.h"
62#include "nodes/pathnodes.h"
63#include "utils/syscache.h"
64
65static bool IndexSupportsBackwardScan(Oid indexid);
66
67
68/*
69 * ExecReScan
70 * Reset a plan node so that its output can be re-scanned.
71 *
72 * Note that if the plan node has parameters that have changed value,
73 * the output might be different from last time.
74 */
75void
77{
78 /* If collecting timing stats, update them */
79 if (node->instrument)
81
82 /*
83 * If we have changed parameters, propagate that info.
84 *
85 * Note: ExecReScanSetParamPlan() can add bits to node->chgParam,
86 * corresponding to the output param(s) that the InitPlan will update.
87 * Since we make only one pass over the list, that means that an InitPlan
88 * can depend on the output param(s) of a sibling InitPlan only if that
89 * sibling appears earlier in the list. This is workable for now given
90 * the limited ways in which one InitPlan could depend on another, but
91 * eventually we might need to work harder (or else make the planner
92 * enlarge the extParam/allParam sets to include the params of depended-on
93 * InitPlans).
94 */
95 if (node->chgParam != NULL)
96 {
97 ListCell *l;
98
99 foreach(l, node->initPlan)
100 {
101 SubPlanState *sstate = (SubPlanState *) lfirst(l);
102 PlanState *splan = sstate->planstate;
103
104 if (splan->plan->extParam != NULL) /* don't care about child
105 * local Params */
107 if (splan->chgParam != NULL)
108 ExecReScanSetParamPlan(sstate, node);
109 }
110 foreach(l, node->subPlan)
111 {
112 SubPlanState *sstate = (SubPlanState *) lfirst(l);
113 PlanState *splan = sstate->planstate;
114
115 if (splan->plan->extParam != NULL)
117 }
118 /* Well. Now set chgParam for child trees. */
119 if (outerPlanState(node) != NULL)
121 if (innerPlanState(node) != NULL)
123 }
124
125 /* Call expression callbacks */
126 if (node->ps_ExprContext)
128
129 /* And do node-type-specific processing */
130 switch (nodeTag(node))
131 {
132 case T_ResultState:
134 break;
135
136 case T_ProjectSetState:
138 break;
139
140 case T_ModifyTableState:
142 break;
143
144 case T_AppendState:
146 break;
147
148 case T_MergeAppendState:
150 break;
151
152 case T_RecursiveUnionState:
154 break;
155
156 case T_BitmapAndState:
158 break;
159
160 case T_BitmapOrState:
162 break;
163
164 case T_SeqScanState:
166 break;
167
168 case T_SampleScanState:
170 break;
171
172 case T_GatherState:
174 break;
175
176 case T_GatherMergeState:
178 break;
179
180 case T_IndexScanState:
182 break;
183
184 case T_IndexOnlyScanState:
186 break;
187
188 case T_BitmapIndexScanState:
190 break;
191
192 case T_BitmapHeapScanState:
194 break;
195
196 case T_TidScanState:
198 break;
199
200 case T_TidRangeScanState:
202 break;
203
204 case T_SubqueryScanState:
206 break;
207
208 case T_FunctionScanState:
210 break;
211
212 case T_TableFuncScanState:
214 break;
215
216 case T_ValuesScanState:
218 break;
219
220 case T_CteScanState:
222 break;
223
224 case T_NamedTuplestoreScanState:
226 break;
227
228 case T_WorkTableScanState:
230 break;
231
232 case T_ForeignScanState:
234 break;
235
236 case T_CustomScanState:
238 break;
239
240 case T_NestLoopState:
242 break;
243
244 case T_MergeJoinState:
246 break;
247
248 case T_HashJoinState:
250 break;
251
252 case T_MaterialState:
254 break;
255
256 case T_MemoizeState:
258 break;
259
260 case T_SortState:
261 ExecReScanSort((SortState *) node);
262 break;
263
264 case T_IncrementalSortState:
266 break;
267
268 case T_GroupState:
269 ExecReScanGroup((GroupState *) node);
270 break;
271
272 case T_AggState:
273 ExecReScanAgg((AggState *) node);
274 break;
275
276 case T_WindowAggState:
278 break;
279
280 case T_UniqueState:
282 break;
283
284 case T_HashState:
285 ExecReScanHash((HashState *) node);
286 break;
287
288 case T_SetOpState:
289 ExecReScanSetOp((SetOpState *) node);
290 break;
291
292 case T_LockRowsState:
294 break;
295
296 case T_LimitState:
297 ExecReScanLimit((LimitState *) node);
298 break;
299
300 default:
301 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
302 break;
303 }
304
305 if (node->chgParam != NULL)
306 {
307 bms_free(node->chgParam);
308 node->chgParam = NULL;
309 }
310}
311
312/*
313 * ExecMarkPos
314 *
315 * Marks the current scan position.
316 *
317 * NOTE: mark/restore capability is currently needed only for plan nodes
318 * that are the immediate inner child of a MergeJoin node. Since MergeJoin
319 * requires sorted input, there is never any need to support mark/restore in
320 * node types that cannot produce sorted output. There are some cases in
321 * which a node can pass through sorted data from its child; if we don't
322 * implement mark/restore for such a node type, the planner compensates by
323 * inserting a Material node above that node.
324 */
325void
327{
328 switch (nodeTag(node))
329 {
330 case T_IndexScanState:
332 break;
333
334 case T_IndexOnlyScanState:
336 break;
337
338 case T_CustomScanState:
340 break;
341
342 case T_MaterialState:
344 break;
345
346 case T_SortState:
347 ExecSortMarkPos((SortState *) node);
348 break;
349
350 case T_ResultState:
352 break;
353
354 default:
355 /* don't make hard error unless caller asks to restore... */
356 elog(DEBUG2, "unrecognized node type: %d", (int) nodeTag(node));
357 break;
358 }
359}
360
361/*
362 * ExecRestrPos
363 *
364 * restores the scan position previously saved with ExecMarkPos()
365 *
366 * NOTE: the semantics of this are that the first ExecProcNode following
367 * the restore operation will yield the same tuple as the first one following
368 * the mark operation. It is unspecified what happens to the plan node's
369 * result TupleTableSlot. (In most cases the result slot is unchanged by
370 * a restore, but the node may choose to clear it or to load it with the
371 * restored-to tuple.) Hence the caller should discard any previously
372 * returned TupleTableSlot after doing a restore.
373 */
374void
376{
377 switch (nodeTag(node))
378 {
379 case T_IndexScanState:
381 break;
382
383 case T_IndexOnlyScanState:
385 break;
386
387 case T_CustomScanState:
389 break;
390
391 case T_MaterialState:
393 break;
394
395 case T_SortState:
396 ExecSortRestrPos((SortState *) node);
397 break;
398
399 case T_ResultState:
401 break;
402
403 default:
404 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
405 break;
406 }
407}
408
409/*
410 * ExecSupportsMarkRestore - does a Path support mark/restore?
411 *
412 * This is used during planning and so must accept a Path, not a Plan.
413 * We keep it here to be adjacent to the routines above, which also must
414 * know which plan types support mark/restore.
415 */
416bool
418{
419 /*
420 * For consistency with the routines above, we do not examine the nodeTag
421 * but rather the pathtype, which is the Plan node type the Path would
422 * produce.
423 */
424 switch (pathnode->pathtype)
425 {
426 case T_IndexScan:
427 case T_IndexOnlyScan:
428
429 /*
430 * Not all index types support mark/restore.
431 */
432 return castNode(IndexPath, pathnode)->indexinfo->amcanmarkpos;
433
434 case T_Material:
435 case T_Sort:
436 return true;
437
438 case T_CustomScan:
440 return true;
441 return false;
442
443 case T_Result:
444
445 /*
446 * Result supports mark/restore iff it has a child plan that does.
447 *
448 * We have to be careful here because there is more than one Path
449 * type that can produce a Result plan node.
450 */
451 if (IsA(pathnode, ProjectionPath))
452 return ExecSupportsMarkRestore(((ProjectionPath *) pathnode)->subpath);
453 else if (IsA(pathnode, MinMaxAggPath))
454 return false; /* childless Result */
455 else if (IsA(pathnode, GroupResultPath))
456 return false; /* childless Result */
457 else
458 {
459 /* Simple RTE_RESULT base relation */
460 Assert(IsA(pathnode, Path));
461 return false; /* childless Result */
462 }
463
464 case T_Append:
465 {
466 AppendPath *appendPath = castNode(AppendPath, pathnode);
467
468 /*
469 * If there's exactly one child, then there will be no Append
470 * in the final plan, so we can handle mark/restore if the
471 * child plan node can.
472 */
473 if (list_length(appendPath->subpaths) == 1)
474 return ExecSupportsMarkRestore((Path *) linitial(appendPath->subpaths));
475 /* Otherwise, Append can't handle it */
476 return false;
477 }
478
479 case T_MergeAppend:
480 {
481 MergeAppendPath *mapath = castNode(MergeAppendPath, pathnode);
482
483 /*
484 * Like the Append case above, single-subpath MergeAppends
485 * won't be in the final plan, so just return the child's
486 * mark/restore ability.
487 */
488 if (list_length(mapath->subpaths) == 1)
489 return ExecSupportsMarkRestore((Path *) linitial(mapath->subpaths));
490 /* Otherwise, MergeAppend can't handle it */
491 return false;
492 }
493
494 default:
495 break;
496 }
497
498 return false;
499}
500
501/*
502 * ExecSupportsBackwardScan - does a plan type support backwards scanning?
503 *
504 * Ideally, all plan types would support backwards scan, but that seems
505 * unlikely to happen soon. In some cases, a plan node passes the backwards
506 * scan down to its children, and so supports backwards scan only if its
507 * children do. Therefore, this routine must be passed a complete plan tree.
508 */
509bool
511{
512 if (node == NULL)
513 return false;
514
515 /*
516 * Parallel-aware nodes return a subset of the tuples in each worker, and
517 * in general we can't expect to have enough bookkeeping state to know
518 * which ones we returned in this worker as opposed to some other worker.
519 */
520 if (node->parallel_aware)
521 return false;
522
523 switch (nodeTag(node))
524 {
525 case T_Result:
526 if (outerPlan(node) != NULL)
528 else
529 return false;
530
531 case T_Append:
532 {
533 ListCell *l;
534
535 /* With async, tuples may be interleaved, so can't back up. */
536 if (((Append *) node)->nasyncplans > 0)
537 return false;
538
539 foreach(l, ((Append *) node)->appendplans)
540 {
542 return false;
543 }
544 /* need not check tlist because Append doesn't evaluate it */
545 return true;
546 }
547
548 case T_SampleScan:
549 /* Simplify life for tablesample methods by disallowing this */
550 return false;
551
552 case T_Gather:
553 return false;
554
555 case T_IndexScan:
556 return IndexSupportsBackwardScan(((IndexScan *) node)->indexid);
557
558 case T_IndexOnlyScan:
559 return IndexSupportsBackwardScan(((IndexOnlyScan *) node)->indexid);
560
561 case T_SubqueryScan:
562 return ExecSupportsBackwardScan(((SubqueryScan *) node)->subplan);
563
564 case T_CustomScan:
565 if (((CustomScan *) node)->flags & CUSTOMPATH_SUPPORT_BACKWARD_SCAN)
566 return true;
567 return false;
568
569 case T_SeqScan:
570 case T_TidScan:
571 case T_TidRangeScan:
572 case T_FunctionScan:
573 case T_ValuesScan:
574 case T_CteScan:
575 case T_Material:
576 case T_Sort:
577 /* these don't evaluate tlist */
578 return true;
579
580 case T_IncrementalSort:
581
582 /*
583 * Unlike full sort, incremental sort keeps only a single group of
584 * tuples in memory, so it can't scan backwards.
585 */
586 return false;
587
588 case T_LockRows:
589 case T_Limit:
591
592 default:
593 return false;
594 }
595}
596
597/*
598 * An IndexScan or IndexOnlyScan node supports backward scan only if the
599 * index's AM does.
600 */
601static bool
603{
604 bool result;
605 HeapTuple ht_idxrel;
606 Form_pg_class idxrelrec;
607 IndexAmRoutine *amroutine;
608
609 /* Fetch the pg_class tuple of the index relation */
610 ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexid));
611 if (!HeapTupleIsValid(ht_idxrel))
612 elog(ERROR, "cache lookup failed for relation %u", indexid);
613 idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
614
615 /* Fetch the index AM's API struct */
616 amroutine = GetIndexAmRoutineByAmId(idxrelrec->relam, false);
617
618 result = amroutine->amcanbackward;
619
620 pfree(amroutine);
621 ReleaseSysCache(ht_idxrel);
622
623 return result;
624}
625
626/*
627 * ExecMaterializesOutput - does a plan type materialize its output?
628 *
629 * Returns true if the plan node type is one that automatically materializes
630 * its output (typically by keeping it in a tuplestore). For such plans,
631 * a rescan without any parameter change will have zero startup cost and
632 * very low per-tuple cost.
633 */
634bool
636{
637 switch (plantype)
638 {
639 case T_Material:
640 case T_FunctionScan:
641 case T_TableFuncScan:
642 case T_CteScan:
643 case T_NamedTuplestoreScan:
644 case T_WorkTableScan:
645 case T_Sort:
646 return true;
647
648 default:
649 break;
650 }
651
652 return false;
653}
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
#define Assert(condition)
Definition: c.h:815
#define DEBUG2
Definition: elog.h:29
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
void ExecMarkPos(PlanState *node)
Definition: execAmi.c:326
bool ExecSupportsMarkRestore(Path *pathnode)
Definition: execAmi.c:417
static bool IndexSupportsBackwardScan(Oid indexid)
Definition: execAmi.c:602
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:635
bool ExecSupportsBackwardScan(Plan *node)
Definition: execAmi.c:510
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
void ExecRestrPos(PlanState *node)
Definition: execAmi.c:375
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:444
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:900
#define outerPlanState(node)
Definition: execnodes.h:1246
#define innerPlanState(node)
Definition: execnodes.h:1245
#define CUSTOMPATH_SUPPORT_BACKWARD_SCAN
Definition: extensible.h:84
#define CUSTOMPATH_SUPPORT_MARK_RESTORE
Definition: extensible.h:85
#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:308
void pfree(void *pointer)
Definition: mcxt.c:1521
void ExecReScanAgg(AggState *node)
Definition: nodeAgg.c:4397
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:2246
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:682
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:158
#define nodeTag(nodeptr)
Definition: nodes.h:133
NodeTag
Definition: nodes.h:27
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#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:231
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:32
static SPIPlanPtr splan
Definition: regress.c:267
List * subpaths
Definition: pathnodes.h:1968
bool amcanbackward
Definition: amapi.h:247
NodeTag pathtype
Definition: pathnodes.h:1661
Instrumentation * instrument
Definition: execnodes.h:1160
List * subPlan
Definition: execnodes.h:1177
List * initPlan
Definition: execnodes.h:1175
Bitmapset * chgParam
Definition: execnodes.h:1182
ExprContext * ps_ExprContext
Definition: execnodes.h:1189
bool parallel_aware
Definition: plannodes.h:183
struct PlanState * planstate
Definition: execnodes.h:997
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221