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

368 {
369  Relation indexRelationDesc;
370  IndexScanDesc indexScanDesc;
371 
372  /*
373  * extract information from the node
374  */
375  indexRelationDesc = node->ioss_RelationDesc;
376  indexScanDesc = node->ioss_ScanDesc;
377 
378  /* Release VM buffer pin, if any. */
379  if (node->ioss_VMBuffer != InvalidBuffer)
380  {
383  }
384 
385  /*
386  * Free the exprcontext(s) ... now dead code, see ExecFreeExprContext
387  */
388 #ifdef NOT_USED
389  ExecFreeExprContext(&node->ss.ps);
390  if (node->ioss_RuntimeContext)
392 #endif
393 
394  /*
395  * clear out tuple table slots
396  */
397  if (node->ss.ps.ps_ResultTupleSlot)
400 
401  /*
402  * close the index relation (no-op if we didn't open it)
403  */
404  if (indexScanDesc)
405  index_endscan(indexScanDesc);
406  if (indexRelationDesc)
407  index_close(indexRelationDesc, NoLock);
408 }
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3931
void FreeExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:413
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:652
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:323
#define NoLock
Definition: lockdefs.h:34
struct IndexScanDescData * ioss_ScanDesc
Definition: execnodes.h:1598
ExprContext * ioss_RuntimeContext
Definition: execnodes.h:1596
Relation ioss_RelationDesc
Definition: execnodes.h:1597
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1067
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1457
PlanState ps
Definition: execnodes.h:1454
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433

References ExecClearTuple(), 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, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

◆ ExecIndexOnlyMarkPos()

void ExecIndexOnlyMarkPos ( IndexOnlyScanState node)

Definition at line 418 of file nodeIndexonlyscan.c.

419 {
420  EState *estate = node->ss.ps.state;
421  EPQState *epqstate = estate->es_epq_active;
422 
423  if (epqstate != NULL)
424  {
425  /*
426  * We are inside an EvalPlanQual recheck. If a test tuple exists for
427  * this relation, then we shouldn't access the index at all. We would
428  * instead need to save, and later restore, the state of the
429  * relsubs_done flag, so that re-fetching the test tuple is possible.
430  * However, given the assumption that no caller sets a mark at the
431  * start of the scan, we can only get here with relsubs_done[i]
432  * already set, and so no state need be saved.
433  */
434  Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
435 
436  Assert(scanrelid > 0);
437  if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
438  epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
439  {
440  /* Verify the claim above */
441  if (!epqstate->relsubs_done[scanrelid - 1])
442  elog(ERROR, "unexpected ExecIndexOnlyMarkPos call in EPQ recheck");
443  return;
444  }
445  }
446 
448 }
unsigned int Index
Definition: c.h:550
#define ERROR
Definition: elog.h:35
void index_markpos(IndexScanDesc scan)
Definition: indexam.c:353
Assert(fmt[strlen(fmt) - 1] !='\n')
ExecAuxRowMark ** relsubs_rowmark
Definition: execnodes.h:1203
TupleTableSlot ** relsubs_slot
Definition: execnodes.h:1175
bool * relsubs_done
Definition: execnodes.h:1209
struct EPQState * es_epq_active
Definition: execnodes.h:684
Plan * plan
Definition: execnodes.h:1029
EState * state
Definition: execnodes.h:1031

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

456 {
457  EState *estate = node->ss.ps.state;
458  EPQState *epqstate = estate->es_epq_active;
459 
460  if (estate->es_epq_active != NULL)
461  {
462  /* See comments in ExecIndexMarkPos */
463  Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
464 
465  Assert(scanrelid > 0);
466  if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
467  epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
468  {
469  /* Verify the claim above */
470  if (!epqstate->relsubs_done[scanrelid - 1])
471  elog(ERROR, "unexpected ExecIndexOnlyRestrPos call in EPQ recheck");
472  return;
473  }
474  }
475 
477 }
void index_restrpos(IndexScanDesc scan)
Definition: indexam.c:377

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

