]> granicus.if.org Git - postgresql/blobdiff - src/bin/psql/describe.c
Teach psql to display the comments on conversions and domains.
[postgresql] / src / bin / psql / describe.c
index dca1f577c03fd6a2ff2052b4d644203a27a8a351..5587721ea8f0c4b040e5dfe833849f012a4896fb 100644 (file)
@@ -6,9 +6,9 @@
  * with servers of versions 7.4 and up.  It's okay to omit irrelevant
  * information for an old server, but not to fail outright.
  *
- * Copyright (c) 2000-2010, PostgreSQL Global Development Group
+ * Copyright (c) 2000-2011, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.234 2010/01/17 22:56:23 tgl Exp $
+ * src/bin/psql/describe.c
  */
 #include "postgres_fe.h"
 
@@ -38,6 +38,7 @@ static bool describeOneTSConfig(const char *oid, const char *nspname,
                                        const char *cfgname,
                                        const char *pnspname, const char *prsname);
 static void printACLColumn(PQExpBuffer buf, const char *colname);
+static bool listOneExtensionContents(const char *extname, const char *oid);
 
 
 /*----------------
@@ -473,17 +474,27 @@ describeTypes(const char *pattern, bool verbose, bool showSystem)
                                                  gettext_noop("Internal name"),
                                                  gettext_noop("Size"));
        if (verbose && pset.sversion >= 80300)
+       {
                appendPQExpBuffer(&buf,
                                                  "  pg_catalog.array_to_string(\n"
                                                  "      ARRAY(\n"
                                                  "                  SELECT e.enumlabel\n"
                                                  "          FROM pg_catalog.pg_enum e\n"
-                                                 "          WHERE e.enumtypid = t.oid\n"
-                                                 "          ORDER BY e.oid\n"
+                                                 "          WHERE e.enumtypid = t.oid\n");
+
+               if (pset.sversion >= 90100)
+                       appendPQExpBuffer(&buf,
+                                                         "          ORDER BY e.enumsortorder\n");
+               else
+                       appendPQExpBuffer(&buf,
+                                                         "          ORDER BY e.oid\n");
+
+               appendPQExpBuffer(&buf,
                                                  "      ),\n"
                                                  "      E'\\n'\n"
                                                  "  ) AS \"%s\",\n",
                                                  gettext_noop("Elements"));
+       }
 
        appendPQExpBuffer(&buf,
                                "  pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
@@ -548,6 +559,19 @@ describeOperators(const char *pattern, bool showSystem)
 
        initPQExpBuffer(&buf);
 
+       /*
+        * Note: before Postgres 9.1, we did not assign comments to any built-in
+        * operators, preferring to let the comment on the underlying function
+        * suffice.  The coalesce() on the obj_description() calls below supports
+        * this convention by providing a fallback lookup of a comment on the
+        * operator's function.  As of 9.1 there is a policy that every built-in
+        * operator should have a comment; so the coalesce() is no longer
+        * necessary so far as built-in operators are concerned.  We keep it
+        * anyway, for now, because (1) third-party modules may still be following
+        * the old convention, and (2) we'd need to do it anyway when talking to a
+        * pre-9.1 server.
+        */
+
        printfPQExpBuffer(&buf,
                                          "SELECT n.nspname as \"%s\",\n"
                                          "  o.oprname AS \"%s\",\n"
@@ -616,7 +640,7 @@ listAllDbs(bool verbose)
                appendPQExpBuffer(&buf,
                                                  "       d.datcollate as \"%s\",\n"
                                                  "       d.datctype as \"%s\",\n",
-                                                 gettext_noop("Collation"),
+                                                 gettext_noop("Collate"),
                                                  gettext_noop("Ctype"));
        appendPQExpBuffer(&buf, "       ");
        printACLColumn(&buf, "d.datacl");
@@ -677,11 +701,12 @@ permissionsList(const char *pattern)
        printfPQExpBuffer(&buf,
                                          "SELECT n.nspname as \"%s\",\n"
                                          "  c.relname as \"%s\",\n"
-                                         "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' END as \"%s\",\n"
+                                         "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
                                          "  ",
                                          gettext_noop("Schema"),
                                          gettext_noop("Name"),
           gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
+                                         gettext_noop("foreign table"),
                                          gettext_noop("Type"));
 
        printACLColumn(&buf, "c.relacl");
@@ -697,7 +722,7 @@ permissionsList(const char *pattern)
 
        appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_class c\n"
           "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
-                                         "WHERE c.relkind IN ('r', 'v', 'S')\n");
+                                         "WHERE c.relkind IN ('r', 'v', 'S', 'f')\n");
 
        /*
         * Unless a schema pattern is specified, we suppress system and temp
@@ -745,7 +770,7 @@ listDefaultACLs(const char *pattern)
        printQueryOpt myopt = pset.popt;
        static const bool translate_columns[] = {false, false, true, false};
 
-       if (pset.sversion < 80500)
+       if (pset.sversion < 90000)
        {
                fprintf(stderr, _("The server (version %d.%d) does not support altering default privileges.\n"),
                                pset.sversion / 10000, (pset.sversion / 100) % 100);
@@ -755,7 +780,7 @@ listDefaultACLs(const char *pattern)
        initPQExpBuffer(&buf);
 
        printfPQExpBuffer(&buf,
-                                         "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
+                          "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
                                          "  n.nspname AS \"%s\",\n"
                                          "  CASE d.defaclobjtype WHEN 'r' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END AS \"%s\",\n"
                                          "  ",
@@ -769,7 +794,7 @@ listDefaultACLs(const char *pattern)
        printACLColumn(&buf, "d.defaclacl");
 
        appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
-          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
+                                         "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
 
        processSQLNamePattern(pset.db, &buf, pattern, false, false,
                                                  NULL,
@@ -865,7 +890,7 @@ objectDescription(const char *pattern, bool showSystem)
                                                  "n.nspname", "p.proname", NULL,
                                                  "pg_catalog.pg_function_is_visible(p.oid)");
 
-       /* Operator descriptions (only if operator has its own comment) */
+       /* Operator descriptions */
        appendPQExpBuffer(&buf,
                                          "UNION ALL\n"
                                          "  SELECT o.oid as oid, o.tableoid as tableoid,\n"
@@ -884,7 +909,7 @@ objectDescription(const char *pattern, bool showSystem)
                                                  "n.nspname", "o.oprname", NULL,
                                                  "pg_catalog.pg_operator_is_visible(o.oid)");
 
-       /* Type description */
+       /* Type descriptions */
        appendPQExpBuffer(&buf,
                                          "UNION ALL\n"
                                          "  SELECT t.oid as oid, t.tableoid as tableoid,\n"
@@ -911,15 +936,16 @@ objectDescription(const char *pattern, bool showSystem)
                                          "  n.nspname as nspname,\n"
                                          "  CAST(c.relname AS pg_catalog.text) as name,\n"
                                          "  CAST(\n"
-                                         "    CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' END"
+                                         "    CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END"
                                          "  AS pg_catalog.text) as object\n"
                                          "  FROM pg_catalog.pg_class c\n"
         "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
-                                         "  WHERE c.relkind IN ('r', 'v', 'i', 'S')\n",
+                                         "  WHERE c.relkind IN ('r', 'v', 'i', 'S', 'f')\n",
                                          gettext_noop("table"),
                                          gettext_noop("view"),
                                          gettext_noop("index"),
-                                         gettext_noop("sequence"));
+                                         gettext_noop("sequence"),
+                                         gettext_noop("foreign table"));
 
        if (!showSystem && !pattern)
                appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
@@ -929,7 +955,7 @@ objectDescription(const char *pattern, bool showSystem)
                                                  "n.nspname", "c.relname", NULL,
                                                  "pg_catalog.pg_table_is_visible(c.oid)");
 
-       /* Rule description (ignore rules for views) */
+       /* Rule descriptions (ignore rules for views) */
        appendPQExpBuffer(&buf,
                                          "UNION ALL\n"
                                          "  SELECT r.oid as oid, r.tableoid as tableoid,\n"
@@ -951,7 +977,7 @@ objectDescription(const char *pattern, bool showSystem)
                                                  "n.nspname", "r.rulename", NULL,
                                                  "pg_catalog.pg_table_is_visible(c.oid)");
 
