PostgreSQL Source Code  git master
nodeTidrangescan.c File Reference
#include "postgres.h"
#include "access/relscan.h"
#include "access/sysattr.h"
#include "access/tableam.h"
#include "catalog/pg_operator.h"
#include "executor/execdebug.h"
#include "executor/nodeTidrangescan.h"
#include "nodes/nodeFuncs.h"
#include "storage/bufmgr.h"
#include "utils/rel.h"
Include dependency graph for nodeTidrangescan.c:

Go to the source code of this file.

Data Structures

struct  TidOpExpr
 

Macros

#define IsCTIDVar(node)
 

Typedefs

typedef struct TidOpExpr TidOpExpr
 

Enumerations

enum  TidExprType { TIDEXPR_UPPER_BOUND , TIDEXPR_LOWER_BOUND }
 

Functions

static TidOpExprMakeTidOpExpr (OpExpr *expr, TidRangeScanState *tidstate)
 
static void TidExprListCreate (TidRangeScanState *tidrangestate)
 
static bool TidRangeEval (TidRangeScanState *node)
 
static TupleTableSlotTidRangeNext (TidRangeScanState *node)
 
static bool TidRangeRecheck (TidRangeScanState *node, TupleTableSlot *slot)
 
static TupleTableSlotExecTidRangeScan (PlanState *pstate)
 
void ExecReScanTidRangeScan (TidRangeScanState *node)
 
void ExecEndTidRangeScan (TidRangeScanState *node)
 
