1 /*-------------------------------------------------------------------------
2  *
3  * fdwapi.h
4  *	  API for foreign-data wrappers
5  *
6  * Copyright (c) 2010-2018, 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/relation.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 HeapTuple (*RefetchForeignRow_function) (EState *estate,
125 												 ExecRowMark *erm,
126 												 Datum rowid,
127 												 bool *updated);
128 
129 typedef void (*ExplainForeignScan_function) (ForeignScanState *node,
130 											 struct ExplainState *es);
131 
132 typedef void (*ExplainForeignModify_function) (ModifyTableState *mtstate,
133 											   ResultRelInfo *rinfo,
134 											   List *fdw_private,
135 											   int subplan_index,
136 											   struct ExplainState *es);
137 
138 typedef void (*ExplainDirectModify_function) (ForeignScanState *node,
139 											  struct ExplainState *es);
140 
141 typedef int (*AcquireSampleRowsFunc) (Relation relation, int elevel,
142 									  HeapTuple *rows, int targrows,
143 									  double *totalrows,
144 									  double *totaldeadrows);
145 
146 typedef bool (*AnalyzeForeignTable_function) (Relation relation,
147 											  AcquireSampleRowsFunc *func,
148 											  BlockNumber *totalpages);
149 
150 typedef List *(*ImportForeignSchema_function) (ImportForeignSchemaStmt *stmt,
151 											   Oid serverOid);
152 
153 typedef Size (*EstimateDSMForeignScan_function) (ForeignScanState *node,
154 												 ParallelContext *pcxt);
155 typedef void (*InitializeDSMForeignScan_function) (ForeignScanState *node,
156 												   ParallelContext *pcxt,
157 												   void *coordinate);
158 typedef void (*ReInitializeDSMForeignScan_function) (ForeignScanState *node,
159 													 ParallelContext *pcxt,
160 													 void *coordinate);
161 typedef void (*InitializeWorkerForeignScan_function) (ForeignScanState *node,
162 													  shm_toc *toc,
163 													  void *coordinate);
164 typedef void (*ShutdownForeignScan_function) (ForeignScanState *node);
165 typedef bool (*IsForeignScanParallelSafe_function) (PlannerInfo *root,
166 													RelOptInfo *rel,
167 													RangeTblEntry *rte);
168 typedef List *(*ReparameterizeForeignPathByChild_function) (PlannerInfo *root,
169 															List *fdw_private,
170 															RelOptInfo *child_rel);
171 
172 /*
173  * FdwRoutine is the struct returned by a foreign-data wrapper's handler
174  * function.  It provides pointers to the callback functions needed by the
175  * planner and executor.
176  *
177  * More function pointers are likely to be added in the future.  Therefore
178  * it's recommended that the handler initialize the struct with
179  * makeNode(FdwRoutine) so that all fields are set to NULL.  This will
180  * ensure that no fields are accidentally left undefined.
181  */
182 typedef struct FdwRoutine
183 {
184 	NodeTag		type;
185 
186 	/* Functions for scanning foreign tables */
187 	GetForeignRelSize_function GetForeignRelSize;
188 	GetForeignPaths_function GetForeignPaths;
189 	GetForeignPlan_function GetForeignPlan;
190 	BeginForeignScan_function BeginForeignScan;
191 	IterateForeignScan_function IterateForeignScan;
192 	ReScanForeignScan_function ReScanForeignScan;
193 	EndForeignScan_function EndForeignScan;
194 
195 	/*
196 	 * Remaining functions are optional.  Set the pointer to NULL for any that
197 	 * are not provided.
198 	 */
199 
200 	/* Functions for remote-join planning */
201 	GetForeignJoinPaths_function GetForeignJoinPaths;
202 
203 	/* Functions for remote upper-relation (post scan/join) planning */
204 	GetForeignUpperPaths_function GetForeignUpperPaths;
205 
206 	/* Functions for updating foreign tables */
207 	AddForeignUpdateTargets_function AddForeignUpdateTargets;
208 	PlanForeignModify_function PlanForeignModify;
209 	BeginForeignModify_function BeginForeignModify;
210 	ExecForeignInsert_function ExecForeignInsert;
211 	ExecForeignUpdate_function ExecForeignUpdate;
212 	ExecForeignDelete_function ExecForeignDelete;
213 	EndForeignModify_function EndForeignModify;
214 	BeginForeignInsert_function BeginForeignInsert;
215 	EndForeignInsert_function EndForeignInsert;
216 	IsForeignRelUpdatable_function IsForeignRelUpdatable;
217 	PlanDirectModify_function PlanDirectModify;
218 	BeginDirectModify_function BeginDirectModify;
219 	IterateDirectModify_function IterateDirectModify;
220 	EndDirectModify_function EndDirectModify;
221 
222 	/* Functions for SELECT FOR UPDATE/SHARE row locking */
223 	GetForeignRowMarkType_function GetForeignRowMarkType;
224 	RefetchForeignRow_function RefetchForeignRow;
225 	RecheckForeignScan_function RecheckForeignScan;
226 
227 	/* Support functions for EXPLAIN */
228 	ExplainForeignScan_function ExplainForeignScan;
229 	ExplainForeignModify_function ExplainForeignModify;
230 	ExplainDirectModify_function ExplainDirectModify;
231 
232 	/* Support functions for ANALYZE */
233 	AnalyzeForeignTable_function AnalyzeForeignTable;
234 
235 	/* Support functions for IMPORT FOREIGN SCHEMA */
236 	ImportForeignSchema_function ImportForeignSchema;
237 
238 	/* Support functions for parallelism under Gather node */
239 	IsForeignScanParallelSafe_function IsForeignScanParallelSafe;
240 	EstimateDSMForeignScan_function EstimateDSMForeignScan;
241 	InitializeDSMForeignScan_function InitializeDSMForeignScan;
242 	ReInitializeDSMForeignScan_function ReInitializeDSMForeignScan;
243 	InitializeWorkerForeignScan_function InitializeWorkerForeignScan;
244 	ShutdownForeignScan_function ShutdownForeignScan;
245 
246 	/* Support functions for path reparameterization. */
247 	ReparameterizeForeignPathByChild_function ReparameterizeForeignPathByChild;
248 } FdwRoutine;
249 
250 
251 /* Functions in foreign/foreign.c */
252 extern FdwRoutine *GetFdwRoutine(Oid fdwhandler);
253 extern Oid	GetForeignServerIdByRelId(Oid relid);
254 extern FdwRoutine *GetFdwRoutineByServerId(Oid serverid);
255 extern FdwRoutine *GetFdwRoutineByRelId(Oid relid);
256 extern FdwRoutine *GetFdwRoutineForRelation(Relation relation, bool makecopy);
257 extern bool IsImportableForeignTable(const char *tablename,
258 						 ImportForeignSchemaStmt *stmt);
259 extern Path *GetExistingLocalJoinPath(RelOptInfo *joinrel);
260 
261 #endif							/* FDWAPI_H */
262