| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* postgres_fdw.h |
| 4 |
* Foreign-data wrapper for remote PostgreSQL servers |
| 5 |
* |
| 6 |
* Portions Copyright (c) 2012-2025, 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 |
*/ |
| 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 |
int disabled_nodes; |
| 66 |
Cost startup_cost; |
| 67 |
Cost total_cost; |
| 68 |
|
| 69 |
/* |
| 70 |
* Estimated number of rows fetched from the foreign server, and costs |
| 71 |
* excluding costs for transferring those rows from the foreign server. |
| 72 |
* These are only used by estimate_path_cost_size(). |
| 73 |
*/ |
| 74 |
double retrieved_rows; |
| 75 |
Cost rel_startup_cost; |
| 76 |
Cost rel_total_cost; |
| 77 |
|
| 78 |
/* Options extracted from catalogs. */ |
| 79 |
bool use_remote_estimate; |
| 80 |
Cost fdw_startup_cost; |
| 81 |
Cost fdw_tuple_cost; |
| 82 |
List *shippable_extensions; /* OIDs of shippable extensions */ |
| 83 |
bool async_capable; |
| 84 |
|
| 85 |
/* Cached catalog information. */ |
| 86 |
ForeignTable *table; |
| 87 |
ForeignServer *server; |
| 88 |
UserMapping *user; /* only set in use_remote_estimate mode */ |
| 89 |
|
| 90 |
int fetch_size; /* fetch size for this remote table */ |
| 91 |
|
| 92 |
/* |
| 93 |
* Name of the relation, for use while EXPLAINing ForeignScan. It is used |
| 94 |
* for join and upper relations but is set for all relations. For a base |
| 95 |
* relation, this is really just the RT index as a string; we convert that |
| 96 |
* while producing EXPLAIN output. For join and upper relations, the name |
| 97 |
* indicates which base foreign tables are included and the join type or |
| 98 |
* aggregation type used. |
| 99 |
*/ |
| 100 |
char *relation_name; |
| 101 |
|
| 102 |
/* Join information */ |
| 103 |
RelOptInfo *outerrel; |
| 104 |
RelOptInfo *innerrel; |
| 105 |
JoinType jointype; |
| 106 |
/* joinclauses contains only JOIN/ON conditions for an outer join */ |
| 107 |
List *joinclauses; /* List of RestrictInfo */ |
| 108 |
|
| 109 |
/* Upper relation information */ |
| 110 |
UpperRelationKind stage; |
| 111 |
|
| 112 |
/* Grouping information */ |
| 113 |
List *grouped_tlist; |
| 114 |
|
| 115 |
/* Subquery information */ |
| 116 |
bool make_outerrel_subquery; /* do we deparse outerrel as a |
| 117 |
* subquery? */ |
| 118 |
bool make_innerrel_subquery; /* do we deparse innerrel as a |
| 119 |
* subquery? */ |
| 120 |
Relids lower_subquery_rels; /* all relids appearing in lower |
| 121 |
* subqueries */ |
| 122 |
Relids hidden_subquery_rels; /* relids, which can't be referred to |
| 123 |
* from upper relations, used |
| 124 |
* internally for equivalence member |
| 125 |
* search */ |
| 126 |
|
| 127 |
/* |
| 128 |
* Index of the relation. It is used to create an alias to a subquery |
| 129 |
* representing the relation. |
| 130 |
*/ |
| 131 |
int relation_index; |
| 132 |
} PgFdwRelationInfo; |
| 133 |
|
| 134 |
/* |
| 135 |
* Extra control information relating to a connection. |
| 136 |
*/ |
| 137 |
typedef struct PgFdwConnState |
| 138 |
{ |
| 139 |
AsyncRequest *pendingAreq; /* pending async request */ |
| 140 |
} PgFdwConnState; |
| 141 |
|
| 142 |
/* |
| 143 |
* Method used by ANALYZE to sample remote rows. |
| 144 |
*/ |
| 145 |
typedef enum PgFdwSamplingMethod |
| 146 |
{ |
| 147 |
ANALYZE_SAMPLE_OFF, /* no remote sampling */ |
| 148 |
ANALYZE_SAMPLE_AUTO, /* choose by server version */ |
| 149 |
ANALYZE_SAMPLE_RANDOM, /* remote random() */ |
| 150 |
ANALYZE_SAMPLE_SYSTEM, /* TABLESAMPLE system */ |
| 151 |
ANALYZE_SAMPLE_BERNOULLI, /* TABLESAMPLE bernoulli */ |
| 152 |
} PgFdwSamplingMethod; |
| 153 |
|
| 154 |
/* in postgres_fdw.c */ |
| 155 |
extern int set_transmission_modes(void); |
| 156 |
extern void reset_transmission_modes(int nestlevel); |
| 157 |
extern void process_pending_request(AsyncRequest *areq); |
| 158 |
|
| 159 |
/* in connection.c */ |
| 160 |
extern PGconn *GetConnection(UserMapping *user, bool will_prep_stmt, |
| 161 |
PgFdwConnState **state); |
| 162 |
extern void ReleaseConnection(PGconn *conn); |
| 163 |
extern unsigned int GetCursorNumber(PGconn *conn); |
| 164 |
extern unsigned int GetPrepStmtNumber(PGconn *conn); |
| 165 |
extern void do_sql_command(PGconn *conn, const char *sql); |
| 166 |
extern PGresult *pgfdw_get_result(PGconn *conn); |
| 167 |
extern PGresult *pgfdw_exec_query(PGconn *conn, const char *query, |
| 168 |
PgFdwConnState *state); |
| 169 |
extern void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn, |
| 170 |
bool clear, const char *sql); |
| 171 |
|
| 172 |
/* in option.c */ |
| 173 |
extern int ExtractConnectionOptions(List *defelems, |
| 174 |
const char **keywords, |
| 175 |
const char **values); |
| 176 |
extern List *ExtractExtensionList(const char *extensionsString, |
| 177 |
bool warnOnMissing); |
| 178 |
extern char *process_pgfdw_appname(const char *appname); |
| 179 |
extern char *pgfdw_application_name; |
| 180 |
|
| 181 |
/* in deparse.c */ |
| 182 |
extern void classifyConditions(PlannerInfo *root, |
| 183 |
RelOptInfo *baserel, |
| 184 |
List *input_conds, |
| 185 |
List **remote_conds, |
| 186 |
List **local_conds); |
| 187 |
extern bool is_foreign_expr(PlannerInfo *root, |
| 188 |
RelOptInfo *baserel, |
| 189 |
Expr *expr); |
| 190 |
extern bool is_foreign_param(PlannerInfo *root, |
| 191 |
RelOptInfo *baserel, |
| 192 |
Expr *expr); |
| 193 |
extern bool is_foreign_pathkey(PlannerInfo *root, |
| 194 |
RelOptInfo *baserel, |
| 195 |
PathKey *pathkey); |
| 196 |
extern void deparseInsertSql(StringInfo buf, RangeTblEntry *rte, |
| 197 |
Index rtindex, Relation rel, |
| 198 |
List *targetAttrs, bool doNothing, |
| 199 |
List *withCheckOptionList, List *returningList, |
| 200 |
List **retrieved_attrs, int *values_end_len); |
| 201 |
extern void rebuildInsertSql(StringInfo buf, Relation rel, |
| 202 |
char *orig_query, List *target_attrs, |
| 203 |
int values_end_len, int num_params, |
| 204 |
int num_rows); |
| 205 |
extern void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte, |
| 206 |
Index rtindex, Relation rel, |
| 207 |
List *targetAttrs, |
| 208 |
List *withCheckOptionList, List *returningList, |
| 209 |
List **retrieved_attrs); |
| 210 |
extern void deparseDirectUpdateSql(StringInfo buf, PlannerInfo *root, |
| 211 |
Index rtindex, Relation rel, |
| 212 |
RelOptInfo *foreignrel, |
| 213 |
List *targetlist, |
| 214 |
List *targetAttrs, |
| 215 |
List *remote_conds, |
| 216 |
List **params_list, |
| 217 |
List *returningList, |
| 218 |
List **retrieved_attrs); |
| 219 |
extern void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte, |
| 220 |
Index rtindex, Relation rel, |
| 221 |
List *returningList, |
| 222 |
List **retrieved_attrs); |
| 223 |
extern void deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root, |
| 224 |
Index rtindex, Relation rel, |
| 225 |
RelOptInfo *foreignrel, |
| 226 |
List *remote_conds, |
| 227 |
List **params_list, |
| 228 |
List *returningList, |
| 229 |
List **retrieved_attrs); |
| 230 |
extern void deparseAnalyzeSizeSql(StringInfo buf, Relation rel); |
| 231 |
extern void deparseAnalyzeInfoSql(StringInfo buf, Relation rel); |
| 232 |
extern void deparseAnalyzeSql(StringInfo buf, Relation rel, |
| 233 |
PgFdwSamplingMethod sample_method, |
| 234 |
double sample_frac, |
| 235 |
List **retrieved_attrs); |
| 236 |
extern void deparseTruncateSql(StringInfo buf, |
| 237 |
List *rels, |
| 238 |
DropBehavior behavior, |
| 239 |
bool restart_seqs); |
| 240 |
extern void deparseStringLiteral(StringInfo buf, const char *val); |
| 241 |
extern EquivalenceMember *find_em_for_rel(PlannerInfo *root, |
| 242 |
EquivalenceClass *ec, |
| 243 |
RelOptInfo *rel); |
| 244 |
extern EquivalenceMember *find_em_for_rel_target(PlannerInfo *root, |
| 245 |
EquivalenceClass *ec, |
| 246 |
RelOptInfo *rel); |
| 247 |
extern List *build_tlist_to_deparse(RelOptInfo *foreignrel); |
| 248 |
extern void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, |
| 249 |
RelOptInfo *rel, List *tlist, |
| 250 |
List *remote_conds, List *pathkeys, |
| 251 |
bool has_final_sort, bool has_limit, |
| 252 |
bool is_subquery, |
| 253 |
List **retrieved_attrs, List **params_list); |
| 254 |
extern const char *get_jointype_name(JoinType jointype); |
| 255 |
|
| 256 |
/* in shippable.c */ |
| 257 |
extern bool is_builtin(Oid objectId); |
| 258 |
extern bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo); |
| 259 |
|
| 260 |
#endif /* POSTGRES_FDW_H */ |