-       /* Trigger description */
+       /* Trigger descriptions */
        appendPQExpBuffer(&buf,
                                          "UNION ALL\n"
                                          "  SELECT t.oid as oid, t.tableoid as tableoid,\n"
@@ -1105,9 +1131,10 @@ describeOneTableDetails(const char *schemaname,
                bool            hasrules;
                bool            hastriggers;
                bool            hasoids;
-               bool            hasexclusion;
                Oid                     tablespace;
                char       *reloptions;
+               char       *reloftype;
+               char            relpersistence;
        }                       tableinfo;
        bool            show_modifiers = false;
        bool            retval;
@@ -1122,15 +1149,33 @@ describeOneTableDetails(const char *schemaname,
        initPQExpBuffer(&tmpbuf);
 
        /* Get general table info */
-       if (pset.sversion >= 80500)
+       if (pset.sversion >= 90100)
+       {
+               printfPQExpBuffer(&buf,
+                         "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
+                                                 "c.relhastriggers, c.relhasoids, "
+                                                 "%s, c.reltablespace, "
+                                                 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
+                                                 "c.relpersistence\n"
+                                                 "FROM pg_catalog.pg_class c\n "
+                  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
+                                                 "WHERE c.oid = '%s';",
+                                                 (verbose ?
+                                                  "pg_catalog.array_to_string(c.reloptions || "
+                                                  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
+                                                  : "''"),
+                                                 oid);
+       }
+       else if (pset.sversion >= 90000)
        {
                printfPQExpBuffer(&buf,
                          "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
                                                  "c.relhastriggers, c.relhasoids, "
-                                                 "%s, c.reltablespace, c.relhasexclusion\n"
+                                                 "%s, c.reltablespace, "
+                                                 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
                                                  "FROM pg_catalog.pg_class c\n "
                   "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
-                                                 "WHERE c.oid = '%s'\n",
+                                                 "WHERE c.oid = '%s';",
                                                  (verbose ?
                                                   "pg_catalog.array_to_string(c.reloptions || "
                                                   "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
@@ -1145,7 +1190,7 @@ describeOneTableDetails(const char *schemaname,
                                                  "%s, c.reltablespace\n"
                                                  "FROM pg_catalog.pg_class c\n "
                   "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
-                                                 "WHERE c.oid = '%s'\n",
+                                                 "WHERE c.oid = '%s';",
                                                  (verbose ?
                                                   "pg_catalog.array_to_string(c.reloptions || "
                                                   "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
@@ -1158,7 +1203,7 @@ describeOneTableDetails(const char *schemaname,
                                          "SELECT relchecks, relkind, relhasindex, relhasrules, "
                                                  "reltriggers <> 0, relhasoids, "
                                                  "%s, reltablespace\n"
-                                                 "FROM pg_catalog.pg_class WHERE oid = '%s'",
+                                                 "FROM pg_catalog.pg_class WHERE oid = '%s';",
                                                  (verbose ?
                                         "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
                                                  oid);
@@ -1169,7 +1214,7 @@ describeOneTableDetails(const char *schemaname,
                                          "SELECT relchecks, relkind, relhasindex, relhasrules, "
                                                  "reltriggers <> 0, relhasoids, "
                                                  "'', reltablespace\n"
-                                                 "FROM pg_catalog.pg_class WHERE oid = '%s'",
+                                                 "FROM pg_catalog.pg_class WHERE oid = '%s';",
                                                  oid);
        }
        else
@@ -1178,7 +1223,7 @@ describeOneTableDetails(const char *schemaname,
                                          "SELECT relchecks, relkind, relhasindex, relhasrules, "
                                                  "reltriggers <> 0, relhasoids, "
                                                  "'', ''\n"
-                                                 "FROM pg_catalog.pg_class WHERE oid = '%s'",
+                                                 "FROM pg_catalog.pg_class WHERE oid = '%s';",
                                                  oid);
        }
 
@@ -1205,8 +1250,10 @@ describeOneTableDetails(const char *schemaname,
                strdup(PQgetvalue(res, 0, 6)) : 0;
        tableinfo.tablespace = (pset.sversion >= 80000) ?
                atooid(PQgetvalue(res, 0, 7)) : 0;
-       tableinfo.hasexclusion = (pset.sversion >= 80500) ?
-               strcmp(PQgetvalue(res, 0, 8), "t") == 0 : false;
+       tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
+               strdup(PQgetvalue(res, 0, 8)) : 0;
+       tableinfo.relpersistence = (pset.sversion >= 90100 && strcmp(PQgetvalue(res, 0, 9), "") != 0) ?
+               PQgetvalue(res, 0, 9)[0] : 0;
        PQclear(res);
        res = NULL;
 
@@ -1218,7 +1265,7 @@ describeOneTableDetails(const char *schemaname,
        {
                printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
                /* must be separate because fmtId isn't reentrant */
-               appendPQExpBuffer(&buf, ".%s", fmtId(relationname));
+               appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
 
                res = PSQLexec(buf.data, false);
                if (!res)
@@ -1234,20 +1281,46 @@ describeOneTableDetails(const char *schemaname,
                res = NULL;
        }
 
-       /* Get column info */
+       /*
+        * Get column info
+        *
+        * You need to modify value of "firstvcol" which willbe defined below if
+        * you are adding column(s) preceding to verbose-only columns.
+        */
        printfPQExpBuffer(&buf, "SELECT a.attname,");
        appendPQExpBuffer(&buf, "\n  pg_catalog.format_type(a.atttypid, a.atttypmod),"
                                          "\n  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
                                          "\n   FROM pg_catalog.pg_attrdef d"
                                          "\n   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
-                                         "\n  a.attnotnull, a.attnum");
+                                         "\n  a.attnotnull, a.attnum,");
+       if (pset.sversion >= 90100)
+               appendPQExpBuffer(&buf, "\n  (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
+                                                 "   WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
+       else
+               appendPQExpBuffer(&buf, "\n  NULL AS attcollation");
        if (tableinfo.relkind == 'i')
                appendPQExpBuffer(&buf, ",\n  pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
+       else
+               appendPQExpBuffer(&buf, ",\n  NULL AS indexdef");
+       if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
+               appendPQExpBuffer(&buf, ",\n  a.attfdwoptions");
+       else
+               appendPQExpBuffer(&buf, ",\n  NULL AS attfdwoptions");
        if (verbose)
-               appendPQExpBuffer(&buf, ",\n  a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)");
+       {
+               appendPQExpBuffer(&buf, ",\n  a.attstorage");
+               /*
+                * In 9.0+, we have column comments for: relations, views, composite
+                * types, and foreign tables (c.f. CommentObject() in comment.c).
+                */
+               if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
+                       tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
+                       appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
+       }
+
        appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
        appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
-       appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
+       appendPQExpBuffer(&buf, "\nORDER BY a.attnum;");
 
        res = PSQLexec(buf.data, false);
        if (!res)
@@ -1258,8 +1331,12 @@ describeOneTableDetails(const char *schemaname,
        switch (tableinfo.relkind)
        {
                case 'r':
-                       printfPQExpBuffer(&title, _("Table \"%s.%s\""),
-                                                         schemaname, relationname);
+                       if (tableinfo.relpersistence == 'u')
+                               printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
+                                                                 schemaname, relationname);
+                       else
+                               printfPQExpBuffer(&title, _("Table \"%s.%s\""),
+                                                                 schemaname, relationname);
                        break;
                case 'v':
                        printfPQExpBuffer(&title, _("View \"%s.%s\""),
@@ -1270,8 +1347,12 @@ describeOneTableDetails(const char *schemaname,
                                                          schemaname, relationname);
                        break;
                case 'i':
-                       printfPQExpBuffer(&title, _("Index \"%s.%s\""),
-                                                         schemaname, relationname);
+                       if (tableinfo.relpersistence == 'u')
+                               printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
+                                                                 schemaname, relationname);
+                       else
+                               printfPQExpBuffer(&title, _("Index \"%s.%s\""),
+                                                                 schemaname, relationname);
                        break;
                case 's':
                        /* not used as of 8.2, but keep it for backwards compatibility */
@@ -1286,6 +1367,10 @@ describeOneTableDetails(const char *schemaname,
                        printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
                                                          schemaname, relationname);
                        break;
+               case 'f':
+                       printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
+                                                         schemaname, relationname);
+                       break;
                default:
                        /* untranslated unknown relkind */
                        printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
@@ -1298,7 +1383,8 @@ describeOneTableDetails(const char *schemaname,
        headers[1] = gettext_noop("Type");
        cols = 2;
 
-       if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v')
+       if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
+               tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
        {
                show_modifiers = true;
                headers[cols++] = gettext_noop("Modifiers");
@@ -1311,10 +1397,16 @@ describeOneTableDetails(const char *schemaname,
        if (tableinfo.relkind == 'i')
                headers[cols++] = gettext_noop("Definition");
 
+       if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
+               headers[cols++] = gettext_noop("Options");
+
        if (verbose)
        {
                headers[cols++] = gettext_noop("Storage");
-               headers[cols++] = gettext_noop("Description");
+               /* Column comments, if the relkind supports this feature. */
+               if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
+                       tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
+                       headers[cols++] = gettext_noop("Description");
        }
 
        printTableInit(&cont, &myopt, title.data, cols, numrows);
@@ -1329,7 +1421,7 @@ describeOneTableDetails(const char *schemaname,
                PGresult   *result;
 
                printfPQExpBuffer(&buf,
-                         "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
+                         "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
                                                  oid);
                result = PSQLexec(buf.data, false);
                if (!result)
@@ -1345,17 +1437,30 @@ describeOneTableDetails(const char *schemaname,
        for (i = 0; i < numrows; i++)
        {
                /* Column */
-               printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
+               printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
 
                /* Type */
-               printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
+               printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
 
-               /* Modifiers: not null and default */
+               /* Modifiers: collate, not null, default */
                if (show_modifiers)
                {
                        resetPQExpBuffer(&tmpbuf);
+
+                       if (!PQgetisnull(res, i, 5))
+                       {
+                               if (tmpbuf.len > 0)
+                                       appendPQExpBufferStr(&tmpbuf, " ");
+                               appendPQExpBuffer(&tmpbuf, _("collate %s"),
+                                                                 PQgetvalue(res, i, 5));
+                       }
+
                        if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
+                       {
+                               if (tmpbuf.len > 0)
+                                       appendPQExpBufferStr(&tmpbuf, " ");
                                appendPQExpBufferStr(&tmpbuf, _("not null"));
+                       }
 
                        /* handle "default" here */
                        /* (note: above we cut off the 'default' string at 128) */
@@ -1369,22 +1474,26 @@ describeOneTableDetails(const char *schemaname,
                        }
 
                        modifiers[i] = pg_strdup(tmpbuf.data);
-                       printTableAddCell(&cont, modifiers[i], false);
+                       printTableAddCell(&cont, modifiers[i], false, false);
                }
 
                /* Value: for sequences only */
                if (tableinfo.relkind == 'S')
-                       printTableAddCell(&cont, seq_values[i], false);
+                       printTableAddCell(&cont, seq_values[i], false, false);
 
                /* Expression for index column */
                if (tableinfo.relkind == 'i')
-                       printTableAddCell(&cont, PQgetvalue(res, i, 5), false);
+                       printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
+
+               /* FDW options for foreign table column, only for 9.2 or later */
+               if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
+                       printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
 
                /* Storage and Description */
                if (verbose)
                {
-                       int                     firstvcol = (tableinfo.relkind == 'i' ? 6 : 5);
-                       char       *storage  = PQgetvalue(res, i, firstvcol);
+                       int                     firstvcol = 8;
+                       char       *storage = PQgetvalue(res, i, firstvcol);
 
                        /* these strings are literal in our syntax, so not translated. */
                        printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
@@ -1392,8 +1501,12 @@ describeOneTableDetails(const char *schemaname,
                                                                           (storage[0] == 'x' ? "extended" :
                                                                                (storage[0] == 'e' ? "external" :
                                                                                 "???")))),
-                                                         false);
-                       printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1), false);
+                                                         false, false);
+                       /* Column comments, if the relkind supports this feature. */
+                       if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
+                               tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
+                               printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
+                                                                 false, false);
                }
        }
 
@@ -1409,32 +1522,28 @@ describeOneTableDetails(const char *schemaname,
                        appendPQExpBuffer(&buf, "i.indisvalid,\n");
                else
                        appendPQExpBuffer(&buf, "true AS indisvalid,\n");
-               if (pset.sversion >= 80500)
+               if (pset.sversion >= 90000)
                        appendPQExpBuffer(&buf,
                                                          "  (NOT i.indimmediate) AND "
-                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_depend d, "
-                                                         "pg_catalog.pg_constraint con WHERE "
-                                                         "d.classid = 'pg_catalog.pg_class'::pg_catalog.regclass AND "
-                                                         "d.objid = i.indexrelid AND "
-                                                         "d.refclassid = 'pg_catalog.pg_constraint'::pg_catalog.regclass AND "
-                                                         "d.refobjid = con.oid AND d.deptype = 'i' AND "
-                                                         "con.condeferrable) AS condeferrable,\n"
+                                                       "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
+                                                         "WHERE conrelid = i.indrelid AND "
+                                                         "conindid = i.indexrelid AND "
+                                                         "contype IN ('p','u','x') AND "
+                                                         "condeferrable) AS condeferrable,\n"
                                                          "  (NOT i.indimmediate) AND "
-                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_depend d, "
-                                                         "pg_catalog.pg_constraint con WHERE "
-                                                         "d.classid = 'pg_catalog.pg_class'::pg_catalog.regclass AND "
-                                                         "d.objid = i.indexrelid AND "
-                                                         "d.refclassid = 'pg_catalog.pg_constraint'::pg_catalog.regclass AND "
-                                                         "d.refobjid = con.oid AND d.deptype = 'i' AND "
-                                                         "con.condeferred) AS condeferred,\n");
+                                                       "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
+                                                         "WHERE conrelid = i.indrelid AND "
+                                                         "conindid = i.indexrelid AND "
+                                                         "contype IN ('p','u','x') AND "
+                                                         "condeferred) AS condeferred,\n");
                else
                        appendPQExpBuffer(&buf,
                                                "  false AS condeferrable, false AS condeferred,\n");
                appendPQExpBuffer(&buf, "  a.amname, c2.relname, "
-                                                 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
+                                         "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
                                                  "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
                  "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
-                                                 "AND i.indrelid = c2.oid",
+                                                 "AND i.indrelid = c2.oid;",
                                                  oid);
 
                result = PSQLexec(buf.data, false);
@@ -1505,7 +1614,7 @@ describeOneTableDetails(const char *schemaname,
                        printfPQExpBuffer(&buf,
                                                          "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
                                                          "FROM pg_catalog.pg_rewrite r\n"
-                       "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1",
+                       "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
                                                          oid);
                        result = PSQLexec(buf.data, false);
                        if (!result)
@@ -1529,7 +1638,7 @@ describeOneTableDetails(const char *schemaname,
                        PQclear(result);
                }
        }
-       else if (tableinfo.relkind == 'r')
+       else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
        {
                /* Footer information about a table */
                PGresult   *result = NULL;
@@ -1544,33 +1653,25 @@ describeOneTableDetails(const char *schemaname,
                                appendPQExpBuffer(&buf, "i.indisvalid, ");
                        else
                                appendPQExpBuffer(&buf, "true as indisvalid, ");
-                       appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)");
-                       if (pset.sversion >= 80500)
+                       appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n  ");
+                       if (pset.sversion >= 90000)
                                appendPQExpBuffer(&buf,
-                                                         ",\n  (NOT i.indimmediate) AND "
-                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_depend d, "
-                                                         "pg_catalog.pg_constraint con WHERE "
-                                                         "d.classid = 'pg_catalog.pg_class'::pg_catalog.regclass AND "
-                                                         "d.objid = i.indexrelid AND "
-                                                         "d.refclassid = 'pg_catalog.pg_constraint'::pg_catalog.regclass AND "
-                                                         "d.refobjid = con.oid AND d.deptype = 'i' AND "
-                                                         "con.condeferrable) AS condeferrable"
-                                                         ",\n  (NOT i.indimmediate) AND "
-                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_depend d, "
-                                                         "pg_catalog.pg_constraint con WHERE "
-                                                         "d.classid = 'pg_catalog.pg_class'::pg_catalog.regclass AND "
-                                                         "d.objid = i.indexrelid AND "
-                                                         "d.refclassid = 'pg_catalog.pg_constraint'::pg_catalog.regclass AND "
-                                                         "d.refobjid = con.oid AND d.deptype = 'i' AND "
-                                                         "con.condeferred) AS condeferred");
+                                                  "pg_catalog.pg_get_constraintdef(con.oid, true), "
+                                                                 "contype, condeferrable, condeferred");
                        else
-                               appendPQExpBuffer(&buf, ", false AS condeferrable, false AS condeferred");
+                               appendPQExpBuffer(&buf,
+                                                                 "null AS constraintdef, null AS contype, "
+                                                        "false AS condeferrable, false AS condeferred");
                        if (pset.sversion >= 80000)
                                appendPQExpBuffer(&buf, ", c2.reltablespace");
                        appendPQExpBuffer(&buf,
-                                                         "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
+                                                         "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
+                       if (pset.sversion >= 90000)
+                               appendPQExpBuffer(&buf,
+                                                                 "  LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
+                       appendPQExpBuffer(&buf,
                                                          "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
-                         "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
+                         "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
                                                          oid);
                        result = PSQLexec(buf.data, false);
                        if (!result)
@@ -1583,46 +1684,60 @@ describeOneTableDetails(const char *schemaname,
                                printTableAddFooter(&cont, _("Indexes:"));
                                for (i = 0; i < tuples; i++)
                                {
-                                       const char *indexdef;
-                                       const char *usingpos;
-
                                        /* untranslated index name */
                                        printfPQExpBuffer(&buf, "    \"%s\"",
                                                                          PQgetvalue(result, i, 0));
 
-                                       /* Label as primary key or unique (but not both) */
-                                       appendPQExpBuffer(&buf,
-                                                                  strcmp(PQgetvalue(result, i, 1), "t") == 0
-                                                                         ? " PRIMARY KEY," :
-                                                                 (strcmp(PQgetvalue(result, i, 2), "t") == 0
-                                                                  ? " UNIQUE,"
-                                                                  : ""));
-                                       /* Everything after "USING" is echoed verbatim */
-                                       indexdef = PQgetvalue(result, i, 5);
-                                       usingpos = strstr(indexdef, " USING ");
-                                       if (usingpos)
-                                               indexdef = usingpos + 7;
+                                       /* If exclusion constraint, print the constraintdef */
+                                       if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
+                                       {
+                                               appendPQExpBuffer(&buf, " %s",
+                                                                                 PQgetvalue(result, i, 6));
+                                       }
+                                       else
+                                       {
+                                               const char *indexdef;
+                                               const char *usingpos;
+
+                                               /* Label as primary key or unique (but not both) */
+                                               if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
+                                                       appendPQExpBuffer(&buf, " PRIMARY KEY,");
+                                               else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
+                                               {
+                                                       if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
+                                                               appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
+                                                       else
+                                                               appendPQExpBuffer(&buf, " UNIQUE,");
+                                               }
+
+                                               /* Everything after "USING" is echoed verbatim */
+                                               indexdef = PQgetvalue(result, i, 5);
+                                               usingpos = strstr(indexdef, " USING ");
+                                               if (usingpos)
+                                                       indexdef = usingpos + 7;
+                                               appendPQExpBuffer(&buf, " %s", indexdef);
+
+                                               /* Need these for deferrable PK/UNIQUE indexes */
+                                               if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
+                                                       appendPQExpBuffer(&buf, " DEFERRABLE");
 
-                                       appendPQExpBuffer(&buf, " %s", indexdef);
+                                               if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
+                                                       appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
+                                       }
 
+                                       /* Add these for all cases */
                                        if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
                                                appendPQExpBuffer(&buf, " CLUSTER");
 
                                        if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
                                                appendPQExpBuffer(&buf, " INVALID");
 
-                                       if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
-                                               appendPQExpBuffer(&buf, " DEFERRABLE");
-
-                                       if (strcmp(PQgetvalue(result, i, 7), "t") == 0)
-                                               appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
-
                                        printTableAddFooter(&cont, buf.data);
 
                                        /* Print tablespace of the index on the same line */
                                        if (pset.sversion >= 80000)
                                                add_tablespace_footer(&cont, 'i',
-                                                                                       atooid(PQgetvalue(result, i, 8)),
+                                                                                  atooid(PQgetvalue(result, i, 10)),
                                                                                          false);
                                }
                        }
@@ -1636,7 +1751,7 @@ describeOneTableDetails(const char *schemaname,
                                                          "SELECT r.conname, "
                                                          "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
                                                          "FROM pg_catalog.pg_constraint r\n"
-                                  "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1",
+                                  "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1;",
                                                          oid);
                        result = PSQLexec(buf.data, false);
                        if (!result)
@@ -1660,38 +1775,6 @@ describeOneTableDetails(const char *schemaname,
                        PQclear(result);
                }
 
-               /* print exclusion constraints */
-               if (tableinfo.hasexclusion)
-               {
-                       printfPQExpBuffer(&buf,
-                                                         "SELECT r.conname, "
-                                                         "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
-                                                         "FROM pg_catalog.pg_constraint r\n"
-                                                         "WHERE r.conrelid = '%s' AND r.contype = 'x'\n"
-                                                         "ORDER BY 1",
-                                                         oid);
-                       result = PSQLexec(buf.data, false);
-                       if (!result)
-                               goto error_return;
-                       else
-                               tuples = PQntuples(result);
-
-                       if (tuples > 0)
-                       {
-                               printTableAddFooter(&cont, _("Exclusion constraints:"));
-                               for (i = 0; i < tuples; i++)
-                               {
-                                       /* untranslated contraint name and def */
-                                       printfPQExpBuffer(&buf, "    \"%s\" %s",
-                                                                         PQgetvalue(result, i, 0),
-                                                                         PQgetvalue(result, i, 1));
-
-                                       printTableAddFooter(&cont, buf.data);
-                               }
-                       }
-                       PQclear(result);
-               }
-
                /* print foreign-key constraints (there are none if no triggers) */
                if (tableinfo.hastriggers)
                {
@@ -1699,7 +1782,7 @@ describeOneTableDetails(const char *schemaname,
                                                          "SELECT conname,\n"
                                 "  pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
                                                          "FROM pg_catalog.pg_constraint r\n"
-                                       "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1",
+                                       "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
                                                          oid);
                        result = PSQLexec(buf.data, false);
                        if (!result)
@@ -1730,7 +1813,7 @@ describeOneTableDetails(const char *schemaname,
                                                   "SELECT conname, conrelid::pg_catalog.regclass,\n"
                                 "  pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
                                                          "FROM pg_catalog.pg_constraint c\n"
-                                  "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
+                                  "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
                                                          oid);
                        result = PSQLexec(buf.data, false);
                        if (!result)
@@ -1763,7 +1846,7 @@ describeOneTableDetails(const char *schemaname,
                                                                  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
                                                                  "ev_enabled\n"
                                                                  "FROM pg_catalog.pg_rewrite r\n"
-                                                                 "WHERE r.ev_class = '%s' ORDER BY 1",
+                                                                 "WHERE r.ev_class = '%s' ORDER BY 1;",
                                                                  oid);
                        }
                        else
@@ -1772,7 +1855,7 @@ describeOneTableDetails(const char *schemaname,
                                                                  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
                                                                  "'O'::char AS ev_enabled\n"
                                                                  "FROM pg_catalog.pg_rewrite r\n"
-                                                                 "WHERE r.ev_class = '%s' ORDER BY 1",
+                                                                 "WHERE r.ev_class = '%s' ORDER BY 1;",
                                                                  oid);
                        }
                        result = PSQLexec(buf.data, false);
@@ -1848,123 +1931,163 @@ describeOneTableDetails(const char *schemaname,
                        }
                        PQclear(result);
                }
+       }
 
-               /* print triggers (but only user-defined triggers) */
-               if (tableinfo.hastriggers)
-               {
-                       printfPQExpBuffer(&buf,
-                                                         "SELECT t.tgname, "
-                                                         "pg_catalog.pg_get_triggerdef(t.oid), "
-                                                         "t.tgenabled\n"
-                                                         "FROM pg_catalog.pg_trigger t\n"
-                                                         "WHERE t.tgrelid = '%s' AND ",
-                                                         oid);
-                       if (pset.sversion >= 80500)
-                               appendPQExpBuffer(&buf, "NOT t.tgisinternal");
-                       else if (pset.sversion >= 80300)
-                               appendPQExpBuffer(&buf, "t.tgconstraint = 0");
-                       else
-                               appendPQExpBuffer(&buf,
-                                                                 "(NOT tgisconstraint "
-                                                                 " OR NOT EXISTS"
-                                                                 "  (SELECT 1 FROM pg_catalog.pg_depend d "
-                                                                 "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
-                                                                 "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
-                       appendPQExpBuffer(&buf, "\nORDER BY 1");
+       /*
+        * Print triggers next, if any (but only user-defined triggers).  This
+        * could apply to either a table or a view.
+        */
+       if (tableinfo.hastriggers)
+       {
+               PGresult   *result;
+               int                     tuples;
 
-                       result = PSQLexec(buf.data, false);
-                       if (!result)
-                               goto error_return;
-                       else
-                               tuples = PQntuples(result);
+               printfPQExpBuffer(&buf,
+                                                 "SELECT t.tgname, "
+                                                 "pg_catalog.pg_get_triggerdef(t.oid%s), "
+                                                 "t.tgenabled\n"
+                                                 "FROM pg_catalog.pg_trigger t\n"
+                                                 "WHERE t.tgrelid = '%s' AND ",
+                                                 (pset.sversion >= 90000 ? ", true" : ""),
+                                                 oid);
+               if (pset.sversion >= 90000)
+                       appendPQExpBuffer(&buf, "NOT t.tgisinternal");
+               else if (pset.sversion >= 80300)
+                       appendPQExpBuffer(&buf, "t.tgconstraint = 0");
+               else
+                       appendPQExpBuffer(&buf,
+                                                         "(NOT tgisconstraint "
+                                                         " OR NOT EXISTS"
+                                                         "  (SELECT 1 FROM pg_catalog.pg_depend d "
+                                                         "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
+                                                         "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
+               appendPQExpBuffer(&buf, "\nORDER BY 1;");
 
-                       if (tuples > 0)
-                       {
-                               bool            have_heading;
-                               int                     category;
+               result = PSQLexec(buf.data, false);
+               if (!result)
+                       goto error_return;
+               else
+                       tuples = PQntuples(result);
 
-                               /*
-                                * split the output into 4 different categories. Enabled
-                                * triggers, disabled triggers and the two special ALWAYS and
-                                * REPLICA configurations.
-                                */
-                               for (category = 0; category < 4; category++)
+               if (tuples > 0)
+               {
+                       bool            have_heading;
+                       int                     category;
+
+                       /*
+                        * split the output into 4 different categories. Enabled triggers,
+                        * disabled triggers and the two special ALWAYS and REPLICA
+                        * configurations.
+                        */
+                       for (category = 0; category < 4; category++)
+                       {
+                               have_heading = false;
+                               for (i = 0; i < tuples; i++)
                                {
-                                       have_heading = false;
-                                       for (i = 0; i < tuples; i++)
+                                       bool            list_trigger;
+                                       const char *tgdef;
+                                       const char *usingpos;
+                                       const char *tgenabled;
+
+                                       /*
+                                        * Check if this trigger falls into the current category
+                                        */
+                                       tgenabled = PQgetvalue(result, i, 2);
+                                       list_trigger = false;
+                                       switch (category)
+                                       {
+                                               case 0:
+                                                       if (*tgenabled == 'O' || *tgenabled == 't')
+                                                               list_trigger = true;
+                                                       break;
+                                               case 1:
+                                                       if (*tgenabled == 'D' || *tgenabled == 'f')
+                                                               list_trigger = true;
+                                                       break;
+                                               case 2:
+                                                       if (*tgenabled == 'A')
+                                                               list_trigger = true;
+                                                       break;
+                                               case 3:
+                                                       if (*tgenabled == 'R')
+                                                               list_trigger = true;
+                                                       break;
+                                       }
+                                       if (list_trigger == false)
+                                               continue;
+
+                                       /* Print the category heading once */
+                                       if (have_heading == false)
                                        {
-                                               bool            list_trigger;
-                                               const char *tgdef;
-                                               const char *usingpos;
-                                               const char *tgenabled;
-
-                                               /*
-                                                * Check if this trigger falls into the current
-                                                * category
-                                                */
-                                               tgenabled = PQgetvalue(result, i, 2);
-                                               list_trigger = false;
                                                switch (category)
                                                {
                                                        case 0:
-                                                               if (*tgenabled == 'O' || *tgenabled == 't')
-                                                                       list_trigger = true;
+                                                               printfPQExpBuffer(&buf, _("Triggers:"));
                                                                break;
                                                        case 1:
-                                                               if (*tgenabled == 'D' || *tgenabled == 'f')
-                                                                       list_trigger = true;
+                                                               printfPQExpBuffer(&buf, _("Disabled triggers:"));
                                                                break;
                                                        case 2:
-                                                               if (*tgenabled == 'A')
-                                                                       list_trigger = true;
+                                                               printfPQExpBuffer(&buf, _("Triggers firing always:"));
                                                                break;
                                                        case 3:
-                                                               if (*tgenabled == 'R')
-                                                                       list_trigger = true;
+                                                               printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
                                                                break;
-                                               }
-                                               if (list_trigger == false)
-                                                       continue;
-
-                                               /* Print the category heading once */
-                                               if (have_heading == false)
-                                               {
-                                                       switch (category)
-                                                       {
-                                                               case 0:
-                                                                       printfPQExpBuffer(&buf, _("Triggers:"));
-                                                                       break;
-                                                               case 1:
-                                                                       printfPQExpBuffer(&buf, _("Disabled triggers:"));
-                                                                       break;
-                                                               case 2:
-                                                                       printfPQExpBuffer(&buf, _("Triggers firing always:"));
-                                                                       break;
-                                                               case 3:
-                                                                       printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
-                                                                       break;
 
-                                                       }
-                                                       printTableAddFooter(&cont, buf.data);
-                                                       have_heading = true;
                                                }
-
-                                               /* Everything after "TRIGGER" is echoed verbatim */
-                                               tgdef = PQgetvalue(result, i, 1);
-                                               usingpos = strstr(tgdef, " TRIGGER ");
-                                               if (usingpos)
-                                                       tgdef = usingpos + 9;
-
-                                               printfPQExpBuffer(&buf, "    %s", tgdef);
                                                printTableAddFooter(&cont, buf.data);
+                                               have_heading = true;
                                        }
+
+                                       /* Everything after "TRIGGER" is echoed verbatim */
+                                       tgdef = PQgetvalue(result, i, 1);
+                                       usingpos = strstr(tgdef, " TRIGGER ");
+                                       if (usingpos)
+                                               tgdef = usingpos + 9;
+
+                                       printfPQExpBuffer(&buf, "    %s", tgdef);
+                                       printTableAddFooter(&cont, buf.data);
                                }
                        }
+               }
+               PQclear(result);
+       }
+
+       /*
+        * Finish printing the footer information about a table.
+        */
+       if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
+       {
+               PGresult   *result;
+               int                     tuples;
+
+               /* print foreign server name */
+               if (tableinfo.relkind == 'f')
+               {
+                       /* Footer information about foreign table */
+                       printfPQExpBuffer(&buf,
+                                                         "SELECT s.srvname\n"
+                                                         "FROM pg_catalog.pg_foreign_table f,\n"
+                                                         "     pg_catalog.pg_foreign_server s\n"
+                                                         "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
+                                                         oid);
+                       result = PSQLexec(buf.data, false);
+                       if (!result)
+                               goto error_return;
+                       else if (PQntuples(result) != 1)
+                       {
+                               PQclear(result);
+                               goto error_return;
+                       }
+
+                       printfPQExpBuffer(&buf, "Server: %s",
+                                                         PQgetvalue(result, 0, 0));
+                       printTableAddFooter(&cont, buf.data);
                        PQclear(result);
                }
 
                /* print inherited tables */
-               printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '%s' ORDER BY inhseqno", oid);
+               printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '%s' ORDER BY inhseqno;", oid);
 
                result = PSQLexec(buf.data, false);
                if (!result)
@@ -2030,6 +2153,13 @@ describeOneTableDetails(const char *schemaname,
                }
                PQclear(result);
 
+               /* Table type */
+               if (tableinfo.reloftype)
+               {
+                       printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
+                       printTableAddFooter(&cont, buf.data);
+               }
+
                /* OIDs and options */
                if (verbose)
                {
@@ -2119,7 +2249,7 @@ add_tablespace_footer(printTableContent *const cont, char relkind,
                        initPQExpBuffer(&buf);
                        printfPQExpBuffer(&buf,
                                                          "SELECT spcname FROM pg_catalog.pg_tablespace\n"
-                                                         "WHERE oid = '%u'", tablespace);
+                                                         "WHERE oid = '%u';", tablespace);
                        result = PSQLexec(buf.data, false);
                        if (!result)
                                return;
@@ -2190,6 +2320,10 @@ describeRoles(const char *pattern, bool verbose)
                        appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
                        ncols++;
                }
+               if (pset.sversion >= 90100)
+               {
+                       appendPQExpBufferStr(&buf, "\n, r.rolreplication");
+               }
 
                appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
 
@@ -2231,7 +2365,7 @@ describeRoles(const char *pattern, bool verbose)
 
        for (i = 0; i < nrows; i++)
        {
-               printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
+               printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
 
                resetPQExpBuffer(&buf);
                if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
@@ -2249,6 +2383,10 @@ describeRoles(const char *pattern, bool verbose)
                if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
                        add_role_attribute(&buf, _("Cannot login"));
 
+               if (pset.sversion >= 90100)
+                       if (strcmp(PQgetvalue(res, i, (verbose ? 9 : 8)), "t") == 0)
+                               add_role_attribute(&buf, _("Replication"));
+
                conns = atoi(PQgetvalue(res, i, 6));
                if (conns >= 0)
                {
@@ -2266,12 +2404,12 @@ describeRoles(const char *pattern, bool verbose)
 
                attr[i] = pg_strdup(buf.data);
 
-               printTableAddCell(&cont, attr[i], false);
+               printTableAddCell(&cont, attr[i], false, false);
 
-               printTableAddCell(&cont, PQgetvalue(res, i, 7), false);
+               printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
 
                if (verbose && pset.sversion >= 80200)
-                       printTableAddCell(&cont, PQgetvalue(res, i, 8), false);
+                       printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
        }
        termPQExpBuffer(&buf);
 
@@ -2301,31 +2439,31 @@ add_role_attribute(PQExpBuffer buf, const char *const str)
 bool
 listDbRoleSettings(const char *pattern, const char *pattern2)
 {
-       PQExpBufferData buf;
-       PGresult           *res;
+       PQExpBufferData buf;
+       PGresult   *res;
        printQueryOpt myopt = pset.popt;
 
        initPQExpBuffer(&buf);
 
-       if (pset.sversion >= 80500)
+       if (pset.sversion >= 90000)
        {
-               bool    havewhere;
+               bool            havewhere;
 
                printfPQExpBuffer(&buf, "SELECT rolname AS role, datname AS database,\n"
-                                                 "pg_catalog.array_to_string(setconfig, E'\\n') AS settings\n"
+                               "pg_catalog.array_to_string(setconfig, E'\\n') AS settings\n"
                                                  "FROM pg_db_role_setting AS s\n"
-                                                 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
+                                  "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
                                                  "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n");
                havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
-                                                                                 NULL, "pg_roles.rolname", NULL, NULL);
+                                                                          NULL, "pg_roles.rolname", NULL, NULL);
                processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
                                                          NULL, "pg_database.datname", NULL, NULL);
-               appendPQExpBufferStr(&buf, "ORDER BY role, database");
+               appendPQExpBufferStr(&buf, "ORDER BY role, database;");
        }
        else
        {
                fprintf(pset.queryFout,
-                               _("No per-database role settings support in this server version.\n"));
+               _("No per-database role settings support in this server version.\n"));
                return false;
        }
 
@@ -2365,8 +2503,9 @@ listDbRoleSettings(const char *pattern, const char *pattern2)
  * i - indexes
  * v - views
  * s - sequences
+ * E - foreign table (Note: different from 'f', the relkind value)
  * (any order of the above is fine)
- * If tabtypes is empty, we default to \dtvs.
+ * If tabtypes is empty, we default to \dtvsE.
  */
 bool
 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
@@ -2375,14 +2514,15 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
        bool            showIndexes = strchr(tabtypes, 'i') != NULL;
        bool            showViews = strchr(tabtypes, 'v') != NULL;
        bool            showSeq = strchr(tabtypes, 's') != NULL;
+       bool            showForeign = strchr(tabtypes, 'E') != NULL;
 
        PQExpBufferData buf;
        PGresult   *res;
        printQueryOpt myopt = pset.popt;
        static const bool translate_columns[] = {false, false, true, false, false, false, false};
 
-       if (!(showTables || showIndexes || showViews || showSeq))
-               showTables = showViews = showSeq = true;
+       if (!(showTables || showIndexes || showViews || showSeq || showForeign))
+               showTables = showViews = showSeq = showForeign = true;
 
        initPQExpBuffer(&buf);
 
@@ -2393,7 +2533,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
        printfPQExpBuffer(&buf,
                                          "SELECT n.nspname as \"%s\",\n"
                                          "  c.relname as \"%s\",\n"
-                                         "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' END as \"%s\",\n"
+                                         "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
                                          "  pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
                                          gettext_noop("Schema"),
                                          gettext_noop("Name"),
@@ -2402,6 +2542,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
                                          gettext_noop("index"),
                                          gettext_noop("sequence"),
                                          gettext_noop("special"),
+                                         gettext_noop("foreign table"),
                                          gettext_noop("Type"),
                                          gettext_noop("Owner"));
 
@@ -2410,14 +2551,25 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
                                                  ",\n c2.relname as \"%s\"",
                                                  gettext_noop("Table"));
 
-       if (verbose && pset.sversion >= 80100)
-               appendPQExpBuffer(&buf,
-                                                 ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
-                                                 gettext_noop("Size"));
        if (verbose)
+       {
+               /*
+                * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
+                * size of a table, including FSM, VM and TOAST tables.
+                */
+               if (pset.sversion >= 90000)
+                       appendPQExpBuffer(&buf,
+                                                         ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
+                                                         gettext_noop("Size"));
+               else if (pset.sversion >= 80100)
+                       appendPQExpBuffer(&buf,
+                                                         ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
+                                                         gettext_noop("Size"));
+
                appendPQExpBuffer(&buf,
                          ",\n  pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
                                                  gettext_noop("Description"));
+       }
 
        appendPQExpBuffer(&buf,
                                          "\nFROM pg_catalog.pg_class c"
@@ -2439,6 +2591,9 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
        if (showSystem || pattern)
                appendPQExpBuffer(&buf, "'s',");                /* was RELKIND_SPECIAL in <=
                                                                                                 * 8.1 */
+       if (showForeign)
+               appendPQExpBuffer(&buf, "'f',");
+
        appendPQExpBuffer(&buf, "''");          /* dummy */
        appendPQExpBuffer(&buf, ")\n");
 
@@ -2488,12 +2643,12 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 
 
 /*
- * \dD
+ * \dL
  *
- * Describes domains.
+ * Describes languages.
  */
 bool
-listDomains(const char *pattern, bool showSystem)
+listLanguages(const char *pattern, bool verbose, bool showSystem)
 {
        PQExpBufferData buf;
        PGresult   *res;
@@ -2502,35 +2657,49 @@ listDomains(const char *pattern, bool showSystem)
        initPQExpBuffer(&buf);
 
        printfPQExpBuffer(&buf,
-                                         "SELECT n.nspname as \"%s\",\n"
-                                         "       t.typname as \"%s\",\n"
-        "       pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
-                                         "       CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
-       "            WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
-                                         "            WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
-                                         "            ELSE ''\n"
-                                         "       END as \"%s\",\n"
-                                         "       pg_catalog.array_to_string(ARRAY(\n"
-                                         "         SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
-                                         "       ), ' ') as \"%s\"\n"
-                                         "FROM pg_catalog.pg_type t\n"
-          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
-                                         "WHERE t.typtype = 'd'\n",
-                                         gettext_noop("Schema"),
-                                         gettext_noop("Name"),
-                                         gettext_noop("Type"),
-                                         gettext_noop("Modifier"),
-                                         gettext_noop("Check"));
+                                         "SELECT l.lanname AS \"%s\",\n",
+                                         gettext_noop("Name"));
+       if (pset.sversion >= 80300)
+               appendPQExpBuffer(&buf,
+                               "       pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
+                                                 gettext_noop("Owner"));
+
+       appendPQExpBuffer(&buf,
+                                         "       l.lanpltrusted AS \"%s\"",
+                                         gettext_noop("Trusted"));
+
+       if (verbose)
+       {
+               appendPQExpBuffer(&buf,
+                                                 ",\n       NOT l.lanispl AS \"%s\",\n"
+                                                 "       l.lanplcallfoid::regprocedure AS \"%s\",\n"
+                                  "       l.lanvalidator::regprocedure AS \"%s\",\n       ",
+                                                 gettext_noop("Internal Language"),
+                                                 gettext_noop("Call Handler"),
+                                                 gettext_noop("Validator"));
+               if (pset.sversion >= 90000)
+                       appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n       ",
+                                                         gettext_noop("Inline Handler"));
+               printACLColumn(&buf, "l.lanacl");
+       }
+
+       appendPQExpBuffer(&buf,
+                                         ",\n       d.description AS \"%s\""
+                                         "\nFROM pg_catalog.pg_language l\n"
+                                         "LEFT JOIN pg_catalog.pg_description d\n"
+                                         "  ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
+                                         "  AND d.objsubid = 0\n",
+                                         gettext_noop("Description"));
+
+       if (pattern)
+               processSQLNamePattern(pset.db, &buf, pattern, false, false,
+                                                         NULL, "l.lanname", NULL, NULL);
 
        if (!showSystem && !pattern)
-               appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
-                                                 "      AND n.nspname <> 'information_schema'\n");
+               appendPQExpBuffer(&buf, "WHERE l.lanplcallfoid != 0\n");
 
-       processSQLNamePattern(pset.db, &buf, pattern, true, false,
-                                                 "n.nspname", "t.typname", NULL,
-                                                 "pg_catalog.pg_type_is_visible(t.oid)");
 
-       appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
+       appendPQExpBuffer(&buf, "ORDER BY 1;");
 
        res = PSQLexec(buf.data, false);
        termPQExpBuffer(&buf);
@@ -2538,7 +2707,7 @@ listDomains(const char *pattern, bool showSystem)
                return false;
 
        myopt.nullPrint = NULL;
-       myopt.title = _("List of domains");
+       myopt.title = _("List of languages");
        myopt.translate_header = true;
 
        printQuery(res, &myopt, pset.queryFout, pset.logfile);
@@ -2547,41 +2716,137 @@ listDomains(const char *pattern, bool showSystem)
        return true;
 }
 
+
 /*
- * \dc
+ * \dD
  *
- * Describes conversions.
+ * Describes domains.
  */
 bool
-listConversions(const char *pattern, bool showSystem)
+listDomains(const char *pattern, bool verbose, bool showSystem)
 {
        PQExpBufferData buf;
        PGresult   *res;
        printQueryOpt myopt = pset.popt;
-       static const bool translate_columns[] = {false, false, false, false, true};
 
        initPQExpBuffer(&buf);
 
        printfPQExpBuffer(&buf,
-                                         "SELECT n.nspname AS \"%s\",\n"
-                                         "       c.conname AS \"%s\",\n"
-          "       pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
-               "       pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
-                                         "       CASE WHEN c.condefault THEN '%s'\n"
-                                         "       ELSE '%s' END AS \"%s\"\n"
-                          "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
-                                         "WHERE n.oid = c.connamespace\n",
+                                         "SELECT n.nspname as \"%s\",\n"
+                                         "       t.typname as \"%s\",\n"
+        "       pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
+                                         "       TRIM(LEADING\n",
                                          gettext_noop("Schema"),
                                          gettext_noop("Name"),
-                                         gettext_noop("Source"),
-                                         gettext_noop("Destination"),
-                                         gettext_noop("yes"), gettext_noop("no"),
-                                         gettext_noop("Default?"));
+                                         gettext_noop("Type"));
+       if (pset.sversion >= 90100)
+               appendPQExpBuffer(&buf,
+                                                 "            COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
+                                                 "                      WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
+       appendPQExpBuffer(&buf,
+          "            CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
+                                         "            CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
+                                         "       ) as \"%s\",\n"
+                                         "       pg_catalog.array_to_string(ARRAY(\n"
+                                         "         SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
+                                         "       ), ' ') as \"%s\"",
+                                         gettext_noop("Modifier"),
+                                         gettext_noop("Check"));
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 ",\n       d.description as \"%s\"",
+                                                 gettext_noop("Description"));
+
+       appendPQExpBuffer(&buf,
+                                         "\nFROM pg_catalog.pg_type t\n"
+          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 "     LEFT JOIN pg_catalog.pg_description d "
+                                                 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
+                                                 "AND d.objsubid = 0\n");
+
+       appendPQExpBuffer(&buf, "WHERE t.typtype = 'd'\n");
 
        if (!showSystem && !pattern)
                appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
                                                  "      AND n.nspname <> 'information_schema'\n");
 
+       processSQLNamePattern(pset.db, &buf, pattern, true, false,
+                                                 "n.nspname", "t.typname", NULL,
+                                                 "pg_catalog.pg_type_is_visible(t.oid)");
+
+       appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       myopt.title = _("List of domains");
+       myopt.translate_header = true;
+
+       printQuery(res, &myopt, pset.queryFout, pset.logfile);
+
+       PQclear(res);
+       return true;
+}
+
+/*
+ * \dc
+ *
+ * Describes conversions.
+ */
+bool
+listConversions(const char *pattern, bool verbose, bool showSystem)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       printQueryOpt myopt = pset.popt;
+       static const bool translate_columns[] = {false, false, false, false, true};
+
+       initPQExpBuffer(&buf);
+
+       printfPQExpBuffer(&buf,
+                                         "SELECT n.nspname AS \"%s\",\n"
+                                         "       c.conname AS \"%s\",\n"
+          "       pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
+               "       pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
+                                         "       CASE WHEN c.condefault THEN '%s'\n"
+                                         "       ELSE '%s' END AS \"%s\"",
+                                         gettext_noop("Schema"),
+                                         gettext_noop("Name"),
+                                         gettext_noop("Source"),
+                                         gettext_noop("Destination"),
+                                         gettext_noop("yes"), gettext_noop("no"),
+                                         gettext_noop("Default?"));
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 ",\n       d.description AS \"%s\"",
+                                                 gettext_noop("Description"));
+
+       appendPQExpBuffer(&buf,
+                                         "\nFROM pg_catalog.pg_conversion c\n"
+                                         "     JOIN pg_catalog.pg_namespace n "
+                                         "ON n.oid = c.connamespace\n");
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 "LEFT JOIN pg_catalog.pg_description d "
+                                                 "ON d.classoid = c.tableoid\n"
+                                                 "          AND d.objoid = c.oid "
+                                                 "AND d.objsubid = 0\n");
+
+       appendPQExpBuffer(&buf, "WHERE true\n");
+
+       if (!showSystem && !pattern)
+               appendPQExpBuffer(&buf, "  AND n.nspname <> 'pg_catalog'\n"
+                                                 "  AND n.nspname <> 'information_schema'\n");
+
        processSQLNamePattern(pset.db, &buf, pattern, true, false,
                                                  "n.nspname", "c.conname", NULL,
                                                  "pg_catalog.pg_conversion_is_visible(c.oid)");
@@ -2610,7 +2875,7 @@ listConversions(const char *pattern, bool showSystem)
  * Describes casts.
  */
 bool
-listCasts(const char *pattern)
+listCasts(const char *pattern, bool verbose)
 {
        PQExpBufferData buf;
        PGresult   *res;
@@ -2634,7 +2899,21 @@ listCasts(const char *pattern)
                                          "       CASE WHEN c.castcontext = 'e' THEN '%s'\n"
                                          "            WHEN c.castcontext = 'a' THEN '%s'\n"
                                          "            ELSE '%s'\n"
-                                         "       END as \"%s\"\n"
+                                         "       END as \"%s\"",
+                                         gettext_noop("Source type"),
+                                         gettext_noop("Target type"),
+                                         gettext_noop("Function"),
+                                         gettext_noop("no"),
+                                         gettext_noop("in assignment"),
+                                         gettext_noop("yes"),
+                                         gettext_noop("Implicit?"));
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 ",\n       d.description AS \"%s\"\n",
+                                                 gettext_noop("Description"));
+
+       appendPQExpBuffer(&buf,
                                 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
                                          "     ON c.castfunc = p.oid\n"
                                          "     LEFT JOIN pg_catalog.pg_type ts\n"
@@ -2644,13 +2923,15 @@ listCasts(const char *pattern)
                                          "     LEFT JOIN pg_catalog.pg_type tt\n"
                                          "     ON c.casttarget = tt.oid\n"
                                          "     LEFT JOIN pg_catalog.pg_namespace nt\n"
-                                         "     ON nt.oid = tt.typnamespace\n"
-                                         "WHERE (true",
-                                         gettext_noop("Source type"),
-                                         gettext_noop("Target type"),
-                                         gettext_noop("Function"),
-         gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
-                                         gettext_noop("Implicit?"));
+                                         "     ON nt.oid = tt.typnamespace\n");
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 "     LEFT JOIN pg_catalog.pg_description d\n"
+                                                 "     ON d.classoid = c.tableoid AND d.objoid = "
+                                                 "c.oid AND d.objsubid = 0\n");
+
+       appendPQExpBuffer(&buf, "WHERE ( (true");
 
        /*
         * Match name pattern against either internal or external name of either
@@ -2668,7 +2949,7 @@ listCasts(const char *pattern)
                                                  "pg_catalog.format_type(tt.oid, NULL)",
                                                  "pg_catalog.pg_type_is_visible(tt.oid)");
 
-       appendPQExpBuffer(&buf, ")\nORDER BY 1, 2;");
+       appendPQExpBuffer(&buf, ") )\nORDER BY 1, 2;");
 
        res = PSQLexec(buf.data, false);
        termPQExpBuffer(&buf);
@@ -2686,13 +2967,81 @@ listCasts(const char *pattern)
        return true;
 }
 
+/*
+ * \dO
+ *
+ * Describes collations.
+ */
+bool
+listCollations(const char *pattern, bool verbose, bool showSystem)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       printQueryOpt myopt = pset.popt;
+       static const bool translate_columns[] = {false, false, false, false, false};
+
+       initPQExpBuffer(&buf);
+
+       printfPQExpBuffer(&buf,
+                                         "SELECT n.nspname AS \"%s\",\n"
+                                         "       c.collname AS \"%s\",\n"
+                                         "       c.collcollate AS \"%s\",\n"
+                                         "       c.collctype AS \"%s\"",
+                                         gettext_noop("Schema"),
+                                         gettext_noop("Name"),
+                                         gettext_noop("Collate"),
+                                         gettext_noop("Ctype"));
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 ",\n       pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
+                                                 gettext_noop("Description"));
+
+       appendPQExpBuffer(&buf,
+                         "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
+                                         "WHERE n.oid = c.collnamespace\n");
+
+       if (!showSystem && !pattern)
+               appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
+                                                 "      AND n.nspname <> 'information_schema'\n");
+
+       /*
+        * Hide collations that aren't usable in the current database's encoding.
+        * If you think to change this, note that pg_collation_is_visible rejects
+        * unusable collations, so you will need to hack name pattern processing
+        * somehow to avoid inconsistent behavior.
+        */
+       appendPQExpBuffer(&buf, "      AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
+
+       processSQLNamePattern(pset.db, &buf, pattern, true, false,
+                                                 "n.nspname", "c.collname", NULL,
+                                                 "pg_catalog.pg_collation_is_visible(c.oid)");
+
+       appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       myopt.title = _("List of collations");
+       myopt.translate_header = true;
+       myopt.translate_columns = translate_columns;
+
+       printQuery(res, &myopt, pset.queryFout, pset.logfile);
+
+       PQclear(res);
+       return true;
+}
+
 /*
  * \dn
  *
  * Describes schemas (namespaces)
  */
 bool
