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 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)
 
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 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 107 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 110 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

◆ SUBQUERY_REL_ALIAS_PREFIX

#define SUBQUERY_REL_ALIAS_PREFIX   "s"

Definition at line 109 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 75 of file deparse.c.

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

Function Documentation

◆ appendAggOrderBy()

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

Definition at line 3047 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().

3048 {
3049  StringInfo buf = context->buf;
3050  ListCell *lc;
3051  bool first = true;
3052 
3053  foreach(lc, orderList)
3054  {
3055  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
3056  Node *sortexpr;
3057  Oid sortcoltype;
3058  TypeCacheEntry *typentry;
3059 
3060  if (!first)
3061  appendStringInfoString(buf, ", ");
3062  first = false;
3063 
3064  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
3065  false, context);
3066  sortcoltype = exprType(sortexpr);
3067  /* See whether operator is default < or > for datatype */
3068  typentry = lookup_type_cache(sortcoltype,
3070  if (srt->sortop == typentry->lt_opr)
3071  appendStringInfoString(buf, " ASC");
3072  else if (srt->sortop == typentry->gt_opr)
3073  appendStringInfoString(buf, " DESC");
3074  else
3075  {
3076  HeapTuple opertup;
3077  Form_pg_operator operform;
3078 
3079  appendStringInfoString(buf, " USING ");
3080 
3081  /* Append operator name. */
3082  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
3083  if (!HeapTupleIsValid(opertup))
3084  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
3085  operform = (Form_pg_operator) GETSTRUCT(opertup);
3086  deparseOperatorName(buf, operform);
3087  ReleaseSysCache(opertup);
3088  }
3089 
3090  if (srt->nulls_first)
3091  appendStringInfoString(buf, " NULLS FIRST");
3092  else
3093  appendStringInfoString(buf, " NULLS LAST");
3094  }
3095 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3299
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Index tleSortGroupRef
Definition: parsenodes.h:1257
Definition: nodes.h:529
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2754
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
#define TYPECACHE_GT_OPR
Definition: typcache.h:132
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
StringInfo buf
Definition: deparse.c:101
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:331
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:190
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:214
#define TYPECACHE_LT_OPR
Definition: typcache.h:131

◆ appendConditions()

static void appendConditions ( List exprs,
deparse_expr_cxt context 
)
static

Definition at line 1335 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().

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

◆ appendFunctionName()

static void appendFunctionName ( Oid  funcid,
deparse_expr_cxt context 
)
static

Definition at line 3264 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().

3265 {
3266  StringInfo buf = context->buf;
3267  HeapTuple proctup;
3268  Form_pg_proc procform;
3269  const char *proname;
3270 
3271  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3272  if (!HeapTupleIsValid(proctup))
3273  elog(ERROR, "cache lookup failed for function %u", funcid);
3274  procform = (Form_pg_proc) GETSTRUCT(proctup);
3275 
3276  /* Print schema name only if it's not pg_catalog */
3277  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3278  {
3279  const char *schemaname;
3280 
3281  schemaname = get_namespace_name(procform->pronamespace);
3282  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3283  }
3284 
3285  /* Always print the function name */
3286  proname = NameStr(procform->proname);
3288 
3289  ReleaseSysCache(proctup);
3290 }
NameData proname
Definition: pg_proc.h:35
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10727
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
static char * buf
Definition: pg_test_fsync.c:67
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
StringInfo buf
Definition: deparse.c:101
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:615

◆ appendGroupByClause()

static void appendGroupByClause ( List tlist,
deparse_expr_cxt context 
)
static

Definition at line 3145 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().

3146 {
3147  StringInfo buf = context->buf;
3148  Query *query = context->root->parse;
3149  ListCell *lc;
3150  bool first = true;
3151 
3152  /* Nothing to be done, if there's no GROUP BY clause in the query. */
3153  if (!query->groupClause)
3154  return;
3155 
3156  appendStringInfoString(buf, " GROUP BY ");
3157 
3158  /*
3159  * Queries with grouping sets are not pushed down, so we don't expect
3160  * grouping sets here.
3161  */
3162  Assert(!query->groupingSets);
3163 
3164  foreach(lc, query->groupClause)
3165  {
3166  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
3167 
3168  if (!first)
3169  appendStringInfoString(buf, ", ");
3170  first = false;
3171 
3172  deparseSortGroupClause(grp->tleSortGroupRef, tlist, true, context);
3173  }
3174 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3299
Query * parse
Definition: pathnodes.h:179
Index tleSortGroupRef
Definition: parsenodes.h:1257
List * groupingSets
Definition: parsenodes.h:150
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
StringInfo buf
Definition: deparse.c:101
PlannerInfo * root
Definition: deparse.c:96
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * groupClause
Definition: parsenodes.h:148

◆ appendLimitClause()

static void appendLimitClause ( deparse_expr_cxt context)
static

Definition at line 3236 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().

3237 {
3238  PlannerInfo *root = context->root;
3239  StringInfo buf = context->buf;
3240  int nestlevel;
3241 
3242  /* Make sure any constants in the exprs are printed portably */
3243  nestlevel = set_transmission_modes();
3244 
3245  if (root->parse->limitCount)
3246  {
3247  appendStringInfoString(buf, " LIMIT ");
3248  deparseExpr((Expr *) root->parse->limitCount, context);
3249  }
3250  if (root->parse->limitOffset)
3251  {
3252  appendStringInfoString(buf, " OFFSET ");
3253  deparseExpr((Expr *) root->parse->limitOffset, context);
3254  }
3255 
3256  reset_transmission_modes(nestlevel);
3257 }
Node * limitOffset
Definition: parsenodes.h:160
Query * parse
Definition: pathnodes.h:179
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Node * limitCount
Definition: parsenodes.h:161
static char * buf
Definition: pg_test_fsync.c:67
StringInfo buf
Definition: deparse.c:101
PlannerInfo * root
Definition: deparse.c:96
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327

◆ appendOrderByClause()

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

Definition at line 3182 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().

3184 {
3185  ListCell *lcell;
3186  int nestlevel;
3187  char *delim = " ";
3188  RelOptInfo *baserel = context->scanrel;
3189  StringInfo buf = context->buf;
3190 
3191  /* Make sure any constants in the exprs are printed portably */
3192  nestlevel = set_transmission_modes();
3193 
3194  appendStringInfoString(buf, " ORDER BY");
3195  foreach(lcell, pathkeys)
3196  {
3197  PathKey *pathkey = lfirst(lcell);
3198  Expr *em_expr;
3199 
3200  if (has_final_sort)
3201  {
3202  /*
3203  * By construction, context->foreignrel is the input relation to
3204  * the final sort.
3205  */
3206  em_expr = find_em_expr_for_input_target(context->root,
3207  pathkey->pk_eclass,
3208  context->foreignrel->reltarget);
3209  }
3210  else
3211  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
3212 
3213  Assert(em_expr != NULL);
3214 
3215  appendStringInfoString(buf, delim);
3216  deparseExpr(em_expr, context);
3217  if (pathkey->pk_strategy == BTLessStrategyNumber)
3218  appendStringInfoString(buf, " ASC");
3219  else
3220  appendStringInfoString(buf, " DESC");
3221 
3222  if (pathkey->pk_nulls_first)
3223  appendStringInfoString(buf, " NULLS FIRST");
3224  else
3225  appendStringInfoString(buf, " NULLS LAST");
3226 
3227  delim = ", ";
3228  }
3229  reset_transmission_modes(nestlevel);
3230 }
int set_transmission_modes(void)
int pk_strategy
Definition: pathnodes.h:1043
Expr * find_em_expr_for_input_target(PlannerInfo *root, EquivalenceClass *ec, PathTarget *target)
bool pk_nulls_first
Definition: pathnodes.h:1044
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
StringInfo buf
Definition: deparse.c:101
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
Definition: equivclass.c:782
PlannerInfo * root
Definition: deparse.c:96
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1041
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
RelOptInfo * scanrel
Definition: deparse.c:98
#define BTLessStrategyNumber
Definition: stratnum.h:29
RelOptInfo * foreignrel
Definition: deparse.c:97
struct PathTarget * reltarget
Definition: pathnodes.h:676

◆ build_tlist_to_deparse()

List* build_tlist_to_deparse ( RelOptInfo foreignrel)

Definition at line 938 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().

939 {
940  List *tlist = NIL;
941  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
942  ListCell *lc;
943 
944  /*
945  * For an upper relation, we have already built the target list while
946  * checking shippability, so just return that.
947  */
948  if (IS_UPPER_REL(foreignrel))
949  return fpinfo->grouped_tlist;
950 
951  /*
952  * We require columns specified in foreignrel->reltarget->exprs and those
953  * required for evaluating the local conditions.
954  */
955  tlist = add_to_flat_tlist(tlist,
956  pull_var_clause((Node *) foreignrel->reltarget->exprs,
958  foreach(lc, fpinfo->local_conds)
959  {
960  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
961 
962  tlist = add_to_flat_tlist(tlist,
963  pull_var_clause((Node *) rinfo->clause,
965  }
966 
967  return tlist;
968 }
#define NIL
Definition: pg_list.h:65
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:177
Definition: nodes.h:529
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
#define lfirst_node(type, lc)
Definition: pg_list.h:193
Expr * clause
Definition: pathnodes.h:1985
List * exprs
Definition: pathnodes.h:1074
void * fdw_private
Definition: pathnodes.h:719
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:154
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:648
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:676

◆ classifyConditions()

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

Definition at line 204 of file deparse.c.

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

Referenced by estimate_path_cost_size(), and postgresGetForeignRelSize().

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

◆ deparse_type_name()

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

Definition at line 919 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().

920 {
922 
923  if (!is_builtin(type_oid))
924  flags |= FORMAT_TYPE_FORCE_QUALIFY;
925 
926  return format_type_extended(type_oid, typemod, flags);
927 }
bool is_builtin(Oid objectId)
Definition: shippable.c:153
uint16 bits16
Definition: c.h:375
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:113
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:115
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:111

◆ deparseAggref()

static void deparseAggref ( Aggref node,
deparse_expr_cxt context 
)
static

Definition at line 2955 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().

2956 {
2957  StringInfo buf = context->buf;
2958  bool use_variadic;
2959 
2960  /* Only basic, non-split aggregation accepted. */
2961  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
2962 
2963  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2964  use_variadic = node->aggvariadic;
2965 
2966  /* Find aggregate name from aggfnoid which is a pg_proc entry */
2967  appendFunctionName(node->aggfnoid, context);
2968  appendStringInfoChar(buf, '(');
2969 
2970  /* Add DISTINCT */
2971  appendStringInfoString(buf, (node->aggdistinct != NIL) ? "DISTINCT " : "");
2972 
2973  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
2974  {
2975  /* Add WITHIN GROUP (ORDER BY ..) */
2976  ListCell *arg;
2977  bool first = true;
2978 
2979  Assert(!node->aggvariadic);
2980  Assert(node->aggorder != NIL);
2981 
2982  foreach(arg, node->aggdirectargs)
2983  {
2984  if (!first)
2985  appendStringInfoString(buf, ", ");
2986  first = false;
2987 
2988  deparseExpr((Expr *) lfirst(arg), context);
2989  }
2990 
2991  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
2992  appendAggOrderBy(node->aggorder, node->args, context);
2993  }
2994  else
2995  {
2996  /* aggstar can be set only in zero-argument aggregates */
2997  if (node->aggstar)
2998  appendStringInfoChar(buf, '*');
2999  else
3000  {
3001  ListCell *arg;
3002  bool first = true;
3003 
3004  /* Add all the arguments */
3005  foreach(arg, node->args)
3006  {
3007  TargetEntry *tle = (TargetEntry *) lfirst(arg);
3008  Node *n = (Node *) tle->expr;
3009 
3010  if (tle->resjunk)
3011  continue;
3012 
3013  if (!first)
3014  appendStringInfoString(buf, ", ");
3015  first = false;
3016 
3017  /* Add VARIADIC */
3018  if (use_variadic && lnext(node->args, arg) == NULL)
3019  appendStringInfoString(buf, "VARIADIC ");
3020 
3021  deparseExpr((Expr *) n, context);
3022  }
3023  }
3024 
3025  /* Add ORDER BY */
3026  if (node->aggorder != NIL)
3027  {
3028  appendStringInfoString(buf, " ORDER BY ");
3029  appendAggOrderBy(node->aggorder, node->args, context);
3030  }
3031  }
3032 
3033  /* Add FILTER (WHERE ..) */
3034  if (node->aggfilter != NULL)
3035  {
3036  appendStringInfoString(buf, ") FILTER (WHERE ");
3037  deparseExpr((Expr *) node->aggfilter, context);
3038  }
3039 
3040  appendStringInfoChar(buf, ')');
3041 }
List * aggdistinct
Definition: primnodes.h:321
#define NIL
Definition: pg_list.h:65
bool aggvariadic
Definition: primnodes.h:324
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3264
Definition: nodes.h:529
List * args
Definition: primnodes.h:319
bool aggstar
Definition: primnodes.h:323
bool resjunk
Definition: primnodes.h:1414
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
List * aggorder
Definition: primnodes.h:320
List * aggdirectargs
Definition: primnodes.h:318
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
Definition: deparse.c:3047
StringInfo buf
Definition: deparse.c:101
Oid aggfnoid
Definition: primnodes.h:312
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1407
AggSplit aggsplit
Definition: primnodes.h:328
Expr * aggfilter
Definition: primnodes.h:322
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
void * arg
char aggkind
Definition: primnodes.h:326

◆ deparseAnalyzeSizeSql()

void deparseAnalyzeSizeSql ( StringInfo  buf,
Relation  rel 
)

Definition at line 2043 of file deparse.c.

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

Referenced by postgresAnalyzeForeignTable().

2044 {
2046 
2047  /* We'll need the remote relation name as a literal. */
2048  initStringInfo(&relname);
2049  deparseRelation(&relname, rel);
2050 
2051  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
2052  deparseStringLiteral(buf, relname.data);
2053  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
2054 }
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:2292
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2252

◆ deparseAnalyzeSql()

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

Definition at line 2063 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().

2064 {
2065  Oid relid = RelationGetRelid(rel);
2066  TupleDesc tupdesc = RelationGetDescr(rel);
2067  int i;
2068  char *colname;
2069  List *options;
2070  ListCell *lc;
2071  bool first = true;
2072 
2073  *retrieved_attrs = NIL;
2074 
2075  appendStringInfoString(buf, "SELECT ");
2076  for (i = 0; i < tupdesc->natts; i++)
2077  {
2078  /* Ignore dropped columns. */
2079  if (TupleDescAttr(tupdesc, i)->attisdropped)
2080  continue;
2081 
2082  if (!first)
2083  appendStringInfoString(buf, ", ");
2084  first = false;
2085 
2086  /* Use attribute name or column_name option. */
2087  colname = NameStr(TupleDescAttr(tupdesc, i)->attname);
2088  options = GetForeignColumnOptions(relid, i + 1);
2089 
2090  foreach(lc, options)
2091  {
2092  DefElem *def = (DefElem *) lfirst(lc);
2093 
2094  if (strcmp(def->defname, "column_name") == 0)
2095  {
2096  colname = defGetString(def);
2097  break;
2098  }
2099  }
2100 
2102 
2103  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
2104  }
2105 
2106  /* Don't generate bad syntax for zero-column relation. */
2107  if (first)
2108  appendStringInfoString(buf, "NULL");
2109 
2110  /*
2111  * Construct FROM clause
2112  */
2113  appendStringInfoString(buf, " FROM ");
2114  deparseRelation(buf, rel);
2115 }
#define NIL
Definition: pg_list.h:65
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10727
#define RelationGetDescr(relation)
Definition: rel.h:482
#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:40
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:339
static char ** options
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2252
#define lfirst(lc)
Definition: pg_list.h:190
int i
#define NameStr(name)
Definition: c.h:615
char * defname
Definition: parsenodes.h:732
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:456

◆ deparseArrayExpr()

static void deparseArrayExpr ( ArrayExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2929 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().

2930 {
2931  StringInfo buf = context->buf;
2932  bool first = true;
2933  ListCell *lc;
2934 
2935  appendStringInfoString(buf, "ARRAY[");
2936  foreach(lc, node->elements)
2937  {
2938  if (!first)
2939  appendStringInfoString(buf, ", ");
2940  deparseExpr(lfirst(lc), context);
2941  first = false;
2942  }
2943  appendStringInfoChar(buf, ']');
2944 
2945  /* If the array is empty, we need an explicit cast to the array type. */
2946  if (node->elements == NIL)
2947  appendStringInfo(buf, "::%s",
2948  deparse_type_name(node->array_typeid, -1));
2949 }
#define NIL
Definition: pg_list.h:65
Oid array_typeid
Definition: primnodes.h:988
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:67
List * elements
Definition: primnodes.h:991
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:919
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327

◆ deparseBoolExpr()

static void deparseBoolExpr ( BoolExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2858 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().

2859 {
2860  StringInfo buf = context->buf;
2861  const char *op = NULL; /* keep compiler quiet */
2862  bool first;
2863  ListCell *lc;
2864 
2865  switch (node->boolop)
2866  {
2867  case AND_EXPR:
2868  op = "AND";
2869  break;
2870  case OR_EXPR:
2871  op = "OR";
2872  break;
2873  case NOT_EXPR:
2874  appendStringInfoString(buf, "(NOT ");
2875  deparseExpr(linitial(node->args), context);
2876  appendStringInfoChar(buf, ')');
2877  return;
2878  }
2879 
2880  appendStringInfoChar(buf, '(');
2881  first = true;
2882  foreach(lc, node->args)
2883  {
2884  if (!first)
2885  appendStringInfo(buf, " %s ", op);
2886  deparseExpr((Expr *) lfirst(lc), context);
2887  first = false;
2888  }
2889  appendStringInfoChar(buf, ')');
2890 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:195
BoolExprType boolop
Definition: primnodes.h:582
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
#define lfirst(lc)
Definition: pg_list.h:190
List * args
Definition: primnodes.h:583
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327

◆ deparseColumnRef()

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

Definition at line 2124 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().

2126 {
2127  /* We support fetching the remote side's CTID and OID. */
2128  if (varattno == SelfItemPointerAttributeNumber)
2129  {
2130  if (qualify_col)
2131  ADD_REL_QUALIFIER(buf, varno);
2132  appendStringInfoString(buf, "ctid");
2133  }
2134  else if (varattno < 0)
2135  {
2136  /*
2137  * All other system attributes are fetched as 0, except for table OID,
2138  * which is fetched as the local table OID. However, we must be
2139  * careful; the table could be beneath an outer join, in which case it
2140  * must go to NULL whenever the rest of the row does.
2141  */
2142  Oid fetchval = 0;
2143 
2144  if (varattno == TableOidAttributeNumber)
2145  fetchval = rte->relid;
2146 
2147  if (qualify_col)
2148  {
2149  appendStringInfoString(buf, "CASE WHEN (");
2150  ADD_REL_QUALIFIER(buf, varno);
2151  appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval);
2152  }
2153  else
2154  appendStringInfo(buf, "%u", fetchval);
2155  }
2156  else if (varattno == 0)
2157  {
2158  /* Whole row reference */
2159  Relation rel;
2160  Bitmapset *attrs_used;
2161 
2162  /* Required only to be passed down to deparseTargetList(). */
2163  List *retrieved_attrs;
2164 
2165  /*
2166  * The lock on the relation will be held by upper callers, so it's
2167  * fine to open it with no lock here.
2168  */
2169  rel = table_open(rte->relid, NoLock);
2170 
2171  /*
2172  * The local name of the foreign table can not be recognized by the
2173  * foreign server and the table it references on foreign server might
2174  * have different column ordering or different columns than those
2175  * declared locally. Hence we have to deparse whole-row reference as
2176  * ROW(columns referenced locally). Construct this by deparsing a
2177  * "whole row" attribute.
2178  */
2179  attrs_used = bms_add_member(NULL,
2181 
2182  /*
2183  * In case the whole-row reference is under an outer join then it has
2184  * to go NULL whenever the rest of the row goes NULL. Deparsing a join
2185  * query would always involve multiple relations, thus qualify_col
2186  * would be true.
2187  */
2188  if (qualify_col)
2189  {
2190  appendStringInfoString(buf, "CASE WHEN (");
2191  ADD_REL_QUALIFIER(buf, varno);
2192  appendStringInfoString(buf, "*)::text IS NOT NULL THEN ");
2193  }
2194 
2195  appendStringInfoString(buf, "ROW(");
2196  deparseTargetList(buf, rte, varno, rel, false, attrs_used, qualify_col,
2197  &retrieved_attrs);
2198  appendStringInfoChar(buf, ')');
2199 
2200  /* Complete the CASE WHEN statement started above. */
2201  if (qualify_col)
2202  appendStringInfoString(buf, " END");
2203 
2204  table_close(rel, NoLock);
2205  bms_free(attrs_used);
2206  }
2207  else
2208  {
2209  char *colname = NULL;
2210  List *options;
2211  ListCell *lc;
2212 
2213  /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
2214  Assert(!IS_SPECIAL_VARNO(varno));
2215 
2216  /*
2217  * If it's a column of a foreign table, and it has the column_name FDW
2218  * option, use that value.
2219  */
2220  options = GetForeignColumnOptions(rte->relid, varattno);
2221  foreach(lc, options)
2222  {
2223  DefElem *def = (DefElem *) lfirst(lc);
2224 
2225  if (strcmp(def->defname, "column_name") == 0)
2226  {
2227  colname = defGetString(def);
2228  break;
2229  }
2230  }
2231 
2232  /*
2233  * If it's a column of a regular table or it doesn't have column_name
2234  * FDW option, use attribute name.
2235  */
2236  if (colname == NULL)
2237  colname = get_attname(rte->relid, varattno, false);
2238 
2239  if (qualify_col)
2240  ADD_REL_QUALIFIER(buf, varno);
2241 
2243  }
2244 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10727
#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:1169
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:286
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:175
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:107
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:738
#define lfirst(lc)
Definition: pg_list.h:190
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:732
#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 2455 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().

2456 {
2457  StringInfo buf = context->buf;
2458  Oid typoutput;
2459  bool typIsVarlena;
2460  char *extval;
2461  bool isfloat = false;
2462  bool needlabel;
2463 
2464  if (node->constisnull)
2465  {
2466  appendStringInfoString(buf, "NULL");
2467  if (showtype >= 0)
2468  appendStringInfo(buf, "::%s",
2470  node->consttypmod));
2471  return;
2472  }
2473 
2475  &typoutput, &typIsVarlena);
2476  extval = OidOutputFunctionCall(typoutput, node->constvalue);
2477 
2478  switch (node->consttype)
2479  {
2480  case INT2OID:
2481  case INT4OID:
2482  case INT8OID:
2483  case OIDOID:
2484  case FLOAT4OID:
2485  case FLOAT8OID:
2486  case NUMERICOID:
2487  {
2488  /*
2489  * No need to quote unless it's a special value such as 'NaN'.
2490  * See comments in get_const_expr().
2491  */
2492  if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2493  {
2494  if (extval[0] == '+' || extval[0] == '-')
2495  appendStringInfo(buf, "(%s)", extval);
2496  else
2497  appendStringInfoString(buf, extval);
2498  if (strcspn(extval, "eE.") != strlen(extval))
2499  isfloat = true; /* it looks like a float */
2500  }
2501  else
2502  appendStringInfo(buf, "'%s'", extval);
2503  }
2504  break;
2505  case BITOID:
2506  case VARBITOID:
2507  appendStringInfo(buf, "B'%s'", extval);
2508  break;
2509  case BOOLOID:
2510  if (strcmp(extval, "t") == 0)
2511  appendStringInfoString(buf, "true");
2512  else
2513  appendStringInfoString(buf, "false");
2514  break;
2515  default:
2516  deparseStringLiteral(buf, extval);
2517  break;
2518  }
2519 
2520  pfree(extval);
2521 
2522  if (showtype < 0)
2523  return;
2524 
2525  /*
2526  * For showtype == 0, append ::typename unless the constant will be
2527  * implicitly typed as the right type when it is read in.
2528  *
2529  * XXX this code has to be kept in sync with the behavior of the parser,
2530  * especially make_const.
2531  */
2532  switch (node->consttype)
2533  {
2534  case BOOLOID:
2535  case INT4OID:
2536  case UNKNOWNOID:
2537  needlabel = false;
2538  break;
2539  case NUMERICOID:
2540  needlabel = !isfloat || (node->consttypmod >= 0);
2541  break;
2542  default:
2543  needlabel = true;
2544  break;
2545  }
2546  if (needlabel || showtype > 0)
2547  appendStringInfo(buf, "::%s",
2549  node->consttypmod));
2550 }
Datum constvalue
Definition: primnodes.h:214
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2784
unsigned int Oid
Definition: postgres_ext.h:31
Oid consttype
Definition: primnodes.h:210
void pfree(void *pointer)
Definition: mcxt.c:1056
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:67
StringInfo buf
Definition: deparse.c:101
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2292
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:919
int32 consttypmod
Definition: primnodes.h:211
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1657
bool constisnull
Definition: primnodes.h:215

◆ deparseDeleteSql()

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

Definition at line 1906 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1910 {
1911  appendStringInfoString(buf, "DELETE FROM ");
1912  deparseRelation(buf, rel);
1913  appendStringInfoString(buf, " WHERE ctid = $1");
1914 
1915  deparseReturningList(buf, rte, rtindex, rel,
1916  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
1917  NIL, returningList, retrieved_attrs);
1918 }
#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:1986
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:2252

◆ 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 1935 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().

1942 {
1943  deparse_expr_cxt context;
1944 
1945  /* Set up context struct for recursion */
1946  context.root = root;
1947  context.foreignrel = foreignrel;
1948  context.scanrel = foreignrel;
1949  context.buf = buf;
1950  context.params_list = params_list;
1951 
1952  appendStringInfoString(buf, "DELETE FROM ");
1953  deparseRelation(buf, rel);
1954  if (foreignrel->reloptkind == RELOPT_JOINREL)
1955  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
1956 
1957  if (foreignrel->reloptkind == RELOPT_JOINREL)
1958  {
1959  List *ignore_conds = NIL;
1960 
1961  appendStringInfoString(buf, " USING ");
1962  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
1963  &ignore_conds, params_list);
1964  remote_conds = list_concat(remote_conds, ignore_conds);
1965  }
1966 
1967  if (remote_conds)
1968  {
1969  appendStringInfoString(buf, " WHERE ");
1970  appendConditions(remote_conds, &context);
1971  }
1972 
1973  if (foreignrel->reloptkind == RELOPT_JOINREL)
1974  deparseExplicitTargetList(returningList, true, retrieved_attrs,
1975  &context);
1976  else
1977  deparseReturningList(buf, planner_rt_fetch(rtindex, root),
1978  rtindex, rel, false,
1979  NIL, returningList, retrieved_attrs);
1980 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:662
List ** params_list
Definition: deparse.c:102
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
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:1986
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
#define REL_ALIAS_PREFIX
Definition: deparse.c:105
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1406
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1335
StringInfo buf
Definition: deparse.c:101
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2252
PlannerInfo * root
Definition: deparse.c:96
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1483
RelOptInfo * scanrel
Definition: deparse.c:98
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:97

◆ 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 1821 of file deparse.c.

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

Referenced by postgresPlanDirectModify().

1830 {
1831  deparse_expr_cxt context;
1832  int nestlevel;
1833  bool first;
1834  ListCell *lc;
1835  RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
1836 
1837  /* Set up context struct for recursion */
1838  context.root = root;
1839  context.foreignrel = foreignrel;
1840  context.scanrel = foreignrel;
1841  context.buf = buf;
1842  context.params_list = params_list;
1843 
1844  appendStringInfoString(buf, "UPDATE ");
1845  deparseRelation(buf, rel);
1846  if (foreignrel->reloptkind == RELOPT_JOINREL)
1847  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
1848  appendStringInfoString(buf, " SET ");
1849 
1850  /* Make sure any constants in the exprs are printed portably */
1851  nestlevel = set_transmission_modes();
1852 
1853  first = true;
1854  foreach(lc, targetAttrs)
1855  {
1856  int attnum = lfirst_int(lc);
1857  TargetEntry *tle = get_tle_by_resno(targetlist, attnum);
1858 
1859  if (!tle)
1860  elog(ERROR, "attribute number %d not found in UPDATE targetlist",
1861  attnum);
1862 
1863  if (!first)
1864  appendStringInfoString(buf, ", ");
1865  first = false;
1866 
1867  deparseColumnRef(buf, rtindex, attnum, rte, false);
1868  appendStringInfoString(buf, " = ");
1869  deparseExpr((Expr *) tle->expr, &context);
1870  }
1871 
1872  reset_transmission_modes(nestlevel);
1873 
1874  if (foreignrel->reloptkind == RELOPT_JOINREL)
1875  {
1876  List *ignore_conds = NIL;
1877 
1878  appendStringInfoString(buf, " FROM ");
1879  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
1880  &ignore_conds, params_list);
1881  remote_conds = list_concat(remote_conds, ignore_conds);
1882  }
1883 
1884  if (remote_conds)
1885  {
1886  appendStringInfoString(buf, " WHERE ");
1887  appendConditions(remote_conds, &context);
1888  }
1889 
1890  if (foreignrel->reloptkind == RELOPT_JOINREL)
1891  deparseExplicitTargetList(returningList, true, retrieved_attrs,
1892  &context);
1893  else
1894  deparseReturningList(buf, rte, rtindex, rel, false,
1895  NIL, returningList, retrieved_attrs);
1896 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:662
List ** params_list
Definition: deparse.c:102
int set_transmission_modes(void)
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
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:1986
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:191
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
#define REL_ALIAS_PREFIX
Definition: deparse.c:105
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1406
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1335
StringInfo buf
Definition: deparse.c:101
int16 attnum
Definition: pg_attribute.h:79
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2252
PlannerInfo * root
Definition: deparse.c:96
Expr * expr
Definition: primnodes.h:1407
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1483
#define elog(elevel,...)
Definition: elog.h:214
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
RelOptInfo * scanrel
Definition: deparse.c:98
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:97
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2124

◆ deparseDistinctExpr()

static void deparseDistinctExpr ( DistinctExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2782 of file deparse.c.

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

Referenced by deparseExpr().

2783 {
2784  StringInfo buf = context->buf;
2785 
2786  Assert(list_length(node->args) == 2);
2787 
2788  appendStringInfoChar(buf, '(');
2789  deparseExpr(linitial(node->args), context);
2790  appendStringInfoString(buf, " IS DISTINCT FROM ");
2791  deparseExpr(lsecond(node->args), context);
2792  appendStringInfoChar(buf, ')');
2793 }
#define lsecond(l)
Definition: pg_list.h:200
#define linitial(l)
Definition: pg_list.h:195
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
List * args
Definition: primnodes.h:522

◆ deparseExplicitTargetList()

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

Definition at line 1406 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().

1410 {
1411  ListCell *lc;
1412  StringInfo buf = context->buf;
1413  int i = 0;
1414 
1415  *retrieved_attrs = NIL;
1416 
1417  foreach(lc, tlist)
1418  {
1419  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1420 
1421  if (i > 0)
1422  appendStringInfoString(buf, ", ");
1423  else if (is_returning)
1424  appendStringInfoString(buf, " RETURNING ");
1425 
1426  deparseExpr((Expr *) tle->expr, context);
1427 
1428  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1429  i++;
1430  }
1431 
1432  if (i == 0 && !is_returning)
1433  appendStringInfoString(buf, "NULL");
1434 }
#define NIL
Definition: pg_list.h:65
#define lfirst_node(type, lc)
Definition: pg_list.h:193
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
List * lappend_int(List *list, int datum)
Definition: list.c:339
StringInfo buf
Definition: deparse.c:101
Expr * expr
Definition: primnodes.h:1407
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
int i

◆ deparseExpr()

static void deparseExpr ( Expr expr,
deparse_expr_cxt context 
)
static

Definition at line 2327 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().

2328 {
2329  if (node == NULL)
2330  return;
2331 
2332  switch (nodeTag(node))
2333  {
2334  case T_Var:
2335  deparseVar((Var *) node, context);
2336  break;
2337  case T_Const:
2338  deparseConst((Const *) node, context, 0);
2339  break;
2340  case T_Param:
2341  deparseParam((Param *) node, context);
2342  break;
2343  case T_SubscriptingRef:
2344  deparseSubscriptingRef((SubscriptingRef *) node, context);
2345  break;
2346  case T_FuncExpr:
2347  deparseFuncExpr((FuncExpr *) node, context);
2348  break;
2349  case T_OpExpr:
2350  deparseOpExpr((OpExpr *) node, context);
2351  break;
2352  case T_DistinctExpr:
2353  deparseDistinctExpr((DistinctExpr *) node, context);
2354  break;
2355  case T_ScalarArrayOpExpr:
2356  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2357  break;
2358  case T_RelabelType:
2359  deparseRelabelType((RelabelType *) node, context);
2360  break;
2361  case T_BoolExpr:
2362  deparseBoolExpr((BoolExpr *) node, context);
2363  break;
2364  case T_NullTest:
2365  deparseNullTest((NullTest *) node, context);
2366  break;
2367  case T_ArrayExpr:
2368  deparseArrayExpr((ArrayExpr *) node, context);
2369  break;
2370  case T_Aggref:
2371  deparseAggref((Aggref *) node, context);
2372  break;
2373  default:
2374  elog(ERROR, "unsupported expression type for deparse: %d",
2375  (int) nodeTag(node));
2376  break;
2377  }
2378 }
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:2955
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2858
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:2896
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2782
Definition: primnodes.h:181
#define ERROR
Definition: elog.h:43
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2561
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2800
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2703
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2929
static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2594
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:2845
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2389
Definition: nodes.h:154
Definition: nodes.h:153
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2455
#define nodeTag(nodeptr)
Definition: nodes.h:534
#define elog(elevel,...)
Definition: elog.h:214
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2640
Definition: nodes.h:155

◆ deparseFromExpr()

static void deparseFromExpr ( List quals,
deparse_expr_cxt context 
)
static

Definition at line 1135 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().

1136 {
1137  StringInfo buf = context->buf;
1138  RelOptInfo *scanrel = context->scanrel;
1139 
1140  /* For upper relations, scanrel must be either a joinrel or a baserel */
1141  Assert(!IS_UPPER_REL(context->foreignrel) ||
1142  IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1143 
1144  /* Construct FROM clause */
1145  appendStringInfoString(buf, " FROM ");
1146  deparseFromExprForRel(buf, context->root, scanrel,
1147  (bms_membership(scanrel->relids) == BMS_MULTIPLE),
1148  (Index) 0, NULL, context->params_list);
1149 
1150  /* Construct WHERE clause */
1151  if (quals != NIL)
1152  {
1153  appendStringInfoString(buf, " WHERE ");
1154  appendConditions(quals, context);
1155  }
1156 }
#define NIL
Definition: pg_list.h:65
List ** params_list
Definition: deparse.c:102
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:643
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:638
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
Relids relids
Definition: pathnodes.h:665
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1335
StringInfo buf
Definition: deparse.c:101
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
unsigned int Index
Definition: c.h:475
PlannerInfo * root
Definition: deparse.c:96
#define Assert(condition)
Definition: c.h:738
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:648
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1483
RelOptInfo * scanrel
Definition: deparse.c:98
RelOptInfo * foreignrel
Definition: deparse.c:97

◆ 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 1483 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().

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

2641 {
2642  StringInfo buf = context->buf;
2643  bool use_variadic;
2644  bool first;
2645  ListCell *arg;
2646 
2647  /*
2648  * If the function call came from an implicit coercion, then just show the
2649  * first argument.
2650  */
2651  if (node->funcformat == COERCE_IMPLICIT_CAST)
2652  {
2653  deparseExpr((Expr *) linitial(node->args), context);
2654  return;
2655  }
2656 
2657  /*
2658  * If the function call came from a cast, then show the first argument
2659  * plus an explicit cast operation.
2660  */
2661  if (node->funcformat == COERCE_EXPLICIT_CAST)
2662  {
2663  Oid rettype = node->funcresulttype;
2664  int32 coercedTypmod;
2665 
2666  /* Get the typmod if this is a length-coercion function */
2667  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2668 
2669  deparseExpr((Expr *) linitial(node->args), context);
2670  appendStringInfo(buf, "::%s",
2671  deparse_type_name(rettype, coercedTypmod));
2672  return;
2673  }
2674 
2675  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2676  use_variadic = node->funcvariadic;
2677 
2678  /*
2679  * Normal function: display as proname(args).
2680  */
2681  appendFunctionName(node->funcid, context);
2682  appendStringInfoChar(buf, '(');
2683 
2684  /* ... and all the arguments */
2685  first = true;
2686  foreach(arg, node->args)
2687  {
2688  if (!first)
2689  appendStringInfoString(buf, ", ");
2690  if (use_variadic && lnext(node->args, arg) == NULL)
2691  appendStringInfoString(buf, "VARIADIC ");
2692  deparseExpr((Expr *) lfirst(arg), context);
2693  first = false;
2694  }
2695  appendStringInfoChar(buf, ')');
2696 }
Oid funcresulttype
Definition: primnodes.h:470
List * args
Definition: primnodes.h:477
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3264
Definition: nodes.h:529
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:512
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:355
CoercionForm funcformat
Definition: primnodes.h:474
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:195
Oid funcid
Definition: primnodes.h:469
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:919
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
void * arg
bool funcvariadic
Definition: primnodes.h:472

◆ deparseInsertSql()

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

Definition at line 1705 of file deparse.c.

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

Referenced by postgresBeginForeignInsert(), and postgresPlanForeignModify().

1710 {
1711  AttrNumber pindex;
1712  bool first;
1713  ListCell *lc;
1714 
1715  appendStringInfoString(buf, "INSERT INTO ");
1716  deparseRelation(buf, rel);
1717 
1718  if (targetAttrs)
1719  {
1720  appendStringInfoChar(buf, '(');
1721 
1722  first = true;
1723  foreach(lc, targetAttrs)
1724  {
1725  int attnum = lfirst_int(lc);
1726 
1727  if (!first)
1728  appendStringInfoString(buf, ", ");
1729  first = false;
1730 
1731  deparseColumnRef(buf, rtindex, attnum, rte, false);
1732  }
1733 
1734  appendStringInfoString(buf, ") VALUES (");
1735 
1736  pindex = 1;
1737  first = true;
1738  foreach(lc, targetAttrs)
1739  {
1740  if (!first)
1741  appendStringInfoString(buf, ", ");
1742  first = false;
1743 
1744  appendStringInfo(buf, "$%d", pindex);
1745  pindex++;
1746  }
1747 
1748  appendStringInfoChar(buf, ')');
1749  }
1750  else
1751  appendStringInfoString(buf, " DEFAULT VALUES");
1752 
1753  if (doNothing)
1754  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1755 
1756  deparseReturningList(buf, rte, rtindex, rel,
1757  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1758  withCheckOptionList, returningList, retrieved_attrs);
1759 }
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:1986
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define lfirst_int(lc)
Definition: pg_list.h:191
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:79
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2252
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2124

◆ deparseLockingClause()

static void deparseLockingClause ( deparse_expr_cxt context)
static

Definition at line 1245 of file deparse.c.

References 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, Query::resultRelation, foreign_glob_cxt::root, deparse_expr_cxt::root, PlannerInfo::rowMarks, deparse_expr_cxt::scanrel, and PlanRowMark::strength.

Referenced by deparseSelectStmtForRel().

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

2897 {
2898  StringInfo buf = context->buf;
2899 
2900  appendStringInfoChar(buf, '(');
2901  deparseExpr(node->arg, context);
2902 
2903  /*
2904  * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
2905  * shorter and traditional. If it's a rowtype input but we're applying a
2906  * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
2907  * correct.
2908  */
2909  if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
2910  {
2911  if (node->nulltesttype == IS_NULL)
2912  appendStringInfoString(buf, " IS NULL)");
2913  else
2914  appendStringInfoString(buf, " IS NOT NULL)");
2915  }
2916  else
2917  {
2918  if (node->nulltesttype == IS_NULL)
2919  appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
2920  else
2921  appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
2922  }
2923 }
Definition: nodes.h:529
Expr * arg
Definition: primnodes.h:1219
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2543
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
NullTestType nulltesttype
Definition: primnodes.h:1220
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
bool argisrow
Definition: primnodes.h:1221

◆ deparseOperatorName()

static void deparseOperatorName ( StringInfo  buf,
Form_pg_operator  opform 
)
static

Definition at line 2754 of file deparse.c.

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

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

2755 {
2756  char *opname;
2757 
2758  /* opname is not a SQL identifier, so we should not quote it. */
2759  opname = NameStr(opform->oprname);
2760 
2761  /* Print schema name only if it's not pg_catalog */
2762  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
2763  {
2764  const char *opnspname;
2765 
2766  opnspname = get_namespace_name(opform->oprnamespace);
2767  /* Print fully qualified operator name. */
2768  appendStringInfo(buf, "OPERATOR(%s.%s)",
2769  quote_identifier(opnspname), opname);
2770  }
2771  else
2772  {
2773  /* Just print operator name. */
2774  appendStringInfoString(buf, opname);
2775  }
2776 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10727
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:3191
#define NameStr(name)
Definition: c.h:615

◆ deparseOpExpr()

static void deparseOpExpr ( OpExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2703 of file deparse.c.

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

Referenced by deparseExpr().

2704 {
2705  StringInfo buf = context->buf;
2706  HeapTuple tuple;
2707  Form_pg_operator form;
2708  char oprkind;
2709  ListCell *arg;
2710 
2711  /* Retrieve information about the operator from system catalog. */
2712  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2713  if (!HeapTupleIsValid(tuple))
2714  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2715  form = (Form_pg_operator) GETSTRUCT(tuple);
2716  oprkind = form->oprkind;
2717 
2718  /* Sanity check. */
2719  Assert((oprkind == 'r' && list_length(node->args) == 1) ||
2720  (oprkind == 'l' && list_length(node->args) == 1) ||
2721  (oprkind == 'b' && list_length(node->args) == 2));
2722 
2723  /* Always parenthesize the expression. */
2724  appendStringInfoChar(buf, '(');
2725 
2726  /* Deparse left operand. */
2727  if (oprkind == 'r' || oprkind == 'b')
2728  {
2729  arg = list_head(node->args);
2730  deparseExpr(lfirst(arg), context);
2731  appendStringInfoChar(buf, ' ');
2732  }
2733 
2734  /* Deparse operator name. */
2735  deparseOperatorName(buf, form);
2736 
2737  /* Deparse right operand. */
2738  if (oprkind == 'l' || oprkind == 'b')
2739  {
2740  arg = list_tail(node->args);
2741  appendStringInfoChar(buf, ' ');
2742  deparseExpr(lfirst(arg), context);
2743  }
2744 
2745  appendStringInfoChar(buf, ')');
2746 
2747  ReleaseSysCache(tuple);
2748 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2754
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
StringInfo buf
Definition: deparse.c:101
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
#define elog(elevel,...)
Definition: elog.h:214
static ListCell * list_tail(const List *l)
Definition: pg_list.h:132
void * arg
Oid opno
Definition: primnodes.h:516
List * args
Definition: primnodes.h:522

◆ deparseParam()

static void deparseParam ( Param node,
deparse_expr_cxt context 
)
static

Definition at line 2561 of file deparse.c.

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

Referenced by deparseExpr().

2562 {
2563  if (context->params_list)
2564  {
2565  int pindex = 0;
2566  ListCell *lc;
2567 
2568  /* find its index in params_list */
2569  foreach(lc, *context->params_list)
2570  {
2571  pindex++;
2572  if (equal(node, (Node *) lfirst(lc)))
2573  break;
2574  }
2575  if (lc == NULL)
2576  {
2577  /* not in list, so add it */
2578  pindex++;
2579  *context->params_list = lappend(*context->params_list, node);
2580  }
2581 
2582  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2583  }
2584  else
2585  {
2586  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2587  }
2588 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
List ** params_list
Definition: deparse.c:102
Definition: nodes.h:529
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3106
List * lappend(List *list, void *datum)
Definition: list.c:321
int32 paramtypmod
Definition: primnodes.h:265
#define lfirst(lc)
Definition: pg_list.h:190
Oid paramtype
Definition: primnodes.h:264
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3132

◆ 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 1634 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().

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

◆ deparseRelabelType()

static void deparseRelabelType ( RelabelType node,
deparse_expr_cxt context 
)
static

Definition at line 2845 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().

2846 {
2847  deparseExpr(node->arg, context);
2848  if (node->relabelformat != COERCE_IMPLICIT_CAST)
2849  appendStringInfo(context->buf, "::%s",
2851  node->resulttypmod));
2852 }
Expr * arg
Definition: primnodes.h:814
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
StringInfo buf
Definition: deparse.c:101
Oid resulttype
Definition: primnodes.h:815
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:919
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
int32 resulttypmod
Definition: primnodes.h:816
CoercionForm relabelformat
Definition: primnodes.h:818

◆ deparseRelation()

static void deparseRelation ( StringInfo  buf,
Relation  rel 
)
static

Definition at line 2252 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(), and deparseUpdateSql().

2253 {
2254  ForeignTable *table;
2255  const char *nspname = NULL;
2256  const char *relname = NULL;
2257  ListCell *lc;
2258 
2259  /* obtain additional catalog information. */
2260  table = GetForeignTable(RelationGetRelid(rel));
2261 
2262  /*
2263  * Use value of FDW options if any, instead of the name of object itself.
2264  */
2265  foreach(lc, table->options)
2266  {
2267  DefElem *def = (DefElem *) lfirst(lc);
2268 
2269  if (strcmp(def->defname, "schema_name") == 0)
2270  nspname = defGetString(def);
2271  else if (strcmp(def->defname, "table_name") == 0)
2272  relname = defGetString(def);
2273  }
2274 
2275  /*
2276  * Note: we could skip printing the schema name if it's pg_catalog, but
2277  * that doesn't seem worth the trouble.
2278  */
2279  if (nspname == NULL)
2280  nspname = get_namespace_name(RelationGetNamespace(rel));
2281  if (relname == NULL)
2282  relname = RelationGetRelationName(rel);
2283 
2284  appendStringInfo(buf, "%s.%s",
2285  quote_identifier(nspname), quote_identifier(relname));
2286 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10727
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:3191
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define lfirst(lc)
Definition: pg_list.h:190
List * options
Definition: foreign.h:57
char * defname
Definition: parsenodes.h:732
#define RelationGetRelid(relation)
Definition: rel.h:456
#define RelationGetNamespace(relation)
Definition: rel.h:497

◆ 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 1986 of file deparse.c.

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

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

1992 {
1993  Bitmapset *attrs_used = NULL;
1994 
1995  if (trig_after_row)
1996  {
1997  /* whole-row reference acquires all non-system columns */
1998  attrs_used =
2000  }
2001 
2002  if (withCheckOptionList != NIL)
2003  {
2004  /*
2005  * We need the attrs, non-system and system, mentioned in the local
2006  * query's WITH CHECK OPTION list.
2007  *
2008  * Note: we do this to ensure that WCO constraints will be evaluated
2009  * on the data actually inserted/updated on the remote side, which
2010  * might differ from the data supplied by the core code, for example
2011  * as a result of remote triggers.
2012  */
2013  pull_varattnos((Node *) withCheckOptionList, rtindex,
2014  &attrs_used);
2015  }
2016 
2017  if (returningList != NIL)
2018  {
2019  /*
2020  * We need the attrs, non-system and system, mentioned in the local
2021  * query's RETURNING list.
2022  */
2023  pull_varattnos((Node *) returningList, rtindex,
2024  &attrs_used);
2025  }
2026 
2027  if (attrs_used != NULL)
2028  deparseTargetList(buf, rte, rtindex, rel, true, attrs_used, false,
2029  retrieved_attrs);
2030  else
2031  *retrieved_attrs = NIL;
2032 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:529
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
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:1169
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186

◆ deparseScalarArrayOpExpr()

static void deparseScalarArrayOpExpr ( ScalarArrayOpExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2800 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().

2801 {
2802  StringInfo buf = context->buf;
2803  HeapTuple tuple;
2804  Form_pg_operator form;
2805  Expr *arg1;
2806  Expr *arg2;
2807 
2808  /* Retrieve information about the operator from system catalog. */
2809  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2810  if (!HeapTupleIsValid(tuple))
2811  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2812  form = (Form_pg_operator) GETSTRUCT(tuple);
2813 
2814  /* Sanity check. */
2815  Assert(list_length(node->args) == 2);
2816 
2817  /* Always parenthesize the expression. */
2818  appendStringInfoChar(buf, '(');
2819 
2820  /* Deparse left operand. */
2821  arg1 = linitial(node->args);
2822  deparseExpr(arg1, context);
2823  appendStringInfoChar(buf, ' ');
2824 
2825  /* Deparse operator name plus decoration. */
2826  deparseOperatorName(buf, form);
2827  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
2828 
2829  /* Deparse right operand. */
2830  arg2 = lsecond(node->args);
2831  deparseExpr(arg2, context);
2832 
2833  appendStringInfoChar(buf, ')');
2834 
2835  /* Always parenthesize the expression. */
2836  appendStringInfoChar(buf, ')');
2837 
2838  ReleaseSysCache(tuple);
2839 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2754
#define lsecond(l)
Definition: pg_list.h:200
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:195
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
StringInfo buf
Definition: deparse.c:101
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
#define elog(elevel,...)
Definition: elog.h:214

◆ deparseSelectSql()

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

Definition at line 1077 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().

1079 {
1080  StringInfo buf = context->buf;
1081  RelOptInfo *foreignrel = context->foreignrel;
1082  PlannerInfo *root = context->root;
1083  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1084 
1085  /*
1086  * Construct SELECT list
1087  */
1088  appendStringInfoString(buf, "SELECT ");
1089 
1090  if (is_subquery)
1091  {
1092  /*
1093  * For a relation that is deparsed as a subquery, emit expressions
1094  * specified in the relation's reltarget. Note that since this is for
1095  * the subquery, no need to care about *retrieved_attrs.
1096  */
1097  deparseSubqueryTargetList(context);
1098  }
1099  else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
1100  {
1101  /*
1102  * For a join or upper relation the input tlist gives the list of
1103  * columns required to be fetched from the foreign server.
1104  */
1105  deparseExplicitTargetList(tlist, false, retrieved_attrs, context);
1106  }
1107  else
1108  {
1109  /*
1110  * For a base relation fpinfo->attrs_used gives the list of columns
1111  * required to be fetched from the foreign server.
1112  */
1113  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1114 
1115  /*
1116  * Core code already has some lock on each rel being planned, so we
1117  * can use NoLock here.
1118  */
1119  Relation rel = table_open(rte->relid, NoLock);
1120 
1121  deparseTargetList(buf, rte, foreignrel->relid, rel, false,
1122  fpinfo->attrs_used, false, retrieved_attrs);
1123  table_close(rel, NoLock);
1124  }
1125 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1442
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:643
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:1169
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
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:67
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1406
Index relid
Definition: pathnodes.h:693
StringInfo buf
Definition: deparse.c:101
void * fdw_private
Definition: pathnodes.h:719
PlannerInfo * root
Definition: deparse.c:96
Bitmapset * attrs_used
Definition: postgres_fdw.h:49
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:648
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
RelOptInfo * foreignrel
Definition: deparse.c:97

◆ 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 995 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().

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

◆ deparseSortGroupClause()

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

Definition at line 3299 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().

3301 {
3302  StringInfo buf = context->buf;
3303  TargetEntry *tle;
3304  Expr *expr;
3305 
3306  tle = get_sortgroupref_tle(ref, tlist);
3307  expr = tle->expr;
3308 
3309  if (force_colno)
3310  {
3311  /* Use column-number form when requested by caller. */
3312  Assert(!tle->resjunk);
3313  appendStringInfo(buf, "%d", tle->resno);
3314  }
3315  else if (expr && IsA(expr, Const))
3316  {
3317  /*
3318  * Force a typecast here so that we don't emit something like "GROUP
3319  * BY 2", which will be misconstrued as a column position rather than
3320  * a constant.
3321  */
3322  deparseConst((Const *) expr, context, 1);
3323  }
3324  else if (!expr || IsA(expr, Var))
3325  deparseExpr(expr, context);
3326  else
3327  {
3328  /* Always parenthesize the expression. */
3329  appendStringInfoChar(buf, '(');
3330  deparseExpr(expr, context);
3331  appendStringInfoChar(buf, ')');
3332  }
3333 
3334  return (Node *) expr;
3335 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Definition: nodes.h:529
Definition: primnodes.h:181
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool resjunk
Definition: primnodes.h:1414
static char * buf
Definition: pg_test_fsync.c:67
AttrNumber resno
Definition: primnodes.h:1408
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:367
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
#define Assert(condition)
Definition: c.h:738
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2455
Expr * expr
Definition: primnodes.h:1407
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327

◆ deparseStringLiteral()

void deparseStringLiteral ( StringInfo  buf,
const char *  val 
)

Definition at line 2292 of file deparse.c.

References appendStringInfoChar(), ESCAPE_STRING_SYNTAX, and SQL_STR_DOUBLE.

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

2293 {
2294  const char *valptr;
2295 
2296  /*
2297  * Rather than making assumptions about the remote server's value of
2298  * standard_conforming_strings, always use E'foo' syntax if there are any
2299  * backslashes. This will fail on remote servers before 8.1, but those
2300  * are long out of support.
2301  */
2302  if (strchr(val, '\\') != NULL)
2304  appendStringInfoChar(buf, '\'');
2305  for (valptr = val; *valptr; valptr++)
2306  {
2307  char ch = *valptr;
2308 
2309  if (SQL_STR_DOUBLE(ch, true))
2310  appendStringInfoChar(buf, ch);
2311  appendStringInfoChar(buf, ch);
2312  }
2313  appendStringInfoChar(buf, '\'');
2314 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1130
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1127
long val
Definition: informix.c:664

◆ deparseSubqueryTargetList()

static void deparseSubqueryTargetList ( deparse_expr_cxt context)
static

Definition at line 1442 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().

1443 {
1444  StringInfo buf = context->buf;
1445  RelOptInfo *foreignrel = context->foreignrel;
1446  bool first;
1447  ListCell *lc;
1448 
1449  /* Should only be called in these cases. */
1450  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1451 
1452  first = true;
1453  foreach(lc, foreignrel->reltarget->exprs)
1454  {
1455  Node *node = (Node *) lfirst(lc);
1456 
1457  if (!first)
1458  appendStringInfoString(buf, ", ");
1459  first = false;
1460 
1461  deparseExpr((Expr *) node, context);
1462  }
1463 
1464  /* Don't generate bad syntax if no expressions */
1465  if (first)
1466  appendStringInfoString(buf, "NULL");
1467 }
Definition: nodes.h:529
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:643
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:638
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
List * exprs
Definition: pathnodes.h:1074
StringInfo buf
Definition: deparse.c:101
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
RelOptInfo * foreignrel
Definition: deparse.c:97
struct PathTarget * reltarget
Definition: pathnodes.h:676

◆ deparseSubscriptingRef()

static void deparseSubscriptingRef ( SubscriptingRef node,
deparse_expr_cxt context 
)
static

Definition at line 2594 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().

2595 {
2596  StringInfo buf = context->buf;
2597  ListCell *lowlist_item;
2598  ListCell *uplist_item;
2599 
2600  /* Always parenthesize the expression. */
2601  appendStringInfoChar(buf, '(');
2602 
2603  /*
2604  * Deparse referenced array expression first. If that expression includes
2605  * a cast, we have to parenthesize to prevent the array subscript from
2606  * being taken as typename decoration. We can avoid that in the typical
2607  * case of subscripting a Var, but otherwise do it.
2608  */
2609  if (IsA(node->refexpr, Var))
2610  deparseExpr(node->refexpr, context);
2611  else
2612  {
2613  appendStringInfoChar(buf, '(');
2614  deparseExpr(node->refexpr, context);
2615  appendStringInfoChar(buf, ')');
2616  }
2617 
2618  /* Deparse subscript expressions. */
2619  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2620  foreach(uplist_item, node->refupperindexpr)
2621  {
2622  appendStringInfoChar(buf, '[');
2623  if (lowlist_item)
2624  {
2625  deparseExpr(lfirst(lowlist_item), context);
2626  appendStringInfoChar(buf, ':');
2627  lowlist_item = lnext(node->reflowerindexpr, lowlist_item);
2628  }
2629  deparseExpr(lfirst(uplist_item), context);
2630  appendStringInfoChar(buf, ']');
2631  }
2632 
2633  appendStringInfoChar(buf, ')');
2634 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
Definition: primnodes.h:181
List * refupperindexpr
Definition: primnodes.h:422
static char * buf
Definition: pg_test_fsync.c:67
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:101
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2327
List * reflowerindexpr
Definition: primnodes.h:424
Expr * refexpr
Definition: primnodes.h:427

◆ 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 1169 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().

1177 {
1178  TupleDesc tupdesc = RelationGetDescr(rel);
1179  bool have_wholerow;
1180  bool first;
1181  int i;
1182 
1183  *retrieved_attrs = NIL;
1184 
1185  /* If there's a whole-row reference, we'll need all the columns. */
1187  attrs_used);
1188 
1189  first = true;
1190  for (i = 1; i <= tupdesc->natts; i++)
1191  {
1192  Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1);
1193 
1194  /* Ignore dropped attributes. */
1195  if (attr->attisdropped)
1196  continue;
1197 
1198  if (have_wholerow ||
1200  attrs_used))
1201  {
1202  if (!first)
1203  appendStringInfoString(buf, ", ");
1204  else if (is_returning)
1205  appendStringInfoString(buf, " RETURNING ");
1206  first = false;
1207 
1208  deparseColumnRef(buf, rtindex, i, rte, qualify_col);
1209 
1210  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1211  }
1212  }
1213 
1214  /*
1215  * Add ctid if needed. We currently don't support retrieving any other
1216  * system columns.
1217  */
1219  attrs_used))
1220  {
1221  if (!first)
1222  appendStringInfoString(buf, ", ");
1223  else if (is_returning)
1224  appendStringInfoString(buf, " RETURNING ");
1225  first = false;
1226 
1227  if (qualify_col)
1228  ADD_REL_QUALIFIER(buf, rtindex);
1229  appendStringInfoString(buf, "ctid");
1230 
1231  *retrieved_attrs = lappend_int(*retrieved_attrs,
1233  }
1234 
1235  /* Don't generate bad syntax if no undropped columns */
1236  if (first && !is_returning)
1237  appendStringInfoString(buf, "NULL");
1238 }
#define NIL
Definition: pg_list.h:65
#define RelationGetDescr(relation)
Definition: rel.h:482
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:107
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
List * lappend_int(List *list, int datum)
Definition: list.c:339
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:2124

◆ deparseUpdateSql()

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

Definition at line 1769 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1774 {
1775  AttrNumber pindex;
1776  bool first;
1777  ListCell *lc;
1778 
1779  appendStringInfoString(buf, "UPDATE ");
1780  deparseRelation(buf, rel);
1781  appendStringInfoString(buf, " SET ");
1782 
1783  pindex = 2; /* ctid is always the first param */
1784  first = true;
1785  foreach(lc, targetAttrs)
1786  {
1787  int attnum = lfirst_int(lc);
1788 
1789  if (!first)
1790  appendStringInfoString(buf, ", ");
1791  first = false;
1792 
1793  deparseColumnRef(buf, rtindex, attnum, rte, false);
1794  appendStringInfo(buf, " = $%d", pindex);
1795  pindex++;
1796  }
1797  appendStringInfoString(buf, " WHERE ctid = $1");
1798 
1799  deparseReturningList(buf, rte, rtindex, rel,
1800  rel->trigdesc && rel->trigdesc->trig_update_after_row,
1801  withCheckOptionList, returningList, retrieved_attrs);
1802 }
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:1986
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define lfirst_int(lc)
Definition: pg_list.h:191
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:79
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2252
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2124

◆ deparseVar()

static void deparseVar ( Var node,
deparse_expr_cxt context 
)
static

Definition at line 2389 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().

2390 {
2391  Relids relids = context->scanrel->relids;
2392  int relno;
2393  int colno;
2394 
2395  /* Qualify columns when multiple relations are involved. */
2396  bool qualify_col = (bms_membership(relids) == BMS_MULTIPLE);
2397 
2398  /*
2399  * If the Var belongs to the foreign relation that is deparsed as a
2400  * subquery, use the relation and column alias to the Var provided by the
2401  * subquery, instead of the remote name.
2402  */
2403  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2404  {
2405  appendStringInfo(context->buf, "%s%d.%s%d",
2407  SUBQUERY_COL_ALIAS_PREFIX, colno);
2408  return;
2409  }
2410 
2411  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2412  deparseColumnRef(context->buf, node->varno, node->varattno,
2413  planner_rt_fetch(node->varno, context->root),
2414  qualify_col);
2415  else
2416  {
2417  /* Treat like a Param */
2418  if (context->params_list)
2419  {
2420  int pindex = 0;
2421  ListCell *lc;
2422 
2423  /* find its index in params_list */
2424  foreach(lc, *context->params_list)
2425  {
2426  pindex++;
2427  if (equal(node, (Node *) lfirst(lc)))
2428  break;
2429  }
2430  if (lc == NULL)
2431  {
2432  /* not in list, so add it */
2433  pindex++;
2434  *context->params_list = lappend(*context->params_list, node);
2435  }
2436 
2437  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2438  }
2439  else
2440  {
2441  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2442  }
2443  }
2444 }
Index varlevelsup
Definition: primnodes.h:191
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
List ** params_list
Definition: deparse.c:102
Definition: nodes.h:529
AttrNumber varattno
Definition: primnodes.h:186
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3106
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:110
Oid vartype
Definition: primnodes.h:188
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:109
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
Relids relids
Definition: pathnodes.h:665
List * lappend(List *list, void *datum)
Definition: list.c:321
Index varno
Definition: primnodes.h:184
StringInfo buf
Definition: deparse.c:101
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:3344
PlannerInfo * root
Definition: deparse.c:96
#define lfirst(lc)
Definition: pg_list.h:190
RelOptInfo * scanrel
Definition: deparse.c:98
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:2124
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3132
int32 vartypmod
Definition: primnodes.h:189

◆ foreign_expr_walker()

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

Definition at line 294 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().

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

◆ get_jointype_name()

const char* get_jointype_name ( JoinType  jointype)

Definition at line 1369 of file deparse.c.

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

Referenced by deparseFromExprForRel(), and foreign_join_ok().

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

◆ get_relation_column_alias_ids()

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

Definition at line 3406 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().

3408 {
3409  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3410  int i;
3411  ListCell *lc;
3412 
3413  /* Get the relation alias ID */
3414  *relno = fpinfo->relation_index;
3415 
3416  /* Get the column alias ID */
3417  i = 1;
3418  foreach(lc, foreignrel->reltarget->exprs)
3419  {
3420  if (equal(lfirst(lc), (Node *) node))
3421  {
3422  *colno = i;
3423  return;
3424  }
3425  i++;
3426  }
3427 
3428  /* Shouldn't get here */
3429  elog(ERROR, "unexpected expression in subquery output");
3430 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
Definition: nodes.h:529
#define ERROR
Definition: elog.h:43
List * exprs
Definition: pathnodes.h:1074
void * fdw_private
Definition: pathnodes.h:719
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:214
int i
struct PathTarget * reltarget
Definition: pathnodes.h:676

◆ is_foreign_expr()

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

Definition at line 230 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().

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

◆ is_foreign_param()

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

Definition at line 871 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().

874 {
875  if (expr == NULL)
876  return false;
877 
878  switch (nodeTag(expr))
879  {
880  case T_Var:
881  {
882  /* It would have to be sent unless it's a foreign Var */
883  Var *var = (Var *) expr;
884  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
885  Relids relids;
886 
887  if (IS_UPPER_REL(baserel))
888  relids = fpinfo->outerrel->relids;
889  else
890  relids = baserel->relids;
891 
892  if (bms_is_member(var->varno, relids) && var->varlevelsup == 0)
893  return false; /* foreign Var, so not a param */
894  else
895  return true; /* it'd have to be a param */
896  break;
897  }
898  case T_Param:
899  /* Params always have to be sent to the foreign server */
900  return true;
901  default:
902  break;
903  }
904  return false;
905 }
Index varlevelsup
Definition: primnodes.h:191
Definition: primnodes.h:181
RelOptInfo * outerrel
Definition: postgres_fdw.h:100
Relids relids
Definition: pathnodes.h:665
Index varno
Definition: primnodes.h:184
Definition: nodes.h:153
void * fdw_private
Definition: pathnodes.h:719
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:648
#define nodeTag(nodeptr)
Definition: nodes.h:534
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Definition: nodes.h:155

◆ is_subquery_var()

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

Definition at line 3344 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().

3345 {
3346  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3347  RelOptInfo *outerrel = fpinfo->outerrel;
3348  RelOptInfo *innerrel = fpinfo->innerrel;
3349 
3350  /* Should only be called in these cases. */
3351  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
3352 
3353  /*
3354  * If the given relation isn't a join relation, it doesn't have any lower
3355  * subqueries, so the Var isn't a subquery output column.
3356  */
3357  if (!IS_JOIN_REL(foreignrel))
3358  return false;
3359 
3360  /*
3361  * If the Var doesn't belong to any lower subqueries, it isn't a subquery
3362  * output column.
3363  */
3364  if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels))
3365  return false;
3366 
3367  if (bms_is_member(node->varno, outerrel->relids))
3368  {
3369  /*
3370  * If outer relation is deparsed as a subquery, the Var is an output
3371  * column of the subquery; get the IDs for the relation/column alias.
3372  */
3373  if (fpinfo->make_outerrel_subquery)
3374  {
3375  get_relation_column_alias_ids(node, outerrel, relno, colno);
3376  return true;
3377  }
3378 
3379  /* Otherwise, recurse into the outer relation. */
3380  return is_subquery_var(node, outerrel, relno, colno);
3381  }
3382  else
3383  {
3384  Assert(bms_is_member(node->varno, innerrel->relids));
3385 
3386  /*
3387  * If inner relation is deparsed as a subquery, the Var is an output
3388  * column of the subquery; get the IDs for the relation/column alias.
3389  */
3390  if (fpinfo->make_innerrel_subquery)
3391  {
3392  get_relation_column_alias_ids(node, innerrel, relno, colno);
3393  return true;
3394  }
3395 
3396  /* Otherwise, recurse into the inner relation. */
3397  return is_subquery_var(node, innerrel, relno, colno);
3398  }
3399 }
Relids lower_subquery_rels
Definition: postgres_fdw.h:117
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:643
RelOptInfo * outerrel
Definition: postgres_fdw.h:100
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:638
Index varno
Definition: primnodes.h:184
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3344
void * fdw_private
Definition: pathnodes.h:719
static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3406
#define Assert(condition)
Definition: c.h:738
RelOptInfo * innerrel
Definition: postgres_fdw.h:101
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 3106 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

3108 {
3109  StringInfo buf = context->buf;
3110  char *ptypename = deparse_type_name(paramtype, paramtypmod);
3111 
3112  appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
3113 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static char * buf
Definition: pg_test_fsync.c:67
StringInfo buf
Definition: deparse.c:101
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:919

◆ printRemotePlaceholder()

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

Definition at line 3132 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

3134 {
3135  StringInfo buf = context->buf;
3136  char *ptypename = deparse_type_name(paramtype, paramtypmod);
3137 
3138  appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename);
3139 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static char * buf
Definition: pg_test_fsync.c:67
StringInfo buf
Definition: deparse.c:101
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:919