PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
deparse.c File Reference
#include "postgres.h"
#include "postgres_fdw.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.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/clauses.h"
#include "optimizer/prep.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parsetree.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for deparse.c:

Go to the source code of this file.

Data Structures

struct  foreign_glob_cxt
 
struct  foreign_loc_cxt
 
struct  deparse_expr_cxt
 

Macros

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

Typedefs

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

Enumerations

enum  FDWCollateState { FDW_COLLATE_NONE, FDW_COLLATE_SAFE, FDW_COLLATE_UNSAFE }
 

Functions

static bool foreign_expr_walker (Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt)
 
static char * deparse_type_name (Oid type_oid, int32 typemod)
 
static void deparseTargetList (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
 
static void deparseExplicitTargetList (List *tlist, List **retrieved_attrs, deparse_expr_cxt *context)
 
static void deparseSubqueryTargetList (deparse_expr_cxt *context)
 
static void deparseReturningList (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
 
static void deparseColumnRef (StringInfo buf, int varno, int varattno, PlannerInfo *root, 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 deparseArrayRef (ArrayRef *node, deparse_expr_cxt *context)
 
static void deparseFuncExpr (FuncExpr *node, deparse_expr_cxt *context)
 
static void deparseOpExpr (OpExpr *node, deparse_expr_cxt *context)
 
static void deparseOperatorName (StringInfo buf, Form_pg_operator opform)
 
static void deparseDistinctExpr (DistinctExpr *node, deparse_expr_cxt *context)
 
static void deparseScalarArrayOpExpr (ScalarArrayOpExpr *node, deparse_expr_cxt *context)
 
static void deparseRelabelType (RelabelType *node, deparse_expr_cxt *context)
 
static void deparseBoolExpr (BoolExpr *node, deparse_expr_cxt *context)
 
static void deparseNullTest (NullTest *node, deparse_expr_cxt *context)
 
static void deparseArrayExpr (ArrayExpr *node, deparse_expr_cxt *context)
 
static void printRemoteParam (int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
 
static void printRemotePlaceholder (Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
 
static void deparseSelectSql (List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
 
static void deparseLockingClause (deparse_expr_cxt *context)
 
static void appendOrderByClause (List *pathkeys, deparse_expr_cxt *context)
 
static void appendConditions (List *exprs, deparse_expr_cxt *context)
 
static void deparseFromExprForRel (StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list)
 
static void deparseFromExpr (List *quals, deparse_expr_cxt *context)
 
static void deparseRangeTblRef (StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, 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, 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)
 
Listbuild_tlist_to_deparse (RelOptInfo *foreignrel)
 
void deparseSelectStmtForRel (StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, bool is_subquery, List **retrieved_attrs, List **params_list)
 
const char * get_jointype_name (JoinType jointype)
 
void deparseInsertSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *targetAttrs, bool doNothing, List *returningList, List **retrieved_attrs)
 
void deparseUpdateSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *targetAttrs, List *returningList, List **retrieved_attrs)
 
void deparseDirectUpdateSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *targetlist, List *targetAttrs, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
 
void deparseDeleteSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs)
 
void deparseDirectDeleteSql (StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
 
void deparseAnalyzeSizeSql (StringInfo buf, Relation rel)
 
void deparseAnalyzeSql (StringInfo buf, Relation rel, List **retrieved_attrs)
 
void deparseStringLiteral (StringInfo buf, const char *val)
 

Macro Definition Documentation

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

Definition at line 110 of file deparse.c.

Referenced by deparseColumnRef(), and deparseTargetList().

#define REL_ALIAS_PREFIX   "r"

Definition at line 108 of file deparse.c.

Referenced by deparseFromExprForRel(), and deparseLockingClause().

#define SUBQUERY_COL_ALIAS_PREFIX   "c"

Definition at line 113 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

#define SUBQUERY_REL_ALIAS_PREFIX   "s"

Definition at line 112 of file deparse.c.

Referenced by deparseRangeTblRef(), and deparseVar().

Typedef Documentation

Enumeration Type Documentation

Enumerator
FDW_COLLATE_NONE 
FDW_COLLATE_SAFE 
FDW_COLLATE_UNSAFE 

Definition at line 78 of file deparse.c.

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

Function Documentation

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

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

2839 {
2840  StringInfo buf = context->buf;
2841  ListCell *lc;
2842  bool first = true;
2843 
2844  foreach(lc, orderList)
2845  {
2846  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
2847  Node *sortexpr;
2848  Oid sortcoltype;
2849  TypeCacheEntry *typentry;
2850 
2851  if (!first)
2852  appendStringInfoString(buf, ", ");
2853  first = false;
2854 
2855  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
2856  context);
2857  sortcoltype = exprType(sortexpr);
2858  /* See whether operator is default < or > for datatype */
2859  typentry = lookup_type_cache(sortcoltype,
2861  if (srt->sortop == typentry->lt_opr)
2862  appendStringInfoString(buf, " ASC");
2863  else if (srt->sortop == typentry->gt_opr)
2864  appendStringInfoString(buf, " DESC");
2865  else
2866  {
2867  HeapTuple opertup;
2868  Form_pg_operator operform;
2869 
2870  appendStringInfoString(buf, " USING ");
2871 
2872  /* Append operator name. */
2873  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
2874  if (!HeapTupleIsValid(opertup))
2875  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
2876  operform = (Form_pg_operator) GETSTRUCT(opertup);
2877  deparseOperatorName(buf, operform);
2878  ReleaseSysCache(opertup);
2879  }
2880 
2881  if (srt->nulls_first)
2882  appendStringInfoString(buf, " NULLS FIRST");
2883  else
2884  appendStringInfoString(buf, " NULLS LAST");
2885  }
2886 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Index tleSortGroupRef
Definition: parsenodes.h:1156
Definition: nodes.h:509
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2545
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
#define TYPECACHE_GT_OPR
Definition: typcache.h:112
static Node * deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3050
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
FormData_pg_operator* Form_pg_operator
Definition: pg_operator.h:57
#define TYPECACHE_LT_OPR
Definition: typcache.h:111
#define elog
Definition: elog.h:219
static void appendConditions ( List exprs,
deparse_expr_cxt context 
)
static

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

1278 {
1279  int nestlevel;
1280  ListCell *lc;
1281  bool is_first = true;
1282  StringInfo buf = context->buf;
1283 
1284  /* Make sure any constants in the exprs are printed portably */
1285  nestlevel = set_transmission_modes();
1286 
1287  foreach(lc, exprs)
1288  {
1289  Expr *expr = (Expr *) lfirst(lc);
1290 
1291  /* Extract clause from RestrictInfo, if required */
1292  if (IsA(expr, RestrictInfo))
1293  expr = ((RestrictInfo *) expr)->clause;
1294 
1295  /* Connect expressions with "AND" and parenthesize each condition. */
1296  if (!is_first)
1297  appendStringInfoString(buf, " AND ");
1298 
1299  appendStringInfoChar(buf, '(');
1300  deparseExpr(expr, context);
1301  appendStringInfoChar(buf, ')');
1302 
1303  is_first = false;
1304  }
1305 
1306  reset_transmission_modes(nestlevel);
1307 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
#define lfirst(lc)
Definition: pg_list.h:106
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
static void appendFunctionName ( Oid  funcid,
deparse_expr_cxt context 
)
static

Definition at line 3015 of file deparse.c.

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

Referenced by deparseAggref(), and deparseFuncExpr().

3016 {
3017  StringInfo buf = context->buf;
3018  HeapTuple proctup;
3019  Form_pg_proc procform;
3020  const char *proname;
3021 
3022  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3023  if (!HeapTupleIsValid(proctup))
3024  elog(ERROR, "cache lookup failed for function %u", funcid);
3025  procform = (Form_pg_proc) GETSTRUCT(proctup);
3026 
3027  /* Print schema name only if it's not pg_catalog */
3028  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3029  {
3030  const char *schemaname;
3031 
3032  schemaname = get_namespace_name(procform->pronamespace);
3033  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3034  }
3035 
3036  /* Always print the function name */
3037  proname = NameStr(procform->proname);
3038  appendStringInfo(buf, "%s", quote_identifier(proname));
3039 
3040  ReleaseSysCache(proctup);
3041 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10246
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
static char * buf
Definition: pg_test_fsync.c:66
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
static void appendGroupByClause ( List tlist,
deparse_expr_cxt context 
)
static

Definition at line 2936 of file deparse.c.

References appendStringInfo(), 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().

2937 {
2938  StringInfo buf = context->buf;
2939  Query *query = context->root->parse;
2940  ListCell *lc;
2941  bool first = true;
2942 
2943  /* Nothing to be done, if there's no GROUP BY clause in the query. */
2944  if (!query->groupClause)
2945  return;
2946 
2947  appendStringInfo(buf, " GROUP BY ");
2948 
2949  /*
2950  * Queries with grouping sets are not pushed down, so we don't expect
2951  * grouping sets here.
2952  */
2953  Assert(!query->groupingSets);
2954 
2955  foreach(lc, query->groupClause)
2956  {
2957  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
2958 
2959  if (!first)
2960  appendStringInfoString(buf, ", ");
2961  first = false;
2962 
2963  deparseSortGroupClause(grp->tleSortGroupRef, tlist, context);
2964  }
2965 }
Query * parse
Definition: relation.h:154
Index tleSortGroupRef
Definition: parsenodes.h:1156
List * groupingSets
Definition: parsenodes.h:148
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
static Node * deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3050
StringInfo buf
Definition: deparse.c:104
PlannerInfo * root
Definition: deparse.c:99
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * groupClause
Definition: parsenodes.h:146
static void appendOrderByClause ( List pathkeys,
deparse_expr_cxt context 
)
static

Definition at line 2973 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), Assert, BTLessStrategyNumber, buf, deparse_expr_cxt::buf, deparseExpr(), find_em_expr_for_rel(), lfirst, NULL, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_strategy, reset_transmission_modes(), deparse_expr_cxt::scanrel, and set_transmission_modes().

Referenced by deparseSelectStmtForRel().

2974 {
2975  ListCell *lcell;
2976  int nestlevel;
2977  char *delim = " ";
2978  RelOptInfo *baserel = context->scanrel;
2979  StringInfo buf = context->buf;
2980 
2981  /* Make sure any constants in the exprs are printed portably */
2982  nestlevel = set_transmission_modes();
2983 
2984  appendStringInfo(buf, " ORDER BY");
2985  foreach(lcell, pathkeys)
2986  {
2987  PathKey *pathkey = lfirst(lcell);
2988  Expr *em_expr;
2989 
2990  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
2991  Assert(em_expr != NULL);
2992 
2993  appendStringInfoString(buf, delim);
2994  deparseExpr(em_expr, context);
2995  if (pathkey->pk_strategy == BTLessStrategyNumber)
2996  appendStringInfoString(buf, " ASC");
2997  else
2998  appendStringInfoString(buf, " DESC");
2999 
3000  if (pathkey->pk_nulls_first)
3001  appendStringInfoString(buf, " NULLS FIRST");
3002  else
3003  appendStringInfoString(buf, " NULLS LAST");
3004 
3005  delim = ", ";
3006  }
3007  reset_transmission_modes(nestlevel);
3008 }
int set_transmission_modes(void)
int pk_strategy
Definition: relation.h:851
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
bool pk_nulls_first
Definition: relation.h:852
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
StringInfo buf
Definition: deparse.c:104
#define NULL
Definition: c.h:229
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:849
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
RelOptInfo * scanrel
Definition: deparse.c:101
#define BTLessStrategyNumber
Definition: stratnum.h:29
List* build_tlist_to_deparse ( RelOptInfo foreignrel)

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

869 {
870  List *tlist = NIL;
871  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
872  ListCell *lc;
873 
874  /*
875  * For an upper relation, we have already built the target list while
876  * checking shippability, so just return that.
877  */
878  if (IS_UPPER_REL(foreignrel))
879  return fpinfo->grouped_tlist;
880 
881  /*
882  * We require columns specified in foreignrel->reltarget->exprs and those
883  * required for evaluating the local conditions.
884  */
885  tlist = add_to_flat_tlist(tlist,
886  pull_var_clause((Node *) foreignrel->reltarget->exprs,
888  foreach(lc, fpinfo->local_conds)
889  {
890  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
891 
892  tlist = add_to_flat_tlist(tlist,
893  pull_var_clause((Node *) rinfo->clause,
895  }
896 
897  return tlist;
898 }
#define NIL
Definition: pg_list.h:69
#define IS_UPPER_REL(rel)
Definition: relation.h:512
Definition: nodes.h:509
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
#define PVC_RECURSE_PLACEHOLDERS
Definition: var.h:26
#define lfirst_node(type, lc)
Definition: pg_list.h:109
Expr * clause
Definition: relation.h:1746
List * exprs
Definition: relation.h:882
void * fdw_private
Definition: relation.h:576
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:135
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:535
void classifyConditions ( PlannerInfo root,
RelOptInfo baserel,
List input_conds,
List **  remote_conds,
List **  local_conds 
)

Definition at line 200 of file deparse.c.

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

Referenced by estimate_path_cost_size(), and postgresGetForeignRelSize().

205 {
206  ListCell *lc;
207 
208  *remote_conds = NIL;
209  *local_conds = NIL;
210 
211  foreach(lc, input_conds)
212  {
214 
215  if (is_foreign_expr(root, baserel, ri->clause))
216  *remote_conds = lappend(*remote_conds, ri);
217  else
218  *local_conds = lappend(*local_conds, ri);
219  }
220 }
#define NIL
Definition: pg_list.h:69
#define lfirst_node(type, lc)
Definition: pg_list.h:109
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1746
bool is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:226
static char * deparse_type_name ( Oid  type_oid,
int32  typemod 
)
static

Definition at line 851 of file deparse.c.

References format_type_with_typemod(), format_type_with_typemod_qualified(), and is_builtin().

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

852 {
853  if (is_builtin(type_oid))
854  return format_type_with_typemod(type_oid, typemod);
855  else
856  return format_type_with_typemod_qualified(type_oid, typemod);
857 }
bool is_builtin(Oid objectId)
Definition: shippable.c:155
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:113
char * format_type_with_typemod_qualified(Oid type_oid, int32 typemod)
Definition: format_type.c:123
static void deparseAggref ( Aggref node,
deparse_expr_cxt context 
)
static

Definition at line 2746 of file deparse.c.

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

Referenced by deparseExpr().

2747 {
2748  StringInfo buf = context->buf;
2749  bool use_variadic;
2750 
2751  /* Only basic, non-split aggregation accepted. */
2752  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
2753 
2754  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2755  use_variadic = node->aggvariadic;
2756 
2757  /* Find aggregate name from aggfnoid which is a pg_proc entry */
2758  appendFunctionName(node->aggfnoid, context);
2759  appendStringInfoChar(buf, '(');
2760 
2761  /* Add DISTINCT */
2762  appendStringInfo(buf, "%s", (node->aggdistinct != NIL) ? "DISTINCT " : "");
2763 
2764  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
2765  {
2766  /* Add WITHIN GROUP (ORDER BY ..) */
2767  ListCell *arg;
2768  bool first = true;
2769 
2770  Assert(!node->aggvariadic);
2771  Assert(node->aggorder != NIL);
2772 
2773  foreach(arg, node->aggdirectargs)
2774  {
2775  if (!first)
2776  appendStringInfoString(buf, ", ");
2777  first = false;
2778 
2779  deparseExpr((Expr *) lfirst(arg), context);
2780  }
2781 
2782  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
2783  appendAggOrderBy(node->aggorder, node->args, context);
2784  }
2785  else
2786  {
2787  /* aggstar can be set only in zero-argument aggregates */
2788  if (node->aggstar)
2789  appendStringInfoChar(buf, '*');
2790  else
2791  {
2792  ListCell *arg;
2793  bool first = true;
2794 
2795  /* Add all the arguments */
2796  foreach(arg, node->args)
2797  {
2798  TargetEntry *tle = (TargetEntry *) lfirst(arg);
2799  Node *n = (Node *) tle->expr;
2800 
2801  if (tle->resjunk)
2802  continue;
2803 
2804  if (!first)
2805  appendStringInfoString(buf, ", ");
2806  first = false;
2807 
2808  /* Add VARIADIC */
2809  if (use_variadic && lnext(arg) == NULL)
2810  appendStringInfoString(buf, "VARIADIC ");
2811 
2812  deparseExpr((Expr *) n, context);
2813  }
2814  }
2815 
2816  /* Add ORDER BY */
2817  if (node->aggorder != NIL)
2818  {
2819  appendStringInfoString(buf, " ORDER BY ");
2820  appendAggOrderBy(node->aggorder, node->args, context);
2821  }
2822  }
2823 
2824  /* Add FILTER (WHERE ..) */
2825  if (node->aggfilter != NULL)
2826  {
2827  appendStringInfoString(buf, ") FILTER (WHERE ");
2828  deparseExpr((Expr *) node->aggfilter, context);
2829  }
2830 
2831  appendStringInfoChar(buf, ')');
2832 }
List * aggdistinct
Definition: primnodes.h:303
#define NIL
Definition: pg_list.h:69
bool aggvariadic
Definition: primnodes.h:306
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3015
Definition: nodes.h:509
List * args
Definition: primnodes.h:301
bool aggstar
Definition: primnodes.h:305
#define AGGKIND_IS_ORDERED_SET(kind)
Definition: pg_aggregate.h:129
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
bool resjunk
Definition: primnodes.h:1374
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
List * aggorder
Definition: primnodes.h:302
List * aggdirectargs
Definition: primnodes.h:300
#define lnext(lc)
Definition: pg_list.h:105
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
Definition: deparse.c:2838
StringInfo buf
Definition: deparse.c:104
Oid aggfnoid
Definition: primnodes.h:294
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1367
AggSplit aggsplit
Definition: primnodes.h:310
Expr * aggfilter
Definition: primnodes.h:304
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
void * arg
char aggkind
Definition: primnodes.h:308
void deparseAnalyzeSizeSql ( StringInfo  buf,
Relation  rel 
)

Definition at line 1818 of file deparse.c.

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

Referenced by postgresAnalyzeForeignTable().

1819 {
1820  StringInfoData relname;
1821 
1822  /* We'll need the remote relation name as a literal. */
1823  initStringInfo(&relname);
1824  deparseRelation(&relname, rel);
1825 
1826  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
1827  deparseStringLiteral(buf, relname.data);
1828  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
1829 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2084
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
void deparseAnalyzeSql ( StringInfo  buf,
Relation  rel,
List **  retrieved_attrs 
)

Definition at line 1838 of file deparse.c.

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

Referenced by postgresAcquireSampleRowsFunc().

1839 {
1840  Oid relid = RelationGetRelid(rel);
1841  TupleDesc tupdesc = RelationGetDescr(rel);
1842  int i;
1843  char *colname;
1844  List *options;
1845  ListCell *lc;
1846  bool first = true;
1847 
1848  *retrieved_attrs = NIL;
1849 
1850  appendStringInfoString(buf, "SELECT ");
1851  for (i = 0; i < tupdesc->natts; i++)
1852  {
1853  /* Ignore dropped columns. */
1854  if (tupdesc->attrs[i]->attisdropped)
1855  continue;
1856 
1857  if (!first)
1858  appendStringInfoString(buf, ", ");
1859  first = false;
1860 
1861  /* Use attribute name or column_name option. */
1862  colname = NameStr(tupdesc->attrs[i]->attname);
1863  options = GetForeignColumnOptions(relid, i + 1);
1864 
1865  foreach(lc, options)
1866  {
1867  DefElem *def = (DefElem *) lfirst(lc);
1868 
1869  if (strcmp(def->defname, "column_name") == 0)
1870  {
1871  colname = defGetString(def);
1872  break;
1873  }
1874  }
1875 
1877 
1878  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1879  }
1880 
1881  /* Don't generate bad syntax for zero-column relation. */
1882  if (first)
1883  appendStringInfoString(buf, "NULL");
1884 
1885  /*
1886  * Construct FROM clause
1887  */
1888  appendStringInfoString(buf, " FROM ");
1889  deparseRelation(buf, rel);
1890 }
#define NIL
Definition: pg_list.h:69
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10246
#define RelationGetDescr(relation)
Definition: rel.h:429
Form_pg_attribute * attrs
Definition: tupdesc.h:74
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:254
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
List * lappend_int(List *list, int datum)
Definition: list.c:146
static char ** options
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define NameStr(name)
Definition: c.h:499
char * defname
Definition: parsenodes.h:719
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:417
static void deparseArrayExpr ( ArrayExpr node,
deparse_expr_cxt context 
)
static

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

2721 {
2722  StringInfo buf = context->buf;
2723  bool first = true;
2724  ListCell *lc;
2725 
2726  appendStringInfoString(buf, "ARRAY[");
2727  foreach(lc, node->elements)
2728  {
2729  if (!first)
2730  appendStringInfoString(buf, ", ");
2731  deparseExpr(lfirst(lc), context);
2732  first = false;
2733  }
2734  appendStringInfoChar(buf, ']');
2735 
2736  /* If the array is empty, we need an explicit cast to the array type. */
2737  if (node->elements == NIL)
2738  appendStringInfo(buf, "::%s",
2739  deparse_type_name(node->array_typeid, -1));
2740 }
#define NIL
Definition: pg_list.h:69
Oid array_typeid
Definition: primnodes.h:951
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
List * elements
Definition: primnodes.h:954
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851
#define lfirst(lc)
Definition: pg_list.h:106
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
static void deparseArrayRef ( ArrayRef node,
deparse_expr_cxt context 
)
static

Definition at line 2385 of file deparse.c.

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

Referenced by deparseExpr().

2386 {
2387  StringInfo buf = context->buf;
2388  ListCell *lowlist_item;
2389  ListCell *uplist_item;
2390 
2391  /* Always parenthesize the expression. */
2392  appendStringInfoChar(buf, '(');
2393 
2394  /*
2395  * Deparse referenced array expression first. If that expression includes
2396  * a cast, we have to parenthesize to prevent the array subscript from
2397  * being taken as typename decoration. We can avoid that in the typical
2398  * case of subscripting a Var, but otherwise do it.
2399  */
2400  if (IsA(node->refexpr, Var))
2401  deparseExpr(node->refexpr, context);
2402  else
2403  {
2404  appendStringInfoChar(buf, '(');
2405  deparseExpr(node->refexpr, context);
2406  appendStringInfoChar(buf, ')');
2407  }
2408 
2409  /* Deparse subscript expressions. */
2410  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2411  foreach(uplist_item, node->refupperindexpr)
2412  {
2413  appendStringInfoChar(buf, '[');
2414  if (lowlist_item)
2415  {
2416  deparseExpr(lfirst(lowlist_item), context);
2417  appendStringInfoChar(buf, ':');
2418  lowlist_item = lnext(lowlist_item);
2419  }
2420  deparseExpr(lfirst(uplist_item), context);
2421  appendStringInfoChar(buf, ']');
2422  }
2423 
2424  appendStringInfoChar(buf, ')');
2425 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: primnodes.h:163
static char * buf
Definition: pg_test_fsync.c:66
List * refupperindexpr
Definition: primnodes.h:403
List * reflowerindexpr
Definition: primnodes.h:405
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
#define lfirst(lc)
Definition: pg_list.h:106
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
Expr * refexpr
Definition: primnodes.h:407
static void deparseBoolExpr ( BoolExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2649 of file deparse.c.

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

Referenced by deparseExpr().

2650 {
2651  StringInfo buf = context->buf;
2652  const char *op = NULL; /* keep compiler quiet */
2653  bool first;
2654  ListCell *lc;
2655 
2656  switch (node->boolop)
2657  {
2658  case AND_EXPR:
2659  op = "AND";
2660  break;
2661  case OR_EXPR:
2662  op = "OR";
2663  break;
2664  case NOT_EXPR:
2665  appendStringInfoString(buf, "(NOT ");
2666  deparseExpr(linitial(node->args), context);
2667  appendStringInfoChar(buf, ')');
2668  return;
2669  }
2670 
2671  appendStringInfoChar(buf, '(');
2672  first = true;
2673  foreach(lc, node->args)
2674  {
2675  if (!first)
2676  appendStringInfo(buf, " %s ", op);
2677  deparseExpr((Expr *) lfirst(lc), context);
2678  first = false;
2679  }
2680  appendStringInfoChar(buf, ')');
2681 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define linitial(l)
Definition: pg_list.h:111
BoolExprType boolop
Definition: primnodes.h:561
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * args
Definition: primnodes.h:562
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
static void deparseColumnRef ( StringInfo  buf,
int  varno,
int  varattno,
PlannerInfo root,
bool  qualify_col 
)
static

Definition at line 1899 of file deparse.c.

References ADD_REL_QUALIFIER, appendStringInfo(), appendStringInfoString(), Assert, bms_add_member(), bms_free(), defGetString(), DefElem::defname, deparseTargetList(), FirstLowInvalidHeapAttributeNumber, get_relid_attribute_name(), GetForeignColumnOptions(), heap_close, heap_open(), IS_SPECIAL_VARNO, lfirst, NoLock, NULL, ObjectIdAttributeNumber, options, planner_rt_fetch, quote_identifier(), RangeTblEntry::relid, SelfItemPointerAttributeNumber, and TableOidAttributeNumber.

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

1901 {
1902  RangeTblEntry *rte;
1903 
1904  /* We support fetching the remote side's CTID and OID. */
1905  if (varattno == SelfItemPointerAttributeNumber)
1906  {
1907  if (qualify_col)
1908  ADD_REL_QUALIFIER(buf, varno);
1909  appendStringInfoString(buf, "ctid");
1910  }
1911  else if (varattno == ObjectIdAttributeNumber)
1912  {
1913  if (qualify_col)
1914  ADD_REL_QUALIFIER(buf, varno);
1915  appendStringInfoString(buf, "oid");
1916  }
1917  else if (varattno < 0)
1918  {
1919  /*
1920  * All other system attributes are fetched as 0, except for table OID,
1921  * which is fetched as the local table OID. However, we must be
1922  * careful; the table could be beneath an outer join, in which case it
1923  * must go to NULL whenever the rest of the row does.
1924  */
1925  Oid fetchval = 0;
1926 
1927  if (varattno == TableOidAttributeNumber)
1928  {
1929  rte = planner_rt_fetch(varno, root);
1930  fetchval = rte->relid;
1931  }
1932 
1933  if (qualify_col)
1934  {
1935  appendStringInfoString(buf, "CASE WHEN (");
1936  ADD_REL_QUALIFIER(buf, varno);
1937  appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval);
1938  }
1939  else
1940  appendStringInfo(buf, "%u", fetchval);
1941  }
1942  else if (varattno == 0)
1943  {
1944  /* Whole row reference */
1945  Relation rel;
1946  Bitmapset *attrs_used;
1947 
1948  /* Required only to be passed down to deparseTargetList(). */
1949  List *retrieved_attrs;
1950 
1951  /* Get RangeTblEntry from array in PlannerInfo. */
1952  rte = planner_rt_fetch(varno, root);
1953 
1954  /*
1955  * The lock on the relation will be held by upper callers, so it's
1956  * fine to open it with no lock here.
1957  */
1958  rel = heap_open(rte->relid, NoLock);
1959 
1960  /*
1961  * The local name of the foreign table can not be recognized by the
1962  * foreign server and the table it references on foreign server might
1963  * have different column ordering or different columns than those
1964  * declared locally. Hence we have to deparse whole-row reference as
1965  * ROW(columns referenced locally). Construct this by deparsing a
1966  * "whole row" attribute.
1967  */
1968  attrs_used = bms_add_member(NULL,
1970 
1971  /*
1972  * In case the whole-row reference is under an outer join then it has
1973  * to go NULL whenever the rest of the row goes NULL. Deparsing a join
1974  * query would always involve multiple relations, thus qualify_col
1975  * would be true.
1976  */
1977  if (qualify_col)
1978  {
1979  appendStringInfoString(buf, "CASE WHEN (");
1980  ADD_REL_QUALIFIER(buf, varno);
1981  appendStringInfo(buf, "*)::text IS NOT NULL THEN ");
1982  }
1983 
1984  appendStringInfoString(buf, "ROW(");
1985  deparseTargetList(buf, root, varno, rel, false, attrs_used, qualify_col,
1986  &retrieved_attrs);
1987  appendStringInfoString(buf, ")");
1988 
1989  /* Complete the CASE WHEN statement started above. */
1990  if (qualify_col)
1991  appendStringInfo(buf, " END");
1992 
1993  heap_close(rel, NoLock);
1994  bms_free(attrs_used);
1995  }
1996  else
1997  {
1998  char *colname = NULL;
1999  List *options;
2000  ListCell *lc;
2001 
2002  /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
2003  Assert(!IS_SPECIAL_VARNO(varno));
2004 
2005  /* Get RangeTblEntry from array in PlannerInfo. */
2006  rte = planner_rt_fetch(varno, root);
2007 
2008  /*
2009  * If it's a column of a foreign table, and it has the column_name FDW
2010  * option, use that value.
2011  */
2012  options = GetForeignColumnOptions(rte->relid, varattno);
2013  foreach(lc, options)
2014  {
2015  DefElem *def = (DefElem *) lfirst(lc);
2016 
2017  if (strcmp(def->defname, "column_name") == 0)
2018  {
2019  colname = defGetString(def);
2020  break;
2021  }
2022  }
2023 
2024  /*
2025  * If it's a column of a regular table or it doesn't have column_name
2026  * FDW option, use attribute name.
2027  */
2028  if (colname == NULL)
2029  colname = get_relid_attribute_name(rte->relid, varattno);
2030 
2031  if (qualify_col)
2032  ADD_REL_QUALIFIER(buf, varno);
2033 
2035  }
2036 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10246
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
#define heap_close(r, l)
Definition: heapam.h:97
static void deparseTargetList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1095
unsigned int Oid
Definition: postgres_ext.h:31
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:254
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:157
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:110
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define NoLock
Definition: lockdefs.h:34
#define TableOidAttributeNumber
Definition: sysattr.h:27
static char ** options
char * get_relid_attribute_name(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:801
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
char * defname
Definition: parsenodes.h:719
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
Definition: pg_list.h:45
static void deparseConst ( Const node,
deparse_expr_cxt context,
int  showtype 
)
static

Definition at line 2246 of file deparse.c.

References appendStringInfo(), appendStringInfoString(), BITOID, BOOLOID, buf, deparse_expr_cxt::buf, Const::constisnull, Const::consttype, Const::consttypmod, Const::constvalue, deparse_type_name(), deparseStringLiteral(), FLOAT4OID, FLOAT8OID, getTypeOutputInfo(), INT2OID, INT4OID, INT8OID, NUMERICOID, OIDOID, OidOutputFunctionCall(), pfree(), UNKNOWNOID, and VARBITOID.

Referenced by deparseExpr(), and deparseSortGroupClause().

2247 {
2248  StringInfo buf = context->buf;
2249  Oid typoutput;
2250  bool typIsVarlena;
2251  char *extval;
2252  bool isfloat = false;
2253  bool needlabel;
2254 
2255  if (node->constisnull)
2256  {
2257  appendStringInfoString(buf, "NULL");
2258  if (showtype >= 0)
2259  appendStringInfo(buf, "::%s",
2261  node->consttypmod));
2262  return;
2263  }
2264 
2266  &typoutput, &typIsVarlena);
2267  extval = OidOutputFunctionCall(typoutput, node->constvalue);
2268 
2269  switch (node->consttype)
2270  {
2271  case INT2OID:
2272  case INT4OID:
2273  case INT8OID:
2274  case OIDOID:
2275  case FLOAT4OID:
2276  case FLOAT8OID:
2277  case NUMERICOID:
2278  {
2279  /*
2280  * No need to quote unless it's a special value such as 'NaN'.
2281  * See comments in get_const_expr().
2282  */
2283  if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2284  {
2285  if (extval[0] == '+' || extval[0] == '-')
2286  appendStringInfo(buf, "(%s)", extval);
2287  else
2288  appendStringInfoString(buf, extval);
2289  if (strcspn(extval, "eE.") != strlen(extval))
2290  isfloat = true; /* it looks like a float */
2291  }
2292  else
2293  appendStringInfo(buf, "'%s'", extval);
2294  }
2295  break;
2296  case BITOID:
2297  case VARBITOID:
2298  appendStringInfo(buf, "B'%s'", extval);
2299  break;
2300  case BOOLOID:
2301  if (strcmp(extval, "t") == 0)
2302  appendStringInfoString(buf, "true");
2303  else
2304  appendStringInfoString(buf, "false");
2305  break;
2306  default:
2307  deparseStringLiteral(buf, extval);
2308  break;
2309  }
2310 
2311  pfree(extval);
2312 
2313  if (showtype < 0)
2314  return;
2315 
2316  /*
2317  * For showtype == 0, append ::typename unless the constant will be
2318  * implicitly typed as the right type when it is read in.
2319  *
2320  * XXX this code has to be kept in sync with the behavior of the parser,
2321  * especially make_const.
2322  */
2323  switch (node->consttype)
2324  {
2325  case BOOLOID:
2326  case INT4OID:
2327  case UNKNOWNOID:
2328  needlabel = false;
2329  break;
2330  case NUMERICOID:
2331  needlabel = !isfloat || (node->consttypmod >= 0);
2332  break;
2333  default:
2334  needlabel = true;
2335  break;
2336  }
2337  if (needlabel || showtype > 0)
2338  appendStringInfo(buf, "::%s",
2340  node->consttypmod));
2341 }
Datum constvalue
Definition: primnodes.h:196
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define OIDOID
Definition: pg_type.h:328
#define NUMERICOID
Definition: pg_type.h:554
#define INT4OID
Definition: pg_type.h:316
#define VARBITOID
Definition: pg_type.h:546
unsigned int Oid
Definition: postgres_ext.h:31
Oid consttype
Definition: primnodes.h:192
void pfree(void *pointer)
Definition: mcxt.c:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define INT2OID
Definition: pg_type.h:308
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
StringInfo buf
Definition: deparse.c:104
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2084
#define FLOAT4OID
Definition: pg_type.h:416
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851
#define INT8OID
Definition: pg_type.h:304
#define BITOID
Definition: pg_type.h:542
#define FLOAT8OID
Definition: pg_type.h:419
#define BOOLOID
Definition: pg_type.h:288
#define UNKNOWNOID
Definition: pg_type.h:431
int32 consttypmod
Definition: primnodes.h:193
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
bool constisnull
Definition: primnodes.h:197
void deparseDeleteSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1721 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1725 {
1726  appendStringInfoString(buf, "DELETE FROM ");
1727  deparseRelation(buf, rel);
1728  appendStringInfoString(buf, " WHERE ctid = $1");
1729 
1730  deparseReturningList(buf, root, rtindex, rel,
1731  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
1732  returningList, retrieved_attrs);
1733 }
TriggerDesc * trigdesc
Definition: rel.h:120
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
bool trig_delete_after_row
Definition: reltrigger.h:66
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1777
void deparseDirectDeleteSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List remote_conds,
List **  params_list,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1743 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseRelation(), deparseReturningList(), deparse_expr_cxt::foreignrel, deparse_expr_cxt::params_list, deparse_expr_cxt::root, deparse_expr_cxt::scanrel, and PlannerInfo::simple_rel_array.

Referenced by postgresPlanDirectModify().

1749 {
1750  RelOptInfo *baserel = root->simple_rel_array[rtindex];
1751  deparse_expr_cxt context;
1752 
1753  /* Set up context struct for recursion */
1754  context.root = root;
1755  context.foreignrel = baserel;
1756  context.scanrel = baserel;
1757  context.buf = buf;
1758  context.params_list = params_list;
1759 
1760  appendStringInfoString(buf, "DELETE FROM ");
1761  deparseRelation(buf, rel);
1762 
1763  if (remote_conds)
1764  {
1765  appendStringInfo(buf, " WHERE ");
1766  appendConditions(remote_conds, &context);
1767  }
1768 
1769  deparseReturningList(buf, root, rtindex, rel, false,
1770  returningList, retrieved_attrs);
1771 }
List ** params_list
Definition: deparse.c:105
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1277
StringInfo buf
Definition: deparse.c:104
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
PlannerInfo * root
Definition: deparse.c:99
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1777
RelOptInfo * scanrel
Definition: deparse.c:101
RelOptInfo * foreignrel
Definition: deparse.c:100
void deparseDirectUpdateSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List targetlist,
List targetAttrs,
List remote_conds,
List **  params_list,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1653 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseColumnRef(), deparseExpr(), deparseRelation(), deparseReturningList(), elog, ERROR, TargetEntry::expr, deparse_expr_cxt::foreignrel, get_tle_by_resno(), lfirst_int, deparse_expr_cxt::params_list, reset_transmission_modes(), deparse_expr_cxt::root, deparse_expr_cxt::scanrel, set_transmission_modes(), and PlannerInfo::simple_rel_array.

Referenced by postgresPlanDirectModify().

1661 {
1662  RelOptInfo *baserel = root->simple_rel_array[rtindex];
1663  deparse_expr_cxt context;
1664  int nestlevel;
1665  bool first;
1666  ListCell *lc;
1667 
1668  /* Set up context struct for recursion */
1669  context.root = root;
1670  context.foreignrel = baserel;
1671  context.scanrel = baserel;
1672  context.buf = buf;
1673  context.params_list = params_list;
1674 
1675  appendStringInfoString(buf, "UPDATE ");
1676  deparseRelation(buf, rel);
1677  appendStringInfoString(buf, " SET ");
1678 
1679  /* Make sure any constants in the exprs are printed portably */
1680  nestlevel = set_transmission_modes();
1681 
1682  first = true;
1683  foreach(lc, targetAttrs)
1684  {
1685  int attnum = lfirst_int(lc);
1686  TargetEntry *tle = get_tle_by_resno(targetlist, attnum);
1687 
1688  if (!tle)
1689  elog(ERROR, "attribute number %d not found in UPDATE targetlist",
1690  attnum);
1691 
1692  if (!first)
1693  appendStringInfoString(buf, ", ");
1694  first = false;
1695 
1696  deparseColumnRef(buf, rtindex, attnum, root, false);
1697  appendStringInfoString(buf, " = ");
1698  deparseExpr((Expr *) tle->expr, &context);
1699  }
1700 
1701  reset_transmission_modes(nestlevel);
1702 
1703  if (remote_conds)
1704  {
1705  appendStringInfo(buf, " WHERE ");
1706  appendConditions(remote_conds, &context);
1707  }
1708 
1709  deparseReturningList(buf, root, rtindex, rel, false,
1710  returningList, retrieved_attrs);
1711 }
List ** params_list
Definition: deparse.c:105
int set_transmission_modes(void)
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1899
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1277
StringInfo buf
Definition: deparse.c:104
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
PlannerInfo * root
Definition: deparse.c:99
Expr * expr
Definition: primnodes.h:1367
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1777
#define elog
Definition: elog.h:219
RelOptInfo * scanrel
Definition: deparse.c:101
RelOptInfo * foreignrel
Definition: deparse.c:100
static void deparseDistinctExpr ( DistinctExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2573 of file deparse.c.

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

Referenced by deparseExpr().

2574 {
2575  StringInfo buf = context->buf;
2576 
2577  Assert(list_length(node->args) == 2);
2578 
2579  appendStringInfoChar(buf, '(');
2580  deparseExpr(linitial(node->args), context);
2581  appendStringInfoString(buf, " IS DISTINCT FROM ");
2582  deparseExpr(lsecond(node->args), context);
2583  appendStringInfoChar(buf, ')');
2584 }
#define lsecond(l)
Definition: pg_list.h:116
#define linitial(l)
Definition: pg_list.h:111
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
List * args
Definition: primnodes.h:501
static void deparseExplicitTargetList ( List tlist,
List **  retrieved_attrs,
deparse_expr_cxt context 
)
static

Definition at line 1345 of file deparse.c.

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

Referenced by deparseSelectSql().

1347 {
1348  ListCell *lc;
1349  StringInfo buf = context->buf;
1350  int i = 0;
1351 
1352  *retrieved_attrs = NIL;
1353 
1354  foreach(lc, tlist)
1355  {
1356  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1357 
1358  if (i > 0)
1359  appendStringInfoString(buf, ", ");
1360  deparseExpr((Expr *) tle->expr, context);
1361 
1362  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1363  i++;
1364  }
1365 
1366  if (i == 0)
1367  appendStringInfoString(buf, "NULL");
1368 }
#define NIL
Definition: pg_list.h:69
#define lfirst_node(type, lc)
Definition: pg_list.h:109
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
List * lappend_int(List *list, int datum)
Definition: list.c:146
StringInfo buf
Definition: deparse.c:104
Expr * expr
Definition: primnodes.h:1367
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
int i
static void deparseExpr ( Expr expr,
deparse_expr_cxt context 
)
static

Definition at line 2119 of file deparse.c.

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

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

2120 {
2121  if (node == NULL)
2122  return;
2123 
2124  switch (nodeTag(node))
2125  {
2126  case T_Var:
2127  deparseVar((Var *) node, context);
2128  break;
2129  case T_Const:
2130  deparseConst((Const *) node, context, 0);
2131  break;
2132  case T_Param:
2133  deparseParam((Param *) node, context);
2134  break;
2135  case T_ArrayRef:
2136  deparseArrayRef((ArrayRef *) node, context);
2137  break;
2138  case T_FuncExpr:
2139  deparseFuncExpr((FuncExpr *) node, context);
2140  break;
2141  case T_OpExpr:
2142  deparseOpExpr((OpExpr *) node, context);
2143  break;
2144  case T_DistinctExpr:
2145  deparseDistinctExpr((DistinctExpr *) node, context);
2146  break;
2147  case T_ScalarArrayOpExpr:
2148  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2149  break;
2150  case T_RelabelType:
2151  deparseRelabelType((RelabelType *) node, context);
2152  break;
2153  case T_BoolExpr:
2154  deparseBoolExpr((BoolExpr *) node, context);
2155  break;
2156  case T_NullTest:
2157  deparseNullTest((NullTest *) node, context);
2158  break;
2159  case T_ArrayExpr:
2160  deparseArrayExpr((ArrayExpr *) node, context);
2161  break;
2162  case T_Aggref:
2163  deparseAggref((Aggref *) node, context);
2164  break;
2165  default:
2166  elog(ERROR, "unsupported expression type for deparse: %d",
2167  (int) nodeTag(node));
2168  break;
2169  }
2170 }
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:2746
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2649
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:2687
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2573
static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2385
Definition: primnodes.h:163
#define ERROR
Definition: elog.h:43
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2352
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2591
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2494
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2720
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:2636
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2181
Definition: nodes.h:147
Definition: nodes.h:146
#define NULL
Definition: c.h:229
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2246
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define elog
Definition: elog.h:219
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2431
Definition: nodes.h:148
static void deparseFromExpr ( List quals,
deparse_expr_cxt context 
)
static

Definition at line 1061 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), Assert, bms_num_members(), 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().

1062 {
1063  StringInfo buf = context->buf;
1064  RelOptInfo *scanrel = context->scanrel;
1065 
1066  /* For upper relations, scanrel must be either a joinrel or a baserel */
1067  Assert(!IS_UPPER_REL(context->foreignrel) ||
1068  IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1069 
1070  /* Construct FROM clause */
1071  appendStringInfoString(buf, " FROM ");
1072  deparseFromExprForRel(buf, context->root, scanrel,
1073  (bms_num_members(scanrel->relids) > 1),
1074  context->params_list);
1075 
1076  /* Construct WHERE clause */
1077  if (quals != NIL)
1078  {
1079  appendStringInfo(buf, " WHERE ");
1080  appendConditions(quals, context);
1081  }
1082 }
#define NIL
Definition: pg_list.h:69
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list)
Definition: deparse.c:1411
List ** params_list
Definition: deparse.c:105
#define IS_UPPER_REL(rel)
Definition: relation.h:512
#define IS_JOIN_REL(rel)
Definition: relation.h:509
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
Relids relids
Definition: relation.h:524
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1277
StringInfo buf
Definition: deparse.c:104
PlannerInfo * root
Definition: deparse.c:99
#define Assert(condition)
Definition: c.h:675
RelOptInfo * scanrel
Definition: deparse.c:101
RelOptInfo * foreignrel
Definition: deparse.c:100
static void deparseFromExprForRel ( StringInfo  buf,
PlannerInfo root,
RelOptInfo joinrel,
bool  use_alias,
List **  params_list 
)
static

Definition at line 1411 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), buf, deparse_expr_cxt::buf, StringInfoData::data, deparseRangeTblRef(), deparseRelation(), RelOptInfo::fdw_private, deparse_expr_cxt::foreignrel, get_jointype_name(), heap_close, heap_open(), initStringInfo(), PgFdwRelationInfo::innerrel, IS_JOIN_REL, PgFdwRelationInfo::joinclauses, PgFdwRelationInfo::jointype, PgFdwRelationInfo::make_innerrel_subquery, PgFdwRelationInfo::make_outerrel_subquery, NoLock, PgFdwRelationInfo::outerrel, deparse_expr_cxt::params_list, planner_rt_fetch, REL_ALIAS_PREFIX, RelOptInfo::relid, RangeTblEntry::relid, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

Referenced by deparseFromExpr(), and deparseRangeTblRef().

1413 {
1414  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1415 
1416  if (IS_JOIN_REL(foreignrel))
1417  {
1418  StringInfoData join_sql_o;
1419  StringInfoData join_sql_i;
1420 
1421  /* Deparse outer relation */
1422  initStringInfo(&join_sql_o);
1423  deparseRangeTblRef(&join_sql_o, root, fpinfo->outerrel,
1424  fpinfo->make_outerrel_subquery, params_list);
1425 
1426  /* Deparse inner relation */
1427  initStringInfo(&join_sql_i);
1428  deparseRangeTblRef(&join_sql_i, root, fpinfo->innerrel,
1429  fpinfo->make_innerrel_subquery, params_list);
1430 
1431  /*
1432  * For a join relation FROM clause entry is deparsed as
1433  *
1434  * ((outer relation) <join type> (inner relation) ON (joinclauses))
1435  */
1436  appendStringInfo(buf, "(%s %s JOIN %s ON ", join_sql_o.data,
1437  get_jointype_name(fpinfo->jointype), join_sql_i.data);
1438 
1439  /* Append join clause; (TRUE) if no join clause */
1440  if (fpinfo->joinclauses)
1441  {
1442  deparse_expr_cxt context;
1443 
1444  context.buf = buf;
1445  context.foreignrel = foreignrel;
1446  context.scanrel = foreignrel;
1447  context.root = root;
1448  context.params_list = params_list;
1449 
1450  appendStringInfo(buf, "(");
1451  appendConditions(fpinfo->joinclauses, &context);
1452  appendStringInfo(buf, ")");
1453  }
1454  else
1455  appendStringInfoString(buf, "(TRUE)");
1456 
1457  /* End the FROM clause entry. */
1458  appendStringInfo(buf, ")");
1459  }
1460  else
1461  {
1462  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1463 
1464  /*
1465  * Core code already has some lock on each rel being planned, so we
1466  * can use NoLock here.
1467  */
1468  Relation rel = heap_open(rte->relid, NoLock);
1469 
1470  deparseRelation(buf, rel);
1471 
1472  /*
1473  * Add a unique alias to avoid any conflict in relation names due to
1474  * pulled up subqueries in the query being built for a pushed down
1475  * join.
1476  */
1477  if (use_alias)
1478  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid);
1479 
1480  heap_close(rel, NoLock);
1481  }
1482 }
static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, List **params_list)
Definition: deparse.c:1488
List ** params_list
Definition: deparse.c:105
#define IS_JOIN_REL(rel)
Definition: relation.h:509
#define heap_close(r, l)
Definition: heapam.h:97
RelOptInfo * outerrel
Definition: postgres_fdw.h:89
bool make_outerrel_subquery
Definition: postgres_fdw.h:99
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
#define REL_ALIAS_PREFIX
Definition: deparse.c:108
const char * get_jointype_name(JoinType jointype)
Definition: deparse.c:1311
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1277
StringInfo buf
Definition: deparse.c:104
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
PlannerInfo * root
Definition: deparse.c:99
RelOptInfo * innerrel
Definition: postgres_fdw.h:90
RelOptInfo * scanrel
Definition: deparse.c:101
RelOptInfo * foreignrel
Definition: deparse.c:100
static void deparseFuncExpr ( FuncExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2431 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, lnext, and NULL.

Referenced by deparseExpr().

2432 {
2433  StringInfo buf = context->buf;
2434  bool use_variadic;
2435  bool first;
2436  ListCell *arg;
2437 
2438  /*
2439  * If the function call came from an implicit coercion, then just show the
2440  * first argument.
2441  */
2442  if (node->funcformat == COERCE_IMPLICIT_CAST)
2443  {
2444  deparseExpr((Expr *) linitial(node->args), context);
2445  return;
2446  }
2447 
2448  /*
2449  * If the function call came from a cast, then show the first argument
2450  * plus an explicit cast operation.
2451  */
2452  if (node->funcformat == COERCE_EXPLICIT_CAST)
2453  {
2454  Oid rettype = node->funcresulttype;
2455  int32 coercedTypmod;
2456 
2457  /* Get the typmod if this is a length-coercion function */
2458  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2459 
2460  deparseExpr((Expr *) linitial(node->args), context);
2461  appendStringInfo(buf, "::%s",
2462  deparse_type_name(rettype, coercedTypmod));
2463  return;
2464  }
2465 
2466  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2467  use_variadic = node->funcvariadic;
2468 
2469  /*
2470  * Normal function: display as proname(args).
2471  */
2472  appendFunctionName(node->funcid, context);
2473  appendStringInfoChar(buf, '(');
2474 
2475  /* ... and all the arguments */
2476  first = true;
2477  foreach(arg, node->args)
2478  {
2479  if (!first)
2480  appendStringInfoString(buf, ", ");
2481  if (use_variadic && lnext(arg) == NULL)
2482  appendStringInfoString(buf, "VARIADIC ");
2483  deparseExpr((Expr *) lfirst(arg), context);
2484  first = false;
2485  }
2486  appendStringInfoChar(buf, ')');
2487 }
Oid funcresulttype
Definition: primnodes.h:449
List * args
Definition: primnodes.h:456
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3015
Definition: nodes.h:509
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:513
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
CoercionForm funcformat
Definition: primnodes.h:453
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define linitial(l)
Definition: pg_list.h:111
Oid funcid
Definition: primnodes.h:448
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
#define lnext(lc)
Definition: pg_list.h:105
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
void * arg
bool funcvariadic
Definition: primnodes.h:451
void deparseInsertSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List targetAttrs,
bool  doNothing,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1548 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1552 {
1553  AttrNumber pindex;
1554  bool first;
1555  ListCell *lc;
1556 
1557  appendStringInfoString(buf, "INSERT INTO ");
1558  deparseRelation(buf, rel);
1559 
1560  if (targetAttrs)
1561  {
1562  appendStringInfoChar(buf, '(');
1563 
1564  first = true;
1565  foreach(lc, targetAttrs)
1566  {
1567  int attnum = lfirst_int(lc);
1568 
1569  if (!first)
1570  appendStringInfoString(buf, ", ");
1571  first = false;
1572 
1573  deparseColumnRef(buf, rtindex, attnum, root, false);
1574  }
1575 
1576  appendStringInfoString(buf, ") VALUES (");
1577 
1578  pindex = 1;
1579  first = true;
1580  foreach(lc, targetAttrs)
1581  {
1582  if (!first)
1583  appendStringInfoString(buf, ", ");
1584  first = false;
1585 
1586  appendStringInfo(buf, "$%d", pindex);
1587  pindex++;
1588  }
1589 
1590  appendStringInfoChar(buf, ')');
1591  }
1592  else
1593  appendStringInfoString(buf, " DEFAULT VALUES");
1594 
1595  if (doNothing)
1596  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1597 
1598  deparseReturningList(buf, root, rtindex, rel,
1599  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1600  returningList, retrieved_attrs);
1601 }
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1899
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define lfirst_int(lc)
Definition: pg_list.h:107
TriggerDesc * trigdesc
Definition: rel.h:120
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
bool trig_insert_after_row
Definition: reltrigger.h:56
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1777
int16 AttrNumber
Definition: attnum.h:21
static void deparseLockingClause ( deparse_expr_cxt context)
static

Definition at line 1187 of file deparse.c.

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

Referenced by deparseSelectStmtForRel().

1188 {
1189  StringInfo buf = context->buf;
1190  PlannerInfo *root = context->root;
1191  RelOptInfo *rel = context->scanrel;
1192  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1193  int relid = -1;
1194 
1195  while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1196  {
1197  /*
1198  * Ignore relation if it appears in a lower subquery. Locking clause
1199  * for such a relation is included in the subquery if necessary.
1200  */
1201  if (bms_is_member(relid, fpinfo->lower_subquery_rels))
1202  continue;
1203 
1204  /*
1205  * Add FOR UPDATE/SHARE if appropriate. We apply locking during the
1206  * initial row fetch, rather than later on as is done for local
1207  * tables. The extra roundtrips involved in trying to duplicate the
1208  * local semantics exactly don't seem worthwhile (see also comments
1209  * for RowMarkType).
1210  *
1211  * Note: because we actually run the query as a cursor, this assumes
1212  * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't
1213  * before 8.3.
1214  */
1215  if (relid == root->parse->resultRelation &&
1216  (root->parse->commandType == CMD_UPDATE ||
1217  root->parse->commandType == CMD_DELETE))
1218  {
1219  /* Relation is UPDATE/DELETE target, so use FOR UPDATE */
1220  appendStringInfoString(buf, " FOR UPDATE");
1221 
1222  /* Add the relation alias if we are here for a join relation */
1223  if (IS_JOIN_REL(rel))
1224  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1225  }
1226  else
1227  {
1228  PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid);
1229 
1230  if (rc)
1231  {
1232  /*
1233  * Relation is specified as a FOR UPDATE/SHARE target, so
1234  * handle that. (But we could also see LCS_NONE, meaning this
1235  * isn't a target relation after all.)
1236  *
1237  * For now, just ignore any [NO] KEY specification, since (a)
1238  * it's not clear what that means for a remote table that we
1239  * don't have complete information about, and (b) it wouldn't
1240  * work anyway on older remote servers. Likewise, we don't
1241  * worry about NOWAIT.
1242  */
1243  switch (rc->strength)
1244  {
1245  case LCS_NONE:
1246  /* No locking needed */
1247  break;
1248  case LCS_FORKEYSHARE:
1249  case LCS_FORSHARE:
1250  appendStringInfoString(buf, " FOR SHARE");
1251  break;
1252  case LCS_FORNOKEYUPDATE:
1253  case LCS_FORUPDATE:
1254  appendStringInfoString(buf, " FOR UPDATE");
1255  break;
1256  }
1257 
1258  /* Add the relation alias if we are here for a join relation */
1259  if (bms_num_members(rel->relids) > 1 &&
1260  rc->strength != LCS_NONE)
1261  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1262  }
1263  }
1264  }
1265 }
List * rowMarks
Definition: relation.h:255
Query * parse
Definition: relation.h:154
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
int resultRelation
Definition: parsenodes.h:120
#define IS_JOIN_REL(rel)
Definition: relation.h:509
Relids lower_subquery_rels
Definition: postgres_fdw.h:103
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
#define REL_ALIAS_PREFIX
Definition: deparse.c:108
Relids relids
Definition: relation.h:524
StringInfo buf
Definition: deparse.c:104
void * fdw_private
Definition: relation.h:576
CmdType commandType
Definition: parsenodes.h:110
PlannerInfo * root
Definition: deparse.c:99
LockClauseStrength strength
Definition: plannodes.h:1003
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:401
RelOptInfo * scanrel
Definition: deparse.c:101
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static void deparseNullTest ( NullTest node,
deparse_expr_cxt context 
)
static

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

2688 {
2689  StringInfo buf = context->buf;
2690 
2691  appendStringInfoChar(buf, '(');
2692  deparseExpr(node->arg, context);
2693 
2694  /*
2695  * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
2696  * shorter and traditional. If it's a rowtype input but we're applying a
2697  * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
2698  * correct.
2699  */
2700  if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
2701  {
2702  if (node->nulltesttype == IS_NULL)
2703  appendStringInfoString(buf, " IS NULL)");
2704  else
2705  appendStringInfoString(buf, " IS NOT NULL)");
2706  }
2707  else
2708  {
2709  if (node->nulltesttype == IS_NULL)
2710  appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
2711  else
2712  appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
2713  }
2714 }
Definition: nodes.h:509
Expr * arg
Definition: primnodes.h:1179
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2404
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
NullTestType nulltesttype
Definition: primnodes.h:1180
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
bool argisrow
Definition: primnodes.h:1181
static void deparseOperatorName ( StringInfo  buf,
Form_pg_operator  opform 
)
static

Definition at line 2545 of file deparse.c.

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

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

2546 {
2547  char *opname;
2548 
2549  /* opname is not a SQL identifier, so we should not quote it. */
2550  opname = NameStr(opform->oprname);
2551 
2552  /* Print schema name only if it's not pg_catalog */
2553  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
2554  {
2555  const char *opnspname;
2556 
2557  opnspname = get_namespace_name(opform->oprnamespace);
2558  /* Print fully qualified operator name. */
2559  appendStringInfo(buf, "OPERATOR(%s.%s)",
2560  quote_identifier(opnspname), opname);
2561  }
2562  else
2563  {
2564  /* Just print operator name. */
2565  appendStringInfoString(buf, opname);
2566  }
2567 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10246
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
#define NameStr(name)
Definition: c.h:499
static void deparseOpExpr ( OpExpr node,
deparse_expr_cxt context 
)
static

Definition at line 2494 of file deparse.c.

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

Referenced by deparseExpr().

2495 {
2496  StringInfo buf = context->buf;
2497  HeapTuple tuple;
2498  Form_pg_operator form;
2499  char oprkind;
2500  ListCell *arg;
2501 
2502  /* Retrieve information about the operator from system catalog. */
2503  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2504  if (!HeapTupleIsValid(tuple))
2505  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2506  form = (Form_pg_operator) GETSTRUCT(tuple);
2507  oprkind = form->oprkind;
2508 
2509  /* Sanity check. */
2510  Assert((oprkind == 'r' && list_length(node->args) == 1) ||
2511  (oprkind == 'l' && list_length(node->args) == 1) ||
2512  (oprkind == 'b' && list_length(node->args) == 2));
2513 
2514  /* Always parenthesize the expression. */
2515  appendStringInfoChar(buf, '(');
2516 
2517  /* Deparse left operand. */
2518  if (oprkind == 'r' || oprkind == 'b')
2519  {
2520  arg = list_head(node->args);
2521  deparseExpr(lfirst(arg), context);
2522  appendStringInfoChar(buf, ' ');
2523  }
2524 
2525  /* Deparse operator name. */
2526  deparseOperatorName(buf, form);
2527 
2528  /* Deparse right operand. */
2529  if (oprkind == 'l' || oprkind == 'b')
2530  {
2531  arg = list_tail(node->args);
2532  appendStringInfoChar(buf, ' ');
2533  deparseExpr(lfirst(arg), context);
2534  }
2535 
2536  appendStringInfoChar(buf, ')');
2537 
2538  ReleaseSysCache(tuple);
2539 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2545
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
static ListCell * list_tail(List *l)
Definition: pg_list.h:83
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:66
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
FormData_pg_operator* Form_pg_operator
Definition: pg_operator.h:57
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
void * arg
Oid opno
Definition: primnodes.h:495
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:501
static void deparseParam ( Param node,
deparse_expr_cxt context 
)
static

Definition at line 2352 of file deparse.c.

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

Referenced by deparseExpr().

2353 {
2354  if (context->params_list)
2355  {
2356  int pindex = 0;
2357  ListCell *lc;
2358 
2359  /* find its index in params_list */
2360  foreach(lc, *context->params_list)
2361  {
2362  pindex++;
2363  if (equal(node, (Node *) lfirst(lc)))
2364  break;
2365  }
2366  if (lc == NULL)
2367  {
2368  /* not in list, so add it */
2369  pindex++;
2370  *context->params_list = lappend(*context->params_list, node);
2371  }
2372 
2373  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2374  }
2375  else
2376  {
2377  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2378  }
2379 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2961
List ** params_list
Definition: deparse.c:105
Definition: nodes.h:509
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2897
List * lappend(List *list, void *datum)
Definition: list.c:128
int32 paramtypmod
Definition: primnodes.h:247
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Oid paramtype
Definition: primnodes.h:246
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2923
static void deparseRangeTblRef ( StringInfo  buf,
PlannerInfo root,
RelOptInfo foreignrel,
bool  make_subquery,
List **  params_list 
)
static

Definition at line 1488 of file deparse.c.

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

Referenced by deparseFromExprForRel().

1490 {
1491  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1492 
1493  /* Should only be called in these cases. */
1494  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1495 
1496  Assert(fpinfo->local_conds == NIL);
1497 
1498  /* If make_subquery is true, deparse the relation as a subquery. */
1499  if (make_subquery)
1500  {
1501  List *retrieved_attrs;
1502  int ncols;
1503 
1504  /* Deparse the subquery representing the relation. */
1505  appendStringInfoChar(buf, '(');
1506  deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1507  fpinfo->remote_conds, NIL, true,
1508  &retrieved_attrs, params_list);
1509  appendStringInfoChar(buf, ')');
1510 
1511  /* Append the relation alias. */
1513  fpinfo->relation_index);
1514 
1515  /*
1516  * Append the column aliases if needed. Note that the subquery emits
1517  * expressions specified in the relation's reltarget (see
1518  * deparseSubqueryTargetList).
1519  */
1520  ncols = list_length(foreignrel->reltarget->exprs);
1521  if (ncols > 0)
1522  {
1523  int i;
1524 
1525  appendStringInfoChar(buf, '(');
1526  for (i = 1; i <= ncols; i++)
1527  {
1528  if (i > 1)
1529  appendStringInfoString(buf, ", ");
1530 
1532  }
1533  appendStringInfoChar(buf, ')');
1534  }
1535  }
1536  else
1537  deparseFromExprForRel(buf, root, foreignrel, true, params_list);
1538 }
#define NIL
Definition: pg_list.h:69
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list)
Definition: deparse.c:1411
#define IS_JOIN_REL(rel)
Definition: relation.h:509
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:113
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:112
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
List * exprs
Definition: relation.h:882
void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, bool is_subquery, List **retrieved_attrs, List **params_list)
Definition: deparse.c:925
void * fdw_private
Definition: relation.h:576
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
int i
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:535
static void deparseRelabelType ( RelabelType node,
deparse_expr_cxt context 
)
static

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

2637 {
2638  deparseExpr(node->arg, context);
2639  if (node->relabelformat != COERCE_IMPLICIT_CAST)
2640  appendStringInfo(context->buf, "::%s",
2642  node->resulttypmod));
2643 }
Expr * arg
Definition: primnodes.h:790
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
StringInfo buf
Definition: deparse.c:104
Oid resulttype
Definition: primnodes.h:791
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
int32 resulttypmod
Definition: primnodes.h:792
CoercionForm relabelformat
Definition: primnodes.h:794
static void deparseRelation ( StringInfo  buf,
Relation  rel 
)
static

Definition at line 2044 of file deparse.c.

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

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

2045 {
2046  ForeignTable *table;
2047  const char *nspname = NULL;
2048  const char *relname = NULL;
2049  ListCell *lc;
2050 
2051  /* obtain additional catalog information. */
2052  table = GetForeignTable(RelationGetRelid(rel));
2053 
2054  /*
2055  * Use value of FDW options if any, instead of the name of object itself.
2056  */
2057  foreach(lc, table->options)
2058  {
2059  DefElem *def = (DefElem *) lfirst(lc);
2060 
2061  if (strcmp(def->defname, "schema_name") == 0)
2062  nspname = defGetString(def);
2063  else if (strcmp(def->defname, "table_name") == 0)
2064  relname = defGetString(def);
2065  }
2066 
2067  /*
2068  * Note: we could skip printing the schema name if it's pg_catalog, but
2069  * that doesn't seem worth the trouble.
2070  */
2071  if (nspname == NULL)
2072  nspname = get_namespace_name(RelationGetNamespace(rel));
2073  if (relname == NULL)
2074  relname = RelationGetRelationName(rel);
2075 
2076  appendStringInfo(buf, "%s.%s",
2077  quote_identifier(nspname), quote_identifier(relname));
2078 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10246
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:216
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
char * defGetString(DefElem *def)
Definition: define.c:49
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * options
Definition: foreign.h:68
char * defname
Definition: parsenodes.h:719
#define RelationGetRelid(relation)
Definition: rel.h:417
#define RelationGetNamespace(relation)
Definition: rel.h:444
static void deparseReturningList ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
bool  trig_after_row,
List returningList,
List **  retrieved_attrs 
)
static

Definition at line 1777 of file deparse.c.

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

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

1782 {
1783  Bitmapset *attrs_used = NULL;
1784 
1785  if (trig_after_row)
1786  {
1787  /* whole-row reference acquires all non-system columns */
1788  attrs_used =
1790  }
1791 
1792  if (returningList != NIL)
1793  {
1794  /*
1795  * We need the attrs, non-system and system, mentioned in the local
1796  * query's RETURNING list.
1797  */
1798  pull_varattnos((Node *) returningList, rtindex,
1799  &attrs_used);
1800  }
1801 
1802  if (attrs_used != NULL)
1803  deparseTargetList(buf, root, rtindex, rel, true, attrs_used, false,
1804  retrieved_attrs);
1805  else
1806  *retrieved_attrs = NIL;
1807 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:509
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
static void deparseTargetList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1095
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:179
#define NULL
Definition: c.h:229
static void deparseScalarArrayOpExpr ( ScalarArrayOpExpr node,
deparse_expr_cxt context 
)
static

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

2592 {
2593  StringInfo buf = context->buf;
2594  HeapTuple tuple;
2595  Form_pg_operator form;
2596  Expr *arg1;
2597  Expr *arg2;
2598 
2599  /* Retrieve information about the operator from system catalog. */
2600  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2601  if (!HeapTupleIsValid(tuple))
2602  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2603  form = (Form_pg_operator) GETSTRUCT(tuple);
2604 
2605  /* Sanity check. */
2606  Assert(list_length(node->args) == 2);
2607 
2608  /* Always parenthesize the expression. */
2609  appendStringInfoChar(buf, '(');
2610 
2611  /* Deparse left operand. */
2612  arg1 = linitial(node->args);
2613  deparseExpr(arg1, context);
2614  appendStringInfoChar(buf, ' ');
2615 
2616  /* Deparse operator name plus decoration. */
2617  deparseOperatorName(buf, form);
2618  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
2619 
2620  /* Deparse right operand. */
2621  arg2 = lsecond(node->args);
2622  deparseExpr(arg2, context);
2623 
2624  appendStringInfoChar(buf, ')');
2625 
2626  /* Always parenthesize the expression. */
2627  appendStringInfoChar(buf, ')');
2628 
2629  ReleaseSysCache(tuple);
2630 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2545
#define lsecond(l)
Definition: pg_list.h:116
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define linitial(l)
Definition: pg_list.h:111
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:66
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
FormData_pg_operator* Form_pg_operator
Definition: pg_operator.h:57
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
#define elog
Definition: elog.h:219
static void deparseSelectSql ( List tlist,
bool  is_subquery,
List **  retrieved_attrs,
deparse_expr_cxt context 
)
static

Definition at line 1003 of file deparse.c.

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

Referenced by deparseSelectStmtForRel().

1005 {
1006  StringInfo buf = context->buf;
1007  RelOptInfo *foreignrel = context->foreignrel;
1008  PlannerInfo *root = context->root;
1009  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1010 
1011  /*
1012  * Construct SELECT list
1013  */
1014  appendStringInfoString(buf, "SELECT ");
1015 
1016  if (is_subquery)
1017  {
1018  /*
1019  * For a relation that is deparsed as a subquery, emit expressions
1020  * specified in the relation's reltarget. Note that since this is
1021  * for the subquery, no need to care about *retrieved_attrs.
1022  */
1023  deparseSubqueryTargetList(context);
1024  }
1025  else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
1026  {
1027  /*
1028  * For a join or upper relation the input tlist gives the list of
1029  * columns required to be fetched from the foreign server.
1030  */
1031  deparseExplicitTargetList(tlist, retrieved_attrs, context);
1032  }
1033  else
1034  {
1035  /*
1036  * For a base relation fpinfo->attrs_used gives the list of columns
1037  * required to be fetched from the foreign server.
1038  */
1039  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1040 
1041  /*
1042  * Core code already has some lock on each rel being planned, so we
1043  * can use NoLock here.
1044  */
1045  Relation rel = heap_open(rte->relid, NoLock);
1046 
1047  deparseTargetList(buf, root, foreignrel->relid, rel, false,
1048  fpinfo->attrs_used, false, retrieved_attrs);
1049  heap_close(rel, NoLock);
1050  }
1051 }
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1376
#define IS_UPPER_REL(rel)
Definition: relation.h:512
#define IS_JOIN_REL(rel)
Definition: relation.h:509
#define heap_close(r, l)
Definition: heapam.h:97
static void deparseTargetList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1095
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
static void deparseExplicitTargetList(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1345
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
Index relid
Definition: relation.h:552
StringInfo buf
Definition: deparse.c:104
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
void * fdw_private
Definition: relation.h:576
PlannerInfo * root
Definition: deparse.c:99
Bitmapset * attrs_used
Definition: postgres_fdw.h:50
RelOptInfo * foreignrel
Definition: deparse.c:100
void deparseSelectStmtForRel ( StringInfo  buf,
PlannerInfo root,
RelOptInfo rel,
List tlist,
List remote_conds,
List pathkeys,
bool  is_subquery,
List **  retrieved_attrs,
List **  params_list 
)

Definition at line 925 of file deparse.c.

References appendConditions(), appendGroupByClause(), appendOrderByClause(), appendStringInfo(), 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, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

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

929 {
930  deparse_expr_cxt context;
932  List *quals;
933 
934  /*
935  * We handle relations for foreign tables, joins between those and upper
936  * relations.
937  */
938  Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel));
939 
940  /* Fill portions of context common to upper, join and base relation */
941  context.buf = buf;
942  context.root = root;
943  context.foreignrel = rel;
944  context.scanrel = IS_UPPER_REL(rel) ? fpinfo->outerrel : rel;
945  context.params_list = params_list;
946 
947  /* Construct SELECT clause */
948  deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
949 
950  /*
951  * For upper relations, the WHERE clause is built from the remote
952  * conditions of the underlying scan relation; otherwise, we can use the
953  * supplied list of remote conditions directly.
954  */
955  if (IS_UPPER_REL(rel))
956  {
957  PgFdwRelationInfo *ofpinfo;
958 
959  ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
960  quals = ofpinfo->remote_conds;
961  }
962  else
963  quals = remote_conds;
964 
965  /* Construct FROM and WHERE clauses */
966  deparseFromExpr(quals, &context);
967 
968  if (IS_UPPER_REL(rel))
969  {
970  /* Append GROUP BY clause */
971  appendGroupByClause(tlist, &context);
972 
973  /* Append HAVING clause */
974  if (remote_conds)
975  {
976  appendStringInfo(buf, " HAVING ");
977  appendConditions(remote_conds, &context);
978  }
979  }
980 
981  /* Add ORDER BY clause if we found any useful pathkeys */
982  if (pathkeys)
983  appendOrderByClause(pathkeys, &context);
984 
985  /* Add any necessary FOR UPDATE/SHARE. */
986  deparseLockingClause(&context);
987 }
static void appendGroupByClause(List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:2936
List ** params_list
Definition: deparse.c:105
#define IS_UPPER_REL(rel)
Definition: relation.h:512
#define IS_JOIN_REL(rel)
Definition: relation.h:509
RelOptInfo * outerrel
Definition: postgres_fdw.h:89
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static void appendOrderByClause(List *pathkeys, deparse_expr_cxt *context)
Definition: deparse.c:2973
static char * buf
Definition: pg_test_fsync.c:66
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1003
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1277
StringInfo buf
Definition: deparse.c:104
void * fdw_private
Definition: relation.h:576
PlannerInfo * root
Definition: deparse.c:99
#define Assert(condition)
Definition: c.h:675
static void deparseFromExpr(List *quals, deparse_expr_cxt *context)
Definition: deparse.c:1061
static void deparseLockingClause(deparse_expr_cxt *context)
Definition: deparse.c:1187
RelOptInfo * scanrel
Definition: deparse.c:101
Definition: pg_list.h:45
RelOptInfo * foreignrel
Definition: deparse.c:100
static Node * deparseSortGroupClause ( Index  ref,
List tlist,
deparse_expr_cxt context 
)
static

Definition at line 3050 of file deparse.c.

References appendStringInfoString(), buf, deparse_expr_cxt::buf, deparseConst(), deparseExpr(), TargetEntry::expr, get_sortgroupref_tle(), and IsA.

Referenced by appendAggOrderBy(), and appendGroupByClause().

3051 {
3052  StringInfo buf = context->buf;
3053  TargetEntry *tle;
3054  Expr *expr;
3055 
3056  tle = get_sortgroupref_tle(ref, tlist);
3057  expr = tle->expr;
3058 
3059  if (expr && IsA(expr, Const))
3060  {
3061  /*
3062  * Force a typecast here so that we don't emit something like "GROUP
3063  * BY 2", which will be misconstrued as a column position rather than
3064  * a constant.
3065  */
3066  deparseConst((Const *) expr, context, 1);
3067  }
3068  else if (!expr || IsA(expr, Var))
3069  deparseExpr(expr, context);
3070  else
3071  {
3072  /* Always parenthesize the expression. */
3073  appendStringInfoString(buf, "(");
3074  deparseExpr(expr, context);
3075  appendStringInfoString(buf, ")");
3076  }
3077 
3078  return (Node *) expr;
3079 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: nodes.h:509
Definition: primnodes.h:163
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
StringInfo buf
Definition: deparse.c:104
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2246
Expr * expr
Definition: primnodes.h:1367
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
void deparseStringLiteral ( StringInfo  buf,
const char *  val 
)

Definition at line 2084 of file deparse.c.

References appendStringInfoChar(), ESCAPE_STRING_SYNTAX, NULL, and SQL_STR_DOUBLE.

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

2085 {
2086  const char *valptr;
2087 
2088  /*
2089  * Rather than making assumptions about the remote server's value of
2090  * standard_conforming_strings, always use E'foo' syntax if there are any
2091  * backslashes. This will fail on remote servers before 8.1, but those
2092  * are long out of support.
2093  */
2094  if (strchr(val, '\\') != NULL)
2096  appendStringInfoChar(buf, '\'');
2097  for (valptr = val; *valptr; valptr++)
2098  {
2099  char ch = *valptr;
2100 
2101  if (SQL_STR_DOUBLE(ch, true))
2102  appendStringInfoChar(buf, ch);
2103  appendStringInfoChar(buf, ch);
2104  }
2105  appendStringInfoChar(buf, '\'');
2106 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:510
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
#define NULL
Definition: c.h:229
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:507
long val
Definition: informix.c:689
static void deparseSubqueryTargetList ( deparse_expr_cxt context)
static

Definition at line 1376 of file deparse.c.

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

Referenced by deparseSelectSql().

1377 {
1378  StringInfo buf = context->buf;
1379  RelOptInfo *foreignrel = context->foreignrel;
1380  bool first;
1381  ListCell *lc;
1382 
1383  /* Should only be called in these cases. */
1384  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1385 
1386  first = true;
1387  foreach(lc, foreignrel->reltarget->exprs)
1388  {
1389  Node *node = (Node *) lfirst(lc);
1390 
1391  if (!first)
1392  appendStringInfoString(buf, ", ");
1393  first = false;
1394 
1395  deparseExpr((Expr *) node, context);
1396  }
1397 
1398  /* Don't generate bad syntax if no expressions */
1399  if (first)
1400  appendStringInfoString(buf, "NULL");
1401 }
#define IS_JOIN_REL(rel)
Definition: relation.h:509
Definition: nodes.h:509
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:66
List * exprs
Definition: relation.h:882
StringInfo buf
Definition: deparse.c:104
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
RelOptInfo * foreignrel
Definition: deparse.c:100
struct PathTarget * reltarget
Definition: relation.h:535
static void deparseTargetList ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
bool  is_returning,
Bitmapset attrs_used,
bool  qualify_col,
List **  retrieved_attrs 
)
static

Definition at line 1095 of file deparse.c.

References ADD_REL_QUALIFIER, appendStringInfoString(), tupleDesc::attrs, bms_is_member(), deparseColumnRef(), FirstLowInvalidHeapAttributeNumber, i, lappend_int(), tupleDesc::natts, NIL, ObjectIdAttributeNumber, RelationGetDescr, and SelfItemPointerAttributeNumber.

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

1103 {
1104  TupleDesc tupdesc = RelationGetDescr(rel);
1105  bool have_wholerow;
1106  bool first;
1107  int i;
1108 
1109  *retrieved_attrs = NIL;
1110 
1111  /* If there's a whole-row reference, we'll need all the columns. */
1113  attrs_used);
1114 
1115  first = true;
1116  for (i = 1; i <= tupdesc->natts; i++)
1117  {
1118  Form_pg_attribute attr = tupdesc->attrs[i - 1];
1119 
1120  /* Ignore dropped attributes. */
1121  if (attr->attisdropped)
1122  continue;
1123 
1124  if (have_wholerow ||
1126  attrs_used))
1127  {
1128  if (!first)
1129  appendStringInfoString(buf, ", ");
1130  else if (is_returning)
1131  appendStringInfoString(buf, " RETURNING ");
1132  first = false;
1133 
1134  deparseColumnRef(buf, rtindex, i, root, qualify_col);
1135 
1136  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1137  }
1138  }
1139 
1140  /*
1141  * Add ctid and oid if needed. We currently don't support retrieving any
1142  * other system columns.
1143  */
1145  attrs_used))
1146  {
1147  if (!first)
1148  appendStringInfoString(buf, ", ");
1149  else if (is_returning)
1150  appendStringInfoString(buf, " RETURNING ");
1151  first = false;
1152 
1153  if (qualify_col)
1154  ADD_REL_QUALIFIER(buf, rtindex);
1155  appendStringInfoString(buf, "ctid");
1156 
1157  *retrieved_attrs = lappend_int(*retrieved_attrs,
1159  }
1161  attrs_used))
1162  {
1163  if (!first)
1164  appendStringInfoString(buf, ", ");
1165  else if (is_returning)
1166  appendStringInfoString(buf, " RETURNING ");
1167  first = false;
1168 
1169  if (qualify_col)
1170  ADD_REL_QUALIFIER(buf, rtindex);
1171  appendStringInfoString(buf, "oid");
1172 
1173  *retrieved_attrs = lappend_int(*retrieved_attrs,
1175  }
1176 
1177  /* Don't generate bad syntax if no undropped columns */
1178  if (first && !is_returning)
1179  appendStringInfoString(buf, "NULL");
1180 }
#define NIL
Definition: pg_list.h:69
#define RelationGetDescr(relation)
Definition: rel.h:429
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1899
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
int natts
Definition: tupdesc.h:73
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
List * lappend_int(List *list, int datum)
Definition: list.c:146
int i
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
void deparseUpdateSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List targetAttrs,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1611 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1615 {
1616  AttrNumber pindex;
1617  bool first;
1618  ListCell *lc;
1619 
1620  appendStringInfoString(buf, "UPDATE ");
1621  deparseRelation(buf, rel);
1622  appendStringInfoString(buf, " SET ");
1623 
1624  pindex = 2; /* ctid is always the first param */
1625  first = true;
1626  foreach(lc, targetAttrs)
1627  {
1628  int attnum = lfirst_int(lc);
1629 
1630  if (!first)
1631  appendStringInfoString(buf, ", ");
1632  first = false;
1633 
1634  deparseColumnRef(buf, rtindex, attnum, root, false);
1635  appendStringInfo(buf, " = $%d", pindex);
1636  pindex++;
1637  }
1638  appendStringInfoString(buf, " WHERE ctid = $1");
1639 
1640  deparseReturningList(buf, root, rtindex, rel,
1641  rel->trigdesc && rel->trigdesc->trig_update_after_row,
1642  returningList, retrieved_attrs);
1643 }
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1899
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define lfirst_int(lc)
Definition: pg_list.h:107
TriggerDesc * trigdesc
Definition: rel.h:120
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
bool trig_update_after_row
Definition: reltrigger.h:61
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1777
int16 AttrNumber
Definition: attnum.h:21
static void deparseVar ( Var node,
deparse_expr_cxt context 
)
static

Definition at line 2181 of file deparse.c.

References appendStringInfo(), bms_is_member(), bms_num_members(), deparse_expr_cxt::buf, deparseColumnRef(), equal(), is_subquery_var(), lappend(), lfirst, NULL, deparse_expr_cxt::params_list, printRemoteParam(), printRemotePlaceholder(), 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().

2182 {
2183  Relids relids = context->scanrel->relids;
2184  int relno;
2185  int colno;
2186 
2187  /* Qualify columns when multiple relations are involved. */
2188  bool qualify_col = (bms_num_members(relids) > 1);
2189 
2190  /*
2191  * If the Var belongs to the foreign relation that is deparsed as a
2192  * subquery, use the relation and column alias to the Var provided
2193  * by the subquery, instead of the remote name.
2194  */
2195  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2196  {
2197  appendStringInfo(context->buf, "%s%d.%s%d",
2199  SUBQUERY_COL_ALIAS_PREFIX, colno);
2200  return;
2201  }
2202 
2203  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2204  deparseColumnRef(context->buf, node->varno, node->varattno,
2205  context->root, qualify_col);
2206  else
2207  {
2208  /* Treat like a Param */
2209  if (context->params_list)
2210  {
2211  int pindex = 0;
2212  ListCell *lc;
2213 
2214  /* find its index in params_list */
2215  foreach(lc, *context->params_list)
2216  {
2217  pindex++;
2218  if (equal(node, (Node *) lfirst(lc)))
2219  break;
2220  }
2221  if (lc == NULL)
2222  {
2223  /* not in list, so add it */
2224  pindex++;
2225  *context->params_list = lappend(*context->params_list, node);
2226  }
2227 
2228  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2229  }
2230  else
2231  {
2232  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2233  }
2234  }
2235 }
Index varlevelsup
Definition: primnodes.h:173
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2961
List ** params_list
Definition: deparse.c:105
Definition: nodes.h:509
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1899
AttrNumber varattno
Definition: primnodes.h:168
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:113
Oid vartype
Definition: primnodes.h:170
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:112
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
Relids relids
Definition: relation.h:524
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:166
StringInfo buf
Definition: deparse.c:104
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3088
PlannerInfo * root
Definition: deparse.c:99
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
RelOptInfo * scanrel
Definition: deparse.c:101
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2923
int32 vartypmod
Definition: primnodes.h:171
static bool foreign_expr_walker ( Node node,
foreign_glob_cxt glob_cxt,
foreign_loc_cxt outer_cxt 
)
static

Definition at line 290 of file deparse.c.

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

Referenced by is_foreign_expr().

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

Definition at line 1311 of file deparse.c.

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

Referenced by deparseFromExprForRel(), and foreign_join_ok().

1312 {
1313  switch (jointype)
1314  {
1315  case JOIN_INNER:
1316  return "INNER";
1317 
1318  case JOIN_LEFT:
1319  return "LEFT";
1320 
1321  case JOIN_RIGHT:
1322  return "RIGHT";
1323 
1324  case JOIN_FULL:
1325  return "FULL";
1326 
1327  default:
1328  /* Shouldn't come here, but protect from buggy code. */
1329  elog(ERROR, "unsupported join type %d", jointype);
1330  }
1331 
1332  /* Keep compiler happy */
1333  return NULL;
1334 }
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
static void get_relation_column_alias_ids ( Var node,
RelOptInfo foreignrel,
int *  relno,
int *  colno 
)
static

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

3152 {
3153  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3154  int i;
3155  ListCell *lc;
3156 
3157  /* Get the relation alias ID */
3158  *relno = fpinfo->relation_index;
3159 
3160  /* Get the column alias ID */
3161  i = 1;
3162  foreach(lc, foreignrel->reltarget->exprs)
3163  {
3164  if (equal(lfirst(lc), (Node *) node))
3165  {
3166  *colno = i;
3167  return;
3168  }
3169  i++;
3170  }
3171 
3172  /* Shouldn't get here */
3173  elog(ERROR, "unexpected expression in subquery output");
3174 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2961
Definition: nodes.h:509
#define ERROR
Definition: elog.h:43
List * exprs
Definition: relation.h:882
void * fdw_private
Definition: relation.h:576
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define elog
Definition: elog.h:219
struct PathTarget * reltarget
Definition: relation.h:535
bool is_foreign_expr ( PlannerInfo root,
RelOptInfo baserel,
Expr expr 
)

Definition at line 226 of file deparse.c.

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

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

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

Definition at line 3088 of file deparse.c.

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

Referenced by deparseVar().

3089 {
3090  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3091  RelOptInfo *outerrel = fpinfo->outerrel;
3092  RelOptInfo *innerrel = fpinfo->innerrel;
3093 
3094  /* Should only be called in these cases. */
3095  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
3096 
3097  /*
3098  * If the given relation isn't a join relation, it doesn't have any lower
3099  * subqueries, so the Var isn't a subquery output column.
3100  */
3101  if (!IS_JOIN_REL(foreignrel))
3102  return false;
3103 
3104  /*
3105  * If the Var doesn't belong to any lower subqueries, it isn't a subquery
3106  * output column.
3107  */
3108  if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels))
3109  return false;
3110 
3111  if (bms_is_member(node->varno, outerrel->relids))
3112  {
3113  /*
3114  * If outer relation is deparsed as a subquery, the Var is an output
3115  * column of the subquery; get the IDs for the relation/column alias.
3116  */
3117  if (fpinfo->make_outerrel_subquery)
3118  {
3119  get_relation_column_alias_ids(node, outerrel, relno, colno);
3120  return true;
3121  }
3122 
3123  /* Otherwise, recurse into the outer relation. */
3124  return is_subquery_var(node, outerrel, relno, colno);
3125  }
3126  else
3127  {
3128  Assert(bms_is_member(node->varno, innerrel->relids));
3129 
3130  /*
3131  * If inner relation is deparsed as a subquery, the Var is an output
3132  * column of the subquery; get the IDs for the relation/column alias.
3133  */
3134  if (fpinfo->make_innerrel_subquery)
3135  {
3136  get_relation_column_alias_ids(node, innerrel, relno, colno);
3137  return true;
3138  }
3139 
3140  /* Otherwise, recurse into the inner relation. */
3141  return is_subquery_var(node, innerrel, relno, colno);
3142  }
3143 }
#define IS_JOIN_REL(rel)
Definition: relation.h:509
Relids lower_subquery_rels
Definition: postgres_fdw.h:103
RelOptInfo * outerrel
Definition: postgres_fdw.h:89
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
bool make_outerrel_subquery
Definition: postgres_fdw.h:99
Index varno
Definition: primnodes.h:166
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3088
void * fdw_private
Definition: relation.h:576
static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3150
#define Assert(condition)
Definition: c.h:675
RelOptInfo * innerrel
Definition: postgres_fdw.h:90
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static void printRemoteParam ( int  paramindex,
Oid  paramtype,
int32  paramtypmod,
deparse_expr_cxt context 
)
static

Definition at line 2897 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

2899 {
2900  StringInfo buf = context->buf;
2901  char *ptypename = deparse_type_name(paramtype, paramtypmod);
2902 
2903  appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
2904 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static char * buf
Definition: pg_test_fsync.c:66
StringInfo buf
Definition: deparse.c:104
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851
static void printRemotePlaceholder ( Oid  paramtype,
int32  paramtypmod,
deparse_expr_cxt context 
)
static

Definition at line 2923 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

2925 {
2926  StringInfo buf = context->buf;
2927  char *ptypename = deparse_type_name(paramtype, paramtypmod);
2928 
2929  appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename);
2930 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static char * buf
Definition: pg_test_fsync.c:66
StringInfo buf
Definition: deparse.c:104
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851