PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeIndexonlyscan.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "access/tupdesc.h"
#include "access/visibilitymap.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "executor/instrument.h"
#include "executor/nodeIndexonlyscan.h"
#include "executor/nodeIndexscan.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/predicate.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for nodeIndexonlyscan.c:

Go to the source code of this file.

Functions

static TupleTableSlotIndexOnlyNext (IndexOnlyScanState *node)
 
static void StoreIndexTuple (IndexOnlyScanState *node, TupleTableSlot *slot, IndexScanDesc scandesc)
 
static bool IndexOnlyRecheck (IndexOnlyScanState *node, TupleTableSlot *slot)
 
static TupleTableSlotExecIndexOnlyScan (PlanState *pstate)
 
void ExecReScanIndexOnlyScan (IndexOnlyScanState *node)
 
void ExecEndIndexOnlyScan (IndexOnlyScanState *node)
 
void ExecIndexOnlyMarkPos (IndexOnlyScanState *node)
 
void ExecIndexOnlyRestrPos (IndexOnlyScanState *node)
 
IndexOnlyScanStateExecInitIndexOnlyScan (IndexOnlyScan *node, EState *estate, int eflags)
 
void ExecIndexOnlyScanEstimate (IndexOnlyScanState *node, ParallelContext *pcxt)
 
void ExecIndexOnlyScanInitializeDSM (IndexOnlyScanState *node, ParallelContext *pcxt)
 
void ExecIndexOnlyScanReInitializeDSM (IndexOnlyScanState *node, ParallelContext *pcxt)
 
