PostgreSQL Source Code git master
ruleutils.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include "access/amapi.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/table.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_am.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_depend.h"
#include "catalog/pg_language.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_partitioned_table.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "commands/tablespace.h"
#include "common/keywords.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pathnodes.h"
#include "optimizer/optimizer.h"
#include "parser/parse_agg.h"
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteHandler.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rewriteSupport.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "utils/partcache.h"
#include "utils/rel.h"
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "utils/varlena.h"
#include "utils/xml.h"
Include dependency graph for ruleutils.c:

Go to the source code of this file.

Data Structures

struct  deparse_context
 
struct  deparse_namespace
 
struct  deparse_columns
 
struct  NameHashEntry
 

Macros

#define PRETTYINDENT_STD   8
 
#define PRETTYINDENT_JOIN   4
 
#define PRETTYINDENT_VAR   4
 
#define PRETTYINDENT_LIMIT   40 /* wrap limit */
 
#define PRETTYFLAG_PAREN   0x0001
 
#define PRETTYFLAG_INDENT   0x0002
 
#define PRETTYFLAG_SCHEMA   0x0004
 
#define GET_PRETTY_FLAGS(pretty)
 
#define WRAP_COLUMN_DEFAULT   0
 
#define PRETTY_PAREN(context)   ((context)->prettyFlags & PRETTYFLAG_PAREN)
 
#define PRETTY_INDENT(context)   ((context)->prettyFlags & PRETTYFLAG_INDENT)
 
#define PRETTY_SCHEMA(context)   ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
 
#define deparse_columns_fetch(rangetable_index, dpns)    ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
 
#define only_marker(rte)   ((rte)->inh ? "" : "ONLY ")
 

Typedefs

typedef void(* rsv_callback) (Node *node, deparse_context *context, void *callback_arg)
 

Functions

static char * deparse_expression_pretty (Node *expr, List *dpcontext, bool forceprefix, bool showimplicit, int prettyFlags, int startIndent)
 
static char * pg_get_viewdef_worker (Oid viewoid, int prettyFlags, int wrapColumn)
 
static char * pg_get_triggerdef_worker (Oid trigid, bool pretty)
 
static int decompile_column_index_array (Datum column_index_array, Oid relId, bool withPeriod, StringInfo buf)
 
static char * pg_get_ruledef_worker (Oid ruleoid, int prettyFlags)
 
static char * pg_get_indexdef_worker (Oid indexrelid, int colno, const Oid *excludeOps, bool attrsOnly, bool keysOnly, bool showTblSpc, bool inherits, int prettyFlags, bool missing_ok)
 
static char * pg_get_statisticsobj_worker (Oid statextid, bool columns_only, bool missing_ok)
 
static char * pg_get_partkeydef_worker (Oid relid, int prettyFlags, bool attrsOnly, bool missing_ok)
 
static char * pg_get_constraintdef_worker (Oid constraintId, bool fullCommand, int prettyFlags, bool missing_ok)
 
static textpg_get_expr_worker (text *expr, Oid relid, int prettyFlags)
 
static int print_function_arguments (StringInfo buf, HeapTuple proctup, bool print_table_args, bool print_defaults)
 
static void print_function_rettype (StringInfo buf, HeapTuple proctup)
 
static void print_function_trftypes (StringInfo buf, HeapTuple proctup)
 
static void print_function_sqlbody (StringInfo buf, HeapTuple proctup)
 
static void set_rtable_names (deparse_namespace *dpns, List *parent_namespaces, Bitmapset *rels_used)
 
static void set_deparse_for_query (deparse_namespace *dpns, Query *query, List *parent_namespaces)
 
static void set_simple_column_names (deparse_namespace *dpns)
 
static bool has_dangerous_join_using (deparse_namespace *dpns, Node *jtnode)
 
static void set_using_names (deparse_namespace *dpns, Node *jtnode, List *parentUsing)
 
static void set_relation_column_names (deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo)
 
static void set_join_column_names (deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo)
 
static bool colname_is_unique (const char *colname, deparse_namespace *dpns, deparse_columns *colinfo)
 
static char * make_colname_unique (char *colname, deparse_namespace *dpns, deparse_columns *colinfo)
 
static void expand_colnames_array_to (deparse_columns *colinfo, int n)
 
static void build_colinfo_names_hash (deparse_columns *colinfo)
 
static void add_to_names_hash (deparse_columns *colinfo, const char *name)
 
static void destroy_colinfo_names_hash (deparse_columns *colinfo)
 
static void identify_join_columns (JoinExpr *j, RangeTblEntry *jrte, deparse_columns *colinfo)
 
static char * get_rtable_name (int rtindex, deparse_context *context)
 
static void set_deparse_plan (deparse_namespace *dpns, Plan *plan)
 
static Planfind_recursive_union (deparse_namespace *dpns, WorkTableScan *wtscan)
 
static void push_child_plan (deparse_namespace *dpns, Plan *plan, deparse_namespace *save_dpns)
 
static void pop_child_plan (deparse_namespace *dpns, deparse_namespace *save_dpns)
 
static void push_ancestor_plan (deparse_namespace *dpns, ListCell *ancestor_cell, deparse_namespace *save_dpns)
 
static void pop_ancestor_plan (deparse_namespace *dpns, deparse_namespace *save_dpns)
 
static void make_ruledef (StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags)
 
static void make_viewdef (StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags, int wrapColumn)
 
static void get_query_def (Query *query, StringInfo buf, List *parentnamespace, TupleDesc resultDesc, bool colNamesVisible, int prettyFlags, int wrapColumn, int startIndent)
 
static void get_values_def (List *values_lists, deparse_context *context)
 
static void get_with_clause (Query *query, deparse_context *context)
 
static void get_select_query_def (Query *query, deparse_context *context)
 
static void get_insert_query_def (Query *query, deparse_context *context)
 
static void get_update_query_def (Query *query, deparse_context *context)
 
static void get_update_query_targetlist_def (Query *query, List *targetList, deparse_context *context, RangeTblEntry *rte)
 
static void get_delete_query_def (Query *query, deparse_context *context)
 
static void get_merge_query_def (Query *query, deparse_context *context)
 
static void get_utility_query_def (Query *query, deparse_context *context)
 
static void get_basic_select_query (Query *query, deparse_context *context)
 
static void get_target_list (List *targetList, deparse_context *context)
 
static void get_returning_clause (Query *query, deparse_context *context)
 
static void get_setop_query (Node *setOp, Query *query, deparse_context *context)
 
static Nodeget_rule_sortgroupclause (Index ref, List *tlist, bool force_colno, deparse_context *context)
 
static void get_rule_groupingset (GroupingSet *gset, List *targetlist, bool omit_parens, deparse_context *context)
 
static void get_rule_orderby (List *orderList, List *targetList, bool force_colno, deparse_context *context)
 
static void get_rule_windowclause (Query *query, deparse_context *context)
 
static void get_rule_windowspec (WindowClause *wc, List *targetList, deparse_context *context)
 
static void get_window_frame_options (int frameOptions, Node *startOffset, Node *endOffset, deparse_context *context)
 
static char * get_variable (Var *var, int levelsup, bool istoplevel, deparse_context *context)
 
static void get_special_variable (Node *node, deparse_context *context, void *callback_arg)
 
static void resolve_special_varno (Node *node, deparse_context *context, rsv_callback callback, void *callback_arg)
 
static Nodefind_param_referent (Param *param, deparse_context *context, deparse_namespace **dpns_p, ListCell **ancestor_cell_p)
 
static SubPlanfind_param_generator (Param *param, deparse_context *context, int *column_p)
 
static SubPlanfind_param_generator_initplan (Param *param, Plan *plan, int *column_p)
 
static void get_parameter (Param *param, deparse_context *context)
 
static const char * get_simple_binary_op_name (OpExpr *expr)
 
static bool isSimpleNode (Node *node, Node *parentNode, int prettyFlags)
 
static void appendContextKeyword (deparse_context *context, const char *str, int indentBefore, int indentAfter, int indentPlus)
 
static void removeStringInfoSpaces (StringInfo str)
 
static void get_rule_expr (Node *node, deparse_context *context, bool showimplicit)
 
static void get_rule_expr_toplevel (Node *node, deparse_context *context, bool showimplicit)
 
static void get_rule_list_toplevel (List *lst, deparse_context *context, bool showimplicit)
 
static void get_rule_expr_funccall (Node *node, deparse_context *context, bool showimplicit)
 
static bool looks_like_function (Node *node)
 
static void get_oper_expr (OpExpr *expr, deparse_context *context)
 
static void get_func_expr (FuncExpr *expr, deparse_context *context, bool showimplicit)
 
static void get_agg_expr (Aggref *aggref, deparse_context *context, Aggref *original_aggref)
 
static void get_agg_expr_helper (Aggref *aggref, deparse_context *context, Aggref *original_aggref, const char *funcname, const char *options, bool is_json_objectagg)
 
static void get_agg_combine_expr (Node *node, deparse_context *context, void *callback_arg)
 
static void get_windowfunc_expr (WindowFunc *wfunc, deparse_context *context)
 
static void get_windowfunc_expr_helper (WindowFunc *wfunc, deparse_context *context, const char *funcname, const char *options, bool is_json_objectagg)
 
static bool get_func_sql_syntax (FuncExpr *expr, deparse_context *context)
 
static void get_coercion_expr (Node *arg, deparse_context *context, Oid resulttype, int32 resulttypmod, Node *parentNode)
 
static void get_const_expr (Const *constval, deparse_context *context, int showtype)
 
static void get_const_collation (Const *constval, deparse_context *context)
 
static void get_json_format (JsonFormat *format, StringInfo buf)
 
static void get_json_returning (JsonReturning *returning, StringInfo buf, bool json_format_by_default)
 
static void get_json_constructor (JsonConstructorExpr *ctor, deparse_context *context, bool showimplicit)
 
static void get_json_constructor_options (JsonConstructorExpr *ctor, StringInfo buf)
 
static void get_json_agg_constructor (JsonConstructorExpr *ctor, deparse_context *context, const char *funcname, bool is_json_objectagg)
 
static void simple_quote_literal (StringInfo buf, const char *val)
 
static void get_sublink_expr (SubLink *sublink, deparse_context *context)
 
static void get_tablefunc (TableFunc *tf, deparse_context *context, bool showimplicit)
 
static void get_from_clause (Query *query, const char *prefix, deparse_context *context)
 
static void get_from_clause_item (Node *jtnode, Query *query, deparse_context *context)
 
static void get_rte_alias (RangeTblEntry *rte, int varno, bool use_as, deparse_context *context)
 
static void get_column_alias_list (deparse_columns *colinfo, deparse_context *context)
 
static void get_from_clause_coldeflist (RangeTblFunction *rtfunc, deparse_columns *colinfo, deparse_context *context)
 
static void get_tablesample_def (TableSampleClause *tablesample, deparse_context *context)
 
static void get_opclass_name (Oid opclass, Oid actual_datatype, StringInfo buf)
 
static NodeprocessIndirection (Node *node, deparse_context *context)
 
static void printSubscripts (SubscriptingRef *sbsref, deparse_context *context)
 
static char * get_relation_name (Oid relid)
 
static char * generate_relation_name (Oid relid, List *namespaces)
 
static char * generate_qualified_relation_name (Oid relid)
 
static char * generate_function_name (Oid funcid, int nargs, List *argnames, Oid *argtypes, bool has_variadic, bool *use_variadic_p, bool inGroupBy)
 
static char * generate_operator_name (Oid operid, Oid arg1, Oid arg2)
 
static void add_cast_to (StringInfo buf, Oid typid)
 
static char * generate_qualified_type_name (Oid typid)
 
static textstring_to_text (char *str)
 
static char * flatten_reloptions (Oid relid)
 
static void get_reloptions (StringInfo buf, Datum reloptions)
 
static void get_json_path_spec (Node *path_spec, deparse_context *context, bool showimplicit)
 
static void get_json_table_columns (TableFunc *tf, JsonTablePathScan *scan, deparse_context *context, bool showimplicit)
 
static void get_json_table_nested_columns (TableFunc *tf, JsonTablePlan *plan, deparse_context *context, bool showimplicit, bool needcomma)
 
Datum pg_get_ruledef (PG_FUNCTION_ARGS)
 
Datum pg_get_ruledef_ext (PG_FUNCTION_ARGS)
 
Datum pg_get_viewdef (PG_FUNCTION_ARGS)
 
Datum pg_get_viewdef_ext (PG_FUNCTION_ARGS)
 
Datum pg_get_viewdef_wrap (PG_FUNCTION_ARGS)
 
Datum pg_get_viewdef_name (PG_FUNCTION_ARGS)
 
Datum pg_get_viewdef_name_ext (PG_FUNCTION_ARGS)
 
Datum pg_get_triggerdef (PG_FUNCTION_ARGS)
 
Datum pg_get_triggerdef_ext (PG_FUNCTION_ARGS)
 
Datum pg_get_indexdef (PG_FUNCTION_ARGS)
 
Datum pg_get_indexdef_ext (PG_FUNCTION_ARGS)
 
char * pg_get_indexdef_string (Oid indexrelid)
 
char * pg_get_indexdef_columns (Oid indexrelid, bool pretty)
 
char * pg_get_indexdef_columns_extended (Oid indexrelid, bits16 flags)
 
char * pg_get_querydef (Query *query, bool pretty)
 
Datum pg_get_statisticsobjdef (PG_FUNCTION_ARGS)
 
char * pg_get_statisticsobjdef_string (Oid statextid)
 
Datum pg_get_statisticsobjdef_columns (PG_FUNCTION_ARGS)
 
Datum pg_get_statisticsobjdef_expressions (PG_FUNCTION_ARGS)
 
Datum pg_get_partkeydef (PG_FUNCTION_ARGS)
 
char * pg_get_partkeydef_columns (Oid relid, bool pretty)
 
Datum pg_get_partition_constraintdef (PG_FUNCTION_ARGS)
 
char * pg_get_partconstrdef_string (Oid partitionId, char *aliasname)
 
Datum pg_get_constraintdef (PG_FUNCTION_ARGS)
 
Datum pg_get_constraintdef_ext (PG_FUNCTION_ARGS)
 
char * pg_get_constraintdef_command (Oid constraintId)
 
Datum pg_get_expr (PG_FUNCTION_ARGS)
 
Datum pg_get_expr_ext (PG_FUNCTION_ARGS)
 
Datum pg_get_userbyid (PG_FUNCTION_ARGS)
 
Datum pg_get_serial_sequence (PG_FUNCTION_ARGS)
 
Datum pg_get_functiondef (PG_FUNCTION_ARGS)
 
Datum pg_get_function_arguments (PG_FUNCTION_ARGS)
 
Datum pg_get_function_identity_arguments (PG_FUNCTION_ARGS)
 
Datum pg_get_function_result (PG_FUNCTION_ARGS)
 
static bool is_input_argument (int nth, const char *argmodes)
 
Datum pg_get_function_arg_default (PG_FUNCTION_ARGS)
 
Datum pg_get_function_sqlbody (PG_FUNCTION_ARGS)
 
char * deparse_expression (Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
 
Listdeparse_context_for (const char *aliasname, Oid relid)
 
Listdeparse_context_for_plan_tree (PlannedStmt *pstmt, List *rtable_names)
 
Listset_deparse_context_plan (List *dpcontext, Plan *plan, List *ancestors)
 
Listselect_rtable_names_for_explain (List *rtable, Bitmapset *rels_used)
 
static RangeTblEntryget_simple_values_rte (Query *query, TupleDesc resultDesc)
 
char * get_window_frame_options_for_explain (int frameOptions, Node *startOffset, Node *endOffset, List *dpcontext, bool forceprefix)
 
static const char * get_name_for_var_field (Var *var, int fieldno, int levelsup, deparse_context *context)
 
static void get_rule_expr_paren (Node *node, deparse_context *context, bool showimplicit, Node *parentNode)
 
static void get_json_behavior (JsonBehavior *behavior, deparse_context *context, const char *on)
 
static void get_json_expr_options (JsonExpr *jsexpr, deparse_context *context, JsonBehaviorType default_behavior)
 
static void get_xmltable (TableFunc *tf, deparse_context *context, bool showimplicit)
 
static void get_json_table (TableFunc *tf, deparse_context *context, bool showimplicit)
 
char * generate_opclass_name (Oid opclass)
 
const char * quote_identifier (const char *ident)
 
char * quote_qualified_identifier (const char *qualifier, const char *ident)
 
void generate_operator_clause (StringInfo buf, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
 
char * generate_collation_name (Oid collid)
 
char * get_range_partbound_string (List *bound_datums)
 

Variables

static SPIPlanPtr plan_getrulebyoid = NULL
 
static const char *const query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1"
 
static SPIPlanPtr plan_getviewrule = NULL
 
static const char *const query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2"
 
bool quote_all_identifiers = false
 

Macro Definition Documentation

◆ deparse_columns_fetch

#define deparse_columns_fetch (   rangetable_index,
  dpns 
)     ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))

Definition at line 312 of file ruleutils.c.

◆ GET_PRETTY_FLAGS

#define GET_PRETTY_FLAGS (   pretty)
Value:
#define PRETTYFLAG_INDENT
Definition: ruleutils.c:89
#define PRETTYFLAG_PAREN
Definition: ruleutils.c:88
#define PRETTYFLAG_SCHEMA
Definition: ruleutils.c:90

Definition at line 93 of file ruleutils.c.

◆ only_marker

#define only_marker (   rte)    ((rte)->inh ? "" : "ONLY ")

Definition at line 550 of file ruleutils.c.

◆ PRETTY_INDENT

#define PRETTY_INDENT (   context)    ((context)->prettyFlags & PRETTYFLAG_INDENT)

Definition at line 102 of file ruleutils.c.

◆ PRETTY_PAREN

#define PRETTY_PAREN (   context)    ((context)->prettyFlags & PRETTYFLAG_PAREN)

Definition at line 101 of file ruleutils.c.

◆ PRETTY_SCHEMA

#define PRETTY_SCHEMA (   context)    ((context)->prettyFlags & PRETTYFLAG_SCHEMA)

Definition at line 103 of file ruleutils.c.

◆ PRETTYFLAG_INDENT

#define PRETTYFLAG_INDENT   0x0002

Definition at line 89 of file ruleutils.c.

◆ PRETTYFLAG_PAREN

#define PRETTYFLAG_PAREN   0x0001

Definition at line 88 of file ruleutils.c.

◆ PRETTYFLAG_SCHEMA

#define PRETTYFLAG_SCHEMA   0x0004

Definition at line 90 of file ruleutils.c.

◆ PRETTYINDENT_JOIN

#define PRETTYINDENT_JOIN   4

Definition at line 82 of file ruleutils.c.

◆ PRETTYINDENT_LIMIT

#define PRETTYINDENT_LIMIT   40 /* wrap limit */

Definition at line 85 of file ruleutils.c.

◆ PRETTYINDENT_STD

#define PRETTYINDENT_STD   8

Definition at line 81 of file ruleutils.c.

◆ PRETTYINDENT_VAR

#define PRETTYINDENT_VAR   4

Definition at line 83 of file ruleutils.c.

◆ WRAP_COLUMN_DEFAULT

#define WRAP_COLUMN_DEFAULT   0

Definition at line 98 of file ruleutils.c.

Typedef Documentation

◆ rsv_callback

typedef void(* rsv_callback) (Node *node, deparse_context *context, void *callback_arg)

Definition at line 325 of file ruleutils.c.

Function Documentation

◆ add_cast_to()

static void add_cast_to ( StringInfo  buf,
Oid  typid 
)
static

Definition at line 13469 of file ruleutils.c.

13470{
13471 HeapTuple typetup;
13472 Form_pg_type typform;
13473 char *typname;
13474 char *nspname;
13475
13476 typetup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
13477 if (!HeapTupleIsValid(typetup))
13478 elog(ERROR, "cache lookup failed for type %u", typid);
13479 typform = (Form_pg_type) GETSTRUCT(typetup);
13480
13481 typname = NameStr(typform->typname);
13482 nspname = get_namespace_name_or_temp(typform->typnamespace);
13483
13484 appendStringInfo(buf, "::%s.%s",
13486
13487 ReleaseSysCache(typetup);
13488}
#define NameStr(name)
Definition: c.h:717
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3473
static char * buf
Definition: pg_test_fsync.c:72
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
NameData typname
Definition: pg_type.h:41
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:13019
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

References appendStringInfo(), buf, elog, ERROR, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), quote_identifier(), ReleaseSysCache(), SearchSysCache1(), and typname.

Referenced by generate_operator_clause().

◆ add_to_names_hash()

static void add_to_names_hash ( deparse_columns colinfo,
const char *  name 
)
static

Definition at line 5036 of file ruleutils.c.

5037{
5038 if (colinfo->names_hash)
5039 (void) hash_search(colinfo->names_hash,
5040 name,
5041 HASH_ENTER,
5042 NULL);
5043}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
@ HASH_ENTER
Definition: hsearch.h:114
HTAB * names_hash
Definition: ruleutils.c:308
const char * name

References HASH_ENTER, hash_search(), name, and deparse_columns::names_hash.

Referenced by build_colinfo_names_hash(), set_join_column_names(), and set_relation_column_names().

◆ appendContextKeyword()

static void appendContextKeyword ( deparse_context context,
const char *  str,
int  indentBefore,
int  indentAfter,
int  indentPlus 
)
static

Definition at line 9072 of file ruleutils.c.

9074{
9075 StringInfo buf = context->buf;
9076
9077 if (PRETTY_INDENT(context))
9078 {
9079 int indentAmount;
9080
9081 context->indentLevel += indentBefore;
9082
9083 /* remove any trailing spaces currently in the buffer ... */
9085 /* ... then add a newline and some spaces */
9087
9088 if (context->indentLevel < PRETTYINDENT_LIMIT)
9089 indentAmount = Max(context->indentLevel, 0) + indentPlus;
9090 else
9091 {
9092 /*
9093 * If we're indented more than PRETTYINDENT_LIMIT characters, try
9094 * to conserve horizontal space by reducing the per-level
9095 * indentation. For best results the scale factor here should
9096 * divide all the indent amounts that get added to indentLevel
9097 * (PRETTYINDENT_STD, etc). It's important that the indentation
9098 * not grow unboundedly, else deeply-nested trees use O(N^2)
9099 * whitespace; so we also wrap modulo PRETTYINDENT_LIMIT.
9100 */
9101 indentAmount = PRETTYINDENT_LIMIT +
9102 (context->indentLevel - PRETTYINDENT_LIMIT) /
9103 (PRETTYINDENT_STD / 2);
9104 indentAmount %= PRETTYINDENT_LIMIT;
9105 /* scale/wrap logic affects indentLevel, but not indentPlus */
9106 indentAmount += indentPlus;
9107 }
9108 appendStringInfoSpaces(buf, indentAmount);
9109
9111
9112 context->indentLevel += indentAfter;
9113 if (context->indentLevel < 0)
9114 context->indentLevel = 0;
9115 }
9116 else
9118}
#define Max(x, y)
Definition: c.h:969
const char * str
static void removeStringInfoSpaces(StringInfo str)
Definition: ruleutils.c:9126
#define PRETTYINDENT_LIMIT
Definition: ruleutils.c:85
#define PRETTYINDENT_STD
Definition: ruleutils.c:81
#define PRETTY_INDENT(context)
Definition: ruleutils.c:102
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:260
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
StringInfo buf
Definition: ruleutils.c:114

References appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), deparse_context::buf, buf, deparse_context::indentLevel, Max, PRETTY_INDENT, PRETTYINDENT_LIMIT, PRETTYINDENT_STD, removeStringInfoSpaces(), and str.

Referenced by get_basic_select_query(), get_delete_query_def(), get_from_clause(), get_from_clause_item(), get_insert_query_def(), get_json_table(), get_json_table_columns(), get_json_table_nested_columns(), get_merge_query_def(), get_returning_clause(), get_rule_expr(), get_rule_windowclause(), get_select_query_def(), get_setop_query(), get_target_list(), get_update_query_def(), get_utility_query_def(), and get_with_clause().

◆ build_colinfo_names_hash()

static void build_colinfo_names_hash ( deparse_columns colinfo)
static

Definition at line 4978 of file ruleutils.c.

4979{
4980 HASHCTL hash_ctl;
4981 int i;
4982 ListCell *lc;
4983
4984 /*
4985 * Use a hash table only for RTEs with at least 32 columns. (The cutoff
4986 * is somewhat arbitrary, but let's choose it so that this code does get
4987 * exercised in the regression tests.)
4988 */
4989 if (colinfo->num_cols < 32)
4990 return;
4991
4992 /*
4993 * Set up the hash table. The entries are just strings with no other
4994 * payload.
4995 */
4996 hash_ctl.keysize = NAMEDATALEN;
4997 hash_ctl.entrysize = NAMEDATALEN;
4998 hash_ctl.hcxt = CurrentMemoryContext;
4999 colinfo->names_hash = hash_create("deparse_columns names",
5000 colinfo->num_cols + colinfo->num_new_cols,
5001 &hash_ctl,
5003
5004 /*
5005 * Preload the hash table with any names already present (these would have
5006 * come from set_using_names).
5007 */
5008 for (i = 0; i < colinfo->num_cols; i++)
5009 {
5010 char *oldname = colinfo->colnames[i];
5011
5012 if (oldname)
5013 add_to_names_hash(colinfo, oldname);
5014 }
5015
5016 for (i = 0; i < colinfo->num_new_cols; i++)
5017 {
5018 char *oldname = colinfo->new_colnames[i];
5019
5020 if (oldname)
5021 add_to_names_hash(colinfo, oldname);
5022 }
5023
5024 foreach(lc, colinfo->parentUsing)
5025 {
5026 char *oldname = (char *) lfirst(lc);
5027
5028 add_to_names_hash(colinfo, oldname);
5029 }
5030}
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_STRINGS
Definition: hsearch.h:96
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
int i
Definition: isn.c:74
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define NAMEDATALEN
#define lfirst(lc)
Definition: pg_list.h:172
static void add_to_names_hash(deparse_columns *colinfo, const char *name)
Definition: ruleutils.c:5036
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
List * parentUsing
Definition: ruleutils.c:276
char ** new_colnames
Definition: ruleutils.c:269
char ** colnames
Definition: ruleutils.c:252

References add_to_names_hash(), deparse_columns::colnames, CurrentMemoryContext, HASHCTL::entrysize, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_STRINGS, HASHCTL::hcxt, i, HASHCTL::keysize, lfirst, NAMEDATALEN, deparse_columns::names_hash, deparse_columns::new_colnames, deparse_columns::num_cols, deparse_columns::num_new_cols, and deparse_columns::parentUsing.

Referenced by set_join_column_names(), and set_relation_column_names().

◆ colname_is_unique()

static bool colname_is_unique ( const char *  colname,
deparse_namespace dpns,
deparse_columns colinfo 
)
static

Definition at line 4848 of file ruleutils.c.

4850{
4851 int i;
4852 ListCell *lc;
4853
4854 /*
4855 * If we have a hash table, consult that instead of linearly scanning the
4856 * colinfo's strings.
4857 */
4858 if (colinfo->names_hash)
4859 {
4860 if (hash_search(colinfo->names_hash,
4861 colname,
4862 HASH_FIND,
4863 NULL) != NULL)
4864 return false;
4865 }
4866 else
4867 {
4868 /* Check against already-assigned column aliases within RTE */
4869 for (i = 0; i < colinfo->num_cols; i++)
4870 {
4871 char *oldname = colinfo->colnames[i];
4872
4873 if (oldname && strcmp(oldname, colname) == 0)
4874 return false;
4875 }
4876
4877 /*
4878 * If we're building a new_colnames array, check that too (this will
4879 * be partially but not completely redundant with the previous checks)
4880 */
4881 for (i = 0; i < colinfo->num_new_cols; i++)
4882 {
4883 char *oldname = colinfo->new_colnames[i];
4884
4885 if (oldname && strcmp(oldname, colname) == 0)
4886 return false;
4887 }
4888
4889 /*
4890 * Also check against names already assigned for parent-join USING
4891 * cols
4892 */
4893 foreach(lc, colinfo->parentUsing)
4894 {
4895 char *oldname = (char *) lfirst(lc);
4896
4897 if (strcmp(oldname, colname) == 0)
4898 return false;
4899 }
4900 }
4901
4902 /*
4903 * Also check against USING-column names that must be globally unique.
4904 * These are not hashed, but there should be few of them.
4905 */
4906 foreach(lc, dpns->using_names)
4907 {
4908 char *oldname = (char *) lfirst(lc);
4909
4910 if (strcmp(oldname, colname) == 0)
4911 return false;
4912 }
4913
4914 return true;
4915}
@ HASH_FIND
Definition: hsearch.h:113
List * using_names
Definition: ruleutils.c:174

References deparse_columns::colnames, HASH_FIND, hash_search(), i, lfirst, deparse_columns::names_hash, deparse_columns::new_colnames, deparse_columns::num_cols, deparse_columns::num_new_cols, deparse_columns::parentUsing, and deparse_namespace::using_names.

Referenced by make_colname_unique().

◆ decompile_column_index_array()

static int decompile_column_index_array ( Datum  column_index_array,
Oid  relId,
bool  withPeriod,
StringInfo  buf 
)
static

Definition at line 2621 of file ruleutils.c.

2623{
2624 Datum *keys;
2625 int nKeys;
2626 int j;
2627
2628 /* Extract data from array of int16 */
2629 deconstruct_array_builtin(DatumGetArrayTypeP(column_index_array), INT2OID,
2630 &keys, NULL, &nKeys);
2631
2632 for (j = 0; j < nKeys; j++)
2633 {
2634 char *colName;
2635
2636 colName = get_attname(relId, DatumGetInt16(keys[j]), false);
2637
2638 if (j == 0)
2640 else
2641 appendStringInfo(buf, ", %s%s",
2642 (withPeriod && j == nKeys - 1) ? "PERIOD " : "",
2643 quote_identifier(colName));
2644 }
2645
2646 return nKeys;
2647}
#define DatumGetArrayTypeP(X)
Definition: array.h:261
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3697
int j
Definition: isn.c:75
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:862
uintptr_t Datum
Definition: postgres.h:69
static int16 DatumGetInt16(Datum X)
Definition: postgres.h:167

References appendStringInfo(), appendStringInfoString(), buf, DatumGetArrayTypeP, DatumGetInt16(), deconstruct_array_builtin(), get_attname(), j, and quote_identifier().

Referenced by pg_get_constraintdef_worker().

◆ deparse_context_for()

List * deparse_context_for ( const char *  aliasname,
Oid  relid 
)

Definition at line 3708 of file ruleutils.c.

3709{
3710 deparse_namespace *dpns;
3711 RangeTblEntry *rte;
3712
3713 dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3714
3715 /* Build a minimal RTE for the rel */
3716 rte = makeNode(RangeTblEntry);
3717 rte->rtekind = RTE_RELATION;
3718 rte->relid = relid;
3719 rte->relkind = RELKIND_RELATION; /* no need for exactness here */
3720 rte->rellockmode = AccessShareLock;
3721 rte->alias = makeAlias(aliasname, NIL);
3722 rte->eref = rte->alias;
3723 rte->lateral = false;
3724 rte->inh = false;
3725 rte->inFromCl = true;
3726
3727 /* Build one-element rtable */
3728 dpns->rtable = list_make1(rte);
3729 dpns->subplans = NIL;
3730 dpns->ctes = NIL;
3731 dpns->appendrels = NULL;
3732 set_rtable_names(dpns, NIL, NULL);
3734
3735 /* Return a one-deep namespace stack */
3736 return list_make1(dpns);
3737}
#define AccessShareLock
Definition: lockdefs.h:36
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:438
void * palloc0(Size size)
Definition: mcxt.c:1347
#define makeNode(_type_)
Definition: nodes.h:157
@ RTE_RELATION
Definition: parsenodes.h:1026
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
static void set_simple_column_names(deparse_namespace *dpns)
Definition: ruleutils.c:4098
static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces, Bitmapset *rels_used)
Definition: ruleutils.c:3884
RTEKind rtekind
Definition: parsenodes.h:1056
AppendRelInfo ** appendrels
Definition: ruleutils.c:169

References AccessShareLock, deparse_namespace::appendrels, deparse_namespace::ctes, RangeTblEntry::inh, list_make1, makeAlias(), makeNode, NIL, palloc0(), RangeTblEntry::relid, deparse_namespace::rtable, RTE_RELATION, RangeTblEntry::rtekind, set_rtable_names(), set_simple_column_names(), and deparse_namespace::subplans.

Referenced by pg_get_constraintdef_worker(), pg_get_expr_worker(), pg_get_indexdef_worker(), pg_get_partconstrdef_string(), pg_get_partition_constraintdef(), pg_get_partkeydef_worker(), pg_get_statisticsobj_worker(), pg_get_statisticsobjdef_expressions(), transformPartitionBound(), and transformPartitionRangeBounds().

◆ deparse_context_for_plan_tree()

List * deparse_context_for_plan_tree ( PlannedStmt pstmt,
List rtable_names 
)

Definition at line 3753 of file ruleutils.c.

3754{
3755 deparse_namespace *dpns;
3756
3757 dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3758
3759 /* Initialize fields that stay the same across the whole plan tree */
3760 dpns->rtable = pstmt->rtable;
3761 dpns->rtable_names = rtable_names;
3762 dpns->subplans = pstmt->subplans;
3763 dpns->ctes = NIL;
3764 if (pstmt->appendRelations)
3765 {
3766 /* Set up the array, indexed by child relid */
3767 int ntables = list_length(dpns->rtable);
3768 ListCell *lc;
3769
3770 dpns->appendrels = (AppendRelInfo **)
3771 palloc0((ntables + 1) * sizeof(AppendRelInfo *));
3772 foreach(lc, pstmt->appendRelations)
3773 {
3774 AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
3775 Index crelid = appinfo->child_relid;
3776
3777 Assert(crelid > 0 && crelid <= ntables);
3778 Assert(dpns->appendrels[crelid] == NULL);
3779 dpns->appendrels[crelid] = appinfo;
3780 }
3781 }
3782 else
3783 dpns->appendrels = NULL; /* don't need it */
3784
3785 /*
3786 * Set up column name aliases, ignoring any join RTEs; they don't matter
3787 * because plan trees don't contain any join alias Vars.
3788 */
3790
3791 /* Return a one-deep namespace stack */
3792 return list_make1(dpns);
3793}
unsigned int Index
Definition: c.h:585
Assert(PointerIsAligned(start, uint64))
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
Index child_relid
Definition: pathnodes.h:3008
List * appendRelations
Definition: plannodes.h:116
List * subplans
Definition: plannodes.h:121
List * rtable
Definition: plannodes.h:91
List * rtable_names
Definition: ruleutils.c:165

References PlannedStmt::appendRelations, deparse_namespace::appendrels, Assert(), AppendRelInfo::child_relid, deparse_namespace::ctes, lfirst_node, list_length(), list_make1, NIL, palloc0(), deparse_namespace::rtable, PlannedStmt::rtable, deparse_namespace::rtable_names, set_simple_column_names(), deparse_namespace::subplans, and PlannedStmt::subplans.

Referenced by ExplainPrintPlan().

◆ deparse_expression()

char * deparse_expression ( Node expr,
List dpcontext,
bool  forceprefix,
bool  showimplicit 
)

Definition at line 3645 of file ruleutils.c.

3647{
3648 return deparse_expression_pretty(expr, dpcontext, forceprefix,
3649 showimplicit, 0, 0);
3650}
static char * deparse_expression_pretty(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit, int prettyFlags, int startIndent)
Definition: ruleutils.c:3672

References deparse_expression_pretty().

Referenced by AlterDomainDefault(), DefineDomain(), pg_get_function_arg_default(), pg_get_partconstrdef_string(), print_function_arguments(), show_expression(), show_grouping_set_keys(), show_memoize_info(), show_plan_tlist(), show_sort_group_keys(), show_tablesample(), show_window_keys(), transformPartitionBound(), and transformPartitionRangeBounds().

◆ deparse_expression_pretty()

static char * deparse_expression_pretty ( Node expr,
List dpcontext,
bool  forceprefix,
bool  showimplicit,
int  prettyFlags,
int  startIndent 
)
static

Definition at line 3672 of file ruleutils.c.

3675{
3677 deparse_context context;
3678
3680 context.buf = &buf;
3681 context.namespaces = dpcontext;
3682 context.resultDesc = NULL;
3683 context.targetList = NIL;
3684 context.windowClause = NIL;
3685 context.varprefix = forceprefix;
3686 context.prettyFlags = prettyFlags;
3688 context.indentLevel = startIndent;
3689 context.colNamesVisible = true;
3690 context.inGroupBy = false;
3691 context.varInOrderBy = false;
3692 context.appendparents = NULL;
3693
3694 get_rule_expr(expr, &context, showimplicit);
3695
3696 return buf.data;
3697}
#define WRAP_COLUMN_DEFAULT
Definition: ruleutils.c:98
static void get_rule_expr(Node *node, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:9242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
TupleDesc resultDesc
Definition: ruleutils.c:116
List * targetList
Definition: ruleutils.c:117
bool colNamesVisible
Definition: ruleutils.c:123
List * namespaces
Definition: ruleutils.c:115
List * windowClause
Definition: ruleutils.c:118
Bitmapset * appendparents
Definition: ruleutils.c:126

References deparse_context::appendparents, deparse_context::buf, buf, deparse_context::colNamesVisible, get_rule_expr(), deparse_context::indentLevel, deparse_context::inGroupBy, initStringInfo(), deparse_context::namespaces, NIL, deparse_context::prettyFlags, deparse_context::resultDesc, deparse_context::targetList, deparse_context::varInOrderBy, deparse_context::varprefix, deparse_context::windowClause, WRAP_COLUMN_DEFAULT, and deparse_context::wrapColumn.

Referenced by deparse_expression(), pg_get_constraintdef_worker(), pg_get_expr_worker(), pg_get_indexdef_worker(), pg_get_partition_constraintdef(), pg_get_partkeydef_worker(), pg_get_statisticsobj_worker(), and pg_get_statisticsobjdef_expressions().

◆ destroy_colinfo_names_hash()

static void destroy_colinfo_names_hash ( deparse_columns colinfo)
static

Definition at line 5049 of file ruleutils.c.

5050{
5051 if (colinfo->names_hash)
5052 {
5053 hash_destroy(colinfo->names_hash);
5054 colinfo->names_hash = NULL;
5055 }
5056}
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:865

References hash_destroy(), and deparse_columns::names_hash.

Referenced by set_join_column_names(), and set_relation_column_names().

◆ expand_colnames_array_to()

static void expand_colnames_array_to ( deparse_columns colinfo,
int  n 
)
static

Definition at line 4962 of file ruleutils.c.

4963{
4964 if (n > colinfo->num_cols)
4965 {
4966 if (colinfo->colnames == NULL)
4967 colinfo->colnames = palloc0_array(char *, n);
4968 else
4969 colinfo->colnames = repalloc0_array(colinfo->colnames, char *, colinfo->num_cols, n);
4970 colinfo->num_cols = n;
4971 }
4972}
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109

References deparse_columns::colnames, deparse_columns::num_cols, palloc0_array, and repalloc0_array.

Referenced by set_join_column_names(), set_relation_column_names(), and set_using_names().

◆ find_param_generator()

static SubPlan * find_param_generator ( Param param,
deparse_context context,
int *  column_p 
)
static

Definition at line 8559 of file ruleutils.c.

8560{
8561 /* Initialize output parameter to prevent compiler warnings */
8562 *column_p = 0;
8563
8564 /*
8565 * If it's a PARAM_EXEC parameter, search the current plan node as well as
8566 * ancestor nodes looking for a subplan or initplan that emits the value
8567 * for the Param. It could appear in the setParams of an initplan or
8568 * MULTIEXPR_SUBLINK subplan, or in the paramIds of an ancestral SubPlan.
8569 */
8570 if (param->paramkind == PARAM_EXEC)
8571 {
8572 SubPlan *result;
8573 deparse_namespace *dpns;
8574 ListCell *lc;
8575
8576 dpns = (deparse_namespace *) linitial(context->namespaces);
8577
8578 /* First check the innermost plan node's initplans */
8579 result = find_param_generator_initplan(param, dpns->plan, column_p);
8580 if (result)
8581 return result;
8582
8583 /*
8584 * The plan's targetlist might contain MULTIEXPR_SUBLINK SubPlans,
8585 * which can be referenced by Params elsewhere in the targetlist.
8586 * (Such Params should always be in the same targetlist, so there's no
8587 * need to do this work at upper plan nodes.)
8588 */
8590 {
8591 if (tle->expr && IsA(tle->expr, SubPlan))
8592 {
8593 SubPlan *subplan = (SubPlan *) tle->expr;
8594
8595 if (subplan->subLinkType == MULTIEXPR_SUBLINK)
8596 {
8597 foreach_int(paramid, subplan->setParam)
8598 {
8599 if (paramid == param->paramid)
8600 {
8601 /* Found a match, so return it. */
8602 *column_p = foreach_current_index(paramid);
8603 return subplan;
8604 }
8605 }
8606 }
8607 }
8608 }
8609
8610 /* No luck, so check the ancestor nodes */
8611 foreach(lc, dpns->ancestors)
8612 {
8613 Node *ancestor = (Node *) lfirst(lc);
8614
8615 /*
8616 * If ancestor is a SubPlan, check the paramIds it provides.
8617 */
8618 if (IsA(ancestor, SubPlan))
8619 {
8620 SubPlan *subplan = (SubPlan *) ancestor;
8621
8622 foreach_int(paramid, subplan->paramIds)
8623 {
8624 if (paramid == param->paramid)
8625 {
8626 /* Found a match, so return it. */
8627 *column_p = foreach_current_index(paramid);
8628 return subplan;
8629 }
8630 }
8631
8632 /* SubPlan isn't a kind of Plan, so skip the rest */
8633 continue;
8634 }
8635
8636 /*
8637 * Otherwise, it's some kind of Plan node, so check its initplans.
8638 */
8639 result = find_param_generator_initplan(param, (Plan *) ancestor,
8640 column_p);
8641 if (result)
8642 return result;
8643
8644 /* No luck, crawl up to next ancestor */
8645 }
8646 }
8647
8648 /* No generator found */
8649 return NULL;
8650}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:78
#define IsA(nodeptr, _type_)
Definition: nodes.h:160
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
#define linitial(l)
Definition: pg_list.h:178
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
#define foreach_int(var, lst)
Definition: pg_list.h:470
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:1019
@ PARAM_EXEC
Definition: primnodes.h:385
static SubPlan * find_param_generator_initplan(Param *param, Plan *plan, int *column_p)
Definition: ruleutils.c:8656
Definition: nodes.h:131
int paramid
Definition: primnodes.h:394
ParamKind paramkind
Definition: primnodes.h:393
List * targetlist
Definition: plannodes.h:209
List * paramIds
Definition: primnodes.h:1085
List * setParam
Definition: primnodes.h:1105
SubLinkType subLinkType
Definition: primnodes.h:1082

References deparse_namespace::ancestors, find_param_generator_initplan(), foreach_current_index, foreach_int, foreach_node, if(), IsA, lfirst, linitial, MULTIEXPR_SUBLINK, deparse_context::namespaces, PARAM_EXEC, Param::paramid, SubPlan::paramIds, Param::paramkind, deparse_namespace::plan, SubPlan::setParam, SubPlan::subLinkType, and Plan::targetlist.

Referenced by get_parameter().

◆ find_param_generator_initplan()

static SubPlan * find_param_generator_initplan ( Param param,
Plan plan,
int *  column_p 
)
static

Definition at line 8656 of file ruleutils.c.

8657{
8658 foreach_node(SubPlan, subplan, plan->initPlan)
8659 {
8660 foreach_int(paramid, subplan->setParam)
8661 {
8662 if (paramid == param->paramid)
8663 {
8664 /* Found a match, so return it. */
8665 *column_p = foreach_current_index(paramid);
8666 return subplan;
8667 }
8668 }
8669 }
8670 return NULL;
8671}
#define plan(x)
Definition: pg_regress.c:161

References foreach_current_index, foreach_int, foreach_node, Param::paramid, and plan.

Referenced by find_param_generator().

◆ find_param_referent()

static Node * find_param_referent ( Param param,
deparse_context context,
deparse_namespace **  dpns_p,
ListCell **  ancestor_cell_p 
)
static

Definition at line 8445 of file ruleutils.c.

8447{
8448 /* Initialize output parameters to prevent compiler warnings */
8449 *dpns_p = NULL;
8450 *ancestor_cell_p = NULL;
8451
8452 /*
8453 * If it's a PARAM_EXEC parameter, look for a matching NestLoopParam or
8454 * SubPlan argument. This will necessarily be in some ancestor of the
8455 * current expression's Plan node.
8456 */
8457 if (param->paramkind == PARAM_EXEC)
8458 {
8459 deparse_namespace *dpns;
8460 Plan *child_plan;
8461 ListCell *lc;
8462
8463 dpns = (deparse_namespace *) linitial(context->namespaces);
8464 child_plan = dpns->plan;
8465
8466 foreach(lc, dpns->ancestors)
8467 {
8468 Node *ancestor = (Node *) lfirst(lc);
8469 ListCell *lc2;
8470
8471 /*
8472 * NestLoops transmit params to their inner child only.
8473 */
8474 if (IsA(ancestor, NestLoop) &&
8475 child_plan == innerPlan(ancestor))
8476 {
8477 NestLoop *nl = (NestLoop *) ancestor;
8478
8479 foreach(lc2, nl->nestParams)
8480 {
8481 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc2);
8482
8483 if (nlp->paramno == param->paramid)
8484 {
8485 /* Found a match, so return it */
8486 *dpns_p = dpns;
8487 *ancestor_cell_p = lc;
8488 return (Node *) nlp->paramval;
8489 }
8490 }
8491 }
8492
8493 /*
8494 * If ancestor is a SubPlan, check the arguments it provides.
8495 */
8496 if (IsA(ancestor, SubPlan))
8497 {
8498 SubPlan *subplan = (SubPlan *) ancestor;
8499 ListCell *lc3;
8500 ListCell *lc4;
8501
8502 forboth(lc3, subplan->parParam, lc4, subplan->args)
8503 {
8504 int paramid = lfirst_int(lc3);
8505 Node *arg = (Node *) lfirst(lc4);
8506
8507 if (paramid == param->paramid)
8508 {
8509 /*
8510 * Found a match, so return it. But, since Vars in
8511 * the arg are to be evaluated in the surrounding
8512 * context, we have to point to the next ancestor item
8513 * that is *not* a SubPlan.
8514 */
8515 ListCell *rest;
8516
8517 for_each_cell(rest, dpns->ancestors,
8518 lnext(dpns->ancestors, lc))
8519 {
8520 Node *ancestor2 = (Node *) lfirst(rest);
8521
8522 if (!IsA(ancestor2, SubPlan))
8523 {
8524 *dpns_p = dpns;
8525 *ancestor_cell_p = rest;
8526 return arg;
8527 }
8528 }
8529 elog(ERROR, "SubPlan cannot be outermost ancestor");
8530 }
8531 }
8532
8533 /* SubPlan isn't a kind of Plan, so skip the rest */
8534 continue;
8535 }
8536
8537 /*
8538 * We need not consider the ancestor's initPlan list, since
8539 * initplans never have any parParams.
8540 */
8541
8542 /* No luck, crawl up to next ancestor */
8543 child_plan = (Plan *) ancestor;
8544 }
8545 }
8546
8547 /* No referent found */
8548 return NULL;
8549}
void * arg
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lfirst_int(lc)
Definition: pg_list.h:173
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define innerPlan(node)
Definition: plannodes.h:240
Var * paramval
Definition: plannodes.h:960
List * nestParams
Definition: plannodes.h:949
List * args
Definition: primnodes.h:1108
List * parParam
Definition: primnodes.h:1107

References deparse_namespace::ancestors, arg, SubPlan::args, elog, ERROR, for_each_cell, forboth, innerPlan, IsA, lfirst, lfirst_int, linitial, lnext(), deparse_context::namespaces, NestLoop::nestParams, PARAM_EXEC, Param::paramid, Param::paramkind, NestLoopParam::paramno, NestLoopParam::paramval, SubPlan::parParam, and deparse_namespace::plan.

Referenced by get_name_for_var_field(), and get_parameter().

◆ find_recursive_union()

static Plan * find_recursive_union ( deparse_namespace dpns,
WorkTableScan wtscan 
)
static

Definition at line 5233 of file ruleutils.c.

5234{
5235 ListCell *lc;
5236
5237 foreach(lc, dpns->ancestors)
5238 {
5239 Plan *ancestor = (Plan *) lfirst(lc);
5240
5241 if (IsA(ancestor, RecursiveUnion) &&
5242 ((RecursiveUnion *) ancestor)->wtParam == wtscan->wtParam)
5243 return ancestor;
5244 }
5245 elog(ERROR, "could not find RecursiveUnion for WorkTableScan with wtParam %d",
5246 wtscan->wtParam);
5247 return NULL;
5248}

References deparse_namespace::ancestors, elog, ERROR, IsA, lfirst, and WorkTableScan::wtParam.

Referenced by set_deparse_plan().

◆ flatten_reloptions()

static char * flatten_reloptions ( Oid  relid)
static

Definition at line 13633 of file ruleutils.c.

13634{
13635 char *result = NULL;
13636 HeapTuple tuple;
13637 Datum reloptions;
13638 bool isnull;
13639
13640 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
13641 if (!HeapTupleIsValid(tuple))
13642 elog(ERROR, "cache lookup failed for relation %u", relid);
13643
13644 reloptions = SysCacheGetAttr(RELOID, tuple,
13645 Anum_pg_class_reloptions, &isnull);
13646 if (!isnull)
13647 {
13649
13651 get_reloptions(&buf, reloptions);
13652
13653 result = buf.data;
13654 }
13655
13656 ReleaseSysCache(tuple);
13657
13658 return result;
13659}
static void get_reloptions(StringInfo buf, Datum reloptions)
Definition: ruleutils.c:13578
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600

References buf, elog, ERROR, get_reloptions(), HeapTupleIsValid, initStringInfo(), ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache1(), and SysCacheGetAttr().

Referenced by pg_get_constraintdef_worker(), and pg_get_indexdef_worker().

◆ generate_collation_name()

char * generate_collation_name ( Oid  collid)

Definition at line 13533 of file ruleutils.c.

13534{
13535 HeapTuple tp;
13536 Form_pg_collation colltup;
13537 char *collname;
13538 char *nspname;
13539 char *result;
13540
13541 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
13542 if (!HeapTupleIsValid(tp))
13543 elog(ERROR, "cache lookup failed for collation %u", collid);
13544 colltup = (Form_pg_collation) GETSTRUCT(tp);
13545 collname = NameStr(colltup->collname);
13546
13548 nspname = get_namespace_name_or_temp(colltup->collnamespace);
13549 else
13550 nspname = NULL;
13551
13552 result = quote_qualified_identifier(nspname, collname);
13553
13554 ReleaseSysCache(tp);
13555
13556 return result;
13557}
Oid collid
bool CollationIsVisible(Oid collid)
Definition: namespace.c:2407
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:58
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:13103

References CollationIsVisible(), collid, elog, ERROR, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), quote_qualified_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by get_const_collation(), get_from_clause_coldeflist(), get_rule_expr(), pg_collation_for(), pg_get_indexdef_worker(), and pg_get_partkeydef_worker().

◆ generate_function_name()

static char * generate_function_name ( Oid  funcid,
int  nargs,
List argnames,
Oid argtypes,
bool  has_variadic,
bool *  use_variadic_p,
bool  inGroupBy 
)
static

Definition at line 13247 of file ruleutils.c.

13250{
13251 char *result;
13252 HeapTuple proctup;
13253 Form_pg_proc procform;
13254 char *proname;
13255 bool use_variadic;
13256 char *nspname;
13257 FuncDetailCode p_result;
13258 Oid p_funcid;
13259 Oid p_rettype;
13260 bool p_retset;
13261 int p_nvargs;
13262 Oid p_vatype;
13263 Oid *p_true_typeids;
13264 bool force_qualify = false;
13265
13266 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
13267 if (!HeapTupleIsValid(proctup))
13268 elog(ERROR, "cache lookup failed for function %u", funcid);
13269 procform = (Form_pg_proc) GETSTRUCT(proctup);
13270 proname = NameStr(procform->proname);
13271
13272 /*
13273 * Due to parser hacks to avoid needing to reserve CUBE, we need to force
13274 * qualification of some function names within GROUP BY.
13275 */
13276 if (inGroupBy)
13277 {
13278 if (strcmp(proname, "cube") == 0 || strcmp(proname, "rollup") == 0)
13279 force_qualify = true;
13280 }
13281
13282 /*
13283 * Determine whether VARIADIC should be printed. We must do this first
13284 * since it affects the lookup rules in func_get_detail().
13285 *
13286 * We always print VARIADIC if the function has a merged variadic-array
13287 * argument. Note that this is always the case for functions taking a
13288 * VARIADIC argument type other than VARIADIC ANY. If we omitted VARIADIC
13289 * and printed the array elements as separate arguments, the call could
13290 * match a newer non-VARIADIC function.
13291 */
13292 if (use_variadic_p)
13293 {
13294 /* Parser should not have set funcvariadic unless fn is variadic */
13295 Assert(!has_variadic || OidIsValid(procform->provariadic));
13296 use_variadic = has_variadic;
13297 *use_variadic_p = use_variadic;
13298 }
13299 else
13300 {
13301 Assert(!has_variadic);
13302 use_variadic = false;
13303 }
13304
13305 /*
13306 * The idea here is to schema-qualify only if the parser would fail to
13307 * resolve the correct function given the unqualified func name with the
13308 * specified argtypes and VARIADIC flag. But if we already decided to
13309 * force qualification, then we can skip the lookup and pretend we didn't
13310 * find it.
13311 */
13312 if (!force_qualify)
13314 NIL, argnames, nargs, argtypes,
13315 !use_variadic, true, false,
13316 &p_funcid, &p_rettype,
13317 &p_retset, &p_nvargs, &p_vatype,
13318 &p_true_typeids, NULL);
13319 else
13320 {
13321 p_result = FUNCDETAIL_NOTFOUND;
13322 p_funcid = InvalidOid;
13323 }
13324
13325 if ((p_result == FUNCDETAIL_NORMAL ||
13326 p_result == FUNCDETAIL_AGGREGATE ||
13327 p_result == FUNCDETAIL_WINDOWFUNC) &&
13328 p_funcid == funcid)
13329 nspname = NULL;
13330 else
13331 nspname = get_namespace_name_or_temp(procform->pronamespace);
13332
13333 result = quote_qualified_identifier(nspname, proname);
13334
13335 ReleaseSysCache(proctup);
13336
13337 return result;
13338}
#define OidIsValid(objectId)
Definition: c.h:746
FuncDetailCode func_get_detail(List *funcname, List *fargs, List *fargnames, int nargs, Oid *argtypes, bool expand_variadic, bool expand_defaults, bool include_out_arguments, Oid *funcid, Oid *rettype, bool *retset, int *nvargs, Oid *vatype, Oid **true_typeids, List **argdefaults)
Definition: parse_func.c:1395
FuncDetailCode
Definition: parse_func.h:23
@ FUNCDETAIL_NORMAL
Definition: parse_func.h:26
@ FUNCDETAIL_WINDOWFUNC
Definition: parse_func.h:29
@ FUNCDETAIL_NOTFOUND
Definition: parse_func.h:24
@ FUNCDETAIL_AGGREGATE
Definition: parse_func.h:28
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
NameData proname
Definition: pg_proc.h:35
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
String * makeString(char *str)
Definition: value.c:63

References Assert(), elog, ERROR, func_get_detail(), FUNCDETAIL_AGGREGATE, FUNCDETAIL_NORMAL, FUNCDETAIL_NOTFOUND, FUNCDETAIL_WINDOWFUNC, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, list_make1, makeString(), NameStr, NIL, ObjectIdGetDatum(), OidIsValid, proname, quote_qualified_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by get_agg_expr_helper(), get_func_expr(), get_tablesample_def(), get_windowfunc_expr_helper(), pg_get_functiondef(), and pg_get_triggerdef_worker().

◆ generate_opclass_name()

char * generate_opclass_name ( Oid  opclass)

Definition at line 12889 of file ruleutils.c.

12890{
12892
12894 get_opclass_name(opclass, InvalidOid, &buf);
12895
12896 return &buf.data[1]; /* get_opclass_name() prepends space */
12897}
static void get_opclass_name(Oid opclass, Oid actual_datatype, StringInfo buf)
Definition: ruleutils.c:12851

References buf, get_opclass_name(), initStringInfo(), and InvalidOid.

Referenced by index_opclass_options().

◆ generate_operator_clause()

void generate_operator_clause ( StringInfo  buf,
const char *  leftop,
Oid  leftoptype,
Oid  opoid,
const char *  rightop,
Oid  rightoptype 
)

Definition at line 13429 of file ruleutils.c.

13433{
13434 HeapTuple opertup;
13435 Form_pg_operator operform;
13436 char *oprname;
13437 char *nspname;
13438
13439 opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opoid));
13440 if (!HeapTupleIsValid(opertup))
13441 elog(ERROR, "cache lookup failed for operator %u", opoid);
13442 operform = (Form_pg_operator) GETSTRUCT(opertup);
13443 Assert(operform->oprkind == 'b');
13444 oprname = NameStr(operform->oprname);
13445
13446 nspname = get_namespace_name(operform->oprnamespace);
13447
13448 appendStringInfoString(buf, leftop);
13449 if (leftoptype != operform->oprleft)
13450 add_cast_to(buf, operform->oprleft);
13451 appendStringInfo(buf, " OPERATOR(%s.", quote_identifier(nspname));
13452 appendStringInfoString(buf, oprname);
13453 appendStringInfo(buf, ") %s", rightop);
13454 if (rightoptype != operform->oprright)
13455 add_cast_to(buf, operform->oprright);
13456
13457 ReleaseSysCache(opertup);
13458}
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3449
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
static void add_cast_to(StringInfo buf, Oid typid)
Definition: ruleutils.c:13469

References add_cast_to(), appendStringInfo(), appendStringInfoString(), Assert(), buf, elog, ERROR, get_namespace_name(), GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), quote_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by refresh_by_match_merge(), and ri_GenerateQual().

◆ generate_operator_name()

static char * generate_operator_name ( Oid  operid,
Oid  arg1,
Oid  arg2 
)
static

Definition at line 13352 of file ruleutils.c.

13353{
13355 HeapTuple opertup;
13356 Form_pg_operator operform;
13357 char *oprname;
13358 char *nspname;
13359 Operator p_result;
13360
13362
13363 opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operid));
13364 if (!HeapTupleIsValid(opertup))
13365 elog(ERROR, "cache lookup failed for operator %u", operid);
13366 operform = (Form_pg_operator) GETSTRUCT(opertup);
13367 oprname = NameStr(operform->oprname);
13368
13369 /*
13370 * The idea here is to schema-qualify only if the parser would fail to
13371 * resolve the correct operator given the unqualified op name with the
13372 * specified argtypes.
13373 */
13374 switch (operform->oprkind)
13375 {
13376 case 'b':
13377 p_result = oper(NULL, list_make1(makeString(oprname)), arg1, arg2,
13378 true, -1);
13379 break;
13380 case 'l':
13381 p_result = left_oper(NULL, list_make1(makeString(oprname)), arg2,
13382 true, -1);
13383 break;
13384 default:
13385 elog(ERROR, "unrecognized oprkind: %d", operform->oprkind);
13386 p_result = NULL; /* keep compiler quiet */
13387 break;
13388 }
13389
13390 if (p_result != NULL && oprid(p_result) == operid)
13391 nspname = NULL;
13392 else
13393 {
13394 nspname = get_namespace_name_or_temp(operform->oprnamespace);
13395 appendStringInfo(&buf, "OPERATOR(%s.", quote_identifier(nspname));
13396 }
13397
13398 appendStringInfoString(&buf, oprname);
13399
13400 if (nspname)
13402
13403 if (p_result != NULL)
13404 ReleaseSysCache(p_result);
13405
13406 ReleaseSysCache(opertup);
13407
13408 return buf.data;
13409}
Operator left_oper(ParseState *pstate, List *op, Oid arg, bool noError, int location)
Definition: parse_oper.c:518
Oid oprid(Operator op)
Definition: parse_oper.c:238
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:370

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), buf, elog, ERROR, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, initStringInfo(), left_oper(), list_make1, makeString(), NameStr, ObjectIdGetDatum(), oper(), oprid(), quote_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by get_oper_expr(), get_rule_expr(), get_rule_orderby(), get_simple_binary_op_name(), get_sublink_expr(), and pg_get_indexdef_worker().

◆ generate_qualified_relation_name()

static char * generate_qualified_relation_name ( Oid  relid)
static

Definition at line 13203 of file ruleutils.c.

13204{
13205 HeapTuple tp;
13206 Form_pg_class reltup;
13207 char *relname;
13208 char *nspname;
13209 char *result;
13210
13211 tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
13212 if (!HeapTupleIsValid(tp))
13213 elog(ERROR, "cache lookup failed for relation %u", relid);
13214 reltup = (Form_pg_class) GETSTRUCT(tp);
13215 relname = NameStr(reltup->relname);
13216
13217 nspname = get_namespace_name_or_temp(reltup->relnamespace);
13218 if (!nspname)
13219 elog(ERROR, "cache lookup failed for namespace %u",
13220 reltup->relnamespace);
13221
13222 result = quote_qualified_identifier(nspname, relname);
13223
13224 ReleaseSysCache(tp);
13225
13226 return result;
13227}
NameData relname
Definition: pg_class.h:38
FormData_pg_class * Form_pg_class
Definition: pg_class.h:156

References elog, ERROR, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), quote_qualified_identifier(), ReleaseSysCache(), relname, and SearchSysCache1().

Referenced by make_ruledef(), pg_get_constraintdef_worker(), pg_get_indexdef_worker(), pg_get_serial_sequence(), and pg_get_triggerdef_worker().

◆ generate_qualified_type_name()

static char * generate_qualified_type_name ( Oid  typid)
static

Definition at line 13500 of file ruleutils.c.

13501{
13502 HeapTuple tp;
13503 Form_pg_type typtup;
13504 char *typname;
13505 char *nspname;
13506 char *result;
13507
13508 tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
13509 if (!HeapTupleIsValid(tp))
13510 elog(ERROR, "cache lookup failed for type %u", typid);
13511 typtup = (Form_pg_type) GETSTRUCT(tp);
13512 typname = NameStr(typtup->typname);
13513
13514 nspname = get_namespace_name_or_temp(typtup->typnamespace);
13515 if (!nspname)
13516 elog(ERROR, "cache lookup failed for namespace %u",
13517 typtup->typnamespace);
13518
13519 result = quote_qualified_identifier(nspname, typname);
13520
13521 ReleaseSysCache(tp);
13522
13523 return result;
13524}

References elog, ERROR, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), and typname.

Referenced by pg_get_constraintdef_worker().

◆ generate_relation_name()

static char * generate_relation_name ( Oid  relid,
List namespaces 
)
static

Definition at line 13143 of file ruleutils.c.

13144{
13145 HeapTuple tp;
13146 Form_pg_class reltup;
13147 bool need_qual;
13148 ListCell *nslist;
13149 char *relname;
13150 char *nspname;
13151 char *result;
13152
13153 tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
13154 if (!HeapTupleIsValid(tp))
13155 elog(ERROR, "cache lookup failed for relation %u", relid);
13156 reltup = (Form_pg_class) GETSTRUCT(tp);
13157 relname = NameStr(reltup->relname);
13158
13159 /* Check for conflicting CTE name */
13160 need_qual = false;
13161 foreach(nslist, namespaces)
13162 {
13163 deparse_namespace *dpns = (deparse_namespace *) lfirst(nslist);
13164 ListCell *ctlist;
13165
13166 foreach(ctlist, dpns->ctes)
13167 {
13168 CommonTableExpr *cte = (CommonTableExpr *) lfirst(ctlist);
13169
13170 if (strcmp(cte->ctename, relname) == 0)
13171 {
13172 need_qual = true;
13173 break;
13174 }
13175 }
13176 if (need_qual)
13177 break;
13178 }
13179
13180 /* Otherwise, qualify the name if not visible in search path */
13181 if (!need_qual)
13182 need_qual = !RelationIsVisible(relid);
13183
13184 if (need_qual)
13185 nspname = get_namespace_name_or_temp(reltup->relnamespace);
13186 else
13187 nspname = NULL;
13188
13189 result = quote_qualified_identifier(nspname, relname);
13190
13191 ReleaseSysCache(tp);
13192
13193 return result;
13194}
bool RelationIsVisible(Oid relid)
Definition: namespace.c:913

References CommonTableExpr::ctename, deparse_namespace::ctes, elog, ERROR, get_namespace_name_or_temp(), GETSTRUCT(), HeapTupleIsValid, lfirst, NameStr, ObjectIdGetDatum(), quote_qualified_identifier(), RelationIsVisible(), ReleaseSysCache(), relname, and SearchSysCache1().

Referenced by get_delete_query_def(), get_from_clause_item(), get_insert_query_def(), get_merge_query_def(), get_rule_expr(), get_update_query_def(), make_ruledef(), pg_get_constraintdef_worker(), pg_get_indexdef_worker(), pg_get_statisticsobj_worker(), and pg_get_triggerdef_worker().

◆ get_agg_combine_expr()

static void get_agg_combine_expr ( Node node,
deparse_context context,
void *  callback_arg 
)
static

Definition at line 10999 of file ruleutils.c.

11000{
11001 Aggref *aggref;
11002 Aggref *original_aggref = callback_arg;
11003
11004 if (!IsA(node, Aggref))
11005 elog(ERROR, "combining Aggref does not point to an Aggref");
11006
11007 aggref = (Aggref *) node;
11008 get_agg_expr(aggref, context, original_aggref);
11009}
static void get_agg_expr(Aggref *aggref, deparse_context *context, Aggref *original_aggref)
Definition: ruleutils.c:10861

References elog, ERROR, get_agg_expr(), and IsA.

Referenced by get_agg_expr_helper().

◆ get_agg_expr()

static void get_agg_expr ( Aggref aggref,
deparse_context context,
Aggref original_aggref 
)
static

Definition at line 10861 of file ruleutils.c.

10863{
10864 get_agg_expr_helper(aggref, context, original_aggref, NULL, NULL,
10865 false);
10866}
static void get_agg_expr_helper(Aggref *aggref, deparse_context *context, Aggref *original_aggref, const char *funcname, const char *options, bool is_json_objectagg)
Definition: ruleutils.c:10873

References get_agg_expr_helper().

Referenced by get_agg_combine_expr(), and get_rule_expr().

◆ get_agg_expr_helper()

static void get_agg_expr_helper ( Aggref aggref,
deparse_context context,
Aggref original_aggref,
const char *  funcname,
const char *  options,
bool  is_json_objectagg 
)
static

Definition at line 10873 of file ruleutils.c.

10876{
10877 StringInfo buf = context->buf;
10878 Oid argtypes[FUNC_MAX_ARGS];
10879 int nargs;
10880 bool use_variadic = false;
10881
10882 /*
10883 * For a combining aggregate, we look up and deparse the corresponding
10884 * partial aggregate instead. This is necessary because our input
10885 * argument list has been replaced; the new argument list always has just
10886 * one element, which will point to a partial Aggref that supplies us with
10887 * transition states to combine.
10888 */
10889 if (DO_AGGSPLIT_COMBINE(aggref->aggsplit))
10890 {
10891 TargetEntry *tle;
10892
10893 Assert(list_length(aggref->args) == 1);
10894 tle = linitial_node(TargetEntry, aggref->args);
10895 resolve_special_varno((Node *) tle->expr, context,
10896 get_agg_combine_expr, original_aggref);
10897 return;
10898 }
10899
10900 /*
10901 * Mark as PARTIAL, if appropriate. We look to the original aggref so as
10902 * to avoid printing this when recursing from the code just above.
10903 */
10904 if (DO_AGGSPLIT_SKIPFINAL(original_aggref->aggsplit))
10905 appendStringInfoString(buf, "PARTIAL ");
10906
10907 /* Extract the argument types as seen by the parser */
10908 nargs = get_aggregate_argtypes(aggref, argtypes);
10909
10910 if (!funcname)
10911 funcname = generate_function_name(aggref->aggfnoid, nargs, NIL,
10912 argtypes, aggref->aggvariadic,
10913 &use_variadic,
10914 context->inGroupBy);
10915
10916 /* Print the aggregate name, schema-qualified if needed */
10917 appendStringInfo(buf, "%s(%s", funcname,
10918 (aggref->aggdistinct != NIL) ? "DISTINCT " : "");
10919
10920 if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
10921 {
10922 /*
10923 * Ordered-set aggregates do not use "*" syntax. Also, we needn't
10924 * worry about inserting VARIADIC. So we can just dump the direct
10925 * args as-is.
10926 */
10927 Assert(!aggref->aggvariadic);
10928 get_rule_expr((Node *) aggref->aggdirectargs, context, true);
10929 Assert(aggref->aggorder != NIL);
10930 appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
10931 get_rule_orderby(aggref->aggorder, aggref->args, false, context);
10932 }
10933 else
10934 {
10935 /* aggstar can be set only in zero-argument aggregates */
10936 if (aggref->aggstar)
10938 else
10939 {
10940 ListCell *l;
10941 int i;
10942
10943 i = 0;
10944 foreach(l, aggref->args)
10945 {
10946 TargetEntry *tle = (TargetEntry *) lfirst(l);
10947 Node *arg = (Node *) tle->expr;
10948
10950 if (tle->resjunk)
10951 continue;
10952 if (i++ > 0)
10953 {
10954 if (is_json_objectagg)
10955 {
10956 /*
10957 * the ABSENT ON NULL and WITH UNIQUE args are printed
10958 * separately, so ignore them here
10959 */
10960 if (i > 2)
10961 break;
10962
10964 }
10965 else
10967 }
10968 if (use_variadic && i == nargs)
10969 appendStringInfoString(buf, "VARIADIC ");
10970 get_rule_expr(arg, context, true);
10971 }
10972 }
10973
10974 if (aggref->aggorder != NIL)
10975 {
10976 appendStringInfoString(buf, " ORDER BY ");
10977 get_rule_orderby(aggref->aggorder, aggref->args, false, context);
10978 }
10979 }
10980
10981 if (options)
10983
10984 if (aggref->aggfilter != NULL)
10985 {
10986 appendStringInfoString(buf, ") FILTER (WHERE ");
10987 get_rule_expr((Node *) aggref->aggfilter, context, false);
10988 }
10989
10991}
#define funcname
Definition: indent_codes.h:69
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:388
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:387
int get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
Definition: parse_agg.c:1997
#define FUNC_MAX_ARGS
#define linitial_node(type, l)
Definition: pg_list.h:181
static char * generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes, bool has_variadic, bool *use_variadic_p, bool inGroupBy)
Definition: ruleutils.c:13247
static void get_agg_combine_expr(Node *node, deparse_context *context, void *callback_arg)
Definition: ruleutils.c:10999
static void get_rule_orderby(List *orderList, List *targetList, bool force_colno, deparse_context *context)
Definition: ruleutils.c:6681
static void resolve_special_varno(Node *node, deparse_context *context, rsv_callback callback, void *callback_arg)
Definition: ruleutils.c:7899
Oid aggfnoid
Definition: primnodes.h:461
List * aggdistinct
Definition: primnodes.h:491
List * aggdirectargs
Definition: primnodes.h:482
List * args
Definition: primnodes.h:485
Expr * aggfilter
Definition: primnodes.h:494
List * aggorder
Definition: primnodes.h:488
Expr * expr
Definition: primnodes.h:2219

References Aggref::aggdirectargs, Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggfnoid, Aggref::aggorder, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), arg, Aggref::args, Assert(), deparse_context::buf, buf, DO_AGGSPLIT_COMBINE, DO_AGGSPLIT_SKIPFINAL, TargetEntry::expr, FUNC_MAX_ARGS, funcname, generate_function_name(), get_agg_combine_expr(), get_aggregate_argtypes(), get_rule_expr(), get_rule_orderby(), i, deparse_context::inGroupBy, IsA, lfirst, linitial_node, list_length(), NIL, and resolve_special_varno().

Referenced by get_agg_expr(), and get_json_agg_constructor().

◆ get_basic_select_query()

static void get_basic_select_query ( Query query,
deparse_context context 
)
static

Definition at line 6098 of file ruleutils.c.

6099{
6100 StringInfo buf = context->buf;
6101 RangeTblEntry *values_rte;
6102 char *sep;
6103 ListCell *l;
6104
6105 if (PRETTY_INDENT(context))
6106 {
6107 context->indentLevel += PRETTYINDENT_STD;
6109 }
6110
6111 /*
6112 * If the query looks like SELECT * FROM (VALUES ...), then print just the
6113 * VALUES part. This reverses what transformValuesClause() did at parse
6114 * time.
6115 */
6116 values_rte = get_simple_values_rte(query, context->resultDesc);
6117 if (values_rte)
6118 {
6119 get_values_def(values_rte->values_lists, context);
6120 return;
6121 }
6122
6123 /*
6124 * Build up the query string - first we say SELECT
6125 */
6126 if (query->isReturn)
6127 appendStringInfoString(buf, "RETURN");
6128 else
6129 appendStringInfoString(buf, "SELECT");
6130
6131 /* Add the DISTINCT clause if given */
6132 if (query->distinctClause != NIL)
6133 {
6134 if (query->hasDistinctOn)
6135 {
6136 appendStringInfoString(buf, " DISTINCT ON (");
6137 sep = "";
6138 foreach(l, query->distinctClause)
6139 {
6141
6144 false, context);
6145 sep = ", ";
6146 }
6148 }
6149 else
6150 appendStringInfoString(buf, " DISTINCT");
6151 }
6152
6153 /* Then we tell what to select (the targetlist) */
6154 get_target_list(query->targetList, context);
6155
6156 /* Add the FROM clause if needed */
6157 get_from_clause(query, " FROM ", context);
6158
6159 /* Add the WHERE clause if given */
6160 if (query->jointree->quals != NULL)
6161 {
6162 appendContextKeyword(context, " WHERE ",
6164 get_rule_expr(query->jointree->quals, context, false);
6165 }
6166
6167 /* Add the GROUP BY clause if given */
6168 if (query->groupClause != NULL || query->groupingSets != NULL)
6169 {
6170 bool save_ingroupby;
6171
6172 appendContextKeyword(context, " GROUP BY ",
6174 if (query->groupDistinct)
6175 appendStringInfoString(buf, "DISTINCT ");
6176
6177 save_ingroupby = context->inGroupBy;
6178 context->inGroupBy = true;
6179
6180 if (query->groupingSets == NIL)
6181 {
6182 sep = "";
6183 foreach(l, query->groupClause)
6184 {
6186
6189 false, context);
6190 sep = ", ";
6191 }
6192 }
6193 else
6194 {
6195 sep = "";
6196 foreach(l, query->groupingSets)
6197 {
6198 GroupingSet *grp = lfirst(l);
6199
6201 get_rule_groupingset(grp, query->targetList, true, context);
6202 sep = ", ";
6203 }
6204 }
6205
6206 context->inGroupBy = save_ingroupby;
6207 }
6208
6209 /* Add the HAVING clause if given */
6210 if (query->havingQual != NULL)
6211 {
6212 appendContextKeyword(context, " HAVING ",
6214 get_rule_expr(query->havingQual, context, false);
6215 }
6216
6217 /* Add the WINDOW clause if needed */
6218 if (query->windowClause != NIL)
6219 get_rule_windowclause(query, context);
6220}
static void appendContextKeyword(deparse_context *context, const char *str, int indentBefore, int indentAfter, int indentPlus)
Definition: ruleutils.c:9072
static void get_values_def(List *values_lists, deparse_context *context)
Definition: ruleutils.c:5724
static void get_from_clause(Query *query, const char *prefix, deparse_context *context)
Definition: ruleutils.c:12260
static void get_rule_groupingset(GroupingSet *gset, List *targetlist, bool omit_parens, deparse_context *context)
Definition: ruleutils.c:6621
static void get_target_list(List *targetList, deparse_context *context)
Definition: ruleutils.c:6229
static Node * get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno, deparse_context *context)
Definition: ruleutils.c:6552
static void get_rule_windowclause(Query *query, deparse_context *context)
Definition: ruleutils.c:6739
static RangeTblEntry * get_simple_values_rte(Query *query, TupleDesc resultDesc)
Definition: ruleutils.c:6029
Node * quals
Definition: primnodes.h:2338
bool groupDistinct
Definition: parsenodes.h:212
FromExpr * jointree
Definition: parsenodes.h:177
List * groupClause
Definition: parsenodes.h:211
Node * havingQual
Definition: parsenodes.h:216
List * windowClause
Definition: parsenodes.h:218
List * targetList
Definition: parsenodes.h:193
List * groupingSets
Definition: parsenodes.h:214
List * distinctClause
Definition: parsenodes.h:220
List * values_lists
Definition: parsenodes.h:1199
Index tleSortGroupRef
Definition: parsenodes.h:1447

References appendContextKeyword(), appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, Query::distinctClause, get_from_clause(), get_rule_expr(), get_rule_groupingset(), get_rule_sortgroupclause(), get_rule_windowclause(), get_simple_values_rte(), get_target_list(), get_values_def(), Query::groupClause, Query::groupDistinct, Query::groupingSets, Query::havingQual, deparse_context::indentLevel, deparse_context::inGroupBy, Query::jointree, lfirst, NIL, PRETTY_INDENT, PRETTYINDENT_STD, FromExpr::quals, deparse_context::resultDesc, Query::targetList, SortGroupClause::tleSortGroupRef, RangeTblEntry::values_lists, and Query::windowClause.

Referenced by get_select_query_def().

◆ get_coercion_expr()

static void get_coercion_expr ( Node arg,
deparse_context context,
Oid  resulttype,
int32  resulttypmod,
Node parentNode 
)
static

Definition at line 11391 of file ruleutils.c.

11394{
11395 StringInfo buf = context->buf;
11396
11397 /*
11398 * Since parse_coerce.c doesn't immediately collapse application of
11399 * length-coercion functions to constants, what we'll typically see in
11400 * such cases is a Const with typmod -1 and a length-coercion function
11401 * right above it. Avoid generating redundant output. However, beware of
11402 * suppressing casts when the user actually wrote something like
11403 * 'foo'::text::char(3).
11404 *
11405 * Note: it might seem that we are missing the possibility of needing to
11406 * print a COLLATE clause for such a Const. However, a Const could only
11407 * have nondefault collation in a post-constant-folding tree, in which the
11408 * length coercion would have been folded too. See also the special
11409 * handling of CollateExpr in coerce_to_target_type(): any collation
11410 * marking will be above the coercion node, not below it.
11411 */
11412 if (arg && IsA(arg, Const) &&
11413 ((Const *) arg)->consttype == resulttype &&
11414 ((Const *) arg)->consttypmod == -1)
11415 {
11416 /* Show the constant without normal ::typename decoration */
11417 get_const_expr((Const *) arg, context, -1);
11418 }
11419 else
11420 {
11421 if (!PRETTY_PAREN(context))
11423 get_rule_expr_paren(arg, context, false, parentNode);
11424 if (!PRETTY_PAREN(context))
11426 }
11427
11428 /*
11429 * Never emit resulttype(arg) functional notation. A pg_proc entry could
11430 * take precedence, and a resulttype in pg_temp would require schema
11431 * qualification that format_type_with_typemod() would usually omit. We've
11432 * standardized on arg::resulttype, but CAST(arg AS resulttype) notation
11433 * would work fine.
11434 */
11435 appendStringInfo(buf, "::%s",
11436 format_type_with_typemod(resulttype, resulttypmod));
11437}
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:362
static void get_rule_expr_paren(Node *node, deparse_context *context, bool showimplicit, Node *parentNode)
Definition: ruleutils.c:9145
#define PRETTY_PAREN(context)
Definition: ruleutils.c:101
static void get_const_expr(Const *constval, deparse_context *context, int showtype)
Definition: ruleutils.c:11455

References appendStringInfo(), appendStringInfoChar(), arg, deparse_context::buf, buf, format_type_with_typemod(), get_const_expr(), get_rule_expr_paren(), IsA, and PRETTY_PAREN.

Referenced by get_func_expr(), and get_rule_expr().

◆ get_column_alias_list()

static void get_column_alias_list ( deparse_columns colinfo,
deparse_context context 
)
static

Definition at line 12716 of file ruleutils.c.

12717{
12718 StringInfo buf = context->buf;
12719 int i;
12720 bool first = true;
12721
12722 /* Don't print aliases if not needed */
12723 if (!colinfo->printaliases)
12724 return;
12725
12726 for (i = 0; i < colinfo->num_new_cols; i++)
12727 {
12728 char *colname = colinfo->new_colnames[i];
12729
12730 if (first)
12731 {
12733 first = false;
12734 }
12735 else
12738 }
12739 if (!first)
12741}

References appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, i, deparse_columns::new_colnames, deparse_columns::num_new_cols, deparse_columns::printaliases, and quote_identifier().

Referenced by get_from_clause_item().

◆ get_const_collation()

static void get_const_collation ( Const constval,
deparse_context context 
)
static

Definition at line 11585 of file ruleutils.c.

11586{
11587 StringInfo buf = context->buf;
11588
11589 if (OidIsValid(constval->constcollid))
11590 {
11591 Oid typcollation = get_typcollation(constval->consttype);
11592
11593 if (constval->constcollid != typcollation)
11594 {
11595 appendStringInfo(buf, " COLLATE %s",
11596 generate_collation_name(constval->constcollid));
11597 }
11598 }
11599}
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3139
char * generate_collation_name(Oid collid)
Definition: ruleutils.c:13533
Oid consttype
Definition: primnodes.h:329

References appendStringInfo(), deparse_context::buf, buf, Const::consttype, generate_collation_name(), get_typcollation(), and OidIsValid.

Referenced by get_const_expr().

◆ get_const_expr()

static void get_const_expr ( Const constval,
deparse_context context,
int  showtype 
)
static

Definition at line 11455 of file ruleutils.c.

11456{
11457 StringInfo buf = context->buf;
11458 Oid typoutput;
11459 bool typIsVarlena;
11460 char *extval;
11461 bool needlabel = false;
11462
11463 if (constval->constisnull)
11464 {
11465 /*
11466 * Always label the type of a NULL constant to prevent misdecisions
11467 * about type when reparsing.
11468 */
11469 appendStringInfoString(buf, "NULL");
11470 if (showtype >= 0)
11471 {
11472 appendStringInfo(buf, "::%s",
11474 constval->consttypmod));
11475 get_const_collation(constval, context);
11476 }
11477 return;
11478 }
11479
11480 getTypeOutputInfo(constval->consttype,
11481 &typoutput, &typIsVarlena);
11482
11483 extval = OidOutputFunctionCall(typoutput, constval->constvalue);
11484
11485 switch (constval->consttype)
11486 {
11487 case INT4OID:
11488
11489 /*
11490 * INT4 can be printed without any decoration, unless it is
11491 * negative; in that case print it as '-nnn'::integer to ensure
11492 * that the output will re-parse as a constant, not as a constant
11493 * plus operator. In most cases we could get away with printing
11494 * (-nnn) instead, because of the way that gram.y handles negative
11495 * literals; but that doesn't work for INT_MIN, and it doesn't
11496 * seem that much prettier anyway.
11497 */
11498 if (extval[0] != '-')
11499 appendStringInfoString(buf, extval);
11500 else
11501 {
11502 appendStringInfo(buf, "'%s'", extval);
11503 needlabel = true; /* we must attach a cast */
11504 }
11505 break;
11506
11507 case NUMERICOID:
11508
11509 /*
11510 * NUMERIC can be printed without quotes if it looks like a float
11511 * constant (not an integer, and not Infinity or NaN) and doesn't
11512 * have a leading sign (for the same reason as for INT4).
11513 */
11514 if (isdigit((unsigned char) extval[0]) &&
11515 strcspn(extval, "eE.") != strlen(extval))
11516 {
11517 appendStringInfoString(buf, extval);
11518 }
11519 else
11520 {
11521 appendStringInfo(buf, "'%s'", extval);
11522 needlabel = true; /* we must attach a cast */
11523 }
11524 break;
11525
11526 case BOOLOID:
11527 if (strcmp(extval, "t") == 0)
11528 appendStringInfoString(buf, "true");
11529 else
11530 appendStringInfoString(buf, "false");
11531 break;
11532
11533 default:
11534 simple_quote_literal(buf, extval);
11535 break;
11536 }
11537
11538 pfree(extval);
11539
11540 if (showtype < 0)
11541 return;
11542
11543 /*
11544 * For showtype == 0, append ::typename unless the constant will be
11545 * implicitly typed as the right type when it is read in.
11546 *
11547 * XXX this code has to be kept in sync with the behavior of the parser,
11548 * especially make_const.
11549 */
11550 switch (constval->consttype)
11551 {
11552 case BOOLOID:
11553 case UNKNOWNOID:
11554 /* These types can be left unlabeled */
11555 needlabel = false;
11556 break;
11557 case INT4OID:
11558 /* We determined above whether a label is needed */
11559 break;
11560 case NUMERICOID:
11561
11562 /*
11563 * Float-looking constants will be typed as numeric, which we
11564 * checked above; but if there's a nondefault typmod we need to
11565 * show it.
11566 */
11567 needlabel |= (constval->consttypmod >= 0);
11568 break;
11569 default:
11570 needlabel = true;
11571 break;
11572 }
11573 if (needlabel || showtype > 0)
11574 appendStringInfo(buf, "::%s",
11576 constval->consttypmod));
11577
11578 get_const_collation(constval, context);
11579}
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2990
void pfree(void *pointer)
Definition: mcxt.c:1524
static void simple_quote_literal(StringInfo buf, const char *val)
Definition: ruleutils.c:11783
static void get_const_collation(Const *constval, deparse_context *context)
Definition: ruleutils.c:11585

References appendStringInfo(), appendStringInfoString(), deparse_context::buf, buf, Const::consttype, format_type_with_typemod(), get_const_collation(), getTypeOutputInfo(), OidOutputFunctionCall(), pfree(), and simple_quote_literal().

Referenced by get_coercion_expr(), get_json_path_spec(), get_json_table(), get_json_table_nested_columns(), get_range_partbound_string(), get_rule_expr(), and get_rule_sortgroupclause().

◆ get_delete_query_def()

static void get_delete_query_def ( Query query,
deparse_context context 
)
static

Definition at line 7345 of file ruleutils.c.

7346{
7347 StringInfo buf = context->buf;
7348 RangeTblEntry *rte;
7349
7350 /* Insert the WITH clause if given */
7351 get_with_clause(query, context);
7352
7353 /*
7354 * Start the query with DELETE FROM relname
7355 */
7356 rte = rt_fetch(query->resultRelation, query->rtable);
7357 Assert(rte->rtekind == RTE_RELATION);
7358 if (PRETTY_INDENT(context))
7359 {
7361 context->indentLevel += PRETTYINDENT_STD;
7362 }
7363 appendStringInfo(buf, "DELETE FROM %s%s",
7364 only_marker(rte),
7366
7367 /* Print the relation alias, if needed */
7368 get_rte_alias(rte, query->resultRelation, false, context);
7369
7370 /* Add the USING clause if given */
7371 get_from_clause(query, " USING ", context);
7372
7373 /* Add a WHERE clause if given */
7374 if (query->jointree->quals != NULL)
7375 {
7376 appendContextKeyword(context, " WHERE ",
7378 get_rule_expr(query->jointree->quals, context, false);
7379 }
7380
7381 /* Add RETURNING if present */
7382 if (query->returningList)
7383 get_returning_clause(query, context);
7384}
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static void get_rte_alias(RangeTblEntry *rte, int varno, bool use_as, deparse_context *context)
Definition: ruleutils.c:12645
#define only_marker(rte)
Definition: ruleutils.c:550
static void get_returning_clause(Query *query, deparse_context *context)
Definition: ruleutils.c:6365
static void get_with_clause(Query *query, deparse_context *context)
Definition: ruleutils.c:5767
static char * generate_relation_name(Oid relid, List *namespaces)
Definition: ruleutils.c:13143
List * returningList
Definition: parsenodes.h:209
List * rtable
Definition: parsenodes.h:170

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), Assert(), deparse_context::buf, buf, generate_relation_name(), get_from_clause(), get_returning_clause(), get_rte_alias(), get_rule_expr(), get_with_clause(), deparse_context::indentLevel, Query::jointree, NIL, only_marker, PRETTY_INDENT, PRETTYINDENT_STD, FromExpr::quals, RangeTblEntry::relid, Query::returningList, rt_fetch, Query::rtable, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by get_query_def().

◆ get_from_clause()

static void get_from_clause ( Query query,
const char *  prefix,
deparse_context context 
)
static

Definition at line 12260 of file ruleutils.c.

12261{
12262 StringInfo buf = context->buf;
12263 bool first = true;
12264 ListCell *l;
12265
12266 /*
12267 * We use the query's jointree as a guide to what to print. However, we
12268 * must ignore auto-added RTEs that are marked not inFromCl. (These can
12269 * only appear at the top level of the jointree, so it's sufficient to
12270 * check here.) This check also ensures we ignore the rule pseudo-RTEs
12271 * for NEW and OLD.
12272 */
12273 foreach(l, query->jointree->fromlist)
12274 {
12275 Node *jtnode = (Node *) lfirst(l);
12276
12277 if (IsA(jtnode, RangeTblRef))
12278 {
12279 int varno = ((RangeTblRef *) jtnode)->rtindex;
12280 RangeTblEntry *rte = rt_fetch(varno, query->rtable);
12281
12282 if (!rte->inFromCl)
12283 continue;
12284 }
12285
12286 if (first)
12287 {
12288 appendContextKeyword(context, prefix,
12290 first = false;
12291
12292 get_from_clause_item(jtnode, query, context);
12293 }
12294 else
12295 {
12296 StringInfoData itembuf;
12297
12299
12300 /*
12301 * Put the new FROM item's text into itembuf so we can decide
12302 * after we've got it whether or not it needs to go on a new line.
12303 */
12304 initStringInfo(&itembuf);
12305 context->buf = &itembuf;
12306
12307 get_from_clause_item(jtnode, query, context);
12308
12309 /* Restore context's output buffer */
12310 context->buf = buf;
12311
12312 /* Consider line-wrapping if enabled */
12313 if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
12314 {
12315 /* Does the new item start with a new line? */
12316 if (itembuf.len > 0 && itembuf.data[0] == '\n')
12317 {
12318 /* If so, we shouldn't add anything */
12319 /* instead, remove any trailing spaces currently in buf */
12321 }
12322 else
12323 {
12324 char *trailing_nl;
12325
12326 /* Locate the start of the current line in the buffer */
12327 trailing_nl = strrchr(buf->data, '\n');
12328 if (trailing_nl == NULL)
12329 trailing_nl = buf->data;
12330 else
12331 trailing_nl++;
12332
12333 /*
12334 * Add a newline, plus some indentation, if the new item
12335 * would cause an overflow.
12336 */
12337 if (strlen(trailing_nl) + itembuf.len > context->wrapColumn)
12341 }
12342 }
12343
12344 /* Add the new item */
12345 appendBinaryStringInfo(buf, itembuf.data, itembuf.len);
12346
12347 /* clean up */
12348 pfree(itembuf.data);
12349 }
12350 }
12351}
#define PRETTYINDENT_VAR
Definition: ruleutils.c:83
static void get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
Definition: ruleutils.c:12354
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:281
List * fromlist
Definition: primnodes.h:2337

References appendBinaryStringInfo(), appendContextKeyword(), appendStringInfoString(), deparse_context::buf, buf, StringInfoData::data, FromExpr::fromlist, get_from_clause_item(), initStringInfo(), IsA, Query::jointree, StringInfoData::len, lfirst, pfree(), PRETTY_INDENT, PRETTYINDENT_STD, PRETTYINDENT_VAR, removeStringInfoSpaces(), rt_fetch, Query::rtable, and deparse_context::wrapColumn.

Referenced by get_basic_select_query(), get_delete_query_def(), get_merge_query_def(), and get_update_query_def().

◆ get_from_clause_coldeflist()

static void get_from_clause_coldeflist ( RangeTblFunction rtfunc,
deparse_columns colinfo,
deparse_context context 
)
static

Definition at line 12756 of file ruleutils.c.

12759{
12760 StringInfo buf = context->buf;
12761 ListCell *l1;
12762 ListCell *l2;
12763 ListCell *l3;
12764 ListCell *l4;
12765 int i;
12766
12768
12769 i = 0;
12770 forfour(l1, rtfunc->funccoltypes,
12771 l2, rtfunc->funccoltypmods,
12772 l3, rtfunc->funccolcollations,
12773 l4, rtfunc->funccolnames)
12774 {
12775 Oid atttypid = lfirst_oid(l1);
12776 int32 atttypmod = lfirst_int(l2);
12777 Oid attcollation = lfirst_oid(l3);
12778 char *attname;
12779
12780 if (colinfo)
12781 attname = colinfo->colnames[i];
12782 else
12783 attname = strVal(lfirst(l4));
12784
12785 Assert(attname); /* shouldn't be any dropped columns here */
12786
12787 if (i > 0)
12789 appendStringInfo(buf, "%s %s",
12791 format_type_with_typemod(atttypid, atttypmod));
12792 if (OidIsValid(attcollation) &&
12793 attcollation != get_typcollation(atttypid))
12794 appendStringInfo(buf, " COLLATE %s",
12795 generate_collation_name(attcollation));
12796
12797 i++;
12798 }
12799
12801}
int32_t int32
Definition: c.h:498
NameData attname
Definition: pg_attribute.h:41
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
Definition: pg_list.h:575
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define strVal(v)
Definition: value.h:82

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), attname, deparse_context::buf, buf, deparse_columns::colnames, forfour, format_type_with_typemod(), generate_collation_name(), get_typcollation(), i, lfirst, lfirst_int, lfirst_oid, OidIsValid, quote_identifier(), and strVal.

Referenced by get_from_clause_item().

◆ get_from_clause_item()

static void get_from_clause_item ( Node jtnode,
Query query,
deparse_context context 
)
static

Definition at line 12354 of file ruleutils.c.

12355{
12356 StringInfo buf = context->buf;
12358
12359 if (IsA(jtnode, RangeTblRef))
12360 {
12361 int varno = ((RangeTblRef *) jtnode)->rtindex;
12362 RangeTblEntry *rte = rt_fetch(varno, query->rtable);
12363 deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
12364 RangeTblFunction *rtfunc1 = NULL;
12365
12366 if (rte->lateral)
12367 appendStringInfoString(buf, "LATERAL ");
12368
12369 /* Print the FROM item proper */
12370 switch (rte->rtekind)
12371 {
12372 case RTE_RELATION:
12373 /* Normal relation RTE */
12374 appendStringInfo(buf, "%s%s",
12375 only_marker(rte),
12377 context->namespaces));
12378 break;
12379 case RTE_SUBQUERY:
12380 /* Subquery RTE */
12382 get_query_def(rte->subquery, buf, context->namespaces, NULL,
12383 true,
12384 context->prettyFlags, context->wrapColumn,
12385 context->indentLevel);
12387 break;
12388 case RTE_FUNCTION:
12389 /* Function RTE */
12390 rtfunc1 = (RangeTblFunction *) linitial(rte->functions);
12391
12392 /*
12393 * Omit ROWS FROM() syntax for just one function, unless it
12394 * has both a coldeflist and WITH ORDINALITY. If it has both,
12395 * we must use ROWS FROM() syntax to avoid ambiguity about
12396 * whether the coldeflist includes the ordinality column.
12397 */
12398 if (list_length(rte->functions) == 1 &&
12399 (rtfunc1->funccolnames == NIL || !rte->funcordinality))
12400 {
12401 get_rule_expr_funccall(rtfunc1->funcexpr, context, true);
12402 /* we'll print the coldeflist below, if it has one */
12403 }
12404 else
12405 {
12406 bool all_unnest;
12407 ListCell *lc;
12408
12409 /*
12410 * If all the function calls in the list are to unnest,
12411 * and none need a coldeflist, then collapse the list back
12412 * down to UNNEST(args). (If we had more than one
12413 * built-in unnest function, this would get more
12414 * difficult.)
12415 *
12416 * XXX This is pretty ugly, since it makes not-terribly-
12417 * future-proof assumptions about what the parser would do
12418 * with the output; but the alternative is to emit our
12419 * nonstandard ROWS FROM() notation for what might have
12420 * been a perfectly spec-compliant multi-argument
12421 * UNNEST().
12422 */
12423 all_unnest = true;
12424 foreach(lc, rte->functions)
12425 {
12426 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
12427
12428 if (!IsA(rtfunc->funcexpr, FuncExpr) ||
12429 ((FuncExpr *) rtfunc->funcexpr)->funcid != F_UNNEST_ANYARRAY ||
12430 rtfunc->funccolnames != NIL)
12431 {
12432 all_unnest = false;
12433 break;
12434 }
12435 }
12436
12437 if (all_unnest)
12438 {
12439 List *allargs = NIL;
12440
12441 foreach(lc, rte->functions)
12442 {
12443 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
12444 List *args = ((FuncExpr *) rtfunc->funcexpr)->args;
12445
12446 allargs = list_concat(allargs, args);
12447 }
12448
12449 appendStringInfoString(buf, "UNNEST(");
12450 get_rule_expr((Node *) allargs, context, true);
12452 }
12453 else
12454 {
12455 int funcno = 0;
12456
12457 appendStringInfoString(buf, "ROWS FROM(");
12458 foreach(lc, rte->functions)
12459 {
12460 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
12461
12462 if (funcno > 0)
12464 get_rule_expr_funccall(rtfunc->funcexpr, context, true);
12465 if (rtfunc->funccolnames != NIL)
12466 {
12467 /* Reconstruct the column definition list */
12468 appendStringInfoString(buf, " AS ");
12470 NULL,
12471 context);
12472 }
12473 funcno++;
12474 }
12476 }
12477 /* prevent printing duplicate coldeflist below */
12478 rtfunc1 = NULL;
12479 }
12480 if (rte->funcordinality)
12481 appendStringInfoString(buf, " WITH ORDINALITY");
12482 break;
12483 case RTE_TABLEFUNC:
12484 get_tablefunc(rte->tablefunc, context, true);
12485 break;
12486 case RTE_VALUES:
12487 /* Values list RTE */
12489 get_values_def(rte->values_lists, context);
12491 break;
12492 case RTE_CTE:
12494 break;
12495 default:
12496 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
12497 break;
12498 }
12499
12500 /* Print the relation alias, if needed */
12501 get_rte_alias(rte, varno, false, context);
12502
12503 /* Print the column definitions or aliases, if needed */
12504 if (rtfunc1 && rtfunc1->funccolnames != NIL)
12505 {
12506 /* Reconstruct the columndef list, which is also the aliases */
12507 get_from_clause_coldeflist(rtfunc1, colinfo, context);
12508 }
12509 else
12510 {
12511 /* Else print column aliases as needed */
12512 get_column_alias_list(colinfo, context);
12513 }
12514
12515 /* Tablesample clause must go after any alias */
12516 if (rte->rtekind == RTE_RELATION && rte->tablesample)
12517 get_tablesample_def(rte->tablesample, context);
12518 }
12519 else if (IsA(jtnode, JoinExpr))
12520 {
12521 JoinExpr *j = (JoinExpr *) jtnode;
12522 deparse_columns *colinfo = deparse_columns_fetch(j->rtindex, dpns);
12523 bool need_paren_on_right;
12524
12525 need_paren_on_right = PRETTY_PAREN(context) &&
12526 !IsA(j->rarg, RangeTblRef) &&
12527 !(IsA(j->rarg, JoinExpr) && ((JoinExpr *) j->rarg)->alias != NULL);
12528
12529 if (!PRETTY_PAREN(context) || j->alias != NULL)
12531
12532 get_from_clause_item(j->larg, query, context);
12533
12534 switch (j->jointype)
12535 {
12536 case JOIN_INNER:
12537 if (j->quals)
12538 appendContextKeyword(context, " JOIN ",
12542 else
12543 appendContextKeyword(context, " CROSS JOIN ",
12547 break;
12548 case JOIN_LEFT:
12549 appendContextKeyword(context, " LEFT JOIN ",
12553 break;
12554 case JOIN_FULL:
12555 appendContextKeyword(context, " FULL JOIN ",
12559 break;
12560 case JOIN_RIGHT:
12561 appendContextKeyword(context, " RIGHT JOIN ",
12565 break;
12566 default:
12567 elog(ERROR, "unrecognized join type: %d",
12568 (int) j->jointype);
12569 }
12570
12571 if (need_paren_on_right)
12573 get_from_clause_item(j->rarg, query, context);
12574 if (need_paren_on_right)
12576
12577 if (j->usingClause)
12578 {
12579 ListCell *lc;
12580 bool first = true;
12581
12582 appendStringInfoString(buf, " USING (");
12583 /* Use the assigned names, not what's in usingClause */
12584 foreach(lc, colinfo->usingNames)
12585 {
12586 char *colname = (char *) lfirst(lc);
12587
12588 if (first)
12589 first = false;
12590 else
12593 }
12595
12596 if (j->join_using_alias)
12597 appendStringInfo(buf, " AS %s",
12598 quote_identifier(j->join_using_alias->aliasname));
12599 }
12600 else if (j->quals)
12601 {
12602 appendStringInfoString(buf, " ON ");
12603 if (!PRETTY_PAREN(context))
12605 get_rule_expr(j->quals, context, false);
12606 if (!PRETTY_PAREN(context))
12608 }
12609 else if (j->jointype != JOIN_INNER)
12610 {
12611 /* If we didn't say CROSS JOIN above, we must provide an ON */
12612 appendStringInfoString(buf, " ON TRUE");
12613 }
12614
12615 if (!PRETTY_PAREN(context) || j->alias != NULL)
12617
12618 /* Yes, it's correct to put alias after the right paren ... */
12619 if (j->alias != NULL)
12620 {
12621 /*
12622 * Note that it's correct to emit an alias clause if and only if
12623 * there was one originally. Otherwise we'd be converting a named
12624 * join to unnamed or vice versa, which creates semantic
12625 * subtleties we don't want. However, we might print a different
12626 * alias name than was there originally.
12627 */
12628 appendStringInfo(buf, " %s",
12630 context)));
12631 get_column_alias_list(colinfo, context);
12632 }
12633 }
12634 else
12635 elog(ERROR, "unrecognized node type: %d",
12636 (int) nodeTag(jtnode));
12637}
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define nodeTag(nodeptr)
Definition: nodes.h:135
@ JOIN_FULL
Definition: nodes.h:297
@ JOIN_INNER
Definition: nodes.h:295
@ JOIN_RIGHT
Definition: nodes.h:298
@ JOIN_LEFT
Definition: nodes.h:296
@ RTE_CTE
Definition: parsenodes.h:1032
@ RTE_VALUES
Definition: parsenodes.h:1031
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
@ RTE_FUNCTION
Definition: parsenodes.h:1029
@ RTE_TABLEFUNC
Definition: parsenodes.h:1030
static char * get_rtable_name(int rtindex, deparse_context *context)
Definition: ruleutils.c:5133
#define PRETTYINDENT_JOIN
Definition: ruleutils.c:82
static void get_query_def(Query *query, StringInfo buf, List *parentnamespace, TupleDesc resultDesc, bool colNamesVisible, int prettyFlags, int wrapColumn, int startIndent)
Definition: ruleutils.c:5624
static void get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
Definition: ruleutils.c:12807
static void get_column_alias_list(deparse_columns *colinfo, deparse_context *context)
Definition: ruleutils.c:12716
static void get_from_clause_coldeflist(RangeTblFunction *rtfunc, deparse_columns *colinfo, deparse_context *context)
Definition: ruleutils.c:12756
static void get_rule_expr_funccall(Node *node, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:10673
#define deparse_columns_fetch(rangetable_index, dpns)
Definition: ruleutils.c:312
static void get_tablefunc(TableFunc *tf, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:12241
Definition: pg_list.h:54
char * ctename
Definition: parsenodes.h:1205
TableFunc * tablefunc
Definition: parsenodes.h:1193
bool funcordinality
Definition: parsenodes.h:1188
struct TableSampleClause * tablesample
Definition: parsenodes.h:1107
Query * subquery
Definition: parsenodes.h:1113
List * functions
Definition: parsenodes.h:1186
List * usingNames
Definition: ruleutils.c:299

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), generate_unaccent_rules::args, deparse_context::buf, buf, RangeTblEntry::ctename, deparse_columns_fetch, elog, ERROR, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, generate_relation_name(), get_column_alias_list(), get_from_clause_coldeflist(), get_from_clause_item(), get_query_def(), get_rtable_name(), get_rte_alias(), get_rule_expr(), get_rule_expr_funccall(), get_tablefunc(), get_tablesample_def(), get_values_def(), deparse_context::indentLevel, IsA, j, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, lfirst, linitial, list_concat(), list_length(), deparse_context::namespaces, NIL, nodeTag, only_marker, PRETTY_PAREN, deparse_context::prettyFlags, PRETTYINDENT_JOIN, PRETTYINDENT_STD, quote_identifier(), RangeTblEntry::relid, rt_fetch, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::subquery, RangeTblEntry::tablefunc, RangeTblEntry::tablesample, deparse_columns::usingNames, RangeTblEntry::values_lists, and deparse_context::wrapColumn.

Referenced by get_from_clause(), and get_from_clause_item().

◆ get_func_expr()

static void get_func_expr ( FuncExpr expr,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 10765 of file ruleutils.c.

10767{
10768 StringInfo buf = context->buf;
10769 Oid funcoid = expr->funcid;
10770 Oid argtypes[FUNC_MAX_ARGS];
10771 int nargs;
10772 List *argnames;
10773 bool use_variadic;
10774 ListCell *l;
10775
10776 /*
10777 * If the function call came from an implicit coercion, then just show the
10778 * first argument --- unless caller wants to see implicit coercions.
10779 */
10780 if (expr->funcformat == COERCE_IMPLICIT_CAST && !showimplicit)
10781 {
10782 get_rule_expr_paren((Node *) linitial(expr->args), context,
10783 false, (Node *) expr);
10784 return;
10785 }
10786
10787 /*
10788 * If the function call came from a cast, then show the first argument
10789 * plus an explicit cast operation.
10790 */
10791 if (expr->funcformat == COERCE_EXPLICIT_CAST ||
10792 expr->funcformat == COERCE_IMPLICIT_CAST)
10793 {
10794 Node *arg = linitial(expr->args);
10795 Oid rettype = expr->funcresulttype;
10796 int32 coercedTypmod;
10797
10798 /* Get the typmod if this is a length-coercion function */
10799 (void) exprIsLengthCoercion((Node *) expr, &coercedTypmod);
10800
10801 get_coercion_expr(arg, context,
10802 rettype, coercedTypmod,
10803 (Node *) expr);
10804
10805 return;
10806 }
10807
10808 /*
10809 * If the function was called using one of the SQL spec's random special
10810 * syntaxes, try to reproduce that. If we don't recognize the function,
10811 * fall through.
10812 */
10813 if (expr->funcformat == COERCE_SQL_SYNTAX)
10814 {
10815 if (get_func_sql_syntax(expr, context))
10816 return;
10817 }
10818
10819 /*
10820 * Normal function: display as proname(args). First we need to extract
10821 * the argument datatypes.
10822 */
10823 if (list_length(expr->args) > FUNC_MAX_ARGS)
10824 ereport(ERROR,
10825 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
10826 errmsg("too many arguments")));
10827 nargs = 0;
10828 argnames = NIL;
10829 foreach(l, expr->args)
10830 {
10831 Node *arg = (Node *) lfirst(l);
10832
10833 if (IsA(arg, NamedArgExpr))
10834 argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
10835 argtypes[nargs] = exprType(arg);
10836 nargs++;
10837 }
10838
10839 appendStringInfo(buf, "%s(",
10840 generate_function_name(funcoid, nargs,
10841 argnames, argtypes,
10842 expr->funcvariadic,
10843 &use_variadic,
10844 context->inGroupBy));
10845 nargs = 0;
10846 foreach(l, expr->args)
10847 {
10848 if (nargs++ > 0)
10850 if (use_variadic && lnext(expr->args, l) == NULL)
10851 appendStringInfoString(buf, "VARIADIC ");
10852 get_rule_expr((Node *) lfirst(l), context, true);
10853 }
10855}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
List * lappend(List *list, void *datum)
Definition: list.c:339
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:557
@ COERCE_SQL_SYNTAX
Definition: primnodes.h:754
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:753
@ COERCE_EXPLICIT_CAST
Definition: primnodes.h:752
static bool get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
Definition: ruleutils.c:11139
static void get_coercion_expr(Node *arg, deparse_context *context, Oid resulttype, int32 resulttypmod, Node *parentNode)
Definition: ruleutils.c:11391
Oid funcid
Definition: primnodes.h:767
List * args
Definition: primnodes.h:785

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), arg, FuncExpr::args, deparse_context::buf, buf, COERCE_EXPLICIT_CAST, COERCE_IMPLICIT_CAST, COERCE_SQL_SYNTAX, ereport, errcode(), errmsg(), ERROR, exprIsLengthCoercion(), exprType(), FUNC_MAX_ARGS, FuncExpr::funcid, generate_function_name(), get_coercion_expr(), get_func_sql_syntax(), get_rule_expr(), get_rule_expr_paren(), deparse_context::inGroupBy, IsA, lappend(), lfirst, linitial, list_length(), lnext(), and NIL.

Referenced by get_rule_expr().

◆ get_func_sql_syntax()

static bool get_func_sql_syntax ( FuncExpr expr,
deparse_context context 
)
static

Definition at line 11139 of file ruleutils.c.

11140{
11141 StringInfo buf = context->buf;
11142 Oid funcoid = expr->funcid;
11143
11144 switch (funcoid)
11145 {
11146 case F_TIMEZONE_INTERVAL_TIMESTAMP:
11147 case F_TIMEZONE_INTERVAL_TIMESTAMPTZ:
11148 case F_TIMEZONE_INTERVAL_TIMETZ:
11149 case F_TIMEZONE_TEXT_TIMESTAMP:
11150 case F_TIMEZONE_TEXT_TIMESTAMPTZ:
11151 case F_TIMEZONE_TEXT_TIMETZ:
11152 /* AT TIME ZONE ... note reversed argument order */
11154 get_rule_expr_paren((Node *) lsecond(expr->args), context, false,
11155 (Node *) expr);
11156 appendStringInfoString(buf, " AT TIME ZONE ");
11157 get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11158 (Node *) expr);
11160 return true;
11161
11162 case F_TIMEZONE_TIMESTAMP:
11163 case F_TIMEZONE_TIMESTAMPTZ:
11164 case F_TIMEZONE_TIMETZ:
11165 /* AT LOCAL */
11167 get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11168 (Node *) expr);
11169 appendStringInfoString(buf, " AT LOCAL)");
11170 return true;
11171
11172 case F_OVERLAPS_TIMESTAMPTZ_INTERVAL_TIMESTAMPTZ_INTERVAL:
11173 case F_OVERLAPS_TIMESTAMPTZ_INTERVAL_TIMESTAMPTZ_TIMESTAMPTZ:
11174 case F_OVERLAPS_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ_INTERVAL:
11175 case F_OVERLAPS_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ_TIMESTAMPTZ:
11176 case F_OVERLAPS_TIMESTAMP_INTERVAL_TIMESTAMP_INTERVAL:
11177 case F_OVERLAPS_TIMESTAMP_INTERVAL_TIMESTAMP_TIMESTAMP:
11178 case F_OVERLAPS_TIMESTAMP_TIMESTAMP_TIMESTAMP_INTERVAL:
11179 case F_OVERLAPS_TIMESTAMP_TIMESTAMP_TIMESTAMP_TIMESTAMP:
11180 case F_OVERLAPS_TIMETZ_TIMETZ_TIMETZ_TIMETZ:
11181 case F_OVERLAPS_TIME_INTERVAL_TIME_INTERVAL:
11182 case F_OVERLAPS_TIME_INTERVAL_TIME_TIME:
11183 case F_OVERLAPS_TIME_TIME_TIME_INTERVAL:
11184 case F_OVERLAPS_TIME_TIME_TIME_TIME:
11185 /* (x1, x2) OVERLAPS (y1, y2) */
11187 get_rule_expr((Node *) linitial(expr->args), context, false);
11189 get_rule_expr((Node *) lsecond(expr->args), context, false);
11190 appendStringInfoString(buf, ") OVERLAPS (");
11191 get_rule_expr((Node *) lthird(expr->args), context, false);
11193 get_rule_expr((Node *) lfourth(expr->args), context, false);
11195 return true;
11196
11197 case F_EXTRACT_TEXT_DATE:
11198 case F_EXTRACT_TEXT_TIME:
11199 case F_EXTRACT_TEXT_TIMETZ:
11200 case F_EXTRACT_TEXT_TIMESTAMP:
11201 case F_EXTRACT_TEXT_TIMESTAMPTZ:
11202 case F_EXTRACT_TEXT_INTERVAL:
11203 /* EXTRACT (x FROM y) */
11204 appendStringInfoString(buf, "EXTRACT(");
11205 {
11206 Const *con = (Const *) linitial(expr->args);
11207
11208 Assert(IsA(con, Const) &&
11209 con->consttype == TEXTOID &&
11210 !con->constisnull);
11212 }
11213 appendStringInfoString(buf, " FROM ");
11214 get_rule_expr((Node *) lsecond(expr->args), context, false);
11216 return true;
11217
11218 case F_IS_NORMALIZED:
11219 /* IS xxx NORMALIZED */
11221 get_rule_expr_paren((Node *) linitial(expr->args), context, false,
11222 (Node *) expr);
11224 if (list_length(expr->args) == 2)
11225 {
11226 Const *con = (Const *) lsecond(expr->args);
11227
11228 Assert(IsA(con, Const) &&
11229 con->consttype == TEXTOID &&
11230 !con->constisnull);
11231 appendStringInfo(buf, " %s",
11232 TextDatumGetCString(con->constvalue));
11233 }
11234 appendStringInfoString(buf, " NORMALIZED)");
11235 return true;
11236
11237 case F_PG_COLLATION_FOR:
11238 /* COLLATION FOR */
11239 appendStringInfoString(buf, "COLLATION FOR (");
11240 get_rule_expr((Node *) linitial(expr->args), context, false);
11242 return true;
11243
11244 case F_NORMALIZE:
11245 /* NORMALIZE() */
11246 appendStringInfoString(buf, "NORMALIZE(");
11247 get_rule_expr((Node *) linitial(expr->args), context, false);
11248 if (list_length(expr->args) == 2)
11249 {
11250 Const *con = (Const *) lsecond(expr->args);
11251
11252 Assert(IsA(con, Const) &&
11253 con->consttype == TEXTOID &&
11254 !con->constisnull);
11255 appendStringInfo(buf, ", %s",
11256 TextDatumGetCString(con->constvalue));
11257 }
11259 return true;
11260
11261 case F_OVERLAY_BIT_BIT_INT4:
11262 case F_OVERLAY_BIT_BIT_INT4_INT4:
11263 case F_OVERLAY_BYTEA_BYTEA_INT4:
11264 case F_OVERLAY_BYTEA_BYTEA_INT4_INT4:
11265 case F_OVERLAY_TEXT_TEXT_INT4:
11266 case F_OVERLAY_TEXT_TEXT_INT4_INT4:
11267 /* OVERLAY() */
11268 appendStringInfoString(buf, "OVERLAY(");
11269 get_rule_expr((Node *) linitial(expr->args), context, false);
11270 appendStringInfoString(buf, " PLACING ");
11271 get_rule_expr((Node *) lsecond(expr->args), context, false);
11272 appendStringInfoString(buf, " FROM ");
11273 get_rule_expr((Node *) lthird(expr->args), context, false);
11274 if (list_length(expr->args) == 4)
11275 {
11276 appendStringInfoString(buf, " FOR ");
11277 get_rule_expr((Node *) lfourth(expr->args), context, false);
11278 }
11280 return true;
11281
11282 case F_POSITION_BIT_BIT:
11283 case F_POSITION_BYTEA_BYTEA:
11284 case F_POSITION_TEXT_TEXT:
11285 /* POSITION() ... extra parens since args are b_expr not a_expr */
11286 appendStringInfoString(buf, "POSITION((");
11287 get_rule_expr((Node *) lsecond(expr->args), context, false);
11288 appendStringInfoString(buf, ") IN (");
11289 get_rule_expr((Node *) linitial(expr->args), context, false);
11291 return true;
11292
11293 case F_SUBSTRING_BIT_INT4:
11294 case F_SUBSTRING_BIT_INT4_INT4:
11295 case F_SUBSTRING_BYTEA_INT4:
11296 case F_SUBSTRING_BYTEA_INT4_INT4:
11297 case F_SUBSTRING_TEXT_INT4:
11298 case F_SUBSTRING_TEXT_INT4_INT4:
11299 /* SUBSTRING FROM/FOR (i.e., integer-position variants) */
11300 appendStringInfoString(buf, "SUBSTRING(");
11301 get_rule_expr((Node *) linitial(expr->args), context, false);
11302 appendStringInfoString(buf, " FROM ");
11303 get_rule_expr((Node *) lsecond(expr->args), context, false);
11304 if (list_length(expr->args) == 3)
11305 {
11306 appendStringInfoString(buf, " FOR ");
11307 get_rule_expr((Node *) lthird(expr->args), context, false);
11308 }
11310 return true;
11311
11312 case F_SUBSTRING_TEXT_TEXT_TEXT:
11313 /* SUBSTRING SIMILAR/ESCAPE */
11314 appendStringInfoString(buf, "SUBSTRING(");
11315 get_rule_expr((Node *) linitial(expr->args), context, false);
11316 appendStringInfoString(buf, " SIMILAR ");
11317 get_rule_expr((Node *) lsecond(expr->args), context, false);
11318 appendStringInfoString(buf, " ESCAPE ");
11319 get_rule_expr((Node *) lthird(expr->args), context, false);
11321 return true;
11322
11323 case F_BTRIM_BYTEA_BYTEA:
11324 case F_BTRIM_TEXT:
11325 case F_BTRIM_TEXT_TEXT:
11326 /* TRIM() */
11327 appendStringInfoString(buf, "TRIM(BOTH");
11328 if (list_length(expr->args) == 2)
11329 {
11331 get_rule_expr((Node *) lsecond(expr->args), context, false);
11332 }
11333 appendStringInfoString(buf, " FROM ");
11334 get_rule_expr((Node *) linitial(expr->args), context, false);
11336 return true;
11337
11338 case F_LTRIM_BYTEA_BYTEA:
11339 case F_LTRIM_TEXT:
11340 case F_LTRIM_TEXT_TEXT:
11341 /* TRIM() */
11342 appendStringInfoString(buf, "TRIM(LEADING");
11343 if (list_length(expr->args) == 2)
11344 {
11346 get_rule_expr((Node *) lsecond(expr->args), context, false);
11347 }
11348 appendStringInfoString(buf, " FROM ");
11349 get_rule_expr((Node *) linitial(expr->args), context, false);
11351 return true;
11352
11353 case F_RTRIM_BYTEA_BYTEA:
11354 case F_RTRIM_TEXT:
11355 case F_RTRIM_TEXT_TEXT:
11356 /* TRIM() */
11357 appendStringInfoString(buf, "TRIM(TRAILING");
11358 if (list_length(expr->args) == 2)
11359 {
11361 get_rule_expr((Node *) lsecond(expr->args), context, false);
11362 }
11363 appendStringInfoString(buf, " FROM ");
11364 get_rule_expr((Node *) linitial(expr->args), context, false);
11366 return true;
11367
11368 case F_SYSTEM_USER:
11369 appendStringInfoString(buf, "SYSTEM_USER");
11370 return true;
11371
11372 case F_XMLEXISTS:
11373 /* XMLEXISTS ... extra parens because args are c_expr */
11374 appendStringInfoString(buf, "XMLEXISTS((");
11375 get_rule_expr((Node *) linitial(expr->args), context, false);
11376 appendStringInfoString(buf, ") PASSING (");
11377 get_rule_expr((Node *) lsecond(expr->args), context, false);
11379 return true;
11380 }
11381 return false;
11382}
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define lthird(l)
Definition: pg_list.h:188
#define lsecond(l)
Definition: pg_list.h:183
#define lfourth(l)
Definition: pg_list.h:193

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), FuncExpr::args, Assert(), deparse_context::buf, buf, Const::consttype, FuncExpr::funcid, get_rule_expr(), get_rule_expr_paren(), IsA, lfourth, linitial, list_length(), lsecond, lthird, and TextDatumGetCString.

Referenced by get_func_expr().

◆ get_insert_query_def()

static void get_insert_query_def ( Query query,
deparse_context context 
)
static

Definition at line 6929 of file ruleutils.c.

6930{
6931 StringInfo buf = context->buf;
6932 RangeTblEntry *select_rte = NULL;
6933 RangeTblEntry *values_rte = NULL;
6934 RangeTblEntry *rte;
6935 char *sep;
6936 ListCell *l;
6937 List *strippedexprs;
6938
6939 /* Insert the WITH clause if given */
6940 get_with_clause(query, context);
6941
6942 /*
6943 * If it's an INSERT ... SELECT or multi-row VALUES, there will be a
6944 * single RTE for the SELECT or VALUES. Plain VALUES has neither.
6945 */
6946 foreach(l, query->rtable)
6947 {
6948 rte = (RangeTblEntry *) lfirst(l);
6949
6950 if (rte->rtekind == RTE_SUBQUERY)
6951 {
6952 if (select_rte)
6953 elog(ERROR, "too many subquery RTEs in INSERT");
6954 select_rte = rte;
6955 }
6956
6957 if (rte->rtekind == RTE_VALUES)
6958 {
6959 if (values_rte)
6960 elog(ERROR, "too many values RTEs in INSERT");
6961 values_rte = rte;
6962 }
6963 }
6964 if (select_rte && values_rte)
6965 elog(ERROR, "both subquery and values RTEs in INSERT");
6966
6967 /*
6968 * Start the query with INSERT INTO relname
6969 */
6970 rte = rt_fetch(query->resultRelation, query->rtable);
6971 Assert(rte->rtekind == RTE_RELATION);
6972
6973 if (PRETTY_INDENT(context))
6974 {
6975 context->indentLevel += PRETTYINDENT_STD;
6977 }
6978 appendStringInfo(buf, "INSERT INTO %s",
6980
6981 /* Print the relation alias, if needed; INSERT requires explicit AS */
6982 get_rte_alias(rte, query->resultRelation, true, context);
6983
6984 /* always want a space here */
6986
6987 /*
6988 * Add the insert-column-names list. Any indirection decoration needed on
6989 * the column names can be inferred from the top targetlist.
6990 */
6991 strippedexprs = NIL;
6992 sep = "";
6993 if (query->targetList)
6995 foreach(l, query->targetList)
6996 {
6997 TargetEntry *tle = (TargetEntry *) lfirst(l);
6998
6999 if (tle->resjunk)
7000 continue; /* ignore junk entries */
7001
7003 sep = ", ";
7004
7005 /*
7006 * Put out name of target column; look in the catalogs, not at
7007 * tle->resname, since resname will fail to track RENAME.
7008 */
7011 tle->resno,
7012 false)));
7013
7014 /*
7015 * Print any indirection needed (subfields or subscripts), and strip
7016 * off the top-level nodes representing the indirection assignments.
7017 * Add the stripped expressions to strippedexprs. (If it's a
7018 * single-VALUES statement, the stripped expressions are the VALUES to
7019 * print below. Otherwise they're just Vars and not really
7020 * interesting.)
7021 */
7022 strippedexprs = lappend(strippedexprs,
7023 processIndirection((Node *) tle->expr,
7024 context));
7025 }
7026 if (query->targetList)
7028
7029 if (query->override)
7030 {
7031 if (query->override == OVERRIDING_SYSTEM_VALUE)
7032 appendStringInfoString(buf, "OVERRIDING SYSTEM VALUE ");
7033 else if (query->override == OVERRIDING_USER_VALUE)
7034 appendStringInfoString(buf, "OVERRIDING USER VALUE ");
7035 }
7036
7037 if (select_rte)
7038 {
7039 /* Add the SELECT */
7040 get_query_def(select_rte->subquery, buf, context->namespaces, NULL,
7041 false,
7042 context->prettyFlags, context->wrapColumn,
7043 context->indentLevel);
7044 }
7045 else if (values_rte)
7046 {
7047 /* Add the multi-VALUES expression lists */
7048 get_values_def(values_rte->values_lists, context);
7049 }
7050 else if (strippedexprs)
7051 {
7052 /* Add the single-VALUES expression list */
7053 appendContextKeyword(context, "VALUES (",
7055 get_rule_list_toplevel(strippedexprs, context, false);
7057 }
7058 else
7059 {
7060 /* No expressions, so it must be DEFAULT VALUES */
7061 appendStringInfoString(buf, "DEFAULT VALUES");
7062 }
7063
7064 /* Add ON CONFLICT if present */
7065 if (query->onConflict)
7066 {
7067 OnConflictExpr *confl = query->onConflict;
7068
7069 appendStringInfoString(buf, " ON CONFLICT");
7070
7071 if (confl->arbiterElems)
7072 {
7073 /* Add the single-VALUES expression list */
7075 get_rule_expr((Node *) confl->arbiterElems, context, false);
7077
7078 /* Add a WHERE clause (for partial indexes) if given */
7079 if (confl->arbiterWhere != NULL)
7080 {
7081 bool save_varprefix;
7082
7083 /*
7084 * Force non-prefixing of Vars, since parser assumes that they
7085 * belong to target relation. WHERE clause does not use
7086 * InferenceElem, so this is separately required.
7087 */
7088 save_varprefix = context->varprefix;
7089 context->varprefix = false;
7090
7091 appendContextKeyword(context, " WHERE ",
7093 get_rule_expr(confl->arbiterWhere, context, false);
7094
7095 context->varprefix = save_varprefix;
7096 }
7097 }
7098 else if (OidIsValid(confl->constraint))
7099 {
7100 char *constraint = get_constraint_name(confl->constraint);
7101
7102 if (!constraint)
7103 elog(ERROR, "cache lookup failed for constraint %u",
7104 confl->constraint);
7105 appendStringInfo(buf, " ON CONSTRAINT %s",
7106 quote_identifier(constraint));
7107 }
7108
7109 if (confl->action == ONCONFLICT_NOTHING)
7110 {
7111 appendStringInfoString(buf, " DO NOTHING");
7112 }
7113 else
7114 {
7115 appendStringInfoString(buf, " DO UPDATE SET ");
7116 /* Deparse targetlist */
7118 context, rte);
7119
7120 /* Add a WHERE clause if given */
7121 if (confl->onConflictWhere != NULL)
7122 {
7123 appendContextKeyword(context, " WHERE ",
7125 get_rule_expr(confl->onConflictWhere, context, false);
7126 }
7127 }
7128 }
7129
7130 /* Add RETURNING if present */
7131 if (query->returningList)
7132 get_returning_clause(query, context);
7133}
char * get_constraint_name(Oid conoid)
Definition: lsyscache.c:1116
@ ONCONFLICT_NOTHING
Definition: nodes.h:421
@ OVERRIDING_SYSTEM_VALUE
Definition: primnodes.h:30
@ OVERRIDING_USER_VALUE
Definition: primnodes.h:29
static void get_rule_list_toplevel(List *lst, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:10643
static Node * processIndirection(Node *node, deparse_context *context)
Definition: ruleutils.c:12911
static void get_update_query_targetlist_def(Query *query, List *targetList, deparse_context *context, RangeTblEntry *rte)
Definition: ruleutils.c:7193
List * arbiterElems
Definition: primnodes.h:2356
OnConflictAction action
Definition: primnodes.h:2353
List * onConflictSet
Definition: primnodes.h:2362
Node * onConflictWhere
Definition: primnodes.h:2363
Node * arbiterWhere
Definition: primnodes.h:2358
OnConflictExpr * onConflict
Definition: parsenodes.h:198
AttrNumber resno
Definition: primnodes.h:2221

References OnConflictExpr::action, appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), OnConflictExpr::arbiterElems, OnConflictExpr::arbiterWhere, Assert(), deparse_context::buf, buf, OnConflictExpr::constraint, elog, ERROR, TargetEntry::expr, generate_relation_name(), get_attname(), get_constraint_name(), get_query_def(), get_returning_clause(), get_rte_alias(), get_rule_expr(), get_rule_list_toplevel(), get_update_query_targetlist_def(), get_values_def(), get_with_clause(), deparse_context::indentLevel, lappend(), lfirst, deparse_context::namespaces, NIL, OidIsValid, Query::onConflict, ONCONFLICT_NOTHING, OnConflictExpr::onConflictSet, OnConflictExpr::onConflictWhere, OVERRIDING_SYSTEM_VALUE, OVERRIDING_USER_VALUE, PRETTY_INDENT, deparse_context::prettyFlags, PRETTYINDENT_STD, processIndirection(), quote_identifier(), RangeTblEntry::relid, TargetEntry::resno, Query::returningList, rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::subquery, Query::targetList, RangeTblEntry::values_lists, deparse_context::varprefix, and deparse_context::wrapColumn.

Referenced by get_query_def().

◆ get_json_agg_constructor()

static void get_json_agg_constructor ( JsonConstructorExpr ctor,
deparse_context context,
const char *  funcname,
bool  is_json_objectagg 
)
static

Definition at line 11758 of file ruleutils.c.

11760{
11762
11765
11766 if (IsA(ctor->func, Aggref))
11767 get_agg_expr_helper((Aggref *) ctor->func, context,
11768 (Aggref *) ctor->func,
11769 funcname, options.data, is_json_objectagg);
11770 else if (IsA(ctor->func, WindowFunc))
11771 get_windowfunc_expr_helper((WindowFunc *) ctor->func, context,
11772 funcname, options.data,
11773 is_json_objectagg);
11774 else
11775 elog(ERROR, "invalid JsonConstructorExpr underlying node type: %d",
11776 nodeTag(ctor->func));
11777}
static char ** options
static void get_windowfunc_expr_helper(WindowFunc *wfunc, deparse_context *context, const char *funcname, const char *options, bool is_json_objectagg)
Definition: ruleutils.c:11026
static void get_json_constructor_options(JsonConstructorExpr *ctor, StringInfo buf)
Definition: ruleutils.c:11728

References elog, ERROR, JsonConstructorExpr::func, funcname, get_agg_expr_helper(), get_json_constructor_options(), get_windowfunc_expr_helper(), initStringInfo(), IsA, nodeTag, and options.

Referenced by get_json_constructor().

◆ get_json_behavior()

static void get_json_behavior ( JsonBehavior behavior,
deparse_context context,
const char *  on 
)
static

Definition at line 9163 of file ruleutils.c.

9165{
9166 /*
9167 * The order of array elements must correspond to the order of
9168 * JsonBehaviorType members.
9169 */
9170 const char *behavior_names[] =
9171 {
9172 " NULL",
9173 " ERROR",
9174 " EMPTY",
9175 " TRUE",
9176 " FALSE",
9177 " UNKNOWN",
9178 " EMPTY ARRAY",
9179 " EMPTY OBJECT",
9180 " DEFAULT "
9181 };
9182
9183 if ((int) behavior->btype < 0 || behavior->btype >= lengthof(behavior_names))
9184 elog(ERROR, "invalid json behavior type: %d", behavior->btype);
9185
9186 appendStringInfoString(context->buf, behavior_names[behavior->btype]);
9187
9188 if (behavior->btype == JSON_BEHAVIOR_DEFAULT)
9189 get_rule_expr(behavior->expr, context, false);
9190
9191 appendStringInfo(context->buf, " ON %s", on);
9192}
#define lengthof(array)
Definition: c.h:759
@ JSON_BEHAVIOR_DEFAULT
Definition: primnodes.h:1778
Node * expr
Definition: primnodes.h:1796
JsonBehaviorType btype
Definition: primnodes.h:1795

References appendStringInfo(), appendStringInfoString(), JsonBehavior::btype, deparse_context::buf, elog, ERROR, JsonBehavior::expr, get_rule_expr(), JSON_BEHAVIOR_DEFAULT, and lengthof.

Referenced by get_json_expr_options(), and get_json_table().

◆ get_json_constructor()

static void get_json_constructor ( JsonConstructorExpr ctor,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 11662 of file ruleutils.c.

11664{
11665 StringInfo buf = context->buf;
11666 const char *funcname;
11667 bool is_json_object;
11668 int curridx;
11669 ListCell *lc;
11670
11671 if (ctor->type == JSCTOR_JSON_OBJECTAGG)
11672 {
11673 get_json_agg_constructor(ctor, context, "JSON_OBJECTAGG", true);
11674 return;
11675 }
11676 else if (ctor->type == JSCTOR_JSON_ARRAYAGG)
11677 {
11678 get_json_agg_constructor(ctor, context, "JSON_ARRAYAGG", false);
11679 return;
11680 }
11681
11682 switch (ctor->type)
11683 {
11684 case JSCTOR_JSON_OBJECT:
11685 funcname = "JSON_OBJECT";
11686 break;
11687 case JSCTOR_JSON_ARRAY:
11688 funcname = "JSON_ARRAY";
11689 break;
11690 case JSCTOR_JSON_PARSE:
11691 funcname = "JSON";
11692 break;
11693 case JSCTOR_JSON_SCALAR:
11694 funcname = "JSON_SCALAR";
11695 break;
11697 funcname = "JSON_SERIALIZE";
11698 break;
11699 default:
11700 elog(ERROR, "invalid JsonConstructorType %d", ctor->type);
11701 }
11702
11703 appendStringInfo(buf, "%s(", funcname);
11704
11705 is_json_object = ctor->type == JSCTOR_JSON_OBJECT;
11706 foreach(lc, ctor->args)
11707 {
11708 curridx = foreach_current_index(lc);
11709 if (curridx > 0)
11710 {
11711 const char *sep;
11712
11713 sep = (is_json_object && (curridx % 2) != 0) ? " : " : ", ";
11715 }
11716
11717 get_rule_expr((Node *) lfirst(lc), context, true);
11718 }
11719
11722}
@ JSCTOR_JSON_SERIALIZE
Definition: primnodes.h:1701
@ JSCTOR_JSON_ARRAYAGG
Definition: primnodes.h:1698
@ JSCTOR_JSON_PARSE
Definition: primnodes.h:1699
@ JSCTOR_JSON_OBJECT
Definition: primnodes.h:1695
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1700
@ JSCTOR_JSON_ARRAY
Definition: primnodes.h:1696
@ JSCTOR_JSON_OBJECTAGG
Definition: primnodes.h:1697
static void get_json_agg_constructor(JsonConstructorExpr *ctor, deparse_context *context, const char *funcname, bool is_json_objectagg)
Definition: ruleutils.c:11758
JsonConstructorType type
Definition: primnodes.h:1711

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), JsonConstructorExpr::args, deparse_context::buf, buf, elog, ERROR, foreach_current_index, funcname, get_json_agg_constructor(), get_json_constructor_options(), get_rule_expr(), JSCTOR_JSON_ARRAY, JSCTOR_JSON_ARRAYAGG, JSCTOR_JSON_OBJECT, JSCTOR_JSON_OBJECTAGG, JSCTOR_JSON_PARSE, JSCTOR_JSON_SCALAR, JSCTOR_JSON_SERIALIZE, lfirst, and JsonConstructorExpr::type.

Referenced by get_rule_expr().

◆ get_json_constructor_options()

static void get_json_constructor_options ( JsonConstructorExpr ctor,
StringInfo  buf 
)
static

Definition at line 11728 of file ruleutils.c.

11729{
11730 if (ctor->absent_on_null)
11731 {
11732 if (ctor->type == JSCTOR_JSON_OBJECT ||
11733 ctor->type == JSCTOR_JSON_OBJECTAGG)
11734 appendStringInfoString(buf, " ABSENT ON NULL");
11735 }
11736 else
11737 {
11738 if (ctor->type == JSCTOR_JSON_ARRAY ||
11739 ctor->type == JSCTOR_JSON_ARRAYAGG)
11740 appendStringInfoString(buf, " NULL ON NULL");
11741 }
11742
11743 if (ctor->unique)
11744 appendStringInfoString(buf, " WITH UNIQUE KEYS");
11745
11746 /*
11747 * Append RETURNING clause if needed; JSON() and JSON_SCALAR() don't
11748 * support one.
11749 */
11750 if (ctor->type != JSCTOR_JSON_PARSE && ctor->type != JSCTOR_JSON_SCALAR)
11751 get_json_returning(ctor->returning, buf, true);
11752}
static void get_json_returning(JsonReturning *returning, StringInfo buf, bool json_format_by_default)
Definition: ruleutils.c:11642
JsonReturning * returning
Definition: primnodes.h:1715

References JsonConstructorExpr::absent_on_null, appendStringInfoString(), buf, get_json_returning(), JSCTOR_JSON_ARRAY, JSCTOR_JSON_ARRAYAGG, JSCTOR_JSON_OBJECT, JSCTOR_JSON_OBJECTAGG, JSCTOR_JSON_PARSE, JSCTOR_JSON_SCALAR, JsonConstructorExpr::returning, JsonConstructorExpr::type, and JsonConstructorExpr::unique.

Referenced by get_json_agg_constructor(), and get_json_constructor().

◆ get_json_expr_options()

static void get_json_expr_options ( JsonExpr jsexpr,
deparse_context context,
JsonBehaviorType  default_behavior 
)
static

Definition at line 9201 of file ruleutils.c.

9203{
9204 if (jsexpr->op == JSON_QUERY_OP)
9205 {
9206 if (jsexpr->wrapper == JSW_CONDITIONAL)
9207 appendStringInfoString(context->buf, " WITH CONDITIONAL WRAPPER");
9208 else if (jsexpr->wrapper == JSW_UNCONDITIONAL)
9209 appendStringInfoString(context->buf, " WITH UNCONDITIONAL WRAPPER");
9210 /* The default */
9211 else if (jsexpr->wrapper == JSW_NONE || jsexpr->wrapper == JSW_UNSPEC)
9212 appendStringInfoString(context->buf, " WITHOUT WRAPPER");
9213
9214 if (jsexpr->omit_quotes)
9215 appendStringInfoString(context->buf, " OMIT QUOTES");
9216 /* The default */
9217 else
9218 appendStringInfoString(context->buf, " KEEP QUOTES");
9219 }
9220
9221 if (jsexpr->on_empty && jsexpr->on_empty->btype != default_behavior)
9222 get_json_behavior(jsexpr->on_empty, context, "EMPTY");
9223
9224 if (jsexpr->on_error && jsexpr->on_error->btype != default_behavior)
9225 get_json_behavior(jsexpr->on_error, context, "ERROR");
9226}
@ JSW_UNCONDITIONAL
Definition: primnodes.h:1758
@ JSW_CONDITIONAL
Definition: primnodes.h:1757
@ JSW_UNSPEC
Definition: primnodes.h:1755
@ JSW_NONE
Definition: primnodes.h:1756
@ JSON_QUERY_OP
Definition: primnodes.h:1808
static void get_json_behavior(JsonBehavior *behavior, deparse_context *context, const char *on)
Definition: ruleutils.c:9163
JsonBehavior * on_empty
Definition: primnodes.h:1844
JsonWrapper wrapper
Definition: primnodes.h:1855
JsonExprOp op
Definition: primnodes.h:1822
JsonBehavior * on_error
Definition: primnodes.h:1845
bool omit_quotes
Definition: primnodes.h:1858

References appendStringInfoString(), JsonBehavior::btype, deparse_context::buf, get_json_behavior(), JSON_QUERY_OP, JSW_CONDITIONAL, JSW_NONE, JSW_UNCONDITIONAL, JSW_UNSPEC, JsonExpr::omit_quotes, JsonExpr::on_empty, JsonExpr::on_error, JsonExpr::op, and JsonExpr::wrapper.

Referenced by get_json_table_columns(), and get_rule_expr().

◆ get_json_format()

static void get_json_format ( JsonFormat format,
StringInfo  buf 
)
static

Definition at line 11617 of file ruleutils.c.

11618{
11619 if (format->format_type == JS_FORMAT_DEFAULT)
11620 return;
11621
11623 format->format_type == JS_FORMAT_JSONB ?
11624 " FORMAT JSONB" : " FORMAT JSON");
11625
11626 if (format->encoding != JS_ENC_DEFAULT)
11627 {
11628 const char *encoding;
11629
11630 encoding =
11631 format->encoding == JS_ENC_UTF16 ? "UTF16" :
11632 format->encoding == JS_ENC_UTF32 ? "UTF32" : "UTF8";
11633
11634 appendStringInfo(buf, " ENCODING %s", encoding);
11635 }
11636}
static char format
int32 encoding
Definition: pg_database.h:41
@ JS_FORMAT_JSONB
Definition: primnodes.h:1645
@ JS_FORMAT_DEFAULT
Definition: primnodes.h:1643
@ JS_ENC_DEFAULT
Definition: primnodes.h:1631
@ JS_ENC_UTF32
Definition: primnodes.h:1634
@ JS_ENC_UTF16
Definition: primnodes.h:1633

References appendStringInfo(), appendStringInfoString(), buf, encoding, format, JS_ENC_DEFAULT, JS_ENC_UTF16, JS_ENC_UTF32, JS_FORMAT_DEFAULT, and JS_FORMAT_JSONB.

Referenced by get_json_returning(), and get_rule_expr().

◆ get_json_path_spec()

static void get_json_path_spec ( Node path_spec,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 11605 of file ruleutils.c.

11606{
11607 if (IsA(path_spec, Const))
11608 get_const_expr((Const *) path_spec, context, -1);
11609 else
11610 get_rule_expr(path_spec, context, showimplicit);
11611}

References get_const_expr(), get_rule_expr(), and IsA.

Referenced by get_json_table_columns(), and get_rule_expr().

◆ get_json_returning()

static void get_json_returning ( JsonReturning returning,
StringInfo  buf,
bool  json_format_by_default 
)
static

Definition at line 11642 of file ruleutils.c.

11644{
11645 if (!OidIsValid(returning->typid))
11646 return;
11647
11648 appendStringInfo(buf, " RETURNING %s",
11650 returning->typmod));
11651
11652 if (!json_format_by_default ||
11653 returning->format->format_type !=
11654 (returning->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON))
11655 get_json_format(returning->format, buf);
11656}
@ JS_FORMAT_JSON
Definition: primnodes.h:1644
static void get_json_format(JsonFormat *format, StringInfo buf)
Definition: ruleutils.c:11617
JsonFormatType format_type
Definition: primnodes.h:1656
JsonFormat * format
Definition: primnodes.h:1668

References appendStringInfo(), buf, JsonReturning::format, JsonFormat::format_type, format_type_with_typemod(), get_json_format(), JS_FORMAT_JSON, JS_FORMAT_JSONB, OidIsValid, JsonReturning::typid, and JsonReturning::typmod.

Referenced by get_json_constructor_options(), and get_rule_expr().

◆ get_json_table()

static void get_json_table ( TableFunc tf,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 12172 of file ruleutils.c.

12173{
12174 StringInfo buf = context->buf;
12175 JsonExpr *jexpr = castNode(JsonExpr, tf->docexpr);
12177
12178 appendStringInfoString(buf, "JSON_TABLE(");
12179
12180 if (PRETTY_INDENT(context))
12181 context->indentLevel += PRETTYINDENT_VAR;
12182
12183 appendContextKeyword(context, "", 0, 0, 0);
12184
12185 get_rule_expr(jexpr->formatted_expr, context, showimplicit);
12186
12188
12189 get_const_expr(root->path->value, context, -1);
12190
12191 appendStringInfo(buf, " AS %s", quote_identifier(root->path->name));
12192
12193 if (jexpr->passing_values)
12194 {
12195 ListCell *lc1,
12196 *lc2;
12197 bool needcomma = false;
12198
12200 appendContextKeyword(context, "PASSING ", 0, 0, 0);
12201
12202 if (PRETTY_INDENT(context))
12203 context->indentLevel += PRETTYINDENT_VAR;
12204
12205 forboth(lc1, jexpr->passing_names,
12206 lc2, jexpr->passing_values)
12207 {
12208 if (needcomma)
12210 needcomma = true;
12211
12212 appendContextKeyword(context, "", 0, 0, 0);
12213
12214 get_rule_expr((Node *) lfirst(lc2), context, false);
12215 appendStringInfo(buf, " AS %s",
12216 quote_identifier((lfirst_node(String, lc1))->sval)
12217 );
12218 }
12219
12220 if (PRETTY_INDENT(context))
12221 context->indentLevel -= PRETTYINDENT_VAR;
12222 }
12223
12224 get_json_table_columns(tf, castNode(JsonTablePathScan, tf->plan), context,
12225 showimplicit);
12226
12228 get_json_behavior(jexpr->on_error, context, "ERROR");
12229
12230 if (PRETTY_INDENT(context))
12231 context->indentLevel -= PRETTYINDENT_VAR;
12232
12233 appendContextKeyword(context, ")", 0, 0, 0);
12234}
#define castNode(_type_, nodeptr)
Definition: nodes.h:178
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition: primnodes.h:1776
tree ctl root
Definition: radixtree.h:1857
static void get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:12066
Node * formatted_expr
Definition: primnodes.h:1828
List * passing_values
Definition: primnodes.h:1841
List * passing_names
Definition: primnodes.h:1840
Definition: value.h:64
Node * docexpr
Definition: primnodes.h:120

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), JsonBehavior::btype, deparse_context::buf, buf, castNode, TableFunc::docexpr, forboth, JsonExpr::formatted_expr, get_const_expr(), get_json_behavior(), get_json_table_columns(), get_rule_expr(), deparse_context::indentLevel, JSON_BEHAVIOR_EMPTY_ARRAY, lfirst, lfirst_node, JsonExpr::on_error, JsonExpr::passing_names, JsonExpr::passing_values, PRETTY_INDENT, PRETTYINDENT_VAR, quote_identifier(), and root.

Referenced by get_tablefunc().

◆ get_json_table_columns()

static void get_json_table_columns ( TableFunc tf,
JsonTablePathScan scan,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 12066 of file ruleutils.c.

12069{
12070 StringInfo buf = context->buf;
12071 ListCell *lc_colname;
12072 ListCell *lc_coltype;
12073 ListCell *lc_coltypmod;
12074 ListCell *lc_colvalexpr;
12075 int colnum = 0;
12076
12078 appendContextKeyword(context, "COLUMNS (", 0, 0, 0);
12079
12080 if (PRETTY_INDENT(context))
12081 context->indentLevel += PRETTYINDENT_VAR;
12082
12083 forfour(lc_colname, tf->colnames,
12084 lc_coltype, tf->coltypes,
12085 lc_coltypmod, tf->coltypmods,
12086 lc_colvalexpr, tf->colvalexprs)
12087 {
12088 char *colname = strVal(lfirst(lc_colname));
12089 JsonExpr *colexpr;
12090 Oid typid;
12091 int32 typmod;
12092 bool ordinality;
12093 JsonBehaviorType default_behavior;
12094
12095 typid = lfirst_oid(lc_coltype);
12096 typmod = lfirst_int(lc_coltypmod);
12097 colexpr = castNode(JsonExpr, lfirst(lc_colvalexpr));
12098
12099 /* Skip columns that don't belong to this scan. */
12100 if (scan->colMin < 0 || colnum < scan->colMin)
12101 {
12102 colnum++;
12103 continue;
12104 }
12105 if (colnum > scan->colMax)
12106 break;
12107
12108 if (colnum > scan->colMin)
12110
12111 colnum++;
12112
12113 ordinality = !colexpr;
12114
12115 appendContextKeyword(context, "", 0, 0, 0);
12116
12117 appendStringInfo(buf, "%s %s", quote_identifier(colname),
12118 ordinality ? "FOR ORDINALITY" :
12119 format_type_with_typemod(typid, typmod));
12120 if (ordinality)
12121 continue;
12122
12123 /*
12124 * Set default_behavior to guide get_json_expr_options() on whether to
12125 * to emit the ON ERROR / EMPTY clauses.
12126 */
12127 if (colexpr->op == JSON_EXISTS_OP)
12128 {
12129 appendStringInfoString(buf, " EXISTS");
12130 default_behavior = JSON_BEHAVIOR_FALSE;
12131 }
12132 else
12133 {
12134 if (colexpr->op == JSON_QUERY_OP)
12135 {
12136 char typcategory;
12137 bool typispreferred;
12138
12139 get_type_category_preferred(typid, &typcategory, &typispreferred);
12140
12141 if (typcategory == TYPCATEGORY_STRING)
12143 colexpr->format->format_type == JS_FORMAT_JSONB ?
12144 " FORMAT JSONB" : " FORMAT JSON");
12145 }
12146
12147 default_behavior = JSON_BEHAVIOR_NULL;
12148 }
12149
12150 appendStringInfoString(buf, " PATH ");
12151
12152 get_json_path_spec(colexpr->path_spec, context, showimplicit);
12153
12154 get_json_expr_options(colexpr, context, default_behavior);
12155 }
12156
12157 if (scan->child)
12158 get_json_table_nested_columns(tf, scan->child, context, showimplicit,
12159 scan->colMin >= 0);
12160
12161 if (PRETTY_INDENT(context))
12162 context->indentLevel -= PRETTYINDENT_VAR;
12163
12164 appendContextKeyword(context, ")", 0, 0, 0);
12165}
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2793
JsonBehaviorType
Definition: primnodes.h:1769
@ JSON_BEHAVIOR_FALSE
Definition: primnodes.h:1774
@ JSON_BEHAVIOR_NULL
Definition: primnodes.h:1770
@ JSON_EXISTS_OP
Definition: primnodes.h:1807
static void get_json_expr_options(JsonExpr *jsexpr, deparse_context *context, JsonBehaviorType default_behavior)
Definition: ruleutils.c:9201
static void get_json_path_spec(Node *path_spec, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:11605
static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan, deparse_context *context, bool showimplicit, bool needcomma)
Definition: ruleutils.c:12034
JsonFormat * format
Definition: primnodes.h:1831
Node * path_spec
Definition: primnodes.h:1834
JsonTablePlan * child
Definition: primnodes.h:1912

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, castNode, JsonTablePathScan::child, JsonTablePathScan::colMax, JsonTablePathScan::colMin, forfour, JsonExpr::format, JsonFormat::format_type, format_type_with_typemod(), get_json_expr_options(), get_json_path_spec(), get_json_table_nested_columns(), get_type_category_preferred(), deparse_context::indentLevel, JS_FORMAT_JSONB, JSON_BEHAVIOR_FALSE, JSON_BEHAVIOR_NULL, JSON_EXISTS_OP, JSON_QUERY_OP, lfirst, lfirst_int, lfirst_oid, JsonExpr::op, JsonExpr::path_spec, PRETTY_INDENT, PRETTYINDENT_VAR, quote_identifier(), and strVal.

Referenced by get_json_table(), and get_json_table_nested_columns().

◆ get_json_table_nested_columns()

static void get_json_table_nested_columns ( TableFunc tf,
JsonTablePlan plan,
deparse_context context,
bool  showimplicit,
bool  needcomma 
)
static

Definition at line 12034 of file ruleutils.c.

12037{
12039 {
12041
12042 if (needcomma)
12043 appendStringInfoChar(context->buf, ',');
12044
12045 appendStringInfoChar(context->buf, ' ');
12046 appendContextKeyword(context, "NESTED PATH ", 0, 0, 0);
12047 get_const_expr(scan->path->value, context, -1);
12048 appendStringInfo(context->buf, " AS %s", quote_identifier(scan->path->name));
12049 get_json_table_columns(tf, scan, context, showimplicit);
12050 }
12051 else if (IsA(plan, JsonTableSiblingJoin))
12052 {
12054
12055 get_json_table_nested_columns(tf, join->lplan, context, showimplicit,
12056 needcomma);
12057 get_json_table_nested_columns(tf, join->rplan, context, showimplicit,
12058 true);
12059 }
12060}
JsonTablePath * path
Definition: primnodes.h:1903
Const * value
Definition: primnodes.h:1876
JsonTablePlan * rplan
Definition: primnodes.h:1933
JsonTablePlan * lplan
Definition: primnodes.h:1932

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), deparse_context::buf, castNode, get_const_expr(), get_json_table_columns(), get_json_table_nested_columns(), IsA, JsonTableSiblingJoin::lplan, JsonTablePath::name, JsonTablePathScan::path, plan, quote_identifier(), JsonTableSiblingJoin::rplan, and JsonTablePath::value.

Referenced by get_json_table_columns(), and get_json_table_nested_columns().

◆ get_merge_query_def()

static void get_merge_query_def ( Query query,
deparse_context context 
)
static

Definition at line 7392 of file ruleutils.c.

7393{
7394 StringInfo buf = context->buf;
7395 RangeTblEntry *rte;
7396 ListCell *lc;
7397 bool haveNotMatchedBySource;
7398
7399 /* Insert the WITH clause if given */
7400 get_with_clause(query, context);
7401
7402 /*
7403 * Start the query with MERGE INTO relname
7404 */
7405 rte = rt_fetch(query->resultRelation, query->rtable);
7406 Assert(rte->rtekind == RTE_RELATION);
7407 if (PRETTY_INDENT(context))
7408 {
7410 context->indentLevel += PRETTYINDENT_STD;
7411 }
7412 appendStringInfo(buf, "MERGE INTO %s%s",
7413 only_marker(rte),
7415
7416 /* Print the relation alias, if needed */
7417 get_rte_alias(rte, query->resultRelation, false, context);
7418
7419 /* Print the source relation and join clause */
7420 get_from_clause(query, " USING ", context);
7421 appendContextKeyword(context, " ON ",
7423 get_rule_expr(query->mergeJoinCondition, context, false);
7424
7425 /*
7426 * Test for any NOT MATCHED BY SOURCE actions. If there are none, then
7427 * any NOT MATCHED BY TARGET actions are output as "WHEN NOT MATCHED", per
7428 * SQL standard. Otherwise, we have a non-SQL-standard query, so output
7429 * "BY SOURCE" / "BY TARGET" qualifiers for all NOT MATCHED actions, to be
7430 * more explicit.
7431 */
7432 haveNotMatchedBySource = false;
7433 foreach(lc, query->mergeActionList)
7434 {
7436
7437 if (action->matchKind == MERGE_WHEN_NOT_MATCHED_BY_SOURCE)
7438 {
7439 haveNotMatchedBySource = true;
7440 break;
7441 }
7442 }
7443
7444 /* Print each merge action */
7445 foreach(lc, query->mergeActionList)
7446 {
7448
7449 appendContextKeyword(context, " WHEN ",
7451 switch (action->matchKind)
7452 {
7453 case MERGE_WHEN_MATCHED:
7454 appendStringInfoString(buf, "MATCHED");
7455 break;
7457 appendStringInfoString(buf, "NOT MATCHED BY SOURCE");
7458 break;
7460 if (haveNotMatchedBySource)
7461 appendStringInfoString(buf, "NOT MATCHED BY TARGET");
7462 else
7463 appendStringInfoString(buf, "NOT MATCHED");
7464 break;
7465 default:
7466 elog(ERROR, "unrecognized matchKind: %d",
7467 (int) action->matchKind);
7468 }
7469
7470 if (action->qual)
7471 {
7472 appendContextKeyword(context, " AND ",
7474 get_rule_expr(action->qual, context, false);
7475 }
7476 appendContextKeyword(context, " THEN ",
7478
7479 if (action->commandType == CMD_INSERT)
7480 {
7481 /* This generally matches get_insert_query_def() */
7482 List *strippedexprs = NIL;
7483 const char *sep = "";
7484 ListCell *lc2;
7485
7486 appendStringInfoString(buf, "INSERT");
7487
7488 if (action->targetList)
7490 foreach(lc2, action->targetList)
7491 {
7492 TargetEntry *tle = (TargetEntry *) lfirst(lc2);
7493
7494 Assert(!tle->resjunk);
7495
7497 sep = ", ";
7498
7501 tle->resno,
7502 false)));
7503 strippedexprs = lappend(strippedexprs,
7504 processIndirection((Node *) tle->expr,
7505 context));
7506 }
7507 if (action->targetList)
7509
7510 if (action->override)
7511 {
7512 if (action->override == OVERRIDING_SYSTEM_VALUE)
7513 appendStringInfoString(buf, " OVERRIDING SYSTEM VALUE");
7514 else if (action->override == OVERRIDING_USER_VALUE)
7515 appendStringInfoString(buf, " OVERRIDING USER VALUE");
7516 }
7517
7518 if (strippedexprs)
7519 {
7520 appendContextKeyword(context, " VALUES (",
7522 get_rule_list_toplevel(strippedexprs, context, false);
7524 }
7525 else
7526 appendStringInfoString(buf, " DEFAULT VALUES");
7527 }
7528 else if (action->commandType == CMD_UPDATE)
7529 {
7530 appendStringInfoString(buf, "UPDATE SET ");
7531 get_update_query_targetlist_def(query, action->targetList,
7532 context, rte);
7533 }
7534 else if (action->commandType == CMD_DELETE)
7535 appendStringInfoString(buf, "DELETE");
7536 else if (action->commandType == CMD_NOTHING)
7537 appendStringInfoString(buf, "DO NOTHING");
7538 }
7539
7540 /* Add RETURNING if present */
7541 if (query->returningList)
7542 get_returning_clause(query, context);
7543}
@ CMD_INSERT
Definition: nodes.h:269
@ CMD_DELETE
Definition: nodes.h:270
@ CMD_UPDATE
Definition: nodes.h:268
@ CMD_NOTHING
Definition: nodes.h:274
@ MERGE_WHEN_NOT_MATCHED_BY_TARGET
Definition: primnodes.h:2003
@ MERGE_WHEN_NOT_MATCHED_BY_SOURCE
Definition: primnodes.h:2002
@ MERGE_WHEN_MATCHED
Definition: primnodes.h:2001
Node * mergeJoinCondition
Definition: parsenodes.h:191
List * mergeActionList
Definition: parsenodes.h:180

References generate_unaccent_rules::action, appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), deparse_context::buf, buf, CMD_DELETE, CMD_INSERT, CMD_NOTHING, CMD_UPDATE, elog, ERROR, TargetEntry::expr, generate_relation_name(), get_attname(), get_from_clause(), get_returning_clause(), get_rte_alias(), get_rule_expr(), get_rule_list_toplevel(), get_update_query_targetlist_def(), get_with_clause(), deparse_context::indentLevel, lappend(), lfirst, lfirst_node, MERGE_WHEN_MATCHED, MERGE_WHEN_NOT_MATCHED_BY_SOURCE, MERGE_WHEN_NOT_MATCHED_BY_TARGET, Query::mergeActionList, Query::mergeJoinCondition, NIL, only_marker, OVERRIDING_SYSTEM_VALUE, OVERRIDING_USER_VALUE, PRETTY_INDENT, PRETTYINDENT_STD, processIndirection(), quote_identifier(), RangeTblEntry::relid, TargetEntry::resno, Query::returningList, rt_fetch, Query::rtable, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by get_query_def().

◆ get_name_for_var_field()

static const char * get_name_for_var_field ( Var var,
int  fieldno,
int  levelsup,
deparse_context context 
)
static

Definition at line 8007 of file ruleutils.c.

8009{
8010 RangeTblEntry *rte;
8012 int netlevelsup;
8013 deparse_namespace *dpns;
8014 int varno;
8015 AttrNumber varattno;
8016 TupleDesc tupleDesc;
8017 Node *expr;
8018
8019 /*
8020 * If it's a RowExpr that was expanded from a whole-row Var, use the
8021 * column names attached to it. (We could let get_expr_result_tupdesc()
8022 * handle this, but it's much cheaper to just pull out the name we need.)
8023 */
8024 if (IsA(var, RowExpr))
8025 {
8026 RowExpr *r = (RowExpr *) var;
8027
8028 if (fieldno > 0 && fieldno <= list_length(r->colnames))
8029 return strVal(list_nth(r->colnames, fieldno - 1));
8030 }
8031
8032 /*
8033 * If it's a Param of type RECORD, try to find what the Param refers to.
8034 */
8035 if (IsA(var, Param))
8036 {
8037 Param *param = (Param *) var;
8038 ListCell *ancestor_cell;
8039
8040 expr = find_param_referent(param, context, &dpns, &ancestor_cell);
8041 if (expr)
8042 {
8043 /* Found a match, so recurse to decipher the field name */
8044 deparse_namespace save_dpns;
8045 const char *result;
8046
8047 push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
8048 result = get_name_for_var_field((Var *) expr, fieldno,
8049 0, context);
8050 pop_ancestor_plan(dpns, &save_dpns);
8051 return result;
8052 }
8053 }
8054
8055 /*
8056 * If it's a Var of type RECORD, we have to find what the Var refers to;
8057 * if not, we can use get_expr_result_tupdesc().
8058 */
8059 if (!IsA(var, Var) ||
8060 var->vartype != RECORDOID)
8061 {
8062 tupleDesc = get_expr_result_tupdesc((Node *) var, false);
8063 /* Got the tupdesc, so we can extract the field name */
8064 Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
8065 return NameStr(TupleDescAttr(tupleDesc, fieldno - 1)->attname);
8066 }
8067
8068 /* Find appropriate nesting depth */
8069 netlevelsup = var->varlevelsup + levelsup;
8070 if (netlevelsup >= list_length(context->namespaces))
8071 elog(ERROR, "bogus varlevelsup: %d offset %d",
8072 var->varlevelsup, levelsup);
8073 dpns = (deparse_namespace *) list_nth(context->namespaces,
8074 netlevelsup);
8075
8076 /*
8077 * If we have a syntactic referent for the Var, and we're working from a
8078 * parse tree, prefer to use the syntactic referent. Otherwise, fall back
8079 * on the semantic referent. (See comments in get_variable().)
8080 */
8081 if (var->varnosyn > 0 && dpns->plan == NULL)
8082 {
8083 varno = var->varnosyn;
8084 varattno = var->varattnosyn;
8085 }
8086 else
8087 {
8088 varno = var->varno;
8089 varattno = var->varattno;
8090 }
8091
8092 /*
8093 * Try to find the relevant RTE in this rtable. In a plan tree, it's
8094 * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
8095 * down into the subplans, or INDEX_VAR, which is resolved similarly.
8096 *
8097 * Note: unlike get_variable and resolve_special_varno, we need not worry
8098 * about inheritance mapping: a child Var should have the same datatype as
8099 * its parent, and here we're really only interested in the Var's type.
8100 */
8101 if (varno >= 1 && varno <= list_length(dpns->rtable))
8102 {
8103 rte = rt_fetch(varno, dpns->rtable);
8104 attnum = varattno;
8105 }
8106 else if (varno == OUTER_VAR && dpns->outer_tlist)
8107 {
8108 TargetEntry *tle;
8109 deparse_namespace save_dpns;
8110 const char *result;
8111
8112 tle = get_tle_by_resno(dpns->outer_tlist, varattno);
8113 if (!tle)
8114 elog(ERROR, "bogus varattno for OUTER_VAR var: %d", varattno);
8115
8116 Assert(netlevelsup == 0);
8117 push_child_plan(dpns, dpns->outer_plan, &save_dpns);
8118
8119 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8120 levelsup, context);
8121
8122 pop_child_plan(dpns, &save_dpns);
8123 return result;
8124 }
8125 else if (varno == INNER_VAR && dpns->inner_tlist)
8126 {
8127 TargetEntry *tle;
8128 deparse_namespace save_dpns;
8129 const char *result;
8130
8131 tle = get_tle_by_resno(dpns->inner_tlist, varattno);
8132 if (!tle)
8133 elog(ERROR, "bogus varattno for INNER_VAR var: %d", varattno);
8134
8135 Assert(netlevelsup == 0);
8136 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8137
8138 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8139 levelsup, context);
8140
8141 pop_child_plan(dpns, &save_dpns);
8142 return result;
8143 }
8144 else if (varno == INDEX_VAR && dpns->index_tlist)
8145 {
8146 TargetEntry *tle;
8147 const char *result;
8148
8149 tle = get_tle_by_resno(dpns->index_tlist, varattno);
8150 if (!tle)
8151 elog(ERROR, "bogus varattno for INDEX_VAR var: %d", varattno);
8152
8153 Assert(netlevelsup == 0);
8154
8155 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8156 levelsup, context);
8157
8158 return result;
8159 }
8160 else
8161 {
8162 elog(ERROR, "bogus varno: %d", varno);
8163 return NULL; /* keep compiler quiet */
8164 }
8165
8167 {
8168 /* Var is whole-row reference to RTE, so select the right field */
8169 return get_rte_attribute_name(rte, fieldno);
8170 }
8171
8172 /*
8173 * This part has essentially the same logic as the parser's
8174 * expandRecordVariable() function, but we are dealing with a different
8175 * representation of the input context, and we only need one field name
8176 * not a TupleDesc. Also, we need special cases for finding subquery and
8177 * CTE subplans when deparsing Plan trees.
8178 */
8179 expr = (Node *) var; /* default if we can't drill down */
8180
8181 switch (rte->rtekind)
8182 {
8183 case RTE_RELATION:
8184 case RTE_VALUES:
8186 case RTE_RESULT:
8187
8188 /*
8189 * This case should not occur: a column of a table, values list,
8190 * or ENR shouldn't have type RECORD. Fall through and fail (most
8191 * likely) at the bottom.
8192 */
8193 break;
8194 case RTE_SUBQUERY:
8195 /* Subselect-in-FROM: examine sub-select's output expr */
8196 {
8197 if (rte->subquery)
8198 {
8200 attnum);
8201
8202 if (ste == NULL || ste->resjunk)
8203 elog(ERROR, "subquery %s does not have attribute %d",
8204 rte->eref->aliasname, attnum);
8205 expr = (Node *) ste->expr;
8206 if (IsA(expr, Var))
8207 {
8208 /*
8209 * Recurse into the sub-select to see what its Var
8210 * refers to. We have to build an additional level of
8211 * namespace to keep in step with varlevelsup in the
8212 * subselect; furthermore, the subquery RTE might be
8213 * from an outer query level, in which case the
8214 * namespace for the subselect must have that outer
8215 * level as parent namespace.
8216 */
8217 List *save_nslist = context->namespaces;
8218 List *parent_namespaces;
8219 deparse_namespace mydpns;
8220 const char *result;
8221
8222 parent_namespaces = list_copy_tail(context->namespaces,
8223 netlevelsup);
8224
8225 set_deparse_for_query(&mydpns, rte->subquery,
8226 parent_namespaces);
8227
8228 context->namespaces = lcons(&mydpns, parent_namespaces);
8229
8230 result = get_name_for_var_field((Var *) expr, fieldno,
8231 0, context);
8232
8233 context->namespaces = save_nslist;
8234
8235 return result;
8236 }
8237 /* else fall through to inspect the expression */
8238 }
8239 else
8240 {
8241 /*
8242 * We're deparsing a Plan tree so we don't have complete
8243 * RTE entries (in particular, rte->subquery is NULL). But
8244 * the only place we'd normally see a Var directly
8245 * referencing a SUBQUERY RTE is in a SubqueryScan plan
8246 * node, and we can look into the child plan's tlist
8247 * instead. An exception occurs if the subquery was
8248 * proven empty and optimized away: then we'd find such a
8249 * Var in a childless Result node, and there's nothing in
8250 * the plan tree that would let us figure out what it had
8251 * originally referenced. In that case, fall back on
8252 * printing "fN", analogously to the default column names
8253 * for RowExprs.
8254 */
8255 TargetEntry *tle;
8256 deparse_namespace save_dpns;
8257 const char *result;
8258
8259 if (!dpns->inner_plan)
8260 {
8261 char *dummy_name = palloc(32);
8262
8263 Assert(dpns->plan && IsA(dpns->plan, Result));
8264 snprintf(dummy_name, 32, "f%d", fieldno);
8265 return dummy_name;
8266 }
8267 Assert(dpns->plan && IsA(dpns->plan, SubqueryScan));
8268
8269 tle = get_tle_by_resno(dpns->inner_tlist, attnum);
8270 if (!tle)
8271 elog(ERROR, "bogus varattno for subquery var: %d",
8272 attnum);
8273 Assert(netlevelsup == 0);
8274 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8275
8276 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8277 levelsup, context);
8278
8279 pop_child_plan(dpns, &save_dpns);
8280 return result;
8281 }
8282 }
8283 break;
8284 case RTE_JOIN:
8285 /* Join RTE --- recursively inspect the alias variable */
8286 if (rte->joinaliasvars == NIL)
8287 elog(ERROR, "cannot decompile join alias var in plan tree");
8288 Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
8289 expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
8290 Assert(expr != NULL);
8291 /* we intentionally don't strip implicit coercions here */
8292 if (IsA(expr, Var))
8293 return get_name_for_var_field((Var *) expr, fieldno,
8294 var->varlevelsup + levelsup,
8295 context);
8296 /* else fall through to inspect the expression */
8297 break;
8298 case RTE_FUNCTION:
8299 case RTE_TABLEFUNC:
8300
8301 /*
8302 * We couldn't get here unless a function is declared with one of
8303 * its result columns as RECORD, which is not allowed.
8304 */
8305 break;
8306 case RTE_CTE:
8307 /* CTE reference: examine subquery's output expr */
8308 {
8309 CommonTableExpr *cte = NULL;
8310 Index ctelevelsup;
8311 ListCell *lc;
8312
8313 /*
8314 * Try to find the referenced CTE using the namespace stack.
8315 */
8316 ctelevelsup = rte->ctelevelsup + netlevelsup;
8317 if (ctelevelsup >= list_length(context->namespaces))
8318 lc = NULL;
8319 else
8320 {
8321 deparse_namespace *ctedpns;
8322
8323 ctedpns = (deparse_namespace *)
8324 list_nth(context->namespaces, ctelevelsup);
8325 foreach(lc, ctedpns->ctes)
8326 {
8327 cte = (CommonTableExpr *) lfirst(lc);
8328 if (strcmp(cte->ctename, rte->ctename) == 0)
8329 break;
8330 }
8331 }
8332 if (lc != NULL)
8333 {
8334 Query *ctequery = (Query *) cte->ctequery;
8336 attnum);
8337
8338 if (ste == NULL || ste->resjunk)
8339 elog(ERROR, "CTE %s does not have attribute %d",
8340 rte->eref->aliasname, attnum);
8341 expr = (Node *) ste->expr;
8342 if (IsA(expr, Var))
8343 {
8344 /*
8345 * Recurse into the CTE to see what its Var refers to.
8346 * We have to build an additional level of namespace
8347 * to keep in step with varlevelsup in the CTE;
8348 * furthermore it could be an outer CTE (compare
8349 * SUBQUERY case above).
8350 */
8351 List *save_nslist = context->namespaces;
8352 List *parent_namespaces;
8353 deparse_namespace mydpns;
8354 const char *result;
8355
8356 parent_namespaces = list_copy_tail(context->namespaces,
8357 ctelevelsup);
8358
8359 set_deparse_for_query(&mydpns, ctequery,
8360 parent_namespaces);
8361
8362 context->namespaces = lcons(&mydpns, parent_namespaces);
8363
8364 result = get_name_for_var_field((Var *) expr, fieldno,
8365 0, context);
8366
8367 context->namespaces = save_nslist;
8368
8369 return result;
8370 }
8371 /* else fall through to inspect the expression */
8372 }
8373 else
8374 {
8375 /*
8376 * We're deparsing a Plan tree so we don't have a CTE
8377 * list. But the only places we'd normally see a Var
8378 * directly referencing a CTE RTE are in CteScan or
8379 * WorkTableScan plan nodes. For those cases,
8380 * set_deparse_plan arranged for dpns->inner_plan to be
8381 * the plan node that emits the CTE or RecursiveUnion
8382 * result, and we can look at its tlist instead. As
8383 * above, this can fail if the CTE has been proven empty,
8384 * in which case fall back to "fN".
8385 */
8386 TargetEntry *tle;
8387 deparse_namespace save_dpns;
8388 const char *result;
8389
8390 if (!dpns->inner_plan)
8391 {
8392 char *dummy_name = palloc(32);
8393
8394 Assert(dpns->plan && IsA(dpns->plan, Result));
8395 snprintf(dummy_name, 32, "f%d", fieldno);
8396 return dummy_name;
8397 }
8398 Assert(dpns->plan && (IsA(dpns->plan, CteScan) ||
8399 IsA(dpns->plan, WorkTableScan)));
8400
8401 tle = get_tle_by_resno(dpns->inner_tlist, attnum);
8402 if (!tle)
8403 elog(ERROR, "bogus varattno for subquery var: %d",
8404 attnum);
8405 Assert(netlevelsup == 0);
8406 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
8407
8408 result = get_name_for_var_field((Var *) tle->expr, fieldno,
8409 levelsup, context);
8410
8411 pop_child_plan(dpns, &save_dpns);
8412 return result;
8413 }
8414 }
8415 break;
8416 case RTE_GROUP:
8417
8418 /*
8419 * We couldn't get here: any Vars that reference the RTE_GROUP RTE
8420 * should have been replaced with the underlying grouping
8421 * expressions.
8422 */
8423 break;
8424 }
8425
8426 /*
8427 * We now have an expression we can't expand any more, so see if
8428 * get_expr_result_tupdesc() can do anything with it.
8429 */
8430 tupleDesc = get_expr_result_tupdesc(expr, false);
8431 /* Got the tupdesc, so we can extract the field name */
8432 Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
8433 return NameStr(TupleDescAttr(tupleDesc, fieldno - 1)->attname);
8434}
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1613
List * lcons(void *datum, List *list)
Definition: list.c:495
void * palloc(Size size)
Definition: mcxt.c:1317
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define GetCTETargetList(cte)
Definition: parsenodes.h:1714
@ RTE_JOIN
Definition: parsenodes.h:1028
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1033
@ RTE_RESULT
Definition: parsenodes.h:1034
@ RTE_GROUP
Definition: parsenodes.h:1037
int16 attnum
Definition: pg_attribute.h:74
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define snprintf
Definition: port.h:239
#define OUTER_VAR
Definition: primnodes.h:243
#define INNER_VAR
Definition: primnodes.h:242
#define INDEX_VAR
Definition: primnodes.h:244
static void set_deparse_for_query(deparse_namespace *dpns, Query *query, List *parent_namespaces)
Definition: ruleutils.c:4029
static const char * get_name_for_var_field(Var *var, int fieldno, int levelsup, deparse_context *context)
Definition: ruleutils.c:8007
static void pop_ancestor_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
Definition: ruleutils.c:5331
static void push_ancestor_plan(deparse_namespace *dpns, ListCell *ancestor_cell, deparse_namespace *save_dpns)
Definition: ruleutils.c:5310
static Node * find_param_referent(Param *param, deparse_context *context, deparse_namespace **dpns_p, ListCell **ancestor_cell_p)
Definition: ruleutils.c:8445
static void pop_child_plan(deparse_namespace *dpns, deparse_namespace *save_dpns)
Definition: ruleutils.c:5280
static void push_child_plan(deparse_namespace *dpns, Plan *plan, deparse_namespace *save_dpns)
Definition: ruleutils.c:5263
Index ctelevelsup
Definition: parsenodes.h:1207
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294
List * inner_tlist
Definition: ruleutils.c:181
List * outer_tlist
Definition: ruleutils.c:180
List * index_tlist
Definition: ruleutils.c:182
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:154

References Assert(), attname, attnum, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, deparse_namespace::ctes, elog, ERROR, TargetEntry::expr, find_param_referent(), get_expr_result_tupdesc(), get_name_for_var_field(), get_rte_attribute_name(), get_tle_by_resno(), GetCTETargetList, if(), deparse_namespace::index_tlist, INDEX_VAR, deparse_namespace::inner_plan, deparse_namespace::inner_tlist, INNER_VAR, InvalidAttrNumber, IsA, lcons(), lfirst, list_copy_tail(), list_length(), list_nth(), deparse_context::namespaces, NameStr, NIL, deparse_namespace::outer_plan, deparse_namespace::outer_tlist, OUTER_VAR, palloc(), deparse_namespace::plan, pop_ancestor_plan(), pop_child_plan(), push_ancestor_plan(), push_child_plan(), rt_fetch, deparse_namespace::rtable, RTE_CTE, RTE_FUNCTION, RTE_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, set_deparse_for_query(), snprintf, strVal, RangeTblEntry::subquery, Query::targetList, TupleDescAttr(), Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by get_name_for_var_field(), and get_rule_expr().

◆ get_opclass_name()

static void get_opclass_name ( Oid  opclass,
Oid  actual_datatype,
StringInfo  buf 
)
static

Definition at line 12851 of file ruleutils.c.

12853{
12854 HeapTuple ht_opc;
12855 Form_pg_opclass opcrec;
12856 char *opcname;
12857 char *nspname;
12858
12859 ht_opc = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
12860 if (!HeapTupleIsValid(ht_opc))
12861 elog(ERROR, "cache lookup failed for opclass %u", opclass);
12862 opcrec = (Form_pg_opclass) GETSTRUCT(ht_opc);
12863
12864 if (!OidIsValid(actual_datatype) ||
12865 GetDefaultOpClass(actual_datatype, opcrec->opcmethod) != opclass)
12866 {
12867 /* Okay, we need the opclass name. Do we need to qualify it? */
12868 opcname = NameStr(opcrec->opcname);
12869 if (OpclassIsVisible(opclass))
12870 appendStringInfo(buf, " %s", quote_identifier(opcname));
12871 else
12872 {
12873 nspname = get_namespace_name_or_temp(opcrec->opcnamespace);
12874 appendStringInfo(buf, " %s.%s",
12875 quote_identifier(nspname),
12876 quote_identifier(opcname));
12877 }
12878 }
12879 ReleaseSysCache(ht_opc);
12880}
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition: indexcmds.c:2343
bool OpclassIsVisible(Oid opcid)
Definition: namespace.c:2154
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83

References appendStringInfo(), buf, elog, ERROR, get_namespace_name_or_temp(), GetDefaultOpClass(), GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), OidIsValid, OpclassIsVisible(), quote_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by generate_opclass_name(), get_rule_expr(), pg_get_indexdef_worker(), and pg_get_partkeydef_worker().

◆ get_oper_expr()

static void get_oper_expr ( OpExpr expr,
deparse_context context 
)
static

Definition at line 10725 of file ruleutils.c.

10726{
10727 StringInfo buf = context->buf;
10728 Oid opno = expr->opno;
10729 List *args = expr->args;
10730
10731 if (!PRETTY_PAREN(context))
10733 if (list_length(args) == 2)
10734 {
10735 /* binary operator */
10736 Node *arg1 = (Node *) linitial(args);
10737 Node *arg2 = (Node *) lsecond(args);
10738
10739 get_rule_expr_paren(arg1, context, true, (Node *) expr);
10740 appendStringInfo(buf, " %s ",
10742 exprType(arg1),
10743 exprType(arg2)));
10744 get_rule_expr_paren(arg2, context, true, (Node *) expr);
10745 }
10746 else
10747 {
10748 /* prefix operator */
10749 Node *arg = (Node *) linitial(args);
10750
10751 appendStringInfo(buf, "%s ",
10753 InvalidOid,
10754 exprType(arg)));
10755 get_rule_expr_paren(arg, context, true, (Node *) expr);
10756 }
10757 if (!PRETTY_PAREN(context))
10759}
static char * generate_operator_name(Oid operid, Oid arg1, Oid arg2)
Definition: ruleutils.c:13352
Oid opno
Definition: primnodes.h:835
List * args
Definition: primnodes.h:853

References appendStringInfo(), appendStringInfoChar(), arg, generate_unaccent_rules::args, OpExpr::args, deparse_context::buf, buf, exprType(), generate_operator_name(), get_rule_expr_paren(), InvalidOid, linitial, list_length(), lsecond, OpExpr::opno, and PRETTY_PAREN.

Referenced by get_rule_expr().

◆ get_parameter()

static void get_parameter ( Param param,
deparse_context context 
)
static

Definition at line 8677 of file ruleutils.c.

8678{
8679 Node *expr;
8680 deparse_namespace *dpns;
8681 ListCell *ancestor_cell;
8682 SubPlan *subplan;
8683 int column;
8684
8685 /*
8686 * If it's a PARAM_EXEC parameter, try to locate the expression from which
8687 * the parameter was computed. This stanza handles only cases in which
8688 * the Param represents an input to the subplan we are currently in.
8689 */
8690 expr = find_param_referent(param, context, &dpns, &ancestor_cell);
8691 if (expr)
8692 {
8693 /* Found a match, so print it */
8694 deparse_namespace save_dpns;
8695 bool save_varprefix;
8696 bool need_paren;
8697
8698 /* Switch attention to the ancestor plan node */
8699 push_ancestor_plan(dpns, ancestor_cell, &save_dpns);
8700
8701 /*
8702 * Force prefixing of Vars, since they won't belong to the relation
8703 * being scanned in the original plan node.
8704 */
8705 save_varprefix = context->varprefix;
8706 context->varprefix = true;
8707
8708 /*
8709 * A Param's expansion is typically a Var, Aggref, GroupingFunc, or
8710 * upper-level Param, which wouldn't need extra parentheses.
8711 * Otherwise, insert parens to ensure the expression looks atomic.
8712 */
8713 need_paren = !(IsA(expr, Var) ||
8714 IsA(expr, Aggref) ||
8715 IsA(expr, GroupingFunc) ||
8716 IsA(expr, Param));
8717 if (need_paren)
8718 appendStringInfoChar(context->buf, '(');
8719
8720 get_rule_expr(expr, context, false);
8721
8722 if (need_paren)
8723 appendStringInfoChar(context->buf, ')');
8724
8725 context->varprefix = save_varprefix;
8726
8727 pop_ancestor_plan(dpns, &save_dpns);
8728
8729 return;
8730 }
8731
8732 /*
8733 * Alternatively, maybe it's a subplan output, which we print as a
8734 * reference to the subplan. (We could drill down into the subplan and
8735 * print the relevant targetlist expression, but that has been deemed too
8736 * confusing since it would violate normal SQL scope rules. Also, we're
8737 * relying on this reference to show that the testexpr containing the
8738 * Param has anything to do with that subplan at all.)
8739 */
8740 subplan = find_param_generator(param, context, &column);
8741 if (subplan)
8742 {
8743 appendStringInfo(context->buf, "(%s%s).col%d",
8744 subplan->useHashTable ? "hashed " : "",
8745 subplan->plan_name, column + 1);
8746
8747 return;
8748 }
8749
8750 /*
8751 * If it's an external parameter, see if the outermost namespace provides
8752 * function argument names.
8753 */
8754 if (param->paramkind == PARAM_EXTERN && context->namespaces != NIL)
8755 {
8756 dpns = llast(context->namespaces);
8757 if (dpns->argnames &&
8758 param->paramid > 0 &&
8759 param->paramid <= dpns->numargs)
8760 {
8761 char *argname = dpns->argnames[param->paramid - 1];
8762
8763 if (argname)
8764 {
8765 bool should_qualify = false;
8766 ListCell *lc;
8767
8768 /*
8769 * Qualify the parameter name if there are any other deparse
8770 * namespaces with range tables. This avoids qualifying in
8771 * trivial cases like "RETURN a + b", but makes it safe in all
8772 * other cases.
8773 */
8774 foreach(lc, context->namespaces)
8775 {
8776 deparse_namespace *depns = lfirst(lc);
8777
8778 if (depns->rtable_names != NIL)
8779 {
8780 should_qualify = true;
8781 break;
8782 }
8783 }
8784 if (should_qualify)
8785 {
8787 appendStringInfoChar(context->buf, '.');
8788 }
8789
8790 appendStringInfoString(context->buf, quote_identifier(argname));
8791 return;
8792 }
8793 }
8794 }
8795
8796 /*
8797 * Not PARAM_EXEC, or couldn't find referent: just print $N.
8798 *
8799 * It's a bug if we get here for anything except PARAM_EXTERN Params, but
8800 * in production builds printing $N seems more useful than failing.
8801 */
8802 Assert(param->paramkind == PARAM_EXTERN);
8803
8804 appendStringInfo(context->buf, "$%d", param->paramid);
8805}
#define llast(l)
Definition: pg_list.h:198
@ PARAM_EXTERN
Definition: primnodes.h:384
static SubPlan * find_param_generator(Param *param, deparse_context *context, int *column_p)
Definition: ruleutils.c:8559
char * plan_name
Definition: primnodes.h:1089
bool useHashTable
Definition: primnodes.h:1096
char ** argnames
Definition: ruleutils.c:186

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), deparse_namespace::argnames, Assert(), deparse_context::buf, find_param_generator(), find_param_referent(), deparse_namespace::funcname, get_rule_expr(), IsA, lfirst, llast, deparse_context::namespaces, NIL, deparse_namespace::numargs, PARAM_EXTERN, Param::paramid, Param::paramkind, SubPlan::plan_name, pop_ancestor_plan(), push_ancestor_plan(), quote_identifier(), deparse_namespace::rtable_names, SubPlan::useHashTable, and deparse_context::varprefix.

Referenced by get_rule_expr().

◆ get_query_def()

static void get_query_def ( Query query,
StringInfo  buf,
List parentnamespace,
TupleDesc  resultDesc,
bool  colNamesVisible,
int  prettyFlags,
int  wrapColumn,
int  startIndent 
)
static

Definition at line 5624 of file ruleutils.c.

5627{
5628 deparse_context context;
5629 deparse_namespace dpns;
5630 int rtable_size;
5631
5632 /* Guard against excessively long or deeply-nested queries */
5635
5636 rtable_size = query->hasGroupRTE ?
5637 list_length(query->rtable) - 1 :
5638 list_length(query->rtable);
5639
5640 /*
5641 * Replace any Vars in the query's targetlist and havingQual that
5642 * reference GROUP outputs with the underlying grouping expressions.
5643 */
5644 if (query->hasGroupRTE)
5645 {
5646 query->targetList = (List *)
5647 flatten_group_exprs(NULL, query, (Node *) query->targetList);
5648 query->havingQual =
5649 flatten_group_exprs(NULL, query, query->havingQual);
5650 }
5651
5652 /*
5653 * Before we begin to examine the query, acquire locks on referenced
5654 * relations, and fix up deleted columns in JOIN RTEs. This ensures
5655 * consistent results. Note we assume it's OK to scribble on the passed
5656 * querytree!
5657 *
5658 * We are only deparsing the query (we are not about to execute it), so we
5659 * only need AccessShareLock on the relations it mentions.
5660 */
5661 AcquireRewriteLocks(query, false, false);
5662
5663 context.buf = buf;
5664 context.namespaces = lcons(&dpns, list_copy(parentnamespace));
5665 context.resultDesc = NULL;
5666 context.targetList = NIL;
5667 context.windowClause = NIL;
5668 context.varprefix = (parentnamespace != NIL ||
5669 rtable_size != 1);
5670 context.prettyFlags = prettyFlags;
5671 context.wrapColumn = wrapColumn;
5672 context.indentLevel = startIndent;
5673 context.colNamesVisible = colNamesVisible;
5674 context.inGroupBy = false;
5675 context.varInOrderBy = false;
5676 context.appendparents = NULL;
5677
5678 set_deparse_for_query(&dpns, query, parentnamespace);
5679
5680 switch (query->commandType)
5681 {
5682 case CMD_SELECT:
5683 /* We set context.resultDesc only if it's a SELECT */
5684 context.resultDesc = resultDesc;
5685 get_select_query_def(query, &context);
5686 break;
5687
5688 case CMD_UPDATE:
5689 get_update_query_def(query, &context);
5690 break;
5691
5692 case CMD_INSERT:
5693 get_insert_query_def(query, &context);
5694 break;
5695
5696 case CMD_DELETE:
5697 get_delete_query_def(query, &context);
5698 break;
5699
5700 case CMD_MERGE:
5701 get_merge_query_def(query, &context);
5702 break;
5703
5704 case CMD_NOTHING:
5705 appendStringInfoString(buf, "NOTHING");
5706 break;
5707
5708 case CMD_UTILITY:
5709 get_utility_query_def(query, &context);
5710 break;
5711
5712 default:
5713 elog(ERROR, "unrecognized query command type: %d",
5714 query->commandType);
5715 break;
5716 }
5717}
List * list_copy(const List *oldlist)
Definition: list.c:1573
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
@ CMD_MERGE
Definition: nodes.h:271
@ CMD_UTILITY
Definition: nodes.h:272
@ CMD_SELECT
Definition: nodes.h:267
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
static void get_delete_query_def(Query *query, deparse_context *context)
Definition: ruleutils.c:7345
static void get_insert_query_def(Query *query, deparse_context *context)
Definition: ruleutils.c:6929
static void get_utility_query_def(Query *query, deparse_context *context)
Definition: ruleutils.c:7551
static void get_merge_query_def(Query *query, deparse_context *context)
Definition: ruleutils.c:7392
static void get_update_query_def(Query *query, deparse_context *context)
Definition: ruleutils.c:7141
static void get_select_query_def(Query *query, deparse_context *context)
Definition: ruleutils.c:5906
void check_stack_depth(void)
Definition: stack_depth.c:95
CmdType commandType
Definition: parsenodes.h:121
Node * flatten_group_exprs(PlannerInfo *root, Query *query, Node *node)
Definition: var.c:968

References AcquireRewriteLocks(), deparse_context::appendparents, appendStringInfoString(), deparse_context::buf, buf, CHECK_FOR_INTERRUPTS, check_stack_depth(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_SELECT, CMD_UPDATE, CMD_UTILITY, deparse_context::colNamesVisible, Query::commandType, elog, ERROR, flatten_group_exprs(), get_delete_query_def(), get_insert_query_def(), get_merge_query_def(), get_select_query_def(), get_update_query_def(), get_utility_query_def(), Query::havingQual, deparse_context::indentLevel, deparse_context::inGroupBy, lcons(), list_copy(), list_length(), deparse_context::namespaces, NIL, deparse_context::prettyFlags, deparse_context::resultDesc, Query::rtable, set_deparse_for_query(), deparse_context::targetList, Query::targetList, deparse_context::varInOrderBy, deparse_context::varprefix, deparse_context::windowClause, and deparse_context::wrapColumn.

Referenced by get_from_clause_item(), get_insert_query_def(), get_setop_query(), get_sublink_expr(), get_with_clause(), make_ruledef(), make_viewdef(), pg_get_querydef(), and print_function_sqlbody().

◆ get_range_partbound_string()

char * get_range_partbound_string ( List bound_datums)

Definition at line 13666 of file ruleutils.c.

13667{
13668 deparse_context context;
13670 ListCell *cell;
13671 char *sep;
13672
13673 memset(&context, 0, sizeof(deparse_context));
13674 context.buf = buf;
13675
13677 sep = "";
13678 foreach(cell, bound_datums)
13679 {
13680 PartitionRangeDatum *datum =
13682
13685 appendStringInfoString(buf, "MINVALUE");
13686 else if (datum->kind == PARTITION_RANGE_DATUM_MAXVALUE)
13687 appendStringInfoString(buf, "MAXVALUE");
13688 else
13689 {
13690 Const *val = castNode(Const, datum->value);
13691
13692 get_const_expr(val, &context, -1);
13693 }
13694 sep = ", ";
13695 }
13697
13698 return buf->data;
13699}
long val
Definition: informix.c:689
@ PARTITION_RANGE_DATUM_MAXVALUE
Definition: parsenodes.h:937
@ PARTITION_RANGE_DATUM_MINVALUE
Definition: parsenodes.h:935
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
PartitionRangeDatumKind kind
Definition: parsenodes.h:944

References appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, castNode, get_const_expr(), PartitionRangeDatum::kind, lfirst_node, makeStringInfo(), PARTITION_RANGE_DATUM_MAXVALUE, PARTITION_RANGE_DATUM_MINVALUE, val, and PartitionRangeDatum::value.

Referenced by check_new_partition_bound(), and get_rule_expr().

◆ get_relation_name()

static char * get_relation_name ( Oid  relid)
static

Definition at line 13123 of file ruleutils.c.

13124{
13125 char *relname = get_rel_name(relid);
13126
13127 if (!relname)
13128 elog(ERROR, "cache lookup failed for relation %u", relid);
13129 return relname;
13130}
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2011

References elog, ERROR, get_rel_name(), and relname.

Referenced by get_rte_alias(), pg_get_constraintdef_worker(), pg_get_indexdef_worker(), pg_get_partition_constraintdef(), pg_get_partkeydef_worker(), pg_get_statisticsobj_worker(), and pg_get_statisticsobjdef_expressions().

◆ get_reloptions()

static void get_reloptions ( StringInfo  buf,
Datum  reloptions 
)
static

Definition at line 13578 of file ruleutils.c.

13579{
13580 Datum *options;
13581 int noptions;
13582 int i;
13583
13584 deconstruct_array_builtin(DatumGetArrayTypeP(reloptions), TEXTOID,
13585 &options, NULL, &noptions);
13586
13587 for (i = 0; i < noptions; i++)
13588 {
13590 char *name;
13591 char *separator;
13592 char *value;
13593
13594 /*
13595 * Each array element should have the form name=value. If the "=" is
13596 * missing for some reason, treat it like an empty value.
13597 */
13598 name = option;
13599 separator = strchr(option, '=');
13600 if (separator)
13601 {
13602 *separator = '\0';
13603 value = separator + 1;
13604 }
13605 else
13606 value = "";
13607
13608 if (i > 0)
13611
13612 /*
13613 * In general we need to quote the value; but to avoid unnecessary
13614 * clutter, do not quote if it is an identifier that would not need
13615 * quoting. (We could also allow numbers, but that is a bit trickier
13616 * than it looks --- for example, are leading zeroes significant? We
13617 * don't want to assume very much here about what custom reloptions
13618 * might mean.)
13619 */
13622 else
13624
13625 pfree(option);
13626 }
13627}
static struct @165 value
static size_t noptions

References appendStringInfo(), appendStringInfoString(), buf, DatumGetArrayTypeP, deconstruct_array_builtin(), i, name, noptions, options, pfree(), quote_identifier(), simple_quote_literal(), TextDatumGetCString, and value.

Referenced by flatten_reloptions(), and pg_get_indexdef_worker().

◆ get_returning_clause()

static void get_returning_clause ( Query query,
deparse_context context 
)
static

Definition at line 6365 of file ruleutils.c.

6366{
6367 StringInfo buf = context->buf;
6368
6369 if (query->returningList)
6370 {
6371 bool have_with = false;
6372
6373 appendContextKeyword(context, " RETURNING",
6375
6376 /* Add WITH (OLD/NEW) options, if they're not the defaults */
6377 if (query->returningOldAlias && strcmp(query->returningOldAlias, "old") != 0)
6378 {
6379 appendStringInfo(buf, " WITH (OLD AS %s",
6380 quote_identifier(query->returningOldAlias));
6381 have_with = true;
6382 }
6383 if (query->returningNewAlias && strcmp(query->returningNewAlias, "new") != 0)
6384 {
6385 if (have_with)
6386 appendStringInfo(buf, ", NEW AS %s",
6387 quote_identifier(query->returningNewAlias));
6388 else
6389 {
6390 appendStringInfo(buf, " WITH (NEW AS %s",
6391 quote_identifier(query->returningNewAlias));
6392 have_with = true;
6393 }
6394 }
6395 if (have_with)
6397
6398 /* Add the returning expressions themselves */
6399 get_target_list(query->returningList, context);
6400 }
6401}

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), deparse_context::buf, buf, get_target_list(), PRETTYINDENT_STD, quote_identifier(), and Query::returningList.

Referenced by get_delete_query_def(), get_insert_query_def(), get_merge_query_def(), and get_update_query_def().

◆ get_rtable_name()

static char * get_rtable_name ( int  rtindex,
deparse_context context 
)
static

Definition at line 5133 of file ruleutils.c.

5134{
5136
5137 Assert(rtindex > 0 && rtindex <= list_length(dpns->rtable_names));
5138 return (char *) list_nth(dpns->rtable_names, rtindex - 1);
5139}

References Assert(), linitial, list_length(), list_nth(), deparse_context::namespaces, and deparse_namespace::rtable_names.

Referenced by get_from_clause_item(), get_rte_alias(), and get_select_query_def().

◆ get_rte_alias()

static void get_rte_alias ( RangeTblEntry rte,
int  varno,
bool  use_as,
deparse_context context 
)
static

Definition at line 12645 of file ruleutils.c.

12647{
12649 char *refname = get_rtable_name(varno, context);
12650 deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
12651 bool printalias = false;
12652
12653 if (rte->alias != NULL)
12654 {
12655 /* Always print alias if user provided one */
12656 printalias = true;
12657 }
12658 else if (colinfo->printaliases)
12659 {
12660 /* Always print alias if we need to print column aliases */
12661 printalias = true;
12662 }
12663 else if (rte->rtekind == RTE_RELATION)
12664 {
12665 /*
12666 * No need to print alias if it's same as relation name (this would
12667 * normally be the case, but not if set_rtable_names had to resolve a
12668 * conflict).
12669 */
12670 if (strcmp(refname, get_relation_name(rte->relid)) != 0)
12671 printalias = true;
12672 }
12673 else if (rte->rtekind == RTE_FUNCTION)
12674 {
12675 /*
12676 * For a function RTE, always print alias. This covers possible
12677 * renaming of the function and/or instability of the FigureColname
12678 * rules for things that aren't simple functions. Note we'd need to
12679 * force it anyway for the columndef list case.
12680 */
12681 printalias = true;
12682 }
12683 else if (rte->rtekind == RTE_SUBQUERY ||
12684 rte->rtekind == RTE_VALUES)
12685 {
12686 /*
12687 * For a subquery, always print alias. This makes the output
12688 * SQL-spec-compliant, even though we allow such aliases to be omitted
12689 * on input.
12690 */
12691 printalias = true;
12692 }
12693 else if (rte->rtekind == RTE_CTE)
12694 {
12695 /*
12696 * No need to print alias if it's same as CTE name (this would
12697 * normally be the case, but not if set_rtable_names had to resolve a
12698 * conflict).
12699 */
12700 if (strcmp(refname, rte->ctename) != 0)
12701 printalias = true;
12702 }
12703
12704 if (printalias)
12705 appendStringInfo(context->buf, "%s%s",
12706 use_as ? " AS " : " ",
12707 quote_identifier(refname));
12708}
static char * get_relation_name(Oid relid)
Definition: ruleutils.c:13123

References appendStringInfo(), deparse_context::buf, RangeTblEntry::ctename, deparse_columns_fetch, get_relation_name(), get_rtable_name(), linitial, deparse_context::namespaces, deparse_columns::printaliases, quote_identifier(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, and RangeTblEntry::rtekind.

Referenced by get_delete_query_def(), get_from_clause_item(), get_insert_query_def(), get_merge_query_def(), and get_update_query_def().

◆ get_rule_expr()

static void get_rule_expr ( Node node,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 9242 of file ruleutils.c.

9244{
9245 StringInfo buf = context->buf;
9246
9247 if (node == NULL)
9248 return;
9249
9250 /* Guard against excessively long or deeply-nested queries */
9253
9254 /*
9255 * Each level of get_rule_expr must emit an indivisible term
9256 * (parenthesized if necessary) to ensure result is reparsed into the same
9257 * expression tree. The only exception is that when the input is a List,
9258 * we emit the component items comma-separated with no surrounding
9259 * decoration; this is convenient for most callers.
9260 */
9261 switch (nodeTag(node))
9262 {
9263 case T_Var:
9264 (void) get_variable((Var *) node, 0, false, context);
9265 break;
9266
9267 case T_Const:
9268 get_const_expr((Const *) node, context, 0);
9269 break;
9270
9271 case T_Param:
9272 get_parameter((Param *) node, context);
9273 break;
9274
9275 case T_Aggref:
9276 get_agg_expr((Aggref *) node, context, (Aggref *) node);
9277 break;
9278
9279 case T_GroupingFunc:
9280 {
9281 GroupingFunc *gexpr = (GroupingFunc *) node;
9282
9283 appendStringInfoString(buf, "GROUPING(");
9284 get_rule_expr((Node *) gexpr->args, context, true);
9286 }
9287 break;
9288
9289 case T_WindowFunc:
9290 get_windowfunc_expr((WindowFunc *) node, context);
9291 break;
9292
9293 case T_MergeSupportFunc:
9294 appendStringInfoString(buf, "MERGE_ACTION()");
9295 break;
9296
9297 case T_SubscriptingRef:
9298 {
9299 SubscriptingRef *sbsref = (SubscriptingRef *) node;
9300 bool need_parens;
9301
9302 /*
9303 * If the argument is a CaseTestExpr, we must be inside a
9304 * FieldStore, ie, we are assigning to an element of an array
9305 * within a composite column. Since we already punted on
9306 * displaying the FieldStore's target information, just punt
9307 * here too, and display only the assignment source
9308 * expression.
9309 */
9310 if (IsA(sbsref->refexpr, CaseTestExpr))
9311 {
9312 Assert(sbsref->refassgnexpr);
9313 get_rule_expr((Node *) sbsref->refassgnexpr,
9314 context, showimplicit);
9315 break;
9316 }
9317
9318 /*
9319 * Parenthesize the argument unless it's a simple Var or a
9320 * FieldSelect. (In particular, if it's another
9321 * SubscriptingRef, we *must* parenthesize to avoid
9322 * confusion.)
9323 */
9324 need_parens = !IsA(sbsref->refexpr, Var) &&
9325 !IsA(sbsref->refexpr, FieldSelect);
9326 if (need_parens)
9328 get_rule_expr((Node *) sbsref->refexpr, context, showimplicit);
9329 if (need_parens)
9331
9332 /*
9333 * If there's a refassgnexpr, we want to print the node in the
9334 * format "container[subscripts] := refassgnexpr". This is
9335 * not legal SQL, so decompilation of INSERT or UPDATE
9336 * statements should always use processIndirection as part of
9337 * the statement-level syntax. We should only see this when
9338 * EXPLAIN tries to print the targetlist of a plan resulting
9339 * from such a statement.
9340 */
9341 if (sbsref->refassgnexpr)
9342 {
9343 Node *refassgnexpr;
9344
9345 /*
9346 * Use processIndirection to print this node's subscripts
9347 * as well as any additional field selections or
9348 * subscripting in immediate descendants. It returns the
9349 * RHS expr that is actually being "assigned".
9350 */
9351 refassgnexpr = processIndirection(node, context);
9352 appendStringInfoString(buf, " := ");
9353 get_rule_expr(refassgnexpr, context, showimplicit);
9354 }
9355 else
9356 {
9357 /* Just an ordinary container fetch, so print subscripts */
9358 printSubscripts(sbsref, context);
9359 }
9360 }
9361 break;
9362
9363 case T_FuncExpr:
9364 get_func_expr((FuncExpr *) node, context, showimplicit);
9365 break;
9366
9367 case T_NamedArgExpr:
9368 {
9369 NamedArgExpr *na = (NamedArgExpr *) node;
9370
9371 appendStringInfo(buf, "%s => ", quote_identifier(na->name));
9372 get_rule_expr((Node *) na->arg, context, showimplicit);
9373 }
9374 break;
9375
9376 case T_OpExpr:
9377 get_oper_expr((OpExpr *) node, context);
9378 break;
9379
9380 case T_DistinctExpr:
9381 {
9382 DistinctExpr *expr = (DistinctExpr *) node;
9383 List *args = expr->args;
9384 Node *arg1 = (Node *) linitial(args);
9385 Node *arg2 = (Node *) lsecond(args);
9386
9387 if (!PRETTY_PAREN(context))
9389 get_rule_expr_paren(arg1, context, true, node);
9390 appendStringInfoString(buf, " IS DISTINCT FROM ");
9391 get_rule_expr_paren(arg2, context, true, node);
9392 if (!PRETTY_PAREN(context))
9394 }
9395 break;
9396
9397 case T_NullIfExpr:
9398 {
9399 NullIfExpr *nullifexpr = (NullIfExpr *) node;
9400
9401 appendStringInfoString(buf, "NULLIF(");
9402 get_rule_expr((Node *) nullifexpr->args, context, true);
9404 }
9405 break;
9406
9407 case T_ScalarArrayOpExpr:
9408 {
9409 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
9410 List *args = expr->args;
9411 Node *arg1 = (Node *) linitial(args);
9412 Node *arg2 = (Node *) lsecond(args);
9413
9414 if (!PRETTY_PAREN(context))
9416 get_rule_expr_paren(arg1, context, true, node);
9417 appendStringInfo(buf, " %s %s (",
9419 exprType(arg1),
9421 expr->useOr ? "ANY" : "ALL");
9422 get_rule_expr_paren(arg2, context, true, node);
9423
9424 /*
9425 * There's inherent ambiguity in "x op ANY/ALL (y)" when y is
9426 * a bare sub-SELECT. Since we're here, the sub-SELECT must
9427 * be meant as a scalar sub-SELECT yielding an array value to
9428 * be used in ScalarArrayOpExpr; but the grammar will
9429 * preferentially interpret such a construct as an ANY/ALL
9430 * SubLink. To prevent misparsing the output that way, insert
9431 * a dummy coercion (which will be stripped by parse analysis,
9432 * so no inefficiency is added in dump and reload). This is
9433 * indeed most likely what the user wrote to get the construct
9434 * accepted in the first place.
9435 */
9436 if (IsA(arg2, SubLink) &&
9437 ((SubLink *) arg2)->subLinkType == EXPR_SUBLINK)
9438 appendStringInfo(buf, "::%s",
9440 exprTypmod(arg2)));
9442 if (!PRETTY_PAREN(context))
9444 }
9445 break;
9446
9447 case T_BoolExpr:
9448 {
9449 BoolExpr *expr = (BoolExpr *) node;
9450 Node *first_arg = linitial(expr->args);
9451 ListCell *arg;
9452
9453 switch (expr->boolop)
9454 {
9455 case AND_EXPR:
9456 if (!PRETTY_PAREN(context))
9458 get_rule_expr_paren(first_arg, context,
9459 false, node);
9460 for_each_from(arg, expr->args, 1)
9461 {
9462 appendStringInfoString(buf, " AND ");
9463 get_rule_expr_paren((Node *) lfirst(arg), context,
9464 false, node);
9465 }
9466 if (!PRETTY_PAREN(context))
9468 break;
9469
9470 case OR_EXPR:
9471 if (!PRETTY_PAREN(context))
9473 get_rule_expr_paren(first_arg, context,
9474 false, node);
9475 for_each_from(arg, expr->args, 1)
9476 {
9477 appendStringInfoString(buf, " OR ");
9478 get_rule_expr_paren((Node *) lfirst(arg), context,
9479 false, node);
9480 }
9481 if (!PRETTY_PAREN(context))
9483 break;
9484
9485 case NOT_EXPR:
9486 if (!PRETTY_PAREN(context))
9488 appendStringInfoString(buf, "NOT ");
9489 get_rule_expr_paren(first_arg, context,
9490 false, node);
9491 if (!PRETTY_PAREN(context))
9493 break;
9494
9495 default:
9496 elog(ERROR, "unrecognized boolop: %d",
9497 (int) expr->boolop);
9498 }
9499 }
9500 break;
9501
9502 case T_SubLink:
9503 get_sublink_expr((SubLink *) node, context);
9504 break;
9505
9506 case T_SubPlan:
9507 {
9508 SubPlan *subplan = (SubPlan *) node;
9509
9510 /*
9511 * We cannot see an already-planned subplan in rule deparsing,
9512 * only while EXPLAINing a query plan. We don't try to
9513 * reconstruct the original SQL, just reference the subplan
9514 * that appears elsewhere in EXPLAIN's result. It does seem
9515 * useful to show the subLinkType and testexpr (if any), and
9516 * we also note whether the subplan will be hashed.
9517 */
9518 switch (subplan->subLinkType)
9519 {
9520 case EXISTS_SUBLINK:
9521 appendStringInfoString(buf, "EXISTS(");
9522 Assert(subplan->testexpr == NULL);
9523 break;
9524 case ALL_SUBLINK:
9525 appendStringInfoString(buf, "(ALL ");
9526 Assert(subplan->testexpr != NULL);
9527 break;
9528 case ANY_SUBLINK:
9529 appendStringInfoString(buf, "(ANY ");
9530 Assert(subplan->testexpr != NULL);
9531 break;
9532 case ROWCOMPARE_SUBLINK:
9533 /* Parenthesizing the testexpr seems sufficient */
9535 Assert(subplan->testexpr != NULL);
9536 break;
9537 case EXPR_SUBLINK:
9538 /* No need to decorate these subplan references */
9540 Assert(subplan->testexpr == NULL);
9541 break;
9542 case MULTIEXPR_SUBLINK:
9543 /* MULTIEXPR isn't executed in the normal way */
9544 appendStringInfoString(buf, "(rescan ");
9545 Assert(subplan->testexpr == NULL);
9546 break;
9547 case ARRAY_SUBLINK:
9548 appendStringInfoString(buf, "ARRAY(");
9549 Assert(subplan->testexpr == NULL);
9550 break;
9551 case CTE_SUBLINK:
9552 /* This case is unreachable within expressions */
9553 appendStringInfoString(buf, "CTE(");
9554 Assert(subplan->testexpr == NULL);
9555 break;
9556 }
9557
9558 if (subplan->testexpr != NULL)
9559 {
9560 deparse_namespace *dpns;
9561
9562 /*
9563 * Push SubPlan into ancestors list while deparsing
9564 * testexpr, so that we can handle PARAM_EXEC references
9565 * to the SubPlan's paramIds. (This makes it look like
9566 * the SubPlan is an "ancestor" of the current plan node,
9567 * which is a little weird, but it does no harm.) In this
9568 * path, we don't need to mention the SubPlan explicitly,
9569 * because the referencing Params will show its existence.
9570 */
9571 dpns = (deparse_namespace *) linitial(context->namespaces);
9572 dpns->ancestors = lcons(subplan, dpns->ancestors);
9573
9574 get_rule_expr(subplan->testexpr, context, showimplicit);
9576
9577 dpns->ancestors = list_delete_first(dpns->ancestors);
9578 }
9579 else
9580 {
9581 /* No referencing Params, so show the SubPlan's name */
9582 if (subplan->useHashTable)
9583 appendStringInfo(buf, "hashed %s)", subplan->plan_name);
9584 else
9585 appendStringInfo(buf, "%s)", subplan->plan_name);
9586 }
9587 }
9588 break;
9589
9590 case T_AlternativeSubPlan:
9591 {
9592 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
9593 ListCell *lc;
9594
9595 /*
9596 * This case cannot be reached in normal usage, since no
9597 * AlternativeSubPlan can appear either in parsetrees or
9598 * finished plan trees. We keep it just in case somebody
9599 * wants to use this code to print planner data structures.
9600 */
9601 appendStringInfoString(buf, "(alternatives: ");
9602 foreach(lc, asplan->subplans)
9603 {
9605
9606 if (splan->useHashTable)
9607 appendStringInfo(buf, "hashed %s", splan->plan_name);
9608 else
9609 appendStringInfoString(buf, splan->plan_name);
9610 if (lnext(asplan->subplans, lc))
9611 appendStringInfoString(buf, " or ");
9612 }
9614 }
9615 break;
9616
9617 case T_FieldSelect:
9618 {
9619 FieldSelect *fselect = (FieldSelect *) node;
9620 Node *arg = (Node *) fselect->arg;
9621 int fno = fselect->fieldnum;
9622 const char *fieldname;
9623 bool need_parens;
9624
9625 /*
9626 * Parenthesize the argument unless it's an SubscriptingRef or
9627 * another FieldSelect. Note in particular that it would be
9628 * WRONG to not parenthesize a Var argument; simplicity is not
9629 * the issue here, having the right number of names is.
9630 */
9631 need_parens = !IsA(arg, SubscriptingRef) &&
9632 !IsA(arg, FieldSelect);
9633 if (need_parens)
9635 get_rule_expr(arg, context, true);
9636 if (need_parens)
9638
9639 /*
9640 * Get and print the field name.
9641 */
9642 fieldname = get_name_for_var_field((Var *) arg, fno,
9643 0, context);
9644 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
9645 }
9646 break;
9647
9648 case T_FieldStore:
9649 {
9650 FieldStore *fstore = (FieldStore *) node;
9651 bool need_parens;
9652
9653 /*
9654 * There is no good way to represent a FieldStore as real SQL,
9655 * so decompilation of INSERT or UPDATE statements should
9656 * always use processIndirection as part of the
9657 * statement-level syntax. We should only get here when
9658 * EXPLAIN tries to print the targetlist of a plan resulting
9659 * from such a statement. The plan case is even harder than
9660 * ordinary rules would be, because the planner tries to
9661 * collapse multiple assignments to the same field or subfield
9662 * into one FieldStore; so we can see a list of target fields
9663 * not just one, and the arguments could be FieldStores
9664 * themselves. We don't bother to try to print the target
9665 * field names; we just print the source arguments, with a
9666 * ROW() around them if there's more than one. This isn't
9667 * terribly complete, but it's probably good enough for
9668 * EXPLAIN's purposes; especially since anything more would be
9669 * either hopelessly confusing or an even poorer
9670 * representation of what the plan is actually doing.
9671 */
9672 need_parens = (list_length(fstore->newvals) != 1);
9673 if (need_parens)
9674 appendStringInfoString(buf, "ROW(");
9675 get_rule_expr((Node *) fstore->newvals, context, showimplicit);
9676 if (need_parens)
9678 }
9679 break;
9680
9681 case T_RelabelType:
9682 {
9683 RelabelType *relabel = (RelabelType *) node;
9684 Node *arg = (Node *) relabel->arg;
9685
9686 if (relabel->relabelformat == COERCE_IMPLICIT_CAST &&
9687 !showimplicit)
9688 {
9689 /* don't show the implicit cast */
9690 get_rule_expr_paren(arg, context, false, node);
9691 }
9692 else
9693 {
9694 get_coercion_expr(arg, context,
9695 relabel->resulttype,
9696 relabel->resulttypmod,
9697 node);
9698 }
9699 }
9700 break;
9701
9702 case T_CoerceViaIO:
9703 {
9704 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
9705 Node *arg = (Node *) iocoerce->arg;
9706
9707 if (iocoerce->coerceformat == COERCE_IMPLICIT_CAST &&
9708 !showimplicit)
9709 {
9710 /* don't show the implicit cast */
9711 get_rule_expr_paren(arg, context, false, node);
9712 }
9713 else
9714 {
9715 get_coercion_expr(arg, context,
9716 iocoerce->resulttype,
9717 -1,
9718 node);
9719 }
9720 }
9721 break;
9722
9723 case T_ArrayCoerceExpr:
9724 {
9725 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
9726 Node *arg = (Node *) acoerce->arg;
9727
9728 if (acoerce->coerceformat == COERCE_IMPLICIT_CAST &&
9729 !showimplicit)
9730 {
9731 /* don't show the implicit cast */
9732 get_rule_expr_paren(arg, context, false, node);
9733 }
9734 else
9735 {
9736 get_coercion_expr(arg, context,
9737 acoerce->resulttype,
9738 acoerce->resulttypmod,
9739 node);
9740 }
9741 }
9742 break;
9743
9744 case T_ConvertRowtypeExpr:
9745 {
9747 Node *arg = (Node *) convert->arg;
9748
9749 if (convert->convertformat == COERCE_IMPLICIT_CAST &&
9750 !showimplicit)
9751 {
9752 /* don't show the implicit cast */
9753 get_rule_expr_paren(arg, context, false, node);
9754 }
9755 else
9756 {
9757 get_coercion_expr(arg, context,
9758 convert->resulttype, -1,
9759 node);
9760 }
9761 }
9762 break;
9763
9764 case T_CollateExpr:
9765 {
9766 CollateExpr *collate = (CollateExpr *) node;
9767 Node *arg = (Node *) collate->arg;
9768
9769 if (!PRETTY_PAREN(context))
9771 get_rule_expr_paren(arg, context, showimplicit, node);
9772 appendStringInfo(buf, " COLLATE %s",
9774 if (!PRETTY_PAREN(context))
9776 }
9777 break;
9778
9779 case T_CaseExpr:
9780 {
9781 CaseExpr *caseexpr = (CaseExpr *) node;
9782 ListCell *temp;
9783
9784 appendContextKeyword(context, "CASE",
9785 0, PRETTYINDENT_VAR, 0);
9786 if (caseexpr->arg)
9787 {
9789 get_rule_expr((Node *) caseexpr->arg, context, true);
9790 }
9791 foreach(temp, caseexpr->args)
9792 {
9793 CaseWhen *when = (CaseWhen *) lfirst(temp);
9794 Node *w = (Node *) when->expr;
9795
9796 if (caseexpr->arg)
9797 {
9798 /*
9799 * The parser should have produced WHEN clauses of the
9800 * form "CaseTestExpr = RHS", possibly with an
9801 * implicit coercion inserted above the CaseTestExpr.
9802 * For accurate decompilation of rules it's essential
9803 * that we show just the RHS. However in an
9804 * expression that's been through the optimizer, the
9805 * WHEN clause could be almost anything (since the
9806 * equality operator could have been expanded into an
9807 * inline function). If we don't recognize the form
9808 * of the WHEN clause, just punt and display it as-is.
9809 */
9810 if (IsA(w, OpExpr))
9811 {
9812 List *args = ((OpExpr *) w)->args;
9813
9814 if (list_length(args) == 2 &&
9816 CaseTestExpr))
9817 w = (Node *) lsecond(args);
9818 }
9819 }
9820
9821 if (!PRETTY_INDENT(context))
9823 appendContextKeyword(context, "WHEN ",
9824 0, 0, 0);
9825 get_rule_expr(w, context, false);
9826 appendStringInfoString(buf, " THEN ");
9827 get_rule_expr((Node *) when->result, context, true);
9828 }
9829 if (!PRETTY_INDENT(context))
9831 appendContextKeyword(context, "ELSE ",
9832 0, 0, 0);
9833 get_rule_expr((Node *) caseexpr->defresult, context, true);
9834 if (!PRETTY_INDENT(context))
9836 appendContextKeyword(context, "END",
9837 -PRETTYINDENT_VAR, 0, 0);
9838 }
9839 break;
9840
9841 case T_CaseTestExpr:
9842 {
9843 /*
9844 * Normally we should never get here, since for expressions
9845 * that can contain this node type we attempt to avoid
9846 * recursing to it. But in an optimized expression we might
9847 * be unable to avoid that (see comments for CaseExpr). If we
9848 * do see one, print it as CASE_TEST_EXPR.
9849 */
9850 appendStringInfoString(buf, "CASE_TEST_EXPR");
9851 }
9852 break;
9853
9854 case T_ArrayExpr:
9855 {
9856 ArrayExpr *arrayexpr = (ArrayExpr *) node;
9857
9858 appendStringInfoString(buf, "ARRAY[");
9859 get_rule_expr((Node *) arrayexpr->elements, context, true);
9861
9862 /*
9863 * If the array isn't empty, we assume its elements are
9864 * coerced to the desired type. If it's empty, though, we
9865 * need an explicit coercion to the array type.
9866 */
9867 if (arrayexpr->elements == NIL)
9868 appendStringInfo(buf, "::%s",
9869 format_type_with_typemod(arrayexpr->array_typeid, -1));
9870 }
9871 break;
9872
9873 case T_RowExpr:
9874 {
9875 RowExpr *rowexpr = (RowExpr *) node;
9876 TupleDesc tupdesc = NULL;
9877 ListCell *arg;
9878 int i;
9879 char *sep;
9880
9881 /*
9882 * If it's a named type and not RECORD, we may have to skip
9883 * dropped columns and/or claim there are NULLs for added
9884 * columns.
9885 */
9886 if (rowexpr->row_typeid != RECORDOID)
9887 {
9888 tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
9889 Assert(list_length(rowexpr->args) <= tupdesc->natts);
9890 }
9891
9892 /*
9893 * SQL99 allows "ROW" to be omitted when there is more than
9894 * one column, but for simplicity we always print it.
9895 */
9896 appendStringInfoString(buf, "ROW(");
9897 sep = "";
9898 i = 0;
9899 foreach(arg, rowexpr->args)
9900 {
9901 Node *e = (Node *) lfirst(arg);
9902
9903 if (tupdesc == NULL ||
9904 !TupleDescAttr(tupdesc, i)->attisdropped)
9905 {
9907 /* Whole-row Vars need special treatment here */
9908 get_rule_expr_toplevel(e, context, true);
9909 sep = ", ";
9910 }
9911 i++;
9912 }
9913 if (tupdesc != NULL)
9914 {
9915 while (i < tupdesc->natts)
9916 {
9917 if (!TupleDescAttr(tupdesc, i)->attisdropped)
9918 {
9920 appendStringInfoString(buf, "NULL");
9921 sep = ", ";
9922 }
9923 i++;
9924 }
9925
9926 ReleaseTupleDesc(tupdesc);
9927 }
9929 if (rowexpr->row_format == COERCE_EXPLICIT_CAST)
9930 appendStringInfo(buf, "::%s",
9931 format_type_with_typemod(rowexpr->row_typeid, -1));
9932 }
9933 break;
9934
9935 case T_RowCompareExpr:
9936 {
9937 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
9938
9939 /*
9940 * SQL99 allows "ROW" to be omitted when there is more than
9941 * one column, but for simplicity we always print it. Within
9942 * a ROW expression, whole-row Vars need special treatment, so
9943 * use get_rule_list_toplevel.
9944 */
9945 appendStringInfoString(buf, "(ROW(");
9946 get_rule_list_toplevel(rcexpr->largs, context, true);
9947
9948 /*
9949 * We assume that the name of the first-column operator will
9950 * do for all the rest too. This is definitely open to
9951 * failure, eg if some but not all operators were renamed
9952 * since the construct was parsed, but there seems no way to
9953 * be perfect.
9954 */
9955 appendStringInfo(buf, ") %s ROW(",
9956 generate_operator_name(linitial_oid(rcexpr->opnos),
9957 exprType(linitial(rcexpr->largs)),
9958 exprType(linitial(rcexpr->rargs))));
9959 get_rule_list_toplevel(rcexpr->rargs, context, true);
9961 }
9962 break;
9963
9964 case T_CoalesceExpr:
9965 {
9966 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
9967
9968 appendStringInfoString(buf, "COALESCE(");
9969 get_rule_expr((Node *) coalesceexpr->args, context, true);
9971 }
9972 break;
9973
9974 case T_MinMaxExpr:
9975 {
9976 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
9977
9978 switch (minmaxexpr->op)
9979 {
9980 case IS_GREATEST:
9981 appendStringInfoString(buf, "GREATEST(");
9982 break;
9983 case IS_LEAST:
9984 appendStringInfoString(buf, "LEAST(");
9985 break;
9986 }
9987 get_rule_expr((Node *) minmaxexpr->args, context, true);
9989 }
9990 break;
9991
9992 case T_SQLValueFunction:
9993 {
9994 SQLValueFunction *svf = (SQLValueFunction *) node;
9995
9996 /*
9997 * Note: this code knows that typmod for time, timestamp, and
9998 * timestamptz just prints as integer.
9999 */
10000 switch (svf->op)
10001 {
10002 case SVFOP_CURRENT_DATE:
10003 appendStringInfoString(buf, "CURRENT_DATE");
10004 break;
10005 case SVFOP_CURRENT_TIME:
10006 appendStringInfoString(buf, "CURRENT_TIME");
10007 break;
10009 appendStringInfo(buf, "CURRENT_TIME(%d)", svf->typmod);
10010 break;
10012 appendStringInfoString(buf, "CURRENT_TIMESTAMP");
10013 break;
10015 appendStringInfo(buf, "CURRENT_TIMESTAMP(%d)",
10016 svf->typmod);
10017 break;
10018 case SVFOP_LOCALTIME:
10019 appendStringInfoString(buf, "LOCALTIME");
10020 break;
10021 case SVFOP_LOCALTIME_N:
10022 appendStringInfo(buf, "LOCALTIME(%d)", svf->typmod);
10023 break;
10025 appendStringInfoString(buf, "LOCALTIMESTAMP");
10026 break;
10028 appendStringInfo(buf, "LOCALTIMESTAMP(%d)",
10029 svf->typmod);
10030 break;
10031 case SVFOP_CURRENT_ROLE:
10032 appendStringInfoString(buf, "CURRENT_ROLE");
10033 break;
10034 case SVFOP_CURRENT_USER:
10035 appendStringInfoString(buf, "CURRENT_USER");
10036 break;
10037 case SVFOP_USER:
10038 appendStringInfoString(buf, "USER");
10039 break;
10040 case SVFOP_SESSION_USER:
10041 appendStringInfoString(buf, "SESSION_USER");
10042 break;
10044 appendStringInfoString(buf, "CURRENT_CATALOG");
10045 break;
10047 appendStringInfoString(buf, "CURRENT_SCHEMA");
10048 break;
10049 }
10050 }
10051 break;
10052
10053 case T_XmlExpr:
10054 {
10055 XmlExpr *xexpr = (XmlExpr *) node;
10056 bool needcomma = false;
10057 ListCell *arg;
10058 ListCell *narg;
10059 Const *con;
10060
10061 switch (xexpr->op)
10062 {
10063 case IS_XMLCONCAT:
10064 appendStringInfoString(buf, "XMLCONCAT(");
10065 break;
10066 case IS_XMLELEMENT:
10067 appendStringInfoString(buf, "XMLELEMENT(");
10068 break;
10069 case IS_XMLFOREST:
10070 appendStringInfoString(buf, "XMLFOREST(");
10071 break;
10072 case IS_XMLPARSE:
10073 appendStringInfoString(buf, "XMLPARSE(");
10074 break;
10075 case IS_XMLPI:
10076 appendStringInfoString(buf, "XMLPI(");
10077 break;
10078 case IS_XMLROOT:
10079 appendStringInfoString(buf, "XMLROOT(");
10080 break;
10081 case IS_XMLSERIALIZE:
10082 appendStringInfoString(buf, "XMLSERIALIZE(");
10083 break;
10084 case IS_DOCUMENT:
10085 break;
10086 }
10087 if (xexpr->op == IS_XMLPARSE || xexpr->op == IS_XMLSERIALIZE)
10088 {
10089 if (xexpr->xmloption == XMLOPTION_DOCUMENT)
10090 appendStringInfoString(buf, "DOCUMENT ");
10091 else
10092 appendStringInfoString(buf, "CONTENT ");
10093 }
10094 if (xexpr->name)
10095 {
10096 appendStringInfo(buf, "NAME %s",
10098 needcomma = true;
10099 }
10100 if (xexpr->named_args)
10101 {
10102 if (xexpr->op != IS_XMLFOREST)
10103 {
10104 if (needcomma)
10106 appendStringInfoString(buf, "XMLATTRIBUTES(");
10107 needcomma = false;
10108 }
10109 forboth(arg, xexpr->named_args, narg, xexpr->arg_names)
10110 {
10111 Node *e = (Node *) lfirst(arg);
10112 char *argname = strVal(lfirst(narg));
10113
10114 if (needcomma)
10116 get_rule_expr((Node *) e, context, true);
10117 appendStringInfo(buf, " AS %s",
10119 needcomma = true;
10120 }
10121 if (xexpr->op != IS_XMLFOREST)
10123 }
10124 if (xexpr->args)
10125 {
10126 if (needcomma)
10128 switch (xexpr->op)
10129 {
10130 case IS_XMLCONCAT:
10131 case IS_XMLELEMENT:
10132 case IS_XMLFOREST:
10133 case IS_XMLPI:
10134 case IS_XMLSERIALIZE:
10135 /* no extra decoration needed */
10136 get_rule_expr((Node *) xexpr->args, context, true);
10137 break;
10138 case IS_XMLPARSE:
10139 Assert(list_length(xexpr->args) == 2);
10140
10141 get_rule_expr((Node *) linitial(xexpr->args),
10142 context, true);
10143
10144 con = lsecond_node(Const, xexpr->args);
10145 Assert(!con->constisnull);
10146 if (DatumGetBool(con->constvalue))
10148 " PRESERVE WHITESPACE");
10149 else
10151 " STRIP WHITESPACE");
10152 break;
10153 case IS_XMLROOT:
10154 Assert(list_length(xexpr->args) == 3);
10155
10156 get_rule_expr((Node *) linitial(xexpr->args),
10157 context, true);
10158
10159 appendStringInfoString(buf, ", VERSION ");
10160 con = (Const *) lsecond(xexpr->args);
10161 if (IsA(con, Const) &&
10162 con->constisnull)
10163 appendStringInfoString(buf, "NO VALUE");
10164 else
10165 get_rule_expr((Node *) con, context, false);
10166
10167 con = lthird_node(Const, xexpr->args);
10168 if (con->constisnull)
10169 /* suppress STANDALONE NO VALUE */ ;
10170 else
10171 {
10172 switch (DatumGetInt32(con->constvalue))
10173 {
10174 case XML_STANDALONE_YES:
10176 ", STANDALONE YES");
10177 break;
10178 case XML_STANDALONE_NO:
10180 ", STANDALONE NO");
10181 break;
10184 ", STANDALONE NO VALUE");
10185 break;
10186 default:
10187 break;
10188 }
10189 }
10190 break;
10191 case IS_DOCUMENT:
10192 get_rule_expr_paren((Node *) xexpr->args, context, false, node);
10193 break;
10194 }
10195 }
10196 if (xexpr->op == IS_XMLSERIALIZE)
10197 {
10198 appendStringInfo(buf, " AS %s",
10199 format_type_with_typemod(xexpr->type,
10200 xexpr->typmod));
10201 if (xexpr->indent)
10202 appendStringInfoString(buf, " INDENT");
10203 else
10204 appendStringInfoString(buf, " NO INDENT");
10205 }
10206
10207 if (xexpr->op == IS_DOCUMENT)
10208 appendStringInfoString(buf, " IS DOCUMENT");
10209 else
10211 }
10212 break;
10213
10214 case T_NullTest:
10215 {
10216 NullTest *ntest = (NullTest *) node;
10217
10218 if (!PRETTY_PAREN(context))
10220 get_rule_expr_paren((Node *) ntest->arg, context, true, node);
10221
10222 /*
10223 * For scalar inputs, we prefer to print as IS [NOT] NULL,
10224 * which is shorter and traditional. If it's a rowtype input
10225 * but we're applying a scalar test, must print IS [NOT]
10226 * DISTINCT FROM NULL to be semantically correct.
10227 */
10228 if (ntest->argisrow ||
10229 !type_is_rowtype(exprType((Node *) ntest->arg)))
10230 {
10231 switch (ntest->nulltesttype)
10232 {
10233 case IS_NULL:
10234 appendStringInfoString(buf, " IS NULL");
10235 break;
10236 case IS_NOT_NULL:
10237 appendStringInfoString(buf, " IS NOT NULL");
10238 break;
10239 default:
10240 elog(ERROR, "unrecognized nulltesttype: %d",
10241 (int) ntest->nulltesttype);
10242 }
10243 }
10244 else
10245 {
10246 switch (ntest->nulltesttype)
10247 {
10248 case IS_NULL:
10249 appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL");
10250 break;
10251 case IS_NOT_NULL:
10252 appendStringInfoString(buf, " IS DISTINCT FROM NULL");
10253 break;
10254 default:
10255 elog(ERROR, "unrecognized nulltesttype: %d",
10256 (int) ntest->nulltesttype);
10257 }
10258 }
10259 if (!PRETTY_PAREN(context))
10261 }
10262 break;
10263
10264 case T_BooleanTest:
10265 {
10266 BooleanTest *btest = (BooleanTest *) node;
10267
10268 if (!PRETTY_PAREN(context))
10270 get_rule_expr_paren((Node *) btest->arg, context, false, node);
10271 switch (btest->booltesttype)
10272 {
10273 case IS_TRUE:
10274 appendStringInfoString(buf, " IS TRUE");
10275 break;
10276 case IS_NOT_TRUE:
10277 appendStringInfoString(buf, " IS NOT TRUE");
10278 break;
10279 case IS_FALSE:
10280 appendStringInfoString(buf, " IS FALSE");
10281 break;
10282 case IS_NOT_FALSE:
10283 appendStringInfoString(buf, " IS NOT FALSE");
10284 break;
10285 case IS_UNKNOWN:
10286 appendStringInfoString(buf, " IS UNKNOWN");
10287 break;
10288 case IS_NOT_UNKNOWN:
10289 appendStringInfoString(buf, " IS NOT UNKNOWN");
10290 break;
10291 default:
10292 elog(ERROR, "unrecognized booltesttype: %d",
10293 (int) btest->booltesttype);
10294 }
10295 if (!PRETTY_PAREN(context))
10297 }
10298 break;
10299
10300 case T_CoerceToDomain:
10301 {
10302 CoerceToDomain *ctest = (CoerceToDomain *) node;
10303 Node *arg = (Node *) ctest->arg;
10304
10305 if (ctest->coercionformat == COERCE_IMPLICIT_CAST &&
10306 !showimplicit)
10307 {
10308 /* don't show the implicit cast */
10309 get_rule_expr(arg, context, false);
10310 }
10311 else
10312 {
10313 get_coercion_expr(arg, context,
10314 ctest->resulttype,
10315 ctest->resulttypmod,
10316 node);
10317 }
10318 }
10319 break;
10320
10321 case T_CoerceToDomainValue:
10322 appendStringInfoString(buf, "VALUE");
10323 break;
10324
10325 case T_SetToDefault:
10326 appendStringInfoString(buf, "DEFAULT");
10327 break;
10328
10329 case T_CurrentOfExpr:
10330 {
10331 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
10332
10333 if (cexpr->cursor_name)
10334 appendStringInfo(buf, "CURRENT OF %s",
10336 else
10337 appendStringInfo(buf, "CURRENT OF $%d",
10338 cexpr->cursor_param);
10339 }
10340 break;
10341
10342 case T_NextValueExpr:
10343 {
10344 NextValueExpr *nvexpr = (NextValueExpr *) node;
10345
10346 /*
10347 * This isn't exactly nextval(), but that seems close enough
10348 * for EXPLAIN's purposes.
10349 */
10350 appendStringInfoString(buf, "nextval(");
10353 NIL));
10355 }
10356 break;
10357
10358 case T_InferenceElem:
10359 {
10360 InferenceElem *iexpr = (InferenceElem *) node;
10361 bool save_varprefix;
10362 bool need_parens;
10363
10364 /*
10365 * InferenceElem can only refer to target relation, so a
10366 * prefix is not useful, and indeed would cause parse errors.
10367 */
10368 save_varprefix = context->varprefix;
10369 context->varprefix = false;
10370
10371 /*
10372 * Parenthesize the element unless it's a simple Var or a bare
10373 * function call. Follows pg_get_indexdef_worker().
10374 */
10375 need_parens = !IsA(iexpr->expr, Var);
10376 if (IsA(iexpr->expr, FuncExpr) &&
10377 ((FuncExpr *) iexpr->expr)->funcformat ==
10379 need_parens = false;
10380
10381 if (need_parens)
10383 get_rule_expr((Node *) iexpr->expr,
10384 context, false);
10385 if (need_parens)
10387
10388 context->varprefix = save_varprefix;
10389
10390 if (iexpr->infercollid)
10391 appendStringInfo(buf, " COLLATE %s",
10393
10394 /* Add the operator class name, if not default */
10395 if (iexpr->inferopclass)
10396 {
10397 Oid inferopclass = iexpr->inferopclass;
10398 Oid inferopcinputtype = get_opclass_input_type(iexpr->inferopclass);
10399
10400 get_opclass_name(inferopclass, inferopcinputtype, buf);
10401 }
10402 }
10403 break;
10404
10405 case T_ReturningExpr:
10406 {
10407 ReturningExpr *retExpr = (ReturningExpr *) node;
10408
10409 /*
10410 * We cannot see a ReturningExpr in rule deparsing, only while
10411 * EXPLAINing a query plan (ReturningExpr nodes are only ever
10412 * adding during query rewriting). Just display the expression
10413 * returned (an expanded view column).
10414 */
10415 get_rule_expr((Node *) retExpr->retexpr, context, showimplicit);
10416 }
10417 break;
10418
10419 case T_PartitionBoundSpec:
10420 {
10421 PartitionBoundSpec *spec = (PartitionBoundSpec *) node;
10422 ListCell *cell;
10423 char *sep;
10424
10425 if (spec->is_default)
10426 {
10427 appendStringInfoString(buf, "DEFAULT");
10428 break;
10429 }
10430
10431 switch (spec->strategy)
10432 {
10434 Assert(spec->modulus > 0 && spec->remainder >= 0);
10435 Assert(spec->modulus > spec->remainder);
10436
10437 appendStringInfoString(buf, "FOR VALUES");
10438 appendStringInfo(buf, " WITH (modulus %d, remainder %d)",
10439 spec->modulus, spec->remainder);
10440 break;
10441
10443 Assert(spec->listdatums != NIL);
10444
10445 appendStringInfoString(buf, "FOR VALUES IN (");
10446 sep = "";
10447 foreach(cell, spec->listdatums)
10448 {
10449 Const *val = lfirst_node(Const, cell);
10450
10452 get_const_expr(val, context, -1);
10453 sep = ", ";
10454 }
10455
10457 break;
10458
10460 Assert(spec->lowerdatums != NIL &&
10461 spec->upperdatums != NIL &&
10462 list_length(spec->lowerdatums) ==
10463 list_length(spec->upperdatums));
10464
10465 appendStringInfo(buf, "FOR VALUES FROM %s TO %s",
10468 break;
10469
10470 default:
10471 elog(ERROR, "unrecognized partition strategy: %d",
10472 (int) spec->strategy);
10473 break;
10474 }
10475 }
10476 break;
10477
10478 case T_JsonValueExpr:
10479 {
10480 JsonValueExpr *jve = (JsonValueExpr *) node;
10481
10482 get_rule_expr((Node *) jve->raw_expr, context, false);
10483 get_json_format(jve->format, context->buf);
10484 }
10485 break;
10486
10487 case T_JsonConstructorExpr:
10488 get_json_constructor((JsonConstructorExpr *) node, context, false);
10489 break;
10490
10491 case T_JsonIsPredicate:
10492 {
10493 JsonIsPredicate *pred = (JsonIsPredicate *) node;
10494
10495 if (!PRETTY_PAREN(context))
10496 appendStringInfoChar(context->buf, '(');
10497
10498 get_rule_expr_paren(pred->expr, context, true, node);
10499
10500 appendStringInfoString(context->buf, " IS JSON");
10501
10502 /* TODO: handle FORMAT clause */
10503
10504 switch (pred->item_type)
10505 {
10506 case JS_TYPE_SCALAR:
10507 appendStringInfoString(context->buf, " SCALAR");
10508 break;
10509 case JS_TYPE_ARRAY:
10510 appendStringInfoString(context->buf, " ARRAY");
10511 break;
10512 case JS_TYPE_OBJECT:
10513 appendStringInfoString(context->buf, " OBJECT");
10514 break;
10515 default:
10516 break;
10517 }
10518
10519 if (pred->unique_keys)
10520 appendStringInfoString(context->buf, " WITH UNIQUE KEYS");
10521
10522 if (!PRETTY_PAREN(context))
10523 appendStringInfoChar(context->buf, ')');
10524 }
10525 break;
10526
10527 case T_JsonExpr:
10528 {
10529 JsonExpr *jexpr = (JsonExpr *) node;
10530
10531 switch (jexpr->op)
10532 {
10533 case JSON_EXISTS_OP:
10534 appendStringInfoString(buf, "JSON_EXISTS(");
10535 break;
10536 case JSON_QUERY_OP:
10537 appendStringInfoString(buf, "JSON_QUERY(");
10538 break;
10539 case JSON_VALUE_OP:
10540 appendStringInfoString(buf, "JSON_VALUE(");
10541 break;
10542 default:
10543 elog(ERROR, "unrecognized JsonExpr op: %d",
10544 (int) jexpr->op);
10545 }
10546
10547 get_rule_expr(jexpr->formatted_expr, context, showimplicit);
10548
10550
10551 get_json_path_spec(jexpr->path_spec, context, showimplicit);
10552
10553 if (jexpr->passing_values)
10554 {
10555 ListCell *lc1,
10556 *lc2;
10557 bool needcomma = false;
10558
10559 appendStringInfoString(buf, " PASSING ");
10560
10561 forboth(lc1, jexpr->passing_names,
10562 lc2, jexpr->passing_values)
10563 {
10564 if (needcomma)
10566 needcomma = true;
10567
10568 get_rule_expr((Node *) lfirst(lc2), context, showimplicit);
10569 appendStringInfo(buf, " AS %s",
10570 quote_identifier(lfirst_node(String, lc1)->sval));
10571 }
10572 }
10573
10574 if (jexpr->op != JSON_EXISTS_OP ||
10575 jexpr->returning->typid != BOOLOID)
10576 get_json_returning(jexpr->returning, context->buf,
10577 jexpr->op == JSON_QUERY_OP);
10578
10579 get_json_expr_options(jexpr, context,
10580 jexpr->op != JSON_EXISTS_OP ?
10583
10585 }
10586 break;
10587
10588 case T_List:
10589 {
10590 char *sep;
10591 ListCell *l;
10592
10593 sep = "";
10594 foreach(l, (List *) node)
10595 {
10597 get_rule_expr((Node *) lfirst(l), context, showimplicit);
10598 sep = ", ";
10599 }
10600 }
10601 break;
10602
10603 case T_TableFunc:
10604 get_tablefunc((TableFunc *) node, context, showimplicit);
10605 break;
10606
10607 default:
10608 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
10609 break;
10610 }
10611}
List * list_delete_first(List *list)
Definition: list.c:943
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1247
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2738
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2915
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:705
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:885
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:883
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:884
#define lsecond_node(type, l)
Definition: pg_list.h:186
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
#define linitial_oid(l)
Definition: pg_list.h:180
#define lthird_node(type, l)
Definition: pg_list.h:191
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207
e
Definition: preproc-init.c:82
@ IS_NOT_TRUE
Definition: primnodes.h:1981
@ IS_NOT_FALSE
Definition: primnodes.h:1981
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1981
@ IS_TRUE
Definition: primnodes.h:1981
@ IS_UNKNOWN
Definition: primnodes.h:1981
@ IS_FALSE
Definition: primnodes.h:1981
@ ARRAY_SUBLINK
Definition: primnodes.h:1020
@ ANY_SUBLINK
Definition: primnodes.h:1016
@ CTE_SUBLINK
Definition: primnodes.h:1021
@ EXPR_SUBLINK
Definition: primnodes.h:1018
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:1017
@ ALL_SUBLINK
Definition: primnodes.h:1015
@ EXISTS_SUBLINK
Definition: primnodes.h:1014
@ IS_LEAST
Definition: primnodes.h:1508
@ IS_GREATEST
Definition: primnodes.h:1507
@ AND_EXPR
Definition: primnodes.h:948
@ OR_EXPR
Definition: primnodes.h:948
@ NOT_EXPR
Definition: primnodes.h:948
@ XMLOPTION_DOCUMENT
Definition: primnodes.h:1597
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1554
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1547
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1544
@ SVFOP_LOCALTIME
Definition: primnodes.h:1546
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1545
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1550
@ SVFOP_USER
Definition: primnodes.h:1552
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1555
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1549
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1541
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1543
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1542
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1548
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1551
@ SVFOP_SESSION_USER
Definition: primnodes.h:1553
@ IS_DOCUMENT
Definition: primnodes.h:1592
@ IS_XMLFOREST
Definition: primnodes.h:1587
@ IS_XMLCONCAT
Definition: primnodes.h:1585
@ IS_XMLPI
Definition: primnodes.h:1589
@ IS_XMLPARSE
Definition: primnodes.h:1588
@ IS_XMLSERIALIZE
Definition: primnodes.h:1591
@ IS_XMLROOT
Definition: primnodes.h:1590
@ IS_XMLELEMENT
Definition: primnodes.h:1586
@ JSON_VALUE_OP
Definition: primnodes.h:1809
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:751
@ IS_NULL
Definition: primnodes.h:1957
@ IS_NOT_NULL
Definition: primnodes.h:1957
@ JS_TYPE_ARRAY
Definition: primnodes.h:1729
@ JS_TYPE_OBJECT
Definition: primnodes.h:1728
@ JS_TYPE_SCALAR
Definition: primnodes.h:1730
static SPIPlanPtr splan
Definition: regress.c:268
static void get_sublink_expr(SubLink *sublink, deparse_context *context)
Definition: ruleutils.c:11810
static void get_parameter(Param *param, deparse_context *context)
Definition: ruleutils.c:8677
static void get_json_constructor(JsonConstructorExpr *ctor, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:11662
static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context)
Definition: ruleutils.c:12989
static void get_oper_expr(OpExpr *expr, deparse_context *context)
Definition: ruleutils.c:10725
static void get_func_expr(FuncExpr *expr, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:10765
char * get_range_partbound_string(List *bound_datums)
Definition: ruleutils.c:13666
static void get_rule_expr_toplevel(Node *node, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:10625
static void get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
Definition: ruleutils.c:11015
static char * get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
Definition: ruleutils.c:7596
BoolExprType boolop
Definition: primnodes.h:956
List * args
Definition: primnodes.h:957
BoolTestType booltesttype
Definition: primnodes.h:1988
Expr * arg
Definition: primnodes.h:1987
Expr * arg
Definition: primnodes.h:1330
Expr * defresult
Definition: primnodes.h:1332
List * args
Definition: primnodes.h:1331
List * args
Definition: primnodes.h:1497
Expr * arg
Definition: primnodes.h:1224
Oid resulttype
Definition: primnodes.h:1225
Expr * arg
Definition: primnodes.h:1296
char * cursor_name
Definition: primnodes.h:2103
AttrNumber fieldnum
Definition: primnodes.h:1146
Expr * arg
Definition: primnodes.h:1145
List * newvals
Definition: primnodes.h:1177
JsonReturning * returning
Definition: primnodes.h:1837
JsonValueType item_type
Definition: primnodes.h:1742
JsonFormat * format
Definition: primnodes.h:1690
Expr * raw_expr
Definition: primnodes.h:1688
List * args
Definition: primnodes.h:1523
MinMaxOp op
Definition: primnodes.h:1521
Expr * arg
Definition: primnodes.h:808
NullTestType nulltesttype
Definition: primnodes.h:1964
Expr * arg
Definition: primnodes.h:1963
Oid resulttype
Definition: primnodes.h:1202
Expr * arg
Definition: primnodes.h:1201
Expr * retexpr
Definition: primnodes.h:2157
List * args
Definition: primnodes.h:1428
SQLValueFunctionOp op
Definition: primnodes.h:1561
Node * testexpr
Definition: primnodes.h:1084
Expr * refassgnexpr
Definition: primnodes.h:720
Expr * refexpr
Definition: primnodes.h:718
List * args
Definition: primnodes.h:1613
bool indent
Definition: primnodes.h:1617
List * named_args
Definition: primnodes.h:1609
XmlExprOp op
Definition: primnodes.h:1605
Definition: type.h:89
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:213
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1920
char * map_xml_name_to_sql_identifier(const char *name)
Definition: xml.c:2434
@ XML_STANDALONE_NO_VALUE
Definition: xml.h:29
@ XML_STANDALONE_YES
Definition: xml.h:27
@ XML_STANDALONE_NO
Definition: xml.h:28
static void convert(const int32 val, char *const buf)
Definition: zic.c:1992

References ALL_SUBLINK, deparse_namespace::ancestors, AND_EXPR, ANY_SUBLINK, appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), arg, NamedArgExpr::arg, FieldSelect::arg, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, CollateExpr::arg, CaseExpr::arg, NullTest::arg, BooleanTest::arg, CoerceToDomain::arg, generate_unaccent_rules::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, CaseExpr::args, RowExpr::args, CoalesceExpr::args, MinMaxExpr::args, XmlExpr::args, ARRAY_SUBLINK, Assert(), BoolExpr::boolop, BooleanTest::booltesttype, deparse_context::buf, buf, CHECK_FOR_INTERRUPTS, check_stack_depth(), COERCE_EXPLICIT_CALL, COERCE_EXPLICIT_CAST, COERCE_IMPLICIT_CAST, CollateExpr::collOid, convert(), CTE_SUBLINK, CurrentOfExpr::cursor_name, CurrentOfExpr::cursor_param, DatumGetBool(), DatumGetInt32(), CaseExpr::defresult, elog, ERROR, EXISTS_SUBLINK, JsonIsPredicate::expr, InferenceElem::expr, EXPR_SUBLINK, exprType(), exprTypmod(), FieldSelect::fieldnum, for_each_from, forboth, JsonValueExpr::format, format_type_with_typemod(), JsonExpr::formatted_expr, generate_collation_name(), generate_operator_name(), generate_relation_name(), get_agg_expr(), get_base_element_type(), get_coercion_expr(), get_const_expr(), get_func_expr(), get_json_constructor(), get_json_expr_options(), get_json_format(), get_json_path_spec(), get_json_returning(), get_name_for_var_field(), get_opclass_input_type(), get_opclass_name(), get_oper_expr(), get_parameter(), get_range_partbound_string(), get_rule_expr(), get_rule_expr_paren(), get_rule_expr_toplevel(), get_rule_list_toplevel(), get_sublink_expr(), get_tablefunc(), get_variable(), get_windowfunc_expr(), i, if(), XmlExpr::indent, InferenceElem::infercollid, InferenceElem::inferopclass, PartitionBoundSpec::is_default, IS_DOCUMENT, IS_FALSE, IS_GREATEST, IS_LEAST, IS_NOT_FALSE, IS_NOT_NULL, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_NULL, IS_TRUE, IS_UNKNOWN, IS_XMLCONCAT, IS_XMLELEMENT, IS_XMLFOREST, IS_XMLPARSE, IS_XMLPI, IS_XMLROOT, IS_XMLSERIALIZE, IsA, JsonIsPredicate::item_type, JS_TYPE_ARRAY, JS_TYPE_OBJECT, JS_TYPE_SCALAR, JSON_BEHAVIOR_FALSE, JSON_BEHAVIOR_NULL, JSON_EXISTS_OP, JSON_QUERY_OP, JSON_VALUE_OP, RowCompareExpr::largs, lcons(), lfirst, lfirst_node, linitial, linitial_oid, list_delete_first(), list_length(), PartitionBoundSpec::listdatums, lnext(), lookup_rowtype_tupdesc(), PartitionBoundSpec::lowerdatums, lsecond, lsecond_node, lthird_node, map_xml_name_to_sql_identifier(), PartitionBoundSpec::modulus, MULTIEXPR_SUBLINK, XmlExpr::named_args, deparse_context::namespaces, TupleDescData::natts, FieldStore::newvals, NIL, nodeTag, NOT_EXPR, NullTest::nulltesttype, MinMaxExpr::op, SQLValueFunction::op, XmlExpr::op, JsonExpr::op, ScalarArrayOpExpr::opno, OR_EXPR, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, JsonExpr::passing_names, JsonExpr::passing_values, JsonExpr::path_spec, SubPlan::plan_name, PRETTY_INDENT, PRETTY_PAREN, PRETTYINDENT_VAR, printSubscripts(), processIndirection(), quote_identifier(), RowCompareExpr::rargs, JsonValueExpr::raw_expr, SubscriptingRef::refassgnexpr, SubscriptingRef::refexpr, ReleaseTupleDesc, PartitionBoundSpec::remainder, RelabelType::resulttype, CoerceViaIO::resulttype, ArrayCoerceExpr::resulttype, CoerceToDomain::resulttype, ReturningExpr::retexpr, JsonExpr::returning, ROWCOMPARE_SUBLINK, NextValueExpr::seqid, simple_quote_literal(), splan, PartitionBoundSpec::strategy, strip_implicit_coercions(), strVal, SubPlan::subLinkType, AlternativeSubPlan::subplans, SVFOP_CURRENT_CATALOG, SVFOP_CURRENT_DATE, SVFOP_CURRENT_ROLE, SVFOP_CURRENT_SCHEMA, SVFOP_CURRENT_TIME, SVFOP_CURRENT_TIME_N, SVFOP_CURRENT_TIMESTAMP, SVFOP_CURRENT_TIMESTAMP_N, SVFOP_CURRENT_USER, SVFOP_LOCALTIME, SVFOP_LOCALTIME_N, SVFOP_LOCALTIMESTAMP, SVFOP_LOCALTIMESTAMP_N, SVFOP_SESSION_USER, SVFOP_USER, SubPlan::testexpr, TupleDescAttr(), type_is_rowtype(), JsonReturning::typid, SQLValueFunction::typmod, JsonIsPredicate::unique_keys, PartitionBoundSpec::upperdatums, SubPlan::useHashTable, ScalarArrayOpExpr::useOr, val, deparse_context::varprefix, XML_STANDALONE_NO, XML_STANDALONE_NO_VALUE, XML_STANDALONE_YES, and XMLOPTION_DOCUMENT.

Referenced by deparse_expression_pretty(), get_agg_expr_helper(), get_basic_select_query(), get_delete_query_def(), get_from_clause_item(), get_func_expr(), get_func_sql_syntax(), get_insert_query_def(), get_json_behavior(), get_json_constructor(), get_json_path_spec(), get_json_table(), get_merge_query_def(), get_parameter(), get_rule_expr(), get_rule_expr_funccall(), get_rule_expr_paren(), get_rule_expr_toplevel(), get_rule_sortgroupclause(), get_select_query_def(), get_special_variable(), get_sublink_expr(), get_tablesample_def(), get_target_list(), get_update_query_def(), get_update_query_targetlist_def(), get_variable(), get_window_frame_options(), get_windowfunc_expr_helper(), get_with_clause(), get_xmltable(), make_ruledef(), pg_get_triggerdef_worker(), and printSubscripts().

◆ get_rule_expr_funccall()

static void get_rule_expr_funccall ( Node node,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 10673 of file ruleutils.c.

10675{
10676 if (looks_like_function(node))
10677 get_rule_expr(node, context, showimplicit);
10678 else
10679 {
10680 StringInfo buf = context->buf;
10681
10682 appendStringInfoString(buf, "CAST(");
10683 /* no point in showing any top-level implicit cast */
10684 get_rule_expr(node, context, false);
10685 appendStringInfo(buf, " AS %s)",
10687 exprTypmod(node)));
10688 }
10689}
static bool looks_like_function(Node *node)
Definition: ruleutils.c:10696

References appendStringInfo(), appendStringInfoString(), deparse_context::buf, buf, exprType(), exprTypmod(), format_type_with_typemod(), get_rule_expr(), and looks_like_function().

Referenced by get_from_clause_item().

◆ get_rule_expr_paren()

static void get_rule_expr_paren ( Node node,
deparse_context context,
bool  showimplicit,
Node parentNode 
)
static

Definition at line 9145 of file ruleutils.c.

9147{
9148 bool need_paren;
9149
9150 need_paren = PRETTY_PAREN(context) &&
9151 !isSimpleNode(node, parentNode, context->prettyFlags);
9152
9153 if (need_paren)
9154 appendStringInfoChar(context->buf, '(');
9155
9156 get_rule_expr(node, context, showimplicit);
9157
9158 if (need_paren)
9159 appendStringInfoChar(context->buf, ')');
9160}
static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
Definition: ruleutils.c:8840

References appendStringInfoChar(), deparse_context::buf, get_rule_expr(), isSimpleNode(), PRETTY_PAREN, and deparse_context::prettyFlags.

Referenced by get_coercion_expr(), get_func_expr(), get_func_sql_syntax(), get_oper_expr(), and get_rule_expr().

◆ get_rule_expr_toplevel()

static void get_rule_expr_toplevel ( Node node,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 10625 of file ruleutils.c.

10627{
10628 if (node && IsA(node, Var))
10629 (void) get_variable((Var *) node, 0, true, context);
10630 else
10631 get_rule_expr(node, context, showimplicit);
10632}

References get_rule_expr(), get_variable(), and IsA.

Referenced by get_rule_expr(), get_rule_list_toplevel(), and get_values_def().

◆ get_rule_groupingset()

static void get_rule_groupingset ( GroupingSet gset,
List targetlist,
bool  omit_parens,
deparse_context context 
)
static

Definition at line 6621 of file ruleutils.c.

6623{
6624 ListCell *l;
6625 StringInfo buf = context->buf;
6626 bool omit_child_parens = true;
6627 char *sep = "";
6628
6629 switch (gset->kind)
6630 {
6631 case GROUPING_SET_EMPTY:
6633 return;
6634
6636 {
6637 if (!omit_parens || list_length(gset->content) != 1)
6639
6640 foreach(l, gset->content)
6641 {
6642 Index ref = lfirst_int(l);
6643
6645 get_rule_sortgroupclause(ref, targetlist,
6646 false, context);
6647 sep = ", ";
6648 }
6649
6650 if (!omit_parens || list_length(gset->content) != 1)
6652 }
6653 return;
6654
6656 appendStringInfoString(buf, "ROLLUP(");
6657 break;
6658 case GROUPING_SET_CUBE:
6659 appendStringInfoString(buf, "CUBE(");
6660 break;
6661 case GROUPING_SET_SETS:
6662 appendStringInfoString(buf, "GROUPING SETS (");
6663 omit_child_parens = false;
6664 break;
6665 }
6666
6667 foreach(l, gset->content)
6668 {
6670 get_rule_groupingset(lfirst(l), targetlist, omit_child_parens, context);
6671 sep = ", ";
6672 }
6673
6675}
@ GROUPING_SET_CUBE
Definition: parsenodes.h:1511
@ GROUPING_SET_SIMPLE
Definition: parsenodes.h:1509
@ GROUPING_SET_ROLLUP
Definition: parsenodes.h:1510
@ GROUPING_SET_SETS
Definition: parsenodes.h:1512
@ GROUPING_SET_EMPTY
Definition: parsenodes.h:1508
List * content
Definition: parsenodes.h:1519

References appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, GroupingSet::content, get_rule_groupingset(), get_rule_sortgroupclause(), GROUPING_SET_CUBE, GROUPING_SET_EMPTY, GROUPING_SET_ROLLUP, GROUPING_SET_SETS, GROUPING_SET_SIMPLE, lfirst, lfirst_int, and list_length().

Referenced by get_basic_select_query(), and get_rule_groupingset().

◆ get_rule_list_toplevel()

static void get_rule_list_toplevel ( List lst,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 10643 of file ruleutils.c.

10645{
10646 const char *sep;
10647 ListCell *lc;
10648
10649 sep = "";
10650 foreach(lc, lst)
10651 {
10652 Node *e = (Node *) lfirst(lc);
10653
10654 appendStringInfoString(context->buf, sep);
10655 get_rule_expr_toplevel(e, context, showimplicit);
10656 sep = ", ";
10657 }
10658}

References appendStringInfoString(), deparse_context::buf, get_rule_expr_toplevel(), and lfirst.

Referenced by get_insert_query_def(), get_merge_query_def(), and get_rule_expr().

◆ get_rule_orderby()

static void get_rule_orderby ( List orderList,
List targetList,
bool  force_colno,
deparse_context context 
)
static

Definition at line 6681 of file ruleutils.c.

6683{
6684 StringInfo buf = context->buf;
6685 const char *sep;
6686 ListCell *l;
6687
6688 sep = "";
6689 foreach(l, orderList)
6690 {
6692 Node *sortexpr;
6693 Oid sortcoltype;
6694 TypeCacheEntry *typentry;
6695
6697 sortexpr = get_rule_sortgroupclause(srt->tleSortGroupRef, targetList,
6698 force_colno, context);
6699 sortcoltype = exprType(sortexpr);
6700 /* See whether operator is default < or > for datatype */
6701 typentry = lookup_type_cache(sortcoltype,
6703 if (srt->sortop == typentry->lt_opr)
6704 {
6705 /* ASC is default, so emit nothing for it */
6706 if (srt->nulls_first)
6707 appendStringInfoString(buf, " NULLS FIRST");
6708 }
6709 else if (srt->sortop == typentry->gt_opr)
6710 {
6711 appendStringInfoString(buf, " DESC");
6712 /* DESC defaults to NULLS FIRST */
6713 if (!srt->nulls_first)
6714 appendStringInfoString(buf, " NULLS LAST");
6715 }
6716 else
6717 {
6718 appendStringInfo(buf, " USING %s",
6720 sortcoltype,
6721 sortcoltype));
6722 /* be specific to eliminate ambiguity */
6723 if (srt->nulls_first)
6724 appendStringInfoString(buf, " NULLS FIRST");
6725 else
6726 appendStringInfoString(buf, " NULLS LAST");
6727 }
6728 sep = ", ";
6729 }
6730}
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386
#define TYPECACHE_GT_OPR
Definition: typcache.h:139
#define TYPECACHE_LT_OPR
Definition: typcache.h:138

References appendStringInfo(), appendStringInfoString(), deparse_context::buf, buf, exprType(), generate_operator_name(), get_rule_sortgroupclause(), TypeCacheEntry::gt_opr, lfirst, lookup_type_cache(), TypeCacheEntry::lt_opr, SortGroupClause::nulls_first, SortGroupClause::sortop, SortGroupClause::tleSortGroupRef, TYPECACHE_GT_OPR, and TYPECACHE_LT_OPR.

Referenced by get_agg_expr_helper(), get_rule_windowspec(), and get_select_query_def().

◆ get_rule_sortgroupclause()

static Node * get_rule_sortgroupclause ( Index  ref,
List tlist,
bool  force_colno,
deparse_context context 
)
static

Definition at line 6552 of file ruleutils.c.

6554{
6555 StringInfo buf = context->buf;
6556 TargetEntry *tle;
6557 Node *expr;
6558
6559 tle = get_sortgroupref_tle(ref, tlist);
6560 expr = (Node *) tle->expr;
6561
6562 /*
6563 * Use column-number form if requested by caller. Otherwise, if
6564 * expression is a constant, force it to be dumped with an explicit cast
6565 * as decoration --- this is because a simple integer constant is
6566 * ambiguous (and will be misinterpreted by findTargetlistEntrySQL92()) if
6567 * we dump it without any decoration. Similarly, if it's just a Var,
6568 * there is risk of misinterpretation if the column name is reassigned in
6569 * the SELECT list, so we may need to force table qualification. And, if
6570 * it's anything more complex than a simple Var, then force extra parens
6571 * around it, to ensure it can't be misinterpreted as a cube() or rollup()
6572 * construct.
6573 */
6574 if (force_colno)
6575 {
6576 Assert(!tle->resjunk);
6577 appendStringInfo(buf, "%d", tle->resno);
6578 }
6579 else if (!expr)
6580 /* do nothing, probably can't happen */ ;
6581 else if (IsA(expr, Const))
6582 get_const_expr((Const *) expr, context, 1);
6583 else if (IsA(expr, Var))
6584 {
6585 /* Tell get_variable to check for name conflict */
6586 bool save_varinorderby = context->varInOrderBy;
6587
6588 context->varInOrderBy = true;
6589 (void) get_variable((Var *) expr, 0, false, context);
6590 context->varInOrderBy = save_varinorderby;
6591 }
6592 else
6593 {
6594 /*
6595 * We must force parens for function-like expressions even if
6596 * PRETTY_PAREN is off, since those are the ones in danger of
6597 * misparsing. For other expressions we need to force them only if
6598 * PRETTY_PAREN is on, since otherwise the expression will output them
6599 * itself. (We can't skip the parens.)
6600 */
6601 bool need_paren = (PRETTY_PAREN(context)
6602 || IsA(expr, FuncExpr)
6603 || IsA(expr, Aggref)
6604 || IsA(expr, WindowFunc)
6605 || IsA(expr, JsonConstructorExpr));
6606
6607 if (need_paren)
6608 appendStringInfoChar(context->buf, '(');
6609 get_rule_expr(expr, context, true);
6610 if (need_paren)
6611 appendStringInfoChar(context->buf, ')');
6612 }
6613
6614 return expr;
6615}
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:345

References appendStringInfo(), appendStringInfoChar(), Assert(), deparse_context::buf, buf, TargetEntry::expr, get_const_expr(), get_rule_expr(), get_sortgroupref_tle(), get_variable(), if(), IsA, PRETTY_PAREN, TargetEntry::resno, and deparse_context::varInOrderBy.

Referenced by get_basic_select_query(), get_rule_groupingset(), get_rule_orderby(), and get_rule_windowspec().

◆ get_rule_windowclause()

static void get_rule_windowclause ( Query query,
deparse_context context 
)
static

Definition at line 6739 of file ruleutils.c.

6740{
6741 StringInfo buf = context->buf;
6742 const char *sep;
6743 ListCell *l;
6744
6745 sep = NULL;
6746 foreach(l, query->windowClause)
6747 {
6748 WindowClause *wc = (WindowClause *) lfirst(l);
6749
6750 if (wc->name == NULL)
6751 continue; /* ignore anonymous windows */
6752
6753 if (sep == NULL)
6754 appendContextKeyword(context, " WINDOW ",
6756 else
6758
6759 appendStringInfo(buf, "%s AS ", quote_identifier(wc->name));
6760
6761 get_rule_windowspec(wc, query->targetList, context);
6762
6763 sep = ", ";
6764 }
6765}
static void get_rule_windowspec(WindowClause *wc, List *targetList, deparse_context *context)
Definition: ruleutils.c:6771

References appendContextKeyword(), appendStringInfo(), appendStringInfoString(), deparse_context::buf, buf, get_rule_windowspec(), lfirst, PRETTYINDENT_STD, quote_identifier(), Query::targetList, and Query::windowClause.

Referenced by get_basic_select_query().

◆ get_rule_windowspec()

static void get_rule_windowspec ( WindowClause wc,
List targetList,
deparse_context context 
)
static

Definition at line 6771 of file ruleutils.c.

6773{
6774 StringInfo buf = context->buf;
6775 bool needspace = false;
6776 const char *sep;
6777 ListCell *l;
6778
6780 if (wc->refname)
6781 {
6783 needspace = true;
6784 }
6785 /* partition clauses are always inherited, so only print if no refname */
6786 if (wc->partitionClause && !wc->refname)
6787 {
6788 if (needspace)
6790 appendStringInfoString(buf, "PARTITION BY ");
6791 sep = "";
6792 foreach(l, wc->partitionClause)
6793 {
6795
6798 false, context);
6799 sep = ", ";
6800 }
6801 needspace = true;
6802 }
6803 /* print ordering clause only if not inherited */
6804 if (wc->orderClause && !wc->copiedOrder)
6805 {
6806 if (needspace)
6808 appendStringInfoString(buf, "ORDER BY ");
6809 get_rule_orderby(wc->orderClause, targetList, false, context);
6810 needspace = true;
6811 }
6812 /* framing clause is never inherited, so print unless it's default */
6814 {
6815 if (needspace)
6818 wc->startOffset, wc->endOffset,
6819 context);
6820 }
6822}
#define FRAMEOPTION_NONDEFAULT
Definition: parsenodes.h:592
static void get_window_frame_options(int frameOptions, Node *startOffset, Node *endOffset, deparse_context *context)
Definition: ruleutils.c:6828
Node * startOffset
Definition: parsenodes.h:1556
List * partitionClause
Definition: parsenodes.h:1552
Node * endOffset
Definition: parsenodes.h:1557
List * orderClause
Definition: parsenodes.h:1554

References appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, WindowClause::endOffset, FRAMEOPTION_NONDEFAULT, WindowClause::frameOptions, get_rule_orderby(), get_rule_sortgroupclause(), get_window_frame_options(), lfirst, WindowClause::orderClause, WindowClause::partitionClause, quote_identifier(), WindowClause::startOffset, and SortGroupClause::tleSortGroupRef.

Referenced by get_rule_windowclause(), and get_windowfunc_expr_helper().

◆ get_select_query_def()

static void get_select_query_def ( Query query,
deparse_context context 
)
static

Definition at line 5906 of file ruleutils.c.

5907{
5908 StringInfo buf = context->buf;
5909 bool force_colno;
5910 ListCell *l;
5911
5912 /* Insert the WITH clause if given */
5913 get_with_clause(query, context);
5914
5915 /* Subroutines may need to consult the SELECT targetlist and windowClause */
5916 context->targetList = query->targetList;
5917 context->windowClause = query->windowClause;
5918
5919 /*
5920 * If the Query node has a setOperations tree, then it's the top level of
5921 * a UNION/INTERSECT/EXCEPT query; only the WITH, ORDER BY and LIMIT
5922 * fields are interesting in the top query itself.
5923 */
5924 if (query->setOperations)
5925 {
5926 get_setop_query(query->setOperations, query, context);
5927 /* ORDER BY clauses must be simple in this case */
5928 force_colno = true;
5929 }
5930 else
5931 {
5932 get_basic_select_query(query, context);
5933 force_colno = false;
5934 }
5935
5936 /* Add the ORDER BY clause if given */
5937 if (query->sortClause != NIL)
5938 {
5939 appendContextKeyword(context, " ORDER BY ",
5941 get_rule_orderby(query->sortClause, query->targetList,
5942 force_colno, context);
5943 }
5944
5945 /*
5946 * Add the LIMIT/OFFSET clauses if given. If non-default options, use the
5947 * standard spelling of LIMIT.
5948 */
5949 if (query->limitOffset != NULL)
5950 {
5951 appendContextKeyword(context, " OFFSET ",
5953 get_rule_expr(query->limitOffset, context, false);
5954 }
5955 if (query->limitCount != NULL)
5956 {
5957 if (query->limitOption == LIMIT_OPTION_WITH_TIES)
5958 {
5959 appendContextKeyword(context, " FETCH FIRST ",
5961 get_rule_expr(query->limitCount, context, false);
5962 appendStringInfoString(buf, " ROWS WITH TIES");
5963 }
5964 else
5965 {
5966 appendContextKeyword(context, " LIMIT ",
5968 if (IsA(query->limitCount, Const) &&
5969 ((Const *) query->limitCount)->constisnull)
5971 else
5972 get_rule_expr(query->limitCount, context, false);
5973 }
5974 }
5975
5976 /* Add FOR [KEY] UPDATE/SHARE clauses if present */
5977 if (query->hasForUpdate)
5978 {
5979 foreach(l, query->rowMarks)
5980 {
5981 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
5982
5983 /* don't print implicit clauses */
5984 if (rc->pushedDown)
5985 continue;
5986
5987 switch (rc->strength)
5988 {
5989 case LCS_NONE:
5990 /* we intentionally throw an error for LCS_NONE */
5991 elog(ERROR, "unrecognized LockClauseStrength %d",
5992 (int) rc->strength);
5993 break;
5994 case LCS_FORKEYSHARE:
5995 appendContextKeyword(context, " FOR KEY SHARE",
5997 break;
5998 case LCS_FORSHARE:
5999 appendContextKeyword(context, " FOR SHARE",
6001 break;
6002 case LCS_FORNOKEYUPDATE:
6003 appendContextKeyword(context, " FOR NO KEY UPDATE",
6005 break;
6006 case LCS_FORUPDATE:
6007 appendContextKeyword(context, " FOR UPDATE",
6009 break;
6010 }
6011
6012 appendStringInfo(buf, " OF %s",
6014 context)));
6015 if (rc->waitPolicy == LockWaitError)
6016 appendStringInfoString(buf, " NOWAIT");
6017 else if (rc->waitPolicy == LockWaitSkip)
6018 appendStringInfoString(buf, " SKIP LOCKED");
6019 }
6020 }
6021}
@ LockWaitSkip
Definition: lockoptions.h:41
@ LockWaitError
Definition: lockoptions.h:43
@ LCS_FORUPDATE
Definition: lockoptions.h:27
@ LCS_NONE
Definition: lockoptions.h:23
@ LCS_FORSHARE
Definition: lockoptions.h:25
@ LCS_FORKEYSHARE
Definition: lockoptions.h:24
@ LCS_FORNOKEYUPDATE
Definition: lockoptions.h:26
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:434
static void get_setop_query(Node *setOp, Query *query, deparse_context *context)
Definition: ruleutils.c:6404
static void get_basic_select_query(Query *query, deparse_context *context)
Definition: ruleutils.c:6098
List * rowMarks
Definition: parsenodes.h:228
Node * limitCount
Definition: parsenodes.h:225
Node * setOperations
Definition: parsenodes.h:230
Node * limitOffset
Definition: parsenodes.h:224
LimitOption limitOption
Definition: parsenodes.h:226
List * sortClause
Definition: parsenodes.h:222
LockClauseStrength strength
Definition: parsenodes.h:1589
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1590

References appendContextKeyword(), appendStringInfo(), appendStringInfoString(), deparse_context::buf, buf, elog, ERROR, get_basic_select_query(), get_rtable_name(), get_rule_expr(), get_rule_orderby(), get_setop_query(), get_with_clause(), IsA, LCS_FORKEYSHARE, LCS_FORNOKEYUPDATE, LCS_FORSHARE, LCS_FORUPDATE, LCS_NONE, lfirst, LIMIT_OPTION_WITH_TIES, Query::limitCount, Query::limitOffset, Query::limitOption, LockWaitError, LockWaitSkip, NIL, PRETTYINDENT_STD, RowMarkClause::pushedDown, quote_identifier(), Query::rowMarks, RowMarkClause::rti, Query::setOperations, Query::sortClause, RowMarkClause::strength, deparse_context::targetList, Query::targetList, RowMarkClause::waitPolicy, deparse_context::windowClause, and Query::windowClause.

Referenced by get_query_def().

◆ get_setop_query()

static void get_setop_query ( Node setOp,
Query query,
deparse_context context 
)
static

Definition at line 6404 of file ruleutils.c.

6405{
6406 StringInfo buf = context->buf;
6407 bool need_paren;
6408
6409 /* Guard against excessively long or deeply-nested queries */
6412
6413 if (IsA(setOp, RangeTblRef))
6414 {
6415 RangeTblRef *rtr = (RangeTblRef *) setOp;
6416 RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable);
6417 Query *subquery = rte->subquery;
6418
6419 Assert(subquery != NULL);
6420
6421 /*
6422 * We need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y.
6423 * Also add parens if the leaf query contains its own set operations.
6424 * (That shouldn't happen unless one of the other clauses is also
6425 * present, see transformSetOperationTree; but let's be safe.)
6426 */
6427 need_paren = (subquery->cteList ||
6428 subquery->sortClause ||
6429 subquery->rowMarks ||
6430 subquery->limitOffset ||
6431 subquery->limitCount ||
6432 subquery->setOperations);
6433 if (need_paren)
6435 get_query_def(subquery, buf, context->namespaces,
6436 context->resultDesc, context->colNamesVisible,
6437 context->prettyFlags, context->wrapColumn,
6438 context->indentLevel);
6439 if (need_paren)
6441 }
6442 else if (IsA(setOp, SetOperationStmt))
6443 {
6444 SetOperationStmt *op = (SetOperationStmt *) setOp;
6445 int subindent;
6446 bool save_colnamesvisible;
6447
6448 /*
6449 * We force parens when nesting two SetOperationStmts, except when the
6450 * lefthand input is another setop of the same kind. Syntactically,
6451 * we could omit parens in rather more cases, but it seems best to use
6452 * parens to flag cases where the setop operator changes. If we use
6453 * parens, we also increase the indentation level for the child query.
6454 *
6455 * There are some cases in which parens are needed around a leaf query
6456 * too, but those are more easily handled at the next level down (see
6457 * code above).
6458 */
6459 if (IsA(op->larg, SetOperationStmt))
6460 {
6461 SetOperationStmt *lop = (SetOperationStmt *) op->larg;
6462
6463 if (op->op == lop->op && op->all == lop->all)
6464 need_paren = false;
6465 else
6466 need_paren = true;
6467 }
6468 else
6469 need_paren = false;
6470
6471 if (need_paren)
6472 {
6474 subindent = PRETTYINDENT_STD;
6475 appendContextKeyword(context, "", subindent, 0, 0);
6476 }
6477 else
6478 subindent = 0;
6479
6480 get_setop_query(op->larg, query, context);
6481
6482 if (need_paren)
6483 appendContextKeyword(context, ") ", -subindent, 0, 0);
6484 else if (PRETTY_INDENT(context))
6485 appendContextKeyword(context, "", -subindent, 0, 0);
6486 else
6488
6489 switch (op->op)
6490 {
6491 case SETOP_UNION:
6492 appendStringInfoString(buf, "UNION ");
6493 break;
6494 case SETOP_INTERSECT:
6495 appendStringInfoString(buf, "INTERSECT ");
6496 break;
6497 case SETOP_EXCEPT:
6498 appendStringInfoString(buf, "EXCEPT ");
6499 break;
6500 default:
6501 elog(ERROR, "unrecognized set op: %d",
6502 (int) op->op);
6503 }
6504 if (op->all)
6505 appendStringInfoString(buf, "ALL ");
6506
6507 /* Always parenthesize if RHS is another setop */
6508 need_paren = IsA(op->rarg, SetOperationStmt);
6509
6510 /*
6511 * The indentation code here is deliberately a bit different from that
6512 * for the lefthand input, because we want the line breaks in
6513 * different places.
6514 */
6515 if (need_paren)
6516 {
6518 subindent = PRETTYINDENT_STD;
6519 }
6520 else
6521 subindent = 0;
6522 appendContextKeyword(context, "", subindent, 0, 0);
6523
6524 /*
6525 * The output column names of the RHS sub-select don't matter.
6526 */
6527 save_colnamesvisible = context->colNamesVisible;
6528 context->colNamesVisible = false;
6529
6530 get_setop_query(op->rarg, query, context);
6531
6532 context->colNamesVisible = save_colnamesvisible;
6533
6534 if (PRETTY_INDENT(context))
6535 context->indentLevel -= subindent;
6536 if (need_paren)
6537 appendContextKeyword(context, ")", 0, 0, 0);
6538 }
6539 else
6540 {
6541 elog(ERROR, "unrecognized node type: %d",
6542 (int) nodeTag(setOp));
6543 }
6544}
@ SETOP_INTERSECT
Definition: parsenodes.h:2164
@ SETOP_UNION
Definition: parsenodes.h:2163
@ SETOP_EXCEPT
Definition: parsenodes.h:2165
List * cteList
Definition: parsenodes.h:168
SetOperation op
Definition: parsenodes.h:2242

References SetOperationStmt::all, appendContextKeyword(), appendStringInfoChar(), appendStringInfoString(), Assert(), deparse_context::buf, buf, CHECK_FOR_INTERRUPTS, check_stack_depth(), deparse_context::colNamesVisible, Query::cteList, elog, ERROR, get_query_def(), get_setop_query(), if(), deparse_context::indentLevel, IsA, SetOperationStmt::larg, Query::limitCount, Query::limitOffset, deparse_context::namespaces, nodeTag, SetOperationStmt::op, PRETTY_INDENT, deparse_context::prettyFlags, PRETTYINDENT_STD, SetOperationStmt::rarg, deparse_context::resultDesc, Query::rowMarks, rt_fetch, Query::rtable, RangeTblRef::rtindex, SETOP_EXCEPT, SETOP_INTERSECT, SETOP_UNION, Query::setOperations, Query::sortClause, RangeTblEntry::subquery, and deparse_context::wrapColumn.

Referenced by get_select_query_def(), and get_setop_query().

◆ get_simple_binary_op_name()

static const char * get_simple_binary_op_name ( OpExpr expr)
static

Definition at line 8814 of file ruleutils.c.

8815{
8816 List *args = expr->args;
8817
8818 if (list_length(args) == 2)
8819 {
8820 /* binary operator */
8821 Node *arg1 = (Node *) linitial(args);
8822 Node *arg2 = (Node *) lsecond(args);
8823 const char *op;
8824
8825 op = generate_operator_name(expr->opno, exprType(arg1), exprType(arg2));
8826 if (strlen(op) == 1)
8827 return op;
8828 }
8829 return NULL;
8830}

References generate_unaccent_rules::args, OpExpr::args, exprType(), generate_operator_name(), linitial, list_length(), lsecond, and OpExpr::opno.

Referenced by isSimpleNode().

◆ get_simple_values_rte()

static RangeTblEntry * get_simple_values_rte ( Query query,
TupleDesc  resultDesc 
)
static

Definition at line 6029 of file ruleutils.c.

6030{
6031 RangeTblEntry *result = NULL;
6032 ListCell *lc;
6033
6034 /*
6035 * We want to detect a match even if the Query also contains OLD or NEW
6036 * rule RTEs. So the idea is to scan the rtable and see if there is only
6037 * one inFromCl RTE that is a VALUES RTE.
6038 */
6039 foreach(lc, query->rtable)
6040 {
6041 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
6042
6043 if (rte->rtekind == RTE_VALUES && rte->inFromCl)
6044 {
6045 if (result)
6046 return NULL; /* multiple VALUES (probably not possible) */
6047 result = rte;
6048 }
6049 else if (rte->rtekind == RTE_RELATION && !rte->inFromCl)
6050 continue; /* ignore rule entries */
6051 else
6052 return NULL; /* something else -> not simple VALUES */
6053 }
6054
6055 /*
6056 * We don't need to check the targetlist in any great detail, because
6057 * parser/analyze.c will never generate a "bare" VALUES RTE --- they only
6058 * appear inside auto-generated sub-queries with very restricted
6059 * structure. However, DefineView might have modified the tlist by
6060 * injecting new column aliases, or we might have some other column
6061 * aliases forced by a resultDesc. We can only simplify if the RTE's
6062 * column names match the names that get_target_list() would select.
6063 */
6064 if (result)
6065 {
6066 ListCell *lcn;
6067 int colno;
6068
6069 if (list_length(query->targetList) != list_length(result->eref->colnames))
6070 return NULL; /* this probably cannot happen */
6071 colno = 0;
6072 forboth(lc, query->targetList, lcn, result->eref->colnames)
6073 {
6074 TargetEntry *tle = (TargetEntry *) lfirst(lc);
6075 char *cname = strVal(lfirst(lcn));
6076 char *colname;
6077
6078 if (tle->resjunk)
6079 return NULL; /* this probably cannot happen */
6080
6081 /* compute name that get_target_list would use for column */
6082 colno++;
6083 if (resultDesc && colno <= resultDesc->natts)
6084 colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
6085 else
6086 colname = tle->resname;
6087
6088 /* does it match the VALUES RTE? */
6089 if (colname == NULL || strcmp(colname, cname) != 0)
6090 return NULL; /* column name has been changed */
6091 }
6092 }
6093
6094 return result;
6095}

References attname, forboth, lfirst, list_length(), NameStr, Query::rtable, RTE_RELATION, RTE_VALUES, RangeTblEntry::rtekind, strVal, Query::targetList, and TupleDescAttr().

Referenced by get_basic_select_query().

◆ get_special_variable()

static void get_special_variable ( Node node,
deparse_context context,
void *  callback_arg 
)
static

Definition at line 7878 of file ruleutils.c.

7879{
7880 StringInfo buf = context->buf;
7881
7882 /*
7883 * For a non-Var referent, force parentheses because our caller probably
7884 * assumed a Var is a simple expression.
7885 */
7886 if (!IsA(node, Var))
7888 get_rule_expr(node, context, true);
7889 if (!IsA(node, Var))
7891}

References appendStringInfoChar(), deparse_context::buf, buf, get_rule_expr(), and IsA.

Referenced by get_variable().

◆ get_sublink_expr()

static void get_sublink_expr ( SubLink sublink,
deparse_context context 
)
static

Definition at line 11810 of file ruleutils.c.

11811{
11812 StringInfo buf = context->buf;
11813 Query *query = (Query *) (sublink->subselect);
11814 char *opname = NULL;
11815 bool need_paren;
11816
11817 if (sublink->subLinkType == ARRAY_SUBLINK)
11818 appendStringInfoString(buf, "ARRAY(");
11819 else
11821
11822 /*
11823 * Note that we print the name of only the first operator, when there are
11824 * multiple combining operators. This is an approximation that could go
11825 * wrong in various scenarios (operators in different schemas, renamed
11826 * operators, etc) but there is not a whole lot we can do about it, since
11827 * the syntax allows only one operator to be shown.
11828 */
11829 if (sublink->testexpr)
11830 {
11831 if (IsA(sublink->testexpr, OpExpr))
11832 {
11833 /* single combining operator */
11834 OpExpr *opexpr = (OpExpr *) sublink->testexpr;
11835
11836 get_rule_expr(linitial(opexpr->args), context, true);
11837 opname = generate_operator_name(opexpr->opno,
11838 exprType(linitial(opexpr->args)),
11839 exprType(lsecond(opexpr->args)));
11840 }
11841 else if (IsA(sublink->testexpr, BoolExpr))
11842 {
11843 /* multiple combining operators, = or <> cases */
11844 char *sep;
11845 ListCell *l;
11846
11848 sep = "";
11849 foreach(l, ((BoolExpr *) sublink->testexpr)->args)
11850 {
11851 OpExpr *opexpr = lfirst_node(OpExpr, l);
11852
11854 get_rule_expr(linitial(opexpr->args), context, true);
11855 if (!opname)
11856 opname = generate_operator_name(opexpr->opno,
11857 exprType(linitial(opexpr->args)),
11858 exprType(lsecond(opexpr->args)));
11859 sep = ", ";
11860 }
11862 }
11863 else if (IsA(sublink->testexpr, RowCompareExpr))
11864 {
11865 /* multiple combining operators, < <= > >= cases */
11866 RowCompareExpr *rcexpr = (RowCompareExpr *) sublink->testexpr;
11867
11869 get_rule_expr((Node *) rcexpr->largs, context, true);
11870 opname = generate_operator_name(linitial_oid(rcexpr->opnos),
11871 exprType(linitial(rcexpr->largs)),
11872 exprType(linitial(rcexpr->rargs)));
11874 }
11875 else
11876 elog(ERROR, "unrecognized testexpr type: %d",
11877 (int) nodeTag(sublink->testexpr));
11878 }
11879
11880 need_paren = true;
11881
11882 switch (sublink->subLinkType)
11883 {
11884 case EXISTS_SUBLINK:
11885 appendStringInfoString(buf, "EXISTS ");
11886 break;
11887
11888 case ANY_SUBLINK:
11889 if (strcmp(opname, "=") == 0) /* Represent = ANY as IN */
11890 appendStringInfoString(buf, " IN ");
11891 else
11892 appendStringInfo(buf, " %s ANY ", opname);
11893 break;
11894
11895 case ALL_SUBLINK:
11896 appendStringInfo(buf, " %s ALL ", opname);
11897 break;
11898
11899 case ROWCOMPARE_SUBLINK:
11900 appendStringInfo(buf, " %s ", opname);
11901 break;
11902
11903 case EXPR_SUBLINK:
11904 case MULTIEXPR_SUBLINK:
11905 case ARRAY_SUBLINK:
11906 need_paren = false;
11907 break;
11908
11909 case CTE_SUBLINK: /* shouldn't occur in a SubLink */
11910 default:
11911 elog(ERROR, "unrecognized sublink type: %d",
11912 (int) sublink->subLinkType);
11913 break;
11914 }
11915
11916 if (need_paren)
11918
11919 get_query_def(query, buf, context->namespaces, NULL, false,
11920 context->prettyFlags, context->wrapColumn,
11921 context->indentLevel);
11922
11923 if (need_paren)
11925 else
11927}

References ALL_SUBLINK, ANY_SUBLINK, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), OpExpr::args, ARRAY_SUBLINK, deparse_context::buf, buf, CTE_SUBLINK, elog, ERROR, EXISTS_SUBLINK, EXPR_SUBLINK, exprType(), generate_operator_name(), get_query_def(), get_rule_expr(), deparse_context::indentLevel, IsA, RowCompareExpr::largs, lfirst_node, linitial, linitial_oid, lsecond, MULTIEXPR_SUBLINK, deparse_context::namespaces, nodeTag, OpExpr::opno, deparse_context::prettyFlags, RowCompareExpr::rargs, ROWCOMPARE_SUBLINK, SubLink::subLinkType, SubLink::subselect, SubLink::testexpr, and deparse_context::wrapColumn.

Referenced by get_rule_expr().

◆ get_tablefunc()

static void get_tablefunc ( TableFunc tf,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 12241 of file ruleutils.c.

12242{
12243 /* XMLTABLE and JSON_TABLE are the only existing implementations. */
12244
12245 if (tf->functype == TFT_XMLTABLE)
12246 get_xmltable(tf, context, showimplicit);
12247 else if (tf->functype == TFT_JSON_TABLE)
12248 get_json_table(tf, context, showimplicit);
12249}
@ TFT_XMLTABLE
Definition: primnodes.h:100
@ TFT_JSON_TABLE
Definition: primnodes.h:101
static void get_xmltable(TableFunc *tf, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:11935
static void get_json_table(TableFunc *tf, deparse_context *context, bool showimplicit)
Definition: ruleutils.c:12172
TableFuncType functype
Definition: primnodes.h:114

References TableFunc::functype, get_json_table(), get_xmltable(), TFT_JSON_TABLE, and TFT_XMLTABLE.

Referenced by get_from_clause_item(), and get_rule_expr().

◆ get_tablesample_def()

static void get_tablesample_def ( TableSampleClause tablesample,
deparse_context context 
)
static

Definition at line 12807 of file ruleutils.c.

12808{
12809 StringInfo buf = context->buf;
12810 Oid argtypes[1];
12811 int nargs;
12812 ListCell *l;
12813
12814 /*
12815 * We should qualify the handler's function name if it wouldn't be
12816 * resolved by lookup in the current search path.
12817 */
12818 argtypes[0] = INTERNALOID;
12819 appendStringInfo(buf, " TABLESAMPLE %s (",
12820 generate_function_name(tablesample->tsmhandler, 1,
12821 NIL, argtypes,
12822 false, NULL, false));
12823
12824 nargs = 0;
12825 foreach(l, tablesample->args)
12826 {
12827 if (nargs++ > 0)
12829 get_rule_expr((Node *) lfirst(l), context, false);
12830 }
12832
12833 if (tablesample->repeatable != NULL)
12834 {
12835 appendStringInfoString(buf, " REPEATABLE (");
12836 get_rule_expr((Node *) tablesample->repeatable, context, false);
12838 }
12839}

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), TableSampleClause::args, deparse_context::buf, buf, generate_function_name(), get_rule_expr(), lfirst, NIL, TableSampleClause::repeatable, and TableSampleClause::tsmhandler.

Referenced by get_from_clause_item().

◆ get_target_list()

static void get_target_list ( List targetList,
deparse_context context 
)
static

Definition at line 6229 of file ruleutils.c.

6230{
6231 StringInfo buf = context->buf;
6232 StringInfoData targetbuf;
6233 bool last_was_multiline = false;
6234 char *sep;
6235 int colno;
6236 ListCell *l;
6237
6238 /* we use targetbuf to hold each TLE's text temporarily */
6239 initStringInfo(&targetbuf);
6240
6241 sep = " ";
6242 colno = 0;
6243 foreach(l, targetList)
6244 {
6245 TargetEntry *tle = (TargetEntry *) lfirst(l);
6246 char *colname;
6247 char *attname;
6248
6249 if (tle->resjunk)
6250 continue; /* ignore junk entries */
6251
6253 sep = ", ";
6254 colno++;
6255
6256 /*
6257 * Put the new field text into targetbuf so we can decide after we've
6258 * got it whether or not it needs to go on a new line.
6259 */
6260 resetStringInfo(&targetbuf);
6261 context->buf = &targetbuf;
6262
6263 /*
6264 * We special-case Var nodes rather than using get_rule_expr. This is
6265 * needed because get_rule_expr will display a whole-row Var as
6266 * "foo.*", which is the preferred notation in most contexts, but at
6267 * the top level of a SELECT list it's not right (the parser will
6268 * expand that notation into multiple columns, yielding behavior
6269 * different from a whole-row Var). We need to call get_variable
6270 * directly so that we can tell it to do the right thing, and so that
6271 * we can get the attribute name which is the default AS label.
6272 */
6273 if (tle->expr && (IsA(tle->expr, Var)))
6274 {
6275 attname = get_variable((Var *) tle->expr, 0, true, context);
6276 }
6277 else
6278 {
6279 get_rule_expr((Node *) tle->expr, context, true);
6280
6281 /*
6282 * When colNamesVisible is true, we should always show the
6283 * assigned column name explicitly. Otherwise, show it only if
6284 * it's not FigureColname's fallback.
6285 */
6286 attname = context->colNamesVisible ? NULL : "?column?";
6287 }
6288
6289 /*
6290 * Figure out what the result column should be called. In the context
6291 * of a view, use the view's tuple descriptor (so as to pick up the
6292 * effects of any column RENAME that's been done on the view).
6293 * Otherwise, just use what we can find in the TLE.
6294 */
6295 if (context->resultDesc && colno <= context->resultDesc->natts)
6296 colname = NameStr(TupleDescAttr(context->resultDesc,
6297 colno - 1)->attname);
6298 else
6299 colname = tle->resname;
6300
6301 /* Show AS unless the column's name is correct as-is */
6302 if (colname) /* resname could be NULL */
6303 {
6304 if (attname == NULL || strcmp(attname, colname) != 0)
6305 appendStringInfo(&targetbuf, " AS %s", quote_identifier(colname));
6306 }
6307
6308 /* Restore context's output buffer */
6309 context->buf = buf;
6310
6311 /* Consider line-wrapping if enabled */
6312 if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
6313 {
6314 int leading_nl_pos;
6315
6316 /* Does the new field start with a new line? */
6317 if (targetbuf.len > 0 && targetbuf.data[0] == '\n')
6318 leading_nl_pos = 0;
6319 else
6320 leading_nl_pos = -1;
6321
6322 /* If so, we shouldn't add anything */
6323 if (leading_nl_pos >= 0)
6324 {
6325 /* instead, remove any trailing spaces currently in buf */
6327 }
6328 else
6329 {
6330 char *trailing_nl;
6331
6332 /* Locate the start of the current line in the output buffer */
6333 trailing_nl = strrchr(buf->data, '\n');
6334 if (trailing_nl == NULL)
6335 trailing_nl = buf->data;
6336 else
6337 trailing_nl++;
6338
6339 /*
6340 * Add a newline, plus some indentation, if the new field is
6341 * not the first and either the new field would cause an
6342 * overflow or the last field used more than one line.
6343 */
6344 if (colno > 1 &&
6345 ((strlen(trailing_nl) + targetbuf.len > context->wrapColumn) ||
6346 last_was_multiline))
6349 }
6350
6351 /* Remember this field's multiline status for next iteration */
6352 last_was_multiline =
6353 (strchr(targetbuf.data + leading_nl_pos + 1, '\n') != NULL);
6354 }
6355
6356 /* Add the new field */
6357 appendBinaryStringInfo(buf, targetbuf.data, targetbuf.len);
6358 }
6359
6360 /* clean up */
6361 pfree(targetbuf.data);
6362}
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126

References appendBinaryStringInfo(), appendContextKeyword(), appendStringInfo(), appendStringInfoString(), attname, deparse_context::buf, buf, deparse_context::colNamesVisible, StringInfoData::data, TargetEntry::expr, get_rule_expr(), get_variable(), initStringInfo(), IsA, StringInfoData::len, lfirst, NameStr, TupleDescData::natts, pfree(), PRETTY_INDENT, PRETTYINDENT_STD, PRETTYINDENT_VAR, quote_identifier(), removeStringInfoSpaces(), resetStringInfo(), deparse_context::resultDesc, TupleDescAttr(), and deparse_context::wrapColumn.

Referenced by get_basic_select_query(), and get_returning_clause().

◆ get_update_query_def()

static void get_update_query_def ( Query query,
deparse_context context 
)
static

Definition at line 7141 of file ruleutils.c.

7142{
7143 StringInfo buf = context->buf;
7144 RangeTblEntry *rte;
7145
7146 /* Insert the WITH clause if given */
7147 get_with_clause(query, context);
7148
7149 /*
7150 * Start the query with UPDATE relname SET
7151 */
7152 rte = rt_fetch(query->resultRelation, query->rtable);
7153 Assert(rte->rtekind == RTE_RELATION);
7154 if (PRETTY_INDENT(context))
7155 {
7157 context->indentLevel += PRETTYINDENT_STD;
7158 }
7159 appendStringInfo(buf, "UPDATE %s%s",
7160 only_marker(rte),
7162
7163 /* Print the relation alias, if needed */
7164 get_rte_alias(rte, query->resultRelation, false, context);
7165
7166 appendStringInfoString(buf, " SET ");
7167
7168 /* Deparse targetlist */
7169 get_update_query_targetlist_def(query, query->targetList, context, rte);
7170
7171 /* Add the FROM clause if needed */
7172 get_from_clause(query, " FROM ", context);
7173
7174 /* Add a WHERE clause if given */
7175 if (query->jointree->quals != NULL)
7176 {
7177 appendContextKeyword(context, " WHERE ",
7179 get_rule_expr(query->jointree->quals, context, false);
7180 }
7181
7182 /* Add RETURNING if present */
7183 if (query->returningList)
7184 get_returning_clause(query, context);
7185}

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), deparse_context::buf, buf, generate_relation_name(), get_from_clause(), get_returning_clause(), get_rte_alias(), get_rule_expr(), get_update_query_targetlist_def(), get_with_clause(), deparse_context::indentLevel, Query::jointree, NIL, only_marker, PRETTY_INDENT, PRETTYINDENT_STD, FromExpr::quals, RangeTblEntry::relid, Query::returningList, rt_fetch, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, and Query::targetList.

Referenced by get_query_def().

◆ get_update_query_targetlist_def()

static void get_update_query_targetlist_def ( Query query,
List targetList,
deparse_context context,
RangeTblEntry rte 
)
static

Definition at line 7193 of file ruleutils.c.

7195{
7196 StringInfo buf = context->buf;
7197 ListCell *l;
7198 ListCell *next_ma_cell;
7199 int remaining_ma_columns;
7200 const char *sep;
7201 SubLink *cur_ma_sublink;
7202 List *ma_sublinks;
7203
7204 /*
7205 * Prepare to deal with MULTIEXPR assignments: collect the source SubLinks
7206 * into a list. We expect them to appear, in ID order, in resjunk tlist
7207 * entries.
7208 */
7209 ma_sublinks = NIL;
7210 if (query->hasSubLinks) /* else there can't be any */
7211 {
7212 foreach(l, targetList)
7213 {
7214 TargetEntry *tle = (TargetEntry *) lfirst(l);
7215
7216 if (tle->resjunk && IsA(tle->expr, SubLink))
7217 {
7218 SubLink *sl = (SubLink *) tle->expr;
7219
7221 {
7222 ma_sublinks = lappend(ma_sublinks, sl);
7223 Assert(sl->subLinkId == list_length(ma_sublinks));
7224 }
7225 }
7226 }
7227 }
7228 next_ma_cell = list_head(ma_sublinks);
7229 cur_ma_sublink = NULL;
7230 remaining_ma_columns = 0;
7231
7232 /* Add the comma separated list of 'attname = value' */
7233 sep = "";
7234 foreach(l, targetList)
7235 {
7236 TargetEntry *tle = (TargetEntry *) lfirst(l);
7237 Node *expr;
7238
7239 if (tle->resjunk)
7240 continue; /* ignore junk entries */
7241
7242 /* Emit separator (OK whether we're in multiassignment or not) */
7244 sep = ", ";
7245
7246 /*
7247 * Check to see if we're starting a multiassignment group: if so,
7248 * output a left paren.
7249 */
7250 if (next_ma_cell != NULL && cur_ma_sublink == NULL)
7251 {
7252 /*
7253 * We must dig down into the expr to see if it's a PARAM_MULTIEXPR
7254 * Param. That could be buried under FieldStores and
7255 * SubscriptingRefs and CoerceToDomains (cf processIndirection()),
7256 * and underneath those there could be an implicit type coercion.
7257 * Because we would ignore implicit type coercions anyway, we
7258 * don't need to be as careful as processIndirection() is about
7259 * descending past implicit CoerceToDomains.
7260 */
7261 expr = (Node *) tle->expr;
7262 while (expr)
7263 {
7264 if (IsA(expr, FieldStore))
7265 {
7266 FieldStore *fstore = (FieldStore *) expr;
7267
7268 expr = (Node *) linitial(fstore->newvals);
7269 }
7270 else if (IsA(expr, SubscriptingRef))
7271 {
7272 SubscriptingRef *sbsref = (SubscriptingRef *) expr;
7273
7274 if (sbsref->refassgnexpr == NULL)
7275 break;
7276
7277 expr = (Node *) sbsref->refassgnexpr;
7278 }
7279 else if (IsA(expr, CoerceToDomain))
7280 {
7281 CoerceToDomain *cdomain = (CoerceToDomain *) expr;
7282
7283 if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
7284 break;
7285 expr = (Node *) cdomain->arg;
7286 }
7287 else
7288 break;
7289 }
7290 expr = strip_implicit_coercions(expr);
7291
7292 if (expr && IsA(expr, Param) &&
7293 ((Param *) expr)->paramkind == PARAM_MULTIEXPR)
7294 {
7295 cur_ma_sublink = (SubLink *) lfirst(next_ma_cell);
7296 next_ma_cell = lnext(ma_sublinks, next_ma_cell);
7297 remaining_ma_columns = count_nonjunk_tlist_entries(((Query *) cur_ma_sublink->subselect)->targetList);
7298 Assert(((Param *) expr)->paramid ==
7299 ((cur_ma_sublink->subLinkId << 16) | 1));
7301 }
7302 }
7303
7304 /*
7305 * Put out name of target column; look in the catalogs, not at
7306 * tle->resname, since resname will fail to track RENAME.
7307 */
7310 tle->resno,
7311 false)));
7312
7313 /*
7314 * Print any indirection needed (subfields or subscripts), and strip
7315 * off the top-level nodes representing the indirection assignments.
7316 */
7317 expr = processIndirection((Node *) tle->expr, context);
7318
7319 /*
7320 * If we're in a multiassignment, skip printing anything more, unless
7321 * this is the last column; in which case, what we print should be the
7322 * sublink, not the Param.
7323 */
7324 if (cur_ma_sublink != NULL)
7325 {
7326 if (--remaining_ma_columns > 0)
7327 continue; /* not the last column of multiassignment */
7329 expr = (Node *) cur_ma_sublink;
7330 cur_ma_sublink = NULL;
7331 }
7332
7334
7335 get_rule_expr(expr, context, false);
7336 }
7337}
while(p+4<=pend)
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
@ PARAM_MULTIEXPR
Definition: primnodes.h:387
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:186

References appendStringInfoChar(), appendStringInfoString(), CoerceToDomain::arg, Assert(), deparse_context::buf, buf, COERCE_IMPLICIT_CAST, count_nonjunk_tlist_entries(), TargetEntry::expr, get_attname(), get_rule_expr(), if(), IsA, lappend(), lfirst, linitial, list_head(), list_length(), lnext(), MULTIEXPR_SUBLINK, FieldStore::newvals, NIL, PARAM_MULTIEXPR, processIndirection(), quote_identifier(), SubscriptingRef::refassgnexpr, RangeTblEntry::relid, TargetEntry::resno, strip_implicit_coercions(), SubLink::subLinkId, SubLink::subLinkType, SubLink::subselect, and while().

Referenced by get_insert_query_def(), get_merge_query_def(), and get_update_query_def().

◆ get_utility_query_def()

static void get_utility_query_def ( Query query,
deparse_context context 
)
static

Definition at line 7551 of file ruleutils.c.

7552{
7553 StringInfo buf = context->buf;
7554
7555 if (query->utilityStmt && IsA(query->utilityStmt, NotifyStmt))
7556 {
7557 NotifyStmt *stmt = (NotifyStmt *) query->utilityStmt;
7558
7559 appendContextKeyword(context, "",
7560 0, PRETTYINDENT_STD, 1);
7561 appendStringInfo(buf, "NOTIFY %s",
7562 quote_identifier(stmt->conditionname));
7563 if (stmt->payload)
7564 {
7566 simple_quote_literal(buf, stmt->payload);
7567 }
7568 }
7569 else
7570 {
7571 /* Currently only NOTIFY utility commands can appear in rules */
7572 elog(ERROR, "unexpected utility statement type");
7573 }
7574}
#define stmt
Definition: indent_codes.h:59
Node * utilityStmt
Definition: parsenodes.h:136

References appendContextKeyword(), appendStringInfo(), appendStringInfoString(), deparse_context::buf, buf, elog, ERROR, IsA, PRETTYINDENT_STD, quote_identifier(), simple_quote_literal(), stmt, and Query::utilityStmt.

Referenced by get_query_def().

◆ get_values_def()

static void get_values_def ( List values_lists,
deparse_context context 
)
static

Definition at line 5724 of file ruleutils.c.

5725{
5726 StringInfo buf = context->buf;
5727 bool first_list = true;
5728 ListCell *vtl;
5729
5730 appendStringInfoString(buf, "VALUES ");
5731
5732 foreach(vtl, values_lists)
5733 {
5734 List *sublist = (List *) lfirst(vtl);
5735 bool first_col = true;
5736 ListCell *lc;
5737
5738 if (first_list)
5739 first_list = false;
5740 else
5742
5744 foreach(lc, sublist)
5745 {
5746 Node *col = (Node *) lfirst(lc);
5747
5748 if (first_col)
5749 first_col = false;
5750 else
5752
5753 /*
5754 * Print the value. Whole-row Vars need special treatment.
5755 */
5756 get_rule_expr_toplevel(col, context, false);
5757 }
5759 }
5760}

References appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, get_rule_expr_toplevel(), and lfirst.

Referenced by get_basic_select_query(), get_from_clause_item(), and get_insert_query_def().

◆ get_variable()

static char * get_variable ( Var var,
int  levelsup,
bool  istoplevel,
deparse_context context 
)
static

Definition at line 7596 of file ruleutils.c.

7597{
7598 StringInfo buf = context->buf;
7599 RangeTblEntry *rte;
7601 int netlevelsup;
7602 deparse_namespace *dpns;
7603 int varno;
7604 AttrNumber varattno;
7605 deparse_columns *colinfo;
7606 char *refname;
7607 char *attname;
7608 bool need_prefix;
7609
7610 /* Find appropriate nesting depth */
7611 netlevelsup = var->varlevelsup + levelsup;
7612 if (netlevelsup >= list_length(context->namespaces))
7613 elog(ERROR, "bogus varlevelsup: %d offset %d",
7614 var->varlevelsup, levelsup);
7615 dpns = (deparse_namespace *) list_nth(context->namespaces,
7616 netlevelsup);
7617
7618 /*
7619 * If we have a syntactic referent for the Var, and we're working from a
7620 * parse tree, prefer to use the syntactic referent. Otherwise, fall back
7621 * on the semantic referent. (Forcing use of the semantic referent when
7622 * printing plan trees is a design choice that's perhaps more motivated by
7623 * backwards compatibility than anything else. But it does have the
7624 * advantage of making plans more explicit.)
7625 */
7626 if (var->varnosyn > 0 && dpns->plan == NULL)
7627 {
7628 varno = var->varnosyn;
7629 varattno = var->varattnosyn;
7630 }
7631 else
7632 {
7633 varno = var->varno;
7634 varattno = var->varattno;
7635 }
7636
7637 /*
7638 * Try to find the relevant RTE in this rtable. In a plan tree, it's
7639 * likely that varno is OUTER_VAR or INNER_VAR, in which case we must dig
7640 * down into the subplans, or INDEX_VAR, which is resolved similarly. Also
7641 * find the aliases previously assigned for this RTE.
7642 */
7643 if (varno >= 1 && varno <= list_length(dpns->rtable))
7644 {
7645 /*
7646 * We might have been asked to map child Vars to some parent relation.
7647 */
7648 if (context->appendparents && dpns->appendrels)
7649 {
7650 int pvarno = varno;
7651 AttrNumber pvarattno = varattno;
7652 AppendRelInfo *appinfo = dpns->appendrels[pvarno];
7653 bool found = false;
7654
7655 /* Only map up to inheritance parents, not UNION ALL appendrels */
7656 while (appinfo &&
7657 rt_fetch(appinfo->parent_relid,
7658 dpns->rtable)->rtekind == RTE_RELATION)
7659 {
7660 found = false;
7661 if (pvarattno > 0) /* system columns stay as-is */
7662 {
7663 if (pvarattno > appinfo->num_child_cols)
7664 break; /* safety check */
7665 pvarattno = appinfo->parent_colnos[pvarattno - 1];
7666 if (pvarattno == 0)
7667 break; /* Var is local to child */
7668 }
7669
7670 pvarno = appinfo->parent_relid;
7671 found = true;
7672
7673 /* If the parent is itself a child, continue up. */
7674 Assert(pvarno > 0 && pvarno <= list_length(dpns->rtable));
7675 appinfo = dpns->appendrels[pvarno];
7676 }
7677
7678 /*
7679 * If we found an ancestral rel, and that rel is included in
7680 * appendparents, print that column not the original one.
7681 */
7682 if (found && bms_is_member(pvarno, context->appendparents))
7683 {
7684 varno = pvarno;
7685 varattno = pvarattno;
7686 }
7687 }
7688
7689 rte = rt_fetch(varno, dpns->rtable);
7690
7691 /* might be returning old/new column value */
7693 refname = dpns->ret_old_alias;
7694 else if (var->varreturningtype == VAR_RETURNING_NEW)
7695 refname = dpns->ret_new_alias;
7696 else
7697 refname = (char *) list_nth(dpns->rtable_names, varno - 1);
7698
7699 colinfo = deparse_columns_fetch(varno, dpns);
7700 attnum = varattno;
7701 }
7702 else
7703 {
7704 resolve_special_varno((Node *) var, context,
7705 get_special_variable, NULL);
7706 return NULL;
7707 }
7708
7709 /*
7710 * The planner will sometimes emit Vars referencing resjunk elements of a
7711 * subquery's target list (this is currently only possible if it chooses
7712 * to generate a "physical tlist" for a SubqueryScan or CteScan node).
7713 * Although we prefer to print subquery-referencing Vars using the
7714 * subquery's alias, that's not possible for resjunk items since they have
7715 * no alias. So in that case, drill down to the subplan and print the
7716 * contents of the referenced tlist item. This works because in a plan
7717 * tree, such Vars can only occur in a SubqueryScan or CteScan node, and
7718 * we'll have set dpns->inner_plan to reference the child plan node.
7719 */
7720 if ((rte->rtekind == RTE_SUBQUERY || rte->rtekind == RTE_CTE) &&
7721 attnum > list_length(rte->eref->colnames) &&
7722 dpns->inner_plan)
7723 {
7724 TargetEntry *tle;
7725 deparse_namespace save_dpns;
7726
7727 tle = get_tle_by_resno(dpns->inner_tlist, attnum);
7728 if (!tle)
7729 elog(ERROR, "invalid attnum %d for relation \"%s\"",
7730 attnum, rte->eref->aliasname);
7731
7732 Assert(netlevelsup == 0);
7733 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
7734
7735 /*
7736 * Force parentheses because our caller probably assumed a Var is a
7737 * simple expression.
7738 */
7739 if (!IsA(tle->expr, Var))
7741 get_rule_expr((Node *) tle->expr, context, true);
7742 if (!IsA(tle->expr, Var))
7744
7745 pop_child_plan(dpns, &save_dpns);
7746 return NULL;
7747 }
7748
7749 /*
7750 * If it's an unnamed join, look at the expansion of the alias variable.
7751 * If it's a simple reference to one of the input vars, then recursively
7752 * print the name of that var instead. When it's not a simple reference,
7753 * we have to just print the unqualified join column name. (This can only
7754 * happen with "dangerous" merged columns in a JOIN USING; we took pains
7755 * previously to make the unqualified column name unique in such cases.)
7756 *
7757 * This wouldn't work in decompiling plan trees, because we don't store
7758 * joinaliasvars lists after planning; but a plan tree should never
7759 * contain a join alias variable.
7760 */
7761 if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
7762 {
7763 if (rte->joinaliasvars == NIL)
7764 elog(ERROR, "cannot decompile join alias var in plan tree");
7765 if (attnum > 0)
7766 {
7767 Var *aliasvar;
7768
7769 aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
7770 /* we intentionally don't strip implicit coercions here */
7771 if (aliasvar && IsA(aliasvar, Var))
7772 {
7773 return get_variable(aliasvar, var->varlevelsup + levelsup,
7774 istoplevel, context);
7775 }
7776 }
7777
7778 /*
7779 * Unnamed join has no refname. (Note: since it's unnamed, there is
7780 * no way the user could have referenced it to create a whole-row Var
7781 * for it. So we don't have to cover that case below.)
7782 */
7783 Assert(refname == NULL);
7784 }
7785
7787 attname = NULL;
7788 else if (attnum > 0)
7789 {
7790 /* Get column name to use from the colinfo struct */
7791 if (attnum > colinfo->num_cols)
7792 elog(ERROR, "invalid attnum %d for relation \"%s\"",
7793 attnum, rte->eref->aliasname);
7794 attname = colinfo->colnames[attnum - 1];
7795
7796 /*
7797 * If we find a Var referencing a dropped column, it seems better to
7798 * print something (anything) than to fail. In general this should
7799 * not happen, but it used to be possible for some cases involving
7800 * functions returning named composite types, and perhaps there are
7801 * still bugs out there.
7802 */
7803 if (attname == NULL)
7804 attname = "?dropped?column?";
7805 }
7806 else
7807 {
7808 /* System column - name is fixed, get it from the catalog */
7810 }
7811
7812 need_prefix = (context->varprefix || attname == NULL ||
7814
7815 /*
7816 * If we're considering a plain Var in an ORDER BY (but not GROUP BY)
7817 * clause, we may need to add a table-name prefix to prevent
7818 * findTargetlistEntrySQL92 from misinterpreting the name as an
7819 * output-column name. To avoid cluttering the output with unnecessary
7820 * prefixes, do so only if there is a name match to a SELECT tlist item
7821 * that is different from the Var.
7822 */
7823 if (context->varInOrderBy && !context->inGroupBy && !need_prefix)
7824 {
7825 int colno = 0;
7826
7827 foreach_node(TargetEntry, tle, context->targetList)
7828 {
7829 char *colname;
7830
7831 if (tle->resjunk)
7832 continue; /* ignore junk entries */
7833 colno++;
7834
7835 /* This must match colname-choosing logic in get_target_list() */
7836 if (context->resultDesc && colno <= context->resultDesc->natts)
7837 colname = NameStr(TupleDescAttr(context->resultDesc,
7838 colno - 1)->attname);
7839 else
7840 colname = tle->resname;
7841
7842 if (colname && strcmp(colname, attname) == 0 &&
7843 !equal(var, tle->expr))
7844 {
7845 need_prefix = true;
7846 break;
7847 }
7848 }
7849 }
7850
7851 if (refname && need_prefix)
7852 {
7855 }
7856 if (attname)
7858 else
7859 {
7861 if (istoplevel)
7862 appendStringInfo(buf, "::%s",
7863 format_type_with_typemod(var->vartype,
7864 var->vartypmod));
7865 }
7866
7867 return attname;
7868}
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
@ VAR_RETURNING_OLD
Definition: primnodes.h:257
@ VAR_RETURNING_NEW
Definition: primnodes.h:258
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:256
static void get_special_variable(Node *node, deparse_context *context, void *callback_arg)
Definition: ruleutils.c:7878
Index parent_relid
Definition: pathnodes.h:3007
int num_child_cols
Definition: pathnodes.h:3043
VarReturningType varreturningtype
Definition: primnodes.h:297
char * ret_old_alias
Definition: ruleutils.c:170
char * ret_new_alias
Definition: ruleutils.c:171

References deparse_context::appendparents, deparse_namespace::appendrels, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), attname, attnum, bms_is_member(), deparse_context::buf, buf, deparse_columns::colnames, deparse_columns_fetch, elog, equal(), ERROR, TargetEntry::expr, foreach_node, format_type_with_typemod(), get_rte_attribute_name(), get_rule_expr(), get_special_variable(), get_tle_by_resno(), get_variable(), deparse_context::inGroupBy, deparse_namespace::inner_plan, deparse_namespace::inner_tlist, InvalidAttrNumber, IsA, list_length(), list_nth(), deparse_context::namespaces, NameStr, TupleDescData::natts, NIL, AppendRelInfo::num_child_cols, deparse_columns::num_cols, AppendRelInfo::parent_relid, deparse_namespace::plan, pop_child_plan(), push_child_plan(), quote_identifier(), resolve_special_varno(), deparse_context::resultDesc, deparse_namespace::ret_new_alias, deparse_namespace::ret_old_alias, rt_fetch, deparse_namespace::rtable, deparse_namespace::rtable_names, RTE_CTE, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, deparse_context::targetList, TupleDescAttr(), VAR_RETURNING_DEFAULT, VAR_RETURNING_NEW, VAR_RETURNING_OLD, Var::varattno, deparse_context::varInOrderBy, Var::varlevelsup, Var::varno, deparse_context::varprefix, and Var::varreturningtype.

Referenced by get_rule_expr(), get_rule_expr_toplevel(), get_rule_sortgroupclause(), get_target_list(), and get_variable().

◆ get_window_frame_options()

static void get_window_frame_options ( int  frameOptions,
Node startOffset,
Node endOffset,
deparse_context context 
)
static

Definition at line 6828 of file ruleutils.c.

6831{
6832 StringInfo buf = context->buf;
6833
6834 if (frameOptions & FRAMEOPTION_NONDEFAULT)
6835 {
6836 if (frameOptions & FRAMEOPTION_RANGE)
6837 appendStringInfoString(buf, "RANGE ");
6838 else if (frameOptions & FRAMEOPTION_ROWS)
6839 appendStringInfoString(buf, "ROWS ");
6840 else if (frameOptions & FRAMEOPTION_GROUPS)
6841 appendStringInfoString(buf, "GROUPS ");
6842 else
6843 Assert(false);
6844 if (frameOptions & FRAMEOPTION_BETWEEN)
6845 appendStringInfoString(buf, "BETWEEN ");
6846 if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
6847 appendStringInfoString(buf, "UNBOUNDED PRECEDING ");
6848 else if (frameOptions & FRAMEOPTION_START_CURRENT_ROW)
6849 appendStringInfoString(buf, "CURRENT ROW ");
6850 else if (frameOptions & FRAMEOPTION_START_OFFSET)
6851 {
6852 get_rule_expr(startOffset, context, false);
6853 if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
6854 appendStringInfoString(buf, " PRECEDING ");
6855 else if (frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
6856 appendStringInfoString(buf, " FOLLOWING ");
6857 else
6858 Assert(false);
6859 }
6860 else
6861 Assert(false);
6862 if (frameOptions & FRAMEOPTION_BETWEEN)
6863 {
6864 appendStringInfoString(buf, "AND ");
6865 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
6866 appendStringInfoString(buf, "UNBOUNDED FOLLOWING ");
6867 else if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
6868 appendStringInfoString(buf, "CURRENT ROW ");
6869 else if (frameOptions & FRAMEOPTION_END_OFFSET)
6870 {
6871 get_rule_expr(endOffset, context, false);
6872 if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
6873 appendStringInfoString(buf, " PRECEDING ");
6874 else if (frameOptions & FRAMEOPTION_END_OFFSET_FOLLOWING)
6875 appendStringInfoString(buf, " FOLLOWING ");
6876 else
6877 Assert(false);
6878 }
6879 else
6880 Assert(false);
6881 }
6882 if (frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
6883 appendStringInfoString(buf, "EXCLUDE CURRENT ROW ");
6884 else if (frameOptions & FRAMEOPTION_EXCLUDE_GROUP)
6885 appendStringInfoString(buf, "EXCLUDE GROUP ");
6886 else if (frameOptions & FRAMEOPTION_EXCLUDE_TIES)
6887 appendStringInfoString(buf, "EXCLUDE TIES ");
6888 /* we will now have a trailing space; remove it */
6889 buf->data[--(buf->len)] = '\0';
6890 }
6891}
#define FRAMEOPTION_END_CURRENT_ROW
Definition: parsenodes.h:602
#define FRAMEOPTION_END_OFFSET
Definition: parsenodes.h:613
#define FRAMEOPTION_EXCLUDE_CURRENT_ROW
Definition: parsenodes.h:607
#define FRAMEOPTION_END_OFFSET_PRECEDING
Definition: parsenodes.h:604
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:597
#define FRAMEOPTION_START_CURRENT_ROW
Definition: parsenodes.h:601
#define FRAMEOPTION_START_OFFSET
Definition: parsenodes.h:611
#define FRAMEOPTION_END_OFFSET_FOLLOWING
Definition: parsenodes.h:606
#define FRAMEOPTION_EXCLUDE_TIES
Definition: parsenodes.h:609
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:593
#define FRAMEOPTION_EXCLUDE_GROUP
Definition: parsenodes.h:608
#define FRAMEOPTION_GROUPS
Definition: parsenodes.h:595
#define FRAMEOPTION_BETWEEN
Definition: parsenodes.h:596
#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING
Definition: parsenodes.h:600
#define FRAMEOPTION_START_OFFSET_PRECEDING
Definition: parsenodes.h:603
#define FRAMEOPTION_START_OFFSET_FOLLOWING
Definition: parsenodes.h:605
#define FRAMEOPTION_ROWS
Definition: parsenodes.h:594

References appendStringInfoString(), Assert(), deparse_context::buf, buf, FRAMEOPTION_BETWEEN, FRAMEOPTION_END_CURRENT_ROW, FRAMEOPTION_END_OFFSET, FRAMEOPTION_END_OFFSET_FOLLOWING, FRAMEOPTION_END_OFFSET_PRECEDING, FRAMEOPTION_END_UNBOUNDED_FOLLOWING, FRAMEOPTION_EXCLUDE_CURRENT_ROW, FRAMEOPTION_EXCLUDE_GROUP, FRAMEOPTION_EXCLUDE_TIES, FRAMEOPTION_GROUPS, FRAMEOPTION_NONDEFAULT, FRAMEOPTION_RANGE, FRAMEOPTION_ROWS, FRAMEOPTION_START_CURRENT_ROW, FRAMEOPTION_START_OFFSET, FRAMEOPTION_START_OFFSET_FOLLOWING, FRAMEOPTION_START_OFFSET_PRECEDING, FRAMEOPTION_START_UNBOUNDED_PRECEDING, and get_rule_expr().

Referenced by get_rule_windowspec(), and get_window_frame_options_for_explain().

◆ get_window_frame_options_for_explain()

char * get_window_frame_options_for_explain ( int  frameOptions,
Node startOffset,
Node endOffset,
List dpcontext,
bool  forceprefix 
)

Definition at line 6897 of file ruleutils.c.

6900{
6902 deparse_context context;
6903
6905 context.buf = &buf;
6906 context.namespaces = dpcontext;
6907 context.resultDesc = NULL;
6908 context.targetList = NIL;
6909 context.windowClause = NIL;
6910 context.varprefix = forceprefix;
6911 context.prettyFlags = 0;
6913 context.indentLevel = 0;
6914 context.colNamesVisible = true;
6915 context.inGroupBy = false;
6916 context.varInOrderBy = false;
6917 context.appendparents = NULL;
6918
6919 get_window_frame_options(frameOptions, startOffset, endOffset, &context);
6920
6921 return buf.data;
6922}

References deparse_context::appendparents, deparse_context::buf, buf, deparse_context::colNamesVisible, get_window_frame_options(), deparse_context::indentLevel, deparse_context::inGroupBy, initStringInfo(), deparse_context::namespaces, NIL, deparse_context::prettyFlags, deparse_context::resultDesc, deparse_context::targetList, deparse_context::varInOrderBy, deparse_context::varprefix, deparse_context::windowClause, WRAP_COLUMN_DEFAULT, and deparse_context::wrapColumn.

Referenced by show_window_def().

◆ get_windowfunc_expr()

static void get_windowfunc_expr ( WindowFunc wfunc,
deparse_context context 
)
static

Definition at line 11015 of file ruleutils.c.

11016{
11017 get_windowfunc_expr_helper(wfunc, context, NULL, NULL, false);
11018}

References get_windowfunc_expr_helper().

Referenced by get_rule_expr().

◆ get_windowfunc_expr_helper()

static void get_windowfunc_expr_helper ( WindowFunc wfunc,
deparse_context context,
const char *  funcname,
const char *  options,
bool  is_json_objectagg 
)
static

Definition at line 11026 of file ruleutils.c.

11029{
11030 StringInfo buf = context->buf;
11031 Oid argtypes[FUNC_MAX_ARGS];
11032 int nargs;
11033 List *argnames;
11034 ListCell *l;
11035
11036 if (list_length(wfunc->args) > FUNC_MAX_ARGS)
11037 ereport(ERROR,
11038 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
11039 errmsg("too many arguments")));
11040 nargs = 0;
11041 argnames = NIL;
11042 foreach(l, wfunc->args)
11043 {
11044 Node *arg = (Node *) lfirst(l);
11045
11046 if (IsA(arg, NamedArgExpr))
11047 argnames = lappend(argnames, ((NamedArgExpr *) arg)->name);
11048 argtypes[nargs] = exprType(arg);
11049 nargs++;
11050 }
11051
11052 if (!funcname)
11053 funcname = generate_function_name(wfunc->winfnoid, nargs, argnames,
11054 argtypes, false, NULL,
11055 context->inGroupBy);
11056
11057 appendStringInfo(buf, "%s(", funcname);
11058
11059 /* winstar can be set only in zero-argument aggregates */
11060 if (wfunc->winstar)
11062 else
11063 {
11064 if (is_json_objectagg)
11065 {
11066 get_rule_expr((Node *) linitial(wfunc->args), context, false);
11068 get_rule_expr((Node *) lsecond(wfunc->args), context, false);
11069 }
11070 else
11071 get_rule_expr((Node *) wfunc->args, context, true);
11072 }
11073
11074 if (options)
11076
11077 if (wfunc->aggfilter != NULL)
11078 {
11079 appendStringInfoString(buf, ") FILTER (WHERE ");
11080 get_rule_expr((Node *) wfunc->aggfilter, context, false);
11081 }
11082
11083 appendStringInfoString(buf, ") OVER ");
11084
11085 if (context->windowClause)
11086 {
11087 /* Query-decompilation case: search the windowClause list */
11088 foreach(l, context->windowClause)
11089 {
11090 WindowClause *wc = (WindowClause *) lfirst(l);
11091
11092 if (wc->winref == wfunc->winref)
11093 {
11094 if (wc->name)
11096 else
11097 get_rule_windowspec(wc, context->targetList, context);
11098 break;
11099 }
11100 }
11101 if (l == NULL)
11102 elog(ERROR, "could not find window clause for winref %u",
11103 wfunc->winref);
11104 }
11105 else
11106 {
11107 /*
11108 * In EXPLAIN, search the namespace stack for a matching WindowAgg
11109 * node (probably it's always the first entry), and print winname.
11110 */
11111 foreach(l, context->namespaces)
11112 {
11114
11115 if (dpns->plan && IsA(dpns->plan, WindowAgg))
11116 {
11117 WindowAgg *wagg = (WindowAgg *) dpns->plan;
11118
11119 if (wagg->winref == wfunc->winref)
11120 {
11122 break;
11123 }
11124 }
11125 }
11126 if (l == NULL)
11127 elog(ERROR, "could not find window clause for winref %u",
11128 wfunc->winref);
11129 }
11130}
char * winname
Definition: plannodes.h:1185
Index winref
Definition: plannodes.h:1188
List * args
Definition: primnodes.h:592
Index winref
Definition: primnodes.h:598
Expr * aggfilter
Definition: primnodes.h:594
Oid winfnoid
Definition: primnodes.h:584

References WindowFunc::aggfilter, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), arg, WindowFunc::args, deparse_context::buf, buf, elog, ereport, errcode(), errmsg(), ERROR, exprType(), FUNC_MAX_ARGS, funcname, generate_function_name(), get_rule_expr(), get_rule_windowspec(), if(), deparse_context::inGroupBy, IsA, lappend(), lfirst, linitial, list_length(), lsecond, deparse_context::namespaces, NIL, deparse_namespace::plan, quote_identifier(), deparse_context::targetList, deparse_context::windowClause, WindowFunc::winfnoid, WindowAgg::winname, WindowClause::winref, WindowAgg::winref, and WindowFunc::winref.

Referenced by get_json_agg_constructor(), and get_windowfunc_expr().

◆ get_with_clause()

static void get_with_clause ( Query query,
deparse_context context 
)
static

Definition at line 5767 of file ruleutils.c.

5768{
5769 StringInfo buf = context->buf;
5770 const char *sep;
5771 ListCell *l;
5772
5773 if (query->cteList == NIL)
5774 return;
5775
5776 if (PRETTY_INDENT(context))
5777 {
5778 context->indentLevel += PRETTYINDENT_STD;
5780 }
5781
5782 if (query->hasRecursive)
5783 sep = "WITH RECURSIVE ";
5784 else
5785 sep = "WITH ";
5786 foreach(l, query->cteList)
5787 {
5789
5792 if (cte->aliascolnames)
5793 {
5794 bool first = true;
5795 ListCell *col;
5796
5798 foreach(col, cte->aliascolnames)
5799 {
5800 if (first)
5801 first = false;
5802 else
5806 }
5808 }
5809 appendStringInfoString(buf, " AS ");
5810 switch (cte->ctematerialized)
5811 {
5813 break;
5815 appendStringInfoString(buf, "MATERIALIZED ");
5816 break;
5818 appendStringInfoString(buf, "NOT MATERIALIZED ");
5819 break;
5820 }
5822 if (PRETTY_INDENT(context))
5823 appendContextKeyword(context, "", 0, 0, 0);
5824 get_query_def((Query *) cte->ctequery, buf, context->namespaces, NULL,
5825 true,
5826 context->prettyFlags, context->wrapColumn,
5827 context->indentLevel);
5828 if (PRETTY_INDENT(context))
5829 appendContextKeyword(context, "", 0, 0, 0);
5831
5832 if (cte->search_clause)
5833 {
5834 bool first = true;
5835 ListCell *lc;
5836
5837 appendStringInfo(buf, " SEARCH %s FIRST BY ",
5838 cte->search_clause->search_breadth_first ? "BREADTH" : "DEPTH");
5839
5840 foreach(lc, cte->search_clause->search_col_list)
5841 {
5842 if (first)
5843 first = false;
5844 else
5848 }
5849
5850 appendStringInfo(buf, " SET %s", quote_identifier(cte->search_clause->search_seq_column));
5851 }
5852
5853 if (cte->cycle_clause)
5854 {
5855 bool first = true;
5856 ListCell *lc;
5857
5858 appendStringInfoString(buf, " CYCLE ");
5859
5860 foreach(lc, cte->cycle_clause->cycle_col_list)
5861 {
5862 if (first)
5863 first = false;
5864 else
5868 }
5869
5870 appendStringInfo(buf, " SET %s", quote_identifier(cte->cycle_clause->cycle_mark_column));
5871
5872 {
5873 Const *cmv = castNode(Const, cte->cycle_clause->cycle_mark_value);
5874 Const *cmd = castNode(Const, cte->cycle_clause->cycle_mark_default);
5875
5876 if (!(cmv->consttype == BOOLOID && !cmv->constisnull && DatumGetBool(cmv->constvalue) == true &&
5877 cmd->consttype == BOOLOID && !cmd->constisnull && DatumGetBool(cmd->constvalue) == false))
5878 {
5879 appendStringInfoString(buf, " TO ");
5880 get_rule_expr(cte->cycle_clause->cycle_mark_value, context, false);
5881 appendStringInfoString(buf, " DEFAULT ");
5882 get_rule_expr(cte->cycle_clause->cycle_mark_default, context, false);
5883 }
5884 }
5885
5886 appendStringInfo(buf, " USING %s", quote_identifier(cte->cycle_clause->cycle_path_column));
5887 }
5888
5889 sep = ", ";
5890 }
5891
5892 if (PRETTY_INDENT(context))
5893 {
5894 context->indentLevel -= PRETTYINDENT_STD;
5895 appendContextKeyword(context, "", 0, 0, 0);
5896 }
5897 else
5899}
@ CTEMaterializeNever
Definition: parsenodes.h:1649
@ CTEMaterializeAlways
Definition: parsenodes.h:1648
@ CTEMaterializeDefault
Definition: parsenodes.h:1647
CTEMaterialize ctematerialized
Definition: parsenodes.h:1688

References appendContextKeyword(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), deparse_context::buf, buf, castNode, Const::consttype, Query::cteList, CTEMaterializeAlways, CommonTableExpr::ctematerialized, CTEMaterializeDefault, CTEMaterializeNever, CommonTableExpr::ctename, CommonTableExpr::ctequery, DatumGetBool(), get_query_def(), get_rule_expr(), deparse_context::indentLevel, lfirst, deparse_context::namespaces, NIL, PRETTY_INDENT, deparse_context::prettyFlags, PRETTYINDENT_STD, quote_identifier(), strVal, and deparse_context::wrapColumn.

Referenced by get_delete_query_def(), get_insert_query_def(), get_merge_query_def(), get_select_query_def(), and get_update_query_def().

◆ get_xmltable()

static void get_xmltable ( TableFunc tf,
deparse_context context,
bool  showimplicit 
)
static

Definition at line 11935 of file ruleutils.c.

11936{
11937 StringInfo buf = context->buf;
11938
11939 appendStringInfoString(buf, "XMLTABLE(");
11940
11941 if (tf->ns_uris != NIL)
11942 {
11943 ListCell *lc1,
11944 *lc2;
11945 bool first = true;
11946
11947 appendStringInfoString(buf, "XMLNAMESPACES (");
11948 forboth(lc1, tf->ns_uris, lc2, tf->ns_names)
11949 {
11950 Node *expr = (Node *) lfirst(lc1);
11951 String *ns_node = lfirst_node(String, lc2);
11952
11953 if (!first)
11955 else
11956 first = false;
11957
11958 if (ns_node != NULL)
11959 {
11960 get_rule_expr(expr, context, showimplicit);
11961 appendStringInfo(buf, " AS %s",
11962 quote_identifier(strVal(ns_node)));
11963 }
11964 else
11965 {
11966 appendStringInfoString(buf, "DEFAULT ");
11967 get_rule_expr(expr, context, showimplicit);
11968 }
11969 }
11971 }
11972
11974 get_rule_expr((Node *) tf->rowexpr, context, showimplicit);
11975 appendStringInfoString(buf, ") PASSING (");
11976 get_rule_expr((Node *) tf->docexpr, context, showimplicit);
11978
11979 if (tf->colexprs != NIL)
11980 {
11981 ListCell *l1;
11982 ListCell *l2;
11983 ListCell *l3;
11984 ListCell *l4;
11985 ListCell *l5;
11986 int colnum = 0;
11987
11988 appendStringInfoString(buf, " COLUMNS ");
11989 forfive(l1, tf->colnames, l2, tf->coltypes, l3, tf->coltypmods,
11990 l4, tf->colexprs, l5, tf->coldefexprs)
11991 {
11992 char *colname = strVal(lfirst(l1));
11993 Oid typid = lfirst_oid(l2);
11994 int32 typmod = lfirst_int(l3);
11995 Node *colexpr = (Node *) lfirst(l4);
11996 Node *coldefexpr = (Node *) lfirst(l5);
11997 bool ordinality = (tf->ordinalitycol == colnum);
11998 bool notnull = bms_is_member(colnum, tf->notnulls);
11999
12000 if (colnum > 0)
12002 colnum++;
12003
12004 appendStringInfo(buf, "%s %s", quote_identifier(colname),
12005 ordinality ? "FOR ORDINALITY" :
12006 format_type_with_typemod(typid, typmod));
12007 if (ordinality)
12008 continue;
12009
12010 if (coldefexpr != NULL)
12011 {
12012 appendStringInfoString(buf, " DEFAULT (");
12013 get_rule_expr((Node *) coldefexpr, context, showimplicit);
12015 }
12016 if (colexpr != NULL)
12017 {
12018 appendStringInfoString(buf, " PATH (");
12019 get_rule_expr((Node *) colexpr, context, showimplicit);
12021 }
12022 if (notnull)
12023 appendStringInfoString(buf, " NOT NULL");
12024 }
12025 }
12026
12028}
#define forfive(cell1, list1, cell2, list2, cell3, list3, cell4, list4, cell5, list5)
Definition: pg_list.h:588
Node * rowexpr
Definition: primnodes.h:122
List * colexprs
Definition: primnodes.h:132

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), bms_is_member(), deparse_context::buf, buf, TableFunc::colexprs, TableFunc::docexpr, forboth, forfive, format_type_with_typemod(), get_rule_expr(), lfirst, lfirst_int, lfirst_node, lfirst_oid, NIL, quote_identifier(), TableFunc::rowexpr, and strVal.

Referenced by get_tablefunc().

◆ has_dangerous_join_using()

static bool has_dangerous_join_using ( deparse_namespace dpns,
Node jtnode 
)
static

Definition at line 4140 of file ruleutils.c.

4141{
4142 if (IsA(jtnode, RangeTblRef))
4143 {
4144 /* nothing to do here */
4145 }
4146 else if (IsA(jtnode, FromExpr))
4147 {
4148 FromExpr *f = (FromExpr *) jtnode;
4149 ListCell *lc;
4150
4151 foreach(lc, f->fromlist)
4152 {
4153 if (has_dangerous_join_using(dpns, (Node *) lfirst(lc)))
4154 return true;
4155 }
4156 }
4157 else if (IsA(jtnode, JoinExpr))
4158 {
4159 JoinExpr *j = (JoinExpr *) jtnode;
4160
4161 /* Is it an unnamed JOIN with USING? */
4162 if (j->alias == NULL && j->usingClause)
4163 {
4164 /*
4165 * Yes, so check each join alias var to see if any of them are not
4166 * simple references to underlying columns. If so, we have a
4167 * dangerous situation and must pick unique aliases.
4168 */
4169 RangeTblEntry *jrte = rt_fetch(j->rtindex, dpns->rtable);
4170
4171 /* We need only examine the merged columns */
4172 for (int i = 0; i < jrte->joinmergedcols; i++)
4173 {
4174 Node *aliasvar = list_nth(jrte->joinaliasvars, i);
4175
4176 if (!IsA(aliasvar, Var))
4177 return true;
4178 }
4179 }
4180
4181 /* Nope, but inspect children */
4182 if (has_dangerous_join_using(dpns, j->larg))
4183 return true;
4184 if (has_dangerous_join_using(dpns, j->rarg))
4185 return true;
4186 }
4187 else
4188 elog(ERROR, "unrecognized node type: %d",
4189 (int) nodeTag(jtnode));
4190 return false;
4191}
static bool has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode)
Definition: ruleutils.c:4140

References elog, ERROR, FromExpr::fromlist, has_dangerous_join_using(), i, IsA, j, lfirst, list_nth(), nodeTag, rt_fetch, and deparse_namespace::rtable.

Referenced by has_dangerous_join_using(), and set_deparse_for_query().

◆ identify_join_columns()

static void identify_join_columns ( JoinExpr j,
RangeTblEntry jrte,
deparse_columns colinfo 
)
static

Definition at line 5065 of file ruleutils.c.

5067{
5068 int numjoincols;
5069 int jcolno;
5070 int rcolno;
5071 ListCell *lc;
5072
5073 /* Extract left/right child RT indexes */
5074 if (IsA(j->larg, RangeTblRef))
5075 colinfo->leftrti = ((RangeTblRef *) j->larg)->rtindex;
5076 else if (IsA(j->larg, JoinExpr))
5077 colinfo->leftrti = ((JoinExpr *) j->larg)->rtindex;
5078 else
5079 elog(ERROR, "unrecognized node type in jointree: %d",
5080 (int) nodeTag(j->larg));
5081 if (IsA(j->rarg, RangeTblRef))
5082 colinfo->rightrti = ((RangeTblRef *) j->rarg)->rtindex;
5083 else if (IsA(j->rarg, JoinExpr))
5084 colinfo->rightrti = ((JoinExpr *) j->rarg)->rtindex;
5085 else
5086 elog(ERROR, "unrecognized node type in jointree: %d",
5087 (int) nodeTag(j->rarg));
5088
5089 /* Assert children will be processed earlier than join in second pass */
5090 Assert(colinfo->leftrti < j->rtindex);
5091 Assert(colinfo->rightrti < j->rtindex);
5092
5093 /* Initialize result arrays with zeroes */
5094 numjoincols = list_length(jrte->joinaliasvars);
5095 Assert(numjoincols == list_length(jrte->eref->colnames));
5096 colinfo->leftattnos = (int *) palloc0(numjoincols * sizeof(int));
5097 colinfo->rightattnos = (int *) palloc0(numjoincols * sizeof(int));
5098
5099 /*
5100 * Deconstruct RTE's joinleftcols/joinrightcols into desired format.
5101 * Recall that the column(s) merged due to USING are the first column(s)
5102 * of the join output. We need not do anything special while scanning
5103 * joinleftcols, but while scanning joinrightcols we must distinguish
5104 * merged from unmerged columns.
5105 */
5106 jcolno = 0;
5107 foreach(lc, jrte->joinleftcols)
5108 {
5109 int leftattno = lfirst_int(lc);
5110
5111 colinfo->leftattnos[jcolno++] = leftattno;
5112 }
5113 rcolno = 0;
5114 foreach(lc, jrte->joinrightcols)
5115 {
5116 int rightattno = lfirst_int(lc);
5117
5118 if (rcolno < jrte->joinmergedcols) /* merged column? */
5119 colinfo->rightattnos[rcolno] = rightattno;
5120 else
5121 colinfo->rightattnos[jcolno++] = rightattno;
5122 rcolno++;
5123 }
5124 Assert(jcolno == numjoincols);
5125}
int * rightattnos
Definition: ruleutils.c:298
int * leftattnos
Definition: ruleutils.c:297

References Assert(), elog, ERROR, IsA, j, deparse_columns::leftattnos, deparse_columns::leftrti, lfirst_int, list_length(), nodeTag, palloc0(), deparse_columns::rightattnos, and deparse_columns::rightrti.

Referenced by set_using_names().

◆ is_input_argument()

static bool is_input_argument ( int  nth,
const char *  argmodes 
)
static

Definition at line 3446 of file ruleutils.c.

3447{
3448 return (!argmodes
3449 || argmodes[nth] == PROARGMODE_IN
3450 || argmodes[nth] == PROARGMODE_INOUT
3451 || argmodes[nth] == PROARGMODE_VARIADIC);
3452}

Referenced by pg_get_function_arg_default().

◆ isSimpleNode()

static bool isSimpleNode ( Node node,
Node parentNode,
int  prettyFlags 
)
static

Definition at line 8840 of file ruleutils.c.

8841{
8842 if (!node)
8843 return false;
8844
8845 switch (nodeTag(node))
8846 {
8847 case T_Var:
8848 case T_Const:
8849 case T_Param:
8850 case T_CoerceToDomainValue:
8851 case T_SetToDefault:
8852 case T_CurrentOfExpr:
8853 /* single words: always simple */
8854 return true;
8855
8856 case T_SubscriptingRef:
8857 case T_ArrayExpr:
8858 case T_RowExpr:
8859 case T_CoalesceExpr:
8860 case T_MinMaxExpr:
8861 case T_SQLValueFunction:
8862 case T_XmlExpr:
8863 case T_NextValueExpr:
8864 case T_NullIfExpr:
8865 case T_Aggref:
8866 case T_GroupingFunc:
8867 case T_WindowFunc:
8868 case T_MergeSupportFunc:
8869 case T_FuncExpr:
8870 case T_JsonConstructorExpr:
8871 case T_JsonExpr:
8872 /* function-like: name(..) or name[..] */
8873 return true;
8874
8875 /* CASE keywords act as parentheses */
8876 case T_CaseExpr:
8877 return true;
8878
8879 case T_FieldSelect:
8880
8881 /*
8882 * appears simple since . has top precedence, unless parent is
8883 * T_FieldSelect itself!
8884 */
8885 return !IsA(parentNode, FieldSelect);
8886
8887 case T_FieldStore:
8888
8889 /*
8890 * treat like FieldSelect (probably doesn't matter)
8891 */
8892 return !IsA(parentNode, FieldStore);
8893
8894 case T_CoerceToDomain:
8895 /* maybe simple, check args */
8896 return isSimpleNode((Node *) ((CoerceToDomain *) node)->arg,
8897 node, prettyFlags);
8898 case T_RelabelType:
8899 return isSimpleNode((Node *) ((RelabelType *) node)->arg,
8900 node, prettyFlags);
8901 case T_CoerceViaIO:
8902 return isSimpleNode((Node *) ((CoerceViaIO *) node)->arg,
8903 node, prettyFlags);
8904 case T_ArrayCoerceExpr:
8905 return isSimpleNode((Node *) ((ArrayCoerceExpr *) node)->arg,
8906 node, prettyFlags);
8907 case T_ConvertRowtypeExpr:
8908 return isSimpleNode((Node *) ((ConvertRowtypeExpr *) node)->arg,
8909 node, prettyFlags);
8910 case T_ReturningExpr:
8911 return isSimpleNode((Node *) ((ReturningExpr *) node)->retexpr,
8912 node, prettyFlags);
8913
8914 case T_OpExpr:
8915 {
8916 /* depends on parent node type; needs further checking */
8917 if (prettyFlags & PRETTYFLAG_PAREN && IsA(parentNode, OpExpr))
8918 {
8919 const char *op;
8920 const char *parentOp;
8921 bool is_lopriop;
8922 bool is_hipriop;
8923 bool is_lopriparent;
8924 bool is_hipriparent;
8925
8926 op = get_simple_binary_op_name((OpExpr *) node);
8927 if (!op)
8928 return false;
8929
8930 /* We know only the basic operators + - and * / % */
8931 is_lopriop = (strchr("+-", *op) != NULL);
8932 is_hipriop = (strchr("*/%", *op) != NULL);
8933 if (!(is_lopriop || is_hipriop))
8934 return false;
8935
8936 parentOp = get_simple_binary_op_name((OpExpr *) parentNode);
8937 if (!parentOp)
8938 return false;
8939
8940 is_lopriparent = (strchr("+-", *parentOp) != NULL);
8941 is_hipriparent = (strchr("*/%", *parentOp) != NULL);
8942 if (!(is_lopriparent || is_hipriparent))
8943 return false;
8944
8945 if (is_hipriop && is_lopriparent)
8946 return true; /* op binds tighter than parent */
8947
8948 if (is_lopriop && is_hipriparent)
8949 return false;
8950
8951 /*
8952 * Operators are same priority --- can skip parens only if
8953 * we have (a - b) - c, not a - (b - c).
8954 */
8955 if (node == (Node *) linitial(((OpExpr *) parentNode)->args))
8956 return true;
8957
8958 return false;
8959 }
8960 /* else do the same stuff as for T_SubLink et al. */
8961 }
8962 /* FALLTHROUGH */
8963
8964 case T_SubLink:
8965 case T_NullTest:
8966 case T_BooleanTest:
8967 case T_DistinctExpr:
8968 case T_JsonIsPredicate:
8969 switch (nodeTag(parentNode))
8970 {
8971 case T_FuncExpr:
8972 {
8973 /* special handling for casts and COERCE_SQL_SYNTAX */
8974 CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
8975
8976 if (type == COERCE_EXPLICIT_CAST ||
8979 return false;
8980 return true; /* own parentheses */
8981 }
8982 case T_BoolExpr: /* lower precedence */
8983 case T_SubscriptingRef: /* other separators */
8984 case T_ArrayExpr: /* other separators */
8985 case T_RowExpr: /* other separators */
8986 case T_CoalesceExpr: /* own parentheses */
8987 case T_MinMaxExpr: /* own parentheses */
8988 case T_XmlExpr: /* own parentheses */
8989 case T_NullIfExpr: /* other separators */
8990 case T_Aggref: /* own parentheses */
8991 case T_GroupingFunc: /* own parentheses */
8992 case T_WindowFunc: /* own parentheses */
8993 case T_CaseExpr: /* other separators */
8994 return true;
8995 default:
8996 return false;
8997 }
8998
8999 case T_BoolExpr:
9000 switch (nodeTag(parentNode))
9001 {
9002 case T_BoolExpr:
9003 if (prettyFlags & PRETTYFLAG_PAREN)
9004 {
9006 BoolExprType parentType;
9007
9008 type = ((BoolExpr *) node)->boolop;
9009 parentType = ((BoolExpr *) parentNode)->boolop;
9010 switch (type)
9011 {
9012 case NOT_EXPR:
9013 case AND_EXPR:
9014 if (parentType == AND_EXPR || parentType == OR_EXPR)
9015 return true;
9016 break;
9017 case OR_EXPR:
9018 if (parentType == OR_EXPR)
9019 return true;
9020 break;
9021 }
9022 }
9023 return false;
9024 case T_FuncExpr:
9025 {
9026 /* special handling for casts and COERCE_SQL_SYNTAX */
9027 CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
9028
9029 if (type == COERCE_EXPLICIT_CAST ||
9032 return false;
9033 return true; /* own parentheses */
9034 }
9035 case T_SubscriptingRef: /* other separators */
9036 case T_ArrayExpr: /* other separators */
9037 case T_RowExpr: /* other separators */
9038 case T_CoalesceExpr: /* own parentheses */
9039 case T_MinMaxExpr: /* own parentheses */
9040 case T_XmlExpr: /* own parentheses */
9041 case T_NullIfExpr: /* other separators */
9042 case T_Aggref: /* own parentheses */
9043 case T_GroupingFunc: /* own parentheses */
9044 case T_WindowFunc: /* own parentheses */
9045 case T_CaseExpr: /* other separators */
9046 case T_JsonExpr: /* own parentheses */
9047 return true;
9048 default:
9049 return false;
9050 }
9051
9052 case T_JsonValueExpr:
9053 /* maybe simple, check args */
9054 return isSimpleNode((Node *) ((JsonValueExpr *) node)->raw_expr,
9055 node, prettyFlags);
9056
9057 default:
9058 break;
9059 }
9060 /* those we don't know: in dubio complexo */
9061 return false;
9062}
BoolExprType
Definition: primnodes.h:947
CoercionForm
Definition: primnodes.h:750
static const char * get_simple_binary_op_name(OpExpr *expr)
Definition: ruleutils.c:8814
const char * type

References AND_EXPR, arg, generate_unaccent_rules::args, COERCE_EXPLICIT_CAST, COERCE_IMPLICIT_CAST, COERCE_SQL_SYNTAX, get_simple_binary_op_name(), IsA, isSimpleNode(), linitial, nodeTag, NOT_EXPR, OR_EXPR, PRETTYFLAG_PAREN, and type.

Referenced by get_rule_expr_paren(), and isSimpleNode().

◆ looks_like_function()

static bool looks_like_function ( Node node)
static

Definition at line 10696 of file ruleutils.c.

10697{
10698 if (node == NULL)
10699 return false; /* probably shouldn't happen */
10700 switch (nodeTag(node))
10701 {
10702 case T_FuncExpr:
10703 /* OK, unless it's going to deparse as a cast */
10704 return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL ||
10705 ((FuncExpr *) node)->funcformat == COERCE_SQL_SYNTAX);
10706 case T_NullIfExpr:
10707 case T_CoalesceExpr:
10708 case T_MinMaxExpr:
10709 case T_SQLValueFunction:
10710 case T_XmlExpr:
10711 case T_JsonExpr:
10712 /* these are all accepted by func_expr_common_subexpr */
10713 return true;
10714 default:
10715 break;
10716 }
10717 return false;
10718}

References COERCE_EXPLICIT_CALL, COERCE_SQL_SYNTAX, and nodeTag.

Referenced by get_rule_expr_funccall(), pg_get_indexdef_worker(), pg_get_partkeydef_worker(), and pg_get_statisticsobj_worker().

◆ make_colname_unique()

static char * make_colname_unique ( char *  colname,
deparse_namespace dpns,
deparse_columns colinfo 
)
static

Definition at line 4923 of file ruleutils.c.

4925{
4926 /*
4927 * If the selected name isn't unique, append digits to make it so. For a
4928 * very long input name, we might have to truncate to stay within
4929 * NAMEDATALEN.
4930 */
4931 if (!colname_is_unique(colname, dpns, colinfo))
4932 {
4933 int colnamelen = strlen(colname);
4934 char *modname = (char *) palloc(colnamelen + 16);
4935 int i = 0;
4936
4937 do
4938 {
4939 i++;
4940 for (;;)
4941 {
4942 memcpy(modname, colname, colnamelen);
4943 sprintf(modname + colnamelen, "_%d", i);
4944 if (strlen(modname) < NAMEDATALEN)
4945 break;
4946 /* drop chars from colname to keep all the digits */
4947 colnamelen = pg_mbcliplen(colname, colnamelen,
4948 colnamelen - 1);
4949 }
4950 } while (!colname_is_unique(modname, dpns, colinfo));
4951 colname = modname;
4952 }
4953 return colname;
4954}
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1083
#define sprintf
Definition: port.h:241
static bool colname_is_unique(const char *colname, deparse_namespace *dpns, deparse_columns *colinfo)
Definition: ruleutils.c:4848

References colname_is_unique(), i, NAMEDATALEN, palloc(), pg_mbcliplen(), and sprintf.

Referenced by set_join_column_names(), set_relation_column_names(), and set_using_names().

◆ make_ruledef()

static void make_ruledef ( StringInfo  buf,
HeapTuple  ruletup,
TupleDesc  rulettc,
int  prettyFlags 
)
static

Definition at line 5347 of file ruleutils.c.

5349{
5350 char *rulename;
5351 char ev_type;
5352 Oid ev_class;
5353 bool is_instead;
5354 char *ev_qual;
5355 char *ev_action;
5356 List *actions;
5357 Relation ev_relation;
5358 TupleDesc viewResultDesc = NULL;
5359 int fno;
5360 Datum dat;
5361 bool isnull;
5362
5363 /*
5364 * Get the attribute values from the rules tuple
5365 */
5366 fno = SPI_fnumber(rulettc, "rulename");
5367 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5368 Assert(!isnull);
5369 rulename = NameStr(*(DatumGetName(dat)));
5370
5371 fno = SPI_fnumber(rulettc, "ev_type");
5372 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5373 Assert(!isnull);
5374 ev_type = DatumGetChar(dat);
5375
5376 fno = SPI_fnumber(rulettc, "ev_class");
5377 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5378 Assert(!isnull);
5379 ev_class = DatumGetObjectId(dat);
5380
5381 fno = SPI_fnumber(rulettc, "is_instead");
5382 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5383 Assert(!isnull);
5384 is_instead = DatumGetBool(dat);
5385
5386 fno = SPI_fnumber(rulettc, "ev_qual");
5387 ev_qual = SPI_getvalue(ruletup, rulettc, fno);
5388 Assert(ev_qual != NULL);
5389
5390 fno = SPI_fnumber(rulettc, "ev_action");
5391 ev_action = SPI_getvalue(ruletup, rulettc, fno);
5392 Assert(ev_action != NULL);
5393 actions = (List *) stringToNode(ev_action);
5394 if (actions == NIL)
5395 elog(ERROR, "invalid empty ev_action list");
5396
5397 ev_relation = table_open(ev_class, AccessShareLock);
5398
5399 /*
5400 * Build the rules definition text
5401 */
5402 appendStringInfo(buf, "CREATE RULE %s AS",
5403 quote_identifier(rulename));
5404
5405 if (prettyFlags & PRETTYFLAG_INDENT)
5406 appendStringInfoString(buf, "\n ON ");
5407 else
5408 appendStringInfoString(buf, " ON ");
5409
5410 /* The event the rule is fired for */
5411 switch (ev_type)
5412 {
5413 case '1':
5414 appendStringInfoString(buf, "SELECT");
5415 viewResultDesc = RelationGetDescr(ev_relation);
5416 break;
5417
5418 case '2':
5419 appendStringInfoString(buf, "UPDATE");
5420 break;
5421
5422 case '3':
5423 appendStringInfoString(buf, "INSERT");
5424 break;
5425
5426 case '4':
5427 appendStringInfoString(buf, "DELETE");
5428 break;
5429
5430 default:
5431 ereport(ERROR,
5432 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5433 errmsg("rule \"%s\" has unsupported event type %d",
5434 rulename, ev_type)));
5435 break;
5436 }
5437
5438 /* The relation the rule is fired on */
5439 appendStringInfo(buf, " TO %s",
5440 (prettyFlags & PRETTYFLAG_SCHEMA) ?
5441 generate_relation_name(ev_class, NIL) :
5443
5444 /* If the rule has an event qualification, add it */
5445 if (strcmp(ev_qual, "<>") != 0)
5446 {
5447 Node *qual;
5448 Query *query;
5449 deparse_context context;
5450 deparse_namespace dpns;
5451
5452 if (prettyFlags & PRETTYFLAG_INDENT)
5454 appendStringInfoString(buf, " WHERE ");
5455
5456 qual = stringToNode(ev_qual);
5457
5458 /*
5459 * We need to make a context for recognizing any Vars in the qual
5460 * (which can only be references to OLD and NEW). Use the rtable of
5461 * the first query in the action list for this purpose.
5462 */
5463 query = (Query *) linitial(actions);
5464
5465 /*
5466 * If the action is INSERT...SELECT, OLD/NEW have been pushed down
5467 * into the SELECT, and that's what we need to look at. (Ugly kluge
5468 * ... try to fix this when we redesign querytrees.)
5469 */
5470 query = getInsertSelectQuery(query, NULL);
5471
5472 /* Must acquire locks right away; see notes in get_query_def() */
5473 AcquireRewriteLocks(query, false, false);
5474
5475 context.buf = buf;
5476 context.namespaces = list_make1(&dpns);
5477 context.resultDesc = NULL;
5478 context.targetList = NIL;
5479 context.windowClause = NIL;
5480 context.varprefix = (list_length(query->rtable) != 1);
5481 context.prettyFlags = prettyFlags;
5483 context.indentLevel = PRETTYINDENT_STD;
5484 context.colNamesVisible = true;
5485 context.inGroupBy = false;
5486 context.varInOrderBy = false;
5487 context.appendparents = NULL;
5488
5489 set_deparse_for_query(&dpns, query, NIL);
5490
5491 get_rule_expr(qual, &context, false);
5492 }
5493
5494 appendStringInfoString(buf, " DO ");
5495
5496 /* The INSTEAD keyword (if so) */
5497 if (is_instead)
5498 appendStringInfoString(buf, "INSTEAD ");
5499
5500 /* Finally the rules actions */
5501 if (list_length(actions) > 1)
5502 {
5504 Query *query;
5505
5507 foreach(action, actions)
5508 {
5509 query = (Query *) lfirst(action);
5510 get_query_def(query, buf, NIL, viewResultDesc, true,
5511 prettyFlags, WRAP_COLUMN_DEFAULT, 0);
5512 if (prettyFlags)
5514 else
5516 }
5518 }
5519 else
5520 {
5521 Query *query;
5522
5523 query = (Query *) linitial(actions);
5524 get_query_def(query, buf, NIL, viewResultDesc, true,
5525 prettyFlags, WRAP_COLUMN_DEFAULT, 0);
5527 }
5528
5529 table_close(ev_relation, AccessShareLock);
5530}
static Name DatumGetName(Datum X)
Definition: postgres.h:365
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
static char DatumGetChar(Datum X)
Definition: postgres.h:117
void * stringToNode(const char *str)
Definition: read.c:90
#define RelationGetDescr(relation)
Definition: rel.h:539
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
static char * generate_qualified_relation_name(Oid relid)
Definition: ruleutils.c:13203
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1176
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1221
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1253
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessShareLock, AcquireRewriteLocks(), generate_unaccent_rules::action, deparse_context::appendparents, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), deparse_context::buf, buf, deparse_context::colNamesVisible, DatumGetBool(), DatumGetChar(), DatumGetName(), DatumGetObjectId(), elog, ereport, errcode(), errmsg(), ERROR, generate_qualified_relation_name(), generate_relation_name(), get_query_def(), get_rule_expr(), getInsertSelectQuery(), deparse_context::indentLevel, deparse_context::inGroupBy, lfirst, linitial, list_length(), list_make1, deparse_context::namespaces, NameStr, NIL, PRETTYFLAG_INDENT, PRETTYFLAG_SCHEMA, deparse_context::prettyFlags, PRETTYINDENT_STD, quote_identifier(), RelationGetDescr, deparse_context::resultDesc, Query::rtable, set_deparse_for_query(), SPI_fnumber(), SPI_getbinval(), SPI_getvalue(), stringToNode(), table_close(), table_open(), deparse_context::targetList, deparse_context::varInOrderBy, deparse_context::varprefix, deparse_context::windowClause, WRAP_COLUMN_DEFAULT, and deparse_context::wrapColumn.

Referenced by pg_get_ruledef_worker().

◆ make_viewdef()

static void make_viewdef ( StringInfo  buf,
HeapTuple  ruletup,
TupleDesc  rulettc,
int  prettyFlags,
int  wrapColumn 
)
static

Definition at line 5539 of file ruleutils.c.

5541{
5542 Query *query;
5543 char ev_type;
5544 Oid ev_class;
5545 bool is_instead;
5546 char *ev_qual;
5547 char *ev_action;
5548 List *actions;
5549 Relation ev_relation;
5550 int fno;
5551 Datum dat;
5552 bool isnull;
5553
5554 /*
5555 * Get the attribute values from the rules tuple
5556 */
5557 fno = SPI_fnumber(rulettc, "ev_type");
5558 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5559 Assert(!isnull);
5560 ev_type = DatumGetChar(dat);
5561
5562 fno = SPI_fnumber(rulettc, "ev_class");
5563 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5564 Assert(!isnull);
5565 ev_class = DatumGetObjectId(dat);
5566
5567 fno = SPI_fnumber(rulettc, "is_instead");
5568 dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
5569 Assert(!isnull);
5570 is_instead = DatumGetBool(dat);
5571
5572 fno = SPI_fnumber(rulettc, "ev_qual");
5573 ev_qual = SPI_getvalue(ruletup, rulettc, fno);
5574 Assert(ev_qual != NULL);
5575
5576 fno = SPI_fnumber(rulettc, "ev_action");
5577 ev_action = SPI_getvalue(ruletup, rulettc, fno);
5578 Assert(ev_action != NULL);
5579 actions = (List *) stringToNode(ev_action);
5580
5581 if (list_length(actions) != 1)
5582 {
5583 /* keep output buffer empty and leave */
5584 return;
5585 }
5586
5587 query = (Query *) linitial(actions);
5588
5589 if (ev_type != '1' || !is_instead ||
5590 strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
5591 {
5592 /* keep output buffer empty and leave */
5593 return;
5594 }
5595
5596 ev_relation = table_open(ev_class, AccessShareLock);
5597
5598 get_query_def(query, buf, NIL, RelationGetDescr(ev_relation), true,
5599 prettyFlags, wrapColumn, 0);
5601
5602 table_close(ev_relation, AccessShareLock);
5603}

References AccessShareLock, appendStringInfoChar(), Assert(), buf, CMD_SELECT, Query::commandType, DatumGetBool(), DatumGetChar(), DatumGetObjectId(), get_query_def(), linitial, list_length(), NIL, RelationGetDescr, SPI_fnumber(), SPI_getbinval(), SPI_getvalue(), stringToNode(), table_close(), and table_open().

Referenced by pg_get_viewdef_worker().

◆ pg_get_constraintdef()

Datum pg_get_constraintdef ( PG_FUNCTION_ARGS  )

Definition at line 2146 of file ruleutils.c.

2147{
2148 Oid constraintId = PG_GETARG_OID(0);
2149 int prettyFlags;
2150 char *res;
2151
2152 prettyFlags = PRETTYFLAG_INDENT;
2153
2154 res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2155
2156 if (res == NULL)
2158
2160}
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static text * string_to_text(char *str)
Definition: ruleutils.c:13565
static char * pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags, bool missing_ok)
Definition: ruleutils.c:2193

References pg_get_constraintdef_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, and string_to_text().

◆ pg_get_constraintdef_command()

char * pg_get_constraintdef_command ( Oid  constraintId)

Definition at line 2184 of file ruleutils.c.

2185{
2186 return pg_get_constraintdef_worker(constraintId, true, 0, false);
2187}

References pg_get_constraintdef_worker().

Referenced by RememberConstraintForRebuilding().

◆ pg_get_constraintdef_ext()

Datum pg_get_constraintdef_ext ( PG_FUNCTION_ARGS  )

Definition at line 2163 of file ruleutils.c.

2164{
2165 Oid constraintId = PG_GETARG_OID(0);
2166 bool pretty = PG_GETARG_BOOL(1);
2167 int prettyFlags;
2168 char *res;
2169
2170 prettyFlags = GET_PRETTY_FLAGS(pretty);
2171
2172 res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
2173
2174 if (res == NULL)
2176
2178}
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define GET_PRETTY_FLAGS(pretty)
Definition: ruleutils.c:93

References GET_PRETTY_FLAGS, pg_get_constraintdef_worker(), PG_GETARG_BOOL, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_constraintdef_worker()

static char * pg_get_constraintdef_worker ( Oid  constraintId,
bool  fullCommand,
int  prettyFlags,
bool  missing_ok 
)
static

Definition at line 2193 of file ruleutils.c.

2195{
2196 HeapTuple tup;
2197 Form_pg_constraint conForm;
2199 SysScanDesc scandesc;
2200 ScanKeyData scankey[1];
2202 Relation relation = table_open(ConstraintRelationId, AccessShareLock);
2203
2204 ScanKeyInit(&scankey[0],
2205 Anum_pg_constraint_oid,
2206 BTEqualStrategyNumber, F_OIDEQ,
2207 ObjectIdGetDatum(constraintId));
2208
2209 scandesc = systable_beginscan(relation,
2210 ConstraintOidIndexId,
2211 true,
2212 snapshot,
2213 1,
2214 scankey);
2215
2216 /*
2217 * We later use the tuple with SysCacheGetAttr() as if we had obtained it
2218 * via SearchSysCache, which works fine.
2219 */
2220 tup = systable_getnext(scandesc);
2221
2222 UnregisterSnapshot(snapshot);
2223
2224 if (!HeapTupleIsValid(tup))
2225 {
2226 if (missing_ok)
2227 {
2228 systable_endscan(scandesc);
2229 table_close(relation, AccessShareLock);
2230 return NULL;
2231 }
2232 elog(ERROR, "could not find tuple for constraint %u", constraintId);
2233 }
2234
2235 conForm = (Form_pg_constraint) GETSTRUCT(tup);
2236
2238
2239 if (fullCommand)
2240 {
2241 if (OidIsValid(conForm->conrelid))
2242 {
2243 /*
2244 * Currently, callers want ALTER TABLE (without ONLY) for CHECK
2245 * constraints, and other types of constraints don't inherit
2246 * anyway so it doesn't matter whether we say ONLY or not. Someday
2247 * we might need to let callers specify whether to put ONLY in the
2248 * command.
2249 */
2250 appendStringInfo(&buf, "ALTER TABLE %s ADD CONSTRAINT %s ",
2251 generate_qualified_relation_name(conForm->conrelid),
2252 quote_identifier(NameStr(conForm->conname)));
2253 }
2254 else
2255 {
2256 /* Must be a domain constraint */
2257 Assert(OidIsValid(conForm->contypid));
2258 appendStringInfo(&buf, "ALTER DOMAIN %s ADD CONSTRAINT %s ",
2259 generate_qualified_type_name(conForm->contypid),
2260 quote_identifier(NameStr(conForm->conname)));
2261 }
2262 }
2263
2264 switch (conForm->contype)
2265 {
2266 case CONSTRAINT_FOREIGN:
2267 {
2268 Datum val;
2269 bool isnull;
2270 const char *string;
2271
2272 /* Start off the constraint definition */
2273 appendStringInfoString(&buf, "FOREIGN KEY (");
2274
2275 /* Fetch and build referencing-column list */
2276 val = SysCacheGetAttrNotNull(CONSTROID, tup,
2277 Anum_pg_constraint_conkey);
2278
2279 /* If it is a temporal foreign key then it uses PERIOD. */
2280 decompile_column_index_array(val, conForm->conrelid, conForm->conperiod, &buf);
2281
2282 /* add foreign relation name */
2283 appendStringInfo(&buf, ") REFERENCES %s(",
2284 generate_relation_name(conForm->confrelid,
2285 NIL));
2286
2287 /* Fetch and build referenced-column list */
2288 val = SysCacheGetAttrNotNull(CONSTROID, tup,
2289 Anum_pg_constraint_confkey);
2290
2291 decompile_column_index_array(val, conForm->confrelid, conForm->conperiod, &buf);
2292
2294
2295 /* Add match type */
2296 switch (conForm->confmatchtype)
2297 {
2299 string = " MATCH FULL";
2300 break;
2302 string = " MATCH PARTIAL";
2303 break;
2305 string = "";
2306 break;
2307 default:
2308 elog(ERROR, "unrecognized confmatchtype: %d",
2309 conForm->confmatchtype);
2310 string = ""; /* keep compiler quiet */
2311 break;
2312 }
2313 appendStringInfoString(&buf, string);
2314
2315 /* Add ON UPDATE and ON DELETE clauses, if needed */
2316 switch (conForm->confupdtype)
2317 {
2319 string = NULL; /* suppress default */
2320 break;
2322 string = "RESTRICT";
2323 break;
2325 string = "CASCADE";
2326 break;
2328 string = "SET NULL";
2329 break;
2331 string = "SET DEFAULT";
2332 break;
2333 default:
2334 elog(ERROR, "unrecognized confupdtype: %d",
2335 conForm->confupdtype);
2336 string = NULL; /* keep compiler quiet */
2337 break;
2338 }
2339 if (string)
2340 appendStringInfo(&buf, " ON UPDATE %s", string);
2341
2342 switch (conForm->confdeltype)
2343 {
2345 string = NULL; /* suppress default */
2346 break;
2348 string = "RESTRICT";
2349 break;
2351 string = "CASCADE";
2352 break;
2354 string = "SET NULL";
2355 break;
2357 string = "SET DEFAULT";
2358 break;
2359 default:
2360 elog(ERROR, "unrecognized confdeltype: %d",
2361 conForm->confdeltype);
2362 string = NULL; /* keep compiler quiet */
2363 break;
2364 }
2365 if (string)
2366 appendStringInfo(&buf, " ON DELETE %s", string);
2367
2368 /*
2369 * Add columns specified to SET NULL or SET DEFAULT if
2370 * provided.
2371 */
2372 val = SysCacheGetAttr(CONSTROID, tup,
2373 Anum_pg_constraint_confdelsetcols, &isnull);
2374 if (!isnull)
2375 {
2377 decompile_column_index_array(val, conForm->conrelid, false, &buf);
2379 }
2380
2381 break;
2382 }
2383 case CONSTRAINT_PRIMARY:
2384 case CONSTRAINT_UNIQUE:
2385 {
2386 Datum val;
2387 Oid indexId;
2388 int keyatts;
2389 HeapTuple indtup;
2390
2391 /* Start off the constraint definition */
2392 if (conForm->contype == CONSTRAINT_PRIMARY)
2393 appendStringInfoString(&buf, "PRIMARY KEY ");
2394 else
2395 appendStringInfoString(&buf, "UNIQUE ");
2396
2397 indexId = conForm->conindid;
2398
2399 indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
2400 if (!HeapTupleIsValid(indtup))
2401 elog(ERROR, "cache lookup failed for index %u", indexId);
2402 if (conForm->contype == CONSTRAINT_UNIQUE &&
2403 ((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct)
2404 appendStringInfoString(&buf, "NULLS NOT DISTINCT ");
2405
2407
2408 /* Fetch and build target column list */
2409 val = SysCacheGetAttrNotNull(CONSTROID, tup,
2410 Anum_pg_constraint_conkey);
2411
2412 keyatts = decompile_column_index_array(val, conForm->conrelid, false, &buf);
2413 if (conForm->conperiod)
2414 appendStringInfoString(&buf, " WITHOUT OVERLAPS");
2415
2417
2418 /* Build including column list (from pg_index.indkeys) */
2419 val = SysCacheGetAttrNotNull(INDEXRELID, indtup,
2420 Anum_pg_index_indnatts);
2421 if (DatumGetInt32(val) > keyatts)
2422 {
2423 Datum cols;
2424 Datum *keys;
2425 int nKeys;
2426 int j;
2427
2428 appendStringInfoString(&buf, " INCLUDE (");
2429
2430 cols = SysCacheGetAttrNotNull(INDEXRELID, indtup,
2431 Anum_pg_index_indkey);
2432
2434 &keys, NULL, &nKeys);
2435
2436 for (j = keyatts; j < nKeys; j++)
2437 {
2438 char *colName;
2439
2440 colName = get_attname(conForm->conrelid,
2441 DatumGetInt16(keys[j]), false);
2442 if (j > keyatts)
2445 }
2446
2448 }
2449 ReleaseSysCache(indtup);
2450
2451 /* XXX why do we only print these bits if fullCommand? */
2452 if (fullCommand && OidIsValid(indexId))
2453 {
2454 char *options = flatten_reloptions(indexId);
2455 Oid tblspc;
2456
2457 if (options)
2458 {
2459 appendStringInfo(&buf, " WITH (%s)", options);
2460 pfree(options);
2461 }
2462
2463 /*
2464 * Print the tablespace, unless it's the database default.
2465 * This is to help ALTER TABLE usage of this facility,
2466 * which needs this behavior to recreate exact catalog
2467 * state.
2468 */
2469 tblspc = get_rel_tablespace(indexId);
2470 if (OidIsValid(tblspc))
2471 appendStringInfo(&buf, " USING INDEX TABLESPACE %s",
2473 }
2474
2475 break;
2476 }
2477 case CONSTRAINT_CHECK:
2478 {
2479 Datum val;
2480 char *conbin;
2481 char *consrc;
2482 Node *expr;
2483 List *context;
2484
2485 /* Fetch constraint expression in parsetree form */
2486 val = SysCacheGetAttrNotNull(CONSTROID, tup,
2487 Anum_pg_constraint_conbin);
2488
2489 conbin = TextDatumGetCString(val);
2490 expr = stringToNode(conbin);
2491
2492 /* Set up deparsing context for Var nodes in constraint */
2493 if (conForm->conrelid != InvalidOid)
2494 {
2495 /* relation constraint */
2496 context = deparse_context_for(get_relation_name(conForm->conrelid),
2497 conForm->conrelid);
2498 }
2499 else
2500 {
2501 /* domain constraint --- can't have Vars */
2502 context = NIL;
2503 }
2504
2505 consrc = deparse_expression_pretty(expr, context, false, false,
2506 prettyFlags, 0);
2507
2508 /*
2509 * Now emit the constraint definition, adding NO INHERIT if
2510 * necessary.
2511 *
2512 * There are cases where the constraint expression will be
2513 * fully parenthesized and we don't need the outer parens ...
2514 * but there are other cases where we do need 'em. Be
2515 * conservative for now.
2516 *
2517 * Note that simply checking for leading '(' and trailing ')'
2518 * would NOT be good enough, consider "(x > 0) AND (y > 0)".
2519 */
2520 appendStringInfo(&buf, "CHECK (%s)%s",
2521 consrc,
2522 conForm->connoinherit ? " NO INHERIT" : "");
2523 break;
2524 }
2525 case CONSTRAINT_NOTNULL:
2526 {
2527 if (conForm->conrelid)
2528 {
2530
2532
2533 appendStringInfo(&buf, "NOT NULL %s",
2534 quote_identifier(get_attname(conForm->conrelid,
2535 attnum, false)));
2536 if (((Form_pg_constraint) GETSTRUCT(tup))->connoinherit)
2537 appendStringInfoString(&buf, " NO INHERIT");
2538 }
2539 else if (conForm->contypid)
2540 {
2541 /* conkey is null for domain not-null constraints */
2542 appendStringInfoString(&buf, "NOT NULL");
2543 }
2544 break;
2545 }
2546
2547 case CONSTRAINT_TRIGGER:
2548
2549 /*
2550 * There isn't an ALTER TABLE syntax for creating a user-defined
2551 * constraint trigger, but it seems better to print something than
2552 * throw an error; if we throw error then this function couldn't
2553 * safely be applied to all rows of pg_constraint.
2554 */
2555 appendStringInfoString(&buf, "TRIGGER");
2556 break;
2557 case CONSTRAINT_EXCLUSION:
2558 {
2559 Oid indexOid = conForm->conindid;
2560 Datum val;
2561 Datum *elems;
2562 int nElems;
2563 int i;
2564 Oid *operators;
2565
2566 /* Extract operator OIDs from the pg_constraint tuple */
2567 val = SysCacheGetAttrNotNull(CONSTROID, tup,
2568 Anum_pg_constraint_conexclop);
2569
2571 &elems, NULL, &nElems);
2572
2573 operators = (Oid *) palloc(nElems * sizeof(Oid));
2574 for (i = 0; i < nElems; i++)
2575 operators[i] = DatumGetObjectId(elems[i]);
2576
2577 /* pg_get_indexdef_worker does the rest */
2578 /* suppress tablespace because pg_dump wants it that way */
2580 pg_get_indexdef_worker(indexOid,
2581 0,
2582 operators,
2583 false,
2584 false,
2585 false,
2586 false,
2587 prettyFlags,
2588 false));
2589 break;
2590 }
2591 default:
2592 elog(ERROR, "invalid constraint type \"%c\"", conForm->contype);
2593 break;
2594 }
2595
2596 if (conForm->condeferrable)
2597 appendStringInfoString(&buf, " DEFERRABLE");
2598 if (conForm->condeferred)
2599 appendStringInfoString(&buf, " INITIALLY DEFERRED");
2600
2601 /* Validated status is irrelevant when the constraint is NOT ENFORCED. */
2602 if (!conForm->conenforced)
2603 appendStringInfoString(&buf, " NOT ENFORCED");
2604 else if (!conForm->convalidated)
2605 appendStringInfoString(&buf, " NOT VALID");
2606
2607 /* Cleanup */
2608 systable_endscan(scandesc);
2609 table_close(relation, AccessShareLock);
2610
2611 return buf.data;
2612}
char * get_tablespace_name(Oid spc_oid)
Definition: tablespace.c:1472
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:388
Oid get_rel_tablespace(Oid relid)
Definition: lsyscache.c:2137
#define FKCONSTR_ACTION_RESTRICT
Definition: parsenodes.h:2802
#define FKCONSTR_ACTION_SETDEFAULT
Definition: parsenodes.h:2805
#define FKCONSTR_MATCH_SIMPLE
Definition: parsenodes.h:2810
#define FKCONSTR_MATCH_PARTIAL
Definition: parsenodes.h:2809
#define FKCONSTR_ACTION_CASCADE
Definition: parsenodes.h:2803
#define FKCONSTR_ACTION_SETNULL
Definition: parsenodes.h:2804
#define FKCONSTR_MATCH_FULL
Definition: parsenodes.h:2808
#define FKCONSTR_ACTION_NOACTION
Definition: parsenodes.h:2801
AttrNumber extractNotNullColumn(HeapTuple constrTup)
FormData_pg_constraint * Form_pg_constraint
FormData_pg_index * Form_pg_index
Definition: pg_index.h:70
char string[11]
Definition: preproc-type.c:52
static int decompile_column_index_array(Datum column_index_array, Oid relId, bool withPeriod, StringInfo buf)
Definition: ruleutils.c:2621
List * deparse_context_for(const char *aliasname, Oid relid)
Definition: ruleutils.c:3708
static char * pg_get_indexdef_worker(Oid indexrelid, int colno, const Oid *excludeOps, bool attrsOnly, bool keysOnly, bool showTblSpc, bool inherits, int prettyFlags, bool missing_ok)
Definition: ruleutils.c:1270
static char * flatten_reloptions(Oid relid)
Definition: ruleutils.c:13633
static char * generate_qualified_type_name(Oid typid)
Definition: ruleutils.c:13500
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:271
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:853
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:811
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631

References AccessShareLock, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), attnum, BTEqualStrategyNumber, buf, DatumGetArrayTypeP, DatumGetInt16(), DatumGetInt32(), DatumGetObjectId(), decompile_column_index_array(), deconstruct_array_builtin(), deparse_context_for(), deparse_expression_pretty(), elog, ERROR, extractNotNullColumn(), FKCONSTR_ACTION_CASCADE, FKCONSTR_ACTION_NOACTION, FKCONSTR_ACTION_RESTRICT, FKCONSTR_ACTION_SETDEFAULT, FKCONSTR_ACTION_SETNULL, FKCONSTR_MATCH_FULL, FKCONSTR_MATCH_PARTIAL, FKCONSTR_MATCH_SIMPLE, flatten_reloptions(), generate_qualified_relation_name(), generate_qualified_type_name(), generate_relation_name(), get_attname(), get_rel_tablespace(), get_relation_name(), get_tablespace_name(), GETSTRUCT(), GetTransactionSnapshot(), HeapTupleIsValid, i, initStringInfo(), InvalidOid, j, NameStr, NIL, ObjectIdGetDatum(), OidIsValid, palloc(), pfree(), pg_get_indexdef_worker(), quote_identifier(), RegisterSnapshot(), ReleaseSysCache(), ScanKeyInit(), SearchSysCache1(), stringToNode(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), TextDatumGetCString, UnregisterSnapshot(), and val.

Referenced by pg_get_constraintdef(), pg_get_constraintdef_command(), and pg_get_constraintdef_ext().

◆ pg_get_expr()

Datum pg_get_expr ( PG_FUNCTION_ARGS  )

Definition at line 2675 of file ruleutils.c.

2676{
2677 text *expr = PG_GETARG_TEXT_PP(0);
2678 Oid relid = PG_GETARG_OID(1);
2679 text *result;
2680 int prettyFlags;
2681
2682 prettyFlags = PRETTYFLAG_INDENT;
2683
2684 result = pg_get_expr_worker(expr, relid, prettyFlags);
2685 if (result)
2686 PG_RETURN_TEXT_P(result);
2687 else
2689}
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static text * pg_get_expr_worker(text *expr, Oid relid, int prettyFlags)
Definition: ruleutils.c:2710
Definition: c.h:658

References pg_get_expr_worker(), PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and PRETTYFLAG_INDENT.

Referenced by decompile_conbin().

◆ pg_get_expr_ext()

Datum pg_get_expr_ext ( PG_FUNCTION_ARGS  )

Definition at line 2692 of file ruleutils.c.

2693{
2694 text *expr = PG_GETARG_TEXT_PP(0);
2695 Oid relid = PG_GETARG_OID(1);
2696 bool pretty = PG_GETARG_BOOL(2);
2697 text *result;
2698 int prettyFlags;
2699
2700 prettyFlags = GET_PRETTY_FLAGS(pretty);
2701
2702 result = pg_get_expr_worker(expr, relid, prettyFlags);
2703 if (result)
2704 PG_RETURN_TEXT_P(result);
2705 else
2707}

References GET_PRETTY_FLAGS, pg_get_expr_worker(), PG_GETARG_BOOL, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ pg_get_expr_worker()

static text * pg_get_expr_worker ( text expr,
Oid  relid,
int  prettyFlags 
)
static

Definition at line 2710 of file ruleutils.c.

2711{
2712 Node *node;
2713 Node *tst;
2714 Relids relids;
2715 List *context;
2716 char *exprstr;
2717 Relation rel = NULL;
2718 char *str;
2719
2720 /* Convert input pg_node_tree (really TEXT) object to C string */
2721 exprstr = text_to_cstring(expr);
2722
2723 /* Convert expression to node tree */
2724 node = (Node *) stringToNode(exprstr);
2725
2726 pfree(exprstr);
2727
2728 /*
2729 * Throw error if the input is a querytree rather than an expression tree.
2730 * While we could support queries here, there seems no very good reason
2731 * to. In most such catalog columns, we'll see a List of Query nodes, or
2732 * even nested Lists, so drill down to a non-List node before checking.
2733 */
2734 tst = node;
2735 while (tst && IsA(tst, List))
2736 tst = linitial((List *) tst);
2737 if (tst && IsA(tst, Query))
2738 ereport(ERROR,
2739 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2740 errmsg("input is a query, not an expression")));
2741
2742 /*
2743 * Throw error if the expression contains Vars we won't be able to
2744 * deparse.
2745 */
2746 relids = pull_varnos(NULL, node);
2747 if (OidIsValid(relid))
2748 {
2749 if (!bms_is_subset(relids, bms_make_singleton(1)))
2750 ereport(ERROR,
2751 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2752 errmsg("expression contains variables of more than one relation")));
2753 }
2754 else
2755 {
2756 if (!bms_is_empty(relids))
2757 ereport(ERROR,
2758 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2759 errmsg("expression contains variables")));
2760 }
2761
2762 /*
2763 * Prepare deparse context if needed. If we are deparsing with a relid,
2764 * we need to transiently open and lock the rel, to make sure it won't go
2765 * away underneath us. (set_relation_column_names would lock it anyway,
2766 * so this isn't really introducing any new behavior.)
2767 */
2768 if (OidIsValid(relid))
2769 {
2770 rel = try_relation_open(relid, AccessShareLock);
2771 if (rel == NULL)
2772 return NULL;
2773 context = deparse_context_for(RelationGetRelationName(rel), relid);
2774 }
2775 else
2776 context = NIL;
2777
2778 /* Deparse */
2779 str = deparse_expression_pretty(node, context, false, false,
2780 prettyFlags, 0);
2781
2782 if (rel != NULL)
2784
2785 return string_to_text(str);
2786}
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
#define bms_is_empty(a)
Definition: bitmapset.h:118
#define RelationGetRelationName(relation)
Definition: rel.h:547
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:114
char * text_to_cstring(const text *t)
Definition: varlena.c:225

References AccessShareLock, bms_is_empty, bms_is_subset(), bms_make_singleton(), deparse_context_for(), deparse_expression_pretty(), ereport, errcode(), errmsg(), ERROR, IsA, linitial, NIL, OidIsValid, pfree(), pull_varnos(), relation_close(), RelationGetRelationName, str, string_to_text(), stringToNode(), text_to_cstring(), and try_relation_open().

Referenced by pg_get_expr(), and pg_get_expr_ext().

◆ pg_get_function_arg_default()

Datum pg_get_function_arg_default ( PG_FUNCTION_ARGS  )

Definition at line 3486 of file ruleutils.c.

3487{
3488 Oid funcid = PG_GETARG_OID(0);
3489 int32 nth_arg = PG_GETARG_INT32(1);
3490 HeapTuple proctup;
3491 Form_pg_proc proc;
3492 int numargs;
3493 Oid *argtypes;
3494 char **argnames;
3495 char *argmodes;
3496 int i;
3497 List *argdefaults;
3498 Node *node;
3499 char *str;
3500 int nth_inputarg;
3501 Datum proargdefaults;
3502 bool isnull;
3503 int nth_default;
3504
3505 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3506 if (!HeapTupleIsValid(proctup))
3508
3509 numargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
3510 if (nth_arg < 1 || nth_arg > numargs || !is_input_argument(nth_arg - 1, argmodes))
3511 {
3512 ReleaseSysCache(proctup);
3514 }
3515
3516 nth_inputarg = 0;
3517 for (i = 0; i < nth_arg; i++)
3518 if (is_input_argument(i, argmodes))
3519 nth_inputarg++;
3520
3521 proargdefaults = SysCacheGetAttr(PROCOID, proctup,
3522 Anum_pg_proc_proargdefaults,
3523 &isnull);
3524 if (isnull)
3525 {
3526 ReleaseSysCache(proctup);
3528 }
3529
3530 str = TextDatumGetCString(proargdefaults);
3531 argdefaults = castNode(List, stringToNode(str));
3532 pfree(str);
3533
3534 proc = (Form_pg_proc) GETSTRUCT(proctup);
3535
3536 /*
3537 * Calculate index into proargdefaults: proargdefaults corresponds to the
3538 * last N input arguments, where N = pronargdefaults.
3539 */
3540 nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3541
3542 if (nth_default < 0 || nth_default >= list_length(argdefaults))
3543 {
3544 ReleaseSysCache(proctup);
3546 }
3547 node = list_nth(argdefaults, nth_default);
3548 str = deparse_expression(node, NIL, false, false);
3549
3550 ReleaseSysCache(proctup);
3551
3553}
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:1379
static bool is_input_argument(int nth, const char *argmodes)
Definition: ruleutils.c:3446
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition: ruleutils.c:3645

References castNode, deparse_expression(), get_func_arg_info(), GETSTRUCT(), HeapTupleIsValid, i, is_input_argument(), list_length(), list_nth(), NIL, ObjectIdGetDatum(), pfree(), PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, ReleaseSysCache(), SearchSysCache1(), str, string_to_text(), stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

◆ pg_get_function_arguments()

Datum pg_get_function_arguments ( PG_FUNCTION_ARGS  )

Definition at line 3179 of file ruleutils.c.

3180{
3181 Oid funcid = PG_GETARG_OID(0);
3183 HeapTuple proctup;
3184
3185 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3186 if (!HeapTupleIsValid(proctup))
3188
3190
3191 (void) print_function_arguments(&buf, proctup, false, true);
3192
3193 ReleaseSysCache(proctup);
3194
3196}
static int print_function_arguments(StringInfo buf, HeapTuple proctup, bool print_table_args, bool print_defaults)
Definition: ruleutils.c:3298

References buf, HeapTupleIsValid, initStringInfo(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, print_function_arguments(), ReleaseSysCache(), SearchSysCache1(), and string_to_text().

◆ pg_get_function_identity_arguments()

Datum pg_get_function_identity_arguments ( PG_FUNCTION_ARGS  )

Definition at line 3205 of file ruleutils.c.

3206{
3207 Oid funcid = PG_GETARG_OID(0);
3209 HeapTuple proctup;
3210
3211 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3212 if (!HeapTupleIsValid(proctup))
3214
3216
3217 (void) print_function_arguments(&buf, proctup, false, false);
3218
3219 ReleaseSysCache(proctup);
3220
3222}

References buf, HeapTupleIsValid, initStringInfo(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, print_function_arguments(), ReleaseSysCache(), SearchSysCache1(), and string_to_text().

◆ pg_get_function_result()

Datum pg_get_function_result ( PG_FUNCTION_ARGS  )

Definition at line 3230 of file ruleutils.c.

3231{
3232 Oid funcid = PG_GETARG_OID(0);
3234 HeapTuple proctup;
3235
3236 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3237 if (!HeapTupleIsValid(proctup))
3239
3240 if (((Form_pg_proc) GETSTRUCT(proctup))->prokind == PROKIND_PROCEDURE)
3241 {
3242 ReleaseSysCache(proctup);
3244 }
3245
3247
3248 print_function_rettype(&buf, proctup);
3249
3250 ReleaseSysCache(proctup);
3251
3253}
static void print_function_rettype(StringInfo buf, HeapTuple proctup)
Definition: ruleutils.c:3260

References buf, GETSTRUCT(), HeapTupleIsValid, initStringInfo(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, print_function_rettype(), ReleaseSysCache(), SearchSysCache1(), and string_to_text().

◆ pg_get_function_sqlbody()

Datum pg_get_function_sqlbody ( PG_FUNCTION_ARGS  )

Definition at line 3610 of file ruleutils.c.

3611{
3612 Oid funcid = PG_GETARG_OID(0);
3614 HeapTuple proctup;
3615 bool isnull;
3616
3618
3619 /* Look up the function */
3620 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3621 if (!HeapTupleIsValid(proctup))
3623
3624 (void) SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3625 if (isnull)
3626 {
3627 ReleaseSysCache(proctup);
3629 }
3630
3631 print_function_sqlbody(&buf, proctup);
3632
3633 ReleaseSysCache(proctup);
3634
3636}
static void print_function_sqlbody(StringInfo buf, HeapTuple proctup)
Definition: ruleutils.c:3556
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:204

References buf, cstring_to_text_with_len(), HeapTupleIsValid, initStringInfo(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, print_function_sqlbody(), ReleaseSysCache(), SearchSysCache1(), and SysCacheGetAttr().

◆ pg_get_functiondef()

Datum pg_get_functiondef ( PG_FUNCTION_ARGS  )

Definition at line 2927 of file ruleutils.c.

2928{
2929 Oid funcid = PG_GETARG_OID(0);
2931 StringInfoData dq;
2932 HeapTuple proctup;
2933 Form_pg_proc proc;
2934 bool isfunction;
2935 Datum tmp;
2936 bool isnull;
2937 const char *prosrc;
2938 const char *name;
2939 const char *nsp;
2940 float4 procost;
2941 int oldlen;
2942
2944
2945 /* Look up the function */
2946 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2947 if (!HeapTupleIsValid(proctup))
2949
2950 proc = (Form_pg_proc) GETSTRUCT(proctup);
2951 name = NameStr(proc->proname);
2952
2953 if (proc->prokind == PROKIND_AGGREGATE)
2954 ereport(ERROR,
2955 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2956 errmsg("\"%s\" is an aggregate function", name)));
2957
2958 isfunction = (proc->prokind != PROKIND_PROCEDURE);
2959
2960 /*
2961 * We always qualify the function name, to ensure the right function gets
2962 * replaced.
2963 */
2964 nsp = get_namespace_name_or_temp(proc->pronamespace);
2965 appendStringInfo(&buf, "CREATE OR REPLACE %s %s(",
2966 isfunction ? "FUNCTION" : "PROCEDURE",
2968 (void) print_function_arguments(&buf, proctup, false, true);
2969 appendStringInfoString(&buf, ")\n");
2970 if (isfunction)
2971 {
2972 appendStringInfoString(&buf, " RETURNS ");
2973 print_function_rettype(&buf, proctup);
2974 appendStringInfoChar(&buf, '\n');
2975 }
2976
2977 print_function_trftypes(&buf, proctup);
2978
2979 appendStringInfo(&buf, " LANGUAGE %s\n",
2980 quote_identifier(get_language_name(proc->prolang, false)));
2981
2982 /* Emit some miscellaneous options on one line */
2983 oldlen = buf.len;
2984
2985 if (proc->prokind == PROKIND_WINDOW)
2986 appendStringInfoString(&buf, " WINDOW");
2987 switch (proc->provolatile)
2988 {
2989 case PROVOLATILE_IMMUTABLE:
2990 appendStringInfoString(&buf, " IMMUTABLE");
2991 break;
2992 case PROVOLATILE_STABLE:
2993 appendStringInfoString(&buf, " STABLE");
2994 break;
2995 case PROVOLATILE_VOLATILE:
2996 break;
2997 }
2998
2999 switch (proc->proparallel)
3000 {
3001 case PROPARALLEL_SAFE:
3002 appendStringInfoString(&buf, " PARALLEL SAFE");
3003 break;
3004 case PROPARALLEL_RESTRICTED:
3005 appendStringInfoString(&buf, " PARALLEL RESTRICTED");
3006 break;
3007 case PROPARALLEL_UNSAFE:
3008 break;
3009 }
3010
3011 if (proc->proisstrict)
3012 appendStringInfoString(&buf, " STRICT");
3013 if (proc->prosecdef)
3014 appendStringInfoString(&buf, " SECURITY DEFINER");
3015 if (proc->proleakproof)
3016 appendStringInfoString(&buf, " LEAKPROOF");
3017
3018 /* This code for the default cost and rows should match functioncmds.c */
3019 if (proc->prolang == INTERNALlanguageId ||
3020 proc->prolang == ClanguageId)
3021 procost = 1;
3022 else
3023 procost = 100;
3024 if (proc->procost != procost)
3025 appendStringInfo(&buf, " COST %g", proc->procost);
3026
3027 if (proc->prorows > 0 && proc->prorows != 1000)
3028 appendStringInfo(&buf, " ROWS %g", proc->prorows);
3029
3030 if (proc->prosupport)
3031 {
3032 Oid argtypes[1];
3033
3034 /*
3035 * We should qualify the support function's name if it wouldn't be
3036 * resolved by lookup in the current search path.
3037 */
3038 argtypes[0] = INTERNALOID;
3039 appendStringInfo(&buf, " SUPPORT %s",
3040 generate_function_name(proc->prosupport, 1,
3041 NIL, argtypes,
3042 false, NULL, false));
3043 }
3044
3045 if (oldlen != buf.len)
3046 appendStringInfoChar(&buf, '\n');
3047
3048 /* Emit any proconfig options, one per line */
3049 tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
3050 if (!isnull)
3051 {
3053 int i;
3054
3055 Assert(ARR_ELEMTYPE(a) == TEXTOID);
3056 Assert(ARR_NDIM(a) == 1);
3057 Assert(ARR_LBOUND(a)[0] == 1);
3058
3059 for (i = 1; i <= ARR_DIMS(a)[0]; i++)
3060 {
3061 Datum d;
3062
3063 d = array_ref(a, 1, &i,
3064 -1 /* varlenarray */ ,
3065 -1 /* TEXT's typlen */ ,
3066 false /* TEXT's typbyval */ ,
3067 TYPALIGN_INT /* TEXT's typalign */ ,
3068 &isnull);
3069 if (!isnull)
3070 {
3071 char *configitem = TextDatumGetCString(d);
3072 char *pos;
3073
3074 pos = strchr(configitem, '=');
3075 if (pos == NULL)
3076 continue;
3077 *pos++ = '\0';
3078
3079 appendStringInfo(&buf, " SET %s TO ",
3080 quote_identifier(configitem));
3081
3082 /*
3083 * Variables that are marked GUC_LIST_QUOTE were already fully
3084 * quoted by flatten_set_variable_args() before they were put
3085 * into the proconfig array. However, because the quoting
3086 * rules used there aren't exactly like SQL's, we have to
3087 * break the list value apart and then quote the elements as
3088 * string literals. (The elements may be double-quoted as-is,
3089 * but we can't just feed them to the SQL parser; it would do
3090 * the wrong thing with elements that are zero-length or
3091 * longer than NAMEDATALEN.)
3092 *
3093 * Variables that are not so marked should just be emitted as
3094 * simple string literals. If the variable is not known to
3095 * guc.c, we'll do that; this makes it unsafe to use
3096 * GUC_LIST_QUOTE for extension variables.
3097 */
3098 if (GetConfigOptionFlags(configitem, true) & GUC_LIST_QUOTE)
3099 {
3100 List *namelist;
3101 ListCell *lc;
3102
3103 /* Parse string into list of identifiers */
3104 if (!SplitGUCList(pos, ',', &namelist))
3105 {
3106 /* this shouldn't fail really */
3107 elog(ERROR, "invalid list syntax in proconfig item");
3108 }
3109 foreach(lc, namelist)
3110 {
3111 char *curname = (char *) lfirst(lc);
3112
3113 simple_quote_literal(&buf, curname);
3114 if (lnext(namelist, lc))
3116 }
3117 }
3118 else
3120 appendStringInfoChar(&buf, '\n');
3121 }
3122 }
3123 }
3124
3125 /* And finally the function definition ... */
3126 (void) SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3127 if (proc->prolang == SQLlanguageId && !isnull)
3128 {
3129 print_function_sqlbody(&buf, proctup);
3130 }
3131 else
3132 {
3133 appendStringInfoString(&buf, "AS ");
3134
3135 tmp = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
3136 if (!isnull)
3137 {
3139 appendStringInfoString(&buf, ", "); /* assume prosrc isn't null */
3140 }
3141
3142 tmp = SysCacheGetAttrNotNull(PROCOID, proctup, Anum_pg_proc_prosrc);
3143 prosrc = TextDatumGetCString(tmp);
3144
3145 /*
3146 * We always use dollar quoting. Figure out a suitable delimiter.
3147 *
3148 * Since the user is likely to be editing the function body string, we
3149 * shouldn't use a short delimiter that he might easily create a
3150 * conflict with. Hence prefer "$function$"/"$procedure$", but extend
3151 * if needed.
3152 */
3153 initStringInfo(&dq);
3154 appendStringInfoChar(&dq, '$');
3155 appendStringInfoString(&dq, (isfunction ? "function" : "procedure"));
3156 while (strstr(prosrc, dq.data) != NULL)
3157 appendStringInfoChar(&dq, 'x');
3158 appendStringInfoChar(&dq, '$');
3159
3161 appendStringInfoString(&buf, prosrc);
3163 }
3164
3165 appendStringInfoChar(&buf, '\n');
3166
3167 ReleaseSysCache(proctup);
3168
3170}
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_DIMS(a)
Definition: array.h:294
#define ARR_LBOUND(a)
Definition: array.h:296
Datum array_ref(ArrayType *array, int nSubscripts, int *indx, int arraytyplen, int elmlen, bool elmbyval, char elmalign, bool *isNull)
Definition: arrayfuncs.c:3146
float float4
Definition: c.h:600
int GetConfigOptionFlags(const char *name, bool missing_ok)
Definition: guc.c:4452
#define GUC_LIST_QUOTE
Definition: guc.h:215
int a
Definition: isn.c:70
char * get_language_name(Oid langoid, bool missing_ok)
Definition: lsyscache.c:1196
static void print_function_trftypes(StringInfo buf, HeapTuple proctup)
Definition: ruleutils.c:3458
bool SplitGUCList(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3773

References a, appendBinaryStringInfo(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), ARR_DIMS, ARR_ELEMTYPE, ARR_LBOUND, ARR_NDIM, array_ref(), Assert(), buf, StringInfoData::data, DatumGetArrayTypeP, elog, ereport, errcode(), errmsg(), ERROR, generate_function_name(), get_language_name(), get_namespace_name_or_temp(), GetConfigOptionFlags(), GETSTRUCT(), GUC_LIST_QUOTE, HeapTupleIsValid, i, initStringInfo(), StringInfoData::len, lfirst, lnext(), name, NameStr, NIL, ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, print_function_arguments(), print_function_rettype(), print_function_sqlbody(), print_function_trftypes(), quote_identifier(), quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), simple_quote_literal(), SplitGUCList(), string_to_text(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

◆ pg_get_indexdef()

Datum pg_get_indexdef ( PG_FUNCTION_ARGS  )

Definition at line 1178 of file ruleutils.c.

1179{
1180 Oid indexrelid = PG_GETARG_OID(0);
1181 int prettyFlags;
1182 char *res;
1183
1184 prettyFlags = PRETTYFLAG_INDENT;
1185
1186 res = pg_get_indexdef_worker(indexrelid, 0, NULL,
1187 false, false,
1188 false, false,
1189 prettyFlags, true);
1190
1191 if (res == NULL)
1193
1195}

References pg_get_indexdef_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, and string_to_text().

◆ pg_get_indexdef_columns()

char * pg_get_indexdef_columns ( Oid  indexrelid,
bool  pretty 
)

Definition at line 1235 of file ruleutils.c.

1236{
1237 int prettyFlags;
1238
1239 prettyFlags = GET_PRETTY_FLAGS(pretty);
1240
1241 return pg_get_indexdef_worker(indexrelid, 0, NULL,
1242 true, true,
1243 false, false,
1244 prettyFlags, false);
1245}

References GET_PRETTY_FLAGS, and pg_get_indexdef_worker().

Referenced by BuildIndexValueDescription().

◆ pg_get_indexdef_columns_extended()

char * pg_get_indexdef_columns_extended ( Oid  indexrelid,
bits16  flags 
)

Definition at line 1249 of file ruleutils.c.

1250{
1251 bool pretty = ((flags & RULE_INDEXDEF_PRETTY) != 0);
1252 bool keys_only = ((flags & RULE_INDEXDEF_KEYS_ONLY) != 0);
1253 int prettyFlags;
1254
1255 prettyFlags = GET_PRETTY_FLAGS(pretty);
1256
1257 return pg_get_indexdef_worker(indexrelid, 0, NULL,
1258 true, keys_only,
1259 false, false,
1260 prettyFlags, false);
1261}
#define RULE_INDEXDEF_PRETTY
Definition: ruleutils.h:24
#define RULE_INDEXDEF_KEYS_ONLY
Definition: ruleutils.h:25

References GET_PRETTY_FLAGS, pg_get_indexdef_worker(), RULE_INDEXDEF_KEYS_ONLY, and RULE_INDEXDEF_PRETTY.

Referenced by gist_page_items().

◆ pg_get_indexdef_ext()

Datum pg_get_indexdef_ext ( PG_FUNCTION_ARGS  )

Definition at line 1198 of file ruleutils.c.

1199{
1200 Oid indexrelid = PG_GETARG_OID(0);
1201 int32 colno = PG_GETARG_INT32(1);
1202 bool pretty = PG_GETARG_BOOL(2);
1203 int prettyFlags;
1204 char *res;
1205
1206 prettyFlags = GET_PRETTY_FLAGS(pretty);
1207
1208 res = pg_get_indexdef_worker(indexrelid, colno, NULL,
1209 colno != 0, false,
1210 false, false,
1211 prettyFlags, true);
1212
1213 if (res == NULL)
1215
1217}

References GET_PRETTY_FLAGS, pg_get_indexdef_worker(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_indexdef_string()

char * pg_get_indexdef_string ( Oid  indexrelid)

Definition at line 1225 of file ruleutils.c.

1226{
1227 return pg_get_indexdef_worker(indexrelid, 0, NULL,
1228 false, false,
1229 true, true,
1230 0, false);
1231}

References pg_get_indexdef_worker().

Referenced by RememberIndexForRebuilding().

◆ pg_get_indexdef_worker()

static char * pg_get_indexdef_worker ( Oid  indexrelid,
int  colno,
const Oid excludeOps,
bool  attrsOnly,
bool  keysOnly,
bool  showTblSpc,
bool  inherits,
int  prettyFlags,
bool  missing_ok 
)
static

Definition at line 1270 of file ruleutils.c.

1275{
1276 /* might want a separate isConstraint parameter later */
1277 bool isConstraint = (excludeOps != NULL);
1278 HeapTuple ht_idx;
1279 HeapTuple ht_idxrel;
1280 HeapTuple ht_am;
1281 Form_pg_index idxrec;
1282 Form_pg_class idxrelrec;
1283 Form_pg_am amrec;
1284 IndexAmRoutine *amroutine;
1285 List *indexprs;
1286 ListCell *indexpr_item;
1287 List *context;
1288 Oid indrelid;
1289 int keyno;
1290 Datum indcollDatum;
1291 Datum indclassDatum;
1292 Datum indoptionDatum;
1293 oidvector *indcollation;
1294 oidvector *indclass;
1295 int2vector *indoption;
1297 char *str;
1298 char *sep;
1299
1300 /*
1301 * Fetch the pg_index tuple by the Oid of the index
1302 */
1303 ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
1304 if (!HeapTupleIsValid(ht_idx))
1305 {
1306 if (missing_ok)
1307 return NULL;
1308 elog(ERROR, "cache lookup failed for index %u", indexrelid);
1309 }
1310 idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
1311
1312 indrelid = idxrec->indrelid;
1313 Assert(indexrelid == idxrec->indexrelid);
1314
1315 /* Must get indcollation, indclass, and indoption the hard way */
1316 indcollDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1317 Anum_pg_index_indcollation);
1318 indcollation = (oidvector *) DatumGetPointer(indcollDatum);
1319
1320 indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1321 Anum_pg_index_indclass);
1322 indclass = (oidvector *) DatumGetPointer(indclassDatum);
1323
1324 indoptionDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1325 Anum_pg_index_indoption);
1326 indoption = (int2vector *) DatumGetPointer(indoptionDatum);
1327
1328 /*
1329 * Fetch the pg_class tuple of the index relation
1330 */
1331 ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexrelid));
1332 if (!HeapTupleIsValid(ht_idxrel))
1333 elog(ERROR, "cache lookup failed for relation %u", indexrelid);
1334 idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
1335
1336 /*
1337 * Fetch the pg_am tuple of the index' access method
1338 */
1339 ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
1340 if (!HeapTupleIsValid(ht_am))
1341 elog(ERROR, "cache lookup failed for access method %u",
1342 idxrelrec->relam);
1343 amrec = (Form_pg_am) GETSTRUCT(ht_am);
1344
1345 /* Fetch the index AM's API struct */
1346 amroutine = GetIndexAmRoutine(amrec->amhandler);
1347
1348 /*
1349 * Get the index expressions, if any. (NOTE: we do not use the relcache
1350 * versions of the expressions and predicate, because we want to display
1351 * non-const-folded expressions.)
1352 */
1353 if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs, NULL))
1354 {
1355 Datum exprsDatum;
1356 char *exprsString;
1357
1358 exprsDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1359 Anum_pg_index_indexprs);
1360 exprsString = TextDatumGetCString(exprsDatum);
1361 indexprs = (List *) stringToNode(exprsString);
1362 pfree(exprsString);
1363 }
1364 else
1365 indexprs = NIL;
1366
1367 indexpr_item = list_head(indexprs);
1368
1369 context = deparse_context_for(get_relation_name(indrelid), indrelid);
1370
1371 /*
1372 * Start the index definition. Note that the index's name should never be
1373 * schema-qualified, but the indexed rel's name may be.
1374 */
1376
1377 if (!attrsOnly)
1378 {
1379 if (!isConstraint)
1380 appendStringInfo(&buf, "CREATE %sINDEX %s ON %s%s USING %s (",
1381 idxrec->indisunique ? "UNIQUE " : "",
1382 quote_identifier(NameStr(idxrelrec->relname)),
1383 idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
1384 && !inherits ? "ONLY " : "",
1385 (prettyFlags & PRETTYFLAG_SCHEMA) ?
1386 generate_relation_name(indrelid, NIL) :
1388 quote_identifier(NameStr(amrec->amname)));
1389 else /* currently, must be EXCLUDE constraint */
1390 appendStringInfo(&buf, "EXCLUDE USING %s (",
1391 quote_identifier(NameStr(amrec->amname)));
1392 }
1393
1394 /*
1395 * Report the indexed attributes
1396 */
1397 sep = "";
1398 for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1399 {
1400 AttrNumber attnum = idxrec->indkey.values[keyno];
1401 Oid keycoltype;
1402 Oid keycolcollation;
1403
1404 /*
1405 * Ignore non-key attributes if told to.
1406 */
1407 if (keysOnly && keyno >= idxrec->indnkeyatts)
1408 break;
1409
1410 /* Otherwise, print INCLUDE to divide key and non-key attrs. */
1411 if (!colno && keyno == idxrec->indnkeyatts)
1412 {
1413 appendStringInfoString(&buf, ") INCLUDE (");
1414 sep = "";
1415 }
1416
1417 if (!colno)
1419 sep = ", ";
1420
1421 if (attnum != 0)
1422 {
1423 /* Simple index column */
1424 char *attname;
1425 int32 keycoltypmod;
1426
1427 attname = get_attname(indrelid, attnum, false);
1428 if (!colno || colno == keyno + 1)
1430 get_atttypetypmodcoll(indrelid, attnum,
1431 &keycoltype, &keycoltypmod,
1432 &keycolcollation);
1433 }
1434 else
1435 {
1436 /* expressional index */
1437 Node *indexkey;
1438
1439 if (indexpr_item == NULL)
1440 elog(ERROR, "too few entries in indexprs list");
1441 indexkey = (Node *) lfirst(indexpr_item);
1442 indexpr_item = lnext(indexprs, indexpr_item);
1443 /* Deparse */
1444 str = deparse_expression_pretty(indexkey, context, false, false,
1445 prettyFlags, 0);
1446 if (!colno || colno == keyno + 1)
1447 {
1448 /* Need parens if it's not a bare function call */
1449 if (looks_like_function(indexkey))
1451 else
1452 appendStringInfo(&buf, "(%s)", str);
1453 }
1454 keycoltype = exprType(indexkey);
1455 keycolcollation = exprCollation(indexkey);
1456 }
1457
1458 /* Print additional decoration for (selected) key columns */
1459 if (!attrsOnly && keyno < idxrec->indnkeyatts &&
1460 (!colno || colno == keyno + 1))
1461 {
1462 int16 opt = indoption->values[keyno];
1463 Oid indcoll = indcollation->values[keyno];
1464 Datum attoptions = get_attoptions(indexrelid, keyno + 1);
1465 bool has_options = attoptions != (Datum) 0;
1466
1467 /* Add collation, if not default for column */
1468 if (OidIsValid(indcoll) && indcoll != keycolcollation)
1469 appendStringInfo(&buf, " COLLATE %s",
1470 generate_collation_name((indcoll)));
1471
1472 /* Add the operator class name, if not default */
1473 get_opclass_name(indclass->values[keyno],
1474 has_options ? InvalidOid : keycoltype, &buf);
1475
1476 if (has_options)
1477 {
1479 get_reloptions(&buf, attoptions);
1481 }
1482
1483 /* Add options if relevant */
1484 if (amroutine->amcanorder)
1485 {
1486 /* if it supports sort ordering, report DESC and NULLS opts */
1487 if (opt & INDOPTION_DESC)
1488 {
1489 appendStringInfoString(&buf, " DESC");
1490 /* NULLS FIRST is the default in this case */
1491 if (!(opt & INDOPTION_NULLS_FIRST))
1492 appendStringInfoString(&buf, " NULLS LAST");
1493 }
1494 else
1495 {
1496 if (opt & INDOPTION_NULLS_FIRST)
1497 appendStringInfoString(&buf, " NULLS FIRST");
1498 }
1499 }
1500
1501 /* Add the exclusion operator if relevant */
1502 if (excludeOps != NULL)
1503 appendStringInfo(&buf, " WITH %s",
1504 generate_operator_name(excludeOps[keyno],
1505 keycoltype,
1506 keycoltype));
1507 }
1508 }
1509
1510 if (!attrsOnly)
1511 {
1513
1514 if (idxrec->indnullsnotdistinct)
1515 appendStringInfoString(&buf, " NULLS NOT DISTINCT");
1516
1517 /*
1518 * If it has options, append "WITH (options)"
1519 */
1520 str = flatten_reloptions(indexrelid);
1521 if (str)
1522 {
1523 appendStringInfo(&buf, " WITH (%s)", str);
1524 pfree(str);
1525 }
1526
1527 /*
1528 * Print tablespace, but only if requested
1529 */
1530 if (showTblSpc)
1531 {
1532 Oid tblspc;
1533
1534 tblspc = get_rel_tablespace(indexrelid);
1535 if (OidIsValid(tblspc))
1536 {
1537 if (isConstraint)
1538 appendStringInfoString(&buf, " USING INDEX");
1539 appendStringInfo(&buf, " TABLESPACE %s",
1541 }
1542 }
1543
1544 /*
1545 * If it's a partial index, decompile and append the predicate
1546 */
1547 if (!heap_attisnull(ht_idx, Anum_pg_index_indpred, NULL))
1548 {
1549 Node *node;
1550 Datum predDatum;
1551 char *predString;
1552
1553 /* Convert text string to node tree */
1554 predDatum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1555 Anum_pg_index_indpred);
1556 predString = TextDatumGetCString(predDatum);
1557 node = (Node *) stringToNode(predString);
1558 pfree(predString);
1559
1560 /* Deparse */
1561 str = deparse_expression_pretty(node, context, false, false,
1562 prettyFlags, 0);
1563 if (isConstraint)
1564 appendStringInfo(&buf, " WHERE (%s)", str);
1565 else
1566 appendStringInfo(&buf, " WHERE %s", str);
1567 }
1568 }
1569
1570 /* Clean up */
1571 ReleaseSysCache(ht_idx);
1572 ReleaseSysCache(ht_idxrel);
1573 ReleaseSysCache(ht_am);
1574
1575 return buf.data;
1576}
IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition: amapi.c:33
int16_t int16
Definition: c.h:497
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:456
Datum get_attoptions(Oid relid, int16 attnum)
Definition: lsyscache.c:1005
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition: lsyscache.c:978
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
FormData_pg_am * Form_pg_am
Definition: pg_am.h:48
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
bool amcanorder
Definition: amapi.h:243
Definition: c.h:686
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:693
Definition: c.h:697
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:704

References IndexAmRoutine::amcanorder, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), attname, attnum, buf, DatumGetPointer(), deparse_context_for(), deparse_expression_pretty(), elog, ERROR, exprCollation(), exprType(), flatten_reloptions(), generate_collation_name(), generate_operator_name(), generate_qualified_relation_name(), generate_relation_name(), get_attname(), get_attoptions(), get_atttypetypmodcoll(), get_opclass_name(), get_rel_tablespace(), get_relation_name(), get_reloptions(), get_tablespace_name(), GetIndexAmRoutine(), GETSTRUCT(), heap_attisnull(), HeapTupleIsValid, initStringInfo(), InvalidOid, lfirst, list_head(), lnext(), looks_like_function(), NameStr, NIL, ObjectIdGetDatum(), OidIsValid, pfree(), PRETTYFLAG_SCHEMA, quote_identifier(), ReleaseSysCache(), SearchSysCache1(), str, stringToNode(), SysCacheGetAttrNotNull(), TextDatumGetCString, int2vector::values, and oidvector::values.

Referenced by pg_get_constraintdef_worker(), pg_get_indexdef(), pg_get_indexdef_columns(), pg_get_indexdef_columns_extended(), pg_get_indexdef_ext(), and pg_get_indexdef_string().

◆ pg_get_partconstrdef_string()

char * pg_get_partconstrdef_string ( Oid  partitionId,
char *  aliasname 
)

Definition at line 2128 of file ruleutils.c.

2129{
2130 Expr *constr_expr;
2131 List *context;
2132
2133 constr_expr = get_partition_qual_relid(partitionId);
2134 context = deparse_context_for(aliasname, partitionId);
2135
2136 return deparse_expression((Node *) constr_expr, context, true, false);
2137}
Expr * get_partition_qual_relid(Oid relid)
Definition: partcache.c:299

References deparse_context_for(), deparse_expression(), and get_partition_qual_relid().

Referenced by RI_PartitionRemove_Check().

◆ pg_get_partition_constraintdef()

Datum pg_get_partition_constraintdef ( PG_FUNCTION_ARGS  )

Definition at line 2096 of file ruleutils.c.

2097{
2098 Oid relationId = PG_GETARG_OID(0);
2099 Expr *constr_expr;
2100 int prettyFlags;
2101 List *context;
2102 char *consrc;
2103
2104 constr_expr = get_partition_qual_relid(relationId);
2105
2106 /* Quick exit if no partition constraint */
2107 if (constr_expr == NULL)
2109
2110 /*
2111 * Deparse and return the constraint expression.
2112 */
2113 prettyFlags = PRETTYFLAG_INDENT;
2114 context = deparse_context_for(get_relation_name(relationId), relationId);
2115 consrc = deparse_expression_pretty((Node *) constr_expr, context, false,
2116 false, prettyFlags, 0);
2117
2119}

References deparse_context_for(), deparse_expression_pretty(), get_partition_qual_relid(), get_relation_name(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, and string_to_text().

◆ pg_get_partkeydef()

Datum pg_get_partkeydef ( PG_FUNCTION_ARGS  )

Definition at line 1909 of file ruleutils.c.

1910{
1911 Oid relid = PG_GETARG_OID(0);
1912 char *res;
1913
1914 res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
1915
1916 if (res == NULL)
1918
1920}
static char * pg_get_partkeydef_worker(Oid relid, int prettyFlags, bool attrsOnly, bool missing_ok)
Definition: ruleutils.c:1937

References pg_get_partkeydef_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, and string_to_text().

◆ pg_get_partkeydef_columns()

char * pg_get_partkeydef_columns ( Oid  relid,
bool  pretty 
)

Definition at line 1924 of file ruleutils.c.

1925{
1926 int prettyFlags;
1927
1928 prettyFlags = GET_PRETTY_FLAGS(pretty);
1929
1930 return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
1931}

References GET_PRETTY_FLAGS, and pg_get_partkeydef_worker().

Referenced by ExecBuildSlotPartitionKeyDescription().

◆ pg_get_partkeydef_worker()

static char * pg_get_partkeydef_worker ( Oid  relid,
int  prettyFlags,
bool  attrsOnly,
bool  missing_ok 
)
static

Definition at line 1937 of file ruleutils.c.

1939{
1941 HeapTuple tuple;
1942 oidvector *partclass;
1943 oidvector *partcollation;
1944 List *partexprs;
1945 ListCell *partexpr_item;
1946 List *context;
1947 Datum datum;
1949 int keyno;
1950 char *str;
1951 char *sep;
1952
1953 tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
1954 if (!HeapTupleIsValid(tuple))
1955 {
1956 if (missing_ok)
1957 return NULL;
1958 elog(ERROR, "cache lookup failed for partition key of %u", relid);
1959 }
1960
1961 form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
1962
1963 Assert(form->partrelid == relid);
1964
1965 /* Must get partclass and partcollation the hard way */
1966 datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
1967 Anum_pg_partitioned_table_partclass);
1968 partclass = (oidvector *) DatumGetPointer(datum);
1969
1970 datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
1971 Anum_pg_partitioned_table_partcollation);
1972 partcollation = (oidvector *) DatumGetPointer(datum);
1973
1974
1975 /*
1976 * Get the expressions, if any. (NOTE: we do not use the relcache
1977 * versions of the expressions, because we want to display
1978 * non-const-folded expressions.)
1979 */
1980 if (!heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
1981 {
1982 Datum exprsDatum;
1983 char *exprsString;
1984
1985 exprsDatum = SysCacheGetAttrNotNull(PARTRELID, tuple,
1986 Anum_pg_partitioned_table_partexprs);
1987 exprsString = TextDatumGetCString(exprsDatum);
1988 partexprs = (List *) stringToNode(exprsString);
1989
1990 if (!IsA(partexprs, List))
1991 elog(ERROR, "unexpected node type found in partexprs: %d",
1992 (int) nodeTag(partexprs));
1993
1994 pfree(exprsString);
1995 }
1996 else
1997 partexprs = NIL;
1998
1999 partexpr_item = list_head(partexprs);
2000 context = deparse_context_for(get_relation_name(relid), relid);
2001
2003
2004 switch (form->partstrat)
2005 {
2007 if (!attrsOnly)
2008 appendStringInfoString(&buf, "HASH");
2009 break;
2011 if (!attrsOnly)
2012 appendStringInfoString(&buf, "LIST");
2013 break;
2015 if (!attrsOnly)
2016 appendStringInfoString(&buf, "RANGE");
2017 break;
2018 default:
2019 elog(ERROR, "unexpected partition strategy: %d",
2020 (int) form->partstrat);
2021 }
2022
2023 if (!attrsOnly)
2025 sep = "";
2026 for (keyno = 0; keyno < form->partnatts; keyno++)
2027 {
2028 AttrNumber attnum = form->partattrs.values[keyno];
2029 Oid keycoltype;
2030 Oid keycolcollation;
2031 Oid partcoll;
2032
2034 sep = ", ";
2035 if (attnum != 0)
2036 {
2037 /* Simple attribute reference */
2038 char *attname;
2039 int32 keycoltypmod;
2040
2041 attname = get_attname(relid, attnum, false);
2044 &keycoltype, &keycoltypmod,
2045 &keycolcollation);
2046 }
2047 else
2048 {
2049 /* Expression */
2050 Node *partkey;
2051
2052 if (partexpr_item == NULL)
2053 elog(ERROR, "too few entries in partexprs list");
2054 partkey = (Node *) lfirst(partexpr_item);
2055 partexpr_item = lnext(partexprs, partexpr_item);
2056
2057 /* Deparse */
2058 str = deparse_expression_pretty(partkey, context, false, false,
2059 prettyFlags, 0);
2060 /* Need parens if it's not a bare function call */
2061 if (looks_like_function(partkey))
2063 else
2064 appendStringInfo(&buf, "(%s)", str);
2065
2066 keycoltype = exprType(partkey);
2067 keycolcollation = exprCollation(partkey);
2068 }
2069
2070 /* Add collation, if not default for column */
2071 partcoll = partcollation->values[keyno];
2072 if (!attrsOnly && OidIsValid(partcoll) && partcoll != keycolcollation)
2073 appendStringInfo(&buf, " COLLATE %s",
2074 generate_collation_name((partcoll)));
2075
2076 /* Add the operator class name, if not default */
2077 if (!attrsOnly)
2078 get_opclass_name(partclass->values[keyno], keycoltype, &buf);
2079 }
2080
2081 if (!attrsOnly)
2083
2084 /* Clean up */
2085 ReleaseSysCache(tuple);
2086
2087 return buf.data;
2088}
FormData_pg_partitioned_table * Form_pg_partitioned_table

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), attname, attnum, buf, DatumGetPointer(), deparse_context_for(), deparse_expression_pretty(), elog, ERROR, exprCollation(), exprType(), generate_collation_name(), get_attname(), get_atttypetypmodcoll(), get_opclass_name(), get_relation_name(), GETSTRUCT(), heap_attisnull(), HeapTupleIsValid, initStringInfo(), IsA, lfirst, list_head(), lnext(), looks_like_function(), NIL, nodeTag, ObjectIdGetDatum(), OidIsValid, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, pfree(), quote_identifier(), ReleaseSysCache(), SearchSysCache1(), str, stringToNode(), SysCacheGetAttrNotNull(), TextDatumGetCString, and oidvector::values.

Referenced by pg_get_partkeydef(), and pg_get_partkeydef_columns().

◆ pg_get_querydef()

char * pg_get_querydef ( Query query,
bool  pretty 
)

Definition at line 1588 of file ruleutils.c.

1589{
1591 int prettyFlags;
1592
1593 prettyFlags = GET_PRETTY_FLAGS(pretty);
1594
1596
1597 get_query_def(query, &buf, NIL, NULL, true,
1598 prettyFlags, WRAP_COLUMN_DEFAULT, 0);
1599
1600 return buf.data;
1601}

References buf, GET_PRETTY_FLAGS, get_query_def(), initStringInfo(), NIL, and WRAP_COLUMN_DEFAULT.

◆ pg_get_ruledef()

Datum pg_get_ruledef ( PG_FUNCTION_ARGS  )

Definition at line 560 of file ruleutils.c.

561{
562 Oid ruleoid = PG_GETARG_OID(0);
563 int prettyFlags;
564 char *res;
565
566 prettyFlags = PRETTYFLAG_INDENT;
567
568 res = pg_get_ruledef_worker(ruleoid, prettyFlags);
569
570 if (res == NULL)
572
574}
static char * pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
Definition: ruleutils.c:597

References pg_get_ruledef_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, and string_to_text().

◆ pg_get_ruledef_ext()

Datum pg_get_ruledef_ext ( PG_FUNCTION_ARGS  )

Definition at line 578 of file ruleutils.c.

579{
580 Oid ruleoid = PG_GETARG_OID(0);
581 bool pretty = PG_GETARG_BOOL(1);
582 int prettyFlags;
583 char *res;
584
585 prettyFlags = GET_PRETTY_FLAGS(pretty);
586
587 res = pg_get_ruledef_worker(ruleoid, prettyFlags);
588
589 if (res == NULL)
591
593}

References GET_PRETTY_FLAGS, pg_get_ruledef_worker(), PG_GETARG_BOOL, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_ruledef_worker()

static char * pg_get_ruledef_worker ( Oid  ruleoid,
int  prettyFlags 
)
static

Definition at line 597 of file ruleutils.c.

598{
599 Datum args[1];
600 char nulls[1];
601 int spirc;
602 HeapTuple ruletup;
603 TupleDesc rulettc;
605
606 /*
607 * Do this first so that string is alloc'd in outer context not SPI's.
608 */
610
611 /*
612 * Connect to SPI manager
613 */
614 SPI_connect();
615
616 /*
617 * On the first call prepare the plan to lookup pg_rewrite. We read
618 * pg_rewrite over the SPI manager instead of using the syscache to be
619 * checked for read access on pg_rewrite.
620 */
621 if (plan_getrulebyoid == NULL)
622 {
623 Oid argtypes[1];
625
626 argtypes[0] = OIDOID;
627 plan = SPI_prepare(query_getrulebyoid, 1, argtypes);
628 if (plan == NULL)
629 elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid);
632 }
633
634 /*
635 * Get the pg_rewrite tuple for this rule
636 */
637 args[0] = ObjectIdGetDatum(ruleoid);
638 nulls[0] = ' ';
639 spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 0);
640 if (spirc != SPI_OK_SELECT)
641 elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
642 if (SPI_processed != 1)
643 {
644 /*
645 * There is no tuple data available here, just keep the output buffer
646 * empty.
647 */
648 }
649 else
650 {
651 /*
652 * Get the rule's definition and put it into executor's memory
653 */
654 ruletup = SPI_tuptable->vals[0];
655 rulettc = SPI_tuptable->tupdesc;
656 make_ruledef(&buf, ruletup, rulettc, prettyFlags);
657 }
658
659 /*
660 * Disconnect from SPI manager
661 */
662 if (SPI_finish() != SPI_OK_FINISH)
663 elog(ERROR, "SPI_finish failed");
664
665 if (buf.len == 0)
666 return NULL;
667
668 return buf.data;
669}
static const char *const query_getrulebyoid
Definition: ruleutils.c:334
static SPIPlanPtr plan_getrulebyoid
Definition: ruleutils.c:333
static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags)
Definition: ruleutils.c:5347
uint64 SPI_processed
Definition: spi.c:44
SPITupleTable * SPI_tuptable
Definition: spi.c:45
int SPI_connect(void)
Definition: spi.c:95
int SPI_finish(void)
Definition: spi.c:183
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:673
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:861
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:977
#define SPI_OK_FINISH
Definition: spi.h:83
#define SPI_OK_SELECT
Definition: spi.h:86
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26

References generate_unaccent_rules::args, buf, elog, ERROR, initStringInfo(), make_ruledef(), ObjectIdGetDatum(), plan, plan_getrulebyoid, query_getrulebyoid, SPI_connect(), SPI_execute_plan(), SPI_finish(), SPI_keepplan(), SPI_OK_FINISH, SPI_OK_SELECT, SPI_prepare(), SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by pg_get_ruledef(), and pg_get_ruledef_ext().

◆ pg_get_serial_sequence()

Datum pg_get_serial_sequence ( PG_FUNCTION_ARGS  )

Definition at line 2833 of file ruleutils.c.

2834{
2835 text *tablename = PG_GETARG_TEXT_PP(0);
2836 text *columnname = PG_GETARG_TEXT_PP(1);
2837 RangeVar *tablerv;
2838 Oid tableOid;
2839 char *column;
2841 Oid sequenceId = InvalidOid;
2842 Relation depRel;
2843 ScanKeyData key[3];
2844 SysScanDesc scan;
2845 HeapTuple tup;
2846
2847 /* Look up table name. Can't lock it - we might not have privileges. */
2849 tableOid = RangeVarGetRelid(tablerv, NoLock, false);
2850
2851 /* Get the number of the column */
2852 column = text_to_cstring(columnname);
2853
2854 attnum = get_attnum(tableOid, column);
2856 ereport(ERROR,
2857 (errcode(ERRCODE_UNDEFINED_COLUMN),
2858 errmsg("column \"%s\" of relation \"%s\" does not exist",
2859 column, tablerv->relname)));
2860
2861 /* Search the dependency table for the dependent sequence */
2862 depRel = table_open(DependRelationId, AccessShareLock);
2863
2864 ScanKeyInit(&key[0],
2865 Anum_pg_depend_refclassid,
2866 BTEqualStrategyNumber, F_OIDEQ,
2867 ObjectIdGetDatum(RelationRelationId));
2868 ScanKeyInit(&key[1],
2869 Anum_pg_depend_refobjid,
2870 BTEqualStrategyNumber, F_OIDEQ,
2871 ObjectIdGetDatum(tableOid));
2872 ScanKeyInit(&key[2],
2873 Anum_pg_depend_refobjsubid,
2874 BTEqualStrategyNumber, F_INT4EQ,
2876
2877 scan = systable_beginscan(depRel, DependReferenceIndexId, true,
2878 NULL, 3, key);
2879
2880 while (HeapTupleIsValid(tup = systable_getnext(scan)))
2881 {
2882 Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
2883
2884 /*
2885 * Look for an auto dependency (serial column) or internal dependency
2886 * (identity column) of a sequence on a column. (We need the relkind
2887 * test because indexes can also have auto dependencies on columns.)
2888 */
2889 if (deprec->classid == RelationRelationId &&
2890 deprec->objsubid == 0 &&
2891 (deprec->deptype == DEPENDENCY_AUTO ||
2892 deprec->deptype == DEPENDENCY_INTERNAL) &&
2893 get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
2894 {
2895 sequenceId = deprec->objid;
2896 break;
2897 }
2898 }
2899
2900 systable_endscan(scan);
2902
2903 if (OidIsValid(sequenceId))
2904 {
2905 char *result;
2906
2907 result = generate_qualified_relation_name(sequenceId);
2908
2910 }
2911
2913}
@ DEPENDENCY_AUTO
Definition: dependency.h:34
@ DEPENDENCY_INTERNAL
Definition: dependency.h:35
#define NoLock
Definition: lockdefs.h:34
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:893
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2086
RangeVar * makeRangeVarFromNameList(const List *names)
Definition: namespace.c:3554
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:80
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:72
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
char * relname
Definition: primnodes.h:83
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3467

References AccessShareLock, attnum, BTEqualStrategyNumber, DEPENDENCY_AUTO, DEPENDENCY_INTERNAL, ereport, errcode(), errmsg(), ERROR, generate_qualified_relation_name(), get_attnum(), get_rel_relkind(), GETSTRUCT(), HeapTupleIsValid, Int32GetDatum(), InvalidAttrNumber, InvalidOid, sort-test::key, makeRangeVarFromNameList(), NoLock, ObjectIdGetDatum(), OidIsValid, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, RangeVarGetRelid, RangeVar::relname, ScanKeyInit(), string_to_text(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), text_to_cstring(), and textToQualifiedNameList().

◆ pg_get_statisticsobj_worker()

static char * pg_get_statisticsobj_worker ( Oid  statextid,
bool  columns_only,
bool  missing_ok 
)
static

Definition at line 1654 of file ruleutils.c.

1655{
1656 Form_pg_statistic_ext statextrec;
1657 HeapTuple statexttup;
1659 int colno;
1660 char *nsp;
1661 ArrayType *arr;
1662 char *enabled;
1663 Datum datum;
1664 bool ndistinct_enabled;
1665 bool dependencies_enabled;
1666 bool mcv_enabled;
1667 int i;
1668 List *context;
1669 ListCell *lc;
1670 List *exprs = NIL;
1671 bool has_exprs;
1672 int ncolumns;
1673
1674 statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
1675
1676 if (!HeapTupleIsValid(statexttup))
1677 {
1678 if (missing_ok)
1679 return NULL;
1680 elog(ERROR, "cache lookup failed for statistics object %u", statextid);
1681 }
1682
1683 /* has the statistics expressions? */
1684 has_exprs = !heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1685
1686 statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
1687
1688 /*
1689 * Get the statistics expressions, if any. (NOTE: we do not use the
1690 * relcache versions of the expressions, because we want to display
1691 * non-const-folded expressions.)
1692 */
1693 if (has_exprs)
1694 {
1695 Datum exprsDatum;
1696 char *exprsString;
1697
1698 exprsDatum = SysCacheGetAttrNotNull(STATEXTOID, statexttup,
1699 Anum_pg_statistic_ext_stxexprs);
1700 exprsString = TextDatumGetCString(exprsDatum);
1701 exprs = (List *) stringToNode(exprsString);
1702 pfree(exprsString);
1703 }
1704 else
1705 exprs = NIL;
1706
1707 /* count the number of columns (attributes and expressions) */
1708 ncolumns = statextrec->stxkeys.dim1 + list_length(exprs);
1709
1711
1712 if (!columns_only)
1713 {
1714 nsp = get_namespace_name_or_temp(statextrec->stxnamespace);
1715 appendStringInfo(&buf, "CREATE STATISTICS %s",
1717 NameStr(statextrec->stxname)));
1718
1719 /*
1720 * Decode the stxkind column so that we know which stats types to
1721 * print.
1722 */
1723 datum = SysCacheGetAttrNotNull(STATEXTOID, statexttup,
1724 Anum_pg_statistic_ext_stxkind);
1725 arr = DatumGetArrayTypeP(datum);
1726 if (ARR_NDIM(arr) != 1 ||
1727 ARR_HASNULL(arr) ||
1728 ARR_ELEMTYPE(arr) != CHAROID)
1729 elog(ERROR, "stxkind is not a 1-D char array");
1730 enabled = (char *) ARR_DATA_PTR(arr);
1731
1732 ndistinct_enabled = false;
1733 dependencies_enabled = false;
1734 mcv_enabled = false;
1735
1736 for (i = 0; i < ARR_DIMS(arr)[0]; i++)
1737 {
1738 if (enabled[i] == STATS_EXT_NDISTINCT)
1739 ndistinct_enabled = true;
1740 else if (enabled[i] == STATS_EXT_DEPENDENCIES)
1741 dependencies_enabled = true;
1742 else if (enabled[i] == STATS_EXT_MCV)
1743 mcv_enabled = true;
1744
1745 /* ignore STATS_EXT_EXPRESSIONS (it's built automatically) */
1746 }
1747
1748 /*
1749 * If any option is disabled, then we'll need to append the types
1750 * clause to show which options are enabled. We omit the types clause
1751 * on purpose when all options are enabled, so a pg_dump/pg_restore
1752 * will create all statistics types on a newer postgres version, if
1753 * the statistics had all options enabled on the original version.
1754 *
1755 * But if the statistics is defined on just a single column, it has to
1756 * be an expression statistics. In that case we don't need to specify
1757 * kinds.
1758 */
1759 if ((!ndistinct_enabled || !dependencies_enabled || !mcv_enabled) &&
1760 (ncolumns > 1))
1761 {
1762 bool gotone = false;
1763
1765
1766 if (ndistinct_enabled)
1767 {
1768 appendStringInfoString(&buf, "ndistinct");
1769 gotone = true;
1770 }
1771
1772 if (dependencies_enabled)
1773 {
1774 appendStringInfo(&buf, "%sdependencies", gotone ? ", " : "");
1775 gotone = true;
1776 }
1777
1778 if (mcv_enabled)
1779 appendStringInfo(&buf, "%smcv", gotone ? ", " : "");
1780
1782 }
1783
1784 appendStringInfoString(&buf, " ON ");
1785 }
1786
1787 /* decode simple column references */
1788 for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
1789 {
1790 AttrNumber attnum = statextrec->stxkeys.values[colno];
1791 char *attname;
1792
1793 if (colno > 0)
1795
1796 attname = get_attname(statextrec->stxrelid, attnum, false);
1797
1799 }
1800
1801 context = deparse_context_for(get_relation_name(statextrec->stxrelid),
1802 statextrec->stxrelid);
1803
1804 foreach(lc, exprs)
1805 {
1806 Node *expr = (Node *) lfirst(lc);
1807 char *str;
1808 int prettyFlags = PRETTYFLAG_PAREN;
1809
1810 str = deparse_expression_pretty(expr, context, false, false,
1811 prettyFlags, 0);
1812
1813 if (colno > 0)
1815
1816 /* Need parens if it's not a bare function call */
1817 if (looks_like_function(expr))
1819 else
1820 appendStringInfo(&buf, "(%s)", str);
1821
1822 colno++;
1823 }
1824
1825 if (!columns_only)
1826 appendStringInfo(&buf, " FROM %s",
1827 generate_relation_name(statextrec->stxrelid, NIL));
1828
1829 ReleaseSysCache(statexttup);
1830
1831 return buf.data;
1832}
#define ARR_DATA_PTR(a)
Definition: array.h:322
#define ARR_HASNULL(a)
Definition: array.h:291
FormData_pg_statistic_ext * Form_pg_statistic_ext

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, attname, attnum, buf, DatumGetArrayTypeP, deparse_context_for(), deparse_expression_pretty(), elog, ERROR, generate_relation_name(), get_attname(), get_namespace_name_or_temp(), get_relation_name(), GETSTRUCT(), heap_attisnull(), HeapTupleIsValid, i, initStringInfo(), lfirst, list_length(), looks_like_function(), NameStr, NIL, ObjectIdGetDatum(), pfree(), PRETTYFLAG_PAREN, quote_identifier(), quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), str, stringToNode(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

Referenced by pg_get_statisticsobjdef(), pg_get_statisticsobjdef_columns(), and pg_get_statisticsobjdef_string().

◆ pg_get_statisticsobjdef()

Datum pg_get_statisticsobjdef ( PG_FUNCTION_ARGS  )

Definition at line 1608 of file ruleutils.c.

1609{
1610 Oid statextid = PG_GETARG_OID(0);
1611 char *res;
1612
1613 res = pg_get_statisticsobj_worker(statextid, false, true);
1614
1615 if (res == NULL)
1617
1619}
static char * pg_get_statisticsobj_worker(Oid statextid, bool columns_only, bool missing_ok)
Definition: ruleutils.c:1654

References pg_get_statisticsobj_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_statisticsobjdef_columns()

Datum pg_get_statisticsobjdef_columns ( PG_FUNCTION_ARGS  )

Definition at line 1637 of file ruleutils.c.

1638{
1639 Oid statextid = PG_GETARG_OID(0);
1640 char *res;
1641
1642 res = pg_get_statisticsobj_worker(statextid, true, true);
1643
1644 if (res == NULL)
1646
1648}

References pg_get_statisticsobj_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_statisticsobjdef_expressions()

Datum pg_get_statisticsobjdef_expressions ( PG_FUNCTION_ARGS  )

Definition at line 1838 of file ruleutils.c.

1839{
1840 Oid statextid = PG_GETARG_OID(0);
1841 Form_pg_statistic_ext statextrec;
1842 HeapTuple statexttup;
1843 Datum datum;
1844 List *context;
1845 ListCell *lc;
1846 List *exprs = NIL;
1847 bool has_exprs;
1848 char *tmp;
1849 ArrayBuildState *astate = NULL;
1850
1851 statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
1852
1853 if (!HeapTupleIsValid(statexttup))
1855
1856 /* Does the stats object have expressions? */
1857 has_exprs = !heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1858
1859 /* no expressions? we're done */
1860 if (!has_exprs)
1861 {
1862 ReleaseSysCache(statexttup);
1864 }
1865
1866 statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
1867
1868 /*
1869 * Get the statistics expressions, and deparse them into text values.
1870 */
1871 datum = SysCacheGetAttrNotNull(STATEXTOID, statexttup,
1872 Anum_pg_statistic_ext_stxexprs);
1873 tmp = TextDatumGetCString(datum);
1874 exprs = (List *) stringToNode(tmp);
1875 pfree(tmp);
1876
1877 context = deparse_context_for(get_relation_name(statextrec->stxrelid),
1878 statextrec->stxrelid);
1879
1880 foreach(lc, exprs)
1881 {
1882 Node *expr = (Node *) lfirst(lc);
1883 char *str;
1884 int prettyFlags = PRETTYFLAG_INDENT;
1885
1886 str = deparse_expression_pretty(expr, context, false, false,
1887 prettyFlags, 0);
1888
1889 astate = accumArrayResult(astate,
1891 false,
1892 TEXTOID,
1894 }
1895
1896 ReleaseSysCache(statexttup);
1897
1899}
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5350
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5420
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
text * cstring_to_text(const char *s)
Definition: varlena.c:192

References accumArrayResult(), cstring_to_text(), CurrentMemoryContext, deparse_context_for(), deparse_expression_pretty(), get_relation_name(), GETSTRUCT(), heap_attisnull(), HeapTupleIsValid, lfirst, makeArrayResult(), NIL, ObjectIdGetDatum(), pfree(), PG_GETARG_OID, PG_RETURN_DATUM, PG_RETURN_NULL, PointerGetDatum(), PRETTYFLAG_INDENT, ReleaseSysCache(), SearchSysCache1(), str, stringToNode(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

◆ pg_get_statisticsobjdef_string()

char * pg_get_statisticsobjdef_string ( Oid  statextid)

Definition at line 1627 of file ruleutils.c.

1628{
1629 return pg_get_statisticsobj_worker(statextid, false, false);
1630}

References pg_get_statisticsobj_worker().

Referenced by RememberStatisticsForRebuilding().

◆ pg_get_triggerdef()

Datum pg_get_triggerdef ( PG_FUNCTION_ARGS  )

Definition at line 871 of file ruleutils.c.

872{
873 Oid trigid = PG_GETARG_OID(0);
874 char *res;
875
876 res = pg_get_triggerdef_worker(trigid, false);
877
878 if (res == NULL)
880
882}
static char * pg_get_triggerdef_worker(Oid trigid, bool pretty)
Definition: ruleutils.c:900

References pg_get_triggerdef_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_triggerdef_ext()

Datum pg_get_triggerdef_ext ( PG_FUNCTION_ARGS  )

Definition at line 885 of file ruleutils.c.

886{
887 Oid trigid = PG_GETARG_OID(0);
888 bool pretty = PG_GETARG_BOOL(1);
889 char *res;
890
891 res = pg_get_triggerdef_worker(trigid, pretty);
892
893 if (res == NULL)
895
897}

References pg_get_triggerdef_worker(), PG_GETARG_BOOL, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pg_get_triggerdef_worker()

static char * pg_get_triggerdef_worker ( Oid  trigid,
bool  pretty 
)
static

Definition at line 900 of file ruleutils.c.

901{
902 HeapTuple ht_trig;
903 Form_pg_trigger trigrec;
905 Relation tgrel;
906 ScanKeyData skey[1];
907 SysScanDesc tgscan;
908 int findx = 0;
909 char *tgname;
910 char *tgoldtable;
911 char *tgnewtable;
912 Datum value;
913 bool isnull;
914
915 /*
916 * Fetch the pg_trigger tuple by the Oid of the trigger
917 */
918 tgrel = table_open(TriggerRelationId, AccessShareLock);
919
920 ScanKeyInit(&skey[0],
921 Anum_pg_trigger_oid,
922 BTEqualStrategyNumber, F_OIDEQ,
923 ObjectIdGetDatum(trigid));
924
925 tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
926 NULL, 1, skey);
927
928 ht_trig = systable_getnext(tgscan);
929
930 if (!HeapTupleIsValid(ht_trig))
931 {
932 systable_endscan(tgscan);
934 return NULL;
935 }
936
937 trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
938
939 /*
940 * Start the trigger definition. Note that the trigger's name should never
941 * be schema-qualified, but the trigger rel's name may be.
942 */
944
945 tgname = NameStr(trigrec->tgname);
946 appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
947 OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
948 quote_identifier(tgname));
949
950 if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
951 appendStringInfoString(&buf, "BEFORE");
952 else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
953 appendStringInfoString(&buf, "AFTER");
954 else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
955 appendStringInfoString(&buf, "INSTEAD OF");
956 else
957 elog(ERROR, "unexpected tgtype value: %d", trigrec->tgtype);
958
959 if (TRIGGER_FOR_INSERT(trigrec->tgtype))
960 {
961 appendStringInfoString(&buf, " INSERT");
962 findx++;
963 }
964 if (TRIGGER_FOR_DELETE(trigrec->tgtype))
965 {
966 if (findx > 0)
967 appendStringInfoString(&buf, " OR DELETE");
968 else
969 appendStringInfoString(&buf, " DELETE");
970 findx++;
971 }
972 if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
973 {
974 if (findx > 0)
975 appendStringInfoString(&buf, " OR UPDATE");
976 else
977 appendStringInfoString(&buf, " UPDATE");
978 findx++;
979 /* tgattr is first var-width field, so OK to access directly */
980 if (trigrec->tgattr.dim1 > 0)
981 {
982 int i;
983
984 appendStringInfoString(&buf, " OF ");
985 for (i = 0; i < trigrec->tgattr.dim1; i++)
986 {
987 char *attname;
988
989 if (i > 0)
991 attname = get_attname(trigrec->tgrelid,
992 trigrec->tgattr.values[i], false);
994 }
995 }
996 }
997 if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
998 {
999 if (findx > 0)
1000 appendStringInfoString(&buf, " OR TRUNCATE");
1001 else
1002 appendStringInfoString(&buf, " TRUNCATE");
1003 findx++;
1004 }
1005
1006 /*
1007 * In non-pretty mode, always schema-qualify the target table name for
1008 * safety. In pretty mode, schema-qualify only if not visible.
1009 */
1010 appendStringInfo(&buf, " ON %s ",
1011 pretty ?
1012 generate_relation_name(trigrec->tgrelid, NIL) :
1013 generate_qualified_relation_name(trigrec->tgrelid));
1014
1015 if (OidIsValid(trigrec->tgconstraint))
1016 {
1017 if (OidIsValid(trigrec->tgconstrrelid))
1018 appendStringInfo(&buf, "FROM %s ",
1019 generate_relation_name(trigrec->tgconstrrelid, NIL));
1020 if (!trigrec->tgdeferrable)
1021 appendStringInfoString(&buf, "NOT ");
1022 appendStringInfoString(&buf, "DEFERRABLE INITIALLY ");
1023 if (trigrec->tginitdeferred)
1024 appendStringInfoString(&buf, "DEFERRED ");
1025 else
1026 appendStringInfoString(&buf, "IMMEDIATE ");
1027 }
1028
1029 value = fastgetattr(ht_trig, Anum_pg_trigger_tgoldtable,
1030 tgrel->rd_att, &isnull);
1031 if (!isnull)
1032 tgoldtable = NameStr(*DatumGetName(value));
1033 else
1034 tgoldtable = NULL;
1035 value = fastgetattr(ht_trig, Anum_pg_trigger_tgnewtable,
1036 tgrel->rd_att, &isnull);
1037 if (!isnull)
1038 tgnewtable = NameStr(*DatumGetName(value));
1039 else
1040 tgnewtable = NULL;
1041 if (tgoldtable != NULL || tgnewtable != NULL)
1042 {
1043 appendStringInfoString(&buf, "REFERENCING ");
1044 if (tgoldtable != NULL)
1045 appendStringInfo(&buf, "OLD TABLE AS %s ",
1046 quote_identifier(tgoldtable));
1047 if (tgnewtable != NULL)
1048 appendStringInfo(&buf, "NEW TABLE AS %s ",
1049 quote_identifier(tgnewtable));
1050 }
1051
1052 if (TRIGGER_FOR_ROW(trigrec->tgtype))
1053 appendStringInfoString(&buf, "FOR EACH ROW ");
1054 else
1055 appendStringInfoString(&buf, "FOR EACH STATEMENT ");
1056
1057 /* If the trigger has a WHEN qualification, add that */
1058 value = fastgetattr(ht_trig, Anum_pg_trigger_tgqual,
1059 tgrel->rd_att, &isnull);
1060 if (!isnull)
1061 {
1062 Node *qual;
1063 char relkind;
1064 deparse_context context;
1065 deparse_namespace dpns;
1066 RangeTblEntry *oldrte;
1067 RangeTblEntry *newrte;
1068
1069 appendStringInfoString(&buf, "WHEN (");
1070
1072
1073 relkind = get_rel_relkind(trigrec->tgrelid);
1074
1075 /* Build minimal OLD and NEW RTEs for the rel */
1076 oldrte = makeNode(RangeTblEntry);
1077 oldrte->rtekind = RTE_RELATION;
1078 oldrte->relid = trigrec->tgrelid;
1079 oldrte->relkind = relkind;
1080 oldrte->rellockmode = AccessShareLock;
1081 oldrte->alias = makeAlias("old", NIL);
1082 oldrte->eref = oldrte->alias;
1083 oldrte->lateral = false;
1084 oldrte->inh = false;
1085 oldrte->inFromCl = true;
1086
1087 newrte = makeNode(RangeTblEntry);
1088 newrte->rtekind = RTE_RELATION;
1089 newrte->relid = trigrec->tgrelid;
1090 newrte->relkind = relkind;
1091 newrte->rellockmode = AccessShareLock;
1092 newrte->alias = makeAlias("new", NIL);
1093 newrte->eref = newrte->alias;
1094 newrte->lateral = false;
1095 newrte->inh = false;
1096 newrte->inFromCl = true;
1097
1098 /* Build two-element rtable */
1099 memset(&dpns, 0, sizeof(dpns));
1100 dpns.rtable = list_make2(oldrte, newrte);
1101 dpns.subplans = NIL;
1102 dpns.ctes = NIL;
1103 dpns.appendrels = NULL;
1104 set_rtable_names(&dpns, NIL, NULL);
1106
1107 /* Set up context with one-deep namespace stack */
1108 context.buf = &buf;
1109 context.namespaces = list_make1(&dpns);
1110 context.resultDesc = NULL;
1111 context.targetList = NIL;
1112 context.windowClause = NIL;
1113 context.varprefix = true;
1114 context.prettyFlags = GET_PRETTY_FLAGS(pretty);
1116 context.indentLevel = PRETTYINDENT_STD;
1117 context.colNamesVisible = true;
1118 context.inGroupBy = false;
1119 context.varInOrderBy = false;
1120 context.appendparents = NULL;
1121
1122 get_rule_expr(qual, &context, false);
1123
1125 }
1126
1127 appendStringInfo(&buf, "EXECUTE FUNCTION %s(",
1128 generate_function_name(trigrec->tgfoid, 0,
1129 NIL, NULL,
1130 false, NULL, false));
1131
1132 if (trigrec->tgnargs > 0)
1133 {
1134 char *p;
1135 int i;
1136
1137 value = fastgetattr(ht_trig, Anum_pg_trigger_tgargs,
1138 tgrel->rd_att, &isnull);
1139 if (isnull)
1140 elog(ERROR, "tgargs is null for trigger %u", trigid);
1141 p = (char *) VARDATA_ANY(DatumGetByteaPP(value));
1142 for (i = 0; i < trigrec->tgnargs; i++)
1143 {
1144 if (i > 0)
1147 /* advance p to next string embedded in tgargs */
1148 while (*p)
1149 p++;
1150 p++;
1151 }
1152 }
1153
1154 /* We deliberately do not put semi-colon at end */
1156
1157 /* Clean up */
1158 systable_endscan(tgscan);
1159
1161
1162 return buf.data;
1163}
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:860
#define list_make2(x1, x2)
Definition: pg_list.h:214
FormData_pg_trigger * Form_pg_trigger
Definition: pg_trigger.h:80
TupleDesc rd_att
Definition: rel.h:112
#define VARDATA_ANY(PTR)
Definition: varatt.h:324

References AccessShareLock, deparse_context::appendparents, deparse_namespace::appendrels, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), attname, BTEqualStrategyNumber, deparse_context::buf, buf, deparse_context::colNamesVisible, deparse_namespace::ctes, DatumGetByteaPP, DatumGetName(), elog, ERROR, fastgetattr(), generate_function_name(), generate_qualified_relation_name(), generate_relation_name(), get_attname(), GET_PRETTY_FLAGS, get_rel_relkind(), get_rule_expr(), GETSTRUCT(), HeapTupleIsValid, i, deparse_context::indentLevel, deparse_context::inGroupBy, RangeTblEntry::inh, initStringInfo(), list_make1, list_make2, makeAlias(), makeNode, deparse_context::namespaces, NameStr, NIL, ObjectIdGetDatum(), OidIsValid, deparse_context::prettyFlags, PRETTYINDENT_STD, quote_identifier(), RelationData::rd_att, RangeTblEntry::relid, deparse_context::resultDesc, deparse_namespace::rtable, RTE_RELATION, RangeTblEntry::rtekind, ScanKeyInit(), set_rtable_names(), set_simple_column_names(), simple_quote_literal(), stringToNode(), deparse_namespace::subplans, systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), deparse_context::targetList, TextDatumGetCString, value, VARDATA_ANY, deparse_context::varInOrderBy, deparse_context::varprefix, deparse_context::windowClause, WRAP_COLUMN_DEFAULT, and deparse_context::wrapColumn.

Referenced by pg_get_triggerdef(), and pg_get_triggerdef_ext().

◆ pg_get_userbyid()

Datum pg_get_userbyid ( PG_FUNCTION_ARGS  )

Definition at line 2795 of file ruleutils.c.

2796{
2797 Oid roleid = PG_GETARG_OID(0);
2798 Name result;
2799 HeapTuple roletup;
2800 Form_pg_authid role_rec;
2801
2802 /*
2803 * Allocate space for the result
2804 */
2805 result = (Name) palloc(NAMEDATALEN);
2806 memset(NameStr(*result), 0, NAMEDATALEN);
2807
2808 /*
2809 * Get the pg_authid entry and print the result
2810 */
2811 roletup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
2812 if (HeapTupleIsValid(roletup))
2813 {
2814 role_rec = (Form_pg_authid) GETSTRUCT(roletup);
2815 *result = role_rec->rolname;
2816 ReleaseSysCache(roletup);
2817 }
2818 else
2819 sprintf(NameStr(*result), "unknown (OID=%u)", roleid);
2820
2821 PG_RETURN_NAME(result);
2822}
NameData * Name
Definition: c.h:715
#define PG_RETURN_NAME(x)
Definition: fmgr.h:363
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
Definition: c.h:712

References GETSTRUCT(), HeapTupleIsValid, NAMEDATALEN, NameStr, ObjectIdGetDatum(), palloc(), PG_GETARG_OID, PG_RETURN_NAME, ReleaseSysCache(), SearchSysCache1(), and sprintf.

◆ pg_get_viewdef()

Datum pg_get_viewdef ( PG_FUNCTION_ARGS  )

Definition at line 678 of file ruleutils.c.

679{
680 /* By OID */
681 Oid viewoid = PG_GETARG_OID(0);
682 int prettyFlags;
683 char *res;
684
685 prettyFlags = PRETTYFLAG_INDENT;
686
687 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
688
689 if (res == NULL)
691
693}
static char * pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
Definition: ruleutils.c:789

References pg_get_viewdef_worker(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, string_to_text(), and WRAP_COLUMN_DEFAULT.

◆ pg_get_viewdef_ext()

Datum pg_get_viewdef_ext ( PG_FUNCTION_ARGS  )

Definition at line 697 of file ruleutils.c.

698{
699 /* By OID */
700 Oid viewoid = PG_GETARG_OID(0);
701 bool pretty = PG_GETARG_BOOL(1);
702 int prettyFlags;
703 char *res;
704
705 prettyFlags = GET_PRETTY_FLAGS(pretty);
706
707 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
708
709 if (res == NULL)
711
713}

References GET_PRETTY_FLAGS, pg_get_viewdef_worker(), PG_GETARG_BOOL, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, string_to_text(), and WRAP_COLUMN_DEFAULT.

◆ pg_get_viewdef_name()

Datum pg_get_viewdef_name ( PG_FUNCTION_ARGS  )

Definition at line 736 of file ruleutils.c.

737{
738 /* By qualified name */
739 text *viewname = PG_GETARG_TEXT_PP(0);
740 int prettyFlags;
741 RangeVar *viewrel;
742 Oid viewoid;
743 char *res;
744
745 prettyFlags = PRETTYFLAG_INDENT;
746
747 /* Look up view name. Can't lock it - we might not have privileges. */
749 viewoid = RangeVarGetRelid(viewrel, NoLock, false);
750
751 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
752
753 if (res == NULL)
755
757}

References makeRangeVarFromNameList(), NoLock, pg_get_viewdef_worker(), PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, PRETTYFLAG_INDENT, RangeVarGetRelid, string_to_text(), textToQualifiedNameList(), and WRAP_COLUMN_DEFAULT.

◆ pg_get_viewdef_name_ext()

Datum pg_get_viewdef_name_ext ( PG_FUNCTION_ARGS  )

Definition at line 761 of file ruleutils.c.

762{
763 /* By qualified name */
764 text *viewname = PG_GETARG_TEXT_PP(0);
765 bool pretty = PG_GETARG_BOOL(1);
766 int prettyFlags;
767 RangeVar *viewrel;
768 Oid viewoid;
769 char *res;
770
771 prettyFlags = GET_PRETTY_FLAGS(pretty);
772
773 /* Look up view name. Can't lock it - we might not have privileges. */
775 viewoid = RangeVarGetRelid(viewrel, NoLock, false);
776
777 res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
778
779 if (res == NULL)
781
783}

References GET_PRETTY_FLAGS, makeRangeVarFromNameList(), NoLock, pg_get_viewdef_worker(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, RangeVarGetRelid, string_to_text(), textToQualifiedNameList(), and WRAP_COLUMN_DEFAULT.

◆ pg_get_viewdef_worker()

static char * pg_get_viewdef_worker ( Oid  viewoid,
int  prettyFlags,
int  wrapColumn 
)
static

Definition at line 789 of file ruleutils.c.

790{
791 Datum args[2];
792 char nulls[2];
793 int spirc;
794 HeapTuple ruletup;
795 TupleDesc rulettc;
797
798 /*
799 * Do this first so that string is alloc'd in outer context not SPI's.
800 */
802
803 /*
804 * Connect to SPI manager
805 */
806 SPI_connect();
807
808 /*
809 * On the first call prepare the plan to lookup pg_rewrite. We read
810 * pg_rewrite over the SPI manager instead of using the syscache to be
811 * checked for read access on pg_rewrite.
812 */
813 if (plan_getviewrule == NULL)
814 {
815 Oid argtypes[2];
817
818 argtypes[0] = OIDOID;
819 argtypes[1] = NAMEOID;
820 plan = SPI_prepare(query_getviewrule, 2, argtypes);
821 if (plan == NULL)
822 elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule);
825 }
826
827 /*
828 * Get the pg_rewrite tuple for the view's SELECT rule
829 */
830 args[0] = ObjectIdGetDatum(viewoid);
832 nulls[0] = ' ';
833 nulls[1] = ' ';
834 spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 0);
835 if (spirc != SPI_OK_SELECT)
836 elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
837 if (SPI_processed != 1)
838 {
839 /*
840 * There is no tuple data available here, just keep the output buffer
841 * empty.
842 */
843 }
844 else
845 {
846 /*
847 * Get the rule's definition and put it into executor's memory
848 */
849 ruletup = SPI_tuptable->vals[0];
850 rulettc = SPI_tuptable->tupdesc;
851 make_viewdef(&buf, ruletup, rulettc, prettyFlags, wrapColumn);
852 }
853
854 /*
855 * Disconnect from SPI manager
856 */
857 if (SPI_finish() != SPI_OK_FINISH)
858 elog(ERROR, "SPI_finish failed");
859
860 if (buf.len == 0)
861 return NULL;
862
863 return buf.data;
864}
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
#define ViewSelectRuleName
static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags, int wrapColumn)
Definition: ruleutils.c:5539
static SPIPlanPtr plan_getviewrule
Definition: ruleutils.c:335
static const char *const query_getviewrule
Definition: ruleutils.c:336

References generate_unaccent_rules::args, buf, CStringGetDatum(), DirectFunctionCall1, elog, ERROR, initStringInfo(), make_viewdef(), namein(), ObjectIdGetDatum(), plan, plan_getviewrule, query_getviewrule, SPI_connect(), SPI_execute_plan(), SPI_finish(), SPI_keepplan(), SPI_OK_FINISH, SPI_OK_SELECT, SPI_prepare(), SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, SPITupleTable::vals, and ViewSelectRuleName.

Referenced by pg_get_viewdef(), pg_get_viewdef_ext(), pg_get_viewdef_name(), pg_get_viewdef_name_ext(), and pg_get_viewdef_wrap().

◆ pg_get_viewdef_wrap()

Datum pg_get_viewdef_wrap ( PG_FUNCTION_ARGS  )

Definition at line 716 of file ruleutils.c.

717{
718 /* By OID */
719 Oid viewoid = PG_GETARG_OID(0);
720 int wrap = PG_GETARG_INT32(1);
721 int prettyFlags;
722 char *res;
723
724 /* calling this implies we want pretty printing */
725 prettyFlags = GET_PRETTY_FLAGS(true);
726
727 res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
728
729 if (res == NULL)
731
733}

References GET_PRETTY_FLAGS, pg_get_viewdef_worker(), PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, and string_to_text().

◆ pop_ancestor_plan()

static void pop_ancestor_plan ( deparse_namespace dpns,
deparse_namespace save_dpns 
)
static

Definition at line 5331 of file ruleutils.c.

5332{
5333 /* Free the ancestor list made in push_ancestor_plan */
5334 list_free(dpns->ancestors);
5335
5336 /* Restore fields changed by push_ancestor_plan */
5337 *dpns = *save_dpns;
5338}
void list_free(List *list)
Definition: list.c:1546

References deparse_namespace::ancestors, and list_free().

Referenced by get_name_for_var_field(), and get_parameter().

◆ pop_child_plan()

static void pop_child_plan ( deparse_namespace dpns,
deparse_namespace save_dpns 
)
static

Definition at line 5280 of file ruleutils.c.

5281{
5282 List *ancestors;
5283
5284 /* Get rid of ancestors list cell added by push_child_plan */
5285 ancestors = list_delete_first(dpns->ancestors);
5286
5287 /* Restore fields changed by push_child_plan */
5288 *dpns = *save_dpns;
5289
5290 /* Make sure dpns->ancestors is right (may be unnecessary) */
5291 dpns->ancestors = ancestors;
5292}

References deparse_namespace::ancestors, and list_delete_first().

Referenced by get_name_for_var_field(), get_variable(), and resolve_special_varno().

◆ print_function_arguments()

static int print_function_arguments ( StringInfo  buf,
HeapTuple  proctup,
bool  print_table_args,
bool  print_defaults 
)
static

Definition at line 3298 of file ruleutils.c.

3300{
3301 Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
3302 int numargs;
3303 Oid *argtypes;
3304 char **argnames;
3305 char *argmodes;
3306 int insertorderbyat = -1;
3307 int argsprinted;
3308 int inputargno;
3309 int nlackdefaults;
3310 List *argdefaults = NIL;
3311 ListCell *nextargdefault = NULL;
3312 int i;
3313
3314 numargs = get_func_arg_info(proctup,
3315 &argtypes, &argnames, &argmodes);
3316
3317 nlackdefaults = numargs;
3318 if (print_defaults && proc->pronargdefaults > 0)
3319 {
3320 Datum proargdefaults;
3321 bool isnull;
3322
3323 proargdefaults = SysCacheGetAttr(PROCOID, proctup,
3324 Anum_pg_proc_proargdefaults,
3325 &isnull);
3326 if (!isnull)
3327 {
3328 char *str;
3329
3330 str = TextDatumGetCString(proargdefaults);
3331 argdefaults = castNode(List, stringToNode(str));
3332 pfree(str);
3333 nextargdefault = list_head(argdefaults);
3334 /* nlackdefaults counts only *input* arguments lacking defaults */
3335 nlackdefaults = proc->pronargs - list_length(argdefaults);
3336 }
3337 }
3338
3339 /* Check for special treatment of ordered-set aggregates */
3340 if (proc->prokind == PROKIND_AGGREGATE)
3341 {
3342 HeapTuple aggtup;
3344
3345 aggtup = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(proc->oid));
3346 if (!HeapTupleIsValid(aggtup))
3347 elog(ERROR, "cache lookup failed for aggregate %u",
3348 proc->oid);
3349 agg = (Form_pg_aggregate) GETSTRUCT(aggtup);
3350 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3351 insertorderbyat = agg->aggnumdirectargs;
3352 ReleaseSysCache(aggtup);
3353 }
3354
3355 argsprinted = 0;
3356 inputargno = 0;
3357 for (i = 0; i < numargs; i++)
3358 {
3359 Oid argtype = argtypes[i];
3360 char *argname = argnames ? argnames[i] : NULL;
3361 char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
3362 const char *modename;
3363 bool isinput;
3364
3365 switch (argmode)
3366 {
3367 case PROARGMODE_IN:
3368
3369 /*
3370 * For procedures, explicitly mark all argument modes, so as
3371 * to avoid ambiguity with the SQL syntax for DROP PROCEDURE.
3372 */
3373 if (proc->prokind == PROKIND_PROCEDURE)
3374 modename = "IN ";
3375 else
3376 modename = "";
3377 isinput = true;
3378 break;
3379 case PROARGMODE_INOUT:
3380 modename = "INOUT ";
3381 isinput = true;
3382 break;
3383 case PROARGMODE_OUT:
3384 modename = "OUT ";
3385 isinput = false;
3386 break;
3387 case PROARGMODE_VARIADIC:
3388 modename = "VARIADIC ";
3389 isinput = true;
3390 break;
3391 case PROARGMODE_TABLE:
3392 modename = "";
3393 isinput = false;
3394 break;
3395 default:
3396 elog(ERROR, "invalid parameter mode '%c'", argmode);
3397 modename = NULL; /* keep compiler quiet */
3398 isinput = false;
3399 break;
3400 }
3401 if (isinput)
3402 inputargno++; /* this is a 1-based counter */
3403
3404 if (print_table_args != (argmode == PROARGMODE_TABLE))
3405 continue;
3406
3407 if (argsprinted == insertorderbyat)
3408 {
3409 if (argsprinted)
3411 appendStringInfoString(buf, "ORDER BY ");
3412 }
3413 else if (argsprinted)
3415
3416 appendStringInfoString(buf, modename);
3417 if (argname && argname[0])
3418 appendStringInfo(buf, "%s ", quote_identifier(argname));
3420 if (print_defaults && isinput && inputargno > nlackdefaults)
3421 {
3422 Node *expr;
3423
3424 Assert(nextargdefault != NULL);
3425 expr = (Node *) lfirst(nextargdefault);
3426 nextargdefault = lnext(argdefaults, nextargdefault);
3427
3428 appendStringInfo(buf, " DEFAULT %s",
3429 deparse_expression(expr, NIL, false, false));
3430 }
3431 argsprinted++;
3432
3433 /* nasty hack: print the last arg twice for variadic ordered-set agg */
3434 if (argsprinted == insertorderbyat && i == numargs - 1)
3435 {
3436 i--;
3437 /* aggs shouldn't have defaults anyway, but just to be sure ... */
3438 print_defaults = false;
3439 }
3440 }
3441
3442 return argsprinted;
3443}
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
FormData_pg_aggregate * Form_pg_aggregate
Definition: pg_aggregate.h:109

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert(), buf, castNode, deparse_expression(), elog, ERROR, format_type_be(), get_func_arg_info(), GETSTRUCT(), HeapTupleIsValid, i, lfirst, list_head(), list_length(), lnext(), NIL, ObjectIdGetDatum(), pfree(), quote_identifier(), ReleaseSysCache(), SearchSysCache1(), str, stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by pg_get_function_arguments(), pg_get_function_identity_arguments(), pg_get_functiondef(), and print_function_rettype().

◆ print_function_rettype()

static void print_function_rettype ( StringInfo  buf,
HeapTuple  proctup 
)
static

Definition at line 3260 of file ruleutils.c.

3261{
3262 Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup);
3263 int ntabargs = 0;
3264 StringInfoData rbuf;
3265
3266 initStringInfo(&rbuf);
3267
3268 if (proc->proretset)
3269 {
3270 /* It might be a table function; try to print the arguments */
3271 appendStringInfoString(&rbuf, "TABLE(");
3272 ntabargs = print_function_arguments(&rbuf, proctup, true, false);
3273 if (ntabargs > 0)
3274 appendStringInfoChar(&rbuf, ')');
3275 else
3276 resetStringInfo(&rbuf);
3277 }
3278
3279 if (ntabargs == 0)
3280 {
3281 /* Not a table function, so do the normal thing */
3282 if (proc->proretset)
3283 appendStringInfoString(&rbuf, "SETOF ");
3284 appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
3285 }
3286
3287 appendBinaryStringInfo(buf, rbuf.data, rbuf.len);
3288}

References appendBinaryStringInfo(), appendStringInfoChar(), appendStringInfoString(), buf, StringInfoData::data, format_type_be(), GETSTRUCT(), initStringInfo(), StringInfoData::len, print_function_arguments(), and resetStringInfo().

Referenced by pg_get_function_result(), and pg_get_functiondef().

◆ print_function_sqlbody()

static void print_function_sqlbody ( StringInfo  buf,
HeapTuple  proctup 
)
static

Definition at line 3556 of file ruleutils.c.

3557{
3558 int numargs;
3559 Oid *argtypes;
3560 char **argnames;
3561 char *argmodes;
3562 deparse_namespace dpns = {0};
3563 Datum tmp;
3564 Node *n;
3565
3566 dpns.funcname = pstrdup(NameStr(((Form_pg_proc) GETSTRUCT(proctup))->proname));
3567 numargs = get_func_arg_info(proctup,
3568 &argtypes, &argnames, &argmodes);
3569 dpns.numargs = numargs;
3570 dpns.argnames = argnames;
3571
3572 tmp = SysCacheGetAttrNotNull(PROCOID, proctup, Anum_pg_proc_prosqlbody);
3574
3575 if (IsA(n, List))
3576 {
3577 List *stmts;
3578 ListCell *lc;
3579
3580 stmts = linitial(castNode(List, n));
3581
3582 appendStringInfoString(buf, "BEGIN ATOMIC\n");
3583
3584 foreach(lc, stmts)
3585 {
3586 Query *query = lfirst_node(Query, lc);
3587
3588 /* It seems advisable to get at least AccessShareLock on rels */
3589 AcquireRewriteLocks(query, false, false);
3590 get_query_def(query, buf, list_make1(&dpns), NULL, false,
3594 }
3595
3597 }
3598 else
3599 {
3600 Query *query = castNode(Query, n);
3601
3602 /* It seems advisable to get at least AccessShareLock on rels */
3603 AcquireRewriteLocks(query, false, false);
3604 get_query_def(query, buf, list_make1(&dpns), NULL, false,
3605 0, WRAP_COLUMN_DEFAULT, 0);
3606 }
3607}
char * pstrdup(const char *in)
Definition: mcxt.c:1699

References AcquireRewriteLocks(), appendStringInfoChar(), appendStringInfoString(), deparse_namespace::argnames, buf, castNode, deparse_namespace::funcname, get_func_arg_info(), get_query_def(), GETSTRUCT(), IsA, lfirst_node, linitial, list_make1, NameStr, deparse_namespace::numargs, PRETTYFLAG_INDENT, proname, pstrdup(), stringToNode(), SysCacheGetAttrNotNull(), TextDatumGetCString, and WRAP_COLUMN_DEFAULT.

Referenced by pg_get_function_sqlbody(), and pg_get_functiondef().

◆ print_function_trftypes()

static void print_function_trftypes ( StringInfo  buf,
HeapTuple  proctup 
)
static

Definition at line 3458 of file ruleutils.c.

3459{
3460 Oid *trftypes;
3461 int ntypes;
3462
3463 ntypes = get_func_trftypes(proctup, &trftypes);
3464 if (ntypes > 0)
3465 {
3466 int i;
3467
3468 appendStringInfoString(buf, " TRANSFORM ");
3469 for (i = 0; i < ntypes; i++)
3470 {
3471 if (i != 0)
3473 appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
3474 }
3476 }
3477}
int get_func_trftypes(HeapTuple procTup, Oid **p_trftypes)
Definition: funcapi.c:1475

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), buf, format_type_be(), get_func_trftypes(), and i.

Referenced by pg_get_functiondef().

◆ printSubscripts()

static void printSubscripts ( SubscriptingRef sbsref,
deparse_context context 
)
static

Definition at line 12989 of file ruleutils.c.

12990{
12991 StringInfo buf = context->buf;
12992 ListCell *lowlist_item;
12993 ListCell *uplist_item;
12994
12995 lowlist_item = list_head(sbsref->reflowerindexpr); /* could be NULL */
12996 foreach(uplist_item, sbsref->refupperindexpr)
12997 {
12999 if (lowlist_item)
13000 {
13001 /* If subexpression is NULL, get_rule_expr prints nothing */
13002 get_rule_expr((Node *) lfirst(lowlist_item), context, false);
13004 lowlist_item = lnext(sbsref->reflowerindexpr, lowlist_item);
13005 }
13006 /* If subexpression is NULL, get_rule_expr prints nothing */
13007 get_rule_expr((Node *) lfirst(uplist_item), context, false);
13009 }
13010}
List * refupperindexpr
Definition: primnodes.h:710
List * reflowerindexpr
Definition: primnodes.h:716

References appendStringInfoChar(), deparse_context::buf, buf, get_rule_expr(), lfirst, list_head(), lnext(), SubscriptingRef::reflowerindexpr, and SubscriptingRef::refupperindexpr.

Referenced by get_rule_expr(), and processIndirection().

◆ processIndirection()

static Node * processIndirection ( Node node,
deparse_context context 
)
static

Definition at line 12911 of file ruleutils.c.

12912{
12913 StringInfo buf = context->buf;
12914 CoerceToDomain *cdomain = NULL;
12915
12916 for (;;)
12917 {
12918 if (node == NULL)
12919 break;
12920 if (IsA(node, FieldStore))
12921 {
12922 FieldStore *fstore = (FieldStore *) node;
12923 Oid typrelid;
12924 char *fieldname;
12925
12926 /* lookup tuple type */
12927 typrelid = get_typ_typrelid(fstore->resulttype);
12928 if (!OidIsValid(typrelid))
12929 elog(ERROR, "argument type %s of FieldStore is not a tuple type",
12930 format_type_be(fstore->resulttype));
12931
12932 /*
12933 * Print the field name. There should only be one target field in
12934 * stored rules. There could be more than that in executable
12935 * target lists, but this function cannot be used for that case.
12936 */
12937 Assert(list_length(fstore->fieldnums) == 1);
12938 fieldname = get_attname(typrelid,
12939 linitial_int(fstore->fieldnums), false);
12940 appendStringInfo(buf, ".%s", quote_identifier(fieldname));
12941
12942 /*
12943 * We ignore arg since it should be an uninteresting reference to
12944 * the target column or subcolumn.
12945 */
12946 node = (Node *) linitial(fstore->newvals);
12947 }
12948 else if (IsA(node, SubscriptingRef))
12949 {
12950 SubscriptingRef *sbsref = (SubscriptingRef *) node;
12951
12952 if (sbsref->refassgnexpr == NULL)
12953 break;
12954
12955 printSubscripts(sbsref, context);
12956
12957 /*
12958 * We ignore refexpr since it should be an uninteresting reference
12959 * to the target column or subcolumn.
12960 */
12961 node = (Node *) sbsref->refassgnexpr;
12962 }
12963 else if (IsA(node, CoerceToDomain))
12964 {
12965 cdomain = (CoerceToDomain *) node;
12966 /* If it's an explicit domain coercion, we're done */
12967 if (cdomain->coercionformat != COERCE_IMPLICIT_CAST)
12968 break;
12969 /* Tentatively descend past the CoerceToDomain */
12970 node = (Node *) cdomain->arg;
12971 }
12972 else
12973 break;
12974 }
12975
12976 /*
12977 * If we descended past a CoerceToDomain whose argument turned out not to
12978 * be a FieldStore or array assignment, back up to the CoerceToDomain.
12979 * (This is not enough to be fully correct if there are nested implicit
12980 * CoerceToDomains, but such cases shouldn't ever occur.)
12981 */
12982 if (cdomain && node == (Node *) cdomain->arg)
12983 node = (Node *) cdomain;
12984
12985 return node;
12986}
Oid get_typ_typrelid(Oid typid)
Definition: lsyscache.c:2814
#define linitial_int(l)
Definition: pg_list.h:179

References appendStringInfo(), CoerceToDomain::arg, Assert(), deparse_context::buf, buf, COERCE_IMPLICIT_CAST, elog, ERROR, format_type_be(), get_attname(), get_typ_typrelid(), IsA, linitial, linitial_int, list_length(), FieldStore::newvals, OidIsValid, printSubscripts(), quote_identifier(), and SubscriptingRef::refassgnexpr.

Referenced by get_insert_query_def(), get_merge_query_def(), get_rule_expr(), and get_update_query_targetlist_def().

◆ push_ancestor_plan()

static void push_ancestor_plan ( deparse_namespace dpns,
ListCell ancestor_cell,
deparse_namespace save_dpns 
)
static

Definition at line 5310 of file ruleutils.c.

5312{
5313 Plan *plan = (Plan *) lfirst(ancestor_cell);
5314
5315 /* Save state for restoration later */
5316 *save_dpns = *dpns;
5317
5318 /* Build a new ancestor list with just this node's ancestors */
5319 dpns->ancestors =
5321 list_cell_number(dpns->ancestors, ancestor_cell) + 1);
5322
5323 /* Set attention on selected ancestor */
5324 set_deparse_plan(dpns, plan);
5325}
static int list_cell_number(const List *l, const ListCell *c)
Definition: pg_list.h:333
static void set_deparse_plan(deparse_namespace *dpns, Plan *plan)
Definition: ruleutils.c:5152

References deparse_namespace::ancestors, lfirst, list_cell_number(), list_copy_tail(), plan, and set_deparse_plan().

Referenced by get_name_for_var_field(), and get_parameter().

◆ push_child_plan()

static void push_child_plan ( deparse_namespace dpns,
Plan plan,
deparse_namespace save_dpns 
)
static

Definition at line 5263 of file ruleutils.c.

5265{
5266 /* Save state for restoration later */
5267 *save_dpns = *dpns;
5268
5269 /* Link current plan node into ancestors list */
5270 dpns->ancestors = lcons(dpns->plan, dpns->ancestors);
5271
5272 /* Set attention on selected child */
5273 set_deparse_plan(dpns, plan);
5274}

References deparse_namespace::ancestors, lcons(), deparse_namespace::plan, plan, and set_deparse_plan().

Referenced by get_name_for_var_field(), get_variable(), and resolve_special_varno().

◆ quote_identifier()

const char * quote_identifier ( const char *  ident)

Definition at line 13019 of file ruleutils.c.

13020{
13021 /*
13022 * Can avoid quoting if ident starts with a lowercase letter or underscore
13023 * and contains only lowercase letters, digits, and underscores, *and* is
13024 * not any SQL keyword. Otherwise, supply quotes.
13025 */
13026 int nquotes = 0;
13027 bool safe;
13028 const char *ptr;
13029 char *result;
13030 char *optr;
13031
13032 /*
13033 * would like to use <ctype.h> macros here, but they might yield unwanted
13034 * locale-specific results...
13035 */
13036 safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
13037
13038 for (ptr = ident; *ptr; ptr++)
13039 {
13040 char ch = *ptr;
13041
13042 if ((ch >= 'a' && ch <= 'z') ||
13043 (ch >= '0' && ch <= '9') ||
13044 (ch == '_'))
13045 {
13046 /* okay */
13047 }
13048 else
13049 {
13050 safe = false;
13051 if (ch == '"')
13052 nquotes++;
13053 }
13054 }
13055
13057 safe = false;
13058
13059 if (safe)
13060 {
13061 /*
13062 * Check for keyword. We quote keywords except for unreserved ones.
13063 * (In some cases we could avoid quoting a col_name or type_func_name
13064 * keyword, but it seems much harder than it's worth to tell that.)
13065 *
13066 * Note: ScanKeywordLookup() does case-insensitive comparison, but
13067 * that's fine, since we already know we have all-lower-case.
13068 */
13069 int kwnum = ScanKeywordLookup(ident, &ScanKeywords);
13070
13071 if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
13072 safe = false;
13073 }
13074
13075 if (safe)
13076 return ident; /* no change needed */
13077
13078 result = (char *) palloc(strlen(ident) + nquotes + 2 + 1);
13079
13080 optr = result;
13081 *optr++ = '"';
13082 for (ptr = ident; *ptr; ptr++)
13083 {
13084 char ch = *ptr;
13085
13086 if (ch == '"')
13087 *optr++ = '"';
13088 *optr++ = ch;
13089 }
13090 *optr++ = '"';
13091 *optr = '\0';
13092
13093 return result;
13094}
const uint8 ScanKeywordCategories[SCANKEYWORDS_NUM_KEYWORDS]
Definition: keywords.c:29
#define ident
Definition: indent_codes.h:47
PGDLLIMPORT const ScanKeywordList ScanKeywords
#define UNRESERVED_KEYWORD
Definition: keywords.h:20
int ScanKeywordLookup(const char *str, const ScanKeywordList *keywords)
Definition: kwlookup.c:38
bool quote_all_identifiers
Definition: ruleutils.c:339

References ident, palloc(), quote_all_identifiers, ScanKeywordCategories, ScanKeywordLookup(), ScanKeywords, and UNRESERVED_KEYWORD.

Referenced by add_cast_to(), appendFunctionName(), ATPrepAlterColumnType(), CheckMyDatabase(), copy_table(), createdb(), CreateSchemaCommand(), decompile_column_index_array(), deparseAnalyzeSql(), deparseColumnRef(), deparseOperatorName(), deparseRelation(), execute_extension_script(), ExplainIndexScanDetails(), ExplainNode(), ExplainTargetRel(), flatten_set_variable_args(), format_operator_extended(), generate_operator_clause(), generate_operator_name(), get_column_alias_list(), get_from_clause_coldeflist(), get_from_clause_item(), get_insert_query_def(), get_json_table(), get_json_table_columns(), get_json_table_nested_columns(), get_merge_query_def(), get_opclass_name(), get_parameter(), get_reloptions(), get_returning_clause(), get_rte_alias(), get_rule_expr(), get_rule_windowclause(), get_rule_windowspec(), get_select_query_def(), get_target_list(), get_update_query_targetlist_def(), get_utility_query_def(), get_variable(), get_windowfunc_expr_helper(), get_with_clause(), get_xmltable(), getObjectIdentityParts(), libpqrcv_alter_slot(), make_ruledef(), NameListToQuotedString(), old_9_6_invalidate_hash_indexes(), pg_get_constraintdef_worker(), pg_get_functiondef(), pg_get_indexdef_worker(), pg_get_partkeydef_worker(), pg_get_statisticsobj_worker(), pg_get_triggerdef_worker(), pg_identify_object(), PLy_quote_ident(), postgresExplainForeignScan(), postgresImportForeignSchema(), print_function_arguments(), process_extension_updates(), processIndirection(), quote_ident(), quote_object_name(), quote_qualified_identifier(), regnamespaceout(), regoperout(), regroleout(), ReplicationSlotDropAtPubNode(), sepgsql_attribute_post_create(), sepgsql_database_post_create(), sepgsql_relation_post_create(), sepgsql_schema_post_create(), serialize_deflist(), set_frozenxids(), show_sortorder_options(), show_window_def(), text_format_string_conversion(), tuple_to_stringinfo(), and worker_spi_main().

◆ quote_qualified_identifier()

◆ removeStringInfoSpaces()

static void removeStringInfoSpaces ( StringInfo  str)
static

Definition at line 9126 of file ruleutils.c.

9127{
9128 while (str->len > 0 && str->data[str->len - 1] == ' ')
9129 str->data[--(str->len)] = '\0';
9130}

References str.

Referenced by appendContextKeyword(), get_from_clause(), and get_target_list().

◆ resolve_special_varno()

static void resolve_special_varno ( Node node,
deparse_context context,
rsv_callback  callback,
void *  callback_arg 
)
static

Definition at line 7899 of file ruleutils.c.

7901{
7902 Var *var;
7903 deparse_namespace *dpns;
7904
7905 /* This function is recursive, so let's be paranoid. */
7907
7908 /* If it's not a Var, invoke the callback. */
7909 if (!IsA(node, Var))
7910 {
7911 (*callback) (node, context, callback_arg);
7912 return;
7913 }
7914
7915 /* Find appropriate nesting depth */
7916 var = (Var *) node;
7917 dpns = (deparse_namespace *) list_nth(context->namespaces,
7918 var->varlevelsup);
7919
7920 /*
7921 * If varno is special, recurse. (Don't worry about varnosyn; if we're
7922 * here, we already decided not to use that.)
7923 */
7924 if (var->varno == OUTER_VAR && dpns->outer_tlist)
7925 {
7926 TargetEntry *tle;
7927 deparse_namespace save_dpns;
7928 Bitmapset *save_appendparents;
7929
7930 tle = get_tle_by_resno(dpns->outer_tlist, var->varattno);
7931 if (!tle)
7932 elog(ERROR, "bogus varattno for OUTER_VAR var: %d", var->varattno);
7933
7934 /*
7935 * If we're descending to the first child of an Append or MergeAppend,
7936 * update appendparents. This will affect deparsing of all Vars
7937 * appearing within the eventually-resolved subexpression.
7938 */
7939 save_appendparents = context->appendparents;
7940
7941 if (IsA(dpns->plan, Append))
7942 context->appendparents = bms_union(context->appendparents,
7943 ((Append *) dpns->plan)->apprelids);
7944 else if (IsA(dpns->plan, MergeAppend))
7945 context->appendparents = bms_union(context->appendparents,
7946 ((MergeAppend *) dpns->plan)->apprelids);
7947
7948 push_child_plan(dpns, dpns->outer_plan, &save_dpns);
7949 resolve_special_varno((Node *) tle->expr, context,
7950 callback, callback_arg);
7951 pop_child_plan(dpns, &save_dpns);
7952 context->appendparents = save_appendparents;
7953 return;
7954 }
7955 else if (var->varno == INNER_VAR && dpns->inner_tlist)
7956 {
7957 TargetEntry *tle;
7958 deparse_namespace save_dpns;
7959
7960 tle = get_tle_by_resno(dpns->inner_tlist, var->varattno);
7961 if (!tle)
7962 elog(ERROR, "bogus varattno for INNER_VAR var: %d", var->varattno);
7963
7964 push_child_plan(dpns, dpns->inner_plan, &save_dpns);
7965 resolve_special_varno((Node *) tle->expr, context,
7966 callback, callback_arg);
7967 pop_child_plan(dpns, &save_dpns);
7968 return;
7969 }
7970 else if (var->varno == INDEX_VAR && dpns->index_tlist)
7971 {
7972 TargetEntry *tle;
7973
7974 tle = get_tle_by_resno(dpns->index_tlist, var->varattno);
7975 if (!tle)
7976 elog(ERROR, "bogus varattno for INDEX_VAR var: %d", var->varattno);
7977
7978 resolve_special_varno((Node *) tle->expr, context,
7979 callback, callback_arg);
7980 return;
7981 }
7982 else if (var->varno < 1 || var->varno > list_length(dpns->rtable))
7983 elog(ERROR, "bogus varno: %d", var->varno);
7984
7985 /* Not special. Just invoke the callback. */
7986 (*callback) (node, context, callback_arg);
7987}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References deparse_context::appendparents, bms_union(), callback(), check_stack_depth(), elog, ERROR, TargetEntry::expr, get_tle_by_resno(), deparse_namespace::index_tlist, INDEX_VAR, deparse_namespace::inner_plan, deparse_namespace::inner_tlist, INNER_VAR, IsA, list_length(), list_nth(), deparse_context::namespaces, deparse_namespace::outer_plan, deparse_namespace::outer_tlist, OUTER_VAR, deparse_namespace::plan, pop_child_plan(), push_child_plan(), resolve_special_varno(), deparse_namespace::rtable, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by get_agg_expr_helper(), get_variable(), and resolve_special_varno().

◆ select_rtable_names_for_explain()

List * select_rtable_names_for_explain ( List rtable,
Bitmapset rels_used 
)

Definition at line 3855 of file ruleutils.c.

3856{
3857 deparse_namespace dpns;
3858
3859 memset(&dpns, 0, sizeof(dpns));
3860 dpns.rtable = rtable;
3861 dpns.subplans = NIL;
3862 dpns.ctes = NIL;
3863 dpns.appendrels = NULL;
3864 set_rtable_names(&dpns, NIL, rels_used);
3865 /* We needn't bother computing column aliases yet */
3866
3867 return dpns.rtable_names;
3868}

References deparse_namespace::appendrels, deparse_namespace::ctes, NIL, deparse_namespace::rtable, deparse_namespace::rtable_names, set_rtable_names(), and deparse_namespace::subplans.

Referenced by ExplainPrintPlan().

◆ set_deparse_context_plan()

List * set_deparse_context_plan ( List dpcontext,
Plan plan,
List ancestors 
)

Definition at line 3825 of file ruleutils.c.

3826{
3827 deparse_namespace *dpns;
3828
3829 /* Should always have one-entry namespace list for Plan deparsing */
3830 Assert(list_length(dpcontext) == 1);
3831 dpns = (deparse_namespace *) linitial(dpcontext);
3832
3833 /* Set our attention on the specific plan node passed in */
3834 dpns->ancestors = ancestors;
3835 set_deparse_plan(dpns, plan);
3836
3837 /* For ModifyTable, set aliases for OLD and NEW in RETURNING */
3838 if (IsA(plan, ModifyTable))
3839 {
3840 dpns->ret_old_alias = ((ModifyTable *) plan)->returningOldAlias;
3841 dpns->ret_new_alias = ((ModifyTable *) plan)->returningNewAlias;
3842 }
3843
3844 return dpcontext;
3845}

References deparse_namespace::ancestors, Assert(), IsA, linitial, list_length(), plan, deparse_namespace::ret_new_alias, deparse_namespace::ret_old_alias, and set_deparse_plan().

Referenced by show_expression(), show_grouping_sets(), show_memoize_info(), show_plan_tlist(), show_sort_group_keys(), show_tablesample(), show_window_def(), and show_window_keys().

◆ set_deparse_for_query()

static void set_deparse_for_query ( deparse_namespace dpns,
Query query,
List parent_namespaces 
)
static

Definition at line 4029 of file ruleutils.c.

4031{
4032 ListCell *lc;
4033 ListCell *lc2;
4034
4035 /* Initialize *dpns and fill rtable/ctes links */
4036 memset(dpns, 0, sizeof(deparse_namespace));
4037 dpns->rtable = query->rtable;
4038 dpns->subplans = NIL;
4039 dpns->ctes = query->cteList;
4040 dpns->appendrels = NULL;
4041 dpns->ret_old_alias = query->returningOldAlias;
4042 dpns->ret_new_alias = query->returningNewAlias;
4043
4044 /* Assign a unique relation alias to each RTE */
4045 set_rtable_names(dpns, parent_namespaces, NULL);
4046
4047 /* Initialize dpns->rtable_columns to contain zeroed structs */
4048 dpns->rtable_columns = NIL;
4049 while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
4051 palloc0(sizeof(deparse_columns)));
4052
4053 /* If it's a utility query, it won't have a jointree */
4054 if (query->jointree)
4055 {
4056 /* Detect whether global uniqueness of USING names is needed */
4057 dpns->unique_using =
4058 has_dangerous_join_using(dpns, (Node *) query->jointree);
4059
4060 /*
4061 * Select names for columns merged by USING, via a recursive pass over
4062 * the query jointree.
4063 */
4064 set_using_names(dpns, (Node *) query->jointree, NIL);
4065 }
4066
4067 /*
4068 * Now assign remaining column aliases for each RTE. We do this in a
4069 * linear scan of the rtable, so as to process RTEs whether or not they
4070 * are in the jointree (we mustn't miss NEW.*, INSERT target relations,
4071 * etc). JOIN RTEs must be processed after their children, but this is
4072 * okay because they appear later in the rtable list than their children
4073 * (cf Asserts in identify_join_columns()).
4074 */
4075 forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
4076 {
4077 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
4078 deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
4079
4080 if (rte->rtekind == RTE_JOIN)
4081 set_join_column_names(dpns, rte, colinfo);
4082 else
4083 set_relation_column_names(dpns, rte, colinfo);
4084 }
4085}
static void set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo)
Definition: ruleutils.c:4375
static void set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing)
Definition: ruleutils.c:4210
static void set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo)
Definition: ruleutils.c:4578
List * rtable_columns
Definition: ruleutils.c:166

References deparse_namespace::appendrels, Query::cteList, deparse_namespace::ctes, forboth, has_dangerous_join_using(), Query::jointree, lappend(), lfirst, list_length(), NIL, palloc0(), deparse_namespace::ret_new_alias, deparse_namespace::ret_old_alias, deparse_namespace::rtable, Query::rtable, deparse_namespace::rtable_columns, RTE_JOIN, RangeTblEntry::rtekind, set_join_column_names(), set_relation_column_names(), set_rtable_names(), set_using_names(), deparse_namespace::subplans, and deparse_namespace::unique_using.

Referenced by get_name_for_var_field(), get_query_def(), and make_ruledef().

◆ set_deparse_plan()

static void set_deparse_plan ( deparse_namespace dpns,
Plan plan 
)
static

Definition at line 5152 of file ruleutils.c.

5153{
5154 dpns->plan = plan;
5155
5156 /*
5157 * We special-case Append and MergeAppend to pretend that the first child
5158 * plan is the OUTER referent; we have to interpret OUTER Vars in their
5159 * tlists according to one of the children, and the first one is the most
5160 * natural choice.
5161 */
5162 if (IsA(plan, Append))
5163 dpns->outer_plan = linitial(((Append *) plan)->appendplans);
5164 else if (IsA(plan, MergeAppend))
5165 dpns->outer_plan = linitial(((MergeAppend *) plan)->mergeplans);
5166 else
5167 dpns->outer_plan = outerPlan(plan);
5168
5169 if (dpns->outer_plan)
5170 dpns->outer_tlist = dpns->outer_plan->targetlist;
5171 else
5172 dpns->outer_tlist = NIL;
5173
5174 /*
5175 * For a SubqueryScan, pretend the subplan is INNER referent. (We don't
5176 * use OUTER because that could someday conflict with the normal meaning.)
5177 * Likewise, for a CteScan, pretend the subquery's plan is INNER referent.
5178 * For a WorkTableScan, locate the parent RecursiveUnion plan node and use
5179 * that as INNER referent.
5180 *
5181 * For MERGE, pretend the ModifyTable's source plan (its outer plan) is
5182 * INNER referent. This is the join from the target relation to the data
5183 * source, and all INNER_VAR Vars in other parts of the query refer to its
5184 * targetlist.
5185 *
5186 * For ON CONFLICT .. UPDATE we just need the inner tlist to point to the
5187 * excluded expression's tlist. (Similar to the SubqueryScan we don't want
5188 * to reuse OUTER, it's used for RETURNING in some modify table cases,
5189 * although not INSERT .. CONFLICT).
5190 */
5191 if (IsA(plan, SubqueryScan))
5192 dpns->inner_plan = ((SubqueryScan *) plan)->subplan;
5193 else if (IsA(plan, CteScan))
5194 dpns->inner_plan = list_nth(dpns->subplans,
5195 ((CteScan *) plan)->ctePlanId - 1);
5196 else if (IsA(plan, WorkTableScan))
5197 dpns->inner_plan = find_recursive_union(dpns,
5198 (WorkTableScan *) plan);
5199 else if (IsA(plan, ModifyTable))
5200 {
5201 if (((ModifyTable *) plan)->operation == CMD_MERGE)
5202 dpns->inner_plan = outerPlan(plan);
5203 else
5204 dpns->inner_plan = plan;
5205 }
5206 else
5207 dpns->inner_plan = innerPlan(plan);
5208
5209 if (IsA(plan, ModifyTable) && ((ModifyTable *) plan)->operation == CMD_INSERT)
5210 dpns->inner_tlist = ((ModifyTable *) plan)->exclRelTlist;
5211 else if (dpns->inner_plan)
5212 dpns->inner_tlist = dpns->inner_plan->targetlist;
5213 else
5214 dpns->inner_tlist = NIL;
5215
5216 /* Set up referent for INDEX_VAR Vars, if needed */
5217 if (IsA(plan, IndexOnlyScan))
5218 dpns->index_tlist = ((IndexOnlyScan *) plan)->indextlist;
5219 else if (IsA(plan, ForeignScan))
5220 dpns->index_tlist = ((ForeignScan *) plan)->fdw_scan_tlist;
5221 else if (IsA(plan, CustomScan))
5222 dpns->index_tlist = ((CustomScan *) plan)->custom_scan_tlist;
5223 else
5224 dpns->index_tlist = NIL;
5225}
#define outerPlan(node)
Definition: plannodes.h:241
static Plan * find_recursive_union(deparse_namespace *dpns, WorkTableScan *wtscan)
Definition: ruleutils.c:5233

References CMD_INSERT, CMD_MERGE, find_recursive_union(), deparse_namespace::index_tlist, deparse_namespace::inner_plan, deparse_namespace::inner_tlist, innerPlan, IsA, linitial, list_nth(), NIL, deparse_namespace::outer_plan, deparse_namespace::outer_tlist, outerPlan, deparse_namespace::plan, plan, deparse_namespace::subplans, and Plan::targetlist.

Referenced by push_ancestor_plan(), push_child_plan(), and set_deparse_context_plan().

◆ set_join_column_names()

static void set_join_column_names ( deparse_namespace dpns,
RangeTblEntry rte,
deparse_columns colinfo 
)
static

Definition at line 4578 of file ruleutils.c.

4580{
4581 deparse_columns *leftcolinfo;
4582 deparse_columns *rightcolinfo;
4583 bool changed_any;
4584 int noldcolumns;
4585 int nnewcolumns;
4586 Bitmapset *leftmerged = NULL;
4587 Bitmapset *rightmerged = NULL;
4588 int i;
4589 int j;
4590 int ic;
4591 int jc;
4592
4593 /* Look up the previously-filled-in child deparse_columns structs */
4594 leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
4595 rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
4596
4597 /*
4598 * Ensure colinfo->colnames has a slot for each column. (It could be long
4599 * enough already, if we pushed down a name for the last column.) Note:
4600 * it's possible that one or both inputs now have more columns than there
4601 * were when the query was parsed, but we'll deal with that below. We
4602 * only need entries in colnames for pre-existing columns.
4603 */
4604 noldcolumns = list_length(rte->eref->colnames);
4605 expand_colnames_array_to(colinfo, noldcolumns);
4606 Assert(colinfo->num_cols == noldcolumns);
4607
4608 /* If the RTE is wide enough, use a hash table to avoid O(N^2) costs */
4609 build_colinfo_names_hash(colinfo);
4610
4611 /*
4612 * Scan the join output columns, select an alias for each one, and store
4613 * it in colinfo->colnames. If there are USING columns, set_using_names()
4614 * already selected their names, so we can start the loop at the first
4615 * non-merged column.
4616 */
4617 changed_any = false;
4618 for (i = list_length(colinfo->usingNames); i < noldcolumns; i++)
4619 {
4620 char *colname = colinfo->colnames[i];
4621 char *real_colname;
4622
4623 /* Join column must refer to at least one input column */
4624 Assert(colinfo->leftattnos[i] != 0 || colinfo->rightattnos[i] != 0);
4625
4626 /* Get the child column name */
4627 if (colinfo->leftattnos[i] > 0)
4628 real_colname = leftcolinfo->colnames[colinfo->leftattnos[i] - 1];
4629 else if (colinfo->rightattnos[i] > 0)
4630 real_colname = rightcolinfo->colnames[colinfo->rightattnos[i] - 1];
4631 else
4632 {
4633 /* We're joining system columns --- use eref name */
4634 real_colname = strVal(list_nth(rte->eref->colnames, i));
4635 }
4636
4637 /* If child col has been dropped, no need to assign a join colname */
4638 if (real_colname == NULL)
4639 {
4640 colinfo->colnames[i] = NULL;
4641 continue;
4642 }
4643
4644 /* In an unnamed join, just report child column names as-is */
4645 if (rte->alias == NULL)
4646 {
4647 colinfo->colnames[i] = real_colname;
4648 add_to_names_hash(colinfo, real_colname);
4649 continue;
4650 }
4651
4652 /* If alias already assigned, that's what to use */
4653 if (colname == NULL)
4654 {
4655 /* If user wrote an alias, prefer that over real column name */
4656 if (rte->alias && i < list_length(rte->alias->colnames))
4657 colname = strVal(list_nth(rte->alias->colnames, i));
4658 else
4659 colname = real_colname;
4660
4661 /* Unique-ify and insert into colinfo */
4662 colname = make_colname_unique(colname, dpns, colinfo);
4663
4664 colinfo->colnames[i] = colname;
4665 add_to_names_hash(colinfo, colname);
4666 }
4667
4668 /* Remember if any assigned aliases differ from "real" name */
4669 if (!changed_any && strcmp(colname, real_colname) != 0)
4670 changed_any = true;
4671 }
4672
4673 /*
4674 * Calculate number of columns the join would have if it were re-parsed
4675 * now, and create storage for the new_colnames and is_new_col arrays.
4676 *
4677 * Note: colname_is_unique will be consulting new_colnames[] during the
4678 * loops below, so its not-yet-filled entries must be zeroes.
4679 */
4680 nnewcolumns = leftcolinfo->num_new_cols + rightcolinfo->num_new_cols -
4681 list_length(colinfo->usingNames);
4682 colinfo->num_new_cols = nnewcolumns;
4683 colinfo->new_colnames = (char **) palloc0(nnewcolumns * sizeof(char *));
4684 colinfo->is_new_col = (bool *) palloc0(nnewcolumns * sizeof(bool));
4685
4686 /*
4687 * Generating the new_colnames array is a bit tricky since any new columns
4688 * added since parse time must be inserted in the right places. This code
4689 * must match the parser, which will order a join's columns as merged
4690 * columns first (in USING-clause order), then non-merged columns from the
4691 * left input (in attnum order), then non-merged columns from the right
4692 * input (ditto). If one of the inputs is itself a join, its columns will
4693 * be ordered according to the same rule, which means newly-added columns
4694 * might not be at the end. We can figure out what's what by consulting
4695 * the leftattnos and rightattnos arrays plus the input is_new_col arrays.
4696 *
4697 * In these loops, i indexes leftattnos/rightattnos (so it's join varattno
4698 * less one), j indexes new_colnames/is_new_col, and ic/jc have similar
4699 * meanings for the current child RTE.
4700 */
4701
4702 /* Handle merged columns; they are first and can't be new */
4703 i = j = 0;
4704 while (i < noldcolumns &&
4705 colinfo->leftattnos[i] != 0 &&
4706 colinfo->rightattnos[i] != 0)
4707 {
4708 /* column name is already determined and known unique */
4709 colinfo->new_colnames[j] = colinfo->colnames[i];
4710 colinfo->is_new_col[j] = false;
4711
4712 /* build bitmapsets of child attnums of merged columns */
4713 if (colinfo->leftattnos[i] > 0)
4714 leftmerged = bms_add_member(leftmerged, colinfo->leftattnos[i]);
4715 if (colinfo->rightattnos[i] > 0)
4716 rightmerged = bms_add_member(rightmerged, colinfo->rightattnos[i]);
4717
4718 i++, j++;
4719 }
4720
4721 /* Handle non-merged left-child columns */
4722 ic = 0;
4723 for (jc = 0; jc < leftcolinfo->num_new_cols; jc++)
4724 {
4725 char *child_colname = leftcolinfo->new_colnames[jc];
4726
4727 if (!leftcolinfo->is_new_col[jc])
4728 {
4729 /* Advance ic to next non-dropped old column of left child */
4730 while (ic < leftcolinfo->num_cols &&
4731 leftcolinfo->colnames[ic] == NULL)
4732 ic++;
4733 Assert(ic < leftcolinfo->num_cols);
4734 ic++;
4735 /* If it is a merged column, we already processed it */
4736 if (bms_is_member(ic, leftmerged))
4737 continue;
4738 /* Else, advance i to the corresponding existing join column */
4739 while (i < colinfo->num_cols &&
4740 colinfo->colnames[i] == NULL)
4741 i++;
4742 Assert(i < colinfo->num_cols);
4743 Assert(ic == colinfo->leftattnos[i]);
4744 /* Use the already-assigned name of this column */
4745 colinfo->new_colnames[j] = colinfo->colnames[i];
4746 i++;
4747 }
4748 else
4749 {
4750 /*
4751 * Unique-ify the new child column name and assign, unless we're
4752 * in an unnamed join, in which case just copy
4753 */
4754 if (rte->alias != NULL)
4755 {
4756 colinfo->new_colnames[j] =
4757 make_colname_unique(child_colname, dpns, colinfo);
4758 if (!changed_any &&
4759 strcmp(colinfo->new_colnames[j], child_colname) != 0)
4760 changed_any = true;
4761 }
4762 else
4763 colinfo->new_colnames[j] = child_colname;
4764 add_to_names_hash(colinfo, colinfo->new_colnames[j]);
4765 }
4766
4767 colinfo->is_new_col[j] = leftcolinfo->is_new_col[jc];
4768 j++;
4769 }
4770
4771 /* Handle non-merged right-child columns in exactly the same way */
4772 ic = 0;
4773 for (jc = 0; jc < rightcolinfo->num_new_cols; jc++)
4774 {
4775 char *child_colname = rightcolinfo->new_colnames[jc];
4776
4777 if (!rightcolinfo->is_new_col[jc])
4778 {
4779 /* Advance ic to next non-dropped old column of right child */
4780 while (ic < rightcolinfo->num_cols &&
4781 rightcolinfo->colnames[ic] == NULL)
4782 ic++;
4783 Assert(ic < rightcolinfo->num_cols);
4784 ic++;
4785 /* If it is a merged column, we already processed it */
4786 if (bms_is_member(ic, rightmerged))
4787 continue;
4788 /* Else, advance i to the corresponding existing join column */
4789 while (i < colinfo->num_cols &&
4790 colinfo->colnames[i] == NULL)
4791 i++;
4792 Assert(i < colinfo->num_cols);
4793 Assert(ic == colinfo->rightattnos[i]);
4794 /* Use the already-assigned name of this column */
4795 colinfo->new_colnames[j] = colinfo->colnames[i];
4796 i++;
4797 }
4798 else
4799 {
4800 /*
4801 * Unique-ify the new child column name and assign, unless we're
4802 * in an unnamed join, in which case just copy
4803 */
4804 if (rte->alias != NULL)
4805 {
4806 colinfo->new_colnames[j] =
4807 make_colname_unique(child_colname, dpns, colinfo);
4808 if (!changed_any &&
4809 strcmp(colinfo->new_colnames[j], child_colname) != 0)
4810 changed_any = true;
4811 }
4812 else
4813 colinfo->new_colnames[j] = child_colname;
4814 add_to_names_hash(colinfo, colinfo->new_colnames[j]);
4815 }
4816
4817 colinfo->is_new_col[j] = rightcolinfo->is_new_col[jc];
4818 j++;
4819 }
4820
4821 /* Assert we processed the right number of columns */
4822#ifdef USE_ASSERT_CHECKING
4823 while (i < colinfo->num_cols && colinfo->colnames[i] == NULL)
4824 i++;
4825 Assert(i == colinfo->num_cols);
4826 Assert(j == nnewcolumns);
4827#endif
4828
4829 /* We're now done needing the colinfo's names_hash */
4831
4832 /*
4833 * For a named join, print column aliases if we changed any from the child
4834 * names. Unnamed joins cannot print aliases.
4835 */
4836 if (rte->alias != NULL)
4837 colinfo->printaliases = changed_any;
4838 else
4839 colinfo->printaliases = false;
4840}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
static char * make_colname_unique(char *colname, deparse_namespace *dpns, deparse_columns *colinfo)
Definition: ruleutils.c:4923
static void build_colinfo_names_hash(deparse_columns *colinfo)
Definition: ruleutils.c:4978
static void expand_colnames_array_to(deparse_columns *colinfo, int n)
Definition: ruleutils.c:4962
static void destroy_colinfo_names_hash(deparse_columns *colinfo)
Definition: ruleutils.c:5049
bool * is_new_col
Definition: ruleutils.c:270

References add_to_names_hash(), Assert(), bms_add_member(), bms_is_member(), build_colinfo_names_hash(), deparse_columns::colnames, deparse_columns_fetch, destroy_colinfo_names_hash(), expand_colnames_array_to(), i, deparse_columns::is_new_col, j, deparse_columns::leftattnos, deparse_columns::leftrti, list_length(), list_nth(), make_colname_unique(), deparse_columns::new_colnames, deparse_columns::num_cols, deparse_columns::num_new_cols, palloc0(), deparse_columns::printaliases, deparse_columns::rightattnos, deparse_columns::rightrti, strVal, and deparse_columns::usingNames.

Referenced by set_deparse_for_query().

◆ set_relation_column_names()

static void set_relation_column_names ( deparse_namespace dpns,
RangeTblEntry rte,
deparse_columns colinfo 
)
static

Definition at line 4375 of file ruleutils.c.

4377{
4378 int ncolumns;
4379 char **real_colnames;
4380 bool changed_any;
4381 int noldcolumns;
4382 int i;
4383 int j;
4384
4385 /*
4386 * Construct an array of the current "real" column names of the RTE.
4387 * real_colnames[] will be indexed by physical column number, with NULL
4388 * entries for dropped columns.
4389 */
4390 if (rte->rtekind == RTE_RELATION)
4391 {
4392 /* Relation --- look to the system catalogs for up-to-date info */
4393 Relation rel;
4394 TupleDesc tupdesc;
4395
4396 rel = relation_open(rte->relid, AccessShareLock);
4397 tupdesc = RelationGetDescr(rel);
4398
4399 ncolumns = tupdesc->natts;
4400 real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4401
4402 for (i = 0; i < ncolumns; i++)
4403 {
4404 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4405
4406 if (attr->attisdropped)
4407 real_colnames[i] = NULL;
4408 else
4409 real_colnames[i] = pstrdup(NameStr(attr->attname));
4410 }
4412 }
4413 else
4414 {
4415 /* Otherwise get the column names from eref or expandRTE() */
4416 List *colnames;
4417 ListCell *lc;
4418
4419 /*
4420 * Functions returning composites have the annoying property that some
4421 * of the composite type's columns might have been dropped since the
4422 * query was parsed. If possible, use expandRTE() to handle that
4423 * case, since it has the tedious logic needed to find out about
4424 * dropped columns. However, if we're explaining a plan, then we
4425 * don't have rte->functions because the planner thinks that won't be
4426 * needed later, and that breaks expandRTE(). So in that case we have
4427 * to rely on rte->eref, which may lead us to report a dropped
4428 * column's old name; that seems close enough for EXPLAIN's purposes.
4429 *
4430 * For non-RELATION, non-FUNCTION RTEs, we can just look at rte->eref,
4431 * which should be sufficiently up-to-date: no other RTE types can
4432 * have columns get dropped from under them after parsing.
4433 */
4434 if (rte->rtekind == RTE_FUNCTION && rte->functions != NIL)
4435 {
4436 /* Since we're not creating Vars, rtindex etc. don't matter */
4437 expandRTE(rte, 1, 0, VAR_RETURNING_DEFAULT, -1,
4438 true /* include dropped */ , &colnames, NULL);
4439 }
4440 else
4441 colnames = rte->eref->colnames;
4442
4443 ncolumns = list_length(colnames);
4444 real_colnames = (char **) palloc(ncolumns * sizeof(char *));
4445
4446 i = 0;
4447 foreach(lc, colnames)
4448 {
4449 /*
4450 * If the column name we find here is an empty string, then it's a
4451 * dropped column, so change to NULL.
4452 */
4453 char *cname = strVal(lfirst(lc));
4454
4455 if (cname[0] == '\0')
4456 cname = NULL;
4457 real_colnames[i] = cname;
4458 i++;
4459 }
4460 }
4461
4462 /*
4463 * Ensure colinfo->colnames has a slot for each column. (It could be long
4464 * enough already, if we pushed down a name for the last column.) Note:
4465 * it's possible that there are now more columns than there were when the
4466 * query was parsed, ie colnames could be longer than rte->eref->colnames.
4467 * We must assign unique aliases to the new columns too, else there could
4468 * be unresolved conflicts when the view/rule is reloaded.
4469 */
4470 expand_colnames_array_to(colinfo, ncolumns);
4471 Assert(colinfo->num_cols == ncolumns);
4472
4473 /*
4474 * Make sufficiently large new_colnames and is_new_col arrays, too.
4475 *
4476 * Note: because we leave colinfo->num_new_cols zero until after the loop,
4477 * colname_is_unique will not consult that array, which is fine because it
4478 * would only be duplicate effort.
4479 */
4480 colinfo->new_colnames = (char **) palloc(ncolumns * sizeof(char *));
4481 colinfo->is_new_col = (bool *) palloc(ncolumns * sizeof(bool));
4482
4483 /* If the RTE is wide enough, use a hash table to avoid O(N^2) costs */
4484 build_colinfo_names_hash(colinfo);
4485
4486 /*
4487 * Scan the columns, select a unique alias for each one, and store it in
4488 * colinfo->colnames and colinfo->new_colnames. The former array has NULL
4489 * entries for dropped columns, the latter omits them. Also mark
4490 * new_colnames entries as to whether they are new since parse time; this
4491 * is the case for entries beyond the length of rte->eref->colnames.
4492 */
4493 noldcolumns = list_length(rte->eref->colnames);
4494 changed_any = false;
4495 j = 0;
4496 for (i = 0; i < ncolumns; i++)
4497 {
4498 char *real_colname = real_colnames[i];
4499 char *colname = colinfo->colnames[i];
4500
4501 /* Skip dropped columns */
4502 if (real_colname == NULL)
4503 {
4504 Assert(colname == NULL); /* colnames[i] is already NULL */
4505 continue;
4506 }
4507
4508 /* If alias already assigned, that's what to use */
4509 if (colname == NULL)
4510 {
4511 /* If user wrote an alias, prefer that over real column name */
4512 if (rte->alias && i < list_length(rte->alias->colnames))
4513 colname = strVal(list_nth(rte->alias->colnames, i));
4514 else
4515 colname = real_colname;
4516
4517 /* Unique-ify and insert into colinfo */
4518 colname = make_colname_unique(colname, dpns, colinfo);
4519
4520 colinfo->colnames[i] = colname;
4521 add_to_names_hash(colinfo, colname);
4522 }
4523
4524 /* Put names of non-dropped columns in new_colnames[] too */
4525 colinfo->new_colnames[j] = colname;
4526 /* And mark them as new or not */
4527 colinfo->is_new_col[j] = (i >= noldcolumns);
4528 j++;
4529
4530 /* Remember if any assigned aliases differ from "real" name */
4531 if (!changed_any && strcmp(colname, real_colname) != 0)
4532 changed_any = true;
4533 }
4534
4535 /* We're now done needing the colinfo's names_hash */
4537
4538 /*
4539 * Set correct length for new_colnames[] array. (Note: if columns have
4540 * been added, colinfo->num_cols includes them, which is not really quite
4541 * right but is harmless, since any new columns must be at the end where
4542 * they won't affect varattnos of pre-existing columns.)
4543 */
4544 colinfo->num_new_cols = j;
4545
4546 /*
4547 * For a relation RTE, we need only print the alias column names if any
4548 * are different from the underlying "real" names. For a function RTE,
4549 * always emit a complete column alias list; this is to protect against
4550 * possible instability of the default column names (eg, from altering
4551 * parameter names). For tablefunc RTEs, we never print aliases, because
4552 * the column names are part of the clause itself. For other RTE types,
4553 * print if we changed anything OR if there were user-written column
4554 * aliases (since the latter would be part of the underlying "reality").
4555 */
4556 if (rte->rtekind == RTE_RELATION)
4557 colinfo->printaliases = changed_any;
4558 else if (rte->rtekind == RTE_FUNCTION)
4559 colinfo->printaliases = true;
4560 else if (rte->rtekind == RTE_TABLEFUNC)
4561 colinfo->printaliases = false;
4562 else if (rte->alias && rte->alias->colnames != NIL)
4563 colinfo->printaliases = true;
4564 else
4565 colinfo->printaliases = changed_any;
4566}
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47

References AccessShareLock, add_to_names_hash(), Assert(), build_colinfo_names_hash(), deparse_columns::colnames, destroy_colinfo_names_hash(), expand_colnames_array_to(), expandRTE(), RangeTblEntry::functions, i, deparse_columns::is_new_col, j, lfirst, list_length(), list_nth(), make_colname_unique(), NameStr, TupleDescData::natts, deparse_columns::new_colnames, NIL, deparse_columns::num_cols, deparse_columns::num_new_cols, palloc(), deparse_columns::printaliases, pstrdup(), relation_close(), relation_open(), RelationGetDescr, RangeTblEntry::relid, RTE_FUNCTION, RTE_RELATION, RTE_TABLEFUNC, RangeTblEntry::rtekind, strVal, TupleDescAttr(), and VAR_RETURNING_DEFAULT.

Referenced by set_deparse_for_query(), and set_simple_column_names().

◆ set_rtable_names()

static void set_rtable_names ( deparse_namespace dpns,
List parent_namespaces,
Bitmapset rels_used 
)
static

Definition at line 3884 of file ruleutils.c.

3886{
3887 HASHCTL hash_ctl;
3888 HTAB *names_hash;
3889 NameHashEntry *hentry;
3890 bool found;
3891 int rtindex;
3892 ListCell *lc;
3893
3894 dpns->rtable_names = NIL;
3895 /* nothing more to do if empty rtable */
3896 if (dpns->rtable == NIL)
3897 return;
3898
3899 /*
3900 * We use a hash table to hold known names, so that this process is O(N)
3901 * not O(N^2) for N names.
3902 */
3903 hash_ctl.keysize = NAMEDATALEN;
3904 hash_ctl.entrysize = sizeof(NameHashEntry);
3905 hash_ctl.hcxt = CurrentMemoryContext;
3906 names_hash = hash_create("set_rtable_names names",
3907 list_length(dpns->rtable),
3908 &hash_ctl,
3910
3911 /* Preload the hash table with names appearing in parent_namespaces */
3912 foreach(lc, parent_namespaces)
3913 {
3914 deparse_namespace *olddpns = (deparse_namespace *) lfirst(lc);
3915 ListCell *lc2;
3916
3917 foreach(lc2, olddpns->rtable_names)
3918 {
3919 char *oldname = (char *) lfirst(lc2);
3920
3921 if (oldname == NULL)
3922 continue;
3923 hentry = (NameHashEntry *) hash_search(names_hash,
3924 oldname,
3925 HASH_ENTER,
3926 &found);
3927 /* we do not complain about duplicate names in parent namespaces */
3928 hentry->counter = 0;
3929 }
3930 }
3931
3932 /* Now we can scan the rtable */
3933 rtindex = 1;
3934 foreach(lc, dpns->rtable)
3935 {
3936 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3937 char *refname;
3938
3939 /* Just in case this takes an unreasonable amount of time ... */
3941
3942 if (rels_used && !bms_is_member(rtindex, rels_used))
3943 {
3944 /* Ignore unreferenced RTE */
3945 refname = NULL;
3946 }
3947 else if (rte->alias)
3948 {
3949 /* If RTE has a user-defined alias, prefer that */
3950 refname = rte->alias->aliasname;
3951 }
3952 else if (rte->rtekind == RTE_RELATION)
3953 {
3954 /* Use the current actual name of the relation */
3955 refname = get_rel_name(rte->relid);
3956 }
3957 else if (rte->rtekind == RTE_JOIN)
3958 {
3959 /* Unnamed join has no refname */
3960 refname = NULL;
3961 }
3962 else
3963 {
3964 /* Otherwise use whatever the parser assigned */
3965 refname = rte->eref->aliasname;
3966 }
3967
3968 /*
3969 * If the selected name isn't unique, append digits to make it so, and
3970 * make a new hash entry for it once we've got a unique name. For a
3971 * very long input name, we might have to truncate to stay within
3972 * NAMEDATALEN.
3973 */
3974 if (refname)
3975 {
3976 hentry = (NameHashEntry *) hash_search(names_hash,
3977 refname,
3978 HASH_ENTER,
3979 &found);
3980 if (found)
3981 {
3982 /* Name already in use, must choose a new one */
3983 int refnamelen = strlen(refname);
3984 char *modname = (char *) palloc(refnamelen + 16);
3985 NameHashEntry *hentry2;
3986
3987 do
3988 {
3989 hentry->counter++;
3990 for (;;)
3991 {
3992 memcpy(modname, refname, refnamelen);
3993 sprintf(modname + refnamelen, "_%d", hentry->counter);
3994 if (strlen(modname) < NAMEDATALEN)
3995 break;
3996 /* drop chars from refname to keep all the digits */
3997 refnamelen = pg_mbcliplen(refname, refnamelen,
3998 refnamelen - 1);
3999 }
4000 hentry2 = (NameHashEntry *) hash_search(names_hash,
4001 modname,
4002 HASH_ENTER,
4003 &found);
4004 } while (found);
4005 hentry2->counter = 0; /* init new hash entry */
4006 refname = modname;
4007 }
4008 else
4009 {
4010 /* Name not previously used, need only initialize hentry */
4011 hentry->counter = 0;
4012 }
4013 }
4014
4015 dpns->rtable_names = lappend(dpns->rtable_names, refname);
4016 rtindex++;
4017 }
4018
4019 hash_destroy(names_hash);
4020}
Definition: dynahash.c:220

References bms_is_member(), CHECK_FOR_INTERRUPTS, NameHashEntry::counter, CurrentMemoryContext, HASHCTL::entrysize, get_rel_name(), HASH_CONTEXT, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_search(), HASH_STRINGS, HASHCTL::hcxt, HASHCTL::keysize, lappend(), lfirst, list_length(), NAMEDATALEN, NIL, palloc(), pg_mbcliplen(), RangeTblEntry::relid, deparse_namespace::rtable, deparse_namespace::rtable_names, RTE_JOIN, RTE_RELATION, RangeTblEntry::rtekind, and sprintf.

Referenced by deparse_context_for(), pg_get_triggerdef_worker(), select_rtable_names_for_explain(), and set_deparse_for_query().

◆ set_simple_column_names()

static void set_simple_column_names ( deparse_namespace dpns)
static

Definition at line 4098 of file ruleutils.c.

4099{
4100 ListCell *lc;
4101 ListCell *lc2;
4102
4103 /* Initialize dpns->rtable_columns to contain zeroed structs */
4104 dpns->rtable_columns = NIL;
4105 while (list_length(dpns->rtable_columns) < list_length(dpns->rtable))
4107 palloc0(sizeof(deparse_columns)));
4108
4109 /* Assign unique column aliases within each non-join RTE */
4110 forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
4111 {
4112 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
4113 deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
4114
4115 if (rte->rtekind != RTE_JOIN)
4116 set_relation_column_names(dpns, rte, colinfo);
4117 }
4118}

References forboth, lappend(), lfirst, list_length(), NIL, palloc0(), deparse_namespace::rtable, deparse_namespace::rtable_columns, RTE_JOIN, RangeTblEntry::rtekind, and set_relation_column_names().

Referenced by deparse_context_for(), deparse_context_for_plan_tree(), and pg_get_triggerdef_worker().

◆ set_using_names()

static void set_using_names ( deparse_namespace dpns,
Node jtnode,
List parentUsing 
)
static

Definition at line 4210 of file ruleutils.c.

4211{
4212 if (IsA(jtnode, RangeTblRef))
4213 {
4214 /* nothing to do now */
4215 }
4216 else if (IsA(jtnode, FromExpr))
4217 {
4218 FromExpr *f = (FromExpr *) jtnode;
4219 ListCell *lc;
4220
4221 foreach(lc, f->fromlist)
4222 set_using_names(dpns, (Node *) lfirst(lc), parentUsing);
4223 }
4224 else if (IsA(jtnode, JoinExpr))
4225 {
4226 JoinExpr *j = (JoinExpr *) jtnode;
4227 RangeTblEntry *rte = rt_fetch(j->rtindex, dpns->rtable);
4228 deparse_columns *colinfo = deparse_columns_fetch(j->rtindex, dpns);
4229 int *leftattnos;
4230 int *rightattnos;
4231 deparse_columns *leftcolinfo;
4232 deparse_columns *rightcolinfo;
4233 int i;
4234 ListCell *lc;
4235
4236 /* Get info about the shape of the join */
4237 identify_join_columns(j, rte, colinfo);
4238 leftattnos = colinfo->leftattnos;
4239 rightattnos = colinfo->rightattnos;
4240
4241 /* Look up the not-yet-filled-in child deparse_columns structs */
4242 leftcolinfo = deparse_columns_fetch(colinfo->leftrti, dpns);
4243 rightcolinfo = deparse_columns_fetch(colinfo->rightrti, dpns);
4244
4245 /*
4246 * If this join is unnamed, then we cannot substitute new aliases at
4247 * this level, so any name requirements pushed down to here must be
4248 * pushed down again to the children.
4249 */
4250 if (rte->alias == NULL)
4251 {
4252 for (i = 0; i < colinfo->num_cols; i++)
4253 {
4254 char *colname = colinfo->colnames[i];
4255
4256 if (colname == NULL)
4257 continue;
4258
4259 /* Push down to left column, unless it's a system column */
4260 if (leftattnos[i] > 0)
4261 {
4262 expand_colnames_array_to(leftcolinfo, leftattnos[i]);
4263 leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4264 }
4265
4266 /* Same on the righthand side */
4267 if (rightattnos[i] > 0)
4268 {
4269 expand_colnames_array_to(rightcolinfo, rightattnos[i]);
4270 rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4271 }
4272 }
4273 }
4274
4275 /*
4276 * If there's a USING clause, select the USING column names and push
4277 * those names down to the children. We have two strategies:
4278 *
4279 * If dpns->unique_using is true, we force all USING names to be
4280 * unique across the whole query level. In principle we'd only need
4281 * the names of dangerous USING columns to be globally unique, but to
4282 * safely assign all USING names in a single pass, we have to enforce
4283 * the same uniqueness rule for all of them. However, if a USING
4284 * column's name has been pushed down from the parent, we should use
4285 * it as-is rather than making a uniqueness adjustment. This is
4286 * necessary when we're at an unnamed join, and it creates no risk of
4287 * ambiguity. Also, if there's a user-written output alias for a
4288 * merged column, we prefer to use that rather than the input name;
4289 * this simplifies the logic and seems likely to lead to less aliasing
4290 * overall.
4291 *
4292 * If dpns->unique_using is false, we only need USING names to be
4293 * unique within their own join RTE. We still need to honor
4294 * pushed-down names, though.
4295 *
4296 * Though significantly different in results, these two strategies are
4297 * implemented by the same code, with only the difference of whether
4298 * to put assigned names into dpns->using_names.
4299 */
4300 if (j->usingClause)
4301 {
4302 /* Copy the input parentUsing list so we don't modify it */
4303 parentUsing = list_copy(parentUsing);
4304
4305 /* USING names must correspond to the first join output columns */
4306 expand_colnames_array_to(colinfo, list_length(j->usingClause));
4307 i = 0;
4308 foreach(lc, j->usingClause)
4309 {
4310 char *colname = strVal(lfirst(lc));
4311
4312 /* Assert it's a merged column */
4313 Assert(leftattnos[i] != 0 && rightattnos[i] != 0);
4314
4315 /* Adopt passed-down name if any, else select unique name */
4316 if (colinfo->colnames[i] != NULL)
4317 colname = colinfo->colnames[i];
4318 else
4319 {
4320 /* Prefer user-written output alias if any */
4321 if (rte->alias && i < list_length(rte->alias->colnames))
4322 colname = strVal(list_nth(rte->alias->colnames, i));
4323 /* Make it appropriately unique */
4324 colname = make_colname_unique(colname, dpns, colinfo);
4325 if (dpns->unique_using)
4326 dpns->using_names = lappend(dpns->using_names,
4327 colname);
4328 /* Save it as output column name, too */
4329 colinfo->colnames[i] = colname;
4330 }
4331
4332 /* Remember selected names for use later */
4333 colinfo->usingNames = lappend(colinfo->usingNames, colname);
4334 parentUsing = lappend(parentUsing, colname);
4335
4336 /* Push down to left column, unless it's a system column */
4337 if (leftattnos[i] > 0)
4338 {
4339 expand_colnames_array_to(leftcolinfo, leftattnos[i]);
4340 leftcolinfo->colnames[leftattnos[i] - 1] = colname;
4341 }
4342
4343 /* Same on the righthand side */
4344 if (rightattnos[i] > 0)
4345 {
4346 expand_colnames_array_to(rightcolinfo, rightattnos[i]);
4347 rightcolinfo->colnames[rightattnos[i] - 1] = colname;
4348 }
4349
4350 i++;
4351 }
4352 }
4353
4354 /* Mark child deparse_columns structs with correct parentUsing info */
4355 leftcolinfo->parentUsing = parentUsing;
4356 rightcolinfo->parentUsing = parentUsing;
4357
4358 /* Now recursively assign USING column names in children */
4359 set_using_names(dpns, j->larg, parentUsing);
4360 set_using_names(dpns, j->rarg, parentUsing);
4361 }
4362 else
4363 elog(ERROR, "unrecognized node type: %d",
4364 (int) nodeTag(jtnode));
4365}
static void identify_join_columns(JoinExpr *j, RangeTblEntry *jrte, deparse_columns *colinfo)
Definition: ruleutils.c:5065

References Assert(), deparse_columns::colnames, deparse_columns_fetch, elog, ERROR, expand_colnames_array_to(), FromExpr::fromlist, i, identify_join_columns(), IsA, j, lappend(), deparse_columns::leftattnos, deparse_columns::leftrti, lfirst, list_copy(), list_length(), list_nth(), make_colname_unique(), nodeTag, deparse_columns::num_cols, deparse_columns::parentUsing, deparse_columns::rightattnos, deparse_columns::rightrti, rt_fetch, deparse_namespace::rtable, set_using_names(), strVal, deparse_namespace::unique_using, deparse_namespace::using_names, and deparse_columns::usingNames.

Referenced by set_deparse_for_query(), and set_using_names().

◆ simple_quote_literal()

static void simple_quote_literal ( StringInfo  buf,
const char *  val 
)
static

Definition at line 11783 of file ruleutils.c.

11784{
11785 const char *valptr;
11786
11787 /*
11788 * We form the string literal according to the prevailing setting of
11789 * standard_conforming_strings; we never use E''. User is responsible for
11790 * making sure result is used correctly.
11791 */
11793 for (valptr = val; *valptr; valptr++)
11794 {
11795 char ch = *valptr;
11796
11800 }
11802}
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1134
bool standard_conforming_strings
Definition: scan.l:70

References appendStringInfoChar(), buf, SQL_STR_DOUBLE, standard_conforming_strings, and val.

Referenced by get_const_expr(), get_reloptions(), get_rule_expr(), get_utility_query_def(), pg_get_functiondef(), and pg_get_triggerdef_worker().

◆ string_to_text()

Variable Documentation

◆ plan_getrulebyoid

SPIPlanPtr plan_getrulebyoid = NULL
static

Definition at line 333 of file ruleutils.c.

Referenced by pg_get_ruledef_worker().

◆ plan_getviewrule

SPIPlanPtr plan_getviewrule = NULL
static

Definition at line 335 of file ruleutils.c.

Referenced by pg_get_viewdef_worker().

◆ query_getrulebyoid

const char* const query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1"
static

Definition at line 334 of file ruleutils.c.

Referenced by pg_get_ruledef_worker().

◆ query_getviewrule

const char* const query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2"
static

Definition at line 336 of file ruleutils.c.

Referenced by pg_get_viewdef_worker().

◆ quote_all_identifiers

bool quote_all_identifiers = false

Definition at line 339 of file ruleutils.c.

Referenced by main(), quote_identifier(), and setup_connection().