-listSchemas(const char *pattern, bool verbose)
+listSchemas(const char *pattern, bool verbose, bool showSystem)
 {
        PQExpBufferData buf;
        PGresult   *res;
@@ -2715,11 +3064,14 @@ listSchemas(const char *pattern, bool verbose)
        }
 
        appendPQExpBuffer(&buf,
-                                         "\nFROM pg_catalog.pg_namespace n\n"
-                                         "WHERE        (n.nspname !~ '^pg_temp_' OR\n"
-                  "             n.nspname = (pg_catalog.current_schemas(true))[1])\n");                /* temp schema is first */
+                                         "\nFROM pg_catalog.pg_namespace n\n");
 
-       processSQLNamePattern(pset.db, &buf, pattern, true, false,
+       if (!showSystem && !pattern)
+               appendPQExpBuffer(&buf,
+               "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
+
+       processSQLNamePattern(pset.db, &buf, pattern,
+                                                 !showSystem && !pattern, false,
                                                  NULL, "n.nspname", NULL,
                                                  NULL);
 
@@ -2905,7 +3257,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
                                          "   p.prslextype::pg_catalog.regproc, \n"
                                  "   pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
                                          " FROM pg_catalog.pg_ts_parser p \n"
-                                         " WHERE p.oid = '%s' \n",
+                                         " WHERE p.oid = '%s';",
                                          gettext_noop("Start parse"),
                                          gettext_noop("Method"),
                                          gettext_noop("Function"),
@@ -3266,7 +3618,7 @@ describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
         "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
                                          "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
                                          "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
-                                         "ORDER BY 1",
+                                         "ORDER BY 1;",
                                          gettext_noop("Token"),
                                          gettext_noop("Dictionaries"),
                                          oid);
@@ -3329,10 +3681,15 @@ listForeignDataWrappers(const char *pattern, bool verbose)
        initPQExpBuffer(&buf);
        printfPQExpBuffer(&buf,
                                          "SELECT fdwname AS \"%s\",\n"
-                                         "  pg_catalog.pg_get_userbyid(fdwowner) AS \"%s\",\n"
-                                         "  fdwvalidator::pg_catalog.regproc AS \"%s\"",
+                                         "  pg_catalog.pg_get_userbyid(fdwowner) AS \"%s\",\n",
                                          gettext_noop("Name"),
-                                         gettext_noop("Owner"),
+                                         gettext_noop("Owner"));
+       if (pset.sversion >= 90100)
+               appendPQExpBuffer(&buf,
+                                                 "  fdwhandler::pg_catalog.regproc AS \"%s\",\n",
+                                                 gettext_noop("Handler"));
+       appendPQExpBuffer(&buf,
+                                         "  fdwvalidator::pg_catalog.regproc AS \"%s\"",
                                          gettext_noop("Validator"));
 
        if (verbose)
@@ -3485,6 +3842,228 @@ listUserMappings(const char *pattern, bool verbose)
        return true;
 }
 
