Skip to content

Commit 6f81a1f

Browse files
committed
Make psql \d on a sequence show the table/column owning it
1 parent 039680a commit 6f81a1f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/bin/psql/describe.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,43 @@ describeOneTableDetails(const char *schemaname,
16091609
PQclear(result);
16101610
}
16111611
}
1612+
else if (tableinfo.relkind == 'S')
1613+
{
1614+
/* Footer information about a sequence */
1615+
PGresult *result = NULL;
1616+
1617+
/* Get the column that owns this sequence */
1618+
printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1619+
"\n pg_catalog.quote_ident(relname) || '.' ||"
1620+
"\n pg_catalog.quote_ident(attname)"
1621+
"\nFROM pg_catalog.pg_class c"
1622+
"\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1623+
"\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1624+
"\nINNER JOIN pg_catalog.pg_attribute a ON ("
1625+
"\n a.attrelid=c.oid AND"
1626+
"\n a.attnum=d.refobjsubid)"
1627+
"\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1628+
"\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1629+
"\n AND d.objid=%s"
1630+
"\n AND d.deptype='a'",
1631+
oid);
1632+
1633+
result = PSQLexec(buf.data, false);
1634+
if (!result)
1635+
goto error_return;
1636+
else if (PQntuples(result) == 1)
1637+
{
1638+
printfPQExpBuffer(&buf, _("Owned by: %s"),
1639+
PQgetvalue(result, 0, 0));
1640+
printTableAddFooter(&cont, buf.data);
1641+
}
1642+
/*
1643+
* If we get no rows back, don't show anything (obviously).
1644+
* We should never get more than one row back, but if we do,
1645+
* just ignore it and don't print anything.
1646+
*/
1647+
PQclear(result);
1648+
}
16121649
else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
16131650
{
16141651
/* Footer information about a table */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy