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 3031 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().

3032 {
3033  StringInfo buf = context->buf;
3034  ListCell *lc;
3035  bool first = true;
3036 
3037  foreach(lc, orderList)
3038  {
3039  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
3040  Node *sortexpr;
3041  Oid sortcoltype;
3042  TypeCacheEntry *typentry;
3043 
3044  if (!first)
3045  appendStringInfoString(buf, ", ");
3046  first = false;
3047 
3048  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
3049  false, context);
3050  sortcoltype = exprType(sortexpr);
3051  /* See whether operator is default < or > for datatype */
3052  typentry = lookup_type_cache(sortcoltype,
3054  if (srt->sortop == typentry->lt_opr)
3055  appendStringInfoString(buf, " ASC");
3056  else if (srt->sortop == typentry->gt_opr)
3057  appendStringInfoString(buf, " DESC");
3058  else
3059  {
3060  HeapTuple opertup;
3061  Form_pg_operator operform;
3062 
3063  appendStringInfoString(buf, " USING ");
3064 
3065  /* Append operator name. */
3066  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
3067  if (!HeapTupleIsValid(opertup))
3068  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
3069  operform = (Form_pg_operator) GETSTRUCT(opertup);
3070  deparseOperatorName(buf, operform);
3071  ReleaseSysCache(opertup);
3072  }
3073 
3074  if (srt->nulls_first)
3075  appendStringInfoString(buf, " NULLS FIRST");
3076  else
3077  appendStringInfoString(buf, " NULLS LAST");
3078  }
3079 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3283
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Index tleSortGroupRef
Definition: parsenodes.h:1234
Definition: nodes.h:525
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2738
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:130
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:322
#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:228
#define TYPECACHE_LT_OPR
Definition: typcache.h:129

◆ appendConditions()

static void appendConditions ( List exprs,
deparse_expr_cxt context 
)
static

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

1320 {
1321  int nestlevel;
1322  ListCell *lc;
1323  bool is_first = true;
1324  StringInfo buf = context->buf;
1325 
1326  /* Make sure any constants in the exprs are printed portably */
1327  nestlevel = set_transmission_modes();
1328 
1329  foreach(lc, exprs)
1330  {
1331  Expr *expr = (Expr *) lfirst(lc);
1332 
1333  /* Extract clause from RestrictInfo, if required */
1334  if (IsA(expr, RestrictInfo))
1335  expr = ((RestrictInfo *) expr)->clause;
1336 
1337  /* Connect expressions with "AND" and parenthesize each condition. */
1338  if (!is_first)
1339  appendStringInfoString(buf, " AND ");
1340 
1341  appendStringInfoChar(buf, '(');
1342  deparseExpr(expr, context);
1343  appendStringInfoChar(buf, ')');
1344 
1345  is_first = false;
1346  }
1347 
1348  reset_transmission_modes(nestlevel);
1349 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
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:2311

◆ appendFunctionName()

static void appendFunctionName ( Oid  funcid,
deparse_expr_cxt context 
)
static

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

3249 {
3250  StringInfo buf = context->buf;
3251  HeapTuple proctup;
3252  Form_pg_proc procform;
3253  const char *proname;
3254 
3255  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3256  if (!HeapTupleIsValid(proctup))
3257  elog(ERROR, "cache lookup failed for function %u", funcid);
3258  procform = (Form_pg_proc) GETSTRUCT(proctup);
3259 
3260  /* Print schema name only if it's not pg_catalog */
3261  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3262  {
3263  const char *schemaname;
3264 
3265  schemaname = get_namespace_name(procform->pronamespace);
3266  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3267  }
3268 
3269  /* Always print the function name */
3270  proname = NameStr(procform->proname);
3272 
3273  ReleaseSysCache(proctup);
3274 }
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:10640
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:3094
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:228
#define NameStr(name)
Definition: c.h:616

◆ appendGroupByClause()

static void appendGroupByClause ( List tlist,
deparse_expr_cxt context 
)
static

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

3130 {
3131  StringInfo buf = context->buf;
3132  Query *query = context->root->parse;
3133  ListCell *lc;
3134  bool first = true;
3135 
3136  /* Nothing to be done, if there's no GROUP BY clause in the query. */
3137  if (!query->groupClause)
3138  return;
3139 
3140  appendStringInfoString(buf, " GROUP BY ");
3141 
3142  /*
3143  * Queries with grouping sets are not pushed down, so we don't expect
3144  * grouping sets here.
3145  */
3146  Assert(!query->groupingSets);
3147 
3148  foreach(lc, query->groupClause)
3149  {
3150  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
3151 
3152  if (!first)
3153  appendStringInfoString(buf, ", ");
3154  first = false;
3155 
3156  deparseSortGroupClause(grp->tleSortGroupRef, tlist, true, context);
3157  }
3158 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3283
Query * parse
Definition: pathnodes.h:177
Index tleSortGroupRef
Definition: parsenodes.h:1234
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:739
#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 3220 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().

3221 {
3222  PlannerInfo *root = context->root;
3223  StringInfo buf = context->buf;
3224  int nestlevel;
3225 
3226  /* Make sure any constants in the exprs are printed portably */
3227  nestlevel = set_transmission_modes();
3228 
3229  if (root->parse->limitCount)
3230  {
3231  appendStringInfoString(buf, " LIMIT ");
3232  deparseExpr((Expr *) root->parse->limitCount, context);
3233  }
3234  if (root->parse->limitOffset)
3235  {
3236  appendStringInfoString(buf, " OFFSET ");
3237  deparseExpr((Expr *) root->parse->limitOffset, context);
3238  }
3239 
3240  reset_transmission_modes(nestlevel);
3241 }
Node * limitOffset
Definition: parsenodes.h:160
Query * parse
Definition: pathnodes.h:177
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:2311

◆ appendOrderByClause()

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

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

3168 {
3169  ListCell *lcell;
3170  int nestlevel;
3171  char *delim = " ";
3172  RelOptInfo *baserel = context->scanrel;
3173  StringInfo buf = context->buf;
3174 
3175  /* Make sure any constants in the exprs are printed portably */
3176  nestlevel = set_transmission_modes();
3177 
3178  appendStringInfoString(buf, " ORDER BY");
3179  foreach(lcell, pathkeys)
3180  {
3181  PathKey *pathkey = lfirst(lcell);
3182  Expr *em_expr;
3183 
3184  if (has_final_sort)
3185  {
3186  /*
3187  * By construction, context->foreignrel is the input relation to
3188  * the final sort.
3189  */
3190  em_expr = find_em_expr_for_input_target(context->root,
3191  pathkey->pk_eclass,
3192  context->foreignrel->reltarget);
3193  }
3194  else
3195  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
3196 
3197  Assert(em_expr != NULL);
3198 
3199  appendStringInfoString(buf, delim);
3200  deparseExpr(em_expr, context);
3201  if (pathkey->pk_strategy == BTLessStrategyNumber)
3202  appendStringInfoString(buf, " ASC");
3203  else
3204  appendStringInfoString(buf, " DESC");
3205 
3206  if (pathkey->pk_nulls_first)
3207  appendStringInfoString(buf, " NULLS FIRST");
3208  else
3209  appendStringInfoString(buf, " NULLS LAST");
3210 
3211  delim = ", ";
3212  }
3213  reset_transmission_modes(nestlevel);
3214 }
int set_transmission_modes(void)
int pk_strategy
Definition: pathnodes.h:1013
Expr * find_em_expr_for_input_target(PlannerInfo *root, EquivalenceClass *ec, PathTarget *target)
bool pk_nulls_first
Definition: pathnodes.h:1014
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
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1011
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
RelOptInfo * scanrel
Definition: deparse.c:98
#define BTLessStrategyNumber
Definition: stratnum.h:29
RelOptInfo * foreignrel
Definition: deparse.c:97
struct PathTarget * reltarget
Definition: pathnodes.h:652

◆ build_tlist_to_deparse()

List* build_tlist_to_deparse ( RelOptInfo foreignrel)

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

923 {
924  List *tlist = NIL;
925  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
926  ListCell *lc;
927 
928  /*
929  * For an upper relation, we have already built the target list while
930  * checking shippability, so just return that.
931  */
932  if (IS_UPPER_REL(foreignrel))
933  return fpinfo->grouped_tlist;
934 
935  /*
936  * We require columns specified in foreignrel->reltarget->exprs and those
937  * required for evaluating the local conditions.
938  */
939  tlist = add_to_flat_tlist(tlist,
940  pull_var_clause((Node *) foreignrel->reltarget->exprs,
942  foreach(lc, fpinfo->local_conds)
943  {
944  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
945 
946  tlist = add_to_flat_tlist(tlist,
947  pull_var_clause((Node *) rinfo->clause,
949  }
950 
951  return tlist;
952 }
#define NIL
Definition: pg_list.h:65
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:176
Definition: nodes.h:525
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:1943
List * exprs
Definition: pathnodes.h:1044
void * fdw_private
Definition: pathnodes.h:695
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:154
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:652

◆ 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:322
Expr * clause
Definition: pathnodes.h:1943
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 903 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().

904 {
906 
907  if (!is_builtin(type_oid))
908  flags |= FORMAT_TYPE_FORCE_QUALIFY;
909 
910  return format_type_extended(type_oid, typemod, flags);
911 }
bool is_builtin(Oid objectId)
Definition: shippable.c:153
uint16 bits16
Definition: c.h:367
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:109
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:111
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:108

◆ deparseAggref()

static void deparseAggref ( Aggref node,
deparse_expr_cxt context 
)
static

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

2940 {
2941  StringInfo buf = context->buf;
2942  bool use_variadic;
2943 
2944  /* Only basic, non-split aggregation accepted. */
2945  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
2946 
2947  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2948  use_variadic = node->aggvariadic;
2949 
2950  /* Find aggregate name from aggfnoid which is a pg_proc entry */
2951  appendFunctionName(node->aggfnoid, context);
2952  appendStringInfoChar(buf, '(');
2953 
2954  /* Add DISTINCT */
2955  appendStringInfoString(buf, (node->aggdistinct != NIL) ? "DISTINCT " : "");
2956 
2957  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
2958  {
2959  /* Add WITHIN GROUP (ORDER BY ..) */
2960  ListCell *arg;
2961  bool first = true;
2962 
2963  Assert(!node->aggvariadic);
2964  Assert(node->aggorder != NIL);
2965 
2966  foreach(arg, node->aggdirectargs)
2967  {
2968  if (!first)
2969  appendStringInfoString(buf, ", ");
2970  first = false;
2971 
2972  deparseExpr((Expr *) lfirst(arg), context);
2973  }
2974 
2975  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
2976  appendAggOrderBy(node->aggorder, node->args, context);
2977  }
2978  else
2979  {
2980  /* aggstar can be set only in zero-argument aggregates */
2981  if (node->aggstar)
2982  appendStringInfoChar(buf, '*');
2983  else
2984  {
2985  ListCell *arg;
2986  bool first = true;
2987 
2988  /* Add all the arguments */
2989  foreach(arg, node->args)
2990  {
2991  TargetEntry *tle = (TargetEntry *) lfirst(arg);
2992  Node *n = (Node *) tle->expr;
2993 
2994  if (tle->resjunk)
2995  continue;
2996 
2997  if (!first)
2998  appendStringInfoString(buf, ", ");
2999  first = false;
3000 
3001  /* Add VARIADIC */
3002  if (use_variadic && lnext(node->args, arg) == NULL)
3003  appendStringInfoString(buf, "VARIADIC ");
3004 
3005  deparseExpr((Expr *) n, context);
3006  }
3007  }
3008 
3009  /* Add ORDER BY */
3010  if (node->aggorder != NIL)
3011  {
3012  appendStringInfoString(buf, " ORDER BY ");
3013  appendAggOrderBy(node->aggorder, node->args, context);
3014  }
3015  }
3016 
3017  /* Add FILTER (WHERE ..) */
3018  if (node->aggfilter != NULL)
3019  {
3020  appendStringInfoString(buf, ") FILTER (WHERE ");
3021  deparseExpr((Expr *) node->aggfilter, context);
3022  }
3023 
3024  appendStringInfoChar(buf, ')');
3025 }
List * aggdistinct
Definition: primnodes.h:307
#define NIL
Definition: pg_list.h:65
bool aggvariadic
Definition: primnodes.h:310
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:3248
Definition: nodes.h:525
List * args
Definition: primnodes.h:305
bool aggstar
Definition: primnodes.h:309
bool resjunk
Definition: primnodes.h:1400
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:306
List * aggdirectargs
Definition: primnodes.h:304
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
Definition: deparse.c:3031
StringInfo buf
Definition: deparse.c:101
Oid aggfnoid
Definition: primnodes.h:298
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1393
AggSplit aggsplit
Definition: primnodes.h:314
Expr * aggfilter
Definition: primnodes.h:308
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
void * arg
char aggkind
Definition: primnodes.h:312

◆ deparseAnalyzeSizeSql()

void deparseAnalyzeSizeSql ( StringInfo  buf,
Relation  rel 
)

Definition at line 2027 of file deparse.c.

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

Referenced by postgresAnalyzeForeignTable().

2028 {
2030 
2031  /* We'll need the remote relation name as a literal. */
2032  initStringInfo(&relname);
2033  deparseRelation(&relname, rel);
2034 
2035  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
2036  deparseStringLiteral(buf, relname.data);
2037  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
2038 }
NameData relname
Definition: pg_class.h:35
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:2276
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2236

◆ deparseAnalyzeSql()

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

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

2048 {
2049  Oid relid = RelationGetRelid(rel);
2050  TupleDesc tupdesc = RelationGetDescr(rel);
2051  int i;
2052  char *colname;
2053  List *options;
2054  ListCell *lc;
2055  bool first = true;
2056 
2057  *retrieved_attrs = NIL;
2058 
2059  appendStringInfoString(buf, "SELECT ");
2060  for (i = 0; i < tupdesc->natts; i++)
2061  {
2062  /* Ignore dropped columns. */
2063  if (TupleDescAttr(tupdesc, i)->attisdropped)
2064  continue;
2065 
2066  if (!first)
2067  appendStringInfoString(buf, ", ");
2068  first = false;
2069 
2070  /* Use attribute name or column_name option. */
2071  colname = NameStr(TupleDescAttr(tupdesc, i)->attname);
2072  options = GetForeignColumnOptions(relid, i + 1);
2073 
2074  foreach(lc, options)
2075  {
2076  DefElem *def = (DefElem *) lfirst(lc);
2077 
2078  if (strcmp(def->defname, "column_name") == 0)
2079  {
2080  colname = defGetString(def);
2081  break;
2082  }
2083  }
2084 
2086 
2087  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
2088  }
2089 
2090  /* Don't generate bad syntax for zero-column relation. */
2091  if (first)
2092  appendStringInfoString(buf, "NULL");
2093 
2094  /*
2095  * Construct FROM clause
2096  */
2097  appendStringInfoString(buf, " FROM ");
2098  deparseRelation(buf, rel);
2099 }
#define NIL
Definition: pg_list.h:65
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10640
#define RelationGetDescr(relation)
Definition: rel.h:448
#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:340
static char ** options
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2236
#define lfirst(lc)
Definition: pg_list.h:190
int i
#define NameStr(name)
Definition: c.h:616
char * defname
Definition: parsenodes.h:730
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:422

◆ deparseArrayExpr()

static void deparseArrayExpr ( ArrayExpr node,
deparse_expr_cxt context 
)
static

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

2914 {
2915  StringInfo buf = context->buf;
2916  bool first = true;
2917  ListCell *lc;
2918 
2919  appendStringInfoString(buf, "ARRAY[");
2920  foreach(lc, node->elements)
2921  {
2922  if (!first)
2923  appendStringInfoString(buf, ", ");
2924  deparseExpr(lfirst(lc), context);
2925  first = false;
2926  }
2927  appendStringInfoChar(buf, ']');
2928 
2929  /* If the array is empty, we need an explicit cast to the array type. */
2930  if (node->elements == NIL)
2931  appendStringInfo(buf, "::%s",
2932  deparse_type_name(node->array_typeid, -1));
2933 }
#define NIL
Definition: pg_list.h:65
Oid array_typeid
Definition: primnodes.h:974
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:977
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:903
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311

◆ deparseBoolExpr()

static void deparseBoolExpr ( BoolExpr node,
deparse_expr_cxt context 
)
static

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

2843 {
2844  StringInfo buf = context->buf;
2845  const char *op = NULL; /* keep compiler quiet */
2846  bool first;
2847  ListCell *lc;
2848 
2849  switch (node->boolop)
2850  {
2851  case AND_EXPR:
2852  op = "AND";
2853  break;
2854  case OR_EXPR:
2855  op = "OR";
2856  break;
2857  case NOT_EXPR:
2858  appendStringInfoString(buf, "(NOT ");
2859  deparseExpr(linitial(node->args), context);
2860  appendStringInfoChar(buf, ')');
2861  return;
2862  }
2863 
2864  appendStringInfoChar(buf, '(');
2865  first = true;
2866  foreach(lc, node->args)
2867  {
2868  if (!first)
2869  appendStringInfo(buf, " %s ", op);
2870  deparseExpr((Expr *) lfirst(lc), context);
2871  first = false;
2872  }
2873  appendStringInfoChar(buf, ')');
2874 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:195
BoolExprType boolop
Definition: primnodes.h:568
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:569
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311

◆ deparseColumnRef()

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

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

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

2440 {
2441  StringInfo buf = context->buf;
2442  Oid typoutput;
2443  bool typIsVarlena;
2444  char *extval;
2445  bool isfloat = false;
2446  bool needlabel;
2447 
2448  if (node->constisnull)
2449  {
2450  appendStringInfoString(buf, "NULL");
2451  if (showtype >= 0)
2452  appendStringInfo(buf, "::%s",
2454  node->consttypmod));
2455  return;
2456  }
2457 
2459  &typoutput, &typIsVarlena);
2460  extval = OidOutputFunctionCall(typoutput, node->constvalue);
2461 
2462  switch (node->consttype)
2463  {
2464  case INT2OID:
2465  case INT4OID:
2466  case INT8OID:
2467  case OIDOID:
2468  case FLOAT4OID:
2469  case FLOAT8OID:
2470  case NUMERICOID:
2471  {
2472  /*
2473  * No need to quote unless it's a special value such as 'NaN'.
2474  * See comments in get_const_expr().
2475  */
2476  if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2477  {
2478  if (extval[0] == '+' || extval[0] == '-')
2479  appendStringInfo(buf, "(%s)", extval);
2480  else
2481  appendStringInfoString(buf, extval);
2482  if (strcspn(extval, "eE.") != strlen(extval))
2483  isfloat = true; /* it looks like a float */
2484  }
2485  else
2486  appendStringInfo(buf, "'%s'", extval);
2487  }
2488  break;
2489  case BITOID:
2490  case VARBITOID:
2491  appendStringInfo(buf, "B'%s'", extval);
2492  break;
2493  case BOOLOID:
2494  if (strcmp(extval, "t") == 0)
2495  appendStringInfoString(buf, "true");
2496  else
2497  appendStringInfoString(buf, "false");
2498  break;
2499  default:
2500  deparseStringLiteral(buf, extval);
2501  break;
2502  }
2503 
2504  pfree(extval);
2505 
2506  if (showtype < 0)
2507  return;
2508 
2509  /*
2510  * For showtype == 0, append ::typename unless the constant will be
2511  * implicitly typed as the right type when it is read in.
2512  *
2513  * XXX this code has to be kept in sync with the behavior of the parser,
2514  * especially make_const.
2515  */
2516  switch (node->consttype)
2517  {
2518  case BOOLOID:
2519  case INT4OID:
2520  case UNKNOWNOID:
2521  needlabel = false;
2522  break;
2523  case NUMERICOID:
2524  needlabel = !isfloat || (node->consttypmod >= 0);
2525  break;
2526  default:
2527  needlabel = true;
2528  break;
2529  }
2530  if (needlabel || showtype > 0)
2531  appendStringInfo(buf, "::%s",
2533  node->consttypmod));
2534 }
Datum constvalue
Definition: primnodes.h:200
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
unsigned int Oid
Definition: postgres_ext.h:31
Oid consttype
Definition: primnodes.h:196
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:2276
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:903
int32 consttypmod
Definition: primnodes.h:197
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1655
bool constisnull
Definition: primnodes.h:201

◆ deparseDeleteSql()

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

Definition at line 1890 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1894 {
1895  appendStringInfoString(buf, "DELETE FROM ");
1896  deparseRelation(buf, rel);
1897  appendStringInfoString(buf, " WHERE ctid = $1");
1898 
1899  deparseReturningList(buf, rte, rtindex, rel,
1900  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
1901  NIL, returningList, retrieved_attrs);
1902 }
#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:1970
TriggerDesc * trigdesc
Definition: rel.h:89
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_delete_after_row
Definition: reltrigger.h:66
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2236

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

1926 {
1927  deparse_expr_cxt context;
1928 
1929  /* Set up context struct for recursion */
1930  context.root = root;
1931  context.foreignrel = foreignrel;
1932  context.scanrel = foreignrel;
1933  context.buf = buf;
1934  context.params_list = params_list;
1935 
1936  appendStringInfoString(buf, "DELETE FROM ");
1937  deparseRelation(buf, rel);
1938  if (foreignrel->reloptkind == RELOPT_JOINREL)
1939  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
1940 
1941  if (foreignrel->reloptkind == RELOPT_JOINREL)
1942  {
1943  List *ignore_conds = NIL;
1944 
1945  appendStringInfoString(buf, " USING ");
1946  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
1947  &ignore_conds, params_list);
1948  remote_conds = list_concat(remote_conds, ignore_conds);
1949  }
1950 
1951  if (remote_conds)
1952  {
1953  appendStringInfoString(buf, " WHERE ");
1954  appendConditions(remote_conds, &context);
1955  }
1956 
1957  if (foreignrel->reloptkind == RELOPT_JOINREL)
1958  deparseExplicitTargetList(returningList, true, retrieved_attrs,
1959  &context);
1960  else
1961  deparseReturningList(buf, planner_rt_fetch(rtindex, root),
1962  rtindex, rel, false,
1963  NIL, returningList, retrieved_attrs);
1964 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:638
List ** params_list
Definition: deparse.c:102
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
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:1970
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:371
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:1390
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1319
StringInfo buf
Definition: deparse.c:101
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2236
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:1467
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 1805 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().

1814 {
1815  deparse_expr_cxt context;
1816  int nestlevel;
1817  bool first;
1818  ListCell *lc;
1819  RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
1820 
1821  /* Set up context struct for recursion */
1822  context.root = root;
1823  context.foreignrel = foreignrel;
1824  context.scanrel = foreignrel;
1825  context.buf = buf;
1826  context.params_list = params_list;
1827 
1828  appendStringInfoString(buf, "UPDATE ");
1829  deparseRelation(buf, rel);
1830  if (foreignrel->reloptkind == RELOPT_JOINREL)
1831  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
1832  appendStringInfoString(buf, " SET ");
1833 
1834  /* Make sure any constants in the exprs are printed portably */
1835  nestlevel = set_transmission_modes();
1836 
1837  first = true;
1838  foreach(lc, targetAttrs)
1839  {
1840  int attnum = lfirst_int(lc);
1841  TargetEntry *tle = get_tle_by_resno(targetlist, attnum);
1842 
1843  if (!tle)
1844  elog(ERROR, "attribute number %d not found in UPDATE targetlist",
1845  attnum);
1846 
1847  if (!first)
1848  appendStringInfoString(buf, ", ");
1849  first = false;
1850 
1851  deparseColumnRef(buf, rtindex, attnum, rte, false);
1852  appendStringInfoString(buf, " = ");
1853  deparseExpr((Expr *) tle->expr, &context);
1854  }
1855 
1856  reset_transmission_modes(nestlevel);
1857 
1858  if (foreignrel->reloptkind == RELOPT_JOINREL)
1859  {
1860  List *ignore_conds = NIL;
1861 
1862  appendStringInfoString(buf, " FROM ");
1863  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
1864  &ignore_conds, params_list);
1865  remote_conds = list_concat(remote_conds, ignore_conds);
1866  }
1867 
1868  if (remote_conds)
1869  {
1870  appendStringInfoString(buf, " WHERE ");
1871  appendConditions(remote_conds, &context);
1872  }
1873 
1874  if (foreignrel->reloptkind == RELOPT_JOINREL)
1875  deparseExplicitTargetList(returningList, true, retrieved_attrs,
1876  &context);
1877  else
1878  deparseReturningList(buf, rte, rtindex, rel, false,
1879  NIL, returningList, retrieved_attrs);
1880 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:638
List ** params_list
Definition: deparse.c:102
int set_transmission_modes(void)
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
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:1970
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:371
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:1390
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1319
StringInfo buf
Definition: deparse.c:101
int16 attnum
Definition: pg_attribute.h:79
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2236
PlannerInfo * root
Definition: deparse.c:96
Expr * expr
Definition: primnodes.h:1393
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1467
#define elog(elevel,...)
Definition: elog.h:228
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:2108

◆ deparseDistinctExpr()

static void deparseDistinctExpr ( DistinctExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2766 of file deparse.c.

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

Referenced by deparseExpr().

2767 {
2768  StringInfo buf = context->buf;
2769 
2770  Assert(list_length(node->args) == 2);
2771 
2772  appendStringInfoChar(buf, '(');
2773  deparseExpr(linitial(node->args), context);
2774  appendStringInfoString(buf, " IS DISTINCT FROM ");
2775  deparseExpr(lsecond(node->args), context);
2776  appendStringInfoChar(buf, ')');
2777 }
#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:739
static int list_length(const List *l)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
List * args
Definition: primnodes.h:508

◆ deparseExplicitTargetList()

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

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

1394 {
1395  ListCell *lc;
1396  StringInfo buf = context->buf;
1397  int i = 0;
1398 
1399  *retrieved_attrs = NIL;
1400 
1401  foreach(lc, tlist)
1402  {
1403  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1404 
1405  if (i > 0)
1406  appendStringInfoString(buf, ", ");
1407  else if (is_returning)
1408  appendStringInfoString(buf, " RETURNING ");
1409 
1410  deparseExpr((Expr *) tle->expr, context);
1411 
1412  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1413  i++;
1414  }
1415 
1416  if (i == 0 && !is_returning)
1417  appendStringInfoString(buf, "NULL");
1418 }
#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:340
StringInfo buf
Definition: deparse.c:101
Expr * expr
Definition: primnodes.h:1393
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
int i

◆ deparseExpr()

static void deparseExpr ( Expr expr,
deparse_expr_cxt context 
)
static

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

2312 {
2313  if (node == NULL)
2314  return;
2315 
2316  switch (nodeTag(node))
2317  {
2318  case T_Var:
2319  deparseVar((Var *) node, context);
2320  break;
2321  case T_Const:
2322  deparseConst((Const *) node, context, 0);
2323  break;
2324  case T_Param:
2325  deparseParam((Param *) node, context);
2326  break;
2327  case T_SubscriptingRef:
2328  deparseSubscriptingRef((SubscriptingRef *) node, context);
2329  break;
2330  case T_FuncExpr:
2331  deparseFuncExpr((FuncExpr *) node, context);
2332  break;
2333  case T_OpExpr:
2334  deparseOpExpr((OpExpr *) node, context);
2335  break;
2336  case T_DistinctExpr:
2337  deparseDistinctExpr((DistinctExpr *) node, context);
2338  break;
2339  case T_ScalarArrayOpExpr:
2340  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2341  break;
2342  case T_RelabelType:
2343  deparseRelabelType((RelabelType *) node, context);
2344  break;
2345  case T_BoolExpr:
2346  deparseBoolExpr((BoolExpr *) node, context);
2347  break;
2348  case T_NullTest:
2349  deparseNullTest((NullTest *) node, context);
2350  break;
2351  case T_ArrayExpr:
2352  deparseArrayExpr((ArrayExpr *) node, context);
2353  break;
2354  case T_Aggref:
2355  deparseAggref((Aggref *) node, context);
2356  break;
2357  default:
2358  elog(ERROR, "unsupported expression type for deparse: %d",
2359  (int) nodeTag(node));
2360  break;
2361  }
2362 }
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:2939
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2842
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:2880
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2766
Definition: primnodes.h:167
#define ERROR
Definition: elog.h:43
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2545
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2784
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2687
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2913
static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2578
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:2829
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2373
Definition: nodes.h:152
Definition: nodes.h:151
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2439
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define elog(elevel,...)
Definition: elog.h:228
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2624
Definition: nodes.h:153

◆ deparseFromExpr()

static void deparseFromExpr ( List quals,
deparse_expr_cxt context 
)
static

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

1120 {
1121  StringInfo buf = context->buf;
1122  RelOptInfo *scanrel = context->scanrel;
1123 
1124  /* For upper relations, scanrel must be either a joinrel or a baserel */
1125  Assert(!IS_UPPER_REL(context->foreignrel) ||
1126  IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1127 
1128  /* Construct FROM clause */
1129  appendStringInfoString(buf, " FROM ");
1130  deparseFromExprForRel(buf, context->root, scanrel,
1131  (bms_membership(scanrel->relids) == BMS_MULTIPLE),
1132  (Index) 0, NULL, context->params_list);
1133 
1134  /* Construct WHERE clause */
1135  if (quals != NIL)
1136  {
1137  appendStringInfoString(buf, " WHERE ");
1138  appendConditions(quals, context);
1139  }
1140 }
#define NIL
Definition: pg_list.h:65
List ** params_list
Definition: deparse.c:102
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:619
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
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:641
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1319
StringInfo buf
Definition: deparse.c:101
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
unsigned int Index
Definition: c.h:476
PlannerInfo * root
Definition: deparse.c:96
#define Assert(condition)
Definition: c.h:739
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1467
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 1467 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().

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

2625 {
2626  StringInfo buf = context->buf;
2627  bool use_variadic;
2628  bool first;
2629  ListCell *arg;
2630 
2631  /*
2632  * If the function call came from an implicit coercion, then just show the
2633  * first argument.
2634  */
2635  if (node->funcformat == COERCE_IMPLICIT_CAST)
2636  {
2637  deparseExpr((Expr *) linitial(node->args), context);
2638  return;
2639  }
2640 
2641  /*
2642  * If the function call came from a cast, then show the first argument
2643  * plus an explicit cast operation.
2644  */
2645  if (node->funcformat == COERCE_EXPLICIT_CAST)
2646  {
2647  Oid rettype = node->funcresulttype;
2648  int32 coercedTypmod;
2649 
2650  /* Get the typmod if this is a length-coercion function */
2651  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2652 
2653  deparseExpr((Expr *) linitial(node->args), context);
2654  appendStringInfo(buf, "::%s",
2655  deparse_type_name(rettype, coercedTypmod));
2656  return;
2657  }
2658 
2659  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2660  use_variadic = node->funcvariadic;
2661 
2662  /*
2663  * Normal function: display as proname(args).
2664  */
2665  appendFunctionName(node->funcid, context);
2666  appendStringInfoChar(buf, '(');
2667 
2668  /* ... and all the arguments */
2669  first = true;
2670  foreach(arg, node->args)
2671  {
2672  if (!first)
2673  appendStringInfoString(buf, ", ");
2674  if (use_variadic && lnext(node->args, arg) == NULL)
2675  appendStringInfoString(buf, "VARIADIC ");
2676  deparseExpr((Expr *) lfirst(arg), context);
2677  first = false;
2678  }
2679  appendStringInfoChar(buf, ')');
2680 }
Oid funcresulttype
Definition: primnodes.h:456
List * args
Definition: primnodes.h:463
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:3248
Definition: nodes.h:525
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:347
CoercionForm funcformat
Definition: primnodes.h:460
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:195
Oid funcid
Definition: primnodes.h:455
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:903
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
void * arg
bool funcvariadic
Definition: primnodes.h:458

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

1694 {
1695  AttrNumber pindex;
1696  bool first;
1697  ListCell *lc;
1698 
1699  appendStringInfoString(buf, "INSERT INTO ");
1700  deparseRelation(buf, rel);
1701 
1702  if (targetAttrs)
1703  {
1704  appendStringInfoChar(buf, '(');
1705 
1706  first = true;
1707  foreach(lc, targetAttrs)
1708  {
1709  int attnum = lfirst_int(lc);
1710 
1711  if (!first)
1712  appendStringInfoString(buf, ", ");
1713  first = false;
1714 
1715  deparseColumnRef(buf, rtindex, attnum, rte, false);
1716  }
1717 
1718  appendStringInfoString(buf, ") VALUES (");
1719 
1720  pindex = 1;
1721  first = true;
1722  foreach(lc, targetAttrs)
1723  {
1724  if (!first)
1725  appendStringInfoString(buf, ", ");
1726  first = false;
1727 
1728  appendStringInfo(buf, "$%d", pindex);
1729  pindex++;
1730  }
1731 
1732  appendStringInfoChar(buf, ')');
1733  }
1734  else
1735  appendStringInfoString(buf, " DEFAULT VALUES");
1736 
1737  if (doNothing)
1738  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1739 
1740  deparseReturningList(buf, rte, rtindex, rel,
1741  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1742  withCheckOptionList, returningList, retrieved_attrs);
1743 }
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:1970
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:89
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_insert_after_row
Definition: reltrigger.h:56
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:2236
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2108

◆ deparseLockingClause()

static void deparseLockingClause ( deparse_expr_cxt context)
static

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

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

2881 {
2882  StringInfo buf = context->buf;
2883 
2884  appendStringInfoChar(buf, '(');
2885  deparseExpr(node->arg, context);
2886 
2887  /*
2888  * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
2889  * shorter and traditional. If it's a rowtype input but we're applying a
2890  * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
2891  * correct.
2892  */
2893  if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
2894  {
2895  if (node->nulltesttype == IS_NULL)
2896  appendStringInfoString(buf, " IS NULL)");
2897  else
2898  appendStringInfoString(buf, " IS NOT NULL)");
2899  }
2900  else
2901  {
2902  if (node->nulltesttype == IS_NULL)
2903  appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
2904  else
2905  appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
2906  }
2907 }
Definition: nodes.h:525
Expr * arg
Definition: primnodes.h:1205
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:2433
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:101
NullTestType nulltesttype
Definition: primnodes.h:1206
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
bool argisrow
Definition: primnodes.h:1207

◆ deparseOperatorName()

static void deparseOperatorName ( StringInfo  buf,
Form_pg_operator  opform 
)
static

Definition at line 2738 of file deparse.c.

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

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

2739 {
2740  char *opname;
2741 
2742  /* opname is not a SQL identifier, so we should not quote it. */
2743  opname = NameStr(opform->oprname);
2744 
2745  /* Print schema name only if it's not pg_catalog */
2746  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
2747  {
2748  const char *opnspname;
2749 
2750  opnspname = get_namespace_name(opform->oprnamespace);
2751  /* Print fully qualified operator name. */
2752  appendStringInfo(buf, "OPERATOR(%s.%s)",
2753  quote_identifier(opnspname), opname);
2754  }
2755  else
2756  {
2757  /* Just print operator name. */
2758  appendStringInfoString(buf, opname);
2759  }
2760 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10640
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:3094
#define NameStr(name)
Definition: c.h:616

◆ deparseOpExpr()

static void deparseOpExpr ( OpExpr node,
deparse_expr_cxt context 
)
static

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

2688 {
2689  StringInfo buf = context->buf;
2690  HeapTuple tuple;
2691  Form_pg_operator form;
2692  char oprkind;
2693  ListCell *arg;
2694 
2695  /* Retrieve information about the operator from system catalog. */
2696  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2697  if (!HeapTupleIsValid(tuple))
2698  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2699  form = (Form_pg_operator) GETSTRUCT(tuple);
2700  oprkind = form->oprkind;
2701 
2702  /* Sanity check. */
2703  Assert((oprkind == 'r' && list_length(node->args) == 1) ||
2704  (oprkind == 'l' && list_length(node->args) == 1) ||
2705  (oprkind == 'b' && list_length(node->args) == 2));
2706 
2707  /* Always parenthesize the expression. */
2708  appendStringInfoChar(buf, '(');
2709 
2710  /* Deparse left operand. */
2711  if (oprkind == 'r' || oprkind == 'b')
2712  {
2713  arg = list_head(node->args);
2714  deparseExpr(lfirst(arg), context);
2715  appendStringInfoChar(buf, ' ');
2716  }
2717 
2718  /* Deparse operator name. */
2719  deparseOperatorName(buf, form);
2720 
2721  /* Deparse right operand. */
2722  if (oprkind == 'l' || oprkind == 'b')
2723  {
2724  arg = list_tail(node->args);
2725  appendStringInfoChar(buf, ' ');
2726  deparseExpr(lfirst(arg), context);
2727  }
2728 
2729  appendStringInfoChar(buf, ')');
2730 
2731  ReleaseSysCache(tuple);
2732 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2738
#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:739
#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:2311
#define elog(elevel,...)
Definition: elog.h:228
static ListCell * list_tail(const List *l)
Definition: pg_list.h:132
void * arg
Oid opno
Definition: primnodes.h:502
List * args
Definition: primnodes.h:508

◆ deparseParam()

static void deparseParam ( Param node,
deparse_expr_cxt context 
)
static

Definition at line 2545 of file deparse.c.

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

Referenced by deparseExpr().

2546 {
2547  if (context->params_list)
2548  {
2549  int pindex = 0;
2550  ListCell *lc;
2551 
2552  /* find its index in params_list */
2553  foreach(lc, *context->params_list)
2554  {
2555  pindex++;
2556  if (equal(node, (Node *) lfirst(lc)))
2557  break;
2558  }
2559  if (lc == NULL)
2560  {
2561  /* not in list, so add it */
2562  pindex++;
2563  *context->params_list = lappend(*context->params_list, node);
2564  }
2565 
2566  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2567  }
2568  else
2569  {
2570  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2571  }
2572 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3011
List ** params_list
Definition: deparse.c:102
Definition: nodes.h:525
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3090
List * lappend(List *list, void *datum)
Definition: list.c:322
int32 paramtypmod
Definition: primnodes.h:251
#define lfirst(lc)
Definition: pg_list.h:190
Oid paramtype
Definition: primnodes.h:250
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3116

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

1621 {
1622  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1623 
1624  /* Should only be called in these cases. */
1625  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1626 
1627  Assert(fpinfo->local_conds == NIL);
1628 
1629  /* If make_subquery is true, deparse the relation as a subquery. */
1630  if (make_subquery)
1631  {
1632  List *retrieved_attrs;
1633  int ncols;
1634 
1635  /*
1636  * The given relation shouldn't contain the target relation, because
1637  * this should only happen for input relations for a full join, and
1638  * such relations can never contain an UPDATE/DELETE target.
1639  */
1640  Assert(ignore_rel == 0 ||
1641  !bms_is_member(ignore_rel, foreignrel->relids));
1642 
1643  /* Deparse the subquery representing the relation. */
1644  appendStringInfoChar(buf, '(');
1645  deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1646  fpinfo->remote_conds, NIL,
1647  false, false, true,
1648  &retrieved_attrs, params_list);
1649  appendStringInfoChar(buf, ')');
1650 
1651  /* Append the relation alias. */
1653  fpinfo->relation_index);
1654 
1655  /*
1656  * Append the column aliases if needed. Note that the subquery emits
1657  * expressions specified in the relation's reltarget (see
1658  * deparseSubqueryTargetList).
1659  */
1660  ncols = list_length(foreignrel->reltarget->exprs);
1661  if (ncols > 0)
1662  {
1663  int i;
1664 
1665  appendStringInfoChar(buf, '(');
1666  for (i = 1; i <= ncols; i++)
1667  {
1668  if (i > 1)
1669  appendStringInfoString(buf, ", ");
1670 
1672  }
1673  appendStringInfoChar(buf, ')');
1674  }
1675  }
1676  else
1677  deparseFromExprForRel(buf, root, foreignrel, true, ignore_rel,
1678  ignore_conds, params_list);
1679 }
#define NIL
Definition: pg_list.h:65
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:619
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
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:641
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
List * exprs
Definition: pathnodes.h:1044
void * fdw_private
Definition: pathnodes.h:695
#define Assert(condition)
Definition: c.h:739
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:979
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:1467
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:652

◆ deparseRelabelType()

static void deparseRelabelType ( RelabelType node,
deparse_expr_cxt context 
)
static

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

2830 {
2831  deparseExpr(node->arg, context);
2832  if (node->relabelformat != COERCE_IMPLICIT_CAST)
2833  appendStringInfo(context->buf, "::%s",
2835  node->resulttypmod));
2836 }
Expr * arg
Definition: primnodes.h:800
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
StringInfo buf
Definition: deparse.c:101
Oid resulttype
Definition: primnodes.h:801
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:903
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
int32 resulttypmod
Definition: primnodes.h:802
CoercionForm relabelformat
Definition: primnodes.h:804

◆ deparseRelation()

static void deparseRelation ( StringInfo  buf,
Relation  rel 
)
static

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

2237 {
2238  ForeignTable *table;
2239  const char *nspname = NULL;
2240  const char *relname = NULL;
2241  ListCell *lc;
2242 
2243  /* obtain additional catalog information. */
2244  table = GetForeignTable(RelationGetRelid(rel));
2245 
2246  /*
2247  * Use value of FDW options if any, instead of the name of object itself.
2248  */
2249  foreach(lc, table->options)
2250  {
2251  DefElem *def = (DefElem *) lfirst(lc);
2252 
2253  if (strcmp(def->defname, "schema_name") == 0)
2254  nspname = defGetString(def);
2255  else if (strcmp(def->defname, "table_name") == 0)
2256  relname = defGetString(def);
2257  }
2258 
2259  /*
2260  * Note: we could skip printing the schema name if it's pg_catalog, but
2261  * that doesn't seem worth the trouble.
2262  */
2263  if (nspname == NULL)
2264  nspname = get_namespace_name(RelationGetNamespace(rel));
2265  if (relname == NULL)
2266  relname = RelationGetRelationName(rel);
2267 
2268  appendStringInfo(buf, "%s.%s",
2269  quote_identifier(nspname), quote_identifier(relname));
2270 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10640
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:248
NameData relname
Definition: pg_class.h:35
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:3094
#define RelationGetRelationName(relation)
Definition: rel.h:456
#define lfirst(lc)
Definition: pg_list.h:190
List * options
Definition: foreign.h:57
char * defname
Definition: parsenodes.h:730
#define RelationGetRelid(relation)
Definition: rel.h:422
#define RelationGetNamespace(relation)
Definition: rel.h:463

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

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

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

1976 {
1977  Bitmapset *attrs_used = NULL;
1978 
1979  if (trig_after_row)
1980  {
1981  /* whole-row reference acquires all non-system columns */
1982  attrs_used =
1984  }
1985 
1986  if (withCheckOptionList != NIL)
1987  {
1988  /*
1989  * We need the attrs, non-system and system, mentioned in the local
1990  * query's WITH CHECK OPTION list.
1991  *
1992  * Note: we do this to ensure that WCO constraints will be evaluated
1993  * on the data actually inserted/updated on the remote side, which
1994  * might differ from the data supplied by the core code, for example
1995  * as a result of remote triggers.
1996  */
1997  pull_varattnos((Node *) withCheckOptionList, rtindex,
1998  &attrs_used);
1999  }
2000 
2001  if (returningList != NIL)
2002  {
2003  /*
2004  * We need the attrs, non-system and system, mentioned in the local
2005  * query's RETURNING list.
2006  */
2007  pull_varattnos((Node *) returningList, rtindex,
2008  &attrs_used);
2009  }
2010 
2011  if (attrs_used != NULL)
2012  deparseTargetList(buf, rte, rtindex, rel, true, attrs_used, false,
2013  retrieved_attrs);
2014  else
2015  *retrieved_attrs = NIL;
2016 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:525
#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:1153
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186

◆ deparseScalarArrayOpExpr()

static void deparseScalarArrayOpExpr ( ScalarArrayOpExpr node,
deparse_expr_cxt context 
)
static

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

2785 {
2786  StringInfo buf = context->buf;
2787  HeapTuple tuple;
2788  Form_pg_operator form;
2789  Expr *arg1;
2790  Expr *arg2;
2791 
2792  /* Retrieve information about the operator from system catalog. */
2793  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2794  if (!HeapTupleIsValid(tuple))
2795  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2796  form = (Form_pg_operator) GETSTRUCT(tuple);
2797 
2798  /* Sanity check. */
2799  Assert(list_length(node->args) == 2);
2800 
2801  /* Always parenthesize the expression. */
2802  appendStringInfoChar(buf, '(');
2803 
2804  /* Deparse left operand. */
2805  arg1 = linitial(node->args);
2806  deparseExpr(arg1, context);
2807  appendStringInfoChar(buf, ' ');
2808 
2809  /* Deparse operator name plus decoration. */
2810  deparseOperatorName(buf, form);
2811  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
2812 
2813  /* Deparse right operand. */
2814  arg2 = lsecond(node->args);
2815  deparseExpr(arg2, context);
2816 
2817  appendStringInfoChar(buf, ')');
2818 
2819  /* Always parenthesize the expression. */
2820  appendStringInfoChar(buf, ')');
2821 
2822  ReleaseSysCache(tuple);
2823 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2738
#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:739
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:2311
#define elog(elevel,...)
Definition: elog.h:228

◆ deparseSelectSql()

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

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

1063 {
1064  StringInfo buf = context->buf;
1065  RelOptInfo *foreignrel = context->foreignrel;
1066  PlannerInfo *root = context->root;
1067  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1068 
1069  /*
1070  * Construct SELECT list
1071  */
1072  appendStringInfoString(buf, "SELECT ");
1073 
1074  if (is_subquery)
1075  {
1076  /*
1077  * For a relation that is deparsed as a subquery, emit expressions
1078  * specified in the relation's reltarget. Note that since this is for
1079  * the subquery, no need to care about *retrieved_attrs.
1080  */
1081  deparseSubqueryTargetList(context);
1082  }
1083  else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
1084  {
1085  /*
1086  * For a join or upper relation the input tlist gives the list of
1087  * columns required to be fetched from the foreign server.
1088  */
1089  deparseExplicitTargetList(tlist, false, retrieved_attrs, context);
1090  }
1091  else
1092  {
1093  /*
1094  * For a base relation fpinfo->attrs_used gives the list of columns
1095  * required to be fetched from the foreign server.
1096  */
1097  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1098 
1099  /*
1100  * Core code already has some lock on each rel being planned, so we
1101  * can use NoLock here.
1102  */
1103  Relation rel = table_open(rte->relid, NoLock);
1104 
1105  deparseTargetList(buf, rte, foreignrel->relid, rel, false,
1106  fpinfo->attrs_used, false, retrieved_attrs);
1107  table_close(rel, NoLock);
1108  }
1109 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1426
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:619
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:1153
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:371
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:1390
Index relid
Definition: pathnodes.h:669
StringInfo buf
Definition: deparse.c:101
void * fdw_private
Definition: pathnodes.h:695
PlannerInfo * root
Definition: deparse.c:96
Bitmapset * attrs_used
Definition: postgres_fdw.h:49
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
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 979 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().

983 {
984  deparse_expr_cxt context;
986  List *quals;
987 
988  /*
989  * We handle relations for foreign tables, joins between those and upper
990  * relations.
991  */
992  Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel));
993 
994  /* Fill portions of context common to upper, join and base relation */
995  context.buf = buf;
996  context.root = root;
997  context.foreignrel = rel;
998  context.scanrel = IS_UPPER_REL(rel) ? fpinfo->outerrel : rel;
999  context.params_list = params_list;
1000 
1001  /* Construct SELECT clause */
1002  deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
1003 
1004  /*
1005  * For upper relations, the WHERE clause is built from the remote
1006  * conditions of the underlying scan relation; otherwise, we can use the
1007  * supplied list of remote conditions directly.
1008  */
1009  if (IS_UPPER_REL(rel))
1010  {
1011  PgFdwRelationInfo *ofpinfo;
1012 
1013  ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
1014  quals = ofpinfo->remote_conds;
1015  }
1016  else
1017  quals = remote_conds;
1018 
1019  /* Construct FROM and WHERE clauses */
1020  deparseFromExpr(quals, &context);
1021 
1022  if (IS_UPPER_REL(rel))
1023  {
1024  /* Append GROUP BY clause */
1025  appendGroupByClause(tlist, &context);
1026 
1027  /* Append HAVING clause */
1028  if (remote_conds)
1029  {
1030  appendStringInfoString(buf, " HAVING ");
1031  appendConditions(remote_conds, &context);
1032  }
1033  }
1034 
1035  /* Add ORDER BY clause if we found any useful pathkeys */
1036  if (pathkeys)
1037  appendOrderByClause(pathkeys, has_final_sort, &context);
1038 
1039  /* Add LIMIT clause if necessary */
1040  if (has_limit)
1041  appendLimitClause(&context);
1042 
1043  /* Add any necessary FOR UPDATE/SHARE. */
1044  deparseLockingClause(&context);
1045 }
static void appendGroupByClause(List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3129
List ** params_list
Definition: deparse.c:102
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:619
static void appendLimitClause(deparse_expr_cxt *context)
Definition: deparse.c:3220
RelOptInfo * outerrel
Definition: postgres_fdw.h:100
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
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:3166
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1061
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1319
StringInfo buf
Definition: deparse.c:101
void * fdw_private
Definition: pathnodes.h:695
PlannerInfo * root
Definition: deparse.c:96
#define Assert(condition)
Definition: c.h:739
static void deparseFromExpr(List *quals, deparse_expr_cxt *context)
Definition: deparse.c:1119
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
static void deparseLockingClause(deparse_expr_cxt *context)
Definition: deparse.c:1229
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 3283 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().

3285 {
3286  StringInfo buf = context->buf;
3287  TargetEntry *tle;
3288  Expr *expr;
3289 
3290  tle = get_sortgroupref_tle(ref, tlist);
3291  expr = tle->expr;
3292 
3293  if (force_colno)
3294  {
3295  /* Use column-number form when requested by caller. */
3296  Assert(!tle->resjunk);
3297  appendStringInfo(buf, "%d", tle->resno);
3298  }
3299  else if (expr && IsA(expr, Const))
3300  {
3301  /*
3302  * Force a typecast here so that we don't emit something like "GROUP
3303  * BY 2", which will be misconstrued as a column position rather than
3304  * a constant.
3305  */
3306  deparseConst((Const *) expr, context, 1);
3307  }
3308  else if (!expr || IsA(expr, Var))
3309  deparseExpr(expr, context);
3310  else
3311  {
3312  /* Always parenthesize the expression. */
3313  appendStringInfoChar(buf, '(');
3314  deparseExpr(expr, context);
3315  appendStringInfoChar(buf, ')');
3316  }
3317 
3318  return (Node *) expr;
3319 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Definition: nodes.h:525
Definition: primnodes.h:167
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool resjunk
Definition: primnodes.h:1400
static char * buf
Definition: pg_test_fsync.c:67
AttrNumber resno
Definition: primnodes.h:1394
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:739
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2439
Expr * expr
Definition: primnodes.h:1393
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311

◆ deparseStringLiteral()

void deparseStringLiteral ( StringInfo  buf,
const char *  val 
)

Definition at line 2276 of file deparse.c.

References appendStringInfoChar(), ESCAPE_STRING_SYNTAX, and SQL_STR_DOUBLE.

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

2277 {
2278  const char *valptr;
2279 
2280  /*
2281  * Rather than making assumptions about the remote server's value of
2282  * standard_conforming_strings, always use E'foo' syntax if there are any
2283  * backslashes. This will fail on remote servers before 8.1, but those
2284  * are long out of support.
2285  */
2286  if (strchr(val, '\\') != NULL)
2288  appendStringInfoChar(buf, '\'');
2289  for (valptr = val; *valptr; valptr++)
2290  {
2291  char ch = *valptr;
2292 
2293  if (SQL_STR_DOUBLE(ch, true))
2294  appendStringInfoChar(buf, ch);
2295  appendStringInfoChar(buf, ch);
2296  }
2297  appendStringInfoChar(buf, '\'');
2298 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1117
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1114
long val
Definition: informix.c:664

◆ deparseSubqueryTargetList()

static void deparseSubqueryTargetList ( deparse_expr_cxt context)
static

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

1427 {
1428  StringInfo buf = context->buf;
1429  RelOptInfo *foreignrel = context->foreignrel;
1430  bool first;
1431  ListCell *lc;
1432 
1433  /* Should only be called in these cases. */
1434  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1435 
1436  first = true;
1437  foreach(lc, foreignrel->reltarget->exprs)
1438  {
1439  Node *node = (Node *) lfirst(lc);
1440 
1441  if (!first)
1442  appendStringInfoString(buf, ", ");
1443  first = false;
1444 
1445  deparseExpr((Expr *) node, context);
1446  }
1447 
1448  /* Don't generate bad syntax if no expressions */
1449  if (first)
1450  appendStringInfoString(buf, "NULL");
1451 }
Definition: nodes.h:525
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:619
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
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:1044
StringInfo buf
Definition: deparse.c:101
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2311
RelOptInfo * foreignrel
Definition: deparse.c:97
struct PathTarget * reltarget
Definition: pathnodes.h:652

◆ deparseSubscriptingRef()

static void deparseSubscriptingRef ( SubscriptingRef node,
deparse_expr_cxt context 
)
static

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

2579 {
2580  StringInfo buf = context->buf;
2581  ListCell *lowlist_item;
2582  ListCell *uplist_item;
2583 
2584  /* Always parenthesize the expression. */
2585  appendStringInfoChar(buf, '(');
2586 
2587  /*
2588  * Deparse referenced array expression first. If that expression includes
2589  * a cast, we have to parenthesize to prevent the array subscript from
2590  * being taken as typename decoration. We can avoid that in the typical
2591  * case of subscripting a Var, but otherwise do it.
2592  */
2593  if (IsA(node->refexpr, Var))
2594  deparseExpr(node->refexpr, context);
2595  else
2596  {
2597  appendStringInfoChar(buf, '(');
2598  deparseExpr(node->refexpr, context);
2599  appendStringInfoChar(buf, ')');
2600  }
2601 
2602  /* Deparse subscript expressions. */
2603  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2604  foreach(uplist_item, node->refupperindexpr)
2605  {
2606  appendStringInfoChar(buf, '[');
2607  if (lowlist_item)
2608  {
2609  deparseExpr(lfirst(lowlist_item), context);
2610  appendStringInfoChar(buf, ':');
2611  lowlist_item = lnext(node->reflowerindexpr, lowlist_item);
2612  }
2613  deparseExpr(lfirst(uplist_item), context);
2614  appendStringInfoChar(buf, ']');
2615  }
2616 
2617  appendStringInfoChar(buf, ')');
2618 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
Definition: primnodes.h:167
List * refupperindexpr
Definition: primnodes.h:408
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:2311
List * reflowerindexpr
Definition: primnodes.h:410
Expr * refexpr
Definition: primnodes.h:413

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

1161 {
1162  TupleDesc tupdesc = RelationGetDescr(rel);
1163  bool have_wholerow;
1164  bool first;
1165  int i;
1166 
1167  *retrieved_attrs = NIL;
1168 
1169  /* If there's a whole-row reference, we'll need all the columns. */
1171  attrs_used);
1172 
1173  first = true;
1174  for (i = 1; i <= tupdesc->natts; i++)
1175  {
1176  Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1);
1177 
1178  /* Ignore dropped attributes. */
1179  if (attr->attisdropped)
1180  continue;
1181 
1182  if (have_wholerow ||
1184  attrs_used))
1185  {
1186  if (!first)
1187  appendStringInfoString(buf, ", ");
1188  else if (is_returning)
1189  appendStringInfoString(buf, " RETURNING ");
1190  first = false;
1191 
1192  deparseColumnRef(buf, rtindex, i, rte, qualify_col);
1193 
1194  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1195  }
1196  }
1197 
1198  /*
1199  * Add ctid if needed. We currently don't support retrieving any other
1200  * system columns.
1201  */
1203  attrs_used))
1204  {
1205  if (!first)
1206  appendStringInfoString(buf, ", ");
1207  else if (is_returning)
1208  appendStringInfoString(buf, " RETURNING ");
1209  first = false;
1210 
1211  if (qualify_col)
1212  ADD_REL_QUALIFIER(buf, rtindex);
1213  appendStringInfoString(buf, "ctid");
1214 
1215  *retrieved_attrs = lappend_int(*retrieved_attrs,
1217  }
1218 
1219  /* Don't generate bad syntax if no undropped columns */
1220  if (first && !is_returning)
1221  appendStringInfoString(buf, "NULL");
1222 }
#define NIL
Definition: pg_list.h:65
#define RelationGetDescr(relation)
Definition: rel.h:448
#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:200
List * lappend_int(List *list, int datum)
Definition: list.c:340
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:2108

◆ deparseUpdateSql()

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

Definition at line 1753 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1758 {
1759  AttrNumber pindex;
1760  bool first;
1761  ListCell *lc;
1762 
1763  appendStringInfoString(buf, "UPDATE ");
1764  deparseRelation(buf, rel);
1765  appendStringInfoString(buf, " SET ");
1766 
1767  pindex = 2; /* ctid is always the first param */
1768  first = true;
1769  foreach(lc, targetAttrs)
1770  {
1771  int attnum = lfirst_int(lc);
1772 
1773  if (!first)
1774  appendStringInfoString(buf, ", ");
1775  first = false;
1776 
1777  deparseColumnRef(buf, rtindex, attnum, rte, false);
1778  appendStringInfo(buf, " = $%d", pindex);
1779  pindex++;
1780  }
1781  appendStringInfoString(buf, " WHERE ctid = $1");
1782 
1783  deparseReturningList(buf, rte, rtindex, rel,
1784  rel->trigdesc && rel->trigdesc->trig_update_after_row,
1785  withCheckOptionList, returningList, retrieved_attrs);
1786 }
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:1970
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:89
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_update_after_row
Definition: reltrigger.h:61
int16 attnum
Definition: pg_attribute.h:79
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2236
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2108

◆ deparseVar()

static void deparseVar ( Var node,
deparse_expr_cxt context 
)
static

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

2374 {
2375  Relids relids = context->scanrel->relids;
2376  int relno;
2377  int colno;
2378 
2379  /* Qualify columns when multiple relations are involved. */
2380  bool qualify_col = (bms_membership(relids) == BMS_MULTIPLE);
2381 
2382  /*
2383  * If the Var belongs to the foreign relation that is deparsed as a
2384  * subquery, use the relation and column alias to the Var provided by the
2385  * subquery, instead of the remote name.
2386  */
2387  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2388  {
2389  appendStringInfo(context->buf, "%s%d.%s%d",
2391  SUBQUERY_COL_ALIAS_PREFIX, colno);
2392  return;
2393  }
2394 
2395  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2396  deparseColumnRef(context->buf, node->varno, node->varattno,
2397  planner_rt_fetch(node->varno, context->root),
2398  qualify_col);
2399  else
2400  {
2401  /* Treat like a Param */
2402  if (context->params_list)
2403  {
2404  int pindex = 0;
2405  ListCell *lc;
2406 
2407  /* find its index in params_list */
2408  foreach(lc, *context->params_list)
2409  {
2410  pindex++;
2411  if (equal(node, (Node *) lfirst(lc)))
2412  break;
2413  }
2414  if (lc == NULL)
2415  {
2416  /* not in list, so add it */
2417  pindex++;
2418  *context->params_list = lappend(*context->params_list, node);
2419  }
2420 
2421  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2422  }
2423  else
2424  {
2425  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2426  }
2427  }
2428 }
Index varlevelsup
Definition: primnodes.h:177
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3011
List ** params_list
Definition: deparse.c:102
Definition: nodes.h:525
AttrNumber varattno
Definition: primnodes.h:172
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3090
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:174
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:109
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:371
Relids relids
Definition: pathnodes.h:641
List * lappend(List *list, void *datum)
Definition: list.c:322
Index varno
Definition: primnodes.h:170
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:3328
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:2108
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3116
int32 vartypmod
Definition: primnodes.h:175

◆ 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::paramcollid, 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  * Collation rule is same as for Consts and non-foreign Vars.
395  */
396  collation = p->paramcollid;
397  if (collation == InvalidOid ||
398  collation == DEFAULT_COLLATION_OID)
399  state = FDW_COLLATE_NONE;
400  else
401  state = FDW_COLLATE_UNSAFE;
402  }
403  break;
404  case T_SubscriptingRef:
405  {
406  SubscriptingRef *sr = (SubscriptingRef *) node;
407 
408  /* Assignment should not be in restrictions. */
409  if (sr->refassgnexpr != NULL)
410  return false;
411 
412  /*
413  * Recurse to remaining subexpressions. Since the container
414  * subscripts must yield (noncollatable) integers, they won't
415  * affect the inner_cxt state.
416  */
418  glob_cxt, &inner_cxt))
419  return false;
421  glob_cxt, &inner_cxt))
422  return false;
423  if (!foreign_expr_walker((Node *) sr->refexpr,
424  glob_cxt, &inner_cxt))
425  return false;
426 
427  /*
428  * Container subscripting should yield same collation as
429  * input, but for safety use same logic as for function nodes.
430  */
431  collation = sr->refcollid;
432  if (collation == InvalidOid)
433  state = FDW_COLLATE_NONE;
434  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
435  collation == inner_cxt.collation)
436  state = FDW_COLLATE_SAFE;
437  else if (collation == DEFAULT_COLLATION_OID)
438  state = FDW_COLLATE_NONE;
439  else
440  state = FDW_COLLATE_UNSAFE;
441  }
442  break;
443  case T_FuncExpr:
444  {
445  FuncExpr *fe = (FuncExpr *) node;
446 
447  /*
448  * If function used by the expression is not shippable, it
449  * can't be sent to remote because it might have incompatible
450  * semantics on remote side.
451  */
452  if (!is_shippable(fe->funcid, ProcedureRelationId, fpinfo))
453  return false;
454 
455  /*
456  * Recurse to input subexpressions.
457  */
458  if (!foreign_expr_walker((Node *) fe->args,
459  glob_cxt, &inner_cxt))
460  return false;
461 
462  /*
463  * If function's input collation is not derived from a foreign
464  * Var, it can't be sent to remote.
465  */
466  if (fe->inputcollid == InvalidOid)
467  /* OK, inputs are all noncollatable */ ;
468  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
469  fe->inputcollid != inner_cxt.collation)
470  return false;
471 
472  /*
473  * Detect whether node is introducing a collation not derived
474  * from a foreign Var. (If so, we just mark it unsafe for now
475  * rather than immediately returning false, since the parent
476  * node might not care.)
477  */
478  collation = fe->funccollid;
479  if (collation == InvalidOid)
480  state = FDW_COLLATE_NONE;
481  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
482  collation == inner_cxt.collation)
483  state = FDW_COLLATE_SAFE;
484  else if (collation == DEFAULT_COLLATION_OID)
485  state = FDW_COLLATE_NONE;
486  else
487  state = FDW_COLLATE_UNSAFE;
488  }
489  break;
490  case T_OpExpr:
491  case T_DistinctExpr: /* struct-equivalent to OpExpr */
492  {
493  OpExpr *oe = (OpExpr *) node;
494 
495  /*
496  * Similarly, only shippable operators can be sent to remote.
497  * (If the operator is shippable, we assume its underlying
498  * function is too.)
499  */
500  if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
501  return false;
502 
503  /*
504  * Recurse to input subexpressions.
505  */
506  if (!foreign_expr_walker((Node *) oe->args,
507  glob_cxt, &inner_cxt))
508  return false;
509 
510  /*
511  * If operator's input collation is not derived from a foreign
512  * Var, it can't be sent to remote.
513  */
514  if (oe->inputcollid == InvalidOid)
515  /* OK, inputs are all noncollatable */ ;
516  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
517  oe->inputcollid != inner_cxt.collation)
518  return false;
519 
520  /* Result-collation handling is same as for functions */
521  collation = oe->opcollid;
522  if (collation == InvalidOid)
523  state = FDW_COLLATE_NONE;
524  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
525  collation == inner_cxt.collation)
526  state = FDW_COLLATE_SAFE;
527  else if (collation == DEFAULT_COLLATION_OID)
528  state = FDW_COLLATE_NONE;
529  else
530  state = FDW_COLLATE_UNSAFE;
531  }
532  break;
533  case T_ScalarArrayOpExpr:
534  {
535  ScalarArrayOpExpr *oe = (ScalarArrayOpExpr *) node;
536 
537  /*
538  * Again, only shippable operators can be sent to remote.
539  */
540  if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
541  return false;
542 
543  /*
544  * Recurse to input subexpressions.
545  */
546  if (!foreign_expr_walker((Node *) oe->args,
547  glob_cxt, &inner_cxt))
548  return false;
549 
550  /*
551  * If operator's input collation is not derived from a foreign
552  * Var, it can't be sent to remote.
553  */
554  if (oe->inputcollid == InvalidOid)
555  /* OK, inputs are all noncollatable */ ;
556  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
557  oe->inputcollid != inner_cxt.collation)
558  return false;
559 
560  /* Output is always boolean and so noncollatable. */
561  collation = InvalidOid;
562  state = FDW_COLLATE_NONE;
563  }
564  break;
565  case T_RelabelType:
566  {
567  RelabelType *r = (RelabelType *) node;
568 
569  /*
570  * Recurse to input subexpression.
571  */
572  if (!foreign_expr_walker((Node *) r->arg,
573  glob_cxt, &inner_cxt))
574  return false;
575 
576  /*
577  * RelabelType must not introduce a collation not derived from
578  * an input foreign Var (same logic as for a real function).
579  */
580  collation = r->resultcollid;
581  if (collation == InvalidOid)
582  state = FDW_COLLATE_NONE;
583  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
584  collation == inner_cxt.collation)
585  state = FDW_COLLATE_SAFE;
586  else if (collation == DEFAULT_COLLATION_OID)
587  state = FDW_COLLATE_NONE;
588  else
589  state = FDW_COLLATE_UNSAFE;
590  }
591  break;
592  case T_BoolExpr:
593  {
594  BoolExpr *b = (BoolExpr *) node;
595 
596  /*
597  * Recurse to input subexpressions.
598  */
599  if (!foreign_expr_walker((Node *) b->args,
600  glob_cxt, &inner_cxt))
601  return false;
602 
603  /* Output is always boolean and so noncollatable. */
604  collation = InvalidOid;
605  state = FDW_COLLATE_NONE;
606  }
607  break;
608  case T_NullTest:
609  {
610  NullTest *nt = (NullTest *) node;
611 
612  /*
613  * Recurse to input subexpressions.
614  */
615  if (!foreign_expr_walker((Node *) nt->arg,
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_ArrayExpr:
625  {
626  ArrayExpr *a = (ArrayExpr *) node;
627 
628  /*
629  * Recurse to input subexpressions.
630  */
631  if (!foreign_expr_walker((Node *) a->elements,
632  glob_cxt, &inner_cxt))
633  return false;
634 
635  /*
636  * ArrayExpr must not introduce a collation not derived from
637  * an input foreign Var (same logic as for a function).
638  */
639  collation = a->array_collid;
640  if (collation == InvalidOid)
641  state = FDW_COLLATE_NONE;
642  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
643  collation == inner_cxt.collation)
644  state = FDW_COLLATE_SAFE;
645  else if (collation == DEFAULT_COLLATION_OID)
646  state = FDW_COLLATE_NONE;
647  else
648  state = FDW_COLLATE_UNSAFE;
649  }
650  break;
651  case T_List:
652  {
653  List *l = (List *) node;
654  ListCell *lc;
655 
656  /*
657  * Recurse to component subexpressions.
658  */
659  foreach(lc, l)
660  {
661  if (!foreign_expr_walker((Node *) lfirst(lc),
662  glob_cxt, &inner_cxt))
663  return false;
664  }
665 
666  /*
667  * When processing a list, collation state just bubbles up
668  * from the list elements.
669  */
670  collation = inner_cxt.collation;
671  state = inner_cxt.state;
672 
673  /* Don't apply exprType() to the list. */
674  check_type = false;
675  }
676  break;
677  case T_Aggref:
678  {
679  Aggref *agg = (Aggref *) node;
680  ListCell *lc;
681 
682  /* Not safe to pushdown when not in grouping context */
683  if (!IS_UPPER_REL(glob_cxt->foreignrel))
684  return false;
685 
686  /* Only non-split aggregates are pushable. */
687  if (agg->aggsplit != AGGSPLIT_SIMPLE)
688  return false;
689 
690  /* As usual, it must be shippable. */
691  if (!is_shippable(agg->aggfnoid, ProcedureRelationId, fpinfo))
692  return false;
693 
694  /*
695  * Recurse to input args. aggdirectargs, aggorder and
696  * aggdistinct are all present in args, so no need to check
697  * their shippability explicitly.
698  */
699  foreach(lc, agg->args)
700  {
701  Node *n = (Node *) lfirst(lc);
702 
703  /* If TargetEntry, extract the expression from it */
704  if (IsA(n, TargetEntry))
705  {
706  TargetEntry *tle = (TargetEntry *) n;
707 
708  n = (Node *) tle->expr;
709  }
710 
711  if (!foreign_expr_walker(n, glob_cxt, &inner_cxt))
712  return false;
713  }
714 
715  /*
716  * For aggorder elements, check whether the sort operator, if
717  * specified, is shippable or not.
718  */
719  if (agg->aggorder)
720  {
721  ListCell *lc;
722 
723  foreach(lc, agg->aggorder)
724  {
725  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
726  Oid sortcoltype;
727  TypeCacheEntry *typentry;
728  TargetEntry *tle;
729 
731  agg->args);
732  sortcoltype = exprType((Node *) tle->expr);
733  typentry = lookup_type_cache(sortcoltype,
735  /* Check shippability of non-default sort operator. */
736  if (srt->sortop != typentry->lt_opr &&
737  srt->sortop != typentry->gt_opr &&
738  !is_shippable(srt->sortop, OperatorRelationId,
739  fpinfo))
740  return false;
741  }
742  }
743 
744  /* Check aggregate filter */
745  if (!foreign_expr_walker((Node *) agg->aggfilter,
746  glob_cxt, &inner_cxt))
747  return false;
748 
749  /*
750  * If aggregate's input collation is not derived from a
751  * foreign Var, it can't be sent to remote.
752  */
753  if (agg->inputcollid == InvalidOid)
754  /* OK, inputs are all noncollatable */ ;
755  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
756  agg->inputcollid != inner_cxt.collation)
757  return false;
758 
759  /*
760  * Detect whether node is introducing a collation not derived
761  * from a foreign Var. (If so, we just mark it unsafe for now
762  * rather than immediately returning false, since the parent
763  * node might not care.)
764  */
765  collation = agg->aggcollid;
766  if (collation == InvalidOid)
767  state = FDW_COLLATE_NONE;
768  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
769  collation == inner_cxt.collation)
770  state = FDW_COLLATE_SAFE;
771  else if (collation == DEFAULT_COLLATION_OID)
772  state = FDW_COLLATE_NONE;
773  else
774  state = FDW_COLLATE_UNSAFE;
775  }
776  break;
777  default:
778 
779  /*
780  * If it's anything else, assume it's unsafe. This list can be
781  * expanded later, but don't forget to add deparse support below.
782  */
783  return false;
784  }
785 
786  /*
787  * If result type of given expression is not shippable, it can't be sent
788  * to remote because it might have incompatible semantics on remote side.
789  */
790  if (check_type && !is_shippable(exprType(node), TypeRelationId, fpinfo))
791  return false;
792 
793  /*
794  * Now, merge my collation information into my parent's state.
795  */
796  if (state > outer_cxt->state)
797  {
798  /* Override previous parent state */
799  outer_cxt->collation = collation;
800  outer_cxt->state = state;
801  }
802  else if (state == outer_cxt->state)
803  {
804  /* Merge, or detect error if there's a collation conflict */
805  switch (state)
806  {
807  case FDW_COLLATE_NONE:
808  /* Nothing + nothing is still nothing */
809  break;
810  case FDW_COLLATE_SAFE:
811  if (collation != outer_cxt->collation)
812  {
813  /*
814  * Non-default collation always beats default.
815  */
816  if (outer_cxt->collation == DEFAULT_COLLATION_OID)
817  {
818  /* Override previous parent state */
819  outer_cxt->collation = collation;
820  }
821  else if (collation != DEFAULT_COLLATION_OID)
822  {
823  /*
824  * Conflict; show state as indeterminate. We don't
825  * want to "return false" right away, since parent
826  * node might not care about collation.
827  */
828  outer_cxt->state = FDW_COLLATE_UNSAFE;
829  }
830  }
831  break;
832  case FDW_COLLATE_UNSAFE:
833  /* We're still conflicted ... */
834  break;
835  }
836  }
837 
838  /* It looks OK */
839  return true;
840 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:177
List * args
Definition: primnodes.h:463
Oid funccollid
Definition: primnodes.h:461
FDWCollateState state
Definition: deparse.c:88
Index tleSortGroupRef
Definition: parsenodes.h:1234
Expr * arg
Definition: primnodes.h:800
Oid inputcollid
Definition: primnodes.h:301
Definition: nodes.h:525
List * args
Definition: primnodes.h:305
AttrNumber varattno
Definition: primnodes.h:172
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:167
List * refupperindexpr
Definition: primnodes.h:408
bool is_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
Definition: shippable.c:163
#define OidIsValid(objectId)
Definition: c.h:645
FDWCollateState
Definition: deparse.c:75
Oid funcid
Definition: primnodes.h:455
Oid paramcollid
Definition: primnodes.h:252
Expr * arg
Definition: primnodes.h:1205
Oid constcollid
Definition: primnodes.h:198
Relids relids
Definition: deparse.c:67
char * c
List * aggorder
Definition: primnodes.h:306
RelOptInfo * foreignrel
Definition: deparse.c:66
List * elements
Definition: primnodes.h:977
#define TYPECACHE_GT_OPR
Definition: typcache.h:130
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:506
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:367
Definition: nodes.h:297
Definition: nodes.h:152
Index varno
Definition: primnodes.h:170
Definition: nodes.h:151
Oid resultcollid
Definition: primnodes.h:803
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
#define InvalidOid
Definition: postgres_ext.h:36
Oid aggfnoid
Definition: primnodes.h:298
void * fdw_private
Definition: pathnodes.h:695
Oid inputcollid
Definition: primnodes.h:462
#define lfirst(lc)
Definition: pg_list.h:190
Definition: regguts.h:298
Expr * expr
Definition: primnodes.h:1393
AggSplit aggsplit
Definition: primnodes.h:314
Oid array_collid
Definition: primnodes.h:975
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Expr * aggfilter
Definition: primnodes.h:308
Oid inputcollid
Definition: primnodes.h:507
List * args
Definition: primnodes.h:569
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
#define nodeTag(nodeptr)
Definition: nodes.h:530
Expr * refassgnexpr
Definition: primnodes.h:416
Oid aggcollid
Definition: primnodes.h:300
static int fe(enum e x)
Definition: preproc-init.c:111
List * reflowerindexpr
Definition: primnodes.h:410
#define TYPECACHE_LT_OPR
Definition: typcache.h:129
Oid varcollid
Definition: primnodes.h:176
Expr * refexpr
Definition: primnodes.h:413
Oid opno
Definition: primnodes.h:502
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
List * args
Definition: primnodes.h:508
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Definition: nodes.h:153

