diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 49d4c0e3ce..c450972f27 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -769,7 +769,10 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) success = describeTablespaces(pattern, show_verbose); break; case 'c': - success = listConversions(pattern, show_verbose, show_system); + if (strncmp(cmd, "dco", 3) == 0 || strncmp(cmd, "dcoS", 4) == 0) /* Constraint */ + success = listConstraints(pattern, show_system); + else + success = listConversions(pattern, show_verbose, show_system); /* Conversion */ break; case 'C': success = listCasts(pattern, show_verbose); diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index ea721d963a..bd1ec2df5f 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -4815,6 +4815,61 @@ listExtendedStats(const char *pattern) return true; } +/* + * \dco + * + * Describes constraints + */ +bool +listConstraints(const char *pattern, bool showSystem) +{ + PQExpBufferData buf; + PGresult *res; + printQueryOpt myopt = pset.popt; + + initPQExpBuffer(&buf); + printfPQExpBuffer(&buf, + "SELECT \n" + "n.nspname AS \"%s\", \n" + "cst.conname AS \"%s\", \n" + "pg_catalog.pg_get_constraintdef(cst.oid) AS \"%s\", \n" + "c.relname AS \"%s\" \n" + "FROM pg_constraint cst \n" + "JOIN pg_namespace n ON n.oid = cst.connamespace \n" + "JOIN pg_class c ON c.oid = cst.conrelid \n", + gettext_noop("Schema"), + gettext_noop("Name"), + gettext_noop("Definition"), + gettext_noop("Table") + ); + + if (!showSystem && !pattern) + appendPQExpBufferStr(&buf, + "WHERE n.nspname <> 'pg_catalog' \n" + " AND n.nspname <> 'information_schema' \n"); + + processSQLNamePattern(pset.db, &buf, pattern, + !showSystem && !pattern, false, + "n.nspname", "cst.conname", + NULL, "pg_catalog.pg_table_is_visible(cst.conrelid)"); + + appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;"); + + res = PSQLexec(buf.data); + termPQExpBuffer(&buf); + if (!res) + return false; + + myopt.nullPrint = NULL; + myopt.title = _("List of constsraints"); + myopt.translate_header = true; + + printQuery(res, &myopt, pset.queryFout, false, pset.logfile); + + PQclear(res); + return true; +} + /* * \dC * diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h index 71b320f1fc..53e51db036 100644 --- a/src/bin/psql/describe.h +++ b/src/bin/psql/describe.h @@ -76,6 +76,9 @@ extern bool listDomains(const char *pattern, bool verbose, bool showSystem); /* \dc */ extern bool listConversions(const char *pattern, bool verbose, bool showSystem); +/* \dco */ +extern bool listConstraints(const char *pattern, bool showSystem); + /* \dC */ extern bool listCasts(const char *pattern, bool verbose);