PostgreSQL Source Code  git master
nodeIndexonlyscan.c File Reference
#include "postgres.h"
#include "access/relscan.h"
#include "access/visibilitymap.h"
#include "executor/execdebug.h"
#include "executor/nodeIndexonlyscan.h"
#include "executor/nodeIndexscan.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/predicate.h"
#include "utils/memutils.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 (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 372 of file nodeIndexonlyscan.c.

References ExecClearTuple(), ExecCloseScanRelation(), ExecFreeExprContext(), FreeExprContext(), index_close(), index_endscan(), InvalidBuffer, IndexOnlyScanState::ioss_RelationDesc, IndexOnlyScanState::ioss_RuntimeContext, IndexOnlyScanState::ioss_ScanDesc, IndexOnlyScanState::ioss_VMBuffer, NoLock, ScanState::ps, PlanState::ps_ResultTupleSlot, ReleaseBuffer(), IndexOnlyScanState::ss, ScanState::ss_currentRelation, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

373 {
374  Relation indexRelationDesc;
375  IndexScanDesc indexScanDesc;
376  Relation relation;
377 
378  /*
379  * extract information from the node
380  */
381  indexRelationDesc = node->ioss_RelationDesc;
382  indexScanDesc = node->ioss_ScanDesc;
383  relation = node->ss.ss_currentRelation;
384 
385  /* Release VM buffer pin, if any. */
386  if (node->ioss_VMBuffer != InvalidBuffer)
387  {
390  }
391 
392  /*
393  * Free the exprcontext(s) ... now dead code, see ExecFreeExprContext
394  */
395 #ifdef NOT_USED
396  ExecFreeExprContext(&node->ss.ps);
397  if (node->ioss_RuntimeContext)
399 #endif
400 
401  /*
402  * clear out tuple table slots
403  */
406 
407  /*
408  * close the index relation (no-op if we didn't open it)
409  */
410  if (indexScanDesc)
411  index_endscan(indexScanDesc);
412  if (indexRelationDesc)
413  index_close(indexRelationDesc, NoLock);
414 
415  /*
416  * close the heap relation.
417  */
418  ExecCloseScanRelation(relation);
419 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
#define InvalidBuffer
Definition: buf.h:25
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1127
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
Relation ss_currentRelation
Definition: execnodes.h:1125
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:561
PlanState ps
Definition: execnodes.h:1124
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:901
ExprContext * ioss_RuntimeContext
Definition: execnodes.h:1263
#define NoLock
Definition: lockdefs.h:34
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:341
void FreeExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:356
Relation ioss_RelationDesc
Definition: execnodes.h:1264
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:696
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177

◆ ExecIndexOnlyMarkPos()

void ExecIndexOnlyMarkPos ( IndexOnlyScanState node)

Definition at line 429 of file nodeIndexonlyscan.c.

References Assert, elog, ERROR, EState::es_epqScanDone, EState::es_epqTuple, EState::es_epqTupleSet, index_markpos(), IndexOnlyScanState::ioss_ScanDesc, PlanState::plan, ScanState::ps, IndexOnlyScanState::ss, and PlanState::state.

Referenced by ExecMarkPos().

430 {
431  EState *estate = node->ss.ps.state;
432 
433  if (estate->es_epqTuple != NULL)
434  {
435  /*
436  * We are inside an EvalPlanQual recheck. If a test tuple exists for
437  * this relation, then we shouldn't access the index at all. We would
438  * instead need to save, and later restore, the state of the
439  * es_epqScanDone flag, so that re-fetching the test tuple is
440  * possible. However, given the assumption that no caller sets a mark
441  * at the start of the scan, we can only get here with es_epqScanDone
442  * already set, and so no state need be saved.
443  */
444  Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
445 
446  Assert(scanrelid > 0);
447  if (estate->es_epqTupleSet[scanrelid - 1])
448  {
449  /* Verify the claim above */
450  if (!estate->es_epqScanDone[scanrelid - 1])
451  elog(ERROR, "unexpected ExecIndexOnlyMarkPos call in EPQ recheck");
452  return;
453  }
454  }
455 
457 }
HeapTuple * es_epqTuple
Definition: execnodes.h:523
void index_markpos(IndexScanDesc scan)
Definition: indexam.c:371
EState * state
Definition: execnodes.h:870
PlanState ps
Definition: execnodes.h:1124
#define ERROR
Definition: elog.h:43
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265
unsigned int Index
Definition: c.h:431
Plan * plan
Definition: execnodes.h:868
#define Assert(condition)
Definition: c.h:688
bool * es_epqTupleSet
Definition: execnodes.h:524
bool * es_epqScanDone
Definition: execnodes.h:525
#define elog
Definition: elog.h:219

◆ ExecIndexOnlyRestrPos()

void ExecIndexOnlyRestrPos ( IndexOnlyScanState node)

Definition at line 464 of file nodeIndexonlyscan.c.

References Assert, elog, ERROR, EState::es_epqScanDone, EState::es_epqTuple, EState::es_epqTupleSet, index_restrpos(), IndexOnlyScanState::ioss_ScanDesc, PlanState::plan, ScanState::ps, IndexOnlyScanState::ss, and PlanState::state.

Referenced by ExecRestrPos().

465 {
466  EState *estate = node->ss.ps.state;
467 
468  if (estate->es_epqTuple != NULL)
469  {
470  /* See comments in ExecIndexOnlyMarkPos */
471  Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
472 
473  Assert(scanrelid > 0);
474  if (estate->es_epqTupleSet[scanrelid - 1])
475  {
476  /* Verify the claim above */
477  if (!estate->es_epqScanDone[scanrelid - 1])
478  elog(ERROR, "unexpected ExecIndexOnlyRestrPos call in EPQ recheck");
479  return;
480  }
481  }
482 
484 }
HeapTuple * es_epqTuple
Definition: execnodes.h:523
EState * state
Definition: execnodes.h:870
void index_restrpos(IndexScanDesc scan)
Definition: indexam.c:396
PlanState ps
Definition: execnodes.h:1124
#define ERROR
Definition: elog.h:43
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265
unsigned int Index
Definition: c.h:431
Plan * plan
Definition: execnodes.h:868
#define Assert(condition)
Definition: c.h:688
bool * es_epqTupleSet
Definition: execnodes.h:524
bool * es_epqScanDone
Definition: execnodes.h:525
#define elog
Definition: elog.h:219

◆ ExecIndexOnlyScan()

static TupleTableSlot* ExecIndexOnlyScan ( PlanState pstate)
static

Definition at line 310 of file nodeIndexonlyscan.c.

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

Referenced by ExecInitIndexOnlyScan().

311 {
313 
314  /*
315  * If we have runtime keys and they've not already been set up, do it now.
316  */
317  if (node->ioss_NumRuntimeKeys != 0 && !node->ioss_RuntimeKeysReady)
318  ExecReScan((PlanState *) node);
319 
320  return ExecScan(&node->ss,
323 }
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1262
#define castNode(_type_, nodeptr)
Definition: nodes.h:582
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:121
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
static bool IndexOnlyRecheck(IndexOnlyScanState *node, TupleTableSlot *slot)
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:423
static TupleTableSlot * IndexOnlyNext(IndexOnlyScanState *node)
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:422

◆ ExecIndexOnlyScanEstimate()

void ExecIndexOnlyScanEstimate ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 650 of file nodeIndexonlyscan.c.

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

Referenced by ExecParallelEstimate().

652 {
653  EState *estate = node->ss.ps.state;
654 
656  estate->es_snapshot);
658  shm_toc_estimate_keys(&pcxt->estimator, 1);
659 }
shm_toc_estimator estimator
Definition: parallel.h:41
Snapshot es_snapshot
Definition: execnodes.h:443
EState * state
Definition: execnodes.h:870
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size index_parallelscan_estimate(Relation indexRelation, Snapshot snapshot)
Definition: indexam.c:418
PlanState ps
Definition: execnodes.h:1124
Relation ioss_RelationDesc
Definition: execnodes.h:1264
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53

◆ ExecIndexOnlyScanInitializeDSM()

void ExecIndexOnlyScanInitializeDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 668 of file nodeIndexonlyscan.c.

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().

670 {
671  EState *estate = node->ss.ps.state;
672  ParallelIndexScanDesc piscan;
673 
674  piscan = shm_toc_allocate(pcxt->toc, node->ioss_PscanLen);
676  node->ioss_RelationDesc,
677  estate->es_snapshot,
678  piscan);
679  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
680  node->ioss_ScanDesc =
682  node->ioss_RelationDesc,
683  node->ioss_NumScanKeys,
684  node->ioss_NumOrderByKeys,
685  piscan);
686  node->ioss_ScanDesc->xs_want_itup = true;
688 
689  /*
690  * If no run-time keys to calculate or they are ready, go ahead and pass
691  * the scankeys to the index AM.
692  */
693  if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
695  node->ioss_ScanKeys, node->ioss_NumScanKeys,
697 }
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1262
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:497
int plan_node_id
Definition: plannodes.h:143
#define InvalidBuffer
Definition: buf.h:25
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:311
Snapshot es_snapshot
Definition: execnodes.h:443
Relation ss_currentRelation
Definition: execnodes.h:1125
EState * state
Definition: execnodes.h:870
PlanState ps
Definition: execnodes.h:1124
ScanKey ioss_ScanKeys
Definition: execnodes.h:1256
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265
ScanKey ioss_OrderByKeys
Definition: execnodes.h:1258
Plan * plan
Definition: execnodes.h:868
Relation ioss_RelationDesc
Definition: execnodes.h:1264
bool xs_want_itup
Definition: relscan.h:97
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 index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, ParallelIndexScanDesc target)
Definition: indexam.c:451
shm_toc * toc
Definition: parallel.h:44