void ExecIndexOnlyScanInitializeWorker (IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
 
void ExecIndexOnlyScanInstrumentEstimate (IndexOnlyScanState *node, ParallelContext *pcxt)
 
void ExecIndexOnlyScanInstrumentInitDSM (IndexOnlyScanState *node, ParallelContext *pcxt)
 
void ExecIndexOnlyScanInstrumentInitWorker (IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
 
void ExecIndexOnlyScanRetrieveInstrumentation (IndexOnlyScanState *node)
 

Function Documentation

◆ ExecEndIndexOnlyScan()

void ExecEndIndexOnlyScan ( IndexOnlyScanState node)

Definition at line 407 of file nodeIndexonlyscan.c.

408{
411
412 /*
413 * extract information from the node
414 */
417
418 /* Release VM buffer pin, if any. */
419 if (node->ioss_VMBuffer != InvalidBuffer)
420 {
423 }
424
425 /*
426 * When ending a parallel worker, copy the statistics gathered by the
427 * worker back into shared memory so that it can be picked up by the main
428 * process to report in EXPLAIN ANALYZE
429 */
430 if (node->ioss_SharedInfo != NULL && IsParallelWorker())
431 {
432 IndexScanInstrumentation *winstrument;
433
434 Assert(ParallelWorkerNumber < node->ioss_SharedInfo->num_workers);
435 winstrument = &node->ioss_SharedInfo->winstrument[ParallelWorkerNumber];
436
437 /*
438 * We have to accumulate the stats rather than performing a memcpy.
439 * When a Gather/GatherMerge node finishes it will perform planner
440 * shutdown on the workers. On rescan it will spin up new workers
441 * which will have a new IndexOnlyScanState and zeroed stats.
442 */
443 winstrument->nsearches += node->ioss_Instrument->nsearches;
444 }
445
446 /*
447 * close the index relation (no-op if we didn't open it)
448 */
449 if (indexScanDesc)
453}
int ParallelWorkerNumber
Definition parallel.c:117
#define InvalidBuffer
Definition buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5609
#define Assert(condition)
Definition c.h:1002
#define IsParallelWorker()
Definition parallel.h:62
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
void index_endscan(IndexScanDesc scan)
Definition indexam.c:394
#define NoLock
Definition lockdefs.h:34
static int fb(int x)
SharedIndexScanInstrumentation * ioss_SharedInfo
Definition execnodes.h:1817
IndexScanInstrumentation * ioss_Instrument
Definition execnodes.h:1816
struct IndexScanDescData * ioss_ScanDesc
Definition execnodes.h:1815
Relation ioss_RelationDesc
Definition execnodes.h:1814
IndexScanInstrumentation winstrument[FLEXIBLE_ARRAY_MEMBER]

References Assert, fb(), index_close(), index_endscan(), InvalidBuffer, IndexOnlyScanState::ioss_Instrument, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_SharedInfo, IndexOnlyScanState::ioss_VMBuffer, IsParallelWorker, NoLock, IndexScanInstrumentation::nsearches, ParallelWorkerNumber, ReleaseBuffer(), and SharedIndexScanInstrumentation::winstrument.

Referenced by ExecEndNode().

◆ ExecIndexOnlyMarkPos()

void ExecIndexOnlyMarkPos ( IndexOnlyScanState node)

Definition at line 463 of file nodeIndexonlyscan.c.

464{
465 EState *estate = node->ss.ps.state;
466 EPQState *epqstate = estate->es_epq_active;
467
468 if (epqstate != NULL)
469 {
470 /*
471 * We are inside an EvalPlanQual recheck. If a test tuple exists for
472 * this relation, then we shouldn't access the index at all. We would
473 * instead need to save, and later restore, the state of the
474 * relsubs_done flag, so that re-fetching the test tuple is possible.
475 * However, given the assumption that no caller sets a mark at the
476 * start of the scan, we can only get here with relsubs_done[i]
477 * already set, and so no state need be saved.
478 */
479 Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
480
481 Assert(scanrelid > 0);
482 if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
483 epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
484 {
485 /* Verify the claim above */
486 if (!epqstate->relsubs_done[scanrelid - 1])
487 elog(ERROR, "unexpected ExecIndexOnlyMarkPos call in EPQ recheck");
488 return;
489 }
490 }
491
493}
unsigned int Index
Definition c.h:757
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
void index_markpos(IndexScanDesc scan)
Definition indexam.c:424
ExecAuxRowMark ** relsubs_rowmark
Definition execnodes.h:1379
TupleTableSlot ** relsubs_slot
Definition execnodes.h:1351
bool * relsubs_done
Definition execnodes.h:1386
struct EPQState * es_epq_active
Definition execnodes.h:779
Plan * plan
Definition execnodes.h:1202
EState * state
Definition execnodes.h:1204
PlanState ps
Definition execnodes.h:1662

References Assert, elog, ERROR, EState::es_epq_active, fb(), index_markpos(), IndexOnlyScanState::ioss_ScanDesc, PlanState::plan, ScanState::ps, EPQState::relsubs_done, EPQState::relsubs_rowmark, EPQState::relsubs_slot, IndexOnlyScanState::ss, and PlanState::state.

Referenced by ExecMarkPos().

◆ ExecIndexOnlyRestrPos()

void ExecIndexOnlyRestrPos ( IndexOnlyScanState node)

Definition at line 500 of file nodeIndexonlyscan.c.

501{
502 EState *estate = node->ss.ps.state;
503 EPQState *epqstate = estate->es_epq_active;
504
505 if (estate->es_epq_active != NULL)
506 {
507 /* See comments in ExecIndexMarkPos */
508 Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
509
510 Assert(scanrelid > 0);
511 if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
512 epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
513 {
514 /* Verify the claim above */
515 if (!epqstate->relsubs_done[scanrelid - 1])
516 elog(ERROR, "unexpected ExecIndexOnlyRestrPos call in EPQ recheck");
517 return;
518 }
519 }
520
522}
void index_restrpos(IndexScanDesc scan)
Definition indexam.c:448

References Assert, elog, ERROR, EState::es_epq_active, fb(), index_restrpos(), IndexOnlyScanState::ioss_ScanDesc, PlanState::plan, ScanState::ps, EPQState::relsubs_done, EPQState::relsubs_rowmark, EPQState::relsubs_slot, IndexOnlyScanState::ss, and PlanState::state.

Referenced by ExecRestrPos().

◆ ExecIndexOnlyScan()

static TupleTableSlot * ExecIndexOnlyScan ( PlanState pstate)
static

Definition at line 345 of file nodeIndexonlyscan.c.

346{
348
349 /*
350 * If we have runtime keys and they've not already been set up, do it now.
351 */
352 if (node->ioss_NumRuntimeKeys != 0 && !node->ioss_RuntimeKeysReady)
353 ExecReScan((PlanState *) node);
354
355 return ExecScan(&node->ss,
358}
void ExecReScan(PlanState *node)
Definition execAmi.c:78
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition execScan.c:47
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition executor.h:588
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition executor.h:587
static TupleTableSlot * IndexOnlyNext(IndexOnlyScanState *node)
static bool IndexOnlyRecheck(IndexOnlyScanState *node, TupleTableSlot *slot)
#define castNode(_type_, nodeptr)
Definition nodes.h:180

References castNode, ExecReScan(), ExecScan(), IndexOnlyNext(), IndexOnlyRecheck(), IndexOnlyScanState::ioss_NumRuntimeKeys, IndexOnlyScanState::ioss_RuntimeKeysReady, and IndexOnlyScanState::ss.

Referenced by ExecInitIndexOnlyScan().

◆ ExecIndexOnlyScanEstimate()

void ExecIndexOnlyScanEstimate ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 740 of file nodeIndexonlyscan.c.

742{
743 EState *estate = node->ss.ps.state;
744
746 node->ioss_NumScanKeys,
748 estate->es_snapshot);
751}
Size index_parallelscan_estimate(Relation indexRelation, int nkeys, int norderbys, Snapshot snapshot)
Definition indexam.c:470
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
Snapshot es_snapshot
Definition execnodes.h:697
shm_toc_estimator estimator
Definition parallel.h:43

References EState::es_snapshot, ParallelContext::estimator, index_parallelscan_estimate(), IndexOnlyScanState::ioss_NumOrderByKeys, IndexOnlyScanState::ioss_NumScanKeys, IndexOnlyScanState::ioss_PscanLen, IndexOnlyScanState::ioss_RelationDesc, ScanState::ps, shm_toc_estimate_chunk, shm_toc_estimate_keys, IndexOnlyScanState::ss, and PlanState::state.

Referenced by ExecParallelEstimate().

◆ ExecIndexOnlyScanInitializeDSM()

void ExecIndexOnlyScanInitializeDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 760 of file nodeIndexonlyscan.c.

762{
763 EState *estate = node->ss.ps.state;
765
768 node->ioss_RelationDesc,
769 estate->es_snapshot,
770 piscan);
772
773 node->ioss_ScanDesc =
775 node->ioss_RelationDesc,
776 node->ioss_Instrument,
777 node->ioss_NumScanKeys,
779 piscan,
780 ScanRelIsReadOnly(&node->ss) ?
782 node->ioss_ScanDesc->xs_want_itup = true;
784
785 /*
786 * If no run-time keys to calculate or they are ready, go ahead and pass
787 * the scankeys to the index AM.
788 */
789 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
791 node->ioss_ScanKeys, node->ioss_NumScanKeys,
793}
bool ScanRelIsReadOnly(ScanState *ss)
Definition execUtils.c:751
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, IndexScanInstrumentation *instrument, int nkeys, int norderbys, ParallelIndexScanDesc pscan, uint32 flags)
Definition indexam.c:560
void index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, ParallelIndexScanDesc target)
Definition indexam.c:505
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition indexam.c:368
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:169
ScanKeyData * ioss_OrderByKeys
Definition execnodes.h:1808
ScanKeyData * ioss_ScanKeys
Definition execnodes.h:1806
shm_toc * toc
Definition parallel.h:46
int plan_node_id
Definition plannodes.h:233
Relation ss_currentRelation
Definition execnodes.h:1663
@ SO_HINT_REL_READ_ONLY
Definition tableam.h:71
@ SO_NONE
Definition tableam.h:49

