1 /*-------------------------------------------------------------------------
2  *
3  * postgres_fdw.h
4  *		  Foreign-data wrapper for remote PostgreSQL servers
5  *
6  * Portions Copyright (c) 2012-2016, 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/relation.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  * foreign table.  This information is collected by postgresGetForeignRelSize.
26  */
27 typedef struct PgFdwRelationInfo
28 {
29 	/*
30 	 * True means that the relation can be pushed down. Always true for simple
31 	 * foreign scan.
32 	 */
33 	bool		pushdown_safe;
34 
35 	/*
36 	 * Restriction clauses, divided into safe and unsafe to pushdown subsets.
37 	 *
38 	 * For a base foreign relation this is a list of clauses along-with
39 	 * RestrictInfo wrapper. Keeping RestrictInfo wrapper helps while dividing
40 	 * scan_clauses in postgresGetForeignPlan into safe and unsafe subsets.
41 	 * Also it helps in estimating costs since RestrictInfo caches the
42 	 * selectivity and qual cost for the clause in it.
43 	 *
44 	 * For a join relation, however, they are part of otherclause list
45 	 * obtained from extract_actual_join_clauses, which strips RestrictInfo
46 	 * construct. So, for a join relation they are list of bare clauses.
47 	 */
48 	List	   *remote_conds;
49 	List	   *local_conds;
50 
51 	/* Bitmap of attr numbers we need to fetch from the remote server. */
52 	Bitmapset  *attrs_used;
53 
54 	/* Cost and selectivity of local_conds. */
55 	QualCost	local_conds_cost;
56 	Selectivity local_conds_sel;
57 
58 	/* Selectivity of join conditions */
59 	Selectivity joinclause_sel;
60 
61 	/* Estimated size and cost for a scan or join. */
62 	double		rows;
63 	int			width;
64 	Cost		startup_cost;
65 	Cost		total_cost;
66 	/* Costs excluding costs for transferring data from the foreign server */
67 	Cost		rel_startup_cost;
68 	Cost		rel_total_cost;
69 
70 	/* Options extracted from catalogs. */
71 	bool		use_remote_estimate;
72 	Cost		fdw_startup_cost;
73 	Cost		fdw_tuple_cost;
74 	List	   *shippable_extensions;	/* OIDs of whitelisted extensions */
75 
76 	/* Cached catalog information. */
77 	ForeignTable *table;
78 	ForeignServer *server;
79 	UserMapping *user;			/* only set in use_remote_estimate mode */
80 
81 	int			fetch_size;		/* fetch size for this remote table */
82 
83 	/*
84 	 * Name of the relation while EXPLAINing ForeignScan. It is used for join
85 	 * relations but is set for all relations. For join relation, the name
86 	 * indicates which foreign tables are being joined and the join type used.
87 	 */
88 	StringInfo	relation_name;
89 
90 	/* Join information */
91 	RelOptInfo *outerrel;
92 	RelOptInfo *innerrel;
93 	JoinType	jointype;
94 	List	   *joinclauses;
95 } PgFdwRelationInfo;
96 
97 /* in postgres_fdw.c */
98 extern int	set_transmission_modes(void);
99 extern void reset_transmission_modes(int nestlevel);
100 
101 /* in connection.c */
102 extern PGconn *GetConnection(UserMapping *user, bool will_prep_stmt);
103 extern void ReleaseConnection(PGconn *conn);
104 extern unsigned int GetCursorNumber(PGconn *conn);
105 extern unsigned int GetPrepStmtNumber(PGconn *conn);
106 extern PGresult *pgfdw_get_result(PGconn *conn, const char *query);
107 extern PGresult *pgfdw_exec_query(PGconn *conn, const char *query);
108 extern void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
109 				   bool clear, const char *sql);
110 
111 /* in option.c */
112 extern int ExtractConnectionOptions(List *defelems,
113 						 const char **keywords,
114 						 const char **values);
115 extern List *ExtractExtensionList(const char *extensionsString,
116 					 bool warnOnMissing);
117 
118 /* in deparse.c */
119 extern void classifyConditions(PlannerInfo *root,
120 				   RelOptInfo *baserel,
121 				   List *input_conds,
122 				   List **remote_conds,
123 				   List **local_conds);
124 extern bool is_foreign_expr(PlannerInfo *root,
125 				RelOptInfo *baserel,
126 				Expr *expr);
127 extern void deparseInsertSql(StringInfo buf, PlannerInfo *root,
128 				 Index rtindex, Relation rel,
129 				 List *targetAttrs, bool doNothing, List *returningList,
130 				 List **retrieved_attrs);
131 extern void deparseUpdateSql(StringInfo buf, PlannerInfo *root,
132 				 Index rtindex, Relation rel,
133 				 List *targetAttrs, List *returningList,
134 				 List **retrieved_attrs);
135 extern void deparseDirectUpdateSql(StringInfo buf, PlannerInfo *root,
136 					   Index rtindex, Relation rel,
137 					   List *targetlist,
138 					   List *targetAttrs,
139 					   List *remote_conds,
140 					   List **params_list,
141 					   List *returningList,
142 					   List **retrieved_attrs);
143 extern void deparseDeleteSql(StringInfo buf, PlannerInfo *root,
144 				 Index rtindex, Relation rel,
145 				 List *returningList,
146 				 List **retrieved_attrs);
147 extern void deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root,
148 					   Index rtindex, Relation rel,
149 					   List *remote_conds,
150 					   List **params_list,
151 					   List *returningList,
152 					   List **retrieved_attrs);
153 extern void deparseAnalyzeSizeSql(StringInfo buf, Relation rel);
154 extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
155 				  List **retrieved_attrs);
156 extern void deparseStringLiteral(StringInfo buf, const char *val);
157 extern Expr *find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel);
158 extern List *build_tlist_to_deparse(RelOptInfo *foreign_rel);
159 extern void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root,
160 						RelOptInfo *foreignrel, List *tlist,
161 						List *remote_conds, List *pathkeys,
162 						List **retrieved_attrs, List **params_list);
163 
164 /* in shippable.c */
165 extern bool is_builtin(Oid objectId);
166 extern bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo);
167 extern const char *get_jointype_name(JoinType jointype);
168 
169 #endif   /* POSTGRES_FDW_H */
170