◆ ExecIndexOnlyScanInitializeWorker()

void ExecIndexOnlyScanInitializeWorker ( IndexOnlyScanState node,
ParallelWorkerContext pwcxt 
)

Definition at line 719 of file nodeIndexonlyscan.c.

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().

721 {
722  ParallelIndexScanDesc piscan;
723 
724  piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
725  node->ioss_ScanDesc =
727  node->ioss_RelationDesc,
728  node->ioss_NumScanKeys,
729  node->ioss_NumOrderByKeys,
730  piscan);
731  node->ioss_ScanDesc->xs_want_itup = true;
732 
733  /*
734  * If no run-time keys to calculate or they are ready, go ahead and pass
735  * the scankeys to the index AM.
736  */
737  if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
739  node->ioss_ScanKeys, node->ioss_NumScanKeys,
741 }
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1262
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:497
int plan_node_id
Definition: plannodes.h:143
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:311
Relation ss_currentRelation
Definition: execnodes.h:1125
PlanState ps
Definition: execnodes.h:1124
ScanKey ioss_ScanKeys
Definition: execnodes.h:1256
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265
ScanKey ioss_OrderByKeys
Definition: execnodes.h:1258
Plan * plan
Definition: execnodes.h:868
Relation ioss_RelationDesc
Definition: execnodes.h:1264
bool xs_want_itup
Definition: relscan.h:97
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecIndexOnlyScanReInitializeDSM()

void ExecIndexOnlyScanReInitializeDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 706 of file nodeIndexonlyscan.c.

References index_parallelrescan(), and IndexOnlyScanState::ioss_ScanDesc.

Referenced by ExecParallelReInitializeDSM().

708 {
710 }
void index_parallelrescan(IndexScanDesc scan)
Definition: indexam.c:482
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265

◆ ExecInitIndexOnlyScan()

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

Definition at line 498 of file nodeIndexonlyscan.c.

References AccessShareLock, EXEC_FLAG_EXPLAIN_ONLY, ExecAssignExprContext(), ExecAssignScanProjectionInfoWithVarno(), ExecIndexBuildScanKeys(), ExecIndexOnlyScan(), ExecInitQual(), ExecInitResultTupleSlotTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecRelationIsTargetRelation(), ExecTypeFromTL(), index_open(), INDEX_VAR, IndexOnlyScan::indexid, IndexOnlyScan::indexorderby, IndexOnlyScan::indexqual, IndexOnlyScan::indextlist, IndexOnlyScanState::ioss_HeapFetches, makeNode, NoLock, Scan::plan, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, Plan::qual, PlanState::qual, IndexOnlyScan::scan, Scan::scanrelid, IndexOnlyScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, and PlanState::state.

Referenced by ExecInitNode().