References EState::es_snapshot, fb(), index_beginscan_parallel(), index_parallelscan_initialize(), index_rescan(), InvalidBuffer, IndexOnlyScanState::ioss_Instrument, IndexOnlyScanState::ioss_NumOrderByKeys, IndexOnlyScanState::ioss_NumRuntimeKeys, IndexOnlyScanState::ioss_NumScanKeys, IndexOnlyScanState::ioss_OrderByKeys, IndexOnlyScanState::ioss_PscanLen, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_RuntimeKeysReady, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_ScanKeys, IndexOnlyScanState::ioss_VMBuffer, PlanState::plan, Plan::plan_node_id, ScanState::ps, ScanRelIsReadOnly(), shm_toc_allocate(), shm_toc_insert(), SO_HINT_REL_READ_ONLY, SO_NONE, IndexOnlyScanState::ss, ScanState::ss_currentRelation, PlanState::state, ParallelContext::toc, and IndexScanDescData::xs_want_itup.

Referenced by ExecParallelInitializeDSM().

◆ ExecIndexOnlyScanInitializeWorker()

void ExecIndexOnlyScanInitializeWorker ( IndexOnlyScanState node,
ParallelWorkerContext pwcxt 
)

Definition at line 816 of file nodeIndexonlyscan.c.

818{
820
821 piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
822
823 node->ioss_ScanDesc =
825 node->ioss_RelationDesc,
826 node->ioss_Instrument,
827 node->ioss_NumScanKeys,
829 piscan,
830 ScanRelIsReadOnly(&node->ss) ?
832 node->ioss_ScanDesc->xs_want_itup = true;
833
834 /*
835 * If no run-time keys to calculate or they are ready, go ahead and pass
836 * the scankeys to the index AM.
837 */
838 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
840 node->ioss_ScanKeys, node->ioss_NumScanKeys,
842}
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:236

References fb(), index_beginscan_parallel(), index_rescan(), IndexOnlyScanState::ioss_Instrument, IndexOnlyScanState::ioss_NumOrderByKeys, IndexOnlyScanState::ioss_NumRuntimeKeys, IndexOnlyScanState::ioss_NumScanKeys, IndexOnlyScanState::ioss_OrderByKeys, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_RuntimeKeysReady, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_ScanKeys, PlanState::plan, Plan::plan_node_id, ScanState::ps, ScanRelIsReadOnly(), shm_toc_lookup(), SO_HINT_REL_READ_ONLY, SO_NONE, IndexOnlyScanState::ss, ScanState::ss_currentRelation, and IndexScanDescData::xs_want_itup.

Referenced by ExecParallelInitializeWorker().

◆ ExecIndexOnlyScanInstrumentEstimate()

void ExecIndexOnlyScanInstrumentEstimate ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 849 of file nodeIndexonlyscan.c.

851{
852 Size size;
853
854 if (!node->ss.ps.instrument || pcxt->nworkers == 0)
855 return;
856
857 /*
858 * This size calculation is trivial enough that we don't bother saving it
859 * in the IndexOnlyScanState. We'll recalculate the needed size in
860 * ExecIndexOnlyScanInstrumentInitDSM().
861 */
864 shm_toc_estimate_chunk(&pcxt->estimator, size);
866}
size_t Size
Definition c.h:748
Size add_size(Size s1, Size s2)
Definition mcxt.c:1733
Size mul_size(Size s1, Size s2)
Definition mcxt.c:1752
NodeInstrumentation * instrument
Definition execnodes.h:1212

