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