The attached patch makes the \d output for psql on a sequence show
which table/column owns the sequence. The table already showed the
dependency the other way through the default value, but going from
sequence back to table was not possible.

Comments/reviews?

-- 
 Magnus Hagander
 Me: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index d5466f8..21e9171 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -1609,6 +1609,42 @@ describeOneTableDetails(const char *schemaname,
 			PQclear(result);
 		}
 	}
+	else if (tableinfo.relkind == 'S')
+	{
+		/* Footer information about a sequence */
+		PGresult   *result = NULL;
+
+		/* Get the column that owns this sequence */
+		printfPQExpBuffer(&buf, "SELECT quote_ident(nspname) || '.' ||"
+						  "\n   quote_ident(relname) || '.' ||"
+						  "\n   quote_ident(attname)"
+						  "\nFROM pg_class"
+						  "\nINNER JOIN pg_depend ON pg_class.oid=pg_depend.refobjid"
+						  "\nINNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace"
+						  "\nINNER JOIN pg_attribute ON ("
+						  "\n pg_attribute.attrelid=pg_class.oid AND"
+						  "\n pg_attribute.attnum=pg_depend.refobjsubid)"
+						  "\nWHERE classid='pg_class'::regclass"
+						  "\n AND objid=%s"
+						  "\n AND deptype='a'",
+						  oid);
+
+		result = PSQLexec(buf.data, false);
+		if (!result)
+			goto error_return;
+		else if (PQntuples(result) > 1)
+		{
+			PQclear(result);
+			goto error_return;
+		}
+		else if (PQntuples(result) == 1)
+		{
+			printfPQExpBuffer(&buf, _("Owned by: %s"),
+							  PQgetvalue(result, 0, 0));
+			printTableAddFooter(&cont, buf.data);
+		}
+		PQclear(result);
+	}
 	else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
 	{
 		/* Footer information about a table */
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to