References add_size(), ParallelContext::estimator, fb(), PlanState::instrument, mul_size(), ParallelContext::nworkers, ScanState::ps, shm_toc_estimate_chunk, shm_toc_estimate_keys, and IndexOnlyScanState::ss.

Referenced by ExecParallelEstimate().

◆ ExecIndexOnlyScanInstrumentInitDSM()

void ExecIndexOnlyScanInstrumentInitDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

◆ ExecIndexOnlyScanInstrumentInitWorker()

◆ ExecIndexOnlyScanReInitializeDSM()

void ExecIndexOnlyScanReInitializeDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

◆ ExecIndexOnlyScanRetrieveInstrumentation()

void ExecIndexOnlyScanRetrieveInstrumentation ( IndexOnlyScanState node)

Definition at line 918 of file nodeIndexonlyscan.c.

919{
921 size_t size;
922
923 if (SharedInfo == NULL)
924 return;
925
926 /* Create a copy of SharedInfo in backend-local memory */
927 size = offsetof(SharedIndexScanInstrumentation, winstrument) +
929 node->ioss_SharedInfo = palloc(size);
930 memcpy(node->ioss_SharedInfo, SharedInfo, size);
931}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
void * palloc(Size size)
Definition mcxt.c:1390

References fb(), IndexOnlyScanState::ioss_SharedInfo, memcpy(), SharedIndexScanInstrumentation::num_workers, and palloc().

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecInitIndexOnlyScan()

IndexOnlyScanState * ExecInitIndexOnlyScan ( IndexOnlyScan node,
EState estate,
int  eflags 
)

Definition at line 536 of file nodeIndexonlyscan.c.

