PostgreSQL Source Code  git master
deparse.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/plannodes.h"
#include "optimizer/optimizer.h"
#include "optimizer/prep.h"
#include "optimizer/tlist.h"
#include "parser/parsetree.h"
#include "postgres_fdw.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "commands/tablecmds.h"
Include dependency graph for deparse.c:

Go to the source code of this file.

Data Structures

struct  foreign_glob_cxt
 
struct  foreign_loc_cxt
 
struct  deparse_expr_cxt
 

Macros

#define REL_ALIAS_PREFIX   "r"
 
#define ADD_REL_QUALIFIER(buf, varno)   appendStringInfo((buf), "%s%d.", REL_ALIAS_PREFIX, (varno))
 
#define SUBQUERY_REL_ALIAS_PREFIX   "s"
 
#define SUBQUERY_COL_ALIAS_PREFIX   "c"
 

Typedefs

typedef struct foreign_glob_cxt foreign_glob_cxt
 
typedef struct foreign_loc_cxt foreign_loc_cxt
 
typedef struct deparse_expr_cxt deparse_expr_cxt
 

Enumerations

enum  FDWCollateState { FDW_COLLATE_NONE, FDW_COLLATE_SAFE, FDW_COLLATE_UNSAFE }
 

Functions

static bool foreign_expr_walker (Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt)
 
static char * deparse_type_name (Oid type_oid, int32 typemod)
 
