LCOV - code coverage report
Current view: top level - src/backend/utils/adt - ruleutils.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 3903 4444 87.8 %
Date: 2020-12-05 17:06:23 Functions: 137 137 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * ruleutils.c
       4             :  *    Functions to convert stored expressions/querytrees back to
       5             :  *    source text
       6             :  *
       7             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/utils/adt/ruleutils.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : #include "postgres.h"
      17             : 
      18             : #include <ctype.h>
      19             : #include <unistd.h>
      20             : #include <fcntl.h>
      21             : 
      22             : #include "access/amapi.h"
      23             : #include "access/htup_details.h"
      24             : #include "access/relation.h"
      25             : #include "access/sysattr.h"
      26             : #include "access/table.h"
      27             : #include "catalog/dependency.h"
      28             : #include "catalog/pg_aggregate.h"
      29             : #include "catalog/pg_am.h"
      30             : #include "catalog/pg_authid.h"
      31             : #include "catalog/pg_collation.h"
      32             : #include "catalog/pg_constraint.h"
      33             : #include "catalog/pg_depend.h"
      34             : #include "catalog/pg_language.h"
      35             : #include "catalog/pg_opclass.h"
      36             : #include "catalog/pg_operator.h"
      37             : #include "catalog/pg_partitioned_table.h"
      38             : #include "catalog/pg_proc.h"
      39             : #include "catalog/pg_statistic_ext.h"
      40             : #include "catalog/pg_trigger.h"
      41             : #include "catalog/pg_type.h"
      42             : #include "commands/defrem.h"
      43             : #include "commands/tablespace.h"
      44             : #include "common/keywords.h"
      45             : #include "executor/spi.h"
      46             : #include "funcapi.h"
      47             : #include "mb/pg_wchar.h"
      48             : #include "miscadmin.h"
      49             : #include "nodes/makefuncs.h"
      50             : #include "nodes/nodeFuncs.h"
      51             : #include "nodes/pathnodes.h"
      52             : #include "optimizer/optimizer.h"
      53             : #include "parser/parse_agg.h"
      54             : #include "parser/parse_func.h"
      55             : #include "parser/parse_node.h"
      56             : #include "parser/parse_oper.h"
      57             : #include "parser/parser.h"
      58             : #include "parser/parsetree.h"
      59             : #include "rewrite/rewriteHandler.h"
      60             : #include "rewrite/rewriteManip.h"
      61             : #include "rewrite/rewriteSupport.h"
      62             : #include "utils/array.h"
      63             : #include "utils/builtins.h"
      64             : #include "utils/fmgroids.h"
      65             : #include "utils/guc.h"
      66             : #include "utils/hsearch.h"
      67             : #include "utils/lsyscache.h"
      68             : #include "utils/partcache.h"
      69             : #include "utils/rel.h"
      70             : #include "utils/ruleutils.h"
      71             : #include "utils/snapmgr.h"
      72             : #include "utils/syscache.h"
      73             : #include "utils/typcache.h"
      74             : #include "utils/varlena.h"
      75             : #include "utils/xml.h"
      76             : 
      77             : /* ----------
      78             :  * Pretty formatting constants
      79             :  * ----------
      80             :  */
      81             : 
      82             : /* Indent counts */
      83             : #define PRETTYINDENT_STD        8
      84             : #define PRETTYINDENT_JOIN       4
      85             : #define PRETTYINDENT_VAR        4
      86             : 
      87             : #define PRETTYINDENT_LIMIT      40  /* wrap limit */
      88             : 
      89             : /* Pretty flags */
      90             : #define PRETTYFLAG_PAREN        0x0001
      91             : #define PRETTYFLAG_INDENT       0x0002
      92             : #define PRETTYFLAG_SCHEMA       0x0004
      93             : 
      94             : /* Default line length for pretty-print wrapping: 0 means wrap always */
      95             : #define WRAP_COLUMN_DEFAULT     0
      96             : 
      97             : /* macros to test if pretty action needed */
      98             : #define PRETTY_PAREN(context)   ((context)->prettyFlags & PRETTYFLAG_PAREN)
      99             : #define PRETTY_INDENT(context)  ((context)->prettyFlags & PRETTYFLAG_INDENT)
     100             : #define PRETTY_SCHEMA(context)  ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
     101             : 
     102             : 
     103             : /* ----------
     104             :  * Local data types
     105             :  * ----------
     106             :  */
     107             : 
     108             : /* Context info needed for invoking a recursive querytree display routine */
     109             : typedef struct
     110             : {
     111             :     StringInfo  buf;            /* output buffer to append to */
     112             :     List       *namespaces;     /* List of deparse_namespace nodes */
     113             :     List       *windowClause;   /* Current query level's WINDOW clause */
     114             :     List       *windowTList;    /* targetlist for resolving WINDOW clause */
     115             :     int         prettyFlags;    /* enabling of pretty-print functions */
     116             :     int         wrapColumn;     /* max line length, or -1 for no limit */
     117             :     int         indentLevel;    /* current indent level for pretty-print */
     118             :     bool        varprefix;      /* true to print prefixes on Vars */
     119             :     ParseExprKind special_exprkind; /* set only for exprkinds needing special
     120             :                                      * handling */
     121             :     Bitmapset  *appendparents;  /* if not null, map child Vars of these relids
     122             :                                  * back to the parent rel */
     123             : } deparse_context;
     124             : 
     125             : /*
     126             :  * Each level of query context around a subtree needs a level of Var namespace.
     127             :  * A Var having varlevelsup=N refers to the N'th item (counting from 0) in
     128             :  * the current context's namespaces list.
     129             :  *
     130             :  * rtable is the list of actual RTEs from the Query or PlannedStmt.
     131             :  * rtable_names holds the alias name to be used for each RTE (either a C
     132             :  * string, or NULL for nameless RTEs such as unnamed joins).
     133             :  * rtable_columns holds the column alias names to be used for each RTE.
     134             :  *
     135             :  * subplans is a list of Plan trees for SubPlans and CTEs (it's only used
     136             :  * in the PlannedStmt case).
     137             :  * ctes is a list of CommonTableExpr nodes (only used in the Query case).
     138             :  * appendrels, if not null (it's only used in the PlannedStmt case), is an
     139             :  * array of AppendRelInfo nodes, indexed by child relid.  We use that to map
     140             :  * child-table Vars to their inheritance parents.
     141             :  *
     142             :  * In some cases we need to make names of merged JOIN USING columns unique
     143             :  * across the whole query, not only per-RTE.  If so, unique_using is true
     144             :  * and using_names is a list of C strings representing names already assigned
     145             :  * to USING columns.
     146             :  *
     147             :  * When deparsing plan trees, there is always just a single item in the
     148             :  * deparse_namespace list (since a plan tree never contains Vars with
     149             :  * varlevelsup > 0).  We store the Plan node that is the immediate
     150             :  * parent of the expression to be deparsed, as well as a list of that
     151             :  * Plan's ancestors.  In addition, we store its outer and inner subplan nodes,
     152             :  * as well as their targetlists, and the index tlist if the current plan node
     153             :  * might contain INDEX_VAR Vars.  (These fields could be derived on-the-fly
     154             :  * from the current Plan node, but it seems notationally clearer to set them
     155             :  * up as separate fields.)
     156             :  */
     157             : typedef struct
     158             : {
     159             :     List       *rtable;         /* List of RangeTblEntry nodes */
     160             :     List       *rtable_names;   /* Parallel list of names for RTEs */
     161             :     List       *rtable_columns; /* Parallel list of deparse_columns structs */
     162             :     List       *subplans;       /* List of Plan trees for SubPlans */
     163             :     List       *ctes;           /* List of CommonTableExpr nodes */
     164             :     AppendRelInfo **appendrels; /* Array of AppendRelInfo nodes, or NULL */
     165             :     /* Workspace for column alias assignment: */
     166             :     bool        unique_using;   /* Are we making USING names globally unique */
     167             :     List       *using_names;    /* List of assigned names for USING columns */
     168             :     /* Remaining fields are used only when deparsing a Plan tree: */
     169             :     Plan       *plan;           /* immediate parent of current expression */
     170             :     List       *ancestors;      /* ancestors of plan */
     171             :     Plan       *outer_plan;     /* outer subnode, or NULL if none */
     172             :     Plan       *inner_plan;     /* inner subnode, or NULL if none */
     173             :     List       *outer_tlist;    /* referent for OUTER_VAR Vars */
     174             :     List       *inner_tlist;    /* referent for INNER_VAR Vars */
     175             :     List       *index_tlist;    /* referent for INDEX_VAR Vars */
     176             : } deparse_namespace;
     177             : 
     178             : /*
     179             :  * Per-relation data about column alias names.
     180             :  *
     181             :  * Selecting aliases is unreasonably complicated because of the need to dump
     182             :  * rules/views whose underlying tables may have had columns added, deleted, or
     183             :  * renamed since the query was parsed.  We must nonetheless print the rule/view
     184             :  * in a form that can be reloaded and will produce the same results as before.
     185             :  *
     186             :  * For each RTE used in the query, we must assign column aliases that are
     187             :  * unique within that RTE.  SQL does not require this of the original query,
     188             :  * but due to factors such as *-expansion we need to be able to uniquely
     189             :  * reference every column in a decompiled query.  As long as we qualify all
     190             :  * column references, per-RTE uniqueness is sufficient for that.
     191             :  *
     192             :  * However, we can't ensure per-column name uniqueness for unnamed join RTEs,
     193             :  * since they just inherit column names from their input RTEs, and we can't
     194             :  * rename the columns at the join level.  Most of the time this isn't an issue
     195             :  * because we don't need to reference the join's output columns as such; we
     196             :  * can reference the input columns instead.  That approach can fail for merged
     197             :  * JOIN USING columns, however, so when we have one of those in an unnamed
     198             :  * join, we have to make that column's alias globally unique across the whole
     199             :  * query to ensure it can be referenced unambiguously.
     200             :  *
     201             :  * Another problem is that a JOIN USING clause requires the columns to be
     202             :  * merged to have the same aliases in both input RTEs, and that no other
     203             :  * columns in those RTEs or their children conflict with the USING names.
     204             :  * To handle that, we do USING-column alias assignment in a recursive
     205             :  * traversal of the query's jointree.  When descending through a JOIN with
     206             :  * USING, we preassign the USING column names to the child columns, overriding
     207             :  * other rules for column alias assignment.  We also mark each RTE with a list
     208             :  * of all USING column names selected for joins containing that RTE, so that
     209             :  * when we assign other columns' aliases later, we can avoid conflicts.
     210             :  *
     211             :  * Another problem is that if a JOIN's input tables have had columns added or
     212             :  * deleted since the query was parsed, we must generate a column alias list
     213             :  * for the join that matches the current set of input columns --- otherwise, a
     214             :  * change in the number of columns in the left input would throw off matching
     215             :  * of aliases to columns of the right input.  Thus, positions in the printable
     216             :  * column alias list are not necessarily one-for-one with varattnos of the
     217             :  * JOIN, so we need a separate new_colnames[] array for printing purposes.
     218             :  */
     219             : typedef struct
     220             : {
     221             :     /*
     222             :      * colnames is an array containing column aliases to use for columns that
     223             :      * existed when the query was parsed.  Dropped columns have NULL entries.
     224             :      * This array can be directly indexed by varattno to get a Var's name.
     225             :      *
     226             :      * Non-NULL entries are guaranteed unique within the RTE, *except* when
     227             :      * this is for an unnamed JOIN RTE.  In that case we merely copy up names
     228             :      * from the two input RTEs.
     229             :      *
     230             :      * During the recursive descent in set_using_names(), forcible assignment
     231             :      * of a child RTE's column name is represented by pre-setting that element
     232             :      * of the child's colnames array.  So at that stage, NULL entries in this
     233             :      * array just mean that no name has been preassigned, not necessarily that
     234             :      * the column is dropped.
     235             :      */
     236             :     int         num_cols;       /* length of colnames[] array */
     237             :     char      **colnames;       /* array of C strings and NULLs */
     238             : 
     239             :     /*
     240             :      * new_colnames is an array containing column aliases to use for columns
     241             :      * that would exist if the query was re-parsed against the current
     242             :      * definitions of its base tables.  This is what to print as the column
     243             :      * alias list for the RTE.  This array does not include dropped columns,
     244             :      * but it will include columns added since original parsing.  Indexes in
     245             :      * it therefore have little to do with current varattno values.  As above,
     246             :      * entries are unique unless this is for an unnamed JOIN RTE.  (In such an
     247             :      * RTE, we never actually print this array, but we must compute it anyway
     248             :      * for possible use in computing column names of upper joins.) The
     249             :      * parallel array is_new_col marks which of these columns are new since
     250             :      * original parsing.  Entries with is_new_col false must match the
     251             :      * non-NULL colnames entries one-for-one.
     252             :      */
     253             :     int         num_new_cols;   /* length of new_colnames[] array */
     254             :     char      **new_colnames;   /* array of C strings */
     255             :     bool       *is_new_col;     /* array of bool flags */
     256             : 
     257             :     /* This flag tells whether we should actually print a column alias list */
     258             :     bool        printaliases;
     259             : 
     260             :     /* This list has all names used as USING names in joins above this RTE */
     261             :     List       *parentUsing;    /* names assigned to parent merged columns */
     262             : 
     263             :     /*
     264             :      * If this struct is for a JOIN RTE, we fill these fields during the
     265             :      * set_using_names() pass to describe its relationship to its child RTEs.
     266             :      *
     267             :      * leftattnos and rightattnos are arrays with one entry per existing
     268             :      * output column of the join (hence, indexable by join varattno).  For a
     269             :      * simple reference to a column of the left child, leftattnos[i] is the
     270             :      * child RTE's attno and rightattnos[i] is zero; and conversely for a
     271             :      * column of the right child.  But for merged columns produced by JOIN
     272             :      * USING/NATURAL JOIN, both leftattnos[i] and rightattnos[i] are nonzero.
     273             :      * Note that a simple reference might be to a child RTE column that's been
     274             :      * dropped; but that's OK since the column could not be used in the query.
     275             :      *
     276             :      * If it's a JOIN USING, usingNames holds the alias names selected for the
     277             :      * merged columns (these might be different from the original USING list,
     278             :      * if we had to modify names to achieve uniqueness).
     279             :      */
     280             :     int         leftrti;        /* rangetable index of left child */
     281             :     int         rightrti;       /* rangetable index of right child */
     282             :     int        *leftattnos;     /* left-child varattnos of join cols, or 0 */
     283             :     int        *rightattnos;    /* right-child varattnos of join cols, or 0 */
     284             :     List       *usingNames;     /* names assigned to merged columns */
     285             : } deparse_columns;
     286             : 
     287             : /* This macro is analogous to rt_fetch(), but for deparse_columns structs */
     288             : #define deparse_columns_fetch(rangetable_index, dpns) \
     289             :     ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
     290             : 
     291             : /*
     292             :  * Entry in set_rtable_names' hash table
     293             :  */
     294             : typedef struct
     295             : {
     296             :     char        name[NAMEDATALEN];  /* Hash key --- must be first */
     297             :     int         counter;        /* Largest addition used so far for name */
     298             : } NameHashEntry;
     299             : 
     300             : /* Callback signature for resolve_special_varno() */
     301             : typedef void (*rsv_callback) (Node *node, deparse_context *context,
     302             :                               void *callback_arg);
     303             : 
     304             : 
     305             : /* ----------
     306             :  * Global data
     307             :  * ----------
     308             :  */
     309             : static SPIPlanPtr plan_getrulebyoid = NULL;
     310             : static const char *query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1";
     311             : static SPIPlanPtr plan_getviewrule = NULL;
     312             : static const char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
     313             : 
     314             : /* GUC parameters */
     315             : bool        quote_all_identifiers = false;
     316             : 
     317             : 
     318             : /* ----------
     319             :  * Local functions
     320             :  *
     321             :  * Most of these functions used to use fixed-size buffers to build their
     322             :  * results.  Now, they take an (already initialized) StringInfo object
     323             :  * as a parameter, and append their text output to its contents.
     324             :  * ----------
     325             :  */
     326             : static char *deparse_expression_pretty(Node *expr, List *dpcontext,
     327             :                                        bool forceprefix, bool showimplicit,
     328             :                                        int prettyFlags, int startIndent);
     329             : static char *pg_get_viewdef_worker(Oid viewoid,
     330             :                                    int prettyFlags, int wrapColumn);
     331             : static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
     332             : static int  decompile_column_index_array(Datum column_index_array, Oid relId,
     333             :                                          StringInfo buf);
     334             : static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
     335             : static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
     336             :                                     const Oid *excludeOps,
     337             :                                     bool attrsOnly, bool keysOnly,
     338             :                                     bool showTblSpc, bool inherits,
     339             :                                     int prettyFlags, bool missing_ok);
     340             : static char *pg_get_statisticsobj_worker(Oid statextid, bool missing_ok);
     341             : static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags,
     342             :                                       bool attrsOnly, bool missing_ok);
     343             : static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
     344             :                                          int prettyFlags, bool missing_ok);
     345             : static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname,
     346             :                                 int prettyFlags);
     347             : static int  print_function_arguments(StringInfo buf, HeapTuple proctup,
     348             :                                      bool print_table_args, bool print_defaults);
     349             : static void print_function_rettype(StringInfo buf, HeapTuple proctup);
     350             : static void print_function_trftypes(StringInfo buf, HeapTuple proctup);
     351             : static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
     352             :                              Bitmapset *rels_used);
     353             : static void set_deparse_for_query(deparse_namespace *dpns, Query *query,
     354             :                                   List *parent_namespaces);
     355             : static void set_simple_column_names(deparse_namespace *dpns);
     356             : static bool has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode);
     357             : static void set_using_names(deparse_namespace *dpns, Node *jtnode,
     358             :                             List *parentUsing);
     359             : static void set_relation_column_names(deparse_namespace *dpns,
     360             :                                       RangeTblEntry *rte,
     361             :                                       deparse_columns *colinfo);
     362             : static void set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
     363             :                                   deparse_columns *colinfo);
     364             : static bool colname_is_unique(const char *colname, deparse_namespace *dpns,
     365             :                               deparse_columns *colinfo);
     366             : static char *make_colname_unique(char *colname, deparse_namespace *dpns,
     367             :                                  deparse_columns *colinfo);
     368             : static void expand_colnames_array_to(deparse_columns *colinfo, int n);
     369             : static void identify_join_columns(JoinExpr *j, RangeTblEntry *jrte,
     370             :                                   deparse_columns *colinfo);
     371             : static char *get_rtable_name(int rtindex, deparse_context *context);
     372             : static void set_deparse_plan(deparse_namespace *dpns, Plan *plan);
     373             : static void push_child_plan(deparse_namespace *dpns, Plan *plan,
     374             :                             deparse_namespace *save_dpns);
     375             : static void pop_child_plan(deparse_namespace *dpns,
     376             :                            deparse_namespace *save_dpns);
     377             : static void push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
     378             :                                deparse_namespace *save_dpns);
     379             : static void pop_ancestor_plan(deparse_namespace *dpns,
     380             :                               deparse_namespace *save_dpns);
     381             : static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
     382             :                          int prettyFlags);
     383             : static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
     384             :                          int prettyFlags, int wrapColumn);
     385             : static void get_query_def(Query *query, StringInfo buf, List *parentnamespace,
     386             :                           TupleDesc resultDesc,
     387             :                           int prettyFlags, int wrapColumn, int startIndent);
     388             : static void get_values_def(List *values_lists, deparse_context *context);
     389             : static void get_with_clause(Query *query, deparse_context *context);
     390             : static void get_select_query_def(Query *query, deparse_context *context,
     391             :                                  TupleDesc resultDesc);
     392             : static void get_insert_query_def(Query *query, deparse_context *context);
     393             : static void get_update_query_def(Query *query, deparse_context *context);
     394             : static void get_update_query_targetlist_def(Query *query, List *targetList,
     395             :                                             deparse_context *context,
     396             :                                             RangeTblEntry *rte);
     397             : static void get_delete_query_def(Query *query, deparse_context *context);
     398             : static void get_utility_query_def(Query *query, deparse_context *context);
     399             : static void get_basic_select_query(Query *query, deparse_context *context,
     400             :                                    TupleDesc resultDesc);
     401             : static void get_target_list(List *targetList, deparse_context *context,
     402             :                             TupleDesc resultDesc);
     403             : static void get_setop_query(Node *setOp, Query *query,
     404             :                             deparse_context *context,
     405             :                             TupleDesc resultDesc);
     406             : static Node *get_rule_sortgroupclause(Index ref, List *tlist,
     407             :                                       bool force_colno,
     408             :                                       deparse_context *context);
     409             : static void get_rule_groupingset(GroupingSet *gset, List *targetlist,
     410             :                                  bool omit_parens, deparse_context *context);
     411             : static void get_rule_orderby(List *orderList, List *targetList,
     412             :                              bool force_colno, deparse_context *context);
     413             : static void get_rule_windowclause(Query *query, deparse_context *context);
     414             : static void get_rule_windowspec(WindowClause *wc, List *targetList,
     415             :                                 deparse_context *context);
     416             : static char *get_variable(Var *var, int levelsup, bool istoplevel,
     417             :                           deparse_context *context);
     418             : static void get_special_variable(Node *node, deparse_context *context,
     419             :                                  void *callback_arg);
     420             : static void resolve_special_varno(Node *node, deparse_context *context,
     421             :                                   rsv_callback callback, void *callback_arg);
     422             : static Node *find_param_referent(Param *param, deparse_context *context,
     423             :                                  deparse_namespace **dpns_p, ListCell **ancestor_cell_p);
     424             : static void get_parameter(Param *param, deparse_context *context);
     425             : static const char *get_simple_binary_op_name(OpExpr *expr);
     426             : static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);
     427             : static void appendContextKeyword(deparse_context *context, const char *str,
     428             :                                  int indentBefore, int indentAfter, int indentPlus);
     429             : static void removeStringInfoSpaces(StringInfo str);
     430             : static void get_rule_expr(Node *node, deparse_context *context,
     431             :                           bool showimplicit);
     432             : static void get_rule_expr_toplevel(Node *node, deparse_context *context,
     433             :                                    bool showimplicit);
     434             : static void get_rule_expr_funccall(Node *node, deparse_context *context,
     435             :                                    bool showimplicit);
     436             : static bool looks_like_function(Node *node);
     437             : static void get_oper_expr(OpExpr *expr, deparse_context *context);
     438             : static void get_func_expr(FuncExpr *expr, deparse_context *context,
     439             :                           bool showimplicit);
     440             : static void get_agg_expr(Aggref *aggref, deparse_context *context,
     441             :                          Aggref *original_aggref);
     442             : static void get_agg_combine_expr(Node *node, deparse_context *context,
     443             :                                  void *callback_arg);
     444             : static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context);
     445             : static bool get_func_sql_syntax(FuncExpr *expr, deparse_context *context);
     446             : static void get_coercion_expr(Node *arg, deparse_context *context,
     447             :                               Oid resulttype, int32 resulttypmod,
     448             :                               Node *parentNode);
     449             : static void get_const_expr(Const *constval, deparse_context *context,
     450             :                            int showtype);
     451             : static void get_const_collation(Const *constval, deparse_context *context);
     452             : static void simple_quote_literal(StringInfo buf, const char *val);
     453             : static void get_sublink_expr(SubLink *sublink, deparse_context *context);
     454             : static void get_tablefunc(TableFunc *tf, deparse_context *context,
     455             :                           bool showimplicit);
     456             : static void get_from_clause(Query *query, const char *prefix,
     457             :                             deparse_context *context);
     458             : static void get_from_clause_item(Node *jtnode, Query *query,
     459             :                                  deparse_context *context);
     460             : static void get_column_alias_list(deparse_columns *colinfo,
     461             :                                   deparse_context *context);
     462             : static void get_from_clause_coldeflist(RangeTblFunction *rtfunc,
     463             :                                        deparse_columns *colinfo,
     464             :                                        deparse_context *context);
     465             : static void get_tablesample_def(TableSampleClause *tablesample,
     466             :                                 deparse_context *context);
     467             : static void get_opclass_name(Oid opclass, Oid actual_datatype,
     468             :                              StringInfo buf);
     469             : static Node *processIndirection(Node *node, deparse_context *context);
     470             : static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context);
     471             : static char *get_relation_name(Oid relid);
     472             : static char *generate_relation_name(Oid relid, List *namespaces);
     473             : static char *generate_qualified_relation_name(Oid relid);
     474             : static char *generate_function_name(Oid funcid, int nargs,
     475             :                                     List *argnames, Oid *argtypes,
     476             :                                     bool has_variadic, bool *use_variadic_p,
     477             :                                     ParseExprKind special_exprkind);
     478             : static char *generate_operator_name(Oid operid, Oid arg1, Oid arg2);
     479             : static void add_cast_to(StringInfo buf, Oid typid);
     480             : static char *generate_qualified_type_name(Oid typid);
     481             : static text *string_to_text(char *str);
     482             : static char *flatten_reloptions(Oid relid);
     483             : static void get_reloptions(StringInfo buf, Datum reloptions);
     484             : 
     485             : #define only_marker(rte)  ((rte)->inh ? "" : "ONLY ")
     486             : 
     487             : 
     488             : /* ----------
     489             :  * pg_get_ruledef       - Do it all and return a text
     490             :  *                that could be used as a statement
     491             :  *                to recreate the rule
     492             :  * ----------
     493             :  */
     494             : Datum
     495         344 : pg_get_ruledef(PG_FUNCTION_ARGS)
     496             : {
     497         344 :     Oid         ruleoid = PG_GETARG_OID(0);
     498             :     int         prettyFlags;
     499             :     char       *res;
     500             : 
     501         344 :     prettyFlags = PRETTYFLAG_INDENT;
     502             : 
     503         344 :     res = pg_get_ruledef_worker(ruleoid, prettyFlags);
     504             : 
     505         344 :     if (res == NULL)
     506           4 :         PG_RETURN_NULL();
     507             : 
     508         340 :     PG_RETURN_TEXT_P(string_to_text(res));
     509             : }
     510             : 
     511             : 
     512             : Datum
     513          64 : pg_get_ruledef_ext(PG_FUNCTION_ARGS)
     514             : {
     515          64 :     Oid         ruleoid = PG_GETARG_OID(0);
     516          64 :     bool        pretty = PG_GETARG_BOOL(1);
     517             :     int         prettyFlags;
     518             :     char       *res;
     519             : 
     520          64 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
     521             : 
     522          64 :     res = pg_get_ruledef_worker(ruleoid, prettyFlags);
     523             : 
     524          64 :     if (res == NULL)
     525           0 :         PG_RETURN_NULL();
     526             : 
     527          64 :     PG_RETURN_TEXT_P(string_to_text(res));
     528             : }
     529             : 
     530             : 
     531             : static char *
     532         408 : pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
     533             : {
     534             :     Datum       args[1];
     535             :     char        nulls[1];
     536             :     int         spirc;
     537             :     HeapTuple   ruletup;
     538             :     TupleDesc   rulettc;
     539             :     StringInfoData buf;
     540             : 
     541             :     /*
     542             :      * Do this first so that string is alloc'd in outer context not SPI's.
     543             :      */
     544         408 :     initStringInfo(&buf);
     545             : 
     546             :     /*
     547             :      * Connect to SPI manager
     548             :      */
     549         408 :     if (SPI_connect() != SPI_OK_CONNECT)
     550           0 :         elog(ERROR, "SPI_connect failed");
     551             : 
     552             :     /*
     553             :      * On the first call prepare the plan to lookup pg_rewrite. We read
     554             :      * pg_rewrite over the SPI manager instead of using the syscache to be
     555             :      * checked for read access on pg_rewrite.
     556             :      */
     557         408 :     if (plan_getrulebyoid == NULL)
     558             :     {
     559             :         Oid         argtypes[1];
     560             :         SPIPlanPtr  plan;
     561             : 
     562          22 :         argtypes[0] = OIDOID;
     563          22 :         plan = SPI_prepare(query_getrulebyoid, 1, argtypes);
     564          22 :         if (plan == NULL)
     565           0 :             elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid);
     566          22 :         SPI_keepplan(plan);
     567          22 :         plan_getrulebyoid = plan;
     568             :     }
     569             : 
     570             :     /*
     571             :      * Get the pg_rewrite tuple for this rule
     572             :      */
     573         408 :     args[0] = ObjectIdGetDatum(ruleoid);
     574         408 :     nulls[0] = ' ';
     575         408 :     spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 0);
     576         408 :     if (spirc != SPI_OK_SELECT)
     577           0 :         elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
     578         408 :     if (SPI_processed != 1)
     579             :     {
     580             :         /*
     581             :          * There is no tuple data available here, just keep the output buffer
     582             :          * empty.
     583             :          */
     584             :     }
     585             :     else
     586             :     {
     587             :         /*
     588             :          * Get the rule's definition and put it into executor's memory
     589             :          */
     590         404 :         ruletup = SPI_tuptable->vals[0];
     591         404 :         rulettc = SPI_tuptable->tupdesc;
     592         404 :         make_ruledef(&buf, ruletup, rulettc, prettyFlags);
     593             :     }
     594             : 
     595             :     /*
     596             :      * Disconnect from SPI manager
     597             :      */
     598         408 :     if (SPI_finish() != SPI_OK_FINISH)
     599           0 :         elog(ERROR, "SPI_finish failed");
     600             : 
     601         408 :     if (buf.len == 0)
     602           4 :         return NULL;
     603             : 
     604         404 :     return buf.data;
     605             : }
     606             : 
     607             : 
     608             : /* ----------
     609             :  * pg_get_viewdef       - Mainly the same thing, but we
     610             :  *                only return the SELECT part of a view
     611             :  * ----------
     612             :  */
     613             : Datum
     614        1180 : pg_get_viewdef(PG_FUNCTION_ARGS)
     615             : {
     616             :     /* By OID */
     617        1180 :     Oid         viewoid = PG_GETARG_OID(0);
     618             :     int         prettyFlags;
     619             :     char       *res;
     620             : 
     621        1180 :     prettyFlags = PRETTYFLAG_INDENT;
     622             : 
     623        1180 :     res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
     624             : 
     625        1180 :     if (res == NULL)
     626           4 :         PG_RETURN_NULL();
     627             : 
     628        1176 :     PG_RETURN_TEXT_P(string_to_text(res));
     629             : }
     630             : 
     631             : 
     632             : Datum
     633         256 : pg_get_viewdef_ext(PG_FUNCTION_ARGS)
     634             : {
     635             :     /* By OID */
     636         256 :     Oid         viewoid = PG_GETARG_OID(0);
     637         256 :     bool        pretty = PG_GETARG_BOOL(1);
     638             :     int         prettyFlags;
     639             :     char       *res;
     640             : 
     641         256 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
     642             : 
     643         256 :     res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
     644             : 
     645         256 :     if (res == NULL)
     646           0 :         PG_RETURN_NULL();
     647             : 
     648         256 :     PG_RETURN_TEXT_P(string_to_text(res));
     649             : }
     650             : 
     651             : Datum
     652           4 : pg_get_viewdef_wrap(PG_FUNCTION_ARGS)
     653             : {
     654             :     /* By OID */
     655           4 :     Oid         viewoid = PG_GETARG_OID(0);
     656           4 :     int         wrap = PG_GETARG_INT32(1);
     657             :     int         prettyFlags;
     658             :     char       *res;
     659             : 
     660             :     /* calling this implies we want pretty printing */
     661           4 :     prettyFlags = PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA;
     662             : 
     663           4 :     res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
     664             : 
     665           4 :     if (res == NULL)
     666           0 :         PG_RETURN_NULL();
     667             : 
     668           4 :     PG_RETURN_TEXT_P(string_to_text(res));
     669             : }
     670             : 
     671             : Datum
     672          36 : pg_get_viewdef_name(PG_FUNCTION_ARGS)
     673             : {
     674             :     /* By qualified name */
     675          36 :     text       *viewname = PG_GETARG_TEXT_PP(0);
     676             :     int         prettyFlags;
     677             :     RangeVar   *viewrel;
     678             :     Oid         viewoid;
     679             :     char       *res;
     680             : 
     681          36 :     prettyFlags = PRETTYFLAG_INDENT;
     682             : 
     683             :     /* Look up view name.  Can't lock it - we might not have privileges. */
     684          36 :     viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
     685          36 :     viewoid = RangeVarGetRelid(viewrel, NoLock, false);
     686             : 
     687          36 :     res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
     688             : 
     689          36 :     if (res == NULL)
     690           0 :         PG_RETURN_NULL();
     691             : 
     692          36 :     PG_RETURN_TEXT_P(string_to_text(res));
     693             : }
     694             : 
     695             : 
     696             : Datum
     697         232 : pg_get_viewdef_name_ext(PG_FUNCTION_ARGS)
     698             : {
     699             :     /* By qualified name */
     700         232 :     text       *viewname = PG_GETARG_TEXT_PP(0);
     701         232 :     bool        pretty = PG_GETARG_BOOL(1);
     702             :     int         prettyFlags;
     703             :     RangeVar   *viewrel;
     704             :     Oid         viewoid;
     705             :     char       *res;
     706             : 
     707         232 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
     708             : 
     709             :     /* Look up view name.  Can't lock it - we might not have privileges. */
     710         232 :     viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
     711         232 :     viewoid = RangeVarGetRelid(viewrel, NoLock, false);
     712             : 
     713         232 :     res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
     714             : 
     715         232 :     if (res == NULL)
     716           0 :         PG_RETURN_NULL();
     717             : 
     718         232 :     PG_RETURN_TEXT_P(string_to_text(res));
     719             : }
     720             : 
     721             : /*
     722             :  * Common code for by-OID and by-name variants of pg_get_viewdef
     723             :  */
     724             : static char *
     725        1708 : pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
     726             : {
     727             :     Datum       args[2];
     728             :     char        nulls[2];
     729             :     int         spirc;
     730             :     HeapTuple   ruletup;
     731             :     TupleDesc   rulettc;
     732             :     StringInfoData buf;
     733             : 
     734             :     /*
     735             :      * Do this first so that string is alloc'd in outer context not SPI's.
     736             :      */
     737        1708 :     initStringInfo(&buf);
     738             : 
     739             :     /*
     740             :      * Connect to SPI manager
     741             :      */
     742        1708 :     if (SPI_connect() != SPI_OK_CONNECT)
     743           0 :         elog(ERROR, "SPI_connect failed");
     744             : 
     745             :     /*
     746             :      * On the first call prepare the plan to lookup pg_rewrite. We read
     747             :      * pg_rewrite over the SPI manager instead of using the syscache to be
     748             :      * checked for read access on pg_rewrite.
     749             :      */
     750        1708 :     if (plan_getviewrule == NULL)
     751             :     {
     752             :         Oid         argtypes[2];
     753             :         SPIPlanPtr  plan;
     754             : 
     755         116 :         argtypes[0] = OIDOID;
     756         116 :         argtypes[1] = NAMEOID;
     757         116 :         plan = SPI_prepare(query_getviewrule, 2, argtypes);
     758         116 :         if (plan == NULL)
     759           0 :             elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule);
     760         116 :         SPI_keepplan(plan);
     761         116 :         plan_getviewrule = plan;
     762             :     }
     763             : 
     764             :     /*
     765             :      * Get the pg_rewrite tuple for the view's SELECT rule
     766             :      */
     767        1708 :     args[0] = ObjectIdGetDatum(viewoid);
     768        1708 :     args[1] = DirectFunctionCall1(namein, CStringGetDatum(ViewSelectRuleName));
     769        1708 :     nulls[0] = ' ';
     770        1708 :     nulls[1] = ' ';
     771        1708 :     spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 0);
     772        1708 :     if (spirc != SPI_OK_SELECT)
     773           0 :         elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
     774        1708 :     if (SPI_processed != 1)
     775             :     {
     776             :         /*
     777             :          * There is no tuple data available here, just keep the output buffer
     778             :          * empty.
     779             :          */
     780             :     }
     781             :     else
     782             :     {
     783             :         /*
     784             :          * Get the rule's definition and put it into executor's memory
     785             :          */
     786        1704 :         ruletup = SPI_tuptable->vals[0];
     787        1704 :         rulettc = SPI_tuptable->tupdesc;
     788        1704 :         make_viewdef(&buf, ruletup, rulettc, prettyFlags, wrapColumn);
     789             :     }
     790             : 
     791             :     /*
     792             :      * Disconnect from SPI manager
     793             :      */
     794        1708 :     if (SPI_finish() != SPI_OK_FINISH)
     795           0 :         elog(ERROR, "SPI_finish failed");
     796             : 
     797        1708 :     if (buf.len == 0)
     798           4 :         return NULL;
     799             : 
     800        1704 :     return buf.data;
     801             : }
     802             : 
     803             : /* ----------
     804             :  * pg_get_triggerdef        - Get the definition of a trigger
     805             :  * ----------
     806             :  */
     807             : Datum
     808         136 : pg_get_triggerdef(PG_FUNCTION_ARGS)
     809             : {
     810         136 :     Oid         trigid = PG_GETARG_OID(0);
     811             :     char       *res;
     812             : 
     813         136 :     res = pg_get_triggerdef_worker(trigid, false);
     814             : 
     815         136 :     if (res == NULL)
     816           4 :         PG_RETURN_NULL();
     817             : 
     818         132 :     PG_RETURN_TEXT_P(string_to_text(res));
     819             : }
     820             : 
     821             : Datum
     822         536 : pg_get_triggerdef_ext(PG_FUNCTION_ARGS)
     823             : {
     824         536 :     Oid         trigid = PG_GETARG_OID(0);
     825         536 :     bool        pretty = PG_GETARG_BOOL(1);
     826             :     char       *res;
     827             : 
     828         536 :     res = pg_get_triggerdef_worker(trigid, pretty);
     829             : 
     830         536 :     if (res == NULL)
     831           0 :         PG_RETURN_NULL();
     832             : 
     833         536 :     PG_RETURN_TEXT_P(string_to_text(res));
     834             : }
     835             : 
     836             : static char *
     837         672 : pg_get_triggerdef_worker(Oid trigid, bool pretty)
     838             : {
     839             :     HeapTuple   ht_trig;
     840             :     Form_pg_trigger trigrec;
     841             :     StringInfoData buf;
     842             :     Relation    tgrel;
     843             :     ScanKeyData skey[1];
     844             :     SysScanDesc tgscan;
     845         672 :     int         findx = 0;
     846             :     char       *tgname;
     847             :     char       *tgoldtable;
     848             :     char       *tgnewtable;
     849             :     Datum       value;
     850             :     bool        isnull;
     851             : 
     852             :     /*
     853             :      * Fetch the pg_trigger tuple by the Oid of the trigger
     854             :      */
     855         672 :     tgrel = table_open(TriggerRelationId, AccessShareLock);
     856             : 
     857         672 :     ScanKeyInit(&skey[0],
     858             :                 Anum_pg_trigger_oid,
     859             :                 BTEqualStrategyNumber, F_OIDEQ,
     860             :                 ObjectIdGetDatum(trigid));
     861             : 
     862         672 :     tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
     863             :                                 NULL, 1, skey);
     864             : 
     865         672 :     ht_trig = systable_getnext(tgscan);
     866             : 
     867         672 :     if (!HeapTupleIsValid(ht_trig))
     868             :     {
     869           4 :         systable_endscan(tgscan);
     870           4 :         table_close(tgrel, AccessShareLock);
     871           4 :         return NULL;
     872             :     }
     873             : 
     874         668 :     trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
     875             : 
     876             :     /*
     877             :      * Start the trigger definition. Note that the trigger's name should never
     878             :      * be schema-qualified, but the trigger rel's name may be.
     879             :      */
     880         668 :     initStringInfo(&buf);
     881             : 
     882         668 :     tgname = NameStr(trigrec->tgname);
     883        1336 :     appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
     884         668 :                      OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
     885             :                      quote_identifier(tgname));
     886             : 
     887         668 :     if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
     888         322 :         appendStringInfoString(&buf, "BEFORE");
     889         346 :     else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
     890         330 :         appendStringInfoString(&buf, "AFTER");
     891          16 :     else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
     892          16 :         appendStringInfoString(&buf, "INSTEAD OF");
     893             :     else
     894           0 :         elog(ERROR, "unexpected tgtype value: %d", trigrec->tgtype);
     895             : 
     896         668 :     if (TRIGGER_FOR_INSERT(trigrec->tgtype))
     897             :     {
     898         380 :         appendStringInfoString(&buf, " INSERT");
     899         380 :         findx++;
     900             :     }
     901         668 :     if (TRIGGER_FOR_DELETE(trigrec->tgtype))
     902             :     {
     903         138 :         if (findx > 0)
     904          54 :             appendStringInfoString(&buf, " OR DELETE");
     905             :         else
     906          84 :             appendStringInfoString(&buf, " DELETE");
     907         138 :         findx++;
     908             :     }
     909         668 :     if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
     910             :     {
     911         388 :         if (findx > 0)
     912         184 :             appendStringInfoString(&buf, " OR UPDATE");
     913             :         else
     914         204 :             appendStringInfoString(&buf, " UPDATE");
     915         388 :         findx++;
     916             :         /* tgattr is first var-width field, so OK to access directly */
     917         388 :         if (trigrec->tgattr.dim1 > 0)
     918             :         {
     919             :             int         i;
     920             : 
     921          48 :             appendStringInfoString(&buf, " OF ");
     922         106 :             for (i = 0; i < trigrec->tgattr.dim1; i++)
     923             :             {
     924             :                 char       *attname;
     925             : 
     926          58 :                 if (i > 0)
     927          10 :                     appendStringInfoString(&buf, ", ");
     928          58 :                 attname = get_attname(trigrec->tgrelid,
     929          58 :                                       trigrec->tgattr.values[i], false);
     930          58 :                 appendStringInfoString(&buf, quote_identifier(attname));
     931             :             }
     932             :         }
     933             :     }
     934         668 :     if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
     935             :     {
     936           0 :         if (findx > 0)
     937           0 :             appendStringInfoString(&buf, " OR TRUNCATE");
     938             :         else
     939           0 :             appendStringInfoString(&buf, " TRUNCATE");
     940           0 :         findx++;
     941             :     }
     942             : 
     943             :     /*
     944             :      * In non-pretty mode, always schema-qualify the target table name for
     945             :      * safety.  In pretty mode, schema-qualify only if not visible.
     946             :      */
     947        1336 :     appendStringInfo(&buf, " ON %s ",
     948             :                      pretty ?
     949          80 :                      generate_relation_name(trigrec->tgrelid, NIL) :
     950         588 :                      generate_qualified_relation_name(trigrec->tgrelid));
     951             : 
     952         668 :     if (OidIsValid(trigrec->tgconstraint))
     953             :     {
     954           0 :         if (OidIsValid(trigrec->tgconstrrelid))
     955           0 :             appendStringInfo(&buf, "FROM %s ",
     956             :                              generate_relation_name(trigrec->tgconstrrelid, NIL));
     957           0 :         if (!trigrec->tgdeferrable)
     958           0 :             appendStringInfoString(&buf, "NOT ");
     959           0 :         appendStringInfoString(&buf, "DEFERRABLE INITIALLY ");
     960           0 :         if (trigrec->tginitdeferred)
     961           0 :             appendStringInfoString(&buf, "DEFERRED ");
     962             :         else
     963           0 :             appendStringInfoString(&buf, "IMMEDIATE ");
     964             :     }
     965             : 
     966         668 :     value = fastgetattr(ht_trig, Anum_pg_trigger_tgoldtable,
     967             :                         tgrel->rd_att, &isnull);
     968         668 :     if (!isnull)
     969          62 :         tgoldtable = NameStr(*DatumGetName(value));
     970             :     else
     971         606 :         tgoldtable = NULL;
     972         668 :     value = fastgetattr(ht_trig, Anum_pg_trigger_tgnewtable,
     973             :                         tgrel->rd_att, &isnull);
     974         668 :     if (!isnull)
     975          68 :         tgnewtable = NameStr(*DatumGetName(value));
     976             :     else
     977         600 :         tgnewtable = NULL;
     978         668 :     if (tgoldtable != NULL || tgnewtable != NULL)
     979             :     {
     980          96 :         appendStringInfoString(&buf, "REFERENCING ");
     981          96 :         if (tgoldtable != NULL)
     982          62 :             appendStringInfo(&buf, "OLD TABLE AS %s ",
     983             :                              quote_identifier(tgoldtable));
     984          96 :         if (tgnewtable != NULL)
     985          68 :             appendStringInfo(&buf, "NEW TABLE AS %s ",
     986             :                              quote_identifier(tgnewtable));
     987             :     }
     988             : 
     989         668 :     if (TRIGGER_FOR_ROW(trigrec->tgtype))
     990         474 :         appendStringInfoString(&buf, "FOR EACH ROW ");
     991             :     else
     992         194 :         appendStringInfoString(&buf, "FOR EACH STATEMENT ");
     993             : 
     994             :     /* If the trigger has a WHEN qualification, add that */
     995         668 :     value = fastgetattr(ht_trig, Anum_pg_trigger_tgqual,
     996             :                         tgrel->rd_att, &isnull);
     997         668 :     if (!isnull)
     998             :     {
     999             :         Node       *qual;
    1000             :         char        relkind;
    1001             :         deparse_context context;
    1002             :         deparse_namespace dpns;
    1003             :         RangeTblEntry *oldrte;
    1004             :         RangeTblEntry *newrte;
    1005             : 
    1006          92 :         appendStringInfoString(&buf, "WHEN (");
    1007             : 
    1008          92 :         qual = stringToNode(TextDatumGetCString(value));
    1009             : 
    1010          92 :         relkind = get_rel_relkind(trigrec->tgrelid);
    1011             : 
    1012             :         /* Build minimal OLD and NEW RTEs for the rel */
    1013          92 :         oldrte = makeNode(RangeTblEntry);
    1014          92 :         oldrte->rtekind = RTE_RELATION;
    1015          92 :         oldrte->relid = trigrec->tgrelid;
    1016          92 :         oldrte->relkind = relkind;
    1017          92 :         oldrte->rellockmode = AccessShareLock;
    1018          92 :         oldrte->alias = makeAlias("old", NIL);
    1019          92 :         oldrte->eref = oldrte->alias;
    1020          92 :         oldrte->lateral = false;
    1021          92 :         oldrte->inh = false;
    1022          92 :         oldrte->inFromCl = true;
    1023             : 
    1024          92 :         newrte = makeNode(RangeTblEntry);
    1025          92 :         newrte->rtekind = RTE_RELATION;
    1026          92 :         newrte->relid = trigrec->tgrelid;
    1027          92 :         newrte->relkind = relkind;
    1028          92 :         newrte->rellockmode = AccessShareLock;
    1029          92 :         newrte->alias = makeAlias("new", NIL);
    1030          92 :         newrte->eref = newrte->alias;
    1031          92 :         newrte->lateral = false;
    1032          92 :         newrte->inh = false;
    1033          92 :         newrte->inFromCl = true;
    1034             : 
    1035             :         /* Build two-element rtable */
    1036          92 :         memset(&dpns, 0, sizeof(dpns));
    1037          92 :         dpns.rtable = list_make2(oldrte, newrte);
    1038          92 :         dpns.subplans = NIL;
    1039          92 :         dpns.ctes = NIL;
    1040          92 :         dpns.appendrels = NULL;
    1041          92 :         set_rtable_names(&dpns, NIL, NULL);
    1042          92 :         set_simple_column_names(&dpns);
    1043             : 
    1044             :         /* Set up context with one-deep namespace stack */
    1045          92 :         context.buf = &buf;
    1046          92 :         context.namespaces = list_make1(&dpns);
    1047          92 :         context.windowClause = NIL;
    1048          92 :         context.windowTList = NIL;
    1049          92 :         context.varprefix = true;
    1050          92 :         context.prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
    1051          92 :         context.wrapColumn = WRAP_COLUMN_DEFAULT;
    1052          92 :         context.indentLevel = PRETTYINDENT_STD;
    1053          92 :         context.special_exprkind = EXPR_KIND_NONE;
    1054          92 :         context.appendparents = NULL;
    1055             : 
    1056          92 :         get_rule_expr(qual, &context, false);
    1057             : 
    1058          92 :         appendStringInfoString(&buf, ") ");
    1059             :     }
    1060             : 
    1061         668 :     appendStringInfo(&buf, "EXECUTE FUNCTION %s(",
    1062             :                      generate_function_name(trigrec->tgfoid, 0,
    1063             :                                             NIL, NULL,
    1064             :                                             false, NULL, EXPR_KIND_NONE));
    1065             : 
    1066         668 :     if (trigrec->tgnargs > 0)
    1067             :     {
    1068             :         char       *p;
    1069             :         int         i;
    1070             : 
    1071         286 :         value = fastgetattr(ht_trig, Anum_pg_trigger_tgargs,
    1072             :                             tgrel->rd_att, &isnull);
    1073         286 :         if (isnull)
    1074           0 :             elog(ERROR, "tgargs is null for trigger %u", trigid);
    1075         286 :         p = (char *) VARDATA_ANY(DatumGetByteaPP(value));
    1076         768 :         for (i = 0; i < trigrec->tgnargs; i++)
    1077             :         {
    1078         482 :             if (i > 0)
    1079         196 :                 appendStringInfoString(&buf, ", ");
    1080         482 :             simple_quote_literal(&buf, p);
    1081             :             /* advance p to next string embedded in tgargs */
    1082        4386 :             while (*p)
    1083        3904 :                 p++;
    1084         482 :             p++;
    1085             :         }
    1086             :     }
    1087             : 
    1088             :     /* We deliberately do not put semi-colon at end */
    1089         668 :     appendStringInfoChar(&buf, ')');
    1090             : 
    1091             :     /* Clean up */
    1092         668 :     systable_endscan(tgscan);
    1093             : 
    1094         668 :     table_close(tgrel, AccessShareLock);
    1095             : 
    1096         668 :     return buf.data;
    1097             : }
    1098             : 
    1099             : /* ----------
    1100             :  * pg_get_indexdef          - Get the definition of an index
    1101             :  *
    1102             :  * In the extended version, there is a colno argument as well as pretty bool.
    1103             :  *  if colno == 0, we want a complete index definition.
    1104             :  *  if colno > 0, we only want the Nth index key's variable or expression.
    1105             :  *
    1106             :  * Note that the SQL-function versions of this omit any info about the
    1107             :  * index tablespace; this is intentional because pg_dump wants it that way.
    1108             :  * However pg_get_indexdef_string() includes the index tablespace.
    1109             :  * ----------
    1110             :  */
    1111             : Datum
    1112        2186 : pg_get_indexdef(PG_FUNCTION_ARGS)
    1113             : {
    1114        2186 :     Oid         indexrelid = PG_GETARG_OID(0);
    1115             :     int         prettyFlags;
    1116             :     char       *res;
    1117             : 
    1118        2186 :     prettyFlags = PRETTYFLAG_INDENT;
    1119             : 
    1120        2186 :     res = pg_get_indexdef_worker(indexrelid, 0, NULL,
    1121             :                                  false, false,
    1122             :                                  false, false,
    1123             :                                  prettyFlags, true);
    1124             : 
    1125        2186 :     if (res == NULL)
    1126           4 :         PG_RETURN_NULL();
    1127             : 
    1128        2182 :     PG_RETURN_TEXT_P(string_to_text(res));
    1129             : }
    1130             : 
    1131             : Datum
    1132        1036 : pg_get_indexdef_ext(PG_FUNCTION_ARGS)
    1133             : {
    1134        1036 :     Oid         indexrelid = PG_GETARG_OID(0);
    1135        1036 :     int32       colno = PG_GETARG_INT32(1);
    1136        1036 :     bool        pretty = PG_GETARG_BOOL(2);
    1137             :     int         prettyFlags;
    1138             :     char       *res;
    1139             : 
    1140        1036 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
    1141             : 
    1142        1036 :     res = pg_get_indexdef_worker(indexrelid, colno, NULL,
    1143             :                                  colno != 0, false,
    1144             :                                  false, false,
    1145             :                                  prettyFlags, true);
    1146             : 
    1147        1036 :     if (res == NULL)
    1148           0 :         PG_RETURN_NULL();
    1149             : 
    1150        1036 :     PG_RETURN_TEXT_P(string_to_text(res));
    1151             : }
    1152             : 
    1153             : /*
    1154             :  * Internal version for use by ALTER TABLE.
    1155             :  * Includes a tablespace clause in the result.
    1156             :  * Returns a palloc'd C string; no pretty-printing.
    1157             :  */
    1158             : char *
    1159         128 : pg_get_indexdef_string(Oid indexrelid)
    1160             : {
    1161         128 :     return pg_get_indexdef_worker(indexrelid, 0, NULL,
    1162             :                                   false, false,
    1163             :                                   true, true,
    1164             :                                   0, false);
    1165             : }
    1166             : 
    1167             : /* Internal version that just reports the key-column definitions */
    1168             : char *
    1169         472 : pg_get_indexdef_columns(Oid indexrelid, bool pretty)
    1170             : {
    1171             :     int         prettyFlags;
    1172             : 
    1173         472 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
    1174             : 
    1175         472 :     return pg_get_indexdef_worker(indexrelid, 0, NULL,
    1176             :                                   true, true,
    1177             :                                   false, false,
    1178             :                                   prettyFlags, false);
    1179             : }
    1180             : 
    1181             : /*
    1182             :  * Internal workhorse to decompile an index definition.
    1183             :  *
    1184             :  * This is now used for exclusion constraints as well: if excludeOps is not
    1185             :  * NULL then it points to an array of exclusion operator OIDs.
    1186             :  */
    1187             : static char *
    1188        3890 : pg_get_indexdef_worker(Oid indexrelid, int colno,
    1189             :                        const Oid *excludeOps,
    1190             :                        bool attrsOnly, bool keysOnly,
    1191             :                        bool showTblSpc, bool inherits,
    1192             :                        int prettyFlags, bool missing_ok)
    1193             : {
    1194             :     /* might want a separate isConstraint parameter later */
    1195        3890 :     bool        isConstraint = (excludeOps != NULL);
    1196             :     HeapTuple   ht_idx;
    1197             :     HeapTuple   ht_idxrel;
    1198             :     HeapTuple   ht_am;
    1199             :     Form_pg_index idxrec;
    1200             :     Form_pg_class idxrelrec;
    1201             :     Form_pg_am  amrec;
    1202             :     IndexAmRoutine *amroutine;
    1203             :     List       *indexprs;
    1204             :     ListCell   *indexpr_item;
    1205             :     List       *context;
    1206             :     Oid         indrelid;
    1207             :     int         keyno;
    1208             :     Datum       indcollDatum;
    1209             :     Datum       indclassDatum;
    1210             :     Datum       indoptionDatum;
    1211             :     bool        isnull;
    1212             :     oidvector  *indcollation;
    1213             :     oidvector  *indclass;
    1214             :     int2vector *indoption;
    1215             :     StringInfoData buf;
    1216             :     char       *str;
    1217             :     char       *sep;
    1218             : 
    1219             :     /*
    1220             :      * Fetch the pg_index tuple by the Oid of the index
    1221             :      */
    1222        3890 :     ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
    1223        3890 :     if (!HeapTupleIsValid(ht_idx))
    1224             :     {
    1225           4 :         if (missing_ok)
    1226           4 :             return NULL;
    1227           0 :         elog(ERROR, "cache lookup failed for index %u", indexrelid);
    1228             :     }
    1229        3886 :     idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
    1230             : 
    1231        3886 :     indrelid = idxrec->indrelid;
    1232             :     Assert(indexrelid == idxrec->indexrelid);
    1233             : 
    1234             :     /* Must get indcollation, indclass, and indoption the hard way */
    1235        3886 :     indcollDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
    1236             :                                    Anum_pg_index_indcollation, &isnull);
    1237             :     Assert(!isnull);
    1238        3886 :     indcollation = (oidvector *) DatumGetPointer(indcollDatum);
    1239             : 
    1240        3886 :     indclassDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
    1241             :                                     Anum_pg_index_indclass, &isnull);
    1242             :     Assert(!isnull);
    1243        3886 :     indclass = (oidvector *) DatumGetPointer(indclassDatum);
    1244             : 
    1245        3886 :     indoptionDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
    1246             :                                      Anum_pg_index_indoption, &isnull);
    1247             :     Assert(!isnull);
    1248        3886 :     indoption = (int2vector *) DatumGetPointer(indoptionDatum);
    1249             : 
    1250             :     /*
    1251             :      * Fetch the pg_class tuple of the index relation
    1252             :      */
    1253        3886 :     ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexrelid));
    1254        3886 :     if (!HeapTupleIsValid(ht_idxrel))
    1255           0 :         elog(ERROR, "cache lookup failed for relation %u", indexrelid);
    1256        3886 :     idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
    1257             : 
    1258             :     /*
    1259             :      * Fetch the pg_am tuple of the index' access method
    1260             :      */
    1261        3886 :     ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
    1262        3886 :     if (!HeapTupleIsValid(ht_am))
    1263           0 :         elog(ERROR, "cache lookup failed for access method %u",
    1264             :              idxrelrec->relam);
    1265        3886 :     amrec = (Form_pg_am) GETSTRUCT(ht_am);
    1266             : 
    1267             :     /* Fetch the index AM's API struct */
    1268        3886 :     amroutine = GetIndexAmRoutine(amrec->amhandler);
    1269             : 
    1270             :     /*
    1271             :      * Get the index expressions, if any.  (NOTE: we do not use the relcache
    1272             :      * versions of the expressions and predicate, because we want to display
    1273             :      * non-const-folded expressions.)
    1274             :      */
    1275        3886 :     if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs, NULL))
    1276             :     {
    1277             :         Datum       exprsDatum;
    1278             :         bool        isnull;
    1279             :         char       *exprsString;
    1280             : 
    1281         326 :         exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
    1282             :                                      Anum_pg_index_indexprs, &isnull);
    1283             :         Assert(!isnull);
    1284         326 :         exprsString = TextDatumGetCString(exprsDatum);
    1285         326 :         indexprs = (List *) stringToNode(exprsString);
    1286         326 :         pfree(exprsString);
    1287             :     }
    1288             :     else
    1289        3560 :         indexprs = NIL;
    1290             : 
    1291        3886 :     indexpr_item = list_head(indexprs);
    1292             : 
    1293        3886 :     context = deparse_context_for(get_relation_name(indrelid), indrelid);
    1294             : 
    1295             :     /*
    1296             :      * Start the index definition.  Note that the index's name should never be
    1297             :      * schema-qualified, but the indexed rel's name may be.
    1298             :      */
    1299        3886 :     initStringInfo(&buf);
    1300             : 
    1301        3886 :     if (!attrsOnly)
    1302             :     {
    1303        3130 :         if (!isConstraint)
    1304        6124 :             appendStringInfo(&buf, "CREATE %sINDEX %s ON %s%s USING %s (",
    1305        3062 :                              idxrec->indisunique ? "UNIQUE " : "",
    1306        3062 :                              quote_identifier(NameStr(idxrelrec->relname)),
    1307        3062 :                              idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
    1308         364 :                              && !inherits ? "ONLY " : "",
    1309        3062 :                              (prettyFlags & PRETTYFLAG_SCHEMA) ?
    1310         752 :                              generate_relation_name(indrelid, NIL) :
    1311        2310 :                              generate_qualified_relation_name(indrelid),
    1312        3062 :                              quote_identifier(NameStr(amrec->amname)));
    1313             :         else                    /* currently, must be EXCLUDE constraint */
    1314          68 :             appendStringInfo(&buf, "EXCLUDE USING %s (",
    1315          68 :                              quote_identifier(NameStr(amrec->amname)));
    1316             :     }
    1317             : 
    1318             :     /*
    1319             :      * Report the indexed attributes
    1320             :      */
    1321        3886 :     sep = "";
    1322        9416 :     for (keyno = 0; keyno < idxrec->indnatts; keyno++)
    1323             :     {
    1324        5594 :         AttrNumber  attnum = idxrec->indkey.values[keyno];
    1325             :         Oid         keycoltype;
    1326             :         Oid         keycolcollation;
    1327             : 
    1328             :         /*
    1329             :          * Ignore non-key attributes if told to.
    1330             :          */
    1331        5594 :         if (keysOnly && keyno >= idxrec->indnkeyatts)
    1332          64 :             break;
    1333             : 
    1334             :         /* Otherwise, print INCLUDE to divide key and non-key attrs. */
    1335        5530 :         if (!colno && keyno == idxrec->indnkeyatts)
    1336             :         {
    1337         158 :             appendStringInfoString(&buf, ") INCLUDE (");
    1338         158 :             sep = "";
    1339             :         }
    1340             : 
    1341        5530 :         if (!colno)
    1342        5142 :             appendStringInfoString(&buf, sep);
    1343        5530 :         sep = ", ";
    1344             : 
    1345        5530 :         if (attnum != 0)
    1346             :         {
    1347             :             /* Simple index column */
    1348             :             char       *attname;
    1349             :             int32       keycoltypmod;
    1350             : 
    1351        5108 :             attname = get_attname(indrelid, attnum, false);
    1352        5108 :             if (!colno || colno == keyno + 1)
    1353        5012 :                 appendStringInfoString(&buf, quote_identifier(attname));
    1354        5108 :             get_atttypetypmodcoll(indrelid, attnum,
    1355             :                                   &keycoltype, &keycoltypmod,
    1356             :                                   &keycolcollation);
    1357             :         }
    1358             :         else
    1359             :         {
    1360             :             /* expressional index */
    1361             :             Node       *indexkey;
    1362             : 
    1363         422 :             if (indexpr_item == NULL)
    1364           0 :                 elog(ERROR, "too few entries in indexprs list");
    1365         422 :             indexkey = (Node *) lfirst(indexpr_item);
    1366         422 :             indexpr_item = lnext(indexprs, indexpr_item);
    1367             :             /* Deparse */
    1368         422 :             str = deparse_expression_pretty(indexkey, context, false, false,
    1369             :                                             prettyFlags, 0);
    1370         422 :             if (!colno || colno == keyno + 1)
    1371             :             {
    1372             :                 /* Need parens if it's not a bare function call */
    1373         414 :                 if (looks_like_function(indexkey))
    1374          34 :                     appendStringInfoString(&buf, str);
    1375             :                 else
    1376         380 :                     appendStringInfo(&buf, "(%s)", str);
    1377             :             }
    1378         422 :             keycoltype = exprType(indexkey);
    1379         422 :             keycolcollation = exprCollation(indexkey);
    1380             :         }
    1381             : 
    1382             :         /* Print additional decoration for (selected) key columns */
    1383        5530 :         if (!attrsOnly && keyno < idxrec->indnkeyatts &&
    1384           0 :             (!colno || colno == keyno + 1))
    1385             :         {
    1386        4272 :             int16       opt = indoption->values[keyno];
    1387        4272 :             Oid         indcoll = indcollation->values[keyno];
    1388        4272 :             Datum       attoptions = get_attoptions(indexrelid, keyno + 1);
    1389        4272 :             bool        has_options = attoptions != (Datum) 0;
    1390             : 
    1391             :             /* Add collation, if not default for column */
    1392        4272 :             if (OidIsValid(indcoll) && indcoll != keycolcollation)
    1393          48 :                 appendStringInfo(&buf, " COLLATE %s",
    1394             :                                  generate_collation_name((indcoll)));
    1395             : 
    1396             :             /* Add the operator class name, if not default */
    1397        4272 :             get_opclass_name(indclass->values[keyno],
    1398             :                              has_options ? InvalidOid : keycoltype, &buf);
    1399             : 
    1400        4272 :             if (has_options)
    1401             :             {
    1402           8 :                 appendStringInfoString(&buf, " (");
    1403           8 :                 get_reloptions(&buf, attoptions);
    1404           8 :                 appendStringInfoChar(&buf, ')');
    1405             :             }
    1406             : 
    1407             :             /* Add options if relevant */
    1408        4272 :             if (amroutine->amcanorder)
    1409             :             {
    1410             :                 /* if it supports sort ordering, report DESC and NULLS opts */
    1411        3800 :                 if (opt & INDOPTION_DESC)
    1412             :                 {
    1413           0 :                     appendStringInfoString(&buf, " DESC");
    1414             :                     /* NULLS FIRST is the default in this case */
    1415           0 :                     if (!(opt & INDOPTION_NULLS_FIRST))
    1416           0 :                         appendStringInfoString(&buf, " NULLS LAST");
    1417             :                 }
    1418             :                 else
    1419             :                 {
    1420        3800 :                     if (opt & INDOPTION_NULLS_FIRST)
    1421           0 :                         appendStringInfoString(&buf, " NULLS FIRST");
    1422             :                 }
    1423             :             }
    1424             : 
    1425             :             /* Add the exclusion operator if relevant */
    1426        4272 :             if (excludeOps != NULL)
    1427          80 :                 appendStringInfo(&buf, " WITH %s",
    1428          80 :                                  generate_operator_name(excludeOps[keyno],
    1429             :                                                         keycoltype,
    1430             :                                                         keycoltype));
    1431             :         }
    1432             :     }
    1433             : 
    1434        3886 :     if (!attrsOnly)
    1435             :     {
    1436        3130 :         appendStringInfoChar(&buf, ')');
    1437             : 
    1438             :         /*
    1439             :          * If it has options, append "WITH (options)"
    1440             :          */
    1441        3130 :         str = flatten_reloptions(indexrelid);
    1442        3130 :         if (str)
    1443             :         {
    1444          90 :             appendStringInfo(&buf, " WITH (%s)", str);
    1445          90 :             pfree(str);
    1446             :         }
    1447             : 
    1448             :         /*
    1449             :          * Print tablespace, but only if requested
    1450             :          */
    1451        3130 :         if (showTblSpc)
    1452             :         {
    1453             :             Oid         tblspc;
    1454             : 
    1455         128 :             tblspc = get_rel_tablespace(indexrelid);
    1456         128 :             if (OidIsValid(tblspc))
    1457             :             {
    1458          36 :                 if (isConstraint)
    1459           0 :                     appendStringInfoString(&buf, " USING INDEX");
    1460          36 :                 appendStringInfo(&buf, " TABLESPACE %s",
    1461          36 :                                  quote_identifier(get_tablespace_name(tblspc)));
    1462             :             }
    1463             :         }
    1464             : 
    1465             :         /*
    1466             :          * If it's a partial index, decompile and append the predicate
    1467             :          */
    1468        3130 :         if (!heap_attisnull(ht_idx, Anum_pg_index_indpred, NULL))
    1469             :         {
    1470             :             Node       *node;
    1471             :             Datum       predDatum;
    1472             :             bool        isnull;
    1473             :             char       *predString;
    1474             : 
    1475             :             /* Convert text string to node tree */
    1476         192 :             predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
    1477             :                                         Anum_pg_index_indpred, &isnull);
    1478             :             Assert(!isnull);
    1479         192 :             predString = TextDatumGetCString(predDatum);
    1480         192 :             node = (Node *) stringToNode(predString);
    1481         192 :             pfree(predString);
    1482             : 
    1483             :             /* Deparse */
    1484         192 :             str = deparse_expression_pretty(node, context, false, false,
    1485             :                                             prettyFlags, 0);
    1486         192 :             if (isConstraint)
    1487          28 :                 appendStringInfo(&buf, " WHERE (%s)", str);
    1488             :             else
    1489         164 :                 appendStringInfo(&buf, " WHERE %s", str);
    1490             :         }
    1491             :     }
    1492             : 
    1493             :     /* Clean up */
    1494        3886 :     ReleaseSysCache(ht_idx);
    1495        3886 :     ReleaseSysCache(ht_idxrel);
    1496        3886 :     ReleaseSysCache(ht_am);
    1497             : 
    1498        3886 :     return buf.data;
    1499             : }
    1500             : 
    1501             : /*
    1502             :  * pg_get_statisticsobjdef
    1503             :  *      Get the definition of an extended statistics object
    1504             :  */
    1505             : Datum
    1506         120 : pg_get_statisticsobjdef(PG_FUNCTION_ARGS)
    1507             : {
    1508         120 :     Oid         statextid = PG_GETARG_OID(0);
    1509             :     char       *res;
    1510             : 
    1511         120 :     res = pg_get_statisticsobj_worker(statextid, true);
    1512             : 
    1513         120 :     if (res == NULL)
    1514           4 :         PG_RETURN_NULL();
    1515             : 
    1516         116 :     PG_RETURN_TEXT_P(string_to_text(res));
    1517             : }
    1518             : 
    1519             : /*
    1520             :  * Internal workhorse to decompile an extended statistics object.
    1521             :  */
    1522             : static char *
    1523         120 : pg_get_statisticsobj_worker(Oid statextid, bool missing_ok)
    1524             : {
    1525             :     Form_pg_statistic_ext statextrec;
    1526             :     HeapTuple   statexttup;
    1527             :     StringInfoData buf;
    1528             :     int         colno;
    1529             :     char       *nsp;
    1530             :     ArrayType  *arr;
    1531             :     char       *enabled;
    1532             :     Datum       datum;
    1533             :     bool        isnull;
    1534             :     bool        ndistinct_enabled;
    1535             :     bool        dependencies_enabled;
    1536             :     bool        mcv_enabled;
    1537             :     int         i;
    1538             : 
    1539         120 :     statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
    1540             : 
    1541         120 :     if (!HeapTupleIsValid(statexttup))
    1542             :     {
    1543           4 :         if (missing_ok)
    1544           4 :             return NULL;
    1545           0 :         elog(ERROR, "cache lookup failed for statistics object %u", statextid);
    1546             :     }
    1547             : 
    1548         116 :     statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
    1549             : 
    1550         116 :     initStringInfo(&buf);
    1551             : 
    1552         116 :     nsp = get_namespace_name(statextrec->stxnamespace);
    1553         116 :     appendStringInfo(&buf, "CREATE STATISTICS %s",
    1554             :                      quote_qualified_identifier(nsp,
    1555         116 :                                                 NameStr(statextrec->stxname)));
    1556             : 
    1557             :     /*
    1558             :      * Decode the stxkind column so that we know which stats types to print.
    1559             :      */
    1560         116 :     datum = SysCacheGetAttr(STATEXTOID, statexttup,
    1561             :                             Anum_pg_statistic_ext_stxkind, &isnull);
    1562             :     Assert(!isnull);
    1563         116 :     arr = DatumGetArrayTypeP(datum);
    1564         116 :     if (ARR_NDIM(arr) != 1 ||
    1565         116 :         ARR_HASNULL(arr) ||
    1566         116 :         ARR_ELEMTYPE(arr) != CHAROID)
    1567           0 :         elog(ERROR, "stxkind is not a 1-D char array");
    1568         116 :     enabled = (char *) ARR_DATA_PTR(arr);
    1569             : 
    1570         116 :     ndistinct_enabled = false;
    1571         116 :     dependencies_enabled = false;
    1572         116 :     mcv_enabled = false;
    1573             : 
    1574         328 :     for (i = 0; i < ARR_DIMS(arr)[0]; i++)
    1575             :     {
    1576         212 :         if (enabled[i] == STATS_EXT_NDISTINCT)
    1577          92 :             ndistinct_enabled = true;
    1578         212 :         if (enabled[i] == STATS_EXT_DEPENDENCIES)
    1579          54 :             dependencies_enabled = true;
    1580         212 :         if (enabled[i] == STATS_EXT_MCV)
    1581          66 :             mcv_enabled = true;
    1582             :     }
    1583             : 
    1584             :     /*
    1585             :      * If any option is disabled, then we'll need to append the types clause
    1586             :      * to show which options are enabled.  We omit the types clause on purpose
    1587             :      * when all options are enabled, so a pg_dump/pg_restore will create all
    1588             :      * statistics types on a newer postgres version, if the statistics had all
    1589             :      * options enabled on the original version.
    1590             :      */
    1591         116 :     if (!ndistinct_enabled || !dependencies_enabled || !mcv_enabled)
    1592             :     {
    1593          68 :         bool        gotone = false;
    1594             : 
    1595          68 :         appendStringInfoString(&buf, " (");
    1596             : 
    1597          68 :         if (ndistinct_enabled)
    1598             :         {
    1599          44 :             appendStringInfoString(&buf, "ndistinct");
    1600          44 :             gotone = true;
    1601             :         }
    1602             : 
    1603          68 :         if (dependencies_enabled)
    1604             :         {
    1605           6 :             appendStringInfo(&buf, "%sdependencies", gotone ? ", " : "");
    1606           6 :             gotone = true;
    1607             :         }
    1608             : 
    1609          68 :         if (mcv_enabled)
    1610          18 :             appendStringInfo(&buf, "%smcv", gotone ? ", " : "");
    1611             : 
    1612          68 :         appendStringInfoChar(&buf, ')');
    1613             :     }
    1614             : 
    1615         116 :     appendStringInfoString(&buf, " ON ");
    1616             : 
    1617         372 :     for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
    1618             :     {
    1619         256 :         AttrNumber  attnum = statextrec->stxkeys.values[colno];
    1620             :         char       *attname;
    1621             : 
    1622         256 :         if (colno > 0)
    1623         140 :             appendStringInfoString(&buf, ", ");
    1624             : 
    1625         256 :         attname = get_attname(statextrec->stxrelid, attnum, false);
    1626             : 
    1627         256 :         appendStringInfoString(&buf, quote_identifier(attname));
    1628             :     }
    1629             : 
    1630         116 :     appendStringInfo(&buf, " FROM %s",
    1631             :                      generate_relation_name(statextrec->stxrelid, NIL));
    1632             : 
    1633         116 :     ReleaseSysCache(statexttup);
    1634             : 
    1635         116 :     return buf.data;
    1636             : }
    1637             : 
    1638             : /*
    1639             :  * pg_get_partkeydef
    1640             :  *
    1641             :  * Returns the partition key specification, ie, the following:
    1642             :  *
    1643             :  * PARTITION BY { RANGE | LIST | HASH } (column opt_collation opt_opclass [, ...])
    1644             :  */
    1645             : Datum
    1646       39046 : pg_get_partkeydef(PG_FUNCTION_ARGS)
    1647             : {
    1648       39046 :     Oid         relid = PG_GETARG_OID(0);
    1649             :     char       *res;
    1650             : 
    1651       39046 :     res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
    1652             : 
    1653       39046 :     if (res == NULL)
    1654       38360 :         PG_RETURN_NULL();
    1655             : 
    1656         686 :     PG_RETURN_TEXT_P(string_to_text(res));
    1657             : }
    1658             : 
    1659             : /* Internal version that just reports the column definitions */
    1660             : char *
    1661          76 : pg_get_partkeydef_columns(Oid relid, bool pretty)
    1662             : {
    1663             :     int         prettyFlags;
    1664             : 
    1665          76 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
    1666             : 
    1667          76 :     return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
    1668             : }
    1669             : 
    1670             : /*
    1671             :  * Internal workhorse to decompile a partition key definition.
    1672             :  */
    1673             : static char *
    1674       39122 : pg_get_partkeydef_worker(Oid relid, int prettyFlags,
    1675             :                          bool attrsOnly, bool missing_ok)
    1676             : {
    1677             :     Form_pg_partitioned_table form;
    1678             :     HeapTuple   tuple;
    1679             :     oidvector  *partclass;
    1680             :     oidvector  *partcollation;
    1681             :     List       *partexprs;
    1682             :     ListCell   *partexpr_item;
    1683             :     List       *context;
    1684             :     Datum       datum;
    1685             :     bool        isnull;
    1686             :     StringInfoData buf;
    1687             :     int         keyno;
    1688             :     char       *str;
    1689             :     char       *sep;
    1690             : 
    1691       39122 :     tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
    1692       39122 :     if (!HeapTupleIsValid(tuple))
    1693             :     {
    1694       38360 :         if (missing_ok)
    1695       38360 :             return NULL;
    1696           0 :         elog(ERROR, "cache lookup failed for partition key of %u", relid);
    1697             :     }
    1698             : 
    1699         762 :     form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
    1700             : 
    1701             :     Assert(form->partrelid == relid);
    1702             : 
    1703             :     /* Must get partclass and partcollation the hard way */
    1704         762 :     datum = SysCacheGetAttr(PARTRELID, tuple,
    1705             :                             Anum_pg_partitioned_table_partclass, &isnull);
    1706             :     Assert(!isnull);
    1707         762 :     partclass = (oidvector *) DatumGetPointer(datum);
    1708             : 
    1709         762 :     datum = SysCacheGetAttr(PARTRELID, tuple,
    1710             :                             Anum_pg_partitioned_table_partcollation, &isnull);
    1711             :     Assert(!isnull);
    1712         762 :     partcollation = (oidvector *) DatumGetPointer(datum);
    1713             : 
    1714             : 
    1715             :     /*
    1716             :      * Get the expressions, if any.  (NOTE: we do not use the relcache
    1717             :      * versions of the expressions, because we want to display
    1718             :      * non-const-folded expressions.)
    1719             :      */
    1720         762 :     if (!heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
    1721             :     {
    1722             :         Datum       exprsDatum;
    1723             :         bool        isnull;
    1724             :         char       *exprsString;
    1725             : 
    1726          92 :         exprsDatum = SysCacheGetAttr(PARTRELID, tuple,
    1727             :                                      Anum_pg_partitioned_table_partexprs, &isnull);
    1728             :         Assert(!isnull);
    1729          92 :         exprsString = TextDatumGetCString(exprsDatum);
    1730          92 :         partexprs = (List *) stringToNode(exprsString);
    1731             : 
    1732          92 :         if (!IsA(partexprs, List))
    1733           0 :             elog(ERROR, "unexpected node type found in partexprs: %d",
    1734             :                  (int) nodeTag(partexprs));
    1735             : 
    1736          92 :         pfree(exprsString);
    1737             :     }
    1738             :     else
    1739         670 :         partexprs = NIL;
    1740             : 
    1741         762 :     partexpr_item = list_head(partexprs);
    1742         762 :     context = deparse_context_for(get_relation_name(relid), relid);
    1743             : 
    1744         762 :     initStringInfo(&buf);
    1745             : 
    1746         762 :     switch (form->partstrat)
    1747             :     {
    1748          22 :         case PARTITION_STRATEGY_HASH:
    1749          22 :             if (!attrsOnly)
    1750          22 :                 appendStringInfoString(&buf, "HASH");
    1751          22 :             break;
    1752         234 :         case PARTITION_STRATEGY_LIST:
    1753         234 :             if (!attrsOnly)
    1754         222 :                 appendStringInfoString(&buf, "LIST");
    1755         234 :             break;
    1756         506 :         case PARTITION_STRATEGY_RANGE:
    1757         506 :             if (!attrsOnly)
    1758         442 :                 appendStringInfoString(&buf, "RANGE");
    1759         506 :             break;
    1760           0 :         default:
    1761           0 :             elog(ERROR, "unexpected partition strategy: %d",
    1762             :                  (int) form->partstrat);
    1763             :     }
    1764             : 
    1765         762 :     if (!attrsOnly)
    1766         686 :         appendStringInfoString(&buf, " (");
    1767         762 :     sep = "";
    1768        1622 :     for (keyno = 0; keyno < form->partnatts; keyno++)
    1769             :     {
    1770         860 :         AttrNumber  attnum = form->partattrs.values[keyno];
    1771             :         Oid         keycoltype;
    1772             :         Oid         keycolcollation;
    1773             :         Oid         partcoll;
    1774             : 
    1775         860 :         appendStringInfoString(&buf, sep);
    1776         860 :         sep = ", ";
    1777         860 :         if (attnum != 0)
    1778             :         {
    1779             :             /* Simple attribute reference */
    1780             :             char       *attname;
    1781             :             int32       keycoltypmod;
    1782             : 
    1783         760 :             attname = get_attname(relid, attnum, false);
    1784         760 :             appendStringInfoString(&buf, quote_identifier(attname));
    1785         760 :             get_atttypetypmodcoll(relid, attnum,
    1786             :                                   &keycoltype, &keycoltypmod,
    1787             :                                   &keycolcollation);
    1788             :         }
    1789             :         else
    1790             :         {
    1791             :             /* Expression */
    1792             :             Node       *partkey;
    1793             : 
    1794         100 :             if (partexpr_item == NULL)
    1795           0 :                 elog(ERROR, "too few entries in partexprs list");
    1796         100 :             partkey = (Node *) lfirst(partexpr_item);
    1797         100 :             partexpr_item = lnext(partexprs, partexpr_item);
    1798             : 
    1799             :             /* Deparse */
    1800         100 :             str = deparse_expression_pretty(partkey, context, false, false,
    1801             :                                             prettyFlags, 0);
    1802             :             /* Need parens if it's not a bare function call */
    1803         100 :             if (looks_like_function(partkey))
    1804          36 :                 appendStringInfoString(&buf, str);
    1805             :             else
    1806          64 :                 appendStringInfo(&buf, "(%s)", str);
    1807             : 
    1808         100 :             keycoltype = exprType(partkey);
    1809         100 :             keycolcollation = exprCollation(partkey);
    1810             :         }
    1811             : 
    1812             :         /* Add collation, if not default for column */
    1813         860 :         partcoll = partcollation->values[keyno];
    1814         860 :         if (!attrsOnly && OidIsValid(partcoll) && partcoll != keycolcollation)
    1815           4 :             appendStringInfo(&buf, " COLLATE %s",
    1816             :                              generate_collation_name((partcoll)));
    1817             : 
    1818             :         /* Add the operator class name, if not default */
    1819         860 :         if (!attrsOnly)
    1820         748 :             get_opclass_name(partclass->values[keyno], keycoltype, &buf);
    1821             :     }
    1822             : 
    1823         762 :     if (!attrsOnly)
    1824         686 :         appendStringInfoChar(&buf, ')');
    1825             : 
    1826             :     /* Clean up */
    1827         762 :     ReleaseSysCache(tuple);
    1828             : 
    1829         762 :     return buf.data;
    1830             : }
    1831             : 
    1832             : /*
    1833             :  * pg_get_partition_constraintdef
    1834             :  *
    1835             :  * Returns partition constraint expression as a string for the input relation
    1836             :  */
    1837             : Datum
    1838         104 : pg_get_partition_constraintdef(PG_FUNCTION_ARGS)
    1839             : {
    1840         104 :     Oid         relationId = PG_GETARG_OID(0);
    1841             :     Expr       *constr_expr;
    1842             :     int         prettyFlags;
    1843             :     List       *context;
    1844             :     char       *consrc;
    1845             : 
    1846         104 :     constr_expr = get_partition_qual_relid(relationId);
    1847             : 
    1848             :     /* Quick exit if no partition constraint */
    1849         104 :     if (constr_expr == NULL)
    1850          12 :         PG_RETURN_NULL();
    1851             : 
    1852             :     /*
    1853             :      * Deparse and return the constraint expression.
    1854             :      */
    1855          92 :     prettyFlags = PRETTYFLAG_INDENT;
    1856          92 :     context = deparse_context_for(get_relation_name(relationId), relationId);
    1857          92 :     consrc = deparse_expression_pretty((Node *) constr_expr, context, false,
    1858             :                                        false, prettyFlags, 0);
    1859             : 
    1860          92 :     PG_RETURN_TEXT_P(string_to_text(consrc));
    1861             : }
    1862             : 
    1863             : /*
    1864             :  * pg_get_partconstrdef_string
    1865             :  *
    1866             :  * Returns the partition constraint as a C-string for the input relation, with
    1867             :  * the given alias.  No pretty-printing.
    1868             :  */
    1869             : char *
    1870          24 : pg_get_partconstrdef_string(Oid partitionId, char *aliasname)
    1871             : {
    1872             :     Expr       *constr_expr;
    1873             :     List       *context;
    1874             : 
    1875          24 :     constr_expr = get_partition_qual_relid(partitionId);
    1876          24 :     context = deparse_context_for(aliasname, partitionId);
    1877             : 
    1878          24 :     return deparse_expression((Node *) constr_expr, context, true, false);
    1879             : }
    1880             : 
    1881             : /*
    1882             :  * pg_get_constraintdef
    1883             :  *
    1884             :  * Returns the definition for the constraint, ie, everything that needs to
    1885             :  * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>".
    1886             :  */
    1887             : Datum
    1888         914 : pg_get_constraintdef(PG_FUNCTION_ARGS)
    1889             : {
    1890         914 :     Oid         constraintId = PG_GETARG_OID(0);
    1891             :     int         prettyFlags;
    1892             :     char       *res;
    1893             : 
    1894         914 :     prettyFlags = PRETTYFLAG_INDENT;
    1895             : 
    1896         914 :     res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
    1897             : 
    1898         914 :     if (res == NULL)
    1899           4 :         PG_RETURN_NULL();
    1900             : 
    1901         910 :     PG_RETURN_TEXT_P(string_to_text(res));
    1902             : }
    1903             : 
    1904             : Datum
    1905        1654 : pg_get_constraintdef_ext(PG_FUNCTION_ARGS)
    1906             : {
    1907        1654 :     Oid         constraintId = PG_GETARG_OID(0);
    1908        1654 :     bool        pretty = PG_GETARG_BOOL(1);
    1909             :     int         prettyFlags;
    1910             :     char       *res;
    1911             : 
    1912        1654 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
    1913             : 
    1914        1654 :     res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
    1915             : 
    1916        1654 :     if (res == NULL)
    1917           0 :         PG_RETURN_NULL();
    1918             : 
    1919        1654 :     PG_RETURN_TEXT_P(string_to_text(res));
    1920             : }
    1921             : 
    1922             : /*
    1923             :  * Internal version that returns a full ALTER TABLE ... ADD CONSTRAINT command
    1924             :  */
    1925             : char *
    1926         208 : pg_get_constraintdef_command(Oid constraintId)
    1927             : {
    1928         208 :     return pg_get_constraintdef_worker(constraintId, true, 0, false);
    1929             : }
    1930             : 
    1931             : /*
    1932             :  * As of 9.4, we now use an MVCC snapshot for this.
    1933             :  */
    1934             : static char *
    1935        2776 : pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
    1936             :                             int prettyFlags, bool missing_ok)
    1937             : {
    1938             :     HeapTuple   tup;
    1939             :     Form_pg_constraint conForm;
    1940             :     StringInfoData buf;
    1941             :     SysScanDesc scandesc;
    1942             :     ScanKeyData scankey[1];
    1943        2776 :     Snapshot    snapshot = RegisterSnapshot(GetTransactionSnapshot());
    1944        2776 :     Relation    relation = table_open(ConstraintRelationId, AccessShareLock);
    1945             : 
    1946        2776 :     ScanKeyInit(&scankey[0],
    1947             :                 Anum_pg_constraint_oid,
    1948             :                 BTEqualStrategyNumber, F_OIDEQ,
    1949             :                 ObjectIdGetDatum(constraintId));
    1950             : 
    1951        2776 :     scandesc = systable_beginscan(relation,
    1952             :                                   ConstraintOidIndexId,
    1953             :                                   true,
    1954             :                                   snapshot,
    1955             :                                   1,
    1956             :                                   scankey);
    1957             : 
    1958             :     /*
    1959             :      * We later use the tuple with SysCacheGetAttr() as if we had obtained it
    1960             :      * via SearchSysCache, which works fine.
    1961             :      */
    1962        2776 :     tup = systable_getnext(scandesc);
    1963             : 
    1964        2776 :     UnregisterSnapshot(snapshot);
    1965             : 
    1966        2776 :     if (!HeapTupleIsValid(tup))
    1967             :     {
    1968           4 :         if (missing_ok)
    1969             :         {
    1970           4 :             systable_endscan(scandesc);
    1971           4 :             table_close(relation, AccessShareLock);
    1972           4 :             return NULL;
    1973             :         }
    1974           0 :         elog(ERROR, "could not find tuple for constraint %u", constraintId);
    1975             :     }
    1976             : 
    1977        2772 :     conForm = (Form_pg_constraint) GETSTRUCT(tup);
    1978             : 
    1979        2772 :     initStringInfo(&buf);
    1980             : 
    1981        2772 :     if (fullCommand)
    1982             :     {
    1983         208 :         if (OidIsValid(conForm->conrelid))
    1984             :         {
    1985             :             /*
    1986             :              * Currently, callers want ALTER TABLE (without ONLY) for CHECK
    1987             :              * constraints, and other types of constraints don't inherit
    1988             :              * anyway so it doesn't matter whether we say ONLY or not. Someday
    1989             :              * we might need to let callers specify whether to put ONLY in the
    1990             :              * command.
    1991             :              */
    1992         200 :             appendStringInfo(&buf, "ALTER TABLE %s ADD CONSTRAINT %s ",
    1993             :                              generate_qualified_relation_name(conForm->conrelid),
    1994         200 :                              quote_identifier(NameStr(conForm->conname)));
    1995             :         }
    1996             :         else
    1997             :         {
    1998             :             /* Must be a domain constraint */
    1999             :             Assert(OidIsValid(conForm->contypid));
    2000           8 :             appendStringInfo(&buf, "ALTER DOMAIN %s ADD CONSTRAINT %s ",
    2001             :                              generate_qualified_type_name(conForm->contypid),
    2002           8 :                              quote_identifier(NameStr(conForm->conname)));
    2003             :         }
    2004             :     }
    2005             : 
    2006        2772 :     switch (conForm->contype)
    2007             :     {
    2008         370 :         case CONSTRAINT_FOREIGN:
    2009             :             {
    2010             :                 Datum       val;
    2011             :                 bool        isnull;
    2012             :                 const char *string;
    2013             : 
    2014             :                 /* Start off the constraint definition */
    2015         370 :                 appendStringInfoString(&buf, "FOREIGN KEY (");
    2016             : 
    2017             :                 /* Fetch and build referencing-column list */
    2018         370 :                 val = SysCacheGetAttr(CONSTROID, tup,
    2019             :                                       Anum_pg_constraint_conkey, &isnull);
    2020         370 :                 if (isnull)
    2021           0 :                     elog(ERROR, "null conkey for constraint %u",
    2022             :                          constraintId);
    2023             : 
    2024         370 :                 decompile_column_index_array(val, conForm->conrelid, &buf);
    2025             : 
    2026             :                 /* add foreign relation name */
    2027         370 :                 appendStringInfo(&buf, ") REFERENCES %s(",
    2028             :                                  generate_relation_name(conForm->confrelid,
    2029             :                                                         NIL));
    2030             : 
    2031             :                 /* Fetch and build referenced-column list */
    2032         370 :                 val = SysCacheGetAttr(CONSTROID, tup,
    2033             :                                       Anum_pg_constraint_confkey, &isnull);
    2034         370 :                 if (isnull)
    2035           0 :                     elog(ERROR, "null confkey for constraint %u",
    2036             :                          constraintId);
    2037             : 
    2038         370 :                 decompile_column_index_array(val, conForm->confrelid, &buf);
    2039             : 
    2040         370 :                 appendStringInfoChar(&buf, ')');
    2041             : 
    2042             :                 /* Add match type */
    2043         370 :                 switch (conForm->confmatchtype)
    2044             :                 {
    2045          22 :                     case FKCONSTR_MATCH_FULL:
    2046          22 :                         string = " MATCH FULL";
    2047          22 :                         break;
    2048           0 :                     case FKCONSTR_MATCH_PARTIAL:
    2049           0 :                         string = " MATCH PARTIAL";
    2050           0 :                         break;
    2051         348 :                     case FKCONSTR_MATCH_SIMPLE:
    2052         348 :                         string = "";
    2053         348 :                         break;
    2054           0 :                     default:
    2055           0 :                         elog(ERROR, "unrecognized confmatchtype: %d",
    2056             :                              conForm->confmatchtype);
    2057             :                         string = "";  /* keep compiler quiet */
    2058             :                         break;
    2059             :                 }
    2060         370 :                 appendStringInfoString(&buf, string);
    2061             : 
    2062             :                 /* Add ON UPDATE and ON DELETE clauses, if needed */
    2063         370 :                 switch (conForm->confupdtype)
    2064             :                 {
    2065         290 :                     case FKCONSTR_ACTION_NOACTION:
    2066         290 :                         string = NULL;  /* suppress default */
    2067         290 :                         break;
    2068           0 :                     case FKCONSTR_ACTION_RESTRICT:
    2069           0 :                         string = "RESTRICT";
    2070           0 :                         break;
    2071          62 :                     case FKCONSTR_ACTION_CASCADE:
    2072          62 :                         string = "CASCADE";
    2073          62 :                         break;
    2074          18 :                     case FKCONSTR_ACTION_SETNULL:
    2075          18 :                         string = "SET NULL";
    2076          18 :                         break;
    2077           0 :                     case FKCONSTR_ACTION_SETDEFAULT:
    2078           0 :                         string = "SET DEFAULT";
    2079           0 :                         break;
    2080           0 :                     default:
    2081           0 :                         elog(ERROR, "unrecognized confupdtype: %d",
    2082             :                              conForm->confupdtype);
    2083             :                         string = NULL;  /* keep compiler quiet */
    2084             :                         break;
    2085             :                 }
    2086         370 :                 if (string)
    2087          80 :                     appendStringInfo(&buf, " ON UPDATE %s", string);
    2088             : 
    2089         370 :                 switch (conForm->confdeltype)
    2090             :                 {
    2091         300 :                     case FKCONSTR_ACTION_NOACTION:
    2092         300 :                         string = NULL;  /* suppress default */
    2093         300 :                         break;
    2094           0 :                     case FKCONSTR_ACTION_RESTRICT:
    2095           0 :                         string = "RESTRICT";
    2096           0 :                         break;
    2097          62 :                     case FKCONSTR_ACTION_CASCADE:
    2098          62 :                         string = "CASCADE";
    2099          62 :                         break;
    2100           8 :                     case FKCONSTR_ACTION_SETNULL:
    2101           8 :                         string = "SET NULL";
    2102           8 :                         break;
    2103           0 :                     case FKCONSTR_ACTION_SETDEFAULT:
    2104           0 :                         string = "SET DEFAULT";
    2105           0 :                         break;
    2106           0 :                     default:
    2107           0 :                         elog(ERROR, "unrecognized confdeltype: %d",
    2108             :                              conForm->confdeltype);
    2109             :                         string = NULL;  /* keep compiler quiet */
    2110             :                         break;
    2111             :                 }
    2112         370 :                 if (string)
    2113          70 :                     appendStringInfo(&buf, " ON DELETE %s", string);
    2114             : 
    2115         370 :                 break;
    2116             :             }
    2117        1234 :         case CONSTRAINT_PRIMARY:
    2118             :         case CONSTRAINT_UNIQUE:
    2119             :             {
    2120             :                 Datum       val;
    2121             :                 bool        isnull;
    2122             :                 Oid         indexId;
    2123             :                 int         keyatts;
    2124             :                 HeapTuple   indtup;
    2125             : 
    2126             :                 /* Start off the constraint definition */
    2127        1234 :                 if (conForm->contype == CONSTRAINT_PRIMARY)
    2128        1062 :                     appendStringInfoString(&buf, "PRIMARY KEY (");
    2129             :                 else
    2130         172 :                     appendStringInfoString(&buf, "UNIQUE (");
    2131             : 
    2132             :                 /* Fetch and build target column list */
    2133        1234 :                 val = SysCacheGetAttr(CONSTROID, tup,
    2134             :                                       Anum_pg_constraint_conkey, &isnull);
    2135        1234 :                 if (isnull)
    2136           0 :                     elog(ERROR, "null conkey for constraint %u",
    2137             :                          constraintId);
    2138             : 
    2139        1234 :                 keyatts = decompile_column_index_array(val, conForm->conrelid, &buf);
    2140             : 
    2141        1234 :                 appendStringInfoChar(&buf, ')');
    2142             : 
    2143        1234 :                 indexId = get_constraint_index(constraintId);
    2144             : 
    2145             :                 /* Build including column list (from pg_index.indkeys) */
    2146        1234 :                 indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
    2147        1234 :                 if (!HeapTupleIsValid(indtup))
    2148           0 :                     elog(ERROR, "cache lookup failed for index %u", indexId);
    2149        1234 :                 val = SysCacheGetAttr(INDEXRELID, indtup,
    2150             :                                       Anum_pg_index_indnatts, &isnull);
    2151        1234 :                 if (isnull)
    2152           0 :                     elog(ERROR, "null indnatts for index %u", indexId);
    2153        1234 :                 if (DatumGetInt32(val) > keyatts)
    2154             :                 {
    2155             :                     Datum       cols;
    2156             :                     Datum      *keys;
    2157             :                     int         nKeys;
    2158             :                     int         j;
    2159             : 
    2160          52 :                     appendStringInfoString(&buf, " INCLUDE (");
    2161             : 
    2162          52 :                     cols = SysCacheGetAttr(INDEXRELID, indtup,
    2163             :                                            Anum_pg_index_indkey, &isnull);
    2164          52 :                     if (isnull)
    2165           0 :                         elog(ERROR, "null indkey for index %u", indexId);
    2166             : 
    2167          52 :                     deconstruct_array(DatumGetArrayTypeP(cols),
    2168             :                                       INT2OID, 2, true, TYPALIGN_SHORT,
    2169             :                                       &keys, NULL, &nKeys);
    2170             : 
    2171         156 :                     for (j = keyatts; j < nKeys; j++)
    2172             :                     {
    2173             :                         char       *colName;
    2174             : 
    2175         104 :                         colName = get_attname(conForm->conrelid,
    2176         104 :                                               DatumGetInt16(keys[j]), false);
    2177         104 :                         if (j > keyatts)
    2178          52 :                             appendStringInfoString(&buf, ", ");
    2179         104 :                         appendStringInfoString(&buf, quote_identifier(colName));
    2180             :                     }
    2181             : 
    2182          52 :                     appendStringInfoChar(&buf, ')');
    2183             :                 }
    2184        1234 :                 ReleaseSysCache(indtup);
    2185             : 
    2186             :                 /* XXX why do we only print these bits if fullCommand? */
    2187        1234 :                 if (fullCommand && OidIsValid(indexId))
    2188             :                 {
    2189         112 :                     char       *options = flatten_reloptions(indexId);
    2190             :                     Oid         tblspc;
    2191             : 
    2192         112 :                     if (options)
    2193             :                     {
    2194           0 :                         appendStringInfo(&buf, " WITH (%s)", options);
    2195           0 :                         pfree(options);
    2196             :                     }
    2197             : 
    2198             :                     /*
    2199             :                      * Print the tablespace, unless it's the database default.
    2200             :                      * This is to help ALTER TABLE usage of this facility,
    2201             :                      * which needs this behavior to recreate exact catalog
    2202             :                      * state.
    2203             :                      */
    2204         112 :                     tblspc = get_rel_tablespace(indexId);
    2205         112 :                     if (OidIsValid(tblspc))
    2206          16 :                         appendStringInfo(&buf, " USING INDEX TABLESPACE %s",
    2207          16 :                                          quote_identifier(get_tablespace_name(tblspc)));
    2208             :                 }
    2209             : 
    2210        1234 :                 break;
    2211             :             }
    2212        1100 :         case CONSTRAINT_CHECK:
    2213             :             {
    2214             :                 Datum       val;
    2215             :                 bool        isnull;
    2216             :                 char       *conbin;
    2217             :                 char       *consrc;
    2218             :                 Node       *expr;
    2219             :                 List       *context;
    2220             : 
    2221             :                 /* Fetch constraint expression in parsetree form */
    2222        1100 :                 val = SysCacheGetAttr(CONSTROID, tup,
    2223             :                                       Anum_pg_constraint_conbin, &isnull);
    2224        1100 :                 if (isnull)
    2225           0 :                     elog(ERROR, "null conbin for constraint %u",
    2226             :                          constraintId);
    2227             : 
    2228        1100 :                 conbin = TextDatumGetCString(val);
    2229        1100 :                 expr = stringToNode(conbin);
    2230             : 
    2231             :                 /* Set up deparsing context for Var nodes in constraint */
    2232        1100 :                 if (conForm->conrelid != InvalidOid)
    2233             :                 {
    2234             :                     /* relation constraint */
    2235         990 :                     context = deparse_context_for(get_relation_name(conForm->conrelid),
    2236             :                                                   conForm->conrelid);
    2237             :                 }
    2238             :                 else
    2239             :                 {
    2240             :                     /* domain constraint --- can't have Vars */
    2241         110 :                     context = NIL;
    2242             :                 }
    2243             : 
    2244        1100 :                 consrc = deparse_expression_pretty(expr, context, false, false,
    2245             :                                                    prettyFlags, 0);
    2246             : 
    2247             :                 /*
    2248             :                  * Now emit the constraint definition, adding NO INHERIT if
    2249             :                  * necessary.
    2250             :                  *
    2251             :                  * There are cases where the constraint expression will be
    2252             :                  * fully parenthesized and we don't need the outer parens ...
    2253             :                  * but there are other cases where we do need 'em.  Be
    2254             :                  * conservative for now.
    2255             :                  *
    2256             :                  * Note that simply checking for leading '(' and trailing ')'
    2257             :                  * would NOT be good enough, consider "(x > 0) AND (y > 0)".
    2258             :                  */
    2259        1100 :                 appendStringInfo(&buf, "CHECK (%s)%s",
    2260             :                                  consrc,
    2261        1100 :                                  conForm->connoinherit ? " NO INHERIT" : "");
    2262        1100 :                 break;
    2263             :             }
    2264           0 :         case CONSTRAINT_TRIGGER:
    2265             : 
    2266             :             /*
    2267             :              * There isn't an ALTER TABLE syntax for creating a user-defined
    2268             :              * constraint trigger, but it seems better to print something than
    2269             :              * throw an error; if we throw error then this function couldn't
    2270             :              * safely be applied to all rows of pg_constraint.
    2271             :              */
    2272           0 :             appendStringInfoString(&buf, "TRIGGER");
    2273           0 :             break;
    2274          68 :         case CONSTRAINT_EXCLUSION:
    2275             :             {
    2276          68 :                 Oid         indexOid = conForm->conindid;
    2277             :                 Datum       val;
    2278             :                 bool        isnull;
    2279             :                 Datum      *elems;
    2280             :                 int         nElems;
    2281             :                 int         i;
    2282             :                 Oid        *operators;
    2283             : 
    2284             :                 /* Extract operator OIDs from the pg_constraint tuple */
    2285          68 :                 val = SysCacheGetAttr(CONSTROID, tup,
    2286             :                                       Anum_pg_constraint_conexclop,
    2287             :                                       &isnull);
    2288          68 :                 if (isnull)
    2289           0 :                     elog(ERROR, "null conexclop for constraint %u",
    2290             :                          constraintId);
    2291             : 
    2292          68 :                 deconstruct_array(DatumGetArrayTypeP(val),
    2293             :                                   OIDOID, sizeof(Oid), true, TYPALIGN_INT,
    2294             :                                   &elems, NULL, &nElems);
    2295             : 
    2296          68 :                 operators = (Oid *) palloc(nElems * sizeof(Oid));
    2297         148 :                 for (i = 0; i < nElems; i++)
    2298          80 :                     operators[i] = DatumGetObjectId(elems[i]);
    2299             : 
    2300             :                 /* pg_get_indexdef_worker does the rest */
    2301             :                 /* suppress tablespace because pg_dump wants it that way */
    2302          68 :                 appendStringInfoString(&buf,
    2303          68 :                                        pg_get_indexdef_worker(indexOid,
    2304             :                                                               0,
    2305             :                                                               operators,
    2306             :                                                               false,
    2307             :                                                               false,
    2308             :                                                               false,
    2309             :                                                               false,
    2310             :                                                               prettyFlags,
    2311             :                                                               false));
    2312          68 :                 break;
    2313             :             }
    2314           0 :         default:
    2315           0 :             elog(ERROR, "invalid constraint type \"%c\"", conForm->contype);
    2316             :             break;
    2317             :     }
    2318             : 
    2319        2772 :     if (conForm->condeferrable)
    2320          26 :         appendStringInfoString(&buf, " DEFERRABLE");
    2321        2772 :     if (conForm->condeferred)
    2322           0 :         appendStringInfoString(&buf, " INITIALLY DEFERRED");
    2323        2772 :     if (!conForm->convalidated)
    2324          58 :         appendStringInfoString(&buf, " NOT VALID");
    2325             : 
    2326             :     /* Cleanup */
    2327        2772 :     systable_endscan(scandesc);
    2328        2772 :     table_close(relation, AccessShareLock);
    2329             : 
    2330        2772 :     return buf.data;
    2331             : }
    2332             : 
    2333             : 
    2334             : /*
    2335             :  * Convert an int16[] Datum into a comma-separated list of column names
    2336             :  * for the indicated relation; append the list to buf.  Returns the number
    2337             :  * of keys.
    2338             :  */
    2339             : static int
    2340        1974 : decompile_column_index_array(Datum column_index_array, Oid relId,
    2341             :                              StringInfo buf)
    2342             : {
    2343             :     Datum      *keys;
    2344             :     int         nKeys;
    2345             :     int         j;
    2346             : 
    2347             :     /* Extract data from array of int16 */
    2348        1974 :     deconstruct_array(DatumGetArrayTypeP(column_index_array),
    2349             :                       INT2OID, 2, true, TYPALIGN_SHORT,
    2350             :                       &keys, NULL, &nKeys);
    2351             : 
    2352        4468 :     for (j = 0; j < nKeys; j++)
    2353             :     {
    2354             :         char       *colName;
    2355             : 
    2356        2494 :         colName = get_attname(relId, DatumGetInt16(keys[j]), false);
    2357             : 
    2358        2494 :         if (j == 0)
    2359        1974 :             appendStringInfoString(buf, quote_identifier(colName));
    2360             :         else
    2361         520 :             appendStringInfo(buf, ", %s", quote_identifier(colName));
    2362             :     }
    2363             : 
    2364        1974 :     return nKeys;
    2365             : }
    2366             : 
    2367             : 
    2368             : /* ----------
    2369             :  * pg_get_expr          - Decompile an expression tree
    2370             :  *
    2371             :  * Input: an expression tree in nodeToString form, and a relation OID
    2372             :  *
    2373             :  * Output: reverse-listed expression
    2374             :  *
    2375             :  * Currently, the expression can only refer to a single relation, namely
    2376             :  * the one specified by the second parameter.  This is sufficient for
    2377             :  * partial indexes, column default expressions, etc.  We also support
    2378             :  * Var-free expressions, for which the OID can be InvalidOid.
    2379             :  * ----------
    2380             :  */
    2381             : Datum
    2382        3316 : pg_get_expr(PG_FUNCTION_ARGS)
    2383             : {
    2384        3316 :     text       *expr = PG_GETARG_TEXT_PP(0);
    2385        3316 :     Oid         relid = PG_GETARG_OID(1);
    2386             :     int         prettyFlags;
    2387             :     char       *relname;
    2388             : 
    2389        3316 :     prettyFlags = PRETTYFLAG_INDENT;
    2390             : 
    2391        3316 :     if (OidIsValid(relid))
    2392             :     {
    2393             :         /* Get the name for the relation */
    2394        3316 :         relname = get_rel_name(relid);
    2395             : 
    2396             :         /*
    2397             :          * If the OID isn't actually valid, don't throw an error, just return
    2398             :          * NULL.  This is a bit questionable, but it's what we've done
    2399             :          * historically, and it can help avoid unwanted failures when
    2400             :          * examining catalog entries for just-deleted relations.
    2401             :          */
    2402        3316 :         if (relname == NULL)
    2403           0 :             PG_RETURN_NULL();
    2404             :     }
    2405             :     else
    2406           0 :         relname = NULL;
    2407             : 
    2408        3316 :     PG_RETURN_TEXT_P(pg_get_expr_worker(expr, relid, relname, prettyFlags));
    2409             : }
    2410             : 
    2411             : Datum
    2412         252 : pg_get_expr_ext(PG_FUNCTION_ARGS)
    2413             : {
    2414         252 :     text       *expr = PG_GETARG_TEXT_PP(0);
    2415         252 :     Oid         relid = PG_GETARG_OID(1);
    2416         252 :     bool        pretty = PG_GETARG_BOOL(2);
    2417             :     int         prettyFlags;
    2418             :     char       *relname;
    2419             : 
    2420         252 :     prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
    2421             : 
    2422         252 :     if (OidIsValid(relid))
    2423             :     {
    2424             :         /* Get the name for the relation */
    2425         252 :         relname = get_rel_name(relid);
    2426             :         /* See notes above */
    2427         252 :         if (relname == NULL)
    2428           0 :             PG_RETURN_NULL();
    2429             :     }
    2430             :     else
    2431           0 :         relname = NULL;
    2432             : 
    2433         252 :     PG_RETURN_TEXT_P(pg_get_expr_worker(expr, relid, relname, prettyFlags));
    2434             : }
    2435             : 
    2436             : static text *
    2437        3568 : pg_get_expr_worker(text *expr, Oid relid, const char *relname, int prettyFlags)
    2438             : {
    2439             :     Node       *node;
    2440             :     List       *context;
    2441             :     char       *exprstr;
    2442             :     char       *str;
    2443             : 
    2444             :     /* Convert input TEXT object to C string */
    2445        3568 :     exprstr = text_to_cstring(expr);
    2446             : 
    2447             :     /* Convert expression to node tree */
    2448        3568 :     node = (Node *) stringToNode(exprstr);
    2449             : 
    2450        3568 :     pfree(exprstr);
    2451             : 
    2452             :     /* Prepare deparse context if needed */
    2453        3568 :     if (OidIsValid(relid))
    2454        3568 :         context = deparse_context_for(relname, relid);
    2455             :     else
    2456           0 :         context = NIL;
    2457             : 
    2458             :     /* Deparse */
    2459        3568 :     str = deparse_expression_pretty(node, context, false, false,
    2460             :                                     prettyFlags, 0);
    2461             : 
    2462        3568 :     return string_to_text(str);
    2463             : }
    2464             : 
    2465             : 
    2466             : /* ----------
    2467             :  * pg_get_userbyid      - Get a user name by roleid and
    2468             :  *                fallback to 'unknown (OID=n)'
    2469             :  * ----------
    2470             :  */
    2471             : Datum
    2472         804 : pg_get_userbyid(PG_FUNCTION_ARGS)
    2473             : {
    2474         804 :     Oid         roleid = PG_GETARG_OID(0);
    2475             :     Name        result;
    2476             :     HeapTuple   roletup;
    2477             :     Form_pg_authid role_rec;
    2478             : 
    2479             :     /*
    2480             :      * Allocate space for the result
    2481             :      */
    2482         804 :     result = (Name) palloc(NAMEDATALEN);
    2483         804 :     memset(NameStr(*result), 0, NAMEDATALEN);
    2484             : 
    2485             :     /*
    2486             :      * Get the pg_authid entry and print the result
    2487             :      */
    2488         804 :     roletup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
    2489         804 :     if (HeapTupleIsValid(roletup))
    2490             :     {
    2491         804 :         role_rec = (Form_pg_authid) GETSTRUCT(roletup);
    2492         804 :         *result = role_rec->rolname;
    2493         804 :         ReleaseSysCache(roletup);
    2494             :     }
    2495             :     else
    2496           0 :         sprintf(NameStr(*result), "unknown (OID=%u)", roleid);
    2497             : 
    2498         804 :     PG_RETURN_NAME(result);
    2499             : }
    2500             : 
    2501             : 
    2502             : /*
    2503             :  * pg_get_serial_sequence
    2504             :  *      Get the name of the sequence used by an identity or serial column,
    2505             :  *      formatted suitably for passing to setval, nextval or currval.
    2506             :  *      First parameter is not treated as double-quoted, second parameter
    2507             :  *      is --- see documentation for reason.
    2508             :  */
    2509             : Datum
    2510           8 : pg_get_serial_sequence(PG_FUNCTION_ARGS)
    2511             : {
    2512           8 :     text       *tablename = PG_GETARG_TEXT_PP(0);
    2513           8 :     text       *columnname = PG_GETARG_TEXT_PP(1);
    2514             :     RangeVar   *tablerv;
    2515             :     Oid         tableOid;
    2516             :     char       *column;
    2517             :     AttrNumber  attnum;
    2518           8 :     Oid         sequenceId = InvalidOid;
    2519             :     Relation    depRel;
    2520             :     ScanKeyData key[3];
    2521             :     SysScanDesc scan;
    2522             :     HeapTuple   tup;
    2523             : 
    2524             :     /* Look up table name.  Can't lock it - we might not have privileges. */
    2525           8 :     tablerv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
    2526           8 :     tableOid = RangeVarGetRelid(tablerv, NoLock, false);
    2527             : 
    2528             :     /* Get the number of the column */
    2529           8 :     column = text_to_cstring(columnname);
    2530             : 
    2531           8 :     attnum = get_attnum(tableOid, column);
    2532           8 :     if (attnum == InvalidAttrNumber)
    2533           0 :         ereport(ERROR,
    2534             :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
    2535             :                  errmsg("column \"%s\" of relation \"%s\" does not exist",
    2536             :                         column, tablerv->relname)));
    2537             : 
    2538             :     /* Search the dependency table for the dependent sequence */
    2539           8 :     depRel = table_open(DependRelationId, AccessShareLock);
    2540             : 
    2541           8 :     ScanKeyInit(&key[0],
    2542             :                 Anum_pg_depend_refclassid,
    2543             :                 BTEqualStrategyNumber, F_OIDEQ,
    2544             :                 ObjectIdGetDatum(RelationRelationId));
    2545           8 :     ScanKeyInit(&key[1],
    2546             :                 Anum_pg_depend_refobjid,
    2547             :                 BTEqualStrategyNumber, F_OIDEQ,
    2548             :                 ObjectIdGetDatum(tableOid));
    2549           8 :     ScanKeyInit(&key[2],
    2550             :                 Anum_pg_depend_refobjsubid,
    2551             :                 BTEqualStrategyNumber, F_INT4EQ,
    2552             :                 Int32GetDatum(attnum));
    2553             : 
    2554           8 :     scan = systable_beginscan(depRel, DependReferenceIndexId, true,
    2555             :                               NULL, 3, key);
    2556             : 
    2557          12 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
    2558             :     {
    2559          12 :         Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
    2560             : 
    2561             :         /*
    2562             :          * Look for an auto dependency (serial column) or internal dependency
    2563             :          * (identity column) of a sequence on a column.  (We need the relkind
    2564             :          * test because indexes can also have auto dependencies on columns.)
    2565             :          */
    2566          12 :         if (deprec->classid == RelationRelationId &&
    2567           8 :             deprec->objsubid == 0 &&
    2568           8 :             (deprec->deptype == DEPENDENCY_AUTO ||
    2569          12 :              deprec->deptype == DEPENDENCY_INTERNAL) &&
    2570           8 :             get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
    2571             :         {
    2572           8 :             sequenceId = deprec->objid;
    2573           8 :             break;
    2574             :         }
    2575             :     }
    2576             : 
    2577           8 :     systable_endscan(scan);
    2578           8 :     table_close(depRel, AccessShareLock);
    2579             : 
    2580           8 :     if (OidIsValid(sequenceId))
    2581             :     {
    2582             :         char       *result;
    2583             : 
    2584           8 :         result = generate_qualified_relation_name(sequenceId);
    2585             : 
    2586           8 :         PG_RETURN_TEXT_P(string_to_text(result));
    2587             :     }
    2588             : 
    2589           0 :     PG_RETURN_NULL();
    2590             : }
    2591             : 
    2592             : 
    2593             : /*
    2594             :  * pg_get_functiondef
    2595             :  *      Returns the complete "CREATE OR REPLACE FUNCTION ..." statement for
    2596             :  *      the specified function.
    2597             :  *
    2598             :  * Note: if you change the output format of this function, be careful not
    2599             :  * to break psql's rules (in \ef and \sf) for identifying the start of the
    2600             :  * function body.  To wit: the function body starts on a line that begins
    2601             :  * with "AS ", and no preceding line will look like that.
    2602             :  */
    2603             : Datum
    2604          28 : pg_get_functiondef(PG_FUNCTION_ARGS)
    2605             : {
    2606          28 :     Oid         funcid = PG_GETARG_OID(0);
    2607             :     StringInfoData buf;
    2608             :     StringInfoData dq;
    2609             :     HeapTuple   proctup;
    2610             :     Form_pg_proc proc;
    2611             :     bool        isfunction;
    2612             :     Datum       tmp;
    2613             :     bool        isnull;
    2614             :     const char *prosrc;
    2615             :     const char *name;
    2616             :     const char *nsp;
    2617             :     float4      procost;
    2618             :     int         oldlen;
    2619             : 
    2620          28 :     initStringInfo(&buf);
    2621             : 
    2622             :     /* Look up the function */
    2623          28 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2624          28 :     if (!HeapTupleIsValid(proctup))
    2625           4 :         PG_RETURN_NULL();
    2626             : 
    2627          24 :     proc = (Form_pg_proc) GETSTRUCT(proctup);
    2628          24 :     name = NameStr(proc->proname);
    2629             : 
    2630          24 :     if (proc->prokind == PROKIND_AGGREGATE)
    2631           0 :         ereport(ERROR,
    2632             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2633             :                  errmsg("\"%s\" is an aggregate function", name)));
    2634             : 
    2635          24 :     isfunction = (proc->prokind != PROKIND_PROCEDURE);
    2636             : 
    2637             :     /*
    2638             :      * We always qualify the function name, to ensure the right function gets
    2639             :      * replaced.
    2640             :      */
    2641          24 :     nsp = get_namespace_name(proc->pronamespace);
    2642          24 :     appendStringInfo(&buf, "CREATE OR REPLACE %s %s(",
    2643             :                      isfunction ? "FUNCTION" : "PROCEDURE",
    2644             :                      quote_qualified_identifier(nsp, name));
    2645          24 :     (void) print_function_arguments(&buf, proctup, false, true);
    2646          24 :     appendStringInfoString(&buf, ")\n");
    2647          24 :     if (isfunction)
    2648             :     {
    2649          20 :         appendStringInfoString(&buf, " RETURNS ");
    2650          20 :         print_function_rettype(&buf, proctup);
    2651          20 :         appendStringInfoChar(&buf, '\n');
    2652             :     }
    2653             : 
    2654          24 :     print_function_trftypes(&buf, proctup);
    2655             : 
    2656          24 :     appendStringInfo(&buf, " LANGUAGE %s\n",
    2657          24 :                      quote_identifier(get_language_name(proc->prolang, false)));
    2658             : 
    2659             :     /* Emit some miscellaneous options on one line */
    2660          24 :     oldlen = buf.len;
    2661             : 
    2662          24 :     if (proc->prokind == PROKIND_WINDOW)
    2663           0 :         appendStringInfoString(&buf, " WINDOW");
    2664          24 :     switch (proc->provolatile)
    2665             :     {
    2666           4 :         case PROVOLATILE_IMMUTABLE:
    2667           4 :             appendStringInfoString(&buf, " IMMUTABLE");
    2668           4 :             break;
    2669           4 :         case PROVOLATILE_STABLE:
    2670           4 :             appendStringInfoString(&buf, " STABLE");
    2671           4 :             break;
    2672          16 :         case PROVOLATILE_VOLATILE:
    2673          16 :             break;
    2674             :     }
    2675             : 
    2676          24 :     switch (proc->proparallel)
    2677             :     {
    2678           0 :         case PROPARALLEL_SAFE:
    2679           0 :             appendStringInfoString(&buf, " PARALLEL SAFE");
    2680           0 :             break;
    2681           0 :         case PROPARALLEL_RESTRICTED:
    2682           0 :             appendStringInfoString(&buf, " PARALLEL RESTRICTED");
    2683           0 :             break;
    2684          24 :         case PROPARALLEL_UNSAFE:
    2685          24 :             break;
    2686             :     }
    2687             : 
    2688          24 :     if (proc->proisstrict)
    2689           8 :         appendStringInfoString(&buf, " STRICT");
    2690          24 :     if (proc->prosecdef)
    2691           4 :         appendStringInfoString(&buf, " SECURITY DEFINER");
    2692          24 :     if (proc->proleakproof)
    2693           0 :         appendStringInfoString(&buf, " LEAKPROOF");
    2694             : 
    2695             :     /* This code for the default cost and rows should match functioncmds.c */
    2696          24 :     if (proc->prolang == INTERNALlanguageId ||
    2697          24 :         proc->prolang == ClanguageId)
    2698           0 :         procost = 1;
    2699             :     else
    2700          24 :         procost = 100;
    2701          24 :     if (proc->procost != procost)
    2702           0 :         appendStringInfo(&buf, " COST %g", proc->procost);
    2703             : 
    2704          24 :     if (proc->prorows > 0 && proc->prorows != 1000)
    2705           0 :         appendStringInfo(&buf, " ROWS %g", proc->prorows);
    2706             : 
    2707          24 :     if (proc->prosupport)
    2708             :     {
    2709             :         Oid         argtypes[1];
    2710             : 
    2711             :         /*
    2712             :          * We should qualify the support function's name if it wouldn't be
    2713             :          * resolved by lookup in the current search path.
    2714             :          */
    2715           0 :         argtypes[0] = INTERNALOID;
    2716           0 :         appendStringInfo(&buf, " SUPPORT %s",
    2717             :                          generate_function_name(proc->prosupport, 1,
    2718             :                                                 NIL, argtypes,
    2719             :                                                 false, NULL, EXPR_KIND_NONE));
    2720             :     }
    2721             : 
    2722          24 :     if (oldlen != buf.len)
    2723          16 :         appendStringInfoChar(&buf, '\n');
    2724             : 
    2725             :     /* Emit any proconfig options, one per line */
    2726          24 :     tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
    2727          24 :     if (!isnull)
    2728             :     {
    2729           4 :         ArrayType  *a = DatumGetArrayTypeP(tmp);
    2730             :         int         i;
    2731             : 
    2732             :         Assert(ARR_ELEMTYPE(a) == TEXTOID);
    2733             :         Assert(ARR_NDIM(a) == 1);
    2734             :         Assert(ARR_LBOUND(a)[0] == 1);
    2735             : 
    2736          24 :         for (i = 1; i <= ARR_DIMS(a)[0]; i++)
    2737             :         {
    2738             :             Datum       d;
    2739             : 
    2740          20 :             d = array_ref(a, 1, &i,
    2741             :                           -1 /* varlenarray */ ,
    2742             :                           -1 /* TEXT's typlen */ ,
    2743             :                           false /* TEXT's typbyval */ ,
    2744             :                           TYPALIGN_INT /* TEXT's typalign */ ,
    2745             :                           &isnull);
    2746          20 :             if (!isnull)
    2747             :             {
    2748          20 :                 char       *configitem = TextDatumGetCString(d);
    2749             :                 char       *pos;
    2750             : 
    2751          20 :                 pos = strchr(configitem, '=');
    2752          20 :                 if (pos == NULL)
    2753           0 :                     continue;
    2754          20 :                 *pos++ = '\0';
    2755             : 
    2756          20 :                 appendStringInfo(&buf, " SET %s TO ",
    2757             :                                  quote_identifier(configitem));
    2758             : 
    2759             :                 /*
    2760             :                  * Variables that are marked GUC_LIST_QUOTE were already fully
    2761             :                  * quoted by flatten_set_variable_args() before they were put
    2762             :                  * into the proconfig array.  However, because the quoting
    2763             :                  * rules used there aren't exactly like SQL's, we have to
    2764             :                  * break the list value apart and then quote the elements as
    2765             :                  * string literals.  (The elements may be double-quoted as-is,
    2766             :                  * but we can't just feed them to the SQL parser; it would do
    2767             :                  * the wrong thing with elements that are zero-length or
    2768             :                  * longer than NAMEDATALEN.)
    2769             :                  *
    2770             :                  * Variables that are not so marked should just be emitted as
    2771             :                  * simple string literals.  If the variable is not known to
    2772             :                  * guc.c, we'll do that; this makes it unsafe to use
    2773             :                  * GUC_LIST_QUOTE for extension variables.
    2774             :                  */
    2775          20 :                 if (GetConfigOptionFlags(configitem, true) & GUC_LIST_QUOTE)
    2776             :                 {
    2777             :                     List       *namelist;
    2778             :                     ListCell   *lc;
    2779             : 
    2780             :                     /* Parse string into list of identifiers */
    2781           8 :                     if (!SplitGUCList(pos, ',', &namelist))
    2782             :                     {
    2783             :                         /* this shouldn't fail really */
    2784           0 :                         elog(ERROR, "invalid list syntax in proconfig item");
    2785             :                     }
    2786          28 :                     foreach(lc, namelist)
    2787             :                     {
    2788          20 :                         char       *curname = (char *) lfirst(lc);
    2789             : 
    2790          20 :                         simple_quote_literal(&buf, curname);
    2791          20 :                         if (lnext(namelist, lc))
    2792          12 :                             appendStringInfoString(&buf, ", ");
    2793             :                     }
    2794             :                 }
    2795             :                 else
    2796          12 :                     simple_quote_literal(&buf, pos);
    2797          20 :                 appendStringInfoChar(&buf, '\n');
    2798             :             }
    2799             :         }
    2800             :     }
    2801             : 
    2802             :     /* And finally the function definition ... */
    2803          24 :     appendStringInfoString(&buf, "AS ");
    2804             : 
    2805          24 :     tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
    2806          24 :     if (!isnull)
    2807             :     {
    2808           0 :         simple_quote_literal(&buf, TextDatumGetCString(tmp));
    2809           0 :         appendStringInfoString(&buf, ", "); /* assume prosrc isn't null */
    2810             :     }
    2811             : 
    2812          24 :     tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosrc, &isnull);
    2813          24 :     if (isnull)
    2814           0 :         elog(ERROR, "null prosrc");
    2815          24 :     prosrc = TextDatumGetCString(tmp);
    2816             : 
    2817             :     /*
    2818             :      * We always use dollar quoting.  Figure out a suitable delimiter.
    2819             :      *
    2820             :      * Since the user is likely to be editing the function body string, we
    2821             :      * shouldn't use a short delimiter that he might easily create a conflict
    2822             :      * with.  Hence prefer "$function$"/"$procedure$", but extend if needed.
    2823             :      */
    2824          24 :     initStringInfo(&dq);
    2825          24 :     appendStringInfoChar(&dq, '$');
    2826          24 :     appendStringInfoString(&dq, (isfunction ? "function" : "procedure"));
    2827          24 :     while (strstr(prosrc, dq.data) != NULL)
    2828           0 :         appendStringInfoChar(&dq, 'x');
    2829          24 :     appendStringInfoChar(&dq, '$');
    2830             : 
    2831          24 :     appendBinaryStringInfo(&buf, dq.data, dq.len);
    2832          24 :     appendStringInfoString(&buf, prosrc);
    2833          24 :     appendBinaryStringInfo(&buf, dq.data, dq.len);
    2834             : 
    2835          24 :     appendStringInfoChar(&buf, '\n');
    2836             : 
    2837          24 :     ReleaseSysCache(proctup);
    2838             : 
    2839          24 :     PG_RETURN_TEXT_P(string_to_text(buf.data));
    2840             : }
    2841             : 
    2842             : /*
    2843             :  * pg_get_function_arguments
    2844             :  *      Get a nicely-formatted list of arguments for a function.
    2845             :  *      This is everything that would go between the parentheses in
    2846             :  *      CREATE FUNCTION.
    2847             :  */
    2848             : Datum
    2849        2346 : pg_get_function_arguments(PG_FUNCTION_ARGS)
    2850             : {
    2851        2346 :     Oid         funcid = PG_GETARG_OID(0);
    2852             :     StringInfoData buf;
    2853             :     HeapTuple   proctup;
    2854             : 
    2855        2346 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2856        2346 :     if (!HeapTupleIsValid(proctup))
    2857           4 :         PG_RETURN_NULL();
    2858             : 
    2859        2342 :     initStringInfo(&buf);
    2860             : 
    2861        2342 :     (void) print_function_arguments(&buf, proctup, false, true);
    2862             : 
    2863        2342 :     ReleaseSysCache(proctup);
    2864             : 
    2865        2342 :     PG_RETURN_TEXT_P(string_to_text(buf.data));
    2866             : }
    2867             : 
    2868             : /*
    2869             :  * pg_get_function_identity_arguments
    2870             :  *      Get a formatted list of arguments for a function.
    2871             :  *      This is everything that would go between the parentheses in
    2872             :  *      ALTER FUNCTION, etc.  In particular, don't print defaults.
    2873             :  */
    2874             : Datum
    2875        2172 : pg_get_function_identity_arguments(PG_FUNCTION_ARGS)
    2876             : {
    2877        2172 :     Oid         funcid = PG_GETARG_OID(0);
    2878             :     StringInfoData buf;
    2879             :     HeapTuple   proctup;
    2880             : 
    2881        2172 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2882        2172 :     if (!HeapTupleIsValid(proctup))
    2883           4 :         PG_RETURN_NULL();
    2884             : 
    2885        2168 :     initStringInfo(&buf);
    2886             : 
    2887        2168 :     (void) print_function_arguments(&buf, proctup, false, false);
    2888             : 
    2889        2168 :     ReleaseSysCache(proctup);
    2890             : 
    2891        2168 :     PG_RETURN_TEXT_P(string_to_text(buf.data));
    2892             : }
    2893             : 
    2894             : /*
    2895             :  * pg_get_function_result
    2896             :  *      Get a nicely-formatted version of the result type of a function.
    2897             :  *      This is what would appear after RETURNS in CREATE FUNCTION.
    2898             :  */
    2899             : Datum
    2900        1978 : pg_get_function_result(PG_FUNCTION_ARGS)
    2901             : {
    2902        1978 :     Oid         funcid = PG_GETARG_OID(0);
    2903             :     StringInfoData buf;
    2904             :     HeapTuple   proctup;
    2905             : 
    2906        1978 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2907        1978 :     if (!HeapTupleIsValid(proctup))
    2908           4 :         PG_RETURN_NULL();
    2909             : 
    2910        1974 :     if (((Form_pg_proc) GETSTRUCT(proctup))->prokind == PROKIND_PROCEDURE)
    2911             :     {
    2912          82 :         ReleaseSysCache(proctup);
    2913          82 :         PG_RETURN_NULL();
    2914             :     }
    2915             : 
    2916        1892 :     initStringInfo(&buf);
    2917             : 
    2918        1892 :     print_function_rettype(&buf, proctup);
    2919             : 
    2920        1892 :     ReleaseSysCache(proctup);
    2921             : 
    2922        1892 :     PG_RETURN_TEXT_P(string_to_text(buf.data));
    2923             : }
    2924             : 
    2925             : /*
    2926             :  * Guts of pg_get_function_result: append the function's return type
    2927             :  * to the specified buffer.
    2928             :  */
    2929             : static void
    2930        1912 : print_function_rettype(StringInfo buf, HeapTuple proctup)
    2931             : {
    2932        1912 :     Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
    2933        1912 :     int         ntabargs = 0;
    2934             :     StringInfoData rbuf;
    2935             : 
    2936        1912 :     initStringInfo(&rbuf);
    2937             : 
    2938        1912 :     if (proc->proretset)
    2939             :     {
    2940             :         /* It might be a table function; try to print the arguments */
    2941         174 :         appendStringInfoString(&rbuf, "TABLE(");
    2942         174 :         ntabargs = print_function_arguments(&rbuf, proctup, true, false);
    2943         174 :         if (ntabargs > 0)
    2944          36 :             appendStringInfoChar(&rbuf, ')');
    2945             :         else
    2946         138 :             resetStringInfo(&rbuf);
    2947             :     }
    2948             : 
    2949        1912 :     if (ntabargs == 0)
    2950             :     {
    2951             :         /* Not a table function, so do the normal thing */
    2952        1876 :         if (proc->proretset)
    2953         138 :             appendStringInfoString(&rbuf, "SETOF ");
    2954        1876 :         appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
    2955             :     }
    2956             : 
    2957        1912 :     appendBinaryStringInfo(buf, rbuf.data, rbuf.len);
    2958        1912 : }
    2959             : 
    2960             : /*
    2961             :  * Common code for pg_get_function_arguments and pg_get_function_result:
    2962             :  * append the desired subset of arguments to buf.  We print only TABLE
    2963             :  * arguments when print_table_args is true, and all the others when it's false.
    2964             :  * We print argument defaults only if print_defaults is true.
    2965             :  * Function return value is the number of arguments printed.
    2966             :  */
    2967             : static int
    2968        4708 : print_function_arguments(StringInfo buf, HeapTuple proctup,
    2969             :                          bool print_table_args, bool print_defaults)
    2970             : {
    2971        4708 :     Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
    2972             :     int         numargs;
    2973             :     Oid        *argtypes;
    2974             :     char      **argnames;
    2975             :     char       *argmodes;
    2976        4708 :     int         insertorderbyat = -1;
    2977             :     int         argsprinted;
    2978             :     int         inputargno;
    2979             :     int         nlackdefaults;
    2980        4708 :     List       *argdefaults = NIL;
    2981        4708 :     ListCell   *nextargdefault = NULL;
    2982             :     int         i;
    2983             : 
    2984        4708 :     numargs = get_func_arg_info(proctup,
    2985             :                                 &argtypes, &argnames, &argmodes);
    2986             : 
    2987        4708 :     nlackdefaults = numargs;
    2988        4708 :     if (print_defaults && proc->pronargdefaults > 0)
    2989             :     {
    2990             :         Datum       proargdefaults;
    2991             :         bool        isnull;
    2992             : 
    2993          20 :         proargdefaults = SysCacheGetAttr(PROCOID, proctup,
    2994             :                                          Anum_pg_proc_proargdefaults,
    2995             :                                          &isnull);
    2996          20 :         if (!isnull)
    2997             :         {
    2998             :             char       *str;
    2999             : 
    3000          20 :             str = TextDatumGetCString(proargdefaults);
    3001          20 :             argdefaults = castNode(List, stringToNode(str));
    3002          20 :             pfree(str);
    3003          20 :             nextargdefault = list_head(argdefaults);
    3004             :             /* nlackdefaults counts only *input* arguments lacking defaults */
    3005          20 :             nlackdefaults = proc->pronargs - list_length(argdefaults);
    3006             :         }
    3007             :     }
    3008             : 
    3009             :     /* Check for special treatment of ordered-set aggregates */
    3010        4708 :     if (proc->prokind == PROKIND_AGGREGATE)
    3011             :     {
    3012             :         HeapTuple   aggtup;
    3013             :         Form_pg_aggregate agg;
    3014             : 
    3015         724 :         aggtup = SearchSysCache1(AGGFNOID, proc->oid);
    3016         724 :         if (!HeapTupleIsValid(aggtup))
    3017           0 :             elog(ERROR, "cache lookup failed for aggregate %u",
    3018             :                  proc->oid);
    3019         724 :         agg = (Form_pg_aggregate) GETSTRUCT(aggtup);
    3020         724 :         if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
    3021          32 :             insertorderbyat = agg->aggnumdirectargs;
    3022         724 :         ReleaseSysCache(aggtup);
    3023             :     }
    3024             : 
    3025        4708 :     argsprinted = 0;
    3026        4708 :     inputargno = 0;
    3027        9056 :     for (i = 0; i < numargs; i++)
    3028             :     {
    3029        4348 :         Oid         argtype = argtypes[i];
    3030        4348 :         char       *argname = argnames ? argnames[i] : NULL;
    3031        4348 :         char        argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
    3032             :         const char *modename;
    3033             :         bool        isinput;
    3034             : 
    3035        4348 :         switch (argmode)
    3036             :         {
    3037        3936 :             case PROARGMODE_IN:
    3038        3936 :                 modename = "";
    3039        3936 :                 isinput = true;
    3040        3936 :                 break;
    3041          12 :             case PROARGMODE_INOUT:
    3042          12 :                 modename = "INOUT ";
    3043          12 :                 isinput = true;
    3044          12 :                 break;
    3045         160 :             case PROARGMODE_OUT:
    3046         160 :                 modename = "OUT ";
    3047         160 :                 isinput = false;
    3048         160 :                 break;
    3049          96 :             case PROARGMODE_VARIADIC:
    3050          96 :                 modename = "VARIADIC ";
    3051          96 :                 isinput = true;
    3052          96 :                 break;
    3053         144 :             case PROARGMODE_TABLE:
    3054         144 :                 modename = "";
    3055         144 :                 isinput = false;
    3056         144 :                 break;
    3057           0 :             default:
    3058           0 :                 elog(ERROR, "invalid parameter mode '%c'", argmode);
    3059             :                 modename = NULL;    /* keep compiler quiet */
    3060             :                 isinput = false;
    3061             :                 break;
    3062             :         }
    3063        4348 :         if (isinput)
    3064        4044 :             inputargno++;       /* this is a 1-based counter */
    3065             : 
    3066        4348 :         if (print_table_args != (argmode == PROARGMODE_TABLE))
    3067         288 :             continue;
    3068             : 
    3069        4060 :         if (argsprinted == insertorderbyat)
    3070             :         {
    3071          32 :             if (argsprinted)
    3072          32 :                 appendStringInfoChar(buf, ' ');
    3073          32 :             appendStringInfoString(buf, "ORDER BY ");
    3074             :         }
    3075        4028 :         else if (argsprinted)
    3076         996 :             appendStringInfoString(buf, ", ");
    3077             : 
    3078        4060 :         appendStringInfoString(buf, modename);
    3079        4060 :         if (argname && argname[0])
    3080         996 :             appendStringInfo(buf, "%s ", quote_identifier(argname));
    3081        4060 :         appendStringInfoString(buf, format_type_be(argtype));
    3082        4060 :         if (print_defaults && isinput && inputargno > nlackdefaults)
    3083             :         {
    3084             :             Node       *expr;
    3085             : 
    3086             :             Assert(nextargdefault != NULL);
    3087          24 :             expr = (Node *) lfirst(nextargdefault);
    3088          24 :             nextargdefault = lnext(argdefaults, nextargdefault);
    3089             : 
    3090          24 :             appendStringInfo(buf, " DEFAULT %s",
    3091             :                              deparse_expression(expr, NIL, false, false));
    3092             :         }
    3093        4060 :         argsprinted++;
    3094             : 
    3095             :         /* nasty hack: print the last arg twice for variadic ordered-set agg */
    3096        4060 :         if (argsprinted == insertorderbyat && i == numargs - 1)
    3097             :         {
    3098          16 :             i--;
    3099             :             /* aggs shouldn't have defaults anyway, but just to be sure ... */
    3100          16 :             print_defaults = false;
    3101             :         }
    3102             :     }
    3103             : 
    3104        4708 :     return argsprinted;
    3105             : }
    3106             : 
    3107             : static bool
    3108          64 : is_input_argument(int nth, const char *argmodes)
    3109             : {
    3110             :     return (!argmodes
    3111          28 :             || argmodes[nth] == PROARGMODE_IN
    3112          12 :             || argmodes[nth] == PROARGMODE_INOUT
    3113          92 :             || argmodes[nth] == PROARGMODE_VARIADIC);
    3114             : }
    3115             : 
    3116             : /*
    3117             :  * Append used transformed types to specified buffer
    3118             :  */
    3119             : static void
    3120          24 : print_function_trftypes(StringInfo buf, HeapTuple proctup)
    3121             : {
    3122             :     Oid        *trftypes;
    3123             :     int         ntypes;
    3124             : 
    3125          24 :     ntypes = get_func_trftypes(proctup, &trftypes);
    3126          24 :     if (ntypes > 0)
    3127             :     {
    3128             :         int         i;
    3129             : 
    3130           0 :         appendStringInfoString(buf, "\n TRANSFORM ");
    3131           0 :         for (i = 0; i < ntypes; i++)
    3132             :         {
    3133           0 :             if (i != 0)
    3134           0 :                 appendStringInfoString(buf, ", ");
    3135           0 :             appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
    3136             :         }
    3137             :     }
    3138          24 : }
    3139             : 
    3140             : /*
    3141             :  * Get textual representation of a function argument's default value.  The
    3142             :  * second argument of this function is the argument number among all arguments
    3143             :  * (i.e. proallargtypes, *not* proargtypes), starting with 1, because that's
    3144             :  * how information_schema.sql uses it.
    3145             :  */
    3146             : Datum
    3147          36 : pg_get_function_arg_default(PG_FUNCTION_ARGS)
    3148             : {
    3149          36 :     Oid         funcid = PG_GETARG_OID(0);
    3150          36 :     int32       nth_arg = PG_GETARG_INT32(1);
    3151             :     HeapTuple   proctup;
    3152             :     Form_pg_proc proc;
    3153             :     int         numargs;
    3154             :     Oid        *argtypes;
    3155             :     char      **argnames;
    3156             :     char       *argmodes;
    3157             :     int         i;
    3158             :     List       *argdefaults;
    3159             :     Node       *node;
    3160             :     char       *str;
    3161             :     int         nth_inputarg;
    3162             :     Datum       proargdefaults;
    3163             :     bool        isnull;
    3164             :     int         nth_default;
    3165             : 
    3166          36 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    3167          36 :     if (!HeapTupleIsValid(proctup))
    3168           8 :         PG_RETURN_NULL();
    3169             : 
    3170          28 :     numargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
    3171          28 :     if (nth_arg < 1 || nth_arg > numargs || !is_input_argument(nth_arg - 1, argmodes))
    3172             :     {
    3173           8 :         ReleaseSysCache(proctup);
    3174           8 :         PG_RETURN_NULL();
    3175             :     }
    3176             : 
    3177          20 :     nth_inputarg = 0;
    3178          56 :     for (i = 0; i < nth_arg; i++)
    3179          36 :         if (is_input_argument(i, argmodes))
    3180          32 :             nth_inputarg++;
    3181             : 
    3182          20 :     proargdefaults = SysCacheGetAttr(PROCOID, proctup,
    3183             :                                      Anum_pg_proc_proargdefaults,
    3184             :                                      &isnull);
    3185          20 :     if (isnull)
    3186             :     {
    3187           0 :         ReleaseSysCache(proctup);
    3188           0 :         PG_RETURN_NULL();
    3189             :     }
    3190             : 
    3191          20 :     str = TextDatumGetCString(proargdefaults);
    3192          20 :     argdefaults = castNode(List, stringToNode(str));
    3193          20 :     pfree(str);
    3194             : 
    3195          20 :     proc = (Form_pg_proc) GETSTRUCT(proctup);
    3196             : 
    3197             :     /*
    3198             :      * Calculate index into proargdefaults: proargdefaults corresponds to the
    3199             :      * last N input arguments, where N = pronargdefaults.
    3200             :      */
    3201          20 :     nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
    3202             : 
    3203          20 :     if (nth_default < 0 || nth_default >= list_length(argdefaults))
    3204             :     {
    3205           4 :         ReleaseSysCache(proctup);
    3206           4 :         PG_RETURN_NULL();
    3207             :     }
    3208          16 :     node = list_nth(argdefaults, nth_default);
    3209          16 :     str = deparse_expression(node, NIL, false, false);
    3210             : 
    3211          16 :     ReleaseSysCache(proctup);
    3212             : 
    3213          16 :     PG_RETURN_TEXT_P(string_to_text(str));
    3214             : }
    3215             : 
    3216             : 
    3217             : /*
    3218             :  * deparse_expression           - General utility for deparsing expressions
    3219             :  *
    3220             :  * calls deparse_expression_pretty with all prettyPrinting disabled
    3221             :  */
    3222             : char *
    3223       33250 : deparse_expression(Node *expr, List *dpcontext,
    3224             :                    bool forceprefix, bool showimplicit)
    3225             : {
    3226       33250 :     return deparse_expression_pretty(expr, dpcontext, forceprefix,
    3227             :                                      showimplicit, 0, 0);
    3228             : }
    3229             : 
    3230             : /* ----------
    3231             :  * deparse_expression_pretty    - General utility for deparsing expressions
    3232             :  *
    3233             :  * expr is the node tree to be deparsed.  It must be a transformed expression
    3234             :  * tree (ie, not the raw output of gram.y).
    3235             :  *
    3236             :  * dpcontext is a list of deparse_namespace nodes representing the context
    3237             :  * for interpreting Vars in the node tree.  It can be NIL if no Vars are
    3238             :  * expected.
    3239             :  *
    3240             :  * forceprefix is true to force all Vars to be prefixed with their table names.
    3241             :  *
    3242             :  * showimplicit is true to force all implicit casts to be shown explicitly.
    3243             :  *
    3244             :  * Tries to pretty up the output according to prettyFlags and startIndent.
    3245             :  *
    3246             :  * The result is a palloc'd string.
    3247             :  * ----------
    3248             :  */
    3249             : static char *
    3250       38724 : deparse_expression_pretty(Node *expr, List *dpcontext,
    3251             :                           bool forceprefix, bool showimplicit,
    3252             :                           int prettyFlags, int startIndent)
    3253             : {
    3254             :     StringInfoData buf;
    3255             :     deparse_context context;
    3256             : 
    3257       38724 :     initStringInfo(&buf);
    3258       38724 :     context.buf = &buf;
    3259       38724 :     context.namespaces = dpcontext;
    3260       38724 :     context.windowClause = NIL;
    3261       38724 :     context.windowTList = NIL;
    3262       38724 :     context.varprefix = forceprefix;
    3263       38724 :     context.prettyFlags = prettyFlags;
    3264       38724 :     context.wrapColumn = WRAP_COLUMN_DEFAULT;
    3265       38724 :     context.indentLevel = startIndent;
    3266       38724 :     context.special_exprkind = EXPR_KIND_NONE;
    3267       38724 :     context.appendparents = NULL;
    3268             : 
    3269       38724 :     get_rule_expr(expr, &context, showimplicit);
    3270             : 
    3271       38724 :     return buf.data;
    3272             : }
    3273             : 
    3274             : /* ----------
    3275             :  * deparse_context_for          - Build deparse context for a single relation
    3276             :  *
    3277             :  * Given the reference name (alias) and OID of a relation, build deparsing
    3278             :  * context for an expression referencing only that relation (as varno 1,
    3279             :  * varlevelsup 0).  This is sufficient for many uses of deparse_expression.
    3280             :  * ----------
    3281             :  */
    3282             : List *
    3283        9960 : deparse_context_for(const char *aliasname, Oid relid)
    3284             : {
    3285             :     deparse_namespace *dpns;
    3286             :     RangeTblEntry *rte;
    3287             : 
    3288        9960 :     dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
    3289             : 
    3290             :     /* Build a minimal RTE for the rel */
    3291        9960 :     rte = makeNode(RangeTblEntry);
    3292        9960 :     rte->rtekind = RTE_RELATION;
    3293        9960 :     rte->relid = relid;
    3294        9960 :     rte->relkind = RELKIND_RELATION; /* no need for exactness here */
    3295        9960 :     rte->rellockmode = AccessShareLock;
    3296        9960 :     rte->alias = makeAlias(aliasname, NIL);
    3297        9960 :     rte->eref = rte->alias;
    3298        9960 :     rte->lateral = false;
    3299        9960 :     rte->inh = false;
    3300        9960 :     rte->inFromCl = true;
    3301             : 
    3302             :     /* Build one-element rtable */
    3303        9960 :     dpns->rtable = list_make1(rte);
    3304        9960 :     dpns->subplans = NIL;
    3305        9960 :     dpns->ctes = NIL;
    3306        9960 :     dpns->appendrels = NULL;
    3307        9960 :     set_rtable_names(dpns, NIL, NULL);
    3308        9960 :     set_simple_column_names(dpns);
    3309             : 
    3310             :     /* Return a one-deep namespace stack */
    3311        9960 :     return list_make1(dpns);
    3312             : }
    3313             : 
    3314             : /*
    3315             :  * deparse_context_for_plan_tree - Build deparse context for a Plan tree
    3316             :  *
    3317             :  * When deparsing an expression in a Plan tree, we use the plan's rangetable
    3318             :  * to resolve names of simple Vars.  The initialization of column names for
    3319             :  * this is rather expensive if the rangetable is large, and it'll be the same
    3320             :  * for every expression in the Plan tree; so we do it just once and re-use
    3321             :  * the result of this function for each expression.  (Note that the result
    3322             :  * is not usable until set_deparse_context_plan() is applied to it.)
    3323             :  *
    3324             :  * In addition to the PlannedStmt, pass the per-RTE alias names
    3325             :  * assigned by a previous call to select_rtable_names_for_explain.
    3326             :  */
    3327             : List *
    3328       10776 : deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
    3329             : {
    3330             :     deparse_namespace *dpns;
    3331             : 
    3332       10776 :     dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
    3333             : 
    3334             :     /* Initialize fields that stay the same across the whole plan tree */
    3335       10776 :     dpns->rtable = pstmt->rtable;
    3336       10776 :     dpns->rtable_names = rtable_names;
    3337       10776 :     dpns->subplans = pstmt->subplans;
    3338       10776 :     dpns->ctes = NIL;
    3339       10776 :     if (pstmt->appendRelations)
    3340             :     {
    3341             :         /* Set up the array, indexed by child relid */
    3342        1956 :         int         ntables = list_length(dpns->rtable);
    3343             :         ListCell   *lc;
    3344             : 
    3345        1956 :         dpns->appendrels = (AppendRelInfo **)
    3346        1956 :             palloc0((ntables + 1) * sizeof(AppendRelInfo *));
    3347       11434 :         foreach(lc, pstmt->appendRelations)
    3348             :         {
    3349        9478 :             AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
    3350        9478 :             Index       crelid = appinfo->child_relid;
    3351             : 
    3352             :             Assert(crelid > 0 && crelid <= ntables);
    3353             :             Assert(dpns->appendrels[crelid] == NULL);
    3354        9478 :             dpns->appendrels[crelid] = appinfo;
    3355             :         }
    3356             :     }
    3357             :     else
    3358        8820 :         dpns->appendrels = NULL; /* don't need it */
    3359             : 
    3360             :     /*
    3361             :      * Set up column name aliases.  We will get rather bogus results for join
    3362             :      * RTEs, but that doesn't matter because plan trees don't contain any join
    3363             :      * alias Vars.
    3364             :      */
    3365       10776 :     set_simple_column_names(dpns);
    3366             : 
    3367             :     /* Return a one-deep namespace stack */
    3368       10776 :     return list_make1(dpns);
    3369             : }
    3370             : 
    3371             : /*
    3372             :  * set_deparse_context_plan - Specify Plan node containing expression
    3373             :  *
    3374             :  * When deparsing an expression in a Plan tree, we might have to resolve
    3375             :  * OUTER_VAR, INNER_VAR, or INDEX_VAR references.  To do this, the caller must
    3376             :  * provide the parent Plan node.  Then OUTER_VAR and INNER_VAR references
    3377             :  * can be resolved by drilling down into the left and right child plans.
    3378             :  * Similarly, INDEX_VAR references can be resolved by reference to the
    3379             :  * indextlist given in a parent IndexOnlyScan node, or to the scan tlist in
    3380             :  * ForeignScan and CustomScan nodes.  (Note that we don't currently support
    3381             :  * deparsing of indexquals in regular IndexScan or BitmapIndexScan nodes;
    3382             :  * for those, we can only deparse the indexqualorig fields, which won't
    3383             :  * contain INDEX_VAR Vars.)
    3384             :  *
    3385             :  * The ancestors list is a list of the Plan's parent Plan and SubPlan nodes,
    3386             :  * the most-closely-nested first.  This is needed to resolve PARAM_EXEC
    3387             :  * Params.  Note we assume that all the Plan nodes share the same rtable.
    3388             :  *
    3389             :  * Once this function has been called, deparse_expression() can be called on
    3390             :  * subsidiary expression(s) of the specified Plan node.  To deparse
    3391             :  * expressions of a different Plan node in the same Plan tree, re-call this
    3392             :  * function to identify the new parent Plan node.
    3393             :  *
    3394             :  * The result is the same List passed in; this is a notational convenience.
    3395             :  */
    3396             : List *
    3397       24562 : set_deparse_context_plan(List *dpcontext, Plan *plan, List *ancestors)
    3398             : {
    3399             :     deparse_namespace *dpns;
    3400             : 
    3401             :     /* Should always have one-entry namespace list for Plan deparsing */
    3402             :     Assert(list_length(dpcontext) == 1);
    3403       24562 :     dpns = (deparse_namespace *) linitial(dpcontext);
    3404             : 
    3405             :     /* Set our attention on the specific plan node passed in */
    3406       24562 :     set_deparse_plan(dpns, plan);
    3407       24562 :     dpns->ancestors = ancestors;
    3408             : 
    3409       24562 :     return dpcontext;
    3410             : }
    3411             : 
    3412             : /*
    3413             :  * select_rtable_names_for_explain  - Select RTE aliases for EXPLAIN
    3414             :  *
    3415             :  * Determine the relation aliases we'll use during an EXPLAIN operation.
    3416             :  * This is just a frontend to set_rtable_names.  We have to expose the aliases
    3417             :  * to EXPLAIN because EXPLAIN needs to know the right alias names to print.
    3418             :  */
    3419             : List *
    3420       10776 : select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
    3421             : {
    3422             :     deparse_namespace dpns;
    3423             : 
    3424       10776 :     memset(&dpns, 0, sizeof(dpns));
    3425       10776 :     dpns.rtable = rtable;
    3426       10776 :     dpns.subplans = NIL;
    3427       10776 :     dpns.ctes = NIL;
    3428       10776 :     dpns.appendrels = NULL;
    3429       10776 :     set_rtable_names(&dpns, NIL, rels_used);
    3430             :     /* We needn't bother computing column aliases yet */
    3431             : 
    3432       10776 :     return dpns.rtable_names;
    3433             : }
    3434             : 
    3435             : /*
    3436             :  * set_rtable_names: select RTE aliases to be used in printing a query
    3437             :  *
    3438             :  * We fill in dpns->rtable_names with a list of names that is one-for-one with
    3439             :  * the already-filled dpns->rtable list.  Each RTE name is unique among those
    3440             :  * in the new namespace plus any ancestor namespaces listed in
    3441             :  * parent_namespaces.
    3442             :  *
    3443             :  * If rels_used isn't NULL, only RTE indexes listed in it are given aliases.
    3444             :  *
    3445             :  * Note that this function is only concerned with relation names, not column
    3446             :  * names.
    3447             :  */
    3448             : static void
    3449       23642 : set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
    3450             :                  Bitmapset *rels_used)
    3451             : {
    3452             :     HASHCTL     hash_ctl;
    3453             :     HTAB       *names_hash;
    3454             :     NameHashEntry *hentry;
    3455             :     bool        found;
    3456             :     int         rtindex;
    3457             :     ListCell   *lc;
    3458             : 
    3459       23642 :     dpns->rtable_names = NIL;
    3460             :     /* nothing more to do if empty rtable */
    3461       23642 :     if (dpns->rtable == NIL)
    3462          38 :         return;
    3463             : 
    3464             :     /*
    3465             :      * We use a hash table to hold known names, so that this process is O(N)
    3466             :      * not O(N^2) for N names.
    3467             :      */
    3468      306852 :     MemSet(&hash_ctl, 0, sizeof(hash_ctl));
    3469       23604 :     hash_ctl.keysize = NAMEDATALEN;
    3470       23604 :     hash_ctl.entrysize = sizeof(NameHashEntry);
    3471       23604 :     hash_ctl.hcxt = CurrentMemoryContext;
    3472       23604 :     names_hash = hash_create("set_rtable_names names",
    3473       23604 :                              list_length(dpns->rtable),
    3474             :                              &hash_ctl,
    3475             :                              HASH_ELEM | HASH_CONTEXT);
    3476             :     /* Preload the hash table with names appearing in parent_namespaces */
    3477       24230 :     foreach(lc, parent_namespaces)
    3478             :     {
    3479         626 :         deparse_namespace *olddpns = (deparse_namespace *) lfirst(lc);
    3480             :         ListCell   *lc2;
    3481             : 
    3482        3350 :         foreach(lc2, olddpns->rtable_names)
    3483             :         {
    3484        2724 :             char       *oldname = (char *) lfirst(lc2);
    3485             : 
    3486        2724 :             if (oldname == NULL)
    3487         116 :                 continue;
    3488        2608 :             hentry = (NameHashEntry *) hash_search(names_hash,
    3489             :                                                    oldname,
    3490             :                                                    HASH_ENTER,
    3491             :                                                    &found);
    3492             :             /* we do not complain about duplicate names in parent namespaces */
    3493        2608 :             hentry->counter = 0;
    3494             :         }
    3495             :     }
    3496             : 
    3497             :     /* Now we can scan the rtable */
    3498       23604 :     rtindex = 1;
    3499       72558 :     foreach(lc, dpns->rtable)
    3500             :     {
    3501       48954 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
    3502             :         char       *refname;
    3503             : 
    3504             :         /* Just in case this takes an unreasonable amount of time ... */
    3505       48954 :         CHECK_FOR_INTERRUPTS();
    3506             : 
    3507       48954 :         if (rels_used && !bms_is_member(rtindex, rels_used))
    3508             :         {
    3509             :             /* Ignore unreferenced RTE */
    3510        7864 :             refname = NULL;
    3511             :         }
    3512       41090 :         else if (rte->alias)
    3513             :         {
    3514             :             /* If RTE has a user-defined alias, prefer that */
    3515       28852 :             refname = rte->alias->aliasname;
    3516             :         }
    3517       12238 :         else if (rte->rtekind == RTE_RELATION)
    3518             :         {
    3519             :             /* Use the current actual name of the relation */
    3520       10674 :             refname = get_rel_name(rte->relid);
    3521             :         }
    3522        1564 :         else if (rte->rtekind == RTE_JOIN)
    3523             :         {
    3524             :             /* Unnamed join has no refname */
    3525         592 :             refname = NULL;
    3526             :         }
    3527             :         else
    3528             :         {
    3529             :             /* Otherwise use whatever the parser assigned */
    3530         972 :             refname = rte->eref->aliasname;
    3531             :         }
    3532             : 
    3533             :         /*
    3534             :          * If the selected name isn't unique, append digits to make it so, and
    3535             :          * make a new hash entry for it once we've got a unique name.  For a
    3536             :          * very long input name, we might have to truncate to stay within
    3537             :          * NAMEDATALEN.
    3538             :          */
    3539       48954 :         if (refname)
    3540             :         {
    3541       40498 :             hentry = (NameHashEntry *) hash_search(names_hash,
    3542             :                                                    refname,
    3543             :                                                    HASH_ENTER,
    3544             :                                                    &found);
    3545       40498 :             if (found)
    3546             :             {
    3547             :                 /* Name already in use, must choose a new one */
    3548        7860 :                 int         refnamelen = strlen(refname);
    3549        7860 :                 char       *modname = (char *) palloc(refnamelen + 16);
    3550             :                 NameHashEntry *hentry2;
    3551             : 
    3552             :                 do
    3553             :                 {
    3554        7860 :                     hentry->counter++;
    3555             :                     for (;;)
    3556             :                     {
    3557           8 :                         memcpy(modname, refname, refnamelen);
    3558        7868 :                         sprintf(modname + refnamelen, "_%d", hentry->counter);
    3559        7868 :                         if (strlen(modname) < NAMEDATALEN)
    3560        7860 :                             break;
    3561             :                         /* drop chars from refname to keep all the digits */
    3562           8 :                         refnamelen = pg_mbcliplen(refname, refnamelen,
    3563             :                                                   refnamelen - 1);
    3564             :                     }
    3565        7860 :                     hentry2 = (NameHashEntry *) hash_search(names_hash,
    3566             :                                                             modname,
    3567             :                                                             HASH_ENTER,
    3568             :                                                             &found);
    3569        7860 :                 } while (found);
    3570        7860 :                 hentry2->counter = 0;    /* init new hash entry */
    3571        7860 :                 refname = modname;
    3572             :             }
    3573             :             else
    3574             :             {
    3575             :                 /* Name not previously used, need only initialize hentry */
    3576       32638 :                 hentry->counter = 0;
    3577             :             }
    3578             :         }
    3579             : 
    3580       48954 :         dpns->rtable_names = lappend(dpns->rtable_names, refname);
    3581       48954 :         rtindex++;
    3582             :     }
    3583             : 
    3584       23604 :     hash_destroy(names_hash);
    3585             : }
    3586             : 
    3587             : /*
    3588             :  * set_deparse_for_query: set up deparse_namespace for deparsing a Query tree
    3589             :  *
    3590             :  * For convenience, this is defined to initialize the deparse_namespace struct
    3591             :  * from scratch.
    3592             :  */
    3593             : static void
    3594        2814 : set_deparse_for_query(deparse_namespace *dpns, Query *query,
    3595             :                       List *parent_namespaces)
    3596             : {
    3597             :     ListCell   *lc;
    3598             :     ListCell   *lc2;
    3599             : 
    3600             :     /* Initialize *dpns and fill rtable/ctes links */
    3601        2814 :     memset(dpns, 0, sizeof(deparse_namespace));
    3602        2814 :     dpns->rtable = query->rtable;
    3603        2814 :     dpns->subplans = NIL;
    3604        2814 :     dpns->ctes = query->cteList;
    3605        2814 :     dpns->appendrels = NULL;
    3606             : 
    3607             :     /* Assign a unique relation alias to each RTE */
    3608        2814 :     set_rtable_names(dpns, parent_namespaces, NULL);
    3609             : 
    3610             :     /* Initialize dpns->rtable_columns to contain zeroed structs */
    3611        2814 :     dpns->rtable_columns = NIL;
    3612       11362 :     while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
    3613        8548 :         dpns->rtable_columns = lappend(dpns->rtable_columns,
    3614             :                                        palloc0(sizeof(deparse_columns)));
    3615             : 
    3616             :     /* If it's a utility query, it won't have a jointree */
    3617        2814 :     if (query->jointree)
    3618             :     {
    3619             :         /* Detect whether global uniqueness of USING names is needed */
    3620        2800 :         dpns->unique_using =
    3621        2800 :             has_dangerous_join_using(dpns, (Node *) query->jointree);
    3622             : 
    3623             :         /*
    3624             :          * Select names for columns merged by USING, via a recursive pass over
    3625             :          * the query jointree.
    3626             :          */
    3627        2800 :         set_using_names(dpns, (Node *) query->jointree, NIL);
    3628             :     }
    3629             : 
    3630             :     /*
    3631             :      * Now assign remaining column aliases for each RTE.  We do this in a
    3632             :      * linear scan of the rtable, so as to process RTEs whether or not they
    3633             :      * are in the jointree (we mustn't miss NEW.*, INSERT target relations,
    3634             :      * etc).  JOIN RTEs must be processed after their children, but this is
    3635             :      * okay because they appear later in the rtable list than their children
    3636             :      * (cf Asserts in identify_join_columns()).
    3637             :      */
    3638       11362 :     forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
    3639             :     {
    3640        8548 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
    3641        8548 :         deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
    3642             : 
    3643        8548 :         if (rte->rtekind == RTE_JOIN)
    3644         624 :             set_join_column_names(dpns, rte, colinfo);
    3645             :         else
    3646        7924 :             set_relation_column_names(dpns, rte, colinfo);
    3647             :     }
    3648        2814 : }
    3649             : 
    3650             : /*
    3651             :  * set_simple_column_names: fill in column aliases for non-query situations
    3652             :  *
    3653             :  * This handles EXPLAIN and cases where we only have relation RTEs.  Without
    3654             :  * a join tree, we can't do anything smart about join RTEs, but we don't
    3655             :  * need to (note that EXPLAIN should never see join alias Vars anyway).
    3656             :  * If we do hit a join RTE we'll just process it like a non-table base RTE.
    3657             :  */
    3658             : static void
    3659       20828 : set_simple_column_names(deparse_namespace *dpns)
    3660             : {
    3661             :     ListCell   *lc;
    3662             :     ListCell   *lc2;
    3663             : 
    3664             :     /* Initialize dpns->rtable_columns to contain zeroed structs */
    3665       20828 :     dpns->rtable_columns = NIL;
    3666       61234 :     while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
    3667       40406 :         dpns->rtable_columns = lappend(dpns->rtable_columns,
    3668             :                                        palloc0(sizeof(deparse_columns)));
    3669             : 
    3670             :     /* Assign unique column aliases within each RTE */
    3671       61234 :     forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
    3672             :     {
    3673       40406 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
    3674       40406 :         deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
    3675             : 
    3676       40406 :         set_relation_column_names(dpns, rte, colinfo);
    3677             :     }
    3678       20828 : }
    3679             : 
    3680             : /*
    3681             :  * has_dangerous_join_using: search jointree for unnamed JOIN USING
    3682             :  *
    3683             :  * Merged columns of a JOIN USING may act differently from either of the input
    3684             :  * columns, either because they are merged with COALESCE (in a FULL JOIN) or
    3685             :  * because an implicit coercion of the underlying input column is required.
    3686             :  * In such a case the column must be referenced as a column of the JOIN not as
    3687             :  * a column of either input.  And this is problematic if the join is unnamed
    3688             :  * (alias-less): we cannot qualify the column's name with an RTE name, since
    3689             :  * there is none.  (Forcibly assigning an alias to the join is not a solution,
    3690             :  * since that will prevent legal references to tables below the join.)
    3691             :  * To ensure that every column in the query is unambiguously referenceable,
    3692             :  * we must assign such merged columns names that are globally unique across
    3693             :  * the whole query, aliasing other columns out of the way as necessary.
    3694             :  *
    3695             :  * Because the ensuing re-aliasing is fairly damaging to the readability of
    3696             :  * the query, we don't do this unless we have to.  So, we must pre-scan
    3697             :  * the join tree to see if we have to, before starting set_using_names().
    3698             :  */
    3699             : static bool
    3700        6380 : has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode)
    3701             : {
    3702        6380 :     if (IsA(jtnode, RangeTblRef))
    3703             :     {
    3704             :         /* nothing to do here */
    3705             :     }
    3706        3380 :     else if (IsA(jtnode, FromExpr))
    3707             :     {
    3708        2800 :         FromExpr   *f = (FromExpr *) jtnode;
    3709             :         ListCell   *lc;
    3710             : 
    3711        5268 :         foreach(lc, f->fromlist)
    3712             :         {
    3713        2516 :             if (has_dangerous_join_using(dpns, (Node *) lfirst(lc)))
    3714          48 :                 return true;
    3715             :         }
    3716             :     }
    3717         580 :     else if (IsA(jtnode, JoinExpr))
    3718             :     {
    3719         580 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3720             : 
    3721             :         /* Is it an unnamed JOIN with USING? */
    3722         580 :         if (j->alias == NULL && j->usingClause)
    3723             :         {
    3724             :             /*
    3725             :              * Yes, so check each join alias var to see if any of them are not
    3726             :              * simple references to underlying columns.  If so, we have a
    3727             :              * dangerous situation and must pick unique aliases.
    3728             :              */
    3729         180 :             RangeTblEntry *jrte = rt_fetch(j->rtindex, dpns->rtable);
    3730             : 
    3731             :             /* We need only examine the merged columns */
    3732         376 :             for (int i = 0; i < jrte->joinmergedcols; i++)
    3733             :             {
    3734         244 :                 Node       *aliasvar = list_nth(jrte->joinaliasvars, i);
    3735             : 
    3736         244 :                 if (!IsA(aliasvar, Var))
    3737          48 :                     return true;
    3738             :             }
    3739             :         }
    3740             : 
    3741             :         /* Nope, but inspect children */
    3742         532 :         if (has_dangerous_join_using(dpns, j->larg))
    3743           0 :             return true;
    3744         532 :         if (has_dangerous_join_using(dpns, j->rarg))
    3745           0 :             return true;
    3746             :     }
    3747             :     else
    3748           0 :         elog(ERROR, "unrecognized node type: %d",
    3749             :              (int) nodeTag(jtnode));
    3750        6284 :     return false;
    3751             : }
    3752             : 
    3753             : /*
    3754             :  * set_using_names: select column aliases to be used for merged USING columns
    3755             :  *
    3756             :  * We do this during a recursive descent of the query jointree.
    3757             :  * dpns->unique_using must already be set to determine the global strategy.
    3758             :  *
    3759             :  * Column alias info is saved in the dpns->rtable_columns list, which is
    3760             :  * assumed to be filled with pre-zeroed deparse_columns structs.
    3761             :  *
    3762             :  * parentUsing is a list of all USING aliases assigned in parent joins of
    3763             :  * the current jointree node.  (The passed-in list must not be modified.)
    3764             :  */
    3765             : static void
    3766        6592 : set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing)
    3767             : {
    3768        6592 :     if (IsA(jtnode, RangeTblRef))
    3769             :     {
    3770             :         /* nothing to do now */
    3771             :     }
    3772        3424 :     else if (IsA(jtnode, FromExpr))
    3773             :     {
    3774        2800 :         FromExpr   *f = (FromExpr *) jtnode;
    3775             :         ListCell   *lc;
    3776             : 
    3777        5344 :         foreach(lc, f->fromlist)
    3778        2544 :             set_using_names(dpns, (Node *) lfirst(lc), parentUsing);
    3779             :     }
    3780         624 :     else if (IsA(jtnode, JoinExpr))
    3781             :     {
    3782         624 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3783         624 :         RangeTblEntry *rte = rt_fetch(j->rtindex, dpns->rtable);
    3784         624 :         deparse_columns *colinfo = deparse_columns_fetch(j->rtindex, dpns);
    3785             :         int        *leftattnos;
    3786             :         int        *rightattnos;
    3787             :         deparse_columns *leftcolinfo;
    3788             :         deparse_columns *rightcolinfo;
    3789             :         int         i;
    3790             :         ListCell   *lc;
    3791             : 
    3792             :         /* Get info about the shape of the join */
    3793         624 :         identify_join_columns(j, rte, colinfo);
    3794         624 :         leftattnos = colinfo->leftattnos;
    3795         624 :         rightattnos = colinfo->rightattnos;
    3796             : 
    3797             :         /* Look up the not-yet-filled-in child deparse_columns structs */
    3798         624 :         leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
    3799         624 :         rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
    3800             : 
    3801             :         /*
    3802             :          * If this join is unnamed, then we cannot substitute new aliases at
    3803             :          * this level, so any name requirements pushed down to here must be
    3804             :          * pushed down again to the children.
    3805             :          */
    3806         624 :         if (rte->alias == NULL)
    3807             :         {
    3808         652 :             for (i = 0; i < colinfo->num_cols; i++)
    3809             :             {
    3810          92 :                 char       *colname = colinfo->colnames[i];
    3811             : 
    3812          92 :                 if (colname == NULL)
    3813          16 :                     continue;
    3814             : 
    3815             :                 /* Push down to left column, unless it's a system column */
    3816          76 :                 if (leftattnos[i] > 0)
    3817             :                 {
    3818          68 :                     expand_colnames_array_to(leftcolinfo, leftattnos[i]);
    3819          68 :                     leftcolinfo->colnames[leftattnos[i] - 1] = colname;
    3820             :                 }
    3821             : 
    3822             :                 /* Same on the righthand side */
    3823          76 :                 if (rightattnos[i] > 0)
    3824             :                 {
    3825          76 :                     expand_colnames_array_to(rightcolinfo, rightattnos[i]);
    3826          76 :                     rightcolinfo->colnames[rightattnos[i] - 1] = colname;
    3827             :                 }
    3828             :             }
    3829             :         }
    3830             : 
    3831             :         /*
    3832             :          * If there's a USING clause, select the USING column names and push
    3833             :          * those names down to the children.  We have two strategies:
    3834             :          *
    3835             :          * If dpns->unique_using is true, we force all USING names to be
    3836             :          * unique across the whole query level.  In principle we'd only need
    3837             :          * the names of dangerous USING columns to be globally unique, but to
    3838             :          * safely assign all USING names in a single pass, we have to enforce
    3839             :          * the same uniqueness rule for all of them.  However, if a USING
    3840             :          * column's name has been pushed down from the parent, we should use
    3841             :          * it as-is rather than making a uniqueness adjustment.  This is
    3842             :          * necessary when we're at an unnamed join, and it creates no risk of
    3843             :          * ambiguity.  Also, if there's a user-written output alias for a
    3844             :          * merged column, we prefer to use that rather than the input name;
    3845             :          * this simplifies the logic and seems likely to lead to less aliasing
    3846             :          * overall.
    3847             :          *
    3848             :          * If dpns->unique_using is false, we only need USING names to be
    3849             :          * unique within their own join RTE.  We still need to honor
    3850             :          * pushed-down names, though.
    3851             :          *
    3852             :          * Though significantly different in results, these two strategies are
    3853             :          * implemented by the same code, with only the difference of whether
    3854             :          * to put assigned names into dpns->using_names.
    3855             :          */
    3856         624 :         if (j->usingClause)
    3857             :         {
    3858             :             /* Copy the input parentUsing list so we don't modify it */
    3859         264 :             parentUsing = list_copy(parentUsing);
    3860             : 
    3861             :             /* USING names must correspond to the first join output columns */
    3862         264 :             expand_colnames_array_to(colinfo, list_length(j->usingClause));
    3863         264 :             i = 0;
    3864         632 :             foreach(lc, j->usingClause)
    3865             :             {
    3866         368 :                 char       *colname = strVal(lfirst(lc));
    3867             : 
    3868             :                 /* Assert it's a merged column */
    3869             :                 Assert(leftattnos[i] != 0 && rightattnos[i] != 0);
    3870             : 
    3871             :                 /* Adopt passed-down name if any, else select unique name */
    3872         368 :                 if (colinfo->colnames[i] != NULL)
    3873          68 :                     colname = colinfo->colnames[i];
    3874             :                 else
    3875             :                 {
    3876             :                     /* Prefer user-written output alias if any */
    3877         300 :                     if (rte->alias && i < list_length(rte->alias->colnames))
    3878           0 :                         colname = strVal(list_nth(rte->alias->colnames, i));
    3879             :                     /* Make it appropriately unique */
    3880         300 :                     colname = make_colname_unique(colname, dpns, colinfo);
    3881         300 :                     if (dpns->unique_using)
    3882          84 :                         dpns->using_names = lappend(dpns->using_names,
    3883             :                                                     colname);
    3884             :                     /* Save it as output column name, too */
    3885         300 :                     colinfo->colnames[i] = colname;
    3886             :                 }
    3887             : 
    3888             :                 /* Remember selected names for use later */
    3889         368 :                 colinfo->usingNames = lappend(colinfo->usingNames, colname);
    3890         368 :                 parentUsing = lappend(parentUsing, colname);
    3891             : 
    3892             :                 /* Push down to left column, unless it's a system column */
    3893         368 :                 if (leftattnos[i] > 0)
    3894             :                 {
    3895         368 :                     expand_colnames_array_to(leftcolinfo, leftattnos[i]);
    3896         368 :                     leftcolinfo->colnames[leftattnos[i] - 1] = colname;
    3897             :                 }
    3898             : 
    3899             :                 /* Same on the righthand side */
    3900         368 :                 if (rightattnos[i] > 0)
    3901             :                 {
    3902         368 :                     expand_colnames_array_to(rightcolinfo, rightattnos[i]);
    3903         368 :                     rightcolinfo->colnames[rightattnos[i] - 1] = colname;
    3904             :                 }
    3905             : 
    3906         368 :                 i++;
    3907             :             }
    3908             :         }
    3909             : 
    3910             :         /* Mark child deparse_columns structs with correct parentUsing info */
    3911         624 :         leftcolinfo->parentUsing = parentUsing;
    3912         624 :         rightcolinfo->parentUsing = parentUsing;
    3913             : 
    3914             :         /* Now recursively assign USING column names in children */
    3915         624 :         set_using_names(dpns, j->larg, parentUsing);
    3916         624 :         set_using_names(dpns, j->rarg, parentUsing);
    3917             :     }
    3918             :     else
    3919           0 :         elog(ERROR, "unrecognized node type: %d",
    3920             :              (int) nodeTag(jtnode));
    3921        6592 : }
    3922             : 
    3923             : /*
    3924             :  * set_relation_column_names: select column aliases for a non-join RTE
    3925             :  *
    3926             :  * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
    3927             :  * If any colnames entries are already filled in, those override local
    3928             :  * choices.
    3929             :  */
    3930             : static void
    3931       48330 : set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
    3932             :                           deparse_columns *colinfo)
    3933             : {
    3934             :     int         ncolumns;
    3935             :     char      **real_colnames;
    3936             :     bool        changed_any;
    3937             :     int         noldcolumns;
    3938             :     int         i;
    3939             :     int         j;
    3940             : 
    3941             :     /*
    3942             :      * Extract the RTE's "real" column names.  This is comparable to
    3943             :      * get_rte_attribute_name, except that it's important to disregard dropped
    3944             :      * columns.  We put NULL into the array for a dropped column.
    3945             :      */
    3946       48330 :     if (rte->rtekind == RTE_RELATION)
    3947             :     {
    3948             :         /* Relation --- look to the system catalogs for up-to-date info */
    3949             :         Relation    rel;
    3950             :         TupleDesc   tupdesc;
    3951             : 
    3952       41130 :         rel = relation_open(rte->relid, AccessShareLock);
    3953       41130 :         tupdesc = RelationGetDescr(rel);
    3954             : 
    3955       41130 :         ncolumns = tupdesc->natts;
    3956       41130 :         real_colnames = (char **) palloc(ncolumns * sizeof(char *));
    3957             : 
    3958      244266 :         for (i = 0; i < ncolumns; i++)
    3959             :         {
    3960      203136 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
    3961             : 
    3962      203136 :             if (attr->attisdropped)
    3963        1964 :                 real_colnames[i] = NULL;
    3964             :             else
    3965      201172 :                 real_colnames[i] = pstrdup(NameStr(attr->attname));
    3966             :         }
    3967       41130 :         relation_close(rel, AccessShareLock);
    3968             :     }
    3969             :     else
    3970             :     {
    3971             :         /* Otherwise use the column names from eref */
    3972             :         ListCell   *lc;
    3973             : 
    3974        7200 :         ncolumns = list_length(rte->eref->colnames);
    3975        7200 :         real_colnames = (char **) palloc(ncolumns * sizeof(char *));
    3976             : 
    3977        7200 :         i = 0;
    3978       40424 :         foreach(lc, rte->eref->colnames)
    3979             :         {
    3980             :             /*
    3981             :              * If the column name shown in eref is an empty string, then it's
    3982             :              * a column that was dropped at the time of parsing the query, so
    3983             :              * treat it as dropped.
    3984             :              */
    3985       33224 :             char       *cname = strVal(lfirst(lc));
    3986             : 
    3987       33224 :             if (cname[0] == '\0')
    3988          12 :                 cname = NULL;
    3989       33224 :             real_colnames[i] = cname;
    3990       33224 :             i++;
    3991             :         }
    3992             :     }
    3993             : 
    3994             :     /*
    3995             :      * Ensure colinfo->colnames has a slot for each column.  (It could be long
    3996             :      * enough already, if we pushed down a name for the last column.)  Note:
    3997             :      * it's possible that there are now more columns than there were when the
    3998             :      * query was parsed, ie colnames could be longer than rte->eref->colnames.
    3999             :      * We must assign unique aliases to the new columns too, else there could
    4000             :      * be unresolved conflicts when the view/rule is reloaded.
    4001             :      */
    4002       48330 :     expand_colnames_array_to(colinfo, ncolumns);
    4003             :     Assert(colinfo->num_cols == ncolumns);
    4004             : 
    4005             :     /*
    4006             :      * Make sufficiently large new_colnames and is_new_col arrays, too.
    4007             :      *
    4008             :      * Note: because we leave colinfo->num_new_cols zero until after the loop,
    4009             :      * colname_is_unique will not consult that array, which is fine because it
    4010             :      * would only be duplicate effort.
    4011             :      */
    4012       48330 :     colinfo->new_colnames = (char **) palloc(ncolumns * sizeof(char *));
    4013       48330 :     colinfo->is_new_col = (bool *) palloc(ncolumns * sizeof(bool));
    4014             : 
    4015             :     /*
    4016             :      * Scan the columns, select a unique alias for each one, and store it in
    4017             :      * colinfo->colnames and colinfo->new_colnames.  The former array has NULL
    4018             :      * entries for dropped columns, the latter omits them.  Also mark
    4019             :      * new_colnames entries as to whether they are new since parse time; this
    4020             :      * is the case for entries beyond the length of rte->eref->colnames.
    4021             :      */
    4022       48330 :     noldcolumns = list_length(rte->eref->colnames);
    4023       48330 :     changed_any = false;
    4024       48330 :     j = 0;
    4025      284690 :     for (i = 0; i < ncolumns; i++)
    4026             :     {
    4027      236360 :         char       *real_colname = real_colnames[i];
    4028      236360 :         char       *colname = colinfo->colnames[i];
    4029             : 
    4030             :         /* Skip dropped columns */
    4031      236360 :         if (real_colname == NULL)
    4032             :         {
    4033             :             Assert(colname == NULL);    /* colnames[i] is already NULL */
    4034        1976 :             continue;
    4035             :         }
    4036             : 
    4037             :         /* If alias already assigned, that's what to use */
    4038      234384 :         if (colname == NULL)
    4039             :         {
    4040             :             /* If user wrote an alias, prefer that over real column name */
    4041      233716 :             if (rte->alias && i < list_length(rte->alias->colnames))
    4042       24572 :                 colname = strVal(list_nth(rte->alias->colnames, i));
    4043             :             else
    4044      209144 :                 colname = real_colname;
    4045             : 
    4046             :             /* Unique-ify and insert into colinfo */
    4047      233716 :             colname = make_colname_unique(colname, dpns, colinfo);
    4048             : 
    4049      233716 :             colinfo->colnames[i] = colname;
    4050             :         }
    4051             : 
    4052             :         /* Put names of non-dropped columns in new_colnames[] too */
    4053      234384 :         colinfo->new_colnames[j] = colname;
    4054             :         /* And mark them as new or not */
    4055      234384 :         colinfo->is_new_col[j] = (i >= noldcolumns);
    4056      234384 :         j++;
    4057             : 
    4058             :         /* Remember if any assigned aliases differ from "real" name */
    4059      234384 :         if (!changed_any && strcmp(colname, real_colname) != 0)
    4060        2216 :             changed_any = true;
    4061             :     }
    4062             : 
    4063             :     /*
    4064             :      * Set correct length for new_colnames[] array.  (Note: if columns have
    4065             :      * been added, colinfo->num_cols includes them, which is not really quite
    4066             :      * right but is harmless, since any new columns must be at the end where
    4067             :      * they won't affect varattnos of pre-existing columns.)
    4068             :      */
    4069       48330 :     colinfo->num_new_cols = j;
    4070             : 
    4071             :     /*
    4072             :      * For a relation RTE, we need only print the alias column names if any
    4073             :      * are different from the underlying "real" names.  For a function RTE,
    4074             :      * always emit a complete column alias list; this is to protect against
    4075             :      * possible instability of the default column names (eg, from altering
    4076             :      * parameter names).  For tablefunc RTEs, we never print aliases, because
    4077             :      * the column names are part of the clause itself.  For other RTE types,
    4078             :      * print if we changed anything OR if there were user-written column
    4079             :      * aliases (since the latter would be part of the underlying "reality").
    4080             :      */
    4081       48330 :     if (rte->rtekind == RTE_RELATION)
    4082       41130 :         colinfo->printaliases = changed_any;
    4083        7200 :     else if (rte->rtekind == RTE_FUNCTION)
    4084         440 :         colinfo->printaliases = true;
    4085        6760 :     else if (rte->rtekind == RTE_TABLEFUNC)
    4086          36 :         colinfo->printaliases = false;
    4087        6724 :     else if (rte->alias && rte->alias->colnames != NIL)
    4088         376 :         colinfo->printaliases = true;
    4089             :     else
    4090        6348 :         colinfo->printaliases = changed_any;
    4091       48330 : }
    4092             : 
    4093             : /*
    4094             :  * set_join_column_names: select column aliases for a join RTE
    4095             :  *
    4096             :  * Column alias info is saved in *colinfo, which is assumed to be pre-zeroed.
    4097             :  * If any colnames entries are already filled in, those override local
    4098             :  * choices.  Also, names for USING columns were already chosen by
    4099             :  * set_using_names().  We further expect that column alias selection has been
    4100             :  * completed for both input RTEs.
    4101             :  */
    4102             : static void
    4103         624 : set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
    4104             :                       deparse_columns *colinfo)
    4105             : {
    4106             :     deparse_columns *leftcolinfo;
    4107             :     deparse_columns *rightcolinfo;
    4108             :     bool        changed_any;
    4109             :     int         noldcolumns;
    4110             :     int         nnewcolumns;
    4111         624 :     Bitmapset  *leftmerged = NULL;
    4112         624 :     Bitmapset  *rightmerged = NULL;
    4113             :     int         i;
    4114             :     int         j;
    4115             :     int         ic;
    4116             :     int         jc;
    4117             : 
    4118             :     /* Look up the previously-filled-in child deparse_columns structs */
    4119         624 :     leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
    4120         624 :     rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
    4121             : 
    4122             :     /*
    4123             :      * Ensure colinfo->colnames has a slot for each column.  (It could be long
    4124             :      * enough already, if we pushed down a name for the last column.)  Note:
    4125             :      * it's possible that one or both inputs now have more columns than there
    4126             :      * were when the query was parsed, but we'll deal with that below.  We
    4127             :      * only need entries in colnames for pre-existing columns.
    4128             :      */
    4129         624 :     noldcolumns = list_length(rte->eref->colnames);
    4130         624 :     expand_colnames_array_to(colinfo, noldcolumns);
    4131             :     Assert(colinfo->num_cols == noldcolumns);
    4132             : 
    4133             :     /*
    4134             :      * Scan the join output columns, select an alias for each one, and store
    4135             :      * it in colinfo->colnames.  If there are USING columns, set_using_names()
    4136             :      * already selected their names, so we can start the loop at the first
    4137             :      * non-merged column.
    4138             :      */
    4139         624 :     changed_any = false;
    4140       15776 :     for (i = list_length(colinfo->usingNames); i < noldcolumns; i++)
    4141             :     {
    4142       15152 :         char       *colname = colinfo->colnames[i];
    4143             :         char       *real_colname;
    4144             : 
    4145             :         /* Join column must refer to at least one input column */
    4146             :         Assert(colinfo->leftattnos[i] != 0 || colinfo->rightattnos[i] != 0);
    4147             : 
    4148             :         /* Get the child column name */
    4149       15152 :         if (colinfo->leftattnos[i] > 0)
    4150       10460 :             real_colname = leftcolinfo->colnames[colinfo->leftattnos[i] - 1];
    4151        4692 :         else if (colinfo->rightattnos[i] > 0)
    4152        4692 :             real_colname = rightcolinfo->colnames[colinfo->rightattnos[i] - 1];
    4153             :         else
    4154             :         {
    4155             :             /* We're joining system columns --- use eref name */
    4156           0 :             real_colname = strVal(list_nth(rte->eref->colnames, i));
    4157             :         }
    4158             : 
    4159             :         /* If child col has been dropped, no need to assign a join colname */
    4160       15152 :         if (real_colname == NULL)
    4161             :         {
    4162           4 :             colinfo->colnames[i] = NULL;
    4163           4 :             continue;
    4164             :         }
    4165             : 
    4166             :         /* In an unnamed join, just report child column names as-is */
    4167       15148 :         if (rte->alias == NULL)
    4168             :         {
    4169       14912 :             colinfo->colnames[i] = real_colname;
    4170       14912 :             continue;
    4171             :         }
    4172             : 
    4173             :         /* If alias already assigned, that's what to use */
    4174         236 :         if (colname == NULL)
    4175             :         {
    4176             :             /* If user wrote an alias, prefer that over real column name */
    4177         236 :             if (rte->alias && i < list_length(rte->alias->colnames))
    4178          64 :                 colname = strVal(list_nth(rte->alias->colnames, i));
    4179             :             else
    4180         172 :                 colname = real_colname;
    4181             : 
    4182             :             /* Unique-ify and insert into colinfo */
    4183         236 :             colname = make_colname_unique(colname, dpns, colinfo);
    4184             : 
    4185         236 :             colinfo->colnames[i] = colname;
    4186             :         }
    4187             : 
    4188             :         /* Remember if any assigned aliases differ from "real" name */
    4189         236 :         if (!changed_any && strcmp(colname, real_colname) != 0)
    4190          16 :             changed_any = true;
    4191             :     }
    4192             : 
    4193             :     /*
    4194             :      * Calculate number of columns the join would have if it were re-parsed
    4195             :      * now, and create storage for the new_colnames and is_new_col arrays.
    4196             :      *
    4197             :      * Note: colname_is_unique will be consulting new_colnames[] during the
    4198             :      * loops below, so its not-yet-filled entries must be zeroes.
    4199             :      */
    4200        1248 :     nnewcolumns = leftcolinfo->num_new_cols + rightcolinfo->num_new_cols -
    4201         624 :         list_length(colinfo->usingNames);
    4202         624 :     colinfo->num_new_cols = nnewcolumns;
    4203         624 :     colinfo->new_colnames = (char **) palloc0(nnewcolumns * sizeof(char *));
    4204         624 :     colinfo->is_new_col = (bool *) palloc0(nnewcolumns * sizeof(bool));
    4205             : 
    4206             :     /*
    4207             :      * Generating the new_colnames array is a bit tricky since any new columns
    4208             :      * added since parse time must be inserted in the right places.  This code
    4209             :      * must match the parser, which will order a join's columns as merged
    4210             :      * columns first (in USING-clause order), then non-merged columns from the
    4211             :      * left input (in attnum order), then non-merged columns from the right
    4212             :      * input (ditto).  If one of the inputs is itself a join, its columns will
    4213             :      * be ordered according to the same rule, which means newly-added columns
    4214             :      * might not be at the end.  We can figure out what's what by consulting
    4215             :      * the leftattnos and rightattnos arrays plus the input is_new_col arrays.
    4216             :      *
    4217             :      * In these loops, i indexes leftattnos/rightattnos (so it's join varattno
    4218             :      * less one), j indexes new_colnames/is_new_col, and ic/jc have similar
    4219             :      * meanings for the current child RTE.
    4220             :      */
    4221             : 
    4222             :     /* Handle merged columns; they are first and can't be new */
    4223         624 :     i = j = 0;
    4224         992 :     while (i < noldcolumns &&
    4225         992 :            colinfo->leftattnos[i] != 0 &&
    4226         992 :            colinfo->rightattnos[i] != 0)
    4227             :     {
    4228             :         /* column name is already determined and known unique */
    4229         368 :         colinfo->new_colnames[j] = colinfo->colnames[i];
    4230         368 :         colinfo->is_new_col[j] = false;
    4231             : 
    4232             :         /* build bitmapsets of child attnums of merged columns */
    4233         368 :         if (colinfo->leftattnos[i] > 0)
    4234         368 :             leftmerged = bms_add_member(leftmerged, colinfo->leftattnos[i]);
    4235         368 :         if (colinfo->rightattnos[i] > 0)
    4236         368 :             rightmerged = bms_add_member(rightmerged, colinfo->rightattnos[i]);
    4237             : 
    4238         368 :         i++, j++;
    4239             :     }
    4240             : 
    4241             :     /* Handle non-merged left-child columns */
    4242         624 :     ic = 0;
    4243       11776 :     for (jc = 0; jc < leftcolinfo->num_new_cols; jc++)
    4244             :     {
    4245       11152 :         char       *child_colname = leftcolinfo->new_colnames[jc];
    4246             : 
    4247       11152 :         if (!leftcolinfo->is_new_col[jc])
    4248             :         {
    4249             :             /* Advance ic to next non-dropped old column of left child */
    4250       10880 :             while (ic < leftcolinfo->num_cols &&
    4251       10880 :                    leftcolinfo->colnames[ic] == NULL)
    4252          56 :                 ic++;
    4253             :             Assert(ic < leftcolinfo->num_cols);
    4254       10824 :             ic++;
    4255             :             /* If it is a merged column, we already processed it */
    4256       10824 :             if (bms_is_member(ic, leftmerged))
    4257         368 :                 continue;
    4258             :             /* Else, advance i to the corresponding existing join column */
    4259       10460 :             while (i < colinfo->num_cols &&
    4260       10460 :                    colinfo->colnames[i] == NULL)
    4261           4 :                 i++;
    4262             :             Assert(i < colinfo->num_cols);
    4263             :             Assert(ic == colinfo->leftattnos[i]);
    4264             :             /* Use the already-assigned name of this column */
    4265       10456 :             colinfo->new_colnames[j] = colinfo->colnames[i];
    4266       10456 :             i++;
    4267             :         }
    4268             :         else
    4269             :         {
    4270             :             /*
    4271             :              * Unique-ify the new child column name and assign, unless we're
    4272             :              * in an unnamed join, in which case just copy
    4273             :              */
    4274         328 :             if (rte->alias != NULL)
    4275             :             {
    4276         176 :                 colinfo->new_colnames[j] =
    4277          88 :                     make_colname_unique(child_colname, dpns, colinfo);
    4278          88 :                 if (!changed_any &&
    4279          72 :                     strcmp(colinfo->new_colnames[j], child_colname) != 0)
    4280           8 :                     changed_any = true;
    4281             :             }
    4282             :             else
    4283         240 :                 colinfo->new_colnames[j] = child_colname;
    4284             :         }
    4285             : 
    4286       10784 :         colinfo->is_new_col[j] = leftcolinfo->is_new_col[jc];
    4287       10784 :         j++;
    4288             :     }
    4289             : 
    4290             :     /* Handle non-merged right-child columns in exactly the same way */
    4291         624 :     ic = 0;
    4292        5796 :     for (jc = 0; jc < rightcolinfo->num_new_cols; jc++)
    4293             :     {
    4294        5172 :         char       *child_colname = rightcolinfo->new_colnames[jc];
    4295             : 
    4296        5172 :         if (!rightcolinfo->is_new_col[jc])
    4297             :         {
    4298             :             /* Advance ic to next non-dropped old column of right child */
    4299        5060 :             while (ic < rightcolinfo->num_cols &&
    4300        5060 :                    rightcolinfo->colnames[ic] == NULL)
    4301           0 :                 ic++;
    4302             :             Assert(ic < rightcolinfo->num_cols);
    4303        5060 :             ic++;
    4304             :             /* If it is a merged column, we already processed it */
    4305        5060 :             if (bms_is_member(ic, rightmerged))
    4306         368 :                 continue;
    4307             :             /* Else, advance i to the corresponding existing join column */
    4308        4692 :             while (i < colinfo->num_cols &&
    4309        4692 :                    colinfo->colnames[i] == NULL)
    4310           0 :                 i++;
    4311             :             Assert(i < colinfo->num_cols);
    4312             :             Assert(ic == colinfo->rightattnos[i]);
    4313             :             /* Use the already-assigned name of this column */
    4314        4692 :             colinfo->new_colnames[j] = colinfo->colnames[i];
    4315        4692 :             i++;
    4316             :         }
    4317             :         else
    4318             :         {
    4319             :             /*
    4320             :              * Unique-ify the new child column name and assign, unless we're
    4321             :              * in an unnamed join, in which case just copy
    4322             :              */
    4323         112 :             if (rte->alias != NULL)
    4324             :             {
    4325          32 :                 colinfo->new_colnames[j] =
    4326          16 :                     make_colname_unique(child_colname, dpns, colinfo);
    4327          16 :                 if (!changed_any &&
    4328          16 :                     strcmp(colinfo->new_colnames[j], child_colname) != 0)
    4329           8 :                     changed_any = true;
    4330             :             }
    4331             :             else
    4332          96 :                 colinfo->new_colnames[j] = child_colname;
    4333             :         }
    4334             : 
    4335        4804 :         colinfo->is_new_col[j] = rightcolinfo->is_new_col[jc];
    4336        4804 :         j++;
    4337             :     }
    4338             : 
    4339             :     /* Assert we processed the right number of columns */
    4340             : #ifdef USE_ASSERT_CHECKING
    4341             :     while (i < colinfo->num_cols && colinfo->colnames[i] == NULL)
    4342             :         i++;
    4343             :     Assert(i == colinfo->num_cols);
    4344             :     Assert(j == nnewcolumns);
    4345             : #endif
    4346             : 
    4347             :     /*
    4348             :      * For a named join, print column aliases if we changed any from the child
    4349             :      * names.  Unnamed joins cannot print aliases.
    4350             :      */
    4351         624 :     if (rte->alias != NULL)
    4352          64 :         colinfo->printaliases = changed_any;
    4353             :     else
    4354         560 :         colinfo->printaliases = false;
    4355         624 : }
    4356             : 
    4357             : /*
    4358             :  * colname_is_unique: is colname distinct from already-chosen column names?
    4359             :  *
    4360             :  * dpns is query-wide info, colinfo is for the column's RTE
    4361             :  */
    4362             : static bool
    4363      245614 : colname_is_unique(const char *colname, deparse_namespace *dpns,
    4364             :                   deparse_columns *colinfo)
    4365             : {
    4366             :     int         i;
    4367             :     ListCell   *lc;
    4368             : 
    4369             :     /* Check against already-assigned column aliases within RTE */
    4370     3528368 :     for (i = 0; i < colinfo->num_cols; i++)
    4371             :     {
    4372     3293884 :         char       *oldname = colinfo->colnames[i];
    4373             : 
    4374     3293884 :         if (oldname && strcmp(oldname, colname) == 0)
    4375       11130 :             return false;
    4376             :     }
    4377             : 
    4378             :     /*
    4379             :      * If we're building a new_colnames array, check that too (this will be
    4380             :      * partially but not completely redundant with the previous checks)
    4381             :      */
    4382      235332 :     for (i = 0; i < colinfo->num_new_cols; i++)
    4383             :     {
    4384         864 :         char       *oldname = colinfo->new_colnames[i];
    4385             : 
    4386         864 :         if (oldname && strcmp(oldname, colname) == 0)
    4387          16 :             return false;
    4388             :     }
    4389             : 
    4390             :     /* Also check against USING-column names that must be globally unique */
    4391      235548 :     foreach(lc, dpns->using_names)
    4392             :     {
    4393        1188 :         char       *oldname = (char *) lfirst(lc);
    4394             : 
    4395        1188 :         if (strcmp(oldname, colname) == 0)
    4396         108 :             return false;
    4397             :     }
    4398             : 
    4399             :     /* Also check against names already assigned for parent-join USING cols */
    4400      236040 :     foreach(lc, colinfo->parentUsing)
    4401             :     {
    4402        1684 :         char       *oldname = (char *) lfirst(lc);
    4403             : 
    4404        1684 :         if (strcmp(oldname, colname) == 0)
    4405           4 :             return false;
    4406             :     }
    4407             : 
    4408      234356 :     return true;
    4409             : }
    4410             : 
    4411             : /*
    4412             :  * make_colname_unique: modify colname if necessary to make it unique
    4413             :  *
    4414             :  * dpns is query-wide info, colinfo is for the column's RTE
    4415             :  */
    4416             : static char *
    4417      234356 : make_colname_unique(char *colname, deparse_namespace *dpns,
    4418             :                     deparse_columns *colinfo)
    4419             : {
    4420             :     /*
    4421             :      * If the selected name isn't unique, append digits to make it so.  For a
    4422             :      * very long input name, we might have to truncate to stay within
    4423             :      * NAMEDATALEN.
    4424             :      */
    4425      234356 :     if (!colname_is_unique(colname, dpns, colinfo))
    4426             :     {
    4427        9994 :         int         colnamelen = strlen(colname);
    4428        9994 :         char       *modname = (char *) palloc(colnamelen + 16);
    4429        9994 :         int         i = 0;
    4430             : 
    4431             :         do
    4432             :         {
    4433       11258 :             i++;
    4434             :             for (;;)
    4435             :             {
    4436           0 :                 memcpy(modname, colname, colnamelen);
    4437       11258 :                 sprintf(modname + colnamelen, "_%d", i);
    4438       11258 :                 if (strlen(modname) < NAMEDATALEN)
    4439       11258 :                     break;
    4440             :                 /* drop chars from colname to keep all the digits */
    4441           0 :                 colnamelen = pg_mbcliplen(colname, colnamelen,
    4442             :                                           colnamelen - 1);
    4443             :             }
    4444       11258 :         } while (!colname_is_unique(modname, dpns, colinfo));
    4445        9994 :         colname = modname;
    4446             :     }
    4447      234356 :     return colname;
    4448             : }
    4449             : 
    4450             : /*
    4451             :  * expand_colnames_array_to: make colinfo->colnames at least n items long
    4452             :  *
    4453             :  * Any added array entries are initialized to zero.
    4454             :  */
    4455             : static void
    4456       50098 : expand_colnames_array_to(deparse_columns *colinfo, int n)
    4457             : {
    4458       50098 :     if (n > colinfo->num_cols)
    4459             :     {
    4460       48936 :         if (colinfo->colnames == NULL)
    4461       48048 :             colinfo->colnames = (char **) palloc0(n * sizeof(char *));
    4462             :         else
    4463             :         {
    4464         888 :             colinfo->colnames = (char **) repalloc(colinfo->colnames,
    4465             :                                                    n * sizeof(char *));
    4466         888 :             memset(colinfo->colnames + colinfo->num_cols, 0,
    4467         888 :                    (n - colinfo->num_cols) * sizeof(char *));
    4468             :         }
    4469       48936 :         colinfo->num_cols = n;
    4470             :     }
    4471       50098 : }
    4472             : 
    4473             : /*
    4474             :  * identify_join_columns: figure out where columns of a join come from
    4475             :  *
    4476             :  * Fills the join-specific fields of the colinfo struct, except for
    4477             :  * usingNames which is filled later.
    4478             :  */
    4479             : static void
    4480         624 : identify_join_columns(JoinExpr *j, RangeTblEntry *jrte,
    4481             :                       deparse_columns *colinfo)
    4482             : {
    4483             :     int         numjoincols;
    4484             :     int         jcolno;
    4485             :     int         rcolno;
    4486             :     ListCell   *lc;
    4487             : 
    4488             :     /* Extract left/right child RT indexes */
    4489         624 :     if (IsA(j->larg, RangeTblRef))
    4490         420 :         colinfo->leftrti = ((RangeTblRef *) j->larg)->rtindex;
    4491         204 :     else if (IsA(j->larg, JoinExpr))
    4492         204 :         colinfo->leftrti = ((JoinExpr *) j->larg)->rtindex;
    4493             :     else
    4494           0 :         elog(ERROR, "unrecognized node type in jointree: %d",
    4495             :              (int) nodeTag(j->larg));
    4496         624 :     if (IsA(j->rarg, RangeTblRef))
    4497         624 :         colinfo->rightrti = ((RangeTblRef *) j->rarg)->rtindex;
    4498           0 :     else if (IsA(j->rarg, JoinExpr))
    4499           0 :         colinfo->rightrti = ((JoinExpr *) j->rarg)->rtindex;
    4500             :     else
    4501           0 :         elog(ERROR, "unrecognized node type in jointree: %d",
    4502             :              (int) nodeTag(j->rarg));
    4503             : 
    4504             :     /* Assert children will be processed earlier than join in second pass */
    4505             :     Assert(colinfo->leftrti < j->rtindex);
    4506             :     Assert(colinfo->rightrti < j->rtindex);
    4507             : 
    4508             :     /* Initialize result arrays with zeroes */
    4509         624 :     numjoincols = list_length(jrte->joinaliasvars);
    4510             :     Assert(numjoincols == list_length(jrte->eref->colnames));
    4511         624 :     colinfo->leftattnos = (int *) palloc0(numjoincols * sizeof(int));
    4512         624 :     colinfo->rightattnos = (int *) palloc0(numjoincols * sizeof(int));
    4513             : 
    4514             :     /*
    4515             :      * Deconstruct RTE's joinleftcols/joinrightcols into desired format.
    4516             :      * Recall that the column(s) merged due to USING are the first column(s)
    4517             :      * of the join output.  We need not do anything special while scanning
    4518             :      * joinleftcols, but while scanning joinrightcols we must distinguish
    4519             :      * merged from unmerged columns.
    4520             :      */
    4521         624 :     jcolno = 0;
    4522       11452 :     foreach(lc, jrte->joinleftcols)
    4523             :     {
    4524       10828 :         int         leftattno = lfirst_int(lc);
    4525             : 
    4526       10828 :         colinfo->leftattnos[jcolno++] = leftattno;
    4527             :     }
    4528         624 :     rcolno = 0;
    4529        5684 :     foreach(lc, jrte->joinrightcols)
    4530             :     {
    4531        5060 :         int         rightattno = lfirst_int(lc);
    4532             : 
    4533        5060 :         if (rcolno < jrte->joinmergedcols)    /* merged column? */
    4534         368 :             colinfo->rightattnos[rcolno] = rightattno;
    4535             :         else
    4536        4692 :             colinfo->rightattnos[jcolno++] = rightattno;
    4537        5060 :         rcolno++;
    4538             :     }
    4539             :     Assert(jcolno == numjoincols);
    4540         624 : }
    4541             : 
    4542             : /*
    4543             :  * get_rtable_name: convenience function to get a previously assigned RTE alias
    4544             :  *
    4545             :  * The RTE must belong to the topmost namespace level in "context".
    4546             :  */
    4547             : static char *
    4548        2702 : get_rtable_name(int rtindex, deparse_context *context)
    4549             : {
    4550        2702 :     deparse_namespace *dpns = (deparse_namespace *) linitial(context->namespaces);
    4551             : 
    4552             :     Assert(rtindex > 0 && rtindex <= list_length(dpns->rtable_names));
    4553        2702 :     return (char *) list_nth(dpns->rtable_names, rtindex - 1);
    4554             : }
    4555             : 
    4556             : /*
    4557             :  * set_deparse_plan: set up deparse_namespace to parse subexpressions
    4558             :  * of a given Plan node
    4559             :  *
    4560             :  * This sets the plan, outer_plan, inner_plan, outer_tlist, inner_tlist,
    4561             :  * and index_tlist fields.  Caller is responsible for adjusting the ancestors
    4562             :  * list if necessary.  Note that the rtable, subplans, and ctes fields do
    4563             :  * not need to change when shifting attention to different plan nodes in a
    4564             :  * single plan tree.
    4565             :  */
    4566             : static void
    4567       59094 : set_deparse_plan(deparse_namespace *dpns, Plan *plan)
    4568             : {
    4569       59094 :     dpns->plan = plan;
    4570             : 
    4571             :     /*
    4572             :      * We special-case Append and MergeAppend to pretend that the first child
    4573             :      * plan is the OUTER referent; we have to interpret OUTER Vars in their
    4574             :      * tlists according to one of the children, and the first one is the most
    4575             :      * natural choice.  Likewise special-case ModifyTable to pretend that the
    4576             :      * first child plan is the OUTER referent; this is to support RETURNING
    4577             :      * lists containing references to non-target relations.
    4578             :      */
    4579       59094 :     if (IsA(plan, Append))
    4580        2416 :         dpns->outer_plan = linitial(((Append *) plan)->appendplans);
    4581       56678 :     else if (IsA(plan, MergeAppend))
    4582         272 :         dpns->outer_plan = linitial(((MergeAppend *) plan)->mergeplans);
    4583       56406 :     else if (IsA(plan, ModifyTable))
    4584         148 :         dpns->outer_plan = linitial(((ModifyTable *) plan)->plans);
    4585             :     else
    4586       56258 :         dpns->outer_plan = outerPlan(plan);
    4587             : 
    4588       59094 :     if (dpns->outer_plan)
    4589       26634 :         dpns->outer_tlist = dpns->outer_plan->targetlist;
    4590             :     else
    4591       32460 :         dpns->outer_tlist = NIL;
    4592             : 
    4593             :     /*
    4594             :      * For a SubqueryScan, pretend the subplan is INNER referent.  (We don't
    4595             :      * use OUTER because that could someday conflict with the normal meaning.)
    4596             :      * Likewise, for a CteScan, pretend the subquery's plan is INNER referent.
    4597             :      * For ON CONFLICT .. UPDATE we just need the inner tlist to point to the
    4598             :      * excluded expression's tlist. (Similar to the SubqueryScan we don't want
    4599             :      * to reuse OUTER, it's used for RETURNING in some modify table cases,
    4600             :      * although not INSERT .. CONFLICT).
    4601             :      */
    4602       59094 :     if (IsA(plan, SubqueryScan))
    4603         288 :         dpns->inner_plan = ((SubqueryScan *) plan)->subplan;
    4604       58806 :     else if (IsA(plan, CteScan))
    4605         210 :         dpns->inner_plan = list_nth(dpns->subplans,
    4606         210 :                                     ((CteScan *) plan)->ctePlanId - 1);
    4607       58596 :     else if (IsA(plan, ModifyTable))
    4608         148 :         dpns->inner_plan = plan;
    4609             :     else
    4610       58448 :         dpns->inner_plan = innerPlan(plan);
    4611             : 
    4612       59094 :     if (IsA(plan, ModifyTable))
    4613         148 :         dpns->inner_tlist = ((ModifyTable *) plan)->exclRelTlist;
    4614       58946 :     else if (dpns->inner_plan)
    4615        9616 :         dpns->inner_tlist = dpns->inner_plan->targetlist;
    4616             :     else
    4617       49330 :         dpns->inner_tlist = NIL;
    4618             : 
    4619             :     /* Set up referent for INDEX_VAR Vars, if needed */
    4620       59094 :     if (IsA(plan, IndexOnlyScan))
    4621        1336 :         dpns->index_tlist = ((IndexOnlyScan *) plan)->indextlist;
    4622       57758 :     else if (IsA(plan, ForeignScan))
    4623        2050 :         dpns->index_tlist = ((ForeignScan *) plan)->fdw_scan_tlist;
    4624       55708 :     else if (IsA(plan, CustomScan))
    4625           0 :         dpns->index_tlist = ((CustomScan *) plan)->custom_scan_tlist;
    4626             :     else
    4627       55708 :         dpns->index_tlist = NIL;
    4628       59094 : }
    4629             : 
    4630             : /*
    4631             :  * push_child_plan: temporarily transfer deparsing attention to a child plan
    4632             :  *
    4633             :  * When expanding an OUTER_VAR or INNER_VAR reference, we must adjust the
    4634             :  * deparse context in case the referenced expression itself uses
    4635             :  * OUTER_VAR/INNER_VAR.  We modify the top stack entry in-place to avoid
    4636             :  * affecting levelsup issues (although in a Plan tree there really shouldn't
    4637             :  * be any).
    4638             :  *
    4639             :  * Caller must provide a local deparse_namespace variable to save the
    4640             :  * previous state for pop_child_plan.
    4641             :  */
    4642             : static void
    4643       32966 : push_child_plan(deparse_namespace *dpns, Plan *plan,
    4644             :                 deparse_namespace *save_dpns)
    4645             : {
    4646             :     /* Save state for restoration later */
    4647       32966 :     *save_dpns = *dpns;
    4648             : 
    4649             :     /* Link current plan node into ancestors list */
    4650       32966 :     dpns->ancestors = lcons(dpns->plan, dpns->ancestors);
    4651             : 
    4652             :     /* Set attention on selected child */
    4653       32966 :     set_deparse_plan(dpns, plan);
    4654       32966 : }
    4655             : 
    4656             : /*
    4657             :  * pop_child_plan: undo the effects of push_child_plan
    4658             :  */
    4659             : static void
    4660       32966 : pop_child_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
    4661             : {
    4662             :     List       *ancestors;
    4663             : 
    4664             :     /* Get rid of ancestors list cell added by push_child_plan */
    4665       32966 :     ancestors = list_delete_first(dpns->ancestors);
    4666             : 
    4667             :     /* Restore fields changed by push_child_plan */
    4668       32966 :     *dpns = *save_dpns;
    4669             : 
    4670             :     /* Make sure dpns->ancestors is right (may be unnecessary) */
    4671       32966 :     dpns->ancestors = ancestors;
    4672       32966 : }
    4673             : 
    4674             : /*
    4675             :  * push_ancestor_plan: temporarily transfer deparsing attention to an
    4676             :  * ancestor plan
    4677             :  *
    4678             :  * When expanding a Param reference, we must adjust the deparse context
    4679             :  * to match the plan node that contains the expression being printed;
    4680             :  * otherwise we'd fail if that expression itself contains a Param or
    4681             :  * OUTER_VAR/INNER_VAR/INDEX_VAR variable.
    4682             :  *
    4683             :  * The target ancestor is conveniently identified by the ListCell holding it
    4684             :  * in dpns->ancestors.
    4685             :  *
    4686             :  * Caller must provide a local deparse_namespace variable to save the
    4687             :  * previous state for pop_ancestor_plan.
    4688             :  */
    4689             : static void
    4690        1566 : push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell,
    4691             :                    deparse_namespace *save_dpns)
    4692             : {
    4693        1566 :     Plan       *plan = (Plan *) lfirst(ancestor_cell);
    4694             : 
    4695             :     /* Save state for restoration later */
    4696        1566 :     *save_dpns = *dpns;
    4697             : 
    4698             :     /* Build a new ancestor list with just this node's ancestors */
    4699        1566 :     dpns->ancestors =
    4700        1566 :         list_copy_tail(dpns->ancestors,
    4701        1566 :                        list_cell_number(dpns->ancestors, ancestor_cell) + 1);
    4702             : 
    4703             :     /* Set attention on selected ancestor */
    4704        1566 :     set_deparse_plan(dpns, plan);
    4705        1566 : }
    4706             : 
    4707             : /*
    4708             :  * pop_ancestor_plan: undo the effects of push_ancestor_plan
    4709             :  */
    4710             : static void
    4711        1566 : pop_ancestor_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
    4712             : {
    4713             :     /* Free the ancestor list made in push_ancestor_plan */
    4714        1566 :     list_free(dpns->ancestors);
    4715             : 
    4716             :     /* Restore fields changed by push_ancestor_plan */
    4717        1566 :     *dpns = *save_dpns;
    4718        1566 : }
    4719             : 
    4720             : 
    4721             : /* ----------
    4722             :  * make_ruledef         - reconstruct the CREATE RULE command
    4723             :  *                for a given pg_rewrite tuple
    4724             :  * ----------
    4725             :  */
    4726             : static void
    4727         404 : make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
    4728             :              int prettyFlags)
    4729             : {
    4730             :     char       *rulename;
    4731             :     char        ev_type;
    4732             :     Oid         ev_class;
    4733             :     bool        is_instead;
    4734             :     char       *ev_qual;
    4735             :     char       *ev_action;
    4736             :     List       *actions;
    4737             :     Relation    ev_relation;
    4738         404 :     TupleDesc   viewResultDesc = NULL;
    4739             :     int         fno;
    4740             :     Datum       dat;
    4741             :     bool        isnull;
    4742             : 
    4743             :     /*
    4744             :      * Get the attribute values from the rules tuple
    4745             :      */
    4746         404 :     fno = SPI_fnumber(rulettc, "rulename");
    4747         404 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4748             :     Assert(!isnull);
    4749         404 :     rulename = NameStr(*(DatumGetName(dat)));
    4750             : 
    4751         404 :     fno = SPI_fnumber(rulettc, "ev_type");
    4752         404 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4753             :     Assert(!isnull);
    4754         404 :     ev_type = DatumGetChar(dat);
    4755             : 
    4756         404 :     fno = SPI_fnumber(rulettc, "ev_class");
    4757         404 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4758             :     Assert(!isnull);
    4759         404 :     ev_class = DatumGetObjectId(dat);
    4760             : 
    4761         404 :     fno = SPI_fnumber(rulettc, "is_instead");
    4762         404 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4763             :     Assert(!isnull);
    4764         404 :     is_instead = DatumGetBool(dat);
    4765             : 
    4766         404 :     fno = SPI_fnumber(rulettc, "ev_qual");
    4767         404 :     ev_qual = SPI_getvalue(ruletup, rulettc, fno);
    4768             :     Assert(ev_qual != NULL);
    4769             : 
    4770         404 :     fno = SPI_fnumber(rulettc, "ev_action");
    4771         404 :     ev_action = SPI_getvalue(ruletup, rulettc, fno);
    4772             :     Assert(ev_action != NULL);
    4773         404 :     actions = (List *) stringToNode(ev_action);
    4774         404 :     if (actions == NIL)
    4775           0 :         elog(ERROR, "invalid empty ev_action list");
    4776             : 
    4777         404 :     ev_relation = table_open(ev_class, AccessShareLock);
    4778             : 
    4779             :     /*
    4780             :      * Build the rules definition text
    4781             :      */
    4782         404 :     appendStringInfo(buf, "CREATE RULE %s AS",
    4783             :                      quote_identifier(rulename));
    4784             : 
    4785         404 :     if (prettyFlags & PRETTYFLAG_INDENT)
    4786         404 :         appendStringInfoString(buf, "\n    ON ");
    4787             :     else
    4788           0 :         appendStringInfoString(buf, " ON ");
    4789             : 
    4790             :     /* The event the rule is fired for */
    4791         404 :     switch (ev_type)
    4792             :     {
    4793           4 :         case '1':
    4794           4 :             appendStringInfoString(buf, "SELECT");
    4795           4 :             viewResultDesc = RelationGetDescr(ev_relation);
    4796           4 :             break;
    4797             : 
    4798         110 :         case '2':
    4799         110 :             appendStringInfoString(buf, "UPDATE");
    4800         110 :             break;
    4801             : 
    4802         218 :         case '3':
    4803         218 :             appendStringInfoString(buf, "INSERT");
    4804         218 :             break;
    4805             : 
    4806          72 :         case '4':
    4807          72 :             appendStringInfoString(buf, "DELETE");
    4808          72 :             break;
    4809             : 
    4810           0 :         default:
    4811           0 :             ereport(ERROR,
    4812             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4813             :                      errmsg("rule \"%s\" has unsupported event type %d",
    4814             :                             rulename, ev_type)));
    4815             :             break;
    4816             :     }
    4817             : 
    4818             :     /* The relation the rule is fired on */
    4819         404 :     appendStringInfo(buf, " TO %s",
    4820         404 :                      (prettyFlags & PRETTYFLAG_SCHEMA) ?
    4821          64 :                      generate_relation_name(ev_class, NIL) :
    4822         340 :                      generate_qualified_relation_name(ev_class));
    4823             : 
    4824             :     /* If the rule has an event qualification, add it */
    4825         404 :     if (strcmp(ev_qual, "<>") != 0)
    4826             :     {
    4827             :         Node       *qual;
    4828             :         Query      *query;
    4829             :         deparse_context context;
    4830             :         deparse_namespace dpns;
    4831             : 
    4832         110 :         if (prettyFlags & PRETTYFLAG_INDENT)
    4833         110 :             appendStringInfoString(buf, "\n  ");
    4834         110 :         appendStringInfoString(buf, " WHERE ");
    4835             : 
    4836         110 :         qual = stringToNode(ev_qual);
    4837             : 
    4838             :         /*
    4839             :          * We need to make a context for recognizing any Vars in the qual
    4840             :          * (which can only be references to OLD and NEW).  Use the rtable of
    4841             :          * the first query in the action list for this purpose.
    4842             :          */
    4843         110 :         query = (Query *) linitial(actions);
    4844             : 
    4845             :         /*
    4846             :          * If the action is INSERT...SELECT, OLD/NEW have been pushed down
    4847             :          * into the SELECT, and that's what we need to look at. (Ugly kluge
    4848             :          * ... try to fix this when we redesign querytrees.)
    4849             :          */
    4850         110 :         query = getInsertSelectQuery(query, NULL);
    4851             : 
    4852             :         /* Must acquire locks right away; see notes in get_query_def() */
    4853         110 :         AcquireRewriteLocks(query, false, false);
    4854             : 
    4855         110 :         context.buf = buf;
    4856         110 :         context.namespaces = list_make1(&dpns);
    4857         110 :         context.windowClause = NIL;
    4858         110 :         context.windowTList = NIL;
    4859         110 :         context.varprefix = (list_length(query->rtable) != 1);
    4860         110 :         context.prettyFlags = prettyFlags;
    4861         110 :         context.wrapColumn = WRAP_COLUMN_DEFAULT;
    4862         110 :         context.indentLevel = PRETTYINDENT_STD;
    4863         110 :         context.special_exprkind = EXPR_KIND_NONE;
    4864         110 :         context.appendparents = NULL;
    4865             : 
    4866         110 :         set_deparse_for_query(&dpns, query, NIL);
    4867             : 
    4868         110 :         get_rule_expr(qual, &context, false);
    4869             :     }
    4870             : 
    4871         404 :     appendStringInfoString(buf, " DO ");
    4872             : 
    4873             :     /* The INSTEAD keyword (if so) */
    4874         404 :     if (is_instead)
    4875         236 :         appendStringInfoString(buf, "INSTEAD ");
    4876             : 
    4877             :     /* Finally the rules actions */
    4878         404 :     if (list_length(actions) > 1)
    4879             :     {
    4880             :         ListCell   *action;
    4881             :         Query      *query;
    4882             : 
    4883          20 :         appendStringInfoChar(buf, '(');
    4884          60 :         foreach(action, actions)
    4885             :         {
    4886          40 :             query = (Query *) lfirst(action);
    4887          40 :             get_query_def(query, buf, NIL, viewResultDesc,
    4888             :                           prettyFlags, WRAP_COLUMN_DEFAULT, 0);
    4889          40 :             if (prettyFlags)
    4890          40 :                 appendStringInfoString(buf, ";\n");
    4891             :             else
    4892           0 :                 appendStringInfoString(buf, "; ");
    4893             :         }
    4894          20 :         appendStringInfoString(buf, ");");
    4895             :     }
    4896             :     else
    4897             :     {
    4898             :         Query      *query;
    4899             : 
    4900         384 :         query = (Query *) linitial(actions);
    4901         384 :         get_query_def(query, buf, NIL, viewResultDesc,
    4902             :                       prettyFlags, WRAP_COLUMN_DEFAULT, 0);
    4903         384 :         appendStringInfoChar(buf, ';');
    4904             :     }
    4905             : 
    4906         404 :     table_close(ev_relation, AccessShareLock);
    4907         404 : }
    4908             : 
    4909             : 
    4910             : /* ----------
    4911             :  * make_viewdef         - reconstruct the SELECT part of a
    4912             :  *                view rewrite rule
    4913             :  * ----------
    4914             :  */
    4915             : static void
    4916        1704 : make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
    4917             :              int prettyFlags, int wrapColumn)
    4918             : {
    4919             :     Query      *query;
    4920             :     char        ev_type;
    4921             :     Oid         ev_class;
    4922             :     bool        is_instead;
    4923             :     char       *ev_qual;
    4924             :     char       *ev_action;
    4925             :     List       *actions;
    4926             :     Relation    ev_relation;
    4927             :     int         fno;
    4928             :     Datum       dat;
    4929             :     bool        isnull;
    4930             : 
    4931             :     /*
    4932             :      * Get the attribute values from the rules tuple
    4933             :      */
    4934        1704 :     fno = SPI_fnumber(rulettc, "ev_type");
    4935        1704 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4936             :     Assert(!isnull);
    4937        1704 :     ev_type = DatumGetChar(dat);
    4938             : 
    4939        1704 :     fno = SPI_fnumber(rulettc, "ev_class");
    4940        1704 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4941             :     Assert(!isnull);
    4942        1704 :     ev_class = DatumGetObjectId(dat);
    4943             : 
    4944        1704 :     fno = SPI_fnumber(rulettc, "is_instead");
    4945        1704 :     dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
    4946             :     Assert(!isnull);
    4947        1704 :     is_instead = DatumGetBool(dat);
    4948             : 
    4949        1704 :     fno = SPI_fnumber(rulettc, "ev_qual");
    4950        1704 :     ev_qual = SPI_getvalue(ruletup, rulettc, fno);
    4951             :     Assert(ev_qual != NULL);
    4952             : 
    4953        1704 :     fno = SPI_fnumber(rulettc, "ev_action");
    4954        1704 :     ev_action = SPI_getvalue(ruletup, rulettc, fno);
    4955             :     Assert(ev_action != NULL);
    4956        1704 :     actions = (List *) stringToNode(ev_action);
    4957             : 
    4958        1704 :     if (list_length(actions) != 1)
    4959             :     {
    4960             :         /* keep output buffer empty and leave */
    4961           0 :         return;
    4962             :     }
    4963             : 
    4964        1704 :     query = (Query *) linitial(actions);
    4965             : 
    4966        1704 :     if (ev_type != '1' || !is_instead ||
    4967        1704 :         strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
    4968             :     {
    4969             :         /* keep output buffer empty and leave */
    4970           0 :         return;
    4971             :     }
    4972             : 
    4973        1704 :     ev_relation = table_open(ev_class, AccessShareLock);
    4974             : 
    4975        1704 :     get_query_def(query, buf, NIL, RelationGetDescr(ev_relation),
    4976             :                   prettyFlags, wrapColumn, 0);
    4977        1704 :     appendStringInfoChar(buf, ';');
    4978             : 
    4979        1704 :     table_close(ev_relation, AccessShareLock);
    4980             : }
    4981             : 
    4982             : 
    4983             : /* ----------
    4984             :  * get_query_def            - Parse back one query parsetree
    4985             :  *
    4986             :  * If resultDesc is not NULL, then it is the output tuple descriptor for
    4987             :  * the view represented by a SELECT query.
    4988             :  * ----------
    4989             :  */
    4990             : static void
    4991        2688 : get_query_def(Query *query, StringInfo buf, List *parentnamespace,
    4992             :               TupleDesc resultDesc,
    4993             :               int prettyFlags, int wrapColumn, int startIndent)
    4994             : {
    4995             :     deparse_context context;
    4996             :     deparse_namespace dpns;
    4997             : 
    4998             :     /* Guard against excessively long or deeply-nested queries */
    4999        2688 :     CHECK_FOR_INTERRUPTS();
    5000        2688 :     check_stack_depth();
    5001             : 
    5002             :     /*
    5003             :      * Before we begin to examine the query, acquire locks on referenced
    5004             :      * relations, and fix up deleted columns in JOIN RTEs.  This ensures
    5005             :      * consistent results.  Note we assume it's OK to scribble on the passed
    5006             :      * querytree!
    5007             :      *
    5008             :      * We are only deparsing the query (we are not about to execute it), so we
    5009             :      * only need AccessShareLock on the relations it mentions.
    5010             :      */
    5011        2688 :     AcquireRewriteLocks(query, false, false);
    5012             : 
    5013        2688 :     context.buf = buf;
    5014        2688 :     context.namespaces = lcons(&dpns, list_copy(parentnamespace));
    5015        2688 :     context.windowClause = NIL;
    5016        2688 :     context.windowTList = NIL;
    5017        4830 :     context.varprefix = (parentnamespace != NIL ||
    5018        2142 :                          list_length(query->rtable) != 1);
    5019        2688 :     context.prettyFlags = prettyFlags;
    5020        2688 :     context.wrapColumn = wrapColumn;
    5021        2688 :     context.indentLevel = startIndent;
    5022        2688 :     context.special_exprkind = EXPR_KIND_NONE;
    5023        2688 :     context.appendparents = NULL;
    5024             : 
    5025        2688 :     set_deparse_for_query(&dpns, query, parentnamespace);
    5026             : 
    5027        2688 :     switch (query->commandType)
    5028             :     {
    5029        2286 :         case CMD_SELECT:
    5030        2286 :             get_select_query_def(query, &context, resultDesc);
    5031        2286 :             break;
    5032             : 
    5033          88 :         case CMD_UPDATE:
    5034          88 :             get_update_query_def(query, &context);
    5035          88 :             break;
    5036             : 
    5037         216 :         case CMD_INSERT:
    5038         216 :             get_insert_query_def(query, &context);
    5039         216 :             break;
    5040             : 
    5041          50 :         case CMD_DELETE:
    5042          50 :             get_delete_query_def(query, &context);
    5043          50 :             break;
    5044             : 
    5045          34 :         case CMD_NOTHING:
    5046          34 :             appendStringInfoString(buf, "NOTHING");
    5047          34 :             break;
    5048             : 
    5049          14 :         case CMD_UTILITY:
    5050          14 :             get_utility_query_def(query, &context);
    5051          14 :             break;
    5052             : 
    5053           0 :         default:
    5054           0 :             elog(ERROR, "unrecognized query command type: %d",
    5055             :                  query->commandType);
    5056             :             break;
    5057             :     }
    5058        2688 : }
    5059             : 
    5060             : /* ----------
    5061             :  * get_values_def           - Parse back a VALUES list
    5062             :  * ----------
    5063             :  */
    5064             : static void
    5065         164 : get_values_def(List *values_lists, deparse_context *context)
    5066             : {
    5067         164 :     StringInfo  buf = context->buf;
    5068         164 :     bool        first_list = true;
    5069             :     ListCell   *vtl;
    5070             : 
    5071         164 :     appendStringInfoString(buf, "VALUES ");
    5072             : 
    5073         464 :     foreach(vtl, values_lists)
    5074             :     {
    5075         300 :         List       *sublist = (List *) lfirst(vtl);
    5076         300 :         bool        first_col = true;
    5077             :         ListCell   *lc;
    5078             : 
    5079         300 :         if (first_list)
    5080         164 :             first_list = false;
    5081             :         else
    5082         136 :             appendStringInfoString(buf, ", ");
    5083             : 
    5084         300 :         appendStringInfoChar(buf, '(');
    5085        1164 :         foreach(lc, sublist)
    5086             :         {
    5087         864 :             Node       *col = (Node *) lfirst(lc);
    5088             : 
    5089         864 :             if (first_col)
    5090         300 :                 first_col = false;
    5091             :             else
    5092         564 :                 appendStringInfoChar(buf, ',');
    5093             : 
    5094             :             /*
    5095             :              * Print the value.  Whole-row Vars need special treatment.
    5096             :              */
    5097         864 :             get_rule_expr_toplevel(col, context, false);
    5098             :         }
    5099         300 :         appendStringInfoChar(buf, ')');
    5100             :     }
    5101         164 : }
    5102             : 
    5103             : /* ----------
    5104             :  * get_with_clause          - Parse back a WITH clause
    5105             :  * ----------
    5106             :  */
    5107             : static void
    5108        2640 : get_with_clause(Query *query, deparse_context *context)
    5109             : {
    5110        2640 :     StringInfo  buf = context->buf;
    5111             :     const char *sep;
    5112             :     ListCell   *l;
    5113             : 
    5114        2640 :     if (query->cteList == NIL)
    5115        2608 :         return;
    5116             : 
    5117          32 :     if (PRETTY_INDENT(context))
    5118             :     {
    5119          32 :         context->indentLevel += PRETTYINDENT_STD;
    5120          32 :         appendStringInfoChar(buf, ' ');
    5121             :     }
    5122             : 
    5123          32 :     if (query->hasRecursive)
    5124          24 :         sep = "WITH RECURSIVE ";
    5125             :     else
    5126           8 :         sep = "WITH ";
    5127          64 :     foreach(l, query->cteList)
    5128             :     {
    5129          32 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
    5130             : 
    5131          32 :         appendStringInfoString(buf, sep);
    5132          32 :         appendStringInfoString(buf, quote_identifier(cte->ctename));
    5133          32 :         if (cte->aliascolnames)
    5134             :         {
    5135          16 :             bool        first = true;
    5136             :             ListCell   *col;
    5137             : 
    5138          16 :             appendStringInfoChar(buf, '(');
    5139          32 :             foreach(col, cte->aliascolnames)
    5140             :             {
    5141          16 :                 if (first)
    5142          16 :                     first = false;
    5143             :                 else
    5144           0 :                     appendStringInfoString(buf, ", ");
    5145          16 :                 appendStringInfoString(buf,
    5146          16 :                                        quote_identifier(strVal(lfirst(col))));
    5147             :             }
    5148          16 :             appendStringInfoChar(buf, ')');
    5149             :         }
    5150          32 :         appendStringInfoString(buf, " AS ");
    5151          32 :         switch (cte->ctematerialized)
    5152             :         {
    5153          24 :             case CTEMaterializeDefault:
    5154          24 :                 break;
    5155           8 :             case CTEMaterializeAlways:
    5156           8 :                 appendStringInfoString(buf, "MATERIALIZED ");
    5157           8 :                 break;
    5158           0 :             case CTEMaterializeNever:
    5159           0 :                 appendStringInfoString(buf, "NOT MATERIALIZED ");
    5160           0 :                 break;
    5161             :         }
    5162          32 :         appendStringInfoChar(buf, '(');
    5163          32 :         if (PRETTY_INDENT(context))
    5164          32 :             appendContextKeyword(context, "", 0, 0, 0);
    5165          32 :         get_query_def((Query *) cte->ctequery, buf, context->namespaces, NULL,
    5166             :                       context->prettyFlags, context->wrapColumn,
    5167             :                       context->indentLevel);
    5168          32 :         if (PRETTY_INDENT(context))
    5169          32 :             appendContextKeyword(context, "", 0, 0, 0);
    5170          32 :         appendStringInfoChar(buf, ')');
    5171          32 :         sep = ", ";
    5172             :     }
    5173             : 
    5174          32 :     if (PRETTY_INDENT(context))
    5175             :     {
    5176          32 :         context->indentLevel -= PRETTYINDENT_STD;
    5177          32 :         appendContextKeyword(context, "", 0, 0, 0);
    5178             :     }
    5179             :     else
    5180           0 :         appendStringInfoChar(buf, ' ');
    5181             : }
    5182             : 
    5183             : /* ----------
    5184             :  * get_select_query_def         - Parse back a SELECT parsetree
    5185             :  * ----------
    5186             :  */
    5187             : static void
    5188        2286 : get_select_query_def(Query *query, deparse_context *context,
    5189             :                      TupleDesc resultDesc)
    5190             : {
    5191        2286 :     StringInfo  buf = context->buf;
    5192             :     List       *save_windowclause;
    5193             :     List       *save_windowtlist;
    5194             :     bool        force_colno;
    5195             :     ListCell   *l;
    5196             : 
    5197             :     /* Insert the WITH clause if given */
    5198        2286 :     get_with_clause(query, context);
    5199             : 
    5200             :     /* Set up context for possible window functions */
    5201        2286 :     save_windowclause = context->windowClause;
    5202        2286 :     context->windowClause = query->windowClause;
    5203        2286 :     save_windowtlist = context->windowTList;
    5204        2286 :     context->windowTList = query->targetList;
    5205             : 
    5206             :     /*
    5207             :      * If the Query node has a setOperations tree, then it's the top level of
    5208             :      * a UNION/INTERSECT/EXCEPT query; only the WITH, ORDER BY and LIMIT
    5209             :      * fields are interesting in the top query itself.
    5210             :      */
    5211        2286 :     if (query->setOperations)
    5212             :     {
    5213          80 :         get_setop_query(query->setOperations, query, context, resultDesc);
    5214             :         /* ORDER BY clauses must be simple in this case */
    5215          80 :         force_colno = true;
    5216             :     }
    5217             :     else
    5218             :     {
    5219        2206 :         get_basic_select_query(query, context, resultDesc);
    5220        2206 :         force_colno = false;
    5221             :     }
    5222             : 
    5223             :     /* Add the ORDER BY clause if given */
    5224        2286 :     if (query->sortClause != NIL)
    5225             :     {
    5226          70 :         appendContextKeyword(context, " ORDER BY ",
    5227             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    5228          70 :         get_rule_orderby(query->sortClause, query->targetList,
    5229             :                          force_colno, context);
    5230             :     }
    5231             : 
    5232             :     /*
    5233             :      * Add the LIMIT/OFFSET clauses if given. If non-default options, use the
    5234             :      * standard spelling of LIMIT.
    5235             :      */
    5236        2286 :     if (query->limitOffset != NULL)
    5237             :     {
    5238          20 :         appendContextKeyword(context, " OFFSET ",
    5239             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5240          20 :         get_rule_expr(query->limitOffset, context, false);
    5241             :     }
    5242        2286 :     if (query->limitCount != NULL)
    5243             :     {
    5244          44 :         if (query->limitOption == LIMIT_OPTION_WITH_TIES)
    5245             :         {
    5246          20 :             appendContextKeyword(context, " FETCH FIRST ",
    5247             :                                  -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5248          20 :             get_rule_expr(query->limitCount, context, false);
    5249          20 :             appendStringInfoString(buf, " ROWS WITH TIES");
    5250             :         }
    5251             :         else
    5252             :         {
    5253          24 :             appendContextKeyword(context, " LIMIT ",
    5254             :                                  -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5255          24 :             if (IsA(query->limitCount, Const) &&
    5256          10 :                 ((Const *) query->limitCount)->constisnull)
    5257          10 :                 appendStringInfoString(buf, "ALL");
    5258             :             else
    5259          14 :                 get_rule_expr(query->limitCount, context, false);
    5260             :         }
    5261             :     }
    5262             : 
    5263             :     /* Add FOR [KEY] UPDATE/SHARE clauses if present */
    5264        2286 :     if (query->hasForUpdate)
    5265             :     {
    5266           0 :         foreach(l, query->rowMarks)
    5267             :         {
    5268           0 :             RowMarkClause *rc = (RowMarkClause *) lfirst(l);
    5269             : 
    5270             :             /* don't print implicit clauses */
    5271           0 :             if (rc->pushedDown)
    5272           0 :                 continue;
    5273             : 
    5274           0 :             switch (rc->strength)
    5275             :             {
    5276           0 :                 case LCS_NONE:
    5277             :                     /* we intentionally throw an error for LCS_NONE */
    5278           0 :                     elog(ERROR, "unrecognized LockClauseStrength %d",
    5279             :                          (int) rc->strength);
    5280             :                     break;
    5281           0 :                 case LCS_FORKEYSHARE:
    5282           0 :                     appendContextKeyword(context, " FOR KEY SHARE",
    5283             :                                          -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5284           0 :                     break;
    5285           0 :                 case LCS_FORSHARE:
    5286           0 :                     appendContextKeyword(context, " FOR SHARE",
    5287             :                                          -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5288           0 :                     break;
    5289           0 :                 case LCS_FORNOKEYUPDATE:
    5290           0 :                     appendContextKeyword(context, " FOR NO KEY UPDATE",
    5291             :                                          -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5292           0 :                     break;
    5293           0 :                 case LCS_FORUPDATE:
    5294           0 :                     appendContextKeyword(context, " FOR UPDATE",
    5295             :                                          -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5296           0 :                     break;
    5297             :             }
    5298             : 
    5299           0 :             appendStringInfo(buf, " OF %s",
    5300           0 :                              quote_identifier(get_rtable_name(rc->rti,
    5301             :                                                               context)));
    5302           0 :             if (rc->waitPolicy == LockWaitError)
    5303           0 :                 appendStringInfoString(buf, " NOWAIT");
    5304           0 :             else if (rc->waitPolicy == LockWaitSkip)
    5305           0 :                 appendStringInfoString(buf, " SKIP LOCKED");
    5306             :         }
    5307             :     }
    5308             : 
    5309        2286 :     context->windowClause = save_windowclause;
    5310        2286 :     context->windowTList = save_windowtlist;
    5311        2286 : }
    5312             : 
    5313             : /*
    5314             :  * Detect whether query looks like SELECT ... FROM VALUES(),
    5315             :  * with no need to rename the output columns of the VALUES RTE.
    5316             :  * If so, return the VALUES RTE.  Otherwise return NULL.
    5317             :  */
    5318             : static RangeTblEntry *
    5319        2206 : get_simple_values_rte(Query *query, TupleDesc resultDesc)
    5320             : {
    5321        2206 :     RangeTblEntry *result = NULL;
    5322             :     ListCell   *lc;
    5323             : 
    5324             :     /*
    5325             :      * We want to detect a match even if the Query also contains OLD or NEW
    5326             :      * rule RTEs.  So the idea is to scan the rtable and see if there is only
    5327             :      * one inFromCl RTE that is a VALUES RTE.
    5328             :      */
    5329        5728 :     foreach(lc, query->rtable)
    5330             :     {
    5331        5352 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
    5332             : 
    5333        5352 :         if (rte->rtekind == RTE_VALUES && rte->inFromCl)
    5334             :         {
    5335         146 :             if (result)
    5336        1830 :                 return NULL;    /* multiple VALUES (probably not possible) */
    5337         146 :             result = rte;
    5338             :         }
    5339        5206 :         else if (rte->rtekind == RTE_RELATION && !rte->inFromCl)
    5340        3376 :             continue;           /* ignore rule entries */
    5341             :         else
    5342        1830 :             return NULL;        /* something else -> not simple VALUES */
    5343             :     }
    5344             : 
    5345             :     /*
    5346             :      * We don't need to check the targetlist in any great detail, because
    5347             :      * parser/analyze.c will never generate a "bare" VALUES RTE --- they only
    5348             :      * appear inside auto-generated sub-queries with very restricted
    5349             :      * structure.  However, DefineView might have modified the tlist by
    5350             :      * injecting new column aliases, or we might have some other column
    5351             :      * aliases forced by a resultDesc.  We can only simplify if the RTE's
    5352             :      * column names match the names that get_target_list() would select.
    5353             :      */
    5354         376 :     if (result)
    5355             :     {
    5356             :         ListCell   *lcn;
    5357             :         int         colno;
    5358             : 
    5359         146 :         if (list_length(query->targetList) != list_length(result->eref->colnames))
    5360           0 :             return NULL;        /* this probably cannot happen */
    5361         146 :         colno = 0;
    5362         556 :         forboth(lc, query->targetList, lcn, result->eref->colnames)
    5363             :         {
    5364         418 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
    5365         418 :             char       *cname = strVal(lfirst(lcn));
    5366             :             char       *colname;
    5367             : 
    5368         418 :             if (tle->resjunk)
    5369           8 :                 return NULL;    /* this probably cannot happen */
    5370             : 
    5371             :             /* compute name that get_target_list would use for column */
    5372         418 :             colno++;
    5373         418 :             if (resultDesc && colno <= resultDesc->natts)
    5374          20 :                 colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
    5375             :             else
    5376         398 :                 colname = tle->resname;
    5377             : 
    5378             :             /* does it match the VALUES RTE? */
    5379         418 :             if (colname == NULL || strcmp(colname, cname) != 0)
    5380           8 :                 return NULL;    /* column name has been changed */
    5381             :         }
    5382             :     }
    5383             : 
    5384         368 :     return result;
    5385             : }
    5386             : 
    5387             : static void
    5388        2206 : get_basic_select_query(Query *query, deparse_context *context,
    5389             :                        TupleDesc resultDesc)
    5390             : {
    5391        2206 :     StringInfo  buf = context->buf;
    5392             :     RangeTblEntry *values_rte;
    5393             :     char       *sep;
    5394             :     ListCell   *l;
    5395             : 
    5396        2206 :     if (PRETTY_INDENT(context))
    5397             :     {
    5398        2206 :         context->indentLevel += PRETTYINDENT_STD;
    5399        2206 :         appendStringInfoChar(buf, ' ');
    5400             :     }
    5401             : 
    5402             :     /*
    5403             :      * If the query looks like SELECT * FROM (VALUES ...), then print just the
    5404             :      * VALUES part.  This reverses what transformValuesClause() did at parse
    5405             :      * time.
    5406             :      */
    5407        2206 :     values_rte = get_simple_values_rte(query, resultDesc);
    5408        2206 :     if (values_rte)
    5409             :     {
    5410         138 :         get_values_def(values_rte->values_lists, context);
    5411         138 :         return;
    5412             :     }
    5413             : 
    5414             :     /*
    5415             :      * Build up the query string - first we say SELECT
    5416             :      */
    5417        2068 :     appendStringInfoString(buf, "SELECT");
    5418             : 
    5419             :     /* Add the DISTINCT clause if given */
    5420        2068 :     if (query->distinctClause != NIL)
    5421             :     {
    5422           0 :         if (query->hasDistinctOn)
    5423             :         {
    5424           0 :             appendStringInfoString(buf, " DISTINCT ON (");
    5425           0 :             sep = "";
    5426           0 :             foreach(l, query->distinctClause)
    5427             :             {
    5428           0 :                 SortGroupClause *srt = (SortGroupClause *) lfirst(l);
    5429             : 
    5430           0 :                 appendStringInfoString(buf, sep);
    5431           0 :                 get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList,
    5432             :                                          false, context);
    5433           0 :                 sep = ", ";
    5434             :             }
    5435           0 :             appendStringInfoChar(buf, ')');
    5436             :         }
    5437             :         else
    5438           0 :             appendStringInfoString(buf, " DISTINCT");
    5439             :     }
    5440             : 
    5441             :     /* Then we tell what to select (the targetlist) */
    5442        2068 :     get_target_list(query->targetList, context, resultDesc);
    5443             : 
    5444             :     /* Add the FROM clause if needed */
    5445        2068 :     get_from_clause(query, " FROM ", context);
    5446             : 
    5447             :     /* Add the WHERE clause if given */
    5448        2068 :     if (query->jointree->quals != NULL)
    5449             :     {
    5450         712 :         appendContextKeyword(context, " WHERE ",
    5451             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    5452         712 :         get_rule_expr(query->jointree->quals, context, false);
    5453             :     }
    5454             : 
    5455             :     /* Add the GROUP BY clause if given */
    5456        2068 :     if (query->groupClause != NULL || query->groupingSets != NULL)
    5457             :     {
    5458             :         ParseExprKind save_exprkind;
    5459             : 
    5460          66 :         appendContextKeyword(context, " GROUP BY ",
    5461             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    5462             : 
    5463          66 :         save_exprkind = context->special_exprkind;
    5464          66 :         context->special_exprkind = EXPR_KIND_GROUP_BY;
    5465             : 
    5466          66 :         if (query->groupingSets == NIL)
    5467             :         {
    5468          62 :             sep = "";
    5469         166 :             foreach(l, query->groupClause)
    5470             :             {
    5471         104 :                 SortGroupClause *grp = (SortGroupClause *) lfirst(l);
    5472             : 
    5473         104 :                 appendStringInfoString(buf, sep);
    5474         104 :                 get_rule_sortgroupclause(grp->tleSortGroupRef, query->targetList,
    5475             :                                          false, context);
    5476         104 :                 sep = ", ";
    5477             :             }
    5478             :         }
    5479             :         else
    5480             :         {
    5481           4 :             sep = "";
    5482           8 :             foreach(l, query->groupingSets)
    5483             :             {
    5484           4 :                 GroupingSet *grp = lfirst(l);
    5485             : 
    5486           4 :                 appendStringInfoString(buf, sep);
    5487           4 :                 get_rule_groupingset(grp, query->targetList, true, context);
    5488           4 :                 sep = ", ";
    5489             :             }
    5490             :         }
    5491             : 
    5492          66 :         context->special_exprkind = save_exprkind;
    5493             :     }
    5494             : 
    5495             :     /* Add the HAVING clause if given */
    5496        2068 :     if (query->havingQual != NULL)
    5497             :     {
    5498          10 :         appendContextKeyword(context, " HAVING ",
    5499             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
    5500          10 :         get_rule_expr(query->havingQual, context, false);
    5501             :     }
    5502             : 
    5503             :     /* Add the WINDOW clause if needed */
    5504        2068 :     if (query->windowClause != NIL)
    5505          28 :         get_rule_windowclause(query, context);
    5506             : }
    5507             : 
    5508             : /* ----------
    5509             :  * get_target_list          - Parse back a SELECT target list
    5510             :  *
    5511             :  * This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
    5512             :  * ----------
    5513             :  */
    5514             : static void
    5515        2102 : get_target_list(List *targetList, deparse_context *context,
    5516             :                 TupleDesc resultDesc)
    5517             : {
    5518        2102 :     StringInfo  buf = context->buf;
    5519             :     StringInfoData targetbuf;
    5520        2102 :     bool        last_was_multiline = false;
    5521             :     char       *sep;
    5522             :     int         colno;
    5523             :     ListCell   *l;
    5524             : 
    5525             :     /* we use targetbuf to hold each TLE's text temporarily */
    5526        2102 :     initStringInfo(&targetbuf);
    5527             : 
    5528        2102 :     sep = " ";
    5529        2102 :     colno = 0;
    5530        9376 :     foreach(l, targetList)
    5531             :     {
    5532        7274 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
    5533             :         char       *colname;
    5534             :         char       *attname;
    5535             : 
    5536        7274 :         if (tle->resjunk)
    5537          30 :             continue;           /* ignore junk entries */
    5538             : 
    5539        7244 :         appendStringInfoString(buf, sep);
    5540        7244 :         sep = ", ";
    5541        7244 :         colno++;
    5542             : 
    5543             :         /*
    5544             :          * Put the new field text into targetbuf so we can decide after we've
    5545             :          * got it whether or not it needs to go on a new line.
    5546             :          */
    5547        7244 :         resetStringInfo(&targetbuf);
    5548        7244 :         context->buf = &targetbuf;
    5549             : 
    5550             :         /*
    5551             :          * We special-case Var nodes rather than using get_rule_expr. This is
    5552             :          * needed because get_rule_expr will display a whole-row Var as
    5553             :          * "foo.*", which is the preferred notation in most contexts, but at
    5554             :          * the top level of a SELECT list it's not right (the parser will
    5555             :          * expand that notation into multiple columns, yielding behavior
    5556             :          * different from a whole-row Var).  We need to call get_variable
    5557             :          * directly so that we can tell it to do the right thing, and so that
    5558             :          * we can get the attribute name which is the default AS label.
    5559             :          */
    5560        7244 :         if (tle->expr && (IsA(tle->expr, Var)))
    5561             :         {
    5562        5692 :             attname = get_variable((Var *) tle->expr, 0, true, context);
    5563             :         }
    5564             :         else
    5565             :         {
    5566        1552 :             get_rule_expr((Node *) tle->expr, context, true);
    5567             :             /* We'll show the AS name unless it's this: */
    5568        1552 :             attname = "?column?";
    5569             :         }
    5570             : 
    5571             :         /*
    5572             :          * Figure out what the result column should be called.  In the context
    5573             :          * of a view, use the view's tuple descriptor (so as to pick up the
    5574             :          * effects of any column RENAME that's been done on the view).
    5575             :          * Otherwise, just use what we can find in the TLE.
    5576             :          */
    5577        7244 :         if (resultDesc && colno <= resultDesc->natts)
    5578        6822 :             colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
    5579             :         else
    5580         422 :             colname = tle->resname;
    5581             : 
    5582             :         /* Show AS unless the column's name is correct as-is */
    5583        7244 :         if (colname)            /* resname could be NULL */
    5584             :         {
    5585        7244 :             if (attname == NULL || strcmp(attname, colname) != 0)
    5586        2078 :                 appendStringInfo(&targetbuf, " AS %s", quote_identifier(colname));
    5587             :         }
    5588             : 
    5589             :         /* Restore context's output buffer */
    5590        7244 :         context->buf = buf;
    5591             : 
    5592             :         /* Consider line-wrapping if enabled */
    5593        7244 :         if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
    5594             :         {
    5595             :             int         leading_nl_pos;
    5596             : 
    5597             :             /* Does the new field start with a new line? */
    5598        7244 :             if (targetbuf.len > 0 && targetbuf.data[0] == '\n')
    5599         108 :                 leading_nl_pos = 0;
    5600             :             else
    5601        7136 :                 leading_nl_pos = -1;
    5602             : 
    5603             :             /* If so, we shouldn't add anything */
    5604        7244 :             if (leading_nl_pos >= 0)
    5605             :             {
    5606             :                 /* instead, remove any trailing spaces currently in buf */
    5607         108 :                 removeStringInfoSpaces(buf);
    5608             :             }
    5609             :             else
    5610             :             {
    5611             :                 char       *trailing_nl;
    5612             : 
    5613             :                 /* Locate the start of the current line in the output buffer */
    5614        7136 :                 trailing_nl = strrchr(buf->data, '\n');
    5615        7136 :                 if (trailing_nl == NULL)
    5616        2688 :                     trailing_nl = buf->data;
    5617             :                 else
    5618        4448 :                     trailing_nl++;
    5619             : 
    5620             :                 /*
    5621             :                  * Add a newline, plus some indentation, if the new field is
    5622             :                  * not the first and either the new field would cause an
    5623             :                  * overflow or the last field used more than one line.
    5624             :                  */
    5625        7136 :                 if (colno > 1 &&
    5626        5044 :                     ((strlen(trailing_nl) + targetbuf.len > context->wrapColumn) ||
    5627             :                      last_was_multiline))
    5628        5044 :                     appendContextKeyword(context, "", -PRETTYINDENT_STD,
    5629             :                                          PRETTYINDENT_STD, PRETTYINDENT_VAR);
    5630             :             }
    5631             : 
    5632             :             /* Remember this field's multiline status for next iteration */
    5633        7244 :             last_was_multiline =
    5634        7244 :                 (strchr(targetbuf.data + leading_nl_pos + 1, '\n') != NULL);
    5635             :         }
    5636             : 
    5637             :         /* Add the new field */
    5638        7244 :         appendBinaryStringInfo(buf, targetbuf.data, targetbuf.len);
    5639             :     }
    5640             : 
    5641             :     /* clean up */
    5642        2102 :     pfree(targetbuf.data);
    5643        2102 : }
    5644             : 
    5645             : static void
    5646         328 : get_setop_query(Node *setOp, Query *query, deparse_context *context,
    5647             :                 TupleDesc resultDesc)
    5648             : {
    5649         328 :     StringInfo  buf = context->buf;
    5650             :     bool        need_paren;
    5651             : 
    5652             :     /* Guard against excessively long or deeply-nested queries */
    5653         328 :     CHECK_FOR_INTERRUPTS();
    5654         328 :     check_stack_depth();
    5655             : 
    5656         328 :     if (IsA(setOp, RangeTblRef))
    5657             :     {
    5658         204 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    5659         204 :         RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable);
    5660         204 :         Query      *subquery = rte->subquery;
    5661             : 
    5662             :         Assert(subquery != NULL);
    5663             :         Assert(subquery->setOperations == NULL);
    5664             :         /* Need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y */
    5665         612 :         need_paren = (subquery->cteList ||
    5666         204 :                       subquery->sortClause ||
    5667         204 :                       subquery->rowMarks ||
    5668         612 :                       subquery->limitOffset ||
    5669         204 :                       subquery->limitCount);
    5670         204 :         if (need_paren)
    5671           0 :             appendStringInfoChar(buf, '(');
    5672         204 :         get_query_def(subquery, buf, context->namespaces, resultDesc,
    5673             :                       context->prettyFlags, context->wrapColumn,
    5674             :                       context->indentLevel);
    5675         204 :         if (need_paren)
    5676           0 :             appendStringInfoChar(buf, ')');
    5677             :     }
    5678         124 :     else if (IsA(setOp, SetOperationStmt))
    5679             :     {
    5680         124 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    5681             :         int         subindent;
    5682             : 
    5683             :         /*
    5684             :          * We force parens when nesting two SetOperationStmts, except when the
    5685             :          * lefthand input is another setop of the same kind.  Syntactically,
    5686             :          * we could omit parens in rather more cases, but it seems best to use
    5687             :          * parens to flag cases where the setop operator changes.  If we use
    5688             :          * parens, we also increase the indentation level for the child query.
    5689             :          *
    5690             :          * There are some cases in which parens are needed around a leaf query
    5691             :          * too, but those are more easily handled at the next level down (see
    5692             :          * code above).
    5693             :          */
    5694         124 :         if (IsA(op->larg, SetOperationStmt))
    5695             :         {
    5696          44 :             SetOperationStmt *lop = (SetOperationStmt *) op->larg;
    5697             : 
    5698          44 :             if (op->op == lop->op && op->all == lop->all)
    5699          44 :                 need_paren = false;
    5700             :             else
    5701           0 :                 need_paren = true;
    5702             :         }
    5703             :         else
    5704          80 :             need_paren = false;
    5705             : 
    5706         124 :         if (need_paren)
    5707             :         {
    5708           0 :             appendStringInfoChar(buf, '(');
    5709           0 :             subindent = PRETTYINDENT_STD;
    5710           0 :             appendContextKeyword(context, "", subindent, 0, 0);
    5711             :         }
    5712             :         else
    5713         124 :             subindent = 0;
    5714             : 
    5715         124 :         get_setop_query(op->larg, query, context, resultDesc);
    5716             : 
    5717         124 :         if (need_paren)
    5718           0 :             appendContextKeyword(context, ") ", -subindent, 0, 0);
    5719         124 :         else if (PRETTY_INDENT(context))
    5720         124 :             appendContextKeyword(context, "", -subindent, 0, 0);
    5721             :         else
    5722           0 :             appendStringInfoChar(buf, ' ');
    5723             : 
    5724         124 :         switch (op->op)
    5725             :         {
    5726         124 :             case SETOP_UNION:
    5727         124 :                 appendStringInfoString(buf, "UNION ");
    5728         124 :                 break;
    5729           0 :             case SETOP_INTERSECT:
    5730           0 :                 appendStringInfoString(buf, "INTERSECT ");
    5731           0 :                 break;
    5732           0 :             case SETOP_EXCEPT:
    5733           0 :                 appendStringInfoString(buf, "EXCEPT ");
    5734           0 :                 break;
    5735           0 :             default:
    5736           0 :                 elog(ERROR, "unrecognized set op: %d",
    5737             :                      (int) op->op);
    5738             :         }
    5739         124 :         if (op->all)
    5740         116 :             appendStringInfoString(buf, "ALL ");
    5741             : 
    5742             :         /* Always parenthesize if RHS is another setop */
    5743         124 :         need_paren = IsA(op->rarg, SetOperationStmt);
    5744             : 
    5745             :         /*
    5746             :          * The indentation code here is deliberately a bit different from that
    5747             :          * for the lefthand input, because we want the line breaks in
    5748             :          * different places.
    5749             :          */
    5750         124 :         if (need_paren)
    5751             :         {
    5752           0 :             appendStringInfoChar(buf, '(');
    5753           0 :             subindent = PRETTYINDENT_STD;
    5754             :         }
    5755             :         else
    5756         124 :             subindent = 0;
    5757         124 :         appendContextKeyword(context, "", subindent, 0, 0);
    5758             : 
    5759         124 :         get_setop_query(op->rarg, query, context, resultDesc);
    5760             : 
    5761         124 :         if (PRETTY_INDENT(context))
    5762         124 :             context->indentLevel -= subindent;
    5763         124 :         if (need_paren)
    5764           0 :             appendContextKeyword(context, ")", 0, 0, 0);
    5765             :     }
    5766             :     else
    5767             :     {
    5768           0 :         elog(ERROR, "unrecognized node type: %d",
    5769             :              (int) nodeTag(setOp));
    5770             :     }
    5771         328 : }
    5772             : 
    5773             : /*
    5774             :  * Display a sort/group clause.
    5775             :  *
    5776             :  * Also returns the expression tree, so caller need not find it again.
    5777             :  */
    5778             : static Node *
    5779         310 : get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno,
    5780             :                          deparse_context *context)
    5781             : {
    5782         310 :     StringInfo  buf = context->buf;
    5783             :     TargetEntry *tle;
    5784             :     Node       *expr;
    5785             : 
    5786         310 :     tle = get_sortgroupref_tle(ref, tlist);
    5787         310 :     expr = (Node *) tle->expr;
    5788             : 
    5789             :     /*
    5790             :      * Use column-number form if requested by caller.  Otherwise, if
    5791             :      * expression is a constant, force it to be dumped with an explicit cast
    5792             :      * as decoration --- this is because a simple integer constant is
    5793             :      * ambiguous (and will be misinterpreted by findTargetlistEntry()) if we
    5794             :      * dump it without any decoration.  If it's anything more complex than a
    5795             :      * simple Var, then force extra parens around it, to ensure it can't be
    5796             :      * misinterpreted as a cube() or rollup() construct.
    5797             :      */
    5798         310 :     if (force_colno)
    5799             :     {
    5800             :         Assert(!tle->resjunk);
    5801           0 :         appendStringInfo(buf, "%d", tle->resno);
    5802             :     }
    5803         310 :     else if (expr && IsA(expr, Const))
    5804           0 :         get_const_expr((Const *) expr, context, 1);
    5805         310 :     else if (!expr || IsA(expr, Var))
    5806         292 :         get_rule_expr(expr, context, true);
    5807             :     else
    5808             :     {
    5809             :         /*
    5810             :          * We must force parens for function-like expressions even if
    5811             :          * PRETTY_PAREN is off, since those are the ones in danger of
    5812             :          * misparsing. For other expressions we need to force them only if
    5813             :          * PRETTY_PAREN is on, since otherwise the expression will output them
    5814             :          * itself. (We can't skip the parens.)
    5815             :          */
    5816          36 :         bool        need_paren = (PRETTY_PAREN(context)
    5817          18 :                                   || IsA(expr, FuncExpr)
    5818          14 :                                   || IsA(expr, Aggref)
    5819          36 :                                   || IsA(expr, WindowFunc));
    5820             : 
    5821          18 :         if (need_paren)
    5822           4 :             appendStringInfoChar(context->buf, '(');
    5823          18 :         get_rule_expr(expr, context, true);
    5824          18 :         if (need_paren)
    5825           4 :             appendStringInfoChar(context->buf, ')');
    5826             :     }
    5827             : 
    5828         310 :     return expr;
    5829             : }
    5830             : 
    5831             : /*
    5832             :  * Display a GroupingSet
    5833             :  */
    5834             : static void
    5835          12 : get_rule_groupingset(GroupingSet *gset, List *targetlist,
    5836             :                      bool omit_parens, deparse_context *context)
    5837             : {
    5838             :     ListCell   *l;
    5839          12 :     StringInfo  buf = context->buf;
    5840          12 :     bool        omit_child_parens = true;
    5841          12 :     char       *sep = "";
    5842             : 
    5843          12 :     switch (gset->kind)
    5844             :     {
    5845           0 :         case GROUPING_SET_EMPTY:
    5846           0 :             appendStringInfoString(buf, "()");
    5847           0 :             return;
    5848             : 
    5849           8 :         case GROUPING_SET_SIMPLE:
    5850             :             {
    5851           8 :                 if (!omit_parens || list_length(gset->content) != 1)
    5852           8 :                     appendStringInfoChar(buf, '(');
    5853             : 
    5854          28 :                 foreach(l, gset->content)
    5855             :                 {
    5856          20 :                     Index       ref = lfirst_int(l);
    5857             : 
    5858          20 :                     appendStringInfoString(buf, sep);
    5859          20 :                     get_rule_sortgroupclause(ref, targetlist,
    5860             :                                              false, context);
    5861          20 :                     sep = ", ";
    5862             :                 }
    5863             : 
    5864           8 :                 if (!omit_parens || list_length(gset->content) != 1)
    5865           8 :                     appendStringInfoChar(buf, ')');
    5866             :             }
    5867           8 :             return;
    5868             : 
    5869           4 :         case GROUPING_SET_ROLLUP:
    5870           4 :             appendStringInfoString(buf, "ROLLUP(");
    5871           4 :             break;
    5872           0 :         case GROUPING_SET_CUBE:
    5873           0 :             appendStringInfoString(buf, "CUBE(");
    5874           0 :             break;
    5875           0 :         case GROUPING_SET_SETS:
    5876           0 :             appendStringInfoString(buf, "GROUPING SETS (");
    5877           0 :             omit_child_parens = false;
    5878           0 :             break;
    5879             :     }
    5880             : 
    5881          12 :     foreach(l, gset->content)
    5882             :     {
    5883           8 :         appendStringInfoString(buf, sep);
    5884           8 :         get_rule_groupingset(lfirst(l), targetlist, omit_child_parens, context);
    5885           8 :         sep = ", ";
    5886             :     }
    5887             : 
    5888           4 :     appendStringInfoChar(buf, ')');
    5889             : }
    5890             : 
    5891             : /*
    5892             :  * Display an ORDER BY list.
    5893             :  */
    5894             : static void
    5895         168 : get_rule_orderby(List *orderList, List *targetList,
    5896             :                  bool force_colno, deparse_context *context)
    5897             : {
    5898         168 :     StringInfo  buf = context->buf;
    5899             :     const char *sep;
    5900             :     ListCell   *l;
    5901             : 
    5902         168 :     sep = "";
    5903         354 :     foreach(l, orderList)
    5904             :     {
    5905         186 :         SortGroupClause *srt = (SortGroupClause *) lfirst(l);
    5906             :         Node       *sortexpr;
    5907             :         Oid         sortcoltype;
    5908             :         TypeCacheEntry *typentry;
    5909             : 
    5910         186 :         appendStringInfoString(buf, sep);
    5911         186 :         sortexpr = get_rule_sortgroupclause(srt->tleSortGroupRef, targetList,
    5912             :                                             force_colno, context);
    5913         186 :         sortcoltype = exprType(sortexpr);
    5914             :         /* See whether operator is default < or > for datatype */
    5915         186 :         typentry = lookup_type_cache(sortcoltype,
    5916             :                                      TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
    5917         186 :         if (srt->sortop == typentry->lt_opr)
    5918             :         {
    5919             :             /* ASC is default, so emit nothing for it */
    5920         164 :             if (srt->nulls_first)
    5921           0 :                 appendStringInfoString(buf, " NULLS FIRST");
    5922             :         }
    5923          22 :         else if (srt->sortop == typentry->gt_opr)
    5924             :         {
    5925           8 :             appendStringInfoString(buf, " DESC");
    5926             :             /* DESC defaults to NULLS FIRST */
    5927           8 :             if (!srt->nulls_first)
    5928           2 :                 appendStringInfoString(buf, " NULLS LAST");
    5929             :         }
    5930             :         else
    5931             :         {
    5932          14 :             appendStringInfo(buf, " USING %s",
    5933             :                              generate_operator_name(srt->sortop,
    5934             :                                                     sortcoltype,
    5935             :                                                     sortcoltype));
    5936             :             /* be specific to eliminate ambiguity */
    5937          14 :             if (srt->nulls_first)
    5938           0 :                 appendStringInfoString(buf, " NULLS FIRST");
    5939             :             else
    5940          14 :                 appendStringInfoString(buf, " NULLS LAST");
    5941             :         }
    5942         186 :         sep = ", ";
    5943             :     }
    5944         168 : }
    5945             : 
    5946             : /*
    5947             :  * Display a WINDOW clause.
    5948             :  *
    5949             :  * Note that the windowClause list might contain only anonymous window
    5950             :  * specifications, in which case we should print nothing here.
    5951             :  */
    5952             : static void
    5953          28 : get_rule_windowclause(Query *query, deparse_context *context)
    5954             : {
    5955          28 :     StringInfo  buf = context->buf;
    5956             :     const char *sep;
    5957             :     ListCell   *l;
    5958             : 
    5959          28 :     sep = NULL;
    5960          56 :     foreach(l, query->windowClause)
    5961             :     {
    5962          28 :         WindowClause *wc = (WindowClause *) lfirst(l);
    5963             : 
    5964          28 :         if (wc->name == NULL)
    5965          28 :             continue;           /* ignore anonymous windows */
    5966             : 
    5967           0 :         if (sep == NULL)
    5968           0 :             appendContextKeyword(context, " WINDOW ",
    5969             :                                  -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    5970             :         else
    5971           0 :             appendStringInfoString(buf, sep);
    5972             : 
    5973           0 :         appendStringInfo(buf, "%s AS ", quote_identifier(wc->name));
    5974             : 
    5975           0 :         get_rule_windowspec(wc, query->targetList, context);
    5976             : 
    5977           0 :         sep = ", ";
    5978             :     }
    5979          28 : }
    5980             : 
    5981             : /*
    5982             :  * Display a window definition
    5983             :  */
    5984             : static void
    5985          28 : get_rule_windowspec(WindowClause *wc, List *targetList,
    5986             :                     deparse_context *context)
    5987             : {
    5988          28 :     StringInfo  buf = context->buf;
    5989          28 :     bool        needspace = false;
    5990             :     const char *sep;
    5991             :     ListCell   *l;
    5992             : 
    5993          28 :     appendStringInfoChar(buf, '(');
    5994          28 :     if (wc->refname)
    5995             :     {
    5996           0 :         appendStringInfoString(buf, quote_identifier(wc->refname));
    5997           0 :         needspace = true;
    5998             :     }
    5999             :     /* partition clauses are always inherited, so only print if no refname */
    6000          28 :     if (wc->partitionClause && !wc->refname)
    6001             :     {
    6002           0 :         if (needspace)
    6003           0 :             appendStringInfoChar(buf, ' ');
    6004           0 :         appendStringInfoString(buf, "PARTITION BY ");
    6005           0 :         sep = "";
    6006           0 :         foreach(l, wc->partitionClause)
    6007             :         {
    6008           0 :             SortGroupClause *grp = (SortGroupClause *) lfirst(l);
    6009             : 
    6010           0 :             appendStringInfoString(buf, sep);
    6011           0 :             get_rule_sortgroupclause(grp->tleSortGroupRef, targetList,
    6012             :                                      false, context);
    6013           0 :             sep = ", ";
    6014             :         }
    6015           0 :         needspace = true;
    6016             :     }
    6017             :     /* print ordering clause only if not inherited */
    6018          28 :     if (wc->orderClause && !wc->copiedOrder)
    6019             :     {
    6020          28 :         if (needspace)
    6021           0 :             appendStringInfoChar(buf, ' ');
    6022          28 :         appendStringInfoString(buf, "ORDER BY ");
    6023          28 :         get_rule_orderby(wc->orderClause, targetList, false, context);
    6024          28 :         needspace = true;
    6025             :     }
    6026             :     /* framing clause is never inherited, so print unless it's default */
    6027          28 :     if (wc->frameOptions & FRAMEOPTION_NONDEFAULT)
    6028             :     {
    6029          28 :         if (needspace)
    6030          28 :             appendStringInfoChar(buf, ' ');
    6031          28 :         if (wc->frameOptions & FRAMEOPTION_RANGE)
    6032           4 :             appendStringInfoString(buf, "RANGE ");
    6033          24 :         else if (wc->frameOptions & FRAMEOPTION_ROWS)
    6034          20 :             appendStringInfoString(buf, "ROWS ");
    6035           4 :         else if (wc->frameOptions & FRAMEOPTION_GROUPS)
    6036           4 :             appendStringInfoString(buf, "GROUPS ");
    6037             :         else
    6038             :             Assert(false);
    6039          28 :         if (wc->frameOptions & FRAMEOPTION_BETWEEN)
    6040          28 :             appendStringInfoString(buf, "BETWEEN ");
    6041          28 :         if (wc->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
    6042           0 :             appendStringInfoString(buf, "UNBOUNDED PRECEDING ");
    6043          28 :         else if (wc->frameOptions & FRAMEOPTION_START_CURRENT_ROW)
    6044           0 :             appendStringInfoString(buf, "CURRENT ROW ");
    6045          28 :         else if (wc->frameOptions & FRAMEOPTION_START_OFFSET)
    6046             :         {
    6047          28 :             get_rule_expr(wc->startOffset, context, false);
    6048          28 :             if (wc->frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
    6049          28 :                 appendStringInfoString(buf, " PRECEDING ");
    6050           0 :             else if (wc->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
    6051           0 :                 appendStringInfoString(buf, " FOLLOWING ");
    6052             :             else
    6053             :                 Assert(false);
    6054             :         }
    6055             :         else
    6056             :             Assert(false);
    6057          28 :         if (wc->frameOptions & FRAMEOPTION_BETWEEN)
    6058             :         {
    6059          28 :             appendStringInfoString(buf, "AND ");
    6060          28 :             if (wc->frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
    6061           0 :                 appendStringInfoString(buf, "UNBOUNDED FOLLOWING ");
    6062          28 :             else if (wc->frameOptions & FRAMEOPTION_END_CURRENT_ROW)
    6063           0 :                 appendStringInfoString(buf, "CURRENT ROW ");
    6064          28 :             else if (wc->frameOptions & FRAMEOPTION_END_OFFSET)
    6065             :             {
    6066          28 :                 get_rule_expr(wc->endOffset, context, false);
    6067          28 :                 if (wc->frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
    6068           0 :                     appendStringInfoString(buf, " PRECEDING ");
    6069          28 :                 else if (wc->frameOptions & FRAMEOPTION_END_OFFSET_FOLLOWING)
    6070          28 :                     appendStringInfoString(buf, " FOLLOWING ");
    6071             :                 else
    6072             :                     Assert(false);
    6073             :             }
    6074             :             else
    6075             :                 Assert(false);
    6076             :         }
    6077          28 :         if (wc->frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
    6078           4 :             appendStringInfoString(buf, "EXCLUDE CURRENT ROW ");
    6079          24 :         else if (wc->frameOptions & FRAMEOPTION_EXCLUDE_GROUP)
    6080           4 :             appendStringInfoString(buf, "EXCLUDE GROUP ");
    6081          20 :         else if (wc->frameOptions & FRAMEOPTION_EXCLUDE_TIES)
    6082           4 :             appendStringInfoString(buf, "EXCLUDE TIES ");
    6083             :         /* we will now have a trailing space; remove it */
    6084          28 :         buf->len--;
    6085             :     }
    6086          28 :     appendStringInfoChar(buf, ')');
    6087          28 : }
    6088             : 
    6089             : /* ----------
    6090             :  * get_insert_query_def         - Parse back an INSERT parsetree
    6091             :  * ----------
    6092             :  */
    6093             : static void
    6094         216 : get_insert_query_def(Query *query, deparse_context *context)
    6095             : {
    6096         216 :     StringInfo  buf = context->buf;
    6097         216 :     RangeTblEntry *select_rte = NULL;
    6098         216 :     RangeTblEntry *values_rte = NULL;
    6099             :     RangeTblEntry *rte;
    6100             :     char       *sep;
    6101             :     ListCell   *l;
    6102             :     List       *strippedexprs;
    6103             : 
    6104             :     /* Insert the WITH clause if given */
    6105         216 :     get_with_clause(query, context);
    6106             : 
    6107             :     /*
    6108             :      * If it's an INSERT ... SELECT or multi-row VALUES, there will be a
    6109             :      * single RTE for the SELECT or VALUES.  Plain VALUES has neither.
    6110             :      */
    6111         876 :     foreach(l, query->rtable)
    6112             :     {
    6113         660 :         rte = (RangeTblEntry *) lfirst(l);
    6114             : 
    6115         660 :         if (rte->rtekind == RTE_SUBQUERY)
    6116             :         {
    6117          14 :             if (select_rte)
    6118           0 :                 elog(ERROR, "too many subquery RTEs in INSERT");
    6119          14 :             select_rte = rte;
    6120             :         }
    6121             : 
    6122         660 :         if (rte->rtekind == RTE_VALUES)
    6123             :         {
    6124          18 :             if (values_rte)
    6125           0 :                 elog(ERROR, "too many values RTEs in INSERT");
    6126          18 :             values_rte = rte;
    6127             :         }
    6128             :     }
    6129         216 :     if (select_rte && values_rte)
    6130           0 :         elog(ERROR, "both subquery and values RTEs in INSERT");
    6131             : 
    6132             :     /*
    6133             :      * Start the query with INSERT INTO relname
    6134             :      */
    6135         216 :     rte = rt_fetch(query->resultRelation, query->rtable);
    6136             :     Assert(rte->rtekind == RTE_RELATION);
    6137             : 
    6138         216 :     if (PRETTY_INDENT(context))
    6139             :     {
    6140         216 :         context->indentLevel += PRETTYINDENT_STD;
    6141         216 :         appendStringInfoChar(buf, ' ');
    6142             :     }
    6143         216 :     appendStringInfo(buf, "INSERT INTO %s ",
    6144             :                      generate_relation_name(rte->relid, NIL));
    6145             :     /* INSERT requires AS keyword for target alias */
    6146         216 :     if (rte->alias != NULL)
    6147          10 :         appendStringInfo(buf, "AS %s ",
    6148          10 :                          quote_identifier(rte->alias->aliasname));
    6149             : 
    6150             :     /*
    6151             :      * Add the insert-column-names list.  Any indirection decoration needed on
    6152             :      * the column names can be inferred from the top targetlist.
    6153             :      */
    6154         216 :     strippedexprs = NIL;
    6155         216 :     sep = "";
    6156         216 :     if (query->targetList)
    6157         216 :         appendStringInfoChar(buf, '(');
    6158         838 :     foreach(l, query->targetList)
    6159             :     {
    6160         622 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
    6161             : 
    6162         622 :         if (tle->resjunk)
    6163           0 :             continue;           /* ignore junk entries */
    6164             : 
    6165         622 :         appendStringInfoString(buf, sep);
    6166         622 :         sep = ", ";
    6167             : 
    6168             :         /*
    6169             :          * Put out name of target column; look in the catalogs, not at
    6170             :          * tle->resname, since resname will fail to track RENAME.
    6171             :          */
    6172         622 :         appendStringInfoString(buf,
    6173         622 :                                quote_identifier(get_attname(rte->relid,
    6174         622 :                                                             tle->resno,
    6175             :                                                             false)));
    6176             : 
    6177             :         /*
    6178             :          * Print any indirection needed (subfields or subscripts), and strip
    6179             :          * off the top-level nodes representing the indirection assignments.
    6180             :          * Add the stripped expressions to strippedexprs.  (If it's a
    6181             :          * single-VALUES statement, the stripped expressions are the VALUES to
    6182             :          * print below.  Otherwise they're just Vars and not really
    6183             :          * interesting.)
    6184             :          */
    6185         622 :         strippedexprs = lappend(strippedexprs,
    6186         622 :                                 processIndirection((Node *) tle->expr,
    6187             :                                                    context));
    6188             :     }
    6189         216 :     if (query->targetList)
    6190         216 :         appendStringInfoString(buf, ") ");
    6191             : 
    6192         216 :     if (query->override)
    6193             :     {
    6194           0 :         if (query->override == OVERRIDING_SYSTEM_VALUE)
    6195           0 :             appendStringInfoString(buf, "OVERRIDING SYSTEM VALUE ");
    6196           0 :         else if (query->override == OVERRIDING_USER_VALUE)
    6197           0 :             appendStringInfoString(buf, "OVERRIDING USER VALUE ");
    6198             :     }
    6199             : 
    6200         216 :     if (select_rte)
    6201             :     {
    6202             :         /* Add the SELECT */
    6203          14 :         get_query_def(select_rte->subquery, buf, NIL, NULL,
    6204             :                       context->prettyFlags, context->wrapColumn,
    6205             :                       context->indentLevel);
    6206             :     }
    6207         202 :     else if (values_rte)
    6208             :     {
    6209             :         /* Add the multi-VALUES expression lists */
    6210          18 :         get_values_def(values_rte->values_lists, context);
    6211             :     }
    6212         184 :     else if (strippedexprs)
    6213             :     {
    6214             :         /* Add the single-VALUES expression list */
    6215         184 :         appendContextKeyword(context, "VALUES (",
    6216             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
    6217         184 :         get_rule_expr((Node *) strippedexprs, context, false);
    6218         184 :         appendStringInfoChar(buf, ')');
    6219             :     }
    6220             :     else
    6221             :     {
    6222             :         /* No expressions, so it must be DEFAULT VALUES */
    6223           0 :         appendStringInfoString(buf, "DEFAULT VALUES");
    6224             :     }
    6225             : 
    6226             :     /* Add ON CONFLICT if present */
    6227         216 :     if (query->onConflict)
    6228             :     {
    6229          20 :         OnConflictExpr *confl = query->onConflict;
    6230             : 
    6231          20 :         appendStringInfoString(buf, " ON CONFLICT");
    6232             : 
    6233          20 :         if (confl->arbiterElems)
    6234             :         {
    6235             :             /* Add the single-VALUES expression list */
    6236          16 :             appendStringInfoChar(buf, '(');
    6237          16 :             get_rule_expr((Node *) confl->arbiterElems, context, false);
    6238          16 :             appendStringInfoChar(buf, ')');
    6239             : 
    6240             :             /* Add a WHERE clause (for partial indexes) if given */
    6241          16 :             if (confl->arbiterWhere != NULL)
    6242             :             {
    6243             :                 bool        save_varprefix;
    6244             : 
    6245             :                 /*
    6246             :                  * Force non-prefixing of Vars, since parser assumes that they
    6247             :                  * belong to target relation.  WHERE clause does not use
    6248             :                  * InferenceElem, so this is separately required.
    6249             :                  */
    6250           8 :                 save_varprefix = context->varprefix;
    6251           8 :                 context->varprefix = false;
    6252             : 
    6253           8 :                 appendContextKeyword(context, " WHERE ",
    6254             :                                      -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6255           8 :                 get_rule_expr(confl->arbiterWhere, context, false);
    6256             : 
    6257           8 :                 context->varprefix = save_varprefix;
    6258             :             }
    6259             :         }
    6260           4 :         else if (OidIsValid(confl->constraint))
    6261             :         {
    6262           0 :             char       *constraint = get_constraint_name(confl->constraint);
    6263             : 
    6264           0 :             if (!constraint)
    6265           0 :                 elog(ERROR, "cache lookup failed for constraint %u",
    6266             :                      confl->constraint);
    6267           0 :             appendStringInfo(buf, " ON CONSTRAINT %s",
    6268             :                              quote_identifier(constraint));
    6269             :         }
    6270             : 
    6271          20 :         if (confl->action == ONCONFLICT_NOTHING)
    6272             :         {
    6273          12 :             appendStringInfoString(buf, " DO NOTHING");
    6274             :         }
    6275             :         else
    6276             :         {
    6277           8 :             appendStringInfoString(buf, " DO UPDATE SET ");
    6278             :             /* Deparse targetlist */
    6279           8 :             get_update_query_targetlist_def(query, confl->onConflictSet,
    6280             :                                             context, rte);
    6281             : 
    6282             :             /* Add a WHERE clause if given */
    6283           8 :             if (confl->onConflictWhere != NULL)
    6284             :             {
    6285           8 :                 appendContextKeyword(context, " WHERE ",
    6286             :                                      -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6287           8 :                 get_rule_expr(confl->onConflictWhere, context, false);
    6288             :             }
    6289             :         }
    6290             :     }
    6291             : 
    6292             :     /* Add RETURNING if present */
    6293         216 :     if (query->returningList)
    6294             :     {
    6295          30 :         appendContextKeyword(context, " RETURNING",
    6296             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6297          30 :         get_target_list(query->returningList, context, NULL);
    6298             :     }
    6299         216 : }
    6300             : 
    6301             : 
    6302             : /* ----------
    6303             :  * get_update_query_def         - Parse back an UPDATE parsetree
    6304             :  * ----------
    6305             :  */
    6306             : static void
    6307          88 : get_update_query_def(Query *query, deparse_context *context)
    6308             : {
    6309          88 :     StringInfo  buf = context->buf;
    6310             :     RangeTblEntry *rte;
    6311             : 
    6312             :     /* Insert the WITH clause if given */
    6313          88 :     get_with_clause(query, context);
    6314             : 
    6315             :     /*
    6316             :      * Start the query with UPDATE relname SET
    6317             :      */
    6318          88 :     rte = rt_fetch(query->resultRelation, query->rtable);
    6319             :     Assert(rte->rtekind == RTE_RELATION);
    6320          88 :     if (PRETTY_INDENT(context))
    6321             :     {
    6322          88 :         appendStringInfoChar(buf, ' ');
    6323          88 :         context->indentLevel += PRETTYINDENT_STD;
    6324             :     }
    6325         176 :     appendStringInfo(buf, "UPDATE %s%s",
    6326          88 :                      only_marker(rte),
    6327             :                      generate_relation_name(rte->relid, NIL));
    6328          88 :     if (rte->alias != NULL)
    6329          14 :         appendStringInfo(buf, " %s",
    6330          14 :                          quote_identifier(rte->alias->aliasname));
    6331          88 :     appendStringInfoString(buf, " SET ");
    6332             : 
    6333             :     /* Deparse targetlist */
    6334          88 :     get_update_query_targetlist_def(query, query->targetList, context, rte);
    6335             : 
    6336             :     /* Add the FROM clause if needed */
    6337          88 :     get_from_clause(query, " FROM ", context);
    6338             : 
    6339             :     /* Add a WHERE clause if given */
    6340          88 :     if (query->jointree->quals != NULL)
    6341             :     {
    6342          88 :         appendContextKeyword(context, " WHERE ",
    6343             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6344          88 :         get_rule_expr(query->jointree->quals, context, false);
    6345             :     }
    6346             : 
    6347             :     /* Add RETURNING if present */
    6348          88 :     if (query->returningList)
    6349             :     {
    6350           4 :         appendContextKeyword(context, " RETURNING",
    6351             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6352           4 :         get_target_list(query->returningList, context, NULL);
    6353             :     }
    6354          88 : }
    6355             : 
    6356             : 
    6357             : /* ----------
    6358             :  * get_update_query_targetlist_def          - Parse back an UPDATE targetlist
    6359             :  * ----------
    6360             :  */
    6361             : static void
    6362          96 : get_update_query_targetlist_def(Query *query, List *targetList,
    6363             :                                 deparse_context *context, RangeTblEntry *rte)
    6364             : {
    6365          96 :     StringInfo  buf = context->buf;
    6366             :     ListCell   *l;
    6367             :     ListCell   *next_ma_cell;
    6368             :     int         remaining_ma_columns;
    6369             :     const char *sep;
    6370             :     SubLink    *cur_ma_sublink;
    6371             :     List       *ma_sublinks;
    6372             : 
    6373             :     /*
    6374             :      * Prepare to deal with MULTIEXPR assignments: collect the source SubLinks
    6375             :      * into a list.  We expect them to appear, in ID order, in resjunk tlist
    6376             :      * entries.
    6377             :      */
    6378          96 :     ma_sublinks = NIL;
    6379          96 :     if (query->hasSubLinks)      /* else there can't be any */
    6380             :     {
    6381          20 :         foreach(l, targetList)
    6382             :         {
    6383          16 :             TargetEntry *tle = (TargetEntry *) lfirst(l);
    6384             : 
    6385          16 :             if (tle->resjunk && IsA(tle->expr, SubLink))
    6386             :             {
    6387           4 :                 SubLink    *sl = (SubLink *) tle->expr;
    6388             : 
    6389           4 :                 if (sl->subLinkType == MULTIEXPR_SUBLINK)
    6390             :                 {
    6391           4 :                     ma_sublinks = lappend(ma_sublinks, sl);
    6392             :                     Assert(sl->subLinkId == list_length(ma_sublinks));
    6393             :                 }
    6394             :             }
    6395             :         }
    6396             :     }
    6397          96 :     next_ma_cell = list_head(ma_sublinks);
    6398          96 :     cur_ma_sublink = NULL;
    6399          96 :     remaining_ma_columns = 0;
    6400             : 
    6401             :     /* Add the comma separated list of 'attname = value' */
    6402          96 :     sep = "";
    6403         270 :     foreach(l, targetList)
    6404             :     {
    6405         174 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
    6406             :         Node       *expr;
    6407             : 
    6408         174 :         if (tle->resjunk)
    6409           4 :             continue;           /* ignore junk entries */
    6410             : 
    6411             :         /* Emit separator (OK whether we're in multiassignment or not) */
    6412         170 :         appendStringInfoString(buf, sep);
    6413         170 :         sep = ", ";
    6414             : 
    6415             :         /*
    6416             :          * Check to see if we're starting a multiassignment group: if so,
    6417             :          * output a left paren.
    6418             :          */
    6419         170 :         if (next_ma_cell != NULL && cur_ma_sublink == NULL)
    6420             :         {
    6421             :             /*
    6422             :              * We must dig down into the expr to see if it's a PARAM_MULTIEXPR
    6423             :              * Param.  That could be buried under FieldStores and
    6424             :              * SubscriptingRefs and CoerceToDomains (cf processIndirection()),
    6425             :              * and underneath those there could be an implicit type coercion.
    6426             :              * Because we would ignore implicit type coercions anyway, we
    6427             :              * don't need to be as careful as processIndirection() is about
    6428             :              * descending past implicit CoerceToDomains.
    6429             :              */
    6430           4 :             expr = (Node *) tle->expr;
    6431           8 :             while (expr)
    6432             :             {
    6433           8 :                 if (IsA(expr, FieldStore))
    6434             :                 {
    6435           0 :                     FieldStore *fstore = (FieldStore *) expr;
    6436             : 
    6437           0 :                     expr = (Node *) linitial(fstore->newvals);
    6438             :                 }
    6439           8 :                 else if (IsA(expr, SubscriptingRef))
    6440             :                 {
    6441           4 :                     SubscriptingRef *sbsref = (SubscriptingRef *) expr;
    6442             : 
    6443           4 :                     if (sbsref->refassgnexpr == NULL)
    6444           0 :                         break;
    6445             : 
    6446           4 :                     expr = (Node *) sbsref->refassgnexpr;
    6447             :                 }
    6448           4 :                 else if (IsA(expr, CoerceToDomain))
    6449             :                 {
    6450           0 :                     CoerceToDomain *cdomain = (CoerceToDomain *) expr;
    6451             : 
    6452           0 :                     if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
    6453           0 :                         break;
    6454           0 :                     expr = (Node *) cdomain->arg;
    6455             :                 }
    6456             :                 else
    6457           4 :                     break;
    6458             :             }
    6459           4 :             expr = strip_implicit_coercions(expr);
    6460             : 
    6461           4 :             if (expr && IsA(expr, Param) &&
    6462           4 :                 ((Param *) expr)->paramkind == PARAM_MULTIEXPR)
    6463             :             {
    6464           4 :                 cur_ma_sublink = (SubLink *) lfirst(next_ma_cell);
    6465           4 :                 next_ma_cell = lnext(ma_sublinks, next_ma_cell);
    6466           4 :                 remaining_ma_columns = count_nonjunk_tlist_entries(((Query *) cur_ma_sublink->subselect)->targetList);
    6467             :                 Assert(((Param *) expr)->paramid ==
    6468             :                        ((cur_ma_sublink->subLinkId << 16) | 1));
    6469           4 :                 appendStringInfoChar(buf, '(');
    6470             :             }
    6471             :         }
    6472             : 
    6473             :         /*
    6474             :          * Put out name of target column; look in the catalogs, not at
    6475             :          * tle->resname, since resname will fail to track RENAME.
    6476             :          */
    6477         170 :         appendStringInfoString(buf,
    6478         170 :                                quote_identifier(get_attname(rte->relid,
    6479         170 :                                                             tle->resno,
    6480             :                                                             false)));
    6481             : 
    6482             :         /*
    6483             :          * Print any indirection needed (subfields or subscripts), and strip
    6484             :          * off the top-level nodes representing the indirection assignments.
    6485             :          */
    6486         170 :         expr = processIndirection((Node *) tle->expr, context);
    6487             : 
    6488             :         /*
    6489             :          * If we're in a multiassignment, skip printing anything more, unless
    6490             :          * this is the last column; in which case, what we print should be the
    6491             :          * sublink, not the Param.
    6492             :          */
    6493         170 :         if (cur_ma_sublink != NULL)
    6494             :         {
    6495          12 :             if (--remaining_ma_columns > 0)
    6496           8 :                 continue;       /* not the last column of multiassignment */
    6497           4 :             appendStringInfoChar(buf, ')');
    6498           4 :             expr = (Node *) cur_ma_sublink;
    6499           4 :             cur_ma_sublink = NULL;
    6500             :         }
    6501             : 
    6502         162 :         appendStringInfoString(buf, " = ");
    6503             : 
    6504         162 :         get_rule_expr(expr, context, false);
    6505             :     }
    6506          96 : }
    6507             : 
    6508             : 
    6509             : /* ----------
    6510             :  * get_delete_query_def         - Parse back a DELETE parsetree
    6511             :  * ----------
    6512             :  */
    6513             : static void
    6514          50 : get_delete_query_def(Query *query, deparse_context *context)
    6515             : {
    6516          50 :     StringInfo  buf = context->buf;
    6517             :     RangeTblEntry *rte;
    6518             : 
    6519             :     /* Insert the WITH clause if given */
    6520          50 :     get_with_clause(query, context);
    6521             : 
    6522             :     /*
    6523             :      * Start the query with DELETE FROM relname
    6524             :      */
    6525          50 :     rte = rt_fetch(query->resultRelation, query->rtable);
    6526             :     Assert(rte->rtekind == RTE_RELATION);
    6527          50 :     if (PRETTY_INDENT(context))
    6528             :     {
    6529          50 :         appendStringInfoChar(buf, ' ');
    6530          50 :         context->indentLevel += PRETTYINDENT_STD;
    6531             :     }
    6532         100 :     appendStringInfo(buf, "DELETE FROM %s%s",
    6533          50 :                      only_marker(rte),
    6534             :                      generate_relation_name(rte->relid, NIL));
    6535          50 :     if (rte->alias != NULL)
    6536           0 :         appendStringInfo(buf, " %s",
    6537           0 :                          quote_identifier(rte->alias->aliasname));
    6538             : 
    6539             :     /* Add the USING clause if given */
    6540          50 :     get_from_clause(query, " USING ", context);
    6541             : 
    6542             :     /* Add a WHERE clause if given */
    6543          50 :     if (query->jointree->quals != NULL)
    6544             :     {
    6545          50 :         appendContextKeyword(context, " WHERE ",
    6546             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6547          50 :         get_rule_expr(query->jointree->quals, context, false);
    6548             :     }
    6549             : 
    6550             :     /* Add RETURNING if present */
    6551          50 :     if (query->returningList)
    6552             :     {
    6553           0 :         appendContextKeyword(context, " RETURNING",
    6554             :                              -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
    6555           0 :         get_target_list(query->returningList, context, NULL);
    6556             :     }
    6557          50 : }
    6558             : 
    6559             : 
    6560             : /* ----------
    6561             :  * get_utility_query_def            - Parse back a UTILITY parsetree
    6562             :  * ----------
    6563             :  */
    6564             : static void
    6565          14 : get_utility_query_def(Query *query, deparse_context *context)
    6566             : {
    6567          14 :     StringInfo  buf = context->buf;
    6568             : 
    6569          14 :     if (query->utilityStmt && IsA(query->utilityStmt, NotifyStmt))
    6570          14 :     {
    6571          14 :         NotifyStmt *stmt = (NotifyStmt *) query->utilityStmt;
    6572             : 
    6573          14 :         appendContextKeyword(context, "",
    6574             :                              0, PRETTYINDENT_STD, 1);
    6575          14 :         appendStringInfo(buf, "NOTIFY %s",
    6576          14 :                          quote_identifier(stmt->conditionname));
    6577          14 :         if (stmt->payload)
    6578             :         {
    6579           0 :             appendStringInfoString(buf, ", ");
    6580           0 :             simple_quote_literal(buf, stmt->payload);
    6581             :         }
    6582             :     }
    6583             :     else
    6584             :     {
    6585             :         /* Currently only NOTIFY utility commands can appear in rules */
    6586           0 :         elog(ERROR, "unexpected utility statement type");
    6587             :     }
    6588          14 : }
    6589             : 
    6590             : /*
    6591             :  * Display a Var appropriately.
    6592             :  *
    6593             :  * In some cases (currently only when recursing into an unnamed join)
    6594             :  * the Var's varlevelsup has to be interpreted with respect to a context
    6595             :  * above the current one; levelsup indicates the offset.
    6596             :  *
    6597             :  * If istoplevel is true, the Var is at the top level of a SELECT's
    6598             :  * targetlist, which means we need special treatment of whole-row Vars.
    6599             :  * Instead of the normal "tab.*", we'll print "tab.*::typename", which is a
    6600             :  * dirty hack to prevent "tab.*" from being expanded into multiple columns.
    6601             :  * (The parser will strip the useless coercion, so no inefficiency is added in
    6602             :  * dump and reload.)  We used to print just "tab" in such cases, but that is
    6603             :  * ambiguous and will yield the wrong result if "tab" is also a plain column
    6604             :  * name in the query.
    6605             :  *
    6606             :  * Returns the attname of the Var, or NULL if the Var has no attname (because
    6607             :  * it is a whole-row Var or a subplan output reference).
    6608             :  */
    6609             : static char *
    6610       77086 : get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
    6611             : {
    6612       77086 :     StringInfo  buf = context->buf;
    6613             :     RangeTblEntry *rte;
    6614             :     AttrNumber  attnum;
    6615             :     int         netlevelsup;
    6616             :     deparse_namespace *dpns;
    6617             :     Index       varno;
    6618             :     AttrNumber  varattno;
    6619             :     deparse_columns *colinfo;
    6620             :     char       *refname;
    6621             :     char       *attname;
    6622             : 
    6623             :     /* Find appropriate nesting depth */
    6624       77086 :     netlevelsup = var->varlevelsup + levelsup;
    6625       77086 :     if (netlevelsup >= list_length(context->namespaces))
    6626           0 :         elog(ERROR, "bogus varlevelsup: %d offset %d",
    6627             :              var->varlevelsup, levelsup);
    6628       77086 :     dpns = (deparse_namespace *) list_nth(context->namespaces,
    6629             :                                           netlevelsup);
    6630             : 
    6631             :     /*
    6632             :      * If we have a syntactic referent for the Var, and we're working from a
    6633             :      * parse tree, prefer to use the syntactic referent.  Otherwise, fall back
    6634             :      * on the semantic referent.  (Forcing use of the semantic referent when
    6635             :      * printing plan trees is a design choice that's perhaps more motivated by
    6636             :      * backwards compatibility than anything else.  But it does have the
    6637             :      * advantage of making plans more explicit.)
    6638             :      */
    6639       77086 :     if (var->varnosyn > 0 && dpns->plan == NULL)
    6640             :     {
    6641       14622 :         varno = var->varnosyn;
    6642       14622 :         varattno = var->varattnosyn;
    6643             :     }
    6644             :     else
    6645             :     {
    6646       62464 :         varno = var->varno;
    6647       62464 :         varattno = var->varattno;
    6648             :     }
    6649             : 
    6650             :     /*
    6651             :      * Try to find the relevant RTE in this rtable.  In a plan tree, it's
    6652             :      * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
    6653             :      * down into the subplans, or INDEX_VAR, which is resolved similarly. Also
    6654             :      * find the aliases previously assigned for this RTE.
    6655             :      */
    6656       77086 :     if (varno >= 1 && varno <= list_length(dpns->rtable))
    6657             :     {
    6658             :         /*
    6659             :          * We might have been asked to map child Vars to some parent relation.
    6660             :          */
    6661       56836 :         if (context->appendparents && dpns->appendrels)
    6662             :         {
    6663        2178 :             Index       pvarno = varno;
    6664        2178 :             AttrNumber  pvarattno = varattno;
    6665        2178 :             AppendRelInfo *appinfo = dpns->appendrels[pvarno];
    6666        2178 :             bool        found = false;
    6667             : 
    6668             :             /* Only map up to inheritance parents, not UNION ALL appendrels */
    6669        4416 :             while (appinfo &&
    6670        2428 :                    rt_fetch(appinfo->parent_relid,
    6671        2428 :                             dpns->rtable)->rtekind == RTE_RELATION)
    6672             :             {
    6673        2238 :                 found = false;
    6674        2238 :                 if (pvarattno > 0)   /* system columns stay as-is */
    6675             :                 {
    6676        2106 :                     if (pvarattno > appinfo->num_child_cols)
    6677           0 :                         break;  /* safety check */
    6678        2106 :                     pvarattno = appinfo->parent_colnos[pvarattno - 1];
    6679        2106 :                     if (pvarattno == 0)
    6680           0 :                         break;  /* Var is local to child */
    6681             :                 }
    6682             : 
    6683        2238 :                 pvarno = appinfo->parent_relid;
    6684        2238 :                 found = true;
    6685             : 
    6686             :                 /* If the parent is itself a child, continue up. */
    6687             :                 Assert(pvarno > 0 && pvarno <= list_length(dpns->rtable));
    6688        2238 :                 appinfo = dpns->appendrels[pvarno];
    6689             :             }
    6690             : 
    6691             :             /*
    6692             :              * If we found an ancestral rel, and that rel is included in
    6693             :              * appendparents, print that column not the original one.
    6694             :              */
    6695        2178 :             if (found && bms_is_member(pvarno, context->appendparents))
    6696             :             {
    6697        1784 :                 varno = pvarno;
    6698        1784 :                 varattno = pvarattno;
    6699             :             }
    6700             :         }
    6701             : 
    6702       56836 :         rte = rt_fetch(varno, dpns->rtable);
    6703       56836 :         refname = (char *) list_nth(dpns->rtable_names, varno - 1);
    6704       56836 :         colinfo = deparse_columns_fetch(varno, dpns);
    6705       56836 :         attnum = varattno;
    6706             :     }
    6707             :     else
    6708             :     {
    6709       20250 :         resolve_special_varno((Node *) var, context,
    6710             :                               get_special_variable, NULL);
    6711       20250 :         return NULL;
    6712             :     }
    6713             : 
    6714             :     /*
    6715             :      * The planner will sometimes emit Vars referencing resjunk elements of a
    6716             :      * subquery's target list (this is currently only possible if it chooses
    6717             :      * to generate a "physical tlist" for a SubqueryScan or CteScan node).
    6718             :      * Although we prefer to print subquery-referencing Vars using the
    6719             :      * subquery's alias, that's not possible for resjunk items since they have
    6720             :      * no alias.  So in that case, drill down to the subplan and print the
    6721             :      * contents of the referenced tlist item.  This works because in a plan
    6722             :      * tree, such Vars can only occur in a SubqueryScan or CteScan node, and
    6723             :      * we'll have set dpns->inner_plan to reference the child plan node.
    6724             :      */
    6725       58174 :     if ((rte->rtekind == RTE_SUBQUERY || rte->rtekind == RTE_CTE) &&
    6726        1338 :         attnum > list_length(rte->eref->colnames) &&
    6727           2 :         dpns->inner_plan)
    6728             :     {
    6729             :         TargetEntry *tle;
    6730             :         deparse_namespace save_dpns;
    6731             : 
    6732           2 :         tle = get_tle_by_resno(dpns->inner_tlist, attnum);
    6733           2 :         if (!tle)
    6734           0 :             elog(ERROR, "invalid attnum %d for relation \"%s\"",
    6735             :                  attnum, rte->eref->aliasname);
    6736             : 
    6737             :         Assert(netlevelsup == 0);
    6738           2 :         push_child_plan(dpns, dpns->inner_plan, &save_dpns);
    6739             : 
    6740             :         /*
    6741             :          * Force parentheses because our caller probably assumed a Var is a
    6742             :          * simple expression.
    6743             :          */
    6744           2 :         if (!IsA(tle->expr, Var))
    6745           0 :             appendStringInfoChar(buf, '(');
    6746           2 :         get_rule_expr((Node *) tle->expr, context, true);
    6747           2 :         if (!IsA(tle->expr, Var))
    6748           0 :             appendStringInfoChar(buf, ')');
    6749             : 
    6750           2 :         pop_child_plan(dpns, &save_dpns);
    6751           2 :         return NULL;
    6752             :     }
    6753             : 
    6754             :     /*
    6755             :      * If it's an unnamed join, look at the expansion of the alias variable.
    6756             :      * If it's a simple reference to one of the input vars, then recursively
    6757             :      * print the name of that var instead.  When it's not a simple reference,
    6758             :      * we have to just print the unqualified join column name.  (This can only
    6759             :      * happen with "dangerous" merged columns in a JOIN USING; we took pains
    6760             :      * previously to make the unqualified column name unique in such cases.)
    6761             :      *
    6762             :      * This wouldn't work in decompiling plan trees, because we don't store
    6763             :      * joinaliasvars lists after planning; but a plan tree should never
    6764             :      * contain a join alias variable.
    6765             :      */
    6766       56834 :     if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
    6767             :     {
    6768          64 :         if (rte->joinaliasvars == NIL)
    6769           0 :             elog(ERROR, "cannot decompile join alias var in plan tree");
    6770          64 :         if (attnum > 0)
    6771             :         {
    6772             :             Var        *aliasvar;
    6773             : 
    6774          64 :             aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
    6775             :             /* we intentionally don't strip implicit coercions here */
    6776          64 :             if (aliasvar && IsA(aliasvar, Var))
    6777             :             {
    6778           0 :                 return get_variable(aliasvar, var->varlevelsup + levelsup,
    6779             :                                     istoplevel, context);
    6780             :             }
    6781             :         }
    6782             : 
    6783             :         /*
    6784             :          * Unnamed join has no refname.  (Note: since it's unnamed, there is
    6785             :          * no way the user could have referenced it to create a whole-row Var
    6786             :          * for it.  So we don't have to cover that case below.)
    6787             :          */
    6788             :         Assert(refname == NULL);
    6789             :     }
    6790             : 
    6791       56834 :     if (attnum == InvalidAttrNumber)
    6792         492 :         attname = NULL;
    6793       56342 :     else if (attnum > 0)
    6794             :     {
    6795             :         /* Get column name to use from the colinfo struct */
    6796       55788 :         if (attnum > colinfo->num_cols)
    6797           0 :             elog(ERROR, "invalid attnum %d for relation \"%s\"",
    6798             :                  attnum, rte->eref->aliasname);
    6799       55788 :         attname = colinfo->colnames[attnum - 1];
    6800       55788 :         if (attname == NULL)    /* dropped column? */
    6801           0 :             elog(ERROR, "invalid attnum %d for relation \"%s\"",
    6802             :                  attnum, rte->eref->aliasname);
    6803             :     }
    6804             :     else
    6805             :     {
    6806             :         /* System column - name is fixed, get it from the catalog */
    6807         554 :         attname = get_rte_attribute_name(rte, attnum);
    6808             :     }
    6809             : 
    6810       56834 :     if (refname && (context->varprefix || attname == NULL))
    6811             :     {
    6812       31156 :         appendStringInfoString(buf, quote_identifier(refname));
    6813       31156 :         appendStringInfoChar(buf, '.');
    6814             :     }
    6815       56834 :     if (attname)
    6816       56342 :         appendStringInfoString(buf, quote_identifier(attname));
    6817             :     else
    6818             :     {
    6819         492 :         appendStringInfoChar(buf, '*');
    6820         492 :         if (istoplevel)
    6821          20 :             appendStringInfo(buf, "::%s",
    6822             :                              format_type_with_typemod(var->vartype,
    6823             :                                                       var->vartypmod));
    6824             :     }
    6825             : 
    6826       56834 :     return attname;
    6827             : }
    6828             : 
    6829             : /*
    6830             :  * Deparse a Var which references OUTER_VAR, INNER_VAR, or INDEX_VAR.  This
    6831             :  * routine is actually a callback for resolve_special_varno, which handles
    6832             :  * finding the correct TargetEntry.  We get the expression contained in that
    6833             :  * TargetEntry and just need to deparse it, a job we can throw back on
    6834             :  * get_rule_expr.
    6835             :  */
    6836             : static void
    6837       20250 : get_special_variable(Node *node, deparse_context *context, void *callback_arg)
    6838             : {
    6839       20250 :     StringInfo  buf = context->buf;
    6840             : 
    6841             :     /*
    6842             :      * For a non-Var referent, force parentheses because our caller probably
    6843             :      * assumed a Var is a simple expression.
    6844             :      */
    6845       20250 :     if (!IsA(node, Var))
    6846        1782 :         appendStringInfoChar(buf, '(');
    6847       20250 :     get_rule_expr(node, context, true);
    6848       20250 :     if (!IsA(node, Var))
    6849        1782 :         appendStringInfoChar(buf, ')');
    6850       20250 : }
    6851             : 
    6852             : /*
    6853             :  * Chase through plan references to special varnos (OUTER_VAR, INNER_VAR,
    6854             :  * INDEX_VAR) until we find a real Var or some kind of non-Var node; then,
    6855             :  * invoke the callback provided.
    6856             :  */
    6857             : static void
    6858       55730 : resolve_special_varno(Node *node, deparse_context *context,
    6859             :                       rsv_callback callback, void *callback_arg)
    6860             : {
    6861             :     Var        *var;
    6862             :     deparse_namespace *dpns;
    6863             : 
    6864             :     /* This function is recursive, so let's be paranoid. */
    6865       55730 :     check_stack_depth();
    6866             : 
    6867             :     /* If it's not a Var, invoke the callback. */
    6868       55730 :     if (!IsA(node, Var))
    6869             :     {
    6870        1948 :         (*callback) (node, context, callback_arg);
    6871        1948 :         return;
    6872             :     }
    6873             : 
    6874             :     /* Find appropriate nesting depth */
    6875       53782 :     var = (Var *) node;
    6876       53782 :     dpns = (deparse_namespace *) list_nth(context->namespaces,
    6877       53782 :                                           var->varlevelsup);
    6878             : 
    6879             :     /*
    6880             :      * If varno is special, recurse.  (Don't worry about varnosyn; if we're
    6881             :      * here, we already decided not to use that.)
    6882             :      */
    6883       53782 :     if (var->varno == OUTER_VAR && dpns->outer_tlist)
    6884             :     {
    6885             :         TargetEntry *tle;
    6886             :         deparse_namespace save_dpns;
    6887             :         Bitmapset  *save_appendparents;
    6888             : 
    6889       26260 :         tle = get_tle_by_resno(dpns->outer_tlist, var->varattno);
    6890       26260 :         if (!tle)
    6891           0 :             elog(ERROR, "bogus varattno for OUTER_VAR var: %d", var->varattno);
    6892             : 
    6893             :         /*
    6894             :          * If we're descending to the first child of an Append or MergeAppend,
    6895             :          * update appendparents.  This will affect deparsing of all Vars
    6896             :          * appearing within the eventually-resolved subexpression.
    6897             :          */
    6898       26260 :         save_appendparents = context->appendparents;
    6899             : 
    6900       26260 :         if (IsA(dpns->plan, Append))
    6901        2442 :             context->appendparents = bms_union(context->appendparents,
    6902        2442 :                                                ((Append *) dpns->plan)->apprelids);
    6903       23818 :         else if (IsA(dpns->plan, MergeAppend))
    6904         312 :             context->appendparents = bms_union(context->appendparents,
    6905         312 :                                                ((MergeAppend *) dpns->plan)->apprelids);
    6906             : 
    6907       26260 :         push_child_plan(dpns, dpns->outer_plan, &save_dpns);
    6908       26260 :         resolve_special_varno((Node *) tle->expr, context,
    6909             :                               callback, callback_arg);
    6910       26260 :         pop_child_plan(dpns, &save_dpns);
    6911       26260 :         context->appendparents = save_appendparents;
    6912       26260 :         return;
    6913             :     }
    6914       27522 :     else if (var->varno == INNER_VAR && dpns->inner_tlist)
    6915             :     {
    6916             :         TargetEntry *tle;
    6917             :         deparse_namespace save_dpns;
    6918             : 
    6919        6656 :         tle = get_tle_by_resno(dpns->inner_tlist, var->varattno);
    6920        6656 :         if (!tle)
    6921           0 :             elog(ERROR, "bogus varattno for INNER_VAR var: %d", var->varattno);
    6922             : 
    6923        6656 :         push_child_plan(dpns, dpns->inner_plan, &save_dpns);
    6924        6656 :         resolve_special_varno((Node *) tle->expr, context,
    6925             :                               callback, callback_arg);
    6926        6656 :         pop_child_plan(dpns, &save_dpns);
    6927        6656 :         return;
    6928             :     }
    6929       20866 :     else if (var->varno == INDEX_VAR && dpns->index_tlist)
    6930             :     {
    6931             :         TargetEntry *tle;
    6932             : 
    6933        2398 :         tle = get_tle_by_resno(dpns->index_tlist, var->varattno);
    6934        2398 :         if (!tle)
    6935           0 :             elog(ERROR, "bogus varattno for INDEX_VAR var: %d", var->varattno);
    6936             : 
    6937        2398 :         resolve_special_varno((Node *) tle->expr, context,
    6938             :                               callback, callback_arg);
    6939        2398 :         return;
    6940             :     }
    6941       18468 :     else if (var->varno < 1 || var->varno > list_length(dpns->rtable))
    6942           0 :         elog(ERROR, "bogus varno: %d", var->varno);
    6943             : 
    6944             :     /* Not special.  Just invoke the callback. */
    6945       18468 :     (*callback) (node, context, callback_arg);
    6946             : }
    6947             : 
    6948             : /*
    6949             :  * Get the name of a field of an expression of composite type.  The
    6950             :  * expression is usually a Var, but we handle other cases too.
    6951             :  *
    6952             :  * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
    6953             :  *
    6954             :  * This is fairly straightforward when the expression has a named composite
    6955             :  * type; we need only look up the type in the catalogs.  However, the type
    6956             :  * could also be RECORD.  Since no actual table or view column is allowed to
    6957             :  * have type RECORD, a Var of type RECORD must refer to a JOIN or FUNCTION RTE
    6958             :  * or to a subquery output.  We drill down to find the ultimate defining
    6959             :  * expression and attempt to infer the field name from it.  We ereport if we
    6960             :  * can't determine the name.
    6961             :  *
    6962             :  * Similarly, a PARAM of type RECORD has to refer to some expression of
    6963             :  * a determinable composite type.
    6964             :  */
    6965             : static const char *
    6966         176 : get_name_for_var_field(Var *var, int fieldno,
    6967             :                        int levelsup, deparse_context *context)
    6968             : {
    6969             :     RangeTblEntry *rte;
    6970             :     AttrNumber  attnum;
    6971             :     int         netlevelsup;
    6972             :     deparse_namespace *dpns;
    6973             :     Index       varno;
    6974             :     AttrNumber  varattno;
    6975             :     TupleDesc   tupleDesc;
    6976             :     Node       *expr;
    6977             : 
    6978             :     /*
    6979             :      * If it's a RowExpr that was expanded from a whole-row Var, use the
    6980             :      * column names attached to it.
    6981             :      */
    6982         176 :     if (IsA(var, RowExpr))
    6983             :     {
    6984          20 :         RowExpr    *r = (RowExpr *) var;
    6985             : 
    6986          20 :         if (fieldno > 0 && fieldno <= list_length(r->colnames))
    6987          20 :             return strVal(list_nth(r->colnames, fieldno - 1));
    6988             :     }
    6989             : 
    6990             :     /*
    6991             :      * If it's a Param of type RECORD, try to find what the Param refers to.
    6992             :      */
    6993         156 :     if (IsA(var, Param))
    6994             :     {
    6995           8 :         Param      *param = (Param *) var;
    6996             :         ListCell   *ancestor_cell;
    6997             : 
    6998           8 :         expr = find_param_referent(param, context, &dpns, &ancestor_cell);
    6999           8 :         if (expr)
    7000             :         {
    7001             :             /* Found a match, so recurse to decipher the field name */
    7002             :             deparse_namespace save_dpns;
    7003             :             const char *result;
    7004             : 
    7005           8 :             push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
    7006           8 :             result = get_name_for_var_field((Var *) expr, fieldno,
    7007             :                                             0, context);
    7008           8 :             pop_ancestor_plan(dpns, &save_dpns);
    7009           8 :             return result;
    7010             :         }
    7011             :     }
    7012             : 
    7013             :     /*
    7014             :      * If it's a Var of type RECORD, we have to find what the Var refers to;
    7015             :      * if not, we can use get_expr_result_tupdesc().
    7016             :      */
    7017         148 :     if (!IsA(var, Var) ||
    7018         116 :         var->vartype != RECORDOID)
    7019             :     {
    7020          64 :         tupleDesc = get_expr_result_tupdesc((Node *) var, false);
    7021             :         /* Got the tupdesc, so we can extract the field name */
    7022             :         Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
    7023          64 :         return NameStr(TupleDescAttr(tupleDesc, fieldno - 1)->attname);
    7024             :     }
    7025             : 
    7026             :     /* Find appropriate nesting depth */
    7027          84 :     netlevelsup = var->varlevelsup + levelsup;
    7028          84 :     if (netlevelsup >= list_length(context->namespaces))
    7029           0 :         elog(ERROR, "bogus varlevelsup: %d offset %d",
    7030             :              var->varlevelsup, levelsup);
    7031          84 :     dpns = (deparse_namespace *) list_nth(context->namespaces,
    7032             :                                           netlevelsup);
    7033             : 
    7034             :     /*
    7035             :      * If we have a syntactic referent for the Var, and we're working from a
    7036             :      * parse tree, prefer to use the syntactic referent.  Otherwise, fall back
    7037             :      * on the semantic referent.  (See comments in get_variable().)
    7038             :      */
    7039          84 :     if (var->varnosyn > 0 && dpns->plan == NULL)
    7040             :     {
    7041          36 :         varno = var->varnosyn;
    7042          36 :         varattno = var->varattnosyn;
    7043             :     }
    7044             :     else
    7045             :     {
    7046          48 :         varno = var->varno;
    7047          48 :         varattno = var->varattno;
    7048             :     }
    7049             : 
    7050             :     /*
    7051             :      * Try to find the relevant RTE in this rtable.  In a plan tree, it's
    7052             :      * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
    7053             :      * down into the subplans, or INDEX_VAR, which is resolved similarly.
    7054             :      *
    7055             :      * Note: unlike get_variable and resolve_special_varno, we need not worry
    7056             :      * about inheritance mapping: a child Var should have the same datatype as
    7057             :      * its parent, and here we're really only interested in the Var's type.
    7058             :      */
    7059          84 :     if (varno >= 1 && varno <= list_length(dpns->rtable))
    7060             :     {
    7061          48 :         rte = rt_fetch(varno, dpns->rtable);
    7062          48 :         attnum = varattno;
    7063             :     }
    7064          36 :     else if (varno == OUTER_VAR && dpns->outer_tlist)
    7065             :     {
    7066             :         TargetEntry *tle;
    7067             :         deparse_namespace save_dpns;
    7068             :         const char *result;
    7069             : 
    7070          28 :         tle = get_tle_by_resno(dpns->outer_tlist, varattno);
    7071          28 :         if (!tle)
    7072           0 :             elog(ERROR, "bogus varattno for OUTER_VAR var: %d", varattno);
    7073             : 
    7074             :         Assert(netlevelsup == 0);
    7075          28 :         push_child_plan(dpns, dpns->outer_plan, &save_dpns);
    7076             : 
    7077          28 :         result = get_name_for_var_field((Var *) tle->expr, fieldno,
    7078             :                                         levelsup, context);
    7079             : 
    7080          28 :         pop_child_plan(dpns, &save_dpns);
    7081          28 :         return result;
    7082             :     }
    7083           8 :     else if (varno == INNER_VAR && dpns->inner_tlist)
    7084             :     {
    7085             :         TargetEntry *tle;
    7086             :         deparse_namespace save_dpns;
    7087             :         const char *result;
    7088             : 
    7089           8 :         tle = get_tle_by_resno(dpns->inner_tlist, varattno);
    7090           8 :         if (!tle)
    7091           0 :             elog(ERROR, "bogus varattno for INNER_VAR var: %d", varattno);
    7092             : 
    7093             :         Assert(netlevelsup == 0);
    7094           8 :         push_child_plan(dpns, dpns->inner_plan, &save_dpns);
    7095             : 
    7096           8 :         result = get_name_for_var_field((Var *) tle->expr, fieldno,
    7097             :                                         levelsup, context);
    7098             : 
    7099           8 :         pop_child_plan(dpns, &save_dpns);
    7100           8 :         return result;
    7101             :     }
    7102           0 :     else if (varno == INDEX_VAR && dpns->index_tlist)
    7103             :     {
    7104             :         TargetEntry *tle;
    7105             :         const char *result;
    7106             : 
    7107           0 :         tle = get_tle_by_resno(dpns->index_tlist, varattno);
    7108           0 :         if (!tle)
    7109           0 :             elog(ERROR, "bogus varattno for INDEX_VAR var: %d", varattno);
    7110             : 
    7111             :         Assert(netlevelsup == 0);
    7112             : 
    7113           0 :         result = get_name_for_var_field((Var *) tle->expr, fieldno,
    7114             :                                         levelsup, context);
    7115             : 
    7116           0 :         return result;
    7117             :     }
    7118             :     else
    7119             :     {
    7120           0 :         elog(ERROR, "bogus varno: %d", varno);
    7121             :         return NULL;            /* keep compiler quiet */
    7122             :     }
    7123             : 
    7124          48 :     if (attnum == InvalidAttrNumber)
    7125             :     {
    7126             :         /* Var is whole-row reference to RTE, so select the right field */
    7127          16 :         return get_rte_attribute_name(rte, fieldno);
    7128             :     }
    7129             : 
    7130             :     /*
    7131             :      * This part has essentially the same logic as the parser's
    7132             :      * expandRecordVariable() function, but we are dealing with a different
    7133             :      * representation of the input context, and we only need one field name
    7134             :      * not a TupleDesc.  Also, we need special cases for finding subquery and
    7135             :      * CTE subplans when deparsing Plan trees.
    7136             :      */
    7137          32 :     expr = (Node *) var;        /* default if we can't drill down */
    7138             : 
    7139          32 :     switch (rte->rtekind)
    7140             :     {
    7141           0 :         case RTE_RELATION:
    7142             :         case RTE_VALUES:
    7143             :         case RTE_NAMEDTUPLESTORE:
    7144             :         case RTE_RESULT:
    7145             : 
    7146             :             /*
    7147             :              * This case should not occur: a column of a table, values list,
    7148             :              * or ENR shouldn't have type RECORD.  Fall through and fail (most
    7149             :              * likely) at the bottom.
    7150             :              */
    7151           0 :             break;
    7152          12 :         case RTE_SUBQUERY:
    7153             :             /* Subselect-in-FROM: examine sub-select's output expr */
    7154             :             {
    7155          12 :                 if (rte->subquery)
    7156             :                 {
    7157           8 :                     TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
    7158             :                                                         attnum);
    7159             : 
    7160           8 :                     if (ste == NULL || ste->resjunk)
    7161           0 :                         elog(ERROR, "subquery %s does not have attribute %d",
    7162             :                              rte->eref->aliasname, attnum);
    7163           8 :                     expr = (Node *) ste->expr;
    7164           8 :                     if (IsA(expr, Var))
    7165             :                     {
    7166             :                         /*
    7167             :                          * Recurse into the sub-select to see what its Var
    7168             :                          * refers to. We have to build an additional level of
    7169             :                          * namespace to keep in step with varlevelsup in the
    7170             :                          * subselect.
    7171             :                          */
    7172             :                         deparse_namespace mydpns;
    7173             :                         const char *result;
    7174             : 
    7175           8 :                         set_deparse_for_query(&mydpns, rte->subquery,
    7176             :                                               context->namespaces);
    7177             : 
    7178           8 :                         context->namespaces = lcons(&mydpns,
    7179             :                                                     context->namespaces);
    7180             : 
    7181           8 :                         result = get_name_for_var_field((Var *) expr, fieldno,
    7182             :                                                         0, context);
    7183             : 
    7184           8 :                         context->namespaces =
    7185           8 :                             list_delete_first(context->namespaces);
    7186             : 
    7187           8 :                         return result;
    7188             :                     }
    7189             :                     /* else fall through to inspect the expression */
    7190             :                 }
    7191             :                 else
    7192             :                 {
    7193             :                     /*
    7194             :                      * We're deparsing a Plan tree so we don't have complete
    7195             :                      * RTE entries (in particular, rte->subquery is NULL). But
    7196             :                      * the only place we'd see a Var directly referencing a
    7197             :                      * SUBQUERY RTE is in a SubqueryScan plan node, and we can
    7198             :                      * look into the child plan's tlist instead.
    7199             :                      */
    7200             :                     TargetEntry *tle;
    7201             :                     deparse_namespace save_dpns;
    7202             :                     const char *result;
    7203             : 
    7204           4 :                     if (!dpns->inner_plan)
    7205           0 :                         elog(ERROR, "failed to find plan for subquery %s",
    7206             :                              rte->eref->aliasname);
    7207           4 :                     tle = get_tle_by_resno(dpns->inner_tlist, attnum);
    7208           4 :                     if (!tle)
    7209           0 :                         elog(ERROR, "bogus varattno for subquery var: %d",
    7210             :                              attnum);
    7211             :                     Assert(netlevelsup == 0);
    7212           4 :                     push_child_plan(dpns, dpns->inner_plan, &save_dpns);
    7213             : 
    7214           4 :                     result = get_name_for_var_field((Var *) tle->expr, fieldno,
    7215             :                                                     levelsup, context);
    7216             : 
    7217           4 :                     pop_child_plan(dpns, &save_dpns);
    7218           4 :                     return result;
    7219             :                 }
    7220             :             }
    7221           0 :             break;
    7222           0 :         case RTE_JOIN:
    7223             :             /* Join RTE --- recursively inspect the alias variable */
    7224           0 :             if (rte->joinaliasvars == NIL)
    7225           0 :                 elog(ERROR, "cannot decompile join alias var in plan tree");
    7226             :             Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
    7227           0 :             expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
    7228             :             Assert(expr != NULL);
    7229             :             /* we intentionally don't strip implicit coercions here */
    7230           0 :             if (IsA(expr, Var))
    7231           0 :                 return get_name_for_var_field((Var *) expr, fieldno,
    7232           0 :                                               var->varlevelsup + levelsup,
    7233             :                                               context);
    7234             :             /* else fall through to inspect the expression */
    7235           0 :             break;
    7236           0 :         case RTE_FUNCTION:
    7237             :         case RTE_TABLEFUNC:
    7238             : 
    7239             :             /*
    7240             :              * We couldn't get here unless a function is declared with one of
    7241             :              * its result columns as RECORD, which is not allowed.
    7242             :              */
    7243           0 :             break;
    7244          20 :         case RTE_CTE:
    7245             :             /* CTE reference: examine subquery's output expr */
    7246             :             {
    7247          20 :                 CommonTableExpr *cte = NULL;
    7248             :                 Index       ctelevelsup;
    7249             :                 ListCell   *lc;
    7250             : 
    7251             :                 /*
    7252             :                  * Try to find the referenced CTE using the namespace stack.
    7253             :                  */
    7254          20 :                 ctelevelsup = rte->ctelevelsup + netlevelsup;
    7255          20 :                 if (ctelevelsup >= list_length(context->namespaces))
    7256           0 :                     lc = NULL;
    7257             :                 else
    7258             :                 {
    7259             :                     deparse_namespace *ctedpns;
    7260             : 
    7261             :                     ctedpns = (deparse_namespace *)
    7262          20 :                         list_nth(context->namespaces, ctelevelsup);
    7263          20 :                     foreach(lc, ctedpns->ctes)
    7264             :                     {
    7265          12 :                         cte = (CommonTableExpr *) lfirst(lc);
    7266          12 :                         if (strcmp(cte->ctename, rte->ctename) == 0)
    7267          12 :                             break;
    7268             :                     }
    7269             :                 }
    7270          20 :                 if (lc != NULL)
    7271             :                 {
    7272          12 :                     Query      *ctequery = (Query *) cte->ctequery;
    7273          12 :                     TargetEntry *ste = get_tle_by_resno(GetCTETargetList(cte),
    7274             :                                                         attnum);
    7275             : 
    7276          12 :                     if (ste == NULL || ste->resjunk)
    7277           0 :                         elog(ERROR, "subquery %s does not have attribute %d",
    7278             :                              rte->eref->aliasname, attnum);
    7279          12 :                     expr = (Node *) ste->expr;
    7280          12 :                     if (IsA(expr, Var))
    7281             :                     {
    7282             :                         /*
    7283             :                          * Recurse into the CTE to see what its Var refers to.
    7284             :                          * We have to build an additional level of namespace
    7285             :                          * to keep in step with varlevelsup in the CTE.
    7286             :                          * Furthermore it could be an outer CTE, so we may
    7287             :                          * have to delete some levels of namespace.
    7288             :                          */
    7289           8 :                         List       *save_nslist = context->namespaces;
    7290             :                         List       *new_nslist;
    7291             :                         deparse_namespace mydpns;
    7292             :                         const char *result;
    7293             : 
    7294           8 :                         set_deparse_for_query(&mydpns, ctequery,
    7295             :                                               context->namespaces);
    7296             : 
    7297           8 :                         new_nslist = list_copy_tail(context->namespaces,
    7298             :                                                     ctelevelsup);
    7299           8 :                         context->namespaces = lcons(&mydpns, new_nslist);
    7300             : 
    7301           8 :                         result = get_name_for_var_field((Var *) expr, fieldno,
    7302             :                                                         0, context);
    7303             : 
    7304           8 :                         context->namespaces = save_nslist;
    7305             : 
    7306           8 :                         return result;
    7307             :                     }
    7308             :                     /* else fall through to inspect the expression */
    7309             :                 }
    7310             :                 else
    7311             :                 {
    7312             :                     /*
    7313             :                      * We're deparsing a Plan tree so we don't have a CTE
    7314             :                      * list.  But the only place we'd see a Var directly
    7315             :                      * referencing a CTE RTE is in a CteScan plan node, and we
    7316             :                      * can look into the subplan's tlist instead.
    7317             :                      */
    7318             :                     TargetEntry *tle;
    7319             :                     deparse_namespace save_dpns;
    7320             :                     const char *result;
    7321             : 
    7322           8 :                     if (!dpns->inner_plan)
    7323           0 :                         elog(ERROR, "failed to find plan for CTE %s",
    7324             :                              rte->eref->aliasname);
    7325           8 :                     tle = get_tle_by_resno(dpns->inner_tlist, attnum);
    7326           8 :                     if (!tle)
    7327           0 :                         elog(ERROR, "bogus varattno for subquery var: %d",
    7328             :                              attnum);
    7329             :                     Assert(netlevelsup == 0);
    7330           8 :                     push_child_plan(dpns, dpns->inner_plan, &save_dpns);
    7331             : 
    7332           8 :                     result = get_name_for_var_field((Var *) tle->expr, fieldno,
    7333             :                                                     levelsup, context);
    7334             : 
    7335           8 :                     pop_child_plan(dpns, &save_dpns);
    7336           8 :                     return result;
    7337             :                 }
    7338             :             }
    7339           4 :             break;
    7340             :     }
    7341             : 
    7342             :     /*
    7343             :      * We now have an expression we can't expand any more, so see if
    7344             :      * get_expr_result_tupdesc() can do anything with it.
    7345             :      */
    7346           4 :     tupleDesc = get_expr_result_tupdesc(expr, false);
    7347             :     /* Got the tupdesc, so we can extract the field name */
    7348             :     Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
    7349           4 :     return NameStr(TupleDescAttr(tupleDesc, fieldno - 1)->attname);
    7350             : }
    7351             : 
    7352             : /*
    7353             :  * Try to find the referenced expression for a PARAM_EXEC Param that might
    7354             :  * reference a parameter supplied by an upper NestLoop or SubPlan plan node.
    7355             :  *
    7356             :  * If successful, return the expression and set *dpns_p and *ancestor_cell_p
    7357             :  * appropriately for calling push_ancestor_plan().  If no referent can be
    7358             :  * found, return NULL.
    7359             :  */
    7360             : static Node *
    7361        2940 : find_param_referent(Param *param, deparse_context *context,
    7362             :                     deparse_namespace **dpns_p, ListCell **ancestor_cell_p)
    7363             : {
    7364             :     /* Initialize output parameters to prevent compiler warnings */
    7365        2940 :     *dpns_p = NULL;
    7366        2940 :     *ancestor_cell_p = NULL;
    7367             : 
    7368             :     /*
    7369             :      * If it's a PARAM_EXEC parameter, look for a matching NestLoopParam or
    7370             :      * SubPlan argument.  This will necessarily be in some ancestor of the
    7371             :      * current expression's Plan node.
    7372             :      */
    7373        2940 :     if (param->paramkind == PARAM_EXEC)
    7374             :     {
    7375             :         deparse_namespace *dpns;
    7376             :         Plan       *child_plan;
    7377             :         bool        in_same_plan_level;
    7378             :         ListCell   *lc;
    7379             : 
    7380        2474 :         dpns = (deparse_namespace *) linitial(context->namespaces);
    7381        2474 :         child_plan = dpns->plan;
    7382        2474 :         in_same_plan_level = true;
    7383             : 
    7384        4684 :         foreach(lc, dpns->ancestors)
    7385             :         {
    7386        3776 :             Node       *ancestor = (Node *) lfirst(lc);
    7387             :             ListCell   *lc2;
    7388             : 
    7389             :             /*
    7390             :              * NestLoops transmit params to their inner child only; also, once
    7391             :              * we've crawled up out of a subplan, this couldn't possibly be
    7392             :              * the right match.
    7393             :              */
    7394        3776 :             if (IsA(ancestor, NestLoop) &&
    7395        1540 :                 child_plan == innerPlan(ancestor) &&
    7396             :                 in_same_plan_level)
    7397             :             {
    7398        1460 :                 NestLoop   *nl = (NestLoop *) ancestor;
    7399             : 
    7400        1798 :                 foreach(lc2, nl->nestParams)
    7401             :                 {
    7402        1738 :                     NestLoopParam *nlp = (NestLoopParam *) lfirst(lc2);
    7403             : 
    7404        1738 :                     if (nlp->paramno == param->paramid)
    7405             :                     {
    7406             :                         /* Found a match, so return it */
    7407        1400 :                         *dpns_p = dpns;
    7408        1400 :                         *ancestor_cell_p = lc;
    7409        1400 :                         return (Node *) nlp->paramval;
    7410             :                     }
    7411             :                 }
    7412             :             }
    7413             : 
    7414             :             /*
    7415             :              * If ancestor is a SubPlan, check the arguments it provides.
    7416             :              */
    7417        2376 :             if (IsA(ancestor, SubPlan))
    7418             :             {
    7419         210 :                 SubPlan    *subplan = (SubPlan *) ancestor;
    7420             :                 ListCell   *lc3;
    7421             :                 ListCell   *lc4;
    7422             : 
    7423         228 :                 forboth(lc3, subplan->parParam, lc4, subplan->args)
    7424             :                 {
    7425         184 :                     int         paramid = lfirst_int(lc3);
    7426         184 :                     Node       *arg = (Node *) lfirst(lc4);
    7427             : 
    7428         184 :                     if (paramid == param->paramid)
    7429             :                     {
    7430             :                         /*
    7431             :                          * Found a match, so return it.  But, since Vars in
    7432             :                          * the arg are to be evaluated in the surrounding
    7433             :                          * context, we have to point to the next ancestor item
    7434             :                          * that is *not* a SubPlan.
    7435             :                          */
    7436             :                         ListCell   *rest;
    7437             : 
    7438         166 :                         for_each_cell(rest, dpns->ancestors,
    7439             :                                       lnext(dpns->ancestors, lc))
    7440             :                         {
    7441         166 :                             Node       *ancestor2 = (Node *) lfirst(rest);
    7442             : 
    7443         166 :                             if (!IsA(ancestor2, SubPlan))
    7444             :                             {
    7445         166 :                                 *dpns_p = dpns;
    7446         166 :                                 *ancestor_cell_p = rest;
    7447         166 :                                 return arg;
    7448             :                             }
    7449             :                         }
    7450           0 :                         elog(ERROR, "SubPlan cannot be outermost ancestor");
    7451             :                     }
    7452             :                 }
    7453             : 
    7454             :                 /* We have emerged from a subplan. */
    7455          44 :                 in_same_plan_level = false;
    7456             : 
    7457             :                 /* SubPlan isn't a kind of Plan, so skip the rest */
    7458          44 :                 continue;
    7459             :             }
    7460             : 
    7461             :             /*
    7462             :              * Check to see if we're emerging from an initplan of the current
    7463             :              * ancestor plan.  Initplans never have any parParams, so no need
    7464             :              * to search that list, but we need to know if we should reset
    7465             :              * in_same_plan_level.
    7466             :              */
    7467        2948 :             foreach(lc2, ((Plan *) ancestor)->initPlan)
    7468             :             {
    7469         798 :                 SubPlan    *subplan = castNode(SubPlan, lfirst(lc2));
    7470             : 
    7471         798 :                 if (child_plan != (Plan *) list_nth(dpns->subplans,
    7472         798 :                                                     subplan->plan_id - 1))
    7473         782 :                     continue;
    7474             : 
    7475             :                 /* No parameters to be had here. */
    7476             :                 Assert(subplan->parParam == NIL);
    7477             : 
    7478             :                 /* We have emerged from an initplan. */
    7479          16 :                 in_same_plan_level = false;
    7480          16 :                 break;
    7481             :             }
    7482             : 
    7483             :             /* No luck, crawl up to next ancestor */
    7484        2166 :             child_plan = (Plan *) ancestor;
    7485             :         }
    7486             :     }
    7487             : 
    7488             :     /* No referent found */
    7489        1374 :     return NULL;
    7490             : }
    7491             : 
    7492             : /*
    7493             :  * Display a Param appropriately.
    7494             :  */
    7495             : static void
    7496        2932 : get_parameter(Param *param, deparse_context *context)
    7497             : {
    7498             :     Node       *expr;
    7499             :     deparse_namespace *dpns;
    7500             :     ListCell   *ancestor_cell;
    7501             : 
    7502             :     /*
    7503             :      * If it's a PARAM_EXEC parameter, try to locate the expression from which
    7504             :      * the parameter was computed.  Note that failing to find a referent isn't
    7505             :      * an error, since the Param might well be a subplan output rather than an
    7506             :      * input.
    7507             :      */
    7508        2932 :     expr = find_param_referent(param, context, &dpns, &ancestor_cell);
    7509        2932 :     if (expr)
    7510             :     {
    7511             :         /* Found a match, so print it */
    7512             :         deparse_namespace save_dpns;
    7513             :         bool        save_varprefix;
    7514             :         bool        need_paren;
    7515             : 
    7516             :         /* Switch attention to the ancestor plan node */
    7517        1558 :         push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
    7518             : 
    7519             :         /*
    7520             :          * Force prefixing of Vars, since they won't belong to the relation
    7521             :          * being scanned in the original plan node.
    7522             :          */
    7523        1558 :         save_varprefix = context->varprefix;
    7524        1558 :         context->varprefix = true;
    7525             : 
    7526             :         /*
    7527             :          * A Param's expansion is typically a Var, Aggref, or upper-level
    7528             :          * Param, which wouldn't need extra parentheses.  Otherwise, insert
    7529             :          * parens to ensure the expression looks atomic.
    7530             :          */
    7531        1558 :         need_paren = !(IsA(expr, Var) ||
    7532           0 :                        IsA(expr, Aggref) ||
    7533           0 :                        IsA(expr, Param));
    7534        1558 :         if (need_paren)
    7535           0 :             appendStringInfoChar(context->buf, '(');
    7536             : 
    7537        1558 :         get_rule_expr(expr, context, false);
    7538             : 
    7539        1558 :         if (need_paren)
    7540           0 :             appendStringInfoChar(context->buf, ')');
    7541             : 
    7542        1558 :         context->varprefix = save_varprefix;
    7543             : 
    7544        1558 :         pop_ancestor_plan(dpns, &save_dpns);
    7545             : 
    7546        1558 :         return;
    7547             :     }
    7548             : 
    7549             :     /*
    7550             :      * Not PARAM_EXEC, or couldn't find referent: just print $N.
    7551             :      */
    7552        1374 :     appendStringInfo(context->buf, "$%d", param->paramid);
    7553             : }
    7554             : 
    7555             : /*
    7556             :  * get_simple_binary_op_name
    7557             :  *
    7558             :  * helper function for isSimpleNode
    7559             :  * will return single char binary operator name, or NULL if it's not
    7560             :  */
    7561             : static const char *
    7562          76 : get_simple_binary_op_name(OpExpr *expr)
    7563             : {
    7564          76 :     List       *args = expr->args;
    7565             : 
    7566          76 :     if (list_length(args) == 2)
    7567             :     {
    7568             :         /* binary operator */
    7569          76 :         Node       *arg1 = (Node *) linitial(args);
    7570          76 :         Node       *arg2 = (Node *) lsecond(args);
    7571             :         const char *op;
    7572             : 
    7573          76 :         op = generate_operator_name(expr->opno, exprType(arg1), exprType(arg2));
    7574          76 :         if (strlen(op) == 1)
    7575          76 :             return op;
    7576             :     }
    7577           0 :     return NULL;
    7578             : }
    7579             : 
    7580             : 
    7581             : /*
    7582             :  * isSimpleNode - check if given node is simple (doesn't need parenthesizing)
    7583             :  *
    7584             :  *  true   : simple in the context of parent node's type
    7585             :  *  false  : not simple
    7586             :  */
    7587             : static bool
    7588        2356 : isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
    7589             : {
    7590        2356 :     if (!node)
    7591           0 :         return false;
    7592             : 
    7593        2356 :     switch (nodeTag(node))
    7594             :     {
    7595        1952 :         case T_Var:
    7596             :         case T_Const:
    7597             :         case T_Param:
    7598             :         case T_CoerceToDomainValue:
    7599             :         case T_SetToDefault:
    7600             :         case T_CurrentOfExpr:
    7601             :             /* single words: always simple */
    7602        1952 :             return true;
    7603             : 
    7604         224 :         case T_SubscriptingRef:
    7605             :         case T_ArrayExpr:
    7606             :         case T_RowExpr:
    7607             :         case T_CoalesceExpr:
    7608             :         case T_MinMaxExpr:
    7609             :         case T_SQLValueFunction:
    7610             :         case T_XmlExpr:
    7611             :         case T_NextValueExpr:
    7612             :         case T_NullIfExpr:
    7613             :         case T_Aggref:
    7614             :         case T_WindowFunc:
    7615             :         case T_FuncExpr:
    7616             :             /* function-like: name(..) or name[..] */
    7617         224 :             return true;
    7618             : 
    7619             :             /* CASE keywords act as parentheses */
    7620           0 :         case T_CaseExpr:
    7621           0 :             return true;
    7622             : 
    7623          32 :         case T_FieldSelect:
    7624             : 
    7625             :             /*
    7626             :              * appears simple since . has top precedence, unless parent is
    7627             :              * T_FieldSelect itself!
    7628             :              */
    7629          32 :             return (IsA(parentNode, FieldSelect) ? false : true);
    7630             : 
    7631           0 :         case T_FieldStore:
    7632             : 
    7633             :             /*
    7634             :              * treat like FieldSelect (probably doesn't matter)
    7635             :              */
    7636           0 :             return (IsA(parentNode, FieldStore) ? false : true);
    7637             : 
    7638           0 :         case T_CoerceToDomain:
    7639             :             /* maybe simple, check args */
    7640           0 :             return isSimpleNode((Node *) ((CoerceToDomain *) node)->arg,
    7641             :                                 node, prettyFlags);
    7642           4 :         case T_RelabelType:
    7643           4 :             return isSimpleNode((Node *) ((RelabelType *) node)->arg,
    7644             :                                 node, prettyFlags);
    7645           0 :         case T_CoerceViaIO:
    7646           0 :             return isSimpleNode((Node *) ((CoerceViaIO *) node)->arg,
    7647             :                                 node, prettyFlags);
    7648           0 :         case T_ArrayCoerceExpr:
    7649           0 :             return isSimpleNode((Node *) ((ArrayCoerceExpr *) node)->arg,
    7650             :                                 node, prettyFlags);
    7651           0 :         case T_ConvertRowtypeExpr:
    7652           0 :             return isSimpleNode((Node *) ((ConvertRowtypeExpr *) node)->arg,
    7653             :                                 node, prettyFlags);
    7654             : 
    7655         128 :         case T_OpExpr:
    7656             :             {
    7657             :                 /* depends on parent node type; needs further checking */
    7658         128 :                 if (prettyFlags & PRETTYFLAG_PAREN && IsA(parentNode, OpExpr))
    7659             :                 {
    7660             :                     const char *op;
    7661             :                     const char *parentOp;
    7662             :                     bool        is_lopriop;
    7663             :                     bool        is_hipriop;
    7664             :                     bool        is_lopriparent;
    7665             :                     bool        is_hipriparent;
    7666             : 
    7667          40 :                     op = get_simple_binary_op_name((OpExpr *) node);
    7668          40 :                     if (!op)
    7669           0 :                         return false;
    7670             : 
    7671             :                     /* We know only the basic operators + - and * / % */
    7672          40 :                     is_lopriop = (strchr("+-", *op) != NULL);
    7673          40 :                     is_hipriop = (strchr("*/%", *op) != NULL);
    7674          40 :                     if (!(is_lopriop || is_hipriop))
    7675           4 :                         return false;
    7676             : 
    7677          36 :                     parentOp = get_simple_binary_op_name((OpExpr *) parentNode);
    7678          36 :                     if (!parentOp)
    7679           0 :                         return false;
    7680             : 
    7681          36 :                     is_lopriparent = (strchr("+-", *parentOp) != NULL);
    7682          36 :                     is_hipriparent = (strchr("*/%", *parentOp) != NULL);
    7683          36 :                     if (!(is_lopriparent || is_hipriparent))
    7684           0 :                         return false;
    7685             : 
    7686          36 :                     if (is_hipriop && is_lopriparent)
    7687           8 :                         return true;    /* op binds tighter than parent */
    7688             : 
    7689          28 :                     if (is_lopriop && is_hipriparent)
    7690          20 :                         return false;
    7691             : 
    7692             :                     /*
    7693             :                      * Operators are same priority --- can skip parens only if
    7694             :                      * we have (a - b) - c, not a - (b - c).
    7695             :                      */
    7696           8 :                     if (node == (Node *) linitial(((OpExpr *) parentNode)->args))
    7697           4 :                         return true;
    7698             : 
    7699           4 :                     return false;
    7700             :                 }
    7701             :                 /* else do the same stuff as for T_SubLink et al. */
    7702             :             }
    7703             :             /* FALLTHROUGH */
    7704             : 
    7705             :         case T_SubLink:
    7706             :         case T_NullTest:
    7707             :         case T_BooleanTest:
    7708             :         case T_DistinctExpr:
    7709          92 :             switch (nodeTag(parentNode))
    7710             :             {
    7711          16 :                 case T_FuncExpr:
    7712             :                     {
    7713             :                         /* special handling for casts */
    7714          16 :                         CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
    7715             : 
    7716          16 :                         if (type == COERCE_EXPLICIT_CAST ||
    7717             :                             type == COERCE_IMPLICIT_CAST)
    7718          16 :                             return false;
    7719           0 :                         return true;    /* own parentheses */
    7720             :                     }
    7721          72 :                 case T_BoolExpr:    /* lower precedence */
    7722             :                 case T_SubscriptingRef: /* other separators */
    7723             :                 case T_ArrayExpr:   /* other separators */
    7724             :                 case T_RowExpr: /* other separators */
    7725             :                 case T_CoalesceExpr:    /* own parentheses */
    7726             :                 case T_MinMaxExpr:  /* own parentheses */
    7727             :                 case T_XmlExpr: /* own parentheses */
    7728             :                 case T_NullIfExpr:  /* other separators */
    7729             :                 case T_Aggref:  /* own parentheses */
    7730             :                 case T_WindowFunc:  /* own parentheses */
    7731             :                 case T_CaseExpr:    /* other separators */
    7732          72 :                     return true;
    7733           4 :                 default:
    7734           4 :                     return false;
    7735             :             }
    7736             : 
    7737          12 :         case T_BoolExpr:
    7738          12 :             switch (nodeTag(parentNode))
    7739             :             {
    7740          12 :                 case T_BoolExpr:
    7741          12 :                     if (prettyFlags & PRETTYFLAG_PAREN)
    7742             :                     {
    7743             :                         BoolExprType type;
    7744             :                         BoolExprType parentType;
    7745             : 
    7746          12 :                         type = ((BoolExpr *) node)->boolop;
    7747          12 :                         parentType = ((BoolExpr *) parentNode)->boolop;
    7748             :                         switch (type)
    7749             :                         {
    7750           8 :                             case NOT_EXPR:
    7751             :                             case AND_EXPR:
    7752           8 :                                 if (parentType == AND_EXPR || parentType == OR_EXPR)
    7753           8 :                                     return true;
    7754           0 :                                 break;
    7755           4 :                             case OR_EXPR:
    7756           4 :                                 if (parentType == OR_EXPR)
    7757           0 :                                     return true;
    7758           4 :                                 break;
    7759             :                         }
    7760           0 :                     }
    7761           4 :                     return false;
    7762           0 :                 case T_FuncExpr:
    7763             :                     {
    7764             :                         /* special handling for casts */
    7765           0 :                         CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
    7766             : 
    7767           0 :                         if (type == COERCE_EXPLICIT_CAST ||
    7768             :                             type == COERCE_IMPLICIT_CAST)
    7769           0 :                             return false;
    7770           0 :                         return true;    /* own parentheses */
    7771             :                     }
    7772           0 :                 case T_SubscriptingRef: /* other separators */
    7773             :                 case T_ArrayExpr:   /* other separators */
    7774             :                 case T_RowExpr: /* other separators */
    7775             :                 case T_CoalesceExpr:    /* own parentheses */
    7776             :                 case T_MinMaxExpr:  /* own parentheses */
    7777             :                 case T_XmlExpr: /* own parentheses */
    7778             :                 case T_NullIfExpr:  /* other separators */
    7779             :                 case T_Aggref:  /* own parentheses */
    7780             :                 case T_WindowFunc:  /* own parentheses */
    7781             :                 case T_CaseExpr:    /* other separators */
    7782           0 :                     return true;
    7783           0 :                 default:
    7784           0 :                     return false;
    7785             :             }
    7786             : 
    7787           0 :         default:
    7788           0 :             break;
    7789             :     }
    7790             :     /* those we don't know: in dubio complexo */
    7791           0 :     return false;
    7792             : }
    7793             : 
    7794             : 
    7795             : /*
    7796             :  * appendContextKeyword - append a keyword to buffer
    7797             :  *
    7798             :  * If prettyPrint is enabled, perform a line break, and adjust indentation.
    7799             :  * Otherwise, just append the keyword.
    7800             :  */
    7801             : static void
    7802       10162 : appendContextKeyword(deparse_context *context, const char *str,
    7803             :                      int indentBefore, int indentAfter, int indentPlus)
    7804             : {
    7805       10162 :     StringInfo  buf = context->buf;
    7806             : 
    7807       10162 :     if (PRETTY_INDENT(context))
    7808             :     {
    7809             :         int         indentAmount;
    7810             : 
    7811       10098 :         context->indentLevel += indentBefore;
    7812             : 
    7813             :         /* remove any trailing spaces currently in the buffer ... */
    7814       10098 :         removeStringInfoSpaces(buf);
    7815             :         /* ... then add a newline and some spaces */
    7816       10098 :         appendStringInfoChar(buf, '\n');
    7817             : 
    7818       10098 :         if (context->indentLevel < PRETTYINDENT_LIMIT)
    7819       10098 :             indentAmount = Max(context->indentLevel, 0) + indentPlus;
    7820             :         else
    7821             :         {
    7822             :             /*
    7823             :              * If we're indented more than PRETTYINDENT_LIMIT characters, try
    7824             :              * to conserve horizontal space by reducing the per-level
    7825             :              * indentation.  For best results the scale factor here should
    7826             :              * divide all the indent amounts that get added to indentLevel
    7827             :              * (PRETTYINDENT_STD, etc).  It's important that the indentation
    7828             :              * not grow unboundedly, else deeply-nested trees use O(N^2)
    7829             :              * whitespace; so we also wrap modulo PRETTYINDENT_LIMIT.
    7830             :              */
    7831           0 :             indentAmount = PRETTYINDENT_LIMIT +
    7832           0 :                 (context->indentLevel - PRETTYINDENT_LIMIT) /
    7833             :                 (PRETTYINDENT_STD / 2);
    7834           0 :             indentAmount %= PRETTYINDENT_LIMIT;
    7835             :             /* scale/wrap logic affects indentLevel, but not indentPlus */
    7836           0 :             indentAmount += indentPlus;
    7837             :         }
    7838       10098 :         appendStringInfoSpaces(buf, indentAmount);
    7839             : 
    7840       10098 :         appendStringInfoString(buf, str);
    7841             : 
    7842       10098 :         context->indentLevel += indentAfter;
    7843       10098 :         if (context->indentLevel < 0)
    7844           0 :             context->indentLevel = 0;
    7845             :     }
    7846             :     else
    7847          64 :         appendStringInfoString(buf, str);
    7848       10162 : }
    7849             : 
    7850             : /*
    7851             :  * removeStringInfoSpaces - delete trailing spaces from a buffer.
    7852             :  *
    7853             :  * Possibly this should move to stringinfo.c at some point.
    7854             :  */
    7855             : static void
    7856       10206 : removeStringInfoSpaces(StringInfo str)
    7857             : {
    7858       15856 :     while (str->len > 0 && str->data[str->len - 1] == ' ')
    7859        5650 :         str->data[--(str->len)] = '\0';
    7860       10206 : }
    7861             : 
    7862             : 
    7863             : /*
    7864             :  * get_rule_expr_paren  - deparse expr using get_rule_expr,
    7865             :  * embracing the string with parentheses if necessary for prettyPrint.
    7866             :  *
    7867             :  * Never embrace if prettyFlags=0, because it's done in the calling node.
    7868             :  *
    7869             :  * Any node that does *not* embrace its argument node by sql syntax (with
    7870             :  * parentheses, non-operator keywords like CASE/WHEN/ON, or comma etc) should
    7871             :  * use get_rule_expr_paren instead of get_rule_expr so parentheses can be
    7872             :  * added.
    7873             :  */
    7874             : static void
    7875       74388 : get_rule_expr_paren(Node *node, deparse_context *context,
    7876             :                     bool showimplicit, Node *parentNode)
    7877             : {
    7878             :     bool        need_paren;
    7879             : 
    7880       76740 :     need_paren = PRETTY_PAREN(context) &&
    7881        2352 :         !isSimpleNode(node, parentNode, context->prettyFlags);
    7882             : 
    7883       74388 :     if (need_paren)
    7884          52 :         appendStringInfoChar(context->buf, '(');
    7885             : 
    7886       74388 :     get_rule_expr(node, context, showimplicit);
    7887             : 
    7888       74388 :     if (need_paren)
    7889          52 :         appendStringInfoChar(context->buf, ')');
    7890       74388 : }
    7891             : 
    7892             : 
    7893             : /* ----------
    7894             :  * get_rule_expr            - Parse back an expression
    7895             :  *
    7896             :  * Note: showimplicit determines whether we display any implicit cast that
    7897             :  * is present at the top of the expression tree.  It is a passed argument,
    7898             :  * not a field of the context struct, because we change the value as we
    7899             :  * recurse down into the expression.  In general we suppress implicit casts
    7900             :  * when the result type is known with certainty (eg, the arguments of an
    7901             :  * OR must be boolean).  We display implicit casts for arguments of functions
    7902             :  * and operators, since this is needed to be certain that the same function
    7903             :  * or operator will be chosen when the expression is re-parsed.
    7904             :  * ----------
    7905             :  */
    7906             : static void
    7907      150282 : get_rule_expr(Node *node, deparse_context *context,
    7908             :               bool showimplicit)
    7909             : {
    7910      150282 :     StringInfo  buf = context->buf;
    7911             : 
    7912      150282 :     if (node == NULL)
    7913          16 :         return;
    7914             : 
    7915             :     /* Guard against excessively long or deeply-nested queries */
    7916      150266 :     CHECK_FOR_INTERRUPTS();
    7917      150266 :     check_stack_depth();
    7918             : 
    7919             :     /*
    7920             :      * Each level of get_rule_expr must emit an indivisible term
    7921             :      * (parenthesized if necessary) to ensure result is reparsed into the same
    7922             :      * expression tree.  The only exception is that when the input is a List,
    7923             :      * we emit the component items comma-separated with no surrounding
    7924             :      * decoration; this is convenient for most callers.
    7925             :      */
    7926      150266 :     switch (nodeTag(node))
    7927             :     {
    7928       71140 :         case T_Var:
    7929       71140 :             (void) get_variable((Var *) node, 0, false, context);
    7930       71140 :             break;
    7931             : 
    7932       27088 :         case T_Const:
    7933       27088 :             get_const_expr((Const *) node, context, 0);
    7934       27088 :             break;
    7935             : 
    7936        2932 :         case T_Param:
    7937        2932 :             get_parameter((Param *) node, context);
    7938        2932 :             break;
    7939             : 
    7940        1086 :         case T_Aggref:
    7941        1086 :             get_agg_expr((Aggref *) node, context, (Aggref *) node);
    7942        1086 :             break;
    7943             : 
    7944          28 :         case T_GroupingFunc:
    7945             :             {
    7946          28 :                 GroupingFunc *gexpr = (GroupingFunc *) node;
    7947             : 
    7948          28 :                 appendStringInfoString(buf, "GROUPING(");
    7949          28 :                 get_rule_expr((Node *) gexpr->args, context, true);
    7950          28 :                 appendStringInfoChar(buf, ')');
    7951             :             }
    7952          28 :             break;
    7953             : 
    7954          56 :         case T_WindowFunc:
    7955          56 :             get_windowfunc_expr((WindowFunc *) node, context);
    7956          56 :             break;
    7957             : 
    7958         104 :         case T_SubscriptingRef:
    7959             :             {
    7960         104 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    7961             :                 bool        need_parens;
    7962             : 
    7963             :                 /*
    7964             :                  * If the argument is a CaseTestExpr, we must be inside a
    7965             :                  * FieldStore, ie, we are assigning to an element of an array
    7966             :                  * within a composite column.  Since we already punted on
    7967             :                  * displaying the FieldStore's target information, just punt
    7968             :                  * here too, and display only the assignment source
    7969             :                  * expression.
    7970             :                  */
    7971         104 :                 if (IsA(sbsref->refexpr, CaseTestExpr))
    7972             :                 {
    7973             :                     Assert(sbsref->refassgnexpr);
    7974           0 :                     get_rule_expr((Node *) sbsref->refassgnexpr,
    7975             :                                   context, showimplicit);
    7976           0 :                     break;
    7977             :                 }
    7978             : 
    7979             :                 /*
    7980             :                  * Parenthesize the argument unless it's a simple Var or a
    7981             :                  * FieldSelect.  (In particular, if it's another
    7982             :                  * SubscriptingRef, we *must* parenthesize to avoid
    7983             :                  * confusion.)
    7984             :                  */
    7985         152 :                 need_parens = !IsA(sbsref->refexpr, Var) &&
    7986          48 :                     !IsA(sbsref->refexpr, FieldSelect);
    7987         104 :                 if (need_parens)
    7988          48 :                     appendStringInfoChar(buf, '(');
    7989         104 :                 get_rule_expr((Node *) sbsref->refexpr, context, showimplicit);
    7990         104 :                 if (need_parens)
    7991          48 :                     appendStringInfoChar(buf, ')');
    7992             : 
    7993             :                 /*
    7994             :                  * If there's a refassgnexpr, we want to print the node in the
    7995             :                  * format "container[subscripts] := refassgnexpr".  This is
    7996             :                  * not legal SQL, so decompilation of INSERT or UPDATE
    7997             :                  * statements should always use processIndirection as part of
    7998             :                  * the statement-level syntax.  We should only see this when
    7999             :                  * EXPLAIN tries to print the targetlist of a plan resulting
    8000             :                  * from such a statement.
    8001             :                  */
    8002         104 :                 if (sbsref->refassgnexpr)
    8003             :                 {
    8004             :                     Node       *refassgnexpr;
    8005             : 
    8006             :                     /*
    8007             :                      * Use processIndirection to print this node's subscripts
    8008             :                      * as well as any additional field selections or
    8009             :                      * subscripting in immediate descendants.  It returns the
    8010             :                      * RHS expr that is actually being "assigned".
    8011             :                      */
    8012           8 :                     refassgnexpr = processIndirection(node, context);
    8013           8 :                     appendStringInfoString(buf, " := ");
    8014           8 :                     get_rule_expr(refassgnexpr, context, showimplicit);
    8015             :                 }
    8016             :                 else
    8017             :                 {
    8018             :                     /* Just an ordinary container fetch, so print subscripts */
    8019          96 :                     printSubscripts(sbsref, context);
    8020             :                 }
    8021             :             }
    8022         104 :             break;
    8023             : 
    8024        5008 :         case T_FuncExpr:
    8025        5008 :             get_func_expr((FuncExpr *) node, context, showimplicit);
    8026        5008 :             break;
    8027             : 
    8028          12 :         case T_NamedArgExpr:
    8029             :             {
    8030          12 :                 NamedArgExpr *na = (NamedArgExpr *) node;
    8031             : 
    8032          12 :                 appendStringInfo(buf, "%s => ", quote_identifier(na->name));
    8033          12 :                 get_rule_expr((Node *) na->arg, context, showimplicit);
    8034             :             }
    8035          12 :             break;
    8036             : 
    8037       28026 :         case T_OpExpr:
    8038       28026 :             get_oper_expr((OpExpr *) node, context);
    8039       28026 :             break;
    8040             : 
    8041          12 :         case T_DistinctExpr:
    8042           4 :             {
    8043          12 :                 DistinctExpr *expr = (DistinctExpr *) node;
    8044          12 :                 List       *args = expr->args;
    8045          12 :                 Node       *arg1 = (Node *) linitial(args);
    8046          12 :                 Node       *arg2 = (Node *) lsecond(args);
    8047             : 
    8048          12 :                 if (!PRETTY_PAREN(context))
    8049           8 :                     appendStringInfoChar(buf, '(');
    8050          12 :                 get_rule_expr_paren(arg1, context, true, node);
    8051          12 :                 appendStringInfoString(buf, " IS DISTINCT FROM ");
    8052          12 :                 get_rule_expr_paren(arg2, context, true, node);
    8053          12 :                 if (!PRETTY_PAREN(context))
    8054           8 :                     appendStringInfoChar(buf, ')');
    8055             :             }
    8056          12 :             break;
    8057             : 
    8058          12 :         case T_NullIfExpr:
    8059             :             {
    8060          12 :                 NullIfExpr *nullifexpr = (NullIfExpr *) node;
    8061             : 
    8062          12 :                 appendStringInfoString(buf, "NULLIF(");
    8063          12 :                 get_rule_expr((Node *) nullifexpr->args, context, true);
    8064          12 :                 appendStringInfoChar(buf, ')');
    8065             :             }
    8066          12 :             break;
    8067             : 
    8068        1046 :         case T_ScalarArrayOpExpr:
    8069           8 :             {
    8070        1046 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    8071        1046 :                 List       *args = expr->args;
    8072        1046 :                 Node       *arg1 = (Node *) linitial(args);
    8073        1046 :                 Node       *arg2 = (Node *) lsecond(args);
    8074             : 
    8075        1046 :                 if (!PRETTY_PAREN(context))
    8076        1038 :                     appendStringInfoChar(buf, '(');
    8077        1046 :                 get_rule_expr_paren(arg1, context, true, node);
    8078        1046 :                 appendStringInfo(buf, " %s %s (",
    8079             :                                  generate_operator_name(expr->opno,
    8080             :                                                         exprType(arg1),
    8081             :                                                         get_base_element_type(exprType(arg2))),
    8082        1046 :                                  expr->useOr ? "ANY" : "ALL");
    8083        1046 :                 get_rule_expr_paren(arg2, context, true, node);
    8084             : 
    8085             :                 /*
    8086             :                  * There's inherent ambiguity in "x op ANY/ALL (y)" when y is
    8087             :                  * a bare sub-SELECT.  Since we're here, the sub-SELECT must
    8088             :                  * be meant as a scalar sub-SELECT yielding an array value to
    8089             :                  * be used in ScalarArrayOpExpr; but the grammar will
    8090             :                  * preferentially interpret such a construct as an ANY/ALL
    8091             :                  * SubLink.  To prevent misparsing the output that way, insert
    8092             :                  * a dummy coercion (which will be stripped by parse analysis,
    8093             :                  * so no inefficiency is added in dump and reload).  This is
    8094             :                  * indeed most likely what the user wrote to get the construct
    8095             :                  * accepted in the first place.
    8096             :                  */
    8097        1046 :                 if (IsA(arg2, SubLink) &&
    8098           4 :                     ((SubLink *) arg2)->subLinkType == EXPR_SUBLINK)
    8099           4 :                     appendStringInfo(buf, "::%s",
    8100             :                                      format_type_with_typemod(exprType(arg2),
    8101             :                                                               exprTypmod(arg2)));
    8102        1046 :                 appendStringInfoChar(buf, ')');
    8103        1046 :                 if (!PRETTY_PAREN(context))
    8104        1038 :                     appendStringInfoChar(buf, ')');
    8105             :             }
    8106        1046 :             break;
    8107             : 
    8108        5496 :         case T_BoolExpr:
    8109             :             {
    8110        5496 :                 BoolExpr   *expr = (BoolExpr *) node;
    8111        5496 :                 Node       *first_arg = linitial(expr->args);
    8112             :                 ListCell   *arg;
    8113             : 
    8114        5496 :                 switch (expr->boolop)
    8115             :                 {
    8116        4506 :                     case AND_EXPR:
    8117        4506 :                         if (!PRETTY_PAREN(context))
    8118        4474 :                             appendStringInfoChar(buf, '(');
    8119        4506 :                         get_rule_expr_paren(first_arg, context,
    8120             :                                             false, node);
    8121       10222 :                         for_each_from(arg, expr->args, 1)
    8122             :                         {
    8123        5716 :                             appendStringInfoString(buf, " AND ");
    8124        5716 :                             get_rule_expr_paren((Node *) lfirst(arg), context,
    8125             :                                                 false, node);
    8126             :                         }
    8127        4506 :                         if (!PRETTY_PAREN(context))
    8128        4474 :                             appendStringInfoChar(buf, ')');
    8129        4506 :                         break;
    8130             : 
    8131         860 :                     case OR_EXPR:
    8132         860 :                         if (!PRETTY_PAREN(context))
    8133         852 :                             appendStringInfoChar(buf, '(');
    8134         860 :                         get_rule_expr_paren(first_arg, context,
    8135             :                                             false, node);
    8136        2044 :                         for_each_from(arg, expr->args, 1)
    8137             :                         {
    8138        1184 :                             appendStringInfoString(buf, " OR ");
    8139        1184 :                             get_rule_expr_paren((Node *) lfirst(arg), context,
    8140             :                                                 false, node);
    8141             :                         }
    8142         860 :                         if (!PRETTY_PAREN(context))
    8143         852 :                             appendStringInfoChar(buf, ')');
    8144         860 :                         break;
    8145             : 
    8146         130 :                     case NOT_EXPR:
    8147         130 :                         if (!PRETTY_PAREN(context))
    8148         126 :                             appendStringInfoChar(buf, '(');
    8149         130 :                         appendStringInfoString(buf, "NOT ");
    8150         130 :                         get_rule_expr_paren(first_arg, context,
    8151             :                                             false, node);
    8152         130 :                         if (!PRETTY_PAREN(context))
    8153         126 :                             appendStringInfoChar(buf, ')');
    8154         130 :                         break;
    8155             : 
    8156           0 :                     default:
    8157           0 :                         elog(ERROR, "unrecognized boolop: %d",
    8158             :                              (int) expr->boolop);
    8159             :                 }
    8160             :             }
    8161        5496 :             break;
    8162             : 
    8163         192 :         case T_SubLink:
    8164         192 :             get_sublink_expr((SubLink *) node, context);
    8165         192 :             break;
    8166             : 
    8167         196 :         case T_SubPlan:
    8168             :             {
    8169         196 :                 SubPlan    *subplan = (SubPlan *) node;
    8170             : 
    8171             :                 /*
    8172             :                  * We cannot see an already-planned subplan in rule deparsing,
    8173             :                  * only while EXPLAINing a query plan.  We don't try to
    8174             :                  * reconstruct the original SQL, just reference the subplan
    8175             :                  * that appears elsewhere in EXPLAIN's result.
    8176             :                  */
    8177         196 :                 if (subplan->useHashTable)
    8178          54 :                     appendStringInfo(buf, "(hashed %s)", subplan->plan_name);
    8179             :                 else
    8180         142 :                     appendStringInfo(buf, "(%s)", subplan->plan_name);
    8181             :             }
    8182         196 :             break;
    8183             : 
    8184           0 :         case T_AlternativeSubPlan:
    8185             :             {
    8186           0 :                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
    8187             :                 ListCell   *lc;
    8188             : 
    8189             :                 /*
    8190             :                  * This case cannot be reached in normal usage, since no
    8191             :                  * AlternativeSubPlan can appear either in parsetrees or
    8192             :                  * finished plan trees.  We keep it just in case somebody
    8193             :                  * wants to use this code to print planner data structures.
    8194             :                  */
    8195           0 :                 appendStringInfoString(buf, "(alternatives: ");
    8196           0 :                 foreach(lc, asplan->subplans)
    8197             :                 {
    8198           0 :                     SubPlan    *splan = lfirst_node(SubPlan, lc);
    8199             : 
    8200           0 :                     if (splan->useHashTable)
    8201           0 :                         appendStringInfo(buf, "hashed %s", splan->plan_name);
    8202             :                     else
    8203           0 :                         appendStringInfoString(buf, splan->plan_name);
    8204           0 :                     if (lnext(asplan->subplans, lc))
    8205           0 :                         appendStringInfoString(buf, " or ");
    8206             :                 }
    8207           0 :                 appendStringInfoChar(buf, ')');
    8208             :             }
    8209           0 :             break;
    8210             : 
    8211         104 :         case T_FieldSelect:
    8212             :             {
    8213         104 :                 FieldSelect *fselect = (FieldSelect *) node;
    8214         104 :                 Node       *arg = (Node *) fselect->arg;
    8215         104 :                 int         fno = fselect->fieldnum;
    8216             :                 const char *fieldname;
    8217             :                 bool        need_parens;
    8218             : 
    8219             :                 /*
    8220             :                  * Parenthesize the argument unless it's an SubscriptingRef or
    8221             :                  * another FieldSelect.  Note in particular that it would be
    8222             :                  * WRONG to not parenthesize a Var argument; simplicity is not
    8223             :                  * the issue here, having the right number of names is.
    8224             :                  */
    8225         184 :                 need_parens = !IsA(arg, SubscriptingRef) &&
    8226          80 :                     !IsA(arg, FieldSelect);
    8227         104 :                 if (need_parens)
    8228          80 :                     appendStringInfoChar(buf, '(');
    8229         104 :                 get_rule_expr(arg, context, true);
    8230         104 :                 if (need_parens)
    8231          80 :                     appendStringInfoChar(buf, ')');
    8232             : 
    8233             :                 /*
    8234             :                  * Get and print the field name.
    8235             :                  */
    8236         104 :                 fieldname = get_name_for_var_field((Var *) arg, fno,
    8237             :                                                    0, context);
    8238         104 :                 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
    8239             :             }
    8240         104 :             break;
    8241             : 
    8242           4 :         case T_FieldStore:
    8243           0 :             {
    8244           4 :                 FieldStore *fstore = (FieldStore *) node;
    8245             :                 bool        need_parens;
    8246             : 
    8247             :                 /*
    8248             :                  * There is no good way to represent a FieldStore as real SQL,
    8249             :                  * so decompilation of INSERT or UPDATE statements should
    8250             :                  * always use processIndirection as part of the
    8251             :                  * statement-level syntax.  We should only get here when
    8252             :                  * EXPLAIN tries to print the targetlist of a plan resulting
    8253             :                  * from such a statement.  The plan case is even harder than
    8254             :                  * ordinary rules would be, because the planner tries to
    8255             :                  * collapse multiple assignments to the same field or subfield
    8256             :                  * into one FieldStore; so we can see a list of target fields
    8257             :                  * not just one, and the arguments could be FieldStores
    8258             :                  * themselves.  We don't bother to try to print the target
    8259             :                  * field names; we just print the source arguments, with a
    8260             :                  * ROW() around them if there's more than one.  This isn't
    8261             :                  * terribly complete, but it's probably good enough for
    8262             :                  * EXPLAIN's purposes; especially since anything more would be
    8263             :                  * either hopelessly confusing or an even poorer
    8264             :                  * representation of what the plan is actually doing.
    8265             :                  */
    8266           4 :                 need_parens = (list_length(fstore->newvals) != 1);
    8267           4 :                 if (need_parens)
    8268           4 :                     appendStringInfoString(buf, "ROW(");
    8269           4 :                 get_rule_expr((Node *) fstore->newvals, context, showimplicit);
    8270           4 :                 if (need_parens)
    8271           4 :                     appendStringInfoChar(buf, ')');
    8272             :             }
    8273           4 :             break;
    8274             : 
    8275        1198 :         case T_RelabelType:
    8276             :             {
    8277        1198 :                 RelabelType *relabel = (RelabelType *) node;
    8278        1198 :                 Node       *arg = (Node *) relabel->arg;
    8279             : 
    8280        1198 :                 if (relabel->relabelformat == COERCE_IMPLICIT_CAST &&
    8281        1138 :                     !showimplicit)
    8282             :                 {
    8283             :                     /* don't show the implicit cast */
    8284          26 :                     get_rule_expr_paren(arg, context, false, node);
    8285             :                 }
    8286             :                 else
    8287             :                 {
    8288        1172 :                     get_coercion_expr(arg, context,
    8289             :                                       relabel->resulttype,
    8290             :                                       relabel->resulttypmod,
    8291             :                                       node);
    8292             :                 }
    8293             :             }
    8294        1198 :             break;
    8295             : 
    8296         124 :         case T_CoerceViaIO:
    8297             :             {
    8298         124 :                 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
    8299         124 :                 Node       *arg = (Node *) iocoerce->arg;
    8300             : 
    8301         124 :                 if (iocoerce->coerceformat == COERCE_IMPLICIT_CAST &&
    8302           8 :                     !showimplicit)
    8303             :                 {
    8304             :                     /* don't show the implicit cast */
    8305           8 :                     get_rule_expr_paren(arg, context, false, node);
    8306             :                 }
    8307             :                 else
    8308             :                 {
    8309         116 :                     get_coercion_expr(arg, context,
    8310             :                                       iocoerce->resulttype,
    8311             :                                       -1,
    8312             :                                       node);
    8313             :                 }
    8314             :             }
    8315         124 :             break;
    8316             : 
    8317          16 :         case T_ArrayCoerceExpr:
    8318             :             {
    8319          16 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    8320          16 :                 Node       *arg = (Node *) acoerce->arg;
    8321             : 
    8322          16 :                 if (acoerce->coerceformat == COERCE_IMPLICIT_CAST &&
    8323          16 :                     !showimplicit)
    8324             :                 {
    8325             :                     /* don't show the implicit cast */
    8326           0 :                     get_rule_expr_paren(arg, context, false, node);
    8327             :                 }
    8328             :                 else
    8329             :                 {
    8330          16 :                     get_coercion_expr(arg, context,
    8331             :                                       acoerce->resulttype,
    8332             :                                       acoerce->resulttypmod,
    8333             :                                       node);
    8334             :                 }
    8335             :             }
    8336          16 :             break;
    8337             : 
    8338          66 :         case T_ConvertRowtypeExpr:
    8339             :             {
    8340          66 :                 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
    8341          66 :                 Node       *arg = (Node *) convert->arg;
    8342             : 
    8343          66 :                 if (convert->convertformat == COERCE_IMPLICIT_CAST &&
    8344          62 :                     !showimplicit)
    8345             :                 {
    8346             :                     /* don't show the implicit cast */
    8347          18 :                     get_rule_expr_paren(arg, context, false, node);
    8348             :                 }
    8349             :                 else
    8350             :                 {
    8351          48 :                     get_coercion_expr(arg, context,
    8352             :                                       convert->resulttype, -1,
    8353             :                                       node);
    8354             :                 }
    8355             :             }
    8356          66 :             break;
    8357             : 
    8358          40 :         case T_CollateExpr:
    8359           0 :             {
    8360          40 :                 CollateExpr *collate = (CollateExpr *) node;
    8361          40 :                 Node       *arg = (Node *) collate->arg;
    8362             : 
    8363          40 :                 if (!PRETTY_PAREN(context))
    8364          40 :                     appendStringInfoChar(buf, '(');
    8365          40 :                 get_rule_expr_paren(arg, context, showimplicit, node);
    8366          40 :                 appendStringInfo(buf, " COLLATE %s",
    8367             :                                  generate_collation_name(collate->collOid));
    8368          40 :                 if (!PRETTY_PAREN(context))
    8369          40 :                     appendStringInfoChar(buf, ')');
    8370             :             }
    8371          40 :             break;
    8372             : 
    8373         132 :         case T_CaseExpr:
    8374             :             {
    8375         132 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    8376             :                 ListCell   *temp;
    8377             : 
    8378         132 :                 appendContextKeyword(context, "CASE",
    8379             :                                      0, PRETTYINDENT_VAR, 0);
    8380         132 :                 if (caseexpr->arg)
    8381             :                 {
    8382          48 :                     appendStringInfoChar(buf, ' ');
    8383          48 :                     get_rule_expr((Node *) caseexpr->arg, context, true);
    8384             :                 }
    8385         564 :                 foreach(temp, caseexpr->args)
    8386             :                 {
    8387         432 :                     CaseWhen   *when = (CaseWhen *) lfirst(temp);
    8388         432 :                     Node       *w = (Node *) when->expr;
    8389             : 
    8390         432 :                     if (caseexpr->arg)
    8391             :                     {
    8392             :                         /*
    8393             :                          * The parser should have produced WHEN clauses of the
    8394             :                          * form "CaseTestExpr = RHS", possibly with an
    8395             :                          * implicit coercion inserted above the CaseTestExpr.
    8396             :                          * For accurate decompilation of rules it's essential
    8397             :                          * that we show just the RHS.  However in an
    8398             :                          * expression that's been through the optimizer, the
    8399             :                          * WHEN clause could be almost anything (since the
    8400             :                          * equality operator could have been expanded into an
    8401             :                          * inline function).  If we don't recognize the form
    8402             :                          * of the WHEN clause, just punt and display it as-is.
    8403             :                          */
    8404         220 :                         if (IsA(w, OpExpr))
    8405             :                         {
    8406         220 :                             List       *args = ((OpExpr *) w)->args;
    8407             : 
    8408         220 :                             if (list_length(args) == 2 &&
    8409         220 :                                 IsA(strip_implicit_coercions(linitial(args)),
    8410             :                                     CaseTestExpr))
    8411         220 :                                 w = (Node *) lsecond(args);
    8412             :                         }
    8413             :                     }
    8414             : 
    8415         432 :                     if (!PRETTY_INDENT(context))
    8416          16 :                         appendStringInfoChar(buf, ' ');
    8417         432 :                     appendContextKeyword(context, "WHEN ",
    8418             :                                          0, 0, 0);
    8419         432 :                     get_rule_expr(w, context, false);
    8420         432 :                     appendStringInfoString(buf, " THEN ");
    8421         432 :                     get_rule_expr((Node *) when->result, context, true);
    8422             :                 }
    8423         132 :                 if (!PRETTY_INDENT(context))
    8424          16 :                     appendStringInfoChar(buf, ' ');
    8425         132 :                 appendContextKeyword(context, "ELSE ",
    8426             :                                      0, 0, 0);
    8427         132 :                 get_rule_expr((Node *) caseexpr->defresult, context, true);
    8428         132 :                 if (!PRETTY_INDENT(context))
    8429          16 :                     appendStringInfoChar(buf, ' ');
    8430         132 :                 appendContextKeyword(context, "END",
    8431             :                                      -PRETTYINDENT_VAR, 0, 0);
    8432             :             }
    8433         132 :             break;
    8434             : 
    8435           0 :         case T_CaseTestExpr:
    8436             :             {
    8437             :                 /*
    8438             :                  * Normally we should never get here, since for expressions
    8439             :                  * that can contain this node type we attempt to avoid
    8440             :                  * recursing to it.  But in an optimized expression we might
    8441             :                  * be unable to avoid that (see comments for CaseExpr).  If we
    8442             :                  * do see one, print it as CASE_TEST_EXPR.
    8443             :                  */
    8444           0 :                 appendStringInfoString(buf, "CASE_TEST_EXPR");
    8445             :             }
    8446           0 :             break;
    8447             : 
    8448         216 :         case T_ArrayExpr:
    8449         212 :             {
    8450         216 :                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
    8451             : 
    8452         216 :                 appendStringInfoString(buf, "ARRAY[");
    8453         216 :                 get_rule_expr((Node *) arrayexpr->elements, context, true);
    8454         216 :                 appendStringInfoChar(buf, ']');
    8455             : 
    8456             :                 /*
    8457             :                  * If the array isn't empty, we assume its elements are
    8458             :                  * coerced to the desired type.  If it's empty, though, we
    8459             :                  * need an explicit coercion to the array type.
    8460             :                  */
    8461         216 :                 if (arrayexpr->elements == NIL)
    8462           4 :                     appendStringInfo(buf, "::%s",
    8463             :                                      format_type_with_typemod(arrayexpr->array_typeid, -1));
    8464             :             }
    8465         216 :             break;
    8466             : 
    8467          82 :         case T_RowExpr:
    8468          62 :             {
    8469          82 :                 RowExpr    *rowexpr = (RowExpr *) node;
    8470          82 :                 TupleDesc   tupdesc = NULL;
    8471             :                 ListCell   *arg;
    8472             :                 int         i;
    8473             :                 char       *sep;
    8474             : 
    8475             :                 /*
    8476             :                  * If it's a named type and not RECORD, we may have to skip
    8477             :                  * dropped columns and/or claim there are NULLs for added
    8478             :                  * columns.
    8479             :                  */
    8480          82 :                 if (rowexpr->row_typeid != RECORDOID)
    8481             :                 {
    8482          28 :                     tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
    8483             :                     Assert(list_length(rowexpr->args) <= tupdesc->natts);
    8484             :                 }
    8485             : 
    8486             :                 /*
    8487             :                  * SQL99 allows "ROW" to be omitted when there is more than
    8488             :                  * one column, but for simplicity we always print it.
    8489             :                  */
    8490          82 :                 appendStringInfoString(buf, "ROW(");
    8491          82 :                 sep = "";
    8492          82 :                 i = 0;
    8493         220 :                 foreach(arg, rowexpr->args)
    8494             :                 {
    8495         138 :                     Node       *e = (Node *) lfirst(arg);
    8496             : 
    8497         138 :                     if (tupdesc == NULL ||
    8498          52 :                         !TupleDescAttr(tupdesc, i)->attisdropped)
    8499             :                     {
    8500         138 :                         appendStringInfoString(buf, sep);
    8501             :                         /* Whole-row Vars need special treatment here */
    8502         138 :                         get_rule_expr_toplevel(e, context, true);
    8503         138 :                         sep = ", ";
    8504             :                     }
    8505         138 :                     i++;
    8506             :                 }
    8507          82 :                 if (tupdesc != NULL)
    8508             :                 {
    8509          32 :                     while (i < tupdesc->natts)
    8510             :                     {
    8511           4 :                         if (!TupleDescAttr(tupdesc, i)->attisdropped)
    8512             :                         {
    8513           4 :                             appendStringInfoString(buf, sep);
    8514           4 :                             appendStringInfoString(buf, "NULL");
    8515           4 :                             sep = ", ";
    8516             :                         }
    8517           4 :                         i++;
    8518             :                     }
    8519             : 
    8520          28 :                     ReleaseTupleDesc(tupdesc);
    8521             :                 }
    8522          82 :                 appendStringInfoChar(buf, ')');
    8523          82 :                 if (rowexpr->row_format == COERCE_EXPLICIT_CAST)
    8524          20 :                     appendStringInfo(buf, "::%s",
    8525             :                                      format_type_with_typemod(rowexpr->row_typeid, -1));
    8526             :             }
    8527          82 :             break;
    8528             : 
    8529          40 :         case T_RowCompareExpr:
    8530             :             {
    8531          40 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    8532             :                 ListCell   *arg;
    8533             :                 char       *sep;
    8534             : 
    8535             :                 /*
    8536             :                  * SQL99 allows "ROW" to be omitted when there is more than
    8537             :                  * one column, but for simplicity we always print it.
    8538             :                  */
    8539          40 :                 appendStringInfoString(buf, "(ROW(");
    8540          40 :                 sep = "";
    8541         132 :                 foreach(arg, rcexpr->largs)
    8542             :                 {
    8543          92 :                     Node       *e = (Node *) lfirst(arg);
    8544             : 
    8545          92 :                     appendStringInfoString(buf, sep);
    8546          92 :                     get_rule_expr(e, context, true);
    8547          92 :                     sep = ", ";
    8548             :                 }
    8549             : 
    8550             :                 /*
    8551             :                  * We assume that the name of the first-column operator will
    8552             :                  * do for all the rest too.  This is definitely open to
    8553             :                  * failure, eg if some but not all operators were renamed
    8554             :                  * since the construct was parsed, but there seems no way to
    8555             :                  * be perfect.
    8556             :                  */
    8557          40 :                 appendStringInfo(buf, ") %s ROW(",
    8558          40 :                                  generate_operator_name(linitial_oid(rcexpr->opnos),
    8559          40 :                                                         exprType(linitial(rcexpr->largs)),
    8560          40 :                                                         exprType(linitial(rcexpr->rargs))));
    8561          40 :                 sep = "";
    8562         132 :                 foreach(arg, rcexpr->rargs)
    8563             :                 {
    8564          92 :                     Node       *e = (Node *) lfirst(arg);
    8565             : 
    8566          92 :                     appendStringInfoString(buf, sep);
    8567          92 :                     get_rule_expr(e, context, true);
    8568          92 :                     sep = ", ";
    8569             :                 }
    8570          40 :                 appendStringInfoString(buf, "))");
    8571             :             }
    8572          40 :             break;
    8573             : 
    8574         632 :         case T_CoalesceExpr:
    8575             :             {
    8576         632 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
    8577             : 
    8578         632 :                 appendStringInfoString(buf, "COALESCE(");
    8579         632 :                 get_rule_expr((Node *) coalesceexpr->args, context, true);
    8580         632 :                 appendStringInfoChar(buf, ')');
    8581             :             }
    8582         632 :             break;
    8583             : 
    8584          24 :         case T_MinMaxExpr:
    8585             :             {
    8586          24 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
    8587             : 
    8588          24 :                 switch (minmaxexpr->op)
    8589             :                 {
    8590           4 :                     case IS_GREATEST:
    8591           4 :                         appendStringInfoString(buf, "GREATEST(");
    8592           4 :                         break;
    8593          20 :                     case IS_LEAST:
    8594          20 :                         appendStringInfoString(buf, "LEAST(");
    8595          20 :                         break;
    8596             :                 }
    8597          24 :                 get_rule_expr((Node *) minmaxexpr->args, context, true);
    8598          24 :                 appendStringInfoChar(buf, ')');
    8599             :             }
    8600          24 :             break;
    8601             : 
    8602         600 :         case T_SQLValueFunction:
    8603             :             {
    8604         600 :                 SQLValueFunction *svf = (SQLValueFunction *) node;
    8605             : 
    8606             :                 /*
    8607             :                  * Note: this code knows that typmod for time, timestamp, and
    8608             :                  * timestamptz just prints as integer.
    8609             :                  */
    8610         600 :                 switch (svf->op)
    8611             :                 {
    8612           4 :                     case SVFOP_CURRENT_DATE:
    8613           4 :                         appendStringInfoString(buf, "CURRENT_DATE");
    8614           4 :                         break;
    8615           0 :                     case SVFOP_CURRENT_TIME:
    8616           0 :                         appendStringInfoString(buf, "CURRENT_TIME");
    8617           0 :                         break;
    8618           0 :                     case SVFOP_CURRENT_TIME_N:
    8619           0 :                         appendStringInfo(buf, "CURRENT_TIME(%d)", svf->typmod);
    8620           0 :                         break;
    8621           0 :                     case SVFOP_CURRENT_TIMESTAMP:
    8622           0 :                         appendStringInfoString(buf, "CURRENT_TIMESTAMP");
    8623           0 :                         break;
    8624         402 :                     case SVFOP_CURRENT_TIMESTAMP_N:
    8625         402 :                         appendStringInfo(buf, "CURRENT_TIMESTAMP(%d)",
    8626             :                                          svf->typmod);
    8627         402 :                         break;
    8628           0 :                     case SVFOP_LOCALTIME:
    8629           0 :                         appendStringInfoString(buf, "LOCALTIME");
    8630           0 :                         break;
    8631           0 :                     case SVFOP_LOCALTIME_N:
    8632           0 :                         appendStringInfo(buf, "LOCALTIME(%d)", svf->typmod);
    8633           0 :                         break;
    8634          12 :                     case SVFOP_LOCALTIMESTAMP:
    8635          12 :                         appendStringInfoString(buf, "LOCALTIMESTAMP");
    8636          12 :                         break;
    8637           4 :                     case SVFOP_LOCALTIMESTAMP_N:
    8638           4 :                         appendStringInfo(buf, "LOCALTIMESTAMP(%d)",
    8639             :                                          svf->typmod);
    8640           4 :                         break;
    8641           0 :                     case SVFOP_CURRENT_ROLE:
    8642           0 :                         appendStringInfoString(buf, "CURRENT_ROLE");
    8643           0 :                         break;
    8644         174 :                     case SVFOP_CURRENT_USER:
    8645         174 :                         appendStringInfoString(buf, "CURRENT_USER");
    8646         174 :                         break;
    8647           0 :                     case SVFOP_USER:
    8648           0 :                         appendStringInfoString(buf, "USER");
    8649           0 :                         break;
    8650           4 :                     case SVFOP_SESSION_USER:
    8651           4 :                         appendStringInfoString(buf, "SESSION_USER");
    8652           4 :                         break;
    8653           0 :                     case SVFOP_CURRENT_CATALOG:
    8654           0 :                         appendStringInfoString(buf, "CURRENT_CATALOG");
    8655           0 :                         break;
    8656           0 :                     case SVFOP_CURRENT_SCHEMA:
    8657           0 :                         appendStringInfoString(buf, "CURRENT_SCHEMA");
    8658           0 :                         break;
    8659             :                 }
    8660         600 :             }
    8661         600 :             break;
    8662             : 
    8663          90 :         case T_XmlExpr:
    8664             :             {
    8665          90 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    8666          90 :                 bool        needcomma = false;
    8667             :                 ListCell   *arg;
    8668             :                 ListCell   *narg;
    8669             :                 Const      *con;
    8670             : 
    8671          90 :                 switch (xexpr->op)
    8672             :                 {
    8673          10 :                     case IS_XMLCONCAT:
    8674          10 :                         appendStringInfoString(buf, "XMLCONCAT(");
    8675          10 :                         break;
    8676          20 :                     case IS_XMLELEMENT:
    8677          20 :                         appendStringInfoString(buf, "XMLELEMENT(");
    8678          20 :                         break;
    8679          10 :                     case IS_XMLFOREST:
    8680          10 :                         appendStringInfoString(buf, "XMLFOREST(");
    8681          10 :                         break;
    8682          10 :                     case IS_XMLPARSE:
    8683          10 :                         appendStringInfoString(buf, "XMLPARSE(");
    8684          10 :                         break;
    8685          10 :                     case IS_XMLPI:
    8686          10 :                         appendStringInfoString(buf, "XMLPI(");
    8687          10 :                         break;
    8688          10 :                     case IS_XMLROOT:
    8689          10 :                         appendStringInfoString(buf, "XMLROOT(");
    8690          10 :                         break;
    8691          20 :                     case IS_XMLSERIALIZE:
    8692          20 :                         appendStringInfoString(buf, "XMLSERIALIZE(");
    8693          20 :                         break;
    8694           0 :                     case IS_DOCUMENT:
    8695           0 :                         break;
    8696             :                 }
    8697          90 :                 if (xexpr->op == IS_XMLPARSE || xexpr->op == IS_XMLSERIALIZE)
    8698             :                 {
    8699          30 :                     if (xexpr->xmloption == XMLOPTION_DOCUMENT)
    8700           0 :                         appendStringInfoString(buf, "DOCUMENT ");
    8701             :                     else
    8702          30 :                         appendStringInfoString(buf, "CONTENT ");
    8703             :                 }
    8704          90 :                 if (xexpr->name)
    8705             :                 {
    8706          30 :                     appendStringInfo(buf, "NAME %s",
    8707          30 :                                      quote_identifier(map_xml_name_to_sql_identifier(xexpr->name)));
    8708          30 :                     needcomma = true;
    8709             :                 }
    8710          90 :                 if (xexpr->named_args)
    8711             :                 {
    8712          20 :                     if (xexpr->op != IS_XMLFOREST)
    8713             :                     {
    8714          10 :                         if (needcomma)
    8715          10 :                             appendStringInfoString(buf, ", ");
    8716          10 :                         appendStringInfoString(buf, "XMLATTRIBUTES(");
    8717          10 :                         needcomma = false;
    8718             :                     }
    8719          70 :                     forboth(arg, xexpr->named_args, narg, xexpr->arg_names)
    8720             :                     {
    8721          50 :                         Node       *e = (Node *) lfirst(arg);
    8722          50 :                         char       *argname = strVal(lfirst(narg));
    8723             : 
    8724          50 :                         if (needcomma)
    8725          30 :                             appendStringInfoString(buf, ", ");
    8726          50 :                         get_rule_expr((Node *) e, context, true);
    8727          50 :                         appendStringInfo(buf, " AS %s",
    8728          50 :                                          quote_identifier(map_xml_name_to_sql_identifier(argname)));
    8729          50 :                         needcomma = true;
    8730             :                     }
    8731          20 :                     if (xexpr->op != IS_XMLFOREST)
    8732          10 :                         appendStringInfoChar(buf, ')');
    8733             :                 }
    8734          90 :                 if (xexpr->args)
    8735             :                 {
    8736          80 :                     if (needcomma)
    8737          30 :                         appendStringInfoString(buf, ", ");
    8738          80 :                     switch (xexpr->op)
    8739             :                     {
    8740          60 :                         case IS_XMLCONCAT:
    8741             :                         case IS_XMLELEMENT:
    8742             :                         case IS_XMLFOREST:
    8743             :                         case IS_XMLPI:
    8744             :                         case IS_XMLSERIALIZE:
    8745             :                             /* no extra decoration needed */
    8746          60 :                             get_rule_expr((Node *) xexpr->args, context, true);
    8747          60 :                             break;
    8748          10 :                         case IS_XMLPARSE:
    8749             :                             Assert(list_length(xexpr->args) == 2);
    8750             : 
    8751          10 :                             get_rule_expr((Node *) linitial(xexpr->args),
    8752             :                                           context, true);
    8753             : 
    8754          10 :                             con = lsecond_node(Const, xexpr->args);
    8755             :                             Assert(!con->constisnull);
    8756          10 :                             if (DatumGetBool(con->constvalue))
    8757           0 :                                 appendStringInfoString(buf,
    8758             :                                                        " PRESERVE WHITESPACE");
    8759             :                             else
    8760          10 :                                 appendStringInfoString(buf,
    8761             :                                                        " STRIP WHITESPACE");
    8762          10 :                             break;
    8763          10 :                         case IS_XMLROOT:
    8764             :                             Assert(list_length(xexpr->args) == 3);
    8765             : 
    8766          10 :                             get_rule_expr((Node *) linitial(xexpr->args),
    8767             :                                           context, true);
    8768             : 
    8769          10 :                             appendStringInfoString(buf, ", VERSION ");
    8770          10 :                             con = (Const *) lsecond(xexpr->args);
    8771          10 :                             if (IsA(con, Const) &&
    8772          10 :                                 con->constisnull)
    8773          10 :                                 appendStringInfoString(buf, "NO VALUE");
    8774             :                             else
    8775           0 :                                 get_rule_expr((Node *) con, context, false);
    8776             : 
    8777          10 :                             con = lthird_node(Const, xexpr->args);
    8778          10 :                             if (con->constisnull)
    8779             :                                  /* suppress STANDALONE NO VALUE */ ;
    8780             :                             else
    8781             :                             {
    8782          10 :                                 switch (DatumGetInt32(con->constvalue))
    8783             :                                 {
    8784          10 :                                     case XML_STANDALONE_YES:
    8785          10 :                                         appendStringInfoString(buf,
    8786             :                                                                ", STANDALONE YES");
    8787          10 :                                         break;
    8788           0 :                                     case XML_STANDALONE_NO:
    8789           0 :                                         appendStringInfoString(buf,
    8790             :                                                                ", STANDALONE NO");
    8791           0 :                                         break;
    8792           0 :                                     case XML_STANDALONE_NO_VALUE:
    8793           0 :                                         appendStringInfoString(buf,
    8794             :                                                                ", STANDALONE NO VALUE");
    8795           0 :                                         break;
    8796           0 :                                     default:
    8797           0 :                                         break;
    8798             :                                 }
    8799             :                             }
    8800          10 :                             break;
    8801           0 :                         case IS_DOCUMENT:
    8802           0 :                             get_rule_expr_paren((Node *) xexpr->args, context, false, node);
    8803           0 :                             break;
    8804             :                     }
    8805             : 
    8806          10 :                 }
    8807          90 :                 if (xexpr->op == IS_XMLSERIALIZE)
    8808          20 :                     appendStringInfo(buf, " AS %s",
    8809             :                                      format_type_with_typemod(xexpr->type,
    8810             :                                                               xexpr->typmod));
    8811          90 :                 if (xexpr->op == IS_DOCUMENT)
    8812           0 :                     appendStringInfoString(buf, " IS DOCUMENT");
    8813             :                 else
    8814          90 :                     appendStringInfoChar(buf, ')');
    8815             :             }
    8816          90 :             break;
    8817             : 
    8818        1006 :         case T_NullTest:
    8819          16 :             {
    8820        1006 :                 NullTest   *ntest = (NullTest *) node;
    8821             : 
    8822        1006 :                 if (!PRETTY_PAREN(context))
    8823         990 :                     appendStringInfoChar(buf, '(');
    8824        1006 :                 get_rule_expr_paren((Node *) ntest->arg, context, true, node);
    8825             : 
    8826             :                 /*
    8827             :                  * For scalar inputs, we prefer to print as IS [NOT] NULL,
    8828             :                  * which is shorter and traditional.  If it's a rowtype input
    8829             :                  * but we're applying a scalar test, must print IS [NOT]
    8830             :                  * DISTINCT FROM NULL to be semantically correct.
    8831             :                  */
    8832        1006 :                 if (ntest->argisrow ||
    8833         994 :                     !type_is_rowtype(exprType((Node *) ntest->arg)))
    8834             :                 {
    8835        1988 :                     switch (ntest->nulltesttype)
    8836             :                     {
    8837         288 :                         case IS_NULL:
    8838         288 :                             appendStringInfoString(buf, " IS NULL");
    8839         288 :                             break;
    8840         706 :                         case IS_NOT_NULL:
    8841         706 :                             appendStringInfoString(buf, " IS NOT NULL");
    8842         706 :                             break;
    8843           0 :                         default:
    8844           0 :                             elog(ERROR, "unrecognized nulltesttype: %d",
    8845             :                                  (int) ntest->nulltesttype);
    8846             :                     }
    8847             :                 }
    8848             :                 else
    8849             :                 {
    8850          12 :                     switch (ntest->nulltesttype)
    8851             :                     {
    8852           4 :                         case IS_NULL:
    8853           4 :                             appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL");
    8854           4 :                             break;
    8855           8 :                         case IS_NOT_NULL:
    8856           8 :                             appendStringInfoString(buf, " IS DISTINCT FROM NULL");
    8857           8 :                             break;
    8858           0 :                         default:
    8859           0 :                             elog(ERROR, "unrecognized nulltesttype: %d",
    8860             :                                  (int) ntest->nulltesttype);
    8861             :                     }
    8862             :                 }
    8863        1006 :                 if (!PRETTY_PAREN(context))
    8864         990 :                     appendStringInfoChar(buf, ')');
    8865             :             }
    8866        1006 :             break;
    8867             : 
    8868          48 :         case T_BooleanTest:
    8869           0 :             {
    8870          48 :                 BooleanTest *btest = (BooleanTest *) node;
    8871             : 
    8872          48 :                 if (!PRETTY_PAREN(context))
    8873          48 :                     appendStringInfoChar(buf, '(');
    8874          48 :                 get_rule_expr_paren((Node *) btest->arg, context, false, node);
    8875          48 :                 switch (btest->booltesttype)
    8876             :                 {
    8877           8 :                     case IS_TRUE:
    8878           8 :                         appendStringInfoString(buf, " IS TRUE");
    8879           8 :                         break;
    8880          16 :                     case IS_NOT_TRUE:
    8881          16 :                         appendStringInfoString(buf, " IS NOT TRUE");
    8882          16 :                         break;
    8883           0 :                     case IS_FALSE:
    8884           0 :                         appendStringInfoString(buf, " IS FALSE");
    8885           0 :                         break;
    8886           0 :                     case IS_NOT_FALSE:
    8887           0 :                         appendStringInfoString(buf, " IS NOT FALSE");
    8888           0 :                         break;
    8889          12 :                     case IS_UNKNOWN:
    8890          12 :                         appendStringInfoString(buf, " IS UNKNOWN");
    8891          12 :                         break;
    8892          12 :                     case IS_NOT_UNKNOWN:
    8893          12 :                         appendStringInfoString(buf, " IS NOT UNKNOWN");
    8894          12 :                         break;
    8895           0 :                     default:
    8896           0 :                         elog(ERROR, "unrecognized booltesttype: %d",
    8897             :                              (int) btest->booltesttype);
    8898             :                 }
    8899          48 :                 if (!PRETTY_PAREN(context))
    8900          48 :                     appendStringInfoChar(buf, ')');
    8901             :             }
    8902          48 :             break;
    8903             : 
    8904          20 :         case T_CoerceToDomain:
    8905             :             {
    8906          20 :                 CoerceToDomain *ctest = (CoerceToDomain *) node;
    8907          20 :                 Node       *arg = (Node *) ctest->arg;
    8908             : 
    8909          20 :                 if (ctest->coercionformat == COERCE_IMPLICIT_CAST &&
    8910          14 :                     !showimplicit)
    8911             :                 {
    8912             :                     /* don't show the implicit cast */
    8913          14 :                     get_rule_expr(arg, context, false);
    8914             :                 }
    8915             :                 else
    8916             :                 {
    8917           6 :                     get_coercion_expr(arg, context,
    8918             :                                       ctest->resulttype,
    8919             :                                       ctest->resulttypmod,
    8920             :                                       node);
    8921             :                 }
    8922             :             }
    8923          20 :             break;
    8924             : 
    8925         170 :         case T_CoerceToDomainValue:
    8926         170 :             appendStringInfoString(buf, "VALUE");
    8927         170 :             break;
    8928             : 
    8929           0 :         case T_SetToDefault:
    8930           0 :             appendStringInfoString(buf, "DEFAULT");
    8931           0 :             break;
    8932             : 
    8933          12 :         case T_CurrentOfExpr:
    8934             :             {
    8935          12 :                 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
    8936             : 
    8937          12 :                 if (cexpr->cursor_name)
    8938          12 :                     appendStringInfo(buf, "CURRENT OF %s",
    8939          12 :                                      quote_identifier(cexpr->cursor_name));
    8940             :                 else
    8941           0 :                     appendStringInfo(buf, "CURRENT OF $%d",
    8942             :                                      cexpr->cursor_param);
    8943             :             }
    8944          12 :             break;
    8945             : 
    8946           0 :         case T_NextValueExpr:
    8947             :             {
    8948           0 :                 NextValueExpr *nvexpr = (NextValueExpr *) node;
    8949             : 
    8950             :                 /*
    8951             :                  * This isn't exactly nextval(), but that seems close enough
    8952             :                  * for EXPLAIN's purposes.
    8953             :                  */
    8954           0 :                 appendStringInfoString(buf, "nextval(");
    8955           0 :                 simple_quote_literal(buf,
    8956           0 :                                      generate_relation_name(nvexpr->seqid,
    8957             :                                                             NIL));
    8958           0 :                 appendStringInfoChar(buf, ')');
    8959             :             }
    8960           0 :             break;
    8961             : 
    8962          16 :         case T_InferenceElem:
    8963           8 :             {
    8964          16 :                 InferenceElem *iexpr = (InferenceElem *) node;
    8965             :                 bool        save_varprefix;
    8966             :                 bool        need_parens;
    8967             : 
    8968             :                 /*
    8969             :                  * InferenceElem can only refer to target relation, so a
    8970             :                  * prefix is not useful, and indeed would cause parse errors.
    8971             :                  */
    8972          16 :                 save_varprefix = context->varprefix;
    8973          16 :                 context->varprefix = false;
    8974             : 
    8975             :                 /*
    8976             :                  * Parenthesize the element unless it's a simple Var or a bare
    8977             :                  * function call.  Follows pg_get_indexdef_worker().
    8978             :                  */
    8979          16 :                 need_parens = !IsA(iexpr->expr, Var);
    8980          16 :                 if (IsA(iexpr->expr, FuncExpr) &&
    8981           0 :                     ((FuncExpr *) iexpr->expr)->funcformat ==
    8982             :                     COERCE_EXPLICIT_CALL)
    8983           0 :                     need_parens = false;
    8984             : 
    8985          16 :                 if (need_parens)
    8986           0 :                     appendStringInfoChar(buf, '(');
    8987          16 :                 get_rule_expr((Node *) iexpr->expr,
    8988             :                               context, false);
    8989          16 :                 if (need_parens)
    8990           0 :                     appendStringInfoChar(buf, ')');
    8991             : 
    8992          16 :                 context->varprefix = save_varprefix;
    8993             : 
    8994          16 :                 if (iexpr->infercollid)
    8995           8 :                     appendStringInfo(buf, " COLLATE %s",
    8996             :                                      generate_collation_name(iexpr->infercollid));
    8997             : 
    8998             :                 /* Add the operator class name, if not default */
    8999          16 :                 if (iexpr->inferopclass)
    9000             :                 {
    9001           8 :                     Oid         inferopclass = iexpr->inferopclass;
    9002           8 :                     Oid         inferopcinputtype = get_opclass_input_type(iexpr->inferopclass);
    9003             : 
    9004           8 :                     get_opclass_name(inferopclass, inferopcinputtype, buf);
    9005             :                 }
    9006             :             }
    9007          16 :             break;
    9008             : 
    9009        1870 :         case T_PartitionBoundSpec:
    9010             :             {
    9011        1870 :                 PartitionBoundSpec *spec = (PartitionBoundSpec *) node;
    9012             :                 ListCell   *cell;
    9013             :                 char       *sep;
    9014             : 
    9015        1870 :                 if (spec->is_default)
    9016             :                 {
    9017          82 :                     appendStringInfoString(buf, "DEFAULT");
    9018          82 :                     break;
    9019             :                 }
    9020             : 
    9021        1788 :                 switch (spec->strategy)
    9022             :                 {
    9023          78 :                     case PARTITION_STRATEGY_HASH:
    9024             :                         Assert(spec->modulus > 0 && spec->remainder >= 0);
    9025             :                         Assert(spec->modulus > spec->remainder);
    9026             : 
    9027          78 :                         appendStringInfoString(buf, "FOR VALUES");
    9028          78 :                         appendStringInfo(buf, " WITH (modulus %d, remainder %d)",
    9029             :                                          spec->modulus, spec->remainder);
    9030          78 :                         break;
    9031             : 
    9032         648 :                     case PARTITION_STRATEGY_LIST:
    9033             :                         Assert(spec->listdatums != NIL);
    9034             : 
    9035         648 :                         appendStringInfoString(buf, "FOR VALUES IN (");
    9036         648 :                         sep = "";
    9037        1792 :                         foreach(cell, spec->listdatums)
    9038             :                         {
    9039        1144 :                             Const      *val = castNode(Const, lfirst(cell));
    9040             : 
    9041        1144 :                             appendStringInfoString(buf, sep);
    9042        1144 :                             get_const_expr(val, context, -1);
    9043        1144 :                             sep = ", ";
    9044             :                         }
    9045             : 
    9046         648 :                         appendStringInfoChar(buf, ')');
    9047         648 :                         break;
    9048             : 
    9049        1062 :                     case PARTITION_STRATEGY_RANGE:
    9050             :                         Assert(spec->lowerdatums != NIL &&
    9051             :                                spec->upperdatums != NIL &&
    9052             :                                list_length(spec->lowerdatums) ==
    9053             :                                list_length(spec->upperdatums));
    9054             : 
    9055        1062 :                         appendStringInfo(buf, "FOR VALUES FROM %s TO %s",
    9056             :                                          get_range_partbound_string(spec->lowerdatums),
    9057             :                                          get_range_partbound_string(spec->upperdatums));
    9058        1062 :                         break;
    9059             : 
    9060           0 :                     default:
    9061           0 :                         elog(ERROR, "unrecognized partition strategy: %d",
    9062             :                              (int) spec->strategy);
    9063             :                         break;
    9064             :                 }
    9065             :             }
    9066        1788 :             break;
    9067             : 
    9068        1306 :         case T_List:
    9069        1306 :             {
    9070             :                 char       *sep;
    9071             :                 ListCell   *l;
    9072             : 
    9073        1306 :                 sep = "";
    9074        3980 :                 foreach(l, (List *) node)
    9075             :                 {
    9076        2674 :                     appendStringInfoString(buf, sep);
    9077        2674 :                     get_rule_expr((Node *) lfirst(l), context, showimplicit);
    9078        2674 :                     sep = ", ";
    9079             :                 }
    9080             :             }
    9081        1306 :             break;
    9082             : 
    9083          16 :         case T_TableFunc:
    9084          16 :             get_tablefunc((TableFunc *) node, context, showimplicit);
    9085          16 :             break;
    9086             : 
    9087           0 :         default:
    9088           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
    9089             :             break;
    9090             :     }
    9091             : }
    9092             : 
    9093             : /*
    9094             :  * get_rule_expr_toplevel       - Parse back a toplevel expression
    9095             :  *
    9096             :  * Same as get_rule_expr(), except that if the expr is just a Var, we pass
    9097             :  * istoplevel = true not false to get_variable().  This causes whole-row Vars
    9098             :  * to get printed with decoration that will prevent expansion of "*".
    9099             :  * We need to use this in contexts such as ROW() and VALUES(), where the
    9100             :  * parser would expand "foo.*" appearing at top level.  (In principle we'd
    9101             :  * use this in get_target_list() too, but that has additional worries about
    9102             :  * whether to print AS, so it needs to invoke get_variable() directly anyway.)
    9103             :  */
    9104             : static void
    9105        1002 : get_rule_expr_toplevel(Node *node, deparse_context *context,
    9106             :                        bool showimplicit)
    9107             : {
    9108        1002 :     if (node && IsA(node, Var))
    9109         254 :         (void) get_variable((Var *) node, 0, true, context);
    9110             :     else
    9111         748 :         get_rule_expr(node, context, showimplicit);
    9112        1002 : }
    9113             : 
    9114             : /*
    9115             :  * get_rule_expr_funccall       - Parse back a function-call expression
    9116             :  *
    9117             :  * Same as get_rule_expr(), except that we guarantee that the output will
    9118             :  * look like a function call, or like one of the things the grammar treats as
    9119             :  * equivalent to a function call (see the func_expr_windowless production).
    9120             :  * This is needed in places where the grammar uses func_expr_windowless and
    9121             :  * you can't substitute a parenthesized a_expr.  If what we have isn't going
    9122             :  * to look like a function call, wrap it in a dummy CAST() expression, which
    9123             :  * will satisfy the grammar --- and, indeed, is likely what the user wrote to
    9124             :  * produce such a thing.
    9125             :  */
    9126             : static void
    9127         256 : get_rule_expr_funccall(Node *node, deparse_context *context,
    9128             :                        bool showimplicit)
    9129             : {
    9130         256 :     if (looks_like_function(node))
    9131         248 :         get_rule_expr(node, context, showimplicit);
    9132             :     else
    9133             :     {
    9134           8 :         StringInfo  buf = context->buf;
    9135             : 
    9136           8 :         appendStringInfoString(buf, "CAST(");
    9137             :         /* no point in showing any top-level implicit cast */
    9138           8 :         get_rule_expr(node, context, false);
    9139           8 :         appendStringInfo(buf, " AS %s)",
    9140             :                          format_type_with_typemod(exprType(node),
    9141             :                                                   exprTypmod(node)));
    9142             :     }
    9143         256 : }
    9144             : 
    9145             : /*
    9146             :  * Helper function to identify node types that satisfy func_expr_windowless.
    9147             :  * If in doubt, "false" is always a safe answer.
    9148             :  */
    9149             : static bool
    9150         770 : looks_like_function(Node *node)
    9151             : {
    9152         770 :     if (node == NULL)
    9153           0 :         return false;           /* probably shouldn't happen */
    9154         770 :     switch (nodeTag(node))
    9155             :     {
    9156         310 :         case T_FuncExpr:
    9157             :             /* OK, unless it's going to deparse as a cast */
    9158         318 :             return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL ||
    9159           8 :                     ((FuncExpr *) node)->funcformat == COERCE_SQL_SYNTAX);
    9160          12 :         case T_NullIfExpr:
    9161             :         case T_CoalesceExpr:
    9162             :         case T_MinMaxExpr:
    9163             :         case T_SQLValueFunction:
    9164             :         case T_XmlExpr:
    9165             :             /* these are all accepted by func_expr_common_subexpr */
    9166          12 :             return true;
    9167         448 :         default:
    9168         448 :             break;
    9169             :     }
    9170         448 :     return false;
    9171             : }
    9172             : 
    9173             : 
    9174             : /*
    9175             :  * get_oper_expr            - Parse back an OpExpr node
    9176             :  */
    9177             : static void
    9178       28026 : get_oper_expr(OpExpr *expr, deparse_context *context)
    9179             : {
    9180       28026 :     StringInfo  buf = context->buf;
    9181       28026 :     Oid         opno = expr->opno;
    9182       28026 :     List       *args = expr->args;
    9183             : 
    9184       28026 :     if (!PRETTY_PAREN(context))
    9185       27050 :         appendStringInfoChar(buf, '(');
    9186       28026 :     if (list_length(args) == 2)
    9187             :     {
    9188             :         /* binary operator */
    9189       28006 :         Node       *arg1 = (Node *) linitial(args);
    9190       28006 :         Node       *arg2 = (Node *) lsecond(args);
    9191             : 
    9192       28006 :         get_rule_expr_paren(arg1, context, true, (Node *) expr);
    9193       28006 :         appendStringInfo(buf, " %s ",
    9194             :                          generate_operator_name(opno,
    9195             :                                                 exprType(arg1),
    9196             :                                                 exprType(arg2)));
    9197       28006 :         get_rule_expr_paren(arg2, context, true, (Node *) expr);
    9198             :     }
    9199             :     else
    9200             :     {
    9201             :         /* prefix operator */
    9202          20 :         Node       *arg = (Node *) linitial(args);
    9203             : 
    9204          20 :         appendStringInfo(buf, "%s ",
    9205             :                          generate_operator_name(opno,
    9206             :                                                 InvalidOid,
    9207             :                                                 exprType(arg)));
    9208          20 :         get_rule_expr_paren(arg, context, true, (Node *) expr);
    9209             :     }
    9210       28026 :     if (!PRETTY_PAREN(context))
    9211       27050 :         appendStringInfoChar(buf, ')');
    9212       28026 : }
    9213             : 
    9214             : /*
    9215             :  * get_func_expr            - Parse back a FuncExpr node
    9216             :  */
    9217             : static void
    9218        5008 : get_func_expr(FuncExpr *expr, deparse_context *context,
    9219             :               bool showimplicit)
    9220             : {
    9221        5008 :     StringInfo  buf = context->buf;
    9222        5008 :     Oid         funcoid = expr->funcid;
    9223             :     Oid         argtypes[FUNC_MAX_ARGS];
    9224             :     int         nargs;
    9225             :     List       *argnames;
    9226             :     bool        use_variadic;
    9227             :     ListCell   *l;
    9228             : 
    9229             :     /*
    9230             :      * If the function call came from an implicit coercion, then just show the
    9231             :      * first argument --- unless caller wants to see implicit coercions.
    9232             :      */
    9233        5008 :     if (expr->funcformat == COERCE_IMPLICIT_CAST && !showimplicit)
    9234             :     {
    9235         594 :         get_rule_expr_paren((Node *) linitial(expr->args), context,
    9236             :                             false, (Node *) expr);
    9237        1416 :         return;
    9238             :     }
    9239             : 
    9240             :     /*
    9241             :      * If the function call came from a cast, then show the first argument
    9242             :      * plus an explicit cast operation.
    9243             :      */
    9244        4414 :     if (expr->funcformat == COERCE_EXPLICIT_CAST ||
    9245        4114 :         expr->funcformat == COERCE_IMPLICIT_CAST)
    9246             :     {
    9247         762 :         Node       *arg = linitial(expr->args);
    9248         762 :         Oid         rettype = expr->funcresulttype;
    9249             :         int32       coercedTypmod;
    9250             : 
    9251             :         /* Get the typmod if this is a length-coercion function */
    9252         762 :         (void) exprIsLengthCoercion((Node *) expr, &coercedTypmod);
    9253             : 
    9254         762 :         get_coercion_expr(arg, context,
    9255             :                           rettype, coercedTypmod,
    9256             :                           (Node *) expr);
    9257             : 
    9258         762 :         return;
    9259             :     }
    9260             : 
    9261             :     /*
    9262             :      * If the function was called using one of the SQL spec's random special
    9263             :      * syntaxes, try to reproduce that.  If we don't recognize the function,
    9264             :      * fall through.
    9265             :      */
    9266        3652 :     if (expr->funcformat == COERCE_SQL_SYNTAX)
    9267             :     {
    9268          68 :         if (get_func_sql_syntax(expr, context))
    9269          60 :             return;
    9270             :     }
    9271             : 
    9272             :     /*
    9273             :      * Normal function: display as proname(args).  First we need to extract
    9274             :      * the argument datatypes.
    9275             :      */
    9276        3592 :     if (list_length(expr->args) > FUNC_MAX_ARGS)
    9277           0 :         ereport(ERROR,
    9278             :                 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
    9279             :                  errmsg("too many arguments")));
    9280        3592 :     nargs = 0;
    9281        3592 :     argnames = NIL;
    9282        7332 :     foreach(l, expr->args)
    9283             :     {
    9284        3740 :         Node       *arg = (Node *) lfirst(l);
    9285             : 
    9286        3740 :         if (IsA(arg, NamedArgExpr))
    9287          12 :             argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
    9288        3740 :         argtypes[nargs] = exprType(arg);
    9289        3740 :         nargs++;
    9290             :     }
    9291             : 
    9292        3592 :     appendStringInfo(buf, "%s(",
    9293             :                      generate_function_name(funcoid, nargs,
    9294             :                                             argnames, argtypes,
    9295        3592 :                                             expr->funcvariadic,
    9296             :                                             &use_variadic,
    9297             :                                             context->special_exprkind));
    9298        3592 :     nargs = 0;
    9299        7332 :     foreach(l, expr->args)
    9300             :     {
    9301        3740 :         if (nargs++ > 0)
    9302         544 :             appendStringInfoString(buf, ", ");
    9303        3740 :         if (use_variadic && lnext(expr->args, l) == NULL)
    9304           0 :             appendStringInfoString(buf, "VARIADIC ");
    9305        3740 :         get_rule_expr((Node *) lfirst(l), context, true);
    9306             :     }
    9307        3592 :     appendStringInfoChar(buf, ')');
    9308             : }
    9309             : 
    9310             : /*
    9311             :  * get_agg_expr         - Parse back an Aggref node
    9312             :  */
    9313             : static void
    9314        1252 : get_agg_expr(Aggref *aggref, deparse_context *context,
    9315             :              Aggref *original_aggref)
    9316             : {
    9317        1252 :     StringInfo  buf = context->buf;
    9318             :     Oid         argtypes[FUNC_MAX_ARGS];
    9319             :     int         nargs;
    9320             :     bool        use_variadic;
    9321             : 
    9322             :     /*
    9323             :      * For a combining aggregate, we look up and deparse the corresponding
    9324             :      * partial aggregate instead.  This is necessary because our input
    9325             :      * argument list has been replaced; the new argument list always has just
    9326             :      * one element, which will point to a partial Aggref that supplies us with
    9327             :      * transition states to combine.
    9328             :      */
    9329        1252 :     if (DO_AGGSPLIT_COMBINE(aggref->aggsplit))
    9330             :     {
    9331             :         TargetEntry *tle;
    9332             : 
    9333             :         Assert(list_length(aggref->args) == 1);
    9334         166 :         tle = linitial_node(TargetEntry, aggref->args);
    9335         166 :         resolve_special_varno((Node *) tle->expr, context,
    9336             :                               get_agg_combine_expr, original_aggref);
    9337         166 :         return;
    9338             :     }
    9339             : 
    9340             :     /*
    9341             :      * Mark as PARTIAL, if appropriate.  We look to the original aggref so as
    9342             :      * to avoid printing this when recursing from the code just above.
    9343             :      */
    9344        1086 :     if (DO_AGGSPLIT_SKIPFINAL(original_aggref->aggsplit))
    9345          40 :         appendStringInfoString(buf, "PARTIAL ");
    9346             : 
    9347             :     /* Extract the argument types as seen by the parser */
    9348        1086 :     nargs = get_aggregate_argtypes(aggref, argtypes);
    9349             : 
    9350             :     /* Print the aggregate name, schema-qualified if needed */
    9351        2172 :     appendStringInfo(buf, "%s(%s",
    9352             :                      generate_function_name(aggref->aggfnoid, nargs,
    9353             :                                             NIL, argtypes,
    9354        1086 :                                             aggref->aggvariadic,
    9355             :                                             &use_variadic,
    9356             :                                             context->special_exprkind),
    9357        1086 :                      (aggref->aggdistinct != NIL) ? "DISTINCT " : "");
    9358             : 
    9359        1086 :     if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
    9360             :     {
    9361             :         /*
    9362             :          * Ordered-set aggregates do not use "*" syntax.  Also, we needn't
    9363             :          * worry about inserting VARIADIC.  So we can just dump the direct
    9364             :          * args as-is.
    9365             :          */
    9366             :         Assert(!aggref->aggvariadic);
    9367          22 :         get_rule_expr((Node *) aggref->aggdirectargs, context, true);
    9368             :         Assert(aggref->aggorder != NIL);
    9369          22 :         appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
    9370          22 :         get_rule_orderby(aggref->aggorder, aggref->args, false, context);
    9371             :     }
    9372             :     else
    9373             :     {
    9374             :         /* aggstar can be set only in zero-argument aggregates */
    9375        1064 :         if (aggref->aggstar)
    9376         150 :             appendStringInfoChar(buf, '*');
    9377             :         else
    9378             :         {
    9379             :             ListCell   *l;
    9380             :             int         i;
    9381             : 
    9382         914 :             i = 0;
    9383        1918 :             foreach(l, aggref->args)
    9384             :             {
    9385        1004 :                 TargetEntry *tle = (TargetEntry *) lfirst(l);
    9386        1004 :                 Node       *arg = (Node *) tle->expr;
    9387             : 
    9388             :                 Assert(!IsA(arg, NamedArgExpr));
    9389        1004 :                 if (tle->resjunk)
    9390          22 :                     continue;
    9391         982 :                 if (i++ > 0)
    9392          68 :                     appendStringInfoString(buf, ", ");
    9393         982 :                 if (use_variadic && i == nargs)
    9394           8 :                     appendStringInfoString(buf, "VARIADIC ");
    9395         982 :                 get_rule_expr(arg, context, true);
    9396             :             }
    9397             :         }
    9398             : 
    9399        1064 :         if (aggref->aggorder != NIL)
    9400             :         {
    9401          48 :             appendStringInfoString(buf, " ORDER BY ");
    9402          48 :             get_rule_orderby(aggref->aggorder, aggref->args, false, context);
    9403             :         }
    9404             :     }
    9405             : 
    9406        1086 :     if (aggref->aggfilter != NULL)
    9407             :     {
    9408          20 :         appendStringInfoString(buf, ") FILTER (WHERE ");
    9409          20 :         get_rule_expr((Node *) aggref->aggfilter, context, false);
    9410             :     }
    9411             : 
    9412        1086 :     appendStringInfoChar(buf, ')');
    9413             : }
    9414             : 
    9415             : /*
    9416             :  * This is a helper function for get_agg_expr().  It's used when we deparse
    9417             :  * a combining Aggref; resolve_special_varno locates the corresponding partial
    9418             :  * Aggref and then calls this.
    9419             :  */
    9420             : static void
    9421         166 : get_agg_combine_expr(Node *node, deparse_context *context, void *callback_arg)
    9422             : {
    9423             :     Aggref     *aggref;
    9424         166 :     Aggref     *original_aggref = callback_arg;
    9425             : 
    9426         166 :     if (!IsA(node, Aggref))
    9427           0 :         elog(ERROR, "combining Aggref does not point to an Aggref");
    9428             : 
    9429         166 :     aggref = (Aggref *) node;
    9430         166 :     get_agg_expr(aggref, context, original_aggref);
    9431         166 : }
    9432             : 
    9433             : /*
    9434             :  * get_windowfunc_expr  - Parse back a WindowFunc node
    9435             :  */
    9436             : static void
    9437          56 : get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
    9438             : {
    9439          56 :     StringInfo  buf = context->buf;
    9440             :     Oid         argtypes[FUNC_MAX_ARGS];
    9441             :     int         nargs;
    9442             :     List       *argnames;
    9443             :     ListCell   *l;
    9444             : 
    9445          56 :     if (list_length(wfunc->args) > FUNC_MAX_ARGS)
    9446           0 :         ereport(ERROR,
    9447             :                 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
    9448             :                  errmsg("too many arguments")));
    9449          56 :     nargs = 0;
    9450          56 :     argnames = NIL;
    9451         100 :     foreach(l, wfunc->args)
    9452             :     {
    9453          44 :         Node       *arg = (Node *) lfirst(l);
    9454             : 
    9455          44 :         if (IsA(arg, NamedArgExpr))
    9456           0 :             argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
    9457          44 :         argtypes[nargs] = exprType(arg);
    9458          44 :         nargs++;
    9459             :     }
    9460             : 
    9461          56 :     appendStringInfo(buf, "%s(",
    9462             :                      generate_function_name(wfunc->winfnoid, nargs,
    9463             :                                             argnames, argtypes,
    9464             :                                             false, NULL,
    9465             :                                             context->special_exprkind));
    9466             :     /* winstar can be set only in zero-argument aggregates */
    9467          56 :     if (wfunc->winstar)
    9468           0 :         appendStringInfoChar(buf, '*');
    9469             :     else
    9470          56 :         get_rule_expr((Node *) wfunc->args, context, true);
    9471             : 
    9472          56 :     if (wfunc->aggfilter != NULL)
    9473             :     {
    9474           0 :         appendStringInfoString(buf, ") FILTER (WHERE ");
    9475           0 :         get_rule_expr((Node *) wfunc->aggfilter, context, false);
    9476             :     }
    9477             : 
    9478          56 :     appendStringInfoString(buf, ") OVER ");
    9479             : 
    9480          56 :     foreach(l, context->windowClause)
    9481             :     {
    9482          28 :         WindowClause *wc = (WindowClause *) lfirst(l);
    9483             : 
    9484          28 :         if (wc->winref == wfunc->winref)
    9485             :         {
    9486          28 :             if (wc->name)
    9487           0 :                 appendStringInfoString(buf, quote_identifier(wc->name));
    9488             :             else
    9489          28 :                 get_rule_windowspec(wc, context->windowTList, context);
    9490          28 :             break;
    9491             :         }
    9492             :     }
    9493          56 :     if (l == NULL)
    9494             :     {
    9495          28 :         if (context->windowClause)
    9496           0 :             elog(ERROR, "could not find window clause for winref %u",
    9497             :                  wfunc->winref);
    9498             : 
    9499             :         /*
    9500             :          * In EXPLAIN, we don't have window context information available, so
    9501             :          * we have to settle for this:
    9502             :          */
    9503          28 :         appendStringInfoString(buf, "(?)");
    9504             :     }
    9505          56 : }
    9506             : 
    9507             : /*
    9508             :  * get_func_sql_syntax      - Parse back a SQL-syntax function call
    9509             :  *
    9510             :  * Returns true if we successfully deparsed, false if we did not
    9511             :  * recognize the function.
    9512             :  */
    9513             : static bool
    9514          68 : get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
    9515             : {
    9516          68 :     StringInfo  buf = context->buf;
    9517          68 :     Oid         funcoid = expr->funcid;
    9518             : 
    9519          68 :     switch (funcoid)
    9520             :     {
    9521           4 :         case F_TIMEZONE_INTERVAL_TIMESTAMP:
    9522             :         case F_TIMEZONE_INTERVAL_TIMESTAMPTZ:
    9523             :         case F_TIMEZONE_INTERVAL_TIMETZ:
    9524             :         case F_TIMEZONE_TEXT_TIMESTAMP:
    9525             :         case F_TIMEZONE_TEXT_TIMESTAMPTZ:
    9526             :         case F_TIMEZONE_TEXT_TIMETZ:
    9527             :             /* AT TIME ZONE ... note reversed argument order */
    9528           4 :             appendStringInfoChar(buf, '(');
    9529           4 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9530           4 :             appendStringInfoString(buf, " AT TIME ZONE ");
    9531           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9532           4 :             appendStringInfoChar(buf, ')');
    9533           4 :             return true;
    9534             : 
    9535           4 :         case F_OVERLAPS_TIMESTAMPTZ_INTERVAL_TIMESTAMPTZ_INTERVAL:
    9536             :         case F_OVERLAPS_TIMESTAMPTZ_INTERVAL_TIMESTAMPTZ_TIMESTAMPTZ:
    9537             :         case F_OVERLAPS_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ_INTERVAL:
    9538             :         case F_OVERLAPS_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ:
    9539             :         case F_OVERLAPS_TIMESTAMP_INTERVAL_TIMESTAMP_INTERVAL:
    9540             :         case F_OVERLAPS_TIMESTAMP_INTERVAL_TIMESTAMP_TIMESTAMP:
    9541             :         case F_OVERLAPS_TIMESTAMP_TIMESTAMP_TIMESTAMP_INTERVAL:
    9542             :         case F_OVERLAPS_TIMESTAMP_TIMESTAMP_TIMESTAMP_TIMESTAMP:
    9543             :         case F_OVERLAPS_TIMETZ_TIMETZ_TIMETZ_TIMETZ:
    9544             :         case F_OVERLAPS_TIME_INTERVAL_TIME_INTERVAL:
    9545             :         case F_OVERLAPS_TIME_INTERVAL_TIME_TIME:
    9546             :         case F_OVERLAPS_TIME_TIME_TIME_INTERVAL:
    9547             :         case F_OVERLAPS_TIME_TIME_TIME_TIME:
    9548             :             /* (x1, x2) OVERLAPS (y1, y2) */
    9549           4 :             appendStringInfoString(buf, "((");
    9550           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9551           4 :             appendStringInfoString(buf, ", ");
    9552           4 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9553           4 :             appendStringInfoString(buf, ") OVERLAPS (");
    9554           4 :             get_rule_expr((Node *) lthird(expr->args), context, false);
    9555           4 :             appendStringInfoString(buf, ", ");
    9556           4 :             get_rule_expr((Node *) lfourth(expr->args), context, false);
    9557           4 :             appendStringInfoString(buf, "))");
    9558           4 :             return true;
    9559             : 
    9560           8 :         case F_IS_NORMALIZED:
    9561             :             /* IS xxx NORMALIZED */
    9562           8 :             appendStringInfoString(buf, "((");
    9563           8 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9564           8 :             appendStringInfoString(buf, ") IS");
    9565           8 :             if (list_length(expr->args) == 2)
    9566             :             {
    9567           4 :                 Const      *con = (Const *) lsecond(expr->args);
    9568             : 
    9569             :                 Assert(IsA(con, Const) &&
    9570             :                        con->consttype == TEXTOID &&
    9571             :                        !con->constisnull);
    9572           4 :                 appendStringInfo(buf, " %s",
    9573           4 :                                  TextDatumGetCString(con->constvalue));
    9574             :             }
    9575           8 :             appendStringInfoString(buf, " NORMALIZED)");
    9576           8 :             return true;
    9577             : 
    9578           4 :         case F_PG_COLLATION_FOR:
    9579             :             /* COLLATION FOR */
    9580           4 :             appendStringInfoString(buf, "COLLATION FOR (");
    9581           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9582           4 :             appendStringInfoChar(buf, ')');
    9583           4 :             return true;
    9584             : 
    9585             :             /*
    9586             :              * XXX EXTRACT, a/k/a date_part(), is intentionally not covered
    9587             :              * yet.  Add it after we change the return type to numeric.
    9588             :              */
    9589             : 
    9590           8 :         case F_NORMALIZE:
    9591             :             /* NORMALIZE() */
    9592           8 :             appendStringInfoString(buf, "NORMALIZE(");
    9593           8 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9594           8 :             if (list_length(expr->args) == 2)
    9595             :             {
    9596           4 :                 Const      *con = (Const *) lsecond(expr->args);
    9597             : 
    9598             :                 Assert(IsA(con, Const) &&
    9599             :                        con->consttype == TEXTOID &&
    9600             :                        !con->constisnull);
    9601           4 :                 appendStringInfo(buf, ", %s",
    9602           4 :                                  TextDatumGetCString(con->constvalue));
    9603             :             }
    9604           8 :             appendStringInfoChar(buf, ')');
    9605           8 :             return true;
    9606             : 
    9607           8 :         case F_OVERLAY_BIT_BIT_INT4:
    9608             :         case F_OVERLAY_BIT_BIT_INT4_INT4:
    9609             :         case F_OVERLAY_BYTEA_BYTEA_INT4:
    9610             :         case F_OVERLAY_BYTEA_BYTEA_INT4_INT4:
    9611             :         case F_OVERLAY_TEXT_TEXT_INT4:
    9612             :         case F_OVERLAY_TEXT_TEXT_INT4_INT4:
    9613             :             /* OVERLAY() */
    9614           8 :             appendStringInfoString(buf, "OVERLAY(");
    9615           8 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9616           8 :             appendStringInfoString(buf, " PLACING ");
    9617           8 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9618           8 :             appendStringInfoString(buf, " FROM ");
    9619           8 :             get_rule_expr((Node *) lthird(expr->args), context, false);
    9620           8 :             if (list_length(expr->args) == 4)
    9621             :             {
    9622           4 :                 appendStringInfoString(buf, " FOR ");
    9623           4 :                 get_rule_expr((Node *) lfourth(expr->args), context, false);
    9624             :             }
    9625           8 :             appendStringInfoChar(buf, ')');
    9626           8 :             return true;
    9627             : 
    9628           4 :         case F_POSITION_BIT_BIT:
    9629             :         case F_POSITION_BYTEA_BYTEA:
    9630             :         case F_POSITION_TEXT_TEXT:
    9631             :             /* POSITION() ... extra parens since args are b_expr not a_expr */
    9632           4 :             appendStringInfoString(buf, "POSITION((");
    9633           4 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9634           4 :             appendStringInfoString(buf, ") IN (");
    9635           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9636           4 :             appendStringInfoString(buf, "))");
    9637           4 :             return true;
    9638             : 
    9639           4 :         case F_SUBSTRING_BIT_INT4:
    9640             :         case F_SUBSTRING_BIT_INT4_INT4:
    9641             :         case F_SUBSTRING_BYTEA_INT4:
    9642             :         case F_SUBSTRING_BYTEA_INT4_INT4:
    9643             :         case F_SUBSTRING_TEXT_INT4:
    9644             :         case F_SUBSTRING_TEXT_INT4_INT4:
    9645             :             /* SUBSTRING FROM/FOR (i.e., integer-position variants) */
    9646           4 :             appendStringInfoString(buf, "SUBSTRING(");
    9647           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9648           4 :             appendStringInfoString(buf, " FROM ");
    9649           4 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9650           4 :             if (list_length(expr->args) == 3)
    9651             :             {
    9652           4 :                 appendStringInfoString(buf, " FOR ");
    9653           4 :                 get_rule_expr((Node *) lthird(expr->args), context, false);
    9654             :             }
    9655           4 :             appendStringInfoChar(buf, ')');
    9656           4 :             return true;
    9657             : 
    9658           4 :         case F_SUBSTRING_TEXT_TEXT_TEXT:
    9659             :             /* SUBSTRING SIMILAR/ESCAPE */
    9660           4 :             appendStringInfoString(buf, "SUBSTRING(");
    9661           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9662           4 :             appendStringInfoString(buf, " SIMILAR ");
    9663           4 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9664           4 :             appendStringInfoString(buf, " ESCAPE ");
    9665           4 :             get_rule_expr((Node *) lthird(expr->args), context, false);
    9666           4 :             appendStringInfoChar(buf, ')');
    9667           4 :             return true;
    9668             : 
    9669           4 :         case F_BTRIM_BYTEA_BYTEA:
    9670             :         case F_BTRIM_TEXT:
    9671             :         case F_BTRIM_TEXT_TEXT:
    9672             :             /* TRIM() */
    9673           4 :             appendStringInfoString(buf, "TRIM(BOTH");
    9674           4 :             if (list_length(expr->args) == 2)
    9675             :             {
    9676           4 :                 appendStringInfoChar(buf, ' ');
    9677           4 :                 get_rule_expr((Node *) lsecond(expr->args), context, false);
    9678             :             }
    9679           4 :             appendStringInfoString(buf, " FROM ");
    9680           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9681           4 :             appendStringInfoChar(buf, ')');
    9682           4 :             return true;
    9683             : 
    9684           4 :         case F_LTRIM_TEXT:
    9685             :         case F_LTRIM_TEXT_TEXT:
    9686             :             /* TRIM() */
    9687           4 :             appendStringInfoString(buf, "TRIM(LEADING");
    9688           4 :             if (list_length(expr->args) == 2)
    9689             :             {
    9690           4 :                 appendStringInfoChar(buf, ' ');
    9691           4 :                 get_rule_expr((Node *) lsecond(expr->args), context, false);
    9692             :             }
    9693           4 :             appendStringInfoString(buf, " FROM ");
    9694           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9695           4 :             appendStringInfoChar(buf, ')');
    9696           4 :             return true;
    9697             : 
    9698           4 :         case F_RTRIM_TEXT:
    9699             :         case F_RTRIM_TEXT_TEXT:
    9700             :             /* TRIM() */
    9701           4 :             appendStringInfoString(buf, "TRIM(TRAILING");
    9702           4 :             if (list_length(expr->args) == 2)
    9703             :             {
    9704           0 :                 appendStringInfoChar(buf, ' ');
    9705           0 :                 get_rule_expr((Node *) lsecond(expr->args), context, false);
    9706             :             }
    9707           4 :             appendStringInfoString(buf, " FROM ");
    9708           4 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9709           4 :             appendStringInfoChar(buf, ')');
    9710           4 :             return true;
    9711             : 
    9712           0 :         case F_XMLEXISTS:
    9713             :             /* XMLEXISTS ... extra parens because args are c_expr */
    9714           0 :             appendStringInfoString(buf, "XMLEXISTS((");
    9715           0 :             get_rule_expr((Node *) linitial(expr->args), context, false);
    9716           0 :             appendStringInfoString(buf, ") PASSING (");
    9717           0 :             get_rule_expr((Node *) lsecond(expr->args), context, false);
    9718           0 :             appendStringInfoString(buf, "))");
    9719           0 :             return true;
    9720             :     }
    9721           8 :     return false;
    9722             : }
    9723             : 
    9724             : /* ----------
    9725             :  * get_coercion_expr
    9726             :  *
    9727             :  *  Make a string representation of a value coerced to a specific type
    9728             :  * ----------
    9729             :  */
    9730             : static void
    9731        2120 : get_coercion_expr(Node *arg, deparse_context *context,
    9732             :                   Oid resulttype, int32 resulttypmod,
    9733             :                   Node *parentNode)
    9734             : {
    9735        2120 :     StringInfo  buf = context->buf;
    9736             : 
    9737             :     /*
    9738             :      * Since parse_coerce.c doesn't immediately collapse application of
    9739             :      * length-coercion functions to constants, what we'll typically see in
    9740             :      * such cases is a Const with typmod -1 and a length-coercion function
    9741             :      * right above it.  Avoid generating redundant output. However, beware of
    9742             :      * suppressing casts when the user actually wrote something like
    9743             :      * 'foo'::text::char(3).
    9744             :      *
    9745             :      * Note: it might seem that we are missing the possibility of needing to
    9746             :      * print a COLLATE clause for such a Const.  However, a Const could only
    9747             :      * have nondefault collation in a post-constant-folding tree, in which the
    9748             :      * length coercion would have been folded too.  See also the special
    9749             :      * handling of CollateExpr in coerce_to_target_type(): any collation
    9750             :      * marking will be above the coercion node, not below it.
    9751             :      */
    9752        2120 :     if (arg && IsA(arg, Const) &&
    9753         272 :         ((Const *) arg)->consttype == resulttype &&
    9754          16 :         ((Const *) arg)->consttypmod == -1)
    9755             :     {
    9756             :         /* Show the constant without normal ::typename decoration */
    9757          16 :         get_const_expr((Const *) arg, context, -1);
    9758             :     }
    9759             :     else
    9760             :     {
    9761        2104 :         if (!PRETTY_PAREN(context))
    9762        1944 :             appendStringInfoChar(buf, '(');
    9763        2104 :         get_rule_expr_paren(arg, context, false, parentNode);
    9764        2104 :         if (!PRETTY_PAREN(context))
    9765        1944 :             appendStringInfoChar(buf, ')');
    9766             :     }
    9767             : 
    9768             :     /*
    9769             :      * Never emit resulttype(arg) functional notation. A pg_proc entry could
    9770             :      * take precedence, and a resulttype in pg_temp would require schema
    9771             :      * qualification that format_type_with_typemod() would usually omit. We've
    9772             :      * standardized on arg::resulttype, but CAST(arg AS resulttype) notation
    9773             :      * would work fine.
    9774             :      */
    9775        2120 :     appendStringInfo(buf, "::%s",
    9776             :                      format_type_with_typemod(resulttype, resulttypmod));
    9777        2120 : }
    9778             : 
    9779             : /* ----------
    9780             :  * get_const_expr
    9781             :  *
    9782             :  *  Make a string representation of a Const
    9783             :  *
    9784             :  * showtype can be -1 to never show "::typename" decoration, or +1 to always
    9785             :  * show it, or 0 to show it only if the constant wouldn't be assumed to be
    9786             :  * the right type by default.
    9787             :  *
    9788             :  * If the Const's collation isn't default for its type, show that too.
    9789             :  * We mustn't do this when showtype is -1 (since that means the caller will
    9790             :  * print "::typename", and we can't put a COLLATE clause in between).  It's
    9791             :  * caller's responsibility that collation isn't missed in such cases.
    9792             :  * ----------
    9793             :  */
    9794             : static void
    9795       30668 : get_const_expr(Const *constval, deparse_context *context, int showtype)
    9796             : {
    9797       30668 :     StringInfo  buf = context->buf;
    9798             :     Oid         typoutput;
    9799             :     bool        typIsVarlena;
    9800             :     char       *extval;
    9801       30668 :     bool        needlabel = false;
    9802             : 
    9803       30668 :     if (constval->constisnull)
    9804             :     {
    9805             :         /*
    9806             :          * Always label the type of a NULL constant to prevent misdecisions
    9807             :          * about type when reparsing.
    9808             :          */
    9809         362 :         appendStringInfoString(buf, "NULL");
    9810         362 :         if (showtype >= 0)
    9811             :         {
    9812         334 :             appendStringInfo(buf, "::%s",
    9813             :                              format_type_with_typemod(constval->consttype,
    9814             :                                                       constval->consttypmod));
    9815         334 :             get_const_collation(constval, context);
    9816             :         }
    9817        3914 :         return;
    9818             :     }
    9819             : 
    9820       30306 :     getTypeOutputInfo(constval->consttype,
    9821             :                       &typoutput, &typIsVarlena);
    9822             : 
    9823       30306 :     extval = OidOutputFunctionCall(typoutput, constval->constvalue);
    9824             : 
    9825       30306 :     switch (constval->consttype)
    9826             :     {
    9827       18316 :         case INT4OID:
    9828             : 
    9829             :             /*
    9830             :              * INT4 can be printed without any decoration, unless it is
    9831             :              * negative; in that case print it as '-nnn'::integer to ensure
    9832             :              * that the output will re-parse as a constant, not as a constant
    9833             :              * plus operator.  In most cases we could get away with printing
    9834             :              * (-nnn) instead, because of the way that gram.y handles negative
    9835             :              * literals; but that doesn't work for INT_MIN, and it doesn't
    9836             :              * seem that much prettier anyway.
    9837             :              */
    9838       18316 :             if (extval[0] != '-')
    9839       18134 :                 appendStringInfoString(buf, extval);
    9840             :             else
    9841             :             {
    9842         182 :                 appendStringInfo(buf, "'%s'", extval);
    9843         182 :                 needlabel = true;   /* we must attach a cast */
    9844             :             }
    9845       18316 :             break;
    9846             : 
    9847         464 :         case NUMERICOID:
    9848             : 
    9849             :             /*
    9850             :              * NUMERIC can be printed without quotes if it looks like a float
    9851             :              * constant (not an integer, and not Infinity or NaN) and doesn't
    9852             :              * have a leading sign (for the same reason as for INT4).
    9853             :              */
    9854         464 :             if (isdigit((unsigned char) extval[0]) &&
    9855         464 :                 strcspn(extval, "eE.") != strlen(extval))
    9856             :             {
    9857         170 :                 appendStringInfoString(buf, extval);
    9858             :             }
    9859             :             else
    9860             :             {
    9861         294 :                 appendStringInfo(buf, "'%s'", extval);
    9862         294 :                 needlabel = true;   /* we must attach a cast */
    9863             :             }
    9864         464 :             break;
    9865             : 
    9866         696 :         case BOOLOID:
    9867         696 :             if (strcmp(extval, "t") == 0)
    9868         414 :                 appendStringInfoString(buf, "true");
    9869             :             else
    9870         282 :                 appendStringInfoString(buf, "false");
    9871         696 :             break;
    9872             : 
    9873       10830 :         default:
    9874       10830 :             simple_quote_literal(buf, extval);
    9875       10830 :             break;
    9876             :     }
    9877             : 
    9878       30306 :     pfree(extval);
    9879             : 
    9880       30306 :     if (showtype < 0)
    9881        3552 :         return;
    9882             : 
    9883             :     /*
    9884             :      * For showtype == 0, append ::typename unless the constant will be
    9885             :      * implicitly typed as the right type when it is read in.
    9886             :      *
    9887             :      * XXX this code has to be kept in sync with the behavior of the parser,
    9888             :      * especially make_const.
    9889             :      */
    9890       26754 :     switch (constval->consttype)
    9891             :     {
    9892         700 :         case BOOLOID:
    9893             :         case UNKNOWNOID:
    9894             :             /* These types can be left unlabeled */
    9895         700 :             needlabel = false;
    9896         700 :             break;
    9897       15838 :         case INT4OID:
    9898             :             /* We determined above whether a label is needed */
    9899       15838 :             break;
    9900         464 :         case NUMERICOID:
    9901             : 
    9902             :             /*
    9903             :              * Float-looking constants will be typed as numeric, which we
    9904             :              * checked above; but if there's a nondefault typmod we need to
    9905             :              * show it.
    9906             :              */
    9907         464 :             needlabel |= (constval->consttypmod >= 0);
    9908         464 :             break;
    9909        9752 :         default:
    9910        9752 :             needlabel = true;
    9911        9752 :             break;
    9912             :     }
    9913       26754 :     if (needlabel || showtype > 0)
    9914       10220 :         appendStringInfo(buf, "::%s",
    9915             :                          format_type_with_typemod(constval->consttype,
    9916             :                                                   constval->consttypmod));
    9917             : 
    9918       26754 :     get_const_collation(constval, context);
    9919             : }
    9920             : 
    9921             : /*
    9922             :  * helper for get_const_expr: append COLLATE if needed
    9923             :  */
    9924             : static void
    9925       27088 : get_const_collation(Const *constval, deparse_context *context)
    9926             : {
    9927       27088 :     StringInfo  buf = context->buf;
    9928             : 
    9929       27088 :     if (OidIsValid(constval->constcollid))
    9930             :     {
    9931        4222 :         Oid         typcollation = get_typcollation(constval->consttype);
    9932             : 
    9933        4222 :         if (constval->constcollid != typcollation)
    9934             :         {
    9935          48 :             appendStringInfo(buf, " COLLATE %s",
    9936             :                              generate_collation_name(constval->constcollid));
    9937             :         }
    9938             :     }
    9939       27088 : }
    9940             : 
    9941             : /*
    9942             :  * simple_quote_literal - Format a string as a SQL literal, append to buf
    9943             :  */
    9944             : static void
    9945       11450 : simple_quote_literal(StringInfo buf, const char *val)
    9946             : {
    9947             :     const char *valptr;
    9948             : 
    9949             :     /*
    9950             :      * We form the string literal according to the prevailing setting of
    9951             :      * standard_conforming_strings; we never use E''. User is responsible for
    9952             :      * making sure result is used correctly.
    9953             :      */
    9954       11450 :     appendStringInfoChar(buf, '\'');
    9955      114358 :     for (valptr = val; *valptr; valptr++)
    9956             :     {
    9957      102908 :         char        ch = *valptr;
    9958             : 
    9959      102908 :         if (SQL_STR_DOUBLE(ch, !standard_conforming_strings))
    9960         204 :             appendStringInfoChar(buf, ch);
    9961      102908 :         appendStringInfoChar(buf, ch);
    9962             :     }
    9963       11450 :     appendStringInfoChar(buf, '\'');
    9964       11450 : }
    9965             : 
    9966             : 
    9967             : /* ----------
    9968             :  * get_sublink_expr         - Parse back a sublink
    9969             :  * ----------
    9970             :  */
    9971             : static void
    9972         192 : get_sublink_expr(SubLink *sublink, deparse_context *context)
    9973             : {
    9974         192 :     StringInfo  buf = context->buf;
    9975         192 :     Query      *query = (Query *) (sublink->subselect);
    9976         192 :     char       *opname = NULL;
    9977             :     bool        need_paren;
    9978             : 
    9979         192 :     if (sublink->subLinkType == ARRAY_SUBLINK)
    9980           8 :         appendStringInfoString(buf, "ARRAY(");
    9981             :     else
    9982         184 :         appendStringInfoChar(buf, '(');
    9983             : 
    9984             :     /*
    9985             :      * Note that we print the name of only the first operator, when there are
    9986             :      * multiple combining operators.  This is an approximation that could go
    9987             :      * wrong in various scenarios (operators in different schemas, renamed
    9988             :      * operators, etc) but there is not a whole lot we can do about it, since
    9989             :      * the syntax allows only one operator to be shown.
    9990             :      */
    9991         192 :     if (sublink->testexpr)
    9992             :     {
    9993          12 :         if (IsA(sublink->testexpr, OpExpr))
    9994             :         {
    9995             :             /* single combining operator */
    9996           4 :             OpExpr     *opexpr = (OpExpr *) sublink->testexpr;
    9997             : 
    9998           4 :             get_rule_expr(linitial(opexpr->args), context, true);
    9999           4 :             opname = generate_operator_name(opexpr->opno,
   10000           4 :                                             exprType(linitial(opexpr->args)),
   10001           4 :                                             exprType(lsecond(opexpr->args)));
   10002             :         }
   10003           8 :         else if (IsA(sublink->testexpr, BoolExpr))
   10004             :         {
   10005             :             /* multiple combining operators, = or <> cases */
   10006             :             char       *sep;
   10007             :             ListCell   *l;
   10008             : 
   10009           4 :             appendStringInfoChar(buf, '(');
   10010           4 :             sep = "";
   10011          12 :             foreach(l, ((BoolExpr *) sublink->testexpr)->args)
   10012             :             {
   10013           8 :                 OpExpr     *opexpr = lfirst_node(OpExpr, l);
   10014             : 
   10015           8 :                 appendStringInfoString(buf, sep);
   10016           8 :                 get_rule_expr(linitial(opexpr->args), context, true);
   10017           8 :                 if (!opname)
   10018           4 :                     opname = generate_operator_name(opexpr->opno,
   10019           4 :                                                     exprType(linitial(opexpr->args)),
   10020           4 :                                                     exprType(lsecond(opexpr->args)));
   10021           8 :                 sep = ", ";
   10022             :             }
   10023           4 :             appendStringInfoChar(buf, ')');
   10024             :         }
   10025           4 :         else if (IsA(sublink->testexpr, RowCompareExpr))
   10026             :         {
   10027             :             /* multiple combining operators, < <= > >= cases */
   10028           4 :             RowCompareExpr *rcexpr = (RowCompareExpr *) sublink->testexpr;
   10029             : 
   10030           4 :             appendStringInfoChar(buf, '(');
   10031           4 :             get_rule_expr((Node *) rcexpr->largs, context, true);
   10032           4 :             opname = generate_operator_name(linitial_oid(rcexpr->opnos),
   10033           4 :                                             exprType(linitial(rcexpr->largs)),
   10034           4 :                                             exprType(linitial(rcexpr->rargs)));
   10035           4 :             appendStringInfoChar(buf, ')');
   10036             :         }
   10037             :         else
   10038           0 :             elog(ERROR, "unrecognized testexpr type: %d",
   10039             :                  (int) nodeTag(sublink->testexpr));
   10040             :     }
   10041             : 
   10042         192 :     need_paren = true;
   10043             : 
   10044         192 :     switch (sublink->subLinkType)
   10045             :     {
   10046         116 :         case EXISTS_SUBLINK:
   10047         116 :             appendStringInfoString(buf, "EXISTS ");
   10048         116 :             break;
   10049             : 
   10050           8 :         case ANY_SUBLINK:
   10051           8 :             if (strcmp(opname, "=") == 0) /* Represent = ANY as IN */
   10052           4 :                 appendStringInfoString(buf, " IN ");
   10053             :             else
   10054           4 :                 appendStringInfo(buf, " %s ANY ", opname);
   10055           8 :             break;
   10056             : 
   10057           4 :         case ALL_SUBLINK:
   10058           4 :             appendStringInfo(buf, " %s ALL ", opname);
   10059           4 :             break;
   10060             : 
   10061           0 :         case ROWCOMPARE_SUBLINK:
   10062           0 :             appendStringInfo(buf, " %s ", opname);
   10063           0 :             break;
   10064             : 
   10065          64 :         case EXPR_SUBLINK:
   10066             :         case MULTIEXPR_SUBLINK:
   10067             :         case ARRAY_SUBLINK:
   10068          64 :             need_paren = false;
   10069          64 :             break;
   10070             : 
   10071           0 :         case CTE_SUBLINK:       /* shouldn't occur in a SubLink */
   10072             :         default:
   10073           0 :             elog(ERROR, "unrecognized sublink type: %d",
   10074             :                  (int) sublink->subLinkType);
   10075             :             break;
   10076             :     }
   10077             : 
   10078         192 :     if (need_paren)
   10079         128 :         appendStringInfoChar(buf, '(');
   10080             : 
   10081         192 :     get_query_def(query, buf, context->namespaces, NULL,
   10082             :                   context->prettyFlags, context->wrapColumn,
   10083             :                   context->indentLevel);
   10084             : 
   10085         192 :     if (need_paren)
   10086         128 :         appendStringInfoString(buf, "))");
   10087             :     else
   10088          64 :         appendStringInfoChar(buf, ')');
   10089         192 : }
   10090             : 
   10091             : 
   10092             : /* ----------
   10093             :  * get_tablefunc            - Parse back a table function
   10094             :  * ----------
   10095             :  */
   10096             : static void
   10097          32 : get_tablefunc(TableFunc *tf, deparse_context *context, bool showimplicit)
   10098             : {
   10099          32 :     StringInfo  buf = context->buf;
   10100             : 
   10101             :     /* XMLTABLE is the only existing implementation.  */
   10102             : 
   10103          32 :     appendStringInfoString(buf, "XMLTABLE(");
   10104             : 
   10105          32 :     if (tf->ns_uris != NIL)
   10106             :     {
   10107             :         ListCell   *lc1,
   10108             :                    *lc2;
   10109           6 :         bool        first = true;
   10110             : 
   10111           6 :         appendStringInfoString(buf, "XMLNAMESPACES (");
   10112          12 :         forboth(lc1, tf->ns_uris, lc2, tf->ns_names)
   10113             :         {
   10114           6 :             Node       *expr = (Node *) lfirst(lc1);
   10115           6 :             Value      *ns_node = (Value *) lfirst(lc2);
   10116             : 
   10117           6 :             if (!first)
   10118           0 :                 appendStringInfoString(buf, ", ");
   10119             :             else
   10120           6 :                 first = false;
   10121             : 
   10122           6 :             if (ns_node != NULL)
   10123             :             {
   10124           6 :                 get_rule_expr(expr, context, showimplicit);
   10125           6 :                 appendStringInfo(buf, " AS %s", strVal(ns_node));
   10126             :             }
   10127             :             else
   10128             :             {
   10129           0 :                 appendStringInfoString(buf, "DEFAULT ");
   10130           0 :                 get_rule_expr(expr, context, showimplicit);
   10131             :             }
   10132             :         }
   10133           6 :         appendStringInfoString(buf, "), ");
   10134             :     }
   10135             : 
   10136          32 :     appendStringInfoChar(buf, '(');
   10137          32 :     get_rule_expr((Node *) tf->rowexpr, context, showimplicit);
   10138          32 :     appendStringInfoString(buf, ") PASSING (");
   10139          32 :     get_rule_expr((Node *) tf->docexpr, context, showimplicit);
   10140          32 :     appendStringInfoChar(buf, ')');
   10141             : 
   10142          32 :     if (tf->colexprs != NIL)
   10143             :     {
   10144             :         ListCell   *l1;
   10145             :         ListCell   *l2;
   10146             :         ListCell   *l3;
   10147             :         ListCell   *l4;
   10148             :         ListCell   *l5;
   10149          32 :         int         colnum = 0;
   10150             : 
   10151          32 :         appendStringInfoString(buf, " COLUMNS ");
   10152         222 :         forfive(l1, tf->colnames, l2, tf->coltypes, l3, tf->coltypmods,
   10153             :                 l4, tf->colexprs, l5, tf->coldefexprs)
   10154             :         {
   10155         190 :             char       *colname = strVal(lfirst(l1));
   10156         190 :             Oid         typid = lfirst_oid(l2);
   10157         190 :             int32       typmod = lfirst_int(l3);
   10158         190 :             Node       *colexpr = (Node *) lfirst(l4);
   10159         190 :             Node       *coldefexpr = (Node *) lfirst(l5);
   10160         190 :             bool        ordinality = (tf->ordinalitycol == colnum);
   10161         190 :             bool        notnull = bms_is_member(colnum, tf->notnulls);
   10162             : 
   10163         190 :             if (colnum > 0)
   10164         158 :                 appendStringInfoString(buf, ", ");
   10165         190 :             colnum++;
   10166             : 
   10167         358 :             appendStringInfo(buf, "%s %s", quote_identifier(colname),
   10168             :                              ordinality ? "FOR ORDINALITY" :
   10169         168 :                              format_type_with_typemod(typid, typmod));
   10170         190 :             if (ordinality)
   10171          22 :                 continue;
   10172             : 
   10173         168 :             if (coldefexpr != NULL)
   10174             :             {
   10175          22 :                 appendStringInfoString(buf, " DEFAULT (");
   10176          22 :                 get_rule_expr((Node *) coldefexpr, context, showimplicit);
   10177          22 :                 appendStringInfoChar(buf, ')');
   10178             :             }
   10179         168 :             if (colexpr != NULL)
   10180             :             {
   10181         160 :                 appendStringInfoString(buf, " PATH (");
   10182         160 :                 get_rule_expr((Node *) colexpr, context, showimplicit);
   10183         160 :                 appendStringInfoChar(buf, ')');
   10184             :             }
   10185         168 :             if (notnull)
   10186          22 :                 appendStringInfoString(buf, " NOT NULL");
   10187             :         }
   10188             :     }
   10189             : 
   10190          32 :     appendStringInfoChar(buf, ')');
   10191          32 : }
   10192             : 
   10193             : /* ----------
   10194             :  * get_from_clause          - Parse back a FROM clause
   10195             :  *
   10196             :  * "prefix" is the keyword that denotes the start of the list of FROM
   10197             :  * elements. It is FROM when used to parse back SELECT and UPDATE, but
   10198             :  * is USING when parsing back DELETE.
   10199             :  * ----------
   10200             :  */
   10201             : static void
   10202        2206 : get_from_clause(Query *query, const char *prefix, deparse_context *context)
   10203             : {
   10204        2206 :     StringInfo  buf = context->buf;
   10205        2206 :     bool        first = true;
   10206             :     ListCell   *l;
   10207             : 
   10208             :     /*
   10209             :      * We use the query's jointree as a guide to what to print.  However, we
   10210             :      * must ignore auto-added RTEs that are marked not inFromCl. (These can
   10211             :      * only appear at the top level of the jointree, so it's sufficient to
   10212             :      * check here.)  This check also ensures we ignore the rule pseudo-RTEs
   10213             :      * for NEW and OLD.
   10214             :      */
   10215        4476 :     foreach(l, query->jointree->fromlist)
   10216             :     {
   10217        2270 :         Node       *jtnode = (Node *) lfirst(l);
   10218             : 
   10219        2270 :         if (IsA(jtnode, RangeTblRef))
   10220             :         {
   10221        1850 :             int         varno = ((RangeTblRef *) jtnode)->rtindex;
   10222        1850 :             RangeTblEntry *rte = rt_fetch(varno, query->rtable);
   10223             : 
   10224        1850 :             if (!rte->inFromCl)
   10225         256 :                 continue;
   10226             :         }
   10227             : 
   10228        2014 :         if (first)
   10229             :         {
   10230        1838 :             appendContextKeyword(context, prefix,
   10231             :                                  -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
   10232        1838 :             first = false;
   10233             : 
   10234        1838 :             get_from_clause_item(jtnode, query, context);
   10235             :         }
   10236             :         else
   10237             :         {
   10238             :             StringInfoData itembuf;
   10239             : 
   10240         176 :             appendStringInfoString(buf, ", ");
   10241             : 
   10242             :             /*
   10243             :              * Put the new FROM item's text into itembuf so we can decide
   10244             :              * after we've got it whether or not it needs to go on a new line.
   10245             :              */
   10246         176 :             initStringInfo(&itembuf);
   10247         176 :             context->buf = &itembuf;
   10248             : 
   10249         176 :             get_from_clause_item(jtnode, query, context);
   10250             : 
   10251             :             /* Restore context's output buffer */
   10252         176 :             context->buf = buf;
   10253             : 
   10254             :             /* Consider line-wrapping if enabled */
   10255         176 :             if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
   10256             :             {
   10257             :                 /* Does the new item start with a new line? */
   10258         176 :                 if (itembuf.len > 0 && itembuf.data[0] == '\n')
   10259             :                 {
   10260             :                     /* If so, we shouldn't add anything */
   10261             :                     /* instead, remove any trailing spaces currently in buf */
   10262           0 :                     removeStringInfoSpaces(buf);
   10263             :                 }
   10264             :                 else
   10265             :                 {
   10266             :                     char       *trailing_nl;
   10267             : 
   10268             :                     /* Locate the start of the current line in the buffer */
   10269         176 :                     trailing_nl = strrchr(buf->data, '\n');
   10270         176 :                     if (trailing_nl == NULL)
   10271           0 :                         trailing_nl = buf->data;
   10272             :                     else
   10273         176 :                         trailing_nl++;
   10274             : 
   10275             :                     /*
   10276             :                      * Add a newline, plus some indentation, if the new item
   10277             :                      * would cause an overflow.
   10278             :                      */
   10279         176 :                     if (strlen(trailing_nl) + itembuf.len > context->wrapColumn)
   10280         176 :                         appendContextKeyword(context, "", -PRETTYINDENT_STD,
   10281             :                                              PRETTYINDENT_STD,
   10282             :                                              PRETTYINDENT_VAR);
   10283             :                 }
   10284             :             }
   10285             : 
   10286             :             /* Add the new item */
   10287         176 :             appendBinaryStringInfo(buf, itembuf.data, itembuf.len);
   10288             : 
   10289             :             /* clean up */
   10290         176 :             pfree(itembuf.data);
   10291             :         }
   10292             :     }
   10293        2206 : }
   10294             : 
   10295             : static void
   10296        3262 : get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
   10297             : {
   10298        3262 :     StringInfo  buf = context->buf;
   10299        3262 :     deparse_namespace *dpns = (deparse_namespace *) linitial(context->namespaces);
   10300             : 
   10301        3262 :     if (IsA(jtnode, RangeTblRef))
   10302             :     {
   10303        2638 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
   10304        2638 :         RangeTblEntry *rte = rt_fetch(varno, query->rtable);
   10305        2638 :         char       *refname = get_rtable_name(varno, context);
   10306        2638 :         deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
   10307        2638 :         RangeTblFunction *rtfunc1 = NULL;
   10308             :         bool        printalias;
   10309             : 
   10310        2638 :         if (rte->lateral)
   10311          22 :             appendStringInfoString(buf, "LATERAL ");
   10312             : 
   10313             :         /* Print the FROM item proper */
   10314        2638 :         switch (rte->rtekind)
   10315             :         {
   10316        2196 :             case RTE_RELATION:
   10317             :                 /* Normal relation RTE */
   10318        4392 :                 appendStringInfo(buf, "%s%s",
   10319        2196 :                                  only_marker(rte),
   10320             :                                  generate_relation_name(rte->relid,
   10321             :                                                         context->namespaces));
   10322        2196 :                 break;
   10323         118 :             case RTE_SUBQUERY:
   10324             :                 /* Subquery RTE */
   10325         118 :                 appendStringInfoChar(buf, '(');
   10326         118 :                 get_query_def(rte->subquery, buf, context->namespaces, NULL,
   10327             :                               context->prettyFlags, context->wrapColumn,
   10328             :                               context->indentLevel);
   10329         118 :                 appendStringInfoChar(buf, ')');
   10330         118 :                 break;
   10331         244 :             case RTE_FUNCTION:
   10332             :                 /* Function RTE */
   10333         244 :                 rtfunc1 = (RangeTblFunction *) linitial(rte->functions);
   10334             : 
   10335             :                 /*
   10336             :                  * Omit ROWS FROM() syntax for just one function, unless it
   10337             :                  * has both a coldeflist and WITH ORDINALITY. If it has both,
   10338             :                  * we must use ROWS FROM() syntax to avoid ambiguity about
   10339             :                  * whether the coldeflist includes the ordinality column.
   10340             :                  */
   10341         244 :                 if (list_length(rte->functions) == 1 &&
   10342         224 :                     (rtfunc1->funccolnames == NIL || !rte->funcordinality))
   10343             :                 {
   10344         224 :                     get_rule_expr_funccall(rtfunc1->funcexpr, context, true);
   10345             :                     /* we'll print the coldeflist below, if it has one */
   10346             :                 }
   10347             :                 else
   10348             :                 {
   10349             :                     bool        all_unnest;
   10350             :                     ListCell   *lc;
   10351             : 
   10352             :                     /*
   10353             :                      * If all the function calls in the list are to unnest,
   10354             :                      * and none need a coldeflist, then collapse the list back
   10355             :                      * down to UNNEST(args).  (If we had more than one
   10356             :                      * built-in unnest function, this would get more
   10357             :                      * difficult.)
   10358             :                      *
   10359             :                      * XXX This is pretty ugly, since it makes not-terribly-
   10360             :                      * future-proof assumptions about what the parser would do
   10361             :                      * with the output; but the alternative is to emit our
   10362             :                      * nonstandard ROWS FROM() notation for what might have
   10363             :                      * been a perfectly spec-compliant multi-argument
   10364             :                      * UNNEST().
   10365             :                      */
   10366          20 :                     all_unnest = true;
   10367          52 :                     foreach(lc, rte->functions)
   10368             :                     {
   10369          44 :                         RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
   10370             : 
   10371          44 :                         if (!IsA(rtfunc->funcexpr, FuncExpr) ||
   10372          44 :                             ((FuncExpr *) rtfunc->funcexpr)->funcid != F_UNNEST_ANYARRAY ||
   10373          32 :                             rtfunc->funccolnames != NIL)
   10374             :                         {
   10375          12 :                             all_unnest = false;
   10376          12 :                             break;
   10377             :                         }
   10378             :                     }
   10379             : 
   10380          20 :                     if (all_unnest)
   10381             :                     {
   10382           8 :                         List       *allargs = NIL;
   10383             : 
   10384          32 :                         foreach(lc, rte->functions)
   10385             :                         {
   10386          24 :                             RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
   10387          24 :                             List       *args = ((FuncExpr *) rtfunc->funcexpr)->args;
   10388             : 
   10389          24 :                             allargs = list_concat(allargs, args);
   10390             :                         }
   10391             : 
   10392           8 :                         appendStringInfoString(buf, "UNNEST(");
   10393           8 :                         get_rule_expr((Node *) allargs, context, true);
   10394           8 :                         appendStringInfoChar(buf, ')');
   10395             :                     }
   10396             :                     else
   10397             :                     {
   10398          12 :                         int         funcno = 0;
   10399             : 
   10400          12 :                         appendStringInfoString(buf, "ROWS FROM(");
   10401          44 :                         foreach(lc, rte->functions)
   10402             :                         {
   10403          32 :                             RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
   10404             : 
   10405          32 :                             if (funcno > 0)
   10406          20 :                                 appendStringInfoString(buf, ", ");
   10407          32 :                             get_rule_expr_funccall(rtfunc->funcexpr, context, true);
   10408          32 :                             if (rtfunc->funccolnames != NIL)
   10409             :                             {
   10410             :                                 /* Reconstruct the column definition list */
   10411           4 :                                 appendStringInfoString(buf, " AS ");
   10412           4 :                                 get_from_clause_coldeflist(rtfunc,
   10413             :                                                            NULL,
   10414             :                                                            context);
   10415             :                             }
   10416          32 :                             funcno++;
   10417             :                         }
   10418          12 :                         appendStringInfoChar(buf, ')');
   10419             :                     }
   10420             :                     /* prevent printing duplicate coldeflist below */
   10421          20 :                     rtfunc1 = NULL;
   10422             :                 }
   10423         244 :                 if (rte->funcordinality)
   10424          12 :                     appendStringInfoString(buf, " WITH ORDINALITY");
   10425         244 :                 break;
   10426          16 :             case RTE_TABLEFUNC:
   10427          16 :                 get_tablefunc(rte->tablefunc, context, true);
   10428          16 :                 break;
   10429           8 :             case RTE_VALUES:
   10430             :                 /* Values list RTE */
   10431           8 :