On Fri, Nov 4, 2011 at 15:44, Tom Lane <t...@sss.pgh.pa.us> wrote:
> Stephen Frost <sfr...@snowman.net> writes:
>> I just noticed it was pulling from pg_depend and we could be creating
>> multiple dependencies on a single sequence by having two tables use it
>> as a default value.  If that situation doesn't cause a problem for this,
>> then that's fine. :)  Couldn't remember if we distinguished 'owned by'
>> from 'dependend upon' for seqeunces.
>
> Yeah, we do, via the deptype.  The check for deptype = 'a' is the
> correct thing here.
>
> Still, I'm not terribly comfortable with having multiple matches be
> treated as a reason to fail the entire \d command.  It'd likely be
> better to just not add a footer if you get an unexpected number of
> matches.

Ok.

Updated patch attached that does this, and the proper schema qualifications.

-- 
 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..746f18e 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -1609,6 +1609,43 @@ 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_catalog.pg_class"
+						  "\nINNER JOIN pg_catalog.pg_depend ON pg_class.oid=pg_depend.refobjid"
+						  "\nINNER JOIN pg_catalog.pg_namespace ON pg_namespace.oid=pg_class.relnamespace"
+						  "\nINNER JOIN pg_catalog.pg_attribute ON ("
+						  "\n pg_attribute.attrelid=pg_class.oid AND"
+						  "\n pg_attribute.attnum=pg_depend.refobjsubid)"
+						  "\nWHERE classid='pg_catalog.pg_class'::regclass"
+						  "\n AND refclassid='pg_catalog.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)
+		{
+			printfPQExpBuffer(&buf, _("Owned by: %s"),
+							  PQgetvalue(result, 0, 0));
+			printTableAddFooter(&cont, buf.data);
+		}
+		/*
+		 * If we get no rows back, don't show anything (obviously).
+		 * We should never get more than one row back, but if we do,
+		 * just ignore it and don't print anything.
+		 */
+		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