Age Owner Branch data TLA Line data Source code
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"
26 : : #include "catalog/pg_aggregate.h"
27 : : #include "catalog/pg_am.h"
28 : : #include "catalog/pg_authid.h"
29 : : #include "catalog/pg_collation.h"
30 : : #include "catalog/pg_constraint.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"
35 : : #include "catalog/pg_partitioned_table.h"
36 : : #include "catalog/pg_proc.h"
37 : : #include "catalog/pg_propgraph_element.h"
38 : : #include "catalog/pg_propgraph_element_label.h"
39 : : #include "catalog/pg_propgraph_label.h"
40 : : #include "catalog/pg_propgraph_label_property.h"
41 : : #include "catalog/pg_propgraph_property.h"
42 : : #include "catalog/pg_statistic_ext.h"
43 : : #include "catalog/pg_trigger.h"
44 : : #include "catalog/pg_type.h"
45 : : #include "commands/defrem.h"
46 : : #include "commands/tablespace.h"
47 : : #include "common/keywords.h"
48 : : #include "executor/spi.h"
49 : : #include "funcapi.h"
50 : : #include "mb/pg_wchar.h"
51 : : #include "miscadmin.h"
52 : : #include "nodes/makefuncs.h"
53 : : #include "nodes/nodeFuncs.h"
54 : : #include "nodes/pathnodes.h"
55 : : #include "optimizer/optimizer.h"
56 : : #include "parser/parse_agg.h"
57 : : #include "parser/parse_func.h"
58 : : #include "parser/parse_oper.h"
59 : : #include "parser/parse_relation.h"
60 : : #include "parser/parser.h"
61 : : #include "parser/parsetree.h"
62 : : #include "rewrite/rewriteHandler.h"
63 : : #include "rewrite/rewriteManip.h"
64 : : #include "rewrite/rewriteSupport.h"
65 : : #include "utils/array.h"
66 : : #include "utils/builtins.h"
67 : : #include "utils/fmgroids.h"
68 : : #include "utils/guc.h"
69 : : #include "utils/hsearch.h"
70 : : #include "utils/lsyscache.h"
71 : : #include "utils/partcache.h"
72 : : #include "utils/rel.h"
73 : : #include "utils/ruleutils.h"
74 : : #include "utils/snapmgr.h"
75 : : #include "utils/syscache.h"
76 : : #include "utils/typcache.h"
77 : : #include "utils/varlena.h"
78 : : #include "utils/xml.h"
79 : :
80 : : /* ----------
81 : : * Pretty formatting constants
82 : : * ----------
83 : : */
84 : :
85 : : /* Indent counts */
86 : : #define PRETTYINDENT_STD 8
87 : : #define PRETTYINDENT_JOIN 4
88 : : #define PRETTYINDENT_VAR 4
89 : :
90 : : #define PRETTYINDENT_LIMIT 40 /* wrap limit */
91 : :
92 : : /* Pretty flags */
93 : : #define PRETTYFLAG_PAREN 0x0001
94 : : #define PRETTYFLAG_INDENT 0x0002
95 : : #define PRETTYFLAG_SCHEMA 0x0004
96 : :
97 : : /* Standard conversion of a "bool pretty" option to detailed flags */
98 : : #define GET_PRETTY_FLAGS(pretty) \
99 : : ((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \
100 : : : PRETTYFLAG_INDENT)
101 : :
102 : : /* Default line length for pretty-print wrapping: 0 means wrap always */
103 : : #define WRAP_COLUMN_DEFAULT 0
104 : :
105 : : /* macros to test if pretty action needed */
106 : : #define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)
107 : : #define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)
108 : : #define PRETTY_SCHEMA(context) ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
109 : :
110 : :
111 : : /* ----------
112 : : * Local data types
113 : : * ----------
114 : : */
115 : :
116 : : /* Context info needed for invoking a recursive querytree display routine */
117 : : typedef struct
118 : : {
119 : : StringInfo buf; /* output buffer to append to */
120 : : List *namespaces; /* List of deparse_namespace nodes */
121 : : TupleDesc resultDesc; /* if top level of a view, the view's tupdesc */
122 : : List *targetList; /* Current query level's SELECT targetlist */
123 : : List *windowClause; /* Current query level's WINDOW clause */
124 : : int prettyFlags; /* enabling of pretty-print functions */
125 : : int wrapColumn; /* max line length, or -1 for no limit */
126 : : int indentLevel; /* current indent level for pretty-print */
127 : : bool varprefix; /* true to print prefixes on Vars */
128 : : bool colNamesVisible; /* do we care about output column names? */
129 : : bool inGroupBy; /* deparsing GROUP BY clause? */
130 : : bool varInOrderBy; /* deparsing simple Var in ORDER BY? */
131 : : Bitmapset *appendparents; /* if not null, map child Vars of these relids
132 : : * back to the parent rel */
133 : : } deparse_context;
134 : :
135 : : /*
136 : : * Each level of query context around a subtree needs a level of Var namespace.
137 : : * A Var having varlevelsup=N refers to the N'th item (counting from 0) in
138 : : * the current context's namespaces list.
139 : : *
140 : : * rtable is the list of actual RTEs from the Query or PlannedStmt.
141 : : * rtable_names holds the alias name to be used for each RTE (either a C
142 : : * string, or NULL for nameless RTEs such as unnamed joins).
143 : : * rtable_columns holds the column alias names to be used for each RTE.
144 : : *
145 : : * subplans is a list of Plan trees for SubPlans and CTEs (it's only used
146 : : * in the PlannedStmt case).
147 : : * ctes is a list of CommonTableExpr nodes (only used in the Query case).
148 : : * appendrels, if not null (it's only used in the PlannedStmt case), is an
149 : : * array of AppendRelInfo nodes, indexed by child relid. We use that to map
150 : : * child-table Vars to their inheritance parents.
151 : : *
152 : : * In some cases we need to make names of merged JOIN USING columns unique
153 : : * across the whole query, not only per-RTE. If so, unique_using is true
154 : : * and using_names is a list of C strings representing names already assigned
155 : : * to USING columns.
156 : : *
157 : : * When deparsing plan trees, there is always just a single item in the
158 : : * deparse_namespace list (since a plan tree never contains Vars with
159 : : * varlevelsup > 0). We store the Plan node that is the immediate
160 : : * parent of the expression to be deparsed, as well as a list of that
161 : : * Plan's ancestors. In addition, we store its outer and inner subplan nodes,
162 : : * as well as their targetlists, and the index tlist if the current plan node
163 : : * might contain INDEX_VAR Vars. (These fields could be derived on-the-fly
164 : : * from the current Plan node, but it seems notationally clearer to set them
165 : : * up as separate fields.)
166 : : */
167 : : typedef struct
168 : : {
169 : : List *rtable; /* List of RangeTblEntry nodes */
170 : : List *rtable_names; /* Parallel list of names for RTEs */
171 : : List *rtable_columns; /* Parallel list of deparse_columns structs */
172 : : List *subplans; /* List of Plan trees for SubPlans */
173 : : List *ctes; /* List of CommonTableExpr nodes */
174 : : AppendRelInfo **appendrels; /* Array of AppendRelInfo nodes, or NULL */
175 : : char *ret_old_alias; /* alias for OLD in RETURNING list */
176 : : char *ret_new_alias; /* alias for NEW in RETURNING list */
177 : : /* Workspace for column alias assignment: */
178 : : bool unique_using; /* Are we making USING names globally unique */
179 : : List *using_names; /* List of assigned names for USING columns */
180 : : /* Remaining fields are used only when deparsing a Plan tree: */
181 : : Plan *plan; /* immediate parent of current expression */
182 : : List *ancestors; /* ancestors of plan */
183 : : Plan *outer_plan; /* outer subnode, or NULL if none */
184 : : Plan *inner_plan; /* inner subnode, or NULL if none */
185 : : List *outer_tlist; /* referent for OUTER_VAR Vars */
186 : : List *inner_tlist; /* referent for INNER_VAR Vars */
187 : : List *index_tlist; /* referent for INDEX_VAR Vars */
188 : : /* Special namespace representing a function signature: */
189 : : char *funcname;
190 : : int numargs;
191 : : char **argnames;
192 : : } deparse_namespace;
193 : :
194 : : /*
195 : : * Per-relation data about column alias names.
196 : : *
197 : : * Selecting aliases is unreasonably complicated because of the need to dump
198 : : * rules/views whose underlying tables may have had columns added, deleted, or
199 : : * renamed since the query was parsed. We must nonetheless print the rule/view
200 : : * in a form that can be reloaded and will produce the same results as before.
201 : : *
202 : : * For each RTE used in the query, we must assign column aliases that are
203 : : * unique within that RTE. SQL does not require this of the original query,
204 : : * but due to factors such as *-expansion we need to be able to uniquely
205 : : * reference every column in a decompiled query. As long as we qualify all
206 : : * column references, per-RTE uniqueness is sufficient for that.
207 : : *
208 : : * However, we can't ensure per-column name uniqueness for unnamed join RTEs,
209 : : * since they just inherit column names from their input RTEs, and we can't
210 : : * rename the columns at the join level. Most of the time this isn't an issue
211 : : * because we don't need to reference the join's output columns as such; we
212 : : * can reference the input columns instead. That approach can fail for merged
213 : : * JOIN USING columns, however, so when we have one of those in an unnamed
214 : : * join, we have to make that column's alias globally unique across the whole
215 : : * query to ensure it can be referenced unambiguously.
216 : : *
217 : : * Another problem is that a JOIN USING clause requires the columns to be
218 : : * merged to have the same aliases in both input RTEs, and that no other
219 : : * columns in those RTEs or their children conflict with the USING names.
220 : : * To handle that, we do USING-column alias assignment in a recursive
221 : : * traversal of the query's jointree. When descending through a JOIN with
222 : : * USING, we preassign the USING column names to the child columns, overriding
223 : : * other rules for column alias assignment. We also mark each RTE with a list
224 : : * of all USING column names selected for joins containing that RTE, so that
225 : : * when we assign other columns' aliases later, we can avoid conflicts.
226 : : *
227 : : * Another problem is that if a JOIN's input tables have had columns added or
228 : : * deleted since the query was parsed, we must generate a column alias list
229 : : * for the join that matches the current set of input columns --- otherwise, a
230 : : * change in the number of columns in the left input would throw off matching
231 : : * of aliases to columns of the right input. Thus, positions in the printable
232 : : * column alias list are not necessarily one-for-one with varattnos of the
233 : : * JOIN, so we need a separate new_colnames[] array for printing purposes.
234 : : *
235 : : * Finally, when dealing with wide tables we risk O(N^2) costs in assigning
236 : : * non-duplicate column names. We ameliorate that by using a hash table that
237 : : * holds all the strings appearing in colnames, new_colnames, and parentUsing.
238 : : */
239 : : typedef struct
240 : : {
241 : : /*
242 : : * colnames is an array containing column aliases to use for columns that
243 : : * existed when the query was parsed. Dropped columns have NULL entries.
244 : : * This array can be directly indexed by varattno to get a Var's name.
245 : : *
246 : : * Non-NULL entries are guaranteed unique within the RTE, *except* when
247 : : * this is for an unnamed JOIN RTE. In that case we merely copy up names
248 : : * from the two input RTEs.
249 : : *
250 : : * During the recursive descent in set_using_names(), forcible assignment
251 : : * of a child RTE's column name is represented by pre-setting that element
252 : : * of the child's colnames array. So at that stage, NULL entries in this
253 : : * array just mean that no name has been preassigned, not necessarily that
254 : : * the column is dropped.
255 : : */
256 : : int num_cols; /* length of colnames[] array */
257 : : char **colnames; /* array of C strings and NULLs */
258 : :
259 : : /*
260 : : * new_colnames is an array containing column aliases to use for columns
261 : : * that would exist if the query was re-parsed against the current
262 : : * definitions of its base tables. This is what to print as the column
263 : : * alias list for the RTE. This array does not include dropped columns,
264 : : * but it will include columns added since original parsing. Indexes in
265 : : * it therefore have little to do with current varattno values. As above,
266 : : * entries are unique unless this is for an unnamed JOIN RTE. (In such an
267 : : * RTE, we never actually print this array, but we must compute it anyway
268 : : * for possible use in computing column names of upper joins.) The
269 : : * parallel array is_new_col marks which of these columns are new since
270 : : * original parsing. Entries with is_new_col false must match the
271 : : * non-NULL colnames entries one-for-one.
272 : : */
273 : : int num_new_cols; /* length of new_colnames[] array */
274 : : char **new_colnames; /* array of C strings */
275 : : bool *is_new_col; /* array of bool flags */
276 : :
277 : : /* This flag tells whether we should actually print a column alias list */
278 : : bool printaliases;
279 : :
280 : : /* This list has all names used as USING names in joins above this RTE */
281 : : List *parentUsing; /* names assigned to parent merged columns */
282 : :
283 : : /*
284 : : * If this struct is for a JOIN RTE, we fill these fields during the
285 : : * set_using_names() pass to describe its relationship to its child RTEs.
286 : : *
287 : : * leftattnos and rightattnos are arrays with one entry per existing
288 : : * output column of the join (hence, indexable by join varattno). For a
289 : : * simple reference to a column of the left child, leftattnos[i] is the
290 : : * child RTE's attno and rightattnos[i] is zero; and conversely for a
291 : : * column of the right child. But for merged columns produced by JOIN
292 : : * USING/NATURAL JOIN, both leftattnos[i] and rightattnos[i] are nonzero.
293 : : * Note that a simple reference might be to a child RTE column that's been
294 : : * dropped; but that's OK since the column could not be used in the query.
295 : : *
296 : : * If it's a JOIN USING, usingNames holds the alias names selected for the
297 : : * merged columns (these might be different from the original USING list,
298 : : * if we had to modify names to achieve uniqueness).
299 : : */
300 : : int leftrti; /* rangetable index of left child */
301 : : int rightrti; /* rangetable index of right child */
302 : : int *leftattnos; /* left-child varattnos of join cols, or 0 */
303 : : int *rightattnos; /* right-child varattnos of join cols, or 0 */
304 : : List *usingNames; /* names assigned to merged columns */
305 : :
306 : : /*
307 : : * Hash table holding copies of all the strings appearing in this struct's
308 : : * colnames, new_colnames, and parentUsing. We use a hash table only for
309 : : * sufficiently wide relations, and only during the colname-assignment
310 : : * functions set_relation_column_names and set_join_column_names;
311 : : * otherwise, names_hash is NULL.
312 : : */
313 : : HTAB *names_hash; /* entries are just strings */
314 : : } deparse_columns;
315 : :
316 : : /* This macro is analogous to rt_fetch(), but for deparse_columns structs */
317 : : #define deparse_columns_fetch(rangetable_index, dpns) \
318 : : ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
319 : :
320 : : /*
321 : : * Entry in set_rtable_names' hash table
322 : : */
323 : : typedef struct
324 : : {
325 : : char name[NAMEDATALEN]; /* Hash key --- must be first */
326 : : int counter; /* Largest addition used so far for name */
327 : : } NameHashEntry;
328 : :
329 : : /* Callback signature for resolve_special_varno() */
330 : : typedef void (*rsv_callback) (Node *node, deparse_context *context,
331 : : void *callback_arg);
332 : :
333 : :
334 : : /* ----------
335 : : * Global data
336 : : * ----------
337 : : */
338 : : static SPIPlanPtr plan_getrulebyoid = NULL;
339 : : static const char *const query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1";
340 : : static SPIPlanPtr plan_getviewrule = NULL;
341 : : static const char *const query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
342 : :
343 : : /* GUC parameters */
344 : : bool quote_all_identifiers = false;
345 : :
346 : :
347 : : /* ----------
348 : : * Local functions
349 : : *
350 : : * Most of these functions used to use fixed-size buffers to build their
351 : : * results. Now, they take an (already initialized) StringInfo object
352 : : * as a parameter, and append their text output to its contents.
353 : : * ----------
354 : : */
355 : : static char *deparse_expression_pretty(Node *expr, List *dpcontext,
356 : : bool forceprefix, bool showimplicit,
357 : : int prettyFlags, int startIndent);
358 : : static char *pg_get_viewdef_worker(Oid viewoid,
359 : : int prettyFlags, int wrapColumn);
360 : : static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
361 : : static int decompile_column_index_array(Datum column_index_array, Oid relId,
362 : : bool withPeriod, StringInfo buf);
363 : : static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
364 : : static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
365 : : const Oid *excludeOps,
366 : : bool attrsOnly, bool keysOnly,
367 : : bool showTblSpc, bool inherits,
368 : : int prettyFlags, bool missing_ok);
369 : : static void make_propgraphdef_elements(StringInfo buf, Oid pgrelid, char pgekind);
370 : : static void make_propgraphdef_labels(StringInfo buf, Oid elid, const char *elalias, Oid elrelid);
371 : : static void make_propgraphdef_properties(StringInfo buf, Oid ellabelid, Oid elrelid);
372 : : static char *pg_get_statisticsobj_worker(Oid statextid, bool columns_only,
373 : : bool missing_ok);
374 : : static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags,
375 : : bool attrsOnly, bool missing_ok);
376 : : static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
377 : : int prettyFlags, bool missing_ok);
378 : : static text *pg_get_expr_worker(text *expr, Oid relid, int prettyFlags);
379 : : static int print_function_arguments(StringInfo buf, HeapTuple proctup,
380 : : bool print_table_args, bool print_defaults);
381 : : static void print_function_rettype(StringInfo buf, HeapTuple proctup);
382 : : static void print_function_trftypes(StringInfo buf, HeapTuple proctup);
383 : : static void print_function_sqlbody(StringInfo buf, HeapTuple proctup);
384 : : static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
385 : : Bitmapset *rels_used);
386 : : static void set_deparse_for_query(deparse_namespace *dpns, Query *query,
387 : : List *parent_namespaces);
388 : : static void set_simple_column_names(deparse_namespace *dpns);
389 : : static bool has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode);
390 : : static void set_using_names(deparse_namespace *dpns, Node *jtnode,
391 : : List *parentUsing);
392 : : static void set_relation_column_names(deparse_namespace *dpns,
393 : : RangeTblEntry *rte,
394 : : deparse_columns *colinfo);
395 : : static void set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
396 : : deparse_columns *colinfo);
397 : : static bool colname_is_unique(const char *colname, deparse_namespace *dpns,
398 : : deparse_columns *colinfo);
399 : : static char *make_colname_unique(char *colname, deparse_namespace *dpns,
400 : : deparse_columns *colinfo);
401 : : static void expand_colnames_array_to(deparse_columns *colinfo, int n);
402 : : static void build_colinfo_names_hash(deparse_columns *colinfo);
403 : : static void add_to_names_hash(deparse_columns *colinfo, const char *name);
404 : : static void destroy_colinfo_names_hash(deparse_columns *colinfo);
405 : : static void identify_join_columns(JoinExpr *j, RangeTblEntry *jrte,
406 : : deparse_columns *colinfo);
407 : : static char *get_rtable_name(int rtindex, deparse_context *context);
408 : : static void set_deparse_plan(deparse_namespace *dpns, Plan *plan);
409 : : static Plan *find_recursive_union(deparse_namespace *dpns,
410 : : WorkTableScan *wtscan);
411 : : static void push_child_plan(deparse_namespace *dpns, Plan *plan,
412 : : deparse_namespace *save_dpns);
413 : : static void pop_child_plan(deparse_namespace *dpns,
414 : : deparse_namespace *save_dpns);
415 : : static void push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
416 : : deparse_namespace *save_dpns);
417 : : static void pop_ancestor_plan(deparse_namespace *dpns,
418 : : deparse_namespace *save_dpns);
419 : : static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
420 : : int prettyFlags);
421 : : static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
422 : : int prettyFlags, int wrapColumn);
423 : : static void get_query_def(Query *query, StringInfo buf, List *parentnamespace,
424 : : TupleDesc resultDesc, bool colNamesVisible,
425 : : int prettyFlags, int wrapColumn, int startIndent);
426 : : static void get_values_def(List *values_lists, deparse_context *context);
427 : : static void get_with_clause(Query *query, deparse_context *context);
428 : : static void get_select_query_def(Query *query, deparse_context *context);
429 : : static void get_insert_query_def(Query *query, deparse_context *context);
430 : : static void get_update_query_def(Query *query, deparse_context *context);
431 : : static void get_update_query_targetlist_def(Query *query, List *targetList,
432 : : deparse_context *context,
433 : : RangeTblEntry *rte);
434 : : static void get_delete_query_def(Query *query, deparse_context *context);
435 : : static void get_merge_query_def(Query *query, deparse_context *context);
436 : : static void get_utility_query_def(Query *query, deparse_context *context);
437 : : static char *get_lock_clause_strength(LockClauseStrength strength);
438 : : static void get_basic_select_query(Query *query, deparse_context *context);
439 : : static void get_target_list(List *targetList, deparse_context *context);
440 : : static void get_returning_clause(Query *query, deparse_context *context);
441 : : static void get_setop_query(Node *setOp, Query *query,
442 : : deparse_context *context);
443 : : static Node *get_rule_sortgroupclause(Index ref, List *tlist,
444 : : bool force_colno,
445 : : deparse_context *context);
446 : : static void get_rule_groupingset(GroupingSet *gset, List *targetlist,
447 : : bool omit_parens, deparse_context *context);
448 : : static void get_rule_orderby(List *orderList, List *targetList,
449 : : bool force_colno, deparse_context *context);
450 : : static void get_rule_windowclause(Query *query, deparse_context *context);
451 : : static void get_rule_windowspec(WindowClause *wc, List *targetList,
452 : : deparse_context *context);
453 : : static void get_window_frame_options(int frameOptions,
454 : : Node *startOffset, Node *endOffset,
455 : : deparse_context *context);
456 : : static char *get_variable(Var *var, int levelsup, bool istoplevel,
457 : : deparse_context *context);
458 : : static void get_special_variable(Node *node, deparse_context *context,
459 : : void *callback_arg);
460 : : static void resolve_special_varno(Node *node, deparse_context *context,
461 : : rsv_callback callback, void *callback_arg);
462 : : static Node *find_param_referent(Param *param, deparse_context *context,
463 : : deparse_namespace **dpns_p, ListCell **ancestor_cell_p);
464 : : static SubPlan *find_param_generator(Param *param, deparse_context *context,
465 : : int *column_p);
466 : : static SubPlan *find_param_generator_initplan(Param *param, Plan *plan,
467 : : int *column_p);
468 : : static void get_parameter(Param *param, deparse_context *context);
469 : : static const char *get_simple_binary_op_name(OpExpr *expr);
470 : : static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);
471 : : static void appendContextKeyword(deparse_context *context, const char *str,
472 : : int indentBefore, int indentAfter, int indentPlus);
473 : : static void removeStringInfoSpaces(StringInfo str);
474 : : static void get_rule_expr(Node *node, deparse_context *context,
475 : : bool showimplicit);
476 : : static void get_rule_expr_toplevel(Node *node, deparse_context *context,
477 : : bool showimplicit);
478 : : static void get_rule_list_toplevel(List *lst, deparse_context *context,
479 : : bool showimplicit);
480 : : static void get_rule_expr_funccall(Node *node, deparse_context *context,
481 : : bool showimplicit);
482 : : static bool looks_like_function(Node *node);
483 : : static void get_oper_expr(OpExpr *expr, deparse_context *context);
484 : : static void get_func_expr(FuncExpr *expr, deparse_context *context,
485 : : bool showimplicit);
486 : : static void get_agg_expr(Aggref *aggref, deparse_context *context,
487 : : Aggref *original_aggref);
488 : : static void get_agg_expr_helper(Aggref *aggref, deparse_context *context,
489 : : Aggref *original_aggref, const char *funcname,
490 : : const char *options, bool is_json_objectagg);
491 : : static void get_agg_combine_expr(Node *node, deparse_context *context,
492 : : void *callback_arg);
493 : : static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context);
494 : : static void get_windowfunc_expr_helper(WindowFunc *wfunc, deparse_context *context,
495 : : const char *funcname, const char *options,
496 : : bool is_json_objectagg);
497 : : static bool get_func_sql_syntax(FuncExpr *expr, deparse_context *context);
498 : : static void get_coercion_expr(Node *arg, deparse_context *context,
499 : : Oid resulttype, int32 resulttypmod,
500 : : Node *parentNode);
501 : : static void get_const_expr(Const *constval, deparse_context *context,
502 : : int showtype);
503 : : static void get_const_collation(Const *constval, deparse_context *context);
504 : : static void get_json_format(JsonFormat *format, StringInfo buf);
505 : : static void get_json_returning(JsonReturning *returning, StringInfo buf,
506 : : bool json_format_by_default);
507 : : static void get_json_constructor(JsonConstructorExpr *ctor,
508 : : deparse_context *context, bool showimplicit);
509 : : static void get_json_constructor_options(JsonConstructorExpr *ctor,
510 : : StringInfo buf);
511 : : static void get_json_agg_constructor(JsonConstructorExpr *ctor,
512 : : deparse_context *context,
513 : : const char *funcname,
514 : : bool is_json_objectagg);
515 : : static void simple_quote_literal(StringInfo buf, const char *val);
516 : : static void get_sublink_expr(SubLink *sublink, deparse_context *context);
517 : : static void get_tablefunc(TableFunc *tf, deparse_context *context,
518 : : bool showimplicit);
519 : : static void get_from_clause(Query *query, const char *prefix,
520 : : deparse_context *context);
521 : : static void get_from_clause_item(Node *jtnode, Query *query,
522 : : deparse_context *context);
523 : : static void get_rte_alias(RangeTblEntry *rte, int varno, bool use_as,
524 : : deparse_context *context);
525 : : static void get_column_alias_list(deparse_columns *colinfo,
526 : : deparse_context *context);
527 : : static void get_for_portion_of(ForPortionOfExpr *forPortionOf,
528 : : deparse_context *context);
529 : : static void get_from_clause_coldeflist(RangeTblFunction *rtfunc,
530 : : deparse_columns *colinfo,
531 : : deparse_context *context);
532 : : static void get_tablesample_def(TableSampleClause *tablesample,
533 : : deparse_context *context);
534 : : static void get_opclass_name(Oid opclass, Oid actual_datatype,
535 : : StringInfo buf);
536 : : static Node *processIndirection(Node *node, deparse_context *context);
537 : : static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context);
538 : : static char *get_relation_name(Oid relid);
539 : : static char *generate_relation_name(Oid relid, List *namespaces);
540 : : static char *generate_qualified_relation_name(Oid relid);
541 : : static char *generate_function_name(Oid funcid, int nargs,
542 : : List *argnames, Oid *argtypes,
543 : : bool has_variadic, bool *use_variadic_p,
544 : : bool inGroupBy);
545 : : static char *generate_operator_name(Oid operid, Oid arg1, Oid arg2);
546 : : static void add_cast_to(StringInfo buf, Oid typid);
547 : : static char *generate_qualified_type_name(Oid typid);
548 : : static text *string_to_text(char *str);
549 : : static char *flatten_reloptions(Oid relid);
550 : : void get_reloptions(StringInfo buf, Datum reloptions);
551 : : static void get_json_path_spec(Node *path_spec, deparse_context *context,
552 : : bool showimplicit);
553 : : static void get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan,
554 : : deparse_context *context,
555 : : bool showimplicit);
556 : : static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan,
557 : : deparse_context *context,
558 : : bool showimplicit,
559 : : bool needcomma);
560 : :
561 : : #define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
562 : :
563 : :
564 : : /* ----------
565 : : * pg_get_ruledef - Do it all and return a text
566 : : * that could be used as a statement
567 : : * to recreate the rule
568 : : * ----------
569 : : */
570 : : Datum
9434 tgl@sss.pgh.pa.us 571 :CBC 237 : pg_get_ruledef(PG_FUNCTION_ARGS)
572 : : {
8783 573 : 237 : Oid ruleoid = PG_GETARG_OID(0);
574 : : int prettyFlags;
575 : : char *res;
576 : :
4839 577 : 237 : prettyFlags = PRETTYFLAG_INDENT;
578 : :
3570 rhaas@postgresql.org 579 : 237 : res = pg_get_ruledef_worker(ruleoid, prettyFlags);
580 : :
581 [ + + ]: 237 : if (res == NULL)
582 : 4 : PG_RETURN_NULL();
583 : :
584 : 233 : PG_RETURN_TEXT_P(string_to_text(res));
585 : : }
586 : :
587 : :
588 : : Datum
8315 tgl@sss.pgh.pa.us 589 : 76 : pg_get_ruledef_ext(PG_FUNCTION_ARGS)
590 : : {
591 : 76 : Oid ruleoid = PG_GETARG_OID(0);
592 : 76 : bool pretty = PG_GETARG_BOOL(1);
593 : : int prettyFlags;
594 : : char *res;
595 : :
1499 596 [ + - ]: 76 : prettyFlags = GET_PRETTY_FLAGS(pretty);
597 : :
3570 rhaas@postgresql.org 598 : 76 : res = pg_get_ruledef_worker(ruleoid, prettyFlags);
599 : :
600 [ - + ]: 76 : if (res == NULL)
3570 rhaas@postgresql.org 601 :UBC 0 : PG_RETURN_NULL();
602 : :
3570 rhaas@postgresql.org 603 :CBC 76 : PG_RETURN_TEXT_P(string_to_text(res));
604 : : }
605 : :
606 : :
607 : : static char *
8315 tgl@sss.pgh.pa.us 608 : 313 : pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
609 : : {
610 : : Datum args[1];
611 : : char nulls[1];
612 : : int spirc;
613 : : HeapTuple ruletup;
614 : : TupleDesc rulettc;
615 : : StringInfoData buf;
616 : :
617 : : /*
618 : : * Do this first so that string is alloc'd in outer context not SPI's.
619 : : */
8035 620 : 313 : initStringInfo(&buf);
621 : :
622 : : /*
623 : : * Connect to SPI manager
624 : : */
603 625 : 313 : SPI_connect();
626 : :
627 : : /*
628 : : * On the first call prepare the plan to lookup pg_rewrite. We read
629 : : * pg_rewrite over the SPI manager instead of using the syscache to be
630 : : * checked for read access on pg_rewrite.
631 : : */
8783 632 [ + + ]: 313 : if (plan_getrulebyoid == NULL)
633 : : {
634 : : Oid argtypes[1];
635 : : SPIPlanPtr plan;
636 : :
637 : 24 : argtypes[0] = OIDOID;
638 : 24 : plan = SPI_prepare(query_getrulebyoid, 1, argtypes);
10108 bruce@momjian.us 639 [ - + ]: 24 : if (plan == NULL)
8318 tgl@sss.pgh.pa.us 640 [ # # ]:UBC 0 : elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid);
5345 tgl@sss.pgh.pa.us 641 :CBC 24 : SPI_keepplan(plan);
642 : 24 : plan_getrulebyoid = plan;
643 : : }
644 : :
645 : : /*
646 : : * Get the pg_rewrite tuple for this rule
647 : : */
8783 648 : 313 : args[0] = ObjectIdGetDatum(ruleoid);
649 : 313 : nulls[0] = ' ';
4546 peter_e@gmx.net 650 : 313 : spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 0);
10108 bruce@momjian.us 651 [ - + ]: 313 : if (spirc != SPI_OK_SELECT)
8318 tgl@sss.pgh.pa.us 652 [ # # ]:UBC 0 : elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
10108 bruce@momjian.us 653 [ + + ]:CBC 313 : if (SPI_processed != 1)
654 : : {
655 : : /*
656 : : * There is no tuple data available here, just keep the output buffer
657 : : * empty.
658 : : */
659 : : }
660 : : else
661 : : {
662 : : /*
663 : : * Get the rule's definition and put it into executor's memory
664 : : */
8035 tgl@sss.pgh.pa.us 665 : 309 : ruletup = SPI_tuptable->vals[0];
666 : 309 : rulettc = SPI_tuptable->tupdesc;
667 : 309 : make_ruledef(&buf, ruletup, rulettc, prettyFlags);
668 : : }
669 : :
670 : : /*
671 : : * Disconnect from SPI manager
672 : : */
10108 bruce@momjian.us 673 [ - + ]: 313 : if (SPI_finish() != SPI_OK_FINISH)
8318 tgl@sss.pgh.pa.us 674 [ # # ]:UBC 0 : elog(ERROR, "SPI_finish failed");
675 : :
3570 rhaas@postgresql.org 676 [ + + ]:CBC 313 : if (buf.len == 0)
677 : 4 : return NULL;
678 : :
8035 tgl@sss.pgh.pa.us 679 : 309 : return buf.data;
680 : : }
681 : :
682 : :
683 : : /* ----------
684 : : * pg_get_viewdef - Mainly the same thing, but we
685 : : * only return the SELECT part of a view
686 : : * ----------
687 : : */
688 : : Datum
9434 689 : 1422 : pg_get_viewdef(PG_FUNCTION_ARGS)
690 : : {
691 : : /* By OID */
8783 692 : 1422 : Oid viewoid = PG_GETARG_OID(0);
693 : : int prettyFlags;
694 : : char *res;
695 : :
4839 696 : 1422 : prettyFlags = PRETTYFLAG_INDENT;
697 : :
3570 rhaas@postgresql.org 698 : 1422 : res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
699 : :
700 [ + + ]: 1422 : if (res == NULL)
701 : 4 : PG_RETURN_NULL();
702 : :
703 : 1418 : PG_RETURN_TEXT_P(string_to_text(res));
704 : : }
705 : :
706 : :
707 : : Datum
8315 tgl@sss.pgh.pa.us 708 : 391 : pg_get_viewdef_ext(PG_FUNCTION_ARGS)
709 : : {
710 : : /* By OID */
711 : 391 : Oid viewoid = PG_GETARG_OID(0);
712 : 391 : bool pretty = PG_GETARG_BOOL(1);
713 : : int prettyFlags;
714 : : char *res;
715 : :
1499 716 [ + - ]: 391 : prettyFlags = GET_PRETTY_FLAGS(pretty);
717 : :
3570 rhaas@postgresql.org 718 : 391 : res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
719 : :
720 [ - + ]: 391 : if (res == NULL)
3570 rhaas@postgresql.org 721 :UBC 0 : PG_RETURN_NULL();
722 : :
3570 rhaas@postgresql.org 723 :CBC 391 : PG_RETURN_TEXT_P(string_to_text(res));
724 : : }
725 : :
726 : : Datum
5189 andrew@dunslane.net 727 : 4 : pg_get_viewdef_wrap(PG_FUNCTION_ARGS)
728 : : {
729 : : /* By OID */
730 : 4 : Oid viewoid = PG_GETARG_OID(0);
5077 bruce@momjian.us 731 : 4 : int wrap = PG_GETARG_INT32(1);
732 : : int prettyFlags;
733 : : char *res;
734 : :
735 : : /* calling this implies we want pretty printing */
1499 tgl@sss.pgh.pa.us 736 : 4 : prettyFlags = GET_PRETTY_FLAGS(true);
737 : :
3570 rhaas@postgresql.org 738 : 4 : res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
739 : :
740 [ - + ]: 4 : if (res == NULL)
3570 rhaas@postgresql.org 741 :UBC 0 : PG_RETURN_NULL();
742 : :
3570 rhaas@postgresql.org 743 :CBC 4 : PG_RETURN_TEXT_P(string_to_text(res));
744 : : }
745 : :
746 : : Datum
8783 tgl@sss.pgh.pa.us 747 : 52 : pg_get_viewdef_name(PG_FUNCTION_ARGS)
748 : : {
749 : : /* By qualified name */
3341 noah@leadboat.com 750 : 52 : text *viewname = PG_GETARG_TEXT_PP(0);
751 : : int prettyFlags;
752 : : RangeVar *viewrel;
753 : : Oid viewoid;
754 : : char *res;
755 : :
4839 tgl@sss.pgh.pa.us 756 : 52 : prettyFlags = PRETTYFLAG_INDENT;
757 : :
758 : : /* Look up view name. Can't lock it - we might not have privileges. */
7648 neilc@samurai.com 759 : 52 : viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
5270 rhaas@postgresql.org 760 : 52 : viewoid = RangeVarGetRelid(viewrel, NoLock, false);
761 : :
3570 762 : 52 : res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
763 : :
764 [ - + ]: 52 : if (res == NULL)
3570 rhaas@postgresql.org 765 :UBC 0 : PG_RETURN_NULL();
766 : :
3570 rhaas@postgresql.org 767 :CBC 52 : PG_RETURN_TEXT_P(string_to_text(res));
768 : : }
769 : :
770 : :
771 : : Datum
8315 tgl@sss.pgh.pa.us 772 : 268 : pg_get_viewdef_name_ext(PG_FUNCTION_ARGS)
773 : : {
774 : : /* By qualified name */
3341 noah@leadboat.com 775 : 268 : text *viewname = PG_GETARG_TEXT_PP(0);
8315 tgl@sss.pgh.pa.us 776 : 268 : bool pretty = PG_GETARG_BOOL(1);
777 : : int prettyFlags;
778 : : RangeVar *viewrel;
779 : : Oid viewoid;
780 : : char *res;
781 : :
1499 782 [ + - ]: 268 : prettyFlags = GET_PRETTY_FLAGS(pretty);
783 : :
784 : : /* Look up view name. Can't lock it - we might not have privileges. */
7648 neilc@samurai.com 785 : 268 : viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
5270 rhaas@postgresql.org 786 : 268 : viewoid = RangeVarGetRelid(viewrel, NoLock, false);
787 : :
3558 tgl@sss.pgh.pa.us 788 : 268 : res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
789 : :
790 [ - + ]: 268 : if (res == NULL)
3558 tgl@sss.pgh.pa.us 791 :UBC 0 : PG_RETURN_NULL();
792 : :
3558 tgl@sss.pgh.pa.us 793 :CBC 268 : PG_RETURN_TEXT_P(string_to_text(res));
794 : : }
795 : :
796 : : /*
797 : : * Common code for by-OID and by-name variants of pg_get_viewdef
798 : : */
799 : : static char *
4880 800 : 2137 : pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
801 : : {
802 : : Datum args[2];
803 : : char nulls[2];
804 : : int spirc;
805 : : HeapTuple ruletup;
806 : : TupleDesc rulettc;
807 : : StringInfoData buf;
808 : :
809 : : /*
810 : : * Do this first so that string is alloc'd in outer context not SPI's.
811 : : */
8035 812 : 2137 : initStringInfo(&buf);
813 : :
814 : : /*
815 : : * Connect to SPI manager
816 : : */
603 817 : 2137 : SPI_connect();
818 : :
819 : : /*
820 : : * On the first call prepare the plan to lookup pg_rewrite. We read
821 : : * pg_rewrite over the SPI manager instead of using the syscache to be
822 : : * checked for read access on pg_rewrite.
823 : : */
8783 824 [ + + ]: 2137 : if (plan_getviewrule == NULL)
825 : : {
826 : : Oid argtypes[2];
827 : : SPIPlanPtr plan;
828 : :
829 : 156 : argtypes[0] = OIDOID;
830 : 156 : argtypes[1] = NAMEOID;
831 : 156 : plan = SPI_prepare(query_getviewrule, 2, argtypes);
10108 bruce@momjian.us 832 [ - + ]: 156 : if (plan == NULL)
8318 tgl@sss.pgh.pa.us 833 [ # # ]:UBC 0 : elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule);
5345 tgl@sss.pgh.pa.us 834 :CBC 156 : SPI_keepplan(plan);
835 : 156 : plan_getviewrule = plan;
836 : : }
837 : :
838 : : /*
839 : : * Get the pg_rewrite tuple for the view's SELECT rule
840 : : */
8783 841 : 2137 : args[0] = ObjectIdGetDatum(viewoid);
4546 peter_e@gmx.net 842 : 2137 : args[1] = DirectFunctionCall1(namein, CStringGetDatum(ViewSelectRuleName));
10108 bruce@momjian.us 843 : 2137 : nulls[0] = ' ';
8783 tgl@sss.pgh.pa.us 844 : 2137 : nulls[1] = ' ';
4546 peter_e@gmx.net 845 : 2137 : spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 0);
10108 bruce@momjian.us 846 [ - + ]: 2137 : if (spirc != SPI_OK_SELECT)
8768 tgl@sss.pgh.pa.us 847 [ # # ]:UBC 0 : elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
10108 bruce@momjian.us 848 [ + + ]:CBC 2137 : if (SPI_processed != 1)
849 : : {
850 : : /*
851 : : * There is no tuple data available here, just keep the output buffer
852 : : * empty.
853 : : */
854 : : }
855 : : else
856 : : {
857 : : /*
858 : : * Get the rule's definition and put it into executor's memory
859 : : */
860 : 2133 : ruletup = SPI_tuptable->vals[0];
861 : 2133 : rulettc = SPI_tuptable->tupdesc;
4880 tgl@sss.pgh.pa.us 862 : 2133 : make_viewdef(&buf, ruletup, rulettc, prettyFlags, wrapColumn);
863 : : }
864 : :
865 : : /*
866 : : * Disconnect from SPI manager
867 : : */
10108 bruce@momjian.us 868 [ - + ]: 2137 : if (SPI_finish() != SPI_OK_FINISH)
8318 tgl@sss.pgh.pa.us 869 [ # # ]:UBC 0 : elog(ERROR, "SPI_finish failed");
870 : :
3570 rhaas@postgresql.org 871 [ + + ]:CBC 2137 : if (buf.len == 0)
872 : 4 : return NULL;
873 : :
8035 tgl@sss.pgh.pa.us 874 : 2133 : return buf.data;
875 : : }
876 : :
877 : : /* ----------
878 : : * pg_get_triggerdef - Get the definition of a trigger
879 : : * ----------
880 : : */
881 : : Datum
8447 bruce@momjian.us 882 : 106 : pg_get_triggerdef(PG_FUNCTION_ARGS)
883 : : {
884 : 106 : Oid trigid = PG_GETARG_OID(0);
885 : : char *res;
886 : :
3570 rhaas@postgresql.org 887 : 106 : res = pg_get_triggerdef_worker(trigid, false);
888 : :
889 [ + + ]: 106 : if (res == NULL)
890 : 4 : PG_RETURN_NULL();
891 : :
892 : 102 : PG_RETURN_TEXT_P(string_to_text(res));
893 : : }
894 : :
895 : : Datum
6052 peter_e@gmx.net 896 : 643 : pg_get_triggerdef_ext(PG_FUNCTION_ARGS)
897 : : {
898 : 643 : Oid trigid = PG_GETARG_OID(0);
899 : 643 : bool pretty = PG_GETARG_BOOL(1);
900 : : char *res;
901 : :
3570 rhaas@postgresql.org 902 : 643 : res = pg_get_triggerdef_worker(trigid, pretty);
903 : :
904 [ - + ]: 643 : if (res == NULL)
3570 rhaas@postgresql.org 905 :UBC 0 : PG_RETURN_NULL();
906 : :
3570 rhaas@postgresql.org 907 :CBC 643 : PG_RETURN_TEXT_P(string_to_text(res));
908 : : }
909 : :
910 : : static char *
6052 peter_e@gmx.net 911 : 749 : pg_get_triggerdef_worker(Oid trigid, bool pretty)
912 : : {
913 : : HeapTuple ht_trig;
914 : : Form_pg_trigger trigrec;
915 : : StringInfoData buf;
916 : : Relation tgrel;
917 : : ScanKeyData skey[1];
918 : : SysScanDesc tgscan;
8386 tgl@sss.pgh.pa.us 919 : 749 : int findx = 0;
920 : : char *tgname;
921 : : char *tgoldtable;
922 : : char *tgnewtable;
923 : : Datum value;
924 : : bool isnull;
925 : :
926 : : /*
927 : : * Fetch the pg_trigger tuple by the Oid of the trigger
928 : : */
2661 andres@anarazel.de 929 : 749 : tgrel = table_open(TriggerRelationId, AccessShareLock);
930 : :
8210 tgl@sss.pgh.pa.us 931 : 749 : ScanKeyInit(&skey[0],
932 : : Anum_pg_trigger_oid,
933 : : BTEqualStrategyNumber, F_OIDEQ,
934 : : ObjectIdGetDatum(trigid));
935 : :
7691 936 : 749 : tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
937 : : NULL, 1, skey);
938 : :
8386 939 : 749 : ht_trig = systable_getnext(tgscan);
940 : :
941 [ + + ]: 749 : if (!HeapTupleIsValid(ht_trig))
942 : : {
3570 rhaas@postgresql.org 943 : 4 : systable_endscan(tgscan);
2661 andres@anarazel.de 944 : 4 : table_close(tgrel, AccessShareLock);
3570 rhaas@postgresql.org 945 : 4 : return NULL;
946 : : }
947 : :
8447 bruce@momjian.us 948 : 745 : trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
949 : :
950 : : /*
951 : : * Start the trigger definition. Note that the trigger's name should never
952 : : * be schema-qualified, but the trigger rel's name may be.
953 : : */
954 : 745 : initStringInfo(&buf);
955 : :
956 : 745 : tgname = NameStr(trigrec->tgname);
5948 itagaki.takahiro@gma 957 : 1490 : appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
5952 tgl@sss.pgh.pa.us 958 [ - + ]: 745 : OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
959 : : quote_identifier(tgname));
960 : :
8447 bruce@momjian.us 961 [ + + ]: 745 : if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
4569 rhaas@postgresql.org 962 : 284 : appendStringInfoString(&buf, "BEFORE");
5686 tgl@sss.pgh.pa.us 963 [ + + ]: 461 : else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
4569 rhaas@postgresql.org 964 : 445 : appendStringInfoString(&buf, "AFTER");
5686 tgl@sss.pgh.pa.us 965 [ + - ]: 16 : else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
4569 rhaas@postgresql.org 966 : 16 : appendStringInfoString(&buf, "INSTEAD OF");
967 : : else
5686 tgl@sss.pgh.pa.us 968 [ # # ]:UBC 0 : elog(ERROR, "unexpected tgtype value: %d", trigrec->tgtype);
969 : :
8447 bruce@momjian.us 970 [ + + ]:CBC 745 : if (TRIGGER_FOR_INSERT(trigrec->tgtype))
971 : : {
4569 rhaas@postgresql.org 972 : 506 : appendStringInfoString(&buf, " INSERT");
8447 bruce@momjian.us 973 : 506 : findx++;
974 : : }
975 [ + + ]: 745 : if (TRIGGER_FOR_DELETE(trigrec->tgtype))
976 : : {
977 [ + + ]: 117 : if (findx > 0)
4569 rhaas@postgresql.org 978 : 45 : appendStringInfoString(&buf, " OR DELETE");
979 : : else
980 : 72 : appendStringInfoString(&buf, " DELETE");
8447 bruce@momjian.us 981 : 117 : findx++;
982 : : }
983 [ + + ]: 745 : if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
984 : : {
985 [ + + ]: 342 : if (findx > 0)
4569 rhaas@postgresql.org 986 : 175 : appendStringInfoString(&buf, " OR UPDATE");
987 : : else
988 : 167 : appendStringInfoString(&buf, " UPDATE");
6010 tgl@sss.pgh.pa.us 989 : 342 : findx++;
990 : : /* tgattr is first var-width field, so OK to access directly */
6047 991 [ + + ]: 342 : if (trigrec->tgattr.dim1 > 0)
992 : : {
993 : : int i;
994 : :
995 : 44 : appendStringInfoString(&buf, " OF ");
996 [ + + ]: 97 : for (i = 0; i < trigrec->tgattr.dim1; i++)
997 : : {
998 : : char *attname;
999 : :
1000 [ + + ]: 53 : if (i > 0)
1001 : 9 : appendStringInfoString(&buf, ", ");
3004 alvherre@alvh.no-ip. 1002 : 53 : attname = get_attname(trigrec->tgrelid,
1003 : 53 : trigrec->tgattr.values[i], false);
6047 tgl@sss.pgh.pa.us 1004 : 53 : appendStringInfoString(&buf, quote_identifier(attname));
1005 : : }
1006 : : }
1007 : : }
6612 1008 [ - + ]: 745 : if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
1009 : : {
6612 tgl@sss.pgh.pa.us 1010 [ # # ]:UBC 0 : if (findx > 0)
4569 rhaas@postgresql.org 1011 : 0 : appendStringInfoString(&buf, " OR TRUNCATE");
1012 : : else
1013 : 0 : appendStringInfoString(&buf, " TRUNCATE");
6010 tgl@sss.pgh.pa.us 1014 : 0 : findx++;
1015 : : }
1016 : :
1017 : : /*
1018 : : * In non-pretty mode, always schema-qualify the target table name for
1019 : : * safety. In pretty mode, schema-qualify only if not visible.
1020 : : */
5948 itagaki.takahiro@gma 1021 [ + + ]:CBC 1490 : appendStringInfo(&buf, " ON %s ",
1022 : : pretty ?
2990 tgl@sss.pgh.pa.us 1023 : 116 : generate_relation_name(trigrec->tgrelid, NIL) :
1024 : 629 : generate_qualified_relation_name(trigrec->tgrelid));
1025 : :
5952 1026 [ - + ]: 745 : if (OidIsValid(trigrec->tgconstraint))
1027 : : {
6010 tgl@sss.pgh.pa.us 1028 [ # # ]:UBC 0 : if (OidIsValid(trigrec->tgconstrrelid))
5948 itagaki.takahiro@gma 1029 : 0 : appendStringInfo(&buf, "FROM %s ",
1030 : : generate_relation_name(trigrec->tgconstrrelid, NIL));
8447 bruce@momjian.us 1031 [ # # ]: 0 : if (!trigrec->tgdeferrable)
4569 rhaas@postgresql.org 1032 : 0 : appendStringInfoString(&buf, "NOT ");
1033 : 0 : appendStringInfoString(&buf, "DEFERRABLE INITIALLY ");
8447 bruce@momjian.us 1034 [ # # ]: 0 : if (trigrec->tginitdeferred)
4569 rhaas@postgresql.org 1035 : 0 : appendStringInfoString(&buf, "DEFERRED ");
1036 : : else
1037 : 0 : appendStringInfoString(&buf, "IMMEDIATE ");
1038 : : }
1039 : :
3469 kgrittn@postgresql.o 1040 :CBC 745 : value = fastgetattr(ht_trig, Anum_pg_trigger_tgoldtable,
1041 : : tgrel->rd_att, &isnull);
1042 [ + + ]: 745 : if (!isnull)
2755 tgl@sss.pgh.pa.us 1043 : 57 : tgoldtable = NameStr(*DatumGetName(value));
1044 : : else
3469 kgrittn@postgresql.o 1045 : 688 : tgoldtable = NULL;
1046 : 745 : value = fastgetattr(ht_trig, Anum_pg_trigger_tgnewtable,
1047 : : tgrel->rd_att, &isnull);
1048 [ + + ]: 745 : if (!isnull)
2755 tgl@sss.pgh.pa.us 1049 : 62 : tgnewtable = NameStr(*DatumGetName(value));
1050 : : else
3469 kgrittn@postgresql.o 1051 : 683 : tgnewtable = NULL;
1052 [ + + + + ]: 745 : if (tgoldtable != NULL || tgnewtable != NULL)
1053 : : {
1054 : 88 : appendStringInfoString(&buf, "REFERENCING ");
1055 [ + + ]: 88 : if (tgoldtable != NULL)
2755 tgl@sss.pgh.pa.us 1056 : 57 : appendStringInfo(&buf, "OLD TABLE AS %s ",
1057 : : quote_identifier(tgoldtable));
3469 kgrittn@postgresql.o 1058 [ + + ]: 88 : if (tgnewtable != NULL)
2755 tgl@sss.pgh.pa.us 1059 : 62 : appendStringInfo(&buf, "NEW TABLE AS %s ",
1060 : : quote_identifier(tgnewtable));
1061 : : }
1062 : :
8447 bruce@momjian.us 1063 [ + + ]: 745 : if (TRIGGER_FOR_ROW(trigrec->tgtype))
4569 rhaas@postgresql.org 1064 : 556 : appendStringInfoString(&buf, "FOR EACH ROW ");
1065 : : else
1066 : 189 : appendStringInfoString(&buf, "FOR EACH STATEMENT ");
1067 : :
1068 : : /* If the trigger has a WHEN qualification, add that */
6010 tgl@sss.pgh.pa.us 1069 : 745 : value = fastgetattr(ht_trig, Anum_pg_trigger_tgqual,
1070 : : tgrel->rd_att, &isnull);
1071 [ + + ]: 745 : if (!isnull)
1072 : : {
1073 : : Node *qual;
1074 : : char relkind;
1075 : : deparse_context context;
1076 : : deparse_namespace dpns;
1077 : : RangeTblEntry *oldrte;
1078 : : RangeTblEntry *newrte;
1079 : :
1080 : 85 : appendStringInfoString(&buf, "WHEN (");
1081 : :
1082 : 85 : qual = stringToNode(TextDatumGetCString(value));
1083 : :
5551 1084 : 85 : relkind = get_rel_relkind(trigrec->tgrelid);
1085 : :
1086 : : /* Build minimal OLD and NEW RTEs for the rel */
6010 1087 : 85 : oldrte = makeNode(RangeTblEntry);
1088 : 85 : oldrte->rtekind = RTE_RELATION;
1089 : 85 : oldrte->relid = trigrec->tgrelid;
5551 1090 : 85 : oldrte->relkind = relkind;
2774 1091 : 85 : oldrte->rellockmode = AccessShareLock;
4974 1092 : 85 : oldrte->alias = makeAlias("old", NIL);
1093 : 85 : oldrte->eref = oldrte->alias;
5019 1094 : 85 : oldrte->lateral = false;
6010 1095 : 85 : oldrte->inh = false;
1096 : 85 : oldrte->inFromCl = true;
1097 : :
1098 : 85 : newrte = makeNode(RangeTblEntry);
1099 : 85 : newrte->rtekind = RTE_RELATION;
1100 : 85 : newrte->relid = trigrec->tgrelid;
5551 1101 : 85 : newrte->relkind = relkind;
2774 1102 : 85 : newrte->rellockmode = AccessShareLock;
4974 1103 : 85 : newrte->alias = makeAlias("new", NIL);
1104 : 85 : newrte->eref = newrte->alias;
5019 1105 : 85 : newrte->lateral = false;
6010 1106 : 85 : newrte->inh = false;
1107 : 85 : newrte->inFromCl = true;
1108 : :
1109 : : /* Build two-element rtable */
5775 1110 : 85 : memset(&dpns, 0, sizeof(dpns));
6010 1111 : 85 : dpns.rtable = list_make2(oldrte, newrte);
2337 1112 : 85 : dpns.subplans = NIL;
6010 1113 : 85 : dpns.ctes = NIL;
2337 1114 : 85 : dpns.appendrels = NULL;
4974 1115 : 85 : set_rtable_names(&dpns, NIL, NULL);
4873 1116 : 85 : set_simple_column_names(&dpns);
1117 : :
1118 : : /* Set up context with one-deep namespace stack */
6010 1119 : 85 : context.buf = &buf;
1120 : 85 : context.namespaces = list_make1(&dpns);
614 1121 : 85 : context.resultDesc = NULL;
1122 : 85 : context.targetList = NIL;
6010 1123 : 85 : context.windowClause = NIL;
1124 : 85 : context.varprefix = true;
1499 1125 [ + + ]: 85 : context.prettyFlags = GET_PRETTY_FLAGS(pretty);
4880 1126 : 85 : context.wrapColumn = WRAP_COLUMN_DEFAULT;
6010 1127 : 85 : context.indentLevel = PRETTYINDENT_STD;
614 1128 : 85 : context.colNamesVisible = true;
1129 : 85 : context.inGroupBy = false;
1130 : 85 : context.varInOrderBy = false;
2337 1131 : 85 : context.appendparents = NULL;
1132 : :
6010 1133 : 85 : get_rule_expr(qual, &context, false);
1134 : :
4569 rhaas@postgresql.org 1135 : 85 : appendStringInfoString(&buf, ") ");
1136 : : }
1137 : :
2644 peter@eisentraut.org 1138 : 745 : appendStringInfo(&buf, "EXECUTE FUNCTION %s(",
1139 : : generate_function_name(trigrec->tgfoid, 0,
1140 : : NIL, NULL,
1141 : : false, NULL, false));
1142 : :
8386 tgl@sss.pgh.pa.us 1143 [ + + ]: 745 : if (trigrec->tgnargs > 0)
1144 : : {
1145 : : char *p;
1146 : : int i;
1147 : :
6010 1148 : 259 : value = fastgetattr(ht_trig, Anum_pg_trigger_tgargs,
1149 : : tgrel->rd_att, &isnull);
8386 1150 [ - + ]: 259 : if (isnull)
8386 tgl@sss.pgh.pa.us 1151 [ # # ]:UBC 0 : elog(ERROR, "tgargs is null for trigger %u", trigid);
3341 noah@leadboat.com 1152 [ + - ]:CBC 259 : p = (char *) VARDATA_ANY(DatumGetByteaPP(value));
8386 tgl@sss.pgh.pa.us 1153 [ + + ]: 574 : for (i = 0; i < trigrec->tgnargs; i++)
1154 : : {
1155 [ + + ]: 315 : if (i > 0)
4569 rhaas@postgresql.org 1156 : 56 : appendStringInfoString(&buf, ", ");
6450 tgl@sss.pgh.pa.us 1157 : 315 : simple_quote_literal(&buf, p);
1158 : : /* advance p to next string embedded in tgargs */
1159 [ + + ]: 3251 : while (*p)
1160 : 2936 : p++;
7282 1161 : 315 : p++;
1162 : : }
1163 : : }
1164 : :
1165 : : /* We deliberately do not put semi-colon at end */
4569 rhaas@postgresql.org 1166 : 745 : appendStringInfoChar(&buf, ')');
1167 : :
1168 : : /* Clean up */
8386 tgl@sss.pgh.pa.us 1169 : 745 : systable_endscan(tgscan);
1170 : :
2661 andres@anarazel.de 1171 : 745 : table_close(tgrel, AccessShareLock);
1172 : :
6052 peter_e@gmx.net 1173 : 745 : return buf.data;
1174 : : }
1175 : :
1176 : : /* ----------
1177 : : * pg_get_indexdef - Get the definition of an index
1178 : : *
1179 : : * In the extended version, there is a colno argument as well as pretty bool.
1180 : : * if colno == 0, we want a complete index definition.
1181 : : * if colno > 0, we only want the Nth index key's variable or expression.
1182 : : *
1183 : : * Note that the SQL-function versions of this omit any info about the
1184 : : * index tablespace; this is intentional because pg_dump wants it that way.
1185 : : * However pg_get_indexdef_string() includes the index tablespace.
1186 : : * ----------
1187 : : */
1188 : : Datum
9461 tgl@sss.pgh.pa.us 1189 : 3105 : pg_get_indexdef(PG_FUNCTION_ARGS)
1190 : : {
1191 : 3105 : Oid indexrelid = PG_GETARG_OID(0);
1192 : : int prettyFlags;
1193 : : char *res;
1194 : :
4839 1195 : 3105 : prettyFlags = PRETTYFLAG_INDENT;
1196 : :
2847 1197 : 3105 : res = pg_get_indexdef_worker(indexrelid, 0, NULL,
1198 : : false, false,
1199 : : false, false,
1200 : : prettyFlags, true);
1201 : :
3570 rhaas@postgresql.org 1202 [ + + ]: 3105 : if (res == NULL)
1203 : 4 : PG_RETURN_NULL();
1204 : :
1205 : 3101 : PG_RETURN_TEXT_P(string_to_text(res));
1206 : : }
1207 : :
1208 : : Datum
8315 tgl@sss.pgh.pa.us 1209 : 1347 : pg_get_indexdef_ext(PG_FUNCTION_ARGS)
1210 : : {
1211 : 1347 : Oid indexrelid = PG_GETARG_OID(0);
8310 bruce@momjian.us 1212 : 1347 : int32 colno = PG_GETARG_INT32(1);
8315 tgl@sss.pgh.pa.us 1213 : 1347 : bool pretty = PG_GETARG_BOOL(2);
1214 : : int prettyFlags;
1215 : : char *res;
1216 : :
1499 1217 [ + - ]: 1347 : prettyFlags = GET_PRETTY_FLAGS(pretty);
1218 : :
2847 1219 : 1347 : res = pg_get_indexdef_worker(indexrelid, colno, NULL,
1220 : : colno != 0, false,
1221 : : false, false,
1222 : : prettyFlags, true);
1223 : :
3570 rhaas@postgresql.org 1224 [ - + ]: 1347 : if (res == NULL)
3570 rhaas@postgresql.org 1225 :UBC 0 : PG_RETURN_NULL();
1226 : :
3570 rhaas@postgresql.org 1227 :CBC 1347 : PG_RETURN_TEXT_P(string_to_text(res));
1228 : : }
1229 : :
1230 : : /*
1231 : : * Internal version for use by ALTER TABLE.
1232 : : * Includes a tablespace clause in the result.
1233 : : * Returns a palloc'd C string; no pretty-printing.
1234 : : */
1235 : : char *
8035 tgl@sss.pgh.pa.us 1236 : 154 : pg_get_indexdef_string(Oid indexrelid)
1237 : : {
2847 1238 : 154 : return pg_get_indexdef_worker(indexrelid, 0, NULL,
1239 : : false, false,
1240 : : true, true,
1241 : : 0, false);
1242 : : }
1243 : :
1244 : : /* Internal version that just reports the key-column definitions */
1245 : : char *
6121 1246 : 700 : pg_get_indexdef_columns(Oid indexrelid, bool pretty)
1247 : : {
1248 : : int prettyFlags;
1249 : :
1499 1250 [ + - ]: 700 : prettyFlags = GET_PRETTY_FLAGS(pretty);
1251 : :
2847 1252 : 700 : return pg_get_indexdef_worker(indexrelid, 0, NULL,
1253 : : true, true,
1254 : : false, false,
1255 : : prettyFlags, false);
1256 : : }
1257 : :
1258 : : /* Internal version, extensible with flags to control its behavior */
1259 : : char *
36 nathan@postgresql.or 1260 :GNC 4 : pg_get_indexdef_columns_extended(Oid indexrelid, uint16 flags)
1261 : : {
1082 michael@paquier.xyz 1262 :CBC 4 : bool pretty = ((flags & RULE_INDEXDEF_PRETTY) != 0);
1263 : 4 : bool keys_only = ((flags & RULE_INDEXDEF_KEYS_ONLY) != 0);
1264 : : int prettyFlags;
1265 : :
1266 [ + - ]: 4 : prettyFlags = GET_PRETTY_FLAGS(pretty);
1267 : :
1268 : 4 : return pg_get_indexdef_worker(indexrelid, 0, NULL,
1269 : : true, keys_only,
1270 : : false, false,
1271 : : prettyFlags, false);
1272 : : }
1273 : :
1274 : : /*
1275 : : * Internal workhorse to decompile an index definition.
1276 : : *
1277 : : * This is now used for exclusion constraints as well: if excludeOps is not
1278 : : * NULL then it points to an array of exclusion operator OIDs.
1279 : : */
1280 : : static char *
6121 tgl@sss.pgh.pa.us 1281 : 5376 : pg_get_indexdef_worker(Oid indexrelid, int colno,
1282 : : const Oid *excludeOps,
1283 : : bool attrsOnly, bool keysOnly,
1284 : : bool showTblSpc, bool inherits,
1285 : : int prettyFlags, bool missing_ok)
1286 : : {
1287 : : /* might want a separate isConstraint parameter later */
5993 1288 : 5376 : bool isConstraint = (excludeOps != NULL);
1289 : : HeapTuple ht_idx;
1290 : : HeapTuple ht_idxrel;
1291 : : HeapTuple ht_am;
1292 : : Form_pg_index idxrec;
1293 : : Form_pg_class idxrelrec;
1294 : : Form_pg_am amrec;
1295 : : const IndexAmRoutine *amroutine;
1296 : : List *indexprs;
1297 : : ListCell *indexpr_item;
1298 : : List *context;
1299 : : Oid indrelid;
1300 : : int keyno;
1301 : : Datum indcollDatum;
1302 : : Datum indclassDatum;
1303 : : Datum indoptionDatum;
1304 : : oidvector *indcollation;
1305 : : oidvector *indclass;
1306 : : int2vector *indoption;
1307 : : StringInfoData buf;
1308 : : char *str;
1309 : : char *sep;
1310 : :
1311 : : /*
1312 : : * Fetch the pg_index tuple by the Oid of the index
1313 : : */
5924 rhaas@postgresql.org 1314 : 5376 : ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
10077 bruce@momjian.us 1315 [ + + ]: 5376 : if (!HeapTupleIsValid(ht_idx))
1316 : : {
3570 rhaas@postgresql.org 1317 [ + - ]: 4 : if (missing_ok)
1318 : 4 : return NULL;
8318 tgl@sss.pgh.pa.us 1319 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u", indexrelid);
1320 : : }
9842 bruce@momjian.us 1321 :CBC 5372 : idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
1322 : :
8768 tgl@sss.pgh.pa.us 1323 : 5372 : indrelid = idxrec->indrelid;
1324 [ - + ]: 5372 : Assert(indexrelid == idxrec->indexrelid);
1325 : :
1326 : : /* Must get indcollation, indclass, and indoption the hard way */
1137 dgustafsson@postgres 1327 : 5372 : indcollDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1328 : : Anum_pg_index_indcollation);
5565 peter_e@gmx.net 1329 : 5372 : indcollation = (oidvector *) DatumGetPointer(indcollDatum);
1330 : :
1137 dgustafsson@postgres 1331 : 5372 : indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1332 : : Anum_pg_index_indclass);
7707 tgl@sss.pgh.pa.us 1333 : 5372 : indclass = (oidvector *) DatumGetPointer(indclassDatum);
1334 : :
1137 dgustafsson@postgres 1335 : 5372 : indoptionDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1336 : : Anum_pg_index_indoption);
7056 tgl@sss.pgh.pa.us 1337 : 5372 : indoption = (int2vector *) DatumGetPointer(indoptionDatum);
1338 : :
1339 : : /*
1340 : : * Fetch the pg_class tuple of the index relation
1341 : : */
5924 rhaas@postgresql.org 1342 : 5372 : ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexrelid));
10077 bruce@momjian.us 1343 [ - + ]: 5372 : if (!HeapTupleIsValid(ht_idxrel))
8318 tgl@sss.pgh.pa.us 1344 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", indexrelid);
9842 bruce@momjian.us 1345 :CBC 5372 : idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
1346 : :
1347 : : /*
1348 : : * Fetch the pg_am tuple of the index' access method
1349 : : */
5924 rhaas@postgresql.org 1350 : 5372 : ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
8841 tgl@sss.pgh.pa.us 1351 [ - + ]: 5372 : if (!HeapTupleIsValid(ht_am))
8318 tgl@sss.pgh.pa.us 1352 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for access method %u",
1353 : : idxrelrec->relam);
8841 tgl@sss.pgh.pa.us 1354 :CBC 5372 : amrec = (Form_pg_am) GETSTRUCT(ht_am);
1355 : :
1356 : : /* Fetch the index AM's API struct */
3761 1357 : 5372 : amroutine = GetIndexAmRoutine(amrec->amhandler);
1358 : :
1359 : : /*
1360 : : * Get the index expressions, if any. (NOTE: we do not use the relcache
1361 : : * versions of the expressions and predicate, because we want to display
1362 : : * non-const-folded expressions.)
1363 : : */
2960 andrew@dunslane.net 1364 [ + + ]: 5372 : if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs, NULL))
1365 : : {
1366 : : Datum exprsDatum;
1367 : : char *exprsString;
1368 : :
1137 dgustafsson@postgres 1369 : 389 : exprsDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1370 : : Anum_pg_index_indexprs);
6615 tgl@sss.pgh.pa.us 1371 : 389 : exprsString = TextDatumGetCString(exprsDatum);
8378 1372 : 389 : indexprs = (List *) stringToNode(exprsString);
1373 : 389 : pfree(exprsString);
1374 : : }
1375 : : else
1376 : 4983 : indexprs = NIL;
1377 : :
8014 neilc@samurai.com 1378 : 5372 : indexpr_item = list_head(indexprs);
1379 : :
5663 tgl@sss.pgh.pa.us 1380 : 5372 : context = deparse_context_for(get_relation_name(indrelid), indrelid);
1381 : :
1382 : : /*
1383 : : * Start the index definition. Note that the index's name should never be
1384 : : * schema-qualified, but the indexed rel's name may be.
1385 : : */
9712 1386 : 5372 : initStringInfo(&buf);
1387 : :
6121 1388 [ + + ]: 5372 : if (!attrsOnly)
1389 : : {
5993 1390 [ + + ]: 4362 : if (!isConstraint)
3028 alvherre@alvh.no-ip. 1391 : 8592 : appendStringInfo(&buf, "CREATE %sINDEX %s ON %s%s USING %s (",
5993 tgl@sss.pgh.pa.us 1392 [ + + ]: 4296 : idxrec->indisunique ? "UNIQUE " : "",
1393 : 4296 : quote_identifier(NameStr(idxrelrec->relname)),
3028 alvherre@alvh.no-ip. 1394 [ + + ]: 4296 : idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
1395 [ + + ]: 403 : && !inherits ? "ONLY " : "",
2990 tgl@sss.pgh.pa.us 1396 [ + + ]: 4296 : (prettyFlags & PRETTYFLAG_SCHEMA) ?
1397 : 1041 : generate_relation_name(indrelid, NIL) :
1398 : 3255 : generate_qualified_relation_name(indrelid),
5993 1399 : 4296 : quote_identifier(NameStr(amrec->amname)));
1400 : : else /* currently, must be EXCLUDE constraint */
1401 : 66 : appendStringInfo(&buf, "EXCLUDE USING %s (",
1402 : 66 : quote_identifier(NameStr(amrec->amname)));
1403 : : }
1404 : :
1405 : : /*
1406 : : * Report the indexed attributes
1407 : : */
10077 bruce@momjian.us 1408 : 5372 : sep = "";
8378 tgl@sss.pgh.pa.us 1409 [ + + ]: 13347 : for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1410 : : {
7707 1411 : 8040 : AttrNumber attnum = idxrec->indkey.values[keyno];
1412 : : Oid keycoltype;
1413 : : Oid keycolcollation;
1414 : :
1415 : : /*
1416 : : * Ignore non-key attributes if told to.
1417 : : */
2847 1418 [ + + + + ]: 8040 : if (keysOnly && keyno >= idxrec->indnkeyatts)
2950 teodor@sigaev.ru 1419 : 65 : break;
1420 : :
1421 : : /* Otherwise, print INCLUDE to divide key and non-key attrs. */
2847 tgl@sss.pgh.pa.us 1422 [ + + + + ]: 7975 : if (!colno && keyno == idxrec->indnkeyatts)
1423 : : {
2950 teodor@sigaev.ru 1424 : 148 : appendStringInfoString(&buf, ") INCLUDE (");
1425 : 148 : sep = "";
1426 : : }
1427 : :
8315 tgl@sss.pgh.pa.us 1428 [ + + ]: 7975 : if (!colno)
7675 neilc@samurai.com 1429 : 7553 : appendStringInfoString(&buf, sep);
10077 bruce@momjian.us 1430 : 7975 : sep = ", ";
1431 : :
8378 tgl@sss.pgh.pa.us 1432 [ + + ]: 7975 : if (attnum != 0)
1433 : : {
1434 : : /* Simple index column */
1435 : : char *attname;
1436 : : int32 keycoltypmod;
1437 : :
3004 alvherre@alvh.no-ip. 1438 : 7509 : attname = get_attname(indrelid, attnum, false);
8310 bruce@momjian.us 1439 [ + + + + ]: 7509 : if (!colno || colno == keyno + 1)
8130 neilc@samurai.com 1440 : 7401 : appendStringInfoString(&buf, quote_identifier(attname));
5519 tgl@sss.pgh.pa.us 1441 : 7509 : get_atttypetypmodcoll(indrelid, attnum,
1442 : : &keycoltype, &keycoltypmod,
1443 : : &keycolcollation);
1444 : : }
1445 : : else
1446 : : {
1447 : : /* expressional index */
1448 : : Node *indexkey;
1449 : :
8014 neilc@samurai.com 1450 [ - + ]: 466 : if (indexpr_item == NULL)
8378 tgl@sss.pgh.pa.us 1451 [ # # ]:UBC 0 : elog(ERROR, "too few entries in indexprs list");
8014 neilc@samurai.com 1452 :CBC 466 : indexkey = (Node *) lfirst(indexpr_item);
2486 tgl@sss.pgh.pa.us 1453 : 466 : indexpr_item = lnext(indexprs, indexpr_item);
1454 : : /* Deparse */
8315 1455 : 466 : str = deparse_expression_pretty(indexkey, context, false, false,
1456 : : prettyFlags, 0);
8310 bruce@momjian.us 1457 [ + + + + ]: 466 : if (!colno || colno == keyno + 1)
1458 : : {
1459 : : /* Need parens if it's not a bare function call */
3218 tgl@sss.pgh.pa.us 1460 [ + + ]: 458 : if (looks_like_function(indexkey))
8130 neilc@samurai.com 1461 : 33 : appendStringInfoString(&buf, str);
1462 : : else
8315 tgl@sss.pgh.pa.us 1463 : 425 : appendStringInfo(&buf, "(%s)", str);
1464 : : }
8378 1465 : 466 : keycoltype = exprType(indexkey);
5521 1466 : 466 : keycolcollation = exprCollation(indexkey);
1467 : : }
1468 : :
1469 : : /* Print additional decoration for (selected) key columns */
2847 1470 [ + + + + : 7975 : if (!attrsOnly && keyno < idxrec->indnkeyatts &&
- + ]
2847 tgl@sss.pgh.pa.us 1471 [ # # ]:UBC 0 : (!colno || colno == keyno + 1))
1472 : : {
2585 tgl@sss.pgh.pa.us 1473 :CBC 6296 : int16 opt = indoption->values[keyno];
1474 : 6296 : Oid indcoll = indcollation->values[keyno];
2227 akorotkov@postgresql 1475 : 6296 : Datum attoptions = get_attoptions(indexrelid, keyno + 1);
1476 : 6296 : bool has_options = attoptions != (Datum) 0;
1477 : :
1478 : : /* Add collation, if not default for column */
5521 tgl@sss.pgh.pa.us 1479 [ + + + + ]: 6296 : if (OidIsValid(indcoll) && indcoll != keycolcollation)
1480 : 62 : appendStringInfo(&buf, " COLLATE %s",
1481 : : generate_collation_name((indcoll)));
1482 : :
1483 : : /* Add the operator class name, if not default */
2227 akorotkov@postgresql 1484 [ + + ]: 6296 : get_opclass_name(indclass->values[keyno],
1485 : : has_options ? InvalidOid : keycoltype, &buf);
1486 : :
1487 [ + + ]: 6296 : if (has_options)
1488 : : {
1489 : 22 : appendStringInfoString(&buf, " (");
1490 : 22 : get_reloptions(&buf, attoptions);
1491 : 22 : appendStringInfoChar(&buf, ')');
1492 : : }
1493 : :
1494 : : /* Add options if relevant */
3761 tgl@sss.pgh.pa.us 1495 [ + + ]: 6296 : if (amroutine->amcanorder)
1496 : : {
1497 : : /* if it supports sort ordering, report DESC and NULLS opts */
6711 1498 [ - + ]: 5162 : if (opt & INDOPTION_DESC)
1499 : : {
4569 rhaas@postgresql.org 1500 :UBC 0 : appendStringInfoString(&buf, " DESC");
1501 : : /* NULLS FIRST is the default in this case */
6711 tgl@sss.pgh.pa.us 1502 [ # # ]: 0 : if (!(opt & INDOPTION_NULLS_FIRST))
4569 rhaas@postgresql.org 1503 : 0 : appendStringInfoString(&buf, " NULLS LAST");
1504 : : }
1505 : : else
1506 : : {
6711 tgl@sss.pgh.pa.us 1507 [ - + ]:CBC 5162 : if (opt & INDOPTION_NULLS_FIRST)
4569 rhaas@postgresql.org 1508 :UBC 0 : appendStringInfoString(&buf, " NULLS FIRST");
1509 : : }
1510 : : }
1511 : :
1512 : : /* Add the exclusion operator if relevant */
5993 tgl@sss.pgh.pa.us 1513 [ + + ]:CBC 6296 : if (excludeOps != NULL)
1514 : 76 : appendStringInfo(&buf, " WITH %s",
1515 : 76 : generate_operator_name(excludeOps[keyno],
1516 : : keycoltype,
1517 : : keycoltype));
1518 : : }
1519 : : }
1520 : :
6121 1521 [ + + ]: 5372 : if (!attrsOnly)
1522 : : {
8310 bruce@momjian.us 1523 : 4362 : appendStringInfoChar(&buf, ')');
1524 : :
1552 peter@eisentraut.org 1525 [ + + ]: 4362 : if (idxrec->indnullsnotdistinct)
1337 drowley@postgresql.o 1526 : 8 : appendStringInfoString(&buf, " NULLS NOT DISTINCT");
1527 : :
1528 : : /*
1529 : : * If it has options, append "WITH (options)"
1530 : : */
7246 tgl@sss.pgh.pa.us 1531 : 4362 : str = flatten_reloptions(indexrelid);
1532 [ + + ]: 4362 : if (str)
1533 : : {
1534 : 105 : appendStringInfo(&buf, " WITH (%s)", str);
1535 : 105 : pfree(str);
1536 : : }
1537 : :
1538 : : /*
1539 : : * Print tablespace, but only if requested
1540 : : */
6779 1541 [ + + ]: 4362 : if (showTblSpc)
1542 : : {
1543 : : Oid tblspc;
1544 : :
1545 : 154 : tblspc = get_rel_tablespace(indexrelid);
2567 alvherre@alvh.no-ip. 1546 [ + + ]: 154 : if (OidIsValid(tblspc))
1547 : : {
1548 [ - + ]: 35 : if (isConstraint)
2567 alvherre@alvh.no-ip. 1549 :UBC 0 : appendStringInfoString(&buf, " USING INDEX");
2567 alvherre@alvh.no-ip. 1550 :CBC 35 : appendStringInfo(&buf, " TABLESPACE %s",
1551 : 35 : quote_identifier(get_tablespace_name(tblspc)));
1552 : : }
1553 : : }
1554 : :
1555 : : /*
1556 : : * If it's a partial index, decompile and append the predicate
1557 : : */
2960 andrew@dunslane.net 1558 [ + + ]: 4362 : if (!heap_attisnull(ht_idx, Anum_pg_index_indpred, NULL))
1559 : : {
1560 : : Node *node;
1561 : : Datum predDatum;
1562 : : char *predString;
1563 : :
1564 : : /* Convert text string to node tree */
1137 dgustafsson@postgres 1565 : 204 : predDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1566 : : Anum_pg_index_indpred);
6615 tgl@sss.pgh.pa.us 1567 : 204 : predString = TextDatumGetCString(predDatum);
8315 1568 : 204 : node = (Node *) stringToNode(predString);
1569 : 204 : pfree(predString);
1570 : :
1571 : : /* Deparse */
1572 : 204 : str = deparse_expression_pretty(node, context, false, false,
1573 : : prettyFlags, 0);
5993 1574 [ + + ]: 204 : if (isConstraint)
1575 : 28 : appendStringInfo(&buf, " WHERE (%s)", str);
1576 : : else
1577 : 176 : appendStringInfo(&buf, " WHERE %s", str);
1578 : : }
1579 : : }
1580 : :
1581 : : /* Clean up */
9301 1582 : 5372 : ReleaseSysCache(ht_idx);
1583 : 5372 : ReleaseSysCache(ht_idxrel);
8841 1584 : 5372 : ReleaseSysCache(ht_am);
1585 : :
8035 1586 : 5372 : return buf.data;
1587 : : }
1588 : :
1589 : : /* ----------
1590 : : * pg_get_querydef
1591 : : *
1592 : : * Public entry point to deparse one query parsetree.
1593 : : * The pretty flags are determined by GET_PRETTY_FLAGS(pretty).
1594 : : *
1595 : : * The result is a palloc'd C string.
1596 : : * ----------
1597 : : */
1598 : : char *
1499 tgl@sss.pgh.pa.us 1599 :UBC 0 : pg_get_querydef(Query *query, bool pretty)
1600 : : {
1601 : : StringInfoData buf;
1602 : : int prettyFlags;
1603 : :
1604 [ # # ]: 0 : prettyFlags = GET_PRETTY_FLAGS(pretty);
1605 : :
1606 : 0 : initStringInfo(&buf);
1607 : :
1445 1608 : 0 : get_query_def(query, &buf, NIL, NULL, true,
1609 : : prettyFlags, WRAP_COLUMN_DEFAULT, 0);
1610 : :
1499 1611 : 0 : return buf.data;
1612 : : }
1613 : :
1614 : : /*
1615 : : * pg_get_propgraphdef - get the definition of a property graph
1616 : : */
1617 : : Datum
50 peter@eisentraut.org 1618 :GNC 125 : pg_get_propgraphdef(PG_FUNCTION_ARGS)
1619 : : {
1620 : 125 : Oid pgrelid = PG_GETARG_OID(0);
1621 : : StringInfoData buf;
1622 : : HeapTuple classtup;
1623 : : Form_pg_class classform;
1624 : : char *name;
1625 : : char *nsp;
1626 : :
1627 : 125 : initStringInfo(&buf);
1628 : :
1629 : 125 : classtup = SearchSysCache1(RELOID, ObjectIdGetDatum(pgrelid));
1630 [ - + ]: 125 : if (!HeapTupleIsValid(classtup))
50 peter@eisentraut.org 1631 :UNC 0 : PG_RETURN_NULL();
1632 : :
50 peter@eisentraut.org 1633 :GNC 125 : classform = (Form_pg_class) GETSTRUCT(classtup);
1634 : 125 : name = NameStr(classform->relname);
1635 : :
1636 [ + + ]: 125 : if (classform->relkind != RELKIND_PROPGRAPH)
1637 [ + - ]: 4 : ereport(ERROR,
1638 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1639 : : errmsg("\"%s\" is not a property graph", name)));
1640 : :
1641 : 121 : nsp = get_namespace_name(classform->relnamespace);
1642 : :
1643 : 121 : appendStringInfo(&buf, "CREATE PROPERTY GRAPH %s",
1644 : : quote_qualified_identifier(nsp, name));
1645 : :
1646 : 121 : ReleaseSysCache(classtup);
1647 : :
1648 : 121 : make_propgraphdef_elements(&buf, pgrelid, PGEKIND_VERTEX);
1649 : 121 : make_propgraphdef_elements(&buf, pgrelid, PGEKIND_EDGE);
1650 : :
1651 : 121 : PG_RETURN_TEXT_P(string_to_text(buf.data));
1652 : : }
1653 : :
1654 : : /*
1655 : : * Generates a VERTEX TABLES (...) or EDGE TABLES (...) clause. Pass in the
1656 : : * property graph relation OID and the element kind (vertex or edge). Result
1657 : : * is appended to buf.
1658 : : */
1659 : : static void
1660 : 242 : make_propgraphdef_elements(StringInfo buf, Oid pgrelid, char pgekind)
1661 : : {
1662 : : Relation pgerel;
1663 : : ScanKeyData scankey[1];
1664 : : SysScanDesc scan;
1665 : : bool first;
1666 : : HeapTuple tup;
1667 : :
1668 : 242 : pgerel = table_open(PropgraphElementRelationId, AccessShareLock);
1669 : :
1670 : 242 : ScanKeyInit(&scankey[0],
1671 : : Anum_pg_propgraph_element_pgepgid,
1672 : : BTEqualStrategyNumber, F_OIDEQ,
1673 : : ObjectIdGetDatum(pgrelid));
1674 : :
1675 : 242 : scan = systable_beginscan(pgerel, PropgraphElementAliasIndexId, true, NULL, 1, scankey);
1676 : :
1677 : 242 : first = true;
1678 [ + + ]: 992 : while ((tup = systable_getnext(scan)))
1679 : : {
1680 : 750 : Form_pg_propgraph_element pgeform = (Form_pg_propgraph_element) GETSTRUCT(tup);
1681 : : char *relname;
1682 : : Datum datum;
1683 : : bool isnull;
1684 : :
1685 [ + + ]: 750 : if (pgeform->pgekind != pgekind)
1686 : 375 : continue;
1687 : :
1688 [ + + ]: 375 : if (first)
1689 : : {
1690 [ + + ]: 161 : appendStringInfo(buf, "\n %s TABLES (\n", pgekind == PGEKIND_VERTEX ? "VERTEX" : "EDGE");
1691 : 161 : first = false;
1692 : : }
1693 : : else
22 drowley@postgresql.o 1694 : 214 : appendStringInfoString(buf, ",\n");
1695 : :
50 peter@eisentraut.org 1696 : 375 : relname = get_rel_name(pgeform->pgerelid);
1697 [ + - + - ]: 375 : if (relname && strcmp(relname, NameStr(pgeform->pgealias)) == 0)
1698 : 375 : appendStringInfo(buf, " %s",
1699 : : generate_relation_name(pgeform->pgerelid, NIL));
1700 : : else
50 peter@eisentraut.org 1701 :UNC 0 : appendStringInfo(buf, " %s AS %s",
1702 : : generate_relation_name(pgeform->pgerelid, NIL),
1703 : 0 : quote_identifier(NameStr(pgeform->pgealias)));
1704 : :
50 peter@eisentraut.org 1705 :GNC 375 : datum = heap_getattr(tup, Anum_pg_propgraph_element_pgekey, RelationGetDescr(pgerel), &isnull);
1706 [ + - ]: 375 : if (!isnull)
1707 : : {
1708 : 375 : appendStringInfoString(buf, " KEY (");
1709 : 375 : decompile_column_index_array(datum, pgeform->pgerelid, false, buf);
22 drowley@postgresql.o 1710 : 375 : appendStringInfoChar(buf, ')');
1711 : : }
1712 : : else
50 peter@eisentraut.org 1713 [ # # ]:UNC 0 : elog(ERROR, "null pgekey for element %u", pgeform->oid);
1714 : :
50 peter@eisentraut.org 1715 [ + + ]:GNC 375 : if (pgekind == PGEKIND_EDGE)
1716 : : {
1717 : : Datum srckey;
1718 : : Datum srcref;
1719 : : Datum destkey;
1720 : : Datum destref;
1721 : : HeapTuple tup2;
1722 : : Form_pg_propgraph_element pgeform2;
1723 : :
1724 : 163 : datum = heap_getattr(tup, Anum_pg_propgraph_element_pgesrckey, RelationGetDescr(pgerel), &isnull);
1725 [ - + ]: 163 : srckey = isnull ? 0 : datum;
1726 : 163 : datum = heap_getattr(tup, Anum_pg_propgraph_element_pgesrcref, RelationGetDescr(pgerel), &isnull);
1727 [ - + ]: 163 : srcref = isnull ? 0 : datum;
1728 : 163 : datum = heap_getattr(tup, Anum_pg_propgraph_element_pgedestkey, RelationGetDescr(pgerel), &isnull);
1729 [ - + ]: 163 : destkey = isnull ? 0 : datum;
1730 : 163 : datum = heap_getattr(tup, Anum_pg_propgraph_element_pgedestref, RelationGetDescr(pgerel), &isnull);
1731 [ - + ]: 163 : destref = isnull ? 0 : datum;
1732 : :
1733 : 163 : appendStringInfoString(buf, " SOURCE");
1734 : 163 : tup2 = SearchSysCache1(PROPGRAPHELOID, ObjectIdGetDatum(pgeform->pgesrcvertexid));
1735 [ - + ]: 163 : if (!tup2)
50 peter@eisentraut.org 1736 [ # # ]:UNC 0 : elog(ERROR, "cache lookup failed for property graph element %u", pgeform->pgesrcvertexid);
50 peter@eisentraut.org 1737 :GNC 163 : pgeform2 = (Form_pg_propgraph_element) GETSTRUCT(tup2);
1738 [ + - ]: 163 : if (srckey)
1739 : : {
1740 : 163 : appendStringInfoString(buf, " KEY (");
1741 : 163 : decompile_column_index_array(srckey, pgeform->pgerelid, false, buf);
1742 : 163 : appendStringInfo(buf, ") REFERENCES %s (", quote_identifier(NameStr(pgeform2->pgealias)));
1743 : 163 : decompile_column_index_array(srcref, pgeform2->pgerelid, false, buf);
22 drowley@postgresql.o 1744 : 163 : appendStringInfoChar(buf, ')');
1745 : : }
1746 : : else
50 peter@eisentraut.org 1747 :UNC 0 : appendStringInfo(buf, " %s ", quote_identifier(NameStr(pgeform2->pgealias)));
50 peter@eisentraut.org 1748 :GNC 163 : ReleaseSysCache(tup2);
1749 : :
1750 : 163 : appendStringInfoString(buf, " DESTINATION");
1751 : 163 : tup2 = SearchSysCache1(PROPGRAPHELOID, ObjectIdGetDatum(pgeform->pgedestvertexid));
1752 [ - + ]: 163 : if (!tup2)
50 peter@eisentraut.org 1753 [ # # ]:UNC 0 : elog(ERROR, "cache lookup failed for property graph element %u", pgeform->pgedestvertexid);
50 peter@eisentraut.org 1754 :GNC 163 : pgeform2 = (Form_pg_propgraph_element) GETSTRUCT(tup2);
1755 [ + - ]: 163 : if (destkey)
1756 : : {
1757 : 163 : appendStringInfoString(buf, " KEY (");
1758 : 163 : decompile_column_index_array(destkey, pgeform->pgerelid, false, buf);
1759 : 163 : appendStringInfo(buf, ") REFERENCES %s (", quote_identifier(NameStr(pgeform2->pgealias)));
1760 : 163 : decompile_column_index_array(destref, pgeform2->pgerelid, false, buf);
22 drowley@postgresql.o 1761 : 163 : appendStringInfoChar(buf, ')');
1762 : : }
1763 : : else
50 peter@eisentraut.org 1764 :UNC 0 : appendStringInfo(buf, " %s", quote_identifier(NameStr(pgeform2->pgealias)));
50 peter@eisentraut.org 1765 :GNC 163 : ReleaseSysCache(tup2);
1766 : : }
1767 : :
1768 : 375 : make_propgraphdef_labels(buf, pgeform->oid, NameStr(pgeform->pgealias), pgeform->pgerelid);
1769 : : }
1770 [ + + ]: 242 : if (!first)
22 drowley@postgresql.o 1771 : 161 : appendStringInfoString(buf, "\n )");
1772 : :
50 peter@eisentraut.org 1773 : 242 : systable_endscan(scan);
1774 : 242 : table_close(pgerel, AccessShareLock);
1775 : 242 : }
1776 : :
1777 : : struct oid_str_pair
1778 : : {
1779 : : Oid oid;
1780 : : char *str;
1781 : : };
1782 : :
1783 : : static int
49 1784 : 106 : list_oid_str_pair_cmp_by_str(const ListCell *p1, const ListCell *p2)
1785 : : {
1786 : 106 : struct oid_str_pair *v1 = lfirst(p1);
1787 : 106 : struct oid_str_pair *v2 = lfirst(p2);
1788 : :
1789 : 106 : return strcmp(v1->str, v2->str);
1790 : : }
1791 : :
1792 : : /*
1793 : : * Generates label and properties list. Pass in the element OID, the element
1794 : : * alias, and the graph relation OID. Result is appended to buf.
1795 : : */
1796 : : static void
50 1797 : 375 : make_propgraphdef_labels(StringInfo buf, Oid elid, const char *elalias, Oid elrelid)
1798 : : {
1799 : : Relation pglrel;
1800 : : ScanKeyData scankey[1];
1801 : : SysScanDesc scan;
1802 : : int count;
1803 : : HeapTuple tup;
49 1804 : 375 : List *label_list = NIL;
1805 : :
50 1806 : 375 : pglrel = table_open(PropgraphElementLabelRelationId, AccessShareLock);
1807 : :
1808 : 375 : ScanKeyInit(&scankey[0],
1809 : : Anum_pg_propgraph_element_label_pgelelid,
1810 : : BTEqualStrategyNumber, F_OIDEQ,
1811 : : ObjectIdGetDatum(elid));
1812 : :
1813 : : /*
1814 : : * We want to output the labels in a deterministic order. So we first
1815 : : * read all the data, then sort, then print it.
1816 : : */
1817 : 375 : scan = systable_beginscan(pglrel, PropgraphElementLabelElementLabelIndexId, true, NULL, 1, scankey);
1818 : :
1819 [ + + ]: 856 : while ((tup = systable_getnext(scan)))
1820 : : {
49 1821 : 481 : Form_pg_propgraph_element_label pgelform = (Form_pg_propgraph_element_label) GETSTRUCT(tup);
1822 : : struct oid_str_pair *osp;
1823 : :
1824 : 481 : osp = palloc_object(struct oid_str_pair);
1825 : 481 : osp->oid = pgelform->oid;
1826 : 481 : osp->str = get_propgraph_label_name(pgelform->pgellabelid);
1827 : :
1828 : 481 : label_list = lappend(label_list, osp);
1829 : : }
1830 : :
50 1831 : 375 : systable_endscan(scan);
49 1832 : 375 : table_close(pglrel, AccessShareLock);
1833 : :
1834 : 375 : count = list_length(label_list);
1835 : :
1836 : : /* Each element has at least one label. */
1837 [ - + ]: 375 : Assert(count > 0);
1838 : :
1839 : : /*
1840 : : * It is enough for the comparison function to compare just labels, since
1841 : : * all the labels of an element table should have distinct names.
1842 : : */
1843 : 375 : list_sort(label_list, list_oid_str_pair_cmp_by_str);
1844 : :
1845 [ + - + + : 1231 : foreach_ptr(struct oid_str_pair, osp, label_list)
+ + ]
1846 : : {
1847 [ + + ]: 481 : if (strcmp(osp->str, elalias) == 0)
1848 : : {
1849 : : /* If the default label is the only label, don't print anything. */
50 1850 [ + + ]: 259 : if (count != 1)
22 drowley@postgresql.o 1851 : 30 : appendStringInfoString(buf, " DEFAULT LABEL");
1852 : : }
1853 : : else
49 peter@eisentraut.org 1854 : 222 : appendStringInfo(buf, " LABEL %s", quote_identifier(osp->str));
1855 : :
1856 : 481 : make_propgraphdef_properties(buf, osp->oid, elrelid);
1857 : : }
50 1858 : 375 : }
1859 : :
1860 : : /*
1861 : : * Helper function for make_propgraphdef_properties(): Sort (propname, expr)
1862 : : * pairs by name.
1863 : : */
1864 : : static int
1865 : 748 : propdata_by_name_cmp(const ListCell *a, const ListCell *b)
1866 : : {
1867 : 748 : List *la = lfirst_node(List, a);
1868 : 748 : List *lb = lfirst_node(List, b);
1869 : 748 : char *pna = strVal(linitial(la));
1870 : 748 : char *pnb = strVal(linitial(lb));
1871 : :
1872 : 748 : return strcmp(pna, pnb);
1873 : : }
1874 : :
1875 : : /*
1876 : : * Generates element table properties clause (PROPERTIES (...) or NO
1877 : : * PROPERTIES). Pass in label OID and element table OID. Result is appended
1878 : : * to buf.
1879 : : */
1880 : : static void
1881 : 481 : make_propgraphdef_properties(StringInfo buf, Oid ellabelid, Oid elrelid)
1882 : : {
1883 : : Relation plprel;
1884 : : ScanKeyData scankey[1];
1885 : : SysScanDesc scan;
1886 : : HeapTuple tup;
1887 : 481 : List *outlist = NIL;
1888 : :
1889 : 481 : plprel = table_open(PropgraphLabelPropertyRelationId, AccessShareLock);
1890 : :
1891 : 481 : ScanKeyInit(&scankey[0],
1892 : : Anum_pg_propgraph_label_property_plpellabelid,
1893 : : BTEqualStrategyNumber, F_OIDEQ,
1894 : : ObjectIdGetDatum(ellabelid));
1895 : :
1896 : : /*
1897 : : * We want to output the properties in a deterministic order. So we first
1898 : : * read all the data, then sort, then print it.
1899 : : */
1900 : 481 : scan = systable_beginscan(plprel, PropgraphLabelPropertyLabelPropIndexId, true, NULL, 1, scankey);
1901 : :
1902 [ + + ]: 1567 : while ((tup = systable_getnext(scan)))
1903 : : {
1904 : 1086 : Form_pg_propgraph_label_property plpform = (Form_pg_propgraph_label_property) GETSTRUCT(tup);
1905 : : Datum exprDatum;
1906 : : bool isnull;
1907 : : char *tmp;
1908 : : Node *expr;
1909 : : char *propname;
1910 : :
1911 : 1086 : exprDatum = heap_getattr(tup, Anum_pg_propgraph_label_property_plpexpr, RelationGetDescr(plprel), &isnull);
1912 [ - + ]: 1086 : Assert(!isnull);
1913 : 1086 : tmp = TextDatumGetCString(exprDatum);
1914 : 1086 : expr = stringToNode(tmp);
1915 : 1086 : pfree(tmp);
1916 : :
1917 : 1086 : propname = get_propgraph_property_name(plpform->plppropid);
1918 : :
1919 : 1086 : outlist = lappend(outlist, list_make2(makeString(propname), expr));
1920 : : }
1921 : :
1922 : 481 : systable_endscan(scan);
1923 : 481 : table_close(plprel, AccessShareLock);
1924 : :
1925 : 481 : list_sort(outlist, propdata_by_name_cmp);
1926 : :
1927 [ + + ]: 481 : if (outlist)
1928 : : {
1929 : : List *context;
1930 : : ListCell *lc;
1931 : 472 : bool first = true;
1932 : :
1933 : 472 : context = deparse_context_for(get_relation_name(elrelid), elrelid);
1934 : :
22 drowley@postgresql.o 1935 : 472 : appendStringInfoString(buf, " PROPERTIES (");
1936 : :
50 peter@eisentraut.org 1937 [ + - + + : 1558 : foreach(lc, outlist)
+ + ]
1938 : : {
1939 : 1086 : List *data = lfirst_node(List, lc);
1940 : 1086 : char *propname = strVal(linitial(data));
1941 : 1086 : Node *expr = lsecond(data);
1942 : :
1943 [ + + ]: 1086 : if (first)
1944 : 472 : first = false;
1945 : : else
22 drowley@postgresql.o 1946 : 614 : appendStringInfoString(buf, ", ");
1947 : :
50 peter@eisentraut.org 1948 [ + + + + ]: 1086 : if (IsA(expr, Var) && strcmp(propname, get_attname(elrelid, castNode(Var, expr)->varattno, false)) == 0)
22 drowley@postgresql.o 1949 : 885 : appendStringInfoString(buf, quote_identifier(propname));
1950 : : else
50 peter@eisentraut.org 1951 : 201 : appendStringInfo(buf, "%s AS %s",
1952 : : deparse_expression_pretty(expr, context, false, false, 0, 0),
1953 : : quote_identifier(propname));
1954 : : }
1955 : :
22 drowley@postgresql.o 1956 : 472 : appendStringInfoChar(buf, ')');
1957 : : }
1958 : : else
1959 : 9 : appendStringInfoString(buf, " NO PROPERTIES");
50 peter@eisentraut.org 1960 : 481 : }
1961 : :
1962 : : /*
1963 : : * pg_get_statisticsobjdef
1964 : : * Get the definition of an extended statistics object
1965 : : */
1966 : : Datum
3278 tgl@sss.pgh.pa.us 1967 :CBC 155 : pg_get_statisticsobjdef(PG_FUNCTION_ARGS)
1968 : : {
3329 alvherre@alvh.no-ip. 1969 : 155 : Oid statextid = PG_GETARG_OID(0);
1970 : : char *res;
1971 : :
1866 tomas.vondra@postgre 1972 : 155 : res = pg_get_statisticsobj_worker(statextid, false, true);
1973 : :
1974 [ + + ]: 155 : if (res == NULL)
1975 : 4 : PG_RETURN_NULL();
1976 : :
1977 : 151 : PG_RETURN_TEXT_P(string_to_text(res));
1978 : : }
1979 : :
1980 : : /*
1981 : : * Internal version for use by ALTER TABLE.
1982 : : * Returns a palloc'd C string; no pretty-printing.
1983 : : */
1984 : : char *
1985 : 53 : pg_get_statisticsobjdef_string(Oid statextid)
1986 : : {
1987 : 53 : return pg_get_statisticsobj_worker(statextid, false, false);
1988 : : }
1989 : :
1990 : : /*
1991 : : * pg_get_statisticsobjdef_columns
1992 : : * Get columns and expressions for an extended statistics object
1993 : : */
1994 : : Datum
1995 : 284 : pg_get_statisticsobjdef_columns(PG_FUNCTION_ARGS)
1996 : : {
1997 : 284 : Oid statextid = PG_GETARG_OID(0);
1998 : : char *res;
1999 : :
2000 : 284 : res = pg_get_statisticsobj_worker(statextid, true, true);
2001 : :
3329 alvherre@alvh.no-ip. 2002 [ - + ]: 284 : if (res == NULL)
3329 alvherre@alvh.no-ip. 2003 :UBC 0 : PG_RETURN_NULL();
2004 : :
3329 alvherre@alvh.no-ip. 2005 :CBC 284 : PG_RETURN_TEXT_P(string_to_text(res));
2006 : : }
2007 : :
2008 : : /*
2009 : : * Internal workhorse to decompile an extended statistics object.
2010 : : */
2011 : : static char *
1866 tomas.vondra@postgre 2012 : 492 : pg_get_statisticsobj_worker(Oid statextid, bool columns_only, bool missing_ok)
2013 : : {
2014 : : Form_pg_statistic_ext statextrec;
2015 : : HeapTuple statexttup;
2016 : : StringInfoData buf;
2017 : : int colno;
2018 : : char *nsp;
2019 : : ArrayType *arr;
2020 : : char *enabled;
2021 : : Datum datum;
2022 : : bool ndistinct_enabled;
2023 : : bool dependencies_enabled;
2024 : : bool mcv_enabled;
2025 : : int i;
2026 : : List *context;
2027 : : ListCell *lc;
2028 : 492 : List *exprs = NIL;
2029 : : bool has_exprs;
2030 : : int ncolumns;
2031 : :
3329 alvherre@alvh.no-ip. 2032 : 492 : statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
2033 : :
2034 [ + + ]: 492 : if (!HeapTupleIsValid(statexttup))
2035 : : {
2036 [ + - ]: 4 : if (missing_ok)
2037 : 4 : return NULL;
3278 tgl@sss.pgh.pa.us 2038 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for statistics object %u", statextid);
2039 : : }
2040 : :
2041 : : /* has the statistics expressions? */
1866 tomas.vondra@postgre 2042 :CBC 488 : has_exprs = !heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
2043 : :
2044 : 488 : statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
2045 : :
2046 : : /*
2047 : : * Get the statistics expressions, if any. (NOTE: we do not use the
2048 : : * relcache versions of the expressions, because we want to display
2049 : : * non-const-folded expressions.)
2050 : : */
2051 [ + + ]: 488 : if (has_exprs)
2052 : : {
2053 : : Datum exprsDatum;
2054 : : char *exprsString;
2055 : :
1137 dgustafsson@postgres 2056 : 125 : exprsDatum = SysCacheGetAttrNotNull(STATEXTOID, statexttup,
2057 : : Anum_pg_statistic_ext_stxexprs);
1866 tomas.vondra@postgre 2058 : 125 : exprsString = TextDatumGetCString(exprsDatum);
2059 : 125 : exprs = (List *) stringToNode(exprsString);
2060 : 125 : pfree(exprsString);
2061 : : }
2062 : : else
2063 : 363 : exprs = NIL;
2064 : :
2065 : : /* count the number of columns (attributes and expressions) */
2066 : 488 : ncolumns = statextrec->stxkeys.dim1 + list_length(exprs);
2067 : :
2068 : 488 : initStringInfo(&buf);
2069 : :
2070 [ + + ]: 488 : if (!columns_only)
2071 : : {
1743 tgl@sss.pgh.pa.us 2072 : 204 : nsp = get_namespace_name_or_temp(statextrec->stxnamespace);
1866 tomas.vondra@postgre 2073 : 204 : appendStringInfo(&buf, "CREATE STATISTICS %s",
2074 : : quote_qualified_identifier(nsp,
2075 : 204 : NameStr(statextrec->stxname)));
2076 : :
2077 : : /*
2078 : : * Decode the stxkind column so that we know which stats types to
2079 : : * print.
2080 : : */
1137 dgustafsson@postgres 2081 : 204 : datum = SysCacheGetAttrNotNull(STATEXTOID, statexttup,
2082 : : Anum_pg_statistic_ext_stxkind);
1866 tomas.vondra@postgre 2083 : 204 : arr = DatumGetArrayTypeP(datum);
2084 [ + - ]: 204 : if (ARR_NDIM(arr) != 1 ||
2085 [ + - ]: 204 : ARR_HASNULL(arr) ||
2086 [ - + ]: 204 : ARR_ELEMTYPE(arr) != CHAROID)
1866 tomas.vondra@postgre 2087 [ # # ]:UBC 0 : elog(ERROR, "stxkind is not a 1-D char array");
1866 tomas.vondra@postgre 2088 [ - + ]:CBC 204 : enabled = (char *) ARR_DATA_PTR(arr);
2089 : :
2090 : 204 : ndistinct_enabled = false;
2091 : 204 : dependencies_enabled = false;
2092 : 204 : mcv_enabled = false;
2093 : :
2094 [ + + ]: 643 : for (i = 0; i < ARR_DIMS(arr)[0]; i++)
2095 : : {
2096 [ + + ]: 439 : if (enabled[i] == STATS_EXT_NDISTINCT)
2097 : 131 : ndistinct_enabled = true;
2098 [ + + ]: 308 : else if (enabled[i] == STATS_EXT_DEPENDENCIES)
2099 : 109 : dependencies_enabled = true;
2100 [ + + ]: 199 : else if (enabled[i] == STATS_EXT_MCV)
2101 : 118 : mcv_enabled = true;
2102 : :
2103 : : /* ignore STATS_EXT_EXPRESSIONS (it's built automatically) */
2104 : : }
2105 : :
2106 : : /*
2107 : : * If any option is disabled, then we'll need to append the types
2108 : : * clause to show which options are enabled. We omit the types clause
2109 : : * on purpose when all options are enabled, so a pg_dump/pg_restore
2110 : : * will create all statistics types on a newer postgres version, if
2111 : : * the statistics had all options enabled on the original version.
2112 : : *
2113 : : * But if the statistics is defined on just a single column, it has to
2114 : : * be an expression statistics. In that case we don't need to specify
2115 : : * kinds.
2116 : : */
2117 [ + + + + : 204 : if ((!ndistinct_enabled || !dependencies_enabled || !mcv_enabled) &&
- + + + ]
2118 : : (ncolumns > 1))
2119 : : {
2120 : 61 : bool gotone = false;
2121 : :
2122 : 61 : appendStringInfoString(&buf, " (");
2123 : :
2124 [ + + ]: 61 : if (ndistinct_enabled)
2125 : : {
2126 : 32 : appendStringInfoString(&buf, "ndistinct");
2127 : 32 : gotone = true;
2128 : : }
2129 : :
2130 [ + + ]: 61 : if (dependencies_enabled)
2131 : : {
2132 [ - + ]: 10 : appendStringInfo(&buf, "%sdependencies", gotone ? ", " : "");
2133 : 10 : gotone = true;
2134 : : }
2135 : :
2136 [ + + ]: 61 : if (mcv_enabled)
2137 [ - + ]: 19 : appendStringInfo(&buf, "%smcv", gotone ? ", " : "");
2138 : :
2139 : 61 : appendStringInfoChar(&buf, ')');
2140 : : }
2141 : :
2142 : 204 : appendStringInfoString(&buf, " ON ");
2143 : : }
2144 : :
2145 : : /* decode simple column references */
3305 alvherre@alvh.no-ip. 2146 [ + + ]: 1393 : for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
2147 : : {
2148 : 905 : AttrNumber attnum = statextrec->stxkeys.values[colno];
2149 : : char *attname;
2150 : :
3329 2151 [ + + ]: 905 : if (colno > 0)
2152 : 502 : appendStringInfoString(&buf, ", ");
2153 : :
3004 2154 : 905 : attname = get_attname(statextrec->stxrelid, attnum, false);
2155 : :
3329 2156 : 905 : appendStringInfoString(&buf, quote_identifier(attname));
2157 : : }
2158 : :
1866 tomas.vondra@postgre 2159 : 488 : context = deparse_context_for(get_relation_name(statextrec->stxrelid),
2160 : : statextrec->stxrelid);
2161 : :
2162 [ + + + + : 667 : foreach(lc, exprs)
+ + ]
2163 : : {
2164 : 179 : Node *expr = (Node *) lfirst(lc);
2165 : : char *str;
1707 2166 : 179 : int prettyFlags = PRETTYFLAG_PAREN;
2167 : :
1866 2168 : 179 : str = deparse_expression_pretty(expr, context, false, false,
2169 : : prettyFlags, 0);
2170 : :
2171 [ + + ]: 179 : if (colno > 0)
2172 : 94 : appendStringInfoString(&buf, ", ");
2173 : :
2174 : : /* Need parens if it's not a bare function call */
2175 [ + + ]: 179 : if (looks_like_function(expr))
2176 : 21 : appendStringInfoString(&buf, str);
2177 : : else
2178 : 158 : appendStringInfo(&buf, "(%s)", str);
2179 : :
2180 : 179 : colno++;
2181 : : }
2182 : :
2183 [ + + ]: 488 : if (!columns_only)
2184 : 204 : appendStringInfo(&buf, " FROM %s",
2185 : : generate_relation_name(statextrec->stxrelid, NIL));
2186 : :
3329 alvherre@alvh.no-ip. 2187 : 488 : ReleaseSysCache(statexttup);
2188 : :
2189 : 488 : return buf.data;
2190 : : }
2191 : :
2192 : : /*
2193 : : * Generate text array of expressions for statistics object.
2194 : : */
2195 : : Datum
1866 tomas.vondra@postgre 2196 : 125 : pg_get_statisticsobjdef_expressions(PG_FUNCTION_ARGS)
2197 : : {
2198 : 125 : Oid statextid = PG_GETARG_OID(0);
2199 : : Form_pg_statistic_ext statextrec;
2200 : : HeapTuple statexttup;
2201 : : Datum datum;
2202 : : List *context;
2203 : : ListCell *lc;
2204 : 125 : List *exprs = NIL;
2205 : : bool has_exprs;
2206 : : char *tmp;
2207 : 125 : ArrayBuildState *astate = NULL;
2208 : :
2209 : 125 : statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
2210 : :
2211 [ - + ]: 125 : if (!HeapTupleIsValid(statexttup))
1824 tomas.vondra@postgre 2212 :UBC 0 : PG_RETURN_NULL();
2213 : :
2214 : : /* Does the stats object have expressions? */
1866 tomas.vondra@postgre 2215 :CBC 125 : has_exprs = !heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
2216 : :
2217 : : /* no expressions? we're done */
2218 [ + + ]: 125 : if (!has_exprs)
2219 : : {
2220 : 11 : ReleaseSysCache(statexttup);
2221 : 11 : PG_RETURN_NULL();
2222 : : }
2223 : :
2224 : 114 : statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
2225 : :
2226 : : /*
2227 : : * Get the statistics expressions, and deparse them into text values.
2228 : : */
1137 dgustafsson@postgres 2229 : 114 : datum = SysCacheGetAttrNotNull(STATEXTOID, statexttup,
2230 : : Anum_pg_statistic_ext_stxexprs);
1866 tomas.vondra@postgre 2231 : 114 : tmp = TextDatumGetCString(datum);
2232 : 114 : exprs = (List *) stringToNode(tmp);
2233 : 114 : pfree(tmp);
2234 : :
2235 : 114 : context = deparse_context_for(get_relation_name(statextrec->stxrelid),
2236 : : statextrec->stxrelid);
2237 : :
2238 [ + - + + : 274 : foreach(lc, exprs)
+ + ]
2239 : : {
2240 : 160 : Node *expr = (Node *) lfirst(lc);
2241 : : char *str;
2242 : 160 : int prettyFlags = PRETTYFLAG_INDENT;
2243 : :
2244 : 160 : str = deparse_expression_pretty(expr, context, false, false,
2245 : : prettyFlags, 0);
2246 : :
2247 : 160 : astate = accumArrayResult(astate,
2248 : 160 : PointerGetDatum(cstring_to_text(str)),
2249 : : false,
2250 : : TEXTOID,
2251 : : CurrentMemoryContext);
2252 : : }
2253 : :
2254 : 114 : ReleaseSysCache(statexttup);
2255 : :
2256 : 114 : PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
2257 : : }
2258 : :
2259 : : /*
2260 : : * pg_get_partkeydef
2261 : : *
2262 : : * Returns the partition key specification, ie, the following:
2263 : : *
2264 : : * { RANGE | LIST | HASH } (column opt_collation opt_opclass [, ...])
2265 : : */
2266 : : Datum
3436 rhaas@postgresql.org 2267 : 813 : pg_get_partkeydef(PG_FUNCTION_ARGS)
2268 : : {
2269 : 813 : Oid relid = PG_GETARG_OID(0);
2270 : : char *res;
2271 : :
3296 sfrost@snowman.net 2272 : 813 : res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
2273 : :
2274 [ + + ]: 813 : if (res == NULL)
2275 : 4 : PG_RETURN_NULL();
2276 : :
2277 : 809 : PG_RETURN_TEXT_P(string_to_text(res));
2278 : : }
2279 : :
2280 : : /* Internal version that just reports the column definitions */
2281 : : char *
3350 rhaas@postgresql.org 2282 : 94 : pg_get_partkeydef_columns(Oid relid, bool pretty)
2283 : : {
2284 : : int prettyFlags;
2285 : :
1499 tgl@sss.pgh.pa.us 2286 [ + - ]: 94 : prettyFlags = GET_PRETTY_FLAGS(pretty);
2287 : :
3296 sfrost@snowman.net 2288 : 94 : return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
2289 : : }
2290 : :
2291 : : /*
2292 : : * Internal workhorse to decompile a partition key definition.
2293 : : */
2294 : : static char *
3350 rhaas@postgresql.org 2295 : 907 : pg_get_partkeydef_worker(Oid relid, int prettyFlags,
2296 : : bool attrsOnly, bool missing_ok)
2297 : : {
2298 : : Form_pg_partitioned_table form;
2299 : : HeapTuple tuple;
2300 : : oidvector *partclass;
2301 : : oidvector *partcollation;
2302 : : List *partexprs;
2303 : : ListCell *partexpr_item;
2304 : : List *context;
2305 : : Datum datum;
2306 : : StringInfoData buf;
2307 : : int keyno;
2308 : : char *str;
2309 : : char *sep;
2310 : :
3436 2311 : 907 : tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
2312 [ + + ]: 907 : if (!HeapTupleIsValid(tuple))
2313 : : {
3296 sfrost@snowman.net 2314 [ + - ]: 4 : if (missing_ok)
2315 : 4 : return NULL;
3436 rhaas@postgresql.org 2316 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for partition key of %u", relid);
2317 : : }
2318 : :
3436 rhaas@postgresql.org 2319 :CBC 903 : form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
2320 : :
2321 [ - + ]: 903 : Assert(form->partrelid == relid);
2322 : :
2323 : : /* Must get partclass and partcollation the hard way */
1137 dgustafsson@postgres 2324 : 903 : datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
2325 : : Anum_pg_partitioned_table_partclass);
3436 rhaas@postgresql.org 2326 : 903 : partclass = (oidvector *) DatumGetPointer(datum);
2327 : :
1137 dgustafsson@postgres 2328 : 903 : datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
2329 : : Anum_pg_partitioned_table_partcollation);
3436 rhaas@postgresql.org 2330 : 903 : partcollation = (oidvector *) DatumGetPointer(datum);
2331 : :
2332 : :
2333 : : /*
2334 : : * Get the expressions, if any. (NOTE: we do not use the relcache
2335 : : * versions of the expressions, because we want to display
2336 : : * non-const-folded expressions.)
2337 : : */
2960 andrew@dunslane.net 2338 [ + + ]: 903 : if (!heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
2339 : : {
2340 : : Datum exprsDatum;
2341 : : char *exprsString;
2342 : :
1137 dgustafsson@postgres 2343 : 84 : exprsDatum = SysCacheGetAttrNotNull(PARTRELID, tuple,
2344 : : Anum_pg_partitioned_table_partexprs);
3436 rhaas@postgresql.org 2345 : 84 : exprsString = TextDatumGetCString(exprsDatum);
2346 : 84 : partexprs = (List *) stringToNode(exprsString);
2347 : :
2348 [ - + ]: 84 : if (!IsA(partexprs, List))
3436 rhaas@postgresql.org 2349 [ # # ]:UBC 0 : elog(ERROR, "unexpected node type found in partexprs: %d",
2350 : : (int) nodeTag(partexprs));
2351 : :
3436 rhaas@postgresql.org 2352 :CBC 84 : pfree(exprsString);
2353 : : }
2354 : : else
2355 : 819 : partexprs = NIL;
2356 : :
2357 : 903 : partexpr_item = list_head(partexprs);
2358 : 903 : context = deparse_context_for(get_relation_name(relid), relid);
2359 : :
2360 : 903 : initStringInfo(&buf);
2361 : :
2362 [ + + + - ]: 903 : switch (form->partstrat)
2363 : : {
3099 2364 : 59 : case PARTITION_STRATEGY_HASH:
2365 [ + - ]: 59 : if (!attrsOnly)
2497 drowley@postgresql.o 2366 : 59 : appendStringInfoString(&buf, "HASH");
3099 rhaas@postgresql.org 2367 : 59 : break;
3436 2368 : 339 : case PARTITION_STRATEGY_LIST:
3350 2369 [ + + ]: 339 : if (!attrsOnly)
3185 peter_e@gmx.net 2370 : 313 : appendStringInfoString(&buf, "LIST");
3436 rhaas@postgresql.org 2371 : 339 : break;
2372 : 505 : case PARTITION_STRATEGY_RANGE:
3350 2373 [ + + ]: 505 : if (!attrsOnly)
3185 peter_e@gmx.net 2374 : 437 : appendStringInfoString(&buf, "RANGE");
3436 rhaas@postgresql.org 2375 : 505 : break;
3436 rhaas@postgresql.org 2376 :UBC 0 : default:
2377 [ # # ]: 0 : elog(ERROR, "unexpected partition strategy: %d",
2378 : : (int) form->partstrat);
2379 : : }
2380 : :
3350 rhaas@postgresql.org 2381 [ + + ]:CBC 903 : if (!attrsOnly)
3185 peter_e@gmx.net 2382 : 809 : appendStringInfoString(&buf, " (");
3436 rhaas@postgresql.org 2383 : 903 : sep = "";
2384 [ + + ]: 1899 : for (keyno = 0; keyno < form->partnatts; keyno++)
2385 : : {
2386 : 996 : AttrNumber attnum = form->partattrs.values[keyno];
2387 : : Oid keycoltype;
2388 : : Oid keycolcollation;
2389 : : Oid partcoll;
2390 : :
2391 : 996 : appendStringInfoString(&buf, sep);
2392 : 996 : sep = ", ";
2393 [ + + ]: 996 : if (attnum != 0)
2394 : : {
2395 : : /* Simple attribute reference */
2396 : : char *attname;
2397 : : int32 keycoltypmod;
2398 : :
3004 alvherre@alvh.no-ip. 2399 : 904 : attname = get_attname(relid, attnum, false);
3436 rhaas@postgresql.org 2400 : 904 : appendStringInfoString(&buf, quote_identifier(attname));
2401 : 904 : get_atttypetypmodcoll(relid, attnum,
2402 : : &keycoltype, &keycoltypmod,
2403 : : &keycolcollation);
2404 : : }
2405 : : else
2406 : : {
2407 : : /* Expression */
2408 : : Node *partkey;
2409 : :
2410 [ - + ]: 92 : if (partexpr_item == NULL)
3436 rhaas@postgresql.org 2411 [ # # ]:UBC 0 : elog(ERROR, "too few entries in partexprs list");
3436 rhaas@postgresql.org 2412 :CBC 92 : partkey = (Node *) lfirst(partexpr_item);
2486 tgl@sss.pgh.pa.us 2413 : 92 : partexpr_item = lnext(partexprs, partexpr_item);
2414 : :
2415 : : /* Deparse */
3436 rhaas@postgresql.org 2416 : 92 : str = deparse_expression_pretty(partkey, context, false, false,
2417 : : prettyFlags, 0);
2418 : : /* Need parens if it's not a bare function call */
3218 tgl@sss.pgh.pa.us 2419 [ + + ]: 92 : if (looks_like_function(partkey))
2420 : 34 : appendStringInfoString(&buf, str);
2421 : : else
2422 : 58 : appendStringInfo(&buf, "(%s)", str);
2423 : :
3436 rhaas@postgresql.org 2424 : 92 : keycoltype = exprType(partkey);
2425 : 92 : keycolcollation = exprCollation(partkey);
2426 : : }
2427 : :
2428 : : /* Add collation, if not default for column */
2429 : 996 : partcoll = partcollation->values[keyno];
3350 2430 [ + + + + : 996 : if (!attrsOnly && OidIsValid(partcoll) && partcoll != keycolcollation)
+ + ]
3436 2431 : 4 : appendStringInfo(&buf, " COLLATE %s",
2432 : : generate_collation_name((partcoll)));
2433 : :
2434 : : /* Add the operator class name, if not default */
3350 2435 [ + + ]: 996 : if (!attrsOnly)
2436 : 866 : get_opclass_name(partclass->values[keyno], keycoltype, &buf);
2437 : : }
2438 : :
2439 [ + + ]: 903 : if (!attrsOnly)
2440 : 809 : appendStringInfoChar(&buf, ')');
2441 : :
2442 : : /* Clean up */
3436 2443 : 903 : ReleaseSysCache(tuple);
2444 : :
2445 : 903 : return buf.data;
2446 : : }
2447 : :
2448 : : /*
2449 : : * pg_get_partition_constraintdef
2450 : : *
2451 : : * Returns partition constraint expression as a string for the input relation
2452 : : */
2453 : : Datum
3279 2454 : 153 : pg_get_partition_constraintdef(PG_FUNCTION_ARGS)
2455 : : {
3275 bruce@momjian.us 2456 : 153 : Oid relationId = PG_GETARG_OID(0);
2457 : : Expr *constr_expr;
2458 : : int prettyFlags;
2459 : : List *context;
2460 : : char *consrc;
2461 : :
3279 rhaas@postgresql.org 2462 : 153 : constr_expr = get_partition_qual_relid(relationId);
2463 : :
2464 : : /* Quick exit if no partition constraint */
2465 [ + + ]: 153 : if (constr_expr == NULL)
2466 : 16 : PG_RETURN_NULL();
2467 : :
2468 : : /*
2469 : : * Deparse and return the constraint expression.
2470 : : */
2471 : 137 : prettyFlags = PRETTYFLAG_INDENT;
2472 : 137 : context = deparse_context_for(get_relation_name(relationId), relationId);
2473 : 137 : consrc = deparse_expression_pretty((Node *) constr_expr, context, false,
2474 : : false, prettyFlags, 0);
2475 : :
2476 : 137 : PG_RETURN_TEXT_P(string_to_text(consrc));
2477 : : }
2478 : :
2479 : : /*
2480 : : * pg_get_partconstrdef_string
2481 : : *
2482 : : * Returns the partition constraint as a C-string for the input relation, with
2483 : : * the given alias. No pretty-printing.
2484 : : */
2485 : : char *
2589 alvherre@alvh.no-ip. 2486 : 65 : pg_get_partconstrdef_string(Oid partitionId, char *aliasname)
2487 : : {
2488 : : Expr *constr_expr;
2489 : : List *context;
2490 : :
2491 : 65 : constr_expr = get_partition_qual_relid(partitionId);
2492 : 65 : context = deparse_context_for(aliasname, partitionId);
2493 : :
2494 : 65 : return deparse_expression((Node *) constr_expr, context, true, false);
2495 : : }
2496 : :
2497 : : /*
2498 : : * pg_get_constraintdef
2499 : : *
2500 : : * Returns the definition for the constraint, ie, everything that needs to
2501 : : * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>".
2502 : : */
2503 : : Datum
8663 tgl@sss.pgh.pa.us 2504 : 1198 : pg_get_constraintdef(PG_FUNCTION_ARGS)
2505 : : {
8644 bruce@momjian.us 2506 : 1198 : Oid constraintId = PG_GETARG_OID(0);
2507 : : int prettyFlags;
2508 : : char *res;
2509 : :
4839 tgl@sss.pgh.pa.us 2510 : 1198 : prettyFlags = PRETTYFLAG_INDENT;
2511 : :
3570 rhaas@postgresql.org 2512 : 1198 : res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2513 : :
2514 [ + + ]: 1198 : if (res == NULL)
2515 : 4 : PG_RETURN_NULL();
2516 : :
2517 : 1194 : PG_RETURN_TEXT_P(string_to_text(res));
2518 : : }
2519 : :
2520 : : Datum
8315 tgl@sss.pgh.pa.us 2521 : 2917 : pg_get_constraintdef_ext(PG_FUNCTION_ARGS)
2522 : : {
2523 : 2917 : Oid constraintId = PG_GETARG_OID(0);
2524 : 2917 : bool pretty = PG_GETARG_BOOL(1);
2525 : : int prettyFlags;
2526 : : char *res;
2527 : :
1499 2528 [ + + ]: 2917 : prettyFlags = GET_PRETTY_FLAGS(pretty);
2529 : :
3570 rhaas@postgresql.org 2530 : 2917 : res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2531 : :
2532 [ - + ]: 2917 : if (res == NULL)
3570 rhaas@postgresql.org 2533 :UBC 0 : PG_RETURN_NULL();
2534 : :
3570 rhaas@postgresql.org 2535 :CBC 2917 : PG_RETURN_TEXT_P(string_to_text(res));
2536 : : }
2537 : :
2538 : : /*
2539 : : * Internal version that returns a full ALTER TABLE ... ADD CONSTRAINT command
2540 : : */
2541 : : char *
3819 tgl@sss.pgh.pa.us 2542 : 452 : pg_get_constraintdef_command(Oid constraintId)
2543 : : {
3570 rhaas@postgresql.org 2544 : 452 : return pg_get_constraintdef_worker(constraintId, true, 0, false);
2545 : : }
2546 : :
2547 : : /*
2548 : : * As of 9.4, we now use an MVCC snapshot for this.
2549 : : */
2550 : : static char *
8035 tgl@sss.pgh.pa.us 2551 : 4567 : pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
2552 : : int prettyFlags, bool missing_ok)
2553 : : {
2554 : : HeapTuple tup;
2555 : : Form_pg_constraint conForm;
2556 : : StringInfoData buf;
2557 : : SysScanDesc scandesc;
2558 : : ScanKeyData scankey[1];
4382 bruce@momjian.us 2559 : 4567 : Snapshot snapshot = RegisterSnapshot(GetTransactionSnapshot());
2661 andres@anarazel.de 2560 : 4567 : Relation relation = table_open(ConstraintRelationId, AccessShareLock);
2561 : :
4412 simon@2ndQuadrant.co 2562 : 4567 : ScanKeyInit(&scankey[0],
2563 : : Anum_pg_constraint_oid,
2564 : : BTEqualStrategyNumber, F_OIDEQ,
2565 : : ObjectIdGetDatum(constraintId));
2566 : :
2567 : 4567 : scandesc = systable_beginscan(relation,
2568 : : ConstraintOidIndexId,
2569 : : true,
2570 : : snapshot,
2571 : : 1,
2572 : : scankey);
2573 : :
2574 : : /*
2575 : : * We later use the tuple with SysCacheGetAttr() as if we had obtained it
2576 : : * via SearchSysCache, which works fine.
2577 : : */
2578 : 4567 : tup = systable_getnext(scandesc);
2579 : :
2580 : 4567 : UnregisterSnapshot(snapshot);
2581 : :
3570 rhaas@postgresql.org 2582 [ + + ]: 4567 : if (!HeapTupleIsValid(tup))
2583 : : {
2584 [ + - ]: 4 : if (missing_ok)
2585 : : {
2586 : 4 : systable_endscan(scandesc);
2661 andres@anarazel.de 2587 : 4 : table_close(relation, AccessShareLock);
3570 rhaas@postgresql.org 2588 : 4 : return NULL;
2589 : : }
3257 tgl@sss.pgh.pa.us 2590 [ # # ]:UBC 0 : elog(ERROR, "could not find tuple for constraint %u", constraintId);
2591 : : }
2592 : :
8663 tgl@sss.pgh.pa.us 2593 :CBC 4563 : conForm = (Form_pg_constraint) GETSTRUCT(tup);
2594 : :
2595 : 4563 : initStringInfo(&buf);
2596 : :
3819 2597 [ + + ]: 4563 : if (fullCommand)
2598 : : {
3107 2599 [ + + ]: 452 : if (OidIsValid(conForm->conrelid))
2600 : : {
2601 : : /*
2602 : : * Currently, callers want ALTER TABLE (without ONLY) for CHECK
2603 : : * constraints, and other types of constraints don't inherit
2604 : : * anyway so it doesn't matter whether we say ONLY or not. Someday
2605 : : * we might need to let callers specify whether to put ONLY in the
2606 : : * command.
2607 : : */
2608 : 443 : appendStringInfo(&buf, "ALTER TABLE %s ADD CONSTRAINT %s ",
2609 : : generate_qualified_relation_name(conForm->conrelid),
2610 : 443 : quote_identifier(NameStr(conForm->conname)));
2611 : : }
2612 : : else
2613 : : {
2614 : : /* Must be a domain constraint */
2615 [ - + ]: 9 : Assert(OidIsValid(conForm->contypid));
2616 : 9 : appendStringInfo(&buf, "ALTER DOMAIN %s ADD CONSTRAINT %s ",
2617 : : generate_qualified_type_name(conForm->contypid),
2618 : 9 : quote_identifier(NameStr(conForm->conname)));
2619 : : }
2620 : : }
2621 : :
8663 2622 [ + + + + : 4563 : switch (conForm->contype)
- + - ]
2623 : : {
2624 : 541 : case CONSTRAINT_FOREIGN:
2625 : : {
2626 : : Datum val;
2627 : : bool isnull;
2628 : : const char *string;
2629 : :
2630 : : /* Start off the constraint definition */
4569 rhaas@postgresql.org 2631 : 541 : appendStringInfoString(&buf, "FOREIGN KEY (");
2632 : :
2633 : : /* Fetch and build referencing-column list */
1137 dgustafsson@postgres 2634 : 541 : val = SysCacheGetAttrNotNull(CONSTROID, tup,
2635 : : Anum_pg_constraint_conkey);
2636 : :
2637 : : /* If it is a temporal foreign key then it uses PERIOD. */
595 peter@eisentraut.org 2638 : 541 : decompile_column_index_array(val, conForm->conrelid, conForm->conperiod, &buf);
2639 : :
2640 : : /* add foreign relation name */
8644 bruce@momjian.us 2641 : 541 : appendStringInfo(&buf, ") REFERENCES %s(",
2642 : : generate_relation_name(conForm->confrelid,
2643 : : NIL));
2644 : :
2645 : : /* Fetch and build referenced-column list */
1137 dgustafsson@postgres 2646 : 541 : val = SysCacheGetAttrNotNull(CONSTROID, tup,
2647 : : Anum_pg_constraint_confkey);
2648 : :
595 peter@eisentraut.org 2649 : 541 : decompile_column_index_array(val, conForm->confrelid, conForm->conperiod, &buf);
2650 : :
4569 rhaas@postgresql.org 2651 : 541 : appendStringInfoChar(&buf, ')');
2652 : :
2653 : : /* Add match type */
8644 bruce@momjian.us 2654 [ + - + - ]: 541 : switch (conForm->confmatchtype)
2655 : : {
2656 : 21 : case FKCONSTR_MATCH_FULL:
2657 : 21 : string = " MATCH FULL";
2658 : 21 : break;
8644 bruce@momjian.us 2659 :UBC 0 : case FKCONSTR_MATCH_PARTIAL:
2660 : 0 : string = " MATCH PARTIAL";
2661 : 0 : break;
5070 tgl@sss.pgh.pa.us 2662 :CBC 520 : case FKCONSTR_MATCH_SIMPLE:
8644 bruce@momjian.us 2663 : 520 : string = "";
2664 : 520 : break;
8644 bruce@momjian.us 2665 :UBC 0 : default:
8318 tgl@sss.pgh.pa.us 2666 [ # # ]: 0 : elog(ERROR, "unrecognized confmatchtype: %d",
2667 : : conForm->confmatchtype);
2668 : : string = ""; /* keep compiler quiet */
2669 : : break;
2670 : : }
8130 neilc@samurai.com 2671 :CBC 541 : appendStringInfoString(&buf, string);
2672 : :
2673 : : /* Add ON UPDATE and ON DELETE clauses, if needed */
8644 bruce@momjian.us 2674 [ + - + + : 541 : switch (conForm->confupdtype)
- - ]
2675 : : {
2676 : 457 : case FKCONSTR_ACTION_NOACTION:
8310 2677 : 457 : string = NULL; /* suppress default */
8644 2678 : 457 : break;
8644 bruce@momjian.us 2679 :UBC 0 : case FKCONSTR_ACTION_RESTRICT:
2680 : 0 : string = "RESTRICT";
2681 : 0 : break;
8644 bruce@momjian.us 2682 :CBC 67 : case FKCONSTR_ACTION_CASCADE:
2683 : 67 : string = "CASCADE";
2684 : 67 : break;
2685 : 17 : case FKCONSTR_ACTION_SETNULL:
2686 : 17 : string = "SET NULL";
2687 : 17 : break;
8644 bruce@momjian.us 2688 :UBC 0 : case FKCONSTR_ACTION_SETDEFAULT:
2689 : 0 : string = "SET DEFAULT";
2690 : 0 : break;
2691 : 0 : default:
8318 tgl@sss.pgh.pa.us 2692 [ # # ]: 0 : elog(ERROR, "unrecognized confupdtype: %d",
2693 : : conForm->confupdtype);
2694 : : string = NULL; /* keep compiler quiet */
2695 : : break;
2696 : : }
8492 tgl@sss.pgh.pa.us 2697 [ + + ]:CBC 541 : if (string)
bruce@momjian.us 2698 : 84 : appendStringInfo(&buf, " ON UPDATE %s", string);
2699 : :
8644 2700 [ + - + + : 541 : switch (conForm->confdeltype)
+ - ]
2701 : : {
2702 : 458 : case FKCONSTR_ACTION_NOACTION:
8310 2703 : 458 : string = NULL; /* suppress default */
8644 2704 : 458 : break;
8644 bruce@momjian.us 2705 :UBC 0 : case FKCONSTR_ACTION_RESTRICT:
2706 : 0 : string = "RESTRICT";
2707 : 0 : break;
8644 bruce@momjian.us 2708 :CBC 67 : case FKCONSTR_ACTION_CASCADE:
2709 : 67 : string = "CASCADE";
2710 : 67 : break;
2711 : 12 : case FKCONSTR_ACTION_SETNULL:
2712 : 12 : string = "SET NULL";
2713 : 12 : break;
2714 : 4 : case FKCONSTR_ACTION_SETDEFAULT:
2715 : 4 : string = "SET DEFAULT";
2716 : 4 : break;
8644 bruce@momjian.us 2717 :UBC 0 : default:
8318 tgl@sss.pgh.pa.us 2718 [ # # ]: 0 : elog(ERROR, "unrecognized confdeltype: %d",
2719 : : conForm->confdeltype);
2720 : : string = NULL; /* keep compiler quiet */
2721 : : break;
2722 : : }
8492 tgl@sss.pgh.pa.us 2723 [ + + ]:CBC 541 : if (string)
bruce@momjian.us 2724 : 83 : appendStringInfo(&buf, " ON DELETE %s", string);
2725 : :
2726 : : /*
2727 : : * Add columns specified to SET NULL or SET DEFAULT if
2728 : : * provided.
2729 : : */
1609 peter@eisentraut.org 2730 : 541 : val = SysCacheGetAttr(CONSTROID, tup,
2731 : : Anum_pg_constraint_confdelsetcols, &isnull);
2732 [ + + ]: 541 : if (!isnull)
2733 : : {
1337 drowley@postgresql.o 2734 : 8 : appendStringInfoString(&buf, " (");
595 peter@eisentraut.org 2735 : 8 : decompile_column_index_array(val, conForm->conrelid, false, &buf);
1337 drowley@postgresql.o 2736 : 8 : appendStringInfoChar(&buf, ')');
2737 : : }
2738 : :
8644 bruce@momjian.us 2739 : 541 : break;
2740 : : }
8482 2741 : 2315 : case CONSTRAINT_PRIMARY:
2742 : : case CONSTRAINT_UNIQUE:
2743 : : {
2744 : : Datum val;
2745 : : Oid indexId;
2746 : : int keyatts;
2747 : : HeapTuple indtup;
2748 : :
2749 : : /* Start off the constraint definition */
2750 [ + + ]: 2315 : if (conForm->contype == CONSTRAINT_PRIMARY)
1552 peter@eisentraut.org 2751 : 1889 : appendStringInfoString(&buf, "PRIMARY KEY ");
2752 : : else
2753 : 426 : appendStringInfoString(&buf, "UNIQUE ");
2754 : :
2755 : 2315 : indexId = conForm->conindid;
2756 : :
2757 : 2315 : indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
2758 [ - + ]: 2315 : if (!HeapTupleIsValid(indtup))
1552 peter@eisentraut.org 2759 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u", indexId);
1552 peter@eisentraut.org 2760 [ + + ]:CBC 2315 : if (conForm->contype == CONSTRAINT_UNIQUE &&
2761 [ - + ]: 426 : ((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct)
1552 peter@eisentraut.org 2762 :UBC 0 : appendStringInfoString(&buf, "NULLS NOT DISTINCT ");
2763 : :
1337 drowley@postgresql.o 2764 :CBC 2315 : appendStringInfoChar(&buf, '(');
2765 : :
2766 : : /* Fetch and build target column list */
1137 dgustafsson@postgres 2767 : 2315 : val = SysCacheGetAttrNotNull(CONSTROID, tup,
2768 : : Anum_pg_constraint_conkey);
2769 : :
595 peter@eisentraut.org 2770 : 2315 : keyatts = decompile_column_index_array(val, conForm->conrelid, false, &buf);
2771 [ + + ]: 2315 : if (conForm->conperiod)
2772 : 220 : appendStringInfoString(&buf, " WITHOUT OVERLAPS");
2773 : :
4569 rhaas@postgresql.org 2774 : 2315 : appendStringInfoChar(&buf, ')');
2775 : :
2776 : : /* Build including column list (from pg_index.indkeys) */
1137 dgustafsson@postgres 2777 : 2315 : val = SysCacheGetAttrNotNull(INDEXRELID, indtup,
2778 : : Anum_pg_index_indnatts);
2801 alvherre@alvh.no-ip. 2779 [ + + ]: 2315 : if (DatumGetInt32(val) > keyatts)
2780 : : {
2781 : : Datum cols;
2782 : : Datum *keys;
2783 : : int nKeys;
2784 : : int j;
2785 : :
2950 teodor@sigaev.ru 2786 : 48 : appendStringInfoString(&buf, " INCLUDE (");
2787 : :
1137 dgustafsson@postgres 2788 : 48 : cols = SysCacheGetAttrNotNull(INDEXRELID, indtup,
2789 : : Anum_pg_index_indkey);
2790 : :
1404 peter@eisentraut.org 2791 : 48 : deconstruct_array_builtin(DatumGetArrayTypeP(cols), INT2OID,
2792 : : &keys, NULL, &nKeys);
2793 : :
2801 alvherre@alvh.no-ip. 2794 [ + + ]: 144 : for (j = keyatts; j < nKeys; j++)
2795 : : {
2796 : : char *colName;
2797 : :
2798 : 96 : colName = get_attname(conForm->conrelid,
2799 : 96 : DatumGetInt16(keys[j]), false);
2800 [ + + ]: 96 : if (j > keyatts)
2801 : 48 : appendStringInfoString(&buf, ", ");
2802 : 96 : appendStringInfoString(&buf, quote_identifier(colName));
2803 : : }
2804 : :
2950 teodor@sigaev.ru 2805 : 48 : appendStringInfoChar(&buf, ')');
2806 : : }
2801 alvherre@alvh.no-ip. 2807 : 2315 : ReleaseSysCache(indtup);
2808 : :
2809 : : /* XXX why do we only print these bits if fullCommand? */
6779 tgl@sss.pgh.pa.us 2810 [ + + + - ]: 2315 : if (fullCommand && OidIsValid(indexId))
2811 : : {
2812 : 136 : char *options = flatten_reloptions(indexId);
2813 : : Oid tblspc;
2814 : :
7247 bruce@momjian.us 2815 [ - + ]: 136 : if (options)
2816 : : {
7247 bruce@momjian.us 2817 :UBC 0 : appendStringInfo(&buf, " WITH (%s)", options);
2818 : 0 : pfree(options);
2819 : : }
2820 : :
2821 : : /*
2822 : : * Print the tablespace, unless it's the database default.
2823 : : * This is to help ALTER TABLE usage of this facility,
2824 : : * which needs this behavior to recreate exact catalog
2825 : : * state.
2826 : : */
6779 tgl@sss.pgh.pa.us 2827 :CBC 136 : tblspc = get_rel_tablespace(indexId);
2828 [ + + ]: 136 : if (OidIsValid(tblspc))
2829 : 16 : appendStringInfo(&buf, " USING INDEX TABLESPACE %s",
3240 2830 : 16 : quote_identifier(get_tablespace_name(tblspc)));
2831 : : }
2832 : :
8482 bruce@momjian.us 2833 : 2315 : break;
2834 : : }
2835 : 1354 : case CONSTRAINT_CHECK:
2836 : : {
2837 : : Datum val;
2838 : : char *conbin;
2839 : : char *consrc;
2840 : : Node *expr;
2841 : : List *context;
2842 : :
2843 : : /* Fetch constraint expression in parsetree form */
1137 dgustafsson@postgres 2844 : 1354 : val = SysCacheGetAttrNotNull(CONSTROID, tup,
2845 : : Anum_pg_constraint_conbin);
2846 : :
6615 tgl@sss.pgh.pa.us 2847 : 1354 : conbin = TextDatumGetCString(val);
8350 bruce@momjian.us 2848 : 1354 : expr = stringToNode(conbin);
2849 : :
2850 : : /* Set up deparsing context for Var nodes in constraint */
2851 [ + + ]: 1354 : if (conForm->conrelid != InvalidOid)
2852 : : {
2853 : : /* relation constraint */
5663 tgl@sss.pgh.pa.us 2854 : 1204 : context = deparse_context_for(get_relation_name(conForm->conrelid),
2855 : : conForm->conrelid);
2856 : : }
2857 : : else
2858 : : {
2859 : : /* domain constraint --- can't have Vars */
8249 2860 : 150 : context = NIL;
2861 : : }
2862 : :
8315 2863 : 1354 : consrc = deparse_expression_pretty(expr, context, false, false,
2864 : : prettyFlags, 0);
2865 : :
2866 : : /*
2867 : : * Now emit the constraint definition, adding NO INHERIT if
2868 : : * necessary.
2869 : : *
2870 : : * There are cases where the constraint expression will be
2871 : : * fully parenthesized and we don't need the outer parens ...
2872 : : * but there are other cases where we do need 'em. Be
2873 : : * conservative for now.
2874 : : *
2875 : : * Note that simply checking for leading '(' and trailing ')'
2876 : : * would NOT be good enough, consider "(x > 0) AND (y > 0)".
2877 : : */
5033 alvherre@alvh.no-ip. 2878 : 1354 : appendStringInfo(&buf, "CHECK (%s)%s",
2879 : : consrc,
2880 [ + + ]: 1354 : conForm->connoinherit ? " NO INHERIT" : "");
8482 bruce@momjian.us 2881 : 1354 : break;
2882 : : }
543 alvherre@alvh.no-ip. 2883 : 287 : case CONSTRAINT_NOTNULL:
2884 : : {
2885 [ + + ]: 287 : if (conForm->conrelid)
2886 : : {
2887 : : AttrNumber attnum;
2888 : :
2889 : 230 : attnum = extractNotNullColumn(tup);
2890 : :
2891 : 230 : appendStringInfo(&buf, "NOT NULL %s",
2892 : 230 : quote_identifier(get_attname(conForm->conrelid,
2893 : : attnum, false)));
2894 [ - + ]: 230 : if (((Form_pg_constraint) GETSTRUCT(tup))->connoinherit)
543 alvherre@alvh.no-ip. 2895 :UBC 0 : appendStringInfoString(&buf, " NO INHERIT");
2896 : : }
543 alvherre@alvh.no-ip. 2897 [ + - ]:CBC 57 : else if (conForm->contypid)
2898 : : {
2899 : : /* conkey is null for domain not-null constraints */
2900 : 57 : appendStringInfoString(&buf, "NOT NULL");
2901 : : }
2902 : 287 : break;
2903 : : }
2904 : :
5952 tgl@sss.pgh.pa.us 2905 :UBC 0 : case CONSTRAINT_TRIGGER:
2906 : :
2907 : : /*
2908 : : * There isn't an ALTER TABLE syntax for creating a user-defined
2909 : : * constraint trigger, but it seems better to print something than
2910 : : * throw an error; if we throw error then this function couldn't
2911 : : * safely be applied to all rows of pg_constraint.
2912 : : */
4569 rhaas@postgresql.org 2913 : 0 : appendStringInfoString(&buf, "TRIGGER");
5952 tgl@sss.pgh.pa.us 2914 : 0 : break;
5993 tgl@sss.pgh.pa.us 2915 :CBC 66 : case CONSTRAINT_EXCLUSION:
2916 : : {
5912 bruce@momjian.us 2917 : 66 : Oid indexOid = conForm->conindid;
2918 : : Datum val;
2919 : : Datum *elems;
2920 : : int nElems;
2921 : : int i;
2922 : : Oid *operators;
2923 : :
2924 : : /* Extract operator OIDs from the pg_constraint tuple */
1137 dgustafsson@postgres 2925 : 66 : val = SysCacheGetAttrNotNull(CONSTROID, tup,
2926 : : Anum_pg_constraint_conexclop);
2927 : :
1404 peter@eisentraut.org 2928 : 66 : deconstruct_array_builtin(DatumGetArrayTypeP(val), OIDOID,
2929 : : &elems, NULL, &nElems);
2930 : :
5993 tgl@sss.pgh.pa.us 2931 : 66 : operators = (Oid *) palloc(nElems * sizeof(Oid));
2932 [ + + ]: 142 : for (i = 0; i < nElems; i++)
2933 : 76 : operators[i] = DatumGetObjectId(elems[i]);
2934 : :
2935 : : /* pg_get_indexdef_worker does the rest */
2936 : : /* suppress tablespace because pg_dump wants it that way */
2937 : 66 : appendStringInfoString(&buf,
2938 : 66 : pg_get_indexdef_worker(indexOid,
2939 : : 0,
2940 : : operators,
2941 : : false,
2942 : : false,
2943 : : false,
2944 : : false,
2945 : : prettyFlags,
2946 : : false));
2947 : 66 : break;
2948 : : }
8663 tgl@sss.pgh.pa.us 2949 :UBC 0 : default:
8268 peter_e@gmx.net 2950 [ # # ]: 0 : elog(ERROR, "invalid constraint type \"%c\"", conForm->contype);
2951 : : break;
2952 : : }
2953 : :
6124 tgl@sss.pgh.pa.us 2954 [ + + ]:CBC 4563 : if (conForm->condeferrable)
4569 rhaas@postgresql.org 2955 : 86 : appendStringInfoString(&buf, " DEFERRABLE");
6124 tgl@sss.pgh.pa.us 2956 [ + + ]: 4563 : if (conForm->condeferred)
4569 rhaas@postgresql.org 2957 : 39 : appendStringInfoString(&buf, " INITIALLY DEFERRED");
2958 : :
2959 : : /* Validated status is irrelevant when the constraint is NOT ENFORCED. */
479 peter@eisentraut.org 2960 [ + + ]: 4563 : if (!conForm->conenforced)
2961 : 77 : appendStringInfoString(&buf, " NOT ENFORCED");
2962 [ + + ]: 4486 : else if (!conForm->convalidated)
5451 alvherre@alvh.no-ip. 2963 : 148 : appendStringInfoString(&buf, " NOT VALID");
2964 : :
2965 : : /* Cleanup */
4412 simon@2ndQuadrant.co 2966 : 4563 : systable_endscan(scandesc);
2661 andres@anarazel.de 2967 : 4563 : table_close(relation, AccessShareLock);
2968 : :
8035 tgl@sss.pgh.pa.us 2969 : 4563 : return buf.data;
2970 : : }
2971 : :
2972 : :
2973 : : /*
2974 : : * Convert an int16[] Datum into a comma-separated list of column names
2975 : : * for the indicated relation; append the list to buf. Returns the number
2976 : : * of keys.
2977 : : */
2978 : : static int
8663 2979 : 4432 : decompile_column_index_array(Datum column_index_array, Oid relId,
2980 : : bool withPeriod, StringInfo buf)
2981 : : {
2982 : : Datum *keys;
2983 : : int nKeys;
2984 : : int j;
2985 : :
2986 : : /* Extract data from array of int16 */
1404 peter@eisentraut.org 2987 : 4432 : deconstruct_array_builtin(DatumGetArrayTypeP(column_index_array), INT2OID,
2988 : : &keys, NULL, &nKeys);
2989 : :
8663 tgl@sss.pgh.pa.us 2990 [ + + ]: 10411 : for (j = 0; j < nKeys; j++)
2991 : : {
2992 : : char *colName;
2993 : :
3004 alvherre@alvh.no-ip. 2994 : 5979 : colName = get_attname(relId, DatumGetInt16(keys[j]), false);
2995 : :
8663 tgl@sss.pgh.pa.us 2996 [ + + ]: 5979 : if (j == 0)
8130 neilc@samurai.com 2997 : 4432 : appendStringInfoString(buf, quote_identifier(colName));
2998 : : else
595 peter@eisentraut.org 2999 [ + + ]: 1681 : appendStringInfo(buf, ", %s%s",
3000 [ + + ]: 134 : (withPeriod && j == nKeys - 1) ? "PERIOD " : "",
3001 : : quote_identifier(colName));
3002 : : }
3003 : :
2801 alvherre@alvh.no-ip. 3004 : 4432 : return nKeys;
3005 : : }
3006 : :
3007 : :
3008 : : /* ----------
3009 : : * pg_get_expr - Decompile an expression tree
3010 : : *
3011 : : * Input: an expression tree in nodeToString form, and a relation OID
3012 : : *
3013 : : * Output: reverse-listed expression
3014 : : *
3015 : : * Currently, the expression can only refer to a single relation, namely
3016 : : * the one specified by the second parameter. This is sufficient for
3017 : : * partial indexes, column default expressions, etc. We also support
3018 : : * Var-free expressions, for which the OID can be InvalidOid.
3019 : : *
3020 : : * If the OID is nonzero but not actually valid, don't throw an error,
3021 : : * just return NULL. This is a bit questionable, but it's what we've
3022 : : * done historically, and it can help avoid unwanted failures when
3023 : : * examining catalog entries for just-deleted relations.
3024 : : *
3025 : : * We expect this function to work, or throw a reasonably clean error,
3026 : : * for any node tree that can appear in a catalog pg_node_tree column.
3027 : : * Query trees, such as those appearing in pg_rewrite.ev_action, are
3028 : : * not supported. Nor are expressions in more than one relation, which
3029 : : * can appear in places like pg_rewrite.ev_qual.
3030 : : * ----------
3031 : : */
3032 : : Datum
9059 tgl@sss.pgh.pa.us 3033 : 5525 : pg_get_expr(PG_FUNCTION_ARGS)
3034 : : {
3341 noah@leadboat.com 3035 : 5525 : text *expr = PG_GETARG_TEXT_PP(0);
8310 bruce@momjian.us 3036 : 5525 : Oid relid = PG_GETARG_OID(1);
3037 : : text *result;
3038 : : int prettyFlags;
3039 : :
4839 tgl@sss.pgh.pa.us 3040 : 5525 : prettyFlags = PRETTYFLAG_INDENT;
3041 : :
816 3042 : 5525 : result = pg_get_expr_worker(expr, relid, prettyFlags);
3043 [ + - ]: 5525 : if (result)
3044 : 5525 : PG_RETURN_TEXT_P(result);
3045 : : else
816 tgl@sss.pgh.pa.us 3046 :UBC 0 : PG_RETURN_NULL();
3047 : : }
3048 : :
3049 : : Datum
8315 tgl@sss.pgh.pa.us 3050 :CBC 572 : pg_get_expr_ext(PG_FUNCTION_ARGS)
3051 : : {
3341 noah@leadboat.com 3052 : 572 : text *expr = PG_GETARG_TEXT_PP(0);
8310 bruce@momjian.us 3053 : 572 : Oid relid = PG_GETARG_OID(1);
8315 tgl@sss.pgh.pa.us 3054 : 572 : bool pretty = PG_GETARG_BOOL(2);
3055 : : text *result;
3056 : : int prettyFlags;
3057 : :
1499 3058 [ + - ]: 572 : prettyFlags = GET_PRETTY_FLAGS(pretty);
3059 : :
816 3060 : 572 : result = pg_get_expr_worker(expr, relid, prettyFlags);
3061 [ + - ]: 572 : if (result)
3062 : 572 : PG_RETURN_TEXT_P(result);
3063 : : else
816 tgl@sss.pgh.pa.us 3064 :UBC 0 : PG_RETURN_NULL();
3065 : : }
3066 : :
3067 : : static text *
816 tgl@sss.pgh.pa.us 3068 :CBC 6097 : pg_get_expr_worker(text *expr, Oid relid, int prettyFlags)
3069 : : {
3070 : : Node *node;
3071 : : Node *tst;
3072 : : Relids relids;
3073 : : List *context;
3074 : : char *exprstr;
3075 : 6097 : Relation rel = NULL;
3076 : : char *str;
3077 : :
3078 : : /* Convert input pg_node_tree (really TEXT) object to C string */
6615 3079 : 6097 : exprstr = text_to_cstring(expr);
3080 : :
3081 : : /* Convert expression to node tree */
9059 3082 : 6097 : node = (Node *) stringToNode(exprstr);
3083 : :
6188 3084 : 6097 : pfree(exprstr);
3085 : :
3086 : : /*
3087 : : * Throw error if the input is a querytree rather than an expression tree.
3088 : : * While we could support queries here, there seems no very good reason
3089 : : * to. In most such catalog columns, we'll see a List of Query nodes, or
3090 : : * even nested Lists, so drill down to a non-List node before checking.
3091 : : */
1577 3092 : 6097 : tst = node;
3093 [ + - - + ]: 6097 : while (tst && IsA(tst, List))
1577 tgl@sss.pgh.pa.us 3094 :UBC 0 : tst = linitial((List *) tst);
1577 tgl@sss.pgh.pa.us 3095 [ + - - + ]:CBC 6097 : if (tst && IsA(tst, Query))
1577 tgl@sss.pgh.pa.us 3096 [ # # ]:UBC 0 : ereport(ERROR,
3097 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3098 : : errmsg("input is a query, not an expression")));
3099 : :
3100 : : /*
3101 : : * Throw error if the expression contains Vars we won't be able to
3102 : : * deparse.
3103 : : */
1577 tgl@sss.pgh.pa.us 3104 :CBC 6097 : relids = pull_varnos(NULL, node);
3105 [ + + ]: 6097 : if (OidIsValid(relid))
3106 : : {
3107 [ - + ]: 6025 : if (!bms_is_subset(relids, bms_make_singleton(1)))
1577 tgl@sss.pgh.pa.us 3108 [ # # ]:UBC 0 : ereport(ERROR,
3109 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3110 : : errmsg("expression contains variables of more than one relation")));
3111 : : }
3112 : : else
3113 : : {
1577 tgl@sss.pgh.pa.us 3114 [ - + ]:CBC 72 : if (!bms_is_empty(relids))
1577 tgl@sss.pgh.pa.us 3115 [ # # ]:UBC 0 : ereport(ERROR,
3116 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3117 : : errmsg("expression contains variables")));
3118 : : }
3119 : :
3120 : : /*
3121 : : * Prepare deparse context if needed. If we are deparsing with a relid,
3122 : : * we need to transiently open and lock the rel, to make sure it won't go
3123 : : * away underneath us. (set_relation_column_names would lock it anyway,
3124 : : * so this isn't really introducing any new behavior.)
3125 : : */
6188 tgl@sss.pgh.pa.us 3126 [ + + ]:CBC 6097 : if (OidIsValid(relid))
3127 : : {
816 3128 : 6025 : rel = try_relation_open(relid, AccessShareLock);
3129 [ - + ]: 6025 : if (rel == NULL)
816 tgl@sss.pgh.pa.us 3130 :UBC 0 : return NULL;
816 tgl@sss.pgh.pa.us 3131 :CBC 6025 : context = deparse_context_for(RelationGetRelationName(rel), relid);
3132 : : }
3133 : : else
6188 3134 : 72 : context = NIL;
3135 : :
3136 : : /* Deparse */
8315 3137 : 6097 : str = deparse_expression_pretty(node, context, false, false,
3138 : : prettyFlags, 0);
3139 : :
816 3140 [ + + ]: 6097 : if (rel != NULL)
3141 : 6025 : relation_close(rel, AccessShareLock);
3142 : :
6188 3143 : 6097 : return string_to_text(str);
3144 : : }
3145 : :
3146 : :
3147 : : /* ----------
3148 : : * pg_get_userbyid - Get a user name by roleid and
3149 : : * fallback to 'unknown (OID=n)'
3150 : : * ----------
3151 : : */
3152 : : Datum
9457 3153 : 1267 : pg_get_userbyid(PG_FUNCTION_ARGS)
3154 : : {
7616 3155 : 1267 : Oid roleid = PG_GETARG_OID(0);
3156 : : Name result;
3157 : : HeapTuple roletup;
3158 : : Form_pg_authid role_rec;
3159 : :
3160 : : /*
3161 : : * Allocate space for the result
3162 : : */
9457 3163 : 1267 : result = (Name) palloc(NAMEDATALEN);
9676 bruce@momjian.us 3164 : 1267 : memset(NameStr(*result), 0, NAMEDATALEN);
3165 : :
3166 : : /*
3167 : : * Get the pg_authid entry and print the result
3168 : : */
5924 rhaas@postgresql.org 3169 : 1267 : roletup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
7616 tgl@sss.pgh.pa.us 3170 [ + - ]: 1267 : if (HeapTupleIsValid(roletup))
3171 : : {
3172 : 1267 : role_rec = (Form_pg_authid) GETSTRUCT(roletup);
2094 peter@eisentraut.org 3173 : 1267 : *result = role_rec->rolname;
7616 tgl@sss.pgh.pa.us 3174 : 1267 : ReleaseSysCache(roletup);
3175 : : }
3176 : : else
7616 tgl@sss.pgh.pa.us 3177 :UBC 0 : sprintf(NameStr(*result), "unknown (OID=%u)", roleid);
3178 : :
9457 tgl@sss.pgh.pa.us 3179 :CBC 1267 : PG_RETURN_NAME(result);
3180 : : }
3181 : :
3182 : :
3183 : : /*
3184 : : * pg_get_serial_sequence
3185 : : * Get the name of the sequence used by an identity or serial column,
3186 : : * formatted suitably for passing to setval, nextval or currval.
3187 : : * First parameter is not treated as double-quoted, second parameter
3188 : : * is --- see documentation for reason.
3189 : : */
3190 : : Datum
7984 3191 : 8 : pg_get_serial_sequence(PG_FUNCTION_ARGS)
3192 : : {
3341 noah@leadboat.com 3193 : 8 : text *tablename = PG_GETARG_TEXT_PP(0);
6615 tgl@sss.pgh.pa.us 3194 : 8 : text *columnname = PG_GETARG_TEXT_PP(1);
3195 : : RangeVar *tablerv;
3196 : : Oid tableOid;
3197 : : char *column;
3198 : : AttrNumber attnum;
7919 bruce@momjian.us 3199 : 8 : Oid sequenceId = InvalidOid;
3200 : : Relation depRel;
3201 : : ScanKeyData key[3];
3202 : : SysScanDesc scan;
3203 : : HeapTuple tup;
3204 : :
3205 : : /* Look up table name. Can't lock it - we might not have privileges. */
7648 neilc@samurai.com 3206 : 8 : tablerv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
5270 rhaas@postgresql.org 3207 : 8 : tableOid = RangeVarGetRelid(tablerv, NoLock, false);
3208 : :
3209 : : /* Get the number of the column */
6615 tgl@sss.pgh.pa.us 3210 : 8 : column = text_to_cstring(columnname);
3211 : :
7984 3212 : 8 : attnum = get_attnum(tableOid, column);
3213 [ - + ]: 8 : if (attnum == InvalidAttrNumber)
7984 tgl@sss.pgh.pa.us 3214 [ # # ]:UBC 0 : ereport(ERROR,
3215 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
3216 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
3217 : : column, tablerv->relname)));
3218 : :
3219 : : /* Search the dependency table for the dependent sequence */
2661 andres@anarazel.de 3220 :CBC 8 : depRel = table_open(DependRelationId, AccessShareLock);
3221 : :
7984 tgl@sss.pgh.pa.us 3222 : 8 : ScanKeyInit(&key[0],
3223 : : Anum_pg_depend_refclassid,
3224 : : BTEqualStrategyNumber, F_OIDEQ,
3225 : : ObjectIdGetDatum(RelationRelationId));
3226 : 8 : ScanKeyInit(&key[1],
3227 : : Anum_pg_depend_refobjid,
3228 : : BTEqualStrategyNumber, F_OIDEQ,
3229 : : ObjectIdGetDatum(tableOid));
3230 : 8 : ScanKeyInit(&key[2],
3231 : : Anum_pg_depend_refobjsubid,
3232 : : BTEqualStrategyNumber, F_INT4EQ,
3233 : : Int32GetDatum(attnum));
3234 : :
7691 3235 : 8 : scan = systable_beginscan(depRel, DependReferenceIndexId, true,
3236 : : NULL, 3, key);
3237 : :
7984 3238 [ + - ]: 20 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
3239 : : {
7919 bruce@momjian.us 3240 : 20 : Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
3241 : :
3242 : : /*
3243 : : * Look for an auto dependency (serial column) or internal dependency
3244 : : * (identity column) of a sequence on a column. (We need the relkind
3245 : : * test because indexes can also have auto dependencies on columns.)
3246 : : */
7691 tgl@sss.pgh.pa.us 3247 [ + + ]: 20 : if (deprec->classid == RelationRelationId &&
7984 3248 [ + - ]: 8 : deprec->objsubid == 0 &&
3154 peter_e@gmx.net 3249 [ + + ]: 8 : (deprec->deptype == DEPENDENCY_AUTO ||
3250 [ + - + - ]: 12 : deprec->deptype == DEPENDENCY_INTERNAL) &&
7116 tgl@sss.pgh.pa.us 3251 : 8 : get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
3252 : : {
7984 3253 : 8 : sequenceId = deprec->objid;
3254 : 8 : break;
3255 : : }
3256 : : }
3257 : :
3258 : 8 : systable_endscan(scan);
2661 andres@anarazel.de 3259 : 8 : table_close(depRel, AccessShareLock);
3260 : :
7984 tgl@sss.pgh.pa.us 3261 [ + - ]: 8 : if (OidIsValid(sequenceId))
3262 : : {
3263 : : char *result;
3264 : :
3819 3265 : 8 : result = generate_qualified_relation_name(sequenceId);
3266 : :
7984 3267 : 8 : PG_RETURN_TEXT_P(string_to_text(result));
3268 : : }
3269 : :
7984 tgl@sss.pgh.pa.us 3270 :UBC 0 : PG_RETURN_NULL();
3271 : : }
3272 : :
3273 : :
3274 : : /*
3275 : : * pg_get_functiondef
3276 : : * Returns the complete "CREATE OR REPLACE FUNCTION ..." statement for
3277 : : * the specified function.
3278 : : *
3279 : : * Note: if you change the output format of this function, be careful not
3280 : : * to break psql's rules (in \ef and \sf) for identifying the start of the
3281 : : * function body. To wit: the function body starts on a line that begins with
3282 : : * "AS ", "BEGIN ", or "RETURN ", and no preceding line will look like that.
3283 : : */
3284 : : Datum
6450 tgl@sss.pgh.pa.us 3285 :CBC 127 : pg_get_functiondef(PG_FUNCTION_ARGS)
3286 : : {
3287 : 127 : Oid funcid = PG_GETARG_OID(0);
3288 : : StringInfoData buf;
3289 : : StringInfoData dq;
3290 : : HeapTuple proctup;
3291 : : Form_pg_proc proc;
3292 : : bool isfunction;
3293 : : Datum tmp;
3294 : : bool isnull;
3295 : : const char *prosrc;
3296 : : const char *name;
3297 : : const char *nsp;
3298 : : float4 procost;
3299 : : int oldlen;
3300 : :
3301 : 127 : initStringInfo(&buf);
3302 : :
3303 : : /* Look up the function */
5924 rhaas@postgresql.org 3304 : 127 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
6450 tgl@sss.pgh.pa.us 3305 [ + + ]: 127 : if (!HeapTupleIsValid(proctup))
3570 rhaas@postgresql.org 3306 : 4 : PG_RETURN_NULL();
3307 : :
6450 tgl@sss.pgh.pa.us 3308 : 123 : proc = (Form_pg_proc) GETSTRUCT(proctup);
3309 : 123 : name = NameStr(proc->proname);
3310 : :
2986 peter_e@gmx.net 3311 [ - + ]: 123 : if (proc->prokind == PROKIND_AGGREGATE)
6450 tgl@sss.pgh.pa.us 3312 [ # # ]:UBC 0 : ereport(ERROR,
3313 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3314 : : errmsg("\"%s\" is an aggregate function", name)));
3315 : :
2986 peter_e@gmx.net 3316 :CBC 123 : isfunction = (proc->prokind != PROKIND_PROCEDURE);
3317 : :
3318 : : /*
3319 : : * We always qualify the function name, to ensure the right function gets
3320 : : * replaced.
3321 : : */
1743 tgl@sss.pgh.pa.us 3322 : 123 : nsp = get_namespace_name_or_temp(proc->pronamespace);
3003 peter_e@gmx.net 3323 [ + + ]: 123 : appendStringInfo(&buf, "CREATE OR REPLACE %s %s(",
3324 : : isfunction ? "FUNCTION" : "PROCEDURE",
3325 : : quote_qualified_identifier(nsp, name));
6361 3326 : 123 : (void) print_function_arguments(&buf, proctup, false, true);
3003 3327 : 123 : appendStringInfoString(&buf, ")\n");
3328 [ + + ]: 123 : if (isfunction)
3329 : : {
3330 : 110 : appendStringInfoString(&buf, " RETURNS ");
3331 : 110 : print_function_rettype(&buf, proctup);
3332 : 110 : appendStringInfoChar(&buf, '\n');
3333 : : }
3334 : :
4027 3335 : 123 : print_function_trftypes(&buf, proctup);
3336 : :
3003 3337 : 123 : appendStringInfo(&buf, " LANGUAGE %s\n",
3240 tgl@sss.pgh.pa.us 3338 : 123 : quote_identifier(get_language_name(proc->prolang, false)));
3339 : :
3340 : : /* Emit some miscellaneous options on one line */
6450 3341 : 123 : oldlen = buf.len;
3342 : :
2986 peter_e@gmx.net 3343 [ - + ]: 123 : if (proc->prokind == PROKIND_WINDOW)
6334 tgl@sss.pgh.pa.us 3344 :UBC 0 : appendStringInfoString(&buf, " WINDOW");
6450 tgl@sss.pgh.pa.us 3345 [ + + + - ]:CBC 123 : switch (proc->provolatile)
3346 : : {
3347 : 8 : case PROVOLATILE_IMMUTABLE:
3348 : 8 : appendStringInfoString(&buf, " IMMUTABLE");
3349 : 8 : break;
3350 : 20 : case PROVOLATILE_STABLE:
3351 : 20 : appendStringInfoString(&buf, " STABLE");
3352 : 20 : break;
3353 : 95 : case PROVOLATILE_VOLATILE:
3354 : 95 : break;
3355 : : }
3356 : :
3661 rhaas@postgresql.org 3357 [ + - + - ]: 123 : switch (proc->proparallel)
3358 : : {
3359 : 17 : case PROPARALLEL_SAFE:
3360 : 17 : appendStringInfoString(&buf, " PARALLEL SAFE");
3361 : 17 : break;
3661 rhaas@postgresql.org 3362 :UBC 0 : case PROPARALLEL_RESTRICTED:
3363 : 0 : appendStringInfoString(&buf, " PARALLEL RESTRICTED");
3364 : 0 : break;
3661 rhaas@postgresql.org 3365 :CBC 106 : case PROPARALLEL_UNSAFE:
3366 : 106 : break;
3367 : : }
3368 : :
6450 tgl@sss.pgh.pa.us 3369 [ + + ]: 123 : if (proc->proisstrict)
3370 : 32 : appendStringInfoString(&buf, " STRICT");
3371 [ + + ]: 123 : if (proc->prosecdef)
3372 : 4 : appendStringInfoString(&buf, " SECURITY DEFINER");
3995 3373 [ - + ]: 123 : if (proc->proleakproof)
3995 tgl@sss.pgh.pa.us 3374 :UBC 0 : appendStringInfoString(&buf, " LEAKPROOF");
3375 : :
3376 : : /* This code for the default cost and rows should match functioncmds.c */
6450 tgl@sss.pgh.pa.us 3377 [ + - ]:CBC 123 : if (proc->prolang == INTERNALlanguageId ||
3378 [ + + ]: 123 : proc->prolang == ClanguageId)
3379 : 5 : procost = 1;
3380 : : else
3381 : 118 : procost = 100;
3382 [ + + ]: 123 : if (proc->procost != procost)
3383 : 4 : appendStringInfo(&buf, " COST %g", proc->procost);
3384 : :
3385 [ + + - + ]: 123 : if (proc->prorows > 0 && proc->prorows != 1000)
6450 tgl@sss.pgh.pa.us 3386 :UBC 0 : appendStringInfo(&buf, " ROWS %g", proc->prorows);
3387 : :
2642 tgl@sss.pgh.pa.us 3388 [ - + ]:CBC 123 : if (proc->prosupport)
3389 : : {
3390 : : Oid argtypes[1];
3391 : :
3392 : : /*
3393 : : * We should qualify the support function's name if it wouldn't be
3394 : : * resolved by lookup in the current search path.
3395 : : */
2642 tgl@sss.pgh.pa.us 3396 :UBC 0 : argtypes[0] = INTERNALOID;
3397 : 0 : appendStringInfo(&buf, " SUPPORT %s",
3398 : : generate_function_name(proc->prosupport, 1,
3399 : : NIL, argtypes,
3400 : : false, NULL, false));
3401 : : }
3402 : :
6450 tgl@sss.pgh.pa.us 3403 [ + + ]:CBC 123 : if (oldlen != buf.len)
3404 : 41 : appendStringInfoChar(&buf, '\n');
3405 : :
3406 : : /* Emit any proconfig options, one per line */
3407 : 123 : tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
3408 [ + + ]: 123 : if (!isnull)
3409 : : {
6172 bruce@momjian.us 3410 : 4 : ArrayType *a = DatumGetArrayTypeP(tmp);
3411 : : int i;
3412 : :
6450 tgl@sss.pgh.pa.us 3413 [ - + ]: 4 : Assert(ARR_ELEMTYPE(a) == TEXTOID);
3414 [ - + ]: 4 : Assert(ARR_NDIM(a) == 1);
3415 [ - + ]: 4 : Assert(ARR_LBOUND(a)[0] == 1);
3416 : :
3417 [ + + ]: 28 : for (i = 1; i <= ARR_DIMS(a)[0]; i++)
3418 : : {
3419 : : Datum d;
3420 : :
3421 : 24 : d = array_ref(a, 1, &i,
3422 : : -1 /* varlenarray */ ,
3423 : : -1 /* TEXT's typlen */ ,
3424 : : false /* TEXT's typbyval */ ,
3425 : : TYPALIGN_INT /* TEXT's typalign */ ,
3426 : : &isnull);
3427 [ + - ]: 24 : if (!isnull)
3428 : : {
3429 : 24 : char *configitem = TextDatumGetCString(d);
3430 : : char *pos;
3431 : :
3432 : 24 : pos = strchr(configitem, '=');
3433 [ - + ]: 24 : if (pos == NULL)
6450 tgl@sss.pgh.pa.us 3434 :UBC 0 : continue;
6450 tgl@sss.pgh.pa.us 3435 :CBC 24 : *pos++ = '\0';
3436 : :
3437 : 24 : appendStringInfo(&buf, " SET %s TO ",
3438 : : quote_identifier(configitem));
3439 : :
3440 : : /*
3441 : : * Variables that are marked GUC_LIST_QUOTE were already fully
3442 : : * quoted by flatten_set_variable_args() before they were put
3443 : : * into the proconfig array. However, because the quoting
3444 : : * rules used there aren't exactly like SQL's, we have to
3445 : : * break the list value apart and then quote the elements as
3446 : : * string literals. (The elements may be double-quoted as-is,
3447 : : * but we can't just feed them to the SQL parser; it would do
3448 : : * the wrong thing with elements that are zero-length or
3449 : : * longer than NAMEDATALEN.) Also, we need a special case for
3450 : : * empty lists.
3451 : : *
3452 : : * Variables that are not so marked should just be emitted as
3453 : : * simple string literals. If the variable is not known to
3454 : : * guc.c, we'll do that; this makes it unsafe to use
3455 : : * GUC_LIST_QUOTE for extension variables.
3456 : : */
2967 3457 [ + + ]: 24 : if (GetConfigOptionFlags(configitem, true) & GUC_LIST_QUOTE)
3458 : : {
3459 : : List *namelist;
3460 : : ListCell *lc;
3461 : :
3462 : : /* Parse string into list of identifiers */
2835 3463 [ - + ]: 12 : if (!SplitGUCList(pos, ',', &namelist))
3464 : : {
3465 : : /* this shouldn't fail really */
2835 tgl@sss.pgh.pa.us 3466 [ # # ]:UBC 0 : elog(ERROR, "invalid list syntax in proconfig item");
3467 : : }
3468 : : /* Special case: represent an empty list as NULL */
182 tgl@sss.pgh.pa.us 3469 [ + + ]:GNC 12 : if (namelist == NIL)
3470 : 4 : appendStringInfoString(&buf, "NULL");
2835 tgl@sss.pgh.pa.us 3471 [ + + + + :CBC 32 : foreach(lc, namelist)
+ + ]
3472 : : {
3473 : 20 : char *curname = (char *) lfirst(lc);
3474 : :
3475 : 20 : simple_quote_literal(&buf, curname);
2486 3476 [ + + ]: 20 : if (lnext(namelist, lc))
2835 3477 : 12 : appendStringInfoString(&buf, ", ");
3478 : : }
3479 : : }
3480 : : else
6450 3481 : 12 : simple_quote_literal(&buf, pos);
3482 : 24 : appendStringInfoChar(&buf, '\n');
3483 : : }
3484 : : }
3485 : : }
3486 : :
3487 : : /* And finally the function definition ... */
1708 3488 : 123 : (void) SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
1854 peter@eisentraut.org 3489 [ + + + + ]: 123 : if (proc->prolang == SQLlanguageId && !isnull)
3490 : : {
3491 : 91 : print_function_sqlbody(&buf, proctup);
3492 : : }
3493 : : else
3494 : : {
1819 tgl@sss.pgh.pa.us 3495 : 32 : appendStringInfoString(&buf, "AS ");
3496 : :
3497 : 32 : tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
3498 [ + + ]: 32 : if (!isnull)
3499 : : {
3500 : 5 : simple_quote_literal(&buf, TextDatumGetCString(tmp));
3501 : 5 : appendStringInfoString(&buf, ", "); /* assume prosrc isn't null */
3502 : : }
3503 : :
1137 dgustafsson@postgres 3504 : 32 : tmp = SysCacheGetAttrNotNull(PROCOID, proctup, Anum_pg_proc_prosrc);
1819 tgl@sss.pgh.pa.us 3505 : 32 : prosrc = TextDatumGetCString(tmp);
3506 : :
3507 : : /*
3508 : : * We always use dollar quoting. Figure out a suitable delimiter.
3509 : : *
3510 : : * Since the user is likely to be editing the function body string, we
3511 : : * shouldn't use a short delimiter that he might easily create a
3512 : : * conflict with. Hence prefer "$function$"/"$procedure$", but extend
3513 : : * if needed.
3514 : : */
3515 : 32 : initStringInfo(&dq);
3516 : 32 : appendStringInfoChar(&dq, '$');
3517 [ + + ]: 32 : appendStringInfoString(&dq, (isfunction ? "function" : "procedure"));
3518 [ - + ]: 32 : while (strstr(prosrc, dq.data) != NULL)
1819 tgl@sss.pgh.pa.us 3519 :UBC 0 : appendStringInfoChar(&dq, 'x');
1819 tgl@sss.pgh.pa.us 3520 :CBC 32 : appendStringInfoChar(&dq, '$');
3521 : :
3522 : 32 : appendBinaryStringInfo(&buf, dq.data, dq.len);
3523 : 32 : appendStringInfoString(&buf, prosrc);
3524 : 32 : appendBinaryStringInfo(&buf, dq.data, dq.len);
3525 : : }
3526 : :
4569 rhaas@postgresql.org 3527 : 123 : appendStringInfoChar(&buf, '\n');
3528 : :
6450 tgl@sss.pgh.pa.us 3529 : 123 : ReleaseSysCache(proctup);
3530 : :
3531 : 123 : PG_RETURN_TEXT_P(string_to_text(buf.data));
3532 : : }
3533 : :
3534 : : /*
3535 : : * pg_get_function_arguments
3536 : : * Get a nicely-formatted list of arguments for a function.
3537 : : * This is everything that would go between the parentheses in
3538 : : * CREATE FUNCTION.
3539 : : */
3540 : : Datum
6500 3541 : 2491 : pg_get_function_arguments(PG_FUNCTION_ARGS)
3542 : : {
3543 : 2491 : Oid funcid = PG_GETARG_OID(0);
3544 : : StringInfoData buf;
3545 : : HeapTuple proctup;
3546 : :
5924 rhaas@postgresql.org 3547 : 2491 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
6500 tgl@sss.pgh.pa.us 3548 [ + + ]: 2491 : if (!HeapTupleIsValid(proctup))
3567 rhaas@postgresql.org 3549 : 4 : PG_RETURN_NULL();
3550 : :
3551 : 2487 : initStringInfo(&buf);
3552 : :
6361 peter_e@gmx.net 3553 : 2487 : (void) print_function_arguments(&buf, proctup, false, true);
3554 : :
6500 tgl@sss.pgh.pa.us 3555 : 2487 : ReleaseSysCache(proctup);
3556 : :
3557 : 2487 : PG_RETURN_TEXT_P(string_to_text(buf.data));
3558 : : }
3559 : :
3560 : : /*
3561 : : * pg_get_function_identity_arguments
3562 : : * Get a formatted list of arguments for a function.
3563 : : * This is everything that would go between the parentheses in
3564 : : * ALTER FUNCTION, etc. In particular, don't print defaults.
3565 : : */
3566 : : Datum
6361 peter_e@gmx.net 3567 : 2125 : pg_get_function_identity_arguments(PG_FUNCTION_ARGS)
3568 : : {
3569 : 2125 : Oid funcid = PG_GETARG_OID(0);
3570 : : StringInfoData buf;
3571 : : HeapTuple proctup;
3572 : :
5924 rhaas@postgresql.org 3573 : 2125 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
6361 peter_e@gmx.net 3574 [ + + ]: 2125 : if (!HeapTupleIsValid(proctup))
3567 rhaas@postgresql.org 3575 : 4 : PG_RETURN_NULL();
3576 : :
3577 : 2121 : initStringInfo(&buf);
3578 : :
6361 peter_e@gmx.net 3579 : 2121 : (void) print_function_arguments(&buf, proctup, false, false);
3580 : :
3581 : 2121 : ReleaseSysCache(proctup);
3582 : :
3583 : 2121 : PG_RETURN_TEXT_P(string_to_text(buf.data));
3584 : : }
3585 : :
3586 : : /*
3587 : : * pg_get_function_result
3588 : : * Get a nicely-formatted version of the result type of a function.
3589 : : * This is what would appear after RETURNS in CREATE FUNCTION.
3590 : : */
3591 : : Datum
6500 tgl@sss.pgh.pa.us 3592 : 2193 : pg_get_function_result(PG_FUNCTION_ARGS)
3593 : : {
3594 : 2193 : Oid funcid = PG_GETARG_OID(0);
3595 : : StringInfoData buf;
3596 : : HeapTuple proctup;
3597 : :
5924 rhaas@postgresql.org 3598 : 2193 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
6500 tgl@sss.pgh.pa.us 3599 [ + + ]: 2193 : if (!HeapTupleIsValid(proctup))
3567 rhaas@postgresql.org 3600 : 4 : PG_RETURN_NULL();
3601 : :
2986 peter_e@gmx.net 3602 [ + + ]: 2189 : if (((Form_pg_proc) GETSTRUCT(proctup))->prokind == PROKIND_PROCEDURE)
3603 : : {
3078 3604 : 132 : ReleaseSysCache(proctup);
3605 : 132 : PG_RETURN_NULL();
3606 : : }
3607 : :
3567 rhaas@postgresql.org 3608 : 2057 : initStringInfo(&buf);
3609 : :
6450 tgl@sss.pgh.pa.us 3610 : 2057 : print_function_rettype(&buf, proctup);
3611 : :
3612 : 2057 : ReleaseSysCache(proctup);
3613 : :
3614 : 2057 : PG_RETURN_TEXT_P(string_to_text(buf.data));
3615 : : }
3616 : :
3617 : : /*
3618 : : * Guts of pg_get_function_result: append the function's return type
3619 : : * to the specified buffer.
3620 : : */
3621 : : static void
3622 : 2167 : print_function_rettype(StringInfo buf, HeapTuple proctup)
3623 : : {
3624 : 2167 : Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
3625 : 2167 : int ntabargs = 0;
3626 : : StringInfoData rbuf;
3627 : :
3628 : 2167 : initStringInfo(&rbuf);
3629 : :
3630 [ + + ]: 2167 : if (proc->proretset)
3631 : : {
3632 : : /* It might be a table function; try to print the arguments */
3633 : 212 : appendStringInfoString(&rbuf, "TABLE(");
6347 3634 : 212 : ntabargs = print_function_arguments(&rbuf, proctup, true, false);
6500 3635 [ + + ]: 212 : if (ntabargs > 0)
4012 peter_e@gmx.net 3636 : 44 : appendStringInfoChar(&rbuf, ')');
3637 : : else
6450 tgl@sss.pgh.pa.us 3638 : 168 : resetStringInfo(&rbuf);
3639 : : }
3640 : :
6500 3641 [ + + ]: 2167 : if (ntabargs == 0)
3642 : : {
3643 : : /* Not a table function, so do the normal thing */
6450 3644 [ + + ]: 2123 : if (proc->proretset)
3645 : 168 : appendStringInfoString(&rbuf, "SETOF ");
3646 : 2123 : appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
3647 : : }
3648 : :
2478 drowley@postgresql.o 3649 : 2167 : appendBinaryStringInfo(buf, rbuf.data, rbuf.len);
6500 tgl@sss.pgh.pa.us 3650 : 2167 : }
3651 : :
3652 : : /*
3653 : : * Common code for pg_get_function_arguments and pg_get_function_result:
3654 : : * append the desired subset of arguments to buf. We print only TABLE
3655 : : * arguments when print_table_args is true, and all the others when it's false.
3656 : : * We print argument defaults only if print_defaults is true.
3657 : : * Function return value is the number of arguments printed.
3658 : : */
3659 : : static int
3660 : 4943 : print_function_arguments(StringInfo buf, HeapTuple proctup,
3661 : : bool print_table_args, bool print_defaults)
3662 : : {
6347 3663 : 4943 : Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
3664 : : int numargs;
3665 : : Oid *argtypes;
3666 : : char **argnames;
3667 : : char *argmodes;
4516 3668 : 4943 : int insertorderbyat = -1;
3669 : : int argsprinted;
3670 : : int inputargno;
3671 : : int nlackdefaults;
2486 3672 : 4943 : List *argdefaults = NIL;
6347 3673 : 4943 : ListCell *nextargdefault = NULL;
3674 : : int i;
3675 : :
6500 3676 : 4943 : numargs = get_func_arg_info(proctup,
3677 : : &argtypes, &argnames, &argmodes);
3678 : :
6347 3679 : 4943 : nlackdefaults = numargs;
3680 [ + + + + ]: 4943 : if (print_defaults && proc->pronargdefaults > 0)
3681 : : {
3682 : : Datum proargdefaults;
3683 : : bool isnull;
3684 : :
3685 : 21 : proargdefaults = SysCacheGetAttr(PROCOID, proctup,
3686 : : Anum_pg_proc_proargdefaults,
3687 : : &isnull);
3688 [ + - ]: 21 : if (!isnull)
3689 : : {
3690 : : char *str;
3691 : :
3692 : 21 : str = TextDatumGetCString(proargdefaults);
3360 peter_e@gmx.net 3693 : 21 : argdefaults = castNode(List, stringToNode(str));
6347 tgl@sss.pgh.pa.us 3694 : 21 : pfree(str);
3695 : 21 : nextargdefault = list_head(argdefaults);
3696 : : /* nlackdefaults counts only *input* arguments lacking defaults */
3697 : 21 : nlackdefaults = proc->pronargs - list_length(argdefaults);
3698 : : }
3699 : : }
3700 : :
3701 : : /* Check for special treatment of ordered-set aggregates */
2986 peter_e@gmx.net 3702 [ + + ]: 4943 : if (proc->prokind == PROKIND_AGGREGATE)
3703 : : {
3704 : : HeapTuple aggtup;
3705 : : Form_pg_aggregate agg;
3706 : :
1020 michael@paquier.xyz 3707 : 594 : aggtup = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(proc->oid));
4516 tgl@sss.pgh.pa.us 3708 [ - + ]: 594 : if (!HeapTupleIsValid(aggtup))
4516 tgl@sss.pgh.pa.us 3709 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for aggregate %u",
3710 : : proc->oid);
4516 tgl@sss.pgh.pa.us 3711 :CBC 594 : agg = (Form_pg_aggregate) GETSTRUCT(aggtup);
3712 [ + + ]: 594 : if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3713 : 28 : insertorderbyat = agg->aggnumdirectargs;
3714 : 594 : ReleaseSysCache(aggtup);
3715 : : }
3716 : :
6500 3717 : 4943 : argsprinted = 0;
6347 3718 : 4943 : inputargno = 0;
6500 3719 [ + + ]: 10062 : for (i = 0; i < numargs; i++)
3720 : : {
6172 bruce@momjian.us 3721 : 5119 : Oid argtype = argtypes[i];
3722 [ + + ]: 5119 : char *argname = argnames ? argnames[i] : NULL;
3723 [ + + ]: 5119 : char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
3724 : : const char *modename;
3725 : : bool isinput;
3726 : :
6500 tgl@sss.pgh.pa.us 3727 [ + + + + : 5119 : switch (argmode)
+ - ]
3728 : : {
3729 : 4184 : case PROARGMODE_IN:
3730 : :
3731 : : /*
3732 : : * For procedures, explicitly mark all argument modes, so as
3733 : : * to avoid ambiguity with the SQL syntax for DROP PROCEDURE.
3734 : : */
1790 3735 [ + + ]: 4184 : if (proc->prokind == PROKIND_PROCEDURE)
3736 : 291 : modename = "IN ";
3737 : : else
3738 : 3893 : modename = "";
6347 3739 : 4184 : isinput = true;
6500 3740 : 4184 : break;
3741 : 50 : case PROARGMODE_INOUT:
3742 : 50 : modename = "INOUT ";
6347 3743 : 50 : isinput = true;
6500 3744 : 50 : break;
3745 : 517 : case PROARGMODE_OUT:
3746 : 517 : modename = "OUT ";
6347 3747 : 517 : isinput = false;
6500 3748 : 517 : break;
3749 : 92 : case PROARGMODE_VARIADIC:
3750 : 92 : modename = "VARIADIC ";
6347 3751 : 92 : isinput = true;
6500 3752 : 92 : break;
3753 : 276 : case PROARGMODE_TABLE:
3754 : 276 : modename = "";
6347 3755 : 276 : isinput = false;
6500 3756 : 276 : break;
6500 tgl@sss.pgh.pa.us 3757 :UBC 0 : default:
3758 [ # # ]: 0 : elog(ERROR, "invalid parameter mode '%c'", argmode);
3759 : : modename = NULL; /* keep compiler quiet */
3760 : : isinput = false;
3761 : : break;
3762 : : }
6347 tgl@sss.pgh.pa.us 3763 [ + + ]:CBC 5119 : if (isinput)
3764 : 4326 : inputargno++; /* this is a 1-based counter */
3765 : :
3766 [ + + ]: 5119 : if (print_table_args != (argmode == PROARGMODE_TABLE))
3767 : 441 : continue;
3768 : :
4516 3769 [ + + ]: 4678 : if (argsprinted == insertorderbyat)
3770 : : {
3771 [ + - ]: 28 : if (argsprinted)
3772 : 28 : appendStringInfoChar(buf, ' ');
3773 : 28 : appendStringInfoString(buf, "ORDER BY ");
3774 : : }
3775 [ + + ]: 4650 : else if (argsprinted)
6500 3776 : 1530 : appendStringInfoString(buf, ", ");
3777 : :
3778 : 4678 : appendStringInfoString(buf, modename);
6347 3779 [ + + + + ]: 4678 : if (argname && argname[0])
6174 3780 : 1729 : appendStringInfo(buf, "%s ", quote_identifier(argname));
6500 3781 : 4678 : appendStringInfoString(buf, format_type_be(argtype));
6347 3782 [ + + + + : 4678 : if (print_defaults && isinput && inputargno > nlackdefaults)
+ + ]
3783 : : {
3784 : : Node *expr;
3785 : :
3786 [ - + ]: 32 : Assert(nextargdefault != NULL);
3787 : 32 : expr = (Node *) lfirst(nextargdefault);
2486 3788 : 32 : nextargdefault = lnext(argdefaults, nextargdefault);
3789 : :
6347 3790 : 32 : appendStringInfo(buf, " DEFAULT %s",
3791 : : deparse_expression(expr, NIL, false, false));
3792 : : }
6500 3793 : 4678 : argsprinted++;
3794 : :
3795 : : /* nasty hack: print the last arg twice for variadic ordered-set agg */
4516 3796 [ + + + + ]: 4678 : if (argsprinted == insertorderbyat && i == numargs - 1)
3797 : : {
3798 : 14 : i--;
3799 : : /* aggs shouldn't have defaults anyway, but just to be sure ... */
3800 : 14 : print_defaults = false;
3801 : : }
3802 : : }
3803 : :
6500 3804 : 4943 : return argsprinted;
3805 : : }
3806 : :
3807 : : static bool
4543 peter_e@gmx.net 3808 : 64 : is_input_argument(int nth, const char *argmodes)
3809 : : {
3810 : : return (!argmodes
3811 [ + + ]: 28 : || argmodes[nth] == PROARGMODE_IN
3812 [ + - ]: 12 : || argmodes[nth] == PROARGMODE_INOUT
3813 [ + + - + ]: 92 : || argmodes[nth] == PROARGMODE_VARIADIC);
3814 : : }
3815 : :
3816 : : /*
3817 : : * Append used transformed types to specified buffer
3818 : : */
3819 : : static void
4027 3820 : 123 : print_function_trftypes(StringInfo buf, HeapTuple proctup)
3821 : : {
3822 : : Oid *trftypes;
3823 : : int ntypes;
3824 : :
3825 : 123 : ntypes = get_func_trftypes(proctup, &trftypes);
3826 [ + + ]: 123 : if (ntypes > 0)
3827 : : {
3828 : : int i;
3829 : :
1926 tgl@sss.pgh.pa.us 3830 : 3 : appendStringInfoString(buf, " TRANSFORM ");
4027 peter_e@gmx.net 3831 [ + + ]: 8 : for (i = 0; i < ntypes; i++)
3832 : : {
3833 [ + + ]: 5 : if (i != 0)
3834 : 2 : appendStringInfoString(buf, ", ");
4016 magnus@hagander.net 3835 : 5 : appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
3836 : : }
1926 tgl@sss.pgh.pa.us 3837 : 3 : appendStringInfoChar(buf, '\n');
3838 : : }
4027 peter_e@gmx.net 3839 : 123 : }
3840 : :
3841 : : /*
3842 : : * Get textual representation of a function argument's default value. The
3843 : : * second argument of this function is the argument number among all arguments
3844 : : * (i.e. proallargtypes, *not* proargtypes), starting with 1, because that's
3845 : : * how information_schema.sql uses it.
3846 : : */
3847 : : Datum
4543 3848 : 36 : pg_get_function_arg_default(PG_FUNCTION_ARGS)
3849 : : {
3850 : 36 : Oid funcid = PG_GETARG_OID(0);
3851 : 36 : int32 nth_arg = PG_GETARG_INT32(1);
3852 : : HeapTuple proctup;
3853 : : Form_pg_proc proc;
3854 : : int numargs;
3855 : : Oid *argtypes;
3856 : : char **argnames;
3857 : : char *argmodes;
3858 : : int i;
3859 : : List *argdefaults;
3860 : : Node *node;
3861 : : char *str;
3862 : : int nth_inputarg;
3863 : : Datum proargdefaults;
3864 : : bool isnull;
3865 : : int nth_default;
3866 : :
3867 : 36 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3868 [ + + ]: 36 : if (!HeapTupleIsValid(proctup))
3567 rhaas@postgresql.org 3869 : 8 : PG_RETURN_NULL();
3870 : :
4543 peter_e@gmx.net 3871 : 28 : numargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
3872 [ + - + - : 28 : if (nth_arg < 1 || nth_arg > numargs || !is_input_argument(nth_arg - 1, argmodes))
+ + ]
3873 : : {
3874 : 8 : ReleaseSysCache(proctup);
3875 : 8 : PG_RETURN_NULL();
3876 : : }
3877 : :
3878 : 20 : nth_inputarg = 0;
3879 [ + + ]: 56 : for (i = 0; i < nth_arg; i++)
3880 [ + + ]: 36 : if (is_input_argument(i, argmodes))
3881 : 32 : nth_inputarg++;
3882 : :
3883 : 20 : proargdefaults = SysCacheGetAttr(PROCOID, proctup,
3884 : : Anum_pg_proc_proargdefaults,
3885 : : &isnull);
3886 [ - + ]: 20 : if (isnull)
3887 : : {
4543 peter_e@gmx.net 3888 :UBC 0 : ReleaseSysCache(proctup);
3889 : 0 : PG_RETURN_NULL();
3890 : : }
3891 : :
4543 peter_e@gmx.net 3892 :CBC 20 : str = TextDatumGetCString(proargdefaults);
3360 3893 : 20 : argdefaults = castNode(List, stringToNode(str));
4543 3894 : 20 : pfree(str);
3895 : :
3896 : 20 : proc = (Form_pg_proc) GETSTRUCT(proctup);
3897 : :
3898 : : /*
3899 : : * Calculate index into proargdefaults: proargdefaults corresponds to the
3900 : : * last N input arguments, where N = pronargdefaults.
3901 : : */
3902 : 20 : nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3903 : :
3904 [ + + - + ]: 20 : if (nth_default < 0 || nth_default >= list_length(argdefaults))
3905 : : {
3906 : 4 : ReleaseSysCache(proctup);
3907 : 4 : PG_RETURN_NULL();
3908 : : }
3909 : 16 : node = list_nth(argdefaults, nth_default);
3910 : 16 : str = deparse_expression(node, NIL, false, false);
3911 : :
3912 : 16 : ReleaseSysCache(proctup);
3913 : :
3914 : 16 : PG_RETURN_TEXT_P(string_to_text(str));
3915 : : }
3916 : :
3917 : : static void
1854 peter@eisentraut.org 3918 : 139 : print_function_sqlbody(StringInfo buf, HeapTuple proctup)
3919 : : {
3920 : : int numargs;
3921 : : Oid *argtypes;
3922 : : char **argnames;
3923 : : char *argmodes;
3924 : 139 : deparse_namespace dpns = {0};
3925 : : Datum tmp;
3926 : : Node *n;
3927 : :
3928 : 139 : dpns.funcname = pstrdup(NameStr(((Form_pg_proc) GETSTRUCT(proctup))->proname));
3929 : 139 : numargs = get_func_arg_info(proctup,
3930 : : &argtypes, &argnames, &argmodes);
3931 : 139 : dpns.numargs = numargs;
3932 : 139 : dpns.argnames = argnames;
3933 : :
1137 dgustafsson@postgres 3934 : 139 : tmp = SysCacheGetAttrNotNull(PROCOID, proctup, Anum_pg_proc_prosqlbody);
1854 peter@eisentraut.org 3935 : 139 : n = stringToNode(TextDatumGetCString(tmp));
3936 : :
3937 [ + + ]: 139 : if (IsA(n, List))
3938 : : {
3939 : : List *stmts;
3940 : : ListCell *lc;
3941 : :
3942 : 112 : stmts = linitial(castNode(List, n));
3943 : :
3944 : 112 : appendStringInfoString(buf, "BEGIN ATOMIC\n");
3945 : :
3946 [ + + + + : 219 : foreach(lc, stmts)
+ + ]
3947 : : {
3948 : 107 : Query *query = lfirst_node(Query, lc);
3949 : :
3950 : : /* It seems advisable to get at least AccessShareLock on rels */
1708 tgl@sss.pgh.pa.us 3951 : 107 : AcquireRewriteLocks(query, false, false);
1445 3952 : 107 : get_query_def(query, buf, list_make1(&dpns), NULL, false,
3953 : : PRETTYFLAG_INDENT, WRAP_COLUMN_DEFAULT, 1);
1854 peter@eisentraut.org 3954 : 107 : appendStringInfoChar(buf, ';');
3955 : 107 : appendStringInfoChar(buf, '\n');
3956 : : }
3957 : :
3958 : 112 : appendStringInfoString(buf, "END");
3959 : : }
3960 : : else
3961 : : {
1708 tgl@sss.pgh.pa.us 3962 : 27 : Query *query = castNode(Query, n);
3963 : :
3964 : : /* It seems advisable to get at least AccessShareLock on rels */
3965 : 27 : AcquireRewriteLocks(query, false, false);
1445 3966 : 27 : get_query_def(query, buf, list_make1(&dpns), NULL, false,
3967 : : 0, WRAP_COLUMN_DEFAULT, 0);
3968 : : }
1854 peter@eisentraut.org 3969 : 139 : }
3970 : :
3971 : : Datum
3972 : 1836 : pg_get_function_sqlbody(PG_FUNCTION_ARGS)
3973 : : {
3974 : 1836 : Oid funcid = PG_GETARG_OID(0);
3975 : : StringInfoData buf;
3976 : : HeapTuple proctup;
3977 : : bool isnull;
3978 : :
3979 : 1836 : initStringInfo(&buf);
3980 : :
3981 : : /* Look up the function */
3982 : 1836 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3983 [ - + ]: 1836 : if (!HeapTupleIsValid(proctup))
1854 peter@eisentraut.org 3984 :UBC 0 : PG_RETURN_NULL();
3985 : :
1708 tgl@sss.pgh.pa.us 3986 :CBC 1836 : (void) SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
1854 peter@eisentraut.org 3987 [ + + ]: 1836 : if (isnull)
3988 : : {
3989 : 1788 : ReleaseSysCache(proctup);
3990 : 1788 : PG_RETURN_NULL();
3991 : : }
3992 : :
3993 : 48 : print_function_sqlbody(&buf, proctup);
3994 : :
3995 : 48 : ReleaseSysCache(proctup);
3996 : :
1337 drowley@postgresql.o 3997 : 48 : PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len));
3998 : : }
3999 : :
4000 : :
4001 : : /*
4002 : : * deparse_expression - General utility for deparsing expressions
4003 : : *
4004 : : * calls deparse_expression_pretty with all prettyPrinting disabled
4005 : : */
4006 : : char *
8315 tgl@sss.pgh.pa.us 4007 : 54180 : deparse_expression(Node *expr, List *dpcontext,
4008 : : bool forceprefix, bool showimplicit)
4009 : : {
8310 bruce@momjian.us 4010 : 54180 : return deparse_expression_pretty(expr, dpcontext, forceprefix,
4011 : : showimplicit, 0, 0);
4012 : : }
4013 : :
4014 : : /* ----------
4015 : : * deparse_expression_pretty - General utility for deparsing expressions
4016 : : *
4017 : : * expr is the node tree to be deparsed. It must be a transformed expression
4018 : : * tree (ie, not the raw output of gram.y).
4019 : : *
4020 : : * dpcontext is a list of deparse_namespace nodes representing the context
4021 : : * for interpreting Vars in the node tree. It can be NIL if no Vars are
4022 : : * expected.
4023 : : *
4024 : : * forceprefix is true to force all Vars to be prefixed with their table names.
4025 : : *
4026 : : * showimplicit is true to force all implicit casts to be shown explicitly.
4027 : : *
4028 : : * Tries to pretty up the output according to prettyFlags and startIndent.
4029 : : *
4030 : : * The result is a palloc'd string.
4031 : : * ----------
4032 : : */
4033 : : static char *
8315 tgl@sss.pgh.pa.us 4034 : 63070 : deparse_expression_pretty(Node *expr, List *dpcontext,
4035 : : bool forceprefix, bool showimplicit,
4036 : : int prettyFlags, int startIndent)
4037 : : {
4038 : : StringInfoData buf;
4039 : : deparse_context context;
4040 : :
9711 4041 : 63070 : initStringInfo(&buf);
4042 : 63070 : context.buf = &buf;
9211 4043 : 63070 : context.namespaces = dpcontext;
614 4044 : 63070 : context.resultDesc = NULL;
4045 : 63070 : context.targetList = NIL;
6337 4046 : 63070 : context.windowClause = NIL;
9211 4047 : 63070 : context.varprefix = forceprefix;
8315 4048 : 63070 : context.prettyFlags = prettyFlags;
4880 4049 : 63070 : context.wrapColumn = WRAP_COLUMN_DEFAULT;
8315 4050 : 63070 : context.indentLevel = startIndent;
614 4051 : 63070 : context.colNamesVisible = true;
4052 : 63070 : context.inGroupBy = false;
4053 : 63070 : context.varInOrderBy = false;
2337 4054 : 63070 : context.appendparents = NULL;
4055 : :
8629 4056 : 63070 : get_rule_expr(expr, &context, showimplicit);
4057 : :
9711 4058 : 63070 : return buf.data;
4059 : : }
4060 : :
4061 : : /* ----------
4062 : : * deparse_context_for - Build deparse context for a single relation
4063 : : *
4064 : : * Given the reference name (alias) and OID of a relation, build deparsing
4065 : : * context for an expression referencing only that relation (as varno 1,
4066 : : * varlevelsup 0). This is sufficient for many uses of deparse_expression.
4067 : : * ----------
4068 : : */
4069 : : List *
8810 4070 : 15453 : deparse_context_for(const char *aliasname, Oid relid)
4071 : : {
4072 : : deparse_namespace *dpns;
4073 : : RangeTblEntry *rte;
4074 : :
146 michael@paquier.xyz 4075 :GNC 15453 : dpns = palloc0_object(deparse_namespace);
4076 : :
4077 : : /* Build a minimal RTE for the rel */
9211 tgl@sss.pgh.pa.us 4078 :CBC 15453 : rte = makeNode(RangeTblEntry);
8820 4079 : 15453 : rte->rtekind = RTE_RELATION;
9211 4080 : 15453 : rte->relid = relid;
5551 4081 : 15453 : rte->relkind = RELKIND_RELATION; /* no need for exactness here */
2774 4082 : 15453 : rte->rellockmode = AccessShareLock;
4974 4083 : 15453 : rte->alias = makeAlias(aliasname, NIL);
4084 : 15453 : rte->eref = rte->alias;
5019 4085 : 15453 : rte->lateral = false;
9211 4086 : 15453 : rte->inh = false;
4087 : 15453 : rte->inFromCl = true;
4088 : :
4089 : : /* Build one-element rtable */
8010 neilc@samurai.com 4090 : 15453 : dpns->rtable = list_make1(rte);
2337 tgl@sss.pgh.pa.us 4091 : 15453 : dpns->subplans = NIL;
6420 4092 : 15453 : dpns->ctes = NIL;
2337 4093 : 15453 : dpns->appendrels = NULL;
4974 4094 : 15453 : set_rtable_names(dpns, NIL, NULL);
4873 4095 : 15453 : set_simple_column_names(dpns);
4096 : :
4097 : : /* Return a one-deep namespace stack */
8010 neilc@samurai.com 4098 : 15453 : return list_make1(dpns);
4099 : : }
4100 : :
4101 : : /*
4102 : : * deparse_context_for_plan_tree - Build deparse context for a Plan tree
4103 : : *
4104 : : * When deparsing an expression in a Plan tree, we use the plan's rangetable
4105 : : * to resolve names of simple Vars. The initialization of column names for
4106 : : * this is rather expensive if the rangetable is large, and it'll be the same
4107 : : * for every expression in the Plan tree; so we do it just once and re-use
4108 : : * the result of this function for each expression. (Note that the result
4109 : : * is not usable until set_deparse_context_plan() is applied to it.)
4110 : : *
4111 : : * In addition to the PlannedStmt, pass the per-RTE alias names
4112 : : * assigned by a previous call to select_rtable_names_for_explain.
4113 : : */
4114 : : List *
2337 tgl@sss.pgh.pa.us 4115 : 16390 : deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
4116 : : {
4117 : : deparse_namespace *dpns;
4118 : :
146 michael@paquier.xyz 4119 :GNC 16390 : dpns = palloc0_object(deparse_namespace);
4120 : :
4121 : : /* Initialize fields that stay the same across the whole plan tree */
2337 tgl@sss.pgh.pa.us 4122 :CBC 16390 : dpns->rtable = pstmt->rtable;
4128 4123 : 16390 : dpns->rtable_names = rtable_names;
2337 4124 : 16390 : dpns->subplans = pstmt->subplans;
4128 4125 : 16390 : dpns->ctes = NIL;
2337 4126 [ + + ]: 16390 : if (pstmt->appendRelations)
4127 : : {
4128 : : /* Set up the array, indexed by child relid */
4129 : 2635 : int ntables = list_length(dpns->rtable);
4130 : : ListCell *lc;
4131 : :
4132 : 2635 : dpns->appendrels = (AppendRelInfo **)
4133 : 2635 : palloc0((ntables + 1) * sizeof(AppendRelInfo *));
4134 [ + - + + : 14560 : foreach(lc, pstmt->appendRelations)
+ + ]
4135 : : {
4136 : 11925 : AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
4137 : 11925 : Index crelid = appinfo->child_relid;
4138 : :
4139 [ + - - + ]: 11925 : Assert(crelid > 0 && crelid <= ntables);
4140 [ - + ]: 11925 : Assert(dpns->appendrels[crelid] == NULL);
4141 : 11925 : dpns->appendrels[crelid] = appinfo;
4142 : : }
4143 : : }
4144 : : else
4145 : 13755 : dpns->appendrels = NULL; /* don't need it */
4146 : :
4147 : : /*
4148 : : * Set up column name aliases, ignoring any join RTEs; they don't matter
4149 : : * because plan trees don't contain any join alias Vars.
4150 : : */
4128 4151 : 16390 : set_simple_column_names(dpns);
4152 : :
4153 : : /* Return a one-deep namespace stack */
4154 : 16390 : return list_make1(dpns);
4155 : : }
4156 : :
4157 : : /*
4158 : : * set_deparse_context_plan - Specify Plan node containing expression
4159 : : *
4160 : : * When deparsing an expression in a Plan tree, we might have to resolve
4161 : : * OUTER_VAR, INNER_VAR, or INDEX_VAR references. To do this, the caller must
4162 : : * provide the parent Plan node. Then OUTER_VAR and INNER_VAR references
4163 : : * can be resolved by drilling down into the left and right child plans.
4164 : : * Similarly, INDEX_VAR references can be resolved by reference to the
4165 : : * indextlist given in a parent IndexOnlyScan node, or to the scan tlist in
4166 : : * ForeignScan and CustomScan nodes. (Note that we don't currently support
4167 : : * deparsing of indexquals in regular IndexScan or BitmapIndexScan nodes;
4168 : : * for those, we can only deparse the indexqualorig fields, which won't
4169 : : * contain INDEX_VAR Vars.)
4170 : : *
4171 : : * The ancestors list is a list of the Plan's parent Plan and SubPlan nodes,
4172 : : * the most-closely-nested first. This is needed to resolve PARAM_EXEC
4173 : : * Params. Note we assume that all the Plan nodes share the same rtable.
4174 : : *
4175 : : * For a ModifyTable plan, we might also need to resolve references to OLD/NEW
4176 : : * variables in the RETURNING list, so we copy the alias names of the OLD and
4177 : : * NEW rows from the ModifyTable plan node.
4178 : : *
4179 : : * Once this function has been called, deparse_expression() can be called on
4180 : : * subsidiary expression(s) of the specified Plan node. To deparse
4181 : : * expressions of a different Plan node in the same Plan tree, re-call this
4182 : : * function to identify the new parent Plan node.
4183 : : *
4184 : : * The result is the same List passed in; this is a notational convenience.
4185 : : */
4186 : : List *
2337 4187 : 39120 : set_deparse_context_plan(List *dpcontext, Plan *plan, List *ancestors)
4188 : : {
4189 : : deparse_namespace *dpns;
4190 : :
4191 : : /* Should always have one-entry namespace list for Plan deparsing */
4128 4192 [ - + ]: 39120 : Assert(list_length(dpcontext) == 1);
4193 : 39120 : dpns = (deparse_namespace *) linitial(dpcontext);
4194 : :
4195 : : /* Set our attention on the specific plan node passed in */
5775 4196 : 39120 : dpns->ancestors = ancestors;
1667 4197 : 39120 : set_deparse_plan(dpns, plan);
4198 : :
4199 : : /* For ModifyTable, set aliases for OLD and NEW in RETURNING */
474 dean.a.rasheed@gmail 4200 [ + + ]: 39120 : if (IsA(plan, ModifyTable))
4201 : : {
4202 : 139 : dpns->ret_old_alias = ((ModifyTable *) plan)->returningOldAlias;
4203 : 139 : dpns->ret_new_alias = ((ModifyTable *) plan)->returningNewAlias;
4204 : : }
4205 : :
4128 tgl@sss.pgh.pa.us 4206 : 39120 : return dpcontext;
4207 : : }
4208 : :
4209 : : /*
4210 : : * select_rtable_names_for_explain - Select RTE aliases for EXPLAIN
4211 : : *
4212 : : * Determine the relation aliases we'll use during an EXPLAIN operation.
4213 : : * This is just a frontend to set_rtable_names. We have to expose the aliases
4214 : : * to EXPLAIN because EXPLAIN needs to know the right alias names to print.
4215 : : */
4216 : : List *
4974 4217 : 16390 : select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
4218 : : {
4219 : : deparse_namespace dpns;
4220 : :
4221 : 16390 : memset(&dpns, 0, sizeof(dpns));
4222 : 16390 : dpns.rtable = rtable;
2337 4223 : 16390 : dpns.subplans = NIL;
4974 4224 : 16390 : dpns.ctes = NIL;
2337 4225 : 16390 : dpns.appendrels = NULL;
4974 4226 : 16390 : set_rtable_names(&dpns, NIL, rels_used);
4227 : : /* We needn't bother computing column aliases yet */
4228 : :
4229 : 16390 : return dpns.rtable_names;
4230 : : }
4231 : :
4232 : : /*
4233 : : * set_rtable_names: select RTE aliases to be used in printing a query
4234 : : *
4235 : : * We fill in dpns->rtable_names with a list of names that is one-for-one with
4236 : : * the already-filled dpns->rtable list. Each RTE name is unique among those
4237 : : * in the new namespace plus any ancestor namespaces listed in
4238 : : * parent_namespaces.
4239 : : *
4240 : : * If rels_used isn't NULL, only RTE indexes listed in it are given aliases.
4241 : : *
4242 : : * Note that this function is only concerned with relation names, not column
4243 : : * names.
4244 : : */
4245 : : static void
4246 : 35512 : set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
4247 : : Bitmapset *rels_used)
4248 : : {
4249 : : HASHCTL hash_ctl;
4250 : : HTAB *names_hash;
4251 : : NameHashEntry *hentry;
4252 : : bool found;
4253 : : int rtindex;
4254 : : ListCell *lc;
4255 : :
4256 : 35512 : dpns->rtable_names = NIL;
4257 : : /* nothing more to do if empty rtable */
3823 4258 [ + + ]: 35512 : if (dpns->rtable == NIL)
4259 : 339 : return;
4260 : :
4261 : : /*
4262 : : * We use a hash table to hold known names, so that this process is O(N)
4263 : : * not O(N^2) for N names.
4264 : : */
4265 : 35173 : hash_ctl.keysize = NAMEDATALEN;
4266 : 35173 : hash_ctl.entrysize = sizeof(NameHashEntry);
4267 : 35173 : hash_ctl.hcxt = CurrentMemoryContext;
4268 : 35173 : names_hash = hash_create("set_rtable_names names",
4269 : 35173 : list_length(dpns->rtable),
4270 : : &hash_ctl,
4271 : : HASH_ELEM | HASH_STRINGS | HASH_CONTEXT);
4272 : :
4273 : : /* Preload the hash table with names appearing in parent_namespaces */
4274 [ + + + + : 36306 : foreach(lc, parent_namespaces)
+ + ]
4275 : : {
4276 : 1133 : deparse_namespace *olddpns = (deparse_namespace *) lfirst(lc);
4277 : : ListCell *lc2;
4278 : :
4279 [ + + + + : 3905 : foreach(lc2, olddpns->rtable_names)
+ + ]
4280 : : {
4281 : 2772 : char *oldname = (char *) lfirst(lc2);
4282 : :
4283 [ + + ]: 2772 : if (oldname == NULL)
4284 : 201 : continue;
4285 : 2571 : hentry = (NameHashEntry *) hash_search(names_hash,
4286 : : oldname,
4287 : : HASH_ENTER,
4288 : : &found);
4289 : : /* we do not complain about duplicate names in parent namespaces */
4290 : 2571 : hentry->counter = 0;
4291 : : }
4292 : : }
4293 : :
4294 : : /* Now we can scan the rtable */
4295 : 35173 : rtindex = 1;
4974 4296 [ + - + + : 102721 : foreach(lc, dpns->rtable)
+ + ]
4297 : : {
4298 : 67548 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
4299 : : char *refname;
4300 : :
4301 : : /* Just in case this takes an unreasonable amount of time ... */
3823 4302 [ + + ]: 67548 : CHECK_FOR_INTERRUPTS();
4303 : :
4974 4304 [ + + + + ]: 67548 : if (rels_used && !bms_is_member(rtindex, rels_used))
4305 : : {
4306 : : /* Ignore unreferenced RTE */
4307 : 12257 : refname = NULL;
4308 : : }
4309 [ + + ]: 55291 : else if (rte->alias)
4310 : : {
4311 : : /* If RTE has a user-defined alias, prefer that */
4312 : 35862 : refname = rte->alias->aliasname;
4313 : : }
4314 [ + + ]: 19429 : else if (rte->rtekind == RTE_RELATION)
4315 : : {
4316 : : /* Use the current actual name of the relation */
4317 : 14829 : refname = get_rel_name(rte->relid);
4318 : : }
4319 [ + + ]: 4600 : else if (rte->rtekind == RTE_JOIN)
4320 : : {
4321 : : /* Unnamed join has no refname */
4322 : 1129 : refname = NULL;
4323 : : }
4324 : : else
4325 : : {
4326 : : /* Otherwise use whatever the parser assigned */
4327 : 3471 : refname = rte->eref->aliasname;
4328 : : }
4329 : :
4330 : : /*
4331 : : * If the selected name isn't unique, append digits to make it so, and
4332 : : * make a new hash entry for it once we've got a unique name. For a
4333 : : * very long input name, we might have to truncate to stay within
4334 : : * NAMEDATALEN.
4335 : : */
3823 4336 [ + + ]: 67548 : if (refname)
4337 : : {
4338 : 54162 : hentry = (NameHashEntry *) hash_search(names_hash,
4339 : : refname,
4340 : : HASH_ENTER,
4341 : : &found);
4342 [ + + ]: 54162 : if (found)
4343 : : {
4344 : : /* Name already in use, must choose a new one */
4345 : 10205 : int refnamelen = strlen(refname);
4346 : 10205 : char *modname = (char *) palloc(refnamelen + 16);
4347 : : NameHashEntry *hentry2;
4348 : :
4349 : : do
4350 : : {
4351 : 10209 : hentry->counter++;
4352 : : for (;;)
4353 : : {
4354 : 10217 : memcpy(modname, refname, refnamelen);
4355 : 10217 : sprintf(modname + refnamelen, "_%d", hentry->counter);
4356 [ + + ]: 10217 : if (strlen(modname) < NAMEDATALEN)
4357 : 10209 : break;
4358 : : /* drop chars from refname to keep all the digits */
4359 : 8 : refnamelen = pg_mbcliplen(refname, refnamelen,
4360 : : refnamelen - 1);
4361 : : }
4362 : 10209 : hentry2 = (NameHashEntry *) hash_search(names_hash,
4363 : : modname,
4364 : : HASH_ENTER,
4365 : : &found);
4366 [ + + ]: 10209 : } while (found);
4367 : 10205 : hentry2->counter = 0; /* init new hash entry */
4368 : 10205 : refname = modname;
4369 : : }
4370 : : else
4371 : : {
4372 : : /* Name not previously used, need only initialize hentry */
4373 : 43957 : hentry->counter = 0;
4374 : : }
4375 : : }
4376 : :
4974 4377 : 67548 : dpns->rtable_names = lappend(dpns->rtable_names, refname);
4378 : 67548 : rtindex++;
4379 : : }
4380 : :
3823 4381 : 35173 : hash_destroy(names_hash);
4382 : : }
4383 : :
4384 : : /*
4385 : : * set_deparse_for_query: set up deparse_namespace for deparsing a Query tree
4386 : : *
4387 : : * For convenience, this is defined to initialize the deparse_namespace struct
4388 : : * from scratch.
4389 : : */
4390 : : static void
4873 4391 : 3584 : set_deparse_for_query(deparse_namespace *dpns, Query *query,
4392 : : List *parent_namespaces)
4393 : : {
4394 : : ListCell *lc;
4395 : : ListCell *lc2;
4396 : :
4397 : : /* Initialize *dpns and fill rtable/ctes links */
4398 : 3584 : memset(dpns, 0, sizeof(deparse_namespace));
4399 : 3584 : dpns->rtable = query->rtable;
2337 4400 : 3584 : dpns->subplans = NIL;
4873 4401 : 3584 : dpns->ctes = query->cteList;
2337 4402 : 3584 : dpns->appendrels = NULL;
474 dean.a.rasheed@gmail 4403 : 3584 : dpns->ret_old_alias = query->returningOldAlias;
4404 : 3584 : dpns->ret_new_alias = query->returningNewAlias;
4405 : :
4406 : : /* Assign a unique relation alias to each RTE */
4873 tgl@sss.pgh.pa.us 4407 : 3584 : set_rtable_names(dpns, parent_namespaces, NULL);
4408 : :
4409 : : /* Initialize dpns->rtable_columns to contain zeroed structs */
4410 : 3584 : dpns->rtable_columns = NIL;
4411 [ + + ]: 9990 : while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
4412 : 6406 : dpns->rtable_columns = lappend(dpns->rtable_columns,
4413 : : palloc0(sizeof(deparse_columns)));
4414 : :
4415 : : /* If it's a utility query, it won't have a jointree */
4737 4416 [ + + ]: 3584 : if (query->jointree)
4417 : : {
4418 : : /* Detect whether global uniqueness of USING names is needed */
4419 : 3575 : dpns->unique_using =
4669 4420 : 3575 : has_dangerous_join_using(dpns, (Node *) query->jointree);
4421 : :
4422 : : /*
4423 : : * Select names for columns merged by USING, via a recursive pass over
4424 : : * the query jointree.
4425 : : */
4387 4426 : 3575 : set_using_names(dpns, (Node *) query->jointree, NIL);
4427 : : }
4428 : :
4429 : : /*
4430 : : * Now assign remaining column aliases for each RTE. We do this in a
4431 : : * linear scan of the rtable, so as to process RTEs whether or not they
4432 : : * are in the jointree (we mustn't miss NEW.*, INSERT target relations,
4433 : : * etc). JOIN RTEs must be processed after their children, but this is
4434 : : * okay because they appear later in the rtable list than their children
4435 : : * (cf Asserts in identify_join_columns()).
4436 : : */
4873 4437 [ + + + + : 9990 : forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
+ + + + +
+ + - +
+ ]
4438 : : {
4439 : 6406 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
4440 : 6406 : deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
4441 : :
4442 [ + + ]: 6406 : if (rte->rtekind == RTE_JOIN)
4443 : 940 : set_join_column_names(dpns, rte, colinfo);
4444 : : else
4445 : 5466 : set_relation_column_names(dpns, rte, colinfo);
4446 : : }
4447 : 3584 : }
4448 : :
4449 : : /*
4450 : : * set_simple_column_names: fill in column aliases for non-query situations
4451 : : *
4452 : : * This handles EXPLAIN and cases where we only have relation RTEs. Without
4453 : : * a join tree, we can't do anything smart about join RTEs, but we don't
4454 : : * need to, because EXPLAIN should never see join alias Vars anyway.
4455 : : * If we find a join RTE we'll just skip it, leaving its deparse_columns
4456 : : * struct all-zero. If somehow we try to deparse a join alias Var, we'll
4457 : : * error out cleanly because the struct's num_cols will be zero.
4458 : : */
4459 : : static void
4460 : 31928 : set_simple_column_names(deparse_namespace *dpns)
4461 : : {
4462 : : ListCell *lc;
4463 : : ListCell *lc2;
4464 : :
4465 : : /* Initialize dpns->rtable_columns to contain zeroed structs */
4466 : 31928 : dpns->rtable_columns = NIL;
4467 [ + + ]: 93070 : while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
4468 : 61142 : dpns->rtable_columns = lappend(dpns->rtable_columns,
4469 : : palloc0(sizeof(deparse_columns)));
4470 : :
4471 : : /* Assign unique column aliases within each non-join RTE */
4472 [ + - + + : 93070 : forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
+ - + + +
+ + - +
+ ]
4473 : : {
4474 : 61142 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
4475 : 61142 : deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
4476 : :
504 4477 [ + + ]: 61142 : if (rte->rtekind != RTE_JOIN)
4478 : 57152 : set_relation_column_names(dpns, rte, colinfo);
4479 : : }
4873 4480 : 31928 : }
4481 : :
4482 : : /*
4483 : : * has_dangerous_join_using: search jointree for unnamed JOIN USING
4484 : : *
4485 : : * Merged columns of a JOIN USING may act differently from either of the input
4486 : : * columns, either because they are merged with COALESCE (in a FULL JOIN) or
4487 : : * because an implicit coercion of the underlying input column is required.
4488 : : * In such a case the column must be referenced as a column of the JOIN not as
4489 : : * a column of either input. And this is problematic if the join is unnamed
4490 : : * (alias-less): we cannot qualify the column's name with an RTE name, since
4491 : : * there is none. (Forcibly assigning an alias to the join is not a solution,
4492 : : * since that will prevent legal references to tables below the join.)
4493 : : * To ensure that every column in the query is unambiguously referenceable,
4494 : : * we must assign such merged columns names that are globally unique across
4495 : : * the whole query, aliasing other columns out of the way as necessary.
4496 : : *
4497 : : * Because the ensuing re-aliasing is fairly damaging to the readability of
4498 : : * the query, we don't do this unless we have to. So, we must pre-scan
4499 : : * the join tree to see if we have to, before starting set_using_names().
4500 : : */
4501 : : static bool
4669 4502 : 8512 : has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode)
4503 : : {
4873 4504 [ + + ]: 8512 : if (IsA(jtnode, RangeTblRef))
4505 : : {
4506 : : /* nothing to do here */
4507 : : }
4508 [ + + ]: 4471 : else if (IsA(jtnode, FromExpr))
4509 : : {
4510 : 3575 : FromExpr *f = (FromExpr *) jtnode;
4511 : : ListCell *lc;
4512 : :
4513 [ + + + + : 6772 : foreach(lc, f->fromlist)
+ + ]
4514 : : {
4669 4515 [ + + ]: 3249 : if (has_dangerous_join_using(dpns, (Node *) lfirst(lc)))
4873 4516 : 52 : return true;
4517 : : }
4518 : : }
4519 [ + - ]: 896 : else if (IsA(jtnode, JoinExpr))
4520 : : {
4521 : 896 : JoinExpr *j = (JoinExpr *) jtnode;
4522 : :
4523 : : /* Is it an unnamed JOIN with USING? */
4669 4524 [ + + + + ]: 896 : if (j->alias == NULL && j->usingClause)
4525 : : {
4526 : : /*
4527 : : * Yes, so check each join alias var to see if any of them are not
4528 : : * simple references to underlying columns. If so, we have a
4529 : : * dangerous situation and must pick unique aliases.
4530 : : */
4531 : 188 : RangeTblEntry *jrte = rt_fetch(j->rtindex, dpns->rtable);
4532 : :
4533 : : /* We need only examine the merged columns */
2308 4534 [ + + ]: 388 : for (int i = 0; i < jrte->joinmergedcols; i++)
4535 : : {
4536 : 252 : Node *aliasvar = list_nth(jrte->joinaliasvars, i);
4537 : :
4538 [ + + ]: 252 : if (!IsA(aliasvar, Var))
4669 4539 : 52 : return true;
4540 : : }
4541 : : }
4542 : :
4543 : : /* Nope, but inspect children */
4544 [ - + ]: 844 : if (has_dangerous_join_using(dpns, j->larg))
4873 tgl@sss.pgh.pa.us 4545 :UBC 0 : return true;
4669 tgl@sss.pgh.pa.us 4546 [ - + ]:CBC 844 : if (has_dangerous_join_using(dpns, j->rarg))
4873 tgl@sss.pgh.pa.us 4547 :UBC 0 : return true;
4548 : : }
4549 : : else
4550 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
4551 : : (int) nodeTag(jtnode));
4873 tgl@sss.pgh.pa.us 4552 :CBC 8408 : return false;
4553 : : }
4554 : :
4555 : : /*
4556 : : * set_using_names: select column aliases to be used for merged USING columns
4557 : : *
4558 : : * We do this during a recursive descent of the query jointree.
4559 : : * dpns->unique_using must already be set to determine the global strategy.
4560 : : *
4561 : : * Column alias info is saved in the dpns->rtable_columns list, which is
4562 : : * assumed to be filled with pre-zeroed deparse_columns structs.
4563 : : *
4564 : : * parentUsing is a list of all USING aliases assigned in parent joins of
4565 : : * the current jointree node. (The passed-in list must not be modified.)
4566 : : *
4567 : : * Note that we do not use per-deparse_columns hash tables in this function.
4568 : : * The number of names that need to be assigned should be small enough that
4569 : : * we don't need to trouble with that.
4570 : : */
4571 : : static void
4387 4572 : 8732 : set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing)
4573 : : {
4873 4574 [ + + ]: 8732 : if (IsA(jtnode, RangeTblRef))
4575 : : {
4576 : : /* nothing to do now */
4577 : : }
4578 [ + + ]: 4515 : else if (IsA(jtnode, FromExpr))
4579 : : {
4580 : 3575 : FromExpr *f = (FromExpr *) jtnode;
4581 : : ListCell *lc;
4582 : :
4583 [ + + + + : 6852 : foreach(lc, f->fromlist)
+ + ]
4387 4584 : 3277 : set_using_names(dpns, (Node *) lfirst(lc), parentUsing);
4585 : : }
4873 4586 [ + - ]: 940 : else if (IsA(jtnode, JoinExpr))
4587 : : {
4588 : 940 : JoinExpr *j = (JoinExpr *) jtnode;
4589 : 940 : RangeTblEntry *rte = rt_fetch(j->rtindex, dpns->rtable);
4590 : 940 : deparse_columns *colinfo = deparse_columns_fetch(j->rtindex, dpns);
4591 : : int *leftattnos;
4592 : : int *rightattnos;
4593 : : deparse_columns *leftcolinfo;
4594 : : deparse_columns *rightcolinfo;
4595 : : int i;
4596 : : ListCell *lc;
4597 : :
4598 : : /* Get info about the shape of the join */
4599 : 940 : identify_join_columns(j, rte, colinfo);
4600 : 940 : leftattnos = colinfo->leftattnos;
4601 : 940 : rightattnos = colinfo->rightattnos;
4602 : :
4603 : : /* Look up the not-yet-filled-in child deparse_columns structs */
4604 : 940 : leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
4605 : 940 : rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
4606 : :
4607 : : /*
4608 : : * If this join is unnamed, then we cannot substitute new aliases at
4609 : : * this level, so any name requirements pushed down to here must be
4610 : : * pushed down again to the children.
4611 : : */
4612 [ + + ]: 940 : if (rte->alias == NULL)
4613 : : {
4614 [ + + ]: 960 : for (i = 0; i < colinfo->num_cols; i++)
4615 : : {
4616 : 92 : char *colname = colinfo->colnames[i];
4617 : :
4618 [ + + ]: 92 : if (colname == NULL)
4619 : 16 : continue;
4620 : :
4621 : : /* Push down to left column, unless it's a system column */
4622 [ + + ]: 76 : if (leftattnos[i] > 0)
4623 : : {
4624 : 68 : expand_colnames_array_to(leftcolinfo, leftattnos[i]);
4625 : 68 : leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4626 : : }
4627 : :
4628 : : /* Same on the righthand side */
4629 [ + - ]: 76 : if (rightattnos[i] > 0)
4630 : : {
4631 : 76 : expand_colnames_array_to(rightcolinfo, rightattnos[i]);
4632 : 76 : rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4633 : : }
4634 : : }
4635 : : }
4636 : :
4637 : : /*
4638 : : * If there's a USING clause, select the USING column names and push
4639 : : * those names down to the children. We have two strategies:
4640 : : *
4641 : : * If dpns->unique_using is true, we force all USING names to be
4642 : : * unique across the whole query level. In principle we'd only need
4643 : : * the names of dangerous USING columns to be globally unique, but to
4644 : : * safely assign all USING names in a single pass, we have to enforce
4645 : : * the same uniqueness rule for all of them. However, if a USING
4646 : : * column's name has been pushed down from the parent, we should use
4647 : : * it as-is rather than making a uniqueness adjustment. This is
4648 : : * necessary when we're at an unnamed join, and it creates no risk of
4649 : : * ambiguity. Also, if there's a user-written output alias for a
4650 : : * merged column, we prefer to use that rather than the input name;
4651 : : * this simplifies the logic and seems likely to lead to less aliasing
4652 : : * overall.
4653 : : *
4654 : : * If dpns->unique_using is false, we only need USING names to be
4655 : : * unique within their own join RTE. We still need to honor
4656 : : * pushed-down names, though.
4657 : : *
4658 : : * Though significantly different in results, these two strategies are
4659 : : * implemented by the same code, with only the difference of whether
4660 : : * to put assigned names into dpns->using_names.
4661 : : */
4662 [ + + ]: 940 : if (j->usingClause)
4663 : : {
4664 : : /* Copy the input parentUsing list so we don't modify it */
4387 4665 : 280 : parentUsing = list_copy(parentUsing);
4666 : :
4667 : : /* USING names must correspond to the first join output columns */
4873 4668 : 280 : expand_colnames_array_to(colinfo, list_length(j->usingClause));
4669 : 280 : i = 0;
4670 [ + - + + : 664 : foreach(lc, j->usingClause)
+ + ]
4671 : : {
4672 : 384 : char *colname = strVal(lfirst(lc));
4673 : :
4674 : : /* Assert it's a merged column */
4675 [ + - - + ]: 384 : Assert(leftattnos[i] != 0 && rightattnos[i] != 0);
4676 : :
4677 : : /* Adopt passed-down name if any, else select unique name */
4678 [ + + ]: 384 : if (colinfo->colnames[i] != NULL)
4679 : 68 : colname = colinfo->colnames[i];
4680 : : else
4681 : : {
4682 : : /* Prefer user-written output alias if any */
4683 [ + + - + ]: 316 : if (rte->alias && i < list_length(rte->alias->colnames))
4873 tgl@sss.pgh.pa.us 4684 :UBC 0 : colname = strVal(list_nth(rte->alias->colnames, i));
4685 : : /* Make it appropriately unique */
4873 tgl@sss.pgh.pa.us 4686 :CBC 316 : colname = make_colname_unique(colname, dpns, colinfo);
4687 [ + + ]: 316 : if (dpns->unique_using)
4688 : 88 : dpns->using_names = lappend(dpns->using_names,
4689 : : colname);
4690 : : /* Save it as output column name, too */
4691 : 316 : colinfo->colnames[i] = colname;
4692 : : }
4693 : :
4694 : : /* Remember selected names for use later */
4695 : 384 : colinfo->usingNames = lappend(colinfo->usingNames, colname);
4387 4696 : 384 : parentUsing = lappend(parentUsing, colname);
4697 : :
4698 : : /* Push down to left column, unless it's a system column */
4873 4699 [ + - ]: 384 : if (leftattnos[i] > 0)
4700 : : {
4701 : 384 : expand_colnames_array_to(leftcolinfo, leftattnos[i]);
4702 : 384 : leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4703 : : }
4704 : :
4705 : : /* Same on the righthand side */
4706 [ + - ]: 384 : if (rightattnos[i] > 0)
4707 : : {
4708 : 384 : expand_colnames_array_to(rightcolinfo, rightattnos[i]);
4709 : 384 : rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4710 : : }
4711 : :
4712 : 384 : i++;
4713 : : }
4714 : : }
4715 : :
4716 : : /* Mark child deparse_columns structs with correct parentUsing info */
4387 4717 : 940 : leftcolinfo->parentUsing = parentUsing;
4718 : 940 : rightcolinfo->parentUsing = parentUsing;
4719 : :
4720 : : /* Now recursively assign USING column names in children */
4721 : 940 : set_using_names(dpns, j->larg, parentUsing);
4722 : 940 : set_using_names(dpns, j->rarg, parentUsing);
4723 : : }
4724 : : else
4873 tgl@sss.pgh.pa.us 4725 [ # # ]:UBC 0 : elog(ERROR, "unrecognized node type: %d",
4726 : : (int) nodeTag(jtnode));
4873 tgl@sss.pgh.pa.us 4727 :CBC 8732 : }
4728 : :
4729 : : /*
4730 : : * set_relation_column_names: select column aliases for a non-join RTE
4731 : : *
4732 : : * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
4733 : : * If any colnames entries are already filled in, those override local
4734 : : * choices.
4735 : : */
4736 : : static void
4737 : 62618 : set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
4738 : : deparse_columns *colinfo)
4739 : : {
4740 : : int ncolumns;
4741 : : char **real_colnames;
4742 : : bool changed_any;
4743 : : int noldcolumns;
4744 : : int i;
4745 : : int j;
4746 : :
4747 : : /*
4748 : : * Construct an array of the current "real" column names of the RTE.
4749 : : * real_colnames[] will be indexed by physical column number, with NULL
4750 : : * entries for dropped columns.
4751 : : */
4752 [ + + ]: 62618 : if (rte->rtekind == RTE_RELATION)
4753 : : {
4754 : : /* Relation --- look to the system catalogs for up-to-date info */
4755 : : Relation rel;
4756 : : TupleDesc tupdesc;
4757 : :
4758 : 52793 : rel = relation_open(rte->relid, AccessShareLock);
4759 : 52793 : tupdesc = RelationGetDescr(rel);
4760 : :
4761 : 52793 : ncolumns = tupdesc->natts;
4762 : 52793 : real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4763 : :
4764 [ + + ]: 332677 : for (i = 0; i < ncolumns; i++)
4765 : : {
3180 andres@anarazel.de 4766 : 279884 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4767 : :
4768 [ + + ]: 279884 : if (attr->attisdropped)
4873 tgl@sss.pgh.pa.us 4769 : 1904 : real_colnames[i] = NULL;
4770 : : else
3180 andres@anarazel.de 4771 : 277980 : real_colnames[i] = pstrdup(NameStr(attr->attname));
4772 : : }
4873 tgl@sss.pgh.pa.us 4773 : 52793 : relation_close(rel, AccessShareLock);
4774 : : }
4775 : : else
4776 : : {
4777 : : /* Otherwise get the column names from eref or expandRTE() */
4778 : : List *colnames;
4779 : : ListCell *lc;
4780 : :
4781 : : /*
4782 : : * Functions returning composites have the annoying property that some
4783 : : * of the composite type's columns might have been dropped since the
4784 : : * query was parsed. If possible, use expandRTE() to handle that
4785 : : * case, since it has the tedious logic needed to find out about
4786 : : * dropped columns. However, if we're explaining a plan, then we
4787 : : * don't have rte->functions because the planner thinks that won't be
4788 : : * needed later, and that breaks expandRTE(). So in that case we have
4789 : : * to rely on rte->eref, which may lead us to report a dropped
4790 : : * column's old name; that seems close enough for EXPLAIN's purposes.
4791 : : *
4792 : : * For non-RELATION, non-FUNCTION RTEs, we can just look at rte->eref,
4793 : : * which should be sufficiently up-to-date: no other RTE types can
4794 : : * have columns get dropped from under them after parsing.
4795 : : */
1384 4796 [ + + + + ]: 9825 : if (rte->rtekind == RTE_FUNCTION && rte->functions != NIL)
4797 : : {
4798 : : /* Since we're not creating Vars, rtindex etc. don't matter */
474 dean.a.rasheed@gmail 4799 : 553 : expandRTE(rte, 1, 0, VAR_RETURNING_DEFAULT, -1,
4800 : : true /* include dropped */ , &colnames, NULL);
4801 : : }
4802 : : else
1384 tgl@sss.pgh.pa.us 4803 : 9272 : colnames = rte->eref->colnames;
4804 : :
4805 : 9825 : ncolumns = list_length(colnames);
4873 4806 : 9825 : real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4807 : :
4808 : 9825 : i = 0;
1384 4809 [ + + + + : 31596 : foreach(lc, colnames)
+ + ]
4810 : : {
4811 : : /*
4812 : : * If the column name we find here is an empty string, then it's a
4813 : : * dropped column, so change to NULL.
4814 : : */
4308 4815 : 21771 : char *cname = strVal(lfirst(lc));
4816 : :
4817 [ + + ]: 21771 : if (cname[0] == '\0')
4818 : 36 : cname = NULL;
4819 : 21771 : real_colnames[i] = cname;
4873 4820 : 21771 : i++;
4821 : : }
4822 : : }
4823 : :
4824 : : /*
4825 : : * Ensure colinfo->colnames has a slot for each column. (It could be long
4826 : : * enough already, if we pushed down a name for the last column.) Note:
4827 : : * it's possible that there are now more columns than there were when the
4828 : : * query was parsed, ie colnames could be longer than rte->eref->colnames.
4829 : : * We must assign unique aliases to the new columns too, else there could
4830 : : * be unresolved conflicts when the view/rule is reloaded.
4831 : : */
4832 : 62618 : expand_colnames_array_to(colinfo, ncolumns);
4833 [ - + ]: 62618 : Assert(colinfo->num_cols == ncolumns);
4834 : :
4835 : : /*
4836 : : * Make sufficiently large new_colnames and is_new_col arrays, too.
4837 : : *
4838 : : * Note: because we leave colinfo->num_new_cols zero until after the loop,
4839 : : * colname_is_unique will not consult that array, which is fine because it
4840 : : * would only be duplicate effort.
4841 : : */
4842 : 62618 : colinfo->new_colnames = (char **) palloc(ncolumns * sizeof(char *));
4843 : 62618 : colinfo->is_new_col = (bool *) palloc(ncolumns * sizeof(bool));
4844 : :
4845 : : /* If the RTE is wide enough, use a hash table to avoid O(N^2) costs */
602 4846 : 62618 : build_colinfo_names_hash(colinfo);
4847 : :
4848 : : /*
4849 : : * Scan the columns, select a unique alias for each one, and store it in
4850 : : * colinfo->colnames and colinfo->new_colnames. The former array has NULL
4851 : : * entries for dropped columns, the latter omits them. Also mark
4852 : : * new_colnames entries as to whether they are new since parse time; this
4853 : : * is the case for entries beyond the length of rte->eref->colnames.
4854 : : */
4873 4855 : 62618 : noldcolumns = list_length(rte->eref->colnames);
4856 : 62618 : changed_any = false;
4857 : 62618 : j = 0;
4858 [ + + ]: 364273 : for (i = 0; i < ncolumns; i++)
4859 : : {
4860 : 301655 : char *real_colname = real_colnames[i];
4861 : 301655 : char *colname = colinfo->colnames[i];
4862 : :
4863 : : /* Skip dropped columns */
4864 [ + + ]: 301655 : if (real_colname == NULL)
4865 : : {
4866 [ - + ]: 1940 : Assert(colname == NULL); /* colnames[i] is already NULL */
4867 : 1940 : continue;
4868 : : }
4869 : :
4870 : : /* If alias already assigned, that's what to use */
4871 [ + + ]: 299715 : if (colname == NULL)
4872 : : {
4873 : : /* If user wrote an alias, prefer that over real column name */
4874 [ + + + + ]: 299015 : if (rte->alias && i < list_length(rte->alias->colnames))
4875 : 29980 : colname = strVal(list_nth(rte->alias->colnames, i));
4876 : : else
4877 : 269035 : colname = real_colname;
4878 : :
4879 : : /* Unique-ify and insert into colinfo */
4880 : 299015 : colname = make_colname_unique(colname, dpns, colinfo);
4881 : :
4882 : 299015 : colinfo->colnames[i] = colname;
602 4883 : 299015 : add_to_names_hash(colinfo, colname);
4884 : : }
4885 : :
4886 : : /* Put names of non-dropped columns in new_colnames[] too */
4873 4887 : 299715 : colinfo->new_colnames[j] = colname;
4888 : : /* And mark them as new or not */
4889 : 299715 : colinfo->is_new_col[j] = (i >= noldcolumns);
4890 : 299715 : j++;
4891 : :
4892 : : /* Remember if any assigned aliases differ from "real" name */
4893 [ + + + + ]: 299715 : if (!changed_any && strcmp(colname, real_colname) != 0)
4894 : 793 : changed_any = true;
4895 : : }
4896 : :
4897 : : /* We're now done needing the colinfo's names_hash */
602 4898 : 62618 : destroy_colinfo_names_hash(colinfo);
4899 : :
4900 : : /*
4901 : : * Set correct length for new_colnames[] array. (Note: if columns have
4902 : : * been added, colinfo->num_cols includes them, which is not really quite
4903 : : * right but is harmless, since any new columns must be at the end where
4904 : : * they won't affect varattnos of pre-existing columns.)
4905 : : */
4873 4906 : 62618 : colinfo->num_new_cols = j;
4907 : :
4908 : : /*
4909 : : * For a relation RTE, we need only print the alias column names if any
4910 : : * are different from the underlying "real" names. For a function RTE,
4911 : : * always emit a complete column alias list; this is to protect against
4912 : : * possible instability of the default column names (eg, from altering
4913 : : * parameter names). For tablefunc RTEs, we never print aliases, because
4914 : : * the column names are part of the clause itself. For other RTE types,
4915 : : * print if we changed anything OR if there were user-written column
4916 : : * aliases (since the latter would be part of the underlying "reality").
4917 : : */
4918 [ + + ]: 62618 : if (rte->rtekind == RTE_RELATION)
4919 : 52793 : colinfo->printaliases = changed_any;
4920 [ + + ]: 9825 : else if (rte->rtekind == RTE_FUNCTION)
4921 : 960 : colinfo->printaliases = true;
3345 alvherre@alvh.no-ip. 4922 [ + + ]: 8865 : else if (rte->rtekind == RTE_TABLEFUNC)
4923 : 114 : colinfo->printaliases = false;
4873 tgl@sss.pgh.pa.us 4924 [ + + + + ]: 8751 : else if (rte->alias && rte->alias->colnames != NIL)
4925 : 530 : colinfo->printaliases = true;
4926 : : else
4927 : 8221 : colinfo->printaliases = changed_any;
4928 : 62618 : }
4929 : :
4930 : : /*
4931 : : * set_join_column_names: select column aliases for a join RTE
4932 : : *
4933 : : * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
4934 : : * If any colnames entries are already filled in, those override local
4935 : : * choices. Also, names for USING columns were already chosen by
4936 : : * set_using_names(). We further expect that column alias selection has been
4937 : : * completed for both input RTEs.
4938 : : */
4939 : : static void
4940 : 940 : set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
4941 : : deparse_columns *colinfo)
4942 : : {
4943 : : deparse_columns *leftcolinfo;
4944 : : deparse_columns *rightcolinfo;
4945 : : bool changed_any;
4946 : : int noldcolumns;
4947 : : int nnewcolumns;
4948 : 940 : Bitmapset *leftmerged = NULL;
4949 : 940 : Bitmapset *rightmerged = NULL;
4950 : : int i;
4951 : : int j;
4952 : : int ic;
4953 : : int jc;
4954 : :
4955 : : /* Look up the previously-filled-in child deparse_columns structs */
4956 : 940 : leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
4957 : 940 : rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
4958 : :
4959 : : /*
4960 : : * Ensure colinfo->colnames has a slot for each column. (It could be long
4961 : : * enough already, if we pushed down a name for the last column.) Note:
4962 : : * it's possible that one or both inputs now have more columns than there
4963 : : * were when the query was parsed, but we'll deal with that below. We
4964 : : * only need entries in colnames for pre-existing columns.
4965 : : */
4966 : 940 : noldcolumns = list_length(rte->eref->colnames);
4967 : 940 : expand_colnames_array_to(colinfo, noldcolumns);
4968 [ - + ]: 940 : Assert(colinfo->num_cols == noldcolumns);
4969 : :
4970 : : /* If the RTE is wide enough, use a hash table to avoid O(N^2) costs */
602 4971 : 940 : build_colinfo_names_hash(colinfo);
4972 : :
4973 : : /*
4974 : : * Scan the join output columns, select an alias for each one, and store
4975 : : * it in colinfo->colnames. If there are USING columns, set_using_names()
4976 : : * already selected their names, so we can start the loop at the first
4977 : : * non-merged column.
4978 : : */
4873 4979 : 940 : changed_any = false;
4980 [ + + ]: 30493 : for (i = list_length(colinfo->usingNames); i < noldcolumns; i++)
4981 : : {
4982 : 29553 : char *colname = colinfo->colnames[i];
4983 : : char *real_colname;
4984 : :
4985 : : /* Join column must refer to at least one input column */
2308 4986 [ + + - + ]: 29553 : Assert(colinfo->leftattnos[i] != 0 || colinfo->rightattnos[i] != 0);
4987 : :
4988 : : /* Get the child column name */
4873 4989 [ + + ]: 29553 : if (colinfo->leftattnos[i] > 0)
4990 : 20721 : real_colname = leftcolinfo->colnames[colinfo->leftattnos[i] - 1];
4991 [ + - ]: 8832 : else if (colinfo->rightattnos[i] > 0)
4992 : 8832 : real_colname = rightcolinfo->colnames[colinfo->rightattnos[i] - 1];
4993 : : else
4994 : : {
4995 : : /* We're joining system columns --- use eref name */
4669 tgl@sss.pgh.pa.us 4996 :UBC 0 : real_colname = strVal(list_nth(rte->eref->colnames, i));
4997 : : }
4998 : :
4999 : : /* If child col has been dropped, no need to assign a join colname */
2308 tgl@sss.pgh.pa.us 5000 [ + + ]:CBC 29553 : if (real_colname == NULL)
5001 : : {
5002 : 4 : colinfo->colnames[i] = NULL;
5003 : 4 : continue;
5004 : : }
5005 : :
5006 : : /* In an unnamed join, just report child column names as-is */
4873 5007 [ + + ]: 29549 : if (rte->alias == NULL)
5008 : : {
5009 : 29297 : colinfo->colnames[i] = real_colname;
602 5010 : 29297 : add_to_names_hash(colinfo, real_colname);
4873 5011 : 29297 : continue;
5012 : : }
5013 : :
5014 : : /* If alias already assigned, that's what to use */
5015 [ + - ]: 252 : if (colname == NULL)
5016 : : {
5017 : : /* If user wrote an alias, prefer that over real column name */
5018 [ + - + + ]: 252 : if (rte->alias && i < list_length(rte->alias->colnames))
5019 : 64 : colname = strVal(list_nth(rte->alias->colnames, i));
5020 : : else
5021 : 188 : colname = real_colname;
5022 : :
5023 : : /* Unique-ify and insert into colinfo */
5024 : 252 : colname = make_colname_unique(colname, dpns, colinfo);
5025 : :
5026 : 252 : colinfo->colnames[i] = colname;
602 5027 : 252 : add_to_names_hash(colinfo, colname);
5028 : : }
5029 : :
5030 : : /* Remember if any assigned aliases differ from "real" name */
4873 5031 [ + + + + ]: 252 : if (!changed_any && strcmp(colname, real_colname) != 0)
5032 : 16 : changed_any = true;
5033 : : }
5034 : :
5035 : : /*
5036 : : * Calculate number of columns the join would have if it were re-parsed
5037 : : * now, and create storage for the new_colnames and is_new_col arrays.
5038 : : *
5039 : : * Note: colname_is_unique will be consulting new_colnames[] during the
5040 : : * loops below, so its not-yet-filled entries must be zeroes.
5041 : : */
5042 : 1880 : nnewcolumns = leftcolinfo->num_new_cols + rightcolinfo->num_new_cols -
5043 : 940 : list_length(colinfo->usingNames);
5044 : 940 : colinfo->num_new_cols = nnewcolumns;
5045 : 940 : colinfo->new_colnames = (char **) palloc0(nnewcolumns * sizeof(char *));
5046 : 940 : colinfo->is_new_col = (bool *) palloc0(nnewcolumns * sizeof(bool));
5047 : :
5048 : : /*
5049 : : * Generating the new_colnames array is a bit tricky since any new columns
5050 : : * added since parse time must be inserted in the right places. This code
5051 : : * must match the parser, which will order a join's columns as merged
5052 : : * columns first (in USING-clause order), then non-merged columns from the
5053 : : * left input (in attnum order), then non-merged columns from the right
5054 : : * input (ditto). If one of the inputs is itself a join, its columns will
5055 : : * be ordered according to the same rule, which means newly-added columns
5056 : : * might not be at the end. We can figure out what's what by consulting
5057 : : * the leftattnos and rightattnos arrays plus the input is_new_col arrays.
5058 : : *
5059 : : * In these loops, i indexes leftattnos/rightattnos (so it's join varattno
5060 : : * less one), j indexes new_colnames/is_new_col, and ic/jc have similar
5061 : : * meanings for the current child RTE.
5062 : : */
5063 : :
5064 : : /* Handle merged columns; they are first and can't be new */
5065 : 940 : i = j = 0;
5066 : 940 : while (i < noldcolumns &&
5067 [ + - + - ]: 1324 : colinfo->leftattnos[i] != 0 &&
5068 [ + + ]: 1324 : colinfo->rightattnos[i] != 0)
5069 : : {
5070 : : /* column name is already determined and known unique */
5071 : 384 : colinfo->new_colnames[j] = colinfo->colnames[i];
5072 : 384 : colinfo->is_new_col[j] = false;
5073 : :
5074 : : /* build bitmapsets of child attnums of merged columns */
5075 [ + - ]: 384 : if (colinfo->leftattnos[i] > 0)
5076 : 384 : leftmerged = bms_add_member(leftmerged, colinfo->leftattnos[i]);
5077 [ + - ]: 384 : if (colinfo->rightattnos[i] > 0)
5078 : 384 : rightmerged = bms_add_member(rightmerged, colinfo->rightattnos[i]);
5079 : :
5080 : 384 : i++, j++;
5081 : : }
5082 : :
5083 : : /* Handle non-merged left-child columns */
5084 : 940 : ic = 0;
5085 [ + + ]: 22369 : for (jc = 0; jc < leftcolinfo->num_new_cols; jc++)
5086 : : {
5087 : 21429 : char *child_colname = leftcolinfo->new_colnames[jc];
5088 : :
5089 [ + + ]: 21429 : if (!leftcolinfo->is_new_col[jc])
5090 : : {
5091 : : /* Advance ic to next non-dropped old column of left child */
5092 [ + - ]: 21157 : while (ic < leftcolinfo->num_cols &&
5093 [ + + ]: 21157 : leftcolinfo->colnames[ic] == NULL)
5094 : 56 : ic++;
5095 [ - + ]: 21101 : Assert(ic < leftcolinfo->num_cols);
5096 : 21101 : ic++;
5097 : : /* If it is a merged column, we already processed it */
5098 [ + + ]: 21101 : if (bms_is_member(ic, leftmerged))
5099 : 384 : continue;
5100 : : /* Else, advance i to the corresponding existing join column */
5101 [ + - ]: 20721 : while (i < colinfo->num_cols &&
5102 [ + + ]: 20721 : colinfo->colnames[i] == NULL)
5103 : 4 : i++;
5104 [ - + ]: 20717 : Assert(i < colinfo->num_cols);
5105 [ - + ]: 20717 : Assert(ic == colinfo->leftattnos[i]);
5106 : : /* Use the already-assigned name of this column */
5107 : 20717 : colinfo->new_colnames[j] = colinfo->colnames[i];
5108 : 20717 : i++;
5109 : : }
5110 : : else
5111 : : {
5112 : : /*
5113 : : * Unique-ify the new child column name and assign, unless we're
5114 : : * in an unnamed join, in which case just copy
5115 : : */
5116 [ + + ]: 328 : if (rte->alias != NULL)
5117 : : {
5118 : 176 : colinfo->new_colnames[j] =
5119 : 88 : make_colname_unique(child_colname, dpns, colinfo);
5120 [ + + ]: 88 : if (!changed_any &&
5121 [ + + ]: 72 : strcmp(colinfo->new_colnames[j], child_colname) != 0)
5122 : 8 : changed_any = true;
5123 : : }
5124 : : else
5125 : 240 : colinfo->new_colnames[j] = child_colname;
602 5126 : 328 : add_to_names_hash(colinfo, colinfo->new_colnames[j]);
5127 : : }
5128 : :
4873 5129 : 21045 : colinfo->is_new_col[j] = leftcolinfo->is_new_col[jc];
5130 : 21045 : j++;
5131 : : }
5132 : :
5133 : : /* Handle non-merged right-child columns in exactly the same way */
5134 : 940 : ic = 0;
5135 [ + + ]: 10268 : for (jc = 0; jc < rightcolinfo->num_new_cols; jc++)
5136 : : {
5137 : 9328 : char *child_colname = rightcolinfo->new_colnames[jc];
5138 : :
5139 [ + + ]: 9328 : if (!rightcolinfo->is_new_col[jc])
5140 : : {
5141 : : /* Advance ic to next non-dropped old column of right child */
5142 [ + - ]: 9216 : while (ic < rightcolinfo->num_cols &&
5143 [ - + ]: 9216 : rightcolinfo->colnames[ic] == NULL)
4873 tgl@sss.pgh.pa.us 5144 :UBC 0 : ic++;
4873 tgl@sss.pgh.pa.us 5145 [ - + ]:CBC 9216 : Assert(ic < rightcolinfo->num_cols);
5146 : 9216 : ic++;
5147 : : /* If it is a merged column, we already processed it */
5148 [ + + ]: 9216 : if (bms_is_member(ic, rightmerged))
5149 : 384 : continue;
5150 : : /* Else, advance i to the corresponding existing join column */
5151 [ + - ]: 8832 : while (i < colinfo->num_cols &&
5152 [ - + ]: 8832 : colinfo->colnames[i] == NULL)
4873 tgl@sss.pgh.pa.us 5153 :UBC 0 : i++;
4873 tgl@sss.pgh.pa.us 5154 [ - + ]:CBC 8832 : Assert(i < colinfo->num_cols);
5155 [ - + ]: 8832 : Assert(ic == colinfo->rightattnos[i]);
5156 : : /* Use the already-assigned name of this column */
5157 : 8832 : colinfo->new_colnames[j] = colinfo->colnames[i];
5158 : 8832 : i++;
5159 : : }
5160 : : else
5161 : : {
5162 : : /*
5163 : : * Unique-ify the new child column name and assign, unless we're
5164 : : * in an unnamed join, in which case just copy
5165 : : */
5166 [ + + ]: 112 : if (rte->alias != NULL)
5167 : : {
5168 : 32 : colinfo->new_colnames[j] =
5169 : 16 : make_colname_unique(child_colname, dpns, colinfo);
5170 [ + - ]: 16 : if (!changed_any &&
5171 [ + + ]: 16 : strcmp(colinfo->new_colnames[j], child_colname) != 0)
5172 : 8 : changed_any = true;
5173 : : }
5174 : : else
5175 : 96 : colinfo->new_colnames[j] = child_colname;
602 5176 : 112 : add_to_names_hash(colinfo, colinfo->new_colnames[j]);
5177 : : }
5178 : :
4873 5179 : 8944 : colinfo->is_new_col[j] = rightcolinfo->is_new_col[jc];
5180 : 8944 : j++;
5181 : : }
5182 : :
5183 : : /* Assert we processed the right number of columns */
5184 : : #ifdef USE_ASSERT_CHECKING
5185 [ - + - - ]: 940 : while (i < colinfo->num_cols && colinfo->colnames[i] == NULL)
4873 tgl@sss.pgh.pa.us 5186 :UBC 0 : i++;
4873 tgl@sss.pgh.pa.us 5187 [ - + ]:CBC 940 : Assert(i == colinfo->num_cols);
5188 [ - + ]: 940 : Assert(j == nnewcolumns);
5189 : : #endif
5190 : :
5191 : : /* We're now done needing the colinfo's names_hash */
602 5192 : 940 : destroy_colinfo_names_hash(colinfo);
5193 : :
5194 : : /*
5195 : : * For a named join, print column aliases if we changed any from the child
5196 : : * names. Unnamed joins cannot print aliases.
5197 : : */
4873 5198 [ + + ]: 940 : if (rte->alias != NULL)
5199 : 72 : colinfo->printaliases = changed_any;
5200 : : else
5201 : 868 : colinfo->printaliases = false;
5202 : 940 : }
5203 : :
5204 : : /*
5205 : : * colname_is_unique: is colname distinct from already-chosen column names?
5206 : : *
5207 : : * dpns is query-wide info, colinfo is for the column's RTE
5208 : : */
5209 : : static bool
3108 peter_e@gmx.net 5210 : 301254 : colname_is_unique(const char *colname, deparse_namespace *dpns,
5211 : : deparse_columns *colinfo)
5212 : : {
5213 : : int i;
5214 : : ListCell *lc;
5215 : :
5216 : : /*
5217 : : * If we have a hash table, consult that instead of linearly scanning the
5218 : : * colinfo's strings.
5219 : : */
602 tgl@sss.pgh.pa.us 5220 [ + + ]: 301254 : if (colinfo->names_hash)
5221 : : {
5222 [ - + ]: 10322 : if (hash_search(colinfo->names_hash,
5223 : : colname,
5224 : : HASH_FIND,
5225 : : NULL) != NULL)
4873 tgl@sss.pgh.pa.us 5226 :UBC 0 : return false;
5227 : : }
5228 : : else
5229 : : {
5230 : : /* Check against already-assigned column aliases within RTE */
602 tgl@sss.pgh.pa.us 5231 [ + + ]:CBC 4014316 : for (i = 0; i < colinfo->num_cols; i++)
5232 : : {
5233 : 3724903 : char *oldname = colinfo->colnames[i];
5234 : :
5235 [ + + + + ]: 3724903 : if (oldname && strcmp(oldname, colname) == 0)
5236 : 1519 : return false;
5237 : : }
5238 : :
5239 : : /*
5240 : : * If we're building a new_colnames array, check that too (this will
5241 : : * be partially but not completely redundant with the previous checks)
5242 : : */
5243 [ + + ]: 290261 : for (i = 0; i < colinfo->num_new_cols; i++)
5244 : : {
5245 : 864 : char *oldname = colinfo->new_colnames[i];
5246 : :
5247 [ + + + + ]: 864 : if (oldname && strcmp(oldname, colname) == 0)
5248 : 16 : return false;
5249 : : }
5250 : :
5251 : : /*
5252 : : * Also check against names already assigned for parent-join USING
5253 : : * cols
5254 : : */
5255 [ + + + + : 291093 : foreach(lc, colinfo->parentUsing)
+ + ]
5256 : : {
5257 : 1700 : char *oldname = (char *) lfirst(lc);
5258 : :
5259 [ + + ]: 1700 : if (strcmp(oldname, colname) == 0)
5260 : 4 : return false;
5261 : : }
5262 : : }
5263 : :
5264 : : /*
5265 : : * Also check against USING-column names that must be globally unique.
5266 : : * These are not hashed, but there should be few of them.
5267 : : */
5268 [ + + + + : 300287 : foreach(lc, dpns->using_names)
+ + ]
5269 : : {
4387 5270 : 600 : char *oldname = (char *) lfirst(lc);
5271 : :
5272 [ + + ]: 600 : if (strcmp(oldname, colname) == 0)
5273 : 28 : return false;
5274 : : }
5275 : :
4873 5276 : 299687 : return true;
5277 : : }
5278 : :
5279 : : /*
5280 : : * make_colname_unique: modify colname if necessary to make it unique
5281 : : *
5282 : : * dpns is query-wide info, colinfo is for the column's RTE
5283 : : */
5284 : : static char *
5285 : 299687 : make_colname_unique(char *colname, deparse_namespace *dpns,
5286 : : deparse_columns *colinfo)
5287 : : {
5288 : : /*
5289 : : * If the selected name isn't unique, append digits to make it so. For a
5290 : : * very long input name, we might have to truncate to stay within
5291 : : * NAMEDATALEN.
5292 : : */
5293 [ + + ]: 299687 : if (!colname_is_unique(colname, dpns, colinfo))
5294 : : {
3823 5295 : 1086 : int colnamelen = strlen(colname);
5296 : 1086 : char *modname = (char *) palloc(colnamelen + 16);
4873 5297 : 1086 : int i = 0;
5298 : :
5299 : : do
5300 : : {
3823 5301 : 1567 : i++;
5302 : : for (;;)
5303 : : {
5304 : 1567 : memcpy(modname, colname, colnamelen);
5305 : 1567 : sprintf(modname + colnamelen, "_%d", i);
5306 [ + - ]: 1567 : if (strlen(modname) < NAMEDATALEN)
5307 : 1567 : break;
5308 : : /* drop chars from colname to keep all the digits */
3823 tgl@sss.pgh.pa.us 5309 :UBC 0 : colnamelen = pg_mbcliplen(colname, colnamelen,
5310 : : colnamelen - 1);
5311 : : }
4873 tgl@sss.pgh.pa.us 5312 [ + + ]:CBC 1567 : } while (!colname_is_unique(modname, dpns, colinfo));
5313 : 1086 : colname = modname;
5314 : : }
5315 : 299687 : return colname;
5316 : : }
5317 : :
5318 : : /*
5319 : : * expand_colnames_array_to: make colinfo->colnames at least n items long
5320 : : *
5321 : : * Any added array entries are initialized to zero.
5322 : : */
5323 : : static void
5324 : 64750 : expand_colnames_array_to(deparse_columns *colinfo, int n)
5325 : : {
5326 [ + + ]: 64750 : if (n > colinfo->num_cols)
5327 : : {
5328 [ + + ]: 62975 : if (colinfo->colnames == NULL)
1270 peter@eisentraut.org 5329 : 62039 : colinfo->colnames = palloc0_array(char *, n);
5330 : : else
5331 : 936 : colinfo->colnames = repalloc0_array(colinfo->colnames, char *, colinfo->num_cols, n);
4873 tgl@sss.pgh.pa.us 5332 : 62975 : colinfo->num_cols = n;
5333 : : }
5334 : 64750 : }
5335 : :
5336 : : /*
5337 : : * build_colinfo_names_hash: optionally construct a hash table for colinfo
5338 : : */
5339 : : static void
602 5340 : 63558 : build_colinfo_names_hash(deparse_columns *colinfo)
5341 : : {
5342 : : HASHCTL hash_ctl;
5343 : : int i;
5344 : : ListCell *lc;
5345 : :
5346 : : /*
5347 : : * Use a hash table only for RTEs with at least 32 columns. (The cutoff
5348 : : * is somewhat arbitrary, but let's choose it so that this code does get
5349 : : * exercised in the regression tests.)
5350 : : */
5351 [ + + ]: 63558 : if (colinfo->num_cols < 32)
5352 : 62754 : return;
5353 : :
5354 : : /*
5355 : : * Set up the hash table. The entries are just strings with no other
5356 : : * payload.
5357 : : */
5358 : 804 : hash_ctl.keysize = NAMEDATALEN;
5359 : 804 : hash_ctl.entrysize = NAMEDATALEN;
5360 : 804 : hash_ctl.hcxt = CurrentMemoryContext;
5361 : 1608 : colinfo->names_hash = hash_create("deparse_columns names",
5362 : 804 : colinfo->num_cols + colinfo->num_new_cols,
5363 : : &hash_ctl,
5364 : : HASH_ELEM | HASH_STRINGS | HASH_CONTEXT);
5365 : :
5366 : : /*
5367 : : * Preload the hash table with any names already present (these would have
5368 : : * come from set_using_names).
5369 : : */
5370 [ + + ]: 37754 : for (i = 0; i < colinfo->num_cols; i++)
5371 : : {
5372 : 36950 : char *oldname = colinfo->colnames[i];
5373 : :
5374 [ - + ]: 36950 : if (oldname)
602 tgl@sss.pgh.pa.us 5375 :UBC 0 : add_to_names_hash(colinfo, oldname);
5376 : : }
5377 : :
602 tgl@sss.pgh.pa.us 5378 [ - + ]:CBC 804 : for (i = 0; i < colinfo->num_new_cols; i++)
5379 : : {
602 tgl@sss.pgh.pa.us 5380 :UBC 0 : char *oldname = colinfo->new_colnames[i];
5381 : :
5382 [ # # ]: 0 : if (oldname)
5383 : 0 : add_to_names_hash(colinfo, oldname);
5384 : : }
5385 : :
602 tgl@sss.pgh.pa.us 5386 [ - + - - :CBC 804 : foreach(lc, colinfo->parentUsing)
- + ]
5387 : : {
602 tgl@sss.pgh.pa.us 5388 :UBC 0 : char *oldname = (char *) lfirst(lc);
5389 : :
5390 : 0 : add_to_names_hash(colinfo, oldname);
5391 : : }
5392 : : }
5393 : :
5394 : : /*
5395 : : * add_to_names_hash: add a string to the names_hash, if we're using one
5396 : : */
5397 : : static void
602 tgl@sss.pgh.pa.us 5398 :CBC 329004 : add_to_names_hash(deparse_columns *colinfo, const char *name)
5399 : : {
5400 [ + + ]: 329004 : if (colinfo->names_hash)
5401 : 36950 : (void) hash_search(colinfo->names_hash,
5402 : : name,
5403 : : HASH_ENTER,
5404 : : NULL);
5405 : 329004 : }
5406 : :
5407 : : /*
5408 : : * destroy_colinfo_names_hash: destroy hash table when done with it
5409 : : */
5410 : : static void
5411 : 63558 : destroy_colinfo_names_hash(deparse_columns *colinfo)
5412 : : {
5413 [ + + ]: 63558 : if (colinfo->names_hash)
5414 : : {
5415 : 804 : hash_destroy(colinfo->names_hash);
5416 : 804 : colinfo->names_hash = NULL;
5417 : : }
5418 : 63558 : }
5419 : :
5420 : : /*
5421 : : * identify_join_columns: figure out where columns of a join come from
5422 : : *
5423 : : * Fills the join-specific fields of the colinfo struct, except for
5424 : : * usingNames which is filled later.
5425 : : */
5426 : : static void
4873 5427 : 940 : identify_join_columns(JoinExpr *j, RangeTblEntry *jrte,
5428 : : deparse_columns *colinfo)
5429 : : {
5430 : : int numjoincols;
5431 : : int jcolno;
5432 : : int rcolno;
5433 : : ListCell *lc;
5434 : :
5435 : : /* Extract left/right child RT indexes */
5436 [ + + ]: 940 : if (IsA(j->larg, RangeTblRef))
5437 : 605 : colinfo->leftrti = ((RangeTblRef *) j->larg)->rtindex;
5438 [ + - ]: 335 : else if (IsA(j->larg, JoinExpr))
5439 : 335 : colinfo->leftrti = ((JoinExpr *) j->larg)->rtindex;
5440 : : else
4873 tgl@sss.pgh.pa.us 5441 [ # # ]:UBC 0 : elog(ERROR, "unrecognized node type in jointree: %d",
5442 : : (int) nodeTag(j->larg));
4873 tgl@sss.pgh.pa.us 5443 [ + - ]:CBC 940 : if (IsA(j->rarg, RangeTblRef))
5444 : 940 : colinfo->rightrti = ((RangeTblRef *) j->rarg)->rtindex;
4873 tgl@sss.pgh.pa.us 5445 [ # # ]:UBC 0 : else if (IsA(j->rarg, JoinExpr))
5446 : 0 : colinfo->rightrti = ((JoinExpr *) j->rarg)->rtindex;
5447 : : else
5448 [ # # ]: 0 : elog(ERROR, "unrecognized node type in jointree: %d",
5449 : : (int) nodeTag(j->rarg));
5450 : :
5451 : : /* Assert children will be processed earlier than join in second pass */
4873 tgl@sss.pgh.pa.us 5452 [ - + ]:CBC 940 : Assert(colinfo->leftrti < j->rtindex);
5453 [ - + ]: 940 : Assert(colinfo->rightrti < j->rtindex);
5454 : :
5455 : : /* Initialize result arrays with zeroes */
5456 : 940 : numjoincols = list_length(jrte->joinaliasvars);
5457 [ - + ]: 940 : Assert(numjoincols == list_length(jrte->eref->colnames));
5458 : 940 : colinfo->leftattnos = (int *) palloc0(numjoincols * sizeof(int));
5459 : 940 : colinfo->rightattnos = (int *) palloc0(numjoincols * sizeof(int));
5460 : :
5461 : : /*
5462 : : * Deconstruct RTE's joinleftcols/joinrightcols into desired format.
5463 : : * Recall that the column(s) merged due to USING are the first column(s)
5464 : : * of the join output. We need not do anything special while scanning
5465 : : * joinleftcols, but while scanning joinrightcols we must distinguish
5466 : : * merged from unmerged columns.
5467 : : */
2308 5468 : 940 : jcolno = 0;
5469 [ + - + + : 22045 : foreach(lc, jrte->joinleftcols)
+ + ]
5470 : : {
5471 : 21105 : int leftattno = lfirst_int(lc);
5472 : :
5473 : 21105 : colinfo->leftattnos[jcolno++] = leftattno;
5474 : : }
5475 : 940 : rcolno = 0;
5476 [ + - + + : 10156 : foreach(lc, jrte->joinrightcols)
+ + ]
5477 : : {
5478 : 9216 : int rightattno = lfirst_int(lc);
5479 : :
5480 [ + + ]: 9216 : if (rcolno < jrte->joinmergedcols) /* merged column? */
5481 : 384 : colinfo->rightattnos[rcolno] = rightattno;
5482 : : else
5483 : 8832 : colinfo->rightattnos[jcolno++] = rightattno;
5484 : 9216 : rcolno++;
5485 : : }
5486 [ - + ]: 940 : Assert(jcolno == numjoincols);
4873 5487 : 940 : }
5488 : :
5489 : : /*
5490 : : * get_rtable_name: convenience function to get a previously assigned RTE alias
5491 : : *
5492 : : * The RTE must belong to the topmost namespace level in "context".
5493 : : */
5494 : : static char *
4974 5495 : 4112 : get_rtable_name(int rtindex, deparse_context *context)
5496 : : {
5497 : 4112 : deparse_namespace *dpns = (deparse_namespace *) linitial(context->namespaces);
5498 : :
5499 [ + - - + ]: 4112 : Assert(rtindex > 0 && rtindex <= list_length(dpns->rtable_names));
5500 : 4112 : return (char *) list_nth(dpns->rtable_names, rtindex - 1);
5501 : : }
5502 : :
5503 : : /*
5504 : : * set_deparse_plan: set up deparse_namespace to parse subexpressions
5505 : : * of a given Plan node
5506 : : *
5507 : : * This sets the plan, outer_plan, inner_plan, outer_tlist, inner_tlist,
5508 : : * and index_tlist fields. Caller must already have adjusted the ancestors
5509 : : * list if necessary. Note that the rtable, subplans, and ctes fields do
5510 : : * not need to change when shifting attention to different plan nodes in a
5511 : : * single plan tree.
5512 : : */
5513 : : static void
2337 5514 : 100509 : set_deparse_plan(deparse_namespace *dpns, Plan *plan)
5515 : : {
5516 : 100509 : dpns->plan = plan;
5517 : :
5518 : : /*
5519 : : * We special-case Append and MergeAppend to pretend that the first child
5520 : : * plan is the OUTER referent; we have to interpret OUTER Vars in their
5521 : : * tlists according to one of the children, and the first one is the most
5522 : : * natural choice.
5523 : : */
5524 [ + + ]: 100509 : if (IsA(plan, Append))
5525 : 2948 : dpns->outer_plan = linitial(((Append *) plan)->appendplans);
5526 [ + + ]: 97561 : else if (IsA(plan, MergeAppend))
5527 : 360 : dpns->outer_plan = linitial(((MergeAppend *) plan)->mergeplans);
5528 : : else
5529 : 97201 : dpns->outer_plan = outerPlan(plan);
5530 : :
5531 [ + + ]: 100509 : if (dpns->outer_plan)
5532 : 48544 : dpns->outer_tlist = dpns->outer_plan->targetlist;
5533 : : else
5320 5534 : 51965 : dpns->outer_tlist = NIL;
5535 : :
5536 : : /*
5537 : : * For a SubqueryScan, pretend the subplan is INNER referent. (We don't
5538 : : * use OUTER because that could someday conflict with the normal meaning.)
5539 : : * Likewise, for a CteScan, pretend the subquery's plan is INNER referent.
5540 : : * For a WorkTableScan, locate the parent RecursiveUnion plan node and use
5541 : : * that as INNER referent.
5542 : : *
5543 : : * For MERGE, pretend the ModifyTable's source plan (its outer plan) is
5544 : : * INNER referent. This is the join from the target relation to the data
5545 : : * source, and all INNER_VAR Vars in other parts of the query refer to its
5546 : : * targetlist.
5547 : : *
5548 : : * For ON CONFLICT DO SELECT/UPDATE we just need the inner tlist to point
5549 : : * to the excluded expression's tlist. (Similar to the SubqueryScan we
5550 : : * don't want to reuse OUTER, it's used for RETURNING in some modify table
5551 : : * cases, although not INSERT .. CONFLICT).
5552 : : */
2337 5553 [ + + ]: 100509 : if (IsA(plan, SubqueryScan))
5554 : 453 : dpns->inner_plan = ((SubqueryScan *) plan)->subplan;
5555 [ + + ]: 100056 : else if (IsA(plan, CteScan))
5556 : 373 : dpns->inner_plan = list_nth(dpns->subplans,
5557 : 373 : ((CteScan *) plan)->ctePlanId - 1);
1692 5558 [ + + ]: 99683 : else if (IsA(plan, WorkTableScan))
5559 : 116 : dpns->inner_plan = find_recursive_union(dpns,
5560 : : (WorkTableScan *) plan);
2337 5561 [ + + ]: 99567 : else if (IsA(plan, ModifyTable))
5562 : : {
1499 alvherre@alvh.no-ip. 5563 [ + + ]: 287 : if (((ModifyTable *) plan)->operation == CMD_MERGE)
779 dean.a.rasheed@gmail 5564 : 40 : dpns->inner_plan = outerPlan(plan);
5565 : : else
5566 : 247 : dpns->inner_plan = plan;
5567 : : }
5568 : : else
5569 : 99280 : dpns->inner_plan = innerPlan(plan);
5570 : :
5571 [ + + + + ]: 100509 : if (IsA(plan, ModifyTable) && ((ModifyTable *) plan)->operation == CMD_INSERT)
5572 : 141 : dpns->inner_tlist = ((ModifyTable *) plan)->exclRelTlist;
2337 tgl@sss.pgh.pa.us 5573 [ + + ]: 100368 : else if (dpns->inner_plan)
5574 : 17444 : dpns->inner_tlist = dpns->inner_plan->targetlist;
5575 : : else
5320 5576 : 82924 : dpns->inner_tlist = NIL;
5577 : :
5578 : : /* Set up referent for INDEX_VAR Vars, if needed */
2337 5579 [ + + ]: 100509 : if (IsA(plan, IndexOnlyScan))
5580 : 2288 : dpns->index_tlist = ((IndexOnlyScan *) plan)->indextlist;
5581 [ + + ]: 98221 : else if (IsA(plan, ForeignScan))
5582 : 1558 : dpns->index_tlist = ((ForeignScan *) plan)->fdw_scan_tlist;
5583 [ - + ]: 96663 : else if (IsA(plan, CustomScan))
2337 tgl@sss.pgh.pa.us 5584 :UBC 0 : dpns->index_tlist = ((CustomScan *) plan)->custom_scan_tlist;
5585 : : else
5320 tgl@sss.pgh.pa.us 5586 :CBC 96663 : dpns->index_tlist = NIL;
5775 5587 : 100509 : }
5588 : :
5589 : : /*
5590 : : * Locate the ancestor plan node that is the RecursiveUnion generating
5591 : : * the WorkTableScan's work table. We can match on wtParam, since that
5592 : : * should be unique within the plan tree.
5593 : : */
5594 : : static Plan *
1692 5595 : 116 : find_recursive_union(deparse_namespace *dpns, WorkTableScan *wtscan)
5596 : : {
5597 : : ListCell *lc;
5598 : :
5599 [ + - + - : 292 : foreach(lc, dpns->ancestors)
+ - ]
5600 : : {
5601 : 292 : Plan *ancestor = (Plan *) lfirst(lc);
5602 : :
5603 [ + + ]: 292 : if (IsA(ancestor, RecursiveUnion) &&
5604 [ + - ]: 116 : ((RecursiveUnion *) ancestor)->wtParam == wtscan->wtParam)
5605 : 116 : return ancestor;
5606 : : }
1692 tgl@sss.pgh.pa.us 5607 [ # # ]:UBC 0 : elog(ERROR, "could not find RecursiveUnion for WorkTableScan with wtParam %d",
5608 : : wtscan->wtParam);
5609 : : return NULL;
5610 : : }
5611 : :
5612 : : /*
5613 : : * push_child_plan: temporarily transfer deparsing attention to a child plan
5614 : : *
5615 : : * When expanding an OUTER_VAR or INNER_VAR reference, we must adjust the
5616 : : * deparse context in case the referenced expression itself uses
5617 : : * OUTER_VAR/INNER_VAR. We modify the top stack entry in-place to avoid
5618 : : * affecting levelsup issues (although in a Plan tree there really shouldn't
5619 : : * be any).
5620 : : *
5621 : : * Caller must provide a local deparse_namespace variable to save the
5622 : : * previous state for pop_child_plan.
5623 : : */
5624 : : static void
2337 tgl@sss.pgh.pa.us 5625 :CBC 58293 : push_child_plan(deparse_namespace *dpns, Plan *plan,
5626 : : deparse_namespace *save_dpns)
5627 : : {
5628 : : /* Save state for restoration later */
5775 5629 : 58293 : *save_dpns = *dpns;
5630 : :
5631 : : /* Link current plan node into ancestors list */
2337 5632 : 58293 : dpns->ancestors = lcons(dpns->plan, dpns->ancestors);
5633 : :
5634 : : /* Set attention on selected child */
5635 : 58293 : set_deparse_plan(dpns, plan);
9211 5636 : 58293 : }
5637 : :
5638 : : /*
5639 : : * pop_child_plan: undo the effects of push_child_plan
5640 : : */
5641 : : static void
5775 5642 : 58293 : pop_child_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
5643 : : {
5644 : : List *ancestors;
5645 : :
5646 : : /* Get rid of ancestors list cell added by push_child_plan */
4996 5647 : 58293 : ancestors = list_delete_first(dpns->ancestors);
5648 : :
5649 : : /* Restore fields changed by push_child_plan */
5775 5650 : 58293 : *dpns = *save_dpns;
5651 : :
5652 : : /* Make sure dpns->ancestors is right (may be unnecessary) */
4996 5653 : 58293 : dpns->ancestors = ancestors;
5775 5654 : 58293 : }
5655 : :
5656 : : /*
5657 : : * push_ancestor_plan: temporarily transfer deparsing attention to an
5658 : : * ancestor plan
5659 : : *
5660 : : * When expanding a Param reference, we must adjust the deparse context
5661 : : * to match the plan node that contains the expression being printed;
5662 : : * otherwise we'd fail if that expression itself contains a Param or
5663 : : * OUTER_VAR/INNER_VAR/INDEX_VAR variable.
5664 : : *
5665 : : * The target ancestor is conveniently identified by the ListCell holding it
5666 : : * in dpns->ancestors.
5667 : : *
5668 : : * Caller must provide a local deparse_namespace variable to save the
5669 : : * previous state for pop_ancestor_plan.
5670 : : */
5671 : : static void
5672 : 3096 : push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
5673 : : deparse_namespace *save_dpns)
5674 : : {
2337 5675 : 3096 : Plan *plan = (Plan *) lfirst(ancestor_cell);
5676 : :
5677 : : /* Save state for restoration later */
5775 5678 : 3096 : *save_dpns = *dpns;
5679 : :
5680 : : /* Build a new ancestor list with just this node's ancestors */
2486 5681 : 3096 : dpns->ancestors =
5682 : 3096 : list_copy_tail(dpns->ancestors,
5683 : 3096 : list_cell_number(dpns->ancestors, ancestor_cell) + 1);
5684 : :
5685 : : /* Set attention on selected ancestor */
2337 5686 : 3096 : set_deparse_plan(dpns, plan);
5775 5687 : 3096 : }
5688 : :
5689 : : /*
5690 : : * pop_ancestor_plan: undo the effects of push_ancestor_plan
5691 : : */
5692 : : static void
5693 : 3096 : pop_ancestor_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
5694 : : {
5695 : : /* Free the ancestor list made in push_ancestor_plan */
5696 : 3096 : list_free(dpns->ancestors);
5697 : :
5698 : : /* Restore fields changed by push_ancestor_plan */
5699 : 3096 : *dpns = *save_dpns;
5700 : 3096 : }
5701 : :
5702 : :
5703 : : /* ----------
5704 : : * make_ruledef - reconstruct the CREATE RULE command
5705 : : * for a given pg_rewrite tuple
5706 : : * ----------
5707 : : */
5708 : : static void
8315 5709 : 309 : make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5710 : : int prettyFlags)
5711 : : {
5712 : : char *rulename;
5713 : : char ev_type;
5714 : : Oid ev_class;
5715 : : bool is_instead;
5716 : : char *ev_qual;
5717 : : char *ev_action;
5718 : : List *actions;
5719 : : Relation ev_relation;
3207 5720 : 309 : TupleDesc viewResultDesc = NULL;
5721 : : int fno;
5722 : : Datum dat;
5723 : : bool isnull;
5724 : :
5725 : : /*
5726 : : * Get the attribute values from the rules tuple
5727 : : */
8783 5728 : 309 : fno = SPI_fnumber(rulettc, "rulename");
5729 : 309 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5730 [ - + ]: 309 : Assert(!isnull);
5731 : 309 : rulename = NameStr(*(DatumGetName(dat)));
5732 : :
10108 bruce@momjian.us 5733 : 309 : fno = SPI_fnumber(rulettc, "ev_type");
8783 tgl@sss.pgh.pa.us 5734 : 309 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5735 [ - + ]: 309 : Assert(!isnull);
5736 : 309 : ev_type = DatumGetChar(dat);
5737 : :
10108 bruce@momjian.us 5738 : 309 : fno = SPI_fnumber(rulettc, "ev_class");
8783 tgl@sss.pgh.pa.us 5739 : 309 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5740 [ - + ]: 309 : Assert(!isnull);
5741 : 309 : ev_class = DatumGetObjectId(dat);
5742 : :
10108 bruce@momjian.us 5743 : 309 : fno = SPI_fnumber(rulettc, "is_instead");
8783 tgl@sss.pgh.pa.us 5744 : 309 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5745 [ - + ]: 309 : Assert(!isnull);
5746 : 309 : is_instead = DatumGetBool(dat);
5747 : :
10108 bruce@momjian.us 5748 : 309 : fno = SPI_fnumber(rulettc, "ev_qual");
5749 : 309 : ev_qual = SPI_getvalue(ruletup, rulettc, fno);
2010 tgl@sss.pgh.pa.us 5750 [ - + ]: 309 : Assert(ev_qual != NULL);
5751 : :
10108 bruce@momjian.us 5752 : 309 : fno = SPI_fnumber(rulettc, "ev_action");
5753 : 309 : ev_action = SPI_getvalue(ruletup, rulettc, fno);
2010 tgl@sss.pgh.pa.us 5754 [ - + ]: 309 : Assert(ev_action != NULL);
5755 : 309 : actions = (List *) stringToNode(ev_action);
5756 [ - + ]: 309 : if (actions == NIL)
2010 tgl@sss.pgh.pa.us 5757 [ # # ]:UBC 0 : elog(ERROR, "invalid empty ev_action list");
5758 : :
2661 andres@anarazel.de 5759 :CBC 309 : ev_relation = table_open(ev_class, AccessShareLock);
5760 : :
5761 : : /*
5762 : : * Build the rules definition text
5763 : : */
8315 tgl@sss.pgh.pa.us 5764 : 309 : appendStringInfo(buf, "CREATE RULE %s AS",
5765 : : quote_identifier(rulename));
5766 : :
5767 [ + - ]: 309 : if (prettyFlags & PRETTYFLAG_INDENT)
8310 bruce@momjian.us 5768 : 309 : appendStringInfoString(buf, "\n ON ");
5769 : : else
8310 bruce@momjian.us 5770 :UBC 0 : appendStringInfoString(buf, " ON ");
5771 : :
5772 : : /* The event the rule is fired for */
10108 bruce@momjian.us 5773 [ + + + + :CBC 309 : switch (ev_type)
- ]
5774 : : {
5775 : 4 : case '1':
4569 rhaas@postgresql.org 5776 : 4 : appendStringInfoString(buf, "SELECT");
3207 tgl@sss.pgh.pa.us 5777 : 4 : viewResultDesc = RelationGetDescr(ev_relation);
10116 bruce@momjian.us 5778 : 4 : break;
5779 : :
10108 5780 : 84 : case '2':
4569 rhaas@postgresql.org 5781 : 84 : appendStringInfoString(buf, "UPDATE");
10116 bruce@momjian.us 5782 : 84 : break;
5783 : :
10108 5784 : 165 : case '3':
4569 rhaas@postgresql.org 5785 : 165 : appendStringInfoString(buf, "INSERT");
10116 bruce@momjian.us 5786 : 165 : break;
5787 : :
10108 5788 : 56 : case '4':
4569 rhaas@postgresql.org 5789 : 56 : appendStringInfoString(buf, "DELETE");
10116 bruce@momjian.us 5790 : 56 : break;
5791 : :
10108 bruce@momjian.us 5792 :UBC 0 : default:
8318 tgl@sss.pgh.pa.us 5793 [ # # ]: 0 : ereport(ERROR,
5794 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5795 : : errmsg("rule \"%s\" has unsupported event type %d",
5796 : : rulename, ev_type)));
5797 : : break;
5798 : : }
5799 : :
5800 : : /* The relation the rule is fired on */
2990 tgl@sss.pgh.pa.us 5801 :CBC 309 : appendStringInfo(buf, " TO %s",
5802 [ + + ]: 309 : (prettyFlags & PRETTYFLAG_SCHEMA) ?
5803 : 76 : generate_relation_name(ev_class, NIL) :
5804 : 233 : generate_qualified_relation_name(ev_class));
5805 : :
5806 : : /* If the rule has an event qualification, add it */
2010 5807 [ + + ]: 309 : if (strcmp(ev_qual, "<>") != 0)
5808 : : {
5809 : : Node *qual;
5810 : : Query *query;
5811 : : deparse_context context;
5812 : : deparse_namespace dpns;
5813 : :
8315 5814 [ + - ]: 62 : if (prettyFlags & PRETTYFLAG_INDENT)
8310 bruce@momjian.us 5815 : 62 : appendStringInfoString(buf, "\n ");
4569 rhaas@postgresql.org 5816 : 62 : appendStringInfoString(buf, " WHERE ");
5817 : :
10108 bruce@momjian.us 5818 : 62 : qual = stringToNode(ev_qual);
5819 : :
5820 : : /*
5821 : : * We need to make a context for recognizing any Vars in the qual
5822 : : * (which can only be references to OLD and NEW). Use the rtable of
5823 : : * the first query in the action list for this purpose.
5824 : : */
8014 neilc@samurai.com 5825 : 62 : query = (Query *) linitial(actions);
5826 : :
5827 : : /*
5828 : : * If the action is INSERT...SELECT, OLD/NEW have been pushed down
5829 : : * into the SELECT, and that's what we need to look at. (Ugly kluge
5830 : : * ... try to fix this when we redesign querytrees.)
5831 : : */
8926 tgl@sss.pgh.pa.us 5832 : 62 : query = getInsertSelectQuery(query, NULL);
5833 : :
5834 : : /* Must acquire locks right away; see notes in get_query_def() */
4443 5835 : 62 : AcquireRewriteLocks(query, false, false);
5836 : :
9711 5837 : 62 : context.buf = buf;
8010 neilc@samurai.com 5838 : 62 : context.namespaces = list_make1(&dpns);
614 tgl@sss.pgh.pa.us 5839 : 62 : context.resultDesc = NULL;
5840 : 62 : context.targetList = NIL;
6337 5841 : 62 : context.windowClause = NIL;
8010 neilc@samurai.com 5842 : 62 : context.varprefix = (list_length(query->rtable) != 1);
8315 tgl@sss.pgh.pa.us 5843 : 62 : context.prettyFlags = prettyFlags;
4880 5844 : 62 : context.wrapColumn = WRAP_COLUMN_DEFAULT;
8315 5845 : 62 : context.indentLevel = PRETTYINDENT_STD;
614 5846 : 62 : context.colNamesVisible = true;
5847 : 62 : context.inGroupBy = false;
5848 : 62 : context.varInOrderBy = false;
2337 5849 : 62 : context.appendparents = NULL;
5850 : :
4873 5851 : 62 : set_deparse_for_query(&dpns, query, NIL);
5852 : :
8629 5853 : 62 : get_rule_expr(qual, &context, false);
5854 : : }
5855 : :
4569 rhaas@postgresql.org 5856 : 309 : appendStringInfoString(buf, " DO ");
5857 : :
5858 : : /* The INSTEAD keyword (if so) */
10108 bruce@momjian.us 5859 [ + + ]: 309 : if (is_instead)
4569 rhaas@postgresql.org 5860 : 183 : appendStringInfoString(buf, "INSTEAD ");
5861 : :
5862 : : /* Finally the rules actions */
8010 neilc@samurai.com 5863 [ + + ]: 309 : if (list_length(actions) > 1)
5864 : : {
5865 : : ListCell *action;
5866 : : Query *query;
5867 : :
4569 rhaas@postgresql.org 5868 : 10 : appendStringInfoChar(buf, '(');
10108 bruce@momjian.us 5869 [ + - + + : 30 : foreach(action, actions)
+ + ]
5870 : : {
5871 : 20 : query = (Query *) lfirst(action);
1445 tgl@sss.pgh.pa.us 5872 : 20 : get_query_def(query, buf, NIL, viewResultDesc, true,
5873 : : prettyFlags, WRAP_COLUMN_DEFAULT, 0);
8315 5874 [ + - ]: 20 : if (prettyFlags)
4569 rhaas@postgresql.org 5875 : 20 : appendStringInfoString(buf, ";\n");
5876 : : else
4569 rhaas@postgresql.org 5877 :UBC 0 : appendStringInfoString(buf, "; ");
5878 : : }
4569 rhaas@postgresql.org 5879 :CBC 10 : appendStringInfoString(buf, ");");
5880 : : }
5881 : : else
5882 : : {
5883 : : Query *query;
5884 : :
8014 neilc@samurai.com 5885 : 299 : query = (Query *) linitial(actions);
1445 tgl@sss.pgh.pa.us 5886 : 299 : get_query_def(query, buf, NIL, viewResultDesc, true,
5887 : : prettyFlags, WRAP_COLUMN_DEFAULT, 0);
4569 rhaas@postgresql.org 5888 : 299 : appendStringInfoChar(buf, ';');
5889 : : }
5890 : :
2661 andres@anarazel.de 5891 : 309 : table_close(ev_relation, AccessShareLock);
10116 bruce@momjian.us 5892 : 309 : }
5893 : :
5894 : :
5895 : : /* ----------
5896 : : * make_viewdef - reconstruct the SELECT part of a
5897 : : * view rewrite rule
5898 : : * ----------
5899 : : */
5900 : : static void
8315 tgl@sss.pgh.pa.us 5901 : 2133 : make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5902 : : int prettyFlags, int wrapColumn)
5903 : : {
5904 : : Query *query;
5905 : : char ev_type;
5906 : : Oid ev_class;
5907 : : bool is_instead;
5908 : : char *ev_qual;
5909 : : char *ev_action;
5910 : : List *actions;
5911 : : Relation ev_relation;
5912 : : int fno;
5913 : : Datum dat;
5914 : : bool isnull;
5915 : :
5916 : : /*
5917 : : * Get the attribute values from the rules tuple
5918 : : */
10108 bruce@momjian.us 5919 : 2133 : fno = SPI_fnumber(rulettc, "ev_type");
3207 tgl@sss.pgh.pa.us 5920 : 2133 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5921 [ - + ]: 2133 : Assert(!isnull);
5922 : 2133 : ev_type = DatumGetChar(dat);
5923 : :
10108 bruce@momjian.us 5924 : 2133 : fno = SPI_fnumber(rulettc, "ev_class");
3207 tgl@sss.pgh.pa.us 5925 : 2133 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5926 [ - + ]: 2133 : Assert(!isnull);
5927 : 2133 : ev_class = DatumGetObjectId(dat);
5928 : :
10108 bruce@momjian.us 5929 : 2133 : fno = SPI_fnumber(rulettc, "is_instead");
3207 tgl@sss.pgh.pa.us 5930 : 2133 : dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5931 [ - + ]: 2133 : Assert(!isnull);
5932 : 2133 : is_instead = DatumGetBool(dat);
5933 : :
10108 bruce@momjian.us 5934 : 2133 : fno = SPI_fnumber(rulettc, "ev_qual");
5935 : 2133 : ev_qual = SPI_getvalue(ruletup, rulettc, fno);
2010 tgl@sss.pgh.pa.us 5936 [ - + ]: 2133 : Assert(ev_qual != NULL);
5937 : :
10108 bruce@momjian.us 5938 : 2133 : fno = SPI_fnumber(rulettc, "ev_action");
5939 : 2133 : ev_action = SPI_getvalue(ruletup, rulettc, fno);
2010 tgl@sss.pgh.pa.us 5940 [ - + ]: 2133 : Assert(ev_action != NULL);
5941 : 2133 : actions = (List *) stringToNode(ev_action);
5942 : :
8010 neilc@samurai.com 5943 [ - + ]: 2133 : if (list_length(actions) != 1)
5944 : : {
5945 : : /* keep output buffer empty and leave */
9712 tgl@sss.pgh.pa.us 5946 :UBC 0 : return;
5947 : : }
5948 : :
8014 neilc@samurai.com 5949 :CBC 2133 : query = (Query *) linitial(actions);
5950 : :
4625 kgrittn@postgresql.o 5951 [ + - + - ]: 2133 : if (ev_type != '1' || !is_instead ||
8671 tgl@sss.pgh.pa.us 5952 [ + - - + ]: 2133 : strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
5953 : : {
5954 : : /* keep output buffer empty and leave */
9712 tgl@sss.pgh.pa.us 5955 :UBC 0 : return;
5956 : : }
5957 : :
2661 andres@anarazel.de 5958 :CBC 2133 : ev_relation = table_open(ev_class, AccessShareLock);
5959 : :
1445 tgl@sss.pgh.pa.us 5960 : 2133 : get_query_def(query, buf, NIL, RelationGetDescr(ev_relation), true,
5961 : : prettyFlags, wrapColumn, 0);
4569 rhaas@postgresql.org 5962 : 2133 : appendStringInfoChar(buf, ';');
5963 : :
2661 andres@anarazel.de 5964 : 2133 : table_close(ev_relation, AccessShareLock);
5965 : : }
5966 : :
5967 : :
5968 : : /* ----------
5969 : : * get_query_def - Parse back one query parsetree
5970 : : *
5971 : : * query: parsetree to be displayed
5972 : : * buf: output text is appended to buf
5973 : : * parentnamespace: list (initially empty) of outer-level deparse_namespace's
5974 : : * resultDesc: if not NULL, the output tuple descriptor for the view
5975 : : * represented by a SELECT query. We use the column names from it
5976 : : * to label SELECT output columns, in preference to names in the query
5977 : : * colNamesVisible: true if the surrounding context cares about the output
5978 : : * column names at all (as, for example, an EXISTS() context does not);
5979 : : * when false, we can suppress dummy column labels such as "?column?"
5980 : : * prettyFlags: bitmask of PRETTYFLAG_XXX options
5981 : : * wrapColumn: maximum line length, or -1 to disable wrapping
5982 : : * startIndent: initial indentation amount
5983 : : * ----------
5984 : : */
5985 : : static void
8671 tgl@sss.pgh.pa.us 5986 : 3498 : get_query_def(Query *query, StringInfo buf, List *parentnamespace,
5987 : : TupleDesc resultDesc, bool colNamesVisible,
5988 : : int prettyFlags, int wrapColumn, int startIndent)
5989 : : {
5990 : : deparse_context context;
5991 : : deparse_namespace dpns;
5992 : : int rtable_size;
5993 : :
5994 : : /* Guard against excessively long or deeply-nested queries */
4388 5995 [ - + ]: 3498 : CHECK_FOR_INTERRUPTS();
5996 : 3498 : check_stack_depth();
5997 : :
602 rguo@postgresql.org 5998 : 6996 : rtable_size = query->hasGroupRTE ?
5999 [ + + ]: 3498 : list_length(query->rtable) - 1 :
6000 : 3379 : list_length(query->rtable);
6001 : :
6002 : : /*
6003 : : * Replace any Vars in the query's targetlist and havingQual that
6004 : : * reference GROUP outputs with the underlying grouping expressions.
6005 : : *
6006 : : * We can safely pass NULL for the root here. Preserving varnullingrels
6007 : : * makes no difference to the deparsed source text.
6008 : : */
6009 [ + + ]: 3498 : if (query->hasGroupRTE)
6010 : : {
6011 : 119 : query->targetList = (List *)
6012 : 119 : flatten_group_exprs(NULL, query, (Node *) query->targetList);
6013 : 119 : query->havingQual =
6014 : 119 : flatten_group_exprs(NULL, query, query->havingQual);
6015 : : }
6016 : :
6017 : : /*
6018 : : * Before we begin to examine the query, acquire locks on referenced
6019 : : * relations, and fix up deleted columns in JOIN RTEs. This ensures
6020 : : * consistent results. Note we assume it's OK to scribble on the passed
6021 : : * querytree!
6022 : : *
6023 : : * We are only deparsing the query (we are not about to execute it), so we
6024 : : * only need AccessShareLock on the relations it mentions.
6025 : : */
4443 tgl@sss.pgh.pa.us 6026 : 3498 : AcquireRewriteLocks(query, false, false);
6027 : :
9711 6028 : 3498 : context.buf = buf;
8014 neilc@samurai.com 6029 : 3498 : context.namespaces = lcons(&dpns, list_copy(parentnamespace));
614 tgl@sss.pgh.pa.us 6030 : 3498 : context.resultDesc = NULL;
6031 : 3498 : context.targetList = NIL;
6337 6032 : 3498 : context.windowClause = NIL;
9211 6033 [ + + + + ]: 3498 : context.varprefix = (parentnamespace != NIL ||
6034 : : rtable_size != 1);
8315 6035 : 3498 : context.prettyFlags = prettyFlags;
4880 6036 : 3498 : context.wrapColumn = wrapColumn;
8315 6037 : 3498 : context.indentLevel = startIndent;
614 6038 : 3498 : context.colNamesVisible = colNamesVisible;
6039 : 3498 : context.inGroupBy = false;
6040 : 3498 : context.varInOrderBy = false;
2337 6041 : 3498 : context.appendparents = NULL;
6042 : :
4873 6043 : 3498 : set_deparse_for_query(&dpns, query, parentnamespace);
6044 : :
10108 bruce@momjian.us 6045 [ + + + + : 3498 : switch (query->commandType)
+ + + - ]
6046 : : {
9842 6047 : 3124 : case CMD_SELECT:
6048 : : /* We set context.resultDesc only if it's a SELECT */
614 tgl@sss.pgh.pa.us 6049 : 3124 : context.resultDesc = resultDesc;
6050 : 3124 : get_select_query_def(query, &context);
10108 bruce@momjian.us 6051 : 3124 : break;
6052 : :
6053 : 94 : case CMD_UPDATE:
614 tgl@sss.pgh.pa.us 6054 : 94 : get_update_query_def(query, &context);
10108 bruce@momjian.us 6055 : 94 : break;
6056 : :
6057 : 194 : case CMD_INSERT:
614 tgl@sss.pgh.pa.us 6058 : 194 : get_insert_query_def(query, &context);
10108 bruce@momjian.us 6059 : 194 : break;
6060 : :
6061 : 47 : case CMD_DELETE:
614 tgl@sss.pgh.pa.us 6062 : 47 : get_delete_query_def(query, &context);
10108 bruce@momjian.us 6063 : 47 : break;
6064 : :
1094 tgl@sss.pgh.pa.us 6065 : 8 : case CMD_MERGE:
614 6066 : 8 : get_merge_query_def(query, &context);
1094 6067 : 8 : break;
6068 : :
10108 bruce@momjian.us 6069 : 22 : case CMD_NOTHING:
4569 rhaas@postgresql.org 6070 : 22 : appendStringInfoString(buf, "NOTHING");
10108 bruce@momjian.us 6071 : 22 : break;
6072 : :
9253 tgl@sss.pgh.pa.us 6073 : 9 : case CMD_UTILITY:
6074 : 9 : get_utility_query_def(query, &context);
6075 : 9 : break;
6076 : :
10108 bruce@momjian.us 6077 :UBC 0 : default:
8318 tgl@sss.pgh.pa.us 6078 [ # # ]: 0 : elog(ERROR, "unrecognized query command type: %d",
6079 : : query->commandType);
6080 : : break;
6081 : : }
10116 bruce@momjian.us 6082 :CBC 3498 : }
6083 : :
6084 : : /* ----------
6085 : : * get_values_def - Parse back a VALUES list
6086 : : * ----------
6087 : : */
6088 : : static void
7216 mail@joeconway.com 6089 : 193 : get_values_def(List *values_lists, deparse_context *context)
6090 : : {
6091 : 193 : StringInfo buf = context->buf;
6092 : 193 : bool first_list = true;
6093 : : ListCell *vtl;
6094 : :
6095 : 193 : appendStringInfoString(buf, "VALUES ");
6096 : :
6097 [ + - + + : 581 : foreach(vtl, values_lists)
+ + ]
6098 : : {
6099 : 388 : List *sublist = (List *) lfirst(vtl);
6100 : 388 : bool first_col = true;
6101 : : ListCell *lc;
6102 : :
6103 [ + + ]: 388 : if (first_list)
6104 : 193 : first_list = false;
6105 : : else
6106 : 195 : appendStringInfoString(buf, ", ");
6107 : :
6108 : 388 : appendStringInfoChar(buf, '(');
6109 [ + - + + : 1380 : foreach(lc, sublist)
+ + ]
6110 : : {
7153 bruce@momjian.us 6111 : 992 : Node *col = (Node *) lfirst(lc);
6112 : :
7216 mail@joeconway.com 6113 [ + + ]: 992 : if (first_col)
6114 : 388 : first_col = false;
6115 : : else
6116 : 604 : appendStringInfoChar(buf, ',');
6117 : :
6118 : : /*
6119 : : * Print the value. Whole-row Vars need special treatment.
6120 : : */
3562 tgl@sss.pgh.pa.us 6121 : 992 : get_rule_expr_toplevel(col, context, false);
6122 : : }
7216 mail@joeconway.com 6123 : 388 : appendStringInfoChar(buf, ')');
6124 : : }
6125 : 193 : }
6126 : :
6127 : : /* ----------
6128 : : * get_with_clause - Parse back a WITH clause
6129 : : * ----------
6130 : : */
6131 : : static void
6422 tgl@sss.pgh.pa.us 6132 : 3467 : get_with_clause(Query *query, deparse_context *context)
6133 : : {
6134 : 3467 : StringInfo buf = context->buf;
6135 : : const char *sep;
6136 : : ListCell *l;
6137 : :
6138 [ + + ]: 3467 : if (query->cteList == NIL)
6139 : 3404 : return;
6140 : :
6141 [ + - ]: 63 : if (PRETTY_INDENT(context))
6142 : : {
6143 : 63 : context->indentLevel += PRETTYINDENT_STD;
6144 : 63 : appendStringInfoChar(buf, ' ');
6145 : : }
6146 : :
6147 [ + + ]: 63 : if (query->hasRecursive)
6148 : 34 : sep = "WITH RECURSIVE ";
6149 : : else
6150 : 29 : sep = "WITH ";
6151 [ + - + + : 156 : foreach(l, query->cteList)
+ + ]
6152 : : {
6153 : 93 : CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
6154 : :
6155 : 93 : appendStringInfoString(buf, sep);
6156 : 93 : appendStringInfoString(buf, quote_identifier(cte->ctename));
6157 [ + + ]: 93 : if (cte->aliascolnames)
6158 : : {
6159 : 38 : bool first = true;
6160 : : ListCell *col;
6161 : :
6162 : 38 : appendStringInfoChar(buf, '(');
6163 [ + - + + : 100 : foreach(col, cte->aliascolnames)
+ + ]
6164 : : {
6165 [ + + ]: 62 : if (first)
6166 : 38 : first = false;
6167 : : else
6168 : 24 : appendStringInfoString(buf, ", ");
6169 : 62 : appendStringInfoString(buf,
6170 : 62 : quote_identifier(strVal(lfirst(col))));
6171 : : }
6172 : 38 : appendStringInfoChar(buf, ')');
6173 : : }
2635 6174 : 93 : appendStringInfoString(buf, " AS ");
6175 [ + + - - ]: 93 : switch (cte->ctematerialized)
6176 : : {
6177 : 81 : case CTEMaterializeDefault:
6178 : 81 : break;
6179 : 12 : case CTEMaterializeAlways:
6180 : 12 : appendStringInfoString(buf, "MATERIALIZED ");
6181 : 12 : break;
2635 tgl@sss.pgh.pa.us 6182 :UBC 0 : case CTEMaterializeNever:
6183 : 0 : appendStringInfoString(buf, "NOT MATERIALIZED ");
6184 : 0 : break;
6185 : : }
2635 tgl@sss.pgh.pa.us 6186 :CBC 93 : appendStringInfoChar(buf, '(');
6422 6187 [ + - ]: 93 : if (PRETTY_INDENT(context))
6188 : 93 : appendContextKeyword(context, "", 0, 0, 0);
6189 : 93 : get_query_def((Query *) cte->ctequery, buf, context->namespaces, NULL,
6190 : : true,
6191 : : context->prettyFlags, context->wrapColumn,
6192 : : context->indentLevel);
6193 [ + - ]: 93 : if (PRETTY_INDENT(context))
6194 : 93 : appendContextKeyword(context, "", 0, 0, 0);
6195 : 93 : appendStringInfoChar(buf, ')');
6196 : :
1919 peter@eisentraut.org 6197 [ + + ]: 93 : if (cte->search_clause)
6198 : : {
6199 : 4 : bool first = true;
6200 : : ListCell *lc;
6201 : :
6202 : 4 : appendStringInfo(buf, " SEARCH %s FIRST BY ",
6203 [ - + ]: 4 : cte->search_clause->search_breadth_first ? "BREADTH" : "DEPTH");
6204 : :
6205 [ + - + + : 12 : foreach(lc, cte->search_clause->search_col_list)
+ + ]
6206 : : {
6207 [ + + ]: 8 : if (first)
6208 : 4 : first = false;
6209 : : else
6210 : 4 : appendStringInfoString(buf, ", ");
6211 : 8 : appendStringInfoString(buf,
6212 : 8 : quote_identifier(strVal(lfirst(lc))));
6213 : : }
6214 : :
6215 : 4 : appendStringInfo(buf, " SET %s", quote_identifier(cte->search_clause->search_seq_column));
6216 : : }
6217 : :
6218 [ + + ]: 93 : if (cte->cycle_clause)
6219 : : {
6220 : 8 : bool first = true;
6221 : : ListCell *lc;
6222 : :
6223 : 8 : appendStringInfoString(buf, " CYCLE ");
6224 : :
6225 [ + - + + : 24 : foreach(lc, cte->cycle_clause->cycle_col_list)
+ + ]
6226 : : {
6227 [ + + ]: 16 : if (first)
6228 : 8 : first = false;
6229 : : else
6230 : 8 : appendStringInfoString(buf, ", ");
6231 : 16 : appendStringInfoString(buf,
6232 : 16 : quote_identifier(strVal(lfirst(lc))));
6233 : : }
6234 : :
6235 : 8 : appendStringInfo(buf, " SET %s", quote_identifier(cte->cycle_clause->cycle_mark_column));
6236 : :
6237 : : {
1893 6238 : 8 : Const *cmv = castNode(Const, cte->cycle_clause->cycle_mark_value);
6239 : 8 : Const *cmd = castNode(Const, cte->cycle_clause->cycle_mark_default);
6240 : :
6241 [ + + + - : 12 : if (!(cmv->consttype == BOOLOID && !cmv->constisnull && DatumGetBool(cmv->constvalue) == true &&
+ - - + ]
6242 [ + - + - ]: 4 : cmd->consttype == BOOLOID && !cmd->constisnull && DatumGetBool(cmd->constvalue) == false))
6243 : : {
6244 : 4 : appendStringInfoString(buf, " TO ");
6245 : 4 : get_rule_expr(cte->cycle_clause->cycle_mark_value, context, false);
6246 : 4 : appendStringInfoString(buf, " DEFAULT ");
6247 : 4 : get_rule_expr(cte->cycle_clause->cycle_mark_default, context, false);
6248 : : }
6249 : : }
6250 : :
1919 6251 : 8 : appendStringInfo(buf, " USING %s", quote_identifier(cte->cycle_clause->cycle_path_column));
6252 : : }
6253 : :
6422 tgl@sss.pgh.pa.us 6254 : 93 : sep = ", ";
6255 : : }
6256 : :
6257 [ + - ]: 63 : if (PRETTY_INDENT(context))
6258 : : {
6259 : 63 : context->indentLevel -= PRETTYINDENT_STD;
6260 : 63 : appendContextKeyword(context, "", 0, 0, 0);
6261 : : }
6262 : : else
6422 tgl@sss.pgh.pa.us 6263 :UBC 0 : appendStringInfoChar(buf, ' ');
6264 : : }
6265 : :
6266 : : /* ----------
6267 : : * get_select_query_def - Parse back a SELECT parsetree
6268 : : * ----------
6269 : : */
6270 : : static void
614 tgl@sss.pgh.pa.us 6271 :CBC 3124 : get_select_query_def(Query *query, deparse_context *context)
6272 : : {
9343 6273 : 3124 : StringInfo buf = context->buf;
6274 : : bool force_colno;
6275 : : ListCell *l;
6276 : :
6277 : : /* Insert the WITH clause if given */
6422 6278 : 3124 : get_with_clause(query, context);
6279 : :
6280 : : /* Subroutines may need to consult the SELECT targetlist and windowClause */
614 6281 : 3124 : context->targetList = query->targetList;
6337 6282 : 3124 : context->windowClause = query->windowClause;
6283 : :
6284 : : /*
6285 : : * If the Query node has a setOperations tree, then it's the top level of
6286 : : * a UNION/INTERSECT/EXCEPT query; only the WITH, ORDER BY and LIMIT
6287 : : * fields are interesting in the top query itself.
6288 : : */
9343 6289 [ + + ]: 3124 : if (query->setOperations)
6290 : : {
614 6291 : 103 : get_setop_query(query->setOperations, query, context);
6292 : : /* ORDER BY clauses must be simple in this case */
9151 6293 : 103 : force_colno = true;
6294 : : }
6295 : : else
6296 : : {
614 6297 : 3021 : get_basic_select_query(query, context);
9151 6298 : 3021 : force_colno = false;
6299 : : }
6300 : :
6301 : : /* Add the ORDER BY clause if given */
9343 6302 [ + + ]: 3124 : if (query->sortClause != NIL)
6303 : : {
8310 bruce@momjian.us 6304 : 128 : appendContextKeyword(context, " ORDER BY ",
6305 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
6337 tgl@sss.pgh.pa.us 6306 : 128 : get_rule_orderby(query->sortClause, query->targetList,
6307 : : force_colno, context);
6308 : : }
6309 : :
6310 : : /*
6311 : : * Add the LIMIT/OFFSET clauses if given. If non-default options, use the
6312 : : * standard spelling of LIMIT.
6313 : : */
9322 6314 [ + + ]: 3124 : if (query->limitOffset != NULL)
6315 : : {
8315 6316 : 18 : appendContextKeyword(context, " OFFSET ",
6317 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
8629 6318 : 18 : get_rule_expr(query->limitOffset, context, false);
6319 : : }
9322 6320 [ + + ]: 3124 : if (query->limitCount != NULL)
6321 : : {
2219 alvherre@alvh.no-ip. 6322 [ + + ]: 53 : if (query->limitOption == LIMIT_OPTION_WITH_TIES)
6323 : : {
6324 : : /*
6325 : : * The limitCount arg is a c_expr, so it needs parens. Simple
6326 : : * literals and function expressions would not need parens, but
6327 : : * unfortunately it's hard to tell if the expression will be
6328 : : * printed as a simple literal like 123 or as a typecast
6329 : : * expression, like '-123'::int4. The grammar accepts the former
6330 : : * without quoting, but not the latter.
6331 : : */
6332 : 27 : appendContextKeyword(context, " FETCH FIRST ",
6333 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
351 heikki.linnakangas@i 6334 : 27 : appendStringInfoChar(buf, '(');
8629 tgl@sss.pgh.pa.us 6335 : 27 : get_rule_expr(query->limitCount, context, false);
351 heikki.linnakangas@i 6336 : 27 : appendStringInfoChar(buf, ')');
2028 drowley@postgresql.o 6337 : 27 : appendStringInfoString(buf, " ROWS WITH TIES");
6338 : : }
6339 : : else
6340 : : {
2219 alvherre@alvh.no-ip. 6341 : 26 : appendContextKeyword(context, " LIMIT ",
6342 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
6343 [ + + ]: 26 : if (IsA(query->limitCount, Const) &&
6344 [ + - ]: 9 : ((Const *) query->limitCount)->constisnull)
6345 : 9 : appendStringInfoString(buf, "ALL");
6346 : : else
6347 : 17 : get_rule_expr(query->limitCount, context, false);
6348 : : }
6349 : : }
6350 : :
6351 : : /* Add FOR [KEY] UPDATE/SHARE clauses if present */
6033 tgl@sss.pgh.pa.us 6352 [ + + ]: 3124 : if (query->hasForUpdate)
6353 : : {
6354 [ + - + + : 8 : foreach(l, query->rowMarks)
+ + ]
6355 : : {
6356 : 4 : RowMarkClause *rc = (RowMarkClause *) lfirst(l);
6357 : :
6358 : : /* don't print implicit clauses */
6359 [ - + ]: 4 : if (rc->pushedDown)
6033 tgl@sss.pgh.pa.us 6360 :UBC 0 : continue;
6361 : :
82 dean.a.rasheed@gmail 6362 :GNC 4 : appendContextKeyword(context,
6363 : 4 : get_lock_clause_strength(rc->strength),
6364 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
6365 : :
6033 tgl@sss.pgh.pa.us 6366 :CBC 4 : appendStringInfo(buf, " OF %s",
4974 6367 : 4 : quote_identifier(get_rtable_name(rc->rti,
6368 : : context)));
4228 alvherre@alvh.no-ip. 6369 [ - + ]: 4 : if (rc->waitPolicy == LockWaitError)
4569 rhaas@postgresql.org 6370 :UBC 0 : appendStringInfoString(buf, " NOWAIT");
4228 alvherre@alvh.no-ip. 6371 [ - + ]:CBC 4 : else if (rc->waitPolicy == LockWaitSkip)
4228 alvherre@alvh.no-ip. 6372 :UBC 0 : appendStringInfoString(buf, " SKIP LOCKED");
6373 : : }
6374 : : }
9343 tgl@sss.pgh.pa.us 6375 :CBC 3124 : }
6376 : :
6377 : : static char *
82 dean.a.rasheed@gmail 6378 :GNC 8 : get_lock_clause_strength(LockClauseStrength strength)
6379 : : {
6380 [ - - - - : 8 : switch (strength)
+ - ]
6381 : : {
82 dean.a.rasheed@gmail 6382 :UNC 0 : case LCS_NONE:
6383 : : /* we intentionally throw an error for LCS_NONE */
6384 [ # # ]: 0 : elog(ERROR, "unrecognized LockClauseStrength %d",
6385 : : (int) strength);
6386 : : break;
6387 : 0 : case LCS_FORKEYSHARE:
6388 : 0 : return " FOR KEY SHARE";
6389 : 0 : case LCS_FORSHARE:
6390 : 0 : return " FOR SHARE";
6391 : 0 : case LCS_FORNOKEYUPDATE:
6392 : 0 : return " FOR NO KEY UPDATE";
82 dean.a.rasheed@gmail 6393 :GNC 8 : case LCS_FORUPDATE:
6394 : 8 : return " FOR UPDATE";
6395 : : }
82 dean.a.rasheed@gmail 6396 :UNC 0 : return NULL; /* keep compiler quiet */
6397 : : }
6398 : :
6399 : : /*
6400 : : * Detect whether query looks like SELECT ... FROM VALUES(),
6401 : : * with no need to rename the output columns of the VALUES RTE.
6402 : : * If so, return the VALUES RTE. Otherwise return NULL.
6403 : : */
6404 : : static RangeTblEntry *
2362 tgl@sss.pgh.pa.us 6405 :CBC 3021 : get_simple_values_rte(Query *query, TupleDesc resultDesc)
6406 : : {
5007 6407 : 3021 : RangeTblEntry *result = NULL;
6408 : : ListCell *lc;
6409 : :
6410 : : /*
6411 : : * We want to detect a match even if the Query also contains OLD or NEW
6412 : : * rule RTEs. So the idea is to scan the rtable and see if there is only
6413 : : * one inFromCl RTE that is a VALUES RTE.
6414 : : */
6415 [ + + + + : 3272 : foreach(lc, query->rtable)
+ + ]
6416 : : {
6417 : 2755 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
6418 : :
6419 [ + + + - ]: 2755 : if (rte->rtekind == RTE_VALUES && rte->inFromCl)
6420 : : {
6421 [ - + ]: 167 : if (result)
6422 : 2504 : return NULL; /* multiple VALUES (probably not possible) */
6423 : 167 : result = rte;
6424 : : }
6425 [ + + + + ]: 2588 : else if (rte->rtekind == RTE_RELATION && !rte->inFromCl)
6426 : 84 : continue; /* ignore rule entries */
6427 : : else
6428 : 2504 : return NULL; /* something else -> not simple VALUES */
6429 : : }
6430 : :
6431 : : /*
6432 : : * We don't need to check the targetlist in any great detail, because
6433 : : * parser/analyze.c will never generate a "bare" VALUES RTE --- they only
6434 : : * appear inside auto-generated sub-queries with very restricted
6435 : : * structure. However, DefineView might have modified the tlist by
6436 : : * injecting new column aliases, or we might have some other column
6437 : : * aliases forced by a resultDesc. We can only simplify if the RTE's
6438 : : * column names match the names that get_target_list() would select.
6439 : : */
4087 6440 [ + + ]: 517 : if (result)
6441 : : {
6442 : : ListCell *lcn;
6443 : : int colno;
6444 : :
6445 [ - + ]: 167 : if (list_length(query->targetList) != list_length(result->eref->colnames))
4087 tgl@sss.pgh.pa.us 6446 :UBC 0 : return NULL; /* this probably cannot happen */
2362 tgl@sss.pgh.pa.us 6447 :CBC 167 : colno = 0;
4087 6448 [ + - + + : 588 : forboth(lc, query->targetList, lcn, result->eref->colnames)
+ - + + +
+ + - +
+ ]
6449 : : {
6450 : 429 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
6451 : 429 : char *cname = strVal(lfirst(lcn));
6452 : : char *colname;
6453 : :
6454 [ - + ]: 429 : if (tle->resjunk)
6455 : 8 : return NULL; /* this probably cannot happen */
6456 : :
6457 : : /* compute name that get_target_list would use for column */
2362 6458 : 429 : colno++;
6459 [ + + + - ]: 429 : if (resultDesc && colno <= resultDesc->natts)
6460 : 20 : colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
6461 : : else
6462 : 409 : colname = tle->resname;
6463 : :
6464 : : /* does it match the VALUES RTE? */
6465 [ + - + + ]: 429 : if (colname == NULL || strcmp(colname, cname) != 0)
4087 6466 : 8 : return NULL; /* column name has been changed */
6467 : : }
6468 : : }
6469 : :
5007 6470 : 509 : return result;
6471 : : }
6472 : :
6473 : : static void
614 6474 : 3021 : get_basic_select_query(Query *query, deparse_context *context)
6475 : : {
9711 6476 : 3021 : StringInfo buf = context->buf;
6477 : : RangeTblEntry *values_rte;
6478 : : char *sep;
6479 : : ListCell *l;
6480 : :
8315 6481 [ + + ]: 3021 : if (PRETTY_INDENT(context))
6482 : : {
8310 bruce@momjian.us 6483 : 2994 : context->indentLevel += PRETTYINDENT_STD;
6484 : 2994 : appendStringInfoChar(buf, ' ');
6485 : : }
6486 : :
6487 : : /*
6488 : : * If the query looks like SELECT * FROM (VALUES ...), then print just the
6489 : : * VALUES part. This reverses what transformValuesClause() did at parse
6490 : : * time.
6491 : : */
614 tgl@sss.pgh.pa.us 6492 : 3021 : values_rte = get_simple_values_rte(query, context->resultDesc);
5007 6493 [ + + ]: 3021 : if (values_rte)
6494 : : {
6495 : 159 : get_values_def(values_rte->values_lists, context);
6496 : 159 : return;
6497 : : }
6498 : :
6499 : : /*
6500 : : * Build up the query string - first we say SELECT
6501 : : */
1854 peter@eisentraut.org 6502 [ + + ]: 2862 : if (query->isReturn)
6503 : 31 : appendStringInfoString(buf, "RETURN");
6504 : : else
6505 : 2831 : appendStringInfoString(buf, "SELECT");
6506 : :
6507 : : /* Add the DISTINCT clause if given */
9343 tgl@sss.pgh.pa.us 6508 [ - + ]: 2862 : if (query->distinctClause != NIL)
6509 : : {
6485 tgl@sss.pgh.pa.us 6510 [ # # ]:UBC 0 : if (query->hasDistinctOn)
6511 : : {
4569 rhaas@postgresql.org 6512 : 0 : appendStringInfoString(buf, " DISTINCT ON (");
9343 tgl@sss.pgh.pa.us 6513 : 0 : sep = "";
6514 [ # # # # : 0 : foreach(l, query->distinctClause)
# # ]
6515 : : {
6485 6516 : 0 : SortGroupClause *srt = (SortGroupClause *) lfirst(l);
6517 : :
7675 neilc@samurai.com 6518 : 0 : appendStringInfoString(buf, sep);
4007 andres@anarazel.de 6519 : 0 : get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList,
6520 : : false, context);
9343 tgl@sss.pgh.pa.us 6521 : 0 : sep = ", ";
6522 : : }
4569 rhaas@postgresql.org 6523 : 0 : appendStringInfoChar(buf, ')');
6524 : : }
6525 : : else
6526 : 0 : appendStringInfoString(buf, " DISTINCT");
6527 : : }
6528 : :
6529 : : /* Then we tell what to select (the targetlist) */
614 tgl@sss.pgh.pa.us 6530 :CBC 2862 : get_target_list(query->targetList, context);
6531 : :
6532 : : /* Add the FROM clause if needed */
7206 6533 : 2862 : get_from_clause(query, " FROM ", context);
6534 : :
6535 : : /* Add the WHERE clause if given */
6536 [ + + ]: 2862 : if (query->jointree->quals != NULL)
6537 : : {
6538 : 903 : appendContextKeyword(context, " WHERE ",
6539 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
6540 : 903 : get_rule_expr(query->jointree->quals, context, false);
6541 : : }
6542 : :
6543 : : /* Add the GROUP BY clause if given */
4007 andres@anarazel.de 6544 [ + + - + ]: 2862 : if (query->groupClause != NULL || query->groupingSets != NULL)
6545 : : {
6546 : : bool save_ingroupby;
6547 : :
7206 tgl@sss.pgh.pa.us 6548 : 119 : appendContextKeyword(context, " GROUP BY ",
6549 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
1874 tomas.vondra@postgre 6550 [ - + ]: 119 : if (query->groupDistinct)
1874 tomas.vondra@postgre 6551 :UBC 0 : appendStringInfoString(buf, "DISTINCT ");
6552 : :
614 tgl@sss.pgh.pa.us 6553 :CBC 119 : save_ingroupby = context->inGroupBy;
6554 : 119 : context->inGroupBy = true;
6555 : :
218 tgl@sss.pgh.pa.us 6556 [ + + ]:GNC 119 : if (query->groupByAll)
6557 : 4 : appendStringInfoString(buf, "ALL");
6558 [ + + ]: 115 : else if (query->groupingSets == NIL)
6559 : : {
4007 andres@anarazel.de 6560 :CBC 111 : sep = "";
6561 [ + - + + : 255 : foreach(l, query->groupClause)
+ + ]
6562 : : {
6563 : 144 : SortGroupClause *grp = (SortGroupClause *) lfirst(l);
6564 : :
6565 : 144 : appendStringInfoString(buf, sep);
6566 : 144 : get_rule_sortgroupclause(grp->tleSortGroupRef, query->targetList,
6567 : : false, context);
6568 : 144 : sep = ", ";
6569 : : }
6570 : : }
6571 : : else
6572 : : {
6573 : 4 : sep = "";
6574 [ + - + + : 8 : foreach(l, query->groupingSets)
+ + ]
6575 : : {
6576 : 4 : GroupingSet *grp = lfirst(l);
6577 : :
6578 : 4 : appendStringInfoString(buf, sep);
6579 : 4 : get_rule_groupingset(grp, query->targetList, true, context);
6580 : 4 : sep = ", ";
6581 : : }
6582 : : }
6583 : :
614 tgl@sss.pgh.pa.us 6584 : 119 : context->inGroupBy = save_ingroupby;
6585 : : }
6586 : :
6587 : : /* Add the HAVING clause if given */
7206 6588 [ + + ]: 2862 : if (query->havingQual != NULL)
6589 : : {
6590 : 5 : appendContextKeyword(context, " HAVING ",
6591 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
6592 : 5 : get_rule_expr(query->havingQual, context, false);
6593 : : }
6594 : :
6595 : : /* Add the WINDOW clause if needed */
6337 6596 [ + + ]: 2862 : if (query->windowClause != NIL)
6597 : 32 : get_rule_windowclause(query, context);
6598 : : }
6599 : :
6600 : : /* ----------
6601 : : * get_target_list - Parse back a SELECT target list
6602 : : *
6603 : : * This is also used for RETURNING lists in INSERT/UPDATE/DELETE/MERGE.
6604 : : * ----------
6605 : : */
6606 : : static void
614 6607 : 2971 : get_target_list(List *targetList, deparse_context *context)
6608 : : {
7206 6609 : 2971 : StringInfo buf = context->buf;
6610 : : StringInfoData targetbuf;
4880 6611 : 2971 : bool last_was_multiline = false;
6612 : : char *sep;
6613 : : int colno;
6614 : : ListCell *l;
6615 : :
6616 : : /* we use targetbuf to hold each TLE's text temporarily */
6617 : 2971 : initStringInfo(&targetbuf);
6618 : :
10116 bruce@momjian.us 6619 : 2971 : sep = " ";
8671 tgl@sss.pgh.pa.us 6620 : 2971 : colno = 0;
7206 6621 [ + - + + : 15716 : foreach(l, targetList)
+ + ]
6622 : : {
9366 6623 : 12745 : TargetEntry *tle = (TargetEntry *) lfirst(l);
6624 : : char *colname;
6625 : : char *attname;
6626 : :
7699 6627 [ + + ]: 12745 : if (tle->resjunk)
9349 6628 : 25 : continue; /* ignore junk entries */
6629 : :
7675 neilc@samurai.com 6630 : 12720 : appendStringInfoString(buf, sep);
10108 bruce@momjian.us 6631 : 12720 : sep = ", ";
8671 tgl@sss.pgh.pa.us 6632 : 12720 : colno++;
6633 : :
6634 : : /*
6635 : : * Put the new field text into targetbuf so we can decide after we've
6636 : : * got it whether or not it needs to go on a new line.
6637 : : */
4880 6638 : 12720 : resetStringInfo(&targetbuf);
5189 andrew@dunslane.net 6639 : 12720 : context->buf = &targetbuf;
6640 : :
6641 : : /*
6642 : : * We special-case Var nodes rather than using get_rule_expr. This is
6643 : : * needed because get_rule_expr will display a whole-row Var as
6644 : : * "foo.*", which is the preferred notation in most contexts, but at
6645 : : * the top level of a SELECT list it's not right (the parser will
6646 : : * expand that notation into multiple columns, yielding behavior
6647 : : * different from a whole-row Var). We need to call get_variable
6648 : : * directly so that we can tell it to do the right thing, and so that
6649 : : * we can get the attribute name which is the default AS label.
6650 : : */
4007 andres@anarazel.de 6651 [ + - + + ]: 12720 : if (tle->expr && (IsA(tle->expr, Var)))
6652 : : {
5121 tgl@sss.pgh.pa.us 6653 : 9791 : attname = get_variable((Var *) tle->expr, 0, true, context);
6654 : : }
6655 : : else
6656 : : {
7404 6657 : 2929 : get_rule_expr((Node *) tle->expr, context, true);
6658 : :
6659 : : /*
6660 : : * When colNamesVisible is true, we should always show the
6661 : : * assigned column name explicitly. Otherwise, show it only if
6662 : : * it's not FigureColname's fallback.
6663 : : */
614 6664 [ + + ]: 2929 : attname = context->colNamesVisible ? NULL : "?column?";
6665 : : }
6666 : :
6667 : : /*
6668 : : * Figure out what the result column should be called. In the context
6669 : : * of a view, use the view's tuple descriptor (so as to pick up the
6670 : : * effects of any column RENAME that's been done on the view).
6671 : : * Otherwise, just use what we can find in the TLE.
6672 : : */
6673 [ + + + - ]: 12720 : if (context->resultDesc && colno <= context->resultDesc->natts)
6674 : 11557 : colname = NameStr(TupleDescAttr(context->resultDesc,
6675 : : colno - 1)->attname);
6676 : : else
7699 6677 : 1163 : colname = tle->resname;
6678 : :
6679 : : /* Show AS unless the column's name is correct as-is */
8303 6680 [ + + ]: 12720 : if (colname) /* resname could be NULL */
6681 : : {
7404 6682 [ + + + + ]: 12689 : if (attname == NULL || strcmp(attname, colname) != 0)
5189 andrew@dunslane.net 6683 : 4159 : appendStringInfo(&targetbuf, " AS %s", quote_identifier(colname));
6684 : : }
6685 : :
6686 : : /* Restore context's output buffer */
6687 : 12720 : context->buf = buf;
6688 : :
6689 : : /* Consider line-wrapping if enabled */
4880 tgl@sss.pgh.pa.us 6690 [ + + + - ]: 12720 : if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
6691 : : {
6692 : : int leading_nl_pos;
6693 : :
6694 : : /* Does the new field start with a new line? */
4558 6695 [ + - + + ]: 12693 : if (targetbuf.len > 0 && targetbuf.data[0] == '\n')
6696 : 368 : leading_nl_pos = 0;
6697 : : else
6698 : 12325 : leading_nl_pos = -1;
6699 : :
6700 : : /* If so, we shouldn't add anything */
6701 [ + + ]: 12693 : if (leading_nl_pos >= 0)
6702 : : {
6703 : : /* instead, remove any trailing spaces currently in buf */
6704 : 368 : removeStringInfoSpaces(buf);
6705 : : }
6706 : : else
6707 : : {
6708 : : char *trailing_nl;
6709 : :
6710 : : /* Locate the start of the current line in the output buffer */
6711 : 12325 : trailing_nl = strrchr(buf->data, '\n');
6712 [ + + ]: 12325 : if (trailing_nl == NULL)
6713 : 3651 : trailing_nl = buf->data;
6714 : : else
6715 : 8674 : trailing_nl++;
6716 : :
6717 : : /*
6718 : : * Add a newline, plus some indentation, if the new field is
6719 : : * not the first and either the new field would cause an
6720 : : * overflow or the last field used more than one line.
6721 : : */
6722 [ + + ]: 12325 : if (colno > 1 &&
6723 [ - + - - ]: 9390 : ((strlen(trailing_nl) + targetbuf.len > context->wrapColumn) ||
6724 : : last_was_multiline))
6725 : 9390 : appendContextKeyword(context, "", -PRETTYINDENT_STD,
6726 : : PRETTYINDENT_STD, PRETTYINDENT_VAR);
6727 : : }
6728 : :
6729 : : /* Remember this field's multiline status for next iteration */
4880 6730 : 12693 : last_was_multiline =
6731 : 12693 : (strchr(targetbuf.data + leading_nl_pos + 1, '\n') != NULL);
6732 : : }
6733 : :
6734 : : /* Add the new field */
2478 drowley@postgresql.o 6735 : 12720 : appendBinaryStringInfo(buf, targetbuf.data, targetbuf.len);
6736 : : }
6737 : :
6738 : : /* clean up */
4880 tgl@sss.pgh.pa.us 6739 : 2971 : pfree(targetbuf.data);
9343 6740 : 2971 : }
6741 : :
6742 : : static void
474 dean.a.rasheed@gmail 6743 : 109 : get_returning_clause(Query *query, deparse_context *context)
6744 : : {
6745 : 109 : StringInfo buf = context->buf;
6746 : :
6747 [ + - ]: 109 : if (query->returningList)
6748 : : {
6749 : 109 : bool have_with = false;
6750 : :
6751 : 109 : appendContextKeyword(context, " RETURNING",
6752 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
6753 : :
6754 : : /* Add WITH (OLD/NEW) options, if they're not the defaults */
6755 [ + + + + ]: 109 : if (query->returningOldAlias && strcmp(query->returningOldAlias, "old") != 0)
6756 : : {
6757 : 12 : appendStringInfo(buf, " WITH (OLD AS %s",
6758 : 12 : quote_identifier(query->returningOldAlias));
6759 : 12 : have_with = true;
6760 : : }
6761 [ + + + + ]: 109 : if (query->returningNewAlias && strcmp(query->returningNewAlias, "new") != 0)
6762 : : {
6763 [ + + ]: 12 : if (have_with)
6764 : 8 : appendStringInfo(buf, ", NEW AS %s",
6765 : 8 : quote_identifier(query->returningNewAlias));
6766 : : else
6767 : : {
6768 : 4 : appendStringInfo(buf, " WITH (NEW AS %s",
6769 : 4 : quote_identifier(query->returningNewAlias));
6770 : 4 : have_with = true;
6771 : : }
6772 : : }
6773 [ + + ]: 109 : if (have_with)
6774 : 16 : appendStringInfoChar(buf, ')');
6775 : :
6776 : : /* Add the returning expressions themselves */
6777 : 109 : get_target_list(query->returningList, context);
6778 : : }
6779 : 109 : }
6780 : :
6781 : : static void
614 tgl@sss.pgh.pa.us 6782 : 463 : get_setop_query(Node *setOp, Query *query, deparse_context *context)
6783 : : {
9343 6784 : 463 : StringInfo buf = context->buf;
6785 : : bool need_paren;
6786 : :
6787 : : /* Guard against excessively long or deeply-nested queries */
4388 6788 [ - + ]: 463 : CHECK_FOR_INTERRUPTS();
6789 : 463 : check_stack_depth();
6790 : :
9343 6791 [ + + ]: 463 : if (IsA(setOp, RangeTblRef))
6792 : : {
6793 : 283 : RangeTblRef *rtr = (RangeTblRef *) setOp;
6794 : 283 : RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable);
9175 bruce@momjian.us 6795 : 283 : Query *subquery = rte->subquery;
6796 : :
9343 tgl@sss.pgh.pa.us 6797 [ - + ]: 283 : Assert(subquery != NULL);
6798 : :
6799 : : /*
6800 : : * We need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y.
6801 : : * Also add parens if the leaf query contains its own set operations.
6802 : : * (That shouldn't happen unless one of the other clauses is also
6803 : : * present, see transformSetOperationTree; but let's be safe.)
6804 : : */
6422 6805 : 849 : need_paren = (subquery->cteList ||
6806 [ + - ]: 283 : subquery->sortClause ||
7973 6807 [ + - ]: 283 : subquery->rowMarks ||
6808 [ + - ]: 283 : subquery->limitOffset ||
531 6809 [ + - + - ]: 849 : subquery->limitCount ||
6810 [ - + ]: 283 : subquery->setOperations);
7973 6811 [ - + ]: 283 : if (need_paren)
7973 tgl@sss.pgh.pa.us 6812 :UBC 0 : appendStringInfoChar(buf, '(');
614 tgl@sss.pgh.pa.us 6813 :CBC 283 : get_query_def(subquery, buf, context->namespaces,
6814 : 283 : context->resultDesc, context->colNamesVisible,
6815 : : context->prettyFlags, context->wrapColumn,
6816 : : context->indentLevel);
7973 6817 [ - + ]: 283 : if (need_paren)
7973 tgl@sss.pgh.pa.us 6818 :UBC 0 : appendStringInfoChar(buf, ')');
6819 : : }
9343 tgl@sss.pgh.pa.us 6820 [ + - ]:CBC 180 : else if (IsA(setOp, SetOperationStmt))
6821 : : {
6822 : 180 : SetOperationStmt *op = (SetOperationStmt *) setOp;
6823 : : int subindent;
6824 : : bool save_colnamesvisible;
6825 : :
6826 : : /*
6827 : : * We force parens when nesting two SetOperationStmts, except when the
6828 : : * lefthand input is another setop of the same kind. Syntactically,
6829 : : * we could omit parens in rather more cases, but it seems best to use
6830 : : * parens to flag cases where the setop operator changes. If we use
6831 : : * parens, we also increase the indentation level for the child query.
6832 : : *
6833 : : * There are some cases in which parens are needed around a leaf query
6834 : : * too, but those are more easily handled at the next level down (see
6835 : : * code above).
6836 : : */
4388 6837 [ + + ]: 180 : if (IsA(op->larg, SetOperationStmt))
6838 : : {
6839 : 77 : SetOperationStmt *lop = (SetOperationStmt *) op->larg;
6840 : :
6841 [ + - + - ]: 77 : if (op->op == lop->op && op->all == lop->all)
6842 : 77 : need_paren = false;
6843 : : else
4388 tgl@sss.pgh.pa.us 6844 :UBC 0 : need_paren = true;
6845 : : }
6846 : : else
4388 tgl@sss.pgh.pa.us 6847 :CBC 103 : need_paren = false;
6848 : :
7973 6849 [ - + ]: 180 : if (need_paren)
6850 : : {
7973 tgl@sss.pgh.pa.us 6851 :UBC 0 : appendStringInfoChar(buf, '(');
4388 6852 : 0 : subindent = PRETTYINDENT_STD;
6853 : 0 : appendContextKeyword(context, "", subindent, 0, 0);
6854 : : }
6855 : : else
4388 tgl@sss.pgh.pa.us 6856 :CBC 180 : subindent = 0;
6857 : :
614 6858 : 180 : get_setop_query(op->larg, query, context);
6859 : :
4388 6860 [ - + ]: 180 : if (need_paren)
4388 tgl@sss.pgh.pa.us 6861 :UBC 0 : appendContextKeyword(context, ") ", -subindent, 0, 0);
4388 tgl@sss.pgh.pa.us 6862 [ + - ]:CBC 180 : else if (PRETTY_INDENT(context))
6863 : 180 : appendContextKeyword(context, "", -subindent, 0, 0);
6864 : : else
8310 bruce@momjian.us 6865 :UBC 0 : appendStringInfoChar(buf, ' ');
6866 : :
9343 tgl@sss.pgh.pa.us 6867 [ + - - - ]:CBC 180 : switch (op->op)
6868 : : {
6869 : 180 : case SETOP_UNION:
4388 6870 : 180 : appendStringInfoString(buf, "UNION ");
9343 6871 : 180 : break;
9343 tgl@sss.pgh.pa.us 6872 :UBC 0 : case SETOP_INTERSECT:
4388 6873 : 0 : appendStringInfoString(buf, "INTERSECT ");
9343 6874 : 0 : break;
6875 : 0 : case SETOP_EXCEPT:
4388 6876 : 0 : appendStringInfoString(buf, "EXCEPT ");
9343 6877 : 0 : break;
6878 : 0 : default:
8318 6879 [ # # ]: 0 : elog(ERROR, "unrecognized set op: %d",
6880 : : (int) op->op);
6881 : : }
9343 tgl@sss.pgh.pa.us 6882 [ + + ]:CBC 180 : if (op->all)
4569 rhaas@postgresql.org 6883 : 172 : appendStringInfoString(buf, "ALL ");
6884 : :
6885 : : /* Always parenthesize if RHS is another setop */
4388 tgl@sss.pgh.pa.us 6886 : 180 : need_paren = IsA(op->rarg, SetOperationStmt);
6887 : :
6888 : : /*
6889 : : * The indentation code here is deliberately a bit different from that
6890 : : * for the lefthand input, because we want the line breaks in
6891 : : * different places.
6892 : : */
7973 6893 [ - + ]: 180 : if (need_paren)
6894 : : {
7973 tgl@sss.pgh.pa.us 6895 :UBC 0 : appendStringInfoChar(buf, '(');
4388 6896 : 0 : subindent = PRETTYINDENT_STD;
6897 : : }
6898 : : else
4388 tgl@sss.pgh.pa.us 6899 :CBC 180 : subindent = 0;
6900 : 180 : appendContextKeyword(context, "", subindent, 0, 0);
6901 : :
6902 : : /*
6903 : : * The output column names of the RHS sub-select don't matter.
6904 : : */
614 6905 : 180 : save_colnamesvisible = context->colNamesVisible;
6906 : 180 : context->colNamesVisible = false;
6907 : :
6908 : 180 : get_setop_query(op->rarg, query, context);
6909 : :
6910 : 180 : context->colNamesVisible = save_colnamesvisible;
6911 : :
6422 6912 [ + - ]: 180 : if (PRETTY_INDENT(context))
4388 6913 : 180 : context->indentLevel -= subindent;
6914 [ - + ]: 180 : if (need_paren)
4388 tgl@sss.pgh.pa.us 6915 :UBC 0 : appendContextKeyword(context, ")", 0, 0, 0);
6916 : : }
6917 : : else
6918 : : {
8318 6919 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
6920 : : (int) nodeTag(setOp));
6921 : : }
9343 tgl@sss.pgh.pa.us 6922 :CBC 463 : }
6923 : :
6924 : : /*
6925 : : * Display a sort/group clause.
6926 : : *
6927 : : * Also returns the expression tree, so caller need not find it again.
6928 : : */
6929 : : static Node *
4007 andres@anarazel.de 6930 : 425 : get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno,
6931 : : deparse_context *context)
6932 : : {
9151 tgl@sss.pgh.pa.us 6933 : 425 : StringInfo buf = context->buf;
6934 : : TargetEntry *tle;
6935 : : Node *expr;
6936 : :
4007 andres@anarazel.de 6937 : 425 : tle = get_sortgroupref_tle(ref, tlist);
8545 tgl@sss.pgh.pa.us 6938 : 425 : expr = (Node *) tle->expr;
6939 : :
6940 : : /*
6941 : : * Use column-number form if requested by caller. Otherwise, if
6942 : : * expression is a constant, force it to be dumped with an explicit cast
6943 : : * as decoration --- this is because a simple integer constant is
6944 : : * ambiguous (and will be misinterpreted by findTargetlistEntrySQL92()) if
6945 : : * we dump it without any decoration. Similarly, if it's just a Var,
6946 : : * there is risk of misinterpretation if the column name is reassigned in
6947 : : * the SELECT list, so we may need to force table qualification. And, if
6948 : : * it's anything more complex than a simple Var, then force extra parens
6949 : : * around it, to ensure it can't be misinterpreted as a cube() or rollup()
6950 : : * construct.
6951 : : */
6694 6952 [ + + ]: 425 : if (force_colno)
6953 : : {
7699 6954 [ - + ]: 7 : Assert(!tle->resjunk);
6955 : 7 : appendStringInfo(buf, "%d", tle->resno);
6956 : : }
614 6957 [ + - ]: 418 : else if (!expr)
6958 : : /* do nothing, probably can't happen */ ;
6959 [ - + ]: 418 : else if (IsA(expr, Const))
6694 tgl@sss.pgh.pa.us 6960 :UBC 0 : get_const_expr((Const *) expr, context, 1);
614 tgl@sss.pgh.pa.us 6961 [ + + ]:CBC 418 : else if (IsA(expr, Var))
6962 : : {
6963 : : /* Tell get_variable to check for name conflict */
6964 : 401 : bool save_varinorderby = context->varInOrderBy;
6965 : :
6966 : 401 : context->varInOrderBy = true;
6967 : 401 : (void) get_variable((Var *) expr, 0, false, context);
6968 : 401 : context->varInOrderBy = save_varinorderby;
6969 : : }
6970 : : else
6971 : : {
6972 : : /*
6973 : : * We must force parens for function-like expressions even if
6974 : : * PRETTY_PAREN is off, since those are the ones in danger of
6975 : : * misparsing. For other expressions we need to force them only if
6976 : : * PRETTY_PAREN is on, since otherwise the expression will output them
6977 : : * itself. (We can't skip the parens.)
6978 : : */
4000 bruce@momjian.us 6979 : 34 : bool need_paren = (PRETTY_PAREN(context)
6980 [ + + ]: 17 : || IsA(expr, FuncExpr)
2180 tgl@sss.pgh.pa.us 6981 [ + - ]: 15 : || IsA(expr, Aggref)
1133 alvherre@alvh.no-ip. 6982 [ + - ]: 15 : || IsA(expr, WindowFunc)
6983 [ + - - + ]: 34 : || IsA(expr, JsonConstructorExpr));
6984 : :
4007 andres@anarazel.de 6985 [ + + ]: 17 : if (need_paren)
3185 peter_e@gmx.net 6986 : 2 : appendStringInfoChar(context->buf, '(');
8629 tgl@sss.pgh.pa.us 6987 : 17 : get_rule_expr(expr, context, true);
4007 andres@anarazel.de 6988 [ + + ]: 17 : if (need_paren)
3185 peter_e@gmx.net 6989 : 2 : appendStringInfoChar(context->buf, ')');
6990 : : }
6991 : :
8768 tgl@sss.pgh.pa.us 6992 : 425 : return expr;
6993 : : }
6994 : :
6995 : : /*
6996 : : * Display a GroupingSet
6997 : : */
6998 : : static void
4007 andres@anarazel.de 6999 : 12 : get_rule_groupingset(GroupingSet *gset, List *targetlist,
7000 : : bool omit_parens, deparse_context *context)
7001 : : {
7002 : : ListCell *l;
7003 : 12 : StringInfo buf = context->buf;
7004 : 12 : bool omit_child_parens = true;
7005 : 12 : char *sep = "";
7006 : :
7007 [ - + + - : 12 : switch (gset->kind)
- - ]
7008 : : {
4007 andres@anarazel.de 7009 :UBC 0 : case GROUPING_SET_EMPTY:
7010 : 0 : appendStringInfoString(buf, "()");
7011 : 0 : return;
7012 : :
4007 andres@anarazel.de 7013 :CBC 8 : case GROUPING_SET_SIMPLE:
7014 : : {
7015 [ + - + - ]: 8 : if (!omit_parens || list_length(gset->content) != 1)
3185 peter_e@gmx.net 7016 : 8 : appendStringInfoChar(buf, '(');
7017 : :
4007 andres@anarazel.de 7018 [ + - + + : 28 : foreach(l, gset->content)
+ + ]
7019 : : {
4000 bruce@momjian.us 7020 : 20 : Index ref = lfirst_int(l);
7021 : :
4007 andres@anarazel.de 7022 : 20 : appendStringInfoString(buf, sep);
7023 : 20 : get_rule_sortgroupclause(ref, targetlist,
7024 : : false, context);
7025 : 20 : sep = ", ";
7026 : : }
7027 : :
7028 [ + - + - ]: 8 : if (!omit_parens || list_length(gset->content) != 1)
3185 peter_e@gmx.net 7029 : 8 : appendStringInfoChar(buf, ')');
7030 : : }
4007 andres@anarazel.de 7031 : 8 : return;
7032 : :
7033 : 4 : case GROUPING_SET_ROLLUP:
7034 : 4 : appendStringInfoString(buf, "ROLLUP(");
7035 : 4 : break;
4007 andres@anarazel.de 7036 :UBC 0 : case GROUPING_SET_CUBE:
7037 : 0 : appendStringInfoString(buf, "CUBE(");
7038 : 0 : break;
7039 : 0 : case GROUPING_SET_SETS:
7040 : 0 : appendStringInfoString(buf, "GROUPING SETS (");
7041 : 0 : omit_child_parens = false;
7042 : 0 : break;
7043 : : }
7044 : :
4007 andres@anarazel.de 7045 [ + - + + :CBC 12 : foreach(l, gset->content)
+ + ]
7046 : : {
7047 : 8 : appendStringInfoString(buf, sep);
7048 : 8 : get_rule_groupingset(lfirst(l), targetlist, omit_child_parens, context);
7049 : 8 : sep = ", ";
7050 : : }
7051 : :
3185 peter_e@gmx.net 7052 : 4 : appendStringInfoChar(buf, ')');
7053 : : }
7054 : :
7055 : : /*
7056 : : * Display an ORDER BY list.
7057 : : */
7058 : : static void
6337 tgl@sss.pgh.pa.us 7059 : 231 : get_rule_orderby(List *orderList, List *targetList,
7060 : : bool force_colno, deparse_context *context)
7061 : : {
7062 : 231 : StringInfo buf = context->buf;
7063 : : const char *sep;
7064 : : ListCell *l;
7065 : :
7066 : 231 : sep = "";
7067 [ + - + + : 492 : foreach(l, orderList)
+ + ]
7068 : : {
7069 : 261 : SortGroupClause *srt = (SortGroupClause *) lfirst(l);
7070 : : Node *sortexpr;
7071 : : Oid sortcoltype;
7072 : : TypeCacheEntry *typentry;
7073 : :
7074 : 261 : appendStringInfoString(buf, sep);
4007 andres@anarazel.de 7075 : 261 : sortexpr = get_rule_sortgroupclause(srt->tleSortGroupRef, targetList,
7076 : : force_colno, context);
6337 tgl@sss.pgh.pa.us 7077 : 261 : sortcoltype = exprType(sortexpr);
7078 : : /* See whether operator is default < or > for datatype */
7079 : 261 : typentry = lookup_type_cache(sortcoltype,
7080 : : TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
7081 [ + + ]: 261 : if (srt->sortop == typentry->lt_opr)
7082 : : {
7083 : : /* ASC is default, so emit nothing for it */
7084 [ - + ]: 244 : if (srt->nulls_first)
4569 rhaas@postgresql.org 7085 :UBC 0 : appendStringInfoString(buf, " NULLS FIRST");
7086 : : }
6337 tgl@sss.pgh.pa.us 7087 [ + + ]:CBC 17 : else if (srt->sortop == typentry->gt_opr)
7088 : : {
4569 rhaas@postgresql.org 7089 : 6 : appendStringInfoString(buf, " DESC");
7090 : : /* DESC defaults to NULLS FIRST */
6337 tgl@sss.pgh.pa.us 7091 [ + + ]: 6 : if (!srt->nulls_first)
4569 rhaas@postgresql.org 7092 : 1 : appendStringInfoString(buf, " NULLS LAST");
7093 : : }
7094 : : else
7095 : : {
6337 tgl@sss.pgh.pa.us 7096 : 11 : appendStringInfo(buf, " USING %s",
7097 : : generate_operator_name(srt->sortop,
7098 : : sortcoltype,
7099 : : sortcoltype));
7100 : : /* be specific to eliminate ambiguity */
7101 [ - + ]: 11 : if (srt->nulls_first)
4569 rhaas@postgresql.org 7102 :UBC 0 : appendStringInfoString(buf, " NULLS FIRST");
7103 : : else
4569 rhaas@postgresql.org 7104 :CBC 11 : appendStringInfoString(buf, " NULLS LAST");
7105 : : }
6337 tgl@sss.pgh.pa.us 7106 : 261 : sep = ", ";
7107 : : }
7108 : 231 : }
7109 : :
7110 : : /*
7111 : : * Display a WINDOW clause.
7112 : : *
7113 : : * Note that the windowClause list might contain only anonymous window
7114 : : * specifications, in which case we should print nothing here.
7115 : : */
7116 : : static void
7117 : 32 : get_rule_windowclause(Query *query, deparse_context *context)
7118 : : {
7119 : 32 : StringInfo buf = context->buf;
7120 : : const char *sep;
7121 : : ListCell *l;
7122 : :
7123 : 32 : sep = NULL;
7124 [ + - + + : 64 : foreach(l, query->windowClause)
+ + ]
7125 : : {
7126 : 32 : WindowClause *wc = (WindowClause *) lfirst(l);
7127 : :
7128 [ + + ]: 32 : if (wc->name == NULL)
7129 : 28 : continue; /* ignore anonymous windows */
7130 : :
6337 tgl@sss.pgh.pa.us 7131 [ + - ]:GBC 4 : if (sep == NULL)
7132 : 4 : appendContextKeyword(context, " WINDOW ",
7133 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
7134 : : else
6337 tgl@sss.pgh.pa.us 7135 :UBC 0 : appendStringInfoString(buf, sep);
7136 : :
6337 tgl@sss.pgh.pa.us 7137 :GBC 4 : appendStringInfo(buf, "%s AS ", quote_identifier(wc->name));
7138 : :
7139 : 4 : get_rule_windowspec(wc, query->targetList, context);
7140 : :
7141 : 4 : sep = ", ";
7142 : : }
6337 tgl@sss.pgh.pa.us 7143 :CBC 32 : }
7144 : :
7145 : : /*
7146 : : * Display a window definition
7147 : : */
7148 : : static void
7149 : 32 : get_rule_windowspec(WindowClause *wc, List *targetList,
7150 : : deparse_context *context)
7151 : : {
7152 : 32 : StringInfo buf = context->buf;
7153 : 32 : bool needspace = false;
7154 : : const char *sep;
7155 : : ListCell *l;
7156 : :
7157 : 32 : appendStringInfoChar(buf, '(');
7158 [ - + ]: 32 : if (wc->refname)
7159 : : {
6337 tgl@sss.pgh.pa.us 7160 :UBC 0 : appendStringInfoString(buf, quote_identifier(wc->refname));
7161 : 0 : needspace = true;
7162 : : }
7163 : : /* partition clauses are always inherited, so only print if no refname */
6337 tgl@sss.pgh.pa.us 7164 [ - + - - ]:CBC 32 : if (wc->partitionClause && !wc->refname)
7165 : : {
6337 tgl@sss.pgh.pa.us 7166 [ # # ]:UBC 0 : if (needspace)
7167 : 0 : appendStringInfoChar(buf, ' ');
7168 : 0 : appendStringInfoString(buf, "PARTITION BY ");
7169 : 0 : sep = "";
7170 [ # # # # : 0 : foreach(l, wc->partitionClause)
# # ]
7171 : : {
7172 : 0 : SortGroupClause *grp = (SortGroupClause *) lfirst(l);
7173 : :
7174 : 0 : appendStringInfoString(buf, sep);
4007 andres@anarazel.de 7175 : 0 : get_rule_sortgroupclause(grp->tleSortGroupRef, targetList,
7176 : : false, context);
6337 tgl@sss.pgh.pa.us 7177 : 0 : sep = ", ";
7178 : : }
7179 : 0 : needspace = true;
7180 : : }
7181 : : /* print ordering clause only if not inherited */
6337 tgl@sss.pgh.pa.us 7182 [ + - + - ]:CBC 32 : if (wc->orderClause && !wc->copiedOrder)
7183 : : {
7184 [ - + ]: 32 : if (needspace)
6337 tgl@sss.pgh.pa.us 7185 :UBC 0 : appendStringInfoChar(buf, ' ');
6337 tgl@sss.pgh.pa.us 7186 :CBC 32 : appendStringInfoString(buf, "ORDER BY ");
7187 : 32 : get_rule_orderby(wc->orderClause, targetList, false, context);
7188 : 32 : needspace = true;
7189 : : }
7190 : : /* framing clause is never inherited, so print unless it's default */
6334 7191 [ + + ]: 32 : if (wc->frameOptions & FRAMEOPTION_NONDEFAULT)
7192 : : {
7193 [ + - ]: 28 : if (needspace)
7194 : 28 : appendStringInfoChar(buf, ' ');
420 7195 : 28 : get_window_frame_options(wc->frameOptions,
7196 : : wc->startOffset, wc->endOffset,
7197 : : context);
7198 : : }
7199 : 32 : appendStringInfoChar(buf, ')');
7200 : 32 : }
7201 : :
7202 : : /*
7203 : : * Append the description of a window's framing options to context->buf
7204 : : */
7205 : : static void
7206 : 158 : get_window_frame_options(int frameOptions,
7207 : : Node *startOffset, Node *endOffset,
7208 : : deparse_context *context)
7209 : : {
7210 : 158 : StringInfo buf = context->buf;
7211 : :
7212 [ + - ]: 158 : if (frameOptions & FRAMEOPTION_NONDEFAULT)
7213 : : {
7214 [ + + ]: 158 : if (frameOptions & FRAMEOPTION_RANGE)
6334 7215 : 13 : appendStringInfoString(buf, "RANGE ");
420 7216 [ + + ]: 145 : else if (frameOptions & FRAMEOPTION_ROWS)
6334 7217 : 137 : appendStringInfoString(buf, "ROWS ");
420 7218 [ + - ]: 8 : else if (frameOptions & FRAMEOPTION_GROUPS)
3009 7219 : 8 : appendStringInfoString(buf, "GROUPS ");
7220 : : else
6334 tgl@sss.pgh.pa.us 7221 :UBC 0 : Assert(false);
420 tgl@sss.pgh.pa.us 7222 [ + + ]:CBC 158 : if (frameOptions & FRAMEOPTION_BETWEEN)
6334 7223 : 61 : appendStringInfoString(buf, "BETWEEN ");
420 7224 [ + + ]: 158 : if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
6334 7225 : 101 : appendStringInfoString(buf, "UNBOUNDED PRECEDING ");
420 7226 [ + + ]: 57 : else if (frameOptions & FRAMEOPTION_START_CURRENT_ROW)
6334 7227 : 17 : appendStringInfoString(buf, "CURRENT ROW ");
420 7228 [ + - ]: 40 : else if (frameOptions & FRAMEOPTION_START_OFFSET)
7229 : : {
7230 : 40 : get_rule_expr(startOffset, context, false);
7231 [ + - ]: 40 : if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
5926 7232 : 40 : appendStringInfoString(buf, " PRECEDING ");
420 tgl@sss.pgh.pa.us 7233 [ # # ]:UBC 0 : else if (frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
5926 7234 : 0 : appendStringInfoString(buf, " FOLLOWING ");
7235 : : else
7236 : 0 : Assert(false);
7237 : : }
7238 : : else
6334 7239 : 0 : Assert(false);
420 tgl@sss.pgh.pa.us 7240 [ + + ]:CBC 158 : if (frameOptions & FRAMEOPTION_BETWEEN)
7241 : : {
6334 7242 : 61 : appendStringInfoString(buf, "AND ");
420 7243 [ + + ]: 61 : if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
6334 7244 : 13 : appendStringInfoString(buf, "UNBOUNDED FOLLOWING ");
420 7245 [ + + ]: 48 : else if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
6334 7246 : 4 : appendStringInfoString(buf, "CURRENT ROW ");
420 7247 [ + - ]: 44 : else if (frameOptions & FRAMEOPTION_END_OFFSET)
7248 : : {
7249 : 44 : get_rule_expr(endOffset, context, false);
7250 [ - + ]: 44 : if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
5926 tgl@sss.pgh.pa.us 7251 :UBC 0 : appendStringInfoString(buf, " PRECEDING ");
420 tgl@sss.pgh.pa.us 7252 [ + - ]:CBC 44 : else if (frameOptions & FRAMEOPTION_END_OFFSET_FOLLOWING)
5926 7253 : 44 : appendStringInfoString(buf, " FOLLOWING ");
7254 : : else
5926 tgl@sss.pgh.pa.us 7255 :UBC 0 : Assert(false);
7256 : : }
7257 : : else
6334 7258 : 0 : Assert(false);
7259 : : }
420 tgl@sss.pgh.pa.us 7260 [ + + ]:CBC 158 : if (frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
3009 7261 : 4 : appendStringInfoString(buf, "EXCLUDE CURRENT ROW ");
420 7262 [ + + ]: 154 : else if (frameOptions & FRAMEOPTION_EXCLUDE_GROUP)
3009 7263 : 4 : appendStringInfoString(buf, "EXCLUDE GROUP ");
420 7264 [ + + ]: 150 : else if (frameOptions & FRAMEOPTION_EXCLUDE_TIES)
3009 7265 : 4 : appendStringInfoString(buf, "EXCLUDE TIES ");
7266 : : /* we will now have a trailing space; remove it */
420 7267 : 158 : buf->data[--(buf->len)] = '\0';
7268 : : }
7269 : 158 : }
7270 : :
7271 : : /*
7272 : : * Return the description of a window's framing options as a palloc'd string
7273 : : */
7274 : : char *
7275 : 130 : get_window_frame_options_for_explain(int frameOptions,
7276 : : Node *startOffset, Node *endOffset,
7277 : : List *dpcontext, bool forceprefix)
7278 : : {
7279 : : StringInfoData buf;
7280 : : deparse_context context;
7281 : :
7282 : 130 : initStringInfo(&buf);
7283 : 130 : context.buf = &buf;
7284 : 130 : context.namespaces = dpcontext;
7285 : 130 : context.resultDesc = NULL;
7286 : 130 : context.targetList = NIL;
7287 : 130 : context.windowClause = NIL;
7288 : 130 : context.varprefix = forceprefix;
7289 : 130 : context.prettyFlags = 0;
7290 : 130 : context.wrapColumn = WRAP_COLUMN_DEFAULT;
7291 : 130 : context.indentLevel = 0;
7292 : 130 : context.colNamesVisible = true;
7293 : 130 : context.inGroupBy = false;
7294 : 130 : context.varInOrderBy = false;
7295 : 130 : context.appendparents = NULL;
7296 : :
7297 : 130 : get_window_frame_options(frameOptions, startOffset, endOffset, &context);
7298 : :
7299 : 130 : return buf.data;
7300 : : }
7301 : :
7302 : : /* ----------
7303 : : * get_insert_query_def - Parse back an INSERT parsetree
7304 : : * ----------
7305 : : */
7306 : : static void
614 7307 : 194 : get_insert_query_def(Query *query, deparse_context *context)
7308 : : {
9711 7309 : 194 : StringInfo buf = context->buf;
9343 7310 : 194 : RangeTblEntry *select_rte = NULL;
7216 mail@joeconway.com 7311 : 194 : RangeTblEntry *values_rte = NULL;
7312 : : RangeTblEntry *rte;
7313 : : char *sep;
7314 : : ListCell *l;
7315 : : List *strippedexprs;
7316 : :
7317 : : /* Insert the WITH clause if given */
5681 tgl@sss.pgh.pa.us 7318 : 194 : get_with_clause(query, context);
7319 : :
7320 : : /*
7321 : : * If it's an INSERT ... SELECT or multi-row VALUES, there will be a
7322 : : * single RTE for the SELECT or VALUES. Plain VALUES has neither.
7323 : : */
10108 bruce@momjian.us 7324 [ + - + + : 758 : foreach(l, query->rtable)
+ + ]
7325 : : {
7326 : 564 : rte = (RangeTblEntry *) lfirst(l);
7327 : :
7216 mail@joeconway.com 7328 [ + + ]: 564 : if (rte->rtekind == RTE_SUBQUERY)
7329 : : {
7330 [ - + ]: 30 : if (select_rte)
7216 mail@joeconway.com 7331 [ # # ]:UBC 0 : elog(ERROR, "too many subquery RTEs in INSERT");
7216 mail@joeconway.com 7332 :CBC 30 : select_rte = rte;
7333 : : }
7334 : :
7335 [ + + ]: 564 : if (rte->rtekind == RTE_VALUES)
7336 : : {
7337 [ - + ]: 26 : if (values_rte)
7216 mail@joeconway.com 7338 [ # # ]:UBC 0 : elog(ERROR, "too many values RTEs in INSERT");
7216 mail@joeconway.com 7339 :CBC 26 : values_rte = rte;
7340 : : }
7341 : : }
7342 [ + + - + ]: 194 : if (select_rte && values_rte)
7216 mail@joeconway.com 7343 [ # # ]:UBC 0 : elog(ERROR, "both subquery and values RTEs in INSERT");
7344 : :
7345 : : /*
7346 : : * Start the query with INSERT INTO relname
7347 : : */
9683 tgl@sss.pgh.pa.us 7348 :CBC 194 : rte = rt_fetch(query->resultRelation, query->rtable);
8768 7349 [ - + ]: 194 : Assert(rte->rtekind == RTE_RELATION);
7350 : :
8315 7351 [ + - ]: 194 : if (PRETTY_INDENT(context))
7352 : : {
8310 bruce@momjian.us 7353 : 194 : context->indentLevel += PRETTYINDENT_STD;
7354 : 194 : appendStringInfoChar(buf, ' ');
7355 : : }
1173 tgl@sss.pgh.pa.us 7356 : 194 : appendStringInfo(buf, "INSERT INTO %s",
7357 : : generate_relation_name(rte->relid, NIL));
7358 : :
7359 : : /* Print the relation alias, if needed; INSERT requires explicit AS */
7360 : 194 : get_rte_alias(rte, query->resultRelation, true, context);
7361 : :
7362 : : /* always want a space here */
7363 : 194 : appendStringInfoChar(buf, ' ');
7364 : :
7365 : : /*
7366 : : * Add the insert-column-names list. Any indirection decoration needed on
7367 : : * the column names can be inferred from the top targetlist.
7368 : : */
8000 7369 : 194 : strippedexprs = NIL;
7370 : 194 : sep = "";
4946 7371 [ + - ]: 194 : if (query->targetList)
7372 : 194 : appendStringInfoChar(buf, '(');
10108 bruce@momjian.us 7373 [ + - + + : 695 : foreach(l, query->targetList)
+ + ]
7374 : : {
9366 tgl@sss.pgh.pa.us 7375 : 501 : TargetEntry *tle = (TargetEntry *) lfirst(l);
7376 : :
7699 7377 [ - + ]: 501 : if (tle->resjunk)
9349 tgl@sss.pgh.pa.us 7378 :UBC 0 : continue; /* ignore junk entries */
7379 : :
7675 neilc@samurai.com 7380 :CBC 501 : appendStringInfoString(buf, sep);
10108 bruce@momjian.us 7381 : 501 : sep = ", ";
7382 : :
7383 : : /*
7384 : : * Put out name of target column; look in the catalogs, not at
7385 : : * tle->resname, since resname will fail to track RENAME.
7386 : : */
8130 neilc@samurai.com 7387 : 501 : appendStringInfoString(buf,
3004 alvherre@alvh.no-ip. 7388 : 501 : quote_identifier(get_attname(rte->relid,
7389 : 501 : tle->resno,
7390 : : false)));
7391 : :
7392 : : /*
7393 : : * Print any indirection needed (subfields or subscripts), and strip
7394 : : * off the top-level nodes representing the indirection assignments.
7395 : : * Add the stripped expressions to strippedexprs. (If it's a
7396 : : * single-VALUES statement, the stripped expressions are the VALUES to
7397 : : * print below. Otherwise they're just Vars and not really
7398 : : * interesting.)
7399 : : */
3562 tgl@sss.pgh.pa.us 7400 : 501 : strippedexprs = lappend(strippedexprs,
7401 : 501 : processIndirection((Node *) tle->expr,
7402 : : context));
7403 : : }
4946 7404 [ + - ]: 194 : if (query->targetList)
4569 rhaas@postgresql.org 7405 : 194 : appendStringInfoString(buf, ") ");
7406 : :
3316 peter_e@gmx.net 7407 [ - + ]: 194 : if (query->override)
7408 : : {
3316 peter_e@gmx.net 7409 [ # # ]:UBC 0 : if (query->override == OVERRIDING_SYSTEM_VALUE)
7410 : 0 : appendStringInfoString(buf, "OVERRIDING SYSTEM VALUE ");
7411 [ # # ]: 0 : else if (query->override == OVERRIDING_USER_VALUE)
7412 : 0 : appendStringInfoString(buf, "OVERRIDING USER VALUE ");
7413 : : }
7414 : :
7216 mail@joeconway.com 7415 [ + + ]:CBC 194 : if (select_rte)
7416 : : {
7417 : : /* Add the SELECT */
1630 tgl@sss.pgh.pa.us 7418 : 30 : get_query_def(select_rte->subquery, buf, context->namespaces, NULL,
7419 : : false,
7420 : : context->prettyFlags, context->wrapColumn,
7421 : : context->indentLevel);
7422 : : }
7216 mail@joeconway.com 7423 [ + + ]: 164 : else if (values_rte)
7424 : : {
7425 : : /* Add the multi-VALUES expression lists */
7426 : 26 : get_values_def(values_rte->values_lists, context);
7427 : : }
4946 tgl@sss.pgh.pa.us 7428 [ + - ]: 138 : else if (strippedexprs)
7429 : : {
7430 : : /* Add the single-VALUES expression list */
8315 7431 : 138 : appendContextKeyword(context, "VALUES (",
7432 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
1573 7433 : 138 : get_rule_list_toplevel(strippedexprs, context, false);
9571 7434 : 138 : appendStringInfoChar(buf, ')');
7435 : : }
7436 : : else
7437 : : {
7438 : : /* No expressions, so it must be DEFAULT VALUES */
4569 rhaas@postgresql.org 7439 :UBC 0 : appendStringInfoString(buf, "DEFAULT VALUES");
7440 : : }
7441 : :
7442 : : /* Add ON CONFLICT if present */
4015 andres@anarazel.de 7443 [ + + ]:CBC 194 : if (query->onConflict)
7444 : : {
7445 : 24 : OnConflictExpr *confl = query->onConflict;
7446 : :
3960 heikki.linnakangas@i 7447 : 24 : appendStringInfoString(buf, " ON CONFLICT");
7448 : :
4004 andres@anarazel.de 7449 [ + + ]: 24 : if (confl->arbiterElems)
7450 : : {
7451 : : /* Add the single-VALUES expression list */
7452 : 20 : appendStringInfoChar(buf, '(');
7453 : 20 : get_rule_expr((Node *) confl->arbiterElems, context, false);
7454 : 20 : appendStringInfoChar(buf, ')');
7455 : :
7456 : : /* Add a WHERE clause (for partial indexes) if given */
7457 [ + + ]: 20 : if (confl->arbiterWhere != NULL)
7458 : : {
7459 : : bool save_varprefix;
7460 : :
7461 : : /*
7462 : : * Force non-prefixing of Vars, since parser assumes that they
7463 : : * belong to target relation. WHERE clause does not use
7464 : : * InferenceElem, so this is separately required.
7465 : : */
3740 tgl@sss.pgh.pa.us 7466 : 8 : save_varprefix = context->varprefix;
7467 : 8 : context->varprefix = false;
7468 : :
4004 andres@anarazel.de 7469 : 8 : appendContextKeyword(context, " WHERE ",
7470 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
7471 : 8 : get_rule_expr(confl->arbiterWhere, context, false);
7472 : :
3740 tgl@sss.pgh.pa.us 7473 : 8 : context->varprefix = save_varprefix;
7474 : : }
7475 : : }
3646 7476 [ - + ]: 4 : else if (OidIsValid(confl->constraint))
7477 : : {
4000 bruce@momjian.us 7478 :UBC 0 : char *constraint = get_constraint_name(confl->constraint);
7479 : :
3646 tgl@sss.pgh.pa.us 7480 [ # # ]: 0 : if (!constraint)
7481 [ # # ]: 0 : elog(ERROR, "cache lookup failed for constraint %u",
7482 : : confl->constraint);
4004 andres@anarazel.de 7483 : 0 : appendStringInfo(buf, " ON CONSTRAINT %s",
7484 : : quote_identifier(constraint));
7485 : : }
7486 : :
4015 andres@anarazel.de 7487 [ + + ]:CBC 24 : if (confl->action == ONCONFLICT_NOTHING)
7488 : : {
4004 7489 : 12 : appendStringInfoString(buf, " DO NOTHING");
7490 : : }
82 dean.a.rasheed@gmail 7491 [ + + ]:GNC 12 : else if (confl->action == ONCONFLICT_UPDATE)
7492 : : {
4004 andres@anarazel.de 7493 :CBC 8 : appendStringInfoString(buf, " DO UPDATE SET ");
7494 : : /* Deparse targetlist */
4015 7495 : 8 : get_update_query_targetlist_def(query, confl->onConflictSet,
7496 : : context, rte);
7497 : :
7498 : : /* Add a WHERE clause if given */
7499 [ + - ]: 8 : if (confl->onConflictWhere != NULL)
7500 : : {
7501 : 8 : appendContextKeyword(context, " WHERE ",
7502 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
7503 : 8 : get_rule_expr(confl->onConflictWhere, context, false);
7504 : : }
7505 : : }
7506 : : else
7507 : : {
82 dean.a.rasheed@gmail 7508 [ - + ]:GNC 4 : Assert(confl->action == ONCONFLICT_SELECT);
7509 : 4 : appendStringInfoString(buf, " DO SELECT");
7510 : :
7511 : : /* Add FOR [KEY] UPDATE/SHARE clause if present */
7512 [ + - ]: 4 : if (confl->lockStrength != LCS_NONE)
7513 : 4 : appendStringInfoString(buf, get_lock_clause_strength(confl->lockStrength));
7514 : :
7515 : : /* Add a WHERE clause if given */
7516 [ + - ]: 4 : if (confl->onConflictWhere != NULL)
7517 : : {
7518 : 4 : appendContextKeyword(context, " WHERE ",
7519 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
7520 : 4 : get_rule_expr(confl->onConflictWhere, context, false);
7521 : : }
7522 : : }
7523 : : }
7524 : :
7525 : : /* Add RETURNING if present */
7206 tgl@sss.pgh.pa.us 7526 [ + + ]:CBC 194 : if (query->returningList)
474 dean.a.rasheed@gmail 7527 : 51 : get_returning_clause(query, context);
10116 bruce@momjian.us 7528 : 194 : }
7529 : :
7530 : :
7531 : : /* ----------
7532 : : * get_update_query_def - Parse back an UPDATE parsetree
7533 : : * ----------
7534 : : */
7535 : : static void
614 tgl@sss.pgh.pa.us 7536 : 94 : get_update_query_def(Query *query, deparse_context *context)
7537 : : {
7919 bruce@momjian.us 7538 : 94 : StringInfo buf = context->buf;
7539 : : RangeTblEntry *rte;
7540 : :
7541 : : /* Insert the WITH clause if given */
5681 tgl@sss.pgh.pa.us 7542 : 94 : get_with_clause(query, context);
7543 : :
7544 : : /*
7545 : : * Start the query with UPDATE relname SET
7546 : : */
9683 7547 : 94 : rte = rt_fetch(query->resultRelation, query->rtable);
8768 7548 [ - + ]: 94 : Assert(rte->rtekind == RTE_RELATION);
8315 7549 [ + - ]: 94 : if (PRETTY_INDENT(context))
7550 : : {
8310 bruce@momjian.us 7551 : 94 : appendStringInfoChar(buf, ' ');
7552 : 94 : context->indentLevel += PRETTYINDENT_STD;
7553 : : }
6576 tgl@sss.pgh.pa.us 7554 : 188 : appendStringInfo(buf, "UPDATE %s%s",
9460 7555 [ + - ]: 94 : only_marker(rte),
7556 : : generate_relation_name(rte->relid, NIL));
7557 : :
7558 : : /* Print the FOR PORTION OF, if needed */
34 peter@eisentraut.org 7559 :GNC 94 : get_for_portion_of(query->forPortionOf, context);
7560 : :
7561 : : /* Print the relation alias, if needed */
1173 tgl@sss.pgh.pa.us 7562 :CBC 94 : get_rte_alias(rte, query->resultRelation, false, context);
7563 : :
6576 7564 : 94 : appendStringInfoString(buf, " SET ");
7565 : :
7566 : : /* Deparse targetlist */
4015 andres@anarazel.de 7567 : 94 : get_update_query_targetlist_def(query, query->targetList, context, rte);
7568 : :
7569 : : /* Add the FROM clause if needed */
7570 : 94 : get_from_clause(query, " FROM ", context);
7571 : :
7572 : : /* Add a WHERE clause if given */
7573 [ + + ]: 94 : if (query->jointree->quals != NULL)
7574 : : {
7575 : 61 : appendContextKeyword(context, " WHERE ",
7576 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
7577 : 61 : get_rule_expr(query->jointree->quals, context, false);
7578 : : }
7579 : :
7580 : : /* Add RETURNING if present */
7581 [ + + ]: 94 : if (query->returningList)
474 dean.a.rasheed@gmail 7582 : 37 : get_returning_clause(query, context);
4015 andres@anarazel.de 7583 : 94 : }
7584 : :
7585 : :
7586 : : /* ----------
7587 : : * get_update_query_targetlist_def - Parse back an UPDATE targetlist
7588 : : * ----------
7589 : : */
7590 : : static void
7591 : 118 : get_update_query_targetlist_def(Query *query, List *targetList,
7592 : : deparse_context *context, RangeTblEntry *rte)
7593 : : {
7594 : 118 : StringInfo buf = context->buf;
7595 : : ListCell *l;
7596 : : ListCell *next_ma_cell;
7597 : : int remaining_ma_columns;
7598 : : const char *sep;
7599 : : SubLink *cur_ma_sublink;
7600 : : List *ma_sublinks;
7601 : :
7602 : : /*
7603 : : * Prepare to deal with MULTIEXPR assignments: collect the source SubLinks
7604 : : * into a list. We expect them to appear, in ID order, in resjunk tlist
7605 : : * entries.
7606 : : */
4339 tgl@sss.pgh.pa.us 7607 : 118 : ma_sublinks = NIL;
7608 [ + + ]: 118 : if (query->hasSubLinks) /* else there can't be any */
7609 : : {
4015 andres@anarazel.de 7610 [ + - + + : 28 : foreach(l, targetList)
+ + ]
7611 : : {
4339 tgl@sss.pgh.pa.us 7612 : 20 : TargetEntry *tle = (TargetEntry *) lfirst(l);
7613 : :
7614 [ + + + - ]: 20 : if (tle->resjunk && IsA(tle->expr, SubLink))
7615 : : {
7616 : 4 : SubLink *sl = (SubLink *) tle->expr;
7617 : :
7618 [ + - ]: 4 : if (sl->subLinkType == MULTIEXPR_SUBLINK)
7619 : : {
7620 : 4 : ma_sublinks = lappend(ma_sublinks, sl);
7621 [ - + ]: 4 : Assert(sl->subLinkId == list_length(ma_sublinks));
7622 : : }
7623 : : }
7624 : : }
7625 : : }
7626 : 118 : next_ma_cell = list_head(ma_sublinks);
7627 : 118 : cur_ma_sublink = NULL;
7628 : 118 : remaining_ma_columns = 0;
7629 : :
7630 : : /* Add the comma separated list of 'attname = value' */
10108 bruce@momjian.us 7631 : 118 : sep = "";
4015 andres@anarazel.de 7632 [ + - + + : 298 : foreach(l, targetList)
+ + ]
7633 : : {
9349 tgl@sss.pgh.pa.us 7634 : 180 : TargetEntry *tle = (TargetEntry *) lfirst(l);
7635 : : Node *expr;
7636 : :
7699 7637 [ + + ]: 180 : if (tle->resjunk)
9349 7638 : 4 : continue; /* ignore junk entries */
7639 : :
7640 : : /* Emit separator (OK whether we're in multiassignment or not) */
7675 neilc@samurai.com 7641 : 176 : appendStringInfoString(buf, sep);
10108 bruce@momjian.us 7642 : 176 : sep = ", ";
7643 : :
7644 : : /*
7645 : : * Check to see if we're starting a multiassignment group: if so,
7646 : : * output a left paren.
7647 : : */
4339 tgl@sss.pgh.pa.us 7648 [ + + + - ]: 176 : if (next_ma_cell != NULL && cur_ma_sublink == NULL)
7649 : : {
7650 : : /*
7651 : : * We must dig down into the expr to see if it's a PARAM_MULTIEXPR
7652 : : * Param. That could be buried under FieldStores and
7653 : : * SubscriptingRefs and CoerceToDomains (cf processIndirection()),
7654 : : * and underneath those there could be an implicit type coercion.
7655 : : * Because we would ignore implicit type coercions anyway, we
7656 : : * don't need to be as careful as processIndirection() is about
7657 : : * descending past implicit CoerceToDomains.
7658 : : */
7659 : 4 : expr = (Node *) tle->expr;
7660 [ + - ]: 8 : while (expr)
7661 : : {
7662 [ - + ]: 8 : if (IsA(expr, FieldStore))
7663 : : {
4339 tgl@sss.pgh.pa.us 7664 :UBC 0 : FieldStore *fstore = (FieldStore *) expr;
7665 : :
7666 : 0 : expr = (Node *) linitial(fstore->newvals);
7667 : : }
2650 alvherre@alvh.no-ip. 7668 [ + + ]:CBC 8 : else if (IsA(expr, SubscriptingRef))
7669 : : {
7670 : 4 : SubscriptingRef *sbsref = (SubscriptingRef *) expr;
7671 : :
7672 [ - + ]: 4 : if (sbsref->refassgnexpr == NULL)
4339 tgl@sss.pgh.pa.us 7673 :UBC 0 : break;
7674 : :
2650 alvherre@alvh.no-ip. 7675 :CBC 4 : expr = (Node *) sbsref->refassgnexpr;
7676 : : }
3219 tgl@sss.pgh.pa.us 7677 [ - + ]: 4 : else if (IsA(expr, CoerceToDomain))
7678 : : {
3219 tgl@sss.pgh.pa.us 7679 :UBC 0 : CoerceToDomain *cdomain = (CoerceToDomain *) expr;
7680 : :
7681 [ # # ]: 0 : if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
7682 : 0 : break;
7683 : 0 : expr = (Node *) cdomain->arg;
7684 : : }
7685 : : else
4339 tgl@sss.pgh.pa.us 7686 :CBC 4 : break;
7687 : : }
7688 : 4 : expr = strip_implicit_coercions(expr);
7689 : :
7690 [ + - + - ]: 4 : if (expr && IsA(expr, Param) &&
7691 [ + - ]: 4 : ((Param *) expr)->paramkind == PARAM_MULTIEXPR)
7692 : : {
7693 : 4 : cur_ma_sublink = (SubLink *) lfirst(next_ma_cell);
2486 7694 : 4 : next_ma_cell = lnext(ma_sublinks, next_ma_cell);
2287 alvherre@alvh.no-ip. 7695 : 4 : remaining_ma_columns = count_nonjunk_tlist_entries(((Query *) cur_ma_sublink->subselect)->targetList);
4339 tgl@sss.pgh.pa.us 7696 [ - + ]: 4 : Assert(((Param *) expr)->paramid ==
7697 : : ((cur_ma_sublink->subLinkId << 16) | 1));
7698 : 4 : appendStringInfoChar(buf, '(');
7699 : : }
7700 : : }
7701 : :
7702 : : /*
7703 : : * Put out name of target column; look in the catalogs, not at
7704 : : * tle->resname, since resname will fail to track RENAME.
7705 : : */
8000 7706 : 176 : appendStringInfoString(buf,
3004 alvherre@alvh.no-ip. 7707 : 176 : quote_identifier(get_attname(rte->relid,
7708 : 176 : tle->resno,
7709 : : false)));
7710 : :
7711 : : /*
7712 : : * Print any indirection needed (subfields or subscripts), and strip
7713 : : * off the top-level nodes representing the indirection assignments.
7714 : : */
3562 tgl@sss.pgh.pa.us 7715 : 176 : expr = processIndirection((Node *) tle->expr, context);
7716 : :
7717 : : /*
7718 : : * If we're in a multiassignment, skip printing anything more, unless
7719 : : * this is the last column; in which case, what we print should be the
7720 : : * sublink, not the Param.
7721 : : */
4339 7722 [ + + ]: 176 : if (cur_ma_sublink != NULL)
7723 : : {
7724 [ + + ]: 12 : if (--remaining_ma_columns > 0)
7725 : 8 : continue; /* not the last column of multiassignment */
7726 : 4 : appendStringInfoChar(buf, ')');
7727 : 4 : expr = (Node *) cur_ma_sublink;
7728 : 4 : cur_ma_sublink = NULL;
7729 : : }
7730 : :
4569 rhaas@postgresql.org 7731 : 168 : appendStringInfoString(buf, " = ");
7732 : :
8000 tgl@sss.pgh.pa.us 7733 : 168 : get_rule_expr(expr, context, false);
7734 : : }
10116 bruce@momjian.us 7735 : 118 : }
7736 : :
7737 : :
7738 : : /* ----------
7739 : : * get_delete_query_def - Parse back a DELETE parsetree
7740 : : * ----------
7741 : : */
7742 : : static void
614 tgl@sss.pgh.pa.us 7743 : 47 : get_delete_query_def(Query *query, deparse_context *context)
7744 : : {
9711 7745 : 47 : StringInfo buf = context->buf;
7746 : : RangeTblEntry *rte;
7747 : :
7748 : : /* Insert the WITH clause if given */
5681 7749 : 47 : get_with_clause(query, context);
7750 : :
7751 : : /*
7752 : : * Start the query with DELETE FROM relname
7753 : : */
9683 7754 : 47 : rte = rt_fetch(query->resultRelation, query->rtable);
8768 7755 [ - + ]: 47 : Assert(rte->rtekind == RTE_RELATION);
8315 7756 [ + - ]: 47 : if (PRETTY_INDENT(context))
7757 : : {
8310 bruce@momjian.us 7758 : 47 : appendStringInfoChar(buf, ' ');
6576 tgl@sss.pgh.pa.us 7759 : 47 : context->indentLevel += PRETTYINDENT_STD;
7760 : : }
9683 7761 : 94 : appendStringInfo(buf, "DELETE FROM %s%s",
9460 7762 [ + - ]: 47 : only_marker(rte),
7763 : : generate_relation_name(rte->relid, NIL));
7764 : :
7765 : : /* Print the FOR PORTION OF, if needed */
34 peter@eisentraut.org 7766 :GNC 47 : get_for_portion_of(query->forPortionOf, context);
7767 : :
7768 : : /* Print the relation alias, if needed */
1173 tgl@sss.pgh.pa.us 7769 :CBC 47 : get_rte_alias(rte, query->resultRelation, false, context);
7770 : :
7771 : : /* Add the USING clause if given */
7582 7772 : 47 : get_from_clause(query, " USING ", context);
7773 : :
7774 : : /* Add a WHERE clause if given */
9349 7775 [ + + ]: 47 : if (query->jointree->quals != NULL)
7776 : : {
8315 7777 : 39 : appendContextKeyword(context, " WHERE ",
7778 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
8629 7779 : 39 : get_rule_expr(query->jointree->quals, context, false);
7780 : : }
7781 : :
7782 : : /* Add RETURNING if present */
7206 7783 [ + + ]: 47 : if (query->returningList)
474 dean.a.rasheed@gmail 7784 : 17 : get_returning_clause(query, context);
10116 bruce@momjian.us 7785 : 47 : }
7786 : :
7787 : :
7788 : : /* ----------
7789 : : * get_merge_query_def - Parse back a MERGE parsetree
7790 : : * ----------
7791 : : */
7792 : : static void
614 tgl@sss.pgh.pa.us 7793 : 8 : get_merge_query_def(Query *query, deparse_context *context)
7794 : : {
1094 7795 : 8 : StringInfo buf = context->buf;
7796 : : RangeTblEntry *rte;
7797 : : ListCell *lc;
7798 : : bool haveNotMatchedBySource;
7799 : :
7800 : : /* Insert the WITH clause if given */
7801 : 8 : get_with_clause(query, context);
7802 : :
7803 : : /*
7804 : : * Start the query with MERGE INTO relname
7805 : : */
7806 : 8 : rte = rt_fetch(query->resultRelation, query->rtable);
7807 [ - + ]: 8 : Assert(rte->rtekind == RTE_RELATION);
7808 [ + - ]: 8 : if (PRETTY_INDENT(context))
7809 : : {
7810 : 8 : appendStringInfoChar(buf, ' ');
7811 : 8 : context->indentLevel += PRETTYINDENT_STD;
7812 : : }
7813 : 16 : appendStringInfo(buf, "MERGE INTO %s%s",
7814 [ + - ]: 8 : only_marker(rte),
7815 : : generate_relation_name(rte->relid, NIL));
7816 : :
7817 : : /* Print the relation alias, if needed */
7818 : 8 : get_rte_alias(rte, query->resultRelation, false, context);
7819 : :
7820 : : /* Print the source relation and join clause */
7821 : 8 : get_from_clause(query, " USING ", context);
7822 : 8 : appendContextKeyword(context, " ON ",
7823 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
766 dean.a.rasheed@gmail 7824 : 8 : get_rule_expr(query->mergeJoinCondition, context, false);
7825 : :
7826 : : /*
7827 : : * Test for any NOT MATCHED BY SOURCE actions. If there are none, then
7828 : : * any NOT MATCHED BY TARGET actions are output as "WHEN NOT MATCHED", per
7829 : : * SQL standard. Otherwise, we have a non-SQL-standard query, so output
7830 : : * "BY SOURCE" / "BY TARGET" qualifiers for all NOT MATCHED actions, to be
7831 : : * more explicit.
7832 : : */
7833 : 8 : haveNotMatchedBySource = false;
7834 [ + - + + : 56 : foreach(lc, query->mergeActionList)
+ + ]
7835 : : {
7836 : 52 : MergeAction *action = lfirst_node(MergeAction, lc);
7837 : :
7838 [ + + ]: 52 : if (action->matchKind == MERGE_WHEN_NOT_MATCHED_BY_SOURCE)
7839 : : {
7840 : 4 : haveNotMatchedBySource = true;
7841 : 4 : break;
7842 : : }
7843 : : }
7844 : :
7845 : : /* Print each merge action */
1094 tgl@sss.pgh.pa.us 7846 [ + - + + : 60 : foreach(lc, query->mergeActionList)
+ + ]
7847 : : {
7848 : 52 : MergeAction *action = lfirst_node(MergeAction, lc);
7849 : :
7850 : 52 : appendContextKeyword(context, " WHEN ",
7851 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
766 dean.a.rasheed@gmail 7852 [ + + + - ]: 52 : switch (action->matchKind)
7853 : : {
7854 : 24 : case MERGE_WHEN_MATCHED:
755 drowley@postgresql.o 7855 : 24 : appendStringInfoString(buf, "MATCHED");
766 dean.a.rasheed@gmail 7856 : 24 : break;
7857 : 4 : case MERGE_WHEN_NOT_MATCHED_BY_SOURCE:
755 drowley@postgresql.o 7858 : 4 : appendStringInfoString(buf, "NOT MATCHED BY SOURCE");
766 dean.a.rasheed@gmail 7859 : 4 : break;
7860 : 24 : case MERGE_WHEN_NOT_MATCHED_BY_TARGET:
7861 [ + + ]: 24 : if (haveNotMatchedBySource)
755 drowley@postgresql.o 7862 : 4 : appendStringInfoString(buf, "NOT MATCHED BY TARGET");
7863 : : else
7864 : 20 : appendStringInfoString(buf, "NOT MATCHED");
766 dean.a.rasheed@gmail 7865 : 24 : break;
766 dean.a.rasheed@gmail 7866 :UBC 0 : default:
7867 [ # # ]: 0 : elog(ERROR, "unrecognized matchKind: %d",
7868 : : (int) action->matchKind);
7869 : : }
7870 : :
1094 tgl@sss.pgh.pa.us 7871 [ + + ]:CBC 52 : if (action->qual)
7872 : : {
7873 : 32 : appendContextKeyword(context, " AND ",
7874 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 3);
7875 : 32 : get_rule_expr(action->qual, context, false);
7876 : : }
7877 : 52 : appendContextKeyword(context, " THEN ",
7878 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 3);
7879 : :
7880 [ + + ]: 52 : if (action->commandType == CMD_INSERT)
7881 : : {
7882 : : /* This generally matches get_insert_query_def() */
7883 : 24 : List *strippedexprs = NIL;
7884 : 24 : const char *sep = "";
7885 : : ListCell *lc2;
7886 : :
7887 : 24 : appendStringInfoString(buf, "INSERT");
7888 : :
7889 [ + + ]: 24 : if (action->targetList)
7890 : 20 : appendStringInfoString(buf, " (");
7891 [ + + + + : 68 : foreach(lc2, action->targetList)
+ + ]
7892 : : {
7893 : 44 : TargetEntry *tle = (TargetEntry *) lfirst(lc2);
7894 : :
7895 [ - + ]: 44 : Assert(!tle->resjunk);
7896 : :
7897 : 44 : appendStringInfoString(buf, sep);
7898 : 44 : sep = ", ";
7899 : :
7900 : 44 : appendStringInfoString(buf,
7901 : 44 : quote_identifier(get_attname(rte->relid,
7902 : 44 : tle->resno,
7903 : : false)));
7904 : 44 : strippedexprs = lappend(strippedexprs,
7905 : 44 : processIndirection((Node *) tle->expr,
7906 : : context));
7907 : : }
7908 [ + + ]: 24 : if (action->targetList)
7909 : 20 : appendStringInfoChar(buf, ')');
7910 : :
7911 [ + + ]: 24 : if (action->override)
7912 : : {
7913 [ - + ]: 4 : if (action->override == OVERRIDING_SYSTEM_VALUE)
1094 tgl@sss.pgh.pa.us 7914 :UBC 0 : appendStringInfoString(buf, " OVERRIDING SYSTEM VALUE");
1094 tgl@sss.pgh.pa.us 7915 [ + - ]:CBC 4 : else if (action->override == OVERRIDING_USER_VALUE)
7916 : 4 : appendStringInfoString(buf, " OVERRIDING USER VALUE");
7917 : : }
7918 : :
7919 [ + + ]: 24 : if (strippedexprs)
7920 : : {
7921 : 20 : appendContextKeyword(context, " VALUES (",
7922 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 4);
7923 : 20 : get_rule_list_toplevel(strippedexprs, context, false);
7924 : 20 : appendStringInfoChar(buf, ')');
7925 : : }
7926 : : else
7927 : 4 : appendStringInfoString(buf, " DEFAULT VALUES");
7928 : : }
7929 [ + + ]: 28 : else if (action->commandType == CMD_UPDATE)
7930 : : {
7931 : 16 : appendStringInfoString(buf, "UPDATE SET ");
7932 : 16 : get_update_query_targetlist_def(query, action->targetList,
7933 : : context, rte);
7934 : : }
7935 [ + + ]: 12 : else if (action->commandType == CMD_DELETE)
7936 : 8 : appendStringInfoString(buf, "DELETE");
7937 [ + - ]: 4 : else if (action->commandType == CMD_NOTHING)
7938 : 4 : appendStringInfoString(buf, "DO NOTHING");
7939 : : }
7940 : :
7941 : : /* Add RETURNING if present */
779 dean.a.rasheed@gmail 7942 [ + + ]: 8 : if (query->returningList)
474 7943 : 4 : get_returning_clause(query, context);
1094 tgl@sss.pgh.pa.us 7944 : 8 : }
7945 : :
7946 : :
7947 : : /* ----------
7948 : : * get_utility_query_def - Parse back a UTILITY parsetree
7949 : : * ----------
7950 : : */
7951 : : static void
9253 7952 : 9 : get_utility_query_def(Query *query, deparse_context *context)
7953 : : {
7954 : 9 : StringInfo buf = context->buf;
7955 : :
7956 [ + - + - ]: 9 : if (query->utilityStmt && IsA(query->utilityStmt, NotifyStmt))
7957 : 9 : {
7958 : 9 : NotifyStmt *stmt = (NotifyStmt *) query->utilityStmt;
7959 : :
8315 7960 : 9 : appendContextKeyword(context, "",
7961 : : 0, PRETTYINDENT_STD, 1);
8810 7962 : 9 : appendStringInfo(buf, "NOTIFY %s",
6455 7963 : 9 : quote_identifier(stmt->conditionname));
5922 7964 [ - + ]: 9 : if (stmt->payload)
7965 : : {
5922 tgl@sss.pgh.pa.us 7966 :UBC 0 : appendStringInfoString(buf, ", ");
7967 : 0 : simple_quote_literal(buf, stmt->payload);
7968 : : }
7969 : : }
7970 : : else
7971 : : {
7972 : : /* Currently only NOTIFY utility commands can appear in rules */
8318 7973 [ # # ]: 0 : elog(ERROR, "unexpected utility statement type");
7974 : : }
9253 tgl@sss.pgh.pa.us 7975 :CBC 9 : }
7976 : :
7977 : :
7978 : : /*
7979 : : * Parse back a graph label expression
7980 : : */
7981 : : static void
50 peter@eisentraut.org 7982 :GNC 96 : get_graph_label_expr(Node *label_expr, deparse_context *context)
7983 : : {
7984 : 96 : StringInfo buf = context->buf;
7985 : :
7986 : 96 : check_stack_depth();
7987 : :
7988 [ + + - ]: 96 : switch (nodeTag(label_expr))
7989 : : {
7990 : 78 : case T_GraphLabelRef:
7991 : : {
7992 : 78 : GraphLabelRef *lref = (GraphLabelRef *) label_expr;
7993 : :
7994 : 78 : appendStringInfoString(buf, quote_identifier(get_propgraph_label_name(lref->labelid)));
7995 : 78 : break;
7996 : : }
7997 : :
7998 : 18 : case T_BoolExpr:
7999 : : {
8000 : 18 : BoolExpr *be = (BoolExpr *) label_expr;
8001 : : ListCell *lc;
8002 : 18 : bool first = true;
8003 : :
8004 [ - + ]: 18 : Assert(be->boolop == OR_EXPR);
8005 : :
8006 [ + - + + : 54 : foreach(lc, be->args)
+ + ]
8007 : : {
8008 [ + + ]: 36 : if (!first)
8009 : : {
8010 [ + - ]: 18 : if (be->boolop == OR_EXPR)
22 drowley@postgresql.o 8011 : 18 : appendStringInfoChar(buf, '|');
8012 : : }
8013 : : else
50 peter@eisentraut.org 8014 : 18 : first = false;
8015 : 36 : get_graph_label_expr(lfirst(lc), context);
8016 : : }
8017 : :
8018 : 18 : break;
8019 : : }
8020 : :
50 peter@eisentraut.org 8021 :UNC 0 : default:
8022 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(label_expr));
8023 : : break;
8024 : : }
50 peter@eisentraut.org 8025 :GNC 96 : }
8026 : :
8027 : : /*
8028 : : * Parse back a path pattern expression
8029 : : */
8030 : : static void
8031 : 14 : get_path_pattern_expr_def(List *path_pattern_expr, deparse_context *context)
8032 : : {
8033 : 14 : StringInfo buf = context->buf;
8034 : : ListCell *lc;
8035 : :
8036 [ + - + + : 74 : foreach(lc, path_pattern_expr)
+ + ]
8037 : : {
8038 : 60 : GraphElementPattern *gep = lfirst_node(GraphElementPattern, lc);
8039 : 60 : const char *sep = "";
8040 : :
8041 [ + - + - : 60 : switch (gep->kind)
- ]
8042 : : {
8043 : 37 : case VERTEX_PATTERN:
22 drowley@postgresql.o 8044 : 37 : appendStringInfoChar(buf, '(');
50 peter@eisentraut.org 8045 : 37 : break;
50 peter@eisentraut.org 8046 :UNC 0 : case EDGE_PATTERN_LEFT:
8047 : 0 : appendStringInfoString(buf, "<-[");
8048 : 0 : break;
50 peter@eisentraut.org 8049 :GNC 23 : case EDGE_PATTERN_RIGHT:
8050 : : case EDGE_PATTERN_ANY:
8051 : 23 : appendStringInfoString(buf, "-[");
8052 : 23 : break;
50 peter@eisentraut.org 8053 :UNC 0 : case PAREN_EXPR:
22 drowley@postgresql.o 8054 : 0 : appendStringInfoChar(buf, '(');
50 peter@eisentraut.org 8055 : 0 : break;
8056 : : }
8057 : :
50 peter@eisentraut.org 8058 [ + + ]:GNC 60 : if (gep->variable)
8059 : : {
8060 : 37 : appendStringInfoString(buf, quote_identifier(gep->variable));
8061 : 37 : sep = " ";
8062 : : }
8063 : :
8064 [ + - ]: 60 : if (gep->labelexpr)
8065 : : {
8066 : 60 : appendStringInfoString(buf, sep);
8067 : 60 : appendStringInfoString(buf, "IS ");
8068 : 60 : get_graph_label_expr(gep->labelexpr, context);
8069 : 60 : sep = " ";
8070 : : }
8071 : :
8072 [ - + ]: 60 : if (gep->subexpr)
8073 : : {
50 peter@eisentraut.org 8074 :UNC 0 : appendStringInfoString(buf, sep);
8075 : 0 : get_path_pattern_expr_def(gep->subexpr, context);
8076 : 0 : sep = " ";
8077 : : }
8078 : :
50 peter@eisentraut.org 8079 [ + + ]:GNC 60 : if (gep->whereClause)
8080 : : {
8081 : 14 : appendStringInfoString(buf, sep);
8082 : 14 : appendStringInfoString(buf, "WHERE ");
8083 : 14 : get_rule_expr(gep->whereClause, context, false);
8084 : : }
8085 : :
8086 [ + - + - : 60 : switch (gep->kind)
- ]
8087 : : {
8088 : 37 : case VERTEX_PATTERN:
22 drowley@postgresql.o 8089 : 37 : appendStringInfoChar(buf, ')');
50 peter@eisentraut.org 8090 : 37 : break;
50 peter@eisentraut.org 8091 :UNC 0 : case EDGE_PATTERN_LEFT:
8092 : : case EDGE_PATTERN_ANY:
8093 : 0 : appendStringInfoString(buf, "]-");
8094 : 0 : break;
50 peter@eisentraut.org 8095 :GNC 23 : case EDGE_PATTERN_RIGHT:
8096 : 23 : appendStringInfoString(buf, "]->");
8097 : 23 : break;
50 peter@eisentraut.org 8098 :UNC 0 : case PAREN_EXPR:
22 drowley@postgresql.o 8099 : 0 : appendStringInfoChar(buf, ')');
50 peter@eisentraut.org 8100 : 0 : break;
8101 : : }
8102 : :
50 peter@eisentraut.org 8103 [ - + ]:GNC 60 : if (gep->quantifier)
8104 : : {
50 peter@eisentraut.org 8105 :UNC 0 : int lower = linitial_int(gep->quantifier);
8106 : 0 : int upper = lsecond_int(gep->quantifier);
8107 : :
8108 : 0 : appendStringInfo(buf, "{%d,%d}", lower, upper);
8109 : : }
8110 : : }
50 peter@eisentraut.org 8111 :GNC 14 : }
8112 : :
8113 : : /*
8114 : : * Parse back a graph pattern
8115 : : */
8116 : : static void
8117 : 14 : get_graph_pattern_def(GraphPattern *graph_pattern, deparse_context *context)
8118 : : {
8119 : 14 : StringInfo buf = context->buf;
8120 : : ListCell *lc;
8121 : 14 : bool first = true;
8122 : :
8123 [ + - + + : 28 : foreach(lc, graph_pattern->path_pattern_list)
+ + ]
8124 : : {
8125 : 14 : List *path_pattern_expr = lfirst_node(List, lc);
8126 : :
8127 [ - + ]: 14 : if (!first)
50 peter@eisentraut.org 8128 :UNC 0 : appendStringInfoString(buf, ", ");
8129 : : else
50 peter@eisentraut.org 8130 :GNC 14 : first = false;
8131 : :
8132 : 14 : get_path_pattern_expr_def(path_pattern_expr, context);
8133 : : }
8134 : :
8135 [ - + ]: 14 : if (graph_pattern->whereClause)
8136 : : {
50 peter@eisentraut.org 8137 :UNC 0 : appendStringInfoString(buf, "WHERE ");
8138 : 0 : get_rule_expr(graph_pattern->whereClause, context, false);
8139 : : }
50 peter@eisentraut.org 8140 :GNC 14 : }
8141 : :
8142 : : /*
8143 : : * Display a Var appropriately.
8144 : : *
8145 : : * In some cases (currently only when recursing into an unnamed join)
8146 : : * the Var's varlevelsup has to be interpreted with respect to a context
8147 : : * above the current one; levelsup indicates the offset.
8148 : : *
8149 : : * If istoplevel is true, the Var is at the top level of a SELECT's
8150 : : * targetlist, which means we need special treatment of whole-row Vars.
8151 : : * Instead of the normal "tab.*", we'll print "tab.*::typename", which is a
8152 : : * dirty hack to prevent "tab.*" from being expanded into multiple columns.
8153 : : * (The parser will strip the useless coercion, so no inefficiency is added in
8154 : : * dump and reload.) We used to print just "tab" in such cases, but that is
8155 : : * ambiguous and will yield the wrong result if "tab" is also a plain column
8156 : : * name in the query.
8157 : : *
8158 : : * Returns the attname of the Var, or NULL if the Var has no attname (because
8159 : : * it is a whole-row Var or a subplan output reference).
8160 : : */
8161 : : static char *
5121 tgl@sss.pgh.pa.us 8162 :CBC 125250 : get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
8163 : : {
7011 8164 : 125250 : StringInfo buf = context->buf;
8165 : : RangeTblEntry *rte;
8166 : : AttrNumber attnum;
8167 : : int netlevelsup;
8168 : : deparse_namespace *dpns;
8169 : : int varno;
8170 : : AttrNumber varattno;
8171 : : deparse_columns *colinfo;
8172 : : char *refname;
8173 : : char *attname;
8174 : : bool need_prefix;
8175 : :
8176 : : /* Find appropriate nesting depth */
7782 8177 : 125250 : netlevelsup = var->varlevelsup + levelsup;
8178 [ - + ]: 125250 : if (netlevelsup >= list_length(context->namespaces))
7782 tgl@sss.pgh.pa.us 8179 [ # # ]:UBC 0 : elog(ERROR, "bogus varlevelsup: %d offset %d",
8180 : : var->varlevelsup, levelsup);
8014 tgl@sss.pgh.pa.us 8181 :CBC 125250 : dpns = (deparse_namespace *) list_nth(context->namespaces,
8182 : : netlevelsup);
8183 : :
8184 : : /*
8185 : : * If we have a syntactic referent for the Var, and we're working from a
8186 : : * parse tree, prefer to use the syntactic referent. Otherwise, fall back
8187 : : * on the semantic referent. (Forcing use of the semantic referent when
8188 : : * printing plan trees is a design choice that's perhaps more motivated by
8189 : : * backwards compatibility than anything else. But it does have the
8190 : : * advantage of making plans more explicit.)
8191 : : */
2308 8192 [ + + + + ]: 125250 : if (var->varnosyn > 0 && dpns->plan == NULL)
8193 : : {
8194 : 24596 : varno = var->varnosyn;
8195 : 24596 : varattno = var->varattnosyn;
8196 : : }
8197 : : else
8198 : : {
8199 : 100654 : varno = var->varno;
8200 : 100654 : varattno = var->varattno;
8201 : : }
8202 : :
8203 : : /*
8204 : : * Try to find the relevant RTE in this rtable. In a plan tree, it's
8205 : : * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
8206 : : * down into the subplans, or INDEX_VAR, which is resolved similarly. Also
8207 : : * find the aliases previously assigned for this RTE.
8208 : : */
8209 [ + + + - ]: 125250 : if (varno >= 1 && varno <= list_length(dpns->rtable))
8210 : : {
8211 : : /*
8212 : : * We might have been asked to map child Vars to some parent relation.
8213 : : */
2337 8214 [ + + + + ]: 91107 : if (context->appendparents && dpns->appendrels)
8215 : : {
1693 8216 : 2517 : int pvarno = varno;
2337 8217 : 2517 : AttrNumber pvarattno = varattno;
8218 : 2517 : AppendRelInfo *appinfo = dpns->appendrels[pvarno];
8219 : 2517 : bool found = false;
8220 : :
8221 : : /* Only map up to inheritance parents, not UNION ALL appendrels */
8222 [ + + ]: 5080 : while (appinfo &&
8223 : 2794 : rt_fetch(appinfo->parent_relid,
8224 [ + + ]: 2794 : dpns->rtable)->rtekind == RTE_RELATION)
8225 : : {
8226 : 2563 : found = false;
8227 [ + + ]: 2563 : if (pvarattno > 0) /* system columns stay as-is */
8228 : : {
8229 [ - + ]: 2411 : if (pvarattno > appinfo->num_child_cols)
2337 tgl@sss.pgh.pa.us 8230 :UBC 0 : break; /* safety check */
2337 tgl@sss.pgh.pa.us 8231 :CBC 2411 : pvarattno = appinfo->parent_colnos[pvarattno - 1];
8232 [ - + ]: 2411 : if (pvarattno == 0)
2337 tgl@sss.pgh.pa.us 8233 :UBC 0 : break; /* Var is local to child */
8234 : : }
8235 : :
2337 tgl@sss.pgh.pa.us 8236 :CBC 2563 : pvarno = appinfo->parent_relid;
8237 : 2563 : found = true;
8238 : :
8239 : : /* If the parent is itself a child, continue up. */
8240 [ + - - + ]: 2563 : Assert(pvarno > 0 && pvarno <= list_length(dpns->rtable));
8241 : 2563 : appinfo = dpns->appendrels[pvarno];
8242 : : }
8243 : :
8244 : : /*
8245 : : * If we found an ancestral rel, and that rel is included in
8246 : : * appendparents, print that column not the original one.
8247 : : */
8248 [ + + + + ]: 2517 : if (found && bms_is_member(pvarno, context->appendparents))
8249 : : {
8250 : 2038 : varno = pvarno;
8251 : 2038 : varattno = pvarattno;
8252 : : }
8253 : : }
8254 : :
8255 : 91107 : rte = rt_fetch(varno, dpns->rtable);
8256 : :
8257 : : /* might be returning old/new column value */
474 dean.a.rasheed@gmail 8258 [ + + ]: 91107 : if (var->varreturningtype == VAR_RETURNING_OLD)
8259 : 274 : refname = dpns->ret_old_alias;
8260 [ + + ]: 90833 : else if (var->varreturningtype == VAR_RETURNING_NEW)
8261 : 273 : refname = dpns->ret_new_alias;
8262 : : else
8263 : 90560 : refname = (char *) list_nth(dpns->rtable_names, varno - 1);
8264 : :
2337 tgl@sss.pgh.pa.us 8265 : 91107 : colinfo = deparse_columns_fetch(varno, dpns);
8266 : 91107 : attnum = varattno;
8267 : : }
8268 : : else
8269 : : {
8270 : 34143 : resolve_special_varno((Node *) var, context,
8271 : : get_special_variable, NULL);
3660 rhaas@postgresql.org 8272 : 34143 : return NULL;
8273 : : }
8274 : :
8275 : : /*
8276 : : * The planner will sometimes emit Vars referencing resjunk elements of a
8277 : : * subquery's target list (this is currently only possible if it chooses
8278 : : * to generate a "physical tlist" for a SubqueryScan or CteScan node).
8279 : : * Although we prefer to print subquery-referencing Vars using the
8280 : : * subquery's alias, that's not possible for resjunk items since they have
8281 : : * no alias. So in that case, drill down to the subplan and print the
8282 : : * contents of the referenced tlist item. This works because in a plan
8283 : : * tree, such Vars can only occur in a SubqueryScan or CteScan node, and
8284 : : * we'll have set dpns->inner_plan to reference the child plan node.
8285 : : */
5779 tgl@sss.pgh.pa.us 8286 [ + + + + : 94130 : if ((rte->rtekind == RTE_SUBQUERY || rte->rtekind == RTE_CTE) &&
+ + ]
8287 : 3023 : attnum > list_length(rte->eref->colnames) &&
2337 8288 [ + - ]: 1 : dpns->inner_plan)
8289 : : {
8290 : : TargetEntry *tle;
8291 : : deparse_namespace save_dpns;
8292 : :
8293 : 1 : tle = get_tle_by_resno(dpns->inner_tlist, attnum);
5779 8294 [ - + ]: 1 : if (!tle)
3595 tgl@sss.pgh.pa.us 8295 [ # # ]:UBC 0 : elog(ERROR, "invalid attnum %d for relation \"%s\"",
8296 : : attnum, rte->eref->aliasname);
8297 : :
5779 tgl@sss.pgh.pa.us 8298 [ - + ]:CBC 1 : Assert(netlevelsup == 0);
2337 8299 : 1 : push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8300 : :
8301 : : /*
8302 : : * Force parentheses because our caller probably assumed a Var is a
8303 : : * simple expression.
8304 : : */
5779 8305 [ - + ]: 1 : if (!IsA(tle->expr, Var))
5779 tgl@sss.pgh.pa.us 8306 :UBC 0 : appendStringInfoChar(buf, '(');
5779 tgl@sss.pgh.pa.us 8307 :CBC 1 : get_rule_expr((Node *) tle->expr, context, true);
8308 [ - + ]: 1 : if (!IsA(tle->expr, Var))
5779 tgl@sss.pgh.pa.us 8309 :UBC 0 : appendStringInfoChar(buf, ')');
8310 : :
5775 tgl@sss.pgh.pa.us 8311 :CBC 1 : pop_child_plan(dpns, &save_dpns);
5779 8312 : 1 : return NULL;
8313 : : }
8314 : :
8315 : : /*
8316 : : * If it's an unnamed join, look at the expansion of the alias variable.
8317 : : * If it's a simple reference to one of the input vars, then recursively
8318 : : * print the name of that var instead. When it's not a simple reference,
8319 : : * we have to just print the unqualified join column name. (This can only
8320 : : * happen with "dangerous" merged columns in a JOIN USING; we took pains
8321 : : * previously to make the unqualified column name unique in such cases.)
8322 : : *
8323 : : * This wouldn't work in decompiling plan trees, because we don't store
8324 : : * joinaliasvars lists after planning; but a plan tree should never
8325 : : * contain a join alias variable.
8326 : : */
4974 8327 [ + + + + ]: 91106 : if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
8328 : : {
8329 [ - + ]: 72 : if (rte->joinaliasvars == NIL)
4974 tgl@sss.pgh.pa.us 8330 [ # # ]:UBC 0 : elog(ERROR, "cannot decompile join alias var in plan tree");
4974 tgl@sss.pgh.pa.us 8331 [ + - ]:CBC 72 : if (attnum > 0)
8332 : : {
8333 : : Var *aliasvar;
8334 : :
8335 : 72 : aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
8336 : : /* we intentionally don't strip implicit coercions here */
4669 8337 [ + - - + ]: 72 : if (aliasvar && IsA(aliasvar, Var))
8338 : : {
4974 tgl@sss.pgh.pa.us 8339 :UBC 0 : return get_variable(aliasvar, var->varlevelsup + levelsup,
8340 : : istoplevel, context);
8341 : : }
8342 : : }
8343 : :
8344 : : /*
8345 : : * Unnamed join has no refname. (Note: since it's unnamed, there is
8346 : : * no way the user could have referenced it to create a whole-row Var
8347 : : * for it. So we don't have to cover that case below.)
8348 : : */
4974 tgl@sss.pgh.pa.us 8349 [ - + ]:CBC 72 : Assert(refname == NULL);
8350 : : }
8351 : :
7332 8352 [ + + ]: 91106 : if (attnum == InvalidAttrNumber)
7011 8353 : 627 : attname = NULL;
4873 8354 [ + + ]: 90479 : else if (attnum > 0)
8355 : : {
8356 : : /* Get column name to use from the colinfo struct */
3595 8357 [ - + ]: 89259 : if (attnum > colinfo->num_cols)
3595 tgl@sss.pgh.pa.us 8358 [ # # ]:UBC 0 : elog(ERROR, "invalid attnum %d for relation \"%s\"",
8359 : : attnum, rte->eref->aliasname);
4873 tgl@sss.pgh.pa.us 8360 :CBC 89259 : attname = colinfo->colnames[attnum - 1];
8361 : :
8362 : : /*
8363 : : * If we find a Var referencing a dropped column, it seems better to
8364 : : * print something (anything) than to fail. In general this should
8365 : : * not happen, but it used to be possible for some cases involving
8366 : : * functions returning named composite types, and perhaps there are
8367 : : * still bugs out there.
8368 : : */
1384 8369 [ + + ]: 89259 : if (attname == NULL)
8370 : 4 : attname = "?dropped?column?";
8371 : : }
8372 : : else
8373 : : {
8374 : : /* System column - name is fixed, get it from the catalog */
7011 8375 : 1220 : attname = get_rte_attribute_name(rte, attnum);
8376 : : }
8377 : :
474 dean.a.rasheed@gmail 8378 [ + + + + ]: 134182 : need_prefix = (context->varprefix || attname == NULL ||
8379 [ + + ]: 43076 : var->varreturningtype != VAR_RETURNING_DEFAULT);
8380 : :
8381 : : /*
8382 : : * If we're considering a plain Var in an ORDER BY (but not GROUP BY)
8383 : : * clause, we may need to add a table-name prefix to prevent
8384 : : * findTargetlistEntrySQL92 from misinterpreting the name as an
8385 : : * output-column name. To avoid cluttering the output with unnecessary
8386 : : * prefixes, do so only if there is a name match to a SELECT tlist item
8387 : : * that is different from the Var.
8388 : : */
614 tgl@sss.pgh.pa.us 8389 [ + + + + : 91106 : if (context->varInOrderBy && !context->inGroupBy && !need_prefix)
+ + ]
8390 : : {
8391 : 155 : int colno = 0;
8392 : :
8393 [ + + + + : 613 : foreach_node(TargetEntry, tle, context->targetList)
+ + ]
8394 : : {
8395 : : char *colname;
8396 : :
8397 [ - + ]: 311 : if (tle->resjunk)
614 tgl@sss.pgh.pa.us 8398 :UBC 0 : continue; /* ignore junk entries */
614 tgl@sss.pgh.pa.us 8399 :CBC 311 : colno++;
8400 : :
8401 : : /* This must match colname-choosing logic in get_target_list() */
8402 [ + - + - ]: 311 : if (context->resultDesc && colno <= context->resultDesc->natts)
8403 : 311 : colname = NameStr(TupleDescAttr(context->resultDesc,
8404 : : colno - 1)->attname);
8405 : : else
614 tgl@sss.pgh.pa.us 8406 :UBC 0 : colname = tle->resname;
8407 : :
614 tgl@sss.pgh.pa.us 8408 [ + - + + ]:CBC 311 : if (colname && strcmp(colname, attname) == 0 &&
8409 [ + + ]: 106 : !equal(var, tle->expr))
8410 : : {
8411 : 8 : need_prefix = true;
8412 : 8 : break;
8413 : : }
8414 : : }
8415 : : }
8416 : :
8417 [ + + + + ]: 91106 : if (refname && need_prefix)
8418 : : {
6025 8419 : 47979 : appendStringInfoString(buf, quote_identifier(refname));
5121 8420 : 47979 : appendStringInfoChar(buf, '.');
8421 : : }
7011 8422 [ + + ]: 91106 : if (attname)
8423 : 90479 : appendStringInfoString(buf, quote_identifier(attname));
8424 : : else
8425 : : {
8426 : 627 : appendStringInfoChar(buf, '*');
5121 8427 [ + + ]: 627 : if (istoplevel)
8428 : 56 : appendStringInfo(buf, "::%s",
8429 : : format_type_with_typemod(var->vartype,
8430 : : var->vartypmod));
8431 : : }
8432 : :
7011 8433 : 91106 : return attname;
8434 : : }
8435 : :
8436 : : /*
8437 : : * Deparse a Var which references OUTER_VAR, INNER_VAR, or INDEX_VAR. This
8438 : : * routine is actually a callback for resolve_special_varno, which handles
8439 : : * finding the correct TargetEntry. We get the expression contained in that
8440 : : * TargetEntry and just need to deparse it, a job we can throw back on
8441 : : * get_rule_expr.
8442 : : */
8443 : : static void
2337 8444 : 34143 : get_special_variable(Node *node, deparse_context *context, void *callback_arg)
8445 : : {
3660 rhaas@postgresql.org 8446 : 34143 : StringInfo buf = context->buf;
8447 : :
8448 : : /*
8449 : : * For a non-Var referent, force parentheses because our caller probably
8450 : : * assumed a Var is a simple expression.
8451 : : */
8452 [ + + ]: 34143 : if (!IsA(node, Var))
8453 : 3483 : appendStringInfoChar(buf, '(');
8454 : 34143 : get_rule_expr(node, context, true);
8455 [ + + ]: 34143 : if (!IsA(node, Var))
8456 : 3483 : appendStringInfoChar(buf, ')');
8457 : 34143 : }
8458 : :
8459 : : /*
8460 : : * Chase through plan references to special varnos (OUTER_VAR, INNER_VAR,
8461 : : * INDEX_VAR) until we find a real Var or some kind of non-Var node; then,
8462 : : * invoke the callback provided.
8463 : : */
8464 : : static void
2337 tgl@sss.pgh.pa.us 8465 : 96265 : resolve_special_varno(Node *node, deparse_context *context,
8466 : : rsv_callback callback, void *callback_arg)
8467 : : {
8468 : : Var *var;
8469 : : deparse_namespace *dpns;
8470 : :
8471 : : /* This function is recursive, so let's be paranoid. */
8472 : 96265 : check_stack_depth();
8473 : :
8474 : : /* If it's not a Var, invoke the callback. */
3660 rhaas@postgresql.org 8475 [ + + ]: 96265 : if (!IsA(node, Var))
8476 : : {
2337 tgl@sss.pgh.pa.us 8477 : 4003 : (*callback) (node, context, callback_arg);
3660 rhaas@postgresql.org 8478 : 4003 : return;
8479 : : }
8480 : :
8481 : : /* Find appropriate nesting depth */
8482 : 92262 : var = (Var *) node;
8483 : 92262 : dpns = (deparse_namespace *) list_nth(context->namespaces,
8484 : 92262 : var->varlevelsup);
8485 : :
8486 : : /*
8487 : : * If varno is special, recurse. (Don't worry about varnosyn; if we're
8488 : : * here, we already decided not to use that.)
8489 : : */
8490 [ + + + - ]: 92262 : if (var->varno == OUTER_VAR && dpns->outer_tlist)
8491 : : {
8492 : : TargetEntry *tle;
8493 : : deparse_namespace save_dpns;
8494 : : Bitmapset *save_appendparents;
8495 : :
8496 : 46807 : tle = get_tle_by_resno(dpns->outer_tlist, var->varattno);
8497 [ - + ]: 46807 : if (!tle)
3660 rhaas@postgresql.org 8498 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for OUTER_VAR var: %d", var->varattno);
8499 : :
8500 : : /*
8501 : : * If we're descending to the first child of an Append or MergeAppend,
8502 : : * update appendparents. This will affect deparsing of all Vars
8503 : : * appearing within the eventually-resolved subexpression.
8504 : : */
2337 tgl@sss.pgh.pa.us 8505 :CBC 46807 : save_appendparents = context->appendparents;
8506 : :
8507 [ + + ]: 46807 : if (IsA(dpns->plan, Append))
8508 : 2967 : context->appendparents = bms_union(context->appendparents,
8509 : 2967 : ((Append *) dpns->plan)->apprelids);
8510 [ + + ]: 43840 : else if (IsA(dpns->plan, MergeAppend))
8511 : 413 : context->appendparents = bms_union(context->appendparents,
8512 : 413 : ((MergeAppend *) dpns->plan)->apprelids);
8513 : :
8514 : 46807 : push_child_plan(dpns, dpns->outer_plan, &save_dpns);
8515 : 46807 : resolve_special_varno((Node *) tle->expr, context,
8516 : : callback, callback_arg);
3660 rhaas@postgresql.org 8517 : 46807 : pop_child_plan(dpns, &save_dpns);
2337 tgl@sss.pgh.pa.us 8518 : 46807 : context->appendparents = save_appendparents;
3660 rhaas@postgresql.org 8519 : 46807 : return;
8520 : : }
8521 [ + + + - ]: 45455 : else if (var->varno == INNER_VAR && dpns->inner_tlist)
8522 : : {
8523 : : TargetEntry *tle;
8524 : : deparse_namespace save_dpns;
8525 : :
8526 : 11397 : tle = get_tle_by_resno(dpns->inner_tlist, var->varattno);
8527 [ - + ]: 11397 : if (!tle)
3660 rhaas@postgresql.org 8528 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for INNER_VAR var: %d", var->varattno);
8529 : :
2337 tgl@sss.pgh.pa.us 8530 :CBC 11397 : push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8531 : 11397 : resolve_special_varno((Node *) tle->expr, context,
8532 : : callback, callback_arg);
3660 rhaas@postgresql.org 8533 : 11397 : pop_child_plan(dpns, &save_dpns);
8534 : 11397 : return;
8535 : : }
8536 [ + + + - ]: 34058 : else if (var->varno == INDEX_VAR && dpns->index_tlist)
8537 : : {
8538 : : TargetEntry *tle;
8539 : :
8540 : 3398 : tle = get_tle_by_resno(dpns->index_tlist, var->varattno);
8541 [ - + ]: 3398 : if (!tle)
3660 rhaas@postgresql.org 8542 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for INDEX_VAR var: %d", var->varattno);
8543 : :
2337 tgl@sss.pgh.pa.us 8544 :CBC 3398 : resolve_special_varno((Node *) tle->expr, context,
8545 : : callback, callback_arg);
3660 rhaas@postgresql.org 8546 : 3398 : return;
8547 : : }
8548 [ + - - + ]: 30660 : else if (var->varno < 1 || var->varno > list_length(dpns->rtable))
3660 rhaas@postgresql.org 8549 [ # # ]:UBC 0 : elog(ERROR, "bogus varno: %d", var->varno);
8550 : :
8551 : : /* Not special. Just invoke the callback. */
2337 tgl@sss.pgh.pa.us 8552 :CBC 30660 : (*callback) (node, context, callback_arg);
8553 : : }
8554 : :
8555 : : /*
8556 : : * Get the name of a field of an expression of composite type. The
8557 : : * expression is usually a Var, but we handle other cases too.
8558 : : *
8559 : : * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
8560 : : *
8561 : : * This is fairly straightforward when the expression has a named composite
8562 : : * type; we need only look up the type in the catalogs. However, the type
8563 : : * could also be RECORD. Since no actual table or view column is allowed to
8564 : : * have type RECORD, a Var of type RECORD must refer to a JOIN or FUNCTION RTE
8565 : : * or to a subquery output. We drill down to find the ultimate defining
8566 : : * expression and attempt to infer the field name from it. We ereport if we
8567 : : * can't determine the name.
8568 : : *
8569 : : * Similarly, a PARAM of type RECORD has to refer to some expression of
8570 : : * a determinable composite type.
8571 : : */
8572 : : static const char *
7644 8573 : 1050 : get_name_for_var_field(Var *var, int fieldno,
8574 : : int levelsup, deparse_context *context)
8575 : : {
8576 : : RangeTblEntry *rte;
8577 : : AttrNumber attnum;
8578 : : int netlevelsup;
8579 : : deparse_namespace *dpns;
8580 : : int varno;
8581 : : AttrNumber varattno;
8582 : : TupleDesc tupleDesc;
8583 : : Node *expr;
8584 : :
8585 : : /*
8586 : : * If it's a RowExpr that was expanded from a whole-row Var, use the
8587 : : * column names attached to it. (We could let get_expr_result_tupdesc()
8588 : : * handle this, but it's much cheaper to just pull out the name we need.)
8589 : : */
6420 8590 [ + + ]: 1050 : if (IsA(var, RowExpr))
8591 : : {
6172 bruce@momjian.us 8592 : 24 : RowExpr *r = (RowExpr *) var;
8593 : :
6420 tgl@sss.pgh.pa.us 8594 [ + - + - ]: 24 : if (fieldno > 0 && fieldno <= list_length(r->colnames))
8595 : 24 : return strVal(list_nth(r->colnames, fieldno - 1));
8596 : : }
8597 : :
8598 : : /*
8599 : : * If it's a Param of type RECORD, try to find what the Param refers to.
8600 : : */
5354 8601 [ + + ]: 1026 : if (IsA(var, Param))
8602 : : {
8603 : 12 : Param *param = (Param *) var;
8604 : : ListCell *ancestor_cell;
8605 : :
8606 : 12 : expr = find_param_referent(param, context, &dpns, &ancestor_cell);
8607 [ + - ]: 12 : if (expr)
8608 : : {
8609 : : /* Found a match, so recurse to decipher the field name */
8610 : : deparse_namespace save_dpns;
8611 : : const char *result;
8612 : :
8613 : 12 : push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
8614 : 12 : result = get_name_for_var_field((Var *) expr, fieldno,
8615 : : 0, context);
8616 : 12 : pop_ancestor_plan(dpns, &save_dpns);
8617 : 12 : return result;
8618 : : }
8619 : : }
8620 : :
8621 : : /*
8622 : : * If it's a Var of type RECORD, we have to find what the Var refers to;
8623 : : * if not, we can use get_expr_result_tupdesc().
8624 : : */
7011 8625 [ + + ]: 1014 : if (!IsA(var, Var) ||
8626 [ + + ]: 961 : var->vartype != RECORDOID)
8627 : : {
3113 8628 : 850 : tupleDesc = get_expr_result_tupdesc((Node *) var, false);
8629 : : /* Got the tupdesc, so we can extract the field name */
7011 8630 [ + - - + ]: 850 : Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
3180 andres@anarazel.de 8631 : 850 : return NameStr(TupleDescAttr(tupleDesc, fieldno - 1)->attname);
8632 : : }
8633 : :
8634 : : /* Find appropriate nesting depth */
7011 tgl@sss.pgh.pa.us 8635 : 164 : netlevelsup = var->varlevelsup + levelsup;
8636 [ - + ]: 164 : if (netlevelsup >= list_length(context->namespaces))
7011 tgl@sss.pgh.pa.us 8637 [ # # ]:UBC 0 : elog(ERROR, "bogus varlevelsup: %d offset %d",
8638 : : var->varlevelsup, levelsup);
7011 tgl@sss.pgh.pa.us 8639 :CBC 164 : dpns = (deparse_namespace *) list_nth(context->namespaces,
8640 : : netlevelsup);
8641 : :
8642 : : /*
8643 : : * If we have a syntactic referent for the Var, and we're working from a
8644 : : * parse tree, prefer to use the syntactic referent. Otherwise, fall back
8645 : : * on the semantic referent. (See comments in get_variable().)
8646 : : */
2308 8647 [ + + + + ]: 164 : if (var->varnosyn > 0 && dpns->plan == NULL)
8648 : : {
8649 : 64 : varno = var->varnosyn;
8650 : 64 : varattno = var->varattnosyn;
8651 : : }
8652 : : else
8653 : : {
8654 : 100 : varno = var->varno;
8655 : 100 : varattno = var->varattno;
8656 : : }
8657 : :
8658 : : /*
8659 : : * Try to find the relevant RTE in this rtable. In a plan tree, it's
8660 : : * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
8661 : : * down into the subplans, or INDEX_VAR, which is resolved similarly.
8662 : : *
8663 : : * Note: unlike get_variable and resolve_special_varno, we need not worry
8664 : : * about inheritance mapping: a child Var should have the same datatype as
8665 : : * its parent, and here we're really only interested in the Var's type.
8666 : : */
8667 [ + + + - ]: 164 : if (varno >= 1 && varno <= list_length(dpns->rtable))
8668 : : {
8669 : 112 : rte = rt_fetch(varno, dpns->rtable);
8670 : 112 : attnum = varattno;
8671 : : }
8672 [ + + + - ]: 52 : else if (varno == OUTER_VAR && dpns->outer_tlist)
8673 : : {
8674 : : TargetEntry *tle;
8675 : : deparse_namespace save_dpns;
8676 : : const char *result;
8677 : :
8678 : 40 : tle = get_tle_by_resno(dpns->outer_tlist, varattno);
7011 8679 [ - + ]: 40 : if (!tle)
2308 tgl@sss.pgh.pa.us 8680 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for OUTER_VAR var: %d", varattno);
8681 : :
7011 tgl@sss.pgh.pa.us 8682 [ - + ]:CBC 40 : Assert(netlevelsup == 0);
2337 8683 : 40 : push_child_plan(dpns, dpns->outer_plan, &save_dpns);
8684 : :
7011 8685 : 40 : result = get_name_for_var_field((Var *) tle->expr, fieldno,
8686 : : levelsup, context);
8687 : :
5775 8688 : 40 : pop_child_plan(dpns, &save_dpns);
7011 8689 : 40 : return result;
8690 : : }
2308 8691 [ + - + - ]: 12 : else if (varno == INNER_VAR && dpns->inner_tlist)
8692 : : {
8693 : : TargetEntry *tle;
8694 : : deparse_namespace save_dpns;
8695 : : const char *result;
8696 : :
8697 : 12 : tle = get_tle_by_resno(dpns->inner_tlist, varattno);
7011 8698 [ - + ]: 12 : if (!tle)
2308 tgl@sss.pgh.pa.us 8699 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for INNER_VAR var: %d", varattno);
8700 : :
7011 tgl@sss.pgh.pa.us 8701 [ - + ]:CBC 12 : Assert(netlevelsup == 0);
2337 8702 : 12 : push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8703 : :
7011 8704 : 12 : result = get_name_for_var_field((Var *) tle->expr, fieldno,
8705 : : levelsup, context);
8706 : :
5775 8707 : 12 : pop_child_plan(dpns, &save_dpns);
7011 8708 : 12 : return result;
8709 : : }
2308 tgl@sss.pgh.pa.us 8710 [ # # # # ]:UBC 0 : else if (varno == INDEX_VAR && dpns->index_tlist)
8711 : : {
8712 : : TargetEntry *tle;
8713 : : const char *result;
8714 : :
8715 : 0 : tle = get_tle_by_resno(dpns->index_tlist, varattno);
5320 8716 [ # # ]: 0 : if (!tle)
2308 8717 [ # # ]: 0 : elog(ERROR, "bogus varattno for INDEX_VAR var: %d", varattno);
8718 : :
5320 8719 [ # # ]: 0 : Assert(netlevelsup == 0);
8720 : :
8721 : 0 : result = get_name_for_var_field((Var *) tle->expr, fieldno,
8722 : : levelsup, context);
8723 : :
8724 : 0 : return result;
8725 : : }
8726 : : else
8727 : : {
2308 8728 [ # # ]: 0 : elog(ERROR, "bogus varno: %d", varno);
8729 : : return NULL; /* keep compiler quiet */
8730 : : }
8731 : :
7644 tgl@sss.pgh.pa.us 8732 [ + + ]:CBC 112 : if (attnum == InvalidAttrNumber)
8733 : : {
8734 : : /* Var is whole-row reference to RTE, so select the right field */
8735 : 16 : return get_rte_attribute_name(rte, fieldno);
8736 : : }
8737 : :
8738 : : /*
8739 : : * This part has essentially the same logic as the parser's
8740 : : * expandRecordVariable() function, but we are dealing with a different
8741 : : * representation of the input context, and we only need one field name
8742 : : * not a TupleDesc. Also, we need special cases for finding subquery and
8743 : : * CTE subplans when deparsing Plan trees.
8744 : : */
8745 : 96 : expr = (Node *) var; /* default if we can't drill down */
8746 : :
8747 [ - + - - : 96 : switch (rte->rtekind)
+ - - ]
8748 : : {
7644 tgl@sss.pgh.pa.us 8749 :UBC 0 : case RTE_RELATION:
8750 : : case RTE_VALUES:
8751 : : case RTE_NAMEDTUPLESTORE:
8752 : : case RTE_GRAPH_TABLE:
8753 : : case RTE_RESULT:
8754 : :
8755 : : /*
8756 : : * This case should not occur: a column of a table, values list,
8757 : : * or ENR shouldn't have type RECORD. Fall through and fail (most
8758 : : * likely) at the bottom.
8759 : : */
8760 : 0 : break;
7644 tgl@sss.pgh.pa.us 8761 :CBC 48 : case RTE_SUBQUERY:
8762 : : /* Subselect-in-FROM: examine sub-select's output expr */
8763 : : {
7011 8764 [ + + ]: 48 : if (rte->subquery)
8765 : : {
8766 : 28 : TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
8767 : : attnum);
8768 : :
8769 [ + - - + ]: 28 : if (ste == NULL || ste->resjunk)
7011 tgl@sss.pgh.pa.us 8770 [ # # ]:UBC 0 : elog(ERROR, "subquery %s does not have attribute %d",
8771 : : rte->eref->aliasname, attnum);
7011 tgl@sss.pgh.pa.us 8772 :CBC 28 : expr = (Node *) ste->expr;
8773 [ + + ]: 28 : if (IsA(expr, Var))
8774 : : {
8775 : : /*
8776 : : * Recurse into the sub-select to see what its Var
8777 : : * refers to. We have to build an additional level of
8778 : : * namespace to keep in step with varlevelsup in the
8779 : : * subselect; furthermore, the subquery RTE might be
8780 : : * from an outer query level, in which case the
8781 : : * namespace for the subselect must have that outer
8782 : : * level as parent namespace.
8783 : : */
963 8784 : 12 : List *save_nslist = context->namespaces;
8785 : : List *parent_namespaces;
8786 : : deparse_namespace mydpns;
8787 : : const char *result;
8788 : :
8789 : 12 : parent_namespaces = list_copy_tail(context->namespaces,
8790 : : netlevelsup);
8791 : :
4873 8792 : 12 : set_deparse_for_query(&mydpns, rte->subquery,
8793 : : parent_namespaces);
8794 : :
963 8795 : 12 : context->namespaces = lcons(&mydpns, parent_namespaces);
8796 : :
7011 8797 : 12 : result = get_name_for_var_field((Var *) expr, fieldno,
8798 : : 0, context);
8799 : :
963 8800 : 12 : context->namespaces = save_nslist;
8801 : :
7011 8802 : 12 : return result;
8803 : : }
8804 : : /* else fall through to inspect the expression */
8805 : : }
8806 : : else
8807 : : {
8808 : : /*
8809 : : * We're deparsing a Plan tree so we don't have complete
8810 : : * RTE entries (in particular, rte->subquery is NULL). But
8811 : : * the only place we'd normally see a Var directly
8812 : : * referencing a SUBQUERY RTE is in a SubqueryScan plan
8813 : : * node, and we can look into the child plan's tlist
8814 : : * instead. An exception occurs if the subquery was
8815 : : * proven empty and optimized away: then we'd find such a
8816 : : * Var in a childless Result node, and there's nothing in
8817 : : * the plan tree that would let us figure out what it had
8818 : : * originally referenced. In that case, fall back on
8819 : : * printing "fN", analogously to the default column names
8820 : : * for RowExprs.
8821 : : */
8822 : : TargetEntry *tle;
8823 : : deparse_namespace save_dpns;
8824 : : const char *result;
8825 : :
2337 8826 [ + + ]: 20 : if (!dpns->inner_plan)
8827 : : {
634 8828 : 8 : char *dummy_name = palloc(32);
8829 : :
632 8830 [ + - - + ]: 8 : Assert(dpns->plan && IsA(dpns->plan, Result));
634 8831 : 8 : snprintf(dummy_name, 32, "f%d", fieldno);
8832 : 8 : return dummy_name;
8833 : : }
632 8834 [ + - - + ]: 12 : Assert(dpns->plan && IsA(dpns->plan, SubqueryScan));
8835 : :
5320 8836 : 12 : tle = get_tle_by_resno(dpns->inner_tlist, attnum);
7011 8837 [ - + ]: 12 : if (!tle)
7011 tgl@sss.pgh.pa.us 8838 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for subquery var: %d",
8839 : : attnum);
7011 tgl@sss.pgh.pa.us 8840 [ - + ]:CBC 12 : Assert(netlevelsup == 0);
2337 8841 : 12 : push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8842 : :
7011 8843 : 12 : result = get_name_for_var_field((Var *) tle->expr, fieldno,
8844 : : levelsup, context);
8845 : :
5775 8846 : 12 : pop_child_plan(dpns, &save_dpns);
7644 8847 : 12 : return result;
8848 : : }
8849 : : }
8850 : 16 : break;
7644 tgl@sss.pgh.pa.us 8851 :UBC 0 : case RTE_JOIN:
8852 : : /* Join RTE --- recursively inspect the alias variable */
6921 8853 [ # # ]: 0 : if (rte->joinaliasvars == NIL)
8854 [ # # ]: 0 : elog(ERROR, "cannot decompile join alias var in plan tree");
7644 8855 [ # # # # ]: 0 : Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
8856 : 0 : expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
4669 8857 [ # # ]: 0 : Assert(expr != NULL);
8858 : : /* we intentionally don't strip implicit coercions here */
7644 8859 [ # # ]: 0 : if (IsA(expr, Var))
8860 : 0 : return get_name_for_var_field((Var *) expr, fieldno,
8861 : 0 : var->varlevelsup + levelsup,
8862 : : context);
8863 : : /* else fall through to inspect the expression */
8864 : 0 : break;
8865 : 0 : case RTE_FUNCTION:
8866 : : case RTE_TABLEFUNC:
8867 : :
8868 : : /*
8869 : : * We couldn't get here unless a function is declared with one of
8870 : : * its result columns as RECORD, which is not allowed.
8871 : : */
8872 : 0 : break;
6422 tgl@sss.pgh.pa.us 8873 :CBC 48 : case RTE_CTE:
8874 : : /* CTE reference: examine subquery's output expr */
8875 : : {
6420 8876 : 48 : CommonTableExpr *cte = NULL;
8877 : : Index ctelevelsup;
8878 : : ListCell *lc;
8879 : :
8880 : : /*
8881 : : * Try to find the referenced CTE using the namespace stack.
8882 : : */
8883 : 48 : ctelevelsup = rte->ctelevelsup + netlevelsup;
8884 [ + + ]: 48 : if (ctelevelsup >= list_length(context->namespaces))
8885 : 8 : lc = NULL;
8886 : : else
8887 : : {
8888 : : deparse_namespace *ctedpns;
8889 : :
8890 : : ctedpns = (deparse_namespace *)
8891 : 40 : list_nth(context->namespaces, ctelevelsup);
8892 [ + + + - : 44 : foreach(lc, ctedpns->ctes)
+ + ]
8893 : : {
8894 : 24 : cte = (CommonTableExpr *) lfirst(lc);
8895 [ + + ]: 24 : if (strcmp(cte->ctename, rte->ctename) == 0)
8896 : 20 : break;
8897 : : }
8898 : : }
8899 [ + + ]: 48 : if (lc != NULL)
8900 : : {
8901 : 20 : Query *ctequery = (Query *) cte->ctequery;
5504 bruce@momjian.us 8902 [ - + + - ]: 20 : TargetEntry *ste = get_tle_by_resno(GetCTETargetList(cte),
8903 : : attnum);
8904 : :
6420 tgl@sss.pgh.pa.us 8905 [ + - - + ]: 20 : if (ste == NULL || ste->resjunk)
963 tgl@sss.pgh.pa.us 8906 [ # # ]:UBC 0 : elog(ERROR, "CTE %s does not have attribute %d",
8907 : : rte->eref->aliasname, attnum);
6420 tgl@sss.pgh.pa.us 8908 :CBC 20 : expr = (Node *) ste->expr;
8909 [ + + ]: 20 : if (IsA(expr, Var))
8910 : : {
8911 : : /*
8912 : : * Recurse into the CTE to see what its Var refers to.
8913 : : * We have to build an additional level of namespace
8914 : : * to keep in step with varlevelsup in the CTE;
8915 : : * furthermore it could be an outer CTE (compare
8916 : : * SUBQUERY case above).
8917 : : */
8918 : 12 : List *save_nslist = context->namespaces;
8919 : : List *parent_namespaces;
8920 : : deparse_namespace mydpns;
8921 : : const char *result;
8922 : :
963 8923 : 12 : parent_namespaces = list_copy_tail(context->namespaces,
8924 : : ctelevelsup);
8925 : :
4873 8926 : 12 : set_deparse_for_query(&mydpns, ctequery,
8927 : : parent_namespaces);
8928 : :
963 8929 : 12 : context->namespaces = lcons(&mydpns, parent_namespaces);
8930 : :
6420 8931 : 12 : result = get_name_for_var_field((Var *) expr, fieldno,
8932 : : 0, context);
8933 : :
8934 : 12 : context->namespaces = save_nslist;
8935 : :
8936 : 12 : return result;
8937 : : }
8938 : : /* else fall through to inspect the expression */
8939 : : }
8940 : : else
8941 : : {
8942 : : /*
8943 : : * We're deparsing a Plan tree so we don't have a CTE
8944 : : * list. But the only places we'd normally see a Var
8945 : : * directly referencing a CTE RTE are in CteScan or
8946 : : * WorkTableScan plan nodes. For those cases,
8947 : : * set_deparse_plan arranged for dpns->inner_plan to be
8948 : : * the plan node that emits the CTE or RecursiveUnion
8949 : : * result, and we can look at its tlist instead. As
8950 : : * above, this can fail if the CTE has been proven empty,
8951 : : * in which case fall back to "fN".
8952 : : */
8953 : : TargetEntry *tle;
8954 : : deparse_namespace save_dpns;
8955 : : const char *result;
8956 : :
2337 8957 [ + + ]: 28 : if (!dpns->inner_plan)
8958 : : {
634 8959 : 4 : char *dummy_name = palloc(32);
8960 : :
632 8961 [ + - - + ]: 4 : Assert(dpns->plan && IsA(dpns->plan, Result));
634 8962 : 4 : snprintf(dummy_name, 32, "f%d", fieldno);
8963 : 4 : return dummy_name;
8964 : : }
632 8965 [ + - + + : 24 : Assert(dpns->plan && (IsA(dpns->plan, CteScan) ||
- + ]
8966 : : IsA(dpns->plan, WorkTableScan)));
8967 : :
5320 8968 : 24 : tle = get_tle_by_resno(dpns->inner_tlist, attnum);
6420 8969 [ - + ]: 24 : if (!tle)
6420 tgl@sss.pgh.pa.us 8970 [ # # ]:UBC 0 : elog(ERROR, "bogus varattno for subquery var: %d",
8971 : : attnum);
6420 tgl@sss.pgh.pa.us 8972 [ - + ]:CBC 24 : Assert(netlevelsup == 0);
2337 8973 : 24 : push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8974 : :
6420 8975 : 24 : result = get_name_for_var_field((Var *) tle->expr, fieldno,
8976 : : levelsup, context);
8977 : :
5775 8978 : 24 : pop_child_plan(dpns, &save_dpns);
6420 8979 : 24 : return result;
8980 : : }
8981 : : }
6422 8982 : 8 : break;
602 rguo@postgresql.org 8983 :UBC 0 : case RTE_GROUP:
8984 : :
8985 : : /*
8986 : : * We couldn't get here: any Vars that reference the RTE_GROUP RTE
8987 : : * should have been replaced with the underlying grouping
8988 : : * expressions.
8989 : : */
8990 : 0 : break;
8991 : : }
8992 : :
8993 : : /*
8994 : : * We now have an expression we can't expand any more, so see if
8995 : : * get_expr_result_tupdesc() can do anything with it.
8996 : : */
3113 tgl@sss.pgh.pa.us 8997 :CBC 24 : tupleDesc = get_expr_result_tupdesc(expr, false);
8998 : : /* Got the tupdesc, so we can extract the field name */
7644 8999 [ + - - + ]: 24 : Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
3180 andres@anarazel.de 9000 : 24 : return NameStr(TupleDescAttr(tupleDesc, fieldno - 1)->attname);
9001 : : }
9002 : :
9003 : : /*
9004 : : * Try to find the referenced expression for a PARAM_EXEC Param that might
9005 : : * reference a parameter supplied by an upper NestLoop or SubPlan plan node.
9006 : : *
9007 : : * If successful, return the expression and set *dpns_p and *ancestor_cell_p
9008 : : * appropriately for calling push_ancestor_plan(). If no referent can be
9009 : : * found, return NULL.
9010 : : */
9011 : : static Node *
5354 tgl@sss.pgh.pa.us 9012 : 4805 : find_param_referent(Param *param, deparse_context *context,
9013 : : deparse_namespace **dpns_p, ListCell **ancestor_cell_p)
9014 : : {
9015 : : /* Initialize output parameters to prevent compiler warnings */
9016 : 4805 : *dpns_p = NULL;
9017 : 4805 : *ancestor_cell_p = NULL;
9018 : :
9019 : : /*
9020 : : * If it's a PARAM_EXEC parameter, look for a matching NestLoopParam or
9021 : : * SubPlan argument. This will necessarily be in some ancestor of the
9022 : : * current expression's Plan node.
9023 : : */
5775 9024 [ + + ]: 4805 : if (param->paramkind == PARAM_EXEC)
9025 : : {
9026 : : deparse_namespace *dpns;
9027 : : Plan *child_plan;
9028 : : ListCell *lc;
9029 : :
9030 : 4216 : dpns = (deparse_namespace *) linitial(context->namespaces);
2337 9031 : 4216 : child_plan = dpns->plan;
9032 : :
5775 9033 [ + + + + : 7487 : foreach(lc, dpns->ancestors)
+ + ]
9034 : : {
2337 9035 : 6367 : Node *ancestor = (Node *) lfirst(lc);
9036 : : ListCell *lc2;
9037 : :
9038 : : /*
9039 : : * NestLoops transmit params to their inner child only.
9040 : : */
9041 [ + + ]: 6367 : if (IsA(ancestor, NestLoop) &&
1266 9042 [ + + ]: 2890 : child_plan == innerPlan(ancestor))
9043 : : {
2337 9044 : 2768 : NestLoop *nl = (NestLoop *) ancestor;
9045 : :
5775 9046 [ + + + + : 3435 : foreach(lc2, nl->nestParams)
+ + ]
9047 : : {
5504 bruce@momjian.us 9048 : 3318 : NestLoopParam *nlp = (NestLoopParam *) lfirst(lc2);
9049 : :
5775 tgl@sss.pgh.pa.us 9050 [ + + ]: 3318 : if (nlp->paramno == param->paramid)
9051 : : {
9052 : : /* Found a match, so return it */
5354 9053 : 2651 : *dpns_p = dpns;
9054 : 2651 : *ancestor_cell_p = lc;
9055 : 2651 : return (Node *) nlp->paramval;
9056 : : }
9057 : : }
9058 : : }
9059 : :
9060 : : /*
9061 : : * If ancestor is a SubPlan, check the arguments it provides.
9062 : : */
2337 9063 [ + + ]: 3716 : if (IsA(ancestor, SubPlan))
5775 9064 : 293 : {
2337 9065 : 738 : SubPlan *subplan = (SubPlan *) ancestor;
9066 : : ListCell *lc3;
9067 : : ListCell *lc4;
9068 : :
5775 9069 [ + + + + : 961 : forboth(lc3, subplan->parParam, lc4, subplan->args)
+ + + + +
+ + - +
+ ]
9070 : : {
5504 bruce@momjian.us 9071 : 668 : int paramid = lfirst_int(lc3);
9072 : 668 : Node *arg = (Node *) lfirst(lc4);
9073 : :
5775 tgl@sss.pgh.pa.us 9074 [ + + ]: 668 : if (paramid == param->paramid)
9075 : : {
9076 : : /*
9077 : : * Found a match, so return it. But, since Vars in
9078 : : * the arg are to be evaluated in the surrounding
9079 : : * context, we have to point to the next ancestor item
9080 : : * that is *not* a SubPlan.
9081 : : */
9082 : : ListCell *rest;
9083 : :
2337 9084 [ + - + - : 445 : for_each_cell(rest, dpns->ancestors,
+ - ]
9085 : : lnext(dpns->ancestors, lc))
9086 : : {
9087 : 445 : Node *ancestor2 = (Node *) lfirst(rest);
9088 : :
9089 [ + - ]: 445 : if (!IsA(ancestor2, SubPlan))
9090 : : {
9091 : 445 : *dpns_p = dpns;
9092 : 445 : *ancestor_cell_p = rest;
9093 : 445 : return arg;
9094 : : }
9095 : : }
2337 tgl@sss.pgh.pa.us 9096 [ # # ]:UBC 0 : elog(ERROR, "SubPlan cannot be outermost ancestor");
9097 : : }
9098 : : }
9099 : :
9100 : : /* SubPlan isn't a kind of Plan, so skip the rest */
2337 tgl@sss.pgh.pa.us 9101 :CBC 293 : continue;
9102 : : }
9103 : :
9104 : : /*
9105 : : * We need not consider the ancestor's initPlan list, since
9106 : : * initplans never have any parParams.
9107 : : */
9108 : :
9109 : : /* No luck, crawl up to next ancestor */
9110 : 2978 : child_plan = (Plan *) ancestor;
9111 : : }
9112 : : }
9113 : :
9114 : : /* No referent found */
5354 9115 : 1709 : return NULL;
9116 : : }
9117 : :
9118 : : /*
9119 : : * Try to find a subplan/initplan that emits the value for a PARAM_EXEC Param.
9120 : : *
9121 : : * If successful, return the generating subplan/initplan and set *column_p
9122 : : * to the subplan's 0-based output column number.
9123 : : * Otherwise, return NULL.
9124 : : */
9125 : : static SubPlan *
777 9126 : 1709 : find_param_generator(Param *param, deparse_context *context, int *column_p)
9127 : : {
9128 : : /* Initialize output parameter to prevent compiler warnings */
9129 : 1709 : *column_p = 0;
9130 : :
9131 : : /*
9132 : : * If it's a PARAM_EXEC parameter, search the current plan node as well as
9133 : : * ancestor nodes looking for a subplan or initplan that emits the value
9134 : : * for the Param. It could appear in the setParams of an initplan or
9135 : : * MULTIEXPR_SUBLINK subplan, or in the paramIds of an ancestral SubPlan.
9136 : : */
9137 [ + + ]: 1709 : if (param->paramkind == PARAM_EXEC)
9138 : : {
9139 : : SubPlan *result;
9140 : : deparse_namespace *dpns;
9141 : : ListCell *lc;
9142 : :
9143 : 1120 : dpns = (deparse_namespace *) linitial(context->namespaces);
9144 : :
9145 : : /* First check the innermost plan node's initplans */
9146 : 1120 : result = find_param_generator_initplan(param, dpns->plan, column_p);
9147 [ + + ]: 1120 : if (result)
9148 : 297 : return result;
9149 : :
9150 : : /*
9151 : : * The plan's targetlist might contain MULTIEXPR_SUBLINK SubPlans,
9152 : : * which can be referenced by Params elsewhere in the targetlist.
9153 : : * (Such Params should always be in the same targetlist, so there's no
9154 : : * need to do this work at upper plan nodes.)
9155 : : */
9156 [ + + + + : 4135 : foreach_node(TargetEntry, tle, dpns->plan->targetlist)
+ + ]
9157 : : {
9158 [ + - + + ]: 2557 : if (tle->expr && IsA(tle->expr, SubPlan))
9159 : : {
9160 : 66 : SubPlan *subplan = (SubPlan *) tle->expr;
9161 : :
9162 [ + + ]: 66 : if (subplan->subLinkType == MULTIEXPR_SUBLINK)
9163 : : {
9164 [ + - + - : 51 : foreach_int(paramid, subplan->setParam)
+ - ]
9165 : : {
9166 [ + + ]: 51 : if (paramid == param->paramid)
9167 : : {
9168 : : /* Found a match, so return it. */
9169 : 34 : *column_p = foreach_current_index(paramid);
9170 : 34 : return subplan;
9171 : : }
9172 : : }
9173 : : }
9174 : : }
9175 : : }
9176 : :
9177 : : /* No luck, so check the ancestor nodes */
9178 [ + - + - : 1024 : foreach(lc, dpns->ancestors)
+ - ]
9179 : : {
9180 : 1024 : Node *ancestor = (Node *) lfirst(lc);
9181 : :
9182 : : /*
9183 : : * If ancestor is a SubPlan, check the paramIds it provides.
9184 : : */
9185 [ + + ]: 1024 : if (IsA(ancestor, SubPlan))
777 tgl@sss.pgh.pa.us 9186 :UBC 0 : {
777 tgl@sss.pgh.pa.us 9187 :CBC 197 : SubPlan *subplan = (SubPlan *) ancestor;
9188 : :
9189 [ + - + - : 222 : foreach_int(paramid, subplan->paramIds)
+ - ]
9190 : : {
9191 [ + + ]: 222 : if (paramid == param->paramid)
9192 : : {
9193 : : /* Found a match, so return it. */
9194 : 197 : *column_p = foreach_current_index(paramid);
9195 : 197 : return subplan;
9196 : : }
9197 : : }
9198 : :
9199 : : /* SubPlan isn't a kind of Plan, so skip the rest */
777 tgl@sss.pgh.pa.us 9200 :UBC 0 : continue;
9201 : : }
9202 : :
9203 : : /*
9204 : : * Otherwise, it's some kind of Plan node, so check its initplans.
9205 : : */
777 tgl@sss.pgh.pa.us 9206 :CBC 827 : result = find_param_generator_initplan(param, (Plan *) ancestor,
9207 : : column_p);
9208 [ + + ]: 827 : if (result)
9209 : 592 : return result;
9210 : :
9211 : : /* No luck, crawl up to next ancestor */
9212 : : }
9213 : : }
9214 : :
9215 : : /* No generator found */
9216 : 589 : return NULL;
9217 : : }
9218 : :
9219 : : /*
9220 : : * Subroutine for find_param_generator: search one Plan node's initplans
9221 : : */
9222 : : static SubPlan *
9223 : 1947 : find_param_generator_initplan(Param *param, Plan *plan, int *column_p)
9224 : : {
9225 [ + + + - : 3097 : foreach_node(SubPlan, subplan, plan->initPlan)
+ + ]
9226 : : {
9227 [ + - + + : 1169 : foreach_int(paramid, subplan->setParam)
+ + ]
9228 : : {
9229 [ + + ]: 985 : if (paramid == param->paramid)
9230 : : {
9231 : : /* Found a match, so return it. */
9232 : 889 : *column_p = foreach_current_index(paramid);
9233 : 889 : return subplan;
9234 : : }
9235 : : }
9236 : : }
9237 : 1058 : return NULL;
9238 : : }
9239 : :
9240 : : /*
9241 : : * Display a Param appropriately.
9242 : : */
9243 : : static void
5354 9244 : 4793 : get_parameter(Param *param, deparse_context *context)
9245 : : {
9246 : : Node *expr;
9247 : : deparse_namespace *dpns;
9248 : : ListCell *ancestor_cell;
9249 : : SubPlan *subplan;
9250 : : int column;
9251 : :
9252 : : /*
9253 : : * If it's a PARAM_EXEC parameter, try to locate the expression from which
9254 : : * the parameter was computed. This stanza handles only cases in which
9255 : : * the Param represents an input to the subplan we are currently in.
9256 : : */
9257 : 4793 : expr = find_param_referent(param, context, &dpns, &ancestor_cell);
9258 [ + + ]: 4793 : if (expr)
9259 : : {
9260 : : /* Found a match, so print it */
9261 : : deparse_namespace save_dpns;
9262 : : bool save_varprefix;
9263 : : bool need_paren;
9264 : :
9265 : : /* Switch attention to the ancestor plan node */
9266 : 3084 : push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
9267 : :
9268 : : /*
9269 : : * Force prefixing of Vars, since they won't belong to the relation
9270 : : * being scanned in the original plan node.
9271 : : */
9272 : 3084 : save_varprefix = context->varprefix;
9273 : 3084 : context->varprefix = true;
9274 : :
9275 : : /*
9276 : : * A Param's expansion is typically a Var, Aggref, GroupingFunc, or
9277 : : * upper-level Param, which wouldn't need extra parentheses.
9278 : : * Otherwise, insert parens to ensure the expression looks atomic.
9279 : : */
9280 [ + + ]: 3100 : need_paren = !(IsA(expr, Var) ||
9281 [ + + ]: 16 : IsA(expr, Aggref) ||
1506 9282 [ + + ]: 12 : IsA(expr, GroupingFunc) ||
5354 9283 [ - + ]: 8 : IsA(expr, Param));
9284 [ - + ]: 3084 : if (need_paren)
5354 tgl@sss.pgh.pa.us 9285 :UBC 0 : appendStringInfoChar(context->buf, '(');
9286 : :
5354 tgl@sss.pgh.pa.us 9287 :CBC 3084 : get_rule_expr(expr, context, false);
9288 : :
9289 [ - + ]: 3084 : if (need_paren)
5354 tgl@sss.pgh.pa.us 9290 :UBC 0 : appendStringInfoChar(context->buf, ')');
9291 : :
5354 tgl@sss.pgh.pa.us 9292 :CBC 3084 : context->varprefix = save_varprefix;
9293 : :
9294 : 3084 : pop_ancestor_plan(dpns, &save_dpns);
9295 : :
9296 : 3084 : return;
9297 : : }
9298 : :
9299 : : /*
9300 : : * Alternatively, maybe it's a subplan output, which we print as a
9301 : : * reference to the subplan. (We could drill down into the subplan and
9302 : : * print the relevant targetlist expression, but that has been deemed too
9303 : : * confusing since it would violate normal SQL scope rules. Also, we're
9304 : : * relying on this reference to show that the testexpr containing the
9305 : : * Param has anything to do with that subplan at all.)
9306 : : */
777 9307 : 1709 : subplan = find_param_generator(param, context, &column);
9308 [ + + ]: 1709 : if (subplan)
9309 : : {
9310 : : const char *nameprefix;
9311 : :
210 rhaas@postgresql.org 9312 [ + + ]:GNC 1120 : if (subplan->isInitPlan)
9313 : 889 : nameprefix = "InitPlan ";
9314 : : else
9315 : 231 : nameprefix = "SubPlan ";
9316 : :
9317 : 1120 : appendStringInfo(context->buf, "(%s%s%s).col%d",
777 tgl@sss.pgh.pa.us 9318 [ + + ]:CBC 1120 : subplan->useHashTable ? "hashed " : "",
9319 : : nameprefix,
9320 : : subplan->plan_name, column + 1);
9321 : :
9322 : 1120 : return;
9323 : : }
9324 : :
9325 : : /*
9326 : : * If it's an external parameter, see if the outermost namespace provides
9327 : : * function argument names.
9328 : : */
1630 9329 [ + - + - ]: 589 : if (param->paramkind == PARAM_EXTERN && context->namespaces != NIL)
9330 : : {
9331 : 589 : dpns = llast(context->namespaces);
9332 [ + + ]: 589 : if (dpns->argnames &&
9333 [ + - ]: 45 : param->paramid > 0 &&
9334 [ + - ]: 45 : param->paramid <= dpns->numargs)
9335 : : {
1854 peter@eisentraut.org 9336 : 45 : char *argname = dpns->argnames[param->paramid - 1];
9337 : :
9338 [ + - ]: 45 : if (argname)
9339 : : {
9340 : 45 : bool should_qualify = false;
9341 : : ListCell *lc;
9342 : :
9343 : : /*
9344 : : * Qualify the parameter name if there are any other deparse
9345 : : * namespaces with range tables. This avoids qualifying in
9346 : : * trivial cases like "RETURN a + b", but makes it safe in all
9347 : : * other cases.
9348 : : */
9349 [ + - + + : 103 : foreach(lc, context->namespaces)
+ + ]
9350 : : {
1308 drowley@postgresql.o 9351 : 78 : deparse_namespace *depns = lfirst(lc);
9352 : :
9353 [ + + ]: 78 : if (depns->rtable_names != NIL)
9354 : : {
1854 peter@eisentraut.org 9355 : 20 : should_qualify = true;
9356 : 20 : break;
9357 : : }
9358 : : }
9359 [ + + ]: 45 : if (should_qualify)
9360 : : {
9361 : 20 : appendStringInfoString(context->buf, quote_identifier(dpns->funcname));
9362 : 20 : appendStringInfoChar(context->buf, '.');
9363 : : }
9364 : :
9365 : 45 : appendStringInfoString(context->buf, quote_identifier(argname));
9366 : 45 : return;
9367 : : }
9368 : : }
9369 : : }
9370 : :
9371 : : /*
9372 : : * Not PARAM_EXEC, or couldn't find referent: just print $N.
9373 : : *
9374 : : * It's a bug if we get here for anything except PARAM_EXTERN Params, but
9375 : : * in production builds printing $N seems more useful than failing.
9376 : : */
777 tgl@sss.pgh.pa.us 9377 [ - + ]: 544 : Assert(param->paramkind == PARAM_EXTERN);
9378 : :
5354 9379 : 544 : appendStringInfo(context->buf, "$%d", param->paramid);
9380 : : }
9381 : :
9382 : : /*
9383 : : * get_simple_binary_op_name
9384 : : *
9385 : : * helper function for isSimpleNode
9386 : : * will return single char binary operator name, or NULL if it's not
9387 : : */
9388 : : static const char *
8306 bruce@momjian.us 9389 : 100 : get_simple_binary_op_name(OpExpr *expr)
9390 : : {
8315 tgl@sss.pgh.pa.us 9391 : 100 : List *args = expr->args;
9392 : :
8010 neilc@samurai.com 9393 [ + - ]: 100 : if (list_length(args) == 2)
9394 : : {
9395 : : /* binary operator */
8014 9396 : 100 : Node *arg1 = (Node *) linitial(args);
8315 tgl@sss.pgh.pa.us 9397 : 100 : Node *arg2 = (Node *) lsecond(args);
9398 : : const char *op;
9399 : :
9400 : 100 : op = generate_operator_name(expr->opno, exprType(arg1), exprType(arg2));
9401 [ + - ]: 100 : if (strlen(op) == 1)
8310 bruce@momjian.us 9402 : 100 : return op;
9403 : : }
8315 tgl@sss.pgh.pa.us 9404 :UBC 0 : return NULL;
9405 : : }
9406 : :
9407 : :
9408 : : /*
9409 : : * isSimpleNode - check if given node is simple (doesn't need parenthesizing)
9410 : : *
9411 : : * true : simple in the context of parent node's type
9412 : : * false : not simple
9413 : : */
9414 : : static bool
8315 tgl@sss.pgh.pa.us 9415 :CBC 3965 : isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
9416 : : {
8310 bruce@momjian.us 9417 [ - + ]: 3965 : if (!node)
8315 tgl@sss.pgh.pa.us 9418 :UBC 0 : return false;
9419 : :
8310 bruce@momjian.us 9420 [ + + - + :CBC 3965 : switch (nodeTag(node))
- + + + -
- - + + +
+ + ]
9421 : : {
8315 tgl@sss.pgh.pa.us 9422 : 3330 : case T_Var:
9423 : : case T_Const:
9424 : : case T_Param:
9425 : : case T_CoerceToDomainValue:
9426 : : case T_SetToDefault:
9427 : : case T_CurrentOfExpr:
9428 : : /* single words: always simple */
9429 : 3330 : return true;
9430 : :
2650 alvherre@alvh.no-ip. 9431 : 331 : case T_SubscriptingRef:
9432 : : case T_ArrayExpr:
9433 : : case T_RowExpr:
9434 : : case T_CoalesceExpr:
9435 : : case T_MinMaxExpr:
9436 : : case T_SQLValueFunction:
9437 : : case T_XmlExpr:
9438 : : case T_NextValueExpr:
9439 : : case T_NullIfExpr:
9440 : : case T_Aggref:
9441 : : case T_GroupingFunc:
9442 : : case T_WindowFunc:
9443 : : case T_MergeSupportFunc:
9444 : : case T_FuncExpr:
9445 : : case T_JsonConstructorExpr:
9446 : : case T_JsonExpr:
9447 : : /* function-like: name(..) or name[..] */
8315 tgl@sss.pgh.pa.us 9448 : 331 : return true;
9449 : :
9450 : : /* CASE keywords act as parentheses */
8315 tgl@sss.pgh.pa.us 9451 :UBC 0 : case T_CaseExpr:
9452 : 0 : return true;
9453 : :
8315 tgl@sss.pgh.pa.us 9454 :CBC 48 : case T_FieldSelect:
9455 : :
9456 : : /*
9457 : : * appears simple since . has top precedence, unless parent is
9458 : : * T_FieldSelect itself!
9459 : : */
1700 michael@paquier.xyz 9460 : 48 : return !IsA(parentNode, FieldSelect);
9461 : :
8000 tgl@sss.pgh.pa.us 9462 :UBC 0 : case T_FieldStore:
9463 : :
9464 : : /*
9465 : : * treat like FieldSelect (probably doesn't matter)
9466 : : */
1700 michael@paquier.xyz 9467 : 0 : return !IsA(parentNode, FieldStore);
9468 : :
8315 tgl@sss.pgh.pa.us 9469 :GBC 12 : case T_CoerceToDomain:
9470 : : /* maybe simple, check args */
8310 bruce@momjian.us 9471 : 12 : return isSimpleNode((Node *) ((CoerceToDomain *) node)->arg,
9472 : : node, prettyFlags);
8315 tgl@sss.pgh.pa.us 9473 :CBC 12 : case T_RelabelType:
8310 bruce@momjian.us 9474 : 12 : return isSimpleNode((Node *) ((RelabelType *) node)->arg,
9475 : : node, prettyFlags);
6909 tgl@sss.pgh.pa.us 9476 :GBC 16 : case T_CoerceViaIO:
9477 : 16 : return isSimpleNode((Node *) ((CoerceViaIO *) node)->arg,
9478 : : node, prettyFlags);
6979 tgl@sss.pgh.pa.us 9479 :UBC 0 : case T_ArrayCoerceExpr:
9480 : 0 : return isSimpleNode((Node *) ((ArrayCoerceExpr *) node)->arg,
9481 : : node, prettyFlags);
7815 9482 : 0 : case T_ConvertRowtypeExpr:
9483 : 0 : return isSimpleNode((Node *) ((ConvertRowtypeExpr *) node)->arg,
9484 : : node, prettyFlags);
474 dean.a.rasheed@gmail 9485 : 0 : case T_ReturningExpr:
9486 : 0 : return isSimpleNode((Node *) ((ReturningExpr *) node)->retexpr,
9487 : : node, prettyFlags);
9488 : :
8315 tgl@sss.pgh.pa.us 9489 :CBC 184 : case T_OpExpr:
9490 : : {
9491 : : /* depends on parent node type; needs further checking */
8310 bruce@momjian.us 9492 [ + - + + ]: 184 : if (prettyFlags & PRETTYFLAG_PAREN && IsA(parentNode, OpExpr))
9493 : : {
9494 : : const char *op;
9495 : : const char *parentOp;
9496 : : bool is_lopriop;
9497 : : bool is_hipriop;
9498 : : bool is_lopriparent;
9499 : : bool is_hipriparent;
9500 : :
9501 : 52 : op = get_simple_binary_op_name((OpExpr *) node);
9502 [ - + ]: 52 : if (!op)
8310 bruce@momjian.us 9503 :UBC 0 : return false;
9504 : :
9505 : : /* We know only the basic operators + - and * / % */
8310 bruce@momjian.us 9506 :CBC 52 : is_lopriop = (strchr("+-", *op) != NULL);
9507 : 52 : is_hipriop = (strchr("*/%", *op) != NULL);
9508 [ + + + + ]: 52 : if (!(is_lopriop || is_hipriop))
9509 : 4 : return false;
9510 : :
9511 : 48 : parentOp = get_simple_binary_op_name((OpExpr *) parentNode);
9512 [ - + ]: 48 : if (!parentOp)
8310 bruce@momjian.us 9513 :UBC 0 : return false;
9514 : :
8310 bruce@momjian.us 9515 :CBC 48 : is_lopriparent = (strchr("+-", *parentOp) != NULL);
9516 : 48 : is_hipriparent = (strchr("*/%", *parentOp) != NULL);
9517 [ + + - + ]: 48 : if (!(is_lopriparent || is_hipriparent))
8310 bruce@momjian.us 9518 :UBC 0 : return false;
9519 : :
8310 bruce@momjian.us 9520 [ + + + - ]:CBC 48 : if (is_hipriop && is_lopriparent)
9521 : 8 : return true; /* op binds tighter than parent */
9522 : :
9523 [ + - + + ]: 40 : if (is_lopriop && is_hipriparent)
9524 : 32 : return false;
9525 : :
9526 : : /*
9527 : : * Operators are same priority --- can skip parens only if
9528 : : * we have (a - b) - c, not a - (b - c).
9529 : : */
8014 neilc@samurai.com 9530 [ + + ]: 8 : if (node == (Node *) linitial(((OpExpr *) parentNode)->args))
8310 bruce@momjian.us 9531 : 4 : return true;
9532 : :
9533 : 4 : return false;
9534 : : }
9535 : : /* else do the same stuff as for T_SubLink et al. */
9536 : : }
9537 : : pg_fallthrough;
9538 : :
9539 : : case T_SubLink:
9540 : : case T_NullTest:
9541 : : case T_BooleanTest:
9542 : : case T_DistinctExpr:
9543 : : case T_JsonIsPredicate:
8315 tgl@sss.pgh.pa.us 9544 [ + + + ]: 144 : switch (nodeTag(parentNode))
9545 : : {
9546 : 40 : case T_FuncExpr:
9547 : : {
9548 : : /* special handling for casts and COERCE_SQL_SYNTAX */
8310 bruce@momjian.us 9549 : 40 : CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
9550 : :
9551 [ + + + + ]: 40 : if (type == COERCE_EXPLICIT_CAST ||
1251 tgl@sss.pgh.pa.us 9552 [ + - ]: 4 : type == COERCE_IMPLICIT_CAST ||
9553 : : type == COERCE_SQL_SYNTAX)
8310 bruce@momjian.us 9554 : 40 : return false;
8310 bruce@momjian.us 9555 :UBC 0 : return true; /* own parentheses */
9556 : : }
3240 tgl@sss.pgh.pa.us 9557 :CBC 84 : case T_BoolExpr: /* lower precedence */
9558 : : case T_SubscriptingRef: /* other separators */
9559 : : case T_ArrayExpr: /* other separators */
9560 : : case T_RowExpr: /* other separators */
9561 : : case T_CoalesceExpr: /* own parentheses */
9562 : : case T_MinMaxExpr: /* own parentheses */
9563 : : case T_XmlExpr: /* own parentheses */
9564 : : case T_NullIfExpr: /* other separators */
9565 : : case T_Aggref: /* own parentheses */
9566 : : case T_GroupingFunc: /* own parentheses */
9567 : : case T_WindowFunc: /* own parentheses */
9568 : : case T_CaseExpr: /* other separators */
8315 9569 : 84 : return true;
9570 : 20 : default:
9571 : 20 : return false;
9572 : : }
9573 : :
9574 : 12 : case T_BoolExpr:
9575 [ + - - - ]: 12 : switch (nodeTag(parentNode))
9576 : : {
9577 : 12 : case T_BoolExpr:
9578 [ + - ]: 12 : if (prettyFlags & PRETTYFLAG_PAREN)
9579 : : {
9580 : : BoolExprType type;
9581 : : BoolExprType parentType;
9582 : :
8310 bruce@momjian.us 9583 : 12 : type = ((BoolExpr *) node)->boolop;
9584 : 12 : parentType = ((BoolExpr *) parentNode)->boolop;
8315 tgl@sss.pgh.pa.us 9585 [ + + - ]: 12 : switch (type)
9586 : : {
9587 : 8 : case NOT_EXPR:
9588 : : case AND_EXPR:
9589 [ + + + - ]: 8 : if (parentType == AND_EXPR || parentType == OR_EXPR)
9590 : 8 : return true;
8315 tgl@sss.pgh.pa.us 9591 :UBC 0 : break;
8315 tgl@sss.pgh.pa.us 9592 :CBC 4 : case OR_EXPR:
9593 [ - + ]: 4 : if (parentType == OR_EXPR)
8315 tgl@sss.pgh.pa.us 9594 :UBC 0 : return true;
8315 tgl@sss.pgh.pa.us 9595 :CBC 4 : break;
9596 : : }
9597 : : }
9598 : 4 : return false;
8315 tgl@sss.pgh.pa.us 9599 :UBC 0 : case T_FuncExpr:
9600 : : {
9601 : : /* special handling for casts and COERCE_SQL_SYNTAX */
8310 bruce@momjian.us 9602 : 0 : CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
9603 : :
9604 [ # # # # ]: 0 : if (type == COERCE_EXPLICIT_CAST ||
1251 tgl@sss.pgh.pa.us 9605 [ # # ]: 0 : type == COERCE_IMPLICIT_CAST ||
9606 : : type == COERCE_SQL_SYNTAX)
8310 bruce@momjian.us 9607 : 0 : return false;
9608 : 0 : return true; /* own parentheses */
9609 : : }
2650 alvherre@alvh.no-ip. 9610 : 0 : case T_SubscriptingRef: /* other separators */
9611 : : case T_ArrayExpr: /* other separators */
9612 : : case T_RowExpr: /* other separators */
9613 : : case T_CoalesceExpr: /* own parentheses */
9614 : : case T_MinMaxExpr: /* own parentheses */
9615 : : case T_XmlExpr: /* own parentheses */
9616 : : case T_NullIfExpr: /* other separators */
9617 : : case T_Aggref: /* own parentheses */
9618 : : case T_GroupingFunc: /* own parentheses */
9619 : : case T_WindowFunc: /* own parentheses */
9620 : : case T_CaseExpr: /* other separators */
9621 : : case T_JsonExpr: /* own parentheses */
8315 tgl@sss.pgh.pa.us 9622 : 0 : return true;
9623 : 0 : default:
9624 : 0 : return false;
9625 : : }
9626 : :
1133 alvherre@alvh.no-ip. 9627 :GBC 4 : case T_JsonValueExpr:
9628 : : /* maybe simple, check args */
9629 : 4 : return isSimpleNode((Node *) ((JsonValueExpr *) node)->raw_expr,
9630 : : node, prettyFlags);
9631 : :
8315 tgl@sss.pgh.pa.us 9632 :CBC 4 : default:
9633 : 4 : break;
9634 : : }
9635 : : /* those we don't know: in dubio complexo */
8310 bruce@momjian.us 9636 : 4 : return false;
9637 : : }
9638 : :
9639 : :
9640 : : /*
9641 : : * appendContextKeyword - append a keyword to buffer
9642 : : *
9643 : : * If prettyPrint is enabled, perform a line break, and adjust indentation.
9644 : : * Otherwise, just append the keyword.
9645 : : */
9646 : : static void
8315 tgl@sss.pgh.pa.us 9647 : 19183 : appendContextKeyword(deparse_context *context, const char *str,
9648 : : int indentBefore, int indentAfter, int indentPlus)
9649 : : {
4558 9650 : 19183 : StringInfo buf = context->buf;
9651 : :
8310 bruce@momjian.us 9652 [ + + ]: 19183 : if (PRETTY_INDENT(context))
9653 : : {
9654 : : int indentAmount;
9655 : :
8315 tgl@sss.pgh.pa.us 9656 : 18562 : context->indentLevel += indentBefore;
9657 : :
9658 : : /* remove any trailing spaces currently in the buffer ... */
4558 9659 : 18562 : removeStringInfoSpaces(buf);
9660 : : /* ... then add a newline and some spaces */
9661 : 18562 : appendStringInfoChar(buf, '\n');
9662 : :
4388 9663 [ + - ]: 18562 : if (context->indentLevel < PRETTYINDENT_LIMIT)
9664 : 18562 : indentAmount = Max(context->indentLevel, 0) + indentPlus;
9665 : : else
9666 : : {
9667 : : /*
9668 : : * If we're indented more than PRETTYINDENT_LIMIT characters, try
9669 : : * to conserve horizontal space by reducing the per-level
9670 : : * indentation. For best results the scale factor here should
9671 : : * divide all the indent amounts that get added to indentLevel
9672 : : * (PRETTYINDENT_STD, etc). It's important that the indentation
9673 : : * not grow unboundedly, else deeply-nested trees use O(N^2)
9674 : : * whitespace; so we also wrap modulo PRETTYINDENT_LIMIT.
9675 : : */
4388 tgl@sss.pgh.pa.us 9676 :UBC 0 : indentAmount = PRETTYINDENT_LIMIT +
9677 : 0 : (context->indentLevel - PRETTYINDENT_LIMIT) /
9678 : : (PRETTYINDENT_STD / 2);
9679 : 0 : indentAmount %= PRETTYINDENT_LIMIT;
9680 : : /* scale/wrap logic affects indentLevel, but not indentPlus */
9681 : 0 : indentAmount += indentPlus;
9682 : : }
4388 tgl@sss.pgh.pa.us 9683 :CBC 18562 : appendStringInfoSpaces(buf, indentAmount);
9684 : :
4558 9685 : 18562 : appendStringInfoString(buf, str);
9686 : :
8315 9687 : 18562 : context->indentLevel += indentAfter;
9688 [ - + ]: 18562 : if (context->indentLevel < 0)
8315 tgl@sss.pgh.pa.us 9689 :UBC 0 : context->indentLevel = 0;
9690 : : }
9691 : : else
4558 tgl@sss.pgh.pa.us 9692 :CBC 621 : appendStringInfoString(buf, str);
8315 9693 : 19183 : }
9694 : :
9695 : : /*
9696 : : * removeStringInfoSpaces - delete trailing spaces from a buffer.
9697 : : *
9698 : : * Possibly this should move to stringinfo.c at some point.
9699 : : */
9700 : : static void
4558 9701 : 18930 : removeStringInfoSpaces(StringInfo str)
9702 : : {
9703 [ + + + + ]: 29552 : while (str->len > 0 && str->data[str->len - 1] == ' ')
9704 : 10622 : str->data[--(str->len)] = '\0';
9705 : 18930 : }
9706 : :
9707 : :
9708 : : /*
9709 : : * get_rule_expr_paren - deparse expr using get_rule_expr,
9710 : : * embracing the string with parentheses if necessary for prettyPrint.
9711 : : *
9712 : : * Never embrace if prettyFlags=0, because it's done in the calling node.
9713 : : *
9714 : : * Any node that does *not* embrace its argument node by sql syntax (with
9715 : : * parentheses, non-operator keywords like CASE/WHEN/ON, or comma etc) should
9716 : : * use get_rule_expr_paren instead of get_rule_expr so parentheses can be
9717 : : * added.
9718 : : */
9719 : : static void
8310 bruce@momjian.us 9720 : 109158 : get_rule_expr_paren(Node *node, deparse_context *context,
9721 : : bool showimplicit, Node *parentNode)
9722 : : {
9723 : : bool need_paren;
9724 : :
8315 tgl@sss.pgh.pa.us 9725 [ + + ]: 113079 : need_paren = PRETTY_PAREN(context) &&
9726 [ + + ]: 3921 : !isSimpleNode(node, parentNode, context->prettyFlags);
9727 : :
9728 [ + + ]: 109158 : if (need_paren)
8310 bruce@momjian.us 9729 : 108 : appendStringInfoChar(context->buf, '(');
9730 : :
8315 tgl@sss.pgh.pa.us 9731 : 109158 : get_rule_expr(node, context, showimplicit);
9732 : :
9733 [ + + ]: 109158 : if (need_paren)
8310 bruce@momjian.us 9734 : 108 : appendStringInfoChar(context->buf, ')');
8315 tgl@sss.pgh.pa.us 9735 : 109158 : }
9736 : :
9737 : : static void
775 amitlan@postgresql.o 9738 : 56 : get_json_behavior(JsonBehavior *behavior, deparse_context *context,
9739 : : const char *on)
9740 : : {
9741 : : /*
9742 : : * The order of array elements must correspond to the order of
9743 : : * JsonBehaviorType members.
9744 : : */
9745 : 56 : const char *behavior_names[] =
9746 : : {
9747 : : " NULL",
9748 : : " ERROR",
9749 : : " EMPTY",
9750 : : " TRUE",
9751 : : " FALSE",
9752 : : " UNKNOWN",
9753 : : " EMPTY ARRAY",
9754 : : " EMPTY OBJECT",
9755 : : " DEFAULT "
9756 : : };
9757 : :
9758 [ + - - + ]: 56 : if ((int) behavior->btype < 0 || behavior->btype >= lengthof(behavior_names))
775 amitlan@postgresql.o 9759 [ # # ]:UBC 0 : elog(ERROR, "invalid json behavior type: %d", behavior->btype);
9760 : :
775 amitlan@postgresql.o 9761 :CBC 56 : appendStringInfoString(context->buf, behavior_names[behavior->btype]);
9762 : :
9763 [ + + ]: 56 : if (behavior->btype == JSON_BEHAVIOR_DEFAULT)
9764 : 12 : get_rule_expr(behavior->expr, context, false);
9765 : :
9766 : 56 : appendStringInfo(context->buf, " ON %s", on);
9767 : 56 : }
9768 : :
9769 : : /*
9770 : : * get_json_expr_options
9771 : : *
9772 : : * Parse back common options for JSON_QUERY, JSON_VALUE, JSON_EXISTS and
9773 : : * JSON_TABLE columns.
9774 : : */
9775 : : static void
9776 : 304 : get_json_expr_options(JsonExpr *jsexpr, deparse_context *context,
9777 : : JsonBehaviorType default_behavior)
9778 : : {
9779 [ + + ]: 304 : if (jsexpr->op == JSON_QUERY_OP)
9780 : : {
9781 [ + + ]: 140 : if (jsexpr->wrapper == JSW_CONDITIONAL)
755 drowley@postgresql.o 9782 : 8 : appendStringInfoString(context->buf, " WITH CONDITIONAL WRAPPER");
775 amitlan@postgresql.o 9783 [ + + ]: 132 : else if (jsexpr->wrapper == JSW_UNCONDITIONAL)
755 drowley@postgresql.o 9784 : 20 : appendStringInfoString(context->buf, " WITH UNCONDITIONAL WRAPPER");
9785 : : /* The default */
757 amitlan@postgresql.o 9786 [ + + + - ]: 112 : else if (jsexpr->wrapper == JSW_NONE || jsexpr->wrapper == JSW_UNSPEC)
755 drowley@postgresql.o 9787 : 112 : appendStringInfoString(context->buf, " WITHOUT WRAPPER");
9788 : :
775 amitlan@postgresql.o 9789 [ + + ]: 140 : if (jsexpr->omit_quotes)
755 drowley@postgresql.o 9790 : 28 : appendStringInfoString(context->buf, " OMIT QUOTES");
9791 : : /* The default */
9792 : : else
9793 : 112 : appendStringInfoString(context->buf, " KEEP QUOTES");
9794 : : }
9795 : :
775 amitlan@postgresql.o 9796 [ + + + + ]: 304 : if (jsexpr->on_empty && jsexpr->on_empty->btype != default_behavior)
9797 : 20 : get_json_behavior(jsexpr->on_empty, context, "EMPTY");
9798 : :
9799 [ + - + + ]: 304 : if (jsexpr->on_error && jsexpr->on_error->btype != default_behavior)
9800 : 32 : get_json_behavior(jsexpr->on_error, context, "ERROR");
9801 : 304 : }
9802 : :
9803 : : /* ----------
9804 : : * get_rule_expr - Parse back an expression
9805 : : *
9806 : : * Note: showimplicit determines whether we display any implicit cast that
9807 : : * is present at the top of the expression tree. It is a passed argument,
9808 : : * not a field of the context struct, because we change the value as we
9809 : : * recurse down into the expression. In general we suppress implicit casts
9810 : : * when the result type is known with certainty (eg, the arguments of an
9811 : : * OR must be boolean). We display implicit casts for arguments of functions
9812 : : * and operators, since this is needed to be certain that the same function
9813 : : * or operator will be chosen when the expression is re-parsed.
9814 : : * ----------
9815 : : */
9816 : : static void
8629 tgl@sss.pgh.pa.us 9817 : 236575 : get_rule_expr(Node *node, deparse_context *context,
9818 : : bool showimplicit)
9819 : : {
9711 9820 : 236575 : StringInfo buf = context->buf;
9821 : :
10108 bruce@momjian.us 9822 [ + + ]: 236575 : if (node == NULL)
9712 tgl@sss.pgh.pa.us 9823 : 60 : return;
9824 : :
9825 : : /* Guard against excessively long or deeply-nested queries */
4388 9826 [ - + ]: 236515 : CHECK_FOR_INTERRUPTS();
9827 : 236515 : check_stack_depth();
9828 : :
9829 : : /*
9830 : : * Each level of get_rule_expr must emit an indivisible term
9831 : : * (parenthesized if necessary) to ensure result is reparsed into the same
9832 : : * expression tree. The only exception is that when the input is a List,
9833 : : * we emit the component items comma-separated with no surrounding
9834 : : * decoration; this is convenient for most callers.
9835 : : */
10108 bruce@momjian.us 9836 [ + + + + : 236515 : switch (nodeTag(node))
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ + - + +
+ + + + +
+ + + + +
+ - + + +
+ + + + +
+ + - ]
9837 : : {
9747 tgl@sss.pgh.pa.us 9838 : 114260 : case T_Var:
5121 9839 : 114260 : (void) get_variable((Var *) node, 0, false, context);
10108 bruce@momjian.us 9840 : 114260 : break;
9841 : :
8545 tgl@sss.pgh.pa.us 9842 : 41319 : case T_Const:
6694 9843 : 41319 : get_const_expr((Const *) node, context, 0);
8545 9844 : 41319 : break;
9845 : :
9846 : 4793 : case T_Param:
5775 9847 : 4793 : get_parameter((Param *) node, context);
10108 bruce@momjian.us 9848 : 4793 : break;
9849 : :
9747 tgl@sss.pgh.pa.us 9850 : 2570 : case T_Aggref:
3660 rhaas@postgresql.org 9851 : 2570 : get_agg_expr((Aggref *) node, context, (Aggref *) node);
9747 tgl@sss.pgh.pa.us 9852 : 2570 : break;
9853 : :
4007 andres@anarazel.de 9854 : 74 : case T_GroupingFunc:
9855 : : {
9856 : 74 : GroupingFunc *gexpr = (GroupingFunc *) node;
9857 : :
9858 : 74 : appendStringInfoString(buf, "GROUPING(");
9859 : 74 : get_rule_expr((Node *) gexpr->args, context, true);
9860 : 74 : appendStringInfoChar(buf, ')');
9861 : : }
9862 : 74 : break;
9863 : :
6337 tgl@sss.pgh.pa.us 9864 : 214 : case T_WindowFunc:
9865 : 214 : get_windowfunc_expr((WindowFunc *) node, context);
9866 : 214 : break;
9867 : :
779 dean.a.rasheed@gmail 9868 : 4 : case T_MergeSupportFunc:
9869 : 4 : appendStringInfoString(buf, "MERGE_ACTION()");
9870 : 4 : break;
9871 : :
2650 alvherre@alvh.no-ip. 9872 : 232 : case T_SubscriptingRef:
9873 : : {
9874 : 232 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
9875 : : bool need_parens;
9876 : :
9877 : : /*
9878 : : * If the argument is a CaseTestExpr, we must be inside a
9879 : : * FieldStore, ie, we are assigning to an element of an array
9880 : : * within a composite column. Since we already punted on
9881 : : * displaying the FieldStore's target information, just punt
9882 : : * here too, and display only the assignment source
9883 : : * expression.
9884 : : */
9885 [ - + ]: 232 : if (IsA(sbsref->refexpr, CaseTestExpr))
9886 : : {
2650 alvherre@alvh.no-ip. 9887 [ # # ]:UBC 0 : Assert(sbsref->refassgnexpr);
9888 : 0 : get_rule_expr((Node *) sbsref->refassgnexpr,
9889 : : context, showimplicit);
5920 tgl@sss.pgh.pa.us 9890 : 0 : break;
9891 : : }
9892 : :
9893 : : /*
9894 : : * Parenthesize the argument unless it's a simple Var or a
9895 : : * FieldSelect. (In particular, if it's another
9896 : : * SubscriptingRef, we *must* parenthesize to avoid
9897 : : * confusion.)
9898 : : */
2650 alvherre@alvh.no-ip. 9899 [ + + ]:CBC 358 : need_parens = !IsA(sbsref->refexpr, Var) &&
9900 [ + + ]: 126 : !IsA(sbsref->refexpr, FieldSelect);
8428 tgl@sss.pgh.pa.us 9901 [ + + ]: 232 : if (need_parens)
9902 : 56 : appendStringInfoChar(buf, '(');
2650 alvherre@alvh.no-ip. 9903 : 232 : get_rule_expr((Node *) sbsref->refexpr, context, showimplicit);
8428 tgl@sss.pgh.pa.us 9904 [ + + ]: 232 : if (need_parens)
9905 : 56 : appendStringInfoChar(buf, ')');
9906 : :
9907 : : /*
9908 : : * If there's a refassgnexpr, we want to print the node in the
9909 : : * format "container[subscripts] := refassgnexpr". This is
9910 : : * not legal SQL, so decompilation of INSERT or UPDATE
9911 : : * statements should always use processIndirection as part of
9912 : : * the statement-level syntax. We should only see this when
9913 : : * EXPLAIN tries to print the targetlist of a plan resulting
9914 : : * from such a statement.
9915 : : */
2650 alvherre@alvh.no-ip. 9916 [ + + ]: 232 : if (sbsref->refassgnexpr)
9917 : : {
9918 : : Node *refassgnexpr;
9919 : :
9920 : : /*
9921 : : * Use processIndirection to print this node's subscripts
9922 : : * as well as any additional field selections or
9923 : : * subscripting in immediate descendants. It returns the
9924 : : * RHS expr that is actually being "assigned".
9925 : : */
3562 tgl@sss.pgh.pa.us 9926 : 8 : refassgnexpr = processIndirection(node, context);
5920 9927 : 8 : appendStringInfoString(buf, " := ");
9928 : 8 : get_rule_expr(refassgnexpr, context, showimplicit);
9929 : : }
9930 : : else
9931 : : {
9932 : : /* Just an ordinary container fetch, so print subscripts */
2650 alvherre@alvh.no-ip. 9933 : 224 : printSubscripts(sbsref, context);
9934 : : }
9935 : : }
8545 tgl@sss.pgh.pa.us 9936 : 232 : break;
9937 : :
9938 : 8158 : case T_FuncExpr:
9939 : 8158 : get_func_expr((FuncExpr *) node, context, showimplicit);
9940 : 8158 : break;
9941 : :
6053 9942 : 28 : case T_NamedArgExpr:
9943 : : {
9944 : 28 : NamedArgExpr *na = (NamedArgExpr *) node;
9945 : :
4022 rhaas@postgresql.org 9946 : 28 : appendStringInfo(buf, "%s => ", quote_identifier(na->name));
6053 tgl@sss.pgh.pa.us 9947 : 28 : get_rule_expr((Node *) na->arg, context, showimplicit);
9948 : : }
9949 : 28 : break;
9950 : :
8545 9951 : 40741 : case T_OpExpr:
9952 : 40741 : get_oper_expr((OpExpr *) node, context);
9953 : 40741 : break;
9954 : :
9955 : 16 : case T_DistinctExpr:
9956 : : {
9957 : 16 : DistinctExpr *expr = (DistinctExpr *) node;
9958 : 16 : List *args = expr->args;
8014 neilc@samurai.com 9959 : 16 : Node *arg1 = (Node *) linitial(args);
8346 tgl@sss.pgh.pa.us 9960 : 16 : Node *arg2 = (Node *) lsecond(args);
9961 : :
8315 9962 [ + + ]: 16 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 9963 : 12 : appendStringInfoChar(buf, '(');
8315 tgl@sss.pgh.pa.us 9964 : 16 : get_rule_expr_paren(arg1, context, true, node);
4569 rhaas@postgresql.org 9965 : 16 : appendStringInfoString(buf, " IS DISTINCT FROM ");
8315 tgl@sss.pgh.pa.us 9966 : 16 : get_rule_expr_paren(arg2, context, true, node);
9967 [ + + ]: 16 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 9968 : 12 : appendStringInfoChar(buf, ')');
9969 : : }
8346 tgl@sss.pgh.pa.us 9970 : 16 : break;
9971 : :
5526 9972 : 140 : case T_NullIfExpr:
9973 : : {
9974 : 140 : NullIfExpr *nullifexpr = (NullIfExpr *) node;
9975 : :
4569 rhaas@postgresql.org 9976 : 140 : appendStringInfoString(buf, "NULLIF(");
5526 tgl@sss.pgh.pa.us 9977 : 140 : get_rule_expr((Node *) nullifexpr->args, context, true);
9978 : 140 : appendStringInfoChar(buf, ')');
9979 : : }
9980 : 140 : break;
9981 : :
8346 9982 : 2022 : case T_ScalarArrayOpExpr:
9983 : : {
9984 : 2022 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
9985 : 2022 : List *args = expr->args;
8014 neilc@samurai.com 9986 : 2022 : Node *arg1 = (Node *) linitial(args);
8346 tgl@sss.pgh.pa.us 9987 : 2022 : Node *arg2 = (Node *) lsecond(args);
9988 : :
8315 9989 [ + + ]: 2022 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 9990 : 2014 : appendStringInfoChar(buf, '(');
8315 tgl@sss.pgh.pa.us 9991 : 2022 : get_rule_expr_paren(arg1, context, true, node);
8346 9992 : 2022 : appendStringInfo(buf, " %s %s (",
9993 : : generate_operator_name(expr->opno,
9994 : : exprType(arg1),
9995 : : get_base_element_type(exprType(arg2))),
9996 [ + + ]: 2022 : expr->useOr ? "ANY" : "ALL");
8315 9997 : 2022 : get_rule_expr_paren(arg2, context, true, node);
9998 : :
9999 : : /*
10000 : : * There's inherent ambiguity in "x op ANY/ALL (y)" when y is
10001 : : * a bare sub-SELECT. Since we're here, the sub-SELECT must
10002 : : * be meant as a scalar sub-SELECT yielding an array value to
10003 : : * be used in ScalarArrayOpExpr; but the grammar will
10004 : : * preferentially interpret such a construct as an ANY/ALL
10005 : : * SubLink. To prevent misparsing the output that way, insert
10006 : : * a dummy coercion (which will be stripped by parse analysis,
10007 : : * so no inefficiency is added in dump and reload). This is
10008 : : * indeed most likely what the user wrote to get the construct
10009 : : * accepted in the first place.
10010 : : */
3666 10011 [ + + ]: 2022 : if (IsA(arg2, SubLink) &&
10012 [ + - ]: 4 : ((SubLink *) arg2)->subLinkType == EXPR_SUBLINK)
10013 : 4 : appendStringInfo(buf, "::%s",
10014 : : format_type_with_typemod(exprType(arg2),
10015 : : exprTypmod(arg2)));
8315 10016 : 2022 : appendStringInfoChar(buf, ')');
10017 [ + + ]: 2022 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10018 : 2014 : appendStringInfoChar(buf, ')');
10019 : : }
10108 10020 : 2022 : break;
10021 : :
8545 tgl@sss.pgh.pa.us 10022 : 7491 : case T_BoolExpr:
10023 : : {
10024 : 7491 : BoolExpr *expr = (BoolExpr *) node;
8014 neilc@samurai.com 10025 : 7491 : Node *first_arg = linitial(expr->args);
10026 : : ListCell *arg;
10027 : :
8545 tgl@sss.pgh.pa.us 10028 [ + + + - ]: 7491 : switch (expr->boolop)
10029 : : {
10030 : 5940 : case AND_EXPR:
8315 10031 [ + + ]: 5940 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10032 : 5904 : appendStringInfoChar(buf, '(');
8014 neilc@samurai.com 10033 : 5940 : get_rule_expr_paren(first_arg, context,
10034 : : false, node);
2045 tgl@sss.pgh.pa.us 10035 [ + - + + : 13549 : for_each_from(arg, expr->args, 1)
+ + ]
10036 : : {
4569 rhaas@postgresql.org 10037 : 7609 : appendStringInfoString(buf, " AND ");
8014 neilc@samurai.com 10038 : 7609 : get_rule_expr_paren((Node *) lfirst(arg), context,
10039 : : false, node);
10040 : : }
8315 tgl@sss.pgh.pa.us 10041 [ + + ]: 5940 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10042 : 5904 : appendStringInfoChar(buf, ')');
8545 tgl@sss.pgh.pa.us 10043 : 5940 : break;
10044 : :
10045 : 1251 : case OR_EXPR:
8315 10046 [ + + ]: 1251 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10047 : 1243 : appendStringInfoChar(buf, '(');
8014 neilc@samurai.com 10048 : 1251 : get_rule_expr_paren(first_arg, context,
10049 : : false, node);
2045 tgl@sss.pgh.pa.us 10050 [ + - + + : 2977 : for_each_from(arg, expr->args, 1)
+ + ]
10051 : : {
4569 rhaas@postgresql.org 10052 : 1726 : appendStringInfoString(buf, " OR ");
8014 neilc@samurai.com 10053 : 1726 : get_rule_expr_paren((Node *) lfirst(arg), context,
10054 : : false, node);
10055 : : }
8315 tgl@sss.pgh.pa.us 10056 [ + + ]: 1251 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10057 : 1243 : appendStringInfoChar(buf, ')');
8545 tgl@sss.pgh.pa.us 10058 : 1251 : break;
10059 : :
10060 : 300 : case NOT_EXPR:
8315 10061 [ + + ]: 300 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10062 : 292 : appendStringInfoChar(buf, '(');
4569 rhaas@postgresql.org 10063 : 300 : appendStringInfoString(buf, "NOT ");
8014 neilc@samurai.com 10064 : 300 : get_rule_expr_paren(first_arg, context,
10065 : : false, node);
8315 tgl@sss.pgh.pa.us 10066 [ + + ]: 300 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10067 : 292 : appendStringInfoChar(buf, ')');
8545 tgl@sss.pgh.pa.us 10068 : 300 : break;
10069 : :
8545 tgl@sss.pgh.pa.us 10070 :UBC 0 : default:
8318 10071 [ # # ]: 0 : elog(ERROR, "unrecognized boolop: %d",
10072 : : (int) expr->boolop);
10073 : : }
10074 : : }
8545 tgl@sss.pgh.pa.us 10075 :CBC 7491 : break;
10076 : :
10077 : 290 : case T_SubLink:
10078 : 290 : get_sublink_expr((SubLink *) node, context);
10079 : 290 : break;
10080 : :
8543 10081 : 536 : case T_SubPlan:
10082 : : {
6172 bruce@momjian.us 10083 : 536 : SubPlan *subplan = (SubPlan *) node;
10084 : :
10085 : : /*
10086 : : * We cannot see an already-planned subplan in rule deparsing,
10087 : : * only while EXPLAINing a query plan. We don't try to
10088 : : * reconstruct the original SQL, just reference the subplan
10089 : : * that appears elsewhere in EXPLAIN's result. It does seem
10090 : : * useful to show the subLinkType and testexpr (if any), and
10091 : : * we also note whether the subplan will be hashed.
10092 : : */
777 tgl@sss.pgh.pa.us 10093 [ + + + + : 536 : switch (subplan->subLinkType)
+ + + -
- ]
10094 : : {
10095 : 69 : case EXISTS_SUBLINK:
10096 : 69 : appendStringInfoString(buf, "EXISTS(");
10097 [ - + ]: 69 : Assert(subplan->testexpr == NULL);
10098 : 69 : break;
10099 : 4 : case ALL_SUBLINK:
10100 : 4 : appendStringInfoString(buf, "(ALL ");
10101 [ - + ]: 4 : Assert(subplan->testexpr != NULL);
10102 : 4 : break;
10103 : 160 : case ANY_SUBLINK:
10104 : 160 : appendStringInfoString(buf, "(ANY ");
10105 [ - + ]: 160 : Assert(subplan->testexpr != NULL);
10106 : 160 : break;
10107 : 4 : case ROWCOMPARE_SUBLINK:
10108 : : /* Parenthesizing the testexpr seems sufficient */
10109 : 4 : appendStringInfoChar(buf, '(');
10110 [ - + ]: 4 : Assert(subplan->testexpr != NULL);
10111 : 4 : break;
10112 : 274 : case EXPR_SUBLINK:
10113 : : /* No need to decorate these subplan references */
10114 : 274 : appendStringInfoChar(buf, '(');
10115 [ - + ]: 274 : Assert(subplan->testexpr == NULL);
10116 : 274 : break;
10117 : 17 : case MULTIEXPR_SUBLINK:
10118 : : /* MULTIEXPR isn't executed in the normal way */
10119 : 17 : appendStringInfoString(buf, "(rescan ");
10120 [ - + ]: 17 : Assert(subplan->testexpr == NULL);
10121 : 17 : break;
10122 : 8 : case ARRAY_SUBLINK:
10123 : 8 : appendStringInfoString(buf, "ARRAY(");
10124 [ - + ]: 8 : Assert(subplan->testexpr == NULL);
10125 : 8 : break;
777 tgl@sss.pgh.pa.us 10126 :UBC 0 : case CTE_SUBLINK:
10127 : : /* This case is unreachable within expressions */
10128 : 0 : appendStringInfoString(buf, "CTE(");
10129 [ # # ]: 0 : Assert(subplan->testexpr == NULL);
10130 : 0 : break;
10131 : : }
10132 : :
777 tgl@sss.pgh.pa.us 10133 [ + + ]:CBC 536 : if (subplan->testexpr != NULL)
10134 : : {
10135 : : deparse_namespace *dpns;
10136 : :
10137 : : /*
10138 : : * Push SubPlan into ancestors list while deparsing
10139 : : * testexpr, so that we can handle PARAM_EXEC references
10140 : : * to the SubPlan's paramIds. (This makes it look like
10141 : : * the SubPlan is an "ancestor" of the current plan node,
10142 : : * which is a little weird, but it does no harm.) In this
10143 : : * path, we don't need to mention the SubPlan explicitly,
10144 : : * because the referencing Params will show its existence.
10145 : : */
10146 : 168 : dpns = (deparse_namespace *) linitial(context->namespaces);
10147 : 168 : dpns->ancestors = lcons(subplan, dpns->ancestors);
10148 : :
10149 : 168 : get_rule_expr(subplan->testexpr, context, showimplicit);
10150 : 168 : appendStringInfoChar(buf, ')');
10151 : :
10152 : 168 : dpns->ancestors = list_delete_first(dpns->ancestors);
10153 : : }
10154 : : else
10155 : : {
10156 : : const char *nameprefix;
10157 : :
10158 : : /* No referencing Params, so show the SubPlan's name */
210 rhaas@postgresql.org 10159 [ - + ]:GNC 368 : if (subplan->isInitPlan)
210 rhaas@postgresql.org 10160 :UNC 0 : nameprefix = "InitPlan ";
10161 : : else
210 rhaas@postgresql.org 10162 :GNC 368 : nameprefix = "SubPlan ";
777 tgl@sss.pgh.pa.us 10163 [ - + ]:CBC 368 : if (subplan->useHashTable)
210 rhaas@postgresql.org 10164 :UNC 0 : appendStringInfo(buf, "hashed %s%s)",
10165 : : nameprefix, subplan->plan_name);
10166 : : else
210 rhaas@postgresql.org 10167 :GNC 368 : appendStringInfo(buf, "%s%s)",
10168 : : nameprefix, subplan->plan_name);
10169 : : }
10170 : : }
8545 tgl@sss.pgh.pa.us 10171 :CBC 536 : break;
10172 : :
6465 tgl@sss.pgh.pa.us 10173 :UBC 0 : case T_AlternativeSubPlan:
10174 : : {
6239 10175 : 0 : AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
10176 : : ListCell *lc;
10177 : :
10178 : : /*
10179 : : * This case cannot be reached in normal usage, since no
10180 : : * AlternativeSubPlan can appear either in parsetrees or
10181 : : * finished plan trees. We keep it just in case somebody
10182 : : * wants to use this code to print planner data structures.
10183 : : */
4569 rhaas@postgresql.org 10184 : 0 : appendStringInfoString(buf, "(alternatives: ");
6239 tgl@sss.pgh.pa.us 10185 [ # # # # : 0 : foreach(lc, asplan->subplans)
# # ]
10186 : : {
3312 10187 : 0 : SubPlan *splan = lfirst_node(SubPlan, lc);
10188 : : const char *nameprefix;
10189 : :
210 rhaas@postgresql.org 10190 [ # # ]:UNC 0 : if (splan->isInitPlan)
10191 : 0 : nameprefix = "InitPlan ";
10192 : : else
10193 : 0 : nameprefix = "SubPlan ";
6239 tgl@sss.pgh.pa.us 10194 [ # # ]:UBC 0 : if (splan->useHashTable)
210 rhaas@postgresql.org 10195 :UNC 0 : appendStringInfo(buf, "hashed %s%s", nameprefix,
10196 : : splan->plan_name);
10197 : : else
10198 : 0 : appendStringInfo(buf, "%s%s", nameprefix,
10199 : : splan->plan_name);
2486 tgl@sss.pgh.pa.us 10200 [ # # ]:UBC 0 : if (lnext(asplan->subplans, lc))
4569 rhaas@postgresql.org 10201 : 0 : appendStringInfoString(buf, " or ");
10202 : : }
10203 : 0 : appendStringInfoChar(buf, ')');
10204 : : }
6465 tgl@sss.pgh.pa.us 10205 : 0 : break;
10206 : :
9401 tgl@sss.pgh.pa.us 10207 :CBC 926 : case T_FieldSelect:
10208 : : {
10209 : 926 : FieldSelect *fselect = (FieldSelect *) node;
7644 10210 : 926 : Node *arg = (Node *) fselect->arg;
10211 : 926 : int fno = fselect->fieldnum;
10212 : : const char *fieldname;
10213 : : bool need_parens;
10214 : :
10215 : : /*
10216 : : * Parenthesize the argument unless it's a SubscriptingRef or
10217 : : * another FieldSelect. Note in particular that it would be
10218 : : * WRONG to not parenthesize a Var argument; simplicity is not
10219 : : * the issue here, having the right number of names is.
10220 : : */
2650 alvherre@alvh.no-ip. 10221 [ + + ]: 1828 : need_parens = !IsA(arg, SubscriptingRef) &&
10222 [ + - ]: 902 : !IsA(arg, FieldSelect);
8000 tgl@sss.pgh.pa.us 10223 [ + + ]: 926 : if (need_parens)
10224 : 902 : appendStringInfoChar(buf, '(');
7644 10225 : 926 : get_rule_expr(arg, context, true);
8000 10226 [ + + ]: 926 : if (need_parens)
10227 : 902 : appendStringInfoChar(buf, ')');
10228 : :
10229 : : /*
10230 : : * Get and print the field name.
10231 : : */
7011 10232 : 926 : fieldname = get_name_for_var_field((Var *) arg, fno,
10233 : : 0, context);
8315 10234 : 926 : appendStringInfo(buf, ".%s", quote_identifier(fieldname));
10235 : : }
9401 10236 : 926 : break;
10237 : :
8000 10238 : 4 : case T_FieldStore:
10239 : : {
5920 10240 : 4 : FieldStore *fstore = (FieldStore *) node;
10241 : : bool need_parens;
10242 : :
10243 : : /*
10244 : : * There is no good way to represent a FieldStore as real SQL,
10245 : : * so decompilation of INSERT or UPDATE statements should
10246 : : * always use processIndirection as part of the
10247 : : * statement-level syntax. We should only get here when
10248 : : * EXPLAIN tries to print the targetlist of a plan resulting
10249 : : * from such a statement. The plan case is even harder than
10250 : : * ordinary rules would be, because the planner tries to
10251 : : * collapse multiple assignments to the same field or subfield
10252 : : * into one FieldStore; so we can see a list of target fields
10253 : : * not just one, and the arguments could be FieldStores
10254 : : * themselves. We don't bother to try to print the target
10255 : : * field names; we just print the source arguments, with a
10256 : : * ROW() around them if there's more than one. This isn't
10257 : : * terribly complete, but it's probably good enough for
10258 : : * EXPLAIN's purposes; especially since anything more would be
10259 : : * either hopelessly confusing or an even poorer
10260 : : * representation of what the plan is actually doing.
10261 : : */
10262 : 4 : need_parens = (list_length(fstore->newvals) != 1);
10263 [ + - ]: 4 : if (need_parens)
10264 : 4 : appendStringInfoString(buf, "ROW(");
10265 : 4 : get_rule_expr((Node *) fstore->newvals, context, showimplicit);
10266 [ + - ]: 4 : if (need_parens)
10267 : 4 : appendStringInfoChar(buf, ')');
10268 : : }
8000 10269 : 4 : break;
10270 : :
9571 10271 : 2013 : case T_RelabelType:
10272 : : {
10273 : 2013 : RelabelType *relabel = (RelabelType *) node;
8310 bruce@momjian.us 10274 : 2013 : Node *arg = (Node *) relabel->arg;
10275 : :
8629 tgl@sss.pgh.pa.us 10276 [ + + ]: 2013 : if (relabel->relabelformat == COERCE_IMPLICIT_CAST &&
10277 [ + + ]: 1815 : !showimplicit)
10278 : : {
10279 : : /* don't show the implicit cast */
7993 10280 : 45 : get_rule_expr_paren(arg, context, false, node);
10281 : : }
10282 : : else
10283 : : {
6989 10284 : 1968 : get_coercion_expr(arg, context,
10285 : : relabel->resulttype,
10286 : : relabel->resulttypmod,
10287 : : node);
10288 : : }
10289 : : }
9571 10290 : 2013 : break;
10291 : :
6909 10292 : 497 : case T_CoerceViaIO:
10293 : : {
10294 : 497 : CoerceViaIO *iocoerce = (CoerceViaIO *) node;
10295 : 497 : Node *arg = (Node *) iocoerce->arg;
10296 : :
10297 [ + + ]: 497 : if (iocoerce->coerceformat == COERCE_IMPLICIT_CAST &&
10298 [ + - ]: 24 : !showimplicit)
10299 : : {
10300 : : /* don't show the implicit cast */
10301 : 24 : get_rule_expr_paren(arg, context, false, node);
10302 : : }
10303 : : else
10304 : : {
10305 : 473 : get_coercion_expr(arg, context,
10306 : : iocoerce->resulttype,
10307 : : -1,
10308 : : node);
10309 : : }
10310 : : }
10311 : 497 : break;
10312 : :
6979 10313 : 36 : case T_ArrayCoerceExpr:
10314 : : {
10315 : 36 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
10316 : 36 : Node *arg = (Node *) acoerce->arg;
10317 : :
10318 [ + - ]: 36 : if (acoerce->coerceformat == COERCE_IMPLICIT_CAST &&
10319 [ + + ]: 36 : !showimplicit)
10320 : : {
10321 : : /* don't show the implicit cast */
10322 : 4 : get_rule_expr_paren(arg, context, false, node);
10323 : : }
10324 : : else
10325 : : {
10326 : 32 : get_coercion_expr(arg, context,
10327 : : acoerce->resulttype,
10328 : : acoerce->resulttypmod,
10329 : : node);
10330 : : }
10331 : : }
10332 : 36 : break;
10333 : :
7815 10334 : 53 : case T_ConvertRowtypeExpr:
10335 : : {
10336 : 53 : ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
10337 : 53 : Node *arg = (Node *) convert->arg;
10338 : :
10339 [ + + ]: 53 : if (convert->convertformat == COERCE_IMPLICIT_CAST &&
10340 [ + + ]: 49 : !showimplicit)
10341 : : {
10342 : : /* don't show the implicit cast */
10343 : 12 : get_rule_expr_paren(arg, context, false, node);
10344 : : }
10345 : : else
10346 : : {
6989 10347 : 41 : get_coercion_expr(arg, context,
10348 : : convert->resulttype, -1,
10349 : : node);
10350 : : }
10351 : : }
7815 10352 : 53 : break;
10353 : :
5534 10354 : 122 : case T_CollateExpr:
10355 : : {
10356 : 122 : CollateExpr *collate = (CollateExpr *) node;
10357 : 122 : Node *arg = (Node *) collate->arg;
10358 : :
10359 [ + + ]: 122 : if (!PRETTY_PAREN(context))
10360 : 118 : appendStringInfoChar(buf, '(');
10361 : 122 : get_rule_expr_paren(arg, context, showimplicit, node);
10362 : 122 : appendStringInfo(buf, " COLLATE %s",
10363 : : generate_collation_name(collate->collOid));
10364 [ + + ]: 122 : if (!PRETTY_PAREN(context))
10365 : 118 : appendStringInfoChar(buf, ')');
10366 : : }
10367 : 122 : break;
10368 : :
9747 10369 : 457 : case T_CaseExpr:
10370 : : {
10371 : 457 : CaseExpr *caseexpr = (CaseExpr *) node;
10372 : : ListCell *temp;
10373 : :
8315 10374 : 457 : appendContextKeyword(context, "CASE",
10375 : : 0, PRETTYINDENT_VAR, 0);
8084 10376 [ + + ]: 457 : if (caseexpr->arg)
10377 : : {
10378 : 172 : appendStringInfoChar(buf, ' ');
10379 : 172 : get_rule_expr((Node *) caseexpr->arg, context, true);
10380 : : }
9747 10381 [ + - + + : 1927 : foreach(temp, caseexpr->args)
+ + ]
10382 : : {
10383 : 1470 : CaseWhen *when = (CaseWhen *) lfirst(temp);
7451 10384 : 1470 : Node *w = (Node *) when->expr;
10385 : :
8084 10386 [ + + ]: 1470 : if (caseexpr->arg)
10387 : : {
10388 : : /*
10389 : : * The parser should have produced WHEN clauses of the
10390 : : * form "CaseTestExpr = RHS", possibly with an
10391 : : * implicit coercion inserted above the CaseTestExpr.
10392 : : * For accurate decompilation of rules it's essential
10393 : : * that we show just the RHS. However in an
10394 : : * expression that's been through the optimizer, the
10395 : : * WHEN clause could be almost anything (since the
10396 : : * equality operator could have been expanded into an
10397 : : * inline function). If we don't recognize the form
10398 : : * of the WHEN clause, just punt and display it as-is.
10399 : : */
7451 10400 [ + - ]: 590 : if (IsA(w, OpExpr))
10401 : : {
6278 10402 : 590 : List *args = ((OpExpr *) w)->args;
10403 : :
5458 10404 [ + - ]: 590 : if (list_length(args) == 2 &&
10405 [ + - ]: 590 : IsA(strip_implicit_coercions(linitial(args)),
10406 : : CaseTestExpr))
10407 : 590 : w = (Node *) lsecond(args);
10408 : : }
10409 : : }
10410 : :
10411 [ + + ]: 1470 : if (!PRETTY_INDENT(context))
10412 : 80 : appendStringInfoChar(buf, ' ');
10413 : 1470 : appendContextKeyword(context, "WHEN ",
10414 : : 0, 0, 0);
10415 : 1470 : get_rule_expr(w, context, false);
4569 rhaas@postgresql.org 10416 : 1470 : appendStringInfoString(buf, " THEN ");
8545 tgl@sss.pgh.pa.us 10417 : 1470 : get_rule_expr((Node *) when->result, context, true);
10418 : : }
8315 10419 [ + + ]: 457 : if (!PRETTY_INDENT(context))
8310 bruce@momjian.us 10420 : 75 : appendStringInfoChar(buf, ' ');
8315 tgl@sss.pgh.pa.us 10421 : 457 : appendContextKeyword(context, "ELSE ",
10422 : : 0, 0, 0);
8545 10423 : 457 : get_rule_expr((Node *) caseexpr->defresult, context, true);
8315 10424 [ + + ]: 457 : if (!PRETTY_INDENT(context))
8310 bruce@momjian.us 10425 : 75 : appendStringInfoChar(buf, ' ');
8315 tgl@sss.pgh.pa.us 10426 : 457 : appendContextKeyword(context, "END",
10427 : : -PRETTYINDENT_VAR, 0, 0);
10428 : : }
10108 bruce@momjian.us 10429 : 457 : break;
10430 : :
5458 tgl@sss.pgh.pa.us 10431 :UBC 0 : case T_CaseTestExpr:
10432 : : {
10433 : : /*
10434 : : * Normally we should never get here, since for expressions
10435 : : * that can contain this node type we attempt to avoid
10436 : : * recursing to it. But in an optimized expression we might
10437 : : * be unable to avoid that (see comments for CaseExpr). If we
10438 : : * do see one, print it as CASE_TEST_EXPR.
10439 : : */
4569 rhaas@postgresql.org 10440 : 0 : appendStringInfoString(buf, "CASE_TEST_EXPR");
10441 : : }
5458 tgl@sss.pgh.pa.us 10442 : 0 : break;
10443 : :
8428 tgl@sss.pgh.pa.us 10444 :CBC 367 : case T_ArrayExpr:
10445 : : {
8310 bruce@momjian.us 10446 : 367 : ArrayExpr *arrayexpr = (ArrayExpr *) node;
10447 : :
4569 rhaas@postgresql.org 10448 : 367 : appendStringInfoString(buf, "ARRAY[");
7880 tgl@sss.pgh.pa.us 10449 : 367 : get_rule_expr((Node *) arrayexpr->elements, context, true);
10450 : 367 : appendStringInfoChar(buf, ']');
10451 : :
10452 : : /*
10453 : : * If the array isn't empty, we assume its elements are
10454 : : * coerced to the desired type. If it's empty, though, we
10455 : : * need an explicit coercion to the array type.
10456 : : */
6346 10457 [ + + ]: 367 : if (arrayexpr->elements == NIL)
10458 : 4 : appendStringInfo(buf, "::%s",
10459 : : format_type_with_typemod(arrayexpr->array_typeid, -1));
10460 : : }
8428 10461 : 367 : break;
10462 : :
8030 10463 : 142 : case T_RowExpr:
10464 : : {
7919 bruce@momjian.us 10465 : 142 : RowExpr *rowexpr = (RowExpr *) node;
7931 tgl@sss.pgh.pa.us 10466 : 142 : TupleDesc tupdesc = NULL;
10467 : : ListCell *arg;
10468 : : int i;
10469 : : char *sep;
10470 : :
10471 : : /*
10472 : : * If it's a named type and not RECORD, we may have to skip
10473 : : * dropped columns and/or claim there are NULLs for added
10474 : : * columns.
10475 : : */
10476 [ + + ]: 142 : if (rowexpr->row_typeid != RECORDOID)
10477 : : {
10478 : 44 : tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
10479 [ - + ]: 44 : Assert(list_length(rowexpr->args) <= tupdesc->natts);
10480 : : }
10481 : :
10482 : : /*
10483 : : * SQL99 allows "ROW" to be omitted when there is more than
10484 : : * one column, but for simplicity we always print it.
10485 : : */
4569 rhaas@postgresql.org 10486 : 142 : appendStringInfoString(buf, "ROW(");
8030 tgl@sss.pgh.pa.us 10487 : 142 : sep = "";
7931 10488 : 142 : i = 0;
8030 10489 [ + - + + : 432 : foreach(arg, rowexpr->args)
+ + ]
10490 : : {
10491 : 290 : Node *e = (Node *) lfirst(arg);
10492 : :
7931 10493 [ + + ]: 290 : if (tupdesc == NULL ||
195 drowley@postgresql.o 10494 [ + - ]:GNC 104 : !TupleDescCompactAttr(tupdesc, i)->attisdropped)
10495 : : {
7675 neilc@samurai.com 10496 :CBC 290 : appendStringInfoString(buf, sep);
10497 : : /* Whole-row Vars need special treatment here */
3824 tgl@sss.pgh.pa.us 10498 : 290 : get_rule_expr_toplevel(e, context, true);
7931 10499 : 290 : sep = ", ";
10500 : : }
10501 : 290 : i++;
10502 : : }
10503 [ + + ]: 142 : if (tupdesc != NULL)
10504 : : {
10505 [ - + ]: 44 : while (i < tupdesc->natts)
10506 : : {
195 drowley@postgresql.o 10507 [ # # ]:UNC 0 : if (!TupleDescCompactAttr(tupdesc, i)->attisdropped)
10508 : : {
7675 neilc@samurai.com 10509 :UBC 0 : appendStringInfoString(buf, sep);
4569 rhaas@postgresql.org 10510 : 0 : appendStringInfoString(buf, "NULL");
7931 tgl@sss.pgh.pa.us 10511 : 0 : sep = ", ";
10512 : : }
10513 : 0 : i++;
10514 : : }
10515 : :
7263 tgl@sss.pgh.pa.us 10516 [ + - ]:CBC 44 : ReleaseTupleDesc(tupdesc);
10517 : : }
4569 rhaas@postgresql.org 10518 : 142 : appendStringInfoChar(buf, ')');
8030 tgl@sss.pgh.pa.us 10519 [ + + ]: 142 : if (rowexpr->row_format == COERCE_EXPLICIT_CAST)
10520 : 24 : appendStringInfo(buf, "::%s",
10521 : : format_type_with_typemod(rowexpr->row_typeid, -1));
10522 : : }
10523 : 142 : break;
10524 : :
7433 10525 : 84 : case T_RowCompareExpr:
10526 : : {
10527 : 84 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
10528 : :
10529 : : /*
10530 : : * SQL99 allows "ROW" to be omitted when there is more than
10531 : : * one column, but for simplicity we always print it. Within
10532 : : * a ROW expression, whole-row Vars need special treatment, so
10533 : : * use get_rule_list_toplevel.
10534 : : */
4569 rhaas@postgresql.org 10535 : 84 : appendStringInfoString(buf, "(ROW(");
1573 tgl@sss.pgh.pa.us 10536 : 84 : get_rule_list_toplevel(rcexpr->largs, context, true);
10537 : :
10538 : : /*
10539 : : * We assume that the name of the first-column operator will
10540 : : * do for all the rest too. This is definitely open to
10541 : : * failure, eg if some but not all operators were renamed
10542 : : * since the construct was parsed, but there seems no way to
10543 : : * be perfect.
10544 : : */
7433 10545 : 84 : appendStringInfo(buf, ") %s ROW(",
3240 10546 : 84 : generate_operator_name(linitial_oid(rcexpr->opnos),
10547 : 84 : exprType(linitial(rcexpr->largs)),
10548 : 84 : exprType(linitial(rcexpr->rargs))));
1573 10549 : 84 : get_rule_list_toplevel(rcexpr->rargs, context, true);
4569 rhaas@postgresql.org 10550 : 84 : appendStringInfoString(buf, "))");
10551 : : }
7433 tgl@sss.pgh.pa.us 10552 : 84 : break;
10553 : :
8479 10554 : 833 : case T_CoalesceExpr:
10555 : : {
10556 : 833 : CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
10557 : :
4569 rhaas@postgresql.org 10558 : 833 : appendStringInfoString(buf, "COALESCE(");
7880 tgl@sss.pgh.pa.us 10559 : 833 : get_rule_expr((Node *) coalesceexpr->args, context, true);
10560 : 833 : appendStringInfoChar(buf, ')');
10561 : : }
8479 10562 : 833 : break;
10563 : :
7618 10564 : 28 : case T_MinMaxExpr:
10565 : : {
10566 : 28 : MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
10567 : :
10568 [ + + - ]: 28 : switch (minmaxexpr->op)
10569 : : {
10570 : 8 : case IS_GREATEST:
4569 rhaas@postgresql.org 10571 : 8 : appendStringInfoString(buf, "GREATEST(");
7618 tgl@sss.pgh.pa.us 10572 : 8 : break;
10573 : 20 : case IS_LEAST:
4569 rhaas@postgresql.org 10574 : 20 : appendStringInfoString(buf, "LEAST(");
7618 tgl@sss.pgh.pa.us 10575 : 20 : break;
10576 : : }
10577 : 28 : get_rule_expr((Node *) minmaxexpr->args, context, true);
10578 : 28 : appendStringInfoChar(buf, ')');
10579 : : }
10580 : 28 : break;
10581 : :
1084 michael@paquier.xyz 10582 : 449 : case T_SQLValueFunction:
10583 : : {
10584 : 449 : SQLValueFunction *svf = (SQLValueFunction *) node;
10585 : :
10586 : : /*
10587 : : * Note: this code knows that typmod for time, timestamp, and
10588 : : * timestamptz just prints as integer.
10589 : : */
10590 [ + + + + : 449 : switch (svf->op)
+ + + + +
+ + + + +
+ - ]
10591 : : {
10592 : 69 : case SVFOP_CURRENT_DATE:
10593 : 69 : appendStringInfoString(buf, "CURRENT_DATE");
10594 : 69 : break;
10595 : 8 : case SVFOP_CURRENT_TIME:
10596 : 8 : appendStringInfoString(buf, "CURRENT_TIME");
10597 : 8 : break;
10598 : 8 : case SVFOP_CURRENT_TIME_N:
10599 : 8 : appendStringInfo(buf, "CURRENT_TIME(%d)", svf->typmod);
10600 : 8 : break;
10601 : 8 : case SVFOP_CURRENT_TIMESTAMP:
10602 : 8 : appendStringInfoString(buf, "CURRENT_TIMESTAMP");
10603 : 8 : break;
10604 : 75 : case SVFOP_CURRENT_TIMESTAMP_N:
10605 : 75 : appendStringInfo(buf, "CURRENT_TIMESTAMP(%d)",
10606 : : svf->typmod);
10607 : 75 : break;
10608 : 8 : case SVFOP_LOCALTIME:
10609 : 8 : appendStringInfoString(buf, "LOCALTIME");
10610 : 8 : break;
10611 : 8 : case SVFOP_LOCALTIME_N:
10612 : 8 : appendStringInfo(buf, "LOCALTIME(%d)", svf->typmod);
10613 : 8 : break;
10614 : 20 : case SVFOP_LOCALTIMESTAMP:
10615 : 20 : appendStringInfoString(buf, "LOCALTIMESTAMP");
10616 : 20 : break;
10617 : 12 : case SVFOP_LOCALTIMESTAMP_N:
10618 : 12 : appendStringInfo(buf, "LOCALTIMESTAMP(%d)",
10619 : : svf->typmod);
10620 : 12 : break;
10621 : 8 : case SVFOP_CURRENT_ROLE:
10622 : 8 : appendStringInfoString(buf, "CURRENT_ROLE");
10623 : 8 : break;
10624 : 181 : case SVFOP_CURRENT_USER:
10625 : 181 : appendStringInfoString(buf, "CURRENT_USER");
10626 : 181 : break;
10627 : 8 : case SVFOP_USER:
10628 : 8 : appendStringInfoString(buf, "USER");
10629 : 8 : break;
10630 : 20 : case SVFOP_SESSION_USER:
10631 : 20 : appendStringInfoString(buf, "SESSION_USER");
10632 : 20 : break;
10633 : 8 : case SVFOP_CURRENT_CATALOG:
10634 : 8 : appendStringInfoString(buf, "CURRENT_CATALOG");
10635 : 8 : break;
10636 : 8 : case SVFOP_CURRENT_SCHEMA:
10637 : 8 : appendStringInfoString(buf, "CURRENT_SCHEMA");
10638 : 8 : break;
10639 : : }
10640 : : }
10641 : 449 : break;
10642 : :
7072 tgl@sss.pgh.pa.us 10643 : 99 : case T_XmlExpr:
10644 : : {
6746 bruce@momjian.us 10645 : 99 : XmlExpr *xexpr = (XmlExpr *) node;
10646 : 99 : bool needcomma = false;
10647 : : ListCell *arg;
10648 : : ListCell *narg;
10649 : : Const *con;
10650 : :
7072 tgl@sss.pgh.pa.us 10651 [ + + + + : 99 : switch (xexpr->op)
+ + + -
- ]
10652 : : {
10653 : 9 : case IS_XMLCONCAT:
10654 : 9 : appendStringInfoString(buf, "XMLCONCAT(");
10655 : 9 : break;
10656 : 18 : case IS_XMLELEMENT:
10657 : 18 : appendStringInfoString(buf, "XMLELEMENT(");
10658 : 18 : break;
10659 : 9 : case IS_XMLFOREST:
10660 : 9 : appendStringInfoString(buf, "XMLFOREST(");
10661 : 9 : break;
10662 : 9 : case IS_XMLPARSE:
10663 : 9 : appendStringInfoString(buf, "XMLPARSE(");
10664 : 9 : break;
10665 : 9 : case IS_XMLPI:
10666 : 9 : appendStringInfoString(buf, "XMLPI(");
10667 : 9 : break;
10668 : 9 : case IS_XMLROOT:
10669 : 9 : appendStringInfoString(buf, "XMLROOT(");
10670 : 9 : break;
7031 peter_e@gmx.net 10671 : 36 : case IS_XMLSERIALIZE:
10672 : 36 : appendStringInfoString(buf, "XMLSERIALIZE(");
10673 : 36 : break;
7051 peter_e@gmx.net 10674 :UBC 0 : case IS_DOCUMENT:
10675 : 0 : break;
10676 : : }
7031 peter_e@gmx.net 10677 [ + + + + ]:CBC 99 : if (xexpr->op == IS_XMLPARSE || xexpr->op == IS_XMLSERIALIZE)
10678 : : {
10679 [ + + ]: 45 : if (xexpr->xmloption == XMLOPTION_DOCUMENT)
10680 : 18 : appendStringInfoString(buf, "DOCUMENT ");
10681 : : else
10682 : 27 : appendStringInfoString(buf, "CONTENT ");
10683 : : }
7072 tgl@sss.pgh.pa.us 10684 [ + + ]: 99 : if (xexpr->name)
10685 : : {
10686 : 27 : appendStringInfo(buf, "NAME %s",
7067 peter_e@gmx.net 10687 : 27 : quote_identifier(map_xml_name_to_sql_identifier(xexpr->name)));
7072 tgl@sss.pgh.pa.us 10688 : 27 : needcomma = true;
10689 : : }
10690 [ + + ]: 99 : if (xexpr->named_args)
10691 : : {
10692 [ + + ]: 18 : if (xexpr->op != IS_XMLFOREST)
10693 : : {
10694 [ + - ]: 9 : if (needcomma)
10695 : 9 : appendStringInfoString(buf, ", ");
10696 : 9 : appendStringInfoString(buf, "XMLATTRIBUTES(");
10697 : 9 : needcomma = false;
10698 : : }
10699 [ + - + + : 63 : forboth(arg, xexpr->named_args, narg, xexpr->arg_names)
+ - + + +
+ + - +
+ ]
10700 : : {
6746 bruce@momjian.us 10701 : 45 : Node *e = (Node *) lfirst(arg);
10702 : 45 : char *argname = strVal(lfirst(narg));
10703 : :
7072 tgl@sss.pgh.pa.us 10704 [ + + ]: 45 : if (needcomma)
10705 : 27 : appendStringInfoString(buf, ", ");
154 peter@eisentraut.org 10706 :GNC 45 : get_rule_expr(e, context, true);
7072 tgl@sss.pgh.pa.us 10707 :CBC 45 : appendStringInfo(buf, " AS %s",
7067 peter_e@gmx.net 10708 : 45 : quote_identifier(map_xml_name_to_sql_identifier(argname)));
7072 tgl@sss.pgh.pa.us 10709 : 45 : needcomma = true;
10710 : : }
10711 [ + + ]: 18 : if (xexpr->op != IS_XMLFOREST)
10712 : 9 : appendStringInfoChar(buf, ')');
10713 : : }
10714 [ + + ]: 99 : if (xexpr->args)
10715 : : {
10716 [ + + ]: 90 : if (needcomma)
10717 : 27 : appendStringInfoString(buf, ", ");
10718 [ + + + - : 90 : switch (xexpr->op)
- ]
10719 : : {
10720 : 72 : case IS_XMLCONCAT:
10721 : : case IS_XMLELEMENT:
10722 : : case IS_XMLFOREST:
10723 : : case IS_XMLPI:
10724 : : case IS_XMLSERIALIZE:
10725 : : /* no extra decoration needed */
10726 : 72 : get_rule_expr((Node *) xexpr->args, context, true);
10727 : 72 : break;
10728 : 9 : case IS_XMLPARSE:
7031 peter_e@gmx.net 10729 [ - + ]: 9 : Assert(list_length(xexpr->args) == 2);
10730 : :
7072 tgl@sss.pgh.pa.us 10731 : 9 : get_rule_expr((Node *) linitial(xexpr->args),
10732 : : context, true);
10733 : :
3312 10734 : 9 : con = lsecond_node(Const, xexpr->args);
7072 10735 [ - + ]: 9 : Assert(!con->constisnull);
10736 [ - + ]: 9 : if (DatumGetBool(con->constvalue))
7072 tgl@sss.pgh.pa.us 10737 :UBC 0 : appendStringInfoString(buf,
10738 : : " PRESERVE WHITESPACE");
10739 : : else
7072 tgl@sss.pgh.pa.us 10740 :CBC 9 : appendStringInfoString(buf,
10741 : : " STRIP WHITESPACE");
10742 : 9 : break;
10743 : 9 : case IS_XMLROOT:
10744 [ - + ]: 9 : Assert(list_length(xexpr->args) == 3);
10745 : :
10746 : 9 : get_rule_expr((Node *) linitial(xexpr->args),
10747 : : context, true);
10748 : :
10749 : 9 : appendStringInfoString(buf, ", VERSION ");
10750 : 9 : con = (Const *) lsecond(xexpr->args);
10751 [ + - ]: 9 : if (IsA(con, Const) &&
10752 [ + - ]: 9 : con->constisnull)
10753 : 9 : appendStringInfoString(buf, "NO VALUE");
10754 : : else
7072 tgl@sss.pgh.pa.us 10755 :UBC 0 : get_rule_expr((Node *) con, context, false);
10756 : :
3312 tgl@sss.pgh.pa.us 10757 :CBC 9 : con = lthird_node(Const, xexpr->args);
7072 10758 [ + - ]: 9 : if (con->constisnull)
10759 : : /* suppress STANDALONE NO VALUE */ ;
10760 : : else
10761 : : {
7031 peter_e@gmx.net 10762 [ + - - - ]: 9 : switch (DatumGetInt32(con->constvalue))
10763 : : {
10764 : 9 : case XML_STANDALONE_YES:
10765 : 9 : appendStringInfoString(buf,
10766 : : ", STANDALONE YES");
10767 : 9 : break;
7031 peter_e@gmx.net 10768 :UBC 0 : case XML_STANDALONE_NO:
10769 : 0 : appendStringInfoString(buf,
10770 : : ", STANDALONE NO");
10771 : 0 : break;
10772 : 0 : case XML_STANDALONE_NO_VALUE:
10773 : 0 : appendStringInfoString(buf,
10774 : : ", STANDALONE NO VALUE");
10775 : 0 : break;
10776 : 0 : default:
10777 : 0 : break;
10778 : : }
10779 : : }
7072 tgl@sss.pgh.pa.us 10780 :CBC 9 : break;
7051 peter_e@gmx.net 10781 :UBC 0 : case IS_DOCUMENT:
10782 : 0 : get_rule_expr_paren((Node *) xexpr->args, context, false, node);
10783 : 0 : break;
10784 : : }
10785 : : }
7031 peter_e@gmx.net 10786 [ + + ]:CBC 99 : if (xexpr->op == IS_XMLSERIALIZE)
10787 : : {
5526 tgl@sss.pgh.pa.us 10788 : 36 : appendStringInfo(buf, " AS %s",
10789 : : format_type_with_typemod(xexpr->type,
10790 : : xexpr->typmod));
438 michael@paquier.xyz 10791 [ + + ]: 36 : if (xexpr->indent)
10792 : 9 : appendStringInfoString(buf, " INDENT");
10793 : : else
10794 : 27 : appendStringInfoString(buf, " NO INDENT");
10795 : : }
10796 : :
7051 peter_e@gmx.net 10797 [ - + ]: 99 : if (xexpr->op == IS_DOCUMENT)
7051 peter_e@gmx.net 10798 :UBC 0 : appendStringInfoString(buf, " IS DOCUMENT");
10799 : : else
7051 peter_e@gmx.net 10800 :CBC 99 : appendStringInfoChar(buf, ')');
10801 : : }
7072 tgl@sss.pgh.pa.us 10802 : 99 : break;
10803 : :
9086 10804 : 1838 : case T_NullTest:
10805 : : {
8958 bruce@momjian.us 10806 : 1838 : NullTest *ntest = (NullTest *) node;
10807 : :
8315 tgl@sss.pgh.pa.us 10808 [ + + ]: 1838 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10809 : 1798 : appendStringInfoChar(buf, '(');
8315 tgl@sss.pgh.pa.us 10810 : 1838 : get_rule_expr_paren((Node *) ntest->arg, context, true, node);
10811 : :
10812 : : /*
10813 : : * For scalar inputs, we prefer to print as IS [NOT] NULL,
10814 : : * which is shorter and traditional. If it's a rowtype input
10815 : : * but we're applying a scalar test, must print IS [NOT]
10816 : : * DISTINCT FROM NULL to be semantically correct.
10817 : : */
3568 10818 [ + + ]: 1838 : if (ntest->argisrow ||
10819 [ + + ]: 1800 : !type_is_rowtype(exprType((Node *) ntest->arg)))
10820 : : {
10821 [ + + - ]: 1818 : switch (ntest->nulltesttype)
10822 : : {
10823 : 580 : case IS_NULL:
10824 : 580 : appendStringInfoString(buf, " IS NULL");
10825 : 580 : break;
10826 : 1238 : case IS_NOT_NULL:
10827 : 1238 : appendStringInfoString(buf, " IS NOT NULL");
10828 : 1238 : break;
3568 tgl@sss.pgh.pa.us 10829 :UBC 0 : default:
10830 [ # # ]: 0 : elog(ERROR, "unrecognized nulltesttype: %d",
10831 : : (int) ntest->nulltesttype);
10832 : : }
10833 : : }
10834 : : else
10835 : : {
3568 tgl@sss.pgh.pa.us 10836 [ + + - ]:CBC 20 : switch (ntest->nulltesttype)
10837 : : {
10838 : 8 : case IS_NULL:
10839 : 8 : appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL");
10840 : 8 : break;
10841 : 12 : case IS_NOT_NULL:
10842 : 12 : appendStringInfoString(buf, " IS DISTINCT FROM NULL");
10843 : 12 : break;
3568 tgl@sss.pgh.pa.us 10844 :UBC 0 : default:
10845 [ # # ]: 0 : elog(ERROR, "unrecognized nulltesttype: %d",
10846 : : (int) ntest->nulltesttype);
10847 : : }
10848 : : }
8315 tgl@sss.pgh.pa.us 10849 [ + + ]:CBC 1838 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10850 : 1798 : appendStringInfoChar(buf, ')');
10851 : : }
9086 tgl@sss.pgh.pa.us 10852 : 1838 : break;
10853 : :
10854 : 212 : case T_BooleanTest:
10855 : : {
8958 bruce@momjian.us 10856 : 212 : BooleanTest *btest = (BooleanTest *) node;
10857 : :
8315 tgl@sss.pgh.pa.us 10858 [ + - ]: 212 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10859 : 212 : appendStringInfoChar(buf, '(');
8315 tgl@sss.pgh.pa.us 10860 : 212 : get_rule_expr_paren((Node *) btest->arg, context, false, node);
8958 bruce@momjian.us 10861 [ + + - + : 212 : switch (btest->booltesttype)
+ + - ]
10862 : : {
10863 : 28 : case IS_TRUE:
4569 rhaas@postgresql.org 10864 : 28 : appendStringInfoString(buf, " IS TRUE");
9086 tgl@sss.pgh.pa.us 10865 : 28 : break;
8958 bruce@momjian.us 10866 : 92 : case IS_NOT_TRUE:
4569 rhaas@postgresql.org 10867 : 92 : appendStringInfoString(buf, " IS NOT TRUE");
9086 tgl@sss.pgh.pa.us 10868 : 92 : break;
8958 bruce@momjian.us 10869 :UBC 0 : case IS_FALSE:
4569 rhaas@postgresql.org 10870 : 0 : appendStringInfoString(buf, " IS FALSE");
9086 tgl@sss.pgh.pa.us 10871 : 0 : break;
8958 bruce@momjian.us 10872 :CBC 36 : case IS_NOT_FALSE:
4569 rhaas@postgresql.org 10873 : 36 : appendStringInfoString(buf, " IS NOT FALSE");
9086 tgl@sss.pgh.pa.us 10874 : 36 : break;
8958 bruce@momjian.us 10875 : 20 : case IS_UNKNOWN:
4569 rhaas@postgresql.org 10876 : 20 : appendStringInfoString(buf, " IS UNKNOWN");
9086 tgl@sss.pgh.pa.us 10877 : 20 : break;
8958 bruce@momjian.us 10878 : 36 : case IS_NOT_UNKNOWN:
4569 rhaas@postgresql.org 10879 : 36 : appendStringInfoString(buf, " IS NOT UNKNOWN");
9086 tgl@sss.pgh.pa.us 10880 : 36 : break;
8958 bruce@momjian.us 10881 :UBC 0 : default:
8318 tgl@sss.pgh.pa.us 10882 [ # # ]: 0 : elog(ERROR, "unrecognized booltesttype: %d",
10883 : : (int) btest->booltesttype);
10884 : : }
8315 tgl@sss.pgh.pa.us 10885 [ + - ]:CBC 212 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 10886 : 212 : appendStringInfoChar(buf, ')');
10887 : : }
9086 tgl@sss.pgh.pa.us 10888 : 212 : break;
10889 : :
8492 10890 : 79 : case T_CoerceToDomain:
10891 : : {
10892 : 79 : CoerceToDomain *ctest = (CoerceToDomain *) node;
8310 bruce@momjian.us 10893 : 79 : Node *arg = (Node *) ctest->arg;
10894 : :
8492 tgl@sss.pgh.pa.us 10895 [ + + ]: 79 : if (ctest->coercionformat == COERCE_IMPLICIT_CAST &&
10896 [ + + ]: 36 : !showimplicit)
10897 : : {
10898 : : /* don't show the implicit cast */
10899 : 28 : get_rule_expr(arg, context, false);
10900 : : }
10901 : : else
10902 : : {
6989 10903 : 51 : get_coercion_expr(arg, context,
10904 : : ctest->resulttype,
10905 : : ctest->resulttypmod,
10906 : : node);
10907 : : }
10908 : : }
8648 10909 : 79 : break;
10910 : :
8492 10911 : 274 : case T_CoerceToDomainValue:
4569 rhaas@postgresql.org 10912 : 274 : appendStringInfoString(buf, "VALUE");
8820 tgl@sss.pgh.pa.us 10913 : 274 : break;
10914 : :
8342 10915 : 44 : case T_SetToDefault:
4569 rhaas@postgresql.org 10916 : 44 : appendStringInfoString(buf, "DEFAULT");
8342 tgl@sss.pgh.pa.us 10917 : 44 : break;
10918 : :
6903 10919 : 16 : case T_CurrentOfExpr:
10920 : : {
10921 : 16 : CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
10922 : :
10923 [ + - ]: 16 : if (cexpr->cursor_name)
10924 : 16 : appendStringInfo(buf, "CURRENT OF %s",
10925 : 16 : quote_identifier(cexpr->cursor_name));
10926 : : else
6903 tgl@sss.pgh.pa.us 10927 :UBC 0 : appendStringInfo(buf, "CURRENT OF $%d",
10928 : : cexpr->cursor_param);
10929 : : }
6903 tgl@sss.pgh.pa.us 10930 :CBC 16 : break;
10931 : :
3217 tgl@sss.pgh.pa.us 10932 :UBC 0 : case T_NextValueExpr:
10933 : : {
10934 : 0 : NextValueExpr *nvexpr = (NextValueExpr *) node;
10935 : :
10936 : : /*
10937 : : * This isn't exactly nextval(), but that seems close enough
10938 : : * for EXPLAIN's purposes.
10939 : : */
10940 : 0 : appendStringInfoString(buf, "nextval(");
10941 : 0 : simple_quote_literal(buf,
10942 : 0 : generate_relation_name(nvexpr->seqid,
10943 : : NIL));
10944 : 0 : appendStringInfoChar(buf, ')');
10945 : : }
10946 : 0 : break;
10947 : :
4004 andres@anarazel.de 10948 :CBC 20 : case T_InferenceElem:
10949 : : {
4000 bruce@momjian.us 10950 : 20 : InferenceElem *iexpr = (InferenceElem *) node;
10951 : : bool save_varprefix;
10952 : : bool need_parens;
10953 : :
10954 : : /*
10955 : : * InferenceElem can only refer to target relation, so a
10956 : : * prefix is not useful, and indeed would cause parse errors.
10957 : : */
3740 tgl@sss.pgh.pa.us 10958 : 20 : save_varprefix = context->varprefix;
4004 andres@anarazel.de 10959 : 20 : context->varprefix = false;
10960 : :
10961 : : /*
10962 : : * Parenthesize the element unless it's a simple Var or a bare
10963 : : * function call. Follows pg_get_indexdef_worker().
10964 : : */
10965 : 20 : need_parens = !IsA(iexpr->expr, Var);
10966 [ - + ]: 20 : if (IsA(iexpr->expr, FuncExpr) &&
4004 andres@anarazel.de 10967 [ # # ]:UBC 0 : ((FuncExpr *) iexpr->expr)->funcformat ==
10968 : : COERCE_EXPLICIT_CALL)
10969 : 0 : need_parens = false;
10970 : :
4004 andres@anarazel.de 10971 [ - + ]:CBC 20 : if (need_parens)
4004 andres@anarazel.de 10972 :UBC 0 : appendStringInfoChar(buf, '(');
4004 andres@anarazel.de 10973 :CBC 20 : get_rule_expr((Node *) iexpr->expr,
10974 : : context, false);
10975 [ - + ]: 20 : if (need_parens)
4004 andres@anarazel.de 10976 :UBC 0 : appendStringInfoChar(buf, ')');
10977 : :
3740 tgl@sss.pgh.pa.us 10978 :CBC 20 : context->varprefix = save_varprefix;
10979 : :
4004 andres@anarazel.de 10980 [ + + ]: 20 : if (iexpr->infercollid)
10981 : 8 : appendStringInfo(buf, " COLLATE %s",
10982 : : generate_collation_name(iexpr->infercollid));
10983 : :
10984 : : /* Add the operator class name, if not default */
10985 [ + + ]: 20 : if (iexpr->inferopclass)
10986 : : {
4000 bruce@momjian.us 10987 : 8 : Oid inferopclass = iexpr->inferopclass;
10988 : 8 : Oid inferopcinputtype = get_opclass_input_type(iexpr->inferopclass);
10989 : :
4004 andres@anarazel.de 10990 : 8 : get_opclass_name(inferopclass, inferopcinputtype, buf);
10991 : : }
10992 : : }
10993 : 20 : break;
10994 : :
474 dean.a.rasheed@gmail 10995 : 8 : case T_ReturningExpr:
10996 : : {
10997 : 8 : ReturningExpr *retExpr = (ReturningExpr *) node;
10998 : :
10999 : : /*
11000 : : * We cannot see a ReturningExpr in rule deparsing, only while
11001 : : * EXPLAINing a query plan (ReturningExpr nodes are only ever
11002 : : * adding during query rewriting). Just display the expression
11003 : : * returned (an expanded view column).
11004 : : */
11005 : 8 : get_rule_expr((Node *) retExpr->retexpr, context, showimplicit);
11006 : : }
11007 : 8 : break;
11008 : :
3436 rhaas@postgresql.org 11009 : 2746 : case T_PartitionBoundSpec:
11010 : : {
11011 : 2746 : PartitionBoundSpec *spec = (PartitionBoundSpec *) node;
11012 : : ListCell *cell;
11013 : : char *sep;
11014 : :
3161 11015 [ + + ]: 2746 : if (spec->is_default)
11016 : : {
11017 : 161 : appendStringInfoString(buf, "DEFAULT");
11018 : 161 : break;
11019 : : }
11020 : :
3436 11021 [ + + + - ]: 2585 : switch (spec->strategy)
11022 : : {
3099 11023 : 161 : case PARTITION_STRATEGY_HASH:
11024 [ + - - + ]: 161 : Assert(spec->modulus > 0 && spec->remainder >= 0);
11025 [ - + ]: 161 : Assert(spec->modulus > spec->remainder);
11026 : :
11027 : 161 : appendStringInfoString(buf, "FOR VALUES");
11028 : 161 : appendStringInfo(buf, " WITH (modulus %d, remainder %d)",
11029 : : spec->modulus, spec->remainder);
11030 : 161 : break;
11031 : :
3436 11032 : 865 : case PARTITION_STRATEGY_LIST:
11033 [ - + ]: 865 : Assert(spec->listdatums != NIL);
11034 : :
3264 tgl@sss.pgh.pa.us 11035 : 865 : appendStringInfoString(buf, "FOR VALUES IN (");
3436 rhaas@postgresql.org 11036 : 865 : sep = "";
3388 11037 [ + - + + : 2358 : foreach(cell, spec->listdatums)
+ + ]
11038 : : {
1751 peter@eisentraut.org 11039 : 1493 : Const *val = lfirst_node(Const, cell);
11040 : :
3436 rhaas@postgresql.org 11041 : 1493 : appendStringInfoString(buf, sep);
11042 : 1493 : get_const_expr(val, context, -1);
11043 : 1493 : sep = ", ";
11044 : : }
11045 : :
3185 peter_e@gmx.net 11046 : 865 : appendStringInfoChar(buf, ')');
3436 rhaas@postgresql.org 11047 : 865 : break;
11048 : :
11049 : 1559 : case PARTITION_STRATEGY_RANGE:
11050 [ + - + - : 1559 : Assert(spec->lowerdatums != NIL &&
- + ]
11051 : : spec->upperdatums != NIL &&
11052 : : list_length(spec->lowerdatums) ==
11053 : : list_length(spec->upperdatums));
11054 : :
3190 11055 : 1559 : appendStringInfo(buf, "FOR VALUES FROM %s TO %s",
11056 : : get_range_partbound_string(spec->lowerdatums),
11057 : : get_range_partbound_string(spec->upperdatums));
3436 11058 : 1559 : break;
11059 : :
3436 rhaas@postgresql.org 11060 :UBC 0 : default:
11061 [ # # ]: 0 : elog(ERROR, "unrecognized partition strategy: %d",
11062 : : (int) spec->strategy);
11063 : : break;
11064 : : }
11065 : : }
3436 rhaas@postgresql.org 11066 :CBC 2585 : break;
11067 : :
1133 alvherre@alvh.no-ip. 11068 : 104 : case T_JsonValueExpr:
11069 : : {
11070 : 104 : JsonValueExpr *jve = (JsonValueExpr *) node;
11071 : :
11072 : 104 : get_rule_expr((Node *) jve->raw_expr, context, false);
11073 : 104 : get_json_format(jve->format, context->buf);
11074 : : }
11075 : 104 : break;
11076 : :
11077 : 148 : case T_JsonConstructorExpr:
11078 : 148 : get_json_constructor((JsonConstructorExpr *) node, context, false);
11079 : 148 : break;
11080 : :
1131 11081 : 52 : case T_JsonIsPredicate:
11082 : : {
11083 : 52 : JsonIsPredicate *pred = (JsonIsPredicate *) node;
11084 : :
11085 [ + + ]: 52 : if (!PRETTY_PAREN(context))
11086 : 20 : appendStringInfoChar(context->buf, '(');
11087 : :
11088 : 52 : get_rule_expr_paren(pred->expr, context, true, node);
11089 : :
11090 : 52 : appendStringInfoString(context->buf, " IS JSON");
11091 : :
11092 : : /* TODO: handle FORMAT clause */
11093 : :
11094 [ + + + + ]: 52 : switch (pred->item_type)
11095 : : {
11096 : 8 : case JS_TYPE_SCALAR:
11097 : 8 : appendStringInfoString(context->buf, " SCALAR");
11098 : 8 : break;
11099 : 8 : case JS_TYPE_ARRAY:
11100 : 8 : appendStringInfoString(context->buf, " ARRAY");
11101 : 8 : break;
11102 : 8 : case JS_TYPE_OBJECT:
11103 : 8 : appendStringInfoString(context->buf, " OBJECT");
11104 : 8 : break;
11105 : 28 : default:
11106 : 28 : break;
11107 : : }
11108 : :
11109 [ + + ]: 52 : if (pred->unique_keys)
11110 : 20 : appendStringInfoString(context->buf, " WITH UNIQUE KEYS");
11111 : :
11112 [ + + ]: 52 : if (!PRETTY_PAREN(context))
11113 : 20 : appendStringInfoChar(context->buf, ')');
11114 : : }
11115 : 52 : break;
11116 : :
775 amitlan@postgresql.o 11117 : 40 : case T_JsonExpr:
11118 : : {
11119 : 40 : JsonExpr *jexpr = (JsonExpr *) node;
11120 : :
11121 [ + + + - ]: 40 : switch (jexpr->op)
11122 : : {
11123 : 8 : case JSON_EXISTS_OP:
11124 : 8 : appendStringInfoString(buf, "JSON_EXISTS(");
11125 : 8 : break;
11126 : 24 : case JSON_QUERY_OP:
11127 : 24 : appendStringInfoString(buf, "JSON_QUERY(");
11128 : 24 : break;
11129 : 8 : case JSON_VALUE_OP:
11130 : 8 : appendStringInfoString(buf, "JSON_VALUE(");
11131 : 8 : break;
775 amitlan@postgresql.o 11132 :UBC 0 : default:
11133 [ # # ]: 0 : elog(ERROR, "unrecognized JsonExpr op: %d",
11134 : : (int) jexpr->op);
11135 : : }
11136 : :
775 amitlan@postgresql.o 11137 :CBC 40 : get_rule_expr(jexpr->formatted_expr, context, showimplicit);
11138 : :
11139 : 40 : appendStringInfoString(buf, ", ");
11140 : :
11141 : 40 : get_json_path_spec(jexpr->path_spec, context, showimplicit);
11142 : :
11143 [ + + ]: 40 : if (jexpr->passing_values)
11144 : : {
11145 : : ListCell *lc1,
11146 : : *lc2;
11147 : 8 : bool needcomma = false;
11148 : :
11149 : 8 : appendStringInfoString(buf, " PASSING ");
11150 : :
11151 [ + - + + : 32 : forboth(lc1, jexpr->passing_names,
+ - + + +
+ + - +
+ ]
11152 : : lc2, jexpr->passing_values)
11153 : : {
11154 [ + + ]: 24 : if (needcomma)
11155 : 16 : appendStringInfoString(buf, ", ");
11156 : 24 : needcomma = true;
11157 : :
11158 : 24 : get_rule_expr((Node *) lfirst(lc2), context, showimplicit);
11159 : 24 : appendStringInfo(buf, " AS %s",
478 dean.a.rasheed@gmail 11160 : 24 : quote_identifier(lfirst_node(String, lc1)->sval));
11161 : : }
11162 : : }
11163 : :
775 amitlan@postgresql.o 11164 [ + + ]: 40 : if (jexpr->op != JSON_EXISTS_OP ||
11165 [ - + ]: 8 : jexpr->returning->typid != BOOLOID)
11166 : 32 : get_json_returning(jexpr->returning, context->buf,
11167 : 32 : jexpr->op == JSON_QUERY_OP);
11168 : :
11169 : 40 : get_json_expr_options(jexpr, context,
11170 [ + + ]: 40 : jexpr->op != JSON_EXISTS_OP ?
11171 : : JSON_BEHAVIOR_NULL :
11172 : : JSON_BEHAVIOR_FALSE);
11173 : :
755 drowley@postgresql.o 11174 : 40 : appendStringInfoChar(buf, ')');
11175 : : }
775 amitlan@postgresql.o 11176 : 40 : break;
11177 : :
7880 tgl@sss.pgh.pa.us 11178 : 1842 : case T_List:
11179 : : {
11180 : : char *sep;
11181 : : ListCell *l;
11182 : :
11183 : 1842 : sep = "";
11184 [ + - + + : 5228 : foreach(l, (List *) node)
+ + ]
11185 : : {
7675 neilc@samurai.com 11186 : 3386 : appendStringInfoString(buf, sep);
7880 tgl@sss.pgh.pa.us 11187 : 3386 : get_rule_expr((Node *) lfirst(l), context, showimplicit);
11188 : 3386 : sep = ", ";
11189 : : }
11190 : : }
11191 : 1842 : break;
11192 : :
3345 alvherre@alvh.no-ip. 11193 : 48 : case T_TableFunc:
11194 : 48 : get_tablefunc((TableFunc *) node, context, showimplicit);
11195 : 48 : break;
11196 : :
50 peter@eisentraut.org 11197 :GNC 46 : case T_GraphPropertyRef:
11198 : : {
11199 : 46 : GraphPropertyRef *gpr = (GraphPropertyRef *) node;
11200 : :
11201 : 46 : appendStringInfo(buf, "%s.%s", quote_identifier(gpr->elvarname), quote_identifier(get_propgraph_property_name(gpr->propid)));
11202 : 46 : break;
11203 : : }
11204 : :
10108 bruce@momjian.us 11205 :UBC 0 : default:
8318 tgl@sss.pgh.pa.us 11206 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
11207 : : break;
11208 : : }
11209 : : }
11210 : :
11211 : : /*
11212 : : * get_rule_expr_toplevel - Parse back a toplevel expression
11213 : : *
11214 : : * Same as get_rule_expr(), except that if the expr is just a Var, we pass
11215 : : * istoplevel = true not false to get_variable(). This causes whole-row Vars
11216 : : * to get printed with decoration that will prevent expansion of "*".
11217 : : * We need to use this in contexts such as ROW() and VALUES(), where the
11218 : : * parser would expand "foo.*" appearing at top level. (In principle we'd
11219 : : * use this in get_target_list() too, but that has additional worries about
11220 : : * whether to print AS, so it needs to invoke get_variable() directly anyway.)
11221 : : */
11222 : : static void
3824 tgl@sss.pgh.pa.us 11223 :CBC 2043 : get_rule_expr_toplevel(Node *node, deparse_context *context,
11224 : : bool showimplicit)
11225 : : {
11226 [ + - + + ]: 2043 : if (node && IsA(node, Var))
11227 : 798 : (void) get_variable((Var *) node, 0, true, context);
11228 : : else
11229 : 1245 : get_rule_expr(node, context, showimplicit);
11230 : 2043 : }
11231 : :
11232 : : /*
11233 : : * get_rule_list_toplevel - Parse back a list of toplevel expressions
11234 : : *
11235 : : * Apply get_rule_expr_toplevel() to each element of a List.
11236 : : *
11237 : : * This adds commas between the expressions, but caller is responsible
11238 : : * for printing surrounding decoration.
11239 : : */
11240 : : static void
1573 11241 : 326 : get_rule_list_toplevel(List *lst, deparse_context *context,
11242 : : bool showimplicit)
11243 : : {
11244 : : const char *sep;
11245 : : ListCell *lc;
11246 : :
11247 : 326 : sep = "";
11248 [ + - + + : 1087 : foreach(lc, lst)
+ + ]
11249 : : {
11250 : 761 : Node *e = (Node *) lfirst(lc);
11251 : :
11252 : 761 : appendStringInfoString(context->buf, sep);
11253 : 761 : get_rule_expr_toplevel(e, context, showimplicit);
11254 : 761 : sep = ", ";
11255 : : }
11256 : 326 : }
11257 : :
11258 : : /*
11259 : : * get_rule_expr_funccall - Parse back a function-call expression
11260 : : *
11261 : : * Same as get_rule_expr(), except that we guarantee that the output will
11262 : : * look like a function call, or like one of the things the grammar treats as
11263 : : * equivalent to a function call (see the func_expr_windowless production).
11264 : : * This is needed in places where the grammar uses func_expr_windowless and
11265 : : * you can't substitute a parenthesized a_expr. If what we have isn't going
11266 : : * to look like a function call, wrap it in a dummy CAST() expression, which
11267 : : * will satisfy the grammar --- and, indeed, is likely what the user wrote to
11268 : : * produce such a thing.
11269 : : */
11270 : : static void
3218 11271 : 565 : get_rule_expr_funccall(Node *node, deparse_context *context,
11272 : : bool showimplicit)
11273 : : {
11274 [ + + ]: 565 : if (looks_like_function(node))
11275 : 557 : get_rule_expr(node, context, showimplicit);
11276 : : else
11277 : : {
11278 : 8 : StringInfo buf = context->buf;
11279 : :
11280 : 8 : appendStringInfoString(buf, "CAST(");
11281 : : /* no point in showing any top-level implicit cast */
11282 : 8 : get_rule_expr(node, context, false);
11283 : 8 : appendStringInfo(buf, " AS %s)",
11284 : : format_type_with_typemod(exprType(node),
11285 : : exprTypmod(node)));
11286 : : }
11287 : 565 : }
11288 : :
11289 : : /*
11290 : : * Helper function to identify node types that satisfy func_expr_windowless.
11291 : : * If in doubt, "false" is always a safe answer.
11292 : : */
11293 : : static bool
11294 : 1294 : looks_like_function(Node *node)
11295 : : {
11296 [ - + ]: 1294 : if (node == NULL)
3218 tgl@sss.pgh.pa.us 11297 :UBC 0 : return false; /* probably shouldn't happen */
3218 tgl@sss.pgh.pa.us 11298 [ + + + ]:CBC 1294 : switch (nodeTag(node))
11299 : : {
11300 : 577 : case T_FuncExpr:
11301 : : /* OK, unless it's going to deparse as a cast */
2008 11302 [ + + ]: 589 : return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL ||
11303 [ + + ]: 12 : ((FuncExpr *) node)->funcformat == COERCE_SQL_SYNTAX);
3218 11304 : 72 : case T_NullIfExpr:
11305 : : case T_CoalesceExpr:
11306 : : case T_MinMaxExpr:
11307 : : case T_SQLValueFunction:
11308 : : case T_XmlExpr:
11309 : : case T_JsonExpr:
11310 : : /* these are all accepted by func_expr_common_subexpr */
11311 : 72 : return true;
11312 : 645 : default:
11313 : 645 : break;
11314 : : }
11315 : 645 : return false;
11316 : : }
11317 : :
11318 : :
11319 : : /*
11320 : : * get_oper_expr - Parse back an OpExpr node
11321 : : */
11322 : : static void
8306 bruce@momjian.us 11323 : 40741 : get_oper_expr(OpExpr *expr, deparse_context *context)
11324 : : {
8768 tgl@sss.pgh.pa.us 11325 : 40741 : StringInfo buf = context->buf;
8545 11326 : 40741 : Oid opno = expr->opno;
8768 11327 : 40741 : List *args = expr->args;
11328 : :
8315 11329 [ + + ]: 40741 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 11330 : 39121 : appendStringInfoChar(buf, '(');
8010 neilc@samurai.com 11331 [ + + ]: 40741 : if (list_length(args) == 2)
11332 : : {
11333 : : /* binary operator */
8014 11334 : 40721 : Node *arg1 = (Node *) linitial(args);
8644 bruce@momjian.us 11335 : 40721 : Node *arg2 = (Node *) lsecond(args);
11336 : :
8310 11337 : 40721 : get_rule_expr_paren(arg1, context, true, (Node *) expr);
8768 tgl@sss.pgh.pa.us 11338 : 40721 : appendStringInfo(buf, " %s ",
11339 : : generate_operator_name(opno,
11340 : : exprType(arg1),
11341 : : exprType(arg2)));
8310 bruce@momjian.us 11342 : 40721 : get_rule_expr_paren(arg2, context, true, (Node *) expr);
11343 : : }
11344 : : else
11345 : : {
11346 : : /* prefix operator */
8014 neilc@samurai.com 11347 : 20 : Node *arg = (Node *) linitial(args);
11348 : :
2056 tgl@sss.pgh.pa.us 11349 : 20 : appendStringInfo(buf, "%s ",
11350 : : generate_operator_name(opno,
11351 : : InvalidOid,
11352 : : exprType(arg)));
11353 : 20 : get_rule_expr_paren(arg, context, true, (Node *) expr);
11354 : : }
8315 11355 [ + + ]: 40741 : if (!PRETTY_PAREN(context))
8310 bruce@momjian.us 11356 : 39121 : appendStringInfoChar(buf, ')');
8768 tgl@sss.pgh.pa.us 11357 : 40741 : }
11358 : :
11359 : : /*
11360 : : * get_func_expr - Parse back a FuncExpr node
11361 : : */
11362 : : static void
8306 bruce@momjian.us 11363 : 8158 : get_func_expr(FuncExpr *expr, deparse_context *context,
11364 : : bool showimplicit)
11365 : : {
9711 tgl@sss.pgh.pa.us 11366 : 8158 : StringInfo buf = context->buf;
8545 11367 : 8158 : Oid funcoid = expr->funcid;
11368 : : Oid argtypes[FUNC_MAX_ARGS];
11369 : : int nargs;
11370 : : List *argnames;
11371 : : bool use_variadic;
11372 : : ListCell *l;
11373 : :
11374 : : /*
11375 : : * If the function call came from an implicit coercion, then just show the
11376 : : * first argument --- unless caller wants to see implicit coercions.
11377 : : */
11378 [ + + + + ]: 8158 : if (expr->funcformat == COERCE_IMPLICIT_CAST && !showimplicit)
11379 : : {
8014 neilc@samurai.com 11380 : 769 : get_rule_expr_paren((Node *) linitial(expr->args), context,
11381 : : false, (Node *) expr);
8630 tgl@sss.pgh.pa.us 11382 : 2025 : return;
11383 : : }
11384 : :
11385 : : /*
11386 : : * If the function call came from a cast, then show the first argument
11387 : : * plus an explicit cast operation.
11388 : : */
8545 11389 [ + + ]: 7389 : if (expr->funcformat == COERCE_EXPLICIT_CAST ||
11390 [ + + ]: 6932 : expr->funcformat == COERCE_IMPLICIT_CAST)
11391 : : {
8014 neilc@samurai.com 11392 : 1140 : Node *arg = linitial(expr->args);
8545 tgl@sss.pgh.pa.us 11393 : 1140 : Oid rettype = expr->funcresulttype;
11394 : : int32 coercedTypmod;
11395 : :
11396 : : /* Get the typmod if this is a length-coercion function */
8630 11397 : 1140 : (void) exprIsLengthCoercion((Node *) expr, &coercedTypmod);
11398 : :
6989 11399 : 1140 : get_coercion_expr(arg, context,
11400 : : rettype, coercedTypmod,
11401 : : (Node *) expr);
11402 : :
9565 11403 : 1140 : return;
11404 : : }
11405 : :
11406 : : /*
11407 : : * If the function was called using one of the SQL spec's random special
11408 : : * syntaxes, try to reproduce that. If we don't recognize the function,
11409 : : * fall through.
11410 : : */
2008 11411 [ + + ]: 6249 : if (expr->funcformat == COERCE_SQL_SYNTAX)
11412 : : {
11413 [ + + ]: 120 : if (get_func_sql_syntax(expr, context))
11414 : 116 : return;
11415 : : }
11416 : :
11417 : : /*
11418 : : * Normal function: display as proname(args). First we need to extract
11419 : : * the argument datatypes.
11420 : : */
6337 11421 [ - + ]: 6133 : if (list_length(expr->args) > FUNC_MAX_ARGS)
6337 tgl@sss.pgh.pa.us 11422 [ # # ]:UBC 0 : ereport(ERROR,
11423 : : (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
11424 : : errmsg("too many arguments")));
8768 tgl@sss.pgh.pa.us 11425 :CBC 6133 : nargs = 0;
6053 11426 : 6133 : argnames = NIL;
8768 11427 [ + + + + : 12849 : foreach(l, expr->args)
+ + ]
11428 : : {
5912 bruce@momjian.us 11429 : 6716 : Node *arg = (Node *) lfirst(l);
11430 : :
6053 tgl@sss.pgh.pa.us 11431 [ + + ]: 6716 : if (IsA(arg, NamedArgExpr))
11432 : 28 : argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
11433 : 6716 : argtypes[nargs] = exprType(arg);
8768 11434 : 6716 : nargs++;
11435 : : }
11436 : :
11437 : 6133 : appendStringInfo(buf, "%s(",
11438 : : generate_function_name(funcoid, nargs,
11439 : : argnames, argtypes,
4852 11440 : 6133 : expr->funcvariadic,
11441 : : &use_variadic,
614 11442 : 6133 : context->inGroupBy));
6502 11443 : 6133 : nargs = 0;
11444 [ + + + + : 12849 : foreach(l, expr->args)
+ + ]
11445 : : {
11446 [ + + ]: 6716 : if (nargs++ > 0)
11447 : 1329 : appendStringInfoString(buf, ", ");
2486 11448 [ + + + - ]: 6716 : if (use_variadic && lnext(expr->args, l) == NULL)
6502 11449 : 7 : appendStringInfoString(buf, "VARIADIC ");
11450 : 6716 : get_rule_expr((Node *) lfirst(l), context, true);
11451 : : }
9571 11452 : 6133 : appendStringInfoChar(buf, ')');
11453 : : }
11454 : :
11455 : : /*
11456 : : * get_agg_expr - Parse back an Aggref node
11457 : : */
11458 : : static void
1342 andrew@dunslane.net 11459 : 3090 : get_agg_expr(Aggref *aggref, deparse_context *context,
11460 : : Aggref *original_aggref)
11461 : : {
1133 alvherre@alvh.no-ip. 11462 : 3090 : get_agg_expr_helper(aggref, context, original_aggref, NULL, NULL,
11463 : : false);
11464 : 3090 : }
11465 : :
11466 : : /*
11467 : : * get_agg_expr_helper - subroutine for get_agg_expr and
11468 : : * get_json_agg_constructor
11469 : : */
11470 : : static void
11471 : 3134 : get_agg_expr_helper(Aggref *aggref, deparse_context *context,
11472 : : Aggref *original_aggref, const char *funcname,
11473 : : const char *options, bool is_json_objectagg)
11474 : : {
8790 tgl@sss.pgh.pa.us 11475 : 3134 : StringInfo buf = context->buf;
11476 : : Oid argtypes[FUNC_MAX_ARGS];
11477 : : int nargs;
1133 alvherre@alvh.no-ip. 11478 : 3134 : bool use_variadic = false;
11479 : :
11480 : : /*
11481 : : * For a combining aggregate, we look up and deparse the corresponding
11482 : : * partial aggregate instead. This is necessary because our input
11483 : : * argument list has been replaced; the new argument list always has just
11484 : : * one element, which will point to a partial Aggref that supplies us with
11485 : : * transition states to combine.
11486 : : */
3600 tgl@sss.pgh.pa.us 11487 [ + + ]: 3134 : if (DO_AGGSPLIT_COMBINE(aggref->aggsplit))
11488 : : {
11489 : : TargetEntry *tle;
11490 : :
3660 rhaas@postgresql.org 11491 [ - + ]: 520 : Assert(list_length(aggref->args) == 1);
2337 tgl@sss.pgh.pa.us 11492 : 520 : tle = linitial_node(TargetEntry, aggref->args);
11493 : 520 : resolve_special_varno((Node *) tle->expr, context,
11494 : : get_agg_combine_expr, original_aggref);
3660 rhaas@postgresql.org 11495 : 520 : return;
11496 : : }
11497 : :
11498 : : /*
11499 : : * Mark as PARTIAL, if appropriate. We look to the original aggref so as
11500 : : * to avoid printing this when recursing from the code just above.
11501 : : */
3600 tgl@sss.pgh.pa.us 11502 [ + + ]: 2614 : if (DO_AGGSPLIT_SKIPFINAL(original_aggref->aggsplit))
3660 rhaas@postgresql.org 11503 : 1148 : appendStringInfoString(buf, "PARTIAL ");
11504 : :
11505 : : /* Extract the argument types as seen by the parser */
4516 tgl@sss.pgh.pa.us 11506 : 2614 : nargs = get_aggregate_argtypes(aggref, argtypes);
11507 : :
1133 alvherre@alvh.no-ip. 11508 [ + + ]: 2614 : if (!funcname)
11509 : 2570 : funcname = generate_function_name(aggref->aggfnoid, nargs, NIL,
11510 : 2570 : argtypes, aggref->aggvariadic,
11511 : : &use_variadic,
614 tgl@sss.pgh.pa.us 11512 : 2570 : context->inGroupBy);
11513 : :
11514 : : /* Print the aggregate name, schema-qualified if needed */
1133 alvherre@alvh.no-ip. 11515 : 2614 : appendStringInfo(buf, "%s(%s", funcname,
5985 tgl@sss.pgh.pa.us 11516 [ + + ]: 2614 : (aggref->aggdistinct != NIL) ? "DISTINCT " : "");
11517 : :
4516 11518 [ + + ]: 2614 : if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
11519 : : {
11520 : : /*
11521 : : * Ordered-set aggregates do not use "*" syntax. Also, we needn't
11522 : : * worry about inserting VARIADIC. So we can just dump the direct
11523 : : * args as-is.
11524 : : */
11525 [ - + ]: 17 : Assert(!aggref->aggvariadic);
11526 : 17 : get_rule_expr((Node *) aggref->aggdirectargs, context, true);
11527 [ - + ]: 17 : Assert(aggref->aggorder != NIL);
11528 : 17 : appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
11529 : 17 : get_rule_orderby(aggref->aggorder, aggref->args, false, context);
11530 : : }
11531 : : else
11532 : : {
11533 : : /* aggstar can be set only in zero-argument aggregates */
11534 [ + + ]: 2597 : if (aggref->aggstar)
11535 : 775 : appendStringInfoChar(buf, '*');
11536 : : else
11537 : : {
11538 : : ListCell *l;
11539 : : int i;
11540 : :
11541 : 1822 : i = 0;
11542 [ + - + + : 3767 : foreach(l, aggref->args)
+ + ]
11543 : : {
11544 : 1945 : TargetEntry *tle = (TargetEntry *) lfirst(l);
11545 : 1945 : Node *arg = (Node *) tle->expr;
11546 : :
11547 [ - + ]: 1945 : Assert(!IsA(arg, NamedArgExpr));
11548 [ + + ]: 1945 : if (tle->resjunk)
11549 : 31 : continue;
11550 [ + + ]: 1914 : if (i++ > 0)
11551 : : {
1133 alvherre@alvh.no-ip. 11552 [ + + ]: 92 : if (is_json_objectagg)
11553 : : {
11554 : : /*
11555 : : * the ABSENT ON NULL and WITH UNIQUE args are printed
11556 : : * separately, so ignore them here
11557 : : */
11558 [ - + ]: 20 : if (i > 2)
1133 alvherre@alvh.no-ip. 11559 :UBC 0 : break;
11560 : :
1133 alvherre@alvh.no-ip. 11561 :CBC 20 : appendStringInfoString(buf, " : ");
11562 : : }
11563 : : else
11564 : 72 : appendStringInfoString(buf, ", ");
11565 : : }
4516 tgl@sss.pgh.pa.us 11566 [ + + + - ]: 1914 : if (use_variadic && i == nargs)
11567 : 4 : appendStringInfoString(buf, "VARIADIC ");
11568 : 1914 : get_rule_expr(arg, context, true);
11569 : : }
11570 : : }
11571 : :
11572 [ + + ]: 2597 : if (aggref->aggorder != NIL)
11573 : : {
11574 : 54 : appendStringInfoString(buf, " ORDER BY ");
11575 : 54 : get_rule_orderby(aggref->aggorder, aggref->args, false, context);
11576 : : }
11577 : : }
11578 : :
1133 alvherre@alvh.no-ip. 11579 [ + + ]: 2614 : if (options)
11580 : 44 : appendStringInfoString(buf, options);
11581 : :
4676 noah@leadboat.com 11582 [ + + ]: 2614 : if (aggref->aggfilter != NULL)
11583 : : {
11584 : 28 : appendStringInfoString(buf, ") FILTER (WHERE ");
11585 : 28 : get_rule_expr((Node *) aggref->aggfilter, context, false);
11586 : : }
11587 : :
8790 tgl@sss.pgh.pa.us 11588 : 2614 : appendStringInfoChar(buf, ')');
11589 : : }
11590 : :
11591 : : /*
11592 : : * This is a helper function for get_agg_expr(). It's used when we deparse
11593 : : * a combining Aggref; resolve_special_varno locates the corresponding partial
11594 : : * Aggref and then calls this.
11595 : : */
11596 : : static void
2337 11597 : 520 : get_agg_combine_expr(Node *node, deparse_context *context, void *callback_arg)
11598 : : {
11599 : : Aggref *aggref;
11600 : 520 : Aggref *original_aggref = callback_arg;
11601 : :
3660 rhaas@postgresql.org 11602 [ - + ]: 520 : if (!IsA(node, Aggref))
3660 rhaas@postgresql.org 11603 [ # # ]:UBC 0 : elog(ERROR, "combining Aggref does not point to an Aggref");
11604 : :
3660 rhaas@postgresql.org 11605 :CBC 520 : aggref = (Aggref *) node;
11606 : 520 : get_agg_expr(aggref, context, original_aggref);
11607 : 520 : }
11608 : :
11609 : : /*
11610 : : * get_windowfunc_expr - Parse back a WindowFunc node
11611 : : */
11612 : : static void
1342 andrew@dunslane.net 11613 : 214 : get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
11614 : : {
1133 alvherre@alvh.no-ip. 11615 : 214 : get_windowfunc_expr_helper(wfunc, context, NULL, NULL, false);
11616 : 214 : }
11617 : :
11618 : :
11619 : : /*
11620 : : * get_windowfunc_expr_helper - subroutine for get_windowfunc_expr and
11621 : : * get_json_agg_constructor
11622 : : */
11623 : : static void
11624 : 222 : get_windowfunc_expr_helper(WindowFunc *wfunc, deparse_context *context,
11625 : : const char *funcname, const char *options,
11626 : : bool is_json_objectagg)
11627 : : {
6337 tgl@sss.pgh.pa.us 11628 : 222 : StringInfo buf = context->buf;
11629 : : Oid argtypes[FUNC_MAX_ARGS];
11630 : : int nargs;
11631 : : List *argnames;
11632 : : ListCell *l;
11633 : :
11634 [ - + ]: 222 : if (list_length(wfunc->args) > FUNC_MAX_ARGS)
6337 tgl@sss.pgh.pa.us 11635 [ # # ]:UBC 0 : ereport(ERROR,
11636 : : (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
11637 : : errmsg("too many arguments")));
6337 tgl@sss.pgh.pa.us 11638 :CBC 222 : nargs = 0;
4563 11639 : 222 : argnames = NIL;
6337 11640 [ + + + + : 376 : foreach(l, wfunc->args)
+ + ]
11641 : : {
5912 bruce@momjian.us 11642 : 154 : Node *arg = (Node *) lfirst(l);
11643 : :
4563 tgl@sss.pgh.pa.us 11644 [ - + ]: 154 : if (IsA(arg, NamedArgExpr))
4563 tgl@sss.pgh.pa.us 11645 :UBC 0 : argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
6053 tgl@sss.pgh.pa.us 11646 :CBC 154 : argtypes[nargs] = exprType(arg);
6337 11647 : 154 : nargs++;
11648 : : }
11649 : :
1133 alvherre@alvh.no-ip. 11650 [ + + ]: 222 : if (!funcname)
11651 : 214 : funcname = generate_function_name(wfunc->winfnoid, nargs, argnames,
11652 : : argtypes, false, NULL,
614 tgl@sss.pgh.pa.us 11653 : 214 : context->inGroupBy);
11654 : :
1133 alvherre@alvh.no-ip. 11655 : 222 : appendStringInfo(buf, "%s(", funcname);
11656 : :
11657 : : /* winstar can be set only in zero-argument aggregates */
6337 tgl@sss.pgh.pa.us 11658 [ + + ]: 222 : if (wfunc->winstar)
11659 : 16 : appendStringInfoChar(buf, '*');
11660 : : else
11661 : : {
1133 alvherre@alvh.no-ip. 11662 [ + + ]: 206 : if (is_json_objectagg)
11663 : : {
11664 : 4 : get_rule_expr((Node *) linitial(wfunc->args), context, false);
11665 : 4 : appendStringInfoString(buf, " : ");
11666 : 4 : get_rule_expr((Node *) lsecond(wfunc->args), context, false);
11667 : : }
11668 : : else
11669 : 202 : get_rule_expr((Node *) wfunc->args, context, true);
11670 : : }
11671 : :
11672 [ + + ]: 222 : if (options)
11673 : 8 : appendStringInfoString(buf, options);
11674 : :
4676 noah@leadboat.com 11675 [ - + ]: 222 : if (wfunc->aggfilter != NULL)
11676 : : {
4676 noah@leadboat.com 11677 :UBC 0 : appendStringInfoString(buf, ") FILTER (WHERE ");
11678 : 0 : get_rule_expr((Node *) wfunc->aggfilter, context, false);
11679 : : }
11680 : :
214 ishii@postgresql.org 11681 :GNC 222 : appendStringInfoString(buf, ") ");
11682 : :
11683 [ + + ]: 222 : if (wfunc->ignore_nulls == PARSER_IGNORE_NULLS)
11684 : 4 : appendStringInfoString(buf, "IGNORE NULLS ");
11685 : :
11686 : 222 : appendStringInfoString(buf, "OVER ");
11687 : :
420 tgl@sss.pgh.pa.us 11688 [ + + ]:CBC 222 : if (context->windowClause)
11689 : : {
11690 : : /* Query-decompilation case: search the windowClause list */
11691 [ + - + - : 40 : foreach(l, context->windowClause)
+ - ]
11692 : : {
11693 : 40 : WindowClause *wc = (WindowClause *) lfirst(l);
11694 : :
11695 [ + - ]: 40 : if (wc->winref == wfunc->winref)
11696 : : {
11697 [ + + ]: 40 : if (wc->name)
420 tgl@sss.pgh.pa.us 11698 :GBC 12 : appendStringInfoString(buf, quote_identifier(wc->name));
11699 : : else
420 tgl@sss.pgh.pa.us 11700 :CBC 28 : get_rule_windowspec(wc, context->targetList, context);
11701 : 40 : break;
11702 : : }
11703 : : }
11704 [ - + ]: 40 : if (l == NULL)
6337 tgl@sss.pgh.pa.us 11705 [ # # ]:UBC 0 : elog(ERROR, "could not find window clause for winref %u",
11706 : : wfunc->winref);
11707 : : }
11708 : : else
11709 : : {
11710 : : /*
11711 : : * In EXPLAIN, search the namespace stack for a matching WindowAgg
11712 : : * node (probably it's always the first entry), and print winname.
11713 : : */
420 tgl@sss.pgh.pa.us 11714 [ + - + - :CBC 182 : foreach(l, context->namespaces)
+ - ]
11715 : : {
11716 : 182 : deparse_namespace *dpns = (deparse_namespace *) lfirst(l);
11717 : :
11718 [ + - + - ]: 182 : if (dpns->plan && IsA(dpns->plan, WindowAgg))
11719 : : {
11720 : 182 : WindowAgg *wagg = (WindowAgg *) dpns->plan;
11721 : :
11722 [ + - ]: 182 : if (wagg->winref == wfunc->winref)
11723 : : {
11724 : 182 : appendStringInfoString(buf, quote_identifier(wagg->winname));
11725 : 182 : break;
11726 : : }
11727 : : }
11728 : : }
11729 [ - + ]: 182 : if (l == NULL)
420 tgl@sss.pgh.pa.us 11730 [ # # ]:UBC 0 : elog(ERROR, "could not find window clause for winref %u",
11731 : : wfunc->winref);
11732 : : }
6337 tgl@sss.pgh.pa.us 11733 :CBC 222 : }
11734 : :
11735 : : /*
11736 : : * get_func_sql_syntax - Parse back a SQL-syntax function call
11737 : : *
11738 : : * Returns true if we successfully deparsed, false if we did not
11739 : : * recognize the function.
11740 : : */
11741 : : static bool
2008 11742 : 120 : get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
11743 : : {
11744 : 120 : StringInfo buf = context->buf;
11745 : 120 : Oid funcoid = expr->funcid;
11746 : :
11747 [ + + + + : 120 : switch (funcoid)
+ + + + +
+ + + + +
+ - + ]
11748 : : {
11749 : 16 : case F_TIMEZONE_INTERVAL_TIMESTAMP:
11750 : : case F_TIMEZONE_INTERVAL_TIMESTAMPTZ:
11751 : : case F_TIMEZONE_INTERVAL_TIMETZ:
11752 : : case F_TIMEZONE_TEXT_TIMESTAMP:
11753 : : case F_TIMEZONE_TEXT_TIMESTAMPTZ:
11754 : : case F_TIMEZONE_TEXT_TIMETZ:
11755 : : /* AT TIME ZONE ... note reversed argument order */
11756 : 16 : appendStringInfoChar(buf, '(');
1251 11757 : 16 : get_rule_expr_paren((Node *) lsecond(expr->args), context, false,
11758 : : (Node *) expr);
2008 11759 : 16 : appendStringInfoString(buf, " AT TIME ZONE ");
1251 11760 : 16 : get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11761 : : (Node *) expr);
2008 11762 : 16 : appendStringInfoChar(buf, ')');
11763 : 16 : return true;
11764 : :
935 michael@paquier.xyz 11765 : 12 : case F_TIMEZONE_TIMESTAMP:
11766 : : case F_TIMEZONE_TIMESTAMPTZ:
11767 : : case F_TIMEZONE_TIMETZ:
11768 : : /* AT LOCAL */
11769 : 12 : appendStringInfoChar(buf, '(');
11770 : 12 : get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11771 : : (Node *) expr);
11772 : 12 : appendStringInfoString(buf, " AT LOCAL)");
11773 : 12 : return true;
11774 : :
2008 tgl@sss.pgh.pa.us 11775 : 4 : case F_OVERLAPS_TIMESTAMPTZ_INTERVAL_TIMESTAMPTZ_INTERVAL:
11776 : : case F_OVERLAPS_TIMESTAMPTZ_INTERVAL_TIMESTAMPTZ_TIMESTAMPTZ:
11777 : : case F_OVERLAPS_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ_INTERVAL:
11778 : : case F_OVERLAPS_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ:
11779 : : case F_OVERLAPS_TIMESTAMP_INTERVAL_TIMESTAMP_INTERVAL:
11780 : : case F_OVERLAPS_TIMESTAMP_INTERVAL_TIMESTAMP_TIMESTAMP:
11781 : : case F_OVERLAPS_TIMESTAMP_TIMESTAMP_TIMESTAMP_INTERVAL:
11782 : : case F_OVERLAPS_TIMESTAMP_TIMESTAMP_TIMESTAMP_TIMESTAMP:
11783 : : case F_OVERLAPS_TIMETZ_TIMETZ_TIMETZ_TIMETZ:
11784 : : case F_OVERLAPS_TIME_INTERVAL_TIME_INTERVAL:
11785 : : case F_OVERLAPS_TIME_INTERVAL_TIME_TIME:
11786 : : case F_OVERLAPS_TIME_TIME_TIME_INTERVAL:
11787 : : case F_OVERLAPS_TIME_TIME_TIME_TIME:
11788 : : /* (x1, x2) OVERLAPS (y1, y2) */
11789 : 4 : appendStringInfoString(buf, "((");
11790 : 4 : get_rule_expr((Node *) linitial(expr->args), context, false);
11791 : 4 : appendStringInfoString(buf, ", ");
11792 : 4 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11793 : 4 : appendStringInfoString(buf, ") OVERLAPS (");
11794 : 4 : get_rule_expr((Node *) lthird(expr->args), context, false);
11795 : 4 : appendStringInfoString(buf, ", ");
11796 : 4 : get_rule_expr((Node *) lfourth(expr->args), context, false);
11797 : 4 : appendStringInfoString(buf, "))");
11798 : 4 : return true;
11799 : :
1855 peter@eisentraut.org 11800 : 12 : case F_EXTRACT_TEXT_DATE:
11801 : : case F_EXTRACT_TEXT_TIME:
11802 : : case F_EXTRACT_TEXT_TIMETZ:
11803 : : case F_EXTRACT_TEXT_TIMESTAMP:
11804 : : case F_EXTRACT_TEXT_TIMESTAMPTZ:
11805 : : case F_EXTRACT_TEXT_INTERVAL:
11806 : : /* EXTRACT (x FROM y) */
11807 : 12 : appendStringInfoString(buf, "EXTRACT(");
11808 : : {
11809 : 12 : Const *con = (Const *) linitial(expr->args);
11810 : :
11811 [ + - + - : 12 : Assert(IsA(con, Const) &&
- + ]
11812 : : con->consttype == TEXTOID &&
11813 : : !con->constisnull);
11814 : 12 : appendStringInfoString(buf, TextDatumGetCString(con->constvalue));
11815 : : }
11816 : 12 : appendStringInfoString(buf, " FROM ");
11817 : 12 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11818 : 12 : appendStringInfoChar(buf, ')');
11819 : 12 : return true;
11820 : :
2008 tgl@sss.pgh.pa.us 11821 : 8 : case F_IS_NORMALIZED:
11822 : : /* IS xxx NORMALIZED */
945 drowley@postgresql.o 11823 : 8 : appendStringInfoChar(buf, '(');
1251 tgl@sss.pgh.pa.us 11824 : 8 : get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11825 : : (Node *) expr);
11826 : 8 : appendStringInfoString(buf, " IS");
2008 11827 [ + + ]: 8 : if (list_length(expr->args) == 2)
11828 : : {
11829 : 4 : Const *con = (Const *) lsecond(expr->args);
11830 : :
11831 [ + - + - : 4 : Assert(IsA(con, Const) &&
- + ]
11832 : : con->consttype == TEXTOID &&
11833 : : !con->constisnull);
11834 : 4 : appendStringInfo(buf, " %s",
11835 : 4 : TextDatumGetCString(con->constvalue));
11836 : : }
11837 : 8 : appendStringInfoString(buf, " NORMALIZED)");
11838 : 8 : return true;
11839 : :
11840 : 4 : case F_PG_COLLATION_FOR:
11841 : : /* COLLATION FOR */
11842 : 4 : appendStringInfoString(buf, "COLLATION FOR (");
11843 : 4 : get_rule_expr((Node *) linitial(expr->args), context, false);
11844 : 4 : appendStringInfoChar(buf, ')');
11845 : 4 : return true;
11846 : :
11847 : 8 : case F_NORMALIZE:
11848 : : /* NORMALIZE() */
11849 : 8 : appendStringInfoString(buf, "NORMALIZE(");
11850 : 8 : get_rule_expr((Node *) linitial(expr->args), context, false);
11851 [ + + ]: 8 : if (list_length(expr->args) == 2)
11852 : : {
11853 : 4 : Const *con = (Const *) lsecond(expr->args);
11854 : :
11855 [ + - + - : 4 : Assert(IsA(con, Const) &&
- + ]
11856 : : con->consttype == TEXTOID &&
11857 : : !con->constisnull);
11858 : 4 : appendStringInfo(buf, ", %s",
11859 : 4 : TextDatumGetCString(con->constvalue));
11860 : : }
11861 : 8 : appendStringInfoChar(buf, ')');
11862 : 8 : return true;
11863 : :
11864 : 8 : case F_OVERLAY_BIT_BIT_INT4:
11865 : : case F_OVERLAY_BIT_BIT_INT4_INT4:
11866 : : case F_OVERLAY_BYTEA_BYTEA_INT4:
11867 : : case F_OVERLAY_BYTEA_BYTEA_INT4_INT4:
11868 : : case F_OVERLAY_TEXT_TEXT_INT4:
11869 : : case F_OVERLAY_TEXT_TEXT_INT4_INT4:
11870 : : /* OVERLAY() */
11871 : 8 : appendStringInfoString(buf, "OVERLAY(");
11872 : 8 : get_rule_expr((Node *) linitial(expr->args), context, false);
11873 : 8 : appendStringInfoString(buf, " PLACING ");
11874 : 8 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11875 : 8 : appendStringInfoString(buf, " FROM ");
11876 : 8 : get_rule_expr((Node *) lthird(expr->args), context, false);
11877 [ + + ]: 8 : if (list_length(expr->args) == 4)
11878 : : {
11879 : 4 : appendStringInfoString(buf, " FOR ");
11880 : 4 : get_rule_expr((Node *) lfourth(expr->args), context, false);
11881 : : }
11882 : 8 : appendStringInfoChar(buf, ')');
11883 : 8 : return true;
11884 : :
11885 : 4 : case F_POSITION_BIT_BIT:
11886 : : case F_POSITION_BYTEA_BYTEA:
11887 : : case F_POSITION_TEXT_TEXT:
11888 : : /* POSITION() ... extra parens since args are b_expr not a_expr */
11889 : 4 : appendStringInfoString(buf, "POSITION((");
11890 : 4 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11891 : 4 : appendStringInfoString(buf, ") IN (");
11892 : 4 : get_rule_expr((Node *) linitial(expr->args), context, false);
11893 : 4 : appendStringInfoString(buf, "))");
11894 : 4 : return true;
11895 : :
11896 : 4 : case F_SUBSTRING_BIT_INT4:
11897 : : case F_SUBSTRING_BIT_INT4_INT4:
11898 : : case F_SUBSTRING_BYTEA_INT4:
11899 : : case F_SUBSTRING_BYTEA_INT4_INT4:
11900 : : case F_SUBSTRING_TEXT_INT4:
11901 : : case F_SUBSTRING_TEXT_INT4_INT4:
11902 : : /* SUBSTRING FROM/FOR (i.e., integer-position variants) */
11903 : 4 : appendStringInfoString(buf, "SUBSTRING(");
11904 : 4 : get_rule_expr((Node *) linitial(expr->args), context, false);
11905 : 4 : appendStringInfoString(buf, " FROM ");
11906 : 4 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11907 [ + - ]: 4 : if (list_length(expr->args) == 3)
11908 : : {
11909 : 4 : appendStringInfoString(buf, " FOR ");
11910 : 4 : get_rule_expr((Node *) lthird(expr->args), context, false);
11911 : : }
11912 : 4 : appendStringInfoChar(buf, ')');
11913 : 4 : return true;
11914 : :
11915 : 4 : case F_SUBSTRING_TEXT_TEXT_TEXT:
11916 : : /* SUBSTRING SIMILAR/ESCAPE */
11917 : 4 : appendStringInfoString(buf, "SUBSTRING(");
11918 : 4 : get_rule_expr((Node *) linitial(expr->args), context, false);
11919 : 4 : appendStringInfoString(buf, " SIMILAR ");
11920 : 4 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11921 : 4 : appendStringInfoString(buf, " ESCAPE ");
11922 : 4 : get_rule_expr((Node *) lthird(expr->args), context, false);
11923 : 4 : appendStringInfoChar(buf, ')');
11924 : 4 : return true;
11925 : :
11926 : 8 : case F_BTRIM_BYTEA_BYTEA:
11927 : : case F_BTRIM_TEXT:
11928 : : case F_BTRIM_TEXT_TEXT:
11929 : : /* TRIM() */
11930 : 8 : appendStringInfoString(buf, "TRIM(BOTH");
11931 [ + - ]: 8 : if (list_length(expr->args) == 2)
11932 : : {
11933 : 8 : appendStringInfoChar(buf, ' ');
11934 : 8 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11935 : : }
11936 : 8 : appendStringInfoString(buf, " FROM ");
11937 : 8 : get_rule_expr((Node *) linitial(expr->args), context, false);
11938 : 8 : appendStringInfoChar(buf, ')');
11939 : 8 : return true;
11940 : :
1933 11941 : 8 : case F_LTRIM_BYTEA_BYTEA:
11942 : : case F_LTRIM_TEXT:
11943 : : case F_LTRIM_TEXT_TEXT:
11944 : : /* TRIM() */
2008 11945 : 8 : appendStringInfoString(buf, "TRIM(LEADING");
11946 [ + - ]: 8 : if (list_length(expr->args) == 2)
11947 : : {
11948 : 8 : appendStringInfoChar(buf, ' ');
11949 : 8 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11950 : : }
11951 : 8 : appendStringInfoString(buf, " FROM ");
11952 : 8 : get_rule_expr((Node *) linitial(expr->args), context, false);
11953 : 8 : appendStringInfoChar(buf, ')');
11954 : 8 : return true;
11955 : :
1933 11956 : 8 : case F_RTRIM_BYTEA_BYTEA:
11957 : : case F_RTRIM_TEXT:
11958 : : case F_RTRIM_TEXT_TEXT:
11959 : : /* TRIM() */
2008 11960 : 8 : appendStringInfoString(buf, "TRIM(TRAILING");
11961 [ + + ]: 8 : if (list_length(expr->args) == 2)
11962 : : {
11963 : 4 : appendStringInfoChar(buf, ' ');
11964 : 4 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11965 : : }
11966 : 8 : appendStringInfoString(buf, " FROM ");
11967 : 8 : get_rule_expr((Node *) linitial(expr->args), context, false);
11968 : 8 : appendStringInfoChar(buf, ')');
11969 : 8 : return true;
11970 : :
1314 michael@paquier.xyz 11971 : 8 : case F_SYSTEM_USER:
11972 : 8 : appendStringInfoString(buf, "SYSTEM_USER");
11973 : 8 : return true;
11974 : :
2008 tgl@sss.pgh.pa.us 11975 :UBC 0 : case F_XMLEXISTS:
11976 : : /* XMLEXISTS ... extra parens because args are c_expr */
11977 : 0 : appendStringInfoString(buf, "XMLEXISTS((");
11978 : 0 : get_rule_expr((Node *) linitial(expr->args), context, false);
11979 : 0 : appendStringInfoString(buf, ") PASSING (");
11980 : 0 : get_rule_expr((Node *) lsecond(expr->args), context, false);
11981 : 0 : appendStringInfoString(buf, "))");
11982 : 0 : return true;
11983 : : }
2008 tgl@sss.pgh.pa.us 11984 :CBC 4 : return false;
11985 : : }
11986 : :
11987 : : /* ----------
11988 : : * get_coercion_expr
11989 : : *
11990 : : * Make a string representation of a value coerced to a specific type
11991 : : * ----------
11992 : : */
11993 : : static void
6989 11994 : 3705 : get_coercion_expr(Node *arg, deparse_context *context,
11995 : : Oid resulttype, int32 resulttypmod,
11996 : : Node *parentNode)
11997 : : {
11998 : 3705 : StringInfo buf = context->buf;
11999 : :
12000 : : /*
12001 : : * Since parse_coerce.c doesn't immediately collapse application of
12002 : : * length-coercion functions to constants, what we'll typically see in
12003 : : * such cases is a Const with typmod -1 and a length-coercion function
12004 : : * right above it. Avoid generating redundant output. However, beware of
12005 : : * suppressing casts when the user actually wrote something like
12006 : : * 'foo'::text::char(3).
12007 : : *
12008 : : * Note: it might seem that we are missing the possibility of needing to
12009 : : * print a COLLATE clause for such a Const. However, a Const could only
12010 : : * have nondefault collation in a post-constant-folding tree, in which the
12011 : : * length coercion would have been folded too. See also the special
12012 : : * handling of CollateExpr in coerce_to_target_type(): any collation
12013 : : * marking will be above the coercion node, not below it.
12014 : : */
12015 [ + - + + ]: 3705 : if (arg && IsA(arg, Const) &&
12016 [ + + ]: 430 : ((Const *) arg)->consttype == resulttype &&
12017 [ + - ]: 41 : ((Const *) arg)->consttypmod == -1)
12018 : : {
12019 : : /* Show the constant without normal ::typename decoration */
6694 12020 : 41 : get_const_expr((Const *) arg, context, -1);
12021 : : }
12022 : : else
12023 : : {
6989 12024 [ + + ]: 3664 : if (!PRETTY_PAREN(context))
12025 : 3399 : appendStringInfoChar(buf, '(');
12026 : 3664 : get_rule_expr_paren(arg, context, false, parentNode);
12027 [ + + ]: 3664 : if (!PRETTY_PAREN(context))
12028 : 3399 : appendStringInfoChar(buf, ')');
12029 : : }
12030 : :
12031 : : /*
12032 : : * Never emit resulttype(arg) functional notation. A pg_proc entry could
12033 : : * take precedence, and a resulttype in pg_temp would require schema
12034 : : * qualification that format_type_with_typemod() would usually omit. We've
12035 : : * standardized on arg::resulttype, but CAST(arg AS resulttype) notation
12036 : : * would work fine.
12037 : : */
12038 : 3705 : appendStringInfo(buf, "::%s",
12039 : : format_type_with_typemod(resulttype, resulttypmod));
12040 : 3705 : }
12041 : :
12042 : : /* ----------
12043 : : * get_const_expr
12044 : : *
12045 : : * Make a string representation of a Const
12046 : : *
12047 : : * showtype can be -1 to never show "::typename" decoration, or +1 to always
12048 : : * show it, or 0 to show it only if the constant wouldn't be assumed to be
12049 : : * the right type by default.
12050 : : *
12051 : : * If the Const's collation isn't default for its type, show that too.
12052 : : * We mustn't do this when showtype is -1 (since that means the caller will
12053 : : * print "::typename", and we can't put a COLLATE clause in between). It's
12054 : : * caller's responsibility that collation isn't missed in such cases.
12055 : : * ----------
12056 : : */
12057 : : static void
6694 12058 : 46669 : get_const_expr(Const *constval, deparse_context *context, int showtype)
12059 : : {
9711 12060 : 46669 : StringInfo buf = context->buf;
12061 : : Oid typoutput;
12062 : : bool typIsVarlena;
12063 : : char *extval;
4054 12064 : 46669 : bool needlabel = false;
12065 : :
9629 12066 [ + + ]: 46669 : if (constval->constisnull)
12067 : : {
12068 : : /*
12069 : : * Always label the type of a NULL constant to prevent misdecisions
12070 : : * about type when reparsing.
12071 : : */
4569 rhaas@postgresql.org 12072 : 829 : appendStringInfoString(buf, "NULL");
6694 tgl@sss.pgh.pa.us 12073 [ + + ]: 829 : if (showtype >= 0)
12074 : : {
6989 12075 : 798 : appendStringInfo(buf, "::%s",
12076 : : format_type_with_typemod(constval->consttype,
12077 : : constval->consttypmod));
5534 12078 : 798 : get_const_collation(constval, context);
12079 : : }
9629 12080 : 6148 : return;
12081 : : }
12082 : :
8003 12083 : 45840 : getTypeOutputInfo(constval->consttype,
12084 : : &typoutput, &typIsVarlena);
12085 : :
7336 12086 : 45840 : extval = OidOutputFunctionCall(typoutput, constval->constvalue);
12087 : :
9710 12088 [ + + + + ]: 45840 : switch (constval->consttype)
12089 : : {
12090 : 26265 : case INT4OID:
12091 : :
12092 : : /*
12093 : : * INT4 can be printed without any decoration, unless it is
12094 : : * negative; in that case print it as '-nnn'::integer to ensure
12095 : : * that the output will re-parse as a constant, not as a constant
12096 : : * plus operator. In most cases we could get away with printing
12097 : : * (-nnn) instead, because of the way that gram.y handles negative
12098 : : * literals; but that doesn't work for INT_MIN, and it doesn't
12099 : : * seem that much prettier anyway.
12100 : : */
4054 12101 [ + + ]: 26265 : if (extval[0] != '-')
12102 : 25948 : appendStringInfoString(buf, extval);
12103 : : else
12104 : : {
12105 : 317 : appendStringInfo(buf, "'%s'", extval);
3240 12106 : 317 : needlabel = true; /* we must attach a cast */
12107 : : }
4054 12108 : 26265 : break;
12109 : :
8661 peter_e@gmx.net 12110 : 720 : case NUMERICOID:
12111 : :
12112 : : /*
12113 : : * NUMERIC can be printed without quotes if it looks like a float
12114 : : * constant (not an integer, and not Infinity or NaN) and doesn't
12115 : : * have a leading sign (for the same reason as for INT4).
12116 : : */
4054 tgl@sss.pgh.pa.us 12117 [ + - ]: 720 : if (isdigit((unsigned char) extval[0]) &&
12118 [ + + ]: 720 : strcspn(extval, "eE.") != strlen(extval))
12119 : : {
12120 : 251 : appendStringInfoString(buf, extval);
12121 : : }
12122 : : else
12123 : : {
12124 : 469 : appendStringInfo(buf, "'%s'", extval);
3240 12125 : 469 : needlabel = true; /* we must attach a cast */
12126 : : }
8644 bruce@momjian.us 12127 : 720 : break;
12128 : :
8661 peter_e@gmx.net 12129 : 1049 : case BOOLOID:
8644 bruce@momjian.us 12130 [ + + ]: 1049 : if (strcmp(extval, "t") == 0)
4569 rhaas@postgresql.org 12131 : 407 : appendStringInfoString(buf, "true");
12132 : : else
12133 : 642 : appendStringInfoString(buf, "false");
8661 peter_e@gmx.net 12134 : 1049 : break;
12135 : :
12136 : 17806 : default:
6450 tgl@sss.pgh.pa.us 12137 : 17806 : simple_quote_literal(buf, extval);
9710 12138 : 17806 : break;
12139 : : }
12140 : :
9712 12141 : 45840 : pfree(extval);
12142 : :
6694 12143 [ + + ]: 45840 : if (showtype < 0)
6989 12144 : 5319 : return;
12145 : :
12146 : : /*
12147 : : * For showtype == 0, append ::typename unless the constant will be
12148 : : * implicitly typed as the right type when it is read in.
12149 : : *
12150 : : * XXX this code has to be kept in sync with the behavior of the parser,
12151 : : * especially make_const.
12152 : : */
9710 12153 [ + + + + ]: 40521 : switch (constval->consttype)
12154 : : {
8661 peter_e@gmx.net 12155 : 1107 : case BOOLOID:
12156 : : case UNKNOWNOID:
12157 : : /* These types can be left unlabeled */
8630 tgl@sss.pgh.pa.us 12158 : 1107 : needlabel = false;
12159 : 1107 : break;
4054 12160 : 23506 : case INT4OID:
12161 : : /* We determined above whether a label is needed */
12162 : 23506 : break;
8630 12163 : 720 : case NUMERICOID:
12164 : :
12165 : : /*
12166 : : * Float-looking constants will be typed as numeric, which we
12167 : : * checked above; but if there's a nondefault typmod we need to
12168 : : * show it.
12169 : : */
4054 12170 : 720 : needlabel |= (constval->consttypmod >= 0);
9710 12171 : 720 : break;
12172 : 15188 : default:
8630 12173 : 15188 : needlabel = true;
9710 12174 : 15188 : break;
12175 : : }
6694 12176 [ + + - + ]: 40521 : if (needlabel || showtype > 0)
8630 12177 : 15968 : appendStringInfo(buf, "::%s",
12178 : : format_type_with_typemod(constval->consttype,
12179 : : constval->consttypmod));
12180 : :
5534 12181 : 40521 : get_const_collation(constval, context);
12182 : : }
12183 : :
12184 : : /*
12185 : : * helper for get_const_expr: append COLLATE if needed
12186 : : */
12187 : : static void
12188 : 41319 : get_const_collation(Const *constval, deparse_context *context)
12189 : : {
12190 : 41319 : StringInfo buf = context->buf;
12191 : :
12192 [ + + ]: 41319 : if (OidIsValid(constval->constcollid))
12193 : : {
5504 bruce@momjian.us 12194 : 5978 : Oid typcollation = get_typcollation(constval->consttype);
12195 : :
5534 tgl@sss.pgh.pa.us 12196 [ + + ]: 5978 : if (constval->constcollid != typcollation)
12197 : : {
12198 : 96 : appendStringInfo(buf, " COLLATE %s",
12199 : : generate_collation_name(constval->constcollid));
12200 : : }
12201 : : }
10077 bruce@momjian.us 12202 : 41319 : }
12203 : :
12204 : : /*
12205 : : * get_json_path_spec - Parse back a JSON path specification
12206 : : */
12207 : : static void
775 amitlan@postgresql.o 12208 : 304 : get_json_path_spec(Node *path_spec, deparse_context *context, bool showimplicit)
12209 : : {
12210 [ + - ]: 304 : if (IsA(path_spec, Const))
12211 : 304 : get_const_expr((Const *) path_spec, context, -1);
12212 : : else
775 amitlan@postgresql.o 12213 :UBC 0 : get_rule_expr(path_spec, context, showimplicit);
775 amitlan@postgresql.o 12214 :CBC 304 : }
12215 : :
12216 : : /*
12217 : : * get_json_format - Parse back a JsonFormat node
12218 : : */
12219 : : static void
1133 alvherre@alvh.no-ip. 12220 : 128 : get_json_format(JsonFormat *format, StringInfo buf)
12221 : : {
12222 [ + + ]: 128 : if (format->format_type == JS_FORMAT_DEFAULT)
12223 : 76 : return;
12224 : :
12225 : 52 : appendStringInfoString(buf,
12226 [ - + ]: 52 : format->format_type == JS_FORMAT_JSONB ?
12227 : : " FORMAT JSONB" : " FORMAT JSON");
12228 : :
12229 [ + + ]: 52 : if (format->encoding != JS_ENC_DEFAULT)
12230 : : {
12231 : : const char *encoding;
12232 : :
12233 : 4 : encoding =
12234 [ + - ]: 8 : format->encoding == JS_ENC_UTF16 ? "UTF16" :
12235 [ - + ]: 4 : format->encoding == JS_ENC_UTF32 ? "UTF32" : "UTF8";
12236 : :
12237 : 4 : appendStringInfo(buf, " ENCODING %s", encoding);
12238 : : }
12239 : : }
12240 : :
12241 : : /*
12242 : : * get_json_returning - Parse back a JsonReturning structure
12243 : : */
12244 : : static void
12245 : 144 : get_json_returning(JsonReturning *returning, StringInfo buf,
12246 : : bool json_format_by_default)
12247 : : {
12248 [ - + ]: 144 : if (!OidIsValid(returning->typid))
1133 alvherre@alvh.no-ip. 12249 :UBC 0 : return;
12250 : :
1133 alvherre@alvh.no-ip. 12251 :CBC 144 : appendStringInfo(buf, " RETURNING %s",
12252 : : format_type_with_typemod(returning->typid,
12253 : : returning->typmod));
12254 : :
12255 [ + + + + ]: 280 : if (!json_format_by_default ||
12256 : 136 : returning->format->format_type !=
12257 [ + + ]: 136 : (returning->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON))
12258 : 24 : get_json_format(returning->format, buf);
12259 : : }
12260 : :
12261 : : /*
12262 : : * get_json_constructor - Parse back a JsonConstructorExpr node
12263 : : */
12264 : : static void
12265 : 148 : get_json_constructor(JsonConstructorExpr *ctor, deparse_context *context,
12266 : : bool showimplicit)
12267 : : {
12268 : 148 : StringInfo buf = context->buf;
12269 : : const char *funcname;
12270 : : bool is_json_object;
12271 : : int curridx;
12272 : : ListCell *lc;
12273 : :
12274 [ + + ]: 148 : if (ctor->type == JSCTOR_JSON_OBJECTAGG)
12275 : : {
12276 : 24 : get_json_agg_constructor(ctor, context, "JSON_OBJECTAGG", true);
12277 : 24 : return;
12278 : : }
12279 [ + + ]: 124 : else if (ctor->type == JSCTOR_JSON_ARRAYAGG)
12280 : : {
12281 : 28 : get_json_agg_constructor(ctor, context, "JSON_ARRAYAGG", false);
12282 : 28 : return;
12283 : : }
4 rguo@postgresql.org 12284 [ + + ]:GNC 96 : else if (ctor->type == JSCTOR_JSON_ARRAY_QUERY)
12285 : : {
12286 : 16 : Query *query = castNode(Query, ctor->orig_query);
12287 : :
12288 : 16 : appendStringInfo(buf, "JSON_ARRAY(");
12289 : :
12290 : 16 : get_query_def(query, buf, context->namespaces, NULL, false,
12291 : : context->prettyFlags, context->wrapColumn,
12292 : : context->indentLevel);
12293 : :
12294 : 16 : get_json_constructor_options(ctor, buf);
12295 : 16 : appendStringInfoChar(buf, ')');
12296 : :
12297 : 16 : return;
12298 : : }
12299 : :
1133 alvherre@alvh.no-ip. 12300 [ + + + + :CBC 80 : switch (ctor->type)
+ - ]
12301 : : {
12302 : 20 : case JSCTOR_JSON_OBJECT:
12303 : 20 : funcname = "JSON_OBJECT";
12304 : 20 : break;
12305 : 16 : case JSCTOR_JSON_ARRAY:
12306 : 16 : funcname = "JSON_ARRAY";
12307 : 16 : break;
1020 amitlan@postgresql.o 12308 : 28 : case JSCTOR_JSON_PARSE:
12309 : 28 : funcname = "JSON";
12310 : 28 : break;
12311 : 8 : case JSCTOR_JSON_SCALAR:
12312 : 8 : funcname = "JSON_SCALAR";
12313 : 8 : break;
12314 : 8 : case JSCTOR_JSON_SERIALIZE:
12315 : 8 : funcname = "JSON_SERIALIZE";
12316 : 8 : break;
1133 alvherre@alvh.no-ip. 12317 :UBC 0 : default:
1132 12318 [ # # ]: 0 : elog(ERROR, "invalid JsonConstructorType %d", ctor->type);
12319 : : }
12320 : :
1133 alvherre@alvh.no-ip. 12321 :CBC 80 : appendStringInfo(buf, "%s(", funcname);
12322 : :
12323 : 80 : is_json_object = ctor->type == JSCTOR_JSON_OBJECT;
12324 [ + - + + : 212 : foreach(lc, ctor->args)
+ + ]
12325 : : {
12326 : 132 : curridx = foreach_current_index(lc);
12327 [ + + ]: 132 : if (curridx > 0)
12328 : : {
12329 : : const char *sep;
12330 : :
12331 [ + + + + ]: 52 : sep = (is_json_object && (curridx % 2) != 0) ? " : " : ", ";
12332 : 52 : appendStringInfoString(buf, sep);
12333 : : }
12334 : :
12335 : 132 : get_rule_expr((Node *) lfirst(lc), context, true);
12336 : : }
12337 : :
12338 : 80 : get_json_constructor_options(ctor, buf);
945 drowley@postgresql.o 12339 : 80 : appendStringInfoChar(buf, ')');
12340 : : }
12341 : :
12342 : : /*
12343 : : * Append options, if any, to the JSON constructor being deparsed
12344 : : */
12345 : : static void
1133 alvherre@alvh.no-ip. 12346 : 148 : get_json_constructor_options(JsonConstructorExpr *ctor, StringInfo buf)
12347 : : {
12348 [ + + ]: 148 : if (ctor->absent_on_null)
12349 : : {
12350 [ + - ]: 48 : if (ctor->type == JSCTOR_JSON_OBJECT ||
12351 [ - + ]: 48 : ctor->type == JSCTOR_JSON_OBJECTAGG)
1133 alvherre@alvh.no-ip. 12352 :UBC 0 : appendStringInfoString(buf, " ABSENT ON NULL");
12353 : : }
12354 : : else
12355 : : {
1133 alvherre@alvh.no-ip. 12356 [ + - ]:CBC 100 : if (ctor->type == JSCTOR_JSON_ARRAY ||
12357 [ + + ]: 100 : ctor->type == JSCTOR_JSON_ARRAYAGG)
12358 : 12 : appendStringInfoString(buf, " NULL ON NULL");
12359 : : }
12360 : :
12361 [ + + ]: 148 : if (ctor->unique)
12362 : 16 : appendStringInfoString(buf, " WITH UNIQUE KEYS");
12363 : :
12364 : : /*
12365 : : * Append RETURNING clause if needed; JSON() and JSON_SCALAR() don't
12366 : : * support one.
12367 : : */
1020 amitlan@postgresql.o 12368 [ + + + + ]: 148 : if (ctor->type != JSCTOR_JSON_PARSE && ctor->type != JSCTOR_JSON_SCALAR)
12369 : 112 : get_json_returning(ctor->returning, buf, true);
1133 alvherre@alvh.no-ip. 12370 : 148 : }
12371 : :
12372 : : /*
12373 : : * get_json_agg_constructor - Parse back an aggregate JsonConstructorExpr node
12374 : : */
12375 : : static void
12376 : 52 : get_json_agg_constructor(JsonConstructorExpr *ctor, deparse_context *context,
12377 : : const char *funcname, bool is_json_objectagg)
12378 : : {
12379 : : StringInfoData options;
12380 : :
12381 : 52 : initStringInfo(&options);
12382 : 52 : get_json_constructor_options(ctor, &options);
12383 : :
12384 [ + + ]: 52 : if (IsA(ctor->func, Aggref))
12385 : 44 : get_agg_expr_helper((Aggref *) ctor->func, context,
12386 : 44 : (Aggref *) ctor->func,
12387 : 44 : funcname, options.data, is_json_objectagg);
12388 [ + - ]: 8 : else if (IsA(ctor->func, WindowFunc))
12389 : 8 : get_windowfunc_expr_helper((WindowFunc *) ctor->func, context,
12390 : 8 : funcname, options.data,
12391 : : is_json_objectagg);
12392 : : else
1133 alvherre@alvh.no-ip. 12393 [ # # ]:UBC 0 : elog(ERROR, "invalid JsonConstructorExpr underlying node type: %d",
12394 : : nodeTag(ctor->func));
1133 alvherre@alvh.no-ip. 12395 :CBC 52 : }
12396 : :
12397 : : /*
12398 : : * simple_quote_literal - Format a string as a SQL literal, append to buf
12399 : : */
12400 : : static void
6450 tgl@sss.pgh.pa.us 12401 : 18307 : simple_quote_literal(StringInfo buf, const char *val)
12402 : : {
12403 : : const char *valptr;
12404 : :
12405 : : /*
12406 : : * We always form the string literal according to standard SQL rules.
12407 : : */
12408 : 18307 : appendStringInfoChar(buf, '\'');
12409 [ + + ]: 185223 : for (valptr = val; *valptr; valptr++)
12410 : : {
12411 : 166916 : char ch = *valptr;
12412 : :
104 tgl@sss.pgh.pa.us 12413 [ + + ]:GNC 166916 : if (SQL_STR_DOUBLE(ch, false))
6450 tgl@sss.pgh.pa.us 12414 :CBC 204 : appendStringInfoChar(buf, ch);
12415 : 166916 : appendStringInfoChar(buf, ch);
12416 : : }
12417 : 18307 : appendStringInfoChar(buf, '\'');
12418 : 18307 : }
12419 : :
12420 : :
12421 : : /* ----------
12422 : : * get_sublink_expr - Parse back a sublink
12423 : : * ----------
12424 : : */
12425 : : static void
8545 12426 : 290 : get_sublink_expr(SubLink *sublink, deparse_context *context)
12427 : : {
9711 12428 : 290 : StringInfo buf = context->buf;
10077 bruce@momjian.us 12429 : 290 : Query *query = (Query *) (sublink->subselect);
7433 tgl@sss.pgh.pa.us 12430 : 290 : char *opname = NULL;
12431 : : bool need_paren;
12432 : :
8428 12433 [ + + ]: 290 : if (sublink->subLinkType == ARRAY_SUBLINK)
4569 rhaas@postgresql.org 12434 : 14 : appendStringInfoString(buf, "ARRAY(");
12435 : : else
8428 tgl@sss.pgh.pa.us 12436 : 276 : appendStringInfoChar(buf, '(');
12437 : :
12438 : : /*
12439 : : * Note that we print the name of only the first operator, when there are
12440 : : * multiple combining operators. This is an approximation that could go
12441 : : * wrong in various scenarios (operators in different schemas, renamed
12442 : : * operators, etc) but there is not a whole lot we can do about it, since
12443 : : * the syntax allows only one operator to be shown.
12444 : : */
7433 12445 [ + + ]: 290 : if (sublink->testexpr)
12446 : : {
12447 [ + + ]: 12 : if (IsA(sublink->testexpr, OpExpr))
12448 : : {
12449 : : /* single combining operator */
7153 bruce@momjian.us 12450 : 4 : OpExpr *opexpr = (OpExpr *) sublink->testexpr;
12451 : :
7433 tgl@sss.pgh.pa.us 12452 : 4 : get_rule_expr(linitial(opexpr->args), context, true);
12453 : 4 : opname = generate_operator_name(opexpr->opno,
12454 : 4 : exprType(linitial(opexpr->args)),
12455 : 4 : exprType(lsecond(opexpr->args)));
12456 : : }
12457 [ + + ]: 8 : else if (IsA(sublink->testexpr, BoolExpr))
12458 : : {
12459 : : /* multiple combining operators, = or <> cases */
12460 : : char *sep;
12461 : : ListCell *l;
12462 : :
9571 12463 : 4 : appendStringInfoChar(buf, '(');
7433 12464 : 4 : sep = "";
12465 [ + - + + : 12 : foreach(l, ((BoolExpr *) sublink->testexpr)->args)
+ + ]
12466 : : {
3312 12467 : 8 : OpExpr *opexpr = lfirst_node(OpExpr, l);
12468 : :
7433 12469 : 8 : appendStringInfoString(buf, sep);
12470 : 8 : get_rule_expr(linitial(opexpr->args), context, true);
12471 [ + + ]: 8 : if (!opname)
12472 : 4 : opname = generate_operator_name(opexpr->opno,
3240 12473 : 4 : exprType(linitial(opexpr->args)),
12474 : 4 : exprType(lsecond(opexpr->args)));
7433 12475 : 8 : sep = ", ";
12476 : : }
7880 12477 : 4 : appendStringInfoChar(buf, ')');
12478 : : }
7433 12479 [ + - ]: 4 : else if (IsA(sublink->testexpr, RowCompareExpr))
12480 : : {
12481 : : /* multiple combining operators, < <= > >= cases */
12482 : 4 : RowCompareExpr *rcexpr = (RowCompareExpr *) sublink->testexpr;
12483 : :
12484 : 4 : appendStringInfoChar(buf, '(');
12485 : 4 : get_rule_expr((Node *) rcexpr->largs, context, true);
12486 : 4 : opname = generate_operator_name(linitial_oid(rcexpr->opnos),
12487 : 4 : exprType(linitial(rcexpr->largs)),
3240 12488 : 4 : exprType(linitial(rcexpr->rargs)));
7433 12489 : 4 : appendStringInfoChar(buf, ')');
12490 : : }
12491 : : else
7433 tgl@sss.pgh.pa.us 12492 [ # # ]:UBC 0 : elog(ERROR, "unrecognized testexpr type: %d",
12493 : : (int) nodeTag(sublink->testexpr));
12494 : : }
12495 : :
9668 tgl@sss.pgh.pa.us 12496 :CBC 290 : need_paren = true;
12497 : :
9842 bruce@momjian.us 12498 [ + + + - : 290 : switch (sublink->subLinkType)
+ - ]
12499 : : {
10077 12500 : 113 : case EXISTS_SUBLINK:
4569 rhaas@postgresql.org 12501 : 113 : appendStringInfoString(buf, "EXISTS ");
10077 bruce@momjian.us 12502 : 113 : break;
12503 : :
12504 : 8 : case ANY_SUBLINK:
3240 tgl@sss.pgh.pa.us 12505 [ + + ]: 8 : if (strcmp(opname, "=") == 0) /* Represent = ANY as IN */
4569 rhaas@postgresql.org 12506 : 4 : appendStringInfoString(buf, " IN ");
12507 : : else
7433 tgl@sss.pgh.pa.us 12508 : 4 : appendStringInfo(buf, " %s ANY ", opname);
10077 bruce@momjian.us 12509 : 8 : break;
12510 : :
12511 : 4 : case ALL_SUBLINK:
7433 tgl@sss.pgh.pa.us 12512 : 4 : appendStringInfo(buf, " %s ALL ", opname);
10077 bruce@momjian.us 12513 : 4 : break;
12514 : :
7433 tgl@sss.pgh.pa.us 12515 :UBC 0 : case ROWCOMPARE_SUBLINK:
12516 : 0 : appendStringInfo(buf, " %s ", opname);
10077 bruce@momjian.us 12517 : 0 : break;
12518 : :
9668 tgl@sss.pgh.pa.us 12519 :CBC 165 : case EXPR_SUBLINK:
12520 : : case MULTIEXPR_SUBLINK:
12521 : : case ARRAY_SUBLINK:
12522 : 165 : need_paren = false;
12523 : 165 : break;
12524 : :
6422 tgl@sss.pgh.pa.us 12525 :UBC 0 : case CTE_SUBLINK: /* shouldn't occur in a SubLink */
12526 : : default:
8318 12527 [ # # ]: 0 : elog(ERROR, "unrecognized sublink type: %d",
12528 : : (int) sublink->subLinkType);
12529 : : break;
12530 : : }
12531 : :
9668 tgl@sss.pgh.pa.us 12532 [ + + ]:CBC 290 : if (need_paren)
9571 12533 : 125 : appendStringInfoChar(buf, '(');
12534 : :
1445 12535 : 290 : get_query_def(query, buf, context->namespaces, NULL, false,
12536 : : context->prettyFlags, context->wrapColumn,
12537 : : context->indentLevel);
12538 : :
9668 12539 [ + + ]: 290 : if (need_paren)
4569 rhaas@postgresql.org 12540 : 125 : appendStringInfoString(buf, "))");
12541 : : else
9571 tgl@sss.pgh.pa.us 12542 : 165 : appendStringInfoChar(buf, ')');
10116 bruce@momjian.us 12543 : 290 : }
12544 : :
12545 : :
12546 : : /* ----------
12547 : : * get_xmltable - Parse back a XMLTABLE function
12548 : : * ----------
12549 : : */
12550 : : static void
761 amitlan@postgresql.o 12551 : 38 : get_xmltable(TableFunc *tf, deparse_context *context, bool showimplicit)
12552 : : {
3345 alvherre@alvh.no-ip. 12553 : 38 : StringInfo buf = context->buf;
12554 : :
12555 : 38 : appendStringInfoString(buf, "XMLTABLE(");
12556 : :
12557 [ + + ]: 38 : if (tf->ns_uris != NIL)
12558 : : {
12559 : : ListCell *lc1,
12560 : : *lc2;
12561 : 9 : bool first = true;
12562 : :
12563 : 9 : appendStringInfoString(buf, "XMLNAMESPACES (");
12564 [ + - + + : 18 : forboth(lc1, tf->ns_uris, lc2, tf->ns_names)
+ - + + +
+ + - +
+ ]
12565 : : {
12566 : 9 : Node *expr = (Node *) lfirst(lc1);
1699 peter@eisentraut.org 12567 : 9 : String *ns_node = lfirst_node(String, lc2);
12568 : :
3345 alvherre@alvh.no-ip. 12569 [ - + ]: 9 : if (!first)
3345 alvherre@alvh.no-ip. 12570 :UBC 0 : appendStringInfoString(buf, ", ");
12571 : : else
3345 alvherre@alvh.no-ip. 12572 :CBC 9 : first = false;
12573 : :
2787 tgl@sss.pgh.pa.us 12574 [ + - ]: 9 : if (ns_node != NULL)
12575 : : {
3345 alvherre@alvh.no-ip. 12576 : 9 : get_rule_expr(expr, context, showimplicit);
478 dean.a.rasheed@gmail 12577 : 9 : appendStringInfo(buf, " AS %s",
12578 : 9 : quote_identifier(strVal(ns_node)));
12579 : : }
12580 : : else
12581 : : {
3345 alvherre@alvh.no-ip. 12582 :UBC 0 : appendStringInfoString(buf, "DEFAULT ");
12583 : 0 : get_rule_expr(expr, context, showimplicit);
12584 : : }
12585 : : }
3345 alvherre@alvh.no-ip. 12586 :CBC 9 : appendStringInfoString(buf, "), ");
12587 : : }
12588 : :
12589 : 38 : appendStringInfoChar(buf, '(');
12590 : 38 : get_rule_expr((Node *) tf->rowexpr, context, showimplicit);
12591 : 38 : appendStringInfoString(buf, ") PASSING (");
12592 : 38 : get_rule_expr((Node *) tf->docexpr, context, showimplicit);
12593 : 38 : appendStringInfoChar(buf, ')');
12594 : :
12595 [ + - ]: 38 : if (tf->colexprs != NIL)
12596 : : {
12597 : : ListCell *l1;
12598 : : ListCell *l2;
12599 : : ListCell *l3;
12600 : : ListCell *l4;
12601 : : ListCell *l5;
12602 : 38 : int colnum = 0;
12603 : :
12604 : 38 : appendStringInfoString(buf, " COLUMNS ");
2623 tgl@sss.pgh.pa.us 12605 [ + - + + : 231 : forfive(l1, tf->colnames, l2, tf->coltypes, l3, tf->coltypmods,
+ - + + +
- + + + -
+ + + - +
+ + + + -
+ - + - +
- + + ]
12606 : : l4, tf->colexprs, l5, tf->coldefexprs)
12607 : : {
3345 alvherre@alvh.no-ip. 12608 : 193 : char *colname = strVal(lfirst(l1));
2623 tgl@sss.pgh.pa.us 12609 : 193 : Oid typid = lfirst_oid(l2);
12610 : 193 : int32 typmod = lfirst_int(l3);
12611 : 193 : Node *colexpr = (Node *) lfirst(l4);
12612 : 193 : Node *coldefexpr = (Node *) lfirst(l5);
12613 : 193 : bool ordinality = (tf->ordinalitycol == colnum);
3345 alvherre@alvh.no-ip. 12614 : 193 : bool notnull = bms_is_member(colnum, tf->notnulls);
12615 : :
12616 [ + + ]: 193 : if (colnum > 0)
12617 : 155 : appendStringInfoString(buf, ", ");
12618 : 193 : colnum++;
12619 : :
12620 [ + + ]: 365 : appendStringInfo(buf, "%s %s", quote_identifier(colname),
12621 : : ordinality ? "FOR ORDINALITY" :
12622 : 172 : format_type_with_typemod(typid, typmod));
12623 [ + + ]: 193 : if (ordinality)
12624 : 21 : continue;
12625 : :
12626 [ + + ]: 172 : if (coldefexpr != NULL)
12627 : : {
12628 : 21 : appendStringInfoString(buf, " DEFAULT (");
12629 : 21 : get_rule_expr((Node *) coldefexpr, context, showimplicit);
12630 : 21 : appendStringInfoChar(buf, ')');
12631 : : }
12632 [ + + ]: 172 : if (colexpr != NULL)
12633 : : {
12634 : 156 : appendStringInfoString(buf, " PATH (");
12635 : 156 : get_rule_expr((Node *) colexpr, context, showimplicit);
12636 : 156 : appendStringInfoChar(buf, ')');
12637 : : }
12638 [ + + ]: 172 : if (notnull)
12639 : 21 : appendStringInfoString(buf, " NOT NULL");
12640 : : }
12641 : : }
12642 : :
12643 : 38 : appendStringInfoChar(buf, ')');
12644 : 38 : }
12645 : :
12646 : : /*
12647 : : * get_json_table_nested_columns - Parse back nested JSON_TABLE columns
12648 : : */
12649 : : static void
757 amitlan@postgresql.o 12650 : 68 : get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan,
12651 : : deparse_context *context, bool showimplicit,
12652 : : bool needcomma)
12653 : : {
12654 [ + + ]: 68 : if (IsA(plan, JsonTablePathScan))
12655 : : {
12656 : 48 : JsonTablePathScan *scan = castNode(JsonTablePathScan, plan);
12657 : :
12658 [ + + ]: 48 : if (needcomma)
12659 : 32 : appendStringInfoChar(context->buf, ',');
12660 : :
12661 : 48 : appendStringInfoChar(context->buf, ' ');
12662 : 48 : appendContextKeyword(context, "NESTED PATH ", 0, 0, 0);
12663 : 48 : get_const_expr(scan->path->value, context, -1);
12664 : 48 : appendStringInfo(context->buf, " AS %s", quote_identifier(scan->path->name));
12665 : 48 : get_json_table_columns(tf, scan, context, showimplicit);
12666 : : }
12667 [ + - ]: 20 : else if (IsA(plan, JsonTableSiblingJoin))
12668 : : {
12669 : 20 : JsonTableSiblingJoin *join = (JsonTableSiblingJoin *) plan;
12670 : :
12671 : 20 : get_json_table_nested_columns(tf, join->lplan, context, showimplicit,
12672 : : needcomma);
12673 : 20 : get_json_table_nested_columns(tf, join->rplan, context, showimplicit,
12674 : : true);
12675 : : }
12676 : 68 : }
12677 : :
12678 : : /*
12679 : : * get_json_table_columns - Parse back JSON_TABLE columns
12680 : : */
12681 : : static void
12682 : 120 : get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan,
12683 : : deparse_context *context,
12684 : : bool showimplicit)
12685 : : {
761 12686 : 120 : StringInfo buf = context->buf;
12687 : : ListCell *lc_colname;
12688 : : ListCell *lc_coltype;
12689 : : ListCell *lc_coltypmod;
12690 : : ListCell *lc_colvalexpr;
12691 : 120 : int colnum = 0;
12692 : :
12693 : 120 : appendStringInfoChar(buf, ' ');
12694 : 120 : appendContextKeyword(context, "COLUMNS (", 0, 0, 0);
12695 : :
12696 [ + + ]: 120 : if (PRETTY_INDENT(context))
12697 : 92 : context->indentLevel += PRETTYINDENT_VAR;
12698 : :
12699 [ + - + + : 572 : forfour(lc_colname, tf->colnames,
+ - + + +
- + + + -
+ + + + +
- + - + -
+ + ]
12700 : : lc_coltype, tf->coltypes,
12701 : : lc_coltypmod, tf->coltypmods,
12702 : : lc_colvalexpr, tf->colvalexprs)
12703 : : {
12704 : 484 : char *colname = strVal(lfirst(lc_colname));
12705 : : JsonExpr *colexpr;
12706 : : Oid typid;
12707 : : int32 typmod;
12708 : : bool ordinality;
12709 : : JsonBehaviorType default_behavior;
12710 : :
12711 : 484 : typid = lfirst_oid(lc_coltype);
12712 : 484 : typmod = lfirst_int(lc_coltypmod);
12713 : 484 : colexpr = castNode(JsonExpr, lfirst(lc_colvalexpr));
12714 : :
12715 : : /* Skip columns that don't belong to this scan. */
757 12716 [ + + + + ]: 484 : if (scan->colMin < 0 || colnum < scan->colMin)
12717 : : {
12718 : 176 : colnum++;
12719 : 176 : continue;
12720 : : }
12721 [ + + ]: 308 : if (colnum > scan->colMax)
12722 : 32 : break;
12723 : :
12724 [ + + ]: 276 : if (colnum > scan->colMin)
761 12725 : 172 : appendStringInfoString(buf, ", ");
12726 : :
12727 : 276 : colnum++;
12728 : :
12729 : 276 : ordinality = !colexpr;
12730 : :
12731 : 276 : appendContextKeyword(context, "", 0, 0, 0);
12732 : :
12733 [ + + ]: 540 : appendStringInfo(buf, "%s %s", quote_identifier(colname),
12734 : : ordinality ? "FOR ORDINALITY" :
12735 : 264 : format_type_with_typemod(typid, typmod));
12736 [ + + ]: 276 : if (ordinality)
12737 : 12 : continue;
12738 : :
12739 : : /*
12740 : : * Set default_behavior to guide get_json_expr_options() on whether to
12741 : : * emit the ON ERROR / EMPTY clauses.
12742 : : */
12743 [ + + ]: 264 : if (colexpr->op == JSON_EXISTS_OP)
12744 : : {
12745 : 24 : appendStringInfoString(buf, " EXISTS");
12746 : 24 : default_behavior = JSON_BEHAVIOR_FALSE;
12747 : : }
12748 : : else
12749 : : {
12750 [ + + ]: 240 : if (colexpr->op == JSON_QUERY_OP)
12751 : : {
12752 : : char typcategory;
12753 : : bool typispreferred;
12754 : :
12755 : 116 : get_type_category_preferred(typid, &typcategory, &typispreferred);
12756 : :
12757 [ + + ]: 116 : if (typcategory == TYPCATEGORY_STRING)
12758 : 24 : appendStringInfoString(buf,
12759 [ - + ]: 24 : colexpr->format->format_type == JS_FORMAT_JSONB ?
12760 : : " FORMAT JSONB" : " FORMAT JSON");
12761 : : }
12762 : :
12763 : 240 : default_behavior = JSON_BEHAVIOR_NULL;
12764 : : }
12765 : :
12766 : 264 : appendStringInfoString(buf, " PATH ");
12767 : :
12768 : 264 : get_json_path_spec(colexpr->path_spec, context, showimplicit);
12769 : :
12770 : 264 : get_json_expr_options(colexpr, context, default_behavior);
12771 : : }
12772 : :
757 12773 [ + + ]: 120 : if (scan->child)
12774 : 28 : get_json_table_nested_columns(tf, scan->child, context, showimplicit,
12775 : 28 : scan->colMin >= 0);
12776 : :
761 12777 [ + + ]: 120 : if (PRETTY_INDENT(context))
12778 : 92 : context->indentLevel -= PRETTYINDENT_VAR;
12779 : :
12780 : 120 : appendContextKeyword(context, ")", 0, 0, 0);
12781 : 120 : }
12782 : :
12783 : : /* ----------
12784 : : * get_json_table - Parse back a JSON_TABLE function
12785 : : * ----------
12786 : : */
12787 : : static void
12788 : 72 : get_json_table(TableFunc *tf, deparse_context *context, bool showimplicit)
12789 : : {
12790 : 72 : StringInfo buf = context->buf;
12791 : 72 : JsonExpr *jexpr = castNode(JsonExpr, tf->docexpr);
12792 : 72 : JsonTablePathScan *root = castNode(JsonTablePathScan, tf->plan);
12793 : :
12794 : 72 : appendStringInfoString(buf, "JSON_TABLE(");
12795 : :
12796 [ + + ]: 72 : if (PRETTY_INDENT(context))
12797 : 44 : context->indentLevel += PRETTYINDENT_VAR;
12798 : :
12799 : 72 : appendContextKeyword(context, "", 0, 0, 0);
12800 : :
12801 : 72 : get_rule_expr(jexpr->formatted_expr, context, showimplicit);
12802 : :
12803 : 72 : appendStringInfoString(buf, ", ");
12804 : :
12805 : 72 : get_const_expr(root->path->value, context, -1);
12806 : :
12807 : 72 : appendStringInfo(buf, " AS %s", quote_identifier(root->path->name));
12808 : :
12809 [ + + ]: 72 : if (jexpr->passing_values)
12810 : : {
12811 : : ListCell *lc1,
12812 : : *lc2;
12813 : 56 : bool needcomma = false;
12814 : :
12815 : 56 : appendStringInfoChar(buf, ' ');
12816 : 56 : appendContextKeyword(context, "PASSING ", 0, 0, 0);
12817 : :
12818 [ + + ]: 56 : if (PRETTY_INDENT(context))
12819 : 28 : context->indentLevel += PRETTYINDENT_VAR;
12820 : :
12821 [ + - + + : 168 : forboth(lc1, jexpr->passing_names,
+ - + + +
+ + - +
+ ]
12822 : : lc2, jexpr->passing_values)
12823 : : {
12824 [ + + ]: 112 : if (needcomma)
12825 : 56 : appendStringInfoString(buf, ", ");
12826 : 112 : needcomma = true;
12827 : :
12828 : 112 : appendContextKeyword(context, "", 0, 0, 0);
12829 : :
12830 : 112 : get_rule_expr((Node *) lfirst(lc2), context, false);
12831 : 112 : appendStringInfo(buf, " AS %s",
12832 : 112 : quote_identifier((lfirst_node(String, lc1))->sval)
12833 : : );
12834 : : }
12835 : :
12836 [ + + ]: 56 : if (PRETTY_INDENT(context))
12837 : 28 : context->indentLevel -= PRETTYINDENT_VAR;
12838 : : }
12839 : :
757 12840 : 72 : get_json_table_columns(tf, castNode(JsonTablePathScan, tf->plan), context,
12841 : : showimplicit);
12842 : :
606 12843 [ + + ]: 72 : if (jexpr->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY)
761 12844 : 4 : get_json_behavior(jexpr->on_error, context, "ERROR");
12845 : :
12846 [ + + ]: 72 : if (PRETTY_INDENT(context))
12847 : 44 : context->indentLevel -= PRETTYINDENT_VAR;
12848 : :
12849 : 72 : appendContextKeyword(context, ")", 0, 0, 0);
12850 : 72 : }
12851 : :
12852 : : /* ----------
12853 : : * get_tablefunc - Parse back a table function
12854 : : * ----------
12855 : : */
12856 : : static void
12857 : 110 : get_tablefunc(TableFunc *tf, deparse_context *context, bool showimplicit)
12858 : : {
12859 : : /* XMLTABLE and JSON_TABLE are the only existing implementations. */
12860 : :
12861 [ + + ]: 110 : if (tf->functype == TFT_XMLTABLE)
12862 : 38 : get_xmltable(tf, context, showimplicit);
12863 [ + - ]: 72 : else if (tf->functype == TFT_JSON_TABLE)
12864 : 72 : get_json_table(tf, context, showimplicit);
12865 : 110 : }
12866 : :
12867 : : /* ----------
12868 : : * get_from_clause - Parse back a FROM clause
12869 : : *
12870 : : * "prefix" is the keyword that denotes the start of the list of FROM
12871 : : * elements. It is FROM when used to parse back SELECT and UPDATE, but
12872 : : * is USING when parsing back DELETE.
12873 : : * ----------
12874 : : */
12875 : : static void
7698 neilc@samurai.com 12876 : 3011 : get_from_clause(Query *query, const char *prefix, deparse_context *context)
12877 : : {
9366 tgl@sss.pgh.pa.us 12878 : 3011 : StringInfo buf = context->buf;
8315 12879 : 3011 : bool first = true;
12880 : : ListCell *l;
12881 : :
12882 : : /*
12883 : : * We use the query's jointree as a guide to what to print. However, we
12884 : : * must ignore auto-added RTEs that are marked not inFromCl. (These can
12885 : : * only appear at the top level of the jointree, so it's sufficient to
12886 : : * check here.) This check also ensures we ignore the rule pseudo-RTEs
12887 : : * for NEW and OLD.
12888 : : */
9349 12889 [ + + + + : 5994 : foreach(l, query->jointree->fromlist)
+ + ]
12890 : : {
9175 bruce@momjian.us 12891 : 2983 : Node *jtnode = (Node *) lfirst(l);
12892 : :
9366 tgl@sss.pgh.pa.us 12893 [ + + ]: 2983 : if (IsA(jtnode, RangeTblRef))
12894 : : {
12895 : 2378 : int varno = ((RangeTblRef *) jtnode)->rtindex;
12896 : 2378 : RangeTblEntry *rte = rt_fetch(varno, query->rtable);
12897 : :
12898 [ + + ]: 2378 : if (!rte->inFromCl)
12899 : 230 : continue;
12900 : : }
12901 : :
8315 12902 [ + + ]: 2753 : if (first)
12903 : : {
7698 neilc@samurai.com 12904 : 2520 : appendContextKeyword(context, prefix,
12905 : : -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
8315 tgl@sss.pgh.pa.us 12906 : 2520 : first = false;
12907 : :
5189 andrew@dunslane.net 12908 : 2520 : get_from_clause_item(jtnode, query, context);
12909 : : }
12910 : : else
12911 : : {
12912 : : StringInfoData itembuf;
12913 : :
8310 bruce@momjian.us 12914 : 233 : appendStringInfoString(buf, ", ");
12915 : :
12916 : : /*
12917 : : * Put the new FROM item's text into itembuf so we can decide
12918 : : * after we've got it whether or not it needs to go on a new line.
12919 : : */
4880 tgl@sss.pgh.pa.us 12920 : 233 : initStringInfo(&itembuf);
12921 : 233 : context->buf = &itembuf;
12922 : :
5189 andrew@dunslane.net 12923 : 233 : get_from_clause_item(jtnode, query, context);
12924 : :
12925 : : /* Restore context's output buffer */
12926 : 233 : context->buf = buf;
12927 : :
12928 : : /* Consider line-wrapping if enabled */
4880 tgl@sss.pgh.pa.us 12929 [ + - + - ]: 233 : if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
12930 : : {
12931 : : /* Does the new item start with a new line? */
4558 12932 [ + - - + ]: 233 : if (itembuf.len > 0 && itembuf.data[0] == '\n')
12933 : : {
12934 : : /* If so, we shouldn't add anything */
12935 : : /* instead, remove any trailing spaces currently in buf */
4558 tgl@sss.pgh.pa.us 12936 :UBC 0 : removeStringInfoSpaces(buf);
12937 : : }
12938 : : else
12939 : : {
12940 : : char *trailing_nl;
12941 : :
12942 : : /* Locate the start of the current line in the buffer */
4558 tgl@sss.pgh.pa.us 12943 :CBC 233 : trailing_nl = strrchr(buf->data, '\n');
12944 [ - + ]: 233 : if (trailing_nl == NULL)
4558 tgl@sss.pgh.pa.us 12945 :UBC 0 : trailing_nl = buf->data;
12946 : : else
4558 tgl@sss.pgh.pa.us 12947 :CBC 233 : trailing_nl++;
12948 : :
12949 : : /*
12950 : : * Add a newline, plus some indentation, if the new item
12951 : : * would cause an overflow.
12952 : : */
12953 [ + - ]: 233 : if (strlen(trailing_nl) + itembuf.len > context->wrapColumn)
12954 : 233 : appendContextKeyword(context, "", -PRETTYINDENT_STD,
12955 : : PRETTYINDENT_STD,
12956 : : PRETTYINDENT_VAR);
12957 : : }
12958 : : }
12959 : :
12960 : : /* Add the new item */
2478 drowley@postgresql.o 12961 : 233 : appendBinaryStringInfo(buf, itembuf.data, itembuf.len);
12962 : :
12963 : : /* clean up */
4880 tgl@sss.pgh.pa.us 12964 : 233 : pfree(itembuf.data);
12965 : : }
12966 : : }
9366 12967 : 3011 : }
12968 : :
12969 : : static void
12970 : 4633 : get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
12971 : : {
12972 : 4633 : StringInfo buf = context->buf;
4873 12973 : 4633 : deparse_namespace *dpns = (deparse_namespace *) linitial(context->namespaces);
12974 : :
9366 12975 [ + + ]: 4633 : if (IsA(jtnode, RangeTblRef))
12976 : : {
12977 : 3693 : int varno = ((RangeTblRef *) jtnode)->rtindex;
12978 : 3693 : RangeTblEntry *rte = rt_fetch(varno, query->rtable);
4873 12979 : 3693 : deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
4548 12980 : 3693 : RangeTblFunction *rtfunc1 = NULL;
12981 : :
5019 12982 [ + + ]: 3693 : if (rte->lateral)
12983 : 73 : appendStringInfoString(buf, "LATERAL ");
12984 : :
12985 : : /* Print the FROM item proper */
8810 12986 [ + + + + : 3693 : switch (rte->rtekind)
+ + + - ]
12987 : : {
12988 : 2741 : case RTE_RELATION:
12989 : : /* Normal relation RTE */
12990 : 5482 : appendStringInfo(buf, "%s%s",
12991 [ + + ]: 2741 : only_marker(rte),
12992 : : generate_relation_name(rte->relid,
12993 : : context->namespaces));
12994 : 2741 : break;
12995 : 200 : case RTE_SUBQUERY:
12996 : : /* Subquery RTE */
12997 : 200 : appendStringInfoChar(buf, '(');
8310 bruce@momjian.us 12998 : 200 : get_query_def(rte->subquery, buf, context->namespaces, NULL,
12999 : : true,
13000 : : context->prettyFlags, context->wrapColumn,
13001 : : context->indentLevel);
8810 tgl@sss.pgh.pa.us 13002 : 200 : appendStringInfoChar(buf, ')');
13003 : 200 : break;
8759 13004 : 553 : case RTE_FUNCTION:
13005 : : /* Function RTE */
4548 13006 : 553 : rtfunc1 = (RangeTblFunction *) linitial(rte->functions);
13007 : :
13008 : : /*
13009 : : * Omit ROWS FROM() syntax for just one function, unless it
13010 : : * has both a coldeflist and WITH ORDINALITY. If it has both,
13011 : : * we must use ROWS FROM() syntax to avoid ambiguity about
13012 : : * whether the coldeflist includes the ordinality column.
13013 : : */
13014 [ + + ]: 553 : if (list_length(rte->functions) == 1 &&
13015 [ - + - - ]: 533 : (rtfunc1->funccolnames == NIL || !rte->funcordinality))
13016 : : {
3218 13017 : 533 : get_rule_expr_funccall(rtfunc1->funcexpr, context, true);
13018 : : /* we'll print the coldeflist below, if it has one */
13019 : : }
13020 : : else
13021 : : {
13022 : : bool all_unnest;
13023 : : ListCell *lc;
13024 : :
13025 : : /*
13026 : : * If all the function calls in the list are to unnest,
13027 : : * and none need a coldeflist, then collapse the list back
13028 : : * down to UNNEST(args). (If we had more than one
13029 : : * built-in unnest function, this would get more
13030 : : * difficult.)
13031 : : *
13032 : : * XXX This is pretty ugly, since it makes not-terribly-
13033 : : * future-proof assumptions about what the parser would do
13034 : : * with the output; but the alternative is to emit our
13035 : : * nonstandard ROWS FROM() notation for what might have
13036 : : * been a perfectly spec-compliant multi-argument
13037 : : * UNNEST().
13038 : : */
4548 13039 : 20 : all_unnest = true;
13040 [ + - + + : 52 : foreach(lc, rte->functions)
+ + ]
13041 : : {
13042 : 44 : RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
13043 : :
13044 [ + - ]: 44 : if (!IsA(rtfunc->funcexpr, FuncExpr) ||
2010 13045 [ + + ]: 44 : ((FuncExpr *) rtfunc->funcexpr)->funcid != F_UNNEST_ANYARRAY ||
4548 13046 [ - + ]: 32 : rtfunc->funccolnames != NIL)
13047 : : {
13048 : 12 : all_unnest = false;
13049 : 12 : break;
13050 : : }
13051 : : }
13052 : :
13053 [ + + ]: 20 : if (all_unnest)
13054 : : {
13055 : 8 : List *allargs = NIL;
13056 : :
13057 [ + - + + : 32 : foreach(lc, rte->functions)
+ + ]
13058 : : {
13059 : 24 : RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
13060 : 24 : List *args = ((FuncExpr *) rtfunc->funcexpr)->args;
13061 : :
2458 13062 : 24 : allargs = list_concat(allargs, args);
13063 : : }
13064 : :
4548 13065 : 8 : appendStringInfoString(buf, "UNNEST(");
13066 : 8 : get_rule_expr((Node *) allargs, context, true);
13067 : 8 : appendStringInfoChar(buf, ')');
13068 : : }
13069 : : else
13070 : : {
13071 : 12 : int funcno = 0;
13072 : :
4529 noah@leadboat.com 13073 : 12 : appendStringInfoString(buf, "ROWS FROM(");
4548 tgl@sss.pgh.pa.us 13074 [ + - + + : 44 : foreach(lc, rte->functions)
+ + ]
13075 : : {
13076 : 32 : RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
13077 : :
13078 [ + + ]: 32 : if (funcno > 0)
13079 : 20 : appendStringInfoString(buf, ", ");
3218 13080 : 32 : get_rule_expr_funccall(rtfunc->funcexpr, context, true);
4548 13081 [ + + ]: 32 : if (rtfunc->funccolnames != NIL)
13082 : : {
13083 : : /* Reconstruct the column definition list */
13084 : 4 : appendStringInfoString(buf, " AS ");
13085 : 4 : get_from_clause_coldeflist(rtfunc,
13086 : : NULL,
13087 : : context);
13088 : : }
13089 : 32 : funcno++;
13090 : : }
13091 : 12 : appendStringInfoChar(buf, ')');
13092 : : }
13093 : : /* prevent printing duplicate coldeflist below */
13094 : 20 : rtfunc1 = NULL;
13095 : : }
4663 stark@mit.edu 13096 [ + + ]: 553 : if (rte->funcordinality)
13097 : 12 : appendStringInfoString(buf, " WITH ORDINALITY");
8759 tgl@sss.pgh.pa.us 13098 : 553 : break;
3345 alvherre@alvh.no-ip. 13099 : 62 : case RTE_TABLEFUNC:
13100 : 62 : get_tablefunc(rte->tablefunc, context, true);
13101 : 62 : break;
50 peter@eisentraut.org 13102 :GNC 14 : case RTE_GRAPH_TABLE:
13103 : 14 : appendStringInfoString(buf, "GRAPH_TABLE (");
13104 : 14 : appendStringInfoString(buf, generate_relation_name(rte->relid, context->namespaces));
13105 : 14 : appendStringInfoString(buf, " MATCH ");
13106 : 14 : get_graph_pattern_def(rte->graph_pattern, context);
13107 : 14 : appendStringInfoString(buf, " COLUMNS (");
13108 : : {
13109 : 14 : bool first = true;
13110 : :
11 13111 [ + - + + : 60 : foreach_node(TargetEntry, te, rte->graph_table_columns)
+ + ]
13112 : : {
50 13113 [ + + ]: 32 : if (!first)
13114 : 18 : appendStringInfoString(buf, ", ");
13115 : : else
13116 : 14 : first = false;
13117 : :
11 13118 : 32 : get_rule_expr((Node *) te->expr, context, false);
50 13119 : 32 : appendStringInfoString(buf, " AS ");
13120 : 32 : appendStringInfoString(buf, quote_identifier(te->resname));
13121 : : }
13122 : : }
22 drowley@postgresql.o 13123 : 14 : appendStringInfoString(buf, "))");
50 peter@eisentraut.org 13124 : 14 : break;
7216 mail@joeconway.com 13125 :CBC 8 : case RTE_VALUES:
13126 : : /* Values list RTE */
4087 tgl@sss.pgh.pa.us 13127 : 8 : appendStringInfoChar(buf, '(');
7216 mail@joeconway.com 13128 : 8 : get_values_def(rte->values_lists, context);
4087 tgl@sss.pgh.pa.us 13129 : 8 : appendStringInfoChar(buf, ')');
7216 mail@joeconway.com 13130 : 8 : break;
6422 tgl@sss.pgh.pa.us 13131 : 115 : case RTE_CTE:
13132 : 115 : appendStringInfoString(buf, quote_identifier(rte->ctename));
13133 : 115 : break;
8810 tgl@sss.pgh.pa.us 13134 :UBC 0 : default:
8318 13135 [ # # ]: 0 : elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
13136 : : break;
13137 : : }
13138 : :
13139 : : /* Print the relation alias, if needed */
1173 tgl@sss.pgh.pa.us 13140 :CBC 3693 : get_rte_alias(rte, varno, false, context);
13141 : :
13142 : : /* Print the column definitions or aliases, if needed */
4548 13143 [ + + - + ]: 3693 : if (rtfunc1 && rtfunc1->funccolnames != NIL)
13144 : : {
13145 : : /* Reconstruct the columndef list, which is also the aliases */
4548 tgl@sss.pgh.pa.us 13146 :UBC 0 : get_from_clause_coldeflist(rtfunc1, colinfo, context);
13147 : : }
13148 : : else
13149 : : {
13150 : : /* Else print column aliases as needed */
4873 tgl@sss.pgh.pa.us 13151 :CBC 3693 : get_column_alias_list(colinfo, context);
13152 : : }
13153 : :
13154 : : /* Tablesample clause must go after any alias */
3937 13155 [ + + + + ]: 3693 : if (rte->rtekind == RTE_RELATION && rte->tablesample)
13156 : 18 : get_tablesample_def(rte->tablesample, context);
13157 : : }
9366 13158 [ + - ]: 940 : else if (IsA(jtnode, JoinExpr))
13159 : : {
13160 : 940 : JoinExpr *j = (JoinExpr *) jtnode;
4873 13161 : 940 : deparse_columns *colinfo = deparse_columns_fetch(j->rtindex, dpns);
13162 : : bool need_paren_on_right;
13163 : :
8315 13164 : 2172 : need_paren_on_right = PRETTY_PAREN(context) &&
7618 13165 [ + + - + ]: 940 : !IsA(j->rarg, RangeTblRef) &&
2180 tgl@sss.pgh.pa.us 13166 [ # # # # ]:UBC 0 : !(IsA(j->rarg, JoinExpr) && ((JoinExpr *) j->rarg)->alias != NULL);
13167 : :
8315 tgl@sss.pgh.pa.us 13168 [ + + + + ]:CBC 940 : if (!PRETTY_PAREN(context) || j->alias != NULL)
8310 bruce@momjian.us 13169 : 720 : appendStringInfoChar(buf, '(');
13170 : :
9366 tgl@sss.pgh.pa.us 13171 : 940 : get_from_clause_item(j->larg, query, context);
13172 : :
4873 13173 [ + + + - : 940 : switch (j->jointype)
- ]
13174 : : {
13175 : 517 : case JOIN_INNER:
13176 [ + + ]: 517 : if (j->quals)
13177 : 489 : appendContextKeyword(context, " JOIN ",
13178 : : -PRETTYINDENT_STD,
13179 : : PRETTYINDENT_STD,
13180 : : PRETTYINDENT_JOIN);
13181 : : else
13182 : 28 : appendContextKeyword(context, " CROSS JOIN ",
13183 : : -PRETTYINDENT_STD,
13184 : : PRETTYINDENT_STD,
13185 : : PRETTYINDENT_JOIN);
13186 : 517 : break;
13187 : 355 : case JOIN_LEFT:
13188 : 355 : appendContextKeyword(context, " LEFT JOIN ",
13189 : : -PRETTYINDENT_STD,
13190 : : PRETTYINDENT_STD,
13191 : : PRETTYINDENT_JOIN);
13192 : 355 : break;
13193 : 68 : case JOIN_FULL:
13194 : 68 : appendContextKeyword(context, " FULL JOIN ",
13195 : : -PRETTYINDENT_STD,
13196 : : PRETTYINDENT_STD,
13197 : : PRETTYINDENT_JOIN);
13198 : 68 : break;
4873 tgl@sss.pgh.pa.us 13199 :UBC 0 : case JOIN_RIGHT:
13200 : 0 : appendContextKeyword(context, " RIGHT JOIN ",
13201 : : -PRETTYINDENT_STD,
13202 : : PRETTYINDENT_STD,
13203 : : PRETTYINDENT_JOIN);
13204 : 0 : break;
13205 : 0 : default:
13206 [ # # ]: 0 : elog(ERROR, "unrecognized join type: %d",
13207 : : (int) j->jointype);
13208 : : }
13209 : :
8315 tgl@sss.pgh.pa.us 13210 [ - + ]:CBC 940 : if (need_paren_on_right)
8310 bruce@momjian.us 13211 :UBC 0 : appendStringInfoChar(buf, '(');
9366 tgl@sss.pgh.pa.us 13212 :CBC 940 : get_from_clause_item(j->rarg, query, context);
8315 13213 [ - + ]: 940 : if (need_paren_on_right)
8310 bruce@momjian.us 13214 :UBC 0 : appendStringInfoChar(buf, ')');
13215 : :
4873 tgl@sss.pgh.pa.us 13216 [ + + ]:CBC 940 : if (j->usingClause)
13217 : : {
13218 : : ListCell *lc;
13219 : 280 : bool first = true;
13220 : :
4569 rhaas@postgresql.org 13221 : 280 : appendStringInfoString(buf, " USING (");
13222 : : /* Use the assigned names, not what's in usingClause */
4873 tgl@sss.pgh.pa.us 13223 [ + - + + : 664 : foreach(lc, colinfo->usingNames)
+ + ]
13224 : : {
13225 : 384 : char *colname = (char *) lfirst(lc);
13226 : :
13227 [ + + ]: 384 : if (first)
13228 : 280 : first = false;
13229 : : else
4569 rhaas@postgresql.org 13230 : 104 : appendStringInfoString(buf, ", ");
4873 tgl@sss.pgh.pa.us 13231 : 384 : appendStringInfoString(buf, quote_identifier(colname));
13232 : : }
13233 : 280 : appendStringInfoChar(buf, ')');
13234 : :
1861 peter@eisentraut.org 13235 [ + + ]: 280 : if (j->join_using_alias)
13236 : 8 : appendStringInfo(buf, " AS %s",
13237 : 8 : quote_identifier(j->join_using_alias->aliasname));
13238 : : }
4873 tgl@sss.pgh.pa.us 13239 [ + + ]: 660 : else if (j->quals)
13240 : : {
4569 rhaas@postgresql.org 13241 : 628 : appendStringInfoString(buf, " ON ");
4873 tgl@sss.pgh.pa.us 13242 [ + + ]: 628 : if (!PRETTY_PAREN(context))
13243 : 624 : appendStringInfoChar(buf, '(');
13244 : 628 : get_rule_expr(j->quals, context, false);
13245 [ + + ]: 628 : if (!PRETTY_PAREN(context))
13246 : 624 : appendStringInfoChar(buf, ')');
13247 : : }
3211 13248 [ + + ]: 32 : else if (j->jointype != JOIN_INNER)
13249 : : {
13250 : : /* If we didn't say CROSS JOIN above, we must provide an ON */
13251 : 4 : appendStringInfoString(buf, " ON TRUE");
13252 : : }
13253 : :
8315 13254 [ + + + + ]: 940 : if (!PRETTY_PAREN(context) || j->alias != NULL)
8310 bruce@momjian.us 13255 : 720 : appendStringInfoChar(buf, ')');
13256 : :
13257 : : /* Yes, it's correct to put alias after the right paren ... */
9366 tgl@sss.pgh.pa.us 13258 [ + + ]: 940 : if (j->alias != NULL)
13259 : : {
13260 : : /*
13261 : : * Note that it's correct to emit an alias clause if and only if
13262 : : * there was one originally. Otherwise we'd be converting a named
13263 : : * join to unnamed or vice versa, which creates semantic
13264 : : * subtleties we don't want. However, we might print a different
13265 : : * alias name than was there originally.
13266 : : */
13267 : 72 : appendStringInfo(buf, " %s",
2519 13268 : 72 : quote_identifier(get_rtable_name(j->rtindex,
13269 : : context)));
4873 13270 : 72 : get_column_alias_list(colinfo, context);
13271 : : }
13272 : : }
13273 : : else
8318 tgl@sss.pgh.pa.us 13274 [ # # ]:UBC 0 : elog(ERROR, "unrecognized node type: %d",
13275 : : (int) nodeTag(jtnode));
9366 tgl@sss.pgh.pa.us 13276 :CBC 4633 : }
13277 : :
13278 : : /*
13279 : : * get_rte_alias - print the relation's alias, if needed
13280 : : *
13281 : : * If printed, the alias is preceded by a space, or by " AS " if use_as is true.
13282 : : */
13283 : : static void
1173 13284 : 4036 : get_rte_alias(RangeTblEntry *rte, int varno, bool use_as,
13285 : : deparse_context *context)
13286 : : {
13287 : 4036 : deparse_namespace *dpns = (deparse_namespace *) linitial(context->namespaces);
13288 : 4036 : char *refname = get_rtable_name(varno, context);
13289 : 4036 : deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
13290 : 4036 : bool printalias = false;
13291 : :
13292 [ + + ]: 4036 : if (rte->alias != NULL)
13293 : : {
13294 : : /* Always print alias if user provided one */
13295 : 1905 : printalias = true;
13296 : : }
13297 [ + + ]: 2131 : else if (colinfo->printaliases)
13298 : : {
13299 : : /* Always print alias if we need to print column aliases */
13300 : 210 : printalias = true;
13301 : : }
13302 [ + + ]: 1921 : else if (rte->rtekind == RTE_RELATION)
13303 : : {
13304 : : /*
13305 : : * No need to print alias if it's same as relation name (this would
13306 : : * normally be the case, but not if set_rtable_names had to resolve a
13307 : : * conflict).
13308 : : */
13309 [ + + ]: 1745 : if (strcmp(refname, get_relation_name(rte->relid)) != 0)
13310 : 52 : printalias = true;
13311 : : }
13312 [ - + ]: 176 : else if (rte->rtekind == RTE_FUNCTION)
13313 : : {
13314 : : /*
13315 : : * For a function RTE, always print alias. This covers possible
13316 : : * renaming of the function and/or instability of the FigureColname
13317 : : * rules for things that aren't simple functions. Note we'd need to
13318 : : * force it anyway for the columndef list case.
13319 : : */
1173 tgl@sss.pgh.pa.us 13320 :UBC 0 : printalias = true;
13321 : : }
1173 tgl@sss.pgh.pa.us 13322 [ + + ]:CBC 176 : else if (rte->rtekind == RTE_SUBQUERY ||
13323 [ + + ]: 160 : rte->rtekind == RTE_VALUES)
13324 : : {
13325 : : /*
13326 : : * For a subquery, always print alias. This makes the output
13327 : : * SQL-spec-compliant, even though we allow such aliases to be omitted
13328 : : * on input.
13329 : : */
13330 : 24 : printalias = true;
13331 : : }
13332 [ + + ]: 152 : else if (rte->rtekind == RTE_CTE)
13333 : : {
13334 : : /*
13335 : : * No need to print alias if it's same as CTE name (this would
13336 : : * normally be the case, but not if set_rtable_names had to resolve a
13337 : : * conflict).
13338 : : */
13339 [ + + ]: 89 : if (strcmp(refname, rte->ctename) != 0)
13340 : 12 : printalias = true;
13341 : : }
13342 : :
13343 [ + + ]: 4036 : if (printalias)
13344 [ + + ]: 2203 : appendStringInfo(context->buf, "%s%s",
13345 : : use_as ? " AS " : " ",
13346 : : quote_identifier(refname));
13347 : 4036 : }
13348 : :
13349 : : /*
13350 : : * get_for_portion_of - print FOR PORTION OF if needed
13351 : : * XXX: Newlines would help here, at least when pretty-printing. But then the
13352 : : * alias and SET will be on their own line with a leading space.
13353 : : */
13354 : : static void
34 peter@eisentraut.org 13355 :GNC 141 : get_for_portion_of(ForPortionOfExpr *forPortionOf, deparse_context *context)
13356 : : {
13357 [ + + ]: 141 : if (forPortionOf)
13358 : : {
13359 : 16 : appendStringInfo(context->buf, " FOR PORTION OF %s",
13360 : 16 : quote_identifier(forPortionOf->range_name));
13361 : :
13362 : : /*
13363 : : * Try to write it as FROM ... TO ... if we received it that way,
13364 : : * otherwise (targetRange).
13365 : : */
13366 [ + + + - ]: 16 : if (forPortionOf->targetFrom && forPortionOf->targetTo)
13367 : : {
13368 : 8 : appendStringInfoString(context->buf, " FROM ");
13369 : 8 : get_rule_expr(forPortionOf->targetFrom, context, false);
13370 : 8 : appendStringInfoString(context->buf, " TO ");
13371 : 8 : get_rule_expr(forPortionOf->targetTo, context, false);
13372 : : }
13373 : : else
13374 : : {
13375 : 8 : appendStringInfoString(context->buf, " (");
13376 : 8 : get_rule_expr(forPortionOf->targetRange, context, false);
22 drowley@postgresql.o 13377 : 8 : appendStringInfoChar(context->buf, ')');
13378 : : }
13379 : : }
34 peter@eisentraut.org 13380 : 141 : }
13381 : :
13382 : : /*
13383 : : * get_column_alias_list - print column alias list for an RTE
13384 : : *
13385 : : * Caller must already have printed the relation's alias name.
13386 : : */
13387 : : static void
4873 tgl@sss.pgh.pa.us 13388 :CBC 3765 : get_column_alias_list(deparse_columns *colinfo, deparse_context *context)
13389 : : {
7929 13390 : 3765 : StringInfo buf = context->buf;
13391 : : int i;
13392 : 3765 : bool first = true;
13393 : :
13394 : : /* Don't print aliases if not needed */
4873 13395 [ + + ]: 3765 : if (!colinfo->printaliases)
13396 : 2960 : return;
13397 : :
13398 [ + + ]: 6322 : for (i = 0; i < colinfo->num_new_cols; i++)
13399 : : {
13400 : 5517 : char *colname = colinfo->new_colnames[i];
13401 : :
7929 13402 [ + + ]: 5517 : if (first)
13403 : : {
13404 : 805 : appendStringInfoChar(buf, '(');
13405 : 805 : first = false;
13406 : : }
13407 : : else
4569 rhaas@postgresql.org 13408 : 4712 : appendStringInfoString(buf, ", ");
4873 tgl@sss.pgh.pa.us 13409 : 5517 : appendStringInfoString(buf, quote_identifier(colname));
13410 : : }
7929 13411 [ + - ]: 805 : if (!first)
13412 : 805 : appendStringInfoChar(buf, ')');
13413 : : }
13414 : :
13415 : : /*
13416 : : * get_from_clause_coldeflist - reproduce FROM clause coldeflist
13417 : : *
13418 : : * When printing a top-level coldeflist (which is syntactically also the
13419 : : * relation's column alias list), use column names from colinfo. But when
13420 : : * printing a coldeflist embedded inside ROWS FROM(), we prefer to use the
13421 : : * original coldeflist's names, which are available in rtfunc->funccolnames.
13422 : : * Pass NULL for colinfo to select the latter behavior.
13423 : : *
13424 : : * The coldeflist is appended immediately (no space) to buf. Caller is
13425 : : * responsible for ensuring that an alias or AS is present before it.
13426 : : */
13427 : : static void
4548 13428 : 4 : get_from_clause_coldeflist(RangeTblFunction *rtfunc,
13429 : : deparse_columns *colinfo,
13430 : : deparse_context *context)
13431 : : {
8650 13432 : 4 : StringInfo buf = context->buf;
13433 : : ListCell *l1;
13434 : : ListCell *l2;
13435 : : ListCell *l3;
13436 : : ListCell *l4;
13437 : : int i;
13438 : :
13439 : 4 : appendStringInfoChar(buf, '(');
13440 : :
4873 13441 : 4 : i = 0;
2623 13442 [ + - + + : 16 : forfour(l1, rtfunc->funccoltypes,
+ - + + +
- + + + -
+ + + + +
- + - + -
+ + ]
13443 : : l2, rtfunc->funccoltypmods,
13444 : : l3, rtfunc->funccolcollations,
13445 : : l4, rtfunc->funccolnames)
13446 : : {
4873 13447 : 12 : Oid atttypid = lfirst_oid(l1);
13448 : 12 : int32 atttypmod = lfirst_int(l2);
13449 : 12 : Oid attcollation = lfirst_oid(l3);
13450 : : char *attname;
13451 : :
4548 13452 [ - + ]: 12 : if (colinfo)
4548 tgl@sss.pgh.pa.us 13453 :UBC 0 : attname = colinfo->colnames[i];
13454 : : else
4548 tgl@sss.pgh.pa.us 13455 :CBC 12 : attname = strVal(lfirst(l4));
13456 : :
4873 13457 [ - + ]: 12 : Assert(attname); /* shouldn't be any dropped columns here */
13458 : :
8650 13459 [ + + ]: 12 : if (i > 0)
4569 rhaas@postgresql.org 13460 : 8 : appendStringInfoString(buf, ", ");
8650 tgl@sss.pgh.pa.us 13461 : 12 : appendStringInfo(buf, "%s %s",
13462 : : quote_identifier(attname),
13463 : : format_type_with_typemod(atttypid, atttypmod));
5492 13464 [ + + - + ]: 16 : if (OidIsValid(attcollation) &&
13465 : 4 : attcollation != get_typcollation(atttypid))
5521 tgl@sss.pgh.pa.us 13466 :UBC 0 : appendStringInfo(buf, " COLLATE %s",
13467 : : generate_collation_name(attcollation));
13468 : :
8650 tgl@sss.pgh.pa.us 13469 :CBC 12 : i++;
13470 : : }
13471 : :
13472 : 4 : appendStringInfoChar(buf, ')');
13473 : 4 : }
13474 : :
13475 : : /*
13476 : : * get_tablesample_def - print a TableSampleClause
13477 : : */
13478 : : static void
3937 13479 : 18 : get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
13480 : : {
13481 : 18 : StringInfo buf = context->buf;
13482 : : Oid argtypes[1];
13483 : : int nargs;
13484 : : ListCell *l;
13485 : :
13486 : : /*
13487 : : * We should qualify the handler's function name if it wouldn't be
13488 : : * resolved by lookup in the current search path.
13489 : : */
13490 : 18 : argtypes[0] = INTERNALOID;
13491 : 18 : appendStringInfo(buf, " TABLESAMPLE %s (",
13492 : : generate_function_name(tablesample->tsmhandler, 1,
13493 : : NIL, argtypes,
13494 : : false, NULL, false));
13495 : :
13496 : 18 : nargs = 0;
13497 [ + - + + : 36 : foreach(l, tablesample->args)
+ + ]
13498 : : {
13499 [ - + ]: 18 : if (nargs++ > 0)
3937 tgl@sss.pgh.pa.us 13500 :UBC 0 : appendStringInfoString(buf, ", ");
3937 tgl@sss.pgh.pa.us 13501 :CBC 18 : get_rule_expr((Node *) lfirst(l), context, false);
13502 : : }
13503 : 18 : appendStringInfoChar(buf, ')');
13504 : :
13505 [ + + ]: 18 : if (tablesample->repeatable != NULL)
13506 : : {
13507 : 9 : appendStringInfoString(buf, " REPEATABLE (");
13508 : 9 : get_rule_expr((Node *) tablesample->repeatable, context, false);
13509 : 9 : appendStringInfoChar(buf, ')');
13510 : : }
13511 : 18 : }
13512 : :
13513 : : /*
13514 : : * get_opclass_name - fetch name of an index operator class
13515 : : *
13516 : : * The opclass name is appended (after a space) to buf.
13517 : : *
13518 : : * Output is suppressed if the opclass is the default for the given
13519 : : * actual_datatype. (If you don't want this behavior, just pass
13520 : : * InvalidOid for actual_datatype.)
13521 : : */
13522 : : static void
8979 13523 : 7174 : get_opclass_name(Oid opclass, Oid actual_datatype,
13524 : : StringInfo buf)
13525 : : {
13526 : : HeapTuple ht_opc;
13527 : : Form_pg_opclass opcrec;
13528 : : char *opcname;
13529 : : char *nspname;
13530 : :
5924 rhaas@postgresql.org 13531 : 7174 : ht_opc = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
8982 tgl@sss.pgh.pa.us 13532 [ - + ]: 7174 : if (!HeapTupleIsValid(ht_opc))
8982 tgl@sss.pgh.pa.us 13533 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for opclass %u", opclass);
8982 tgl@sss.pgh.pa.us 13534 :CBC 7174 : opcrec = (Form_pg_opclass) GETSTRUCT(ht_opc);
13535 : :
7073 13536 [ + + + + ]: 14322 : if (!OidIsValid(actual_datatype) ||
13537 : 7148 : GetDefaultOpClass(actual_datatype, opcrec->opcmethod) != opclass)
13538 : : {
13539 : : /* Okay, we need the opclass name. Do we need to qualify it? */
8768 13540 : 293 : opcname = NameStr(opcrec->opcname);
7073 13541 [ + - ]: 293 : if (OpclassIsVisible(opclass))
8768 13542 : 293 : appendStringInfo(buf, " %s", quote_identifier(opcname));
13543 : : else
13544 : : {
1743 tgl@sss.pgh.pa.us 13545 :UBC 0 : nspname = get_namespace_name_or_temp(opcrec->opcnamespace);
8768 13546 : 0 : appendStringInfo(buf, " %s.%s",
13547 : : quote_identifier(nspname),
13548 : : quote_identifier(opcname));
13549 : : }
13550 : : }
8982 tgl@sss.pgh.pa.us 13551 :CBC 7174 : ReleaseSysCache(ht_opc);
13552 : 7174 : }
13553 : :
13554 : : /*
13555 : : * generate_opclass_name
13556 : : * Compute the name to display for an opclass specified by OID
13557 : : *
13558 : : * The result includes all necessary quoting and schema-prefixing.
13559 : : */
13560 : : char *
2227 akorotkov@postgresql 13561 : 4 : generate_opclass_name(Oid opclass)
13562 : : {
13563 : : StringInfoData buf;
13564 : :
13565 : 4 : initStringInfo(&buf);
13566 : 4 : get_opclass_name(opclass, InvalidOid, &buf);
13567 : :
2182 tgl@sss.pgh.pa.us 13568 : 4 : return &buf.data[1]; /* get_opclass_name() prepends space */
13569 : : }
13570 : :
13571 : : /*
13572 : : * processIndirection - take care of array and subfield assignment
13573 : : *
13574 : : * We strip any top-level FieldStore or assignment SubscriptingRef nodes that
13575 : : * appear in the input, printing them as decoration for the base column
13576 : : * name (which we assume the caller just printed). We might also need to
13577 : : * strip CoerceToDomain nodes, but only ones that appear above assignment
13578 : : * nodes.
13579 : : *
13580 : : * Returns the subexpression that's to be assigned.
13581 : : */
13582 : : static Node *
3562 13583 : 729 : processIndirection(Node *node, deparse_context *context)
13584 : : {
8000 13585 : 729 : StringInfo buf = context->buf;
3219 13586 : 729 : CoerceToDomain *cdomain = NULL;
13587 : :
13588 : : for (;;)
13589 : : {
8000 13590 [ - + ]: 933 : if (node == NULL)
8000 tgl@sss.pgh.pa.us 13591 :UBC 0 : break;
8000 tgl@sss.pgh.pa.us 13592 [ + + ]:CBC 933 : if (IsA(node, FieldStore))
13593 : : {
13594 : 72 : FieldStore *fstore = (FieldStore *) node;
13595 : : Oid typrelid;
13596 : : char *fieldname;
13597 : :
13598 : : /* lookup tuple type */
13599 : 72 : typrelid = get_typ_typrelid(fstore->resulttype);
13600 [ - + ]: 72 : if (!OidIsValid(typrelid))
8000 tgl@sss.pgh.pa.us 13601 [ # # ]:UBC 0 : elog(ERROR, "argument type %s of FieldStore is not a tuple type",
13602 : : format_type_be(fstore->resulttype));
13603 : :
13604 : : /*
13605 : : * Print the field name. There should only be one target field in
13606 : : * stored rules. There could be more than that in executable
13607 : : * target lists, but this function cannot be used for that case.
13608 : : */
5920 tgl@sss.pgh.pa.us 13609 [ - + ]:CBC 72 : Assert(list_length(fstore->fieldnums) == 1);
3004 alvherre@alvh.no-ip. 13610 : 72 : fieldname = get_attname(typrelid,
13611 : 72 : linitial_int(fstore->fieldnums), false);
3562 tgl@sss.pgh.pa.us 13612 : 72 : appendStringInfo(buf, ".%s", quote_identifier(fieldname));
13613 : :
13614 : : /*
13615 : : * We ignore arg since it should be an uninteresting reference to
13616 : : * the target column or subcolumn.
13617 : : */
8000 13618 : 72 : node = (Node *) linitial(fstore->newvals);
13619 : : }
2650 alvherre@alvh.no-ip. 13620 [ + + ]: 861 : else if (IsA(node, SubscriptingRef))
13621 : : {
13622 : 92 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
13623 : :
13624 [ - + ]: 92 : if (sbsref->refassgnexpr == NULL)
8000 tgl@sss.pgh.pa.us 13625 :UBC 0 : break;
13626 : :
2650 alvherre@alvh.no-ip. 13627 :CBC 92 : printSubscripts(sbsref, context);
13628 : :
13629 : : /*
13630 : : * We ignore refexpr since it should be an uninteresting reference
13631 : : * to the target column or subcolumn.
13632 : : */
13633 : 92 : node = (Node *) sbsref->refassgnexpr;
13634 : : }
3219 tgl@sss.pgh.pa.us 13635 [ + + ]: 769 : else if (IsA(node, CoerceToDomain))
13636 : : {
13637 : 40 : cdomain = (CoerceToDomain *) node;
13638 : : /* If it's an explicit domain coercion, we're done */
13639 [ - + ]: 40 : if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
3219 tgl@sss.pgh.pa.us 13640 :UBC 0 : break;
13641 : : /* Tentatively descend past the CoerceToDomain */
3219 tgl@sss.pgh.pa.us 13642 :CBC 40 : node = (Node *) cdomain->arg;
13643 : : }
13644 : : else
8000 13645 : 729 : break;
13646 : : }
13647 : :
13648 : : /*
13649 : : * If we descended past a CoerceToDomain whose argument turned out not to
13650 : : * be a FieldStore or array assignment, back up to the CoerceToDomain.
13651 : : * (This is not enough to be fully correct if there are nested implicit
13652 : : * CoerceToDomains, but such cases shouldn't ever occur.)
13653 : : */
3219 13654 [ + + - + ]: 729 : if (cdomain && node == (Node *) cdomain->arg)
3219 tgl@sss.pgh.pa.us 13655 :UBC 0 : node = (Node *) cdomain;
13656 : :
8000 tgl@sss.pgh.pa.us 13657 :CBC 729 : return node;
13658 : : }
13659 : :
13660 : : static void
2650 alvherre@alvh.no-ip. 13661 : 316 : printSubscripts(SubscriptingRef *sbsref, deparse_context *context)
13662 : : {
8000 tgl@sss.pgh.pa.us 13663 : 316 : StringInfo buf = context->buf;
13664 : : ListCell *lowlist_item;
13665 : : ListCell *uplist_item;
13666 : :
2650 alvherre@alvh.no-ip. 13667 : 316 : lowlist_item = list_head(sbsref->reflowerindexpr); /* could be NULL */
13668 [ + - + + : 632 : foreach(uplist_item, sbsref->refupperindexpr)
+ + ]
13669 : : {
8000 tgl@sss.pgh.pa.us 13670 : 316 : appendStringInfoChar(buf, '[');
13671 [ - + ]: 316 : if (lowlist_item)
13672 : : {
13673 : : /* If subexpression is NULL, get_rule_expr prints nothing */
8000 tgl@sss.pgh.pa.us 13674 :UBC 0 : get_rule_expr((Node *) lfirst(lowlist_item), context, false);
13675 : 0 : appendStringInfoChar(buf, ':');
2486 13676 : 0 : lowlist_item = lnext(sbsref->reflowerindexpr, lowlist_item);
13677 : : }
13678 : : /* If subexpression is NULL, get_rule_expr prints nothing */
8000 tgl@sss.pgh.pa.us 13679 :CBC 316 : get_rule_expr((Node *) lfirst(uplist_item), context, false);
13680 : 316 : appendStringInfoChar(buf, ']');
13681 : : }
9397 13682 : 316 : }
13683 : :
13684 : : /*
13685 : : * quote_identifier - Quote an identifier only if needed
13686 : : *
13687 : : * When quotes are needed, we palloc the required space; slightly
13688 : : * space-wasteful but well worth it for notational simplicity.
13689 : : */
13690 : : const char *
8776 13691 : 1887433 : quote_identifier(const char *ident)
13692 : : {
13693 : : /*
13694 : : * Can avoid quoting if ident starts with a lowercase letter or underscore
13695 : : * and contains only lowercase letters, digits, and underscores, *and* is
13696 : : * not any SQL keyword. Otherwise, supply quotes.
13697 : : */
8754 13698 : 1887433 : int nquotes = 0;
13699 : : bool safe;
13700 : : const char *ptr;
13701 : : char *result;
13702 : : char *optr;
13703 : :
13704 : : /*
13705 : : * would like to use <ctype.h> macros here, but they might yield unwanted
13706 : : * locale-specific results...
13707 : : */
8773 13708 [ + + - + : 1887433 : safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
+ + ]
13709 : :
8754 13710 [ + + ]: 16781583 : for (ptr = ident; *ptr; ptr++)
13711 : : {
13712 : 14894150 : char ch = *ptr;
13713 : :
13714 [ + + + - : 14894150 : if ((ch >= 'a' && ch <= 'z') ||
+ + ]
13715 [ + + + + ]: 2270539 : (ch >= '0' && ch <= '9') ||
13716 : : (ch == '_'))
13717 : : {
13718 : : /* okay */
13719 : : }
13720 : : else
13721 : : {
13722 : 362636 : safe = false;
13723 [ + + ]: 362636 : if (ch == '"')
13724 : 83 : nquotes++;
13725 : : }
13726 : : }
13727 : :
5766 rhaas@postgresql.org 13728 [ + + ]: 1887433 : if (quote_all_identifiers)
13729 : 7747 : safe = false;
13730 : :
9647 tgl@sss.pgh.pa.us 13731 [ + + ]: 1887433 : if (safe)
13732 : : {
13733 : : /*
13734 : : * Check for keyword. We quote keywords except for unreserved ones.
13735 : : * (In some cases we could avoid quoting a col_name or type_func_name
13736 : : * keyword, but it seems much harder than it's worth to tell that.)
13737 : : *
13738 : : * Note: ScanKeywordLookup() does case-insensitive comparison, but
13739 : : * that's fine, since we already know we have all-lower-case.
13740 : : */
2676 13741 : 1824803 : int kwnum = ScanKeywordLookup(ident, &ScanKeywords);
13742 : :
13743 [ + + + + ]: 1824803 : if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
9647 13744 : 2393 : safe = false;
13745 : : }
13746 : :
9710 13747 [ + + ]: 1887433 : if (safe)
13748 : 1822410 : return ident; /* no change needed */
13749 : :
8754 13750 : 65023 : result = (char *) palloc(strlen(ident) + nquotes + 2 + 1);
13751 : :
13752 : 65023 : optr = result;
13753 : 65023 : *optr++ = '"';
13754 [ + + ]: 519458 : for (ptr = ident; *ptr; ptr++)
13755 : : {
13756 : 454435 : char ch = *ptr;
13757 : :
13758 [ + + ]: 454435 : if (ch == '"')
13759 : 83 : *optr++ = '"';
13760 : 454435 : *optr++ = ch;
13761 : : }
13762 : 65023 : *optr++ = '"';
13763 : 65023 : *optr = '\0';
13764 : :
9710 13765 : 65023 : return result;
13766 : : }
13767 : :
13768 : : /*
13769 : : * quote_qualified_identifier - Quote a possibly-qualified identifier
13770 : : *
13771 : : * Return a name of the form qualifier.ident, or just ident if qualifier
13772 : : * is NULL, quoting each component if necessary. The result is palloc'd.
13773 : : */
13774 : : char *
6137 peter_e@gmx.net 13775 : 740856 : quote_qualified_identifier(const char *qualifier,
13776 : : const char *ident)
13777 : : {
13778 : : StringInfoData buf;
13779 : :
8776 tgl@sss.pgh.pa.us 13780 : 740856 : initStringInfo(&buf);
6137 peter_e@gmx.net 13781 [ + + ]: 740856 : if (qualifier)
13782 : 258281 : appendStringInfo(&buf, "%s.", quote_identifier(qualifier));
8130 neilc@samurai.com 13783 : 740856 : appendStringInfoString(&buf, quote_identifier(ident));
8776 tgl@sss.pgh.pa.us 13784 : 740856 : return buf.data;
13785 : : }
13786 : :
13787 : : /*
13788 : : * get_relation_name
13789 : : * Get the unqualified name of a relation specified by OID
13790 : : *
13791 : : * This differs from the underlying get_rel_name() function in that it will
13792 : : * throw error instead of silently returning NULL if the OID is bad.
13793 : : */
13794 : : static char *
5663 13795 : 10435 : get_relation_name(Oid relid)
13796 : : {
13797 : 10435 : char *relname = get_rel_name(relid);
13798 : :
13799 [ - + ]: 10435 : if (!relname)
5663 tgl@sss.pgh.pa.us 13800 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relid);
5663 tgl@sss.pgh.pa.us 13801 :CBC 10435 : return relname;
13802 : : }
13803 : :
13804 : : /*
13805 : : * generate_relation_name
13806 : : * Compute the name to display for a relation specified by OID
13807 : : *
13808 : : * The result includes all necessary quoting and schema-prefixing.
13809 : : *
13810 : : * If namespaces isn't NIL, it must be a list of deparse_namespace nodes.
13811 : : * We will forcibly qualify the relation name if it equals any CTE name
13812 : : * visible in the namespace list.
13813 : : */
13814 : : static char *
6420 13815 : 5451 : generate_relation_name(Oid relid, List *namespaces)
13816 : : {
13817 : : HeapTuple tp;
13818 : : Form_pg_class reltup;
13819 : : bool need_qual;
13820 : : ListCell *nslist;
13821 : : char *relname;
13822 : : char *nspname;
13823 : : char *result;
13824 : :
5924 rhaas@postgresql.org 13825 : 5451 : tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
8768 tgl@sss.pgh.pa.us 13826 [ - + ]: 5451 : if (!HeapTupleIsValid(tp))
8318 tgl@sss.pgh.pa.us 13827 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relid);
8768 tgl@sss.pgh.pa.us 13828 :CBC 5451 : reltup = (Form_pg_class) GETSTRUCT(tp);
6420 13829 : 5451 : relname = NameStr(reltup->relname);
13830 : :
13831 : : /* Check for conflicting CTE name */
13832 : 5451 : need_qual = false;
13833 [ + + + + : 8964 : foreach(nslist, namespaces)
+ + ]
13834 : : {
13835 : 3513 : deparse_namespace *dpns = (deparse_namespace *) lfirst(nslist);
13836 : : ListCell *ctlist;
13837 : :
13838 [ + + + + : 3601 : foreach(ctlist, dpns->ctes)
+ + ]
13839 : : {
13840 : 88 : CommonTableExpr *cte = (CommonTableExpr *) lfirst(ctlist);
13841 : :
13842 [ - + ]: 88 : if (strcmp(cte->ctename, relname) == 0)
13843 : : {
6420 tgl@sss.pgh.pa.us 13844 :UBC 0 : need_qual = true;
13845 : 0 : break;
13846 : : }
13847 : : }
6420 tgl@sss.pgh.pa.us 13848 [ - + ]:CBC 3513 : if (need_qual)
6420 tgl@sss.pgh.pa.us 13849 :UBC 0 : break;
13850 : : }
13851 : :
13852 : : /* Otherwise, qualify the name if not visible in search path */
6420 tgl@sss.pgh.pa.us 13853 [ + - ]:CBC 5451 : if (!need_qual)
13854 : 5451 : need_qual = !RelationIsVisible(relid);
13855 : :
13856 [ + + ]: 5451 : if (need_qual)
1743 13857 : 1622 : nspname = get_namespace_name_or_temp(reltup->relnamespace);
13858 : : else
6420 13859 : 3829 : nspname = NULL;
13860 : :
13861 : 5451 : result = quote_qualified_identifier(nspname, relname);
13862 : :
8768 13863 : 5451 : ReleaseSysCache(tp);
13864 : :
13865 : 5451 : return result;
13866 : : }
13867 : :
13868 : : /*
13869 : : * generate_qualified_relation_name
13870 : : * Compute the name to display for a relation specified by OID
13871 : : *
13872 : : * As above, but unconditionally schema-qualify the name.
13873 : : */
13874 : : static char *
3819 13875 : 4568 : generate_qualified_relation_name(Oid relid)
13876 : : {
13877 : : HeapTuple tp;
13878 : : Form_pg_class reltup;
13879 : : char *relname;
13880 : : char *nspname;
13881 : : char *result;
13882 : :
13883 : 4568 : tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
13884 [ - + ]: 4568 : if (!HeapTupleIsValid(tp))
3819 tgl@sss.pgh.pa.us 13885 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relid);
3819 tgl@sss.pgh.pa.us 13886 :CBC 4568 : reltup = (Form_pg_class) GETSTRUCT(tp);
13887 : 4568 : relname = NameStr(reltup->relname);
13888 : :
1743 13889 : 4568 : nspname = get_namespace_name_or_temp(reltup->relnamespace);
3819 13890 [ - + ]: 4568 : if (!nspname)
3819 tgl@sss.pgh.pa.us 13891 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for namespace %u",
13892 : : reltup->relnamespace);
13893 : :
3819 tgl@sss.pgh.pa.us 13894 :CBC 4568 : result = quote_qualified_identifier(nspname, relname);
13895 : :
13896 : 4568 : ReleaseSysCache(tp);
13897 : :
13898 : 4568 : return result;
13899 : : }
13900 : :
13901 : : /*
13902 : : * generate_function_name
13903 : : * Compute the name to display for a function specified by OID,
13904 : : * given that it is being called with the specified actual arg names and
13905 : : * types. (Those matter because of ambiguous-function resolution rules.)
13906 : : *
13907 : : * If we're dealing with a potentially variadic function (in practice, this
13908 : : * means a FuncExpr or Aggref, not some other way of calling a function), then
13909 : : * has_variadic must specify whether variadic arguments have been merged,
13910 : : * and *use_variadic_p will be set to indicate whether to print VARIADIC in
13911 : : * the output. For non-FuncExpr cases, has_variadic should be false and
13912 : : * use_variadic_p can be NULL.
13913 : : *
13914 : : * inGroupBy must be true if we're deparsing a GROUP BY clause.
13915 : : *
13916 : : * The result includes all necessary quoting and schema-prefixing.
13917 : : */
13918 : : static char *
4852 13919 : 9680 : generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes,
13920 : : bool has_variadic, bool *use_variadic_p,
13921 : : bool inGroupBy)
13922 : : {
13923 : : char *result;
13924 : : HeapTuple proctup;
13925 : : Form_pg_proc procform;
13926 : : char *proname;
13927 : : bool use_variadic;
13928 : : char *nspname;
13929 : : FuncDetailCode p_result;
13930 : : int fgc_flags;
13931 : : Oid p_funcid;
13932 : : Oid p_rettype;
13933 : : bool p_retset;
13934 : : int p_nvargs;
13935 : : Oid p_vatype;
13936 : : Oid *p_true_typeids;
4007 andres@anarazel.de 13937 : 9680 : bool force_qualify = false;
13938 : :
5924 rhaas@postgresql.org 13939 : 9680 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
8768 tgl@sss.pgh.pa.us 13940 [ - + ]: 9680 : if (!HeapTupleIsValid(proctup))
8318 tgl@sss.pgh.pa.us 13941 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
8768 tgl@sss.pgh.pa.us 13942 :CBC 9680 : procform = (Form_pg_proc) GETSTRUCT(proctup);
13943 : 9680 : proname = NameStr(procform->proname);
13944 : :
13945 : : /*
13946 : : * Due to parser hacks to avoid needing to reserve CUBE, we need to force
13947 : : * qualification of some function names within GROUP BY.
13948 : : */
614 13949 [ - + ]: 9680 : if (inGroupBy)
13950 : : {
4007 andres@anarazel.de 13951 [ # # # # ]:UBC 0 : if (strcmp(proname, "cube") == 0 || strcmp(proname, "rollup") == 0)
13952 : 0 : force_qualify = true;
13953 : : }
13954 : :
13955 : : /*
13956 : : * Determine whether VARIADIC should be printed. We must do this first
13957 : : * since it affects the lookup rules in func_get_detail().
13958 : : *
13959 : : * We always print VARIADIC if the function has a merged variadic-array
13960 : : * argument. Note that this is always the case for functions taking a
13961 : : * VARIADIC argument type other than VARIADIC ANY. If we omitted VARIADIC
13962 : : * and printed the array elements as separate arguments, the call could
13963 : : * match a newer non-VARIADIC function.
13964 : : */
4852 tgl@sss.pgh.pa.us 13965 [ + + ]:CBC 9680 : if (use_variadic_p)
13966 : : {
13967 : : /* Parser should not have set funcvariadic unless fn is variadic */
4415 13968 [ + + - + ]: 8703 : Assert(!has_variadic || OidIsValid(procform->provariadic));
13969 : 8703 : use_variadic = has_variadic;
4852 13970 : 8703 : *use_variadic_p = use_variadic;
13971 : : }
13972 : : else
13973 : : {
4415 13974 [ - + ]: 977 : Assert(!has_variadic);
4852 13975 : 977 : use_variadic = false;
13976 : : }
13977 : :
13978 : : /*
13979 : : * The idea here is to schema-qualify only if the parser would fail to
13980 : : * resolve the correct function given the unqualified func name with the
13981 : : * specified argtypes and VARIADIC flag. But if we already decided to
13982 : : * force qualification, then we can skip the lookup and pretend we didn't
13983 : : * find it.
13984 : : */
4007 andres@anarazel.de 13985 [ + - ]: 9680 : if (!force_qualify)
13986 : 9680 : p_result = func_get_detail(list_make1(makeString(proname)),
13987 : : NIL, argnames, nargs, argtypes,
1790 tgl@sss.pgh.pa.us 13988 : 9680 : !use_variadic, true, false,
13989 : : &fgc_flags,
13990 : : &p_funcid, &p_rettype,
13991 : : &p_retset, &p_nvargs, &p_vatype,
4007 andres@anarazel.de 13992 : 9680 : &p_true_typeids, NULL);
13993 : : else
13994 : : {
4007 andres@anarazel.de 13995 :UBC 0 : p_result = FUNCDETAIL_NOTFOUND;
13996 : 0 : p_funcid = InvalidOid;
13997 : : }
13998 : :
6337 tgl@sss.pgh.pa.us 13999 [ + + + + ]:CBC 9680 : if ((p_result == FUNCDETAIL_NORMAL ||
14000 [ + + ]: 664 : p_result == FUNCDETAIL_AGGREGATE ||
14001 : 9100 : p_result == FUNCDETAIL_WINDOWFUNC) &&
8341 14002 [ + - ]: 9100 : p_funcid == funcid)
8768 14003 : 9100 : nspname = NULL;
14004 : : else
1743 14005 : 580 : nspname = get_namespace_name_or_temp(procform->pronamespace);
14006 : :
8768 14007 : 9680 : result = quote_qualified_identifier(nspname, proname);
14008 : :
14009 : 9680 : ReleaseSysCache(proctup);
14010 : :
14011 : 9680 : return result;
14012 : : }
14013 : :
14014 : : /*
14015 : : * generate_operator_name
14016 : : * Compute the name to display for an operator specified by OID,
14017 : : * given that it is being called with the specified actual arg types.
14018 : : * (Arg types matter because of ambiguous-operator resolution rules.
14019 : : * Pass InvalidOid for unused arg of a unary operator.)
14020 : : *
14021 : : * The result includes all necessary quoting and schema-prefixing,
14022 : : * plus the OPERATOR() decoration needed to use a qualified operator name
14023 : : * in an expression.
14024 : : */
14025 : : static char *
14026 : 43046 : generate_operator_name(Oid operid, Oid arg1, Oid arg2)
14027 : : {
14028 : : StringInfoData buf;
14029 : : HeapTuple opertup;
14030 : : Form_pg_operator operform;
14031 : : char *oprname;
14032 : : char *nspname;
14033 : : Operator p_result;
14034 : :
14035 : 43046 : initStringInfo(&buf);
14036 : :
5924 rhaas@postgresql.org 14037 : 43046 : opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operid));
8768 tgl@sss.pgh.pa.us 14038 [ - + ]: 43046 : if (!HeapTupleIsValid(opertup))
8318 tgl@sss.pgh.pa.us 14039 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for operator %u", operid);
8768 tgl@sss.pgh.pa.us 14040 :CBC 43046 : operform = (Form_pg_operator) GETSTRUCT(opertup);
14041 : 43046 : oprname = NameStr(operform->oprname);
14042 : :
14043 : : /*
14044 : : * The idea here is to schema-qualify only if the parser would fail to
14045 : : * resolve the correct operator given the unqualified op name with the
14046 : : * specified argtypes.
14047 : : */
14048 [ + + - ]: 43046 : switch (operform->oprkind)
14049 : : {
14050 : 43026 : case 'b':
7357 14051 : 43026 : p_result = oper(NULL, list_make1(makeString(oprname)), arg1, arg2,
14052 : : true, -1);
8768 14053 : 43026 : break;
14054 : 20 : case 'l':
7357 14055 : 20 : p_result = left_oper(NULL, list_make1(makeString(oprname)), arg2,
14056 : : true, -1);
8768 14057 : 20 : break;
8768 tgl@sss.pgh.pa.us 14058 :UBC 0 : default:
8318 14059 [ # # ]: 0 : elog(ERROR, "unrecognized oprkind: %d", operform->oprkind);
14060 : : p_result = NULL; /* keep compiler quiet */
14061 : : break;
14062 : : }
14063 : :
8768 tgl@sss.pgh.pa.us 14064 [ + + + - ]:CBC 43046 : if (p_result != NULL && oprid(p_result) == operid)
14065 : 43041 : nspname = NULL;
14066 : : else
14067 : : {
1743 14068 : 5 : nspname = get_namespace_name_or_temp(operform->oprnamespace);
8768 14069 : 5 : appendStringInfo(&buf, "OPERATOR(%s.", quote_identifier(nspname));
14070 : : }
14071 : :
8130 neilc@samurai.com 14072 : 43046 : appendStringInfoString(&buf, oprname);
14073 : :
8768 tgl@sss.pgh.pa.us 14074 [ + + ]: 43046 : if (nspname)
14075 : 5 : appendStringInfoChar(&buf, ')');
14076 : :
14077 [ + + ]: 43046 : if (p_result != NULL)
14078 : 43041 : ReleaseSysCache(p_result);
14079 : :
14080 : 43046 : ReleaseSysCache(opertup);
14081 : :
14082 : 43046 : return buf.data;
14083 : : }
14084 : :
14085 : : /*
14086 : : * generate_operator_clause --- generate a binary-operator WHERE clause
14087 : : *
14088 : : * This is used for internally-generated-and-executed SQL queries, where
14089 : : * precision is essential and readability is secondary. The basic
14090 : : * requirement is to append "leftop op rightop" to buf, where leftop and
14091 : : * rightop are given as strings and are assumed to yield types leftoptype
14092 : : * and rightoptype; the operator is identified by OID. The complexity
14093 : : * comes from needing to be sure that the parser will select the desired
14094 : : * operator when the query is parsed. We always name the operator using
14095 : : * OPERATOR(schema.op) syntax, so as to avoid search-path uncertainties.
14096 : : * We have to emit casts too, if either input isn't already the input type
14097 : : * of the operator; else we are at the mercy of the parser's heuristics for
14098 : : * ambiguous-operator resolution. The caller must ensure that leftop and
14099 : : * rightop are suitable arguments for a cast operation; it's best to insert
14100 : : * parentheses if they aren't just variables or parameters.
14101 : : */
14102 : : void
2969 14103 : 3138 : generate_operator_clause(StringInfo buf,
14104 : : const char *leftop, Oid leftoptype,
14105 : : Oid opoid,
14106 : : const char *rightop, Oid rightoptype)
14107 : : {
14108 : : HeapTuple opertup;
14109 : : Form_pg_operator operform;
14110 : : char *oprname;
14111 : : char *nspname;
14112 : :
14113 : 3138 : opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opoid));
14114 [ - + ]: 3138 : if (!HeapTupleIsValid(opertup))
2969 tgl@sss.pgh.pa.us 14115 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for operator %u", opoid);
2969 tgl@sss.pgh.pa.us 14116 :CBC 3138 : operform = (Form_pg_operator) GETSTRUCT(opertup);
14117 [ - + ]: 3138 : Assert(operform->oprkind == 'b');
14118 : 3138 : oprname = NameStr(operform->oprname);
14119 : :
14120 : 3138 : nspname = get_namespace_name(operform->oprnamespace);
14121 : :
14122 : 3138 : appendStringInfoString(buf, leftop);
14123 [ + + ]: 3138 : if (leftoptype != operform->oprleft)
14124 : 764 : add_cast_to(buf, operform->oprleft);
14125 : 3138 : appendStringInfo(buf, " OPERATOR(%s.", quote_identifier(nspname));
14126 : 3138 : appendStringInfoString(buf, oprname);
14127 : 3138 : appendStringInfo(buf, ") %s", rightop);
14128 [ + + ]: 3138 : if (rightoptype != operform->oprright)
14129 : 611 : add_cast_to(buf, operform->oprright);
14130 : :
14131 : 3138 : ReleaseSysCache(opertup);
14132 : 3138 : }
14133 : :
14134 : : /*
14135 : : * Add a cast specification to buf. We spell out the type name the hard way,
14136 : : * intentionally not using format_type_be(). This is to avoid corner cases
14137 : : * for CHARACTER, BIT, and perhaps other types, where specifying the type
14138 : : * using SQL-standard syntax results in undesirable data truncation. By
14139 : : * doing it this way we can be certain that the cast will have default (-1)
14140 : : * target typmod.
14141 : : */
14142 : : static void
14143 : 1375 : add_cast_to(StringInfo buf, Oid typid)
14144 : : {
14145 : : HeapTuple typetup;
14146 : : Form_pg_type typform;
14147 : : char *typname;
14148 : : char *nspname;
14149 : :
14150 : 1375 : typetup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
14151 [ - + ]: 1375 : if (!HeapTupleIsValid(typetup))
2969 tgl@sss.pgh.pa.us 14152 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", typid);
2969 tgl@sss.pgh.pa.us 14153 :CBC 1375 : typform = (Form_pg_type) GETSTRUCT(typetup);
14154 : :
14155 : 1375 : typname = NameStr(typform->typname);
1743 14156 : 1375 : nspname = get_namespace_name_or_temp(typform->typnamespace);
14157 : :
2969 14158 : 1375 : appendStringInfo(buf, "::%s.%s",
14159 : : quote_identifier(nspname), quote_identifier(typname));
14160 : :
14161 : 1375 : ReleaseSysCache(typetup);
14162 : 1375 : }
14163 : :
14164 : : /*
14165 : : * generate_qualified_type_name
14166 : : * Compute the name to display for a type specified by OID
14167 : : *
14168 : : * This is different from format_type_be() in that we unconditionally
14169 : : * schema-qualify the name. That also means no special syntax for
14170 : : * SQL-standard type names ... although in current usage, this should
14171 : : * only get used for domains, so such cases wouldn't occur anyway.
14172 : : */
14173 : : static char *
3107 14174 : 9 : generate_qualified_type_name(Oid typid)
14175 : : {
14176 : : HeapTuple tp;
14177 : : Form_pg_type typtup;
14178 : : char *typname;
14179 : : char *nspname;
14180 : : char *result;
14181 : :
14182 : 9 : tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
14183 [ - + ]: 9 : if (!HeapTupleIsValid(tp))
3107 tgl@sss.pgh.pa.us 14184 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", typid);
3107 tgl@sss.pgh.pa.us 14185 :CBC 9 : typtup = (Form_pg_type) GETSTRUCT(tp);
14186 : 9 : typname = NameStr(typtup->typname);
14187 : :
1743 14188 : 9 : nspname = get_namespace_name_or_temp(typtup->typnamespace);
3107 14189 [ - + ]: 9 : if (!nspname)
3107 tgl@sss.pgh.pa.us 14190 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for namespace %u",
14191 : : typtup->typnamespace);
14192 : :
3107 tgl@sss.pgh.pa.us 14193 :CBC 9 : result = quote_qualified_identifier(nspname, typname);
14194 : :
14195 : 9 : ReleaseSysCache(tp);
14196 : :
14197 : 9 : return result;
14198 : : }
14199 : :
14200 : : /*
14201 : : * generate_collation_name
14202 : : * Compute the name to display for a collation specified by OID
14203 : : *
14204 : : * The result includes all necessary quoting and schema-prefixing.
14205 : : */
14206 : : char *
5565 peter_e@gmx.net 14207 : 304 : generate_collation_name(Oid collid)
14208 : : {
14209 : : HeapTuple tp;
14210 : : Form_pg_collation colltup;
14211 : : char *collname;
14212 : : char *nspname;
14213 : : char *result;
14214 : :
14215 : 304 : tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
14216 [ - + ]: 304 : if (!HeapTupleIsValid(tp))
5565 peter_e@gmx.net 14217 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for collation %u", collid);
5565 peter_e@gmx.net 14218 :CBC 304 : colltup = (Form_pg_collation) GETSTRUCT(tp);
14219 : 304 : collname = NameStr(colltup->collname);
14220 : :
14221 [ - + ]: 304 : if (!CollationIsVisible(collid))
1743 tgl@sss.pgh.pa.us 14222 :UBC 0 : nspname = get_namespace_name_or_temp(colltup->collnamespace);
14223 : : else
5565 peter_e@gmx.net 14224 :CBC 304 : nspname = NULL;
14225 : :
14226 : 304 : result = quote_qualified_identifier(nspname, collname);
14227 : :
14228 : 304 : ReleaseSysCache(tp);
14229 : :
14230 : 304 : return result;
14231 : : }
14232 : :
14233 : : /*
14234 : : * Given a C string, produce a TEXT datum.
14235 : : *
14236 : : * We assume that the input was palloc'd and may be freed.
14237 : : */
14238 : : static text *
8035 tgl@sss.pgh.pa.us 14239 : 26157 : string_to_text(char *str)
14240 : : {
14241 : : text *result;
14242 : :
6615 14243 : 26157 : result = cstring_to_text(str);
8035 14244 : 26157 : pfree(str);
14245 : 26157 : return result;
14246 : : }
14247 : :
14248 : : /*
14249 : : * Generate a C string representing a relation options from text[] datum.
14250 : : */
14251 : : void
2227 akorotkov@postgresql 14252 : 131 : get_reloptions(StringInfo buf, Datum reloptions)
14253 : : {
14254 : : Datum *options;
14255 : : int noptions;
14256 : : int i;
14257 : :
1404 peter@eisentraut.org 14258 : 131 : deconstruct_array_builtin(DatumGetArrayTypeP(reloptions), TEXTOID,
14259 : : &options, NULL, &noptions);
14260 : :
2227 akorotkov@postgresql 14261 [ + + ]: 284 : for (i = 0; i < noptions; i++)
14262 : : {
14263 : 153 : char *option = TextDatumGetCString(options[i]);
14264 : : char *name;
14265 : : char *separator;
14266 : : char *value;
14267 : :
14268 : : /*
14269 : : * Each array element should have the form name=value. If the "=" is
14270 : : * missing for some reason, treat it like an empty value.
14271 : : */
14272 : 153 : name = option;
14273 : 153 : separator = strchr(option, '=');
14274 [ + - ]: 153 : if (separator)
14275 : : {
14276 : 153 : *separator = '\0';
14277 : 153 : value = separator + 1;
14278 : : }
14279 : : else
2227 akorotkov@postgresql 14280 :UBC 0 : value = "";
14281 : :
2227 akorotkov@postgresql 14282 [ + + ]:CBC 153 : if (i > 0)
14283 : 22 : appendStringInfoString(buf, ", ");
14284 : 153 : appendStringInfo(buf, "%s=", quote_identifier(name));
14285 : :
14286 : : /*
14287 : : * In general we need to quote the value; but to avoid unnecessary
14288 : : * clutter, do not quote if it is an identifier that would not need
14289 : : * quoting. (We could also allow numbers, but that is a bit trickier
14290 : : * than it looks --- for example, are leading zeroes significant? We
14291 : : * don't want to assume very much here about what custom reloptions
14292 : : * might mean.)
14293 : : */
14294 [ + + ]: 153 : if (quote_identifier(value) == value)
14295 : 4 : appendStringInfoString(buf, value);
14296 : : else
14297 : 149 : simple_quote_literal(buf, value);
14298 : :
14299 : 153 : pfree(option);
14300 : : }
14301 : 131 : }
14302 : :
14303 : : /*
14304 : : * Generate a C string representing a relation's reloptions, or NULL if none.
14305 : : */
14306 : : static char *
7247 bruce@momjian.us 14307 : 4498 : flatten_reloptions(Oid relid)
14308 : : {
14309 : 4498 : char *result = NULL;
14310 : : HeapTuple tuple;
14311 : : Datum reloptions;
14312 : : bool isnull;
14313 : :
5924 rhaas@postgresql.org 14314 : 4498 : tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
7246 tgl@sss.pgh.pa.us 14315 [ - + ]: 4498 : if (!HeapTupleIsValid(tuple))
7246 tgl@sss.pgh.pa.us 14316 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relid);
14317 : :
7246 tgl@sss.pgh.pa.us 14318 :CBC 4498 : reloptions = SysCacheGetAttr(RELOID, tuple,
14319 : : Anum_pg_class_reloptions, &isnull);
14320 [ + + ]: 4498 : if (!isnull)
14321 : : {
14322 : : StringInfoData buf;
14323 : :
3777 14324 : 105 : initStringInfo(&buf);
2227 akorotkov@postgresql 14325 : 105 : get_reloptions(&buf, reloptions);
14326 : :
3777 tgl@sss.pgh.pa.us 14327 : 105 : result = buf.data;
14328 : : }
14329 : :
7246 14330 : 4498 : ReleaseSysCache(tuple);
14331 : :
7247 bruce@momjian.us 14332 : 4498 : return result;
14333 : : }
14334 : :
14335 : : /*
14336 : : * get_range_partbound_string
14337 : : * A C string representation of one range partition bound
14338 : : */
14339 : : char *
3190 rhaas@postgresql.org 14340 : 3142 : get_range_partbound_string(List *bound_datums)
14341 : : {
14342 : : deparse_context context;
14343 : : StringInfoData buf;
14344 : : ListCell *cell;
14345 : : char *sep;
14346 : :
180 drowley@postgresql.o 14347 :GNC 3142 : initStringInfo(&buf);
3190 rhaas@postgresql.org 14348 :CBC 3142 : memset(&context, 0, sizeof(deparse_context));
180 drowley@postgresql.o 14349 :GNC 3142 : context.buf = &buf;
14350 : :
14351 : 3142 : appendStringInfoChar(&buf, '(');
3190 rhaas@postgresql.org 14352 :CBC 3142 : sep = "";
14353 [ + - + + : 6762 : foreach(cell, bound_datums)
+ + ]
14354 : : {
14355 : : PartitionRangeDatum *datum =
1082 tgl@sss.pgh.pa.us 14356 : 3620 : lfirst_node(PartitionRangeDatum, cell);
14357 : :
180 drowley@postgresql.o 14358 :GNC 3620 : appendStringInfoString(&buf, sep);
3190 rhaas@postgresql.org 14359 [ + + ]:CBC 3620 : if (datum->kind == PARTITION_RANGE_DATUM_MINVALUE)
180 drowley@postgresql.o 14360 :GNC 148 : appendStringInfoString(&buf, "MINVALUE");
3190 rhaas@postgresql.org 14361 [ + + ]:CBC 3472 : else if (datum->kind == PARTITION_RANGE_DATUM_MAXVALUE)
180 drowley@postgresql.o 14362 :GNC 80 : appendStringInfoString(&buf, "MAXVALUE");
14363 : : else
14364 : : {
3190 rhaas@postgresql.org 14365 :CBC 3392 : Const *val = castNode(Const, datum->value);
14366 : :
14367 : 3392 : get_const_expr(val, &context, -1);
14368 : : }
14369 : 3620 : sep = ", ";
14370 : : }
180 drowley@postgresql.o 14371 :GNC 3142 : appendStringInfoChar(&buf, ')');
14372 : :
14373 : 3142 : return buf.data;
14374 : : }
|