537{
540 Relation indexRelation;
541 LOCKMODE lockmode;
542 TupleDesc tupDesc;
543 int indnkeyatts;
544 int namecount;
545
546 /*
547 * create state structure
548 */
550 indexstate->ss.ps.plan = (Plan *) node;
551 indexstate->ss.ps.state = estate;
552 indexstate->ss.ps.ExecProcNode = ExecIndexOnlyScan;
553
554 /*
555 * Miscellaneous initialization
556 *
557 * create expression context for node
558 */
559 ExecAssignExprContext(estate, &indexstate->ss.ps);
560
561 /*
562 * open the scan relation
563 */
564 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
565
566 indexstate->ss.ss_currentRelation = currentRelation;
567 indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
568
569 /*
570 * Build the scan tuple type using the indextlist generated by the
571 * planner. We use this, rather than the index's physical tuple
572 * descriptor, because the latter contains storage column types not the
573 * types of the original datums. (It's the AM's responsibility to return
574 * suitable data anyway.)
575 */
576 tupDesc = ExecTypeFromTL(node->indextlist);
577 ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc,
579 0);
580
581 /*
582 * We need another slot, in a format that's suitable for the table AM, for
583 * when we need to fetch a tuple from the table for rechecking visibility.
584 */
585 indexstate->ioss_TableSlot =
589
590 /*
591 * Initialize result type and projection info. The node's targetlist will
592 * contain Vars with varno = INDEX_VAR, referencing the scan tuple.
593 */
596
597 /*
598 * initialize child expressions
599 *
600 * Note: we don't initialize all of the indexorderby expression, only the
601 * sub-parts corresponding to runtime keys (see below).
602 */
603 indexstate->ss.ps.qual =
604 ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
605 indexstate->recheckqual =
607
608 /*
609 * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
610 * here. This allows an index-advisor plugin to EXPLAIN a plan containing
611 * references to nonexistent indexes.
612 */
613 if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
614 return indexstate;
615
616 /* Set up instrumentation of index-only scans if requested */
617 if (estate->es_instrument)
619
620 /* Open the index relation. */
621 lockmode = exec_rt_fetch(node->scan.scanrelid, estate)->rellockmode;
622 indexRelation = index_open(node->indexid, lockmode);
623 indexstate->ioss_RelationDesc = indexRelation;
624
625 /*
626 * Initialize index-specific scan state
627 */
628 indexstate->ioss_RuntimeKeysReady = false;
629 indexstate->ioss_RuntimeKeys = NULL;
630 indexstate->ioss_NumRuntimeKeys = 0;
631
632 /*
633 * build the index scan keys from the index qualification
634 */
636 indexRelation,
637 node->indexqual,
638 false,
639 &indexstate->ioss_ScanKeys,
640 &indexstate->ioss_NumScanKeys,
641 &indexstate->ioss_RuntimeKeys,
642 &indexstate->ioss_NumRuntimeKeys,
643 NULL, /* no ArrayKeys */
644 NULL);
645
646 /*
647 * any ORDER BY exprs have to be turned into scankeys in the same way
648 */
650 indexRelation,
651 node->indexorderby,
652 true,
653 &indexstate->ioss_OrderByKeys,
654 &indexstate->ioss_NumOrderByKeys,
655 &indexstate->ioss_RuntimeKeys,
656 &indexstate->ioss_NumRuntimeKeys,
657 NULL, /* no ArrayKeys */
658 NULL);
659
660 /*
661 * If we have runtime keys, we need an ExprContext to evaluate them. The
662 * node's standard context won't do because we want to reset that context
663 * for every tuple. So, build another context just like the other one...
664 * -tgl 7/11/00
665 */
666 if (indexstate->ioss_NumRuntimeKeys != 0)
667 {
668 ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
669
670 ExecAssignExprContext(estate, &indexstate->ss.ps);
671 indexstate->ioss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
672 indexstate->ss.ps.ps_ExprContext = stdecontext;
673 }
674 else
675 {
676 indexstate->ioss_RuntimeContext = NULL;
677 }
678
679 indexstate->ioss_NameCStringAttNums = NULL;
680 indnkeyatts = indexRelation->rd_index->indnkeyatts;
681 namecount = 0;
682
683 /*
684 * The "name" type for btree uses text_ops which results in storing
685 * cstrings in the indexed keys rather than names. Here we detect that in
686 * a generic way in case other index AMs want to do the same optimization.
687 * Check for opclasses with an opcintype of NAMEOID and an index tuple
688 * descriptor with CSTRINGOID. If any of these are found, create an array
689 * marking the index attribute number of each of them. StoreIndexTuple()
690 * handles copying the name Datums into a NAMEDATALEN-byte allocation.
691 */
692
693 /* First, count the number of such index keys */
694 for (int attnum = 0; attnum < indnkeyatts; attnum++)
695 {
696 if (TupleDescAttr(indexRelation->rd_att, attnum)->atttypid == CSTRINGOID &&
697 indexRelation->rd_opcintype[attnum] == NAMEOID)
698 namecount++;
699 }
700
701 if (namecount > 0)
702 {
703 int idx = 0;
704
705 /*
706 * Now create an array to mark the attribute numbers of the keys that
707 * need to be converted from cstring to name.
708 */
709 indexstate->ioss_NameCStringAttNums = palloc_array(AttrNumber, namecount);
710
711 for (int attnum = 0; attnum < indnkeyatts; attnum++)
712 {
713 if (TupleDescAttr(indexRelation->rd_att, attnum)->atttypid == CSTRINGOID &&
714 indexRelation->rd_opcintype[attnum] == NAMEOID)
715 indexstate->ioss_NameCStringAttNums[idx++] = (AttrNumber) attnum;
716 }
717 }
718
719 indexstate->ioss_NameCStringCount = namecount;
720
721 /*
722 * all done.
723 */
724 return indexstate;
725}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
int16 AttrNumber
Definition attnum.h:21
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:229
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, int varno)
Definition execScan.c:94
const TupleTableSlotOps TTSOpsVirtual
Definition execTuples.c:84
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 ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition execUtils.c:768
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition executor.h:708
#define EXEC_FLAG_EXPLAIN_ONLY
Definition executor.h:67
#define palloc_array(type, count)
Definition fe_memutils.h:91
#define palloc0_object(type)
Definition fe_memutils.h:90
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
int LOCKMODE
Definition lockdefs.h:26
static TupleTableSlot * ExecIndexOnlyScan(PlanState *pstate)
void ExecIndexBuildScanKeys(PlanState *planstate, Relation index, List *quals, bool isorderby, ScanKey *scanKeys, int *numScanKeys, IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys, IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
#define makeNode(_type_)
Definition nodes.h:159
int16 attnum
#define INDEX_VAR
Definition primnodes.h:245
#define RelationGetDescr(relation)
Definition rel.h:542
int es_instrument
Definition execnodes.h:757
List * es_tupleTable
Definition execnodes.h:749
List * indexqual
Definition plannodes.h:660
List * recheckqual
Definition plannodes.h:662
List * indextlist
Definition plannodes.h:666
List * indexorderby
Definition plannodes.h:664
Oid * rd_opcintype
Definition rel.h:208
TupleDesc rd_att
Definition rel.h:112
Form_pg_index rd_index
Definition rel.h:192
Index scanrelid
Definition plannodes.h:544
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition tableam.c:59
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References attnum, EState::es_instrument, EState::es_tupleTable, EXEC_FLAG_EXPLAIN_ONLY, exec_rt_fetch(), ExecAllocTableSlot(), ExecAssignExprContext(), ExecAssignScanProjectionInfoWithVarno(), ExecIndexBuildScanKeys(), ExecIndexOnlyScan(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), ExecTypeFromTL(), fb(), idx(), index_open(), INDEX_VAR, IndexOnlyScan::indexid, IndexOnlyScan::indexorderby, IndexOnlyScan::indexqual, IndexOnlyScan::indextlist, makeNode, palloc0_object, palloc_array, RelationData::rd_att, RelationData::rd_index, RelationData::rd_opcintype, IndexOnlyScan::recheckqual, RelationGetDescr, IndexOnlyScan::scan, Scan::scanrelid, table_slot_callbacks(), TTSOpsVirtual, and TupleDescAttr().

Referenced by ExecInitNode().

◆ ExecReScanIndexOnlyScan()

void ExecReScanIndexOnlyScan ( IndexOnlyScanState node)

Definition at line 372 of file nodeIndexonlyscan.c.

373{
374 /*
375 * If we are doing runtime key calculations (ie, any of the index key
376 * values weren't simple Consts), compute the new key values. But first,
377 * reset the context so we don't leak memory as each outer tuple is
378 * scanned. Note this assumes that we will recalculate *all* runtime keys
379 * on each call.
380 */
381 if (node->ioss_NumRuntimeKeys != 0)
382 {
383 ExprContext *econtext = node->ioss_RuntimeContext;
384
385 ResetExprContext(econtext);
387 node->ioss_RuntimeKeys,
388 node->ioss_NumRuntimeKeys);
389 }
390 node->ioss_RuntimeKeysReady = true;
391
392 /* reset index scan */
393 if (node->ioss_ScanDesc)
395 node->ioss_ScanKeys, node->ioss_NumScanKeys,
397
398 ExecScanReScan(&node->ss);
399}
void ExecScanReScan(ScanState *node)
Definition execScan.c:108
#define ResetExprContext(econtext)
Definition executor.h:659
void ExecIndexEvalRuntimeKeys(ExprContext *econtext, IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
ExprContext * ioss_RuntimeContext
Definition execnodes.h:1813
IndexRuntimeKeyInfo * ioss_RuntimeKeys
Definition execnodes.h:1810

References ExecIndexEvalRuntimeKeys(), ExecScanReScan(), index_rescan(), IndexOnlyScanState::ioss_NumOrderByKeys, IndexOnlyScanState::ioss_NumRuntimeKeys, IndexOnlyScanState::ioss_NumScanKeys, IndexOnlyScanState::ioss_OrderByKeys, IndexOnlyScanState::ioss_RuntimeContext, IndexOnlyScanState::ioss_RuntimeKeys, IndexOnlyScanState::ioss_RuntimeKeysReady, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_ScanKeys, ResetExprContext, and IndexOnlyScanState::ss.

Referenced by ExecReScan().

◆ IndexOnlyNext()

static TupleTableSlot * IndexOnlyNext ( IndexOnlyScanState node)
static

Definition at line 63 of file nodeIndexonlyscan.c.

64{
65 EState *estate;
66 ExprContext *econtext;
67 ScanDirection direction;
68 IndexScanDesc scandesc;
69 TupleTableSlot *slot;
70 ItemPointer tid;
71
72 /*
73 * extract necessary information from index scan node
74 */
75 estate = node->ss.ps.state;
76
77 /*
78 * Determine which direction to scan the index in based on the plan's scan
79 * direction and the current direction of execution.
80 */
81 direction = ScanDirectionCombine(estate->es_direction,
82 ((IndexOnlyScan *) node->ss.ps.plan)->indexorderdir);
83 scandesc = node->ioss_ScanDesc;
84 econtext = node->ss.ps.ps_ExprContext;
85 slot = node->ss.ss_ScanTupleSlot;
86
87 if (scandesc == NULL)
88 {
89 /*
90 * We reach here if the index only scan is not parallel, or if we're
91 * serially executing an index only scan that was planned to be
92 * parallel.
93 */
94 scandesc = index_beginscan(node->ss.ss_currentRelation,
96 estate->es_snapshot,
97 node->ioss_Instrument,
98 node->ioss_NumScanKeys,
100 ScanRelIsReadOnly(&node->ss) ?
102
103 node->ioss_ScanDesc = scandesc;
104
105
106 /* Set it up for index-only scan */
107 node->ioss_ScanDesc->xs_want_itup = true;
109
110 /*
111 * If no run-time keys to calculate or they are ready, go ahead and
112 * pass the scankeys to the index AM.
113 */
114 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
115 index_rescan(scandesc,
116 node->ioss_ScanKeys,
117 node->ioss_NumScanKeys,
118 node->ioss_OrderByKeys,
119 node->ioss_NumOrderByKeys);
120 }
121
122 /*
123 * OK, now that we have what we need, fetch the next tuple.
124 */
125 while ((tid = index_getnext_tid(scandesc, direction)) != NULL)
126 {
127 bool tuple_from_heap = false;
128
130
131 /*
132 * We can skip the heap fetch if the TID references a heap page on
133 * which all tuples are known visible to everybody. In any case,
134 * we'll use the index tuple not the heap tuple as the data source.
135 *
136 * Note on Memory Ordering Effects: visibilitymap_get_status does not
137 * lock the visibility map buffer, and therefore the result we read
138 * here could be slightly stale. However, it can't be stale enough to
139 * matter.
140 *
141 * We need to detect clearing a VM bit due to an insert right away,
142 * because the tuple is present in the index page but not visible. The
143 * reading of the TID by this scan (using a shared lock on the index
144 * buffer) is serialized with the insert of the TID into the index
145 * (using an exclusive lock on the index buffer). Because the VM bit
146 * is cleared before updating the index, and locking/unlocking of the
147 * index page acts as a full memory barrier, we are sure to see the
148 * cleared bit if we see a recently-inserted TID.
149 *
150 * Deletes do not update the index page (only VACUUM will clear out
151 * the TID), so the clearing of the VM bit by a delete is not
152 * serialized with this test below, and we may see a value that is
153 * significantly stale. However, we don't care about the delete right
154 * away, because the tuple is still visible until the deleting
155 * transaction commits or the statement ends (if it's our
156 * transaction). In either case, the lock on the VM buffer will have
157 * been released (acting as a write barrier) after clearing the bit.
158 * And for us to have a snapshot that includes the deleting
159 * transaction (making the tuple invisible), we must have acquired
160 * ProcArrayLock after that time, acting as a read barrier.
161 *
162 * It's worth going through this complexity to avoid needing to lock
163 * the VM buffer, which could cause significant contention.
164 */
165 if (!VM_ALL_VISIBLE(scandesc->heapRelation,
167 &node->ioss_VMBuffer))
168 {
169 /*
170 * Rats, we have to visit the heap to check visibility.
171 */
172 InstrCountTuples2(node, 1);
173 if (!index_fetch_heap(scandesc, node->ioss_TableSlot))
174 continue; /* no visible tuple, try next index entry */
175
177
178 /*
179 * Only MVCC snapshots are supported here, so there should be no
180 * need to keep following the HOT chain once a visible entry has
181 * been found. If we did want to allow that, we'd need to keep
182 * more state to remember not to call index_getnext_tid next time.
183 */
184 if (scandesc->xs_heap_continue)
185 elog(ERROR, "non-MVCC snapshots are not supported in index-only scans");
186
187 /*
188 * Note: at this point we are holding a pin on the heap page, as
189 * recorded in scandesc->xs_cbuf. We could release that pin now,
190 * but it's not clear whether it's a win to do so. The next index
191 * entry might require a visit to the same heap page.
192 */
193
194 tuple_from_heap = true;
195 }
196
197 /* Fill the scan tuple slot with data from the index */
198 StoreIndexTuple(node, slot, scandesc);
199
200 /*
201 * If the index was lossy, we have to recheck the index quals.
202 */
203 if (scandesc->xs_recheck)
204 {
205 econtext->ecxt_scantuple = slot;
206 if (!ExecQualAndReset(node->recheckqual, econtext))
207 {
208 /* Fails recheck, so drop it and loop back for another */
209 InstrCountFiltered2(node, 1);
210 continue;
211 }
212 }
213
214 /*
215 * We don't currently support rechecking ORDER BY distances. (In
216 * principle, if the index can support retrieval of the originally
217 * indexed value, it should be able to produce an exact distance
218 * calculation too. So it's not clear that adding code here for
219 * recheck/re-sort would be worth the trouble. But we should at least
220 * throw an error if someone tries it.)
221 */
222 if (scandesc->numberOfOrderBys > 0 && scandesc->xs_recheckorderby)
225 errmsg("lossy distance functions are not supported in index-only scans")));
226
227 /*
228 * If we didn't access the heap, then we'll need to take a predicate
229 * lock explicitly, as if we had. For now we do that at page level.
230 */
231 if (!tuple_from_heap)
234 estate->es_snapshot);
235
236 return slot;
237 }
238
239 /*
240 * if we get here it means the index scan failed so we are at the end of
241 * the scan..
242 */
243 return ExecClearTuple(slot);
244}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ereport(elevel,...)
Definition elog.h:152
#define InstrCountTuples2(node, delta)
Definition execnodes.h:1303
#define InstrCountFiltered2(node, delta)
Definition execnodes.h:1313
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition executor.h:554
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, IndexScanInstrumentation *instrument, int nkeys, int norderbys, uint32 flags)
Definition indexam.c:257
ItemPointer index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
Definition indexam.c:599
bool index_fetch_heap(IndexScanDesc scan, TupleTableSlot *slot)
Definition indexam.c:657
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
static void StoreIndexTuple(IndexOnlyScanState *node, TupleTableSlot *slot, IndexScanDesc scandesc)
static char * errmsg
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition predicate.c:2528
#define ScanDirectionCombine(a, b)
Definition sdir.h:36
ScanDirection
Definition sdir.h:25
ScanDirection es_direction
Definition execnodes.h:696
TupleTableSlot * ecxt_scantuple
Definition execnodes.h:287
TupleTableSlot * ioss_TableSlot
Definition execnodes.h:1818
ExprState * recheckqual
Definition execnodes.h:1805
bool xs_heap_continue
Definition relscan.h:186
bool xs_recheckorderby
Definition relscan.h:201
Relation heapRelation
Definition relscan.h:149
ExprContext * ps_ExprContext
Definition execnodes.h:1243
TupleTableSlot * ss_ScanTupleSlot
Definition execnodes.h:1665
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:476
#define VM_ALL_VISIBLE(r, b, v)

