62 #include "utils/fmgroids.h"
81 #define PRETTYINDENT_STD 8
82 #define PRETTYINDENT_JOIN 4
83 #define PRETTYINDENT_VAR 4
85 #define PRETTYINDENT_LIMIT 40
88 #define PRETTYFLAG_PAREN 0x0001
89 #define PRETTYFLAG_INDENT 0x0002
90 #define PRETTYFLAG_SCHEMA 0x0004
93 #define GET_PRETTY_FLAGS(pretty) \
94 ((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \
98 #define WRAP_COLUMN_DEFAULT 0
101 #define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)
102 #define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)
103 #define PRETTY_SCHEMA(context) ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
310 #define deparse_columns_fetch(rangetable_index, dpns) \
311 ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
334 static const char *
const query_getviewrule =
"SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
349 bool forceprefix,
bool showimplicit,
350 int prettyFlags,
int startIndent);
352 int prettyFlags,
int wrapColumn);
358 const Oid *excludeOps,
359 bool attrsOnly,
bool keysOnly,
360 bool showTblSpc,
bool inherits,
361 int prettyFlags,
bool missing_ok);
365 bool attrsOnly,
bool missing_ok);
367 int prettyFlags,
bool missing_ok);
370 bool print_table_args,
bool print_defaults);
377 List *parent_namespaces);
412 int prettyFlags,
int wrapColumn);
414 TupleDesc resultDesc,
bool colNamesVisible,
415 int prettyFlags,
int wrapColumn,
int startIndent);
457 int indentBefore,
int indentAfter,
int indentPlus);
475 const char *
options,
bool is_json_objectagg);
481 bool is_json_objectagg);
491 bool json_format_by_default);
499 bool is_json_objectagg);
526 bool has_variadic,
bool *use_variadic_p,
544 #define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
620 argtypes[0] = OIDOID;
635 elog(
ERROR,
"failed to get pg_rewrite tuple for rule %u", ruleoid);
812 argtypes[0] = OIDOID;
813 argtypes[1] = NAMEOID;
830 elog(
ERROR,
"failed to get pg_rewrite tuple for view %u", viewoid);
939 tgname =
NameStr(trigrec->tgname);
941 OidIsValid(trigrec->tgconstraint) ?
"CONSTRAINT " :
"",
944 if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
946 else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
948 else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
951 elog(
ERROR,
"unexpected tgtype value: %d", trigrec->tgtype);
953 if (TRIGGER_FOR_INSERT(trigrec->tgtype))
958 if (TRIGGER_FOR_DELETE(trigrec->tgtype))
966 if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
974 if (trigrec->tgattr.dim1 > 0)
979 for (
i = 0;
i < trigrec->tgattr.dim1;
i++)
986 trigrec->tgattr.values[
i],
false);
991 if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
1014 if (!trigrec->tgdeferrable)
1017 if (trigrec->tginitdeferred)
1035 if (tgoldtable != NULL || tgnewtable != NULL)
1038 if (tgoldtable != NULL)
1041 if (tgnewtable != NULL)
1046 if (TRIGGER_FOR_ROW(trigrec->tgtype))
1072 oldrte->
relid = trigrec->tgrelid;
1073 oldrte->relkind = relkind;
1076 oldrte->eref = oldrte->alias;
1077 oldrte->lateral =
false;
1078 oldrte->
inh =
false;
1079 oldrte->inFromCl =
true;
1083 newrte->
relid = trigrec->tgrelid;
1084 newrte->relkind = relkind;
1087 newrte->eref = newrte->alias;
1088 newrte->lateral =
false;
1089 newrte->
inh =
false;
1090 newrte->inFromCl =
true;
1093 memset(&dpns, 0,
sizeof(dpns));
1111 context.colNamesVisible =
true;
1124 false, NULL,
false));
1126 if (trigrec->tgnargs > 0)
1134 elog(
ERROR,
"tgargs is null for trigger %u", trigid);
1136 for (
i = 0;
i < trigrec->tgnargs;
i++)
1238 prettyFlags,
false);
1254 prettyFlags,
false);
1265 const Oid *excludeOps,
1266 bool attrsOnly,
bool keysOnly,
1267 bool showTblSpc,
bool inherits,
1268 int prettyFlags,
bool missing_ok)
1271 bool isConstraint = (excludeOps != NULL);
1285 Datum indclassDatum;
1286 Datum indoptionDatum;
1302 elog(
ERROR,
"cache lookup failed for index %u", indexrelid);
1306 indrelid = idxrec->indrelid;
1307 Assert(indexrelid == idxrec->indexrelid);
1311 Anum_pg_index_indcollation);
1315 Anum_pg_index_indclass);
1319 Anum_pg_index_indoption);
1327 elog(
ERROR,
"cache lookup failed for relation %u", indexrelid);
1335 elog(
ERROR,
"cache lookup failed for access method %u",
1353 Anum_pg_index_indexprs);
1375 idxrec->indisunique ?
"UNIQUE " :
"",
1377 idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
1378 && !inherits ?
"ONLY " :
"",
1392 for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1396 Oid keycolcollation;
1401 if (keysOnly && keyno >= idxrec->indnkeyatts)
1405 if (!colno && keyno == idxrec->indnkeyatts)
1422 if (!colno || colno == keyno + 1)
1425 &keycoltype, &keycoltypmod,
1433 if (indexpr_item == NULL)
1434 elog(
ERROR,
"too few entries in indexprs list");
1436 indexpr_item =
lnext(indexprs, indexpr_item);
1440 if (!colno || colno == keyno + 1)
1453 if (!attrsOnly && keyno < idxrec->indnkeyatts &&
1454 (!colno || colno == keyno + 1))
1457 Oid indcoll = indcollation->
values[keyno];
1459 bool has_options = attoptions != (
Datum) 0;
1462 if (
OidIsValid(indcoll) && indcoll != keycolcollation)
1481 if (opt & INDOPTION_DESC)
1485 if (!(opt & INDOPTION_NULLS_FIRST))
1490 if (opt & INDOPTION_NULLS_FIRST)
1496 if (excludeOps != NULL)
1508 if (idxrec->indnullsnotdistinct)
1549 Anum_pg_index_indpred);
1658 bool ndistinct_enabled;
1659 bool dependencies_enabled;
1674 elog(
ERROR,
"cache lookup failed for statistics object %u", statextid);
1678 has_exprs = !
heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1693 Anum_pg_statistic_ext_stxexprs);
1702 ncolumns = statextrec->stxkeys.dim1 +
list_length(exprs);
1711 NameStr(statextrec->stxname)));
1718 Anum_pg_statistic_ext_stxkind);
1723 elog(
ERROR,
"stxkind is not a 1-D char array");
1726 ndistinct_enabled =
false;
1727 dependencies_enabled =
false;
1728 mcv_enabled =
false;
1732 if (enabled[
i] == STATS_EXT_NDISTINCT)
1733 ndistinct_enabled =
true;
1734 else if (enabled[
i] == STATS_EXT_DEPENDENCIES)
1735 dependencies_enabled =
true;
1736 else if (enabled[
i] == STATS_EXT_MCV)
1753 if ((!ndistinct_enabled || !dependencies_enabled || !mcv_enabled) &&
1756 bool gotone =
false;
1760 if (ndistinct_enabled)
1766 if (dependencies_enabled)
1782 for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
1796 statextrec->stxrelid);
1851 has_exprs = !
heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1866 Anum_pg_statistic_ext_stxexprs);
1872 statextrec->stxrelid);
1932 bool attrsOnly,
bool missing_ok)
1952 elog(
ERROR,
"cache lookup failed for partition key of %u", relid);
1957 Assert(form->partrelid == relid);
1961 Anum_pg_partitioned_table_partclass);
1965 Anum_pg_partitioned_table_partcollation);
1974 if (!
heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
1980 Anum_pg_partitioned_table_partexprs);
1985 elog(
ERROR,
"unexpected node type found in partexprs: %d",
1998 switch (form->partstrat)
2013 elog(
ERROR,
"unexpected partition strategy: %d",
2014 (
int) form->partstrat);
2020 for (keyno = 0; keyno < form->partnatts; keyno++)
2024 Oid keycolcollation;
2038 &keycoltype, &keycoltypmod,
2046 if (partexpr_item == NULL)
2047 elog(
ERROR,
"too few entries in partexprs list");
2049 partexpr_item =
lnext(partexprs, partexpr_item);
2065 partcoll = partcollation->
values[keyno];
2066 if (!attrsOnly &&
OidIsValid(partcoll) && partcoll != keycolcollation)
2101 if (constr_expr == NULL)
2110 false, prettyFlags, 0);
2188 int prettyFlags,
bool missing_ok)
2199 Anum_pg_constraint_oid,
2204 ConstraintOidIndexId,
2226 elog(
ERROR,
"could not find tuple for constraint %u", constraintId);
2258 switch (conForm->contype)
2260 case CONSTRAINT_FOREIGN:
2271 Anum_pg_constraint_conkey);
2282 Anum_pg_constraint_confkey);
2289 switch (conForm->confmatchtype)
2292 string =
" MATCH FULL";
2295 string =
" MATCH PARTIAL";
2301 elog(
ERROR,
"unrecognized confmatchtype: %d",
2302 conForm->confmatchtype);
2309 switch (conForm->confupdtype)
2315 string =
"RESTRICT";
2321 string =
"SET NULL";
2324 string =
"SET DEFAULT";
2327 elog(
ERROR,
"unrecognized confupdtype: %d",
2328 conForm->confupdtype);
2335 switch (conForm->confdeltype)
2341 string =
"RESTRICT";
2347 string =
"SET NULL";
2350 string =
"SET DEFAULT";
2353 elog(
ERROR,
"unrecognized confdeltype: %d",
2354 conForm->confdeltype);
2366 Anum_pg_constraint_confdelsetcols, &isnull);
2376 case CONSTRAINT_PRIMARY:
2377 case CONSTRAINT_UNIQUE:
2385 if (conForm->contype == CONSTRAINT_PRIMARY)
2390 indexId = conForm->conindid;
2394 elog(
ERROR,
"cache lookup failed for index %u", indexId);
2395 if (conForm->contype == CONSTRAINT_UNIQUE &&
2403 Anum_pg_constraint_conkey);
2411 Anum_pg_index_indnatts);
2422 Anum_pg_index_indkey);
2425 &keys, NULL, &nKeys);
2427 for (
j = keyatts;
j < nKeys;
j++)
2468 case CONSTRAINT_CHECK:
2478 Anum_pg_constraint_conbin);
2513 conForm->connoinherit ?
" NO INHERIT" :
"");
2516 case CONSTRAINT_TRIGGER:
2526 case CONSTRAINT_EXCLUSION:
2528 Oid indexOid = conForm->conindid;
2537 Anum_pg_constraint_conexclop);
2540 &elems, NULL, &nElems);
2543 for (
i = 0;
i < nElems;
i++)
2561 elog(
ERROR,
"invalid constraint type \"%c\"", conForm->contype);
2565 if (conForm->condeferrable)
2567 if (conForm->condeferred)
2569 if (!conForm->convalidated)
2595 &keys, NULL, &nKeys);
2597 for (
j = 0;
j < nKeys;
j++)
2702 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2703 errmsg(
"input is a query, not an expression")));
2714 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2715 errmsg(
"expression contains variables of more than one relation")));
2721 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2722 errmsg(
"expression contains variables")));
2778 *result = role_rec->rolname;
2820 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2821 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
2828 Anum_pg_depend_refclassid,
2832 Anum_pg_depend_refobjid,
2836 Anum_pg_depend_refobjsubid,
2852 if (deprec->classid == RelationRelationId &&
2853 deprec->objsubid == 0 &&
2858 sequenceId = deprec->objid;
2916 if (proc->prokind == PROKIND_AGGREGATE)
2918 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2919 errmsg(
"\"%s\" is an aggregate function",
name)));
2921 isfunction = (proc->prokind != PROKIND_PROCEDURE);
2929 isfunction ?
"FUNCTION" :
"PROCEDURE",
2948 if (proc->prokind == PROKIND_WINDOW)
2950 switch (proc->provolatile)
2952 case PROVOLATILE_IMMUTABLE:
2955 case PROVOLATILE_STABLE:
2958 case PROVOLATILE_VOLATILE:
2962 switch (proc->proparallel)
2964 case PROPARALLEL_SAFE:
2967 case PROPARALLEL_RESTRICTED:
2970 case PROPARALLEL_UNSAFE:
2974 if (proc->proisstrict)
2976 if (proc->prosecdef)
2978 if (proc->proleakproof)
2982 if (proc->prolang == INTERNALlanguageId ||
2983 proc->prolang == ClanguageId)
2987 if (proc->procost != procost)
2990 if (proc->prorows > 0 && proc->prorows != 1000)
2993 if (proc->prosupport)
3001 argtypes[0] = INTERNALOID;
3005 false, NULL,
false));
3008 if (oldlen !=
buf.len)
3012 tmp =
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
3037 pos = strchr(configitem,
'=');
3070 elog(
ERROR,
"invalid list syntax in proconfig item");
3072 foreach(lc, namelist)
3074 char *curname = (
char *)
lfirst(lc);
3077 if (
lnext(namelist, lc))
3089 (void)
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3090 if (proc->prolang == SQLlanguageId && !isnull)
3098 tmp =
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
3119 while (strstr(prosrc, dq.
data) != NULL)
3231 if (proc->proretset)
3245 if (proc->proretset)
3262 bool print_table_args,
bool print_defaults)
3269 int insertorderbyat = -1;
3278 &argtypes, &argnames, &argmodes);
3280 nlackdefaults = numargs;
3281 if (print_defaults && proc->pronargdefaults > 0)
3283 Datum proargdefaults;
3287 Anum_pg_proc_proargdefaults,
3296 nextargdefault =
list_head(argdefaults);
3298 nlackdefaults = proc->pronargs -
list_length(argdefaults);
3303 if (proc->prokind == PROKIND_AGGREGATE)
3310 elog(
ERROR,
"cache lookup failed for aggregate %u",
3313 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3314 insertorderbyat = agg->aggnumdirectargs;
3320 for (
i = 0;
i < numargs;
i++)
3322 Oid argtype = argtypes[
i];
3323 char *argname = argnames ? argnames[
i] : NULL;
3324 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
3325 const char *modename;
3336 if (proc->prokind == PROKIND_PROCEDURE)
3342 case PROARGMODE_INOUT:
3343 modename =
"INOUT ";
3346 case PROARGMODE_OUT:
3350 case PROARGMODE_VARIADIC:
3351 modename =
"VARIADIC ";
3354 case PROARGMODE_TABLE:
3359 elog(
ERROR,
"invalid parameter mode '%c'", argmode);
3367 if (print_table_args != (argmode == PROARGMODE_TABLE))
3370 if (argsprinted == insertorderbyat)
3376 else if (argsprinted)
3380 if (argname && argname[0])
3383 if (print_defaults && isinput && inputargno > nlackdefaults)
3387 Assert(nextargdefault != NULL);
3389 nextargdefault =
lnext(argdefaults, nextargdefault);
3397 if (argsprinted == insertorderbyat &&
i == numargs - 1)
3401 print_defaults =
false;
3412 || argmodes[nth] == PROARGMODE_IN
3413 || argmodes[nth] == PROARGMODE_INOUT
3414 || argmodes[nth] == PROARGMODE_VARIADIC);
3432 for (
i = 0;
i < ntypes;
i++)
3464 Datum proargdefaults;
3473 if (nth_arg < 1 || nth_arg > numargs || !
is_input_argument(nth_arg - 1, argmodes))
3480 for (
i = 0;
i < nth_arg;
i++)
3485 Anum_pg_proc_proargdefaults,
3503 nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3505 if (nth_default < 0 || nth_default >=
list_length(argdefaults))
3510 node =
list_nth(argdefaults, nth_default);
3531 &argtypes, &argnames, &argmodes);
3587 (void)
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3609 bool forceprefix,
bool showimplicit)
3612 showimplicit, 0, 0);
3636 bool forceprefix,
bool showimplicit,
3637 int prettyFlags,
int startIndent)
3644 context.namespaces = dpcontext;
3648 context.varprefix = forceprefix;
3649 context.prettyFlags = prettyFlags;
3651 context.indentLevel = startIndent;
3652 context.colNamesVisible =
true;
3682 rte->relkind = RELKIND_RELATION;
3685 rte->eref = rte->alias;
3686 rte->lateral =
false;
3688 rte->inFromCl =
true;
3740 Assert(crelid > 0 && crelid <= ntables);
3812 memset(&dpns, 0,
sizeof(dpns));
3859 names_hash =
hash_create(
"set_rtable_names names",
3865 foreach(lc, parent_namespaces)
3872 char *oldname = (
char *)
lfirst(lc2);
3874 if (oldname == NULL)
3887 foreach(lc, dpns->
rtable)
3900 else if (rte->alias)
3903 refname = rte->alias->aliasname;
3918 refname = rte->eref->aliasname;
3936 int refnamelen = strlen(refname);
3937 char *modname = (
char *)
palloc(refnamelen + 16);
3945 memcpy(modname, refname, refnamelen);
3983 List *parent_namespaces)
4110 if (
j->alias == NULL &&
j->usingClause)
4120 for (
int i = 0;
i < jrte->joinmergedcols;
i++)
4136 elog(
ERROR,
"unrecognized node type: %d",
4198 if (rte->alias == NULL)
4204 if (colname == NULL)
4208 if (leftattnos[
i] > 0)
4211 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4215 if (rightattnos[
i] > 0)
4218 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4256 foreach(lc,
j->usingClause)
4261 Assert(leftattnos[
i] != 0 && rightattnos[
i] != 0);
4269 if (rte->alias &&
i <
list_length(rte->alias->colnames))
4282 parentUsing =
lappend(parentUsing, colname);
4285 if (leftattnos[
i] > 0)
4288 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4292 if (rightattnos[
i] > 0)
4295 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4311 elog(
ERROR,
"unrecognized node type: %d",
4327 char **real_colnames;
4347 ncolumns = tupdesc->
natts;
4348 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4350 for (
i = 0;
i < ncolumns;
i++)
4354 if (attr->attisdropped)
4355 real_colnames[
i] = NULL;
4389 colnames = rte->eref->colnames;
4392 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4395 foreach(lc, colnames)
4403 if (
cname[0] ==
'\0')
4405 real_colnames[
i] =
cname;
4442 changed_any =
false;
4444 for (
i = 0;
i < ncolumns;
i++)
4446 char *real_colname = real_colnames[
i];
4450 if (real_colname == NULL)
4457 if (colname == NULL)
4460 if (rte->alias &&
i <
list_length(rte->alias->colnames))
4463 colname = real_colname;
4479 if (!changed_any && strcmp(colname, real_colname) != 0)
4510 else if (rte->alias && rte->alias->colnames !=
NIL)
4565 changed_any =
false;
4586 if (real_colname == NULL)
4593 if (rte->alias == NULL)
4601 if (colname == NULL)
4604 if (rte->alias &&
i <
list_length(rte->alias->colnames))
4607 colname = real_colname;
4617 if (!changed_any && strcmp(colname, real_colname) != 0)
4652 while (
i < noldcolumns &&
4678 while (ic < leftcolinfo->num_cols &&
4681 Assert(ic < leftcolinfo->num_cols);
4687 while (i < colinfo->num_cols &&
4690 Assert(i < colinfo->num_cols);
4702 if (rte->alias != NULL)
4728 while (ic < rightcolinfo->num_cols &&
4729 rightcolinfo->
colnames[ic] == NULL)
4731 Assert(ic < rightcolinfo->num_cols);
4737 while (i < colinfo->num_cols &&
4740 Assert(i < colinfo->num_cols);
4752 if (rte->alias != NULL)
4770 #ifdef USE_ASSERT_CHECKING
4771 while (i < colinfo->num_cols && colinfo->
colnames[
i] == NULL)
4784 if (rte->alias != NULL)
4821 if (oldname && strcmp(oldname, colname) == 0)
4833 if (oldname && strcmp(oldname, colname) == 0)
4843 char *oldname = (
char *)
lfirst(lc);
4845 if (strcmp(oldname, colname) == 0)
4856 char *oldname = (
char *)
lfirst(lc);
4858 if (strcmp(oldname, colname) == 0)
4881 int colnamelen = strlen(colname);
4882 char *modname = (
char *)
palloc(colnamelen + 16);
4890 memcpy(modname, colname, colnamelen);
4891 sprintf(modname + colnamelen,
"_%d",
i);
4974 char *oldname = (
char *)
lfirst(lc);
5027 elog(
ERROR,
"unrecognized node type in jointree: %d",
5034 elog(
ERROR,
"unrecognized node type in jointree: %d",
5055 foreach(lc, jrte->joinleftcols)
5062 foreach(lc, jrte->joinrightcols)
5066 if (rcolno < jrte->joinmergedcols)
5072 Assert(jcolno == numjoincols);
5193 elog(
ERROR,
"could not find RecursiveUnion for WorkTableScan with wtParam %d",
5340 Assert(ev_action != NULL);
5343 elog(
ERROR,
"invalid empty ev_action list");
5380 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5381 errmsg(
"rule \"%s\" has unsupported event type %d",
5382 rulename, ev_type)));
5393 if (strcmp(ev_qual,
"<>") != 0)
5429 context.prettyFlags = prettyFlags;
5432 context.colNamesVisible =
true;
5488 int prettyFlags,
int wrapColumn)