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 "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

CustomScanStateExecInitCustomScan (CustomScan *cscan, EState *estate, int eflags)
 
TupleTableSlotExecCustomScan (CustomScanState *node)
 
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 ExecCustomScanInitializeWorker (CustomScanState *node, shm_toc *toc)
 
void ExecShutdownCustomScan (CustomScanState *node)
 

Function Documentation

void ExecCustomMarkPos ( CustomScanState node)

Definition at line 141 of file nodeCustom.c.

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

Referenced by ExecMarkPos().

142 {
143  if (!node->methods->MarkPosCustomScan)
144  ereport(ERROR,
145  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
146  errmsg("custom scan \"%s\" does not support MarkPos",
147  node->methods->CustomName)));
148  node->methods->MarkPosCustomScan(node);
149 }
int errcode(int sqlerrcode)
Definition: elog.c:575
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
#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 152 of file nodeCustom.c.

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

Referenced by ExecRestrPos().

153 {
154  if (!node->methods->RestrPosCustomScan)
155  ereport(ERROR,
156  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
157  errmsg("custom scan \"%s\" does not support MarkPos",
158  node->methods->CustomName)));
159  node->methods->RestrPosCustomScan(node);
160 }
int errcode(int sqlerrcode)
Definition: elog.c:575
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
#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
TupleTableSlot* ExecCustomScan ( CustomScanState node)

Definition at line 109 of file nodeCustom.c.

References Assert, CustomExecMethods::ExecCustomScan, CustomScanState::methods, and NULL.

Referenced by ExecProcNode().

110 {
111  Assert(node->methods->ExecCustomScan != NULL);
112  return node->methods->ExecCustomScan(node);
113 }
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
TupleTableSlot *(* ExecCustomScan)(CustomScanState *node)
Definition: extensible.h:125
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void ExecCustomScanEstimate ( CustomScanState node,
ParallelContext pcxt 
)

Definition at line 163 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().

164 {
165  const CustomExecMethods *methods = node->methods;
166 
167  if (methods->EstimateDSMCustomScan)
168  {
169  node->pscan_len = methods->EstimateDSMCustomScan(node, pcxt);
171  shm_toc_estimate_keys(&pcxt->estimator, 1);
172  }
173 }
shm_toc_estimator estimator
Definition: parallel.h:42
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:49
Size(* EstimateDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt)
Definition: extensible.h:134
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:52
void ExecCustomScanInitializeDSM ( CustomScanState node,
ParallelContext pcxt 
)

Definition at line 176 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().

177 {
178  const CustomExecMethods *methods = node->methods;
179 
180  if (methods->InitializeDSMCustomScan)
181  {
182  int plan_node_id = node->ss.ps.plan->plan_node_id;
183  void *coordinate;
184 
185  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
186  methods->InitializeDSMCustomScan(node, pcxt, coordinate);
187  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
188  }
189 }
int plan_node_id
Definition: plannodes.h:131
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
PlanState ps
Definition: execnodes.h:1290
ScanState ss
Definition: execnodes.h:1739
Plan * plan
Definition: execnodes.h:1049
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
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:161
shm_toc * toc
Definition: parallel.h:45
void ExecCustomScanInitializeWorker ( CustomScanState node,
shm_toc toc 
)

Definition at line 192 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().

193 {
194  const CustomExecMethods *methods = node->methods;
195 
196  if (methods->InitializeWorkerCustomScan)
197  {
198  int plan_node_id = node->ss.ps.plan->plan_node_id;
199  void *coordinate;
200 
201  coordinate = shm_toc_lookup(toc, plan_node_id);
202  methods->InitializeWorkerCustomScan(node, toc, coordinate);
203  }
204 }
int plan_node_id
Definition: plannodes.h:131
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
PlanState ps
Definition: execnodes.h:1290
ScanState ss
Definition: execnodes.h:1739
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
Plan * plan
Definition: execnodes.h:1049
void(* InitializeWorkerCustomScan)(CustomScanState *node, shm_toc *toc, void *coordinate)
Definition: extensible.h:139
void ExecEndCustomScan ( CustomScanState node)

Definition at line 116 of file nodeCustom.c.

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

Referenced by ExecEndNode().

