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/executor.h"
#include "executor/nodeTidrangescan.h"
#include "nodes/nodeFuncs.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)
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
struct Var Var
Definition: primnodes.h:234
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21

Definition at line 33 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 38 of file nodeTidrangescan.c.

39 {
42 } TidExprType;
TidExprType
@ TIDEXPR_LOWER_BOUND
@ TIDEXPR_UPPER_BOUND

Function Documentation

◆ ExecEndTidRangeScan()

void ExecEndTidRangeScan ( TidRangeScanState node)

Definition at line 327 of file nodeTidrangescan.c.

328 {
329  TableScanDesc scan = node->ss.ss_currentScanDesc;
330 
331  if (scan != NULL)
332  table_endscan(scan);
333 }
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1558
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1028

References TidRangeScanState::ss, ScanState::ss_currentScanDesc, and table_endscan().

Referenced by ExecEndNode().

◆ ExecInitTidRangeScan()

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

Definition at line 347 of file nodeTidrangescan.c.

348 {
349  TidRangeScanState *tidrangestate;
350  Relation currentRelation;
351 
352  /*
353  * create state structure
354  */
355  tidrangestate = makeNode(TidRangeScanState);
356  tidrangestate->ss.ps.plan = (Plan *) node;
357  tidrangestate->ss.ps.state = estate;
358  tidrangestate->ss.ps.ExecProcNode = ExecTidRangeScan;
359 
360  /*
361  * Miscellaneous initialization
362  *
363  * create expression context for node
364  */
365  ExecAssignExprContext(estate, &tidrangestate->ss.ps);
366 
367  /*
368  * mark scan as not in progress, and TID range as not computed yet
369  */
370  tidrangestate->trss_inScan = false;
371 
372  /*
373  * open the scan relation
374  */
375  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
376 
377  tidrangestate->ss.ss_currentRelation = currentRelation;
378  tidrangestate->ss.ss_currentScanDesc = NULL; /* no table scan here */
379 
380  /*
381  * get the scan type from the relation descriptor.
382  */
383  ExecInitScanTupleSlot(estate, &tidrangestate->ss,
384  RelationGetDescr(currentRelation),
385  table_slot_callbacks(currentRelation));
386 
387  /*
388  * Initialize result type and projection.
389  */
390  ExecInitResultTypeTL(&tidrangestate->ss.ps);
391  ExecAssignScanProjectionInfo(&tidrangestate->ss);
392 
393  /*
394  * initialize child expressions
395  */
396  tidrangestate->ss.ps.qual =
397  ExecInitQual(node->scan.plan.qual, (PlanState *) tidrangestate);
398 
399  TidExprListCreate(tidrangestate);
400 
401  /*
402  * all done.
403  */
404  return tidrangestate;
405 }
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:220
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:270
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1898
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1842
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:483
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:697
static void TidExprListCreate(TidRangeScanState *tidrangestate)
static TupleTableSlot * ExecTidRangeScan(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:155
#define RelationGetDescr(relation)
Definition: rel.h:533
ExprState * qual
Definition: execnodes.h:1137
Plan * plan
Definition: execnodes.h:1116
EState * state
Definition: execnodes.h:1118
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1122
Relation ss_currentRelation
Definition: execnodes.h:1557
PlanState ps
Definition: execnodes.h:1556
Index scanrelid
Definition: plannodes.h:387
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:58

References ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecTidRangeScan(), makeNode, PlanState::plan, ScanState::ps, PlanState::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 308 of file nodeTidrangescan.c.

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

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

Referenced by ExecReScan().

◆ ExecTidRangeScan()

static TupleTableSlot* ExecTidRangeScan ( PlanState pstate)
static

Definition at line 294 of file nodeTidrangescan.c.

295 {
297 
298  return ExecScan(&node->ss,
301 }
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:156
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:473
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:474
static TupleTableSlot * TidRangeNext(TidRangeScanState *node)
static bool TidRangeRecheck(TidRangeScanState *node, TupleTableSlot *slot)
#define castNode(_type_, nodeptr)
Definition: nodes.h:176

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

Referenced by ExecInitTidRangeScan().

◆ MakeTidOpExpr()

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

Definition at line 57 of file nodeTidrangescan.c.

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

107 {
108  TidRangeScan *node = (TidRangeScan *) tidrangestate->ss.ps.plan;
109  List *tidexprs = NIL;
110  ListCell *l;
111 
112  foreach(l, node->tidrangequals)
113  {
114  OpExpr *opexpr = lfirst(l);
115  TidOpExpr *tidopexpr;
116 
117  if (!IsA(opexpr, OpExpr))
118  elog(ERROR, "could not identify CTID expression");
119 
120  tidopexpr = MakeTidOpExpr(opexpr, tidrangestate);
121  tidexprs = lappend(tidexprs, tidopexpr);
122  }
123 
124  tidrangestate->trss_tidexprs = tidexprs;
125 }
List * lappend(List *list, void *datum)
Definition: list.c:339
static TidOpExpr * MakeTidOpExpr(OpExpr *expr, TidRangeScanState *tidstate)
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
List * trss_tidexprs
Definition: execnodes.h:1861
List * tidrangequals
Definition: plannodes.h:566

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 137 of file nodeTidrangescan.c.

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

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 220 of file nodeTidrangescan.c.

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

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 273 of file nodeTidrangescan.c.

274 {
275  return true;
276 }

Referenced by ExecTidRangeScan().