PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
deparse.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * deparse.c
4  * Query deparser for postgres_fdw
5  *
6  * This file includes functions that examine query WHERE clauses to see
7  * whether they're safe to send to the remote server for execution, as
8  * well as functions to construct the query text to be sent. The latter
9  * functionality is annoyingly duplicative of ruleutils.c, but there are
10  * enough special considerations that it seems best to keep this separate.
11  * One saving grace is that we only need deparse logic for node types that
12  * we consider safe to send.
13  *
14  * We assume that the remote session's search_path is exactly "pg_catalog",
15  * and thus we need schema-qualify all and only names outside pg_catalog.
16  *
17  * We do not consider that it is ever safe to send COLLATE expressions to
18  * the remote server: it might not have the same collation names we do.
19  * (Later we might consider it safe to send COLLATE "C", but even that would
20  * fail on old remote servers.) An expression is considered safe to send
21  * only if all operator/function input collations used in it are traceable to
22  * Var(s) of the foreign table. That implies that if the remote server gets
23  * a different answer than we do, the foreign table's columns are not marked
24  * with collations that match the remote table's columns, which we can
25  * consider to be user error.
26  *
27  * Portions Copyright (c) 2012-2017, PostgreSQL Global Development Group
28  *
29  * IDENTIFICATION
30  * contrib/postgres_fdw/deparse.c
31  *
32  *-------------------------------------------------------------------------
33  */
34 #include "postgres.h"
35 
36 #include "postgres_fdw.h"
37 
38 #include "access/heapam.h"
39 #include "access/htup_details.h"
40 #include "access/sysattr.h"
41 #include "catalog/pg_aggregate.h"
42 #include "catalog/pg_collation.h"
43 #include "catalog/pg_namespace.h"
44 #include "catalog/pg_operator.h"
45 #include "catalog/pg_proc.h"
46 #include "catalog/pg_type.h"
47 #include "commands/defrem.h"
48 #include "nodes/makefuncs.h"
49 #include "nodes/nodeFuncs.h"
50 #include "nodes/plannodes.h"
51 #include "optimizer/clauses.h"
52 #include "optimizer/prep.h"
53 #include "optimizer/tlist.h"
54 #include "optimizer/var.h"
55 #include "parser/parsetree.h"
56 #include "utils/builtins.h"
57 #include "utils/lsyscache.h"
58 #include "utils/rel.h"
59 #include "utils/syscache.h"
60 #include "utils/typcache.h"
61 
62 
63 /*
64  * Global context for foreign_expr_walker's search of an expression tree.
65  */
66 typedef struct foreign_glob_cxt
67 {
68  PlannerInfo *root; /* global planner state */
69  RelOptInfo *foreignrel; /* the foreign relation we are planning for */
70  Relids relids; /* relids of base relations in the underlying
71  * scan */
73 
74 /*
75  * Local (per-tree-level) context for foreign_expr_walker's search.
76  * This is concerned with identifying collations used in the expression.
77  */
78 typedef enum
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 */
87 
88 typedef struct foreign_loc_cxt
89 {
90  Oid collation; /* OID of current collation, if any */
91  FDWCollateState state; /* state of current collation choice */
93 
94 /*
95  * Context for deparseExpr
96  */
97 typedef struct deparse_expr_cxt
98 {
99  PlannerInfo *root; /* global planner state */
100  RelOptInfo *foreignrel; /* the foreign relation we are planning for */
101  RelOptInfo *scanrel; /* the underlying scan relation. Same as
102  * foreignrel, when that represents a join or
103  * a base relation. */
104  StringInfo buf; /* output buffer to append to */
105  List **params_list; /* exprs that will become remote Params */
107 
108 #define REL_ALIAS_PREFIX "r"
109 /* Handy macro to add relation name qualification */
110 #define ADD_REL_QUALIFIER(buf, varno) \
111  appendStringInfo((buf), "%s%d.", REL_ALIAS_PREFIX, (varno))
112 #define SUBQUERY_REL_ALIAS_PREFIX "s"
113 #define SUBQUERY_COL_ALIAS_PREFIX "c"
114 
115 /*
116  * Functions to determine whether an expression can be evaluated safely on
117  * remote server.
118  */
119 static bool foreign_expr_walker(Node *node,
120  foreign_glob_cxt *glob_cxt,
121  foreign_loc_cxt *outer_cxt);
122 static char *deparse_type_name(Oid type_oid, int32 typemod);
123 
124 /*
125  * Functions to construct string representation of a node tree.
126  */
127 static void deparseTargetList(StringInfo buf,
128  PlannerInfo *root,
129  Index rtindex,
130  Relation rel,
131  bool is_returning,
132  Bitmapset *attrs_used,
133  bool qualify_col,
134  List **retrieved_attrs);
135 static void deparseExplicitTargetList(List *tlist, List **retrieved_attrs,
136  deparse_expr_cxt *context);
137 static void deparseSubqueryTargetList(deparse_expr_cxt *context);
139  Index rtindex, Relation rel,
140  bool trig_after_row,
141  List *returningList,
142  List **retrieved_attrs);
143 static void deparseColumnRef(StringInfo buf, int varno, int varattno,
144  PlannerInfo *root, bool qualify_col);
145 static void deparseRelation(StringInfo buf, Relation rel);
146 static void deparseExpr(Expr *expr, deparse_expr_cxt *context);
147 static void deparseVar(Var *node, deparse_expr_cxt *context);
148 static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype);
149 static void deparseParam(Param *node, deparse_expr_cxt *context);
150 static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context);
151 static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context);
152 static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context);
154 static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context);
156  deparse_expr_cxt *context);
157 static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context);
158 static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context);
159 static void deparseNullTest(NullTest *node, deparse_expr_cxt *context);
160 static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context);
161 static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
162  deparse_expr_cxt *context);
163 static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
164  deparse_expr_cxt *context);
165 static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs,
166  deparse_expr_cxt *context);
167 static void deparseLockingClause(deparse_expr_cxt *context);
168 static void appendOrderByClause(List *pathkeys, deparse_expr_cxt *context);
169 static void appendConditions(List *exprs, deparse_expr_cxt *context);
171  RelOptInfo *joinrel, bool use_alias, List **params_list);
172 static void deparseFromExpr(List *quals, deparse_expr_cxt *context);
173 static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root,
174  RelOptInfo *foreignrel, bool make_subquery,
175  List **params_list);
176 static void deparseAggref(Aggref *node, deparse_expr_cxt *context);
177 static void appendGroupByClause(List *tlist, deparse_expr_cxt *context);
178 static void appendAggOrderBy(List *orderList, List *targetList,
179  deparse_expr_cxt *context);
180 static void appendFunctionName(Oid funcid, deparse_expr_cxt *context);
181 static Node *deparseSortGroupClause(Index ref, List *tlist,
182  deparse_expr_cxt *context);
183 
184 /*
185  * Helper functions
186  */
187 static bool is_subquery_var(Var *node, RelOptInfo *foreignrel,
188  int *relno, int *colno);
189 static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel,
190  int *relno, int *colno);
191 
192 
193 /*
194  * Examine each qual clause in input_conds, and classify them into two groups,
195  * which are returned as two lists:
196  * - remote_conds contains expressions that can be evaluated remotely
197  * - local_conds contains expressions that can't be evaluated remotely
198  */
199 void
201  RelOptInfo *baserel,
202  List *input_conds,
203  List **remote_conds,
204  List **local_conds)
205 {
206  ListCell *lc;
207 
208  *remote_conds = NIL;
209  *local_conds = NIL;
210 
211  foreach(lc, input_conds)
212  {
214 
215  if (is_foreign_expr(root, baserel, ri->clause))
216  *remote_conds = lappend(*remote_conds, ri);
217  else
218  *local_conds = lappend(*local_conds, ri);
219  }
220 }
221 
222 /*
223  * Returns true if given expr is safe to evaluate on the foreign server.
224  */
225 bool
227  RelOptInfo *baserel,
228  Expr *expr)
229 {
230  foreign_glob_cxt glob_cxt;
231  foreign_loc_cxt loc_cxt;
232  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
233 
234  /*
235  * Check that the expression consists of nodes that are safe to execute
236  * remotely.
237  */
238  glob_cxt.root = root;
239  glob_cxt.foreignrel = baserel;
240 
241  /*
242  * For an upper relation, use relids from its underneath scan relation,
243  * because the upperrel's own relids currently aren't set to anything
244  * meaningful by the core code. For other relation, use their own relids.
245  */
246  if (IS_UPPER_REL(baserel))
247  glob_cxt.relids = fpinfo->outerrel->relids;
248  else
249  glob_cxt.relids = baserel->relids;
250  loc_cxt.collation = InvalidOid;
251  loc_cxt.state = FDW_COLLATE_NONE;
252  if (!foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt))
253  return false;
254 
255  /*
256  * If the expression has a valid collation that does not arise from a
257  * foreign var, the expression can not be sent over.
258  */
259  if (loc_cxt.state == FDW_COLLATE_UNSAFE)
260  return false;
261 
262  /*
263  * An expression which includes any mutable functions can't be sent over
264  * because its result is not stable. For example, sending now() remote
265  * side could cause confusion from clock offsets. Future versions might
266  * be able to make this choice with more granularity. (We check this last
267  * because it requires a lot of expensive catalog lookups.)
268  */
269  if (contain_mutable_functions((Node *) expr))
270  return false;
271 
272  /* OK to evaluate on the remote server */
273  return true;
274 }
275 
276 /*
277  * Check if expression is safe to execute remotely, and return true if so.
278  *
279  * In addition, *outer_cxt is updated with collation information.
280  *
281  * We must check that the expression contains only node types we can deparse,
282  * that all types/functions/operators are safe to send (they are "shippable"),
283  * and that all collations used in the expression derive from Vars of the
284  * foreign table. Because of the latter, the logic is pretty close to
285  * assign_collations_walker() in parse_collate.c, though we can assume here
286  * that the given expression is valid. Note function mutability is not
287  * currently considered here.
288  */
289 static bool
291  foreign_glob_cxt *glob_cxt,
292  foreign_loc_cxt *outer_cxt)
293 {
294  bool check_type = true;
295  PgFdwRelationInfo *fpinfo;
296  foreign_loc_cxt inner_cxt;
297  Oid collation;
299 
300  /* Need do nothing for empty subexpressions */
301  if (node == NULL)
302  return true;
303 
304  /* May need server info from baserel's fdw_private struct */
305  fpinfo = (PgFdwRelationInfo *) (glob_cxt->foreignrel->fdw_private);
306 
307  /* Set up inner_cxt for possible recursion to child nodes */
308  inner_cxt.collation = InvalidOid;
309  inner_cxt.state = FDW_COLLATE_NONE;
310 
311  switch (nodeTag(node))
312  {
313  case T_Var:
314  {
315  Var *var = (Var *) node;
316 
317  /*
318  * If the Var is from the foreign table, we consider its
319  * collation (if any) safe to use. If it is from another
320  * table, we treat its collation the same way as we would a
321  * Param's collation, ie it's not safe for it to have a
322  * non-default collation.
323  */
324  if (bms_is_member(var->varno, glob_cxt->relids) &&
325  var->varlevelsup == 0)
326  {
327  /* Var belongs to foreign table */
328 
329  /*
330  * System columns other than ctid and oid should not be
331  * sent to the remote, since we don't make any effort to
332  * ensure that local and remote values match (tableoid, in
333  * particular, almost certainly doesn't match).
334  */
335  if (var->varattno < 0 &&
338  return false;
339 
340  /* Else check the collation */
341  collation = var->varcollid;
342  state = OidIsValid(collation) ? FDW_COLLATE_SAFE : FDW_COLLATE_NONE;
343  }
344  else
345  {
346  /* Var belongs to some other table */
347  collation = var->varcollid;
348  if (collation == InvalidOid ||
349  collation == DEFAULT_COLLATION_OID)
350  {
351  /*
352  * It's noncollatable, or it's safe to combine with a
353  * collatable foreign Var, so set state to NONE.
354  */
355  state = FDW_COLLATE_NONE;
356  }
357  else
358  {
359  /*
360  * Do not fail right away, since the Var might appear
361  * in a collation-insensitive context.
362  */
363  state = FDW_COLLATE_UNSAFE;
364  }
365  }
366  }
367  break;
368  case T_Const:
369  {
370  Const *c = (Const *) node;
371 
372  /*
373  * If the constant has nondefault collation, either it's of a
374  * non-builtin type, or it reflects folding of a CollateExpr.
375  * It's unsafe to send to the remote unless it's used in a
376  * non-collation-sensitive context.
377  */
378  collation = c->constcollid;
379  if (collation == InvalidOid ||
380  collation == DEFAULT_COLLATION_OID)
381  state = FDW_COLLATE_NONE;
382  else
383  state = FDW_COLLATE_UNSAFE;
384  }
385  break;
386  case T_Param:
387  {
388  Param *p = (Param *) node;
389 
390  /*
391  * Collation rule is same as for Consts and non-foreign Vars.
392  */
393  collation = p->paramcollid;
394  if (collation == InvalidOid ||
395  collation == DEFAULT_COLLATION_OID)
396  state = FDW_COLLATE_NONE;
397  else
398  state = FDW_COLLATE_UNSAFE;
399  }
400  break;
401  case T_ArrayRef:
402  {
403  ArrayRef *ar = (ArrayRef *) node;
404 
405  /* Assignment should not be in restrictions. */
406  if (ar->refassgnexpr != NULL)
407  return false;
408 
409  /*
410  * Recurse to remaining subexpressions. Since the array
411  * subscripts must yield (noncollatable) integers, they won't
412  * affect the inner_cxt state.
413  */
415  glob_cxt, &inner_cxt))
416  return false;
418  glob_cxt, &inner_cxt))
419  return false;
420  if (!foreign_expr_walker((Node *) ar->refexpr,
421  glob_cxt, &inner_cxt))
422  return false;
423 
424  /*
425  * Array subscripting should yield same collation as input,
426  * but for safety use same logic as for function nodes.
427  */
428  collation = ar->refcollid;
429  if (collation == InvalidOid)
430  state = FDW_COLLATE_NONE;
431  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
432  collation == inner_cxt.collation)
433  state = FDW_COLLATE_SAFE;
434  else if (collation == DEFAULT_COLLATION_OID)
435  state = FDW_COLLATE_NONE;
436  else
437  state = FDW_COLLATE_UNSAFE;
438  }
439  break;
440  case T_FuncExpr:
441  {
442  FuncExpr *fe = (FuncExpr *) node;
443 
444  /*
445  * If function used by the expression is not shippable, it
446  * can't be sent to remote because it might have incompatible
447  * semantics on remote side.
448  */
449  if (!is_shippable(fe->funcid, ProcedureRelationId, fpinfo))
450  return false;
451 
452  /*
453  * Recurse to input subexpressions.
454  */
455  if (!foreign_expr_walker((Node *) fe->args,
456  glob_cxt, &inner_cxt))
457  return false;
458 
459  /*
460  * If function's input collation is not derived from a foreign
461  * Var, it can't be sent to remote.
462  */
463  if (fe->inputcollid == InvalidOid)
464  /* OK, inputs are all noncollatable */ ;
465  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
466  fe->inputcollid != inner_cxt.collation)
467  return false;
468 
469  /*
470  * Detect whether node is introducing a collation not derived
471  * from a foreign Var. (If so, we just mark it unsafe for now
472  * rather than immediately returning false, since the parent
473  * node might not care.)
474  */
475  collation = fe->funccollid;
476  if (collation == InvalidOid)
477  state = FDW_COLLATE_NONE;
478  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
479  collation == inner_cxt.collation)
480  state = FDW_COLLATE_SAFE;
481  else if (collation == DEFAULT_COLLATION_OID)
482  state = FDW_COLLATE_NONE;
483  else
484  state = FDW_COLLATE_UNSAFE;
485  }
486  break;
487  case T_OpExpr:
488  case T_DistinctExpr: /* struct-equivalent to OpExpr */
489  {
490  OpExpr *oe = (OpExpr *) node;
491 
492  /*
493  * Similarly, only shippable operators can be sent to remote.
494  * (If the operator is shippable, we assume its underlying
495  * function is too.)
496  */
497  if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
498  return false;
499 
500  /*
501  * Recurse to input subexpressions.
502  */
503  if (!foreign_expr_walker((Node *) oe->args,
504  glob_cxt, &inner_cxt))
505  return false;
506 
507  /*
508  * If operator's input collation is not derived from a foreign
509  * Var, it can't be sent to remote.
510  */
511  if (oe->inputcollid == InvalidOid)
512  /* OK, inputs are all noncollatable */ ;
513  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
514  oe->inputcollid != inner_cxt.collation)
515  return false;
516 
517  /* Result-collation handling is same as for functions */
518  collation = oe->opcollid;
519  if (collation == InvalidOid)
520  state = FDW_COLLATE_NONE;
521  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
522  collation == inner_cxt.collation)
523  state = FDW_COLLATE_SAFE;
524  else if (collation == DEFAULT_COLLATION_OID)
525  state = FDW_COLLATE_NONE;
526  else
527  state = FDW_COLLATE_UNSAFE;
528  }
529  break;
530  case T_ScalarArrayOpExpr:
531  {
532  ScalarArrayOpExpr *oe = (ScalarArrayOpExpr *) node;
533 
534  /*
535  * Again, only shippable operators can be sent to remote.
536  */
537  if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
538  return false;
539 
540  /*
541  * Recurse to input subexpressions.
542  */
543  if (!foreign_expr_walker((Node *) oe->args,
544  glob_cxt, &inner_cxt))
545  return false;
546 
547  /*
548  * If operator's input collation is not derived from a foreign
549  * Var, it can't be sent to remote.
550  */
551  if (oe->inputcollid == InvalidOid)
552  /* OK, inputs are all noncollatable */ ;
553  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
554  oe->inputcollid != inner_cxt.collation)
555  return false;
556 
557  /* Output is always boolean and so noncollatable. */
558  collation = InvalidOid;
559  state = FDW_COLLATE_NONE;
560  }
561  break;
562  case T_RelabelType:
563  {
564  RelabelType *r = (RelabelType *) node;
565 
566  /*
567  * Recurse to input subexpression.
568  */
569  if (!foreign_expr_walker((Node *) r->arg,
570  glob_cxt, &inner_cxt))
571  return false;
572 
573  /*
574  * RelabelType must not introduce a collation not derived from
575  * an input foreign Var (same logic as for a real function).
576  */
577  collation = r->resultcollid;
578  if (collation == InvalidOid)
579  state = FDW_COLLATE_NONE;
580  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
581  collation == inner_cxt.collation)
582  state = FDW_COLLATE_SAFE;
583  else if (collation == DEFAULT_COLLATION_OID)
584  state = FDW_COLLATE_NONE;
585  else
586  state = FDW_COLLATE_UNSAFE;
587  }
588  break;
589  case T_BoolExpr:
590  {
591  BoolExpr *b = (BoolExpr *) node;
592 
593  /*
594  * Recurse to input subexpressions.
595  */
596  if (!foreign_expr_walker((Node *) b->args,
597  glob_cxt, &inner_cxt))
598  return false;
599 
600  /* Output is always boolean and so noncollatable. */
601  collation = InvalidOid;
602  state = FDW_COLLATE_NONE;
603  }
604  break;
605  case T_NullTest:
606  {
607  NullTest *nt = (NullTest *) node;
608 
609  /*
610  * Recurse to input subexpressions.
611  */
612  if (!foreign_expr_walker((Node *) nt->arg,
613  glob_cxt, &inner_cxt))
614  return false;
615 
616  /* Output is always boolean and so noncollatable. */
617  collation = InvalidOid;
618  state = FDW_COLLATE_NONE;
619  }
620  break;
621  case T_ArrayExpr:
622  {
623  ArrayExpr *a = (ArrayExpr *) node;
624 
625  /*
626  * Recurse to input subexpressions.
627  */
628  if (!foreign_expr_walker((Node *) a->elements,
629  glob_cxt, &inner_cxt))
630  return false;
631 
632  /*
633  * ArrayExpr must not introduce a collation not derived from
634  * an input foreign Var (same logic as for a function).
635  */
636  collation = a->array_collid;
637  if (collation == InvalidOid)
638  state = FDW_COLLATE_NONE;
639  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
640  collation == inner_cxt.collation)
641  state = FDW_COLLATE_SAFE;
642  else if (collation == DEFAULT_COLLATION_OID)
643  state = FDW_COLLATE_NONE;
644  else
645  state = FDW_COLLATE_UNSAFE;
646  }
647  break;
648  case T_List:
649  {
650  List *l = (List *) node;
651  ListCell *lc;
652 
653  /*
654  * Recurse to component subexpressions.
655  */
656  foreach(lc, l)
657  {
658  if (!foreign_expr_walker((Node *) lfirst(lc),
659  glob_cxt, &inner_cxt))
660  return false;
661  }
662 
663  /*
664  * When processing a list, collation state just bubbles up
665  * from the list elements.
666  */
667  collation = inner_cxt.collation;
668  state = inner_cxt.state;
669 
670  /* Don't apply exprType() to the list. */
671  check_type = false;
672  }
673  break;
674  case T_Aggref:
675  {
676  Aggref *agg = (Aggref *) node;
677  ListCell *lc;
678 
679  /* Not safe to pushdown when not in grouping context */
680  if (!IS_UPPER_REL(glob_cxt->foreignrel))
681  return false;
682 
683  /* Only non-split aggregates are pushable. */
684  if (agg->aggsplit != AGGSPLIT_SIMPLE)
685  return false;
686 
687  /* As usual, it must be shippable. */
688  if (!is_shippable(agg->aggfnoid, ProcedureRelationId, fpinfo))
689  return false;
690 
691  /*
692  * Recurse to input args. aggdirectargs, aggorder and
693  * aggdistinct are all present in args, so no need to check
694  * their shippability explicitly.
695  */
696  foreach(lc, agg->args)
697  {
698  Node *n = (Node *) lfirst(lc);
699 
700  /* If TargetEntry, extract the expression from it */
701  if (IsA(n, TargetEntry))
702  {
703  TargetEntry *tle = (TargetEntry *) n;
704 
705  n = (Node *) tle->expr;
706  }
707 
708  if (!foreign_expr_walker(n, glob_cxt, &inner_cxt))
709  return false;
710  }
711 
712  /*
713  * For aggorder elements, check whether the sort operator, if
714  * specified, is shippable or not.
715  */
716  if (agg->aggorder)
717  {
718  ListCell *lc;
719 
720  foreach(lc, agg->aggorder)
721  {
722  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
723  Oid sortcoltype;
724  TypeCacheEntry *typentry;
725  TargetEntry *tle;
726 
728  agg->args);
729  sortcoltype = exprType((Node *) tle->expr);
730  typentry = lookup_type_cache(sortcoltype,
732  /* Check shippability of non-default sort operator. */
733  if (srt->sortop != typentry->lt_opr &&
734  srt->sortop != typentry->gt_opr &&
736  fpinfo))
737  return false;
738  }
739  }
740 
741  /* Check aggregate filter */
742  if (!foreign_expr_walker((Node *) agg->aggfilter,
743  glob_cxt, &inner_cxt))
744  return false;
745 
746  /*
747  * If aggregate's input collation is not derived from a
748  * foreign Var, it can't be sent to remote.
749  */
750  if (agg->inputcollid == InvalidOid)
751  /* OK, inputs are all noncollatable */ ;
752  else if (inner_cxt.state != FDW_COLLATE_SAFE ||
753  agg->inputcollid != inner_cxt.collation)
754  return false;
755 
756  /*
757  * Detect whether node is introducing a collation not derived
758  * from a foreign Var. (If so, we just mark it unsafe for now
759  * rather than immediately returning false, since the parent
760  * node might not care.)
761  */
762  collation = agg->aggcollid;
763  if (collation == InvalidOid)
764  state = FDW_COLLATE_NONE;
765  else if (inner_cxt.state == FDW_COLLATE_SAFE &&
766  collation == inner_cxt.collation)
767  state = FDW_COLLATE_SAFE;
768  else if (collation == DEFAULT_COLLATION_OID)
769  state = FDW_COLLATE_NONE;
770  else
771  state = FDW_COLLATE_UNSAFE;
772  }
773  break;
774  default:
775 
776  /*
777  * If it's anything else, assume it's unsafe. This list can be
778  * expanded later, but don't forget to add deparse support below.
779  */
780  return false;
781  }
782 
783  /*
784  * If result type of given expression is not shippable, it can't be sent
785  * to remote because it might have incompatible semantics on remote side.
786  */
787  if (check_type && !is_shippable(exprType(node), TypeRelationId, fpinfo))
788  return false;
789 
790  /*
791  * Now, merge my collation information into my parent's state.
792  */
793  if (state > outer_cxt->state)
794  {
795  /* Override previous parent state */
796  outer_cxt->collation = collation;
797  outer_cxt->state = state;
798  }
799  else if (state == outer_cxt->state)
800  {
801  /* Merge, or detect error if there's a collation conflict */
802  switch (state)
803  {
804  case FDW_COLLATE_NONE:
805  /* Nothing + nothing is still nothing */
806  break;
807  case FDW_COLLATE_SAFE:
808  if (collation != outer_cxt->collation)
809  {
810  /*
811  * Non-default collation always beats default.
812  */
813  if (outer_cxt->collation == DEFAULT_COLLATION_OID)
814  {
815  /* Override previous parent state */
816  outer_cxt->collation = collation;
817  }
818  else if (collation != DEFAULT_COLLATION_OID)
819  {
820  /*
821  * Conflict; show state as indeterminate. We don't
822  * want to "return false" right away, since parent
823  * node might not care about collation.
824  */
825  outer_cxt->state = FDW_COLLATE_UNSAFE;
826  }
827  }
828  break;
829  case FDW_COLLATE_UNSAFE:
830  /* We're still conflicted ... */
831  break;
832  }
833  }
834 
835  /* It looks OK */
836  return true;
837 }
838 
839 /*
840  * Convert type OID + typmod info into a type name we can ship to the remote
841  * server. Someplace else had better have verified that this type name is
842  * expected to be known on the remote end.
843  *
844  * This is almost just format_type_with_typemod(), except that if left to its
845  * own devices, that function will make schema-qualification decisions based
846  * on the local search_path, which is wrong. We must schema-qualify all
847  * type names that are not in pg_catalog. We assume here that built-in types
848  * are all in pg_catalog and need not be qualified; otherwise, qualify.
849  */
850 static char *
851 deparse_type_name(Oid type_oid, int32 typemod)
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 }
858 
859 /*
860  * Build the targetlist for given relation to be deparsed as SELECT clause.
861  *
862  * The output targetlist contains the columns that need to be fetched from the
863  * foreign server for the given relation. If foreignrel is an upper relation,
864  * then the output targetlist can also contain expressions to be evaluated on
865  * foreign server.
866  */
867 List *
869 {
870  List *tlist = NIL;
871  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
872  ListCell *lc;
873 
874  /*
875  * For an upper relation, we have already built the target list while
876  * checking shippability, so just return that.
877  */
878  if (IS_UPPER_REL(foreignrel))
879  return fpinfo->grouped_tlist;
880 
881  /*
882  * We require columns specified in foreignrel->reltarget->exprs and those
883  * required for evaluating the local conditions.
884  */
885  tlist = add_to_flat_tlist(tlist,
886  pull_var_clause((Node *) foreignrel->reltarget->exprs,
888  foreach(lc, fpinfo->local_conds)
889  {
890  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
891 
892  tlist = add_to_flat_tlist(tlist,
893  pull_var_clause((Node *) rinfo->clause,
895  }
896 
897  return tlist;
898 }
899 
900 /*
901  * Deparse SELECT statement for given relation into buf.
902  *
903  * tlist contains the list of desired columns to be fetched from foreign server.
904  * For a base relation fpinfo->attrs_used is used to construct SELECT clause,
905  * hence the tlist is ignored for a base relation.
906  *
907  * remote_conds is the list of conditions to be deparsed into the WHERE clause
908  * (or, in the case of upper relations, into the HAVING clause).
909  *
910  * If params_list is not NULL, it receives a list of Params and other-relation
911  * Vars used in the clauses; these values must be transmitted to the remote
912  * server as parameter values.
913  *
914  * If params_list is NULL, we're generating the query for EXPLAIN purposes,
915  * so Params and other-relation Vars should be replaced by dummy values.
916  *
917  * pathkeys is the list of pathkeys to order the result by.
918  *
919  * is_subquery is the flag to indicate whether to deparse the specified
920  * relation as a subquery.
921  *
922  * List of columns selected is returned in retrieved_attrs.
923  */
924 extern void
926  List *tlist, List *remote_conds, List *pathkeys,
927  bool is_subquery, List **retrieved_attrs,
928  List **params_list)
929 {
930  deparse_expr_cxt context;
932  List *quals;
933 
934  /*
935  * We handle relations for foreign tables, joins between those and upper
936  * relations.
937  */
938  Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel));
939 
940  /* Fill portions of context common to upper, join and base relation */
941  context.buf = buf;
942  context.root = root;
943  context.foreignrel = rel;
944  context.scanrel = IS_UPPER_REL(rel) ? fpinfo->outerrel : rel;
945  context.params_list = params_list;
946 
947  /* Construct SELECT clause */
948  deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
949 
950  /*
951  * For upper relations, the WHERE clause is built from the remote
952  * conditions of the underlying scan relation; otherwise, we can use the
953  * supplied list of remote conditions directly.
954  */
955  if (IS_UPPER_REL(rel))
956  {
957  PgFdwRelationInfo *ofpinfo;
958 
959  ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
960  quals = ofpinfo->remote_conds;
961  }
962  else
963  quals = remote_conds;
964 
965  /* Construct FROM and WHERE clauses */
966  deparseFromExpr(quals, &context);
967 
968  if (IS_UPPER_REL(rel))
969  {
970  /* Append GROUP BY clause */
971  appendGroupByClause(tlist, &context);
972 
973  /* Append HAVING clause */
974  if (remote_conds)
975  {
976  appendStringInfo(buf, " HAVING ");
977  appendConditions(remote_conds, &context);
978  }
979  }
980 
981  /* Add ORDER BY clause if we found any useful pathkeys */
982  if (pathkeys)
983  appendOrderByClause(pathkeys, &context);
984 
985  /* Add any necessary FOR UPDATE/SHARE. */
986  deparseLockingClause(&context);
987 }
988 
989 /*
990  * Construct a simple SELECT statement that retrieves desired columns
991  * of the specified foreign table, and append it to "buf". The output
992  * contains just "SELECT ... ".
993  *
994  * We also create an integer List of the columns being retrieved, which is
995  * returned to *retrieved_attrs, unless we deparse the specified relation
996  * as a subquery.
997  *
998  * tlist is the list of desired columns. is_subquery is the flag to
999  * indicate whether to deparse the specified relation as a subquery.
1000  * Read prologue of deparseSelectStmtForRel() for details.
1001  */
1002 static void
1003 deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs,
1004  deparse_expr_cxt *context)
1005 {
1006  StringInfo buf = context->buf;
1007  RelOptInfo *foreignrel = context->foreignrel;
1008  PlannerInfo *root = context->root;
1009  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1010 
1011  /*
1012  * Construct SELECT list
1013  */
1014  appendStringInfoString(buf, "SELECT ");
1015 
1016  if (is_subquery)
1017  {
1018  /*
1019  * For a relation that is deparsed as a subquery, emit expressions
1020  * specified in the relation's reltarget. Note that since this is
1021  * for the subquery, no need to care about *retrieved_attrs.
1022  */
1023  deparseSubqueryTargetList(context);
1024  }
1025  else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
1026  {
1027  /*
1028  * For a join or upper relation the input tlist gives the list of
1029  * columns required to be fetched from the foreign server.
1030  */
1031  deparseExplicitTargetList(tlist, retrieved_attrs, context);
1032  }
1033  else
1034  {
1035  /*
1036  * For a base relation fpinfo->attrs_used gives the list of columns
1037  * required to be fetched from the foreign server.
1038  */
1039  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1040 
1041  /*
1042  * Core code already has some lock on each rel being planned, so we
1043  * can use NoLock here.
1044  */
1045  Relation rel = heap_open(rte->relid, NoLock);
1046 
1047  deparseTargetList(buf, root, foreignrel->relid, rel, false,
1048  fpinfo->attrs_used, false, retrieved_attrs);
1049  heap_close(rel, NoLock);
1050  }
1051 }
1052 
1053 /*
1054  * Construct a FROM clause and, if needed, a WHERE clause, and append those to
1055  * "buf".
1056  *
1057  * quals is the list of clauses to be included in the WHERE clause.
1058  * (These may or may not include RestrictInfo decoration.)
1059  */
1060 static void
1062 {
1063  StringInfo buf = context->buf;
1064  RelOptInfo *scanrel = context->scanrel;
1065 
1066  /* For upper relations, scanrel must be either a joinrel or a baserel */
1067  Assert(!IS_UPPER_REL(context->foreignrel) ||
1068  IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1069 
1070  /* Construct FROM clause */
1071  appendStringInfoString(buf, " FROM ");
1072  deparseFromExprForRel(buf, context->root, scanrel,
1073  (bms_num_members(scanrel->relids) > 1),
1074  context->params_list);
1075 
1076  /* Construct WHERE clause */
1077  if (quals != NIL)
1078  {
1079  appendStringInfo(buf, " WHERE ");
1080  appendConditions(quals, context);
1081  }
1082 }
1083 
1084 /*
1085  * Emit a target list that retrieves the columns specified in attrs_used.
1086  * This is used for both SELECT and RETURNING targetlists; the is_returning
1087  * parameter is true only for a RETURNING targetlist.
1088  *
1089  * The tlist text is appended to buf, and we also create an integer List
1090  * of the columns being retrieved, which is returned to *retrieved_attrs.
1091  *
1092  * If qualify_col is true, add relation alias before the column name.
1093  */
1094 static void
1096  PlannerInfo *root,
1097  Index rtindex,
1098  Relation rel,
1099  bool is_returning,
1100  Bitmapset *attrs_used,
1101  bool qualify_col,
1102  List **retrieved_attrs)
1103 {
1104  TupleDesc tupdesc = RelationGetDescr(rel);
1105  bool have_wholerow;
1106  bool first;
1107  int i;
1108 
1109  *retrieved_attrs = NIL;
1110 
1111  /* If there's a whole-row reference, we'll need all the columns. */
1113  attrs_used);
1114 
1115  first = true;
1116  for (i = 1; i <= tupdesc->natts; i++)
1117  {
1118  Form_pg_attribute attr = tupdesc->attrs[i - 1];
1119 
1120  /* Ignore dropped attributes. */
1121  if (attr->attisdropped)
1122  continue;
1123 
1124  if (have_wholerow ||
1126  attrs_used))
1127  {
1128  if (!first)
1129  appendStringInfoString(buf, ", ");
1130  else if (is_returning)
1131  appendStringInfoString(buf, " RETURNING ");
1132  first = false;
1133 
1134  deparseColumnRef(buf, rtindex, i, root, qualify_col);
1135 
1136  *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1137  }
1138  }
1139 
1140  /*
1141  * Add ctid and oid if needed. We currently don't support retrieving any
1142  * other system columns.
1143  */
1145  attrs_used))
1146  {
1147  if (!first)
1148  appendStringInfoString(buf, ", ");
1149  else if (is_returning)
1150  appendStringInfoString(buf, " RETURNING ");
1151  first = false;
1152 
1153  if (qualify_col)
1154  ADD_REL_QUALIFIER(buf, rtindex);
1155  appendStringInfoString(buf, "ctid");
1156 
1157  *retrieved_attrs = lappend_int(*retrieved_attrs,
1159  }
1161  attrs_used))
1162  {
1163  if (!first)
1164  appendStringInfoString(buf, ", ");
1165  else if (is_returning)
1166  appendStringInfoString(buf, " RETURNING ");
1167  first = false;
1168 
1169  if (qualify_col)
1170  ADD_REL_QUALIFIER(buf, rtindex);
1171  appendStringInfoString(buf, "oid");
1172 
1173  *retrieved_attrs = lappend_int(*retrieved_attrs,
1175  }
1176 
1177  /* Don't generate bad syntax if no undropped columns */
1178  if (first && !is_returning)
1179  appendStringInfoString(buf, "NULL");
1180 }
1181 
1182 /*
1183  * Deparse the appropriate locking clause (FOR UPDATE or FOR SHARE) for a
1184  * given relation (context->scanrel).
1185  */
1186 static void
1188 {
1189  StringInfo buf = context->buf;
1190  PlannerInfo *root = context->root;
1191  RelOptInfo *rel = context->scanrel;
1192  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1193  int relid = -1;
1194 
1195  while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1196  {
1197  /*
1198  * Ignore relation if it appears in a lower subquery. Locking clause
1199  * for such a relation is included in the subquery if necessary.
1200  */
1201  if (bms_is_member(relid, fpinfo->lower_subquery_rels))
1202  continue;
1203 
1204  /*
1205  * Add FOR UPDATE/SHARE if appropriate. We apply locking during the
1206  * initial row fetch, rather than later on as is done for local
1207  * tables. The extra roundtrips involved in trying to duplicate the
1208  * local semantics exactly don't seem worthwhile (see also comments
1209  * for RowMarkType).
1210  *
1211  * Note: because we actually run the query as a cursor, this assumes
1212  * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't
1213  * before 8.3.
1214  */
1215  if (relid == root->parse->resultRelation &&
1216  (root->parse->commandType == CMD_UPDATE ||
1217  root->parse->commandType == CMD_DELETE))
1218  {
1219  /* Relation is UPDATE/DELETE target, so use FOR UPDATE */
1220  appendStringInfoString(buf, " FOR UPDATE");
1221 
1222  /* Add the relation alias if we are here for a join relation */
1223  if (IS_JOIN_REL(rel))
1224  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1225  }
1226  else
1227  {
1228  PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid);
1229 
1230  if (rc)
1231  {
1232  /*
1233  * Relation is specified as a FOR UPDATE/SHARE target, so
1234  * handle that. (But we could also see LCS_NONE, meaning this
1235  * isn't a target relation after all.)
1236  *
1237  * For now, just ignore any [NO] KEY specification, since (a)
1238  * it's not clear what that means for a remote table that we
1239  * don't have complete information about, and (b) it wouldn't
1240  * work anyway on older remote servers. Likewise, we don't
1241  * worry about NOWAIT.
1242  */
1243  switch (rc->strength)
1244  {
1245  case LCS_NONE:
1246  /* No locking needed */
1247  break;
1248  case LCS_FORKEYSHARE:
1249  case LCS_FORSHARE:
1250  appendStringInfoString(buf, " FOR SHARE");
1251  break;
1252  case LCS_FORNOKEYUPDATE:
1253  case LCS_FORUPDATE:
1254  appendStringInfoString(buf, " FOR UPDATE");
1255  break;
1256  }
1257 
1258  /* Add the relation alias if we are here for a join relation */
1259  if (bms_num_members(rel->relids) > 1 &&
1260  rc->strength != LCS_NONE)
1261  appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1262  }
1263  }
1264  }
1265 }
1266 
1267 /*
1268  * Deparse conditions from the provided list and append them to buf.
1269  *
1270  * The conditions in the list are assumed to be ANDed. This function is used to
1271  * deparse WHERE clauses, JOIN .. ON clauses and HAVING clauses.
1272  *
1273  * Depending on the caller, the list elements might be either RestrictInfos
1274  * or bare clauses.
1275  */
1276 static void
1278 {
1279  int nestlevel;
1280  ListCell *lc;
1281  bool is_first = true;
1282  StringInfo buf = context->buf;
1283 
1284  /* Make sure any constants in the exprs are printed portably */
1285  nestlevel = set_transmission_modes();
1286 
1287  foreach(lc, exprs)
1288  {
1289  Expr *expr = (Expr *) lfirst(lc);
1290 
1291  /* Extract clause from RestrictInfo, if required */
1292  if (IsA(expr, RestrictInfo))
1293  expr = ((RestrictInfo *) expr)->clause;
1294 
1295  /* Connect expressions with "AND" and parenthesize each condition. */
1296  if (!is_first)
1297  appendStringInfoString(buf, " AND ");
1298 
1299  appendStringInfoChar(buf, '(');
1300  deparseExpr(expr, context);
1301  appendStringInfoChar(buf, ')');
1302 
1303  is_first = false;
1304  }
1305 
1306  reset_transmission_modes(nestlevel);
1307 }
1308 
1309 /* Output join name for given join type */
1310 extern const char *
1312 {
1313  switch (jointype)
1314  {
1315  case JOIN_INNER:
1316  return "INNER";
1317 
1318  case JOIN_LEFT:
1319  return "LEFT";
1320 
1321  case JOIN_RIGHT:
1322  return "RIGHT";
1323 
1324  case JOIN_FULL:
1325  return "FULL";
1326 
1327  default:
1328  /* Shouldn't come here, but protect from buggy code. */
1329  elog(ERROR, "unsupported join type %d", jointype);
1330  }
1331 
1332  /* Keep compiler happy */
1333  return NULL;
1334 }
1335 
1336 /*
1337  * Deparse given targetlist and append it to context->buf.
1338  *
1339  * tlist is list of TargetEntry's which in turn contain Var nodes.
1340  *
1341  * retrieved_attrs is the list of continuously increasing integers starting
1342  * from 1. It has same number of entries as tlist.
1343  */
1344 static void
1345 deparseExplicitTargetList(List *tlist, List **retrieved_attrs,
1346  deparse_expr_cxt *context)
1347 {
1348  ListCell *lc;
1349  StringInfo buf = context->buf;
1350  int i = 0;
1351 
1352  *retrieved_attrs = NIL;
1353 
1354  foreach(lc, tlist)
1355  {
1356  TargetEntry *tle = lfirst_node(TargetEntry, lc);
1357 
1358  if (i > 0)
1359  appendStringInfoString(buf, ", ");
1360  deparseExpr((Expr *) tle->expr, context);
1361 
1362  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1363  i++;
1364  }
1365 
1366  if (i == 0)
1367  appendStringInfoString(buf, "NULL");
1368 }
1369 
1370 /*
1371  * Emit expressions specified in the given relation's reltarget.
1372  *
1373  * This is used for deparsing the given relation as a subquery.
1374  */
1375 static void
1377 {
1378  StringInfo buf = context->buf;
1379  RelOptInfo *foreignrel = context->foreignrel;
1380  bool first;
1381  ListCell *lc;
1382 
1383  /* Should only be called in these cases. */
1384  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1385 
1386  first = true;
1387  foreach(lc, foreignrel->reltarget->exprs)
1388  {
1389  Node *node = (Node *) lfirst(lc);
1390 
1391  if (!first)
1392  appendStringInfoString(buf, ", ");
1393  first = false;
1394 
1395  deparseExpr((Expr *) node, context);
1396  }
1397 
1398  /* Don't generate bad syntax if no expressions */
1399  if (first)
1400  appendStringInfoString(buf, "NULL");
1401 }
1402 
1403 /*
1404  * Construct FROM clause for given relation
1405  *
1406  * The function constructs ... JOIN ... ON ... for join relation. For a base
1407  * relation it just returns schema-qualified tablename, with the appropriate
1408  * alias if so requested.
1409  */
1410 static void
1412  bool use_alias, List **params_list)
1413 {
1414  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1415 
1416  if (IS_JOIN_REL(foreignrel))
1417  {
1418  StringInfoData join_sql_o;
1419  StringInfoData join_sql_i;
1420 
1421  /* Deparse outer relation */
1422  initStringInfo(&join_sql_o);
1423  deparseRangeTblRef(&join_sql_o, root, fpinfo->outerrel,
1424  fpinfo->make_outerrel_subquery, params_list);
1425 
1426  /* Deparse inner relation */
1427  initStringInfo(&join_sql_i);
1428  deparseRangeTblRef(&join_sql_i, root, fpinfo->innerrel,
1429  fpinfo->make_innerrel_subquery, params_list);
1430 
1431  /*
1432  * For a join relation FROM clause entry is deparsed as
1433  *
1434  * ((outer relation) <join type> (inner relation) ON (joinclauses))
1435  */
1436  appendStringInfo(buf, "(%s %s JOIN %s ON ", join_sql_o.data,
1437  get_jointype_name(fpinfo->jointype), join_sql_i.data);
1438 
1439  /* Append join clause; (TRUE) if no join clause */
1440  if (fpinfo->joinclauses)
1441  {
1442  deparse_expr_cxt context;
1443 
1444  context.buf = buf;
1445  context.foreignrel = foreignrel;
1446  context.scanrel = foreignrel;
1447  context.root = root;
1448  context.params_list = params_list;
1449 
1450  appendStringInfo(buf, "(");
1451  appendConditions(fpinfo->joinclauses, &context);
1452  appendStringInfo(buf, ")");
1453  }
1454  else
1455  appendStringInfoString(buf, "(TRUE)");
1456 
1457  /* End the FROM clause entry. */
1458  appendStringInfo(buf, ")");
1459  }
1460  else
1461  {
1462  RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1463 
1464  /*
1465  * Core code already has some lock on each rel being planned, so we
1466  * can use NoLock here.
1467  */
1468  Relation rel = heap_open(rte->relid, NoLock);
1469 
1470  deparseRelation(buf, rel);
1471 
1472  /*
1473  * Add a unique alias to avoid any conflict in relation names due to
1474  * pulled up subqueries in the query being built for a pushed down
1475  * join.
1476  */
1477  if (use_alias)
1478  appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid);
1479 
1480  heap_close(rel, NoLock);
1481  }
1482 }
1483 
1484 /*
1485  * Append FROM clause entry for the given relation into buf.
1486  */
1487 static void
1489  bool make_subquery, List **params_list)
1490 {
1491  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1492 
1493  /* Should only be called in these cases. */
1494  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1495 
1496  Assert(fpinfo->local_conds == NIL);
1497 
1498  /* If make_subquery is true, deparse the relation as a subquery. */
1499  if (make_subquery)
1500  {
1501  List *retrieved_attrs;
1502  int ncols;
1503 
1504  /* Deparse the subquery representing the relation. */
1505  appendStringInfoChar(buf, '(');
1506  deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1507  fpinfo->remote_conds, NIL, true,
1508  &retrieved_attrs, params_list);
1509  appendStringInfoChar(buf, ')');
1510 
1511  /* Append the relation alias. */
1513  fpinfo->relation_index);
1514 
1515  /*
1516  * Append the column aliases if needed. Note that the subquery emits
1517  * expressions specified in the relation's reltarget (see
1518  * deparseSubqueryTargetList).
1519  */
1520  ncols = list_length(foreignrel->reltarget->exprs);
1521  if (ncols > 0)
1522  {
1523  int i;
1524 
1525  appendStringInfoChar(buf, '(');
1526  for (i = 1; i <= ncols; i++)
1527  {
1528  if (i > 1)
1529  appendStringInfoString(buf, ", ");
1530 
1532  }
1533  appendStringInfoChar(buf, ')');
1534  }
1535  }
1536  else
1537  deparseFromExprForRel(buf, root, foreignrel, true, params_list);
1538 }
1539 
1540 /*
1541  * deparse remote INSERT statement
1542  *
1543  * The statement text is appended to buf, and we also create an integer List
1544  * of the columns being retrieved by RETURNING (if any), which is returned
1545  * to *retrieved_attrs.
1546  */
1547 void
1549  Index rtindex, Relation rel,
1550  List *targetAttrs, bool doNothing,
1551  List *returningList, List **retrieved_attrs)
1552 {
1553  AttrNumber pindex;
1554  bool first;
1555  ListCell *lc;
1556 
1557  appendStringInfoString(buf, "INSERT INTO ");
1558  deparseRelation(buf, rel);
1559 
1560  if (targetAttrs)
1561  {
1562  appendStringInfoChar(buf, '(');
1563 
1564  first = true;
1565  foreach(lc, targetAttrs)
1566  {
1567  int attnum = lfirst_int(lc);
1568 
1569  if (!first)
1570  appendStringInfoString(buf, ", ");
1571  first = false;
1572 
1573  deparseColumnRef(buf, rtindex, attnum, root, false);
1574  }
1575 
1576  appendStringInfoString(buf, ") VALUES (");
1577 
1578  pindex = 1;
1579  first = true;
1580  foreach(lc, targetAttrs)
1581  {
1582  if (!first)
1583  appendStringInfoString(buf, ", ");
1584  first = false;
1585 
1586  appendStringInfo(buf, "$%d", pindex);
1587  pindex++;
1588  }
1589 
1590  appendStringInfoChar(buf, ')');
1591  }
1592  else
1593  appendStringInfoString(buf, " DEFAULT VALUES");
1594 
1595  if (doNothing)
1596  appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
1597 
1598  deparseReturningList(buf, root, rtindex, rel,
1599  rel->trigdesc && rel->trigdesc->trig_insert_after_row,
1600  returningList, retrieved_attrs);
1601 }
1602 
1603 /*
1604  * deparse remote UPDATE statement
1605  *
1606  * The statement text is appended to buf, and we also create an integer List
1607  * of the columns being retrieved by RETURNING (if any), which is returned
1608  * to *retrieved_attrs.
1609  */
1610 void
1612  Index rtindex, Relation rel,
1613  List *targetAttrs, List *returningList,
1614  List **retrieved_attrs)
1615 {
1616  AttrNumber pindex;
1617  bool first;
1618  ListCell *lc;
1619 
1620  appendStringInfoString(buf, "UPDATE ");
1621  deparseRelation(buf, rel);
1622  appendStringInfoString(buf, " SET ");
1623 
1624  pindex = 2; /* ctid is always the first param */
1625  first = true;
1626  foreach(lc, targetAttrs)
1627  {
1628  int attnum = lfirst_int(lc);
1629 
1630  if (!first)
1631  appendStringInfoString(buf, ", ");
1632  first = false;
1633 
1634  deparseColumnRef(buf, rtindex, attnum, root, false);
1635  appendStringInfo(buf, " = $%d", pindex);
1636  pindex++;
1637  }
1638  appendStringInfoString(buf, " WHERE ctid = $1");
1639 
1640  deparseReturningList(buf, root, rtindex, rel,
1641  rel->trigdesc && rel->trigdesc->trig_update_after_row,
1642  returningList, retrieved_attrs);
1643 }
1644 
1645 /*
1646  * deparse remote UPDATE statement
1647  *
1648  * The statement text is appended to buf, and we also create an integer List
1649  * of the columns being retrieved by RETURNING (if any), which is returned
1650  * to *retrieved_attrs.
1651  */
1652 void
1654  Index rtindex, Relation rel,
1655  List *targetlist,
1656  List *targetAttrs,
1657  List *remote_conds,
1658  List **params_list,
1659  List *returningList,
1660  List **retrieved_attrs)
1661 {
1662  RelOptInfo *baserel = root->simple_rel_array[rtindex];
1663  deparse_expr_cxt context;
1664  int nestlevel;
1665  bool first;
1666  ListCell *lc;
1667 
1668  /* Set up context struct for recursion */
1669  context.root = root;
1670  context.foreignrel = baserel;
1671  context.scanrel = baserel;
1672  context.buf = buf;
1673  context.params_list = params_list;
1674 
1675  appendStringInfoString(buf, "UPDATE ");
1676  deparseRelation(buf, rel);
1677  appendStringInfoString(buf, " SET ");
1678 
1679  /* Make sure any constants in the exprs are printed portably */
1680  nestlevel = set_transmission_modes();
1681 
1682  first = true;
1683  foreach(lc, targetAttrs)
1684  {
1685  int attnum = lfirst_int(lc);
1686  TargetEntry *tle = get_tle_by_resno(targetlist, attnum);
1687 
1688  if (!tle)
1689  elog(ERROR, "attribute number %d not found in UPDATE targetlist",
1690  attnum);
1691 
1692  if (!first)
1693  appendStringInfoString(buf, ", ");
1694  first = false;
1695 
1696  deparseColumnRef(buf, rtindex, attnum, root, false);
1697  appendStringInfoString(buf, " = ");
1698  deparseExpr((Expr *) tle->expr, &context);
1699  }
1700 
1701  reset_transmission_modes(nestlevel);
1702 
1703  if (remote_conds)
1704  {
1705  appendStringInfo(buf, " WHERE ");
1706  appendConditions(remote_conds, &context);
1707  }
1708 
1709  deparseReturningList(buf, root, rtindex, rel, false,
1710  returningList, retrieved_attrs);
1711 }
1712 
1713 /*
1714  * deparse remote DELETE statement
1715  *
1716  * The statement text is appended to buf, and we also create an integer List
1717  * of the columns being retrieved by RETURNING (if any), which is returned
1718  * to *retrieved_attrs.
1719  */
1720 void
1722  Index rtindex, Relation rel,
1723  List *returningList,
1724  List **retrieved_attrs)
1725 {
1726  appendStringInfoString(buf, "DELETE FROM ");
1727  deparseRelation(buf, rel);
1728  appendStringInfoString(buf, " WHERE ctid = $1");
1729 
1730  deparseReturningList(buf, root, rtindex, rel,
1731  rel->trigdesc && rel->trigdesc->trig_delete_after_row,
1732  returningList, retrieved_attrs);
1733 }
1734 
1735 /*
1736  * deparse remote DELETE statement
1737  *
1738  * The statement text is appended to buf, and we also create an integer List
1739  * of the columns being retrieved by RETURNING (if any), which is returned
1740  * to *retrieved_attrs.
1741  */
1742 void
1744  Index rtindex, Relation rel,
1745  List *remote_conds,
1746  List **params_list,
1747  List *returningList,
1748  List **retrieved_attrs)
1749 {
1750  RelOptInfo *baserel = root->simple_rel_array[rtindex];
1751  deparse_expr_cxt context;
1752 
1753  /* Set up context struct for recursion */
1754  context.root = root;
1755  context.foreignrel = baserel;
1756  context.scanrel = baserel;
1757  context.buf = buf;
1758  context.params_list = params_list;
1759 
1760  appendStringInfoString(buf, "DELETE FROM ");
1761  deparseRelation(buf, rel);
1762 
1763  if (remote_conds)
1764  {
1765  appendStringInfo(buf, " WHERE ");
1766  appendConditions(remote_conds, &context);
1767  }
1768 
1769  deparseReturningList(buf, root, rtindex, rel, false,
1770  returningList, retrieved_attrs);
1771 }
1772 
1773 /*
1774  * Add a RETURNING clause, if needed, to an INSERT/UPDATE/DELETE.
1775  */
1776 static void
1778  Index rtindex, Relation rel,
1779  bool trig_after_row,
1780  List *returningList,
1781  List **retrieved_attrs)
1782 {
1783  Bitmapset *attrs_used = NULL;
1784 
1785  if (trig_after_row)
1786  {
1787  /* whole-row reference acquires all non-system columns */
1788  attrs_used =
1790  }
1791 
1792  if (returningList != NIL)
1793  {
1794  /*
1795  * We need the attrs, non-system and system, mentioned in the local
1796  * query's RETURNING list.
1797  */
1798  pull_varattnos((Node *) returningList, rtindex,
1799  &attrs_used);
1800  }
1801 
1802  if (attrs_used != NULL)
1803  deparseTargetList(buf, root, rtindex, rel, true, attrs_used, false,
1804  retrieved_attrs);
1805  else
1806  *retrieved_attrs = NIL;
1807 }
1808 
1809 /*
1810  * Construct SELECT statement to acquire size in blocks of given relation.
1811  *
1812  * Note: we use local definition of block size, not remote definition.
1813  * This is perhaps debatable.
1814  *
1815  * Note: pg_relation_size() exists in 8.1 and later.
1816  */
1817 void
1819 {
1820  StringInfoData relname;
1821 
1822  /* We'll need the remote relation name as a literal. */
1823  initStringInfo(&relname);
1824  deparseRelation(&relname, rel);
1825 
1826  appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
1827  deparseStringLiteral(buf, relname.data);
1828  appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
1829 }
1830 
1831 /*
1832  * Construct SELECT statement to acquire sample rows of given relation.
1833  *
1834  * SELECT command is appended to buf, and list of columns retrieved
1835  * is returned to *retrieved_attrs.
1836  */
1837 void
1839 {
1840  Oid relid = RelationGetRelid(rel);
1841  TupleDesc tupdesc = RelationGetDescr(rel);
1842  int i;
1843  char *colname;
1844  List *options;
1845  ListCell *lc;
1846  bool first = true;
1847 
1848  *retrieved_attrs = NIL;
1849 
1850  appendStringInfoString(buf, "SELECT ");
1851  for (i = 0; i < tupdesc->natts; i++)
1852  {
1853  /* Ignore dropped columns. */
1854  if (tupdesc->attrs[i]->attisdropped)
1855  continue;
1856 
1857  if (!first)
1858  appendStringInfoString(buf, ", ");
1859  first = false;
1860 
1861  /* Use attribute name or column_name option. */
1862  colname = NameStr(tupdesc->attrs[i]->attname);
1863  options = GetForeignColumnOptions(relid, i + 1);
1864 
1865  foreach(lc, options)
1866  {
1867  DefElem *def = (DefElem *) lfirst(lc);
1868 
1869  if (strcmp(def->defname, "column_name") == 0)
1870  {
1871  colname = defGetString(def);
1872  break;
1873  }
1874  }
1875 
1877 
1878  *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1879  }
1880 
1881  /* Don't generate bad syntax for zero-column relation. */
1882  if (first)
1883  appendStringInfoString(buf, "NULL");
1884 
1885  /*
1886  * Construct FROM clause
1887  */
1888  appendStringInfoString(buf, " FROM ");
1889  deparseRelation(buf, rel);
1890 }
1891 
1892 /*
1893  * Construct name to use for given column, and emit it into buf.
1894  * If it has a column_name FDW option, use that instead of attribute name.
1895  *
1896  * If qualify_col is true, qualify column name with the alias of relation.
1897  */
1898 static void
1899 deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root,
1900  bool qualify_col)
1901 {
1902  RangeTblEntry *rte;
1903 
1904  /* We support fetching the remote side's CTID and OID. */
1905  if (varattno == SelfItemPointerAttributeNumber)
1906  {
1907  if (qualify_col)
1908  ADD_REL_QUALIFIER(buf, varno);
1909  appendStringInfoString(buf, "ctid");
1910  }
1911  else if (varattno == ObjectIdAttributeNumber)
1912  {
1913  if (qualify_col)
1914  ADD_REL_QUALIFIER(buf, varno);
1915  appendStringInfoString(buf, "oid");
1916  }
1917  else if (varattno < 0)
1918  {
1919  /*
1920  * All other system attributes are fetched as 0, except for table OID,
1921  * which is fetched as the local table OID. However, we must be
1922  * careful; the table could be beneath an outer join, in which case it
1923  * must go to NULL whenever the rest of the row does.
1924  */
1925  Oid fetchval = 0;
1926 
1927  if (varattno == TableOidAttributeNumber)
1928  {
1929  rte = planner_rt_fetch(varno, root);
1930  fetchval = rte->relid;
1931  }
1932 
1933  if (qualify_col)
1934  {
1935  appendStringInfoString(buf, "CASE WHEN (");
1936  ADD_REL_QUALIFIER(buf, varno);
1937  appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval);
1938  }
1939  else
1940  appendStringInfo(buf, "%u", fetchval);
1941  }
1942  else if (varattno == 0)
1943  {
1944  /* Whole row reference */
1945  Relation rel;
1946  Bitmapset *attrs_used;
1947 
1948  /* Required only to be passed down to deparseTargetList(). */
1949  List *retrieved_attrs;
1950 
1951  /* Get RangeTblEntry from array in PlannerInfo. */
1952  rte = planner_rt_fetch(varno, root);
1953 
1954  /*
1955  * The lock on the relation will be held by upper callers, so it's
1956  * fine to open it with no lock here.
1957  */
1958  rel = heap_open(rte->relid, NoLock);
1959 
1960  /*
1961  * The local name of the foreign table can not be recognized by the
1962  * foreign server and the table it references on foreign server might
1963  * have different column ordering or different columns than those
1964  * declared locally. Hence we have to deparse whole-row reference as
1965  * ROW(columns referenced locally). Construct this by deparsing a
1966  * "whole row" attribute.
1967  */
1968  attrs_used = bms_add_member(NULL,
1970 
1971  /*
1972  * In case the whole-row reference is under an outer join then it has
1973  * to go NULL whenever the rest of the row goes NULL. Deparsing a join
1974  * query would always involve multiple relations, thus qualify_col
1975  * would be true.
1976  */
1977  if (qualify_col)
1978  {
1979  appendStringInfoString(buf, "CASE WHEN (");
1980  ADD_REL_QUALIFIER(buf, varno);
1981  appendStringInfo(buf, "*)::text IS NOT NULL THEN ");
1982  }
1983 
1984  appendStringInfoString(buf, "ROW(");
1985  deparseTargetList(buf, root, varno, rel, false, attrs_used, qualify_col,
1986  &retrieved_attrs);
1987  appendStringInfoString(buf, ")");
1988 
1989  /* Complete the CASE WHEN statement started above. */
1990  if (qualify_col)
1991  appendStringInfo(buf, " END");
1992 
1993  heap_close(rel, NoLock);
1994  bms_free(attrs_used);
1995  }
1996  else
1997  {
1998  char *colname = NULL;
1999  List *options;
2000  ListCell *lc;
2001 
2002  /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
2003  Assert(!IS_SPECIAL_VARNO(varno));
2004 
2005  /* Get RangeTblEntry from array in PlannerInfo. */
2006  rte = planner_rt_fetch(varno, root);
2007 
2008  /*
2009  * If it's a column of a foreign table, and it has the column_name FDW
2010  * option, use that value.
2011  */
2012  options = GetForeignColumnOptions(rte->relid, varattno);
2013  foreach(lc, options)
2014  {
2015  DefElem *def = (DefElem *) lfirst(lc);
2016 
2017  if (strcmp(def->defname, "column_name") == 0)
2018  {
2019  colname = defGetString(def);
2020  break;
2021  }
2022  }
2023 
2024  /*
2025  * If it's a column of a regular table or it doesn't have column_name
2026  * FDW option, use attribute name.
2027  */
2028  if (colname == NULL)
2029  colname = get_relid_attribute_name(rte->relid, varattno);
2030 
2031  if (qualify_col)
2032  ADD_REL_QUALIFIER(buf, varno);
2033 
2035  }
2036 }
2037 
2038 /*
2039  * Append remote name of specified foreign table to buf.
2040  * Use value of table_name FDW option (if any) instead of relation's name.
2041  * Similarly, schema_name FDW option overrides schema name.
2042  */
2043 static void
2045 {
2046  ForeignTable *table;
2047  const char *nspname = NULL;
2048  const char *relname = NULL;
2049  ListCell *lc;
2050 
2051  /* obtain additional catalog information. */
2052  table = GetForeignTable(RelationGetRelid(rel));
2053 
2054  /*
2055  * Use value of FDW options if any, instead of the name of object itself.
2056  */
2057  foreach(lc, table->options)
2058  {
2059  DefElem *def = (DefElem *) lfirst(lc);
2060 
2061  if (strcmp(def->defname, "schema_name") == 0)
2062  nspname = defGetString(def);
2063  else if (strcmp(def->defname, "table_name") == 0)
2064  relname = defGetString(def);
2065  }
2066 
2067  /*
2068  * Note: we could skip printing the schema name if it's pg_catalog, but
2069  * that doesn't seem worth the trouble.
2070  */
2071  if (nspname == NULL)
2072  nspname = get_namespace_name(RelationGetNamespace(rel));
2073  if (relname == NULL)
2074  relname = RelationGetRelationName(rel);
2075 
2076  appendStringInfo(buf, "%s.%s",
2077  quote_identifier(nspname), quote_identifier(relname));
2078 }
2079 
2080 /*
2081  * Append a SQL string literal representing "val" to buf.
2082  */
2083 void
2085 {
2086  const char *valptr;
2087 
2088  /*
2089  * Rather than making assumptions about the remote server's value of
2090  * standard_conforming_strings, always use E'foo' syntax if there are any
2091  * backslashes. This will fail on remote servers before 8.1, but those
2092  * are long out of support.
2093  */
2094  if (strchr(val, '\\') != NULL)
2096  appendStringInfoChar(buf, '\'');
2097  for (valptr = val; *valptr; valptr++)
2098  {
2099  char ch = *valptr;
2100 
2101  if (SQL_STR_DOUBLE(ch, true))
2102  appendStringInfoChar(buf, ch);
2103  appendStringInfoChar(buf, ch);
2104  }
2105  appendStringInfoChar(buf, '\'');
2106 }
2107 
2108 /*
2109  * Deparse given expression into context->buf.
2110  *
2111  * This function must support all the same node types that foreign_expr_walker
2112  * accepts.
2113  *
2114  * Note: unlike ruleutils.c, we just use a simple hard-wired parenthesization
2115  * scheme: anything more complex than a Var, Const, function call or cast
2116  * should be self-parenthesized.
2117  */
2118 static void
2120 {
2121  if (node == NULL)
2122  return;
2123 
2124  switch (nodeTag(node))
2125  {
2126  case T_Var:
2127  deparseVar((Var *) node, context);
2128  break;
2129  case T_Const:
2130  deparseConst((Const *) node, context, 0);
2131  break;
2132  case T_Param:
2133  deparseParam((Param *) node, context);
2134  break;
2135  case T_ArrayRef:
2136  deparseArrayRef((ArrayRef *) node, context);
2137  break;
2138  case T_FuncExpr:
2139  deparseFuncExpr((FuncExpr *) node, context);
2140  break;
2141  case T_OpExpr:
2142  deparseOpExpr((OpExpr *) node, context);
2143  break;
2144  case T_DistinctExpr:
2145  deparseDistinctExpr((DistinctExpr *) node, context);
2146  break;
2147  case T_ScalarArrayOpExpr:
2148  deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2149  break;
2150  case T_RelabelType:
2151  deparseRelabelType((RelabelType *) node, context);
2152  break;
2153  case T_BoolExpr:
2154  deparseBoolExpr((BoolExpr *) node, context);
2155  break;
2156  case T_NullTest:
2157  deparseNullTest((NullTest *) node, context);
2158  break;
2159  case T_ArrayExpr:
2160  deparseArrayExpr((ArrayExpr *) node, context);
2161  break;
2162  case T_Aggref:
2163  deparseAggref((Aggref *) node, context);
2164  break;
2165  default:
2166  elog(ERROR, "unsupported expression type for deparse: %d",
2167  (int) nodeTag(node));
2168  break;
2169  }
2170 }
2171 
2172 /*
2173  * Deparse given Var node into context->buf.
2174  *
2175  * If the Var belongs to the foreign relation, just print its remote name.
2176  * Otherwise, it's effectively a Param (and will in fact be a Param at
2177  * run time). Handle it the same way we handle plain Params --- see
2178  * deparseParam for comments.
2179  */
2180 static void
2182 {
2183  Relids relids = context->scanrel->relids;
2184  int relno;
2185  int colno;
2186 
2187  /* Qualify columns when multiple relations are involved. */
2188  bool qualify_col = (bms_num_members(relids) > 1);
2189 
2190  /*
2191  * If the Var belongs to the foreign relation that is deparsed as a
2192  * subquery, use the relation and column alias to the Var provided
2193  * by the subquery, instead of the remote name.
2194  */
2195  if (is_subquery_var(node, context->scanrel, &relno, &colno))
2196  {
2197  appendStringInfo(context->buf, "%s%d.%s%d",
2199  SUBQUERY_COL_ALIAS_PREFIX, colno);
2200  return;
2201  }
2202 
2203  if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
2204  deparseColumnRef(context->buf, node->varno, node->varattno,
2205  context->root, qualify_col);
2206  else
2207  {
2208  /* Treat like a Param */
2209  if (context->params_list)
2210  {
2211  int pindex = 0;
2212  ListCell *lc;
2213 
2214  /* find its index in params_list */
2215  foreach(lc, *context->params_list)
2216  {
2217  pindex++;
2218  if (equal(node, (Node *) lfirst(lc)))
2219  break;
2220  }
2221  if (lc == NULL)
2222  {
2223  /* not in list, so add it */
2224  pindex++;
2225  *context->params_list = lappend(*context->params_list, node);
2226  }
2227 
2228  printRemoteParam(pindex, node->vartype, node->vartypmod, context);
2229  }
2230  else
2231  {
2232  printRemotePlaceholder(node->vartype, node->vartypmod, context);
2233  }
2234  }
2235 }
2236 
2237 /*
2238  * Deparse given constant value into context->buf.
2239  *
2240  * This function has to be kept in sync with ruleutils.c's get_const_expr.
2241  * As for that function, showtype can be -1 to never show "::typename" decoration,
2242  * or +1 to always show it, or 0 to show it only if the constant wouldn't be assumed
2243  * to be the right type by default.
2244  */
2245 static void
2246 deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
2247 {
2248  StringInfo buf = context->buf;
2249  Oid typoutput;
2250  bool typIsVarlena;
2251  char *extval;
2252  bool isfloat = false;
2253  bool needlabel;
2254 
2255  if (node->constisnull)
2256  {
2257  appendStringInfoString(buf, "NULL");
2258  if (showtype >= 0)
2259  appendStringInfo(buf, "::%s",
2261  node->consttypmod));
2262  return;
2263  }
2264 
2266  &typoutput, &typIsVarlena);
2267  extval = OidOutputFunctionCall(typoutput, node->constvalue);
2268 
2269  switch (node->consttype)
2270  {
2271  case INT2OID:
2272  case INT4OID:
2273  case INT8OID:
2274  case OIDOID:
2275  case FLOAT4OID:
2276  case FLOAT8OID:
2277  case NUMERICOID:
2278  {
2279  /*
2280  * No need to quote unless it's a special value such as 'NaN'.
2281  * See comments in get_const_expr().
2282  */
2283  if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2284  {
2285  if (extval[0] == '+' || extval[0] == '-')
2286  appendStringInfo(buf, "(%s)", extval);
2287  else
2288  appendStringInfoString(buf, extval);
2289  if (strcspn(extval, "eE.") != strlen(extval))
2290  isfloat = true; /* it looks like a float */
2291  }
2292  else
2293  appendStringInfo(buf, "'%s'", extval);
2294  }
2295  break;
2296  case BITOID:
2297  case VARBITOID:
2298  appendStringInfo(buf, "B'%s'", extval);
2299  break;
2300  case BOOLOID:
2301  if (strcmp(extval, "t") == 0)
2302  appendStringInfoString(buf, "true");
2303  else
2304  appendStringInfoString(buf, "false");
2305  break;
2306  default:
2307  deparseStringLiteral(buf, extval);
2308  break;
2309  }
2310 
2311  pfree(extval);
2312 
2313  if (showtype < 0)
2314  return;
2315 
2316  /*
2317  * For showtype == 0, append ::typename unless the constant will be
2318  * implicitly typed as the right type when it is read in.
2319  *
2320  * XXX this code has to be kept in sync with the behavior of the parser,
2321  * especially make_const.
2322  */
2323  switch (node->consttype)
2324  {
2325  case BOOLOID:
2326  case INT4OID:
2327  case UNKNOWNOID:
2328  needlabel = false;
2329  break;
2330  case NUMERICOID:
2331  needlabel = !isfloat || (node->consttypmod >= 0);
2332  break;
2333  default:
2334  needlabel = true;
2335  break;
2336  }
2337  if (needlabel || showtype > 0)
2338  appendStringInfo(buf, "::%s",
2340  node->consttypmod));
2341 }
2342 
2343 /*
2344  * Deparse given Param node.
2345  *
2346  * If we're generating the query "for real", add the Param to
2347  * context->params_list if it's not already present, and then use its index
2348  * in that list as the remote parameter number. During EXPLAIN, there's
2349  * no need to identify a parameter number.
2350  */
2351 static void
2353 {
2354  if (context->params_list)
2355  {
2356  int pindex = 0;
2357  ListCell *lc;
2358 
2359  /* find its index in params_list */
2360  foreach(lc, *context->params_list)
2361  {
2362  pindex++;
2363  if (equal(node, (Node *) lfirst(lc)))
2364  break;
2365  }
2366  if (lc == NULL)
2367  {
2368  /* not in list, so add it */
2369  pindex++;
2370  *context->params_list = lappend(*context->params_list, node);
2371  }
2372 
2373  printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
2374  }
2375  else
2376  {
2377  printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
2378  }
2379 }
2380 
2381 /*
2382  * Deparse an array subscript expression.
2383  */
2384 static void
2386 {
2387  StringInfo buf = context->buf;
2388  ListCell *lowlist_item;
2389  ListCell *uplist_item;
2390 
2391  /* Always parenthesize the expression. */
2392  appendStringInfoChar(buf, '(');
2393 
2394  /*
2395  * Deparse referenced array expression first. If that expression includes
2396  * a cast, we have to parenthesize to prevent the array subscript from
2397  * being taken as typename decoration. We can avoid that in the typical
2398  * case of subscripting a Var, but otherwise do it.
2399  */
2400  if (IsA(node->refexpr, Var))
2401  deparseExpr(node->refexpr, context);
2402  else
2403  {
2404  appendStringInfoChar(buf, '(');
2405  deparseExpr(node->refexpr, context);
2406  appendStringInfoChar(buf, ')');
2407  }
2408 
2409  /* Deparse subscript expressions. */
2410  lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
2411  foreach(uplist_item, node->refupperindexpr)
2412  {
2413  appendStringInfoChar(buf, '[');
2414  if (lowlist_item)
2415  {
2416  deparseExpr(lfirst(lowlist_item), context);
2417  appendStringInfoChar(buf, ':');
2418  lowlist_item = lnext(lowlist_item);
2419  }
2420  deparseExpr(lfirst(uplist_item), context);
2421  appendStringInfoChar(buf, ']');
2422  }
2423 
2424  appendStringInfoChar(buf, ')');
2425 }
2426 
2427 /*
2428  * Deparse a function call.
2429  */
2430 static void
2432 {
2433  StringInfo buf = context->buf;
2434  bool use_variadic;
2435  bool first;
2436  ListCell *arg;
2437 
2438  /*
2439  * If the function call came from an implicit coercion, then just show the
2440  * first argument.
2441  */
2442  if (node->funcformat == COERCE_IMPLICIT_CAST)
2443  {
2444  deparseExpr((Expr *) linitial(node->args), context);
2445  return;
2446  }
2447 
2448  /*
2449  * If the function call came from a cast, then show the first argument
2450  * plus an explicit cast operation.
2451  */
2452  if (node->funcformat == COERCE_EXPLICIT_CAST)
2453  {
2454  Oid rettype = node->funcresulttype;
2455  int32 coercedTypmod;
2456 
2457  /* Get the typmod if this is a length-coercion function */
2458  (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
2459 
2460  deparseExpr((Expr *) linitial(node->args), context);
2461  appendStringInfo(buf, "::%s",
2462  deparse_type_name(rettype, coercedTypmod));
2463  return;
2464  }
2465 
2466  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2467  use_variadic = node->funcvariadic;
2468 
2469  /*
2470  * Normal function: display as proname(args).
2471  */
2472  appendFunctionName(node->funcid, context);
2473  appendStringInfoChar(buf, '(');
2474 
2475  /* ... and all the arguments */
2476  first = true;
2477  foreach(arg, node->args)
2478  {
2479  if (!first)
2480  appendStringInfoString(buf, ", ");
2481  if (use_variadic && lnext(arg) == NULL)
2482  appendStringInfoString(buf, "VARIADIC ");
2483  deparseExpr((Expr *) lfirst(arg), context);
2484  first = false;
2485  }
2486  appendStringInfoChar(buf, ')');
2487 }
2488 
2489 /*
2490  * Deparse given operator expression. To avoid problems around
2491  * priority of operations, we always parenthesize the arguments.
2492  */
2493 static void
2495 {
2496  StringInfo buf = context->buf;
2497  HeapTuple tuple;
2498  Form_pg_operator form;
2499  char oprkind;
2500  ListCell *arg;
2501 
2502  /* Retrieve information about the operator from system catalog. */
2503  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2504  if (!HeapTupleIsValid(tuple))
2505  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2506  form = (Form_pg_operator) GETSTRUCT(tuple);
2507  oprkind = form->oprkind;
2508 
2509  /* Sanity check. */
2510  Assert((oprkind == 'r' && list_length(node->args) == 1) ||
2511  (oprkind == 'l' && list_length(node->args) == 1) ||
2512  (oprkind == 'b' && list_length(node->args) == 2));
2513 
2514  /* Always parenthesize the expression. */
2515  appendStringInfoChar(buf, '(');
2516 
2517  /* Deparse left operand. */
2518  if (oprkind == 'r' || oprkind == 'b')
2519  {
2520  arg = list_head(node->args);
2521  deparseExpr(lfirst(arg), context);
2522  appendStringInfoChar(buf, ' ');
2523  }
2524 
2525  /* Deparse operator name. */
2526  deparseOperatorName(buf, form);
2527 
2528  /* Deparse right operand. */
2529  if (oprkind == 'l' || oprkind == 'b')
2530  {
2531  arg = list_tail(node->args);
2532  appendStringInfoChar(buf, ' ');
2533  deparseExpr(lfirst(arg), context);
2534  }
2535 
2536  appendStringInfoChar(buf, ')');
2537 
2538  ReleaseSysCache(tuple);
2539 }
2540 
2541 /*
2542  * Print the name of an operator.
2543  */
2544 static void
2546 {
2547  char *opname;
2548 
2549  /* opname is not a SQL identifier, so we should not quote it. */
2550  opname = NameStr(opform->oprname);
2551 
2552  /* Print schema name only if it's not pg_catalog */
2553  if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
2554  {
2555  const char *opnspname;
2556 
2557  opnspname = get_namespace_name(opform->oprnamespace);
2558  /* Print fully qualified operator name. */
2559  appendStringInfo(buf, "OPERATOR(%s.%s)",
2560  quote_identifier(opnspname), opname);
2561  }
2562  else
2563  {
2564  /* Just print operator name. */
2565  appendStringInfoString(buf, opname);
2566  }
2567 }
2568 
2569 /*
2570  * Deparse IS DISTINCT FROM.
2571  */
2572 static void
2574 {
2575  StringInfo buf = context->buf;
2576 
2577  Assert(list_length(node->args) == 2);
2578 
2579  appendStringInfoChar(buf, '(');
2580  deparseExpr(linitial(node->args), context);
2581  appendStringInfoString(buf, " IS DISTINCT FROM ");
2582  deparseExpr(lsecond(node->args), context);
2583  appendStringInfoChar(buf, ')');
2584 }
2585 
2586 /*
2587  * Deparse given ScalarArrayOpExpr expression. To avoid problems
2588  * around priority of operations, we always parenthesize the arguments.
2589  */
2590 static void
2592 {
2593  StringInfo buf = context->buf;
2594  HeapTuple tuple;
2595  Form_pg_operator form;
2596  Expr *arg1;
2597  Expr *arg2;
2598 
2599  /* Retrieve information about the operator from system catalog. */
2600  tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
2601  if (!HeapTupleIsValid(tuple))
2602  elog(ERROR, "cache lookup failed for operator %u", node->opno);
2603  form = (Form_pg_operator) GETSTRUCT(tuple);
2604 
2605  /* Sanity check. */
2606  Assert(list_length(node->args) == 2);
2607 
2608  /* Always parenthesize the expression. */
2609  appendStringInfoChar(buf, '(');
2610 
2611  /* Deparse left operand. */
2612  arg1 = linitial(node->args);
2613  deparseExpr(arg1, context);
2614  appendStringInfoChar(buf, ' ');
2615 
2616  /* Deparse operator name plus decoration. */
2617  deparseOperatorName(buf, form);
2618  appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
2619 
2620  /* Deparse right operand. */
2621  arg2 = lsecond(node->args);
2622  deparseExpr(arg2, context);
2623 
2624  appendStringInfoChar(buf, ')');
2625 
2626  /* Always parenthesize the expression. */
2627  appendStringInfoChar(buf, ')');
2628 
2629  ReleaseSysCache(tuple);
2630 }
2631 
2632 /*
2633  * Deparse a RelabelType (binary-compatible cast) node.
2634  */
2635 static void
2637 {
2638  deparseExpr(node->arg, context);
2639  if (node->relabelformat != COERCE_IMPLICIT_CAST)
2640  appendStringInfo(context->buf, "::%s",
2642  node->resulttypmod));
2643 }
2644 
2645 /*
2646  * Deparse a BoolExpr node.
2647  */
2648 static void
2650 {
2651  StringInfo buf = context->buf;
2652  const char *op = NULL; /* keep compiler quiet */
2653  bool first;
2654  ListCell *lc;
2655 
2656  switch (node->boolop)
2657  {
2658  case AND_EXPR:
2659  op = "AND";
2660  break;
2661  case OR_EXPR:
2662  op = "OR";
2663  break;
2664  case NOT_EXPR:
2665  appendStringInfoString(buf, "(NOT ");
2666  deparseExpr(linitial(node->args), context);
2667  appendStringInfoChar(buf, ')');
2668  return;
2669  }
2670 
2671  appendStringInfoChar(buf, '(');
2672  first = true;
2673  foreach(lc, node->args)
2674  {
2675  if (!first)
2676  appendStringInfo(buf, " %s ", op);
2677  deparseExpr((Expr *) lfirst(lc), context);
2678  first = false;
2679  }
2680  appendStringInfoChar(buf, ')');
2681 }
2682 
2683 /*
2684  * Deparse IS [NOT] NULL expression.
2685  */
2686 static void
2688 {
2689  StringInfo buf = context->buf;
2690 
2691  appendStringInfoChar(buf, '(');
2692  deparseExpr(node->arg, context);
2693 
2694  /*
2695  * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
2696  * shorter and traditional. If it's a rowtype input but we're applying a
2697  * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
2698  * correct.
2699  */
2700  if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
2701  {
2702  if (node->nulltesttype == IS_NULL)
2703  appendStringInfoString(buf, " IS NULL)");
2704  else
2705  appendStringInfoString(buf, " IS NOT NULL)");
2706  }
2707  else
2708  {
2709  if (node->nulltesttype == IS_NULL)
2710  appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
2711  else
2712  appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
2713  }
2714 }
2715 
2716 /*
2717  * Deparse ARRAY[...] construct.
2718  */
2719 static void
2721 {
2722  StringInfo buf = context->buf;
2723  bool first = true;
2724  ListCell *lc;
2725 
2726  appendStringInfoString(buf, "ARRAY[");
2727  foreach(lc, node->elements)
2728  {
2729  if (!first)
2730  appendStringInfoString(buf, ", ");
2731  deparseExpr(lfirst(lc), context);
2732  first = false;
2733  }
2734  appendStringInfoChar(buf, ']');
2735 
2736  /* If the array is empty, we need an explicit cast to the array type. */
2737  if (node->elements == NIL)
2738  appendStringInfo(buf, "::%s",
2739  deparse_type_name(node->array_typeid, -1));
2740 }
2741 
2742 /*
2743  * Deparse an Aggref node.
2744  */
2745 static void
2747 {
2748  StringInfo buf = context->buf;
2749  bool use_variadic;
2750 
2751  /* Only basic, non-split aggregation accepted. */
2752  Assert(node->aggsplit == AGGSPLIT_SIMPLE);
2753 
2754  /* Check if need to print VARIADIC (cf. ruleutils.c) */
2755  use_variadic = node->aggvariadic;
2756 
2757  /* Find aggregate name from aggfnoid which is a pg_proc entry */
2758  appendFunctionName(node->aggfnoid, context);
2759  appendStringInfoChar(buf, '(');
2760 
2761  /* Add DISTINCT */
2762  appendStringInfo(buf, "%s", (node->aggdistinct != NIL) ? "DISTINCT " : "");
2763 
2764  if (AGGKIND_IS_ORDERED_SET(node->aggkind))
2765  {
2766  /* Add WITHIN GROUP (ORDER BY ..) */
2767  ListCell *arg;
2768  bool first = true;
2769 
2770  Assert(!node->aggvariadic);
2771  Assert(node->aggorder != NIL);
2772 
2773  foreach(arg, node->aggdirectargs)
2774  {
2775  if (!first)
2776  appendStringInfoString(buf, ", ");
2777  first = false;
2778 
2779  deparseExpr((Expr *) lfirst(arg), context);
2780  }
2781 
2782  appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
2783  appendAggOrderBy(node->aggorder, node->args, context);
2784  }
2785  else
2786  {
2787  /* aggstar can be set only in zero-argument aggregates */
2788  if (node->aggstar)
2789  appendStringInfoChar(buf, '*');
2790  else
2791  {
2792  ListCell *arg;
2793  bool first = true;
2794 
2795  /* Add all the arguments */
2796  foreach(arg, node->args)
2797  {
2798  TargetEntry *tle = (TargetEntry *) lfirst(arg);
2799  Node *n = (Node *) tle->expr;
2800 
2801  if (tle->resjunk)
2802  continue;
2803 
2804  if (!first)
2805  appendStringInfoString(buf, ", ");
2806  first = false;
2807 
2808  /* Add VARIADIC */
2809  if (use_variadic && lnext(arg) == NULL)
2810  appendStringInfoString(buf, "VARIADIC ");
2811 
2812  deparseExpr((Expr *) n, context);
2813  }
2814  }
2815 
2816  /* Add ORDER BY */
2817  if (node->aggorder != NIL)
2818  {
2819  appendStringInfoString(buf, " ORDER BY ");
2820  appendAggOrderBy(node->aggorder, node->args, context);
2821  }
2822  }
2823 
2824  /* Add FILTER (WHERE ..) */
2825  if (node->aggfilter != NULL)
2826  {
2827  appendStringInfoString(buf, ") FILTER (WHERE ");
2828  deparseExpr((Expr *) node->aggfilter, context);
2829  }
2830 
2831  appendStringInfoChar(buf, ')');
2832 }
2833 
2834 /*
2835  * Append ORDER BY within aggregate function.
2836  */
2837 static void
2838 appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
2839 {
2840  StringInfo buf = context->buf;
2841  ListCell *lc;
2842  bool first = true;
2843 
2844  foreach(lc, orderList)
2845  {
2846  SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
2847  Node *sortexpr;
2848  Oid sortcoltype;
2849  TypeCacheEntry *typentry;
2850 
2851  if (!first)
2852  appendStringInfoString(buf, ", ");
2853  first = false;
2854 
2855  sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
2856  context);
2857  sortcoltype = exprType(sortexpr);
2858  /* See whether operator is default < or > for datatype */
2859  typentry = lookup_type_cache(sortcoltype,
2861  if (srt->sortop == typentry->lt_opr)
2862  appendStringInfoString(buf, " ASC");
2863  else if (srt->sortop == typentry->gt_opr)
2864  appendStringInfoString(buf, " DESC");
2865  else
2866  {
2867  HeapTuple opertup;
2868  Form_pg_operator operform;
2869 
2870  appendStringInfoString(buf, " USING ");
2871 
2872  /* Append operator name. */
2873  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop));
2874  if (!HeapTupleIsValid(opertup))
2875  elog(ERROR, "cache lookup failed for operator %u", srt->sortop);
2876  operform = (Form_pg_operator) GETSTRUCT(opertup);
2877  deparseOperatorName(buf, operform);
2878  ReleaseSysCache(opertup);
2879  }
2880 
2881  if (srt->nulls_first)
2882  appendStringInfoString(buf, " NULLS FIRST");
2883  else
2884  appendStringInfoString(buf, " NULLS LAST");
2885  }
2886 }
2887 
2888 /*
2889  * Print the representation of a parameter to be sent to the remote side.
2890  *
2891  * Note: we always label the Param's type explicitly rather than relying on
2892  * transmitting a numeric type OID in PQexecParams(). This allows us to
2893  * avoid assuming that types have the same OIDs on the remote side as they
2894  * do locally --- they need only have the same names.
2895  */
2896 static void
2897 printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
2898  deparse_expr_cxt *context)
2899 {
2900  StringInfo buf = context->buf;
2901  char *ptypename = deparse_type_name(paramtype, paramtypmod);
2902 
2903  appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
2904 }
2905 
2906 /*
2907  * Print the representation of a placeholder for a parameter that will be
2908  * sent to the remote side at execution time.
2909  *
2910  * This is used when we're just trying to EXPLAIN the remote query.
2911  * We don't have the actual value of the runtime parameter yet, and we don't
2912  * want the remote planner to generate a plan that depends on such a value
2913  * anyway. Thus, we can't do something simple like "$1::paramtype".
2914  * Instead, we emit "((SELECT null::paramtype)::paramtype)".
2915  * In all extant versions of Postgres, the planner will see that as an unknown
2916  * constant value, which is what we want. This might need adjustment if we
2917  * ever make the planner flatten scalar subqueries. Note: the reason for the
2918  * apparently useless outer cast is to ensure that the representation as a
2919  * whole will be parsed as an a_expr and not a select_with_parens; the latter
2920  * would do the wrong thing in the context "x = ANY(...)".
2921  */
2922 static void
2923 printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
2924  deparse_expr_cxt *context)
2925 {
2926  StringInfo buf = context->buf;
2927  char *ptypename = deparse_type_name(paramtype, paramtypmod);
2928 
2929  appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename);
2930 }
2931 
2932 /*
2933  * Deparse GROUP BY clause.
2934  */
2935 static void
2937 {
2938  StringInfo buf = context->buf;
2939  Query *query = context->root->parse;
2940  ListCell *lc;
2941  bool first = true;
2942 
2943  /* Nothing to be done, if there's no GROUP BY clause in the query. */
2944  if (!query->groupClause)
2945  return;
2946 
2947  appendStringInfo(buf, " GROUP BY ");
2948 
2949  /*
2950  * Queries with grouping sets are not pushed down, so we don't expect
2951  * grouping sets here.
2952  */
2953  Assert(!query->groupingSets);
2954 
2955  foreach(lc, query->groupClause)
2956  {
2957  SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
2958 
2959  if (!first)
2960  appendStringInfoString(buf, ", ");
2961  first = false;
2962 
2963  deparseSortGroupClause(grp->tleSortGroupRef, tlist, context);
2964  }
2965 }
2966 
2967 /*
2968  * Deparse ORDER BY clause according to the given pathkeys for given base
2969  * relation. From given pathkeys expressions belonging entirely to the given
2970  * base relation are obtained and deparsed.
2971  */
2972 static void
2974 {
2975  ListCell *lcell;
2976  int nestlevel;
2977  char *delim = " ";
2978  RelOptInfo *baserel = context->scanrel;
2979  StringInfo buf = context->buf;
2980 
2981  /* Make sure any constants in the exprs are printed portably */
2982  nestlevel = set_transmission_modes();
2983 
2984  appendStringInfo(buf, " ORDER BY");
2985  foreach(lcell, pathkeys)
2986  {
2987  PathKey *pathkey = lfirst(lcell);
2988  Expr *em_expr;
2989 
2990  em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
2991  Assert(em_expr != NULL);
2992 
2993  appendStringInfoString(buf, delim);
2994  deparseExpr(em_expr, context);
2995  if (pathkey->pk_strategy == BTLessStrategyNumber)
2996  appendStringInfoString(buf, " ASC");
2997  else
2998  appendStringInfoString(buf, " DESC");
2999 
3000  if (pathkey->pk_nulls_first)
3001  appendStringInfoString(buf, " NULLS FIRST");
3002  else
3003  appendStringInfoString(buf, " NULLS LAST");
3004 
3005  delim = ", ";
3006  }
3007  reset_transmission_modes(nestlevel);
3008 }
3009 
3010 /*
3011  * appendFunctionName
3012  * Deparses function name from given function oid.
3013  */
3014 static void
3016 {
3017  StringInfo buf = context->buf;
3018  HeapTuple proctup;
3019  Form_pg_proc procform;
3020  const char *proname;
3021 
3022  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3023  if (!HeapTupleIsValid(proctup))
3024  elog(ERROR, "cache lookup failed for function %u", funcid);
3025  procform = (Form_pg_proc) GETSTRUCT(proctup);
3026 
3027  /* Print schema name only if it's not pg_catalog */
3028  if (procform->pronamespace != PG_CATALOG_NAMESPACE)
3029  {
3030  const char *schemaname;
3031 
3032  schemaname = get_namespace_name(procform->pronamespace);
3033  appendStringInfo(buf, "%s.", quote_identifier(schemaname));
3034  }
3035 
3036  /* Always print the function name */
3037  proname = NameStr(procform->proname);
3038  appendStringInfo(buf, "%s", quote_identifier(proname));
3039 
3040  ReleaseSysCache(proctup);
3041 }
3042 
3043 /*
3044  * Appends a sort or group clause.
3045  *
3046  * Like get_rule_sortgroupclause(), returns the expression tree, so caller
3047  * need not find it again.
3048  */
3049 static Node *
3051 {
3052  StringInfo buf = context->buf;
3053  TargetEntry *tle;
3054  Expr *expr;
3055 
3056  tle = get_sortgroupref_tle(ref, tlist);
3057  expr = tle->expr;
3058 
3059  if (expr && IsA(expr, Const))
3060  {
3061  /*
3062  * Force a typecast here so that we don't emit something like "GROUP
3063  * BY 2", which will be misconstrued as a column position rather than
3064  * a constant.
3065  */
3066  deparseConst((Const *) expr, context, 1);
3067  }
3068  else if (!expr || IsA(expr, Var))
3069  deparseExpr(expr, context);
3070  else
3071  {
3072  /* Always parenthesize the expression. */
3073  appendStringInfoString(buf, "(");
3074  deparseExpr(expr, context);
3075  appendStringInfoString(buf, ")");
3076  }
3077 
3078  return (Node *) expr;
3079 }
3080 
3081 
3082 /*
3083  * Returns true if given Var is deparsed as a subquery output column, in
3084  * which case, *relno and *colno are set to the IDs for the relation and
3085  * column alias to the Var provided by the subquery.
3086  */
3087 static bool
3088 is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
3089 {
3090  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3091  RelOptInfo *outerrel = fpinfo->outerrel;
3092  RelOptInfo *innerrel = fpinfo->innerrel;
3093 
3094  /* Should only be called in these cases. */
3095  Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
3096 
3097  /*
3098  * If the given relation isn't a join relation, it doesn't have any lower
3099  * subqueries, so the Var isn't a subquery output column.
3100  */
3101  if (!IS_JOIN_REL(foreignrel))
3102  return false;
3103 
3104  /*
3105  * If the Var doesn't belong to any lower subqueries, it isn't a subquery
3106  * output column.
3107  */
3108  if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels))
3109  return false;
3110 
3111  if (bms_is_member(node->varno, outerrel->relids))
3112  {
3113  /*
3114  * If outer relation is deparsed as a subquery, the Var is an output
3115  * column of the subquery; get the IDs for the relation/column alias.
3116  */
3117  if (fpinfo->make_outerrel_subquery)
3118  {
3119  get_relation_column_alias_ids(node, outerrel, relno, colno);
3120  return true;
3121  }
3122 
3123  /* Otherwise, recurse into the outer relation. */
3124  return is_subquery_var(node, outerrel, relno, colno);
3125  }
3126  else
3127  {
3128  Assert(bms_is_member(node->varno, innerrel->relids));
3129 
3130  /*
3131  * If inner relation is deparsed as a subquery, the Var is an output
3132  * column of the subquery; get the IDs for the relation/column alias.
3133  */
3134  if (fpinfo->make_innerrel_subquery)
3135  {
3136  get_relation_column_alias_ids(node, innerrel, relno, colno);
3137  return true;
3138  }
3139 
3140  /* Otherwise, recurse into the inner relation. */
3141  return is_subquery_var(node, innerrel, relno, colno);
3142  }
3143 }
3144 
3145 /*
3146  * Get the IDs for the relation and column alias to given Var belonging to
3147  * given relation, which are returned into *relno and *colno.
3148  */
3149 static void
3151  int *relno, int *colno)
3152 {
3153  PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
3154  int i;
3155  ListCell *lc;
3156 
3157  /* Get the relation alias ID */
3158  *relno = fpinfo->relation_index;
3159 
3160  /* Get the column alias ID */
3161  i = 1;
3162  foreach(lc, foreignrel->reltarget->exprs)
3163  {
3164  if (equal(lfirst(lc), (Node *) node))
3165  {
3166  *colno = i;
3167  return;
3168  }
3169  i++;
3170  }
3171 
3172  /* Shouldn't get here */
3173  elog(ERROR, "unexpected expression in subquery output");
3174 }
Datum constvalue
Definition: primnodes.h:196
List * aggdistinct
Definition: primnodes.h:303
Oid funcresulttype
Definition: primnodes.h:449
bool is_builtin(Oid objectId)
Definition: shippable.c:155
#define NIL
Definition: pg_list.h:69
List * rowMarks
Definition: relation.h:255
void deparseUpdateSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *targetAttrs, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1611
Expr * refassgnexpr
Definition: primnodes.h:409
bool aggvariadic
Definition: primnodes.h:306
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Query * parse
Definition: relation.h:154
static void deparseAggref(Aggref *node, deparse_expr_cxt *context)
Definition: deparse.c:2746
#define OperatorRelationId
Definition: pg_operator.h:32
Index varlevelsup
Definition: primnodes.h:173
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2649
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void appendGroupByClause(List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:2936
List * args
Definition: primnodes.h:456
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10246
static void deparseNullTest(NullTest *node, deparse_expr_cxt *context)
Definition: deparse.c:2687
struct deparse_expr_cxt deparse_expr_cxt
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2961
#define RelationGetDescr(relation)
Definition: rel.h:429
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, List **params_list)
Definition: deparse.c:1488
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2573
#define OIDOID
Definition: pg_type.h:328
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list)
Definition: deparse.c:1411
#define NUMERICOID
Definition: pg_type.h:554
#define ProcedureRelationId
Definition: pg_proc.h:33
Oid funccollid
Definition: primnodes.h:454
FDWCollateState state
Definition: deparse.c:91
static void deparseSubqueryTargetList(deparse_expr_cxt *context)
Definition: deparse.c:1376
List ** params_list
Definition: deparse.c:105
static void appendFunctionName(Oid funcid, deparse_expr_cxt *context)
Definition: deparse.c:3015
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
#define IS_UPPER_REL(rel)
Definition: relation.h:512
int resultRelation
Definition: parsenodes.h:120
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define IS_JOIN_REL(rel)
Definition: relation.h:509
Index tleSortGroupRef
Definition: parsenodes.h:1156
Expr * arg
Definition: primnodes.h:790
Oid inputcollid
Definition: primnodes.h:297
#define INT4OID
Definition: pg_type.h:316
List * groupingSets
Definition: parsenodes.h:148
void classifyConditions(PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds)
Definition: deparse.c:200
int set_transmission_modes(void)
Definition: nodes.h:509
#define VARBITOID
Definition: pg_type.h:546
Relids lower_subquery_rels
Definition: postgres_fdw.h:103
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:513
List * args
Definition: primnodes.h:301
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:216
static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col)
Definition: deparse.c:1899
AttrNumber varattno
Definition: primnodes.h:168
Oid array_typeid
Definition: primnodes.h:951
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform)
Definition: deparse.c:2545
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
#define heap_close(r, l)
Definition: heapam.h:97
static void deparseTargetList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs)
Definition: deparse.c:1095
bool aggstar
Definition: primnodes.h:305
unsigned int Oid
Definition: postgres_ext.h:31
static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context)
Definition: deparse.c:2385
Definition: primnodes.h:163
#define ESCAPE_STRING_SYNTAX
Definition: c.h:510
#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
#define PVC_RECURSE_PLACEHOLDERS
Definition: var.h:26
int natts
Definition: tupdesc.h:73
#define lsecond(l)
Definition: pg_list.h:116
int pk_strategy
Definition: relation.h:851
RelOptInfo * outerrel
Definition: postgres_fdw.h:89
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
signed int int32
Definition: c.h:256
static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2897
JoinType
Definition: nodes.h:672
void deparseInsertSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *targetAttrs, bool doNothing, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1548
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
#define AGGKIND_IS_ORDERED_SET(kind)
Definition: pg_aggregate.h:129
Oid consttype
Definition: primnodes.h:192
FDWCollateState
Definition: deparse.c:78
static ListCell * list_tail(List *l)
Definition: pg_list.h:83
CoercionForm funcformat
Definition: primnodes.h:453
bool make_outerrel_subquery
Definition: postgres_fdw.h:99
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:254
void pfree(void *pointer)
Definition: mcxt.c:950
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:157
void deparseDeleteSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1721
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
bool resjunk
Definition: primnodes.h:1374
#define linitial(l)
Definition: pg_list.h:111
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
Oid funcid
Definition: primnodes.h:448
bool pk_nulls_first
Definition: relation.h:852
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static void deparseExplicitTargetList(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1345
static void deparseParam(Param *node, deparse_expr_cxt *context)
Definition: deparse.c:2352
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2591
#define ADD_REL_QUALIFIER(buf, varno)
Definition: deparse.c:110
#define SUBQUERY_COL_ALIAS_PREFIX
Definition: deparse.c:113
Oid paramcollid
Definition: primnodes.h:248
#define lfirst_int(lc)
Definition: pg_list.h:107
Oid vartype
Definition: primnodes.h:170
char * defGetString(DefElem *def)
Definition: define.c:49
BoolExprType boolop
Definition: primnodes.h:561
#define SUBQUERY_REL_ALIAS_PREFIX
Definition: deparse.c:112
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2494
Expr * arg
Definition: primnodes.h:1179
TriggerDesc * trigdesc
Definition: rel.h:120
Oid constcollid
Definition: primnodes.h:194
static void appendOrderByClause(List *pathkeys, deparse_expr_cxt *context)
Definition: deparse.c:2973
#define lfirst_node(type, lc)
Definition: pg_list.h:109
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
#define INT2OID
Definition: pg_type.h:308
Relids relids
Definition: deparse.c:70
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
bool trig_delete_after_row
Definition: reltrigger.h:66
static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2720
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:179
char * c
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
List * refupperindexpr
Definition: primnodes.h:403
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#define REL_ALIAS_PREFIX
Definition: deparse.c:108
List * reflowerindexpr
Definition: primnodes.h:405
const char * get_jointype_name(JoinType jointype)
Definition: deparse.c:1311
List * aggorder
Definition: primnodes.h:302
PlannerInfo * root
Definition: deparse.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:437
RelOptInfo * foreignrel
Definition: deparse.c:69
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define TableOidAttributeNumber
Definition: sysattr.h:27
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
Relids relids
Definition: relation.h:524
List * aggdirectargs
Definition: primnodes.h:300
List * elements
Definition: primnodes.h:954
#define TYPECACHE_GT_OPR
Definition: typcache.h:112
bool trig_insert_after_row
Definition: reltrigger.h:56
static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt)
Definition: deparse.c:290
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2404
#define lnext(lc)
Definition: pg_list.h:105
void deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1743
Oid opcollid
Definition: primnodes.h:499
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
void deparseAnalyzeSql(StringInfo buf, Relation rel, List **retrieved_attrs)
Definition: deparse.c:1838
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context)
Definition: deparse.c:1003
Definition: nodes.h:291
List * lappend_int(List *list, int datum)
Definition: list.c:146
Index relid
Definition: relation.h:552
bool trig_update_after_row
Definition: reltrigger.h:61
static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
Definition: deparse.c:2636
static void deparseVar(Var *node, deparse_expr_cxt *context)
Definition: deparse.c:2181
Definition: nodes.h:147
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1746
static char ** options
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
Definition: deparse.c:2838
Index varno
Definition: primnodes.h:166
Definition: nodes.h:146
char * get_relid_attribute_name(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:801
List * exprs
Definition: relation.h:882
static void appendConditions(List *exprs, deparse_expr_cxt *context)
Definition: deparse.c:1277
static Node * deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context)
Definition: deparse.c:3050
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:113
StringInfo buf
Definition: deparse.c:104
void deparseStringLiteral(StringInfo buf, const char *val)
Definition: deparse.c:2084
#define FLOAT4OID
Definition: pg_type.h:416
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
Oid resulttype
Definition: primnodes.h:791
unsigned int Index
Definition: c.h:365
static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3088
NullTestType nulltesttype
Definition: primnodes.h:1180
static char * deparse_type_name(Oid type_oid, int32 typemod)
Definition: deparse.c:851
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, bool is_subquery, List **retrieved_attrs, List **params_list)
Definition: deparse.c:925
Oid resultcollid
Definition: primnodes.h:793
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define InvalidOid
Definition: postgres_ext.h:36
Oid aggfnoid
Definition: primnodes.h:294
struct foreign_loc_cxt foreign_loc_cxt
void * fdw_private
Definition: relation.h:576
static void deparseRelation(StringInfo buf, Relation rel)
Definition: deparse.c:2044
#define INT8OID
Definition: pg_type.h:304
CmdType commandType
Definition: parsenodes.h:110
int32 paramtypmod
Definition: primnodes.h:247
PlannerInfo * root
Definition: deparse.c:99
#define BITOID
Definition: pg_type.h:542
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
Bitmapset * attrs_used
Definition: postgres_fdw.h:50
char * format_type_with_typemod_qualified(Oid type_oid, int32 typemod)
Definition: format_type.c:123
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Oid inputcollid
Definition: primnodes.h:455
#define NULL
Definition: c.h:229
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
Definition: deparse.c:3150
#define Assert(condition)
Definition: c.h:675
List * add_to_flat_tlist(List *tlist, List *exprs)
Definition: tlist.c:135
#define lfirst(lc)
Definition: pg_list.h:106
Definition: regguts.h:298
static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
Definition: deparse.c:2246
Expr * expr
Definition: primnodes.h:1367
AggSplit aggsplit
Definition: primnodes.h:310
LockClauseStrength strength
Definition: plannodes.h:1003
EquivalenceClass * pk_eclass
Definition: relation.h:849
Oid array_collid
Definition: primnodes.h:952
void deparseAnalyzeSizeSql(StringInfo buf, Relation rel)
Definition: deparse.c:1818
void reset_transmission_modes(int nestlevel)
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid refcollid
Definition: primnodes.h:402
#define FLOAT8OID
Definition: pg_type.h:419
Expr * aggfilter
Definition: primnodes.h:304
Oid inputcollid
Definition: primnodes.h:500
static void deparseFromExpr(List *quals, deparse_expr_cxt *context)
Definition: deparse.c:1061
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
#define BOOLOID
Definition: pg_type.h:288
List * args
Definition: primnodes.h:562
#define UNKNOWNOID
Definition: pg_type.h:431
FormData_pg_operator* Form_pg_operator
Definition: pg_operator.h:57
struct foreign_glob_cxt foreign_glob_cxt
#define nodeTag(nodeptr)
Definition: nodes.h:514
int32 consttypmod
Definition: primnodes.h:193
static void deparseExpr(Expr *expr, deparse_expr_cxt *context)
Definition: deparse.c:2119
List * options
Definition: foreign.h:68
RelOptInfo * innerrel
Definition: postgres_fdw.h:90
List * build_tlist_to_deparse(RelOptInfo *foreignrel)
Definition: deparse.c:868
List * groupClause
Definition: parsenodes.h:146
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
Oid aggcollid
Definition: primnodes.h:296
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: deparse.c:1653
static int fe(enum e x)
Definition: preproc-init.c:111
static void deparseLockingClause(deparse_expr_cxt *context)
Definition: deparse.c:1187
bool is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr)
Definition: deparse.c:226
int i
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:401
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define TYPECACHE_LT_OPR
Definition: typcache.h:111
#define NameStr(name)
Definition: c.h:499
Oid varcollid
Definition: primnodes.h:172
void * arg
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:507
static void deparseReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool trig_after_row, List *returningList, List **retrieved_attrs)
Definition: deparse.c:1777
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:878
bool argisrow
Definition: primnodes.h:1181
char aggkind
Definition: primnodes.h:308
char * defname
Definition: parsenodes.h:719
int32 resulttypmod
Definition: primnodes.h:792
Oid opno
Definition: primnodes.h:495
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:501
RelOptInfo * scanrel
Definition: deparse.c:101
CoercionForm relabelformat
Definition: primnodes.h:794
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
RelOptInfo * foreignrel
Definition: deparse.c:100
struct PathTarget * reltarget
Definition: relation.h:535
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:417
long val
Definition: informix.c:689
Oid paramtype
Definition: primnodes.h:246
bool constisnull
Definition: primnodes.h:197
bool funcvariadic
Definition: primnodes.h:451
static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context)
Definition: deparse.c:2923
Expr * refexpr
Definition: primnodes.h:407
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
Definition: deparse.c:2431
Definition: nodes.h:148
int32 vartypmod
Definition: primnodes.h:171
#define RelationGetNamespace(relation)
Definition: rel.h:444