PostgreSQL Source Code  git master
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 "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 365 of file nodeIndexonlyscan.c.

366 {
367  Relation indexRelationDesc;
368  IndexScanDesc indexScanDesc;
369 
370  /*
371  * extract information from the node
372  */
373  indexRelationDesc = node->ioss_RelationDesc;
374  indexScanDesc = node->ioss_ScanDesc;
375 
376  /* Release VM buffer pin, if any. */
377  if (node->ioss_VMBuffer != InvalidBuffer)
378  {
381  }
382 
383  /*
384  * close the index relation (no-op if we didn't open it)
385  */
386  if (indexScanDesc)
387  index_endscan(indexScanDesc);
388  if (indexRelationDesc)
389  index_close(indexRelationDesc, NoLock);
390 }
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4561
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:379
#define NoLock
Definition: lockdefs.h:34
struct IndexScanDescData * ioss_ScanDesc
Definition: execnodes.h:1624
Relation ioss_RelationDesc
Definition: execnodes.h:1623

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

401 {
402  EState *estate = node->ss.ps.state;
403  EPQState *epqstate = estate->es_epq_active;
404 
405  if (epqstate != NULL)
406  {
407  /*
408  * We are inside an EvalPlanQual recheck. If a test tuple exists for
409  * this relation, then we shouldn't access the index at all. We would
410  * instead need to save, and later restore, the state of the
411  * relsubs_done flag, so that re-fetching the test tuple is possible.
412  * However, given the assumption that no caller sets a mark at the
413  * start of the scan, we can only get here with relsubs_done[i]
414  * already set, and so no state need be saved.
415  */
416  Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
417 
418  Assert(scanrelid > 0);
419  if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
420  epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
421  {
422  /* Verify the claim above */
423  if (!epqstate->relsubs_done[scanrelid - 1])
424  elog(ERROR, "unexpected ExecIndexOnlyMarkPos call in EPQ recheck");
425  return;
426  }
427  }
428 
430 }
unsigned int Index
Definition: c.h:603
#define ERROR
Definition: elog.h:39
void index_markpos(IndexScanDesc scan)
Definition: indexam.c:409
Assert(fmt[strlen(fmt) - 1] !='\n')
ExecAuxRowMark ** relsubs_rowmark
Definition: execnodes.h:1218
TupleTableSlot ** relsubs_slot
Definition: execnodes.h:1190
bool * relsubs_done
Definition: execnodes.h:1225
struct EPQState * es_epq_active
Definition: execnodes.h:697
Plan * plan
Definition: execnodes.h:1043
EState * state
Definition: execnodes.h:1045
PlanState ps
Definition: execnodes.h:1480

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

438 {
439  EState *estate = node->ss.ps.state;
440  EPQState *epqstate = estate->es_epq_active;
441 
442  if (estate->es_epq_active != NULL)
443  {
444  /* See comments in ExecIndexMarkPos */
445  Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
446 
447  Assert(scanrelid > 0);
448  if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
449  epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
450  {
451  /* Verify the claim above */
452  if (!epqstate->relsubs_done[scanrelid - 1])
453  elog(ERROR, "unexpected ExecIndexOnlyRestrPos call in EPQ recheck");
454  return;
455  }
456  }
457 
459 }
void index_restrpos(IndexScanDesc scan)
Definition: indexam.c:433

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

