PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeCustom.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "executor/executor.h"
#include "executor/nodeCustom.h"
#include "nodes/execnodes.h"
#include "nodes/plannodes.h"
#include "miscadmin.h"
#include "parser/parsetree.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for nodeCustom.c:

Go to the source code of this file.

Functions

static TupleTableSlotExecCustomScan (PlanState *pstate)
 
CustomScanStateExecInitCustomScan (CustomScan *cscan, EState *estate, int eflags)
 
void ExecEndCustomScan (CustomScanState *node)
 
void ExecReScanCustomScan (CustomScanState *node)
 
void ExecCustomMarkPos (CustomScanState *node)
 
void ExecCustomRestrPos (CustomScanState *node)
 
void ExecCustomScanEstimate (CustomScanState *node, ParallelContext *pcxt)
 
void ExecCustomScanInitializeDSM (CustomScanState *node, ParallelContext *pcxt)
 
void ExecCustomScanReInitializeDSM (CustomScanState *node, ParallelContext *pcxt)
 
void ExecCustomScanInitializeWorker (CustomScanState *node, shm_toc *toc)
 
void ExecShutdownCustomScan (CustomScanState *node)
 

Function Documentation

void ExecCustomMarkPos ( CustomScanState node)

Definition at line 147 of file nodeCustom.c.

References CustomExecMethods::CustomName, ereport, errcode(), errmsg(), ERROR, CustomExecMethods::MarkPosCustomScan, and CustomScanState::methods.

Referenced by ExecMarkPos().

148 {
149  if (!node->methods->MarkPosCustomScan)
150  ereport(ERROR,
151  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
152  errmsg("custom scan \"%s\" does not support MarkPos",
153  node->methods->CustomName)));
154  node->methods->MarkPosCustomScan(node);
155 }
int errcode(int sqlerrcode)
Definition: elog.c:575
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
#define ERROR
Definition: elog.h:43
void(* MarkPosCustomScan)(CustomScanState *node)
Definition: extensible.h:130
#define ereport(elevel, rest)
Definition: elog.h:122
const char * CustomName
Definition: extensible.h:119
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ExecCustomRestrPos ( CustomScanState node)

Definition at line 158 of file nodeCustom.c.

References CustomExecMethods::CustomName, ereport, errcode(), errmsg(), ERROR, CustomScanState::methods, and CustomExecMethods::RestrPosCustomScan.

Referenced by ExecRestrPos().

159 {
160  if (!node->methods->RestrPosCustomScan)
161  ereport(ERROR,
162  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
163  errmsg("custom scan \"%s\" does not support MarkPos",
164  node->methods->CustomName)));
165  node->methods->RestrPosCustomScan(node);
166 }
int errcode(int sqlerrcode)
Definition: elog.c:575
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
const char * CustomName
Definition: extensible.h:119
void(* RestrPosCustomScan)(CustomScanState *node)
Definition: extensible.h:131
int errmsg(const char *fmt,...)
Definition: elog.c:797
static TupleTableSlot * ExecCustomScan ( PlanState pstate)
static

Definition at line 111 of file nodeCustom.c.

References Assert, castNode, CHECK_FOR_INTERRUPTS, CustomExecMethods::ExecCustomScan, and CustomScanState::methods.

Referenced by ExecInitCustomScan().

