PostgreSQL Source Code  git master
nodeTidscan.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "access/tableam.h"
#include "catalog/pg_type.h"
#include "executor/execdebug.h"
#include "executor/nodeTidscan.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "storage/bufmgr.h"
#include "utils/array.h"
#include "utils/rel.h"
Include dependency graph for nodeTidscan.c:

Go to the source code of this file.

Data Structures

struct  TidExpr
 

Macros

#define IsCTIDVar(node)
 

Typedefs

typedef struct TidExpr TidExpr
 

Functions

static void TidExprListCreate (TidScanState *tidstate)
 
static void TidListEval (TidScanState *tidstate)
 
static int itemptr_comparator (const void *a, const void *b)
 
static TupleTableSlotTidNext (TidScanState *node)
 
static bool TidRecheck (TidScanState *node, TupleTableSlot *slot)
 
static TupleTableSlotExecTidScan (PlanState *pstate)
 
void ExecReScanTidScan (TidScanState *node)
 
void ExecEndTidScan (TidScanState *node)
 
TidScanStateExecInitTidScan (TidScan *node, EState *estate, int eflags)
 

Macro Definition Documentation

◆ IsCTIDVar

#define IsCTIDVar (   node)
Value:
((node) != NULL && \
IsA((node), Var) && \
((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
((Var *) (node))->varlevelsup == 0)
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Definition: primnodes.h:181
struct Var Var
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21

Definition at line 38 of file nodeTidscan.c.

Referenced by TidExprListCreate().

Typedef Documentation

◆ TidExpr

typedef struct TidExpr TidExpr

Function Documentation

◆ ExecEndTidScan()

void ExecEndTidScan ( TidScanState node)

Definition at line 468 of file nodeTidscan.c.

References ExecClearTuple(), ExecFreeExprContext(), ScanState::ps, PlanState::ps_ResultTupleSlot, TidScanState::ss, ScanState::ss_currentScanDesc, ScanState::ss_ScanTupleSlot, and table_endscan().

Referenced by ExecEndNode().

469 {
470  if (node->ss.ss_currentScanDesc)
472 
473  /*
474  * Free the exprcontext
475  */
476  ExecFreeExprContext(&node->ss.ps);
477 
478  /*
479  * clear out tuple table slots
480  */
481  if (node->ss.ps.ps_ResultTupleSlot)
484 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1334
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1335
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:655
PlanState ps
Definition: execnodes.h:1332
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:983
ScanState ss
Definition: execnodes.h:1620
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:862

◆ ExecInitTidScan()

TidScanState* ExecInitTidScan ( TidScan node,
EState estate,
int  eflags 
)

Definition at line 498 of file nodeTidscan.c.

References ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecTidScan(), makeNode, Scan::plan, PlanState::plan, ScanState::ps, Plan::qual, PlanState::qual, RelationGetDescr, TidScan::scan, Scan::scanrelid, TidScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, PlanState::state, table_slot_callbacks(), TidExprListCreate(), TidScanState::tss_NumTids, TidScanState::tss_TidList, and TidScanState::tss_TidPtr.

Referenced by ExecInitNode().

499 {
500  TidScanState *tidstate;
501  Relation currentRelation;
502 
503  /*
504  * create state structure
505  */
506  tidstate = makeNode(TidScanState);
507  tidstate->ss.ps.plan = (Plan *) node;
508  tidstate->ss.ps.state = estate;
509  tidstate->ss.ps.ExecProcNode = ExecTidScan;
510 
511  /*
512  * Miscellaneous initialization
513  *
514  * create expression context for node
515  */
516  ExecAssignExprContext(estate, &tidstate->ss.ps);
517 
518  /*
519  * mark tid list as not computed yet
520  */
521  tidstate->tss_TidList = NULL;
522  tidstate->tss_NumTids = 0;
523  tidstate->tss_TidPtr = -1;
524 
525  /*
526  * open the scan relation
527  */
528  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
529 
530  tidstate->ss.ss_currentRelation = currentRelation;
531  tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
532 
533  /*
534  * get the scan type from the relation descriptor.
535  */
536  ExecInitScanTupleSlot(estate, &tidstate->ss,
537  RelationGetDescr(currentRelation),
538  table_slot_callbacks(currentRelation));
539 
540  /*
541  * Initialize result type and projection.
542  */
543  ExecInitResultTypeTL(&tidstate->ss.ps);
544  ExecAssignScanProjectionInfo(&tidstate->ss);
545 
546  /*
547  * initialize child expressions
548  */
549  tidstate->ss.ps.qual =
550  ExecInitQual(node->scan.plan.qual, (PlanState *) tidstate);
551 
552  TidExprListCreate(tidstate);
553 
554  /*
555  * all done.
556  */
557  return tidstate;
558 }
List * qual
Definition: plannodes.h:143
Plan plan
Definition: plannodes.h:344
Index scanrelid
Definition: plannodes.h:345
#define RelationGetDescr(relation)
Definition: rel.h:482
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:44
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1334
Relation ss_currentRelation
Definition: execnodes.h:1333
EState * state
Definition: execnodes.h:947
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:209
PlanState ps
Definition: execnodes.h:1332
ScanState ss
Definition: execnodes.h:1620
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:272
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:734
ItemPointerData * tss_TidList
Definition: execnodes.h:1625
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1781
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1725
static void TidExprListCreate(TidScanState *tidstate)
Definition: nodeTidscan.c:66
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:951
Plan * plan
Definition: execnodes.h:945
#define makeNode(_type_)
Definition: nodes.h:577
Scan scan
Definition: plannodes.h:492
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:485
ExprState * qual
Definition: execnodes.h:966
static TupleTableSlot * ExecTidScan(PlanState *pstate)
Definition: nodeTidscan.c:431

◆ ExecReScanTidScan()

void ExecReScanTidScan ( TidScanState node)

Definition at line 445 of file nodeTidscan.c.

References ExecScanReScan(), pfree(), TidScanState::ss, ScanState::ss_currentScanDesc, table_rescan(), TidScanState::tss_NumTids, TidScanState::tss_TidList, and TidScanState::tss_TidPtr.

Referenced by ExecReScan().

446 {
447  if (node->tss_TidList)
448  pfree(node->tss_TidList);
449  node->tss_TidList = NULL;
450  node->tss_NumTids = 0;
451  node->tss_TidPtr = -1;
452 
453  /* not really necessary, but seems good form */
454  if (node->ss.ss_currentScanDesc)
455  table_rescan(node->ss.ss_currentScanDesc, NULL);
456 
457  ExecScanReScan(&node->ss);
458 }
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1334
static void table_rescan(TableScanDesc scan, struct ScanKeyData *key)
Definition: tableam.h:871
void pfree(void *pointer)
Definition: mcxt.c:1056
ScanState ss
Definition: execnodes.h:1620
ItemPointerData * tss_TidList
Definition: execnodes.h:1625
void ExecScanReScan(ScanState *node)
Definition: execScan.c:299

◆ ExecTidScan()

static TupleTableSlot* ExecTidScan ( PlanState pstate)
static

Definition at line 431 of file nodeTidscan.c.

References castNode, ExecScan(), TidScanState::ss, TidNext(), and TidRecheck().

Referenced by ExecInitTidScan().

432 {
433  TidScanState *node = castNode(TidScanState, pstate);
434 
435  return ExecScan(&node->ss,
438 }
static TupleTableSlot * TidNext(TidScanState *node)
Definition: nodeTidscan.c:310
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:158
static bool TidRecheck(TidScanState *node, TupleTableSlot *slot)
Definition: nodeTidscan.c:401
ScanState ss
Definition: execnodes.h:1620
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:431
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:430

◆ itemptr_comparator()

static int itemptr_comparator ( const void *  a,
const void *  b 
)
static

Definition at line 281 of file nodeTidscan.c.

References ItemPointerGetBlockNumber, and ItemPointerGetOffsetNumber.

Referenced by TidListEval().

282 {
283  const ItemPointerData *ipa = (const ItemPointerData *) a;
284  const ItemPointerData *ipb = (const ItemPointerData *) b;
289 
290  if (ba < bb)
291  return -1;
292  if (ba > bb)
293  return 1;
294  if (oa < ob)
295  return -1;
296  if (oa > ob)
297  return 1;
298  return 0;
299 }
uint32 BlockNumber
Definition: block.h:31
uint16 OffsetNumber
Definition: off.h:24
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98

◆ TidExprListCreate()

static void TidExprListCreate ( TidScanState tidstate)
static

Definition at line 66 of file nodeTidscan.c.

References ScalarArrayOpExpr::args, Assert, TidExpr::cexpr, elog, ERROR, ExecInitExpr(), TidExpr::exprstate, get_leftop(), get_rightop(), is_opclause(), IsA, TidExpr::isarray, IsCTIDVar, lappend(), lfirst, linitial, list_length(), lsecond, NIL, palloc0(), PlanState::plan, ScanState::ps, TidScanState::ss, TidScan::tidquals, TidScanState::tss_isCurrentOf, and TidScanState::tss_tidexprs.

Referenced by ExecInitTidScan().

67 {
68  TidScan *node = (TidScan *) tidstate->ss.ps.plan;
69  ListCell *l;
70 
71  tidstate->tss_tidexprs = NIL;
72  tidstate->tss_isCurrentOf = false;
73 
74  foreach(l, node->tidquals)
75  {
76  Expr *expr = (Expr *) lfirst(l);
77  TidExpr *tidexpr = (TidExpr *) palloc0(sizeof(TidExpr));
78 
79  if (is_opclause(expr))
80  {
81  Node *arg1;
82  Node *arg2;
83 
84  arg1 = get_leftop(expr);
85  arg2 = get_rightop(expr);
86  if (IsCTIDVar(arg1))
87  tidexpr->exprstate = ExecInitExpr((Expr *) arg2,
88  &tidstate->ss.ps);
89  else if (IsCTIDVar(arg2))
90  tidexpr->exprstate = ExecInitExpr((Expr *) arg1,
91  &tidstate->ss.ps);
92  else
93  elog(ERROR, "could not identify CTID variable");
94  tidexpr->isarray = false;
95  }
96  else if (expr && IsA(expr, ScalarArrayOpExpr))
97  {
98  ScalarArrayOpExpr *saex = (ScalarArrayOpExpr *) expr;
99 
100  Assert(IsCTIDVar(linitial(saex->args)));
101  tidexpr->exprstate = ExecInitExpr(lsecond(saex->args),
102  &tidstate->ss.ps);
103  tidexpr->isarray = true;
104  }
105  else if (expr && IsA(expr, CurrentOfExpr))
106  {
107  CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
108 
109  tidexpr->cexpr = cexpr;
110  tidstate->tss_isCurrentOf = true;
111  }
112  else
113  elog(ERROR, "could not identify CTID expression");
114 
115  tidstate->tss_tidexprs = lappend(tidstate->tss_tidexprs, tidexpr);
116  }
117 
118  /* CurrentOfExpr could never appear OR'd with something else */
119  Assert(list_length(tidstate->tss_tidexprs) == 1 ||
120  !tidstate->tss_isCurrentOf);
121 }
CurrentOfExpr * cexpr
Definition: nodeTidscan.c:49
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
bool isarray
Definition: nodeTidscan.c:48
List * tidquals
Definition: plannodes.h:493
Definition: nodes.h:529
#define lsecond(l)
Definition: pg_list.h:200
PlanState ps
Definition: execnodes.h:1332
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
ScanState ss
Definition: execnodes.h:1620
List * tss_tidexprs
Definition: execnodes.h:1621
bool tss_isCurrentOf
Definition: execnodes.h:1622
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
List * lappend(List *list, void *datum)
Definition: list.c:321
void * palloc0(Size size)
Definition: mcxt.c:980
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
Plan * plan
Definition: execnodes.h:945
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
#define IsCTIDVar(node)
Definition: nodeTidscan.c:38
#define elog(elevel,...)
Definition: elog.h:214
ExprState * exprstate
Definition: nodeTidscan.c:47
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63

◆ TidListEval()

static void TidListEval ( TidScanState tidstate)
static

Definition at line 130 of file nodeTidscan.c.

References Assert, TidExpr::cexpr, DatumGetArrayTypeP, DatumGetPointer, deconstruct_array(), EState::es_snapshot, execCurrentOf(), ExecEvalExprSwitchContext(), TidExpr::exprstate, i, TidExpr::isarray, itemptr_comparator(), lfirst, list_length(), palloc(), pfree(), ScanState::ps, PlanState::ps_ExprContext, qsort, qunique(), RelationGetRelid, repalloc(), TidScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, PlanState::state, table_beginscan_tid(), table_tuple_tid_valid(), TidScanState::tss_isCurrentOf, TidScanState::tss_NumTids, TidScanState::tss_tidexprs, TidScanState::tss_TidList, and TidScanState::tss_TidPtr.

Referenced by TidNext().

131 {
132  ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
133  TableScanDesc scan;
134  ItemPointerData *tidList;
135  int numAllocTids;
136  int numTids;
137  ListCell *l;
138 
139  /*
140  * Start scan on-demand - initializing a scan isn't free (e.g. heap stats
141  * the size of the table), so it makes sense to delay that until needed -
142  * the node might never get executed.
143  */
144  if (tidstate->ss.ss_currentScanDesc == NULL)
145  tidstate->ss.ss_currentScanDesc =
147  tidstate->ss.ps.state->es_snapshot);
148  scan = tidstate->ss.ss_currentScanDesc;
149 
150  /*
151  * We initialize the array with enough slots for the case that all quals
152  * are simple OpExprs or CurrentOfExprs. If there are any
153  * ScalarArrayOpExprs, we may have to enlarge the array.
154  */
155  numAllocTids = list_length(tidstate->tss_tidexprs);
156  tidList = (ItemPointerData *)
157  palloc(numAllocTids * sizeof(ItemPointerData));
158  numTids = 0;
159 
160  foreach(l, tidstate->tss_tidexprs)
161  {
162  TidExpr *tidexpr = (TidExpr *) lfirst(l);
163  ItemPointer itemptr;
164  bool isNull;
165 
166  if (tidexpr->exprstate && !tidexpr->isarray)
167  {
168  itemptr = (ItemPointer)
170  econtext,
171  &isNull));
172  if (isNull)
173  continue;
174 
175  /*
176  * We silently discard any TIDs that the AM considers invalid
177  * (E.g. for heap, they could be out of range at the time of scan
178  * start. Since we hold at least AccessShareLock on the table, it
179  * won't be possible for someone to truncate away the blocks we
180  * intend to visit.).
181  */
182  if (!table_tuple_tid_valid(scan, itemptr))
183  continue;
184 
185  if (numTids >= numAllocTids)
186  {
187  numAllocTids *= 2;
188  tidList = (ItemPointerData *)
189  repalloc(tidList,
190  numAllocTids * sizeof(ItemPointerData));
191  }
192  tidList[numTids++] = *itemptr;
193  }
194  else if (tidexpr->exprstate && tidexpr->isarray)
195  {
196  Datum arraydatum;
197  ArrayType *itemarray;
198  Datum *ipdatums;
199  bool *ipnulls;
200  int ndatums;
201  int i;
202 
203  arraydatum = ExecEvalExprSwitchContext(tidexpr->exprstate,
204  econtext,
205  &isNull);
206  if (isNull)
207  continue;
208  itemarray = DatumGetArrayTypeP(arraydatum);
209  deconstruct_array(itemarray,
210  TIDOID, sizeof(ItemPointerData), false, TYPALIGN_SHORT,
211  &ipdatums, &ipnulls, &ndatums);
212  if (numTids + ndatums > numAllocTids)
213  {
214  numAllocTids = numTids + ndatums;
215  tidList = (ItemPointerData *)
216  repalloc(tidList,
217  numAllocTids * sizeof(ItemPointerData));
218  }
219  for (i = 0; i < ndatums; i++)
220  {
221  if (ipnulls[i])
222  continue;
223 
224  itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
225 
226  if (!table_tuple_tid_valid(scan, itemptr))
227  continue;
228 
229  tidList[numTids++] = *itemptr;
230  }
231  pfree(ipdatums);
232  pfree(ipnulls);
233  }
234  else
235  {
236  ItemPointerData cursor_tid;
237 
238  Assert(tidexpr->cexpr);
239  if (execCurrentOf(tidexpr->cexpr, econtext,
241  &cursor_tid))
242  {
243  if (numTids >= numAllocTids)
244  {
245  numAllocTids *= 2;
246  tidList = (ItemPointerData *)
247  repalloc(tidList,
248  numAllocTids * sizeof(ItemPointerData));
249  }
250  tidList[numTids++] = cursor_tid;
251  }
252  }
253  }
254 
255  /*
256  * Sort the array of TIDs into order, and eliminate duplicates.
257  * Eliminating duplicates is necessary since we want OR semantics across
258  * the list. Sorting makes it easier to detect duplicates, and as a bonus
259  * ensures that we will visit the heap in the most efficient way.
260  */
261  if (numTids > 1)
262  {
263  /* CurrentOfExpr could never appear OR'd with something else */
264  Assert(!tidstate->tss_isCurrentOf);
265 
266  qsort((void *) tidList, numTids, sizeof(ItemPointerData),
268  numTids = qunique(tidList, numTids, sizeof(ItemPointerData),
270  }
271 
272  tidstate->tss_TidList = tidList;
273  tidstate->tss_NumTids = numTids;
274  tidstate->tss_TidPtr = -1;
275 }
CurrentOfExpr * cexpr
Definition: nodeTidscan.c:49
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:305
ExprContext * ps_ExprContext
Definition: execnodes.h:984
bool isarray
Definition: nodeTidscan.c:48
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1334
Snapshot es_snapshot
Definition: execnodes.h:508
Relation ss_currentRelation
Definition: execnodes.h:1333
EState * state
Definition: execnodes.h:947
ItemPointerData * ItemPointer
Definition: itemptr.h:49
PlanState ps
Definition: execnodes.h:1332
void pfree(void *pointer)
Definition: mcxt.c:1056
ScanState ss
Definition: execnodes.h:1620
List * tss_tidexprs
Definition: execnodes.h:1621
ItemPointerData * tss_TidList
Definition: execnodes.h:1625
bool tss_isCurrentOf
Definition: execnodes.h:1622
static int itemptr_comparator(const void *a, const void *b)
Definition: nodeTidscan.c:281
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
bool execCurrentOf(CurrentOfExpr *cexpr, ExprContext *econtext, Oid table_oid, ItemPointer current_tid)
Definition: execCurrent.c:44
#define DatumGetPointer(X)
Definition: postgres.h:549
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3462
static TableScanDesc table_beginscan_tid(Relation rel, Snapshot snapshot)
Definition: tableam.h:838
void * palloc(Size size)
Definition: mcxt.c:949
int i
static bool table_tuple_tid_valid(TableScanDesc scan, ItemPointer tid)
Definition: tableam.h:1069
ExprState * exprstate
Definition: nodeTidscan.c:47
#define qsort(a, b, c, d)
Definition: port.h:479
#define RelationGetRelid(relation)
Definition: rel.h:456
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ TidNext()

static TupleTableSlot * TidNext ( TidScanState node)
static

Definition at line 310 of file nodeTidscan.c.

References CHECK_FOR_INTERRUPTS, EState::es_direction, EState::es_snapshot, ExecClearTuple(), ScanState::ps, ScanDirectionIsBackward, TidScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, ScanState::ss_ScanTupleSlot, PlanState::state, table_tuple_fetch_row_version(), table_tuple_get_latest_tid(), TidListEval(), TidScanState::tss_isCurrentOf, TidScanState::tss_NumTids, TidScanState::tss_TidList, and TidScanState::tss_TidPtr.

Referenced by ExecTidScan().

311 {
312  EState *estate;
313  ScanDirection direction;
314  Snapshot snapshot;
315  TableScanDesc scan;
316  Relation heapRelation;
317  TupleTableSlot *slot;
318  ItemPointerData *tidList;
319  int numTids;
320  bool bBackward;
321 
322  /*
323  * extract necessary information from tid scan node
324  */
325  estate = node->ss.ps.state;
326  direction = estate->es_direction;
327  snapshot = estate->es_snapshot;
328  heapRelation = node->ss.ss_currentRelation;
329  slot = node->ss.ss_ScanTupleSlot;
330 
331  /*
332  * First time through, compute the list of TIDs to be visited
333  */
334  if (node->tss_TidList == NULL)
335  TidListEval(node);
336 
337  scan = node->ss.ss_currentScanDesc;
338  tidList = node->tss_TidList;
339  numTids = node->tss_NumTids;
340 
341  /*
342  * Initialize or advance scan position, depending on direction.
343  */
344  bBackward = ScanDirectionIsBackward(direction);
345  if (bBackward)
346  {
347  if (node->tss_TidPtr < 0)
348  {
349  /* initialize for backward scan */
350  node->tss_TidPtr = numTids - 1;
351  }
352  else
353  node->tss_TidPtr--;
354  }
355  else
356  {
357  if (node->tss_TidPtr < 0)
358  {
359  /* initialize for forward scan */
360  node->tss_TidPtr = 0;
361  }
362  else
363  node->tss_TidPtr++;
364  }
365 
366  while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
367  {
368  ItemPointerData tid = tidList[node->tss_TidPtr];
369 
370  /*
371  * For WHERE CURRENT OF, the tuple retrieved from the cursor might
372  * since have been updated; if so, we should fetch the version that is
373  * current according to our snapshot.
374  */
375  if (node->tss_isCurrentOf)
376  table_tuple_get_latest_tid(scan, &tid);
377 
378  if (table_tuple_fetch_row_version(heapRelation, &tid, snapshot, slot))
379  return slot;
380 
381  /* Bad TID or failed snapshot qual; try next */
382  if (bBackward)
383  node->tss_TidPtr--;
384  else
385  node->tss_TidPtr++;
386 
388  }
389 
390  /*
391  * if we get here it means the tid scan failed so we are at the end of the
392  * scan..
393  */
394  return ExecClearTuple(slot);
395 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1334
Snapshot es_snapshot
Definition: execnodes.h:508
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1335
Relation ss_currentRelation
Definition: execnodes.h:1333
EState * state
Definition: execnodes.h:947
static void TidListEval(TidScanState *tidstate)
Definition: nodeTidscan.c:130
ScanDirection es_direction
Definition: execnodes.h:507
PlanState ps
Definition: execnodes.h:1332
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
ScanState ss
Definition: execnodes.h:1620
ItemPointerData * tss_TidList
Definition: execnodes.h:1625
ScanDirection
Definition: sdir.h:22
bool tss_isCurrentOf
Definition: execnodes.h:1622
void table_tuple_get_latest_tid(TableScanDesc scan, ItemPointer tid)
Definition: tableam.c:228
static bool table_tuple_fetch_row_version(Relation rel, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot)
Definition: tableam.h:1052
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ TidRecheck()

static bool TidRecheck ( TidScanState node,
TupleTableSlot slot 
)
static

Definition at line 401 of file nodeTidscan.c.

Referenced by ExecTidScan().

402 {
403  /*
404  * XXX shouldn't we check here to make sure tuple matches TID list? In
405  * runtime-key case this is not certain, is it? However, in the WHERE
406  * CURRENT OF case it might not match anyway ...
407  */
408  return true;
409 }