304 {
306 
307  /*
308  * If we have runtime keys and they've not already been set up, do it now.
309  */
310  if (node->ioss_NumRuntimeKeys != 0 && !node->ioss_RuntimeKeysReady)
311  ExecReScan((PlanState *) node);
312 
313  return ExecScan(&node->ss,
316 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:157
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:472
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:473
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:1621

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

628 {
629  EState *estate = node->ss.ps.state;
630 
632  estate->es_snapshot);
634  shm_toc_estimate_keys(&pcxt->estimator, 1);
635 }
Size index_parallelscan_estimate(Relation indexRelation, Snapshot snapshot)
Definition: indexam.c:458
#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:622
shm_toc_estimator estimator
Definition: parallel.h:42

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

◆ ExecIndexOnlyScanInitializeDSM()

void ExecIndexOnlyScanInitializeDSM ( IndexOnlyScanState node,
ParallelContext pcxt 
)

Definition at line 644 of file nodeIndexonlyscan.c.

646 {
647  EState *estate = node->ss.ps.state;
648  ParallelIndexScanDesc piscan;
649 
650  piscan = shm_toc_allocate(pcxt->toc, node->ioss_PscanLen);
652  node->ioss_RelationDesc,
653  estate->es_snapshot,
654  piscan);
655  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
656  node->ioss_ScanDesc =
658  node->ioss_RelationDesc,
659  node->ioss_NumScanKeys,
660  node->ioss_NumOrderByKeys,
661  piscan);
662  node->ioss_ScanDesc->xs_want_itup = true;
664 
665  /*
666  * If no run-time keys to calculate or they are ready, go ahead and pass
667  * the scankeys to the index AM.
668  */
669  if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
671  node->ioss_ScanKeys, node->ioss_NumScanKeys,
673 }
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:544
void index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, ParallelIndexScanDesc target)
Definition: indexam.c:493
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:353
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
struct ScanKeyData * ioss_ScanKeys
Definition: execnodes.h:1615
struct ScanKeyData * ioss_OrderByKeys
Definition: execnodes.h:1617
shm_toc * toc
Definition: parallel.h:45
int plan_node_id
Definition: plannodes.h:151
Relation ss_currentRelation
Definition: execnodes.h:1481

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

697 {
698  ParallelIndexScanDesc piscan;
699 
700  piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
701  node->ioss_ScanDesc =
703  node->ioss_RelationDesc,
704  node->ioss_NumScanKeys,
705  node->ioss_NumOrderByKeys,
706  piscan);
707  node->ioss_ScanDesc->xs_want_itup = true;
708 
709  /*
710  * If no run-time keys to calculate or they are ready, go ahead and pass
711  * the scankeys to the index AM.
712  */
713  if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
715  node->ioss_ScanKeys, node->ioss_NumScanKeys,
717 }
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 682 of file nodeIndexonlyscan.c.

684 {
686 }
void index_parallelrescan(IndexScanDesc scan)
Definition: indexam.c:526

References index_parallelrescan(), and IndexOnlyScanState::ioss_ScanDesc.

Referenced by ExecParallelReInitializeDSM().

◆ ExecInitIndexOnlyScan()

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

Definition at line 473 of file nodeIndexonlyscan.c.

