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)
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
struct Var Var
Definition: primnodes.h:226
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21

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

40 {
43 } TidExprType;
TidExprType
@ TIDEXPR_LOWER_BOUND
@ TIDEXPR_UPPER_BOUND

Function Documentation

◆ ExecEndTidRangeScan()

void ExecEndTidRangeScan ( TidRangeScanState node)

Definition at line 328 of file nodeTidrangescan.c.

329 {
330  TableScanDesc scan = node->ss.ss_currentScanDesc;
331 
332  if (scan != NULL)
333  table_endscan(scan);
334 
335  /*
336  * Free the exprcontext
337  */
338  ExecFreeExprContext(&node->ss.ps);
339 
340  /*
341  * clear out tuple table slots
342  */
343  if (node->ss.ps.ps_ResultTupleSlot)
346 }
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:658
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1075
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1477
PlanState ps
Definition: execnodes.h:1474
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1476
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1009
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:432

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

361 {
362  TidRangeScanState *tidrangestate;
363  Relation currentRelation;
364 
365  /*
366  * create state structure
367  */
368  tidrangestate = makeNode(TidRangeScanState);
369  tidrangestate->ss.ps.plan = (Plan *) node;
370  tidrangestate->ss.ps.state = estate;
371  tidrangestate->ss.ps.ExecProcNode = ExecTidRangeScan;
372 
373  /*
374  * Miscellaneous initialization
375  *
376  * create expression context for node
377  */
378  ExecAssignExprContext(estate, &tidrangestate->ss.ps);
379 
380  /*
381  * mark scan as not in progress, and TID range as not computed yet
382  */
383  tidrangestate->trss_inScan = false;
384 
385  /*
386  * open the scan relation
387  */
388  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
389 
390  tidrangestate->ss.ss_currentRelation = currentRelation;
391  tidrangestate->ss.ss_currentScanDesc = NULL; /* no table scan here */
392 
393  /*
394  * get the scan type from the relation descriptor.
395  */
396  ExecInitScanTupleSlot(estate, &tidrangestate->ss,
397  RelationGetDescr(currentRelation),
398  table_slot_callbacks(currentRelation));
399 
400  /*
401  * Initialize result type and projection.
402  */
403  ExecInitResultTypeTL(&tidrangestate->ss.ps);
404  ExecAssignScanProjectionInfo(&tidrangestate->ss);
405 
406  /*
407  * initialize child expressions
408  */
409  tidrangestate->ss.ps.qual =
410  ExecInitQual(node->scan.plan.qual, (PlanState *) tidrangestate);
411 
412  TidExprListCreate(tidrangestate);
413 
414  /*
415  * all done.
416  */
417  return tidrangestate;
418 }
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:214
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:271
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1812
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1756
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:488
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:728
static void TidExprListCreate(TidRangeScanState *tidrangestate)
static TupleTableSlot * ExecTidRangeScan(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:176
#define RelationGetDescr(relation)
Definition: rel.h:530
ExprState * qual
Definition: execnodes.h:1058
Plan * plan
Definition: execnodes.h:1037
EState * state
Definition: execnodes.h:1039
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1043
Relation ss_currentRelation
Definition: execnodes.h:1475
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 309 of file nodeTidrangescan.c.

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

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

Referenced by ExecReScan().

◆ ExecTidRangeScan()

static TupleTableSlot* ExecTidRangeScan ( PlanState pstate)
static

Definition at line 295 of file nodeTidrangescan.c.

296 {
298 
299  return ExecScan(&node->ss,
302 }
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:157
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:472
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:473
static TupleTableSlot * TidRangeNext(TidRangeScanState *node)
static bool TidRangeRecheck(TidRangeScanState *node, TupleTableSlot *slot)
#define castNode(_type_, nodeptr)
Definition: nodes.h:197

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

Referenced by ExecInitTidRangeScan().

◆ MakeTidOpExpr()

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

Definition at line 58 of file nodeTidrangescan.c.

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

108 {
109  TidRangeScan *node = (TidRangeScan *) tidrangestate->ss.ps.plan;
110  List *tidexprs = NIL;
111  ListCell *l;
112 
113  foreach(l, node->tidrangequals)
114  {
115  OpExpr *opexpr = lfirst(l);
116  TidOpExpr *tidopexpr;
117 
118  if (!IsA(opexpr, OpExpr))
119  elog(ERROR, "could not identify CTID expression");
120 
121  tidopexpr = MakeTidOpExpr(opexpr, tidrangestate);
122  tidexprs = lappend(tidexprs, tidopexpr);
123  }
124 
125  tidrangestate->trss_tidexprs = tidexprs;
126 }
List * lappend(List *list, void *datum)
Definition: list.c:338
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:1783
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 138 of file nodeTidrangescan.c.

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

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

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

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

275 {
276  return true;
277 }

Referenced by ExecTidRangeScan().