PostgreSQL Source Code  git master
postgres_fdw.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * postgres_fdw.h
4  * Foreign-data wrapper for remote PostgreSQL servers
5  *
6  * Portions Copyright (c) 2012-2021, 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 "libpq-fe.h"
19 #include "nodes/execnodes.h"
20 #include "nodes/pathnodes.h"
21 #include "utils/relcache.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  */
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  */
45 
46  /* Actual remote restriction clauses for scan (sans RestrictInfos) */
48 
49  /* Bitmap of attr numbers we need to fetch from the remote server. */
51 
52  /* True means that the query_pathkeys is safe to push down */
54 
55  /* Cost and selectivity of local_conds. */
58 
59  /* Selectivity of join conditions */
61 
62  /* Estimated size and cost for a scan, join, or grouping/aggregation. */
63  double rows;
64  int width;
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  */
76 
77  /* Options extracted from catalogs. */
81  List *shippable_extensions; /* OIDs of shippable extensions */
83 
84  /* Cached catalog information. */
87  UserMapping *user; /* only set in use_remote_estimate mode */
88 
89  int fetch_size; /* fetch size for this remote table */
90 
91  /*
92  * Name of the relation, for use while EXPLAINing ForeignScan. It is used
93  * for join and upper relations but is set for all relations. For a base
94  * relation, this is really just the RT index as a string; we convert that
95  * while producing EXPLAIN output. For join and upper relations, the name
96  * indicates which base foreign tables are included and the join type or
97  * aggregation type used.
98  */
100 
101  /* Join information */
105  /* joinclauses contains only JOIN/ON conditions for an outer join */
106  List *joinclauses; /* List of RestrictInfo */
107 
108  /* Upper relation information */
110 
111  /* Grouping information */
113 
114  /* Subquery information */
115  bool make_outerrel_subquery; /* do we deparse outerrel as a
116  * subquery? */
117  bool make_innerrel_subquery; /* do we deparse innerrel as a
118  * subquery? */
119  Relids lower_subquery_rels; /* all relids appearing in lower
120  * subqueries */
121 
122  /*
123  * Index of the relation. It is used to create an alias to a subquery
124  * representing the relation.
125  */
128 
129 /*
130  * Extra control information relating to a connection.
131  */
132 typedef struct PgFdwConnState
133 {
134  AsyncRequest *pendingAreq; /* pending async request */
136 
137 /* in postgres_fdw.c */
138 extern int set_transmission_modes(void);
139 extern void reset_transmission_modes(int nestlevel);
140 extern void process_pending_request(AsyncRequest *areq);
141 
142 /* in connection.c */
143 extern PGconn *GetConnection(UserMapping *user, bool will_prep_stmt,
145 extern void ReleaseConnection(PGconn *conn);
146 extern unsigned int GetCursorNumber(PGconn *conn);
147 extern unsigned int GetPrepStmtNumber(PGconn *conn);
148 extern void do_sql_command(PGconn *conn, const char *sql);
149 extern PGresult *pgfdw_get_result(PGconn *conn, const char *query);
150 extern PGresult *pgfdw_exec_query(PGconn *conn, const char *query,
152 extern void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
153  bool clear, const char *sql);
154 
155 /* in option.c */
156 extern int ExtractConnectionOptions(List *defelems,
157  const char **keywords,
158  const char **values);
159 extern List *ExtractExtensionList(const char *extensionsString,
160  bool warnOnMissing);
161 extern char *pgfdw_application_name;
162 
163 /* in deparse.c */
164 extern void classifyConditions(PlannerInfo *root,
165  RelOptInfo *baserel,
166  List *input_conds,
167  List **remote_conds,
168  List **local_conds);
169 extern bool is_foreign_expr(PlannerInfo *root,
170  RelOptInfo *baserel,
171  Expr *expr);
172 extern bool is_foreign_param(PlannerInfo *root,
173  RelOptInfo *baserel,
174  Expr *expr);
175 extern void deparseInsertSql(StringInfo buf, RangeTblEntry *rte,
176  Index rtindex, Relation rel,
177  List *targetAttrs, bool doNothing,
178  List *withCheckOptionList, List *returningList,
179  List **retrieved_attrs, int *values_end_len);
180 extern void rebuildInsertSql(StringInfo buf, Relation rel,
181  char *orig_query, List *target_attrs,
182  int values_end_len, int num_params,
183  int num_rows);
184 extern void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte,
185  Index rtindex, Relation rel,
186  List *targetAttrs,
187  List *withCheckOptionList, List *returningList,
188  List **retrieved_attrs);
190  Index rtindex, Relation rel,
191  RelOptInfo *foreignrel,
192  List *targetlist,
193  List *targetAttrs,
195  List **params_list,
196  List *returningList,
197  List **retrieved_attrs);
198 extern void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte,
199  Index rtindex, Relation rel,
200  List *returningList,
201  List **retrieved_attrs);
203  Index rtindex, Relation rel,
204  RelOptInfo *foreignrel,
206  List **params_list,
207  List *returningList,
208  List **retrieved_attrs);
209 extern void deparseAnalyzeSizeSql(StringInfo buf, Relation rel);
210 extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
211  List **retrieved_attrs);
212 extern void deparseTruncateSql(StringInfo buf,
213  List *rels,
214  DropBehavior behavior,
215  bool restart_seqs);
216 extern void deparseStringLiteral(StringInfo buf, const char *val);
219  EquivalenceClass *ec,
220  PathTarget *target);
221 extern List *build_tlist_to_deparse(RelOptInfo *foreignrel);
223  RelOptInfo *foreignrel, List *tlist,
224  List *remote_conds, List *pathkeys,
225  bool has_final_sort, bool has_limit,
226  bool is_subquery,
227  List **retrieved_attrs, List **params_list);
228 extern const char *get_jointype_name(JoinType jointype);
229 
230 /* in shippable.c */
231 extern bool is_builtin(Oid objectId);
232 extern bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo);
233 
234 #endif /* POSTGRES_FDW_H */
bool is_builtin(Oid objectId)
Definition: shippable.c:152
void deparseInsertSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, bool doNothing, List *withCheckOptionList, List *returningList, List **retrieved_attrs, int *values_end_len)
Definition: deparse.c:1841
UpperRelationKind
Definition: pathnodes.h:67
void reset_transmission_modes(int nestlevel)
List * build_tlist_to_deparse(RelOptInfo *foreignrel)
Definition: deparse.c:1071
bool is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:233
struct PgFdwConnState PgFdwConnState
ForeignServer * server
Definition: postgres_fdw.h:86
int ExtractConnectionOptions(List *defelems, const char **keywords, const char **values)
Definition: option.c:377
Relids lower_subquery_rels
Definition: postgres_fdw.h:119
void classifyConditions(PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds)
Definition: deparse.c:207
double Selectivity
Definition: nodes.h:669
unsigned int Oid
Definition: postgres_ext.h:31
bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
Definition: shippable.c:162
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
JoinType
Definition: nodes.h:704
const char * get_jointype_name(JoinType jointype)
Definition: deparse.c:1502
unsigned int GetPrepStmtNumber(PGconn *conn)
Definition: connection.c:689
bool is_foreign_param(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:1004
PGconn * conn
Definition: streamutil.c:54
Selectivity local_conds_sel
Definition: postgres_fdw.h:57
void deparseDirectUpdateSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *targetlist, List *targetAttrs, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2034
void deparseTruncateSql(StringInfo buf, List *rels, DropBehavior behavior, bool restart_seqs)
Definition: deparse.c:2334
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:408
Selectivity joinclause_sel
Definition: postgres_fdw.h:60
static char * buf
Definition: pg_test_fsync.c:68
PGresult * pgfdw_get_result(PGconn *conn, const char *query)
Definition: connection.c:730
void do_sql_command(PGconn *conn, const char *sql)
Definition: connection.c:588
void process_pending_request(AsyncRequest *areq)
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:125
int set_transmission_modes(void)
void deparseAnalyzeSql(StringInfo buf, Relation rel, List **retrieved_attrs)
Definition: deparse.c:2276
PGresult * pgfdw_exec_query(PGconn *conn, const char *query, PgFdwConnState *state)
Definition: connection.c:702
void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, List *tlist, List *remote_conds, List *pathkeys, bool has_final_sort, bool has_limit, bool is_subquery, List **retrieved_attrs, List **params_list)
Definition: deparse.c:1128
static int elevel
Definition: vacuumlazy.c:401
void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2119
UserMapping * user
Definition: postgres_fdw.h:87
DropBehavior
Definition: parsenodes.h:1858
unsigned int Index
Definition: c.h:549
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
Definition: equivclass.c:939
unsigned int GetCursorNumber(PGconn *conn)
Definition: connection.c:675
AsyncRequest * pendingAreq
Definition: postgres_fdw.h:134
Bitmapset * attrs_used
Definition: postgres_fdw.h:50
void deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2148
Definition: regguts.h:317
ForeignTable * table
Definition: postgres_fdw.h:85
void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn, bool clear, const char *sql)
Definition: connection.c:795
void deparseAnalyzeSizeSql(StringInfo buf, Relation rel)
Definition: deparse.c:2256
void ReleaseConnection(PGconn *conn)
Definition: connection.c:654
static Datum values[MAXATTR]
Definition: bootstrap.c:156
RelOptInfo * innerrel
Definition: postgres_fdw.h:103
List * final_remote_exprs
Definition: postgres_fdw.h:47
UpperRelationKind stage
Definition: postgres_fdw.h:109
Expr * find_em_expr_for_input_target(PlannerInfo *root, EquivalenceClass *ec, PathTarget *target)
char * pgfdw_application_name
Definition: option.c:51
List * shippable_extensions
Definition: postgres_fdw.h:81
Definition: pg_list.h:50
struct PgFdwRelationInfo PgFdwRelationInfo
void rebuildInsertSql(StringInfo buf, Relation rel, char *orig_query, List *target_attrs, int values_end_len, int num_params, int num_rows)
Definition: deparse.c:1914
long val
Definition: informix.c:664
double Cost
Definition: nodes.h:670
void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1974
QualCost local_conds_cost
Definition: postgres_fdw.h:56
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2537