TidRangeScanStateExecInitTidRangeScan (TidRangeScan *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:589
struct Var Var
Definition: primnodes.h:187
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21

Definition at line 28 of file nodeTidrangescan.c.

Typedef Documentation

◆ TidOpExpr

typedef struct TidOpExpr TidOpExpr

Enumeration Type Documentation

◆ TidExprType

Enumerator
TIDEXPR_UPPER_BOUND 
TIDEXPR_LOWER_BOUND 

Definition at line 34 of file nodeTidrangescan.c.

35 {
38 } TidExprType;
TidExprType
@ TIDEXPR_LOWER_BOUND
@ TIDEXPR_UPPER_BOUND

Function Documentation

◆ ExecEndTidRangeScan()

void ExecEndTidRangeScan ( TidRangeScanState node)

Definition at line 323 of file nodeTidrangescan.c.

324 {
325  TableScanDesc scan = node->ss.ss_currentScanDesc;
326 
327  if (scan != NULL)
328  table_endscan(scan);
329 
330  /*
331  * Free the exprcontext
332  */
333  ExecFreeExprContext(&node->ss.ps);
334 
335  /*
336  * clear out tuple table slots
337  */
338  if (node->ss.ps.ps_ResultTupleSlot)
341 }
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:650
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1005
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1381
PlanState ps
Definition: execnodes.h:1378
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1380
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425

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

Referenced by ExecEndNode().

◆ ExecInitTidRangeScan()

TidRangeScanState* ExecInitTidRangeScan ( TidRangeScan node,
EState estate,
int  eflags 
)

Definition at line 355 of file nodeTidrangescan.c.

356 {
357  TidRangeScanState *tidrangestate;
358  Relation currentRelation;
359 
360  /*
361  * create state structure
362  */
363  tidrangestate = makeNode(TidRangeScanState);
364  tidrangestate->ss.ps.plan = (Plan *) node;
365  tidrangestate->ss.ps.state = estate;
366  tidrangestate->ss.ps.ExecProcNode = ExecTidRangeScan;
367 
368  /*
369  * Miscellaneous initialization
370  *
371  * create expression context for node
372  */
373  ExecAssignExprContext(estate, &tidrangestate->ss.ps);
374 
375  /*
376  * mark scan as not in progress, and TID range as not computed yet
377  */
378  tidrangestate->trss_inScan = false;
379 
380  /*
381  * open the scan relation
382  */
383  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
384 
385  tidrangestate->ss.ss_currentRelation = currentRelation;
386  tidrangestate->ss.ss_currentScanDesc = NULL; /* no table scan here */
387 
388  /*
389  * get the scan type from the relation descriptor.
390  */
391  ExecInitScanTupleSlot(estate, &tidrangestate->ss,
392  RelationGetDescr(currentRelation),
393  table_slot_callbacks(currentRelation));
394 
395  /*
396  * Initialize result type and projection.
397  */
398  ExecInitResultTypeTL(&tidrangestate->ss.ps);
399  ExecAssignScanProjectionInfo(&tidrangestate->ss);
400 
401  /*
402  * initialize child expressions
403  */
404  tidrangestate->ss.ps.qual =
405  ExecInitQual(node->scan.plan.qual, (PlanState *) tidrangestate);
406 
407  TidExprListCreate(tidrangestate);
408 
409  /*
410  * all done.
411  */
412  return tidrangestate;
413 }
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:209
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:272
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1811
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1755
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:480
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:720
static void TidExprListCreate(TidRangeScanState *tidrangestate)
static TupleTableSlot * ExecTidRangeScan(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:586
#define RelationGetDescr(relation)
Definition: rel.h:504
ExprState * qual
Definition: execnodes.h:988
Plan * plan
Definition: execnodes.h:967
EState * state
Definition: execnodes.h:969
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:973
List * qual
Definition: plannodes.h:142
Relation ss_currentRelation
Definition: execnodes.h:1379
Plan plan
Definition: plannodes.h:343
Index scanrelid
Definition: plannodes.h:344
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:58

References ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecTidRangeScan(), makeNode, PlanState::plan, Scan::plan, ScanState::ps, PlanState::qual, Plan::qual, RelationGetDescr, TidRangeScan::scan, Scan::scanrelid, TidRangeScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, PlanState::state, table_slot_callbacks(), TidExprListCreate(), and TidRangeScanState::trss_inScan.

Referenced by ExecInitNode().

◆ ExecReScanTidRangeScan()

void ExecReScanTidRangeScan ( TidRangeScanState node)

Definition at line 304 of file nodeTidrangescan.c.

305 {
306  /* mark scan as not in progress, and tid range list as not computed yet */
307  node->trss_inScan = false;
308 
309  /*
310  * We must wait until TidRangeNext before calling table_rescan_tidrange.
311  */
312  ExecScanReScan(&node->ss);
313 }
void ExecScanReScan(ScanState *node)
Definition: execScan.c:299

References ExecScanReScan(), TidRangeScanState::ss, and TidRangeScanState::trss_inScan.

Referenced by ExecReScan().

◆ ExecTidRangeScan()

static TupleTableSlot* ExecTidRangeScan ( PlanState pstate)
static

Definition at line 290 of file nodeTidrangescan.c.

291 {
293 
294  return ExecScan(&node->ss,
297 }
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:158
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:456
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:457
static TupleTableSlot * TidRangeNext(TidRangeScanState *node)
static bool TidRangeRecheck(TidRangeScanState *node, TupleTableSlot *slot)
#define castNode(_type_, nodeptr)
Definition: nodes.h:607

References castNode, ExecScan(), TidRangeScanState::ss, TidRangeNext(), and TidRangeRecheck().

Referenced by ExecInitTidRangeScan().

◆ MakeTidOpExpr()

static TidOpExpr* MakeTidOpExpr ( OpExpr expr,
TidRangeScanState tidstate 
)
static

Definition at line 53 of file nodeTidrangescan.c.

54 {
55  Node *arg1 = get_leftop((Expr *) expr);
56  Node *arg2 = get_rightop((Expr *) expr);
57  ExprState *exprstate = NULL;
58  bool invert = false;
59  TidOpExpr *tidopexpr;
60 
61  if (IsCTIDVar(arg1))
62  exprstate = ExecInitExpr((Expr *) arg2, &tidstate->ss.ps);
63  else if (IsCTIDVar(arg2))
64  {
65  exprstate = ExecInitExpr((Expr *) arg1, &tidstate->ss.ps);
66  invert = true;
67  }
68  else
69  elog(ERROR, "could not identify CTID variable");
70 
71  tidopexpr = (TidOpExpr *) palloc(sizeof(TidOpExpr));
72  tidopexpr->inclusive = false; /* for now */
73 
74  switch (expr->opno)
75  {
76  case TIDLessEqOperator:
77  tidopexpr->inclusive = true;
78  /* fall through */
79  case TIDLessOperator:
80  tidopexpr->exprtype = invert ? TIDEXPR_LOWER_BOUND : TIDEXPR_UPPER_BOUND;
81  break;
82  case TIDGreaterEqOperator:
83  tidopexpr->inclusive = true;
84  /* fall through */
85  case TIDGreaterOperator:
86  tidopexpr->exprtype = invert ? TIDEXPR_UPPER_BOUND : TIDEXPR_LOWER_BOUND;
87  break;
88  default:
89  elog(ERROR, "could not identify CTID operator");
90  }
91 
92  tidopexpr->exprstate = exprstate;
93 
94  return tidopexpr;
95 }
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123
void * palloc(Size size)
Definition: mcxt.c:1062
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:83
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:71
#define IsCTIDVar(node)
Definition: nodes.h:539
Oid opno
Definition: primnodes.h:542
ExprState * exprstate
TidExprType exprtype

References elog, ERROR, ExecInitExpr(), TidOpExpr::exprstate, TidOpExpr::exprtype, get_leftop(), get_rightop(), TidOpExpr::inclusive, IsCTIDVar, OpExpr::opno, palloc(), ScanState::ps, TidRangeScanState::ss, TIDEXPR_LOWER_BOUND, and TIDEXPR_UPPER_BOUND.

Referenced by TidExprListCreate().

◆ TidExprListCreate()

static void TidExprListCreate ( TidRangeScanState tidrangestate)
static

Definition at line 102 of file nodeTidrangescan.c.

103 {
104  TidRangeScan *node = (TidRangeScan *) tidrangestate->ss.ps.plan;
105  List *tidexprs = NIL;
106  ListCell *l;
107 
108  foreach(l, node->tidrangequals)
109  {
110  OpExpr *opexpr = lfirst(l);
111  TidOpExpr *tidopexpr;
112 
113  if (!IsA(opexpr, OpExpr))
114  elog(ERROR, "could not identify CTID expression");
115 
116  tidopexpr = MakeTidOpExpr(opexpr, tidrangestate);
117  tidexprs = lappend(tidexprs, tidopexpr);
118  }
119 
120  tidrangestate->trss_tidexprs = tidexprs;
121 }
List * lappend(List *list, void *datum)
Definition: list.c:336
static TidOpExpr * MakeTidOpExpr(OpExpr *expr, TidRangeScanState *tidstate)
#define lfirst(lc)
Definition: pg_list.h:169
#define NIL
Definition: pg_list.h:65
Definition: pg_list.h:51
List * trss_tidexprs
Definition: execnodes.h:1687
List * tidrangequals
Definition: plannodes.h:508

References elog, ERROR, IsA, lappend(), lfirst, MakeTidOpExpr(), NIL, PlanState::plan, ScanState::ps, TidRangeScanState::ss, TidRangeScan::tidrangequals, and TidRangeScanState::trss_tidexprs.

Referenced by ExecInitTidRangeScan().

◆ TidRangeEval()

static bool TidRangeEval ( TidRangeScanState node)
static

Definition at line 133 of file nodeTidrangescan.c.

134 {
135  ExprContext *econtext = node->ss.ps.ps_ExprContext;
136  ItemPointerData lowerBound;
137  ItemPointerData upperBound;
138  ListCell *l;
139 
140  /*
141  * Set the upper and lower bounds to the absolute limits of the range of
142  * the ItemPointer type. Below we'll try to narrow this range on either
143  * side by looking at the TidOpExprs.
144  */
145  ItemPointerSet(&lowerBound, 0, 0);
147 
148  foreach(l, node->trss_tidexprs)
149  {
150  TidOpExpr *tidopexpr = (TidOpExpr *) lfirst(l);
151  ItemPointer itemptr;
152  bool isNull;
153 
154  /* Evaluate this bound. */
155  itemptr = (ItemPointer)
157  econtext,
158  &isNull));
159 
160  /* If the bound is NULL, *nothing* matches the qual. */
161  if (isNull)
162  return false;
163 
164  if (tidopexpr->exprtype == TIDEXPR_LOWER_BOUND)
165  {
166  ItemPointerData lb;
167 
168  ItemPointerCopy(itemptr, &lb);
169 
170  /*
171  * Normalize non-inclusive ranges to become inclusive. The
172  * resulting ItemPointer here may not be a valid item pointer.
173  */
174  if (!tidopexpr->inclusive)
175  ItemPointerInc(&lb);
176 
177  /* Check if we can narrow the range using this qual */
178  if (ItemPointerCompare(&lb, &lowerBound) > 0)
179  ItemPointerCopy(&lb, &lowerBound);
180  }
181 
182  else if (tidopexpr->exprtype == TIDEXPR_UPPER_BOUND)
183  {
184  ItemPointerData ub;
185 
186  ItemPointerCopy(itemptr, &ub);
187 
188  /*
189  * Normalize non-inclusive ranges to become inclusive. The
190  * resulting ItemPointer here may not be a valid item pointer.
191  */
192  if (!tidopexpr->inclusive)
193  ItemPointerDec(&ub);
194 
195  /* Check if we can narrow the range using this qual */
196  if (ItemPointerCompare(&ub, &upperBound) < 0)
197  ItemPointerCopy(&ub, &upperBound);
198  }
199  }
200 
201  ItemPointerCopy(&lowerBound, &node->trss_mintid);
202  ItemPointerCopy(&upperBound, &node->trss_maxtid);
203 
204  return true;
205 }
#define InvalidBlockNumber
Definition: block.h:33
#define PG_UINT16_MAX
Definition: c.h:522
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:331
void ItemPointerDec(ItemPointer pointer)
Definition: itemptr.c:115
void ItemPointerInc(ItemPointer pointer)
Definition: itemptr.c:85
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127
ItemPointerData * ItemPointer
Definition: itemptr.h:49
#define DatumGetPointer(X)
Definition: postgres.h:593
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
ItemPointerData trss_maxtid
Definition: execnodes.h:1689
ItemPointerData trss_mintid
Definition: execnodes.h:1688

References DatumGetPointer, ExecEvalExprSwitchContext(), TidOpExpr::exprstate, TidOpExpr::exprtype, TidOpExpr::inclusive, InvalidBlockNumber, ItemPointerCompare(), ItemPointerCopy, ItemPointerDec(), ItemPointerInc(), ItemPointerSet, lfirst, PG_UINT16_MAX, ScanState::ps, PlanState::ps_ExprContext, TidRangeScanState::ss, TIDEXPR_LOWER_BOUND, TIDEXPR_UPPER_BOUND, TidRangeScanState::trss_maxtid, TidRangeScanState::trss_mintid, and TidRangeScanState::trss_tidexprs.

Referenced by TidRangeNext().

◆ TidRangeNext()

static TupleTableSlot* TidRangeNext ( TidRangeScanState node)
static

Definition at line 216 of file nodeTidrangescan.c.

217 {
218  TableScanDesc scandesc;
219  EState *estate;
220  ScanDirection direction;
221  TupleTableSlot *slot;
222 
223  /*
224  * extract necessary information from TID scan node
225  */
226  scandesc = node->ss.ss_currentScanDesc;
227  estate = node->ss.ps.state;
228  slot = node->ss.ss_ScanTupleSlot;
229  direction = estate->es_direction;
230 
231  if (!node->trss_inScan)
232  {
233  /* First time through, compute TID range to scan */
234  if (!TidRangeEval(node))
235  return NULL;
236 
237  if (scandesc == NULL)
238  {
240  estate->es_snapshot,
241  &node->trss_mintid,
242  &node->trss_maxtid);
243  node->ss.ss_currentScanDesc = scandesc;
244  }
245  else
246  {
247  /* rescan with the updated TID range */
248  table_rescan_tidrange(scandesc, &node->trss_mintid,
249  &node->trss_maxtid);
250  }
251 
252  node->trss_inScan = true;
253  }
254 
255  /* Fetch the next tuple. */
256  if (!table_scan_getnextslot_tidrange(scandesc, direction, slot))
257  {
258  node->trss_inScan = false;
259  ExecClearTuple(slot);
260  }
261 
262  return slot;
263 }
static bool TidRangeEval(TidRangeScanState *node)
ScanDirection
Definition: sdir.h:23
ScanDirection es_direction
Definition: execnodes.h:558
Snapshot es_snapshot
Definition: execnodes.h:559
static void table_rescan_tidrange(TableScanDesc sscan, ItemPointer mintid, ItemPointer maxtid)
Definition: tableam.h:1080
static TableScanDesc table_beginscan_tidrange(Relation rel, Snapshot snapshot, ItemPointer mintid, ItemPointer maxtid)
Definition: tableam.h:1059
static bool table_scan_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:1096

References EState::es_direction, EState::es_snapshot, ExecClearTuple(), ScanState::ps, TidRangeScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, ScanState::ss_ScanTupleSlot, PlanState::state, table_beginscan_tidrange(), table_rescan_tidrange(), table_scan_getnextslot_tidrange(), TidRangeEval(), TidRangeScanState::trss_inScan, TidRangeScanState::trss_maxtid, and TidRangeScanState::trss_mintid.

Referenced by ExecTidRangeScan().

◆ TidRangeRecheck()

static bool TidRangeRecheck ( TidRangeScanState node,
TupleTableSlot slot 
)
static

Definition at line 269 of file nodeTidrangescan.c.

270 {
271  return true;
272 }

Referenced by ExecTidRangeScan().