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

Go to the source code of this file.

Data Structures

struct  foreign_glob_cxt
 
struct  foreign_loc_cxt
 
struct  deparse_expr_cxt
 

Macros

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

Typedefs

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

Enumerations

enum  FDWCollateState { FDW_COLLATE_NONE, FDW_COLLATE_SAFE, FDW_COLLATE_UNSAFE }
 

Functions

static bool foreign_expr_walker (Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt, foreign_loc_cxt *case_arg_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 deparseCaseExpr (CaseExpr *node, deparse_expr_cxt *context)
 
static void deparseArrayExpr (ArrayExpr *node, deparse_expr_cxt *context)
 
static void printRemoteParam (int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
 
static void printRemotePlaceholder (Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
 
static void deparseSelectSql (List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
 
static void deparseLockingClause (deparse_expr_cxt *context)
 
static void appendOrderByClause (List *pathkeys, bool has_final_sort, deparse_expr_cxt *context)
 
static void appendLimitClause (deparse_expr_cxt *context)
 
static void appendConditions (List *exprs, deparse_expr_cxt *context)
 
static void deparseFromExprForRel (StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
 
static void deparseFromExpr (List *quals, deparse_expr_cxt *context)
 
static void deparseRangeTblRef (StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, Index ignore_rel, List **ignore_conds, List **params_list)
 
static void deparseAggref (Aggref *node, deparse_expr_cxt *context)
 
static void appendGroupByClause (List *tlist, deparse_expr_cxt *context)
 
static void appendAggOrderBy (List *orderList, List *targetList, deparse_expr_cxt *context)
 
static void appendFunctionName (Oid funcid, deparse_expr_cxt *context)
 
static NodedeparseSortGroupClause (Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
 
static bool is_subquery_var (Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
 
static void get_relation_column_alias_ids (Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
 
void classifyConditions (PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds)
 
bool is_foreign_expr (PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
 
bool is_foreign_param (PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
 
Listbuild_tlist_to_deparse (RelOptInfo *foreignrel)
 
void deparseSelectStmtForRel (StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, bool has_final_sort, bool has_limit, bool is_subquery, List **retrieved_attrs, List **params_list)
 
const char * get_jointype_name (JoinType jointype)
 
void deparseInsertSql (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, bool doNothing, List *withCheckOptionList, List *returningList, List **retrieved_attrs, int *values_end_len)
 
void rebuildInsertSql (StringInfo buf, Relation rel, char *orig_query, List *target_attrs, int values_end_len, int num_params, int num_rows)
 
void deparseUpdateSql (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
 
void deparseDirectUpdateSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *targetlist, List *targetAttrs, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
 
void deparseDeleteSql (StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs)
 
void deparseDirectDeleteSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
 
void deparseAnalyzeSizeSql (StringInfo buf, Relation rel)
 
void deparseAnalyzeSql (StringInfo buf, Relation rel, List **retrieved_attrs)
 
void deparseTruncateSql (StringInfo buf, List *rels, DropBehavior behavior, bool restart_seqs)
 
void deparseStringLiteral (StringInfo buf, const char *val)
 

Macro Definition Documentation

◆ ADD_REL_QUALIFIER

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

Definition at line 108 of file deparse.c.

Referenced by deparseColumnRef(), and deparseTargetList().

◆ REL_ALIAS_PREFIX

#define REL_ALIAS_PREFIX   "r"

◆ SUBQUERY_COL_ALIAS_PREFIX

#define SUBQUERY_COL_ALIAS_PREFIX   "c"

Definition at line 111 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

◆ SUBQUERY_REL_ALIAS_PREFIX

#define SUBQUERY_REL_ALIAS_PREFIX   "s"

Definition at line 110 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

Typedef Documentation

◆ deparse_expr_cxt

◆ foreign_glob_cxt

◆ foreign_loc_cxt

Enumeration Type Documentation

◆ FDWCollateState

Enumerator
FDW_COLLATE_NONE 
FDW_COLLATE_SAFE 
FDW_COLLATE_UNSAFE 

Definition at line 76 of file deparse.c.

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

Function Documentation

◆ appendAggOrderBy()

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

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

3339 {
3340  StringInfo buf = context->buf;
3341  ListCell *lc;
3342  bool first = true;
3343 
3344  foreach(lc, orderList)
3345  {
3346  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
3347  Node *sortexpr;
3348  Oid sortcoltype;
3349  TypeCacheEntry *typentry;
3350 
3351  if (!first)
3352  appendStringInfoString(buf, ", ");
3353  first = false;
3354 
3355  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
3356  false, context);
3357  sortcoltype = exprType(sortexpr);
3358  /* See whether operator is default < or > for datatype */
3359  typentry = lookup_type_cache(sortcoltype,
3361  if (srt->sortop == typentry->lt_opr)
3362  appendStringInfoString(buf, " ASC");
3363  else if (srt->sortop == typentry->gt_opr)
3364  appendStringInfoString(buf, " DESC");
3365  else
3366  {
3367  HeapTuple opertup;
3368  Form_pg_operator operform;
3369 
3370  appendStringInfoString(buf, " USING ");
3371 
3372  /* Append operator name. */
3373  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
3374  if (!HeapTupleIsValid(opertup))
3375  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
3376  operform = (Form_pg_operator) GETSTRUCT(opertup);
3377  deparseOperatorName(buf, operform);
3378  ReleaseSysCache(opertup);
3379  }
3380 
3381  if (srt->nulls_first)
3382  appendStringInfoString(buf, " NULLS FIRST");
3383  else
3384  appendStringInfoString(buf, " NULLS LAST");
3385  }
3386 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3590
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Index tleSortGroupRef
Definition: parsenodes.h:1295
Definition: nodes.h:536
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2995
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define TYPECACHE_GT_OPR
Definition: typcache.h:138
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
#define elog(elevel,...)
Definition: elog.h:232
#define TYPECACHE_LT_OPR
Definition: typcache.h:137

◆ appendConditions()

static void appendConditions ( List exprs,
deparse_expr_cxt context 
)
static

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

1469 {
1470  int nestlevel;
1471  ListCell *lc;
1472  bool is_first = true;
1473  StringInfo buf = context->buf;
1474 
1475  /* Make sure any constants in the exprs are printed portably */
1476  nestlevel = set_transmission_modes();
1477 
1478  foreach(lc, exprs)
1479  {
1480  Expr *expr = (Expr *) lfirst(lc);
1481 
1482  /* Extract clause from RestrictInfo, if required */
1483  if (IsA(expr, RestrictInfo))
1484  expr = ((RestrictInfo *) expr)->clause;
1485 
1486  /* Connect expressions with "AND" and parenthesize each condition. */
1487  if (!is_first)
1488  appendStringInfoString(buf, " AND ");
1489 
1490  appendStringInfoChar(buf, '(');
1491  deparseExpr(expr, context);
1492  appendStringInfoChar(buf, ')');
1493 
1494  is_first = false;
1495  }
1496 
1497  reset_transmission_modes(nestlevel);
1498 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572

◆ appendFunctionName()

static void appendFunctionName ( Oid  funcid,
deparse_expr_cxt context 
)
static

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

3556 {
3557  StringInfo buf = context->buf;
3558  HeapTuple proctup;
3559  Form_pg_proc procform;
3560  const char *proname;
3561 
3562  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3563  if (!HeapTupleIsValid(proctup))
3564  elog(ERROR, "cache lookup failed for function %u", funcid);
3565  procform = (Form_pg_proc) GETSTRUCT(proctup);
3566 
3567  /* Print schema name only if it's not pg_catalog */
3568  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3569  {
3570  const char *schemaname;
3571 
3572  schemaname = get_namespace_name(procform->pronamespace);
3573  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3574  }
3575 
3576  /* Always print the function name */
3577  proname = NameStr(procform->proname);
3579 
3580  ReleaseSysCache(proctup);
3581 }
NameData proname
Definition: pg_proc.h:35
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
static char * buf
Definition: pg_test_fsync.c:68
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681

◆ appendGroupByClause()

static void appendGroupByClause ( List tlist,
deparse_expr_cxt context 
)
static

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

3437 {
3438  StringInfo buf = context->buf;
3439  Query *query = context->root->parse;
3440  ListCell *lc;
3441  bool first = true;
3442 
3443  /* Nothing to be done, if there's no GROUP BY clause in the query. */
3444  if (!query->groupClause)
3445  return;
3446 
3447  appendStringInfoString(buf, " GROUP BY ");
3448 
3449  /*
3450  * Queries with grouping sets are not pushed down, so we don't expect
3451  * grouping sets here.
3452  */
3453  Assert(!query->groupingSets);
3454 
3455  foreach(lc, query->groupClause)
3456  {
3457  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
3458 
3459  if (!first)
3460  appendStringInfoString(buf, ", ");
3461  first = false;
3462 
3463  deparseSortGroupClause(grp->tleSortGroupRef, tlist, true, context);
3464  }
3465 }
static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context)
Definition: deparse.c:3590
Query * parse
Definition: pathnodes.h:162
Index tleSortGroupRef
Definition: parsenodes.h:1295
List * groupingSets
Definition: parsenodes.h:161
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * groupClause
Definition: parsenodes.h:158

◆ appendLimitClause()

static void appendLimitClause ( deparse_expr_cxt context)
static

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

3528 {
3529  PlannerInfo *root = context->root;
3530  StringInfo buf = context->buf;
3531  int nestlevel;
3532 
3533  /* Make sure any constants in the exprs are printed portably */
3534  nestlevel = set_transmission_modes();
3535 
3536  if (root->parse->limitCount)
3537  {
3538  appendStringInfoString(buf, " LIMIT ");
3539  deparseExpr((Expr *) root->parse->limitCount, context);
3540  }
3541  if (root->parse->limitOffset)
3542  {
3543  appendStringInfoString(buf, " OFFSET ");
3544  deparseExpr((Expr *) root->parse->limitOffset, context);
3545  }
3546 
3547  reset_transmission_modes(nestlevel);
3548 }
Node * limitOffset
Definition: parsenodes.h:171
Query * parse
Definition: pathnodes.h:162
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Node * limitCount
Definition: parsenodes.h:172
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
PlannerInfo * root
Definition: deparse.c:97
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572

◆ appendOrderByClause()

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

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

3475 {
3476  ListCell *lcell;
3477  int nestlevel;
3478  char *delim = " ";
3479  RelOptInfo *baserel = context->scanrel;
3480  StringInfo buf = context->buf;
3481 
3482  /* Make sure any constants in the exprs are printed portably */
3483  nestlevel = set_transmission_modes();
3484 
3485  appendStringInfoString(buf, " ORDER BY");
3486  foreach(lcell, pathkeys)
3487  {
3488  PathKey *pathkey = lfirst(lcell);
3489  Expr *em_expr;
3490 
3491  if (has_final_sort)
3492  {
3493  /*
3494  * By construction, context->foreignrel is the input relation to
3495  * the final sort.
3496  */
3497  em_expr = find_em_expr_for_input_target(context->root,
3498  pathkey->pk_eclass,
3499  context->foreignrel->reltarget);
3500  }
3501  else
3502  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
3503 
3504  Assert(em_expr != NULL);
3505 
3506  appendStringInfoString(buf, delim);
3507  deparseExpr(em_expr, context);
3508  if (pathkey->pk_strategy == BTLessStrategyNumber)
3509  appendStringInfoString(buf, " ASC");
3510  else
3511  appendStringInfoString(buf, " DESC");
3512 
3513  if (pathkey->pk_nulls_first)
3514  appendStringInfoString(buf, " NULLS FIRST");
3515  else
3516  appendStringInfoString(buf, " NULLS LAST");
3517 
3518  delim = ", ";
3519  }
3520  reset_transmission_modes(nestlevel);
3521 }
int set_transmission_modes(void)
int pk_strategy
Definition: pathnodes.h:1068
Expr * find_em_expr_for_input_target(PlannerInfo *root, EquivalenceClass *ec, PathTarget *target)
bool pk_nulls_first
Definition: pathnodes.h:1069
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
Definition: equivclass.c:939
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1066
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
RelOptInfo * scanrel
Definition: deparse.c:99
#define BTLessStrategyNumber
Definition: stratnum.h:29
RelOptInfo * foreignrel
Definition: deparse.c:98
struct PathTarget * reltarget
Definition: pathnodes.h:692

◆ build_tlist_to_deparse()

List* build_tlist_to_deparse ( RelOptInfo foreignrel)

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

1072 {
1073  List *tlist = NIL;
1074  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1075  ListCell *lc;
1076 
1077  /*
1078  * For an upper relation, we have already built the target list while
1079  * checking shippability, so just return that.
1080  */
1081  if (IS_UPPER_REL(foreignrel))
1082  return fpinfo->grouped_tlist;
1083 
1084  /*
1085  * We require columns specified in foreignrel->reltarget->exprs and those
1086  * required for evaluating the local conditions.
1087  */
1088  tlist = add_to_flat_tlist(tlist,
1089  pull_var_clause((Node *) foreignrel->reltarget->exprs,
1091  foreach(lc, fpinfo->local_conds)
1092  {
1093  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1094 
1095  tlist = add_to_flat_tlist(tlist,
1096  pull_var_clause((Node *) rinfo->clause,
1098  }
1099 
1100  return tlist;
1101 }
#define NIL
Definition: pg_list.h:65
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:192
Definition: nodes.h:536
List * pull_var_clause(Node *node, int flags)
Definition: var.c:597
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Expr * clause
Definition: pathnodes.h:2056
List * exprs
Definition: pathnodes.h:1110
void * fdw_private
Definition: pathnodes.h:737
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:121
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:664
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:692

◆ classifyConditions()

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

Definition at line 207 of file deparse.c.

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

Referenced by estimate_path_cost_size(), and postgresGetForeignRelSize().

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

◆ deparse_type_name()

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

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

1053 {
1055 
1056  if (!is_builtin(type_oid))
1057  flags |= FORMAT_TYPE_FORCE_QUALIFY;
1058 
1059  return format_type_extended(type_oid, typemod, flags);
1060 }
bool is_builtin(Oid objectId)
Definition: shippable.c:152
uint16 bits16
Definition: c.h:449
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:112
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:114
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:112

◆ deparseAggref()

static void deparseAggref ( Aggref node,
deparse_expr_cxt context 
)
static

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

3247 {
3248  StringInfo buf = context->buf;
3249  bool use_variadic;
3250 
3251  /* Only basic, non-split aggregation accepted. */
3252  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
3253 
3254  /* Check if need to print VARIADIC (cf. ruleutils.c) */
3255  use_variadic = node->aggvariadic;
3256 
3257  /* Find aggregate name from aggfnoid which is a pg_proc entry */
3258  appendFunctionName(node->aggfnoid, context);
3259  appendStringInfoChar(buf, '(');
3260 
3261  /* Add DISTINCT */
3262  appendStringInfoString(buf, (node->aggdistinct != NIL) ? "DISTINCT " : "");
3263 
3264  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
3265  {
3266  /* Add WITHIN GROUP (ORDER BY ..) */
3267  ListCell *arg;
3268  bool first = true;
3269 
3270  Assert(!node->aggvariadic);
3271  Assert(node->aggorder != NIL);
3272 
3273  foreach(arg, node->aggdirectargs)
3274  {
3275  if (!first)
3276  appendStringInfoString(buf, ", ");
3277  first = false;
3278 
3279  deparseExpr((Expr *) lfirst(arg), context);
3280  }
3281 
3282  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
3283  appendAggOrderBy(node->aggorder, node->args, context);
3284  }
3285  else
3286  {
3287  /* aggstar can be set only in zero-argument aggregates */
3288  if (node->aggstar)
3289  appendStringInfoChar(buf, '*');
3290  else
3291  {
3292  ListCell *arg;
3293  bool first = true;
3294 
3295  /* Add all the arguments */
3296  foreach(arg, node->args)
3297  {
3298  TargetEntry *tle = (TargetEntry *) lfirst(arg);
3299  Node *n = (Node *) tle->expr;
3300 
3301  if (tle->resjunk)
3302  continue;
3303 
3304  if (!first)
3305  appendStringInfoString(buf, ", ");
3306  first = false;
3307 
3308  /* Add VARIADIC */
3309  if (use_variadic && lnext(node->args, arg) == NULL)
3310  appendStringInfoString(buf, "VARIADIC ");
3311 
3312  deparseExpr((Expr *) n, context);
3313  }
3314  }
3315 
3316  /* Add ORDER BY */
3317  if (node->aggorder != NIL)
3318  {
3319  appendStringInfoString(buf, " ORDER BY ");
3320  appendAggOrderBy(node->aggorder, node->args, context);
3321  }
3322  }
3323 
3324  /* Add FILTER (WHERE ..) */
3325  if (node->aggfilter != NULL)
3326  {
3327  appendStringInfoString(buf, ") FILTER (WHERE ");
3328  deparseExpr((Expr *) node->aggfilter, context);
3329  }
3330 
3331  appendStringInfoChar(buf, ')');
3332 }
List * aggdistinct
Definition: primnodes.h:332
#define NIL
Definition: pg_list.h:65
bool aggvariadic
Definition: primnodes.h:335
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3555
Definition: nodes.h:536
List * args
Definition: primnodes.h:330
bool aggstar
Definition: primnodes.h:334
bool resjunk
Definition: primnodes.h:1462
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * aggorder
Definition: primnodes.h:331
List * aggdirectargs
Definition: primnodes.h:329
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
Definition: deparse.c:3338
StringInfo buf
Definition: deparse.c:102
Oid aggfnoid
Definition: primnodes.h:323
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1455
AggSplit aggsplit
Definition: primnodes.h:339
Expr * aggfilter
Definition: primnodes.h:333
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
void * arg
char aggkind
Definition: primnodes.h:337

◆ deparseAnalyzeSizeSql()

void deparseAnalyzeSizeSql ( StringInfo  buf,
Relation  rel 
)

Definition at line 2256 of file deparse.c.

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

Referenced by postgresAnalyzeForeignTable().

2257 {
2259 
2260  /* We'll need the remote relation name as a literal. */
2261  initStringInfo(&relname);
2262  deparseRelation(&relname, rel);
2263 
2264  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
2265  deparseStringLiteral(buf, relname.data);
2266  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
2267 }
NameData relname
Definition: pg_class.h:38
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2537
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497

◆ deparseAnalyzeSql()

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

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

2277 {
2278  Oid relid = RelationGetRelid(rel);
2279  TupleDesc tupdesc = RelationGetDescr(rel);
2280  int i;
2281  char *colname;
2282  List *options;
2283  ListCell *lc;
2284  bool first = true;
2285 
2286  *retrieved_attrs = NIL;
2287 
2288  appendStringInfoString(buf, "SELECT ");
2289  for (i = 0; i < tupdesc->natts; i++)
2290  {
2291  /* Ignore dropped columns. */
2292  if (TupleDescAttr(tupdesc, i)->attisdropped)
2293  continue;
2294 
2295  if (!first)
2296  appendStringInfoString(buf, ", ");
2297  first = false;
2298 
2299  /* Use attribute name or column_name option. */
2300  colname = NameStr(TupleDescAttr(tupdesc, i)->attname);
2301  options = GetForeignColumnOptions(relid, i + 1);
2302 
2303  foreach(lc, options)
2304  {
2305  DefElem *def = (DefElem *) lfirst(lc);
2306 
2307  if (strcmp(def->defname, "column_name") == 0)
2308  {
2309  colname = defGetString(def);
2310  break;
2311  }
2312  }
2313 
2315 
2316  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
2317  }
2318 
2319  /* Don't generate bad syntax for zero-column relation. */
2320  if (first)
2321  appendStringInfoString(buf, "NULL");
2322 
2323  /*
2324  * Construct FROM clause
2325  */
2326  appendStringInfoString(buf, " FROM ");
2327  deparseRelation(buf, rel);
2328 }
#define NIL
Definition: pg_list.h:65
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
#define RelationGetDescr(relation)
Definition: rel.h:503
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
unsigned int Oid
Definition: postgres_ext.h:31
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:286
NameData attname
Definition: pg_attribute.h:41
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
List * lappend_int(List *list, int datum)
Definition: list.c:354
static char ** options
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
#define lfirst(lc)
Definition: pg_list.h:169
int i
#define NameStr(name)
Definition: c.h:681
char * defname
Definition: parsenodes.h:758
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ deparseArrayExpr()

static void deparseArrayExpr ( ArrayExpr node,
deparse_expr_cxt context 
)
static

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

3221 {
3222  StringInfo buf = context->buf;
3223  bool first = true;
3224  ListCell *lc;
3225 
3226  appendStringInfoString(buf, "ARRAY[");
3227  foreach(lc, node->elements)
3228  {
3229  if (!first)
3230  appendStringInfoString(buf, ", ");
3231  deparseExpr(lfirst(lc), context);
3232  first = false;
3233  }
3234  appendStringInfoChar(buf, ']');
3235 
3236  /* If the array is empty, we need an explicit cast to the array type. */
3237  if (node->elements == NIL)
3238  appendStringInfo(buf, "::%s",
3239  deparse_type_name(node->array_typeid, -1));
3240 }
#define NIL
Definition: pg_list.h:65
Oid array_typeid
Definition: primnodes.h:1034
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * elements
Definition: primnodes.h:1037
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:1052
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572

◆ deparseBoolExpr()

static void deparseBoolExpr ( BoolExpr node,
deparse_expr_cxt context 
)
static

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

3100 {
3101  StringInfo buf = context->buf;
3102  const char *op = NULL; /* keep compiler quiet */
3103  bool first;
3104  ListCell *lc;
3105 
3106  switch (node->boolop)
3107  {
3108  case AND_EXPR:
3109  op = "AND";
3110  break;
3111  case OR_EXPR:
3112  op = "OR";
3113  break;
3114  case NOT_EXPR:
3115  appendStringInfoString(buf, "(NOT ");
3116  deparseExpr(linitial(node->args), context);
3117  appendStringInfoChar(buf, ')');
3118  return;
3119  }
3120 
3121  appendStringInfoChar(buf, '(');
3122  first = true;
3123  foreach(lc, node->args)
3124  {
3125  if (!first)
3126  appendStringInfo(buf, " %s ", op);
3127  deparseExpr((Expr *) lfirst(lc), context);
3128  first = false;
3129  }
3130  appendStringInfoChar(buf, ')');
3131 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
BoolExprType boolop
Definition: primnodes.h:625
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:626
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572

◆ deparseCaseExpr()

static void deparseCaseExpr ( CaseExpr node,
deparse_expr_cxt context 
)
static

Definition at line 3170 of file deparse.c.

References appendStringInfoChar(), appendStringInfoString(), CaseExpr::arg, CaseExpr::args, buf, deparse_expr_cxt::buf, castNode, CaseExpr::defresult, deparseExpr(), CaseWhen::expr, lfirst, lsecond, and CaseWhen::result.

Referenced by deparseExpr().

3171 {
3172  StringInfo buf = context->buf;
3173  ListCell *lc;
3174 
3175  appendStringInfoString(buf, "(CASE");
3176 
3177  /* If this is a CASE arg WHEN then emit the arg expression */
3178  if (node->arg != NULL)
3179  {
3180  appendStringInfoChar(buf, ' ');
3181  deparseExpr(node->arg, context);
3182  }
3183 
3184  /* Add each condition/result of the CASE clause */
3185  foreach(lc, node->args)
3186  {
3187  CaseWhen *whenclause = (CaseWhen *) lfirst(lc);
3188 
3189  /* WHEN */
3190  appendStringInfoString(buf, " WHEN ");
3191  if (node->arg == NULL) /* CASE WHEN */
3192  deparseExpr(whenclause->expr, context);
3193  else /* CASE arg WHEN */
3194  {
3195  /* Ignore the CaseTestExpr and equality operator. */
3196  deparseExpr(lsecond(castNode(OpExpr, whenclause->expr)->args),
3197  context);
3198  }
3199 
3200  /* THEN */
3201  appendStringInfoString(buf, " THEN ");
3202  deparseExpr(whenclause->result, context);
3203  }
3204 
3205  /* add ELSE if present */
3206  if (node->defresult != NULL)
3207  {
3208  appendStringInfoString(buf, " ELSE ");
3209  deparseExpr(node->defresult, context);
3210  }
3211 
3212  /* append END */
3213  appendStringInfoString(buf, " END)");
3214 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
#define lsecond(l)
Definition: pg_list.h:179
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
List * args
Definition: primnodes.h:979
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
Expr * arg
Definition: primnodes.h:978
Expr * result
Definition: primnodes.h:991
Expr * defresult
Definition: primnodes.h:980
Expr * expr
Definition: primnodes.h:990

◆ deparseColumnRef()

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

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

2371 {
2372  /* We support fetching the remote side's CTID and OID. */
2373  if (varattno == SelfItemPointerAttributeNumber)
2374  {
2375  if (qualify_col)
2376  ADD_REL_QUALIFIER(buf, varno);
2377  appendStringInfoString(buf, "ctid");
2378  }
2379  else if (varattno < 0)
2380  {
2381  /*
2382  * All other system attributes are fetched as 0, except for table OID,
2383  * which is fetched as the local table OID. However, we must be
2384  * careful; the table could be beneath an outer join, in which case it
2385  * must go to NULL whenever the rest of the row does.
2386  */
2387  Oid fetchval = 0;
2388 
2389  if (varattno == TableOidAttributeNumber)
2390  fetchval = rte->relid;
2391 
2392  if (qualify_col)
2393  {
2394  appendStringInfoString(buf, "CASE WHEN (");
2395  ADD_REL_QUALIFIER(buf, varno);
2396  appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval);
2397  }
2398  else
2399  appendStringInfo(buf, "%u", fetchval);
2400  }
2401  else if (varattno == 0)
2402  {
2403  /* Whole row reference */
2404  Relation rel;
2405  Bitmapset *attrs_used;
2406 
2407  /* Required only to be passed down to deparseTargetList(). */
2408  List *retrieved_attrs;
2409 
2410  /*
2411  * The lock on the relation will be held by upper callers, so it's
2412  * fine to open it with no lock here.
2413  */
2414  rel = table_open(rte->relid, NoLock);
2415 
2416  /*
2417  * The local name of the foreign table can not be recognized by the
2418  * foreign server and the table it references on foreign server might
2419  * have different column ordering or different columns than those
2420  * declared locally. Hence we have to deparse whole-row reference as
2421  * ROW(columns referenced locally). Construct this by deparsing a
2422  * "whole row" attribute.
2423  */
2424  attrs_used = bms_add_member(NULL,
2426 
2427  /*
2428  * In case the whole-row reference is under an outer join then it has
2429  * to go NULL whenever the rest of the row goes NULL. Deparsing a join
2430  * query would always involve multiple relations, thus qualify_col
2431  * would be true.
2432  */
2433  if (qualify_col)
2434  {
2435  appendStringInfoString(buf, "CASE WHEN (");
2436  ADD_REL_QUALIFIER(buf, varno);
2437  appendStringInfoString(buf, "*)::text IS NOT NULL THEN ");
2438  }
2439 
2440  appendStringInfoString(buf, "ROW(");
2441  deparseTargetList(buf, rte, varno, rel, false, attrs_used, qualify_col,
2442  &retrieved_attrs);
2443  appendStringInfoChar(buf, ')');
2444 
2445  /* Complete the CASE WHEN statement started above. */
2446  if (qualify_col)
2447  appendStringInfoString(buf, " END");
2448 
2449  table_close(rel, NoLock);
2450  bms_free(attrs_used);
2451  }
2452  else
2453  {
2454  char *colname = NULL;
2455  List *options;
2456  ListCell *lc;
2457 
2458  /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
2459  Assert(!IS_SPECIAL_VARNO(varno));
2460 
2461  /*
2462  * If it's a column of a foreign table, and it has the column_name FDW
2463  * option, use that value.
2464  */
2465  options = GetForeignColumnOptions(rte->relid, varattno);
2466  foreach(lc, options)
2467  {
2468  DefElem *def = (DefElem *) lfirst(lc);
2469 
2470  if (strcmp(def->defname, "column_name") == 0)
2471  {
2472  colname = defGetString(def);
2473  break;
2474  }
2475  }
2476 
2477  /*
2478  * If it's a column of a regular table or it doesn't have column_name
2479  * FDW option, use attribute name.
2480  */
2481  if (colname == NULL)
2482  colname = get_attname(rte->relid, varattno, false);
2483 
2484  if (qualify_col)
2485  ADD_REL_QUALIFIER(buf, varno);
2486 
2488  }
2489 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
#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:1302
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:286
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:180
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:108
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
#define TableOidAttributeNumber
Definition: sysattr.h:26
static char ** options
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
char * defname
Definition: parsenodes.h:758
#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 2703 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().

2704 {
2705  StringInfo buf = context->buf;
2706  Oid typoutput;
2707  bool typIsVarlena;
2708  char *extval;
2709  bool isfloat = false;
2710  bool needlabel;
2711 
2712  if (node->constisnull)
2713  {
2714  appendStringInfoString(buf, "NULL");
2715  if (showtype >= 0)
2716  appendStringInfo(buf, "::%s",
2718  node->consttypmod));
2719  return;
2720  }
2721 
2723  &typoutput, &typIsVarlena);
2724  extval = OidOutputFunctionCall(typoutput, node->constvalue);
2725 
2726  switch (node->consttype)
2727  {
2728  case INT2OID:
2729  case INT4OID:
2730  case INT8OID:
2731  case OIDOID:
2732  case FLOAT4OID:
2733  case FLOAT8OID:
2734  case NUMERICOID:
2735  {
2736  /*
2737  * No need to quote unless it's a special value such as 'NaN'.
2738  * See comments in get_const_expr().
2739  */
2740  if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2741  {
2742  if (extval[0] == '+' || extval[0] == '-')
2743  appendStringInfo(buf, "(%s)", extval);
2744  else
2745  appendStringInfoString(buf, extval);
2746  if (strcspn(extval, "eE.") != strlen(extval))
2747  isfloat = true; /* it looks like a float */
2748  }
2749  else
2750  appendStringInfo(buf, "'%s'", extval);
2751  }
2752  break;
2753  case BITOID:
2754  case VARBITOID:
2755  appendStringInfo(buf, "B'%s'", extval);
2756  break;
2757  case BOOLOID:
2758  if (strcmp(extval, "t") == 0)
2759  appendStringInfoString(buf, "true");
2760  else
2761  appendStringInfoString(buf, "false");
2762  break;
2763  default:
2764  deparseStringLiteral(buf, extval);
2765  break;
2766  }
2767 
2768  pfree(extval);
2769 
2770  if (showtype < 0)
2771  return;
2772 
2773  /*
2774  * For showtype == 0, append ::typename unless the constant will be
2775  * implicitly typed as the right type when it is read in.
2776  *
2777  * XXX this code has to be kept in sync with the behavior of the parser,
2778  * especially make_const.
2779  */
2780  switch (node->consttype)
2781  {
2782  case BOOLOID:
2783  case INT4OID:
2784  case UNKNOWNOID:
2785  needlabel = false;
2786  break;
2787  case NUMERICOID:
2788  needlabel = !isfloat || (node->consttypmod >= 0);
2789  break;
2790  default:
2791  needlabel = true;
2792  break;
2793  }
2794  if (needlabel || showtype > 0)
2795  appendStringInfo(buf, "::%s",
2797  node->consttypmod));
2798 }
Datum constvalue
Definition: primnodes.h:219
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
unsigned int Oid
Definition: postgres_ext.h:31
Oid consttype
Definition: primnodes.h:215
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
StringInfo buf
Definition: deparse.c:102
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2537
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:1052
int32 consttypmod
Definition: primnodes.h:216
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1653
bool constisnull
Definition: primnodes.h:220

◆ deparseDeleteSql()

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

Definition at line 2119 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

2123 {
2124  appendStringInfoString(buf, "DELETE FROM ");
2125  deparseRelation(buf, rel);
2126  appendStringInfoString(buf, " WHERE ctid = $1");
2127 
2128  deparseReturningList(buf, rte, rtindex, rel,
2129  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
2130  NIL, returningList, retrieved_attrs);
2131 }
#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:2199
TriggerDesc * trigdesc
Definition: rel.h:115
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool trig_delete_after_row
Definition: reltrigger.h:67
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497

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

2155 {
2156  deparse_expr_cxt context;
2157 
2158  /* Set up context struct for recursion */
2159  context.root = root;
2160  context.foreignrel = foreignrel;
2161  context.scanrel = foreignrel;
2162  context.buf = buf;
2163  context.params_list = params_list;
2164 
2165  appendStringInfoString(buf, "DELETE FROM ");
2166  deparseRelation(buf, rel);
2167  if (foreignrel->reloptkind == RELOPT_JOINREL)
2168  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
2169 
2170  if (foreignrel->reloptkind == RELOPT_JOINREL)
2171  {
2172  List *ignore_conds = NIL;
2173 
2174  appendStringInfoString(buf, " USING ");
2175  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
2176  &ignore_conds, params_list);
2177  remote_conds = list_concat(remote_conds, ignore_conds);
2178  }
2179 
2180  if (remote_conds)
2181  {
2182  appendStringInfoString(buf, " WHERE ");
2183  appendConditions(remote_conds, &context);
2184  }
2185 
2186  if (foreignrel->reloptkind == RELOPT_JOINREL)
2187  deparseExplicitTargetList(returningList, true, retrieved_attrs,
2188  &context);
2189  else
2190  deparseReturningList(buf, planner_rt_fetch(rtindex, root),
2191  rtindex, rel, false,
2192  NIL, returningList, retrieved_attrs);
2193 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:678
List ** params_list
Definition: deparse.c:103
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2199
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1539
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1468
StringInfo buf
Definition: deparse.c:102
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
PlannerInfo * root
Definition: deparse.c:97
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1616
RelOptInfo * scanrel
Definition: deparse.c:99
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseDirectUpdateSql()

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

Definition at line 2034 of file deparse.c.

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

Referenced by postgresPlanDirectModify().

2043 {
2044  deparse_expr_cxt context;
2045  int nestlevel;
2046  bool first;
2047  RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
2048  ListCell *lc,
2049  *lc2;
2050 
2051  /* Set up context struct for recursion */
2052  context.root = root;
2053  context.foreignrel = foreignrel;
2054  context.scanrel = foreignrel;
2055  context.buf = buf;
2056  context.params_list = params_list;
2057 
2058  appendStringInfoString(buf, "UPDATE ");
2059  deparseRelation(buf, rel);
2060  if (foreignrel->reloptkind == RELOPT_JOINREL)
2061  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
2062  appendStringInfoString(buf, " SET ");
2063 
2064  /* Make sure any constants in the exprs are printed portably */
2065  nestlevel = set_transmission_modes();
2066 
2067  first = true;
2068  forboth(lc, targetlist, lc2, targetAttrs)
2069  {
2070  TargetEntry *tle = lfirst_node(TargetEntry, lc);
2071  int attnum = lfirst_int(lc2);
2072 
2073  /* update's new-value expressions shouldn't be resjunk */
2074  Assert(!tle->resjunk);
2075 
2076  if (!first)
2077  appendStringInfoString(buf, ", ");
2078  first = false;
2079 
2080  deparseColumnRef(buf, rtindex, attnum, rte, false);
2081  appendStringInfoString(buf, " = ");
2082  deparseExpr((Expr *) tle->expr, &context);
2083  }
2084 
2085  reset_transmission_modes(nestlevel);
2086 
2087  if (foreignrel->reloptkind == RELOPT_JOINREL)
2088  {
2089  List *ignore_conds = NIL;
2090 
2091  appendStringInfoString(buf, " FROM ");
2092  deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
2093  &ignore_conds, params_list);
2094  remote_conds = list_concat(remote_conds, ignore_conds);
2095  }
2096 
2097  if (remote_conds)
2098  {
2099  appendStringInfoString(buf, " WHERE ");
2100  appendConditions(remote_conds, &context);
2101  }
2102 
2103  if (foreignrel->reloptkind == RELOPT_JOINREL)
2104  deparseExplicitTargetList(returningList, true, retrieved_attrs,
2105  &context);
2106  else
2107  deparseReturningList(buf, rte, rtindex, rel, false,
2108  NIL, returningList, retrieved_attrs);
2109 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
RelOptKind reloptkind
Definition: pathnodes.h:678
List ** params_list
Definition: deparse.c:103
int set_transmission_modes(void)
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs)
Definition: deparse.c:2199
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool resjunk
Definition: primnodes.h:1462
#define lfirst_int(lc)
Definition: pg_list.h:170
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1539
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1468
StringInfo buf
Definition: deparse.c:102
int16 attnum
Definition: pg_attribute.h:83
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
Expr * expr
Definition: primnodes.h:1455
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1616
RelOptInfo * scanrel
Definition: deparse.c:99
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:98
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2369

◆ deparseDistinctExpr()

static void deparseDistinctExpr ( DistinctExpr node,
deparse_expr_cxt context 
)
static

Definition at line 3023 of file deparse.c.

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

Referenced by deparseExpr().

3024 {
3025  StringInfo buf = context->buf;
3026 
3027  Assert(list_length(node->args) == 2);
3028 
3029  appendStringInfoChar(buf, '(');
3030  deparseExpr(linitial(node->args), context);
3031  appendStringInfoString(buf, " IS DISTINCT FROM ");
3032  deparseExpr(lsecond(node->args), context);
3033  appendStringInfoChar(buf, ')');
3034 }
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
List * args
Definition: primnodes.h:548

◆ deparseExplicitTargetList()

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

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

1543 {
1544  ListCell *lc;
1545  StringInfo buf = context->buf;
1546  int i = 0;
1547 
1548  *retrieved_attrs = NIL;
1549 
1550  foreach(lc, tlist)
1551  {
1552  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1553 
1554  if (i > 0)
1555  appendStringInfoString(buf, ", ");
1556  else if (is_returning)
1557  appendStringInfoString(buf, " RETURNING ");
1558 
1559  deparseExpr((Expr *) tle->expr, context);
1560 
1561  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1562  i++;
1563  }
1564 
1565  if (i == 0 && !is_returning)
1566  appendStringInfoString(buf, "NULL");
1567 }
#define NIL
Definition: pg_list.h:65
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * lappend_int(List *list, int datum)
Definition: list.c:354
StringInfo buf
Definition: deparse.c:102
Expr * expr
Definition: primnodes.h:1455
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
int i

◆ deparseExpr()

static void deparseExpr ( Expr expr,
deparse_expr_cxt context 
)
static

Definition at line 2572 of file deparse.c.

References deparseAggref(), deparseArrayExpr(), deparseBoolExpr(), deparseCaseExpr(), deparseConst(), deparseDistinctExpr(), deparseFuncExpr(), deparseNullTest(), deparseOpExpr(), deparseParam(), deparseRelabelType(), deparseScalarArrayOpExpr(), deparseSubscriptingRef(), deparseVar(), elog, ERROR, nodeTag, T_Aggref, T_ArrayExpr, T_BoolExpr, T_CaseExpr, 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(), deparseCaseExpr(), deparseDirectUpdateSql(), deparseDistinctExpr(), deparseExplicitTargetList(), deparseFuncExpr(), deparseNullTest(), deparseOpExpr(), deparseRelabelType(), deparseScalarArrayOpExpr(), deparseSortGroupClause(), deparseSubqueryTargetList(), and deparseSubscriptingRef().

2573 {
2574  if (node == NULL)
2575  return;
2576 
2577  switch (nodeTag(node))
2578  {
2579  case T_Var:
2580  deparseVar((Var *) node, context);
2581  break;
2582  case T_Const:
2583  deparseConst((Const *) node, context, 0);
2584  break;
2585  case T_Param:
2586  deparseParam((Param *) node, context);
2587  break;
2588  case T_SubscriptingRef:
2589  deparseSubscriptingRef((SubscriptingRef *) node, context);
2590  break;
2591  case T_FuncExpr:
2592  deparseFuncExpr((FuncExpr *) node, context);
2593  break;
2594  case T_OpExpr:
2595  deparseOpExpr((OpExpr *) node, context);
2596  break;
2597  case T_DistinctExpr:
2598  deparseDistinctExpr((DistinctExpr *) node, context);
2599  break;
2600  case T_ScalarArrayOpExpr:
2601  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2602  break;
2603  case T_RelabelType:
2604  deparseRelabelType((RelabelType *) node, context);
2605  break;
2606  case T_BoolExpr:
2607  deparseBoolExpr((BoolExpr *) node, context);
2608  break;
2609  case T_NullTest:
2610  deparseNullTest((NullTest *) node, context);
2611  break;
2612  case T_CaseExpr:
2613  deparseCaseExpr((CaseExpr *) node, context);
2614  break;
2615  case T_ArrayExpr:
2616  deparseArrayExpr((ArrayExpr *) node, context);
2617  break;
2618  case T_Aggref:
2619  deparseAggref((Aggref *) node, context);
2620  break;
2621  default:
2622  elog(ERROR, "unsupported expression type for deparse: %d",
2623  (int) nodeTag(node));
2624  break;
2625  }
2626 }
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:3246
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:3099
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:3137
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:3023
Definition: primnodes.h:186
#define ERROR
Definition: elog.h:46
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2809
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:3041
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2951
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:3220
static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2842
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:3086
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2637
Definition: nodes.h:157
Definition: nodes.h:156
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2703
#define nodeTag(nodeptr)
Definition: nodes.h:541
#define elog(elevel,...)
Definition: elog.h:232
static void deparseCaseExpr(CaseExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:3170
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2888
Definition: nodes.h:158

◆ deparseFromExpr()

static void deparseFromExpr ( List quals,
deparse_expr_cxt context 
)
static

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

1269 {
1270  StringInfo buf = context->buf;
1271  RelOptInfo *scanrel = context->scanrel;
1272 
1273  /* For upper relations, scanrel must be either a joinrel or a baserel */
1274  Assert(!IS_UPPER_REL(context->foreignrel) ||
1275  IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1276 
1277  /* Construct FROM clause */
1278  appendStringInfoString(buf, " FROM ");
1279  deparseFromExprForRel(buf, context->root, scanrel,
1280  (bms_membership(scanrel->relids) == BMS_MULTIPLE),
1281  (Index) 0, NULL, context->params_list);
1282 
1283  /* Construct WHERE clause */
1284  if (quals != NIL)
1285  {
1286  appendStringInfoString(buf, " WHERE ");
1287  appendConditions(quals, context);
1288  }
1289 }
#define NIL
Definition: pg_list.h:65
List ** params_list
Definition: deparse.c:103
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:654
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
Relids relids
Definition: pathnodes.h:681
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1468
StringInfo buf
Definition: deparse.c:102
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
unsigned int Index
Definition: c.h:549
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:664
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1616
RelOptInfo * scanrel
Definition: deparse.c:99
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseFromExprForRel()

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

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

1619 {
1620  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1621 
1622  if (IS_JOIN_REL(foreignrel))
1623  {
1624  StringInfoData join_sql_o;
1625  StringInfoData join_sql_i;
1626  RelOptInfo *outerrel = fpinfo->outerrel;
1627  RelOptInfo *innerrel = fpinfo->innerrel;
1628  bool outerrel_is_target = false;
1629  bool innerrel_is_target = false;
1630 
1631  if (ignore_rel > 0 && bms_is_member(ignore_rel, foreignrel->relids))
1632  {
1633  /*
1634  * If this is an inner join, add joinclauses to *ignore_conds and
1635  * set it to empty so that those can be deparsed into the WHERE
1636  * clause. Note that since the target relation can never be
1637  * within the nullable side of an outer join, those could safely
1638  * be pulled up into the WHERE clause (see foreign_join_ok()).
1639  * Note also that since the target relation is only inner-joined
1640  * to any other relation in the query, all conditions in the join
1641  * tree mentioning the target relation could be deparsed into the
1642  * WHERE clause by doing this recursively.
1643  */
1644  if (fpinfo->jointype == JOIN_INNER)
1645  {
1646  *ignore_conds = list_concat(*ignore_conds,
1647  fpinfo->joinclauses);
1648  fpinfo->joinclauses = NIL;
1649  }
1650 
1651  /*
1652  * Check if either of the input relations is the target relation.
1653  */
1654  if (outerrel->relid == ignore_rel)
1655  outerrel_is_target = true;
1656  else if (innerrel->relid == ignore_rel)
1657  innerrel_is_target = true;
1658  }
1659 
1660  /* Deparse outer relation if not the target relation. */
1661  if (!outerrel_is_target)
1662  {
1663  initStringInfo(&join_sql_o);
1664  deparseRangeTblRef(&join_sql_o, root, outerrel,
1665  fpinfo->make_outerrel_subquery,
1666  ignore_rel, ignore_conds, params_list);
1667 
1668  /*
1669  * If inner relation is the target relation, skip deparsing it.
1670  * Note that since the join of the target relation with any other
1671  * relation in the query is an inner join and can never be within
1672  * the nullable side of an outer join, the join could be
1673  * interchanged with higher-level joins (cf. identity 1 on outer
1674  * join reordering shown in src/backend/optimizer/README), which
1675  * means it's safe to skip the target-relation deparsing here.
1676  */
1677  if (innerrel_is_target)
1678  {
1679  Assert(fpinfo->jointype == JOIN_INNER);
1680  Assert(fpinfo->joinclauses == NIL);
1681  appendBinaryStringInfo(buf, join_sql_o.data, join_sql_o.len);
1682  return;
1683  }
1684  }
1685 
1686  /* Deparse inner relation if not the target relation. */
1687  if (!innerrel_is_target)
1688  {
1689  initStringInfo(&join_sql_i);
1690  deparseRangeTblRef(&join_sql_i, root, innerrel,
1691  fpinfo->make_innerrel_subquery,
1692  ignore_rel, ignore_conds, params_list);
1693 
1694  /*
1695  * If outer relation is the target relation, skip deparsing it.
1696  * See the above note about safety.
1697  */
1698  if (outerrel_is_target)
1699  {
1700  Assert(fpinfo->jointype == JOIN_INNER);
1701  Assert(fpinfo->joinclauses == NIL);
1702  appendBinaryStringInfo(buf, join_sql_i.data, join_sql_i.len);
1703  return;
1704  }
1705  }
1706 
1707  /* Neither of the relations is the target relation. */
1708  Assert(!outerrel_is_target && !innerrel_is_target);
1709 
1710  /*
1711  * For a join relation FROM clause entry is deparsed as
1712  *
1713  * ((outer relation) <join type> (inner relation) ON (joinclauses))
1714  */
1715  appendStringInfo(buf, "(%s %s JOIN %s ON ", join_sql_o.data,
1716  get_jointype_name(fpinfo->jointype), join_sql_i.data);
1717 
1718  /* Append join clause; (TRUE) if no join clause */
1719  if (fpinfo->joinclauses)
1720  {
1721  deparse_expr_cxt context;
1722 
1723  context.buf = buf;
1724  context.foreignrel = foreignrel;
1725  context.scanrel = foreignrel;
1726  context.root = root;
1727  context.params_list = params_list;
1728 
1729  appendStringInfoChar(buf, '(');
1730  appendConditions(fpinfo->joinclauses, &context);
1731  appendStringInfoChar(buf, ')');
1732  }
1733  else
1734  appendStringInfoString(buf, "(TRUE)");
1735 
1736  /* End the FROM clause entry. */
1737  appendStringInfoChar(buf, ')');
1738  }
1739  else
1740  {
1741  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1742 
1743  /*
1744  * Core code already has some lock on each rel being planned, so we
1745  * can use NoLock here.
1746  */
1747  Relation rel = table_open(rte->relid, NoLock);
1748 
1749  deparseRelation(buf, rel);
1750 
1751  /*
1752  * Add a unique alias to avoid any conflict in relation names due to
1753  * pulled up subqueries in the query being built for a pushed down
1754  * join.
1755  */
1756  if (use_alias)
1757  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid);
1758 
1759  table_close(rel, NoLock);
1760  }
1761 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
List ** params_list
Definition: deparse.c:103
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
const char * get_jointype_name(JoinType jointype)
Definition: deparse.c:1502
Relids relids
Definition: pathnodes.h:681
static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1767
Index relid
Definition: pathnodes.h:709
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:1468
StringInfo buf
Definition: deparse.c:102
void * fdw_private
Definition: pathnodes.h:737
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
RelOptInfo * innerrel
Definition: postgres_fdw.h:103
RelOptInfo * scanrel
Definition: deparse.c:99
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
RelOptInfo * foreignrel
Definition: deparse.c:98
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ deparseFuncExpr()

static void deparseFuncExpr ( FuncExpr node,
deparse_expr_cxt context 
)
static

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

2889 {
2890  StringInfo buf = context->buf;
2891  bool use_variadic;
2892  bool first;
2893  ListCell *arg;
2894 
2895  /*
2896  * If the function call came from an implicit coercion, then just show the
2897  * first argument.
2898  */
2899  if (node->funcformat == COERCE_IMPLICIT_CAST)
2900  {
2901  deparseExpr((Expr *) linitial(node->args), context);
2902  return;
2903  }
2904 
2905  /*
2906  * If the function call came from a cast, then show the first argument
2907  * plus an explicit cast operation.
2908  */
2909  if (node->funcformat == COERCE_EXPLICIT_CAST)
2910  {
2911  Oid rettype = node->funcresulttype;
2912  int32 coercedTypmod;
2913 
2914  /* Get the typmod if this is a length-coercion function */
2915  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2916 
2917  deparseExpr((Expr *) linitial(node->args), context);
2918  appendStringInfo(buf, "::%s",
2919  deparse_type_name(rettype, coercedTypmod));
2920  return;
2921  }
2922 
2923  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2924  use_variadic = node->funcvariadic;
2925 
2926  /*
2927  * Normal function: display as proname(args).
2928  */
2929  appendFunctionName(node->funcid, context);
2930  appendStringInfoChar(buf, '(');
2931 
2932  /* ... and all the arguments */
2933  first = true;
2934  foreach(arg, node->args)
2935  {
2936  if (!first)
2937  appendStringInfoString(buf, ", ");
2938  if (use_variadic && lnext(node->args, arg) == NULL)
2939  appendStringInfoString(buf, "VARIADIC ");
2940  deparseExpr((Expr *) lfirst(arg), context);
2941  first = false;
2942  }
2943  appendStringInfoChar(buf, ')');
2944 }
Oid funcresulttype
Definition: primnodes.h:496
List * args
Definition: primnodes.h:503
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3555
Definition: nodes.h:536
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:503
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
CoercionForm funcformat
Definition: primnodes.h:500
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
Oid funcid
Definition: primnodes.h:495
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:1052
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
void * arg
bool funcvariadic
Definition: primnodes.h:498

◆ deparseInsertSql()

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

Definition at line 1841 of file deparse.c.

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

Referenced by postgresBeginForeignInsert(), and postgresPlanForeignModify().

1846 {
1847  TupleDesc tupdesc = RelationGetDescr(rel);
1848  AttrNumber pindex;
1849  bool first;
1850  ListCell *lc;
1851 
1852  appendStringInfoString(buf, "INSERT INTO ");
1853  deparseRelation(buf, rel);
1854 
1855  if (targetAttrs)
1856  {
1857  appendStringInfoChar(buf, '(');
1858 
1859  first = true;
1860  foreach(lc, targetAttrs)
1861  {
1862  int attnum = lfirst_int(lc);
1863 
1864  if (!first)
1865  appendStringInfoString(buf, ", ");
1866  first = false;
1867 
1868  deparseColumnRef(buf, rtindex, attnum, rte, false);
1869  }
1870 
1871  appendStringInfoString(buf, ") VALUES (");
1872 
1873  pindex = 1;
1874  first = true;
1875  foreach(lc, targetAttrs)
1876  {
1877  int attnum = lfirst_int(lc);
1878  Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
1879 
1880  if (!first)
1881  appendStringInfoString(buf, ", ");
1882  first = false;
1883 
1884  if (attr->attgenerated)
1885  appendStringInfoString(buf, "DEFAULT");
1886  else
1887  {
1888  appendStringInfo(buf, "$%d", pindex);
1889  pindex++;
1890  }
1891  }
1892 
1893  appendStringInfoChar(buf, ')');
1894  }
1895  else
1896  appendStringInfoString(buf, " DEFAULT VALUES");
1897  *values_end_len = buf->len;
1898 
1899  if (doNothing)
1900  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1901 
1902  deparseReturningList(buf, rte, rtindex, rel,
1903  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1904  withCheckOptionList, returningList, retrieved_attrs);
1905 }
#define RelationGetDescr(relation)
Definition: rel.h:503
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
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:2199
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define lfirst_int(lc)
Definition: pg_list.h:170
TriggerDesc * trigdesc
Definition: rel.h:115
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
bool trig_insert_after_row
Definition: reltrigger.h:57
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
int16 attnum
Definition: pg_attribute.h:83
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2369

◆ deparseLockingClause()

static void deparseLockingClause ( deparse_expr_cxt context)
static

Definition at line 1378 of file deparse.c.

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

Referenced by deparseSelectStmtForRel().

1379 {
1380  StringInfo buf = context->buf;
1381  PlannerInfo *root = context->root;
1382  RelOptInfo *rel = context->scanrel;
1383  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1384  int relid = -1;
1385 
1386  while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1387  {
1388  /*
1389  * Ignore relation if it appears in a lower subquery. Locking clause
1390  * for such a relation is included in the subquery if necessary.
1391  */
1392  if (bms_is_member(relid, fpinfo->lower_subquery_rels))
1393  continue;
1394 
1395  /*
1396  * Add FOR UPDATE/SHARE if appropriate. We apply locking during the
1397  * initial row fetch, rather than later on as is done for local
1398  * tables. The extra roundtrips involved in trying to duplicate the
1399  * local semantics exactly don't seem worthwhile (see also comments
1400  * for RowMarkType).
1401  *
1402  * Note: because we actually run the query as a cursor, this assumes
1403  * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't
1404  * before 8.3.
1405  */
1406  if (bms_is_member(relid, root->all_result_relids) &&
1407  (root->parse->commandType == CMD_UPDATE ||
1408  root->parse->commandType == CMD_DELETE))
1409  {
1410  /* Relation is UPDATE/DELETE target, so use FOR UPDATE */
1411  appendStringInfoString(buf, " FOR UPDATE");
1412 
1413  /* Add the relation alias if we are here for a join relation */
1414  if (IS_JOIN_REL(rel))
1415  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1416  }
1417  else
1418  {
1419  PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid);
1420 
1421  if (rc)
1422  {
1423  /*
1424  * Relation is specified as a FOR UPDATE/SHARE target, so
1425  * handle that. (But we could also see LCS_NONE, meaning this
1426  * isn't a target relation after all.)
1427  *
1428  * For now, just ignore any [NO] KEY specification, since (a)
1429  * it's not clear what that means for a remote table that we
1430  * don't have complete information about, and (b) it wouldn't
1431  * work anyway on older remote servers. Likewise, we don't
1432  * worry about NOWAIT.
1433  */
1434  switch (rc->strength)
1435  {
1436  case LCS_NONE:
1437  /* No locking needed */
1438  break;
1439  case LCS_FORKEYSHARE:
1440  case LCS_FORSHARE:
1441  appendStringInfoString(buf, " FOR SHARE");
1442  break;
1443  case LCS_FORNOKEYUPDATE:
1444  case LCS_FORUPDATE:
1445  appendStringInfoString(buf, " FOR UPDATE");
1446  break;
1447  }
1448 
1449  /* Add the relation alias if we are here for a join relation */
1450  if (bms_membership(rel->relids) == BMS_MULTIPLE &&
1451  rc->strength != LCS_NONE)
1452  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1453  }
1454  }
1455  }
1456 }
List * rowMarks
Definition: pathnodes.h:288
Relids all_result_relids
Definition: pathnodes.h:276
Query * parse
Definition: pathnodes.h:162
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Relids lower_subquery_rels
Definition: postgres_fdw.h:119
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
#define REL_ALIAS_PREFIX
Definition: deparse.c:106
Relids relids
Definition: pathnodes.h:681
StringInfo buf
Definition: deparse.c:102
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
void * fdw_private
Definition: pathnodes.h:737
CmdType commandType
Definition: parsenodes.h:120
PlannerInfo * root
Definition: deparse.c:97
LockClauseStrength strength
Definition: plannodes.h:1125
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:425
RelOptInfo * scanrel
Definition: deparse.c:99
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ deparseNullTest()

static void deparseNullTest ( NullTest node,
deparse_expr_cxt context 
)
static

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

3138 {
3139  StringInfo buf = context->buf;
3140 
3141  appendStringInfoChar(buf, '(');
3142  deparseExpr(node->arg, context);
3143 
3144  /*
3145  * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
3146  * shorter and traditional. If it's a rowtype input but we're applying a
3147  * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
3148  * correct.
3149  */
3150  if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
3151  {
3152  if (node->nulltesttype == IS_NULL)
3153  appendStringInfoString(buf, " IS NULL)");
3154  else
3155  appendStringInfoString(buf, " IS NOT NULL)");
3156  }
3157  else
3158  {
3159  if (node->nulltesttype == IS_NULL)
3160  appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
3161  else
3162  appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
3163  }
3164 }
Definition: nodes.h:536
Expr * arg
Definition: primnodes.h:1265
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2602
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
NullTestType nulltesttype
Definition: primnodes.h:1266
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
bool argisrow
Definition: primnodes.h:1267

◆ deparseOperatorName()

static void deparseOperatorName ( StringInfo  buf,
Form_pg_operator  opform 
)
static

Definition at line 2995 of file deparse.c.

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

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

2996 {
2997  char *opname;
2998 
2999  /* opname is not a SQL identifier, so we should not quote it. */
3000  opname = NameStr(opform->oprname);
3001 
3002  /* Print schema name only if it's not pg_catalog */
3003  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
3004  {
3005  const char *opnspname;
3006 
3007  opnspname = get_namespace_name(opform->oprnamespace);
3008  /* Print fully qualified operator name. */
3009  appendStringInfo(buf, "OPERATOR(%s.%s)",
3010  quote_identifier(opnspname), opname);
3011  }
3012  else
3013  {
3014  /* Just print operator name. */
3015  appendStringInfoString(buf, opname);
3016  }
3017 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define NameStr(name)
Definition: c.h:681

◆ deparseOpExpr()

static void deparseOpExpr ( OpExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2951 of file deparse.c.

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

Referenced by deparseExpr().

2952 {
2953  StringInfo buf = context->buf;
2954  HeapTuple tuple;
2955  Form_pg_operator form;
2956  char oprkind;
2957 
2958  /* Retrieve information about the operator from system catalog. */
2959  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2960  if (!HeapTupleIsValid(tuple))
2961  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2962  form = (Form_pg_operator) GETSTRUCT(tuple);
2963  oprkind = form->oprkind;
2964 
2965  /* Sanity check. */
2966  Assert((oprkind == 'l' && list_length(node->args) == 1) ||
2967  (oprkind == 'b' && list_length(node->args) == 2));
2968 
2969  /* Always parenthesize the expression. */
2970  appendStringInfoChar(buf, '(');
2971 
2972  /* Deparse left operand, if any. */
2973  if (oprkind == 'b')
2974  {
2975  deparseExpr(linitial(node->args), context);
2976  appendStringInfoChar(buf, ' ');
2977  }
2978 
2979  /* Deparse operator name. */
2980  deparseOperatorName(buf, form);
2981 
2982  /* Deparse right operand. */
2983  appendStringInfoChar(buf, ' ');
2984  deparseExpr(llast(node->args), context);
2985 
2986  appendStringInfoChar(buf, ')');
2987 
2988  ReleaseSysCache(tuple);
2989 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define llast(l)
Definition: pg_list.h:194
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2995
#define linitial(l)
Definition: pg_list.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
#define elog(elevel,...)
Definition: elog.h:232
Oid opno
Definition: primnodes.h:542
List * args
Definition: primnodes.h:548

◆ deparseParam()

static void deparseParam ( Param node,
deparse_expr_cxt context 
)
static

Definition at line 2809 of file deparse.c.

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

Referenced by deparseExpr().

2810 {
2811  if (context->params_list)
2812  {
2813  int pindex = 0;
2814  ListCell *lc;
2815 
2816  /* find its index in params_list */
2817  foreach(lc, *context->params_list)
2818  {
2819  pindex++;
2820  if (equal(node, (Node *) lfirst(lc)))
2821  break;
2822  }
2823  if (lc == NULL)
2824  {
2825  /* not in list, so add it */
2826  pindex++;
2827  *context->params_list = lappend(*context->params_list, node);
2828  }
2829 
2830  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2831  }
2832  else
2833  {
2834  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2835  }
2836 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3149
List ** params_list
Definition: deparse.c:103
Definition: nodes.h:536
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3397
List * lappend(List *list, void *datum)
Definition: list.c:336
int32 paramtypmod
Definition: primnodes.h:270
#define lfirst(lc)
Definition: pg_list.h:169
Oid paramtype
Definition: primnodes.h:269
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3423

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

1770 {
1771  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1772 
1773  /* Should only be called in these cases. */
1774  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1775 
1776  Assert(fpinfo->local_conds == NIL);
1777 
1778  /* If make_subquery is true, deparse the relation as a subquery. */
1779  if (make_subquery)
1780  {
1781  List *retrieved_attrs;
1782  int ncols;
1783 
1784  /*
1785  * The given relation shouldn't contain the target relation, because
1786  * this should only happen for input relations for a full join, and
1787  * such relations can never contain an UPDATE/DELETE target.
1788  */
1789  Assert(ignore_rel == 0 ||
1790  !bms_is_member(ignore_rel, foreignrel->relids));
1791 
1792  /* Deparse the subquery representing the relation. */
1793  appendStringInfoChar(buf, '(');
1794  deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1795  fpinfo->remote_conds, NIL,
1796  false, false, true,
1797  &retrieved_attrs, params_list);
1798  appendStringInfoChar(buf, ')');
1799 
1800  /* Append the relation alias. */
1802  fpinfo->relation_index);
1803 
1804  /*
1805  * Append the column aliases if needed. Note that the subquery emits
1806  * expressions specified in the relation's reltarget (see
1807  * deparseSubqueryTargetList).
1808  */
1809  ncols = list_length(foreignrel->reltarget->exprs);
1810  if (ncols > 0)
1811  {
1812  int i;
1813 
1814  appendStringInfoChar(buf, '(');
1815  for (i = 1; i <= ncols; i++)
1816  {
1817  if (i > 1)
1818  appendStringInfoString(buf, ", ");
1819 
1821  }
1822  appendStringInfoChar(buf, ')');
1823  }
1824  }
1825  else
1826  deparseFromExprForRel(buf, root, foreignrel, true, ignore_rel,
1827  ignore_conds, params_list);
1828 }
#define NIL
Definition: pg_list.h:65
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:654
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:111
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Relids relids
Definition: pathnodes.h:681
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
List * exprs
Definition: pathnodes.h:1110
void * fdw_private
Definition: pathnodes.h:737
#define Assert(condition)
Definition: c.h:804
void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, bool has_final_sort, bool has_limit, bool is_subquery, List **retrieved_attrs, List **params_list)
Definition: deparse.c:1128
static int list_length(const List *l)
Definition: pg_list.h:149
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list)
Definition: deparse.c:1616
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:692

◆ deparseRelabelType()

static void deparseRelabelType ( RelabelType node,
deparse_expr_cxt context 
)
static

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

3087 {
3088  deparseExpr(node->arg, context);
3089  if (node->relabelformat != COERCE_IMPLICIT_CAST)
3090  appendStringInfo(context->buf, "::%s",
3092  node->resulttypmod));
3093 }
Expr * arg
Definition: primnodes.h:860
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
StringInfo buf
Definition: deparse.c:102
Oid resulttype
Definition: primnodes.h:861
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:1052
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
int32 resulttypmod
Definition: primnodes.h:862
CoercionForm relabelformat
Definition: primnodes.h:864

◆ deparseRelation()

static void deparseRelation ( StringInfo  buf,
Relation  rel 
)
static

Definition at line 2497 of file deparse.c.

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

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

2498 {
2499  ForeignTable *table;
2500  const char *nspname = NULL;
2501  const char *relname = NULL;
2502  ListCell *lc;
2503 
2504  /* obtain additional catalog information. */
2505  table = GetForeignTable(RelationGetRelid(rel));
2506 
2507  /*
2508  * Use value of FDW options if any, instead of the name of object itself.
2509  */
2510  foreach(lc, table->options)
2511  {
2512  DefElem *def = (DefElem *) lfirst(lc);
2513 
2514  if (strcmp(def->defname, "schema_name") == 0)
2515  nspname = defGetString(def);
2516  else if (strcmp(def->defname, "table_name") == 0)
2517  relname = defGetString(def);
2518  }
2519 
2520  /*
2521  * Note: we could skip printing the schema name if it's pg_catalog, but
2522  * that doesn't seem worth the trouble.
2523  */
2524  if (nspname == NULL)
2525  nspname = get_namespace_name(RelationGetNamespace(rel));
2526  if (relname == NULL)
2527  relname = RelationGetRelationName(rel);
2528 
2529  appendStringInfo(buf, "%s.%s",
2530  quote_identifier(nspname), quote_identifier(relname));
2531 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:248
NameData relname
Definition: pg_class.h:38
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * defGetString(DefElem *def)
Definition: define.c:49
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define RelationGetRelationName(relation)
Definition: rel.h:511
#define lfirst(lc)
Definition: pg_list.h:169
List * options
Definition: foreign.h:57
char * defname
Definition: parsenodes.h:758
#define RelationGetRelid(relation)
Definition: rel.h:477
#define RelationGetNamespace(relation)
Definition: rel.h:518

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

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

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

2205 {
2206  Bitmapset *attrs_used = NULL;
2207 
2208  if (trig_after_row)
2209  {
2210  /* whole-row reference acquires all non-system columns */
2211  attrs_used =
2213  }
2214 
2215  if (withCheckOptionList != NIL)
2216  {
2217  /*
2218  * We need the attrs, non-system and system, mentioned in the local
2219  * query's WITH CHECK OPTION list.
2220  *
2221  * Note: we do this to ensure that WCO constraints will be evaluated
2222  * on the data actually inserted/updated on the remote side, which
2223  * might differ from the data supplied by the core code, for example
2224  * as a result of remote triggers.
2225  */
2226  pull_varattnos((Node *) withCheckOptionList, rtindex,
2227  &attrs_used);
2228  }
2229 
2230  if (returningList != NIL)
2231  {
2232  /*
2233  * We need the attrs, non-system and system, mentioned in the local
2234  * query's RETURNING list.
2235  */
2236  pull_varattnos((Node *) returningList, rtindex,
2237  &attrs_used);
2238  }
2239 
2240  if (attrs_used != NULL)
2241  deparseTargetList(buf, rte, rtindex, rel, true, attrs_used, false,
2242  retrieved_attrs);
2243  else
2244  *retrieved_attrs = NIL;
2245 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:536
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:281
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:1302
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186

◆ deparseScalarArrayOpExpr()

static void deparseScalarArrayOpExpr ( ScalarArrayOpExpr node,
deparse_expr_cxt context 
)
static

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

3042 {
3043  StringInfo buf = context->buf;
3044  HeapTuple tuple;
3045  Form_pg_operator form;
3046  Expr *arg1;
3047  Expr *arg2;
3048 
3049  /* Retrieve information about the operator from system catalog. */
3050  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
3051  if (!HeapTupleIsValid(tuple))
3052  elog(ERROR, "cache lookup failed for operator %u", node->opno);
3053  form = (Form_pg_operator) GETSTRUCT(tuple);
3054 
3055  /* Sanity check. */
3056  Assert(list_length(node->args) == 2);
3057 
3058  /* Always parenthesize the expression. */
3059  appendStringInfoChar(buf, '(');
3060 
3061  /* Deparse left operand. */
3062  arg1 = linitial(node->args);
3063  deparseExpr(arg1, context);
3064  appendStringInfoChar(buf, ' ');
3065 
3066  /* Deparse operator name plus decoration. */
3067  deparseOperatorName(buf, form);
3068  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
3069 
3070  /* Deparse right operand. */
3071  arg2 = lsecond(node->args);
3072  deparseExpr(arg2, context);
3073 
3074  appendStringInfoChar(buf, ')');
3075 
3076  /* Always parenthesize the expression. */
3077  appendStringInfoChar(buf, ')');
3078 
3079  ReleaseSysCache(tuple);
3080 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2995
#define lsecond(l)
Definition: pg_list.h:179
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
StringInfo buf
Definition: deparse.c:102
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
#define elog(elevel,...)
Definition: elog.h:232

◆ deparseSelectSql()

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

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

1212 {
1213  StringInfo buf = context->buf;
1214  RelOptInfo *foreignrel = context->foreignrel;
1215  PlannerInfo *root = context->root;
1216  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1217 
1218  /*
1219  * Construct SELECT list
1220  */
1221  appendStringInfoString(buf, "SELECT ");
1222 
1223  if (is_subquery)
1224  {
1225  /*
1226  * For a relation that is deparsed as a subquery, emit expressions
1227  * specified in the relation's reltarget. Note that since this is for
1228  * the subquery, no need to care about *retrieved_attrs.
1229  */
1230  deparseSubqueryTargetList(context);
1231  }
1232  else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
1233  {
1234  /*
1235  * For a join or upper relation the input tlist gives the list of
1236  * columns required to be fetched from the foreign server.
1237  */
1238  deparseExplicitTargetList(tlist, false, retrieved_attrs, context);
1239  }
1240  else
1241  {
1242  /*
1243  * For a base relation fpinfo->attrs_used gives the list of columns
1244  * required to be fetched from the foreign server.
1245  */
1246  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1247 
1248  /*
1249  * Core code already has some lock on each rel being planned, so we
1250  * can use NoLock here.
1251  */
1252  Relation rel = table_open(rte->relid, NoLock);
1253 
1254  deparseTargetList(buf, rte, foreignrel->relid, rel, false,
1255  fpinfo->attrs_used, false, retrieved_attrs);
1256  table_close(rel, NoLock);
1257  }
1258 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1575
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
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:1302
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:68
static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1539
Index relid
Definition: pathnodes.h:709
StringInfo buf
Definition: deparse.c:102
void * fdw_private
Definition: pathnodes.h:737
PlannerInfo * root
Definition: deparse.c:97
Bitmapset * attrs_used
Definition: postgres_fdw.h:50
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:664
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseSelectStmtForRel()

void deparseSelectStmtForRel ( StringInfo  buf,
PlannerInfo root,
RelOptInfo rel,
List tlist,
List remote_conds,
List pathkeys,
bool  has_final_sort,
bool  has_limit,
bool  is_subquery,
List **  retrieved_attrs,
List **  params_list 
)

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

1132 {
1133  deparse_expr_cxt context;
1134  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1135  List *quals;
1136 
1137  /*
1138  * We handle relations for foreign tables, joins between those and upper
1139  * relations.
1140  */
1141  Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel));
1142 
1143  /* Fill portions of context common to upper, join and base relation */
1144  context.buf = buf;
1145  context.root = root;
1146  context.foreignrel = rel;
1147  context.scanrel = IS_UPPER_REL(rel) ? fpinfo->outerrel : rel;
1148  context.params_list = params_list;
1149 
1150  /* Construct SELECT clause */
1151  deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
1152 
1153  /*
1154  * For upper relations, the WHERE clause is built from the remote
1155  * conditions of the underlying scan relation; otherwise, we can use the
1156  * supplied list of remote conditions directly.
1157  */
1158  if (IS_UPPER_REL(rel))
1159  {
1160  PgFdwRelationInfo *ofpinfo;
1161 
1162  ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
1163  quals = ofpinfo->remote_conds;
1164  }
1165  else
1166  quals = remote_conds;
1167 
1168  /* Construct FROM and WHERE clauses */
1169  deparseFromExpr(quals, &context);
1170 
1171  if (IS_UPPER_REL(rel))
1172  {
1173  /* Append GROUP BY clause */
1174  appendGroupByClause(tlist, &context);
1175 
1176  /* Append HAVING clause */
1177  if (remote_conds)
1178  {
1179  appendStringInfoString(buf, " HAVING ");
1180  appendConditions(remote_conds, &context);
1181  }
1182  }
1183 
1184  /* Add ORDER BY clause if we found any useful pathkeys */
1185  if (pathkeys)
1186  appendOrderByClause(pathkeys, has_final_sort, &context);
1187 
1188  /* Add LIMIT clause if necessary */
1189  if (has_limit)
1190  appendLimitClause(&context);
1191 
1192  /* Add any necessary FOR UPDATE/SHARE. */
1193  deparseLockingClause(&context);
1194 }
static void appendGroupByClause(List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3436
List ** params_list
Definition: deparse.c:103
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
static void appendLimitClause(deparse_expr_cxt *context)
Definition: deparse.c:3527
RelOptInfo * outerrel
Definition: postgres_fdw.h:102
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:654
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
static void appendOrderByClause(List *pathkeys, bool has_final_sort, deparse_expr_cxt *context)
Definition: deparse.c:3473
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1210
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1468
StringInfo buf
Definition: deparse.c:102
void * fdw_private
Definition: pathnodes.h:737
PlannerInfo * root
Definition: deparse.c:97
#define Assert(condition)
Definition: c.h:804
static void deparseFromExpr(List *quals, deparse_expr_cxt *context)
Definition: deparse.c:1268
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:664
static void deparseLockingClause(deparse_expr_cxt *context)
Definition: deparse.c:1378
RelOptInfo * scanrel
Definition: deparse.c:99
Definition: pg_list.h:50
RelOptInfo * foreignrel
Definition: deparse.c:98

◆ deparseSortGroupClause()

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

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

3592 {
3593  StringInfo buf = context->buf;
3594  TargetEntry *tle;
3595  Expr *expr;
3596 
3597  tle = get_sortgroupref_tle(ref, tlist);
3598  expr = tle->expr;
3599 
3600  if (force_colno)
3601  {
3602  /* Use column-number form when requested by caller. */
3603  Assert(!tle->resjunk);
3604  appendStringInfo(buf, "%d", tle->resno);
3605  }
3606  else if (expr && IsA(expr, Const))
3607  {
3608  /*
3609  * Force a typecast here so that we don't emit something like "GROUP
3610  * BY 2", which will be misconstrued as a column position rather than
3611  * a constant.
3612  */
3613  deparseConst((Const *) expr, context, 1);
3614  }
3615  else if (!expr || IsA(expr, Var))
3616  deparseExpr(expr, context);
3617  else
3618  {
3619  /* Always parenthesize the expression. */
3620  appendStringInfoChar(buf, '(');
3621  deparseExpr(expr, context);
3622  appendStringInfoChar(buf, ')');
3623  }
3624 
3625  return (Node *) expr;
3626 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Definition: nodes.h:536
Definition: primnodes.h:186
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool resjunk
Definition: primnodes.h:1462
static char * buf
Definition: pg_test_fsync.c:68
AttrNumber resno
Definition: primnodes.h:1456
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:334
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define Assert(condition)
Definition: c.h:804
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2703
Expr * expr
Definition: primnodes.h:1455
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572

◆ deparseStringLiteral()

void deparseStringLiteral ( StringInfo  buf,
const char *  val 
)

Definition at line 2537 of file deparse.c.

References appendStringInfoChar(), ESCAPE_STRING_SYNTAX, and SQL_STR_DOUBLE.

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

2538 {
2539  const char *valptr;
2540 
2541  /*
2542  * Rather than making assumptions about the remote server's value of
2543  * standard_conforming_strings, always use E'foo' syntax if there are any
2544  * backslashes. This will fail on remote servers before 8.1, but those
2545  * are long out of support.
2546  */
2547  if (strchr(val, '\\') != NULL)
2549  appendStringInfoChar(buf, '\'');
2550  for (valptr = val; *valptr; valptr++)
2551  {
2552  char ch = *valptr;
2553 
2554  if (SQL_STR_DOUBLE(ch, true))
2555  appendStringInfoChar(buf, ch);
2556  appendStringInfoChar(buf, ch);
2557  }
2558  appendStringInfoChar(buf, '\'');
2559 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1167
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1164
long val
Definition: informix.c:664

◆ deparseSubqueryTargetList()

static void deparseSubqueryTargetList ( deparse_expr_cxt context)
static

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

1576 {
1577  StringInfo buf = context->buf;
1578  RelOptInfo *foreignrel = context->foreignrel;
1579  bool first;
1580  ListCell *lc;
1581 
1582  /* Should only be called in these cases. */
1583  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1584 
1585  first = true;
1586  foreach(lc, foreignrel->reltarget->exprs)
1587  {
1588  Node *node = (Node *) lfirst(lc);
1589 
1590  if (!first)
1591  appendStringInfoString(buf, ", ");
1592  first = false;
1593 
1594  deparseExpr((Expr *) node, context);
1595  }
1596 
1597  /* Don't generate bad syntax if no expressions */
1598  if (first)
1599  appendStringInfoString(buf, "NULL");
1600 }
Definition: nodes.h:536
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:654
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
List * exprs
Definition: pathnodes.h:1110
StringInfo buf
Definition: deparse.c:102
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
RelOptInfo * foreignrel
Definition: deparse.c:98
struct PathTarget * reltarget
Definition: pathnodes.h:692

◆ deparseSubscriptingRef()

static void deparseSubscriptingRef ( SubscriptingRef node,
deparse_expr_cxt context 
)
static

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

2843 {
2844  StringInfo buf = context->buf;
2845  ListCell *lowlist_item;
2846  ListCell *uplist_item;
2847 
2848  /* Always parenthesize the expression. */
2849  appendStringInfoChar(buf, '(');
2850 
2851  /*
2852  * Deparse referenced array expression first. If that expression includes
2853  * a cast, we have to parenthesize to prevent the array subscript from
2854  * being taken as typename decoration. We can avoid that in the typical
2855  * case of subscripting a Var, but otherwise do it.
2856  */
2857  if (IsA(node->refexpr, Var))
2858  deparseExpr(node->refexpr, context);
2859  else
2860  {
2861  appendStringInfoChar(buf, '(');
2862  deparseExpr(node->refexpr, context);
2863  appendStringInfoChar(buf, ')');
2864  }
2865 
2866  /* Deparse subscript expressions. */
2867  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2868  foreach(uplist_item, node->refupperindexpr)
2869  {
2870  appendStringInfoChar(buf, '[');
2871  if (lowlist_item)
2872  {
2873  deparseExpr(lfirst(lowlist_item), context);
2874  appendStringInfoChar(buf, ':');
2875  lowlist_item = lnext(node->reflowerindexpr, lowlist_item);
2876  }
2877  deparseExpr(lfirst(uplist_item), context);
2878  appendStringInfoChar(buf, ']');
2879  }
2880 
2881  appendStringInfoChar(buf, ')');
2882 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Definition: primnodes.h:186
List * refupperindexpr
Definition: primnodes.h:444
static char * buf
Definition: pg_test_fsync.c:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
StringInfo buf
Definition: deparse.c:102
#define lfirst(lc)
Definition: pg_list.h:169
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2572
List * reflowerindexpr
Definition: primnodes.h:446
Expr * refexpr
Definition: primnodes.h:449

◆ deparseTargetList()

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

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

1310 {
1311  TupleDesc tupdesc = RelationGetDescr(rel);
1312  bool have_wholerow;
1313  bool first;
1314  int i;
1315 
1316  *retrieved_attrs = NIL;
1317 
1318  /* If there's a whole-row reference, we'll need all the columns. */
1320  attrs_used);
1321 
1322  first = true;
1323  for (i = 1; i <= tupdesc->natts; i++)
1324  {
1325  Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1);
1326 
1327  /* Ignore dropped attributes. */
1328  if (attr->attisdropped)
1329  continue;
1330 
1331  if (have_wholerow ||
1333  attrs_used))
1334  {
1335  if (!first)
1336  appendStringInfoString(buf, ", ");
1337  else if (is_returning)
1338  appendStringInfoString(buf, " RETURNING ");
1339  first = false;
1340 
1341  deparseColumnRef(buf, rtindex, i, rte, qualify_col);
1342 
1343  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1344  }
1345  }
1346 
1347  /*
1348  * Add ctid if needed. We currently don't support retrieving any other
1349  * system columns.
1350  */
1352  attrs_used))
1353  {
1354  if (!first)
1355  appendStringInfoString(buf, ", ");
1356  else if (is_returning)
1357  appendStringInfoString(buf, " RETURNING ");
1358  first = false;
1359 
1360  if (qualify_col)
1361  ADD_REL_QUALIFIER(buf, rtindex);
1362  appendStringInfoString(buf, "ctid");
1363 
1364  *retrieved_attrs = lappend_int(*retrieved_attrs,
1366  }
1367 
1368  /* Don't generate bad syntax if no undropped columns */
1369  if (first && !is_returning)
1370  appendStringInfoString(buf, "NULL");
1371 }
#define NIL
Definition: pg_list.h:65
#define RelationGetDescr(relation)
Definition: rel.h:503
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:108
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
List * lappend_int(List *list, int datum)
Definition: list.c:354
int i
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2369

◆ deparseTruncateSql()

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

Definition at line 2334 of file deparse.c.

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

Referenced by postgresExecForeignTruncate().

2338 {
2339  ListCell *cell;
2340 
2341  appendStringInfoString(buf, "TRUNCATE ");
2342 
2343  foreach(cell, rels)
2344  {
2345  Relation rel = lfirst(cell);
2346 
2347  if (cell != list_head(rels))
2348  appendStringInfoString(buf, ", ");
2349 
2350  deparseRelation(buf, rel);
2351  }
2352 
2353  appendStringInfo(buf, " %s IDENTITY",
2354  restart_seqs ? "RESTART" : "CONTINUE");
2355 
2356  if (behavior == DROP_RESTRICT)
2357  appendStringInfoString(buf, " RESTRICT");
2358  else if (behavior == DROP_CASCADE)
2359  appendStringInfoString(buf, " CASCADE");
2360 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
#define lfirst(lc)
Definition: pg_list.h:169

◆ deparseUpdateSql()

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

Definition at line 1974 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1979 {
1980  TupleDesc tupdesc = RelationGetDescr(rel);
1981  AttrNumber pindex;
1982  bool first;
1983  ListCell *lc;
1984 
1985  appendStringInfoString(buf, "UPDATE ");
1986  deparseRelation(buf, rel);
1987  appendStringInfoString(buf, " SET ");
1988 
1989  pindex = 2; /* ctid is always the first param */
1990  first = true;
1991  foreach(lc, targetAttrs)
1992  {
1993  int attnum = lfirst_int(lc);
1994  Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
1995 
1996  if (!first)
1997  appendStringInfoString(buf, ", ");
1998  first = false;
1999 
2000  deparseColumnRef(buf, rtindex, attnum, rte, false);
2001  if (attr->attgenerated)
2002  appendStringInfoString(buf, " = DEFAULT");
2003  else
2004  {
2005  appendStringInfo(buf, " = $%d", pindex);
2006  pindex++;
2007  }
2008  }
2009  appendStringInfoString(buf, " WHERE ctid = $1");
2010 
2011  deparseReturningList(buf, rte, rtindex, rel,
2012  rel->trigdesc && rel->trigdesc->trig_update_after_row,
2013  withCheckOptionList, returningList, retrieved_attrs);
2014 }
#define RelationGetDescr(relation)
Definition: rel.h:503
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
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:2199
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define lfirst_int(lc)
Definition: pg_list.h:170
TriggerDesc * trigdesc
Definition: rel.h:115
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
bool trig_update_after_row
Definition: reltrigger.h:62
int16 attnum
Definition: pg_attribute.h:83
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2497
int16 AttrNumber
Definition: attnum.h:21
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2369

◆ deparseVar()

static void deparseVar ( Var node,
deparse_expr_cxt context 
)
static

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

2638 {
2639  Relids relids = context->scanrel->relids;
2640  int relno;
2641  int colno;
2642 
2643  /* Qualify columns when multiple relations are involved. */
2644  bool qualify_col = (bms_membership(relids) == BMS_MULTIPLE);
2645 
2646  /*
2647  * If the Var belongs to the foreign relation that is deparsed as a
2648  * subquery, use the relation and column alias to the Var provided by the
2649  * subquery, instead of the remote name.
2650  */
2651  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2652  {
2653  appendStringInfo(context->buf, "%s%d.%s%d",
2655  SUBQUERY_COL_ALIAS_PREFIX, colno);
2656  return;
2657  }
2658 
2659  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2660  deparseColumnRef(context->buf, node->varno, node->varattno,
2661  planner_rt_fetch(node->varno, context->root),
2662  qualify_col);
2663  else
2664  {
2665  /* Treat like a Param */
2666  if (context->params_list)
2667  {
2668  int pindex = 0;
2669  ListCell *lc;
2670 
2671  /* find its index in params_list */
2672  foreach(lc, *context->params_list)
2673  {
2674  pindex++;
2675  if (equal(node, (Node *) lfirst(lc)))
2676  break;
2677  }
2678  if (lc == NULL)
2679  {
2680  /* not in list, so add it */
2681  pindex++;
2682  *context->params_list = lappend(*context->params_list, node);
2683  }
2684 
2685  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2686  }
2687  else
2688  {
2689  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2690  }
2691  }
2692 }
int varno
Definition: primnodes.h:189
Index varlevelsup
Definition: primnodes.h:196
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3149
List ** params_list
Definition: deparse.c:103
Definition: nodes.h:536
AttrNumber varattno
Definition: primnodes.h:191
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3397
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:111
Oid vartype
Definition: primnodes.h:193
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:110
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
Relids relids
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:336
StringInfo buf
Definition: deparse.c:102
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3635
PlannerInfo * root
Definition: deparse.c:97
#define lfirst(lc)
Definition: pg_list.h:169
RelOptInfo * scanrel
Definition: deparse.c:99
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col)
Definition: deparse.c:2369
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:3423
int32 vartypmod
Definition: primnodes.h:194

◆ foreign_expr_walker()

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

Definition at line 301 of file deparse.c.

References Aggref::aggcollid, Aggref::aggfilter, Aggref::aggfnoid, Aggref::aggorder, Aggref::aggsplit, AGGSPLIT_SIMPLE, RelabelType::arg, CaseExpr::arg, NullTest::arg, Aggref::args, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, CaseExpr::args, ArrayExpr::array_collid, bms_is_member(), CaseExpr::casecollid, foreign_loc_cxt::collation, CaseTestExpr::collation, Const::constcollid, CaseExpr::defresult, ArrayExpr::elements, CaseWhen::expr, 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, lfirst_node, linitial, list_length(), lookup_type_cache(), TypeCacheEntry::lt_opr, nodeTag, OidIsValid, OpExpr::opcollid, OpExpr::opno, ScalarArrayOpExpr::opno, PARAM_MULTIEXPR, Param::paramcollid, Param::paramkind, SubscriptingRef::refassgnexpr, SubscriptingRef::refcollid, SubscriptingRef::refexpr, SubscriptingRef::reflowerindexpr, SubscriptingRef::refupperindexpr, foreign_glob_cxt::relids, CaseWhen::result, RelabelType::resultcollid, SelfItemPointerAttributeNumber, SortGroupClause::sortop, foreign_loc_cxt::state, strip_implicit_coercions(), T_Aggref, T_ArrayExpr, T_BoolExpr, T_CaseExpr, T_CaseTestExpr, 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().

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

◆ get_jointype_name()

const char* get_jointype_name ( JoinType  jointype)

Definition at line 1502 of file deparse.c.

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

Referenced by deparseFromExprForRel(), and foreign_join_ok().

1503 {
1504  switch (jointype)
1505  {
1506  case JOIN_INNER:
1507  return "INNER";
1508 
1509  case JOIN_LEFT:
1510  return "LEFT";
1511 
1512  case JOIN_RIGHT:
1513  return "RIGHT";
1514 
1515  case JOIN_FULL:
1516  return "FULL";
1517 
1518  default:
1519  /* Shouldn't come here, but protect from buggy code. */
1520  elog(ERROR, "unsupported join type %d", jointype);
1521  }
1522 
1523  /* Keep compiler happy */
1524  return NULL;
1525 }
#define ERROR
Definition: elog.h:46
#define elog(elevel,...)
Definition: elog.h:232

◆ get_relation_column_alias_ids()

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

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

3699 {
3700  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3701  int i;
3702  ListCell *lc;
3703 
3704  /* Get the relation alias ID */
3705  *relno = fpinfo->relation_index;
3706 
3707  /* Get the column alias ID */
3708  i = 1;
3709  foreach(lc, foreignrel->reltarget->exprs)
3710  {
3711  if (equal(lfirst(lc), (Node *) node))
3712  {
3713  *colno = i;
3714  return;
3715  }
3716  i++;
3717  }
3718 
3719  /* Shouldn't get here */
3720  elog(ERROR, "unexpected expression in subquery output");
3721 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3149
Definition: nodes.h:536
#define ERROR
Definition: elog.h:46
List * exprs
Definition: pathnodes.h:1110
void * fdw_private
Definition: pathnodes.h:737
#define lfirst(lc)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:232
int i
struct PathTarget * reltarget
Definition: pathnodes.h:692

◆ is_foreign_expr()