References CHECK_FOR_INTERRUPTS, ExprContext::ecxt_scantuple, elog, ereport, errcode(), errmsg, ERROR, EState::es_direction, EState::es_snapshot, ExecClearTuple(), ExecQualAndReset(), fb(), IndexScanDescData::heapRelation, index_beginscan(), index_fetch_heap(), index_getnext_tid(), index_rescan(), InstrCountFiltered2, InstrCountTuples2, InvalidBuffer, IndexOnlyScanState::ioss_Instrument, IndexOnlyScanState::ioss_NumOrderByKeys, IndexOnlyScanState::ioss_NumRuntimeKeys, IndexOnlyScanState::ioss_NumScanKeys, IndexOnlyScanState::ioss_OrderByKeys, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_RuntimeKeysReady, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_ScanKeys, IndexOnlyScanState::ioss_TableSlot, IndexOnlyScanState::ioss_VMBuffer, ItemPointerGetBlockNumber(), IndexScanDescData::numberOfOrderBys, PlanState::plan, PredicateLockPage(), ScanState::ps, PlanState::ps_ExprContext, IndexOnlyScanState::recheckqual, ScanDirectionCombine, ScanRelIsReadOnly(), SO_HINT_REL_READ_ONLY, SO_NONE, IndexOnlyScanState::ss, ScanState::ss_currentRelation, ScanState::ss_ScanTupleSlot, PlanState::state, StoreIndexTuple(), VM_ALL_VISIBLE, IndexScanDescData::xs_heap_continue, IndexScanDescData::xs_recheck, IndexScanDescData::xs_recheckorderby, and IndexScanDescData::xs_want_itup.

