1 /*-------------------------------------------------------------------------
2  *
3  * fdwapi.h
4  *	  API for foreign-data wrappers
5  *
6  * Copyright (c) 2010-2019, PostgreSQL Global Development Group
7  *
8  * src/include/foreign/fdwapi.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef FDWAPI_H
13 #define FDWAPI_H
14 
15 #include "access/parallel.h"
16 #include "nodes/execnodes.h"
17 #include "nodes/pathnodes.h"
18 
19 /* To avoid including explain.h here, reference ExplainState thus: */
20 struct ExplainState;
21 
22 
23 /*
24  * Callback function signatures --- see fdwhandler.sgml for more info.
25  */
26 
27 typedef void (*GetForeignRelSize_function) (PlannerInfo *root,
28 											RelOptInfo *baserel,
29 											Oid foreigntableid);
30 
31 typedef void (*GetForeignPaths_function) (PlannerInfo *root,
32 										  RelOptInfo *baserel,
33 										  Oid foreigntableid);
34 
35 typedef ForeignScan *(*GetForeignPlan_function) (PlannerInfo *root,
36 												 RelOptInfo *baserel,
37 												 Oid foreigntableid,
38 												 ForeignPath *best_path,
39 												 List *tlist,
40 												 List *scan_clauses,
41 												 Plan *outer_plan);
42 
43 typedef void (*BeginForeignScan_function) (ForeignScanState *node,
44 										   int eflags);
45 
46 typedef TupleTableSlot *(*IterateForeignScan_function) (ForeignScanState *node);
47 
48 typedef bool (*RecheckForeignScan_function) (ForeignScanState *node,
49 											 TupleTableSlot *slot);
50 
51 typedef void (*ReScanForeignScan_function) (ForeignScanState *node);
52 
53 typedef void (*EndForeignScan_function) (ForeignScanState *node);
54 
55 typedef void (*GetForeignJoinPaths_function) (PlannerInfo *root,
56 											  RelOptInfo *joinrel,
57 											  RelOptInfo *outerrel,
58 											  RelOptInfo *innerrel,
59 											  JoinType jointype,
60 											  JoinPathExtraData *extra);
61 
62 typedef void (*GetForeignUpperPaths_function) (PlannerInfo *root,
63 											   UpperRelationKind stage,
64 											   RelOptInfo *input_rel,
65 											   RelOptInfo *output_rel,
66 											   void *extra);
67 
68 typedef void (*AddForeignUpdateTargets_function) (Query *parsetree,
69 												  RangeTblEntry *target_rte,
70 												  Relation target_relation);
71 
72 typedef List *(*PlanForeignModify_function) (PlannerInfo *root,
73 											 ModifyTable *plan,
74 											 Index resultRelation,
75 											 int subplan_index);
76 
77 typedef void (*BeginForeignModify_function) (ModifyTableState *mtstate,
78 											 ResultRelInfo *rinfo,
79 											 List *fdw_private,
80 											 int subplan_index,
81 											 int eflags);
82 
83 typedef TupleTableSlot *(*ExecForeignInsert_function) (EState *estate,
84 													   ResultRelInfo *rinfo,
85 													   TupleTableSlot *slot,
86 													   TupleTableSlot *planSlot);
87 
88 typedef TupleTableSlot *(*ExecForeignUpdate_function) (EState *estate,
89 													   ResultRelInfo *rinfo,
90 													   TupleTableSlot *slot,
91 													   TupleTableSlot *planSlot);
92 
93 typedef TupleTableSlot *(*ExecForeignDelete_function) (EState *estate,
94 													   ResultRelInfo *rinfo,
95 													   TupleTableSlot *slot,
96 													   TupleTableSlot *planSlot);
97 
98 typedef void (*EndForeignModify_function) (EState *estate,
99 										   ResultRelInfo *rinfo);
100 
101 typedef void (*BeginForeignInsert_function) (ModifyTableState *mtstate,
102 											 ResultRelInfo *rinfo);
103 
104 typedef void (*EndForeignInsert_function) (EState *estate,
105 										   ResultRelInfo *rinfo);
106 
107 typedef int (*IsForeignRelUpdatable_function) (Relation rel);
108 
109 typedef bool (*PlanDirectModify_function) (PlannerInfo *root,
110 										   ModifyTable *plan,
111 										   Index resultRelation,
112 										   int subplan_index);
113 
114 typedef void (*BeginDirectModify_function) (ForeignScanState *node,
115 											int eflags);
116 
117 typedef TupleTableSlot *(*IterateDirectModify_function) (ForeignScanState *node);
118 
119 typedef void (*EndDirectModify_function) (ForeignScanState *node);
120 
121 typedef RowMarkType (*GetForeignRowMarkType_function) (RangeTblEntry *rte,
122 													   LockClauseStrength strength);
123 
124 typedef void (*RefetchForeignRow_function) (EState *estate,
125 											ExecRowMark *erm,
126 											Datum rowid,
127 											TupleTableSlot *slot,
128 											bool *updated);
129 
130 typedef void (*ExplainForeignScan_function) (ForeignScanState *node,
131 											 struct ExplainState *es);
132 
133 typedef void (*ExplainForeignModify_function) (ModifyTableState *mtstate,
134 											   ResultRelInfo *rinfo,
135 											   List *fdw_private,
136 											   int subplan_index,
137 											   struct ExplainState *es);
138 
139 typedef void (*ExplainDirectModify_function) (ForeignScanState *node,
140 											  struct ExplainState *es);
141 
142 typedef int (*AcquireSampleRowsFunc) (Relation relation, int elevel,
143 									  HeapTuple *rows, int targrows,
144 									  double *totalrows,
145 									  double *totaldeadrows);
146 
147 typedef bool (*AnalyzeForeignTable_function) (Relation relation,
148 											  AcquireSampleRowsFunc *func,
149 											  BlockNumber *totalpages);
150 
151 typedef List *(*ImportForeignSchema_function) (ImportForeignSchemaStmt *stmt,
152 											   Oid serverOid);
153 
154 typedef Size (*EstimateDSMForeignScan_function) (ForeignScanState *node,
155 												 ParallelContext *pcxt);
156 typedef void (*InitializeDSMForeignScan_function) (ForeignScanState *node,
157 												   ParallelContext *pcxt,
158 												   void *coordinate);
159 typedef void (*ReInitializeDSMForeignScan_function) (ForeignScanState *node,
160 													 ParallelContext *pcxt,
161 													 void *coordinate);
162 typedef void (*InitializeWorkerForeignScan_function) (ForeignScanState *node,
163 													  shm_toc *toc,
164 													  void *coordinate);
165 typedef void (*ShutdownForeignScan_function) (ForeignScanState *node);
166 typedef bool (*IsForeignScanParallelSafe_function) (PlannerInfo *root,
167 													RelOptInfo *rel,
168 													RangeTblEntry *rte);
169 typedef List *(*ReparameterizeForeignPathByChild_function) (PlannerInfo *root,
170 															List *fdw_private,
171 															RelOptInfo *child_rel);
172 
173 /*
174  * FdwRoutine is the struct returned by a foreign-data wrapper's handler
175  * function.  It provides pointers to the callback functions needed by the
176  * planner and executor.
177  *
178  * More function pointers are likely to be added in the future.  Therefore
179  * it's recommended that the handler initialize the struct with
180  * makeNode(FdwRoutine) so that all fields are set to NULL.  This will
181  * ensure that no fields are accidentally left undefined.
182  */
183 typedef struct FdwRoutine
184 {
185 	NodeTag		type;
186 
187 	/* Functions for scanning foreign tables */
188 	GetForeignRelSize_function GetForeignRelSize;
189 	GetForeignPaths_function GetForeignPaths;
190 	GetForeignPlan_function GetForeignPlan;
191 	BeginForeignScan_function BeginForeignScan;
192 	IterateForeignScan_function IterateForeignScan;
193 	ReScanForeignScan_function ReScanForeignScan;
194 	EndForeignScan_function EndForeignScan;
195 
196 	/*
197 	 * Remaining functions are optional.  Set the pointer to NULL for any that
198 	 * are not provided.
199 	 */
200 
201 	/* Functions for remote-join planning */
202 	GetForeignJoinPaths_function GetForeignJoinPaths;
203 
204 	/* Functions for remote upper-relation (post scan/join) planning */
205 	GetForeignUpperPaths_function GetForeignUpperPaths;
206 
207 	/* Functions for updating foreign tables */
208 	AddForeignUpdateTargets_function AddForeignUpdateTargets;
209 	PlanForeignModify_function PlanForeignModify;
210 	BeginForeignModify_function BeginForeignModify;
211 	ExecForeignInsert_function ExecForeignInsert;
212 	ExecForeignUpdate_function ExecForeignUpdate;
213 	ExecForeignDelete_function ExecForeignDelete;
214 	EndForeignModify_function EndForeignModify;
215 	BeginForeignInsert_function BeginForeignInsert;
216 	EndForeignInsert_function EndForeignInsert;
217 	IsForeignRelUpdatable_function IsForeignRelUpdatable;
218 	PlanDirectModify_function PlanDirectModify;
219 	BeginDirectModify_function BeginDirectModify;
220 	IterateDirectModify_function IterateDirectModify;
221 	EndDirectModify_function EndDirectModify;
222 
223 	/* Functions for SELECT FOR UPDATE/SHARE row locking */
224 	GetForeignRowMarkType_function GetForeignRowMarkType;
225 	RefetchForeignRow_function RefetchForeignRow;
226 	RecheckForeignScan_function RecheckForeignScan;
227 
228 	/* Support functions for EXPLAIN */
229 	ExplainForeignScan_function ExplainForeignScan;
230 	ExplainForeignModify_function ExplainForeignModify;
231 	ExplainDirectModify_function ExplainDirectModify;
232 
233 	/* Support functions for ANALYZE */
234 	AnalyzeForeignTable_function AnalyzeForeignTable;
235 
236 	/* Support functions for IMPORT FOREIGN SCHEMA */
237 	ImportForeignSchema_function ImportForeignSchema;
238 
239 	/* Support functions for parallelism under Gather node */
240 	IsForeignScanParallelSafe_function IsForeignScanParallelSafe;
241 	EstimateDSMForeignScan_function EstimateDSMForeignScan;
242 	InitializeDSMForeignScan_function InitializeDSMForeignScan;
243 	ReInitializeDSMForeignScan_function ReInitializeDSMForeignScan;
244 	InitializeWorkerForeignScan_function InitializeWorkerForeignScan;
245 	ShutdownForeignScan_function ShutdownForeignScan;
246 
247 	/* Support functions for path reparameterization. */
248 	ReparameterizeForeignPathByChild_function ReparameterizeForeignPathByChild;
249 } FdwRoutine;
250 
251 
252 /* Functions in foreign/foreign.c */
253 extern FdwRoutine *GetFdwRoutine(Oid fdwhandler);
254 extern Oid	GetForeignServerIdByRelId(Oid relid);
255 extern FdwRoutine *GetFdwRoutineByServerId(Oid serverid);
256 extern FdwRoutine *GetFdwRoutineByRelId(Oid relid);
257 extern FdwRoutine *GetFdwRoutineForRelation(Relation relation, bool makecopy);
258 extern bool IsImportableForeignTable(const char *tablename,
259 									 ImportForeignSchemaStmt *stmt);
260 extern Path *GetExistingLocalJoinPath(RelOptInfo *joinrel);
261 
262 #endif							/* FDWAPI_H */
263