499 {
500  IndexOnlyScanState *indexstate;
501  Relation currentRelation;
502  bool relistarget;
503  TupleDesc tupDesc;
504 
505  /*
506  * create state structure
507  */
508  indexstate = makeNode(IndexOnlyScanState);
509  indexstate->ss.ps.plan = (Plan *) node;
510  indexstate->ss.ps.state = estate;
511  indexstate->ss.ps.ExecProcNode = ExecIndexOnlyScan;
512  indexstate->ioss_HeapFetches = 0;
513 
514  /*
515  * Miscellaneous initialization
516  *
517  * create expression context for node
518  */
519  ExecAssignExprContext(estate, &indexstate->ss.ps);
520 
521  /*
522  * open the base relation and acquire appropriate lock on it.
523  */
524  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
525 
526  indexstate->ss.ss_currentRelation = currentRelation;
527  indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
528 
529  /*
530  * Build the scan tuple type using the indextlist generated by the
531  * planner. We use this, rather than the index's physical tuple
532  * descriptor, because the latter contains storage column types not the
533  * types of the original datums. (It's the AM's responsibility to return
534  * suitable data anyway.)
535  */
536  tupDesc = ExecTypeFromTL(node->indextlist, false);
537  ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc);
538 
539  /*
540  * Initialize result slot, type and projection info. The node's
541  * targetlist will contain Vars with varno = INDEX_VAR, referencing the
542  * scan tuple.
543  */
544  ExecInitResultTupleSlotTL(estate, &indexstate->ss.ps);
546 
547  /*
548  * initialize child expressions
549  *
550  * Note: we don't initialize all of the indexorderby expression, only the
551  * sub-parts corresponding to runtime keys (see below).
552  */
553  indexstate->ss.ps.qual =
554  ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
555  indexstate->indexqual =
556  ExecInitQual(node->indexqual, (PlanState *) indexstate);
557 
558  /*
559  * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
560  * here. This allows an index-advisor plugin to EXPLAIN a plan containing
561  * references to nonexistent indexes.
562  */
563  if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
564  return indexstate;
565 
566  /*
567  * Open the index relation.
568  *
569  * If the parent table is one of the target relations of the query, then
570  * InitPlan already opened and write-locked the index, so we can avoid
571  * taking another lock here. Otherwise we need a normal reader's lock.
572  */
573  relistarget = ExecRelationIsTargetRelation(estate, node->scan.scanrelid);
574  indexstate->ioss_RelationDesc = index_open(node->indexid,
575  relistarget ? NoLock : AccessShareLock);
576 
577  /*
578  * Initialize index-specific scan state
579  */
580  indexstate->ioss_RuntimeKeysReady = false;
581  indexstate->ioss_RuntimeKeys = NULL;
582  indexstate->ioss_NumRuntimeKeys = 0;
583 
584  /*
585  * build the index scan keys from the index qualification
586  */
587  ExecIndexBuildScanKeys((PlanState *) indexstate,
588  indexstate->ioss_RelationDesc,
589  node->indexqual,
590  false,
591  &indexstate->ioss_ScanKeys,
592  &indexstate->ioss_NumScanKeys,
593  &indexstate->ioss_RuntimeKeys,
594  &indexstate->ioss_NumRuntimeKeys,
595  NULL, /* no ArrayKeys */
596  NULL);
597 
598  /*
599  * any ORDER BY exprs have to be turned into scankeys in the same way
600  */
601  ExecIndexBuildScanKeys((PlanState *) indexstate,
602  indexstate->ioss_RelationDesc,
603  node->indexorderby,
604  true,
605  &indexstate->ioss_OrderByKeys,
606  &indexstate->ioss_NumOrderByKeys,
607  &indexstate->ioss_RuntimeKeys,
608  &indexstate->ioss_NumRuntimeKeys,
609  NULL, /* no ArrayKeys */
610  NULL);
611 
612  /*
613  * If we have runtime keys, we need an ExprContext to evaluate them. The
614  * node's standard context won't do because we want to reset that context
615  * for every tuple. So, build another context just like the other one...
616  * -tgl 7/11/00
617  */
618  if (indexstate->ioss_NumRuntimeKeys != 0)
619  {
620  ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
621 
622  ExecAssignExprContext(estate, &indexstate->ss.ps);
623  indexstate->ioss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
624  indexstate->ss.ps.ps_ExprContext = stdecontext;
625  }
626  else
627  {
628  indexstate->ioss_RuntimeContext = NULL;
629  }
630 
631  /*
632  * all done.
633  */
634  return indexstate;
635 }
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1262
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:330
Index scanrelid
Definition: plannodes.h:331
ExprContext * ps_ExprContext
Definition: execnodes.h:902
#define AccessShareLock
Definition: lockdefs.h:36
IndexRuntimeKeyInfo * ioss_RuntimeKeys
Definition: execnodes.h:1260
Relation ss_currentRelation
Definition: execnodes.h:1125
EState * state
Definition: execnodes.h:870
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:204
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:248
PlanState ps
Definition: execnodes.h:1124
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:638
ExprContext * ioss_RuntimeContext
Definition: execnodes.h:1263
#define NoLock
Definition: lockdefs.h:34
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:944
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:874
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:870
Plan * plan
Definition: execnodes.h:868
Relation ioss_RelationDesc
Definition: execnodes.h:1264
List * indextlist
Definition: plannodes.h:423
List * indexorderby
Definition: plannodes.h:422
#define makeNode(_type_)
Definition: nodes.h:561
static TupleTableSlot * ExecIndexOnlyScan(PlanState *pstate)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:425
void ExecIndexBuildScanKeys(PlanState *planstate, Relation index, List *quals, bool isorderby, ScanKey *scanKeys, int *numScanKeys, IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys, IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
List * indexqual
Definition: plannodes.h:421
ExprState * indexqual
Definition: execnodes.h:1255
ExprState * qual
Definition: execnodes.h:886
HeapScanDesc ss_currentScanDesc
Definition: execnodes.h:1126
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc)
Definition: execTuples.c:895
#define INDEX_VAR
Definition: primnodes.h:155
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:59
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid)
Definition: execUtils.c:612

