PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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/table.h"
27#include "catalog/pg_am.h"
28#include "catalog/pg_authid.h"
31#include "catalog/pg_depend.h"
32#include "catalog/pg_language.h"
33#include "catalog/pg_opclass.h"
34#include "catalog/pg_operator.h"
36#include "catalog/pg_proc.h"
38#include "catalog/pg_trigger.h"
39#include "catalog/pg_type.h"
40#include "commands/defrem.h"
41#include "commands/tablespace.h"
42#include "common/keywords.h"
43#include "executor/spi.h"
44#include "funcapi.h"
45#include "mb/pg_wchar.h"
46#include "miscadmin.h"
47#include "nodes/makefuncs.h"
48#include "nodes/nodeFuncs.h"
49#include "nodes/pathnodes.h"
50#include "optimizer/optimizer.h"
51#include "parser/parse_agg.h"
52#include "parser/parse_func.h"
53#include "parser/parse_oper.h"
55#include "parser/parser.h"
56#include "parser/parsetree.h"
60#include "utils/array.h"
61#include "utils/builtins.h"
62#include "utils/fmgroids.h"
63#include "utils/guc.h"
64#include "utils/hsearch.h"
65#include "utils/lsyscache.h"
66#include "utils/partcache.h"
67#include "utils/rel.h"
68#include "utils/ruleutils.h"
69#include "utils/snapmgr.h"
70#include "utils/syscache.h"
71#include "utils/typcache.h"
72#include "utils/varlena.h"
73#include "utils/xml.h"
74
75/* ----------
76 * Pretty formatting constants
77 * ----------
78 */
79
80/* Indent counts */
81#define PRETTYINDENT_STD 8
82#define PRETTYINDENT_JOIN 4
83#define PRETTYINDENT_VAR 4
84
85#define PRETTYINDENT_LIMIT 40 /* wrap limit */
86
87/* Pretty flags */
88#define PRETTYFLAG_PAREN 0x0001
89#define PRETTYFLAG_INDENT 0x0002
90#define PRETTYFLAG_SCHEMA 0x0004
91
92/* Standard conversion of a "bool pretty" option to detailed flags */
93#define GET_PRETTY_FLAGS(pretty) \
94 ((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \
95 : PRETTYFLAG_INDENT)
96
97/* Default line length for pretty-print wrapping: 0 means wrap always */
98#define WRAP_COLUMN_DEFAULT 0
99
100/* macros to test if pretty action needed */
101#define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)
102#define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)
103#define PRETTY_SCHEMA(context) ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
104
105
106/* ----------
107 * Local data types
108 * ----------
109 */
110
111/* Context info needed for invoking a recursive querytree display routine */
112typedef struct
113{
114 StringInfo buf; /* output buffer to append to */
115 List *namespaces; /* List of deparse_namespace nodes */
116 TupleDesc resultDesc; /* if top level of a view, the view's tupdesc */
117 List *targetList; /* Current query level's SELECT targetlist */
118 List *windowClause; /* Current query level's WINDOW clause */
119 int prettyFlags; /* enabling of pretty-print functions */
120 int wrapColumn; /* max line length, or -1 for no limit */
121 int indentLevel; /* current indent level for pretty-print */
122 bool varprefix; /* true to print prefixes on Vars */
123 bool colNamesVisible; /* do we care about output column names? */
124 bool inGroupBy; /* deparsing GROUP BY clause? */
125 bool varInOrderBy; /* deparsing simple Var in ORDER BY? */
126 Bitmapset *appendparents; /* if not null, map child Vars of these relids
127 * back to the parent rel */
129
130/*
131 * Each level of query context around a subtree needs a level of Var namespace.
132 * A Var having varlevelsup=N refers to the N'th item (counting from 0) in
133 * the current context's namespaces list.
134 *
135 * rtable is the list of actual RTEs from the Query or PlannedStmt.
136 * rtable_names holds the alias name to be used for each RTE (either a C
137 * string, or NULL for nameless RTEs such as unnamed joins).
138 * rtable_columns holds the column alias names to be used for each RTE.
139 *
140 * subplans is a list of Plan trees for SubPlans and CTEs (it's only used
141 * in the PlannedStmt case).
142 * ctes is a list of CommonTableExpr nodes (only used in the Query case).
143 * appendrels, if not null (it's only used in the PlannedStmt case), is an
144 * array of AppendRelInfo nodes, indexed by child relid. We use that to map
145 * child-table Vars to their inheritance parents.
146 *
147 * In some cases we need to make names of merged JOIN USING columns unique
148 * across the whole query, not only per-RTE. If so, unique_using is true
149 * and using_names is a list of C strings representing names already assigned
150 * to USING columns.
151 *
152 * When deparsing plan trees, there is always just a single item in the
153 * deparse_namespace list (since a plan tree never contains Vars with
154 * varlevelsup > 0). We store the Plan node that is the immediate
155 * parent of the expression to be deparsed, as well as a list of that
156 * Plan's ancestors. In addition, we store its outer and inner subplan nodes,
157 * as well as their targetlists, and the index tlist if the current plan node
158 * might contain INDEX_VAR Vars. (These fields could be derived on-the-fly
159 * from the current Plan node, but it seems notationally clearer to set them
160 * up as separate fields.)
161 */
162typedef struct
163{
164 List *rtable; /* List of RangeTblEntry nodes */
165 List *rtable_names; /* Parallel list of names for RTEs */
166 List *rtable_columns; /* Parallel list of deparse_columns structs */
167 List *subplans; /* List of Plan trees for SubPlans */
168 List *ctes; /* List of CommonTableExpr nodes */
169 AppendRelInfo **appendrels; /* Array of AppendRelInfo nodes, or NULL */
170 char *ret_old_alias; /* alias for OLD in RETURNING list */
171 char *ret_new_alias; /* alias for NEW in RETURNING list */
172 /* Workspace for column alias assignment: */
173 bool unique_using; /* Are we making USING names globally unique */
174 List *using_names; /* List of assigned names for USING columns */
175 /* Remaining fields are used only when deparsing a Plan tree: */
176 Plan *plan; /* immediate parent of current expression */
177 List *ancestors; /* ancestors of plan */
178 Plan *outer_plan; /* outer subnode, or NULL if none */
179 Plan *inner_plan; /* inner subnode, or NULL if none */
180 List *outer_tlist; /* referent for OUTER_VAR Vars */
181 List *inner_tlist; /* referent for INNER_VAR Vars */
182 List *index_tlist; /* referent for INDEX_VAR Vars */
183 /* Special namespace representing a function signature: */
184 char *funcname;
186 char **argnames;
188
189/*
190 * Per-relation data about column alias names.
191 *
192 * Selecting aliases is unreasonably complicated because of the need to dump
193 * rules/views whose underlying tables may have had columns added, deleted, or
194 * renamed since the query was parsed. We must nonetheless print the rule/view
195 * in a form that can be reloaded and will produce the same results as before.
196 *
197 * For each RTE used in the query, we must assign column aliases that are
198 * unique within that RTE. SQL does not require this of the original query,
199 * but due to factors such as *-expansion we need to be able to uniquely
200 * reference every column in a decompiled query. As long as we qualify all
201 * column references, per-RTE uniqueness is sufficient for that.
202 *
203 * However, we can't ensure per-column name uniqueness for unnamed join RTEs,
204 * since they just inherit column names from their input RTEs, and we can't
205 * rename the columns at the join level. Most of the time this isn't an issue
206 * because we don't need to reference the join's output columns as such; we
207 * can reference the input columns instead. That approach can fail for merged
208 * JOIN USING columns, however, so when we have one of those in an unnamed
209 * join, we have to make that column's alias globally unique across the whole
210 * query to ensure it can be referenced unambiguously.
211 *
212 * Another problem is that a JOIN USING clause requires the columns to be
213 * merged to have the same aliases in both input RTEs, and that no other
214 * columns in those RTEs or their children conflict with the USING names.
215 * To handle that, we do USING-column alias assignment in a recursive
216 * traversal of the query's jointree. When descending through a JOIN with
217 * USING, we preassign the USING column names to the child columns, overriding
218 * other rules for column alias assignment. We also mark each RTE with a list
219 * of all USING column names selected for joins containing that RTE, so that
220 * when we assign other columns' aliases later, we can avoid conflicts.
221 *
222 * Another problem is that if a JOIN's input tables have had columns added or
223 * deleted since the query was parsed, we must generate a column alias list
224 * for the join that matches the current set of input columns --- otherwise, a
225 * change in the number of columns in the left input would throw off matching
226 * of aliases to columns of the right input. Thus, positions in the printable
227 * column alias list are not necessarily one-for-one with varattnos of the
228 * JOIN, so we need a separate new_colnames[] array for printing purposes.
229 *
230 * Finally, when dealing with wide tables we risk O(N^2) costs in assigning
231 * non-duplicate column names. We ameliorate that by using a hash table that
232 * holds all the strings appearing in colnames, new_colnames, and parentUsing.
233 */
234typedef struct
235{
236 /*
237 * colnames is an array containing column aliases to use for columns that
238 * existed when the query was parsed. Dropped columns have NULL entries.
239 * This array can be directly indexed by varattno to get a Var's name.
240 *
241 * Non-NULL entries are guaranteed unique within the RTE, *except* when
242 * this is for an unnamed JOIN RTE. In that case we merely copy up names
243 * from the two input RTEs.
244 *
245 * During the recursive descent in set_using_names(), forcible assignment
246 * of a child RTE's column name is represented by pre-setting that element
247 * of the child's colnames array. So at that stage, NULL entries in this
248 * array just mean that no name has been preassigned, not necessarily that
249 * the column is dropped.
250 */
251 int num_cols; /* length of colnames[] array */
252 char **colnames; /* array of C strings and NULLs */
253
254 /*
255 * new_colnames is an array containing column aliases to use for columns
256 * that would exist if the query was re-parsed against the current
257 * definitions of its base tables. This is what to print as the column
258 * alias list for the RTE. This array does not include dropped columns,
259 * but it will include columns added since original parsing. Indexes in
260 * it therefore have little to do with current varattno values. As above,
261 * entries are unique unless this is for an unnamed JOIN RTE. (In such an
262 * RTE, we never actually print this array, but we must compute it anyway
263 * for possible use in computing column names of upper joins.) The
264 * parallel array is_new_col marks which of these columns are new since
265 * original parsing. Entries with is_new_col false must match the
266 * non-NULL colnames entries one-for-one.
267 */
268 int num_new_cols; /* length of new_colnames[] array */
269 char **new_colnames; /* array of C strings */
270 bool *is_new_col; /* array of bool flags */
271
272 /* This flag tells whether we should actually print a column alias list */
274
275 /* This list has all names used as USING names in joins above this RTE */
276 List *parentUsing; /* names assigned to parent merged columns */
277
278 /*
279 * If this struct is for a JOIN RTE, we fill these fields during the
280 * set_using_names() pass to describe its relationship to its child RTEs.
281 *
282 * leftattnos and rightattnos are arrays with one entry per existing
283 * output column of the join (hence, indexable by join varattno). For a
284 * simple reference to a column of the left child, leftattnos[i] is the
285 * child RTE's attno and rightattnos[i] is zero; and conversely for a
286 * column of the right child. But for merged columns produced by JOIN
287 * USING/NATURAL JOIN, both leftattnos[i] and rightattnos[i] are nonzero.
288 * Note that a simple reference might be to a child RTE column that's been
289 * dropped; but that's OK since the column could not be used in the query.
290 *
291 * If it's a JOIN USING, usingNames holds the alias names selected for the
292 * merged columns (these might be different from the original USING list,
293 * if we had to modify names to achieve uniqueness).
294 */
295 int leftrti; /* rangetable index of left child */
296 int rightrti; /* rangetable index of right child */
297 int *leftattnos; /* left-child varattnos of join cols, or 0 */
298 int *rightattnos; /* right-child varattnos of join cols, or 0 */
299 List *usingNames; /* names assigned to merged columns */
300
301 /*
302 * Hash table holding copies of all the strings appearing in this struct's
303 * colnames, new_colnames, and parentUsing. We use a hash table only for
304 * sufficiently wide relations, and only during the colname-assignment
305 * functions set_relation_column_names and set_join_column_names;
306 * otherwise, names_hash is NULL.
307 */
308 HTAB *names_hash; /* entries are just strings */
310
311/* This macro is analogous to rt_fetch(), but for deparse_columns structs */
312#define deparse_columns_fetch(rangetable_index, dpns) \
313 ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
314
315/*
316 * Entry in set_rtable_names' hash table
317 */
318typedef struct
319{
320 char name[NAMEDATALEN]; /* Hash key --- must be first */
321 int counter; /* Largest addition used so far for name */
323
324/* Callback signature for resolve_special_varno() */
325typedef void (*rsv_callback) (Node *node, deparse_context *context,
326 void *callback_arg);
327
328
329/* ----------
330 * Global data
331 * ----------
332 */
334static const char *const query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1";
336static const char *const query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
337
338/* GUC parameters */
340
341
342/* ----------
343 * Local functions
344 *
345 * Most of these functions used to use fixed-size buffers to build their
346 * results. Now, they take an (already initialized) StringInfo object
347 * as a parameter, and append their text output to its contents.
348 * ----------
349 */
350static char *deparse_expression_pretty(Node *expr, List *dpcontext,
351 bool forceprefix, bool showimplicit,
352 int prettyFlags, int startIndent);
353static char *pg_get_viewdef_worker(Oid viewoid,
354 int prettyFlags, int wrapColumn);
355static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
358static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
359static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
360 const Oid *excludeOps,
361 bool attrsOnly, bool keysOnly,
362 bool showTblSpc, bool inherits,
363 int prettyFlags, bool missing_ok);
365 bool missing_ok);
366static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags,
367 bool attrsOnly, bool missing_ok);
369 int prettyFlags, bool missing_ok);
370static text *pg_get_expr_worker(text *expr, Oid relid, int prettyFlags);
382static void set_using_names(deparse_namespace *dpns, Node *jtnode,
383 List *parentUsing);
389static bool colname_is_unique(const char *colname, deparse_namespace *dpns,
391static char *make_colname_unique(char *colname, deparse_namespace *dpns,
395static void add_to_names_hash(deparse_columns *colinfo, const char *name);
399static char *get_rtable_name(int rtindex, deparse_context *context);
412 int prettyFlags);
414 int prettyFlags, int wrapColumn);
416 TupleDesc resultDesc, bool colNamesVisible,
417 int prettyFlags, int wrapColumn, int startIndent);
418static void get_values_def(List *values_lists, deparse_context *context);
419static void get_with_clause(Query *query, deparse_context *context);
420static void get_select_query_def(Query *query, deparse_context *context);
421static void get_insert_query_def(Query *query, deparse_context *context);
422static void get_update_query_def(Query *query, deparse_context *context);
423static void get_update_query_targetlist_def(Query *query, List *targetList,
424 deparse_context *context,
426static void get_delete_query_def(Query *query, deparse_context *context);
427static void get_merge_query_def(Query *query, deparse_context *context);
428static void get_utility_query_def(Query *query, deparse_context *context);
429static char *get_lock_clause_strength(LockClauseStrength strength);
430static void get_basic_select_query(Query *query, deparse_context *context);
431static void get_target_list(List *targetList, deparse_context *context);
432static void get_returning_clause(Query *query, deparse_context *context);
433static void get_setop_query(Node *setOp, Query *query,
434 deparse_context *context);
436 bool force_colno,
437 deparse_context *context);
438static void get_rule_groupingset(GroupingSet *gset, List *targetlist,
439 bool omit_parens, deparse_context *context);
440static void get_rule_orderby(List *orderList, List *targetList,
441 bool force_colno, deparse_context *context);
442static void get_rule_windowclause(Query *query, deparse_context *context);
443static void get_rule_windowspec(WindowClause *wc, List *targetList,
444 deparse_context *context);
445static void get_window_frame_options(int frameOptions,
446 Node *startOffset, Node *endOffset,
447 deparse_context *context);
448static char *get_variable(Var *var, int levelsup, bool istoplevel,
449 deparse_context *context);
450static void get_special_variable(Node *node, deparse_context *context,
451 void *callback_arg);
452static void resolve_special_varno(Node *node, deparse_context *context,
453 rsv_callback callback, void *callback_arg);
454static Node *find_param_referent(Param *param, deparse_context *context,
456static SubPlan *find_param_generator(Param *param, deparse_context *context,
457 int *column_p);
459 int *column_p);
460static void get_parameter(Param *param, deparse_context *context);
461static const char *get_simple_binary_op_name(OpExpr *expr);
462static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);
463static void appendContextKeyword(deparse_context *context, const char *str,
464 int indentBefore, int indentAfter, int indentPlus);
466static void get_rule_expr(Node *node, deparse_context *context,
467 bool showimplicit);
468static void get_rule_expr_toplevel(Node *node, deparse_context *context,
469 bool showimplicit);
470static void get_rule_list_toplevel(List *lst, deparse_context *context,
471 bool showimplicit);
472static void get_rule_expr_funccall(Node *node, deparse_context *context,
473 bool showimplicit);
474static bool looks_like_function(Node *node);
475static void get_oper_expr(OpExpr *expr, deparse_context *context);
476static void get_func_expr(FuncExpr *expr, deparse_context *context,
477 bool showimplicit);
478static void get_agg_expr(Aggref *aggref, deparse_context *context,
480static void get_agg_expr_helper(Aggref *aggref, deparse_context *context,
481 Aggref *original_aggref, const char *funcname,
482 const char *options, bool is_json_objectagg);
483static void get_agg_combine_expr(Node *node, deparse_context *context,
484 void *callback_arg);
485static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context);
486static void get_windowfunc_expr_helper(WindowFunc *wfunc, deparse_context *context,
487 const char *funcname, const char *options,
488 bool is_json_objectagg);
489static bool get_func_sql_syntax(FuncExpr *expr, deparse_context *context);
490static void get_coercion_expr(Node *arg, deparse_context *context,
491 Oid resulttype, int32 resulttypmod,
493static void get_const_expr(Const *constval, deparse_context *context,
494 int showtype);
495static void get_const_collation(Const *constval, deparse_context *context);
497static void get_json_returning(JsonReturning *returning, StringInfo buf,
500 deparse_context *context, bool showimplicit);
504 deparse_context *context,
505 const char *funcname,
506 bool is_json_objectagg);
507static void simple_quote_literal(StringInfo buf, const char *val);
508static void get_sublink_expr(SubLink *sublink, deparse_context *context);
509static void get_tablefunc(TableFunc *tf, deparse_context *context,
510 bool showimplicit);
511static void get_from_clause(Query *query, const char *prefix,
512 deparse_context *context);
513static void get_from_clause_item(Node *jtnode, Query *query,
514 deparse_context *context);
515static void get_rte_alias(RangeTblEntry *rte, int varno, bool use_as,
516 deparse_context *context);
518 deparse_context *context);
521 deparse_context *context);
522static void get_tablesample_def(TableSampleClause *tablesample,
523 deparse_context *context);
524static void get_opclass_name(Oid opclass, Oid actual_datatype,
526static Node *processIndirection(Node *node, deparse_context *context);
527static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context);
528static char *get_relation_name(Oid relid);
529static char *generate_relation_name(Oid relid, List *namespaces);
530static char *generate_qualified_relation_name(Oid relid);
531static char *generate_function_name(Oid funcid, int nargs,
532 List *argnames, Oid *argtypes,
533 bool has_variadic, bool *use_variadic_p,
534 bool inGroupBy);
536static void add_cast_to(StringInfo buf, Oid typid);
537static char *generate_qualified_type_name(Oid typid);
538static text *string_to_text(char *str);
539static char *flatten_reloptions(Oid relid);
540static void get_reloptions(StringInfo buf, Datum reloptions);
541static void get_json_path_spec(Node *path_spec, deparse_context *context,
542 bool showimplicit);
544 deparse_context *context,
545 bool showimplicit);
547 deparse_context *context,
548 bool showimplicit,
549 bool needcomma);
550
551#define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
552
553
554/* ----------
555 * pg_get_ruledef - Do it all and return a text
556 * that could be used as a statement
557 * to recreate the rule
558 * ----------
559 */
560Datum
562{
564 int prettyFlags;
565 char *res;
566
567 prettyFlags = PRETTYFLAG_INDENT;
568
569 res = pg_get_ruledef_worker(ruleoid, prettyFlags);
570
571 if (res == NULL)
573
575}
576
577
578Datum
580{
582 bool pretty = PG_GETARG_BOOL(1);
583 int prettyFlags;
584 char *res;
585
586 prettyFlags = GET_PRETTY_FLAGS(pretty);
587
588 res = pg_get_ruledef_worker(ruleoid, prettyFlags);
589
590 if (res == NULL)
592
594}
595
596
597static char *
599{
600 Datum args[1];
601 char nulls[1];
602 int spirc;
606
607 /*
608 * Do this first so that string is alloc'd in outer context not SPI's.
609 */
611
612 /*
613 * Connect to SPI manager
614 */
615 SPI_connect();
616
617 /*
618 * On the first call prepare the plan to lookup pg_rewrite. We read
619 * pg_rewrite over the SPI manager instead of using the syscache to be
620 * checked for read access on pg_rewrite.
621 */
622 if (plan_getrulebyoid == NULL)
623 {
624 Oid argtypes[1];
626
627 argtypes[0] = OIDOID;
628 plan = SPI_prepare(query_getrulebyoid, 1, argtypes);
629 if (plan == NULL)
630 elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid);
633 }
634
635 /*
636 * Get the pg_rewrite tuple for this rule
637 */
638 args[0] = ObjectIdGetDatum(ruleoid);
639 nulls[0] = ' ';
640 spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 0);
641 if (spirc != SPI_OK_SELECT)
642 elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
643 if (SPI_processed != 1)
644 {
645 /*
646 * There is no tuple data available here, just keep the output buffer
647 * empty.
648 */
649 }
650 else
651 {
652 /*
653 * Get the rule's definition and put it into executor's memory
654 */
657 make_ruledef(&buf, ruletup, rulettc, prettyFlags);
658 }
659
660 /*
661 * Disconnect from SPI manager
662 */
663 if (SPI_finish() != SPI_OK_FINISH)
664 elog(ERROR, "SPI_finish failed");
665
666 if (buf.len == 0)
667 return NULL;
668
669 return buf.data;
670}
671
672
673/* ----------
674 * pg_get_viewdef - Mainly the same thing, but we
675 * only return the SELECT part of a view
676 * ----------
677 */
678Datum
680{
681 /* By OID */
682 Oid viewoid = PG_GETARG_OID(0);
683 int prettyFlags;
684 char *res;
685
686 prettyFlags = PRETTYFLAG_INDENT;
687
688 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
689
690 if (res == NULL)
692
694}
695
696
697Datum
699{
700 /* By OID */
701 Oid viewoid = PG_GETARG_OID(0);
702 bool pretty = PG_GETARG_BOOL(1);
703 int prettyFlags;
704 char *res;
705
706 prettyFlags = GET_PRETTY_FLAGS(pretty);
707
708 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
709
710 if (res == NULL)
712
714}
715
716Datum
718{
719 /* By OID */
720 Oid viewoid = PG_GETARG_OID(0);
721 int wrap = PG_GETARG_INT32(1);
722 int prettyFlags;
723 char *res;
724
725 /* calling this implies we want pretty printing */
726 prettyFlags = GET_PRETTY_FLAGS(true);
727
728 res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
729
730 if (res == NULL)
732
734}
735
736Datum
738{
739 /* By qualified name */
741 int prettyFlags;
743 Oid viewoid;
744 char *res;
745
746 prettyFlags = PRETTYFLAG_INDENT;
747
748 /* Look up view name. Can't lock it - we might not have privileges. */
750 viewoid = RangeVarGetRelid(viewrel, NoLock, false);
751
752 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
753
754 if (res == NULL)
756
758}
759
760
761Datum
763{
764 /* By qualified name */
766 bool pretty = PG_GETARG_BOOL(1);
767 int prettyFlags;
769 Oid viewoid;
770 char *res;
771
772 prettyFlags = GET_PRETTY_FLAGS(pretty);
773
774 /* Look up view name. Can't lock it - we might not have privileges. */
776 viewoid = RangeVarGetRelid(viewrel, NoLock, false);
777
778 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
779
780 if (res == NULL)
782
784}
785
786/*
787 * Common code for by-OID and by-name variants of pg_get_viewdef
788 */
789static char *
790pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
791{
792 Datum args[2];
793 char nulls[2];
794 int spirc;
798
799 /*
800 * Do this first so that string is alloc'd in outer context not SPI's.
801 */
803
804 /*
805 * Connect to SPI manager
806 */
807 SPI_connect();
808
809 /*
810 * On the first call prepare the plan to lookup pg_rewrite. We read
811 * pg_rewrite over the SPI manager instead of using the syscache to be
812 * checked for read access on pg_rewrite.
813 */
814 if (plan_getviewrule == NULL)
815 {
816 Oid argtypes[2];
818
819 argtypes[0] = OIDOID;
820 argtypes[1] = NAMEOID;
821 plan = SPI_prepare(query_getviewrule, 2, argtypes);
822 if (plan == NULL)
823 elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule);
826 }
827
828 /*
829 * Get the pg_rewrite tuple for the view's SELECT rule
830 */
831 args[0] = ObjectIdGetDatum(viewoid);
833 nulls[0] = ' ';
834 nulls[1] = ' ';
835 spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 0);
836 if (spirc != SPI_OK_SELECT)
837 elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
838 if (SPI_processed != 1)
839 {
840 /*
841 * There is no tuple data available here, just keep the output buffer
842 * empty.
843 */
844 }
845 else
846 {
847 /*
848 * Get the rule's definition and put it into executor's memory
849 */
852 make_viewdef(&buf, ruletup, rulettc, prettyFlags, wrapColumn);
853 }
854
855 /*
856 * Disconnect from SPI manager
857 */
858 if (SPI_finish() != SPI_OK_FINISH)
859 elog(ERROR, "SPI_finish failed");
860
861 if (buf.len == 0)
862 return NULL;
863
864 return buf.data;
865}
866
867/* ----------
868 * pg_get_triggerdef - Get the definition of a trigger
869 * ----------
870 */
871Datum
873{
875 char *res;
876
877 res = pg_get_triggerdef_worker(trigid, false);
878
879 if (res == NULL)
881
883}
884
885Datum
887{
889 bool pretty = PG_GETARG_BOOL(1);
890 char *res;
891
893
894 if (res == NULL)
896
898}
899
900static char *
902{
907 ScanKeyData skey[1];
909 int findx = 0;
910 char *tgname;
911 char *tgoldtable;
912 char *tgnewtable;
913 Datum value;
914 bool isnull;
915
916 /*
917 * Fetch the pg_trigger tuple by the Oid of the trigger
918 */
920
921 ScanKeyInit(&skey[0],
925
927 NULL, 1, skey);
928
930
932 {
935 return NULL;
936 }
937
939
940 /*
941 * Start the trigger definition. Note that the trigger's name should never
942 * be schema-qualified, but the trigger rel's name may be.
943 */
945
946 tgname = NameStr(trigrec->tgname);
947 appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
948 OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
949 quote_identifier(tgname));
950
951 if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
952 appendStringInfoString(&buf, "BEFORE");
953 else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
954 appendStringInfoString(&buf, "AFTER");
955 else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
956 appendStringInfoString(&buf, "INSTEAD OF");
957 else
958 elog(ERROR, "unexpected tgtype value: %d", trigrec->tgtype);
959
960 if (TRIGGER_FOR_INSERT(trigrec->tgtype))
961 {
962 appendStringInfoString(&buf, " INSERT");
963 findx++;
964 }
965 if (TRIGGER_FOR_DELETE(trigrec->tgtype))
966 {
967 if (findx > 0)
968 appendStringInfoString(&buf, " OR DELETE");
969 else
970 appendStringInfoString(&buf, " DELETE");
971 findx++;
972 }
973 if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
974 {
975 if (findx > 0)
976 appendStringInfoString(&buf, " OR UPDATE");
977 else
978 appendStringInfoString(&buf, " UPDATE");
979 findx++;
980 /* tgattr is first var-width field, so OK to access directly */
981 if (trigrec->tgattr.dim1 > 0)
982 {
983 int i;
984
985 appendStringInfoString(&buf, " OF ");
986 for (i = 0; i < trigrec->tgattr.dim1; i++)
987 {
988 char *attname;
989
990 if (i > 0)
992 attname = get_attname(trigrec->tgrelid,
993 trigrec->tgattr.values[i], false);
995 }
996 }
997 }
998 if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
999 {
1000 if (findx > 0)
1001 appendStringInfoString(&buf, " OR TRUNCATE");
1002 else
1003 appendStringInfoString(&buf, " TRUNCATE");
1004 findx++;
1005 }
1006
1007 /*
1008 * In non-pretty mode, always schema-qualify the target table name for
1009 * safety. In pretty mode, schema-qualify only if not visible.
1010 */
1011 appendStringInfo(&buf, " ON %s ",
1012 pretty ?
1015
1016 if (OidIsValid(trigrec->tgconstraint))
1017 {
1018 if (OidIsValid(trigrec->tgconstrrelid))
1019 appendStringInfo(&buf, "FROM %s ",
1020 generate_relation_name(trigrec->tgconstrrelid, NIL));
1021 if (!trigrec->tgdeferrable)
1022 appendStringInfoString(&buf, "NOT ");
1023 appendStringInfoString(&buf, "DEFERRABLE INITIALLY ");
1024 if (trigrec->tginitdeferred)
1025 appendStringInfoString(&buf, "DEFERRED ");
1026 else
1027 appendStringInfoString(&buf, "IMMEDIATE ");
1028 }
1029
1031 tgrel->rd_att, &isnull);
1032 if (!isnull)
1033 tgoldtable = NameStr(*DatumGetName(value));
1034 else
1035 tgoldtable = NULL;
1037 tgrel->rd_att, &isnull);
1038 if (!isnull)
1039 tgnewtable = NameStr(*DatumGetName(value));
1040 else
1041 tgnewtable = NULL;
1042 if (tgoldtable != NULL || tgnewtable != NULL)
1043 {
1044 appendStringInfoString(&buf, "REFERENCING ");
1045 if (tgoldtable != NULL)
1046 appendStringInfo(&buf, "OLD TABLE AS %s ",
1047 quote_identifier(tgoldtable));
1048 if (tgnewtable != NULL)
1049 appendStringInfo(&buf, "NEW TABLE AS %s ",
1050 quote_identifier(tgnewtable));
1051 }
1052
1053 if (TRIGGER_FOR_ROW(trigrec->tgtype))
1054 appendStringInfoString(&buf, "FOR EACH ROW ");
1055 else
1056 appendStringInfoString(&buf, "FOR EACH STATEMENT ");
1057
1058 /* If the trigger has a WHEN qualification, add that */
1060 tgrel->rd_att, &isnull);
1061 if (!isnull)
1062 {
1063 Node *qual;
1064 char relkind;
1065 deparse_context context;
1069
1070 appendStringInfoString(&buf, "WHEN (");
1071
1073
1074 relkind = get_rel_relkind(trigrec->tgrelid);
1075
1076 /* Build minimal OLD and NEW RTEs for the rel */
1078 oldrte->rtekind = RTE_RELATION;
1079 oldrte->relid = trigrec->tgrelid;
1080 oldrte->relkind = relkind;
1081 oldrte->rellockmode = AccessShareLock;
1082 oldrte->alias = makeAlias("old", NIL);
1083 oldrte->eref = oldrte->alias;
1084 oldrte->lateral = false;
1085 oldrte->inh = false;
1086 oldrte->inFromCl = true;
1087
1089 newrte->rtekind = RTE_RELATION;
1090 newrte->relid = trigrec->tgrelid;
1091 newrte->relkind = relkind;
1092 newrte->rellockmode = AccessShareLock;
1093 newrte->alias = makeAlias("new", NIL);
1094 newrte->eref = newrte->alias;
1095 newrte->lateral = false;
1096 newrte->inh = false;
1097 newrte->inFromCl = true;
1098
1099 /* Build two-element rtable */
1100 memset(&dpns, 0, sizeof(dpns));
1101 dpns.rtable = list_make2(oldrte, newrte);
1102 dpns.subplans = NIL;
1103 dpns.ctes = NIL;
1104 dpns.appendrels = NULL;
1107
1108 /* Set up context with one-deep namespace stack */
1109 context.buf = &buf;
1110 context.namespaces = list_make1(&dpns);
1111 context.resultDesc = NULL;
1112 context.targetList = NIL;
1113 context.windowClause = NIL;
1114 context.varprefix = true;
1117 context.indentLevel = PRETTYINDENT_STD;
1118 context.colNamesVisible = true;
1119 context.inGroupBy = false;
1120 context.varInOrderBy = false;
1121 context.appendparents = NULL;
1122
1123 get_rule_expr(qual, &context, false);
1124
1126 }
1127
1128 appendStringInfo(&buf, "EXECUTE FUNCTION %s(",
1130 NIL, NULL,
1131 false, NULL, false));
1132
1133 if (trigrec->tgnargs > 0)
1134 {
1135 char *p;
1136 int i;
1137
1139 tgrel->rd_att, &isnull);
1140 if (isnull)
1141 elog(ERROR, "tgargs is null for trigger %u", trigid);
1142 p = (char *) VARDATA_ANY(DatumGetByteaPP(value));
1143 for (i = 0; i < trigrec->tgnargs; i++)
1144 {
1145 if (i > 0)
1148 /* advance p to next string embedded in tgargs */
1149 while (*p)
1150 p++;
1151 p++;
1152 }
1153 }
1154
1155 /* We deliberately do not put semi-colon at end */
1157
1158 /* Clean up */
1160
1162
1163 return buf.data;
1164}
1165
1166/* ----------
1167 * pg_get_indexdef - Get the definition of an index
1168 *
1169 * In the extended version, there is a colno argument as well as pretty bool.
1170 * if colno == 0, we want a complete index definition.
1171 * if colno > 0, we only want the Nth index key's variable or expression.
1172 *
1173 * Note that the SQL-function versions of this omit any info about the
1174 * index tablespace; this is intentional because pg_dump wants it that way.
1175 * However pg_get_indexdef_string() includes the index tablespace.
1176 * ----------
1177 */
1178Datum
1180{
1181 Oid indexrelid = PG_GETARG_OID(0);
1182 int prettyFlags;
1183 char *res;
1184
1185 prettyFlags = PRETTYFLAG_INDENT;
1186
1187 res = pg_get_indexdef_worker(indexrelid, 0, NULL,
1188 false, false,
1189 false, false,
1190 prettyFlags, true);
1191
1192 if (res == NULL)
1194
1196}
1197
1198Datum
1200{
1201 Oid indexrelid = PG_GETARG_OID(0);
1202 int32 colno = PG_GETARG_INT32(1);
1203 bool pretty = PG_GETARG_BOOL(2);
1204 int prettyFlags;
1205 char *res;
1206
1207 prettyFlags = GET_PRETTY_FLAGS(pretty);
1208
1209 res = pg_get_indexdef_worker(indexrelid, colno, NULL,
1210 colno != 0, false,
1211 false, false,
1212 prettyFlags, true);
1213
1214 if (res == NULL)
1216
1218}
1219
1220/*
1221 * Internal version for use by ALTER TABLE.
1222 * Includes a tablespace clause in the result.
1223 * Returns a palloc'd C string; no pretty-printing.
1224 */
1225char *
1227{
1228 return pg_get_indexdef_worker(indexrelid, 0, NULL,
1229 false, false,
1230 true, true,
1231 0, false);
1232}
1233
1234/* Internal version that just reports the key-column definitions */
1235char *
1237{
1238 int prettyFlags;
1239
1240 prettyFlags = GET_PRETTY_FLAGS(pretty);
1241
1242 return pg_get_indexdef_worker(indexrelid, 0, NULL,
1243 true, true,
1244 false, false,
1245 prettyFlags, false);
1246}
1247
1248/* Internal version, extensible with flags to control its behavior */
1249char *
1251{
1252 bool pretty = ((flags & RULE_INDEXDEF_PRETTY) != 0);
1253 bool keys_only = ((flags & RULE_INDEXDEF_KEYS_ONLY) != 0);
1254 int prettyFlags;
1255
1256 prettyFlags = GET_PRETTY_FLAGS(pretty);
1257
1258 return pg_get_indexdef_worker(indexrelid, 0, NULL,
1259 true, keys_only,
1260 false, false,
1261 prettyFlags, false);
1262}
1263
1264/*
1265 * Internal workhorse to decompile an index definition.
1266 *
1267 * This is now used for exclusion constraints as well: if excludeOps is not
1268 * NULL then it points to an array of exclusion operator OIDs.
1269 */
1270static char *
1271pg_get_indexdef_worker(Oid indexrelid, int colno,
1272 const Oid *excludeOps,
1273 bool attrsOnly, bool keysOnly,
1274 bool showTblSpc, bool inherits,
1275 int prettyFlags, bool missing_ok)
1276{
1277 /* might want a separate isConstraint parameter later */
1278 bool isConstraint = (excludeOps != NULL);
1286 List *indexprs;
1288 List *context;
1289 Oid indrelid;
1290 int keyno;
1298 char *str;
1299 char *sep;
1300
1301 /*
1302 * Fetch the pg_index tuple by the Oid of the index
1303 */
1306 {
1307 if (missing_ok)
1308 return NULL;
1309 elog(ERROR, "cache lookup failed for index %u", indexrelid);
1310 }
1312
1313 indrelid = idxrec->indrelid;
1314 Assert(indexrelid == idxrec->indexrelid);
1315
1316 /* Must get indcollation, indclass, and indoption the hard way */
1320
1324
1328
1329 /*
1330 * Fetch the pg_class tuple of the index relation
1331 */
1334 elog(ERROR, "cache lookup failed for relation %u", indexrelid);
1336
1337 /*
1338 * Fetch the pg_am tuple of the index' access method
1339 */
1341 if (!HeapTupleIsValid(ht_am))
1342 elog(ERROR, "cache lookup failed for access method %u",
1343 idxrelrec->relam);
1345
1346 /* Fetch the index AM's API struct */
1347 amroutine = GetIndexAmRoutine(amrec->amhandler);
1348
1349 /*
1350 * Get the index expressions, if any. (NOTE: we do not use the relcache
1351 * versions of the expressions and predicate, because we want to display
1352 * non-const-folded expressions.)
1353 */
1355 {
1357 char *exprsString;
1358
1364 }
1365 else
1366 indexprs = NIL;
1367
1369
1371
1372 /*
1373 * Start the index definition. Note that the index's name should never be
1374 * schema-qualified, but the indexed rel's name may be.
1375 */
1377
1378 if (!attrsOnly)
1379 {
1380 if (!isConstraint)
1381 appendStringInfo(&buf, "CREATE %sINDEX %s ON %s%s USING %s (",
1382 idxrec->indisunique ? "UNIQUE " : "",
1385 && !inherits ? "ONLY " : "",
1386 (prettyFlags & PRETTYFLAG_SCHEMA) ?
1389 quote_identifier(NameStr(amrec->amname)));
1390 else /* currently, must be EXCLUDE constraint */
1391 appendStringInfo(&buf, "EXCLUDE USING %s (",
1392 quote_identifier(NameStr(amrec->amname)));
1393 }
1394
1395 /*
1396 * Report the indexed attributes
1397 */
1398 sep = "";
1399 for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1400 {
1401 AttrNumber attnum = idxrec->indkey.values[keyno];
1404
1405 /*
1406 * Ignore non-key attributes if told to.
1407 */
1408 if (keysOnly && keyno >= idxrec->indnkeyatts)
1409 break;
1410
1411 /* Otherwise, print INCLUDE to divide key and non-key attrs. */
1412 if (!colno && keyno == idxrec->indnkeyatts)
1413 {
1414 appendStringInfoString(&buf, ") INCLUDE (");
1415 sep = "";
1416 }
1417
1418 if (!colno)
1420 sep = ", ";
1421
1422 if (attnum != 0)
1423 {
1424 /* Simple index column */
1425 char *attname;
1427
1429 if (!colno || colno == keyno + 1)
1434 }
1435 else
1436 {
1437 /* expressional index */
1438 Node *indexkey;
1439
1440 if (indexpr_item == NULL)
1441 elog(ERROR, "too few entries in indexprs list");
1444 /* Deparse */
1445 str = deparse_expression_pretty(indexkey, context, false, false,
1446 prettyFlags, 0);
1447 if (!colno || colno == keyno + 1)
1448 {
1449 /* Need parens if it's not a bare function call */
1452 else
1453 appendStringInfo(&buf, "(%s)", str);
1454 }
1457 }
1458
1459 /* Print additional decoration for (selected) key columns */
1461 (!colno || colno == keyno + 1))
1462 {
1463 int16 opt = indoption->values[keyno];
1464 Oid indcoll = indcollation->values[keyno];
1465 Datum attoptions = get_attoptions(indexrelid, keyno + 1);
1466 bool has_options = attoptions != (Datum) 0;
1467
1468 /* Add collation, if not default for column */
1470 appendStringInfo(&buf, " COLLATE %s",
1472
1473 /* Add the operator class name, if not default */
1474 get_opclass_name(indclass->values[keyno],
1476
1477 if (has_options)
1478 {
1480 get_reloptions(&buf, attoptions);
1482 }
1483
1484 /* Add options if relevant */
1485 if (amroutine->amcanorder)
1486 {
1487 /* if it supports sort ordering, report DESC and NULLS opts */
1488 if (opt & INDOPTION_DESC)
1489 {
1490 appendStringInfoString(&buf, " DESC");
1491 /* NULLS FIRST is the default in this case */
1492 if (!(opt & INDOPTION_NULLS_FIRST))
1493 appendStringInfoString(&buf, " NULLS LAST");
1494 }
1495 else
1496 {
1497 if (opt & INDOPTION_NULLS_FIRST)
1498 appendStringInfoString(&buf, " NULLS FIRST");
1499 }
1500 }
1501
1502 /* Add the exclusion operator if relevant */
1503 if (excludeOps != NULL)
1504 appendStringInfo(&buf, " WITH %s",
1506 keycoltype,
1507 keycoltype));
1508 }
1509 }
1510
1511 if (!attrsOnly)
1512 {
1514
1515 if (idxrec->indnullsnotdistinct)
1516 appendStringInfoString(&buf, " NULLS NOT DISTINCT");
1517
1518 /*
1519 * If it has options, append "WITH (options)"
1520 */
1521 str = flatten_reloptions(indexrelid);
1522 if (str)
1523 {
1524 appendStringInfo(&buf, " WITH (%s)", str);
1525 pfree(str);
1526 }
1527
1528 /*
1529 * Print tablespace, but only if requested
1530 */
1531 if (showTblSpc)
1532 {
1533 Oid tblspc;
1534
1535 tblspc = get_rel_tablespace(indexrelid);
1536 if (OidIsValid(tblspc))
1537 {
1538 if (isConstraint)
1539 appendStringInfoString(&buf, " USING INDEX");
1540 appendStringInfo(&buf, " TABLESPACE %s",
1542 }
1543 }
1544
1545 /*
1546 * If it's a partial index, decompile and append the predicate
1547 */
1549 {
1550 Node *node;
1552 char *predString;
1553
1554 /* Convert text string to node tree */
1558 node = (Node *) stringToNode(predString);
1560
1561 /* Deparse */
1562 str = deparse_expression_pretty(node, context, false, false,
1563 prettyFlags, 0);
1564 if (isConstraint)
1565 appendStringInfo(&buf, " WHERE (%s)", str);
1566 else
1567 appendStringInfo(&buf, " WHERE %s", str);
1568 }
1569 }
1570
1571 /* Clean up */
1575
1576 return buf.data;
1577}
1578
1579/* ----------
1580 * pg_get_querydef
1581 *
1582 * Public entry point to deparse one query parsetree.
1583 * The pretty flags are determined by GET_PRETTY_FLAGS(pretty).
1584 *
1585 * The result is a palloc'd C string.
1586 * ----------
1587 */
1588char *
1590{
1592 int prettyFlags;
1593
1594 prettyFlags = GET_PRETTY_FLAGS(pretty);
1595
1597
1598 get_query_def(query, &buf, NIL, NULL, true,
1599 prettyFlags, WRAP_COLUMN_DEFAULT, 0);
1600
1601 return buf.data;
1602}
1603
1604/*
1605 * pg_get_statisticsobjdef
1606 * Get the definition of an extended statistics object
1607 */
1608Datum
1610{
1612 char *res;
1613
1614 res = pg_get_statisticsobj_worker(statextid, false, true);
1615
1616 if (res == NULL)
1618
1620}
1621
1622/*
1623 * Internal version for use by ALTER TABLE.
1624 * Returns a palloc'd C string; no pretty-printing.
1625 */
1626char *
1631
1632/*
1633 * pg_get_statisticsobjdef_columns
1634 * Get columns and expressions for an extended statistics object
1635 */
1636Datum
1638{
1640 char *res;
1641
1642 res = pg_get_statisticsobj_worker(statextid, true, true);
1643
1644 if (res == NULL)
1646
1648}
1649
1650/*
1651 * Internal workhorse to decompile an extended statistics object.
1652 */
1653static char *
1655{
1659 int colno;
1660 char *nsp;
1661 ArrayType *arr;
1662 char *enabled;
1663 Datum datum;
1664 bool ndistinct_enabled;
1666 bool mcv_enabled;
1667 int i;
1668 List *context;
1669 ListCell *lc;
1670 List *exprs = NIL;
1671 bool has_exprs;
1672 int ncolumns;
1673
1675
1677 {
1678 if (missing_ok)
1679 return NULL;
1680 elog(ERROR, "cache lookup failed for statistics object %u", statextid);
1681 }
1682
1683 /* has the statistics expressions? */
1685
1687
1688 /*
1689 * Get the statistics expressions, if any. (NOTE: we do not use the
1690 * relcache versions of the expressions, because we want to display
1691 * non-const-folded expressions.)
1692 */
1693 if (has_exprs)
1694 {
1696 char *exprsString;
1697
1701 exprs = (List *) stringToNode(exprsString);
1703 }
1704 else
1705 exprs = NIL;
1706
1707 /* count the number of columns (attributes and expressions) */
1708 ncolumns = statextrec->stxkeys.dim1 + list_length(exprs);
1709
1711
1712 if (!columns_only)
1713 {
1714 nsp = get_namespace_name_or_temp(statextrec->stxnamespace);
1715 appendStringInfo(&buf, "CREATE STATISTICS %s",
1717 NameStr(statextrec->stxname)));
1718
1719 /*
1720 * Decode the stxkind column so that we know which stats types to
1721 * print.
1722 */
1725 arr = DatumGetArrayTypeP(datum);
1726 if (ARR_NDIM(arr) != 1 ||
1727 ARR_HASNULL(arr) ||
1728 ARR_ELEMTYPE(arr) != CHAROID)
1729 elog(ERROR, "stxkind is not a 1-D char array");
1730 enabled = (char *) ARR_DATA_PTR(arr);
1731
1732 ndistinct_enabled = false;
1733 dependencies_enabled = false;
1734 mcv_enabled = false;
1735
1736 for (i = 0; i < ARR_DIMS(arr)[0]; i++)
1737 {
1738 if (enabled[i] == STATS_EXT_NDISTINCT)
1739 ndistinct_enabled = true;
1740 else if (enabled[i] == STATS_EXT_DEPENDENCIES)
1741 dependencies_enabled = true;
1742 else if (enabled[i] == STATS_EXT_MCV)
1743 mcv_enabled = true;
1744
1745 /* ignore STATS_EXT_EXPRESSIONS (it's built automatically) */
1746 }
1747
1748 /*
1749 * If any option is disabled, then we'll need to append the types
1750 * clause to show which options are enabled. We omit the types clause
1751 * on purpose when all options are enabled, so a pg_dump/pg_restore
1752 * will create all statistics types on a newer postgres version, if
1753 * the statistics had all options enabled on the original version.
1754 *
1755 * But if the statistics is defined on just a single column, it has to
1756 * be an expression statistics. In that case we don't need to specify
1757 * kinds.
1758 */
1760 (ncolumns > 1))
1761 {
1762 bool gotone = false;
1763
1765
1767 {
1768 appendStringInfoString(&buf, "ndistinct");
1769 gotone = true;
1770 }
1771
1773 {
1774 appendStringInfo(&buf, "%sdependencies", gotone ? ", " : "");
1775 gotone = true;
1776 }
1777
1778 if (mcv_enabled)
1779 appendStringInfo(&buf, "%smcv", gotone ? ", " : "");
1780
1782 }
1783
1784 appendStringInfoString(&buf, " ON ");
1785 }
1786
1787 /* decode simple column references */
1788 for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
1789 {
1790 AttrNumber attnum = statextrec->stxkeys.values[colno];
1791 char *attname;
1792
1793 if (colno > 0)
1795
1796 attname = get_attname(statextrec->stxrelid, attnum, false);
1797
1799 }
1800
1802 statextrec->stxrelid);
1803
1804 foreach(lc, exprs)
1805 {
1806 Node *expr = (Node *) lfirst(lc);
1807 char *str;
1808 int prettyFlags = PRETTYFLAG_PAREN;
1809
1810 str = deparse_expression_pretty(expr, context, false, false,
1811 prettyFlags, 0);
1812
1813 if (colno > 0)
1815
1816 /* Need parens if it's not a bare function call */
1817 if (looks_like_function(expr))
1819 else
1820 appendStringInfo(&buf, "(%s)", str);
1821
1822 colno++;
1823 }
1824
1825 if (!columns_only)
1826 appendStringInfo(&buf, " FROM %s",
1828
1830
1831 return buf.data;
1832}
1833
1834/*
1835 * Generate text array of expressions for statistics object.
1836 */
1837Datum
1839{
1843 Datum datum;
1844 List *context;
1845 ListCell *lc;
1846 List *exprs = NIL;
1847 bool has_exprs;
1848 char *tmp;
1849 ArrayBuildState *astate = NULL;
1850
1852
1855
1856 /* Does the stats object have expressions? */
1858
1859 /* no expressions? we're done */
1860 if (!has_exprs)
1861 {
1864 }
1865
1867
1868 /*
1869 * Get the statistics expressions, and deparse them into text values.
1870 */
1873 tmp = TextDatumGetCString(datum);
1874 exprs = (List *) stringToNode(tmp);
1875 pfree(tmp);
1876
1878 statextrec->stxrelid);
1879
1880 foreach(lc, exprs)
1881 {
1882 Node *expr = (Node *) lfirst(lc);
1883 char *str;
1884 int prettyFlags = PRETTYFLAG_INDENT;
1885
1886 str = deparse_expression_pretty(expr, context, false, false,
1887 prettyFlags, 0);
1888
1889 astate = accumArrayResult(astate,
1891 false,
1892 TEXTOID,
1894 }
1895
1897
1899}
1900
1901/*
1902 * pg_get_partkeydef
1903 *
1904 * Returns the partition key specification, ie, the following:
1905 *
1906 * { RANGE | LIST | HASH } (column opt_collation opt_opclass [, ...])
1907 */
1908Datum
1910{
1911 Oid relid = PG_GETARG_OID(0);
1912 char *res;
1913
1914 res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
1915
1916 if (res == NULL)
1918
1920}
1921
1922/* Internal version that just reports the column definitions */
1923char *
1925{
1926 int prettyFlags;
1927
1928 prettyFlags = GET_PRETTY_FLAGS(pretty);
1929
1930 return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
1931}
1932
1933/*
1934 * Internal workhorse to decompile a partition key definition.
1935 */
1936static char *
1937pg_get_partkeydef_worker(Oid relid, int prettyFlags,
1938 bool attrsOnly, bool missing_ok)
1939{
1941 HeapTuple tuple;
1943 oidvector *partcollation;
1944 List *partexprs;
1946 List *context;
1947 Datum datum;
1949 int keyno;
1950 char *str;
1951 char *sep;
1952
1954 if (!HeapTupleIsValid(tuple))
1955 {
1956 if (missing_ok)
1957 return NULL;
1958 elog(ERROR, "cache lookup failed for partition key of %u", relid);
1959 }
1960
1962
1963 Assert(form->partrelid == relid);
1964
1965 /* Must get partclass and partcollation the hard way */
1966 datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
1968 partclass = (oidvector *) DatumGetPointer(datum);
1969
1970 datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
1972 partcollation = (oidvector *) DatumGetPointer(datum);
1973
1974
1975 /*
1976 * Get the expressions, if any. (NOTE: we do not use the relcache
1977 * versions of the expressions, because we want to display
1978 * non-const-folded expressions.)
1979 */
1981 {
1983 char *exprsString;
1984
1988 partexprs = (List *) stringToNode(exprsString);
1989
1990 if (!IsA(partexprs, List))
1991 elog(ERROR, "unexpected node type found in partexprs: %d",
1992 (int) nodeTag(partexprs));
1993
1995 }
1996 else
1997 partexprs = NIL;
1998
1999 partexpr_item = list_head(partexprs);
2000 context = deparse_context_for(get_relation_name(relid), relid);
2001
2003
2004 switch (form->partstrat)
2005 {
2007 if (!attrsOnly)
2008 appendStringInfoString(&buf, "HASH");
2009 break;
2011 if (!attrsOnly)
2012 appendStringInfoString(&buf, "LIST");
2013 break;
2015 if (!attrsOnly)
2016 appendStringInfoString(&buf, "RANGE");
2017 break;
2018 default:
2019 elog(ERROR, "unexpected partition strategy: %d",
2020 (int) form->partstrat);
2021 }
2022
2023 if (!attrsOnly)
2025 sep = "";
2026 for (keyno = 0; keyno < form->partnatts; keyno++)
2027 {
2028 AttrNumber attnum = form->partattrs.values[keyno];
2031 Oid partcoll;
2032
2034 sep = ", ";
2035 if (attnum != 0)
2036 {
2037 /* Simple attribute reference */
2038 char *attname;
2040
2041 attname = get_attname(relid, attnum, false);
2046 }
2047 else
2048 {
2049 /* Expression */
2050 Node *partkey;
2051
2052 if (partexpr_item == NULL)
2053 elog(ERROR, "too few entries in partexprs list");
2055 partexpr_item = lnext(partexprs, partexpr_item);
2056
2057 /* Deparse */
2058 str = deparse_expression_pretty(partkey, context, false, false,
2059 prettyFlags, 0);
2060 /* Need parens if it's not a bare function call */
2063 else
2064 appendStringInfo(&buf, "(%s)", str);
2065
2068 }
2069
2070 /* Add collation, if not default for column */
2071 partcoll = partcollation->values[keyno];
2073 appendStringInfo(&buf, " COLLATE %s",
2075
2076 /* Add the operator class name, if not default */
2077 if (!attrsOnly)
2078 get_opclass_name(partclass->values[keyno], keycoltype, &buf);
2079 }
2080
2081 if (!attrsOnly)
2083
2084 /* Clean up */
2085 ReleaseSysCache(tuple);
2086
2087 return buf.data;
2088}
2089
2090/*
2091 * pg_get_partition_constraintdef
2092 *
2093 * Returns partition constraint expression as a string for the input relation
2094 */
2095Datum
2097{
2100 int prettyFlags;
2101 List *context;
2102 char *consrc;
2103
2105
2106 /* Quick exit if no partition constraint */
2107 if (constr_expr == NULL)
2109
2110 /*
2111 * Deparse and return the constraint expression.
2112 */
2113 prettyFlags = PRETTYFLAG_INDENT;
2115 consrc = deparse_expression_pretty((Node *) constr_expr, context, false,
2116 false, prettyFlags, 0);
2117
2119}
2120
2121/*
2122 * pg_get_partconstrdef_string
2123 *
2124 * Returns the partition constraint as a C-string for the input relation, with
2125 * the given alias. No pretty-printing.
2126 */
2127char *
2129{
2131 List *context;
2132
2134 context = deparse_context_for(aliasname, partitionId);
2135
2136 return deparse_expression((Node *) constr_expr, context, true, false);
2137}
2138
2139/*
2140 * pg_get_constraintdef
2141 *
2142 * Returns the definition for the constraint, ie, everything that needs to
2143 * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>".
2144 */
2145Datum
2147{
2149 int prettyFlags;
2150 char *res;
2151
2152 prettyFlags = PRETTYFLAG_INDENT;
2153
2154 res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2155
2156 if (res == NULL)
2158
2160}
2161
2162Datum
2164{
2166 bool pretty = PG_GETARG_BOOL(1);
2167 int prettyFlags;
2168 char *res;
2169
2170 prettyFlags = GET_PRETTY_FLAGS(pretty);
2171
2172 res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2173
2174 if (res == NULL)
2176
2178}
2179
2180/*
2181 * Internal version that returns a full ALTER TABLE ... ADD CONSTRAINT command
2182 */
2183char *
2188
2189/*
2190 * As of 9.4, we now use an MVCC snapshot for this.
2191 */
2192static char *
2194 int prettyFlags, bool missing_ok)
2195{
2196 HeapTuple tup;
2199 SysScanDesc scandesc;
2203
2204 ScanKeyInit(&scankey[0],
2208
2209 scandesc = systable_beginscan(relation,
2211 true,
2212 snapshot,
2213 1,
2214 scankey);
2215
2216 /*
2217 * We later use the tuple with SysCacheGetAttr() as if we had obtained it
2218 * via SearchSysCache, which works fine.
2219 */
2220 tup = systable_getnext(scandesc);
2221
2222 UnregisterSnapshot(snapshot);
2223
2224 if (!HeapTupleIsValid(tup))
2225 {
2226 if (missing_ok)
2227 {
2228 systable_endscan(scandesc);
2229 table_close(relation, AccessShareLock);
2230 return NULL;
2231 }
2232 elog(ERROR, "could not find tuple for constraint %u", constraintId);
2233 }
2234
2236
2238
2239 if (fullCommand)
2240 {
2241 if (OidIsValid(conForm->conrelid))
2242 {
2243 /*
2244 * Currently, callers want ALTER TABLE (without ONLY) for CHECK
2245 * constraints, and other types of constraints don't inherit
2246 * anyway so it doesn't matter whether we say ONLY or not. Someday
2247 * we might need to let callers specify whether to put ONLY in the
2248 * command.
2249 */
2250 appendStringInfo(&buf, "ALTER TABLE %s ADD CONSTRAINT %s ",
2252 quote_identifier(NameStr(conForm->conname)));
2253 }
2254 else
2255 {
2256 /* Must be a domain constraint */
2257 Assert(OidIsValid(conForm->contypid));
2258 appendStringInfo(&buf, "ALTER DOMAIN %s ADD CONSTRAINT %s ",
2260 quote_identifier(NameStr(conForm->conname)));
2261 }
2262 }
2263
2264 switch (conForm->contype)
2265 {
2266 case CONSTRAINT_FOREIGN:
2267 {
2268 Datum val;
2269 bool isnull;
2270 const char *string;
2271
2272 /* Start off the constraint definition */
2273 appendStringInfoString(&buf, "FOREIGN KEY (");
2274
2275 /* Fetch and build referencing-column list */
2278
2279 /* If it is a temporal foreign key then it uses PERIOD. */
2280 decompile_column_index_array(val, conForm->conrelid, conForm->conperiod, &buf);
2281
2282 /* add foreign relation name */
2283 appendStringInfo(&buf, ") REFERENCES %s(",
2285 NIL));
2286
2287 /* Fetch and build referenced-column list */
2290
2291 decompile_column_index_array(val, conForm->confrelid, conForm->conperiod, &buf);
2292
2294
2295 /* Add match type */
2296 switch (conForm->confmatchtype)
2297 {
2299 string = " MATCH FULL";
2300 break;
2302 string = " MATCH PARTIAL";
2303 break;
2305 string = "";
2306 break;
2307 default:
2308 elog(ERROR, "unrecognized confmatchtype: %d",
2309 conForm->confmatchtype);
2310 string = ""; /* keep compiler quiet */
2311 break;
2312 }
2313 appendStringInfoString(&buf, string);
2314
2315 /* Add ON UPDATE and ON DELETE clauses, if needed */
2316 switch (conForm->confupdtype)
2317 {
2319 string = NULL; /* suppress default */
2320 break;
2322 string = "RESTRICT";
2323 break;
2325 string = "CASCADE";
2326 break;
2328 string = "SET NULL";
2329 break;
2331 string = "SET DEFAULT";
2332 break;
2333 default:
2334 elog(ERROR, "unrecognized confupdtype: %d",
2335 conForm->confupdtype);
2336 string = NULL; /* keep compiler quiet */
2337 break;
2338 }
2339 if (string)
2340 appendStringInfo(&buf, " ON UPDATE %s", string);
2341
2342 switch (conForm->confdeltype)
2343 {
2345 string = NULL; /* suppress default */
2346 break;
2348 string = "RESTRICT";
2349 break;
2351 string = "CASCADE";
2352 break;
2354 string = "SET NULL";
2355 break;
2357 string = "SET DEFAULT";
2358 break;
2359 default:
2360 elog(ERROR, "unrecognized confdeltype: %d",
2361 conForm->confdeltype);
2362 string = NULL; /* keep compiler quiet */
2363 break;
2364 }
2365 if (string)
2366 appendStringInfo(&buf, " ON DELETE %s", string);
2367
2368 /*
2369 * Add columns specified to SET NULL or SET DEFAULT if
2370 * provided.
2371 */
2374 if (!isnull)
2375 {
2377 decompile_column_index_array(val, conForm->conrelid, false, &buf);
2379 }
2380
2381 break;
2382 }
2383 case CONSTRAINT_PRIMARY:
2384 case CONSTRAINT_UNIQUE:
2385 {
2386 Datum val;
2387 Oid indexId;
2388 int keyatts;
2390
2391 /* Start off the constraint definition */
2392 if (conForm->contype == CONSTRAINT_PRIMARY)
2393 appendStringInfoString(&buf, "PRIMARY KEY ");
2394 else
2395 appendStringInfoString(&buf, "UNIQUE ");
2396
2397 indexId = conForm->conindid;
2398
2401 elog(ERROR, "cache lookup failed for index %u", indexId);
2402 if (conForm->contype == CONSTRAINT_UNIQUE &&
2403 ((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct)
2404 appendStringInfoString(&buf, "NULLS NOT DISTINCT ");
2405
2407
2408 /* Fetch and build target column list */
2411
2412 keyatts = decompile_column_index_array(val, conForm->conrelid, false, &buf);
2413 if (conForm->conperiod)
2414 appendStringInfoString(&buf, " WITHOUT OVERLAPS");
2415
2417
2418 /* Build including column list (from pg_index.indkeys) */
2421 if (DatumGetInt32(val) > keyatts)
2422 {
2423 Datum cols;
2424 Datum *keys;
2425 int nKeys;
2426 int j;
2427
2428 appendStringInfoString(&buf, " INCLUDE (");
2429
2432
2434 &keys, NULL, &nKeys);
2435
2436 for (j = keyatts; j < nKeys; j++)
2437 {
2438 char *colName;
2439
2440 colName = get_attname(conForm->conrelid,
2441 DatumGetInt16(keys[j]), false);
2442 if (j > keyatts)
2445 }
2446
2448 }
2450
2451 /* XXX why do we only print these bits if fullCommand? */
2453 {
2455 Oid tblspc;
2456
2457 if (options)
2458 {
2459 appendStringInfo(&buf, " WITH (%s)", options);
2460 pfree(options);
2461 }
2462
2463 /*
2464 * Print the tablespace, unless it's the database default.
2465 * This is to help ALTER TABLE usage of this facility,
2466 * which needs this behavior to recreate exact catalog
2467 * state.
2468 */
2470 if (OidIsValid(tblspc))
2471 appendStringInfo(&buf, " USING INDEX TABLESPACE %s",
2473 }
2474
2475 break;
2476 }
2477 case CONSTRAINT_CHECK:
2478 {
2479 Datum val;
2480 char *conbin;
2481 char *consrc;
2482 Node *expr;
2483 List *context;
2484
2485 /* Fetch constraint expression in parsetree form */
2488
2490 expr = stringToNode(conbin);
2491
2492 /* Set up deparsing context for Var nodes in constraint */
2493 if (conForm->conrelid != InvalidOid)
2494 {
2495 /* relation constraint */
2496 context = deparse_context_for(get_relation_name(conForm->conrelid),
2497 conForm->conrelid);
2498 }
2499 else
2500 {
2501 /* domain constraint --- can't have Vars */
2502 context = NIL;
2503 }
2504
2505 consrc = deparse_expression_pretty(expr, context, false, false,
2506 prettyFlags, 0);
2507
2508 /*
2509 * Now emit the constraint definition, adding NO INHERIT if
2510 * necessary.
2511 *
2512 * There are cases where the constraint expression will be
2513 * fully parenthesized and we don't need the outer parens ...
2514 * but there are other cases where we do need 'em. Be
2515 * conservative for now.
2516 *
2517 * Note that simply checking for leading '(' and trailing ')'
2518 * would NOT be good enough, consider "(x > 0) AND (y > 0)".
2519 */
2520 appendStringInfo(&buf, "CHECK (%s)%s",
2521 consrc,
2522 conForm->connoinherit ? " NO INHERIT" : "");
2523 break;
2524 }
2525 case CONSTRAINT_NOTNULL:
2526 {
2527 if (conForm->conrelid)
2528 {
2530
2532
2533 appendStringInfo(&buf, "NOT NULL %s",
2535 attnum, false)));
2537 appendStringInfoString(&buf, " NO INHERIT");
2538 }
2539 else if (conForm->contypid)
2540 {
2541 /* conkey is null for domain not-null constraints */
2542 appendStringInfoString(&buf, "NOT NULL");
2543 }
2544 break;
2545 }
2546
2547 case CONSTRAINT_TRIGGER:
2548
2549 /*
2550 * There isn't an ALTER TABLE syntax for creating a user-defined
2551 * constraint trigger, but it seems better to print something than
2552 * throw an error; if we throw error then this function couldn't
2553 * safely be applied to all rows of pg_constraint.
2554 */
2555 appendStringInfoString(&buf, "TRIGGER");
2556 break;
2558 {
2559 Oid indexOid = conForm->conindid;
2560 Datum val;
2561 Datum *elems;
2562 int nElems;
2563 int i;
2564 Oid *operators;
2565
2566 /* Extract operator OIDs from the pg_constraint tuple */
2569
2571 &elems, NULL, &nElems);
2572
2573 operators = (Oid *) palloc(nElems * sizeof(Oid));
2574 for (i = 0; i < nElems; i++)
2575 operators[i] = DatumGetObjectId(elems[i]);
2576
2577 /* pg_get_indexdef_worker does the rest */
2578 /* suppress tablespace because pg_dump wants it that way */
2580 pg_get_indexdef_worker(indexOid,
2581 0,
2582 operators,
2583 false,
2584 false,
2585 false,
2586 false,
2587 prettyFlags,
2588 false));
2589 break;
2590 }
2591 default:
2592 elog(ERROR, "invalid constraint type \"%c\"", conForm->contype);
2593 break;
2594 }
2595
2596 if (conForm->condeferrable)
2597 appendStringInfoString(&buf, " DEFERRABLE");
2598 if (conForm->condeferred)
2599 appendStringInfoString(&buf, " INITIALLY DEFERRED");
2600
2601 /* Validated status is irrelevant when the constraint is NOT ENFORCED. */
2602 if (!conForm->conenforced)
2603 appendStringInfoString(&buf, " NOT ENFORCED");
2604 else if (!conForm->convalidated)
2605 appendStringInfoString(&buf, " NOT VALID");
2606
2607 /* Cleanup */
2608 systable_endscan(scandesc);
2609 table_close(relation, AccessShareLock);
2610
2611 return buf.data;
2612}
2613
2614
2615/*
2616 * Convert an int16[] Datum into a comma-separated list of column names
2617 * for the indicated relation; append the list to buf. Returns the number
2618 * of keys.
2619 */
2620static int
2623{
2624 Datum *keys;
2625 int nKeys;
2626 int j;
2627
2628 /* Extract data from array of int16 */
2630 &keys, NULL, &nKeys);
2631
2632 for (j = 0; j < nKeys; j++)
2633 {
2634 char *colName;
2635
2636 colName = get_attname(relId, DatumGetInt16(keys[j]), false);
2637
2638 if (j == 0)
2640 else
2641 appendStringInfo(buf, ", %s%s",
2642 (withPeriod && j == nKeys - 1) ? "PERIOD " : "",
2644 }
2645
2646 return nKeys;
2647}
2648
2649
2650/* ----------
2651 * pg_get_expr - Decompile an expression tree
2652 *
2653 * Input: an expression tree in nodeToString form, and a relation OID
2654 *
2655 * Output: reverse-listed expression
2656 *
2657 * Currently, the expression can only refer to a single relation, namely
2658 * the one specified by the second parameter. This is sufficient for
2659 * partial indexes, column default expressions, etc. We also support
2660 * Var-free expressions, for which the OID can be InvalidOid.
2661 *
2662 * If the OID is nonzero but not actually valid, don't throw an error,
2663 * just return NULL. This is a bit questionable, but it's what we've
2664 * done historically, and it can help avoid unwanted failures when
2665 * examining catalog entries for just-deleted relations.
2666 *
2667 * We expect this function to work, or throw a reasonably clean error,
2668 * for any node tree that can appear in a catalog pg_node_tree column.
2669 * Query trees, such as those appearing in pg_rewrite.ev_action, are
2670 * not supported. Nor are expressions in more than one relation, which
2671 * can appear in places like pg_rewrite.ev_qual.
2672 * ----------
2673 */
2674Datum
2676{
2677 text *expr = PG_GETARG_TEXT_PP(0);
2678 Oid relid = PG_GETARG_OID(1);
2679 text *result;
2680 int prettyFlags;
2681
2682 prettyFlags = PRETTYFLAG_INDENT;
2683
2684 result = pg_get_expr_worker(expr, relid, prettyFlags);
2685 if (result)
2686 PG_RETURN_TEXT_P(result);
2687 else
2689}
2690
2691Datum
2693{
2694 text *expr = PG_GETARG_TEXT_PP(0);
2695 Oid relid = PG_GETARG_OID(1);
2696 bool pretty = PG_GETARG_BOOL(2);
2697 text *result;
2698 int prettyFlags;
2699
2700 prettyFlags = GET_PRETTY_FLAGS(pretty);
2701
2702 result = pg_get_expr_worker(expr, relid, prettyFlags);
2703 if (result)
2704 PG_RETURN_TEXT_P(result);
2705 else
2707}
2708
2709static text *
2710pg_get_expr_worker(text *expr, Oid relid, int prettyFlags)
2711{
2712 Node *node;
2713 Node *tst;
2714 Relids relids;
2715 List *context;
2716 char *exprstr;
2717 Relation rel = NULL;
2718 char *str;
2719
2720 /* Convert input pg_node_tree (really TEXT) object to C string */
2721 exprstr = text_to_cstring(expr);
2722
2723 /* Convert expression to node tree */
2724 node = (Node *) stringToNode(exprstr);
2725
2726 pfree(exprstr);
2727
2728 /*
2729 * Throw error if the input is a querytree rather than an expression tree.
2730 * While we could support queries here, there seems no very good reason
2731 * to. In most such catalog columns, we'll see a List of Query nodes, or
2732 * even nested Lists, so drill down to a non-List node before checking.
2733 */
2734 tst = node;
2735 while (tst && IsA(tst, List))
2736 tst = linitial((List *) tst);
2737 if (tst && IsA(tst, Query))
2738 ereport(ERROR,
2740 errmsg("input is a query, not an expression")));
2741
2742 /*
2743 * Throw error if the expression contains Vars we won't be able to
2744 * deparse.
2745 */
2746 relids = pull_varnos(NULL, node);
2747 if (OidIsValid(relid))
2748 {
2749 if (!bms_is_subset(relids, bms_make_singleton(1)))
2750 ereport(ERROR,
2752 errmsg("expression contains variables of more than one relation")));
2753 }
2754 else
2755 {
2756 if (!bms_is_empty(relids))
2757 ereport(ERROR,
2759 errmsg("expression contains variables")));
2760 }
2761
2762 /*
2763 * Prepare deparse context if needed. If we are deparsing with a relid,
2764 * we need to transiently open and lock the rel, to make sure it won't go
2765 * away underneath us. (set_relation_column_names would lock it anyway,
2766 * so this isn't really introducing any new behavior.)
2767 */
2768 if (OidIsValid(relid))
2769 {
2770 rel = try_relation_open(relid, AccessShareLock);
2771 if (rel == NULL)
2772 return NULL;
2773 context = deparse_context_for(RelationGetRelationName(rel), relid);
2774 }
2775 else
2776 context = NIL;
2777
2778 /* Deparse */
2779 str = deparse_expression_pretty(node, context, false, false,
2780 prettyFlags, 0);
2781
2782 if (rel != NULL)
2784
2785 return string_to_text(str);
2786}
2787
2788
2789/* ----------
2790 * pg_get_userbyid - Get a user name by roleid and
2791 * fallback to 'unknown (OID=n)'
2792 * ----------
2793 */
2794Datum
2796{
2797 Oid roleid = PG_GETARG_OID(0);
2798 Name result;
2801
2802 /*
2803 * Allocate space for the result
2804 */
2805 result = (Name) palloc(NAMEDATALEN);
2806 memset(NameStr(*result), 0, NAMEDATALEN);
2807
2808 /*
2809 * Get the pg_authid entry and print the result
2810 */
2813 {
2815 *result = role_rec->rolname;
2817 }
2818 else
2819 sprintf(NameStr(*result), "unknown (OID=%u)", roleid);
2820
2821 PG_RETURN_NAME(result);
2822}
2823
2824
2825/*
2826 * pg_get_serial_sequence
2827 * Get the name of the sequence used by an identity or serial column,
2828 * formatted suitably for passing to setval, nextval or currval.
2829 * First parameter is not treated as double-quoted, second parameter
2830 * is --- see documentation for reason.
2831 */
2832Datum
2834{
2835 text *tablename = PG_GETARG_TEXT_PP(0);
2838 Oid tableOid;
2839 char *column;
2843 ScanKeyData key[3];
2844 SysScanDesc scan;
2845 HeapTuple tup;
2846
2847 /* Look up table name. Can't lock it - we might not have privileges. */
2849 tableOid = RangeVarGetRelid(tablerv, NoLock, false);
2850
2851 /* Get the number of the column */
2853
2854 attnum = get_attnum(tableOid, column);
2856 ereport(ERROR,
2858 errmsg("column \"%s\" of relation \"%s\" does not exist",
2859 column, tablerv->relname)));
2860
2861 /* Search the dependency table for the dependent sequence */
2863
2864 ScanKeyInit(&key[0],
2868 ScanKeyInit(&key[1],
2871 ObjectIdGetDatum(tableOid));
2872 ScanKeyInit(&key[2],
2876
2878 NULL, 3, key);
2879
2880 while (HeapTupleIsValid(tup = systable_getnext(scan)))
2881 {
2883
2884 /*
2885 * Look for an auto dependency (serial column) or internal dependency
2886 * (identity column) of a sequence on a column. (We need the relkind
2887 * test because indexes can also have auto dependencies on columns.)
2888 */
2889 if (deprec->classid == RelationRelationId &&
2890 deprec->objsubid == 0 &&
2891 (deprec->deptype == DEPENDENCY_AUTO ||
2892 deprec->deptype == DEPENDENCY_INTERNAL) &&
2894 {
2895 sequenceId = deprec->objid;
2896 break;
2897 }
2898 }
2899
2900 systable_endscan(scan);
2902
2904 {
2905 char *result;
2906
2908
2910 }
2911
2913}
2914
2915
2916/*
2917 * pg_get_functiondef
2918 * Returns the complete "CREATE OR REPLACE FUNCTION ..." statement for
2919 * the specified function.
2920 *
2921 * Note: if you change the output format of this function, be careful not
2922 * to break psql's rules (in \ef and \sf) for identifying the start of the
2923 * function body. To wit: the function body starts on a line that begins with
2924 * "AS ", "BEGIN ", or "RETURN ", and no preceding line will look like that.
2925 */
2926Datum
2928{
2929 Oid funcid = PG_GETARG_OID(0);
2933 Form_pg_proc proc;
2934 bool isfunction;
2935 Datum tmp;
2936 bool isnull;
2937 const char *prosrc;
2938 const char *name;
2939 const char *nsp;
2941 int oldlen;
2942
2944
2945 /* Look up the function */
2949
2950 proc = (Form_pg_proc) GETSTRUCT(proctup);
2951 name = NameStr(proc->proname);
2952
2953 if (proc->prokind == PROKIND_AGGREGATE)
2954 ereport(ERROR,
2956 errmsg("\"%s\" is an aggregate function", name)));
2957
2958 isfunction = (proc->prokind != PROKIND_PROCEDURE);
2959
2960 /*
2961 * We always qualify the function name, to ensure the right function gets
2962 * replaced.
2963 */
2964 nsp = get_namespace_name_or_temp(proc->pronamespace);
2965 appendStringInfo(&buf, "CREATE OR REPLACE %s %s(",
2966 isfunction ? "FUNCTION" : "PROCEDURE",
2968 (void) print_function_arguments(&buf, proctup, false, true);
2969 appendStringInfoString(&buf, ")\n");
2970 if (isfunction)
2971 {
2972 appendStringInfoString(&buf, " RETURNS ");
2974 appendStringInfoChar(&buf, '\n');
2975 }
2976
2978
2979 appendStringInfo(&buf, " LANGUAGE %s\n",
2980 quote_identifier(get_language_name(proc->prolang, false)));
2981
2982 /* Emit some miscellaneous options on one line */
2983 oldlen = buf.len;
2984
2985 if (proc->prokind == PROKIND_WINDOW)
2986 appendStringInfoString(&buf, " WINDOW");
2987 switch (proc->provolatile)
2988 {
2990 appendStringInfoString(&buf, " IMMUTABLE");
2991 break;
2992 case PROVOLATILE_STABLE:
2993 appendStringInfoString(&buf, " STABLE");
2994 break;
2996 break;
2997 }
2998
2999 switch (proc->proparallel)
3000 {
3001 case PROPARALLEL_SAFE:
3002 appendStringInfoString(&buf, " PARALLEL SAFE");
3003 break;
3005 appendStringInfoString(&buf, " PARALLEL RESTRICTED");
3006 break;
3007 case PROPARALLEL_UNSAFE:
3008 break;
3009 }
3010
3011 if (proc->proisstrict)
3012 appendStringInfoString(&buf, " STRICT");
3013 if (proc->prosecdef)
3014 appendStringInfoString(&buf, " SECURITY DEFINER");
3015 if (proc->proleakproof)
3016 appendStringInfoString(&buf, " LEAKPROOF");
3017
3018 /* This code for the default cost and rows should match functioncmds.c */
3019 if (proc->prolang == INTERNALlanguageId ||
3020 proc->prolang == ClanguageId)
3021 procost = 1;
3022 else
3023 procost = 100;
3024 if (proc->procost != procost)
3025 appendStringInfo(&buf, " COST %g", proc->procost);
3026
3027 if (proc->prorows > 0 && proc->prorows != 1000)
3028 appendStringInfo(&buf, " ROWS %g", proc->prorows);
3029
3030 if (proc->prosupport)
3031 {
3032 Oid argtypes[1];
3033
3034 /*
3035 * We should qualify the support function's name if it wouldn't be
3036 * resolved by lookup in the current search path.
3037 */
3038 argtypes[0] = INTERNALOID;
3039 appendStringInfo(&buf, " SUPPORT %s",
3040 generate_function_name(proc->prosupport, 1,
3041 NIL, argtypes,
3042 false, NULL, false));
3043 }
3044
3045 if (oldlen != buf.len)
3046 appendStringInfoChar(&buf, '\n');
3047
3048 /* Emit any proconfig options, one per line */
3050 if (!isnull)
3051 {
3053 int i;
3054
3056 Assert(ARR_NDIM(a) == 1);
3057 Assert(ARR_LBOUND(a)[0] == 1);
3058
3059 for (i = 1; i <= ARR_DIMS(a)[0]; i++)
3060 {
3061 Datum d;
3062
3063 d = array_ref(a, 1, &i,
3064 -1 /* varlenarray */ ,
3065 -1 /* TEXT's typlen */ ,
3066 false /* TEXT's typbyval */ ,
3067 TYPALIGN_INT /* TEXT's typalign */ ,
3068 &isnull);
3069 if (!isnull)
3070 {
3072 char *pos;
3073
3074 pos = strchr(configitem, '=');
3075 if (pos == NULL)
3076 continue;
3077 *pos++ = '\0';
3078
3079 appendStringInfo(&buf, " SET %s TO ",
3081
3082 /*
3083 * Variables that are marked GUC_LIST_QUOTE were already fully
3084 * quoted by flatten_set_variable_args() before they were put
3085 * into the proconfig array. However, because the quoting
3086 * rules used there aren't exactly like SQL's, we have to
3087 * break the list value apart and then quote the elements as
3088 * string literals. (The elements may be double-quoted as-is,
3089 * but we can't just feed them to the SQL parser; it would do
3090 * the wrong thing with elements that are zero-length or
3091 * longer than NAMEDATALEN.) Also, we need a special case for
3092 * empty lists.
3093 *
3094 * Variables that are not so marked should just be emitted as
3095 * simple string literals. If the variable is not known to
3096 * guc.c, we'll do that; this makes it unsafe to use
3097 * GUC_LIST_QUOTE for extension variables.
3098 */
3100 {
3101 List *namelist;
3102 ListCell *lc;
3103
3104 /* Parse string into list of identifiers */
3105 if (!SplitGUCList(pos, ',', &namelist))
3106 {
3107 /* this shouldn't fail really */
3108 elog(ERROR, "invalid list syntax in proconfig item");
3109 }
3110 /* Special case: represent an empty list as NULL */
3111 if (namelist == NIL)
3112 appendStringInfoString(&buf, "NULL");
3113 foreach(lc, namelist)
3114 {
3115 char *curname = (char *) lfirst(lc);
3116
3118 if (lnext(namelist, lc))
3120 }
3121 }
3122 else
3124 appendStringInfoChar(&buf, '\n');
3125 }
3126 }
3127 }
3128
3129 /* And finally the function definition ... */
3131 if (proc->prolang == SQLlanguageId && !isnull)
3132 {
3134 }
3135 else
3136 {
3137 appendStringInfoString(&buf, "AS ");
3138
3140 if (!isnull)
3141 {
3143 appendStringInfoString(&buf, ", "); /* assume prosrc isn't null */
3144 }
3145
3147 prosrc = TextDatumGetCString(tmp);
3148
3149 /*
3150 * We always use dollar quoting. Figure out a suitable delimiter.
3151 *
3152 * Since the user is likely to be editing the function body string, we
3153 * shouldn't use a short delimiter that he might easily create a
3154 * conflict with. Hence prefer "$function$"/"$procedure$", but extend
3155 * if needed.
3156 */
3158 appendStringInfoChar(&dq, '$');
3159 appendStringInfoString(&dq, (isfunction ? "function" : "procedure"));
3160 while (strstr(prosrc, dq.data) != NULL)
3161 appendStringInfoChar(&dq, 'x');
3162 appendStringInfoChar(&dq, '$');
3163
3164 appendBinaryStringInfo(&buf, dq.data, dq.len);
3165 appendStringInfoString(&buf, prosrc);
3166 appendBinaryStringInfo(&buf, dq.data, dq.len);
3167 }
3168
3169 appendStringInfoChar(&buf, '\n');
3170
3172
3174}
3175
3176/*
3177 * pg_get_function_arguments
3178 * Get a nicely-formatted list of arguments for a function.
3179 * This is everything that would go between the parentheses in
3180 * CREATE FUNCTION.
3181 */
3182Datum
3201
3202/*
3203 * pg_get_function_identity_arguments
3204 * Get a formatted list of arguments for a function.
3205 * This is everything that would go between the parentheses in
3206 * ALTER FUNCTION, etc. In particular, don't print defaults.
3207 */
3208Datum
3227
3228/*
3229 * pg_get_function_result
3230 * Get a nicely-formatted version of the result type of a function.
3231 * This is what would appear after RETURNS in CREATE FUNCTION.
3232 */
3233Datum
3258
3259/*
3260 * Guts of pg_get_function_result: append the function's return type
3261 * to the specified buffer.
3262 */
3263static void
3265{
3267 int ntabargs = 0;
3269
3271
3272 if (proc->proretset)
3273 {
3274 /* It might be a table function; try to print the arguments */
3275 appendStringInfoString(&rbuf, "TABLE(");
3277 if (ntabargs > 0)
3279 else
3281 }
3282
3283 if (ntabargs == 0)
3284 {
3285 /* Not a table function, so do the normal thing */
3286 if (proc->proretset)
3287 appendStringInfoString(&rbuf, "SETOF ");
3288 appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
3289 }
3290
3291 appendBinaryStringInfo(buf, rbuf.data, rbuf.len);
3292}
3293
3294/*
3295 * Common code for pg_get_function_arguments and pg_get_function_result:
3296 * append the desired subset of arguments to buf. We print only TABLE
3297 * arguments when print_table_args is true, and all the others when it's false.
3298 * We print argument defaults only if print_defaults is true.
3299 * Function return value is the number of arguments printed.
3300 */
3301static int
3304{
3306 int numargs;
3307 Oid *argtypes;
3308 char **argnames;
3309 char *argmodes;
3310 int insertorderbyat = -1;
3311 int argsprinted;
3312 int inputargno;
3313 int nlackdefaults;
3314 List *argdefaults = NIL;
3316 int i;
3317
3318 numargs = get_func_arg_info(proctup,
3319 &argtypes, &argnames, &argmodes);
3320
3321 nlackdefaults = numargs;
3322 if (print_defaults && proc->pronargdefaults > 0)
3323 {
3325 bool isnull;
3326
3329 &isnull);
3330 if (!isnull)
3331 {
3332 char *str;
3333
3336 pfree(str);
3338 /* nlackdefaults counts only *input* arguments lacking defaults */
3339 nlackdefaults = proc->pronargs - list_length(argdefaults);
3340 }
3341 }
3342
3343 /* Check for special treatment of ordered-set aggregates */
3344 if (proc->prokind == PROKIND_AGGREGATE)
3345 {
3348
3351 elog(ERROR, "cache lookup failed for aggregate %u",
3352 proc->oid);
3354 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3355 insertorderbyat = agg->aggnumdirectargs;
3357 }
3358
3359 argsprinted = 0;
3360 inputargno = 0;
3361 for (i = 0; i < numargs; i++)
3362 {
3363 Oid argtype = argtypes[i];
3364 char *argname = argnames ? argnames[i] : NULL;
3366 const char *modename;
3367 bool isinput;
3368
3369 switch (argmode)
3370 {
3371 case PROARGMODE_IN:
3372
3373 /*
3374 * For procedures, explicitly mark all argument modes, so as
3375 * to avoid ambiguity with the SQL syntax for DROP PROCEDURE.
3376 */
3377 if (proc->prokind == PROKIND_PROCEDURE)
3378 modename = "IN ";
3379 else
3380 modename = "";
3381 isinput = true;
3382 break;
3383 case PROARGMODE_INOUT:
3384 modename = "INOUT ";
3385 isinput = true;
3386 break;
3387 case PROARGMODE_OUT:
3388 modename = "OUT ";
3389 isinput = false;
3390 break;
3392 modename = "VARIADIC ";
3393 isinput = true;
3394 break;
3395 case PROARGMODE_TABLE:
3396 modename = "";
3397 isinput = false;
3398 break;
3399 default:
3400 elog(ERROR, "invalid parameter mode '%c'", argmode);
3401 modename = NULL; /* keep compiler quiet */
3402 isinput = false;
3403 break;
3404 }
3405 if (isinput)
3406 inputargno++; /* this is a 1-based counter */
3407
3409 continue;
3410
3412 {
3413 if (argsprinted)
3415 appendStringInfoString(buf, "ORDER BY ");
3416 }
3417 else if (argsprinted)
3419
3421 if (argname && argname[0])
3422 appendStringInfo(buf, "%s ", quote_identifier(argname));
3425 {
3426 Node *expr;
3427
3429 expr = (Node *) lfirst(nextargdefault);
3431
3432 appendStringInfo(buf, " DEFAULT %s",
3433 deparse_expression(expr, NIL, false, false));
3434 }
3435 argsprinted++;
3436
3437 /* nasty hack: print the last arg twice for variadic ordered-set agg */
3438 if (argsprinted == insertorderbyat && i == numargs - 1)
3439 {
3440 i--;
3441 /* aggs shouldn't have defaults anyway, but just to be sure ... */
3442 print_defaults = false;
3443 }
3444 }
3445
3446 return argsprinted;
3447}
3448
3449static bool
3451{
3452 return (!argmodes
3456}
3457
3458/*
3459 * Append used transformed types to specified buffer
3460 */
3461static void
3463{
3464 Oid *trftypes;
3465 int ntypes;
3466
3467 ntypes = get_func_trftypes(proctup, &trftypes);
3468 if (ntypes > 0)
3469 {
3470 int i;
3471
3472 appendStringInfoString(buf, " TRANSFORM ");
3473 for (i = 0; i < ntypes; i++)
3474 {
3475 if (i != 0)
3477 appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
3478 }
3480 }
3481}
3482
3483/*
3484 * Get textual representation of a function argument's default value. The
3485 * second argument of this function is the argument number among all arguments
3486 * (i.e. proallargtypes, *not* proargtypes), starting with 1, because that's
3487 * how information_schema.sql uses it.
3488 */
3489Datum
3491{
3492 Oid funcid = PG_GETARG_OID(0);
3495 Form_pg_proc proc;
3496 int numargs;
3497 Oid *argtypes;
3498 char **argnames;
3499 char *argmodes;
3500 int i;
3502 Node *node;
3503 char *str;
3504 int nth_inputarg;
3506 bool isnull;
3507 int nth_default;
3508
3512
3513 numargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
3515 {
3518 }
3519
3520 nth_inputarg = 0;
3521 for (i = 0; i < nth_arg; i++)
3523 nth_inputarg++;
3524
3527 &isnull);
3528 if (isnull)
3529 {
3532 }
3533
3536 pfree(str);
3537
3538 proc = (Form_pg_proc) GETSTRUCT(proctup);
3539
3540 /*
3541 * Calculate index into proargdefaults: proargdefaults corresponds to the
3542 * last N input arguments, where N = pronargdefaults.
3543 */
3544 nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3545
3547 {
3550 }
3552 str = deparse_expression(node, NIL, false, false);
3553
3555
3557}
3558
3559static void
3561{
3562 int numargs;
3563 Oid *argtypes;
3564 char **argnames;
3565 char *argmodes;
3566 deparse_namespace dpns = {0};
3567 Datum tmp;
3568 Node *n;
3569
3571 numargs = get_func_arg_info(proctup,
3572 &argtypes, &argnames, &argmodes);
3573 dpns.numargs = numargs;
3574 dpns.argnames = argnames;
3575
3578
3579 if (IsA(n, List))
3580 {
3581 List *stmts;
3582 ListCell *lc;
3583
3584 stmts = linitial(castNode(List, n));
3585
3586 appendStringInfoString(buf, "BEGIN ATOMIC\n");
3587
3588 foreach(lc, stmts)
3589 {
3590 Query *query = lfirst_node(Query, lc);
3591
3592 /* It seems advisable to get at least AccessShareLock on rels */
3593 AcquireRewriteLocks(query, false, false);
3594 get_query_def(query, buf, list_make1(&dpns), NULL, false,
3598 }
3599
3601 }
3602 else
3603 {
3604 Query *query = castNode(Query, n);
3605
3606 /* It seems advisable to get at least AccessShareLock on rels */
3607 AcquireRewriteLocks(query, false, false);
3608 get_query_def(query, buf, list_make1(&dpns), NULL, false,
3609 0, WRAP_COLUMN_DEFAULT, 0);
3610 }
3611}
3612
3613Datum
3615{
3616 Oid funcid = PG_GETARG_OID(0);
3619 bool isnull;
3620
3622
3623 /* Look up the function */
3627
3629 if (isnull)
3630 {
3633 }
3634
3636
3638
3640}
3641
3642
3643/*
3644 * deparse_expression - General utility for deparsing expressions
3645 *
3646 * calls deparse_expression_pretty with all prettyPrinting disabled
3647 */
3648char *
3650 bool forceprefix, bool showimplicit)
3651{
3653 showimplicit, 0, 0);
3654}
3655
3656/* ----------
3657 * deparse_expression_pretty - General utility for deparsing expressions
3658 *
3659 * expr is the node tree to be deparsed. It must be a transformed expression
3660 * tree (ie, not the raw output of gram.y).
3661 *
3662 * dpcontext is a list of deparse_namespace nodes representing the context
3663 * for interpreting Vars in the node tree. It can be NIL if no Vars are
3664 * expected.
3665 *
3666 * forceprefix is true to force all Vars to be prefixed with their table names.
3667 *
3668 * showimplicit is true to force all implicit casts to be shown explicitly.
3669 *
3670 * Tries to pretty up the output according to prettyFlags and startIndent.
3671 *
3672 * The result is a palloc'd string.
3673 * ----------
3674 */
3675static char *
3677 bool forceprefix, bool showimplicit,
3678 int prettyFlags, int startIndent)
3679{
3681 deparse_context context;
3682
3684 context.buf = &buf;
3685 context.namespaces = dpcontext;
3686 context.resultDesc = NULL;
3687 context.targetList = NIL;
3688 context.windowClause = NIL;
3689 context.varprefix = forceprefix;
3690 context.prettyFlags = prettyFlags;
3692 context.indentLevel = startIndent;
3693 context.colNamesVisible = true;
3694 context.inGroupBy = false;
3695 context.varInOrderBy = false;
3696 context.appendparents = NULL;
3697
3698 get_rule_expr(expr, &context, showimplicit);
3699
3700 return buf.data;
3701}
3702
3703/* ----------
3704 * deparse_context_for - Build deparse context for a single relation
3705 *
3706 * Given the reference name (alias) and OID of a relation, build deparsing
3707 * context for an expression referencing only that relation (as varno 1,
3708 * varlevelsup 0). This is sufficient for many uses of deparse_expression.
3709 * ----------
3710 */
3711List *
3712deparse_context_for(const char *aliasname, Oid relid)
3713{
3716
3718
3719 /* Build a minimal RTE for the rel */
3721 rte->rtekind = RTE_RELATION;
3722 rte->relid = relid;
3723 rte->relkind = RELKIND_RELATION; /* no need for exactness here */
3724 rte->rellockmode = AccessShareLock;
3725 rte->alias = makeAlias(aliasname, NIL);
3726 rte->eref = rte->alias;
3727 rte->lateral = false;
3728 rte->inh = false;
3729 rte->inFromCl = true;
3730
3731 /* Build one-element rtable */
3732 dpns->rtable = list_make1(rte);
3733 dpns->subplans = NIL;
3734 dpns->ctes = NIL;
3735 dpns->appendrels = NULL;
3738
3739 /* Return a one-deep namespace stack */
3740 return list_make1(dpns);
3741}
3742
3743/*
3744 * deparse_context_for_plan_tree - Build deparse context for a Plan tree
3745 *
3746 * When deparsing an expression in a Plan tree, we use the plan's rangetable
3747 * to resolve names of simple Vars. The initialization of column names for
3748 * this is rather expensive if the rangetable is large, and it'll be the same
3749 * for every expression in the Plan tree; so we do it just once and re-use
3750 * the result of this function for each expression. (Note that the result
3751 * is not usable until set_deparse_context_plan() is applied to it.)
3752 *
3753 * In addition to the PlannedStmt, pass the per-RTE alias names
3754 * assigned by a previous call to select_rtable_names_for_explain.
3755 */
3756List *
3758{
3760
3762
3763 /* Initialize fields that stay the same across the whole plan tree */
3764 dpns->rtable = pstmt->rtable;
3765 dpns->rtable_names = rtable_names;
3766 dpns->subplans = pstmt->subplans;
3767 dpns->ctes = NIL;
3768 if (pstmt->appendRelations)
3769 {
3770 /* Set up the array, indexed by child relid */
3771 int ntables = list_length(dpns->rtable);
3772 ListCell *lc;
3773
3774 dpns->appendrels = (AppendRelInfo **)
3775 palloc0((ntables + 1) * sizeof(AppendRelInfo *));
3776 foreach(lc, pstmt->appendRelations)
3777 {
3779 Index crelid = appinfo->child_relid;
3780
3781 Assert(crelid > 0 && crelid <= ntables);
3782 Assert(dpns->appendrels[crelid] == NULL);
3783 dpns->appendrels[crelid] = appinfo;
3784 }
3785 }
3786 else
3787 dpns->appendrels = NULL; /* don't need it */
3788
3789 /*
3790 * Set up column name aliases, ignoring any join RTEs; they don't matter
3791 * because plan trees don't contain any join alias Vars.
3792 */
3794
3795 /* Return a one-deep namespace stack */
3796 return list_make1(dpns);
3797}
3798
3799/*
3800 * set_deparse_context_plan - Specify Plan node containing expression
3801 *
3802 * When deparsing an expression in a Plan tree, we might have to resolve
3803 * OUTER_VAR, INNER_VAR, or INDEX_VAR references. To do this, the caller must
3804 * provide the parent Plan node. Then OUTER_VAR and INNER_VAR references
3805 * can be resolved by drilling down into the left and right child plans.
3806 * Similarly, INDEX_VAR references can be resolved by reference to the
3807 * indextlist given in a parent IndexOnlyScan node, or to the scan tlist in
3808 * ForeignScan and CustomScan nodes. (Note that we don't currently support
3809 * deparsing of indexquals in regular IndexScan or BitmapIndexScan nodes;
3810 * for those, we can only deparse the indexqualorig fields, which won't
3811 * contain INDEX_VAR Vars.)
3812 *
3813 * The ancestors list is a list of the Plan's parent Plan and SubPlan nodes,
3814 * the most-closely-nested first. This is needed to resolve PARAM_EXEC
3815 * Params. Note we assume that all the Plan nodes share the same rtable.
3816 *
3817 * For a ModifyTable plan, we might also need to resolve references to OLD/NEW
3818 * variables in the RETURNING list, so we copy the alias names of the OLD and
3819 * NEW rows from the ModifyTable plan node.
3820 *
3821 * Once this function has been called, deparse_expression() can be called on
3822 * subsidiary expression(s) of the specified Plan node. To deparse
3823 * expressions of a different Plan node in the same Plan tree, re-call this
3824 * function to identify the new parent Plan node.
3825 *
3826 * The result is the same List passed in; this is a notational convenience.
3827 */
3828List *
3830{
3832
3833 /* Should always have one-entry namespace list for Plan deparsing */
3836
3837 /* Set our attention on the specific plan node passed in */
3838 dpns->ancestors = ancestors;
3840
3841 /* For ModifyTable, set aliases for OLD and NEW in RETURNING */
3842 if (IsA(plan, ModifyTable))
3843 {
3844 dpns->ret_old_alias = ((ModifyTable *) plan)->returningOldAlias;
3845 dpns->ret_new_alias = ((ModifyTable *) plan)->returningNewAlias;
3846 }
3847
3848 return dpcontext;
3849}
3850
3851/*
3852 * select_rtable_names_for_explain - Select RTE aliases for EXPLAIN
3853 *
3854 * Determine the relation aliases we'll use during an EXPLAIN operation.
3855 * This is just a frontend to set_rtable_names. We have to expose the aliases
3856 * to EXPLAIN because EXPLAIN needs to know the right alias names to print.
3857 */
3858List *
3860{
3862
3863 memset(&dpns, 0, sizeof(dpns));
3864 dpns.rtable = rtable;
3865 dpns.subplans = NIL;
3866 dpns.ctes = NIL;
3867 dpns.appendrels = NULL;
3869 /* We needn't bother computing column aliases yet */
3870
3871 return dpns.rtable_names;
3872}
3873
3874/*
3875 * set_rtable_names: select RTE aliases to be used in printing a query
3876 *
3877 * We fill in dpns->rtable_names with a list of names that is one-for-one with
3878 * the already-filled dpns->rtable list. Each RTE name is unique among those
3879 * in the new namespace plus any ancestor namespaces listed in
3880 * parent_namespaces.
3881 *
3882 * If rels_used isn't NULL, only RTE indexes listed in it are given aliases.
3883 *
3884 * Note that this function is only concerned with relation names, not column
3885 * names.
3886 */
3887static void
3890{
3892 HTAB *names_hash;
3894 bool found;
3895 int rtindex;
3896 ListCell *lc;
3897
3898 dpns->rtable_names = NIL;
3899 /* nothing more to do if empty rtable */
3900 if (dpns->rtable == NIL)
3901 return;
3902
3903 /*
3904 * We use a hash table to hold known names, so that this process is O(N)
3905 * not O(N^2) for N names.
3906 */
3907 hash_ctl.keysize = NAMEDATALEN;
3908 hash_ctl.entrysize = sizeof(NameHashEntry);
3910 names_hash = hash_create("set_rtable_names names",
3911 list_length(dpns->rtable),
3912 &hash_ctl,
3914
3915 /* Preload the hash table with names appearing in parent_namespaces */
3916 foreach(lc, parent_namespaces)
3917 {
3919 ListCell *lc2;
3920
3921 foreach(lc2, olddpns->rtable_names)
3922 {
3923 char *oldname = (char *) lfirst(lc2);
3924
3925 if (oldname == NULL)
3926 continue;
3927 hentry = (NameHashEntry *) hash_search(names_hash,
3928 oldname,
3929 HASH_ENTER,
3930 &found);
3931 /* we do not complain about duplicate names in parent namespaces */
3932 hentry->counter = 0;
3933 }
3934 }
3935
3936 /* Now we can scan the rtable */
3937 rtindex = 1;
3938 foreach(lc, dpns->rtable)
3939 {
3941 char *refname;
3942
3943 /* Just in case this takes an unreasonable amount of time ... */
3945
3946 if (rels_used && !bms_is_member(rtindex, rels_used))
3947 {
3948 /* Ignore unreferenced RTE */
3949 refname = NULL;
3950 }
3951 else if (rte->alias)
3952 {
3953 /* If RTE has a user-defined alias, prefer that */
3954 refname = rte->alias->aliasname;
3955 }
3956 else if (rte->rtekind == RTE_RELATION)
3957 {
3958 /* Use the current actual name of the relation */
3959 refname = get_rel_name(rte->relid);
3960 }
3961 else if (rte->rtekind == RTE_JOIN)
3962 {
3963 /* Unnamed join has no refname */
3964 refname = NULL;
3965 }
3966 else
3967 {
3968 /* Otherwise use whatever the parser assigned */
3969 refname = rte->eref->aliasname;
3970 }
3971
3972 /*
3973 * If the selected name isn't unique, append digits to make it so, and
3974 * make a new hash entry for it once we've got a unique name. For a
3975 * very long input name, we might have to truncate to stay within
3976 * NAMEDATALEN.
3977 */
3978 if (refname)
3979 {
3980 hentry = (NameHashEntry *) hash_search(names_hash,
3981 refname,
3982 HASH_ENTER,
3983 &found);
3984 if (found)
3985 {
3986 /* Name already in use, must choose a new one */
3987 int refnamelen = strlen(refname);
3988 char *modname = (char *) palloc(refnamelen + 16);
3990
3991 do
3992 {
3993 hentry->counter++;
3994 for (;;)
3995 {
3996 memcpy(modname, refname, refnamelen);
3997 sprintf(modname + refnamelen, "_%d", hentry->counter);
3998 if (strlen(modname) < NAMEDATALEN)
3999 break;
4000 /* drop chars from refname to keep all the digits */
4002 refnamelen - 1);
4003 }
4004 hentry2 = (NameHashEntry *) hash_search(names_hash,
4005 modname,
4006 HASH_ENTER,
4007 &found);
4008 } while (found);
4009 hentry2->counter = 0; /* init new hash entry */
4010 refname = modname;
4011 }
4012 else
4013 {
4014 /* Name not previously used, need only initialize hentry */
4015 hentry->counter = 0;
4016 }
4017 }
4018
4019 dpns->rtable_names = lappend(dpns->rtable_names, refname);
4020 rtindex++;
4021 }
4022
4023 hash_destroy(names_hash);
4024}
4025
4026/*
4027 * set_deparse_for_query: set up deparse_namespace for deparsing a Query tree
4028 *
4029 * For convenience, this is defined to initialize the deparse_namespace struct
4030 * from scratch.
4031 */
4032static void
4035{
4036 ListCell *lc;
4037 ListCell *lc2;
4038
4039 /* Initialize *dpns and fill rtable/ctes links */
4040 memset(dpns, 0, sizeof(deparse_namespace));
4041 dpns->rtable = query->rtable;
4042 dpns->subplans = NIL;
4043 dpns->ctes = query->cteList;
4044 dpns->appendrels = NULL;
4045 dpns->ret_old_alias = query->returningOldAlias;
4046 dpns->ret_new_alias = query->returningNewAlias;
4047
4048 /* Assign a unique relation alias to each RTE */
4050
4051 /* Initialize dpns->rtable_columns to contain zeroed structs */
4052 dpns->rtable_columns = NIL;
4053 while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
4054 dpns->rtable_columns = lappend(dpns->rtable_columns,
4055 palloc0(sizeof(deparse_columns)));
4056
4057 /* If it's a utility query, it won't have a jointree */
4058 if (query->jointree)
4059 {
4060 /* Detect whether global uniqueness of USING names is needed */
4061 dpns->unique_using =
4063
4064 /*
4065 * Select names for columns merged by USING, via a recursive pass over
4066 * the query jointree.
4067 */
4068 set_using_names(dpns, (Node *) query->jointree, NIL);
4069 }
4070
4071 /*
4072 * Now assign remaining column aliases for each RTE. We do this in a
4073 * linear scan of the rtable, so as to process RTEs whether or not they
4074 * are in the jointree (we mustn't miss NEW.*, INSERT target relations,
4075 * etc). JOIN RTEs must be processed after their children, but this is
4076 * okay because they appear later in the rtable list than their children
4077 * (cf Asserts in identify_join_columns()).
4078 */
4079 forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
4080 {
4083
4084 if (rte->rtekind == RTE_JOIN)
4086 else
4088 }
4089}
4090
4091/*
4092 * set_simple_column_names: fill in column aliases for non-query situations
4093 *
4094 * This handles EXPLAIN and cases where we only have relation RTEs. Without
4095 * a join tree, we can't do anything smart about join RTEs, but we don't
4096 * need to, because EXPLAIN should never see join alias Vars anyway.
4097 * If we find a join RTE we'll just skip it, leaving its deparse_columns
4098 * struct all-zero. If somehow we try to deparse a join alias Var, we'll
4099 * error out cleanly because the struct's num_cols will be zero.
4100 */
4101static void
4103{
4104 ListCell *lc;
4105 ListCell *lc2;
4106
4107 /* Initialize dpns->rtable_columns to contain zeroed structs */
4108 dpns->rtable_columns = NIL;
4109 while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
4110 dpns->rtable_columns = lappend(dpns->rtable_columns,
4111 palloc0(sizeof(deparse_columns)));
4112
4113 /* Assign unique column aliases within each non-join RTE */
4114 forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
4115 {
4118
4119 if (rte->rtekind != RTE_JOIN)
4121 }
4122}
4123
4124/*
4125 * has_dangerous_join_using: search jointree for unnamed JOIN USING
4126 *
4127 * Merged columns of a JOIN USING may act differently from either of the input
4128 * columns, either because they are merged with COALESCE (in a FULL JOIN) or
4129 * because an implicit coercion of the underlying input column is required.
4130 * In such a case the column must be referenced as a column of the JOIN not as
4131 * a column of either input. And this is problematic if the join is unnamed
4132 * (alias-less): we cannot qualify the column's name with an RTE name, since
4133 * there is none. (Forcibly assigning an alias to the join is not a solution,
4134 * since that will prevent legal references to tables below the join.)
4135 * To ensure that every column in the query is unambiguously referenceable,
4136 * we must assign such merged columns names that are globally unique across
4137 * the whole query, aliasing other columns out of the way as necessary.
4138 *
4139 * Because the ensuing re-aliasing is fairly damaging to the readability of
4140 * the query, we don't do this unless we have to. So, we must pre-scan
4141 * the join tree to see if we have to, before starting set_using_names().
4142 */
4143static bool
4145{
4146 if (IsA(jtnode, RangeTblRef))
4147 {
4148 /* nothing to do here */
4149 }
4150 else if (IsA(jtnode, FromExpr))
4151 {
4152 FromExpr *f = (FromExpr *) jtnode;
4153 ListCell *lc;
4154
4155 foreach(lc, f->fromlist)
4156 {
4158 return true;
4159 }
4160 }
4161 else if (IsA(jtnode, JoinExpr))
4162 {
4163 JoinExpr *j = (JoinExpr *) jtnode;
4164
4165 /* Is it an unnamed JOIN with USING? */
4166 if (j->alias == NULL && j->usingClause)
4167 {
4168 /*
4169 * Yes, so check each join alias var to see if any of them are not
4170 * simple references to underlying columns. If so, we have a
4171 * dangerous situation and must pick unique aliases.
4172 */
4173 RangeTblEntry *jrte = rt_fetch(j->rtindex, dpns->rtable);
4174
4175 /* We need only examine the merged columns */
4176 for (int i = 0; i < jrte->joinmergedcols; i++)
4177 {
4178 Node *aliasvar = list_nth(jrte->joinaliasvars, i);
4179
4180 if (!IsA(aliasvar, Var))
4181 return true;
4182 }
4183 }
4184
4185 /* Nope, but inspect children */
4186 if (has_dangerous_join_using(dpns, j->larg))
4187 return true;
4188 if (has_dangerous_join_using(dpns, j->rarg))
4189 return true;
4190 }
4191 else
4192 elog(ERROR, "unrecognized node type: %d",
4193 (int) nodeTag(jtnode));
4194 return false;
4195}
4196
4197/*
4198 * set_using_names: select column aliases to be used for merged USING columns
4199 *
4200 * We do this during a recursive descent of the query jointree.
4201 * dpns->unique_using must already be set to determine the global strategy.
4202 *
4203 * Column alias info is saved in the dpns->rtable_columns list, which is
4204 * assumed to be filled with pre-zeroed deparse_columns structs.
4205 *
4206 * parentUsing is a list of all USING aliases assigned in parent joins of
4207 * the current jointree node. (The passed-in list must not be modified.)
4208 *
4209 * Note that we do not use per-deparse_columns hash tables in this function.
4210 * The number of names that need to be assigned should be small enough that
4211 * we don't need to trouble with that.
4212 */
4213static void
4215{
4216 if (IsA(jtnode, RangeTblRef))
4217 {
4218 /* nothing to do now */
4219 }
4220 else if (IsA(jtnode, FromExpr))
4221 {
4222 FromExpr *f = (FromExpr *) jtnode;
4223 ListCell *lc;
4224
4225 foreach(lc, f->fromlist)
4226 set_using_names(dpns, (Node *) lfirst(lc), parentUsing);
4227 }
4228 else if (IsA(jtnode, JoinExpr))
4229 {
4230 JoinExpr *j = (JoinExpr *) jtnode;
4231 RangeTblEntry *rte = rt_fetch(j->rtindex, dpns->rtable);
4233 int *leftattnos;
4234 int *rightattnos;
4237 int i;
4238 ListCell *lc;
4239
4240 /* Get info about the shape of the join */
4242 leftattnos = colinfo->leftattnos;
4243 rightattnos = colinfo->rightattnos;
4244
4245 /* Look up the not-yet-filled-in child deparse_columns structs */
4248
4249 /*
4250 * If this join is unnamed, then we cannot substitute new aliases at
4251 * this level, so any name requirements pushed down to here must be
4252 * pushed down again to the children.
4253 */
4254 if (rte->alias == NULL)
4255 {
4256 for (i = 0; i < colinfo->num_cols; i++)
4257 {
4258 char *colname = colinfo->colnames[i];
4259
4260 if (colname == NULL)
4261 continue;
4262
4263 /* Push down to left column, unless it's a system column */
4264 if (leftattnos[i] > 0)
4265 {
4267 leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4268 }
4269
4270 /* Same on the righthand side */
4271 if (rightattnos[i] > 0)
4272 {
4274 rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4275 }
4276 }
4277 }
4278
4279 /*
4280 * If there's a USING clause, select the USING column names and push
4281 * those names down to the children. We have two strategies:
4282 *
4283 * If dpns->unique_using is true, we force all USING names to be
4284 * unique across the whole query level. In principle we'd only need
4285 * the names of dangerous USING columns to be globally unique, but to
4286 * safely assign all USING names in a single pass, we have to enforce
4287 * the same uniqueness rule for all of them. However, if a USING
4288 * column's name has been pushed down from the parent, we should use
4289 * it as-is rather than making a uniqueness adjustment. This is
4290 * necessary when we're at an unnamed join, and it creates no risk of
4291 * ambiguity. Also, if there's a user-written output alias for a
4292 * merged column, we prefer to use that rather than the input name;
4293 * this simplifies the logic and seems likely to lead to less aliasing
4294 * overall.
4295 *
4296 * If dpns->unique_using is false, we only need USING names to be
4297 * unique within their own join RTE. We still need to honor
4298 * pushed-down names, though.
4299 *
4300 * Though significantly different in results, these two strategies are
4301 * implemented by the same code, with only the difference of whether
4302 * to put assigned names into dpns->using_names.
4303 */
4304 if (j->usingClause)
4305 {
4306 /* Copy the input parentUsing list so we don't modify it */
4307 parentUsing = list_copy(parentUsing);
4308
4309 /* USING names must correspond to the first join output columns */
4311 i = 0;
4312 foreach(lc, j->usingClause)
4313 {
4314 char *colname = strVal(lfirst(lc));
4315
4316 /* Assert it's a merged column */
4317 Assert(leftattnos[i] != 0 && rightattnos[i] != 0);
4318
4319 /* Adopt passed-down name if any, else select unique name */
4320 if (colinfo->colnames[i] != NULL)
4321 colname = colinfo->colnames[i];
4322 else
4323 {
4324 /* Prefer user-written output alias if any */
4325 if (rte->alias && i < list_length(rte->alias->colnames))
4326 colname = strVal(list_nth(rte->alias->colnames, i));
4327 /* Make it appropriately unique */
4328 colname = make_colname_unique(colname, dpns, colinfo);
4329 if (dpns->unique_using)
4330 dpns->using_names = lappend(dpns->using_names,
4331 colname);
4332 /* Save it as output column name, too */
4333 colinfo->colnames[i] = colname;
4334 }
4335
4336 /* Remember selected names for use later */
4337 colinfo->usingNames = lappend(colinfo->usingNames, colname);
4338 parentUsing = lappend(parentUsing, colname);
4339
4340 /* Push down to left column, unless it's a system column */
4341 if (leftattnos[i] > 0)
4342 {
4344 leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4345 }
4346
4347 /* Same on the righthand side */
4348 if (rightattnos[i] > 0)
4349 {
4351 rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4352 }
4353
4354 i++;
4355 }
4356 }
4357
4358 /* Mark child deparse_columns structs with correct parentUsing info */
4359 leftcolinfo->parentUsing = parentUsing;
4360 rightcolinfo->parentUsing = parentUsing;
4361
4362 /* Now recursively assign USING column names in children */
4363 set_using_names(dpns, j->larg, parentUsing);
4364 set_using_names(dpns, j->rarg, parentUsing);
4365 }
4366 else
4367 elog(ERROR, "unrecognized node type: %d",
4368 (int) nodeTag(jtnode));
4369}
4370
4371/*
4372 * set_relation_column_names: select column aliases for a non-join RTE
4373 *
4374 * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
4375 * If any colnames entries are already filled in, those override local
4376 * choices.
4377 */
4378static void
4381{
4382 int ncolumns;
4383 char **real_colnames;
4384 bool changed_any;
4385 int noldcolumns;
4386 int i;
4387 int j;
4388
4389 /*
4390 * Construct an array of the current "real" column names of the RTE.
4391 * real_colnames[] will be indexed by physical column number, with NULL
4392 * entries for dropped columns.
4393 */
4394 if (rte->rtekind == RTE_RELATION)
4395 {
4396 /* Relation --- look to the system catalogs for up-to-date info */
4397 Relation rel;
4398 TupleDesc tupdesc;
4399
4400 rel = relation_open(rte->relid, AccessShareLock);
4401 tupdesc = RelationGetDescr(rel);
4402
4403 ncolumns = tupdesc->natts;
4404 real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4405
4406 for (i = 0; i < ncolumns; i++)
4407 {
4408 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4409
4410 if (attr->attisdropped)
4411 real_colnames[i] = NULL;
4412 else
4413 real_colnames[i] = pstrdup(NameStr(attr->attname));
4414 }
4416 }
4417 else
4418 {
4419 /* Otherwise get the column names from eref or expandRTE() */
4420 List *colnames;
4421 ListCell *lc;
4422
4423 /*
4424 * Functions returning composites have the annoying property that some
4425 * of the composite type's columns might have been dropped since the
4426 * query was parsed. If possible, use expandRTE() to handle that
4427 * case, since it has the tedious logic needed to find out about
4428 * dropped columns. However, if we're explaining a plan, then we
4429 * don't have rte->functions because the planner thinks that won't be
4430 * needed later, and that breaks expandRTE(). So in that case we have
4431 * to rely on rte->eref, which may lead us to report a dropped
4432 * column's old name; that seems close enough for EXPLAIN's purposes.
4433 *
4434 * For non-RELATION, non-FUNCTION RTEs, we can just look at rte->eref,
4435 * which should be sufficiently up-to-date: no other RTE types can
4436 * have columns get dropped from under them after parsing.
4437 */
4438 if (rte->rtekind == RTE_FUNCTION && rte->functions != NIL)
4439 {
4440 /* Since we're not creating Vars, rtindex etc. don't matter */
4442 true /* include dropped */ , &colnames, NULL);
4443 }
4444 else
4445 colnames = rte->eref->colnames;
4446
4447 ncolumns = list_length(colnames);
4448 real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4449
4450 i = 0;
4451 foreach(lc, colnames)
4452 {
4453 /*
4454 * If the column name we find here is an empty string, then it's a
4455 * dropped column, so change to NULL.
4456 */
4457 char *cname = strVal(lfirst(lc));
4458
4459 if (cname[0] == '\0')
4460 cname = NULL;
4462 i++;
4463 }
4464 }
4465
4466 /*
4467 * Ensure colinfo->colnames has a slot for each column. (It could be long
4468 * enough already, if we pushed down a name for the last column.) Note:
4469 * it's possible that there are now more columns than there were when the
4470 * query was parsed, ie colnames could be longer than rte->eref->colnames.
4471 * We must assign unique aliases to the new columns too, else there could
4472 * be unresolved conflicts when the view/rule is reloaded.
4473 */
4475 Assert(colinfo->num_cols == ncolumns);
4476
4477 /*
4478 * Make sufficiently large new_colnames and is_new_col arrays, too.
4479 *
4480 * Note: because we leave colinfo->num_new_cols zero until after the loop,
4481 * colname_is_unique will not consult that array, which is fine because it
4482 * would only be duplicate effort.
4483 */
4484 colinfo->new_colnames = (char **) palloc(ncolumns * sizeof(char *));
4485 colinfo->is_new_col = (bool *) palloc(ncolumns * sizeof(bool));
4486
4487 /* If the RTE is wide enough, use a hash table to avoid O(N^2) costs */
4489
4490 /*
4491 * Scan the columns, select a unique alias for each one, and store it in
4492 * colinfo->colnames and colinfo->new_colnames. The former array has NULL
4493 * entries for dropped columns, the latter omits them. Also mark
4494 * new_colnames entries as to whether they are new since parse time; this
4495 * is the case for entries beyond the length of rte->eref->colnames.
4496 */
4497 noldcolumns = list_length(rte->eref->colnames);
4498 changed_any = false;
4499 j = 0;
4500 for (i = 0; i < ncolumns; i++)
4501 {
4502 char *real_colname = real_colnames[i];
4503 char *colname = colinfo->colnames[i];
4504
4505 /* Skip dropped columns */
4506 if (real_colname == NULL)
4507 {
4508 Assert(colname == NULL); /* colnames[i] is already NULL */
4509 continue;
4510 }
4511
4512 /* If alias already assigned, that's what to use */
4513 if (colname == NULL)
4514 {
4515 /* If user wrote an alias, prefer that over real column name */
4516 if (rte->alias && i < list_length(rte->alias->colnames))
4517 colname = strVal(list_nth(rte->alias->colnames, i));
4518 else
4519 colname = real_colname;
4520
4521 /* Unique-ify and insert into colinfo */
4522 colname = make_colname_unique(colname, dpns, colinfo);
4523
4524 colinfo->colnames[i] = colname;
4525 add_to_names_hash(colinfo, colname);
4526 }
4527
4528 /* Put names of non-dropped columns in new_colnames[] too */
4529 colinfo->new_colnames[j] = colname;
4530 /* And mark them as new or not */
4531 colinfo->is_new_col[j] = (i >= noldcolumns);
4532 j++;
4533
4534 /* Remember if any assigned aliases differ from "real" name */
4535 if (!changed_any && strcmp(colname, real_colname) != 0)
4536 changed_any = true;
4537 }
4538
4539 /* We're now done needing the colinfo's names_hash */
4541
4542 /*
4543 * Set correct length for new_colnames[] array. (Note: if columns have
4544 * been added, colinfo->num_cols includes them, which is not really quite
4545 * right but is harmless, since any new columns must be at the end where
4546 * they won't affect varattnos of pre-existing columns.)
4547 */
4548 colinfo->num_new_cols = j;
4549
4550 /*
4551 * For a relation RTE, we need only print the alias column names if any
4552 * are different from the underlying "real" names. For a function RTE,
4553 * always emit a complete column alias list; this is to protect against
4554 * possible instability of the default column names (eg, from altering
4555 * parameter names). For tablefunc RTEs, we never print aliases, because
4556 * the column names are part of the clause itself. For other RTE types,
4557 * print if we changed anything OR if there were user-written column
4558 * aliases (since the latter would be part of the underlying "reality").
4559 */
4560 if (rte->rtekind == RTE_RELATION)
4561 colinfo->printaliases = changed_any;
4562 else if (rte->rtekind == RTE_FUNCTION)
4563 colinfo->printaliases = true;
4564 else if (rte->rtekind == RTE_TABLEFUNC)
4565 colinfo->printaliases = false;
4566 else if (rte->alias && rte->alias->colnames != NIL)
4567 colinfo->printaliases = true;
4568 else
4569 colinfo->printaliases = changed_any;
4570}
4571
4572/*
4573 * set_join_column_names: select column aliases for a join RTE
4574 *
4575 * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
4576 * If any colnames entries are already filled in, those override local
4577 * choices. Also, names for USING columns were already chosen by
4578 * set_using_names(). We further expect that column alias selection has been
4579 * completed for both input RTEs.
4580 */
4581static void
4584{
4587 bool changed_any;
4588 int noldcolumns;
4589 int nnewcolumns;
4592 int i;
4593 int j;
4594 int ic;
4595 int jc;
4596
4597 /* Look up the previously-filled-in child deparse_columns structs */
4600
4601 /*
4602 * Ensure colinfo->colnames has a slot for each column. (It could be long
4603 * enough already, if we pushed down a name for the last column.) Note:
4604 * it's possible that one or both inputs now have more columns than there
4605 * were when the query was parsed, but we'll deal with that below. We
4606 * only need entries in colnames for pre-existing columns.
4607 */
4608 noldcolumns = list_length(rte->eref->colnames);
4610 Assert(colinfo->num_cols == noldcolumns);
4611
4612 /* If the RTE is wide enough, use a hash table to avoid O(N^2) costs */
4614
4615 /*
4616 * Scan the join output columns, select an alias for each one, and store
4617 * it in colinfo->colnames. If there are USING columns, set_using_names()
4618 * already selected their names, so we can start the loop at the first
4619 * non-merged column.
4620 */
4621 changed_any = false;
4622 for (i = list_length(colinfo->usingNames); i < noldcolumns; i++)
4623 {
4624 char *colname = colinfo->colnames[i];
4625 char *real_colname;
4626
4627 /* Join column must refer to at least one input column */
4628 Assert(colinfo->leftattnos[i] != 0 || colinfo->rightattnos[i] != 0);
4629
4630 /* Get the child column name */
4631 if (colinfo->leftattnos[i] > 0)
4632 real_colname = leftcolinfo->colnames[colinfo->leftattnos[i] - 1];
4633 else if (colinfo->rightattnos[i] > 0)
4634 real_colname = rightcolinfo->colnames[colinfo->rightattnos[i] - 1];
4635 else
4636 {
4637 /* We're joining system columns --- use eref name */
4638 real_colname = strVal(list_nth(rte->eref->colnames, i));
4639 }
4640
4641 /* If child col has been dropped, no need to assign a join colname */
4642 if (real_colname == NULL)
4643 {
4644 colinfo->colnames[i] = NULL;
4645 continue;
4646 }
4647
4648 /* In an unnamed join, just report child column names as-is */
4649 if (rte->alias == NULL)
4650 {
4651 colinfo->colnames[i] = real_colname;
4653 continue;
4654 }
4655
4656 /* If alias already assigned, that's what to use */
4657 if (colname == NULL)
4658 {
4659 /* If user wrote an alias, prefer that over real column name */
4660 if (rte->alias && i < list_length(rte->alias->colnames))
4661 colname = strVal(list_nth(rte->alias->colnames, i));
4662 else
4663 colname = real_colname;
4664
4665 /* Unique-ify and insert into colinfo */
4666 colname = make_colname_unique(colname, dpns, colinfo);
4667
4668 colinfo->colnames[i] = colname;
4669 add_to_names_hash(colinfo, colname);
4670 }
4671
4672 /* Remember if any assigned aliases differ from "real" name */
4673 if (!changed_any && strcmp(colname, real_colname) != 0)
4674 changed_any = true;
4675 }
4676
4677 /*
4678 * Calculate number of columns the join would have if it were re-parsed
4679 * now, and create storage for the new_colnames and is_new_col arrays.
4680 *
4681 * Note: colname_is_unique will be consulting new_colnames[] during the
4682 * loops below, so its not-yet-filled entries must be zeroes.
4683 */
4684 nnewcolumns = leftcolinfo->num_new_cols + rightcolinfo->num_new_cols -
4685 list_length(colinfo->usingNames);
4686 colinfo->num_new_cols = nnewcolumns;
4687 colinfo->new_colnames = (char **) palloc0(nnewcolumns * sizeof(char *));
4688 colinfo->is_new_col = (bool *) palloc0(nnewcolumns * sizeof(bool));
4689
4690 /*
4691 * Generating the new_colnames array is a bit tricky since any new columns
4692 * added since parse time must be inserted in the right places. This code
4693 * must match the parser, which will order a join's columns as merged
4694 * columns first (in USING-clause order), then non-merged columns from the
4695 * left input (in attnum order), then non-merged columns from the right
4696 * input (ditto). If one of the inputs is itself a join, its columns will
4697 * be ordered according to the same rule, which means newly-added columns
4698 * might not be at the end. We can figure out what's what by consulting
4699 * the leftattnos and rightattnos arrays plus the input is_new_col arrays.
4700 *
4701 * In these loops, i indexes leftattnos/rightattnos (so it's join varattno
4702 * less one), j indexes new_colnames/is_new_col, and ic/jc have similar
4703 * meanings for the current child RTE.
4704 */
4705
4706 /* Handle merged columns; they are first and can't be new */
4707 i = j = 0;
4708 while (i < noldcolumns &&
4709 colinfo->leftattnos[i] != 0 &&
4710 colinfo->rightattnos[i] != 0)
4711 {
4712 /* column name is already determined and known unique */
4713 colinfo->new_colnames[j] = colinfo->colnames[i];
4714 colinfo->is_new_col[j] = false;
4715
4716 /* build bitmapsets of child attnums of merged columns */
4717 if (colinfo->leftattnos[i] > 0)
4718 leftmerged = bms_add_member(leftmerged, colinfo->leftattnos[i]);
4719 if (colinfo->rightattnos[i] > 0)
4721
4722 i++, j++;
4723 }
4724
4725 /* Handle non-merged left-child columns */
4726 ic = 0;
4727 for (jc = 0; jc < leftcolinfo->num_new_cols; jc++)
4728 {
4729 char *child_colname = leftcolinfo->new_colnames[jc];
4730
4731 if (!leftcolinfo->is_new_col[jc])
4732 {
4733 /* Advance ic to next non-dropped old column of left child */
4734 while (ic < leftcolinfo->num_cols &&
4735 leftcolinfo->colnames[ic] == NULL)
4736 ic++;
4737 Assert(ic < leftcolinfo->num_cols);
4738 ic++;
4739 /* If it is a merged column, we already processed it */
4741 continue;
4742 /* Else, advance i to the corresponding existing join column */
4743 while (i < colinfo->num_cols &&
4744 colinfo->colnames[i] == NULL)
4745 i++;
4746 Assert(i < colinfo->num_cols);
4747 Assert(ic == colinfo->leftattnos[i]);
4748 /* Use the already-assigned name of this column */
4749 colinfo->new_colnames[j] = colinfo->colnames[i];
4750 i++;
4751 }
4752 else
4753 {
4754 /*
4755 * Unique-ify the new child column name and assign, unless we're
4756 * in an unnamed join, in which case just copy
4757 */
4758 if (rte->alias != NULL)
4759 {
4760 colinfo->new_colnames[j] =
4762 if (!changed_any &&
4763 strcmp(colinfo->new_colnames[j], child_colname) != 0)
4764 changed_any = true;
4765 }
4766 else
4767 colinfo->new_colnames[j] = child_colname;
4768 add_to_names_hash(colinfo, colinfo->new_colnames[j]);
4769 }
4770
4771 colinfo->is_new_col[j] = leftcolinfo->is_new_col[jc];
4772 j++;
4773 }
4774
4775 /* Handle non-merged right-child columns in exactly the same way */
4776 ic = 0;
4777 for (jc = 0; jc < rightcolinfo->num_new_cols; jc++)
4778 {
4779 char *child_colname = rightcolinfo->new_colnames[jc];
4780
4781 if (!rightcolinfo->is_new_col[jc])
4782 {
4783 /* Advance ic to next non-dropped old column of right child */
4784 while (ic < rightcolinfo->num_cols &&
4785 rightcolinfo->colnames[ic] == NULL)
4786 ic++;
4787 Assert(ic < rightcolinfo->num_cols);
4788 ic++;
4789 /* If it is a merged column, we already processed it */
4791 continue;
4792 /* Else, advance i to the corresponding existing join column */
4793 while (i < colinfo->num_cols &&
4794 colinfo->colnames[i] == NULL)
4795 i++;
4796 Assert(i < colinfo->num_cols);
4797 Assert(ic == colinfo->rightattnos[i]);
4798 /* Use the already-assigned name of this column */
4799 colinfo->new_colnames[j] = colinfo->colnames[i];
4800 i++;
4801 }
4802 else
4803 {
4804 /*
4805 * Unique-ify the new child column name and assign, unless we're
4806 * in an unnamed join, in which case just copy
4807 */
4808 if (rte->alias != NULL)
4809 {
4810 colinfo->new_colnames[j] =
4812 if (!changed_any &&
4813 strcmp(colinfo->new_colnames[j], child_colname) != 0)
4814 changed_any = true;
4815 }
4816 else
4817 colinfo->new_colnames[j] = child_colname;
4818 add_to_names_hash(colinfo, colinfo->new_colnames[j]);
4819 }
4820
4821 colinfo->is_new_col[j] = rightcolinfo->is_new_col[jc];
4822 j++;
4823 }
4824
4825 /* Assert we processed the right number of columns */
4826#ifdef USE_ASSERT_CHECKING
4827 while (i < colinfo->num_cols && colinfo->colnames[i] == NULL)
4828 i++;
4829 Assert(i == colinfo->num_cols);
4830 Assert(j == nnewcolumns);
4831#endif
4832
4833 /* We're now done needing the colinfo's names_hash */
4835
4836 /*
4837 * For a named join, print column aliases if we changed any from the child
4838 * names. Unnamed joins cannot print aliases.
4839 */
4840 if (rte->alias != NULL)
4841 colinfo->printaliases = changed_any;
4842 else
4843 colinfo->printaliases = false;
4844}
4845
4846/*
4847 * colname_is_unique: is colname distinct from already-chosen column names?
4848 *
4849 * dpns is query-wide info, colinfo is for the column's RTE
4850 */
4851static bool
4854{
4855 int i;
4856 ListCell *lc;
4857
4858 /*
4859 * If we have a hash table, consult that instead of linearly scanning the
4860 * colinfo's strings.
4861 */
4862 if (colinfo->names_hash)
4863 {
4864 if (hash_search(colinfo->names_hash,
4865 colname,
4866 HASH_FIND,
4867 NULL) != NULL)
4868 return false;
4869 }
4870 else
4871 {
4872 /* Check against already-assigned column aliases within RTE */
4873 for (i = 0; i < colinfo->num_cols; i++)
4874 {
4875 char *oldname = colinfo->colnames[i];
4876
4877 if (oldname && strcmp(oldname, colname) == 0)
4878 return false;
4879 }
4880
4881 /*
4882 * If we're building a new_colnames array, check that too (this will
4883 * be partially but not completely redundant with the previous checks)
4884 */
4885 for (i = 0; i < colinfo->num_new_cols; i++)
4886 {
4887 char *oldname = colinfo->new_colnames[i];
4888
4889 if (oldname && strcmp(oldname, colname) == 0)
4890 return false;
4891 }
4892
4893 /*
4894 * Also check against names already assigned for parent-join USING
4895 * cols
4896 */
4897 foreach(lc, colinfo->parentUsing)
4898 {
4899 char *oldname = (char *) lfirst(lc);
4900
4901 if (strcmp(oldname, colname) == 0)
4902 return false;
4903 }
4904 }
4905
4906 /*
4907 * Also check against USING-column names that must be globally unique.
4908 * These are not hashed, but there should be few of them.
4909 */
4910 foreach(lc, dpns->using_names)
4911 {
4912 char *oldname = (char *) lfirst(lc);
4913
4914 if (strcmp(oldname, colname) == 0)
4915 return false;
4916 }
4917
4918 return true;
4919}
4920
4921/*
4922 * make_colname_unique: modify colname if necessary to make it unique
4923 *
4924 * dpns is query-wide info, colinfo is for the column's RTE
4925 */
4926static char *
4929{
4930 /*
4931 * If the selected name isn't unique, append digits to make it so. For a
4932 * very long input name, we might have to truncate to stay within
4933 * NAMEDATALEN.
4934 */
4935 if (!colname_is_unique(colname, dpns, colinfo))
4936 {
4937 int colnamelen = strlen(colname);
4938 char *modname = (char *) palloc(colnamelen + 16);
4939 int i = 0;
4940
4941 do
4942 {
4943 i++;
4944 for (;;)
4945 {
4946 memcpy(modname, colname, colnamelen);
4947 sprintf(modname + colnamelen, "_%d", i);
4948 if (strlen(modname) < NAMEDATALEN)
4949 break;
4950 /* drop chars from colname to keep all the digits */
4952 colnamelen - 1);
4953 }
4954 } while (!colname_is_unique(modname, dpns, colinfo));
4955 colname = modname;
4956 }
4957 return colname;
4958}
4959
4960/*
4961 * expand_colnames_array_to: make colinfo->colnames at least n items long
4962 *
4963 * Any added array entries are initialized to zero.
4964 */
4965static void
4967{
4968 if (n > colinfo->num_cols)
4969 {
4970 if (colinfo->colnames == NULL)
4971 colinfo->colnames = palloc0_array(char *, n);
4972 else
4973 colinfo->colnames = repalloc0_array(colinfo->colnames, char *, colinfo->num_cols, n);
4974 colinfo->num_cols = n;
4975 }
4976}
4977
4978/*
4979 * build_colinfo_names_hash: optionally construct a hash table for colinfo
4980 */
4981static void
4983{
4985 int i;
4986 ListCell *lc;
4987
4988 /*
4989 * Use a hash table only for RTEs with at least 32 columns. (The cutoff
4990 * is somewhat arbitrary, but let's choose it so that this code does get
4991 * exercised in the regression tests.)
4992 */
4993 if (colinfo->num_cols < 32)
4994 return;
4995
4996 /*
4997 * Set up the hash table. The entries are just strings with no other
4998 * payload.
4999 */
5000 hash_ctl.keysize = NAMEDATALEN;
5001 hash_ctl.entrysize = NAMEDATALEN;
5003 colinfo->names_hash = hash_create("deparse_columns names",
5004 colinfo->num_cols + colinfo->num_new_cols,
5005 &hash_ctl,
5007
5008 /*
5009 * Preload the hash table with any names already present (these would have
5010 * come from set_using_names).
5011 */
5012 for (i = 0; i < colinfo->num_cols; i++)
5013 {
5014 char *oldname = colinfo->colnames[i];
5015
5016 if (oldname)
5018 }
5019
5020 for (i = 0; i < colinfo->num_new_cols; i++)
5021 {
5022 char *oldname = colinfo->new_colnames[i];
5023
5024 if (oldname)
5026 }
5027
5028 foreach(lc, colinfo->parentUsing)
5029 {
5030 char *oldname = (char *) lfirst(lc);
5031
5033 }
5034}
5035
5036/*
5037 * add_to_names_hash: add a string to the names_hash, if we're using one
5038 */
5039static void
5041{
5042 if (colinfo->names_hash)
5043 (void) hash_search(colinfo->names_hash,
5044 name,
5045 HASH_ENTER,
5046 NULL);
5047}
5048
5049/*
5050 * destroy_colinfo_names_hash: destroy hash table when done with it
5051 */
5052static void
5054{
5055 if (colinfo->names_hash)
5056 {
5057 hash_destroy(colinfo->names_hash);
5058 colinfo->names_hash = NULL;
5059 }
5060}
5061
5062/*
5063 * identify_join_columns: figure out where columns of a join come from
5064 *
5065 * Fills the join-specific fields of the colinfo struct, except for
5066 * usingNames which is filled later.
5067 */
5068static void
5071{
5072 int numjoincols;
5073 int jcolno;
5074 int rcolno;
5075 ListCell *lc;
5076
5077 /* Extract left/right child RT indexes */
5078 if (IsA(j->larg, RangeTblRef))
5079 colinfo->leftrti = ((RangeTblRef *) j->larg)->rtindex;
5080 else if (IsA(j->larg, JoinExpr))
5081 colinfo->leftrti = ((JoinExpr *) j->larg)->rtindex;
5082 else
5083 elog(ERROR, "unrecognized node type in jointree: %d",
5084 (int) nodeTag(j->larg));
5085 if (IsA(j->rarg, RangeTblRef))
5086 colinfo->rightrti = ((RangeTblRef *) j->rarg)->rtindex;
5087 else if (IsA(j->rarg, JoinExpr))
5088 colinfo->rightrti = ((JoinExpr *) j->rarg)->rtindex;
5089 else
5090 elog(ERROR, "unrecognized node type in jointree: %d",
5091 (int) nodeTag(j->rarg));
5092
5093 /* Assert children will be processed earlier than join in second pass */
5094 Assert(colinfo->leftrti < j->rtindex);
5095 Assert(colinfo->rightrti < j->rtindex);
5096
5097 /* Initialize result arrays with zeroes */
5098 numjoincols = list_length(jrte->joinaliasvars);
5099 Assert(numjoincols == list_length(jrte->eref->colnames));
5100 colinfo->leftattnos = (int *) palloc0(numjoincols * sizeof(int));
5101 colinfo->rightattnos = (int *) palloc0(numjoincols * sizeof(int));
5102
5103 /*
5104 * Deconstruct RTE's joinleftcols/joinrightcols into desired format.
5105 * Recall that the column(s) merged due to USING are the first column(s)
5106 * of the join output. We need not do anything special while scanning
5107 * joinleftcols, but while scanning joinrightcols we must distinguish
5108 * merged from unmerged columns.
5109 */
5110 jcolno = 0;
5111 foreach(lc, jrte->joinleftcols)
5112 {
5113 int leftattno = lfirst_int(lc);
5114
5115 colinfo->leftattnos[jcolno++] = leftattno;
5116 }
5117 rcolno = 0;
5118 foreach(lc, jrte->joinrightcols)
5119 {
5120 int rightattno = lfirst_int(lc);
5121
5122 if (rcolno < jrte->joinmergedcols) /* merged column? */
5123 colinfo->rightattnos[rcolno] = rightattno;
5124 else
5125 colinfo->rightattnos[jcolno++] = rightattno;
5126 rcolno++;
5127 }
5129}
5130
5131/*
5132 * get_rtable_name: convenience function to get a previously assigned RTE alias
5133 *
5134 * The RTE must belong to the topmost namespace level in "context".
5135 */
5136static char *
5137get_rtable_name(int rtindex, deparse_context *context)
5138{
5140
5141 Assert(rtindex > 0 && rtindex <= list_length(dpns->rtable_names));
5142 return (char *) list_nth(dpns->rtable_names, rtindex - 1);
5143}
5144
5145/*
5146 * set_deparse_plan: set up deparse_namespace to parse subexpressions
5147 * of a given Plan node
5148 *
5149 * This sets the plan, outer_plan, inner_plan, outer_tlist, inner_tlist,
5150 * and index_tlist fields. Caller must already have adjusted the ancestors
5151 * list if necessary. Note that the rtable, subplans, and ctes fields do
5152 * not need to change when shifting attention to different plan nodes in a
5153 * single plan tree.
5154 */
5155static void
5157{
5158 dpns->plan = plan;
5159
5160 /*
5161 * We special-case Append and MergeAppend to pretend that the first child
5162 * plan is the OUTER referent; we have to interpret OUTER Vars in their
5163 * tlists according to one of the children, and the first one is the most
5164 * natural choice.
5165 */
5166 if (IsA(plan, Append))
5167 dpns->outer_plan = linitial(((Append *) plan)->appendplans);
5168 else if (IsA(plan, MergeAppend))
5169 dpns->outer_plan = linitial(((MergeAppend *) plan)->mergeplans);
5170 else
5171 dpns->outer_plan = outerPlan(plan);
5172
5173 if (dpns->outer_plan)
5174 dpns->outer_tlist = dpns->outer_plan->targetlist;
5175 else
5176 dpns->outer_tlist = NIL;
5177
5178 /*
5179 * For a SubqueryScan, pretend the subplan is INNER referent. (We don't
5180 * use OUTER because that could someday conflict with the normal meaning.)
5181 * Likewise, for a CteScan, pretend the subquery's plan is INNER referent.
5182 * For a WorkTableScan, locate the parent RecursiveUnion plan node and use
5183 * that as INNER referent.
5184 *
5185 * For MERGE, pretend the ModifyTable's source plan (its outer plan) is
5186 * INNER referent. This is the join from the target relation to the data
5187 * source, and all INNER_VAR Vars in other parts of the query refer to its
5188 * targetlist.
5189 *
5190 * For ON CONFLICT DO SELECT/UPDATE we just need the inner tlist to point
5191 * to the excluded expression's tlist. (Similar to the SubqueryScan we
5192 * don't want to reuse OUTER, it's used for RETURNING in some modify table
5193 * cases, although not INSERT .. CONFLICT).
5194 */
5195 if (IsA(plan, SubqueryScan))
5196 dpns->inner_plan = ((SubqueryScan *) plan)->subplan;
5197 else if (IsA(plan, CteScan))
5198 dpns->inner_plan = list_nth(dpns->subplans,
5199 ((CteScan *) plan)->ctePlanId - 1);
5200 else if (IsA(plan, WorkTableScan))
5201 dpns->inner_plan = find_recursive_union(dpns,
5202 (WorkTableScan *) plan);
5203 else if (IsA(plan, ModifyTable))
5204 {
5205 if (((ModifyTable *) plan)->operation == CMD_MERGE)
5206 dpns->inner_plan = outerPlan(plan);
5207 else
5208 dpns->inner_plan = plan;
5209 }
5210 else
5211 dpns->inner_plan = innerPlan(plan);
5212
5213 if (IsA(plan, ModifyTable) && ((ModifyTable *) plan)->operation == CMD_INSERT)
5214 dpns->inner_tlist = ((ModifyTable *) plan)->exclRelTlist;
5215 else if (dpns->inner_plan)
5216 dpns->inner_tlist = dpns->inner_plan->targetlist;
5217 else
5218 dpns->inner_tlist = NIL;
5219
5220 /* Set up referent for INDEX_VAR Vars, if needed */
5221 if (IsA(plan, IndexOnlyScan))
5222 dpns->index_tlist = ((IndexOnlyScan *) plan)->indextlist;
5223 else if (IsA(plan, ForeignScan))
5224 dpns->index_tlist = ((ForeignScan *) plan)->fdw_scan_tlist;
5225 else if (IsA(plan, CustomScan))
5226 dpns->index_tlist = ((CustomScan *) plan)->custom_scan_tlist;
5227 else
5228 dpns->index_tlist = NIL;
5229}
5230
5231/*
5232 * Locate the ancestor plan node that is the RecursiveUnion generating
5233 * the WorkTableScan's work table. We can match on wtParam, since that
5234 * should be unique within the plan tree.
5235 */
5236static Plan *
5238{
5239 ListCell *lc;
5240
5241 foreach(lc, dpns->ancestors)
5242 {
5243 Plan *ancestor = (Plan *) lfirst(lc);
5244
5245 if (IsA(ancestor, RecursiveUnion) &&
5246 ((RecursiveUnion *) ancestor)->wtParam == wtscan->wtParam)
5247 return ancestor;
5248 }
5249 elog(ERROR, "could not find RecursiveUnion for WorkTableScan with wtParam %d",
5250 wtscan->wtParam);
5251 return NULL;
5252}
5253
5254/*
5255 * push_child_plan: temporarily transfer deparsing attention to a child plan
5256 *
5257 * When expanding an OUTER_VAR or INNER_VAR reference, we must adjust the
5258 * deparse context in case the referenced expression itself uses
5259 * OUTER_VAR/INNER_VAR. We modify the top stack entry in-place to avoid
5260 * affecting levelsup issues (although in a Plan tree there really shouldn't
5261 * be any).
5262 *
5263 * Caller must provide a local deparse_namespace variable to save the
5264 * previous state for pop_child_plan.
5265 */
5266static void
5269{
5270 /* Save state for restoration later */
5271 *save_dpns = *dpns;
5272
5273 /* Link current plan node into ancestors list */
5274 dpns->ancestors = lcons(dpns->plan, dpns->ancestors);
5275
5276 /* Set attention on selected child */
5278}
5279
5280/*
5281 * pop_child_plan: undo the effects of push_child_plan
5282 */
5283static void
5285{
5286 List *ancestors;
5287
5288 /* Get rid of ancestors list cell added by push_child_plan */
5289 ancestors = list_delete_first(dpns->ancestors);
5290
5291 /* Restore fields changed by push_child_plan */
5292 *dpns = *save_dpns;
5293
5294 /* Make sure dpns->ancestors is right (may be unnecessary) */
5295 dpns->ancestors = ancestors;
5296}
5297
5298/*
5299 * push_ancestor_plan: temporarily transfer deparsing attention to an
5300 * ancestor plan
5301 *
5302 * When expanding a Param reference, we must adjust the deparse context
5303 * to match the plan node that contains the expression being printed;
5304 * otherwise we'd fail if that expression itself contains a Param or
5305 * OUTER_VAR/INNER_VAR/INDEX_VAR variable.
5306 *
5307 * The target ancestor is conveniently identified by the ListCell holding it
5308 * in dpns->ancestors.
5309 *
5310 * Caller must provide a local deparse_namespace variable to save the
5311 * previous state for pop_ancestor_plan.
5312 */
5313static void
5316{
5318
5319 /* Save state for restoration later */
5320 *save_dpns = *dpns;
5321
5322 /* Build a new ancestor list with just this node's ancestors */
5323 dpns->ancestors =
5324 list_copy_tail(dpns->ancestors,
5325 list_cell_number(dpns->ancestors, ancestor_cell) + 1);
5326
5327 /* Set attention on selected ancestor */
5329}
5330
5331/*
5332 * pop_ancestor_plan: undo the effects of push_ancestor_plan
5333 */
5334static void
5336{
5337 /* Free the ancestor list made in push_ancestor_plan */
5338 list_free(dpns->ancestors);
5339
5340 /* Restore fields changed by push_ancestor_plan */
5341 *dpns = *save_dpns;
5342}
5343
5344
5345/* ----------
5346 * make_ruledef - reconstruct the CREATE RULE command
5347 * for a given pg_rewrite tuple
5348 * ----------
5349 */
5350static void
5352 int prettyFlags)
5353{
5354 char *rulename;
5355 char ev_type;
5356 Oid ev_class;
5357 bool is_instead;
5358 char *ev_qual;
5359 char *ev_action;
5360 List *actions;
5363 int fno;
5364 Datum dat;
5365 bool isnull;
5366
5367 /*
5368 * Get the attribute values from the rules tuple
5369 */
5370 fno = SPI_fnumber(rulettc, "rulename");
5371 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5372 Assert(!isnull);
5373 rulename = NameStr(*(DatumGetName(dat)));
5374
5375 fno = SPI_fnumber(rulettc, "ev_type");
5376 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5377 Assert(!isnull);
5378 ev_type = DatumGetChar(dat);
5379
5380 fno = SPI_fnumber(rulettc, "ev_class");
5381 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5382 Assert(!isnull);
5384
5385 fno = SPI_fnumber(rulettc, "is_instead");
5386 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5387 Assert(!isnull);
5388 is_instead = DatumGetBool(dat);
5389
5390 fno = SPI_fnumber(rulettc, "ev_qual");
5392 Assert(ev_qual != NULL);
5393
5394 fno = SPI_fnumber(rulettc, "ev_action");
5396 Assert(ev_action != NULL);
5397 actions = (List *) stringToNode(ev_action);
5398 if (actions == NIL)
5399 elog(ERROR, "invalid empty ev_action list");
5400
5402
5403 /*
5404 * Build the rules definition text
5405 */
5406 appendStringInfo(buf, "CREATE RULE %s AS",
5407 quote_identifier(rulename));
5408
5409 if (prettyFlags & PRETTYFLAG_INDENT)
5410 appendStringInfoString(buf, "\n ON ");
5411 else
5412 appendStringInfoString(buf, " ON ");
5413
5414 /* The event the rule is fired for */
5415 switch (ev_type)
5416 {
5417 case '1':
5418 appendStringInfoString(buf, "SELECT");
5420 break;
5421
5422 case '2':
5423 appendStringInfoString(buf, "UPDATE");
5424 break;
5425
5426 case '3':
5427 appendStringInfoString(buf, "INSERT");
5428 break;
5429
5430 case '4':
5431 appendStringInfoString(buf, "DELETE");
5432 break;
5433
5434 default:
5435 ereport(ERROR,
5437 errmsg("rule \"%s\" has unsupported event type %d",
5438 rulename, ev_type)));
5439 break;
5440 }
5441
5442 /* The relation the rule is fired on */
5443 appendStringInfo(buf, " TO %s",
5444 (prettyFlags & PRETTYFLAG_SCHEMA) ?
5447
5448 /* If the rule has an event qualification, add it */
5449 if (strcmp(ev_qual, "<>") != 0)
5450 {
5451 Node *qual;
5452 Query *query;
5453 deparse_context context;
5455
5456 if (prettyFlags & PRETTYFLAG_INDENT)
5458 appendStringInfoString(buf, " WHERE ");
5459
5460 qual = stringToNode(ev_qual);
5461
5462 /*
5463 * We need to make a context for recognizing any Vars in the qual
5464 * (which can only be references to OLD and NEW). Use the rtable of
5465 * the first query in the action list for this purpose.
5466 */
5467 query = (Query *) linitial(actions);
5468
5469 /*
5470 * If the action is INSERT...SELECT, OLD/NEW have been pushed down
5471 * into the SELECT, and that's what we need to look at. (Ugly kluge
5472 * ... try to fix this when we redesign querytrees.)
5473 */
5474 query = getInsertSelectQuery(query, NULL);
5475
5476 /* Must acquire locks right away; see notes in get_query_def() */
5477 AcquireRewriteLocks(query, false, false);
5478
5479 context.buf = buf;
5480 context.namespaces = list_make1(&dpns);
5481 context.resultDesc = NULL;
5482 context.targetList = NIL;
5483 context.windowClause = NIL;
5484 context.varprefix = (list_length(query->rtable) != 1);
5485 context.prettyFlags = prettyFlags;
5487 context.indentLevel = PRETTYINDENT_STD;
5488 context.colNamesVisible = true;
5489 context.inGroupBy = false;
5490 context.varInOrderBy = false;
5491 context.appendparents = NULL;
5492
5493 set_deparse_for_query(&dpns, query, NIL);
5494
5495 get_rule_expr(qual, &context, false);
5496 }
5497
5498 appendStringInfoString(buf, " DO ");
5499
5500 /* The INSTEAD keyword (if so) */
5501 if (is_instead)
5502 appendStringInfoString(buf, "INSTEAD ");
5503
5504 /* Finally the rules actions */
5505 if (list_length(actions) > 1)
5506 {
5507 ListCell *action;
5508 Query *query;
5509
5511 foreach(action, actions)
5512 {
5513 query = (Query *) lfirst(action);
5514 get_query_def(query, buf, NIL, viewResultDesc, true,
5515 prettyFlags, WRAP_COLUMN_DEFAULT, 0);
5516 if (prettyFlags)
5518 else
5520 }
5522 }
5523 else
5524 {
5525 Query *query;
5526
5527 query = (Query *) linitial(actions);
5528 get_query_def(query, buf, NIL, viewResultDesc, true,
5529 prettyFlags, WRAP_COLUMN_DEFAULT, 0);
5531 }
5532
5534}
5535
5536
5537/* ----------
5538 * make_viewdef - reconstruct the SELECT part of a
5539 * view rewrite rule
5540 * ----------
5541 */
5542static void
5544 int prettyFlags, int wrapColumn)
5545{
5546 Query *query;
5547 char ev_type;
5548 Oid ev_class;
5549 bool is_instead;
5550 char *ev_qual;
5551 char *ev_action;
5552 List *actions;
5554 int fno;
5555 Datum dat;
5556 bool isnull;
5557
5558 /*
5559 * Get the attribute values from the rules tuple
5560 */
5561 fno = SPI_fnumber(rulettc, "ev_type");
5562 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5563 Assert(!isnull);
5564 ev_type = DatumGetChar(dat);
5565
5566 fno = SPI_fnumber(rulettc, "ev_class");
5567 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5568 Assert(!isnull);
5570
5571 fno = SPI_fnumber(rulettc, "is_instead");
5572 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5573 Assert(!isnull);
5574 is_instead = DatumGetBool(dat);
5575
5576 fno = SPI_fnumber(rulettc, "ev_qual");
5578 Assert(ev_qual != NULL);
5579
5580 fno = SPI_fnumber(rulettc, "ev_action");
5582 Assert(ev_action != NULL);
5583 actions = (List *) stringToNode(ev_action);
5584
5585 if (list_length(actions) != 1)
5586 {
5587 /* keep output buffer empty and leave */
5588 return;
5589 }
5590
5591 query = (Query *) linitial(actions);
5592
5593 if (ev_type != '1' || !is_instead ||
5594 strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
5595 {
5596 /* keep output buffer empty and leave */
5597 return;
5598 }
5599
5601
5603 prettyFlags, wrapColumn, 0);
5605
5607}
5608
5609
5610/* ----------
5611 * get_query_def - Parse back one query parsetree
5612 *
5613 * query: parsetree to be displayed
5614 * buf: output text is appended to buf
5615 * parentnamespace: list (initially empty) of outer-level deparse_namespace's
5616 * resultDesc: if not NULL, the output tuple descriptor for the view
5617 * represented by a SELECT query. We use the column names from it
5618 * to label SELECT output columns, in preference to names in the query
5619 * colNamesVisible: true if the surrounding context cares about the output
5620 * column names at all (as, for example, an EXISTS() context does not);
5621 * when false, we can suppress dummy column labels such as "?column?"
5622 * prettyFlags: bitmask of PRETTYFLAG_XXX options
5623 * wrapColumn: maximum line length, or -1 to disable wrapping
5624 * startIndent: initial indentation amount
5625 * ----------
5626 */
5627static void
5629 TupleDesc resultDesc, bool colNamesVisible,
5630 int prettyFlags, int wrapColumn, int startIndent)
5631{
5632 deparse_context context;
5634 int rtable_size;
5635
5636 /* Guard against excessively long or deeply-nested queries */
5639
5640 rtable_size = query->hasGroupRTE ?
5641 list_length(query->rtable) - 1 :
5642 list_length(query->rtable);
5643
5644 /*
5645 * Replace any Vars in the query's targetlist and havingQual that
5646 * reference GROUP outputs with the underlying grouping expressions.
5647 */
5648 if (query->hasGroupRTE)
5649 {
5650 query->targetList = (List *)
5651 flatten_group_exprs(NULL, query, (Node *) query->targetList);
5652 query->havingQual =
5653 flatten_group_exprs(NULL, query, query->havingQual);
5654 }
5655
5656 /*
5657 * Before we begin to examine the query, acquire locks on referenced
5658 * relations, and fix up deleted columns in JOIN RTEs. This ensures
5659 * consistent results. Note we assume it's OK to scribble on the passed
5660 * querytree!
5661 *
5662 * We are only deparsing the query (we are not about to execute it), so we
5663 * only need AccessShareLock on the relations it mentions.
5664 */
5665 AcquireRewriteLocks(query, false, false);
5666
5667 context.buf = buf;
5669 context.resultDesc = NULL;
5670 context.targetList = NIL;
5671 context.windowClause = NIL;
5672 context.varprefix = (parentnamespace != NIL ||
5673 rtable_size != 1);
5674 context.prettyFlags = prettyFlags;
5675 context.wrapColumn = wrapColumn;
5676 context.indentLevel = startIndent;
5677 context.colNamesVisible = colNamesVisible;
5678 context.inGroupBy = false;
5679 context.varInOrderBy = false;
5680 context.appendparents = NULL;
5681
5683
5684 switch (query->commandType)
5685 {
5686 case CMD_SELECT:
5687 /* We set context.resultDesc only if it's a SELECT */
5688 context.resultDesc = resultDesc;
5689 get_select_query_def(query, &context);
5690 break;
5691
5692 case CMD_UPDATE:
5693 get_update_query_def(query, &context);
5694 break;
5695
5696 case CMD_INSERT:
5697 get_insert_query_def(query, &context);
5698 break;
5699
5700 case CMD_DELETE:
5701 get_delete_query_def(query, &context);
5702 break;
5703
5704 case CMD_MERGE:
5705 get_merge_query_def(query, &context);
5706 break;
5707
5708 case CMD_NOTHING:
5709 appendStringInfoString(buf, "NOTHING");
5710 break;
5711
5712 case CMD_UTILITY:
5713 get_utility_query_def(query, &context);
5714 break;
5715
5716 default:
5717 elog(ERROR, "unrecognized query command type: %d",
5718 query->commandType);
5719 break;
5720 }
5721}
5722
5723/* ----------
5724 * get_values_def - Parse back a VALUES list
5725 * ----------
5726 */
5727static void
5728get_values_def(List *values_lists, deparse_context *context)
5729{
5730 StringInfo buf = context->buf;
5731 bool first_list = true;
5732 ListCell *vtl;
5733
5734 appendStringInfoString(buf, "VALUES ");
5735
5736 foreach(vtl, values_lists)
5737 {
5738 List *sublist = (List *) lfirst(vtl);
5739 bool first_col = true;
5740 ListCell *lc;
5741
5742 if (first_list)
5743 first_list = false;
5744 else
5746
5748 foreach(lc, sublist)
5749 {
5750 Node *col = (Node *) lfirst(lc);
5751
5752 if (first_col)
5753 first_col = false;
5754 else
5756
5757 /*
5758 * Print the value. Whole-row Vars need special treatment.
5759 */
5760 get_rule_expr_toplevel(col, context, false);
5761 }
5763 }
5764}
5765
5766/* ----------
5767 * get_with_clause - Parse back a WITH clause
5768 * ----------
5769 */
5770static void
5772{
5773 StringInfo buf = context->buf;
5774 const char *sep;
5775 ListCell *l;
5776
5777 if (query->cteList == NIL)
5778 return;
5779
5780 if (PRETTY_INDENT(context))
5781 {
5782 context->indentLevel += PRETTYINDENT_STD;
5784 }
5785
5786 if (query->hasRecursive)
5787 sep = "WITH RECURSIVE ";
5788 else
5789 sep = "WITH ";
5790 foreach(l, query->cteList)
5791 {
5793
5796 if (cte->aliascolnames)
5797 {
5798 bool first = true;
5799 ListCell *col;
5800
5802 foreach(col, cte->aliascolnames)
5803 {
5804 if (first)
5805 first = false;
5806 else
5810 }
5812 }
5813 appendStringInfoString(buf, " AS ");
5814 switch (cte->ctematerialized)
5815 {
5817 break;
5819 appendStringInfoString(buf, "MATERIALIZED ");
5820 break;
5822 appendStringInfoString(buf, "NOT MATERIALIZED ");
5823 break;
5824 }
5826 if (PRETTY_INDENT(context))
5827 appendContextKeyword(context, "", 0, 0, 0);
5828 get_query_def((Query *) cte->ctequery, buf, context->namespaces, NULL,
5829 true,
5830 context->prettyFlags, context->wrapColumn,
5831 context->indentLevel);
5832 if (PRETTY_INDENT(context))
5833 appendContextKeyword(context, "", 0, 0, 0);
5835
5836 if (cte->search_clause)
5837 {
5838 bool first = true;
5839 ListCell *lc;
5840
5841 appendStringInfo(buf, " SEARCH %s FIRST BY ",
5842 cte->search_clause->search_breadth_first ? "BREADTH" : "DEPTH");
5843
5844 foreach(lc, cte->search_clause->search_col_list)
5845 {
5846 if (first)
5847 first = false;
5848 else
5852 }
5853
5854 appendStringInfo(buf, " SET %s", quote_identifier(cte->search_clause->search_seq_column));
5855 }
5856
5857 if (cte->cycle_clause)
5858 {
5859 bool first = true;
5860 ListCell *lc;
5861
5862 appendStringInfoString(buf, " CYCLE ");
5863
5864 foreach(lc, cte->cycle_clause->cycle_col_list)
5865 {
5866 if (first)
5867 first = false;
5868 else
5872 }
5873
5874 appendStringInfo(buf, " SET %s", quote_identifier(cte->cycle_clause->cycle_mark_column));
5875
5876 {
5877 Const *cmv = castNode(Const, cte->cycle_clause->cycle_mark_value);
5878 Const *cmd = castNode(Const, cte->cycle_clause->cycle_mark_default);
5879
5880 if (!(cmv->consttype == BOOLOID && !cmv->constisnull && DatumGetBool(cmv->constvalue) == true &&
5881 cmd->consttype == BOOLOID && !cmd->constisnull && DatumGetBool(cmd->constvalue) == false))
5882 {
5883 appendStringInfoString(buf, " TO ");
5884 get_rule_expr(cte->cycle_clause->cycle_mark_value, context, false);
5885 appendStringInfoString(buf, " DEFAULT ");
5886 get_rule_expr(cte->cycle_clause->cycle_mark_default, context, false);
5887 }
5888 }
5889
5890 appendStringInfo(buf, " USING %s", quote_identifier(cte->cycle_clause->cycle_path_column));
5891 }
5892
5893 sep = ", ";
5894 }
5895
5896 if (PRETTY_INDENT(context))
5897 {
5898 context->indentLevel -= PRETTYINDENT_STD;
5899 appendContextKeyword(context, "", 0, 0, 0);
5900 }
5901 else
5903}
5904
5905/* ----------
5906 * get_select_query_def - Parse back a SELECT parsetree
5907 * ----------
5908 */
5909static void
5911{
5912 StringInfo buf = context->buf;
5913 bool force_colno;
5914 ListCell *l;
5915
5916 /* Insert the WITH clause if given */
5917 get_with_clause(query, context);
5918
5919 /* Subroutines may need to consult the SELECT targetlist and windowClause */
5920 context->targetList = query->targetList;
5921 context->windowClause = query->windowClause;
5922
5923 /*
5924 * If the Query node has a setOperations tree, then it's the top level of
5925 * a UNION/INTERSECT/EXCEPT query; only the WITH, ORDER BY and LIMIT
5926 * fields are interesting in the top query itself.
5927 */
5928 if (query->setOperations)
5929 {
5930 get_setop_query(query->setOperations, query, context);
5931 /* ORDER BY clauses must be simple in this case */
5932 force_colno = true;
5933 }
5934 else
5935 {
5936 get_basic_select_query(query, context);
5937 force_colno = false;
5938 }
5939
5940 /* Add the ORDER BY clause if given */
5941 if (query->sortClause != NIL)
5942 {
5943 appendContextKeyword(context, " ORDER BY ",
5945 get_rule_orderby(query->sortClause, query->targetList,
5946 force_colno, context);
5947 }
5948
5949 /*
5950 * Add the LIMIT/OFFSET clauses if given. If non-default options, use the
5951 * standard spelling of LIMIT.
5952 */
5953 if (query->limitOffset != NULL)
5954 {
5955 appendContextKeyword(context, " OFFSET ",
5957 get_rule_expr(query->limitOffset, context, false);
5958 }
5959 if (query->limitCount != NULL)
5960 {
5961 if (query->limitOption == LIMIT_OPTION_WITH_TIES)
5962 {
5963 /*
5964 * The limitCount arg is a c_expr, so it needs parens. Simple
5965 * literals and function expressions would not need parens, but
5966 * unfortunately it's hard to tell if the expression will be
5967 * printed as a simple literal like 123 or as a typecast
5968 * expression, like '-123'::int4. The grammar accepts the former
5969 * without quoting, but not the latter.
5970 */
5971 appendContextKeyword(context, " FETCH FIRST ",
5974 get_rule_expr(query->limitCount, context, false);
5976 appendStringInfoString(buf, " ROWS WITH TIES");
5977 }
5978 else
5979 {
5980 appendContextKeyword(context, " LIMIT ",
5982 if (IsA(query->limitCount, Const) &&
5983 ((Const *) query->limitCount)->constisnull)
5985 else
5986 get_rule_expr(query->limitCount, context, false);
5987 }
5988 }
5989
5990 /* Add FOR [KEY] UPDATE/SHARE clauses if present */
5991 if (query->hasForUpdate)
5992 {
5993 foreach(l, query->rowMarks)
5994 {
5995 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
5996
5997 /* don't print implicit clauses */
5998 if (rc->pushedDown)
5999 continue;
6000
6001 appendContextKeyword(context,
6004
6005 appendStringInfo(buf, " OF %s",
6007 context)));
6008 if (rc->waitPolicy == LockWaitError)
6009 appendStringInfoString(buf, " NOWAIT");
6010 else if (rc->waitPolicy == LockWaitSkip)
6011 appendStringInfoString(buf, " SKIP LOCKED");
6012 }
6013 }
6014}
6015
6016static char *
6018{
6019 switch (strength)
6020 {
6021 case LCS_NONE:
6022 /* we intentionally throw an error for LCS_NONE */
6023 elog(ERROR, "unrecognized LockClauseStrength %d",
6024 (int) strength);
6025 break;
6026 case LCS_FORKEYSHARE:
6027 return " FOR KEY SHARE";
6028 case LCS_FORSHARE:
6029 return " FOR SHARE";
6030 case LCS_FORNOKEYUPDATE:
6031 return " FOR NO KEY UPDATE";
6032 case LCS_FORUPDATE:
6033 return " FOR UPDATE";
6034 }
6035 return NULL; /* keep compiler quiet */
6036}
6037
6038/*
6039 * Detect whether query looks like SELECT ... FROM VALUES(),
6040 * with no need to rename the output columns of the VALUES RTE.
6041 * If so, return the VALUES RTE. Otherwise return NULL.
6042 */
6043static RangeTblEntry *
6045{
6046 RangeTblEntry *result = NULL;
6047 ListCell *lc;
6048
6049 /*
6050 * We want to detect a match even if the Query also contains OLD or NEW
6051 * rule RTEs. So the idea is to scan the rtable and see if there is only
6052 * one inFromCl RTE that is a VALUES RTE.
6053 */
6054 foreach(lc, query->rtable)
6055 {
6057
6058 if (rte->rtekind == RTE_VALUES && rte->inFromCl)
6059 {
6060 if (result)
6061 return NULL; /* multiple VALUES (probably not possible) */
6062 result = rte;
6063 }
6064 else if (rte->rtekind == RTE_RELATION && !rte->inFromCl)
6065 continue; /* ignore rule entries */
6066 else
6067 return NULL; /* something else -> not simple VALUES */
6068 }
6069
6070 /*
6071 * We don't need to check the targetlist in any great detail, because
6072 * parser/analyze.c will never generate a "bare" VALUES RTE --- they only
6073 * appear inside auto-generated sub-queries with very restricted
6074 * structure. However, DefineView might have modified the tlist by
6075 * injecting new column aliases, or we might have some other column
6076 * aliases forced by a resultDesc. We can only simplify if the RTE's
6077 * column names match the names that get_target_list() would select.
6078 */
6079 if (result)
6080 {
6081 ListCell *lcn;
6082 int colno;
6083
6084 if (list_length(query->targetList) != list_length(result->eref->colnames))
6085 return NULL; /* this probably cannot happen */
6086 colno = 0;
6087 forboth(lc, query->targetList, lcn, result->eref->colnames)
6088 {
6090 char *cname = strVal(lfirst(lcn));
6091 char *colname;
6092
6093 if (tle->resjunk)
6094 return NULL; /* this probably cannot happen */
6095
6096 /* compute name that get_target_list would use for column */
6097 colno++;
6098 if (resultDesc && colno <= resultDesc->natts)
6099 colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
6100 else
6101 colname = tle->resname;
6102
6103 /* does it match the VALUES RTE? */
6104 if (colname == NULL || strcmp(colname, cname) != 0)
6105 return NULL; /* column name has been changed */
6106 }
6107 }
6108
6109 return result;
6110}
6111
6112static void
6114{
6115 StringInfo buf = context->buf;
6117 char *sep;
6118 ListCell *l;
6119
6120 if (PRETTY_INDENT(context))
6121 {
6122 context->indentLevel += PRETTYINDENT_STD;
6124 }
6125
6126 /*
6127 * If the query looks like SELECT * FROM (VALUES ...), then print just the
6128 * VALUES part. This reverses what transformValuesClause() did at parse
6129 * time.
6130 */
6131 values_rte = get_simple_values_rte(query, context->resultDesc);
6132 if (values_rte)
6133 {
6134 get_values_def(values_rte->values_lists, context);
6135 return;
6136 }
6137
6138 /*
6139 * Build up the query string - first we say SELECT
6140 */
6141 if (query->isReturn)
6142 appendStringInfoString(buf, "RETURN");
6143 else
6144 appendStringInfoString(buf, "SELECT");
6145
6146 /* Add the DISTINCT clause if given */
6147 if (query->distinctClause != NIL)
6148 {
6149 if (query->hasDistinctOn)
6150 {
6151 appendStringInfoString(buf, " DISTINCT ON (");
6152 sep = "";
6153 foreach(l, query->distinctClause)
6154 {
6156
6158 get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList,
6159 false, context);
6160 sep = ", ";
6161 }
6163 }
6164 else
6165 appendStringInfoString(buf, " DISTINCT");
6166 }
6167
6168 /* Then we tell what to select (the targetlist) */
6169 get_target_list(query->targetList, context);
6170
6171 /* Add the FROM clause if needed */
6172 get_from_clause(query, " FROM ", context);
6173
6174 /* Add the WHERE clause if given */
6175 if (query->jointree->quals != NULL)
6176 {
6177 appendContextKeyword(context, " WHERE ",
6179 get_rule_expr(query->jointree->quals, context, false);
6180 }
6181
6182 /* Add the GROUP BY clause if given */
6183 if (query->groupClause != NULL || query->groupingSets != NULL)
6184 {
6185 bool save_ingroupby;
6186
6187 appendContextKeyword(context, " GROUP BY ",
6189 if (query->groupDistinct)
6190 appendStringInfoString(buf, "DISTINCT ");
6191
6192 save_ingroupby = context->inGroupBy;
6193 context->inGroupBy = true;
6194
6195 if (query->groupByAll)
6197 else if (query->groupingSets == NIL)
6198 {
6199 sep = "";
6200 foreach(l, query->groupClause)
6201 {
6203
6205 get_rule_sortgroupclause(grp->tleSortGroupRef, query->targetList,
6206 false, context);
6207 sep = ", ";
6208 }
6209 }
6210 else
6211 {
6212 sep = "";
6213 foreach(l, query->groupingSets)
6214 {
6215 GroupingSet *grp = lfirst(l);
6216
6218 get_rule_groupingset(grp, query->targetList, true, context);
6219 sep = ", ";
6220 }
6221 }
6222
6223 context->inGroupBy = save_ingroupby;
6224 }
6225
6226 /* Add the HAVING clause if given */
6227 if (query->havingQual != NULL)
6228 {
6229 appendContextKeyword(context, " HAVING ",
6231 get_rule_expr(query->havingQual, context, false);
6232 }
6233
6234 /* Add the WINDOW clause if needed */
6235 if (query->windowClause != NIL)
6236 get_rule_windowclause(query, context);
6237}
6238
6239/* ----------
6240 * get_target_list - Parse back a SELECT target list
6241 *
6242 * This is also used for RETURNING lists in INSERT/UPDATE/DELETE/MERGE.
6243 * ----------
6244 */
6245static void
6247{
6248 StringInfo buf = context->buf;
6250 bool last_was_multiline = false;
6251 char *sep;
6252 int colno;
6253 ListCell *l;
6254
6255 /* we use targetbuf to hold each TLE's text temporarily */
6257
6258 sep = " ";
6259 colno = 0;
6260 foreach(l, targetList)
6261 {
6263 char *colname;
6264 char *attname;
6265
6266 if (tle->resjunk)
6267 continue; /* ignore junk entries */
6268
6270 sep = ", ";
6271 colno++;
6272
6273 /*
6274 * Put the new field text into targetbuf so we can decide after we've
6275 * got it whether or not it needs to go on a new line.
6276 */
6278 context->buf = &targetbuf;
6279
6280 /*
6281 * We special-case Var nodes rather than using get_rule_expr. This is
6282 * needed because get_rule_expr will display a whole-row Var as
6283 * "foo.*", which is the preferred notation in most contexts, but at
6284 * the top level of a SELECT list it's not right (the parser will
6285 * expand that notation into multiple columns, yielding behavior
6286 * different from a whole-row Var). We need to call get_variable
6287 * directly so that we can tell it to do the right thing, and so that
6288 * we can get the attribute name which is the default AS label.
6289 */
6290 if (tle->expr && (IsA(tle->expr, Var)))
6291 {
6292 attname = get_variable((Var *) tle->expr, 0, true, context);
6293 }
6294 else
6295 {
6296 get_rule_expr((Node *) tle->expr, context, true);
6297
6298 /*
6299 * When colNamesVisible is true, we should always show the
6300 * assigned column name explicitly. Otherwise, show it only if
6301 * it's not FigureColname's fallback.
6302 */
6303 attname = context->colNamesVisible ? NULL : "?column?";
6304 }
6305
6306 /*
6307 * Figure out what the result column should be called. In the context
6308 * of a view, use the view's tuple descriptor (so as to pick up the
6309 * effects of any column RENAME that's been done on the view).
6310 * Otherwise, just use what we can find in the TLE.
6311 */
6312 if (context->resultDesc && colno <= context->resultDesc->natts)
6313 colname = NameStr(TupleDescAttr(context->resultDesc,
6314 colno - 1)->attname);
6315 else
6316 colname = tle->resname;
6317
6318 /* Show AS unless the column's name is correct as-is */
6319 if (colname) /* resname could be NULL */
6320 {
6321 if (attname == NULL || strcmp(attname, colname) != 0)
6322 appendStringInfo(&targetbuf, " AS %s", quote_identifier(colname));
6323 }
6324
6325 /* Restore context's output buffer */
6326 context->buf = buf;
6327
6328 /* Consider line-wrapping if enabled */
6329 if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
6330 {
6331 int leading_nl_pos;
6332
6333 /* Does the new field start with a new line? */
6334 if (targetbuf.len > 0 && targetbuf.data[0] == '\n')
6335 leading_nl_pos = 0;
6336 else
6337 leading_nl_pos = -1;
6338
6339 /* If so, we shouldn't add anything */
6340 if (leading_nl_pos >= 0)
6341 {
6342 /* instead, remove any trailing spaces currently in buf */
6344 }
6345 else
6346 {
6347 char *trailing_nl;
6348
6349 /* Locate the start of the current line in the output buffer */
6350 trailing_nl = strrchr(buf->data, '\n');
6351 if (trailing_nl == NULL)
6352 trailing_nl = buf->data;
6353 else
6354 trailing_nl++;
6355
6356 /*
6357 * Add a newline, plus some indentation, if the new field is
6358 * not the first and either the new field would cause an
6359 * overflow or the last field used more than one line.
6360 */
6361 if (colno > 1 &&
6362 ((strlen(trailing_nl) + targetbuf.len > context->wrapColumn) ||
6366 }
6367
6368 /* Remember this field's multiline status for next iteration */
6370 (strchr(targetbuf.data + leading_nl_pos + 1, '\n') != NULL);
6371 }
6372
6373 /* Add the new field */
6375 }
6376
6377 /* clean up */
6378 pfree(targetbuf.data);
6379}
6380
6381static void
6383{
6384 StringInfo buf = context->buf;
6385
6386 if (query->returningList)
6387 {
6388 bool have_with = false;
6389
6390 appendContextKeyword(context, " RETURNING",
6392
6393 /* Add WITH (OLD/NEW) options, if they're not the defaults */
6394 if (query->returningOldAlias && strcmp(query->returningOldAlias, "old") != 0)
6395 {
6396 appendStringInfo(buf, " WITH (OLD AS %s",
6397 quote_identifier(query->returningOldAlias));
6398 have_with = true;
6399 }
6400 if (query->returningNewAlias && strcmp(query->returningNewAlias, "new") != 0)
6401 {
6402 if (have_with)
6403 appendStringInfo(buf, ", NEW AS %s",
6404 quote_identifier(query->returningNewAlias));
6405 else
6406 {
6407 appendStringInfo(buf, " WITH (NEW AS %s",
6408 quote_identifier(query->returningNewAlias));
6409 have_with = true;
6410 }
6411 }
6412 if (have_with)
6414
6415 /* Add the returning expressions themselves */
6416 get_target_list(query->returningList, context);
6417 }
6418}
6419
6420static void
6422{
6423 StringInfo buf = context->buf;
6424 bool need_paren;
6425
6426 /* Guard against excessively long or deeply-nested queries */
6429
6430 if (IsA(setOp, RangeTblRef))
6431 {
6433 RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable);
6434 Query *subquery = rte->subquery;
6435
6436 Assert(subquery != NULL);
6437
6438 /*
6439 * We need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y.
6440 * Also add parens if the leaf query contains its own set operations.
6441 * (That shouldn't happen unless one of the other clauses is also
6442 * present, see transformSetOperationTree; but let's be safe.)
6443 */
6444 need_paren = (subquery->cteList ||
6445 subquery->sortClause ||
6446 subquery->rowMarks ||
6447 subquery->limitOffset ||
6448 subquery->limitCount ||
6449 subquery->setOperations);
6450 if (need_paren)
6452 get_query_def(subquery, buf, context->namespaces,
6453 context->resultDesc, context->colNamesVisible,
6454 context->prettyFlags, context->wrapColumn,
6455 context->indentLevel);
6456 if (need_paren)
6458 }
6459 else if (IsA(setOp, SetOperationStmt))
6460 {
6462 int subindent;
6464
6465 /*
6466 * We force parens when nesting two SetOperationStmts, except when the
6467 * lefthand input is another setop of the same kind. Syntactically,
6468 * we could omit parens in rather more cases, but it seems best to use
6469 * parens to flag cases where the setop operator changes. If we use
6470 * parens, we also increase the indentation level for the child query.
6471 *
6472 * There are some cases in which parens are needed around a leaf query
6473 * too, but those are more easily handled at the next level down (see
6474 * code above).
6475 */
6476 if (IsA(op->larg, SetOperationStmt))
6477 {
6479
6480 if (op->op == lop->op && op->all == lop->all)
6481 need_paren = false;
6482 else
6483 need_paren = true;
6484 }
6485 else
6486 need_paren = false;
6487
6488 if (need_paren)
6489 {
6492 appendContextKeyword(context, "", subindent, 0, 0);
6493 }
6494 else
6495 subindent = 0;
6496
6497 get_setop_query(op->larg, query, context);
6498
6499 if (need_paren)
6500 appendContextKeyword(context, ") ", -subindent, 0, 0);
6501 else if (PRETTY_INDENT(context))
6502 appendContextKeyword(context, "", -subindent, 0, 0);
6503 else
6505
6506 switch (op->op)
6507 {
6508 case SETOP_UNION:
6509 appendStringInfoString(buf, "UNION ");
6510 break;
6511 case SETOP_INTERSECT:
6512 appendStringInfoString(buf, "INTERSECT ");
6513 break;
6514 case SETOP_EXCEPT:
6515 appendStringInfoString(buf, "EXCEPT ");
6516 break;
6517 default:
6518 elog(ERROR, "unrecognized set op: %d",
6519 (int) op->op);
6520 }
6521 if (op->all)
6522 appendStringInfoString(buf, "ALL ");
6523
6524 /* Always parenthesize if RHS is another setop */
6526
6527 /*
6528 * The indentation code here is deliberately a bit different from that
6529 * for the lefthand input, because we want the line breaks in
6530 * different places.
6531 */
6532 if (need_paren)
6533 {
6536 }
6537 else
6538 subindent = 0;
6539 appendContextKeyword(context, "", subindent, 0, 0);
6540
6541 /*
6542 * The output column names of the RHS sub-select don't matter.
6543 */
6545 context->colNamesVisible = false;
6546
6547 get_setop_query(op->rarg, query, context);
6548
6550
6551 if (PRETTY_INDENT(context))
6552 context->indentLevel -= subindent;
6553 if (need_paren)
6554 appendContextKeyword(context, ")", 0, 0, 0);
6555 }
6556 else
6557 {
6558 elog(ERROR, "unrecognized node type: %d",
6559 (int) nodeTag(setOp));
6560 }
6561}
6562
6563/*
6564 * Display a sort/group clause.
6565 *
6566 * Also returns the expression tree, so caller need not find it again.
6567 */
6568static Node *
6570 deparse_context *context)
6571{
6572 StringInfo buf = context->buf;
6574 Node *expr;
6575
6576 tle = get_sortgroupref_tle(ref, tlist);
6577 expr = (Node *) tle->expr;
6578
6579 /*
6580 * Use column-number form if requested by caller. Otherwise, if
6581 * expression is a constant, force it to be dumped with an explicit cast
6582 * as decoration --- this is because a simple integer constant is
6583 * ambiguous (and will be misinterpreted by findTargetlistEntrySQL92()) if
6584 * we dump it without any decoration. Similarly, if it's just a Var,
6585 * there is risk of misinterpretation if the column name is reassigned in
6586 * the SELECT list, so we may need to force table qualification. And, if
6587 * it's anything more complex than a simple Var, then force extra parens
6588 * around it, to ensure it can't be misinterpreted as a cube() or rollup()
6589 * construct.
6590 */
6591 if (force_colno)
6592 {
6593 Assert(!tle->resjunk);
6594 appendStringInfo(buf, "%d", tle->resno);
6595 }
6596 else if (!expr)
6597 /* do nothing, probably can't happen */ ;
6598 else if (IsA(expr, Const))
6599 get_const_expr((Const *) expr, context, 1);
6600 else if (IsA(expr, Var))
6601 {
6602 /* Tell get_variable to check for name conflict */
6603 bool save_varinorderby = context->varInOrderBy;
6604
6605 context->varInOrderBy = true;
6606 (void) get_variable((Var *) expr, 0, false, context);
6608 }
6609 else
6610 {
6611 /*
6612 * We must force parens for function-like expressions even if
6613 * PRETTY_PAREN is off, since those are the ones in danger of
6614 * misparsing. For other expressions we need to force them only if
6615 * PRETTY_PAREN is on, since otherwise the expression will output them
6616 * itself. (We can't skip the parens.)
6617 */
6618 bool need_paren = (PRETTY_PAREN(context)
6619 || IsA(expr, FuncExpr)
6620 || IsA(expr, Aggref)
6621 || IsA(expr, WindowFunc)
6622 || IsA(expr, JsonConstructorExpr));
6623
6624 if (need_paren)
6625 appendStringInfoChar(context->buf, '(');
6626 get_rule_expr(expr, context, true);
6627 if (need_paren)
6628 appendStringInfoChar(context->buf, ')');
6629 }
6630
6631 return expr;
6632}
6633
6634/*
6635 * Display a GroupingSet
6636 */
6637static void
6639 bool omit_parens, deparse_context *context)
6640{
6641 ListCell *l;
6642 StringInfo buf = context->buf;
6643 bool omit_child_parens = true;
6644 char *sep = "";
6645
6646 switch (gset->kind)
6647 {
6648 case GROUPING_SET_EMPTY:
6650 return;
6651
6653 {
6654 if (!omit_parens || list_length(gset->content) != 1)
6656
6657 foreach(l, gset->content)
6658 {
6659 Index ref = lfirst_int(l);
6660
6662 get_rule_sortgroupclause(ref, targetlist,
6663 false, context);
6664 sep = ", ";
6665 }
6666
6667 if (!omit_parens || list_length(gset->content) != 1)
6669 }
6670 return;
6671
6673 appendStringInfoString(buf, "ROLLUP(");
6674 break;
6675 case GROUPING_SET_CUBE:
6676 appendStringInfoString(buf, "CUBE(");
6677 break;
6678 case GROUPING_SET_SETS:
6679 appendStringInfoString(buf, "GROUPING SETS (");
6680 omit_child_parens = false;
6681 break;
6682 }
6683
6684 foreach(l, gset->content)
6685 {
6687 get_rule_groupingset(lfirst(l), targetlist, omit_child_parens, context);
6688 sep = ", ";
6689 }
6690
6692}
6693
6694/*
6695 * Display an ORDER BY list.
6696 */
6697static void
6699 bool force_colno, deparse_context *context)
6700{
6701 StringInfo buf = context->buf;
6702 const char *sep;
6703 ListCell *l;
6704
6705 sep = "";
6706 foreach(l, orderList)
6707 {
6709 Node *sortexpr;
6711 TypeCacheEntry *typentry;
6712
6714 sortexpr = get_rule_sortgroupclause(srt->tleSortGroupRef, targetList,
6715 force_colno, context);
6717 /* See whether operator is default < or > for datatype */
6718 typentry = lookup_type_cache(sortcoltype,
6720 if (srt->sortop == typentry->lt_opr)
6721 {
6722 /* ASC is default, so emit nothing for it */
6723 if (srt->nulls_first)
6724 appendStringInfoString(buf, " NULLS FIRST");
6725 }
6726 else if (srt->sortop == typentry->gt_opr)
6727 {
6728 appendStringInfoString(buf, " DESC");
6729 /* DESC defaults to NULLS FIRST */
6730 if (!srt->nulls_first)
6731 appendStringInfoString(buf, " NULLS LAST");
6732 }
6733 else
6734 {
6735 appendStringInfo(buf, " USING %s",
6738 sortcoltype));
6739 /* be specific to eliminate ambiguity */
6740 if (srt->nulls_first)
6741 appendStringInfoString(buf, " NULLS FIRST");
6742 else
6743 appendStringInfoString(buf, " NULLS LAST");
6744 }
6745 sep = ", ";
6746 }
6747}
6748
6749/*
6750 * Display a WINDOW clause.
6751 *
6752 * Note that the windowClause list might contain only anonymous window
6753 * specifications, in which case we should print nothing here.
6754 */
6755static void
6757{
6758 StringInfo buf = context->buf;
6759 const char *sep;
6760 ListCell *l;
6761
6762 sep = NULL;
6763 foreach(l, query->windowClause)
6764 {
6765 WindowClause *wc = (WindowClause *) lfirst(l);
6766
6767 if (wc->name == NULL)
6768 continue; /* ignore anonymous windows */
6769
6770 if (sep == NULL)
6771 appendContextKeyword(context, " WINDOW ",
6773 else
6775
6776 appendStringInfo(buf, "%s AS ", quote_identifier(wc->name));
6777
6778 get_rule_windowspec(wc, query->targetList, context);
6779
6780 sep = ", ";
6781 }
6782}
6783
6784/*
6785 * Display a window definition
6786 */
6787static void
6789 deparse_context *context)
6790{
6791 StringInfo buf = context->buf;
6792 bool needspace = false;
6793 const char *sep;
6794 ListCell *l;
6795
6797 if (wc->refname)
6798 {
6800 needspace = true;
6801 }
6802 /* partition clauses are always inherited, so only print if no refname */
6803 if (wc->partitionClause && !wc->refname)
6804 {
6805 if (needspace)
6807 appendStringInfoString(buf, "PARTITION BY ");
6808 sep = "";
6809 foreach(l, wc->partitionClause)
6810 {
6812
6814 get_rule_sortgroupclause(grp->tleSortGroupRef, targetList,
6815 false, context);
6816 sep = ", ";
6817 }
6818 needspace = true;
6819 }
6820 /* print ordering clause only if not inherited */
6821 if (wc->orderClause && !wc->copiedOrder)
6822 {
6823 if (needspace)
6825 appendStringInfoString(buf, "ORDER BY ");
6826 get_rule_orderby(wc->orderClause, targetList, false, context);
6827 needspace = true;
6828 }
6829 /* framing clause is never inherited, so print unless it's default */
6831 {
6832 if (needspace)
6835 wc->startOffset, wc->endOffset,
6836 context);
6837 }
6839}
6840
6841/*
6842 * Append the description of a window's framing options to context->buf
6843 */
6844static void
6846 Node *startOffset, Node *endOffset,
6847 deparse_context *context)
6848{
6849 StringInfo buf = context->buf;
6850
6851 if (frameOptions & FRAMEOPTION_NONDEFAULT)
6852 {
6853 if (frameOptions & FRAMEOPTION_RANGE)
6854 appendStringInfoString(buf, "RANGE ");
6855 else if (frameOptions & FRAMEOPTION_ROWS)
6856 appendStringInfoString(buf, "ROWS ");
6857 else if (frameOptions & FRAMEOPTION_GROUPS)
6858 appendStringInfoString(buf, "GROUPS ");
6859 else
6860 Assert(false);
6861 if (frameOptions & FRAMEOPTION_BETWEEN)
6862 appendStringInfoString(buf, "BETWEEN ");
6863 if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
6864 appendStringInfoString(buf, "UNBOUNDED PRECEDING ");
6865 else if (frameOptions & FRAMEOPTION_START_CURRENT_ROW)
6866 appendStringInfoString(buf, "CURRENT ROW ");
6867 else if (frameOptions & FRAMEOPTION_START_OFFSET)
6868 {
6869 get_rule_expr(startOffset, context, false);
6870 if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
6871 appendStringInfoString(buf, " PRECEDING ");
6872 else if (frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
6873 appendStringInfoString(buf, " FOLLOWING ");
6874 else
6875 Assert(false);
6876 }
6877 else
6878 Assert(false);
6879 if (frameOptions & FRAMEOPTION_BETWEEN)
6880 {
6881 appendStringInfoString(buf, "AND ");
6882 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
6883 appendStringInfoString(buf, "UNBOUNDED FOLLOWING ");
6884 else if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
6885 appendStringInfoString(buf, "CURRENT ROW ");
6886 else if (frameOptions & FRAMEOPTION_END_OFFSET)
6887 {
6888 get_rule_expr(endOffset, context, false);
6889 if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
6890 appendStringInfoString(buf, " PRECEDING ");
6891 else if (frameOptions & FRAMEOPTION_END_OFFSET_FOLLOWING)
6892 appendStringInfoString(buf, " FOLLOWING ");
6893 else
6894 Assert(false);
6895 }
6896 else
6897 Assert(false);
6898 }
6899 if (frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
6900 appendStringInfoString(buf, "EXCLUDE CURRENT ROW ");
6901 else if (frameOptions & FRAMEOPTION_EXCLUDE_GROUP)
6902 appendStringInfoString(buf, "EXCLUDE GROUP ");
6903 else if (frameOptions & FRAMEOPTION_EXCLUDE_TIES)
6904 appendStringInfoString(buf, "EXCLUDE TIES ");
6905 /* we will now have a trailing space; remove it */
6906 buf->data[--(buf->len)] = '\0';
6907 }
6908}
6909
6910/*
6911 * Return the description of a window's framing options as a palloc'd string
6912 */
6913char *
6915 Node *startOffset, Node *endOffset,
6916 List *dpcontext, bool forceprefix)
6917{
6919 deparse_context context;
6920
6922 context.buf = &buf;
6923 context.namespaces = dpcontext;
6924 context.resultDesc = NULL;
6925 context.targetList = NIL;
6926 context.windowClause = NIL;
6927 context.varprefix = forceprefix;
6928 context.prettyFlags = 0;
6930 context.indentLevel = 0;
6931 context.colNamesVisible = true;
6932 context.inGroupBy = false;
6933 context.varInOrderBy = false;
6934 context.appendparents = NULL;
6935
6936 get_window_frame_options(frameOptions, startOffset, endOffset, &context);
6937
6938 return buf.data;
6939}
6940
6941/* ----------
6942 * get_insert_query_def - Parse back an INSERT parsetree
6943 * ----------
6944 */
6945static void
6947{
6948 StringInfo buf = context->buf;
6952 char *sep;
6953 ListCell *l;
6955
6956 /* Insert the WITH clause if given */
6957 get_with_clause(query, context);
6958
6959 /*
6960 * If it's an INSERT ... SELECT or multi-row VALUES, there will be a
6961 * single RTE for the SELECT or VALUES. Plain VALUES has neither.
6962 */
6963 foreach(l, query->rtable)
6964 {
6965 rte = (RangeTblEntry *) lfirst(l);
6966
6967 if (rte->rtekind == RTE_SUBQUERY)
6968 {
6969 if (select_rte)
6970 elog(ERROR, "too many subquery RTEs in INSERT");
6971 select_rte = rte;
6972 }
6973
6974 if (rte->rtekind == RTE_VALUES)
6975 {
6976 if (values_rte)
6977 elog(ERROR, "too many values RTEs in INSERT");
6978 values_rte = rte;
6979 }
6980 }
6981 if (select_rte && values_rte)
6982 elog(ERROR, "both subquery and values RTEs in INSERT");
6983
6984 /*
6985 * Start the query with INSERT INTO relname
6986 */
6987 rte = rt_fetch(query->resultRelation, query->rtable);
6988 Assert(rte->rtekind == RTE_RELATION);
6989
6990 if (PRETTY_INDENT(context))
6991 {
6992 context->indentLevel += PRETTYINDENT_STD;
6994 }
6995 appendStringInfo(buf, "INSERT INTO %s",
6996 generate_relation_name(rte->relid, NIL));
6997
6998 /* Print the relation alias, if needed; INSERT requires explicit AS */
6999 get_rte_alias(rte, query->resultRelation, true, context);
7000
7001 /* always want a space here */
7003
7004 /*
7005 * Add the insert-column-names list. Any indirection decoration needed on
7006 * the column names can be inferred from the top targetlist.
7007 */
7009 sep = "";
7010 if (query->targetList)
7012 foreach(l, query->targetList)
7013 {
7015
7016 if (tle->resjunk)
7017 continue; /* ignore junk entries */
7018
7020 sep = ", ";
7021
7022 /*
7023 * Put out name of target column; look in the catalogs, not at
7024 * tle->resname, since resname will fail to track RENAME.
7025 */
7028 tle->resno,
7029 false)));
7030
7031 /*
7032 * Print any indirection needed (subfields or subscripts), and strip
7033 * off the top-level nodes representing the indirection assignments.
7034 * Add the stripped expressions to strippedexprs. (If it's a
7035 * single-VALUES statement, the stripped expressions are the VALUES to
7036 * print below. Otherwise they're just Vars and not really
7037 * interesting.)
7038 */
7040 processIndirection((Node *) tle->expr,
7041 context));
7042 }
7043 if (query->targetList)
7045
7046 if (query->override)
7047 {
7048 if (query->override == OVERRIDING_SYSTEM_VALUE)
7049 appendStringInfoString(buf, "OVERRIDING SYSTEM VALUE ");
7050 else if (query->override == OVERRIDING_USER_VALUE)
7051 appendStringInfoString(buf, "OVERRIDING USER VALUE ");
7052 }
7053
7054 if (select_rte)
7055 {
7056 /* Add the SELECT */
7057 get_query_def(select_rte->subquery, buf, context->namespaces, NULL,
7058 false,
7059 context->prettyFlags, context->wrapColumn,
7060 context->indentLevel);
7061 }
7062 else if (values_rte)
7063 {
7064 /* Add the multi-VALUES expression lists */
7065 get_values_def(values_rte->values_lists, context);
7066 }
7067 else if (strippedexprs)
7068 {
7069 /* Add the single-VALUES expression list */
7070 appendContextKeyword(context, "VALUES (",
7072 get_rule_list_toplevel(strippedexprs, context, false);
7074 }
7075 else
7076 {
7077 /* No expressions, so it must be DEFAULT VALUES */
7078 appendStringInfoString(buf, "DEFAULT VALUES");
7079 }
7080
7081 /* Add ON CONFLICT if present */
7082 if (query->onConflict)
7083 {
7085
7086 appendStringInfoString(buf, " ON CONFLICT");
7087
7088 if (confl->arbiterElems)
7089 {
7090 /* Add the single-VALUES expression list */
7092 get_rule_expr((Node *) confl->arbiterElems, context, false);
7094
7095 /* Add a WHERE clause (for partial indexes) if given */
7096 if (confl->arbiterWhere != NULL)
7097 {
7098 bool save_varprefix;
7099
7100 /*
7101 * Force non-prefixing of Vars, since parser assumes that they
7102 * belong to target relation. WHERE clause does not use
7103 * InferenceElem, so this is separately required.
7104 */
7105 save_varprefix = context->varprefix;
7106 context->varprefix = false;
7107
7108 appendContextKeyword(context, " WHERE ",
7110 get_rule_expr(confl->arbiterWhere, context, false);
7111
7112 context->varprefix = save_varprefix;
7113 }
7114 }
7115 else if (OidIsValid(confl->constraint))
7116 {
7117 char *constraint = get_constraint_name(confl->constraint);
7118
7119 if (!constraint)
7120 elog(ERROR, "cache lookup failed for constraint %u",
7121 confl->constraint);
7122 appendStringInfo(buf, " ON CONSTRAINT %s",
7123 quote_identifier(constraint));
7124 }
7125
7126 if (confl->action == ONCONFLICT_NOTHING)
7127 {
7128 appendStringInfoString(buf, " DO NOTHING");
7129 }
7130 else if (confl->action == ONCONFLICT_UPDATE)
7131 {
7132 appendStringInfoString(buf, " DO UPDATE SET ");
7133 /* Deparse targetlist */
7134 get_update_query_targetlist_def(query, confl->onConflictSet,
7135 context, rte);
7136
7137 /* Add a WHERE clause if given */
7138 if (confl->onConflictWhere != NULL)
7139 {
7140 appendContextKeyword(context, " WHERE ",
7142 get_rule_expr(confl->onConflictWhere, context, false);
7143 }
7144 }
7145 else
7146 {
7147 Assert(confl->action == ONCONFLICT_SELECT);
7148 appendStringInfoString(buf, " DO SELECT");
7149
7150 /* Add FOR [KEY] UPDATE/SHARE clause if present */
7151 if (confl->lockStrength != LCS_NONE)
7153
7154 /* Add a WHERE clause if given */
7155 if (confl->onConflictWhere != NULL)
7156 {
7157 appendContextKeyword(context, " WHERE ",
7159 get_rule_expr(confl->onConflictWhere, context, false);
7160 }
7161 }
7162 }
7163
7164 /* Add RETURNING if present */
7165 if (query->returningList)
7166 get_returning_clause(query, context);
7167}
7168
7169
7170/* ----------
7171 * get_update_query_def - Parse back an UPDATE parsetree
7172 * ----------
7173 */
7174static void
7176{
7177 StringInfo buf = context->buf;
7179
7180 /* Insert the WITH clause if given */
7181 get_with_clause(query, context);
7182
7183 /*
7184 * Start the query with UPDATE relname SET
7185 */
7186 rte = rt_fetch(query->resultRelation, query->rtable);
7187 Assert(rte->rtekind == RTE_RELATION);
7188 if (PRETTY_INDENT(context))
7189 {
7191 context->indentLevel += PRETTYINDENT_STD;
7192 }
7193 appendStringInfo(buf, "UPDATE %s%s",
7195 generate_relation_name(rte->relid, NIL));
7196
7197 /* Print the relation alias, if needed */
7198 get_rte_alias(rte, query->resultRelation, false, context);
7199
7200 appendStringInfoString(buf, " SET ");
7201
7202 /* Deparse targetlist */
7203 get_update_query_targetlist_def(query, query->targetList, context, rte);
7204
7205 /* Add the FROM clause if needed */
7206 get_from_clause(query, " FROM ", context);
7207
7208 /* Add a WHERE clause if given */
7209 if (query->jointree->quals != NULL)
7210 {
7211 appendContextKeyword(context, " WHERE ",
7213 get_rule_expr(query->jointree->quals, context, false);
7214 }
7215
7216 /* Add RETURNING if present */
7217 if (query->returningList)
7218 get_returning_clause(query, context);
7219}
7220
7221
7222/* ----------
7223 * get_update_query_targetlist_def - Parse back an UPDATE targetlist
7224 * ----------
7225 */
7226static void
7229{
7230 StringInfo buf = context->buf;
7231 ListCell *l;
7234 const char *sep;
7237
7238 /*
7239 * Prepare to deal with MULTIEXPR assignments: collect the source SubLinks
7240 * into a list. We expect them to appear, in ID order, in resjunk tlist
7241 * entries.
7242 */
7243 ma_sublinks = NIL;
7244 if (query->hasSubLinks) /* else there can't be any */
7245 {
7246 foreach(l, targetList)
7247 {
7249
7250 if (tle->resjunk && IsA(tle->expr, SubLink))
7251 {
7252 SubLink *sl = (SubLink *) tle->expr;
7253
7254 if (sl->subLinkType == MULTIEXPR_SUBLINK)
7255 {
7257 Assert(sl->subLinkId == list_length(ma_sublinks));
7258 }
7259 }
7260 }
7261 }
7265
7266 /* Add the comma separated list of 'attname = value' */
7267 sep = "";
7268 foreach(l, targetList)
7269 {
7271 Node *expr;
7272
7273 if (tle->resjunk)
7274 continue; /* ignore junk entries */
7275
7276 /* Emit separator (OK whether we're in multiassignment or not) */
7278 sep = ", ";
7279
7280 /*
7281 * Check to see if we're starting a multiassignment group: if so,
7282 * output a left paren.
7283 */
7284 if (next_ma_cell != NULL && cur_ma_sublink == NULL)
7285 {
7286 /*
7287 * We must dig down into the expr to see if it's a PARAM_MULTIEXPR
7288 * Param. That could be buried under FieldStores and
7289 * SubscriptingRefs and CoerceToDomains (cf processIndirection()),
7290 * and underneath those there could be an implicit type coercion.
7291 * Because we would ignore implicit type coercions anyway, we
7292 * don't need to be as careful as processIndirection() is about
7293 * descending past implicit CoerceToDomains.
7294 */
7295 expr = (Node *) tle->expr;
7296 while (expr)
7297 {
7298 if (IsA(expr, FieldStore))
7299 {
7300 FieldStore *fstore = (FieldStore *) expr;
7301
7302 expr = (Node *) linitial(fstore->newvals);
7303 }
7304 else if (IsA(expr, SubscriptingRef))
7305 {
7306 SubscriptingRef *sbsref = (SubscriptingRef *) expr;
7307
7308 if (sbsref->refassgnexpr == NULL)
7309 break;
7310
7311 expr = (Node *) sbsref->refassgnexpr;
7312 }
7313 else if (IsA(expr, CoerceToDomain))
7314 {
7316
7317 if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
7318 break;
7319 expr = (Node *) cdomain->arg;
7320 }
7321 else
7322 break;
7323 }
7324 expr = strip_implicit_coercions(expr);
7325
7326 if (expr && IsA(expr, Param) &&
7327 ((Param *) expr)->paramkind == PARAM_MULTIEXPR)
7328 {
7332 Assert(((Param *) expr)->paramid ==
7333 ((cur_ma_sublink->subLinkId << 16) | 1));
7335 }
7336 }
7337
7338 /*
7339 * Put out name of target column; look in the catalogs, not at
7340 * tle->resname, since resname will fail to track RENAME.
7341 */
7344 tle->resno,
7345 false)));
7346
7347 /*
7348 * Print any indirection needed (subfields or subscripts), and strip
7349 * off the top-level nodes representing the indirection assignments.
7350 */
7351 expr = processIndirection((Node *) tle->expr, context);
7352
7353 /*
7354 * If we're in a multiassignment, skip printing anything more, unless
7355 * this is the last column; in which case, what we print should be the
7356 * sublink, not the Param.
7357 */
7358 if (cur_ma_sublink != NULL)
7359 {
7360 if (--remaining_ma_columns > 0)
7361 continue; /* not the last column of multiassignment */
7363 expr = (Node *) cur_ma_sublink;
7365 }
7366
7368
7369 get_rule_expr(expr, context, false);
7370 }
7371}
7372
7373
7374/* ----------
7375 * get_delete_query_def - Parse back a DELETE parsetree
7376 * ----------
7377 */
7378static void
7380{
7381 StringInfo buf = context->buf;
7383
7384 /* Insert the WITH clause if given */
7385 get_with_clause(query, context);
7386
7387 /*
7388 * Start the query with DELETE FROM relname
7389 */
7390 rte = rt_fetch(query->resultRelation, query->rtable);
7391 Assert(rte->rtekind == RTE_RELATION);
7392 if (PRETTY_INDENT(context))
7393 {
7395 context->indentLevel += PRETTYINDENT_STD;
7396 }
7397 appendStringInfo(buf, "DELETE FROM %s%s",
7399 generate_relation_name(rte->relid, NIL));
7400
7401 /* Print the relation alias, if needed */
7402 get_rte_alias(rte, query->resultRelation, false, context);
7403
7404 /* Add the USING clause if given */
7405 get_from_clause(query, " USING ", context);
7406
7407 /* Add a WHERE clause if given */
7408 if (query->jointree->quals != NULL)
7409 {
7410 appendContextKeyword(context, " WHERE ",
7412 get_rule_expr(query->jointree->quals, context, false);
7413 }
7414
7415 /* Add RETURNING if present */
7416 if (query->returningList)
7417 get_returning_clause(query, context);
7418}
7419
7420
7421/* ----------
7422 * get_merge_query_def - Parse back a MERGE parsetree
7423 * ----------
7424 */
7425static void
7427{
7428 StringInfo buf = context->buf;
7430 ListCell *lc;
7432
7433 /* Insert the WITH clause if given */
7434 get_with_clause(query, context);
7435
7436 /*
7437 * Start the query with MERGE INTO relname
7438 */
7439 rte = rt_fetch(query->resultRelation, query->rtable);
7440 Assert(rte->rtekind == RTE_RELATION);
7441 if (PRETTY_INDENT(context))
7442 {
7444 context->indentLevel += PRETTYINDENT_STD;
7445 }
7446 appendStringInfo(buf, "MERGE INTO %s%s",
7448 generate_relation_name(rte->relid, NIL));
7449
7450 /* Print the relation alias, if needed */
7451 get_rte_alias(rte, query->resultRelation, false, context);
7452
7453 /* Print the source relation and join clause */
7454 get_from_clause(query, " USING ", context);
7455 appendContextKeyword(context, " ON ",
7457 get_rule_expr(query->mergeJoinCondition, context, false);
7458
7459 /*
7460 * Test for any NOT MATCHED BY SOURCE actions. If there are none, then
7461 * any NOT MATCHED BY TARGET actions are output as "WHEN NOT MATCHED", per
7462 * SQL standard. Otherwise, we have a non-SQL-standard query, so output
7463 * "BY SOURCE" / "BY TARGET" qualifiers for all NOT MATCHED actions, to be
7464 * more explicit.
7465 */
7466 haveNotMatchedBySource = false;
7467 foreach(lc, query->mergeActionList)
7468 {
7470
7471 if (action->matchKind == MERGE_WHEN_NOT_MATCHED_BY_SOURCE)
7472 {
7474 break;
7475 }
7476 }
7477
7478 /* Print each merge action */
7479 foreach(lc, query->mergeActionList)
7480 {
7482
7483 appendContextKeyword(context, " WHEN ",
7485 switch (action->matchKind)
7486 {
7487 case MERGE_WHEN_MATCHED:
7488 appendStringInfoString(buf, "MATCHED");
7489 break;
7491 appendStringInfoString(buf, "NOT MATCHED BY SOURCE");
7492 break;
7495 appendStringInfoString(buf, "NOT MATCHED BY TARGET");
7496 else
7497 appendStringInfoString(buf, "NOT MATCHED");
7498 break;
7499 default:
7500 elog(ERROR, "unrecognized matchKind: %d",
7501 (int) action->matchKind);
7502 }
7503
7504 if (action->qual)
7505 {
7506 appendContextKeyword(context, " AND ",
7508 get_rule_expr(action->qual, context, false);
7509 }
7510 appendContextKeyword(context, " THEN ",
7512
7513 if (action->commandType == CMD_INSERT)
7514 {
7515 /* This generally matches get_insert_query_def() */
7517 const char *sep = "";
7518 ListCell *lc2;
7519
7520 appendStringInfoString(buf, "INSERT");
7521
7522 if (action->targetList)
7524 foreach(lc2, action->targetList)
7525 {
7527
7528 Assert(!tle->resjunk);
7529
7531 sep = ", ";
7532
7535 tle->resno,
7536 false)));
7538 processIndirection((Node *) tle->expr,
7539 context));
7540 }
7541 if (action->targetList)
7543
7544 if (action->override)
7545 {
7546 if (action->override == OVERRIDING_SYSTEM_VALUE)
7547 appendStringInfoString(buf, " OVERRIDING SYSTEM VALUE");
7548 else if (action->override == OVERRIDING_USER_VALUE)
7549 appendStringInfoString(buf, " OVERRIDING USER VALUE");
7550 }
7551
7552 if (strippedexprs)
7553 {
7554 appendContextKeyword(context, " VALUES (",
7556 get_rule_list_toplevel(strippedexprs, context, false);
7558 }
7559 else
7560 appendStringInfoString(buf, " DEFAULT VALUES");
7561 }
7562 else if (action->commandType == CMD_UPDATE)
7563 {
7564 appendStringInfoString(buf, "UPDATE SET ");
7565 get_update_query_targetlist_def(query, action->targetList,
7566 context, rte);
7567 }
7568 else if (action->commandType == CMD_DELETE)
7569 appendStringInfoString(buf, "DELETE");
7570 else if (action->commandType == CMD_NOTHING)
7571 appendStringInfoString(buf, "DO NOTHING");
7572 }
7573
7574 /* Add RETURNING if present */
7575 if (query->returningList)
7576 get_returning_clause(query, context);
7577}
7578
7579
7580/* ----------
7581 * get_utility_query_def - Parse back a UTILITY parsetree
7582 * ----------
7583 */
7584static void
7586{
7587 StringInfo buf = context->buf;
7588
7589 if (query->utilityStmt && IsA(query->utilityStmt, NotifyStmt))
7590 {
7591 NotifyStmt *stmt = (NotifyStmt *) query->utilityStmt;
7592
7593 appendContextKeyword(context, "",
7594 0, PRETTYINDENT_STD, 1);
7595 appendStringInfo(buf, "NOTIFY %s",
7596 quote_identifier(stmt->conditionname));
7597 if (stmt->payload)
7598 {
7600 simple_quote_literal(buf, stmt->payload);
7601 }
7602 }
7603 else
7604 {
7605 /* Currently only NOTIFY utility commands can appear in rules */
7606 elog(ERROR, "unexpected utility statement type");
7607 }
7608}
7609
7610/*
7611 * Display a Var appropriately.
7612 *
7613 * In some cases (currently only when recursing into an unnamed join)
7614 * the Var's varlevelsup has to be interpreted with respect to a context
7615 * above the current one; levelsup indicates the offset.
7616 *
7617 * If istoplevel is true, the Var is at the top level of a SELECT's
7618 * targetlist, which means we need special treatment of whole-row Vars.
7619 * Instead of the normal "tab.*", we'll print "tab.*::typename", which is a
7620 * dirty hack to prevent "tab.*" from being expanded into multiple columns.
7621 * (The parser will strip the useless coercion, so no inefficiency is added in
7622 * dump and reload.) We used to print just "tab" in such cases, but that is
7623 * ambiguous and will yield the wrong result if "tab" is also a plain column
7624 * name in the query.
7625 *
7626 * Returns the attname of the Var, or NULL if the Var has no attname (because
7627 * it is a whole-row Var or a subplan output reference).
7628 */
7629static char *
7630get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
7631{
7632 StringInfo buf = context->buf;
7635 int netlevelsup;
7637 int varno;
7638 AttrNumber varattno;
7640 char *refname;
7641 char *attname;
7642 bool need_prefix;
7643
7644 /* Find appropriate nesting depth */
7645 netlevelsup = var->varlevelsup + levelsup;
7646 if (netlevelsup >= list_length(context->namespaces))
7647 elog(ERROR, "bogus varlevelsup: %d offset %d",
7648 var->varlevelsup, levelsup);
7650 netlevelsup);
7651
7652 /*
7653 * If we have a syntactic referent for the Var, and we're working from a
7654 * parse tree, prefer to use the syntactic referent. Otherwise, fall back
7655 * on the semantic referent. (Forcing use of the semantic referent when
7656 * printing plan trees is a design choice that's perhaps more motivated by
7657 * backwards compatibility than anything else. But it does have the
7658 * advantage of making plans more explicit.)
7659 */
7660 if (var->varnosyn > 0 && dpns->plan == NULL)
7661 {
7662 varno = var->varnosyn;
7663 varattno = var->varattnosyn;
7664 }
7665 else
7666 {
7667 varno = var->varno;
7668 varattno = var->varattno;
7669 }
7670
7671 /*
7672 * Try to find the relevant RTE in this rtable. In a plan tree, it's
7673 * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
7674 * down into the subplans, or INDEX_VAR, which is resolved similarly. Also
7675 * find the aliases previously assigned for this RTE.
7676 */
7677 if (varno >= 1 && varno <= list_length(dpns->rtable))
7678 {
7679 /*
7680 * We might have been asked to map child Vars to some parent relation.
7681 */
7682 if (context->appendparents && dpns->appendrels)
7683 {
7684 int pvarno = varno;
7685 AttrNumber pvarattno = varattno;
7686 AppendRelInfo *appinfo = dpns->appendrels[pvarno];
7687 bool found = false;
7688
7689 /* Only map up to inheritance parents, not UNION ALL appendrels */
7690 while (appinfo &&
7691 rt_fetch(appinfo->parent_relid,
7692 dpns->rtable)->rtekind == RTE_RELATION)
7693 {
7694 found = false;
7695 if (pvarattno > 0) /* system columns stay as-is */
7696 {
7697 if (pvarattno > appinfo->num_child_cols)
7698 break; /* safety check */
7699 pvarattno = appinfo->parent_colnos[pvarattno - 1];
7700 if (pvarattno == 0)
7701 break; /* Var is local to child */
7702 }
7703
7705 found = true;
7706
7707 /* If the parent is itself a child, continue up. */
7708 Assert(pvarno > 0 && pvarno <= list_length(dpns->rtable));
7709 appinfo = dpns->appendrels[pvarno];
7710 }
7711
7712 /*
7713 * If we found an ancestral rel, and that rel is included in
7714 * appendparents, print that column not the original one.
7715 */
7716 if (found && bms_is_member(pvarno, context->appendparents))
7717 {
7718 varno = pvarno;
7719 varattno = pvarattno;
7720 }
7721 }
7722
7723 rte = rt_fetch(varno, dpns->rtable);
7724
7725 /* might be returning old/new column value */
7727 refname = dpns->ret_old_alias;
7728 else if (var->varreturningtype == VAR_RETURNING_NEW)
7729 refname = dpns->ret_new_alias;
7730 else
7731 refname = (char *) list_nth(dpns->rtable_names, varno - 1);
7732
7734 attnum = varattno;
7735 }
7736 else
7737 {
7738 resolve_special_varno((Node *) var, context,
7740 return NULL;
7741 }
7742
7743 /*
7744 * The planner will sometimes emit Vars referencing resjunk elements of a
7745 * subquery's target list (this is currently only possible if it chooses
7746 * to generate a "physical tlist" for a SubqueryScan or CteScan node).
7747 * Although we prefer to print subquery-referencing Vars using the
7748 * subquery's alias, that's not possible for resjunk items since they have
7749 * no alias. So in that case, drill down to the subplan and print the
7750 * contents of the referenced tlist item. This works because in a plan
7751 * tree, such Vars can only occur in a SubqueryScan or CteScan node, and
7752 * we'll have set dpns->inner_plan to reference the child plan node.
7753 */
7754 if ((rte->rtekind == RTE_SUBQUERY || rte->rtekind == RTE_CTE) &&
7755 attnum > list_length(rte->eref->colnames) &&
7756 dpns->inner_plan)
7757 {
7760
7761 tle = get_tle_by_resno(dpns->inner_tlist, attnum);
7762 if (!tle)
7763 elog(ERROR, "invalid attnum %d for relation \"%s\"",
7764 attnum, rte->eref->aliasname);
7765
7766 Assert(netlevelsup == 0);
7767 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
7768
7769 /*
7770 * Force parentheses because our caller probably assumed a Var is a
7771 * simple expression.
7772 */
7773 if (!IsA(tle->expr, Var))
7775 get_rule_expr((Node *) tle->expr, context, true);
7776 if (!IsA(tle->expr, Var))
7778
7780 return NULL;
7781 }
7782
7783 /*
7784 * If it's an unnamed join, look at the expansion of the alias variable.
7785 * If it's a simple reference to one of the input vars, then recursively
7786 * print the name of that var instead. When it's not a simple reference,
7787 * we have to just print the unqualified join column name. (This can only
7788 * happen with "dangerous" merged columns in a JOIN USING; we took pains
7789 * previously to make the unqualified column name unique in such cases.)
7790 *
7791 * This wouldn't work in decompiling plan trees, because we don't store
7792 * joinaliasvars lists after planning; but a plan tree should never
7793 * contain a join alias variable.
7794 */
7795 if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
7796 {
7797 if (rte->joinaliasvars == NIL)
7798 elog(ERROR, "cannot decompile join alias var in plan tree");
7799 if (attnum > 0)
7800 {
7801 Var *aliasvar;
7802
7803 aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
7804 /* we intentionally don't strip implicit coercions here */
7805 if (aliasvar && IsA(aliasvar, Var))
7806 {
7807 return get_variable(aliasvar, var->varlevelsup + levelsup,
7808 istoplevel, context);
7809 }
7810 }
7811
7812 /*
7813 * Unnamed join has no refname. (Note: since it's unnamed, there is
7814 * no way the user could have referenced it to create a whole-row Var
7815 * for it. So we don't have to cover that case below.)
7816 */
7817 Assert(refname == NULL);
7818 }
7819
7821 attname = NULL;
7822 else if (attnum > 0)
7823 {
7824 /* Get column name to use from the colinfo struct */
7825 if (attnum > colinfo->num_cols)
7826 elog(ERROR, "invalid attnum %d for relation \"%s\"",
7827 attnum, rte->eref->aliasname);
7828 attname = colinfo->colnames[attnum - 1];
7829
7830 /*
7831 * If we find a Var referencing a dropped column, it seems better to
7832 * print something (anything) than to fail. In general this should
7833 * not happen, but it used to be possible for some cases involving
7834 * functions returning named composite types, and perhaps there are
7835 * still bugs out there.
7836 */
7837 if (attname == NULL)
7838 attname = "?dropped?column?";
7839 }
7840 else
7841 {
7842 /* System column - name is fixed, get it from the catalog */
7844 }
7845
7846 need_prefix = (context->varprefix || attname == NULL ||
7848
7849 /*
7850 * If we're considering a plain Var in an ORDER BY (but not GROUP BY)
7851 * clause, we may need to add a table-name prefix to prevent
7852 * findTargetlistEntrySQL92 from misinterpreting the name as an
7853 * output-column name. To avoid cluttering the output with unnecessary
7854 * prefixes, do so only if there is a name match to a SELECT tlist item
7855 * that is different from the Var.
7856 */
7857 if (context->varInOrderBy && !context->inGroupBy && !need_prefix)
7858 {
7859 int colno = 0;
7860
7862 {
7863 char *colname;
7864
7865 if (tle->resjunk)
7866 continue; /* ignore junk entries */
7867 colno++;
7868
7869 /* This must match colname-choosing logic in get_target_list() */
7870 if (context->resultDesc && colno <= context->resultDesc->natts)
7871 colname = NameStr(TupleDescAttr(context->resultDesc,
7872 colno - 1)->attname);
7873 else
7874 colname = tle->resname;
7875
7876 if (colname && strcmp(colname, attname) == 0 &&
7877 !equal(var, tle->expr))
7878 {
7879 need_prefix = true;
7880 break;
7881 }
7882 }
7883 }
7884
7885 if (refname && need_prefix)
7886 {
7889 }
7890 if (attname)
7892 else
7893 {
7895 if (istoplevel)
7896 appendStringInfo(buf, "::%s",
7897 format_type_with_typemod(var->vartype,
7898 var->vartypmod));
7899 }
7900
7901 return attname;
7902}
7903
7904/*
7905 * Deparse a Var which references OUTER_VAR, INNER_VAR, or INDEX_VAR. This
7906 * routine is actually a callback for resolve_special_varno, which handles
7907 * finding the correct TargetEntry. We get the expression contained in that
7908 * TargetEntry and just need to deparse it, a job we can throw back on
7909 * get_rule_expr.
7910 */
7911static void
7912get_special_variable(Node *node, deparse_context *context, void *callback_arg)
7913{
7914 StringInfo buf = context->buf;
7915
7916 /*
7917 * For a non-Var referent, force parentheses because our caller probably
7918 * assumed a Var is a simple expression.
7919 */
7920 if (!IsA(node, Var))
7922 get_rule_expr(node, context, true);
7923 if (!IsA(node, Var))
7925}
7926
7927/*
7928 * Chase through plan references to special varnos (OUTER_VAR, INNER_VAR,
7929 * INDEX_VAR) until we find a real Var or some kind of non-Var node; then,
7930 * invoke the callback provided.
7931 */
7932static void
7934 rsv_callback callback, void *callback_arg)
7935{
7936 Var *var;
7938
7939 /* This function is recursive, so let's be paranoid. */
7941
7942 /* If it's not a Var, invoke the callback. */
7943 if (!IsA(node, Var))
7944 {
7945 (*callback) (node, context, callback_arg);
7946 return;
7947 }
7948
7949 /* Find appropriate nesting depth */
7950 var = (Var *) node;
7952 var->varlevelsup);
7953
7954 /*
7955 * If varno is special, recurse. (Don't worry about varnosyn; if we're
7956 * here, we already decided not to use that.)
7957 */
7958 if (var->varno == OUTER_VAR && dpns->outer_tlist)
7959 {
7963
7964 tle = get_tle_by_resno(dpns->outer_tlist, var->varattno);
7965 if (!tle)
7966 elog(ERROR, "bogus varattno for OUTER_VAR var: %d", var->varattno);
7967
7968 /*
7969 * If we're descending to the first child of an Append or MergeAppend,
7970 * update appendparents. This will affect deparsing of all Vars
7971 * appearing within the eventually-resolved subexpression.
7972 */
7974
7975 if (IsA(dpns->plan, Append))
7976 context->appendparents = bms_union(context->appendparents,
7977 ((Append *) dpns->plan)->apprelids);
7978 else if (IsA(dpns->plan, MergeAppend))
7979 context->appendparents = bms_union(context->appendparents,
7980 ((MergeAppend *) dpns->plan)->apprelids);
7981
7982 push_child_plan(dpns, dpns->outer_plan, &save_dpns);
7983 resolve_special_varno((Node *) tle->expr, context,
7984 callback, callback_arg);
7987 return;
7988 }
7989 else if (var->varno == INNER_VAR && dpns->inner_tlist)
7990 {
7993
7994 tle = get_tle_by_resno(dpns->inner_tlist, var->varattno);
7995 if (!tle)
7996 elog(ERROR, "bogus varattno for INNER_VAR var: %d", var->varattno);
7997
7998 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
7999 resolve_special_varno((Node *) tle->expr, context,
8000 callback, callback_arg);
8002 return;
8003 }
8004 else if (var->varno == INDEX_VAR && dpns->index_tlist)
8005 {
8007
8008 tle = get_tle_by_resno(dpns->index_tlist, var->varattno);
8009 if (!tle)
8010 elog(ERROR, "bogus varattno for INDEX_VAR var: %d", var->varattno);
8011
8012 resolve_special_varno((Node *) tle->expr, context,
8013 callback, callback_arg);
8014 return;
8015 }
8016 else if (var->varno < 1 || var->varno > list_length(dpns->rtable))
8017 elog(ERROR, "bogus varno: %d", var->varno);
8018
8019 /* Not special. Just invoke the callback. */
8020 (*callback) (node, context, callback_arg);
8021}
8022
8023/*
8024 * Get the name of a field of an expression of composite type. The
8025 * expression is usually a Var, but we handle other cases too.
8026 *
8027 * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
8028 *
8029 * This is fairly straightforward when the expression has a named composite
8030 * type; we need only look up the type in the catalogs. However, the type
8031 * could also be RECORD. Since no actual table or view column is allowed to
8032 * have type RECORD, a Var of type RECORD must refer to a JOIN or FUNCTION RTE
8033 * or to a subquery output. We drill down to find the ultimate defining
8034 * expression and attempt to infer the field name from it. We ereport if we
8035 * can't determine the name.
8036 *
8037 * Similarly, a PARAM of type RECORD has to refer to some expression of
8038 * a determinable composite type.
8039 */
8040static const char *
8042 int levelsup, deparse_context *context)
8043{
8046 int netlevelsup;
8048 int varno;
8049 AttrNumber varattno;
8051 Node *expr;
8052
8053 /*
8054 * If it's a RowExpr that was expanded from a whole-row Var, use the
8055 * column names attached to it. (We could let get_expr_result_tupdesc()
8056 * handle this, but it's much cheaper to just pull out the name we need.)
8057 */
8058 if (IsA(var, RowExpr))
8059 {
8060 RowExpr *r = (RowExpr *) var;
8061
8062 if (fieldno > 0 && fieldno <= list_length(r->colnames))
8063 return strVal(list_nth(r->colnames, fieldno - 1));
8064 }
8065
8066 /*
8067 * If it's a Param of type RECORD, try to find what the Param refers to.
8068 */
8069 if (IsA(var, Param))
8070 {
8071 Param *param = (Param *) var;
8073
8074 expr = find_param_referent(param, context, &dpns, &ancestor_cell);
8075 if (expr)
8076 {
8077 /* Found a match, so recurse to decipher the field name */
8079 const char *result;
8080
8082 result = get_name_for_var_field((Var *) expr, fieldno,
8083 0, context);
8085 return result;
8086 }
8087 }
8088
8089 /*
8090 * If it's a Var of type RECORD, we have to find what the Var refers to;
8091 * if not, we can use get_expr_result_tupdesc().
8092 */
8093 if (!IsA(var, Var) ||
8094 var->vartype != RECORDOID)
8095 {
8096 tupleDesc = get_expr_result_tupdesc((Node *) var, false);
8097 /* Got the tupdesc, so we can extract the field name */
8100 }
8101
8102 /* Find appropriate nesting depth */
8103 netlevelsup = var->varlevelsup + levelsup;
8104 if (netlevelsup >= list_length(context->namespaces))
8105 elog(ERROR, "bogus varlevelsup: %d offset %d",
8106 var->varlevelsup, levelsup);
8108 netlevelsup);
8109
8110 /*
8111 * If we have a syntactic referent for the Var, and we're working from a
8112 * parse tree, prefer to use the syntactic referent. Otherwise, fall back
8113 * on the semantic referent. (See comments in get_variable().)
8114 */
8115 if (var->varnosyn > 0 && dpns->plan == NULL)
8116 {
8117 varno = var->varnosyn;
8118 varattno = var->varattnosyn;
8119 }
8120 else
8121 {
8122 varno = var->varno;
8123 varattno = var->varattno;
8124 }
8125
8126 /*
8127 * Try to find the relevant RTE in this rtable. In a plan tree, it's
8128 * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
8129 * down into the subplans, or INDEX_VAR, which is resolved similarly.
8130 *
8131 * Note: unlike get_variable and resolve_special_varno, we need not worry
8132 * about inheritance mapping: a child Var should have the same datatype as
8133 * its parent, and here we're really only interested in the Var's type.
8134 */
8135 if (varno >= 1 && varno <= list_length(dpns->rtable))
8136 {
8137 rte = rt_fetch(varno, dpns->rtable);
8138 attnum = varattno;
8139 }
8140 else if (varno == OUTER_VAR && dpns->outer_tlist)
8141 {
8144 const char *result;
8145
8146 tle = get_tle_by_resno(dpns->outer_tlist, varattno);
8147 if (!tle)
8148 elog(ERROR, "bogus varattno for OUTER_VAR var: %d", varattno);
8149
8150 Assert(netlevelsup == 0);
8151 push_child_plan(dpns, dpns->outer_plan, &save_dpns);
8152
8153 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8154 levelsup, context);
8155
8157 return result;
8158 }
8159 else if (varno == INNER_VAR && dpns->inner_tlist)
8160 {
8163 const char *result;
8164
8165 tle = get_tle_by_resno(dpns->inner_tlist, varattno);
8166 if (!tle)
8167 elog(ERROR, "bogus varattno for INNER_VAR var: %d", varattno);
8168
8169 Assert(netlevelsup == 0);
8170 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8171
8172 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8173 levelsup, context);
8174
8176 return result;
8177 }
8178 else if (varno == INDEX_VAR && dpns->index_tlist)
8179 {
8181 const char *result;
8182
8183 tle = get_tle_by_resno(dpns->index_tlist, varattno);
8184 if (!tle)
8185 elog(ERROR, "bogus varattno for INDEX_VAR var: %d", varattno);
8186
8187 Assert(netlevelsup == 0);
8188
8189 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8190 levelsup, context);
8191
8192 return result;
8193 }
8194 else
8195 {
8196 elog(ERROR, "bogus varno: %d", varno);
8197 return NULL; /* keep compiler quiet */
8198 }
8199
8201 {
8202 /* Var is whole-row reference to RTE, so select the right field */
8204 }
8205
8206 /*
8207 * This part has essentially the same logic as the parser's
8208 * expandRecordVariable() function, but we are dealing with a different
8209 * representation of the input context, and we only need one field name
8210 * not a TupleDesc. Also, we need special cases for finding subquery and
8211 * CTE subplans when deparsing Plan trees.
8212 */
8213 expr = (Node *) var; /* default if we can't drill down */
8214
8215 switch (rte->rtekind)
8216 {
8217 case RTE_RELATION:
8218 case RTE_VALUES:
8220 case RTE_RESULT:
8221
8222 /*
8223 * This case should not occur: a column of a table, values list,
8224 * or ENR shouldn't have type RECORD. Fall through and fail (most
8225 * likely) at the bottom.
8226 */
8227 break;
8228 case RTE_SUBQUERY:
8229 /* Subselect-in-FROM: examine sub-select's output expr */
8230 {
8231 if (rte->subquery)
8232 {
8233 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
8234 attnum);
8235
8236 if (ste == NULL || ste->resjunk)
8237 elog(ERROR, "subquery %s does not have attribute %d",
8238 rte->eref->aliasname, attnum);
8239 expr = (Node *) ste->expr;
8240 if (IsA(expr, Var))
8241 {
8242 /*
8243 * Recurse into the sub-select to see what its Var
8244 * refers to. We have to build an additional level of
8245 * namespace to keep in step with varlevelsup in the
8246 * subselect; furthermore, the subquery RTE might be
8247 * from an outer query level, in which case the
8248 * namespace for the subselect must have that outer
8249 * level as parent namespace.
8250 */
8251 List *save_nslist = context->namespaces;
8254 const char *result;
8255
8257 netlevelsup);
8258
8259 set_deparse_for_query(&mydpns, rte->subquery,
8261
8263
8264 result = get_name_for_var_field((Var *) expr, fieldno,
8265 0, context);
8266
8267 context->namespaces = save_nslist;
8268
8269 return result;
8270 }
8271 /* else fall through to inspect the expression */
8272 }
8273 else
8274 {
8275 /*
8276 * We're deparsing a Plan tree so we don't have complete
8277 * RTE entries (in particular, rte->subquery is NULL). But
8278 * the only place we'd normally see a Var directly
8279 * referencing a SUBQUERY RTE is in a SubqueryScan plan
8280 * node, and we can look into the child plan's tlist
8281 * instead. An exception occurs if the subquery was
8282 * proven empty and optimized away: then we'd find such a
8283 * Var in a childless Result node, and there's nothing in
8284 * the plan tree that would let us figure out what it had
8285 * originally referenced. In that case, fall back on
8286 * printing "fN", analogously to the default column names
8287 * for RowExprs.
8288 */
8291 const char *result;
8292
8293 if (!dpns->inner_plan)
8294 {
8295 char *dummy_name = palloc(32);
8296
8297 Assert(dpns->plan && IsA(dpns->plan, Result));
8298 snprintf(dummy_name, 32, "f%d", fieldno);
8299 return dummy_name;
8300 }
8301 Assert(dpns->plan && IsA(dpns->plan, SubqueryScan));
8302
8303 tle = get_tle_by_resno(dpns->inner_tlist, attnum);
8304 if (!tle)
8305 elog(ERROR, "bogus varattno for subquery var: %d",
8306 attnum);
8307 Assert(netlevelsup == 0);
8308 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8309
8310 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8311 levelsup, context);
8312
8314 return result;
8315 }
8316 }
8317 break;
8318 case RTE_JOIN:
8319 /* Join RTE --- recursively inspect the alias variable */
8320 if (rte->joinaliasvars == NIL)
8321 elog(ERROR, "cannot decompile join alias var in plan tree");
8322 Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
8323 expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
8324 Assert(expr != NULL);
8325 /* we intentionally don't strip implicit coercions here */
8326 if (IsA(expr, Var))
8327 return get_name_for_var_field((Var *) expr, fieldno,
8328 var->varlevelsup + levelsup,
8329 context);
8330 /* else fall through to inspect the expression */
8331 break;
8332 case RTE_FUNCTION:
8333 case RTE_TABLEFUNC:
8334
8335 /*
8336 * We couldn't get here unless a function is declared with one of
8337 * its result columns as RECORD, which is not allowed.
8338 */
8339 break;
8340 case RTE_CTE:
8341 /* CTE reference: examine subquery's output expr */
8342 {
8343 CommonTableExpr *cte = NULL;
8344 Index ctelevelsup;
8345 ListCell *lc;
8346
8347 /*
8348 * Try to find the referenced CTE using the namespace stack.
8349 */
8350 ctelevelsup = rte->ctelevelsup + netlevelsup;
8351 if (ctelevelsup >= list_length(context->namespaces))
8352 lc = NULL;
8353 else
8354 {
8356
8358 list_nth(context->namespaces, ctelevelsup);
8359 foreach(lc, ctedpns->ctes)
8360 {
8361 cte = (CommonTableExpr *) lfirst(lc);
8362 if (strcmp(cte->ctename, rte->ctename) == 0)
8363 break;
8364 }
8365 }
8366 if (lc != NULL)
8367 {
8368 Query *ctequery = (Query *) cte->ctequery;
8370 attnum);
8371
8372 if (ste == NULL || ste->resjunk)
8373 elog(ERROR, "CTE %s does not have attribute %d",
8374 rte->eref->aliasname, attnum);
8375 expr = (Node *) ste->expr;
8376 if (IsA(expr, Var))
8377 {
8378 /*
8379 * Recurse into the CTE to see what its Var refers to.
8380 * We have to build an additional level of namespace
8381 * to keep in step with varlevelsup in the CTE;
8382 * furthermore it could be an outer CTE (compare
8383 * SUBQUERY case above).
8384 */
8385 List *save_nslist = context->namespaces;
8388 const char *result;
8389
8391 ctelevelsup);
8392
8393 set_deparse_for_query(&mydpns, ctequery,
8395
8397
8398 result = get_name_for_var_field((Var *) expr, fieldno,
8399 0, context);
8400
8401 context->namespaces = save_nslist;
8402
8403 return result;
8404 }
8405 /* else fall through to inspect the expression */
8406 }
8407 else
8408 {
8409 /*
8410 * We're deparsing a Plan tree so we don't have a CTE
8411 * list. But the only places we'd normally see a Var
8412 * directly referencing a CTE RTE are in CteScan or
8413 * WorkTableScan plan nodes. For those cases,
8414 * set_deparse_plan arranged for dpns->inner_plan to be
8415 * the plan node that emits the CTE or RecursiveUnion
8416 * result, and we can look at its tlist instead. As
8417 * above, this can fail if the CTE has been proven empty,
8418 * in which case fall back to "fN".
8419 */
8422 const char *result;
8423
8424 if (!dpns->inner_plan)
8425 {
8426 char *dummy_name = palloc(32);
8427
8428 Assert(dpns->plan && IsA(dpns->plan, Result));
8429 snprintf(dummy_name, 32, "f%d", fieldno);
8430 return dummy_name;
8431 }
8432 Assert(dpns->plan && (IsA(dpns->plan, CteScan) ||
8433 IsA(dpns->plan, WorkTableScan)));
8434
8435 tle = get_tle_by_resno(dpns->inner_tlist, attnum);
8436 if (!tle)
8437 elog(ERROR, "bogus varattno for subquery var: %d",
8438 attnum);
8439 Assert(netlevelsup == 0);
8440 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8441
8442 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8443 levelsup, context);
8444
8446 return result;
8447 }
8448 }
8449 break;
8450 case RTE_GROUP:
8451
8452 /*
8453 * We couldn't get here: any Vars that reference the RTE_GROUP RTE
8454 * should have been replaced with the underlying grouping
8455 * expressions.
8456 */
8457 break;
8458 }
8459
8460 /*
8461 * We now have an expression we can't expand any more, so see if
8462 * get_expr_result_tupdesc() can do anything with it.
8463 */
8464 tupleDesc = get_expr_result_tupdesc(expr, false);
8465 /* Got the tupdesc, so we can extract the field name */
8468}
8469
8470/*
8471 * Try to find the referenced expression for a PARAM_EXEC Param that might
8472 * reference a parameter supplied by an upper NestLoop or SubPlan plan node.
8473 *
8474 * If successful, return the expression and set *dpns_p and *ancestor_cell_p
8475 * appropriately for calling push_ancestor_plan(). If no referent can be
8476 * found, return NULL.
8477 */
8478static Node *
8481{
8482 /* Initialize output parameters to prevent compiler warnings */
8483 *dpns_p = NULL;
8485
8486 /*
8487 * If it's a PARAM_EXEC parameter, look for a matching NestLoopParam or
8488 * SubPlan argument. This will necessarily be in some ancestor of the
8489 * current expression's Plan node.
8490 */
8491 if (param->paramkind == PARAM_EXEC)
8492 {
8495 ListCell *lc;
8496
8497 dpns = (deparse_namespace *) linitial(context->namespaces);
8498 child_plan = dpns->plan;
8499
8500 foreach(lc, dpns->ancestors)
8501 {
8502 Node *ancestor = (Node *) lfirst(lc);
8503 ListCell *lc2;
8504
8505 /*
8506 * NestLoops transmit params to their inner child only.
8507 */
8508 if (IsA(ancestor, NestLoop) &&
8510 {
8512
8513 foreach(lc2, nl->nestParams)
8514 {
8516
8517 if (nlp->paramno == param->paramid)
8518 {
8519 /* Found a match, so return it */
8520 *dpns_p = dpns;
8522 return (Node *) nlp->paramval;
8523 }
8524 }
8525 }
8526
8527 /*
8528 * If ancestor is a SubPlan, check the arguments it provides.
8529 */
8530 if (IsA(ancestor, SubPlan))
8531 {
8532 SubPlan *subplan = (SubPlan *) ancestor;
8533 ListCell *lc3;
8534 ListCell *lc4;
8535
8536 forboth(lc3, subplan->parParam, lc4, subplan->args)
8537 {
8538 int paramid = lfirst_int(lc3);
8539 Node *arg = (Node *) lfirst(lc4);
8540
8541 if (paramid == param->paramid)
8542 {
8543 /*
8544 * Found a match, so return it. But, since Vars in
8545 * the arg are to be evaluated in the surrounding
8546 * context, we have to point to the next ancestor item
8547 * that is *not* a SubPlan.
8548 */
8549 ListCell *rest;
8550
8551 for_each_cell(rest, dpns->ancestors,
8552 lnext(dpns->ancestors, lc))
8553 {
8554 Node *ancestor2 = (Node *) lfirst(rest);
8555
8556 if (!IsA(ancestor2, SubPlan))
8557 {
8558 *dpns_p = dpns;
8560 return arg;
8561 }
8562 }
8563 elog(ERROR, "SubPlan cannot be outermost ancestor");
8564 }
8565 }
8566
8567 /* SubPlan isn't a kind of Plan, so skip the rest */
8568 continue;
8569 }
8570
8571 /*
8572 * We need not consider the ancestor's initPlan list, since
8573 * initplans never have any parParams.
8574 */
8575
8576 /* No luck, crawl up to next ancestor */
8577 child_plan = (Plan *) ancestor;
8578 }
8579 }
8580
8581 /* No referent found */
8582 return NULL;
8583}
8584
8585/*
8586 * Try to find a subplan/initplan that emits the value for a PARAM_EXEC Param.
8587 *
8588 * If successful, return the generating subplan/initplan and set *column_p
8589 * to the subplan's 0-based output column number.
8590 * Otherwise, return NULL.
8591 */
8592static SubPlan *
8594{
8595 /* Initialize output parameter to prevent compiler warnings */
8596 *column_p = 0;
8597
8598 /*
8599 * If it's a PARAM_EXEC parameter, search the current plan node as well as
8600 * ancestor nodes looking for a subplan or initplan that emits the value
8601 * for the Param. It could appear in the setParams of an initplan or
8602 * MULTIEXPR_SUBLINK subplan, or in the paramIds of an ancestral SubPlan.
8603 */
8604 if (param->paramkind == PARAM_EXEC)
8605 {
8606 SubPlan *result;
8608 ListCell *lc;
8609
8610 dpns = (deparse_namespace *) linitial(context->namespaces);
8611
8612 /* First check the innermost plan node's initplans */
8613 result = find_param_generator_initplan(param, dpns->plan, column_p);
8614 if (result)
8615 return result;
8616
8617 /*
8618 * The plan's targetlist might contain MULTIEXPR_SUBLINK SubPlans,
8619 * which can be referenced by Params elsewhere in the targetlist.
8620 * (Such Params should always be in the same targetlist, so there's no
8621 * need to do this work at upper plan nodes.)
8622 */
8623 foreach_node(TargetEntry, tle, dpns->plan->targetlist)
8624 {
8625 if (tle->expr && IsA(tle->expr, SubPlan))
8626 {
8627 SubPlan *subplan = (SubPlan *) tle->expr;
8628
8629 if (subplan->subLinkType == MULTIEXPR_SUBLINK)
8630 {
8631 foreach_int(paramid, subplan->setParam)
8632 {
8633 if (paramid == param->paramid)
8634 {
8635 /* Found a match, so return it. */
8636 *column_p = foreach_current_index(paramid);
8637 return subplan;
8638 }
8639 }
8640 }
8641 }
8642 }
8643
8644 /* No luck, so check the ancestor nodes */
8645 foreach(lc, dpns->ancestors)
8646 {
8647 Node *ancestor = (Node *) lfirst(lc);
8648
8649 /*
8650 * If ancestor is a SubPlan, check the paramIds it provides.
8651 */
8652 if (IsA(ancestor, SubPlan))
8653 {
8654 SubPlan *subplan = (SubPlan *) ancestor;
8655
8656 foreach_int(paramid, subplan->paramIds)
8657 {
8658 if (paramid == param->paramid)
8659 {
8660 /* Found a match, so return it. */
8661 *column_p = foreach_current_index(paramid);
8662 return subplan;
8663 }
8664 }
8665
8666 /* SubPlan isn't a kind of Plan, so skip the rest */
8667 continue;
8668 }
8669
8670 /*
8671 * Otherwise, it's some kind of Plan node, so check its initplans.
8672 */
8673 result = find_param_generator_initplan(param, (Plan *) ancestor,
8674 column_p);
8675 if (result)
8676 return result;
8677
8678 /* No luck, crawl up to next ancestor */
8679 }
8680 }
8681
8682 /* No generator found */
8683 return NULL;
8684}
8685
8686/*
8687 * Subroutine for find_param_generator: search one Plan node's initplans
8688 */
8689static SubPlan *
8691{
8692 foreach_node(SubPlan, subplan, plan->initPlan)
8693 {
8694 foreach_int(paramid, subplan->setParam)
8695 {
8696 if (paramid == param->paramid)
8697 {
8698 /* Found a match, so return it. */
8699 *column_p = foreach_current_index(paramid);
8700 return subplan;
8701 }
8702 }
8703 }
8704 return NULL;
8705}
8706
8707/*
8708 * Display a Param appropriately.
8709 */
8710static void
8712{
8713 Node *expr;
8716 SubPlan *subplan;
8717 int column;
8718
8719 /*
8720 * If it's a PARAM_EXEC parameter, try to locate the expression from which
8721 * the parameter was computed. This stanza handles only cases in which
8722 * the Param represents an input to the subplan we are currently in.
8723 */
8724 expr = find_param_referent(param, context, &dpns, &ancestor_cell);
8725 if (expr)
8726 {
8727 /* Found a match, so print it */
8729 bool save_varprefix;
8730 bool need_paren;
8731
8732 /* Switch attention to the ancestor plan node */
8734
8735 /*
8736 * Force prefixing of Vars, since they won't belong to the relation
8737 * being scanned in the original plan node.
8738 */
8739 save_varprefix = context->varprefix;
8740 context->varprefix = true;
8741
8742 /*
8743 * A Param's expansion is typically a Var, Aggref, GroupingFunc, or
8744 * upper-level Param, which wouldn't need extra parentheses.
8745 * Otherwise, insert parens to ensure the expression looks atomic.
8746 */
8747 need_paren = !(IsA(expr, Var) ||
8748 IsA(expr, Aggref) ||
8749 IsA(expr, GroupingFunc) ||
8750 IsA(expr, Param));
8751 if (need_paren)
8752 appendStringInfoChar(context->buf, '(');
8753
8754 get_rule_expr(expr, context, false);
8755
8756 if (need_paren)
8757 appendStringInfoChar(context->buf, ')');
8758
8759 context->varprefix = save_varprefix;
8760
8762
8763 return;
8764 }
8765
8766 /*
8767 * Alternatively, maybe it's a subplan output, which we print as a
8768 * reference to the subplan. (We could drill down into the subplan and
8769 * print the relevant targetlist expression, but that has been deemed too
8770 * confusing since it would violate normal SQL scope rules. Also, we're
8771 * relying on this reference to show that the testexpr containing the
8772 * Param has anything to do with that subplan at all.)
8773 */
8774 subplan = find_param_generator(param, context, &column);
8775 if (subplan)
8776 {
8777 const char *nameprefix;
8778
8779 if (subplan->isInitPlan)
8780 nameprefix = "InitPlan ";
8781 else
8782 nameprefix = "SubPlan ";
8783
8784 appendStringInfo(context->buf, "(%s%s%s).col%d",
8785 subplan->useHashTable ? "hashed " : "",
8786 nameprefix,
8787 subplan->plan_name, column + 1);
8788
8789 return;
8790 }
8791
8792 /*
8793 * If it's an external parameter, see if the outermost namespace provides
8794 * function argument names.
8795 */
8796 if (param->paramkind == PARAM_EXTERN && context->namespaces != NIL)
8797 {
8798 dpns = llast(context->namespaces);
8799 if (dpns->argnames &&
8800 param->paramid > 0 &&
8801 param->paramid <= dpns->numargs)
8802 {
8803 char *argname = dpns->argnames[param->paramid - 1];
8804
8805 if (argname)
8806 {
8807 bool should_qualify = false;
8808 ListCell *lc;
8809
8810 /*
8811 * Qualify the parameter name if there are any other deparse
8812 * namespaces with range tables. This avoids qualifying in
8813 * trivial cases like "RETURN a + b", but makes it safe in all
8814 * other cases.
8815 */
8816 foreach(lc, context->namespaces)
8817 {
8819
8820 if (depns->rtable_names != NIL)
8821 {
8822 should_qualify = true;
8823 break;
8824 }
8825 }
8826 if (should_qualify)
8827 {
8828 appendStringInfoString(context->buf, quote_identifier(dpns->funcname));
8829 appendStringInfoChar(context->buf, '.');
8830 }
8831
8832 appendStringInfoString(context->buf, quote_identifier(argname));
8833 return;
8834 }
8835 }
8836 }
8837
8838 /*
8839 * Not PARAM_EXEC, or couldn't find referent: just print $N.
8840 *
8841 * It's a bug if we get here for anything except PARAM_EXTERN Params, but
8842 * in production builds printing $N seems more useful than failing.
8843 */
8844 Assert(param->paramkind == PARAM_EXTERN);
8845
8846 appendStringInfo(context->buf, "$%d", param->paramid);
8847}
8848
8849/*
8850 * get_simple_binary_op_name
8851 *
8852 * helper function for isSimpleNode
8853 * will return single char binary operator name, or NULL if it's not
8854 */
8855static const char *
8857{
8858 List *args = expr->args;
8859
8860 if (list_length(args) == 2)
8861 {
8862 /* binary operator */
8863 Node *arg1 = (Node *) linitial(args);
8864 Node *arg2 = (Node *) lsecond(args);
8865 const char *op;
8866
8868 if (strlen(op) == 1)
8869 return op;
8870 }
8871 return NULL;
8872}
8873
8874
8875/*
8876 * isSimpleNode - check if given node is simple (doesn't need parenthesizing)
8877 *
8878 * true : simple in the context of parent node's type
8879 * false : not simple
8880 */
8881static bool
8882isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
8883{
8884 if (!node)
8885 return false;
8886
8887 switch (nodeTag(node))
8888 {
8889 case T_Var:
8890 case T_Const:
8891 case T_Param:
8893 case T_SetToDefault:
8894 case T_CurrentOfExpr:
8895 /* single words: always simple */
8896 return true;
8897
8898 case T_SubscriptingRef:
8899 case T_ArrayExpr:
8900 case T_RowExpr:
8901 case T_CoalesceExpr:
8902 case T_MinMaxExpr:
8903 case T_SQLValueFunction:
8904 case T_XmlExpr:
8905 case T_NextValueExpr:
8906 case T_NullIfExpr:
8907 case T_Aggref:
8908 case T_GroupingFunc:
8909 case T_WindowFunc:
8910 case T_MergeSupportFunc:
8911 case T_FuncExpr:
8913 case T_JsonExpr:
8914 /* function-like: name(..) or name[..] */
8915 return true;
8916
8917 /* CASE keywords act as parentheses */
8918 case T_CaseExpr:
8919 return true;
8920
8921 case T_FieldSelect:
8922
8923 /*
8924 * appears simple since . has top precedence, unless parent is
8925 * T_FieldSelect itself!
8926 */
8927 return !IsA(parentNode, FieldSelect);
8928
8929 case T_FieldStore:
8930
8931 /*
8932 * treat like FieldSelect (probably doesn't matter)
8933 */
8934 return !IsA(parentNode, FieldStore);
8935
8936 case T_CoerceToDomain:
8937 /* maybe simple, check args */
8938 return isSimpleNode((Node *) ((CoerceToDomain *) node)->arg,
8939 node, prettyFlags);
8940 case T_RelabelType:
8941 return isSimpleNode((Node *) ((RelabelType *) node)->arg,
8942 node, prettyFlags);
8943 case T_CoerceViaIO:
8944 return isSimpleNode((Node *) ((CoerceViaIO *) node)->arg,
8945 node, prettyFlags);
8946 case T_ArrayCoerceExpr:
8947 return isSimpleNode((Node *) ((ArrayCoerceExpr *) node)->arg,
8948 node, prettyFlags);
8950 return isSimpleNode((Node *) ((ConvertRowtypeExpr *) node)->arg,
8951 node, prettyFlags);
8952 case T_ReturningExpr:
8953 return isSimpleNode((Node *) ((ReturningExpr *) node)->retexpr,
8954 node, prettyFlags);
8955
8956 case T_OpExpr:
8957 {
8958 /* depends on parent node type; needs further checking */
8959 if (prettyFlags & PRETTYFLAG_PAREN && IsA(parentNode, OpExpr))
8960 {
8961 const char *op;
8962 const char *parentOp;
8963 bool is_lopriop;
8964 bool is_hipriop;
8965 bool is_lopriparent;
8966 bool is_hipriparent;
8967
8968 op = get_simple_binary_op_name((OpExpr *) node);
8969 if (!op)
8970 return false;
8971
8972 /* We know only the basic operators + - and * / % */
8973 is_lopriop = (strchr("+-", *op) != NULL);
8974 is_hipriop = (strchr("*/%", *op) != NULL);
8975 if (!(is_lopriop || is_hipriop))
8976 return false;
8977
8979 if (!parentOp)
8980 return false;
8981
8982 is_lopriparent = (strchr("+-", *parentOp) != NULL);
8983 is_hipriparent = (strchr("*/%", *parentOp) != NULL);
8985 return false;
8986
8988 return true; /* op binds tighter than parent */
8989
8991 return false;
8992
8993 /*
8994 * Operators are same priority --- can skip parens only if
8995 * we have (a - b) - c, not a - (b - c).
8996 */
8997 if (node == (Node *) linitial(((OpExpr *) parentNode)->args))
8998 return true;
8999
9000 return false;
9001 }
9002 /* else do the same stuff as for T_SubLink et al. */
9003 }
9005
9006 case T_SubLink:
9007 case T_NullTest:
9008 case T_BooleanTest:
9009 case T_DistinctExpr:
9010 case T_JsonIsPredicate:
9011 switch (nodeTag(parentNode))
9012 {
9013 case T_FuncExpr:
9014 {
9015 /* special handling for casts and COERCE_SQL_SYNTAX */
9016 CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
9017
9018 if (type == COERCE_EXPLICIT_CAST ||
9021 return false;
9022 return true; /* own parentheses */
9023 }
9024 case T_BoolExpr: /* lower precedence */
9025 case T_SubscriptingRef: /* other separators */
9026 case T_ArrayExpr: /* other separators */
9027 case T_RowExpr: /* other separators */
9028 case T_CoalesceExpr: /* own parentheses */
9029 case T_MinMaxExpr: /* own parentheses */
9030 case T_XmlExpr: /* own parentheses */
9031 case T_NullIfExpr: /* other separators */
9032 case T_Aggref: /* own parentheses */
9033 case T_GroupingFunc: /* own parentheses */
9034 case T_WindowFunc: /* own parentheses */
9035 case T_CaseExpr: /* other separators */
9036 return true;
9037 default:
9038 return false;
9039 }
9040
9041 case T_BoolExpr:
9042 switch (nodeTag(parentNode))
9043 {
9044 case T_BoolExpr:
9045 if (prettyFlags & PRETTYFLAG_PAREN)
9046 {
9049
9050 type = ((BoolExpr *) node)->boolop;
9051 parentType = ((BoolExpr *) parentNode)->boolop;
9052 switch (type)
9053 {
9054 case NOT_EXPR:
9055 case AND_EXPR:
9057 return true;
9058 break;
9059 case OR_EXPR:
9060 if (parentType == OR_EXPR)
9061 return true;
9062 break;
9063 }
9064 }
9065 return false;
9066 case T_FuncExpr:
9067 {
9068 /* special handling for casts and COERCE_SQL_SYNTAX */
9069 CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
9070
9071 if (type == COERCE_EXPLICIT_CAST ||
9074 return false;
9075 return true; /* own parentheses */
9076 }
9077 case T_SubscriptingRef: /* other separators */
9078 case T_ArrayExpr: /* other separators */
9079 case T_RowExpr: /* other separators */
9080 case T_CoalesceExpr: /* own parentheses */
9081 case T_MinMaxExpr: /* own parentheses */
9082 case T_XmlExpr: /* own parentheses */
9083 case T_NullIfExpr: /* other separators */
9084 case T_Aggref: /* own parentheses */
9085 case T_GroupingFunc: /* own parentheses */
9086 case T_WindowFunc: /* own parentheses */
9087 case T_CaseExpr: /* other separators */
9088 case T_JsonExpr: /* own parentheses */
9089 return true;
9090 default:
9091 return false;
9092 }
9093
9094 case T_JsonValueExpr:
9095 /* maybe simple, check args */
9096 return isSimpleNode((Node *) ((JsonValueExpr *) node)->raw_expr,
9097 node, prettyFlags);
9098
9099 default:
9100 break;
9101 }
9102 /* those we don't know: in dubio complexo */
9103 return false;
9104}
9105
9106
9107/*
9108 * appendContextKeyword - append a keyword to buffer
9109 *
9110 * If prettyPrint is enabled, perform a line break, and adjust indentation.
9111 * Otherwise, just append the keyword.
9112 */
9113static void
9115 int indentBefore, int indentAfter, int indentPlus)
9116{
9117 StringInfo buf = context->buf;
9118
9119 if (PRETTY_INDENT(context))
9120 {
9121 int indentAmount;
9122
9123 context->indentLevel += indentBefore;
9124
9125 /* remove any trailing spaces currently in the buffer ... */
9127 /* ... then add a newline and some spaces */
9129
9130 if (context->indentLevel < PRETTYINDENT_LIMIT)
9131 indentAmount = Max(context->indentLevel, 0) + indentPlus;
9132 else
9133 {
9134 /*
9135 * If we're indented more than PRETTYINDENT_LIMIT characters, try
9136 * to conserve horizontal space by reducing the per-level
9137 * indentation. For best results the scale factor here should
9138 * divide all the indent amounts that get added to indentLevel
9139 * (PRETTYINDENT_STD, etc). It's important that the indentation
9140 * not grow unboundedly, else deeply-nested trees use O(N^2)
9141 * whitespace; so we also wrap modulo PRETTYINDENT_LIMIT.
9142 */
9144 (context->indentLevel - PRETTYINDENT_LIMIT) /
9145 (PRETTYINDENT_STD / 2);
9147 /* scale/wrap logic affects indentLevel, but not indentPlus */
9149 }
9151
9153
9154 context->indentLevel += indentAfter;
9155 if (context->indentLevel < 0)
9156 context->indentLevel = 0;
9157 }
9158 else
9160}
9161
9162/*
9163 * removeStringInfoSpaces - delete trailing spaces from a buffer.
9164 *
9165 * Possibly this should move to stringinfo.c at some point.
9166 */
9167static void
9169{
9170 while (str->len > 0 && str->data[str->len - 1] == ' ')
9171 str->data[--(str->len)] = '\0';
9172}
9173
9174
9175/*
9176 * get_rule_expr_paren - deparse expr using get_rule_expr,
9177 * embracing the string with parentheses if necessary for prettyPrint.
9178 *
9179 * Never embrace if prettyFlags=0, because it's done in the calling node.
9180 *
9181 * Any node that does *not* embrace its argument node by sql syntax (with
9182 * parentheses, non-operator keywords like CASE/WHEN/ON, or comma etc) should
9183 * use get_rule_expr_paren instead of get_rule_expr so parentheses can be
9184 * added.
9185 */
9186static void
9189{
9190 bool need_paren;
9191
9192 need_paren = PRETTY_PAREN(context) &&
9193 !isSimpleNode(node, parentNode, context->prettyFlags);
9194
9195 if (need_paren)
9196 appendStringInfoChar(context->buf, '(');
9197
9198 get_rule_expr(node, context, showimplicit);
9199
9200 if (need_paren)
9201 appendStringInfoChar(context->buf, ')');
9202}
9203
9204static void
9206 const char *on)
9207{
9208 /*
9209 * The order of array elements must correspond to the order of
9210 * JsonBehaviorType members.
9211 */
9212 const char *behavior_names[] =
9213 {
9214 " NULL",
9215 " ERROR",
9216 " EMPTY",
9217 " TRUE",
9218 " FALSE",
9219 " UNKNOWN",
9220 " EMPTY ARRAY",
9221 " EMPTY OBJECT",
9222 " DEFAULT "
9223 };
9224
9225 if ((int) behavior->btype < 0 || behavior->btype >= lengthof(behavior_names))
9226 elog(ERROR, "invalid json behavior type: %d", behavior->btype);
9227
9228 appendStringInfoString(context->buf, behavior_names[behavior->btype]);
9229
9230 if (behavior->btype == JSON_BEHAVIOR_DEFAULT)
9231 get_rule_expr(behavior->expr, context, false);
9232
9233 appendStringInfo(context->buf, " ON %s", on);
9234}
9235
9236/*
9237 * get_json_expr_options
9238 *
9239 * Parse back common options for JSON_QUERY, JSON_VALUE, JSON_EXISTS and
9240 * JSON_TABLE columns.
9241 */
9242static void
9245{
9246 if (jsexpr->op == JSON_QUERY_OP)
9247 {
9248 if (jsexpr->wrapper == JSW_CONDITIONAL)
9249 appendStringInfoString(context->buf, " WITH CONDITIONAL WRAPPER");
9250 else if (jsexpr->wrapper == JSW_UNCONDITIONAL)
9251 appendStringInfoString(context->buf, " WITH UNCONDITIONAL WRAPPER");
9252 /* The default */
9253 else if (jsexpr->wrapper == JSW_NONE || jsexpr->wrapper == JSW_UNSPEC)
9254 appendStringInfoString(context->buf, " WITHOUT WRAPPER");
9255
9256 if (jsexpr->omit_quotes)
9257 appendStringInfoString(context->buf, " OMIT QUOTES");
9258 /* The default */
9259 else
9260 appendStringInfoString(context->buf, " KEEP QUOTES");
9261 }
9262
9263 if (jsexpr->on_empty && jsexpr->on_empty->btype != default_behavior)
9264 get_json_behavior(jsexpr->on_empty, context, "EMPTY");
9265
9266 if (jsexpr->on_error && jsexpr->on_error->btype != default_behavior)
9267 get_json_behavior(jsexpr->on_error, context, "ERROR");
9268}
9269
9270/* ----------
9271 * get_rule_expr - Parse back an expression
9272 *
9273 * Note: showimplicit determines whether we display any implicit cast that
9274 * is present at the top of the expression tree. It is a passed argument,
9275 * not a field of the context struct, because we change the value as we
9276 * recurse down into the expression. In general we suppress implicit casts
9277 * when the result type is known with certainty (eg, the arguments of an
9278 * OR must be boolean). We display implicit casts for arguments of functions
9279 * and operators, since this is needed to be certain that the same function
9280 * or operator will be chosen when the expression is re-parsed.
9281 * ----------
9282 */
9283static void
9285 bool showimplicit)
9286{
9287 StringInfo buf = context->buf;
9288
9289 if (node == NULL)
9290 return;
9291
9292 /* Guard against excessively long or deeply-nested queries */
9295
9296 /*
9297 * Each level of get_rule_expr must emit an indivisible term
9298 * (parenthesized if necessary) to ensure result is reparsed into the same
9299 * expression tree. The only exception is that when the input is a List,
9300 * we emit the component items comma-separated with no surrounding
9301 * decoration; this is convenient for most callers.
9302 */
9303 switch (nodeTag(node))
9304 {
9305 case T_Var:
9306 (void) get_variable((Var *) node, 0, false, context);
9307 break;
9308
9309 case T_Const:
9310 get_const_expr((Const *) node, context, 0);
9311 break;
9312
9313 case T_Param:
9314 get_parameter((Param *) node, context);
9315 break;
9316
9317 case T_Aggref:
9318 get_agg_expr((Aggref *) node, context, (Aggref *) node);
9319 break;
9320
9321 case T_GroupingFunc:
9322 {
9323 GroupingFunc *gexpr = (GroupingFunc *) node;
9324
9325 appendStringInfoString(buf, "GROUPING(");
9326 get_rule_expr((Node *) gexpr->args, context, true);
9328 }
9329 break;
9330
9331 case T_WindowFunc:
9332 get_windowfunc_expr((WindowFunc *) node, context);
9333 break;
9334
9335 case T_MergeSupportFunc:
9336 appendStringInfoString(buf, "MERGE_ACTION()");
9337 break;
9338
9339 case T_SubscriptingRef:
9340 {
9341 SubscriptingRef *sbsref = (SubscriptingRef *) node;
9342 bool need_parens;
9343
9344 /*
9345 * If the argument is a CaseTestExpr, we must be inside a
9346 * FieldStore, ie, we are assigning to an element of an array
9347 * within a composite column. Since we already punted on
9348 * displaying the FieldStore's target information, just punt
9349 * here too, and display only the assignment source
9350 * expression.
9351 */
9352 if (IsA(sbsref->refexpr, CaseTestExpr))
9353 {
9354 Assert(sbsref->refassgnexpr);
9355 get_rule_expr((Node *) sbsref->refassgnexpr,
9356 context, showimplicit);
9357 break;
9358 }
9359
9360 /*
9361 * Parenthesize the argument unless it's a simple Var or a
9362 * FieldSelect. (In particular, if it's another
9363 * SubscriptingRef, we *must* parenthesize to avoid
9364 * confusion.)
9365 */
9366 need_parens = !IsA(sbsref->refexpr, Var) &&
9367 !IsA(sbsref->refexpr, FieldSelect);
9368 if (need_parens)
9370 get_rule_expr((Node *) sbsref->refexpr, context, showimplicit);
9371 if (need_parens)
9373
9374 /*
9375 * If there's a refassgnexpr, we want to print the node in the
9376 * format "container[subscripts] := refassgnexpr". This is
9377 * not legal SQL, so decompilation of INSERT or UPDATE
9378 * statements should always use processIndirection as part of
9379 * the statement-level syntax. We should only see this when
9380 * EXPLAIN tries to print the targetlist of a plan resulting
9381 * from such a statement.
9382 */
9383 if (sbsref->refassgnexpr)
9384 {
9385 Node *refassgnexpr;
9386
9387 /*
9388 * Use processIndirection to print this node's subscripts
9389 * as well as any additional field selections or
9390 * subscripting in immediate descendants. It returns the
9391 * RHS expr that is actually being "assigned".
9392 */
9393 refassgnexpr = processIndirection(node, context);
9394 appendStringInfoString(buf, " := ");
9395 get_rule_expr(refassgnexpr, context, showimplicit);
9396 }
9397 else
9398 {
9399 /* Just an ordinary container fetch, so print subscripts */
9400 printSubscripts(sbsref, context);
9401 }
9402 }
9403 break;
9404
9405 case T_FuncExpr:
9406 get_func_expr((FuncExpr *) node, context, showimplicit);
9407 break;
9408
9409 case T_NamedArgExpr:
9410 {
9411 NamedArgExpr *na = (NamedArgExpr *) node;
9412
9413 appendStringInfo(buf, "%s => ", quote_identifier(na->name));
9414 get_rule_expr((Node *) na->arg, context, showimplicit);
9415 }
9416 break;
9417
9418 case T_OpExpr:
9419 get_oper_expr((OpExpr *) node, context);
9420 break;
9421
9422 case T_DistinctExpr:
9423 {
9424 DistinctExpr *expr = (DistinctExpr *) node;
9425 List *args = expr->args;
9426 Node *arg1 = (Node *) linitial(args);
9427 Node *arg2 = (Node *) lsecond(args);
9428
9429 if (!PRETTY_PAREN(context))
9431 get_rule_expr_paren(arg1, context, true, node);
9432 appendStringInfoString(buf, " IS DISTINCT FROM ");
9433 get_rule_expr_paren(arg2, context, true, node);
9434 if (!PRETTY_PAREN(context))
9436 }
9437 break;
9438
9439 case T_NullIfExpr:
9440 {
9441 NullIfExpr *nullifexpr = (NullIfExpr *) node;
9442
9443 appendStringInfoString(buf, "NULLIF(");
9444 get_rule_expr((Node *) nullifexpr->args, context, true);
9446 }
9447 break;
9448
9450 {
9451 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
9452 List *args = expr->args;
9453 Node *arg1 = (Node *) linitial(args);
9454 Node *arg2 = (Node *) lsecond(args);
9455
9456 if (!PRETTY_PAREN(context))
9458 get_rule_expr_paren(arg1, context, true, node);
9459 appendStringInfo(buf, " %s %s (",
9461 exprType(arg1),
9463 expr->useOr ? "ANY" : "ALL");
9464 get_rule_expr_paren(arg2, context, true, node);
9465
9466 /*
9467 * There's inherent ambiguity in "x op ANY/ALL (y)" when y is
9468 * a bare sub-SELECT. Since we're here, the sub-SELECT must
9469 * be meant as a scalar sub-SELECT yielding an array value to
9470 * be used in ScalarArrayOpExpr; but the grammar will
9471 * preferentially interpret such a construct as an ANY/ALL
9472 * SubLink. To prevent misparsing the output that way, insert
9473 * a dummy coercion (which will be stripped by parse analysis,
9474 * so no inefficiency is added in dump and reload). This is
9475 * indeed most likely what the user wrote to get the construct
9476 * accepted in the first place.
9477 */
9478 if (IsA(arg2, SubLink) &&
9479 ((SubLink *) arg2)->subLinkType == EXPR_SUBLINK)
9480 appendStringInfo(buf, "::%s",
9482 exprTypmod(arg2)));
9484 if (!PRETTY_PAREN(context))
9486 }
9487 break;
9488
9489 case T_BoolExpr:
9490 {
9491 BoolExpr *expr = (BoolExpr *) node;
9492 Node *first_arg = linitial(expr->args);
9493 ListCell *arg;
9494
9495 switch (expr->boolop)
9496 {
9497 case AND_EXPR:
9498 if (!PRETTY_PAREN(context))
9501 false, node);
9502 for_each_from(arg, expr->args, 1)
9503 {
9504 appendStringInfoString(buf, " AND ");
9505 get_rule_expr_paren((Node *) lfirst(arg), context,
9506 false, node);
9507 }
9508 if (!PRETTY_PAREN(context))
9510 break;
9511
9512 case OR_EXPR:
9513 if (!PRETTY_PAREN(context))
9516 false, node);
9517 for_each_from(arg, expr->args, 1)
9518 {
9519 appendStringInfoString(buf, " OR ");
9520 get_rule_expr_paren((Node *) lfirst(arg), context,
9521 false, node);
9522 }
9523 if (!PRETTY_PAREN(context))
9525 break;
9526
9527 case NOT_EXPR:
9528 if (!PRETTY_PAREN(context))
9530 appendStringInfoString(buf, "NOT ");
9532 false, node);
9533 if (!PRETTY_PAREN(context))
9535 break;
9536
9537 default:
9538 elog(ERROR, "unrecognized boolop: %d",
9539 (int) expr->boolop);
9540 }
9541 }
9542 break;
9543
9544 case T_SubLink:
9545 get_sublink_expr((SubLink *) node, context);
9546 break;
9547
9548 case T_SubPlan:
9549 {
9550 SubPlan *subplan = (SubPlan *) node;
9551
9552 /*
9553 * We cannot see an already-planned subplan in rule deparsing,
9554 * only while EXPLAINing a query plan. We don't try to
9555 * reconstruct the original SQL, just reference the subplan
9556 * that appears elsewhere in EXPLAIN's result. It does seem
9557 * useful to show the subLinkType and testexpr (if any), and
9558 * we also note whether the subplan will be hashed.
9559 */
9560 switch (subplan->subLinkType)
9561 {
9562 case EXISTS_SUBLINK:
9563 appendStringInfoString(buf, "EXISTS(");
9564 Assert(subplan->testexpr == NULL);
9565 break;
9566 case ALL_SUBLINK:
9567 appendStringInfoString(buf, "(ALL ");
9568 Assert(subplan->testexpr != NULL);
9569 break;
9570 case ANY_SUBLINK:
9571 appendStringInfoString(buf, "(ANY ");
9572 Assert(subplan->testexpr != NULL);
9573 break;
9574 case ROWCOMPARE_SUBLINK:
9575 /* Parenthesizing the testexpr seems sufficient */
9577 Assert(subplan->testexpr != NULL);
9578 break;
9579 case EXPR_SUBLINK:
9580 /* No need to decorate these subplan references */
9582 Assert(subplan->testexpr == NULL);
9583 break;
9584 case MULTIEXPR_SUBLINK:
9585 /* MULTIEXPR isn't executed in the normal way */
9586 appendStringInfoString(buf, "(rescan ");
9587 Assert(subplan->testexpr == NULL);
9588 break;
9589 case ARRAY_SUBLINK:
9590 appendStringInfoString(buf, "ARRAY(");
9591 Assert(subplan->testexpr == NULL);
9592 break;
9593 case CTE_SUBLINK:
9594 /* This case is unreachable within expressions */
9595 appendStringInfoString(buf, "CTE(");
9596 Assert(subplan->testexpr == NULL);
9597 break;
9598 }
9599
9600 if (subplan->testexpr != NULL)
9601 {
9603
9604 /*
9605 * Push SubPlan into ancestors list while deparsing
9606 * testexpr, so that we can handle PARAM_EXEC references
9607 * to the SubPlan's paramIds. (This makes it look like
9608 * the SubPlan is an "ancestor" of the current plan node,
9609 * which is a little weird, but it does no harm.) In this
9610 * path, we don't need to mention the SubPlan explicitly,
9611 * because the referencing Params will show its existence.
9612 */
9613 dpns = (deparse_namespace *) linitial(context->namespaces);
9614 dpns->ancestors = lcons(subplan, dpns->ancestors);
9615
9616 get_rule_expr(subplan->testexpr, context, showimplicit);
9618
9619 dpns->ancestors = list_delete_first(dpns->ancestors);
9620 }
9621 else
9622 {
9623 const char *nameprefix;
9624
9625 /* No referencing Params, so show the SubPlan's name */
9626 if (subplan->isInitPlan)
9627 nameprefix = "InitPlan ";
9628 else
9629 nameprefix = "SubPlan ";
9630 if (subplan->useHashTable)
9631 appendStringInfo(buf, "hashed %s%s)",
9632 nameprefix, subplan->plan_name);
9633 else
9634 appendStringInfo(buf, "%s%s)",
9635 nameprefix, subplan->plan_name);
9636 }
9637 }
9638 break;
9639
9641 {
9643 ListCell *lc;
9644
9645 /*
9646 * This case cannot be reached in normal usage, since no
9647 * AlternativeSubPlan can appear either in parsetrees or
9648 * finished plan trees. We keep it just in case somebody
9649 * wants to use this code to print planner data structures.
9650 */
9651 appendStringInfoString(buf, "(alternatives: ");
9652 foreach(lc, asplan->subplans)
9653 {
9654 SubPlan *splan = lfirst_node(SubPlan, lc);
9655 const char *nameprefix;
9656
9657 if (splan->isInitPlan)
9658 nameprefix = "InitPlan ";
9659 else
9660 nameprefix = "SubPlan ";
9661 if (splan->useHashTable)
9662 appendStringInfo(buf, "hashed %s%s", nameprefix,
9663 splan->plan_name);
9664 else
9666 splan->plan_name);
9667 if (lnext(asplan->subplans, lc))
9668 appendStringInfoString(buf, " or ");
9669 }
9671 }
9672 break;
9673
9674 case T_FieldSelect:
9675 {
9676 FieldSelect *fselect = (FieldSelect *) node;
9677 Node *arg = (Node *) fselect->arg;
9678 int fno = fselect->fieldnum;
9679 const char *fieldname;
9680 bool need_parens;
9681
9682 /*
9683 * Parenthesize the argument unless it's a SubscriptingRef or
9684 * another FieldSelect. Note in particular that it would be
9685 * WRONG to not parenthesize a Var argument; simplicity is not
9686 * the issue here, having the right number of names is.
9687 */
9689 !IsA(arg, FieldSelect);
9690 if (need_parens)
9692 get_rule_expr(arg, context, true);
9693 if (need_parens)
9695
9696 /*
9697 * Get and print the field name.
9698 */
9699 fieldname = get_name_for_var_field((Var *) arg, fno,
9700 0, context);
9701 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
9702 }
9703 break;
9704
9705 case T_FieldStore:
9706 {
9707 FieldStore *fstore = (FieldStore *) node;
9708 bool need_parens;
9709
9710 /*
9711 * There is no good way to represent a FieldStore as real SQL,
9712 * so decompilation of INSERT or UPDATE statements should
9713 * always use processIndirection as part of the
9714 * statement-level syntax. We should only get here when
9715 * EXPLAIN tries to print the targetlist of a plan resulting
9716 * from such a statement. The plan case is even harder than
9717 * ordinary rules would be, because the planner tries to
9718 * collapse multiple assignments to the same field or subfield
9719 * into one FieldStore; so we can see a list of target fields
9720 * not just one, and the arguments could be FieldStores
9721 * themselves. We don't bother to try to print the target
9722 * field names; we just print the source arguments, with a
9723 * ROW() around them if there's more than one. This isn't
9724 * terribly complete, but it's probably good enough for
9725 * EXPLAIN's purposes; especially since anything more would be
9726 * either hopelessly confusing or an even poorer
9727 * representation of what the plan is actually doing.
9728 */
9729 need_parens = (list_length(fstore->newvals) != 1);
9730 if (need_parens)
9731 appendStringInfoString(buf, "ROW(");
9732 get_rule_expr((Node *) fstore->newvals, context, showimplicit);
9733 if (need_parens)
9735 }
9736 break;
9737
9738 case T_RelabelType:
9739 {
9740 RelabelType *relabel = (RelabelType *) node;
9741 Node *arg = (Node *) relabel->arg;
9742
9743 if (relabel->relabelformat == COERCE_IMPLICIT_CAST &&
9744 !showimplicit)
9745 {
9746 /* don't show the implicit cast */
9747 get_rule_expr_paren(arg, context, false, node);
9748 }
9749 else
9750 {
9751 get_coercion_expr(arg, context,
9752 relabel->resulttype,
9753 relabel->resulttypmod,
9754 node);
9755 }
9756 }
9757 break;
9758
9759 case T_CoerceViaIO:
9760 {
9761 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
9762 Node *arg = (Node *) iocoerce->arg;
9763
9764 if (iocoerce->coerceformat == COERCE_IMPLICIT_CAST &&
9765 !showimplicit)
9766 {
9767 /* don't show the implicit cast */
9768 get_rule_expr_paren(arg, context, false, node);
9769 }
9770 else
9771 {
9772 get_coercion_expr(arg, context,
9773 iocoerce->resulttype,
9774 -1,
9775 node);
9776 }
9777 }
9778 break;
9779
9780 case T_ArrayCoerceExpr:
9781 {
9783 Node *arg = (Node *) acoerce->arg;
9784
9785 if (acoerce->coerceformat == COERCE_IMPLICIT_CAST &&
9786 !showimplicit)
9787 {
9788 /* don't show the implicit cast */
9789 get_rule_expr_paren(arg, context, false, node);
9790 }
9791 else
9792 {
9793 get_coercion_expr(arg, context,
9794 acoerce->resulttype,
9795 acoerce->resulttypmod,
9796 node);
9797 }
9798 }
9799 break;
9800
9802 {
9804 Node *arg = (Node *) convert->arg;
9805
9806 if (convert->convertformat == COERCE_IMPLICIT_CAST &&
9807 !showimplicit)
9808 {
9809 /* don't show the implicit cast */
9810 get_rule_expr_paren(arg, context, false, node);
9811 }
9812 else
9813 {
9814 get_coercion_expr(arg, context,
9815 convert->resulttype, -1,
9816 node);
9817 }
9818 }
9819 break;
9820
9821 case T_CollateExpr:
9822 {
9823 CollateExpr *collate = (CollateExpr *) node;
9824 Node *arg = (Node *) collate->arg;
9825
9826 if (!PRETTY_PAREN(context))
9828 get_rule_expr_paren(arg, context, showimplicit, node);
9829 appendStringInfo(buf, " COLLATE %s",
9831 if (!PRETTY_PAREN(context))
9833 }
9834 break;
9835
9836 case T_CaseExpr:
9837 {
9838 CaseExpr *caseexpr = (CaseExpr *) node;
9839 ListCell *temp;
9840
9841 appendContextKeyword(context, "CASE",
9842 0, PRETTYINDENT_VAR, 0);
9843 if (caseexpr->arg)
9844 {
9846 get_rule_expr((Node *) caseexpr->arg, context, true);
9847 }
9848 foreach(temp, caseexpr->args)
9849 {
9851 Node *w = (Node *) when->expr;
9852
9853 if (caseexpr->arg)
9854 {
9855 /*
9856 * The parser should have produced WHEN clauses of the
9857 * form "CaseTestExpr = RHS", possibly with an
9858 * implicit coercion inserted above the CaseTestExpr.
9859 * For accurate decompilation of rules it's essential
9860 * that we show just the RHS. However in an
9861 * expression that's been through the optimizer, the
9862 * WHEN clause could be almost anything (since the
9863 * equality operator could have been expanded into an
9864 * inline function). If we don't recognize the form
9865 * of the WHEN clause, just punt and display it as-is.
9866 */
9867 if (IsA(w, OpExpr))
9868 {
9869 List *args = ((OpExpr *) w)->args;
9870
9871 if (list_length(args) == 2 &&
9873 CaseTestExpr))
9874 w = (Node *) lsecond(args);
9875 }
9876 }
9877
9878 if (!PRETTY_INDENT(context))
9880 appendContextKeyword(context, "WHEN ",
9881 0, 0, 0);
9882 get_rule_expr(w, context, false);
9883 appendStringInfoString(buf, " THEN ");
9884 get_rule_expr((Node *) when->result, context, true);
9885 }
9886 if (!PRETTY_INDENT(context))
9888 appendContextKeyword(context, "ELSE ",
9889 0, 0, 0);
9890 get_rule_expr((Node *) caseexpr->defresult, context, true);
9891 if (!PRETTY_INDENT(context))
9893 appendContextKeyword(context, "END",
9894 -PRETTYINDENT_VAR, 0, 0);
9895 }
9896 break;
9897
9898 case T_CaseTestExpr:
9899 {
9900 /*
9901 * Normally we should never get here, since for expressions
9902 * that can contain this node type we attempt to avoid
9903 * recursing to it. But in an optimized expression we might
9904 * be unable to avoid that (see comments for CaseExpr). If we
9905 * do see one, print it as CASE_TEST_EXPR.
9906 */
9907 appendStringInfoString(buf, "CASE_TEST_EXPR");
9908 }
9909 break;
9910
9911 case T_ArrayExpr:
9912 {
9913 ArrayExpr *arrayexpr = (ArrayExpr *) node;
9914
9915 appendStringInfoString(buf, "ARRAY[");
9916 get_rule_expr((Node *) arrayexpr->elements, context, true);
9918
9919 /*
9920 * If the array isn't empty, we assume its elements are
9921 * coerced to the desired type. If it's empty, though, we
9922 * need an explicit coercion to the array type.
9923 */
9924 if (arrayexpr->elements == NIL)
9925 appendStringInfo(buf, "::%s",
9926 format_type_with_typemod(arrayexpr->array_typeid, -1));
9927 }
9928 break;
9929
9930 case T_RowExpr:
9931 {
9932 RowExpr *rowexpr = (RowExpr *) node;
9933 TupleDesc tupdesc = NULL;
9934 ListCell *arg;
9935 int i;
9936 char *sep;
9937
9938 /*
9939 * If it's a named type and not RECORD, we may have to skip
9940 * dropped columns and/or claim there are NULLs for added
9941 * columns.
9942 */
9943 if (rowexpr->row_typeid != RECORDOID)
9944 {
9945 tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
9946 Assert(list_length(rowexpr->args) <= tupdesc->natts);
9947 }
9948
9949 /*
9950 * SQL99 allows "ROW" to be omitted when there is more than
9951 * one column, but for simplicity we always print it.
9952 */
9953 appendStringInfoString(buf, "ROW(");
9954 sep = "";
9955 i = 0;
9956 foreach(arg, rowexpr->args)
9957 {
9958 Node *e = (Node *) lfirst(arg);
9959
9960 if (tupdesc == NULL ||
9962 {
9964 /* Whole-row Vars need special treatment here */
9965 get_rule_expr_toplevel(e, context, true);
9966 sep = ", ";
9967 }
9968 i++;
9969 }
9970 if (tupdesc != NULL)
9971 {
9972 while (i < tupdesc->natts)
9973 {
9974 if (!TupleDescCompactAttr(tupdesc, i)->attisdropped)
9975 {
9977 appendStringInfoString(buf, "NULL");
9978 sep = ", ";
9979 }
9980 i++;
9981 }
9982
9983 ReleaseTupleDesc(tupdesc);
9984 }
9986 if (rowexpr->row_format == COERCE_EXPLICIT_CAST)
9987 appendStringInfo(buf, "::%s",
9988 format_type_with_typemod(rowexpr->row_typeid, -1));
9989 }
9990 break;
9991
9992 case T_RowCompareExpr:
9993 {
9995
9996 /*
9997 * SQL99 allows "ROW" to be omitted when there is more than
9998 * one column, but for simplicity we always print it. Within
9999 * a ROW expression, whole-row Vars need special treatment, so
10000 * use get_rule_list_toplevel.
10001 */
10002 appendStringInfoString(buf, "(ROW(");
10003 get_rule_list_toplevel(rcexpr->largs, context, true);
10004
10005 /*
10006 * We assume that the name of the first-column operator will
10007 * do for all the rest too. This is definitely open to
10008 * failure, eg if some but not all operators were renamed
10009 * since the construct was parsed, but there seems no way to
10010 * be perfect.
10011 */
10012 appendStringInfo(buf, ") %s ROW(",
10014 exprType(linitial(rcexpr->largs)),
10015 exprType(linitial(rcexpr->rargs))));
10016 get_rule_list_toplevel(rcexpr->rargs, context, true);
10018 }
10019 break;
10020
10021 case T_CoalesceExpr:
10022 {
10024
10025 appendStringInfoString(buf, "COALESCE(");
10026 get_rule_expr((Node *) coalesceexpr->args, context, true);
10028 }
10029 break;
10030
10031 case T_MinMaxExpr:
10032 {
10033 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
10034
10035 switch (minmaxexpr->op)
10036 {
10037 case IS_GREATEST:
10038 appendStringInfoString(buf, "GREATEST(");
10039 break;
10040 case IS_LEAST:
10041 appendStringInfoString(buf, "LEAST(");
10042 break;
10043 }
10044 get_rule_expr((Node *) minmaxexpr->args, context, true);
10046 }
10047 break;
10048
10049 case T_SQLValueFunction:
10050 {
10051 SQLValueFunction *svf = (SQLValueFunction *) node;
10052
10053 /*
10054 * Note: this code knows that typmod for time, timestamp, and
10055 * timestamptz just prints as integer.
10056 */
10057 switch (svf->op)
10058 {
10059 case SVFOP_CURRENT_DATE:
10060 appendStringInfoString(buf, "CURRENT_DATE");
10061 break;
10062 case SVFOP_CURRENT_TIME:
10063 appendStringInfoString(buf, "CURRENT_TIME");
10064 break;
10066 appendStringInfo(buf, "CURRENT_TIME(%d)", svf->typmod);
10067 break;
10069 appendStringInfoString(buf, "CURRENT_TIMESTAMP");
10070 break;
10072 appendStringInfo(buf, "CURRENT_TIMESTAMP(%d)",
10073 svf->typmod);
10074 break;
10075 case SVFOP_LOCALTIME:
10076 appendStringInfoString(buf, "LOCALTIME");
10077 break;
10078 case SVFOP_LOCALTIME_N:
10079 appendStringInfo(buf, "LOCALTIME(%d)", svf->typmod);
10080 break;
10082 appendStringInfoString(buf, "LOCALTIMESTAMP");
10083 break;
10085 appendStringInfo(buf, "LOCALTIMESTAMP(%d)",
10086 svf->typmod);
10087 break;
10088 case SVFOP_CURRENT_ROLE:
10089 appendStringInfoString(buf, "CURRENT_ROLE");
10090 break;
10091 case SVFOP_CURRENT_USER:
10092 appendStringInfoString(buf, "CURRENT_USER");
10093 break;
10094 case SVFOP_USER:
10095 appendStringInfoString(buf, "USER");
10096 break;
10097 case SVFOP_SESSION_USER:
10098 appendStringInfoString(buf, "SESSION_USER");
10099 break;
10101 appendStringInfoString(buf, "CURRENT_CATALOG");
10102 break;
10104 appendStringInfoString(buf, "CURRENT_SCHEMA");
10105 break;
10106 }
10107 }
10108 break;
10109
10110 case T_XmlExpr:
10111 {
10112 XmlExpr *xexpr = (XmlExpr *) node;
10113 bool needcomma = false;
10114 ListCell *arg;
10115 ListCell *narg;
10116 Const *con;
10117
10118 switch (xexpr->op)
10119 {
10120 case IS_XMLCONCAT:
10121 appendStringInfoString(buf, "XMLCONCAT(");
10122 break;
10123 case IS_XMLELEMENT:
10124 appendStringInfoString(buf, "XMLELEMENT(");
10125 break;
10126 case IS_XMLFOREST:
10127 appendStringInfoString(buf, "XMLFOREST(");
10128 break;
10129 case IS_XMLPARSE:
10130 appendStringInfoString(buf, "XMLPARSE(");
10131 break;
10132 case IS_XMLPI:
10133 appendStringInfoString(buf, "XMLPI(");
10134 break;
10135 case IS_XMLROOT:
10136 appendStringInfoString(buf, "XMLROOT(");
10137 break;
10138 case IS_XMLSERIALIZE:
10139 appendStringInfoString(buf, "XMLSERIALIZE(");
10140 break;
10141 case IS_DOCUMENT:
10142 break;
10143 }
10144 if (xexpr->op == IS_XMLPARSE || xexpr->op == IS_XMLSERIALIZE)
10145 {
10146 if (xexpr->xmloption == XMLOPTION_DOCUMENT)
10147 appendStringInfoString(buf, "DOCUMENT ");
10148 else
10149 appendStringInfoString(buf, "CONTENT ");
10150 }
10151 if (xexpr->name)
10152 {
10153 appendStringInfo(buf, "NAME %s",
10155 needcomma = true;
10156 }
10157 if (xexpr->named_args)
10158 {
10159 if (xexpr->op != IS_XMLFOREST)
10160 {
10161 if (needcomma)
10163 appendStringInfoString(buf, "XMLATTRIBUTES(");
10164 needcomma = false;
10165 }
10166 forboth(arg, xexpr->named_args, narg, xexpr->arg_names)
10167 {
10168 Node *e = (Node *) lfirst(arg);
10169 char *argname = strVal(lfirst(narg));
10170
10171 if (needcomma)
10173 get_rule_expr(e, context, true);
10174 appendStringInfo(buf, " AS %s",
10176 needcomma = true;
10177 }
10178 if (xexpr->op != IS_XMLFOREST)
10180 }
10181 if (xexpr->args)
10182 {
10183 if (needcomma)
10185 switch (xexpr->op)
10186 {
10187 case IS_XMLCONCAT:
10188 case IS_XMLELEMENT:
10189 case IS_XMLFOREST:
10190 case IS_XMLPI:
10191 case IS_XMLSERIALIZE:
10192 /* no extra decoration needed */
10193 get_rule_expr((Node *) xexpr->args, context, true);
10194 break;
10195 case IS_XMLPARSE:
10196 Assert(list_length(xexpr->args) == 2);
10197
10198 get_rule_expr((Node *) linitial(xexpr->args),
10199 context, true);
10200
10201 con = lsecond_node(Const, xexpr->args);
10202 Assert(!con->constisnull);
10203 if (DatumGetBool(con->constvalue))
10205 " PRESERVE WHITESPACE");
10206 else
10208 " STRIP WHITESPACE");
10209 break;
10210 case IS_XMLROOT:
10211 Assert(list_length(xexpr->args) == 3);
10212
10213 get_rule_expr((Node *) linitial(xexpr->args),
10214 context, true);
10215
10216 appendStringInfoString(buf, ", VERSION ");
10217 con = (Const *) lsecond(xexpr->args);
10218 if (IsA(con, Const) &&
10219 con->constisnull)
10220 appendStringInfoString(buf, "NO VALUE");
10221 else
10222 get_rule_expr((Node *) con, context, false);
10223
10224 con = lthird_node(Const, xexpr->args);
10225 if (con->constisnull)
10226 /* suppress STANDALONE NO VALUE */ ;
10227 else
10228 {
10229 switch (DatumGetInt32(con->constvalue))
10230 {
10231 case XML_STANDALONE_YES:
10233 ", STANDALONE YES");
10234 break;
10235 case XML_STANDALONE_NO:
10237 ", STANDALONE NO");
10238 break;
10241 ", STANDALONE NO VALUE");
10242 break;
10243 default:
10244 break;
10245 }
10246 }
10247 break;
10248 case IS_DOCUMENT:
10249 get_rule_expr_paren((Node *) xexpr->args, context, false, node);
10250 break;
10251 }
10252 }
10253 if (xexpr->op == IS_XMLSERIALIZE)
10254 {
10255 appendStringInfo(buf, " AS %s",
10256 format_type_with_typemod(xexpr->type,
10257 xexpr->typmod));
10258 if (xexpr->indent)
10259 appendStringInfoString(buf, " INDENT");
10260 else
10261 appendStringInfoString(buf, " NO INDENT");
10262 }
10263
10264 if (xexpr->op == IS_DOCUMENT)
10265 appendStringInfoString(buf, " IS DOCUMENT");
10266 else
10268 }
10269 break;
10270
10271 case T_NullTest:
10272 {
10273 NullTest *ntest = (NullTest *) node;
10274
10275 if (!PRETTY_PAREN(context))
10277 get_rule_expr_paren((Node *) ntest->arg, context, true, node);
10278
10279 /*
10280 * For scalar inputs, we prefer to print as IS [NOT] NULL,
10281 * which is shorter and traditional. If it's a rowtype input
10282 * but we're applying a scalar test, must print IS [NOT]
10283 * DISTINCT FROM NULL to be semantically correct.
10284 */
10285 if (ntest->argisrow ||
10286 !type_is_rowtype(exprType((Node *) ntest->arg)))
10287 {
10288 switch (ntest->nulltesttype)
10289 {
10290 case IS_NULL:
10291 appendStringInfoString(buf, " IS NULL");
10292 break;
10293 case IS_NOT_NULL:
10294 appendStringInfoString(buf, " IS NOT NULL");
10295 break;
10296 default:
10297 elog(ERROR, "unrecognized nulltesttype: %d",
10298 (int) ntest->nulltesttype);
10299 }
10300 }
10301 else
10302 {
10303 switch (ntest->nulltesttype)
10304 {
10305 case IS_NULL:
10306 appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL");
10307 break;
10308 case IS_NOT_NULL:
10309 appendStringInfoString(buf, " IS DISTINCT FROM NULL");
10310 break;
10311 default:
10312 elog(ERROR, "unrecognized nulltesttype: %d",
10313 (int) ntest->nulltesttype);
10314 }
10315 }
10316 if (!PRETTY_PAREN(context))
10318 }
10319 break;
10320
10321 case T_BooleanTest:
10322 {
10323 BooleanTest *btest = (BooleanTest *) node;
10324
10325 if (!PRETTY_PAREN(context))
10327 get_rule_expr_paren((Node *) btest->arg, context, false, node);
10328 switch (btest->booltesttype)
10329 {
10330 case IS_TRUE:
10331 appendStringInfoString(buf, " IS TRUE");
10332 break;
10333 case IS_NOT_TRUE:
10334 appendStringInfoString(buf, " IS NOT TRUE");
10335 break;
10336 case IS_FALSE:
10337 appendStringInfoString(buf, " IS FALSE");
10338 break;
10339 case IS_NOT_FALSE:
10340 appendStringInfoString(buf, " IS NOT FALSE");
10341 break;
10342 case IS_UNKNOWN:
10343 appendStringInfoString(buf, " IS UNKNOWN");
10344 break;
10345 case IS_NOT_UNKNOWN:
10346 appendStringInfoString(buf, " IS NOT UNKNOWN");
10347 break;
10348 default:
10349 elog(ERROR, "unrecognized booltesttype: %d",
10350 (int) btest->booltesttype);
10351 }
10352 if (!PRETTY_PAREN(context))
10354 }
10355 break;
10356
10357 case T_CoerceToDomain:
10358 {
10360 Node *arg = (Node *) ctest->arg;
10361
10362 if (ctest->coercionformat == COERCE_IMPLICIT_CAST &&
10363 !showimplicit)
10364 {
10365 /* don't show the implicit cast */
10366 get_rule_expr(arg, context, false);
10367 }
10368 else
10369 {
10370 get_coercion_expr(arg, context,
10371 ctest->resulttype,
10372 ctest->resulttypmod,
10373 node);
10374 }
10375 }
10376 break;
10377
10379 appendStringInfoString(buf, "VALUE");
10380 break;
10381
10382 case T_SetToDefault:
10383 appendStringInfoString(buf, "DEFAULT");
10384 break;
10385
10386 case T_CurrentOfExpr:
10387 {
10388 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
10389
10390 if (cexpr->cursor_name)
10391 appendStringInfo(buf, "CURRENT OF %s",
10393 else
10394 appendStringInfo(buf, "CURRENT OF $%d",
10395 cexpr->cursor_param);
10396 }
10397 break;
10398
10399 case T_NextValueExpr:
10400 {
10402
10403 /*
10404 * This isn't exactly nextval(), but that seems close enough
10405 * for EXPLAIN's purposes.
10406 */
10407 appendStringInfoString(buf, "nextval(");
10410 NIL));
10412 }
10413 break;
10414
10415 case T_InferenceElem:
10416 {
10417 InferenceElem *iexpr = (InferenceElem *) node;
10418 bool save_varprefix;
10419 bool need_parens;
10420
10421 /*
10422 * InferenceElem can only refer to target relation, so a
10423 * prefix is not useful, and indeed would cause parse errors.
10424 */
10425 save_varprefix = context->varprefix;
10426 context->varprefix = false;
10427
10428 /*
10429 * Parenthesize the element unless it's a simple Var or a bare
10430 * function call. Follows pg_get_indexdef_worker().
10431 */
10432 need_parens = !IsA(iexpr->expr, Var);
10433 if (IsA(iexpr->expr, FuncExpr) &&
10434 ((FuncExpr *) iexpr->expr)->funcformat ==
10436 need_parens = false;
10437
10438 if (need_parens)
10440 get_rule_expr((Node *) iexpr->expr,
10441 context, false);
10442 if (need_parens)
10444
10445 context->varprefix = save_varprefix;
10446
10447 if (iexpr->infercollid)
10448 appendStringInfo(buf, " COLLATE %s",
10449 generate_collation_name(iexpr->infercollid));
10450
10451 /* Add the operator class name, if not default */
10452 if (iexpr->inferopclass)
10453 {
10454 Oid inferopclass = iexpr->inferopclass;
10456
10457 get_opclass_name(inferopclass, inferopcinputtype, buf);
10458 }
10459 }
10460 break;
10461
10462 case T_ReturningExpr:
10463 {
10465
10466 /*
10467 * We cannot see a ReturningExpr in rule deparsing, only while
10468 * EXPLAINing a query plan (ReturningExpr nodes are only ever
10469 * adding during query rewriting). Just display the expression
10470 * returned (an expanded view column).
10471 */
10472 get_rule_expr((Node *) retExpr->retexpr, context, showimplicit);
10473 }
10474 break;
10475
10477 {
10479 ListCell *cell;
10480 char *sep;
10481
10482 if (spec->is_default)
10483 {
10484 appendStringInfoString(buf, "DEFAULT");
10485 break;
10486 }
10487
10488 switch (spec->strategy)
10489 {
10491 Assert(spec->modulus > 0 && spec->remainder >= 0);
10492 Assert(spec->modulus > spec->remainder);
10493
10494 appendStringInfoString(buf, "FOR VALUES");
10495 appendStringInfo(buf, " WITH (modulus %d, remainder %d)",
10496 spec->modulus, spec->remainder);
10497 break;
10498
10500 Assert(spec->listdatums != NIL);
10501
10502 appendStringInfoString(buf, "FOR VALUES IN (");
10503 sep = "";
10504 foreach(cell, spec->listdatums)
10505 {
10506 Const *val = lfirst_node(Const, cell);
10507
10509 get_const_expr(val, context, -1);
10510 sep = ", ";
10511 }
10512
10514 break;
10515
10517 Assert(spec->lowerdatums != NIL &&
10518 spec->upperdatums != NIL &&
10519 list_length(spec->lowerdatums) ==
10520 list_length(spec->upperdatums));
10521
10522 appendStringInfo(buf, "FOR VALUES FROM %s TO %s",
10523 get_range_partbound_string(spec->lowerdatums),
10524 get_range_partbound_string(spec->upperdatums));
10525 break;
10526
10527 default:
10528 elog(ERROR, "unrecognized partition strategy: %d",
10529 (int) spec->strategy);
10530 break;
10531 }
10532 }
10533 break;
10534
10535 case T_JsonValueExpr:
10536 {
10537 JsonValueExpr *jve = (JsonValueExpr *) node;
10538
10539 get_rule_expr((Node *) jve->raw_expr, context, false);
10540 get_json_format(jve->format, context->buf);
10541 }
10542 break;
10543
10545 get_json_constructor((JsonConstructorExpr *) node, context, false);
10546 break;
10547
10548 case T_JsonIsPredicate:
10549 {
10550 JsonIsPredicate *pred = (JsonIsPredicate *) node;
10551
10552 if (!PRETTY_PAREN(context))
10553 appendStringInfoChar(context->buf, '(');
10554
10555 get_rule_expr_paren(pred->expr, context, true, node);
10556
10557 appendStringInfoString(context->buf, " IS JSON");
10558
10559 /* TODO: handle FORMAT clause */
10560
10561 switch (pred->item_type)
10562 {
10563 case JS_TYPE_SCALAR:
10564 appendStringInfoString(context->buf, " SCALAR");
10565 break;
10566 case JS_TYPE_ARRAY:
10567 appendStringInfoString(context->buf, " ARRAY");
10568 break;
10569 case JS_TYPE_OBJECT:
10570 appendStringInfoString(context->buf, " OBJECT");
10571 break;
10572 default:
10573 break;
10574 }
10575
10576 if (pred->unique_keys)
10577 appendStringInfoString(context->buf, " WITH UNIQUE KEYS");
10578
10579 if (!PRETTY_PAREN(context))
10580 appendStringInfoChar(context->buf, ')');
10581 }
10582 break;
10583
10584 case T_JsonExpr:
10585 {
10586 JsonExpr *jexpr = (JsonExpr *) node;
10587
10588 switch (jexpr->op)
10589 {
10590 case JSON_EXISTS_OP:
10591 appendStringInfoString(buf, "JSON_EXISTS(");
10592 break;
10593 case JSON_QUERY_OP:
10594 appendStringInfoString(buf, "JSON_QUERY(");
10595 break;
10596 case JSON_VALUE_OP:
10597 appendStringInfoString(buf, "JSON_VALUE(");
10598 break;
10599 default:
10600 elog(ERROR, "unrecognized JsonExpr op: %d",
10601 (int) jexpr->op);
10602 }
10603
10604 get_rule_expr(jexpr->formatted_expr, context, showimplicit);
10605
10607
10608 get_json_path_spec(jexpr->path_spec, context, showimplicit);
10609
10610 if (jexpr->passing_values)
10611 {
10612 ListCell *lc1,
10613 *lc2;
10614 bool needcomma = false;
10615
10616 appendStringInfoString(buf, " PASSING ");
10617
10618 forboth(lc1, jexpr->passing_names,
10619 lc2, jexpr->passing_values)
10620 {
10621 if (needcomma)
10623 needcomma = true;
10624
10625 get_rule_expr((Node *) lfirst(lc2), context, showimplicit);
10626 appendStringInfo(buf, " AS %s",
10628 }
10629 }
10630
10631 if (jexpr->op != JSON_EXISTS_OP ||
10632 jexpr->returning->typid != BOOLOID)
10633 get_json_returning(jexpr->returning, context->buf,
10634 jexpr->op == JSON_QUERY_OP);
10635
10637 jexpr->op != JSON_EXISTS_OP ?
10640
10642 }
10643 break;
10644
10645 case T_List:
10646 {
10647 char *sep;
10648 ListCell *l;
10649
10650 sep = "";
10651 foreach(l, (List *) node)
10652 {
10654 get_rule_expr((Node *) lfirst(l), context, showimplicit);
10655 sep = ", ";
10656 }
10657 }
10658 break;
10659
10660 case T_TableFunc:
10661 get_tablefunc((TableFunc *) node, context, showimplicit);
10662 break;
10663
10664 default:
10665 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
10666 break;
10667 }
10668}
10669
10670/*
10671 * get_rule_expr_toplevel - Parse back a toplevel expression
10672 *
10673 * Same as get_rule_expr(), except that if the expr is just a Var, we pass
10674 * istoplevel = true not false to get_variable(). This causes whole-row Vars
10675 * to get printed with decoration that will prevent expansion of "*".
10676 * We need to use this in contexts such as ROW() and VALUES(), where the
10677 * parser would expand "foo.*" appearing at top level. (In principle we'd
10678 * use this in get_target_list() too, but that has additional worries about
10679 * whether to print AS, so it needs to invoke get_variable() directly anyway.)
10680 */
10681static void
10683 bool showimplicit)
10684{
10685 if (node && IsA(node, Var))
10686 (void) get_variable((Var *) node, 0, true, context);
10687 else
10688 get_rule_expr(node, context, showimplicit);
10689}
10690
10691/*
10692 * get_rule_list_toplevel - Parse back a list of toplevel expressions
10693 *
10694 * Apply get_rule_expr_toplevel() to each element of a List.
10695 *
10696 * This adds commas between the expressions, but caller is responsible
10697 * for printing surrounding decoration.
10698 */
10699static void
10701 bool showimplicit)
10702{
10703 const char *sep;
10704 ListCell *lc;
10705
10706 sep = "";
10707 foreach(lc, lst)
10708 {
10709 Node *e = (Node *) lfirst(lc);
10710
10711 appendStringInfoString(context->buf, sep);
10713 sep = ", ";
10714 }
10715}
10716
10717/*
10718 * get_rule_expr_funccall - Parse back a function-call expression
10719 *
10720 * Same as get_rule_expr(), except that we guarantee that the output will
10721 * look like a function call, or like one of the things the grammar treats as
10722 * equivalent to a function call (see the func_expr_windowless production).
10723 * This is needed in places where the grammar uses func_expr_windowless and
10724 * you can't substitute a parenthesized a_expr. If what we have isn't going
10725 * to look like a function call, wrap it in a dummy CAST() expression, which
10726 * will satisfy the grammar --- and, indeed, is likely what the user wrote to
10727 * produce such a thing.
10728 */
10729static void
10731 bool showimplicit)
10732{
10733 if (looks_like_function(node))
10734 get_rule_expr(node, context, showimplicit);
10735 else
10736 {
10737 StringInfo buf = context->buf;
10738
10739 appendStringInfoString(buf, "CAST(");
10740 /* no point in showing any top-level implicit cast */
10741 get_rule_expr(node, context, false);
10742 appendStringInfo(buf, " AS %s)",
10744 exprTypmod(node)));
10745 }
10746}
10747
10748/*
10749 * Helper function to identify node types that satisfy func_expr_windowless.
10750 * If in doubt, "false" is always a safe answer.
10751 */
10752static bool
10754{
10755 if (node == NULL)
10756 return false; /* probably shouldn't happen */
10757 switch (nodeTag(node))
10758 {
10759 case T_FuncExpr:
10760 /* OK, unless it's going to deparse as a cast */
10761 return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL ||
10762 ((FuncExpr *) node)->funcformat == COERCE_SQL_SYNTAX);
10763 case T_NullIfExpr:
10764 case T_CoalesceExpr:
10765 case T_MinMaxExpr:
10766 case T_SQLValueFunction:
10767 case T_XmlExpr:
10768 case T_JsonExpr:
10769 /* these are all accepted by func_expr_common_subexpr */
10770 return true;
10771 default:
10772 break;
10773 }
10774 return false;
10775}
10776
10777
10778/*
10779 * get_oper_expr - Parse back an OpExpr node
10780 */
10781static void
10783{
10784 StringInfo buf = context->buf;
10785 Oid opno = expr->opno;
10786 List *args = expr->args;
10787
10788 if (!PRETTY_PAREN(context))
10790 if (list_length(args) == 2)
10791 {
10792 /* binary operator */
10793 Node *arg1 = (Node *) linitial(args);
10794 Node *arg2 = (Node *) lsecond(args);
10795
10796 get_rule_expr_paren(arg1, context, true, (Node *) expr);
10797 appendStringInfo(buf, " %s ",
10799 exprType(arg1),
10800 exprType(arg2)));
10801 get_rule_expr_paren(arg2, context, true, (Node *) expr);
10802 }
10803 else
10804 {
10805 /* prefix operator */
10806 Node *arg = (Node *) linitial(args);
10807
10808 appendStringInfo(buf, "%s ",
10810 InvalidOid,
10811 exprType(arg)));
10812 get_rule_expr_paren(arg, context, true, (Node *) expr);
10813 }
10814 if (!PRETTY_PAREN(context))
10816}
10817
10818/*
10819 * get_func_expr - Parse back a FuncExpr node
10820 */
10821static void
10823 bool showimplicit)
10824{
10825 StringInfo buf = context->buf;
10826 Oid funcoid = expr->funcid;
10827 Oid argtypes[FUNC_MAX_ARGS];
10828 int nargs;
10829 List *argnames;
10830 bool use_variadic;
10831 ListCell *l;
10832
10833 /*
10834 * If the function call came from an implicit coercion, then just show the
10835 * first argument --- unless caller wants to see implicit coercions.
10836 */
10837 if (expr->funcformat == COERCE_IMPLICIT_CAST && !showimplicit)
10838 {
10839 get_rule_expr_paren((Node *) linitial(expr->args), context,
10840 false, (Node *) expr);
10841 return;
10842 }
10843
10844 /*
10845 * If the function call came from a cast, then show the first argument
10846 * plus an explicit cast operation.
10847 */
10848 if (expr->funcformat == COERCE_EXPLICIT_CAST ||
10849 expr->funcformat == COERCE_IMPLICIT_CAST)
10850 {
10851 Node *arg = linitial(expr->args);
10852 Oid rettype = expr->funcresulttype;
10854
10855 /* Get the typmod if this is a length-coercion function */
10857
10858 get_coercion_expr(arg, context,
10859 rettype, coercedTypmod,
10860 (Node *) expr);
10861
10862 return;
10863 }
10864
10865 /*
10866 * If the function was called using one of the SQL spec's random special
10867 * syntaxes, try to reproduce that. If we don't recognize the function,
10868 * fall through.
10869 */
10870 if (expr->funcformat == COERCE_SQL_SYNTAX)
10871 {
10872 if (get_func_sql_syntax(expr, context))
10873 return;
10874 }
10875
10876 /*
10877 * Normal function: display as proname(args). First we need to extract
10878 * the argument datatypes.
10879 */
10880 if (list_length(expr->args) > FUNC_MAX_ARGS)
10881 ereport(ERROR,
10883 errmsg("too many arguments")));
10884 nargs = 0;
10885 argnames = NIL;
10886 foreach(l, expr->args)
10887 {
10888 Node *arg = (Node *) lfirst(l);
10889
10890 if (IsA(arg, NamedArgExpr))
10891 argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
10892 argtypes[nargs] = exprType(arg);
10893 nargs++;
10894 }
10895
10896 appendStringInfo(buf, "%s(",
10898 argnames, argtypes,
10899 expr->funcvariadic,
10900 &use_variadic,
10901 context->inGroupBy));
10902 nargs = 0;
10903 foreach(l, expr->args)
10904 {
10905 if (nargs++ > 0)
10907 if (use_variadic && lnext(expr->args, l) == NULL)
10908 appendStringInfoString(buf, "VARIADIC ");
10909 get_rule_expr((Node *) lfirst(l), context, true);
10910 }
10912}
10913
10914/*
10915 * get_agg_expr - Parse back an Aggref node
10916 */
10917static void
10920{
10921 get_agg_expr_helper(aggref, context, original_aggref, NULL, NULL,
10922 false);
10923}
10924
10925/*
10926 * get_agg_expr_helper - subroutine for get_agg_expr and
10927 * get_json_agg_constructor
10928 */
10929static void
10931 Aggref *original_aggref, const char *funcname,
10932 const char *options, bool is_json_objectagg)
10933{
10934 StringInfo buf = context->buf;
10935 Oid argtypes[FUNC_MAX_ARGS];
10936 int nargs;
10937 bool use_variadic = false;
10938
10939 /*
10940 * For a combining aggregate, we look up and deparse the corresponding
10941 * partial aggregate instead. This is necessary because our input
10942 * argument list has been replaced; the new argument list always has just
10943 * one element, which will point to a partial Aggref that supplies us with
10944 * transition states to combine.
10945 */
10946 if (DO_AGGSPLIT_COMBINE(aggref->aggsplit))
10947 {
10949
10950 Assert(list_length(aggref->args) == 1);
10951 tle = linitial_node(TargetEntry, aggref->args);
10952 resolve_special_varno((Node *) tle->expr, context,
10954 return;
10955 }
10956
10957 /*
10958 * Mark as PARTIAL, if appropriate. We look to the original aggref so as
10959 * to avoid printing this when recursing from the code just above.
10960 */
10962 appendStringInfoString(buf, "PARTIAL ");
10963
10964 /* Extract the argument types as seen by the parser */
10965 nargs = get_aggregate_argtypes(aggref, argtypes);
10966
10967 if (!funcname)
10968 funcname = generate_function_name(aggref->aggfnoid, nargs, NIL,
10969 argtypes, aggref->aggvariadic,
10970 &use_variadic,
10971 context->inGroupBy);
10972
10973 /* Print the aggregate name, schema-qualified if needed */
10974 appendStringInfo(buf, "%s(%s", funcname,
10975 (aggref->aggdistinct != NIL) ? "DISTINCT " : "");
10976
10977 if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
10978 {
10979 /*
10980 * Ordered-set aggregates do not use "*" syntax. Also, we needn't
10981 * worry about inserting VARIADIC. So we can just dump the direct
10982 * args as-is.
10983 */
10984 Assert(!aggref->aggvariadic);
10985 get_rule_expr((Node *) aggref->aggdirectargs, context, true);
10986 Assert(aggref->aggorder != NIL);
10987 appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
10988 get_rule_orderby(aggref->aggorder, aggref->args, false, context);
10989 }
10990 else
10991 {
10992 /* aggstar can be set only in zero-argument aggregates */
10993 if (aggref->aggstar)
10995 else
10996 {
10997 ListCell *l;
10998 int i;
10999
11000 i = 0;
11001 foreach(l, aggref->args)
11002 {
11004 Node *arg = (Node *) tle->expr;
11005
11007 if (tle->resjunk)
11008 continue;
11009 if (i++ > 0)
11010 {
11012 {
11013 /*
11014 * the ABSENT ON NULL and WITH UNIQUE args are printed
11015 * separately, so ignore them here
11016 */
11017 if (i > 2)
11018 break;
11019
11021 }
11022 else
11024 }
11025 if (use_variadic && i == nargs)
11026 appendStringInfoString(buf, "VARIADIC ");
11027 get_rule_expr(arg, context, true);
11028 }
11029 }
11030
11031 if (aggref->aggorder != NIL)
11032 {
11033 appendStringInfoString(buf, " ORDER BY ");
11034 get_rule_orderby(aggref->aggorder, aggref->args, false, context);
11035 }
11036 }
11037
11038 if (options)
11040
11041 if (aggref->aggfilter != NULL)
11042 {
11043 appendStringInfoString(buf, ") FILTER (WHERE ");
11044 get_rule_expr((Node *) aggref->aggfilter, context, false);
11045 }
11046
11048}
11049
11050/*
11051 * This is a helper function for get_agg_expr(). It's used when we deparse
11052 * a combining Aggref; resolve_special_varno locates the corresponding partial
11053 * Aggref and then calls this.
11054 */
11055static void
11056get_agg_combine_expr(Node *node, deparse_context *context, void *callback_arg)
11057{
11058 Aggref *aggref;
11059 Aggref *original_aggref = callback_arg;
11060
11061 if (!IsA(node, Aggref))
11062 elog(ERROR, "combining Aggref does not point to an Aggref");
11063
11064 aggref = (Aggref *) node;
11065 get_agg_expr(aggref, context, original_aggref);
11066}
11067
11068/*
11069 * get_windowfunc_expr - Parse back a WindowFunc node
11070 */
11071static void
11073{
11074 get_windowfunc_expr_helper(wfunc, context, NULL, NULL, false);
11075}
11076
11077
11078/*
11079 * get_windowfunc_expr_helper - subroutine for get_windowfunc_expr and
11080 * get_json_agg_constructor
11081 */
11082static void
11084 const char *funcname, const char *options,
11085 bool is_json_objectagg)
11086{
11087 StringInfo buf = context->buf;
11088 Oid argtypes[FUNC_MAX_ARGS];
11089 int nargs;
11090 List *argnames;
11091 ListCell *l;
11092
11093 if (list_length(wfunc->args) > FUNC_MAX_ARGS)
11094 ereport(ERROR,
11096 errmsg("too many arguments")));
11097 nargs = 0;
11098 argnames = NIL;
11099 foreach(l, wfunc->args)
11100 {
11101 Node *arg = (Node *) lfirst(l);
11102
11103 if (IsA(arg, NamedArgExpr))
11104 argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
11105 argtypes[nargs] = exprType(arg);
11106 nargs++;
11107 }
11108
11109 if (!funcname)
11110 funcname = generate_function_name(wfunc->winfnoid, nargs, argnames,
11111 argtypes, false, NULL,
11112 context->inGroupBy);
11113
11114 appendStringInfo(buf, "%s(", funcname);
11115
11116 /* winstar can be set only in zero-argument aggregates */
11117 if (wfunc->winstar)
11119 else
11120 {
11122 {
11123 get_rule_expr((Node *) linitial(wfunc->args), context, false);
11125 get_rule_expr((Node *) lsecond(wfunc->args), context, false);
11126 }
11127 else
11128 get_rule_expr((Node *) wfunc->args, context, true);
11129 }
11130
11131 if (options)
11133
11134 if (wfunc->aggfilter != NULL)
11135 {
11136 appendStringInfoString(buf, ") FILTER (WHERE ");
11137 get_rule_expr((Node *) wfunc->aggfilter, context, false);
11138 }
11139
11141
11142 if (wfunc->ignore_nulls == PARSER_IGNORE_NULLS)
11143 appendStringInfoString(buf, "IGNORE NULLS ");
11144
11145 appendStringInfoString(buf, "OVER ");
11146
11147 if (context->windowClause)
11148 {
11149 /* Query-decompilation case: search the windowClause list */
11150 foreach(l, context->windowClause)
11151 {
11152 WindowClause *wc = (WindowClause *) lfirst(l);
11153
11154 if (wc->winref == wfunc->winref)
11155 {
11156 if (wc->name)
11158 else
11159 get_rule_windowspec(wc, context->targetList, context);
11160 break;
11161 }
11162 }
11163 if (l == NULL)
11164 elog(ERROR, "could not find window clause for winref %u",
11165 wfunc->winref);
11166 }
11167 else
11168 {
11169 /*
11170 * In EXPLAIN, search the namespace stack for a matching WindowAgg
11171 * node (probably it's always the first entry), and print winname.
11172 */
11173 foreach(l, context->namespaces)
11174 {
11176
11177 if (dpns->plan && IsA(dpns->plan, WindowAgg))
11178 {
11180
11181 if (wagg->winref == wfunc->winref)
11182 {
11184 break;
11185 }
11186 }
11187 }
11188 if (l == NULL)
11189 elog(ERROR, "could not find window clause for winref %u",
11190 wfunc->winref);
11191 }
11192}
11193
11194/*
11195 * get_func_sql_syntax - Parse back a SQL-syntax function call
11196 *
11197 * Returns true if we successfully deparsed, false if we did not
11198 * recognize the function.
11199 */
11200static bool
11202{
11203 StringInfo buf = context->buf;
11204 Oid funcoid = expr->funcid;
11205
11206 switch (funcoid)
11207 {
11214 /* AT TIME ZONE ... note reversed argument order */
11216 get_rule_expr_paren((Node *) lsecond(expr->args), context, false,
11217 (Node *) expr);
11218 appendStringInfoString(buf, " AT TIME ZONE ");
11219 get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11220 (Node *) expr);
11222 return true;
11223
11226 case F_TIMEZONE_TIMETZ:
11227 /* AT LOCAL */
11229 get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11230 (Node *) expr);
11231 appendStringInfoString(buf, " AT LOCAL)");
11232 return true;
11233
11247 /* (x1, x2) OVERLAPS (y1, y2) */
11249 get_rule_expr((Node *) linitial(expr->args), context, false);
11251 get_rule_expr((Node *) lsecond(expr->args), context, false);
11252 appendStringInfoString(buf, ") OVERLAPS (");
11253 get_rule_expr((Node *) lthird(expr->args), context, false);
11255 get_rule_expr((Node *) lfourth(expr->args), context, false);
11257 return true;
11258
11265 /* EXTRACT (x FROM y) */
11266 appendStringInfoString(buf, "EXTRACT(");
11267 {
11268 Const *con = (Const *) linitial(expr->args);
11269
11270 Assert(IsA(con, Const) &&
11271 con->consttype == TEXTOID &&
11272 !con->constisnull);
11274 }
11275 appendStringInfoString(buf, " FROM ");
11276 get_rule_expr((Node *) lsecond(expr->args), context, false);
11278 return true;
11279
11280 case F_IS_NORMALIZED:
11281 /* IS xxx NORMALIZED */
11283 get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11284 (Node *) expr);
11286 if (list_length(expr->args) == 2)
11287 {
11288 Const *con = (Const *) lsecond(expr->args);
11289
11290 Assert(IsA(con, Const) &&
11291 con->consttype == TEXTOID &&
11292 !con->constisnull);
11293 appendStringInfo(buf, " %s",
11294 TextDatumGetCString(con->constvalue));
11295 }
11296 appendStringInfoString(buf, " NORMALIZED)");
11297 return true;
11298
11299 case F_PG_COLLATION_FOR:
11300 /* COLLATION FOR */
11301 appendStringInfoString(buf, "COLLATION FOR (");
11302 get_rule_expr((Node *) linitial(expr->args), context, false);
11304 return true;
11305
11306 case F_NORMALIZE:
11307 /* NORMALIZE() */
11308 appendStringInfoString(buf, "NORMALIZE(");
11309 get_rule_expr((Node *) linitial(expr->args), context, false);
11310 if (list_length(expr->args) == 2)
11311 {
11312 Const *con = (Const *) lsecond(expr->args);
11313
11314 Assert(IsA(con, Const) &&
11315 con->consttype == TEXTOID &&
11316 !con->constisnull);
11317 appendStringInfo(buf, ", %s",
11318 TextDatumGetCString(con->constvalue));
11319 }
11321 return true;
11322
11329 /* OVERLAY() */
11330 appendStringInfoString(buf, "OVERLAY(");
11331 get_rule_expr((Node *) linitial(expr->args), context, false);
11332 appendStringInfoString(buf, " PLACING ");
11333 get_rule_expr((Node *) lsecond(expr->args), context, false);
11334 appendStringInfoString(buf, " FROM ");
11335 get_rule_expr((Node *) lthird(expr->args), context, false);
11336 if (list_length(expr->args) == 4)
11337 {
11338 appendStringInfoString(buf, " FOR ");
11339 get_rule_expr((Node *) lfourth(expr->args), context, false);
11340 }
11342 return true;
11343
11344 case F_POSITION_BIT_BIT:
11347 /* POSITION() ... extra parens since args are b_expr not a_expr */
11348 appendStringInfoString(buf, "POSITION((");
11349 get_rule_expr((Node *) lsecond(expr->args), context, false);
11350 appendStringInfoString(buf, ") IN (");
11351 get_rule_expr((Node *) linitial(expr->args), context, false);
11353 return true;
11354
11361 /* SUBSTRING FROM/FOR (i.e., integer-position variants) */
11362 appendStringInfoString(buf, "SUBSTRING(");
11363 get_rule_expr((Node *) linitial(expr->args), context, false);
11364 appendStringInfoString(buf, " FROM ");
11365 get_rule_expr((Node *) lsecond(expr->args), context, false);
11366 if (list_length(expr->args) == 3)
11367 {
11368 appendStringInfoString(buf, " FOR ");
11369 get_rule_expr((Node *) lthird(expr->args), context, false);
11370 }
11372 return true;
11373
11375 /* SUBSTRING SIMILAR/ESCAPE */
11376 appendStringInfoString(buf, "SUBSTRING(");
11377 get_rule_expr((Node *) linitial(expr->args), context, false);
11378 appendStringInfoString(buf, " SIMILAR ");
11379 get_rule_expr((Node *) lsecond(expr->args), context, false);
11380 appendStringInfoString(buf, " ESCAPE ");
11381 get_rule_expr((Node *) lthird(expr->args), context, false);
11383 return true;
11384
11386 case F_BTRIM_TEXT:
11387 case F_BTRIM_TEXT_TEXT:
11388 /* TRIM() */
11389 appendStringInfoString(buf, "TRIM(BOTH");
11390 if (list_length(expr->args) == 2)
11391 {
11393 get_rule_expr((Node *) lsecond(expr->args), context, false);
11394 }
11395 appendStringInfoString(buf, " FROM ");
11396 get_rule_expr((Node *) linitial(expr->args), context, false);
11398 return true;
11399
11401 case F_LTRIM_TEXT:
11402 case F_LTRIM_TEXT_TEXT:
11403 /* TRIM() */
11404 appendStringInfoString(buf, "TRIM(LEADING");
11405 if (list_length(expr->args) == 2)
11406 {
11408 get_rule_expr((Node *) lsecond(expr->args), context, false);
11409 }
11410 appendStringInfoString(buf, " FROM ");
11411 get_rule_expr((Node *) linitial(expr->args), context, false);
11413 return true;
11414
11416 case F_RTRIM_TEXT:
11417 case F_RTRIM_TEXT_TEXT:
11418 /* TRIM() */
11419 appendStringInfoString(buf, "TRIM(TRAILING");
11420 if (list_length(expr->args) == 2)
11421 {
11423 get_rule_expr((Node *) lsecond(expr->args), context, false);
11424 }
11425 appendStringInfoString(buf, " FROM ");
11426 get_rule_expr((Node *) linitial(expr->args), context, false);
11428 return true;
11429
11430 case F_SYSTEM_USER:
11431 appendStringInfoString(buf, "SYSTEM_USER");
11432 return true;
11433
11434 case F_XMLEXISTS:
11435 /* XMLEXISTS ... extra parens because args are c_expr */
11436 appendStringInfoString(buf, "XMLEXISTS((");
11437 get_rule_expr((Node *) linitial(expr->args), context, false);
11438 appendStringInfoString(buf, ") PASSING (");
11439 get_rule_expr((Node *) lsecond(expr->args), context, false);
11441 return true;
11442 }
11443 return false;
11444}
11445
11446/* ----------
11447 * get_coercion_expr
11448 *
11449 * Make a string representation of a value coerced to a specific type
11450 * ----------
11451 */
11452static void
11454 Oid resulttype, int32 resulttypmod,
11456{
11457 StringInfo buf = context->buf;
11458
11459 /*
11460 * Since parse_coerce.c doesn't immediately collapse application of
11461 * length-coercion functions to constants, what we'll typically see in
11462 * such cases is a Const with typmod -1 and a length-coercion function
11463 * right above it. Avoid generating redundant output. However, beware of
11464 * suppressing casts when the user actually wrote something like
11465 * 'foo'::text::char(3).
11466 *
11467 * Note: it might seem that we are missing the possibility of needing to
11468 * print a COLLATE clause for such a Const. However, a Const could only
11469 * have nondefault collation in a post-constant-folding tree, in which the
11470 * length coercion would have been folded too. See also the special
11471 * handling of CollateExpr in coerce_to_target_type(): any collation
11472 * marking will be above the coercion node, not below it.
11473 */
11474 if (arg && IsA(arg, Const) &&
11475 ((Const *) arg)->consttype == resulttype &&
11476 ((Const *) arg)->consttypmod == -1)
11477 {
11478 /* Show the constant without normal ::typename decoration */
11479 get_const_expr((Const *) arg, context, -1);
11480 }
11481 else
11482 {
11483 if (!PRETTY_PAREN(context))
11485 get_rule_expr_paren(arg, context, false, parentNode);
11486 if (!PRETTY_PAREN(context))
11488 }
11489
11490 /*
11491 * Never emit resulttype(arg) functional notation. A pg_proc entry could
11492 * take precedence, and a resulttype in pg_temp would require schema
11493 * qualification that format_type_with_typemod() would usually omit. We've
11494 * standardized on arg::resulttype, but CAST(arg AS resulttype) notation
11495 * would work fine.
11496 */
11497 appendStringInfo(buf, "::%s",
11499}
11500
11501/* ----------
11502 * get_const_expr
11503 *
11504 * Make a string representation of a Const
11505 *
11506 * showtype can be -1 to never show "::typename" decoration, or +1 to always
11507 * show it, or 0 to show it only if the constant wouldn't be assumed to be
11508 * the right type by default.
11509 *
11510 * If the Const's collation isn't default for its type, show that too.
11511 * We mustn't do this when showtype is -1 (since that means the caller will
11512 * print "::typename", and we can't put a COLLATE clause in between). It's
11513 * caller's responsibility that collation isn't missed in such cases.
11514 * ----------
11515 */
11516static void
11518{
11519 StringInfo buf = context->buf;
11520 Oid typoutput;
11521 bool typIsVarlena;
11522 char *extval;
11523 bool needlabel = false;
11524
11525 if (constval->constisnull)
11526 {
11527 /*
11528 * Always label the type of a NULL constant to prevent misdecisions
11529 * about type when reparsing.
11530 */
11531 appendStringInfoString(buf, "NULL");
11532 if (showtype >= 0)
11533 {
11534 appendStringInfo(buf, "::%s",
11536 constval->consttypmod));
11537 get_const_collation(constval, context);
11538 }
11539 return;
11540 }
11541
11542 getTypeOutputInfo(constval->consttype,
11543 &typoutput, &typIsVarlena);
11544
11545 extval = OidOutputFunctionCall(typoutput, constval->constvalue);
11546
11547 switch (constval->consttype)
11548 {
11549 case INT4OID:
11550
11551 /*
11552 * INT4 can be printed without any decoration, unless it is
11553 * negative; in that case print it as '-nnn'::integer to ensure
11554 * that the output will re-parse as a constant, not as a constant
11555 * plus operator. In most cases we could get away with printing
11556 * (-nnn) instead, because of the way that gram.y handles negative
11557 * literals; but that doesn't work for INT_MIN, and it doesn't
11558 * seem that much prettier anyway.
11559 */
11560 if (extval[0] != '-')
11562 else
11563 {
11564 appendStringInfo(buf, "'%s'", extval);
11565 needlabel = true; /* we must attach a cast */
11566 }
11567 break;
11568
11569 case NUMERICOID:
11570
11571 /*
11572 * NUMERIC can be printed without quotes if it looks like a float
11573 * constant (not an integer, and not Infinity or NaN) and doesn't
11574 * have a leading sign (for the same reason as for INT4).
11575 */
11576 if (isdigit((unsigned char) extval[0]) &&
11577 strcspn(extval, "eE.") != strlen(extval))
11578 {
11580 }
11581 else
11582 {
11583 appendStringInfo(buf, "'%s'", extval);
11584 needlabel = true; /* we must attach a cast */
11585 }
11586 break;
11587
11588 case BOOLOID:
11589 if (strcmp(extval, "t") == 0)
11590 appendStringInfoString(buf, "true");
11591 else
11592 appendStringInfoString(buf, "false");
11593 break;
11594
11595 default:
11597 break;
11598 }
11599
11600 pfree(extval);
11601
11602 if (showtype < 0)
11603 return;
11604
11605 /*
11606 * For showtype == 0, append ::typename unless the constant will be
11607 * implicitly typed as the right type when it is read in.
11608 *
11609 * XXX this code has to be kept in sync with the behavior of the parser,
11610 * especially make_const.
11611 */
11612 switch (constval->consttype)
11613 {
11614 case BOOLOID:
11615 case UNKNOWNOID:
11616 /* These types can be left unlabeled */
11617 needlabel = false;
11618 break;
11619 case INT4OID:
11620 /* We determined above whether a label is needed */
11621 break;
11622 case NUMERICOID:
11623
11624 /*
11625 * Float-looking constants will be typed as numeric, which we
11626 * checked above; but if there's a nondefault typmod we need to
11627 * show it.
11628 */
11629 needlabel |= (constval->consttypmod >= 0);
11630 break;
11631 default:
11632 needlabel = true;
11633 break;
11634 }
11635 if (needlabel || showtype > 0)
11636 appendStringInfo(buf, "::%s",
11638 constval->consttypmod));
11639
11640 get_const_collation(constval, context);
11641}
11642
11643/*
11644 * helper for get_const_expr: append COLLATE if needed
11645 */
11646static void
11648{
11649 StringInfo buf = context->buf;
11650
11651 if (OidIsValid(constval->constcollid))
11652 {
11653 Oid typcollation = get_typcollation(constval->consttype);
11654
11655 if (constval->constcollid != typcollation)
11656 {
11657 appendStringInfo(buf, " COLLATE %s",
11658 generate_collation_name(constval->constcollid));
11659 }
11660 }
11661}
11662
11663/*
11664 * get_json_path_spec - Parse back a JSON path specification
11665 */
11666static void
11668{
11669 if (IsA(path_spec, Const))
11670 get_const_expr((Const *) path_spec, context, -1);
11671 else
11672 get_rule_expr(path_spec, context, showimplicit);
11673}
11674
11675/*
11676 * get_json_format - Parse back a JsonFormat node
11677 */
11678static void
11680{
11681 if (format->format_type == JS_FORMAT_DEFAULT)
11682 return;
11683
11685 format->format_type == JS_FORMAT_JSONB ?
11686 " FORMAT JSONB" : " FORMAT JSON");
11687
11688 if (format->encoding != JS_ENC_DEFAULT)
11689 {
11690 const char *encoding;
11691
11692 encoding =
11693 format->encoding == JS_ENC_UTF16 ? "UTF16" :
11694 format->encoding == JS_ENC_UTF32 ? "UTF32" : "UTF8";
11695
11696 appendStringInfo(buf, " ENCODING %s", encoding);
11697 }
11698}
11699
11700/*
11701 * get_json_returning - Parse back a JsonReturning structure
11702 */
11703static void
11706{
11707 if (!OidIsValid(returning->typid))
11708 return;
11709
11710 appendStringInfo(buf, " RETURNING %s",
11712 returning->typmod));
11713
11715 returning->format->format_type !=
11716 (returning->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON))
11717 get_json_format(returning->format, buf);
11718}
11719
11720/*
11721 * get_json_constructor - Parse back a JsonConstructorExpr node
11722 */
11723static void
11725 bool showimplicit)
11726{
11727 StringInfo buf = context->buf;
11728 const char *funcname;
11729 bool is_json_object;
11730 int curridx;
11731 ListCell *lc;
11732
11733 if (ctor->type == JSCTOR_JSON_OBJECTAGG)
11734 {
11735 get_json_agg_constructor(ctor, context, "JSON_OBJECTAGG", true);
11736 return;
11737 }
11738 else if (ctor->type == JSCTOR_JSON_ARRAYAGG)
11739 {
11740 get_json_agg_constructor(ctor, context, "JSON_ARRAYAGG", false);
11741 return;
11742 }
11743
11744 switch (ctor->type)
11745 {
11746 case JSCTOR_JSON_OBJECT:
11747 funcname = "JSON_OBJECT";
11748 break;
11749 case JSCTOR_JSON_ARRAY:
11750 funcname = "JSON_ARRAY";
11751 break;
11752 case JSCTOR_JSON_PARSE:
11753 funcname = "JSON";
11754 break;
11755 case JSCTOR_JSON_SCALAR:
11756 funcname = "JSON_SCALAR";
11757 break;
11759 funcname = "JSON_SERIALIZE";
11760 break;
11761 default:
11762 elog(ERROR, "invalid JsonConstructorType %d", ctor->type);
11763 }
11764
11765 appendStringInfo(buf, "%s(", funcname);
11766
11768 foreach(lc, ctor->args)
11769 {
11771 if (curridx > 0)
11772 {
11773 const char *sep;
11774
11775 sep = (is_json_object && (curridx % 2) != 0) ? " : " : ", ";
11777 }
11778
11779 get_rule_expr((Node *) lfirst(lc), context, true);
11780 }
11781
11784}
11785
11786/*
11787 * Append options, if any, to the JSON constructor being deparsed
11788 */
11789static void
11791{
11792 if (ctor->absent_on_null)
11793 {
11794 if (ctor->type == JSCTOR_JSON_OBJECT ||
11795 ctor->type == JSCTOR_JSON_OBJECTAGG)
11796 appendStringInfoString(buf, " ABSENT ON NULL");
11797 }
11798 else
11799 {
11800 if (ctor->type == JSCTOR_JSON_ARRAY ||
11801 ctor->type == JSCTOR_JSON_ARRAYAGG)
11802 appendStringInfoString(buf, " NULL ON NULL");
11803 }
11804
11805 if (ctor->unique)
11806 appendStringInfoString(buf, " WITH UNIQUE KEYS");
11807
11808 /*
11809 * Append RETURNING clause if needed; JSON() and JSON_SCALAR() don't
11810 * support one.
11811 */
11812 if (ctor->type != JSCTOR_JSON_PARSE && ctor->type != JSCTOR_JSON_SCALAR)
11813 get_json_returning(ctor->returning, buf, true);
11814}
11815
11816/*
11817 * get_json_agg_constructor - Parse back an aggregate JsonConstructorExpr node
11818 */
11819static void
11821 const char *funcname, bool is_json_objectagg)
11822{
11824
11827
11828 if (IsA(ctor->func, Aggref))
11829 get_agg_expr_helper((Aggref *) ctor->func, context,
11830 (Aggref *) ctor->func,
11832 else if (IsA(ctor->func, WindowFunc))
11833 get_windowfunc_expr_helper((WindowFunc *) ctor->func, context,
11834 funcname, options.data,
11836 else
11837 elog(ERROR, "invalid JsonConstructorExpr underlying node type: %d",
11838 nodeTag(ctor->func));
11839}
11840
11841/*
11842 * simple_quote_literal - Format a string as a SQL literal, append to buf
11843 */
11844static void
11846{
11847 const char *valptr;
11848
11849 /*
11850 * We always form the string literal according to standard SQL rules.
11851 */
11853 for (valptr = val; *valptr; valptr++)
11854 {
11855 char ch = *valptr;
11856
11857 if (SQL_STR_DOUBLE(ch, false))
11860 }
11862}
11863
11864
11865/* ----------
11866 * get_sublink_expr - Parse back a sublink
11867 * ----------
11868 */
11869static void
11871{
11872 StringInfo buf = context->buf;
11873 Query *query = (Query *) (sublink->subselect);
11874 char *opname = NULL;
11875 bool need_paren;
11876
11877 if (sublink->subLinkType == ARRAY_SUBLINK)
11878 appendStringInfoString(buf, "ARRAY(");
11879 else
11881
11882 /*
11883 * Note that we print the name of only the first operator, when there are
11884 * multiple combining operators. This is an approximation that could go
11885 * wrong in various scenarios (operators in different schemas, renamed
11886 * operators, etc) but there is not a whole lot we can do about it, since
11887 * the syntax allows only one operator to be shown.
11888 */
11889 if (sublink->testexpr)
11890 {
11891 if (IsA(sublink->testexpr, OpExpr))
11892 {
11893 /* single combining operator */
11894 OpExpr *opexpr = (OpExpr *) sublink->testexpr;
11895
11896 get_rule_expr(linitial(opexpr->args), context, true);
11898 exprType(linitial(opexpr->args)),
11899 exprType(lsecond(opexpr->args)));
11900 }
11901 else if (IsA(sublink->testexpr, BoolExpr))
11902 {
11903 /* multiple combining operators, = or <> cases */
11904 char *sep;
11905 ListCell *l;
11906
11908 sep = "";
11909 foreach(l, ((BoolExpr *) sublink->testexpr)->args)
11910 {
11911 OpExpr *opexpr = lfirst_node(OpExpr, l);
11912
11914 get_rule_expr(linitial(opexpr->args), context, true);
11915 if (!opname)
11917 exprType(linitial(opexpr->args)),
11918 exprType(lsecond(opexpr->args)));
11919 sep = ", ";
11920 }
11922 }
11923 else if (IsA(sublink->testexpr, RowCompareExpr))
11924 {
11925 /* multiple combining operators, < <= > >= cases */
11927
11929 get_rule_expr((Node *) rcexpr->largs, context, true);
11931 exprType(linitial(rcexpr->largs)),
11932 exprType(linitial(rcexpr->rargs)));
11934 }
11935 else
11936 elog(ERROR, "unrecognized testexpr type: %d",
11937 (int) nodeTag(sublink->testexpr));
11938 }
11939
11940 need_paren = true;
11941
11942 switch (sublink->subLinkType)
11943 {
11944 case EXISTS_SUBLINK:
11945 appendStringInfoString(buf, "EXISTS ");
11946 break;
11947
11948 case ANY_SUBLINK:
11949 if (strcmp(opname, "=") == 0) /* Represent = ANY as IN */
11950 appendStringInfoString(buf, " IN ");
11951 else
11952 appendStringInfo(buf, " %s ANY ", opname);
11953 break;
11954
11955 case ALL_SUBLINK:
11956 appendStringInfo(buf, " %s ALL ", opname);
11957 break;
11958
11959 case ROWCOMPARE_SUBLINK:
11960 appendStringInfo(buf, " %s ", opname);
11961 break;
11962
11963 case EXPR_SUBLINK:
11964 case MULTIEXPR_SUBLINK:
11965 case ARRAY_SUBLINK:
11966 need_paren = false;
11967 break;
11968
11969 case CTE_SUBLINK: /* shouldn't occur in a SubLink */
11970 default:
11971 elog(ERROR, "unrecognized sublink type: %d",
11972 (int) sublink->subLinkType);
11973 break;
11974 }
11975
11976 if (need_paren)
11978
11979 get_query_def(query, buf, context->namespaces, NULL, false,
11980 context->prettyFlags, context->wrapColumn,
11981 context->indentLevel);
11982
11983 if (need_paren)
11985 else
11987}
11988
11989
11990/* ----------
11991 * get_xmltable - Parse back a XMLTABLE function
11992 * ----------
11993 */
11994static void
11996{
11997 StringInfo buf = context->buf;
11998
11999 appendStringInfoString(buf, "XMLTABLE(");
12000
12001 if (tf->ns_uris != NIL)
12002 {
12003 ListCell *lc1,
12004 *lc2;
12005 bool first = true;
12006
12007 appendStringInfoString(buf, "XMLNAMESPACES (");
12008 forboth(lc1, tf->ns_uris, lc2, tf->ns_names)
12009 {
12010 Node *expr = (Node *) lfirst(lc1);
12012
12013 if (!first)
12015 else
12016 first = false;
12017
12018 if (ns_node != NULL)
12019 {
12020 get_rule_expr(expr, context, showimplicit);
12021 appendStringInfo(buf, " AS %s",
12023 }
12024 else
12025 {
12026 appendStringInfoString(buf, "DEFAULT ");
12027 get_rule_expr(expr, context, showimplicit);
12028 }
12029 }
12031 }
12032
12034 get_rule_expr((Node *) tf->rowexpr, context, showimplicit);
12035 appendStringInfoString(buf, ") PASSING (");
12036 get_rule_expr((Node *) tf->docexpr, context, showimplicit);
12038
12039 if (tf->colexprs != NIL)
12040 {
12041 ListCell *l1;
12042 ListCell *l2;
12043 ListCell *l3;
12044 ListCell *l4;
12045 ListCell *l5;
12046 int colnum = 0;
12047
12048 appendStringInfoString(buf, " COLUMNS ");
12049 forfive(l1, tf->colnames, l2, tf->coltypes, l3, tf->coltypmods,
12050 l4, tf->colexprs, l5, tf->coldefexprs)
12051 {
12052 char *colname = strVal(lfirst(l1));
12053 Oid typid = lfirst_oid(l2);
12054 int32 typmod = lfirst_int(l3);
12055 Node *colexpr = (Node *) lfirst(l4);
12056 Node *coldefexpr = (Node *) lfirst(l5);
12057 bool ordinality = (tf->ordinalitycol == colnum);
12058 bool notnull = bms_is_member(colnum, tf->notnulls);
12059
12060 if (colnum > 0)
12062 colnum++;
12063
12064 appendStringInfo(buf, "%s %s", quote_identifier(colname),
12065 ordinality ? "FOR ORDINALITY" :
12066 format_type_with_typemod(typid, typmod));
12067 if (ordinality)
12068 continue;
12069
12070 if (coldefexpr != NULL)
12071 {
12072 appendStringInfoString(buf, " DEFAULT (");
12073 get_rule_expr((Node *) coldefexpr, context, showimplicit);
12075 }
12076 if (colexpr != NULL)
12077 {
12078 appendStringInfoString(buf, " PATH (");
12079 get_rule_expr((Node *) colexpr, context, showimplicit);
12081 }
12082 if (notnull)
12083 appendStringInfoString(buf, " NOT NULL");
12084 }
12085 }
12086
12088}
12089
12090/*
12091 * get_json_table_nested_columns - Parse back nested JSON_TABLE columns
12092 */
12093static void
12095 deparse_context *context, bool showimplicit,
12096 bool needcomma)
12097{
12099 {
12101
12102 if (needcomma)
12103 appendStringInfoChar(context->buf, ',');
12104
12105 appendStringInfoChar(context->buf, ' ');
12106 appendContextKeyword(context, "NESTED PATH ", 0, 0, 0);
12107 get_const_expr(scan->path->value, context, -1);
12108 appendStringInfo(context->buf, " AS %s", quote_identifier(scan->path->name));
12109 get_json_table_columns(tf, scan, context, showimplicit);
12110 }
12111 else if (IsA(plan, JsonTableSiblingJoin))
12112 {
12114
12116 needcomma);
12118 true);
12119 }
12120}
12121
12122/*
12123 * get_json_table_columns - Parse back JSON_TABLE columns
12124 */
12125static void
12127 deparse_context *context,
12128 bool showimplicit)
12129{
12130 StringInfo buf = context->buf;
12135 int colnum = 0;
12136
12138 appendContextKeyword(context, "COLUMNS (", 0, 0, 0);
12139
12140 if (PRETTY_INDENT(context))
12141 context->indentLevel += PRETTYINDENT_VAR;
12142
12143 forfour(lc_colname, tf->colnames,
12144 lc_coltype, tf->coltypes,
12145 lc_coltypmod, tf->coltypmods,
12146 lc_colvalexpr, tf->colvalexprs)
12147 {
12148 char *colname = strVal(lfirst(lc_colname));
12149 JsonExpr *colexpr;
12150 Oid typid;
12151 int32 typmod;
12152 bool ordinality;
12154
12155 typid = lfirst_oid(lc_coltype);
12156 typmod = lfirst_int(lc_coltypmod);
12158
12159 /* Skip columns that don't belong to this scan. */
12160 if (scan->colMin < 0 || colnum < scan->colMin)
12161 {
12162 colnum++;
12163 continue;
12164 }
12165 if (colnum > scan->colMax)
12166 break;
12167
12168 if (colnum > scan->colMin)
12170
12171 colnum++;
12172
12173 ordinality = !colexpr;
12174
12175 appendContextKeyword(context, "", 0, 0, 0);
12176
12177 appendStringInfo(buf, "%s %s", quote_identifier(colname),
12178 ordinality ? "FOR ORDINALITY" :
12179 format_type_with_typemod(typid, typmod));
12180 if (ordinality)
12181 continue;
12182
12183 /*
12184 * Set default_behavior to guide get_json_expr_options() on whether to
12185 * emit the ON ERROR / EMPTY clauses.
12186 */
12187 if (colexpr->op == JSON_EXISTS_OP)
12188 {
12189 appendStringInfoString(buf, " EXISTS");
12191 }
12192 else
12193 {
12194 if (colexpr->op == JSON_QUERY_OP)
12195 {
12196 char typcategory;
12197 bool typispreferred;
12198
12200
12203 colexpr->format->format_type == JS_FORMAT_JSONB ?
12204 " FORMAT JSONB" : " FORMAT JSON");
12205 }
12206
12208 }
12209
12210 appendStringInfoString(buf, " PATH ");
12211
12212 get_json_path_spec(colexpr->path_spec, context, showimplicit);
12213
12214 get_json_expr_options(colexpr, context, default_behavior);
12215 }
12216
12217 if (scan->child)
12219 scan->colMin >= 0);
12220
12221 if (PRETTY_INDENT(context))
12222 context->indentLevel -= PRETTYINDENT_VAR;
12223
12224 appendContextKeyword(context, ")", 0, 0, 0);
12225}
12226
12227/* ----------
12228 * get_json_table - Parse back a JSON_TABLE function
12229 * ----------
12230 */
12231static void
12233{
12234 StringInfo buf = context->buf;
12237
12238 appendStringInfoString(buf, "JSON_TABLE(");
12239
12240 if (PRETTY_INDENT(context))
12241 context->indentLevel += PRETTYINDENT_VAR;
12242
12243 appendContextKeyword(context, "", 0, 0, 0);
12244
12245 get_rule_expr(jexpr->formatted_expr, context, showimplicit);
12246
12248
12249 get_const_expr(root->path->value, context, -1);
12250
12251 appendStringInfo(buf, " AS %s", quote_identifier(root->path->name));
12252
12253 if (jexpr->passing_values)
12254 {
12255 ListCell *lc1,
12256 *lc2;
12257 bool needcomma = false;
12258
12260 appendContextKeyword(context, "PASSING ", 0, 0, 0);
12261
12262 if (PRETTY_INDENT(context))
12263 context->indentLevel += PRETTYINDENT_VAR;
12264
12265 forboth(lc1, jexpr->passing_names,
12266 lc2, jexpr->passing_values)
12267 {
12268 if (needcomma)
12270 needcomma = true;
12271
12272 appendContextKeyword(context, "", 0, 0, 0);
12273
12274 get_rule_expr((Node *) lfirst(lc2), context, false);
12275 appendStringInfo(buf, " AS %s",
12277 );
12278 }
12279
12280 if (PRETTY_INDENT(context))
12281 context->indentLevel -= PRETTYINDENT_VAR;
12282 }
12283
12284 get_json_table_columns(tf, castNode(JsonTablePathScan, tf->plan), context,
12285 showimplicit);
12286
12287 if (jexpr->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY)
12288 get_json_behavior(jexpr->on_error, context, "ERROR");
12289
12290 if (PRETTY_INDENT(context))
12291 context->indentLevel -= PRETTYINDENT_VAR;
12292
12293 appendContextKeyword(context, ")", 0, 0, 0);
12294}
12295
12296/* ----------
12297 * get_tablefunc - Parse back a table function
12298 * ----------
12299 */
12300static void
12302{
12303 /* XMLTABLE and JSON_TABLE are the only existing implementations. */
12304
12305 if (tf->functype == TFT_XMLTABLE)
12306 get_xmltable(tf, context, showimplicit);
12307 else if (tf->functype == TFT_JSON_TABLE)
12308 get_json_table(tf, context, showimplicit);
12309}
12310
12311/* ----------
12312 * get_from_clause - Parse back a FROM clause
12313 *
12314 * "prefix" is the keyword that denotes the start of the list of FROM
12315 * elements. It is FROM when used to parse back SELECT and UPDATE, but
12316 * is USING when parsing back DELETE.
12317 * ----------
12318 */
12319static void
12320get_from_clause(Query *query, const char *prefix, deparse_context *context)
12321{
12322 StringInfo buf = context->buf;
12323 bool first = true;
12324 ListCell *l;
12325
12326 /*
12327 * We use the query's jointree as a guide to what to print. However, we
12328 * must ignore auto-added RTEs that are marked not inFromCl. (These can
12329 * only appear at the top level of the jointree, so it's sufficient to
12330 * check here.) This check also ensures we ignore the rule pseudo-RTEs
12331 * for NEW and OLD.
12332 */
12333 foreach(l, query->jointree->fromlist)
12334 {
12335 Node *jtnode = (Node *) lfirst(l);
12336
12337 if (IsA(jtnode, RangeTblRef))
12338 {
12339 int varno = ((RangeTblRef *) jtnode)->rtindex;
12340 RangeTblEntry *rte = rt_fetch(varno, query->rtable);
12341
12342 if (!rte->inFromCl)
12343 continue;
12344 }
12345
12346 if (first)
12347 {
12348 appendContextKeyword(context, prefix,
12350 first = false;
12351
12352 get_from_clause_item(jtnode, query, context);
12353 }
12354 else
12355 {
12357
12359
12360 /*
12361 * Put the new FROM item's text into itembuf so we can decide
12362 * after we've got it whether or not it needs to go on a new line.
12363 */
12365 context->buf = &itembuf;
12366
12367 get_from_clause_item(jtnode, query, context);
12368
12369 /* Restore context's output buffer */
12370 context->buf = buf;
12371
12372 /* Consider line-wrapping if enabled */
12373 if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
12374 {
12375 /* Does the new item start with a new line? */
12376 if (itembuf.len > 0 && itembuf.data[0] == '\n')
12377 {
12378 /* If so, we shouldn't add anything */
12379 /* instead, remove any trailing spaces currently in buf */
12381 }
12382 else
12383 {
12384 char *trailing_nl;
12385
12386 /* Locate the start of the current line in the buffer */
12387 trailing_nl = strrchr(buf->data, '\n');
12388 if (trailing_nl == NULL)
12389 trailing_nl = buf->data;
12390 else
12391 trailing_nl++;
12392
12393 /*
12394 * Add a newline, plus some indentation, if the new item
12395 * would cause an overflow.
12396 */
12397 if (strlen(trailing_nl) + itembuf.len > context->wrapColumn)
12401 }
12402 }
12403
12404 /* Add the new item */
12406
12407 /* clean up */
12408 pfree(itembuf.data);
12409 }
12410 }
12411}
12412
12413static void
12415{
12416 StringInfo buf = context->buf;
12418
12419 if (IsA(jtnode, RangeTblRef))
12420 {
12421 int varno = ((RangeTblRef *) jtnode)->rtindex;
12422 RangeTblEntry *rte = rt_fetch(varno, query->rtable);
12425
12426 if (rte->lateral)
12427 appendStringInfoString(buf, "LATERAL ");
12428
12429 /* Print the FROM item proper */
12430 switch (rte->rtekind)
12431 {
12432 case RTE_RELATION:
12433 /* Normal relation RTE */
12434 appendStringInfo(buf, "%s%s",
12437 context->namespaces));
12438 break;
12439 case RTE_SUBQUERY:
12440 /* Subquery RTE */
12442 get_query_def(rte->subquery, buf, context->namespaces, NULL,
12443 true,
12444 context->prettyFlags, context->wrapColumn,
12445 context->indentLevel);
12447 break;
12448 case RTE_FUNCTION:
12449 /* Function RTE */
12450 rtfunc1 = (RangeTblFunction *) linitial(rte->functions);
12451
12452 /*
12453 * Omit ROWS FROM() syntax for just one function, unless it
12454 * has both a coldeflist and WITH ORDINALITY. If it has both,
12455 * we must use ROWS FROM() syntax to avoid ambiguity about
12456 * whether the coldeflist includes the ordinality column.
12457 */
12458 if (list_length(rte->functions) == 1 &&
12459 (rtfunc1->funccolnames == NIL || !rte->funcordinality))
12460 {
12461 get_rule_expr_funccall(rtfunc1->funcexpr, context, true);
12462 /* we'll print the coldeflist below, if it has one */
12463 }
12464 else
12465 {
12466 bool all_unnest;
12467 ListCell *lc;
12468
12469 /*
12470 * If all the function calls in the list are to unnest,
12471 * and none need a coldeflist, then collapse the list back
12472 * down to UNNEST(args). (If we had more than one
12473 * built-in unnest function, this would get more
12474 * difficult.)
12475 *
12476 * XXX This is pretty ugly, since it makes not-terribly-
12477 * future-proof assumptions about what the parser would do
12478 * with the output; but the alternative is to emit our
12479 * nonstandard ROWS FROM() notation for what might have
12480 * been a perfectly spec-compliant multi-argument
12481 * UNNEST().
12482 */
12483 all_unnest = true;
12484 foreach(lc, rte->functions)
12485 {
12487
12488 if (!IsA(rtfunc->funcexpr, FuncExpr) ||
12489 ((FuncExpr *) rtfunc->funcexpr)->funcid != F_UNNEST_ANYARRAY ||
12490 rtfunc->funccolnames != NIL)
12491 {
12492 all_unnest = false;
12493 break;
12494 }
12495 }
12496
12497 if (all_unnest)
12498 {
12499 List *allargs = NIL;
12500
12501 foreach(lc, rte->functions)
12502 {
12504 List *args = ((FuncExpr *) rtfunc->funcexpr)->args;
12505
12506 allargs = list_concat(allargs, args);
12507 }
12508
12509 appendStringInfoString(buf, "UNNEST(");
12510 get_rule_expr((Node *) allargs, context, true);
12512 }
12513 else
12514 {
12515 int funcno = 0;
12516
12517 appendStringInfoString(buf, "ROWS FROM(");
12518 foreach(lc, rte->functions)
12519 {
12521
12522 if (funcno > 0)
12524 get_rule_expr_funccall(rtfunc->funcexpr, context, true);
12525 if (rtfunc->funccolnames != NIL)
12526 {
12527 /* Reconstruct the column definition list */
12528 appendStringInfoString(buf, " AS ");
12530 NULL,
12531 context);
12532 }
12533 funcno++;
12534 }
12536 }
12537 /* prevent printing duplicate coldeflist below */
12538 rtfunc1 = NULL;
12539 }
12540 if (rte->funcordinality)
12541 appendStringInfoString(buf, " WITH ORDINALITY");
12542 break;
12543 case RTE_TABLEFUNC:
12544 get_tablefunc(rte->tablefunc, context, true);
12545 break;
12546 case RTE_VALUES:
12547 /* Values list RTE */
12549 get_values_def(rte->values_lists, context);
12551 break;
12552 case RTE_CTE:
12554 break;
12555 default:
12556 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
12557 break;
12558 }
12559
12560 /* Print the relation alias, if needed */
12561 get_rte_alias(rte, varno, false, context);
12562
12563 /* Print the column definitions or aliases, if needed */
12564 if (rtfunc1 && rtfunc1->funccolnames != NIL)
12565 {
12566 /* Reconstruct the columndef list, which is also the aliases */
12568 }
12569 else
12570 {
12571 /* Else print column aliases as needed */
12573 }
12574
12575 /* Tablesample clause must go after any alias */
12576 if (rte->rtekind == RTE_RELATION && rte->tablesample)
12577 get_tablesample_def(rte->tablesample, context);
12578 }
12579 else if (IsA(jtnode, JoinExpr))
12580 {
12581 JoinExpr *j = (JoinExpr *) jtnode;
12584
12585 need_paren_on_right = PRETTY_PAREN(context) &&
12586 !IsA(j->rarg, RangeTblRef) &&
12587 !(IsA(j->rarg, JoinExpr) && ((JoinExpr *) j->rarg)->alias != NULL);
12588
12589 if (!PRETTY_PAREN(context) || j->alias != NULL)
12591
12592 get_from_clause_item(j->larg, query, context);
12593
12594 switch (j->jointype)
12595 {
12596 case JOIN_INNER:
12597 if (j->quals)
12598 appendContextKeyword(context, " JOIN ",
12602 else
12603 appendContextKeyword(context, " CROSS JOIN ",
12607 break;
12608 case JOIN_LEFT:
12609 appendContextKeyword(context, " LEFT JOIN ",
12613 break;
12614 case JOIN_FULL:
12615 appendContextKeyword(context, " FULL JOIN ",
12619 break;
12620 case JOIN_RIGHT:
12621 appendContextKeyword(context, " RIGHT JOIN ",
12625 break;
12626 default:
12627 elog(ERROR, "unrecognized join type: %d",
12628 (int) j->jointype);
12629 }
12630
12633 get_from_clause_item(j->rarg, query, context);
12636
12637 if (j->usingClause)
12638 {
12639 ListCell *lc;
12640 bool first = true;
12641
12642 appendStringInfoString(buf, " USING (");
12643 /* Use the assigned names, not what's in usingClause */
12644 foreach(lc, colinfo->usingNames)
12645 {
12646 char *colname = (char *) lfirst(lc);
12647
12648 if (first)
12649 first = false;
12650 else
12653 }
12655
12656 if (j->join_using_alias)
12657 appendStringInfo(buf, " AS %s",
12658 quote_identifier(j->join_using_alias->aliasname));
12659 }
12660 else if (j->quals)
12661 {
12662 appendStringInfoString(buf, " ON ");
12663 if (!PRETTY_PAREN(context))
12665 get_rule_expr(j->quals, context, false);
12666 if (!PRETTY_PAREN(context))
12668 }
12669 else if (j->jointype != JOIN_INNER)
12670 {
12671 /* If we didn't say CROSS JOIN above, we must provide an ON */
12672 appendStringInfoString(buf, " ON TRUE");
12673 }
12674
12675 if (!PRETTY_PAREN(context) || j->alias != NULL)
12677
12678 /* Yes, it's correct to put alias after the right paren ... */
12679 if (j->alias != NULL)
12680 {
12681 /*
12682 * Note that it's correct to emit an alias clause if and only if
12683 * there was one originally. Otherwise we'd be converting a named
12684 * join to unnamed or vice versa, which creates semantic
12685 * subtleties we don't want. However, we might print a different
12686 * alias name than was there originally.
12687 */
12688 appendStringInfo(buf, " %s",
12690 context)));
12692 }
12693 }
12694 else
12695 elog(ERROR, "unrecognized node type: %d",
12696 (int) nodeTag(jtnode));
12697}
12698
12699/*
12700 * get_rte_alias - print the relation's alias, if needed
12701 *
12702 * If printed, the alias is preceded by a space, or by " AS " if use_as is true.
12703 */
12704static void
12706 deparse_context *context)
12707{
12709 char *refname = get_rtable_name(varno, context);
12711 bool printalias = false;
12712
12713 if (rte->alias != NULL)
12714 {
12715 /* Always print alias if user provided one */
12716 printalias = true;
12717 }
12718 else if (colinfo->printaliases)
12719 {
12720 /* Always print alias if we need to print column aliases */
12721 printalias = true;
12722 }
12723 else if (rte->rtekind == RTE_RELATION)
12724 {
12725 /*
12726 * No need to print alias if it's same as relation name (this would
12727 * normally be the case, but not if set_rtable_names had to resolve a
12728 * conflict).
12729 */
12730 if (strcmp(refname, get_relation_name(rte->relid)) != 0)
12731 printalias = true;
12732 }
12733 else if (rte->rtekind == RTE_FUNCTION)
12734 {
12735 /*
12736 * For a function RTE, always print alias. This covers possible
12737 * renaming of the function and/or instability of the FigureColname
12738 * rules for things that aren't simple functions. Note we'd need to
12739 * force it anyway for the columndef list case.
12740 */
12741 printalias = true;
12742 }
12743 else if (rte->rtekind == RTE_SUBQUERY ||
12744 rte->rtekind == RTE_VALUES)
12745 {
12746 /*
12747 * For a subquery, always print alias. This makes the output
12748 * SQL-spec-compliant, even though we allow such aliases to be omitted
12749 * on input.
12750 */
12751 printalias = true;
12752 }
12753 else if (rte->rtekind == RTE_CTE)
12754 {
12755 /*
12756 * No need to print alias if it's same as CTE name (this would
12757 * normally be the case, but not if set_rtable_names had to resolve a
12758 * conflict).
12759 */
12760 if (strcmp(refname, rte->ctename) != 0)
12761 printalias = true;
12762 }
12763
12764 if (printalias)
12765 appendStringInfo(context->buf, "%s%s",
12766 use_as ? " AS " : " ",
12767 quote_identifier(refname));
12768}
12769
12770/*
12771 * get_column_alias_list - print column alias list for an RTE
12772 *
12773 * Caller must already have printed the relation's alias name.
12774 */
12775static void
12777{
12778 StringInfo buf = context->buf;
12779 int i;
12780 bool first = true;
12781
12782 /* Don't print aliases if not needed */
12783 if (!colinfo->printaliases)
12784 return;
12785
12786 for (i = 0; i < colinfo->num_new_cols; i++)
12787 {
12788 char *colname = colinfo->new_colnames[i];
12789
12790 if (first)
12791 {
12793 first = false;
12794 }
12795 else
12798 }
12799 if (!first)
12801}
12802
12803/*
12804 * get_from_clause_coldeflist - reproduce FROM clause coldeflist
12805 *
12806 * When printing a top-level coldeflist (which is syntactically also the
12807 * relation's column alias list), use column names from colinfo. But when
12808 * printing a coldeflist embedded inside ROWS FROM(), we prefer to use the
12809 * original coldeflist's names, which are available in rtfunc->funccolnames.
12810 * Pass NULL for colinfo to select the latter behavior.
12811 *
12812 * The coldeflist is appended immediately (no space) to buf. Caller is
12813 * responsible for ensuring that an alias or AS is present before it.
12814 */
12815static void
12818 deparse_context *context)
12819{
12820 StringInfo buf = context->buf;
12821 ListCell *l1;
12822 ListCell *l2;
12823 ListCell *l3;
12824 ListCell *l4;
12825 int i;
12826
12828
12829 i = 0;
12830 forfour(l1, rtfunc->funccoltypes,
12831 l2, rtfunc->funccoltypmods,
12832 l3, rtfunc->funccolcollations,
12833 l4, rtfunc->funccolnames)
12834 {
12835 Oid atttypid = lfirst_oid(l1);
12836 int32 atttypmod = lfirst_int(l2);
12837 Oid attcollation = lfirst_oid(l3);
12838 char *attname;
12839
12840 if (colinfo)
12841 attname = colinfo->colnames[i];
12842 else
12843 attname = strVal(lfirst(l4));
12844
12845 Assert(attname); /* shouldn't be any dropped columns here */
12846
12847 if (i > 0)
12849 appendStringInfo(buf, "%s %s",
12852 if (OidIsValid(attcollation) &&
12853 attcollation != get_typcollation(atttypid))
12854 appendStringInfo(buf, " COLLATE %s",
12855 generate_collation_name(attcollation));
12856
12857 i++;
12858 }
12859
12861}
12862
12863/*
12864 * get_tablesample_def - print a TableSampleClause
12865 */
12866static void
12868{
12869 StringInfo buf = context->buf;
12870 Oid argtypes[1];
12871 int nargs;
12872 ListCell *l;
12873
12874 /*
12875 * We should qualify the handler's function name if it wouldn't be
12876 * resolved by lookup in the current search path.
12877 */
12878 argtypes[0] = INTERNALOID;
12879 appendStringInfo(buf, " TABLESAMPLE %s (",
12880 generate_function_name(tablesample->tsmhandler, 1,
12881 NIL, argtypes,
12882 false, NULL, false));
12883
12884 nargs = 0;
12885 foreach(l, tablesample->args)
12886 {
12887 if (nargs++ > 0)
12889 get_rule_expr((Node *) lfirst(l), context, false);
12890 }
12892
12893 if (tablesample->repeatable != NULL)
12894 {
12895 appendStringInfoString(buf, " REPEATABLE (");
12896 get_rule_expr((Node *) tablesample->repeatable, context, false);
12898 }
12899}
12900
12901/*
12902 * get_opclass_name - fetch name of an index operator class
12903 *
12904 * The opclass name is appended (after a space) to buf.
12905 *
12906 * Output is suppressed if the opclass is the default for the given
12907 * actual_datatype. (If you don't want this behavior, just pass
12908 * InvalidOid for actual_datatype.)
12909 */
12910static void
12913{
12916 char *opcname;
12917 char *nspname;
12918
12921 elog(ERROR, "cache lookup failed for opclass %u", opclass);
12923
12925 GetDefaultOpClass(actual_datatype, opcrec->opcmethod) != opclass)
12926 {
12927 /* Okay, we need the opclass name. Do we need to qualify it? */
12928 opcname = NameStr(opcrec->opcname);
12929 if (OpclassIsVisible(opclass))
12931 else
12932 {
12933 nspname = get_namespace_name_or_temp(opcrec->opcnamespace);
12934 appendStringInfo(buf, " %s.%s",
12935 quote_identifier(nspname),
12937 }
12938 }
12940}
12941
12942/*
12943 * generate_opclass_name
12944 * Compute the name to display for an opclass specified by OID
12945 *
12946 * The result includes all necessary quoting and schema-prefixing.
12947 */
12948char *
12950{
12952
12954 get_opclass_name(opclass, InvalidOid, &buf);
12955
12956 return &buf.data[1]; /* get_opclass_name() prepends space */
12957}
12958
12959/*
12960 * processIndirection - take care of array and subfield assignment
12961 *
12962 * We strip any top-level FieldStore or assignment SubscriptingRef nodes that
12963 * appear in the input, printing them as decoration for the base column
12964 * name (which we assume the caller just printed). We might also need to
12965 * strip CoerceToDomain nodes, but only ones that appear above assignment
12966 * nodes.
12967 *
12968 * Returns the subexpression that's to be assigned.
12969 */
12970static Node *
12972{
12973 StringInfo buf = context->buf;
12975
12976 for (;;)
12977 {
12978 if (node == NULL)
12979 break;
12980 if (IsA(node, FieldStore))
12981 {
12982 FieldStore *fstore = (FieldStore *) node;
12983 Oid typrelid;
12984 char *fieldname;
12985
12986 /* lookup tuple type */
12987 typrelid = get_typ_typrelid(fstore->resulttype);
12988 if (!OidIsValid(typrelid))
12989 elog(ERROR, "argument type %s of FieldStore is not a tuple type",
12990 format_type_be(fstore->resulttype));
12991
12992 /*
12993 * Print the field name. There should only be one target field in
12994 * stored rules. There could be more than that in executable
12995 * target lists, but this function cannot be used for that case.
12996 */
12997 Assert(list_length(fstore->fieldnums) == 1);
12998 fieldname = get_attname(typrelid,
12999 linitial_int(fstore->fieldnums), false);
13000 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
13001
13002 /*
13003 * We ignore arg since it should be an uninteresting reference to
13004 * the target column or subcolumn.
13005 */
13006 node = (Node *) linitial(fstore->newvals);
13007 }
13008 else if (IsA(node, SubscriptingRef))
13009 {
13010 SubscriptingRef *sbsref = (SubscriptingRef *) node;
13011
13012 if (sbsref->refassgnexpr == NULL)
13013 break;
13014
13015 printSubscripts(sbsref, context);
13016
13017 /*
13018 * We ignore refexpr since it should be an uninteresting reference
13019 * to the target column or subcolumn.
13020 */
13021 node = (Node *) sbsref->refassgnexpr;
13022 }
13023 else if (IsA(node, CoerceToDomain))
13024 {
13025 cdomain = (CoerceToDomain *) node;
13026 /* If it's an explicit domain coercion, we're done */
13027 if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
13028 break;
13029 /* Tentatively descend past the CoerceToDomain */
13030 node = (Node *) cdomain->arg;
13031 }
13032 else
13033 break;
13034 }
13035
13036 /*
13037 * If we descended past a CoerceToDomain whose argument turned out not to
13038 * be a FieldStore or array assignment, back up to the CoerceToDomain.
13039 * (This is not enough to be fully correct if there are nested implicit
13040 * CoerceToDomains, but such cases shouldn't ever occur.)
13041 */
13042 if (cdomain && node == (Node *) cdomain->arg)
13043 node = (Node *) cdomain;
13044
13045 return node;
13046}
13047
13048static void
13050{
13051 StringInfo buf = context->buf;
13054
13055 lowlist_item = list_head(sbsref->reflowerindexpr); /* could be NULL */
13056 foreach(uplist_item, sbsref->refupperindexpr)
13057 {
13059 if (lowlist_item)
13060 {
13061 /* If subexpression is NULL, get_rule_expr prints nothing */
13062 get_rule_expr((Node *) lfirst(lowlist_item), context, false);
13065 }
13066 /* If subexpression is NULL, get_rule_expr prints nothing */
13067 get_rule_expr((Node *) lfirst(uplist_item), context, false);
13069 }
13070}
13071
13072/*
13073 * quote_identifier - Quote an identifier only if needed
13074 *
13075 * When quotes are needed, we palloc the required space; slightly
13076 * space-wasteful but well worth it for notational simplicity.
13077 */
13078const char *
13080{
13081 /*
13082 * Can avoid quoting if ident starts with a lowercase letter or underscore
13083 * and contains only lowercase letters, digits, and underscores, *and* is
13084 * not any SQL keyword. Otherwise, supply quotes.
13085 */
13086 int nquotes = 0;
13087 bool safe;
13088 const char *ptr;
13089 char *result;
13090 char *optr;
13091
13092 /*
13093 * would like to use <ctype.h> macros here, but they might yield unwanted
13094 * locale-specific results...
13095 */
13096 safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
13097
13098 for (ptr = ident; *ptr; ptr++)
13099 {
13100 char ch = *ptr;
13101
13102 if ((ch >= 'a' && ch <= 'z') ||
13103 (ch >= '0' && ch <= '9') ||
13104 (ch == '_'))
13105 {
13106 /* okay */
13107 }
13108 else
13109 {
13110 safe = false;
13111 if (ch == '"')
13112 nquotes++;
13113 }
13114 }
13115
13117 safe = false;
13118
13119 if (safe)
13120 {
13121 /*
13122 * Check for keyword. We quote keywords except for unreserved ones.
13123 * (In some cases we could avoid quoting a col_name or type_func_name
13124 * keyword, but it seems much harder than it's worth to tell that.)
13125 *
13126 * Note: ScanKeywordLookup() does case-insensitive comparison, but
13127 * that's fine, since we already know we have all-lower-case.
13128 */
13130
13132 safe = false;
13133 }
13134
13135 if (safe)
13136 return ident; /* no change needed */
13137
13138 result = (char *) palloc(strlen(ident) + nquotes + 2 + 1);
13139
13140 optr = result;
13141 *optr++ = '"';
13142 for (ptr = ident; *ptr; ptr++)
13143 {
13144 char ch = *ptr;
13145
13146 if (ch == '"')
13147 *optr++ = '"';
13148 *optr++ = ch;
13149 }
13150 *optr++ = '"';
13151 *optr = '\0';
13152
13153 return result;
13154}
13155
13156/*
13157 * quote_qualified_identifier - Quote a possibly-qualified identifier
13158 *
13159 * Return a name of the form qualifier.ident, or just ident if qualifier
13160 * is NULL, quoting each component if necessary. The result is palloc'd.
13161 */
13162char *
13164 const char *ident)
13165{
13167
13169 if (qualifier)
13172 return buf.data;
13173}
13174
13175/*
13176 * get_relation_name
13177 * Get the unqualified name of a relation specified by OID
13178 *
13179 * This differs from the underlying get_rel_name() function in that it will
13180 * throw error instead of silently returning NULL if the OID is bad.
13181 */
13182static char *
13184{
13185 char *relname = get_rel_name(relid);
13186
13187 if (!relname)
13188 elog(ERROR, "cache lookup failed for relation %u", relid);
13189 return relname;
13190}
13191
13192/*
13193 * generate_relation_name
13194 * Compute the name to display for a relation specified by OID
13195 *
13196 * The result includes all necessary quoting and schema-prefixing.
13197 *
13198 * If namespaces isn't NIL, it must be a list of deparse_namespace nodes.
13199 * We will forcibly qualify the relation name if it equals any CTE name
13200 * visible in the namespace list.
13201 */
13202static char *
13204{
13205 HeapTuple tp;
13207 bool need_qual;
13209 char *relname;
13210 char *nspname;
13211 char *result;
13212
13214 if (!HeapTupleIsValid(tp))
13215 elog(ERROR, "cache lookup failed for relation %u", relid);
13217 relname = NameStr(reltup->relname);
13218
13219 /* Check for conflicting CTE name */
13220 need_qual = false;
13221 foreach(nslist, namespaces)
13222 {
13225
13226 foreach(ctlist, dpns->ctes)
13227 {
13229
13230 if (strcmp(cte->ctename, relname) == 0)
13231 {
13232 need_qual = true;
13233 break;
13234 }
13235 }
13236 if (need_qual)
13237 break;
13238 }
13239
13240 /* Otherwise, qualify the name if not visible in search path */
13241 if (!need_qual)
13242 need_qual = !RelationIsVisible(relid);
13243
13244 if (need_qual)
13245 nspname = get_namespace_name_or_temp(reltup->relnamespace);
13246 else
13247 nspname = NULL;
13248
13249 result = quote_qualified_identifier(nspname, relname);
13250
13251 ReleaseSysCache(tp);
13252
13253 return result;
13254}
13255
13256/*
13257 * generate_qualified_relation_name
13258 * Compute the name to display for a relation specified by OID
13259 *
13260 * As above, but unconditionally schema-qualify the name.
13261 */
13262static char *
13264{
13265 HeapTuple tp;
13267 char *relname;
13268 char *nspname;
13269 char *result;
13270
13272 if (!HeapTupleIsValid(tp))
13273 elog(ERROR, "cache lookup failed for relation %u", relid);
13275 relname = NameStr(reltup->relname);
13276
13277 nspname = get_namespace_name_or_temp(reltup->relnamespace);
13278 if (!nspname)
13279 elog(ERROR, "cache lookup failed for namespace %u",
13280 reltup->relnamespace);
13281
13282 result = quote_qualified_identifier(nspname, relname);
13283
13284 ReleaseSysCache(tp);
13285
13286 return result;
13287}
13288
13289/*
13290 * generate_function_name
13291 * Compute the name to display for a function specified by OID,
13292 * given that it is being called with the specified actual arg names and
13293 * types. (Those matter because of ambiguous-function resolution rules.)
13294 *
13295 * If we're dealing with a potentially variadic function (in practice, this
13296 * means a FuncExpr or Aggref, not some other way of calling a function), then
13297 * has_variadic must specify whether variadic arguments have been merged,
13298 * and *use_variadic_p will be set to indicate whether to print VARIADIC in
13299 * the output. For non-FuncExpr cases, has_variadic should be false and
13300 * use_variadic_p can be NULL.
13301 *
13302 * inGroupBy must be true if we're deparsing a GROUP BY clause.
13303 *
13304 * The result includes all necessary quoting and schema-prefixing.
13305 */
13306static char *
13307generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes,
13308 bool has_variadic, bool *use_variadic_p,
13309 bool inGroupBy)
13310{
13311 char *result;
13314 char *proname;
13315 bool use_variadic;
13316 char *nspname;
13318 int fgc_flags;
13319 Oid p_funcid;
13320 Oid p_rettype;
13321 bool p_retset;
13322 int p_nvargs;
13323 Oid p_vatype;
13325 bool force_qualify = false;
13326
13329 elog(ERROR, "cache lookup failed for function %u", funcid);
13331 proname = NameStr(procform->proname);
13332
13333 /*
13334 * Due to parser hacks to avoid needing to reserve CUBE, we need to force
13335 * qualification of some function names within GROUP BY.
13336 */
13337 if (inGroupBy)
13338 {
13339 if (strcmp(proname, "cube") == 0 || strcmp(proname, "rollup") == 0)
13340 force_qualify = true;
13341 }
13342
13343 /*
13344 * Determine whether VARIADIC should be printed. We must do this first
13345 * since it affects the lookup rules in func_get_detail().
13346 *
13347 * We always print VARIADIC if the function has a merged variadic-array
13348 * argument. Note that this is always the case for functions taking a
13349 * VARIADIC argument type other than VARIADIC ANY. If we omitted VARIADIC
13350 * and printed the array elements as separate arguments, the call could
13351 * match a newer non-VARIADIC function.
13352 */
13353 if (use_variadic_p)
13354 {
13355 /* Parser should not have set funcvariadic unless fn is variadic */
13356 Assert(!has_variadic || OidIsValid(procform->provariadic));
13359 }
13360 else
13361 {
13363 use_variadic = false;
13364 }
13365
13366 /*
13367 * The idea here is to schema-qualify only if the parser would fail to
13368 * resolve the correct function given the unqualified func name with the
13369 * specified argtypes and VARIADIC flag. But if we already decided to
13370 * force qualification, then we can skip the lookup and pretend we didn't
13371 * find it.
13372 */
13373 if (!force_qualify)
13375 NIL, argnames, nargs, argtypes,
13376 !use_variadic, true, false,
13377 &fgc_flags,
13381 else
13382 {
13385 }
13386
13387 if ((p_result == FUNCDETAIL_NORMAL ||
13390 p_funcid == funcid)
13391 nspname = NULL;
13392 else
13393 nspname = get_namespace_name_or_temp(procform->pronamespace);
13394
13395 result = quote_qualified_identifier(nspname, proname);
13396
13398
13399 return result;
13400}
13401
13402/*
13403 * generate_operator_name
13404 * Compute the name to display for an operator specified by OID,
13405 * given that it is being called with the specified actual arg types.
13406 * (Arg types matter because of ambiguous-operator resolution rules.
13407 * Pass InvalidOid for unused arg of a unary operator.)
13408 *
13409 * The result includes all necessary quoting and schema-prefixing,
13410 * plus the OPERATOR() decoration needed to use a qualified operator name
13411 * in an expression.
13412 */
13413static char *
13415{
13419 char *oprname;
13420 char *nspname;
13422
13424
13427 elog(ERROR, "cache lookup failed for operator %u", operid);
13429 oprname = NameStr(operform->oprname);
13430
13431 /*
13432 * The idea here is to schema-qualify only if the parser would fail to
13433 * resolve the correct operator given the unqualified op name with the
13434 * specified argtypes.
13435 */
13436 switch (operform->oprkind)
13437 {
13438 case 'b':
13440 true, -1);
13441 break;
13442 case 'l':
13444 true, -1);
13445 break;
13446 default:
13447 elog(ERROR, "unrecognized oprkind: %d", operform->oprkind);
13448 p_result = NULL; /* keep compiler quiet */
13449 break;
13450 }
13451
13452 if (p_result != NULL && oprid(p_result) == operid)
13453 nspname = NULL;
13454 else
13455 {
13456 nspname = get_namespace_name_or_temp(operform->oprnamespace);
13457 appendStringInfo(&buf, "OPERATOR(%s.", quote_identifier(nspname));
13458 }
13459
13460 appendStringInfoString(&buf, oprname);
13461
13462 if (nspname)
13464
13465 if (p_result != NULL)
13467
13469
13470 return buf.data;
13471}
13472
13473/*
13474 * generate_operator_clause --- generate a binary-operator WHERE clause
13475 *
13476 * This is used for internally-generated-and-executed SQL queries, where
13477 * precision is essential and readability is secondary. The basic
13478 * requirement is to append "leftop op rightop" to buf, where leftop and
13479 * rightop are given as strings and are assumed to yield types leftoptype
13480 * and rightoptype; the operator is identified by OID. The complexity
13481 * comes from needing to be sure that the parser will select the desired
13482 * operator when the query is parsed. We always name the operator using
13483 * OPERATOR(schema.op) syntax, so as to avoid search-path uncertainties.
13484 * We have to emit casts too, if either input isn't already the input type
13485 * of the operator; else we are at the mercy of the parser's heuristics for
13486 * ambiguous-operator resolution. The caller must ensure that leftop and
13487 * rightop are suitable arguments for a cast operation; it's best to insert
13488 * parentheses if they aren't just variables or parameters.
13489 */
13490void
13492 const char *leftop, Oid leftoptype,
13493 Oid opoid,
13494 const char *rightop, Oid rightoptype)
13495{
13498 char *oprname;
13499 char *nspname;
13500
13503 elog(ERROR, "cache lookup failed for operator %u", opoid);
13505 Assert(operform->oprkind == 'b');
13506 oprname = NameStr(operform->oprname);
13507
13508 nspname = get_namespace_name(operform->oprnamespace);
13509
13511 if (leftoptype != operform->oprleft)
13512 add_cast_to(buf, operform->oprleft);
13513 appendStringInfo(buf, " OPERATOR(%s.", quote_identifier(nspname));
13514 appendStringInfoString(buf, oprname);
13515 appendStringInfo(buf, ") %s", rightop);
13516 if (rightoptype != operform->oprright)
13517 add_cast_to(buf, operform->oprright);
13518
13520}
13521
13522/*
13523 * Add a cast specification to buf. We spell out the type name the hard way,
13524 * intentionally not using format_type_be(). This is to avoid corner cases
13525 * for CHARACTER, BIT, and perhaps other types, where specifying the type
13526 * using SQL-standard syntax results in undesirable data truncation. By
13527 * doing it this way we can be certain that the cast will have default (-1)
13528 * target typmod.
13529 */
13530static void
13532{
13535 char *typname;
13536 char *nspname;
13537
13540 elog(ERROR, "cache lookup failed for type %u", typid);
13542
13543 typname = NameStr(typform->typname);
13544 nspname = get_namespace_name_or_temp(typform->typnamespace);
13545
13546 appendStringInfo(buf, "::%s.%s",
13548
13550}
13551
13552/*
13553 * generate_qualified_type_name
13554 * Compute the name to display for a type specified by OID
13555 *
13556 * This is different from format_type_be() in that we unconditionally
13557 * schema-qualify the name. That also means no special syntax for
13558 * SQL-standard type names ... although in current usage, this should
13559 * only get used for domains, so such cases wouldn't occur anyway.
13560 */
13561static char *
13563{
13564 HeapTuple tp;
13566 char *typname;
13567 char *nspname;
13568 char *result;
13569
13571 if (!HeapTupleIsValid(tp))
13572 elog(ERROR, "cache lookup failed for type %u", typid);
13574 typname = NameStr(typtup->typname);
13575
13576 nspname = get_namespace_name_or_temp(typtup->typnamespace);
13577 if (!nspname)
13578 elog(ERROR, "cache lookup failed for namespace %u",
13579 typtup->typnamespace);
13580
13581 result = quote_qualified_identifier(nspname, typname);
13582
13583 ReleaseSysCache(tp);
13584
13585 return result;
13586}
13587
13588/*
13589 * generate_collation_name
13590 * Compute the name to display for a collation specified by OID
13591 *
13592 * The result includes all necessary quoting and schema-prefixing.
13593 */
13594char *
13596{
13597 HeapTuple tp;
13599 char *collname;
13600 char *nspname;
13601 char *result;
13602
13604 if (!HeapTupleIsValid(tp))
13605 elog(ERROR, "cache lookup failed for collation %u", collid);
13607 collname = NameStr(colltup->collname);
13608
13610 nspname = get_namespace_name_or_temp(colltup->collnamespace);
13611 else
13612 nspname = NULL;
13613
13614 result = quote_qualified_identifier(nspname, collname);
13615
13616 ReleaseSysCache(tp);
13617
13618 return result;
13619}
13620
13621/*
13622 * Given a C string, produce a TEXT datum.
13623 *
13624 * We assume that the input was palloc'd and may be freed.
13625 */
13626static text *
13628{
13629 text *result;
13630
13631 result = cstring_to_text(str);
13632 pfree(str);
13633 return result;
13634}
13635
13636/*
13637 * Generate a C string representing a relation options from text[] datum.
13638 */
13639static void
13641{
13642 Datum *options;
13643 int noptions;
13644 int i;
13645
13647 &options, NULL, &noptions);
13648
13649 for (i = 0; i < noptions; i++)
13650 {
13652 char *name;
13653 char *separator;
13654 char *value;
13655
13656 /*
13657 * Each array element should have the form name=value. If the "=" is
13658 * missing for some reason, treat it like an empty value.
13659 */
13660 name = option;
13661 separator = strchr(option, '=');
13662 if (separator)
13663 {
13664 *separator = '\0';
13665 value = separator + 1;
13666 }
13667 else
13668 value = "";
13669
13670 if (i > 0)
13673
13674 /*
13675 * In general we need to quote the value; but to avoid unnecessary
13676 * clutter, do not quote if it is an identifier that would not need
13677 * quoting. (We could also allow numbers, but that is a bit trickier
13678 * than it looks --- for example, are leading zeroes significant? We
13679 * don't want to assume very much here about what custom reloptions
13680 * might mean.)
13681 */
13684 else
13686
13687 pfree(option);
13688 }
13689}
13690
13691/*
13692 * Generate a C string representing a relation's reloptions, or NULL if none.
13693 */
13694static char *
13696{
13697 char *result = NULL;
13698 HeapTuple tuple;
13699 Datum reloptions;
13700 bool isnull;
13701
13702 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
13703 if (!HeapTupleIsValid(tuple))
13704 elog(ERROR, "cache lookup failed for relation %u", relid);
13705
13706 reloptions = SysCacheGetAttr(RELOID, tuple,
13707 Anum_pg_class_reloptions, &isnull);
13708 if (!isnull)
13709 {
13711
13713 get_reloptions(&buf, reloptions);
13714
13715 result = buf.data;
13716 }
13717
13718 ReleaseSysCache(tuple);
13719
13720 return result;
13721}
13722
13723/*
13724 * get_range_partbound_string
13725 * A C string representation of one range partition bound
13726 */
13727char *
13729{
13730 deparse_context context;
13732 ListCell *cell;
13733 char *sep;
13734
13736 memset(&context, 0, sizeof(deparse_context));
13737 context.buf = &buf;
13738
13740 sep = "";
13741 foreach(cell, bound_datums)
13742 {
13743 PartitionRangeDatum *datum =
13745
13748 appendStringInfoString(&buf, "MINVALUE");
13749 else if (datum->kind == PARTITION_RANGE_DATUM_MAXVALUE)
13750 appendStringInfoString(&buf, "MAXVALUE");
13751 else
13752 {
13753 Const *val = castNode(Const, datum->value);
13754
13755 get_const_expr(val, &context, -1);
13756 }
13757 sep = ", ";
13758 }
13760
13761 return buf.data;
13762}
const IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition amapi.c:33
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_DATA_PTR(a)
Definition array.h:322
#define DatumGetArrayTypeP(X)
Definition array.h:261
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_DIMS(a)
Definition array.h:294
#define ARR_HASNULL(a)
Definition array.h:291
#define ARR_LBOUND(a)
Definition array.h:296
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Datum array_ref(ArrayType *array, int nSubscripts, int *indx, int arraytyplen, int elmlen, bool elmbyval, char elmalign, bool *isNull)
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
char * get_tablespace_name(Oid spc_oid)
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:412
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
#define bms_is_empty(a)
Definition bitmapset.h:118
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:777
uint16 bits16
Definition c.h:566
NameData * Name
Definition c.h:775
#define Max(x, y)
Definition c.h:1013
#define Assert(condition)
Definition c.h:885
int16_t int16
Definition c.h:553
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition c.h:1180
int32_t int32
Definition c.h:554
#define lengthof(array)
Definition c.h:815
unsigned int Index
Definition c.h:640
float float4
Definition c.h:655
#define pg_fallthrough
Definition c.h:144
#define OidIsValid(objectId)
Definition c.h:800
Oid collid
const uint8 ScanKeywordCategories[SCANKEYWORDS_NUM_KEYWORDS]
Definition keywords.c:29
@ DEPENDENCY_AUTO
Definition dependency.h:34
@ DEPENDENCY_INTERNAL
Definition dependency.h:35
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
void hash_destroy(HTAB *hashp)
Definition dynahash.c:865
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition fmgr.c:1763
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define DatumGetByteaPP(X)
Definition fmgr.h:292
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_RETURN_NAME(x)
Definition fmgr.h:365
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
char * format_type_with_typemod(Oid type_oid, int32 typemod)
char * format_type_be(Oid type_oid)
int get_func_trftypes(HeapTuple procTup, Oid **p_trftypes)
Definition funcapi.c:1475
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition funcapi.c:1379
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition funcapi.c:551
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
int GetConfigOptionFlags(const char *name, bool missing_ok)
Definition guc.c:4316
#define GUC_LIST_QUOTE
Definition guc.h:215
const char * str
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition heaptuple.c:456
#define HASH_STRINGS
Definition hsearch.h:96
@ HASH_FIND
Definition hsearch.h:113
@ HASH_ENTER
Definition hsearch.h:114
#define HASH_CONTEXT
Definition hsearch.h:102
#define HASH_ELEM
Definition hsearch.h:95
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#define stmt
#define ident
#define funcname
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition indexcmds.c:2368
long val
Definition informix.c:689
static struct @174 value
static char * encoding
Definition initdb.c:139
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
PGDLLIMPORT const ScanKeywordList ScanKeywords
#define UNRESERVED_KEYWORD
Definition keywords.h:20
int ScanKeywordLookup(const char *str, const ScanKeywordList *keywords)
Definition kwlookup.c:38
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_copy_tail(const List *oldlist, int nskip)
Definition list.c:1613
List * list_delete_first(List *list)
Definition list.c:943
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * list_copy(const List *oldlist)
Definition list.c:1573
List * lcons(void *datum, List *list)
Definition list.c:495
void list_free(List *list)
Definition list.c:1546
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
@ LockWaitSkip
Definition lockoptions.h:42
@ LockWaitError
Definition lockoptions.h:44
LockClauseStrength
Definition lockoptions.h:22
@ LCS_FORUPDATE
Definition lockoptions.h:28
@ LCS_NONE
Definition lockoptions.h:23
@ LCS_FORSHARE
Definition lockoptions.h:26
@ LCS_FORKEYSHARE
Definition lockoptions.h:25
@ LCS_FORNOKEYUPDATE
Definition lockoptions.h:27
char * get_rel_name(Oid relid)
Definition lsyscache.c:2078
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:934
Oid get_opclass_input_type(Oid opclass)
Definition lsyscache.c:1314
bool type_is_rowtype(Oid typid)
Definition lsyscache.c:2807
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3059
Datum get_attoptions(Oid relid, int16 attnum)
Definition lsyscache.c:1046
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2153
Oid get_typcollation(Oid typid)
Definition lsyscache.c:3208
char * get_language_name(Oid langoid, bool missing_ok)
Definition lsyscache.c:1263
char * get_namespace_name_or_temp(Oid nspid)
Definition lsyscache.c:3542
char * get_constraint_name(Oid conoid)
Definition lsyscache.c:1157
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:903
Oid get_rel_tablespace(Oid relid)
Definition lsyscache.c:2204
Oid get_typ_typrelid(Oid typid)
Definition lsyscache.c:2883
Oid get_base_element_type(Oid typid)
Definition lsyscache.c:2984
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3518
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition lsyscache.c:2862
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition lsyscache.c:1019
Alias * makeAlias(const char *aliasname, List *colnames)
Definition makefuncs.c:438
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition mbutils.c:1211
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
Datum namein(PG_FUNCTION_ARGS)
Definition name.c:48
bool CollationIsVisible(Oid collid)
Definition namespace.c:2476
bool RelationIsVisible(Oid relid)
Definition namespace.c:914
bool OpclassIsVisible(Oid opcid)
Definition namespace.c:2223
RangeVar * makeRangeVarFromNameList(const List *names)
Definition namespace.c:3626
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition namespace.h:98
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition nodeFuncs.c:557
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:301
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:821
Node * strip_implicit_coercions(Node *node)
Definition nodeFuncs.c:705
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition nodes.h:396
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define nodeTag(nodeptr)
Definition nodes.h:139
#define DO_AGGSPLIT_COMBINE(as)
Definition nodes.h:395
@ ONCONFLICT_SELECT
Definition nodes.h:431
@ ONCONFLICT_UPDATE
Definition nodes.h:430
@ ONCONFLICT_NOTHING
Definition nodes.h:429
@ CMD_MERGE
Definition nodes.h:279
@ CMD_UTILITY
Definition nodes.h:280
@ CMD_INSERT
Definition nodes.h:277
@ CMD_DELETE
Definition nodes.h:278
@ CMD_UPDATE
Definition nodes.h:276
@ CMD_SELECT
Definition nodes.h:275
@ CMD_NOTHING
Definition nodes.h:282
@ LIMIT_OPTION_WITH_TIES
Definition nodes.h:443
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_RIGHT
Definition nodes.h:306
@ JOIN_LEFT
Definition nodes.h:304
#define repalloc0_array(pointer, type, oldcount, count)
Definition palloc.h:109
int get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
Definition parse_agg.c:2052
FuncDetailCode func_get_detail(List *funcname, List *fargs, List *fargnames, int nargs, Oid *argtypes, bool expand_variadic, bool expand_defaults, bool include_out_arguments, int *fgc_flags, Oid *funcid, Oid *rettype, bool *retset, int *nvargs, Oid *vatype, Oid **true_typeids, List **argdefaults)
FuncDetailCode
Definition parse_func.h:23
@ FUNCDETAIL_NORMAL
Definition parse_func.h:26
@ FUNCDETAIL_WINDOWFUNC
Definition parse_func.h:29
@ FUNCDETAIL_NOTFOUND
Definition parse_func.h:24
@ FUNCDETAIL_AGGREGATE
Definition parse_func.h:28
Operator left_oper(ParseState *pstate, List *op, Oid arg, bool noError, int location)
Definition parse_oper.c:522
Oid oprid(Operator op)
Definition parse_oper.c:240
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition parse_oper.c:372
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
#define FRAMEOPTION_END_CURRENT_ROW
Definition parsenodes.h:619
#define FRAMEOPTION_END_OFFSET
Definition parsenodes.h:630
#define FRAMEOPTION_EXCLUDE_CURRENT_ROW
Definition parsenodes.h:624
@ GROUPING_SET_CUBE
@ GROUPING_SET_SIMPLE
@ GROUPING_SET_ROLLUP
@ GROUPING_SET_SETS
@ GROUPING_SET_EMPTY
#define FKCONSTR_ACTION_RESTRICT
@ SETOP_INTERSECT
@ SETOP_UNION
@ SETOP_EXCEPT
#define FRAMEOPTION_END_OFFSET_PRECEDING
Definition parsenodes.h:621
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition parsenodes.h:614
#define GetCTETargetList(cte)
#define FKCONSTR_ACTION_SETDEFAULT
@ PARTITION_STRATEGY_HASH
Definition parsenodes.h:903
@ PARTITION_STRATEGY_LIST
Definition parsenodes.h:901
@ PARTITION_STRATEGY_RANGE
Definition parsenodes.h:902
#define FRAMEOPTION_START_CURRENT_ROW
Definition parsenodes.h:618
#define FKCONSTR_MATCH_SIMPLE
@ RTE_JOIN
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_GROUP
@ RTE_RELATION
#define FRAMEOPTION_START_OFFSET
Definition parsenodes.h:628
@ PARTITION_RANGE_DATUM_MAXVALUE
Definition parsenodes.h:955
@ PARTITION_RANGE_DATUM_MINVALUE
Definition parsenodes.h:953
#define FKCONSTR_MATCH_PARTIAL
#define FRAMEOPTION_END_OFFSET_FOLLOWING
Definition parsenodes.h:623
#define FRAMEOPTION_EXCLUDE_TIES
Definition parsenodes.h:626
#define FRAMEOPTION_RANGE
Definition parsenodes.h:610
#define FRAMEOPTION_EXCLUDE_GROUP
Definition parsenodes.h:625
#define FKCONSTR_ACTION_CASCADE
#define FRAMEOPTION_GROUPS
Definition parsenodes.h:612
#define FRAMEOPTION_BETWEEN
Definition parsenodes.h:613
#define FKCONSTR_ACTION_SETNULL
#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING
Definition parsenodes.h:617
#define FRAMEOPTION_START_OFFSET_PRECEDING
Definition parsenodes.h:620
#define FRAMEOPTION_START_OFFSET_FOLLOWING
Definition parsenodes.h:622
#define FRAMEOPTION_NONDEFAULT
Definition parsenodes.h:609
#define FKCONSTR_MATCH_FULL
#define FKCONSTR_ACTION_NOACTION
#define FRAMEOPTION_ROWS
Definition parsenodes.h:611
@ CTEMaterializeNever
@ CTEMaterializeAlways
@ CTEMaterializeDefault
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
Expr * get_partition_qual_relid(Oid relid)
Definition partcache.c:299
END_CATALOG_STRUCT typedef FormData_pg_aggregate * Form_pg_aggregate
END_CATALOG_STRUCT typedef FormData_pg_am * Form_pg_am
Definition pg_am.h:52
NameData attname
int16 attnum
FormData_pg_attribute * Form_pg_attribute
END_CATALOG_STRUCT typedef FormData_pg_authid * Form_pg_authid
Definition pg_authid.h:60
static char format
NameData relname
Definition pg_class.h:40
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
END_CATALOG_STRUCT typedef FormData_pg_collation * Form_pg_collation
#define NAMEDATALEN
#define FUNC_MAX_ARGS
AttrNumber extractNotNullColumn(HeapTuple constrTup)
END_CATALOG_STRUCT typedef FormData_pg_constraint * Form_pg_constraint
END_CATALOG_STRUCT typedef FormData_pg_depend * Form_pg_depend
Definition pg_depend.h:76
END_CATALOG_STRUCT typedef FormData_pg_index * Form_pg_index
Definition pg_index.h:74
#define lfirst(lc)
Definition pg_list.h:172
#define llast(l)
Definition pg_list.h:198
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
#define NIL
Definition pg_list.h:68
#define lsecond_node(type, l)
Definition pg_list.h:186
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:518
#define foreach_current_index(var_or_cell)
Definition pg_list.h:403
#define lfirst_int(lc)
Definition pg_list.h:173
#define lthird(l)
Definition pg_list.h:188
#define list_make1(x1)
Definition pg_list.h:212
#define linitial_int(l)
Definition pg_list.h:179
#define for_each_cell(cell, lst, initcell)
Definition pg_list.h:438
#define for_each_from(cell, lst, N)
Definition pg_list.h:414
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
#define foreach_node(type, var, lst)
Definition pg_list.h:496
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
Definition pg_list.h:575
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
#define lfourth(l)
Definition pg_list.h:193
#define linitial_oid(l)
Definition pg_list.h:180
#define forfive(cell1, list1, cell2, list2, cell3, list3, cell4, list4, cell5, list5)
Definition pg_list.h:588
#define lfirst_oid(lc)
Definition pg_list.h:174
#define list_make2(x1, x2)
Definition pg_list.h:214
#define foreach_int(var, lst)
Definition pg_list.h:470
static int list_cell_number(const List *l, const ListCell *c)
Definition pg_list.h:333
#define lthird_node(type, l)
Definition pg_list.h:191
END_CATALOG_STRUCT typedef FormData_pg_opclass * Form_pg_opclass
Definition pg_opclass.h:87
END_CATALOG_STRUCT typedef FormData_pg_operator * Form_pg_operator
Definition pg_operator.h:87
END_CATALOG_STRUCT typedef FormData_pg_partitioned_table * Form_pg_partitioned_table
END_CATALOG_STRUCT typedef FormData_pg_proc * Form_pg_proc
Definition pg_proc.h:140
NameData proname
Definition pg_proc.h:37
static size_t noptions
#define plan(x)
Definition pg_regress.c:161
END_CATALOG_STRUCT typedef FormData_pg_statistic_ext * Form_pg_statistic_ext
static char buf[DEFAULT_XLOG_SEG_SIZE]
END_CATALOG_STRUCT typedef FormData_pg_trigger * Form_pg_trigger
Definition pg_trigger.h:84
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
NameData typname
Definition pg_type.h:43
#define innerPlan(node)
Definition plannodes.h:266
#define outerPlan(node)
Definition plannodes.h:267
#define sprintf
Definition port.h:262
#define snprintf
Definition port.h:260
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Name DatumGetName(Datum X)
Definition postgres.h:390
static Oid DatumGetObjectId(Datum X)
Definition postgres.h:252
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static char DatumGetChar(Datum X)
Definition postgres.h:122
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static int16 DatumGetInt16(Datum X)
Definition postgres.h:172
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
#define InvalidOid
unsigned int Oid
e
static int fb(int x)
char string[11]
@ IS_NOT_TRUE
Definition primnodes.h:2002
@ IS_NOT_FALSE
Definition primnodes.h:2002
@ IS_NOT_UNKNOWN
Definition primnodes.h:2002
@ IS_TRUE
Definition primnodes.h:2002
@ IS_UNKNOWN
Definition primnodes.h:2002
@ IS_FALSE
Definition primnodes.h:2002
@ ARRAY_SUBLINK
Definition primnodes.h:1036
@ ANY_SUBLINK
Definition primnodes.h:1032
@ MULTIEXPR_SUBLINK
Definition primnodes.h:1035
@ CTE_SUBLINK
Definition primnodes.h:1037
@ EXPR_SUBLINK
Definition primnodes.h:1034
@ ROWCOMPARE_SUBLINK
Definition primnodes.h:1033
@ ALL_SUBLINK
Definition primnodes.h:1031
@ EXISTS_SUBLINK
Definition primnodes.h:1030
@ JS_FORMAT_JSONB
Definition primnodes.h:1666
@ JS_FORMAT_DEFAULT
Definition primnodes.h:1664
@ JS_FORMAT_JSON
Definition primnodes.h:1665
@ IS_LEAST
Definition primnodes.h:1529
@ IS_GREATEST
Definition primnodes.h:1528
@ TFT_XMLTABLE
Definition primnodes.h:101
@ TFT_JSON_TABLE
Definition primnodes.h:102
BoolExprType
Definition primnodes.h:963
@ AND_EXPR
Definition primnodes.h:964
@ OR_EXPR
Definition primnodes.h:964
@ NOT_EXPR
Definition primnodes.h:964
@ JS_ENC_DEFAULT
Definition primnodes.h:1652
@ JS_ENC_UTF32
Definition primnodes.h:1655
@ JS_ENC_UTF16
Definition primnodes.h:1654
@ XMLOPTION_DOCUMENT
Definition primnodes.h:1618
@ SVFOP_CURRENT_CATALOG
Definition primnodes.h:1575
@ SVFOP_LOCALTIME_N
Definition primnodes.h:1568
@ SVFOP_CURRENT_TIMESTAMP
Definition primnodes.h:1565
@ SVFOP_LOCALTIME
Definition primnodes.h:1567
@ SVFOP_CURRENT_TIMESTAMP_N
Definition primnodes.h:1566
@ SVFOP_CURRENT_ROLE
Definition primnodes.h:1571
@ SVFOP_USER
Definition primnodes.h:1573
@ SVFOP_CURRENT_SCHEMA
Definition primnodes.h:1576
@ SVFOP_LOCALTIMESTAMP_N
Definition primnodes.h:1570
@ SVFOP_CURRENT_DATE
Definition primnodes.h:1562
@ SVFOP_CURRENT_TIME_N
Definition primnodes.h:1564
@ SVFOP_CURRENT_TIME
Definition primnodes.h:1563
@ SVFOP_LOCALTIMESTAMP
Definition primnodes.h:1569
@ SVFOP_CURRENT_USER
Definition primnodes.h:1572
@ SVFOP_SESSION_USER
Definition primnodes.h:1574
@ PARAM_MULTIEXPR
Definition primnodes.h:388
@ PARAM_EXTERN
Definition primnodes.h:385
@ PARAM_EXEC
Definition primnodes.h:386
@ JSW_UNCONDITIONAL
Definition primnodes.h:1779
@ JSW_CONDITIONAL
Definition primnodes.h:1778
@ JSW_UNSPEC
Definition primnodes.h:1776
@ JSW_NONE
Definition primnodes.h:1777
#define PARSER_IGNORE_NULLS
Definition primnodes.h:590
@ IS_DOCUMENT
Definition primnodes.h:1613
@ IS_XMLFOREST
Definition primnodes.h:1608
@ IS_XMLCONCAT
Definition primnodes.h:1606
@ IS_XMLPI
Definition primnodes.h:1610
@ IS_XMLPARSE
Definition primnodes.h:1609
@ IS_XMLSERIALIZE
Definition primnodes.h:1612
@ IS_XMLROOT
Definition primnodes.h:1611
@ IS_XMLELEMENT
Definition primnodes.h:1607
@ VAR_RETURNING_OLD
Definition primnodes.h:258
@ VAR_RETURNING_NEW
Definition primnodes.h:259
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257
JsonBehaviorType
Definition primnodes.h:1790
@ JSON_BEHAVIOR_DEFAULT
Definition primnodes.h:1799
@ JSON_BEHAVIOR_FALSE
Definition primnodes.h:1795
@ JSON_BEHAVIOR_NULL
Definition primnodes.h:1791
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition primnodes.h:1797
@ JSON_QUERY_OP
Definition primnodes.h:1829
@ JSON_EXISTS_OP
Definition primnodes.h:1828
@ JSON_VALUE_OP
Definition primnodes.h:1830
CoercionForm
Definition primnodes.h:766
@ COERCE_SQL_SYNTAX
Definition primnodes.h:770
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCE_EXPLICIT_CAST
Definition primnodes.h:768
@ COERCE_EXPLICIT_CALL
Definition primnodes.h:767
@ OVERRIDING_SYSTEM_VALUE
Definition primnodes.h:31
@ OVERRIDING_USER_VALUE
Definition primnodes.h:30
@ IS_NULL
Definition primnodes.h:1978
@ IS_NOT_NULL
Definition primnodes.h:1978
@ JS_TYPE_ARRAY
Definition primnodes.h:1750
@ JS_TYPE_OBJECT
Definition primnodes.h:1749
@ JS_TYPE_SCALAR
Definition primnodes.h:1751
@ MERGE_WHEN_NOT_MATCHED_BY_TARGET
Definition primnodes.h:2024
@ MERGE_WHEN_NOT_MATCHED_BY_SOURCE
Definition primnodes.h:2023
@ MERGE_WHEN_MATCHED
Definition primnodes.h:2022
#define OUTER_VAR
Definition primnodes.h:244
@ JSCTOR_JSON_SERIALIZE
Definition primnodes.h:1722
@ JSCTOR_JSON_ARRAYAGG
Definition primnodes.h:1719
@ JSCTOR_JSON_PARSE
Definition primnodes.h:1720
@ JSCTOR_JSON_OBJECT
Definition primnodes.h:1716
@ JSCTOR_JSON_SCALAR
Definition primnodes.h:1721
@ JSCTOR_JSON_ARRAY
Definition primnodes.h:1717
@ JSCTOR_JSON_OBJECTAGG
Definition primnodes.h:1718
#define INNER_VAR
Definition primnodes.h:243
#define INDEX_VAR
Definition primnodes.h:245
tree ctl root
Definition radixtree.h:1857
void * stringToNode(const char *str)
Definition read.c:90
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetRelationName(relation)
Definition rel.h:548
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
#define ViewSelectRuleName
Datum pg_get_triggerdef_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:886
static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags, int wrapColumn)
Definition ruleutils.c:5543
static void removeStringInfoSpaces(StringInfo str)
Definition ruleutils.c:9168
static bool looks_like_function(Node *node)
Datum pg_get_partition_constraintdef(PG_FUNCTION_ARGS)
Definition ruleutils.c:2096
static char * get_rtable_name(int rtindex, deparse_context *context)
Definition ruleutils.c:5137
Datum pg_get_viewdef_wrap(PG_FUNCTION_ARGS)
Definition ruleutils.c:717
static int decompile_column_index_array(Datum column_index_array, Oid relId, bool withPeriod, StringInfo buf)
Definition ruleutils.c:2621
static void set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo)
Definition ruleutils.c:4379
static void appendContextKeyword(deparse_context *context, const char *str, int indentBefore, int indentAfter, int indentPlus)
Definition ruleutils.c:9114
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition ruleutils.c:3757
char * pg_get_statisticsobjdef_string(Oid statextid)
Definition ruleutils.c:1627
Datum pg_get_indexdef_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:1199
static void set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing)
Definition ruleutils.c:4214
static Plan * find_recursive_union(deparse_namespace *dpns, WorkTableScan *wtscan)
Definition ruleutils.c:5237
static text * string_to_text(char *str)
static void get_values_def(List *values_lists, deparse_context *context)
Definition ruleutils.c:5728
#define PRETTYINDENT_LIMIT
Definition ruleutils.c:85
Datum pg_get_viewdef(PG_FUNCTION_ARGS)
Definition ruleutils.c:679
static char * make_colname_unique(char *colname, deparse_namespace *dpns, deparse_columns *colinfo)
Definition ruleutils.c:4927
static void get_json_behavior(JsonBehavior *behavior, deparse_context *context, const char *on)
Definition ruleutils.c:9205
static const char * get_simple_binary_op_name(OpExpr *expr)
Definition ruleutils.c:8856
static void get_json_agg_constructor(JsonConstructorExpr *ctor, deparse_context *context, const char *funcname, bool is_json_objectagg)
Datum pg_get_constraintdef(PG_FUNCTION_ARGS)
Definition ruleutils.c:2146
static void set_deparse_for_query(deparse_namespace *dpns, Query *query, List *parent_namespaces)
Definition ruleutils.c:4033
static void print_function_trftypes(StringInfo buf, HeapTuple proctup)
Definition ruleutils.c:3462
void(* rsv_callback)(Node *node, deparse_context *context, void *callback_arg)
Definition ruleutils.c:325
#define PRETTYINDENT_STD
Definition ruleutils.c:81
char * quote_qualified_identifier(const char *qualifier, const char *ident)
static void get_setop_query(Node *setOp, Query *query, deparse_context *context)
Definition ruleutils.c:6421
#define PRETTYINDENT_JOIN
Definition ruleutils.c:82
static bool is_input_argument(int nth, const char *argmodes)
Definition ruleutils.c:3450
static void get_query_def(Query *query, StringInfo buf, List *parentnamespace, TupleDesc resultDesc, bool colNamesVisible, int prettyFlags, int wrapColumn, int startIndent)
Definition ruleutils.c:5628
static void get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
Datum pg_get_functiondef(PG_FUNCTION_ARGS)
Definition ruleutils.c:2927
Datum pg_get_function_result(PG_FUNCTION_ARGS)
Definition ruleutils.c:3234
Datum pg_get_indexdef(PG_FUNCTION_ARGS)
Definition ruleutils.c:1179
static void get_sublink_expr(SubLink *sublink, deparse_context *context)
static const char * get_name_for_var_field(Var *var, int fieldno, int levelsup, deparse_context *context)
Definition ruleutils.c:8041
static char * get_lock_clause_strength(LockClauseStrength strength)
Definition ruleutils.c:6017
static void get_rte_alias(RangeTblEntry *rte, int varno, bool use_as, deparse_context *context)
#define only_marker(rte)
Definition ruleutils.c:551
Datum pg_get_function_arg_default(PG_FUNCTION_ARGS)
Definition ruleutils.c:3490
static void get_parameter(Param *param, deparse_context *context)
Definition ruleutils.c:8711
static void build_colinfo_names_hash(deparse_columns *colinfo)
Definition ruleutils.c:4982
Datum pg_get_statisticsobjdef_expressions(PG_FUNCTION_ARGS)
Definition ruleutils.c:1838
static void get_delete_query_def(Query *query, deparse_context *context)
Definition ruleutils.c:7379
Datum pg_get_ruledef(PG_FUNCTION_ARGS)
Definition ruleutils.c:561
static void get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan, deparse_context *context, bool showimplicit)
#define PRETTYFLAG_INDENT
Definition ruleutils.c:89
static void get_column_alias_list(deparse_columns *colinfo, deparse_context *context)
Datum pg_get_statisticsobjdef_columns(PG_FUNCTION_ARGS)
Definition ruleutils.c:1637
Datum pg_get_statisticsobjdef(PG_FUNCTION_ARGS)
Definition ruleutils.c:1609
static void add_to_names_hash(deparse_columns *colinfo, const char *name)
Definition ruleutils.c:5040
static void simple_quote_literal(StringInfo buf, const char *val)
static bool colname_is_unique(const char *colname, deparse_namespace *dpns, deparse_columns *colinfo)
Definition ruleutils.c:4852
static void get_from_clause_coldeflist(RangeTblFunction *rtfunc, deparse_columns *colinfo, deparse_context *context)
char * pg_get_partkeydef_columns(Oid relid, bool pretty)
Definition ruleutils.c:1924
static void get_from_clause(Query *query, const char *prefix, deparse_context *context)
List * deparse_context_for(const char *aliasname, Oid relid)
Definition ruleutils.c:3712
static bool get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
Datum pg_get_partkeydef(PG_FUNCTION_ARGS)
Definition ruleutils.c:1909
static char * generate_qualified_relation_name(Oid relid)
static void set_simple_column_names(deparse_namespace *dpns)
Definition ruleutils.c:4102
static void get_json_expr_options(JsonExpr *jsexpr, deparse_context *context, JsonBehaviorType default_behavior)
Definition ruleutils.c:9243
char * pg_get_indexdef_columns(Oid indexrelid, bool pretty)
Definition ruleutils.c:1236
#define PRETTY_INDENT(context)
Definition ruleutils.c:102
#define PRETTYFLAG_PAREN
Definition ruleutils.c:88
static void get_rule_groupingset(GroupingSet *gset, List *targetlist, bool omit_parens, deparse_context *context)
Definition ruleutils.c:6638
char * pg_get_indexdef_columns_extended(Oid indexrelid, bits16 flags)
Definition ruleutils.c:1250
static char * pg_get_indexdef_worker(Oid indexrelid, int colno, const Oid *excludeOps, bool attrsOnly, bool keysOnly, bool showTblSpc, bool inherits, int prettyFlags, bool missing_ok)
Definition ruleutils.c:1271
static char * pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags, bool missing_ok)
Definition ruleutils.c:2193
static void get_rule_expr_funccall(Node *node, deparse_context *context, bool showimplicit)
static char * generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes, bool has_variadic, bool *use_variadic_p, bool inGroupBy)
static void expand_colnames_array_to(deparse_columns *colinfo, int n)
Definition ruleutils.c:4966
static void get_returning_clause(Query *query, deparse_context *context)
Definition ruleutils.c:6382
List * set_deparse_context_plan(List *dpcontext, Plan *plan, List *ancestors)
Definition ruleutils.c:3829
static SubPlan * find_param_generator(Param *param, deparse_context *context, int *column_p)
Definition ruleutils.c:8593
static void add_cast_to(StringInfo buf, Oid typid)
static void get_special_variable(Node *node, deparse_context *context, void *callback_arg)
Definition ruleutils.c:7912
static void get_windowfunc_expr_helper(WindowFunc *wfunc, deparse_context *context, const char *funcname, const char *options, bool is_json_objectagg)
static void get_rule_list_toplevel(List *lst, deparse_context *context, bool showimplicit)
static char * pg_get_statisticsobj_worker(Oid statextid, bool columns_only, bool missing_ok)
Definition ruleutils.c:1654
#define deparse_columns_fetch(rangetable_index, dpns)
Definition ruleutils.c:312
static void get_json_constructor(JsonConstructorExpr *ctor, deparse_context *context, bool showimplicit)
static const char *const query_getrulebyoid
Definition ruleutils.c:334
static void get_json_path_spec(Node *path_spec, deparse_context *context, bool showimplicit)
static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context)
static SubPlan * find_param_generator_initplan(Param *param, Plan *plan, int *column_p)
Definition ruleutils.c:8690
static void pop_ancestor_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
Definition ruleutils.c:5335
static void get_window_frame_options(int frameOptions, Node *startOffset, Node *endOffset, deparse_context *context)
Definition ruleutils.c:6845
static void get_rule_expr_paren(Node *node, deparse_context *context, bool showimplicit, Node *parentNode)
Definition ruleutils.c:9187
bool quote_all_identifiers
Definition ruleutils.c:339
static void get_agg_expr(Aggref *aggref, deparse_context *context, Aggref *original_aggref)
static void get_const_collation(Const *constval, deparse_context *context)
static void get_target_list(List *targetList, deparse_context *context)
Definition ruleutils.c:6246
static SPIPlanPtr plan_getrulebyoid
Definition ruleutils.c:333
static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan, deparse_context *context, bool showimplicit, bool needcomma)
static char * deparse_expression_pretty(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit, int prettyFlags, int startIndent)
Definition ruleutils.c:3676
Datum pg_get_ruledef_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:579
char * pg_get_indexdef_string(Oid indexrelid)
Definition ruleutils.c:1226
static void get_insert_query_def(Query *query, deparse_context *context)
Definition ruleutils.c:6946
char * pg_get_querydef(Query *query, bool pretty)
Definition ruleutils.c:1589
static void print_function_sqlbody(StringInfo buf, HeapTuple proctup)
Definition ruleutils.c:3560
static bool has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode)
Definition ruleutils.c:4144
const char * quote_identifier(const char *ident)
static Node * get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno, deparse_context *context)
Definition ruleutils.c:6569
static char * pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
Definition ruleutils.c:790
static void push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell, deparse_namespace *save_dpns)
Definition ruleutils.c:5314
static SPIPlanPtr plan_getviewrule
Definition ruleutils.c:335
#define WRAP_COLUMN_DEFAULT
Definition ruleutils.c:98
static char * flatten_reloptions(Oid relid)
static text * pg_get_expr_worker(text *expr, Oid relid, int prettyFlags)
Definition ruleutils.c:2710
static Node * processIndirection(Node *node, deparse_context *context)
static void get_agg_combine_expr(Node *node, deparse_context *context, void *callback_arg)
#define PRETTY_PAREN(context)
Definition ruleutils.c:101
Datum pg_get_triggerdef(PG_FUNCTION_ARGS)
Definition ruleutils.c:872
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition ruleutils.c:3859
Datum pg_get_function_sqlbody(PG_FUNCTION_ARGS)
Definition ruleutils.c:3614
Datum pg_get_expr(PG_FUNCTION_ARGS)
Definition ruleutils.c:2675
static char * generate_qualified_type_name(Oid typid)
static void get_xmltable(TableFunc *tf, deparse_context *context, bool showimplicit)
static void get_utility_query_def(Query *query, deparse_context *context)
Definition ruleutils.c:7585
static char * get_relation_name(Oid relid)
Datum pg_get_expr_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:2692
static void get_rule_windowclause(Query *query, deparse_context *context)
Definition ruleutils.c:6756
static void get_rule_windowspec(WindowClause *wc, List *targetList, deparse_context *context)
Definition ruleutils.c:6788
static void get_json_returning(JsonReturning *returning, StringInfo buf, bool json_format_by_default)
Datum pg_get_viewdef_name_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:762
static Node * find_param_referent(Param *param, deparse_context *context, deparse_namespace **dpns_p, ListCell **ancestor_cell_p)
Definition ruleutils.c:8479
static void get_rule_orderby(List *orderList, List *targetList, bool force_colno, deparse_context *context)
Definition ruleutils.c:6698
static void pop_child_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
Definition ruleutils.c:5284
char * generate_collation_name(Oid collid)
char * pg_get_constraintdef_command(Oid constraintId)
Definition ruleutils.c:2184
char * pg_get_partconstrdef_string(Oid partitionId, char *aliasname)
Definition ruleutils.c:2128
static void set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo)
Definition ruleutils.c:4582
Datum pg_get_constraintdef_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:2163
static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces, Bitmapset *rels_used)
Definition ruleutils.c:3888
char * get_window_frame_options_for_explain(int frameOptions, Node *startOffset, Node *endOffset, List *dpcontext, bool forceprefix)
Definition ruleutils.c:6914
static void get_update_query_targetlist_def(Query *query, List *targetList, deparse_context *context, RangeTblEntry *rte)
Definition ruleutils.c:7227
static void get_rule_expr(Node *node, deparse_context *context, bool showimplicit)
Definition ruleutils.c:9284
Datum pg_get_viewdef_ext(PG_FUNCTION_ARGS)
Definition ruleutils.c:698
static char * pg_get_partkeydef_worker(Oid relid, int prettyFlags, bool attrsOnly, bool missing_ok)
Definition ruleutils.c:1937
static void get_oper_expr(OpExpr *expr, deparse_context *context)
Datum pg_get_function_identity_arguments(PG_FUNCTION_ARGS)
Definition ruleutils.c:3209
static char * pg_get_triggerdef_worker(Oid trigid, bool pretty)
Definition ruleutils.c:901
#define GET_PRETTY_FLAGS(pretty)
Definition ruleutils.c:93
static void get_reloptions(StringInfo buf, Datum reloptions)
static void get_func_expr(FuncExpr *expr, deparse_context *context, bool showimplicit)
static void get_const_expr(Const *constval, deparse_context *context, int showtype)
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition ruleutils.c:3649
void generate_operator_clause(StringInfo buf, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags)
Definition ruleutils.c:5351
static const char *const query_getviewrule
Definition ruleutils.c:336
static char * pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
Definition ruleutils.c:598
static int print_function_arguments(StringInfo buf, HeapTuple proctup, bool print_table_args, bool print_defaults)
Definition ruleutils.c:3302
static void identify_join_columns(JoinExpr *j, RangeTblEntry *jrte, deparse_columns *colinfo)
Definition ruleutils.c:5069
static void print_function_rettype(StringInfo buf, HeapTuple proctup)
Definition ruleutils.c:3264
char * generate_opclass_name(Oid opclass)
static void set_deparse_plan(deparse_namespace *dpns, Plan *plan)
Definition ruleutils.c:5156
static void get_merge_query_def(Query *query, deparse_context *context)
Definition ruleutils.c:7426
static void resolve_special_varno(Node *node, deparse_context *context, rsv_callback callback, void *callback_arg)
Definition ruleutils.c:7933
static void get_json_format(JsonFormat *format, StringInfo buf)
char * get_range_partbound_string(List *bound_datums)
static void get_tablefunc(TableFunc *tf, deparse_context *context, bool showimplicit)
static void get_rule_expr_toplevel(Node *node, deparse_context *context, bool showimplicit)
static RangeTblEntry * get_simple_values_rte(Query *query, TupleDesc resultDesc)
Definition ruleutils.c:6044
static void get_coercion_expr(Node *arg, deparse_context *context, Oid resulttype, int32 resulttypmod, Node *parentNode)
static void push_child_plan(deparse_namespace *dpns, Plan *plan, deparse_namespace *save_dpns)
Definition ruleutils.c:5267
static void get_update_query_def(Query *query, deparse_context *context)
Definition ruleutils.c:7175
static void get_json_constructor_options(JsonConstructorExpr *ctor, StringInfo buf)
static void get_basic_select_query(Query *query, deparse_context *context)
Definition ruleutils.c:6113
static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
Definition ruleutils.c:8882
static void get_with_clause(Query *query, deparse_context *context)
Definition ruleutils.c:5771
#define PRETTYINDENT_VAR
Definition ruleutils.c:83
static void destroy_colinfo_names_hash(deparse_columns *colinfo)
Definition ruleutils.c:5053
static char * generate_relation_name(Oid relid, List *namespaces)
static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
static char * get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
Definition ruleutils.c:7630
Datum pg_get_serial_sequence(PG_FUNCTION_ARGS)
Definition ruleutils.c:2833
static char * generate_operator_name(Oid operid, Oid arg1, Oid arg2)
static void get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
Datum pg_get_function_arguments(PG_FUNCTION_ARGS)
Definition ruleutils.c:3183
static void get_json_table(TableFunc *tf, deparse_context *context, bool showimplicit)
#define PRETTYFLAG_SCHEMA
Definition ruleutils.c:90
static void get_select_query_def(Query *query, deparse_context *context)
Definition ruleutils.c:5910
static void get_opclass_name(Oid opclass, Oid actual_datatype, StringInfo buf)
Datum pg_get_viewdef_name(PG_FUNCTION_ARGS)
Definition ruleutils.c:737
Datum pg_get_userbyid(PG_FUNCTION_ARGS)
Definition ruleutils.c:2795
static void get_agg_expr_helper(Aggref *aggref, deparse_context *context, Aggref *original_aggref, const char *funcname, const char *options, bool is_json_objectagg)
#define RULE_INDEXDEF_PRETTY
Definition ruleutils.h:24
#define RULE_INDEXDEF_KEYS_ONLY
Definition ruleutils.h:25
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void UnregisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:866
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:824
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition spi.c:1175
uint64 SPI_processed
Definition spi.c:44
SPITupleTable * SPI_tuptable
Definition spi.c:45
int SPI_execute_plan(SPIPlanPtr plan, const Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition spi.c:672
int SPI_connect(void)
Definition spi.c:94
int SPI_finish(void)
Definition spi.c:182
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition spi.c:860
int SPI_keepplan(SPIPlanPtr plan)
Definition spi.c:976
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition spi.c:1220
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition spi.c:1252
#define SPI_OK_FINISH
Definition spi.h:83
#define SPI_OK_SELECT
Definition spi.h:86
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:88
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47
void check_stack_depth(void)
Definition stack_depth.c:95
#define BTEqualStrategyNumber
Definition stratnum.h:31
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition stringinfo.c:281
void appendStringInfoSpaces(StringInfo str, int count)
Definition stringinfo.c:260
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Oid aggfnoid
Definition primnodes.h:464
List * aggdistinct
Definition primnodes.h:494
List * aggdirectargs
Definition primnodes.h:485
List * args
Definition primnodes.h:488
Expr * aggfilter
Definition primnodes.h:497
List * aggorder
Definition primnodes.h:491
Index parent_relid
Definition pathnodes.h:3286
BoolExprType boolop
Definition primnodes.h:972
List * args
Definition primnodes.h:973
CTEMaterialize ctematerialized
bool attisdropped
Definition tupdesc.h:77
Oid consttype
Definition primnodes.h:330
char * cursor_name
Definition primnodes.h:2124
List * newvals
Definition primnodes.h:1194
Node * quals
Definition primnodes.h:2359
List * fromlist
Definition primnodes.h:2358
Oid funcid
Definition primnodes.h:783
List * args
Definition primnodes.h:801
JsonBehaviorType btype
Definition primnodes.h:1816
JsonBehavior * on_empty
Definition primnodes.h:1865
JsonFormat * format
Definition primnodes.h:1852
Node * path_spec
Definition primnodes.h:1855
JsonWrapper wrapper
Definition primnodes.h:1876
JsonExprOp op
Definition primnodes.h:1843
JsonBehavior * on_error
Definition primnodes.h:1866
bool omit_quotes
Definition primnodes.h:1879
JsonFormatType format_type
Definition primnodes.h:1677
JsonValueType item_type
Definition primnodes.h:1763
JsonFormat * format
Definition primnodes.h:1689
JsonTablePath * path
Definition primnodes.h:1924
JsonTablePlan * child
Definition primnodes.h:1933
Const * value
Definition primnodes.h:1897
JsonTablePlan * rplan
Definition primnodes.h:1954
JsonTablePlan * lplan
Definition primnodes.h:1953
Definition pg_list.h:54
Definition nodes.h:135
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869
int paramid
Definition primnodes.h:397
ParamKind paramkind
Definition primnodes.h:396
PartitionRangeDatumKind kind
Definition parsenodes.h:962
List * appendRelations
Definition plannodes.h:127
List * subplans
Definition plannodes.h:132
List * rtable
Definition plannodes.h:109
List * rowMarks
Definition parsenodes.h:234
bool groupDistinct
Definition parsenodes.h:217
Node * mergeJoinCondition
Definition parsenodes.h:196
Node * limitCount
Definition parsenodes.h:231
FromExpr * jointree
Definition parsenodes.h:182
List * returningList
Definition parsenodes.h:214
Node * setOperations
Definition parsenodes.h:236
List * cteList
Definition parsenodes.h:173
OnConflictExpr * onConflict
Definition parsenodes.h:203
List * groupClause
Definition parsenodes.h:216
Node * havingQual
Definition parsenodes.h:222
List * rtable
Definition parsenodes.h:175
Node * limitOffset
Definition parsenodes.h:230
CmdType commandType
Definition parsenodes.h:121
LimitOption limitOption
Definition parsenodes.h:232
Node * utilityStmt
Definition parsenodes.h:141
List * mergeActionList
Definition parsenodes.h:185
List * windowClause
Definition parsenodes.h:224
List * targetList
Definition parsenodes.h:198
List * groupingSets
Definition parsenodes.h:220
bool groupByAll
Definition parsenodes.h:218
List * distinctClause
Definition parsenodes.h:226
List * sortClause
Definition parsenodes.h:228
List * args
Definition primnodes.h:1449
LockClauseStrength strength
LockWaitPolicy waitPolicy
TupleDesc tupdesc
Definition spi.h:25
HeapTuple * vals
Definition spi.h:26
SQLValueFunctionOp op
Definition primnodes.h:1582
SetOperation op
Definition value.h:64
char * plan_name
Definition primnodes.h:1105
List * args
Definition primnodes.h:1125
List * paramIds
Definition primnodes.h:1101
bool isInitPlan
Definition primnodes.h:1112
bool useHashTable
Definition primnodes.h:1113
Node * testexpr
Definition primnodes.h:1100
List * parParam
Definition primnodes.h:1124
List * setParam
Definition primnodes.h:1122
SubLinkType subLinkType
Definition primnodes.h:1098
Expr * refassgnexpr
Definition primnodes.h:736
List * refupperindexpr
Definition primnodes.h:726
List * reflowerindexpr
Definition primnodes.h:732
Node * docexpr
Definition primnodes.h:121
Node * rowexpr
Definition primnodes.h:123
List * colexprs
Definition primnodes.h:133
TableFuncType functype
Definition primnodes.h:115
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
VarReturningType varreturningtype
Definition primnodes.h:298
Index varlevelsup
Definition primnodes.h:295
Node * startOffset
List * partitionClause
Node * endOffset
List * orderClause
List * args
Definition primnodes.h:606
Index winref
Definition primnodes.h:612
Expr * aggfilter
Definition primnodes.h:608
int ignore_nulls
Definition primnodes.h:618
List * args
Definition primnodes.h:1634
bool indent
Definition primnodes.h:1638
List * named_args
Definition primnodes.h:1630
XmlExprOp op
Definition primnodes.h:1626
List * parentUsing
Definition ruleutils.c:276
HTAB * names_hash
Definition ruleutils.c:308
char ** new_colnames
Definition ruleutils.c:269
char ** colnames
Definition ruleutils.c:252
List * usingNames
Definition ruleutils.c:299
bool * is_new_col
Definition ruleutils.c:270
TupleDesc resultDesc
Definition ruleutils.c:116
StringInfo buf
Definition ruleutils.c:114
List * targetList
Definition ruleutils.c:117
List * namespaces
Definition ruleutils.c:115
List * windowClause
Definition ruleutils.c:118
Bitmapset * appendparents
Definition ruleutils.c:126
AppendRelInfo ** appendrels
Definition ruleutils.c:169
List * rtable_columns
Definition ruleutils.c:166
Definition c.h:772
Definition c.h:757
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition c.h:764
Definition c.h:718
Definition type.h:89
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:625
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition tlist.c:354
int count_nonjunk_tlist_entries(List *tlist)
Definition tlist.c:195
#define ReleaseTupleDesc(tupdesc)
Definition tupdesc.h:219
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:175
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition typcache.c:1924
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:389
#define TYPECACHE_GT_OPR
Definition typcache.h:140
#define TYPECACHE_LT_OPR
Definition typcache.h:139
String * makeString(char *str)
Definition value.c:63
#define strVal(v)
Definition value.h:82
Node * flatten_group_exprs(PlannerInfo *root, Query *query, Node *node)
Definition var.c:972
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition var.c:114
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:194
bool SplitGUCList(char *rawstring, char separator, List **namelist)
Definition varlena.c:3023
text * cstring_to_text(const char *s)
Definition varlena.c:182
char * text_to_cstring(const text *t)
Definition varlena.c:215
List * textToQualifiedNameList(text *textval)
Definition varlena.c:2717
const char * type
const char * name
char * map_xml_name_to_sql_identifier(const char *name)
Definition xml.c:2474
@ XML_STANDALONE_NO_VALUE
Definition xml.h:29
@ XML_STANDALONE_YES
Definition xml.h:27
@ XML_STANDALONE_NO
Definition xml.h:28
static void convert(const int_fast32_t val, char *const buf)
Definition zic.c:1980