64 #include "utils/fmgroids.h"
83 #define PRETTYINDENT_STD 8
84 #define PRETTYINDENT_JOIN 4
85 #define PRETTYINDENT_VAR 4
87 #define PRETTYINDENT_LIMIT 40
90 #define PRETTYFLAG_PAREN 0x0001
91 #define PRETTYFLAG_INDENT 0x0002
92 #define PRETTYFLAG_SCHEMA 0x0004
95 #define GET_PRETTY_FLAGS(pretty) \
96 ((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \
100 #define WRAP_COLUMN_DEFAULT 0
103 #define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)
104 #define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)
105 #define PRETTY_SCHEMA(context) ((context)->prettyFlags & PRETTYFLAG_SCHEMA)
297 #define deparse_columns_fetch(rangetable_index, dpns) \
298 ((deparse_columns *) list_nth((dpns)->rtable_columns, (rangetable_index)-1))
321 static const char *
const query_getviewrule =
"SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";
336 bool forceprefix,
bool showimplicit,
337 int prettyFlags,
int startIndent);
339 int prettyFlags,
int wrapColumn);
345 const Oid *excludeOps,
346 bool attrsOnly,
bool keysOnly,
347 bool showTblSpc,
bool inherits,
348 int prettyFlags,
bool missing_ok);
352 bool attrsOnly,
bool missing_ok);
354 int prettyFlags,
bool missing_ok);
358 bool print_table_args,
bool print_defaults);
365 List *parent_namespaces);
397 int prettyFlags,
int wrapColumn);
399 TupleDesc resultDesc,
bool colNamesVisible,
400 int prettyFlags,
int wrapColumn,
int startIndent);
404 TupleDesc resultDesc,
bool colNamesVisible);
406 bool colNamesVisible);
408 bool colNamesVisible);
413 bool colNamesVisible);
415 bool colNamesVisible);
418 TupleDesc resultDesc,
bool colNamesVisible);
420 TupleDesc resultDesc,
bool colNamesVisible);
423 TupleDesc resultDesc,
bool colNamesVisible);
446 int indentBefore,
int indentAfter,
int indentPlus);
464 const char *
options,
bool is_json_objectagg);
470 bool is_json_objectagg);
486 bool is_json_objectagg);
513 bool has_variadic,
bool *use_variadic_p,
522 #define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
599 argtypes[0] = OIDOID;
614 elog(
ERROR,
"failed to get pg_rewrite tuple for rule %u", ruleoid);
792 argtypes[0] = OIDOID;
793 argtypes[1] = NAMEOID;
810 elog(
ERROR,
"failed to get pg_rewrite tuple for view %u", viewoid);
919 tgname =
NameStr(trigrec->tgname);
921 OidIsValid(trigrec->tgconstraint) ?
"CONSTRAINT " :
"",
924 if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
926 else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
928 else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
931 elog(
ERROR,
"unexpected tgtype value: %d", trigrec->tgtype);
933 if (TRIGGER_FOR_INSERT(trigrec->tgtype))
938 if (TRIGGER_FOR_DELETE(trigrec->tgtype))
946 if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
954 if (trigrec->tgattr.dim1 > 0)
959 for (
i = 0;
i < trigrec->tgattr.dim1;
i++)
966 trigrec->tgattr.values[
i],
false);
971 if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
994 if (!trigrec->tgdeferrable)
997 if (trigrec->tginitdeferred)
1015 if (tgoldtable != NULL || tgnewtable != NULL)
1018 if (tgoldtable != NULL)
1021 if (tgnewtable != NULL)
1026 if (TRIGGER_FOR_ROW(trigrec->tgtype))
1052 oldrte->
relid = trigrec->tgrelid;
1058 oldrte->
inh =
false;
1063 newrte->
relid = trigrec->tgrelid;
1069 newrte->
inh =
false;
1073 memset(&dpns, 0,
sizeof(dpns));
1103 if (trigrec->tgnargs > 0)
1111 elog(
ERROR,
"tgargs is null for trigger %u", trigid);
1113 for (
i = 0;
i < trigrec->tgnargs;
i++)
1215 prettyFlags,
false);
1231 prettyFlags,
false);
1242 const Oid *excludeOps,
1243 bool attrsOnly,
bool keysOnly,
1244 bool showTblSpc,
bool inherits,
1245 int prettyFlags,
bool missing_ok)
1248 bool isConstraint = (excludeOps != NULL);
1262 Datum indclassDatum;
1263 Datum indoptionDatum;
1279 elog(
ERROR,
"cache lookup failed for index %u", indexrelid);
1283 indrelid = idxrec->indrelid;
1284 Assert(indexrelid == idxrec->indexrelid);
1288 Anum_pg_index_indcollation);
1292 Anum_pg_index_indclass);
1296 Anum_pg_index_indoption);
1304 elog(
ERROR,
"cache lookup failed for relation %u", indexrelid);
1312 elog(
ERROR,
"cache lookup failed for access method %u",
1330 Anum_pg_index_indexprs);
1352 idxrec->indisunique ?
"UNIQUE " :
"",
1354 idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
1355 && !inherits ?
"ONLY " :
"",
1369 for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1373 Oid keycolcollation;
1378 if (keysOnly && keyno >= idxrec->indnkeyatts)
1382 if (!colno && keyno == idxrec->indnkeyatts)
1399 if (!colno || colno == keyno + 1)
1402 &keycoltype, &keycoltypmod,
1410 if (indexpr_item == NULL)
1411 elog(
ERROR,
"too few entries in indexprs list");
1413 indexpr_item =
lnext(indexprs, indexpr_item);
1417 if (!colno || colno == keyno + 1)
1430 if (!attrsOnly && keyno < idxrec->indnkeyatts &&
1431 (!colno || colno == keyno + 1))
1434 Oid indcoll = indcollation->
values[keyno];
1436 bool has_options = attoptions != (
Datum) 0;
1439 if (
OidIsValid(indcoll) && indcoll != keycolcollation)
1458 if (opt & INDOPTION_DESC)
1462 if (!(opt & INDOPTION_NULLS_FIRST))
1467 if (opt & INDOPTION_NULLS_FIRST)
1473 if (excludeOps != NULL)
1485 if (idxrec->indnullsnotdistinct)
1526 Anum_pg_index_indpred);
1635 bool ndistinct_enabled;
1636 bool dependencies_enabled;
1651 elog(
ERROR,
"cache lookup failed for statistics object %u", statextid);
1655 has_exprs = !
heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1670 Anum_pg_statistic_ext_stxexprs);
1679 ncolumns = statextrec->stxkeys.dim1 +
list_length(exprs);
1688 NameStr(statextrec->stxname)));
1695 Anum_pg_statistic_ext_stxkind);
1700 elog(
ERROR,
"stxkind is not a 1-D char array");
1703 ndistinct_enabled =
false;
1704 dependencies_enabled =
false;
1705 mcv_enabled =
false;
1709 if (enabled[
i] == STATS_EXT_NDISTINCT)
1710 ndistinct_enabled =
true;
1711 else if (enabled[
i] == STATS_EXT_DEPENDENCIES)
1712 dependencies_enabled =
true;
1713 else if (enabled[
i] == STATS_EXT_MCV)
1730 if ((!ndistinct_enabled || !dependencies_enabled || !mcv_enabled) &&
1733 bool gotone =
false;
1737 if (ndistinct_enabled)
1743 if (dependencies_enabled)
1759 for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
1773 statextrec->stxrelid);
1828 has_exprs = !
heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1843 Anum_pg_statistic_ext_stxexprs);
1849 statextrec->stxrelid);
1909 bool attrsOnly,
bool missing_ok)
1929 elog(
ERROR,
"cache lookup failed for partition key of %u", relid);
1934 Assert(form->partrelid == relid);
1938 Anum_pg_partitioned_table_partclass);
1942 Anum_pg_partitioned_table_partcollation);
1951 if (!
heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
1957 Anum_pg_partitioned_table_partexprs);
1962 elog(
ERROR,
"unexpected node type found in partexprs: %d",
1975 switch (form->partstrat)
1990 elog(
ERROR,
"unexpected partition strategy: %d",
1991 (
int) form->partstrat);
1997 for (keyno = 0; keyno < form->partnatts; keyno++)
2001 Oid keycolcollation;
2015 &keycoltype, &keycoltypmod,
2023 if (partexpr_item == NULL)
2024 elog(
ERROR,
"too few entries in partexprs list");
2026 partexpr_item =
lnext(partexprs, partexpr_item);
2042 partcoll = partcollation->
values[keyno];
2043 if (!attrsOnly &&
OidIsValid(partcoll) && partcoll != keycolcollation)
2078 if (constr_expr == NULL)
2087 false, prettyFlags, 0);
2165 int prettyFlags,
bool missing_ok)
2176 Anum_pg_constraint_oid,
2181 ConstraintOidIndexId,
2203 elog(
ERROR,
"could not find tuple for constraint %u", constraintId);
2235 switch (conForm->contype)
2237 case CONSTRAINT_FOREIGN:
2248 Anum_pg_constraint_conkey);
2259 Anum_pg_constraint_confkey);
2266 switch (conForm->confmatchtype)
2269 string =
" MATCH FULL";
2272 string =
" MATCH PARTIAL";
2278 elog(
ERROR,
"unrecognized confmatchtype: %d",
2279 conForm->confmatchtype);
2286 switch (conForm->confupdtype)
2292 string =
"RESTRICT";
2298 string =
"SET NULL";
2301 string =
"SET DEFAULT";
2304 elog(
ERROR,
"unrecognized confupdtype: %d",
2305 conForm->confupdtype);
2312 switch (conForm->confdeltype)
2318 string =
"RESTRICT";
2324 string =
"SET NULL";
2327 string =
"SET DEFAULT";
2330 elog(
ERROR,
"unrecognized confdeltype: %d",
2331 conForm->confdeltype);
2343 Anum_pg_constraint_confdelsetcols, &isnull);
2353 case CONSTRAINT_PRIMARY:
2354 case CONSTRAINT_UNIQUE:
2362 if (conForm->contype == CONSTRAINT_PRIMARY)
2367 indexId = conForm->conindid;
2371 elog(
ERROR,
"cache lookup failed for index %u", indexId);
2372 if (conForm->contype == CONSTRAINT_UNIQUE &&
2380 Anum_pg_constraint_conkey);
2388 Anum_pg_index_indnatts);
2399 Anum_pg_index_indkey);
2402 &keys, NULL, &nKeys);
2404 for (
j = keyatts;
j < nKeys;
j++)
2445 case CONSTRAINT_CHECK:
2455 Anum_pg_constraint_conbin);
2490 conForm->connoinherit ?
" NO INHERIT" :
"");
2493 case CONSTRAINT_NOTNULL:
2507 case CONSTRAINT_TRIGGER:
2517 case CONSTRAINT_EXCLUSION:
2519 Oid indexOid = conForm->conindid;
2528 Anum_pg_constraint_conexclop);
2531 &elems, NULL, &nElems);
2534 for (
i = 0;
i < nElems;
i++)
2552 elog(
ERROR,
"invalid constraint type \"%c\"", conForm->contype);
2556 if (conForm->condeferrable)
2558 if (conForm->condeferred)
2560 if (!conForm->convalidated)
2586 &keys, NULL, &nKeys);
2588 for (
j = 0;
j < nKeys;
j++)
2707 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2708 errmsg(
"input is a query, not an expression")));
2719 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2720 errmsg(
"expression contains variables of more than one relation")));
2726 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2727 errmsg(
"expression contains variables")));
2770 *result = role_rec->rolname;
2812 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2813 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
2820 Anum_pg_depend_refclassid,
2824 Anum_pg_depend_refobjid,
2828 Anum_pg_depend_refobjsubid,
2844 if (deprec->classid == RelationRelationId &&
2845 deprec->objsubid == 0 &&
2850 sequenceId = deprec->objid;
2908 if (proc->prokind == PROKIND_AGGREGATE)
2910 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2911 errmsg(
"\"%s\" is an aggregate function",
name)));
2913 isfunction = (proc->prokind != PROKIND_PROCEDURE);
2921 isfunction ?
"FUNCTION" :
"PROCEDURE",
2940 if (proc->prokind == PROKIND_WINDOW)
2942 switch (proc->provolatile)
2944 case PROVOLATILE_IMMUTABLE:
2947 case PROVOLATILE_STABLE:
2950 case PROVOLATILE_VOLATILE:
2954 switch (proc->proparallel)
2956 case PROPARALLEL_SAFE:
2959 case PROPARALLEL_RESTRICTED:
2962 case PROPARALLEL_UNSAFE:
2966 if (proc->proisstrict)
2968 if (proc->prosecdef)
2970 if (proc->proleakproof)
2974 if (proc->prolang == INTERNALlanguageId ||
2975 proc->prolang == ClanguageId)
2979 if (proc->procost != procost)
2982 if (proc->prorows > 0 && proc->prorows != 1000)
2985 if (proc->prosupport)
2993 argtypes[0] = INTERNALOID;
3000 if (oldlen !=
buf.len)
3029 pos = strchr(configitem,
'=');
3062 elog(
ERROR,
"invalid list syntax in proconfig item");
3064 foreach(lc, namelist)
3066 char *curname = (
char *)
lfirst(lc);
3069 if (
lnext(namelist, lc))
3082 if (proc->prolang == SQLlanguageId && !isnull)
3111 while (strstr(prosrc, dq.
data) != NULL)
3223 if (proc->proretset)
3237 if (proc->proretset)
3254 bool print_table_args,
bool print_defaults)
3261 int insertorderbyat = -1;
3270 &argtypes, &argnames, &argmodes);
3272 nlackdefaults = numargs;
3273 if (print_defaults && proc->pronargdefaults > 0)
3275 Datum proargdefaults;
3279 Anum_pg_proc_proargdefaults,
3288 nextargdefault =
list_head(argdefaults);
3290 nlackdefaults = proc->pronargs -
list_length(argdefaults);
3295 if (proc->prokind == PROKIND_AGGREGATE)
3302 elog(
ERROR,
"cache lookup failed for aggregate %u",
3305 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3306 insertorderbyat = agg->aggnumdirectargs;
3312 for (
i = 0;
i < numargs;
i++)
3314 Oid argtype = argtypes[
i];
3315 char *argname = argnames ? argnames[
i] : NULL;
3316 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
3317 const char *modename;
3328 if (proc->prokind == PROKIND_PROCEDURE)
3334 case PROARGMODE_INOUT:
3335 modename =
"INOUT ";
3338 case PROARGMODE_OUT:
3342 case PROARGMODE_VARIADIC:
3343 modename =
"VARIADIC ";
3346 case PROARGMODE_TABLE:
3351 elog(
ERROR,
"invalid parameter mode '%c'", argmode);
3359 if (print_table_args != (argmode == PROARGMODE_TABLE))
3362 if (argsprinted == insertorderbyat)
3368 else if (argsprinted)
3372 if (argname && argname[0])
3375 if (print_defaults && isinput && inputargno > nlackdefaults)
3379 Assert(nextargdefault != NULL);
3381 nextargdefault =
lnext(argdefaults, nextargdefault);
3389 if (argsprinted == insertorderbyat &&
i == numargs - 1)
3393 print_defaults =
false;
3404 || argmodes[nth] == PROARGMODE_IN
3405 || argmodes[nth] == PROARGMODE_INOUT
3406 || argmodes[nth] == PROARGMODE_VARIADIC);
3424 for (
i = 0;
i < ntypes;
i++)
3456 Datum proargdefaults;
3465 if (nth_arg < 1 || nth_arg > numargs || !
is_input_argument(nth_arg - 1, argmodes))
3472 for (
i = 0;
i < nth_arg;
i++)
3477 Anum_pg_proc_proargdefaults,
3495 nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3497 if (nth_default < 0 || nth_default >=
list_length(argdefaults))
3502 node =
list_nth(argdefaults, nth_default);
3523 &argtypes, &argnames, &argmodes);
3601 bool forceprefix,
bool showimplicit)
3604 showimplicit, 0, 0);
3628 bool forceprefix,
bool showimplicit,
3629 int prettyFlags,
int startIndent)
3671 rte->
relkind = RELKIND_RELATION;
3729 Assert(crelid > 0 && crelid <= ntables);
3801 memset(&dpns, 0,
sizeof(dpns));
3848 names_hash =
hash_create(
"set_rtable_names names",
3854 foreach(lc, parent_namespaces)
3861 char *oldname = (
char *)
lfirst(lc2);
3863 if (oldname == NULL)
3876 foreach(lc, dpns->
rtable)
3889 else if (rte->
alias)
3925 int refnamelen = strlen(refname);
3926 char *modname = (
char *)
palloc(refnamelen + 16);
3934 memcpy(modname, refname, refnamelen);
3972 List *parent_namespaces)
4099 if (
j->alias == NULL &&
j->usingClause)
4125 elog(
ERROR,
"unrecognized node type: %d",
4183 if (rte->
alias == NULL)
4189 if (colname == NULL)
4193 if (leftattnos[
i] > 0)
4196 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4200 if (rightattnos[
i] > 0)
4203 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4241 foreach(lc,
j->usingClause)
4246 Assert(leftattnos[
i] != 0 && rightattnos[
i] != 0);
4267 parentUsing =
lappend(parentUsing, colname);
4270 if (leftattnos[
i] > 0)
4273 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4277 if (rightattnos[
i] > 0)
4280 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4296 elog(
ERROR,
"unrecognized node type: %d",
4312 char **real_colnames;
4332 ncolumns = tupdesc->
natts;
4333 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4335 for (
i = 0;
i < ncolumns;
i++)
4339 if (attr->attisdropped)
4340 real_colnames[
i] = NULL;
4377 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4380 foreach(lc, colnames)
4388 if (
cname[0] ==
'\0')
4390 real_colnames[
i] =
cname;
4424 changed_any =
false;
4426 for (
i = 0;
i < ncolumns;
i++)
4428 char *real_colname = real_colnames[
i];
4432 if (real_colname == NULL)
4439 if (colname == NULL)
4445 colname = real_colname;
4460 if (!changed_any && strcmp(colname, real_colname) != 0)
4540 changed_any =
false;
4561 if (real_colname == NULL)
4568 if (rte->
alias == NULL)
4575 if (colname == NULL)
4581 colname = real_colname;
4590 if (!changed_any && strcmp(colname, real_colname) != 0)
4625 while (
i < noldcolumns &&
4651 while (ic < leftcolinfo->num_cols &&
4654 Assert(ic < leftcolinfo->num_cols);
4660 while (i < colinfo->num_cols &&
4663 Assert(i < colinfo->num_cols);
4675 if (rte->
alias != NULL)
4700 while (ic < rightcolinfo->num_cols &&
4701 rightcolinfo->
colnames[ic] == NULL)
4703 Assert(ic < rightcolinfo->num_cols);
4709 while (i < colinfo->num_cols &&
4712 Assert(i < colinfo->num_cols);
4724 if (rte->
alias != NULL)
4741 #ifdef USE_ASSERT_CHECKING
4742 while (i < colinfo->num_cols && colinfo->
colnames[
i] == NULL)
4752 if (rte->
alias != NULL)
4775 if (oldname && strcmp(oldname, colname) == 0)
4787 if (oldname && strcmp(oldname, colname) == 0)
4794 char *oldname = (
char *)
lfirst(lc);
4796 if (strcmp(oldname, colname) == 0)
4803 char *oldname = (
char *)
lfirst(lc);
4805 if (strcmp(oldname, colname) == 0)
4828 int colnamelen = strlen(colname);
4829 char *modname = (
char *)
palloc(colnamelen + 16);
4837 memcpy(modname, colname, colnamelen);
4838 sprintf(modname + colnamelen,
"_%d",
i);
4890 elog(
ERROR,
"unrecognized node type in jointree: %d",
4897 elog(
ERROR,
"unrecognized node type in jointree: %d",
4929 if (rcolno < jrte->joinmergedcols)
4935 Assert(jcolno == numjoincols);
5053 elog(
ERROR,
"could not find RecursiveUnion for WorkTableScan with wtParam %d",
5200 Assert(ev_action != NULL);
5203 elog(
ERROR,
"invalid empty ev_action list");
5240 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5241 errmsg(
"rule \"%s\" has unsupported event type %d",
5242 rulename, ev_type)));
5253 if (strcmp(ev_qual,
"<>") != 0)
5345 int prettyFlags,
int wrapColumn)
5383 Assert(ev_action != NULL);
5394 if (ev_type !=
'1' || !is_instead ||
5404 prettyFlags, wrapColumn, 0);
5430 TupleDesc resultDesc,
bool colNamesVisible,
5431 int prettyFlags,
int wrapColumn,
int startIndent)