1 /*-------------------------------------------------------------------------
2  *
3  * fdwapi.h
4  *	  API for foreign-data wrappers
5  *
6  * Copyright (c) 2010-2021, 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) (PlannerInfo *root,
69 												  Index rtindex,
70 												  RangeTblEntry *target_rte,
71 												  Relation target_relation);
72 
73 typedef List *(*PlanForeignModify_function) (PlannerInfo *root,
74 											 ModifyTable *plan,
75 											 Index resultRelation,
76 											 int subplan_index);
77 
78 typedef void (*BeginForeignModify_function) (ModifyTableState *mtstate,
79 											 ResultRelInfo *rinfo,
80 											 List *fdw_private,
81 											 int subplan_index,
82 											 int eflags);
83 
84 typedef TupleTableSlot *(*ExecForeignInsert_function) (EState *estate,
85 													   ResultRelInfo *rinfo,
86 													   TupleTableSlot *slot,
87 													   TupleTableSlot *planSlot);
88 
89 typedef TupleTableSlot **(*ExecForeignBatchInsert_function) (EState *estate,
90 															 ResultRelInfo *rinfo,
91 															 TupleTableSlot **slots,
92 															 TupleTableSlot **planSlots,
93 															 int *numSlots);
94 
95 typedef int (*GetForeignModifyBatchSize_function) (ResultRelInfo *rinfo);
96 
97 typedef TupleTableSlot *(*ExecForeignUpdate_function) (EState *estate,
98 													   ResultRelInfo *rinfo,
99 													   TupleTableSlot *slot,
100 													   TupleTableSlot *planSlot);
101 
102 typedef TupleTableSlot *(*ExecForeignDelete_function) (EState *estate,
103 													   ResultRelInfo *rinfo,
104 													   TupleTableSlot *slot,
105 													   TupleTableSlot *planSlot);
106 
107 typedef void (*EndForeignModify_function) (EState *estate,
108 										   ResultRelInfo *rinfo);
109 
110 typedef void (*BeginForeignInsert_function) (ModifyTableState *mtstate,
111 											 ResultRelInfo *rinfo);
112 
113 typedef void (*EndForeignInsert_function) (EState *estate,
114 										   ResultRelInfo *rinfo);
115 
116 typedef int (*IsForeignRelUpdatable_function) (Relation rel);
117 
118 typedef bool (*PlanDirectModify_function) (PlannerInfo *root,
119 										   ModifyTable *plan,
120 										   Index resultRelation,
121 										   int subplan_index);
122 
123 typedef void (*BeginDirectModify_function) (ForeignScanState *node,
124 											int eflags);
125 
126 typedef TupleTableSlot *(*IterateDirectModify_function) (ForeignScanState *node);
127 
128 typedef void (*EndDirectModify_function) (ForeignScanState *node);
129 
130 typedef RowMarkType (*GetForeignRowMarkType_function) (RangeTblEntry *rte,
131 													   LockClauseStrength strength);
132 
133 typedef void (*RefetchForeignRow_function) (EState *estate,
134 											ExecRowMark *erm,
135 											Datum rowid,
136 											TupleTableSlot *slot,
137 											bool *updated);
138 
139 typedef void (*ExplainForeignScan_function) (ForeignScanState *node,
140 											 struct ExplainState *es);
141 
142 typedef void (*ExplainForeignModify_function) (ModifyTableState *mtstate,
143 											   ResultRelInfo *rinfo,
144 											   List *fdw_private,
145 											   int subplan_index,
146 											   struct ExplainState *es);
147 
148 typedef void (*ExplainDirectModify_function) (ForeignScanState *node,
149 											  struct ExplainState *es);
150 
151 typedef int (*AcquireSampleRowsFunc) (Relation relation, int elevel,
152 									  HeapTuple *rows, int targrows,
153 									  double *totalrows,
154 									  double *totaldeadrows);
155 
156 typedef bool (*AnalyzeForeignTable_function) (Relation relation,
157 											  AcquireSampleRowsFunc *func,
158 											  BlockNumber *totalpages);
159 
160 typedef List *(*ImportForeignSchema_function) (ImportForeignSchemaStmt *stmt,
161 											   Oid serverOid);
162 
163 typedef void (*ExecForeignTruncate_function) (List *rels,
164 											  DropBehavior behavior,
165 											  bool restart_seqs);
166 
167 typedef Size (*EstimateDSMForeignScan_function) (ForeignScanState *node,
168 												 ParallelContext *pcxt);
169 typedef void (*InitializeDSMForeignScan_function) (ForeignScanState *node,
170 												   ParallelContext *pcxt,
171 												   void *coordinate);
172 typedef void (*ReInitializeDSMForeignScan_function) (ForeignScanState *node,
173 													 ParallelContext *pcxt,
174 													 void *coordinate);
175 typedef void (*InitializeWorkerForeignScan_function) (ForeignScanState *node,
176 													  shm_toc *toc,
177 													  void *coordinate);
178 typedef void (*ShutdownForeignScan_function) (ForeignScanState *node);
179 typedef bool (*IsForeignScanParallelSafe_function) (PlannerInfo *root,
180 													RelOptInfo *rel,
181 													RangeTblEntry *rte);
182 typedef List *(*ReparameterizeForeignPathByChild_function) (PlannerInfo *root,
183 															List *fdw_private,
184 															RelOptInfo *child_rel);
185 
186 typedef bool (*IsForeignPathAsyncCapable_function) (ForeignPath *path);
187 
188 typedef void (*ForeignAsyncRequest_function) (AsyncRequest *areq);
189 
190 typedef void (*ForeignAsyncConfigureWait_function) (AsyncRequest *areq);
191 
192 typedef void (*ForeignAsyncNotify_function) (AsyncRequest *areq);
193 
194 /*
195  * FdwRoutine is the struct returned by a foreign-data wrapper's handler
196  * function.  It provides pointers to the callback functions needed by the
197  * planner and executor.
198  *
199  * More function pointers are likely to be added in the future.  Therefore
200  * it's recommended that the handler initialize the struct with
201  * makeNode(FdwRoutine) so that all fields are set to NULL.  This will
202  * ensure that no fields are accidentally left undefined.
203  */
204 typedef struct FdwRoutine
205 {
206 	NodeTag		type;
207 
208 	/* Functions for scanning foreign tables */
209 	GetForeignRelSize_function GetForeignRelSize;
210 	GetForeignPaths_function GetForeignPaths;
211 	GetForeignPlan_function GetForeignPlan;
212 	BeginForeignScan_function BeginForeignScan;
213 	IterateForeignScan_function IterateForeignScan;
214 	ReScanForeignScan_function ReScanForeignScan;
215 	EndForeignScan_function EndForeignScan;
216 
217 	/*
218 	 * Remaining functions are optional.  Set the pointer to NULL for any that
219 	 * are not provided.
220 	 */
221 
222 	/* Functions for remote-join planning */
223 	GetForeignJoinPaths_function GetForeignJoinPaths;
224 
225 	/* Functions for remote upper-relation (post scan/join) planning */
226 	GetForeignUpperPaths_function GetForeignUpperPaths;
227 
228 	/* Functions for updating foreign tables */
229 	AddForeignUpdateTargets_function AddForeignUpdateTargets;
230 	PlanForeignModify_function PlanForeignModify;
231 	BeginForeignModify_function BeginForeignModify;
232 	ExecForeignInsert_function ExecForeignInsert;
233 	ExecForeignBatchInsert_function ExecForeignBatchInsert;
234 	GetForeignModifyBatchSize_function GetForeignModifyBatchSize;
235 	ExecForeignUpdate_function ExecForeignUpdate;
236 	ExecForeignDelete_function ExecForeignDelete;
237 	EndForeignModify_function EndForeignModify;
238 	BeginForeignInsert_function BeginForeignInsert;
239 	EndForeignInsert_function EndForeignInsert;
240 	IsForeignRelUpdatable_function IsForeignRelUpdatable;
241 	PlanDirectModify_function PlanDirectModify;
242 	BeginDirectModify_function BeginDirectModify;
243 	IterateDirectModify_function IterateDirectModify;
244 	EndDirectModify_function EndDirectModify;
245 
246 	/* Functions for SELECT FOR UPDATE/SHARE row locking */
247 	GetForeignRowMarkType_function GetForeignRowMarkType;
248 	RefetchForeignRow_function RefetchForeignRow;
249 	RecheckForeignScan_function RecheckForeignScan;
250 
251 	/* Support functions for EXPLAIN */
252 	ExplainForeignScan_function ExplainForeignScan;
253 	ExplainForeignModify_function ExplainForeignModify;
254 	ExplainDirectModify_function ExplainDirectModify;
255 
256 	/* Support functions for ANALYZE */
257 	AnalyzeForeignTable_function AnalyzeForeignTable;
258 
259 	/* Support functions for IMPORT FOREIGN SCHEMA */
260 	ImportForeignSchema_function ImportForeignSchema;
261 
262 	/* Support functions for TRUNCATE */
263 	ExecForeignTruncate_function ExecForeignTruncate;
264 
265 	/* Support functions for parallelism under Gather node */
266 	IsForeignScanParallelSafe_function IsForeignScanParallelSafe;
267 	EstimateDSMForeignScan_function EstimateDSMForeignScan;
268 	InitializeDSMForeignScan_function InitializeDSMForeignScan;
269 	ReInitializeDSMForeignScan_function ReInitializeDSMForeignScan;
270 	InitializeWorkerForeignScan_function InitializeWorkerForeignScan;
271 	ShutdownForeignScan_function ShutdownForeignScan;
272 
273 	/* Support functions for path reparameterization. */
274 	ReparameterizeForeignPathByChild_function ReparameterizeForeignPathByChild;
275 
276 	/* Support functions for asynchronous execution */
277 	IsForeignPathAsyncCapable_function IsForeignPathAsyncCapable;
278 	ForeignAsyncRequest_function ForeignAsyncRequest;
279 	ForeignAsyncConfigureWait_function ForeignAsyncConfigureWait;
280 	ForeignAsyncNotify_function ForeignAsyncNotify;
281 } FdwRoutine;
282 
283 
284 /* Functions in foreign/foreign.c */
285 extern FdwRoutine *GetFdwRoutine(Oid fdwhandler);
286 extern Oid	GetForeignServerIdByRelId(Oid relid);
287 extern FdwRoutine *GetFdwRoutineByServerId(Oid serverid);
288 extern FdwRoutine *GetFdwRoutineByRelId(Oid relid);
289 extern FdwRoutine *GetFdwRoutineForRelation(Relation relation, bool makecopy);
290 extern bool IsImportableForeignTable(const char *tablename,
291 									 ImportForeignSchemaStmt *stmt);
292 extern Path *GetExistingLocalJoinPath(RelOptInfo *joinrel);
293 
294 #endif							/* FDWAPI_H */
295