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