static void deparseTargetList (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
 
static void deparseExplicitTargetList (List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
 
static void deparseSubqueryTargetList (deparse_expr_cxt *context)
 
static void deparseReturningList (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
 
static void deparseColumnRef (StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
 
static void deparseRelation (StringInfo buf, Relation rel)
 
static void deparseExpr (Expr *expr, deparse_expr_cxt *context)
 
static void deparseVar (Var *node, deparse_expr_cxt *context)
 
static void deparseConst (Const *node, deparse_expr_cxt *context, int showtype)
 
static void deparseParam (Param *node, deparse_expr_cxt *context)
 
static void deparseSubscriptingRef (SubscriptingRef *node, deparse_expr_cxt *context)
 
static void deparseFuncExpr (FuncExpr *node, deparse_expr_cxt *context)
 
static void deparseOpExpr (OpExpr *node, deparse_expr_cxt *context)
 
static void deparseOperatorName (StringInfo buf, Form_pg_operator opform)
 
static void deparseDistinctExpr (DistinctExpr *node, deparse_expr_cxt *context)
 
static void deparseScalarArrayOpExpr (ScalarArrayOpExpr *node, deparse_expr_cxt *context)
 
static void deparseRelabelType (RelabelType *node, deparse_expr_cxt *context)
 
static void deparseBoolExpr (BoolExpr *node, deparse_expr_cxt *context)
 
static void deparseNullTest (NullTest *node, deparse_expr_cxt *context)
 
static void deparseArrayExpr (ArrayExpr *node, deparse_expr_cxt *context)
 
static void printRemoteParam (int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
 
static void printRemotePlaceholder (Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
 
static void deparseSelectSql (List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
 
static void deparseLockingClause (deparse_expr_cxt *context)
 
static void appendOrderByClause (List *pathkeys, bool has_final_sort, deparse_expr_cxt *context)
 
static void appendLimitClause (deparse_expr_cxt *context)
 
static void appendConditions (List *exprs, deparse_expr_cxt *context)
 
static void deparseFromExprForRel (StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
 
static void deparseFromExpr (List *quals, deparse_expr_cxt *context)
 
static void deparseRangeTblRef (StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, Index ignore_rel, List **ignore_conds, List **params_list)
 
static void deparseAggref (Aggref *node, deparse_expr_cxt *context)
 
static void appendGroupByClause (List *tlist, deparse_expr_cxt *context)
 
static void appendAggOrderBy (List *orderList, List *targetList, deparse_expr_cxt *context)
 
static void appendFunctionName (Oid funcid, deparse_expr_cxt *context)
 
static NodedeparseSortGroupClause (Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
 
static bool is_subquery_var (Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
 
static void get_relation_column_alias_ids (Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
 
void classifyConditions (PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds)
 
bool is_foreign_expr (PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
 
bool is_foreign_param (PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
 
Listbuild_tlist_to_deparse (RelOptInfo *foreignrel)
 
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)
 
const char * get_jointype_name (JoinType jointype)
 
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)
 
void rebuildInsertSql (StringInfo buf, char *orig_query, int values_end_len, int num_cols, int num_rows)
 
void deparseUpdateSql (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
 
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)
 
void deparseDeleteSql (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs)
 
void deparseDirectDeleteSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
 
void deparseAnalyzeSizeSql (StringInfo buf, Relation rel)
 
void deparseAnalyzeSql (StringInfo buf, Relation rel, List **retrieved_attrs)
 
void deparseTruncateSql (StringInfo buf, List *rels, DropBehavior behavior, bool restart_seqs)
 
void deparseStringLiteral (StringInfo buf, const char *val)
 

Macro Definition Documentation

◆ ADD_REL_QUALIFIER

#define ADD_REL_QUALIFIER (   buf,
  varno 
)    appendStringInfo((buf), "%s%d.", REL_ALIAS_PREFIX, (varno))

Definition at line 108 of file deparse.c.

Referenced by deparseColumnRef(), and deparseTargetList().

◆ REL_ALIAS_PREFIX

#define REL_ALIAS_PREFIX   "r"

◆ SUBQUERY_COL_ALIAS_PREFIX

#define SUBQUERY_COL_ALIAS_PREFIX   "c"

Definition at line 111 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

◆ SUBQUERY_REL_ALIAS_PREFIX

#define SUBQUERY_REL_ALIAS_PREFIX   "s"

Definition at line 110 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

Typedef Documentation

◆ deparse_expr_cxt

◆ foreign_glob_cxt

◆ foreign_loc_cxt

Enumeration Type Documentation

◆ FDWCollateState

Enumerator
FDW_COLLATE_NONE 
FDW_COLLATE_SAFE 
FDW_COLLATE_UNSAFE 

Definition at line 76 of file deparse.c.

77 {
78  FDW_COLLATE_NONE, /* expression is of a noncollatable type, or
79  * it has default collation that is not
80  * traceable to a foreign Var */
81  FDW_COLLATE_SAFE, /* collation derives from a foreign Var */
82  FDW_COLLATE_UNSAFE /* collation is non-default and derives from
83  * something other than a foreign Var */
FDWCollateState
Definition: deparse.c:76

Function Documentation

◆ appendAggOrderBy()

static void appendAggOrderBy ( List orderList,
List targetList,
deparse_expr_cxt context 
)
static

Definition at line 3132 of file deparse.c.

References appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseOperatorName(), deparseSortGroupClause(), elog, ERROR, exprType(), GETSTRUCT, TypeCacheEntry::gt_opr, HeapTupleIsValid, lfirst, lookup_type_cache(), TypeCacheEntry::lt_opr, SortGroupClause::nulls_first, ObjectIdGetDatum, OPEROID, ReleaseSysCache(), SearchSysCache1(), SortGroupClause::sortop, SortGroupClause::tleSortGroupRef, TYPECACHE_GT_OPR, and TYPECACHE_LT_OPR.

Referenced by deparseAggref().

3133 {
3134  StringInfo buf = context->buf;
3135  ListCell *lc;
3136  bool first = true;
3137 
3138  foreach(lc, orderList)
3139  {
3140  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
3141  Node *sortexpr;
3142  Oid sortcoltype;
3143  TypeCacheEntry *typentry;
3144 
3145  if (!first)
3146  appendStringInfoString(buf, ", ");
3147  first = false;
3148 
3149  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
3150  false, context);
3151  sortcoltype = exprType(sortexpr);
3152  /* See whether operator is default < or > for datatype */
3153  typentry = lookup_type_cache(sortcoltype,
3155  if (srt->sortop == typentry->lt_opr)
3156  appendStringInfoString(buf, " ASC");
3157  else if (srt->sortop == typentry->gt_opr)
3158  appendStringInfoString(buf, " DESC");
3159  else
3160  {
3161  HeapTuple opertup;
3162  Form_pg_operator operform;
3163 
3164  appendStringInfoString(buf, " USING ");
3165 
3166  /* Append operator name. */
3167  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
3168  if (!HeapTupleIsValid(opertup))
3169  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
3170  operform = (Form_pg_operator) GETSTRUCT(opertup);
3171  deparseOperatorName(buf, operform);
3172  ReleaseSysCache(opertup);
3173  }
3174 
3175  if (srt->nulls_first)
3176  appendStringInfoString(buf, " NULLS FIRST");
3177  else
3178  appendStringInfoString(buf, " NULLS LAST");
3179  }
3180 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3384
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Index tleSortGroupRef
Definition: parsenodes.h:1283
Definition: nodes.h:539
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2839
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define TYPECACHE_GT_OPR
Definition: typcache.h:138
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:338
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
#define elog(elevel,...)
Definition: elog.h:232
#define TYPECACHE_LT_OPR
Definition: typcache.h:137

◆ appendConditions()

static void appendConditions ( List exprs,
deparse_expr_cxt context 
)
static

Definition at line 1341 of file deparse.c.

References appendStringInfoChar(), appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseExpr(), IsA, lfirst, reset_transmission_modes(), and set_transmission_modes().

Referenced by deparseDirectDeleteSql(), deparseDirectUpdateSql(), deparseFromExpr(), deparseFromExprForRel(), and deparseSelectStmtForRel().

1342 {
1343  int nestlevel;
1344  ListCell *lc;
1345  bool is_first = true;
1346  StringInfo buf = context->buf;
1347 
1348  /* Make sure any constants in the exprs are printed portably */
1349  nestlevel = set_transmission_modes();
1350 
1351  foreach(lc, exprs)
1352  {
1353  Expr *expr = (Expr *) lfirst(lc);
1354 
1355  /* Extract clause from RestrictInfo, if required */
1356  if (IsA(expr, RestrictInfo))
1357  expr = ((RestrictInfo *) expr)->clause;
1358 
1359  /* Connect expressions with "AND" and parenthesize each condition. */
1360  if (!is_first)
1361  appendStringInfoString(buf, " AND ");
1362 
1363  appendStringInfoChar(buf, '(');
1364  deparseExpr(expr, context);
1365  appendStringInfoChar(buf, ')');
1366 
1367  is_first = false;
1368  }
1369 
1370  reset_transmission_modes(nestlevel);
1371 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419

◆ appendFunctionName()

static void appendFunctionName ( Oid  funcid,
deparse_expr_cxt context 
)
static

Definition at line 3349 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), buf, deparse_expr_cxt::buf, elog, ERROR, get_namespace_name(), GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, PROCOID, proname, quote_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by deparseAggref(), and deparseFuncExpr().

3350 {
3351  StringInfo buf = context->buf;
3352  HeapTuple proctup;
3353  Form_pg_proc procform;
3354  const char *proname;
3355 
3356  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3357  if (!HeapTupleIsValid(proctup))
3358  elog(ERROR, "cache lookup failed for function %u", funcid);
3359  procform = (Form_pg_proc) GETSTRUCT(proctup);
3360 
3361  /* Print schema name only if it's not pg_catalog */
3362  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3363  {
3364  const char *schemaname;
3365 
3366  schemaname = get_namespace_name(procform->pronamespace);
3367  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3368  }
3369 
3370  /* Always print the function name */
3371  proname = NameStr(procform->proname);
3373 
3374  ReleaseSysCache(proctup);
3375 }
NameData proname
Definition: pg_proc.h:35
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11324
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
static char * buf
Definition: pg_test_fsync.c:68
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681

◆ appendGroupByClause()

static void appendGroupByClause ( List tlist,
deparse_expr_cxt context 
)
static

Definition at line 3230 of file deparse.c.

References appendStringInfoString(), Assert, buf, deparse_expr_cxt::buf, deparseSortGroupClause(), Query::groupClause, Query::groupingSets, lfirst, PlannerInfo::parse, deparse_expr_cxt::root, and SortGroupClause::tleSortGroupRef.

Referenced by deparseSelectStmtForRel().

3231 {
3232  StringInfo buf = context->buf;
3233  Query *query = context->root->parse;
3234  ListCell *lc;
3235  bool first = true;
3236 
3237  /* Nothing to be done, if there's no GROUP BY clause in the query. */
3238  if (!query->groupClause)
3239  return;
3240 
3241  appendStringInfoString(buf, " GROUP BY ");
3242 
3243  /*
3244  * Queries with grouping sets are not pushed down, so we don't expect
3245  * grouping sets here.
3246  */
3247  Assert(!query->groupingSets);
3248 
3249  foreach(lc, query->groupClause)
3250  {
3251  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
3252 
3253  if (!first)
3254  appendStringInfoString(buf, ", ");
3255  first = false;
3256 
3257  deparseSortGroupClause(grp->tleSortGroupRef, tlist, true, context);
3258  }
3259 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3384
Query * parse
Definition: pathnodes.h:161
Index tleSortGroupRef
Definition: parsenodes.h:1283
List * groupingSets
Definition: parsenodes.h:161
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * groupClause
Definition: parsenodes.h:158

◆ appendLimitClause()

static void appendLimitClause ( deparse_expr_cxt context)
static

Definition at line 3321 of file deparse.c.

References appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseExpr(), Query::limitCount, Query::limitOffset, PlannerInfo::parse, reset_transmission_modes(), foreign_glob_cxt::root, deparse_expr_cxt::root, and set_transmission_modes().

Referenced by deparseSelectStmtForRel().

3322 {
3323  PlannerInfo *root = context->root;
3324  StringInfo buf = context->buf;
3325  int nestlevel;
3326 
3327  /* Make sure any constants in the exprs are printed portably */
3328  nestlevel = set_transmission_modes();
3329 
3330  if (root->parse->limitCount)
3331  {
3332  appendStringInfoString(buf, " LIMIT ");
3333  deparseExpr((Expr *) root->parse->limitCount, context);
3334  }
3335  if (root->parse->limitOffset)
3336  {
3337  appendStringInfoString(buf, " OFFSET ");
3338  deparseExpr((Expr *) root->parse->limitOffset, context);
3339  }
3340 
3341  reset_transmission_modes(nestlevel);
3342 }
Node * limitOffset
Definition: parsenodes.h:171
Query * parse
Definition: pathnodes.h:161
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Node * limitCount
Definition: parsenodes.h:172
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
PlannerInfo * root
Definition: deparse.c:97
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419

◆ appendOrderByClause()

static void appendOrderByClause ( List pathkeys,
bool  has_final_sort,
deparse_expr_cxt context 
)
static

Definition at line 3267 of file deparse.c.

References appendStringInfoString(), Assert, BTLessStrategyNumber, buf, deparse_expr_cxt::buf, deparseExpr(), find_em_expr_for_input_target(), find_em_expr_for_rel(), deparse_expr_cxt::foreignrel, lfirst, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_strategy, RelOptInfo::reltarget, reset_transmission_modes(), deparse_expr_cxt::root, deparse_expr_cxt::scanrel, and set_transmission_modes().

Referenced by deparseSelectStmtForRel().

3269 {
3270  ListCell *lcell;
3271  int nestlevel;
3272  char *delim = " ";
3273  RelOptInfo *baserel = context->scanrel;
3274  StringInfo buf = context->buf;
3275 
3276  /* Make sure any constants in the exprs are printed portably */
3277  nestlevel = set_transmission_modes();
3278 
3279  appendStringInfoString(buf, " ORDER BY");
3280  foreach(lcell, pathkeys)
3281  {
3282  PathKey *pathkey = lfirst(lcell);
3283  Expr *em_expr;
3284 
3285  if (has_final_sort)
3286  {
3287  /*
3288  * By construction, context->foreignrel is the input relation to
3289  * the final sort.
3290  */
3291  em_expr = find_em_expr_for_input_target(context->root,
3292  pathkey->pk_eclass,
3293  context->foreignrel->reltarget);
3294  }
3295  else
3296  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
3297 
3298  Assert(em_expr != NULL);
3299 
3300  appendStringInfoString(buf, delim);
3301  deparseExpr(em_expr, context);
3302  if (pathkey->pk_strategy == BTLessStrategyNumber)
3303  appendStringInfoString(buf, " ASC");
3304  else
3305  appendStringInfoString(buf, " DESC");
3306 
3307  if (pathkey->pk_nulls_first)
3308  appendStringInfoString(buf, " NULLS FIRST");
3309  else
3310  appendStringInfoString(buf, " NULLS LAST");
3311 
3312  delim = ", ";
3313  }
3314  reset_transmission_modes(nestlevel);
3315 }
int set_transmission_modes(void)
int pk_strategy
Definition: pathnodes.h:1060
Expr * find_em_expr_for_input_target(PlannerInfo *root, EquivalenceClass *ec, PathTarget *target)
bool pk_nulls_first
Definition: pathnodes.h:1061
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
Definition: equivclass.c:939
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1058
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
RelOptInfo * scanrel
Definition: deparse.c:99
#define BTLessStrategyNumber
Definition: stratnum.h:29
RelOptInfo * foreignrel
Definition: deparse.c:98
struct PathTarget * reltarget
Definition: pathnodes.h:687

◆ build_tlist_to_deparse()

List* build_tlist_to_deparse ( RelOptInfo foreignrel)

Definition at line 944 of file deparse.c.

References add_to_flat_tlist(), RestrictInfo::clause, PathTarget::exprs, RelOptInfo::fdw_private, PgFdwRelationInfo::grouped_tlist, IS_UPPER_REL, lfirst_node, PgFdwRelationInfo::local_conds, NIL, pull_var_clause(), PVC_RECURSE_PLACEHOLDERS, and RelOptInfo::reltarget.

Referenced by estimate_path_cost_size(), and postgresGetForeignPlan().

945 {
946  List *tlist = NIL;
947  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
948  ListCell *lc;
949 
950  /*
951  * For an upper relation, we have already built the target list while
952  * checking shippability, so just return that.
953  */
954  if (IS_UPPER_REL(foreignrel))
955  return fpinfo->grouped_tlist;
956 
957  /*
958  * We require columns specified in foreignrel->reltarget->exprs and those
959  * required for evaluating the local conditions.
960  */
961  tlist = add_to_flat_tlist(tlist,
962  pull_var_clause((Node *) foreignrel->reltarget->exprs,
964  foreach(lc, fpinfo->local_conds)
965  {
966  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
967 
968  tlist = add_to_flat_tlist(tlist,
969  pull_var_clause((Node *) rinfo->clause,
971  }
972 
973  return tlist;
974 }
#define NIL
Definition: pg_list.h:65
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:191
Definition: nodes.h:539
List * pull_var_clause(Node *node, int flags)
Definition: var.c:562
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Expr * clause
Definition: pathnodes.h:2045
List * exprs
Definition: pathnodes.h:1102
void * fdw_private
Definition: pathnodes.h:732
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:121
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:687

◆ classifyConditions()

void classifyConditions ( PlannerInfo root,
RelOptInfo baserel,
List input_conds,
List **  remote_conds,
List **  local_conds 
)

Definition at line 205 of file deparse.c.

References RestrictInfo::clause, is_foreign_expr(), lappend(), lfirst_node, and NIL.

Referenced by estimate_path_cost_size(), and postgresGetForeignRelSize().

210 {
211  ListCell *lc;
212 
213  *remote_conds = NIL;
214  *local_conds = NIL;
215 
216  foreach(lc, input_conds)
217  {
219 
220  if (is_foreign_expr(root, baserel, ri->clause))
221  *remote_conds = lappend(*remote_conds, ri);
222  else
223  *local_conds = lappend(*local_conds, ri);
224  }
225 }
#define NIL
Definition: pg_list.h:65
#define lfirst_node(type, lc)
Definition: pg_list.h:172
List * lappend(List *list, void *datum)
Definition: list.c:336
Expr * clause
Definition: pathnodes.h:2045
bool is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:231

◆ deparse_type_name()

static char * deparse_type_name ( Oid  type_oid,
int32  typemod 
)
static

Definition at line 925 of file deparse.c.

References format_type_extended(), FORMAT_TYPE_FORCE_QUALIFY, FORMAT_TYPE_TYPEMOD_GIVEN, and is_builtin().

Referenced by deparseArrayExpr(), deparseConst(), deparseFuncExpr(), deparseRelabelType(), printRemoteParam(), and printRemotePlaceholder().

926 {
928 
929  if (!is_builtin(type_oid))
930  flags |= FORMAT_TYPE_FORCE_QUALIFY;
931 
932  return format_type_extended(type_oid, typemod, flags);
933 }
bool is_builtin(Oid objectId)
Definition: shippable.c:152
uint16 bits16
Definition: c.h:449
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:108
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:110
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:112

◆ deparseAggref()

static void deparseAggref ( Aggref node,
deparse_expr_cxt context 
)
static

Definition at line 3040 of file deparse.c.

References Aggref::aggdirectargs, Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggfnoid, Aggref::aggkind, Aggref::aggorder, Aggref::aggsplit, AGGSPLIT_SIMPLE, Aggref::aggstar, Aggref::aggvariadic, appendAggOrderBy(), appendFunctionName(), appendStringInfoChar(), appendStringInfoString(), arg, Aggref::args, Assert, buf, deparse_expr_cxt::buf, deparseExpr(), TargetEntry::expr, lfirst, lnext(), NIL, and TargetEntry::resjunk.

Referenced by deparseExpr().

3041 {
3042  StringInfo buf = context->buf;
3043  bool use_variadic;
3044 
3045  /* Only basic, non-split aggregation accepted. */
3046  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
3047 
3048  /* Check if need to print VARIADIC (cf. ruleutils.c) */
3049  use_variadic = node->aggvariadic;
3050 
3051  /* Find aggregate name from aggfnoid which is a pg_proc entry */
3052  appendFunctionName(node->aggfnoid, context);
3053  appendStringInfoChar(buf, '(');
3054 
3055  /* Add DISTINCT */
3056  appendStringInfoString(buf, (node->aggdistinct != NIL) ? "DISTINCT " : "");
3057 
3058  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
3059  {
3060  /* Add WITHIN GROUP (ORDER BY ..) */
3061  ListCell *arg;
3062  bool first = true;
3063 
3064  Assert(!node->aggvariadic);
3065  Assert(node->aggorder != NIL);
3066 
3067  foreach(arg, node->aggdirectargs)
3068  {
3069  if (!first)
3070  appendStringInfoString(buf, ", ");
3071  first = false;
3072 
3073  deparseExpr((Expr *) lfirst(arg), context);
3074  }
3075 
3076  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
3077  appendAggOrderBy(node->aggorder, node->args, context);
3078  }
3079  else
3080  {
3081  /* aggstar can be set only in zero-argument aggregates */
3082  if (node->aggstar)
3083  appendStringInfoChar(buf, '*');
3084  else
3085  {
3086  ListCell *arg;
3087  bool first = true;
3088 
3089  /* Add all the arguments */
3090  foreach(arg, node->args)
3091  {
3092  TargetEntry *tle = (TargetEntry *) lfirst(arg);
3093  Node *n = (Node *) tle->expr;
3094 
3095  if (tle->resjunk)
3096  continue;
3097 
3098  if (!first)
3099  appendStringInfoString(buf, ", ");
3100  first = false;
3101 
3102  /* Add VARIADIC */
3103  if (use_variadic && lnext(node->args, arg) == NULL)
3104  appendStringInfoString(buf, "VARIADIC ");
3105 
3106  deparseExpr((Expr *) n, context);
3107  }
3108  }
3109 
3110  /* Add ORDER BY */
3111  if (node->aggorder != NIL)
3112  {
3113  appendStringInfoString(buf, " ORDER BY ");
3114  appendAggOrderBy(node->aggorder, node->args, context);
3115  }
3116  }
3117 
3118  /* Add FILTER (WHERE ..) */
3119  if (node->aggfilter != NULL)
3120  {
3121  appendStringInfoString(buf, ") FILTER (WHERE ");
3122  deparseExpr((Expr *) node->aggfilter, context);
3123  }
3124 
3125  appendStringInfoChar(buf, ')');
3126 }
List * aggdistinct
Definition: primnodes.h:332
#define NIL
Definition: pg_list.h:65
bool aggvariadic
Definition: primnodes.h:335
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3349
Definition: nodes.h:539
List * args
Definition: primnodes.h:330
bool aggstar
Definition: primnodes.h:334
bool resjunk
Definition: primnodes.h:1451
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * aggorder
Definition: primnodes.h:331
List * aggdirectargs
Definition: primnodes.h:329
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
Definition: deparse.c:3132
StringInfo buf
Definition: deparse.c:102
Oid aggfnoid
Definition: primnodes.h:323
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
AggSplit aggsplit
Definition: primnodes.h:339
Expr * aggfilter
Definition: primnodes.h:333
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
void * arg
char aggkind
Definition: primnodes.h:337

◆ deparseAnalyzeSizeSql()

void deparseAnalyzeSizeSql ( StringInfo  buf,
Relation  rel 
)

Definition at line 2103 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, deparseRelation(), deparseStringLiteral(), initStringInfo(), and relname.

Referenced by postgresAnalyzeForeignTable().

2104 {
2106 
2107  /* We'll need the remote relation name as a literal. */
2108  initStringInfo(&relname);
2109  deparseRelation(&relname, rel);
2110 
2111  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
2112  deparseStringLiteral(buf, relname.data);
2113  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
2114 }
NameData relname
Definition: pg_class.h:38
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2384
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344

◆ deparseAnalyzeSql()

void deparseAnalyzeSql ( StringInfo  buf,
Relation  rel,
List **  retrieved_attrs 
)

Definition at line 2123 of file deparse.c.

References appendStringInfoString(), attname, defGetString(), DefElem::defname, deparseRelation(), GetForeignColumnOptions(), i, lappend_int(), lfirst, NameStr, TupleDescData::natts, NIL, options, quote_identifier(), RelationGetDescr, RelationGetRelid, and TupleDescAttr.

Referenced by postgresAcquireSampleRowsFunc().

2124 {
2125  Oid relid = RelationGetRelid(rel);
2126  TupleDesc tupdesc = RelationGetDescr(rel);
2127  int i;
2128  char *colname;
2129  List *options;
2130  ListCell *lc;
2131  bool first = true;
2132 
2133  *retrieved_attrs = NIL;
2134 
2135  appendStringInfoString(buf, "SELECT ");
2136  for (i = 0; i < tupdesc->natts; i++)
2137  {
2138  /* Ignore dropped columns. */
2139  if (TupleDescAttr(tupdesc, i)->attisdropped)
2140  continue;
2141 
2142  if (!first)
2143  appendStringInfoString(buf, ", ");
2144  first = false;
2145 
2146  /* Use attribute name or column_name option. */
2147  colname = NameStr(TupleDescAttr(tupdesc, i)->attname);
2148  options = GetForeignColumnOptions(relid, i + 1);
2149 
2150  foreach(lc, options)
2151  {
2152  DefElem *def = (DefElem *) lfirst(lc);
2153 
2154  if (strcmp(def->defname, "column_name") == 0)
2155  {
2156  colname = defGetString(def);
2157  break;
2158  }
2159  }
2160 
2162 
2163  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
2164  }
2165 
2166  /* Don't generate bad syntax for zero-column relation. */
2167  if (first)
2168  appendStringInfoString(buf, "NULL");
2169 
2170  /*
2171  * Construct FROM clause
2172  */
2173  appendStringInfoString(buf, " FROM ");
2174  deparseRelation(buf, rel);
2175 }
#define NIL
Definition: pg_list.h:65
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11324
#define RelationGetDescr(relation)
Definition: rel.h:495
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
unsigned int Oid
Definition: postgres_ext.h:31
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:286
NameData attname
Definition: pg_attribute.h:41
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
List * lappend_int(List *list, int datum)
Definition: list.c:354
static char ** options
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
#define lfirst(lc)
Definition: pg_list.h:169
int i
#define NameStr(name)
Definition: c.h:681
char * defname
Definition: parsenodes.h:746
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:469

◆ deparseArrayExpr()

static void deparseArrayExpr ( ArrayExpr node,
deparse_expr_cxt context 
)
static

Definition at line 3014 of file deparse.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), ArrayExpr::array_typeid, buf, deparse_expr_cxt::buf, deparse_type_name(), deparseExpr(), ArrayExpr::elements, lfirst, and NIL.

Referenced by deparseExpr().

3015 {
3016  StringInfo buf = context->buf;
3017  bool first = true;
3018  ListCell *lc;
3019 
3020  appendStringInfoString(buf, "ARRAY[");
3021  foreach(lc, node->elements)
3022  {
3023  if (!first)
3024  appendStringInfoString(buf, ", ");
3025  deparseExpr(lfirst(lc), context);
3026  first = false;
3027  }
3028  appendStringInfoChar(buf, ']');
3029 
3030  /* If the array is empty, we need an explicit cast to the array type. */
3031  if (node->elements == NIL)
3032  appendStringInfo(buf, "::%s",
3033  deparse_type_name(node->array_typeid, -1));
3034 }
#define NIL
Definition: pg_list.h:65
Oid array_typeid
Definition: primnodes.h:1024
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * elements
Definition: primnodes.h:1027
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:925
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419

◆ deparseBoolExpr()

static void deparseBoolExpr ( BoolExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2943 of file deparse.c.

References AND_EXPR, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), BoolExpr::args, BoolExpr::boolop, buf, deparse_expr_cxt::buf, deparseExpr(), lfirst, linitial, NOT_EXPR, and OR_EXPR.

Referenced by deparseExpr().

2944 {
2945  StringInfo buf = context->buf;
2946  const char *op = NULL; /* keep compiler quiet */
2947  bool first;
2948  ListCell *lc;
2949 
2950  switch (node->boolop)
2951  {
2952  case AND_EXPR:
2953  op = "AND";
2954  break;
2955  case OR_EXPR:
2956  op = "OR";
2957  break;
2958  case NOT_EXPR:
2959  appendStringInfoString(buf, "(NOT ");
2960  deparseExpr(linitial(node->args), context);
2961  appendStringInfoChar(buf, ')');
2962  return;
2963  }
2964 
2965  appendStringInfoChar(buf, '(');
2966  first = true;
2967  foreach(lc, node->args)
2968  {
2969  if (!first)
2970  appendStringInfo(buf, " %s ", op);
2971  deparseExpr((Expr *) lfirst(lc), context);
2972  first = false;
2973  }
2974  appendStringInfoChar(buf, ')');
2975 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
BoolExprType boolop
Definition: primnodes.h:615
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:616
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419

◆ deparseColumnRef()

static void deparseColumnRef ( StringInfo  buf,
int  varno,
int  varattno,
RangeTblEntry rte,
bool  qualify_col 
)
static

Definition at line 2216 of file deparse.c.

References ADD_REL_QUALIFIER, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, bms_add_member(), bms_free(), defGetString(), DefElem::defname, deparseTargetList(), FirstLowInvalidHeapAttributeNumber, get_attname(), GetForeignColumnOptions(), IS_SPECIAL_VARNO, lfirst, NoLock, options, quote_identifier(), RangeTblEntry::relid, SelfItemPointerAttributeNumber, table_close(), table_open(), and TableOidAttributeNumber.

Referenced by deparseDirectUpdateSql(), deparseInsertSql(), deparseTargetList(), deparseUpdateSql(), and deparseVar().

2218 {
2219  /* We support fetching the remote side's CTID and OID. */
2220  if (varattno == SelfItemPointerAttributeNumber)
2221  {
2222  if (qualify_col)
2223  ADD_REL_QUALIFIER(buf, varno);
2224  appendStringInfoString(buf, "ctid");
2225  }
2226  else if (varattno < 0)
2227  {
2228  /*
2229  * All other system attributes are fetched as 0, except for table OID,
2230  * which is fetched as the local table OID. However, we must be
2231  * careful; the table could be beneath an outer join, in which case it
2232  * must go to NULL whenever the rest of the row does.
2233  */
2234  Oid fetchval = 0;
2235 
2236  if (varattno == TableOidAttributeNumber)
2237  fetchval = rte->relid;
2238 
2239  if (qualify_col)
2240  {
2241  appendStringInfoString(buf, "CASE WHEN (");
2242  ADD_REL_QUALIFIER(buf, varno);
2243  appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval);
2244  }
2245  else
2246  appendStringInfo(buf, "%u", fetchval);
2247  }
2248  else if (varattno == 0)
2249  {
2250  /* Whole row reference */
2251  Relation rel;
2252  Bitmapset *attrs_used;
2253 
2254  /* Required only to be passed down to deparseTargetList(). */
2255  List *retrieved_attrs;
2256 
2257  /*
2258  * The lock on the relation will be held by upper callers, so it's
2259  * fine to open it with no lock here.
2260  */
2261  rel = table_open(rte->relid, NoLock);
2262 
2263  /*
2264  * The local name of the foreign table can not be recognized by the
2265  * foreign server and the table it references on foreign server might
2266  * have different column ordering or different columns than those
2267  * declared locally. Hence we have to deparse whole-row reference as
2268  * ROW(columns referenced locally). Construct this by deparsing a
2269  * "whole row" attribute.
2270  */
2271  attrs_used = bms_add_member(NULL,
2273 
2274  /*
2275  * In case the whole-row reference is under an outer join then it has
2276  * to go NULL whenever the rest of the row goes NULL. Deparsing a join
2277  * query would always involve multiple relations, thus qualify_col
2278  * would be true.
2279  */
2280  if (qualify_col)
2281  {
2282  appendStringInfoString(buf, "CASE WHEN (");
2283  ADD_REL_QUALIFIER(buf, varno);
2284  appendStringInfoString(buf, "*)::text IS NOT NULL THEN ");
2285  }
2286 
2287  appendStringInfoString(buf, "ROW(");
2288  deparseTargetList(buf, rte, varno, rel, false, attrs_used, qualify_col,
2289  &retrieved_attrs);
2290  appendStringInfoChar(buf, ')');
2291 
2292  /* Complete the CASE WHEN statement started above. */
2293  if (qualify_col)
2294  appendStringInfoString(buf, " END");
2295 
2296  table_close(rel, NoLock);
2297  bms_free(attrs_used);
2298  }
2299  else
2300  {
2301  char *colname = NULL;
2302  List *options;
2303  ListCell *lc;
2304 
2305  /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
2306  Assert(!IS_SPECIAL_VARNO(varno));
2307 
2308  /*
2309  * If it's a column of a foreign table, and it has the column_name FDW
2310  * option, use that value.
2311  */
2312  options = GetForeignColumnOptions(rte->relid, varattno);
2313  foreach(lc, options)
2314  {
2315  DefElem *def = (DefElem *) lfirst(lc);
2316 
2317  if (strcmp(def->defname, "column_name") == 0)
2318  {
2319  colname = defGetString(def);
2320  break;
2321  }
2322  }
2323 
2324  /*
2325  * If it's a column of a regular table or it doesn't have column_name
2326  * FDW option, use attribute name.
2327  */
2328  if (colname == NULL)
2329  colname = get_attname(rte->relid, varattno, false);
2330 
2331  if (qualify_col)
2332  ADD_REL_QUALIFIER(buf, varno);
2333 
2335  }
2336 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11324
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
unsigned int Oid
Definition: postgres_ext.h:31
static void deparseTargetList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1175
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:286
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:180
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:108
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
#define TableOidAttributeNumber
Definition: sysattr.h:26
static char ** options
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
char * defname
Definition: parsenodes.h:746
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50

◆ deparseConst()

static void deparseConst ( Const node,
deparse_expr_cxt context,
int  showtype 
)
static

Definition at line 2547 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), buf, deparse_expr_cxt::buf, Const::constisnull, Const::consttype, Const::consttypmod, Const::constvalue, deparse_type_name(), deparseStringLiteral(), getTypeOutputInfo(), OidOutputFunctionCall(), and pfree().

Referenced by deparseExpr(), and deparseSortGroupClause().

2548 {
2549  StringInfo buf = context->buf;
2550  Oid typoutput;
2551  bool typIsVarlena;
2552  char *extval;
2553  bool isfloat = false;
2554  bool needlabel;
2555 
2556  if (node->constisnull)
2557  {
2558  appendStringInfoString(buf, "NULL");
2559  if (showtype >= 0)
2560  appendStringInfo(buf, "::%s",
2562  node->consttypmod));
2563  return;
2564  }
2565 
2567  &typoutput, &typIsVarlena);
2568  extval = OidOutputFunctionCall(typoutput, node->constvalue);
2569 
2570  switch (node->consttype)
2571  {
2572  case INT2OID:
2573  case INT4OID:
2574  case INT8OID:
2575  case OIDOID:
2576  case FLOAT4OID:
2577  case FLOAT8OID:
2578  case NUMERICOID:
2579  {
2580  /*
2581  * No need to quote unless it's a special value such as 'NaN'.
2582  * See comments in get_const_expr().
2583  */
2584  if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2585  {
2586  if (extval[0] == '+' || extval[0] == '-')
2587  appendStringInfo(buf, "(%s)", extval);
2588  else
2589  appendStringInfoString(buf, extval);
2590  if (strcspn(extval, "eE.") != strlen(extval))
2591  isfloat = true; /* it looks like a float */
2592  }
2593  else
2594  appendStringInfo(buf, "'%s'", extval);
2595  }
2596  break;
2597  case BITOID:
2598  case VARBITOID:
2599  appendStringInfo(buf, "B'%s'", extval);
2600  break;
2601  case BOOLOID:
2602  if (strcmp(extval, "t") == 0)
2603  appendStringInfoString(buf, "true");
2604  else
2605  appendStringInfoString(buf, "false");
2606  break;
2607  default:
2608  deparseStringLiteral(buf, extval);
2609  break;
2610  }
2611 
2612  pfree(extval);
2613 
2614  if (showtype < 0)
2615  return;
2616 
2617  /*
2618  * For showtype == 0, append ::typename unless the constant will be
2619  * implicitly typed as the right type when it is read in.
2620  *
2621  * XXX this code has to be kept in sync with the behavior of the parser,
2622  * especially make_const.
2623  */
2624  switch (node->consttype)
2625  {
2626  case BOOLOID:
2627  case INT4OID:
2628  case UNKNOWNOID:
2629  needlabel = false;
2630  break;
2631  case NUMERICOID:
2632  needlabel = !isfloat || (node->consttypmod >= 0);
2633  break;
2634  default:
2635  needlabel = true;
2636  break;
2637  }
2638  if (needlabel || showtype > 0)
2639  appendStringInfo(buf, "::%s",
2641  node->consttypmod));
2642 }
Datum constvalue
Definition: primnodes.h:219
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
unsigned int Oid
Definition: postgres_ext.h:31
Oid consttype
Definition: primnodes.h:215
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2384
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:925
int32 consttypmod
Definition: primnodes.h:216
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1653
bool constisnull
Definition: primnodes.h:220

◆ deparseDeleteSql()

void deparseDeleteSql ( StringInfo  buf,
RangeTblEntry rte,
Index  rtindex,
Relation  rel,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1966 of file deparse.c.

References appendStringInfoString(), deparseRelation(), deparseReturningList(), NIL, TriggerDesc::trig_delete_after_row, and RelationData::trigdesc.

Referenced by postgresPlanForeignModify().

1970 {
1971  appendStringInfoString(buf, "DELETE FROM ");
1972  deparseRelation(buf, rel);
1973  appendStringInfoString(buf, " WHERE ctid = $1");
1974 
1975  deparseReturningList(buf, rte, rtindex, rel,
1976  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
1977  NIL, returningList, retrieved_attrs);
1978 }
#define NIL
Definition: pg_list.h:65
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2046
TriggerDesc * trigdesc
Definition: rel.h:115
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_delete_after_row
Definition: reltrigger.h:67
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344

◆ deparseDirectDeleteSql()

void deparseDirectDeleteSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
RelOptInfo foreignrel,
List remote_conds,
List **  params_list,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1995 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseExplicitTargetList(), deparseFromExprForRel(), deparseRelation(), deparseReturningList(), foreign_glob_cxt::foreignrel, deparse_expr_cxt::foreignrel, list_concat(), NIL, deparse_expr_cxt::params_list, planner_rt_fetch, REL_ALIAS_PREFIX, RELOPT_JOINREL, RelOptInfo::reloptkind, foreign_glob_cxt::root, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

Referenced by postgresPlanDirectModify().

2002 {
2003  deparse_expr_cxt context;
2004 
2005  /* Set up context struct for recursion */
2006  context.root = root;
2007  context.foreignrel = foreignrel;
2008  context.scanrel = foreignrel;
2009  context.buf = buf;
2010  context.params_list = params_list;
2011 
2012  appendStringInfoString(buf, "DELETE FROM ");
2013  deparseRelation(buf, rel);
2014  if (foreignrel->reloptkind == RELOPT_JOINREL)
2015  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
2016 
2017  if (foreignrel->reloptkind == RELOPT_JOINREL)
2018  {
2019  List *ignore_conds = NIL;
2020 
2021  appendStringInfoString(buf, " USING ");
2022  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
2023  &ignore_conds, params_list);
2024  remote_conds = list_concat(remote_conds, ignore_conds);
2025  }
2026 
2027  if (remote_conds)
2028  {
2029  appendStringInfoString(buf, " WHERE ");
2030  appendConditions(remote_conds, &context);
2031  }
2032 
2033  if (foreignrel->reloptkind == RELOPT_JOINREL)
2034  deparseExplicitTargetList(returningList, true, retrieved_attrs,
2035  &context);
2036  else
2037  deparseReturningList(buf, planner_rt_fetch(rtindex, root),
2038  rtindex, rel, false,
2039  NIL, returningList, retrieved_attrs);
2040 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:673
List ** params_list
Definition: deparse.c:103
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2046
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:383
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1412
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1341
StringInfo buf
Definition: deparse.c:102
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
PlannerInfo * root
Definition: deparse.c:97
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1489
RelOptInfo * scanrel
Definition: deparse.c:99
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseDirectUpdateSql()

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 at line 1881 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), Assert, attnum, buf, deparse_expr_cxt::buf, deparseColumnRef(), deparseExplicitTargetList(), deparseExpr(), deparseFromExprForRel(), deparseRelation(), deparseReturningList(), TargetEntry::expr, forboth, foreign_glob_cxt::foreignrel, deparse_expr_cxt::foreignrel, lfirst_int, lfirst_node, list_concat(), NIL, deparse_expr_cxt::params_list, planner_rt_fetch, REL_ALIAS_PREFIX, RELOPT_JOINREL, RelOptInfo::reloptkind, reset_transmission_modes(), TargetEntry::resjunk, foreign_glob_cxt::root, deparse_expr_cxt::root, deparse_expr_cxt::scanrel, and set_transmission_modes().

Referenced by postgresPlanDirectModify().

1890 {
1891  deparse_expr_cxt context;
1892  int nestlevel;
1893  bool first;
1894  RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
1895  ListCell *lc,
1896  *lc2;
1897 
1898  /* Set up context struct for recursion */
1899  context.root = root;
1900  context.foreignrel = foreignrel;
1901  context.scanrel = foreignrel;
1902  context.buf = buf;
1903  context.params_list = params_list;
1904 
1905  appendStringInfoString(buf, "UPDATE ");
1906  deparseRelation(buf, rel);
1907  if (foreignrel->reloptkind == RELOPT_JOINREL)
1908  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
1909  appendStringInfoString(buf, " SET ");
1910 
1911  /* Make sure any constants in the exprs are printed portably */
1912  nestlevel = set_transmission_modes();
1913 
1914  first = true;
1915  forboth(lc, targetlist, lc2, targetAttrs)
1916  {
1917  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1918  int attnum = lfirst_int(lc2);
1919 
1920  /* update's new-value expressions shouldn't be resjunk */
1921  Assert(!tle->resjunk);
1922 
1923  if (!first)
1924  appendStringInfoString(buf, ", ");
1925  first = false;
1926 
1927  deparseColumnRef(buf, rtindex, attnum, rte, false);
1928  appendStringInfoString(buf, " = ");
1929  deparseExpr((Expr *) tle->expr, &context);
1930  }
1931 
1932  reset_transmission_modes(nestlevel);
1933 
1934  if (foreignrel->reloptkind == RELOPT_JOINREL)
1935  {
1936  List *ignore_conds = NIL;
1937 
1938  appendStringInfoString(buf, " FROM ");
1939  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
1940  &ignore_conds, params_list);
1941  remote_conds = list_concat(remote_conds, ignore_conds);
1942  }
1943 
1944  if (remote_conds)
1945  {
1946  appendStringInfoString(buf, " WHERE ");
1947  appendConditions(remote_conds, &context);
1948  }
1949 
1950  if (foreignrel->reloptkind == RELOPT_JOINREL)
1951  deparseExplicitTargetList(returningList, true, retrieved_attrs,
1952  &context);
1953  else
1954  deparseReturningList(buf, rte, rtindex, rel, false,
1955  NIL, returningList, retrieved_attrs);
1956 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
RelOptKind reloptkind
Definition: pathnodes.h:673
List ** params_list
Definition: deparse.c:103
int set_transmission_modes(void)
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2046
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool resjunk
Definition: primnodes.h:1451
#define lfirst_int(lc)
Definition: pg_list.h:170
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:383
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1412
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1341
StringInfo buf
Definition: deparse.c:102
int16 attnum
Definition: pg_attribute.h:83
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
Expr * expr
Definition: primnodes.h:1444
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1489
RelOptInfo * scanrel
Definition: deparse.c:99
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:98
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2216

◆ deparseDistinctExpr()

static void deparseDistinctExpr ( DistinctExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2867 of file deparse.c.

References appendStringInfoChar(), appendStringInfoString(), OpExpr::args, Assert, buf, deparse_expr_cxt::buf, deparseExpr(), linitial, list_length(), and lsecond.

Referenced by deparseExpr().

2868 {
2869  StringInfo buf = context->buf;
2870 
2871  Assert(list_length(node->args) == 2);
2872 
2873  appendStringInfoChar(buf, '(');
2874  deparseExpr(linitial(node->args), context);
2875  appendStringInfoString(buf, " IS DISTINCT FROM ");
2876  deparseExpr(lsecond(node->args), context);
2877  appendStringInfoChar(buf, ')');
2878 }
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
List * args
Definition: primnodes.h:548

◆ deparseExplicitTargetList()

static void deparseExplicitTargetList ( List tlist,
bool  is_returning,
List **  retrieved_attrs,
deparse_expr_cxt context 
)
static

Definition at line 1412 of file deparse.c.

References appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseExpr(), TargetEntry::expr, i, lappend_int(), lfirst_node, and NIL.

Referenced by deparseDirectDeleteSql(), deparseDirectUpdateSql(), and deparseSelectSql().

1416 {
1417  ListCell *lc;
1418  StringInfo buf = context->buf;
1419  int i = 0;
1420 
1421  *retrieved_attrs = NIL;
1422 
1423  foreach(lc, tlist)
1424  {
1425  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1426 
1427  if (i > 0)
1428  appendStringInfoString(buf, ", ");
1429  else if (is_returning)
1430  appendStringInfoString(buf, " RETURNING ");
1431 
1432  deparseExpr((Expr *) tle->expr, context);
1433 
1434  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1435  i++;
1436  }
1437 
1438  if (i == 0 && !is_returning)
1439  appendStringInfoString(buf, "NULL");
1440 }
#define NIL
Definition: pg_list.h:65
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * lappend_int(List *list, int datum)
Definition: list.c:354
StringInfo buf
Definition: deparse.c:102
Expr * expr
Definition: primnodes.h:1444
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
int i

◆ deparseExpr()

static void deparseExpr ( Expr expr,
deparse_expr_cxt context 
)
static

Definition at line 2419 of file deparse.c.

References deparseAggref(), deparseArrayExpr(), deparseBoolExpr(), deparseConst(), deparseDistinctExpr(), deparseFuncExpr(), deparseNullTest(), deparseOpExpr(), deparseParam(), deparseRelabelType(), deparseScalarArrayOpExpr(), deparseSubscriptingRef(), deparseVar(), elog, ERROR, nodeTag, T_Aggref, T_ArrayExpr, T_BoolExpr, T_Const, T_DistinctExpr, T_FuncExpr, T_NullTest, T_OpExpr, T_Param, T_RelabelType, T_ScalarArrayOpExpr, T_SubscriptingRef, and T_Var.

Referenced by appendConditions(), appendLimitClause(), appendOrderByClause(), deparseAggref(), deparseArrayExpr(), deparseBoolExpr(), deparseDirectUpdateSql(), deparseDistinctExpr(), deparseExplicitTargetList(), deparseFuncExpr(), deparseNullTest(), deparseOpExpr(), deparseRelabelType(), deparseScalarArrayOpExpr(), deparseSortGroupClause(), deparseSubqueryTargetList(), and deparseSubscriptingRef().

2420 {
2421  if (node == NULL)
2422  return;
2423 
2424  switch (nodeTag(node))
2425  {
2426  case T_Var:
2427  deparseVar((Var *) node, context);
2428  break;
2429  case T_Const:
2430  deparseConst((Const *) node, context, 0);
2431  break;
2432  case T_Param:
2433  deparseParam((Param *) node, context);
2434  break;
2435  case T_SubscriptingRef:
2436  deparseSubscriptingRef((SubscriptingRef *) node, context);
2437  break;
2438  case T_FuncExpr:
2439  deparseFuncExpr((FuncExpr *) node, context);
2440  break;
2441  case T_OpExpr:
2442  deparseOpExpr((OpExpr *) node, context);
2443  break;
2444  case T_DistinctExpr:
2445  deparseDistinctExpr((DistinctExpr *) node, context);
2446  break;
2447  case T_ScalarArrayOpExpr:
2448  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2449  break;
2450  case T_RelabelType:
2451  deparseRelabelType((RelabelType *) node, context);
2452  break;
2453  case T_BoolExpr:
2454  deparseBoolExpr((BoolExpr *) node, context);
2455  break;
2456  case T_NullTest:
2457  deparseNullTest((NullTest *) node, context);
2458  break;
2459  case T_ArrayExpr:
2460  deparseArrayExpr((ArrayExpr *) node, context);
2461  break;
2462  case T_Aggref:
2463  deparseAggref((Aggref *) node, context);
2464  break;
2465  default:
2466  elog(ERROR, "unsupported expression type for deparse: %d",
2467  (int) nodeTag(node));
2468  break;
2469  }
2470 }
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:3040
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2943
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:2981
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2867
Definition: primnodes.h:186
#define ERROR
Definition: elog.h:46
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2653
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2885
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2795
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:3014
static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2686
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:2930
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2481
Definition: nodes.h:158
Definition: nodes.h:157
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2547
#define nodeTag(nodeptr)
Definition: nodes.h:544
#define elog(elevel,...)
Definition: elog.h:232
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2732
Definition: nodes.h:159

◆ deparseFromExpr()

static void deparseFromExpr ( List quals,
deparse_expr_cxt context 
)
static

Definition at line 1141 of file deparse.c.

References appendConditions(), appendStringInfoString(), Assert, bms_membership(), BMS_MULTIPLE, buf, deparse_expr_cxt::buf, deparseFromExprForRel(), deparse_expr_cxt::foreignrel, IS_JOIN_REL, IS_SIMPLE_REL, IS_UPPER_REL, NIL, deparse_expr_cxt::params_list, RelOptInfo::relids, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

Referenced by deparseSelectStmtForRel().

1142 {
1143  StringInfo buf = context->buf;
1144  RelOptInfo *scanrel = context->scanrel;
1145 
1146  /* For upper relations, scanrel must be either a joinrel or a baserel */
1147  Assert(!IS_UPPER_REL(context->foreignrel) ||
1148  IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1149 
1150  /* Construct FROM clause */
1151  appendStringInfoString(buf, " FROM ");
1152  deparseFromExprForRel(buf, context->root, scanrel,
1153  (bms_membership(scanrel->relids) == BMS_MULTIPLE),
1154  (Index) 0, NULL, context->params_list);
1155 
1156  /* Construct WHERE clause */
1157  if (quals != NIL)
1158  {
1159  appendStringInfoString(buf, " WHERE ");
1160  appendConditions(quals, context);
1161  }
1162 }
#define NIL
Definition: pg_list.h:65
List ** params_list
Definition: deparse.c:103
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:649
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
Relids relids
Definition: pathnodes.h:676
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1341
StringInfo buf
Definition: deparse.c:102
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
unsigned int Index
Definition: c.h:549
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1489
RelOptInfo * scanrel
Definition: deparse.c:99
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseFromExprForRel()

static void deparseFromExprForRel ( StringInfo  buf,
PlannerInfo root,
RelOptInfo foreignrel,
bool  use_alias,
Index  ignore_rel,
List **  ignore_conds,
List **  params_list 
)
static

Definition at line 1489 of file deparse.c.

References appendBinaryStringInfo(), appendConditions(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, bms_is_member(), buf, deparse_expr_cxt::buf, StringInfoData::data, deparseRangeTblRef(), deparseRelation(), RelOptInfo::fdw_private, foreign_glob_cxt::foreignrel, deparse_expr_cxt::foreignrel, get_jointype_name(), initStringInfo(), PgFdwRelationInfo::innerrel, IS_JOIN_REL, JOIN_INNER, PgFdwRelationInfo::joinclauses, PgFdwRelationInfo::jointype, StringInfoData::len, list_concat(), PgFdwRelationInfo::make_innerrel_subquery, PgFdwRelationInfo::make_outerrel_subquery, NIL, NoLock, PgFdwRelationInfo::outerrel, deparse_expr_cxt::params_list, planner_rt_fetch, REL_ALIAS_PREFIX, RelOptInfo::relid, RangeTblEntry::relid, RelOptInfo::relids, foreign_glob_cxt::root, deparse_expr_cxt::root, deparse_expr_cxt::scanrel, table_close(), and table_open().

Referenced by deparseDirectDeleteSql(), deparseDirectUpdateSql(), deparseFromExpr(), and deparseRangeTblRef().

1492 {
1493  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1494 
1495  if (IS_JOIN_REL(foreignrel))
1496  {
1497  StringInfoData join_sql_o;
1498  StringInfoData join_sql_i;
1499  RelOptInfo *outerrel = fpinfo->outerrel;
1500  RelOptInfo *innerrel = fpinfo->innerrel;
1501  bool outerrel_is_target = false;
1502  bool innerrel_is_target = false;
1503 
1504  if (ignore_rel > 0 && bms_is_member(ignore_rel, foreignrel->relids))
1505  {
1506  /*
1507  * If this is an inner join, add joinclauses to *ignore_conds and
1508  * set it to empty so that those can be deparsed into the WHERE
1509  * clause. Note that since the target relation can never be
1510  * within the nullable side of an outer join, those could safely
1511  * be pulled up into the WHERE clause (see foreign_join_ok()).
1512  * Note also that since the target relation is only inner-joined
1513  * to any other relation in the query, all conditions in the join
1514  * tree mentioning the target relation could be deparsed into the
1515  * WHERE clause by doing this recursively.
1516  */
1517  if (fpinfo->jointype == JOIN_INNER)
1518  {
1519  *ignore_conds = list_concat(*ignore_conds,
1520  fpinfo->joinclauses);
1521  fpinfo->joinclauses = NIL;
1522  }
1523 
1524  /*
1525  * Check if either of the input relations is the target relation.
1526  */
1527  if (outerrel->relid == ignore_rel)
1528  outerrel_is_target = true;
1529  else if (innerrel->relid == ignore_rel)
1530  innerrel_is_target = true;
1531  }
1532 
1533  /* Deparse outer relation if not the target relation. */
1534  if (!outerrel_is_target)
1535  {
1536  initStringInfo(&join_sql_o);
1537  deparseRangeTblRef(&join_sql_o, root, outerrel,
1538  fpinfo->make_outerrel_subquery,
1539  ignore_rel, ignore_conds, params_list);
1540 
1541  /*
1542  * If inner relation is the target relation, skip deparsing it.
1543  * Note that since the join of the target relation with any other
1544  * relation in the query is an inner join and can never be within
1545  * the nullable side of an outer join, the join could be
1546  * interchanged with higher-level joins (cf. identity 1 on outer
1547  * join reordering shown in src/backend/optimizer/README), which
1548  * means it's safe to skip the target-relation deparsing here.
1549  */
1550  if (innerrel_is_target)
1551  {
1552  Assert(fpinfo->jointype == JOIN_INNER);
1553  Assert(fpinfo->joinclauses == NIL);
1554  appendBinaryStringInfo(buf, join_sql_o.data, join_sql_o.len);
1555  return;
1556  }
1557  }
1558 
1559  /* Deparse inner relation if not the target relation. */
1560  if (!innerrel_is_target)
1561  {
1562  initStringInfo(&join_sql_i);
1563  deparseRangeTblRef(&join_sql_i, root, innerrel,
1564  fpinfo->make_innerrel_subquery,
1565  ignore_rel, ignore_conds, params_list);
1566 
1567  /*
1568  * If outer relation is the target relation, skip deparsing it.
1569  * See the above note about safety.
1570  */
1571  if (outerrel_is_target)
1572  {
1573  Assert(fpinfo->jointype == JOIN_INNER);
1574  Assert(fpinfo->joinclauses == NIL);
1575  appendBinaryStringInfo(buf, join_sql_i.data, join_sql_i.len);
1576  return;
1577  }
1578  }
1579 
1580  /* Neither of the relations is the target relation. */
1581  Assert(!outerrel_is_target && !innerrel_is_target);
1582 
1583  /*
1584  * For a join relation FROM clause entry is deparsed as
1585  *
1586  * ((outer relation) <join type> (inner relation) ON (joinclauses))
1587  */
1588  appendStringInfo(buf, "(%s %s JOIN %s ON ", join_sql_o.data,
1589  get_jointype_name(fpinfo->jointype), join_sql_i.data);
1590 
1591  /* Append join clause; (TRUE) if no join clause */
1592  if (fpinfo->joinclauses)
1593  {
1594  deparse_expr_cxt context;
1595 
1596  context.buf = buf;
1597  context.foreignrel = foreignrel;
1598  context.scanrel = foreignrel;
1599  context.root = root;
1600  context.params_list = params_list;
1601 
1602  appendStringInfoChar(buf, '(');
1603  appendConditions(fpinfo->joinclauses, &context);
1604  appendStringInfoChar(buf, ')');
1605  }
1606  else
1607  appendStringInfoString(buf, "(TRUE)");
1608 
1609  /* End the FROM clause entry. */
1610  appendStringInfoChar(buf, ')');
1611  }
1612  else
1613  {
1614  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1615 
1616  /*
1617  * Core code already has some lock on each rel being planned, so we
1618  * can use NoLock here.
1619  */
1620  Relation rel = table_open(rte->relid, NoLock);
1621 
1622  deparseRelation(buf, rel);
1623 
1624  /*
1625  * Add a unique alias to avoid any conflict in relation names due to
1626  * pulled up subqueries in the query being built for a pushed down
1627  * join.
1628  */
1629  if (use_alias)
1630  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid);
1631 
1632  table_close(rel, NoLock);
1633  }
1634 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
List ** params_list
Definition: deparse.c:103
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:383
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
const char * get_jointype_name(JoinType jointype)
Definition: deparse.c:1375
Relids relids
Definition: pathnodes.h:676
static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1640
Index relid
Definition: pathnodes.h:704
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1341
StringInfo buf
Definition: deparse.c:102
void * fdw_private
Definition: pathnodes.h:732
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
RelOptInfo * innerrel
Definition: postgres_fdw.h:103
RelOptInfo * scanrel
Definition: deparse.c:99
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
RelOptInfo * foreignrel
Definition: deparse.c:98
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ deparseFuncExpr()

static void deparseFuncExpr ( FuncExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2732 of file deparse.c.

References appendFunctionName(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), arg, FuncExpr::args, buf, deparse_expr_cxt::buf, COERCE_EXPLICIT_CAST, COERCE_IMPLICIT_CAST, deparse_type_name(), deparseExpr(), exprIsLengthCoercion(), FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcvariadic, lfirst, linitial, and lnext().

Referenced by deparseExpr().

2733 {
2734  StringInfo buf = context->buf;
2735  bool use_variadic;
2736  bool first;
2737  ListCell *arg;
2738 
2739  /*
2740  * If the function call came from an implicit coercion, then just show the
2741  * first argument.
2742  */
2743  if (node->funcformat == COERCE_IMPLICIT_CAST)
2744  {
2745  deparseExpr((Expr *) linitial(node->args), context);
2746  return;
2747  }
2748 
2749  /*
2750  * If the function call came from a cast, then show the first argument
2751  * plus an explicit cast operation.
2752  */
2753  if (node->funcformat == COERCE_EXPLICIT_CAST)
2754  {
2755  Oid rettype = node->funcresulttype;
2756  int32 coercedTypmod;
2757 
2758  /* Get the typmod if this is a length-coercion function */
2759  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2760 
2761  deparseExpr((Expr *) linitial(node->args), context);
2762  appendStringInfo(buf, "::%s",
2763  deparse_type_name(rettype, coercedTypmod));
2764  return;
2765  }
2766 
2767  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2768  use_variadic = node->funcvariadic;
2769 
2770  /*
2771  * Normal function: display as proname(args).
2772  */
2773  appendFunctionName(node->funcid, context);
2774  appendStringInfoChar(buf, '(');
2775 
2776  /* ... and all the arguments */
2777  first = true;
2778  foreach(arg, node->args)
2779  {
2780  if (!first)
2781  appendStringInfoString(buf, ", ");
2782  if (use_variadic && lnext(node->args, arg) == NULL)
2783  appendStringInfoString(buf, "VARIADIC ");
2784  deparseExpr((Expr *) lfirst(arg), context);
2785  first = false;
2786  }
2787  appendStringInfoChar(buf, ')');
2788 }
Oid funcresulttype
Definition: primnodes.h:496
List * args
Definition: primnodes.h:503
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3349
Definition: nodes.h:539
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:503
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
CoercionForm funcformat
Definition: primnodes.h:500
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
Oid funcid
Definition: primnodes.h:495
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:925
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
void * arg
bool funcvariadic
Definition: primnodes.h:498

◆ deparseInsertSql()

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 at line 1714 of file deparse.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), attnum, deparseColumnRef(), deparseRelation(), deparseReturningList(), StringInfoData::len, lfirst_int, TriggerDesc::trig_insert_after_row, and RelationData::trigdesc.

Referenced by postgresBeginForeignInsert(), and postgresPlanForeignModify().

1719 {
1720  AttrNumber pindex;
1721  bool first;
1722  ListCell *lc;
1723 
1724  appendStringInfoString(buf, "INSERT INTO ");
1725  deparseRelation(buf, rel);
1726 
1727  if (targetAttrs)
1728  {
1729  appendStringInfoChar(buf, '(');
1730 
1731  first = true;
1732  foreach(lc, targetAttrs)
1733  {
1734  int attnum = lfirst_int(lc);
1735 
1736  if (!first)
1737  appendStringInfoString(buf, ", ");
1738  first = false;
1739 
1740  deparseColumnRef(buf, rtindex, attnum, rte, false);
1741  }
1742 
1743  appendStringInfoString(buf, ") VALUES (");
1744 
1745  pindex = 1;
1746  first = true;
1747  foreach(lc, targetAttrs)
1748  {
1749  if (!first)
1750  appendStringInfoString(buf, ", ");
1751  first = false;
1752 
1753  appendStringInfo(buf, "$%d", pindex);
1754  pindex++;
1755  }
1756 
1757  appendStringInfoChar(buf, ')');
1758  }
1759  else
1760  appendStringInfoString(buf, " DEFAULT VALUES");
1761  *values_end_len = buf->len;
1762 
1763  if (doNothing)
1764  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1765 
1766  deparseReturningList(buf, rte, rtindex, rel,
1767  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1768  withCheckOptionList, returningList, retrieved_attrs);
1769 }
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2046
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define lfirst_int(lc)
Definition: pg_list.h:170
TriggerDesc * trigdesc
Definition: rel.h:115
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_insert_after_row
Definition: reltrigger.h:57
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
int16 attnum
Definition: pg_attribute.h:83
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2216

◆ deparseLockingClause()

static void deparseLockingClause ( deparse_expr_cxt context)
static

Definition at line 1251 of file deparse.c.

References PlannerInfo::all_result_relids, appendStringInfo(), appendStringInfoString(), bms_is_member(), bms_membership(), BMS_MULTIPLE, bms_next_member(), buf, deparse_expr_cxt::buf, CMD_DELETE, CMD_UPDATE, Query::commandType, RelOptInfo::fdw_private, get_plan_rowmark(), IS_JOIN_REL, LCS_FORKEYSHARE, LCS_FORNOKEYUPDATE, LCS_FORSHARE, LCS_FORUPDATE, LCS_NONE, PgFdwRelationInfo::lower_subquery_rels, PlannerInfo::parse, REL_ALIAS_PREFIX, RelOptInfo::relids, foreign_glob_cxt::root, deparse_expr_cxt::root, PlannerInfo::rowMarks, deparse_expr_cxt::scanrel, and PlanRowMark::strength.

Referenced by deparseSelectStmtForRel().

1252 {
1253  StringInfo buf = context->buf;
1254  PlannerInfo *root = context->root;
1255  RelOptInfo *rel = context->scanrel;
1256  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1257  int relid = -1;
1258 
1259  while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1260  {
1261  /*
1262  * Ignore relation if it appears in a lower subquery. Locking clause
1263  * for such a relation is included in the subquery if necessary.
1264  */
1265  if (bms_is_member(relid, fpinfo->lower_subquery_rels))
1266  continue;
1267 
1268  /*
1269  * Add FOR UPDATE/SHARE if appropriate. We apply locking during the
1270  * initial row fetch, rather than later on as is done for local
1271  * tables. The extra roundtrips involved in trying to duplicate the
1272  * local semantics exactly don't seem worthwhile (see also comments
1273  * for RowMarkType).
1274  *
1275  * Note: because we actually run the query as a cursor, this assumes
1276  * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't
1277  * before 8.3.
1278  */
1279  if (bms_is_member(relid, root->all_result_relids) &&
1280  (root->parse->commandType == CMD_UPDATE ||
1281  root->parse->commandType == CMD_DELETE))
1282  {
1283  /* Relation is UPDATE/DELETE target, so use FOR UPDATE */
1284  appendStringInfoString(buf, " FOR UPDATE");
1285 
1286  /* Add the relation alias if we are here for a join relation */
1287  if (IS_JOIN_REL(rel))
1288  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1289  }
1290  else
1291  {
1292  PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid);
1293 
1294  if (rc)
1295  {
1296  /*
1297  * Relation is specified as a FOR UPDATE/SHARE target, so
1298  * handle that. (But we could also see LCS_NONE, meaning this
1299  * isn't a target relation after all.)
1300  *
1301  * For now, just ignore any [NO] KEY specification, since (a)
1302  * it's not clear what that means for a remote table that we
1303  * don't have complete information about, and (b) it wouldn't
1304  * work anyway on older remote servers. Likewise, we don't
1305  * worry about NOWAIT.
1306  */
1307  switch (rc->strength)
1308  {
1309  case LCS_NONE:
1310  /* No locking needed */
1311  break;
1312  case LCS_FORKEYSHARE:
1313  case LCS_FORSHARE:
1314  appendStringInfoString(buf, " FOR SHARE");
1315  break;
1316  case LCS_FORNOKEYUPDATE:
1317  case LCS_FORUPDATE:
1318  appendStringInfoString(buf, " FOR UPDATE");
1319  break;
1320  }
1321 
1322  /* Add the relation alias if we are here for a join relation */
1323  if (bms_membership(rel->relids) == BMS_MULTIPLE &&
1324  rc->strength != LCS_NONE)
1325  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1326  }
1327  }
1328  }
1329 }
List * rowMarks
Definition: pathnodes.h:287
Relids all_result_relids
Definition: pathnodes.h:275
Query * parse
Definition: pathnodes.h:161
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Relids lower_subquery_rels
Definition: postgres_fdw.h:119
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
Relids relids
Definition: pathnodes.h:676
StringInfo buf
Definition: deparse.c:102
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
void * fdw_private
Definition: pathnodes.h:732
CmdType commandType
Definition: parsenodes.h:120
PlannerInfo * root
Definition: deparse.c:97
LockClauseStrength strength
Definition: plannodes.h:1123
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:425
RelOptInfo * scanrel
Definition: deparse.c:99
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ deparseNullTest()

static void deparseNullTest ( NullTest node,
deparse_expr_cxt context 
)
static

Definition at line 2981 of file deparse.c.

References appendStringInfoChar(), appendStringInfoString(), NullTest::arg, NullTest::argisrow, buf, deparse_expr_cxt::buf, deparseExpr(), exprType(), IS_NULL, NullTest::nulltesttype, and type_is_rowtype().

Referenced by deparseExpr().

2982 {
2983  StringInfo buf = context->buf;
2984 
2985  appendStringInfoChar(buf, '(');
2986  deparseExpr(node->arg, context);
2987 
2988  /*
2989  * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
2990  * shorter and traditional. If it's a rowtype input but we're applying a
2991  * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
2992  * correct.
2993  */
2994  if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
2995  {
2996  if (node->nulltesttype == IS_NULL)
2997  appendStringInfoString(buf, " IS NULL)");
2998  else
2999  appendStringInfoString(buf, " IS NOT NULL)");
3000  }
3001  else
3002  {
3003  if (node->nulltesttype == IS_NULL)
3004  appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
3005  else
3006  appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
3007  }
3008 }
Definition: nodes.h:539
Expr * arg
Definition: primnodes.h:1255
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2602
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
NullTestType nulltesttype
Definition: primnodes.h:1256
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
bool argisrow
Definition: primnodes.h:1257

◆ deparseOperatorName()

static void deparseOperatorName ( StringInfo  buf,
Form_pg_operator  opform 
)
static

Definition at line 2839 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), get_namespace_name(), NameStr, and quote_identifier().

Referenced by appendAggOrderBy(), deparseOpExpr(), and deparseScalarArrayOpExpr().

2840 {
2841  char *opname;
2842 
2843  /* opname is not a SQL identifier, so we should not quote it. */
2844  opname = NameStr(opform->oprname);
2845 
2846  /* Print schema name only if it's not pg_catalog */
2847  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
2848  {
2849  const char *opnspname;
2850 
2851  opnspname = get_namespace_name(opform->oprnamespace);
2852  /* Print fully qualified operator name. */
2853  appendStringInfo(buf, "OPERATOR(%s.%s)",
2854  quote_identifier(opnspname), opname);
2855  }
2856  else
2857  {
2858  /* Just print operator name. */
2859  appendStringInfoString(buf, opname);
2860  }
2861 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11324
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define NameStr(name)
Definition: c.h:681

◆ deparseOpExpr()

static void deparseOpExpr ( OpExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2795 of file deparse.c.

References appendStringInfoChar(), OpExpr::args, Assert, buf, deparse_expr_cxt::buf, deparseExpr(), deparseOperatorName(), elog, ERROR, GETSTRUCT, HeapTupleIsValid, linitial, list_length(), llast, ObjectIdGetDatum, OPEROID, OpExpr::opno, ReleaseSysCache(), and SearchSysCache1().

Referenced by deparseExpr().

2796 {
2797  StringInfo buf = context->buf;
2798  HeapTuple tuple;
2799  Form_pg_operator form;
2800  char oprkind;
2801 
2802  /* Retrieve information about the operator from system catalog. */
2803  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2804  if (!HeapTupleIsValid(tuple))
2805  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2806  form = (Form_pg_operator) GETSTRUCT(tuple);
2807  oprkind = form->oprkind;
2808 
2809  /* Sanity check. */
2810  Assert((oprkind == 'l' && list_length(node->args) == 1) ||
2811  (oprkind == 'b' && list_length(node->args) == 2));
2812 
2813  /* Always parenthesize the expression. */
2814  appendStringInfoChar(buf, '(');
2815 
2816  /* Deparse left operand, if any. */
2817  if (oprkind == 'b')
2818  {
2819  deparseExpr(linitial(node->args), context);
2820  appendStringInfoChar(buf, ' ');
2821  }
2822 
2823  /* Deparse operator name. */
2824  deparseOperatorName(buf, form);
2825 
2826  /* Deparse right operand. */
2827  appendStringInfoChar(buf, ' ');
2828  deparseExpr(llast(node->args), context);
2829 
2830  appendStringInfoChar(buf, ')');
2831 
2832  ReleaseSysCache(tuple);
2833 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define llast(l)
Definition: pg_list.h:194
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2839
#define linitial(l)
Definition: pg_list.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
#define elog(elevel,...)
Definition: elog.h:232
Oid opno
Definition: primnodes.h:542
List * args
Definition: primnodes.h:548

◆ deparseParam()

static void deparseParam ( Param node,
deparse_expr_cxt context 
)
static

Definition at line 2653 of file deparse.c.

References equal(), lappend(), lfirst, deparse_expr_cxt::params_list, Param::paramtype, Param::paramtypmod, printRemoteParam(), and printRemotePlaceholder().

Referenced by deparseExpr().

2654 {
2655  if (context->params_list)
2656  {
2657  int pindex = 0;
2658  ListCell *lc;
2659 
2660  /* find its index in params_list */
2661  foreach(lc, *context->params_list)
2662  {
2663  pindex++;
2664  if (equal(node, (Node *) lfirst(lc)))
2665  break;
2666  }
2667  if (lc == NULL)
2668  {
2669  /* not in list, so add it */
2670  pindex++;
2671  *context->params_list = lappend(*context->params_list, node);
2672  }
2673 
2674  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2675  }
2676  else
2677  {
2678  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2679  }
2680 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
List ** params_list
Definition: deparse.c:103
Definition: nodes.h:539
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3191
List * lappend(List *list, void *datum)
Definition: list.c:336
int32 paramtypmod
Definition: primnodes.h:270
#define lfirst(lc)
Definition: pg_list.h:169
Oid paramtype
Definition: primnodes.h:269
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3217

◆ deparseRangeTblRef()

static void deparseRangeTblRef ( StringInfo  buf,
PlannerInfo root,
RelOptInfo foreignrel,
bool  make_subquery,
Index  ignore_rel,
List **  ignore_conds,
List **  params_list 
)
static

Definition at line 1640 of file deparse.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, bms_is_member(), deparseFromExprForRel(), deparseSelectStmtForRel(), PathTarget::exprs, RelOptInfo::fdw_private, i, IS_JOIN_REL, IS_SIMPLE_REL, list_length(), PgFdwRelationInfo::local_conds, NIL, PgFdwRelationInfo::relation_index, RelOptInfo::relids, RelOptInfo::reltarget, PgFdwRelationInfo::remote_conds, SUBQUERY_COL_ALIAS_PREFIX, and SUBQUERY_REL_ALIAS_PREFIX.

Referenced by deparseFromExprForRel().

1643 {
1644  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1645 
1646  /* Should only be called in these cases. */
1647  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1648 
1649  Assert(fpinfo->local_conds == NIL);
1650 
1651  /* If make_subquery is true, deparse the relation as a subquery. */
1652  if (make_subquery)
1653  {
1654  List *retrieved_attrs;
1655  int ncols;
1656 
1657  /*
1658  * The given relation shouldn't contain the target relation, because
1659  * this should only happen for input relations for a full join, and
1660  * such relations can never contain an UPDATE/DELETE target.
1661  */
1662  Assert(ignore_rel == 0 ||
1663  !bms_is_member(ignore_rel, foreignrel->relids));
1664 
1665  /* Deparse the subquery representing the relation. */
1666  appendStringInfoChar(buf, '(');
1667  deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1668  fpinfo->remote_conds, NIL,
1669  false, false, true,
1670  &retrieved_attrs, params_list);
1671  appendStringInfoChar(buf, ')');
1672 
1673  /* Append the relation alias. */
1675  fpinfo->relation_index);
1676 
1677  /*
1678  * Append the column aliases if needed. Note that the subquery emits
1679  * expressions specified in the relation's reltarget (see
1680  * deparseSubqueryTargetList).
1681  */
1682  ncols = list_length(foreignrel->reltarget->exprs);
1683  if (ncols > 0)
1684  {
1685  int i;
1686 
1687  appendStringInfoChar(buf, '(');
1688  for (i = 1; i <= ncols; i++)
1689  {
1690  if (i > 1)
1691  appendStringInfoString(buf, ", ");
1692 
1694  }
1695  appendStringInfoChar(buf, ')');
1696  }
1697  }
1698  else
1699  deparseFromExprForRel(buf, root, foreignrel, true, ignore_rel,
1700  ignore_conds, params_list);
1701 }
#define NIL
Definition: pg_list.h:65
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:649
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:111
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Relids relids
Definition: pathnodes.h:676
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
List * exprs
Definition: pathnodes.h:1102
void * fdw_private
Definition: pathnodes.h:732
#define Assert(condition)
Definition: c.h:804
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:1001
static int list_length(const List *l)
Definition: pg_list.h:149
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1489
int i
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
struct PathTarget * reltarget
Definition: pathnodes.h:687

◆ deparseRelabelType()

static void deparseRelabelType ( RelabelType node,
deparse_expr_cxt context 
)
static

Definition at line 2930 of file deparse.c.

References appendStringInfo(), RelabelType::arg, deparse_expr_cxt::buf, COERCE_IMPLICIT_CAST, deparse_type_name(), deparseExpr(), RelabelType::relabelformat, RelabelType::resulttype, and RelabelType::resulttypmod.

Referenced by deparseExpr().

2931 {
2932  deparseExpr(node->arg, context);
2933  if (node->relabelformat != COERCE_IMPLICIT_CAST)
2934  appendStringInfo(context->buf, "::%s",
2936  node->resulttypmod));
2937 }
Expr * arg
Definition: primnodes.h:850
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
StringInfo buf
Definition: deparse.c:102
Oid resulttype
Definition: primnodes.h:851
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:925
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
int32 resulttypmod
Definition: primnodes.h:852
CoercionForm relabelformat
Definition: primnodes.h:854

◆ deparseRelation()

static void deparseRelation ( StringInfo  buf,
Relation  rel 
)
static

Definition at line 2344 of file deparse.c.

References appendStringInfo(), defGetString(), DefElem::defname, get_namespace_name(), GetForeignTable(), lfirst, ForeignTable::options, quote_identifier(), RelationGetNamespace, RelationGetRelationName, RelationGetRelid, and relname.

Referenced by deparseAnalyzeSizeSql(), deparseAnalyzeSql(), deparseDeleteSql(), deparseDirectDeleteSql(), deparseDirectUpdateSql(), deparseFromExprForRel(), deparseInsertSql(), deparseTruncateSql(), and deparseUpdateSql().

2345 {
2346  ForeignTable *table;
2347  const char *nspname = NULL;
2348  const char *relname = NULL;
2349  ListCell *lc;
2350 
2351  /* obtain additional catalog information. */
2352  table = GetForeignTable(RelationGetRelid(rel));
2353 
2354  /*
2355  * Use value of FDW options if any, instead of the name of object itself.
2356  */
2357  foreach(lc, table->options)
2358  {
2359  DefElem *def = (DefElem *) lfirst(lc);
2360 
2361  if (strcmp(def->defname, "schema_name") == 0)
2362  nspname = defGetString(def);
2363  else if (strcmp(def->defname, "table_name") == 0)
2364  relname = defGetString(def);
2365  }
2366 
2367  /*
2368  * Note: we could skip printing the schema name if it's pg_catalog, but
2369  * that doesn't seem worth the trouble.
2370  */
2371  if (nspname == NULL)
2372  nspname = get_namespace_name(RelationGetNamespace(rel));
2373  if (relname == NULL)
2374  relname = RelationGetRelationName(rel);
2375 
2376  appendStringInfo(buf, "%s.%s",
2377  quote_identifier(nspname), quote_identifier(relname));
2378 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11324
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:248
NameData relname
Definition: pg_class.h:38
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * defGetString(DefElem *def)
Definition: define.c:49
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define RelationGetRelationName(relation)
Definition: rel.h:503
#define lfirst(lc)
Definition: pg_list.h:169
List * options
Definition: foreign.h:57
char * defname
Definition: parsenodes.h:746
#define RelationGetRelid(relation)
Definition: rel.h:469
#define RelationGetNamespace(relation)
Definition: rel.h:510

◆ deparseReturningList()

static void deparseReturningList ( StringInfo  buf,
RangeTblEntry rte,
Index  rtindex,
Relation  rel,
bool  trig_after_row,
List withCheckOptionList,
List returningList,
List **  retrieved_attrs 
)
static

Definition at line 2046 of file deparse.c.

References bms_make_singleton(), deparseTargetList(), FirstLowInvalidHeapAttributeNumber, NIL, and pull_varattnos().

Referenced by deparseDeleteSql(), deparseDirectDeleteSql(), deparseDirectUpdateSql(), deparseInsertSql(), and deparseUpdateSql().

2052 {
2053  Bitmapset *attrs_used = NULL;
2054 
2055  if (trig_after_row)
2056  {
2057  /* whole-row reference acquires all non-system columns */
2058  attrs_used =
2060  }
2061 
2062  if (withCheckOptionList != NIL)
2063  {
2064  /*
2065  * We need the attrs, non-system and system, mentioned in the local
2066  * query's WITH CHECK OPTION list.
2067  *
2068  * Note: we do this to ensure that WCO constraints will be evaluated
2069  * on the data actually inserted/updated on the remote side, which
2070  * might differ from the data supplied by the core code, for example
2071  * as a result of remote triggers.
2072  */
2073  pull_varattnos((Node *) withCheckOptionList, rtindex,
2074  &attrs_used);
2075  }
2076 
2077  if (returningList != NIL)
2078  {
2079  /*
2080  * We need the attrs, non-system and system, mentioned in the local
2081  * query's RETURNING list.
2082  */
2083  pull_varattnos((Node *) returningList, rtindex,
2084  &attrs_used);
2085  }
2086 
2087  if (attrs_used != NULL)
2088  deparseTargetList(buf, rte, rtindex, rel, true, attrs_used, false,
2089  retrieved_attrs);
2090  else
2091  *retrieved_attrs = NIL;
2092 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:539
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:246
static void deparseTargetList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1175
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186

◆ deparseScalarArrayOpExpr()

static void deparseScalarArrayOpExpr ( ScalarArrayOpExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2885 of file deparse.c.

References appendStringInfo(), appendStringInfoChar(), ScalarArrayOpExpr::args, Assert, buf, deparse_expr_cxt::buf, deparseExpr(), deparseOperatorName(), elog, ERROR, GETSTRUCT, HeapTupleIsValid, linitial, list_length(), lsecond, ObjectIdGetDatum, OPEROID, ScalarArrayOpExpr::opno, ReleaseSysCache(), SearchSysCache1(), and ScalarArrayOpExpr::useOr.

Referenced by deparseExpr().

2886 {
2887  StringInfo buf = context->buf;
2888  HeapTuple tuple;
2889  Form_pg_operator form;
2890  Expr *arg1;
2891  Expr *arg2;
2892 
2893  /* Retrieve information about the operator from system catalog. */
2894  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2895  if (!HeapTupleIsValid(tuple))
2896  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2897  form = (Form_pg_operator) GETSTRUCT(tuple);
2898 
2899  /* Sanity check. */
2900  Assert(list_length(node->args) == 2);
2901 
2902  /* Always parenthesize the expression. */
2903  appendStringInfoChar(buf, '(');
2904 
2905  /* Deparse left operand. */
2906  arg1 = linitial(node->args);
2907  deparseExpr(arg1, context);
2908  appendStringInfoChar(buf, ' ');
2909 
2910  /* Deparse operator name plus decoration. */
2911  deparseOperatorName(buf, form);
2912  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
2913 
2914  /* Deparse right operand. */
2915  arg2 = lsecond(node->args);
2916  deparseExpr(arg2, context);
2917 
2918  appendStringInfoChar(buf, ')');
2919 
2920  /* Always parenthesize the expression. */
2921  appendStringInfoChar(buf, ')');
2922 
2923  ReleaseSysCache(tuple);
2924 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2839
#define lsecond(l)
Definition: pg_list.h:179
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
#define elog(elevel,...)
Definition: elog.h:232

◆ deparseSelectSql()

static void deparseSelectSql ( List tlist,
bool  is_subquery,
List **  retrieved_attrs,
deparse_expr_cxt context 
)
static

Definition at line 1083 of file deparse.c.

References appendStringInfoString(), PgFdwRelationInfo::attrs_used, buf, deparse_expr_cxt::buf, deparseExplicitTargetList(), deparseSubqueryTargetList(), deparseTargetList(), RelOptInfo::fdw_private, foreign_glob_cxt::foreignrel, deparse_expr_cxt::foreignrel, IS_JOIN_REL, IS_UPPER_REL, NoLock, planner_rt_fetch, RelOptInfo::relid, RangeTblEntry::relid, foreign_glob_cxt::root, deparse_expr_cxt::root, table_close(), and table_open().

Referenced by deparseSelectStmtForRel().

1085 {
1086  StringInfo buf = context->buf;
1087  RelOptInfo *foreignrel = context->foreignrel;
1088  PlannerInfo *root = context->root;
1089  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1090 
1091  /*
1092  * Construct SELECT list
1093  */
1094  appendStringInfoString(buf, "SELECT ");
1095 
1096  if (is_subquery)
1097  {
1098  /*
1099  * For a relation that is deparsed as a subquery, emit expressions
1100  * specified in the relation's reltarget. Note that since this is for
1101  * the subquery, no need to care about *retrieved_attrs.
1102  */
1103  deparseSubqueryTargetList(context);
1104  }
1105  else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
1106  {
1107  /*
1108  * For a join or upper relation the input tlist gives the list of
1109  * columns required to be fetched from the foreign server.
1110  */
1111  deparseExplicitTargetList(tlist, false, retrieved_attrs, context);
1112  }
1113  else
1114  {
1115  /*
1116  * For a base relation fpinfo->attrs_used gives the list of columns
1117  * required to be fetched from the foreign server.
1118  */
1119  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1120 
1121  /*
1122  * Core code already has some lock on each rel being planned, so we
1123  * can use NoLock here.
1124  */
1125  Relation rel = table_open(rte->relid, NoLock);
1126 
1127  deparseTargetList(buf, rte, foreignrel->relid, rel, false,
1128  fpinfo->attrs_used, false, retrieved_attrs);
1129  table_close(rel, NoLock);
1130  }
1131 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1448
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
static void deparseTargetList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1175
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:383
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:68
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1412
Index relid
Definition: pathnodes.h:704
StringInfo buf
Definition: deparse.c:102
void * fdw_private
Definition: pathnodes.h:732
PlannerInfo * root
Definition: deparse.c:97
Bitmapset * attrs_used
Definition: postgres_fdw.h:50
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseSelectStmtForRel()

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 at line 1001 of file deparse.c.

References appendConditions(), appendGroupByClause(), appendLimitClause(), appendOrderByClause(), appendStringInfoString(), Assert, buf, deparse_expr_cxt::buf, deparseFromExpr(), deparseLockingClause(), deparseSelectSql(), RelOptInfo::fdw_private, deparse_expr_cxt::foreignrel, IS_JOIN_REL, IS_SIMPLE_REL, IS_UPPER_REL, PgFdwRelationInfo::outerrel, deparse_expr_cxt::params_list, PgFdwRelationInfo::remote_conds, foreign_glob_cxt::root, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

Referenced by deparseRangeTblRef(), estimate_path_cost_size(), and postgresGetForeignPlan().

1005 {
1006  deparse_expr_cxt context;
1007  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1008  List *quals;
1009 
1010  /*
1011  * We handle relations for foreign tables, joins between those and upper
1012  * relations.
1013  */
1014  Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel));
1015 
1016  /* Fill portions of context common to upper, join and base relation */
1017  context.buf = buf;
1018  context.root = root;
1019  context.foreignrel = rel;
1020  context.scanrel = IS_UPPER_REL(rel) ? fpinfo->outerrel : rel;
1021  context.params_list = params_list;
1022 
1023  /* Construct SELECT clause */
1024  deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
1025 
1026  /*
1027  * For upper relations, the WHERE clause is built from the remote
1028  * conditions of the underlying scan relation; otherwise, we can use the
1029  * supplied list of remote conditions directly.
1030  */
1031  if (IS_UPPER_REL(rel))
1032  {
1033  PgFdwRelationInfo *ofpinfo;
1034 
1035  ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
1036  quals = ofpinfo->remote_conds;
1037  }
1038  else
1039  quals = remote_conds;
1040 
1041  /* Construct FROM and WHERE clauses */
1042  deparseFromExpr(quals, &context);
1043 
1044  if (IS_UPPER_REL(rel))
1045  {
1046  /* Append GROUP BY clause */
1047  appendGroupByClause(tlist, &context);
1048 
1049  /* Append HAVING clause */
1050  if (remote_conds)
1051  {
1052  appendStringInfoString(buf, " HAVING ");
1053  appendConditions(remote_conds, &context);
1054  }
1055  }
1056 
1057  /* Add ORDER BY clause if we found any useful pathkeys */
1058  if (pathkeys)
1059  appendOrderByClause(pathkeys, has_final_sort, &context);
1060 
1061  /* Add LIMIT clause if necessary */
1062  if (has_limit)
1063  appendLimitClause(&context);
1064 
1065  /* Add any necessary FOR UPDATE/SHARE. */
1066  deparseLockingClause(&context);
1067 }
static void appendGroupByClause(List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3230
List ** params_list
Definition: deparse.c:103
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
static void appendLimitClause(deparse_expr_cxt *context)
Definition: deparse.c:3321
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:649
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
static void appendOrderByClause(List *pathkeys, bool has_final_sort, deparse_expr_cxt *context)
Definition: deparse.c:3267
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1083
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1341
StringInfo buf
Definition: deparse.c:102
void * fdw_private
Definition: pathnodes.h:732
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
static void deparseFromExpr(List *quals, deparse_expr_cxt *context)
Definition: deparse.c:1141
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
static void deparseLockingClause(deparse_expr_cxt *context)
Definition: deparse.c:1251
RelOptInfo * scanrel
Definition: deparse.c:99
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseSortGroupClause()

static Node * deparseSortGroupClause ( Index  ref,
List tlist,
bool  force_colno,
deparse_expr_cxt context 
)
static

Definition at line 3384 of file deparse.c.

References appendStringInfo(), appendStringInfoChar(), Assert, buf, deparse_expr_cxt::buf, deparseConst(), deparseExpr(), TargetEntry::expr, get_sortgroupref_tle(), IsA, TargetEntry::resjunk, and TargetEntry::resno.

Referenced by appendAggOrderBy(), and appendGroupByClause().

3386 {
3387  StringInfo buf = context->buf;
3388  TargetEntry *tle;
3389  Expr *expr;
3390 
3391  tle = get_sortgroupref_tle(ref, tlist);
3392  expr = tle->expr;
3393 
3394  if (force_colno)
3395  {
3396  /* Use column-number form when requested by caller. */
3397  Assert(!tle->resjunk);
3398  appendStringInfo(buf, "%d", tle->resno);
3399  }
3400  else if (expr && IsA(expr, Const))
3401  {
3402  /*
3403  * Force a typecast here so that we don't emit something like "GROUP
3404  * BY 2", which will be misconstrued as a column position rather than
3405  * a constant.
3406  */
3407  deparseConst((Const *) expr, context, 1);
3408  }
3409  else if (!expr || IsA(expr, Var))
3410  deparseExpr(expr, context);
3411  else
3412  {
3413  /* Always parenthesize the expression. */
3414  appendStringInfoChar(buf, '(');
3415  deparseExpr(expr, context);
3416  appendStringInfoChar(buf, ')');
3417  }
3418 
3419  return (Node *) expr;
3420 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
Definition: primnodes.h:186
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool resjunk
Definition: primnodes.h:1451
static char * buf
Definition: pg_test_fsync.c:68
AttrNumber resno
Definition: primnodes.h:1445
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:334
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define Assert(condition)
Definition: c.h:804
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2547
Expr * expr
Definition: primnodes.h:1444
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419

◆ deparseStringLiteral()

void deparseStringLiteral ( StringInfo  buf,
const char *  val 
)

Definition at line 2384 of file deparse.c.

References appendStringInfoChar(), ESCAPE_STRING_SYNTAX, and SQL_STR_DOUBLE.

Referenced by deparseAnalyzeSizeSql(), deparseConst(), and postgresImportForeignSchema().

2385 {
2386  const char *valptr;
2387 
2388  /*
2389  * Rather than making assumptions about the remote server's value of
2390  * standard_conforming_strings, always use E'foo' syntax if there are any
2391  * backslashes. This will fail on remote servers before 8.1, but those
2392  * are long out of support.
2393  */
2394  if (strchr(val, '\\') != NULL)
2396  appendStringInfoChar(buf, '\'');
2397  for (valptr = val; *valptr; valptr++)
2398  {
2399  char ch = *valptr;
2400 
2401  if (SQL_STR_DOUBLE(ch, true))
2402  appendStringInfoChar(buf, ch);
2403  appendStringInfoChar(buf, ch);
2404  }
2405  appendStringInfoChar(buf, '\'');
2406 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1167
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1164
long val
Definition: informix.c:664

◆ deparseSubqueryTargetList()

static void deparseSubqueryTargetList ( deparse_expr_cxt context)
static

Definition at line 1448 of file deparse.c.

References appendStringInfoString(), Assert, buf, deparse_expr_cxt::buf, deparseExpr(), PathTarget::exprs, foreign_glob_cxt::foreignrel, deparse_expr_cxt::foreignrel, IS_JOIN_REL, IS_SIMPLE_REL, lfirst, and RelOptInfo::reltarget.

Referenced by deparseSelectSql().

1449 {
1450  StringInfo buf = context->buf;
1451  RelOptInfo *foreignrel = context->foreignrel;
1452  bool first;
1453  ListCell *lc;
1454 
1455  /* Should only be called in these cases. */
1456  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1457 
1458  first = true;
1459  foreach(lc, foreignrel->reltarget->exprs)
1460  {
1461  Node *node = (Node *) lfirst(lc);
1462 
1463  if (!first)
1464  appendStringInfoString(buf, ", ");
1465  first = false;
1466 
1467  deparseExpr((Expr *) node, context);
1468  }
1469 
1470  /* Don't generate bad syntax if no expressions */
1471  if (first)
1472  appendStringInfoString(buf, "NULL");
1473 }
Definition: nodes.h:539
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:649
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * exprs
Definition: pathnodes.h:1102
StringInfo buf
Definition: deparse.c:102
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
RelOptInfo * foreignrel
Definition: deparse.c:98
struct PathTarget * reltarget
Definition: pathnodes.h:687

◆ deparseSubscriptingRef()

static void deparseSubscriptingRef ( SubscriptingRef node,
deparse_expr_cxt context 
)
static

Definition at line 2686 of file deparse.c.

References appendStringInfoChar(), buf, deparse_expr_cxt::buf, deparseExpr(), IsA, lfirst, list_head(), lnext(), SubscriptingRef::refexpr, SubscriptingRef::reflowerindexpr, and SubscriptingRef::refupperindexpr.

Referenced by deparseExpr().

2687 {
2688  StringInfo buf = context->buf;
2689  ListCell *lowlist_item;
2690  ListCell *uplist_item;
2691 
2692  /* Always parenthesize the expression. */
2693  appendStringInfoChar(buf, '(');
2694 
2695  /*
2696  * Deparse referenced array expression first. If that expression includes
2697  * a cast, we have to parenthesize to prevent the array subscript from
2698  * being taken as typename decoration. We can avoid that in the typical
2699  * case of subscripting a Var, but otherwise do it.
2700  */
2701  if (IsA(node->refexpr, Var))
2702  deparseExpr(node->refexpr, context);
2703  else
2704  {
2705  appendStringInfoChar(buf, '(');
2706  deparseExpr(node->refexpr, context);
2707  appendStringInfoChar(buf, ')');
2708  }
2709 
2710  /* Deparse subscript expressions. */
2711  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2712  foreach(uplist_item, node->refupperindexpr)
2713  {
2714  appendStringInfoChar(buf, '[');
2715  if (lowlist_item)
2716  {
2717  deparseExpr(lfirst(lowlist_item), context);
2718  appendStringInfoChar(buf, ':');
2719  lowlist_item = lnext(node->reflowerindexpr, lowlist_item);
2720  }
2721  deparseExpr(lfirst(uplist_item), context);
2722  appendStringInfoChar(buf, ']');
2723  }
2724 
2725  appendStringInfoChar(buf, ')');
2726 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Definition: primnodes.h:186
List * refupperindexpr
Definition: primnodes.h:444
static char * buf
Definition: pg_test_fsync.c:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2419
List * reflowerindexpr
Definition: primnodes.h:446
Expr * refexpr
Definition: primnodes.h:449

◆ deparseTargetList()

static void deparseTargetList ( StringInfo  buf,
RangeTblEntry rte,
Index  rtindex,
Relation  rel,
bool  is_returning,
Bitmapset attrs_used,
bool  qualify_col,
List **  retrieved_attrs 
)
static

Definition at line 1175 of file deparse.c.

References ADD_REL_QUALIFIER, appendStringInfoString(), bms_is_member(), deparseColumnRef(), FirstLowInvalidHeapAttributeNumber, i, lappend_int(), TupleDescData::natts, NIL, RelationGetDescr, SelfItemPointerAttributeNumber, and TupleDescAttr.

Referenced by deparseColumnRef(), deparseReturningList(), and deparseSelectSql().

1183 {
1184  TupleDesc tupdesc = RelationGetDescr(rel);
1185  bool have_wholerow;
1186  bool first;
1187  int i;
1188 
1189  *retrieved_attrs = NIL;
1190 
1191  /* If there's a whole-row reference, we'll need all the columns. */
1193  attrs_used);
1194 
1195  first = true;
1196  for (i = 1; i <= tupdesc->natts; i++)
1197  {
1198  Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1);
1199 
1200  /* Ignore dropped attributes. */
1201  if (attr->attisdropped)
1202  continue;
1203 
1204  if (have_wholerow ||
1206  attrs_used))
1207  {
1208  if (!first)
1209  appendStringInfoString(buf, ", ");
1210  else if (is_returning)
1211  appendStringInfoString(buf, " RETURNING ");
1212  first = false;
1213 
1214  deparseColumnRef(buf, rtindex, i, rte, qualify_col);
1215 
1216  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1217  }
1218  }
1219 
1220  /*
1221  * Add ctid if needed. We currently don't support retrieving any other
1222  * system columns.
1223  */
1225  attrs_used))
1226  {
1227  if (!first)
1228  appendStringInfoString(buf, ", ");
1229  else if (is_returning)
1230  appendStringInfoString(buf, " RETURNING ");
1231  first = false;
1232 
1233  if (qualify_col)
1234  ADD_REL_QUALIFIER(buf, rtindex);
1235  appendStringInfoString(buf, "ctid");
1236 
1237  *retrieved_attrs = lappend_int(*retrieved_attrs,
1239  }
1240 
1241  /* Don't generate bad syntax if no undropped columns */
1242  if (first && !is_returning)
1243  appendStringInfoString(buf, "NULL");
1244 }
#define NIL
Definition: pg_list.h:65
#define RelationGetDescr(relation)
Definition: rel.h:495
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:108
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
List * lappend_int(List *list, int datum)
Definition: list.c:354
int i
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2216

◆ deparseTruncateSql()

void deparseTruncateSql ( StringInfo  buf,
List rels,
DropBehavior  behavior,
bool  restart_seqs 
)

Definition at line 2181 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), deparseRelation(), DROP_CASCADE, DROP_RESTRICT, lfirst, and list_head().

Referenced by postgresExecForeignTruncate().

2185 {
2186  ListCell *cell;
2187 
2188  appendStringInfoString(buf, "TRUNCATE ");
2189 
2190  foreach(cell, rels)
2191  {
2192  Relation rel = lfirst(cell);
2193 
2194  if (cell != list_head(rels))
2195  appendStringInfoString(buf, ", ");
2196 
2197  deparseRelation(buf, rel);
2198  }
2199 
2200  appendStringInfo(buf, " %s IDENTITY",
2201  restart_seqs ? "RESTART" : "CONTINUE");
2202 
2203  if (behavior == DROP_RESTRICT)
2204  appendStringInfoString(buf, " RESTRICT");
2205  else if (behavior == DROP_CASCADE)
2206  appendStringInfoString(buf, " CASCADE");
2207 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
#define lfirst(lc)
Definition: pg_list.h:169

◆ deparseUpdateSql()

void deparseUpdateSql ( StringInfo  buf,
RangeTblEntry rte,
Index  rtindex,
Relation  rel,
List targetAttrs,
List withCheckOptionList,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1828 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), attnum, deparseColumnRef(), deparseRelation(), deparseReturningList(), lfirst_int, TriggerDesc::trig_update_after_row, and RelationData::trigdesc.

Referenced by postgresPlanForeignModify().

1833 {
1834  AttrNumber pindex;
1835  bool first;
1836  ListCell *lc;
1837 
1838  appendStringInfoString(buf, "UPDATE ");
1839  deparseRelation(buf, rel);
1840  appendStringInfoString(buf, " SET ");
1841 
1842  pindex = 2; /* ctid is always the first param */
1843  first = true;
1844  foreach(lc, targetAttrs)
1845  {
1846  int attnum = lfirst_int(lc);
1847 
1848  if (!first)
1849  appendStringInfoString(buf, ", ");
1850  first = false;
1851 
1852  deparseColumnRef(buf, rtindex, attnum, rte, false);
1853  appendStringInfo(buf, " = $%d", pindex);
1854  pindex++;
1855  }
1856  appendStringInfoString(buf, " WHERE ctid = $1");
1857 
1858  deparseReturningList(buf, rte, rtindex, rel,
1859  rel->trigdesc && rel->trigdesc->trig_update_after_row,
1860  withCheckOptionList, returningList, retrieved_attrs);
1861 }
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2046
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define lfirst_int(lc)
Definition: pg_list.h:170
TriggerDesc * trigdesc
Definition: rel.h:115
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_update_after_row
Definition: reltrigger.h:62
int16 attnum
Definition: pg_attribute.h:83
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2344
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2216

◆ deparseVar()

static void deparseVar ( Var node,
deparse_expr_cxt context 
)
static

Definition at line 2481 of file deparse.c.

References appendStringInfo(), bms_is_member(), bms_membership(), BMS_MULTIPLE, deparse_expr_cxt::buf, deparseColumnRef(), equal(), is_subquery_var(), lappend(), lfirst, deparse_expr_cxt::params_list, planner_rt_fetch, printRemoteParam(), printRemotePlaceholder(), foreign_glob_cxt::relids, RelOptInfo::relids, deparse_expr_cxt::root, deparse_expr_cxt::scanrel, SUBQUERY_COL_ALIAS_PREFIX, SUBQUERY_REL_ALIAS_PREFIX, Var::varattno, Var::varlevelsup, Var::varno, Var::vartype, and Var::vartypmod.

Referenced by deparseExpr().

2482 {
2483  Relids relids = context->scanrel->relids;
2484  int relno;
2485  int colno;
2486 
2487  /* Qualify columns when multiple relations are involved. */
2488  bool qualify_col = (bms_membership(relids) == BMS_MULTIPLE);
2489 
2490  /*
2491  * If the Var belongs to the foreign relation that is deparsed as a
2492  * subquery, use the relation and column alias to the Var provided by the
2493  * subquery, instead of the remote name.
2494  */
2495  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2496  {
2497  appendStringInfo(context->buf, "%s%d.%s%d",
2499  SUBQUERY_COL_ALIAS_PREFIX, colno);
2500  return;
2501  }
2502 
2503  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2504  deparseColumnRef(context->buf, node->varno, node->varattno,
2505  planner_rt_fetch(node->varno, context->root),
2506  qualify_col);
2507  else
2508  {
2509  /* Treat like a Param */
2510  if (context->params_list)
2511  {
2512  int pindex = 0;
2513  ListCell *lc;
2514 
2515  /* find its index in params_list */
2516  foreach(lc, *context->params_list)
2517  {
2518  pindex++;
2519  if (equal(node, (Node *) lfirst(lc)))
2520  break;
2521  }
2522  if (lc == NULL)
2523  {
2524  /* not in list, so add it */
2525  pindex++;
2526  *context->params_list = lappend(*context->params_list, node);
2527  }
2528 
2529  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2530  }
2531  else
2532  {
2533  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2534  }
2535  }
2536 }
Index varlevelsup
Definition: primnodes.h:196
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
List ** params_list
Definition: deparse.c:103
Definition: nodes.h:539
AttrNumber varattno
Definition: primnodes.h:191
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3191
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:111
Oid vartype
Definition: primnodes.h:193
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:110
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:383
Relids relids
Definition: pathnodes.h:676
List * lappend(List *list, void *datum)
Definition: list.c:336
Index varno
Definition: primnodes.h:189
StringInfo buf
Definition: deparse.c:102
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3429
PlannerInfo * root
Definition: deparse.c:97
#define lfirst(lc)
Definition: pg_list.h:169
RelOptInfo * scanrel
Definition: deparse.c:99
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2216
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3217
int32 vartypmod
Definition: primnodes.h:194

◆ foreign_expr_walker()

static bool foreign_expr_walker ( Node node,
foreign_glob_cxt glob_cxt,
foreign_loc_cxt outer_cxt 
)
static

Definition at line 295 of file deparse.c.

References Aggref::aggcollid, Aggref::aggfilter, Aggref::aggfnoid, Aggref::aggorder, Aggref::aggsplit, AGGSPLIT_SIMPLE, RelabelType::arg, NullTest::arg, Aggref::args, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, ArrayExpr::array_collid, bms_is_member(), foreign_loc_cxt::collation, Const::constcollid, ArrayExpr::elements, TargetEntry::expr, exprType(), FDW_COLLATE_NONE, FDW_COLLATE_SAFE, FDW_COLLATE_UNSAFE, RelOptInfo::fdw_private, fe(), foreign_glob_cxt::foreignrel, FuncExpr::funccollid, FuncExpr::funcid, get_sortgroupref_tle(), TypeCacheEntry::gt_opr, Aggref::inputcollid, FuncExpr::inputcollid, OpExpr::inputcollid, ScalarArrayOpExpr::inputcollid, InvalidOid, is_shippable(), IS_UPPER_REL, IsA, lfirst, lookup_type_cache(), TypeCacheEntry::lt_opr, nodeTag, OidIsValid, OpExpr::opcollid, OpExpr::opno, ScalarArrayOpExpr::opno, PARAM_MULTIEXPR, Param::paramcollid, Param::paramkind, SubscriptingRef::refassgnexpr, SubscriptingRef::refcollid, SubscriptingRef::refexpr, SubscriptingRef::reflowerindexpr, SubscriptingRef::refupperindexpr, foreign_glob_cxt::relids, RelabelType::resultcollid, SelfItemPointerAttributeNumber, SortGroupClause::sortop, foreign_loc_cxt::state, T_Aggref, T_ArrayExpr, T_BoolExpr, T_Const, T_DistinctExpr, T_FuncExpr, T_List, T_NullTest, T_OpExpr, T_Param, T_RelabelType, T_ScalarArrayOpExpr, T_SubscriptingRef, T_Var, SortGroupClause::tleSortGroupRef, TYPECACHE_GT_OPR, TYPECACHE_LT_OPR, Var::varattno, Var::varcollid, Var::varlevelsup, and Var::varno.

Referenced by is_foreign_expr().

298 {
299  bool check_type = true;
300  PgFdwRelationInfo *fpinfo;
301  foreign_loc_cxt inner_cxt;
302  Oid collation;
304 
305  /* Need do nothing for empty subexpressions */
306  if (node == NULL)
307  return true;
308 
309  /* May need server info from baserel's fdw_private struct */
310  fpinfo = (PgFdwRelationInfo *) (glob_cxt->foreignrel->fdw_private);
311 
312  /* Set up inner_cxt for possible recursion to child nodes */
313  inner_cxt.collation = InvalidOid;
314  inner_cxt.state = FDW_COLLATE_NONE;
315 
316  switch (nodeTag(node))
317  {
318  case T_Var:
319  {
320  Var *var = (Var *) node;
321 
322  /*
323  * If the Var is from the foreign table, we consider its
324  * collation (if any) safe to use. If it is from another
325  * table, we treat its collation the same way as we would a
326  * Param's collation, ie it's not safe for it to have a
327  * non-default collation.
328  */
329  if (bms_is_member(var->varno, glob_cxt->relids) &&
330  var->varlevelsup == 0)
331  {
332  /* Var belongs to foreign table */
333 
334  /*
335  * System columns other than ctid should not be sent to
336  * the remote, since we don't make any effort to ensure
337  * that local and remote values match (tableoid, in
338  * particular, almost certainly doesn't match).
339  */
340  if (var->varattno < 0 &&
342  return false;
343 
344  /* Else check the collation */
345  collation = var->varcollid;
346  state = OidIsValid(collation) ? FDW_COLLATE_SAFE : FDW_COLLATE_NONE;
347  }
348  else
349  {
350  /* Var belongs to some other table */
351  collation = var->varcollid;
352  if (collation == InvalidOid ||
353  collation == DEFAULT_COLLATION_OID)
354  {
355  /*
356  * It's noncollatable, or it's safe to combine with a
357  * collatable foreign Var, so set state to NONE.
358  */
359  state = FDW_COLLATE_NONE;
360  }
361  else
362  {
363  /*
364  * Do not fail right away, since the Var might appear
365  * in a collation-insensitive context.
366  */
367  state = FDW_COLLATE_UNSAFE;
368  }
369  }
370  }
371  break;
372  case T_Const:
373  {
374  Const *c = (Const *) node;
375 
376  /*
377  * If the constant has nondefault collation, either it's of a
378  * non-builtin type, or it reflects folding of a CollateExpr.
379  * It's unsafe to send to the remote unless it's used in a
380  * non-collation-sensitive context.
381  */
382  collation = c->constcollid;
383  if (collation == InvalidOid ||
384  collation == DEFAULT_COLLATION_OID)
385  state = FDW_COLLATE_NONE;
386  else
387  state = FDW_COLLATE_UNSAFE;
388  }
389  break;
390  case T_Param:
391  {
392  Param *p = (Param *) node;
393 
394  /*
395  * If it's a MULTIEXPR Param, punt. We can't tell from here
396  * whether the referenced sublink/subplan contains any remote
397  * Vars; if it does, handling that is too complicated to
398  * consider supporting at present. Fortunately, MULTIEXPR
399  * Params are not reduced to plain PARAM_EXEC until the end of
400  * planning, so we can easily detect this case. (Normal
401  * PARAM_EXEC Params are safe to ship because their values
402  * come from somewhere else in the plan tree; but a MULTIEXPR
403  * references a sub-select elsewhere in the same targetlist,
404  * so we'd be on the hook to evaluate it somehow if we wanted
405  * to handle such cases as direct foreign updates.)
406  */
407  if (p->paramkind == PARAM_MULTIEXPR)
408  return false;
409 
410  /*
411  * Collation rule is same as for Consts and non-foreign Vars.
412  */
413  collation = p->paramcollid;
414  if (collation == InvalidOid ||
415  collation == DEFAULT_COLLATION_OID)
416  state = FDW_COLLATE_NONE;
417  else
418  state = FDW_COLLATE_UNSAFE;
419  }
420  break;
421  case T_SubscriptingRef:
422  {
423  SubscriptingRef *sr = (SubscriptingRef *) node;
424 
425  /* Assignment should not be in restrictions. */
426  if (sr->refassgnexpr != NULL)
427  return false;
428 
429  /*
430  * Recurse into the remaining subexpressions. The container
431  * subscripts will not affect collation of the SubscriptingRef
432  * result, so do those first and reset inner_cxt afterwards.
433  */
435  glob_cxt, &inner_cxt))
436  return false;
437  inner_cxt.collation = InvalidOid;
438  inner_cxt.state = FDW_COLLATE_NONE;
440  glob_cxt, &inner_cxt))
441  return false;
442  inner_cxt.collation = InvalidOid;
443  inner_cxt.state = FDW_COLLATE_NONE;
444  if (!foreign_expr_walker((Node *) sr->refexpr,
445  glob_cxt, &inner_cxt))
446  return false;
447 
448  /*
449  * Container subscripting typically yields same collation as
450  * refexpr's, but in case it doesn't, use same logic as for
451  * function nodes.
452  */
453  collation = sr->refcollid;
454  if (collation == InvalidOid)
455  state = FDW_COLLATE_NONE;
456  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
457  collation == inner_cxt.collation)
458  state = FDW_COLLATE_SAFE;
459  else if (collation == DEFAULT_COLLATION_OID)
460  state = FDW_COLLATE_NONE;
461  else
462  state = FDW_COLLATE_UNSAFE;
463  }
464  break;
465  case T_FuncExpr:
466  {
467  FuncExpr *fe = (FuncExpr *) node;
468 
469  /*
470  * If function used by the expression is not shippable, it
471  * can't be sent to remote because it might have incompatible
472  * semantics on remote side.
473  */
474  if (!is_shippable(fe->funcid, ProcedureRelationId, fpinfo))
475  return false;
476 
477  /*
478  * Recurse to input subexpressions.
479  */
480  if (!foreign_expr_walker((Node *) fe->args,
481  glob_cxt, &inner_cxt))
482  return false;
483 
484  /*
485  * If function's input collation is not derived from a foreign
486  * Var, it can't be sent to remote.
487  */
488  if (fe->inputcollid == InvalidOid)
489  /* OK, inputs are all noncollatable */ ;
490  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
491  fe->inputcollid != inner_cxt.collation)
492  return false;
493 
494  /*
495  * Detect whether node is introducing a collation not derived
496  * from a foreign Var. (If so, we just mark it unsafe for now
497  * rather than immediately returning false, since the parent
498  * node might not care.)
499  */
500  collation = fe->funccollid;
501  if (collation == InvalidOid)
502  state = FDW_COLLATE_NONE;
503  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
504  collation == inner_cxt.collation)
505  state = FDW_COLLATE_SAFE;
506  else if (collation == DEFAULT_COLLATION_OID)
507  state = FDW_COLLATE_NONE;
508  else
509  state = FDW_COLLATE_UNSAFE;
510  }
511  break;
512  case T_OpExpr:
513  case T_DistinctExpr: /* struct-equivalent to OpExpr */
514  {
515  OpExpr *oe = (OpExpr *) node;
516 
517  /*
518  * Similarly, only shippable operators can be sent to remote.
519  * (If the operator is shippable, we assume its underlying
520  * function is too.)
521  */
522  if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
523  return false;
524 
525  /*
526  * Recurse to input subexpressions.
527  */
528  if (!foreign_expr_walker((Node *) oe->args,
529  glob_cxt, &inner_cxt))
530  return false;
531 
532  /*
533  * If operator's input collation is not derived from a foreign
534  * Var, it can't be sent to remote.
535  */
536  if (oe->inputcollid == InvalidOid)
537  /* OK, inputs are all noncollatable */ ;
538  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
539  oe->inputcollid != inner_cxt.collation)
540  return false;
541 
542  /* Result-collation handling is same as for functions */
543  collation = oe->opcollid;
544  if (collation == InvalidOid)
545  state = FDW_COLLATE_NONE;
546  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
547  collation == inner_cxt.collation)
548  state = FDW_COLLATE_SAFE;
549  else if (collation == DEFAULT_COLLATION_OID)
550  state = FDW_COLLATE_NONE;
551  else
552  state = FDW_COLLATE_UNSAFE;
553  }
554  break;
555  case T_ScalarArrayOpExpr:
556  {
557  ScalarArrayOpExpr *oe = (ScalarArrayOpExpr *) node;
558 
559  /*
560  * Again, only shippable operators can be sent to remote.
561  */
562  if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
563  return false;
564 
565  /*
566  * Recurse to input subexpressions.
567  */
568  if (!foreign_expr_walker((Node *) oe->args,
569  glob_cxt, &inner_cxt))
570  return false;
571 
572  /*
573  * If operator's input collation is not derived from a foreign
574  * Var, it can't be sent to remote.
575  */
576  if (oe->inputcollid == InvalidOid)
577  /* OK, inputs are all noncollatable */ ;
578  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
579  oe->inputcollid != inner_cxt.collation)
580  return false;
581 
582  /* Output is always boolean and so noncollatable. */
583  collation = InvalidOid;
584  state = FDW_COLLATE_NONE;
585  }
586  break;
587  case T_RelabelType:
588  {
589  RelabelType *r = (RelabelType *) node;
590 
591  /*
592  * Recurse to input subexpression.
593  */
594  if (!foreign_expr_walker((Node *) r->arg,
595  glob_cxt, &inner_cxt))
596  return false;
597 
598  /*
599  * RelabelType must not introduce a collation not derived from
600  * an input foreign Var (same logic as for a real function).
601  */
602  collation = r->resultcollid;
603  if (collation == InvalidOid)
604  state = FDW_COLLATE_NONE;
605  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
606  collation == inner_cxt.collation)
607  state = FDW_COLLATE_SAFE;
608  else if (collation == DEFAULT_COLLATION_OID)
609  state = FDW_COLLATE_NONE;
610  else
611  state = FDW_COLLATE_UNSAFE;
612  }
613  break;
614  case T_BoolExpr:
615  {
616  BoolExpr *b = (BoolExpr *) node;
617 
618  /*
619  * Recurse to input subexpressions.
620  */
621  if (!foreign_expr_walker((Node *) b->args,
622  glob_cxt, &inner_cxt))
623  return false;
624 
625  /* Output is always boolean and so noncollatable. */
626  collation = InvalidOid;
627  state = FDW_COLLATE_NONE;
628  }
629  break;
630  case T_NullTest:
631  {
632  NullTest *nt = (NullTest *) node;
633 
634  /*
635  * Recurse to input subexpressions.
636  */
637  if (!foreign_expr_walker((Node *) nt->arg,
638  glob_cxt, &inner_cxt))
639  return false;
640 
641  /* Output is always boolean and so noncollatable. */
642  collation = InvalidOid;
643  state = FDW_COLLATE_NONE;
644  }
645  break;
646  case T_ArrayExpr:
647  {
648  ArrayExpr *a = (ArrayExpr *) node;
649 
650  /*
651  * Recurse to input subexpressions.
652  */
653  if (!foreign_expr_walker((Node *) a->elements,
654  glob_cxt, &inner_cxt))
655  return false;
656 
657  /*
658  * ArrayExpr must not introduce a collation not derived from
659  * an input foreign Var (same logic as for a function).
660  */
661  collation = a->array_collid;
662  if (collation == InvalidOid)
663  state = FDW_COLLATE_NONE;
664  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
665  collation == inner_cxt.collation)
666  state = FDW_COLLATE_SAFE;
667  else if (collation == DEFAULT_COLLATION_OID)
668  state = FDW_COLLATE_NONE;
669  else
670  state = FDW_COLLATE_UNSAFE;
671  }
672  break;
673  case T_List:
674  {
675  List *l = (List *) node;
676  ListCell *lc;
677 
678  /*
679  * Recurse to component subexpressions.
680  */
681  foreach(lc, l)
682  {
683  if (!foreign_expr_walker((Node *) lfirst(lc),
684  glob_cxt, &inner_cxt))
685  return false;
686  }
687 
688  /*
689  * When processing a list, collation state just bubbles up
690  * from the list elements.
691  */
692  collation = inner_cxt.collation;
693  state = inner_cxt.state;
694 
695  /* Don't apply exprType() to the list. */
696  check_type = false;
697  }
698  break;
699  case T_Aggref:
700  {
701  Aggref *agg = (Aggref *) node;
702  ListCell *lc;
703 
704  /* Not safe to pushdown when not in grouping context */
705  if (!IS_UPPER_REL(glob_cxt->foreignrel))
706  return false;
707 
708  /* Only non-split aggregates are pushable. */
709  if (agg->aggsplit != AGGSPLIT_SIMPLE)
710  return false;
711 
712  /* As usual, it must be shippable. */
713  if (!is_shippable(agg->aggfnoid, ProcedureRelationId, fpinfo))
714  return false;
715 
716  /*
717  * Recurse to input args. aggdirectargs, aggorder and
718  * aggdistinct are all present in args, so no need to check
719  * their shippability explicitly.
720  */
721  foreach(lc, agg->args)
722  {
723  Node *n = (Node *) lfirst(lc);
724 
725  /* If TargetEntry, extract the expression from it */
726  if (IsA(n, TargetEntry))
727  {
728  TargetEntry *tle = (TargetEntry *) n;
729 
730  n = (Node *) tle->expr;
731  }
732 
733  if (!foreign_expr_walker(n, glob_cxt, &inner_cxt))
734  return false;
735  }
736 
737  /*
738  * For aggorder elements, check whether the sort operator, if
739  * specified, is shippable or not.
740  */
741  if (agg->aggorder)
742  {
743  ListCell *lc;
744 
745  foreach(lc, agg->aggorder)
746  {
747  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
748  Oid sortcoltype;
749  TypeCacheEntry *typentry;
750  TargetEntry *tle;
751 
753  agg->args);
754  sortcoltype = exprType((Node *) tle->expr);
755  typentry = lookup_type_cache(sortcoltype,
757  /* Check shippability of non-default sort operator. */
758  if (srt->sortop != typentry->lt_opr &&
759  srt->sortop != typentry->gt_opr &&
760  !is_shippable(srt->sortop, OperatorRelationId,
761  fpinfo))
762  return false;
763  }
764  }
765 
766  /* Check aggregate filter */
767  if (!foreign_expr_walker((Node *) agg->aggfilter,
768  glob_cxt, &inner_cxt))
769  return false;
770 
771  /*
772  * If aggregate's input collation is not derived from a
773  * foreign Var, it can't be sent to remote.
774  */
775  if (agg->inputcollid == InvalidOid)
776  /* OK, inputs are all noncollatable */ ;
777  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
778  agg->inputcollid != inner_cxt.collation)
779  return false;
780 
781  /*
782  * Detect whether node is introducing a collation not derived
783  * from a foreign Var. (If so, we just mark it unsafe for now
784  * rather than immediately returning false, since the parent
785  * node might not care.)
786  */
787  collation = agg->aggcollid;
788  if (collation == InvalidOid)
789  state = FDW_COLLATE_NONE;
790  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
791  collation == inner_cxt.collation)
792  state = FDW_COLLATE_SAFE;
793  else if (collation == DEFAULT_COLLATION_OID)
794  state = FDW_COLLATE_NONE;
795  else
796  state = FDW_COLLATE_UNSAFE;
797  }
798  break;
799  default:
800 
801  /*
802  * If it's anything else, assume it's unsafe. This list can be
803  * expanded later, but don't forget to add deparse support below.
804  */
805  return false;
806  }
807 
808  /*
809  * If result type of given expression is not shippable, it can't be sent
810  * to remote because it might have incompatible semantics on remote side.
811  */
812  if (check_type && !is_shippable(exprType(node), TypeRelationId, fpinfo))
813  return false;
814 
815  /*
816  * Now, merge my collation information into my parent's state.
817  */
818  if (state > outer_cxt->state)
819  {
820  /* Override previous parent state */
821  outer_cxt->collation = collation;
822  outer_cxt->state = state;
823  }
824  else if (state == outer_cxt->state)
825  {
826  /* Merge, or detect error if there's a collation conflict */
827  switch (state)
828  {
829  case FDW_COLLATE_NONE:
830  /* Nothing + nothing is still nothing */
831  break;
832  case FDW_COLLATE_SAFE:
833  if (collation != outer_cxt->collation)
834  {
835  /*
836  * Non-default collation always beats default.
837  */
838  if (outer_cxt->collation == DEFAULT_COLLATION_OID)
839  {
840  /* Override previous parent state */
841  outer_cxt->collation = collation;
842  }
843  else if (collation != DEFAULT_COLLATION_OID)
844  {
845  /*
846  * Conflict; show state as indeterminate. We don't
847  * want to "return false" right away, since parent
848  * node might not care about collation.
849  */
850  outer_cxt->state = FDW_COLLATE_UNSAFE;
851  }
852  }
853  break;
854  case FDW_COLLATE_UNSAFE:
855  /* We're still conflicted ... */
856  break;
857  }
858  }
859 
860  /* It looks OK */
861  return true;
862 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
List * args
Definition: primnodes.h:503
Oid funccollid
Definition: primnodes.h:501
FDWCollateState state
Definition: deparse.c:89
Index tleSortGroupRef
Definition: parsenodes.h:1283
Expr * arg
Definition: primnodes.h:850
Oid inputcollid
Definition: primnodes.h:326
ParamKind paramkind
Definition: primnodes.h:267
Definition: nodes.h:539
List * args
Definition: primnodes.h:330
AttrNumber varattno
Definition: primnodes.h:191
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:186
List * refupperindexpr
Definition: primnodes.h:444
bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
Definition: shippable.c:162
#define OidIsValid(objectId)
Definition: c.h:710
FDWCollateState
Definition: deparse.c:76
Oid funcid
Definition: primnodes.h:495
Oid paramcollid
Definition: primnodes.h:271
Expr * arg
Definition: primnodes.h:1255
Oid constcollid
Definition: primnodes.h:217
Relids relids
Definition: deparse.c:68
char * c
List * aggorder
Definition: primnodes.h:331
RelOptInfo * foreignrel
Definition: deparse.c:67
List * elements
Definition: primnodes.h:1027
#define TYPECACHE_GT_OPR
Definition: typcache.h:138
static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt)
Definition: deparse.c:295
Oid opcollid
Definition: primnodes.h:546
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:334
Definition: nodes.h:305
Definition: nodes.h:158
Index varno
Definition: primnodes.h:189
Definition: nodes.h:157
Oid resultcollid
Definition: primnodes.h:853
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:338
#define InvalidOid
Definition: postgres_ext.h:36
Oid aggfnoid
Definition: primnodes.h:323
void * fdw_private
Definition: pathnodes.h:732
Oid inputcollid
Definition: primnodes.h:502
#define lfirst(lc)
Definition: pg_list.h:169
Definition: regguts.h:317
Expr * expr
Definition: primnodes.h:1444
AggSplit aggsplit
Definition: primnodes.h:339
Oid array_collid
Definition: primnodes.h:1025
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Expr * aggfilter
Definition: primnodes.h:333
Oid inputcollid
Definition: primnodes.h:547
List * args
Definition: primnodes.h:616
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
#define nodeTag(nodeptr)
Definition: nodes.h:544
Expr * refassgnexpr
Definition: primnodes.h:451
Oid aggcollid
Definition: primnodes.h:325
static int fe(enum e x)
Definition: preproc-init.c:111
List * reflowerindexpr
Definition: primnodes.h:446
#define TYPECACHE_LT_OPR
Definition: typcache.h:137
Oid varcollid
Definition: primnodes.h:195
Expr * refexpr
Definition: primnodes.h:449
Oid opno
Definition: primnodes.h:542
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
List * args
Definition: primnodes.h:548
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Definition: nodes.h:159

◆ get_jointype_name()

const char* get_jointype_name ( JoinType  jointype)

Definition at line 1375 of file deparse.c.

References elog, ERROR, JOIN_FULL, JOIN_INNER, JOIN_LEFT, and JOIN_RIGHT.

Referenced by deparseFromExprForRel(), and foreign_join_ok().

1376 {
1377  switch (jointype)
1378  {
1379  case JOIN_INNER:
1380  return "INNER";
1381 
1382  case JOIN_LEFT:
1383  return "LEFT";
1384 
1385  case JOIN_RIGHT:
1386  return "RIGHT";
1387 
1388  case JOIN_FULL:
1389  return "FULL";
1390 
1391  default:
1392  /* Shouldn't come here, but protect from buggy code. */
1393  elog(ERROR, "unsupported join type %d", jointype);
1394  }
1395 
1396  /* Keep compiler happy */
1397  return NULL;
1398 }
#define ERROR
Definition: elog.h:46
#define elog(elevel,...)
Definition: elog.h:232

◆ get_relation_column_alias_ids()

static void get_relation_column_alias_ids ( Var node,
RelOptInfo foreignrel,
int *  relno,
int *  colno 
)
static

Definition at line 3491 of file deparse.c.

References elog, equal(), ERROR, PathTarget::exprs, RelOptInfo::fdw_private, i, lfirst, PgFdwRelationInfo::relation_index, and RelOptInfo::reltarget.

Referenced by is_subquery_var().

3493 {
3494  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3495  int i;
3496  ListCell *lc;
3497 
3498  /* Get the relation alias ID */
3499  *relno = fpinfo->relation_index;
3500 
3501  /* Get the column alias ID */
3502  i = 1;
3503  foreach(lc, foreignrel->reltarget->exprs)
3504  {
3505  if (equal(lfirst(lc), (Node *) node))
3506  {
3507  *colno = i;
3508  return;
3509  }
3510  i++;
3511  }
3512 
3513  /* Shouldn't get here */
3514  elog(ERROR, "unexpected expression in subquery output");
3515 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
Definition: nodes.h:539
#define ERROR
Definition: elog.h:46
List * exprs
Definition: pathnodes.h:1102
void * fdw_private
Definition: pathnodes.h:732
#define lfirst(lc)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:232
int i
struct PathTarget * reltarget
Definition: pathnodes.h:687

◆ is_foreign_expr()

bool is_foreign_expr ( PlannerInfo root,
RelOptInfo baserel,
Expr expr 
)

Definition at line 231 of file deparse.c.

References foreign_loc_cxt::collation, contain_mutable_functions(), FDW_COLLATE_NONE, FDW_COLLATE_UNSAFE, RelOptInfo::fdw_private, foreign_expr_walker(), foreign_glob_cxt::foreignrel, InvalidOid, IS_UPPER_REL, PgFdwRelationInfo::outerrel, foreign_glob_cxt::relids, RelOptInfo::relids, foreign_glob_cxt::root, and foreign_loc_cxt::state.

Referenced by add_foreign_final_paths(), add_foreign_ordered_paths(), classifyConditions(), foreign_grouping_ok(), foreign_join_ok(), get_useful_pathkeys_for_relation(), postgresGetForeignPaths(), postgresGetForeignPlan(), and postgresPlanDirectModify().

234 {
235  foreign_glob_cxt glob_cxt;
236  foreign_loc_cxt loc_cxt;
237  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
238 
239  /*
240  * Check that the expression consists of nodes that are safe to execute
241  * remotely.
242  */
243  glob_cxt.root = root;
244  glob_cxt.foreignrel = baserel;
245 
246  /*
247  * For an upper relation, use relids from its underneath scan relation,
248  * because the upperrel's own relids currently aren't set to anything
249  * meaningful by the core code. For other relation, use their own relids.
250  */
251  if (IS_UPPER_REL(baserel))
252  glob_cxt.relids = fpinfo->outerrel->relids;
253  else
254  glob_cxt.relids = baserel->relids;
255  loc_cxt.collation = InvalidOid;
256  loc_cxt.state = FDW_COLLATE_NONE;
257  if (!foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt))
258  return false;
259 
260  /*
261  * If the expression has a valid collation that does not arise from a
262  * foreign var, the expression can not be sent over.
263  */
264  if (loc_cxt.state == FDW_COLLATE_UNSAFE)
265  return false;
266 
267  /*
268  * An expression which includes any mutable functions can't be sent over
269  * because its result is not stable. For example, sending now() remote
270  * side could cause confusion from clock offsets. Future versions might
271  * be able to make this choice with more granularity. (We check this last
272  * because it requires a lot of expensive catalog lookups.)
273  */
274  if (contain_mutable_functions((Node *) expr))
275  return false;
276 
277  /* OK to evaluate on the remote server */
278  return true;
279 }
FDWCollateState state
Definition: deparse.c:89
Definition: nodes.h:539
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
Relids relids
Definition: deparse.c:68
PlannerInfo * root
Definition: deparse.c:66
RelOptInfo * foreignrel
Definition: deparse.c:67
Relids relids
Definition: pathnodes.h:676
static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt)
Definition: deparse.c:295
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:732
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:359

◆ is_foreign_param()

bool is_foreign_param ( PlannerInfo root,
RelOptInfo baserel,
Expr expr 
)

Definition at line 877 of file deparse.c.

References bms_is_member(), RelOptInfo::fdw_private, IS_UPPER_REL, nodeTag, PgFdwRelationInfo::outerrel, foreign_glob_cxt::relids, RelOptInfo::relids, T_Param, T_Var, Var::varlevelsup, and Var::varno.

Referenced by foreign_grouping_ok().

880 {
881  if (expr == NULL)
882  return false;
883 
884  switch (nodeTag(expr))
885  {
886  case T_Var:
887  {
888  /* It would have to be sent unless it's a foreign Var */
889  Var *var = (Var *) expr;
890  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
891  Relids relids;
892 
893  if (IS_UPPER_REL(baserel))
894  relids = fpinfo->outerrel->relids;
895  else
896  relids = baserel->relids;
897 
898  if (bms_is_member(var->varno, relids) && var->varlevelsup == 0)
899  return false; /* foreign Var, so not a param */
900  else
901  return true; /* it'd have to be a param */
902  break;
903  }
904  case T_Param:
905  /* Params always have to be sent to the foreign server */
906  return true;
907  default:
908  break;
909  }
910  return false;
911 }
Index varlevelsup
Definition: primnodes.h:196
Definition: primnodes.h:186
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
Relids relids
Definition: pathnodes.h:676
Index varno
Definition: primnodes.h:189
Definition: nodes.h:157
void * fdw_private
Definition: pathnodes.h:732
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:659
#define nodeTag(nodeptr)
Definition: nodes.h:544
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Definition: nodes.h:159

◆ is_subquery_var()

static bool is_subquery_var ( Var node,
RelOptInfo foreignrel,
int *  relno,
int *  colno 
)
static

Definition at line 3429 of file deparse.c.

References Assert, bms_is_member(), RelOptInfo::fdw_private, get_relation_column_alias_ids(), PgFdwRelationInfo::innerrel, IS_JOIN_REL, IS_SIMPLE_REL, PgFdwRelationInfo::lower_subquery_rels, PgFdwRelationInfo::make_innerrel_subquery, PgFdwRelationInfo::make_outerrel_subquery, PgFdwRelationInfo::outerrel, and Var::varno.

Referenced by deparseVar().

3430 {
3431  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3432  RelOptInfo *outerrel = fpinfo->outerrel;
3433  RelOptInfo *innerrel = fpinfo->innerrel;
3434 
3435  /* Should only be called in these cases. */
3436  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
3437 
3438  /*
3439  * If the given relation isn't a join relation, it doesn't have any lower
3440  * subqueries, so the Var isn't a subquery output column.
3441  */
3442  if (!IS_JOIN_REL(foreignrel))
3443  return false;
3444 
3445  /*
3446  * If the Var doesn't belong to any lower subqueries, it isn't a subquery
3447  * output column.
3448  */
3449  if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels))
3450  return false;
3451 
3452  if (bms_is_member(node->varno, outerrel->relids))
3453  {
3454  /*
3455  * If outer relation is deparsed as a subquery, the Var is an output
3456  * column of the subquery; get the IDs for the relation/column alias.
3457  */
3458  if (fpinfo->make_outerrel_subquery)
3459  {
3460  get_relation_column_alias_ids(node, outerrel, relno, colno);
3461  return true;
3462  }
3463 
3464  /* Otherwise, recurse into the outer relation. */
3465  return is_subquery_var(node, outerrel, relno, colno);
3466  }
3467  else
3468  {
3469  Assert(bms_is_member(node->varno, innerrel->relids));
3470 
3471  /*
3472  * If inner relation is deparsed as a subquery, the Var is an output
3473  * column of the subquery; get the IDs for the relation/column alias.
3474  */
3475  if (fpinfo->make_innerrel_subquery)
3476  {
3477  get_relation_column_alias_ids(node, innerrel, relno, colno);
3478  return true;
3479  }
3480 
3481  /* Otherwise, recurse into the inner relation. */
3482  return is_subquery_var(node, innerrel, relno, colno);
3483  }
3484 }
Relids lower_subquery_rels
Definition: postgres_fdw.h:119
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:654
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:649
Index varno
Definition: primnodes.h:189
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3429
void * fdw_private
Definition: pathnodes.h:732
static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3491
#define Assert(condition)
Definition: c.h:804
RelOptInfo * innerrel
Definition: postgres_fdw.h:103
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ printRemoteParam()

static void printRemoteParam ( int  paramindex,
Oid  paramtype,
int32  paramtypmod,
deparse_expr_cxt context 
)
static

Definition at line 3191 of file deparse.c.

References appendStringInfo(), buf, deparse_expr_cxt::buf, and deparse_type_name().

Referenced by deparseParam(), and deparseVar().

3193 {
3194  StringInfo buf = context->buf;
3195  char *ptypename = deparse_type_name(paramtype, paramtypmod);
3196 
3197  appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
3198 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:925

◆ printRemotePlaceholder()

<
static void printRemotePlaceholder ( Oid  paramtype,
int32  paramtypmod,
deparse_expr_cxt context 
)