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);
2283 Anum_pg_constraint_confkey);
2290 switch (conForm->confmatchtype)
2293 string =
" MATCH FULL";
2296 string =
" MATCH PARTIAL";
2302 elog(
ERROR,
"unrecognized confmatchtype: %d",
2303 conForm->confmatchtype);
2310 switch (conForm->confupdtype)
2316 string =
"RESTRICT";
2322 string =
"SET NULL";
2325 string =
"SET DEFAULT";
2328 elog(
ERROR,
"unrecognized confupdtype: %d",
2329 conForm->confupdtype);
2336 switch (conForm->confdeltype)
2342 string =
"RESTRICT";
2348 string =
"SET NULL";
2351 string =
"SET DEFAULT";
2354 elog(
ERROR,
"unrecognized confdeltype: %d",
2355 conForm->confdeltype);
2367 Anum_pg_constraint_confdelsetcols, &isnull);
2377 case CONSTRAINT_PRIMARY:
2378 case CONSTRAINT_UNIQUE:
2386 if (conForm->contype == CONSTRAINT_PRIMARY)
2391 indexId = conForm->conindid;
2395 elog(
ERROR,
"cache lookup failed for index %u", indexId);
2396 if (conForm->contype == CONSTRAINT_UNIQUE &&
2404 Anum_pg_constraint_conkey);
2407 if (conForm->conperiod)
2414 Anum_pg_index_indnatts);
2425 Anum_pg_index_indkey);
2428 &keys, NULL, &nKeys);
2430 for (
j = keyatts;
j < nKeys;
j++)
2471 case CONSTRAINT_CHECK:
2481 Anum_pg_constraint_conbin);
2516 conForm->connoinherit ?
" NO INHERIT" :
"");
2519 case CONSTRAINT_NOTNULL:
2521 if (conForm->conrelid)
2533 else if (conForm->contypid)
2541 case CONSTRAINT_TRIGGER:
2551 case CONSTRAINT_EXCLUSION:
2553 Oid indexOid = conForm->conindid;
2562 Anum_pg_constraint_conexclop);
2565 &elems, NULL, &nElems);
2568 for (
i = 0;
i < nElems;
i++)
2586 elog(
ERROR,
"invalid constraint type \"%c\"", conForm->contype);
2590 if (conForm->condeferrable)
2592 if (conForm->condeferred)
2594 if (!conForm->convalidated)
2620 &keys, NULL, &nKeys);
2622 for (
j = 0;
j < nKeys;
j++)
2632 (withPeriod &&
j == nKeys - 1) ?
"PERIOD " :
"",
2729 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2730 errmsg(
"input is a query, not an expression")));
2741 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2742 errmsg(
"expression contains variables of more than one relation")));
2748 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2749 errmsg(
"expression contains variables")));
2805 *result = role_rec->rolname;
2847 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2848 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
2855 Anum_pg_depend_refclassid,
2859 Anum_pg_depend_refobjid,
2863 Anum_pg_depend_refobjsubid,
2879 if (deprec->classid == RelationRelationId &&
2880 deprec->objsubid == 0 &&
2885 sequenceId = deprec->objid;
2943 if (proc->prokind == PROKIND_AGGREGATE)
2945 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2946 errmsg(
"\"%s\" is an aggregate function",
name)));
2948 isfunction = (proc->prokind != PROKIND_PROCEDURE);
2956 isfunction ?
"FUNCTION" :
"PROCEDURE",
2975 if (proc->prokind == PROKIND_WINDOW)
2977 switch (proc->provolatile)
2979 case PROVOLATILE_IMMUTABLE:
2982 case PROVOLATILE_STABLE:
2985 case PROVOLATILE_VOLATILE:
2989 switch (proc->proparallel)
2991 case PROPARALLEL_SAFE:
2994 case PROPARALLEL_RESTRICTED:
2997 case PROPARALLEL_UNSAFE:
3001 if (proc->proisstrict)
3003 if (proc->prosecdef)
3005 if (proc->proleakproof)
3009 if (proc->prolang == INTERNALlanguageId ||
3010 proc->prolang == ClanguageId)
3014 if (proc->procost != procost)
3017 if (proc->prorows > 0 && proc->prorows != 1000)
3020 if (proc->prosupport)
3028 argtypes[0] = INTERNALOID;
3032 false, NULL,
false));
3035 if (oldlen !=
buf.len)
3039 tmp =
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proconfig, &isnull);
3064 pos = strchr(configitem,
'=');
3097 elog(
ERROR,
"invalid list syntax in proconfig item");
3099 foreach(lc, namelist)
3101 char *curname = (
char *)
lfirst(lc);
3104 if (
lnext(namelist, lc))
3116 (void)
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3117 if (proc->prolang == SQLlanguageId && !isnull)
3125 tmp =
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_probin, &isnull);
3146 while (strstr(prosrc, dq.
data) != NULL)
3258 if (proc->proretset)
3272 if (proc->proretset)
3289 bool print_table_args,
bool print_defaults)
3296 int insertorderbyat = -1;
3305 &argtypes, &argnames, &argmodes);
3307 nlackdefaults = numargs;
3308 if (print_defaults && proc->pronargdefaults > 0)
3310 Datum proargdefaults;
3314 Anum_pg_proc_proargdefaults,
3323 nextargdefault =
list_head(argdefaults);
3325 nlackdefaults = proc->pronargs -
list_length(argdefaults);
3330 if (proc->prokind == PROKIND_AGGREGATE)
3337 elog(
ERROR,
"cache lookup failed for aggregate %u",
3340 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3341 insertorderbyat = agg->aggnumdirectargs;
3347 for (
i = 0;
i < numargs;
i++)
3349 Oid argtype = argtypes[
i];
3350 char *argname = argnames ? argnames[
i] : NULL;
3351 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
3352 const char *modename;
3363 if (proc->prokind == PROKIND_PROCEDURE)
3369 case PROARGMODE_INOUT:
3370 modename =
"INOUT ";
3373 case PROARGMODE_OUT:
3377 case PROARGMODE_VARIADIC:
3378 modename =
"VARIADIC ";
3381 case PROARGMODE_TABLE:
3386 elog(
ERROR,
"invalid parameter mode '%c'", argmode);
3394 if (print_table_args != (argmode == PROARGMODE_TABLE))
3397 if (argsprinted == insertorderbyat)
3403 else if (argsprinted)
3407 if (argname && argname[0])
3410 if (print_defaults && isinput && inputargno > nlackdefaults)
3414 Assert(nextargdefault != NULL);
3416 nextargdefault =
lnext(argdefaults, nextargdefault);
3424 if (argsprinted == insertorderbyat &&
i == numargs - 1)
3428 print_defaults =
false;
3439 || argmodes[nth] == PROARGMODE_IN
3440 || argmodes[nth] == PROARGMODE_INOUT
3441 || argmodes[nth] == PROARGMODE_VARIADIC);
3459 for (
i = 0;
i < ntypes;
i++)
3491 Datum proargdefaults;
3500 if (nth_arg < 1 || nth_arg > numargs || !
is_input_argument(nth_arg - 1, argmodes))
3507 for (
i = 0;
i < nth_arg;
i++)
3512 Anum_pg_proc_proargdefaults,
3530 nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3532 if (nth_default < 0 || nth_default >=
list_length(argdefaults))
3537 node =
list_nth(argdefaults, nth_default);
3558 &argtypes, &argnames, &argmodes);
3614 (void)
SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosqlbody, &isnull);
3636 bool forceprefix,
bool showimplicit)
3639 showimplicit, 0, 0);
3663 bool forceprefix,
bool showimplicit,
3664 int prettyFlags,
int startIndent)
3671 context.namespaces = dpcontext;
3675 context.varprefix = forceprefix;
3676 context.prettyFlags = prettyFlags;
3678 context.indentLevel = startIndent;
3679 context.colNamesVisible =
true;
3709 rte->relkind = RELKIND_RELATION;
3712 rte->eref = rte->alias;
3713 rte->lateral =
false;
3715 rte->inFromCl =
true;
3767 Assert(crelid > 0 && crelid <= ntables);
3839 memset(&dpns, 0,
sizeof(dpns));
3886 names_hash =
hash_create(
"set_rtable_names names",
3892 foreach(lc, parent_namespaces)
3899 char *oldname = (
char *)
lfirst(lc2);
3901 if (oldname == NULL)
3914 foreach(lc, dpns->
rtable)
3927 else if (rte->alias)
3930 refname = rte->alias->aliasname;
3945 refname = rte->eref->aliasname;
3963 int refnamelen = strlen(refname);
3964 char *modname = (
char *)
palloc(refnamelen + 16);
3972 memcpy(modname, refname, refnamelen);
4010 List *parent_namespaces)
4137 if (
j->alias == NULL &&
j->usingClause)
4147 for (
int i = 0;
i < jrte->joinmergedcols;
i++)
4163 elog(
ERROR,
"unrecognized node type: %d",
4225 if (rte->alias == NULL)
4231 if (colname == NULL)
4235 if (leftattnos[
i] > 0)
4238 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4242 if (rightattnos[
i] > 0)
4245 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4283 foreach(lc,
j->usingClause)
4288 Assert(leftattnos[
i] != 0 && rightattnos[
i] != 0);
4296 if (rte->alias &&
i <
list_length(rte->alias->colnames))
4309 parentUsing =
lappend(parentUsing, colname);
4312 if (leftattnos[
i] > 0)
4315 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4319 if (rightattnos[
i] > 0)
4322 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4338 elog(
ERROR,
"unrecognized node type: %d",
4354 char **real_colnames;
4374 ncolumns = tupdesc->
natts;
4375 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4377 for (
i = 0;
i < ncolumns;
i++)
4381 if (attr->attisdropped)
4382 real_colnames[
i] = NULL;
4416 colnames = rte->eref->colnames;
4419 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4422 foreach(lc, colnames)
4430 if (
cname[0] ==
'\0')
4432 real_colnames[
i] =
cname;
4469 changed_any =
false;
4471 for (
i = 0;
i < ncolumns;
i++)
4473 char *real_colname = real_colnames[
i];
4477 if (real_colname == NULL)
4484 if (colname == NULL)
4487 if (rte->alias &&
i <
list_length(rte->alias->colnames))
4490 colname = real_colname;
4506 if (!changed_any && strcmp(colname, real_colname) != 0)
4537 else if (rte->alias && rte->alias->colnames !=
NIL)
4592 changed_any =
false;
4613 if (real_colname == NULL)
4620 if (rte->alias == NULL)
4628 if (colname == NULL)
4631 if (rte->alias &&
i <
list_length(rte->alias->colnames))
4634 colname = real_colname;
4644 if (!changed_any && strcmp(colname, real_colname) != 0)
4679 while (
i < noldcolumns &&
4705 while (ic < leftcolinfo->num_cols &&
4708 Assert(ic < leftcolinfo->num_cols);
4714 while (i < colinfo->num_cols &&
4717 Assert(i < colinfo->num_cols);
4729 if (rte->alias != NULL)
4755 while (ic < rightcolinfo->num_cols &&
4756 rightcolinfo->
colnames[ic] == NULL)
4758 Assert(ic < rightcolinfo->num_cols);
4764 while (i < colinfo->num_cols &&
4767 Assert(i < colinfo->num_cols);
4779 if (rte->alias != NULL)
4797 #ifdef USE_ASSERT_CHECKING
4798 while (i < colinfo->num_cols && colinfo->
colnames[
i] == NULL)
4811 if (rte->alias != NULL)
4848 if (oldname && strcmp(oldname, colname) == 0)
4860 if (oldname && strcmp(oldname, colname) == 0)
4870 char *oldname = (
char *)
lfirst(lc);
4872 if (strcmp(oldname, colname) == 0)
4883 char *oldname = (
char *)
lfirst(lc);
4885 if (strcmp(oldname, colname) == 0)
4908 int colnamelen = strlen(colname);
4909 char *modname = (
char *)
palloc(colnamelen + 16);
4917 memcpy(modname, colname, colnamelen);
4918 sprintf(modname + colnamelen,
"_%d",
i);
5001 char *oldname = (
char *)
lfirst(lc);
5054 elog(
ERROR,
"unrecognized node type in jointree: %d",
5061 elog(
ERROR,
"unrecognized node type in jointree: %d",
5082 foreach(lc, jrte->joinleftcols)
5089 foreach(lc, jrte->joinrightcols)
5093 if (rcolno < jrte->joinmergedcols)
5099 Assert(jcolno == numjoincols);
5220 elog(
ERROR,
"could not find RecursiveUnion for WorkTableScan with wtParam %d",
5367 Assert(ev_action != NULL);
5370 elog(
ERROR,
"invalid empty ev_action list");
5407 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5408 errmsg(
"rule \"%s\" has unsupported event type %d",
5409 rulename, ev_type)));
5420 if (strcmp(ev_qual,
"<>") != 0)
5456 context.prettyFlags = prettyFlags;
5459 context.colNamesVisible =
true;