Referenced by ExecIndexOnlyScan().

◆ IndexOnlyRecheck()

static bool IndexOnlyRecheck ( IndexOnlyScanState node,
TupleTableSlot slot 
)
static

Definition at line 334 of file nodeIndexonlyscan.c.

335{
336 elog(ERROR, "EvalPlanQual recheck is not supported in index-only scans");
337 return false; /* keep compiler quiet */
338}

References elog, and ERROR.

Referenced by ExecIndexOnlyScan().

◆ StoreIndexTuple()

static void StoreIndexTuple ( IndexOnlyScanState node,
TupleTableSlot slot,
IndexScanDesc  scandesc 
)
static

Definition at line 259 of file nodeIndexonlyscan.c.

261{
262 ExecClearTuple(slot);
263
264 /*
265 * We must deform the tuple using the tupdesc the index AM formed it with
266 * (xs_hitupdesc or xs_itupdesc), not the slot's tupdesc. The datums
267 * returned by the index AM must be binary compatible, but the descriptors
268 * may align each column differently in certain rare cases. (Actually,
269 * btree's "name" opclass stores cstring tuples that _aren't_ even binary
270 * compatible, in the strictest sense. We directly handle that here.)
271 */
272 if (scandesc->xs_hitup)
273 {
274 Assert(slot->tts_tupleDescriptor->natts == scandesc->xs_hitupdesc->natts);
275
276 heap_deform_tuple(scandesc->xs_hitup, scandesc->xs_hitupdesc,
277 slot->tts_values, slot->tts_isnull);
278 }
279 else if (scandesc->xs_itup)
280 {
281 Assert(slot->tts_tupleDescriptor->natts == scandesc->xs_itupdesc->natts);
282
283 index_deform_tuple(scandesc->xs_itup, scandesc->xs_itupdesc,
284 slot->tts_values, slot->tts_isnull);
285
286 /*
287 * Copy all name columns stored as cstrings back into a NAMEDATALEN
288 * byte sized allocation. We mark this branch as unlikely as
289 * generally "name" is used only for the system catalogs and this
290 * would have to be a user query running on those or some other user
291 * table with an index on a name column.
292 */
294 {
296
297 for (int idx = 0; idx < attcount; idx++)
298 {
300 Name name;
301
302 /* skip null Datums */
303 if (slot->tts_isnull[attnum])
304 continue;
305
306 /*
307 * allocate the NAMEDATALEN and copy the datum into that
308 * memory
309 */
312
313 /* use namestrcpy to zero-pad all trailing bytes */
316 }
317 }
318 }
319 else
320 elog(ERROR, "no data returned for index-only scan");
321
323}
NameData * Name
Definition c.h:892
#define unlikely(x)
Definition c.h:497
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition heaptuple.c:1254
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition indextuple.c:364
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1235
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define NAMEDATALEN
static char * DatumGetCString(Datum X)
Definition postgres.h:365
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:406
MemoryContext ecxt_per_tuple_memory
Definition execnodes.h:295
AttrNumber * ioss_NameCStringAttNums
Definition execnodes.h:1821
HeapTuple xs_hitup
Definition relscan.h:182
IndexTuple xs_itup
Definition relscan.h:180
struct TupleDescData * xs_hitupdesc
Definition relscan.h:183
struct TupleDescData * xs_itupdesc
Definition relscan.h:181
TupleDesc tts_tupleDescriptor
Definition tuptable.h:129
bool * tts_isnull
Definition tuptable.h:133
Datum * tts_values
Definition tuptable.h:131
Definition c.h:889
const char * name

References Assert, attnum, DatumGetCString(), ExprContext::ecxt_per_tuple_memory, elog, ERROR, ExecClearTuple(), ExecStoreVirtualTuple(), fb(), heap_deform_tuple(), idx(), index_deform_tuple(), IndexOnlyScanState::ioss_NameCStringAttNums, IndexOnlyScanState::ioss_NameCStringCount, MemoryContextAlloc(), name, NAMEDATALEN, NameGetDatum(), namestrcpy(), TupleDescData::natts, ScanState::ps, PlanState::ps_ExprContext, IndexOnlyScanState::ss, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, unlikely, IndexScanDescData::xs_hitup, IndexScanDescData::xs_hitupdesc, IndexScanDescData::xs_itup, and IndexScanDescData::xs_itupdesc.

Referenced by IndexOnlyNext().