+/*
+ * \det
+ *
+ * Describes foreign tables.
+ */
+bool
+listForeignTables(const char *pattern, bool verbose)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       printQueryOpt myopt = pset.popt;
+
+       if (pset.sversion < 90100)
+       {
+               fprintf(stderr, _("The server (version %d.%d) does not support foreign tables.\n"),
+                               pset.sversion / 10000, (pset.sversion / 100) % 100);
+               return true;
+       }
+
+       initPQExpBuffer(&buf);
+       printfPQExpBuffer(&buf,
+                                         "SELECT n.nspname AS \"%s\",\n"
+                                         "  c.relname AS \"%s\",\n"
+                                         "  s.srvname AS \"%s\"",
+                                         gettext_noop("Schema"),
+                                         gettext_noop("Table"),
+                                         gettext_noop("Server"));
+
+       if (verbose)
+               appendPQExpBuffer(&buf,
+                                                 ",\n  ft.ftoptions AS \"%s\"",
+                                                 gettext_noop("Options"));
+
+       appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_table ft,");
+       appendPQExpBuffer(&buf, "\n pg_catalog.pg_class c,");
+       appendPQExpBuffer(&buf, "\n pg_catalog.pg_namespace n,");
+       appendPQExpBuffer(&buf, "\n pg_catalog.pg_foreign_server s\n");
+       appendPQExpBuffer(&buf, "\nWHERE c.oid = ft.ftrelid");
+       appendPQExpBuffer(&buf, "\nAND s.oid = ft.ftserver\n");
+       appendPQExpBuffer(&buf, "\nAND n.oid = c.relnamespace\n");
+
+       processSQLNamePattern(pset.db, &buf, pattern, true, false,
+                                                 NULL, "n.nspname", "c.relname", NULL);
+
+       appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       myopt.title = _("List of foreign tables");
+       myopt.translate_header = true;
+
+       printQuery(res, &myopt, pset.queryFout, pset.logfile);
+
+       PQclear(res);
+       return true;
+}
+
+/*
+ * \dx
+ *
+ * Briefly describes installed extensions.
+ */
+bool
+listExtensions(const char *pattern)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       printQueryOpt myopt = pset.popt;
+
+       if (pset.sversion < 90100)
+       {
+               fprintf(stderr, _("The server (version %d.%d) does not support extensions.\n"),
+                               pset.sversion / 10000, (pset.sversion / 100) % 100);
+               return true;
+       }
+
+       initPQExpBuffer(&buf);
+       printfPQExpBuffer(&buf,
+                                         "SELECT e.extname AS \"%s\", "
+        "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
+                                         "FROM pg_catalog.pg_extension e "
+                        "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
+                                "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
+                "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
+                                         gettext_noop("Name"),
+                                         gettext_noop("Version"),
+                                         gettext_noop("Schema"),
+                                         gettext_noop("Description"));
+
+       processSQLNamePattern(pset.db, &buf, pattern,
+                                                 false, false,
+                                                 NULL, "e.extname", NULL,
+                                                 NULL);
+
+       appendPQExpBuffer(&buf, "ORDER BY 1;");
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       myopt.title = _("List of installed extensions");
+       myopt.translate_header = true;
+
+       printQuery(res, &myopt, pset.queryFout, pset.logfile);
+
+       PQclear(res);
+       return true;
+}
+
+/*
+ * \dx+
+ *
+ * List contents of installed extensions.
+ */
+bool
+listExtensionContents(const char *pattern)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       int                     i;
+
+       if (pset.sversion < 90100)
+       {
+               fprintf(stderr, _("The server (version %d.%d) does not support extensions.\n"),
+                               pset.sversion / 10000, (pset.sversion / 100) % 100);
+               return true;
+       }
+
+       initPQExpBuffer(&buf);
+       printfPQExpBuffer(&buf,
+                                         "SELECT e.extname, e.oid\n"
+                                         "FROM pg_catalog.pg_extension e\n");
+
+       processSQLNamePattern(pset.db, &buf, pattern,
+                                                 false, false,
+                                                 NULL, "e.extname", NULL,
+                                                 NULL);
+
+       appendPQExpBuffer(&buf, "ORDER BY 1;");
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       if (PQntuples(res) == 0)
+       {
+               if (!pset.quiet)
+               {
+                       if (pattern)
+                               fprintf(stderr, _("Did not find any extension named \"%s\".\n"),
+                                               pattern);
+                       else
+                               fprintf(stderr, _("Did not find any extensions.\n"));
+               }
+               PQclear(res);
+               return false;
+       }
+
+       for (i = 0; i < PQntuples(res); i++)
+       {
+               const char *extname;
+               const char *oid;
+
+               extname = PQgetvalue(res, i, 0);
+               oid = PQgetvalue(res, i, 1);
+
+               if (!listOneExtensionContents(extname, oid))
+               {
+                       PQclear(res);
+                       return false;
+               }
+               if (cancel_pressed)
+               {
+                       PQclear(res);
+                       return false;
+               }
+       }
+
+       PQclear(res);
+       return true;
+}
+
+static bool
+listOneExtensionContents(const char *extname, const char *oid)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       char            title[1024];
+       printQueryOpt myopt = pset.popt;
+
+       initPQExpBuffer(&buf);
+       printfPQExpBuffer(&buf,
+               "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
+                                         "FROM pg_catalog.pg_depend\n"
+                                         "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
+                                         "ORDER BY 1;",
+                                         gettext_noop("Object Description"),
+                                         oid);
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
+       myopt.title = title;
+       myopt.translate_header = true;
+
+       printQuery(res, &myopt, pset.queryFout, pset.logfile);
+
+       PQclear(res);
+       return true;
+}
+
 /*
  * printACLColumn
  *