PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nodeIndexonlyscan.c File Reference
#include "postgres.h"
#include "access/genam.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/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, IndexTuple itup, TupleDesc itupdesc)
 
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)
 

Function Documentation

◆ ExecEndIndexOnlyScan()

void ExecEndIndexOnlyScan ( IndexOnlyScanState node)

Definition at line 398 of file nodeIndexonlyscan.c.

399{
400 Relation indexRelationDesc;
401 IndexScanDesc indexScanDesc;
402
403 /*
404 * extract information from the node
405 */
406 indexRelationDesc = node->ioss_RelationDesc;
407 indexScanDesc = node->ioss_ScanDesc;
408
409 /* Release VM buffer pin, if any. */
410 if (node->ioss_VMBuffer != InvalidBuffer)
411 {
414 }
415
416 /*
417 * close the index relation (no-op if we didn't open it)
418 */
419 if (indexScanDesc)
420 index_endscan(indexScanDesc);
421 if (indexRelationDesc)
422 index_close(indexRelationDesc, NoLock);
423}
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4924
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:378
#define NoLock
Definition: lockdefs.h:34
struct IndexScanDescData * ioss_ScanDesc
Definition: execnodes.h:1719
Relation ioss_RelationDesc
Definition: execnodes.h:1718

References index_close(), index_endscan(), InvalidBuffer, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_VMBuffer, NoLock, and ReleaseBuffer().

Referenced by ExecEndNode().

◆ ExecIndexOnlyMarkPos()

void ExecIndexOnlyMarkPos ( IndexOnlyScanState node)

Definition at line 433 of file nodeIndexonlyscan.c.

434{
435 EState *estate = node->ss.ps.state;
436 EPQState *epqstate = estate->es_epq_active;
437
438 if (epqstate != NULL)
439 {
440 /*
441 * We are inside an EvalPlanQual recheck. If a test tuple exists for
442 * this relation, then we shouldn't access the index at all. We would
443 * instead need to save, and later restore, the state of the
444 * relsubs_done flag, so that re-fetching the test tuple is possible.
445 * However, given the assumption that no caller sets a mark at the
446 * start of the scan, we can only get here with relsubs_done[i]
447 * already set, and so no state need be saved.
448 */
449 Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
450
451 Assert(scanrelid > 0);
452 if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
453 epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
454 {
455 /* Verify the claim above */
456 if (!epqstate->relsubs_done[scanrelid - 1])
457 elog(ERROR, "unexpected ExecIndexOnlyMarkPos call in EPQ recheck");
458 return;
459 }
460 }
461
463}
#define Assert(condition)
Definition: c.h:812
unsigned int Index
Definition: c.h:568
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
void index_markpos(IndexScanDesc scan)
Definition: indexam.c:408
ExecAuxRowMark ** relsubs_rowmark
Definition: execnodes.h:1301
TupleTableSlot ** relsubs_slot
Definition: execnodes.h:1273
bool * relsubs_done
Definition: execnodes.h:1308
struct EPQState * es_epq_active
Definition: execnodes.h:707
Plan * plan
Definition: execnodes.h:1126
EState * state
Definition: execnodes.h:1128
PlanState ps
Definition: execnodes.h:1573

References Assert, elog, ERROR, EState::es_epq_active, 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 470 of file nodeIndexonlyscan.c.

471{
472 EState *estate = node->ss.ps.state;
473 EPQState *epqstate = estate->es_epq_active;
474
475 if (estate->es_epq_active != NULL)
476 {
477 /* See comments in ExecIndexMarkPos */
478 Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
479
480 Assert(scanrelid > 0);
481 if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
482 epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
483 {
484 /* Verify the claim above */
485 if (!epqstate->relsubs_done[scanrelid - 1])
486 elog(ERROR, "unexpected ExecIndexOnlyRestrPos call in EPQ recheck");
487 return;
488 }
489 }
490
492}
void index_restrpos(IndexScanDesc scan)
Definition: indexam.c:432

References Assert, elog, ERROR, EState::es_epq_active, 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 336 of file nodeIndexonlyscan.c.

