From bbcee544aa551ada483281723faa6cea3b279e84 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 13 Jul 2004 16:48:16 +0000
Subject: [PATCH] Have \dn+ show permissions and description for schemas.

Dennis Bjorklund
---
 doc/src/sgml/ref/psql-ref.sgml |  6 ++++--
 src/bin/psql/command.c         |  4 ++--
 src/bin/psql/describe.c        | 22 +++++++++++++++-------
 src/bin/psql/describe.h        |  4 ++--
 src/bin/psql/help.c            |  4 ++--
 5 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 44b4a91f68..37c73e2281 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.117 2004/07/12 20:41:08 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.118 2004/07/13 16:48:15 momjian Exp $
 PostgreSQL documentation
 -->
 
@@ -990,7 +990,9 @@ testdb=>
         Lists all available schemas (namespaces). If <replaceable
         class="parameter">pattern</replaceable> (a regular expression)
         is specified, only schemas whose names match the pattern are listed.
-        Non-local temporary schemas are suppressed.
+        Non-local temporary schemas are suppressed.  If <literal>+</literal>
+        is appended to the command name, each object is listed with its associated
+        permissions and description, if any.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index ba8d623731..222d761163 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.120 2004/07/11 21:34:03 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.121 2004/07/13 16:48:16 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -326,7 +326,7 @@ exec_command(const char *cmd,
 				success = do_lo_list();
 				break;
 			case 'n':
-				success = listSchemas(pattern);
+				success = listSchemas(pattern, show_verbose);
 				break;
 			case 'o':
 				success = describeOperators(pattern);
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index e9758ec083..432063640d 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.101 2004/07/13 02:46:21 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.102 2004/07/13 16:48:16 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "describe.h"
@@ -1693,7 +1693,7 @@ listCasts(const char *pattern)
  * Describes schemas (namespaces)
  */
 bool
-listSchemas(const char *pattern)
+listSchemas(const char *pattern, bool verbose)
 {
 	PQExpBufferData buf;
 	PGresult   *res;
@@ -1702,13 +1702,21 @@ listSchemas(const char *pattern)
 	initPQExpBuffer(&buf);
 	printfPQExpBuffer(&buf,
 		"SELECT n.nspname AS \"%s\",\n"
-		"       u.usename AS \"%s\"\n"
-		"FROM pg_catalog.pg_namespace n LEFT JOIN pg_catalog.pg_user u\n"
+		"       u.usename AS \"%s\"",
+		_("Name"), _("Owner"));
+		
+	if (verbose)
+		appendPQExpBuffer(&buf,
+			",\n  n.nspacl as \"%s\","
+			"  pg_catalog.obj_description(n.oid, 'pg_namespace') as \"%s\"",
+			_("Access privileges"), _("Description"));
+						  
+	appendPQExpBuffer(&buf,
+		"\nFROM pg_catalog.pg_namespace n LEFT JOIN pg_catalog.pg_user u\n"
 		"       ON n.nspowner=u.usesysid\n"
 		"WHERE	(n.nspname NOT LIKE 'pg\\\\_temp\\\\_%%' OR\n"
-		"		 n.nspname = (pg_catalog.current_schemas(true))[1])\n",	/* temp schema is first */
-					  _("Name"),
-					  _("Owner"));
+		"		 n.nspname = (pg_catalog.current_schemas(true))[1])\n");	/* temp schema is first */
+
 	processNamePattern(&buf, pattern, true, false,
 					   NULL, "n.nspname", NULL,
 					   NULL);
diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h
index 195bf0e557..8dcca75989 100644
--- a/src/bin/psql/describe.h
+++ b/src/bin/psql/describe.h
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/describe.h,v 1.24 2004/06/18 06:14:04 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/describe.h,v 1.25 2004/07/13 16:48:16 momjian Exp $
  */
 #ifndef DESCRIBE_H
 #define DESCRIBE_H
@@ -56,7 +56,7 @@ bool		listConversions(const char *pattern);
 bool		listCasts(const char *pattern);
 
 /* \dn */
-bool		listSchemas(const char *pattern);
+bool		listSchemas(const char *pattern, bool verbose);
 
 
 #endif   /* DESCRIBE_H */
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index ed9574a229..1fffbd25b4 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.88 2004/06/18 06:14:04 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.89 2004/07/13 16:48:16 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -218,7 +218,7 @@ slashUsage(unsigned short int pager)
 	fprintf(output, _("  \\dD [PATTERN]  list domains\n"));
 	fprintf(output, _("  \\df [PATTERN]  list functions (add \"+\" for more detail)\n"));
 	fprintf(output, _("  \\dg [PATTERN]  list groups\n"));
-	fprintf(output, _("  \\dn [PATTERN]  list schemas\n"));
+	fprintf(output, _("  \\dn [PATTERN]  list schemas (add \"+\" for more detail)\n"));
 	fprintf(output, _("  \\do [NAME]     list operators\n"));
 	fprintf(output, _("  \\dl            list large objects, same as \\lo_list\n"));
 	fprintf(output, _("  \\dp [PATTERN]  list table, view and sequence access privileges\n"));