112 {
113  CustomScanState *node = castNode(CustomScanState, pstate);
114 
116 
117  Assert(node->methods->ExecCustomScan != NULL);
118  return node->methods->ExecCustomScan(node);
119 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
TupleTableSlot *(* ExecCustomScan)(CustomScanState *node)
Definition: extensible.h:125
#define Assert(condition)
Definition: c.h:664
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void ExecCustomScanEstimate ( CustomScanState node,
ParallelContext pcxt 
)

Definition at line 169 of file nodeCustom.c.

References CustomExecMethods::EstimateDSMCustomScan, ParallelContext::estimator, CustomScanState::methods, CustomScanState::pscan_len, shm_toc_estimate_chunk, and shm_toc_estimate_keys.

Referenced by ExecParallelEstimate().

170 {
171  const CustomExecMethods *methods = node->methods;
172 
173  if (methods->EstimateDSMCustomScan)
174  {
175  node->pscan_len = methods->EstimateDSMCustomScan(node, pcxt);
177  shm_toc_estimate_keys(&pcxt->estimator, 1);
178  }
179 }
shm_toc_estimator estimator
Definition: parallel.h:41
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size(* EstimateDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt)
Definition: extensible.h:134
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
void ExecCustomScanInitializeDSM ( CustomScanState node,
ParallelContext pcxt 
)

Definition at line 182 of file nodeCustom.c.

References CustomExecMethods::InitializeDSMCustomScan, CustomScanState::methods, PlanState::plan, Plan::plan_node_id, ScanState::ps, CustomScanState::pscan_len, shm_toc_allocate(), shm_toc_insert(), CustomScanState::ss, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

183 {
184  const CustomExecMethods *methods = node->methods;
185 
186  if (methods->InitializeDSMCustomScan)
187  {
188  int plan_node_id = node->ss.ps.plan->plan_node_id;
189  void *coordinate;
190 
191  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
192  methods->InitializeDSMCustomScan(node, pcxt, coordinate);
193  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
194  }
195 }
int plan_node_id
Definition: plannodes.h:143
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
PlanState ps
Definition: execnodes.h:1100
ScanState ss
Definition: execnodes.h:1568
Plan * plan
Definition: execnodes.h:847
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void(* InitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:136
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
shm_toc * toc
Definition: parallel.h:44
void ExecCustomScanInitializeWorker ( CustomScanState node,
shm_toc toc 
)

Definition at line 213 of file nodeCustom.c.

References CustomExecMethods::InitializeWorkerCustomScan, CustomScanState::methods, PlanState::plan, Plan::plan_node_id, ScanState::ps, shm_toc_lookup(), and CustomScanState::ss.

Referenced by ExecParallelInitializeWorker().

214 {
215  const CustomExecMethods *methods = node->methods;
216 
217  if (methods->InitializeWorkerCustomScan)
218  {
219  int plan_node_id = node->ss.ps.plan->plan_node_id;
220  void *coordinate;
221 
222  coordinate = shm_toc_lookup(toc, plan_node_id, false);
223  methods->InitializeWorkerCustomScan(node, toc, coordinate);
224  }
225 }
int plan_node_id
Definition: plannodes.h:143
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
PlanState ps
Definition: execnodes.h:1100
ScanState ss
Definition: execnodes.h:1568
Plan * plan
Definition: execnodes.h:847
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
void(* InitializeWorkerCustomScan)(CustomScanState *node, shm_toc *toc, void *coordinate)
Definition: extensible.h:142
void ExecCustomScanReInitializeDSM ( CustomScanState node,
ParallelContext pcxt 
)

Definition at line 198 of file nodeCustom.c.

References CustomScanState::methods, PlanState::plan, Plan::plan_node_id, ScanState::ps, CustomExecMethods::ReInitializeDSMCustomScan, shm_toc_lookup(), CustomScanState::ss, and ParallelContext::toc.

Referenced by ExecParallelReInitializeDSM().

199 {
200  const CustomExecMethods *methods = node->methods;
201 
202  if (methods->ReInitializeDSMCustomScan)
203  {
204  int plan_node_id = node->ss.ps.plan->plan_node_id;
205  void *coordinate;
206 
207  coordinate = shm_toc_lookup(pcxt->toc, plan_node_id, false);
208  methods->ReInitializeDSMCustomScan(node, pcxt, coordinate);
209  }
210 }
int plan_node_id
Definition: plannodes.h:143
void(* ReInitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:139
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
PlanState ps
Definition: execnodes.h:1100
ScanState ss
Definition: execnodes.h:1568
Plan * plan
Definition: execnodes.h:847
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44
void ExecEndCustomScan ( CustomScanState node)

Definition at line 122 of file nodeCustom.c.

References Assert, CustomExecMethods::EndCustomScan, ExecClearTuple(), ExecCloseScanRelation(), ExecFreeExprContext(), CustomScanState::methods, ScanState::ps, PlanState::ps_ResultTupleSlot, CustomScanState::ss, ScanState::ss_currentRelation, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

123 {
124  Assert(node->methods->EndCustomScan != NULL);
125  node->methods->EndCustomScan(node);
126 
127  /* Free the exprcontext */
128  ExecFreeExprContext(&node->ss.ps);
129 
130  /* Clean out the tuple table */
133 
134  /* Close the heap relation */
135  if (node->ss.ss_currentRelation)
137 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1103
Relation ss_currentRelation
Definition: execnodes.h:1101
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:521
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
PlanState ps
Definition: execnodes.h:1100
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
ScanState ss
Definition: execnodes.h:1568
void(* EndCustomScan)(CustomScanState *node)
Definition: extensible.h:126
#define Assert(condition)
Definition: c.h:664
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:666
CustomScanState* ExecInitCustomScan ( CustomScan cscan,
EState estate,
int  eflags 
)

Definition at line 29 of file nodeCustom.c.

References CustomExecMethods::BeginCustomScan, castNode, CustomScanMethods::CreateCustomScanState, CustomScan::custom_scan_tlist, ExecAssignExprContext(), ExecAssignResultTypeFromTL(), ExecAssignScanProjectionInfoWithVarno(), ExecAssignScanType(), ExecCustomScan(), ExecInitQual(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecTypeFromTL(), CustomScan::flags, CustomScanState::flags, INDEX_VAR, CustomScan::methods, CustomScanState::methods, NIL, Scan::plan, PlanState::plan, ScanState::ps, Plan::qual, PlanState::qual, RelationGetDescr, CustomScan::scan, Scan::scanrelid, CustomScanState::ss, ScanState::ss_currentRelation, and PlanState::state.

Referenced by ExecInitNode().

30 {
31  CustomScanState *css;
32  Relation scan_rel = NULL;
33  Index scanrelid = cscan->scan.scanrelid;
34  Index tlistvarno;
35 
36  /*
37  * Allocate the CustomScanState object. We let the custom scan provider
38  * do the palloc, in case it wants to make a larger object that embeds
39  * CustomScanState as the first field. It must set the node tag and the
40  * methods field correctly at this time. Other standard fields should be
41  * set to zero.
42  */
44  cscan->methods->CreateCustomScanState(cscan));
45 
46  /* ensure flags is filled correctly */
47  css->flags = cscan->flags;
48 
49  /* fill up fields of ScanState */
50  css->ss.ps.plan = &cscan->scan.plan;
51  css->ss.ps.state = estate;
53 
54  /* create expression context for node */
55  ExecAssignExprContext(estate, &css->ss.ps);
56 
57  /* initialize child expressions */
58  css->ss.ps.qual =
59  ExecInitQual(cscan->scan.plan.qual, (PlanState *) css);
60 
61  /* tuple table initialization */
62  ExecInitScanTupleSlot(estate, &css->ss);
63  ExecInitResultTupleSlot(estate, &css->ss.ps);
64 
65  /*
66  * open the base relation, if any, and acquire an appropriate lock on it
67  */
68  if (scanrelid > 0)
69  {
70  scan_rel = ExecOpenScanRelation(estate, scanrelid, eflags);
71  css->ss.ss_currentRelation = scan_rel;
72  }
73 
74  /*
75  * Determine the scan tuple type. If the custom scan provider provided a
76  * targetlist describing the scan tuples, use that; else use base
77  * relation's rowtype.
78  */
79  if (cscan->custom_scan_tlist != NIL || scan_rel == NULL)
80  {
81  TupleDesc scan_tupdesc;
82 
83  scan_tupdesc = ExecTypeFromTL(cscan->custom_scan_tlist, false);
84  ExecAssignScanType(&css->ss, scan_tupdesc);
85  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
86  tlistvarno = INDEX_VAR;
87  }
88  else
89  {
90  ExecAssignScanType(&css->ss, RelationGetDescr(scan_rel));
91  /* Node's targetlist will contain Vars with varno = scanrelid */
92  tlistvarno = scanrelid;
93  }
94 
95  /*
96  * Initialize result tuple type and projection info.
97  */
99  ExecAssignScanProjectionInfoWithVarno(&css->ss, tlistvarno);
100 
101  /*
102  * The callback of custom-scan provider applies the final initialization
103  * of the custom-scan-state node according to its logic.
104  */
105  css->methods->BeginCustomScan(css, estate, eflags);
106 
107  return css;
108 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
Index scanrelid
Definition: plannodes.h:329
static TupleTableSlot * ExecCustomScan(PlanState *pstate)
Definition: nodeCustom.c:111
#define RelationGetDescr(relation)
Definition: rel.h:428
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
Node *(* CreateCustomScanState)(CustomScan *cscan)
Definition: extensible.h:110
Relation ss_currentRelation
Definition: execnodes.h:1101
EState * state
Definition: execnodes.h:849
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:445
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:249
PlanState ps
Definition: execnodes.h:1100
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:608
ScanState ss
Definition: execnodes.h:1568
void(* BeginCustomScan)(CustomScanState *node, EState *estate, int eflags)
Definition: extensible.h:122
const struct CustomScanMethods * methods
Definition: plannodes.h:633
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
Scan scan
Definition: plannodes.h:625
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
unsigned int Index
Definition: c.h:359
Plan * plan
Definition: execnodes.h:847
List * custom_scan_tlist
Definition: plannodes.h:631
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:423
ExprState * qual
Definition: execnodes.h:865
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:545
#define INDEX_VAR
Definition: primnodes.h:155
uint32 flags
Definition: plannodes.h:626
void ExecReScanCustomScan ( CustomScanState node)

Definition at line 140 of file nodeCustom.c.

References Assert, CustomScanState::methods, and CustomExecMethods::ReScanCustomScan.

Referenced by ExecReScan().

141 {
142  Assert(node->methods->ReScanCustomScan != NULL);
143  node->methods->ReScanCustomScan(node);
144 }
void(* ReScanCustomScan)(CustomScanState *node)
Definition: extensible.h:127
const struct CustomExecMethods * methods
Definition: execnodes.h:1573
#define Assert(condition)
Definition: c.h:664
void ExecShutdownCustomScan ( CustomScanState node)

Definition at line 228 of file nodeCustom.c.

References CustomScanState::methods, and CustomExecMethods::ShutdownCustomScan.

Referenced by ExecShutdownNode().

229 {
230  const CustomExecMethods *methods = node->methods;
231 
232  if (methods->ShutdownCustomScan)
233  methods->ShutdownCustomScan(node);
234 }
void(* ShutdownCustomScan)(CustomScanState *node)
Definition: extensible.h:145
const struct CustomExecMethods * methods
Definition: execnodes.h:1573