◆ ExecReScanIndexOnlyScan()

void ExecReScanIndexOnlyScan ( IndexOnlyScanState node)

Definition at line 337 of file nodeIndexonlyscan.c.

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().

338 {
339  /*
340  * If we are doing runtime key calculations (ie, any of the index key
341  * values weren't simple Consts), compute the new key values. But first,
342  * reset the context so we don't leak memory as each outer tuple is
343  * scanned. Note this assumes that we will recalculate *all* runtime keys
344  * on each call.
345  */
346  if (node->ioss_NumRuntimeKeys != 0)
347  {
348  ExprContext *econtext = node->ioss_RuntimeContext;
349 
350  ResetExprContext(econtext);
351  ExecIndexEvalRuntimeKeys(econtext,
352  node->ioss_RuntimeKeys,
353  node->ioss_NumRuntimeKeys);
354  }
355  node->ioss_RuntimeKeysReady = true;
356 
357  /* reset index scan */
358  if (node->ioss_ScanDesc)
360  node->ioss_ScanKeys, node->ioss_NumScanKeys,
362 
363  ExecScanReScan(&node->ss);
364 }
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1262
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:311
IndexRuntimeKeyInfo * ioss_RuntimeKeys
Definition: execnodes.h:1260
ScanKey ioss_ScanKeys
Definition: execnodes.h:1256
ExprContext * ioss_RuntimeContext
Definition: execnodes.h:1263
IndexScanDesc ioss_ScanDesc
Definition: execnodes.h:1265
ScanKey ioss_OrderByKeys
Definition: execnodes.h:1258
void ExecIndexEvalRuntimeKeys(ExprContext *econtext, IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
void ExecScanReScan(ScanState *node)
Definition: execScan.c:262
#define ResetExprContext(econtext)
Definition: executor.h:484

◆ IndexOnlyNext()

static TupleTableSlot * IndexOnlyNext ( IndexOnlyScanState node)
static

Definition at line 57 of file nodeIndexonlyscan.c.

References Assert, BackwardScanDirection, CHECK_FOR_INTERRUPTS, ExprContext::ecxt_scantuple, elog, ereport, errcode(), errmsg(), ERROR, EState::es_direction, EState::es_snapshot, ExecClearTuple(), ExecQualAndReset(), ExecStoreTuple(), ForwardScanDirection, IndexScanDescData::heapRelation, index_beginscan(), index_fetch_heap(), index_getnext_tid(), index_rescan(), IndexOnlyScanState::indexqual, InstrCountFiltered2, InvalidBuffer, IndexOnlyScanState::ioss_HeapFetches, 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_VMBuffer, ItemPointerGetBlockNumber, tupleDesc::natts, IndexScanDescData::numberOfOrderBys, PlanState::plan, PredicateLockPage(), ScanState::ps, PlanState::ps_ExprContext, ScanDirectionIsBackward, ScanDirectionIsForward, IndexOnlyScanState::ss, ScanState::ss_currentRelation, ScanState::ss_ScanTupleSlot, PlanState::state, StoreIndexTuple(), TupleTableSlot::tts_tupleDescriptor, VM_ALL_VISIBLE, IndexScanDescData::xs_continue_hot, 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().

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

◆ IndexOnlyRecheck()

static bool IndexOnlyRecheck ( IndexOnlyScanState node,
TupleTableSlot slot 
)
static

Definition at line 299 of file nodeIndexonlyscan.c.

References elog, and ERROR.

Referenced by ExecIndexOnlyScan().

300 {
301  elog(ERROR, "EvalPlanQual recheck is not supported in index-only scans");
302  return false; /* keep compiler quiet */
303 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219

◆ StoreIndexTuple()

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

Definition at line 268 of file nodeIndexonlyscan.c.

References Assert, ExecClearTuple(), ExecStoreVirtualTuple(), i, index_getattr, tupleDesc::natts, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, and values.

Referenced by IndexOnlyNext().

269 {
270  int nindexatts = itupdesc->natts;
271  Datum *values = slot->tts_values;
272  bool *isnull = slot->tts_isnull;
273  int i;
274 
275  /*
276  * Note: we must use the tupdesc supplied by the AM in index_getattr, not
277  * the slot's tupdesc, in case the latter has different datatypes (this
278  * happens for btree name_ops in particular). They'd better have the same
279  * number of columns though, as well as being datatype-compatible which is
280  * something we can't so easily check.
281  */
282  Assert(slot->tts_tupleDescriptor->natts == nindexatts);
283 
284  ExecClearTuple(slot);
285  for (i = 0; i < nindexatts; i++)
286  values[i] = index_getattr(itup, i + 1, itupdesc, &isnull[i]);
287  ExecStoreVirtualTuple(slot);
288 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:79
bool * tts_isnull
Definition: tuptable.h:126
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:365
#define Assert(condition)
Definition: c.h:688
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int i
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:524