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