1 /*-------------------------------------------------------------------------
2  *
3  * postgres_fdw.h
4  *		  Foreign-data wrapper for remote PostgreSQL servers
5  *
6  * Portions Copyright (c) 2012-2019, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *		  contrib/postgres_fdw/postgres_fdw.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef POSTGRES_FDW_H
14 #define POSTGRES_FDW_H
15 
16 #include "foreign/foreign.h"
17 #include "lib/stringinfo.h"
18 #include "nodes/pathnodes.h"
19 #include "utils/relcache.h"
20 
21 #include "libpq-fe.h"
22 
23 /*
24  * FDW-specific planner information kept in RelOptInfo.fdw_private for a
25  * postgres_fdw foreign table.  For a baserel, this struct is created by
26  * postgresGetForeignRelSize, although some fields are not filled till later.
27  * postgresGetForeignJoinPaths creates it for a joinrel, and
28  * postgresGetForeignUpperPaths creates it for an upperrel.
29  */
30 typedef struct PgFdwRelationInfo
31 {
32 	/*
33 	 * True means that the relation can be pushed down. Always true for simple
34 	 * foreign scan.
35 	 */
36 	bool		pushdown_safe;
37 
38 	/*
39 	 * Restriction clauses, divided into safe and unsafe to pushdown subsets.
40 	 * All entries in these lists should have RestrictInfo wrappers; that
41 	 * improves efficiency of selectivity and cost estimation.
42 	 */
43 	List	   *remote_conds;
44 	List	   *local_conds;
45 
46 	/* Actual remote restriction clauses for scan (sans RestrictInfos) */
47 	List	   *final_remote_exprs;
48 
49 	/* Bitmap of attr numbers we need to fetch from the remote server. */
50 	Bitmapset  *attrs_used;
51 
52 	/* True means that the query_pathkeys is safe to push down */
53 	bool		qp_is_pushdown_safe;
54 
55 	/* Cost and selectivity of local_conds. */
56 	QualCost	local_conds_cost;
57 	Selectivity local_conds_sel;
58 
59 	/* Selectivity of join conditions */
60 	Selectivity joinclause_sel;
61 
62 	/* Estimated size and cost for a scan, join, or grouping/aggregation. */
63 	double		rows;
64 	int			width;
65 	Cost		startup_cost;
66 	Cost		total_cost;
67 
68 	/*
69 	 * Estimated number of rows fetched from the foreign server, and costs
70 	 * excluding costs for transferring those rows from the foreign server.
71 	 * These are only used by estimate_path_cost_size().
72 	 */
73 	double		retrieved_rows;
74 	Cost		rel_startup_cost;
75 	Cost		rel_total_cost;
76 
77 	/* Options extracted from catalogs. */
78 	bool		use_remote_estimate;
79 	Cost		fdw_startup_cost;
80 	Cost		fdw_tuple_cost;
81 	List	   *shippable_extensions;	/* OIDs of whitelisted extensions */
82 
83 	/* Cached catalog information. */
84 	ForeignTable *table;
85 	ForeignServer *server;
86 	UserMapping *user;			/* only set in use_remote_estimate mode */
87 
88 	int			fetch_size;		/* fetch size for this remote table */
89 
90 	/*
91 	 * Name of the relation while EXPLAINing ForeignScan. It is used for join
92 	 * relations but is set for all relations. For join relation, the name
93 	 * indicates which foreign tables are being joined and the join type used.
94 	 */
95 	StringInfo	relation_name;
96 
97 	/* Join information */
98 	RelOptInfo *outerrel;
99 	RelOptInfo *innerrel;
100 	JoinType	jointype;
101 	/* joinclauses contains only JOIN/ON conditions for an outer join */
102 	List	   *joinclauses;	/* List of RestrictInfo */
103 
104 	/* Upper relation information */
105 	UpperRelationKind stage;
106 
107 	/* Grouping information */
108 	List	   *grouped_tlist;
109 
110 	/* Subquery information */
111 	bool		make_outerrel_subquery; /* do we deparse outerrel as a
112 										 * subquery? */
113 	bool		make_innerrel_subquery; /* do we deparse innerrel as a
114 										 * subquery? */
115 	Relids		lower_subquery_rels;	/* all relids appearing in lower
116 										 * subqueries */
117 
118 	/*
119 	 * Index of the relation.  It is used to create an alias to a subquery
120 	 * representing the relation.
121 	 */
122 	int			relation_index;
123 } PgFdwRelationInfo;
124 
125 /* in postgres_fdw.c */
126 extern int	set_transmission_modes(void);
127 extern void reset_transmission_modes(int nestlevel);
128 
129 /* in connection.c */
130 extern PGconn *GetConnection(UserMapping *user, bool will_prep_stmt);
131 extern void ReleaseConnection(PGconn *conn);
132 extern unsigned int GetCursorNumber(PGconn *conn);
133 extern unsigned int GetPrepStmtNumber(PGconn *conn);
134 extern PGresult *pgfdw_get_result(PGconn *conn, const char *query);
135 extern PGresult *pgfdw_exec_query(PGconn *conn, const char *query);
136 extern void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
137 							   bool clear, const char *sql);
138 
139 /* in option.c */
140 extern int	ExtractConnectionOptions(List *defelems,
141 									 const char **keywords,
142 									 const char **values);
143 extern List *ExtractExtensionList(const char *extensionsString,
144 								  bool warnOnMissing);
145 
146 /* in deparse.c */
147 extern void classifyConditions(PlannerInfo *root,
148 							   RelOptInfo *baserel,
149 							   List *input_conds,
150 							   List **remote_conds,
151 							   List **local_conds);
152 extern bool is_foreign_expr(PlannerInfo *root,
153 							RelOptInfo *baserel,
154 							Expr *expr);
155 extern bool is_foreign_param(PlannerInfo *root,
156 							 RelOptInfo *baserel,
157 							 Expr *expr);
158 extern void deparseInsertSql(StringInfo buf, RangeTblEntry *rte,
159 							 Index rtindex, Relation rel,
160 							 List *targetAttrs, bool doNothing,
161 							 List *withCheckOptionList, List *returningList,
162 							 List **retrieved_attrs);
163 extern void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte,
164 							 Index rtindex, Relation rel,
165 							 List *targetAttrs,
166 							 List *withCheckOptionList, List *returningList,
167 							 List **retrieved_attrs);
168 extern void deparseDirectUpdateSql(StringInfo buf, PlannerInfo *root,
169 								   Index rtindex, Relation rel,
170 								   RelOptInfo *foreignrel,
171 								   List *targetlist,
172 								   List *targetAttrs,
173 								   List *remote_conds,
174 								   List **params_list,
175 								   List *returningList,
176 								   List **retrieved_attrs);
177 extern void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte,
178 							 Index rtindex, Relation rel,
179 							 List *returningList,
180 							 List **retrieved_attrs);
181 extern void deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root,
182 								   Index rtindex, Relation rel,
183 								   RelOptInfo *foreignrel,
184 								   List *remote_conds,
185 								   List **params_list,
186 								   List *returningList,
187 								   List **retrieved_attrs);
188 extern void deparseAnalyzeSizeSql(StringInfo buf, Relation rel);
189 extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
190 							  List **retrieved_attrs);
191 extern void deparseStringLiteral(StringInfo buf, const char *val);
192 extern Expr *find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel);
193 extern Expr *find_em_expr_for_input_target(PlannerInfo *root,
194 										   EquivalenceClass *ec,
195 										   PathTarget *target);
196 extern List *build_tlist_to_deparse(RelOptInfo *foreignrel);
197 extern void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root,
198 									RelOptInfo *foreignrel, List *tlist,
199 									List *remote_conds, List *pathkeys,
200 									bool has_final_sort, bool has_limit,
201 									bool is_subquery,
202 									List **retrieved_attrs, List **params_list);
203 extern const char *get_jointype_name(JoinType jointype);
204 
205 /* in shippable.c */
206 extern bool is_builtin(Oid objectId);
207 extern bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo);
208 
209 #endif							/* POSTGRES_FDW_H */
210