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