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

2844 {
2845  StringInfo buf = context->buf;
2846  ListCell *lc;
2847  bool first = true;
2848 
2849  foreach(lc, orderList)
2850  {
2851  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
2852  Node *sortexpr;
2853  Oid sortcoltype;
2854  TypeCacheEntry *typentry;
2855 
2856  if (!first)
2857  appendStringInfoString(buf, ", ");
2858  first = false;
2859 
2860  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
2861  context);
2862  sortcoltype = exprType(sortexpr);
2863  /* See whether operator is default < or > for datatype */
2864  typentry = lookup_type_cache(sortcoltype,
2866  if (srt->sortop == typentry->lt_opr)
2867  appendStringInfoString(buf, " ASC");
2868  else if (srt->sortop == typentry->gt_opr)
2869  appendStringInfoString(buf, " DESC");
2870  else
2871  {
2872  HeapTuple opertup;
2873  Form_pg_operator operform;
2874 
2875  appendStringInfoString(buf, " USING ");
2876 
2877  /* Append operator name. */
2878  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
2879  if (!HeapTupleIsValid(opertup))
2880  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
2881  operform = (Form_pg_operator) GETSTRUCT(opertup);
2882  deparseOperatorName(buf, operform);
2883  ReleaseSysCache(opertup);
2884  }
2885 
2886  if (srt->nulls_first)
2887  appendStringInfoString(buf, " NULLS FIRST");
2888  else
2889  appendStringInfoString(buf, " NULLS LAST");
2890  }
2891 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Index tleSortGroupRef
Definition: parsenodes.h:1141
Definition: nodes.h:520
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2550
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:150
#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:65
#define TYPECACHE_GT_OPR
Definition: typcache.h:112
static Node * deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3055
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1093
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 1273 of file deparse.c.

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

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

1274 {
1275  int nestlevel;
1276  ListCell *lc;
1277  bool is_first = true;
1278  StringInfo buf = context->buf;
1279 
1280  /* Make sure any constants in the exprs are printed portably */
1281  nestlevel = set_transmission_modes();
1282 
1283  foreach(lc, exprs)
1284  {
1285  Expr *expr = (Expr *) lfirst(lc);
1286 
1287  /*
1288  * Extract clause from RestrictInfo, if required. See comments in
1289  * declaration of PgFdwRelationInfo for details.
1290  */
1291  if (IsA(expr, RestrictInfo))
1292  {
1293  RestrictInfo *ri = (RestrictInfo *) expr;
1294 
1295  expr = ri->clause;
1296  }
1297 
1298  /* Connect expressions with "AND" and parenthesize each condition. */
1299  if (!is_first)
1300  appendStringInfoString(buf, " AND ");
1301 
1302  appendStringInfoChar(buf, '(');
1303  deparseExpr(expr, context);
1304  appendStringInfoChar(buf, ')');
1305 
1306  is_first = false;
1307  }
1308 
1309  reset_transmission_modes(nestlevel);
1310 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:571
int set_transmission_modes(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
Expr * clause
Definition: relation.h:1660
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:2124
static void appendFunctionName ( Oid  funcid,
deparse_expr_cxt context 
)
static

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

3021 {
3022  StringInfo buf = context->buf;
3023  HeapTuple proctup;
3024  Form_pg_proc procform;
3025  const char *proname;
3026 
3027  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3028  if (!HeapTupleIsValid(proctup))
3029  elog(ERROR, "cache lookup failed for function %u", funcid);
3030  procform = (Form_pg_proc) GETSTRUCT(proctup);
3031 
3032  /* Print schema name only if it's not pg_catalog */
3033  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3034  {
3035  const char *schemaname;
3036 
3037  schemaname = get_namespace_name(procform->pronamespace);
3038  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3039  }
3040 
3041  /* Always print the function name */
3042  proname = NameStr(procform->proname);
3043  appendStringInfo(buf, "%s", quote_identifier(proname));
3044 
3045  ReleaseSysCache(proctup);
3046 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10114
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:150
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:3006
static char * buf
Definition: pg_test_fsync.c:65
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1093
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 2941 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().

2942 {
2943  StringInfo buf = context->buf;
2944  Query *query = context->root->parse;
2945  ListCell *lc;
2946  bool first = true;
2947 
2948  /* Nothing to be done, if there's no GROUP BY clause in the query. */
2949  if (!query->groupClause)
2950  return;
2951 
2952  appendStringInfo(buf, " GROUP BY ");
2953 
2954  /*
2955  * Queries with grouping sets are not pushed down, so we don't expect
2956  * grouping sets here.
2957  */
2958  Assert(!query->groupingSets);
2959 
2960  foreach(lc, query->groupClause)
2961  {
2962  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
2963 
2964  if (!first)
2965  appendStringInfoString(buf, ", ");
2966  first = false;
2967 
2968  deparseSortGroupClause(grp->tleSortGroupRef, tlist, context);
2969  }
2970 }
Query * parse
Definition: relation.h:154
Index tleSortGroupRef
Definition: parsenodes.h:1141
List * groupingSets
Definition: parsenodes.h:139
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:65
static Node * deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3055
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:137
static void appendOrderByClause ( List pathkeys,
deparse_expr_cxt context 
)
static

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

2979 {
2980  ListCell *lcell;
2981  int nestlevel;
2982  char *delim = " ";
2983  RelOptInfo *baserel = context->scanrel;
2984  StringInfo buf = context->buf;
2985 
2986  /* Make sure any constants in the exprs are printed portably */
2987  nestlevel = set_transmission_modes();
2988 
2989  appendStringInfo(buf, " ORDER BY");
2990  foreach(lcell, pathkeys)
2991  {
2992  PathKey *pathkey = lfirst(lcell);
2993  Expr *em_expr;
2994 
2995  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
2996  Assert(em_expr != NULL);
2997 
2998  appendStringInfoString(buf, delim);
2999  deparseExpr(em_expr, context);
3000  if (pathkey->pk_strategy == BTLessStrategyNumber)
3001  appendStringInfoString(buf, " ASC");
3002  else
3003  appendStringInfoString(buf, " DESC");
3004 
3005  if (pathkey->pk_nulls_first)
3006  appendStringInfoString(buf, " NULLS FIRST");
3007  else
3008  appendStringInfoString(buf, " NULLS LAST");
3009 
3010  delim = ", ";
3011  }
3012  reset_transmission_modes(nestlevel);
3013 }
int set_transmission_modes(void)
int pk_strategy
Definition: relation.h:797
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
bool pk_nulls_first
Definition: relation.h:798
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
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:795
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2124
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(), PathTarget::exprs, RelOptInfo::fdw_private, PgFdwRelationInfo::grouped_tlist, PgFdwRelationInfo::local_conds, NIL, pull_var_clause(), PVC_RECURSE_PLACEHOLDERS, RELOPT_UPPER_REL, RelOptInfo::reloptkind, and RelOptInfo::reltarget.

Referenced by estimate_path_cost_size(), and postgresGetForeignPlan().

869 {
870  List *tlist = NIL;
871  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
872 
873  /*
874  * For an upper relation, we have already built the target list while
875  * checking shippability, so just return that.
876  */
877  if (foreignrel->reloptkind == RELOPT_UPPER_REL)
878  return fpinfo->grouped_tlist;
879 
880  /*
881  * We require columns specified in foreignrel->reltarget->exprs and those
882  * required for evaluating the local conditions.
883  */
884  tlist = add_to_flat_tlist(tlist,
885  pull_var_clause((Node *) foreignrel->reltarget->exprs,
887  tlist = add_to_flat_tlist(tlist,
888  pull_var_clause((Node *) fpinfo->local_conds,
890 
891  return tlist;
892 }
#define NIL
Definition: pg_list.h:69
RelOptKind reloptkind
Definition: relation.h:491
Definition: nodes.h:520
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
#define PVC_RECURSE_PLACEHOLDERS
Definition: var.h:26
List * exprs
Definition: relation.h:828
void * fdw_private
Definition: relation.h:545
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:135
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:505
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, 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  {
213  RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
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
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1660
#define lfirst(lc)
Definition: pg_list.h:106
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 2751 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().

2752 {
2753  StringInfo buf = context->buf;
2754  bool use_variadic;
2755 
2756  /* Only basic, non-split aggregation accepted. */
2757  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
2758 
2759  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2760  use_variadic = node->aggvariadic;
2761 
2762  /* Find aggregate name from aggfnoid which is a pg_proc entry */
2763  appendFunctionName(node->aggfnoid, context);
2764  appendStringInfoChar(buf, '(');
2765 
2766  /* Add DISTINCT */
2767  appendStringInfo(buf, "%s", (node->aggdistinct != NIL) ? "DISTINCT " : "");
2768 
2769  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
2770  {
2771  /* Add WITHIN GROUP (ORDER BY ..) */
2772  ListCell *arg;
2773  bool first = true;
2774 
2775  Assert(!node->aggvariadic);
2776  Assert(node->aggorder != NIL);
2777 
2778  foreach(arg, node->aggdirectargs)
2779  {
2780  if (!first)
2781  appendStringInfoString(buf, ", ");
2782  first = false;
2783 
2784  deparseExpr((Expr *) lfirst(arg), context);
2785  }
2786 
2787  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
2788  appendAggOrderBy(node->aggorder, node->args, context);
2789  }
2790  else
2791  {
2792  /* aggstar can be set only in zero-argument aggregates */
2793  if (node->aggstar)
2794  appendStringInfoChar(buf, '*');
2795  else
2796  {
2797  ListCell *arg;
2798  bool first = true;
2799 
2800  /* Add all the arguments */
2801  foreach(arg, node->args)
2802  {
2803  TargetEntry *tle = (TargetEntry *) lfirst(arg);
2804  Node *n = (Node *) tle->expr;
2805 
2806  if (tle->resjunk)
2807  continue;
2808 
2809  if (!first)
2810  appendStringInfoString(buf, ", ");
2811  first = false;
2812 
2813  /* Add VARIADIC */
2814  if (use_variadic && lnext(arg) == NULL)
2815  appendStringInfoString(buf, "VARIADIC ");
2816 
2817  deparseExpr((Expr *) n, context);
2818  }
2819  }
2820 
2821  /* Add ORDER BY */
2822  if (node->aggorder != NIL)
2823  {
2824  appendStringInfoString(buf, " ORDER BY ");
2825  appendAggOrderBy(node->aggorder, node->args, context);
2826  }
2827  }
2828 
2829  /* Add FILTER (WHERE ..) */
2830  if (node->aggfilter != NULL)
2831  {
2832  appendStringInfoString(buf, ") FILTER (WHERE ");
2833  deparseExpr((Expr *) node->aggfilter, context);
2834  }
2835 
2836  appendStringInfoChar(buf, ')');
2837 }
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:3020
Definition: nodes.h:520
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:1359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
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:2843
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:1352
AggSplit aggsplit
Definition: primnodes.h:310
Expr * aggfilter
Definition: primnodes.h:304
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2124
void * arg
char aggkind
Definition: primnodes.h:308
void deparseAnalyzeSizeSql ( StringInfo  buf,
Relation  rel 
)

Definition at line 1823 of file deparse.c.

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

Referenced by postgresAnalyzeForeignTable().

1824 {
1825  StringInfoData relname;
1826 
1827  /* We'll need the remote relation name as a literal. */
1828  initStringInfo(&relname);
1829  deparseRelation(&relname, rel);
1830 
1831  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
1832  deparseStringLiteral(buf, relname.data);
1833  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
1834 }
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:2089
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2049
void deparseAnalyzeSql ( StringInfo  buf,
Relation  rel,
List **  retrieved_attrs 
)

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

1844 {
1845  Oid relid = RelationGetRelid(rel);
1846  TupleDesc tupdesc = RelationGetDescr(rel);
1847  int i;
1848  char *colname;
1849  List *options;
1850  ListCell *lc;
1851  bool first = true;
1852 
1853  *retrieved_attrs = NIL;
1854 
1855  appendStringInfoString(buf, "SELECT ");
1856  for (i = 0; i < tupdesc->natts; i++)
1857  {
1858  /* Ignore dropped columns. */
1859  if (tupdesc->attrs[i]->attisdropped)
1860  continue;
1861 
1862  if (!first)
1863  appendStringInfoString(buf, ", ");
1864  first = false;
1865 
1866  /* Use attribute name or column_name option. */
1867  colname = NameStr(tupdesc->attrs[i]->attname);
1868  options = GetForeignColumnOptions(relid, i + 1);
1869 
1870  foreach(lc, options)
1871  {
1872  DefElem *def = (DefElem *) lfirst(lc);
1873 
1874  if (strcmp(def->defname, "column_name") == 0)
1875  {
1876  colname = defGetString(def);
1877  break;
1878  }
1879  }
1880 
1882 
1883  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1884  }
1885 
1886  /* Don't generate bad syntax for zero-column relation. */
1887  if (first)
1888  appendStringInfoString(buf, "NULL");
1889 
1890  /*
1891  * Construct FROM clause
1892  */
1893  appendStringInfoString(buf, " FROM ");
1894  deparseRelation(buf, rel);
1895 }
#define NIL
Definition: pg_list.h:69
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10114
#define RelationGetDescr(relation)
Definition: rel.h:425
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:2049
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define NameStr(name)
Definition: c.h:499
char * defname
Definition: parsenodes.h:708
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:413
static void deparseArrayExpr ( ArrayExpr node,
deparse_expr_cxt context 
)
static

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

2726 {
2727  StringInfo buf = context->buf;
2728  bool first = true;
2729  ListCell *lc;
2730 
2731  appendStringInfoString(buf, "ARRAY[");
2732  foreach(lc, node->elements)
2733  {
2734  if (!first)
2735  appendStringInfoString(buf, ", ");
2736  deparseExpr(lfirst(lc), context);
2737  first = false;
2738  }
2739  appendStringInfoChar(buf, ']');
2740 
2741  /* If the array is empty, we need an explicit cast to the array type. */
2742  if (node->elements == NIL)
2743  appendStringInfo(buf, "::%s",
2744  deparse_type_name(node->array_typeid, -1));
2745 }
#define NIL
Definition: pg_list.h:69
Oid array_typeid
Definition: primnodes.h:950
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:65
List * elements
Definition: primnodes.h:953
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:2124
static void deparseArrayRef ( ArrayRef node,
deparse_expr_cxt context 
)
static

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

2391 {
2392  StringInfo buf = context->buf;
2393  ListCell *lowlist_item;
2394  ListCell *uplist_item;
2395 
2396  /* Always parenthesize the expression. */
2397  appendStringInfoChar(buf, '(');
2398 
2399  /*
2400  * Deparse referenced array expression first. If that expression includes
2401  * a cast, we have to parenthesize to prevent the array subscript from
2402  * being taken as typename decoration. We can avoid that in the typical
2403  * case of subscripting a Var, but otherwise do it.
2404  */
2405  if (IsA(node->refexpr, Var))
2406  deparseExpr(node->refexpr, context);
2407  else
2408  {
2409  appendStringInfoChar(buf, '(');
2410  deparseExpr(node->refexpr, context);
2411  appendStringInfoChar(buf, ')');
2412  }
2413 
2414  /* Deparse subscript expressions. */
2415  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2416  foreach(uplist_item, node->refupperindexpr)
2417  {
2418  appendStringInfoChar(buf, '[');
2419  if (lowlist_item)
2420  {
2421  deparseExpr(lfirst(lowlist_item), context);
2422  appendStringInfoChar(buf, ':');
2423  lowlist_item = lnext(lowlist_item);
2424  }
2425  deparseExpr(lfirst(uplist_item), context);
2426  appendStringInfoChar(buf, ']');
2427  }
2428 
2429  appendStringInfoChar(buf, ')');
2430 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:571
Definition: primnodes.h:163
static char * buf
Definition: pg_test_fsync.c:65
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:2124
Expr * refexpr
Definition: primnodes.h:407
static void deparseBoolExpr ( BoolExpr node,
deparse_expr_cxt context 
)
static

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

2655 {
2656  StringInfo buf = context->buf;
2657  const char *op = NULL; /* keep compiler quiet */
2658  bool first;
2659  ListCell *lc;
2660 
2661  switch (node->boolop)
2662  {
2663  case AND_EXPR:
2664  op = "AND";
2665  break;
2666  case OR_EXPR:
2667  op = "OR";
2668  break;
2669  case NOT_EXPR:
2670  appendStringInfoString(buf, "(NOT ");
2671  deparseExpr(linitial(node->args), context);
2672  appendStringInfoChar(buf, ')');
2673  return;
2674  }
2675 
2676  appendStringInfoChar(buf, '(');
2677  first = true;
2678  foreach(lc, node->args)
2679  {
2680  if (!first)
2681  appendStringInfo(buf, " %s ", op);
2682  deparseExpr((Expr *) lfirst(lc), context);
2683  first = false;
2684  }
2685  appendStringInfoChar(buf, ')');
2686 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define linitial(l)
Definition: pg_list.h:110
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:65
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:2124
static void deparseColumnRef ( StringInfo  buf,
int  varno,
int  varattno,
PlannerInfo root,
bool  qualify_col 
)
static

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

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

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

Definition at line 1726 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1730 {
1731  appendStringInfoString(buf, "DELETE FROM ");
1732  deparseRelation(buf, rel);
1733  appendStringInfoString(buf, " WHERE ctid = $1");
1734 
1735  deparseReturningList(buf, root, rtindex, rel,
1736  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
1737  returningList, retrieved_attrs);
1738 }
TriggerDesc * trigdesc
Definition: rel.h:119
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:2049
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1782
void deparseDirectDeleteSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List remote_conds,
List **  params_list,
List returningList,
List **  retrieved_attrs 
)

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

1754 {
1755  RelOptInfo *baserel = root->simple_rel_array[rtindex];
1756  deparse_expr_cxt context;
1757 
1758  /* Set up context struct for recursion */
1759  context.root = root;
1760  context.foreignrel = baserel;
1761  context.scanrel = baserel;
1762  context.buf = buf;
1763  context.params_list = params_list;
1764 
1765  appendStringInfoString(buf, "DELETE FROM ");
1766  deparseRelation(buf, rel);
1767 
1768  if (remote_conds)
1769  {
1770  appendStringInfo(buf, " WHERE ");
1771  appendConditions(remote_conds, &context);
1772  }
1773 
1774  deparseReturningList(buf, root, rtindex, rel, false,
1775  returningList, retrieved_attrs);
1776 }
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:65
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1273
StringInfo buf
Definition: deparse.c:104
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2049
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:1782
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 1658 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().

1666 {
1667  RelOptInfo *baserel = root->simple_rel_array[rtindex];
1668  deparse_expr_cxt context;
1669  int nestlevel;
1670  bool first;
1671  ListCell *lc;
1672 
1673  /* Set up context struct for recursion */
1674  context.root = root;
1675  context.foreignrel = baserel;
1676  context.scanrel = baserel;
1677  context.buf = buf;
1678  context.params_list = params_list;
1679 
1680  appendStringInfoString(buf, "UPDATE ");
1681  deparseRelation(buf, rel);
1682  appendStringInfoString(buf, " SET ");
1683 
1684  /* Make sure any constants in the exprs are printed portably */
1685  nestlevel = set_transmission_modes();
1686 
1687  first = true;
1688  foreach(lc, targetAttrs)
1689  {
1690  int attnum = lfirst_int(lc);
1691  TargetEntry *tle = get_tle_by_resno(targetlist, attnum);
1692 
1693  if (!tle)
1694  elog(ERROR, "attribute number %d not found in UPDATE targetlist",
1695  attnum);
1696 
1697  if (!first)
1698  appendStringInfoString(buf, ", ");
1699  first = false;
1700 
1701  deparseColumnRef(buf, rtindex, attnum, root, false);
1702  appendStringInfoString(buf, " = ");
1703  deparseExpr((Expr *) tle->expr, &context);
1704  }
1705 
1706  reset_transmission_modes(nestlevel);
1707 
1708  if (remote_conds)
1709  {
1710  appendStringInfo(buf, " WHERE ");
1711  appendConditions(remote_conds, &context);
1712  }
1713 
1714  deparseReturningList(buf, root, rtindex, rel, false,
1715  returningList, retrieved_attrs);
1716 }
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:1904
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:65
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1273
StringInfo buf
Definition: deparse.c:104
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2049
PlannerInfo * root
Definition: deparse.c:99
Expr * expr
Definition: primnodes.h:1352
void reset_transmission_modes(int nestlevel)
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2124
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:1782
#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 2578 of file deparse.c.

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

Referenced by deparseExpr().

2579 {
2580  StringInfo buf = context->buf;
2581 
2582  Assert(list_length(node->args) == 2);
2583 
2584  appendStringInfoChar(buf, '(');
2585  deparseExpr(linitial(node->args), context);
2586  appendStringInfoString(buf, " IS DISTINCT FROM ");
2587  deparseExpr(lsecond(node->args), context);
2588  appendStringInfoChar(buf, ')');
2589 }
#define lsecond(l)
Definition: pg_list.h:114
#define linitial(l)
Definition: pg_list.h:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
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:2124
List * args
Definition: primnodes.h:501
static void deparseExplicitTargetList ( List tlist,
List **  retrieved_attrs,
deparse_expr_cxt context 
)
static

Definition at line 1348 of file deparse.c.

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

Referenced by deparseSelectSql().

1350 {
1351  ListCell *lc;
1352  StringInfo buf = context->buf;
1353  int i = 0;
1354 
1355  *retrieved_attrs = NIL;
1356 
1357  foreach(lc, tlist)
1358  {
1359  TargetEntry *tle = castNode(TargetEntry, lfirst(lc));
1360 
1361  if (i > 0)
1362  appendStringInfoString(buf, ", ");
1363  deparseExpr((Expr *) tle->expr, context);
1364 
1365  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1366  i++;
1367  }
1368 
1369  if (i == 0)
1370  appendStringInfoString(buf, "NULL");
1371 }
#define NIL
Definition: pg_list.h:69
#define castNode(_type_, nodeptr)
Definition: nodes.h:589
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
List * lappend_int(List *list, int datum)
Definition: list.c:146
StringInfo buf
Definition: deparse.c:104
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1352
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2124
int i
static void deparseExpr ( Expr expr,
deparse_expr_cxt context 
)
static

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

2125 {
2126  if (node == NULL)
2127  return;
2128 
2129  switch (nodeTag(node))
2130  {
2131  case T_Var:
2132  deparseVar((Var *) node, context);
2133  break;
2134  case T_Const:
2135  deparseConst((Const *) node, context, 0);
2136  break;
2137  case T_Param:
2138  deparseParam((Param *) node, context);
2139  break;
2140  case T_ArrayRef:
2141  deparseArrayRef((ArrayRef *) node, context);
2142  break;
2143  case T_FuncExpr:
2144  deparseFuncExpr((FuncExpr *) node, context);
2145  break;
2146  case T_OpExpr:
2147  deparseOpExpr((OpExpr *) node, context);
2148  break;
2149  case T_DistinctExpr:
2150  deparseDistinctExpr((DistinctExpr *) node, context);
2151  break;
2152  case T_ScalarArrayOpExpr:
2153  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2154  break;
2155  case T_RelabelType:
2156  deparseRelabelType((RelabelType *) node, context);
2157  break;
2158  case T_BoolExpr:
2159  deparseBoolExpr((BoolExpr *) node, context);
2160  break;
2161  case T_NullTest:
2162  deparseNullTest((NullTest *) node, context);
2163  break;
2164  case T_ArrayExpr:
2165  deparseArrayExpr((ArrayExpr *) node, context);
2166  break;
2167  case T_Aggref:
2168  deparseAggref((Aggref *) node, context);
2169  break;
2170  default:
2171  elog(ERROR, "unsupported expression type for deparse: %d",
2172  (int) nodeTag(node));
2173  break;
2174  }
2175 }
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:2751
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2654
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:2692
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2578
static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2390
Definition: primnodes.h:163
#define ERROR
Definition: elog.h:43
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2357
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2596
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2499
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2725
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:2641
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2186
Definition: nodes.h:145
Definition: nodes.h:144
#define NULL
Definition: c.h:229
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2251
#define nodeTag(nodeptr)
Definition: nodes.h:525
#define elog
Definition: elog.h:219
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2436
Definition: nodes.h:146
static void deparseFromExpr ( List quals,
deparse_expr_cxt context 
)
static

Definition at line 1059 of file deparse.c.

References appendConditions(), appendStringInfo(), appendStringInfoString(), Assert, bms_num_members(), buf, deparse_expr_cxt::buf, deparseFromExprForRel(), deparse_expr_cxt::foreignrel, NIL, deparse_expr_cxt::params_list, RelOptInfo::relids, RELOPT_BASEREL, RELOPT_JOINREL, RELOPT_UPPER_REL, RelOptInfo::reloptkind, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

Referenced by deparseSelectStmtForRel().

1060 {
1061  StringInfo buf = context->buf;
1062  RelOptInfo *scanrel = context->scanrel;
1063 
1064  /* For upper relations, scanrel must be either a joinrel or a baserel */
1066  scanrel->reloptkind == RELOPT_JOINREL ||
1067  scanrel->reloptkind == RELOPT_BASEREL);
1068 
1069  /* Construct FROM clause */
1070  appendStringInfoString(buf, " FROM ");
1071  deparseFromExprForRel(buf, context->root, scanrel,
1072  (bms_num_members(scanrel->relids) > 1),
1073  context->params_list);
1074 
1075  /* Construct WHERE clause */
1076  if (quals != NIL)
1077  {
1078  appendStringInfo(buf, " WHERE ");
1079  appendConditions(quals, context);
1080  }
1081 }
#define NIL
Definition: pg_list.h:69
RelOptKind reloptkind
Definition: relation.h:491
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list)
Definition: deparse.c:1415
List ** params_list
Definition: deparse.c:105
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:575
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
Relids relids
Definition: relation.h:494
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1273
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 1415 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, 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, RELOPT_JOINREL, RelOptInfo::reloptkind, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

Referenced by deparseFromExpr(), and deparseRangeTblRef().

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

2437 {
2438  StringInfo buf = context->buf;
2439  bool use_variadic;
2440  bool first;
2441  ListCell *arg;
2442 
2443  /*
2444  * If the function call came from an implicit coercion, then just show the
2445  * first argument.
2446  */
2447  if (node->funcformat == COERCE_IMPLICIT_CAST)
2448  {
2449  deparseExpr((Expr *) linitial(node->args), context);
2450  return;
2451  }
2452 
2453  /*
2454  * If the function call came from a cast, then show the first argument
2455  * plus an explicit cast operation.
2456  */
2457  if (node->funcformat == COERCE_EXPLICIT_CAST)
2458  {
2459  Oid rettype = node->funcresulttype;
2460  int32 coercedTypmod;
2461 
2462  /* Get the typmod if this is a length-coercion function */
2463  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2464 
2465  deparseExpr((Expr *) linitial(node->args), context);
2466  appendStringInfo(buf, "::%s",
2467  deparse_type_name(rettype, coercedTypmod));
2468  return;
2469  }
2470 
2471  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2472  use_variadic = node->funcvariadic;
2473 
2474  /*
2475  * Normal function: display as proname(args).
2476  */
2477  appendFunctionName(node->funcid, context);
2478  appendStringInfoChar(buf, '(');
2479 
2480  /* ... and all the arguments */
2481  first = true;
2482  foreach(arg, node->args)
2483  {
2484  if (!first)
2485  appendStringInfoString(buf, ", ");
2486  if (use_variadic && lnext(arg) == NULL)
2487  appendStringInfoString(buf, "VARIADIC ");
2488  deparseExpr((Expr *) lfirst(arg), context);
2489  first = false;
2490  }
2491  appendStringInfoChar(buf, ')');
2492 }
Oid funcresulttype
Definition: primnodes.h:449
List * args
Definition: primnodes.h:456
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3020
Definition: nodes.h:520
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:510
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:110
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:65
#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:2124
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 1553 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1557 {
1558  AttrNumber pindex;
1559  bool first;
1560  ListCell *lc;
1561 
1562  appendStringInfoString(buf, "INSERT INTO ");
1563  deparseRelation(buf, rel);
1564 
1565  if (targetAttrs)
1566  {
1567  appendStringInfoChar(buf, '(');
1568 
1569  first = true;
1570  foreach(lc, targetAttrs)
1571  {
1572  int attnum = lfirst_int(lc);
1573 
1574  if (!first)
1575  appendStringInfoString(buf, ", ");
1576  first = false;
1577 
1578  deparseColumnRef(buf, rtindex, attnum, root, false);
1579  }
1580 
1581  appendStringInfoString(buf, ") VALUES (");
1582 
1583  pindex = 1;
1584  first = true;
1585  foreach(lc, targetAttrs)
1586  {
1587  if (!first)
1588  appendStringInfoString(buf, ", ");
1589  first = false;
1590 
1591  appendStringInfo(buf, "$%d", pindex);
1592  pindex++;
1593  }
1594 
1595  appendStringInfoChar(buf, ')');
1596  }
1597  else
1598  appendStringInfoString(buf, " DEFAULT VALUES");
1599 
1600  if (doNothing)
1601  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1602 
1603  deparseReturningList(buf, root, rtindex, rel,
1604  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1605  returningList, retrieved_attrs);
1606 }
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1904
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:119
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:2049
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1782
int16 AttrNumber
Definition: attnum.h:21
static void deparseLockingClause ( deparse_expr_cxt context)
static

Definition at line 1186 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(), LCS_FORKEYSHARE, LCS_FORNOKEYUPDATE, LCS_FORSHARE, LCS_FORUPDATE, LCS_NONE, PgFdwRelationInfo::lower_subquery_rels, PlannerInfo::parse, REL_ALIAS_PREFIX, RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, Query::resultRelation, deparse_expr_cxt::root, PlannerInfo::rowMarks, deparse_expr_cxt::scanrel, and PlanRowMark::strength.

Referenced by deparseSelectStmtForRel().

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

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

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

Definition at line 2550 of file deparse.c.

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

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

2551 {
2552  char *opname;
2553 
2554  /* opname is not a SQL identifier, so we should not quote it. */
2555  opname = NameStr(opform->oprname);
2556 
2557  /* Print schema name only if it's not pg_catalog */
2558  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
2559  {
2560  const char *opnspname;
2561 
2562  opnspname = get_namespace_name(opform->oprnamespace);
2563  /* Print fully qualified operator name. */
2564  appendStringInfo(buf, "OPERATOR(%s.%s)",
2565  quote_identifier(opnspname), opname);
2566  }
2567  else
2568  {
2569  /* Just print operator name. */
2570  appendStringInfoString(buf, opname);
2571  }
2572 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10114
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:3006
#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 2499 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().

2500 {
2501  StringInfo buf = context->buf;
2502  HeapTuple tuple;
2503  Form_pg_operator form;
2504  char oprkind;
2505  ListCell *arg;
2506 
2507  /* Retrieve information about the operator from system catalog. */
2508  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2509  if (!HeapTupleIsValid(tuple))
2510  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2511  form = (Form_pg_operator) GETSTRUCT(tuple);
2512  oprkind = form->oprkind;
2513 
2514  /* Sanity check. */
2515  Assert((oprkind == 'r' && list_length(node->args) == 1) ||
2516  (oprkind == 'l' && list_length(node->args) == 1) ||
2517  (oprkind == 'b' && list_length(node->args) == 2));
2518 
2519  /* Always parenthesize the expression. */
2520  appendStringInfoChar(buf, '(');
2521 
2522  /* Deparse left operand. */
2523  if (oprkind == 'r' || oprkind == 'b')
2524  {
2525  arg = list_head(node->args);
2526  deparseExpr(lfirst(arg), context);
2527  appendStringInfoChar(buf, ' ');
2528  }
2529 
2530  /* Deparse operator name. */
2531  deparseOperatorName(buf, form);
2532 
2533  /* Deparse right operand. */
2534  if (oprkind == 'l' || oprkind == 'b')
2535  {
2536  arg = list_tail(node->args);
2537  appendStringInfoChar(buf, ' ');
2538  deparseExpr(lfirst(arg), context);
2539  }
2540 
2541  appendStringInfoChar(buf, ')');
2542 
2543  ReleaseSysCache(tuple);
2544 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2550
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:150
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:65
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:1093
#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:2124
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 2357 of file deparse.c.

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

Referenced by deparseExpr().

2358 {
2359  if (context->params_list)
2360  {
2361  int pindex = 0;
2362  ListCell *lc;
2363 
2364  /* find its index in params_list */
2365  foreach(lc, *context->params_list)
2366  {
2367  pindex++;
2368  if (equal(node, (Node *) lfirst(lc)))
2369  break;
2370  }
2371  if (lc == NULL)
2372  {
2373  /* not in list, so add it */
2374  pindex++;
2375  *context->params_list = lappend(*context->params_list, node);
2376  }
2377 
2378  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2379  }
2380  else
2381  {
2382  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2383  }
2384 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2934
List ** params_list
Definition: deparse.c:105
Definition: nodes.h:520
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2902
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:2928
static void deparseRangeTblRef ( StringInfo  buf,
PlannerInfo root,
RelOptInfo foreignrel,
bool  make_subquery,
List **  params_list 
)
static

Definition at line 1492 of file deparse.c.

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

Referenced by deparseFromExprForRel().

1494 {
1495  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1496 
1497  /* Should only be called in these cases. */
1498  Assert(foreignrel->reloptkind == RELOPT_BASEREL ||
1499  foreignrel->reloptkind == RELOPT_JOINREL);
1500 
1501  Assert(fpinfo->local_conds == NIL);
1502 
1503  /* If make_subquery is true, deparse the relation as a subquery. */
1504  if (make_subquery)
1505  {
1506  List *retrieved_attrs;
1507  int ncols;
1508 
1509  /* Deparse the subquery representing the relation. */
1510  appendStringInfoChar(buf, '(');
1511  deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1512  fpinfo->remote_conds, NIL, true,
1513  &retrieved_attrs, params_list);
1514  appendStringInfoChar(buf, ')');
1515 
1516  /* Append the relation alias. */
1518  fpinfo->relation_index);
1519 
1520  /*
1521  * Append the column aliases if needed. Note that the subquery emits
1522  * expressions specified in the relation's reltarget (see
1523  * deparseSubqueryTargetList).
1524  */
1525  ncols = list_length(foreignrel->reltarget->exprs);
1526  if (ncols > 0)
1527  {
1528  int i;
1529 
1530  appendStringInfoChar(buf, '(');
1531  for (i = 1; i <= ncols; i++)
1532  {
1533  if (i > 1)
1534  appendStringInfoString(buf, ", ");
1535 
1537  }
1538  appendStringInfoChar(buf, ')');
1539  }
1540  }
1541  else
1542  deparseFromExprForRel(buf, root, foreignrel, true, params_list);
1543 }
#define NIL
Definition: pg_list.h:69
RelOptKind reloptkind
Definition: relation.h:491
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list)
Definition: deparse.c:1415
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:828
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:919
void * fdw_private
Definition: relation.h:545
#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:505
static void deparseRelabelType ( RelabelType node,
deparse_expr_cxt context 
)
static

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

2642 {
2643  deparseExpr(node->arg, context);
2644  if (node->relabelformat != COERCE_IMPLICIT_CAST)
2645  appendStringInfo(context->buf, "::%s",
2647  node->resulttypmod));
2648 }
Expr * arg
Definition: primnodes.h:789
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
StringInfo buf
Definition: deparse.c:104
Oid resulttype
Definition: primnodes.h:790
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:2124
int32 resulttypmod
Definition: primnodes.h:791
CoercionForm relabelformat
Definition: primnodes.h:793
static void deparseRelation ( StringInfo  buf,
Relation  rel 
)
static

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

2050 {
2051  ForeignTable *table;
2052  const char *nspname = NULL;
2053  const char *relname = NULL;
2054  ListCell *lc;
2055 
2056  /* obtain additional catalog information. */
2057  table = GetForeignTable(RelationGetRelid(rel));
2058 
2059  /*
2060  * Use value of FDW options if any, instead of the name of object itself.
2061  */
2062  foreach(lc, table->options)
2063  {
2064  DefElem *def = (DefElem *) lfirst(lc);
2065 
2066  if (strcmp(def->defname, "schema_name") == 0)
2067  nspname = defGetString(def);
2068  else if (strcmp(def->defname, "table_name") == 0)
2069  relname = defGetString(def);
2070  }
2071 
2072  /*
2073  * Note: we could skip printing the schema name if it's pg_catalog, but
2074  * that doesn't seem worth the trouble.
2075  */
2076  if (nspname == NULL)
2077  nspname = get_namespace_name(RelationGetNamespace(rel));
2078  if (relname == NULL)
2079  relname = RelationGetRelationName(rel);
2080 
2081  appendStringInfo(buf, "%s.%s",
2082  quote_identifier(nspname), quote_identifier(relname));
2083 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10114
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:3006
#define RelationGetRelationName(relation)
Definition: rel.h:433
#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:708
#define RelationGetRelid(relation)
Definition: rel.h:413
#define RelationGetNamespace(relation)
Definition: rel.h:440
static void deparseReturningList ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
bool  trig_after_row,
List returningList,
List **  retrieved_attrs 
)
static

Definition at line 1782 of file deparse.c.

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

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

1787 {
1788  Bitmapset *attrs_used = NULL;
1789 
1790  if (trig_after_row)
1791  {
1792  /* whole-row reference acquires all non-system columns */
1793  attrs_used =
1795  }
1796 
1797  if (returningList != NIL)
1798  {
1799  /*
1800  * We need the attrs, non-system and system, mentioned in the local
1801  * query's RETURNING list.
1802  */
1803  pull_varattnos((Node *) returningList, rtindex,
1804  &attrs_used);
1805  }
1806 
1807  if (attrs_used != NULL)
1808  deparseTargetList(buf, root, rtindex, rel, true, attrs_used, false,
1809  retrieved_attrs);
1810  else
1811  *retrieved_attrs = NIL;
1812 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:520
#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:1094
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:178
#define NULL
Definition: c.h:229
static void deparseScalarArrayOpExpr ( ScalarArrayOpExpr node,
deparse_expr_cxt context 
)
static

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

2597 {
2598  StringInfo buf = context->buf;
2599  HeapTuple tuple;
2600  Form_pg_operator form;
2601  Expr *arg1;
2602  Expr *arg2;
2603 
2604  /* Retrieve information about the operator from system catalog. */
2605  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2606  if (!HeapTupleIsValid(tuple))
2607  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2608  form = (Form_pg_operator) GETSTRUCT(tuple);
2609 
2610  /* Sanity check. */
2611  Assert(list_length(node->args) == 2);
2612 
2613  /* Always parenthesize the expression. */
2614  appendStringInfoChar(buf, '(');
2615 
2616  /* Deparse left operand. */
2617  arg1 = linitial(node->args);
2618  deparseExpr(arg1, context);
2619  appendStringInfoChar(buf, ' ');
2620 
2621  /* Deparse operator name plus decoration. */
2622  deparseOperatorName(buf, form);
2623  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
2624 
2625  /* Deparse right operand. */
2626  arg2 = lsecond(node->args);
2627  deparseExpr(arg2, context);
2628 
2629  appendStringInfoChar(buf, ')');
2630 
2631  /* Always parenthesize the expression. */
2632  appendStringInfoChar(buf, ')');
2633 
2634  ReleaseSysCache(tuple);
2635 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2550
#define lsecond(l)
Definition: pg_list.h:114
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:150
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define linitial(l)
Definition: pg_list.h:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:65
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
StringInfo buf
Definition: deparse.c:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1093
#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:2124
#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 1001 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(), NoLock, planner_rt_fetch, RelOptInfo::relid, RangeTblEntry::relid, RELOPT_JOINREL, RELOPT_UPPER_REL, RelOptInfo::reloptkind, and deparse_expr_cxt::root.

Referenced by deparseSelectStmtForRel().

1003 {
1004  StringInfo buf = context->buf;
1005  RelOptInfo *foreignrel = context->foreignrel;
1006  PlannerInfo *root = context->root;
1007  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1008 
1009  /*
1010  * Construct SELECT list
1011  */
1012  appendStringInfoString(buf, "SELECT ");
1013 
1014  if (is_subquery)
1015  {
1016  /*
1017  * For a relation that is deparsed as a subquery, emit expressions
1018  * specified in the relation's reltarget. Note that since this is
1019  * for the subquery, no need to care about *retrieved_attrs.
1020  */
1021  deparseSubqueryTargetList(context);
1022  }
1023  else if (foreignrel->reloptkind == RELOPT_JOINREL ||
1024  foreignrel->reloptkind == RELOPT_UPPER_REL)
1025  {
1026  /*
1027  * For a join or upper relation the input tlist gives the list of
1028  * columns required to be fetched from the foreign server.
1029  */
1030  deparseExplicitTargetList(tlist, retrieved_attrs, context);
1031  }
1032  else
1033  {
1034  /*
1035  * For a base relation fpinfo->attrs_used gives the list of columns
1036  * required to be fetched from the foreign server.
1037  */
1038  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1039 
1040  /*
1041  * Core code already has some lock on each rel being planned, so we
1042  * can use NoLock here.
1043  */
1044  Relation rel = heap_open(rte->relid, NoLock);
1045 
1046  deparseTargetList(buf, root, foreignrel->relid, rel, false,
1047  fpinfo->attrs_used, false, retrieved_attrs);
1048  heap_close(rel, NoLock);
1049  }
1050 }
RelOptKind reloptkind
Definition: relation.h:491
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1379
#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:1094
#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:1348
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:65
Index relid
Definition: relation.h:522
StringInfo buf
Definition: deparse.c:104
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
void * fdw_private
Definition: relation.h:545
PlannerInfo * root
Definition: deparse.c:99
Bitmapset * attrs_used
Definition: postgres_fdw.h:52
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 919 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, PgFdwRelationInfo::outerrel, deparse_expr_cxt::params_list, RELOPT_BASEREL, RELOPT_JOINREL, RELOPT_OTHER_MEMBER_REL, RELOPT_UPPER_REL, RelOptInfo::reloptkind, PgFdwRelationInfo::remote_conds, deparse_expr_cxt::root, and deparse_expr_cxt::scanrel.

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

923 {
924  deparse_expr_cxt context;
926  List *quals;
927 
928  /*
929  * We handle relations for foreign tables, joins between those and upper
930  * relations.
931  */
932  Assert(rel->reloptkind == RELOPT_JOINREL ||
933  rel->reloptkind == RELOPT_BASEREL ||
935  rel->reloptkind == RELOPT_UPPER_REL);
936 
937  /* Fill portions of context common to upper, join and base relation */
938  context.buf = buf;
939  context.root = root;
940  context.foreignrel = rel;
941  context.scanrel = (rel->reloptkind == RELOPT_UPPER_REL) ?
942  fpinfo->outerrel : rel;
943  context.params_list = params_list;
944 
945  /* Construct SELECT clause */
946  deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
947 
948  /*
949  * For upper relations, the WHERE clause is built from the remote
950  * conditions of the underlying scan relation; otherwise, we can use the
951  * supplied list of remote conditions directly.
952  */
953  if (rel->reloptkind == RELOPT_UPPER_REL)
954  {
955  PgFdwRelationInfo *ofpinfo;
956 
957  ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
958  quals = ofpinfo->remote_conds;
959  }
960  else
961  quals = remote_conds;
962 
963  /* Construct FROM and WHERE clauses */
964  deparseFromExpr(quals, &context);
965 
966  if (rel->reloptkind == RELOPT_UPPER_REL)
967  {
968  /* Append GROUP BY clause */
969  appendGroupByClause(tlist, &context);
970 
971  /* Append HAVING clause */
972  if (remote_conds)
973  {
974  appendStringInfo(buf, " HAVING ");
975  appendConditions(remote_conds, &context);
976  }
977  }
978 
979  /* Add ORDER BY clause if we found any useful pathkeys */
980  if (pathkeys)
981  appendOrderByClause(pathkeys, &context);
982 
983  /* Add any necessary FOR UPDATE/SHARE. */
984  deparseLockingClause(&context);
985 }
static void appendGroupByClause(List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:2941
RelOptKind reloptkind
Definition: relation.h:491
List ** params_list
Definition: deparse.c:105
RelOptInfo * outerrel
Definition: postgres_fdw.h:91
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static void appendOrderByClause(List *pathkeys, deparse_expr_cxt *context)
Definition: deparse.c:2978
static char * buf
Definition: pg_test_fsync.c:65
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1001
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1273
StringInfo buf
Definition: deparse.c:104
void * fdw_private
Definition: relation.h:545
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:1059
static void deparseLockingClause(deparse_expr_cxt *context)
Definition: deparse.c:1186
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 3055 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().

3056 {
3057  StringInfo buf = context->buf;
3058  TargetEntry *tle;
3059  Expr *expr;
3060 
3061  tle = get_sortgroupref_tle(ref, tlist);
3062  expr = tle->expr;
3063 
3064  if (expr && IsA(expr, Const))
3065  {
3066  /*
3067  * Force a typecast here so that we don't emit something like "GROUP
3068  * BY 2", which will be misconstrued as a column position rather than
3069  * a constant.
3070  */
3071  deparseConst((Const *) expr, context, 1);
3072  }
3073  else if (!expr || IsA(expr, Var))
3074  deparseExpr(expr, context);
3075  else
3076  {
3077  /* Always parenthesize the expression. */
3078  appendStringInfoString(buf, "(");
3079  deparseExpr(expr, context);
3080  appendStringInfoString(buf, ")");
3081  }
3082 
3083  return (Node *) expr;
3084 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:571
Definition: nodes.h:520
Definition: primnodes.h:163
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
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:2251
Expr * expr
Definition: primnodes.h:1352
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2124
void deparseStringLiteral ( StringInfo  buf,
const char *  val 
)

Definition at line 2089 of file deparse.c.

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

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

2090 {
2091  const char *valptr;
2092 
2093  /*
2094  * Rather than making assumptions about the remote server's value of
2095  * standard_conforming_strings, always use E'foo' syntax if there are any
2096  * backslashes. This will fail on remote servers before 8.1, but those
2097  * are long out of support.
2098  */
2099  if (strchr(val, '\\') != NULL)
2101  appendStringInfoChar(buf, '\'');
2102  for (valptr = val; *valptr; valptr++)
2103  {
2104  char ch = *valptr;
2105 
2106  if (SQL_STR_DOUBLE(ch, true))
2107  appendStringInfoChar(buf, ch);
2108  appendStringInfoChar(buf, ch);
2109  }
2110  appendStringInfoChar(buf, '\'');
2111 }
#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 1379 of file deparse.c.

References appendStringInfoString(), Assert, buf, deparse_expr_cxt::buf, deparseExpr(), PathTarget::exprs, deparse_expr_cxt::foreignrel, lfirst, RELOPT_BASEREL, RELOPT_JOINREL, RelOptInfo::reloptkind, and RelOptInfo::reltarget.

Referenced by deparseSelectSql().

1380 {
1381  StringInfo buf = context->buf;
1382  RelOptInfo *foreignrel = context->foreignrel;
1383  bool first;
1384  ListCell *lc;
1385 
1386  /* Should only be called in these cases. */
1387  Assert(foreignrel->reloptkind == RELOPT_BASEREL ||
1388  foreignrel->reloptkind == RELOPT_JOINREL);
1389 
1390  first = true;
1391  foreach(lc, foreignrel->reltarget->exprs)
1392  {
1393  Node *node = (Node *) lfirst(lc);
1394 
1395  if (!first)
1396  appendStringInfoString(buf, ", ");
1397  first = false;
1398 
1399  deparseExpr((Expr *) node, context);
1400  }
1401 
1402  /* Don't generate bad syntax if no expressions */
1403  if (first)
1404  appendStringInfoString(buf, "NULL");
1405 }
RelOptKind reloptkind
Definition: relation.h:491
Definition: nodes.h:520
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
List * exprs
Definition: relation.h:828
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:2124
RelOptInfo * foreignrel
Definition: deparse.c:100
struct PathTarget * reltarget
Definition: relation.h:505
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 1094 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().

1102 {
1103  TupleDesc tupdesc = RelationGetDescr(rel);
1104  bool have_wholerow;
1105  bool first;
1106  int i;
1107 
1108  *retrieved_attrs = NIL;
1109 
1110  /* If there's a whole-row reference, we'll need all the columns. */
1112  attrs_used);
1113 
1114  first = true;
1115  for (i = 1; i <= tupdesc->natts; i++)
1116  {
1117  Form_pg_attribute attr = tupdesc->attrs[i - 1];
1118 
1119  /* Ignore dropped attributes. */
1120  if (attr->attisdropped)
1121  continue;
1122 
1123  if (have_wholerow ||
1125  attrs_used))
1126  {
1127  if (!first)
1128  appendStringInfoString(buf, ", ");
1129  else if (is_returning)
1130  appendStringInfoString(buf, " RETURNING ");
1131  first = false;
1132 
1133  deparseColumnRef(buf, rtindex, i, root, qualify_col);
1134 
1135  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1136  }
1137  }
1138 
1139  /*
1140  * Add ctid and oid if needed. We currently don't support retrieving any
1141  * other system columns.
1142  */
1144  attrs_used))
1145  {
1146  if (!first)
1147  appendStringInfoString(buf, ", ");
1148  else if (is_returning)
1149  appendStringInfoString(buf, " RETURNING ");
1150  first = false;
1151 
1152  if (qualify_col)
1153  ADD_REL_QUALIFIER(buf, rtindex);
1154  appendStringInfoString(buf, "ctid");
1155 
1156  *retrieved_attrs = lappend_int(*retrieved_attrs,
1158  }
1160  attrs_used))
1161  {
1162  if (!first)
1163  appendStringInfoString(buf, ", ");
1164  else if (is_returning)
1165  appendStringInfoString(buf, " RETURNING ");
1166  first = false;
1167 
1168  if (qualify_col)
1169  ADD_REL_QUALIFIER(buf, rtindex);
1170  appendStringInfoString(buf, "oid");
1171 
1172  *retrieved_attrs = lappend_int(*retrieved_attrs,
1174  }
1175 
1176  /* Don't generate bad syntax if no undropped columns */
1177  if (first && !is_returning)
1178  appendStringInfoString(buf, "NULL");
1179 }
#define NIL
Definition: pg_list.h:69
#define RelationGetDescr(relation)
Definition: rel.h:425
#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:1904
#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:184
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:419
void deparseUpdateSql ( StringInfo  buf,
PlannerInfo root,
Index  rtindex,
Relation  rel,
List targetAttrs,
List returningList,
List **  retrieved_attrs 
)

Definition at line 1616 of file deparse.c.

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

Referenced by postgresPlanForeignModify().

1620 {
1621  AttrNumber pindex;
1622  bool first;
1623  ListCell *lc;
1624 
1625  appendStringInfoString(buf, "UPDATE ");
1626  deparseRelation(buf, rel);
1627  appendStringInfoString(buf, " SET ");
1628 
1629  pindex = 2; /* ctid is always the first param */
1630  first = true;
1631  foreach(lc, targetAttrs)
1632  {
1633  int attnum = lfirst_int(lc);
1634 
1635  if (!first)
1636  appendStringInfoString(buf, ", ");
1637  first = false;
1638 
1639  deparseColumnRef(buf, rtindex, attnum, root, false);
1640  appendStringInfo(buf, " = $%d", pindex);
1641  pindex++;
1642  }
1643  appendStringInfoString(buf, " WHERE ctid = $1");
1644 
1645  deparseReturningList(buf, root, rtindex, rel,
1646  rel->trigdesc && rel->trigdesc->trig_update_after_row,
1647  returningList, retrieved_attrs);
1648 }
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1904
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:119
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:2049
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1782
int16 AttrNumber
Definition: attnum.h:21
static void deparseVar ( Var node,
deparse_expr_cxt context 
)
static

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

2187 {
2188  Relids relids = context->scanrel->relids;
2189  int relno;
2190  int colno;
2191 
2192  /* Qualify columns when multiple relations are involved. */
2193  bool qualify_col = (bms_num_members(relids) > 1);
2194 
2195  /*
2196  * If the Var belongs to the foreign relation that is deparsed as a
2197  * subquery, use the relation and column alias to the Var provided
2198  * by the subquery, instead of the remote name.
2199  */
2200  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2201  {
2202  appendStringInfo(context->buf, "%s%d.%s%d",
2204  SUBQUERY_COL_ALIAS_PREFIX, colno);
2205  return;
2206  }
2207 
2208  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2209  deparseColumnRef(context->buf, node->varno, node->varattno,
2210  context->root, qualify_col);
2211  else
2212  {
2213  /* Treat like a Param */
2214  if (context->params_list)
2215  {
2216  int pindex = 0;
2217  ListCell *lc;
2218 
2219  /* find its index in params_list */
2220  foreach(lc, *context->params_list)
2221  {
2222  pindex++;
2223  if (equal(node, (Node *) lfirst(lc)))
2224  break;
2225  }
2226  if (lc == NULL)
2227  {
2228  /* not in list, so add it */
2229  pindex++;
2230  *context->params_list = lappend(*context->params_list, node);
2231  }
2232 
2233  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2234  }
2235  else
2236  {
2237  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2238  }
2239  }
2240 }
Index varlevelsup
Definition: primnodes.h:173
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2934
List ** params_list
Definition: deparse.c:105
Definition: nodes.h:520
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1904
AttrNumber varattno
Definition: primnodes.h:168
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2902
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:575
Relids relids
Definition: relation.h:494
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:3093
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:419
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2928
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(), 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, RELOPT_UPPER_REL, RelOptInfo::reloptkind, 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 (glob_cxt->foreignrel->reloptkind != RELOPT_UPPER_REL)
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:571
#define OperatorRelationId
Definition: pg_operator.h:32
Index varlevelsup
Definition: primnodes.h:173
RelOptKind reloptkind
Definition: relation.h:491
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
Index tleSortGroupRef
Definition: parsenodes.h:1141
Expr * arg
Definition: primnodes.h:789
Oid inputcollid
Definition: primnodes.h:297
Definition: nodes.h:520
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:1178
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:953
#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:303
Definition: nodes.h:145
Index varno
Definition: primnodes.h:166
Definition: nodes.h:144
Oid resultcollid
Definition: primnodes.h:792
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:545
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:1352
AggSplit aggsplit
Definition: primnodes.h:310
Oid array_collid
Definition: primnodes.h:951
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:525
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:419
Expr * refexpr
Definition: primnodes.h:407
Definition: nodes.h:146
const char* get_jointype_name ( JoinType  jointype)

Definition at line 1314 of file deparse.c.

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

Referenced by deparseFromExprForRel(), and foreign_join_ok().

1315 {
1316  switch (jointype)
1317  {
1318  case JOIN_INNER:
1319  return "INNER";
1320 
1321  case JOIN_LEFT:
1322  return "LEFT";
1323 
1324  case JOIN_RIGHT:
1325  return "RIGHT";
1326 
1327  case JOIN_FULL:
1328  return "FULL";
1329 
1330  default:
1331  /* Shouldn't come here, but protect from buggy code. */
1332  elog(ERROR, "unsupported join type %d", jointype);
1333  }
1334 
1335  /* Keep compiler happy */
1336  return NULL;
1337 }
#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 3157 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().

3159 {
3160  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3161  int i;
3162  ListCell *lc;
3163 
3164  /* Get the relation alias ID */
3165  *relno = fpinfo->relation_index;
3166 
3167  /* Get the column alias ID */
3168  i = 1;
3169  foreach(lc, foreignrel->reltarget->exprs)
3170  {
3171  if (equal(lfirst(lc), (Node *) node))
3172  {
3173  *colno = i;
3174  return;
3175  }
3176  i++;
3177  }
3178 
3179  /* Shouldn't get here */
3180  elog(ERROR, "unexpected expression in subquery output");
3181 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2934
Definition: nodes.h:520
#define ERROR
Definition: elog.h:43
List * exprs
Definition: relation.h:828
void * fdw_private
Definition: relation.h:545
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define elog
Definition: elog.h:219
struct PathTarget * reltarget
Definition: relation.h:505
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, PgFdwRelationInfo::outerrel, foreign_glob_cxt::relids, RelOptInfo::relids, RELOPT_UPPER_REL, RelOptInfo::reloptkind, 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 (baserel->reloptkind == RELOPT_UPPER_REL)
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 }
RelOptKind reloptkind
Definition: relation.h:491
FDWCollateState state
Definition: deparse.c:91
Definition: nodes.h:520
RelOptInfo * outerrel
Definition: postgres_fdw.h:91
Relids relids
Definition: deparse.c:70
PlannerInfo * root
Definition: deparse.c:68
RelOptInfo * foreignrel
Definition: deparse.c:69
Relids relids
Definition: relation.h:494
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:545
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:877
static bool is_subquery_var ( Var node,
RelOptInfo foreignrel,
int *  relno,
int *  colno 
)
static

Definition at line 3093 of file deparse.c.

References Assert, bms_is_member(), RelOptInfo::fdw_private, get_relation_column_alias_ids(), PgFdwRelationInfo::innerrel, PgFdwRelationInfo::lower_subquery_rels, PgFdwRelationInfo::make_innerrel_subquery, PgFdwRelationInfo::make_outerrel_subquery, PgFdwRelationInfo::outerrel, RELOPT_BASEREL, RELOPT_JOINREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and Var::varno.

Referenced by deparseVar().

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

Definition at line 2902 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

2904 {
2905  StringInfo buf = context->buf;
2906  char *ptypename = deparse_type_name(paramtype, paramtypmod);
2907 
2908  appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
2909 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static char * buf
Definition: pg_test_fsync.c:65
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 2928 of file deparse.c.

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

Referenced by deparseParam(), and deparseVar().

2930 {
2931  StringInfo buf = context->buf;
2932  char *ptypename = deparse_type_name(paramtype, paramtypmod);
2933 
2934  appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename);
2935 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static char * buf
Definition: pg_test_fsync.c:65
StringInfo buf
Definition: deparse.c:104
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851