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 *
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);
416 TupleDesc resultDesc,
bool colNamesVisible);
418 TupleDesc resultDesc,
bool colNamesVisible);
421 TupleDesc resultDesc,
bool colNamesVisible);
444 int indentBefore,
int indentAfter,
int indentPlus);
496 bool has_variadic,
bool *use_variadic_p,
505 #define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
582 argtypes[0] = OIDOID;
597 elog(
ERROR,
"failed to get pg_rewrite tuple for rule %u", ruleoid);
775 argtypes[0] = OIDOID;
776 argtypes[1] = NAMEOID;
793 elog(
ERROR,
"failed to get pg_rewrite tuple for view %u", viewoid);
902 tgname =
NameStr(trigrec->tgname);
904 OidIsValid(trigrec->tgconstraint) ?
"CONSTRAINT " :
"",
907 if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
909 else if (TRIGGER_FOR_AFTER(trigrec->tgtype))
911 else if (TRIGGER_FOR_INSTEAD(trigrec->tgtype))
914 elog(
ERROR,
"unexpected tgtype value: %d", trigrec->tgtype);
916 if (TRIGGER_FOR_INSERT(trigrec->tgtype))
921 if (TRIGGER_FOR_DELETE(trigrec->tgtype))
929 if (TRIGGER_FOR_UPDATE(trigrec->tgtype))
937 if (trigrec->tgattr.dim1 > 0)
942 for (
i = 0;
i < trigrec->tgattr.dim1;
i++)
949 trigrec->tgattr.values[
i],
false);
954 if (TRIGGER_FOR_TRUNCATE(trigrec->tgtype))
977 if (!trigrec->tgdeferrable)
980 if (trigrec->tginitdeferred)
998 if (tgoldtable != NULL || tgnewtable != NULL)
1001 if (tgoldtable != NULL)
1004 if (tgnewtable != NULL)
1009 if (TRIGGER_FOR_ROW(trigrec->tgtype))
1035 oldrte->
relid = trigrec->tgrelid;
1041 oldrte->
inh =
false;
1046 newrte->
relid = trigrec->tgrelid;
1052 newrte->
inh =
false;
1056 memset(&dpns, 0,
sizeof(dpns));
1086 if (trigrec->tgnargs > 0)
1094 elog(
ERROR,
"tgargs is null for trigger %u", trigid);
1096 for (
i = 0;
i < trigrec->tgnargs;
i++)
1198 prettyFlags,
false);
1209 const Oid *excludeOps,
1210 bool attrsOnly,
bool keysOnly,
1211 bool showTblSpc,
bool inherits,
1212 int prettyFlags,
bool missing_ok)
1215 bool isConstraint = (excludeOps != NULL);
1229 Datum indclassDatum;
1230 Datum indoptionDatum;
1247 elog(
ERROR,
"cache lookup failed for index %u", indexrelid);
1251 indrelid = idxrec->indrelid;
1252 Assert(indexrelid == idxrec->indexrelid);
1256 Anum_pg_index_indcollation, &isnull);
1261 Anum_pg_index_indclass, &isnull);
1266 Anum_pg_index_indoption, &isnull);
1275 elog(
ERROR,
"cache lookup failed for relation %u", indexrelid);
1283 elog(
ERROR,
"cache lookup failed for access method %u",
1301 Anum_pg_index_indexprs, &isnull);
1324 idxrec->indisunique ?
"UNIQUE " :
"",
1326 idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
1327 && !inherits ?
"ONLY " :
"",
1341 for (keyno = 0; keyno < idxrec->indnatts; keyno++)
1345 Oid keycolcollation;
1350 if (keysOnly && keyno >= idxrec->indnkeyatts)
1354 if (!colno && keyno == idxrec->indnkeyatts)
1371 if (!colno || colno == keyno + 1)
1374 &keycoltype, &keycoltypmod,
1382 if (indexpr_item == NULL)
1383 elog(
ERROR,
"too few entries in indexprs list");
1385 indexpr_item =
lnext(indexprs, indexpr_item);
1389 if (!colno || colno == keyno + 1)
1402 if (!attrsOnly && keyno < idxrec->indnkeyatts &&
1403 (!colno || colno == keyno + 1))
1406 Oid indcoll = indcollation->
values[keyno];
1408 bool has_options = attoptions != (
Datum) 0;
1411 if (
OidIsValid(indcoll) && indcoll != keycolcollation)
1430 if (opt & INDOPTION_DESC)
1434 if (!(opt & INDOPTION_NULLS_FIRST))
1439 if (opt & INDOPTION_NULLS_FIRST)
1445 if (excludeOps != NULL)
1457 if (idxrec->indnullsnotdistinct)
1498 Anum_pg_index_indpred, &isnull);
1608 bool ndistinct_enabled;
1609 bool dependencies_enabled;
1624 elog(
ERROR,
"cache lookup failed for statistics object %u", statextid);
1628 has_exprs = !
heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1644 Anum_pg_statistic_ext_stxexprs, &isnull);
1654 ncolumns = statextrec->stxkeys.dim1 +
list_length(exprs);
1665 NameStr(statextrec->stxname)));
1672 Anum_pg_statistic_ext_stxkind, &isnull);
1678 elog(
ERROR,
"stxkind is not a 1-D char array");
1681 ndistinct_enabled =
false;
1682 dependencies_enabled =
false;
1683 mcv_enabled =
false;
1687 if (enabled[
i] == STATS_EXT_NDISTINCT)
1688 ndistinct_enabled =
true;
1689 else if (enabled[
i] == STATS_EXT_DEPENDENCIES)
1690 dependencies_enabled =
true;
1691 else if (enabled[
i] == STATS_EXT_MCV)
1708 if ((!ndistinct_enabled || !dependencies_enabled || !mcv_enabled) &&
1711 bool gotone =
false;
1715 if (ndistinct_enabled)
1721 if (dependencies_enabled)
1737 for (colno = 0; colno < statextrec->stxkeys.dim1; colno++)
1751 statextrec->stxrelid);
1807 has_exprs = !
heap_attisnull(statexttup, Anum_pg_statistic_ext_stxexprs, NULL);
1822 Anum_pg_statistic_ext_stxexprs, &isnull);
1830 statextrec->stxrelid);
1890 bool attrsOnly,
bool missing_ok)
1911 elog(
ERROR,
"cache lookup failed for partition key of %u", relid);
1916 Assert(form->partrelid == relid);
1920 Anum_pg_partitioned_table_partclass, &isnull);
1925 Anum_pg_partitioned_table_partcollation, &isnull);
1935 if (!
heap_attisnull(tuple, Anum_pg_partitioned_table_partexprs, NULL))
1941 Anum_pg_partitioned_table_partexprs, &isnull);
1947 elog(
ERROR,
"unexpected node type found in partexprs: %d",
1960 switch (form->partstrat)
1975 elog(
ERROR,
"unexpected partition strategy: %d",
1976 (
int) form->partstrat);
1982 for (keyno = 0; keyno < form->partnatts; keyno++)
1986 Oid keycolcollation;
2000 &keycoltype, &keycoltypmod,
2008 if (partexpr_item == NULL)
2009 elog(
ERROR,
"too few entries in partexprs list");
2011 partexpr_item =
lnext(partexprs, partexpr_item);
2027 partcoll = partcollation->
values[keyno];
2028 if (!attrsOnly &&
OidIsValid(partcoll) && partcoll != keycolcollation)
2063 if (constr_expr == NULL)
2072 false, prettyFlags, 0);
2150 int prettyFlags,
bool missing_ok)
2161 Anum_pg_constraint_oid,
2166 ConstraintOidIndexId,
2188 elog(
ERROR,
"could not find tuple for constraint %u", constraintId);
2220 switch (conForm->contype)
2222 case CONSTRAINT_FOREIGN:
2233 Anum_pg_constraint_conkey, &isnull);
2235 elog(
ERROR,
"null conkey for constraint %u",
2247 Anum_pg_constraint_confkey, &isnull);
2249 elog(
ERROR,
"null confkey for constraint %u",
2257 switch (conForm->confmatchtype)
2260 string =
" MATCH FULL";
2263 string =
" MATCH PARTIAL";
2269 elog(
ERROR,
"unrecognized confmatchtype: %d",
2270 conForm->confmatchtype);
2277 switch (conForm->confupdtype)
2283 string =
"RESTRICT";
2289 string =
"SET NULL";
2292 string =
"SET DEFAULT";
2295 elog(
ERROR,
"unrecognized confupdtype: %d",
2296 conForm->confupdtype);
2303 switch (conForm->confdeltype)
2309 string =
"RESTRICT";
2315 string =
"SET NULL";
2318 string =
"SET DEFAULT";
2321 elog(
ERROR,
"unrecognized confdeltype: %d",
2322 conForm->confdeltype);
2334 Anum_pg_constraint_confdelsetcols, &isnull);
2344 case CONSTRAINT_PRIMARY:
2345 case CONSTRAINT_UNIQUE:
2354 if (conForm->contype == CONSTRAINT_PRIMARY)
2359 indexId = conForm->conindid;
2363 elog(
ERROR,
"cache lookup failed for index %u", indexId);
2364 if (conForm->contype == CONSTRAINT_UNIQUE &&
2372 Anum_pg_constraint_conkey, &isnull);
2374 elog(
ERROR,
"null conkey for constraint %u",
2383 Anum_pg_index_indnatts, &isnull);
2385 elog(
ERROR,
"null indnatts for index %u", indexId);
2396 Anum_pg_index_indkey, &isnull);
2398 elog(
ERROR,
"null indkey for index %u", indexId);
2401 &keys, NULL, &nKeys);
2403 for (
j = keyatts;
j < nKeys;
j++)
2444 case CONSTRAINT_CHECK:
2455 Anum_pg_constraint_conbin, &isnull);
2457 elog(
ERROR,
"null conbin for constraint %u",
2493 conForm->connoinherit ?
" NO INHERIT" :
"");
2496 case CONSTRAINT_TRIGGER:
2506 case CONSTRAINT_EXCLUSION:
2508 Oid indexOid = conForm->conindid;
2518 Anum_pg_constraint_conexclop,
2521 elog(
ERROR,
"null conexclop for constraint %u",
2525 &elems, NULL, &nElems);
2528 for (
i = 0;
i < nElems;
i++)
2546 elog(
ERROR,
"invalid constraint type \"%c\"", conForm->contype);
2550 if (conForm->condeferrable)
2552 if (conForm->condeferred)
2554 if (!conForm->convalidated)
2580 &keys, NULL, &nKeys);
2582 for (
j = 0;
j < nKeys;
j++)
2701 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2702 errmsg(
"input is a query, not an expression")));
2713 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2714 errmsg(
"expression contains variables of more than one relation")));
2720 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2721 errmsg(
"expression contains variables")));
2764 *result = role_rec->rolname;
2806 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2807 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
2814 Anum_pg_depend_refclassid,
2818 Anum_pg_depend_refobjid,
2822 Anum_pg_depend_refobjsubid,
2838 if (deprec->classid == RelationRelationId &&
2839 deprec->objsubid == 0 &&
2844 sequenceId = deprec->objid;
2902 if (proc->prokind == PROKIND_AGGREGATE)
2904 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2905 errmsg(
"\"%s\" is an aggregate function",
name)));
2907 isfunction = (proc->prokind != PROKIND_PROCEDURE);
2915 isfunction ?
"FUNCTION" :
"PROCEDURE",
2934 if (proc->prokind == PROKIND_WINDOW)
2936 switch (proc->provolatile)
2938 case PROVOLATILE_IMMUTABLE:
2941 case PROVOLATILE_STABLE:
2944 case PROVOLATILE_VOLATILE:
2948 switch (proc->proparallel)
2950 case PROPARALLEL_SAFE:
2953 case PROPARALLEL_RESTRICTED:
2956 case PROPARALLEL_UNSAFE:
2960 if (proc->proisstrict)
2962 if (proc->prosecdef)
2964 if (proc->proleakproof)
2968 if (proc->prolang == INTERNALlanguageId ||
2969 proc->prolang == ClanguageId)
2973 if (proc->procost != procost)
2976 if (proc->prorows > 0 && proc->prorows != 1000)
2979 if (proc->prosupport)
2987 argtypes[0] = INTERNALOID;
2994 if (oldlen !=
buf.len)
3023 pos = strchr(configitem,
'=');
3056 elog(
ERROR,
"invalid list syntax in proconfig item");
3058 foreach(lc, namelist)
3060 char *curname = (
char *)
lfirst(lc);
3063 if (
lnext(namelist, lc))
3076 if (proc->prolang == SQLlanguageId && !isnull)
3107 while (strstr(prosrc, dq.
data) != NULL)
3219 if (proc->proretset)
3233 if (proc->proretset)
3250 bool print_table_args,
bool print_defaults)
3257 int insertorderbyat = -1;
3266 &argtypes, &argnames, &argmodes);
3268 nlackdefaults = numargs;
3269 if (print_defaults && proc->pronargdefaults > 0)
3271 Datum proargdefaults;
3275 Anum_pg_proc_proargdefaults,
3284 nextargdefault =
list_head(argdefaults);
3286 nlackdefaults = proc->pronargs -
list_length(argdefaults);
3291 if (proc->prokind == PROKIND_AGGREGATE)
3298 elog(
ERROR,
"cache lookup failed for aggregate %u",
3301 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
3302 insertorderbyat = agg->aggnumdirectargs;
3308 for (
i = 0;
i < numargs;
i++)
3310 Oid argtype = argtypes[
i];
3311 char *argname = argnames ? argnames[
i] : NULL;
3312 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
3313 const char *modename;
3324 if (proc->prokind == PROKIND_PROCEDURE)
3330 case PROARGMODE_INOUT:
3331 modename =
"INOUT ";
3334 case PROARGMODE_OUT:
3338 case PROARGMODE_VARIADIC:
3339 modename =
"VARIADIC ";
3342 case PROARGMODE_TABLE:
3347 elog(
ERROR,
"invalid parameter mode '%c'", argmode);
3355 if (print_table_args != (argmode == PROARGMODE_TABLE))
3358 if (argsprinted == insertorderbyat)
3364 else if (argsprinted)
3368 if (argname && argname[0])
3371 if (print_defaults && isinput && inputargno > nlackdefaults)
3375 Assert(nextargdefault != NULL);
3377 nextargdefault =
lnext(argdefaults, nextargdefault);
3385 if (argsprinted == insertorderbyat &&
i == numargs - 1)
3389 print_defaults =
false;
3400 || argmodes[nth] == PROARGMODE_IN
3401 || argmodes[nth] == PROARGMODE_INOUT
3402 || argmodes[nth] == PROARGMODE_VARIADIC);
3420 for (
i = 0;
i < ntypes;
i++)
3452 Datum proargdefaults;
3461 if (nth_arg < 1 || nth_arg > numargs || !
is_input_argument(nth_arg - 1, argmodes))
3468 for (
i = 0;
i < nth_arg;
i++)
3473 Anum_pg_proc_proargdefaults,
3491 nth_default = nth_inputarg - 1 - (proc->pronargs - proc->pronargdefaults);
3493 if (nth_default < 0 || nth_default >=
list_length(argdefaults))
3498 node =
list_nth(argdefaults, nth_default);
3520 &argtypes, &argnames, &argmodes);
3599 bool forceprefix,
bool showimplicit)
3602 showimplicit, 0, 0);
3626 bool forceprefix,
bool showimplicit,
3627 int prettyFlags,
int startIndent)
3669 rte->
relkind = RELKIND_RELATION;
3727 Assert(crelid > 0 && crelid <= ntables);
3799 memset(&dpns, 0,
sizeof(dpns));
3846 names_hash =
hash_create(
"set_rtable_names names",
3852 foreach(lc, parent_namespaces)
3859 char *oldname = (
char *)
lfirst(lc2);
3861 if (oldname == NULL)
3874 foreach(lc, dpns->
rtable)
3887 else if (rte->
alias)
3923 int refnamelen = strlen(refname);
3924 char *modname = (
char *)
palloc(refnamelen + 16);
3932 memcpy(modname, refname, refnamelen);
3970 List *parent_namespaces)
4097 if (
j->alias == NULL &&
j->usingClause)
4123 elog(
ERROR,
"unrecognized node type: %d",
4181 if (rte->
alias == NULL)
4187 if (colname == NULL)
4191 if (leftattnos[
i] > 0)
4194 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4198 if (rightattnos[
i] > 0)
4201 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4239 foreach(lc,
j->usingClause)
4244 Assert(leftattnos[
i] != 0 && rightattnos[
i] != 0);
4265 parentUsing =
lappend(parentUsing, colname);
4268 if (leftattnos[
i] > 0)
4271 leftcolinfo->
colnames[leftattnos[
i] - 1] = colname;
4275 if (rightattnos[
i] > 0)
4278 rightcolinfo->
colnames[rightattnos[
i] - 1] = colname;
4294 elog(
ERROR,
"unrecognized node type: %d",
4310 char **real_colnames;
4330 ncolumns = tupdesc->
natts;
4331 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4333 for (
i = 0;
i < ncolumns;
i++)
4337 if (attr->attisdropped)
4338 real_colnames[
i] = NULL;
4375 real_colnames = (
char **)
palloc(ncolumns *
sizeof(
char *));
4378 foreach(lc, colnames)
4386 if (
cname[0] ==
'\0')
4388 real_colnames[
i] =
cname;
4422 changed_any =
false;
4424 for (
i = 0;
i < ncolumns;
i++)
4426 char *real_colname = real_colnames[
i];
4430 if (real_colname == NULL)
4437 if (colname == NULL)
4443 colname = real_colname;
4458 if (!changed_any && strcmp(colname, real_colname) != 0)
4538 changed_any =
false;
4559 if (real_colname == NULL)
4566 if (rte->
alias == NULL)
4573 if (colname == NULL)
4579 colname = real_colname;
4588 if (!changed_any && strcmp(colname, real_colname) != 0)
4623 while (
i < noldcolumns &&
4649 while (ic < leftcolinfo->num_cols &&
4652 Assert(ic < leftcolinfo->num_cols);
4658 while (i < colinfo->num_cols &&
4661 Assert(i < colinfo->num_cols);
4673 if (rte->
alias != NULL)
4698 while (ic < rightcolinfo->num_cols &&
4699 rightcolinfo->
colnames[ic] == NULL)
4701 Assert(ic < rightcolinfo->num_cols);
4707 while (i < colinfo->num_cols &&
4710 Assert(i < colinfo->num_cols);
4722 if (rte->
alias != NULL)
4739 #ifdef USE_ASSERT_CHECKING
4740 while (i < colinfo->num_cols && colinfo->
colnames[
i] == NULL)
4750 if (rte->
alias != NULL)
4773 if (oldname && strcmp(oldname, colname) == 0)
4785 if (oldname && strcmp(oldname, colname) == 0)
4792 char *oldname = (
char *)
lfirst(lc);
4794 if (strcmp(oldname, colname) == 0)
4801 char *oldname = (
char *)
lfirst(lc);
4803 if (strcmp(oldname, colname) == 0)
4826 int colnamelen = strlen(colname);
4827 char *modname = (
char *)
palloc(colnamelen + 16);
4835 memcpy(modname, colname, colnamelen);
4836 sprintf(modname + colnamelen,
"_%d",
i);
4888 elog(
ERROR,
"unrecognized node type in jointree: %d",
4895 elog(
ERROR,
"unrecognized node type in jointree: %d",
4927 if (rcolno < jrte->joinmergedcols)
4933 Assert(jcolno == numjoincols);
5001 ((
CteScan *) plan)->ctePlanId - 1);
5051 elog(
ERROR,
"could not find RecursiveUnion for WorkTableScan with wtParam %d",
5198 Assert(ev_action != NULL);
5201 elog(
ERROR,
"invalid empty ev_action list");
5238 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5239 errmsg(
"rule \"%s\" has unsupported event type %d",
5240 rulename, ev_type)));
5251 if (strcmp(ev_qual,
"<>") != 0)
5343 int prettyFlags,
int wrapColumn)
5381 Assert(ev_action != NULL);
5392 if (ev_type !=
'1' || !is_instead ||
5402 prettyFlags, wrapColumn, 0);
5428 TupleDesc resultDesc,
bool colNamesVisible,
5429 int prettyFlags,
int wrapColumn,
int startIndent)
5490 elog(
ERROR,
"unrecognized query command type: %d",
5504 bool first_list =
true;