PostgreSQL Source Code  git master
ruleutils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * ruleutils.c
4  * Functions to convert stored expressions/querytrees back to
5  * source text
6  *
7  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  * src/backend/utils/adt/ruleutils.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17 
18 #include <ctype.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 
22 #include "access/amapi.h"
23 #include "access/htup_details.h"
24 #include "access/relation.h"
25 #include "access/sysattr.h"
26 #include "access/table.h"
27 #include "catalog/pg_aggregate.h"
28 #include "catalog/pg_am.h"
29 #include "catalog/pg_authid.h"
30 #include "catalog/pg_collation.h"
31 #include "catalog/pg_constraint.h"
32 #include "catalog/pg_depend.h"
33 #include "catalog/pg_language.h"
34 #include "catalog/pg_opclass.h"
35 #include "catalog/pg_operator.h"
37 #include "catalog/pg_proc.h"
39 #include "catalog/pg_trigger.h"
40 #include "catalog/pg_type.h"
41 #include "commands/defrem.h"
42 #include "commands/tablespace.h"
43 #include "common/keywords.h"
44 #include "executor/spi.h"
45 #include "funcapi.h"
46 #include "mb/pg_wchar.h"
47 #include "miscadmin.h"
48 #include "nodes/makefuncs.h"
49 #include "nodes/nodeFuncs.h"
50 #include "nodes/pathnodes.h"
51 #include "optimizer/optimizer.h"
52 #include "parser/parse_agg.h"
53 #include "parser/parse_func.h"
54 #include "parser/parse_node.h"
55 #include "parser/parse_oper.h"
56 #include "parser/parser.h"
57 #include "parser/parsetree.h"
58 #include "rewrite/rewriteHandler.h"
59 #include "rewrite/rewriteManip.h"
60 #include "rewrite/rewriteSupport.h"
61 #include "utils/array.h"
62 #include "utils/builtins.h"
63 #include "utils/fmgroids.h"
64 #include "utils/guc.h"
65 #include "utils/hsearch.h"
66 #include "utils/lsyscache.h"
67 #include "utils/partcache.h"
68 #include "utils/rel.h"
69 #include "utils/ruleutils.h"
70 #include "utils/snapmgr.h"
71 #include "utils/syscache.h"
72 #include "utils/typcache.h"
73 #include "utils/varlena.h"
74 #include "utils/xml.h"
75 
76 /* ----------
77  * Pretty formatting constants
78  * ----------
79  */
80 
81 /* Indent counts */
82 #define PRETTYINDENT_STD 8
83 #define PRETTYINDENT_JOIN 4
84 #define PRETTYINDENT_VAR 4
85 
86 #define PRETTYINDENT_LIMIT 40 /* wrap limit */
87 
88 /* Pretty flags */
89 #define PRETTYFLAG_PAREN 0x0001
90 #define PRETTYFLAG_INDENT 0x0002
91 #define PRETTYFLAG_SCHEMA 0x0004
92 
93 /* Default line length for pretty-print wrapping: 0 means wrap always */
94 #define WRAP_COLUMN_DEFAULT 0
95 
96 /* macros to test if pretty action needed */
97 #define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)
98 #define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)
99 #define PRETTY_SCHEMA(context) ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
100 
101 
102 /* ----------
103  * Local data types
104  * ----------
105  */
106 
107 /* Context info needed for invoking a recursive querytree display routine */
108 typedef struct
109 {
110  StringInfo buf; /* output buffer to append to */
111  List *namespaces; /* List of deparse_namespace nodes */
112  List *windowClause; /* Current query level's WINDOW clause */
113  List *windowTList; /* targetlist for resolving WINDOW clause */
114  int prettyFlags; /* enabling of pretty-print functions */
115  int wrapColumn; /* max line length, or -1 for no limit */
116  int indentLevel; /* current indent level for pretty-print */
117  bool varprefix; /* true to print prefixes on Vars */
118  ParseExprKind special_exprkind; /* set only for exprkinds needing special
119  * handling */
120  Bitmapset *appendparents; /* if not null, map child Vars of these relids
121  * back to the parent rel */
123 
124 /*
125  * Each level of query context around a subtree needs a level of Var namespace.
126  * A Var having varlevelsup=N refers to the N'th item (counting from 0) in
127  * the current context's namespaces list.
128  *
129  * rtable is the list of actual RTEs from the Query or PlannedStmt.
130  * rtable_names holds the alias name to be used for each RTE (either a C
131  * string, or NULL for nameless RTEs such as unnamed joins).
132  * rtable_columns holds the column alias names to be used for each RTE.
133  *
134  * subplans is a list of Plan trees for SubPlans and CTEs (it's only used
135  * in the PlannedStmt case).
136  * ctes is a list of CommonTableExpr nodes (only used in the Query case).
137  * appendrels, if not null (it's only used in the PlannedStmt case), is an
138  * array of AppendRelInfo nodes, indexed by child relid. We use that to map
139  * child-table Vars to their inheritance parents.
140  *
141  * In some cases we need to make names of merged JOIN USING columns unique
142  * across the whole query, not only per-RTE. If so, unique_using is true
143  * and using_names is a list of C strings representing names already assigned
144  * to USING columns.
145  *
146  * When deparsing plan trees, there is always just a single item in the
147  * deparse_namespace list (since a plan tree never contains Vars with
148  * varlevelsup > 0). We store the Plan node that is the immediate
149  * parent of the expression to be deparsed, as well as a list of that
150  * Plan's ancestors. In addition, we store its outer and inner subplan nodes,
151  * as well as their targetlists, and the index tlist if the current plan node
152  * might contain INDEX_VAR Vars. (These fields could be derived on-the-fly
153  * from the current Plan node, but it seems notationally clearer to set them
154  * up as separate fields.)
155  */
156 typedef struct
157 {
158  List *rtable; /* List of RangeTblEntry nodes */
159  List *rtable_names; /* Parallel list of names for RTEs */
160  List *rtable_columns; /* Parallel list of deparse_columns structs */
161  List *subplans; /* List of Plan trees for SubPlans */
162  List *ctes; /* List of CommonTableExpr nodes */
163  AppendRelInfo **appendrels; /* Array of AppendRelInfo nodes, or NULL */
164  /* Workspace for column alias assignment: */
165  bool unique_using; /* Are we making USING names globally unique */
166  List *using_names; /* List of assigned names for USING columns */
167  /* Remaining fields are used only when deparsing a Plan tree: */
168  Plan *plan; /* immediate parent of current expression */
169  List *ancestors; /* ancestors of plan */
170  Plan *outer_plan; /* outer subnode, or NULL if none */
171  Plan *inner_plan; /* inner subnode, or NULL if none */
172  List *outer_tlist; /* referent for OUTER_VAR Vars */
173  List *inner_tlist; /* referent for INNER_VAR Vars */
174  List *index_tlist; /* referent for INDEX_VAR Vars */
175  /* Special namespace representing a function signature: */
176  char *funcname;
177  int numargs;
178  char **argnames;
180 
181 /*
182  * Per-relation data about column alias names.
183  *
184  * Selecting aliases is unreasonably complicated because of the need to dump
185  * rules/views whose underlying tables may have had columns added, deleted, or
186  * renamed since the query was parsed. We must nonetheless print the rule/view
187  * in a form that can be reloaded and will produce the same results as before.
188  *
189  * For each RTE used in the query, we must assign column aliases that are
190  * unique within that RTE. SQL does not require this of the original query,
191  * but due to factors such as *-expansion we need to be able to uniquely
192  * reference every column in a decompiled query. As long as we qualify all
193  * column references, per-RTE uniqueness is sufficient for that.
194  *
195  * However, we can't ensure per-column name uniqueness for unnamed join RTEs,
196  * since they just inherit column names from their input RTEs, and we can't
197  * rename the columns at the join level. Most of the time this isn't an issue
198  * because we don't need to reference the join's output columns as such; we
199  * can reference the input columns instead. That approach can fail for merged
200  * JOIN USING columns, however, so when we have one of those in an unnamed
201  * join, we have to make that column's alias globally unique across the whole
202  * query to ensure it can be referenced unambiguously.
203  *
204  * Another problem is that a JOIN USING clause requires the columns to be
205  * merged to have the same aliases in both input RTEs, and that no other
206  * columns in those RTEs or their children conflict with the USING names.
207  * To handle that, we do USING-column alias assignment in a recursive
208  * traversal of the query's jointree. When descending through a JOIN with
209  * USING, we preassign the USING column names to the child columns, overriding
210  * other rules for column alias assignment. We also mark each RTE with a list
211  * of all USING column names selected for joins containing that RTE, so that
212  * when we assign other columns' aliases later, we can avoid conflicts.
213  *
214  * Another problem is that if a JOIN's input tables have had columns added or
215  * deleted since the query was parsed, we must generate a column alias list
216  * for the join that matches the current set of input columns --- otherwise, a
217  * change in the number of columns in the left input would throw off matching
218  * of aliases to columns of the right input. Thus, positions in the printable
219  * column alias list are not necessarily one-for-one with varattnos of the
220  * JOIN, so we need a separate new_colnames[] array for printing purposes.
221  */
222 typedef struct
223 {
224  /*
225  * colnames is an array containing column aliases to use for columns that
226  * existed when the query was parsed. Dropped columns have NULL entries.
227  * This array can be directly indexed by varattno to get a Var's name.
228  *
229  * Non-NULL entries are guaranteed unique within the RTE, *except* when
230  * this is for an unnamed JOIN RTE. In that case we merely copy up names
231  * from the two input RTEs.
232  *
233  * During the recursive descent in set_using_names(), forcible assignment
234  * of a child RTE's column name is represented by pre-setting that element
235  * of the child's colnames array. So at that stage, NULL entries in this
236  * array just mean that no name has been preassigned, not necessarily that
237  * the column is dropped.
238  */
239  int num_cols; /* length of colnames[] array */
240  char **colnames; /* array of C strings and NULLs */
241 
242  /*
243  * new_colnames is an array containing column aliases to use for columns
244  * that would exist if the query was re-parsed against the current
245  * definitions of its base tables. This is what to print as the column
246  * alias list for the RTE. This array does not include dropped columns,
247  * but it will include columns added since original parsing. Indexes in
248  * it therefore have little to do with current varattno values. As above,
249  * entries are unique unless this is for an unnamed JOIN RTE. (In such an
250  * RTE, we never actually print this array, but we must compute it anyway
251  * for possible use in computing column names of upper joins.) The
252  * parallel array is_new_col marks which of these columns are new since
253  * original parsing. Entries with is_new_col false must match the
254  * non-NULL colnames entries one-for-one.
255  */
256  int num_new_cols; /* length of new_colnames[] array */
257  char **new_colnames; /* array of C strings */
258  bool *is_new_col; /* array of bool flags */
259 
260  /* This flag tells whether we should actually print a column alias list */
262 
263  /* This list has all names used as USING names in joins above this RTE */
264  List *parentUsing; /* names assigned to parent merged columns */
265 
266  /*
267  * If this struct is for a JOIN RTE, we fill these fields during the
268  * set_using_names() pass to describe its relationship to its child RTEs.
269  *
270  * leftattnos and rightattnos are arrays with one entry per existing
271  * output column of the join (hence, indexable by join varattno). For a
272  * simple reference to a column of the left child, leftattnos[i] is the
273  * child RTE's attno and rightattnos[i] is zero; and conversely for a
274  * column of the right child. But for merged columns produced by JOIN
275  * USING/NATURAL JOIN, both leftattnos[i] and rightattnos[i] are nonzero.
276  * Note that a simple reference might be to a child RTE column that's been
277  * dropped; but that's OK since the column could not be used in the query.
278  *
279  * If it's a JOIN USING, usingNames holds the alias names selected for the
280  * merged columns (these might be different from the original USING list,
281  * if we had to modify names to achieve uniqueness).
282  */
283  int leftrti; /* rangetable index of left child */
284  int rightrti; /* rangetable index of right child */
285  int *leftattnos; /* left-child varattnos of join cols, or 0 */
286  int *rightattnos; /* right-child varattnos of join cols, or 0 */
287  List *usingNames; /* names assigned to merged columns */
289 
290 /* This macro is analogous to rt_fetch(), but for deparse_columns structs */
291 #define deparse_columns_fetch(rangetable_index, dpns) \
292  ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
293 
294 /*
295  * Entry in set_rtable_names' hash table
296  */
297 typedef struct
298 {
299  char name[NAMEDATALEN]; /* Hash key --- must be first */
300  int counter; /* Largest addition used so far for name */
301 } NameHashEntry;
302 
303 /* Callback signature for resolve_special_varno() */
304 typedef void (*rsv_callback) (Node *node, deparse_context *context,
305  void *callback_arg);
306 
307 
308 /* ----------
309  * Global data
310  * ----------
311  */
313 static const char *query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1";
315 static const char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
316 
317 /* GUC parameters */
319 
320 
321 /* ----------
322  * Local functions
323  *
324  * Most of these functions used to use fixed-size buffers to build their
325  * results. Now, they take an (already initialized) StringInfo object
326  * as a parameter, and append their text output to its contents.
327  * ----------
328  */
329 static char *deparse_expression_pretty(Node *expr, List *dpcontext,
330  bool forceprefix, bool showimplicit,
331  int prettyFlags, int startIndent);
332 static char *pg_get_viewdef_worker(Oid viewoid,
333  int prettyFlags, int wrapColumn);
334 static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
335 static int decompile_column_index_array(Datum column_index_array, Oid relId,
336  StringInfo buf);
337 static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
338 static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
339  const Oid *excludeOps,
340  bool attrsOnly, bool keysOnly,
341  bool showTblSpc, bool inherits,
342  int prettyFlags, bool missing_ok);
343 static char *pg_get_statisticsobj_worker(Oid statextid, bool columns_only,
344  bool missing_ok);
345 static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags,
346  bool attrsOnly, bool missing_ok);
347 static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
348  int prettyFlags, bool missing_ok);
349 static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname,
350  int prettyFlags);
352  bool print_table_args, bool print_defaults);
353 static void print_function_rettype(StringInfo buf, HeapTuple proctup);
354 static void print_function_trftypes(StringInfo buf, HeapTuple proctup);
355 static void print_function_sqlbody(StringInfo buf, HeapTuple proctup);
356 static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
357  Bitmapset *rels_used);
358 static void set_deparse_for_query(deparse_namespace *dpns, Query *query,
359  List *parent_namespaces);
360 static void set_simple_column_names(deparse_namespace *dpns);
361 static bool has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode);
362 static void set_using_names(deparse_namespace *dpns, Node *jtnode,
363  List *parentUsing);
365  RangeTblEntry *rte,
366  deparse_columns *colinfo);
368  deparse_columns *colinfo);
369 static bool colname_is_unique(const char *colname, deparse_namespace *dpns,
370  deparse_columns *colinfo);
371 static char *make_colname_unique(char *colname, deparse_namespace *dpns,
372  deparse_columns *colinfo);
373 static void expand_colnames_array_to(deparse_columns *colinfo, int n);
374 static void identify_join_columns(JoinExpr *j, RangeTblEntry *jrte,
375  deparse_columns *colinfo);
376 static char *get_rtable_name(int rtindex, deparse_context *context);
377 static void set_deparse_plan(deparse_namespace *dpns, Plan *plan);
378 static void push_child_plan(deparse_namespace *dpns, Plan *plan,
379  deparse_namespace *save_dpns);
380 static void pop_child_plan(deparse_namespace *dpns,
381  deparse_namespace *save_dpns);
382 static void push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
383  deparse_namespace *save_dpns);
384 static void pop_ancestor_plan(deparse_namespace *dpns,
385  deparse_namespace *save_dpns);
386 static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
387  int prettyFlags);
388 static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
389  int prettyFlags, int wrapColumn);
390 static void get_query_def(Query *query, StringInfo buf, List *parentnamespace,
391  TupleDesc resultDesc,
392  int prettyFlags, int wrapColumn, int startIndent);
393 static void get_values_def(List *values_lists, deparse_context *context);
394 static void get_with_clause(Query *query, deparse_context *context);
395 static void get_select_query_def(Query *query, deparse_context *context,
396  TupleDesc resultDesc);
397 static void get_insert_query_def(Query *query, deparse_context *context);
398 static void get_update_query_def(Query *query, deparse_context *context);
399 static void get_update_query_targetlist_def(Query *query, List *targetList,
400  deparse_context *context,
401  RangeTblEntry *rte);
402 static void get_delete_query_def(Query *query, deparse_context *context);
403 static void get_utility_query_def(Query *query, deparse_context *context);
404 static void get_basic_select_query(Query *query, deparse_context *context,
405  TupleDesc resultDesc);
406 static void get_target_list(List *targetList, deparse_context *context,
407  TupleDesc resultDesc);
408 static void get_setop_query(Node *setOp, Query *query,
409  deparse_context *context,
410  TupleDesc resultDesc);
411 static Node *get_rule_sortgroupclause(Index ref, List *tlist,
412  bool force_colno,
413  deparse_context *context);
414 static void get_rule_groupingset(GroupingSet *gset, List *targetlist,
415  bool omit_parens, deparse_context *context);
416 static void get_rule_orderby(List *orderList, List *targetList,
417  bool force_colno, deparse_context *context);
418 static void get_rule_windowclause(Query *query, deparse_context *context);
419 static void get_rule_windowspec(WindowClause *wc, List *targetList,
420  deparse_context *context);
421 static char *get_variable(Var *var, int levelsup, bool istoplevel,
422  deparse_context *context);
423 static void get_special_variable(Node *node, deparse_context *context,
424  void *callback_arg);
425 static void resolve_special_varno(Node *node, deparse_context *context,
426  rsv_callback callback, void *callback_arg);
427 static Node *find_param_referent(Param *param, deparse_context *context,
428  deparse_namespace **dpns_p, ListCell **ancestor_cell_p);
429 static void get_parameter(Param *param, deparse_context *context);
430 static const char *get_simple_binary_op_name(OpExpr *expr);
431 static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);
432 static void appendContextKeyword(deparse_context *context, const char *str,
433  int indentBefore, int indentAfter, int indentPlus);
435 static void get_rule_expr(Node *node, deparse_context *context,
436  bool showimplicit);
437 static void get_rule_expr_toplevel(Node *node, deparse_context *context,
438  bool showimplicit);
439 static void get_rule_expr_funccall(Node *node, deparse_context *context,
440  bool showimplicit);
441 static bool looks_like_function(Node *node);
442 static void get_oper_expr(OpExpr *expr, deparse_context *context);
443 static void get_func_expr(FuncExpr *expr, deparse_context *context,
444  bool showimplicit);
445 static void get_agg_expr(Aggref *aggref, deparse_context *context,
446  Aggref *original_aggref);
447 static void get_agg_combine_expr(Node *node, deparse_context *context,
448  void *callback_arg);
449 static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context);
450 static bool get_func_sql_syntax(FuncExpr *expr, deparse_context *context);
451 static void get_coercion_expr(Node *arg, deparse_context *context,
452  Oid resulttype, int32 resulttypmod,
453  Node *parentNode);
454 static void get_const_expr(Const *constval, deparse_context *context,
455  int showtype);
456 static void get_const_collation(Const *constval, deparse_context *context);
457 static void simple_quote_literal(StringInfo buf, const char *val);
458 static void get_sublink_expr(SubLink *sublink, deparse_context *context);
459 static void get_tablefunc(TableFunc *tf, deparse_context *context,
460  bool showimplicit);
461 static void get_from_clause(Query *query, const char *prefix,
462  deparse_context *context);
463 static void get_from_clause_item(Node *jtnode, Query *query,
464  deparse_context *context);
465 static void get_column_alias_list(deparse_columns *colinfo,
466  deparse_context *context);
467 static void get_from_clause_coldeflist(RangeTblFunction *rtfunc,
468  deparse_columns *colinfo,
469  deparse_context *context);
470 static void get_tablesample_def(TableSampleClause *tablesample,
471  deparse_context *context);
472 static void get_opclass_name(Oid opclass, Oid actual_datatype,
473  StringInfo buf);
474 static Node *processIndirection(Node *node, deparse_context *context);
475 static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context);
476 static char *get_relation_name(Oid relid);
477 static char *generate_relation_name(Oid relid, List *namespaces);
478 static char *generate_qualified_relation_name(Oid relid);
479 static char *generate_function_name(Oid funcid, int nargs,
480  List *argnames, Oid *argtypes,
481  bool has_variadic, bool *use_variadic_p,
482  ParseExprKind special_exprkind);
483 static char *generate_operator_name(Oid operid, Oid arg1, Oid arg2);
484 static void add_cast_to(StringInfo buf, Oid typid);
485 static char *generate_qualified_type_name(Oid typid);
486 static text *string_to_text(char *str);
487 static char *flatten_reloptions(Oid relid);
488 static void get_reloptions(StringInfo buf, Datum reloptions);
489 
490 #define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
491 
492 
493 /* ----------
494  * pg_get_ruledef - Do it all and return a text
495  * that could be used as a statement
496  * to recreate the rule
497  * ----------
498  */
499 Datum
501 {
502  Oid ruleoid = PG_GETARG_OID(0);
503  int prettyFlags;
504  char *res;
505 
506  prettyFlags = PRETTYFLAG_INDENT;
507 
508  res = pg_get_ruledef_worker(ruleoid, prettyFlags);
509 
510  if (res == NULL)
511  PG_RETURN_NULL();
512 
514 }
515 
516 
517 Datum
519 {
520  Oid ruleoid = PG_GETARG_OID(0);
521  bool pretty = PG_GETARG_BOOL(1);
522  int prettyFlags;
523  char *res;
524 
526 
527  res = pg_get_ruledef_worker(ruleoid, prettyFlags);
528 
529  if (res == NULL)
530  PG_RETURN_NULL();
531 
533 }
534 
535 
536 static char *
537 pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
538 {
539  Datum args[1];
540  char nulls[1];
541  int spirc;
542  HeapTuple ruletup;
543  TupleDesc rulettc;
545 
546  /*
547  * Do this first so that string is alloc'd in outer context not SPI's.
548  */
549  initStringInfo(&buf);
550 
551  /*
552  * Connect to SPI manager
553  */
554  if (SPI_connect() != SPI_OK_CONNECT)
555  elog(ERROR, "SPI_connect failed");
556 
557  /*
558  * On the first call prepare the plan to lookup pg_rewrite. We read
559  * pg_rewrite over the SPI manager instead of using the syscache to be
560  * checked for read access on pg_rewrite.
561  */
562  if (plan_getrulebyoid == NULL)
563  {
564  Oid argtypes[1];
565  SPIPlanPtr plan;
566 
567  argtypes[0] = OIDOID;
568  plan = SPI_prepare(query_getrulebyoid, 1, argtypes);
569  if (plan == NULL)
570  elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid);
571  SPI_keepplan(plan);
572  plan_getrulebyoid = plan;
573  }
574 
575  /*
576  * Get the pg_rewrite tuple for this rule
577  */
578  args[0] = ObjectIdGetDatum(ruleoid);
579  nulls[0] = ' ';
580  spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 0);
581  if (spirc != SPI_OK_SELECT)
582  elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
583  if (SPI_processed != 1)
584  {
585  /*
586  * There is no tuple data available here, just keep the output buffer
587  * empty.
588  */
589  }
590  else
591  {
592  /*
593  * Get the rule's definition and put it into executor's memory
594  */
595  ruletup = SPI_tuptable->vals[0];
596  rulettc = SPI_tuptable->tupdesc;
597  make_ruledef(&buf, ruletup, rulettc, prettyFlags);
598  }
599 
600  /*
601  * Disconnect from SPI manager
602  */
603  if (SPI_finish() != SPI_OK_FINISH)
604  elog(ERROR, "SPI_finish failed");
605 
606  if (buf.len == 0)
607  return NULL;
608 
609  return buf.data;
610 }
611 
612 
613 /* ----------
614  * pg_get_viewdef - Mainly the same thing, but we
615  * only return the SELECT part of a view
616  * ----------
617  */
618 Datum
620 {
621  /* By OID */
622  Oid viewoid = PG_GETARG_OID(0);
623  int prettyFlags;
624  char *res;
625 
626  prettyFlags = PRETTYFLAG_INDENT;
627 
628  res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
629 
630  if (res == NULL)
631  PG_RETURN_NULL();
632 
634 }
635 
636 
637 Datum
639 {
640  /* By OID */
641  Oid viewoid = PG_GETARG_OID(0);
642  bool pretty = PG_GETARG_BOOL(1);
643  int prettyFlags;
644  char *res;
645 
647 
648  res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
649 
650  if (res == NULL)
651  PG_RETURN_NULL();
652 
654 }
655 
656 Datum
658 {
659  /* By OID */
660  Oid viewoid = PG_GETARG_OID(0);
661  int wrap = PG_GETARG_INT32(1);
662  int prettyFlags;
663  char *res;
664 
665  /* calling this implies we want pretty printing */
667 
668  res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
669 
670  if (res == NULL)
671  PG_RETURN_NULL();
672 
674 }
675 
676 Datum
678 {
679  /* By qualified name */
680  text *viewname = PG_GETARG_TEXT_PP(0);
681  int prettyFlags;
682  RangeVar *viewrel;
683  Oid viewoid;
684  char *res;
685 
686  prettyFlags = PRETTYFLAG_INDENT;
687 
688  /* Look up view name. Can't lock it - we might not have privileges. */
690  viewoid = RangeVarGetRelid(viewrel, NoLock, false);
691 
692  res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
693 
694  if (res == NULL)
695  PG_RETURN_NULL();
696 
698 }
699 
700 
701 Datum
703 {
704  /* By qualified name */
705  text *viewname = PG_GETARG_TEXT_PP(0);
706  bool pretty = PG_GETARG_BOOL(1);
707  int prettyFlags;
708  RangeVar *viewrel;
709  Oid viewoid;
710  char *res;
711 
713 
714  /* Look up view name. Can't lock it - we might not have privileges. */
716  viewoid = RangeVarGetRelid(viewrel, NoLock, false);
717 
718  res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
719 
720  if (res == NULL)
721  PG_RETURN_NULL();
722 
724 }
725 
726 /*
727  * Common code for by-OID and by-name variants of pg_get_viewdef
728  */
729 static char *
730 pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
731 {
732  Datum args[2];
733  char nulls[2];
734  int spirc;
735  HeapTuple ruletup;
736  TupleDesc rulettc;
738 
739  /*
740  * Do this first so that string is alloc'd in outer context not SPI's.
741  */
742  initStringInfo(&buf);
743 
744  /*
745  * Connect to SPI manager
746  */
747  if (SPI_connect() != SPI_OK_CONNECT)
748  elog(ERROR, "SPI_connect failed");
749 
750  /*
751  * On the first call prepare the plan to lookup pg_rewrite. We read
752  * pg_rewrite over the SPI manager instead of using the syscache to be
753  * checked for read access on pg_rewrite.
754  */
755  if (plan_getviewrule == NULL)
756  {
757  Oid argtypes[2];
758  SPIPlanPtr plan;
759 
760  argtypes[0] = OIDOID;
761  argtypes[1] = NAMEOID;
762  plan = SPI_prepare(query_getviewrule, 2, argtypes);
763  if (plan == NULL)
764  elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule);
765  SPI_keepplan(plan);
766  plan_getviewrule = plan;
767  }
768 
769  /*
770  * Get the pg_rewrite tuple for the view's SELECT rule
771  */
772  args[0] = ObjectIdGetDatum(viewoid);
774  nulls[0] = ' ';
775  nulls[1] = ' ';
776  spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 0);
777  if (spirc != SPI_OK_SELECT)
778  elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
779  if (SPI_processed != 1)
780  {
781  /*
782  * There is no tuple data available here, just keep the output buffer
783  * empty.
784  */
785  }
786  else
787  {
788  /*
789  * Get the rule's definition and put it into executor's memory
790  */
791  ruletup = SPI_tuptable->vals[0];
792  rulettc = SPI_tuptable->tupdesc;
793  make_viewdef(&buf, ruletup, rulettc, prettyFlags, wrapColumn);
794  }
795 
796  /*
797  * Disconnect from SPI manager
798  */
799  if (SPI_finish() != SPI_OK_FINISH)
800  elog(ERROR, "SPI_finish failed");
801 
802  if (buf.len == 0)
803  return NULL;
804 
805  return buf.data;
806 }
807 
808 /* ----------
809  * pg_get_triggerdef - Get the definition of a trigger
810  * ----------
811  */
812 Datum
814 {
815  Oid trigid = PG_GETARG_OID(0);
816  char *res;
817 
818  res = pg_get_triggerdef_worker(trigid, false);
819 
820  if (res == NULL)
821  PG_RETURN_NULL();
822 
824 }
825 
826 Datum
828 {
829  Oid trigid = PG_GETARG_OID(0);
830  bool pretty = PG_GETARG_BOOL(1);
831  char *res;
832 
833  res = pg_get_triggerdef_worker(trigid, pretty);
834 
835  if (res == NULL)
836  PG_RETURN_NULL();
837 
839 }
840 
841 static char *
842 pg_get_triggerdef_worker(Oid trigid, bool pretty)
843 {
844  HeapTuple ht_trig;
845  Form_pg_trigger trigrec;
847  Relation tgrel;
848  ScanKeyData skey[1];
849  SysScanDesc tgscan;
850  int findx = 0;
851  char *tgname;
852  char *tgoldtable;
853  char *tgnewtable;
854  Datum value;
855  bool isnull;
856 
857  /*
858  * Fetch the pg_trigger tuple by the Oid of the trigger
859  */
860  tgrel = table_open(TriggerRelationId, AccessShareLock);
861 
862  ScanKeyInit(&skey[0],
863  Anum_pg_trigger_oid,
864  BTEqualStrategyNumber, F_OIDEQ,
865  ObjectIdGetDatum(trigid));
866 
867  tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
868  NULL, 1, skey);
869 
870  ht_trig = systable_getnext(tgscan);
871 
872  if (!HeapTupleIsValid(ht_trig))
873  {
874  systable_endscan(tgscan);
876  return NULL;
877  }
878 
879  trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
880 
881  /*
882  * Start the trigger definition. Note that the trigger's name should never
883  * be schema-qualified, but the trigger rel's name may be.
884  */
885  initStringInfo(&buf);
886 
887  tgname = NameStr(trigrec->tgname);
888  appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
889  OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
890  quote_identifier(tgname));
891 
892  if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
893  appendStringInfoString(&buf, "BEFORE");
894  else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
895  appendStringInfoString(&buf, "AFTER");
896  else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
897  appendStringInfoString(&buf, "INSTEAD OF");
898  else
899  elog(ERROR, "unexpected tgtype value: %d", trigrec->tgtype);
900 
901  if (TRIGGER_FOR_INSERT(trigrec->tgtype))
902  {
903  appendStringInfoString(&buf, " INSERT");
904  findx++;
905  }
906  if (TRIGGER_FOR_DELETE(trigrec->tgtype))
907  {
908  if (findx > 0)
909  appendStringInfoString(&buf, " OR DELETE");
910  else
911  appendStringInfoString(&buf, " DELETE");
912  findx++;
913  }
914  if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
915  {
916  if (findx > 0)
917  appendStringInfoString(&buf, " OR UPDATE");
918  else
919  appendStringInfoString(&buf, " UPDATE");
920  findx++;
921  /* tgattr is first var-width field, so OK to access directly */
922  if (trigrec->tgattr.dim1 > 0)
923  {
924  int i;
925 
926  appendStringInfoString(&buf, " OF ");
927  for (i = 0; i < trigrec->tgattr.dim1; i++)
928  {
929  char *attname;
930 
931  if (i > 0)
932  appendStringInfoString(&buf, ", ");
933  attname = get_attname(trigrec->tgrelid,
934  trigrec->tgattr.values[i], false);
936  }
937  }
938  }
939  if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
940  {
941  if (findx > 0)
942  appendStringInfoString(&buf, " OR TRUNCATE");
943  else
944  appendStringInfoString(&buf, " TRUNCATE");
945  findx++;
946  }
947 
948  /*
949  * In non-pretty mode, always schema-qualify the target table name for
950  * safety. In pretty mode, schema-qualify only if not visible.
951  */
952  appendStringInfo(&buf, " ON %s ",
953  pretty ?
954  generate_relation_name(trigrec->tgrelid, NIL) :
955  generate_qualified_relation_name(trigrec->tgrelid));
956 
957  if (OidIsValid(trigrec->tgconstraint))
958  {
959  if (OidIsValid(trigrec->tgconstrrelid))
960  appendStringInfo(&buf, "FROM %s ",
961  generate_relation_name(trigrec->tgconstrrelid, NIL));
962  if (!trigrec->tgdeferrable)
963  appendStringInfoString(&buf, "NOT ");
964  appendStringInfoString(&buf, "DEFERRABLE INITIALLY ");
965  if (trigrec->tginitdeferred)
966  appendStringInfoString(&buf, "DEFERRED ");
967  else
968  appendStringInfoString(&buf, "IMMEDIATE ");
969  }
970 
971  value = fastgetattr(ht_trig, Anum_pg_trigger_tgoldtable,
972  tgrel->rd_att, &isnull);
973  if (!isnull)
974  tgoldtable = NameStr(*DatumGetName(value));
975  else
976  tgoldtable = NULL;
977  value = fastgetattr(ht_trig, Anum_pg_trigger_tgnewtable,
978  tgrel->rd_att, &isnull);
979  if (!isnull)
980  tgnewtable = NameStr(*DatumGetName(value));
981  else
982  tgnewtable = NULL;
983  if (tgoldtable != NULL || tgnewtable != NULL)
984  {
985  appendStringInfoString(&buf, "REFERENCING ");
986  if (tgoldtable != NULL)
987  appendStringInfo(&buf, "OLD TABLE AS %s ",
988  quote_identifier(tgoldtable));
989  if (tgnewtable != NULL)
990  appendStringInfo(&buf, "NEW TABLE AS %s ",
991  quote_identifier(tgnewtable));
992  }
993 
994  if (TRIGGER_FOR_ROW(trigrec->tgtype))
995  appendStringInfoString(&buf, "FOR EACH ROW ");
996  else
997  appendStringInfoString(&buf, "FOR EACH STATEMENT ");
998 
999  /* If the trigger has a WHEN qualification, add that */
1000  value = fastgetattr(ht_trig, Anum_pg_trigger_tgqual,
1001  tgrel->rd_att, &isnull);
1002  if (!isnull)
1003  {
1004  Node *qual;
1005  char relkind;
1006  deparse_context context;
1007  deparse_namespace dpns;
1008  RangeTblEntry *oldrte;
1009  RangeTblEntry *newrte;
1010 
1011  appendStringInfoString(&buf, "WHEN (");
1012 
1013  qual = stringToNode(TextDatumGetCString(value));
1014 
1015  relkind = get_rel_relkind(trigrec->tgrelid);
1016 
1017  /* Build minimal OLD and NEW RTEs for the rel */
1018  oldrte = makeNode(RangeTblEntry);
1019  oldrte->rtekind = RTE_RELATION;
1020  oldrte->relid = trigrec->tgrelid;
1021  oldrte->relkind = relkind;
1022  oldrte->rellockmode = AccessShareLock;
1023  oldrte->alias = makeAlias("old", NIL);
1024  oldrte->eref = oldrte->alias;
1025  oldrte->lateral = false;
1026  oldrte->inh = false;
1027  oldrte->inFromCl = true;
1028 
1029  newrte = makeNode(RangeTblEntry);
1030  newrte->rtekind = RTE_RELATION;
1031  newrte->relid = trigrec->tgrelid;
1032  newrte->relkind = relkind;
1033  newrte->rellockmode = AccessShareLock;
1034  newrte->alias = makeAlias("new", NIL);
1035  newrte->eref = newrte->alias;
1036  newrte->lateral = false;
1037  newrte->inh = false;
1038  newrte->inFromCl = true;
1039 
1040  /* Build two-element rtable */
1041  memset(&dpns, 0, sizeof(dpns));
1042  dpns.rtable = list_make2(oldrte, newrte);
1043  dpns.subplans = NIL;
1044  dpns.ctes = NIL;
1045  dpns.appendrels = NULL;
1046  set_rtable_names(&dpns, NIL, NULL);
1047  set_simple_column_names(&dpns);
1048 
1049  /* Set up context with one-deep namespace stack */
1050  context.buf = &buf;
1051  context.namespaces = list_make1(&dpns);
1052  context.windowClause = NIL;
1053  context.windowTList = NIL;
1054  context.varprefix = true;
1056  context.wrapColumn = WRAP_COLUMN_DEFAULT;
1057  context.indentLevel = PRETTYINDENT_STD;
1058  context.special_exprkind = EXPR_KIND_NONE;
1059  context.appendparents = NULL;
1060 
1061  get_rule_expr(qual, &context, false);
1062 
1063  appendStringInfoString(&buf, ") ");
1064  }
1065 
1066  appendStringInfo(&buf, "EXECUTE FUNCTION %s(",
1067  generate_function_name(trigrec->tgfoid, 0,
1068  NIL, NULL,
1069  false, NULL, EXPR_KIND_NONE));
1070 
1071  if (trigrec->tgnargs > 0)
1072  {
1073  char *p;
1074  int i;
1075 
1076  value = fastgetattr(ht_trig, Anum_pg_trigger_tgargs,
1077  tgrel->rd_att, &isnull);
1078  if (isnull)
1079  elog(ERROR, "tgargs is null for trigger %u", trigid);
1080  p = (char *) VARDATA_ANY(DatumGetByteaPP(value));
1081  for (i = 0; i < trigrec->tgnargs; i++)
1082  {
1083  if (i > 0)
1084  appendStringInfoString(&buf, ", ");
1085  simple_quote_literal(&buf, p);
1086  /* advance p to next string embedded in tgargs */
1087  while (*p)
1088  p++;
1089  p++;
1090  }
1091  }
1092 
1093  /* We deliberately do not put semi-colon at end */
1094  appendStringInfoChar(&buf, ')');
1095 
1096  /* Clean up */
1097  systable_endscan(tgscan);
1098 
1099  table_close(tgrel, AccessShareLock);
1100 
1101  return buf.data;
1102 }
1103 
1104 /* ----------
1105  * pg_get_indexdef - Get the definition of an index
1106  *
1107  * In the extended version, there is a colno argument as well as pretty bool.
1108  * if colno == 0, we want a complete index definition.
1109  * if colno > 0, we only want the Nth index key's variable or expression.
1110  *
1111  * Note that the SQL-function versions of this omit any info about the
1112  * index tablespace; this is intentional because pg_dump wants it that way.
1113  * However pg_get_indexdef_string() includes the index tablespace.
1114  * ----------
1115  */
1116 Datum
1118 {
1119  Oid indexrelid = PG_GETARG_OID(0);
1120  int prettyFlags;
1121  char *res;
1122 
1123  prettyFlags = PRETTYFLAG_INDENT;
1124 
1125  res = pg_get_indexdef_worker(indexrelid, 0, NULL,
1126  false, false,
1127  false, false,
1128  prettyFlags, true);
1129 
1130  if (res == NULL)
1131  PG_RETURN_NULL();
1132 
1134 }
1135 
1136 Datum
1138 {
1139  Oid indexrelid = PG_GETARG_OID(0);
1140  int32 colno = PG_GETARG_INT32(1);
1141  bool pretty = PG_GETARG_BOOL(2);
1142  int prettyFlags;
1143  char *res;
1144 
1146 
1147  res = pg_get_indexdef_worker(indexrelid, colno, NULL,
1148  colno != 0, false,
1149  false, false,
1150  prettyFlags, true);
1151 
1152  if (res == NULL)
1153  PG_RETURN_NULL();
1154 
1156 }
1157 
1158 /*
1159  * Internal version for use by ALTER TABLE.
1160  * Includes a tablespace clause in the result.
1161  * Returns a palloc'd C string; no pretty-printing.
1162  */
1163 char *
1165 {
1166  return pg_get_indexdef_worker(indexrelid, 0, NULL,
1167  false, false,
1168  true, true,
1169  0, false);
1170 }
1171 
1172 /* Internal version that just reports the key-column definitions */
1173 char *
1174 pg_get_indexdef_columns(Oid indexrelid, bool pretty)
1175 {
1176  int prettyFlags;
1177 
1179 
1180  return pg_get_indexdef_worker(indexrelid, 0, NULL,
1181  true, true,
1182  false, false,
1183  prettyFlags, false);
1184 }
1185 
1186 /*
1187  * Internal workhorse to decompile an index definition.
1188  *
1189  * This is now used for exclusion constraints as well: if excludeOps is not
1190  * NULL then it points to an array of exclusion operator OIDs.
1191  */
1192 static char *
1193 pg_get_indexdef_worker(Oid indexrelid, int colno,
1194  const Oid *excludeOps,
1195  bool attrsOnly, bool keysOnly,
1196  bool showTblSpc, bool inherits,
1197  int prettyFlags, bool missing_ok)
1198 {
1199  /* might want a separate isConstraint parameter later */
1200  bool isConstraint = (excludeOps != NULL);
1201  HeapTuple ht_idx;
1202  HeapTuple ht_idxrel;
1203  HeapTuple ht_am;
1204  Form_pg_index idxrec;
1205  Form_pg_class idxrelrec;
1206  Form_pg_am amrec;
1207  IndexAmRoutine *amroutine;
1208  List *indexprs;
1209  ListCell *indexpr_item;
1210  List *context;
1211  Oid indrelid;
1212  int keyno;
1213  Datum indcollDatum;
1214  Datum indclassDatum;
1215  Datum indoptionDatum;
1216  bool isnull;
1217  oidvector *indcollation;
1218  oidvector *indclass;
1219  int2vector *indoption;
1221  char *str;
1222  char *sep;
1223 
1224  /*
1225  * Fetch the pg_index tuple by the Oid of the index
1226  */
1227  ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
1228  if (!HeapTupleIsValid(ht_idx))
1229  {
1230  if (missing_ok)
1231  return NULL;
1232  elog(ERROR, "cache lookup failed for index %u", indexrelid);
1233  }
1234  idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
1235 
1236  indrelid = idxrec->indrelid;
1237  Assert(indexrelid == idxrec->indexrelid);
1238 
1239  /* Must get indcollation, indclass, and indoption the hard way */
1240  indcollDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
1241  Anum_pg_index_indcollation, &isnull);
1242  Assert(!isnull);
1243  indcollation = (oidvector *) DatumGetPointer(indcollDatum);
1244 
1245  indclassDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
1246  Anum_pg_index_indclass, &isnull);
1247  Assert(!isnull);
1248  indclass = (oidvector *) DatumGetPointer(indclassDatum);
1249 
1250  indoptionDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
1251  Anum_pg_index_indoption, &isnull);
1252  Assert(!isnull);
1253  indoption = (int2vector *) DatumGetPointer(indoptionDatum);
1254 
1255  /*
1256  * Fetch the pg_class tuple of the index relation
1257  */
1258  ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexrelid));
1259  if (!HeapTupleIsValid(ht_idxrel))
1260  elog(ERROR, "cache lookup failed for relation %u", indexrelid);
1261  idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
1262 
1263  /*
1264  * Fetch the pg_am tuple of the index' access method
1265  */
1266  ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
1267  if (!HeapTupleIsValid(ht_am))
1268  elog(ERROR, "cache lookup failed for access method %u",
1269  idxrelrec->relam);
1270  amrec = (Form_pg_am) GETSTRUCT(ht_am);
1271 
1272  /* Fetch the index AM's API struct */
1273  amroutine = GetIndexAmRoutine(amrec->amhandler);
1274 
1275  /*
1276  * Get the index expressions, if any. (NOTE: we do not use the relcache
1277  * versions of the expressions and predicate, because we want to display
1278  * non-const-folded expressions.)
1279  */
1280  if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs, NULL))
1281  {
1282  Datum exprsDatum;
1283  bool isnull;
1284  char *exprsString;
1285 
1286  exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
1287  Anum_pg_index_indexprs, &isnull);
1288  Assert(!isnull);
1289  exprsString = TextDatumGetCString(exprsDatum);
1290  indexprs = (List *) stringToNode(exprsString);
1291  pfree(exprsString);
1292  }
1293  else
1294  indexprs = NIL;
1295 
1296  indexpr_item = list_head(indexprs);
1297 
1298  context = deparse_context_for(get_relation_name(indrelid), indrelid);
1299 
1300  /*
1301  * Start the index definition. Note that the index's name should never be
1302  * schema-qualified, but the indexed rel's name may be.
1303  */
1304  initStringInfo(&buf);
1305 
1306  if (!attrsOnly)
1307  {
1308  if (!isConstraint)
1309  appendStringInfo(&buf, "CREATE %sINDEX %s ON %s%s USING %s (",
1310  idxrec->indisunique ? "UNIQUE " : "",
1311  quote_identifier(NameStr(idxrelrec->relname)),
1312  idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
1313  && !inherits ? "ONLY " : "",
1314  (prettyFlags & PRETTYFLAG_SCHEMA) ?
1315  generate_relation_name(indrelid, NIL) :
1317  quote_identifier(NameStr(amrec->amname)));
1318  else /* currently, must be EXCLUDE constraint */
1319  appendStringInfo(&buf, "EXCLUDE USING %s (",
1320  quote_identifier(NameStr(amrec->amname)));
1321  }
1322 
1323  /*
1324  * Report the indexed attributes
1325  */
1326  sep = "";
1327  for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1328  {
1329  AttrNumber attnum = idxrec->indkey.values[keyno];
1330  Oid keycoltype;
1331  Oid keycolcollation;
1332 
1333  /*
1334  * Ignore non-key attributes if told to.
1335  */
1336  if (keysOnly && keyno >= idxrec->indnkeyatts)
1337  break;
1338 
1339  /* Otherwise, print INCLUDE to divide key and non-key attrs. */
1340  if (!colno && keyno == idxrec->indnkeyatts)
1341  {
1342  appendStringInfoString(&buf, ") INCLUDE (");
1343  sep = "";
1344  }
1345 
1346  if (!colno)
1347  appendStringInfoString(&buf, sep);
1348  sep = ", ";
1349 
1350  if (attnum != 0)
1351  {
1352  /* Simple index column */
1353  char *attname;
1354  int32 keycoltypmod;
1355 
1356  attname = get_attname(indrelid, attnum, false);
1357  if (!colno || colno == keyno + 1)
1358  appendStringInfoString(&buf, quote_identifier(attname));
1359  get_atttypetypmodcoll(indrelid, attnum,
1360  &keycoltype, &keycoltypmod,
1361  &keycolcollation);
1362  }
1363  else
1364  {
1365  /* expressional index */
1366  Node *indexkey;
1367 
1368  if (indexpr_item == NULL)
1369  elog(ERROR, "too few entries in indexprs list");
1370  indexkey = (Node *) lfirst(indexpr_item);
1371  indexpr_item = lnext(indexprs, indexpr_item);
1372  /* Deparse */
1373  str = deparse_expression_pretty(indexkey, context, false, false,
1374  prettyFlags, 0);
1375  if (!colno || colno == keyno + 1)
1376  {
1377  /* Need parens if it's not a bare function call */
1378  if (looks_like_function(indexkey))
1379  appendStringInfoString(&buf, str);
1380  else
1381  appendStringInfo(&buf, "(%s)", str);
1382  }
1383  keycoltype = exprType(indexkey);
1384  keycolcollation = exprCollation(indexkey);
1385  }
1386 
1387  /* Print additional decoration for (selected) key columns */
1388  if (!attrsOnly && keyno < idxrec->indnkeyatts &&
1389  (!colno || colno == keyno + 1))
1390  {
1391  int16 opt = indoption->values[keyno];
1392  Oid indcoll = indcollation->values[keyno];
1393  Datum attoptions = get_attoptions(indexrelid, keyno + 1);
1394  bool has_options = attoptions != (Datum) 0;
1395 
1396  /* Add collation, if not default for column */
1397  if (OidIsValid(indcoll) && indcoll != keycolcollation)
1398  appendStringInfo(&buf, " COLLATE %s",
1399  generate_collation_name((indcoll)));
1400 
1401  /* Add the operator class name, if not default */
1402  get_opclass_name(indclass->values[keyno],
1403  has_options ? InvalidOid : keycoltype, &buf);
1404 
1405  if (has_options)
1406  {
1407  appendStringInfoString(&buf, " (");
1408  get_reloptions(&buf, attoptions);
1409  appendStringInfoChar(&buf, ')');
1410  }
1411 
1412  /* Add options if relevant */
1413  if (amroutine->amcanorder)
1414  {
1415  /* if it supports sort ordering, report DESC and NULLS opts */
1416  if (opt & INDOPTION_DESC)
1417  {
1418  appendStringInfoString(&buf, " DESC");
1419  /* NULLS FIRST is the default in this case */
1420  if (!(opt & INDOPTION_NULLS_FIRST))
1421  appendStringInfoString(&buf, " NULLS LAST");
1422  }
1423  else
1424  {
1425  if (opt & INDOPTION_NULLS_FIRST)
1426  appendStringInfoString(&buf, " NULLS FIRST");
1427  }
1428  }
1429 
1430  /* Add the exclusion operator if relevant */
1431  if (excludeOps != NULL)
1432  appendStringInfo(&buf, " WITH %s",
1433  generate_operator_name(excludeOps[keyno],
1434  keycoltype,
1435  keycoltype));
1436  }
1437  }
1438 
1439  if (!attrsOnly)
1440  {
1441  appendStringInfoChar(&buf, ')');
1442 
1443  /*
1444  * If it has options, append "WITH (options)"
1445  */
1446  str = flatten_reloptions(indexrelid);
1447  if (str)
1448  {
1449  appendStringInfo(&buf, " WITH (%s)", str);
1450  pfree(str);
1451  }
1452 
1453  /*
1454  * Print tablespace, but only if requested
1455  */
1456  if (showTblSpc)
1457  {
1458  Oid tblspc;
1459 
1460  tblspc = get_rel_tablespace(indexrelid);
1461  if (OidIsValid(tblspc))
1462  {
1463  if (isConstraint)
1464  appendStringInfoString(&buf, " USING INDEX");
1465  appendStringInfo(&buf, " TABLESPACE %s",
1467  }
1468  }
1469 
1470  /*
1471  * If it's a partial index, decompile and append the predicate
1472  */
1473  if (!heap_attisnull(ht_idx, Anum_pg_index_indpred, NULL))
1474  {
1475  Node *node;
1476  Datum predDatum;
1477  bool isnull;
1478  char *predString;
1479 
1480  /* Convert text string to node tree */
1481  predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
1482  Anum_pg_index_indpred, &isnull);
1483  Assert(!isnull);
1484  predString = TextDatumGetCString(predDatum);
1485  node = (Node *) stringToNode(predString);
1486  pfree(predString);
1487 
1488  /* Deparse */
1489  str = deparse_expression_pretty(node, context, false, false,
1490  prettyFlags, 0);
1491  if (isConstraint)
1492  appendStringInfo(&buf, " WHERE (%s)", str);
1493  else
1494  appendStringInfo(&buf, " WHERE %s", str);
1495  }
1496  }
1497 
1498  /* Clean up */
1499  ReleaseSysCache(ht_idx);
1500  ReleaseSysCache(ht_idxrel);
1501  ReleaseSysCache(ht_am);
1502 
1503  return buf.data;
1504 }
1505 
1506 /*
1507  * pg_get_statisticsobjdef
1508  * Get the definition of an extended statistics object
1509  */
1510 Datum
1512 {
1513  Oid statextid = PG_GETARG_OID(0);
1514  char *res;
1515 
1516  res = pg_get_statisticsobj_worker(statextid, false, true);
1517 
1518  if (res == NULL)
1519  PG_RETURN_NULL();
1520 
1522 }
1523 
1524 /*
1525  * Internal version for use by ALTER TABLE.
1526  * Includes a tablespace clause in the result.
1527  * Returns a palloc'd C string; no pretty-printing.
1528  */
1529 char *
1531 {
1532  return pg_get_statisticsobj_worker(statextid, false, false);
1533 }
1534 
1535 /*
1536  * pg_get_statisticsobjdef_columns
1537  * Get columns and expressions for an extended statistics object
1538  */
1539 Datum
1541 {
1542  Oid statextid = PG_GETARG_OID(0);
1543  char *res;
1544 
1545  res = pg_get_statisticsobj_worker(statextid, true, true);
1546 
1547  if (res == NULL)
1548  PG_RETURN_NULL();
1549 
1551 }
1552 
1553 /*
1554  * Internal workhorse to decompile an extended statistics object.
1555  */
1556 static char *
1557 pg_get_statisticsobj_worker(Oid statextid, bool columns_only, bool missing_ok)
1558 {
1559  Form_pg_statistic_ext statextrec;
1560  HeapTuple statexttup;
1562  int colno;
1563  char *nsp;
1564  ArrayType *arr;
1565  char *enabled;
1566  Datum datum;
1567  bool isnull;
1568  bool ndistinct_enabled;
1569  bool dependencies_enabled;
1570  bool mcv_enabled;
1571  int i;
1572  List *context;
1573  ListCell *lc;
1574  List *exprs = NIL;
1575  bool has_exprs;
1576  int ncolumns;
1577 
1578  statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
1579 
1580  if (!HeapTupleIsValid(statexttup))
1581  {
1582  if (missing_ok)
1583  return NULL;
1584  elog(ERROR, "cache lookup failed for statistics object %u", statextid);
1585  }
1586 
1587  /* has the statistics expressions? */
1588  has_exprs = !heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1589 
1590  statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
1591 
1592  /*
1593  * Get the statistics expressions, if any. (NOTE: we do not use the
1594  * relcache versions of the expressions, because we want to display
1595  * non-const-folded expressions.)
1596  */
1597  if (has_exprs)
1598  {
1599  Datum exprsDatum;
1600  bool isnull;
1601  char *exprsString;
1602 
1603  exprsDatum = SysCacheGetAttr(STATEXTOID, statexttup,
1604  Anum_pg_statistic_ext_stxexprs, &isnull);
1605  Assert(!isnull);
1606  exprsString = TextDatumGetCString(exprsDatum);
1607  exprs = (List *) stringToNode(exprsString);
1608  pfree(exprsString);
1609  }
1610  else
1611  exprs = NIL;
1612 
1613  /* count the number of columns (attributes and expressions) */
1614  ncolumns = statextrec->stxkeys.dim1 + list_length(exprs);
1615 
1616  initStringInfo(&buf);
1617 
1618  if (!columns_only)
1619  {
1620  nsp = get_namespace_name(statextrec->stxnamespace);
1621  appendStringInfo(&buf, "CREATE STATISTICS %s",
1623  NameStr(statextrec->stxname)));
1624 
1625  /*
1626  * Decode the stxkind column so that we know which stats types to
1627  * print.
1628  */
1629  datum = SysCacheGetAttr(STATEXTOID, statexttup,
1630  Anum_pg_statistic_ext_stxkind, &isnull);
1631  Assert(!isnull);
1632  arr = DatumGetArrayTypeP(datum);
1633  if (ARR_NDIM(arr) != 1 ||
1634  ARR_HASNULL(arr) ||
1635  ARR_ELEMTYPE(arr) != CHAROID)
1636  elog(ERROR, "stxkind is not a 1-D char array");
1637  enabled = (char *) ARR_DATA_PTR(arr);
1638 
1639  ndistinct_enabled = false;
1640  dependencies_enabled = false;
1641  mcv_enabled = false;
1642 
1643  for (i = 0; i < ARR_DIMS(arr)[0]; i++)
1644  {
1645  if (enabled[i] == STATS_EXT_NDISTINCT)
1646  ndistinct_enabled = true;
1647  else if (enabled[i] == STATS_EXT_DEPENDENCIES)
1648  dependencies_enabled = true;
1649  else if (enabled[i] == STATS_EXT_MCV)
1650  mcv_enabled = true;
1651 
1652  /* ignore STATS_EXT_EXPRESSIONS (it's built automatically) */
1653  }
1654 
1655  /*
1656  * If any option is disabled, then we'll need to append the types
1657  * clause to show which options are enabled. We omit the types clause
1658  * on purpose when all options are enabled, so a pg_dump/pg_restore
1659  * will create all statistics types on a newer postgres version, if
1660  * the statistics had all options enabled on the original version.
1661  *
1662  * But if the statistics is defined on just a single column, it has to
1663  * be an expression statistics. In that case we don't need to specify
1664  * kinds.
1665  */
1666  if ((!ndistinct_enabled || !dependencies_enabled || !mcv_enabled) &&
1667  (ncolumns > 1))
1668  {
1669  bool gotone = false;
1670 
1671  appendStringInfoString(&buf, " (");
1672 
1673  if (ndistinct_enabled)
1674  {
1675  appendStringInfoString(&buf, "ndistinct");
1676  gotone = true;
1677  }
1678 
1679  if (dependencies_enabled)
1680  {
1681  appendStringInfo(&buf, "%sdependencies", gotone ? ", " : "");
1682  gotone = true;
1683  }
1684 
1685  if (mcv_enabled)
1686  appendStringInfo(&buf, "%smcv", gotone ? ", " : "");
1687 
1688  appendStringInfoChar(&buf, ')');
1689  }
1690 
1691  appendStringInfoString(&buf, " ON ");
1692  }
1693 
1694  /* decode simple column references */
1695  for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
1696  {
1697  AttrNumber attnum = statextrec->stxkeys.values[colno];
1698  char *attname;
1699 
1700  if (colno > 0)
1701  appendStringInfoString(&buf, ", ");
1702 
1703  attname = get_attname(statextrec->stxrelid, attnum, false);
1704 
1705  appendStringInfoString(&buf, quote_identifier(attname));
1706  }
1707 
1708  context = deparse_context_for(get_relation_name(statextrec->stxrelid),
1709  statextrec->stxrelid);
1710 
1711  foreach(lc, exprs)
1712  {
1713  Node *expr = (Node *) lfirst(lc);
1714  char *str;
1715  int prettyFlags = PRETTYFLAG_INDENT;
1716 
1717  str = deparse_expression_pretty(expr, context, false, false,
1718  prettyFlags, 0);
1719 
1720  if (colno > 0)
1721  appendStringInfoString(&buf, ", ");
1722 
1723  /* Need parens if it's not a bare function call */
1724  if (looks_like_function(expr))
1725  appendStringInfoString(&buf, str);
1726  else
1727  appendStringInfo(&buf, "(%s)", str);
1728 
1729  colno++;
1730  }
1731 
1732  if (!columns_only)
1733  appendStringInfo(&buf, " FROM %s",
1734  generate_relation_name(statextrec->stxrelid, NIL));
1735 
1736  ReleaseSysCache(statexttup);
1737 
1738  return buf.data;
1739 }
1740 
1741 /*
1742  * Generate text array of expressions for statistics object.
1743  */
1744 Datum
1746 {
1747  Oid statextid = PG_GETARG_OID(0);
1748  Form_pg_statistic_ext statextrec;
1749  HeapTuple statexttup;
1750  Datum datum;
1751  bool isnull;
1752  List *context;
1753  ListCell *lc;
1754  List *exprs = NIL;
1755  bool has_exprs;
1756  char *tmp;
1757  ArrayBuildState *astate = NULL;
1758 
1759  statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
1760 
1761  if (!HeapTupleIsValid(statexttup))
1762  elog(ERROR, "cache lookup failed for statistics object %u", statextid);
1763 
1764  /* has the statistics expressions? */
1765  has_exprs = !heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1766 
1767  /* no expressions? we're done */
1768  if (!has_exprs)
1769  {
1770  ReleaseSysCache(statexttup);
1771  PG_RETURN_NULL();
1772  }
1773 
1774  statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
1775 
1776  /*
1777  * Get the statistics expressions, and deparse them into text values.
1778  */
1779  datum = SysCacheGetAttr(STATEXTOID, statexttup,
1780  Anum_pg_statistic_ext_stxexprs, &isnull);
1781 
1782  Assert(!isnull);
1783  tmp = TextDatumGetCString(datum);
1784  exprs = (List *) stringToNode(tmp);
1785  pfree(tmp);
1786 
1787  context = deparse_context_for(get_relation_name(statextrec->stxrelid),
1788  statextrec->stxrelid);
1789 
1790  foreach(lc, exprs)
1791  {
1792  Node *expr = (Node *) lfirst(lc);
1793  char *str;
1794  int prettyFlags = PRETTYFLAG_INDENT;
1795 
1796  str = deparse_expression_pretty(expr, context, false, false,
1797  prettyFlags, 0);
1798 
1799  astate = accumArrayResult(astate,
1801  false,
1802  TEXTOID,
1804  }
1805 
1806  ReleaseSysCache(statexttup);
1807 
1809 }
1810 
1811 /*
1812  * pg_get_partkeydef
1813  *
1814  * Returns the partition key specification, ie, the following:
1815  *
1816  * PARTITION BY { RANGE | LIST | HASH } (column opt_collation opt_opclass [, ...])
1817  */
1818 Datum
1820 {
1821  Oid relid = PG_GETARG_OID(0);
1822  char *res;
1823 
1824  res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
1825 
1826  if (res == NULL)
1827  PG_RETURN_NULL();
1828 
1830 }
1831 
1832 /* Internal version that just reports the column definitions */
1833 char *
1834 pg_get_partkeydef_columns(Oid relid, bool pretty)
1835 {
1836  int prettyFlags;
1837 
1839 
1840  return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
1841 }
1842 
1843 /*
1844  * Internal workhorse to decompile a partition key definition.
1845  */
1846 static char *
1847 pg_get_partkeydef_worker(Oid relid, int prettyFlags,
1848  bool attrsOnly, bool missing_ok)
1849 {
1851  HeapTuple tuple;
1852  oidvector *partclass;
1853  oidvector *partcollation;
1854  List *partexprs;
1855  ListCell *partexpr_item;
1856  List *context;
1857  Datum datum;
1858  bool isnull;
1860  int keyno;
1861  char *str;
1862  char *sep;
1863 
1864  tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
1865  if (!HeapTupleIsValid(tuple))
1866  {
1867  if (missing_ok)
1868  return NULL;
1869  elog(ERROR, "cache lookup failed for partition key of %u", relid);
1870  }
1871 
1872  form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
1873 
1874  Assert(form->partrelid == relid);
1875 
1876  /* Must get partclass and partcollation the hard way */
1877  datum = SysCacheGetAttr(PARTRELID, tuple,
1878  Anum_pg_partitioned_table_partclass, &isnull);
1879  Assert(!isnull);
1880  partclass = (oidvector *) DatumGetPointer(datum);
1881 
1882  datum = SysCacheGetAttr(PARTRELID, tuple,
1883  Anum_pg_partitioned_table_partcollation, &isnull);
1884  Assert(!isnull);
1885  partcollation = (oidvector *) DatumGetPointer(datum);
1886 
1887 
1888  /*
1889  * Get the expressions, if any. (NOTE: we do not use the relcache
1890  * versions of the expressions, because we want to display
1891  * non-const-folded expressions.)
1892  */
1893  if (!heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
1894  {
1895  Datum exprsDatum;
1896  bool isnull;
1897  char *exprsString;
1898 
1899  exprsDatum = SysCacheGetAttr(PARTRELID, tuple,
1900  Anum_pg_partitioned_table_partexprs, &isnull);
1901  Assert(!isnull);
1902  exprsString = TextDatumGetCString(exprsDatum);
1903  partexprs = (List *) stringToNode(exprsString);
1904 
1905  if (!IsA(partexprs, List))
1906  elog(ERROR, "unexpected node type found in partexprs: %d",
1907  (int) nodeTag(partexprs));
1908 
1909  pfree(exprsString);
1910  }
1911  else
1912  partexprs = NIL;
1913 
1914  partexpr_item = list_head(partexprs);
1915  context = deparse_context_for(get_relation_name(relid), relid);
1916 
1917  initStringInfo(&buf);
1918 
1919  switch (form->partstrat)
1920  {
1922  if (!attrsOnly)
1923  appendStringInfoString(&buf, "HASH");
1924  break;
1926  if (!attrsOnly)
1927  appendStringInfoString(&buf, "LIST");
1928  break;
1930  if (!attrsOnly)
1931  appendStringInfoString(&buf, "RANGE");
1932  break;
1933  default:
1934  elog(ERROR, "unexpected partition strategy: %d",
1935  (int) form->partstrat);
1936  }
1937 
1938  if (!attrsOnly)
1939  appendStringInfoString(&buf, " (");
1940  sep = "";
1941  for (keyno = 0; keyno < form->partnatts; keyno++)
1942  {
1943  AttrNumber attnum = form->partattrs.values[keyno];
1944  Oid keycoltype;
1945  Oid keycolcollation;
1946  Oid partcoll;
1947 
1948  appendStringInfoString(&buf, sep);
1949  sep = ", ";
1950  if (attnum != 0)
1951  {
1952  /* Simple attribute reference */
1953  char *attname;
1954  int32 keycoltypmod;
1955 
1956  attname = get_attname(relid, attnum, false);
1957  appendStringInfoString(&buf, quote_identifier(attname));
1958  get_atttypetypmodcoll(relid, attnum,
1959  &keycoltype, &keycoltypmod,
1960  &keycolcollation);
1961  }
1962  else
1963  {
1964  /* Expression */
1965  Node *partkey;
1966 
1967  if (partexpr_item == NULL)
1968  elog(ERROR, "too few entries in partexprs list");
1969  partkey = (Node *) lfirst(partexpr_item);
1970  partexpr_item = lnext(partexprs, partexpr_item);
1971 
1972  /* Deparse */
1973  str = deparse_expression_pretty(partkey, context, false, false,
1974  prettyFlags, 0);
1975  /* Need parens if it's not a bare function call */
1976  if (looks_like_function(partkey))
1977  appendStringInfoString(&buf, str);
1978  else
1979  appendStringInfo(&buf, "(%s)", str);
1980 
1981  keycoltype = exprType(partkey);
1982  keycolcollation = exprCollation(partkey);
1983  }
1984 
1985  /* Add collation, if not default for column */
1986  partcoll = partcollation->values[keyno];
1987  if (!attrsOnly && OidIsValid(partcoll) && partcoll != keycolcollation)
1988  appendStringInfo(&buf, " COLLATE %s",
1989  generate_collation_name((partcoll)));
1990 
1991  /* Add the operator class name, if not default */
1992  if (!attrsOnly)
1993  get_opclass_name(partclass->values[keyno], keycoltype, &buf);
1994  }
1995 
1996  if (!attrsOnly)
1997  appendStringInfoChar(&buf, ')');
1998 
1999  /* Clean up */
2000  ReleaseSysCache(tuple);
2001 
2002  return buf.data;
2003 }
2004 
2005 /*
2006  * pg_get_partition_constraintdef
2007  *
2008  * Returns partition constraint expression as a string for the input relation
2009  */
2010 Datum
2012 {
2013  Oid relationId = PG_GETARG_OID(0);
2014  Expr *constr_expr;
2015  int prettyFlags;
2016  List *context;
2017  char *consrc;
2018 
2019  constr_expr = get_partition_qual_relid(relationId);
2020 
2021  /* Quick exit if no partition constraint */
2022  if (constr_expr == NULL)
2023  PG_RETURN_NULL();
2024 
2025  /*
2026  * Deparse and return the constraint expression.
2027  */
2028  prettyFlags = PRETTYFLAG_INDENT;
2029  context = deparse_context_for(get_relation_name(relationId), relationId);
2030  consrc = deparse_expression_pretty((Node *) constr_expr, context, false,
2031  false, prettyFlags, 0);
2032 
2034 }
2035 
2036 /*
2037  * pg_get_partconstrdef_string
2038  *
2039  * Returns the partition constraint as a C-string for the input relation, with
2040  * the given alias. No pretty-printing.
2041  */
2042 char *
2043 pg_get_partconstrdef_string(Oid partitionId, char *aliasname)
2044 {
2045  Expr *constr_expr;
2046  List *context;
2047 
2048  constr_expr = get_partition_qual_relid(partitionId);
2049  context = deparse_context_for(aliasname, partitionId);
2050 
2051  return deparse_expression((Node *) constr_expr, context, true, false);
2052 }
2053 
2054 /*
2055  * pg_get_constraintdef
2056  *
2057  * Returns the definition for the constraint, ie, everything that needs to
2058  * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>".
2059  */
2060 Datum
2062 {
2063  Oid constraintId = PG_GETARG_OID(0);
2064  int prettyFlags;
2065  char *res;
2066 
2067  prettyFlags = PRETTYFLAG_INDENT;
2068 
2069  res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2070 
2071  if (res == NULL)
2072  PG_RETURN_NULL();
2073 
2075 }
2076 
2077 Datum
2079 {
2080  Oid constraintId = PG_GETARG_OID(0);
2081  bool pretty = PG_GETARG_BOOL(1);
2082  int prettyFlags;
2083  char *res;
2084 
2086 
2087  res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2088 
2089  if (res == NULL)
2090  PG_RETURN_NULL();
2091 
2093 }
2094 
2095 /*
2096  * Internal version that returns a full ALTER TABLE ... ADD CONSTRAINT command
2097  */
2098 char *
2100 {
2101  return pg_get_constraintdef_worker(constraintId, true, 0, false);
2102 }
2103 
2104 /*
2105  * As of 9.4, we now use an MVCC snapshot for this.
2106  */
2107 static char *
2108 pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
2109  int prettyFlags, bool missing_ok)
2110 {
2111  HeapTuple tup;
2112  Form_pg_constraint conForm;
2114  SysScanDesc scandesc;
2115  ScanKeyData scankey[1];
2117  Relation relation = table_open(ConstraintRelationId, AccessShareLock);
2118 
2119  ScanKeyInit(&scankey[0],
2120  Anum_pg_constraint_oid,
2121  BTEqualStrategyNumber, F_OIDEQ,
2122  ObjectIdGetDatum(constraintId));
2123 
2124  scandesc = systable_beginscan(relation,
2126  true,
2127  snapshot,
2128  1,
2129  scankey);
2130 
2131  /*
2132  * We later use the tuple with SysCacheGetAttr() as if we had obtained it
2133  * via SearchSysCache, which works fine.
2134  */
2135  tup = systable_getnext(scandesc);
2136 
2137  UnregisterSnapshot(snapshot);
2138 
2139  if (!HeapTupleIsValid(tup))
2140  {
2141  if (missing_ok)
2142  {
2143  systable_endscan(scandesc);
2144  table_close(relation, AccessShareLock);
2145  return NULL;
2146  }
2147  elog(ERROR, "could not find tuple for constraint %u", constraintId);
2148  }
2149 
2150  conForm = (Form_pg_constraint) GETSTRUCT(tup);
2151 
2152  initStringInfo(&buf);
2153 
2154  if (fullCommand)
2155  {
2156  if (OidIsValid(conForm->conrelid))
2157  {
2158  /*
2159  * Currently, callers want ALTER TABLE (without ONLY) for CHECK
2160  * constraints, and other types of constraints don't inherit
2161  * anyway so it doesn't matter whether we say ONLY or not. Someday
2162  * we might need to let callers specify whether to put ONLY in the
2163  * command.
2164  */
2165  appendStringInfo(&buf, "ALTER TABLE %s ADD CONSTRAINT %s ",
2166  generate_qualified_relation_name(conForm->conrelid),
2167  quote_identifier(NameStr(conForm->conname)));
2168  }
2169  else
2170  {
2171  /* Must be a domain constraint */
2172  Assert(OidIsValid(conForm->contypid));
2173  appendStringInfo(&buf, "ALTER DOMAIN %s ADD CONSTRAINT %s ",
2174  generate_qualified_type_name(conForm->contypid),
2175  quote_identifier(NameStr(conForm->conname)));
2176  }
2177  }
2178 
2179  switch (conForm->contype)
2180  {
2181  case CONSTRAINT_FOREIGN:
2182  {
2183  Datum val;
2184  bool isnull;
2185  const char *string;
2186 
2187  /* Start off the constraint definition */
2188  appendStringInfoString(&buf, "FOREIGN KEY (");
2189 
2190  /* Fetch and build referencing-column list */
2191  val = SysCacheGetAttr(CONSTROID, tup,
2192  Anum_pg_constraint_conkey, &isnull);
2193  if (isnull)
2194  elog(ERROR, "null conkey for constraint %u",
2195  constraintId);
2196 
2197  decompile_column_index_array(val, conForm->conrelid, &buf);
2198 
2199  /* add foreign relation name */
2200  appendStringInfo(&buf, ") REFERENCES %s(",
2201  generate_relation_name(conForm->confrelid,
2202  NIL));
2203 
2204  /* Fetch and build referenced-column list */
2205  val = SysCacheGetAttr(CONSTROID, tup,
2206  Anum_pg_constraint_confkey, &isnull);
2207  if (isnull)
2208  elog(ERROR, "null confkey for constraint %u",
2209  constraintId);
2210 
2211  decompile_column_index_array(val, conForm->confrelid, &buf);
2212 
2213  appendStringInfoChar(&buf, ')');
2214 
2215  /* Add match type */
2216  switch (conForm->confmatchtype)
2217  {
2218  case FKCONSTR_MATCH_FULL:
2219  string = " MATCH FULL";
2220  break;
2222  string = " MATCH PARTIAL";
2223  break;
2224  case FKCONSTR_MATCH_SIMPLE:
2225  string = "";
2226  break;
2227  default:
2228  elog(ERROR, "unrecognized confmatchtype: %d",
2229  conForm->confmatchtype);
2230  string = ""; /* keep compiler quiet */
2231  break;
2232  }
2233  appendStringInfoString(&buf, string);
2234 
2235  /* Add ON UPDATE and ON DELETE clauses, if needed */
2236  switch (conForm->confupdtype)
2237  {
2239  string = NULL; /* suppress default */
2240  break;
2242  string = "RESTRICT";
2243  break;
2245  string = "CASCADE";
2246  break;
2248  string = "SET NULL";
2249  break;
2251  string = "SET DEFAULT";
2252  break;
2253  default:
2254  elog(ERROR, "unrecognized confupdtype: %d",
2255  conForm->confupdtype);
2256  string = NULL; /* keep compiler quiet */
2257  break;
2258  }
2259  if (string)
2260  appendStringInfo(&buf, " ON UPDATE %s", string);
2261 
2262  switch (conForm->confdeltype)
2263  {
2265  string = NULL; /* suppress default */
2266  break;
2268  string = "RESTRICT";
2269  break;
2271  string = "CASCADE";
2272  break;
2274  string = "SET NULL";
2275  break;
2277  string = "SET DEFAULT";
2278  break;
2279  default:
2280  elog(ERROR, "unrecognized confdeltype: %d",
2281  conForm->confdeltype);
2282  string = NULL; /* keep compiler quiet */
2283  break;
2284  }
2285  if (string)
2286  appendStringInfo(&buf, " ON DELETE %s", string);
2287 
2288  break;
2289  }
2290  case CONSTRAINT_PRIMARY:
2291  case CONSTRAINT_UNIQUE:
2292  {
2293  Datum val;
2294  bool isnull;
2295  Oid indexId;
2296  int keyatts;
2297  HeapTuple indtup;
2298 
2299  /* Start off the constraint definition */
2300  if (conForm->contype == CONSTRAINT_PRIMARY)
2301  appendStringInfoString(&buf, "PRIMARY KEY (");
2302  else
2303  appendStringInfoString(&buf, "UNIQUE (");
2304 
2305  /* Fetch and build target column list */
2306  val = SysCacheGetAttr(CONSTROID, tup,
2307  Anum_pg_constraint_conkey, &isnull);
2308  if (isnull)
2309  elog(ERROR, "null conkey for constraint %u",
2310  constraintId);
2311 
2312  keyatts = decompile_column_index_array(val, conForm->conrelid, &buf);
2313 
2314  appendStringInfoChar(&buf, ')');
2315 
2316  indexId = conForm->conindid;
2317 
2318  /* Build including column list (from pg_index.indkeys) */
2319  indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
2320  if (!HeapTupleIsValid(indtup))
2321  elog(ERROR, "cache lookup failed for index %u", indexId);
2322  val = SysCacheGetAttr(INDEXRELID, indtup,
2323  Anum_pg_index_indnatts, &isnull);
2324  if (isnull)
2325  elog(ERROR, "null indnatts for index %u", indexId);
2326  if (DatumGetInt32(val) > keyatts)
2327  {
2328  Datum cols;
2329  Datum *keys;
2330  int nKeys;
2331  int j;
2332 
2333  appendStringInfoString(&buf, " INCLUDE (");
2334 
2335  cols = SysCacheGetAttr(INDEXRELID, indtup,
2336  Anum_pg_index_indkey, &isnull);
2337  if (isnull)
2338  elog(ERROR, "null indkey for index %u", indexId);
2339 
2341  INT2OID, 2, true, TYPALIGN_SHORT,
2342  &keys, NULL, &nKeys);
2343 
2344  for (j = keyatts; j < nKeys; j++)
2345  {
2346  char *colName;
2347 
2348  colName = get_attname(conForm->conrelid,
2349  DatumGetInt16(keys[j]), false);
2350  if (j > keyatts)
2351  appendStringInfoString(&buf, ", ");
2352  appendStringInfoString(&buf, quote_identifier(colName));
2353  }
2354 
2355  appendStringInfoChar(&buf, ')');
2356  }
2357  ReleaseSysCache(indtup);
2358 
2359  /* XXX why do we only print these bits if fullCommand? */
2360  if (fullCommand && OidIsValid(indexId))
2361  {
2362  char *options = flatten_reloptions(indexId);
2363  Oid tblspc;
2364 
2365  if (options)
2366  {
2367  appendStringInfo(&buf, " WITH (%s)", options);
2368  pfree(options);
2369  }
2370 
2371  /*
2372  * Print the tablespace, unless it's the database default.
2373  * This is to help ALTER TABLE usage of this facility,
2374  * which needs this behavior to recreate exact catalog
2375  * state.
2376  */
2377  tblspc = get_rel_tablespace(indexId);
2378  if (OidIsValid(tblspc))
2379  appendStringInfo(&buf, " USING INDEX TABLESPACE %s",
2381  }
2382 
2383  break;
2384  }
2385  case CONSTRAINT_CHECK:
2386  {
2387  Datum val;
2388  bool isnull;
2389  char *conbin;
2390  char *consrc;
2391  Node *expr;
2392  List *context;
2393 
2394  /* Fetch constraint expression in parsetree form */
2395  val = SysCacheGetAttr(CONSTROID, tup,
2396  Anum_pg_constraint_conbin, &isnull);
2397  if (isnull)
2398  elog(ERROR, "null conbin for constraint %u",
2399  constraintId);
2400 
2401  conbin = TextDatumGetCString(val);
2402  expr = stringToNode(conbin);
2403 
2404  /* Set up deparsing context for Var nodes in constraint */
2405  if (conForm->conrelid != InvalidOid)
2406  {
2407  /* relation constraint */
2408  context = deparse_context_for(get_relation_name(conForm->conrelid),
2409  conForm->conrelid);
2410  }
2411  else
2412  {
2413  /* domain constraint --- can't have Vars */
2414  context = NIL;
2415  }
2416 
2417  consrc = deparse_expression_pretty(expr, context, false, false,
2418  prettyFlags, 0);
2419 
2420  /*
2421  * Now emit the constraint definition, adding NO INHERIT if
2422  * necessary.
2423  *
2424  * There are cases where the constraint expression will be
2425  * fully parenthesized and we don't need the outer parens ...
2426  * but there are other cases where we do need 'em. Be
2427  * conservative for now.
2428  *
2429  * Note that simply checking for leading '(' and trailing ')'
2430  * would NOT be good enough, consider "(x > 0) AND (y > 0)".
2431  */
2432  appendStringInfo(&buf, "CHECK (%s)%s",
2433  consrc,
2434  conForm->connoinherit ? " NO INHERIT" : "");
2435  break;
2436  }
2437  case CONSTRAINT_TRIGGER:
2438 
2439  /*
2440  * There isn't an ALTER TABLE syntax for creating a user-defined
2441  * constraint trigger, but it seems better to print something than
2442  * throw an error; if we throw error then this function couldn't
2443  * safely be applied to all rows of pg_constraint.
2444  */
2445  appendStringInfoString(&buf, "TRIGGER");
2446  break;
2447  case CONSTRAINT_EXCLUSION:
2448  {
2449  Oid indexOid = conForm->conindid;
2450  Datum val;
2451  bool isnull;
2452  Datum *elems;
2453  int nElems;
2454  int i;
2455  Oid *operators;
2456 
2457  /* Extract operator OIDs from the pg_constraint tuple */
2458  val = SysCacheGetAttr(CONSTROID, tup,
2459  Anum_pg_constraint_conexclop,
2460  &isnull);
2461  if (isnull)
2462  elog(ERROR, "null conexclop for constraint %u",
2463  constraintId);
2464 
2466  OIDOID, sizeof(Oid), true, TYPALIGN_INT,
2467  &elems, NULL, &nElems);
2468 
2469  operators = (Oid *) palloc(nElems * sizeof(Oid));
2470  for (i = 0; i < nElems; i++)
2471  operators[i] = DatumGetObjectId(elems[i]);
2472 
2473  /* pg_get_indexdef_worker does the rest */
2474  /* suppress tablespace because pg_dump wants it that way */
2476  pg_get_indexdef_worker(indexOid,
2477  0,
2478  operators,
2479  false,
2480  false,
2481  false,
2482  false,
2483  prettyFlags,
2484  false));
2485  break;
2486  }
2487  default:
2488  elog(ERROR, "invalid constraint type \"%c\"", conForm->contype);
2489  break;
2490  }
2491 
2492  if (conForm->condeferrable)
2493  appendStringInfoString(&buf, " DEFERRABLE");
2494  if (conForm->condeferred)
2495  appendStringInfoString(&buf, " INITIALLY DEFERRED");
2496  if (!conForm->convalidated)
2497  appendStringInfoString(&buf, " NOT VALID");
2498 
2499  /* Cleanup */
2500  systable_endscan(scandesc);
2501  table_close(relation, AccessShareLock);
2502 
2503  return buf.data;
2504 }
2505 
2506 
2507 /*
2508  * Convert an int16[] Datum into a comma-separated list of column names
2509  * for the indicated relation; append the list to buf. Returns the number
2510  * of keys.
2511  */
2512 static int
2513 decompile_column_index_array(Datum column_index_array, Oid relId,
2514  StringInfo buf)
2515 {
2516  Datum *keys;
2517  int nKeys;
2518  int j;
2519 
2520  /* Extract data from array of int16 */
2521  deconstruct_array(DatumGetArrayTypeP(column_index_array),
2522  INT2OID, 2, true, TYPALIGN_SHORT,
2523  &keys, NULL, &nKeys);
2524 
2525  for (j = 0; j < nKeys; j++)
2526  {
2527  char *colName;
2528 
2529  colName = get_attname(relId, DatumGetInt16(keys[j]), false);
2530 
2531  if (j == 0)
2533  else
2534  appendStringInfo(buf, ", %s", quote_identifier(colName));
2535  }
2536 
2537  return nKeys;
2538 }
2539 
2540 
2541 /* ----------
2542  * pg_get_expr - Decompile an expression tree
2543  *
2544  * Input: an expression tree in nodeToString form, and a relation OID
2545  *
2546  * Output: reverse-listed expression
2547  *
2548  * Currently, the expression can only refer to a single relation, namely
2549  * the one specified by the second parameter. This is sufficient for
2550  * partial indexes, column default expressions, etc. We also support
2551  * Var-free expressions, for which the OID can be InvalidOid.
2552  * ----------
2553  */
2554 Datum
2556 {
2557  text *expr = PG_GETARG_TEXT_PP(0);
2558  Oid relid = PG_GETARG_OID(1);
2559  int prettyFlags;
2560  char *relname;
2561 
2562  prettyFlags = PRETTYFLAG_INDENT;
2563 
2564  if (OidIsValid(relid))
2565  {
2566  /* Get the name for the relation */
2567  relname = get_rel_name(relid);
2568 
2569  /*
2570  * If the OID isn't actually valid, don't throw an error, just return
2571  * NULL. This is a bit questionable, but it's what we've done
2572  * historically, and it can help avoid unwanted failures when
2573  * examining catalog entries for just-deleted relations.
2574  */
2575  if (relname == NULL)
2576  PG_RETURN_NULL();
2577  }
2578  else
2579  relname = NULL;
2580 
2581  PG_RETURN_TEXT_P(pg_get_expr_worker(expr, relid, relname, prettyFlags));
2582 }
2583 
2584 Datum
2586 {
2587  text *expr = PG_GETARG_TEXT_PP(0);
2588  Oid relid = PG_GETARG_OID(1);
2589  bool pretty = PG_GETARG_BOOL(2);
2590  int prettyFlags;
2591  char *relname;
2592 
2594 
2595  if (OidIsValid(relid))
2596  {
2597  /* Get the name for the relation */
2598  relname = get_rel_name(relid);
2599  /* See notes above */
2600  if (relname == NULL)
2601  PG_RETURN_NULL();
2602  }
2603  else
2604  relname = NULL;
2605 
2606  PG_RETURN_TEXT_P(pg_get_expr_worker(expr, relid, relname, prettyFlags));
2607 }
2608 
2609 static text *
2610 pg_get_expr_worker(text *expr, Oid relid, const char *relname, int prettyFlags)
2611 {
2612  Node *node;
2613  List *context;
2614  char *exprstr;
2615  char *str;
2616 
2617  /* Convert input TEXT object to C string */
2618  exprstr = text_to_cstring(expr);
2619 
2620  /* Convert expression to node tree */
2621  node = (Node *) stringToNode(exprstr);
2622 
2623  pfree(exprstr);
2624 
2625  /* Prepare deparse context if needed */
2626  if (OidIsValid(relid))
2627  context = deparse_context_for(relname, relid);
2628  else
2629  context = NIL;
2630 
2631  /* Deparse */
2632  str = deparse_expression_pretty(node, context, false, false,
2633  prettyFlags, 0);
2634 
2635  return string_to_text(str);
2636 }
2637 
2638 
2639 /* ----------
2640  * pg_get_userbyid - Get a user name by roleid and
2641  * fallback to 'unknown (OID=n)'
2642  * ----------
2643  */
2644 Datum
2646 {
2647  Oid roleid = PG_GETARG_OID(0);
2648  Name result;
2649  HeapTuple roletup;
2650  Form_pg_authid role_rec;
2651 
2652  /*
2653  * Allocate space for the result
2654  */
2655  result = (Name) palloc(NAMEDATALEN);
2656  memset(NameStr(*result), 0, NAMEDATALEN);
2657 
2658  /*
2659  * Get the pg_authid entry and print the result
2660  */
2661  roletup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
2662  if (HeapTupleIsValid(roletup))
2663  {
2664  role_rec = (Form_pg_authid) GETSTRUCT(roletup);
2665  *result = role_rec->rolname;
2666  ReleaseSysCache(roletup);
2667  }
2668  else
2669  sprintf(NameStr(*result), "unknown (OID=%u)", roleid);
2670 
2671  PG_RETURN_NAME(result);
2672 }
2673 
2674 
2675 /*
2676  * pg_get_serial_sequence
2677  * Get the name of the sequence used by an identity or serial column,
2678  * formatted suitably for passing to setval, nextval or currval.
2679  * First parameter is not treated as double-quoted, second parameter
2680  * is --- see documentation for reason.
2681  */
2682 Datum
2684 {
2685  text *tablename = PG_GETARG_TEXT_PP(0);
2686  text *columnname = PG_GETARG_TEXT_PP(1);
2687  RangeVar *tablerv;
2688  Oid tableOid;
2689  char *column;
2691  Oid sequenceId = InvalidOid;
2692  Relation depRel;
2693  ScanKeyData key[3];
2694  SysScanDesc scan;
2695  HeapTuple tup;
2696 
2697  /* Look up table name. Can't lock it - we might not have privileges. */
2698  tablerv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
2699  tableOid = RangeVarGetRelid(tablerv, NoLock, false);
2700 
2701  /* Get the number of the column */
2702  column = text_to_cstring(columnname);
2703 
2704  attnum = get_attnum(tableOid, column);
2705  if (attnum == InvalidAttrNumber)
2706  ereport(ERROR,
2707  (errcode(ERRCODE_UNDEFINED_COLUMN),
2708  errmsg("column \"%s\" of relation \"%s\" does not exist",
2709  column, tablerv->relname)));
2710 
2711  /* Search the dependency table for the dependent sequence */
2712  depRel = table_open(DependRelationId, AccessShareLock);
2713 
2714  ScanKeyInit(&key[0],
2715  Anum_pg_depend_refclassid,
2716  BTEqualStrategyNumber, F_OIDEQ,
2717  ObjectIdGetDatum(RelationRelationId));
2718  ScanKeyInit(&key[1],
2719  Anum_pg_depend_refobjid,
2720  BTEqualStrategyNumber, F_OIDEQ,
2721  ObjectIdGetDatum(tableOid));
2722  ScanKeyInit(&key[2],
2723  Anum_pg_depend_refobjsubid,
2724  BTEqualStrategyNumber, F_INT4EQ,
2725  Int32GetDatum(attnum));
2726 
2727  scan = systable_beginscan(depRel, DependReferenceIndexId, true,
2728  NULL, 3, key);
2729 
2730  while (HeapTupleIsValid(tup = systable_getnext(scan)))
2731  {
2732  Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
2733 
2734  /*
2735  * Look for an auto dependency (serial column) or internal dependency
2736  * (identity column) of a sequence on a column. (We need the relkind
2737  * test because indexes can also have auto dependencies on columns.)
2738  */
2739  if (deprec->classid == RelationRelationId &&
2740  deprec->objsubid == 0 &&
2741  (deprec->deptype == DEPENDENCY_AUTO ||
2742  deprec->deptype == DEPENDENCY_INTERNAL) &&
2743  get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
2744  {
2745  sequenceId = deprec->objid;
2746  break;
2747  }
2748  }
2749 
2750  systable_endscan(scan);
2751  table_close(depRel, AccessShareLock);
2752 
2753  if (OidIsValid(sequenceId))
2754  {
2755  char *result;
2756 
2757  result = generate_qualified_relation_name(sequenceId);
2758 
2760  }
2761 
2762  PG_RETURN_NULL();
2763 }
2764 
2765 
2766 /*
2767  * pg_get_functiondef
2768  * Returns the complete "CREATE OR REPLACE FUNCTION ..." statement for
2769  * the specified function.
2770  *
2771  * Note: if you change the output format of this function, be careful not
2772  * to break psql's rules (in \ef and \sf) for identifying the start of the
2773  * function body. To wit: the function body starts on a line that begins
2774  * with "AS ", and no preceding line will look like that.
2775  */
2776 Datum
2778 {
2779  Oid funcid = PG_GETARG_OID(0);
2781  StringInfoData dq;
2782  HeapTuple proctup;
2783  Form_pg_proc proc;
2784  bool isfunction;
2785  Datum tmp;
2786  bool isnull;
2787  const char *prosrc;
2788  const char *name;
2789  const char *nsp;
2790  float4 procost;
2791  int oldlen;
2792 
2793  initStringInfo(&buf);
2794 
2795  /* Look up the function */
2796  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2797  if (!HeapTupleIsValid(proctup))
2798  PG_RETURN_NULL();
2799 
2800  proc = (Form_pg_proc) GETSTRUCT(proctup);
2801  name = NameStr(proc->proname);
2802 
2803  if (proc->prokind == PROKIND_AGGREGATE)
2804  ereport(ERROR,
2805  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2806  errmsg("\"%s\" is an aggregate function", name)));
2807 
2808  isfunction = (proc->prokind != PROKIND_PROCEDURE);
2809 
2810  /*
2811  * We always qualify the function name, to ensure the right function gets
2812  * replaced.
2813  */
2814  nsp = get_namespace_name(proc->pronamespace);
2815  appendStringInfo(&buf, "CREATE OR REPLACE %s %s(",
2816  isfunction ? "FUNCTION" : "PROCEDURE",
2817  quote_qualified_identifier(nsp, name));
2818  (void) print_function_arguments(&buf, proctup, false, true);
2819  appendStringInfoString(&buf, ")\n");
2820  if (isfunction)
2821  {
2822  appendStringInfoString(&buf, " RETURNS ");
2823  print_function_rettype(&buf, proctup);
2824  appendStringInfoChar(&buf, '\n');
2825  }
2826 
2827  print_function_trftypes(&buf, proctup);
2828 
2829  appendStringInfo(&buf, " LANGUAGE %s\n",
2830  quote_identifier(get_language_name(proc->prolang, false)));
2831 
2832  /* Emit some miscellaneous options on one line */
2833  oldlen = buf.len;
2834 
2835  if (proc->prokind == PROKIND_WINDOW)
2836  appendStringInfoString(&buf, " WINDOW");
2837  switch (proc->provolatile)
2838  {
2839  case PROVOLATILE_IMMUTABLE:
2840  appendStringInfoString(&buf, " IMMUTABLE");
2841  break;
2842  case PROVOLATILE_STABLE:
2843  appendStringInfoString(&buf, " STABLE");
2844  break;
2845  case PROVOLATILE_VOLATILE:
2846  break;
2847  }
2848 
2849  switch (proc->proparallel)
2850  {
2851  case PROPARALLEL_SAFE:
2852  appendStringInfoString(&buf, " PARALLEL SAFE");
2853  break;
2854  case PROPARALLEL_RESTRICTED:
2855  appendStringInfoString(&buf, " PARALLEL RESTRICTED");
2856  break;
2857  case PROPARALLEL_UNSAFE:
2858  break;
2859  }
2860 
2861  if (proc->proisstrict)
2862  appendStringInfoString(&buf, " STRICT");
2863  if (proc->prosecdef)
2864  appendStringInfoString(&buf, " SECURITY DEFINER");
2865  if (proc->proleakproof)
2866  appendStringInfoString(&buf, " LEAKPROOF");
2867 
2868  /* This code for the default cost and rows should match functioncmds.c */
2869  if (proc->prolang == INTERNALlanguageId ||
2870  proc->prolang == ClanguageId)
2871  procost = 1;
2872  else
2873  procost = 100;
2874  if (proc->procost != procost)
2875  appendStringInfo(&buf, " COST %g", proc->procost);
2876 
2877  if (proc->prorows > 0 && proc->prorows != 1000)
2878  appendStringInfo(&buf, " ROWS %g", proc->prorows);
2879 
2880  if (proc->prosupport)
2881  {
2882  Oid argtypes[1];
2883 
2884  /*
2885  * We should qualify the support function's name if it wouldn't be
2886  * resolved by lookup in the current search path.
2887  */
2888  argtypes[0] = INTERNALOID;
2889  appendStringInfo(&buf, " SUPPORT %s",
2890  generate_function_name(proc->prosupport, 1,
2891  NIL, argtypes,
2892  false, NULL, EXPR_KIND_NONE));
2893  }
2894 
2895  if (oldlen != buf.len)
2896  appendStringInfoChar(&buf, '\n');
2897 
2898  /* Emit any proconfig options, one per line */
2899  tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
2900  if (!isnull)
2901  {
2902  ArrayType *a = DatumGetArrayTypeP(tmp);
2903  int i;
2904 
2905  Assert(ARR_ELEMTYPE(a) == TEXTOID);
2906  Assert(ARR_NDIM(a) == 1);
2907  Assert(ARR_LBOUND(a)[0] == 1);
2908 
2909  for (i = 1; i <= ARR_DIMS(a)[0]; i++)
2910  {
2911  Datum d;
2912 
2913  d = array_ref(a, 1, &i,
2914  -1 /* varlenarray */ ,
2915  -1 /* TEXT's typlen */ ,
2916  false /* TEXT's typbyval */ ,
2917  TYPALIGN_INT /* TEXT's typalign */ ,
2918  &isnull);
2919  if (!isnull)
2920  {
2921  char *configitem = TextDatumGetCString(d);
2922  char *pos;
2923 
2924  pos = strchr(configitem, '=');
2925  if (pos == NULL)
2926  continue;
2927  *pos++ = '\0';
2928 
2929  appendStringInfo(&buf, " SET %s TO ",
2930  quote_identifier(configitem));
2931 
2932  /*
2933  * Variables that are marked GUC_LIST_QUOTE were already fully
2934  * quoted by flatten_set_variable_args() before they were put
2935  * into the proconfig array. However, because the quoting
2936  * rules used there aren't exactly like SQL's, we have to
2937  * break the list value apart and then quote the elements as
2938  * string literals. (The elements may be double-quoted as-is,
2939  * but we can't just feed them to the SQL parser; it would do
2940  * the wrong thing with elements that are zero-length or
2941  * longer than NAMEDATALEN.)
2942  *
2943  * Variables that are not so marked should just be emitted as
2944  * simple string literals. If the variable is not known to
2945  * guc.c, we'll do that; this makes it unsafe to use
2946  * GUC_LIST_QUOTE for extension variables.
2947  */
2948  if (GetConfigOptionFlags(configitem, true) & GUC_LIST_QUOTE)
2949  {
2950  List *namelist;
2951  ListCell *lc;
2952 
2953  /* Parse string into list of identifiers */
2954  if (!SplitGUCList(pos, ',', &namelist))
2955  {
2956  /* this shouldn't fail really */
2957  elog(ERROR, "invalid list syntax in proconfig item");
2958  }
2959  foreach(lc, namelist)
2960  {
2961  char *curname = (char *) lfirst(lc);
2962 
2963  simple_quote_literal(&buf, curname);
2964  if (lnext(namelist, lc))
2965  appendStringInfoString(&buf, ", ");
2966  }
2967  }
2968  else
2969  simple_quote_literal(&buf, pos);
2970  appendStringInfoChar(&buf, '\n');
2971  }
2972  }
2973  }
2974 
2975  /* And finally the function definition ... */
2976  tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
2977  if (proc->prolang == SQLlanguageId && !isnull)
2978  {
2979  print_function_sqlbody(&buf, proctup);
2980  }
2981  else
2982  {
2983  appendStringInfoString(&buf, "AS ");
2984 
2985  tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
2986  if (!isnull)
2987  {
2989  appendStringInfoString(&buf, ", "); /* assume prosrc isn't null */
2990  }
2991 
2992  tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosrc, &isnull);
2993  if (isnull)
2994  elog(ERROR, "null prosrc");
2995  prosrc = TextDatumGetCString(tmp);
2996 
2997  /*
2998  * We always use dollar quoting. Figure out a suitable delimiter.
2999  *
3000  * Since the user is likely to be editing the function body string, we
3001  * shouldn't use a short delimiter that he might easily create a conflict
3002  * with. Hence prefer "$function$"/"$procedure$", but extend if needed.
3003  */
3004  initStringInfo(&dq);
3005  appendStringInfoChar(&dq, '$');
3006  appendStringInfoString(&dq, (isfunction ? "function" : "procedure"));
3007  while (strstr(prosrc, dq.data) != NULL)
3008  appendStringInfoChar(&dq, 'x');
3009  appendStringInfoChar(&dq, '$');
3010 
3011  appendBinaryStringInfo(&buf, dq.data, dq.len);
3012  appendStringInfoString(&buf, prosrc);
3013  appendBinaryStringInfo(&buf, dq.data, dq.len);
3014  }
3015 
3016  appendStringInfoChar(&buf, '\n');
3017 
3018  ReleaseSysCache(proctup);
3019 
3021 }
3022 
3023 /*
3024  * pg_get_function_arguments
3025  * Get a nicely-formatted list of arguments for a function.
3026  * This is everything that would go between the parentheses in
3027  * CREATE FUNCTION.
3028  */
3029 Datum
3031 {
3032  Oid funcid = PG_GETARG_OID(0);
3034  HeapTuple proctup;
3035 
3036  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3037  if (!HeapTupleIsValid(proctup))
3038  PG_RETURN_NULL();
3039 
3040  initStringInfo(&buf);
3041 
3042  (void) print_function_arguments(&buf, proctup, false, true);
3043 
3044  ReleaseSysCache(proctup);
3045 
3047 }
3048 
3049 /*
3050  * pg_get_function_identity_arguments
3051  * Get a formatted list of arguments for a function.
3052  * This is everything that would go between the parentheses in
3053  * ALTER FUNCTION, etc. In particular, don't print defaults.
3054  */
3055 Datum
3057 {
3058  Oid funcid = PG_GETARG_OID(0);
3060  HeapTuple proctup;
3061 
3062  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3063  if (!HeapTupleIsValid(proctup))
3064  PG_RETURN_NULL();
3065 
3066  initStringInfo(&buf);
3067 
3068  (void) print_function_arguments(&buf, proctup, false, false);
3069 
3070  ReleaseSysCache(proctup);
3071 
3073 }
3074 
3075 /*
3076  * pg_get_function_result
3077  * Get a nicely-formatted version of the result type of a function.
3078  * This is what would appear after RETURNS in CREATE FUNCTION.
3079  */
3080 Datum
3082 {
3083  Oid funcid = PG_GETARG_OID(0);
3085  HeapTuple proctup;
3086 
3087  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3088  if (!HeapTupleIsValid(proctup))
3089  PG_RETURN_NULL();
3090 
3091  if (((Form_pg_proc) GETSTRUCT(proctup))->prokind == PROKIND_PROCEDURE)
3092  {
3093  ReleaseSysCache(proctup);
3094  PG_RETURN_NULL();
3095  }
3096 
3097  initStringInfo(&buf);
3098 
3099  print_function_rettype(&buf, proctup);
3100 
3101  ReleaseSysCache(proctup);
3102 
3104 }
3105 
3106 /*
3107  * Guts of pg_get_function_result: append the function's return type
3108  * to the specified buffer.
3109  */
3110 static void
3112 {
3113  Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
3114  int ntabargs = 0;
3115  StringInfoData rbuf;
3116 
3117  initStringInfo(&rbuf);
3118 
3119  if (proc->proretset)
3120  {
3121  /* It might be a table function; try to print the arguments */
3122  appendStringInfoString(&rbuf, "TABLE(");
3123  ntabargs = print_function_arguments(&rbuf, proctup, true, false);
3124  if (ntabargs > 0)
3125  appendStringInfoChar(&rbuf, ')');
3126  else
3127  resetStringInfo(&rbuf);
3128  }
3129 
3130  if (ntabargs == 0)
3131  {
3132  /* Not a table function, so do the normal thing */
3133  if (proc->proretset)
3134  appendStringInfoString(&rbuf, "SETOF ");
3135  appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
3136  }
3137 
3138  appendBinaryStringInfo(buf, rbuf.data, rbuf.len);
3139 }
3140 
3141 /*
3142  * Common code for pg_get_function_arguments and pg_get_function_result:
3143  * append the desired subset of arguments to buf. We print only TABLE
3144  * arguments when print_table_args is true, and all the others when it's false.
3145  * We print argument defaults only if print_defaults is true.
3146  * Function return value is the number of arguments printed.
3147  */
3148 static int
3150  bool print_table_args, bool print_defaults)
3151 {
3152  Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
3153  int numargs;
3154  Oid *argtypes;
3155  char **argnames;
3156  char *argmodes;
3157  int insertorderbyat = -1;
3158  int argsprinted;
3159  int inputargno;
3160  int nlackdefaults;
3161  List *argdefaults = NIL;
3162  ListCell *nextargdefault = NULL;
3163  int i;
3164 
3165  numargs = get_func_arg_info(proctup,
3166  &argtypes, &argnames, &argmodes);
3167 
3168  nlackdefaults = numargs;
3169  if (print_defaults && proc->pronargdefaults > 0)
3170  {
3171  Datum proargdefaults;
3172  bool isnull;
3173 
3174  proargdefaults = SysCacheGetAttr(PROCOID, proctup,
3175  Anum_pg_proc_proargdefaults,
3176  &isnull);
3177  if (!isnull)
3178  {
3179  char *str;
3180 
3181  str = TextDatumGetCString(proargdefaults);
3182  argdefaults = castNode(List, stringToNode(str));
3183  pfree(str);
3184  nextargdefault = list_head(argdefaults);
3185  /* nlackdefaults counts only *input* arguments lacking defaults */
3186  nlackdefaults = proc->pronargs - list_length(argdefaults);
3187  }
3188  }
3189 
3190  /* Check for special treatment of ordered-set aggregates */
3191  if (proc->prokind == PROKIND_AGGREGATE)
3192  {
3193  HeapTuple aggtup;
3194  Form_pg_aggregate agg;
3195 
3196  aggtup = SearchSysCache1(AGGFNOID, proc->oid);
3197  if (!HeapTupleIsValid(aggtup))
3198  elog(ERROR, "cache lookup failed for aggregate %u",
3199  proc->oid);
3200  agg = (Form_pg_aggregate) GETSTRUCT(aggtup);
3201  if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3202  insertorderbyat = agg->aggnumdirectargs;
3203  ReleaseSysCache(aggtup);
3204  }
3205 
3206  argsprinted = 0;
3207  inputargno = 0;
3208  for (i = 0; i < numargs; i++)
3209  {
3210  Oid argtype = argtypes[i];
3211  char *argname = argnames ? argnames[i] : NULL;
3212  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
3213  const char *modename;
3214  bool isinput;
3215 
3216  switch (argmode)
3217  {
3218  case PROARGMODE_IN:
3219  modename = "";
3220  isinput = true;
3221  break;
3222  case PROARGMODE_INOUT:
3223  modename = "INOUT ";
3224  isinput = true;
3225  break;
3226  case PROARGMODE_OUT:
3227  modename = "OUT ";
3228  isinput = false;
3229  break;
3230  case PROARGMODE_VARIADIC:
3231  modename = "VARIADIC ";
3232  isinput = true;
3233  break;
3234  case PROARGMODE_TABLE:
3235  modename = "";
3236  isinput = false;
3237  break;
3238  default:
3239  elog(ERROR, "invalid parameter mode '%c'", argmode);
3240  modename = NULL; /* keep compiler quiet */
3241  isinput = false;
3242  break;
3243  }
3244  if (isinput)
3245  inputargno++; /* this is a 1-based counter */
3246 
3247  if (print_table_args != (argmode == PROARGMODE_TABLE))
3248  continue;
3249 
3250  if (argsprinted == insertorderbyat)
3251  {
3252  if (argsprinted)
3253  appendStringInfoChar(buf, ' ');
3254  appendStringInfoString(buf, "ORDER BY ");
3255  }
3256  else if (argsprinted)
3257  appendStringInfoString(buf, ", ");
3258 
3259  appendStringInfoString(buf, modename);
3260  if (argname && argname[0])
3261  appendStringInfo(buf, "%s ", quote_identifier(argname));
3262  appendStringInfoString(buf, format_type_be(argtype));
3263  if (print_defaults && isinput && inputargno > nlackdefaults)
3264  {
3265  Node *expr;
3266 
3267  Assert(nextargdefault != NULL);
3268  expr = (Node *) lfirst(nextargdefault);
3269  nextargdefault = lnext(argdefaults, nextargdefault);
3270 
3271  appendStringInfo(buf, " DEFAULT %s",
3272  deparse_expression(expr, NIL, false, false));
3273  }
3274  argsprinted++;
3275 
3276  /* nasty hack: print the last arg twice for variadic ordered-set agg */
3277  if (argsprinted == insertorderbyat && i == numargs - 1)
3278  {
3279  i--;
3280  /* aggs shouldn't have defaults anyway, but just to be sure ... */
3281  print_defaults = false;
3282  }
3283  }
3284 
3285  return argsprinted;
3286 }
3287 
3288 static bool
3289 is_input_argument(int nth, const char *argmodes)
3290 {
3291  return (!argmodes
3292  || argmodes[nth] == PROARGMODE_IN
3293  || argmodes[nth] == PROARGMODE_INOUT
3294  || argmodes[nth] == PROARGMODE_VARIADIC);
3295 }
3296 
3297 /*
3298  * Append used transformed types to specified buffer
3299  */
3300 static void
3302 {
3303  Oid *trftypes;
3304  int ntypes;
3305 
3306  ntypes = get_func_trftypes(proctup, &trftypes);
3307  if (ntypes > 0)
3308  {
3309  int i;
3310 
3311  appendStringInfoString(buf, " TRANSFORM ");
3312  for (i = 0; i < ntypes; i++)
3313  {
3314  if (i != 0)
3315  appendStringInfoString(buf, ", ");
3316  appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
3317  }
3318  appendStringInfoChar(buf, '\n');
3319  }
3320 }
3321 
3322 /*
3323  * Get textual representation of a function argument's default value. The
3324  * second argument of this function is the argument number among all arguments
3325  * (i.e. proallargtypes, *not* proargtypes), starting with 1, because that's
3326  * how information_schema.sql uses it.
3327  */
3328 Datum
3330 {
3331  Oid funcid = PG_GETARG_OID(0);
3332  int32 nth_arg = PG_GETARG_INT32(1);
3333  HeapTuple proctup;
3334  Form_pg_proc proc;
3335  int numargs;
3336  Oid *argtypes;
3337  char **argnames;
3338  char *argmodes;
3339  int i;
3340  List *argdefaults;
3341  Node *node;
3342  char *str;
3343  int nth_inputarg;
3344  Datum proargdefaults;
3345  bool isnull;
3346  int nth_default;
3347 
3348  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3349  if (!HeapTupleIsValid(proctup))
3350  PG_RETURN_NULL();
3351 
3352  numargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
3353  if (nth_arg < 1 || nth_arg > numargs || !is_input_argument(nth_arg - 1, argmodes))
3354  {
3355  ReleaseSysCache(proctup);
3356  PG_RETURN_NULL();
3357  }
3358 
3359  nth_inputarg = 0;
3360  for (i = 0; i < nth_arg; i++)
3361  if (is_input_argument(i, argmodes))
3362  nth_inputarg++;
3363 
3364  proargdefaults = SysCacheGetAttr(PROCOID, proctup,
3365  Anum_pg_proc_proargdefaults,
3366  &isnull);
3367  if (isnull)
3368  {
3369  ReleaseSysCache(proctup);
3370  PG_RETURN_NULL();
3371  }
3372 
3373  str = TextDatumGetCString(proargdefaults);
3374  argdefaults = castNode(List, stringToNode(str));
3375  pfree(str);
3376 
3377  proc = (Form_pg_proc) GETSTRUCT(proctup);
3378 
3379  /*
3380  * Calculate index into proargdefaults: proargdefaults corresponds to the
3381  * last N input arguments, where N = pronargdefaults.
3382  */
3383  nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3384 
3385  if (nth_default < 0 || nth_default >= list_length(argdefaults))
3386  {
3387  ReleaseSysCache(proctup);
3388  PG_RETURN_NULL();
3389  }
3390  node = list_nth(argdefaults, nth_default);
3391  str = deparse_expression(node, NIL, false, false);
3392 
3393  ReleaseSysCache(proctup);
3394 
3396 }
3397 
3398 static void
3400 {
3401  int numargs;
3402  Oid *argtypes;
3403  char **argnames;
3404  char *argmodes;
3405  deparse_namespace dpns = {0};
3406  Datum tmp;
3407  bool isnull;
3408  Node *n;
3409 
3410  dpns.funcname = pstrdup(NameStr(((Form_pg_proc) GETSTRUCT(proctup))->proname));
3411  numargs = get_func_arg_info(proctup,
3412  &argtypes, &argnames, &argmodes);
3413  dpns.numargs = numargs;
3414  dpns.argnames = argnames;
3415 
3416  tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3417  Assert(!isnull);
3419 
3420  if (IsA(n, List))
3421  {
3422  List *stmts;
3423  ListCell *lc;
3424 
3425  stmts = linitial(castNode(List, n));
3426 
3427  appendStringInfoString(buf, "BEGIN ATOMIC\n");
3428 
3429  foreach(lc, stmts)
3430  {
3431  Query *query = lfirst_node(Query, lc);
3432 
3433  get_query_def(query, buf, list_make1(&dpns), NULL, PRETTYFLAG_INDENT, WRAP_COLUMN_DEFAULT, 1);
3434  appendStringInfoChar(buf, ';');
3435  appendStringInfoChar(buf, '\n');
3436  }
3437 
3438  appendStringInfoString(buf, "END");
3439  }
3440  else
3441  {
3442  get_query_def(castNode(Query, n), buf, list_make1(&dpns), NULL, 0, WRAP_COLUMN_DEFAULT, 0);
3443  }
3444 }
3445 
3446 Datum
3448 {
3449  Oid funcid = PG_GETARG_OID(0);
3451  HeapTuple proctup;
3452  bool isnull;
3453 
3454  initStringInfo(&buf);
3455 
3456  /* Look up the function */
3457  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3458  if (!HeapTupleIsValid(proctup))
3459  PG_RETURN_NULL();
3460 
3461  SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3462  if (isnull)
3463  {
3464  ReleaseSysCache(proctup);
3465  PG_RETURN_NULL();
3466  }
3467 
3468  print_function_sqlbody(&buf, proctup);
3469 
3470  ReleaseSysCache(proctup);
3471 
3473 }
3474 
3475 
3476 /*
3477  * deparse_expression - General utility for deparsing expressions
3478  *
3479  * calls deparse_expression_pretty with all prettyPrinting disabled
3480  */
3481 char *
3482 deparse_expression(Node *expr, List *dpcontext,
3483  bool forceprefix, bool showimplicit)
3484 {
3485  return deparse_expression_pretty(expr, dpcontext, forceprefix,
3486  showimplicit, 0, 0);
3487 }
3488 
3489 /* ----------
3490  * deparse_expression_pretty - General utility for deparsing expressions
3491  *
3492  * expr is the node tree to be deparsed. It must be a transformed expression
3493  * tree (ie, not the raw output of gram.y).
3494  *
3495  * dpcontext is a list of deparse_namespace nodes representing the context
3496  * for interpreting Vars in the node tree. It can be NIL if no Vars are
3497  * expected.
3498  *
3499  * forceprefix is true to force all Vars to be prefixed with their table names.
3500  *
3501  * showimplicit is true to force all implicit casts to be shown explicitly.
3502  *
3503  * Tries to pretty up the output according to prettyFlags and startIndent.
3504  *
3505  * The result is a palloc'd string.
3506  * ----------
3507  */
3508 static char *
3510  bool forceprefix, bool showimplicit,
3511  int prettyFlags, int startIndent)
3512 {
3514  deparse_context context;
3515 
3516  initStringInfo(&buf);
3517  context.buf = &buf;
3518  context.namespaces = dpcontext;
3519  context.windowClause = NIL;
3520  context.windowTList = NIL;
3521  context.varprefix = forceprefix;
3522  context.prettyFlags = prettyFlags;
3523  context.wrapColumn = WRAP_COLUMN_DEFAULT;
3524  context.indentLevel = startIndent;
3525  context.special_exprkind = EXPR_KIND_NONE;
3526  context.appendparents = NULL;
3527 
3528  get_rule_expr(expr, &context, showimplicit);
3529 
3530  return buf.data;
3531 }
3532 
3533 /* ----------
3534  * deparse_context_for - Build deparse context for a single relation
3535  *
3536  * Given the reference name (alias) and OID of a relation, build deparsing
3537  * context for an expression referencing only that relation (as varno 1,
3538  * varlevelsup 0). This is sufficient for many uses of deparse_expression.
3539  * ----------
3540  */
3541 List *
3542 deparse_context_for(const char *aliasname, Oid relid)
3543 {
3544  deparse_namespace *dpns;
3545  RangeTblEntry *rte;
3546 
3547  dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3548 
3549  /* Build a minimal RTE for the rel */
3550  rte = makeNode(RangeTblEntry);
3551  rte->rtekind = RTE_RELATION;
3552  rte->relid = relid;
3553  rte->relkind = RELKIND_RELATION; /* no need for exactness here */
3555  rte->alias = makeAlias(aliasname, NIL);
3556  rte->eref = rte->alias;
3557  rte->lateral = false;
3558  rte->inh = false;
3559  rte->inFromCl = true;
3560 
3561  /* Build one-element rtable */
3562  dpns->rtable = list_make1(rte);
3563  dpns->subplans = NIL;
3564  dpns->ctes = NIL;
3565  dpns->appendrels = NULL;
3566  set_rtable_names(dpns, NIL, NULL);
3568 
3569  /* Return a one-deep namespace stack */
3570  return list_make1(dpns);
3571 }
3572 
3573 /*
3574  * deparse_context_for_plan_tree - Build deparse context for a Plan tree
3575  *
3576  * When deparsing an expression in a Plan tree, we use the plan's rangetable
3577  * to resolve names of simple Vars. The initialization of column names for
3578  * this is rather expensive if the rangetable is large, and it'll be the same
3579  * for every expression in the Plan tree; so we do it just once and re-use
3580  * the result of this function for each expression. (Note that the result
3581  * is not usable until set_deparse_context_plan() is applied to it.)
3582  *
3583  * In addition to the PlannedStmt, pass the per-RTE alias names
3584  * assigned by a previous call to select_rtable_names_for_explain.
3585  */
3586 List *
3588 {
3589  deparse_namespace *dpns;
3590 
3591  dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3592 
3593  /* Initialize fields that stay the same across the whole plan tree */
3594  dpns->rtable = pstmt->rtable;
3595  dpns->rtable_names = rtable_names;
3596  dpns->subplans = pstmt->subplans;
3597  dpns->ctes = NIL;
3598  if (pstmt->appendRelations)
3599  {
3600  /* Set up the array, indexed by child relid */
3601  int ntables = list_length(dpns->rtable);
3602  ListCell *lc;
3603 
3604  dpns->appendrels = (AppendRelInfo **)
3605  palloc0((ntables + 1) * sizeof(AppendRelInfo *));
3606  foreach(lc, pstmt->appendRelations)
3607  {
3608  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
3609  Index crelid = appinfo->child_relid;
3610 
3611  Assert(crelid > 0 && crelid <= ntables);
3612  Assert(dpns->appendrels[crelid] == NULL);
3613  dpns->appendrels[crelid] = appinfo;
3614  }
3615  }
3616  else
3617  dpns->appendrels = NULL; /* don't need it */
3618 
3619  /*
3620  * Set up column name aliases. We will get rather bogus results for join
3621  * RTEs, but that doesn't matter because plan trees don't contain any join
3622  * alias Vars.
3623  */
3625 
3626  /* Return a one-deep namespace stack */
3627  return list_make1(dpns);
3628 }
3629 
3630 /*
3631  * set_deparse_context_plan - Specify Plan node containing expression
3632  *
3633  * When deparsing an expression in a Plan tree, we might have to resolve
3634  * OUTER_VAR, INNER_VAR, or INDEX_VAR references. To do this, the caller must
3635  * provide the parent Plan node. Then OUTER_VAR and INNER_VAR references
3636  * can be resolved by drilling down into the left and right child plans.
3637  * Similarly, INDEX_VAR references can be resolved by reference to the
3638  * indextlist given in a parent IndexOnlyScan node, or to the scan tlist in
3639  * ForeignScan and CustomScan nodes. (Note that we don't currently support
3640  * deparsing of indexquals in regular IndexScan or BitmapIndexScan nodes;
3641  * for those, we can only deparse the indexqualorig fields, which won't
3642  * contain INDEX_VAR Vars.)
3643  *
3644  * The ancestors list is a list of the Plan's parent Plan and SubPlan nodes,
3645  * the most-closely-nested first. This is needed to resolve PARAM_EXEC
3646  * Params. Note we assume that all the Plan nodes share the same rtable.
3647  *
3648  * Once this function has been called, deparse_expression() can be called on
3649  * subsidiary expression(s) of the specified Plan node. To deparse
3650  * expressions of a different Plan node in the same Plan tree, re-call this
3651  * function to identify the new parent Plan node.
3652  *
3653  * The result is the same List passed in; this is a notational convenience.
3654  */
3655 List *
3656 set_deparse_context_plan(List *dpcontext, Plan *plan, List *ancestors)
3657 {
3658  deparse_namespace *dpns;
3659 
3660  /* Should always have one-entry namespace list for Plan deparsing */
3661  Assert(list_length(dpcontext) == 1);
3662  dpns = (deparse_namespace *) linitial(dpcontext);
3663 
3664  /* Set our attention on the specific plan node passed in */
3665  set_deparse_plan(dpns, plan);
3666  dpns->ancestors = ancestors;
3667 
3668  return dpcontext;
3669 }
3670 
3671 /*
3672  * select_rtable_names_for_explain - Select RTE aliases for EXPLAIN
3673  *
3674  * Determine the relation aliases we'll use during an EXPLAIN operation.
3675  * This is just a frontend to set_rtable_names. We have to expose the aliases
3676  * to EXPLAIN because EXPLAIN needs to know the right alias names to print.
3677  */
3678 List *
3680 {
3681  deparse_namespace dpns;
3682 
3683  memset(&dpns, 0, sizeof(dpns));
3684  dpns.rtable = rtable;
3685  dpns.subplans = NIL;
3686  dpns.ctes = NIL;
3687  dpns.appendrels = NULL;
3688  set_rtable_names(&dpns, NIL, rels_used);
3689  /* We needn't bother computing column aliases yet */
3690 
3691  return dpns.rtable_names;
3692 }
3693 
3694 /*
3695  * set_rtable_names: select RTE aliases to be used in printing a query
3696  *
3697  * We fill in dpns->rtable_names with a list of names that is one-for-one with
3698  * the already-filled dpns->rtable list. Each RTE name is unique among those
3699  * in the new namespace plus any ancestor namespaces listed in
3700  * parent_namespaces.
3701  *
3702  * If rels_used isn't NULL, only RTE indexes listed in it are given aliases.
3703  *
3704  * Note that this function is only concerned with relation names, not column
3705  * names.
3706  */
3707 static void
3708 set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
3709  Bitmapset *rels_used)
3710 {
3711  HASHCTL hash_ctl;
3712  HTAB *names_hash;
3713  NameHashEntry *hentry;
3714  bool found;
3715  int rtindex;
3716  ListCell *lc;
3717 
3718  dpns->rtable_names = NIL;
3719  /* nothing more to do if empty rtable */
3720  if (dpns->rtable == NIL)
3721  return;
3722 
3723  /*
3724  * We use a hash table to hold known names, so that this process is O(N)
3725  * not O(N^2) for N names.
3726  */
3727  hash_ctl.keysize = NAMEDATALEN;
3728  hash_ctl.entrysize = sizeof(NameHashEntry);
3729  hash_ctl.hcxt = CurrentMemoryContext;
3730  names_hash = hash_create("set_rtable_names names",
3731  list_length(dpns->rtable),
3732  &hash_ctl,
3734 
3735  /* Preload the hash table with names appearing in parent_namespaces */
3736  foreach(lc, parent_namespaces)
3737  {
3738  deparse_namespace *olddpns = (deparse_namespace *) lfirst(lc);
3739  ListCell *lc2;
3740 
3741  foreach(lc2, olddpns->rtable_names)
3742  {
3743  char *oldname = (char *) lfirst(lc2);
3744 
3745  if (oldname == NULL)
3746  continue;
3747  hentry = (NameHashEntry *) hash_search(names_hash,
3748  oldname,
3749  HASH_ENTER,
3750  &found);
3751  /* we do not complain about duplicate names in parent namespaces */
3752  hentry->counter = 0;
3753  }
3754  }
3755 
3756  /* Now we can scan the rtable */
3757  rtindex = 1;
3758  foreach(lc, dpns->rtable)
3759  {
3760  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3761  char *refname;
3762 
3763  /* Just in case this takes an unreasonable amount of time ... */
3765 
3766  if (rels_used && !bms_is_member(rtindex, rels_used))
3767  {
3768  /* Ignore unreferenced RTE */
3769  refname = NULL;
3770  }
3771  else if (rte->alias)
3772  {
3773  /* If RTE has a user-defined alias, prefer that */
3774  refname = rte->alias->aliasname;
3775  }
3776  else if (rte->rtekind == RTE_RELATION)
3777  {
3778  /* Use the current actual name of the relation */
3779  refname = get_rel_name(rte->relid);
3780  }
3781  else if (rte->rtekind == RTE_JOIN)
3782  {
3783  /* Unnamed join has no refname */
3784  refname = NULL;
3785  }
3786  else
3787  {
3788  /* Otherwise use whatever the parser assigned */
3789  refname = rte->eref->aliasname;
3790  }
3791 
3792  /*
3793  * If the selected name isn't unique, append digits to make it so, and
3794  * make a new hash entry for it once we've got a unique name. For a
3795  * very long input name, we might have to truncate to stay within
3796  * NAMEDATALEN.
3797  */
3798  if (refname)
3799  {
3800  hentry = (NameHashEntry *) hash_search(names_hash,
3801  refname,
3802  HASH_ENTER,
3803  &found);
3804  if (found)
3805  {
3806  /* Name already in use, must choose a new one */
3807  int refnamelen = strlen(refname);
3808  char *modname = (char *) palloc(refnamelen + 16);
3809  NameHashEntry *hentry2;
3810 
3811  do
3812  {
3813  hentry->counter++;
3814  for (;;)
3815  {
3816  memcpy(modname, refname, refnamelen);
3817  sprintf(modname + refnamelen, "_%d", hentry->counter);
3818  if (strlen(modname) < NAMEDATALEN)
3819  break;
3820  /* drop chars from refname to keep all the digits */
3821  refnamelen = pg_mbcliplen(refname, refnamelen,
3822  refnamelen - 1);
3823  }
3824  hentry2 = (NameHashEntry *) hash_search(names_hash,
3825  modname,
3826  HASH_ENTER,
3827  &found);
3828  } while (found);
3829  hentry2->counter = 0; /* init new hash entry */
3830  refname = modname;
3831  }
3832  else
3833  {
3834  /* Name not previously used, need only initialize hentry */
3835  hentry->counter = 0;
3836  }
3837  }
3838 
3839  dpns->rtable_names = lappend(dpns->rtable_names, refname);
3840  rtindex++;
3841  }
3842 
3843  hash_destroy(names_hash);
3844 }
3845 
3846 /*
3847  * set_deparse_for_query: set up deparse_namespace for deparsing a Query tree
3848  *
3849  * For convenience, this is defined to initialize the deparse_namespace struct
3850  * from scratch.
3851  */
3852 static void
3854  List *parent_namespaces)
3855 {
3856  ListCell *lc;
3857  ListCell *lc2;
3858 
3859  /* Initialize *dpns and fill rtable/ctes links */
3860  memset(dpns, 0, sizeof(deparse_namespace));
3861  dpns->rtable = query->rtable;
3862  dpns->subplans = NIL;
3863  dpns->ctes = query->cteList;
3864  dpns->appendrels = NULL;
3865 
3866  /* Assign a unique relation alias to each RTE */
3867  set_rtable_names(dpns, parent_namespaces, NULL);
3868 
3869  /* Initialize dpns->rtable_columns to contain zeroed structs */
3870  dpns->rtable_columns = NIL;
3871  while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
3872  dpns->rtable_columns = lappend(dpns->rtable_columns,
3873  palloc0(sizeof(deparse_columns)));
3874 
3875  /* If it's a utility query, it won't have a jointree */
3876  if (query->jointree)
3877  {
3878  /* Detect whether global uniqueness of USING names is needed */
3879  dpns->unique_using =
3880  has_dangerous_join_using(dpns, (Node *) query->jointree);
3881 
3882  /*
3883  * Select names for columns merged by USING, via a recursive pass over
3884  * the query jointree.
3885  */
3886  set_using_names(dpns, (Node *) query->jointree, NIL);
3887  }
3888 
3889  /*
3890  * Now assign remaining column aliases for each RTE. We do this in a
3891  * linear scan of the rtable, so as to process RTEs whether or not they
3892  * are in the jointree (we mustn't miss NEW.*, INSERT target relations,
3893  * etc). JOIN RTEs must be processed after their children, but this is
3894  * okay because they appear later in the rtable list than their children
3895  * (cf Asserts in identify_join_columns()).
3896  */
3897  forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
3898  {
3899  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3900  deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
3901 
3902  if (rte->rtekind == RTE_JOIN)
3903  set_join_column_names(dpns, rte, colinfo);
3904  else
3905  set_relation_column_names(dpns, rte, colinfo);
3906  }
3907 }
3908 
3909 /*
3910  * set_simple_column_names: fill in column aliases for non-query situations
3911  *
3912  * This handles EXPLAIN and cases where we only have relation RTEs. Without
3913  * a join tree, we can't do anything smart about join RTEs, but we don't
3914  * need to (note that EXPLAIN should never see join alias Vars anyway).
3915  * If we do hit a join RTE we'll just process it like a non-table base RTE.
3916  */
3917 static void
3919 {
3920  ListCell *lc;
3921  ListCell *lc2;
3922 
3923  /* Initialize dpns->rtable_columns to contain zeroed structs */
3924  dpns->rtable_columns = NIL;
3925  while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
3926  dpns->rtable_columns = lappend(dpns->rtable_columns,
3927  palloc0(sizeof(deparse_columns)));
3928 
3929  /* Assign unique column aliases within each RTE */
3930  forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
3931  {
3932  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3933  deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
3934 
3935  set_relation_column_names(dpns, rte, colinfo);
3936  }
3937 }
3938 
3939 /*
3940  * has_dangerous_join_using: search jointree for unnamed JOIN USING
3941  *
3942  * Merged columns of a JOIN USING may act differently from either of the input
3943  * columns, either because they are merged with COALESCE (in a FULL JOIN) or
3944  * because an implicit coercion of the underlying input column is required.
3945  * In such a case the column must be referenced as a column of the JOIN not as
3946  * a column of either input. And this is problematic if the join is unnamed
3947  * (alias-less): we cannot qualify the column's name with an RTE name, since
3948  * there is none. (Forcibly assigning an alias to the join is not a solution,
3949  * since that will prevent legal references to tables below the join.)
3950  * To ensure that every column in the query is unambiguously referenceable,
3951  * we must assign such merged columns names that are globally unique across
3952  * the whole query, aliasing other columns out of the way as necessary.
3953  *
3954  * Because the ensuing re-aliasing is fairly damaging to the readability of
3955  * the query, we don't do this unless we have to. So, we must pre-scan
3956  * the join tree to see if we have to, before starting set_using_names().
3957  */
3958 static bool
3960 {
3961  if (IsA(jtnode, RangeTblRef))
3962  {
3963  /* nothing to do here */
3964  }
3965  else if (IsA(jtnode, FromExpr))
3966  {
3967  FromExpr *f = (FromExpr *) jtnode;
3968  ListCell *lc;
3969 
3970  foreach(lc, f->fromlist)
3971  {
3972  if (has_dangerous_join_using(dpns, (Node *) lfirst(lc)))
3973  return true;
3974  }
3975  }
3976  else if (IsA(jtnode, JoinExpr))
3977  {
3978  JoinExpr *j = (JoinExpr *) jtnode;
3979 
3980  /* Is it an unnamed JOIN with USING? */
3981  if (j->alias == NULL && j->usingClause)
3982  {
3983  /*
3984  * Yes, so check each join alias var to see if any of them are not
3985  * simple references to underlying columns. If so, we have a
3986  * dangerous situation and must pick unique aliases.
3987  */
3988  RangeTblEntry *jrte = rt_fetch(j->rtindex, dpns->rtable);
3989 
3990  /* We need only examine the merged columns */
3991  for (int i = 0; i < jrte->joinmergedcols; i++)
3992  {
3993  Node *aliasvar = list_nth(jrte->joinaliasvars, i);
3994 
3995  if (!IsA(aliasvar, Var))
3996  return true;
3997  }
3998  }
3999 
4000  /* Nope, but inspect children */
4001  if (has_dangerous_join_using(dpns, j->larg))
4002  return true;
4003  if (has_dangerous_join_using(dpns, j->rarg))
4004  return true;
4005  }
4006  else
4007  elog(ERROR, "unrecognized node type: %d",
4008  (int) nodeTag(jtnode));
4009  return false;
4010 }
4011 
4012 /*
4013  * set_using_names: select column aliases to be used for merged USING columns
4014  *
4015  * We do this during a recursive descent of the query jointree.
4016  * dpns->unique_using must already be set to determine the global strategy.
4017  *
4018  * Column alias info is saved in the dpns->rtable_columns list, which is
4019  * assumed to be filled with pre-zeroed deparse_columns structs.
4020  *
4021  * parentUsing is a list of all USING aliases assigned in parent joins of
4022  * the current jointree node. (The passed-in list must not be modified.)
4023  */
4024 static void
4025 set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing)
4026 {
4027  if (IsA(jtnode, RangeTblRef))
4028  {
4029  /* nothing to do now */
4030  }
4031  else if (IsA(jtnode, FromExpr))
4032  {
4033  FromExpr *f = (FromExpr *) jtnode;
4034  ListCell *lc;
4035 
4036  foreach(lc, f->fromlist)
4037  set_using_names(dpns, (Node *) lfirst(lc), parentUsing);
4038  }
4039  else if (IsA(jtnode, JoinExpr))
4040  {
4041  JoinExpr *j = (JoinExpr *) jtnode;
4042  RangeTblEntry *rte = rt_fetch(j->rtindex, dpns->rtable);
4043  deparse_columns *colinfo = deparse_columns_fetch(j->rtindex, dpns);
4044  int *leftattnos;
4045  int *rightattnos;
4046  deparse_columns *leftcolinfo;
4047  deparse_columns *rightcolinfo;
4048  int i;
4049  ListCell *lc;
4050 
4051  /* Get info about the shape of the join */
4052  identify_join_columns(j, rte, colinfo);
4053  leftattnos = colinfo->leftattnos;
4054  rightattnos = colinfo->rightattnos;
4055 
4056  /* Look up the not-yet-filled-in child deparse_columns structs */
4057  leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
4058  rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
4059 
4060  /*
4061  * If this join is unnamed, then we cannot substitute new aliases at
4062  * this level, so any name requirements pushed down to here must be
4063  * pushed down again to the children.
4064  */
4065  if (rte->alias == NULL)
4066  {
4067  for (i = 0; i < colinfo->num_cols; i++)
4068  {
4069  char *colname = colinfo->colnames[i];
4070 
4071  if (colname == NULL)
4072  continue;
4073 
4074  /* Push down to left column, unless it's a system column */
4075  if (leftattnos[i] > 0)
4076  {
4077  expand_colnames_array_to(leftcolinfo, leftattnos[i]);
4078  leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4079  }
4080 
4081  /* Same on the righthand side */
4082  if (rightattnos[i] > 0)
4083  {
4084  expand_colnames_array_to(rightcolinfo, rightattnos[i]);
4085  rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4086  }
4087  }
4088  }
4089 
4090  /*
4091  * If there's a USING clause, select the USING column names and push
4092  * those names down to the children. We have two strategies:
4093  *
4094  * If dpns->unique_using is true, we force all USING names to be
4095  * unique across the whole query level. In principle we'd only need
4096  * the names of dangerous USING columns to be globally unique, but to
4097  * safely assign all USING names in a single pass, we have to enforce
4098  * the same uniqueness rule for all of them. However, if a USING
4099  * column's name has been pushed down from the parent, we should use
4100  * it as-is rather than making a uniqueness adjustment. This is
4101  * necessary when we're at an unnamed join, and it creates no risk of
4102  * ambiguity. Also, if there's a user-written output alias for a
4103  * merged column, we prefer to use that rather than the input name;
4104  * this simplifies the logic and seems likely to lead to less aliasing
4105  * overall.
4106  *
4107  * If dpns->unique_using is false, we only need USING names to be
4108  * unique within their own join RTE. We still need to honor
4109  * pushed-down names, though.
4110  *
4111  * Though significantly different in results, these two strategies are
4112  * implemented by the same code, with only the difference of whether
4113  * to put assigned names into dpns->using_names.
4114  */
4115  if (j->usingClause)
4116  {
4117  /* Copy the input parentUsing list so we don't modify it */
4118  parentUsing = list_copy(parentUsing);
4119 
4120  /* USING names must correspond to the first join output columns */
4122  i = 0;
4123  foreach(lc, j->usingClause)
4124  {
4125  char *colname = strVal(lfirst(lc));
4126 
4127  /* Assert it's a merged column */
4128  Assert(leftattnos[i] != 0 && rightattnos[i] != 0);
4129 
4130  /* Adopt passed-down name if any, else select unique name */
4131  if (colinfo->colnames[i] != NULL)
4132  colname = colinfo->colnames[i];
4133  else
4134  {
4135  /* Prefer user-written output alias if any */
4136  if (rte->alias && i < list_length(rte->alias->colnames))
4137  colname = strVal(list_nth(rte->alias->colnames, i));
4138  /* Make it appropriately unique */
4139  colname = make_colname_unique(colname, dpns, colinfo);
4140  if (dpns->unique_using)
4141  dpns->using_names = lappend(dpns->using_names,
4142  colname);
4143  /* Save it as output column name, too */
4144  colinfo->colnames[i] = colname;
4145  }
4146 
4147  /* Remember selected names for use later */
4148  colinfo->usingNames = lappend(colinfo->usingNames, colname);
4149  parentUsing = lappend(parentUsing, colname);
4150 
4151  /* Push down to left column, unless it's a system column */
4152  if (leftattnos[i] > 0)
4153  {
4154  expand_colnames_array_to(leftcolinfo, leftattnos[i]);
4155  leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4156  }
4157 
4158  /* Same on the righthand side */
4159  if (rightattnos[i] > 0)
4160  {
4161  expand_colnames_array_to(rightcolinfo, rightattnos[i]);
4162  rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4163  }
4164 
4165  i++;
4166  }
4167  }
4168 
4169  /* Mark child deparse_columns structs with correct parentUsing info */
4170  leftcolinfo->parentUsing = parentUsing;
4171  rightcolinfo->parentUsing = parentUsing;
4172 
4173  /* Now recursively assign USING column names in children */
4174  set_using_names(dpns, j->larg, parentUsing);
4175  set_using_names(dpns, j->rarg, parentUsing);
4176  }
4177  else
4178  elog(ERROR, "unrecognized node type: %d",
4179  (int) nodeTag(jtnode));
4180 }
4181 
4182 /*
4183  * set_relation_column_names: select column aliases for a non-join RTE
4184  *
4185  * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
4186  * If any colnames entries are already filled in, those override local
4187  * choices.
4188  */
4189 static void
4191  deparse_columns *colinfo)
4192 {
4193  int ncolumns;
4194  char **real_colnames;
4195  bool changed_any;
4196  int noldcolumns;
4197  int i;
4198  int j;
4199 
4200  /*
4201  * Extract the RTE's "real" column names. This is comparable to
4202  * get_rte_attribute_name, except that it's important to disregard dropped
4203  * columns. We put NULL into the array for a dropped column.
4204  */
4205  if (rte->rtekind == RTE_RELATION)
4206  {
4207  /* Relation --- look to the system catalogs for up-to-date info */
4208  Relation rel;
4209  TupleDesc tupdesc;
4210 
4211  rel = relation_open(rte->relid, AccessShareLock);
4212  tupdesc = RelationGetDescr(rel);
4213 
4214  ncolumns = tupdesc->natts;
4215  real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4216 
4217  for (i = 0; i < ncolumns; i++)
4218  {
4219  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4220 
4221  if (attr->attisdropped)
4222  real_colnames[i] = NULL;
4223  else
4224  real_colnames[i] = pstrdup(NameStr(attr->attname));
4225  }
4227  }
4228  else
4229  {
4230  /* Otherwise use the column names from eref */
4231  ListCell *lc;
4232 
4233  ncolumns = list_length(rte->eref->colnames);
4234  real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4235 
4236  i = 0;
4237  foreach(lc, rte->eref->colnames)
4238  {
4239  /*
4240  * If the column name shown in eref is an empty string, then it's
4241  * a column that was dropped at the time of parsing the query, so
4242  * treat it as dropped.
4243  */
4244  char *cname = strVal(lfirst(lc));
4245 
4246  if (cname[0] == '\0')
4247  cname = NULL;
4248  real_colnames[i] = cname;
4249  i++;
4250  }
4251  }
4252 
4253  /*
4254  * Ensure colinfo->colnames has a slot for each column. (It could be long
4255  * enough already, if we pushed down a name for the last column.) Note:
4256  * it's possible that there are now more columns than there were when the
4257  * query was parsed, ie colnames could be longer than rte->eref->colnames.
4258  * We must assign unique aliases to the new columns too, else there could
4259  * be unresolved conflicts when the view/rule is reloaded.
4260  */
4261  expand_colnames_array_to(colinfo, ncolumns);
4262  Assert(colinfo->num_cols == ncolumns);
4263 
4264  /*
4265  * Make sufficiently large new_colnames and is_new_col arrays, too.
4266  *
4267  * Note: because we leave colinfo->num_new_cols zero until after the loop,
4268  * colname_is_unique will not consult that array, which is fine because it
4269  * would only be duplicate effort.
4270  */
4271  colinfo->new_colnames = (char **) palloc(ncolumns * sizeof(char *));
4272  colinfo->is_new_col = (bool *) palloc(ncolumns * sizeof(bool));
4273 
4274  /*
4275  * Scan the columns, select a unique alias for each one, and store it in
4276  * colinfo->colnames and colinfo->new_colnames. The former array has NULL
4277  * entries for dropped columns, the latter omits them. Also mark
4278  * new_colnames entries as to whether they are new since parse time; this
4279  * is the case for entries beyond the length of rte->eref->colnames.
4280  */
4281  noldcolumns = list_length(rte->eref->colnames);
4282  changed_any = false;
4283  j = 0;
4284  for (i = 0; i < ncolumns; i++)
4285  {
4286  char *real_colname = real_colnames[i];
4287  char *colname = colinfo->colnames[i];
4288 
4289  /* Skip dropped columns */
4290  if (real_colname == NULL)
4291  {
4292  Assert(colname == NULL); /* colnames[i] is already NULL */
4293  continue;
4294  }
4295 
4296  /* If alias already assigned, that's what to use */
4297  if (colname == NULL)
4298  {
4299  /* If user wrote an alias, prefer that over real column name */
4300  if (rte->alias && i < list_length(rte->alias->colnames))
4301  colname = strVal(list_nth(rte->alias->colnames, i));
4302  else
4303  colname = real_colname;
4304 
4305  /* Unique-ify and insert into colinfo */
4306  colname = make_colname_unique(colname, dpns, colinfo);
4307 
4308  colinfo->colnames[i] = colname;
4309  }
4310 
4311  /* Put names of non-dropped columns in new_colnames[] too */
4312  colinfo->new_colnames[j] = colname;
4313  /* And mark them as new or not */
4314  colinfo->is_new_col[j] = (i >= noldcolumns);
4315  j++;
4316 
4317  /* Remember if any assigned aliases differ from "real" name */
4318  if (!changed_any && strcmp(colname, real_colname) != 0)
4319  changed_any = true;
4320  }
4321 
4322  /*
4323  * Set correct length for new_colnames[] array. (Note: if columns have
4324  * been added, colinfo->num_cols includes them, which is not really quite
4325  * right but is harmless, since any new columns must be at the end where
4326  * they won't affect varattnos of pre-existing columns.)
4327  */
4328  colinfo->num_new_cols = j;
4329 
4330  /*
4331  * For a relation RTE, we need only print the alias column names if any
4332  * are different from the underlying "real" names. For a function RTE,
4333  * always emit a complete column alias list; this is to protect against
4334  * possible instability of the default column names (eg, from altering
4335  * parameter names). For tablefunc RTEs, we never print aliases, because
4336  * the column names are part of the clause itself. For other RTE types,
4337  * print if we changed anything OR if there were user-written column
4338  * aliases (since the latter would be part of the underlying "reality").
4339  */
4340  if (rte->rtekind == RTE_RELATION)
4341  colinfo->printaliases = changed_any;
4342  else if (rte->rtekind == RTE_FUNCTION)
4343  colinfo->printaliases = true;
4344  else if (rte->rtekind == RTE_TABLEFUNC)
4345  colinfo->printaliases = false;
4346  else if (rte->alias && rte->alias->colnames != NIL)
4347  colinfo->printaliases = true;
4348  else
4349  colinfo->printaliases = changed_any;
4350 }
4351 
4352 /*
4353  * set_join_column_names: select column aliases for a join RTE
4354  *
4355  * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
4356  * If any colnames entries are already filled in, those override local
4357  * choices. Also, names for USING columns were already chosen by
4358  * set_using_names(). We further expect that column alias selection has been
4359  * completed for both input RTEs.
4360  */
4361 static void
4363  deparse_columns *colinfo)
4364 {
4365  deparse_columns *leftcolinfo;
4366  deparse_columns *rightcolinfo;
4367  bool changed_any;
4368  int noldcolumns;
4369  int nnewcolumns;
4370  Bitmapset *leftmerged = NULL;
4371  Bitmapset *rightmerged = NULL;
4372  int i;
4373  int j;
4374  int ic;
4375  int jc;
4376 
4377  /* Look up the previously-filled-in child deparse_columns structs */
4378  leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
4379  rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
4380 
4381  /*
4382  * Ensure colinfo->colnames has a slot for each column. (It could be long
4383  * enough already, if we pushed down a name for the last column.) Note:
4384  * it's possible that one or both inputs now have more columns than there
4385  * were when the query was parsed, but we'll deal with that below. We
4386  * only need entries in colnames for pre-existing columns.
4387  */
4388  noldcolumns = list_length(rte->eref->colnames);
4389  expand_colnames_array_to(colinfo, noldcolumns);
4390  Assert(colinfo->num_cols == noldcolumns);
4391 
4392  /*
4393  * Scan the join output columns, select an alias for each one, and store
4394  * it in colinfo->colnames. If there are USING columns, set_using_names()
4395  * already selected their names, so we can start the loop at the first
4396  * non-merged column.
4397  */
4398  changed_any = false;
4399  for (i = list_length(colinfo->usingNames); i < noldcolumns; i++)
4400  {
4401  char *colname = colinfo->colnames[i];
4402  char *real_colname;
4403 
4404  /* Join column must refer to at least one input column */
4405  Assert(colinfo->leftattnos[i] != 0 || colinfo->rightattnos[i] != 0);
4406 
4407  /* Get the child column name */
4408  if (colinfo->leftattnos[i] > 0)
4409  real_colname = leftcolinfo->colnames[colinfo->leftattnos[i] - 1];
4410  else if (colinfo->rightattnos[i] > 0)
4411  real_colname = rightcolinfo->colnames[colinfo->rightattnos[i] - 1];
4412  else
4413  {
4414  /* We're joining system columns --- use eref name */
4415  real_colname = strVal(list_nth(rte->eref->colnames, i));
4416  }
4417 
4418  /* If child col has been dropped, no need to assign a join colname */
4419  if (real_colname == NULL)
4420  {
4421  colinfo->colnames[i] = NULL;
4422  continue;
4423  }
4424 
4425  /* In an unnamed join, just report child column names as-is */
4426  if (rte->alias == NULL)
4427  {
4428  colinfo->colnames[i] = real_colname;
4429  continue;
4430  }
4431 
4432  /* If alias already assigned, that's what to use */
4433  if (colname == NULL)
4434  {
4435  /* If user wrote an alias, prefer that over real column name */
4436  if (rte->alias && i < list_length(rte->alias->colnames))
4437  colname = strVal(list_nth(rte->alias->colnames, i));
4438  else
4439  colname = real_colname;
4440 
4441  /* Unique-ify and insert into colinfo */
4442  colname = make_colname_unique(colname, dpns, colinfo);
4443 
4444  colinfo->colnames[i] = colname;
4445  }
4446 
4447  /* Remember if any assigned aliases differ from "real" name */
4448  if (!changed_any && strcmp(colname, real_colname) != 0)
4449  changed_any = true;
4450  }
4451 
4452  /*
4453  * Calculate number of columns the join would have if it were re-parsed
4454  * now, and create storage for the new_colnames and is_new_col arrays.
4455  *
4456  * Note: colname_is_unique will be consulting new_colnames[] during the
4457  * loops below, so its not-yet-filled entries must be zeroes.
4458  */
4459  nnewcolumns = leftcolinfo->num_new_cols + rightcolinfo->num_new_cols -
4460  list_length(colinfo->usingNames);
4461  colinfo->num_new_cols = nnewcolumns;
4462  colinfo->new_colnames = (char **) palloc0(nnewcolumns * sizeof(char *));
4463  colinfo->is_new_col = (bool *) palloc0(nnewcolumns * sizeof(bool));
4464 
4465  /*
4466  * Generating the new_colnames array is a bit tricky since any new columns
4467  * added since parse time must be inserted in the right places. This code
4468  * must match the parser, which will order a join's columns as merged
4469  * columns first (in USING-clause order), then non-merged columns from the
4470  * left input (in attnum order), then non-merged columns from the right
4471  * input (ditto). If one of the inputs is itself a join, its columns will
4472  * be ordered according to the same rule, which means newly-added columns
4473  * might not be at the end. We can figure out what's what by consulting
4474  * the leftattnos and rightattnos arrays plus the input is_new_col arrays.
4475  *
4476  * In these loops, i indexes leftattnos/rightattnos (so it's join varattno
4477  * less one), j indexes new_colnames/is_new_col, and ic/jc have similar
4478  * meanings for the current child RTE.
4479  */
4480 
4481  /* Handle merged columns; they are first and can't be new */
4482  i = j = 0;
4483  while (i < noldcolumns &&
4484  colinfo->leftattnos[i] != 0 &&
4485  colinfo->rightattnos[i] != 0)
4486  {
4487  /* column name is already determined and known unique */
4488  colinfo->new_colnames[j] = colinfo->colnames[i];
4489  colinfo->is_new_col[j] = false;
4490 
4491  /* build bitmapsets of child attnums of merged columns */
4492  if (colinfo->leftattnos[i] > 0)
4493  leftmerged = bms_add_member(leftmerged, colinfo->leftattnos[i]);
4494  if (colinfo->rightattnos[i] > 0)
4495  rightmerged = bms_add_member(rightmerged, colinfo->rightattnos[i]);
4496 
4497  i++, j++;
4498  }
4499 
4500  /* Handle non-merged left-child columns */
4501  ic = 0;
4502  for (jc = 0; jc < leftcolinfo->num_new_cols; jc++)
4503  {
4504  char *child_colname = leftcolinfo->new_colnames[jc];
4505 
4506  if (!leftcolinfo->is_new_col[jc])
4507  {
4508  /* Advance ic to next non-dropped old column of left child */
4509  while (ic < leftcolinfo->num_cols &&
4510  leftcolinfo->colnames[ic] == NULL)
4511  ic++;
4512  Assert(ic < leftcolinfo->num_cols);
4513  ic++;
4514  /* If it is a merged column, we already processed it */
4515  if (bms_is_member(ic, leftmerged))
4516  continue;
4517  /* Else, advance i to the corresponding existing join column */
4518  while (i < colinfo->num_cols &&
4519  colinfo->colnames[i] == NULL)
4520  i++;
4521  Assert(i < colinfo->num_cols);
4522  Assert(ic == colinfo->leftattnos[i]);
4523  /* Use the already-assigned name of this column */
4524  colinfo->new_colnames[j] = colinfo->colnames[i];
4525  i++;
4526  }
4527  else
4528  {
4529  /*
4530  * Unique-ify the new child column name and assign, unless we're
4531  * in an unnamed join, in which case just copy
4532  */
4533  if (rte->alias != NULL)
4534  {
4535  colinfo->new_colnames[j] =
4536  make_colname_unique(child_colname, dpns, colinfo);
4537  if (!changed_any &&
4538  strcmp(colinfo->new_colnames[j], child_colname) != 0)
4539  changed_any = true;
4540  }
4541  else
4542  colinfo->new_colnames[j] = child_colname;
4543  }
4544 
4545  colinfo->is_new_col[j] = leftcolinfo->is_new_col[jc];
4546  j++;
4547  }
4548 
4549  /* Handle non-merged right-child columns in exactly the same way */
4550  ic = 0;
4551  for (jc = 0; jc < rightcolinfo->num_new_cols; jc++)
4552  {
4553  char *child_colname = rightcolinfo->new_colnames[jc];
4554 
4555  if (!rightcolinfo->is_new_col[jc])
4556  {
4557  /* Advance ic to next non-dropped old column of right child */
4558  while (ic < rightcolinfo->num_cols &&
4559  rightcolinfo->colnames[ic] == NULL)
4560  ic++;
4561  Assert(ic < rightcolinfo->num_cols);
4562  ic++;
4563  /* If it is a merged column, we already processed it */
4564  if (bms_is_member(ic, rightmerged))
4565  continue;
4566  /* Else, advance i to the corresponding existing join column */
4567  while (i < colinfo->num_cols &&
4568  colinfo->colnames[i] == NULL)
4569  i++;
4570  Assert(i < colinfo->num_cols);
4571  Assert(ic == colinfo->rightattnos[i]);
4572  /* Use the already-assigned name of this column */
4573  colinfo->new_colnames[j] = colinfo->colnames[i];
4574  i++;
4575  }
4576  else
4577  {
4578  /*
4579  * Unique-ify the new child column name and assign, unless we're
4580  * in an unnamed join, in which case just copy
4581  */
4582  if (rte->alias != NULL)
4583  {
4584  colinfo->new_colnames[j] =
4585  make_colname_unique(child_colname, dpns, colinfo);
4586  if (!changed_any &&
4587  strcmp(colinfo->new_colnames[j], child_colname) != 0)
4588  changed_any = true;
4589  }
4590  else
4591  colinfo->new_colnames[j] = child_colname;
4592  }
4593 
4594  colinfo->is_new_col[j] = rightcolinfo->is_new_col[jc];
4595  j++;
4596  }
4597 
4598  /* Assert we processed the right number of columns */
4599 #ifdef USE_ASSERT_CHECKING
4600  while (i < colinfo->num_cols && colinfo->colnames[i] == NULL)
4601  i++;
4602  Assert(i == colinfo->num_cols);
4603  Assert(j == nnewcolumns);
4604 #endif
4605 
4606  /*
4607  * For a named join, print column aliases if we changed any from the child
4608  * names. Unnamed joins cannot print aliases.
4609  */
4610  if (rte->alias != NULL)
4611  colinfo->printaliases = changed_any;
4612  else
4613  colinfo->printaliases = false;
4614 }
4615 
4616 /*
4617  * colname_is_unique: is colname distinct from already-chosen column names?
4618  *
4619  * dpns is query-wide info, colinfo is for the column's RTE
4620  */
4621 static bool
4622 colname_is_unique(const char *colname, deparse_namespace *dpns,
4623  deparse_columns *colinfo)
4624 {
4625  int i;
4626  ListCell *lc;
4627 
4628  /* Check against already-assigned column aliases within RTE */
4629  for (i = 0; i < colinfo->num_cols; i++)
4630  {
4631  char *oldname = colinfo->colnames[i];
4632 
4633  if (oldname && strcmp(oldname, colname) == 0)
4634  return false;
4635  }
4636 
4637  /*
4638  * If we're building a new_colnames array, check that too (this will be
4639  * partially but not completely redundant with the previous checks)
4640  */
4641  for (i = 0; i < colinfo->num_new_cols; i++)
4642  {
4643  char *oldname = colinfo->new_colnames[i];
4644 
4645  if (oldname && strcmp(oldname, colname) == 0)
4646  return false;
4647  }
4648 
4649  /* Also check against USING-column names that must be globally unique */
4650  foreach(lc, dpns->using_names)
4651  {
4652  char *oldname = (char *) lfirst(lc);
4653 
4654  if (strcmp(oldname, colname) == 0)
4655  return false;
4656  }
4657 
4658  /* Also check against names already assigned for parent-join USING cols */
4659  foreach(lc, colinfo->parentUsing)
4660  {
4661  char *oldname = (char *) lfirst(lc);
4662 
4663  if (strcmp(oldname, colname) == 0)
4664  return false;
4665  }
4666 
4667  return true;
4668 }
4669 
4670 /*
4671  * make_colname_unique: modify colname if necessary to make it unique
4672  *
4673  * dpns is query-wide info, colinfo is for the column's RTE
4674  */
4675 static char *
4677  deparse_columns *colinfo)
4678 {
4679  /*
4680  * If the selected name isn't unique, append digits to make it so. For a
4681  * very long input name, we might have to truncate to stay within
4682  * NAMEDATALEN.
4683  */
4684  if (!colname_is_unique(colname, dpns, colinfo))
4685  {
4686  int colnamelen = strlen(colname);
4687  char *modname = (char *) palloc(colnamelen + 16);
4688  int i = 0;
4689 
4690  do
4691  {
4692  i++;
4693  for (;;)
4694  {
4695  memcpy(modname, colname, colnamelen);
4696  sprintf(modname + colnamelen, "_%d", i);
4697  if (strlen(modname) < NAMEDATALEN)
4698  break;
4699  /* drop chars from colname to keep all the digits */
4700  colnamelen = pg_mbcliplen(colname, colnamelen,
4701  colnamelen - 1);
4702  }
4703  } while (!colname_is_unique(modname, dpns, colinfo));
4704  colname = modname;
4705  }
4706  return colname;
4707 }
4708 
4709 /*
4710  * expand_colnames_array_to: make colinfo->colnames at least n items long
4711  *
4712  * Any added array entries are initialized to zero.
4713  */
4714 static void
4716 {
4717  if (n > colinfo->num_cols)
4718  {
4719  if (colinfo->colnames == NULL)
4720  colinfo->colnames = (char **) palloc0(n * sizeof(char *));
4721  else
4722  {
4723  colinfo->colnames = (char **) repalloc(colinfo->colnames,
4724  n * sizeof(char *));
4725  memset(colinfo->colnames + colinfo->num_cols, 0,
4726  (n - colinfo->num_cols) * sizeof(char *));
4727  }
4728  colinfo->num_cols = n;
4729  }
4730 }
4731 
4732 /*
4733  * identify_join_columns: figure out where columns of a join come from
4734  *
4735  * Fills the join-specific fields of the colinfo struct, except for
4736  * usingNames which is filled later.
4737  */
4738 static void
4740  deparse_columns *colinfo)
4741 {
4742  int numjoincols;
4743  int jcolno;
4744  int rcolno;
4745  ListCell *lc;
4746 
4747  /* Extract left/right child RT indexes */
4748  if (IsA(j->larg, RangeTblRef))
4749  colinfo->leftrti = ((RangeTblRef *) j->larg)->rtindex;
4750  else if (IsA(j->larg, JoinExpr))
4751  colinfo->leftrti = ((JoinExpr *) j->larg)->rtindex;
4752  else
4753  elog(ERROR, "unrecognized node type in jointree: %d",
4754  (int) nodeTag(j->larg));
4755  if (IsA(j->rarg, RangeTblRef))
4756  colinfo->rightrti = ((RangeTblRef *) j->rarg)->rtindex;
4757  else if (IsA(j->rarg, JoinExpr))
4758  colinfo->rightrti = ((JoinExpr *) j->rarg)->rtindex;
4759  else
4760  elog(ERROR, "unrecognized node type in jointree: %d",
4761  (int) nodeTag(j->rarg));
4762 
4763  /* Assert children will be processed earlier than join in second pass */
4764  Assert(colinfo->leftrti < j->rtindex);
4765  Assert(colinfo->rightrti < j->rtindex);
4766 
4767  /* Initialize result arrays with zeroes */
4768  numjoincols = list_length(jrte->joinaliasvars);
4769  Assert(numjoincols == list_length(jrte->eref->colnames));
4770  colinfo->leftattnos = (int *) palloc0(numjoincols * sizeof(int));
4771  colinfo->rightattnos = (int *) palloc0(numjoincols * sizeof(int));
4772 
4773  /*
4774  * Deconstruct RTE's joinleftcols/joinrightcols into desired format.
4775  * Recall that the column(s) merged due to USING are the first column(s)
4776  * of the join output. We need not do anything special while scanning
4777  * joinleftcols, but while scanning joinrightcols we must distinguish
4778  * merged from unmerged columns.
4779  */
4780  jcolno = 0;
4781  foreach(lc, jrte->joinleftcols)
4782  {
4783  int leftattno = lfirst_int(lc);
4784 
4785  colinfo->leftattnos[jcolno++] = leftattno;
4786  }
4787  rcolno = 0;
4788  foreach(lc, jrte->joinrightcols)
4789  {
4790  int rightattno = lfirst_int(lc);
4791 
4792  if (rcolno < jrte->joinmergedcols) /* merged column? */
4793  colinfo->rightattnos[rcolno] = rightattno;
4794  else
4795  colinfo->rightattnos[jcolno++] = rightattno;
4796  rcolno++;
4797  }
4798  Assert(jcolno == numjoincols);
4799 }
4800 
4801 /*
4802  * get_rtable_name: convenience function to get a previously assigned RTE alias
4803  *
4804  * The RTE must belong to the topmost namespace level in "context".
4805  */
4806 static char *
4807 get_rtable_name(int rtindex, deparse_context *context)
4808 {
4810 
4811  Assert(rtindex > 0 && rtindex <= list_length(dpns->rtable_names));
4812  return (char *) list_nth(dpns->rtable_names, rtindex - 1);
4813 }
4814 
4815 /*
4816  * set_deparse_plan: set up deparse_namespace to parse subexpressions
4817  * of a given Plan node
4818  *
4819  * This sets the plan, outer_plan, inner_plan, outer_tlist, inner_tlist,
4820  * and index_tlist fields. Caller is responsible for adjusting the ancestors
4821  * list if necessary. Note that the rtable, subplans, and ctes fields do
4822  * not need to change when shifting attention to different plan nodes in a
4823  * single plan tree.
4824  */
4825 static void
4827 {
4828  dpns->plan = plan;
4829 
4830  /*
4831  * We special-case Append and MergeAppend to pretend that the first child
4832  * plan is the OUTER referent; we have to interpret OUTER Vars in their
4833  * tlists according to one of the children, and the first one is the most
4834  * natural choice.
4835  */
4836  if (IsA(plan, Append))
4837  dpns->outer_plan = linitial(((Append *) plan)->appendplans);
4838  else if (IsA(plan, MergeAppend))
4839  dpns->outer_plan = linitial(((MergeAppend *) plan)->mergeplans);
4840  else
4841  dpns->outer_plan = outerPlan(plan);
4842 
4843  if (dpns->outer_plan)
4844  dpns->outer_tlist = dpns->outer_plan->targetlist;
4845  else
4846  dpns->outer_tlist = NIL;
4847 
4848  /*
4849  * For a SubqueryScan, pretend the subplan is INNER referent. (We don't
4850  * use OUTER because that could someday conflict with the normal meaning.)
4851  * Likewise, for a CteScan, pretend the subquery's plan is INNER referent.
4852  * For ON CONFLICT .. UPDATE we just need the inner tlist to point to the
4853  * excluded expression's tlist. (Similar to the SubqueryScan we don't want
4854  * to reuse OUTER, it's used for RETURNING in some modify table cases,
4855  * although not INSERT .. CONFLICT).
4856  */
4857  if (IsA(plan, SubqueryScan))
4858  dpns->inner_plan = ((SubqueryScan *) plan)->subplan;
4859  else if (IsA(plan, CteScan))
4860  dpns->inner_plan = list_nth(dpns->subplans,
4861  ((CteScan *) plan)->ctePlanId - 1);
4862  else if (IsA(plan, ModifyTable))
4863  dpns->inner_plan = plan;
4864  else
4865  dpns->inner_plan = innerPlan(plan);
4866 
4867  if (IsA(plan, ModifyTable))
4868  dpns->inner_tlist = ((ModifyTable *) plan)->exclRelTlist;
4869  else if (dpns->inner_plan)
4870  dpns->inner_tlist = dpns->inner_plan->targetlist;
4871  else
4872  dpns->inner_tlist = NIL;
4873 
4874  /* Set up referent for INDEX_VAR Vars, if needed */
4875  if (IsA(plan, IndexOnlyScan))
4876  dpns->index_tlist = ((IndexOnlyScan *) plan)->indextlist;
4877  else if (IsA(plan, ForeignScan))
4878  dpns->index_tlist = ((ForeignScan *) plan)->fdw_scan_tlist;
4879  else if (IsA(plan, CustomScan))
4880  dpns->index_tlist = ((CustomScan *) plan)->custom_scan_tlist;
4881  else
4882  dpns->index_tlist = NIL;
4883 }
4884 
4885 /*
4886  * push_child_plan: temporarily transfer deparsing attention to a child plan
4887  *
4888  * When expanding an OUTER_VAR or INNER_VAR reference, we must adjust the
4889  * deparse context in case the referenced expression itself uses
4890  * OUTER_VAR/INNER_VAR. We modify the top stack entry in-place to avoid
4891  * affecting levelsup issues (although in a Plan tree there really shouldn't
4892  * be any).
4893  *
4894  * Caller must provide a local deparse_namespace variable to save the
4895  * previous state for pop_child_plan.
4896  */
4897 static void
4899  deparse_namespace *save_dpns)
4900 {
4901  /* Save state for restoration later */
4902  *save_dpns = *dpns;
4903 
4904  /* Link current plan node into ancestors list */
4905  dpns->ancestors = lcons(dpns->plan, dpns->ancestors);
4906 
4907  /* Set attention on selected child */
4908  set_deparse_plan(dpns, plan);
4909 }
4910 
4911 /*
4912  * pop_child_plan: undo the effects of push_child_plan
4913  */
4914 static void
4916 {
4917  List *ancestors;
4918 
4919  /* Get rid of ancestors list cell added by push_child_plan */
4920  ancestors = list_delete_first(dpns->ancestors);
4921 
4922  /* Restore fields changed by push_child_plan */
4923  *dpns = *save_dpns;
4924 
4925  /* Make sure dpns->ancestors is right (may be unnecessary) */
4926  dpns->ancestors = ancestors;
4927 }
4928 
4929 /*
4930  * push_ancestor_plan: temporarily transfer deparsing attention to an
4931  * ancestor plan
4932  *
4933  * When expanding a Param reference, we must adjust the deparse context
4934  * to match the plan node that contains the expression being printed;
4935  * otherwise we'd fail if that expression itself contains a Param or
4936  * OUTER_VAR/INNER_VAR/INDEX_VAR variable.
4937  *
4938  * The target ancestor is conveniently identified by the ListCell holding it
4939  * in dpns->ancestors.
4940  *
4941  * Caller must provide a local deparse_namespace variable to save the
4942  * previous state for pop_ancestor_plan.
4943  */
4944 static void
4946  deparse_namespace *save_dpns)
4947 {
4948  Plan *plan = (Plan *) lfirst(ancestor_cell);
4949 
4950  /* Save state for restoration later */
4951  *save_dpns = *dpns;
4952 
4953  /* Build a new ancestor list with just this node's ancestors */
4954  dpns->ancestors =
4955  list_copy_tail(dpns->ancestors,
4956  list_cell_number(dpns->ancestors, ancestor_cell) + 1);
4957 
4958  /* Set attention on selected ancestor */
4959  set_deparse_plan(dpns, plan);
4960 }
4961 
4962 /*
4963  * pop_ancestor_plan: undo the effects of push_ancestor_plan
4964  */
4965 static void
4967 {
4968  /* Free the ancestor list made in push_ancestor_plan */
4969  list_free(dpns->ancestors);
4970 
4971  /* Restore fields changed by push_ancestor_plan */
4972  *dpns = *save_dpns;
4973 }
4974 
4975 
4976 /* ----------
4977  * make_ruledef - reconstruct the CREATE RULE command
4978  * for a given pg_rewrite tuple
4979  * ----------
4980  */
4981 static void
4983  int prettyFlags)
4984 {
4985  char *rulename;
4986  char ev_type;
4987  Oid ev_class;
4988  bool is_instead;
4989  char *ev_qual;
4990  char *ev_action;
4991  List *actions;
4992  Relation ev_relation;
4993  TupleDesc viewResultDesc = NULL;
4994  int fno;
4995  Datum dat;
4996  bool isnull;
4997 
4998  /*
4999  * Get the attribute values from the rules tuple
5000  */
5001  fno = SPI_fnumber(rulettc, "rulename");
5002  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5003  Assert(!isnull);
5004  rulename = NameStr(*(DatumGetName(dat)));
5005 
5006  fno = SPI_fnumber(rulettc, "ev_type");
5007  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5008  Assert(!isnull);
5009  ev_type = DatumGetChar(dat);
5010 
5011  fno = SPI_fnumber(rulettc, "ev_class");
5012  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5013  Assert(!isnull);
5014  ev_class = DatumGetObjectId(dat);
5015 
5016  fno = SPI_fnumber(rulettc, "is_instead");
5017  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5018  Assert(!isnull);
5019  is_instead = DatumGetBool(dat);
5020 
5021  fno = SPI_fnumber(rulettc, "ev_qual");
5022  ev_qual = SPI_getvalue(ruletup, rulettc, fno);
5023  Assert(ev_qual != NULL);
5024 
5025  fno = SPI_fnumber(rulettc, "ev_action");
5026  ev_action = SPI_getvalue(ruletup, rulettc, fno);
5027  Assert(ev_action != NULL);
5028  actions = (List *) stringToNode(ev_action);
5029  if (actions == NIL)
5030  elog(ERROR, "invalid empty ev_action list");
5031 
5032  ev_relation = table_open(ev_class, AccessShareLock);
5033 
5034  /*
5035  * Build the rules definition text
5036  */
5037  appendStringInfo(buf, "CREATE RULE %s AS",
5038  quote_identifier(rulename));
5039 
5040  if (prettyFlags & PRETTYFLAG_INDENT)
5041  appendStringInfoString(buf, "\n ON ");
5042  else
5043  appendStringInfoString(buf, " ON ");
5044 
5045  /* The event the rule is fired for */
5046  switch (ev_type)
5047  {
5048  case '1':
5049  appendStringInfoString(buf, "SELECT");
5050  viewResultDesc = RelationGetDescr(ev_relation);
5051  break;
5052 
5053  case '2':
5054  appendStringInfoString(buf, "UPDATE");
5055  break;
5056 
5057  case '3':
5058  appendStringInfoString(buf, "INSERT");
5059  break;
5060 
5061  case '4':
5062  appendStringInfoString(buf, "DELETE");
5063  break;
5064 
5065  default:
5066  ereport(ERROR,
5067  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5068  errmsg("rule \"%s\" has unsupported event type %d",
5069  rulename, ev_type)));
5070  break;
5071  }
5072 
5073  /* The relation the rule is fired on */
5074  appendStringInfo(buf, " TO %s",
5075  (prettyFlags & PRETTYFLAG_SCHEMA) ?
5076  generate_relation_name(ev_class, NIL) :
5078 
5079  /* If the rule has an event qualification, add it */
5080  if (strcmp(ev_qual, "<>") != 0)
5081  {
5082  Node *qual;
5083  Query *query;
5084  deparse_context context;
5085  deparse_namespace dpns;
5086 
5087  if (prettyFlags & PRETTYFLAG_INDENT)
5088  appendStringInfoString(buf, "\n ");
5089  appendStringInfoString(buf, " WHERE ");
5090 
5091  qual = stringToNode(ev_qual);
5092 
5093  /*
5094  * We need to make a context for recognizing any Vars in the qual
5095  * (which can only be references to OLD and NEW). Use the rtable of
5096  * the first query in the action list for this purpose.
5097  */
5098  query = (Query *) linitial(actions);
5099 
5100  /*
5101  * If the action is INSERT...SELECT, OLD/NEW have been pushed down
5102  * into the SELECT, and that's what we need to look at. (Ugly kluge
5103  * ... try to fix this when we redesign querytrees.)
5104  */
5105  query = getInsertSelectQuery(query, NULL);
5106 
5107  /* Must acquire locks right away; see notes in get_query_def() */
5108  AcquireRewriteLocks(query, false, false);
5109 
5110  context.buf = buf;
5111  context.namespaces = list_make1(&dpns);
5112  context.windowClause = NIL;
5113  context.windowTList = NIL;
5114  context.varprefix = (list_length(query->rtable) != 1);
5115  context.prettyFlags = prettyFlags;
5116  context.wrapColumn = WRAP_COLUMN_DEFAULT;
5117  context.indentLevel = PRETTYINDENT_STD;
5118  context.special_exprkind = EXPR_KIND_NONE;
5119  context.appendparents = NULL;
5120 
5121  set_deparse_for_query(&dpns, query, NIL);
5122 
5123  get_rule_expr(qual, &context, false);
5124  }
5125 
5126  appendStringInfoString(buf, " DO ");
5127 
5128  /* The INSTEAD keyword (if so) */
5129  if (is_instead)
5130  appendStringInfoString(buf, "INSTEAD ");
5131 
5132  /* Finally the rules actions */
5133  if (list_length(actions) > 1)
5134  {
5135  ListCell *action;
5136  Query *query;
5137 
5138  appendStringInfoChar(buf, '(');
5139  foreach(action, actions)
5140  {
5141  query = (Query *) lfirst(action);
5142  get_query_def(query, buf, NIL, viewResultDesc,
5143  prettyFlags, WRAP_COLUMN_DEFAULT, 0);
5144  if (prettyFlags)
5145  appendStringInfoString(buf, ";\n");
5146  else
5147  appendStringInfoString(buf, "; ");
5148  }
5149  appendStringInfoString(buf, ");");
5150  }
5151  else
5152  {
5153  Query *query;
5154 
5155  query = (Query *) linitial(actions);
5156  get_query_def(query, buf, NIL, viewResultDesc,
5157  prettyFlags, WRAP_COLUMN_DEFAULT, 0);
5158  appendStringInfoChar(buf, ';');
5159  }
5160 
5161  table_close(ev_relation, AccessShareLock);
5162 }
5163 
5164 
5165 /* ----------
5166  * make_viewdef - reconstruct the SELECT part of a
5167  * view rewrite rule
5168  * ----------
5169  */
5170 static void
5172  int prettyFlags, int wrapColumn)
5173 {
5174  Query *query;
5175  char ev_type;
5176  Oid ev_class;
5177  bool is_instead;
5178  char *ev_qual;
5179  char *ev_action;
5180  List *actions;
5181  Relation ev_relation;
5182  int fno;
5183  Datum dat;
5184  bool isnull;
5185 
5186  /*
5187  * Get the attribute values from the rules tuple
5188  */
5189  fno = SPI_fnumber(rulettc, "ev_type");
5190  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5191  Assert(!isnull);
5192  ev_type = DatumGetChar(dat);
5193 
5194  fno = SPI_fnumber(rulettc, "ev_class");
5195  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5196  Assert(!isnull);
5197  ev_class = DatumGetObjectId(dat);
5198 
5199  fno = SPI_fnumber(rulettc, "is_instead");
5200  dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5201  Assert(!isnull);
5202  is_instead = DatumGetBool(dat);
5203 
5204  fno = SPI_fnumber(rulettc, "ev_qual");
5205  ev_qual = SPI_getvalue(ruletup, rulettc, fno);
5206  Assert(ev_qual != NULL);
5207 
5208  fno = SPI_fnumber(rulettc, "ev_action");
5209  ev_action = SPI_getvalue(ruletup, rulettc, fno);
5210  Assert(ev_action != NULL);
5211  actions = (List *) stringToNode(ev_action);
5212 
5213  if (list_length(actions) != 1)
5214  {
5215  /* keep output buffer empty and leave */
5216  return;
5217  }
5218 
5219  query = (Query *) linitial(actions);
5220 
5221  if (ev_type != '1' || !is_instead ||
5222  strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
5223  {
5224  /* keep output buffer empty and leave */
5225  return;
5226  }
5227 
5228  ev_relation = table_open(ev_class, AccessShareLock);
5229 
5230  get_query_def(query, buf, NIL, RelationGetDescr(ev_relation),
5231  prettyFlags, wrapColumn, 0);
5232  appendStringInfoChar(buf, ';');
5233 
5234  table_close(ev_relation, AccessShareLock);
5235 }
5236 
5237 
5238 /* ----------
5239  * get_query_def - Parse back one query parsetree
5240  *
5241  * If resultDesc is not NULL, then it is the output tuple descriptor for
5242  * the view represented by a SELECT query.
5243  * ----------
5244  */
5245 static void
5246 get_query_def(Query *query, StringInfo buf, List *parentnamespace,
5247  TupleDesc resultDesc,
5248  int prettyFlags, int wrapColumn, int startIndent)
5249 {
5250  deparse_context context;
5251  deparse_namespace dpns;
5252 
5253  /* Guard against excessively long or deeply-nested queries */
5256 
5257  /*
5258  * Before we begin to examine the query, acquire locks on referenced
5259  * relations, and fix up deleted columns in JOIN RTEs. This ensures
5260  * consistent results. Note we assume it's OK to scribble on the passed
5261  * querytree!
5262  *
5263  * We are only deparsing the query (we are not about to execute it), so we
5264  * only need AccessShareLock on the relations it mentions.
5265  */
5266  AcquireRewriteLocks(query, false, false);
5267 
5268  context.buf = buf;
5269  context.namespaces = lcons(&dpns, list_copy(parentnamespace));
5270  context.windowClause = NIL;
5271  context.windowTList = NIL;
5272  context.varprefix = (parentnamespace != NIL ||
5273  list_length(query->rtable) != 1);
5274  context.prettyFlags = prettyFlags;
5275  context.wrapColumn = wrapColumn;
5276  context.indentLevel = startIndent;
5277  context.special_exprkind = EXPR_KIND_NONE;
5278  context.appendparents = NULL;
5279 
5280  set_deparse_for_query(&dpns, query, parentnamespace);
5281 
5282  switch (query->commandType)
5283  {
5284  case CMD_SELECT:
5285  get_select_query_def(query, &context, resultDesc);
5286  break;
5287 
5288  case CMD_UPDATE:
5289  get_update_query_def(query, &context);
5290  break;
5291 
5292  case CMD_INSERT:
5293  get_insert_query_def(query, &context);
5294  break;
5295 
5296  case CMD_DELETE:
5297  get_delete_query_def(query, &context);
5298  break;
5299 
5300  case CMD_NOTHING:
5301  appendStringInfoString(buf, "NOTHING");
5302  break;
5303 
5304  case CMD_UTILITY:
5305  get_utility_query_def(query, &context);
5306  break;
5307 
5308  default:
5309  elog(ERROR, "unrecognized query command type: %d",
5310  query->commandType);
5311  break;
5312  }
5313 }
5314 
5315 /* ----------
5316  * get_values_def - Parse back a VALUES list
5317  * ----------
5318  */
5319 static void
5320 get_values_def(List *values_lists, deparse_context *context)
5321 {
5322  StringInfo buf = context->buf;
5323  bool first_list = true;
5324  ListCell *vtl;
5325 
5326  appendStringInfoString(buf, "VALUES ");
5327 
5328  foreach(vtl, values_lists)
5329  {
5330  List *sublist = (List *) lfirst(vtl);
5331  bool first_col = true;
5332  ListCell *lc;
5333 
5334  if (first_list)
5335  first_list = false;
5336  else
5337  appendStringInfoString(buf, ", ");
5338 
5339  appendStringInfoChar(buf, '(');
5340  foreach(lc, sublist)
5341  {
5342  Node *col = (Node *) lfirst(lc);
5343 
5344  if (first_col)
5345  first_col = false;
5346  else
5347  appendStringInfoChar(buf, ',');
5348 
5349  /*
5350  * Print the value. Whole-row Vars need special treatment.
5351  */
5352  get_rule_expr_toplevel(col, context, false);
5353  }
5354  appendStringInfoChar(buf, ')');
5355  }
5356 }
5357 
5358 /* ----------
5359  * get_with_clause - Parse back a WITH clause
5360  * ----------
5361  */
5362 static void
5364 {
5365  StringInfo buf = context->buf;
5366  const char *sep;
5367  ListCell *l;
5368 
5369  if (query->cteList == NIL)
5370  return;
5371 
5372  if (PRETTY_INDENT(context))
5373  {
5374  context->indentLevel += PRETTYINDENT_STD;
5375  appendStringInfoChar(buf, ' ');
5376  }
5377 
5378  if (query->hasRecursive)
5379  sep = "WITH RECURSIVE ";
5380  else
5381  sep = "WITH ";
5382  foreach(l, query->cteList)
5383  {
5384  CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
5385 
5386  appendStringInfoString(buf, sep);
5388  if (cte->aliascolnames)
5389  {
5390  bool first = true;
5391  ListCell *col;
5392 
5393  appendStringInfoChar(buf, '(');
5394  foreach(col, cte->aliascolnames)
5395  {
5396  if (first)
5397  first = false;
5398  else
5399  appendStringInfoString(buf, ", ");
5401  quote_identifier(strVal(lfirst(col))));
5402  }
5403  appendStringInfoChar(buf, ')');
5404  }
5405  appendStringInfoString(buf, " AS ");
5406  switch (cte->ctematerialized)
5407  {
5408  case CTEMaterializeDefault:
5409  break;
5410  case CTEMaterializeAlways:
5411  appendStringInfoString(buf, "MATERIALIZED ");
5412  break;
5413  case CTEMaterializeNever:
5414  appendStringInfoString(buf, "NOT MATERIALIZED ");
5415  break;
5416  }
5417  appendStringInfoChar(buf, '(');
5418  if (PRETTY_INDENT(context))
5419  appendContextKeyword(context, "", 0, 0, 0);
5420  get_query_def((Query *) cte->ctequery, buf, context->namespaces, NULL,
5421  context->prettyFlags, context->wrapColumn,
5422  context->indentLevel);
5423  if (PRETTY_INDENT(context))
5424  appendContextKeyword(context, "", 0, 0, 0);
5425  appendStringInfoChar(buf, ')');
5426 
5427  if (cte->search_clause)
5428  {
5429  bool first = true;
5430  ListCell *lc;
5431 
5432  appendStringInfo(buf, " SEARCH %s FIRST BY ",
5433  cte->search_clause->search_breadth_first ? "BREADTH" : "DEPTH");
5434 
5435  foreach(lc, cte->search_clause->search_col_list)
5436  {
5437  if (first)
5438  first = false;
5439  else
5440  appendStringInfoString(buf, ", ");
5443  }
5444 
5446  }
5447 
5448  if (cte->cycle_clause)
5449  {
5450  bool first = true;
5451  ListCell *lc;
5452 
5453  appendStringInfoString(buf, " CYCLE ");
5454 
5455  foreach(lc, cte->cycle_clause->cycle_col_list)
5456  {
5457  if (first)
5458  first = false;
5459  else
5460  appendStringInfoString(buf, ", ");
5463  }
5464 
5466 
5467  {
5470 
5471  if (!(cmv->consttype == BOOLOID && !cmv->constisnull && DatumGetBool(cmv->constvalue) == true &&
5472  cmd->consttype == BOOLOID && !cmd->constisnull && DatumGetBool(cmd->constvalue) == false))
5473  {
5474  appendStringInfoString(buf, " TO ");
5475  get_rule_expr(cte->cycle_clause->cycle_mark_value, context, false);
5476  appendStringInfoString(buf, " DEFAULT ");
5477  get_rule_expr(cte->cycle_clause->cycle_mark_default, context, false);
5478  }
5479  }
5480 
5482  }
5483 
5484  sep = ", ";
5485  }
5486 
5487  if (PRETTY_INDENT(context))
5488  {
5489  context->indentLevel -= PRETTYINDENT_STD;
5490  appendContextKeyword(context, "", 0, 0, 0);
5491  }
5492  else
5493  appendStringInfoChar(buf, ' ');
5494 }
5495 
5496 /* ----------
5497  * get_select_query_def - Parse back a SELECT parsetree
5498  * ----------
5499  */
5500 static void
5502  TupleDesc resultDesc)
5503 {
5504  StringInfo buf = context->buf;
5505  List *save_windowclause;
5506  List *save_windowtlist;
5507  bool force_colno;
5508  ListCell *l;
5509 
5510  /* Insert the WITH clause if given */
5511  get_with_clause(query, context);
5512 
5513  /* Set up context for possible window functions */
5514  save_windowclause = context->windowClause;
5515  context->windowClause = query->windowClause;
5516  save_windowtlist = context->windowTList;
5517  context->windowTList = query->targetList;
5518 
5519  /*
5520  * If the Query node has a setOperations tree, then it's the top level of
5521  * a UNION/INTERSECT/EXCEPT query; only the WITH, ORDER BY and LIMIT
5522  * fields are interesting in the top query itself.
5523  */
5524  if (query->setOperations)
5525  {
5526  get_setop_query(query->setOperations, query, context, resultDesc);
5527  /* ORDER BY clauses must be simple in this case */
5528  force_colno = true;
5529  }
5530  else
5531  {
5532  get_basic_select_query(query, context, resultDesc);
5533  force_colno = false;
5534  }
5535 
5536  /* Add the ORDER BY clause if given */
5537  if (query->sortClause != NIL)
5538  {
5539  appendContextKeyword(context, " ORDER BY ",
5541  get_rule_orderby(query->sortClause, query->targetList,
5542  force_colno, context);
5543  }
5544 
5545  /*
5546  * Add the LIMIT/OFFSET clauses if given. If non-default options, use the
5547  * standard spelling of LIMIT.
5548  */
5549  if (query->limitOffset != NULL)
5550  {
5551  appendContextKeyword(context, " OFFSET ",
5553  get_rule_expr(query->limitOffset, context, false);
5554  }
5555  if (query->limitCount != NULL)
5556  {
5557  if (query->limitOption == LIMIT_OPTION_WITH_TIES)
5558  {
5559  appendContextKeyword(context, " FETCH FIRST ",
5561  get_rule_expr(query->limitCount, context, false);
5562  appendStringInfoString(buf, " ROWS WITH TIES");
5563  }
5564  else
5565  {
5566  appendContextKeyword(context, " LIMIT ",
5568  if (IsA(query->limitCount, Const) &&
5569  ((Const *) query->limitCount)->constisnull)
5570  appendStringInfoString(buf, "ALL");
5571  else
5572  get_rule_expr(query->limitCount, context, false);
5573  }
5574  }
5575 
5576  /* Add FOR [KEY] UPDATE/SHARE clauses if present */
5577  if (query->hasForUpdate)
5578  {
5579  foreach(l, query->rowMarks)
5580  {
5581  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
5582 
5583  /* don't print implicit clauses */
5584  if (rc->pushedDown)
5585  continue;
5586 
5587  switch (rc->strength)
5588  {
5589  case LCS_NONE:
5590  /* we intentionally throw an error for LCS_NONE */
5591  elog(ERROR, "unrecognized LockClauseStrength %d",
5592  (int) rc->strength);
5593  break;
5594  case LCS_FORKEYSHARE:
5595  appendContextKeyword(context, " FOR KEY SHARE",
5597  break;
5598  case LCS_FORSHARE:
5599  appendContextKeyword(context, " FOR SHARE",
5601  break;
5602  case LCS_FORNOKEYUPDATE:
5603  appendContextKeyword(context, " FOR NO KEY UPDATE",
5605  break;
5606  case LCS_FORUPDATE:
5607  appendContextKeyword(context, " FOR UPDATE",
5609  break;
5610  }
5611 
5612  appendStringInfo(buf, " OF %s",
5614  context)));
5615  if (rc->waitPolicy == LockWaitError)
5616  appendStringInfoString(buf, " NOWAIT");
5617  else if (rc->waitPolicy == LockWaitSkip)
5618  appendStringInfoString(buf, " SKIP LOCKED");
5619  }
5620  }
5621 
5622  context->windowClause = save_windowclause;
5623  context->windowTList = save_windowtlist;
5624 }
5625 
5626 /*
5627  * Detect whether query looks like SELECT ... FROM VALUES(),
5628  * with no need to rename the output columns of the VALUES RTE.
5629  * If so, return the VALUES RTE. Otherwise return NULL.
5630  */
5631 static RangeTblEntry *
5633 {
5634  RangeTblEntry *result = NULL;
5635  ListCell *lc;
5636 
5637  /*
5638  * We want to detect a match even if the Query also contains OLD or NEW
5639  * rule RTEs. So the idea is to scan the rtable and see if there is only
5640  * one inFromCl RTE that is a VALUES RTE.
5641  */
5642  foreach(lc, query->rtable)
5643  {
5644  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
5645 
5646  if (rte->rtekind == RTE_VALUES && rte->inFromCl)
5647  {
5648  if (result)
5649  return NULL; /* multiple VALUES (probably not possible) */
5650  result = rte;
5651  }
5652  else if (rte->rtekind == RTE_RELATION && !rte->inFromCl)
5653  continue; /* ignore rule entries */
5654  else
5655  return NULL; /* something else -> not simple VALUES */
5656  }
5657 
5658  /*
5659  * We don't need to check the targetlist in any great detail, because
5660  * parser/analyze.c will never generate a "bare" VALUES RTE --- they only
5661  * appear inside auto-generated sub-queries with very restricted
5662  * structure. However, DefineView might have modified the tlist by
5663  * injecting new column aliases, or we might have some other column
5664  * aliases forced by a resultDesc. We can only simplify if the RTE's
5665  * column names match the names that get_target_list() would select.
5666  */
5667  if (result)
5668  {
5669  ListCell *lcn;
5670  int colno;
5671 
5672  if (list_length(query->targetList) != list_length(result->eref->colnames))
5673  return NULL; /* this probably cannot happen */
5674  colno = 0;
5675  forboth(lc, query->targetList, lcn, result->eref->colnames)
5676  {
5677  TargetEntry *tle = (TargetEntry *) lfirst(lc);
5678  char *cname = strVal(lfirst(lcn));
5679  char *colname;
5680 
5681  if (tle->resjunk)
5682  return NULL; /* this probably cannot happen */
5683 
5684  /* compute name that get_target_list would use for column */
5685  colno++;
5686  if (resultDesc && colno <= resultDesc->natts)
5687  colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
5688  else
5689  colname = tle->resname;
5690 
5691  /* does it match the VALUES RTE? */
5692  if (colname == NULL || strcmp(colname, cname) != 0)
5693  return NULL; /* column name has been changed */
5694  }
5695  }
5696 
5697  return result;
5698 }
5699 
5700 static void
5702  TupleDesc resultDesc)
5703 {
5704  StringInfo buf = context->buf;
5705  RangeTblEntry *values_rte;
5706  char *sep;
5707  ListCell *l;
5708 
5709  if (PRETTY_INDENT(context))
5710  {
5711  context->indentLevel += PRETTYINDENT_STD;
5712  appendStringInfoChar(buf, ' ');
5713  }
5714 
5715  /*
5716  * If the query looks like SELECT * FROM (VALUES ...), then print just the
5717  * VALUES part. This reverses what transformValuesClause() did at parse
5718  * time.
5719  */
5720  values_rte = get_simple_values_rte(query, resultDesc);
5721  if (values_rte)
5722  {
5723  get_values_def(values_rte->values_lists, context);
5724  return;
5725  }
5726 
5727  /*
5728  * Build up the query string - first we say SELECT
5729  */
5730  if (query->isReturn)
5731  appendStringInfoString(buf, "RETURN");
5732  else
5733  appendStringInfoString(buf, "SELECT");
5734 
5735  /* Add the DISTINCT clause if given */
5736  if (query->distinctClause != NIL)
5737  {
5738  if (query->