PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeIndexonlyscan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeIndexonlyscan.c
4 * Routines to support index-only scans
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/executor/nodeIndexonlyscan.c
12 *
13 *-------------------------------------------------------------------------
14 */
15/*
16 * INTERFACE ROUTINES
17 * ExecIndexOnlyScan scans an index
18 * IndexOnlyNext retrieve next tuple
19 * ExecInitIndexOnlyScan creates and initializes state info.
20 * ExecReScanIndexOnlyScan rescans the indexed relation.
21 * ExecEndIndexOnlyScan releases all storage.
22 * ExecIndexOnlyMarkPos marks scan position.
23 * ExecIndexOnlyRestrPos restores scan position.
24 * ExecIndexOnlyScanEstimate estimates DSM space needed for
25 * parallel index-only scan
26 * ExecIndexOnlyScanInitializeDSM initialize DSM for parallel
27 * index-only scan
28 * ExecIndexOnlyScanReInitializeDSM reinitialize DSM for fresh scan
29 * ExecIndexOnlyScanInitializeWorker attach to DSM info in parallel worker
30 */
31#include "postgres.h"
32
33#include "access/genam.h"
34#include "access/relscan.h"
35#include "access/tableam.h"
36#include "access/tupdesc.h"
38#include "catalog/pg_type.h"
39#include "executor/executor.h"
40#include "executor/instrument.h"
43#include "miscadmin.h"
44#include "storage/bufmgr.h"
45#include "storage/predicate.h"
46#include "utils/builtins.h"
47#include "utils/rel.h"
48
49
53
54
55/* ----------------------------------------------------------------
56 * IndexOnlyNext
57 *
58 * Retrieve a tuple from the IndexOnlyScan node's index.
59 * ----------------------------------------------------------------
60 */
61static TupleTableSlot *
63{
64 EState *estate;
65 ExprContext *econtext;
66 ScanDirection direction;
67 IndexScanDesc scandesc;
68 TupleTableSlot *slot;
69 ItemPointer tid;
70
71 /*
72 * extract necessary information from index scan node
73 */
74 estate = node->ss.ps.state;
75
76 /*
77 * Determine which direction to scan the index in based on the plan's scan
78 * direction and the current direction of execution.
79 */
80 direction = ScanDirectionCombine(estate->es_direction,
81 ((IndexOnlyScan *) node->ss.ps.plan)->indexorderdir);
82 scandesc = node->ioss_ScanDesc;
83 econtext = node->ss.ps.ps_ExprContext;
84 slot = node->ss.ss_ScanTupleSlot;
85
86 if (scandesc == NULL)
87 {
88 /*
89 * We reach here if the index only scan is not parallel, or if we're
90 * serially executing an index only scan that was planned to be
91 * parallel.
92 */
93 scandesc = index_beginscan(node->ss.ss_currentRelation,
95 estate->es_snapshot,
96 &node->ioss_Instrument,
97 node->ioss_NumScanKeys,
99
100 node->ioss_ScanDesc = scandesc;
101
102
103 /* Set it up for index-only scan */
104 node->ioss_ScanDesc->xs_want_itup = true;
106
107 /*
108 * If no run-time keys to calculate or they are ready, go ahead and
109 * pass the scankeys to the index AM.
110 */
111 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
112 index_rescan(scandesc,
113 node->ioss_ScanKeys,
114 node->ioss_NumScanKeys,
115 node->ioss_OrderByKeys,
116 node->ioss_NumOrderByKeys);
117 }
118
119 /*
120 * OK, now that we have what we need, fetch the next tuple.
121 */
122 while ((tid = index_getnext_tid(scandesc, direction)) != NULL)
123 {
124 bool tuple_from_heap = false;
125
127
128 /*
129 * We can skip the heap fetch if the TID references a heap page on
130 * which all tuples are known visible to everybody. In any case,
131 * we'll use the index tuple not the heap tuple as the data source.
132 *
133 * Note on Memory Ordering Effects: visibilitymap_get_status does not
134 * lock the visibility map buffer, and therefore the result we read
135 * here could be slightly stale. However, it can't be stale enough to
136 * matter.
137 *
138 * We need to detect clearing a VM bit due to an insert right away,
139 * because the tuple is present in the index page but not visible. The
140 * reading of the TID by this scan (using a shared lock on the index
141 * buffer) is serialized with the insert of the TID into the index
142 * (using an exclusive lock on the index buffer). Because the VM bit
143 * is cleared before updating the index, and locking/unlocking of the
144 * index page acts as a full memory barrier, we are sure to see the
145 * cleared bit if we see a recently-inserted TID.
146 *
147 * Deletes do not update the index page (only VACUUM will clear out
148 * the TID), so the clearing of the VM bit by a delete is not
149 * serialized with this test below, and we may see a value that is
150 * significantly stale. However, we don't care about the delete right
151 * away, because the tuple is still visible until the deleting
152 * transaction commits or the statement ends (if it's our
153 * transaction). In either case, the lock on the VM buffer will have
154 * been released (acting as a write barrier) after clearing the bit.
155 * And for us to have a snapshot that includes the deleting
156 * transaction (making the tuple invisible), we must have acquired
157 * ProcArrayLock after that time, acting as a read barrier.
158 *
159 * It's worth going through this complexity to avoid needing to lock
160 * the VM buffer, which could cause significant contention.
161 */
162 if (!VM_ALL_VISIBLE(scandesc->heapRelation,
164 &node->ioss_VMBuffer))
165 {
166 /*
167 * Rats, we have to visit the heap to check visibility.
168 */
169 InstrCountTuples2(node, 1);
170 if (!index_fetch_heap(scandesc, node->ioss_TableSlot))
171 continue; /* no visible tuple, try next index entry */
172
174
175 /*
176 * Only MVCC snapshots are supported here, so there should be no
177 * need to keep following the HOT chain once a visible entry has
178 * been found. If we did want to allow that, we'd need to keep
179 * more state to remember not to call index_getnext_tid next time.
180 */
181 if (scandesc->xs_heap_continue)
182 elog(ERROR, "non-MVCC snapshots are not supported in index-only scans");
183
184 /*
185 * Note: at this point we are holding a pin on the heap page, as
186 * recorded in scandesc->xs_cbuf. We could release that pin now,
187 * but it's not clear whether it's a win to do so. The next index
188 * entry might require a visit to the same heap page.
189 */
190
191 tuple_from_heap = true;
192 }
193
194 /*
195 * Fill the scan tuple slot with data from the index. This might be
196 * provided in either HeapTuple or IndexTuple format. Conceivably an
197 * index AM might fill both fields, in which case we prefer the heap
198 * format, since it's probably a bit cheaper to fill a slot from.
199 */
200 if (scandesc->xs_hitup)
201 {
202 /*
203 * We don't take the trouble to verify that the provided tuple has
204 * exactly the slot's format, but it seems worth doing a quick
205 * check on the number of fields.
206 */
208 scandesc->xs_hitupdesc->natts);
209 ExecForceStoreHeapTuple(scandesc->xs_hitup, slot, false);
210 }
211 else if (scandesc->xs_itup)
212 StoreIndexTuple(node, slot, scandesc->xs_itup, scandesc->xs_itupdesc);
213 else
214 elog(ERROR, "no data returned for index-only scan");
215
216 /*
217 * If the index was lossy, we have to recheck the index quals.
218 */
219 if (scandesc->xs_recheck)
220 {
221 econtext->ecxt_scantuple = slot;
222 if (!ExecQualAndReset(node->recheckqual, econtext))
223 {
224 /* Fails recheck, so drop it and loop back for another */
225 InstrCountFiltered2(node, 1);
226 continue;
227 }
228 }
229
230 /*
231 * We don't currently support rechecking ORDER BY distances. (In
232 * principle, if the index can support retrieval of the originally
233 * indexed value, it should be able to produce an exact distance
234 * calculation too. So it's not clear that adding code here for
235 * recheck/re-sort would be worth the trouble. But we should at least
236 * throw an error if someone tries it.)
237 */
238 if (scandesc->numberOfOrderBys > 0 && scandesc->xs_recheckorderby)
241 errmsg("lossy distance functions are not supported in index-only scans")));
242
243 /*
244 * If we didn't access the heap, then we'll need to take a predicate
245 * lock explicitly, as if we had. For now we do that at page level.
246 */
247 if (!tuple_from_heap)
250 estate->es_snapshot);
251
252 return slot;
253 }
254
255 /*
256 * if we get here it means the index scan failed so we are at the end of
257 * the scan..
258 */
259 return ExecClearTuple(slot);
260}
261
262/*
263 * StoreIndexTuple
264 * Fill the slot with data from the index tuple.
265 *
266 * At some point this might be generally-useful functionality, but
267 * right now we don't need it elsewhere.
268 */
269static void
272{
273 /*
274 * Note: we must use the tupdesc supplied by the AM in index_deform_tuple,
275 * not the slot's tupdesc, in case the latter has different datatypes
276 * (this happens for btree name_ops in particular). They'd better have
277 * the same number of columns though, as well as being datatype-compatible
278 * which is something we can't so easily check.
279 */
280 Assert(slot->tts_tupleDescriptor->natts == itupdesc->natts);
281
282 ExecClearTuple(slot);
284
285 /*
286 * Copy all name columns stored as cstrings back into a NAMEDATALEN byte
287 * sized allocation. We mark this branch as unlikely as generally "name"
288 * is used only for the system catalogs and this would have to be a user
289 * query running on those or some other user table with an index on a name
290 * column.
291 */
293 {
295
296 for (int idx = 0; idx < attcount; idx++)
297 {
299 Name name;
300
301 /* skip null Datums */
302 if (slot->tts_isnull[attnum])
303 continue;
304
305 /* allocate the NAMEDATALEN and copy the datum into that memory */
308
309 /* use namestrcpy to zero-pad all trailing bytes */
312 }
313 }
314
316}
317
318/*
319 * IndexOnlyRecheck -- access method routine to recheck a tuple in EvalPlanQual
320 *
321 * This can't really happen, since an index can't supply CTID which would
322 * be necessary data for any potential EvalPlanQual target relation. If it
323 * did happen, the EPQ code would pass us the wrong data, namely a heap
324 * tuple not an index tuple. So throw an error.
325 */
326static bool
328{
329 elog(ERROR, "EvalPlanQual recheck is not supported in index-only scans");
330 return false; /* keep compiler quiet */
331}
332
333/* ----------------------------------------------------------------
334 * ExecIndexOnlyScan(node)
335 * ----------------------------------------------------------------
336 */
337static TupleTableSlot *
339{
341
342 /*
343 * If we have runtime keys and they've not already been set up, do it now.
344 */
345 if (node->ioss_NumRuntimeKeys != 0 && !node->ioss_RuntimeKeysReady)
346 ExecReScan((PlanState *) node);
347
348 return ExecScan(&node->ss,
351}
352
353/* ----------------------------------------------------------------
354 * ExecReScanIndexOnlyScan(node)
355 *
356 * Recalculates the values of any scan keys whose value depends on
357 * information known at runtime, then rescans the indexed relation.
358 *
359 * Updating the scan key was formerly done separately in
360 * ExecUpdateIndexScanKeys. Integrating it into ReScan makes
361 * rescans of indices and relations/general streams more uniform.
362 * ----------------------------------------------------------------
363 */
364void
366{
367 /*
368 * If we are doing runtime key calculations (ie, any of the index key
369 * values weren't simple Consts), compute the new key values. But first,
370 * reset the context so we don't leak memory as each outer tuple is
371 * scanned. Note this assumes that we will recalculate *all* runtime keys
372 * on each call.
373 */
374 if (node->ioss_NumRuntimeKeys != 0)
375 {
376 ExprContext *econtext = node->ioss_RuntimeContext;
377
378 ResetExprContext(econtext);
380 node->ioss_RuntimeKeys,
381 node->ioss_NumRuntimeKeys);
382 }
383 node->ioss_RuntimeKeysReady = true;
384
385 /* reset index scan */
386 if (node->ioss_ScanDesc)
388 node->ioss_ScanKeys, node->ioss_NumScanKeys,
390
391 ExecScanReScan(&node->ss);
392}
393
394
395/* ----------------------------------------------------------------
396 * ExecEndIndexOnlyScan
397 * ----------------------------------------------------------------
398 */
399void
401{
404
405 /*
406 * extract information from the node
407 */
410
411 /* Release VM buffer pin, if any. */
412 if (node->ioss_VMBuffer != InvalidBuffer)
413 {
416 }
417
418 /*
419 * When ending a parallel worker, copy the statistics gathered by the
420 * worker back into shared memory so that it can be picked up by the main
421 * process to report in EXPLAIN ANALYZE
422 */
423 if (node->ioss_SharedInfo != NULL && IsParallelWorker())
424 {
425 IndexScanInstrumentation *winstrument;
426
427 Assert(ParallelWorkerNumber < node->ioss_SharedInfo->num_workers);
428 winstrument = &node->ioss_SharedInfo->winstrument[ParallelWorkerNumber];
429
430 /*
431 * We have to accumulate the stats rather than performing a memcpy.
432 * When a Gather/GatherMerge node finishes it will perform planner
433 * shutdown on the workers. On rescan it will spin up new workers
434 * which will have a new IndexOnlyScanState and zeroed stats.
435 */
436 winstrument->nsearches += node->ioss_Instrument.nsearches;
437 }
438
439 /*
440 * close the index relation (no-op if we didn't open it)
441 */
442 if (indexScanDesc)
446}
447
448/* ----------------------------------------------------------------
449 * ExecIndexOnlyMarkPos
450 *
451 * Note: we assume that no caller attempts to set a mark before having read
452 * at least one tuple. Otherwise, ioss_ScanDesc might still be NULL.
453 * ----------------------------------------------------------------
454 */
455void
457{
458 EState *estate = node->ss.ps.state;
459 EPQState *epqstate = estate->es_epq_active;
460
461 if (epqstate != NULL)
462 {
463 /*
464 * We are inside an EvalPlanQual recheck. If a test tuple exists for
465 * this relation, then we shouldn't access the index at all. We would
466 * instead need to save, and later restore, the state of the
467 * relsubs_done flag, so that re-fetching the test tuple is possible.
468 * However, given the assumption that no caller sets a mark at the
469 * start of the scan, we can only get here with relsubs_done[i]
470 * already set, and so no state need be saved.
471 */
472 Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
473
474 Assert(scanrelid > 0);
475 if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
476 epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
477 {
478 /* Verify the claim above */
479 if (!epqstate->relsubs_done[scanrelid - 1])
480 elog(ERROR, "unexpected ExecIndexOnlyMarkPos call in EPQ recheck");
481 return;
482 }
483 }
484
486}
487
488/* ----------------------------------------------------------------
489 * ExecIndexOnlyRestrPos
490 * ----------------------------------------------------------------
491 */
492void
494{
495 EState *estate = node->ss.ps.state;
496 EPQState *epqstate = estate->es_epq_active;
497
498 if (estate->es_epq_active != NULL)
499 {
500 /* See comments in ExecIndexMarkPos */
501 Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
502
503 Assert(scanrelid > 0);
504 if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
505 epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
506 {
507 /* Verify the claim above */
508 if (!epqstate->relsubs_done[scanrelid - 1])
509 elog(ERROR, "unexpected ExecIndexOnlyRestrPos call in EPQ recheck");
510 return;
511 }
512 }
513
515}
516
517/* ----------------------------------------------------------------
518 * ExecInitIndexOnlyScan
519 *
520 * Initializes the index scan's state information, creates
521 * scan keys, and opens the base and index relations.
522 *
523 * Note: index scans have 2 sets of state information because
524 * we have to keep track of the base relation and the
525 * index relation.
526 * ----------------------------------------------------------------
527 */
529ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags)
530{
533 Relation indexRelation;
534 LOCKMODE lockmode;
535 TupleDesc tupDesc;
536 int indnkeyatts;
537 int namecount;
538
539 /*
540 * create state structure
541 */
543 indexstate->ss.ps.plan = (Plan *) node;
544 indexstate->ss.ps.state = estate;
545 indexstate->ss.ps.ExecProcNode = ExecIndexOnlyScan;
546
547 /*
548 * Miscellaneous initialization
549 *
550 * create expression context for node
551 */
552 ExecAssignExprContext(estate, &indexstate->ss.ps);
553
554 /*
555 * open the scan relation
556 */
557 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
558
559 indexstate->ss.ss_currentRelation = currentRelation;
560 indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
561
562 /*
563 * Build the scan tuple type using the indextlist generated by the
564 * planner. We use this, rather than the index's physical tuple
565 * descriptor, because the latter contains storage column types not the
566 * types of the original datums. (It's the AM's responsibility to return
567 * suitable data anyway.)
568 */
569 tupDesc = ExecTypeFromTL(node->indextlist);
570 ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc,
572 0);
573
574 /*
575 * We need another slot, in a format that's suitable for the table AM, for
576 * when we need to fetch a tuple from the table for rechecking visibility.
577 */
578 indexstate->ioss_TableSlot =
582
583 /*
584 * Initialize result type and projection info. The node's targetlist will
585 * contain Vars with varno = INDEX_VAR, referencing the scan tuple.
586 */
589
590 /*
591 * initialize child expressions
592 *
593 * Note: we don't initialize all of the indexorderby expression, only the
594 * sub-parts corresponding to runtime keys (see below).
595 */
596 indexstate->ss.ps.qual =
597 ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
598 indexstate->recheckqual =
600
601 /*
602 * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
603 * here. This allows an index-advisor plugin to EXPLAIN a plan containing
604 * references to nonexistent indexes.
605 */
606 if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
607 return indexstate;
608
609 /* Open the index relation. */
610 lockmode = exec_rt_fetch(node->scan.scanrelid, estate)->rellockmode;
611 indexRelation = index_open(node->indexid, lockmode);
612 indexstate->ioss_RelationDesc = indexRelation;
613
614 /*
615 * Initialize index-specific scan state
616 */
617 indexstate->ioss_RuntimeKeysReady = false;
618 indexstate->ioss_RuntimeKeys = NULL;
619 indexstate->ioss_NumRuntimeKeys = 0;
620
621 /*
622 * build the index scan keys from the index qualification
623 */
625 indexRelation,
626 node->indexqual,
627 false,
628 &indexstate->ioss_ScanKeys,
629 &indexstate->ioss_NumScanKeys,
630 &indexstate->ioss_RuntimeKeys,
631 &indexstate->ioss_NumRuntimeKeys,
632 NULL, /* no ArrayKeys */
633 NULL);
634
635 /*
636 * any ORDER BY exprs have to be turned into scankeys in the same way
637 */
639 indexRelation,
640 node->indexorderby,
641 true,
642 &indexstate->ioss_OrderByKeys,
643 &indexstate->ioss_NumOrderByKeys,
644 &indexstate->ioss_RuntimeKeys,
645 &indexstate->ioss_NumRuntimeKeys,
646 NULL, /* no ArrayKeys */
647 NULL);
648
649 /*
650 * If we have runtime keys, we need an ExprContext to evaluate them. The
651 * node's standard context won't do because we want to reset that context
652 * for every tuple. So, build another context just like the other one...
653 * -tgl 7/11/00
654 */
655 if (indexstate->ioss_NumRuntimeKeys != 0)
656 {
657 ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
658
659 ExecAssignExprContext(estate, &indexstate->ss.ps);
660 indexstate->ioss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
661 indexstate->ss.ps.ps_ExprContext = stdecontext;
662 }
663 else
664 {
665 indexstate->ioss_RuntimeContext = NULL;
666 }
667
668 indexstate->ioss_NameCStringAttNums = NULL;
669 indnkeyatts = indexRelation->rd_index->indnkeyatts;
670 namecount = 0;
671
672 /*
673 * The "name" type for btree uses text_ops which results in storing
674 * cstrings in the indexed keys rather than names. Here we detect that in
675 * a generic way in case other index AMs want to do the same optimization.
676 * Check for opclasses with an opcintype of NAMEOID and an index tuple
677 * descriptor with CSTRINGOID. If any of these are found, create an array
678 * marking the index attribute number of each of them. StoreIndexTuple()
679 * handles copying the name Datums into a NAMEDATALEN-byte allocation.
680 */
681
682 /* First, count the number of such index keys */
683 for (int attnum = 0; attnum < indnkeyatts; attnum++)
684 {
685 if (TupleDescAttr(indexRelation->rd_att, attnum)->atttypid == CSTRINGOID &&
686 indexRelation->rd_opcintype[attnum] == NAMEOID)
687 namecount++;
688 }
689
690 if (namecount > 0)
691 {
692 int idx = 0;
693
694 /*
695 * Now create an array to mark the attribute numbers of the keys that
696 * need to be converted from cstring to name.
697 */
698 indexstate->ioss_NameCStringAttNums = palloc_array(AttrNumber, namecount);
699
700 for (int attnum = 0; attnum < indnkeyatts; attnum++)
701 {
702 if (TupleDescAttr(indexRelation->rd_att, attnum)->atttypid == CSTRINGOID &&
703 indexRelation->rd_opcintype[attnum] == NAMEOID)
704 indexstate->ioss_NameCStringAttNums[idx++] = (AttrNumber) attnum;
705 }
706 }
707
708 indexstate->ioss_NameCStringCount = namecount;
709
710 /*
711 * all done.
712 */
713 return indexstate;
714}
715
716/* ----------------------------------------------------------------
717 * Parallel Index-only Scan Support
718 * ----------------------------------------------------------------
719 */
720
721/* ----------------------------------------------------------------
722 * ExecIndexOnlyScanEstimate
723 *
724 * Compute the amount of space we'll need in the parallel
725 * query DSM, and inform pcxt->estimator about our needs.
726 * ----------------------------------------------------------------
727 */
728void
730 ParallelContext *pcxt)
731{
732 EState *estate = node->ss.ps.state;
733 bool instrument = (node->ss.ps.instrument != NULL);
734 bool parallel_aware = node->ss.ps.plan->parallel_aware;
735
736 if (!instrument && !parallel_aware)
737 {
738 /* No DSM required by the scan */
739 return;
740 }
741
743 node->ioss_NumScanKeys,
745 estate->es_snapshot,
746 instrument, parallel_aware,
747 pcxt->nworkers);
750}
751
752/* ----------------------------------------------------------------
753 * ExecIndexOnlyScanInitializeDSM
754 *
755 * Set up a parallel index-only scan descriptor.
756 * ----------------------------------------------------------------
757 */
758void
760 ParallelContext *pcxt)
761{
762 EState *estate = node->ss.ps.state;
764 bool instrument = node->ss.ps.instrument != NULL;
765 bool parallel_aware = node->ss.ps.plan->parallel_aware;
766
767 if (!instrument && !parallel_aware)
768 {
769 /* No DSM required by the scan */
770 return;
771 }
772
775 node->ioss_RelationDesc,
776 estate->es_snapshot,
777 instrument, parallel_aware, pcxt->nworkers,
778 &node->ioss_SharedInfo, piscan);
780
781 if (!parallel_aware)
782 {
783 /* Only here to initialize SharedInfo in DSM */
784 return;
785 }
786
787 node->ioss_ScanDesc =
789 node->ioss_RelationDesc,
790 &node->ioss_Instrument,
791 node->ioss_NumScanKeys,
793 piscan);
794 node->ioss_ScanDesc->xs_want_itup = true;
796
797 /*
798 * If no run-time keys to calculate or they are ready, go ahead and pass
799 * the scankeys to the index AM.
800 */
801 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
803 node->ioss_ScanKeys, node->ioss_NumScanKeys,
805}
806
807/* ----------------------------------------------------------------
808 * ExecIndexOnlyScanReInitializeDSM
809 *
810 * Reset shared state before beginning a fresh scan.
811 * ----------------------------------------------------------------
812 */
813void
820
821/* ----------------------------------------------------------------
822 * ExecIndexOnlyScanInitializeWorker
823 *
824 * Copy relevant information from TOC into planstate.
825 * ----------------------------------------------------------------
826 */
827void
830{
832 bool instrument = node->ss.ps.instrument != NULL;
833 bool parallel_aware = node->ss.ps.plan->parallel_aware;
834
835 if (!instrument && !parallel_aware)
836 {
837 /* No DSM required by the scan */
838 return;
839 }
840
841 piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
842
843 if (instrument)
845 OffsetToPointer(piscan, piscan->ps_offset_ins);
846
847 if (!parallel_aware)
848 {
849 /* Only here to set up worker node's SharedInfo */
850 return;
851 }
852
853 node->ioss_ScanDesc =
855 node->ioss_RelationDesc,
856 &node->ioss_Instrument,
857 node->ioss_NumScanKeys,
859 piscan);
860 node->ioss_ScanDesc->xs_want_itup = true;
861
862 /*
863 * If no run-time keys to calculate or they are ready, go ahead and pass
864 * the scankeys to the index AM.
865 */
866 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
868 node->ioss_ScanKeys, node->ioss_NumScanKeys,
870}
871
872/* ----------------------------------------------------------------
873 * ExecIndexOnlyScanRetrieveInstrumentation
874 *
875 * Transfer index-only scan statistics from DSM to private memory.
876 * ----------------------------------------------------------------
877 */
878void
880{
882 size_t size;
883
884 if (SharedInfo == NULL)
885 return;
886
887 /* Create a copy of SharedInfo in backend-local memory */
888 size = offsetof(SharedIndexScanInstrumentation, winstrument) +
890 node->ioss_SharedInfo = palloc(size);
891 memcpy(node->ioss_SharedInfo, SharedInfo, size);
892}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
int16 AttrNumber
Definition attnum.h:21
int ParallelWorkerNumber
Definition parallel.c:117
#define InvalidBuffer
Definition buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5505
#define OffsetToPointer(base, offset)
Definition c.h:857
NameData * Name
Definition c.h:835
#define Assert(condition)
Definition c.h:945
#define unlikely(x)
Definition c.h:432
unsigned int Index
Definition c.h:700
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
void ExecReScan(PlanState *node)
Definition execAmi.c:78
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:250
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, int varno)
Definition execScan.c:94
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition execScan.c:47
void ExecScanReScan(ScanState *node)
Definition execScan.c:108
const TupleTableSlotOps TTSOpsVirtual
Definition execTuples.c:84
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops, uint16 flags)
void ExecInitResultTypeTL(PlanState *planstate)
TupleTableSlot * ExecAllocTableSlot(List **tupleTable, TupleDesc desc, const TupleTableSlotOps *tts_ops, uint16 flags)
TupleDesc ExecTypeFromTL(List *targetList)
void ExecForceStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition execUtils.c:747
#define InstrCountTuples2(node, delta)
Definition execnodes.h:1276
#define InstrCountFiltered2(node, delta)
Definition execnodes.h:1286
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition executor.h:701
#define ResetExprContext(econtext)
Definition executor.h:654
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition executor.h:583
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition executor.h:549
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition executor.h:582
#define EXEC_FLAG_EXPLAIN_ONLY
Definition executor.h:67
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define IsParallelWorker()
Definition parallel.h:62
void index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, bool instrument, bool parallel_aware, int nworkers, SharedIndexScanInstrumentation **sharedinfo, ParallelIndexScanDesc target)
Definition indexam.c:520
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, IndexScanInstrumentation *instrument, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition indexam.c:593
void index_restrpos(IndexScanDesc scan)
Definition indexam.c:446
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, IndexScanInstrumentation *instrument, int nkeys, int norderbys)
Definition indexam.c:256
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:177
ItemPointer index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
Definition indexam.c:631
bool index_fetch_heap(IndexScanDesc scan, TupleTableSlot *slot)
Definition indexam.c:689
void index_markpos(IndexScanDesc scan)
Definition indexam.c:422
void index_endscan(IndexScanDesc scan)
Definition indexam.c:392
Size index_parallelscan_estimate(Relation indexRelation, int nkeys, int norderbys, Snapshot snapshot, bool instrument, bool parallel_aware, int nworkers)
Definition indexam.c:471
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:133
void index_parallelrescan(IndexScanDesc scan)
Definition indexam.c:575
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition indexam.c:366
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition indextuple.c:364
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103
int LOCKMODE
Definition lockdefs.h:26
#define NoLock
Definition lockdefs.h:34
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void * palloc(Size size)
Definition mcxt.c:1387
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
void namestrcpy(Name name, const char *str)
Definition name.c:233
void ExecEndIndexOnlyScan(IndexOnlyScanState *node)
static TupleTableSlot * IndexOnlyNext(IndexOnlyScanState *node)
static void StoreIndexTuple(IndexOnlyScanState *node, TupleTableSlot *slot, IndexTuple itup, TupleDesc itupdesc)
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecReScanIndexOnlyScan(IndexOnlyScanState *node)
void ExecIndexOnlyScanRetrieveInstrumentation(IndexOnlyScanState *node)
void ExecIndexOnlyRestrPos(IndexOnlyScanState *node)
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
static TupleTableSlot * ExecIndexOnlyScan(PlanState *pstate)
static bool IndexOnlyRecheck(IndexOnlyScanState *node, TupleTableSlot *slot)
IndexOnlyScanState * ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags)
void ExecIndexOnlyMarkPos(IndexOnlyScanState *node)
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexBuildScanKeys(PlanState *planstate, Relation index, List *quals, bool isorderby, ScanKey *scanKeys, int *numScanKeys, IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys, IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
void ExecIndexEvalRuntimeKeys(ExprContext *econtext, IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
static char * errmsg
int16 attnum
#define NAMEDATALEN
static char * DatumGetCString(Datum X)
Definition postgres.h:355
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:393
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition predicate.c:2608
static int fb(int x)
#define INDEX_VAR
Definition primnodes.h:245
#define RelationGetDescr(relation)
Definition rel.h:540
#define ScanDirectionCombine(a, b)
Definition sdir.h:36
ScanDirection
Definition sdir.h:25
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition shm_toc.c:171
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:232
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
ExecAuxRowMark ** relsubs_rowmark
Definition execnodes.h:1352
TupleTableSlot ** relsubs_slot
Definition execnodes.h:1324
bool * relsubs_done
Definition execnodes.h:1359
List * es_tupleTable
Definition execnodes.h:724
ScanDirection es_direction
Definition execnodes.h:671
struct EPQState * es_epq_active
Definition execnodes.h:754
Snapshot es_snapshot
Definition execnodes.h:672
MemoryContext ecxt_per_tuple_memory
Definition execnodes.h:292
TupleTableSlot * ecxt_scantuple
Definition execnodes.h:284
SharedIndexScanInstrumentation * ioss_SharedInfo
Definition execnodes.h:1787
TupleTableSlot * ioss_TableSlot
Definition execnodes.h:1788
ExprState * recheckqual
Definition execnodes.h:1775
struct IndexScanDescData * ioss_ScanDesc
Definition execnodes.h:1785
ScanKeyData * ioss_OrderByKeys
Definition execnodes.h:1778
ScanKeyData * ioss_ScanKeys
Definition execnodes.h:1776
ExprContext * ioss_RuntimeContext
Definition execnodes.h:1783
AttrNumber * ioss_NameCStringAttNums
Definition execnodes.h:1791
Relation ioss_RelationDesc
Definition execnodes.h:1784
IndexScanInstrumentation ioss_Instrument
Definition execnodes.h:1786
IndexRuntimeKeyInfo * ioss_RuntimeKeys
Definition execnodes.h:1780
List * indexqual
Definition plannodes.h:656
List * recheckqual
Definition plannodes.h:658
List * indextlist
Definition plannodes.h:662
List * indexorderby
Definition plannodes.h:660
bool xs_heap_continue
Definition relscan.h:174
HeapTuple xs_hitup
Definition relscan.h:170
bool xs_recheckorderby
Definition relscan.h:189
IndexTuple xs_itup
Definition relscan.h:168
struct TupleDescData * xs_hitupdesc
Definition relscan.h:171
struct TupleDescData * xs_itupdesc
Definition relscan.h:169
Relation heapRelation
Definition relscan.h:137
shm_toc_estimator estimator
Definition parallel.h:43
shm_toc * toc
Definition parallel.h:46
Instrumentation * instrument
Definition execnodes.h:1187
Plan * plan
Definition execnodes.h:1177
EState * state
Definition execnodes.h:1179
ExprContext * ps_ExprContext
Definition execnodes.h:1216
bool parallel_aware
Definition plannodes.h:217
int plan_node_id
Definition plannodes.h:231
Oid * rd_opcintype
Definition rel.h:208
TupleDesc rd_att
Definition rel.h:112
Form_pg_index rd_index
Definition rel.h:192
Relation ss_currentRelation
Definition execnodes.h:1634
TupleTableSlot * ss_ScanTupleSlot
Definition execnodes.h:1636
PlanState ps
Definition execnodes.h:1633
Index scanrelid
Definition plannodes.h:540
IndexScanInstrumentation winstrument[FLEXIBLE_ARRAY_MEMBER]
TupleDesc tts_tupleDescriptor
Definition tuptable.h:129
bool * tts_isnull
Definition tuptable.h:133
Datum * tts_values
Definition tuptable.h:131
Definition c.h:832
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition tableam.c:59
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:476
#define VM_ALL_VISIBLE(r, b, v)
const char * name