337{
339
340 /*
341 * If we have runtime keys and they've not already been set up, do it now.
342 */
343 if (node->ioss_NumRuntimeKeys != 0 && !node->ioss_RuntimeKeysReady)
344 ExecReScan((PlanState *) node);
345
346 return ExecScan(&node->ss,
349}
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:156
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:487
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:486
static TupleTableSlot * IndexOnlyNext(IndexOnlyScanState *node)
static bool IndexOnlyRecheck(IndexOnlyScanState *node, TupleTableSlot *slot)
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1716

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 706 of file nodeIndexonlyscan.c.

708{
709 EState *estate = node->ss.ps.state;
710
712 node->ioss_NumScanKeys,
714 estate->es_snapshot);
717}
Size index_parallelscan_estimate(Relation indexRelation, int nkeys, int norderbys, Snapshot snapshot)
Definition: indexam.c:453
#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:632
shm_toc_estimator estimator
Definition: parallel.h:41

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 726 of file nodeIndexonlyscan.c.

728{
729 EState *estate = node->ss.ps.state;
731
732 piscan = shm_toc_allocate(pcxt->toc, node->ioss_PscanLen);
734 node->ioss_RelationDesc,
735 estate->es_snapshot,
736 piscan);
737 shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
738 node->ioss_ScanDesc =
740 node->ioss_RelationDesc,
741 node->ioss_NumScanKeys,
743 piscan);
744 node->ioss_ScanDesc->xs_want_itup = true;
746
747 /*
748 * If no run-time keys to calculate or they are ready, go ahead and pass
749 * the scankeys to the index AM.
750 */
751 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
753 node->ioss_ScanKeys, node->ioss_NumScanKeys,
755}
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:541
void index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, ParallelIndexScanDesc target)
Definition: indexam.c:490
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:352
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
struct ScanKeyData * ioss_ScanKeys
Definition: execnodes.h:1710
struct ScanKeyData * ioss_OrderByKeys
Definition: execnodes.h:1712
shm_toc * toc
Definition: parallel.h:44
int plan_node_id
Definition: plannodes.h:152
Relation ss_currentRelation
Definition: execnodes.h:1574

References EState::es_snapshot, index_beginscan_parallel(), index_parallelscan_initialize(), index_rescan(), InvalidBuffer, 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, shm_toc_allocate(), shm_toc_insert(), 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 777 of file nodeIndexonlyscan.c.

779{
781
782 piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
783 node->ioss_ScanDesc =
785 node->ioss_RelationDesc,
786 node->ioss_NumScanKeys,
788 piscan);
789 node->ioss_ScanDesc->xs_want_itup = true;
790
791 /*
792 * If no run-time keys to calculate or they are ready, go ahead and pass
793 * the scankeys to the index AM.
794 */
795 if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
797 node->ioss_ScanKeys, node->ioss_NumScanKeys,
799}
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

References index_beginscan_parallel(), index_rescan(), 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, shm_toc_lookup(), IndexOnlyScanState::ss, ScanState::ss_currentRelation, ParallelWorkerContext::toc, and IndexScanDescData::xs_want_itup.

Referenced by ExecParallelInitializeWorker().

◆ ExecIndexOnlyScanReInitializeDSM()

void ExecIndexOnlyScanReInitializeDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 764 of file nodeIndexonlyscan.c.

766{
768}
void index_parallelrescan(IndexScanDesc scan)
Definition: indexam.c:523

References index_parallelrescan(), and IndexOnlyScanState::ioss_ScanDesc.

Referenced by ExecParallelReInitializeDSM().

◆ ExecInitIndexOnlyScan()

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

Definition at line 506 of file nodeIndexonlyscan.c.