◆ get_jointype_name()

const char* get_jointype_name ( JoinType  jointype)

Definition at line 1353 of file deparse.c.

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

Referenced by deparseFromExprForRel(), and foreign_join_ok().

1354 {
1355  switch (jointype)
1356  {
1357  case JOIN_INNER:
1358  return "INNER";
1359 
1360  case JOIN_LEFT:
1361  return "LEFT";
1362 
1363  case JOIN_RIGHT:
1364  return "RIGHT";
1365 
1366  case JOIN_FULL:
1367  return "FULL";
1368 
1369  default:
1370  /* Shouldn't come here, but protect from buggy code. */
1371  elog(ERROR, "unsupported join type %d", jointype);
1372  }
1373 
1374  /* Keep compiler happy */
1375  return NULL;
1376 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228

◆ get_relation_column_alias_ids()

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

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

3392 {
3393  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3394  int i;
3395  ListCell *lc;
3396 
3397  /* Get the relation alias ID */
3398  *relno = fpinfo->relation_index;
3399 
3400  /* Get the column alias ID */
3401  i = 1;
3402  foreach(lc, foreignrel->reltarget->exprs)
3403  {
3404  if (equal(lfirst(lc), (Node *) node))
3405  {
3406  *colno = i;
3407  return;
3408  }
3409  i++;
3410  }
3411 
3412  /* Shouldn't get here */
3413  elog(ERROR, "unexpected expression in subquery output");
3414 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3011
Definition: nodes.h:525
#define ERROR
Definition: elog.h:43
List * exprs
Definition: pathnodes.h:1044
void * fdw_private
Definition: pathnodes.h:695
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:228
int i
struct PathTarget * reltarget
Definition: pathnodes.h:652

◆ 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:525
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:641
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:695
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:645

◆ is_foreign_param()

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

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

858 {
859  if (expr == NULL)
860  return false;
861 
862  switch (nodeTag(expr))
863  {
864  case T_Var:
865  {
866  /* It would have to be sent unless it's a foreign Var */
867  Var *var = (Var *) expr;
868  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
869  Relids relids;
870 
871  if (IS_UPPER_REL(baserel))
872  relids = fpinfo->outerrel->relids;
873  else
874  relids = baserel->relids;
875 
876  if (bms_is_member(var->varno, relids) && var->varlevelsup == 0)
877  return false; /* foreign Var, so not a param */
878  else
879  return true; /* it'd have to be a param */
880  break;
881  }
882  case T_Param:
883  /* Params always have to be sent to the foreign server */
884  return true;
885  default:
886  break;
887  }
888  return false;
889 }
Index varlevelsup
Definition: primnodes.h:177
Definition: primnodes.h:167
RelOptInfo * outerrel
Definition: postgres_fdw.h:100
Relids relids
Definition: pathnodes.h:641
Index varno
Definition: primnodes.h:170
Definition: nodes.h:151
void * fdw_private
Definition: pathnodes.h:695
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:624
#define nodeTag(nodeptr)
Definition: nodes.h:530
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Definition: nodes.h:153

◆ is_subquery_var()

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

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

3329 {
3330  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3331  RelOptInfo *outerrel = fpinfo->outerrel;
3332  RelOptInfo *innerrel = fpinfo->innerrel;
3333 
3334  /* Should only be called in these cases. */
3335  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
3336 
3337  /*
3338  * If the given relation isn't a join relation, it doesn't have any lower
3339  * subqueries, so the Var isn't a subquery output column.
3340  */
3341  if (!IS_JOIN_REL(foreignrel))
3342  return false;
3343 
3344  /*
3345  * If the Var doesn't belong to any lower subqueries, it isn't a subquery
3346  * output column.
3347  */
3348  if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels))
3349  return false;
3350 
3351  if (bms_is_member(node->varno, outerrel->relids))
3352  {
3353  /*
3354  * If outer relation is deparsed as a subquery, the Var is an output
3355  * column of the subquery; get the IDs for the relation/column alias.
3356  */
3357  if (fpinfo->make_outerrel_subquery)
3358  {
3359  get_relation_column_alias_ids(node, outerrel, relno, colno);
3360  return true;
3361  }
3362 
3363  /* Otherwise, recurse into the outer relation. */
3364  return is_subquery_var(node, outerrel, relno, colno);
3365  }
3366  else
3367  {
3368  Assert(bms_is_member(node->varno, innerrel->relids));
3369 
3370  /*
3371  * If inner relation is deparsed as a subquery, the Var is an output
3372  * column of the subquery; get the IDs for the relation/column alias.
3373  */
3374  if (fpinfo->make_innerrel_subquery)
3375  {
3376  get_relation_column_alias_ids(node, innerrel, relno, colno);
3377  return true;
3378  }
3379 
3380  /* Otherwise, recurse into the inner relation. */
3381  return is_subquery_var(node, innerrel, relno, colno);
3382  }
3383 }
Relids lower_subquery_rels
Definition: postgres_fdw.h:117
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:619
RelOptInfo * outerrel
Definition: postgres_fdw.h:100
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
Index varno
Definition: primnodes.h:170
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3328
void * fdw_private
Definition: pathnodes.h:695
static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3390
#define Assert(condition)
Definition: c.h:739
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 3090 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

3092 {
3093  StringInfo buf = context->buf;
3094  char *ptypename = deparse_type_name(paramtype, paramtypmod);
3095 
3096  appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
3097 }
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:903

◆ printRemotePlaceholder()

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

Definition at line 3116 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

3118 {
3119  StringInfo buf = context->buf;
3120  char *ptypename = deparse_type_name(paramtype, paramtypmod);
3121 
3122  appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename);
3123 }
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:903