]> granicus.if.org Git - postgresql/blob - src/bin/psql/describe.c
If a role has a password expiration date, show that in psql's \du output.
[postgresql] / src / bin / psql / describe.c
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Support for the various \d ("describe") commands.  Note that the current
5  * expectation is that all functions in this file will succeed when working
6  * with servers of versions 7.4 and up.  It's okay to omit irrelevant
7  * information for an old server, but not to fail outright.
8  *
9  * Copyright (c) 2000-2012, PostgreSQL Global Development Group
10  *
11  * src/bin/psql/describe.c
12  */
13 #include "postgres_fe.h"
14
15 #include <ctype.h>
16
17 #include "common.h"
18 #include "describe.h"
19 #include "dumputils.h"
20 #include "mbprint.h"
21 #include "print.h"
22 #include "settings.h"
23 #include "variables.h"
24
25
26 static bool describeOneTableDetails(const char *schemaname,
27                                                 const char *relationname,
28                                                 const char *oid,
29                                                 bool verbose);
30 static void add_tablespace_footer(printTableContent *const cont, char relkind,
31                                           Oid tablespace, const bool newline);
32 static void add_role_attribute(PQExpBuffer buf, const char *const str);
33 static bool listTSParsersVerbose(const char *pattern);
34 static bool describeOneTSParser(const char *oid, const char *nspname,
35                                         const char *prsname);
36 static bool listTSConfigsVerbose(const char *pattern);
37 static bool describeOneTSConfig(const char *oid, const char *nspname,
38                                         const char *cfgname,
39                                         const char *pnspname, const char *prsname);
40 static void printACLColumn(PQExpBuffer buf, const char *colname);
41 static bool listOneExtensionContents(const char *extname, const char *oid);
42
43
44 /*----------------
45  * Handlers for various slash commands displaying some sort of list
46  * of things in the database.
47  *
48  * Note: try to format the queries to look nice in -E output.
49  *----------------
50  */
51
52
53 /* \da
54  * Takes an optional regexp to select particular aggregates
55  */
56 bool
57 describeAggregates(const char *pattern, bool verbose, bool showSystem)
58 {
59         PQExpBufferData buf;
60         PGresult   *res;
61         printQueryOpt myopt = pset.popt;
62
63         initPQExpBuffer(&buf);
64
65         printfPQExpBuffer(&buf,
66                                           "SELECT n.nspname as \"%s\",\n"
67                                           "  p.proname AS \"%s\",\n"
68                                  "  pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
69                                           gettext_noop("Schema"),
70                                           gettext_noop("Name"),
71                                           gettext_noop("Result data type"));
72
73         if (pset.sversion >= 80200)
74                 appendPQExpBuffer(&buf,
75                                                   "  CASE WHEN p.pronargs = 0\n"
76                                                   "    THEN CAST('*' AS pg_catalog.text)\n"
77                                                   "    ELSE\n"
78                                                   "    pg_catalog.array_to_string(ARRAY(\n"
79                                                   "      SELECT\n"
80                                  "        pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
81                                                   "      FROM\n"
82                                                   "        pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
83                                                   "    ), ', ')\n"
84                                                   "  END AS \"%s\",\n",
85                                                   gettext_noop("Argument data types"));
86         else
87                 appendPQExpBuffer(&buf,
88                          "  pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
89                                                   gettext_noop("Argument data types"));
90
91         appendPQExpBuffer(&buf,
92                                  "  pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
93                                           "FROM pg_catalog.pg_proc p\n"
94            "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
95                                           "WHERE p.proisagg\n",
96                                           gettext_noop("Description"));
97
98         if (!showSystem && !pattern)
99                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
100                                                   "      AND n.nspname <> 'information_schema'\n");
101
102         processSQLNamePattern(pset.db, &buf, pattern, true, false,
103                                                   "n.nspname", "p.proname", NULL,
104                                                   "pg_catalog.pg_function_is_visible(p.oid)");
105
106         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
107
108         res = PSQLexec(buf.data, false);
109         termPQExpBuffer(&buf);
110         if (!res)
111                 return false;
112
113         myopt.nullPrint = NULL;
114         myopt.title = _("List of aggregate functions");
115         myopt.translate_header = true;
116
117         printQuery(res, &myopt, pset.queryFout, pset.logfile);
118
119         PQclear(res);
120         return true;
121 }
122
123 /* \db
124  * Takes an optional regexp to select particular tablespaces
125  */
126 bool
127 describeTablespaces(const char *pattern, bool verbose)
128 {
129         PQExpBufferData buf;
130         PGresult   *res;
131         printQueryOpt myopt = pset.popt;
132
133         if (pset.sversion < 80000)
134         {
135                 fprintf(stderr, _("The server (version %d.%d) does not support tablespaces.\n"),
136                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
137                 return true;
138         }
139
140         initPQExpBuffer(&buf);
141
142         if (pset.sversion >= 90200)
143                 printfPQExpBuffer(&buf,
144                                                   "SELECT spcname AS \"%s\",\n"
145                                                   "  pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
146                                                   "  pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
147                                                   gettext_noop("Name"),
148                                                   gettext_noop("Owner"),
149                                                   gettext_noop("Location"));
150         else
151                 printfPQExpBuffer(&buf,
152                                                   "SELECT spcname AS \"%s\",\n"
153                                                   "  pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
154                                                   "  spclocation AS \"%s\"",
155                                                   gettext_noop("Name"),
156                                                   gettext_noop("Owner"),
157                                                   gettext_noop("Location"));
158
159         if (verbose)
160         {
161                 appendPQExpBuffer(&buf, ",\n  ");
162                 printACLColumn(&buf, "spcacl");
163         }
164
165         if (verbose && pset.sversion >= 80200)
166                 appendPQExpBuffer(&buf,
167                  ",\n  pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
168                                                   gettext_noop("Description"));
169
170         appendPQExpBuffer(&buf,
171                                           "\nFROM pg_catalog.pg_tablespace\n");
172
173         processSQLNamePattern(pset.db, &buf, pattern, false, false,
174                                                   NULL, "spcname", NULL,
175                                                   NULL);
176
177         appendPQExpBuffer(&buf, "ORDER BY 1;");
178
179         res = PSQLexec(buf.data, false);
180         termPQExpBuffer(&buf);
181         if (!res)
182                 return false;
183
184         myopt.nullPrint = NULL;
185         myopt.title = _("List of tablespaces");
186         myopt.translate_header = true;
187
188         printQuery(res, &myopt, pset.queryFout, pset.logfile);
189
190         PQclear(res);
191         return true;
192 }
193
194
195 /* \df
196  * Takes an optional regexp to select particular functions.
197  *
198  * As with \d, you can specify the kinds of functions you want:
199  *
200  * a for aggregates
201  * n for normal
202  * t for trigger
203  * w for window
204  *
205  * and you can mix and match these in any order.
206  */
207 bool
208 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
209 {
210         bool            showAggregate = strchr(functypes, 'a') != NULL;
211         bool            showNormal = strchr(functypes, 'n') != NULL;
212         bool            showTrigger = strchr(functypes, 't') != NULL;
213         bool            showWindow = strchr(functypes, 'w') != NULL;
214         bool            have_where;
215         PQExpBufferData buf;
216         PGresult   *res;
217         printQueryOpt myopt = pset.popt;
218         static const bool translate_columns[] = {false, false, false, false, true, true, false, false, false, false};
219
220         if (strlen(functypes) != strspn(functypes, "antwS+"))
221         {
222                 fprintf(stderr, _("\\df only takes [antwS+] as options\n"));
223                 return true;
224         }
225
226         if (showWindow && pset.sversion < 80400)
227         {
228                 fprintf(stderr, _("\\df does not take a \"w\" option with server version %d.%d\n"),
229                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
230                 return true;
231         }
232
233         if (!showAggregate && !showNormal && !showTrigger && !showWindow)
234         {
235                 showAggregate = showNormal = showTrigger = true;
236                 if (pset.sversion >= 80400)
237                         showWindow = true;
238         }
239
240         initPQExpBuffer(&buf);
241
242         printfPQExpBuffer(&buf,
243                                           "SELECT n.nspname as \"%s\",\n"
244                                           "  p.proname as \"%s\",\n",
245                                           gettext_noop("Schema"),
246                                           gettext_noop("Name"));
247
248         if (pset.sversion >= 80400)
249                 appendPQExpBuffer(&buf,
250                                         "  pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
251                                  "  pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
252                                                   " CASE\n"
253                                                   "  WHEN p.proisagg THEN '%s'\n"
254                                                   "  WHEN p.proiswindow THEN '%s'\n"
255                                                   "  WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
256                                                   "  ELSE '%s'\n"
257                                                   "END as \"%s\"",
258                                                   gettext_noop("Result data type"),
259                                                   gettext_noop("Argument data types"),
260                 /* translator: "agg" is short for "aggregate" */
261                                                   gettext_noop("agg"),
262                                                   gettext_noop("window"),
263                                                   gettext_noop("trigger"),
264                                                   gettext_noop("normal"),
265                                                   gettext_noop("Type"));
266         else if (pset.sversion >= 80100)
267                 appendPQExpBuffer(&buf,
268                                          "  CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
269                                   "  pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
270                                                   "  CASE WHEN proallargtypes IS NOT NULL THEN\n"
271                                                   "    pg_catalog.array_to_string(ARRAY(\n"
272                                                   "      SELECT\n"
273                                                   "        CASE\n"
274                                                   "          WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
275                                           "          WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
276                                         "          WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
277                                  "          WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
278                                                   "        END ||\n"
279                                                   "        CASE\n"
280                          "          WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
281                                                   "          ELSE p.proargnames[s.i] || ' ' \n"
282                                                   "        END ||\n"
283                           "        pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
284                                                   "      FROM\n"
285                                                   "        pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
286                                                   "    ), ', ')\n"
287                                                   "  ELSE\n"
288                                                   "    pg_catalog.array_to_string(ARRAY(\n"
289                                                   "      SELECT\n"
290                                                   "        CASE\n"
291                    "          WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
292                                                   "          ELSE p.proargnames[s.i+1] || ' '\n"
293                                                   "          END ||\n"
294                                  "        pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
295                                                   "      FROM\n"
296                                                   "        pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
297                                                   "    ), ', ')\n"
298                                                   "  END AS \"%s\",\n"
299                                                   "  CASE\n"
300                                                   "    WHEN p.proisagg THEN '%s'\n"
301                                                   "    WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
302                                                   "    ELSE '%s'\n"
303                                                   "  END AS \"%s\"",
304                                                   gettext_noop("Result data type"),
305                                                   gettext_noop("Argument data types"),
306                 /* translator: "agg" is short for "aggregate" */
307                                                   gettext_noop("agg"),
308                                                   gettext_noop("trigger"),
309                                                   gettext_noop("normal"),
310                                                   gettext_noop("Type"));
311         else
312                 appendPQExpBuffer(&buf,
313                                          "  CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
314                                   "  pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
315                                         "  pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
316                                                   "  CASE\n"
317                                                   "    WHEN p.proisagg THEN '%s'\n"
318                                                   "    WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
319                                                   "    ELSE '%s'\n"
320                                                   "  END AS \"%s\"",
321                                                   gettext_noop("Result data type"),
322                                                   gettext_noop("Argument data types"),
323                 /* translator: "agg" is short for "aggregate" */
324                                                   gettext_noop("agg"),
325                                                   gettext_noop("trigger"),
326                                                   gettext_noop("normal"),
327                                                   gettext_noop("Type"));
328
329         if (verbose)
330                 appendPQExpBuffer(&buf,
331                                                   ",\n CASE\n"
332                                                   "  WHEN p.provolatile = 'i' THEN '%s'\n"
333                                                   "  WHEN p.provolatile = 's' THEN '%s'\n"
334                                                   "  WHEN p.provolatile = 'v' THEN '%s'\n"
335                                                   "END as \"%s\""
336                                    ",\n  pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
337                                                   "  l.lanname as \"%s\",\n"
338                                                   "  p.prosrc as \"%s\",\n"
339                                   "  pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
340                                                   gettext_noop("immutable"),
341                                                   gettext_noop("stable"),
342                                                   gettext_noop("volatile"),
343                                                   gettext_noop("Volatility"),
344                                                   gettext_noop("Owner"),
345                                                   gettext_noop("Language"),
346                                                   gettext_noop("Source code"),
347                                                   gettext_noop("Description"));
348
349         appendPQExpBuffer(&buf,
350                                           "\nFROM pg_catalog.pg_proc p"
351         "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
352
353         if (verbose)
354                 appendPQExpBuffer(&buf,
355                    "     LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
356
357         have_where = false;
358
359         /* filter by function type, if requested */
360         if (showNormal && showAggregate && showTrigger && showWindow)
361                  /* Do nothing */ ;
362         else if (showNormal)
363         {
364                 if (!showAggregate)
365                 {
366                         if (have_where)
367                                 appendPQExpBuffer(&buf, "      AND ");
368                         else
369                         {
370                                 appendPQExpBuffer(&buf, "WHERE ");
371                                 have_where = true;
372                         }
373                         appendPQExpBuffer(&buf, "NOT p.proisagg\n");
374                 }
375                 if (!showTrigger)
376                 {
377                         if (have_where)
378                                 appendPQExpBuffer(&buf, "      AND ");
379                         else
380                         {
381                                 appendPQExpBuffer(&buf, "WHERE ");
382                                 have_where = true;
383                         }
384                         appendPQExpBuffer(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
385                 }
386                 if (!showWindow && pset.sversion >= 80400)
387                 {
388                         if (have_where)
389                                 appendPQExpBuffer(&buf, "      AND ");
390                         else
391                         {
392                                 appendPQExpBuffer(&buf, "WHERE ");
393                                 have_where = true;
394                         }
395                         appendPQExpBuffer(&buf, "NOT p.proiswindow\n");
396                 }
397         }
398         else
399         {
400                 bool            needs_or = false;
401
402                 appendPQExpBuffer(&buf, "WHERE (\n       ");
403                 have_where = true;
404                 /* Note: at least one of these must be true ... */
405                 if (showAggregate)
406                 {
407                         appendPQExpBuffer(&buf, "p.proisagg\n");
408                         needs_or = true;
409                 }
410                 if (showTrigger)
411                 {
412                         if (needs_or)
413                                 appendPQExpBuffer(&buf, "       OR ");
414                         appendPQExpBuffer(&buf,
415                                 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
416                         needs_or = true;
417                 }
418                 if (showWindow)
419                 {
420                         if (needs_or)
421                                 appendPQExpBuffer(&buf, "       OR ");
422                         appendPQExpBuffer(&buf, "p.proiswindow\n");
423                         needs_or = true;
424                 }
425                 appendPQExpBuffer(&buf, "      )\n");
426         }
427
428         processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
429                                                   "n.nspname", "p.proname", NULL,
430                                                   "pg_catalog.pg_function_is_visible(p.oid)");
431
432         if (!showSystem && !pattern)
433                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
434                                                   "      AND n.nspname <> 'information_schema'\n");
435
436         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
437
438         res = PSQLexec(buf.data, false);
439         termPQExpBuffer(&buf);
440         if (!res)
441                 return false;
442
443         myopt.nullPrint = NULL;
444         myopt.title = _("List of functions");
445         myopt.translate_header = true;
446         myopt.translate_columns = translate_columns;
447
448         printQuery(res, &myopt, pset.queryFout, pset.logfile);
449
450         PQclear(res);
451         return true;
452 }
453
454
455
456 /*
457  * \dT
458  * describe types
459  */
460 bool
461 describeTypes(const char *pattern, bool verbose, bool showSystem)
462 {
463         PQExpBufferData buf;
464         PGresult   *res;
465         printQueryOpt myopt = pset.popt;
466
467         initPQExpBuffer(&buf);
468
469         printfPQExpBuffer(&buf,
470                                           "SELECT n.nspname as \"%s\",\n"
471                                           "  pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
472                                           gettext_noop("Schema"),
473                                           gettext_noop("Name"));
474         if (verbose)
475                 appendPQExpBuffer(&buf,
476                                                   "  t.typname AS \"%s\",\n"
477                                                   "  CASE WHEN t.typrelid != 0\n"
478                                                   "      THEN CAST('tuple' AS pg_catalog.text)\n"
479                                                   "    WHEN t.typlen < 0\n"
480                                                   "      THEN CAST('var' AS pg_catalog.text)\n"
481                                                   "    ELSE CAST(t.typlen AS pg_catalog.text)\n"
482                                                   "  END AS \"%s\",\n",
483                                                   gettext_noop("Internal name"),
484                                                   gettext_noop("Size"));
485         if (verbose && pset.sversion >= 80300)
486         {
487                 appendPQExpBuffer(&buf,
488                                                   "  pg_catalog.array_to_string(\n"
489                                                   "      ARRAY(\n"
490                                                   "                  SELECT e.enumlabel\n"
491                                                   "          FROM pg_catalog.pg_enum e\n"
492                                                   "          WHERE e.enumtypid = t.oid\n");
493
494                 if (pset.sversion >= 90100)
495                         appendPQExpBuffer(&buf,
496                                                           "          ORDER BY e.enumsortorder\n");
497                 else
498                         appendPQExpBuffer(&buf,
499                                                           "          ORDER BY e.oid\n");
500
501                 appendPQExpBuffer(&buf,
502                                                   "      ),\n"
503                                                   "      E'\\n'\n"
504                                                   "  ) AS \"%s\",\n",
505                                                   gettext_noop("Elements"));
506         }
507         if (verbose && pset.sversion >= 90200)
508         {
509                 printACLColumn(&buf, "t.typacl");
510                 appendPQExpBuffer(&buf, ",\n  ");
511         }
512
513         appendPQExpBuffer(&buf,
514                                 "  pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
515                                           gettext_noop("Description"));
516
517         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
518          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
519
520         /*
521          * do not include complex types (typrelid!=0) unless they are standalone
522          * composite types
523          */
524         appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
525         appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
526                                           "WHERE c.oid = t.typrelid))\n");
527
528         /*
529          * do not include array types (before 8.3 we have to use the assumption
530          * that their names start with underscore)
531          */
532         if (pset.sversion >= 80300)
533                 appendPQExpBuffer(&buf, "  AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
534         else
535                 appendPQExpBuffer(&buf, "  AND t.typname !~ '^_'\n");
536
537         if (!showSystem && !pattern)
538                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
539                                                   "      AND n.nspname <> 'information_schema'\n");
540
541         /* Match name pattern against either internal or external name */
542         processSQLNamePattern(pset.db, &buf, pattern, true, false,
543                                                   "n.nspname", "t.typname",
544                                                   "pg_catalog.format_type(t.oid, NULL)",
545                                                   "pg_catalog.pg_type_is_visible(t.oid)");
546
547         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
548
549         res = PSQLexec(buf.data, false);
550         termPQExpBuffer(&buf);
551         if (!res)
552                 return false;
553
554         myopt.nullPrint = NULL;
555         myopt.title = _("List of data types");
556         myopt.translate_header = true;
557
558         printQuery(res, &myopt, pset.queryFout, pset.logfile);
559
560         PQclear(res);
561         return true;
562 }
563
564
565 /* \do
566  */
567 bool
568 describeOperators(const char *pattern, bool showSystem)
569 {
570         PQExpBufferData buf;
571         PGresult   *res;
572         printQueryOpt myopt = pset.popt;
573
574         initPQExpBuffer(&buf);
575
576         /*
577          * Note: before Postgres 9.1, we did not assign comments to any built-in
578          * operators, preferring to let the comment on the underlying function
579          * suffice.  The coalesce() on the obj_description() calls below supports
580          * this convention by providing a fallback lookup of a comment on the
581          * operator's function.  As of 9.1 there is a policy that every built-in
582          * operator should have a comment; so the coalesce() is no longer
583          * necessary so far as built-in operators are concerned.  We keep it
584          * anyway, for now, because (1) third-party modules may still be following
585          * the old convention, and (2) we'd need to do it anyway when talking to a
586          * pre-9.1 server.
587          */
588
589         printfPQExpBuffer(&buf,
590                                           "SELECT n.nspname as \"%s\",\n"
591                                           "  o.oprname AS \"%s\",\n"
592                                           "  CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
593                                           "  CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
594                                    "  pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
595                          "  coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
596         "           pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
597                                           "FROM pg_catalog.pg_operator o\n"
598           "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
599                                           gettext_noop("Schema"),
600                                           gettext_noop("Name"),
601                                           gettext_noop("Left arg type"),
602                                           gettext_noop("Right arg type"),
603                                           gettext_noop("Result type"),
604                                           gettext_noop("Description"));
605
606         if (!showSystem && !pattern)
607                 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
608                                                   "      AND n.nspname <> 'information_schema'\n");
609
610         processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
611                                                   "n.nspname", "o.oprname", NULL,
612                                                   "pg_catalog.pg_operator_is_visible(o.oid)");
613
614         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
615
616         res = PSQLexec(buf.data, false);
617         termPQExpBuffer(&buf);
618         if (!res)
619                 return false;
620
621         myopt.nullPrint = NULL;
622         myopt.title = _("List of operators");
623         myopt.translate_header = true;
624
625         printQuery(res, &myopt, pset.queryFout, pset.logfile);
626
627         PQclear(res);
628         return true;
629 }
630
631
632 /*
633  * listAllDbs
634  *
635  * for \l, \list, and -l switch
636  */
637 bool
638 listAllDbs(bool verbose)
639 {
640         PGresult   *res;
641         PQExpBufferData buf;
642         printQueryOpt myopt = pset.popt;
643
644         initPQExpBuffer(&buf);
645
646         printfPQExpBuffer(&buf,
647                                           "SELECT d.datname as \"%s\",\n"
648                                    "       pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
649                         "       pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
650                                           gettext_noop("Name"),
651                                           gettext_noop("Owner"),
652                                           gettext_noop("Encoding"));
653         if (pset.sversion >= 80400)
654                 appendPQExpBuffer(&buf,
655                                                   "       d.datcollate as \"%s\",\n"
656                                                   "       d.datctype as \"%s\",\n",
657                                                   gettext_noop("Collate"),
658                                                   gettext_noop("Ctype"));
659         appendPQExpBuffer(&buf, "       ");
660         printACLColumn(&buf, "d.datacl");
661         if (verbose && pset.sversion >= 80200)
662                 appendPQExpBuffer(&buf,
663                                                   ",\n       CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
664                                                   "            THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
665                                                   "            ELSE 'No Access'\n"
666                                                   "       END as \"%s\"",
667                                                   gettext_noop("Size"));
668         if (verbose && pset.sversion >= 80000)
669                 appendPQExpBuffer(&buf,
670                                                   ",\n       t.spcname as \"%s\"",
671                                                   gettext_noop("Tablespace"));
672         if (verbose && pset.sversion >= 80200)
673                 appendPQExpBuffer(&buf,
674                                                   ",\n       pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
675                                                   gettext_noop("Description"));
676         appendPQExpBuffer(&buf,
677                                           "\nFROM pg_catalog.pg_database d\n");
678         if (verbose && pset.sversion >= 80000)
679                 appendPQExpBuffer(&buf,
680                    "  JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
681         appendPQExpBuffer(&buf, "ORDER BY 1;");
682         res = PSQLexec(buf.data, false);
683         termPQExpBuffer(&buf);
684         if (!res)
685                 return false;
686
687         myopt.nullPrint = NULL;
688         myopt.title = _("List of databases");
689         myopt.translate_header = true;
690
691         printQuery(res, &myopt, pset.queryFout, pset.logfile);
692
693         PQclear(res);
694         return true;
695 }
696
697
698 /*
699  * List Tables' Grant/Revoke Permissions
700  * \z (now also \dp -- perhaps more mnemonic)
701  */
702 bool
703 permissionsList(const char *pattern)
704 {
705         PQExpBufferData buf;
706         PGresult   *res;
707         printQueryOpt myopt = pset.popt;
708         static const bool translate_columns[] = {false, false, true, false, false};
709
710         initPQExpBuffer(&buf);
711
712         /*
713          * we ignore indexes and toast tables since they have no meaningful rights
714          */
715         printfPQExpBuffer(&buf,
716                                           "SELECT n.nspname as \"%s\",\n"
717                                           "  c.relname as \"%s\",\n"
718                                           "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
719                                           "  ",
720                                           gettext_noop("Schema"),
721                                           gettext_noop("Name"),
722            gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
723                                           gettext_noop("foreign table"),
724                                           gettext_noop("Type"));
725
726         printACLColumn(&buf, "c.relacl");
727
728         if (pset.sversion >= 80400)
729                 appendPQExpBuffer(&buf,
730                                                   ",\n  pg_catalog.array_to_string(ARRAY(\n"
731                                                   "    SELECT attname || E':\\n  ' || pg_catalog.array_to_string(attacl, E'\\n  ')\n"
732                                                   "    FROM pg_catalog.pg_attribute a\n"
733                                                   "    WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
734                                                   "  ), E'\\n') AS \"%s\"",
735                                                   gettext_noop("Column access privileges"));
736
737         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_class c\n"
738            "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
739                                           "WHERE c.relkind IN ('r', 'v', 'S', 'f')\n");
740
741         /*
742          * Unless a schema pattern is specified, we suppress system and temp
743          * tables, since they normally aren't very interesting from a permissions
744          * point of view.  You can see 'em by explicit request though, eg with \z
745          * pg_catalog.*
746          */
747         processSQLNamePattern(pset.db, &buf, pattern, true, false,
748                                                   "n.nspname", "c.relname", NULL,
749                         "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
750
751         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
752
753         res = PSQLexec(buf.data, false);
754         if (!res)
755         {
756                 termPQExpBuffer(&buf);
757                 return false;
758         }
759
760         myopt.nullPrint = NULL;
761         printfPQExpBuffer(&buf, _("Access privileges"));
762         myopt.title = buf.data;
763         myopt.translate_header = true;
764         myopt.translate_columns = translate_columns;
765
766         printQuery(res, &myopt, pset.queryFout, pset.logfile);
767
768         termPQExpBuffer(&buf);
769         PQclear(res);
770         return true;
771 }
772
773
774 /*
775  * \ddp
776  *
777  * List DefaultACLs.  The pattern can match either schema or role name.
778  */
779 bool
780 listDefaultACLs(const char *pattern)
781 {
782         PQExpBufferData buf;
783         PGresult   *res;
784         printQueryOpt myopt = pset.popt;
785         static const bool translate_columns[] = {false, false, true, false};
786
787         if (pset.sversion < 90000)
788         {
789                 fprintf(stderr, _("The server (version %d.%d) does not support altering default privileges.\n"),
790                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
791                 return true;
792         }
793
794         initPQExpBuffer(&buf);
795
796         printfPQExpBuffer(&buf,
797                            "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
798                                           "  n.nspname AS \"%s\",\n"
799                                           "  CASE d.defaclobjtype WHEN 'r' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END AS \"%s\",\n"
800                                           "  ",
801                                           gettext_noop("Owner"),
802                                           gettext_noop("Schema"),
803                                           gettext_noop("table"),
804                                           gettext_noop("sequence"),
805                                           gettext_noop("function"),
806                                           gettext_noop("Type"));
807
808         printACLColumn(&buf, "d.defaclacl");
809
810         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
811                                           "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
812
813         processSQLNamePattern(pset.db, &buf, pattern, false, false,
814                                                   NULL,
815                                                   "n.nspname",
816                                                   "pg_catalog.pg_get_userbyid(d.defaclrole)",
817                                                   NULL);
818
819         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
820
821         res = PSQLexec(buf.data, false);
822         if (!res)
823         {
824                 termPQExpBuffer(&buf);
825                 return false;
826         }
827
828         myopt.nullPrint = NULL;
829         printfPQExpBuffer(&buf, _("Default access privileges"));
830         myopt.title = buf.data;
831         myopt.translate_header = true;
832         myopt.translate_columns = translate_columns;
833
834         printQuery(res, &myopt, pset.queryFout, pset.logfile);
835
836         termPQExpBuffer(&buf);
837         PQclear(res);
838         return true;
839 }
840
841
842 /*
843  * Get object comments
844  *
845  * \dd [foo]
846  *
847  * Note: This command only lists comments for object types which do not have
848  * their comments displayed by their own backslash commands. The following
849  * types of objects will be displayed: constraint, operator class,
850  * operator family, rule, and trigger.
851  *
852  */
853 bool
854 objectDescription(const char *pattern, bool showSystem)
855 {
856         PQExpBufferData buf;
857         PGresult   *res;
858         printQueryOpt myopt = pset.popt;
859         static const bool translate_columns[] = {false, false, true, false};
860
861         initPQExpBuffer(&buf);
862
863         appendPQExpBuffer(&buf,
864                                           "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
865                                           "FROM (\n",
866                                           gettext_noop("Schema"),
867                                           gettext_noop("Name"),
868                                           gettext_noop("Object"),
869                                           gettext_noop("Description"));
870
871         /* Constraint descriptions */
872         appendPQExpBuffer(&buf,
873                                           "  SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
874                                           "  n.nspname as nspname,\n"
875                                           "  CAST(pgc.conname AS pg_catalog.text) as name,"
876                                           "  CAST('%s' AS pg_catalog.text) as object\n"
877                                           "  FROM pg_catalog.pg_constraint pgc\n"
878                                           "    JOIN pg_catalog.pg_class c "
879                                           "ON c.oid = pgc.conrelid\n"
880                                           "    LEFT JOIN pg_catalog.pg_namespace n "
881                                           "    ON n.oid = c.relnamespace\n",
882                                           gettext_noop("constraint"));
883
884         if (!showSystem && !pattern)
885                 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
886                                                   "      AND n.nspname <> 'information_schema'\n");
887
888         processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
889                                                   false, "n.nspname", "pgc.conname", NULL,
890                                                   "pg_catalog.pg_table_is_visible(c.oid)");
891
892         /*
893          * pg_opclass.opcmethod only available in 8.3+
894          */
895         if (pset.sversion >= 80300)
896         {
897                 /* Operator class descriptions */
898                 appendPQExpBuffer(&buf,
899                                                   "UNION ALL\n"
900                                                   "  SELECT o.oid as oid, o.tableoid as tableoid,\n"
901                                                   "  n.nspname as nspname,\n"
902                                                   "  CAST(o.opcname AS pg_catalog.text) as name,\n"
903                                                   "  CAST('%s' AS pg_catalog.text) as object\n"
904                                                   "  FROM pg_catalog.pg_opclass o\n"
905                                                   "    JOIN pg_catalog.pg_am am ON "
906                                                   "o.opcmethod = am.oid\n"
907                                                   "    JOIN pg_catalog.pg_namespace n ON "
908                                                   "n.oid = o.opcnamespace\n",
909                                                   gettext_noop("operator class"));
910
911                 if (!showSystem && !pattern)
912                         appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
913                                                           "      AND n.nspname <> 'information_schema'\n");
914
915                 processSQLNamePattern(pset.db, &buf, pattern, true, false,
916                                                           "n.nspname", "o.opcname", NULL,
917                                                           "pg_catalog.pg_opclass_is_visible(o.oid)");
918         }
919
920         /*
921          * although operator family comments have been around since 8.3,
922          * pg_opfamily_is_visible is only available in 9.2+
923          */
924         if (pset.sversion >= 90200)
925         {
926                 /* Operator family descriptions */
927                 appendPQExpBuffer(&buf,
928                                                   "UNION ALL\n"
929                                                   "  SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
930                                                   "  n.nspname as nspname,\n"
931                                                   "  CAST(opf.opfname AS pg_catalog.text) AS name,\n"
932                                                   "  CAST('%s' AS pg_catalog.text) as object\n"
933                                                   "  FROM pg_catalog.pg_opfamily opf\n"
934                                                   "    JOIN pg_catalog.pg_am am "
935                                                   "ON opf.opfmethod = am.oid\n"
936                                                   "    JOIN pg_catalog.pg_namespace n "
937                                                   "ON opf.opfnamespace = n.oid\n",
938                                                   gettext_noop("operator family"));
939
940                 if (!showSystem && !pattern)
941                         appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
942                                                           "      AND n.nspname <> 'information_schema'\n");
943
944                 processSQLNamePattern(pset.db, &buf, pattern, true, false,
945                                                           "n.nspname", "opf.opfname", NULL,
946                                                           "pg_catalog.pg_opfamily_is_visible(opf.oid)");
947         }
948
949         /* Rule descriptions (ignore rules for views) */
950         appendPQExpBuffer(&buf,
951                                           "UNION ALL\n"
952                                           "  SELECT r.oid as oid, r.tableoid as tableoid,\n"
953                                           "  n.nspname as nspname,\n"
954                                           "  CAST(r.rulename AS pg_catalog.text) as name,"
955                                           "  CAST('%s' AS pg_catalog.text) as object\n"
956                                           "  FROM pg_catalog.pg_rewrite r\n"
957                                   "       JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
958          "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
959                                           "  WHERE r.rulename != '_RETURN'\n",
960                                           gettext_noop("rule"));
961
962         if (!showSystem && !pattern)
963                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
964                                                   "      AND n.nspname <> 'information_schema'\n");
965
966         processSQLNamePattern(pset.db, &buf, pattern, true, false,
967                                                   "n.nspname", "r.rulename", NULL,
968                                                   "pg_catalog.pg_table_is_visible(c.oid)");
969
970         /* Trigger descriptions */
971         appendPQExpBuffer(&buf,
972                                           "UNION ALL\n"
973                                           "  SELECT t.oid as oid, t.tableoid as tableoid,\n"
974                                           "  n.nspname as nspname,\n"
975                                           "  CAST(t.tgname AS pg_catalog.text) as name,"
976                                           "  CAST('%s' AS pg_catalog.text) as object\n"
977                                           "  FROM pg_catalog.pg_trigger t\n"
978                                    "       JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
979         "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
980                                           gettext_noop("trigger"));
981
982         if (!showSystem && !pattern)
983                 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
984                                                   "      AND n.nspname <> 'information_schema'\n");
985
986         processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
987                                                   "n.nspname", "t.tgname", NULL,
988                                                   "pg_catalog.pg_table_is_visible(c.oid)");
989
990         appendPQExpBuffer(&buf,
991                                           ") AS tt\n"
992                                           "  JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
993
994         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
995
996         res = PSQLexec(buf.data, false);
997         termPQExpBuffer(&buf);
998         if (!res)
999                 return false;
1000
1001         myopt.nullPrint = NULL;
1002         myopt.title = _("Object descriptions");
1003         myopt.translate_header = true;
1004         myopt.translate_columns = translate_columns;
1005
1006         printQuery(res, &myopt, pset.queryFout, pset.logfile);
1007
1008         PQclear(res);
1009         return true;
1010 }
1011
1012
1013 /*
1014  * describeTableDetails (for \d)
1015  *
1016  * This routine finds the tables to be displayed, and calls
1017  * describeOneTableDetails for each one.
1018  *
1019  * verbose: if true, this is \d+
1020  */
1021 bool
1022 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1023 {
1024         PQExpBufferData buf;
1025         PGresult   *res;
1026         int                     i;
1027
1028         initPQExpBuffer(&buf);
1029
1030         printfPQExpBuffer(&buf,
1031                                           "SELECT c.oid,\n"
1032                                           "  n.nspname,\n"
1033                                           "  c.relname\n"
1034                                           "FROM pg_catalog.pg_class c\n"
1035          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1036
1037         if (!showSystem && !pattern)
1038                 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1039                                                   "      AND n.nspname <> 'information_schema'\n");
1040
1041         processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1042                                                   "n.nspname", "c.relname", NULL,
1043                                                   "pg_catalog.pg_table_is_visible(c.oid)");
1044
1045         appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
1046
1047         res = PSQLexec(buf.data, false);
1048         termPQExpBuffer(&buf);
1049         if (!res)
1050                 return false;
1051
1052         if (PQntuples(res) == 0)
1053         {
1054                 if (!pset.quiet)
1055                         fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
1056                                         pattern);
1057                 PQclear(res);
1058                 return false;
1059         }
1060
1061         for (i = 0; i < PQntuples(res); i++)
1062         {
1063                 const char *oid;
1064                 const char *nspname;
1065                 const char *relname;
1066
1067                 oid = PQgetvalue(res, i, 0);
1068                 nspname = PQgetvalue(res, i, 1);
1069                 relname = PQgetvalue(res, i, 2);
1070
1071                 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1072                 {
1073                         PQclear(res);
1074                         return false;
1075                 }
1076                 if (cancel_pressed)
1077                 {
1078                         PQclear(res);
1079                         return false;
1080                 }
1081         }
1082
1083         PQclear(res);
1084         return true;
1085 }
1086
1087 /*
1088  * describeOneTableDetails (for \d)
1089  *
1090  * Unfortunately, the information presented here is so complicated that it
1091  * cannot be done in a single query. So we have to assemble the printed table
1092  * by hand and pass it to the underlying printTable() function.
1093  */
1094 static bool
1095 describeOneTableDetails(const char *schemaname,
1096                                                 const char *relationname,
1097                                                 const char *oid,
1098                                                 bool verbose)
1099 {
1100         PQExpBufferData buf;
1101         PGresult   *res = NULL;
1102         printTableOpt myopt = pset.popt.topt;
1103         printTableContent cont;
1104         bool            printTableInitialized = false;
1105         int                     i;
1106         char       *view_def = NULL;
1107         char       *headers[6];
1108         char      **seq_values = NULL;
1109         char      **modifiers = NULL;
1110         char      **ptr;
1111         PQExpBufferData title;
1112         PQExpBufferData tmpbuf;
1113         int                     cols;
1114         int                     numrows = 0;
1115         struct
1116         {
1117                 int16           checks;
1118                 char            relkind;
1119                 bool            hasindex;
1120                 bool            hasrules;
1121                 bool            hastriggers;
1122                 bool            hasoids;
1123                 Oid                     tablespace;
1124                 char       *reloptions;
1125                 char       *reloftype;
1126                 char            relpersistence;
1127         }                       tableinfo;
1128         bool            show_modifiers = false;
1129         bool            retval;
1130
1131         retval = false;
1132
1133         /* This output looks confusing in expanded mode. */
1134         myopt.expanded = false;
1135
1136         initPQExpBuffer(&buf);
1137         initPQExpBuffer(&title);
1138         initPQExpBuffer(&tmpbuf);
1139
1140         /* Get general table info */
1141         if (pset.sversion >= 90100)
1142         {
1143                 printfPQExpBuffer(&buf,
1144                           "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1145                                                   "c.relhastriggers, c.relhasoids, "
1146                                                   "%s, c.reltablespace, "
1147                                                   "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1148                                                   "c.relpersistence\n"
1149                                                   "FROM pg_catalog.pg_class c\n "
1150                    "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1151                                                   "WHERE c.oid = '%s';",
1152                                                   (verbose ?
1153                                                    "pg_catalog.array_to_string(c.reloptions || "
1154                                                    "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1155                                                    : "''"),
1156                                                   oid);
1157         }
1158         else if (pset.sversion >= 90000)
1159         {
1160                 printfPQExpBuffer(&buf,
1161                           "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1162                                                   "c.relhastriggers, c.relhasoids, "
1163                                                   "%s, c.reltablespace, "
1164                                                   "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1165                                                   "FROM pg_catalog.pg_class c\n "
1166                    "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1167                                                   "WHERE c.oid = '%s';",
1168                                                   (verbose ?
1169                                                    "pg_catalog.array_to_string(c.reloptions || "
1170                                                    "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1171                                                    : "''"),
1172                                                   oid);
1173         }
1174         else if (pset.sversion >= 80400)
1175         {
1176                 printfPQExpBuffer(&buf,
1177                           "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1178                                                   "c.relhastriggers, c.relhasoids, "
1179                                                   "%s, c.reltablespace\n"
1180                                                   "FROM pg_catalog.pg_class c\n "
1181                    "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1182                                                   "WHERE c.oid = '%s';",
1183                                                   (verbose ?
1184                                                    "pg_catalog.array_to_string(c.reloptions || "
1185                                                    "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1186                                                    : "''"),
1187                                                   oid);
1188         }
1189         else if (pset.sversion >= 80200)
1190         {
1191                 printfPQExpBuffer(&buf,
1192                                           "SELECT relchecks, relkind, relhasindex, relhasrules, "
1193                                                   "reltriggers <> 0, relhasoids, "
1194                                                   "%s, reltablespace\n"
1195                                                   "FROM pg_catalog.pg_class WHERE oid = '%s';",
1196                                                   (verbose ?
1197                                          "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1198                                                   oid);
1199         }
1200         else if (pset.sversion >= 80000)
1201         {
1202                 printfPQExpBuffer(&buf,
1203                                           "SELECT relchecks, relkind, relhasindex, relhasrules, "
1204                                                   "reltriggers <> 0, relhasoids, "
1205                                                   "'', reltablespace\n"
1206                                                   "FROM pg_catalog.pg_class WHERE oid = '%s';",
1207                                                   oid);
1208         }
1209         else
1210         {
1211                 printfPQExpBuffer(&buf,
1212                                           "SELECT relchecks, relkind, relhasindex, relhasrules, "
1213                                                   "reltriggers <> 0, relhasoids, "
1214                                                   "'', ''\n"
1215                                                   "FROM pg_catalog.pg_class WHERE oid = '%s';",
1216                                                   oid);
1217         }
1218
1219         res = PSQLexec(buf.data, false);
1220         if (!res)
1221                 goto error_return;
1222
1223         /* Did we get anything? */
1224         if (PQntuples(res) == 0)
1225         {
1226                 if (!pset.quiet)
1227                         fprintf(stderr, _("Did not find any relation with OID %s.\n"),
1228                                         oid);
1229                 goto error_return;
1230         }
1231
1232         tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1233         tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1234         tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1235         tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1236         tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1237         tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1238         tableinfo.reloptions = (pset.sversion >= 80200) ?
1239                 strdup(PQgetvalue(res, 0, 6)) : 0;
1240         tableinfo.tablespace = (pset.sversion >= 80000) ?
1241                 atooid(PQgetvalue(res, 0, 7)) : 0;
1242         tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
1243                 strdup(PQgetvalue(res, 0, 8)) : 0;
1244         tableinfo.relpersistence = (pset.sversion >= 90100 && strcmp(PQgetvalue(res, 0, 9), "") != 0) ?
1245                 PQgetvalue(res, 0, 9)[0] : 0;
1246         PQclear(res);
1247         res = NULL;
1248
1249         /*
1250          * If it's a sequence, fetch its values and store into an array that will
1251          * be used later.
1252          */
1253         if (tableinfo.relkind == 'S')
1254         {
1255                 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1256                 /* must be separate because fmtId isn't reentrant */
1257                 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1258
1259                 res = PSQLexec(buf.data, false);
1260                 if (!res)
1261                         goto error_return;
1262
1263                 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1264
1265                 for (i = 0; i < PQnfields(res); i++)
1266                         seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1267                 seq_values[i] = NULL;
1268
1269                 PQclear(res);
1270                 res = NULL;
1271         }
1272
1273         /*
1274          * Get column info
1275          *
1276          * You need to modify value of "firstvcol" which will be defined below if
1277          * you are adding column(s) preceding to verbose-only columns.
1278          */
1279         printfPQExpBuffer(&buf, "SELECT a.attname,");
1280         appendPQExpBuffer(&buf, "\n  pg_catalog.format_type(a.atttypid, a.atttypmod),"
1281                                           "\n  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1282                                           "\n   FROM pg_catalog.pg_attrdef d"
1283                                           "\n   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1284                                           "\n  a.attnotnull, a.attnum,");
1285         if (pset.sversion >= 90100)
1286                 appendPQExpBuffer(&buf, "\n  (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1287                                                   "   WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1288         else
1289                 appendPQExpBuffer(&buf, "\n  NULL AS attcollation");
1290         if (tableinfo.relkind == 'i')
1291                 appendPQExpBuffer(&buf, ",\n  pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1292         else
1293                 appendPQExpBuffer(&buf, ",\n  NULL AS indexdef");
1294         if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1295                 appendPQExpBuffer(&buf, ",\n  CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1296                                                                 "  '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) ||  ' ' || quote_literal(option_value)  FROM "
1297                                                                 "  pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1298         else
1299                 appendPQExpBuffer(&buf, ",\n  NULL AS attfdwoptions");
1300         if (verbose)
1301         {
1302                 appendPQExpBuffer(&buf, ",\n  a.attstorage");
1303                 appendPQExpBuffer(&buf, ",\n  CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1304                 /*
1305                  * In 9.0+, we have column comments for: relations, views, composite
1306                  * types, and foreign tables (c.f. CommentObject() in comment.c).
1307                  */
1308                 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1309                         tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1310                         appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1311         }
1312
1313         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
1314         appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1315         appendPQExpBuffer(&buf, "\nORDER BY a.attnum;");
1316
1317         res = PSQLexec(buf.data, false);
1318         if (!res)
1319                 goto error_return;
1320         numrows = PQntuples(res);
1321
1322         /* Make title */
1323         switch (tableinfo.relkind)
1324         {
1325                 case 'r':
1326                         if (tableinfo.relpersistence == 'u')
1327                                 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1328                                                                   schemaname, relationname);
1329                         else
1330                                 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1331                                                                   schemaname, relationname);
1332                         break;
1333                 case 'v':
1334                         printfPQExpBuffer(&title, _("View \"%s.%s\""),
1335                                                           schemaname, relationname);
1336                         break;
1337                 case 'S':
1338                         printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1339                                                           schemaname, relationname);
1340                         break;
1341                 case 'i':
1342                         if (tableinfo.relpersistence == 'u')
1343                                 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1344                                                                   schemaname, relationname);
1345                         else
1346                                 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1347                                                                   schemaname, relationname);
1348                         break;
1349                 case 's':
1350                         /* not used as of 8.2, but keep it for backwards compatibility */
1351                         printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1352                                                           schemaname, relationname);
1353                         break;
1354                 case 't':
1355                         printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1356                                                           schemaname, relationname);
1357                         break;
1358                 case 'c':
1359                         printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1360                                                           schemaname, relationname);
1361                         break;
1362                 case 'f':
1363                         printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1364                                                           schemaname, relationname);
1365                         break;
1366                 default:
1367                         /* untranslated unknown relkind */
1368                         printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1369                                                           tableinfo.relkind, schemaname, relationname);
1370                         break;
1371         }
1372
1373         /* Set the number of columns, and their names */
1374         headers[0] = gettext_noop("Column");
1375         headers[1] = gettext_noop("Type");
1376         cols = 2;
1377
1378         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1379                 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1380         {
1381                 show_modifiers = true;
1382                 headers[cols++] = gettext_noop("Modifiers");
1383                 modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
1384         }
1385
1386         if (tableinfo.relkind == 'S')
1387                 headers[cols++] = gettext_noop("Value");
1388
1389         if (tableinfo.relkind == 'i')
1390                 headers[cols++] = gettext_noop("Definition");
1391
1392         if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1393                 headers[cols++] = gettext_noop("FDW Options");
1394
1395         if (verbose)
1396         {
1397                 headers[cols++] = gettext_noop("Storage");
1398                 if (tableinfo.relkind == 'r')
1399                         headers[cols++] = gettext_noop("Stats target");
1400                 /* Column comments, if the relkind supports this feature. */
1401                 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1402                         tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1403                         headers[cols++] = gettext_noop("Description");
1404         }
1405
1406         printTableInit(&cont, &myopt, title.data, cols, numrows);
1407         printTableInitialized = true;
1408
1409         for (i = 0; i < cols; i++)
1410                 printTableAddHeader(&cont, headers[i], true, 'l');
1411
1412         /* Check if table is a view */
1413         if (tableinfo.relkind == 'v' && verbose)
1414         {
1415                 PGresult   *result;
1416
1417                 printfPQExpBuffer(&buf,
1418                           "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1419                                                   oid);
1420                 result = PSQLexec(buf.data, false);
1421                 if (!result)
1422                         goto error_return;
1423
1424                 if (PQntuples(result) > 0)
1425                         view_def = pg_strdup(PQgetvalue(result, 0, 0));
1426
1427                 PQclear(result);
1428         }
1429
1430         /* Generate table cells to be printed */
1431         for (i = 0; i < numrows; i++)
1432         {
1433                 /* Column */
1434                 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1435
1436                 /* Type */
1437                 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1438
1439                 /* Modifiers: collate, not null, default */
1440                 if (show_modifiers)
1441                 {
1442                         resetPQExpBuffer(&tmpbuf);
1443
1444                         if (!PQgetisnull(res, i, 5))
1445                         {
1446                                 if (tmpbuf.len > 0)
1447                                         appendPQExpBufferStr(&tmpbuf, " ");
1448                                 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1449                                                                   PQgetvalue(res, i, 5));
1450                         }
1451
1452                         if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1453                         {
1454                                 if (tmpbuf.len > 0)
1455                                         appendPQExpBufferStr(&tmpbuf, " ");
1456                                 appendPQExpBufferStr(&tmpbuf, _("not null"));
1457                         }
1458
1459                         /* handle "default" here */
1460                         /* (note: above we cut off the 'default' string at 128) */
1461                         if (strlen(PQgetvalue(res, i, 2)) != 0)
1462                         {
1463                                 if (tmpbuf.len > 0)
1464                                         appendPQExpBufferStr(&tmpbuf, " ");
1465                                 /* translator: default values of column definitions */
1466                                 appendPQExpBuffer(&tmpbuf, _("default %s"),
1467                                                                   PQgetvalue(res, i, 2));
1468                         }
1469
1470                         modifiers[i] = pg_strdup(tmpbuf.data);
1471                         printTableAddCell(&cont, modifiers[i], false, false);
1472                 }
1473
1474                 /* Value: for sequences only */
1475                 if (tableinfo.relkind == 'S')
1476                         printTableAddCell(&cont, seq_values[i], false, false);
1477
1478                 /* Expression for index column */
1479                 if (tableinfo.relkind == 'i')
1480                         printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1481
1482                 /* FDW options for foreign table column, only for 9.2 or later */
1483                 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1484                         printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1485
1486                 /* Storage and Description */
1487                 if (verbose)
1488                 {
1489                         int                     firstvcol = 8;
1490                         char       *storage = PQgetvalue(res, i, firstvcol);
1491
1492                         /* these strings are literal in our syntax, so not translated. */
1493                         printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1494                                                                           (storage[0] == 'm' ? "main" :
1495                                                                            (storage[0] == 'x' ? "extended" :
1496                                                                                 (storage[0] == 'e' ? "external" :
1497                                                                                  "???")))),
1498                                                           false, false);
1499
1500                         /* Statistics target, if the relkind supports this feature */
1501                         if (tableinfo.relkind == 'r')
1502                         {
1503                                 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1504                                                                   false, false);
1505                         }
1506
1507                         /* Column comments, if the relkind supports this feature. */
1508                         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1509                                 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1510                                 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1511                                                                   false, false);
1512                 }
1513         }
1514
1515         /* Make footers */
1516         if (tableinfo.relkind == 'i')
1517         {
1518                 /* Footer information about an index */
1519                 PGresult   *result;
1520
1521                 printfPQExpBuffer(&buf,
1522                                  "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1523                 if (pset.sversion >= 80200)
1524                         appendPQExpBuffer(&buf, "i.indisvalid,\n");
1525                 else
1526                         appendPQExpBuffer(&buf, "true AS indisvalid,\n");
1527                 if (pset.sversion >= 90000)
1528                         appendPQExpBuffer(&buf,
1529                                                           "  (NOT i.indimmediate) AND "
1530                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1531                                                           "WHERE conrelid = i.indrelid AND "
1532                                                           "conindid = i.indexrelid AND "
1533                                                           "contype IN ('p','u','x') AND "
1534                                                           "condeferrable) AS condeferrable,\n"
1535                                                           "  (NOT i.indimmediate) AND "
1536                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1537                                                           "WHERE conrelid = i.indrelid AND "
1538                                                           "conindid = i.indexrelid AND "
1539                                                           "contype IN ('p','u','x') AND "
1540                                                           "condeferred) AS condeferred,\n");
1541                 else
1542                         appendPQExpBuffer(&buf,
1543                                                 "  false AS condeferrable, false AS condeferred,\n");
1544                 appendPQExpBuffer(&buf, "  a.amname, c2.relname, "
1545                                           "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1546                                                   "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1547                   "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1548                                                   "AND i.indrelid = c2.oid;",
1549                                                   oid);
1550
1551                 result = PSQLexec(buf.data, false);
1552                 if (!result)
1553                         goto error_return;
1554                 else if (PQntuples(result) != 1)
1555                 {
1556                         PQclear(result);
1557                         goto error_return;
1558                 }
1559                 else
1560                 {
1561                         char       *indisunique = PQgetvalue(result, 0, 0);
1562                         char       *indisprimary = PQgetvalue(result, 0, 1);
1563                         char       *indisclustered = PQgetvalue(result, 0, 2);
1564                         char       *indisvalid = PQgetvalue(result, 0, 3);
1565                         char       *deferrable = PQgetvalue(result, 0, 4);
1566                         char       *deferred = PQgetvalue(result, 0, 5);
1567                         char       *indamname = PQgetvalue(result, 0, 6);
1568                         char       *indtable = PQgetvalue(result, 0, 7);
1569                         char       *indpred = PQgetvalue(result, 0, 8);
1570
1571                         if (strcmp(indisprimary, "t") == 0)
1572                                 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1573                         else if (strcmp(indisunique, "t") == 0)
1574                                 printfPQExpBuffer(&tmpbuf, _("unique, "));
1575                         else
1576                                 resetPQExpBuffer(&tmpbuf);
1577                         appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1578
1579                         /* we assume here that index and table are in same schema */
1580                         appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1581                                                           schemaname, indtable);
1582
1583                         if (strlen(indpred))
1584                                 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1585
1586                         if (strcmp(indisclustered, "t") == 0)
1587                                 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1588
1589                         if (strcmp(indisvalid, "t") != 0)
1590                                 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1591
1592                         if (strcmp(deferrable, "t") == 0)
1593                                 appendPQExpBuffer(&tmpbuf, _(", deferrable"));
1594
1595                         if (strcmp(deferred, "t") == 0)
1596                                 appendPQExpBuffer(&tmpbuf, _(", initially deferred"));
1597
1598                         printTableAddFooter(&cont, tmpbuf.data);
1599                         add_tablespace_footer(&cont, tableinfo.relkind,
1600                                                                   tableinfo.tablespace, true);
1601                 }
1602
1603                 PQclear(result);
1604         }
1605         else if (view_def)
1606         {
1607                 PGresult   *result = NULL;
1608
1609                 /* Footer information about a view */
1610                 printTableAddFooter(&cont, _("View definition:"));
1611                 printTableAddFooter(&cont, view_def);
1612
1613                 /* print rules */
1614                 if (tableinfo.hasrules)
1615                 {
1616                         printfPQExpBuffer(&buf,
1617                                                           "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1618                                                           "FROM pg_catalog.pg_rewrite r\n"
1619                         "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
1620                                                           oid);
1621                         result = PSQLexec(buf.data, false);
1622                         if (!result)
1623                                 goto error_return;
1624
1625                         if (PQntuples(result) > 0)
1626                         {
1627                                 printTableAddFooter(&cont, _("Rules:"));
1628                                 for (i = 0; i < PQntuples(result); i++)
1629                                 {
1630                                         const char *ruledef;
1631
1632                                         /* Everything after "CREATE RULE" is echoed verbatim */
1633                                         ruledef = PQgetvalue(result, i, 1);
1634                                         ruledef += 12;
1635
1636                                         printfPQExpBuffer(&buf, " %s", ruledef);
1637                                         printTableAddFooter(&cont, buf.data);
1638                                 }
1639                         }
1640                         PQclear(result);
1641                 }
1642         }
1643         else if (tableinfo.relkind == 'S')
1644         {
1645                 /* Footer information about a sequence */
1646                 PGresult   *result = NULL;
1647
1648                 /* Get the column that owns this sequence */
1649                 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1650                                                   "\n   pg_catalog.quote_ident(relname) || '.' ||"
1651                                                   "\n   pg_catalog.quote_ident(attname)"
1652                                                   "\nFROM pg_catalog.pg_class c"
1653                                                   "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1654                                                   "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1655                                                   "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1656                                                   "\n a.attrelid=c.oid AND"
1657                                                   "\n a.attnum=d.refobjsubid)"
1658                                                   "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1659                                                   "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1660                                                   "\n AND d.objid=%s"
1661                                                   "\n AND d.deptype='a'",
1662                                                   oid);
1663
1664                 result = PSQLexec(buf.data, false);
1665                 if (!result)
1666                         goto error_return;
1667                 else if (PQntuples(result) == 1)
1668                 {
1669                         printfPQExpBuffer(&buf, _("Owned by: %s"),
1670                                                           PQgetvalue(result, 0, 0));
1671                         printTableAddFooter(&cont, buf.data);
1672                 }
1673                 /*
1674                  * If we get no rows back, don't show anything (obviously).
1675                  * We should never get more than one row back, but if we do,
1676                  * just ignore it and don't print anything.
1677                  */
1678                 PQclear(result);
1679         }
1680         else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1681         {
1682                 /* Footer information about a table */
1683                 PGresult   *result = NULL;
1684                 int                     tuples = 0;
1685
1686                 /* print indexes */
1687                 if (tableinfo.hasindex)
1688                 {
1689                         printfPQExpBuffer(&buf,
1690                                                           "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1691                         if (pset.sversion >= 80200)
1692                                 appendPQExpBuffer(&buf, "i.indisvalid, ");
1693                         else
1694                                 appendPQExpBuffer(&buf, "true as indisvalid, ");
1695                         appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n  ");
1696                         if (pset.sversion >= 90000)
1697                                 appendPQExpBuffer(&buf,
1698                                                    "pg_catalog.pg_get_constraintdef(con.oid, true), "
1699                                                                   "contype, condeferrable, condeferred");
1700                         else
1701                                 appendPQExpBuffer(&buf,
1702                                                                   "null AS constraintdef, null AS contype, "
1703                                                          "false AS condeferrable, false AS condeferred");
1704                         if (pset.sversion >= 80000)
1705                                 appendPQExpBuffer(&buf, ", c2.reltablespace");
1706                         appendPQExpBuffer(&buf,
1707                                                           "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1708                         if (pset.sversion >= 90000)
1709                                 appendPQExpBuffer(&buf,
1710                                                                   "  LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1711                         appendPQExpBuffer(&buf,
1712                                                           "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1713                           "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
1714                                                           oid);
1715                         result = PSQLexec(buf.data, false);
1716                         if (!result)
1717                                 goto error_return;
1718                         else
1719                                 tuples = PQntuples(result);
1720
1721                         if (tuples > 0)
1722                         {
1723                                 printTableAddFooter(&cont, _("Indexes:"));
1724                                 for (i = 0; i < tuples; i++)
1725                                 {
1726                                         /* untranslated index name */
1727                                         printfPQExpBuffer(&buf, "    \"%s\"",
1728                                                                           PQgetvalue(result, i, 0));
1729
1730                                         /* If exclusion constraint, print the constraintdef */
1731                                         if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1732                                         {
1733                                                 appendPQExpBuffer(&buf, " %s",
1734                                                                                   PQgetvalue(result, i, 6));
1735                                         }
1736                                         else
1737                                         {
1738                                                 const char *indexdef;
1739                                                 const char *usingpos;
1740
1741                                                 /* Label as primary key or unique (but not both) */
1742                                                 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1743                                                         appendPQExpBuffer(&buf, " PRIMARY KEY,");
1744                                                 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1745                                                 {
1746                                                         if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1747                                                                 appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
1748                                                         else
1749                                                                 appendPQExpBuffer(&buf, " UNIQUE,");
1750                                                 }
1751
1752                                                 /* Everything after "USING" is echoed verbatim */
1753                                                 indexdef = PQgetvalue(result, i, 5);
1754                                                 usingpos = strstr(indexdef, " USING ");
1755                                                 if (usingpos)
1756                                                         indexdef = usingpos + 7;
1757                                                 appendPQExpBuffer(&buf, " %s", indexdef);
1758
1759                                                 /* Need these for deferrable PK/UNIQUE indexes */
1760                                                 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1761                                                         appendPQExpBuffer(&buf, " DEFERRABLE");
1762
1763                                                 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1764                                                         appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
1765                                         }
1766
1767                                         /* Add these for all cases */
1768                                         if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1769                                                 appendPQExpBuffer(&buf, " CLUSTER");
1770
1771                                         if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1772                                                 appendPQExpBuffer(&buf, " INVALID");
1773
1774                                         printTableAddFooter(&cont, buf.data);
1775
1776                                         /* Print tablespace of the index on the same line */
1777                                         if (pset.sversion >= 80000)
1778                                                 add_tablespace_footer(&cont, 'i',
1779                                                                                    atooid(PQgetvalue(result, i, 10)),
1780                                                                                           false);
1781                                 }
1782                         }
1783                         PQclear(result);
1784                 }
1785
1786                 /* print table (and column) check constraints */
1787                 if (tableinfo.checks)
1788                 {
1789                         char *is_only;
1790
1791                         if (pset.sversion >= 90200)
1792                                 is_only = "r.conisonly";
1793                         else
1794                                 is_only = "false AS conisonly";
1795
1796                         printfPQExpBuffer(&buf,
1797                                                           "SELECT r.conname, %s, "
1798                                                           "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1799                                                           "FROM pg_catalog.pg_constraint r\n"
1800                                    "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
1801                                                           "ORDER BY 2 DESC, 1;",
1802                                                           is_only, oid);
1803                         result = PSQLexec(buf.data, false);
1804                         if (!result)
1805                                 goto error_return;
1806                         else
1807                                 tuples = PQntuples(result);
1808
1809                         if (tuples > 0)
1810                         {
1811                                 printTableAddFooter(&cont, _("Check constraints:"));
1812                                 for (i = 0; i < tuples; i++)
1813                                 {
1814                                         /* untranslated contraint name and def */
1815                                         printfPQExpBuffer(&buf, "    \"%s\"%s%s",
1816                                                                           PQgetvalue(result, i, 0),
1817                                                                           (strcmp(PQgetvalue(result, i, 1), "t") == 0) ? " (ONLY) ":" ",
1818                                                                           PQgetvalue(result, i, 2));
1819
1820                                         printTableAddFooter(&cont, buf.data);
1821                                 }
1822                         }
1823                         PQclear(result);
1824                 }
1825
1826                 /* print foreign-key constraints (there are none if no triggers) */
1827                 if (tableinfo.hastriggers)
1828                 {
1829                         printfPQExpBuffer(&buf,
1830                                                           "SELECT conname,\n"
1831                                  "  pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1832                                                           "FROM pg_catalog.pg_constraint r\n"
1833                                         "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
1834                                                           oid);
1835                         result = PSQLexec(buf.data, false);
1836                         if (!result)
1837                                 goto error_return;
1838                         else
1839                                 tuples = PQntuples(result);
1840
1841                         if (tuples > 0)
1842                         {
1843                                 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1844                                 for (i = 0; i < tuples; i++)
1845                                 {
1846                                         /* untranslated constraint name and def */
1847                                         printfPQExpBuffer(&buf, "    \"%s\" %s",
1848                                                                           PQgetvalue(result, i, 0),
1849                                                                           PQgetvalue(result, i, 1));
1850
1851                                         printTableAddFooter(&cont, buf.data);
1852                                 }
1853                         }
1854                         PQclear(result);
1855                 }
1856
1857                 /* print incoming foreign-key references (none if no triggers) */
1858                 if (tableinfo.hastriggers)
1859                 {
1860                         printfPQExpBuffer(&buf,
1861                                                    "SELECT conname, conrelid::pg_catalog.regclass,\n"
1862                                  "  pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1863                                                           "FROM pg_catalog.pg_constraint c\n"
1864                                    "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
1865                                                           oid);
1866                         result = PSQLexec(buf.data, false);
1867                         if (!result)
1868                                 goto error_return;
1869                         else
1870                                 tuples = PQntuples(result);
1871
1872                         if (tuples > 0)
1873                         {
1874                                 printTableAddFooter(&cont, _("Referenced by:"));
1875                                 for (i = 0; i < tuples; i++)
1876                                 {
1877                                         printfPQExpBuffer(&buf, "    TABLE \"%s\" CONSTRAINT \"%s\" %s",
1878                                                                           PQgetvalue(result, i, 1),
1879                                                                           PQgetvalue(result, i, 0),
1880                                                                           PQgetvalue(result, i, 2));
1881
1882                                         printTableAddFooter(&cont, buf.data);
1883                                 }
1884                         }
1885                         PQclear(result);
1886                 }
1887
1888                 /* print rules */
1889                 if (tableinfo.hasrules)
1890                 {
1891                         if (pset.sversion >= 80300)
1892                         {
1893                                 printfPQExpBuffer(&buf,
1894                                                                   "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1895                                                                   "ev_enabled\n"
1896                                                                   "FROM pg_catalog.pg_rewrite r\n"
1897                                                                   "WHERE r.ev_class = '%s' ORDER BY 1;",
1898                                                                   oid);
1899                         }
1900                         else
1901                         {
1902                                 printfPQExpBuffer(&buf,
1903                                                                   "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1904                                                                   "'O'::char AS ev_enabled\n"
1905                                                                   "FROM pg_catalog.pg_rewrite r\n"
1906                                                                   "WHERE r.ev_class = '%s' ORDER BY 1;",
1907                                                                   oid);
1908                         }
1909                         result = PSQLexec(buf.data, false);
1910                         if (!result)
1911                                 goto error_return;
1912                         else
1913                                 tuples = PQntuples(result);
1914
1915                         if (tuples > 0)
1916                         {
1917                                 bool            have_heading;
1918                                 int                     category;
1919
1920                                 for (category = 0; category < 4; category++)
1921                                 {
1922                                         have_heading = false;
1923
1924                                         for (i = 0; i < tuples; i++)
1925                                         {
1926                                                 const char *ruledef;
1927                                                 bool            list_rule = false;
1928
1929                                                 switch (category)
1930                                                 {
1931                                                         case 0:
1932                                                                 if (*PQgetvalue(result, i, 2) == 'O')
1933                                                                         list_rule = true;
1934                                                                 break;
1935                                                         case 1:
1936                                                                 if (*PQgetvalue(result, i, 2) == 'D')
1937                                                                         list_rule = true;
1938                                                                 break;
1939                                                         case 2:
1940                                                                 if (*PQgetvalue(result, i, 2) == 'A')
1941                                                                         list_rule = true;
1942                                                                 break;
1943                                                         case 3:
1944                                                                 if (*PQgetvalue(result, i, 2) == 'R')
1945                                                                         list_rule = true;
1946                                                                 break;
1947                                                 }
1948                                                 if (!list_rule)
1949                                                         continue;
1950
1951                                                 if (!have_heading)
1952                                                 {
1953                                                         switch (category)
1954                                                         {
1955                                                                 case 0:
1956                                                                         printfPQExpBuffer(&buf, _("Rules:"));
1957                                                                         break;
1958                                                                 case 1:
1959                                                                         printfPQExpBuffer(&buf, _("Disabled rules:"));
1960                                                                         break;
1961                                                                 case 2:
1962                                                                         printfPQExpBuffer(&buf, _("Rules firing always:"));
1963                                                                         break;
1964                                                                 case 3:
1965                                                                         printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1966                                                                         break;
1967                                                         }
1968                                                         printTableAddFooter(&cont, buf.data);
1969                                                         have_heading = true;
1970                                                 }
1971
1972                                                 /* Everything after "CREATE RULE" is echoed verbatim */
1973                                                 ruledef = PQgetvalue(result, i, 1);
1974                                                 ruledef += 12;
1975                                                 printfPQExpBuffer(&buf, "    %s", ruledef);
1976                                                 printTableAddFooter(&cont, buf.data);
1977                                         }
1978                                 }
1979                         }
1980                         PQclear(result);
1981                 }
1982         }
1983
1984         /*
1985          * Print triggers next, if any (but only user-defined triggers).  This
1986          * could apply to either a table or a view.
1987          */
1988         if (tableinfo.hastriggers)
1989         {
1990                 PGresult   *result;
1991                 int                     tuples;
1992
1993                 printfPQExpBuffer(&buf,
1994                                                   "SELECT t.tgname, "
1995                                                   "pg_catalog.pg_get_triggerdef(t.oid%s), "
1996                                                   "t.tgenabled\n"
1997                                                   "FROM pg_catalog.pg_trigger t\n"
1998                                                   "WHERE t.tgrelid = '%s' AND ",
1999                                                   (pset.sversion >= 90000 ? ", true" : ""),
2000                                                   oid);
2001                 if (pset.sversion >= 90000)
2002                         appendPQExpBuffer(&buf, "NOT t.tgisinternal");
2003                 else if (pset.sversion >= 80300)
2004                         appendPQExpBuffer(&buf, "t.tgconstraint = 0");
2005                 else
2006                         appendPQExpBuffer(&buf,
2007                                                           "(NOT tgisconstraint "
2008                                                           " OR NOT EXISTS"
2009                                                           "  (SELECT 1 FROM pg_catalog.pg_depend d "
2010                                                           "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2011                                                           "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2012                 appendPQExpBuffer(&buf, "\nORDER BY 1;");
2013
2014                 result = PSQLexec(buf.data, false);
2015                 if (!result)
2016                         goto error_return;
2017                 else
2018                         tuples = PQntuples(result);
2019
2020                 if (tuples > 0)
2021                 {
2022                         bool            have_heading;
2023                         int                     category;
2024
2025                         /*
2026                          * split the output into 4 different categories. Enabled triggers,
2027                          * disabled triggers and the two special ALWAYS and REPLICA
2028                          * configurations.
2029                          */
2030                         for (category = 0; category < 4; category++)
2031                         {
2032                                 have_heading = false;
2033                                 for (i = 0; i < tuples; i++)
2034                                 {
2035                                         bool            list_trigger;
2036                                         const char *tgdef;
2037                                         const char *usingpos;
2038                                         const char *tgenabled;
2039
2040                                         /*
2041                                          * Check if this trigger falls into the current category
2042                                          */
2043                                         tgenabled = PQgetvalue(result, i, 2);
2044                                         list_trigger = false;
2045                                         switch (category)
2046                                         {
2047                                                 case 0:
2048                                                         if (*tgenabled == 'O' || *tgenabled == 't')
2049                                                                 list_trigger = true;
2050                                                         break;
2051                                                 case 1:
2052                                                         if (*tgenabled == 'D' || *tgenabled == 'f')
2053                                                                 list_trigger = true;
2054                                                         break;
2055                                                 case 2:
2056                                                         if (*tgenabled == 'A')
2057                                                                 list_trigger = true;
2058                                                         break;
2059                                                 case 3:
2060                                                         if (*tgenabled == 'R')
2061                                                                 list_trigger = true;
2062                                                         break;
2063                                         }
2064                                         if (list_trigger == false)
2065                                                 continue;
2066
2067                                         /* Print the category heading once */
2068                                         if (have_heading == false)
2069                                         {
2070                                                 switch (category)
2071                                                 {
2072                                                         case 0:
2073                                                                 printfPQExpBuffer(&buf, _("Triggers:"));
2074                                                                 break;
2075                                                         case 1:
2076                                                                 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2077                                                                 break;
2078                                                         case 2:
2079                                                                 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2080                                                                 break;
2081                                                         case 3:
2082                                                                 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2083                                                                 break;
2084
2085                                                 }
2086                                                 printTableAddFooter(&cont, buf.data);
2087                                                 have_heading = true;
2088                                         }
2089
2090                                         /* Everything after "TRIGGER" is echoed verbatim */
2091                                         tgdef = PQgetvalue(result, i, 1);
2092                                         usingpos = strstr(tgdef, " TRIGGER ");
2093                                         if (usingpos)
2094                                                 tgdef = usingpos + 9;
2095
2096                                         printfPQExpBuffer(&buf, "    %s", tgdef);
2097                                         printTableAddFooter(&cont, buf.data);
2098                                 }
2099                         }
2100                 }
2101                 PQclear(result);
2102         }
2103
2104         /*
2105          * Finish printing the footer information about a table.
2106          */
2107         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
2108         {
2109                 PGresult   *result;
2110                 int                     tuples;
2111
2112                 /* print foreign server name */
2113                 if (tableinfo.relkind == 'f')
2114                 {
2115                         char *ftoptions;
2116
2117                         /* Footer information about foreign table */
2118                         printfPQExpBuffer(&buf,
2119                                                           "SELECT s.srvname,\n"
2120                                                           "       array_to_string(ARRAY(SELECT "
2121                                                           "       quote_ident(option_name) ||  ' ' || "
2122                                                           "       quote_literal(option_value)  FROM "
2123                                                           "       pg_options_to_table(ftoptions)),  ', ') "
2124                                                           "FROM pg_catalog.pg_foreign_table f,\n"
2125                                                           "     pg_catalog.pg_foreign_server s\n"
2126                                                           "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2127                                                           oid);
2128                         result = PSQLexec(buf.data, false);
2129                         if (!result)
2130                                 goto error_return;
2131                         else if (PQntuples(result) != 1)
2132                         {
2133                                 PQclear(result);
2134                                 goto error_return;
2135                         }
2136
2137                         /* Print server name */
2138                         printfPQExpBuffer(&buf, "Server: %s",
2139                                                           PQgetvalue(result, 0, 0));
2140                         printTableAddFooter(&cont, buf.data);
2141
2142                         /* Print per-table FDW options, if any */
2143                         ftoptions = PQgetvalue(result, 0, 1);
2144                         if (ftoptions && ftoptions[0] != '\0')
2145                         {
2146                                 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2147                                 printTableAddFooter(&cont, buf.data);
2148                         }
2149                         PQclear(result);
2150                 }
2151
2152                 /* print inherited tables */
2153                 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);
2154
2155                 result = PSQLexec(buf.data, false);
2156                 if (!result)
2157                         goto error_return;
2158                 else
2159                 {
2160                         const char *s = _("Inherits");
2161                         int                     sw = pg_wcswidth(s, strlen(s), pset.encoding);
2162
2163                         tuples = PQntuples(result);
2164
2165                         for (i = 0; i < tuples; i++)
2166                         {
2167                                 if (i == 0)
2168                                         printfPQExpBuffer(&buf, "%s: %s",
2169                                                                           s, PQgetvalue(result, i, 0));
2170                                 else
2171                                         printfPQExpBuffer(&buf, "%*s  %s",
2172                                                                           sw, "", PQgetvalue(result, i, 0));
2173                                 if (i < tuples - 1)
2174                                         appendPQExpBuffer(&buf, ",");
2175
2176                                 printTableAddFooter(&cont, buf.data);
2177                         }
2178
2179                         PQclear(result);
2180                 }
2181
2182                 /* print child tables */
2183                 if (pset.sversion >= 80300)
2184                         printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '%s' ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
2185                 else
2186                         printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '%s' ORDER BY c.relname;", oid);
2187
2188                 result = PSQLexec(buf.data, false);
2189                 if (!result)
2190                         goto error_return;
2191                 else
2192                         tuples = PQntuples(result);
2193
2194                 if (!verbose)
2195                 {
2196                         /* print the number of child tables, if any */
2197                         if (tuples > 0)
2198                         {
2199                                 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2200                                 printTableAddFooter(&cont, buf.data);
2201                         }
2202                 }
2203                 else
2204                 {
2205                         /* display the list of child tables */
2206                         const char *ct = _("Child tables");
2207                         int                     ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2208
2209                         for (i = 0; i < tuples; i++)
2210                         {
2211                                 if (i == 0)
2212                                         printfPQExpBuffer(&buf, "%s: %s",
2213                                                                           ct, PQgetvalue(result, i, 0));
2214                                 else
2215                                         printfPQExpBuffer(&buf, "%*s  %s",
2216                                                                           ctw, "", PQgetvalue(result, i, 0));
2217                                 if (i < tuples - 1)
2218                                         appendPQExpBuffer(&buf, ",");
2219
2220                                 printTableAddFooter(&cont, buf.data);
2221                         }
2222                 }
2223                 PQclear(result);
2224
2225                 /* Table type */
2226                 if (tableinfo.reloftype)
2227                 {
2228                         printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2229                         printTableAddFooter(&cont, buf.data);
2230                 }
2231
2232                 /* OIDs and options */
2233                 if (verbose)
2234                 {
2235                         const char *s = _("Has OIDs");
2236
2237                         printfPQExpBuffer(&buf, "%s: %s", s,
2238                                                           (tableinfo.hasoids ? _("yes") : _("no")));
2239                         printTableAddFooter(&cont, buf.data);
2240
2241                         /* print reloptions */
2242                         if (pset.sversion >= 80200)
2243                         {
2244                                 if (tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2245                                 {
2246                                         const char *t = _("Options");
2247
2248                                         printfPQExpBuffer(&buf, "%s: %s", t,
2249                                                                           tableinfo.reloptions);
2250                                         printTableAddFooter(&cont, buf.data);
2251                                 }
2252                         }
2253                 }
2254
2255                 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2256                                                           true);
2257         }
2258
2259         printTable(&cont, pset.queryFout, pset.logfile);
2260         printTableCleanup(&cont);
2261
2262         retval = true;
2263
2264 error_return:
2265
2266         /* clean up */
2267         if (printTableInitialized)
2268                 printTableCleanup(&cont);
2269         termPQExpBuffer(&buf);
2270         termPQExpBuffer(&title);
2271         termPQExpBuffer(&tmpbuf);
2272
2273         if (seq_values)
2274         {
2275                 for (ptr = seq_values; *ptr; ptr++)
2276                         free(*ptr);
2277                 free(seq_values);
2278         }
2279
2280         if (modifiers)
2281         {
2282                 for (ptr = modifiers; *ptr; ptr++)
2283                         free(*ptr);
2284                 free(modifiers);
2285         }
2286
2287         if (view_def)
2288                 free(view_def);
2289
2290         if (res)
2291                 PQclear(res);
2292
2293         return retval;
2294 }
2295
2296 /*
2297  * Add a tablespace description to a footer.  If 'newline' is true, it is added
2298  * in a new line; otherwise it's appended to the current value of the last
2299  * footer.
2300  */
2301 static void
2302 add_tablespace_footer(printTableContent *const cont, char relkind,
2303                                           Oid tablespace, const bool newline)
2304 {
2305         /* relkinds for which we support tablespaces */
2306         if (relkind == 'r' || relkind == 'i')
2307         {
2308                 /*
2309                  * We ignore the database default tablespace so that users not using
2310                  * tablespaces don't need to know about them.  This case also covers
2311                  * pre-8.0 servers, for which tablespace will always be 0.
2312                  */
2313                 if (tablespace != 0)
2314                 {
2315                         PGresult   *result = NULL;
2316                         PQExpBufferData buf;
2317
2318                         initPQExpBuffer(&buf);
2319                         printfPQExpBuffer(&buf,
2320                                                           "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2321                                                           "WHERE oid = '%u';", tablespace);
2322                         result = PSQLexec(buf.data, false);
2323                         if (!result)
2324                                 return;
2325                         /* Should always be the case, but.... */
2326                         if (PQntuples(result) > 0)
2327                         {
2328                                 if (newline)
2329                                 {
2330                                         /* Add the tablespace as a new footer */
2331                                         printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2332                                                                           PQgetvalue(result, 0, 0));
2333                                         printTableAddFooter(cont, buf.data);
2334                                 }
2335                                 else
2336                                 {
2337                                         /* Append the tablespace to the latest footer */
2338                                         printfPQExpBuffer(&buf, "%s", cont->footer->data);
2339
2340                                         /*
2341                                          * translator: before this string there's an index
2342                                          * description like '"foo_pkey" PRIMARY KEY, btree (a)'
2343                                          */
2344                                         appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2345                                                                           PQgetvalue(result, 0, 0));
2346                                         printTableSetFooter(cont, buf.data);
2347                                 }
2348                         }
2349                         PQclear(result);
2350                         termPQExpBuffer(&buf);
2351                 }
2352         }
2353 }
2354
2355 /*
2356  * \du or \dg
2357  *
2358  * Describes roles.  Any schema portion of the pattern is ignored.
2359  */
2360 bool
2361 describeRoles(const char *pattern, bool verbose)
2362 {
2363         PQExpBufferData buf;
2364         PGresult   *res;
2365         printTableContent cont;
2366         printTableOpt myopt = pset.popt.topt;
2367         int                     ncols = 3;
2368         int                     nrows = 0;
2369         int                     i;
2370         int                     conns;
2371         const char      align = 'l';
2372         char      **attr;
2373
2374         initPQExpBuffer(&buf);
2375
2376         if (pset.sversion >= 80100)
2377         {
2378                 printfPQExpBuffer(&buf,
2379                                                   "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2380                                                   "  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2381                                                   "  r.rolconnlimit, r.rolvaliduntil,\n"
2382                                                   "  ARRAY(SELECT b.rolname\n"
2383                                                   "        FROM pg_catalog.pg_auth_members m\n"
2384                                  "        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2385                                                   "        WHERE m.member = r.oid) as memberof");
2386
2387                 if (verbose && pset.sversion >= 80200)
2388                 {
2389                         appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2390                         ncols++;
2391                 }
2392                 if (pset.sversion >= 90100)
2393                 {
2394                         appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2395                 }
2396
2397                 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2398
2399                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2400                                                           NULL, "r.rolname", NULL, NULL);
2401         }
2402         else
2403         {
2404                 printfPQExpBuffer(&buf,
2405                                                   "SELECT u.usename AS rolname,\n"
2406                                                   "  u.usesuper AS rolsuper,\n"
2407                                                   "  true AS rolinherit, false AS rolcreaterole,\n"
2408                                          "  u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2409                                                   "  -1 AS rolconnlimit,"
2410                                                   "  u.valuntil as rolvaliduntil,\n"
2411                                                   "  ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2412                                                   "\nFROM pg_catalog.pg_user u\n");
2413
2414                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2415                                                           NULL, "u.usename", NULL, NULL);
2416         }
2417
2418         appendPQExpBuffer(&buf, "ORDER BY 1;");
2419
2420         res = PSQLexec(buf.data, false);
2421         if (!res)
2422                 return false;
2423
2424         nrows = PQntuples(res);
2425         attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
2426
2427         printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2428
2429         printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2430         printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2431         printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2432
2433         if (verbose && pset.sversion >= 80200)
2434                 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2435
2436         for (i = 0; i < nrows; i++)
2437         {
2438                 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2439
2440                 resetPQExpBuffer(&buf);
2441                 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2442                         add_role_attribute(&buf, _("Superuser"));
2443
2444                 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2445                         add_role_attribute(&buf, _("No inheritance"));
2446
2447                 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2448                         add_role_attribute(&buf, _("Create role"));
2449
2450                 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2451                         add_role_attribute(&buf, _("Create DB"));
2452
2453                 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2454                         add_role_attribute(&buf, _("Cannot login"));
2455
2456                 if (pset.sversion >= 90100)
2457                         if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2458                                 add_role_attribute(&buf, _("Replication"));
2459
2460                 conns = atoi(PQgetvalue(res, i, 6));
2461                 if (conns >= 0)
2462                 {
2463                         if (buf.len > 0)
2464                                 appendPQExpBufferStr(&buf, "\n");
2465
2466                         if (conns == 0)
2467                                 appendPQExpBuffer(&buf, _("No connections"));
2468                         else
2469                                 appendPQExpBuffer(&buf, ngettext("%d connection",
2470                                                                                                  "%d connections",
2471                                                                                                  conns),
2472                                                                   conns);
2473                 }
2474
2475                 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2476                 {
2477                         if (buf.len > 0)
2478                                 appendPQExpBufferStr(&buf, "\n");
2479                         appendPQExpBufferStr(&buf, _("Password valid until "));
2480                         appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
2481                 }
2482
2483                 attr[i] = pg_strdup(buf.data);
2484
2485                 printTableAddCell(&cont, attr[i], false, false);
2486
2487                 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2488
2489                 if (verbose && pset.sversion >= 80200)
2490                         printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
2491         }
2492         termPQExpBuffer(&buf);
2493
2494         printTable(&cont, pset.queryFout, pset.logfile);
2495         printTableCleanup(&cont);
2496
2497         for (i = 0; i < nrows; i++)
2498                 free(attr[i]);
2499         free(attr);
2500
2501         PQclear(res);
2502         return true;
2503 }
2504
2505 static void
2506 add_role_attribute(PQExpBuffer buf, const char *const str)
2507 {
2508         if (buf->len > 0)
2509                 appendPQExpBufferStr(buf, ", ");
2510
2511         appendPQExpBufferStr(buf, str);
2512 }
2513
2514 /*
2515  * \drds
2516  */
2517 bool
2518 listDbRoleSettings(const char *pattern, const char *pattern2)
2519 {
2520         PQExpBufferData buf;
2521         PGresult   *res;
2522         printQueryOpt myopt = pset.popt;
2523
2524         initPQExpBuffer(&buf);
2525
2526         if (pset.sversion >= 90000)
2527         {
2528                 bool            havewhere;
2529
2530                 printfPQExpBuffer(&buf, "SELECT rolname AS role, datname AS database,\n"
2531                                 "pg_catalog.array_to_string(setconfig, E'\\n') AS settings\n"
2532                                                   "FROM pg_db_role_setting AS s\n"
2533                                    "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2534                                                   "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n");
2535                 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2536                                                                            NULL, "pg_roles.rolname", NULL, NULL);
2537                 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2538                                                           NULL, "pg_database.datname", NULL, NULL);
2539                 appendPQExpBufferStr(&buf, "ORDER BY role, database;");
2540         }
2541         else
2542         {
2543                 fprintf(pset.queryFout,
2544                 _("No per-database role settings support in this server version.\n"));
2545                 return false;
2546         }
2547
2548         res = PSQLexec(buf.data, false);
2549         if (!res)
2550                 return false;
2551
2552         if (PQntuples(res) == 0 && !pset.quiet)
2553         {
2554                 if (pattern)
2555                         fprintf(pset.queryFout, _("No matching settings found.\n"));
2556                 else
2557                         fprintf(pset.queryFout, _("No settings found.\n"));
2558         }
2559         else
2560         {
2561                 myopt.nullPrint = NULL;
2562                 myopt.title = _("List of settings");
2563                 myopt.translate_header = true;
2564
2565                 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2566         }
2567
2568         PQclear(res);
2569         resetPQExpBuffer(&buf);
2570         return true;
2571 }
2572
2573
2574 /*
2575  * listTables()
2576  *
2577  * handler for \dt, \di, etc.
2578  *
2579  * tabtypes is an array of characters, specifying what info is desired:
2580  * t - tables
2581  * i - indexes
2582  * v - views
2583  * s - sequences
2584  * E - foreign table (Note: different from 'f', the relkind value)
2585  * (any order of the above is fine)
2586  * If tabtypes is empty, we default to \dtvsE.
2587  */
2588 bool
2589 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2590 {
2591         bool            showTables = strchr(tabtypes, 't') != NULL;
2592         bool            showIndexes = strchr(tabtypes, 'i') != NULL;
2593         bool            showViews = strchr(tabtypes, 'v') != NULL;
2594         bool            showSeq = strchr(tabtypes, 's') != NULL;
2595         bool            showForeign = strchr(tabtypes, 'E') != NULL;
2596
2597         PQExpBufferData buf;
2598         PGresult   *res;
2599         printQueryOpt myopt = pset.popt;
2600         static const bool translate_columns[] = {false, false, true, false, false, false, false};
2601
2602         if (!(showTables || showIndexes || showViews || showSeq || showForeign))
2603                 showTables = showViews = showSeq = showForeign = true;
2604
2605         initPQExpBuffer(&buf);
2606
2607         /*
2608          * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2609          * for backwards compatibility.
2610          */
2611         printfPQExpBuffer(&buf,
2612                                           "SELECT n.nspname as \"%s\",\n"
2613                                           "  c.relname as \"%s\",\n"
2614                                           "  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"
2615                                           "  pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2616                                           gettext_noop("Schema"),
2617                                           gettext_noop("Name"),
2618                                           gettext_noop("table"),
2619                                           gettext_noop("view"),
2620                                           gettext_noop("index"),
2621                                           gettext_noop("sequence"),
2622                                           gettext_noop("special"),
2623                                           gettext_noop("foreign table"),
2624                                           gettext_noop("Type"),
2625                                           gettext_noop("Owner"));
2626
2627         if (showIndexes)
2628                 appendPQExpBuffer(&buf,
2629                                                   ",\n c2.relname as \"%s\"",
2630                                                   gettext_noop("Table"));
2631
2632         if (verbose)
2633         {
2634                 /*
2635                  * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
2636                  * size of a table, including FSM, VM and TOAST tables.
2637                  */
2638                 if (pset.sversion >= 90000)
2639                         appendPQExpBuffer(&buf,
2640                                                           ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
2641                                                           gettext_noop("Size"));
2642                 else if (pset.sversion >= 80100)
2643                         appendPQExpBuffer(&buf,
2644                                                           ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2645                                                           gettext_noop("Size"));
2646
2647                 appendPQExpBuffer(&buf,
2648                           ",\n  pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2649                                                   gettext_noop("Description"));
2650         }
2651
2652         appendPQExpBuffer(&buf,
2653                                           "\nFROM pg_catalog.pg_class c"
2654          "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2655         if (showIndexes)
2656                 appendPQExpBuffer(&buf,
2657                          "\n     LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2658                    "\n     LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2659
2660         appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
2661         if (showTables)
2662                 appendPQExpBuffer(&buf, "'r',");
2663         if (showViews)
2664                 appendPQExpBuffer(&buf, "'v',");
2665         if (showIndexes)
2666                 appendPQExpBuffer(&buf, "'i',");
2667         if (showSeq)
2668                 appendPQExpBuffer(&buf, "'S',");
2669         if (showSystem || pattern)
2670                 appendPQExpBuffer(&buf, "'s',");                /* was RELKIND_SPECIAL in <=
2671                                                                                                  * 8.1 */
2672         if (showForeign)
2673                 appendPQExpBuffer(&buf, "'f',");
2674
2675         appendPQExpBuffer(&buf, "''");          /* dummy */
2676         appendPQExpBuffer(&buf, ")\n");
2677
2678         if (!showSystem && !pattern)
2679                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
2680                                                   "      AND n.nspname <> 'information_schema'\n");
2681
2682         /*
2683          * TOAST objects are suppressed unconditionally.  Since we don't provide
2684          * any way to select relkind 't' above, we would never show toast tables
2685          * in any case; it seems a bit confusing to allow their indexes to be
2686          * shown. Use plain \d if you really need to look at a TOAST table/index.
2687          */
2688         appendPQExpBuffer(&buf, "      AND n.nspname !~ '^pg_toast'\n");
2689
2690         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2691                                                   "n.nspname", "c.relname", NULL,
2692                                                   "pg_catalog.pg_table_is_visible(c.oid)");
2693
2694         appendPQExpBuffer(&buf, "ORDER BY 1,2;");
2695
2696         res = PSQLexec(buf.data, false);
2697         termPQExpBuffer(&buf);
2698         if (!res)
2699                 return false;
2700
2701         if (PQntuples(res) == 0 && !pset.quiet)
2702         {
2703                 if (pattern)
2704                         fprintf(pset.queryFout, _("No matching relations found.\n"));
2705                 else
2706                         fprintf(pset.queryFout, _("No relations found.\n"));
2707         }
2708         else
2709         {
2710                 myopt.nullPrint = NULL;
2711                 myopt.title = _("List of relations");
2712                 myopt.translate_header = true;
2713                 myopt.translate_columns = translate_columns;
2714
2715                 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2716         }
2717
2718         PQclear(res);
2719         return true;
2720 }
2721
2722
2723 /*
2724  * \dL
2725  *
2726  * Describes languages.
2727  */
2728 bool
2729 listLanguages(const char *pattern, bool verbose, bool showSystem)
2730 {
2731         PQExpBufferData buf;
2732         PGresult   *res;
2733         printQueryOpt myopt = pset.popt;
2734
2735         initPQExpBuffer(&buf);
2736
2737         printfPQExpBuffer(&buf,
2738                                           "SELECT l.lanname AS \"%s\",\n",
2739                                           gettext_noop("Name"));
2740         if (pset.sversion >= 80300)
2741                 appendPQExpBuffer(&buf,
2742                                 "       pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
2743                                                   gettext_noop("Owner"));
2744
2745         appendPQExpBuffer(&buf,
2746                                           "       l.lanpltrusted AS \"%s\"",
2747                                           gettext_noop("Trusted"));
2748
2749         if (verbose)
2750         {
2751                 appendPQExpBuffer(&buf,
2752                                                   ",\n       NOT l.lanispl AS \"%s\",\n"
2753                                                   "       l.lanplcallfoid::regprocedure AS \"%s\",\n"
2754                                    "       l.lanvalidator::regprocedure AS \"%s\",\n       ",
2755                                                   gettext_noop("Internal Language"),
2756                                                   gettext_noop("Call Handler"),
2757                                                   gettext_noop("Validator"));
2758                 if (pset.sversion >= 90000)
2759                         appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n       ",
2760                                                           gettext_noop("Inline Handler"));
2761                 printACLColumn(&buf, "l.lanacl");
2762         }
2763
2764         appendPQExpBuffer(&buf,
2765                                           ",\n       d.description AS \"%s\""
2766                                           "\nFROM pg_catalog.pg_language l\n"
2767                                           "LEFT JOIN pg_catalog.pg_description d\n"
2768                                           "  ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
2769                                           "  AND d.objsubid = 0\n",
2770                                           gettext_noop("Description"));
2771
2772         if (pattern)
2773                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2774                                                           NULL, "l.lanname", NULL, NULL);
2775
2776         if (!showSystem && !pattern)
2777                 appendPQExpBuffer(&buf, "WHERE l.lanplcallfoid != 0\n");
2778
2779
2780         appendPQExpBuffer(&buf, "ORDER BY 1;");
2781
2782         res = PSQLexec(buf.data, false);
2783         termPQExpBuffer(&buf);
2784         if (!res)
2785                 return false;
2786
2787         myopt.nullPrint = NULL;
2788         myopt.title = _("List of languages");
2789         myopt.translate_header = true;
2790
2791         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2792
2793         PQclear(res);
2794         return true;
2795 }
2796
2797
2798 /*
2799  * \dD
2800  *
2801  * Describes domains.
2802  */
2803 bool
2804 listDomains(const char *pattern, bool verbose, bool showSystem)
2805 {
2806         PQExpBufferData buf;
2807         PGresult   *res;
2808         printQueryOpt myopt = pset.popt;
2809
2810         initPQExpBuffer(&buf);
2811
2812         printfPQExpBuffer(&buf,
2813                                           "SELECT n.nspname as \"%s\",\n"
2814                                           "       t.typname as \"%s\",\n"
2815          "       pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2816                                           "       TRIM(LEADING\n",
2817                                           gettext_noop("Schema"),
2818                                           gettext_noop("Name"),
2819                                           gettext_noop("Type"));
2820
2821         if (pset.sversion >= 90100)
2822                 appendPQExpBuffer(&buf,
2823                                                   "            COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
2824                                                   "                      WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
2825         appendPQExpBuffer(&buf,
2826            "            CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
2827                                           "            CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
2828                                           "       ) as \"%s\",\n"
2829                                           "       pg_catalog.array_to_string(ARRAY(\n"
2830                                           "         SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2831                                           "       ), ' ') as \"%s\"",
2832                                           gettext_noop("Modifier"),
2833                                           gettext_noop("Check"));
2834
2835         if (verbose)
2836         {
2837                 if (pset.sversion >= 90200)
2838                 {
2839                         appendPQExpBuffer(&buf, ",\n  ");
2840                         printACLColumn(&buf, "t.typacl");
2841                 }
2842                 appendPQExpBuffer(&buf,
2843                                                   ",\n       d.description as \"%s\"",
2844                                                   gettext_noop("Description"));
2845         }
2846
2847         appendPQExpBuffer(&buf,
2848                                           "\nFROM pg_catalog.pg_type t\n"
2849            "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
2850
2851         if (verbose)
2852                 appendPQExpBuffer(&buf,
2853                                                   "     LEFT JOIN pg_catalog.pg_description d "
2854                                                   "ON d.classoid = t.tableoid AND d.objoid = t.oid "
2855                                                   "AND d.objsubid = 0\n");
2856
2857         appendPQExpBuffer(&buf, "WHERE t.typtype = 'd'\n");
2858
2859         if (!showSystem && !pattern)
2860                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
2861                                                   "      AND n.nspname <> 'information_schema'\n");
2862
2863         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2864                                                   "n.nspname", "t.typname", NULL,
2865                                                   "pg_catalog.pg_type_is_visible(t.oid)");
2866
2867         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2868
2869         res = PSQLexec(buf.data, false);
2870         termPQExpBuffer(&buf);
2871         if (!res)
2872                 return false;
2873
2874         myopt.nullPrint = NULL;
2875         myopt.title = _("List of domains");
2876         myopt.translate_header = true;
2877
2878         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2879
2880         PQclear(res);
2881         return true;
2882 }
2883
2884 /*
2885  * \dc
2886  *
2887  * Describes conversions.
2888  */
2889 bool
2890 listConversions(const char *pattern, bool verbose, bool showSystem)
2891 {
2892         PQExpBufferData buf;
2893         PGresult   *res;
2894         printQueryOpt myopt = pset.popt;
2895         static const bool translate_columns[] = {false, false, false, false, true};
2896
2897         initPQExpBuffer(&buf);
2898
2899         printfPQExpBuffer(&buf,
2900                                           "SELECT n.nspname AS \"%s\",\n"
2901                                           "       c.conname AS \"%s\",\n"
2902            "       pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2903                 "       pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2904                                           "       CASE WHEN c.condefault THEN '%s'\n"
2905                                           "       ELSE '%s' END AS \"%s\"",
2906                                           gettext_noop("Schema"),
2907                                           gettext_noop("Name"),
2908                                           gettext_noop("Source"),
2909                                           gettext_noop("Destination"),
2910                                           gettext_noop("yes"), gettext_noop("no"),
2911                                           gettext_noop("Default?"));
2912
2913         if (verbose)
2914                 appendPQExpBuffer(&buf,
2915                                                   ",\n       d.description AS \"%s\"",
2916                                                   gettext_noop("Description"));
2917
2918         appendPQExpBuffer(&buf,
2919                                           "\nFROM pg_catalog.pg_conversion c\n"
2920                                           "     JOIN pg_catalog.pg_namespace n "
2921                                           "ON n.oid = c.connamespace\n");
2922
2923         if (verbose)
2924                 appendPQExpBuffer(&buf,
2925                                                   "LEFT JOIN pg_catalog.pg_description d "
2926                                                   "ON d.classoid = c.tableoid\n"
2927                                                   "          AND d.objoid = c.oid "
2928                                                   "AND d.objsubid = 0\n");
2929
2930         appendPQExpBuffer(&buf, "WHERE true\n");
2931
2932         if (!showSystem && !pattern)
2933                 appendPQExpBuffer(&buf, "  AND n.nspname <> 'pg_catalog'\n"
2934                                                   "  AND n.nspname <> 'information_schema'\n");
2935
2936         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2937                                                   "n.nspname", "c.conname", NULL,
2938                                                   "pg_catalog.pg_conversion_is_visible(c.oid)");
2939
2940         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2941
2942         res = PSQLexec(buf.data, false);
2943         termPQExpBuffer(&buf);
2944         if (!res)
2945                 return false;
2946
2947         myopt.nullPrint = NULL;
2948         myopt.title = _("List of conversions");
2949         myopt.translate_header = true;
2950         myopt.translate_columns = translate_columns;
2951
2952         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2953
2954         PQclear(res);
2955         return true;
2956 }
2957
2958 /*
2959  * \dC
2960  *
2961  * Describes casts.
2962  */
2963 bool
2964 listCasts(const char *pattern, bool verbose)
2965 {
2966         PQExpBufferData buf;
2967         PGresult   *res;
2968         printQueryOpt myopt = pset.popt;
2969         static const bool translate_columns[] = {false, false, false, true};
2970
2971         initPQExpBuffer(&buf);
2972
2973         /*
2974          * We need a left join to pg_proc for binary casts; the others are just
2975          * paranoia.  Also note that we don't attempt to localize '(binary
2976          * coercible)', because there's too much risk of gettext translating a
2977          * function name that happens to match some string in the PO database.
2978          */
2979         printfPQExpBuffer(&buf,
2980                            "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2981                            "       pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2982                                   "       CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
2983                                           "            ELSE p.proname\n"
2984                                           "       END as \"%s\",\n"
2985                                           "       CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2986                                           "            WHEN c.castcontext = 'a' THEN '%s'\n"
2987                                           "            ELSE '%s'\n"
2988                                           "       END as \"%s\"",
2989                                           gettext_noop("Source type"),
2990                                           gettext_noop("Target type"),
2991                                           gettext_noop("Function"),
2992                                           gettext_noop("no"),
2993                                           gettext_noop("in assignment"),
2994                                           gettext_noop("yes"),
2995                                           gettext_noop("Implicit?"));
2996
2997         if (verbose)
2998                 appendPQExpBuffer(&buf,
2999                                                   ",\n       d.description AS \"%s\"\n",
3000                                                   gettext_noop("Description"));
3001
3002         appendPQExpBuffer(&buf,
3003                                  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3004                                           "     ON c.castfunc = p.oid\n"
3005                                           "     LEFT JOIN pg_catalog.pg_type ts\n"
3006                                           "     ON c.castsource = ts.oid\n"
3007                                           "     LEFT JOIN pg_catalog.pg_namespace ns\n"
3008                                           "     ON ns.oid = ts.typnamespace\n"
3009                                           "     LEFT JOIN pg_catalog.pg_type tt\n"
3010                                           "     ON c.casttarget = tt.oid\n"
3011                                           "     LEFT JOIN pg_catalog.pg_namespace nt\n"
3012                                           "     ON nt.oid = tt.typnamespace\n");
3013
3014         if (verbose)
3015                 appendPQExpBuffer(&buf,
3016                                                   "     LEFT JOIN pg_catalog.pg_description d\n"
3017                                                   "     ON d.classoid = c.tableoid AND d.objoid = "
3018                                                   "c.oid AND d.objsubid = 0\n");
3019
3020         appendPQExpBuffer(&buf, "WHERE ( (true");
3021
3022         /*
3023          * Match name pattern against either internal or external name of either
3024          * castsource or casttarget
3025          */
3026         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3027                                                   "ns.nspname", "ts.typname",
3028                                                   "pg_catalog.format_type(ts.oid, NULL)",
3029                                                   "pg_catalog.pg_type_is_visible(ts.oid)");
3030
3031         appendPQExpBuffer(&buf, ") OR (true");
3032
3033         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3034                                                   "nt.nspname", "tt.typname",
3035                                                   "pg_catalog.format_type(tt.oid, NULL)",
3036                                                   "pg_catalog.pg_type_is_visible(tt.oid)");
3037
3038         appendPQExpBuffer(&buf, ") )\nORDER BY 1, 2;");
3039
3040         res = PSQLexec(buf.data, false);
3041         termPQExpBuffer(&buf);
3042         if (!res)
3043                 return false;
3044
3045         myopt.nullPrint = NULL;
3046         myopt.title = _("List of casts");
3047         myopt.translate_header = true;
3048         myopt.translate_columns = translate_columns;
3049
3050         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3051
3052         PQclear(res);
3053         return true;
3054 }
3055
3056 /*
3057  * \dO
3058  *
3059  * Describes collations.
3060  */
3061 bool
3062 listCollations(const char *pattern, bool verbose, bool showSystem)
3063 {
3064         PQExpBufferData buf;
3065         PGresult   *res;
3066         printQueryOpt myopt = pset.popt;
3067         static const bool translate_columns[] = {false, false, false, false, false};
3068
3069         initPQExpBuffer(&buf);
3070
3071         printfPQExpBuffer(&buf,
3072                                           "SELECT n.nspname AS \"%s\",\n"
3073                                           "       c.collname AS \"%s\",\n"
3074                                           "       c.collcollate AS \"%s\",\n"
3075                                           "       c.collctype AS \"%s\"",
3076                                           gettext_noop("Schema"),
3077                                           gettext_noop("Name"),
3078                                           gettext_noop("Collate"),
3079                                           gettext_noop("Ctype"));
3080
3081         if (verbose)
3082                 appendPQExpBuffer(&buf,
3083                                                   ",\n       pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3084                                                   gettext_noop("Description"));
3085
3086         appendPQExpBuffer(&buf,
3087                           "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3088                                           "WHERE n.oid = c.collnamespace\n");
3089
3090         if (!showSystem && !pattern)
3091                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
3092                                                   "      AND n.nspname <> 'information_schema'\n");
3093
3094         /*
3095          * Hide collations that aren't usable in the current database's encoding.
3096          * If you think to change this, note that pg_collation_is_visible rejects
3097          * unusable collations, so you will need to hack name pattern processing
3098          * somehow to avoid inconsistent behavior.
3099          */
3100         appendPQExpBuffer(&buf, "      AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3101
3102         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3103                                                   "n.nspname", "c.collname", NULL,
3104                                                   "pg_catalog.pg_collation_is_visible(c.oid)");
3105
3106         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3107
3108         res = PSQLexec(buf.data, false);
3109         termPQExpBuffer(&buf);
3110         if (!res)
3111                 return false;
3112
3113         myopt.nullPrint = NULL;
3114         myopt.title = _("List of collations");
3115         myopt.translate_header = true;
3116         myopt.translate_columns = translate_columns;
3117
3118         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3119
3120         PQclear(res);
3121         return true;
3122 }
3123
3124 /*
3125  * \dn
3126  *
3127  * Describes schemas (namespaces)
3128  */
3129 bool
3130 listSchemas(const char *pattern, bool verbose, bool showSystem)
3131 {
3132         PQExpBufferData buf;
3133         PGresult   *res;
3134         printQueryOpt myopt = pset.popt;
3135
3136         initPQExpBuffer(&buf);
3137         printfPQExpBuffer(&buf,
3138                                           "SELECT n.nspname AS \"%s\",\n"
3139                                           "  pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3140                                           gettext_noop("Name"),
3141                                           gettext_noop("Owner"));
3142
3143         if (verbose)
3144         {
3145                 appendPQExpBuffer(&buf, ",\n  ");
3146                 printACLColumn(&buf, "n.nspacl");
3147                 appendPQExpBuffer(&buf,
3148                   ",\n  pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3149                                                   gettext_noop("Description"));
3150         }
3151
3152         appendPQExpBuffer(&buf,
3153                                           "\nFROM pg_catalog.pg_namespace n\n");
3154
3155         if (!showSystem && !pattern)
3156                 appendPQExpBuffer(&buf,
3157                 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3158
3159         processSQLNamePattern(pset.db, &buf, pattern,
3160                                                   !showSystem && !pattern, false,
3161                                                   NULL, "n.nspname", NULL,
3162                                                   NULL);
3163
3164         appendPQExpBuffer(&buf, "ORDER BY 1;");
3165
3166         res = PSQLexec(buf.data, false);
3167         termPQExpBuffer(&buf);
3168         if (!res)
3169                 return false;
3170
3171         myopt.nullPrint = NULL;
3172         myopt.title = _("List of schemas");
3173         myopt.translate_header = true;
3174
3175         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3176
3177         PQclear(res);
3178         return true;
3179 }
3180
3181
3182 /*
3183  * \dFp
3184  * list text search parsers
3185  */
3186 bool
3187 listTSParsers(const char *pattern, bool verbose)
3188 {
3189         PQExpBufferData buf;
3190         PGresult   *res;
3191         printQueryOpt myopt = pset.popt;
3192
3193         if (pset.sversion < 80300)
3194         {
3195                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3196                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3197                 return true;
3198         }
3199
3200         if (verbose)
3201                 return listTSParsersVerbose(pattern);
3202
3203         initPQExpBuffer(&buf);
3204
3205         printfPQExpBuffer(&buf,
3206                                           "SELECT \n"
3207                                           "  n.nspname as \"%s\",\n"
3208                                           "  p.prsname as \"%s\",\n"
3209                         "  pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3210                                           "FROM pg_catalog.pg_ts_parser p \n"
3211                    "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3212                                           gettext_noop("Schema"),
3213                                           gettext_noop("Name"),
3214                                           gettext_noop("Description")
3215                 );
3216
3217         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3218                                                   "n.nspname", "p.prsname", NULL,
3219                                                   "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3220
3221         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3222
3223         res = PSQLexec(buf.data, false);
3224         termPQExpBuffer(&buf);
3225         if (!res)
3226                 return false;
3227
3228         myopt.nullPrint = NULL;
3229         myopt.title = _("List of text search parsers");
3230         myopt.translate_header = true;
3231
3232         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3233
3234         PQclear(res);
3235         return true;
3236 }
3237
3238 /*
3239  * full description of parsers
3240  */
3241 static bool
3242 listTSParsersVerbose(const char *pattern)
3243 {
3244         PQExpBufferData buf;
3245         PGresult   *res;
3246         int                     i;
3247
3248         initPQExpBuffer(&buf);
3249
3250         printfPQExpBuffer(&buf,
3251                                           "SELECT p.oid, \n"
3252                                           "  n.nspname, \n"
3253                                           "  p.prsname \n"
3254                                           "FROM pg_catalog.pg_ts_parser p\n"
3255                         "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3256                 );
3257
3258         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3259                                                   "n.nspname", "p.prsname", NULL,
3260                                                   "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3261
3262         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3263
3264         res = PSQLexec(buf.data, false);
3265         termPQExpBuffer(&buf);
3266         if (!res)
3267                 return false;
3268
3269         if (PQntuples(res) == 0)
3270         {
3271                 if (!pset.quiet)
3272                         fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
3273                                         pattern);
3274                 PQclear(res);
3275                 return false;
3276         }
3277
3278         for (i = 0; i < PQntuples(res); i++)
3279         {
3280                 const char *oid;
3281                 const char *nspname = NULL;
3282                 const char *prsname;
3283
3284                 oid = PQgetvalue(res, i, 0);
3285                 if (!PQgetisnull(res, i, 1))
3286                         nspname = PQgetvalue(res, i, 1);
3287                 prsname = PQgetvalue(res, i, 2);
3288
3289                 if (!describeOneTSParser(oid, nspname, prsname))
3290                 {
3291                         PQclear(res);
3292                         return false;
3293                 }
3294
3295                 if (cancel_pressed)
3296                 {
3297                         PQclear(res);
3298                         return false;
3299                 }
3300         }
3301
3302         PQclear(res);
3303         return true;
3304 }
3305
3306 static bool
3307 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3308 {
3309         PQExpBufferData buf;
3310         PGresult   *res;
3311         char            title[1024];
3312         printQueryOpt myopt = pset.popt;
3313         static const bool translate_columns[] = {true, false, false};
3314
3315         initPQExpBuffer(&buf);
3316
3317         printfPQExpBuffer(&buf,
3318                                           "SELECT '%s' AS \"%s\", \n"
3319                                           "   p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3320                   "   pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3321                                           " FROM pg_catalog.pg_ts_parser p \n"
3322                                           " WHERE p.oid = '%s' \n"
3323                                           "UNION ALL \n"
3324                                           "SELECT '%s', \n"
3325                                           "   p.prstoken::pg_catalog.regproc, \n"
3326                                         "   pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3327                                           " FROM pg_catalog.pg_ts_parser p \n"
3328                                           " WHERE p.oid = '%s' \n"
3329                                           "UNION ALL \n"
3330                                           "SELECT '%s', \n"
3331                                           "   p.prsend::pg_catalog.regproc, \n"
3332                                           "   pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3333                                           " FROM pg_catalog.pg_ts_parser p \n"
3334                                           " WHERE p.oid = '%s' \n"
3335                                           "UNION ALL \n"
3336                                           "SELECT '%s', \n"
3337                                           "   p.prsheadline::pg_catalog.regproc, \n"
3338                                  "   pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3339                                           " FROM pg_catalog.pg_ts_parser p \n"
3340                                           " WHERE p.oid = '%s' \n"
3341                                           "UNION ALL \n"
3342                                           "SELECT '%s', \n"
3343                                           "   p.prslextype::pg_catalog.regproc, \n"
3344                                   "   pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3345                                           " FROM pg_catalog.pg_ts_parser p \n"
3346                                           " WHERE p.oid = '%s';",
3347                                           gettext_noop("Start parse"),
3348                                           gettext_noop("Method"),
3349                                           gettext_noop("Function"),
3350                                           gettext_noop("Description"),
3351                                           oid,
3352                                           gettext_noop("Get next token"),
3353                                           oid,
3354                                           gettext_noop("End parse"),
3355                                           oid,
3356                                           gettext_noop("Get headline"),
3357                                           oid,
3358                                           gettext_noop("Get token types"),
3359                                           oid);
3360
3361         res = PSQLexec(buf.data, false);
3362         termPQExpBuffer(&buf);
3363         if (!res)
3364                 return false;
3365
3366         myopt.nullPrint = NULL;
3367         if (nspname)
3368                 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3369         else
3370                 sprintf(title, _("Text search parser \"%s\""), prsname);
3371         myopt.title = title;
3372         myopt.footers = NULL;
3373         myopt.default_footer = false;
3374         myopt.translate_header = true;
3375         myopt.translate_columns = translate_columns;
3376
3377         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3378
3379         PQclear(res);
3380
3381         initPQExpBuffer(&buf);
3382
3383         printfPQExpBuffer(&buf,
3384                                           "SELECT t.alias as \"%s\", \n"
3385                                           "  t.description as \"%s\" \n"
3386                           "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3387                                           "ORDER BY 1;",
3388                                           gettext_noop("Token name"),
3389                                           gettext_noop("Description"),
3390                                           oid);
3391
3392         res = PSQLexec(buf.data, false);
3393         termPQExpBuffer(&buf);
3394         if (!res)
3395                 return false;
3396
3397         myopt.nullPrint = NULL;
3398         if (nspname)
3399                 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3400         else
3401                 sprintf(title, _("Token types for parser \"%s\""), prsname);
3402         myopt.title = title;
3403         myopt.footers = NULL;
3404         myopt.default_footer = true;
3405         myopt.translate_header = true;
3406         myopt.translate_columns = NULL;
3407
3408         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3409
3410         PQclear(res);
3411         return true;
3412 }
3413
3414
3415 /*
3416  * \dFd
3417  * list text search dictionaries
3418  */
3419 bool
3420 listTSDictionaries(const char *pattern, bool verbose)
3421 {
3422         PQExpBufferData buf;
3423         PGresult   *res;
3424         printQueryOpt myopt = pset.popt;
3425
3426         if (pset.sversion < 80300)
3427         {
3428                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3429                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3430                 return true;
3431         }
3432
3433         initPQExpBuffer(&buf);
3434
3435         printfPQExpBuffer(&buf,
3436                                           "SELECT \n"
3437                                           "  n.nspname as \"%s\",\n"
3438                                           "  d.dictname as \"%s\",\n",
3439                                           gettext_noop("Schema"),
3440                                           gettext_noop("Name"));
3441
3442         if (verbose)
3443         {
3444                 appendPQExpBuffer(&buf,
3445                                                   "  ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3446                                                   "    pg_catalog.pg_ts_template t \n"
3447                                                   "                      LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3448                                                   "                      WHERE d.dicttemplate = t.oid ) AS  \"%s\", \n"
3449                                                   "  d.dictinitoption as \"%s\", \n",
3450                                                   gettext_noop("Template"),
3451                                                   gettext_noop("Init options"));
3452         }
3453
3454         appendPQExpBuffer(&buf,
3455                          "  pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3456                                           gettext_noop("Description"));
3457
3458         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3459                  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3460
3461         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3462                                                   "n.nspname", "d.dictname", NULL,
3463                                                   "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3464
3465         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3466
3467         res = PSQLexec(buf.data, false);
3468         termPQExpBuffer(&buf);
3469         if (!res)
3470                 return false;
3471
3472         myopt.nullPrint = NULL;
3473         myopt.title = _("List of text search dictionaries");
3474         myopt.translate_header = true;
3475
3476         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3477
3478         PQclear(res);
3479         return true;
3480 }
3481
3482
3483 /*
3484  * \dFt
3485  * list text search templates
3486  */
3487 bool
3488 listTSTemplates(const char *pattern, bool verbose)
3489 {
3490         PQExpBufferData buf;
3491         PGresult   *res;
3492         printQueryOpt myopt = pset.popt;
3493
3494         if (pset.sversion < 80300)
3495         {
3496                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3497                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3498                 return true;
3499         }
3500
3501         initPQExpBuffer(&buf);
3502
3503         if (verbose)
3504                 printfPQExpBuffer(&buf,
3505                                                   "SELECT \n"
3506                                                   "  n.nspname AS \"%s\",\n"
3507                                                   "  t.tmplname AS \"%s\",\n"
3508                                                   "  t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3509                                                   "  t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3510                  "  pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3511                                                   gettext_noop("Schema"),
3512                                                   gettext_noop("Name"),
3513                                                   gettext_noop("Init"),
3514                                                   gettext_noop("Lexize"),
3515                                                   gettext_noop("Description"));
3516         else
3517                 printfPQExpBuffer(&buf,
3518                                                   "SELECT \n"
3519                                                   "  n.nspname AS \"%s\",\n"
3520                                                   "  t.tmplname AS \"%s\",\n"
3521                  "  pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3522                                                   gettext_noop("Schema"),
3523                                                   gettext_noop("Name"),
3524                                                   gettext_noop("Description"));
3525
3526         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
3527                  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3528
3529         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3530                                                   "n.nspname", "t.tmplname", NULL,
3531                                                   "pg_catalog.pg_ts_template_is_visible(t.oid)");
3532
3533         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3534
3535         res = PSQLexec(buf.data, false);
3536         termPQExpBuffer(&buf);
3537         if (!res)
3538                 return false;
3539
3540         myopt.nullPrint = NULL;
3541         myopt.title = _("List of text search templates");
3542         myopt.translate_header = true;
3543
3544         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3545
3546         PQclear(res);
3547         return true;
3548 }
3549
3550
3551 /*
3552  * \dF
3553  * list text search configurations
3554  */
3555 bool
3556 listTSConfigs(const char *pattern, bool verbose)
3557 {
3558         PQExpBufferData buf;
3559         PGresult   *res;
3560         printQueryOpt myopt = pset.popt;
3561
3562         if (pset.sversion < 80300)
3563         {
3564                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3565                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3566                 return true;
3567         }
3568
3569         if (verbose)
3570                 return listTSConfigsVerbose(pattern);
3571
3572         initPQExpBuffer(&buf);
3573
3574         printfPQExpBuffer(&buf,
3575                                           "SELECT \n"
3576                                           "   n.nspname as \"%s\",\n"
3577                                           "   c.cfgname as \"%s\",\n"
3578                    "   pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3579                                           "FROM pg_catalog.pg_ts_config c\n"
3580                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3581                                           gettext_noop("Schema"),
3582                                           gettext_noop("Name"),
3583                                           gettext_noop("Description")
3584                 );
3585
3586         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3587                                                   "n.nspname", "c.cfgname", NULL,
3588                                                   "pg_catalog.pg_ts_config_is_visible(c.oid)");
3589
3590         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3591
3592         res = PSQLexec(buf.data, false);
3593         termPQExpBuffer(&buf);
3594         if (!res)
3595                 return false;
3596
3597         myopt.nullPrint = NULL;
3598         myopt.title = _("List of text search configurations");
3599         myopt.translate_header = true;
3600
3601         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3602
3603         PQclear(res);
3604         return true;
3605 }
3606
3607 static bool
3608 listTSConfigsVerbose(const char *pattern)
3609 {
3610         PQExpBufferData buf;
3611         PGresult   *res;
3612         int                     i;
3613
3614         initPQExpBuffer(&buf);
3615
3616         printfPQExpBuffer(&buf,
3617                                           "SELECT c.oid, c.cfgname,\n"
3618                                           "   n.nspname, \n"
3619                                           "   p.prsname, \n"
3620                                           "   np.nspname as pnspname \n"
3621                                           "FROM pg_catalog.pg_ts_config c \n"
3622            "   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3623                                           " pg_catalog.pg_ts_parser p \n"
3624           "   LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3625                                           "WHERE  p.oid = c.cfgparser\n"
3626                 );
3627
3628         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3629                                                   "n.nspname", "c.cfgname", NULL,
3630                                                   "pg_catalog.pg_ts_config_is_visible(c.oid)");
3631
3632         appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
3633
3634         res = PSQLexec(buf.data, false);
3635         termPQExpBuffer(&buf);
3636         if (!res)
3637                 return false;
3638
3639         if (PQntuples(res) == 0)
3640         {
3641                 if (!pset.quiet)
3642                         fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
3643                                         pattern);
3644                 PQclear(res);
3645                 return false;
3646         }
3647
3648         for (i = 0; i < PQntuples(res); i++)
3649         {
3650                 const char *oid;
3651                 const char *cfgname;
3652                 const char *nspname = NULL;
3653                 const char *prsname;
3654                 const char *pnspname = NULL;
3655
3656                 oid = PQgetvalue(res, i, 0);
3657                 cfgname = PQgetvalue(res, i, 1);
3658                 if (!PQgetisnull(res, i, 2))
3659                         nspname = PQgetvalue(res, i, 2);
3660                 prsname = PQgetvalue(res, i, 3);
3661                 if (!PQgetisnull(res, i, 4))
3662                         pnspname = PQgetvalue(res, i, 4);
3663
3664                 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
3665                 {
3666                         PQclear(res);
3667                         return false;
3668                 }
3669
3670                 if (cancel_pressed)
3671                 {
3672                         PQclear(res);
3673                         return false;
3674                 }
3675         }
3676
3677         PQclear(res);
3678         return true;
3679 }
3680
3681 static bool
3682 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3683                                         const char *pnspname, const char *prsname)
3684 {
3685         PQExpBufferData buf,
3686                                 title;
3687         PGresult   *res;
3688         printQueryOpt myopt = pset.popt;
3689
3690         initPQExpBuffer(&buf);
3691
3692         printfPQExpBuffer(&buf,
3693                                           "SELECT \n"
3694                                           "  ( SELECT t.alias FROM \n"
3695                                           "    pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3696                                           "    WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3697                                           "  pg_catalog.btrim( \n"
3698                                   "    ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3699                                           "           FROM pg_catalog.pg_ts_config_map AS mm \n"
3700                                           "           WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3701                                           "           ORDER BY mapcfg, maptokentype, mapseqno \n"
3702                                           "    ) :: pg_catalog.text , \n"
3703                                           "  '{}') AS \"%s\" \n"
3704          "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3705                                           "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3706                                           "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3707                                           "ORDER BY 1;",
3708                                           gettext_noop("Token"),
3709                                           gettext_noop("Dictionaries"),
3710                                           oid);
3711
3712         res = PSQLexec(buf.data, false);
3713         termPQExpBuffer(&buf);
3714         if (!res)
3715                 return false;
3716
3717         initPQExpBuffer(&title);
3718
3719         if (nspname)
3720                 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3721                                                   nspname, cfgname);
3722         else
3723                 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3724                                                   cfgname);
3725
3726         if (pnspname)
3727                 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3728                                                   pnspname, prsname);
3729         else
3730                 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3731                                                   prsname);
3732
3733         myopt.nullPrint = NULL;
3734         myopt.title = title.data;
3735         myopt.footers = NULL;
3736         myopt.default_footer = false;
3737         myopt.translate_header = true;
3738
3739         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3740
3741         termPQExpBuffer(&title);
3742
3743         PQclear(res);
3744         return true;
3745 }
3746
3747
3748 /*
3749  * \dew
3750  *
3751  * Describes foreign-data wrappers
3752  */
3753 bool
3754 listForeignDataWrappers(const char *pattern, bool verbose)
3755 {
3756         PQExpBufferData buf;
3757         PGresult   *res;
3758         printQueryOpt myopt = pset.popt;
3759
3760         if (pset.sversion < 80400)
3761         {
3762                 fprintf(stderr, _("The server (version %d.%d) does not support foreign-data wrappers.\n"),
3763                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3764                 return true;
3765         }
3766
3767         initPQExpBuffer(&buf);
3768         printfPQExpBuffer(&buf,
3769                                           "SELECT fdw.fdwname AS \"%s\",\n"
3770                                           "  pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
3771                                           gettext_noop("Name"),
3772                                           gettext_noop("Owner"));
3773         if (pset.sversion >= 90100)
3774                 appendPQExpBuffer(&buf,
3775                                                   "  fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
3776                                                   gettext_noop("Handler"));
3777         appendPQExpBuffer(&buf,
3778                                           "  fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
3779                                           gettext_noop("Validator"));
3780
3781         if (verbose)
3782         {
3783                 appendPQExpBuffer(&buf, ",\n  ");
3784                 printACLColumn(&buf, "fdwacl");
3785                 appendPQExpBuffer(&buf,
3786                                                   ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
3787                                                   "  '(' || array_to_string(ARRAY(SELECT "
3788                                                   "  quote_ident(option_name) ||  ' ' || "
3789                                                   "  quote_literal(option_value)  FROM "
3790                                                   "  pg_options_to_table(fdwoptions)),  ', ') || ')' "
3791                                                   "  END AS \"%s\"",
3792                                                   gettext_noop("FDW Options"));
3793
3794                 if (pset.sversion >= 90100)
3795                         appendPQExpBuffer(&buf,
3796                                                           ",\n  d.description AS \"%s\" ",
3797                                                           gettext_noop("Description"));
3798         }
3799
3800         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
3801
3802         if (verbose && pset.sversion >= 90100)
3803                 appendPQExpBuffer(&buf,
3804                                                   "LEFT JOIN pg_catalog.pg_description d\n"
3805                                                   "       ON d.classoid = fdw.tableoid "
3806                                                   "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
3807
3808         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3809                                                   NULL, "fdwname", NULL, NULL);
3810
3811         appendPQExpBuffer(&buf, "ORDER BY 1;");
3812
3813         res = PSQLexec(buf.data, false);
3814         termPQExpBuffer(&buf);
3815         if (!res)
3816                 return false;
3817
3818         myopt.nullPrint = NULL;
3819         myopt.title = _("List of foreign-data wrappers");
3820         myopt.translate_header = true;
3821
3822         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3823
3824         PQclear(res);
3825         return true;
3826 }
3827
3828 /*
3829  * \des
3830  *
3831  * Describes foreign servers.
3832  */
3833 bool
3834 listForeignServers(const char *pattern, bool verbose)
3835 {
3836         PQExpBufferData buf;
3837         PGresult   *res;
3838         printQueryOpt myopt = pset.popt;
3839
3840         if (pset.sversion < 80400)
3841         {
3842                 fprintf(stderr, _("The server (version %d.%d) does not support foreign servers.\n"),
3843                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3844                 return true;
3845         }
3846
3847         initPQExpBuffer(&buf);
3848         printfPQExpBuffer(&buf,
3849                                           "SELECT s.srvname AS \"%s\",\n"
3850                                           "  pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3851                                           "  f.fdwname AS \"%s\"",
3852                                           gettext_noop("Name"),
3853                                           gettext_noop("Owner"),
3854                                           gettext_noop("Foreign-data wrapper"));
3855
3856         if (verbose)
3857         {
3858                 appendPQExpBuffer(&buf, ",\n  ");
3859                 printACLColumn(&buf, "s.srvacl");
3860                 appendPQExpBuffer(&buf,
3861                                                   ",\n"
3862                                                   "  s.srvtype AS \"%s\",\n"
3863                                                   "  s.srvversion AS \"%s\",\n"
3864                                                   "  CASE WHEN srvoptions IS NULL THEN '' ELSE "
3865                                                   "  '(' || array_to_string(ARRAY(SELECT "
3866                                                   "  quote_ident(option_name) ||  ' ' || "
3867                                                   "  quote_literal(option_value)  FROM "
3868                                                   "  pg_options_to_table(srvoptions)),  ', ') || ')' "
3869                                                   "  END AS \"%s\",\n"
3870                                                   "  d.description AS \"%s\"",
3871                                                   gettext_noop("Type"),
3872                                                   gettext_noop("Version"),
3873                                                   gettext_noop("FDW Options"),
3874                                                   gettext_noop("Description"));
3875         }
3876
3877         appendPQExpBuffer(&buf,
3878                                           "\nFROM pg_catalog.pg_foreign_server s\n"
3879            "     JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3880
3881         if (verbose)
3882                 appendPQExpBuffer(&buf,
3883                                                   "LEFT JOIN pg_description d\n       "
3884                                                   "ON d.classoid = s.tableoid AND d.objoid = s.oid "
3885                                                   "AND d.objsubid = 0\n");
3886
3887         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3888                                                   NULL, "s.srvname", NULL, NULL);
3889
3890         appendPQExpBuffer(&buf, "ORDER BY 1;");
3891
3892         res = PSQLexec(buf.data, false);
3893         termPQExpBuffer(&buf);
3894         if (!res)
3895                 return false;
3896
3897         myopt.nullPrint = NULL;
3898         myopt.title = _("List of foreign servers");
3899         myopt.translate_header = true;
3900
3901         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3902
3903         PQclear(res);
3904         return true;
3905 }
3906
3907 /*
3908  * \deu
3909  *
3910  * Describes user mappings.
3911  */
3912 bool
3913 listUserMappings(const char *pattern, bool verbose)
3914 {
3915         PQExpBufferData buf;
3916         PGresult   *res;
3917         printQueryOpt myopt = pset.popt;
3918
3919         if (pset.sversion < 80400)
3920         {
3921                 fprintf(stderr, _("The server (version %d.%d) does not support user mappings.\n"),
3922                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3923                 return true;
3924         }
3925
3926         initPQExpBuffer(&buf);
3927         printfPQExpBuffer(&buf,
3928                                           "SELECT um.srvname AS \"%s\",\n"
3929                                           "  um.usename AS \"%s\"",
3930                                           gettext_noop("Server"),
3931                                           gettext_noop("User name"));
3932
3933         if (verbose)
3934                 appendPQExpBuffer(&buf,
3935                                                   ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
3936                                                   "  '(' || array_to_string(ARRAY(SELECT "
3937                                                   "  quote_ident(option_name) ||  ' ' || "
3938                                                   "  quote_literal(option_value)  FROM "
3939                                                   "  pg_options_to_table(umoptions)),  ', ') || ')' "
3940                                                   "  END AS \"%s\"",
3941                                                   gettext_noop("FDW Options"));
3942
3943         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
3944
3945         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3946                                                   NULL, "um.srvname", "um.usename", NULL);
3947
3948         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3949
3950         res = PSQLexec(buf.data, false);
3951         termPQExpBuffer(&buf);
3952         if (!res)
3953                 return false;
3954
3955         myopt.nullPrint = NULL;
3956         myopt.title = _("List of user mappings");
3957         myopt.translate_header = true;
3958
3959         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3960
3961         PQclear(res);
3962         return true;
3963 }
3964
3965 /*
3966  * \det
3967  *
3968  * Describes foreign tables.
3969  */
3970 bool
3971 listForeignTables(const char *pattern, bool verbose)
3972 {
3973         PQExpBufferData buf;
3974         PGresult   *res;
3975         printQueryOpt myopt = pset.popt;
3976
3977         if (pset.sversion < 90100)
3978         {
3979                 fprintf(stderr, _("The server (version %d.%d) does not support foreign tables.\n"),
3980                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3981                 return true;
3982         }
3983
3984         initPQExpBuffer(&buf);
3985         printfPQExpBuffer(&buf,
3986                                           "SELECT n.nspname AS \"%s\",\n"
3987                                           "  c.relname AS \"%s\",\n"
3988                                           "  s.srvname AS \"%s\"",
3989                                           gettext_noop("Schema"),
3990                                           gettext_noop("Table"),
3991                                           gettext_noop("Server"));
3992
3993         if (verbose)
3994                 appendPQExpBuffer(&buf,
3995                                                   ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
3996                                                   "  '(' || array_to_string(ARRAY(SELECT "
3997                                                   "  quote_ident(option_name) ||  ' ' || "
3998                                                   "  quote_literal(option_value)  FROM "
3999                                                   "  pg_options_to_table(ftoptions)),  ', ') || ')' "
4000                                                   "  END AS \"%s\",\n"
4001                                                   "  d.description AS \"%s\"",
4002                                                   gettext_noop("FDW Options"),
4003                                                   gettext_noop("Description"));
4004
4005         appendPQExpBuffer(&buf,
4006                                           "\nFROM pg_catalog.pg_foreign_table ft\n"
4007                                           "  INNER JOIN pg_catalog.pg_class c"
4008                                           " ON c.oid = ft.ftrelid\n"
4009                                           "  INNER JOIN pg_catalog.pg_namespace n"
4010                                           " ON n.oid = c.relnamespace\n"
4011                                           "  INNER JOIN pg_catalog.pg_foreign_server s"
4012                                           " ON s.oid = ft.ftserver\n");
4013         if (verbose)
4014                 appendPQExpBuffer(&buf,
4015                                                   "   LEFT JOIN pg_catalog.pg_description d\n"
4016                                                   "          ON d.classoid = c.tableoid AND "
4017                                                   "d.objoid = c.oid AND d.objsubid = 0\n");
4018
4019         processSQLNamePattern(pset.db, &buf, pattern, false, false,
4020                                                   NULL, "n.nspname", "c.relname", NULL);
4021
4022         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4023
4024         res = PSQLexec(buf.data, false);
4025         termPQExpBuffer(&buf);
4026         if (!res)
4027                 return false;
4028
4029         myopt.nullPrint = NULL;
4030         myopt.title = _("List of foreign tables");
4031         myopt.translate_header = true;
4032
4033         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4034
4035         PQclear(res);
4036         return true;
4037 }
4038
4039 /*
4040  * \dx
4041  *
4042  * Briefly describes installed extensions.
4043  */
4044 bool
4045 listExtensions(const char *pattern)
4046 {
4047         PQExpBufferData buf;
4048         PGresult   *res;
4049         printQueryOpt myopt = pset.popt;
4050
4051         if (pset.sversion < 90100)
4052         {
4053                 fprintf(stderr, _("The server (version %d.%d) does not support extensions.\n"),
4054                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
4055                 return true;
4056         }
4057
4058         initPQExpBuffer(&buf);
4059         printfPQExpBuffer(&buf,
4060                                           "SELECT e.extname AS \"%s\", "
4061          "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4062                                           "FROM pg_catalog.pg_extension e "
4063                          "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4064                                  "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4065                  "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4066                                           gettext_noop("Name"),
4067                                           gettext_noop("Version"),
4068                                           gettext_noop("Schema"),
4069                                           gettext_noop("Description"));
4070
4071         processSQLNamePattern(pset.db, &buf, pattern,
4072                                                   false, false,
4073                                                   NULL, "e.extname", NULL,
4074                                                   NULL);
4075
4076         appendPQExpBuffer(&buf, "ORDER BY 1;");
4077
4078         res = PSQLexec(buf.data, false);
4079         termPQExpBuffer(&buf);
4080         if (!res)
4081                 return false;
4082
4083         myopt.nullPrint = NULL;
4084         myopt.title = _("List of installed extensions");
4085         myopt.translate_header = true;
4086
4087         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4088
4089         PQclear(res);
4090         return true;
4091 }
4092
4093 /*
4094  * \dx+
4095  *
4096  * List contents of installed extensions.
4097  */
4098 bool
4099 listExtensionContents(const char *pattern)
4100 {
4101         PQExpBufferData buf;
4102         PGresult   *res;
4103         int                     i;
4104
4105         if (pset.sversion < 90100)
4106         {
4107                 fprintf(stderr, _("The server (version %d.%d) does not support extensions.\n"),
4108                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
4109                 return true;
4110         }
4111
4112         initPQExpBuffer(&buf);
4113         printfPQExpBuffer(&buf,
4114                                           "SELECT e.extname, e.oid\n"
4115                                           "FROM pg_catalog.pg_extension e\n");
4116
4117         processSQLNamePattern(pset.db, &buf, pattern,
4118                                                   false, false,
4119                                                   NULL, "e.extname", NULL,
4120                                                   NULL);
4121
4122         appendPQExpBuffer(&buf, "ORDER BY 1;");
4123
4124         res = PSQLexec(buf.data, false);
4125         termPQExpBuffer(&buf);
4126         if (!res)
4127                 return false;
4128
4129         if (PQntuples(res) == 0)
4130         {
4131                 if (!pset.quiet)
4132                 {
4133                         if (pattern)
4134                                 fprintf(stderr, _("Did not find any extension named \"%s\".\n"),
4135                                                 pattern);
4136                         else
4137                                 fprintf(stderr, _("Did not find any extensions.\n"));
4138                 }
4139                 PQclear(res);
4140                 return false;
4141         }
4142
4143         for (i = 0; i < PQntuples(res); i++)
4144         {
4145                 const char *extname;
4146                 const char *oid;
4147
4148                 extname = PQgetvalue(res, i, 0);
4149                 oid = PQgetvalue(res, i, 1);
4150
4151                 if (!listOneExtensionContents(extname, oid))
4152                 {
4153                         PQclear(res);
4154                         return false;
4155                 }
4156                 if (cancel_pressed)
4157                 {
4158                         PQclear(res);
4159                         return false;
4160                 }
4161         }
4162
4163         PQclear(res);
4164         return true;
4165 }
4166
4167 static bool
4168 listOneExtensionContents(const char *extname, const char *oid)
4169 {
4170         PQExpBufferData buf;
4171         PGresult   *res;
4172         char            title[1024];
4173         printQueryOpt myopt = pset.popt;
4174
4175         initPQExpBuffer(&buf);
4176         printfPQExpBuffer(&buf,
4177                 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4178                                           "FROM pg_catalog.pg_depend\n"
4179                                           "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4180                                           "ORDER BY 1;",
4181                                           gettext_noop("Object Description"),
4182                                           oid);
4183
4184         res = PSQLexec(buf.data, false);
4185         termPQExpBuffer(&buf);
4186         if (!res)
4187                 return false;
4188
4189         myopt.nullPrint = NULL;
4190         snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4191         myopt.title = title;
4192         myopt.translate_header = true;
4193
4194         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4195
4196         PQclear(res);
4197         return true;
4198 }
4199
4200 /*
4201  * printACLColumn
4202  *
4203  * Helper function for consistently formatting ACL (privilege) columns.
4204  * The proper targetlist entry is appended to buf.      Note lack of any
4205  * whitespace or comma decoration.
4206  */
4207 static void
4208 printACLColumn(PQExpBuffer buf, const char *colname)
4209 {
4210         if (pset.sversion >= 80100)
4211                 appendPQExpBuffer(buf,
4212                                                   "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4213                                                   colname, gettext_noop("Access privileges"));
4214         else
4215                 appendPQExpBuffer(buf,
4216                                                   "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4217                                                   colname, gettext_noop("Access privileges"));
4218 }