507{
508 IndexOnlyScanState *indexstate;
509 Relation currentRelation;
510 Relation indexRelation;
511 LOCKMODE lockmode;
512 TupleDesc tupDesc;
513 int indnkeyatts;
514 int namecount;
515
516 /*
517 * create state structure
518 */
519 indexstate = makeNode(IndexOnlyScanState);
520 indexstate->ss.ps.plan = (Plan *) node;
521 indexstate->ss.ps.state = estate;
522 indexstate->ss.ps.ExecProcNode = ExecIndexOnlyScan;
523
524 /*
525 * Miscellaneous initialization
526 *
527 * create expression context for node
528 */
529 ExecAssignExprContext(estate, &indexstate->ss.ps);
530
531 /*
532 * open the scan relation
533 */
534 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
535
536 indexstate->ss.ss_currentRelation = currentRelation;
537 indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
538
539 /*
540 * Build the scan tuple type using the indextlist generated by the
541 * planner. We use this, rather than the index's physical tuple
542 * descriptor, because the latter contains storage column types not the
543 * types of the original datums. (It's the AM's responsibility to return
544 * suitable data anyway.)
545 */
546 tupDesc = ExecTypeFromTL(node->indextlist);
547 ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc,
549
550 /*
551 * We need another slot, in a format that's suitable for the table AM, for
552 * when we need to fetch a tuple from the table for rechecking visibility.
553 */
554 indexstate->ioss_TableSlot =
556 RelationGetDescr(currentRelation),
557 table_slot_callbacks(currentRelation));
558
559 /*
560 * Initialize result type and projection info. The node's targetlist will
561 * contain Vars with varno = INDEX_VAR, referencing the scan tuple.
562 */
563 ExecInitResultTypeTL(&indexstate->ss.ps);
565
566 /*
567 * initialize child expressions
568 *
569 * Note: we don't initialize all of the indexorderby expression, only the
570 * sub-parts corresponding to runtime keys (see below).
571 */
572 indexstate->ss.ps.qual =
573 ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
574 indexstate->recheckqual =
575 ExecInitQual(node->recheckqual, (PlanState *) indexstate);
576
577 /*
578 * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
579 * here. This allows an index-advisor plugin to EXPLAIN a plan containing
580 * references to nonexistent indexes.
581 */
582 if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
583 return indexstate;
584
585 /* Open the index relation. */
586 lockmode = exec_rt_fetch(node->scan.scanrelid, estate)->rellockmode;
587 indexRelation = index_open(node->indexid, lockmode);
588 indexstate->ioss_RelationDesc = indexRelation;
589
590 /*
591 * Initialize index-specific scan state
592 */
593 indexstate->ioss_RuntimeKeysReady = false;
594 indexstate->ioss_RuntimeKeys = NULL;
595 indexstate->ioss_NumRuntimeKeys = 0;
596
597 /*
598 * build the index scan keys from the index qualification
599 */
600 ExecIndexBuildScanKeys((PlanState *) indexstate,
601 indexRelation,
602 node->indexqual,
603 false,
604 &indexstate->ioss_ScanKeys,
605 &indexstate->ioss_NumScanKeys,
606 &indexstate->ioss_RuntimeKeys,
607 &indexstate->ioss_NumRuntimeKeys,
608 NULL, /* no ArrayKeys */
609 NULL);
610
611 /*
612 * any ORDER BY exprs have to be turned into scankeys in the same way
613 */
614 ExecIndexBuildScanKeys((PlanState *) indexstate,
615 indexRelation,
616 node->indexorderby,
617 true,
618 &indexstate->ioss_OrderByKeys,
619 &indexstate->ioss_NumOrderByKeys,
620 &indexstate->ioss_RuntimeKeys,
621 &indexstate->ioss_NumRuntimeKeys,
622 NULL, /* no ArrayKeys */
623 NULL);
624
625 /*
626 * If we have runtime keys, we need an ExprContext to evaluate them. The
627 * node's standard context won't do because we want to reset that context
628 * for every tuple. So, build another context just like the other one...
629 * -tgl 7/11/00
630 */
631 if (indexstate->ioss_NumRuntimeKeys != 0)
632 {
633 ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
634
635 ExecAssignExprContext(estate, &indexstate->ss.ps);
636 indexstate->ioss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
637 indexstate->ss.ps.ps_ExprContext = stdecontext;
638 }
639 else
640 {
641 indexstate->ioss_RuntimeContext = NULL;
642 }
643
644 indexstate->ioss_NameCStringAttNums = NULL;
645 indnkeyatts = indexRelation->rd_index->indnkeyatts;
646 namecount = 0;
647
648 /*
649 * The "name" type for btree uses text_ops which results in storing
650 * cstrings in the indexed keys rather than names. Here we detect that in
651 * a generic way in case other index AMs want to do the same optimization.
652 * Check for opclasses with an opcintype of NAMEOID and an index tuple
653 * descriptor with CSTRINGOID. If any of these are found, create an array
654 * marking the index attribute number of each of them. StoreIndexTuple()
655 * handles copying the name Datums into a NAMEDATALEN-byte allocation.
656 */
657
658 /* First, count the number of such index keys */
659 for (int attnum = 0; attnum < indnkeyatts; attnum++)
660 {
661 if (TupleDescAttr(indexRelation->rd_att, attnum)->atttypid == CSTRINGOID &&
662 indexRelation->rd_opcintype[attnum] == NAMEOID)
663 namecount++;
664 }
665
666 if (namecount > 0)
667 {
668 int idx = 0;
669
670 /*
671 * Now create an array to mark the attribute numbers of the keys that
672 * need to be converted from cstring to name.
673 */
674 indexstate->ioss_NameCStringAttNums = (AttrNumber *)
675 palloc(sizeof(AttrNumber) * namecount);
676
677 for (int attnum = 0; attnum < indnkeyatts; attnum++)
678 {
679 if (TupleDescAttr(indexRelation->rd_att, attnum)->atttypid == CSTRINGOID &&
680 indexRelation->rd_opcintype[attnum] == NAMEOID)
681 indexstate->ioss_NameCStringAttNums[idx++] = (AttrNumber) attnum;
682 }
683 }
684
685 indexstate->ioss_NameCStringCount = namecount;
686
687 /*
688 * all done.
689 */
690 return indexstate;
691}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
int16 AttrNumber
Definition: attnum.h:21
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:224
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, int varno)
Definition: execScan.c:283
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleTableSlot * ExecAllocTableSlot(List **tupleTable, TupleDesc desc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1358
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1998
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1942
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:2125
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:485
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:742
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition: executor.h:603
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:65
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
int LOCKMODE
Definition: lockdefs.h:26
void * palloc(Size size)
Definition: mcxt.c:1317
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:155
int16 attnum
Definition: pg_attribute.h:74
#define INDEX_VAR
Definition: primnodes.h:238
#define RelationGetDescr(relation)
Definition: rel.h:531
List * es_tupleTable
Definition: execnodes.h:677
TupleTableSlot * ioss_TableSlot
Definition: execnodes.h:1720
ExprState * recheckqual
Definition: execnodes.h:1709
ExprContext * ioss_RuntimeContext
Definition: execnodes.h:1717
AttrNumber * ioss_NameCStringAttNums
Definition: execnodes.h:1723
IndexRuntimeKeyInfo * ioss_RuntimeKeys
Definition: execnodes.h:1714
List * indexqual
Definition: plannodes.h:497
List * recheckqual
Definition: plannodes.h:498
List * indextlist
Definition: plannodes.h:500
List * indexorderby
Definition: plannodes.h:499
ExprState * qual
Definition: execnodes.h:1147
ExprContext * ps_ExprContext
Definition: execnodes.h:1165
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1132
Oid * rd_opcintype
Definition: rel.h:208
TupleDesc rd_att
Definition: rel.h:112
Form_pg_index rd_index
Definition: rel.h:192
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1575
Index scanrelid
Definition: plannodes.h:390
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:58
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:152

References attnum, EState::es_tupleTable, EXEC_FLAG_EXPLAIN_ONLY, exec_rt_fetch(), ExecAllocTableSlot(), ExecAssignExprContext(), ExecAssignScanProjectionInfoWithVarno(), ExecIndexBuildScanKeys(), ExecIndexOnlyScan(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecTypeFromTL(), idx(), index_open(), INDEX_VAR, IndexOnlyScan::indexid, IndexOnlyScan::indexorderby, IndexOnlyScan::indexqual, IndexOnlyScan::indextlist, IndexOnlyScanState::ioss_NameCStringAttNums, IndexOnlyScanState::ioss_NameCStringCount, IndexOnlyScanState::ioss_NumOrderByKeys, IndexOnlyScanState::ioss_NumRuntimeKeys, IndexOnlyScanState::ioss_NumScanKeys, IndexOnlyScanState::ioss_OrderByKeys, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_RuntimeContext, IndexOnlyScanState::ioss_RuntimeKeys, IndexOnlyScanState::ioss_RuntimeKeysReady, IndexOnlyScanState::ioss_ScanKeys, IndexOnlyScanState::ioss_TableSlot, makeNode, palloc(), PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, PlanState::qual, RelationData::rd_att, RelationData::rd_index, RelationData::rd_opcintype, IndexOnlyScanState::recheckqual, IndexOnlyScan::recheckqual, RelationGetDescr, IndexOnlyScan::scan, Scan::scanrelid, IndexOnlyScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, PlanState::state, table_slot_callbacks(), TTSOpsVirtual, and TupleDescAttr().

Referenced by ExecInitNode().

◆ ExecReScanIndexOnlyScan()

void ExecReScanIndexOnlyScan ( IndexOnlyScanState node)

Definition at line 363 of file nodeIndexonlyscan.c.

364{
365 /*
366 * If we are doing runtime key calculations (ie, any of the index key
367 * values weren't simple Consts), compute the new key values. But first,
368 * reset the context so we don't leak memory as each outer tuple is
369 * scanned. Note this assumes that we will recalculate *all* runtime keys
370 * on each call.
371 */
372 if (node->ioss_NumRuntimeKeys != 0)
373 {
374 ExprContext *econtext = node->ioss_RuntimeContext;
375
376 ResetExprContext(econtext);
378 node->ioss_RuntimeKeys,
379 node->ioss_NumRuntimeKeys);
380 }
381 node->ioss_RuntimeKeysReady = true;
382
383 /* reset index scan */
384 if (node->ioss_ScanDesc)
386 node->ioss_ScanKeys, node->ioss_NumScanKeys,
388
389 ExecScanReScan(&node->ss);
390}
void ExecScanReScan(ScanState *node)
Definition: execScan.c:297
#define ResetExprContext(econtext)
Definition: executor.h:557
void ExecIndexEvalRuntimeKeys(ExprContext *econtext, IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)

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 61 of file nodeIndexonlyscan.c.

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

References Assert, CHECK_FOR_INTERRUPTS, ExprContext::ecxt_scantuple, elog, ereport, errcode(), errmsg(), ERROR, EState::es_direction, EState::es_snapshot, ExecClearTuple(), ExecForceStoreHeapTuple(), ExecQualAndReset(), IndexScanDescData::heapRelation, index_beginscan(), index_fetch_heap(), index_getnext_tid(), index_rescan(), InstrCountFiltered2, InstrCountTuples2, InvalidBuffer, 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(), TupleDescData::natts, IndexScanDescData::numberOfOrderBys, PlanState::plan, PredicateLockPage(), ScanState::ps, PlanState::ps_ExprContext, IndexOnlyScanState::recheckqual, ScanDirectionCombine, IndexOnlyScanState::ss, ScanState::ss_currentRelation, ScanState::ss_ScanTupleSlot, PlanState::state, StoreIndexTuple(), TupleTableSlot::tts_tupleDescriptor, VM_ALL_VISIBLE, IndexScanDescData::xs_heap_continue, IndexScanDescData::xs_hitup, IndexScanDescData::xs_hitupdesc, IndexScanDescData::xs_itup, IndexScanDescData::xs_itupdesc, 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 325 of file nodeIndexonlyscan.c.

326{
327 elog(ERROR, "EvalPlanQual recheck is not supported in index-only scans");
328 return false; /* keep compiler quiet */
329}

References elog, and ERROR.

Referenced by ExecIndexOnlyScan().

◆ StoreIndexTuple()

static void StoreIndexTuple ( IndexOnlyScanState node,
TupleTableSlot slot,
IndexTuple  itup,
TupleDesc  itupdesc 
)
static

Definition at line 268 of file nodeIndexonlyscan.c.

270{
271 /*
272 * Note: we must use the tupdesc supplied by the AM in index_deform_tuple,
273 * not the slot's tupdesc, in case the latter has different datatypes
274 * (this happens for btree name_ops in particular). They'd better have
275 * the same number of columns though, as well as being datatype-compatible
276 * which is something we can't so easily check.
277 */
278 Assert(slot->tts_tupleDescriptor->natts == itupdesc->natts);
279
280 ExecClearTuple(slot);
281 index_deform_tuple(itup, itupdesc, slot->tts_values, slot->tts_isnull);
282
283 /*
284 * Copy all name columns stored as cstrings back into a NAMEDATALEN byte
285 * sized allocation. We mark this branch as unlikely as generally "name"
286 * is used only for the system catalogs and this would have to be a user
287 * query running on those or some other user table with an index on a name
288 * column.
289 */
290 if (unlikely(node->ioss_NameCStringAttNums != NULL))
291 {
292 int attcount = node->ioss_NameCStringCount;
293
294 for (int idx = 0; idx < attcount; idx++)
295 {
297 Name name;
298
299 /* skip null Datums */
300 if (slot->tts_isnull[attnum])
301 continue;
302
303 /* allocate the NAMEDATALEN and copy the datum into that memory */
306
307 /* use namestrcpy to zero-pad all trailing bytes */
310 }
311 }
312
314}
NameData * Name
Definition: c.h:698
#define unlikely(x)
Definition: c.h:330
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1739
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:456
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define NAMEDATALEN
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:266
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
Definition: c.h:695
const char * name

References Assert, attnum, DatumGetCString(), ExprContext::ecxt_per_tuple_memory, ExecClearTuple(), ExecStoreVirtualTuple(), 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, and unlikely.

Referenced by IndexOnlyNext().