474 {
475  IndexOnlyScanState *indexstate;
476  Relation currentRelation;
477  LOCKMODE lockmode;
478  TupleDesc tupDesc;
479 
480  /*
481  * create state structure
482  */
483  indexstate = makeNode(IndexOnlyScanState);
484  indexstate->ss.ps.plan = (Plan *) node;
485  indexstate->ss.ps.state = estate;
486  indexstate->ss.ps.ExecProcNode = ExecIndexOnlyScan;
487 
488  /*
489  * Miscellaneous initialization
490  *
491  * create expression context for node
492  */
493  ExecAssignExprContext(estate, &indexstate->ss.ps);
494 
495  /*
496  * open the scan relation
497  */
498  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
499 
500  indexstate->ss.ss_currentRelation = currentRelation;
501  indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
502 
503  /*
504  * Build the scan tuple type using the indextlist generated by the
505  * planner. We use this, rather than the index's physical tuple
506  * descriptor, because the latter contains storage column types not the
507  * types of the original datums. (It's the AM's responsibility to return
508  * suitable data anyway.)
509  */
510  tupDesc = ExecTypeFromTL(node->indextlist);
511  ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc,
512  &TTSOpsVirtual);
513 
514  /*
515  * We need another slot, in a format that's suitable for the table AM, for
516  * when we need to fetch a tuple from the table for rechecking visibility.
517  */
518  indexstate->ioss_TableSlot =
520  RelationGetDescr(currentRelation),
521  table_slot_callbacks(currentRelation));
522 
523  /*
524  * Initialize result type and projection info. The node's targetlist will
525  * contain Vars with varno = INDEX_VAR, referencing the scan tuple.
526  */
527  ExecInitResultTypeTL(&indexstate->ss.ps);
529 
530  /*
531  * initialize child expressions
532  *
533  * Note: we don't initialize all of the indexorderby expression, only the
534  * sub-parts corresponding to runtime keys (see below).
535  */
536  indexstate->ss.ps.qual =
537  ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
538  indexstate->recheckqual =
539  ExecInitQual(node->recheckqual, (PlanState *) indexstate);
540 
541  /*
542  * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
543  * here. This allows an index-advisor plugin to EXPLAIN a plan containing
544  * references to nonexistent indexes.
545  */
546  if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
547  return indexstate;
548 
549  /* Open the index relation. */
550  lockmode = exec_rt_fetch(node->scan.scanrelid, estate)->rellockmode;
551  indexstate->ioss_RelationDesc = index_open(node->indexid, lockmode);
552 
553  /*
554  * Initialize index-specific scan state
555  */
556  indexstate->ioss_RuntimeKeysReady = false;
557  indexstate->ioss_RuntimeKeys = NULL;
558  indexstate->ioss_NumRuntimeKeys = 0;
559 
560  /*
561  * build the index scan keys from the index qualification
562  */
563  ExecIndexBuildScanKeys((PlanState *) indexstate,
564  indexstate->ioss_RelationDesc,
565  node->indexqual,
566  false,
567  &indexstate->ioss_ScanKeys,
568  &indexstate->ioss_NumScanKeys,
569  &indexstate->ioss_RuntimeKeys,
570  &indexstate->ioss_NumRuntimeKeys,
571  NULL, /* no ArrayKeys */
572  NULL);
573 
574  /*
575  * any ORDER BY exprs have to be turned into scankeys in the same way
576  */
577  ExecIndexBuildScanKeys((PlanState *) indexstate,
578  indexstate->ioss_RelationDesc,
579  node->indexorderby,
580  true,
581  &indexstate->ioss_OrderByKeys,
582  &indexstate->ioss_NumOrderByKeys,
583  &indexstate->ioss_RuntimeKeys,
584  &indexstate->ioss_NumRuntimeKeys,
585  NULL, /* no ArrayKeys */
586  NULL);
587 
588  /*
589  * If we have runtime keys, we need an ExprContext to evaluate them. The
590  * node's standard context won't do because we want to reset that context
591  * for every tuple. So, build another context just like the other one...
592  * -tgl 7/11/00
593  */
594  if (indexstate->ioss_NumRuntimeKeys != 0)
595  {
596  ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
597 
598  ExecAssignExprContext(estate, &indexstate->ss.ps);
599  indexstate->ioss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
600  indexstate->ss.ps.ps_ExprContext = stdecontext;
601  }
602  else
603  {
604  indexstate->ioss_RuntimeContext = NULL;
605  }
606 
607  /*
608  * all done.
609  */
610  return indexstate;
611 }
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:214
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, int varno)
Definition: execScan.c:284
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1810
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1754
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1937
TupleTableSlot * ExecAllocTableSlot(List **tupleTable, TupleDesc desc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1170
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:488
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:702
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition: executor.h:586
#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
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
#define INDEX_VAR
Definition: primnodes.h:224
#define RelationGetDescr(relation)
Definition: rel.h:530
List * es_tupleTable
Definition: execnodes.h:667
TupleTableSlot * ioss_TableSlot
Definition: execnodes.h:1625
ExprState * recheckqual
Definition: execnodes.h:1614
ExprContext * ioss_RuntimeContext
Definition: execnodes.h:1622
IndexRuntimeKeyInfo * ioss_RuntimeKeys
Definition: execnodes.h:1619
List * indexqual
Definition: plannodes.h:494
List * recheckqual
Definition: plannodes.h:495
List * indextlist
Definition: plannodes.h:497
List * indexorderby
Definition: plannodes.h:496
ExprState * qual
Definition: execnodes.h:1064
ExprContext * ps_ExprContext
Definition: execnodes.h:1082
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1049
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1482
Index scanrelid
Definition: plannodes.h:387
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:58

References EState::es_tupleTable, EXEC_FLAG_EXPLAIN_ONLY, exec_rt_fetch(), ExecAllocTableSlot(), ExecAssignExprContext(), ExecAssignScanProjectionInfoWithVarno(), ExecIndexBuildScanKeys(), ExecIndexOnlyScan(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecTypeFromTL(), index_open(), INDEX_VAR, IndexOnlyScan::indexid, IndexOnlyScan::indexorderby, IndexOnlyScan::indexqual, IndexOnlyScan::indextlist, 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, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, PlanState::qual, IndexOnlyScanState::recheckqual, IndexOnlyScan::recheckqual, RelationGetDescr, RangeTblEntry::rellockmode, IndexOnlyScan::scan, Scan::scanrelid, IndexOnlyScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, PlanState::state, table_slot_callbacks(), and TTSOpsVirtual.

Referenced by ExecInitNode().

◆ ExecReScanIndexOnlyScan()

void ExecReScanIndexOnlyScan ( IndexOnlyScanState node)

Definition at line 330 of file nodeIndexonlyscan.c.

331 {
332  /*
333  * If we are doing runtime key calculations (ie, any of the index key
334  * values weren't simple Consts), compute the new key values. But first,
335  * reset the context so we don't leak memory as each outer tuple is
336  * scanned. Note this assumes that we will recalculate *all* runtime keys
337  * on each call.
338  */
339  if (node->ioss_NumRuntimeKeys != 0)
340  {
341  ExprContext *econtext = node->ioss_RuntimeContext;
342 
343  ResetExprContext(econtext);
344  ExecIndexEvalRuntimeKeys(econtext,
345  node->ioss_RuntimeKeys,
346  node->ioss_NumRuntimeKeys);
347  }
348  node->ioss_RuntimeKeysReady = true;
349 
350  /* reset index scan */
351  if (node->ioss_ScanDesc)
353  node->ioss_ScanKeys, node->ioss_NumScanKeys,
355 
356  ExecScanReScan(&node->ss);
357 }
void ExecScanReScan(ScanState *node)
Definition: execScan.c:298
#define ResetExprContext(econtext)
Definition: executor.h:543
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 60 of file nodeIndexonlyscan.c.

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

293 {
294  elog(ERROR, "EvalPlanQual recheck is not supported in index-only scans");
295  return false; /* keep compiler quiet */
296 }

References elog(), and ERROR.

Referenced by ExecIndexOnlyScan().

◆ StoreIndexTuple()

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

Definition at line 267 of file nodeIndexonlyscan.c.

268 {
269  /*
270  * Note: we must use the tupdesc supplied by the AM in index_deform_tuple,
271  * not the slot's tupdesc, in case the latter has different datatypes
272  * (this happens for btree name_ops in particular). They'd better have
273  * the same number of columns though, as well as being datatype-compatible
274  * which is something we can't so easily check.
275  */
276  Assert(slot->tts_tupleDescriptor->natts == itupdesc->natts);
277 
278  ExecClearTuple(slot);
279  index_deform_tuple(itup, itupdesc, slot->tts_values, slot->tts_isnull);
280  ExecStoreVirtualTuple(slot);
281 }
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1551
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:456
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125

References Assert(), ExecClearTuple(), ExecStoreVirtualTuple(), index_deform_tuple(), TupleDescData::natts, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by IndexOnlyNext().