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-2023, 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  Relids hidden_subquery_rels; /* relids, which can't be referred to
122  * from upper relations, used
123  * internally for equivalence member
124  * search */
125 
126  /*
127  * Index of the relation. It is used to create an alias to a subquery
128  * representing the relation.
129  */
132 
133 /*
134  * Extra control information relating to a connection.
135  */
136 typedef struct PgFdwConnState
137 {
138  AsyncRequest *pendingAreq; /* pending async request */
140 
141 /*
142  * Method used by ANALYZE to sample remote rows.
143  */
145 {
146  ANALYZE_SAMPLE_OFF, /* no remote sampling */
147  ANALYZE_SAMPLE_AUTO, /* choose by server version */
148  ANALYZE_SAMPLE_RANDOM, /* remote random() */
149  ANALYZE_SAMPLE_SYSTEM, /* TABLESAMPLE system */
150  ANALYZE_SAMPLE_BERNOULLI, /* TABLESAMPLE bernoulli */
152 
153 /* in postgres_fdw.c */
154 extern int set_transmission_modes(void);
155 extern void reset_transmission_modes(int nestlevel);
156 extern void process_pending_request(AsyncRequest *areq);
157 
158 /* in connection.c */
159 extern PGconn *GetConnection(UserMapping *user, bool will_prep_stmt,
161 extern void ReleaseConnection(PGconn *conn);
162 extern unsigned int GetCursorNumber(PGconn *conn);
163 extern unsigned int GetPrepStmtNumber(PGconn *conn);
164 extern void do_sql_command(PGconn *conn, const char *sql);
165 extern PGresult *pgfdw_get_result(PGconn *conn, const char *query);
166 extern PGresult *pgfdw_exec_query(PGconn *conn, const char *query,
168 extern void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
169  bool clear, const char *sql);
170 
171 /* in option.c */
172 extern int ExtractConnectionOptions(List *defelems,
173  const char **keywords,
174  const char **values);
175 extern List *ExtractExtensionList(const char *extensionsString,
176  bool warnOnMissing);
177 extern char *process_pgfdw_appname(const char *appname);
178 extern char *pgfdw_application_name;
179 
180 /* in deparse.c */
181 extern void classifyConditions(PlannerInfo *root,
182  RelOptInfo *baserel,
183  List *input_conds,
184  List **remote_conds,
185  List **local_conds);
186 extern bool is_foreign_expr(PlannerInfo *root,
187  RelOptInfo *baserel,
188  Expr *expr);
189 extern bool is_foreign_param(PlannerInfo *root,
190  RelOptInfo *baserel,
191  Expr *expr);
192 extern bool is_foreign_pathkey(PlannerInfo *root,
193  RelOptInfo *baserel,
194  PathKey *pathkey);
195 extern void deparseInsertSql(StringInfo buf, RangeTblEntry *rte,
196  Index rtindex, Relation rel,
197  List *targetAttrs, bool doNothing,
198  List *withCheckOptionList, List *returningList,
199  List **retrieved_attrs, int *values_end_len);
200 extern void rebuildInsertSql(StringInfo buf, Relation rel,
201  char *orig_query, List *target_attrs,
202  int values_end_len, int num_params,
203  int num_rows);
204 extern void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte,
205  Index rtindex, Relation rel,
206  List *targetAttrs,
207  List *withCheckOptionList, List *returningList,
208  List **retrieved_attrs);
210  Index rtindex, Relation rel,
211  RelOptInfo *foreignrel,
212  List *targetlist,
213  List *targetAttrs,
214  List *remote_conds,
215  List **params_list,
216  List *returningList,
217  List **retrieved_attrs);
218 extern void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte,
219  Index rtindex, Relation rel,
220  List *returningList,
221  List **retrieved_attrs);
223  Index rtindex, Relation rel,
224  RelOptInfo *foreignrel,
225  List *remote_conds,
226  List **params_list,
227  List *returningList,
228  List **retrieved_attrs);
229 extern void deparseAnalyzeSizeSql(StringInfo buf, Relation rel);
230 extern void deparseAnalyzeInfoSql(StringInfo buf, Relation rel);
231 extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
232  PgFdwSamplingMethod sample_method,
233  double sample_frac,
234  List **retrieved_attrs);
235 extern void deparseTruncateSql(StringInfo buf,
236  List *rels,
237  DropBehavior behavior,
238  bool restart_seqs);
239 extern void deparseStringLiteral(StringInfo buf, const char *val);
241  EquivalenceClass *ec,
242  RelOptInfo *rel);
244  EquivalenceClass *ec,
245  RelOptInfo *rel);
246 extern List *build_tlist_to_deparse(RelOptInfo *foreignrel);
248  RelOptInfo *rel, List *tlist,
249  List *remote_conds, List *pathkeys,
250  bool has_final_sort, bool has_limit,
251  bool is_subquery,
252  List **retrieved_attrs, List **params_list);
253 extern const char *get_jointype_name(JoinType jointype);
254 
255 /* in shippable.c */
256 extern bool is_builtin(Oid objectId);
257 extern bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo);
258 
259 #endif /* POSTGRES_FDW_H */
static Datum values[MAXATTR]
Definition: bootstrap.c:156
unsigned int Index
Definition: c.h:603
long val
Definition: informix.c:664
double Cost
Definition: nodes.h:262
double Selectivity
Definition: nodes.h:261
JoinType
Definition: nodes.h:299
DropBehavior
Definition: parsenodes.h:2168
UpperRelationKind
Definition: pathnodes.h:70
static char * user
Definition: pg_regress.c:120
static char * buf
Definition: pg_test_fsync.c:73
unsigned int Oid
Definition: postgres_ext.h:31
bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
Definition: shippable.c:162
unsigned int GetCursorNumber(PGconn *conn)
Definition: connection.c:800
void deparseAnalyzeSizeSql(StringInfo buf, Relation rel)
Definition: deparse.c:2499
void do_sql_command(PGconn *conn, const char *sql)
Definition: connection.c:693
void ReleaseConnection(PGconn *conn)
Definition: connection.c:779
struct PgFdwConnState PgFdwConnState
PGresult * pgfdw_get_result(PGconn *conn, const char *query)
Definition: connection.c:855
char * process_pgfdw_appname(const char *appname)
Definition: option.c:491
const char * get_jointype_name(JoinType jointype)
Definition: deparse.c:1641
void deparseAnalyzeInfoSql(StringInfo buf, Relation rel)
Definition: deparse.c:2521
bool is_builtin(Oid objectId)
Definition: shippable.c:152
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:2391
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:2276
void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn, bool clear, const char *sql)
Definition: connection.c:924
PGresult * pgfdw_exec_query(PGconn *conn, const char *query, PgFdwConnState *state)
Definition: connection.c:827
bool is_foreign_param(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:1082
void reset_transmission_modes(int nestlevel)
int set_transmission_modes(void)
void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, 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:1233
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2849
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:2156
int ExtractConnectionOptions(List *defelems, const char **keywords, const char **values)
Definition: option.c:414
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:2083
void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2216
void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2362
void deparseAnalyzeSql(StringInfo buf, Relation rel, PgFdwSamplingMethod sample_method, double sample_frac, List **retrieved_attrs)
Definition: deparse.c:2561
EquivalenceMember * find_em_for_rel(PlannerInfo *root, EquivalenceClass *ec, RelOptInfo *rel)
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:445
struct PgFdwRelationInfo PgFdwRelationInfo
PgFdwSamplingMethod
Definition: postgres_fdw.h:145
@ ANALYZE_SAMPLE_AUTO
Definition: postgres_fdw.h:147
@ ANALYZE_SAMPLE_OFF
Definition: postgres_fdw.h:146
@ ANALYZE_SAMPLE_BERNOULLI
Definition: postgres_fdw.h:150
@ ANALYZE_SAMPLE_SYSTEM
Definition: postgres_fdw.h:149
@ ANALYZE_SAMPLE_RANDOM
Definition: postgres_fdw.h:148
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:177
unsigned int GetPrepStmtNumber(PGconn *conn)
Definition: connection.c:814
char * pgfdw_application_name
Definition: option.c:52
bool is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:244
List * build_tlist_to_deparse(RelOptInfo *foreignrel)
Definition: deparse.c:1176
void classifyConditions(PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds)
Definition: deparse.c:218
void process_pending_request(AsyncRequest *areq)
void deparseTruncateSql(StringInfo buf, List *rels, DropBehavior behavior, bool restart_seqs)
Definition: deparse.c:2646
bool is_foreign_pathkey(PlannerInfo *root, RelOptInfo *baserel, PathKey *pathkey)
Definition: deparse.c:1123
EquivalenceMember * find_em_for_rel_target(PlannerInfo *root, EquivalenceClass *ec, RelOptInfo *rel)
PGconn * conn
Definition: streamutil.c:54
Definition: pg_list.h:54
AsyncRequest * pendingAreq
Definition: postgres_fdw.h:138
List * shippable_extensions
Definition: postgres_fdw.h:81
Relids lower_subquery_rels
Definition: postgres_fdw.h:119
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
ForeignTable * table
Definition: postgres_fdw.h:85
Selectivity joinclause_sel
Definition: postgres_fdw.h:60
List * final_remote_exprs
Definition: postgres_fdw.h:47
UserMapping * user
Definition: postgres_fdw.h:87
Relids hidden_subquery_rels
Definition: postgres_fdw.h:121
Selectivity local_conds_sel
Definition: postgres_fdw.h:57
ForeignServer * server
Definition: postgres_fdw.h:86
QualCost local_conds_cost
Definition: postgres_fdw.h:56
Bitmapset * attrs_used
Definition: postgres_fdw.h:50
RelOptInfo * innerrel
Definition: postgres_fdw.h:103
UpperRelationKind stage
Definition: postgres_fdw.h:109
Definition: regguts.h:323