1 /*-------------------------------------------------------------------------
4 * Functions to convert stored expressions/querytrees back to
7 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/utils/adt/ruleutils.c
14 *-------------------------------------------------------------------------
21 #include "access/genam.h"
22 #include "access/sysattr.h"
23 #include "catalog/dependency.h"
24 #include "catalog/indexing.h"
25 #include "catalog/pg_authid.h"
26 #include "catalog/pg_collation.h"
27 #include "catalog/pg_constraint.h"
28 #include "catalog/pg_depend.h"
29 #include "catalog/pg_language.h"
30 #include "catalog/pg_opclass.h"
31 #include "catalog/pg_operator.h"
32 #include "catalog/pg_proc.h"
33 #include "catalog/pg_trigger.h"
34 #include "catalog/pg_type.h"
35 #include "commands/defrem.h"
36 #include "commands/tablespace.h"
37 #include "executor/spi.h"
39 #include "nodes/makefuncs.h"
40 #include "nodes/nodeFuncs.h"
41 #include "optimizer/clauses.h"
42 #include "optimizer/tlist.h"
43 #include "parser/keywords.h"
44 #include "parser/parse_func.h"
45 #include "parser/parse_oper.h"
46 #include "parser/parser.h"
47 #include "parser/parsetree.h"
48 #include "rewrite/rewriteHandler.h"
49 #include "rewrite/rewriteManip.h"
50 #include "rewrite/rewriteSupport.h"
51 #include "utils/array.h"
52 #include "utils/builtins.h"
53 #include "utils/fmgroids.h"
54 #include "utils/lsyscache.h"
55 #include "utils/tqual.h"
56 #include "utils/syscache.h"
57 #include "utils/typcache.h"
58 #include "utils/xml.h"
62 * Pretty formatting constants
67 #define PRETTYINDENT_STD 8
68 #define PRETTYINDENT_JOIN 13
69 #define PRETTYINDENT_JOIN_ON (PRETTYINDENT_JOIN-PRETTYINDENT_STD)
70 #define PRETTYINDENT_VAR 4
73 #define PRETTYFLAG_PAREN 1
74 #define PRETTYFLAG_INDENT 2
76 /* macro to test if pretty action needed */
77 #define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)
78 #define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)
86 /* Context info needed for invoking a recursive querytree display routine */
89 StringInfo buf; /* output buffer to append to */
90 List *namespaces; /* List of deparse_namespace nodes */
91 List *windowClause; /* Current query level's WINDOW clause */
92 List *windowTList; /* targetlist for resolving WINDOW clause */
93 int prettyFlags; /* enabling of pretty-print functions */
94 int indentLevel; /* current indent level for prettyprint */
95 bool varprefix; /* TRUE to print prefixes on Vars */
99 * Each level of query context around a subtree needs a level of Var namespace.
100 * A Var having varlevelsup=N refers to the N'th item (counting from 0) in
101 * the current context's namespaces list.
103 * The rangetable is the list of actual RTEs from the query tree, and the
104 * cte list is the list of actual CTEs.
106 * When deparsing plan trees, there is always just a single item in the
107 * deparse_namespace list (since a plan tree never contains Vars with
108 * varlevelsup > 0). We store the PlanState node that is the immediate
109 * parent of the expression to be deparsed, as well as a list of that
110 * PlanState's ancestors. In addition, we store the outer and inner
111 * subplan nodes, whose targetlists are used to resolve OUTER and INNER Vars.
112 * (Note: these could be derived on-the-fly from the planstate instead.)
116 List *rtable; /* List of RangeTblEntry nodes */
117 List *ctes; /* List of CommonTableExpr nodes */
118 /* Remaining fields are used only when deparsing a Plan tree: */
119 PlanState *planstate; /* immediate parent of current expression */
120 List *ancestors; /* ancestors of planstate */
121 PlanState *outer_planstate; /* OUTER subplan state, or NULL if none */
122 PlanState *inner_planstate; /* INNER subplan state, or NULL if none */
123 Plan *outer_plan; /* OUTER subplan, or NULL if none */
124 Plan *inner_plan; /* INNER subplan, or NULL if none */
132 static SPIPlanPtr plan_getrulebyoid = NULL;
133 static const char *query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1";
134 static SPIPlanPtr plan_getviewrule = NULL;
135 static const char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
138 bool quote_all_identifiers = false;
144 * Most of these functions used to use fixed-size buffers to build their
145 * results. Now, they take an (already initialized) StringInfo object
146 * as a parameter, and append their text output to its contents.
149 static char *deparse_expression_pretty(Node *expr, List *dpcontext,
150 bool forceprefix, bool showimplicit,
151 int prettyFlags, int startIndent);
152 static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags);
153 static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
154 static void decompile_column_index_array(Datum column_index_array, Oid relId,
156 static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
157 static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
158 const Oid *excludeOps,
159 bool attrsOnly, bool showTblSpc,
161 static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
163 static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname,
165 static int print_function_arguments(StringInfo buf, HeapTuple proctup,
166 bool print_table_args, bool print_defaults);
167 static void print_function_rettype(StringInfo buf, HeapTuple proctup);
168 static void set_deparse_planstate(deparse_namespace *dpns, PlanState *ps);
169 static void push_child_plan(deparse_namespace *dpns, PlanState *ps,
170 deparse_namespace *save_dpns);
171 static void pop_child_plan(deparse_namespace *dpns,
172 deparse_namespace *save_dpns);
173 static void push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
174 deparse_namespace *save_dpns);
175 static void pop_ancestor_plan(deparse_namespace *dpns,
176 deparse_namespace *save_dpns);
177 static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
179 static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
181 static void get_query_def(Query *query, StringInfo buf, List *parentnamespace,
182 TupleDesc resultDesc, int prettyFlags, int startIndent);
183 static void get_values_def(List *values_lists, deparse_context *context);
184 static void get_with_clause(Query *query, deparse_context *context);
185 static void get_select_query_def(Query *query, deparse_context *context,
186 TupleDesc resultDesc);
187 static void get_insert_query_def(Query *query, deparse_context *context);
188 static void get_update_query_def(Query *query, deparse_context *context);
189 static void get_delete_query_def(Query *query, deparse_context *context);
190 static void get_utility_query_def(Query *query, deparse_context *context);
191 static void get_basic_select_query(Query *query, deparse_context *context,
192 TupleDesc resultDesc);
193 static void get_target_list(List *targetList, deparse_context *context,
194 TupleDesc resultDesc);
195 static void get_setop_query(Node *setOp, Query *query,
196 deparse_context *context,
197 TupleDesc resultDesc);
198 static Node *get_rule_sortgroupclause(SortGroupClause *srt, List *tlist,
200 deparse_context *context);
201 static void get_rule_orderby(List *orderList, List *targetList,
202 bool force_colno, deparse_context *context);
203 static void get_rule_windowclause(Query *query, deparse_context *context);
204 static void get_rule_windowspec(WindowClause *wc, List *targetList,
205 deparse_context *context);
206 static char *get_variable(Var *var, int levelsup, bool showstar,
207 deparse_context *context);
208 static RangeTblEntry *find_rte_by_refname(const char *refname,
209 deparse_context *context);
210 static void get_parameter(Param *param, deparse_context *context);
211 static void print_parameter_expr(Node *expr, ListCell *ancestor_cell,
212 deparse_namespace *dpns, deparse_context *context);
213 static const char *get_simple_binary_op_name(OpExpr *expr);
214 static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);
215 static void appendContextKeyword(deparse_context *context, const char *str,
216 int indentBefore, int indentAfter, int indentPlus);
217 static void get_rule_expr(Node *node, deparse_context *context,
219 static void get_oper_expr(OpExpr *expr, deparse_context *context);
220 static void get_func_expr(FuncExpr *expr, deparse_context *context,
222 static void get_agg_expr(Aggref *aggref, deparse_context *context);
223 static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context);
224 static void get_coercion_expr(Node *arg, deparse_context *context,
225 Oid resulttype, int32 resulttypmod,
227 static void get_const_expr(Const *constval, deparse_context *context,
229 static void get_const_collation(Const *constval, deparse_context *context);
230 static void simple_quote_literal(StringInfo buf, const char *val);
231 static void get_sublink_expr(SubLink *sublink, deparse_context *context);
232 static void get_from_clause(Query *query, const char *prefix,
233 deparse_context *context);
234 static void get_from_clause_item(Node *jtnode, Query *query,
235 deparse_context *context);
236 static void get_from_clause_alias(Alias *alias, RangeTblEntry *rte,
237 deparse_context *context);
238 static void get_from_clause_coldeflist(List *names, List *types, List *typmods, List *collations,
239 deparse_context *context);
240 static void get_opclass_name(Oid opclass, Oid actual_datatype,
242 static Node *processIndirection(Node *node, deparse_context *context,
244 static void printSubscripts(ArrayRef *aref, deparse_context *context);
245 static char *get_relation_name(Oid relid);
246 static char *generate_relation_name(Oid relid, List *namespaces);
247 static char *generate_function_name(Oid funcid, int nargs, List *argnames,
248 Oid *argtypes, bool *is_variadic);
249 static char *generate_operator_name(Oid operid, Oid arg1, Oid arg2);
250 static text *string_to_text(char *str);
251 static char *flatten_reloptions(Oid relid);
253 #define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
257 * get_ruledef - Do it all and return a text
258 * that could be used as a statement
259 * to recreate the rule
263 pg_get_ruledef(PG_FUNCTION_ARGS)
265 Oid ruleoid = PG_GETARG_OID(0);
267 PG_RETURN_TEXT_P(string_to_text(pg_get_ruledef_worker(ruleoid, 0)));
272 pg_get_ruledef_ext(PG_FUNCTION_ARGS)
274 Oid ruleoid = PG_GETARG_OID(0);
275 bool pretty = PG_GETARG_BOOL(1);
278 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
279 PG_RETURN_TEXT_P(string_to_text(pg_get_ruledef_worker(ruleoid, prettyFlags)));
284 pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
294 * Do this first so that string is alloc'd in outer context not SPI's.
296 initStringInfo(&buf);
299 * Connect to SPI manager
301 if (SPI_connect() != SPI_OK_CONNECT)
302 elog(ERROR, "SPI_connect failed");
305 * On the first call prepare the plan to lookup pg_rewrite. We read
306 * pg_rewrite over the SPI manager instead of using the syscache to be
307 * checked for read access on pg_rewrite.
309 if (plan_getrulebyoid == NULL)
314 argtypes[0] = OIDOID;
315 plan = SPI_prepare(query_getrulebyoid, 1, argtypes);
317 elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid);
318 plan_getrulebyoid = SPI_saveplan(plan);
322 * Get the pg_rewrite tuple for this rule
324 args[0] = ObjectIdGetDatum(ruleoid);
326 spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 1);
327 if (spirc != SPI_OK_SELECT)
328 elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
329 if (SPI_processed != 1)
330 appendStringInfo(&buf, "-");
334 * Get the rule's definition and put it into executor's memory
336 ruletup = SPI_tuptable->vals[0];
337 rulettc = SPI_tuptable->tupdesc;
338 make_ruledef(&buf, ruletup, rulettc, prettyFlags);
342 * Disconnect from SPI manager
344 if (SPI_finish() != SPI_OK_FINISH)
345 elog(ERROR, "SPI_finish failed");
352 * get_viewdef - Mainly the same thing, but we
353 * only return the SELECT part of a view
357 pg_get_viewdef(PG_FUNCTION_ARGS)
360 Oid viewoid = PG_GETARG_OID(0);
362 PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, 0)));
367 pg_get_viewdef_ext(PG_FUNCTION_ARGS)
370 Oid viewoid = PG_GETARG_OID(0);
371 bool pretty = PG_GETARG_BOOL(1);
374 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
375 PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags)));
379 pg_get_viewdef_name(PG_FUNCTION_ARGS)
381 /* By qualified name */
382 text *viewname = PG_GETARG_TEXT_P(0);
386 viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
387 viewoid = RangeVarGetRelid(viewrel, false);
389 PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, 0)));
394 pg_get_viewdef_name_ext(PG_FUNCTION_ARGS)
396 /* By qualified name */
397 text *viewname = PG_GETARG_TEXT_P(0);
398 bool pretty = PG_GETARG_BOOL(1);
403 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
404 viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
405 viewoid = RangeVarGetRelid(viewrel, false);
407 PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags)));
411 * Common code for by-OID and by-name variants of pg_get_viewdef
414 pg_get_viewdef_worker(Oid viewoid, int prettyFlags)
424 * Do this first so that string is alloc'd in outer context not SPI's.
426 initStringInfo(&buf);
429 * Connect to SPI manager
431 if (SPI_connect() != SPI_OK_CONNECT)
432 elog(ERROR, "SPI_connect failed");
435 * On the first call prepare the plan to lookup pg_rewrite. We read
436 * pg_rewrite over the SPI manager instead of using the syscache to be
437 * checked for read access on pg_rewrite.
439 if (plan_getviewrule == NULL)
444 argtypes[0] = OIDOID;
445 argtypes[1] = NAMEOID;
446 plan = SPI_prepare(query_getviewrule, 2, argtypes);
448 elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule);
449 plan_getviewrule = SPI_saveplan(plan);
453 * Get the pg_rewrite tuple for the view's SELECT rule
455 args[0] = ObjectIdGetDatum(viewoid);
456 args[1] = PointerGetDatum(ViewSelectRuleName);
459 spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 2);
460 if (spirc != SPI_OK_SELECT)
461 elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
462 if (SPI_processed != 1)
463 appendStringInfo(&buf, "Not a view");
467 * Get the rule's definition and put it into executor's memory
469 ruletup = SPI_tuptable->vals[0];
470 rulettc = SPI_tuptable->tupdesc;
471 make_viewdef(&buf, ruletup, rulettc, prettyFlags);
475 * Disconnect from SPI manager
477 if (SPI_finish() != SPI_OK_FINISH)
478 elog(ERROR, "SPI_finish failed");
484 * get_triggerdef - Get the definition of a trigger
488 pg_get_triggerdef(PG_FUNCTION_ARGS)
490 Oid trigid = PG_GETARG_OID(0);
492 PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, false)));
496 pg_get_triggerdef_ext(PG_FUNCTION_ARGS)
498 Oid trigid = PG_GETARG_OID(0);
499 bool pretty = PG_GETARG_BOOL(1);
501 PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, pretty)));
505 pg_get_triggerdef_worker(Oid trigid, bool pretty)
508 Form_pg_trigger trigrec;
519 * Fetch the pg_trigger tuple by the Oid of the trigger
521 tgrel = heap_open(TriggerRelationId, AccessShareLock);
523 ScanKeyInit(&skey[0],
524 ObjectIdAttributeNumber,
525 BTEqualStrategyNumber, F_OIDEQ,
526 ObjectIdGetDatum(trigid));
528 tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
529 SnapshotNow, 1, skey);
531 ht_trig = systable_getnext(tgscan);
533 if (!HeapTupleIsValid(ht_trig))
534 elog(ERROR, "could not find tuple for trigger %u", trigid);
536 trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
539 * Start the trigger definition. Note that the trigger's name should never
540 * be schema-qualified, but the trigger rel's name may be.
542 initStringInfo(&buf);
544 tgname = NameStr(trigrec->tgname);
545 appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
546 OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
547 quote_identifier(tgname));
549 if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
550 appendStringInfo(&buf, "BEFORE");
551 else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
552 appendStringInfo(&buf, "AFTER");
553 else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
554 appendStringInfo(&buf, "INSTEAD OF");
556 elog(ERROR, "unexpected tgtype value: %d", trigrec->tgtype);
558 if (TRIGGER_FOR_INSERT(trigrec->tgtype))
560 appendStringInfo(&buf, " INSERT");
563 if (TRIGGER_FOR_DELETE(trigrec->tgtype))
566 appendStringInfo(&buf, " OR DELETE");
568 appendStringInfo(&buf, " DELETE");
571 if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
574 appendStringInfo(&buf, " OR UPDATE");
576 appendStringInfo(&buf, " UPDATE");
578 /* tgattr is first var-width field, so OK to access directly */
579 if (trigrec->tgattr.dim1 > 0)
583 appendStringInfoString(&buf, " OF ");
584 for (i = 0; i < trigrec->tgattr.dim1; i++)
589 appendStringInfoString(&buf, ", ");
590 attname = get_relid_attribute_name(trigrec->tgrelid,
591 trigrec->tgattr.values[i]);
592 appendStringInfoString(&buf, quote_identifier(attname));
596 if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
599 appendStringInfo(&buf, " OR TRUNCATE");
601 appendStringInfo(&buf, " TRUNCATE");
604 appendStringInfo(&buf, " ON %s ",
605 generate_relation_name(trigrec->tgrelid, NIL));
607 if (OidIsValid(trigrec->tgconstraint))
609 if (OidIsValid(trigrec->tgconstrrelid))
610 appendStringInfo(&buf, "FROM %s ",
611 generate_relation_name(trigrec->tgconstrrelid, NIL));
612 if (!trigrec->tgdeferrable)
613 appendStringInfo(&buf, "NOT ");
614 appendStringInfo(&buf, "DEFERRABLE INITIALLY ");
615 if (trigrec->tginitdeferred)
616 appendStringInfo(&buf, "DEFERRED ");
618 appendStringInfo(&buf, "IMMEDIATE ");
621 if (TRIGGER_FOR_ROW(trigrec->tgtype))
622 appendStringInfo(&buf, "FOR EACH ROW ");
624 appendStringInfo(&buf, "FOR EACH STATEMENT ");
626 /* If the trigger has a WHEN qualification, add that */
627 value = fastgetattr(ht_trig, Anum_pg_trigger_tgqual,
628 tgrel->rd_att, &isnull);
633 deparse_context context;
634 deparse_namespace dpns;
635 RangeTblEntry *oldrte;
636 RangeTblEntry *newrte;
638 appendStringInfoString(&buf, "WHEN (");
640 qual = stringToNode(TextDatumGetCString(value));
642 relkind = get_rel_relkind(trigrec->tgrelid);
644 /* Build minimal OLD and NEW RTEs for the rel */
645 oldrte = makeNode(RangeTblEntry);
646 oldrte->rtekind = RTE_RELATION;
647 oldrte->relid = trigrec->tgrelid;
648 oldrte->relkind = relkind;
649 oldrte->eref = makeAlias("old", NIL);
651 oldrte->inFromCl = true;
653 newrte = makeNode(RangeTblEntry);
654 newrte->rtekind = RTE_RELATION;
655 newrte->relid = trigrec->tgrelid;
656 newrte->relkind = relkind;
657 newrte->eref = makeAlias("new", NIL);
659 newrte->inFromCl = true;
661 /* Build two-element rtable */
662 memset(&dpns, 0, sizeof(dpns));
663 dpns.rtable = list_make2(oldrte, newrte);
666 /* Set up context with one-deep namespace stack */
668 context.namespaces = list_make1(&dpns);
669 context.windowClause = NIL;
670 context.windowTList = NIL;
671 context.varprefix = true;
672 context.prettyFlags = pretty ? PRETTYFLAG_PAREN : 0;
673 context.indentLevel = PRETTYINDENT_STD;
675 get_rule_expr(qual, &context, false);
677 appendStringInfo(&buf, ") ");
680 appendStringInfo(&buf, "EXECUTE PROCEDURE %s(",
681 generate_function_name(trigrec->tgfoid, 0,
684 if (trigrec->tgnargs > 0)
689 value = fastgetattr(ht_trig, Anum_pg_trigger_tgargs,
690 tgrel->rd_att, &isnull);
692 elog(ERROR, "tgargs is null for trigger %u", trigid);
693 p = (char *) VARDATA(DatumGetByteaP(value));
694 for (i = 0; i < trigrec->tgnargs; i++)
697 appendStringInfo(&buf, ", ");
698 simple_quote_literal(&buf, p);
699 /* advance p to next string embedded in tgargs */
706 /* We deliberately do not put semi-colon at end */
707 appendStringInfo(&buf, ")");
710 systable_endscan(tgscan);
712 heap_close(tgrel, AccessShareLock);
718 * get_indexdef - Get the definition of an index
720 * In the extended version, there is a colno argument as well as pretty bool.
721 * if colno == 0, we want a complete index definition.
722 * if colno > 0, we only want the Nth index key's variable or expression.
724 * Note that the SQL-function versions of this omit any info about the
725 * index tablespace; this is intentional because pg_dump wants it that way.
726 * However pg_get_indexdef_string() includes index tablespace if not default.
730 pg_get_indexdef(PG_FUNCTION_ARGS)
732 Oid indexrelid = PG_GETARG_OID(0);
734 PG_RETURN_TEXT_P(string_to_text(pg_get_indexdef_worker(indexrelid, 0,
740 pg_get_indexdef_ext(PG_FUNCTION_ARGS)
742 Oid indexrelid = PG_GETARG_OID(0);
743 int32 colno = PG_GETARG_INT32(1);
744 bool pretty = PG_GETARG_BOOL(2);
747 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
748 PG_RETURN_TEXT_P(string_to_text(pg_get_indexdef_worker(indexrelid, colno,
755 /* Internal version that returns a palloc'd C string */
757 pg_get_indexdef_string(Oid indexrelid)
759 return pg_get_indexdef_worker(indexrelid, 0, NULL, false, true, 0);
762 /* Internal version that just reports the column definitions */
764 pg_get_indexdef_columns(Oid indexrelid, bool pretty)
768 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
769 return pg_get_indexdef_worker(indexrelid, 0, NULL, true, false, prettyFlags);
773 * Internal workhorse to decompile an index definition.
775 * This is now used for exclusion constraints as well: if excludeOps is not
776 * NULL then it points to an array of exclusion operator OIDs.
779 pg_get_indexdef_worker(Oid indexrelid, int colno,
780 const Oid *excludeOps,
781 bool attrsOnly, bool showTblSpc,
784 /* might want a separate isConstraint parameter later */
785 bool isConstraint = (excludeOps != NULL);
789 Form_pg_index idxrec;
790 Form_pg_class idxrelrec;
793 ListCell *indexpr_item;
799 Datum indoptionDatum;
801 oidvector *indcollation;
803 int2vector *indoption;
809 * Fetch the pg_index tuple by the Oid of the index
811 ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
812 if (!HeapTupleIsValid(ht_idx))
813 elog(ERROR, "cache lookup failed for index %u", indexrelid);
814 idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
816 indrelid = idxrec->indrelid;
817 Assert(indexrelid == idxrec->indexrelid);
819 /* Must get indcollation, indclass, and indoption the hard way */
820 indcollDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
821 Anum_pg_index_indcollation, &isnull);
823 indcollation = (oidvector *) DatumGetPointer(indcollDatum);
825 indclassDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
826 Anum_pg_index_indclass, &isnull);
828 indclass = (oidvector *) DatumGetPointer(indclassDatum);
830 indoptionDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
831 Anum_pg_index_indoption, &isnull);
833 indoption = (int2vector *) DatumGetPointer(indoptionDatum);
836 * Fetch the pg_class tuple of the index relation
838 ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexrelid));
839 if (!HeapTupleIsValid(ht_idxrel))
840 elog(ERROR, "cache lookup failed for relation %u", indexrelid);
841 idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
844 * Fetch the pg_am tuple of the index' access method
846 ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
847 if (!HeapTupleIsValid(ht_am))
848 elog(ERROR, "cache lookup failed for access method %u",
850 amrec = (Form_pg_am) GETSTRUCT(ht_am);
853 * Get the index expressions, if any. (NOTE: we do not use the relcache
854 * versions of the expressions and predicate, because we want to display
855 * non-const-folded expressions.)
857 if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs))
863 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
864 Anum_pg_index_indexprs, &isnull);
866 exprsString = TextDatumGetCString(exprsDatum);
867 indexprs = (List *) stringToNode(exprsString);
873 indexpr_item = list_head(indexprs);
875 context = deparse_context_for(get_relation_name(indrelid), indrelid);
878 * Start the index definition. Note that the index's name should never be
879 * schema-qualified, but the indexed rel's name may be.
881 initStringInfo(&buf);
886 appendStringInfo(&buf, "CREATE %sINDEX %s ON %s USING %s (",
887 idxrec->indisunique ? "UNIQUE " : "",
888 quote_identifier(NameStr(idxrelrec->relname)),
889 generate_relation_name(indrelid, NIL),
890 quote_identifier(NameStr(amrec->amname)));
891 else /* currently, must be EXCLUDE constraint */
892 appendStringInfo(&buf, "EXCLUDE USING %s (",
893 quote_identifier(NameStr(amrec->amname)));
897 * Report the indexed attributes
900 for (keyno = 0; keyno < idxrec->indnatts; keyno++)
902 AttrNumber attnum = idxrec->indkey.values[keyno];
903 int16 opt = indoption->values[keyno];
908 appendStringInfoString(&buf, sep);
913 /* Simple index column */
917 attname = get_relid_attribute_name(indrelid, attnum);
918 if (!colno || colno == keyno + 1)
919 appendStringInfoString(&buf, quote_identifier(attname));
920 get_atttypetypmodcoll(indrelid, attnum,
921 &keycoltype, &keycoltypmod,
926 /* expressional index */
929 if (indexpr_item == NULL)
930 elog(ERROR, "too few entries in indexprs list");
931 indexkey = (Node *) lfirst(indexpr_item);
932 indexpr_item = lnext(indexpr_item);
934 str = deparse_expression_pretty(indexkey, context, false, false,
936 if (!colno || colno == keyno + 1)
938 /* Need parens if it's not a bare function call */
939 if (indexkey && IsA(indexkey, FuncExpr) &&
940 ((FuncExpr *) indexkey)->funcformat == COERCE_EXPLICIT_CALL)
941 appendStringInfoString(&buf, str);
943 appendStringInfo(&buf, "(%s)", str);
945 keycoltype = exprType(indexkey);
946 keycolcollation = exprCollation(indexkey);
949 if (!attrsOnly && (!colno || colno == keyno + 1))
953 /* Add collation, if not default for column */
954 indcoll = indcollation->values[keyno];
955 if (OidIsValid(indcoll) && indcoll != keycolcollation)
956 appendStringInfo(&buf, " COLLATE %s",
957 generate_collation_name((indcoll)));
959 /* Add the operator class name, if not default */
960 get_opclass_name(indclass->values[keyno], keycoltype, &buf);
962 /* Add options if relevant */
963 if (amrec->amcanorder)
965 /* if it supports sort ordering, report DESC and NULLS opts */
966 if (opt & INDOPTION_DESC)
968 appendStringInfo(&buf, " DESC");
969 /* NULLS FIRST is the default in this case */
970 if (!(opt & INDOPTION_NULLS_FIRST))
971 appendStringInfo(&buf, " NULLS LAST");
975 if (opt & INDOPTION_NULLS_FIRST)
976 appendStringInfo(&buf, " NULLS FIRST");
980 /* Add the exclusion operator if relevant */
981 if (excludeOps != NULL)
982 appendStringInfo(&buf, " WITH %s",
983 generate_operator_name(excludeOps[keyno],
991 appendStringInfoChar(&buf, ')');
994 * If it has options, append "WITH (options)"
996 str = flatten_reloptions(indexrelid);
999 appendStringInfo(&buf, " WITH (%s)", str);
1004 * If it's in a nondefault tablespace, say so, but only if requested
1010 tblspc = get_rel_tablespace(indexrelid);
1011 if (OidIsValid(tblspc))
1014 appendStringInfoString(&buf, " USING INDEX");
1015 appendStringInfo(&buf, " TABLESPACE %s",
1016 quote_identifier(get_tablespace_name(tblspc)));
1021 * If it's a partial index, decompile and append the predicate
1023 if (!heap_attisnull(ht_idx, Anum_pg_index_indpred))
1030 /* Convert text string to node tree */
1031 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
1032 Anum_pg_index_indpred, &isnull);
1034 predString = TextDatumGetCString(predDatum);
1035 node = (Node *) stringToNode(predString);
1039 str = deparse_expression_pretty(node, context, false, false,
1042 appendStringInfo(&buf, " WHERE (%s)", str);
1044 appendStringInfo(&buf, " WHERE %s", str);
1049 ReleaseSysCache(ht_idx);
1050 ReleaseSysCache(ht_idxrel);
1051 ReleaseSysCache(ht_am);
1058 * pg_get_constraintdef
1060 * Returns the definition for the constraint, ie, everything that needs to
1061 * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>".
1064 pg_get_constraintdef(PG_FUNCTION_ARGS)
1066 Oid constraintId = PG_GETARG_OID(0);
1068 PG_RETURN_TEXT_P(string_to_text(pg_get_constraintdef_worker(constraintId,
1073 pg_get_constraintdef_ext(PG_FUNCTION_ARGS)
1075 Oid constraintId = PG_GETARG_OID(0);
1076 bool pretty = PG_GETARG_BOOL(1);
1079 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
1080 PG_RETURN_TEXT_P(string_to_text(pg_get_constraintdef_worker(constraintId,
1081 false, prettyFlags)));
1084 /* Internal version that returns a palloc'd C string */
1086 pg_get_constraintdef_string(Oid constraintId)
1088 return pg_get_constraintdef_worker(constraintId, true, 0);
1092 pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
1096 Form_pg_constraint conForm;
1099 tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintId));
1100 if (!HeapTupleIsValid(tup)) /* should not happen */
1101 elog(ERROR, "cache lookup failed for constraint %u", constraintId);
1102 conForm = (Form_pg_constraint) GETSTRUCT(tup);
1104 initStringInfo(&buf);
1106 if (fullCommand && OidIsValid(conForm->conrelid))
1108 appendStringInfo(&buf, "ALTER TABLE ONLY %s ADD CONSTRAINT %s ",
1109 generate_relation_name(conForm->conrelid, NIL),
1110 quote_identifier(NameStr(conForm->conname)));
1113 switch (conForm->contype)
1115 case CONSTRAINT_FOREIGN:
1121 /* Start off the constraint definition */
1122 appendStringInfo(&buf, "FOREIGN KEY (");
1124 /* Fetch and build referencing-column list */
1125 val = SysCacheGetAttr(CONSTROID, tup,
1126 Anum_pg_constraint_conkey, &isnull);
1128 elog(ERROR, "null conkey for constraint %u",
1131 decompile_column_index_array(val, conForm->conrelid, &buf);
1133 /* add foreign relation name */
1134 appendStringInfo(&buf, ") REFERENCES %s(",
1135 generate_relation_name(conForm->confrelid,
1138 /* Fetch and build referenced-column list */
1139 val = SysCacheGetAttr(CONSTROID, tup,
1140 Anum_pg_constraint_confkey, &isnull);
1142 elog(ERROR, "null confkey for constraint %u",
1145 decompile_column_index_array(val, conForm->confrelid, &buf);
1147 appendStringInfo(&buf, ")");
1149 /* Add match type */
1150 switch (conForm->confmatchtype)
1152 case FKCONSTR_MATCH_FULL:
1153 string = " MATCH FULL";
1155 case FKCONSTR_MATCH_PARTIAL:
1156 string = " MATCH PARTIAL";
1158 case FKCONSTR_MATCH_UNSPECIFIED:
1162 elog(ERROR, "unrecognized confmatchtype: %d",
1163 conForm->confmatchtype);
1164 string = ""; /* keep compiler quiet */
1167 appendStringInfoString(&buf, string);
1169 /* Add ON UPDATE and ON DELETE clauses, if needed */
1170 switch (conForm->confupdtype)
1172 case FKCONSTR_ACTION_NOACTION:
1173 string = NULL; /* suppress default */
1175 case FKCONSTR_ACTION_RESTRICT:
1176 string = "RESTRICT";
1178 case FKCONSTR_ACTION_CASCADE:
1181 case FKCONSTR_ACTION_SETNULL:
1182 string = "SET NULL";
1184 case FKCONSTR_ACTION_SETDEFAULT:
1185 string = "SET DEFAULT";
1188 elog(ERROR, "unrecognized confupdtype: %d",
1189 conForm->confupdtype);
1190 string = NULL; /* keep compiler quiet */
1194 appendStringInfo(&buf, " ON UPDATE %s", string);
1196 switch (conForm->confdeltype)
1198 case FKCONSTR_ACTION_NOACTION:
1199 string = NULL; /* suppress default */
1201 case FKCONSTR_ACTION_RESTRICT:
1202 string = "RESTRICT";
1204 case FKCONSTR_ACTION_CASCADE:
1207 case FKCONSTR_ACTION_SETNULL:
1208 string = "SET NULL";
1210 case FKCONSTR_ACTION_SETDEFAULT:
1211 string = "SET DEFAULT";
1214 elog(ERROR, "unrecognized confdeltype: %d",
1215 conForm->confdeltype);
1216 string = NULL; /* keep compiler quiet */
1220 appendStringInfo(&buf, " ON DELETE %s", string);
1224 case CONSTRAINT_PRIMARY:
1225 case CONSTRAINT_UNIQUE:
1231 /* Start off the constraint definition */
1232 if (conForm->contype == CONSTRAINT_PRIMARY)
1233 appendStringInfo(&buf, "PRIMARY KEY (");
1235 appendStringInfo(&buf, "UNIQUE (");
1237 /* Fetch and build target column list */
1238 val = SysCacheGetAttr(CONSTROID, tup,
1239 Anum_pg_constraint_conkey, &isnull);
1241 elog(ERROR, "null conkey for constraint %u",
1244 decompile_column_index_array(val, conForm->conrelid, &buf);
1246 appendStringInfo(&buf, ")");
1248 indexId = get_constraint_index(constraintId);
1250 /* XXX why do we only print these bits if fullCommand? */
1251 if (fullCommand && OidIsValid(indexId))
1253 char *options = flatten_reloptions(indexId);
1258 appendStringInfo(&buf, " WITH (%s)", options);
1262 tblspc = get_rel_tablespace(indexId);
1263 if (OidIsValid(tblspc))
1264 appendStringInfo(&buf, " USING INDEX TABLESPACE %s",
1265 quote_identifier(get_tablespace_name(tblspc)));
1270 case CONSTRAINT_CHECK:
1279 /* Fetch constraint expression in parsetree form */
1280 val = SysCacheGetAttr(CONSTROID, tup,
1281 Anum_pg_constraint_conbin, &isnull);
1283 elog(ERROR, "null conbin for constraint %u",
1286 conbin = TextDatumGetCString(val);
1287 expr = stringToNode(conbin);
1289 /* Set up deparsing context for Var nodes in constraint */
1290 if (conForm->conrelid != InvalidOid)
1292 /* relation constraint */
1293 context = deparse_context_for(get_relation_name(conForm->conrelid),
1298 /* domain constraint --- can't have Vars */
1302 consrc = deparse_expression_pretty(expr, context, false, false,
1306 * Now emit the constraint definition. There are cases where
1307 * the constraint expression will be fully parenthesized and
1308 * we don't need the outer parens ... but there are other
1309 * cases where we do need 'em. Be conservative for now.
1311 * Note that simply checking for leading '(' and trailing ')'
1312 * would NOT be good enough, consider "(x > 0) AND (y > 0)".
1314 appendStringInfo(&buf, "CHECK (%s)", consrc);
1318 case CONSTRAINT_TRIGGER:
1321 * There isn't an ALTER TABLE syntax for creating a user-defined
1322 * constraint trigger, but it seems better to print something than
1323 * throw an error; if we throw error then this function couldn't
1324 * safely be applied to all rows of pg_constraint.
1326 appendStringInfo(&buf, "TRIGGER");
1328 case CONSTRAINT_EXCLUSION:
1330 Oid indexOid = conForm->conindid;
1338 /* Extract operator OIDs from the pg_constraint tuple */
1339 val = SysCacheGetAttr(CONSTROID, tup,
1340 Anum_pg_constraint_conexclop,
1343 elog(ERROR, "null conexclop for constraint %u",
1346 deconstruct_array(DatumGetArrayTypeP(val),
1347 OIDOID, sizeof(Oid), true, 'i',
1348 &elems, NULL, &nElems);
1350 operators = (Oid *) palloc(nElems * sizeof(Oid));
1351 for (i = 0; i < nElems; i++)
1352 operators[i] = DatumGetObjectId(elems[i]);
1354 /* pg_get_indexdef_worker does the rest */
1355 /* suppress tablespace because pg_dump wants it that way */
1356 appendStringInfoString(&buf,
1357 pg_get_indexdef_worker(indexOid,
1366 elog(ERROR, "invalid constraint type \"%c\"", conForm->contype);
1370 if (conForm->condeferrable)
1371 appendStringInfo(&buf, " DEFERRABLE");
1372 if (conForm->condeferred)
1373 appendStringInfo(&buf, " INITIALLY DEFERRED");
1376 ReleaseSysCache(tup);
1383 * Convert an int16[] Datum into a comma-separated list of column names
1384 * for the indicated relation; append the list to buf.
1387 decompile_column_index_array(Datum column_index_array, Oid relId,
1394 /* Extract data from array of int16 */
1395 deconstruct_array(DatumGetArrayTypeP(column_index_array),
1396 INT2OID, 2, true, 's',
1397 &keys, NULL, &nKeys);
1399 for (j = 0; j < nKeys; j++)
1403 colName = get_relid_attribute_name(relId, DatumGetInt16(keys[j]));
1406 appendStringInfoString(buf, quote_identifier(colName));
1408 appendStringInfo(buf, ", %s", quote_identifier(colName));
1414 * get_expr - Decompile an expression tree
1416 * Input: an expression tree in nodeToString form, and a relation OID
1418 * Output: reverse-listed expression
1420 * Currently, the expression can only refer to a single relation, namely
1421 * the one specified by the second parameter. This is sufficient for
1422 * partial indexes, column default expressions, etc. We also support
1423 * Var-free expressions, for which the OID can be InvalidOid.
1427 pg_get_expr(PG_FUNCTION_ARGS)
1429 text *expr = PG_GETARG_TEXT_P(0);
1430 Oid relid = PG_GETARG_OID(1);
1433 if (OidIsValid(relid))
1435 /* Get the name for the relation */
1436 relname = get_rel_name(relid);
1439 * If the OID isn't actually valid, don't throw an error, just return
1440 * NULL. This is a bit questionable, but it's what we've done
1441 * historically, and it can help avoid unwanted failures when
1442 * examining catalog entries for just-deleted relations.
1444 if (relname == NULL)
1450 PG_RETURN_TEXT_P(pg_get_expr_worker(expr, relid, relname, 0));
1454 pg_get_expr_ext(PG_FUNCTION_ARGS)
1456 text *expr = PG_GETARG_TEXT_P(0);
1457 Oid relid = PG_GETARG_OID(1);
1458 bool pretty = PG_GETARG_BOOL(2);
1462 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
1464 if (OidIsValid(relid))
1466 /* Get the name for the relation */
1467 relname = get_rel_name(relid);
1468 /* See notes above */
1469 if (relname == NULL)
1475 PG_RETURN_TEXT_P(pg_get_expr_worker(expr, relid, relname, prettyFlags));
1479 pg_get_expr_worker(text *expr, Oid relid, const char *relname, int prettyFlags)
1486 /* Convert input TEXT object to C string */
1487 exprstr = text_to_cstring(expr);
1489 /* Convert expression to node tree */
1490 node = (Node *) stringToNode(exprstr);
1494 /* Prepare deparse context if needed */
1495 if (OidIsValid(relid))
1496 context = deparse_context_for(relname, relid);
1501 str = deparse_expression_pretty(node, context, false, false,
1504 return string_to_text(str);
1509 * get_userbyid - Get a user name by roleid and
1510 * fallback to 'unknown (OID=n)'
1514 pg_get_userbyid(PG_FUNCTION_ARGS)
1516 Oid roleid = PG_GETARG_OID(0);
1519 Form_pg_authid role_rec;
1522 * Allocate space for the result
1524 result = (Name) palloc(NAMEDATALEN);
1525 memset(NameStr(*result), 0, NAMEDATALEN);
1528 * Get the pg_authid entry and print the result
1530 roletup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
1531 if (HeapTupleIsValid(roletup))
1533 role_rec = (Form_pg_authid) GETSTRUCT(roletup);
1534 StrNCpy(NameStr(*result), NameStr(role_rec->rolname), NAMEDATALEN);
1535 ReleaseSysCache(roletup);
1538 sprintf(NameStr(*result), "unknown (OID=%u)", roleid);
1540 PG_RETURN_NAME(result);
1545 * pg_get_serial_sequence
1546 * Get the name of the sequence used by a serial column,
1547 * formatted suitably for passing to setval, nextval or currval.
1548 * First parameter is not treated as double-quoted, second parameter
1549 * is --- see documentation for reason.
1552 pg_get_serial_sequence(PG_FUNCTION_ARGS)
1554 text *tablename = PG_GETARG_TEXT_P(0);
1555 text *columnname = PG_GETARG_TEXT_PP(1);
1560 Oid sequenceId = InvalidOid;
1566 /* Get the OID of the table */
1567 tablerv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
1568 tableOid = RangeVarGetRelid(tablerv, false);
1570 /* Get the number of the column */
1571 column = text_to_cstring(columnname);
1573 attnum = get_attnum(tableOid, column);
1574 if (attnum == InvalidAttrNumber)
1576 (errcode(ERRCODE_UNDEFINED_COLUMN),
1577 errmsg("column \"%s\" of relation \"%s\" does not exist",
1578 column, tablerv->relname)));
1580 /* Search the dependency table for the dependent sequence */
1581 depRel = heap_open(DependRelationId, AccessShareLock);
1583 ScanKeyInit(&key[0],
1584 Anum_pg_depend_refclassid,
1585 BTEqualStrategyNumber, F_OIDEQ,
1586 ObjectIdGetDatum(RelationRelationId));
1587 ScanKeyInit(&key[1],
1588 Anum_pg_depend_refobjid,
1589 BTEqualStrategyNumber, F_OIDEQ,
1590 ObjectIdGetDatum(tableOid));
1591 ScanKeyInit(&key[2],
1592 Anum_pg_depend_refobjsubid,
1593 BTEqualStrategyNumber, F_INT4EQ,
1594 Int32GetDatum(attnum));
1596 scan = systable_beginscan(depRel, DependReferenceIndexId, true,
1597 SnapshotNow, 3, key);
1599 while (HeapTupleIsValid(tup = systable_getnext(scan)))
1601 Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
1604 * We assume any auto dependency of a sequence on a column must be
1605 * what we are looking for. (We need the relkind test because indexes
1606 * can also have auto dependencies on columns.)
1608 if (deprec->classid == RelationRelationId &&
1609 deprec->objsubid == 0 &&
1610 deprec->deptype == DEPENDENCY_AUTO &&
1611 get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
1613 sequenceId = deprec->objid;
1618 systable_endscan(scan);
1619 heap_close(depRel, AccessShareLock);
1621 if (OidIsValid(sequenceId))
1624 Form_pg_class classtuple;
1628 /* Get the sequence's pg_class entry */
1629 classtup = SearchSysCache1(RELOID, ObjectIdGetDatum(sequenceId));
1630 if (!HeapTupleIsValid(classtup))
1631 elog(ERROR, "cache lookup failed for relation %u", sequenceId);
1632 classtuple = (Form_pg_class) GETSTRUCT(classtup);
1634 /* Get the namespace */
1635 nspname = get_namespace_name(classtuple->relnamespace);
1637 elog(ERROR, "cache lookup failed for namespace %u",
1638 classtuple->relnamespace);
1640 /* And construct the result string */
1641 result = quote_qualified_identifier(nspname,
1642 NameStr(classtuple->relname));
1644 ReleaseSysCache(classtup);
1646 PG_RETURN_TEXT_P(string_to_text(result));
1654 * pg_get_functiondef
1655 * Returns the complete "CREATE OR REPLACE FUNCTION ..." statement for
1656 * the specified function.
1658 * Note: if you change the output format of this function, be careful not
1659 * to break psql's rules (in \ef and \sf) for identifying the start of the
1660 * function body. To wit: the function body starts on a line that begins
1661 * with "AS ", and no preceding line will look like that.
1664 pg_get_functiondef(PG_FUNCTION_ARGS)
1666 Oid funcid = PG_GETARG_OID(0);
1672 Form_pg_language lang;
1681 initStringInfo(&buf);
1683 /* Look up the function */
1684 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1685 if (!HeapTupleIsValid(proctup))
1686 elog(ERROR, "cache lookup failed for function %u", funcid);
1687 proc = (Form_pg_proc) GETSTRUCT(proctup);
1688 name = NameStr(proc->proname);
1692 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1693 errmsg("\"%s\" is an aggregate function", name)));
1695 /* Need its pg_language tuple for the language name */
1696 langtup = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
1697 if (!HeapTupleIsValid(langtup))
1698 elog(ERROR, "cache lookup failed for language %u", proc->prolang);
1699 lang = (Form_pg_language) GETSTRUCT(langtup);
1702 * We always qualify the function name, to ensure the right function gets
1705 nsp = get_namespace_name(proc->pronamespace);
1706 appendStringInfo(&buf, "CREATE OR REPLACE FUNCTION %s(",
1707 quote_qualified_identifier(nsp, name));
1708 (void) print_function_arguments(&buf, proctup, false, true);
1709 appendStringInfoString(&buf, ")\n RETURNS ");
1710 print_function_rettype(&buf, proctup);
1711 appendStringInfo(&buf, "\n LANGUAGE %s\n",
1712 quote_identifier(NameStr(lang->lanname)));
1714 /* Emit some miscellaneous options on one line */
1717 if (proc->proiswindow)
1718 appendStringInfoString(&buf, " WINDOW");
1719 switch (proc->provolatile)
1721 case PROVOLATILE_IMMUTABLE:
1722 appendStringInfoString(&buf, " IMMUTABLE");
1724 case PROVOLATILE_STABLE:
1725 appendStringInfoString(&buf, " STABLE");
1727 case PROVOLATILE_VOLATILE:
1730 if (proc->proisstrict)
1731 appendStringInfoString(&buf, " STRICT");
1732 if (proc->prosecdef)
1733 appendStringInfoString(&buf, " SECURITY DEFINER");
1735 /* This code for the default cost and rows should match functioncmds.c */
1736 if (proc->prolang == INTERNALlanguageId ||
1737 proc->prolang == ClanguageId)
1741 if (proc->procost != procost)
1742 appendStringInfo(&buf, " COST %g", proc->procost);
1744 if (proc->prorows > 0 && proc->prorows != 1000)
1745 appendStringInfo(&buf, " ROWS %g", proc->prorows);
1747 if (oldlen != buf.len)
1748 appendStringInfoChar(&buf, '\n');
1750 /* Emit any proconfig options, one per line */
1751 tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
1754 ArrayType *a = DatumGetArrayTypeP(tmp);
1757 Assert(ARR_ELEMTYPE(a) == TEXTOID);
1758 Assert(ARR_NDIM(a) == 1);
1759 Assert(ARR_LBOUND(a)[0] == 1);
1761 for (i = 1; i <= ARR_DIMS(a)[0]; i++)
1765 d = array_ref(a, 1, &i,
1766 -1 /* varlenarray */ ,
1767 -1 /* TEXT's typlen */ ,
1768 false /* TEXT's typbyval */ ,
1769 'i' /* TEXT's typalign */ ,
1773 char *configitem = TextDatumGetCString(d);
1776 pos = strchr(configitem, '=');
1781 appendStringInfo(&buf, " SET %s TO ",
1782 quote_identifier(configitem));
1785 * Some GUC variable names are 'LIST' type and hence must not
1788 if (pg_strcasecmp(configitem, "DateStyle") == 0
1789 || pg_strcasecmp(configitem, "search_path") == 0)
1790 appendStringInfoString(&buf, pos);
1792 simple_quote_literal(&buf, pos);
1793 appendStringInfoChar(&buf, '\n');
1798 /* And finally the function definition ... */
1799 appendStringInfoString(&buf, "AS ");
1801 tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
1804 simple_quote_literal(&buf, TextDatumGetCString(tmp));
1805 appendStringInfoString(&buf, ", "); /* assume prosrc isn't null */
1808 tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosrc, &isnull);
1810 elog(ERROR, "null prosrc");
1811 prosrc = TextDatumGetCString(tmp);
1814 * We always use dollar quoting. Figure out a suitable delimiter.
1816 * Since the user is likely to be editing the function body string, we
1817 * shouldn't use a short delimiter that he might easily create a conflict
1818 * with. Hence prefer "$function$", but extend if needed.
1820 initStringInfo(&dq);
1821 appendStringInfoString(&dq, "$function");
1822 while (strstr(prosrc, dq.data) != NULL)
1823 appendStringInfoChar(&dq, 'x');
1824 appendStringInfoChar(&dq, '$');
1826 appendStringInfoString(&buf, dq.data);
1827 appendStringInfoString(&buf, prosrc);
1828 appendStringInfoString(&buf, dq.data);
1830 appendStringInfoString(&buf, "\n");
1832 ReleaseSysCache(langtup);
1833 ReleaseSysCache(proctup);
1835 PG_RETURN_TEXT_P(string_to_text(buf.data));
1839 * pg_get_function_arguments
1840 * Get a nicely-formatted list of arguments for a function.
1841 * This is everything that would go between the parentheses in
1845 pg_get_function_arguments(PG_FUNCTION_ARGS)
1847 Oid funcid = PG_GETARG_OID(0);
1851 initStringInfo(&buf);
1853 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1854 if (!HeapTupleIsValid(proctup))
1855 elog(ERROR, "cache lookup failed for function %u", funcid);
1857 (void) print_function_arguments(&buf, proctup, false, true);
1859 ReleaseSysCache(proctup);
1861 PG_RETURN_TEXT_P(string_to_text(buf.data));
1865 * pg_get_function_identity_arguments
1866 * Get a formatted list of arguments for a function.
1867 * This is everything that would go between the parentheses in
1868 * ALTER FUNCTION, etc. In particular, don't print defaults.
1871 pg_get_function_identity_arguments(PG_FUNCTION_ARGS)
1873 Oid funcid = PG_GETARG_OID(0);
1877 initStringInfo(&buf);
1879 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1880 if (!HeapTupleIsValid(proctup))
1881 elog(ERROR, "cache lookup failed for function %u", funcid);
1883 (void) print_function_arguments(&buf, proctup, false, false);
1885 ReleaseSysCache(proctup);
1887 PG_RETURN_TEXT_P(string_to_text(buf.data));
1891 * pg_get_function_result
1892 * Get a nicely-formatted version of the result type of a function.
1893 * This is what would appear after RETURNS in CREATE FUNCTION.
1896 pg_get_function_result(PG_FUNCTION_ARGS)
1898 Oid funcid = PG_GETARG_OID(0);
1902 initStringInfo(&buf);
1904 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1905 if (!HeapTupleIsValid(proctup))
1906 elog(ERROR, "cache lookup failed for function %u", funcid);
1908 print_function_rettype(&buf, proctup);
1910 ReleaseSysCache(proctup);
1912 PG_RETURN_TEXT_P(string_to_text(buf.data));
1916 * Guts of pg_get_function_result: append the function's return type
1917 * to the specified buffer.
1920 print_function_rettype(StringInfo buf, HeapTuple proctup)
1922 Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
1924 StringInfoData rbuf;
1926 initStringInfo(&rbuf);
1928 if (proc->proretset)
1930 /* It might be a table function; try to print the arguments */
1931 appendStringInfoString(&rbuf, "TABLE(");
1932 ntabargs = print_function_arguments(&rbuf, proctup, true, false);
1934 appendStringInfoString(&rbuf, ")");
1936 resetStringInfo(&rbuf);
1941 /* Not a table function, so do the normal thing */
1942 if (proc->proretset)
1943 appendStringInfoString(&rbuf, "SETOF ");
1944 appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
1947 appendStringInfoString(buf, rbuf.data);
1951 * Common code for pg_get_function_arguments and pg_get_function_result:
1952 * append the desired subset of arguments to buf. We print only TABLE
1953 * arguments when print_table_args is true, and all the others when it's false.
1954 * We print argument defaults only if print_defaults is true.
1955 * Function return value is the number of arguments printed.
1958 print_function_arguments(StringInfo buf, HeapTuple proctup,
1959 bool print_table_args, bool print_defaults)
1961 Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
1969 ListCell *nextargdefault = NULL;
1972 numargs = get_func_arg_info(proctup,
1973 &argtypes, &argnames, &argmodes);
1975 nlackdefaults = numargs;
1976 if (print_defaults && proc->pronargdefaults > 0)
1978 Datum proargdefaults;
1981 proargdefaults = SysCacheGetAttr(PROCOID, proctup,
1982 Anum_pg_proc_proargdefaults,
1989 str = TextDatumGetCString(proargdefaults);
1990 argdefaults = (List *) stringToNode(str);
1991 Assert(IsA(argdefaults, List));
1993 nextargdefault = list_head(argdefaults);
1994 /* nlackdefaults counts only *input* arguments lacking defaults */
1995 nlackdefaults = proc->pronargs - list_length(argdefaults);
2001 for (i = 0; i < numargs; i++)
2003 Oid argtype = argtypes[i];
2004 char *argname = argnames ? argnames[i] : NULL;
2005 char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
2006 const char *modename;
2015 case PROARGMODE_INOUT:
2016 modename = "INOUT ";
2019 case PROARGMODE_OUT:
2023 case PROARGMODE_VARIADIC:
2024 modename = "VARIADIC ";
2027 case PROARGMODE_TABLE:
2032 elog(ERROR, "invalid parameter mode '%c'", argmode);
2033 modename = NULL; /* keep compiler quiet */
2038 inputargno++; /* this is a 1-based counter */
2040 if (print_table_args != (argmode == PROARGMODE_TABLE))
2044 appendStringInfoString(buf, ", ");
2045 appendStringInfoString(buf, modename);
2046 if (argname && argname[0])
2047 appendStringInfo(buf, "%s ", quote_identifier(argname));
2048 appendStringInfoString(buf, format_type_be(argtype));
2049 if (print_defaults && isinput && inputargno > nlackdefaults)
2053 Assert(nextargdefault != NULL);
2054 expr = (Node *) lfirst(nextargdefault);
2055 nextargdefault = lnext(nextargdefault);
2057 appendStringInfo(buf, " DEFAULT %s",
2058 deparse_expression(expr, NIL, false, false));
2068 * deparse_expression - General utility for deparsing expressions
2070 * calls deparse_expression_pretty with all prettyPrinting disabled
2073 deparse_expression(Node *expr, List *dpcontext,
2074 bool forceprefix, bool showimplicit)
2076 return deparse_expression_pretty(expr, dpcontext, forceprefix,
2077 showimplicit, 0, 0);
2081 * deparse_expression_pretty - General utility for deparsing expressions
2083 * expr is the node tree to be deparsed. It must be a transformed expression
2084 * tree (ie, not the raw output of gram.y).
2086 * dpcontext is a list of deparse_namespace nodes representing the context
2087 * for interpreting Vars in the node tree.
2089 * forceprefix is TRUE to force all Vars to be prefixed with their table names.
2091 * showimplicit is TRUE to force all implicit casts to be shown explicitly.
2093 * tries to pretty up the output according to prettyFlags and startIndent.
2095 * The result is a palloc'd string.
2099 deparse_expression_pretty(Node *expr, List *dpcontext,
2100 bool forceprefix, bool showimplicit,
2101 int prettyFlags, int startIndent)
2104 deparse_context context;
2106 initStringInfo(&buf);
2108 context.namespaces = dpcontext;
2109 context.windowClause = NIL;
2110 context.windowTList = NIL;
2111 context.varprefix = forceprefix;
2112 context.prettyFlags = prettyFlags;
2113 context.indentLevel = startIndent;
2115 get_rule_expr(expr, &context, showimplicit);
2121 * deparse_context_for - Build deparse context for a single relation
2123 * Given the reference name (alias) and OID of a relation, build deparsing
2124 * context for an expression referencing only that relation (as varno 1,
2125 * varlevelsup 0). This is sufficient for many uses of deparse_expression.
2129 deparse_context_for(const char *aliasname, Oid relid)
2131 deparse_namespace *dpns;
2134 dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
2136 /* Build a minimal RTE for the rel */
2137 rte = makeNode(RangeTblEntry);
2138 rte->rtekind = RTE_RELATION;
2140 rte->relkind = RELKIND_RELATION; /* no need for exactness here */
2141 rte->eref = makeAlias(aliasname, NIL);
2143 rte->inFromCl = true;
2145 /* Build one-element rtable */
2146 dpns->rtable = list_make1(rte);
2149 /* Return a one-deep namespace stack */
2150 return list_make1(dpns);
2154 * deparse_context_for_planstate - Build deparse context for a plan
2156 * When deparsing an expression in a Plan tree, we might have to resolve
2157 * OUTER or INNER references. To do this, the caller must provide the
2158 * parent PlanState node. Then OUTER and INNER references can be resolved
2159 * by drilling down into the left and right child plans.
2161 * Note: planstate really ought to be declared as "PlanState *", but we use
2162 * "Node *" to avoid having to include execnodes.h in builtins.h.
2164 * The ancestors list is a list of the PlanState's parent PlanStates, the
2165 * most-closely-nested first. This is needed to resolve PARAM_EXEC Params.
2166 * Note we assume that all the PlanStates share the same rtable.
2168 * The plan's rangetable list must also be passed. We actually prefer to use
2169 * the rangetable to resolve simple Vars, but the plan inputs are necessary
2170 * for Vars that reference expressions computed in subplan target lists.
2173 deparse_context_for_planstate(Node *planstate, List *ancestors,
2176 deparse_namespace *dpns;
2178 dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
2180 /* Initialize fields that stay the same across the whole plan tree */
2181 dpns->rtable = rtable;
2184 /* Set our attention on the specific plan node passed in */
2185 set_deparse_planstate(dpns, (PlanState *) planstate);
2186 dpns->ancestors = ancestors;
2188 /* Return a one-deep namespace stack */
2189 return list_make1(dpns);
2193 * set_deparse_planstate: set up deparse_namespace to parse subexpressions
2194 * of a given PlanState node
2196 * This sets the planstate, outer_planstate, inner_planstate, outer_plan, and
2197 * inner_plan fields. Caller is responsible for adjusting the ancestors list
2198 * if necessary. Note that the rtable and ctes fields do not need to change
2199 * when shifting attention to different plan nodes in a single plan tree.
2202 set_deparse_planstate(deparse_namespace *dpns, PlanState *ps)
2204 dpns->planstate = ps;
2207 * We special-case Append and MergeAppend to pretend that the first child
2208 * plan is the OUTER referent; we have to interpret OUTER Vars in their
2209 * tlists according to one of the children, and the first one is the most
2210 * natural choice. Likewise special-case ModifyTable to pretend that the
2211 * first child plan is the OUTER referent; this is to support RETURNING
2212 * lists containing references to non-target relations.
2214 if (IsA(ps, AppendState))
2215 dpns->outer_planstate = ((AppendState *) ps)->appendplans[0];
2216 else if (IsA(ps, MergeAppendState))
2217 dpns->outer_planstate = ((MergeAppendState *) ps)->mergeplans[0];
2218 else if (IsA(ps, ModifyTableState))
2219 dpns->outer_planstate = ((ModifyTableState *) ps)->mt_plans[0];
2221 dpns->outer_planstate = outerPlanState(ps);
2223 if (dpns->outer_planstate)
2224 dpns->outer_plan = dpns->outer_planstate->plan;
2226 dpns->outer_plan = NULL;
2229 * For a SubqueryScan, pretend the subplan is INNER referent. (We don't
2230 * use OUTER because that could someday conflict with the normal meaning.)
2231 * Likewise, for a CteScan, pretend the subquery's plan is INNER referent.
2233 if (IsA(ps, SubqueryScanState))
2234 dpns->inner_planstate = ((SubqueryScanState *) ps)->subplan;
2235 else if (IsA(ps, CteScanState))
2236 dpns->inner_planstate = ((CteScanState *) ps)->cteplanstate;
2238 dpns->inner_planstate = innerPlanState(ps);
2240 if (dpns->inner_planstate)
2241 dpns->inner_plan = dpns->inner_planstate->plan;
2243 dpns->inner_plan = NULL;
2247 * push_child_plan: temporarily transfer deparsing attention to a child plan
2249 * When expanding an OUTER or INNER reference, we must adjust the deparse
2250 * context in case the referenced expression itself uses OUTER/INNER. We
2251 * modify the top stack entry in-place to avoid affecting levelsup issues
2252 * (although in a Plan tree there really shouldn't be any).
2254 * Caller must provide a local deparse_namespace variable to save the
2255 * previous state for pop_child_plan.
2258 push_child_plan(deparse_namespace *dpns, PlanState *ps,
2259 deparse_namespace *save_dpns)
2261 /* Save state for restoration later */
2265 * Currently we don't bother to adjust the ancestors list, because an
2266 * OUTER or INNER reference really shouldn't contain any Params that would
2267 * be set by the parent node itself. If we did want to adjust it,
2268 * lcons'ing dpns->planstate onto dpns->ancestors would be the appropriate
2269 * thing --- and pop_child_plan would need to undo the change to the list.
2272 /* Set attention on selected child */
2273 set_deparse_planstate(dpns, ps);
2277 * pop_child_plan: undo the effects of push_child_plan
2280 pop_child_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
2282 /* Restore fields changed by push_child_plan */
2287 * push_ancestor_plan: temporarily transfer deparsing attention to an
2290 * When expanding a Param reference, we must adjust the deparse context
2291 * to match the plan node that contains the expression being printed;
2292 * otherwise we'd fail if that expression itself contains a Param or
2293 * OUTER/INNER variables.
2295 * The target ancestor is conveniently identified by the ListCell holding it
2296 * in dpns->ancestors.
2298 * Caller must provide a local deparse_namespace variable to save the
2299 * previous state for pop_ancestor_plan.
2302 push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
2303 deparse_namespace *save_dpns)
2305 PlanState *ps = (PlanState *) lfirst(ancestor_cell);
2308 /* Save state for restoration later */
2311 /* Build a new ancestor list with just this node's ancestors */
2313 while ((ancestor_cell = lnext(ancestor_cell)) != NULL)
2314 ancestors = lappend(ancestors, lfirst(ancestor_cell));
2315 dpns->ancestors = ancestors;
2317 /* Set attention on selected ancestor */
2318 set_deparse_planstate(dpns, ps);
2322 * pop_ancestor_plan: undo the effects of push_ancestor_plan
2325 pop_ancestor_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
2327 /* Free the ancestor list made in push_ancestor_plan */
2328 list_free(dpns->ancestors);
2330 /* Restore fields changed by push_ancestor_plan */
2336 * make_ruledef - reconstruct the CREATE RULE command
2337 * for a given pg_rewrite tuple
2341 make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
2351 List *actions = NIL;
2357 * Get the attribute values from the rules tuple
2359 fno = SPI_fnumber(rulettc, "rulename");
2360 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
2362 rulename = NameStr(*(DatumGetName(dat)));
2364 fno = SPI_fnumber(rulettc, "ev_type");
2365 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
2367 ev_type = DatumGetChar(dat);
2369 fno = SPI_fnumber(rulettc, "ev_class");
2370 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
2372 ev_class = DatumGetObjectId(dat);
2374 fno = SPI_fnumber(rulettc, "ev_attr");
2375 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
2377 ev_attr = DatumGetInt16(dat);
2379 fno = SPI_fnumber(rulettc, "is_instead");
2380 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
2382 is_instead = DatumGetBool(dat);
2384 /* these could be nulls */
2385 fno = SPI_fnumber(rulettc, "ev_qual");
2386 ev_qual = SPI_getvalue(ruletup, rulettc, fno);
2388 fno = SPI_fnumber(rulettc, "ev_action");
2389 ev_action = SPI_getvalue(ruletup, rulettc, fno);
2390 if (ev_action != NULL)
2391 actions = (List *) stringToNode(ev_action);
2394 * Build the rules definition text
2396 appendStringInfo(buf, "CREATE RULE %s AS",
2397 quote_identifier(rulename));
2399 if (prettyFlags & PRETTYFLAG_INDENT)
2400 appendStringInfoString(buf, "\n ON ");
2402 appendStringInfoString(buf, " ON ");
2404 /* The event the rule is fired for */
2408 appendStringInfo(buf, "SELECT");
2412 appendStringInfo(buf, "UPDATE");
2416 appendStringInfo(buf, "INSERT");
2420 appendStringInfo(buf, "DELETE");
2425 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2426 errmsg("rule \"%s\" has unsupported event type %d",
2427 rulename, ev_type)));
2431 /* The relation the rule is fired on */
2432 appendStringInfo(buf, " TO %s", generate_relation_name(ev_class, NIL));
2434 appendStringInfo(buf, ".%s",
2435 quote_identifier(get_relid_attribute_name(ev_class,
2438 /* If the rule has an event qualification, add it */
2439 if (ev_qual == NULL)
2441 if (strlen(ev_qual) > 0 && strcmp(ev_qual, "<>") != 0)
2445 deparse_context context;
2446 deparse_namespace dpns;
2448 if (prettyFlags & PRETTYFLAG_INDENT)
2449 appendStringInfoString(buf, "\n ");
2450 appendStringInfo(buf, " WHERE ");
2452 qual = stringToNode(ev_qual);
2455 * We need to make a context for recognizing any Vars in the qual
2456 * (which can only be references to OLD and NEW). Use the rtable of
2457 * the first query in the action list for this purpose.
2459 query = (Query *) linitial(actions);
2462 * If the action is INSERT...SELECT, OLD/NEW have been pushed down
2463 * into the SELECT, and that's what we need to look at. (Ugly kluge
2464 * ... try to fix this when we redesign querytrees.)
2466 query = getInsertSelectQuery(query, NULL);
2468 /* Must acquire locks right away; see notes in get_query_def() */
2469 AcquireRewriteLocks(query, false);
2472 context.namespaces = list_make1(&dpns);
2473 context.windowClause = NIL;
2474 context.windowTList = NIL;
2475 context.varprefix = (list_length(query->rtable) != 1);
2476 context.prettyFlags = prettyFlags;
2477 context.indentLevel = PRETTYINDENT_STD;
2479 memset(&dpns, 0, sizeof(dpns));
2480 dpns.rtable = query->rtable;
2481 dpns.ctes = query->cteList;
2483 get_rule_expr(qual, &context, false);
2486 appendStringInfo(buf, " DO ");
2488 /* The INSTEAD keyword (if so) */
2490 appendStringInfo(buf, "INSTEAD ");
2492 /* Finally the rules actions */
2493 if (list_length(actions) > 1)
2498 appendStringInfo(buf, "(");
2499 foreach(action, actions)
2501 query = (Query *) lfirst(action);
2502 get_query_def(query, buf, NIL, NULL, prettyFlags, 0);
2504 appendStringInfo(buf, ";\n");
2506 appendStringInfo(buf, "; ");
2508 appendStringInfo(buf, ");");
2510 else if (list_length(actions) == 0)
2512 appendStringInfo(buf, "NOTHING;");
2518 query = (Query *) linitial(actions);
2519 get_query_def(query, buf, NIL, NULL, prettyFlags, 0);
2520 appendStringInfo(buf, ";");
2526 * make_viewdef - reconstruct the SELECT part of a
2531 make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
2541 List *actions = NIL;
2542 Relation ev_relation;
2547 * Get the attribute values from the rules tuple
2549 fno = SPI_fnumber(rulettc, "ev_type");
2550 ev_type = (char) SPI_getbinval(ruletup, rulettc, fno, &isnull);
2552 fno = SPI_fnumber(rulettc, "ev_class");
2553 ev_class = (Oid) SPI_getbinval(ruletup, rulettc, fno, &isnull);
2555 fno = SPI_fnumber(rulettc, "ev_attr");
2556 ev_attr = (int2) SPI_getbinval(ruletup, rulettc, fno, &isnull);
2558 fno = SPI_fnumber(rulettc, "is_instead");
2559 is_instead = (bool) SPI_getbinval(ruletup, rulettc, fno, &isnull);
2561 fno = SPI_fnumber(rulettc, "ev_qual");
2562 ev_qual = SPI_getvalue(ruletup, rulettc, fno);
2564 fno = SPI_fnumber(rulettc, "ev_action");
2565 ev_action = SPI_getvalue(ruletup, rulettc, fno);
2566 if (ev_action != NULL)
2567 actions = (List *) stringToNode(ev_action);
2569 if (list_length(actions) != 1)
2571 appendStringInfo(buf, "Not a view");
2575 query = (Query *) linitial(actions);
2577 if (ev_type != '1' || ev_attr >= 0 || !is_instead ||
2578 strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
2580 appendStringInfo(buf, "Not a view");
2584 ev_relation = heap_open(ev_class, AccessShareLock);
2586 get_query_def(query, buf, NIL, RelationGetDescr(ev_relation),
2588 appendStringInfo(buf, ";");
2590 heap_close(ev_relation, AccessShareLock);
2595 * get_query_def - Parse back one query parsetree
2597 * If resultDesc is not NULL, then it is the output tuple descriptor for
2598 * the view represented by a SELECT query.
2602 get_query_def(Query *query, StringInfo buf, List *parentnamespace,
2603 TupleDesc resultDesc, int prettyFlags, int startIndent)
2605 deparse_context context;
2606 deparse_namespace dpns;
2609 * Before we begin to examine the query, acquire locks on referenced
2610 * relations, and fix up deleted columns in JOIN RTEs. This ensures
2611 * consistent results. Note we assume it's OK to scribble on the passed
2614 AcquireRewriteLocks(query, false);
2617 context.namespaces = lcons(&dpns, list_copy(parentnamespace));
2618 context.windowClause = NIL;
2619 context.windowTList = NIL;
2620 context.varprefix = (parentnamespace != NIL ||
2621 list_length(query->rtable) != 1);
2622 context.prettyFlags = prettyFlags;
2623 context.indentLevel = startIndent;
2625 memset(&dpns, 0, sizeof(dpns));
2626 dpns.rtable = query->rtable;
2627 dpns.ctes = query->cteList;
2629 switch (query->commandType)
2632 get_select_query_def(query, &context, resultDesc);
2636 get_update_query_def(query, &context);
2640 get_insert_query_def(query, &context);
2644 get_delete_query_def(query, &context);
2648 appendStringInfo(buf, "NOTHING");
2652 get_utility_query_def(query, &context);
2656 elog(ERROR, "unrecognized query command type: %d",
2657 query->commandType);
2663 * get_values_def - Parse back a VALUES list
2667 get_values_def(List *values_lists, deparse_context *context)
2669 StringInfo buf = context->buf;
2670 bool first_list = true;
2673 appendStringInfoString(buf, "VALUES ");
2675 foreach(vtl, values_lists)
2677 List *sublist = (List *) lfirst(vtl);
2678 bool first_col = true;
2684 appendStringInfoString(buf, ", ");
2686 appendStringInfoChar(buf, '(');
2687 foreach(lc, sublist)
2689 Node *col = (Node *) lfirst(lc);
2694 appendStringInfoChar(buf, ',');
2697 * Strip any top-level nodes representing indirection assignments,
2698 * then print the result.
2700 get_rule_expr(processIndirection(col, context, false),
2703 appendStringInfoChar(buf, ')');
2708 * get_with_clause - Parse back a WITH clause
2712 get_with_clause(Query *query, deparse_context *context)
2714 StringInfo buf = context->buf;
2718 if (query->cteList == NIL)
2721 if (PRETTY_INDENT(context))
2723 context->indentLevel += PRETTYINDENT_STD;
2724 appendStringInfoChar(buf, ' ');
2727 if (query->hasRecursive)
2728 sep = "WITH RECURSIVE ";
2731 foreach(l, query->cteList)
2733 CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
2735 appendStringInfoString(buf, sep);
2736 appendStringInfoString(buf, quote_identifier(cte->ctename));
2737 if (cte->aliascolnames)
2742 appendStringInfoChar(buf, '(');
2743 foreach(col, cte->aliascolnames)
2748 appendStringInfoString(buf, ", ");
2749 appendStringInfoString(buf,
2750 quote_identifier(strVal(lfirst(col))));
2752 appendStringInfoChar(buf, ')');
2754 appendStringInfoString(buf, " AS (");
2755 if (PRETTY_INDENT(context))
2756 appendContextKeyword(context, "", 0, 0, 0);
2757 get_query_def((Query *) cte->ctequery, buf, context->namespaces, NULL,
2758 context->prettyFlags, context->indentLevel);
2759 if (PRETTY_INDENT(context))
2760 appendContextKeyword(context, "", 0, 0, 0);
2761 appendStringInfoChar(buf, ')');
2765 if (PRETTY_INDENT(context))
2767 context->indentLevel -= PRETTYINDENT_STD;
2768 appendContextKeyword(context, "", 0, 0, 0);
2771 appendStringInfoChar(buf, ' ');
2775 * get_select_query_def - Parse back a SELECT parsetree
2779 get_select_query_def(Query *query, deparse_context *context,
2780 TupleDesc resultDesc)
2782 StringInfo buf = context->buf;
2783 List *save_windowclause;
2784 List *save_windowtlist;
2788 /* Insert the WITH clause if given */
2789 get_with_clause(query, context);
2791 /* Set up context for possible window functions */
2792 save_windowclause = context->windowClause;
2793 context->windowClause = query->windowClause;
2794 save_windowtlist = context->windowTList;
2795 context->windowTList = query->targetList;
2798 * If the Query node has a setOperations tree, then it's the top level of
2799 * a UNION/INTERSECT/EXCEPT query; only the WITH, ORDER BY and LIMIT
2800 * fields are interesting in the top query itself.
2802 if (query->setOperations)
2804 get_setop_query(query->setOperations, query, context, resultDesc);
2805 /* ORDER BY clauses must be simple in this case */
2810 get_basic_select_query(query, context, resultDesc);
2811 force_colno = false;
2814 /* Add the ORDER BY clause if given */
2815 if (query->sortClause != NIL)
2817 appendContextKeyword(context, " ORDER BY ",
2818 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
2819 get_rule_orderby(query->sortClause, query->targetList,
2820 force_colno, context);
2823 /* Add the LIMIT clause if given */
2824 if (query->limitOffset != NULL)
2826 appendContextKeyword(context, " OFFSET ",
2827 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
2828 get_rule_expr(query->limitOffset, context, false);
2830 if (query->limitCount != NULL)
2832 appendContextKeyword(context, " LIMIT ",
2833 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
2834 if (IsA(query->limitCount, Const) &&
2835 ((Const *) query->limitCount)->constisnull)
2836 appendStringInfo(buf, "ALL");
2838 get_rule_expr(query->limitCount, context, false);
2841 /* Add FOR UPDATE/SHARE clauses if present */
2842 if (query->hasForUpdate)
2844 foreach(l, query->rowMarks)
2846 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
2847 RangeTblEntry *rte = rt_fetch(rc->rti, query->rtable);
2849 /* don't print implicit clauses */
2854 appendContextKeyword(context, " FOR UPDATE",
2855 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
2857 appendContextKeyword(context, " FOR SHARE",
2858 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
2859 appendStringInfo(buf, " OF %s",
2860 quote_identifier(rte->eref->aliasname));
2862 appendStringInfo(buf, " NOWAIT");
2866 context->windowClause = save_windowclause;
2867 context->windowTList = save_windowtlist;
2871 get_basic_select_query(Query *query, deparse_context *context,
2872 TupleDesc resultDesc)
2874 StringInfo buf = context->buf;
2878 if (PRETTY_INDENT(context))
2880 context->indentLevel += PRETTYINDENT_STD;
2881 appendStringInfoChar(buf, ' ');
2885 * If the query looks like SELECT * FROM (VALUES ...), then print just the
2886 * VALUES part. This reverses what transformValuesClause() did at parse
2887 * time. If the jointree contains just a single VALUES RTE, we assume
2888 * this case applies (without looking at the targetlist...)
2890 if (list_length(query->jointree->fromlist) == 1)
2892 RangeTblRef *rtr = (RangeTblRef *) linitial(query->jointree->fromlist);
2894 if (IsA(rtr, RangeTblRef))
2896 RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable);
2898 if (rte->rtekind == RTE_VALUES)
2900 get_values_def(rte->values_lists, context);
2907 * Build up the query string - first we say SELECT
2909 appendStringInfo(buf, "SELECT");
2911 /* Add the DISTINCT clause if given */
2912 if (query->distinctClause != NIL)
2914 if (query->hasDistinctOn)
2916 appendStringInfo(buf, " DISTINCT ON (");
2918 foreach(l, query->distinctClause)
2920 SortGroupClause *srt = (SortGroupClause *) lfirst(l);
2922 appendStringInfoString(buf, sep);
2923 get_rule_sortgroupclause(srt, query->targetList,
2927 appendStringInfo(buf, ")");
2930 appendStringInfo(buf, " DISTINCT");
2933 /* Then we tell what to select (the targetlist) */
2934 get_target_list(query->targetList, context, resultDesc);
2936 /* Add the FROM clause if needed */
2937 get_from_clause(query, " FROM ", context);
2939 /* Add the WHERE clause if given */
2940 if (query->jointree->quals != NULL)
2942 appendContextKeyword(context, " WHERE ",
2943 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
2944 get_rule_expr(query->jointree->quals, context, false);
2947 /* Add the GROUP BY clause if given */
2948 if (query->groupClause != NULL)
2950 appendContextKeyword(context, " GROUP BY ",
2951 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
2953 foreach(l, query->groupClause)
2955 SortGroupClause *grp = (SortGroupClause *) lfirst(l);
2957 appendStringInfoString(buf, sep);
2958 get_rule_sortgroupclause(grp, query->targetList,
2964 /* Add the HAVING clause if given */
2965 if (query->havingQual != NULL)
2967 appendContextKeyword(context, " HAVING ",
2968 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
2969 get_rule_expr(query->havingQual, context, false);
2972 /* Add the WINDOW clause if needed */
2973 if (query->windowClause != NIL)
2974 get_rule_windowclause(query, context);
2978 * get_target_list - Parse back a SELECT target list
2980 * This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
2984 get_target_list(List *targetList, deparse_context *context,
2985 TupleDesc resultDesc)
2987 StringInfo buf = context->buf;
2994 foreach(l, targetList)
2996 TargetEntry *tle = (TargetEntry *) lfirst(l);
3001 continue; /* ignore junk entries */
3003 appendStringInfoString(buf, sep);
3008 * We special-case Var nodes rather than using get_rule_expr. This is
3009 * needed because get_rule_expr will display a whole-row Var as
3010 * "foo.*", which is the preferred notation in most contexts, but at
3011 * the top level of a SELECT list it's not right (the parser will
3012 * expand that notation into multiple columns, yielding behavior
3013 * different from a whole-row Var). We want just "foo", instead.
3015 if (tle->expr && IsA(tle->expr, Var))
3017 attname = get_variable((Var *) tle->expr, 0, false, context);
3021 get_rule_expr((Node *) tle->expr, context, true);
3022 /* We'll show the AS name unless it's this: */
3023 attname = "?column?";
3027 * Figure out what the result column should be called. In the context
3028 * of a view, use the view's tuple descriptor (so as to pick up the
3029 * effects of any column RENAME that's been done on the view).
3030 * Otherwise, just use what we can find in the TLE.
3032 if (resultDesc && colno <= resultDesc->natts)
3033 colname = NameStr(resultDesc->attrs[colno - 1]->attname);
3035 colname = tle->resname;
3037 /* Show AS unless the column's name is correct as-is */
3038 if (colname) /* resname could be NULL */
3040 if (attname == NULL || strcmp(attname, colname) != 0)
3041 appendStringInfo(buf, " AS %s", quote_identifier(colname));
3047 get_setop_query(Node *setOp, Query *query, deparse_context *context,
3048 TupleDesc resultDesc)
3050 StringInfo buf = context->buf;
3053 if (IsA(setOp, RangeTblRef))
3055 RangeTblRef *rtr = (RangeTblRef *) setOp;
3056 RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable);
3057 Query *subquery = rte->subquery;
3059 Assert(subquery != NULL);
3060 Assert(subquery->setOperations == NULL);
3061 /* Need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y */
3062 need_paren = (subquery->cteList ||
3063 subquery->sortClause ||
3064 subquery->rowMarks ||
3065 subquery->limitOffset ||
3066 subquery->limitCount);
3068 appendStringInfoChar(buf, '(');
3069 get_query_def(subquery, buf, context->namespaces, resultDesc,
3070 context->prettyFlags, context->indentLevel);
3072 appendStringInfoChar(buf, ')');
3074 else if (IsA(setOp, SetOperationStmt))
3076 SetOperationStmt *op = (SetOperationStmt *) setOp;
3078 if (PRETTY_INDENT(context))
3080 context->indentLevel += PRETTYINDENT_STD;
3081 appendStringInfoSpaces(buf, PRETTYINDENT_STD);
3085 * We force parens whenever nesting two SetOperationStmts. There are
3086 * some cases in which parens are needed around a leaf query too, but
3087 * those are more easily handled at the next level down (see code
3090 need_paren = !IsA(op->larg, RangeTblRef);
3093 appendStringInfoChar(buf, '(');
3094 get_setop_query(op->larg, query, context, resultDesc);
3096 appendStringInfoChar(buf, ')');
3098 if (!PRETTY_INDENT(context))
3099 appendStringInfoChar(buf, ' ');
3103 appendContextKeyword(context, "UNION ",
3104 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
3106 case SETOP_INTERSECT:
3107 appendContextKeyword(context, "INTERSECT ",
3108 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
3111 appendContextKeyword(context, "EXCEPT ",
3112 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
3115 elog(ERROR, "unrecognized set op: %d",
3119 appendStringInfo(buf, "ALL ");
3121 if (PRETTY_INDENT(context))
3122 appendContextKeyword(context, "", 0, 0, 0);
3124 need_paren = !IsA(op->rarg, RangeTblRef);
3127 appendStringInfoChar(buf, '(');
3128 get_setop_query(op->rarg, query, context, resultDesc);
3130 appendStringInfoChar(buf, ')');
3132 if (PRETTY_INDENT(context))
3133 context->indentLevel -= PRETTYINDENT_STD;
3137 elog(ERROR, "unrecognized node type: %d",
3138 (int) nodeTag(setOp));
3143 * Display a sort/group clause.
3145 * Also returns the expression tree, so caller need not find it again.
3148 get_rule_sortgroupclause(SortGroupClause *srt, List *tlist, bool force_colno,
3149 deparse_context *context)
3151 StringInfo buf = context->buf;
3155 tle = get_sortgroupclause_tle(srt, tlist);
3156 expr = (Node *) tle->expr;
3159 * Use column-number form if requested by caller. Otherwise, if
3160 * expression is a constant, force it to be dumped with an explicit cast
3161 * as decoration --- this is because a simple integer constant is
3162 * ambiguous (and will be misinterpreted by findTargetlistEntry()) if we
3163 * dump it without any decoration. Otherwise, just dump the expression
3168 Assert(!tle->resjunk);
3169 appendStringInfo(buf, "%d", tle->resno);
3171 else if (expr && IsA(expr, Const))
3172 get_const_expr((Const *) expr, context, 1);
3174 get_rule_expr(expr, context, true);
3180 * Display an ORDER BY list.
3183 get_rule_orderby(List *orderList, List *targetList,
3184 bool force_colno, deparse_context *context)
3186 StringInfo buf = context->buf;
3191 foreach(l, orderList)
3193 SortGroupClause *srt = (SortGroupClause *) lfirst(l);
3196 TypeCacheEntry *typentry;
3198 appendStringInfoString(buf, sep);
3199 sortexpr = get_rule_sortgroupclause(srt, targetList,
3200 force_colno, context);
3201 sortcoltype = exprType(sortexpr);
3202 /* See whether operator is default < or > for datatype */
3203 typentry = lookup_type_cache(sortcoltype,
3204 TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
3205 if (srt->sortop == typentry->lt_opr)
3207 /* ASC is default, so emit nothing for it */
3208 if (srt->nulls_first)
3209 appendStringInfo(buf, " NULLS FIRST");
3211 else if (srt->sortop == typentry->gt_opr)
3213 appendStringInfo(buf, " DESC");
3214 /* DESC defaults to NULLS FIRST */
3215 if (!srt->nulls_first)
3216 appendStringInfo(buf, " NULLS LAST");
3220 appendStringInfo(buf, " USING %s",
3221 generate_operator_name(srt->sortop,
3224 /* be specific to eliminate ambiguity */
3225 if (srt->nulls_first)
3226 appendStringInfo(buf, " NULLS FIRST");
3228 appendStringInfo(buf, " NULLS LAST");
3235 * Display a WINDOW clause.
3237 * Note that the windowClause list might contain only anonymous window
3238 * specifications, in which case we should print nothing here.
3241 get_rule_windowclause(Query *query, deparse_context *context)
3243 StringInfo buf = context->buf;
3248 foreach(l, query->windowClause)
3250 WindowClause *wc = (WindowClause *) lfirst(l);
3252 if (wc->name == NULL)
3253 continue; /* ignore anonymous windows */
3256 appendContextKeyword(context, " WINDOW ",
3257 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
3259 appendStringInfoString(buf, sep);
3261 appendStringInfo(buf, "%s AS ", quote_identifier(wc->name));
3263 get_rule_windowspec(wc, query->targetList, context);
3270 * Display a window definition
3273 get_rule_windowspec(WindowClause *wc, List *targetList,
3274 deparse_context *context)
3276 StringInfo buf = context->buf;
3277 bool needspace = false;
3281 appendStringInfoChar(buf, '(');
3284 appendStringInfoString(buf, quote_identifier(wc->refname));
3287 /* partition clauses are always inherited, so only print if no refname */
3288 if (wc->partitionClause && !wc->refname)
3291 appendStringInfoChar(buf, ' ');
3292 appendStringInfoString(buf, "PARTITION BY ");
3294 foreach(l, wc->partitionClause)
3296 SortGroupClause *grp = (SortGroupClause *) lfirst(l);
3298 appendStringInfoString(buf, sep);
3299 get_rule_sortgroupclause(grp, targetList,
3305 /* print ordering clause only if not inherited */
3306 if (wc->orderClause && !wc->copiedOrder)
3309 appendStringInfoChar(buf, ' ');
3310 appendStringInfoString(buf, "ORDER BY ");
3311 get_rule_orderby(wc->orderClause, targetList, false, context);
3314 /* framing clause is never inherited, so print unless it's default */
3315 if (wc->frameOptions & FRAMEOPTION_NONDEFAULT)
3318 appendStringInfoChar(buf, ' ');
3319 if (wc->frameOptions & FRAMEOPTION_RANGE)
3320 appendStringInfoString(buf, "RANGE ");
3321 else if (wc->frameOptions & FRAMEOPTION_ROWS)
3322 appendStringInfoString(buf, "ROWS ");
3325 if (wc->frameOptions & FRAMEOPTION_BETWEEN)
3326 appendStringInfoString(buf, "BETWEEN ");
3327 if (wc->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
3328 appendStringInfoString(buf, "UNBOUNDED PRECEDING ");
3329 else if (wc->frameOptions & FRAMEOPTION_START_CURRENT_ROW)
3330 appendStringInfoString(buf, "CURRENT ROW ");
3331 else if (wc->frameOptions & FRAMEOPTION_START_VALUE)
3333 get_rule_expr(wc->startOffset, context, false);
3334 if (wc->frameOptions & FRAMEOPTION_START_VALUE_PRECEDING)
3335 appendStringInfoString(buf, " PRECEDING ");
3336 else if (wc->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
3337 appendStringInfoString(buf, " FOLLOWING ");
3343 if (wc->frameOptions & FRAMEOPTION_BETWEEN)
3345 appendStringInfoString(buf, "AND ");
3346 if (wc->frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
3347 appendStringInfoString(buf, "UNBOUNDED FOLLOWING ");
3348 else if (wc->frameOptions & FRAMEOPTION_END_CURRENT_ROW)
3349 appendStringInfoString(buf, "CURRENT ROW ");
3350 else if (wc->frameOptions & FRAMEOPTION_END_VALUE)
3352 get_rule_expr(wc->endOffset, context, false);
3353 if (wc->frameOptions & FRAMEOPTION_END_VALUE_PRECEDING)
3354 appendStringInfoString(buf, " PRECEDING ");
3355 else if (wc->frameOptions & FRAMEOPTION_END_VALUE_FOLLOWING)
3356 appendStringInfoString(buf, " FOLLOWING ");
3363 /* we will now have a trailing space; remove it */
3366 appendStringInfoChar(buf, ')');
3370 * get_insert_query_def - Parse back an INSERT parsetree
3374 get_insert_query_def(Query *query, deparse_context *context)
3376 StringInfo buf = context->buf;
3377 RangeTblEntry *select_rte = NULL;
3378 RangeTblEntry *values_rte = NULL;
3381 ListCell *values_cell;
3383 List *strippedexprs;
3385 /* Insert the WITH clause if given */
3386 get_with_clause(query, context);
3389 * If it's an INSERT ... SELECT or VALUES (...), (...), ... there will be
3390 * a single RTE for the SELECT or VALUES.
3392 foreach(l, query->rtable)
3394 rte = (RangeTblEntry *) lfirst(l);
3396 if (rte->rtekind == RTE_SUBQUERY)
3399 elog(ERROR, "too many subquery RTEs in INSERT");
3403 if (rte->rtekind == RTE_VALUES)
3406 elog(ERROR, "too many values RTEs in INSERT");
3410 if (select_rte && values_rte)
3411 elog(ERROR, "both subquery and values RTEs in INSERT");
3414 * Start the query with INSERT INTO relname
3416 rte = rt_fetch(query->resultRelation, query->rtable);
3417 Assert(rte->rtekind == RTE_RELATION);
3419 if (PRETTY_INDENT(context))
3421 context->indentLevel += PRETTYINDENT_STD;
3422 appendStringInfoChar(buf, ' ');
3424 appendStringInfo(buf, "INSERT INTO %s (",
3425 generate_relation_name(rte->relid, NIL));
3428 * Add the insert-column-names list. To handle indirection properly, we
3429 * need to look for indirection nodes in the top targetlist (if it's
3430 * INSERT ... SELECT or INSERT ... single VALUES), or in the first
3431 * expression list of the VALUES RTE (if it's INSERT ... multi VALUES). We
3432 * assume that all the expression lists will have similar indirection in
3436 values_cell = list_head((List *) linitial(values_rte->values_lists));
3439 strippedexprs = NIL;
3441 foreach(l, query->targetList)
3443 TargetEntry *tle = (TargetEntry *) lfirst(l);
3446 continue; /* ignore junk entries */
3448 appendStringInfoString(buf, sep);
3452 * Put out name of target column; look in the catalogs, not at
3453 * tle->resname, since resname will fail to track RENAME.
3455 appendStringInfoString(buf,
3456 quote_identifier(get_relid_attribute_name(rte->relid,
3460 * Print any indirection needed (subfields or subscripts), and strip
3461 * off the top-level nodes representing the indirection assignments.
3465 /* we discard the stripped expression in this case */
3466 processIndirection((Node *) lfirst(values_cell), context, true);
3467 values_cell = lnext(values_cell);
3471 /* we keep a list of the stripped expressions in this case */
3472 strippedexprs = lappend(strippedexprs,
3473 processIndirection((Node *) tle->expr,
3477 appendStringInfo(buf, ") ");
3481 /* Add the SELECT */
3482 get_query_def(select_rte->subquery, buf, NIL, NULL,
3483 context->prettyFlags, context->indentLevel);
3485 else if (values_rte)
3487 /* Add the multi-VALUES expression lists */
3488 get_values_def(values_rte->values_lists, context);
3492 /* Add the single-VALUES expression list */
3493 appendContextKeyword(context, "VALUES (",
3494 -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
3495 get_rule_expr((Node *) strippedexprs, context, false);
3496 appendStringInfoChar(buf, ')');
3499 /* Add RETURNING if present */
3500 if (query->returningList)
3502 appendContextKeyword(context, " RETURNING",
3503 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
3504 get_target_list(query->returningList, context, NULL);
3510 * get_update_query_def - Parse back an UPDATE parsetree
3514 get_update_query_def(Query *query, deparse_context *context)
3516 StringInfo buf = context->buf;
3521 /* Insert the WITH clause if given */
3522 get_with_clause(query, context);
3525 * Start the query with UPDATE relname SET
3527 rte = rt_fetch(query->resultRelation, query->rtable);
3528 Assert(rte->rtekind == RTE_RELATION);
3529 if (PRETTY_INDENT(context))
3531 appendStringInfoChar(buf, ' ');
3532 context->indentLevel += PRETTYINDENT_STD;
3534 appendStringInfo(buf, "UPDATE %s%s",
3536 generate_relation_name(rte->relid, NIL));
3537 if (rte->alias != NULL)
3538 appendStringInfo(buf, " %s",
3539 quote_identifier(rte->alias->aliasname));
3540 appendStringInfoString(buf, " SET ");
3542 /* Add the comma separated list of 'attname = value' */
3544 foreach(l, query->targetList)
3546 TargetEntry *tle = (TargetEntry *) lfirst(l);
3550 continue; /* ignore junk entries */
3552 appendStringInfoString(buf, sep);
3556 * Put out name of target column; look in the catalogs, not at
3557 * tle->resname, since resname will fail to track RENAME.
3559 appendStringInfoString(buf,
3560 quote_identifier(get_relid_attribute_name(rte->relid,
3564 * Print any indirection needed (subfields or subscripts), and strip
3565 * off the top-level nodes representing the indirection assignments.
3567 expr = processIndirection((Node *) tle->expr, context, true);
3569 appendStringInfo(buf, " = ");
3571 get_rule_expr(expr, context, false);
3574 /* Add the FROM clause if needed */
3575 get_from_clause(query, " FROM ", context);
3577 /* Add a WHERE clause if given */
3578 if (query->jointree->quals != NULL)
3580 appendContextKeyword(context, " WHERE ",
3581 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
3582 get_rule_expr(query->jointree->quals, context, false);
3585 /* Add RETURNING if present */
3586 if (query->returningList)
3588 appendContextKeyword(context, " RETURNING",
3589 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
3590 get_target_list(query->returningList, context, NULL);
3596 * get_delete_query_def - Parse back a DELETE parsetree
3600 get_delete_query_def(Query *query, deparse_context *context)
3602 StringInfo buf = context->buf;
3605 /* Insert the WITH clause if given */
3606 get_with_clause(query, context);
3609 * Start the query with DELETE FROM relname
3611 rte = rt_fetch(query->resultRelation, query->rtable);
3612 Assert(rte->rtekind == RTE_RELATION);
3613 if (PRETTY_INDENT(context))
3615 appendStringInfoChar(buf, ' ');
3616 context->indentLevel += PRETTYINDENT_STD;
3618 appendStringInfo(buf, "DELETE FROM %s%s",
3620 generate_relation_name(rte->relid, NIL));
3621 if (rte->alias != NULL)
3622 appendStringInfo(buf, " %s",
3623 quote_identifier(rte->alias->aliasname));
3625 /* Add the USING clause if given */
3626 get_from_clause(query, " USING ", context);
3628 /* Add a WHERE clause if given */
3629 if (query->jointree->quals != NULL)
3631 appendContextKeyword(context, " WHERE ",
3632 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
3633 get_rule_expr(query->jointree->quals, context, false);
3636 /* Add RETURNING if present */
3637 if (query->returningList)
3639 appendContextKeyword(context, " RETURNING",
3640 -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
3641 get_target_list(query->returningList, context, NULL);
3647 * get_utility_query_def - Parse back a UTILITY parsetree
3651 get_utility_query_def(Query *query, deparse_context *context)
3653 StringInfo buf = context->buf;
3655 if (query->utilityStmt && IsA(query->utilityStmt, NotifyStmt))
3657 NotifyStmt *stmt = (NotifyStmt *) query->utilityStmt;
3659 appendContextKeyword(context, "",
3660 0, PRETTYINDENT_STD, 1);
3661 appendStringInfo(buf, "NOTIFY %s",
3662 quote_identifier(stmt->conditionname));
3665 appendStringInfoString(buf, ", ");
3666 simple_quote_literal(buf, stmt->payload);
3671 /* Currently only NOTIFY utility commands can appear in rules */
3672 elog(ERROR, "unexpected utility statement type");
3678 * Display a Var appropriately.
3680 * In some cases (currently only when recursing into an unnamed join)
3681 * the Var's varlevelsup has to be interpreted with respect to a context
3682 * above the current one; levelsup indicates the offset.
3684 * If showstar is TRUE, whole-row Vars are displayed as "foo.*";
3685 * if FALSE, merely as "foo".
3687 * Returns the attname of the Var, or NULL if not determinable.
3690 get_variable(Var *var, int levelsup, bool showstar, deparse_context *context)
3692 StringInfo buf = context->buf;
3696 deparse_namespace *dpns;
3701 /* Find appropriate nesting depth */
3702 netlevelsup = var->varlevelsup + levelsup;
3703 if (netlevelsup >= list_length(context->namespaces))
3704 elog(ERROR, "bogus varlevelsup: %d offset %d",
3705 var->varlevelsup, levelsup);
3706 dpns = (deparse_namespace *) list_nth(context->namespaces,
3710 * Try to find the relevant RTE in this rtable. In a plan tree, it's
3711 * likely that varno is OUTER or INNER, in which case we must dig down
3712 * into the subplans.
3714 if (var->varno >= 1 && var->varno <= list_length(dpns->rtable))
3716 rte = rt_fetch(var->varno, dpns->rtable);
3717 attnum = var->varattno;
3719 else if (var->varno == OUTER && dpns->outer_plan)
3722 deparse_namespace save_dpns;
3724 tle = get_tle_by_resno(dpns->outer_plan->targetlist, var->varattno);
3726 elog(ERROR, "bogus varattno for OUTER var: %d", var->varattno);
3728 Assert(netlevelsup == 0);
3729 push_child_plan(dpns, dpns->outer_planstate, &save_dpns);
3732 * Force parentheses because our caller probably assumed a Var is a
3733 * simple expression.
3735 if (!IsA(tle->expr, Var))
3736 appendStringInfoChar(buf, '(');
3737 get_rule_expr((Node *) tle->expr, context, true);
3738 if (!IsA(tle->expr, Var))
3739 appendStringInfoChar(buf, ')');
3741 pop_child_plan(dpns, &save_dpns);
3744 else if (var->varno == INNER && dpns->inner_plan)
3747 deparse_namespace save_dpns;
3749 tle = get_tle_by_resno(dpns->inner_plan->targetlist, var->varattno);
3751 elog(ERROR, "bogus varattno for INNER var: %d", var->varattno);
3753 Assert(netlevelsup == 0);
3754 push_child_plan(dpns, dpns->inner_planstate, &save_dpns);
3757 * Force parentheses because our caller probably assumed a Var is a
3758 * simple expression.
3760 if (!IsA(tle->expr, Var))
3761 appendStringInfoChar(buf, '(');
3762 get_rule_expr((Node *) tle->expr, context, true);
3763 if (!IsA(tle->expr, Var))
3764 appendStringInfoChar(buf, ')');
3766 pop_child_plan(dpns, &save_dpns);
3771 elog(ERROR, "bogus varno: %d", var->varno);
3772 return NULL; /* keep compiler quiet */
3776 * The planner will sometimes emit Vars referencing resjunk elements of a
3777 * subquery's target list (this is currently only possible if it chooses
3778 * to generate a "physical tlist" for a SubqueryScan or CteScan node).
3779 * Although we prefer to print subquery-referencing Vars using the
3780 * subquery's alias, that's not possible for resjunk items since they have
3781 * no alias. So in that case, drill down to the subplan and print the
3782 * contents of the referenced tlist item. This works because in a plan
3783 * tree, such Vars can only occur in a SubqueryScan or CteScan node, and
3784 * we'll have set dpns->inner_plan to reference the child plan node.
3786 if ((rte->rtekind == RTE_SUBQUERY || rte->rtekind == RTE_CTE) &&
3787 attnum > list_length(rte->eref->colnames) &&
3791 deparse_namespace save_dpns;
3793 tle = get_tle_by_resno(dpns->inner_plan->targetlist, var->varattno);
3795 elog(ERROR, "bogus varattno for subquery var: %d", var->varattno);
3797 Assert(netlevelsup == 0);
3798 push_child_plan(dpns, dpns->inner_planstate, &save_dpns);
3801 * Force parentheses because our caller probably assumed a Var is a
3802 * simple expression.
3804 if (!IsA(tle->expr, Var))
3805 appendStringInfoChar(buf, '(');
3806 get_rule_expr((Node *) tle->expr, context, true);
3807 if (!IsA(tle->expr, Var))
3808 appendStringInfoChar(buf, ')');
3810 pop_child_plan(dpns, &save_dpns);
3814 /* Identify names to use */
3815 schemaname = NULL; /* default assumptions */
3816 refname = rte->eref->aliasname;
3818 /* Exceptions occur only if the RTE is alias-less */
3819 if (rte->alias == NULL)
3821 if (rte->rtekind == RTE_RELATION)
3824 * It's possible that use of the bare refname would find another
3825 * more-closely-nested RTE, or be ambiguous, in which case we need
3826 * to specify the schemaname to avoid these errors.
3828 if (find_rte_by_refname(rte->eref->aliasname, context) != rte)
3829 schemaname = get_namespace_name(get_rel_namespace(rte->relid));
3831 else if (rte->rtekind == RTE_JOIN)
3834 * If it's an unnamed join, look at the expansion of the alias
3835 * variable. If it's a simple reference to one of the input vars
3836 * then recursively print the name of that var, instead. (This
3837 * allows correct decompiling of cases where there are identically
3838 * named columns on both sides of the join.) When it's not a
3839 * simple reference, we have to just print the unqualified
3840 * variable name (this can only happen with columns that were
3841 * merged by USING or NATURAL clauses).
3843 * This wouldn't work in decompiling plan trees, because we don't
3844 * store joinaliasvars lists after planning; but a plan tree
3845 * should never contain a join alias variable.
3847 if (rte->joinaliasvars == NIL)
3848 elog(ERROR, "cannot decompile join alias var in plan tree");
3853 aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3854 if (IsA(aliasvar, Var))
3856 return get_variable(aliasvar, var->varlevelsup + levelsup,
3860 /* Unnamed join has neither schemaname nor refname */
3865 if (attnum == InvalidAttrNumber)
3868 attname = get_rte_attribute_name(rte, attnum);
3870 if (refname && (context->varprefix || attname == NULL))
3873 appendStringInfo(buf, "%s.",
3874 quote_identifier(schemaname));
3875 appendStringInfoString(buf, quote_identifier(refname));
3876 if (attname || showstar)
3877 appendStringInfoChar(buf, '.');
3880 appendStringInfoString(buf, quote_identifier(attname));
3882 appendStringInfoChar(buf, '*');
3889 * Get the name of a field of an expression of composite type.
3891 * This is fairly straightforward except for the case of a Var of type RECORD.
3892 * Since no actual table or view column is allowed to have type RECORD, such
3893 * a Var must refer to a JOIN or FUNCTION RTE or to a subquery output. We
3894 * drill down to find the ultimate defining expression and attempt to infer
3895 * the field name from it. We ereport if we can't determine the name.
3897 * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
3900 get_name_for_var_field(Var *var, int fieldno,
3901 int levelsup, deparse_context *context)
3906 deparse_namespace *dpns;
3907 TupleDesc tupleDesc;
3911 * If it's a RowExpr that was expanded from a whole-row Var, use the
3912 * column names attached to it.
3914 if (IsA(var, RowExpr))
3916 RowExpr *r = (RowExpr *) var;
3918 if (fieldno > 0 && fieldno <= list_length(r->colnames))
3919 return strVal(list_nth(r->colnames, fieldno - 1));
3923 * If it's a Var of type RECORD, we have to find what the Var refers to;
3924 * if not, we can use get_expr_result_type. If that fails, we try
3925 * lookup_rowtype_tupdesc, which will probably fail too, but will ereport
3926 * an acceptable message.
3928 if (!IsA(var, Var) ||
3929 var->vartype != RECORDOID)
3931 if (get_expr_result_type((Node *) var, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
3932 tupleDesc = lookup_rowtype_tupdesc_copy(exprType((Node *) var),
3933 exprTypmod((Node *) var));
3935 /* Got the tupdesc, so we can extract the field name */
3936 Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
3937 return NameStr(tupleDesc->attrs[fieldno - 1]->attname);
3940 /* Find appropriate nesting depth */
3941 netlevelsup = var->varlevelsup + levelsup;
3942 if (netlevelsup >= list_length(context->namespaces))
3943 elog(ERROR, "bogus varlevelsup: %d offset %d",
3944 var->varlevelsup, levelsup);
3945 dpns = (deparse_namespace *) list_nth(context->namespaces,
3949 * Try to find the relevant RTE in this rtable. In a plan tree, it's
3950 * likely that varno is OUTER or INNER, in which case we must dig down
3951 * into the subplans.
3953 if (var->varno >= 1 && var->varno <= list_length(dpns->rtable))
3955 rte = rt_fetch(var->varno, dpns->rtable);
3956 attnum = var->varattno;
3958 else if (var->varno == OUTER && dpns->outer_plan)
3961 deparse_namespace save_dpns;
3964 tle = get_tle_by_resno(dpns->outer_plan->targetlist, var->varattno);
3966 elog(ERROR, "bogus varattno for OUTER var: %d", var->varattno);
3968 Assert(netlevelsup == 0);
3969 push_child_plan(dpns, dpns->outer_planstate, &save_dpns);
3971 result = get_name_for_var_field((Var *) tle->expr, fieldno,
3974 pop_child_plan(dpns, &save_dpns);
3977 else if (var->varno == INNER && dpns->inner_plan)
3980 deparse_namespace save_dpns;
3983 tle = get_tle_by_resno(dpns->inner_plan->targetlist, var->varattno);
3985 elog(ERROR, "bogus varattno for INNER var: %d", var->varattno);
3987 Assert(netlevelsup == 0);
3988 push_child_plan(dpns, dpns->inner_planstate, &save_dpns);
3990 result = get_name_for_var_field((Var *) tle->expr, fieldno,
3993 pop_child_plan(dpns, &save_dpns);
3998 elog(ERROR, "bogus varno: %d", var->varno);
3999 return NULL; /* keep compiler quiet */
4002 if (attnum == InvalidAttrNumber)
4004 /* Var is whole-row reference to RTE, so select the right field */
4005 return get_rte_attribute_name(rte, fieldno);
4009 * This part has essentially the same logic as the parser's
4010 * expandRecordVariable() function, but we are dealing with a different
4011 * representation of the input context, and we only need one field name
4012 * not a TupleDesc. Also, we need special cases for finding subquery and
4013 * CTE subplans when deparsing Plan trees.
4015 expr = (Node *) var; /* default if we can't drill down */
4017 switch (rte->rtekind)
4023 * This case should not occur: a column of a table or values list
4024 * shouldn't have type RECORD. Fall through and fail (most
4025 * likely) at the bottom.
4029 /* Subselect-in-FROM: examine sub-select's output expr */
4033 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
4036 if (ste == NULL || ste->resjunk)
4037 elog(ERROR, "subquery %s does not have attribute %d",
4038 rte->eref->aliasname, attnum);
4039 expr = (Node *) ste->expr;
4043 * Recurse into the sub-select to see what its Var
4044 * refers to. We have to build an additional level of
4045 * namespace to keep in step with varlevelsup in the
4048 deparse_namespace mydpns;
4051 memset(&mydpns, 0, sizeof(mydpns));
4052 mydpns.rtable = rte->subquery->rtable;
4053 mydpns.ctes = rte->subquery->cteList;
4055 context->namespaces = lcons(&mydpns,
4056 context->namespaces);
4058 result = get_name_for_var_field((Var *) expr, fieldno,
4061 context->namespaces =
4062 list_delete_first(context->namespaces);
4066 /* else fall through to inspect the expression */
4071 * We're deparsing a Plan tree so we don't have complete
4072 * RTE entries (in particular, rte->subquery is NULL). But
4073 * the only place we'd see a Var directly referencing a
4074 * SUBQUERY RTE is in a SubqueryScan plan node, and we can
4075 * look into the child plan's tlist instead.
4078 deparse_namespace save_dpns;
4081 if (!dpns->inner_plan)
4082 elog(ERROR, "failed to find plan for subquery %s",
4083 rte->eref->aliasname);
4084 tle = get_tle_by_resno(dpns->inner_plan->targetlist,
4087 elog(ERROR, "bogus varattno for subquery var: %d",
4089 Assert(netlevelsup == 0);
4090 push_child_plan(dpns, dpns->inner_planstate, &save_dpns);
4092 result = get_name_for_var_field((Var *) tle->expr, fieldno,
4095 pop_child_plan(dpns, &save_dpns);
4101 /* Join RTE --- recursively inspect the alias variable */
4102 if (rte->joinaliasvars == NIL)
4103 elog(ERROR, "cannot decompile join alias var in plan tree");
4104 Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
4105 expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
4107 return get_name_for_var_field((Var *) expr, fieldno,
4108 var->varlevelsup + levelsup,
4110 /* else fall through to inspect the expression */
4115 * We couldn't get here unless a function is declared with one of
4116 * its result columns as RECORD, which is not allowed.
4120 /* CTE reference: examine subquery's output expr */
4122 CommonTableExpr *cte = NULL;
4127 * Try to find the referenced CTE using the namespace stack.
4129 ctelevelsup = rte->ctelevelsup + netlevelsup;
4130 if (ctelevelsup >= list_length(context->namespaces))
4134 deparse_namespace *ctedpns;
4136 ctedpns = (deparse_namespace *)
4137 list_nth(context->namespaces, ctelevelsup);
4138 foreach(lc, ctedpns->ctes)
4140 cte = (CommonTableExpr *) lfirst(lc);
4141 if (strcmp(cte->ctename, rte->ctename) == 0)
4147 Query *ctequery = (Query *) cte->ctequery;
4148 TargetEntry *ste = get_tle_by_resno(GetCTETargetList(cte),
4151 if (ste == NULL || ste->resjunk)
4152 elog(ERROR, "subquery %s does not have attribute %d",
4153 rte->eref->aliasname, attnum);
4154 expr = (Node *) ste->expr;
4158 * Recurse into the CTE to see what its Var refers to.
4159 * We have to build an additional level of namespace
4160 * to keep in step with varlevelsup in the CTE.
4161 * Furthermore it could be an outer CTE, so we may
4162 * have to delete some levels of namespace.
4164 List *save_nslist = context->namespaces;
4166 deparse_namespace mydpns;
4169 memset(&mydpns, 0, sizeof(mydpns));
4170 mydpns.rtable = ctequery->rtable;
4171 mydpns.ctes = ctequery->cteList;
4173 new_nslist = list_copy_tail(context->namespaces,
4175 context->namespaces = lcons(&mydpns, new_nslist);
4177 result = get_name_for_var_field((Var *) expr, fieldno,
4180 context->namespaces = save_nslist;
4184 /* else fall through to inspect the expression */
4189 * We're deparsing a Plan tree so we don't have a CTE
4190 * list. But the only place we'd see a Var directly
4191 * referencing a CTE RTE is in a CteScan plan node, and we
4192 * can look into the subplan's tlist instead.
4195 deparse_namespace save_dpns;
4198 if (!dpns->inner_plan)
4199 elog(ERROR, "failed to find plan for CTE %s",
4200 rte->eref->aliasname);
4201 tle = get_tle_by_resno(dpns->inner_plan->targetlist,
4204 elog(ERROR, "bogus varattno for subquery var: %d",
4206 Assert(netlevelsup == 0);
4207 push_child_plan(dpns, dpns->inner_planstate, &save_dpns);
4209 result = get_name_for_var_field((Var *) tle->expr, fieldno,
4212 pop_child_plan(dpns, &save_dpns);
4220 * We now have an expression we can't expand any more, so see if
4221 * get_expr_result_type() can do anything with it. If not, pass to
4222 * lookup_rowtype_tupdesc() which will probably fail, but will give an
4223 * appropriate error message while failing.
4225 if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
4226 tupleDesc = lookup_rowtype_tupdesc_copy(exprType(expr),
4229 /* Got the tupdesc, so we can extract the field name */
4230 Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
4231 return NameStr(tupleDesc->attrs[fieldno - 1]->attname);
4236 * find_rte_by_refname - look up an RTE by refname in a deparse context
4238 * Returns NULL if there is no matching RTE or the refname is ambiguous.
4240 * NOTE: this code is not really correct since it does not take account of
4241 * the fact that not all the RTEs in a rangetable may be visible from the
4242 * point where a Var reference appears. For the purposes we need, however,
4243 * the only consequence of a false match is that we might stick a schema
4244 * qualifier on a Var that doesn't really need it. So it seems close
4247 static RangeTblEntry *
4248 find_rte_by_refname(const char *refname, deparse_context *context)
4250 RangeTblEntry *result = NULL;
4253 foreach(nslist, context->namespaces)
4255 deparse_namespace *dpns = (deparse_namespace *) lfirst(nslist);
4258 foreach(rtlist, dpns->rtable)
4260 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rtlist);
4262 if (strcmp(rte->eref->aliasname, refname) == 0)
4265 return NULL; /* it's ambiguous */
4276 * Display a Param appropriately.
4279 get_parameter(Param *param, deparse_context *context)
4282 * If it's a PARAM_EXEC parameter, try to locate the expression from which
4283 * the parameter was computed. This will necessarily be in some ancestor
4284 * of the current expression's PlanState. Note that failing to find a
4285 * referent isn't an error, since the Param might well be a subplan output
4286 * rather than an input.
4288 if (param->paramkind == PARAM_EXEC)
4290 deparse_namespace *dpns;
4291 PlanState *child_ps;
4292 bool in_same_plan_level;
4295 dpns = (deparse_namespace *) linitial(context->namespaces);
4296 child_ps = dpns->planstate;
4297 in_same_plan_level = true;
4299 foreach(lc, dpns->ancestors)
4301 PlanState *ps = (PlanState *) lfirst(lc);
4305 * NestLoops transmit params to their inner child only; also, once
4306 * we've crawled up out of a subplan, this couldn't possibly be
4309 if (IsA(ps, NestLoopState) &&
4310 child_ps == innerPlanState(ps) &&
4313 NestLoop *nl = (NestLoop *) ps->plan;
4315 foreach(lc2, nl->nestParams)
4317 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc2);
4319 if (nlp->paramno == param->paramid)
4321 /* Found a match, so print it */
4322 print_parameter_expr((Node *) nlp->paramval, lc,
4330 * Check to see if we're crawling up from a subplan.
4332 foreach(lc2, ps->subPlan)
4334 SubPlanState *sstate = (SubPlanState *) lfirst(lc2);
4335 SubPlan *subplan = (SubPlan *) sstate->xprstate.expr;
4339 if (child_ps != sstate->planstate)
4342 /* Matched subplan, so check its arguments */
4343 forboth(lc3, subplan->parParam, lc4, subplan->args)
4345 int paramid = lfirst_int(lc3);
4346 Node *arg = (Node *) lfirst(lc4);
4348 if (paramid == param->paramid)
4350 /* Found a match, so print it */
4351 print_parameter_expr(arg, lc, dpns, context);
4356 /* Keep looking, but we are emerging from a subplan. */
4357 in_same_plan_level = false;
4362 * Likewise check to see if we're emerging from an initplan.
4363 * Initplans never have any parParams, so no need to search that
4364 * list, but we need to know if we should reset
4365 * in_same_plan_level.
4367 foreach(lc2, ps->initPlan)
4369 SubPlanState *sstate = (SubPlanState *) lfirst(lc2);
4371 if (child_ps != sstate->planstate)
4374 /* No parameters to be had here. */
4375 Assert(((SubPlan *) sstate->xprstate.expr)->parParam == NIL);
4377 /* Keep looking, but we are emerging from an initplan. */
4378 in_same_plan_level = false;
4382 /* No luck, crawl up to next ancestor */
4388 * Not PARAM_EXEC, or couldn't find referent: just print $N.
4390 appendStringInfo(context->buf, "$%d", param->paramid);
4393 /* Print a parameter reference expression found by get_parameter */
4395 print_parameter_expr(Node *expr, ListCell *ancestor_cell,
4396 deparse_namespace *dpns, deparse_context *context)
4398 deparse_namespace save_dpns;
4399 bool save_varprefix;
4402 /* Switch attention to the ancestor plan node */
4403 push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
4406 * Force prefixing of Vars, since they won't belong to the relation being
4407 * scanned in the original plan node.
4409 save_varprefix = context->varprefix;
4410 context->varprefix = true;
4413 * A Param's expansion is typically a Var, Aggref, or upper-level Param,
4414 * which wouldn't need extra parentheses. Otherwise, insert parens to
4415 * ensure the expression looks atomic.
4417 need_paren = !(IsA(expr, Var) ||
4418 IsA(expr, Aggref) ||
4421 appendStringInfoChar(context->buf, '(');
4423 get_rule_expr(expr, context, false);
4426 appendStringInfoChar(context->buf, ')');
4428 context->varprefix = save_varprefix;
4430 pop_ancestor_plan(dpns, &save_dpns);
4434 * get_simple_binary_op_name
4436 * helper function for isSimpleNode
4437 * will return single char binary operator name, or NULL if it's not
4440 get_simple_binary_op_name(OpExpr *expr)
4442 List *args = expr->args;
4444 if (list_length(args) == 2)
4446 /* binary operator */
4447 Node *arg1 = (Node *) linitial(args);
4448 Node *arg2 = (Node *) lsecond(args);
4451 op = generate_operator_name(expr->opno, exprType(arg1), exprType(arg2));
4452 if (strlen(op) == 1)
4460 * isSimpleNode - check if given node is simple (doesn't need parenthesizing)
4462 * true : simple in the context of parent node's type
4463 * false : not simple
4466 isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
4471 switch (nodeTag(node))
4476 case T_CoerceToDomainValue:
4477 case T_SetToDefault:
4478 case T_CurrentOfExpr:
4479 /* single words: always simple */
4485 case T_CoalesceExpr:
4492 /* function-like: name(..) or name[..] */
4495 /* CASE keywords act as parentheses */
4502 * appears simple since . has top precedence, unless parent is
4503 * T_FieldSelect itself!
4505 return (IsA(parentNode, FieldSelect) ? false : true);
4510 * treat like FieldSelect (probably doesn't matter)
4512 return (IsA(parentNode, FieldStore) ? false : true);
4514 case T_CoerceToDomain:
4515 /* maybe simple, check args */
4516 return isSimpleNode((Node *) ((CoerceToDomain *) node)->arg,
4519 return isSimpleNode((Node *) ((RelabelType *) node)->arg,
4522 return isSimpleNode((Node *) ((CoerceViaIO *) node)->arg,
4524 case T_ArrayCoerceExpr:
4525 return isSimpleNode((Node *) ((ArrayCoerceExpr *) node)->arg,
4527 case T_ConvertRowtypeExpr:
4528 return isSimpleNode((Node *) ((ConvertRowtypeExpr *) node)->arg,
4533 /* depends on parent node type; needs further checking */
4534 if (prettyFlags & PRETTYFLAG_PAREN && IsA(parentNode, OpExpr))
4537 const char *parentOp;
4540 bool is_lopriparent;
4541 bool is_hipriparent;
4543 op = get_simple_binary_op_name((OpExpr *) node);
4547 /* We know only the basic operators + - and * / % */
4548 is_lopriop = (strchr("+-", *op) != NULL);
4549 is_hipriop = (strchr("*/%", *op) != NULL);
4550 if (!(is_lopriop || is_hipriop))
4553 parentOp = get_simple_binary_op_name((OpExpr *) parentNode);
4557 is_lopriparent = (strchr("+-", *parentOp) != NULL);
4558 is_hipriparent = (strchr("*/%", *parentOp) != NULL);
4559 if (!(is_lopriparent || is_hipriparent))
4562 if (is_hipriop && is_lopriparent)
4563 return true; /* op binds tighter than parent */
4565 if (is_lopriop && is_hipriparent)
4569 * Operators are same priority --- can skip parens only if
4570 * we have (a - b) - c, not a - (b - c).
4572 if (node == (Node *) linitial(((OpExpr *) parentNode)->args))
4577 /* else do the same stuff as for T_SubLink et al. */
4584 case T_DistinctExpr:
4585 switch (nodeTag(parentNode))
4589 /* special handling for casts */
4590 CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
4592 if (type == COERCE_EXPLICIT_CAST ||
4593 type == COERCE_IMPLICIT_CAST)
4595 return true; /* own parentheses */
4597 case T_BoolExpr: /* lower precedence */
4598 case T_ArrayRef: /* other separators */
4599 case T_ArrayExpr: /* other separators */
4600 case T_RowExpr: /* other separators */
4601 case T_CoalesceExpr: /* own parentheses */
4602 case T_MinMaxExpr: /* own parentheses */
4603 case T_XmlExpr: /* own parentheses */
4604 case T_NullIfExpr: /* other separators */
4605 case T_Aggref: /* own parentheses */
4606 case T_WindowFunc: /* own parentheses */
4607 case T_CaseExpr: /* other separators */
4614 switch (nodeTag(parentNode))
4617 if (prettyFlags & PRETTYFLAG_PAREN)
4620 BoolExprType parentType;
4622 type = ((BoolExpr *) node)->boolop;
4623 parentType = ((BoolExpr *) parentNode)->boolop;
4628 if (parentType == AND_EXPR || parentType == OR_EXPR)
4632 if (parentType == OR_EXPR)
4640 /* special handling for casts */
4641 CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
4643 if (type == COERCE_EXPLICIT_CAST ||
4644 type == COERCE_IMPLICIT_CAST)
4646 return true; /* own parentheses */
4648 case T_ArrayRef: /* other separators */
4649 case T_ArrayExpr: /* other separators */
4650 case T_RowExpr: /* other separators */
4651 case T_CoalesceExpr: /* own parentheses */
4652 case T_MinMaxExpr: /* own parentheses */
4653 case T_XmlExpr: /* own parentheses */
4654 case T_NullIfExpr: /* other separators */
4655 case T_Aggref: /* own parentheses */
4656 case T_WindowFunc: /* own parentheses */
4657 case T_CaseExpr: /* other separators */
4666 /* those we don't know: in dubio complexo */
4672 * appendContextKeyword - append a keyword to buffer
4674 * If prettyPrint is enabled, perform a line break, and adjust indentation.
4675 * Otherwise, just append the keyword.
4678 appendContextKeyword(deparse_context *context, const char *str,
4679 int indentBefore, int indentAfter, int indentPlus)
4681 if (PRETTY_INDENT(context))
4683 context->indentLevel += indentBefore;
4685 appendStringInfoChar(context->buf, '\n');
4686 appendStringInfoSpaces(context->buf,
4687 Max(context->indentLevel, 0) + indentPlus);
4688 appendStringInfoString(context->buf, str);
4690 context->indentLevel += indentAfter;
4691 if (context->indentLevel < 0)
4692 context->indentLevel = 0;
4695 appendStringInfoString(context->buf, str);
4699 * get_rule_expr_paren - deparse expr using get_rule_expr,
4700 * embracing the string with parentheses if necessary for prettyPrint.
4702 * Never embrace if prettyFlags=0, because it's done in the calling node.
4704 * Any node that does *not* embrace its argument node by sql syntax (with
4705 * parentheses, non-operator keywords like CASE/WHEN/ON, or comma etc) should
4706 * use get_rule_expr_paren instead of get_rule_expr so parentheses can be
4710 get_rule_expr_paren(Node *node, deparse_context *context,
4711 bool showimplicit, Node *parentNode)
4715 need_paren = PRETTY_PAREN(context) &&
4716 !isSimpleNode(node, parentNode, context->prettyFlags);
4719 appendStringInfoChar(context->buf, '(');
4721 get_rule_expr(node, context, showimplicit);
4724 appendStringInfoChar(context->buf, ')');
4729 * get_rule_expr - Parse back an expression
4731 * Note: showimplicit determines whether we display any implicit cast that
4732 * is present at the top of the expression tree. It is a passed argument,
4733 * not a field of the context struct, because we change the value as we
4734 * recurse down into the expression. In general we suppress implicit casts
4735 * when the result type is known with certainty (eg, the arguments of an
4736 * OR must be boolean). We display implicit casts for arguments of functions
4737 * and operators, since this is needed to be certain that the same function
4738 * or operator will be chosen when the expression is re-parsed.
4742 get_rule_expr(Node *node, deparse_context *context,
4745 StringInfo buf = context->buf;
4751 * Each level of get_rule_expr must emit an indivisible term
4752 * (parenthesized if necessary) to ensure result is reparsed into the same
4753 * expression tree. The only exception is that when the input is a List,
4754 * we emit the component items comma-separated with no surrounding
4755 * decoration; this is convenient for most callers.
4757 switch (nodeTag(node))
4760 (void) get_variable((Var *) node, 0, true, context);
4764 get_const_expr((Const *) node, context, 0);
4768 get_parameter((Param *) node, context);
4772 get_agg_expr((Aggref *) node, context);
4776 get_windowfunc_expr((WindowFunc *) node, context);
4781 ArrayRef *aref = (ArrayRef *) node;
4785 * If the argument is a CaseTestExpr, we must be inside a
4786 * FieldStore, ie, we are assigning to an element of an array
4787 * within a composite column. Since we already punted on
4788 * displaying the FieldStore's target information, just punt
4789 * here too, and display only the assignment source
4792 if (IsA(aref->refexpr, CaseTestExpr))
4794 Assert(aref->refassgnexpr);
4795 get_rule_expr((Node *) aref->refassgnexpr,
4796 context, showimplicit);
4801 * Parenthesize the argument unless it's a simple Var or a
4802 * FieldSelect. (In particular, if it's another ArrayRef, we
4803 * *must* parenthesize to avoid confusion.)
4805 need_parens = !IsA(aref->refexpr, Var) &&
4806 !IsA(aref->refexpr, FieldSelect);
4808 appendStringInfoChar(buf, '(');
4809 get_rule_expr((Node *) aref->refexpr, context, showimplicit);
4811 appendStringInfoChar(buf, ')');
4814 * If there's a refassgnexpr, we want to print the node in the
4815 * format "array[subscripts] := refassgnexpr". This is not
4816 * legal SQL, so decompilation of INSERT or UPDATE statements
4817 * should always use processIndirection as part of the
4818 * statement-level syntax. We should only see this when
4819 * EXPLAIN tries to print the targetlist of a plan resulting
4820 * from such a statement.
4822 if (aref->refassgnexpr)
4827 * Use processIndirection to print this node's subscripts
4828 * as well as any additional field selections or
4829 * subscripting in immediate descendants. It returns the
4830 * RHS expr that is actually being "assigned".
4832 refassgnexpr = processIndirection(node, context, true);
4833 appendStringInfoString(buf, " := ");
4834 get_rule_expr(refassgnexpr, context, showimplicit);
4838 /* Just an ordinary array fetch, so print subscripts */
4839 printSubscripts(aref, context);
4845 get_func_expr((FuncExpr *) node, context, showimplicit);
4848 case T_NamedArgExpr:
4850 NamedArgExpr *na = (NamedArgExpr *) node;
4852 appendStringInfo(buf, "%s := ", quote_identifier(na->name));
4853 get_rule_expr((Node *) na->arg, context, showimplicit);
4858 get_oper_expr((OpExpr *) node, context);
4861 case T_DistinctExpr:
4863 DistinctExpr *expr = (DistinctExpr *) node;
4864 List *args = expr->args;
4865 Node *arg1 = (Node *) linitial(args);
4866 Node *arg2 = (Node *) lsecond(args);
4868 if (!PRETTY_PAREN(context))
4869 appendStringInfoChar(buf, '(');
4870 get_rule_expr_paren(arg1, context, true, node);
4871 appendStringInfo(buf, " IS DISTINCT FROM ");
4872 get_rule_expr_paren(arg2, context, true, node);
4873 if (!PRETTY_PAREN(context))
4874 appendStringInfoChar(buf, ')');
4880 NullIfExpr *nullifexpr = (NullIfExpr *) node;
4882 appendStringInfo(buf, "NULLIF(");
4883 get_rule_expr((Node *) nullifexpr->args, context, true);
4884 appendStringInfoChar(buf, ')');
4888 case T_ScalarArrayOpExpr:
4890 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
4891 List *args = expr->args;
4892 Node *arg1 = (Node *) linitial(args);
4893 Node *arg2 = (Node *) lsecond(args);
4895 if (!PRETTY_PAREN(context))
4896 appendStringInfoChar(buf, '(');
4897 get_rule_expr_paren(arg1, context, true, node);
4898 appendStringInfo(buf, " %s %s (",
4899 generate_operator_name(expr->opno,
4901 get_base_element_type(exprType(arg2))),
4902 expr->useOr ? "ANY" : "ALL");
4903 get_rule_expr_paren(arg2, context, true, node);
4904 appendStringInfoChar(buf, ')');
4905 if (!PRETTY_PAREN(context))
4906 appendStringInfoChar(buf, ')');
4912 BoolExpr *expr = (BoolExpr *) node;
4913 Node *first_arg = linitial(expr->args);
4914 ListCell *arg = lnext(list_head(expr->args));
4916 switch (expr->boolop)
4919 if (!PRETTY_PAREN(context))
4920 appendStringInfoChar(buf, '(');
4921 get_rule_expr_paren(first_arg, context,
4925 appendStringInfo(buf, " AND ");
4926 get_rule_expr_paren((Node *) lfirst(arg), context,
4930 if (!PRETTY_PAREN(context))
4931 appendStringInfoChar(buf, ')');
4935 if (!PRETTY_PAREN(context))
4936 appendStringInfoChar(buf, '(');
4937 get_rule_expr_paren(first_arg, context,
4941 appendStringInfo(buf, " OR ");
4942 get_rule_expr_paren((Node *) lfirst(arg), context,
4946 if (!PRETTY_PAREN(context))
4947 appendStringInfoChar(buf, ')');
4951 if (!PRETTY_PAREN(context))
4952 appendStringInfoChar(buf, '(');
4953 appendStringInfo(buf, "NOT ");
4954 get_rule_expr_paren(first_arg, context,
4956 if (!PRETTY_PAREN(context))
4957 appendStringInfoChar(buf, ')');
4961 elog(ERROR, "unrecognized boolop: %d",
4962 (int) expr->boolop);
4968 get_sublink_expr((SubLink *) node, context);
4973 SubPlan *subplan = (SubPlan *) node;
4976 * We cannot see an already-planned subplan in rule deparsing,
4977 * only while EXPLAINing a query plan. We don't try to
4978 * reconstruct the original SQL, just reference the subplan
4979 * that appears elsewhere in EXPLAIN's result.
4981 if (subplan->useHashTable)
4982 appendStringInfo(buf, "(hashed %s)", subplan->plan_name);
4984 appendStringInfo(buf, "(%s)", subplan->plan_name);
4988 case T_AlternativeSubPlan:
4990 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
4993 /* As above, this can only happen during EXPLAIN */
4994 appendStringInfo(buf, "(alternatives: ");
4995 foreach(lc, asplan->subplans)
4997 SubPlan *splan = (SubPlan *) lfirst(lc);
4999 Assert(IsA(splan, SubPlan));
5000 if (splan->useHashTable)
5001 appendStringInfo(buf, "hashed %s", splan->plan_name);
5003 appendStringInfo(buf, "%s", splan->plan_name);
5005 appendStringInfo(buf, " or ");
5007 appendStringInfo(buf, ")");
5013 FieldSelect *fselect = (FieldSelect *) node;
5014 Node *arg = (Node *) fselect->arg;
5015 int fno = fselect->fieldnum;
5016 const char *fieldname;
5020 * Parenthesize the argument unless it's an ArrayRef or
5021 * another FieldSelect. Note in particular that it would be
5022 * WRONG to not parenthesize a Var argument; simplicity is not
5023 * the issue here, having the right number of names is.
5025 need_parens = !IsA(arg, ArrayRef) &&!IsA(arg, FieldSelect);
5027 appendStringInfoChar(buf, '(');
5028 get_rule_expr(arg, context, true);
5030 appendStringInfoChar(buf, ')');
5033 * Get and print the field name.
5035 fieldname = get_name_for_var_field((Var *) arg, fno,
5037 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
5043 FieldStore *fstore = (FieldStore *) node;
5047 * There is no good way to represent a FieldStore as real SQL,
5048 * so decompilation of INSERT or UPDATE statements should
5049 * always use processIndirection as part of the
5050 * statement-level syntax. We should only get here when
5051 * EXPLAIN tries to print the targetlist of a plan resulting
5052 * from such a statement. The plan case is even harder than
5053 * ordinary rules would be, because the planner tries to
5054 * collapse multiple assignments to the same field or subfield
5055 * into one FieldStore; so we can see a list of target fields
5056 * not just one, and the arguments could be FieldStores
5057 * themselves. We don't bother to try to print the target
5058 * field names; we just print the source arguments, with a
5059 * ROW() around them if there's more than one. This isn't
5060 * terribly complete, but it's probably good enough for
5061 * EXPLAIN's purposes; especially since anything more would be
5062 * either hopelessly confusing or an even poorer
5063 * representation of what the plan is actually doing.
5065 need_parens = (list_length(fstore->newvals) != 1);
5067 appendStringInfoString(buf, "ROW(");
5068 get_rule_expr((Node *) fstore->newvals, context, showimplicit);
5070 appendStringInfoChar(buf, ')');
5076 RelabelType *relabel = (RelabelType *) node;
5077 Node *arg = (Node *) relabel->arg;
5079 if (relabel->relabelformat == COERCE_IMPLICIT_CAST &&
5082 /* don't show the implicit cast */
5083 get_rule_expr_paren(arg, context, false, node);
5087 get_coercion_expr(arg, context,
5088 relabel->resulttype,
5089 relabel->resulttypmod,
5097 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
5098 Node *arg = (Node *) iocoerce->arg;
5100 if (iocoerce->coerceformat == COERCE_IMPLICIT_CAST &&
5103 /* don't show the implicit cast */
5104 get_rule_expr_paren(arg, context, false, node);
5108 get_coercion_expr(arg, context,
5109 iocoerce->resulttype,
5116 case T_ArrayCoerceExpr:
5118 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
5119 Node *arg = (Node *) acoerce->arg;
5121 if (acoerce->coerceformat == COERCE_IMPLICIT_CAST &&
5124 /* don't show the implicit cast */
5125 get_rule_expr_paren(arg, context, false, node);
5129 get_coercion_expr(arg, context,
5130 acoerce->resulttype,
5131 acoerce->resulttypmod,
5137 case T_ConvertRowtypeExpr:
5139 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
5140 Node *arg = (Node *) convert->arg;
5142 if (convert->convertformat == COERCE_IMPLICIT_CAST &&
5145 /* don't show the implicit cast */
5146 get_rule_expr_paren(arg, context, false, node);
5150 get_coercion_expr(arg, context,
5151 convert->resulttype, -1,
5159 CollateExpr *collate = (CollateExpr *) node;
5160 Node *arg = (Node *) collate->arg;
5162 if (!PRETTY_PAREN(context))
5163 appendStringInfoChar(buf, '(');
5164 get_rule_expr_paren(arg, context, showimplicit, node);
5165 appendStringInfo(buf, " COLLATE %s",
5166 generate_collation_name(collate->collOid));
5167 if (!PRETTY_PAREN(context))
5168 appendStringInfoChar(buf, ')');
5174 CaseExpr *caseexpr = (CaseExpr *) node;
5177 appendContextKeyword(context, "CASE",
5178 0, PRETTYINDENT_VAR, 0);
5181 appendStringInfoChar(buf, ' ');
5182 get_rule_expr((Node *) caseexpr->arg, context, true);
5184 foreach(temp, caseexpr->args)
5186 CaseWhen *when = (CaseWhen *) lfirst(temp);
5187 Node *w = (Node *) when->expr;
5189 if (!PRETTY_INDENT(context))
5190 appendStringInfoChar(buf, ' ');
5191 appendContextKeyword(context, "WHEN ",
5196 * The parser should have produced WHEN clauses of the
5197 * form "CaseTestExpr = RHS"; we want to show just the
5198 * RHS. If the user wrote something silly like "CASE
5199 * boolexpr WHEN TRUE THEN ...", then the optimizer's
5200 * simplify_boolean_equality() may have reduced this
5201 * to just "CaseTestExpr" or "NOT CaseTestExpr", for
5202 * which we have to show "TRUE" or "FALSE". We have
5203 * also to consider the possibility that an implicit
5204 * coercion was inserted between the CaseTestExpr and
5209 List *args = ((OpExpr *) w)->args;
5212 Assert(list_length(args) == 2);
5213 Assert(IsA(strip_implicit_coercions(linitial(args)),
5215 rhs = (Node *) lsecond(args);
5216 get_rule_expr(rhs, context, false);
5218 else if (IsA(strip_implicit_coercions(w),
5220 appendStringInfo(buf, "TRUE");
5221 else if (not_clause(w))
5223 Assert(IsA(strip_implicit_coercions((Node *) get_notclausearg((Expr *) w)),
5225 appendStringInfo(buf, "FALSE");
5228 elog(ERROR, "unexpected CASE WHEN clause: %d",
5232 get_rule_expr(w, context, false);
5233 appendStringInfo(buf, " THEN ");
5234 get_rule_expr((Node *) when->result, context, true);
5236 if (!PRETTY_INDENT(context))
5237 appendStringInfoChar(buf, ' ');
5238 appendContextKeyword(context, "ELSE ",
5240 get_rule_expr((Node *) caseexpr->defresult, context, true);
5241 if (!PRETTY_INDENT(context))
5242 appendStringInfoChar(buf, ' ');
5243 appendContextKeyword(context, "END",
5244 -PRETTYINDENT_VAR, 0, 0);
5250 ArrayExpr *arrayexpr = (ArrayExpr *) node;
5252 appendStringInfo(buf, "ARRAY[");
5253 get_rule_expr((Node *) arrayexpr->elements, context, true);
5254 appendStringInfoChar(buf, ']');
5257 * If the array isn't empty, we assume its elements are
5258 * coerced to the desired type. If it's empty, though, we
5259 * need an explicit coercion to the array type.
5261 if (arrayexpr->elements == NIL)
5262 appendStringInfo(buf, "::%s",
5263 format_type_with_typemod(arrayexpr->array_typeid, -1));
5269 RowExpr *rowexpr = (RowExpr *) node;
5270 TupleDesc tupdesc = NULL;
5276 * If it's a named type and not RECORD, we may have to skip
5277 * dropped columns and/or claim there are NULLs for added
5280 if (rowexpr->row_typeid != RECORDOID)
5282 tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
5283 Assert(list_length(rowexpr->args) <= tupdesc->natts);
5287 * SQL99 allows "ROW" to be omitted when there is more than
5288 * one column, but for simplicity we always print it.
5290 appendStringInfo(buf, "ROW(");
5293 foreach(arg, rowexpr->args)
5295 Node *e = (Node *) lfirst(arg);
5297 if (tupdesc == NULL ||
5298 !tupdesc->attrs[i]->attisdropped)
5300 appendStringInfoString(buf, sep);
5301 get_rule_expr(e, context, true);
5306 if (tupdesc != NULL)
5308 while (i < tupdesc->natts)
5310 if (!tupdesc->attrs[i]->attisdropped)
5312 appendStringInfoString(buf, sep);
5313 appendStringInfo(buf, "NULL");
5319 ReleaseTupleDesc(tupdesc);
5321 appendStringInfo(buf, ")");
5322 if (rowexpr->row_format == COERCE_EXPLICIT_CAST)
5323 appendStringInfo(buf, "::%s",
5324 format_type_with_typemod(rowexpr->row_typeid, -1));
5328 case T_RowCompareExpr:
5330 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
5335 * SQL99 allows "ROW" to be omitted when there is more than
5336 * one column, but for simplicity we always print it.
5338 appendStringInfo(buf, "(ROW(");
5340 foreach(arg, rcexpr->largs)
5342 Node *e = (Node *) lfirst(arg);
5344 appendStringInfoString(buf, sep);
5345 get_rule_expr(e, context, true);
5350 * We assume that the name of the first-column operator will
5351 * do for all the rest too. This is definitely open to
5352 * failure, eg if some but not all operators were renamed
5353 * since the construct was parsed, but there seems no way to
5356 appendStringInfo(buf, ") %s ROW(",
5357 generate_operator_name(linitial_oid(rcexpr->opnos),
5358 exprType(linitial(rcexpr->largs)),
5359 exprType(linitial(rcexpr->rargs))));
5361 foreach(arg, rcexpr->rargs)
5363 Node *e = (Node *) lfirst(arg);
5365 appendStringInfoString(buf, sep);
5366 get_rule_expr(e, context, true);
5369 appendStringInfo(buf, "))");
5373 case T_CoalesceExpr:
5375 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
5377 appendStringInfo(buf, "COALESCE(");
5378 get_rule_expr((Node *) coalesceexpr->args, context, true);
5379 appendStringInfoChar(buf, ')');
5385 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
5387 switch (minmaxexpr->op)
5390 appendStringInfo(buf, "GREATEST(");
5393 appendStringInfo(buf, "LEAST(");
5396 get_rule_expr((Node *) minmaxexpr->args, context, true);
5397 appendStringInfoChar(buf, ')');
5403 XmlExpr *xexpr = (XmlExpr *) node;
5404 bool needcomma = false;
5412 appendStringInfoString(buf, "XMLCONCAT(");
5415 appendStringInfoString(buf, "XMLELEMENT(");
5418 appendStringInfoString(buf, "XMLFOREST(");
5421 appendStringInfoString(buf, "XMLPARSE(");
5424 appendStringInfoString(buf, "XMLPI(");
5427 appendStringInfoString(buf, "XMLROOT(");
5429 case IS_XMLSERIALIZE:
5430 appendStringInfoString(buf, "XMLSERIALIZE(");
5435 if (xexpr->op == IS_XMLPARSE || xexpr->op == IS_XMLSERIALIZE)
5437 if (xexpr->xmloption == XMLOPTION_DOCUMENT)
5438 appendStringInfoString(buf, "DOCUMENT ");
5440 appendStringInfoString(buf, "CONTENT ");
5444 appendStringInfo(buf, "NAME %s",
5445 quote_identifier(map_xml_name_to_sql_identifier(xexpr->name)));
5448 if (xexpr->named_args)
5450 if (xexpr->op != IS_XMLFOREST)
5453 appendStringInfoString(buf, ", ");
5454 appendStringInfoString(buf, "XMLATTRIBUTES(");
5457 forboth(arg, xexpr->named_args, narg, xexpr->arg_names)
5459 Node *e = (Node *) lfirst(arg);
5460 char *argname = strVal(lfirst(narg));
5463 appendStringInfoString(buf, ", ");
5464 get_rule_expr((Node *) e, context, true);
5465 appendStringInfo(buf, " AS %s",
5466 quote_identifier(map_xml_name_to_sql_identifier(argname)));
5469 if (xexpr->op != IS_XMLFOREST)
5470 appendStringInfoChar(buf, ')');
5475 appendStringInfoString(buf, ", ");
5482 case IS_XMLSERIALIZE:
5483 /* no extra decoration needed */
5484 get_rule_expr((Node *) xexpr->args, context, true);
5487 Assert(list_length(xexpr->args) == 2);
5489 get_rule_expr((Node *) linitial(xexpr->args),
5492 con = (Const *) lsecond(xexpr->args);
5493 Assert(IsA(con, Const));
5494 Assert(!con->constisnull);
5495 if (DatumGetBool(con->constvalue))
5496 appendStringInfoString(buf,
5497 " PRESERVE WHITESPACE");
5499 appendStringInfoString(buf,
5500 " STRIP WHITESPACE");
5503 Assert(list_length(xexpr->args) == 3);
5505 get_rule_expr((Node *) linitial(xexpr->args),
5508 appendStringInfoString(buf, ", VERSION ");
5509 con = (Const *) lsecond(xexpr->args);
5510 if (IsA(con, Const) &&
5512 appendStringInfoString(buf, "NO VALUE");
5514 get_rule_expr((Node *) con, context, false);
5516 con = (Const *) lthird(xexpr->args);
5517 Assert(IsA(con, Const));
5518 if (con->constisnull)
5519 /* suppress STANDALONE NO VALUE */ ;
5522 switch (DatumGetInt32(con->constvalue))
5524 case XML_STANDALONE_YES:
5525 appendStringInfoString(buf,
5526 ", STANDALONE YES");
5528 case XML_STANDALONE_NO:
5529 appendStringInfoString(buf,
5532 case XML_STANDALONE_NO_VALUE:
5533 appendStringInfoString(buf,
5534 ", STANDALONE NO VALUE");
5542 get_rule_expr_paren((Node *) xexpr->args, context, false, node);
5547 if (xexpr->op == IS_XMLSERIALIZE)
5548 appendStringInfo(buf, " AS %s",
5549 format_type_with_typemod(xexpr->type,
5551 if (xexpr->op == IS_DOCUMENT)
5552 appendStringInfoString(buf, " IS DOCUMENT");
5554 appendStringInfoChar(buf, ')');
5560 NullTest *ntest = (NullTest *) node;
5562 if (!PRETTY_PAREN(context))
5563 appendStringInfoChar(buf, '(');
5564 get_rule_expr_paren((Node *) ntest->arg, context, true, node);
5565 switch (ntest->nulltesttype)
5568 appendStringInfo(buf, " IS NULL");
5571 appendStringInfo(buf, " IS NOT NULL");
5574 elog(ERROR, "unrecognized nulltesttype: %d",
5575 (int) ntest->nulltesttype);
5577 if (!PRETTY_PAREN(context))
5578 appendStringInfoChar(buf, ')');
5584 BooleanTest *btest = (BooleanTest *) node;
5586 if (!PRETTY_PAREN(context))
5587 appendStringInfoChar(buf, '(');
5588 get_rule_expr_paren((Node *) btest->arg, context, false, node);
5589 switch (btest->booltesttype)
5592 appendStringInfo(buf, " IS TRUE");
5595 appendStringInfo(buf, " IS NOT TRUE");
5598 appendStringInfo(buf, " IS FALSE");
5601 appendStringInfo(buf, " IS NOT FALSE");
5604 appendStringInfo(buf, " IS UNKNOWN");
5606 case IS_NOT_UNKNOWN:
5607 appendStringInfo(buf, " IS NOT UNKNOWN");
5610 elog(ERROR, "unrecognized booltesttype: %d",
5611 (int) btest->booltesttype);
5613 if (!PRETTY_PAREN(context))
5614 appendStringInfoChar(buf, ')');
5618 case T_CoerceToDomain:
5620 CoerceToDomain *ctest = (CoerceToDomain *) node;
5621 Node *arg = (Node *) ctest->arg;
5623 if (ctest->coercionformat == COERCE_IMPLICIT_CAST &&
5626 /* don't show the implicit cast */
5627 get_rule_expr(arg, context, false);
5631 get_coercion_expr(arg, context,
5633 ctest->resulttypmod,
5639 case T_CoerceToDomainValue:
5640 appendStringInfo(buf, "VALUE");
5643 case T_SetToDefault:
5644 appendStringInfo(buf, "DEFAULT");
5647 case T_CurrentOfExpr:
5649 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
5651 if (cexpr->cursor_name)
5652 appendStringInfo(buf, "CURRENT OF %s",
5653 quote_identifier(cexpr->cursor_name));
5655 appendStringInfo(buf, "CURRENT OF $%d",
5656 cexpr->cursor_param);
5666 foreach(l, (List *) node)
5668 appendStringInfoString(buf, sep);
5669 get_rule_expr((Node *) lfirst(l), context, showimplicit);
5676 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
5683 * get_oper_expr - Parse back an OpExpr node
5686 get_oper_expr(OpExpr *expr, deparse_context *context)
5688 StringInfo buf = context->buf;
5689 Oid opno = expr->opno;
5690 List *args = expr->args;
5692 if (!PRETTY_PAREN(context))
5693 appendStringInfoChar(buf, '(');
5694 if (list_length(args) == 2)
5696 /* binary operator */
5697 Node *arg1 = (Node *) linitial(args);
5698 Node *arg2 = (Node *) lsecond(args);
5700 get_rule_expr_paren(arg1, context, true, (Node *) expr);
5701 appendStringInfo(buf, " %s ",
5702 generate_operator_name(opno,
5705 get_rule_expr_paren(arg2, context, true, (Node *) expr);
5709 /* unary operator --- but which side? */
5710 Node *arg = (Node *) linitial(args);
5712 Form_pg_operator optup;
5714 tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
5715 if (!HeapTupleIsValid(tp))
5716 elog(ERROR, "cache lookup failed for operator %u", opno);
5717 optup = (Form_pg_operator) GETSTRUCT(tp);
5718 switch (optup->oprkind)
5721 appendStringInfo(buf, "%s ",
5722 generate_operator_name(opno,
5725 get_rule_expr_paren(arg, context, true, (Node *) expr);
5728 get_rule_expr_paren(arg, context, true, (Node *) expr);
5729 appendStringInfo(buf, " %s",
5730 generate_operator_name(opno,
5735 elog(ERROR, "bogus oprkind: %d", optup->oprkind);
5737 ReleaseSysCache(tp);
5739 if (!PRETTY_PAREN(context))
5740 appendStringInfoChar(buf, ')');
5744 * get_func_expr - Parse back a FuncExpr node
5747 get_func_expr(FuncExpr *expr, deparse_context *context,
5750 StringInfo buf = context->buf;
5751 Oid funcoid = expr->funcid;
5752 Oid argtypes[FUNC_MAX_ARGS];
5759 * If the function call came from an implicit coercion, then just show the
5760 * first argument --- unless caller wants to see implicit coercions.
5762 if (expr->funcformat == COERCE_IMPLICIT_CAST && !showimplicit)
5764 get_rule_expr_paren((Node *) linitial(expr->args), context,
5765 false, (Node *) expr);
5770 * If the function call came from a cast, then show the first argument
5771 * plus an explicit cast operation.
5773 if (expr->funcformat == COERCE_EXPLICIT_CAST ||
5774 expr->funcformat == COERCE_IMPLICIT_CAST)
5776 Node *arg = linitial(expr->args);
5777 Oid rettype = expr->funcresulttype;
5778 int32 coercedTypmod;
5780 /* Get the typmod if this is a length-coercion function */
5781 (void) exprIsLengthCoercion((Node *) expr, &coercedTypmod);
5783 get_coercion_expr(arg, context,
5784 rettype, coercedTypmod,
5791 * Normal function: display as proname(args). First we need to extract
5792 * the argument datatypes.
5794 if (list_length(expr->args) > FUNC_MAX_ARGS)
5796 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
5797 errmsg("too many arguments")));
5800 foreach(l, expr->args)
5802 Node *arg = (Node *) lfirst(l);
5804 if (IsA(arg, NamedArgExpr))
5805 argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
5806 argtypes[nargs] = exprType(arg);
5810 appendStringInfo(buf, "%s(",
5811 generate_function_name(funcoid, nargs,
5815 foreach(l, expr->args)
5818 appendStringInfoString(buf, ", ");
5819 if (is_variadic && lnext(l) == NULL)
5820 appendStringInfoString(buf, "VARIADIC ");
5821 get_rule_expr((Node *) lfirst(l), context, true);
5823 appendStringInfoChar(buf, ')');
5827 * get_agg_expr - Parse back an Aggref node
5830 get_agg_expr(Aggref *aggref, deparse_context *context)
5832 StringInfo buf = context->buf;
5833 Oid argtypes[FUNC_MAX_ARGS];
5838 /* Extract the regular arguments, ignoring resjunk stuff for the moment */
5841 foreach(l, aggref->args)
5843 TargetEntry *tle = (TargetEntry *) lfirst(l);
5844 Node *arg = (Node *) tle->expr;
5846 Assert(!IsA(arg, NamedArgExpr));
5849 if (nargs >= FUNC_MAX_ARGS) /* paranoia */
5851 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
5852 errmsg("too many arguments")));
5853 argtypes[nargs] = exprType(arg);
5854 arglist = lappend(arglist, arg);
5858 appendStringInfo(buf, "%s(%s",
5859 generate_function_name(aggref->aggfnoid, nargs,
5860 NIL, argtypes, NULL),
5861 (aggref->aggdistinct != NIL) ? "DISTINCT " : "");
5862 /* aggstar can be set only in zero-argument aggregates */
5863 if (aggref->aggstar)
5864 appendStringInfoChar(buf, '*');
5866 get_rule_expr((Node *) arglist, context, true);
5867 if (aggref->aggorder != NIL)
5869 appendStringInfoString(buf, " ORDER BY ");
5870 get_rule_orderby(aggref->aggorder, aggref->args, false, context);
5872 appendStringInfoChar(buf, ')');
5876 * get_windowfunc_expr - Parse back a WindowFunc node
5879 get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
5881 StringInfo buf = context->buf;
5882 Oid argtypes[FUNC_MAX_ARGS];
5886 if (list_length(wfunc->args) > FUNC_MAX_ARGS)
5888 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
5889 errmsg("too many arguments")));
5891 foreach(l, wfunc->args)
5893 Node *arg = (Node *) lfirst(l);
5895 Assert(!IsA(arg, NamedArgExpr));
5896 argtypes[nargs] = exprType(arg);
5900 appendStringInfo(buf, "%s(",
5901 generate_function_name(wfunc->winfnoid, nargs,
5902 NIL, argtypes, NULL));
5903 /* winstar can be set only in zero-argument aggregates */
5905 appendStringInfoChar(buf, '*');
5907 get_rule_expr((Node *) wfunc->args, context, true);
5908 appendStringInfoString(buf, ") OVER ");
5910 foreach(l, context->windowClause)
5912 WindowClause *wc = (WindowClause *) lfirst(l);
5914 if (wc->winref == wfunc->winref)
5917 appendStringInfoString(buf, quote_identifier(wc->name));
5919 get_rule_windowspec(wc, context->windowTList, context);
5925 if (context->windowClause)
5926 elog(ERROR, "could not find window clause for winref %u",
5930 * In EXPLAIN, we don't have window context information available, so
5931 * we have to settle for this:
5933 appendStringInfoString(buf, "(?)");
5940 * Make a string representation of a value coerced to a specific type
5944 get_coercion_expr(Node *arg, deparse_context *context,
5945 Oid resulttype, int32 resulttypmod,
5948 StringInfo buf = context->buf;
5951 * Since parse_coerce.c doesn't immediately collapse application of
5952 * length-coercion functions to constants, what we'll typically see in
5953 * such cases is a Const with typmod -1 and a length-coercion function
5954 * right above it. Avoid generating redundant output. However, beware of
5955 * suppressing casts when the user actually wrote something like
5956 * 'foo'::text::char(3).
5958 if (arg && IsA(arg, Const) &&
5959 ((Const *) arg)->consttype == resulttype &&
5960 ((Const *) arg)->consttypmod == -1)
5962 /* Show the constant without normal ::typename decoration */
5963 get_const_expr((Const *) arg, context, -1);
5967 if (!PRETTY_PAREN(context))
5968 appendStringInfoChar(buf, '(');
5969 get_rule_expr_paren(arg, context, false, parentNode);
5970 if (!PRETTY_PAREN(context))
5971 appendStringInfoChar(buf, ')');
5973 appendStringInfo(buf, "::%s",
5974 format_type_with_typemod(resulttype, resulttypmod));
5980 * Make a string representation of a Const
5982 * showtype can be -1 to never show "::typename" decoration, or +1 to always
5983 * show it, or 0 to show it only if the constant wouldn't be assumed to be
5984 * the right type by default.
5986 * If the Const's collation isn't default for its type, show that too.
5987 * This can only happen in trees that have been through constant-folding.
5988 * We assume we don't need to do this when showtype is -1.
5992 get_const_expr(Const *constval, deparse_context *context, int showtype)
5994 StringInfo buf = context->buf;
5998 bool isfloat = false;
6001 if (constval->constisnull)
6004 * Always label the type of a NULL constant to prevent misdecisions
6005 * about type when reparsing.
6007 appendStringInfo(buf, "NULL");
6010 appendStringInfo(buf, "::%s",
6011 format_type_with_typemod(constval->consttype,
6012 constval->consttypmod));
6013 get_const_collation(constval, context);
6018 getTypeOutputInfo(constval->consttype,
6019 &typoutput, &typIsVarlena);
6021 extval = OidOutputFunctionCall(typoutput, constval->constvalue);
6023 switch (constval->consttype)
6034 * These types are printed without quotes unless they contain
6035 * values that aren't accepted by the scanner unquoted (e.g.,
6036 * 'NaN'). Note that strtod() and friends might accept NaN,
6037 * so we can't use that to test.
6039 * In reality we only need to defend against infinity and NaN,
6040 * so we need not get too crazy about pattern matching here.
6042 * There is a special-case gotcha: if the constant is signed,
6043 * we need to parenthesize it, else the parser might see a
6044 * leading plus/minus as binding less tightly than adjacent
6045 * operators --- particularly, the cast that we might attach
6048 if (strspn(extval, "0123456789+-eE.") == strlen(extval))
6050 if (extval[0] == '+' || extval[0] == '-')
6051 appendStringInfo(buf, "(%s)", extval);
6053 appendStringInfoString(buf, extval);
6054 if (strcspn(extval, "eE.") != strlen(extval))
6055 isfloat = true; /* it looks like a float */
6058 appendStringInfo(buf, "'%s'", extval);
6064 appendStringInfo(buf, "B'%s'", extval);
6068 if (strcmp(extval, "t") == 0)
6069 appendStringInfo(buf, "true");
6071 appendStringInfo(buf, "false");
6075 simple_quote_literal(buf, extval);
6085 * For showtype == 0, append ::typename unless the constant will be
6086 * implicitly typed as the right type when it is read in.
6088 * XXX this code has to be kept in sync with the behavior of the parser,
6089 * especially make_const.
6091 switch (constval->consttype)
6096 /* These types can be left unlabeled */
6102 * Float-looking constants will be typed as numeric, but if
6103 * there's a specific typmod we need to show it.
6105 needlabel = !isfloat || (constval->consttypmod >= 0);
6111 if (needlabel || showtype > 0)
6112 appendStringInfo(buf, "::%s",
6113 format_type_with_typemod(constval->consttype,
6114 constval->consttypmod));
6116 get_const_collation(constval, context);
6120 * helper for get_const_expr: append COLLATE if needed
6123 get_const_collation(Const *constval, deparse_context *context)
6125 StringInfo buf = context->buf;
6127 if (OidIsValid(constval->constcollid))
6129 Oid typcollation = get_typcollation(constval->consttype);
6131 if (constval->constcollid != typcollation)
6133 appendStringInfo(buf, " COLLATE %s",
6134 generate_collation_name(constval->constcollid));
6140 * simple_quote_literal - Format a string as a SQL literal, append to buf
6143 simple_quote_literal(StringInfo buf, const char *val)
6148 * We form the string literal according to the prevailing setting of
6149 * standard_conforming_strings; we never use E''. User is responsible for
6150 * making sure result is used correctly.
6152 appendStringInfoChar(buf, '\'');
6153 for (valptr = val; *valptr; valptr++)
6157 if (SQL_STR_DOUBLE(ch, !standard_conforming_strings))
6158 appendStringInfoChar(buf, ch);
6159 appendStringInfoChar(buf, ch);
6161 appendStringInfoChar(buf, '\'');
6166 * get_sublink_expr - Parse back a sublink
6170 get_sublink_expr(SubLink *sublink, deparse_context *context)
6172 StringInfo buf = context->buf;
6173 Query *query = (Query *) (sublink->subselect);
6174 char *opname = NULL;
6177 if (sublink->subLinkType == ARRAY_SUBLINK)
6178 appendStringInfo(buf, "ARRAY(");
6180 appendStringInfoChar(buf, '(');
6183 * Note that we print the name of only the first operator, when there are
6184 * multiple combining operators. This is an approximation that could go
6185 * wrong in various scenarios (operators in different schemas, renamed
6186 * operators, etc) but there is not a whole lot we can do about it, since
6187 * the syntax allows only one operator to be shown.
6189 if (sublink->testexpr)
6191 if (IsA(sublink->testexpr, OpExpr))
6193 /* single combining operator */
6194 OpExpr *opexpr = (OpExpr *) sublink->testexpr;
6196 get_rule_expr(linitial(opexpr->args), context, true);
6197 opname = generate_operator_name(opexpr->opno,
6198 exprType(linitial(opexpr->args)),
6199 exprType(lsecond(opexpr->args)));
6201 else if (IsA(sublink->testexpr, BoolExpr))
6203 /* multiple combining operators, = or <> cases */
6207 appendStringInfoChar(buf, '(');
6209 foreach(l, ((BoolExpr *) sublink->testexpr)->args)
6211 OpExpr *opexpr = (OpExpr *) lfirst(l);
6213 Assert(IsA(opexpr, OpExpr));
6214 appendStringInfoString(buf, sep);
6215 get_rule_expr(linitial(opexpr->args), context, true);
6217 opname = generate_operator_name(opexpr->opno,
6218 exprType(linitial(opexpr->args)),
6219 exprType(lsecond(opexpr->args)));
6222 appendStringInfoChar(buf, ')');
6224 else if (IsA(sublink->testexpr, RowCompareExpr))
6226 /* multiple combining operators, < <= > >= cases */
6227 RowCompareExpr *rcexpr = (RowCompareExpr *) sublink->testexpr;
6229 appendStringInfoChar(buf, '(');
6230 get_rule_expr((Node *) rcexpr->largs, context, true);
6231 opname = generate_operator_name(linitial_oid(rcexpr->opnos),
6232 exprType(linitial(rcexpr->largs)),
6233 exprType(linitial(rcexpr->rargs)));
6234 appendStringInfoChar(buf, ')');
6237 elog(ERROR, "unrecognized testexpr type: %d",
6238 (int) nodeTag(sublink->testexpr));
6243 switch (sublink->subLinkType)
6245 case EXISTS_SUBLINK:
6246 appendStringInfo(buf, "EXISTS ");
6250 if (strcmp(opname, "=") == 0) /* Represent = ANY as IN */
6251 appendStringInfo(buf, " IN ");
6253 appendStringInfo(buf, " %s ANY ", opname);
6257 appendStringInfo(buf, " %s ALL ", opname);
6260 case ROWCOMPARE_SUBLINK:
6261 appendStringInfo(buf, " %s ", opname);
6269 case CTE_SUBLINK: /* shouldn't occur in a SubLink */
6271 elog(ERROR, "unrecognized sublink type: %d",
6272 (int) sublink->subLinkType);
6277 appendStringInfoChar(buf, '(');
6279 get_query_def(query, buf, context->namespaces, NULL,
6280 context->prettyFlags, context->indentLevel);
6283 appendStringInfo(buf, "))");
6285 appendStringInfoChar(buf, ')');
6290 * get_from_clause - Parse back a FROM clause
6292 * "prefix" is the keyword that denotes the start of the list of FROM
6293 * elements. It is FROM when used to parse back SELECT and UPDATE, but
6294 * is USING when parsing back DELETE.
6298 get_from_clause(Query *query, const char *prefix, deparse_context *context)
6300 StringInfo buf = context->buf;
6305 * We use the query's jointree as a guide to what to print. However, we
6306 * must ignore auto-added RTEs that are marked not inFromCl. (These can
6307 * only appear at the top level of the jointree, so it's sufficient to
6308 * check here.) This check also ensures we ignore the rule pseudo-RTEs
6311 foreach(l, query->jointree->fromlist)
6313 Node *jtnode = (Node *) lfirst(l);
6315 if (IsA(jtnode, RangeTblRef))
6317 int varno = ((RangeTblRef *) jtnode)->rtindex;
6318 RangeTblEntry *rte = rt_fetch(varno, query->rtable);
6326 appendContextKeyword(context, prefix,
6327 -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
6331 appendStringInfoString(buf, ", ");
6333 get_from_clause_item(jtnode, query, context);
6338 get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
6340 StringInfo buf = context->buf;
6342 if (IsA(jtnode, RangeTblRef))
6344 int varno = ((RangeTblRef *) jtnode)->rtindex;
6345 RangeTblEntry *rte = rt_fetch(varno, query->rtable);
6346 bool gavealias = false;
6348 switch (rte->rtekind)
6351 /* Normal relation RTE */
6352 appendStringInfo(buf, "%s%s",
6354 generate_relation_name(rte->relid,
6355 context->namespaces));
6359 appendStringInfoChar(buf, '(');
6360 get_query_def(rte->subquery, buf, context->namespaces, NULL,
6361 context->prettyFlags, context->indentLevel);
6362 appendStringInfoChar(buf, ')');
6366 get_rule_expr(rte->funcexpr, context, true);
6369 /* Values list RTE */
6370 get_values_def(rte->values_lists, context);
6373 appendStringInfoString(buf, quote_identifier(rte->ctename));
6376 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
6380 if (rte->alias != NULL)
6382 appendStringInfo(buf, " %s",
6383 quote_identifier(rte->alias->aliasname));
6386 else if (rte->rtekind == RTE_RELATION &&
6387 strcmp(rte->eref->aliasname, get_relation_name(rte->relid)) != 0)
6390 * Apparently the rel has been renamed since the rule was made.
6391 * Emit a fake alias clause so that variable references will still
6392 * work. This is not a 100% solution but should work in most
6393 * reasonable situations.
6395 appendStringInfo(buf, " %s",
6396 quote_identifier(rte->eref->aliasname));
6399 else if (rte->rtekind == RTE_FUNCTION)
6402 * For a function RTE, always give an alias. This covers possible
6403 * renaming of the function and/or instability of the
6404 * FigureColname rules for things that aren't simple functions.
6406 appendStringInfo(buf, " %s",
6407 quote_identifier(rte->eref->aliasname));
6411 if (rte->rtekind == RTE_FUNCTION)
6413 if (rte->funccoltypes != NIL)
6415 /* Function returning RECORD, reconstruct the columndefs */
6417 appendStringInfo(buf, " AS ");
6418 get_from_clause_coldeflist(rte->eref->colnames,
6420 rte->funccoltypmods,
6421 rte->funccolcollations,
6427 * For a function RTE, always emit a complete column alias
6428 * list; this is to protect against possible instability of
6429 * the default column names (eg, from altering parameter
6432 get_from_clause_alias(rte->eref, rte, context);
6438 * For non-function RTEs, just report whatever the user originally
6439 * gave as column aliases.
6441 get_from_clause_alias(rte->alias, rte, context);
6444 else if (IsA(jtnode, JoinExpr))
6446 JoinExpr *j = (JoinExpr *) jtnode;
6447 bool need_paren_on_right;
6449 need_paren_on_right = PRETTY_PAREN(context) &&
6450 !IsA(j->rarg, RangeTblRef) &&
6451 !(IsA(j->rarg, JoinExpr) &&((JoinExpr *) j->rarg)->alias != NULL);
6453 if (!PRETTY_PAREN(context) || j->alias != NULL)
6454 appendStringInfoChar(buf, '(');
6456 get_from_clause_item(j->larg, query, context);
6460 if (!PRETTY_INDENT(context))
6461 appendStringInfoChar(buf, ' ');
6462 switch (j->jointype)
6465 appendContextKeyword(context, "NATURAL JOIN ",
6467 PRETTYINDENT_JOIN, 0);
6470 appendContextKeyword(context, "NATURAL LEFT JOIN ",
6472 PRETTYINDENT_JOIN, 0);
6475 appendContextKeyword(context, "NATURAL FULL JOIN ",
6477 PRETTYINDENT_JOIN, 0);
6480 appendContextKeyword(context, "NATURAL RIGHT JOIN ",
6482 PRETTYINDENT_JOIN, 0);
6485 elog(ERROR, "unrecognized join type: %d",
6491 switch (j->jointype)
6495 appendContextKeyword(context, " JOIN ",
6497 PRETTYINDENT_JOIN, 2);
6499 appendContextKeyword(context, " CROSS JOIN ",
6501 PRETTYINDENT_JOIN, 1);
6504 appendContextKeyword(context, " LEFT JOIN ",
6506 PRETTYINDENT_JOIN, 2);
6509 appendContextKeyword(context, " FULL JOIN ",
6511 PRETTYINDENT_JOIN, 2);
6514 appendContextKeyword(context, " RIGHT JOIN ",
6516 PRETTYINDENT_JOIN, 2);
6519 elog(ERROR, "unrecognized join type: %d",
6524 if (need_paren_on_right)
6525 appendStringInfoChar(buf, '(');
6526 get_from_clause_item(j->rarg, query, context);
6527 if (need_paren_on_right)
6528 appendStringInfoChar(buf, ')');
6530 context->indentLevel -= PRETTYINDENT_JOIN_ON;
6538 appendStringInfo(buf, " USING (");
6539 foreach(col, j->usingClause)
6541 if (col != list_head(j->usingClause))
6542 appendStringInfo(buf, ", ");
6543 appendStringInfoString(buf,
6544 quote_identifier(strVal(lfirst(col))));
6546 appendStringInfoChar(buf, ')');
6550 appendStringInfo(buf, " ON ");
6551 if (!PRETTY_PAREN(context))
6552 appendStringInfoChar(buf, '(');
6553 get_rule_expr(j->quals, context, false);
6554 if (!PRETTY_PAREN(context))
6555 appendStringInfoChar(buf, ')');
6558 if (!PRETTY_PAREN(context) || j->alias != NULL)
6559 appendStringInfoChar(buf, ')');
6561 /* Yes, it's correct to put alias after the right paren ... */
6562 if (j->alias != NULL)
6564 appendStringInfo(buf, " %s",
6565 quote_identifier(j->alias->aliasname));
6566 get_from_clause_alias(j->alias,
6567 rt_fetch(j->rtindex, query->rtable),
6572 elog(ERROR, "unrecognized node type: %d",
6573 (int) nodeTag(jtnode));
6577 * get_from_clause_alias - reproduce column alias list
6579 * This is tricky because we must ignore dropped columns.
6582 get_from_clause_alias(Alias *alias, RangeTblEntry *rte,
6583 deparse_context *context)
6585 StringInfo buf = context->buf;
6590 if (alias == NULL || alias->colnames == NIL)
6591 return; /* definitely nothing to do */
6594 foreach(col, alias->colnames)
6597 if (get_rte_attribute_is_dropped(rte, attnum))
6601 appendStringInfoChar(buf, '(');
6605 appendStringInfo(buf, ", ");
6606 appendStringInfoString(buf,
6607 quote_identifier(strVal(lfirst(col))));
6610 appendStringInfoChar(buf, ')');
6614 * get_from_clause_coldeflist - reproduce FROM clause coldeflist
6616 * The coldeflist is appended immediately (no space) to buf. Caller is
6617 * responsible for ensuring that an alias or AS is present before it.
6620 get_from_clause_coldeflist(List *names, List *types, List *typmods, List *collations,
6621 deparse_context *context)
6623 StringInfo buf = context->buf;
6630 appendStringInfoChar(buf, '(');
6632 l2 = list_head(types);
6633 l3 = list_head(typmods);
6634 l4 = list_head(collations);
6637 char *attname = strVal(lfirst(l1));
6642 atttypid = lfirst_oid(l2);
6644 atttypmod = lfirst_int(l3);
6646 attcollation = lfirst_oid(l4);
6650 appendStringInfo(buf, ", ");
6651 appendStringInfo(buf, "%s %s",
6652 quote_identifier(attname),
6653 format_type_with_typemod(atttypid, atttypmod));
6654 if (attcollation && attcollation != DEFAULT_COLLATION_OID)
6655 appendStringInfo(buf, " COLLATE %s",
6656 generate_collation_name(attcollation));
6660 appendStringInfoChar(buf, ')');
6664 * get_opclass_name - fetch name of an index operator class
6666 * The opclass name is appended (after a space) to buf.
6668 * Output is suppressed if the opclass is the default for the given
6669 * actual_datatype. (If you don't want this behavior, just pass
6670 * InvalidOid for actual_datatype.)
6673 get_opclass_name(Oid opclass, Oid actual_datatype,
6677 Form_pg_opclass opcrec;
6681 ht_opc = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
6682 if (!HeapTupleIsValid(ht_opc))
6683 elog(ERROR, "cache lookup failed for opclass %u", opclass);
6684 opcrec = (Form_pg_opclass) GETSTRUCT(ht_opc);
6686 if (!OidIsValid(actual_datatype) ||
6687 GetDefaultOpClass(actual_datatype, opcrec->opcmethod) != opclass)
6689 /* Okay, we need the opclass name. Do we need to qualify it? */
6690 opcname = NameStr(opcrec->opcname);
6691 if (OpclassIsVisible(opclass))
6692 appendStringInfo(buf, " %s", quote_identifier(opcname));
6695 nspname = get_namespace_name(opcrec->opcnamespace);
6696 appendStringInfo(buf, " %s.%s",
6697 quote_identifier(nspname),
6698 quote_identifier(opcname));
6701 ReleaseSysCache(ht_opc);
6705 * processIndirection - take care of array and subfield assignment
6707 * We strip any top-level FieldStore or assignment ArrayRef nodes that
6708 * appear in the input, and return the subexpression that's to be assigned.
6709 * If printit is true, we also print out the appropriate decoration for the
6710 * base column name (that the caller just printed).
6713 processIndirection(Node *node, deparse_context *context, bool printit)
6715 StringInfo buf = context->buf;
6721 if (IsA(node, FieldStore))
6723 FieldStore *fstore = (FieldStore *) node;
6727 /* lookup tuple type */
6728 typrelid = get_typ_typrelid(fstore->resulttype);
6729 if (!OidIsValid(typrelid))
6730 elog(ERROR, "argument type %s of FieldStore is not a tuple type",
6731 format_type_be(fstore->resulttype));
6734 * Print the field name. There should only be one target field in
6735 * stored rules. There could be more than that in executable
6736 * target lists, but this function cannot be used for that case.
6738 Assert(list_length(fstore->fieldnums) == 1);
6739 fieldname = get_relid_attribute_name(typrelid,
6740 linitial_int(fstore->fieldnums));
6742 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
6745 * We ignore arg since it should be an uninteresting reference to
6746 * the target column or subcolumn.
6748 node = (Node *) linitial(fstore->newvals);
6750 else if (IsA(node, ArrayRef))
6752 ArrayRef *aref = (ArrayRef *) node;
6754 if (aref->refassgnexpr == NULL)
6757 printSubscripts(aref, context);
6760 * We ignore refexpr since it should be an uninteresting reference
6761 * to the target column or subcolumn.
6763 node = (Node *) aref->refassgnexpr;
6773 printSubscripts(ArrayRef *aref, deparse_context *context)
6775 StringInfo buf = context->buf;
6776 ListCell *lowlist_item;
6777 ListCell *uplist_item;
6779 lowlist_item = list_head(aref->reflowerindexpr); /* could be NULL */
6780 foreach(uplist_item, aref->refupperindexpr)
6782 appendStringInfoChar(buf, '[');
6785 get_rule_expr((Node *) lfirst(lowlist_item), context, false);
6786 appendStringInfoChar(buf, ':');
6787 lowlist_item = lnext(lowlist_item);
6789 get_rule_expr((Node *) lfirst(uplist_item), context, false);
6790 appendStringInfoChar(buf, ']');
6795 * quote_identifier - Quote an identifier only if needed
6797 * When quotes are needed, we palloc the required space; slightly
6798 * space-wasteful but well worth it for notational simplicity.
6801 quote_identifier(const char *ident)
6804 * Can avoid quoting if ident starts with a lowercase letter or underscore
6805 * and contains only lowercase letters, digits, and underscores, *and* is
6806 * not any SQL keyword. Otherwise, supply quotes.
6815 * would like to use <ctype.h> macros here, but they might yield unwanted
6816 * locale-specific results...
6818 safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
6820 for (ptr = ident; *ptr; ptr++)
6824 if ((ch >= 'a' && ch <= 'z') ||
6825 (ch >= '0' && ch <= '9') ||
6838 if (quote_all_identifiers)
6844 * Check for keyword. We quote keywords except for unreserved ones.
6845 * (In some cases we could avoid quoting a col_name or type_func_name
6846 * keyword, but it seems much harder than it's worth to tell that.)
6848 * Note: ScanKeywordLookup() does case-insensitive comparison, but
6849 * that's fine, since we already know we have all-lower-case.
6851 const ScanKeyword *keyword = ScanKeywordLookup(ident,
6855 if (keyword != NULL && keyword->category != UNRESERVED_KEYWORD)
6860 return ident; /* no change needed */
6862 result = (char *) palloc(strlen(ident) + nquotes + 2 + 1);
6866 for (ptr = ident; *ptr; ptr++)
6881 * quote_qualified_identifier - Quote a possibly-qualified identifier
6883 * Return a name of the form qualifier.ident, or just ident if qualifier
6884 * is NULL, quoting each component if necessary. The result is palloc'd.
6887 quote_qualified_identifier(const char *qualifier,
6892 initStringInfo(&buf);
6894 appendStringInfo(&buf, "%s.", quote_identifier(qualifier));
6895 appendStringInfoString(&buf, quote_identifier(ident));
6901 * Get the unqualified name of a relation specified by OID
6903 * This differs from the underlying get_rel_name() function in that it will
6904 * throw error instead of silently returning NULL if the OID is bad.
6907 get_relation_name(Oid relid)
6909 char *relname = get_rel_name(relid);
6912 elog(ERROR, "cache lookup failed for relation %u", relid);
6917 * generate_relation_name
6918 * Compute the name to display for a relation specified by OID
6920 * The result includes all necessary quoting and schema-prefixing.
6922 * If namespaces isn't NIL, it must be a list of deparse_namespace nodes.
6923 * We will forcibly qualify the relation name if it equals any CTE name
6924 * visible in the namespace list.
6927 generate_relation_name(Oid relid, List *namespaces)
6930 Form_pg_class reltup;
6937 tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
6938 if (!HeapTupleIsValid(tp))
6939 elog(ERROR, "cache lookup failed for relation %u", relid);
6940 reltup = (Form_pg_class) GETSTRUCT(tp);
6941 relname = NameStr(reltup->relname);
6943 /* Check for conflicting CTE name */
6945 foreach(nslist, namespaces)
6947 deparse_namespace *dpns = (deparse_namespace *) lfirst(nslist);
6950 foreach(ctlist, dpns->ctes)
6952 CommonTableExpr *cte = (CommonTableExpr *) lfirst(ctlist);
6954 if (strcmp(cte->ctename, relname) == 0)
6964 /* Otherwise, qualify the name if not visible in search path */
6966 need_qual = !RelationIsVisible(relid);
6969 nspname = get_namespace_name(reltup->relnamespace);
6973 result = quote_qualified_identifier(nspname, relname);
6975 ReleaseSysCache(tp);
6981 * generate_function_name
6982 * Compute the name to display for a function specified by OID,
6983 * given that it is being called with the specified actual arg names and
6984 * types. (Those matter because of ambiguous-function resolution rules.)
6986 * The result includes all necessary quoting and schema-prefixing. We can
6987 * also pass back an indication of whether the function is variadic.
6990 generate_function_name(Oid funcid, int nargs, List *argnames,
6991 Oid *argtypes, bool *is_variadic)
6994 Form_pg_proc procform;
6998 FuncDetailCode p_result;
7003 Oid *p_true_typeids;
7005 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
7006 if (!HeapTupleIsValid(proctup))
7007 elog(ERROR, "cache lookup failed for function %u", funcid);
7008 procform = (Form_pg_proc) GETSTRUCT(proctup);
7009 proname = NameStr(procform->proname);
7012 * The idea here is to schema-qualify only if the parser would fail to
7013 * resolve the correct function given the unqualified func name with the
7014 * specified argtypes. If the function is variadic, we should presume
7015 * that VARIADIC will be included in the call.
7017 p_result = func_get_detail(list_make1(makeString(proname)),
7018 NIL, argnames, nargs, argtypes,
7019 !OidIsValid(procform->provariadic), true,
7020 &p_funcid, &p_rettype,
7021 &p_retset, &p_nvargs, &p_true_typeids, NULL);
7022 if ((p_result == FUNCDETAIL_NORMAL ||
7023 p_result == FUNCDETAIL_AGGREGATE ||
7024 p_result == FUNCDETAIL_WINDOWFUNC) &&
7028 nspname = get_namespace_name(procform->pronamespace);
7030 result = quote_qualified_identifier(nspname, proname);
7032 /* Check variadic-ness if caller cares */
7035 /* "any" variadics are not treated as variadics for listing */
7036 if (OidIsValid(procform->provariadic) &&
7037 procform->provariadic != ANYOID)
7038 *is_variadic = true;
7040 *is_variadic = false;
7043 ReleaseSysCache(proctup);
7049 * generate_operator_name
7050 * Compute the name to display for an operator specified by OID,
7051 * given that it is being called with the specified actual arg types.
7052 * (Arg types matter because of ambiguous-operator resolution rules.
7053 * Pass InvalidOid for unused arg of a unary operator.)
7055 * The result includes all necessary quoting and schema-prefixing,
7056 * plus the OPERATOR() decoration needed to use a qualified operator name
7060 generate_operator_name(Oid operid, Oid arg1, Oid arg2)
7064 Form_pg_operator operform;
7069 initStringInfo(&buf);
7071 opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operid));
7072 if (!HeapTupleIsValid(opertup))
7073 elog(ERROR, "cache lookup failed for operator %u", operid);
7074 operform = (Form_pg_operator) GETSTRUCT(opertup);
7075 oprname = NameStr(operform->oprname);
7078 * The idea here is to schema-qualify only if the parser would fail to
7079 * resolve the correct operator given the unqualified op name with the
7080 * specified argtypes.
7082 switch (operform->oprkind)
7085 p_result = oper(NULL, list_make1(makeString(oprname)), arg1, arg2,
7089 p_result = left_oper(NULL, list_make1(makeString(oprname)), arg2,
7093 p_result = right_oper(NULL, list_make1(makeString(oprname)), arg1,
7097 elog(ERROR, "unrecognized oprkind: %d", operform->oprkind);
7098 p_result = NULL; /* keep compiler quiet */
7102 if (p_result != NULL && oprid(p_result) == operid)
7106 nspname = get_namespace_name(operform->oprnamespace);
7107 appendStringInfo(&buf, "OPERATOR(%s.", quote_identifier(nspname));
7110 appendStringInfoString(&buf, oprname);
7113 appendStringInfoChar(&buf, ')');
7115 if (p_result != NULL)
7116 ReleaseSysCache(p_result);
7118 ReleaseSysCache(opertup);
7124 * generate_collation_name
7125 * Compute the name to display for a collation specified by OID
7127 * The result includes all necessary quoting and schema-prefixing.
7130 generate_collation_name(Oid collid)
7133 Form_pg_collation colltup;
7138 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
7139 if (!HeapTupleIsValid(tp))
7140 elog(ERROR, "cache lookup failed for collation %u", collid);
7141 colltup = (Form_pg_collation) GETSTRUCT(tp);
7142 collname = NameStr(colltup->collname);
7144 if (!CollationIsVisible(collid))
7145 nspname = get_namespace_name(colltup->collnamespace);
7149 result = quote_qualified_identifier(nspname, collname);
7151 ReleaseSysCache(tp);
7157 * Given a C string, produce a TEXT datum.
7159 * We assume that the input was palloc'd and may be freed.
7162 string_to_text(char *str)
7166 result = cstring_to_text(str);
7172 * Generate a C string representing a relation's reloptions, or NULL if none.
7175 flatten_reloptions(Oid relid)
7177 char *result = NULL;
7182 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
7183 if (!HeapTupleIsValid(tuple))
7184 elog(ERROR, "cache lookup failed for relation %u", relid);
7186 reloptions = SysCacheGetAttr(RELOID, tuple,
7187 Anum_pg_class_reloptions, &isnull);
7194 * We want to use array_to_text(reloptions, ', ') --- but
7195 * DirectFunctionCall2(array_to_text) does not work, because
7196 * array_to_text() relies on flinfo to be valid. So use
7199 sep = CStringGetTextDatum(", ");
7200 txt = OidFunctionCall2(F_ARRAY_TO_TEXT, reloptions, sep);
7201 result = TextDatumGetCString(txt);
7204 ReleaseSysCache(tuple);