117 {
118  Assert(node->methods->EndCustomScan != NULL);
119  node->methods->EndCustomScan(node);
120 
121  /* Free the exprcontext */
122  ExecFreeExprContext(&node->ss.ps);
123 
124  /* Clean out the tuple table */
127 
128  /* Close the heap relation */
129  if (node->ss.ss_currentRelation)
131 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1293
Relation ss_currentRelation
Definition: execnodes.h:1291
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:686
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
PlanState ps
Definition: execnodes.h:1290
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1079
ScanState ss
Definition: execnodes.h:1739
void(* EndCustomScan)(CustomScanState *node)
Definition: extensible.h:126
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:831
CustomScanState* ExecInitCustomScan ( CustomScan cscan,
EState estate,
int  eflags 
)

Definition at line 24 of file nodeCustom.c.

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

Referenced by ExecInitNode().

25 {
26  CustomScanState *css;
27  Relation scan_rel = NULL;
28  Index scanrelid = cscan->scan.scanrelid;
29  Index tlistvarno;
30 
31  /*
32  * Allocate the CustomScanState object. We let the custom scan provider
33  * do the palloc, in case it wants to make a larger object that embeds
34  * CustomScanState as the first field. It must set the node tag and the
35  * methods field correctly at this time. Other standard fields should be
36  * set to zero.
37  */
39  cscan->methods->CreateCustomScanState(cscan));
40 
41  /* ensure flags is filled correctly */
42  css->flags = cscan->flags;
43 
44  /* fill up fields of ScanState */
45  css->ss.ps.plan = &cscan->scan.plan;
46  css->ss.ps.state = estate;
47 
48  /* create expression context for node */
49  ExecAssignExprContext(estate, &css->ss.ps);
50 
51  /* initialize child expressions */
52  css->ss.ps.targetlist = (List *)
54  (PlanState *) css);
55  css->ss.ps.qual = (List *)
56  ExecInitExpr((Expr *) cscan->scan.plan.qual,
57  (PlanState *) css);
58 
59  /* tuple table initialization */
60  ExecInitScanTupleSlot(estate, &css->ss);
61  ExecInitResultTupleSlot(estate, &css->ss.ps);
62 
63  /*
64  * open the base relation, if any, and acquire an appropriate lock on it
65  */
66  if (scanrelid > 0)
67  {
68  scan_rel = ExecOpenScanRelation(estate, scanrelid, eflags);
69  css->ss.ss_currentRelation = scan_rel;
70  }
71 
72  /*
73  * Determine the scan tuple type. If the custom scan provider provided a
74  * targetlist describing the scan tuples, use that; else use base
75  * relation's rowtype.
76  */
77  if (cscan->custom_scan_tlist != NIL || scan_rel == NULL)
78  {
79  TupleDesc scan_tupdesc;
80 
81  scan_tupdesc = ExecTypeFromTL(cscan->custom_scan_tlist, false);
82  ExecAssignScanType(&css->ss, scan_tupdesc);
83  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
84  tlistvarno = INDEX_VAR;
85  }
86  else
87  {
88  ExecAssignScanType(&css->ss, RelationGetDescr(scan_rel));
89  /* Node's targetlist will contain Vars with varno = scanrelid */
90  tlistvarno = scanrelid;
91  }
92 
93  /*
94  * Initialize result tuple type and projection info.
95  */
97  ExecAssignScanProjectionInfoWithVarno(&css->ss, tlistvarno);
98 
99  /*
100  * The callback of custom-scan provider applies the final initialization
101  * of the custom-scan-state node according to its logic.
102  */
103  css->methods->BeginCustomScan(css, estate, eflags);
104 
105  return css;
106 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:133
Plan plan
Definition: plannodes.h:315
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
Index scanrelid
Definition: plannodes.h:316
#define RelationGetDescr(relation)
Definition: rel.h:425
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
List * qual
Definition: execnodes.h:1064
Node *(* CreateCustomScanState)(CustomScan *cscan)
Definition: extensible.h:110
List * targetlist
Definition: execnodes.h:1063
Relation ss_currentRelation
Definition: execnodes.h:1291
EState * state
Definition: execnodes.h:1051
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:431
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:247
PlanState ps
Definition: execnodes.h:1290
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4267
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:773
ScanState ss
Definition: execnodes.h:1739
void(* BeginCustomScan)(CustomScanState *node, EState *estate, int eflags)
Definition: extensible.h:122
const struct CustomScanMethods * methods
Definition: plannodes.h:612
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
Scan scan
Definition: plannodes.h:603
unsigned int Index
Definition: c.h:365
Plan * plan
Definition: execnodes.h:1049
#define NULL
Definition: c.h:229
List * custom_scan_tlist
Definition: plannodes.h:609
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:409
List * targetlist
Definition: plannodes.h:132
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:710
#define INDEX_VAR
Definition: primnodes.h:155
Definition: pg_list.h:45
uint32 flags
Definition: plannodes.h:604
void ExecReScanCustomScan ( CustomScanState node)

Definition at line 134 of file nodeCustom.c.

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

Referenced by ExecReScan().

135 {
136  Assert(node->methods->ReScanCustomScan != NULL);
137  node->methods->ReScanCustomScan(node);
138 }
void(* ReScanCustomScan)(CustomScanState *node)
Definition: extensible.h:127
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void ExecShutdownCustomScan ( CustomScanState node)

Definition at line 207 of file nodeCustom.c.

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

Referenced by ExecShutdownNode().

208 {
209  const CustomExecMethods *methods = node->methods;
210 
211  if (methods->ShutdownCustomScan)
212  methods->ShutdownCustomScan(node);
213 }
void(* ShutdownCustomScan)(CustomScanState *node)
Definition: extensible.h:142
const struct CustomExecMethods * methods
Definition: execnodes.h:1744