]> granicus.if.org Git - postgresql/blob - src/bin/psql/describe.c
psql: Mark table headers in \drds output for translation
[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                 psql_error("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                 psql_error("\\df only takes [antwS+] as options\n");
223                 return true;
224         }
225
226         if (showWindow && pset.sversion < 80400)
227         {
228                 psql_error("\\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                 psql_error("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                         psql_error("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[9];
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         myopt.default_footer = false;
1134         /* This output looks confusing in expanded mode. */
1135         myopt.expanded = false;
1136
1137         initPQExpBuffer(&buf);
1138         initPQExpBuffer(&title);
1139         initPQExpBuffer(&tmpbuf);
1140
1141         /* Get general table info */
1142         if (pset.sversion >= 90100)
1143         {
1144                 printfPQExpBuffer(&buf,
1145                           "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1146                                                   "c.relhastriggers, c.relhasoids, "
1147                                                   "%s, c.reltablespace, "
1148                                                   "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1149                                                   "c.relpersistence\n"
1150                                                   "FROM pg_catalog.pg_class c\n "
1151                    "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1152                                                   "WHERE c.oid = '%s';",
1153                                                   (verbose ?
1154                                                    "pg_catalog.array_to_string(c.reloptions || "
1155                                                    "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1156                                                    : "''"),
1157                                                   oid);
1158         }
1159         else if (pset.sversion >= 90000)
1160         {
1161                 printfPQExpBuffer(&buf,
1162                           "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1163                                                   "c.relhastriggers, c.relhasoids, "
1164                                                   "%s, c.reltablespace, "
1165                                                   "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1166                                                   "FROM pg_catalog.pg_class c\n "
1167                    "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1168                                                   "WHERE c.oid = '%s';",
1169                                                   (verbose ?
1170                                                    "pg_catalog.array_to_string(c.reloptions || "
1171                                                    "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1172                                                    : "''"),
1173                                                   oid);
1174         }
1175         else if (pset.sversion >= 80400)
1176         {
1177                 printfPQExpBuffer(&buf,
1178                           "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1179                                                   "c.relhastriggers, c.relhasoids, "
1180                                                   "%s, c.reltablespace\n"
1181                                                   "FROM pg_catalog.pg_class c\n "
1182                    "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1183                                                   "WHERE c.oid = '%s';",
1184                                                   (verbose ?
1185                                                    "pg_catalog.array_to_string(c.reloptions || "
1186                                                    "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1187                                                    : "''"),
1188                                                   oid);
1189         }
1190         else if (pset.sversion >= 80200)
1191         {
1192                 printfPQExpBuffer(&buf,
1193                                           "SELECT relchecks, relkind, relhasindex, relhasrules, "
1194                                                   "reltriggers <> 0, relhasoids, "
1195                                                   "%s, reltablespace\n"
1196                                                   "FROM pg_catalog.pg_class WHERE oid = '%s';",
1197                                                   (verbose ?
1198                                          "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1199                                                   oid);
1200         }
1201         else if (pset.sversion >= 80000)
1202         {
1203                 printfPQExpBuffer(&buf,
1204                                           "SELECT relchecks, relkind, relhasindex, relhasrules, "
1205                                                   "reltriggers <> 0, relhasoids, "
1206                                                   "'', reltablespace\n"
1207                                                   "FROM pg_catalog.pg_class WHERE oid = '%s';",
1208                                                   oid);
1209         }
1210         else
1211         {
1212                 printfPQExpBuffer(&buf,
1213                                           "SELECT relchecks, relkind, relhasindex, relhasrules, "
1214                                                   "reltriggers <> 0, relhasoids, "
1215                                                   "'', ''\n"
1216                                                   "FROM pg_catalog.pg_class WHERE oid = '%s';",
1217                                                   oid);
1218         }
1219
1220         res = PSQLexec(buf.data, false);
1221         if (!res)
1222                 goto error_return;
1223
1224         /* Did we get anything? */
1225         if (PQntuples(res) == 0)
1226         {
1227                 if (!pset.quiet)
1228                         psql_error("Did not find any relation with OID %s.\n", 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)) : NULL;
1240         tableinfo.tablespace = (pset.sversion >= 80000) ?
1241                 atooid(PQgetvalue(res, 0, 7)) : 0;
1242         tableinfo.reloftype = (pset.sversion >= 90000 &&
1243                                                    strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
1244                 strdup(PQgetvalue(res, 0, 8)) : NULL;
1245         tableinfo.relpersistence = (pset.sversion >= 90100) ?
1246                 *(PQgetvalue(res, 0, 9)) : 0;
1247         PQclear(res);
1248         res = NULL;
1249
1250         /*
1251          * If it's a sequence, fetch its values and store into an array that will
1252          * be used later.
1253          */
1254         if (tableinfo.relkind == 'S')
1255         {
1256                 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1257                 /* must be separate because fmtId isn't reentrant */
1258                 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1259
1260                 res = PSQLexec(buf.data, false);
1261                 if (!res)
1262                         goto error_return;
1263
1264                 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1265
1266                 for (i = 0; i < PQnfields(res); i++)
1267                         seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1268                 seq_values[i] = NULL;
1269
1270                 PQclear(res);
1271                 res = NULL;
1272         }
1273
1274         /*
1275          * Get column info
1276          *
1277          * You need to modify value of "firstvcol" which will be defined below if
1278          * you are adding column(s) preceding to verbose-only columns.
1279          */
1280         printfPQExpBuffer(&buf, "SELECT a.attname,");
1281         appendPQExpBuffer(&buf, "\n  pg_catalog.format_type(a.atttypid, a.atttypmod),"
1282                                           "\n  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1283                                           "\n   FROM pg_catalog.pg_attrdef d"
1284                                           "\n   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1285                                           "\n  a.attnotnull, a.attnum,");
1286         if (pset.sversion >= 90100)
1287                 appendPQExpBuffer(&buf, "\n  (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1288                                                   "   WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1289         else
1290                 appendPQExpBuffer(&buf, "\n  NULL AS attcollation");
1291         if (tableinfo.relkind == 'i')
1292                 appendPQExpBuffer(&buf, ",\n  pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1293         else
1294                 appendPQExpBuffer(&buf, ",\n  NULL AS indexdef");
1295         if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1296                 appendPQExpBuffer(&buf, ",\n  CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1297                                                   "  '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) ||  ' ' || quote_literal(option_value)  FROM "
1298                                                   "  pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1299         else
1300                 appendPQExpBuffer(&buf, ",\n  NULL AS attfdwoptions");
1301         if (verbose)
1302         {
1303                 appendPQExpBuffer(&buf, ",\n  a.attstorage");
1304                 appendPQExpBuffer(&buf, ",\n  CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1305
1306                 /*
1307                  * In 9.0+, we have column comments for: relations, views, composite
1308                  * types, and foreign tables (c.f. CommentObject() in comment.c).
1309                  */
1310                 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1311                         tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1312                         appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1313         }
1314
1315         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
1316         appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1317         appendPQExpBuffer(&buf, "\nORDER BY a.attnum;");
1318
1319         res = PSQLexec(buf.data, false);
1320         if (!res)
1321                 goto error_return;
1322         numrows = PQntuples(res);
1323
1324         /* Make title */
1325         switch (tableinfo.relkind)
1326         {
1327                 case 'r':
1328                         if (tableinfo.relpersistence == 'u')
1329                                 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1330                                                                   schemaname, relationname);
1331                         else
1332                                 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1333                                                                   schemaname, relationname);
1334                         break;
1335                 case 'v':
1336                         printfPQExpBuffer(&title, _("View \"%s.%s\""),
1337                                                           schemaname, relationname);
1338                         break;
1339                 case 'S':
1340                         printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1341                                                           schemaname, relationname);
1342                         break;
1343                 case 'i':
1344                         if (tableinfo.relpersistence == 'u')
1345                                 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1346                                                                   schemaname, relationname);
1347                         else
1348                                 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1349                                                                   schemaname, relationname);
1350                         break;
1351                 case 's':
1352                         /* not used as of 8.2, but keep it for backwards compatibility */
1353                         printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1354                                                           schemaname, relationname);
1355                         break;
1356                 case 't':
1357                         printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1358                                                           schemaname, relationname);
1359                         break;
1360                 case 'c':
1361                         printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1362                                                           schemaname, relationname);
1363                         break;
1364                 case 'f':
1365                         printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1366                                                           schemaname, relationname);
1367                         break;
1368                 default:
1369                         /* untranslated unknown relkind */
1370                         printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1371                                                           tableinfo.relkind, schemaname, relationname);
1372                         break;
1373         }
1374
1375         /* Set the number of columns, and their names */
1376         headers[0] = gettext_noop("Column");
1377         headers[1] = gettext_noop("Type");
1378         cols = 2;
1379
1380         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1381                 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1382         {
1383                 show_modifiers = true;
1384                 headers[cols++] = gettext_noop("Modifiers");
1385                 modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
1386         }
1387
1388         if (tableinfo.relkind == 'S')
1389                 headers[cols++] = gettext_noop("Value");
1390
1391         if (tableinfo.relkind == 'i')
1392                 headers[cols++] = gettext_noop("Definition");
1393
1394         if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1395                 headers[cols++] = gettext_noop("FDW Options");
1396
1397         if (verbose)
1398         {
1399                 headers[cols++] = gettext_noop("Storage");
1400                 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1401                         headers[cols++] = gettext_noop("Stats target");
1402                 /* Column comments, if the relkind supports this feature. */
1403                 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1404                         tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1405                         headers[cols++] = gettext_noop("Description");
1406         }
1407
1408         printTableInit(&cont, &myopt, title.data, cols, numrows);
1409         printTableInitialized = true;
1410
1411         for (i = 0; i < cols; i++)
1412                 printTableAddHeader(&cont, headers[i], true, 'l');
1413
1414         /* Check if table is a view */
1415         if (tableinfo.relkind == 'v' && verbose)
1416         {
1417                 PGresult   *result;
1418
1419                 printfPQExpBuffer(&buf,
1420                          "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1421                                                   oid);
1422                 result = PSQLexec(buf.data, false);
1423                 if (!result)
1424                         goto error_return;
1425
1426                 if (PQntuples(result) > 0)
1427                         view_def = pg_strdup(PQgetvalue(result, 0, 0));
1428
1429                 PQclear(result);
1430         }
1431
1432         /* Generate table cells to be printed */
1433         for (i = 0; i < numrows; i++)
1434         {
1435                 /* Column */
1436                 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1437
1438                 /* Type */
1439                 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1440
1441                 /* Modifiers: collate, not null, default */
1442                 if (show_modifiers)
1443                 {
1444                         resetPQExpBuffer(&tmpbuf);
1445
1446                         if (!PQgetisnull(res, i, 5))
1447                         {
1448                                 if (tmpbuf.len > 0)
1449                                         appendPQExpBufferStr(&tmpbuf, " ");
1450                                 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1451                                                                   PQgetvalue(res, i, 5));
1452                         }
1453
1454                         if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1455                         {
1456                                 if (tmpbuf.len > 0)
1457                                         appendPQExpBufferStr(&tmpbuf, " ");
1458                                 appendPQExpBufferStr(&tmpbuf, _("not null"));
1459                         }
1460
1461                         /* handle "default" here */
1462                         /* (note: above we cut off the 'default' string at 128) */
1463                         if (strlen(PQgetvalue(res, i, 2)) != 0)
1464                         {
1465                                 if (tmpbuf.len > 0)
1466                                         appendPQExpBufferStr(&tmpbuf, " ");
1467                                 /* translator: default values of column definitions */
1468                                 appendPQExpBuffer(&tmpbuf, _("default %s"),
1469                                                                   PQgetvalue(res, i, 2));
1470                         }
1471
1472                         modifiers[i] = pg_strdup(tmpbuf.data);
1473                         printTableAddCell(&cont, modifiers[i], false, false);
1474                 }
1475
1476                 /* Value: for sequences only */
1477                 if (tableinfo.relkind == 'S')
1478                         printTableAddCell(&cont, seq_values[i], false, false);
1479
1480                 /* Expression for index column */
1481                 if (tableinfo.relkind == 'i')
1482                         printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1483
1484                 /* FDW options for foreign table column, only for 9.2 or later */
1485                 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1486                         printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1487
1488                 /* Storage and Description */
1489                 if (verbose)
1490                 {
1491                         int                     firstvcol = 8;
1492                         char       *storage = PQgetvalue(res, i, firstvcol);
1493
1494                         /* these strings are literal in our syntax, so not translated. */
1495                         printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1496                                                                           (storage[0] == 'm' ? "main" :
1497                                                                            (storage[0] == 'x' ? "extended" :
1498                                                                                 (storage[0] == 'e' ? "external" :
1499                                                                                  "???")))),
1500                                                           false, false);
1501
1502                         /* Statistics target, if the relkind supports this feature */
1503                         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1504                         {
1505                                 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1506                                                                   false, false);
1507                         }
1508
1509                         /* Column comments, if the relkind supports this feature. */
1510                         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1511                                 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1512                                 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1513                                                                   false, false);
1514                 }
1515         }
1516
1517         /* Make footers */
1518         if (tableinfo.relkind == 'i')
1519         {
1520                 /* Footer information about an index */
1521                 PGresult   *result;
1522
1523                 printfPQExpBuffer(&buf,
1524                                  "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1525                 if (pset.sversion >= 80200)
1526                         appendPQExpBuffer(&buf, "i.indisvalid,\n");
1527                 else
1528                         appendPQExpBuffer(&buf, "true AS indisvalid,\n");
1529                 if (pset.sversion >= 90000)
1530                         appendPQExpBuffer(&buf,
1531                                                           "  (NOT i.indimmediate) AND "
1532                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1533                                                           "WHERE conrelid = i.indrelid AND "
1534                                                           "conindid = i.indexrelid AND "
1535                                                           "contype IN ('p','u','x') AND "
1536                                                           "condeferrable) AS condeferrable,\n"
1537                                                           "  (NOT i.indimmediate) AND "
1538                                                         "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1539                                                           "WHERE conrelid = i.indrelid AND "
1540                                                           "conindid = i.indexrelid AND "
1541                                                           "contype IN ('p','u','x') AND "
1542                                                           "condeferred) AS condeferred,\n");
1543                 else
1544                         appendPQExpBuffer(&buf,
1545                                                 "  false AS condeferrable, false AS condeferred,\n");
1546                 appendPQExpBuffer(&buf, "  a.amname, c2.relname, "
1547                                           "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1548                                                   "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1549                   "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1550                                                   "AND i.indrelid = c2.oid;",
1551                                                   oid);
1552
1553                 result = PSQLexec(buf.data, false);
1554                 if (!result)
1555                         goto error_return;
1556                 else if (PQntuples(result) != 1)
1557                 {
1558                         PQclear(result);
1559                         goto error_return;
1560                 }
1561                 else
1562                 {
1563                         char       *indisunique = PQgetvalue(result, 0, 0);
1564                         char       *indisprimary = PQgetvalue(result, 0, 1);
1565                         char       *indisclustered = PQgetvalue(result, 0, 2);
1566                         char       *indisvalid = PQgetvalue(result, 0, 3);
1567                         char       *deferrable = PQgetvalue(result, 0, 4);
1568                         char       *deferred = PQgetvalue(result, 0, 5);
1569                         char       *indamname = PQgetvalue(result, 0, 6);
1570                         char       *indtable = PQgetvalue(result, 0, 7);
1571                         char       *indpred = PQgetvalue(result, 0, 8);
1572
1573                         if (strcmp(indisprimary, "t") == 0)
1574                                 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1575                         else if (strcmp(indisunique, "t") == 0)
1576                                 printfPQExpBuffer(&tmpbuf, _("unique, "));
1577                         else
1578                                 resetPQExpBuffer(&tmpbuf);
1579                         appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1580
1581                         /* we assume here that index and table are in same schema */
1582                         appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1583                                                           schemaname, indtable);
1584
1585                         if (strlen(indpred))
1586                                 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1587
1588                         if (strcmp(indisclustered, "t") == 0)
1589                                 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1590
1591                         if (strcmp(indisvalid, "t") != 0)
1592                                 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1593
1594                         if (strcmp(deferrable, "t") == 0)
1595                                 appendPQExpBuffer(&tmpbuf, _(", deferrable"));
1596
1597                         if (strcmp(deferred, "t") == 0)
1598                                 appendPQExpBuffer(&tmpbuf, _(", initially deferred"));
1599
1600                         printTableAddFooter(&cont, tmpbuf.data);
1601                         add_tablespace_footer(&cont, tableinfo.relkind,
1602                                                                   tableinfo.tablespace, true);
1603                 }
1604
1605                 PQclear(result);
1606         }
1607         else if (view_def)
1608         {
1609                 PGresult   *result = NULL;
1610
1611                 /* Footer information about a view */
1612                 printTableAddFooter(&cont, _("View definition:"));
1613                 printTableAddFooter(&cont, view_def);
1614
1615                 /* print rules */
1616                 if (tableinfo.hasrules)
1617                 {
1618                         printfPQExpBuffer(&buf,
1619                                                           "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1620                                                           "FROM pg_catalog.pg_rewrite r\n"
1621                         "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
1622                                                           oid);
1623                         result = PSQLexec(buf.data, false);
1624                         if (!result)
1625                                 goto error_return;
1626
1627                         if (PQntuples(result) > 0)
1628                         {
1629                                 printTableAddFooter(&cont, _("Rules:"));
1630                                 for (i = 0; i < PQntuples(result); i++)
1631                                 {
1632                                         const char *ruledef;
1633
1634                                         /* Everything after "CREATE RULE" is echoed verbatim */
1635                                         ruledef = PQgetvalue(result, i, 1);
1636                                         ruledef += 12;
1637
1638                                         printfPQExpBuffer(&buf, " %s", ruledef);
1639                                         printTableAddFooter(&cont, buf.data);
1640                                 }
1641                         }
1642                         PQclear(result);
1643                 }
1644         }
1645         else if (tableinfo.relkind == 'S')
1646         {
1647                 /* Footer information about a sequence */
1648                 PGresult   *result = NULL;
1649
1650                 /* Get the column that owns this sequence */
1651                 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1652                                                   "\n   pg_catalog.quote_ident(relname) || '.' ||"
1653                                                   "\n   pg_catalog.quote_ident(attname)"
1654                                                   "\nFROM pg_catalog.pg_class c"
1655                                         "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1656                          "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1657                                                   "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1658                                                   "\n a.attrelid=c.oid AND"
1659                                                   "\n a.attnum=d.refobjsubid)"
1660                            "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1661                          "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1662                                                   "\n AND d.objid=%s"
1663                                                   "\n AND d.deptype='a'",
1664                                                   oid);
1665
1666                 result = PSQLexec(buf.data, false);
1667                 if (!result)
1668                         goto error_return;
1669                 else if (PQntuples(result) == 1)
1670                 {
1671                         printfPQExpBuffer(&buf, _("Owned by: %s"),
1672                                                           PQgetvalue(result, 0, 0));
1673                         printTableAddFooter(&cont, buf.data);
1674                 }
1675
1676                 /*
1677                  * If we get no rows back, don't show anything (obviously). We should
1678                  * never get more than one row back, but if we do, just ignore it and
1679                  * don't print anything.
1680                  */
1681                 PQclear(result);
1682         }
1683         else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1684         {
1685                 /* Footer information about a table */
1686                 PGresult   *result = NULL;
1687                 int                     tuples = 0;
1688
1689                 /* print indexes */
1690                 if (tableinfo.hasindex)
1691                 {
1692                         printfPQExpBuffer(&buf,
1693                                                           "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1694                         if (pset.sversion >= 80200)
1695                                 appendPQExpBuffer(&buf, "i.indisvalid, ");
1696                         else
1697                                 appendPQExpBuffer(&buf, "true as indisvalid, ");
1698                         appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n  ");
1699                         if (pset.sversion >= 90000)
1700                                 appendPQExpBuffer(&buf,
1701                                                    "pg_catalog.pg_get_constraintdef(con.oid, true), "
1702                                                                   "contype, condeferrable, condeferred");
1703                         else
1704                                 appendPQExpBuffer(&buf,
1705                                                                   "null AS constraintdef, null AS contype, "
1706                                                          "false AS condeferrable, false AS condeferred");
1707                         if (pset.sversion >= 80000)
1708                                 appendPQExpBuffer(&buf, ", c2.reltablespace");
1709                         appendPQExpBuffer(&buf,
1710                                                           "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1711                         if (pset.sversion >= 90000)
1712                                 appendPQExpBuffer(&buf,
1713                                                                   "  LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1714                         appendPQExpBuffer(&buf,
1715                                                           "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1716                          "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
1717                                                           oid);
1718                         result = PSQLexec(buf.data, false);
1719                         if (!result)
1720                                 goto error_return;
1721                         else
1722                                 tuples = PQntuples(result);
1723
1724                         if (tuples > 0)
1725                         {
1726                                 printTableAddFooter(&cont, _("Indexes:"));
1727                                 for (i = 0; i < tuples; i++)
1728                                 {
1729                                         /* untranslated index name */
1730                                         printfPQExpBuffer(&buf, "    \"%s\"",
1731                                                                           PQgetvalue(result, i, 0));
1732
1733                                         /* If exclusion constraint, print the constraintdef */
1734                                         if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1735                                         {
1736                                                 appendPQExpBuffer(&buf, " %s",
1737                                                                                   PQgetvalue(result, i, 6));
1738                                         }
1739                                         else
1740                                         {
1741                                                 const char *indexdef;
1742                                                 const char *usingpos;
1743
1744                                                 /* Label as primary key or unique (but not both) */
1745                                                 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1746                                                         appendPQExpBuffer(&buf, " PRIMARY KEY,");
1747                                                 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1748                                                 {
1749                                                         if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1750                                                                 appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
1751                                                         else
1752                                                                 appendPQExpBuffer(&buf, " UNIQUE,");
1753                                                 }
1754
1755                                                 /* Everything after "USING" is echoed verbatim */
1756                                                 indexdef = PQgetvalue(result, i, 5);
1757                                                 usingpos = strstr(indexdef, " USING ");
1758                                                 if (usingpos)
1759                                                         indexdef = usingpos + 7;
1760                                                 appendPQExpBuffer(&buf, " %s", indexdef);
1761
1762                                                 /* Need these for deferrable PK/UNIQUE indexes */
1763                                                 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1764                                                         appendPQExpBuffer(&buf, " DEFERRABLE");
1765
1766                                                 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1767                                                         appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
1768                                         }
1769
1770                                         /* Add these for all cases */
1771                                         if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1772                                                 appendPQExpBuffer(&buf, " CLUSTER");
1773
1774                                         if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1775                                                 appendPQExpBuffer(&buf, " INVALID");
1776
1777                                         printTableAddFooter(&cont, buf.data);
1778
1779                                         /* Print tablespace of the index on the same line */
1780                                         if (pset.sversion >= 80000)
1781                                                 add_tablespace_footer(&cont, 'i',
1782                                                                                    atooid(PQgetvalue(result, i, 10)),
1783                                                                                           false);
1784                                 }
1785                         }
1786                         PQclear(result);
1787                 }
1788
1789                 /* print table (and column) check constraints */
1790                 if (tableinfo.checks)
1791                 {
1792                         printfPQExpBuffer(&buf,
1793                                                           "SELECT r.conname, "
1794                                                           "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1795                                                           "FROM pg_catalog.pg_constraint r\n"
1796                                                           "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
1797                                                           "ORDER BY 1;",
1798                                                           oid);
1799                         result = PSQLexec(buf.data, false);
1800                         if (!result)
1801                                 goto error_return;
1802                         else
1803                                 tuples = PQntuples(result);
1804
1805                         if (tuples > 0)
1806                         {
1807                                 printTableAddFooter(&cont, _("Check constraints:"));
1808                                 for (i = 0; i < tuples; i++)
1809                                 {
1810                                         /* untranslated contraint name and def */
1811                                         printfPQExpBuffer(&buf, "    \"%s\" %s",
1812                                                                           PQgetvalue(result, i, 0),
1813                                                                           PQgetvalue(result, i, 1));
1814
1815                                         printTableAddFooter(&cont, buf.data);
1816                                 }
1817                         }
1818                         PQclear(result);
1819                 }
1820
1821                 /* print foreign-key constraints (there are none if no triggers) */
1822                 if (tableinfo.hastriggers)
1823                 {
1824                         printfPQExpBuffer(&buf,
1825                                                           "SELECT conname,\n"
1826                                  "  pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1827                                                           "FROM pg_catalog.pg_constraint r\n"
1828                                    "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
1829                                                           oid);
1830                         result = PSQLexec(buf.data, false);
1831                         if (!result)
1832                                 goto error_return;
1833                         else
1834                                 tuples = PQntuples(result);
1835
1836                         if (tuples > 0)
1837                         {
1838                                 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1839                                 for (i = 0; i < tuples; i++)
1840                                 {
1841                                         /* untranslated constraint name and def */
1842                                         printfPQExpBuffer(&buf, "    \"%s\" %s",
1843                                                                           PQgetvalue(result, i, 0),
1844                                                                           PQgetvalue(result, i, 1));
1845
1846                                         printTableAddFooter(&cont, buf.data);
1847                                 }
1848                         }
1849                         PQclear(result);
1850                 }
1851
1852                 /* print incoming foreign-key references (none if no triggers) */
1853                 if (tableinfo.hastriggers)
1854                 {
1855                         printfPQExpBuffer(&buf,
1856                                                    "SELECT conname, conrelid::pg_catalog.regclass,\n"
1857                                  "  pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1858                                                           "FROM pg_catalog.pg_constraint c\n"
1859                                   "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
1860                                                           oid);
1861                         result = PSQLexec(buf.data, false);
1862                         if (!result)
1863                                 goto error_return;
1864                         else
1865                                 tuples = PQntuples(result);
1866
1867                         if (tuples > 0)
1868                         {
1869                                 printTableAddFooter(&cont, _("Referenced by:"));
1870                                 for (i = 0; i < tuples; i++)
1871                                 {
1872                                         printfPQExpBuffer(&buf, "    TABLE \"%s\" CONSTRAINT \"%s\" %s",
1873                                                                           PQgetvalue(result, i, 1),
1874                                                                           PQgetvalue(result, i, 0),
1875                                                                           PQgetvalue(result, i, 2));
1876
1877                                         printTableAddFooter(&cont, buf.data);
1878                                 }
1879                         }
1880                         PQclear(result);
1881                 }
1882
1883                 /* print rules */
1884                 if (tableinfo.hasrules)
1885                 {
1886                         if (pset.sversion >= 80300)
1887                         {
1888                                 printfPQExpBuffer(&buf,
1889                                                                   "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1890                                                                   "ev_enabled\n"
1891                                                                   "FROM pg_catalog.pg_rewrite r\n"
1892                                                                   "WHERE r.ev_class = '%s' ORDER BY 1;",
1893                                                                   oid);
1894                         }
1895                         else
1896                         {
1897                                 printfPQExpBuffer(&buf,
1898                                                                   "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1899                                                                   "'O'::char AS ev_enabled\n"
1900                                                                   "FROM pg_catalog.pg_rewrite r\n"
1901                                                                   "WHERE r.ev_class = '%s' ORDER BY 1;",
1902                                                                   oid);
1903                         }
1904                         result = PSQLexec(buf.data, false);
1905                         if (!result)
1906                                 goto error_return;
1907                         else
1908                                 tuples = PQntuples(result);
1909
1910                         if (tuples > 0)
1911                         {
1912                                 bool            have_heading;
1913                                 int                     category;
1914
1915                                 for (category = 0; category < 4; category++)
1916                                 {
1917                                         have_heading = false;
1918
1919                                         for (i = 0; i < tuples; i++)
1920                                         {
1921                                                 const char *ruledef;
1922                                                 bool            list_rule = false;
1923
1924                                                 switch (category)
1925                                                 {
1926                                                         case 0:
1927                                                                 if (*PQgetvalue(result, i, 2) == 'O')
1928                                                                         list_rule = true;
1929                                                                 break;
1930                                                         case 1:
1931                                                                 if (*PQgetvalue(result, i, 2) == 'D')
1932                                                                         list_rule = true;
1933                                                                 break;
1934                                                         case 2:
1935                                                                 if (*PQgetvalue(result, i, 2) == 'A')
1936                                                                         list_rule = true;
1937                                                                 break;
1938                                                         case 3:
1939                                                                 if (*PQgetvalue(result, i, 2) == 'R')
1940                                                                         list_rule = true;
1941                                                                 break;
1942                                                 }
1943                                                 if (!list_rule)
1944                                                         continue;
1945
1946                                                 if (!have_heading)
1947                                                 {
1948                                                         switch (category)
1949                                                         {
1950                                                                 case 0:
1951                                                                         printfPQExpBuffer(&buf, _("Rules:"));
1952                                                                         break;
1953                                                                 case 1:
1954                                                                         printfPQExpBuffer(&buf, _("Disabled rules:"));
1955                                                                         break;
1956                                                                 case 2:
1957                                                                         printfPQExpBuffer(&buf, _("Rules firing always:"));
1958                                                                         break;
1959                                                                 case 3:
1960                                                                         printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1961                                                                         break;
1962                                                         }
1963                                                         printTableAddFooter(&cont, buf.data);
1964                                                         have_heading = true;
1965                                                 }
1966
1967                                                 /* Everything after "CREATE RULE" is echoed verbatim */
1968                                                 ruledef = PQgetvalue(result, i, 1);
1969                                                 ruledef += 12;
1970                                                 printfPQExpBuffer(&buf, "    %s", ruledef);
1971                                                 printTableAddFooter(&cont, buf.data);
1972                                         }
1973                                 }
1974                         }
1975                         PQclear(result);
1976                 }
1977         }
1978
1979         /*
1980          * Print triggers next, if any (but only user-defined triggers).  This
1981          * could apply to either a table or a view.
1982          */
1983         if (tableinfo.hastriggers)
1984         {
1985                 PGresult   *result;
1986                 int                     tuples;
1987
1988                 printfPQExpBuffer(&buf,
1989                                                   "SELECT t.tgname, "
1990                                                   "pg_catalog.pg_get_triggerdef(t.oid%s), "
1991                                                   "t.tgenabled\n"
1992                                                   "FROM pg_catalog.pg_trigger t\n"
1993                                                   "WHERE t.tgrelid = '%s' AND ",
1994                                                   (pset.sversion >= 90000 ? ", true" : ""),
1995                                                   oid);
1996                 if (pset.sversion >= 90000)
1997                         appendPQExpBuffer(&buf, "NOT t.tgisinternal");
1998                 else if (pset.sversion >= 80300)
1999                         appendPQExpBuffer(&buf, "t.tgconstraint = 0");
2000                 else
2001                         appendPQExpBuffer(&buf,
2002                                                           "(NOT tgisconstraint "
2003                                                           " OR NOT EXISTS"
2004                                                           "  (SELECT 1 FROM pg_catalog.pg_depend d "
2005                                                           "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2006                                                           "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2007                 appendPQExpBuffer(&buf, "\nORDER BY 1;");
2008
2009                 result = PSQLexec(buf.data, false);
2010                 if (!result)
2011                         goto error_return;
2012                 else
2013                         tuples = PQntuples(result);
2014
2015                 if (tuples > 0)
2016                 {
2017                         bool            have_heading;
2018                         int                     category;
2019
2020                         /*
2021                          * split the output into 4 different categories. Enabled triggers,
2022                          * disabled triggers and the two special ALWAYS and REPLICA
2023                          * configurations.
2024                          */
2025                         for (category = 0; category < 4; category++)
2026                         {
2027                                 have_heading = false;
2028                                 for (i = 0; i < tuples; i++)
2029                                 {
2030                                         bool            list_trigger;
2031                                         const char *tgdef;
2032                                         const char *usingpos;
2033                                         const char *tgenabled;
2034
2035                                         /*
2036                                          * Check if this trigger falls into the current category
2037                                          */
2038                                         tgenabled = PQgetvalue(result, i, 2);
2039                                         list_trigger = false;
2040                                         switch (category)
2041                                         {
2042                                                 case 0:
2043                                                         if (*tgenabled == 'O' || *tgenabled == 't')
2044                                                                 list_trigger = true;
2045                                                         break;
2046                                                 case 1:
2047                                                         if (*tgenabled == 'D' || *tgenabled == 'f')
2048                                                                 list_trigger = true;
2049                                                         break;
2050                                                 case 2:
2051                                                         if (*tgenabled == 'A')
2052                                                                 list_trigger = true;
2053                                                         break;
2054                                                 case 3:
2055                                                         if (*tgenabled == 'R')
2056                                                                 list_trigger = true;
2057                                                         break;
2058                                         }
2059                                         if (list_trigger == false)
2060                                                 continue;
2061
2062                                         /* Print the category heading once */
2063                                         if (have_heading == false)
2064                                         {
2065                                                 switch (category)
2066                                                 {
2067                                                         case 0:
2068                                                                 printfPQExpBuffer(&buf, _("Triggers:"));
2069                                                                 break;
2070                                                         case 1:
2071                                                                 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2072                                                                 break;
2073                                                         case 2:
2074                                                                 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2075                                                                 break;
2076                                                         case 3:
2077                                                                 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2078                                                                 break;
2079
2080                                                 }
2081                                                 printTableAddFooter(&cont, buf.data);
2082                                                 have_heading = true;
2083                                         }
2084
2085                                         /* Everything after "TRIGGER" is echoed verbatim */
2086                                         tgdef = PQgetvalue(result, i, 1);
2087                                         usingpos = strstr(tgdef, " TRIGGER ");
2088                                         if (usingpos)
2089                                                 tgdef = usingpos + 9;
2090
2091                                         printfPQExpBuffer(&buf, "    %s", tgdef);
2092                                         printTableAddFooter(&cont, buf.data);
2093                                 }
2094                         }
2095                 }
2096                 PQclear(result);
2097         }
2098
2099         /*
2100          * Finish printing the footer information about a table.
2101          */
2102         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
2103         {
2104                 PGresult   *result;
2105                 int                     tuples;
2106
2107                 /* print foreign server name */
2108                 if (tableinfo.relkind == 'f')
2109                 {
2110                         char       *ftoptions;
2111
2112                         /* Footer information about foreign table */
2113                         printfPQExpBuffer(&buf,
2114                                                           "SELECT s.srvname,\n"
2115                                                           "       array_to_string(ARRAY(SELECT "
2116                                                           "       quote_ident(option_name) ||  ' ' || "
2117                                                           "       quote_literal(option_value)  FROM "
2118                                                         "       pg_options_to_table(ftoptions)),  ', ') "
2119                                                           "FROM pg_catalog.pg_foreign_table f,\n"
2120                                                           "     pg_catalog.pg_foreign_server s\n"
2121                                                           "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2122                                                           oid);
2123                         result = PSQLexec(buf.data, false);
2124                         if (!result)
2125                                 goto error_return;
2126                         else if (PQntuples(result) != 1)
2127                         {
2128                                 PQclear(result);
2129                                 goto error_return;
2130                         }
2131
2132                         /* Print server name */
2133                         printfPQExpBuffer(&buf, "Server: %s",
2134                                                           PQgetvalue(result, 0, 0));
2135                         printTableAddFooter(&cont, buf.data);
2136
2137                         /* Print per-table FDW options, if any */
2138                         ftoptions = PQgetvalue(result, 0, 1);
2139                         if (ftoptions && ftoptions[0] != '\0')
2140                         {
2141                                 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2142                                 printTableAddFooter(&cont, buf.data);
2143                         }
2144                         PQclear(result);
2145                 }
2146
2147                 /* print inherited tables */
2148                 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);
2149
2150                 result = PSQLexec(buf.data, false);
2151                 if (!result)
2152                         goto error_return;
2153                 else
2154                 {
2155                         const char *s = _("Inherits");
2156                         int                     sw = pg_wcswidth(s, strlen(s), pset.encoding);
2157
2158                         tuples = PQntuples(result);
2159
2160                         for (i = 0; i < tuples; i++)
2161                         {
2162                                 if (i == 0)
2163                                         printfPQExpBuffer(&buf, "%s: %s",
2164                                                                           s, PQgetvalue(result, i, 0));
2165                                 else
2166                                         printfPQExpBuffer(&buf, "%*s  %s",
2167                                                                           sw, "", PQgetvalue(result, i, 0));
2168                                 if (i < tuples - 1)
2169                                         appendPQExpBuffer(&buf, ",");
2170
2171                                 printTableAddFooter(&cont, buf.data);
2172                         }
2173
2174                         PQclear(result);
2175                 }
2176
2177                 /* print child tables */
2178                 if (pset.sversion >= 80300)
2179                         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);
2180                 else
2181                         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);
2182
2183                 result = PSQLexec(buf.data, false);
2184                 if (!result)
2185                         goto error_return;
2186                 else
2187                         tuples = PQntuples(result);
2188
2189                 if (!verbose)
2190                 {
2191                         /* print the number of child tables, if any */
2192                         if (tuples > 0)
2193                         {
2194                                 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2195                                 printTableAddFooter(&cont, buf.data);
2196                         }
2197                 }
2198                 else
2199                 {
2200                         /* display the list of child tables */
2201                         const char *ct = _("Child tables");
2202                         int                     ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2203
2204                         for (i = 0; i < tuples; i++)
2205                         {
2206                                 if (i == 0)
2207                                         printfPQExpBuffer(&buf, "%s: %s",
2208                                                                           ct, PQgetvalue(result, i, 0));
2209                                 else
2210                                         printfPQExpBuffer(&buf, "%*s  %s",
2211                                                                           ctw, "", PQgetvalue(result, i, 0));
2212                                 if (i < tuples - 1)
2213                                         appendPQExpBuffer(&buf, ",");
2214
2215                                 printTableAddFooter(&cont, buf.data);
2216                         }
2217                 }
2218                 PQclear(result);
2219
2220                 /* Table type */
2221                 if (tableinfo.reloftype)
2222                 {
2223                         printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2224                         printTableAddFooter(&cont, buf.data);
2225                 }
2226
2227                 /* OIDs, if verbose */
2228                 if (verbose)
2229                 {
2230                         const char *s = _("Has OIDs");
2231
2232                         printfPQExpBuffer(&buf, "%s: %s", s,
2233                                                           (tableinfo.hasoids ? _("yes") : _("no")));
2234                         printTableAddFooter(&cont, buf.data);
2235                 }
2236
2237                 /* Tablespace info */
2238                 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2239                                                           true);
2240         }
2241
2242         /* reloptions, if verbose */
2243         if (verbose &&
2244                 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2245         {
2246                 const char *t = _("Options");
2247
2248                 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
2249                 printTableAddFooter(&cont, buf.data);
2250         }
2251
2252         printTable(&cont, pset.queryFout, pset.logfile);
2253         printTableCleanup(&cont);
2254
2255         retval = true;
2256
2257 error_return:
2258
2259         /* clean up */
2260         if (printTableInitialized)
2261                 printTableCleanup(&cont);
2262         termPQExpBuffer(&buf);
2263         termPQExpBuffer(&title);
2264         termPQExpBuffer(&tmpbuf);
2265
2266         if (seq_values)
2267         {
2268                 for (ptr = seq_values; *ptr; ptr++)
2269                         free(*ptr);
2270                 free(seq_values);
2271         }
2272
2273         if (modifiers)
2274         {
2275                 for (ptr = modifiers; *ptr; ptr++)
2276                         free(*ptr);
2277                 free(modifiers);
2278         }
2279
2280         if (view_def)
2281                 free(view_def);
2282
2283         if (res)
2284                 PQclear(res);
2285
2286         return retval;
2287 }
2288
2289 /*
2290  * Add a tablespace description to a footer.  If 'newline' is true, it is added
2291  * in a new line; otherwise it's appended to the current value of the last
2292  * footer.
2293  */
2294 static void
2295 add_tablespace_footer(printTableContent *const cont, char relkind,
2296                                           Oid tablespace, const bool newline)
2297 {
2298         /* relkinds for which we support tablespaces */
2299         if (relkind == 'r' || relkind == 'i')
2300         {
2301                 /*
2302                  * We ignore the database default tablespace so that users not using
2303                  * tablespaces don't need to know about them.  This case also covers
2304                  * pre-8.0 servers, for which tablespace will always be 0.
2305                  */
2306                 if (tablespace != 0)
2307                 {
2308                         PGresult   *result = NULL;
2309                         PQExpBufferData buf;
2310
2311                         initPQExpBuffer(&buf);
2312                         printfPQExpBuffer(&buf,
2313                                                           "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2314                                                           "WHERE oid = '%u';", tablespace);
2315                         result = PSQLexec(buf.data, false);
2316                         if (!result)
2317                                 return;
2318                         /* Should always be the case, but.... */
2319                         if (PQntuples(result) > 0)
2320                         {
2321                                 if (newline)
2322                                 {
2323                                         /* Add the tablespace as a new footer */
2324                                         printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2325                                                                           PQgetvalue(result, 0, 0));
2326                                         printTableAddFooter(cont, buf.data);
2327                                 }
2328                                 else
2329                                 {
2330                                         /* Append the tablespace to the latest footer */
2331                                         printfPQExpBuffer(&buf, "%s", cont->footer->data);
2332
2333                                         /*
2334                                          * translator: before this string there's an index
2335                                          * description like '"foo_pkey" PRIMARY KEY, btree (a)'
2336                                          */
2337                                         appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2338                                                                           PQgetvalue(result, 0, 0));
2339                                         printTableSetFooter(cont, buf.data);
2340                                 }
2341                         }
2342                         PQclear(result);
2343                         termPQExpBuffer(&buf);
2344                 }
2345         }
2346 }
2347
2348 /*
2349  * \du or \dg
2350  *
2351  * Describes roles.  Any schema portion of the pattern is ignored.
2352  */
2353 bool
2354 describeRoles(const char *pattern, bool verbose)
2355 {
2356         PQExpBufferData buf;
2357         PGresult   *res;
2358         printTableContent cont;
2359         printTableOpt myopt = pset.popt.topt;
2360         int                     ncols = 3;
2361         int                     nrows = 0;
2362         int                     i;
2363         int                     conns;
2364         const char      align = 'l';
2365         char      **attr;
2366
2367         myopt.default_footer = false;
2368
2369         initPQExpBuffer(&buf);
2370
2371         if (pset.sversion >= 80100)
2372         {
2373                 printfPQExpBuffer(&buf,
2374                                                   "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2375                                                   "  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2376                                                   "  r.rolconnlimit, r.rolvaliduntil,\n"
2377                                                   "  ARRAY(SELECT b.rolname\n"
2378                                                   "        FROM pg_catalog.pg_auth_members m\n"
2379                                  "        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2380                                                   "        WHERE m.member = r.oid) as memberof");
2381
2382                 if (verbose && pset.sversion >= 80200)
2383                 {
2384                         appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2385                         ncols++;
2386                 }
2387                 if (pset.sversion >= 90100)
2388                 {
2389                         appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2390                 }
2391
2392                 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2393
2394                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2395                                                           NULL, "r.rolname", NULL, NULL);
2396         }
2397         else
2398         {
2399                 printfPQExpBuffer(&buf,
2400                                                   "SELECT u.usename AS rolname,\n"
2401                                                   "  u.usesuper AS rolsuper,\n"
2402                                                   "  true AS rolinherit, false AS rolcreaterole,\n"
2403                                          "  u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2404                                                   "  -1 AS rolconnlimit,"
2405                                                   "  u.valuntil as rolvaliduntil,\n"
2406                                                   "  ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2407                                                   "\nFROM pg_catalog.pg_user u\n");
2408
2409                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2410                                                           NULL, "u.usename", NULL, NULL);
2411         }
2412
2413         appendPQExpBuffer(&buf, "ORDER BY 1;");
2414
2415         res = PSQLexec(buf.data, false);
2416         if (!res)
2417                 return false;
2418
2419         nrows = PQntuples(res);
2420         attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
2421
2422         printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2423
2424         printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2425         printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2426         printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2427
2428         if (verbose && pset.sversion >= 80200)
2429                 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2430
2431         for (i = 0; i < nrows; i++)
2432         {
2433                 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2434
2435                 resetPQExpBuffer(&buf);
2436                 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2437                         add_role_attribute(&buf, _("Superuser"));
2438
2439                 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2440                         add_role_attribute(&buf, _("No inheritance"));
2441
2442                 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2443                         add_role_attribute(&buf, _("Create role"));
2444
2445                 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2446                         add_role_attribute(&buf, _("Create DB"));
2447
2448                 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2449                         add_role_attribute(&buf, _("Cannot login"));
2450
2451                 if (pset.sversion >= 90100)
2452                         if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2453                                 add_role_attribute(&buf, _("Replication"));
2454
2455                 conns = atoi(PQgetvalue(res, i, 6));
2456                 if (conns >= 0)
2457                 {
2458                         if (buf.len > 0)
2459                                 appendPQExpBufferStr(&buf, "\n");
2460
2461                         if (conns == 0)
2462                                 appendPQExpBuffer(&buf, _("No connections"));
2463                         else
2464                                 appendPQExpBuffer(&buf, ngettext("%d connection",
2465                                                                                                  "%d connections",
2466                                                                                                  conns),
2467                                                                   conns);
2468                 }
2469
2470                 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2471                 {
2472                         if (buf.len > 0)
2473                                 appendPQExpBufferStr(&buf, "\n");
2474                         appendPQExpBufferStr(&buf, _("Password valid until "));
2475                         appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
2476                 }
2477
2478                 attr[i] = pg_strdup(buf.data);
2479
2480                 printTableAddCell(&cont, attr[i], false, false);
2481
2482                 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2483
2484                 if (verbose && pset.sversion >= 80200)
2485                         printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
2486         }
2487         termPQExpBuffer(&buf);
2488
2489         printTable(&cont, pset.queryFout, pset.logfile);
2490         printTableCleanup(&cont);
2491
2492         for (i = 0; i < nrows; i++)
2493                 free(attr[i]);
2494         free(attr);
2495
2496         PQclear(res);
2497         return true;
2498 }
2499
2500 static void
2501 add_role_attribute(PQExpBuffer buf, const char *const str)
2502 {
2503         if (buf->len > 0)
2504                 appendPQExpBufferStr(buf, ", ");
2505
2506         appendPQExpBufferStr(buf, str);
2507 }
2508
2509 /*
2510  * \drds
2511  */
2512 bool
2513 listDbRoleSettings(const char *pattern, const char *pattern2)
2514 {
2515         PQExpBufferData buf;
2516         PGresult   *res;
2517         printQueryOpt myopt = pset.popt;
2518
2519         initPQExpBuffer(&buf);
2520
2521         if (pset.sversion >= 90000)
2522         {
2523                 bool            havewhere;
2524
2525                 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
2526                                 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2527                                                   "FROM pg_db_role_setting AS s\n"
2528                                    "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2529                                                   "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2530                                                   gettext_noop("Role"),
2531                                                   gettext_noop("Database"),
2532                                                   gettext_noop("Settings"));
2533                 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2534                                                                            NULL, "pg_roles.rolname", NULL, NULL);
2535                 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2536                                                           NULL, "pg_database.datname", NULL, NULL);
2537                 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
2538         }
2539         else
2540         {
2541                 fprintf(pset.queryFout,
2542                 _("No per-database role settings support in this server version.\n"));
2543                 return false;
2544         }
2545
2546         res = PSQLexec(buf.data, false);
2547         if (!res)
2548                 return false;
2549
2550         if (PQntuples(res) == 0 && !pset.quiet)
2551         {
2552                 if (pattern)
2553                         fprintf(pset.queryFout, _("No matching settings found.\n"));
2554                 else
2555                         fprintf(pset.queryFout, _("No settings found.\n"));
2556         }
2557         else
2558         {
2559                 myopt.nullPrint = NULL;
2560                 myopt.title = _("List of settings");
2561                 myopt.translate_header = true;
2562
2563                 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2564         }
2565
2566         PQclear(res);
2567         resetPQExpBuffer(&buf);
2568         return true;
2569 }
2570
2571
2572 /*
2573  * listTables()
2574  *
2575  * handler for \dt, \di, etc.
2576  *
2577  * tabtypes is an array of characters, specifying what info is desired:
2578  * t - tables
2579  * i - indexes
2580  * v - views
2581  * s - sequences
2582  * E - foreign table (Note: different from 'f', the relkind value)
2583  * (any order of the above is fine)
2584  * If tabtypes is empty, we default to \dtvsE.
2585  */
2586 bool
2587 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2588 {
2589         bool            showTables = strchr(tabtypes, 't') != NULL;
2590         bool            showIndexes = strchr(tabtypes, 'i') != NULL;
2591         bool            showViews = strchr(tabtypes, 'v') != NULL;
2592         bool            showSeq = strchr(tabtypes, 's') != NULL;
2593         bool            showForeign = strchr(tabtypes, 'E') != NULL;
2594
2595         PQExpBufferData buf;
2596         PGresult   *res;
2597         printQueryOpt myopt = pset.popt;
2598         static const bool translate_columns[] = {false, false, true, false, false, false, false};
2599
2600         if (!(showTables || showIndexes || showViews || showSeq || showForeign))
2601                 showTables = showViews = showSeq = showForeign = true;
2602
2603         initPQExpBuffer(&buf);
2604
2605         /*
2606          * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2607          * for backwards compatibility.
2608          */
2609         printfPQExpBuffer(&buf,
2610                                           "SELECT n.nspname as \"%s\",\n"
2611                                           "  c.relname as \"%s\",\n"
2612                                           "  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"
2613                                           "  pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2614                                           gettext_noop("Schema"),
2615                                           gettext_noop("Name"),
2616                                           gettext_noop("table"),
2617                                           gettext_noop("view"),
2618                                           gettext_noop("index"),
2619                                           gettext_noop("sequence"),
2620                                           gettext_noop("special"),
2621                                           gettext_noop("foreign table"),
2622                                           gettext_noop("Type"),
2623                                           gettext_noop("Owner"));
2624
2625         if (showIndexes)
2626                 appendPQExpBuffer(&buf,
2627                                                   ",\n c2.relname as \"%s\"",
2628                                                   gettext_noop("Table"));
2629
2630         if (verbose)
2631         {
2632                 /*
2633                  * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
2634                  * size of a table, including FSM, VM and TOAST tables.
2635                  */
2636                 if (pset.sversion >= 90000)
2637                         appendPQExpBuffer(&buf,
2638                                                           ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
2639                                                           gettext_noop("Size"));
2640                 else if (pset.sversion >= 80100)
2641                         appendPQExpBuffer(&buf,
2642                                                           ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2643                                                           gettext_noop("Size"));
2644
2645                 appendPQExpBuffer(&buf,
2646                           ",\n  pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2647                                                   gettext_noop("Description"));
2648         }
2649
2650         appendPQExpBuffer(&buf,
2651                                           "\nFROM pg_catalog.pg_class c"
2652          "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2653         if (showIndexes)
2654                 appendPQExpBuffer(&buf,
2655                          "\n     LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2656                    "\n     LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2657
2658         appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
2659         if (showTables)
2660                 appendPQExpBuffer(&buf, "'r',");
2661         if (showViews)
2662                 appendPQExpBuffer(&buf, "'v',");
2663         if (showIndexes)
2664                 appendPQExpBuffer(&buf, "'i',");
2665         if (showSeq)
2666                 appendPQExpBuffer(&buf, "'S',");
2667         if (showSystem || pattern)
2668                 appendPQExpBuffer(&buf, "'s',");                /* was RELKIND_SPECIAL in <=
2669                                                                                                  * 8.1 */
2670         if (showForeign)
2671                 appendPQExpBuffer(&buf, "'f',");
2672
2673         appendPQExpBuffer(&buf, "''");          /* dummy */
2674         appendPQExpBuffer(&buf, ")\n");
2675
2676         if (!showSystem && !pattern)
2677                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
2678                                                   "      AND n.nspname <> 'information_schema'\n");
2679
2680         /*
2681          * TOAST objects are suppressed unconditionally.  Since we don't provide
2682          * any way to select relkind 't' above, we would never show toast tables
2683          * in any case; it seems a bit confusing to allow their indexes to be
2684          * shown. Use plain \d if you really need to look at a TOAST table/index.
2685          */
2686         appendPQExpBuffer(&buf, "      AND n.nspname !~ '^pg_toast'\n");
2687
2688         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2689                                                   "n.nspname", "c.relname", NULL,
2690                                                   "pg_catalog.pg_table_is_visible(c.oid)");
2691
2692         appendPQExpBuffer(&buf, "ORDER BY 1,2;");
2693
2694         res = PSQLexec(buf.data, false);
2695         termPQExpBuffer(&buf);
2696         if (!res)
2697                 return false;
2698
2699         if (PQntuples(res) == 0 && !pset.quiet)
2700         {
2701                 if (pattern)
2702                         fprintf(pset.queryFout, _("No matching relations found.\n"));
2703                 else
2704                         fprintf(pset.queryFout, _("No relations found.\n"));
2705         }
2706         else
2707         {
2708                 myopt.nullPrint = NULL;
2709                 myopt.title = _("List of relations");
2710                 myopt.translate_header = true;
2711                 myopt.translate_columns = translate_columns;
2712
2713                 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2714         }
2715
2716         PQclear(res);
2717         return true;
2718 }
2719
2720
2721 /*
2722  * \dL
2723  *
2724  * Describes languages.
2725  */
2726 bool
2727 listLanguages(const char *pattern, bool verbose, bool showSystem)
2728 {
2729         PQExpBufferData buf;
2730         PGresult   *res;
2731         printQueryOpt myopt = pset.popt;
2732
2733         initPQExpBuffer(&buf);
2734
2735         printfPQExpBuffer(&buf,
2736                                           "SELECT l.lanname AS \"%s\",\n",
2737                                           gettext_noop("Name"));
2738         if (pset.sversion >= 80300)
2739                 appendPQExpBuffer(&buf,
2740                                 "       pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
2741                                                   gettext_noop("Owner"));
2742
2743         appendPQExpBuffer(&buf,
2744                                           "       l.lanpltrusted AS \"%s\"",
2745                                           gettext_noop("Trusted"));
2746
2747         if (verbose)
2748         {
2749                 appendPQExpBuffer(&buf,
2750                                                   ",\n       NOT l.lanispl AS \"%s\",\n"
2751                                                   "       l.lanplcallfoid::regprocedure AS \"%s\",\n"
2752                                    "       l.lanvalidator::regprocedure AS \"%s\",\n       ",
2753                                                   gettext_noop("Internal Language"),
2754                                                   gettext_noop("Call Handler"),
2755                                                   gettext_noop("Validator"));
2756                 if (pset.sversion >= 90000)
2757                         appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n       ",
2758                                                           gettext_noop("Inline Handler"));
2759                 printACLColumn(&buf, "l.lanacl");
2760         }
2761
2762         appendPQExpBuffer(&buf,
2763                                           ",\n       d.description AS \"%s\""
2764                                           "\nFROM pg_catalog.pg_language l\n"
2765                                           "LEFT JOIN pg_catalog.pg_description d\n"
2766                                           "  ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
2767                                           "  AND d.objsubid = 0\n",
2768                                           gettext_noop("Description"));
2769
2770         if (pattern)
2771                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2772                                                           NULL, "l.lanname", NULL, NULL);
2773
2774         if (!showSystem && !pattern)
2775                 appendPQExpBuffer(&buf, "WHERE l.lanplcallfoid != 0\n");
2776
2777
2778         appendPQExpBuffer(&buf, "ORDER BY 1;");
2779
2780         res = PSQLexec(buf.data, false);
2781         termPQExpBuffer(&buf);
2782         if (!res)
2783                 return false;
2784
2785         myopt.nullPrint = NULL;
2786         myopt.title = _("List of languages");
2787         myopt.translate_header = true;
2788
2789         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2790
2791         PQclear(res);
2792         return true;
2793 }
2794
2795
2796 /*
2797  * \dD
2798  *
2799  * Describes domains.
2800  */
2801 bool
2802 listDomains(const char *pattern, bool verbose, bool showSystem)
2803 {
2804         PQExpBufferData buf;
2805         PGresult   *res;
2806         printQueryOpt myopt = pset.popt;
2807
2808         initPQExpBuffer(&buf);
2809
2810         printfPQExpBuffer(&buf,
2811                                           "SELECT n.nspname as \"%s\",\n"
2812                                           "       t.typname as \"%s\",\n"
2813          "       pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2814                                           "       TRIM(LEADING\n",
2815                                           gettext_noop("Schema"),
2816                                           gettext_noop("Name"),
2817                                           gettext_noop("Type"));
2818
2819         if (pset.sversion >= 90100)
2820                 appendPQExpBuffer(&buf,
2821                                                   "            COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
2822                                                   "                      WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
2823         appendPQExpBuffer(&buf,
2824            "            CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
2825                                           "            CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
2826                                           "       ) as \"%s\",\n"
2827                                           "       pg_catalog.array_to_string(ARRAY(\n"
2828                                           "         SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2829                                           "       ), ' ') as \"%s\"",
2830                                           gettext_noop("Modifier"),
2831                                           gettext_noop("Check"));
2832
2833         if (verbose)
2834         {
2835                 if (pset.sversion >= 90200)
2836                 {
2837                         appendPQExpBuffer(&buf, ",\n  ");
2838                         printACLColumn(&buf, "t.typacl");
2839                 }
2840                 appendPQExpBuffer(&buf,
2841                                                   ",\n       d.description as \"%s\"",
2842                                                   gettext_noop("Description"));
2843         }
2844
2845         appendPQExpBuffer(&buf,
2846                                           "\nFROM pg_catalog.pg_type t\n"
2847          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
2848
2849         if (verbose)
2850                 appendPQExpBuffer(&buf,
2851                                                   "     LEFT JOIN pg_catalog.pg_description d "
2852                                                   "ON d.classoid = t.tableoid AND d.objoid = t.oid "
2853                                                   "AND d.objsubid = 0\n");
2854
2855         appendPQExpBuffer(&buf, "WHERE t.typtype = 'd'\n");
2856
2857         if (!showSystem && !pattern)
2858                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
2859                                                   "      AND n.nspname <> 'information_schema'\n");
2860
2861         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2862                                                   "n.nspname", "t.typname", NULL,
2863                                                   "pg_catalog.pg_type_is_visible(t.oid)");
2864
2865         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2866
2867         res = PSQLexec(buf.data, false);
2868         termPQExpBuffer(&buf);
2869         if (!res)
2870                 return false;
2871
2872         myopt.nullPrint = NULL;
2873         myopt.title = _("List of domains");
2874         myopt.translate_header = true;
2875
2876         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2877
2878         PQclear(res);
2879         return true;
2880 }
2881
2882 /*
2883  * \dc
2884  *
2885  * Describes conversions.
2886  */
2887 bool
2888 listConversions(const char *pattern, bool verbose, bool showSystem)
2889 {
2890         PQExpBufferData buf;
2891         PGresult   *res;
2892         printQueryOpt myopt = pset.popt;
2893         static const bool translate_columns[] = {false, false, false, false, true};
2894
2895         initPQExpBuffer(&buf);
2896
2897         printfPQExpBuffer(&buf,
2898                                           "SELECT n.nspname AS \"%s\",\n"
2899                                           "       c.conname AS \"%s\",\n"
2900            "       pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2901                 "       pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2902                                           "       CASE WHEN c.condefault THEN '%s'\n"
2903                                           "       ELSE '%s' END AS \"%s\"",
2904                                           gettext_noop("Schema"),
2905                                           gettext_noop("Name"),
2906                                           gettext_noop("Source"),
2907                                           gettext_noop("Destination"),
2908                                           gettext_noop("yes"), gettext_noop("no"),
2909                                           gettext_noop("Default?"));
2910
2911         if (verbose)
2912                 appendPQExpBuffer(&buf,
2913                                                   ",\n       d.description AS \"%s\"",
2914                                                   gettext_noop("Description"));
2915
2916         appendPQExpBuffer(&buf,
2917                                           "\nFROM pg_catalog.pg_conversion c\n"
2918                                           "     JOIN pg_catalog.pg_namespace n "
2919                                           "ON n.oid = c.connamespace\n");
2920
2921         if (verbose)
2922                 appendPQExpBuffer(&buf,
2923                                                   "LEFT JOIN pg_catalog.pg_description d "
2924                                                   "ON d.classoid = c.tableoid\n"
2925                                                   "          AND d.objoid = c.oid "
2926                                                   "AND d.objsubid = 0\n");
2927
2928         appendPQExpBuffer(&buf, "WHERE true\n");
2929
2930         if (!showSystem && !pattern)
2931                 appendPQExpBuffer(&buf, "  AND n.nspname <> 'pg_catalog'\n"
2932                                                   "  AND n.nspname <> 'information_schema'\n");
2933
2934         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2935                                                   "n.nspname", "c.conname", NULL,
2936                                                   "pg_catalog.pg_conversion_is_visible(c.oid)");
2937
2938         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2939
2940         res = PSQLexec(buf.data, false);
2941         termPQExpBuffer(&buf);
2942         if (!res)
2943                 return false;
2944
2945         myopt.nullPrint = NULL;
2946         myopt.title = _("List of conversions");
2947         myopt.translate_header = true;
2948         myopt.translate_columns = translate_columns;
2949
2950         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2951
2952         PQclear(res);
2953         return true;
2954 }
2955
2956 /*
2957  * \dy
2958  *
2959  * Describes Event Triggers.
2960  */
2961 bool
2962 listEventTriggers(const char *pattern, bool verbose)
2963 {
2964         PQExpBufferData buf;
2965         PGresult   *res;
2966         printQueryOpt myopt = pset.popt;
2967         static const bool translate_columns[] =
2968                 {false, false, false, true, false, false, false};
2969
2970         initPQExpBuffer(&buf);
2971
2972         printfPQExpBuffer(&buf,
2973                                           "select evtname as \"%s\", "
2974                                           "evtevent as  \"%s\", "
2975                                           "pg_catalog.pg_get_userbyid(e.evtowner) AS \"%s\", "
2976                                           "case evtenabled when 'O' then 'enabled' "
2977                                           "  when 'R' then 'replica' "
2978                                           "  when 'A' then 'always' "
2979                                           "  when 'D' then 'disabled' end as  \"%s\", "
2980                                           "e.evtfoid::regproc as \"%s\", "
2981                                           "array_to_string(array(select x "
2982                                           "      from unnest(evttags) as t(x)), ', ') as  \"%s\" ",
2983                                           gettext_noop("Name"),
2984                                           gettext_noop("Event"),
2985                                           gettext_noop("Owner"),
2986                                           gettext_noop("Enabled"),
2987                                           gettext_noop("Procedure"),
2988                                           gettext_noop("Tags"));
2989         if (verbose)
2990                 appendPQExpBuffer(&buf,
2991                                                   ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
2992                                                   gettext_noop("Description"));
2993         appendPQExpBuffer(&buf,
2994                                           "\nFROM pg_event_trigger e ");
2995
2996         processSQLNamePattern(pset.db, &buf, pattern, false, false,
2997                                                   NULL, "evtname", NULL, NULL);
2998
2999         appendPQExpBuffer(&buf, "ORDER BY 1");
3000
3001         res = PSQLexec(buf.data, false);
3002         termPQExpBuffer(&buf);
3003         if (!res)
3004                 return false;
3005
3006         myopt.nullPrint = NULL;
3007         myopt.title = _("List of event triggers");
3008         myopt.translate_header = true;
3009         myopt.translate_columns = translate_columns;
3010
3011         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3012
3013         PQclear(res);
3014         return true;
3015 }
3016
3017 /*
3018  * \dC
3019  *
3020  * Describes casts.
3021  */
3022 bool
3023 listCasts(const char *pattern, bool verbose)
3024 {
3025         PQExpBufferData buf;
3026         PGresult   *res;
3027         printQueryOpt myopt = pset.popt;
3028         static const bool translate_columns[] = {false, false, false, true};
3029
3030         initPQExpBuffer(&buf);
3031
3032         /*
3033          * We need a left join to pg_proc for binary casts; the others are just
3034          * paranoia.  Also note that we don't attempt to localize '(binary
3035          * coercible)', because there's too much risk of gettext translating a
3036          * function name that happens to match some string in the PO database.
3037          */
3038         printfPQExpBuffer(&buf,
3039                            "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3040                            "       pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3041                                   "       CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3042                                           "            ELSE p.proname\n"
3043                                           "       END as \"%s\",\n"
3044                                           "       CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3045                                           "            WHEN c.castcontext = 'a' THEN '%s'\n"
3046                                           "            ELSE '%s'\n"
3047                                           "       END as \"%s\"",
3048                                           gettext_noop("Source type"),
3049                                           gettext_noop("Target type"),
3050                                           gettext_noop("Function"),
3051                                           gettext_noop("no"),
3052                                           gettext_noop("in assignment"),
3053                                           gettext_noop("yes"),
3054                                           gettext_noop("Implicit?"));
3055
3056         if (verbose)
3057                 appendPQExpBuffer(&buf,
3058                                                   ",\n       d.description AS \"%s\"\n",
3059                                                   gettext_noop("Description"));
3060
3061         appendPQExpBuffer(&buf,
3062                                  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3063                                           "     ON c.castfunc = p.oid\n"
3064                                           "     LEFT JOIN pg_catalog.pg_type ts\n"
3065                                           "     ON c.castsource = ts.oid\n"
3066                                           "     LEFT JOIN pg_catalog.pg_namespace ns\n"
3067                                           "     ON ns.oid = ts.typnamespace\n"
3068                                           "     LEFT JOIN pg_catalog.pg_type tt\n"
3069                                           "     ON c.casttarget = tt.oid\n"
3070                                           "     LEFT JOIN pg_catalog.pg_namespace nt\n"
3071                                           "     ON nt.oid = tt.typnamespace\n");
3072
3073         if (verbose)
3074                 appendPQExpBuffer(&buf,
3075                                                   "     LEFT JOIN pg_catalog.pg_description d\n"
3076                                                   "     ON d.classoid = c.tableoid AND d.objoid = "
3077                                                   "c.oid AND d.objsubid = 0\n");
3078
3079         appendPQExpBuffer(&buf, "WHERE ( (true");
3080
3081         /*
3082          * Match name pattern against either internal or external name of either
3083          * castsource or casttarget
3084          */
3085         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3086                                                   "ns.nspname", "ts.typname",
3087                                                   "pg_catalog.format_type(ts.oid, NULL)",
3088                                                   "pg_catalog.pg_type_is_visible(ts.oid)");
3089
3090         appendPQExpBuffer(&buf, ") OR (true");
3091
3092         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3093                                                   "nt.nspname", "tt.typname",
3094                                                   "pg_catalog.format_type(tt.oid, NULL)",
3095                                                   "pg_catalog.pg_type_is_visible(tt.oid)");
3096
3097         appendPQExpBuffer(&buf, ") )\nORDER BY 1, 2;");
3098
3099         res = PSQLexec(buf.data, false);
3100         termPQExpBuffer(&buf);
3101         if (!res)
3102                 return false;
3103
3104         myopt.nullPrint = NULL;
3105         myopt.title = _("List of casts");
3106         myopt.translate_header = true;
3107         myopt.translate_columns = translate_columns;
3108
3109         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3110
3111         PQclear(res);
3112         return true;
3113 }
3114
3115 /*
3116  * \dO
3117  *
3118  * Describes collations.
3119  */
3120 bool
3121 listCollations(const char *pattern, bool verbose, bool showSystem)
3122 {
3123         PQExpBufferData buf;
3124         PGresult   *res;
3125         printQueryOpt myopt = pset.popt;
3126         static const bool translate_columns[] = {false, false, false, false, false};
3127
3128         if (pset.sversion < 90100)
3129         {
3130                 psql_error("The server (version %d.%d) does not support collations.\n",
3131                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3132                 return true;
3133         }
3134
3135         initPQExpBuffer(&buf);
3136
3137         printfPQExpBuffer(&buf,
3138                                           "SELECT n.nspname AS \"%s\",\n"
3139                                           "       c.collname AS \"%s\",\n"
3140                                           "       c.collcollate AS \"%s\",\n"
3141                                           "       c.collctype AS \"%s\"",
3142                                           gettext_noop("Schema"),
3143                                           gettext_noop("Name"),
3144                                           gettext_noop("Collate"),
3145                                           gettext_noop("Ctype"));
3146
3147         if (verbose)
3148                 appendPQExpBuffer(&buf,
3149                                                   ",\n       pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3150                                                   gettext_noop("Description"));
3151
3152         appendPQExpBuffer(&buf,
3153                           "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3154                                           "WHERE n.oid = c.collnamespace\n");
3155
3156         if (!showSystem && !pattern)
3157                 appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
3158                                                   "      AND n.nspname <> 'information_schema'\n");
3159
3160         /*
3161          * Hide collations that aren't usable in the current database's encoding.
3162          * If you think to change this, note that pg_collation_is_visible rejects
3163          * unusable collations, so you will need to hack name pattern processing
3164          * somehow to avoid inconsistent behavior.
3165          */
3166         appendPQExpBuffer(&buf, "      AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3167
3168         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3169                                                   "n.nspname", "c.collname", NULL,
3170                                                   "pg_catalog.pg_collation_is_visible(c.oid)");
3171
3172         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3173
3174         res = PSQLexec(buf.data, false);
3175         termPQExpBuffer(&buf);
3176         if (!res)
3177                 return false;
3178
3179         myopt.nullPrint = NULL;
3180         myopt.title = _("List of collations");
3181         myopt.translate_header = true;
3182         myopt.translate_columns = translate_columns;
3183
3184         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3185
3186         PQclear(res);
3187         return true;
3188 }
3189
3190 /*
3191  * \dn
3192  *
3193  * Describes schemas (namespaces)
3194  */
3195 bool
3196 listSchemas(const char *pattern, bool verbose, bool showSystem)
3197 {
3198         PQExpBufferData buf;
3199         PGresult   *res;
3200         printQueryOpt myopt = pset.popt;
3201
3202         initPQExpBuffer(&buf);
3203         printfPQExpBuffer(&buf,
3204                                           "SELECT n.nspname AS \"%s\",\n"
3205                                           "  pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3206                                           gettext_noop("Name"),
3207                                           gettext_noop("Owner"));
3208
3209         if (verbose)
3210         {
3211                 appendPQExpBuffer(&buf, ",\n  ");
3212                 printACLColumn(&buf, "n.nspacl");
3213                 appendPQExpBuffer(&buf,
3214                   ",\n  pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3215                                                   gettext_noop("Description"));
3216         }
3217
3218         appendPQExpBuffer(&buf,
3219                                           "\nFROM pg_catalog.pg_namespace n\n");
3220
3221         if (!showSystem && !pattern)
3222                 appendPQExpBuffer(&buf,
3223                 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3224
3225         processSQLNamePattern(pset.db, &buf, pattern,
3226                                                   !showSystem && !pattern, false,
3227                                                   NULL, "n.nspname", NULL,
3228                                                   NULL);
3229
3230         appendPQExpBuffer(&buf, "ORDER BY 1;");
3231
3232         res = PSQLexec(buf.data, false);
3233         termPQExpBuffer(&buf);
3234         if (!res)
3235                 return false;
3236
3237         myopt.nullPrint = NULL;
3238         myopt.title = _("List of schemas");
3239         myopt.translate_header = true;
3240
3241         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3242
3243         PQclear(res);
3244         return true;
3245 }
3246
3247
3248 /*
3249  * \dFp
3250  * list text search parsers
3251  */
3252 bool
3253 listTSParsers(const char *pattern, bool verbose)
3254 {
3255         PQExpBufferData buf;
3256         PGresult   *res;
3257         printQueryOpt myopt = pset.popt;
3258
3259         if (pset.sversion < 80300)
3260         {
3261                 psql_error("The server (version %d.%d) does not support full text search.\n",
3262                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3263                 return true;
3264         }
3265
3266         if (verbose)
3267                 return listTSParsersVerbose(pattern);
3268
3269         initPQExpBuffer(&buf);
3270
3271         printfPQExpBuffer(&buf,
3272                                           "SELECT \n"
3273                                           "  n.nspname as \"%s\",\n"
3274                                           "  p.prsname as \"%s\",\n"
3275                         "  pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3276                                           "FROM pg_catalog.pg_ts_parser p \n"
3277                    "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3278                                           gettext_noop("Schema"),
3279                                           gettext_noop("Name"),
3280                                           gettext_noop("Description")
3281                 );
3282
3283         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3284                                                   "n.nspname", "p.prsname", NULL,
3285                                                   "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3286
3287         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3288
3289         res = PSQLexec(buf.data, false);
3290         termPQExpBuffer(&buf);
3291         if (!res)
3292                 return false;
3293
3294         myopt.nullPrint = NULL;
3295         myopt.title = _("List of text search parsers");
3296         myopt.translate_header = true;
3297
3298         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3299
3300         PQclear(res);
3301         return true;
3302 }
3303
3304 /*
3305  * full description of parsers
3306  */
3307 static bool
3308 listTSParsersVerbose(const char *pattern)
3309 {
3310         PQExpBufferData buf;
3311         PGresult   *res;
3312         int                     i;
3313
3314         initPQExpBuffer(&buf);
3315
3316         printfPQExpBuffer(&buf,
3317                                           "SELECT p.oid, \n"
3318                                           "  n.nspname, \n"
3319                                           "  p.prsname \n"
3320                                           "FROM pg_catalog.pg_ts_parser p\n"
3321                         "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3322                 );
3323
3324         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3325                                                   "n.nspname", "p.prsname", NULL,
3326                                                   "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3327
3328         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3329
3330         res = PSQLexec(buf.data, false);
3331         termPQExpBuffer(&buf);
3332         if (!res)
3333                 return false;
3334
3335         if (PQntuples(res) == 0)
3336         {
3337                 if (!pset.quiet)
3338                         psql_error("Did not find any text search parser named \"%s\".\n",
3339                                         pattern);
3340                 PQclear(res);
3341                 return false;
3342         }
3343
3344         for (i = 0; i < PQntuples(res); i++)
3345         {
3346                 const char *oid;
3347                 const char *nspname = NULL;
3348                 const char *prsname;
3349
3350                 oid = PQgetvalue(res, i, 0);
3351                 if (!PQgetisnull(res, i, 1))
3352                         nspname = PQgetvalue(res, i, 1);
3353                 prsname = PQgetvalue(res, i, 2);
3354
3355                 if (!describeOneTSParser(oid, nspname, prsname))
3356                 {
3357                         PQclear(res);
3358                         return false;
3359                 }
3360
3361                 if (cancel_pressed)
3362                 {
3363                         PQclear(res);
3364                         return false;
3365                 }
3366         }
3367
3368         PQclear(res);
3369         return true;
3370 }
3371
3372 static bool
3373 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3374 {
3375         PQExpBufferData buf;
3376         PGresult   *res;
3377         char            title[1024];
3378         printQueryOpt myopt = pset.popt;
3379         static const bool translate_columns[] = {true, false, false};
3380
3381         initPQExpBuffer(&buf);
3382
3383         printfPQExpBuffer(&buf,
3384                                           "SELECT '%s' AS \"%s\", \n"
3385                                           "   p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3386                   "   pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3387                                           " FROM pg_catalog.pg_ts_parser p \n"
3388                                           " WHERE p.oid = '%s' \n"
3389                                           "UNION ALL \n"
3390                                           "SELECT '%s', \n"
3391                                           "   p.prstoken::pg_catalog.regproc, \n"
3392                                         "   pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3393                                           " FROM pg_catalog.pg_ts_parser p \n"
3394                                           " WHERE p.oid = '%s' \n"
3395                                           "UNION ALL \n"
3396                                           "SELECT '%s', \n"
3397                                           "   p.prsend::pg_catalog.regproc, \n"
3398                                           "   pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3399                                           " FROM pg_catalog.pg_ts_parser p \n"
3400                                           " WHERE p.oid = '%s' \n"
3401                                           "UNION ALL \n"
3402                                           "SELECT '%s', \n"
3403                                           "   p.prsheadline::pg_catalog.regproc, \n"
3404                                  "   pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3405                                           " FROM pg_catalog.pg_ts_parser p \n"
3406                                           " WHERE p.oid = '%s' \n"
3407                                           "UNION ALL \n"
3408                                           "SELECT '%s', \n"
3409                                           "   p.prslextype::pg_catalog.regproc, \n"
3410                                   "   pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3411                                           " FROM pg_catalog.pg_ts_parser p \n"
3412                                           " WHERE p.oid = '%s';",
3413                                           gettext_noop("Start parse"),
3414                                           gettext_noop("Method"),
3415                                           gettext_noop("Function"),
3416                                           gettext_noop("Description"),
3417                                           oid,
3418                                           gettext_noop("Get next token"),
3419                                           oid,
3420                                           gettext_noop("End parse"),
3421                                           oid,
3422                                           gettext_noop("Get headline"),
3423                                           oid,
3424                                           gettext_noop("Get token types"),
3425                                           oid);
3426
3427         res = PSQLexec(buf.data, false);
3428         termPQExpBuffer(&buf);
3429         if (!res)
3430                 return false;
3431
3432         myopt.nullPrint = NULL;
3433         if (nspname)
3434                 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3435         else
3436                 sprintf(title, _("Text search parser \"%s\""), prsname);
3437         myopt.title = title;
3438         myopt.footers = NULL;
3439         myopt.topt.default_footer = false;
3440         myopt.translate_header = true;
3441         myopt.translate_columns = translate_columns;
3442
3443         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3444
3445         PQclear(res);
3446
3447         initPQExpBuffer(&buf);
3448
3449         printfPQExpBuffer(&buf,
3450                                           "SELECT t.alias as \"%s\", \n"
3451                                           "  t.description as \"%s\" \n"
3452                           "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3453                                           "ORDER BY 1;",
3454                                           gettext_noop("Token name"),
3455                                           gettext_noop("Description"),
3456                                           oid);
3457
3458         res = PSQLexec(buf.data, false);
3459         termPQExpBuffer(&buf);
3460         if (!res)
3461                 return false;
3462
3463         myopt.nullPrint = NULL;
3464         if (nspname)
3465                 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3466         else
3467                 sprintf(title, _("Token types for parser \"%s\""), prsname);
3468         myopt.title = title;
3469         myopt.footers = NULL;
3470         myopt.topt.default_footer = true;
3471         myopt.translate_header = true;
3472         myopt.translate_columns = NULL;
3473
3474         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3475
3476         PQclear(res);
3477         return true;
3478 }
3479
3480
3481 /*
3482  * \dFd
3483  * list text search dictionaries
3484  */
3485 bool
3486 listTSDictionaries(const char *pattern, bool verbose)
3487 {
3488         PQExpBufferData buf;
3489         PGresult   *res;
3490         printQueryOpt myopt = pset.popt;
3491
3492         if (pset.sversion < 80300)
3493         {
3494                 psql_error("The server (version %d.%d) does not support full text search.\n",
3495                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3496                 return true;
3497         }
3498
3499         initPQExpBuffer(&buf);
3500
3501         printfPQExpBuffer(&buf,
3502                                           "SELECT \n"
3503                                           "  n.nspname as \"%s\",\n"
3504                                           "  d.dictname as \"%s\",\n",
3505                                           gettext_noop("Schema"),
3506                                           gettext_noop("Name"));
3507
3508         if (verbose)
3509         {
3510                 appendPQExpBuffer(&buf,
3511                                                   "  ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3512                                                   "    pg_catalog.pg_ts_template t \n"
3513                                                   "                      LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3514                                                   "                      WHERE d.dicttemplate = t.oid ) AS  \"%s\", \n"
3515                                                   "  d.dictinitoption as \"%s\", \n",
3516                                                   gettext_noop("Template"),
3517                                                   gettext_noop("Init options"));
3518         }
3519
3520         appendPQExpBuffer(&buf,
3521                          "  pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3522                                           gettext_noop("Description"));
3523
3524         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3525                  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3526
3527         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3528                                                   "n.nspname", "d.dictname", NULL,
3529                                                   "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3530
3531         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3532
3533         res = PSQLexec(buf.data, false);
3534         termPQExpBuffer(&buf);
3535         if (!res)
3536                 return false;
3537
3538         myopt.nullPrint = NULL;
3539         myopt.title = _("List of text search dictionaries");
3540         myopt.translate_header = true;
3541
3542         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3543
3544         PQclear(res);
3545         return true;
3546 }
3547
3548
3549 /*
3550  * \dFt
3551  * list text search templates
3552  */
3553 bool
3554 listTSTemplates(const char *pattern, bool verbose)
3555 {
3556         PQExpBufferData buf;
3557         PGresult   *res;
3558         printQueryOpt myopt = pset.popt;
3559
3560         if (pset.sversion < 80300)
3561         {
3562                 psql_error("The server (version %d.%d) does not support full text search.\n",
3563                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3564                 return true;
3565         }
3566
3567         initPQExpBuffer(&buf);
3568
3569         if (verbose)
3570                 printfPQExpBuffer(&buf,
3571                                                   "SELECT \n"
3572                                                   "  n.nspname AS \"%s\",\n"
3573                                                   "  t.tmplname AS \"%s\",\n"
3574                                                   "  t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3575                                                   "  t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3576                  "  pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3577                                                   gettext_noop("Schema"),
3578                                                   gettext_noop("Name"),
3579                                                   gettext_noop("Init"),
3580                                                   gettext_noop("Lexize"),
3581                                                   gettext_noop("Description"));
3582         else
3583                 printfPQExpBuffer(&buf,
3584                                                   "SELECT \n"
3585                                                   "  n.nspname AS \"%s\",\n"
3586                                                   "  t.tmplname AS \"%s\",\n"
3587                  "  pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3588                                                   gettext_noop("Schema"),
3589                                                   gettext_noop("Name"),
3590                                                   gettext_noop("Description"));
3591
3592         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
3593                  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3594
3595         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3596                                                   "n.nspname", "t.tmplname", NULL,
3597                                                   "pg_catalog.pg_ts_template_is_visible(t.oid)");
3598
3599         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3600
3601         res = PSQLexec(buf.data, false);
3602         termPQExpBuffer(&buf);
3603         if (!res)
3604                 return false;
3605
3606         myopt.nullPrint = NULL;
3607         myopt.title = _("List of text search templates");
3608         myopt.translate_header = true;
3609
3610         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3611
3612         PQclear(res);
3613         return true;
3614 }
3615
3616
3617 /*
3618  * \dF
3619  * list text search configurations
3620  */
3621 bool
3622 listTSConfigs(const char *pattern, bool verbose)
3623 {
3624         PQExpBufferData buf;
3625         PGresult   *res;
3626         printQueryOpt myopt = pset.popt;
3627
3628         if (pset.sversion < 80300)
3629         {
3630                 psql_error("The server (version %d.%d) does not support full text search.\n",
3631                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3632                 return true;
3633         }
3634
3635         if (verbose)
3636                 return listTSConfigsVerbose(pattern);
3637
3638         initPQExpBuffer(&buf);
3639
3640         printfPQExpBuffer(&buf,
3641                                           "SELECT \n"
3642                                           "   n.nspname as \"%s\",\n"
3643                                           "   c.cfgname as \"%s\",\n"
3644                    "   pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3645                                           "FROM pg_catalog.pg_ts_config c\n"
3646                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3647                                           gettext_noop("Schema"),
3648                                           gettext_noop("Name"),
3649                                           gettext_noop("Description")
3650                 );
3651
3652         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3653                                                   "n.nspname", "c.cfgname", NULL,
3654                                                   "pg_catalog.pg_ts_config_is_visible(c.oid)");
3655
3656         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3657
3658         res = PSQLexec(buf.data, false);
3659         termPQExpBuffer(&buf);
3660         if (!res)
3661                 return false;
3662
3663         myopt.nullPrint = NULL;
3664         myopt.title = _("List of text search configurations");
3665         myopt.translate_header = true;
3666
3667         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3668
3669         PQclear(res);
3670         return true;
3671 }
3672
3673 static bool
3674 listTSConfigsVerbose(const char *pattern)
3675 {
3676         PQExpBufferData buf;
3677         PGresult   *res;
3678         int                     i;
3679
3680         initPQExpBuffer(&buf);
3681
3682         printfPQExpBuffer(&buf,
3683                                           "SELECT c.oid, c.cfgname,\n"
3684                                           "   n.nspname, \n"
3685                                           "   p.prsname, \n"
3686                                           "   np.nspname as pnspname \n"
3687                                           "FROM pg_catalog.pg_ts_config c \n"
3688            "   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3689                                           " pg_catalog.pg_ts_parser p \n"
3690           "   LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3691                                           "WHERE  p.oid = c.cfgparser\n"
3692                 );
3693
3694         processSQLNamePattern(pset.db, &buf, pattern, true, false,
3695                                                   "n.nspname", "c.cfgname", NULL,
3696                                                   "pg_catalog.pg_ts_config_is_visible(c.oid)");
3697
3698         appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
3699
3700         res = PSQLexec(buf.data, false);
3701         termPQExpBuffer(&buf);
3702         if (!res)
3703                 return false;
3704
3705         if (PQntuples(res) == 0)
3706         {
3707                 if (!pset.quiet)
3708                         psql_error("Did not find any text search configuration named \"%s\".\n",
3709                                         pattern);
3710                 PQclear(res);
3711                 return false;
3712         }
3713
3714         for (i = 0; i < PQntuples(res); i++)
3715         {
3716                 const char *oid;
3717                 const char *cfgname;
3718                 const char *nspname = NULL;
3719                 const char *prsname;
3720                 const char *pnspname = NULL;
3721
3722                 oid = PQgetvalue(res, i, 0);
3723                 cfgname = PQgetvalue(res, i, 1);
3724                 if (!PQgetisnull(res, i, 2))
3725                         nspname = PQgetvalue(res, i, 2);
3726                 prsname = PQgetvalue(res, i, 3);
3727                 if (!PQgetisnull(res, i, 4))
3728                         pnspname = PQgetvalue(res, i, 4);
3729
3730                 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
3731                 {
3732                         PQclear(res);
3733                         return false;
3734                 }
3735
3736                 if (cancel_pressed)
3737                 {
3738                         PQclear(res);
3739                         return false;
3740                 }
3741         }
3742
3743         PQclear(res);
3744         return true;
3745 }
3746
3747 static bool
3748 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3749                                         const char *pnspname, const char *prsname)
3750 {
3751         PQExpBufferData buf,
3752                                 title;
3753         PGresult   *res;
3754         printQueryOpt myopt = pset.popt;
3755
3756         initPQExpBuffer(&buf);
3757
3758         printfPQExpBuffer(&buf,
3759                                           "SELECT \n"
3760                                           "  ( SELECT t.alias FROM \n"
3761                                           "    pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3762                                           "    WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3763                                           "  pg_catalog.btrim( \n"
3764                                   "    ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3765                                           "           FROM pg_catalog.pg_ts_config_map AS mm \n"
3766                                           "           WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3767                                           "           ORDER BY mapcfg, maptokentype, mapseqno \n"
3768                                           "    ) :: pg_catalog.text , \n"
3769                                           "  '{}') AS \"%s\" \n"
3770          "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3771                                           "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3772                                           "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3773                                           "ORDER BY 1;",
3774                                           gettext_noop("Token"),
3775                                           gettext_noop("Dictionaries"),
3776                                           oid);
3777
3778         res = PSQLexec(buf.data, false);
3779         termPQExpBuffer(&buf);
3780         if (!res)
3781                 return false;
3782
3783         initPQExpBuffer(&title);
3784
3785         if (nspname)
3786                 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3787                                                   nspname, cfgname);
3788         else
3789                 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3790                                                   cfgname);
3791
3792         if (pnspname)
3793                 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3794                                                   pnspname, prsname);
3795         else
3796                 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3797                                                   prsname);
3798
3799         myopt.nullPrint = NULL;
3800         myopt.title = title.data;
3801         myopt.footers = NULL;
3802         myopt.topt.default_footer = false;
3803         myopt.translate_header = true;
3804
3805         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3806
3807         termPQExpBuffer(&title);
3808
3809         PQclear(res);
3810         return true;
3811 }
3812
3813
3814 /*
3815  * \dew
3816  *
3817  * Describes foreign-data wrappers
3818  */
3819 bool
3820 listForeignDataWrappers(const char *pattern, bool verbose)
3821 {
3822         PQExpBufferData buf;
3823         PGresult   *res;
3824         printQueryOpt myopt = pset.popt;
3825
3826         if (pset.sversion < 80400)
3827         {
3828                 psql_error("The server (version %d.%d) does not support foreign-data wrappers.\n",
3829                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3830                 return true;
3831         }
3832
3833         initPQExpBuffer(&buf);
3834         printfPQExpBuffer(&buf,
3835                                           "SELECT fdw.fdwname AS \"%s\",\n"
3836                                    "  pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
3837                                           gettext_noop("Name"),
3838                                           gettext_noop("Owner"));
3839         if (pset.sversion >= 90100)
3840                 appendPQExpBuffer(&buf,
3841                                                   "  fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
3842                                                   gettext_noop("Handler"));
3843         appendPQExpBuffer(&buf,
3844                                           "  fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
3845                                           gettext_noop("Validator"));
3846
3847         if (verbose)
3848         {
3849                 appendPQExpBuffer(&buf, ",\n  ");
3850                 printACLColumn(&buf, "fdwacl");
3851                 appendPQExpBuffer(&buf,
3852                                                   ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
3853                                                   "  '(' || array_to_string(ARRAY(SELECT "
3854                                                   "  quote_ident(option_name) ||  ' ' || "
3855                                                   "  quote_literal(option_value)  FROM "
3856                                                   "  pg_options_to_table(fdwoptions)),  ', ') || ')' "
3857                                                   "  END AS \"%s\"",
3858                                                   gettext_noop("FDW Options"));
3859
3860                 if (pset.sversion >= 90100)
3861                         appendPQExpBuffer(&buf,
3862                                                           ",\n  d.description AS \"%s\" ",
3863                                                           gettext_noop("Description"));
3864         }
3865
3866         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
3867
3868         if (verbose && pset.sversion >= 90100)
3869                 appendPQExpBuffer(&buf,
3870                                                   "LEFT JOIN pg_catalog.pg_description d\n"
3871                                                   "       ON d.classoid = fdw.tableoid "
3872                                                   "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
3873
3874         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3875                                                   NULL, "fdwname", NULL, NULL);
3876
3877         appendPQExpBuffer(&buf, "ORDER BY 1;");
3878
3879         res = PSQLexec(buf.data, false);
3880         termPQExpBuffer(&buf);
3881         if (!res)
3882                 return false;
3883
3884         myopt.nullPrint = NULL;
3885         myopt.title = _("List of foreign-data wrappers");
3886         myopt.translate_header = true;
3887
3888         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3889
3890         PQclear(res);
3891         return true;
3892 }
3893
3894 /*
3895  * \des
3896  *
3897  * Describes foreign servers.
3898  */
3899 bool
3900 listForeignServers(const char *pattern, bool verbose)
3901 {
3902         PQExpBufferData buf;
3903         PGresult   *res;
3904         printQueryOpt myopt = pset.popt;
3905
3906         if (pset.sversion < 80400)
3907         {
3908                 psql_error("The server (version %d.%d) does not support foreign servers.\n",
3909                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3910                 return true;
3911         }
3912
3913         initPQExpBuffer(&buf);
3914         printfPQExpBuffer(&buf,
3915                                           "SELECT s.srvname AS \"%s\",\n"
3916                                           "  pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3917                                           "  f.fdwname AS \"%s\"",
3918                                           gettext_noop("Name"),
3919                                           gettext_noop("Owner"),
3920                                           gettext_noop("Foreign-data wrapper"));
3921
3922         if (verbose)
3923         {
3924                 appendPQExpBuffer(&buf, ",\n  ");
3925                 printACLColumn(&buf, "s.srvacl");
3926                 appendPQExpBuffer(&buf,
3927                                                   ",\n"
3928                                                   "  s.srvtype AS \"%s\",\n"
3929                                                   "  s.srvversion AS \"%s\",\n"
3930                                                   "  CASE WHEN srvoptions IS NULL THEN '' ELSE "
3931                                                   "  '(' || array_to_string(ARRAY(SELECT "
3932                                                   "  quote_ident(option_name) ||  ' ' || "
3933                                                   "  quote_literal(option_value)  FROM "
3934                                                   "  pg_options_to_table(srvoptions)),  ', ') || ')' "
3935                                                   "  END AS \"%s\",\n"
3936                                                   "  d.description AS \"%s\"",
3937                                                   gettext_noop("Type"),
3938                                                   gettext_noop("Version"),
3939                                                   gettext_noop("FDW Options"),
3940                                                   gettext_noop("Description"));
3941         }
3942
3943         appendPQExpBuffer(&buf,
3944                                           "\nFROM pg_catalog.pg_foreign_server s\n"
3945            "     JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3946
3947         if (verbose)
3948                 appendPQExpBuffer(&buf,
3949                                                   "LEFT JOIN pg_description d\n       "
3950                                                   "ON d.classoid = s.tableoid AND d.objoid = s.oid "
3951                                                   "AND d.objsubid = 0\n");
3952
3953         processSQLNamePattern(pset.db, &buf, pattern, false, false,
3954                                                   NULL, "s.srvname", NULL, NULL);
3955
3956         appendPQExpBuffer(&buf, "ORDER BY 1;");
3957
3958         res = PSQLexec(buf.data, false);
3959         termPQExpBuffer(&buf);
3960         if (!res)
3961                 return false;
3962
3963         myopt.nullPrint = NULL;
3964         myopt.title = _("List of foreign servers");
3965         myopt.translate_header = true;
3966
3967         printQuery(res, &myopt, pset.queryFout, pset.logfile);
3968
3969         PQclear(res);
3970         return true;
3971 }
3972
3973 /*
3974  * \deu
3975  *
3976  * Describes user mappings.
3977  */
3978 bool
3979 listUserMappings(const char *pattern, bool verbose)
3980 {
3981         PQExpBufferData buf;
3982         PGresult   *res;
3983         printQueryOpt myopt = pset.popt;
3984
3985         if (pset.sversion < 80400)
3986         {
3987                 psql_error("The server (version %d.%d) does not support user mappings.\n",
3988                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
3989                 return true;
3990         }
3991
3992         initPQExpBuffer(&buf);
3993         printfPQExpBuffer(&buf,
3994                                           "SELECT um.srvname AS \"%s\",\n"
3995                                           "  um.usename AS \"%s\"",
3996                                           gettext_noop("Server"),
3997                                           gettext_noop("User name"));
3998
3999         if (verbose)
4000                 appendPQExpBuffer(&buf,
4001                                                   ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4002                                                   "  '(' || array_to_string(ARRAY(SELECT "
4003                                                   "  quote_ident(option_name) ||  ' ' || "
4004                                                   "  quote_literal(option_value)  FROM "
4005                                                   "  pg_options_to_table(umoptions)),  ', ') || ')' "
4006                                                   "  END AS \"%s\"",
4007                                                   gettext_noop("FDW Options"));
4008
4009         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4010
4011         processSQLNamePattern(pset.db, &buf, pattern, false, false,
4012                                                   NULL, "um.srvname", "um.usename", NULL);
4013
4014         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4015
4016         res = PSQLexec(buf.data, false);
4017         termPQExpBuffer(&buf);
4018         if (!res)
4019                 return false;
4020
4021         myopt.nullPrint = NULL;
4022         myopt.title = _("List of user mappings");
4023         myopt.translate_header = true;
4024
4025         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4026
4027         PQclear(res);
4028         return true;
4029 }
4030
4031 /*
4032  * \det
4033  *
4034  * Describes foreign tables.
4035  */
4036 bool
4037 listForeignTables(const char *pattern, bool verbose)
4038 {
4039         PQExpBufferData buf;
4040         PGresult   *res;
4041         printQueryOpt myopt = pset.popt;
4042
4043         if (pset.sversion < 90100)
4044         {
4045                 psql_error("The server (version %d.%d) does not support foreign tables.\n",
4046                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
4047                 return true;
4048         }
4049
4050         initPQExpBuffer(&buf);
4051         printfPQExpBuffer(&buf,
4052                                           "SELECT n.nspname AS \"%s\",\n"
4053                                           "  c.relname AS \"%s\",\n"
4054                                           "  s.srvname AS \"%s\"",
4055                                           gettext_noop("Schema"),
4056                                           gettext_noop("Table"),
4057                                           gettext_noop("Server"));
4058
4059         if (verbose)
4060                 appendPQExpBuffer(&buf,
4061                                                   ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4062                                                   "  '(' || array_to_string(ARRAY(SELECT "
4063                                                   "  quote_ident(option_name) ||  ' ' || "
4064                                                   "  quote_literal(option_value)  FROM "
4065                                                   "  pg_options_to_table(ftoptions)),  ', ') || ')' "
4066                                                   "  END AS \"%s\",\n"
4067                                                   "  d.description AS \"%s\"",
4068                                                   gettext_noop("FDW Options"),
4069                                                   gettext_noop("Description"));
4070
4071         appendPQExpBuffer(&buf,
4072                                           "\nFROM pg_catalog.pg_foreign_table ft\n"
4073                                           "  INNER JOIN pg_catalog.pg_class c"
4074                                           " ON c.oid = ft.ftrelid\n"
4075                                           "  INNER JOIN pg_catalog.pg_namespace n"
4076                                           " ON n.oid = c.relnamespace\n"
4077                                           "  INNER JOIN pg_catalog.pg_foreign_server s"
4078                                           " ON s.oid = ft.ftserver\n");
4079         if (verbose)
4080                 appendPQExpBuffer(&buf,
4081                                                   "   LEFT JOIN pg_catalog.pg_description d\n"
4082                                                   "          ON d.classoid = c.tableoid AND "
4083                                                   "d.objoid = c.oid AND d.objsubid = 0\n");
4084
4085         processSQLNamePattern(pset.db, &buf, pattern, false, false,
4086                                                   NULL, "n.nspname", "c.relname", NULL);
4087
4088         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4089
4090         res = PSQLexec(buf.data, false);
4091         termPQExpBuffer(&buf);
4092         if (!res)
4093                 return false;
4094
4095         myopt.nullPrint = NULL;
4096         myopt.title = _("List of foreign tables");
4097         myopt.translate_header = true;
4098
4099         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4100
4101         PQclear(res);
4102         return true;
4103 }
4104
4105 /*
4106  * \dx
4107  *
4108  * Briefly describes installed extensions.
4109  */
4110 bool
4111 listExtensions(const char *pattern)
4112 {
4113         PQExpBufferData buf;
4114         PGresult   *res;
4115         printQueryOpt myopt = pset.popt;
4116
4117         if (pset.sversion < 90100)
4118         {
4119                 psql_error("The server (version %d.%d) does not support extensions.\n",
4120                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
4121                 return true;
4122         }
4123
4124         initPQExpBuffer(&buf);
4125         printfPQExpBuffer(&buf,
4126                                           "SELECT e.extname AS \"%s\", "
4127          "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4128                                           "FROM pg_catalog.pg_extension e "
4129                          "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4130                                  "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4131                  "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4132                                           gettext_noop("Name"),
4133                                           gettext_noop("Version"),
4134                                           gettext_noop("Schema"),
4135                                           gettext_noop("Description"));
4136
4137         processSQLNamePattern(pset.db, &buf, pattern,
4138                                                   false, false,
4139                                                   NULL, "e.extname", NULL,
4140                                                   NULL);
4141
4142         appendPQExpBuffer(&buf, "ORDER BY 1;");
4143
4144         res = PSQLexec(buf.data, false);
4145         termPQExpBuffer(&buf);
4146         if (!res)
4147                 return false;
4148
4149         myopt.nullPrint = NULL;
4150         myopt.title = _("List of installed extensions");
4151         myopt.translate_header = true;
4152
4153         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4154
4155         PQclear(res);
4156         return true;
4157 }
4158
4159 /*
4160  * \dx+
4161  *
4162  * List contents of installed extensions.
4163  */
4164 bool
4165 listExtensionContents(const char *pattern)
4166 {
4167         PQExpBufferData buf;
4168         PGresult   *res;
4169         int                     i;
4170
4171         if (pset.sversion < 90100)
4172         {
4173                 psql_error("The server (version %d.%d) does not support extensions.\n",
4174                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
4175                 return true;
4176         }
4177
4178         initPQExpBuffer(&buf);
4179         printfPQExpBuffer(&buf,
4180                                           "SELECT e.extname, e.oid\n"
4181                                           "FROM pg_catalog.pg_extension e\n");
4182
4183         processSQLNamePattern(pset.db, &buf, pattern,
4184                                                   false, false,
4185                                                   NULL, "e.extname", NULL,
4186                                                   NULL);
4187
4188         appendPQExpBuffer(&buf, "ORDER BY 1;");
4189
4190         res = PSQLexec(buf.data, false);
4191         termPQExpBuffer(&buf);
4192         if (!res)
4193                 return false;
4194
4195         if (PQntuples(res) == 0)
4196         {
4197                 if (!pset.quiet)
4198                 {
4199                         if (pattern)
4200                                 psql_error("Did not find any extension named \"%s\".\n",
4201                                                 pattern);
4202                         else
4203                                 psql_error("Did not find any extensions.\n");
4204                 }
4205                 PQclear(res);
4206                 return false;
4207         }
4208
4209         for (i = 0; i < PQntuples(res); i++)
4210         {
4211                 const char *extname;
4212                 const char *oid;
4213
4214                 extname = PQgetvalue(res, i, 0);
4215                 oid = PQgetvalue(res, i, 1);
4216
4217                 if (!listOneExtensionContents(extname, oid))
4218                 {
4219                         PQclear(res);
4220                         return false;
4221                 }
4222                 if (cancel_pressed)
4223                 {
4224                         PQclear(res);
4225                         return false;
4226                 }
4227         }
4228
4229         PQclear(res);
4230         return true;
4231 }
4232
4233 static bool
4234 listOneExtensionContents(const char *extname, const char *oid)
4235 {
4236         PQExpBufferData buf;
4237         PGresult   *res;
4238         char            title[1024];
4239         printQueryOpt myopt = pset.popt;
4240
4241         initPQExpBuffer(&buf);
4242         printfPQExpBuffer(&buf,
4243                 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4244                                           "FROM pg_catalog.pg_depend\n"
4245                                           "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4246                                           "ORDER BY 1;",
4247                                           gettext_noop("Object Description"),
4248                                           oid);
4249
4250         res = PSQLexec(buf.data, false);
4251         termPQExpBuffer(&buf);
4252         if (!res)
4253                 return false;
4254
4255         myopt.nullPrint = NULL;
4256         snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4257         myopt.title = title;
4258         myopt.translate_header = true;
4259
4260         printQuery(res, &myopt, pset.queryFout, pset.logfile);
4261
4262         PQclear(res);
4263         return true;
4264 }
4265
4266 /*
4267  * printACLColumn
4268  *
4269  * Helper function for consistently formatting ACL (privilege) columns.
4270  * The proper targetlist entry is appended to buf.      Note lack of any
4271  * whitespace or comma decoration.
4272  */
4273 static void
4274 printACLColumn(PQExpBuffer buf, const char *colname)
4275 {
4276         if (pset.sversion >= 80100)
4277                 appendPQExpBuffer(buf,
4278                                                   "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4279                                                   colname, gettext_noop("Access privileges"));
4280         else
4281                 appendPQExpBuffer(buf,
4282                                                   "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4283                                                   colname, gettext_noop("Access privileges"));
4284 }