306 {
308 
309  /*
310  * If we have runtime keys and they've not already been set up, do it now.
311  */
312  if (node->ioss_NumRuntimeKeys != 0 && !node->ioss_RuntimeKeysReady)
313  ExecReScan((PlanState *) node);
314 
315  return ExecScan(&node->ss,
318 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:158
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:458
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:459
static TupleTableSlot * IndexOnlyNext(IndexOnlyScanState *node)
static bool IndexOnlyRecheck(IndexOnlyScanState *node, TupleTableSlot *slot)
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
bool ioss_RuntimeKeysReady
Definition: execnodes.h:1595

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

646 {
647  EState *estate = node->ss.ps.state;
648 
650  estate->es_snapshot);
652  shm_toc_estimate_keys(&pcxt->estimator, 1);
653 }
Size index_parallelscan_estimate(Relation indexRelation, Snapshot snapshot)
Definition: indexam.c:402
#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:612
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 662 of file nodeIndexonlyscan.c.

664 {
665  EState *estate = node->ss.ps.state;
666  ParallelIndexScanDesc piscan;
667 
668  piscan = shm_toc_allocate(pcxt->toc, node->ioss_PscanLen);
670  node->ioss_RelationDesc,
671  estate->es_snapshot,
672  piscan);
673  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
674  node->ioss_ScanDesc =
676  node->ioss_RelationDesc,
677  node->ioss_NumScanKeys,
678  node->ioss_NumOrderByKeys,
679  piscan);
680  node->ioss_ScanDesc->xs_want_itup = true;
682 
683  /*
684  * If no run-time keys to calculate or they are ready, go ahead and pass
685  * the scankeys to the index AM.
686  */
687  if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
689  node->ioss_ScanKeys, node->ioss_NumScanKeys,
691 }
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:484
void index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, ParallelIndexScanDesc target)
Definition: indexam.c:435
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:297
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:1589
struct ScanKeyData * ioss_OrderByKeys
Definition: execnodes.h:1591
shm_toc * toc
Definition: parallel.h:45
int plan_node_id
Definition: plannodes.h:152
Relation ss_currentRelation
Definition: execnodes.h:1455

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

715 {
716  ParallelIndexScanDesc piscan;
717 
718  piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
719  node->ioss_ScanDesc =
721  node->ioss_RelationDesc,
722  node->ioss_NumScanKeys,
723  node->ioss_NumOrderByKeys,
724  piscan);
725  node->ioss_ScanDesc->xs_want_itup = true;
726 
727  /*
728  * If no run-time keys to calculate or they are ready, go ahead and pass
729  * the scankeys to the index AM.
730  */
731  if (node->ioss_NumRuntimeKeys == 0 || node->ioss_RuntimeKeysReady)
733  node->ioss_ScanKeys, node->ioss_NumScanKeys,
735 }
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 700 of file nodeIndexonlyscan.c.

702 {
704 }
void index_parallelrescan(IndexScanDesc scan)
Definition: indexam.c:466

References index_parallelrescan(), and IndexOnlyScanState::ioss_ScanDesc.

Referenced by ExecParallelReInitializeDSM().

◆ ExecInitIndexOnlyScan()

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

Definition at line 491 of file nodeIndexonlyscan.c.

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

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

References Assert(), BackwardScanDirection, CHECK_FOR_INTERRUPTS, ExprContext::ecxt_scantuple, elog(), ereport, errcode(), errmsg(), ERROR, EState::es_direction, EState::es_snapshot, ExecClearTuple(), ExecForceStoreHeapTuple(), ExecQualAndReset(), ForwardScanDirection, 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, ScanDirectionIsBackward, ScanDirectionIsForward, 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 294 of file nodeIndexonlyscan.c.

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

References elog(), and ERROR.

Referenced by ExecIndexOnlyScan().

◆ StoreIndexTuple()

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

Definition at line 269 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  ExecStoreVirtualTuple(slot);
283 }
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:456
bool * tts_isnull
Definition: tuptable.h:128
Datum * tts_values
Definition: tuptable.h:126

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

Referenced by IndexOnlyNext().