]> granicus.if.org Git - postgresql/blob - src/bin/psql/describe.c
Don't print the name of the database in psql \z.
[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-2008, PostgreSQL Global Development Group
10  *
11  * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.175 2008/07/03 15:59:55 petere Exp $
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
41
42 /*----------------
43  * Handlers for various slash commands displaying some sort of list
44  * of things in the database.
45  *
46  * Note: try to format the queries to look nice in -E output.
47  *----------------
48  */
49
50
51 /* \da
52  * Takes an optional regexp to select particular aggregates
53  */
54 bool
55 describeAggregates(const char *pattern, bool verbose)
56 {
57         PQExpBufferData buf;
58         PGresult   *res;
59         printQueryOpt myopt = pset.popt;
60
61         initPQExpBuffer(&buf);
62
63         printfPQExpBuffer(&buf,
64                                           "SELECT n.nspname as \"%s\",\n"
65                                           "  p.proname AS \"%s\",\n"
66                                           "  pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
67                                           gettext_noop("Schema"),
68                                           gettext_noop("Name"),
69                                           gettext_noop("Result data type"));
70
71         if (pset.sversion >= 80200)
72             appendPQExpBuffer(&buf,
73                                           "  CASE WHEN p.pronargs = 0\n"
74                                           "    THEN CAST('*' AS pg_catalog.text)\n"
75                                           "    ELSE\n"
76                                           "    pg_catalog.array_to_string(ARRAY(\n"
77                                           "      SELECT\n"
78                                  "        pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
79                                           "      FROM\n"
80                                           "        pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
81                                           "    ), ', ')\n"
82                                           "  END AS \"%s\",\n",
83                                           gettext_noop("Argument data types"));
84         else
85             appendPQExpBuffer(&buf,
86                                           "  pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
87                                           gettext_noop("Argument data types"));
88
89         appendPQExpBuffer(&buf,
90                                  "  pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
91                                           "FROM pg_catalog.pg_proc p\n"
92            "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
93                                           "WHERE p.proisagg\n",
94                                           gettext_noop("Description"));
95
96         processSQLNamePattern(pset.db, &buf, pattern, true, false,
97                                                   "n.nspname", "p.proname", NULL,
98                                                   "pg_catalog.pg_function_is_visible(p.oid)");
99
100         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
101
102         res = PSQLexec(buf.data, false);
103         termPQExpBuffer(&buf);
104         if (!res)
105                 return false;
106
107         myopt.nullPrint = NULL;
108         myopt.title = _("List of aggregate functions");
109         myopt.trans_headers = true;
110
111         printQuery(res, &myopt, pset.queryFout, pset.logfile);
112
113         PQclear(res);
114         return true;
115 }
116
117 /* \db
118  * Takes an optional regexp to select particular tablespaces
119  */
120 bool
121 describeTablespaces(const char *pattern, bool verbose)
122 {
123         PQExpBufferData buf;
124         PGresult   *res;
125         printQueryOpt myopt = pset.popt;
126
127         if (pset.sversion < 80000)
128         {
129                 fprintf(stderr, _("The server (version %d.%d) does not support tablespaces.\n"),
130                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
131                 return true;
132         }
133
134         initPQExpBuffer(&buf);
135
136         printfPQExpBuffer(&buf,
137                                           "SELECT spcname AS \"%s\",\n"
138                                           "  pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
139                                           "  spclocation AS \"%s\"",
140                                           gettext_noop("Name"),
141                                           gettext_noop("Owner"),
142                                           gettext_noop("Location"));
143
144         if (verbose)
145                 appendPQExpBuffer(&buf,
146                                                   ",\n  spcacl AS \"%s\"",
147                                                   gettext_noop("Access privileges"));
148         if (verbose && pset.sversion >= 80200)
149                 appendPQExpBuffer(&buf,
150                           ",\n  pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
151                                                   gettext_noop("Description"));
152
153         appendPQExpBuffer(&buf,
154                                           "\nFROM pg_catalog.pg_tablespace\n");
155
156         processSQLNamePattern(pset.db, &buf, pattern, false, false,
157                                                   NULL, "spcname", NULL,
158                                                   NULL);
159
160         appendPQExpBuffer(&buf, "ORDER BY 1;");
161
162         res = PSQLexec(buf.data, false);
163         termPQExpBuffer(&buf);
164         if (!res)
165                 return false;
166
167         myopt.nullPrint = NULL;
168         myopt.title = _("List of tablespaces");
169         myopt.trans_headers = true;
170
171         printQuery(res, &myopt, pset.queryFout, pset.logfile);
172
173         PQclear(res);
174         return true;
175 }
176
177
178 /* \df
179  * Takes an optional regexp to select particular functions
180  */
181 bool
182 describeFunctions(const char *pattern, bool verbose)
183 {
184         PQExpBufferData buf;
185         PGresult   *res;
186         printQueryOpt myopt = pset.popt;
187
188         initPQExpBuffer(&buf);
189
190         printfPQExpBuffer(&buf,
191                                           "SELECT n.nspname as \"%s\",\n"
192                                           "  p.proname as \"%s\",\n"
193                                           "  CASE WHEN p.proretset THEN 'setof ' ELSE '' END ||\n"
194                                   "  pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n",
195                                           gettext_noop("Schema"),
196                                           gettext_noop("Name"),
197                                           gettext_noop("Result data type"));
198
199     if (pset.sversion >= 80100)
200                 appendPQExpBuffer(&buf,
201                                           "  CASE WHEN proallargtypes IS NOT NULL THEN\n"
202                                           "    pg_catalog.array_to_string(ARRAY(\n"
203                                           "      SELECT\n"
204                                           "        CASE\n"
205                                           "          WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
206                                           "          WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
207                                         "          WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
208                                           "        END ||\n"
209                                           "        CASE\n"
210                          "          WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
211                                           "          ELSE p.proargnames[s.i] || ' ' \n"
212                                           "        END ||\n"
213                           "        pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
214                                           "      FROM\n"
215                                           "        pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
216                                           "    ), ', ')\n"
217                                           "  ELSE\n"
218                                           "    pg_catalog.array_to_string(ARRAY(\n"
219                                           "      SELECT\n"
220                                           "        CASE\n"
221                    "          WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
222                                           "          ELSE p.proargnames[s.i+1] || ' '\n"
223                                           "          END ||\n"
224                                  "        pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
225                                           "      FROM\n"
226                                           "        pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
227                                           "    ), ', ')\n"
228                                           "  END AS \"%s\"",
229                                           gettext_noop("Argument data types"));
230         else
231                 appendPQExpBuffer(&buf,
232                                           "  pg_catalog.oidvectortypes(p.proargtypes) as \"%s\"",
233                                                   gettext_noop("Argument data types"));
234
235         if (verbose)
236                 appendPQExpBuffer(&buf,
237                                                   ",\n CASE\n"
238                                                   "  WHEN p.provolatile = 'i' THEN 'immutable'\n"
239                                                   "  WHEN p.provolatile = 's' THEN 'stable'\n"
240                                                   "  WHEN p.provolatile = 'v' THEN 'volatile'\n"
241                                                   "END as \"%s\""
242                                                   ",\n  pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
243                                                   "  l.lanname as \"%s\",\n"
244                                                   "  p.prosrc as \"%s\",\n"
245                                   "  pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
246                                                   gettext_noop("Volatility"),
247                                                   gettext_noop("Owner"),
248                                                   gettext_noop("Language"),
249                                                   gettext_noop("Source code"),
250                                                   gettext_noop("Description"));
251
252         appendPQExpBuffer(&buf,
253                                           "\nFROM pg_catalog.pg_proc p"
254                                           "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
255
256         if (verbose)
257                 appendPQExpBuffer(&buf,
258                                                   "     LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
259
260         /*
261          * we skip in/out funcs by excluding functions that take or return cstring
262          */
263         appendPQExpBuffer(&buf,
264                    "WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
265                                           "      AND p.proargtypes[0] IS DISTINCT FROM 'pg_catalog.cstring'::pg_catalog.regtype\n"
266                                           "      AND NOT p.proisagg\n");
267
268         processSQLNamePattern(pset.db, &buf, pattern, true, false,
269                                                   "n.nspname", "p.proname", NULL,
270                                                   "pg_catalog.pg_function_is_visible(p.oid)");
271
272         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
273
274         res = PSQLexec(buf.data, false);
275         termPQExpBuffer(&buf);
276         if (!res)
277                 return false;
278
279         myopt.nullPrint = NULL;
280         myopt.title = _("List of functions");
281         myopt.trans_headers = true;
282
283         printQuery(res, &myopt, pset.queryFout, pset.logfile);
284
285         PQclear(res);
286         return true;
287 }
288
289
290
291 /*
292  * \dT
293  * describe types
294  */
295 bool
296 describeTypes(const char *pattern, bool verbose)
297 {
298         PQExpBufferData buf;
299         PGresult   *res;
300         printQueryOpt myopt = pset.popt;
301
302         initPQExpBuffer(&buf);
303
304         printfPQExpBuffer(&buf,
305                                           "SELECT n.nspname as \"%s\",\n"
306                                           "  pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
307                                           gettext_noop("Schema"),
308                                           gettext_noop("Name"));
309         if (verbose)
310                 appendPQExpBuffer(&buf,
311                                                   "  t.typname AS \"%s\",\n"
312                                                   "  CASE WHEN t.typrelid != 0\n"
313                                                   "      THEN CAST('tuple' AS pg_catalog.text)\n"
314                                                   "    WHEN t.typlen < 0\n"
315                                                   "      THEN CAST('var' AS pg_catalog.text)\n"
316                                                   "    ELSE CAST(t.typlen AS pg_catalog.text)\n"
317                                                   "  END AS \"%s\",\n",
318                                                   gettext_noop("Internal name"),
319                                                   gettext_noop("Size"));
320         if (verbose && pset.sversion >= 80300)
321                 appendPQExpBuffer(&buf,
322                                                   "  pg_catalog.array_to_string(\n"
323                                                   "      ARRAY(\n"
324                                                   "                  SELECT e.enumlabel\n"
325                                                   "          FROM pg_catalog.pg_enum e\n"
326                                                   "          WHERE e.enumtypid = t.oid\n"
327                                                   "          ORDER BY e.oid\n"
328                                                   "      ),\n"
329                                                   "      E'\\n'\n"
330                                                   "  ) AS \"%s\",\n",
331                                                   gettext_noop("Elements"));
332
333         appendPQExpBuffer(&buf,
334                                           "  pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
335                                           gettext_noop("Description"));
336
337         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
338          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
339
340         /*
341          * do not include complex types (typrelid!=0) unless they are standalone
342          * composite types
343          */
344         appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
345         appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
346                                           "WHERE c.oid = t.typrelid))\n");
347         /*
348          * do not include array types (before 8.3 we have to use the assumption
349          * that their names start with underscore)
350          */
351         if (pset.sversion >= 80300)
352                 appendPQExpBuffer(&buf, "  AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
353         else
354                 appendPQExpBuffer(&buf, "  AND t.typname !~ '^_'\n");
355
356         /* Match name pattern against either internal or external name */
357         processSQLNamePattern(pset.db, &buf, pattern, true, false,
358                                                   "n.nspname", "t.typname",
359                                                   "pg_catalog.format_type(t.oid, NULL)",
360                                                   "pg_catalog.pg_type_is_visible(t.oid)");
361
362         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
363
364         res = PSQLexec(buf.data, false);
365         termPQExpBuffer(&buf);
366         if (!res)
367                 return false;
368
369         myopt.nullPrint = NULL;
370         myopt.title = _("List of data types");
371         myopt.trans_headers = true;
372
373         printQuery(res, &myopt, pset.queryFout, pset.logfile);
374
375         PQclear(res);
376         return true;
377 }
378
379
380 /* \do
381  */
382 bool
383 describeOperators(const char *pattern)
384 {
385         PQExpBufferData buf;
386         PGresult   *res;
387         printQueryOpt myopt = pset.popt;
388
389         initPQExpBuffer(&buf);
390
391         printfPQExpBuffer(&buf,
392                                           "SELECT n.nspname as \"%s\",\n"
393                                           "  o.oprname AS \"%s\",\n"
394                                           "  CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
395                                           "  CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
396                                    "  pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
397                          "  coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
398         "           pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
399                                           "FROM pg_catalog.pg_operator o\n"
400           "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
401                                           gettext_noop("Schema"),
402                                           gettext_noop("Name"),
403                                           gettext_noop("Left arg type"),
404                                           gettext_noop("Right arg type"),
405                                           gettext_noop("Result type"),
406                                           gettext_noop("Description"));
407
408         processSQLNamePattern(pset.db, &buf, pattern, false, true,
409                                                   "n.nspname", "o.oprname", NULL,
410                                                   "pg_catalog.pg_operator_is_visible(o.oid)");
411
412         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
413
414         res = PSQLexec(buf.data, false);
415         termPQExpBuffer(&buf);
416         if (!res)
417                 return false;
418
419         myopt.nullPrint = NULL;
420         myopt.title = _("List of operators");
421         myopt.trans_headers = true;
422
423         printQuery(res, &myopt, pset.queryFout, pset.logfile);
424
425         PQclear(res);
426         return true;
427 }
428
429
430 /*
431  * listAllDbs
432  *
433  * for \l, \list, and -l switch
434  */
435 bool
436 listAllDbs(bool verbose)
437 {
438         PGresult   *res;
439         PQExpBufferData buf;
440         printQueryOpt myopt = pset.popt;
441
442         initPQExpBuffer(&buf);
443
444         printfPQExpBuffer(&buf,
445                                           "SELECT d.datname as \"%s\",\n"
446                                           "       pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
447                                           "       pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n"
448                                           "       d.datacl as \"%s\"",
449                                           gettext_noop("Name"),
450                                           gettext_noop("Owner"),
451                                           gettext_noop("Encoding"),
452                                           gettext_noop("Access Privileges"));
453         if (verbose && pset.sversion >= 80200)
454                 appendPQExpBuffer(&buf,
455                                                   ",\n       CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
456                                                   "            THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
457                                                   "            ELSE 'No Access'\n"
458                                                   "       END as \"%s\"",
459                                                   gettext_noop("Size"));
460         if (verbose && pset.sversion >= 80000)
461                 appendPQExpBuffer(&buf,
462                                                   ",\n       t.spcname as \"%s\"",
463                                                   gettext_noop("Tablespace"));
464         if (verbose && pset.sversion >= 80200)
465                     appendPQExpBuffer(&buf,
466                                                   ",\n       pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
467                                                   gettext_noop("Description"));
468         appendPQExpBuffer(&buf,
469                                           "\nFROM pg_catalog.pg_database d\n");
470         if (verbose && pset.sversion >= 80000)
471                 appendPQExpBuffer(&buf,
472                    "  JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
473         appendPQExpBuffer(&buf, "ORDER BY 1;");
474         res = PSQLexec(buf.data, false);
475         termPQExpBuffer(&buf);
476         if (!res)
477                 return false;
478
479         myopt.nullPrint = NULL;
480         myopt.title = _("List of databases");
481         myopt.trans_headers = true;
482
483         printQuery(res, &myopt, pset.queryFout, pset.logfile);
484
485         PQclear(res);
486         return true;
487 }
488
489
490 /*
491  * List Tables Grant/Revoke Permissions
492  * \z (now also \dp -- perhaps more mnemonic)
493  */
494 bool
495 permissionsList(const char *pattern)
496 {
497         PQExpBufferData buf;
498         PGresult   *res;
499         printQueryOpt myopt = pset.popt;
500         static const bool trans_columns[] = {false, false, true, false};
501
502         initPQExpBuffer(&buf);
503
504         /*
505          * we ignore indexes and toast tables since they have no meaningful rights
506          */
507         printfPQExpBuffer(&buf,
508                                           "SELECT n.nspname as \"%s\",\n"
509                                           "  c.relname as \"%s\",\n"
510                                           "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' END as \"%s\",\n",
511                                           gettext_noop("Schema"),
512                                           gettext_noop("Name"),
513                                           gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
514                                           gettext_noop("Type"));
515
516     if (pset.sversion >= 80100)
517             appendPQExpBuffer(&buf, "  pg_catalog.array_to_string(c.relacl, E'\\n') as \"%s\"\n",
518                                           gettext_noop("Access privileges"));
519     else
520             appendPQExpBuffer(&buf, "  pg_catalog.array_to_string(c.relacl, '\\n') as \"%s\"\n",
521                                           gettext_noop("Access privileges"));
522
523         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_class c\n"
524            "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
525                                           "WHERE c.relkind IN ('r', 'v', 'S')\n");
526
527         /*
528          * Unless a schema pattern is specified, we suppress system and temp
529          * tables, since they normally aren't very interesting from a permissions
530          * point of view.  You can see 'em by explicit request though, eg with \z
531          * pg_catalog.*
532          */
533         processSQLNamePattern(pset.db, &buf, pattern, true, false,
534                                                   "n.nspname", "c.relname", NULL,
535                         "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
536
537         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
538
539         res = PSQLexec(buf.data, false);
540         if (!res)
541         {
542                 termPQExpBuffer(&buf);
543                 return false;
544         }
545
546         myopt.nullPrint = NULL;
547         printfPQExpBuffer(&buf, _("Access privileges"));
548         myopt.title = buf.data;
549         myopt.trans_headers = true;
550         myopt.trans_columns = trans_columns;
551
552         printQuery(res, &myopt, pset.queryFout, pset.logfile);
553
554         termPQExpBuffer(&buf);
555         PQclear(res);
556         return true;
557 }
558
559
560
561 /*
562  * Get object comments
563  *
564  * \dd [foo]
565  *
566  * Note: This only lists things that actually have a description. For complete
567  * lists of things, there are other \d? commands.
568  */
569 bool
570 objectDescription(const char *pattern)
571 {
572         PQExpBufferData buf;
573         PGresult   *res;
574         printQueryOpt myopt = pset.popt;
575         static const bool trans_columns[] = {false, false, true, false};
576
577         initPQExpBuffer(&buf);
578
579         appendPQExpBuffer(&buf,
580                                           "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
581                                           "FROM (\n",
582                                           gettext_noop("Schema"),
583                                           gettext_noop("Name"),
584                                           gettext_noop("Object"),
585                                           gettext_noop("Description"));
586
587         /* Aggregate descriptions */
588         appendPQExpBuffer(&buf,
589                                           "  SELECT p.oid as oid, p.tableoid as tableoid,\n"
590                                           "  n.nspname as nspname,\n"
591                                           "  CAST(p.proname AS pg_catalog.text) as name,"
592                                           "  CAST('%s' AS pg_catalog.text) as object\n"
593                                           "  FROM pg_catalog.pg_proc p\n"
594          "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
595                                           "  WHERE p.proisagg\n",
596                                           gettext_noop("aggregate"));
597         processSQLNamePattern(pset.db, &buf, pattern, true, false,
598                                                   "n.nspname", "p.proname", NULL,
599                                                   "pg_catalog.pg_function_is_visible(p.oid)");
600
601         /* Function descriptions (except in/outs for datatypes) */
602         appendPQExpBuffer(&buf,
603                                           "UNION ALL\n"
604                                           "  SELECT p.oid as oid, p.tableoid as tableoid,\n"
605                                           "  n.nspname as nspname,\n"
606                                           "  CAST(p.proname AS pg_catalog.text) as name,"
607                                           "  CAST('%s' AS pg_catalog.text) as object\n"
608                                           "  FROM pg_catalog.pg_proc p\n"
609          "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
610
611                  "  WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
612                                           "      AND (p.proargtypes[0] IS NULL\n"
613                                           "      OR   p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)\n"
614                                           "      AND NOT p.proisagg\n",
615                                           gettext_noop("function"));
616         processSQLNamePattern(pset.db, &buf, pattern, true, false,
617                                                   "n.nspname", "p.proname", NULL,
618                                                   "pg_catalog.pg_function_is_visible(p.oid)");
619
620         /* Operator descriptions (only if operator has its own comment) */
621         appendPQExpBuffer(&buf,
622                                           "UNION ALL\n"
623                                           "  SELECT o.oid as oid, o.tableoid as tableoid,\n"
624                                           "  n.nspname as nspname,\n"
625                                           "  CAST(o.oprname AS pg_catalog.text) as name,"
626                                           "  CAST('%s' AS pg_catalog.text) as object\n"
627                                           "  FROM pg_catalog.pg_operator o\n"
628         "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
629                                           gettext_noop("operator"));
630         processSQLNamePattern(pset.db, &buf, pattern, false, false,
631                                                   "n.nspname", "o.oprname", NULL,
632                                                   "pg_catalog.pg_operator_is_visible(o.oid)");
633
634         /* Type description */
635         appendPQExpBuffer(&buf,
636                                           "UNION ALL\n"
637                                           "  SELECT t.oid as oid, t.tableoid as tableoid,\n"
638                                           "  n.nspname as nspname,\n"
639                                           "  pg_catalog.format_type(t.oid, NULL) as name,"
640                                           "  CAST('%s' AS pg_catalog.text) as object\n"
641                                           "  FROM pg_catalog.pg_type t\n"
642         "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n",
643                                           gettext_noop("data type"));
644         processSQLNamePattern(pset.db, &buf, pattern, false, false,
645                                                   "n.nspname", "pg_catalog.format_type(t.oid, NULL)",
646                                                   NULL,
647                                                   "pg_catalog.pg_type_is_visible(t.oid)");
648
649         /* Relation (tables, views, indexes, sequences) descriptions */
650         appendPQExpBuffer(&buf,
651                                           "UNION ALL\n"
652                                           "  SELECT c.oid as oid, c.tableoid as tableoid,\n"
653                                           "  n.nspname as nspname,\n"
654                                           "  CAST(c.relname AS pg_catalog.text) as name,\n"
655                                           "  CAST(\n"
656                                           "    CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' END"
657                                           "  AS pg_catalog.text) as object\n"
658                                           "  FROM pg_catalog.pg_class c\n"
659          "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
660                                           "  WHERE c.relkind IN ('r', 'v', 'i', 'S')\n",
661                                           gettext_noop("table"),
662                                           gettext_noop("view"),
663                                           gettext_noop("index"),
664                                           gettext_noop("sequence"));
665         processSQLNamePattern(pset.db, &buf, pattern, true, false,
666                                                   "n.nspname", "c.relname", NULL,
667                                                   "pg_catalog.pg_table_is_visible(c.oid)");
668
669         /* Rule description (ignore rules for views) */
670         appendPQExpBuffer(&buf,
671                                           "UNION ALL\n"
672                                           "  SELECT r.oid as oid, r.tableoid as tableoid,\n"
673                                           "  n.nspname as nspname,\n"
674                                           "  CAST(r.rulename AS pg_catalog.text) as name,"
675                                           "  CAST('%s' AS pg_catalog.text) as object\n"
676                                           "  FROM pg_catalog.pg_rewrite r\n"
677                                   "       JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
678          "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
679                                           "  WHERE r.rulename != '_RETURN'\n",
680                                           gettext_noop("rule"));
681         /* XXX not sure what to do about visibility rule here? */
682         processSQLNamePattern(pset.db, &buf, pattern, true, false,
683                                                   "n.nspname", "r.rulename", NULL,
684                                                   "pg_catalog.pg_table_is_visible(c.oid)");
685
686         /* Trigger description */
687         appendPQExpBuffer(&buf,
688                                           "UNION ALL\n"
689                                           "  SELECT t.oid as oid, t.tableoid as tableoid,\n"
690                                           "  n.nspname as nspname,\n"
691                                           "  CAST(t.tgname AS pg_catalog.text) as name,"
692                                           "  CAST('%s' AS pg_catalog.text) as object\n"
693                                           "  FROM pg_catalog.pg_trigger t\n"
694                                    "       JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
695         "       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
696                                           gettext_noop("trigger"));
697         /* XXX not sure what to do about visibility rule here? */
698         processSQLNamePattern(pset.db, &buf, pattern, false, false,
699                                                   "n.nspname", "t.tgname", NULL,
700                                                   "pg_catalog.pg_table_is_visible(c.oid)");
701
702         appendPQExpBuffer(&buf,
703                                           ") AS tt\n"
704                                           "  JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
705
706         appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
707
708         res = PSQLexec(buf.data, false);
709         termPQExpBuffer(&buf);
710         if (!res)
711                 return false;
712
713         myopt.nullPrint = NULL;
714         myopt.title = _("Object descriptions");
715         myopt.trans_headers = true;
716         myopt.trans_columns = trans_columns;
717
718         printQuery(res, &myopt, pset.queryFout, pset.logfile);
719
720         PQclear(res);
721         return true;
722 }
723
724
725 /*
726  * describeTableDetails (for \d)
727  *
728  * This routine finds the tables to be displayed, and calls
729  * describeOneTableDetails for each one.
730  *
731  * verbose: if true, this is \d+
732  */
733 bool
734 describeTableDetails(const char *pattern, bool verbose)
735 {
736         PQExpBufferData buf;
737         PGresult   *res;
738         int                     i;
739
740         initPQExpBuffer(&buf);
741
742         printfPQExpBuffer(&buf,
743                                           "SELECT c.oid,\n"
744                                           "  n.nspname,\n"
745                                           "  c.relname\n"
746                                           "FROM pg_catalog.pg_class c\n"
747          "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
748
749         processSQLNamePattern(pset.db, &buf, pattern, false, false,
750                                                   "n.nspname", "c.relname", NULL,
751                                                   "pg_catalog.pg_table_is_visible(c.oid)");
752
753         appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
754
755         res = PSQLexec(buf.data, false);
756         termPQExpBuffer(&buf);
757         if (!res)
758                 return false;
759
760         if (PQntuples(res) == 0)
761         {
762                 if (!pset.quiet)
763                         fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
764                                         pattern);
765                 PQclear(res);
766                 return false;
767         }
768
769         for (i = 0; i < PQntuples(res); i++)
770         {
771                 const char *oid;
772                 const char *nspname;
773                 const char *relname;
774
775                 oid = PQgetvalue(res, i, 0);
776                 nspname = PQgetvalue(res, i, 1);
777                 relname = PQgetvalue(res, i, 2);
778
779                 if (!describeOneTableDetails(nspname, relname, oid, verbose))
780                 {
781                         PQclear(res);
782                         return false;
783                 }
784                 if (cancel_pressed)
785                 {
786                         PQclear(res);
787                         return false;
788                 }
789         }
790
791         PQclear(res);
792         return true;
793 }
794
795 /*
796  * describeOneTableDetails (for \d)
797  *
798  * Unfortunately, the information presented here is so complicated that it
799  * cannot be done in a single query. So we have to assemble the printed table
800  * by hand and pass it to the underlying printTable() function.
801  */
802 static bool
803 describeOneTableDetails(const char *schemaname,
804                                                 const char *relationname,
805                                                 const char *oid,
806                                                 bool verbose)
807 {
808         PQExpBufferData buf;
809         PGresult   *res = NULL;
810         printTableOpt myopt = pset.popt.topt;
811         printTableContent cont;
812         int                     i;
813         char       *view_def = NULL;
814         char       *headers[4];
815         char      **modifiers = NULL;
816         char      **ptr;
817         PQExpBufferData title;
818         PQExpBufferData tmpbuf;
819         int                     cols = 0;
820         int                     numrows = 0;
821         struct
822         {
823                 int16           checks;
824                 int16           triggers;
825                 char            relkind;
826                 bool            hasindex;
827                 bool            hasrules;
828                 bool            hasoids;
829                 Oid                     tablespace;
830         }                       tableinfo;
831         bool            show_modifiers = false;
832         bool            retval;
833
834         retval = false;
835
836         /* This output looks confusing in expanded mode. */
837         myopt.expanded = false;
838
839         initPQExpBuffer(&buf);
840         initPQExpBuffer(&title);
841         initPQExpBuffer(&tmpbuf);
842
843         /* Get general table info */
844         printfPQExpBuffer(&buf,
845            "SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules, "
846                                           "relhasoids%s\n"
847                                           "FROM pg_catalog.pg_class WHERE oid = '%s'",
848                                           (pset.sversion >= 80000 ? ", reltablespace" : ""),
849                                           oid);
850         res = PSQLexec(buf.data, false);
851         if (!res)
852                 goto error_return;
853
854         /* Did we get anything? */
855         if (PQntuples(res) == 0)
856         {
857                 if (!pset.quiet)
858                         fprintf(stderr, _("Did not find any relation with OID %s.\n"),
859                                         oid);
860                 goto error_return;
861         }
862
863         tableinfo.checks = atoi(PQgetvalue(res, 0, 2));
864         tableinfo.triggers = atoi(PQgetvalue(res, 0, 3));
865         tableinfo.relkind = *(PQgetvalue(res, 0, 1));
866         tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 0), "t") == 0;
867         tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
868         tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
869         tableinfo.tablespace = (pset.sversion >= 80000) ?
870                 atooid(PQgetvalue(res, 0, 6)) : 0;
871         PQclear(res);
872
873         /* Get column info (index requires additional checks) */
874         printfPQExpBuffer(&buf, "SELECT a.attname,");
875         appendPQExpBuffer(&buf, "\n  pg_catalog.format_type(a.atttypid, a.atttypmod),"
876                                           "\n  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
877                                           "\n   FROM pg_catalog.pg_attrdef d"
878                                           "\n   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
879                                           "\n  a.attnotnull, a.attnum");
880         if (verbose)
881                 appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
882         appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
883         if (tableinfo.relkind == 'i')
884                 appendPQExpBuffer(&buf, ", pg_catalog.pg_index i");
885         appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
886         if (tableinfo.relkind == 'i')
887                 appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid");
888         appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
889
890         res = PSQLexec(buf.data, false);
891         if (!res)
892                 goto error_return;
893         numrows = PQntuples(res);
894
895         /* Make title */
896         switch (tableinfo.relkind)
897         {
898                 case 'r':
899                         printfPQExpBuffer(&title, _("Table \"%s.%s\""),
900                                                           schemaname, relationname);
901                         break;
902                 case 'v':
903                         printfPQExpBuffer(&title, _("View \"%s.%s\""),
904                                                           schemaname, relationname);
905                         break;
906                 case 'S':
907                         printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
908                                                           schemaname, relationname);
909                         break;
910                 case 'i':
911                         printfPQExpBuffer(&title, _("Index \"%s.%s\""),
912                                                           schemaname, relationname);
913                         break;
914                 case 's':
915                         /* not used as of 8.2, but keep it for backwards compatibility */
916                         printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
917                                                           schemaname, relationname);
918                         break;
919                 case 't':
920                         printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
921                                                           schemaname, relationname);
922                         break;
923                 case 'c':
924                         printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
925                                                           schemaname, relationname);
926                         break;
927                 default:
928                         /* untranslated unknown relkind */
929                         printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
930                                                           tableinfo.relkind, schemaname, relationname);
931                         break;
932         }
933
934         /* Set the number of columns, and their names */
935         cols = 2;
936         headers[0] = "Column";
937         headers[1] = "Type";
938
939         if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v')
940         {
941                 show_modifiers = true;
942                 headers[cols++] = "Modifiers";
943                 modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
944         }
945
946         if (verbose)
947                 headers[cols++] = "Description";
948
949         printTableInit(&cont, &myopt, title.data, cols, numrows);
950
951         for (i = 0; i < cols; i++)
952                 printTableAddHeader(&cont, headers[i], true, 'l');
953
954         /* Check if table is a view */
955         if (tableinfo.relkind == 'v')
956         {
957                 PGresult   *result;
958
959                 printfPQExpBuffer(&buf,
960                                                   "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
961                                                   oid);
962                 result = PSQLexec(buf.data, false);
963                 if (!result)
964                         goto error_return;
965
966                 if (PQntuples(result) > 0)
967                         view_def = pg_strdup(PQgetvalue(result, 0, 0));
968
969                 PQclear(result);
970         }
971
972         /* Generate table cells to be printed */
973         for (i = 0; i < numrows; i++)
974         {
975                 /* Column */
976                 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
977
978                 /* Type */
979                 printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
980
981                 /* Extra: not null and default */
982                 if (show_modifiers)
983                 {
984                         resetPQExpBuffer(&tmpbuf);
985                         if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
986                                 appendPQExpBufferStr(&tmpbuf, _("not null"));
987
988                         /* handle "default" here */
989                         /* (note: above we cut off the 'default' string at 128) */
990                         if (strlen(PQgetvalue(res, i, 2)) != 0)
991                         {
992                                 if (tmpbuf.len > 0)
993                                         appendPQExpBufferStr(&tmpbuf, " ");
994                                 /* translator: default values of column definitions */
995                                 appendPQExpBuffer(&tmpbuf, _("default %s"),
996                                                                   PQgetvalue(res, i, 2));
997                         }
998
999                         modifiers[i] = pg_strdup(tmpbuf.data);
1000                         printTableAddCell(&cont, modifiers[i], false);
1001                 }
1002
1003                 /* Description */
1004                 if (verbose)
1005                         printTableAddCell(&cont, PQgetvalue(res, i, 5), false);
1006         }
1007
1008         /* Make footers */
1009         if (tableinfo.relkind == 'i')
1010         {
1011                 /* Footer information about an index */
1012                 PGresult   *result;
1013
1014                 printfPQExpBuffer(&buf,
1015                                                   "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1016                 if (pset.sversion >= 80200)
1017                         appendPQExpBuffer(&buf, "i.indisvalid, ");
1018                 else
1019                         appendPQExpBuffer(&buf, "true as indisvalid, ");
1020                 appendPQExpBuffer(&buf, "a.amname, c2.relname,\n"
1021                                         "  pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1022                                                   "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1023                   "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1024                                                   "AND i.indrelid = c2.oid",
1025                                                   oid);
1026
1027                 result = PSQLexec(buf.data, false);
1028                 if (!result)
1029                         goto error_return;
1030                 else if (PQntuples(result) != 1)
1031                 {
1032                         PQclear(result);
1033                         goto error_return;
1034                 }
1035                 else
1036                 {
1037                         char       *indisunique = PQgetvalue(result, 0, 0);
1038                         char       *indisprimary = PQgetvalue(result, 0, 1);
1039                         char       *indisclustered = PQgetvalue(result, 0, 2);
1040                         char       *indisvalid = PQgetvalue(result, 0, 3);
1041                         char       *indamname = PQgetvalue(result, 0, 4);
1042                         char       *indtable = PQgetvalue(result, 0, 5);
1043                         char       *indpred = PQgetvalue(result, 0, 6);
1044
1045                         if (strcmp(indisprimary, "t") == 0)
1046                                 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1047                         else if (strcmp(indisunique, "t") == 0)
1048                                 printfPQExpBuffer(&tmpbuf, _("unique, "));
1049                         else
1050                                 resetPQExpBuffer(&tmpbuf);
1051                         appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1052
1053                         /* we assume here that index and table are in same schema */
1054                         appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1055                                                           schemaname, indtable);
1056
1057                         if (strlen(indpred))
1058                                 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1059
1060                         if (strcmp(indisclustered, "t") == 0)
1061                                 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1062
1063                         if (strcmp(indisvalid, "t") != 0)
1064                                 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1065
1066                         printTableAddFooter(&cont, tmpbuf.data);
1067                         add_tablespace_footer(&cont, tableinfo.relkind,
1068                                                                   tableinfo.tablespace, true);
1069                 }
1070
1071                 PQclear(result);
1072         }
1073         else if (view_def)
1074         {
1075                 PGresult   *result = NULL;
1076
1077                 /* Footer information about a view */
1078                 printTableAddFooter(&cont, _("View definition:"));
1079                 printTableAddFooter(&cont, view_def);
1080
1081                 /* print rules */
1082                 if (tableinfo.hasrules)
1083                 {
1084                         printfPQExpBuffer(&buf,
1085                                                           "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1086                                                           "FROM pg_catalog.pg_rewrite r\n"
1087                         "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1",
1088                                                           oid);
1089                         result = PSQLexec(buf.data, false);
1090                         if (!result)
1091                                 goto error_return;
1092
1093                         if (PQntuples(result) > 0)
1094                         {
1095                                 printTableAddFooter(&cont, _("Rules:"));
1096                                 for (i = 0; i < PQntuples(result); i++)
1097                                 {
1098                                         const char *ruledef;
1099
1100                                         /* Everything after "CREATE RULE" is echoed verbatim */
1101                                         ruledef = PQgetvalue(result, i, 1);
1102                                         ruledef += 12;
1103
1104                                         printfPQExpBuffer(&buf, " %s", ruledef);
1105                                         printTableAddFooter(&cont, buf.data);
1106                                 }
1107                         }
1108                         PQclear(result);
1109                 }
1110         }
1111         else if (tableinfo.relkind == 'r')
1112         {
1113                 /* Footer information about a table */
1114                 PGresult   *result = NULL;
1115                 int                     tuples = 0;
1116
1117                 /* print indexes */
1118                 if (tableinfo.hasindex)
1119                 {
1120                         printfPQExpBuffer(&buf,
1121                                                           "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1122             if (pset.sversion >= 80200)
1123                                 appendPQExpBuffer(&buf, "i.indisvalid, ");
1124             else
1125                                 appendPQExpBuffer(&buf, "true as indisvalid, ");
1126                         appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)");
1127             if (pset.sversion >= 80000)
1128                                 appendPQExpBuffer(&buf, ", c2.reltablespace");
1129                         appendPQExpBuffer(&buf,
1130                                                           "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
1131                                                           "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1132                           "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
1133                                                           oid);
1134                         result = PSQLexec(buf.data, false);
1135                         if (!result)
1136                                 goto error_return;
1137                         else
1138                                 tuples = PQntuples(result);
1139
1140                         if (tuples > 0)
1141                         {
1142                                 printTableAddFooter(&cont, _("Indexes:"));
1143                                 for (i = 0; i < tuples; i++)
1144                                 {
1145                                         const char *indexdef;
1146                                         const char *usingpos;
1147
1148                                         /* untranslated index name */
1149                                         printfPQExpBuffer(&buf, "    \"%s\"",
1150                                                                           PQgetvalue(result, i, 0));
1151
1152                                         /* Label as primary key or unique (but not both) */
1153                                         appendPQExpBuffer(&buf,
1154                                                                           strcmp(PQgetvalue(result, i, 1), "t") == 0
1155                                                                           ? " PRIMARY KEY," :
1156                                                                           (strcmp(PQgetvalue(result, i, 2), "t") == 0
1157                                                                            ? " UNIQUE,"
1158                                                                            : ""));
1159                                         /* Everything after "USING" is echoed verbatim */
1160                                         indexdef = PQgetvalue(result, i, 5);
1161                                         usingpos = strstr(indexdef, " USING ");
1162                                         if (usingpos)
1163                                                 indexdef = usingpos + 7;
1164
1165                                         appendPQExpBuffer(&buf, " %s", indexdef);
1166
1167                                         if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1168                                                 appendPQExpBuffer(&buf, " CLUSTER");
1169
1170                                         if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1171                                                 appendPQExpBuffer(&buf, " INVALID");
1172
1173                                         printTableAddFooter(&cont, buf.data);
1174
1175                                         /* Print tablespace of the index on the same line */
1176                                         if (pset.sversion >= 80000)
1177                                             add_tablespace_footer(&cont, 'i',
1178                                                                                           atooid(PQgetvalue(result, i, 6)),
1179                                                                                           false);
1180                                 }
1181                         }
1182                         PQclear(result);
1183                 }
1184
1185                 /* print table (and column) check constraints */
1186                 if (tableinfo.checks)
1187                 {
1188                         printfPQExpBuffer(&buf,
1189                                                           "SELECT r.conname, "
1190                                                           "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1191                                                           "FROM pg_catalog.pg_constraint r\n"
1192                                         "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1",
1193                                                           oid);
1194                         result = PSQLexec(buf.data, false);
1195                         if (!result)
1196                                 goto error_return;
1197                         else
1198                                 tuples = PQntuples(result);
1199
1200                         if (tuples > 0)
1201                         {
1202                                 printTableAddFooter(&cont, _("Check constraints:"));
1203                                 for (i = 0; i < tuples; i++)
1204                                 {
1205                                         /* untranslated contraint name and def */
1206                                         printfPQExpBuffer(&buf, "    \"%s\" %s",
1207                                                                           PQgetvalue(result, i, 0),
1208                                                                           PQgetvalue(result, i, 1));
1209
1210                                         printTableAddFooter(&cont, buf.data);
1211                                 }
1212                         }
1213                         PQclear(result);
1214                 }
1215
1216                 /* print foreign-key constraints (there are none if no triggers) */
1217                 if (tableinfo.triggers)
1218                 {
1219                         printfPQExpBuffer(&buf,
1220                                                           "SELECT conname,\n"
1221                                    "  pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1222                                                           "FROM pg_catalog.pg_constraint r\n"
1223                                         "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1",
1224                                                           oid);
1225                         result = PSQLexec(buf.data, false);
1226                         if (!result)
1227                                 goto error_return;
1228                         else
1229                                 tuples = PQntuples(result);
1230
1231                         if (tuples > 0)
1232                         {
1233                                 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1234                                 for (i = 0; i < tuples; i++)
1235                                 {
1236                                         /* untranslated constraint name and def */
1237                                         printfPQExpBuffer(&buf, "    \"%s\" %s",
1238                                                                           PQgetvalue(result, i, 0),
1239                                                                           PQgetvalue(result, i, 1));
1240
1241                                         printTableAddFooter(&cont, buf.data);
1242                                 }
1243                         }
1244                         PQclear(result);
1245                 }
1246
1247                 /* print incoming foreign-key references (none if no triggers) */
1248                 if (tableinfo.triggers)
1249                 {
1250                         printfPQExpBuffer(&buf,
1251                                                           "SELECT conname, conrelid::pg_catalog.regclass,\n"
1252                                                           "  pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1253                                                           "FROM pg_catalog.pg_constraint c\n"
1254                                                           "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
1255                                                           oid);
1256                         result = PSQLexec(buf.data, false);
1257                         if (!result)
1258                                 goto error_return;
1259                         else
1260                                 tuples = PQntuples(result);
1261
1262                         if (tuples > 0)
1263                         {
1264                                 printTableAddFooter(&cont, _("Referenced by:"));
1265                                 for (i = 0; i < tuples; i++)
1266                                 {
1267                                         /* translator: the first %s is a FK name, the following are
1268                                          * a table name and the FK definition */
1269                                         printfPQExpBuffer(&buf, _("  \"%s\" IN %s %s"),
1270                                                                           PQgetvalue(result, i, 0),
1271                                                                           PQgetvalue(result, i, 1),
1272                                                                           PQgetvalue(result, i, 2));
1273
1274                                         printTableAddFooter(&cont, buf.data);
1275                                 }
1276                         }
1277                         PQclear(result);
1278                 }
1279
1280                 /* print rules */
1281                 if (tableinfo.hasrules)
1282                 {
1283                         if (pset.sversion >= 80300)
1284                         {
1285                                 printfPQExpBuffer(&buf,
1286                                                                   "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1287                                                                   "ev_enabled\n"
1288                                                                   "FROM pg_catalog.pg_rewrite r\n"
1289                                                                   "WHERE r.ev_class = '%s' ORDER BY 1",
1290                                                                   oid);
1291                         }
1292                         else
1293                         {
1294                                 printfPQExpBuffer(&buf,
1295                                                                   "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1296                                                                   "'O'::char AS ev_enabled\n"
1297                                                                   "FROM pg_catalog.pg_rewrite r\n"
1298                                                                   "WHERE r.ev_class = '%s' ORDER BY 1",
1299                                                                   oid);
1300                         }
1301                         result = PSQLexec(buf.data, false);
1302                         if (!result)
1303                                 goto error_return;
1304                         else
1305                                 tuples = PQntuples(result);
1306
1307                         if (tuples > 0)
1308                         {
1309                                 bool            have_heading;
1310                                 int                     category;
1311
1312                                 for (category = 0; category < 4; category++)
1313                                 {
1314                                         have_heading = false;
1315
1316                                         for (i = 0; i < tuples; i++)
1317                                         {
1318                                                 const char *ruledef;
1319                                                 bool            list_rule = false;
1320
1321                                                 switch (category)
1322                                                 {
1323                                                         case 0:
1324                                                                 if (*PQgetvalue(result, i, 2) == 'O')
1325                                                                         list_rule = true;
1326                                                                 break;
1327                                                         case 1:
1328                                                                 if (*PQgetvalue(result, i, 2) == 'D')
1329                                                                         list_rule = true;
1330                                                                 break;
1331                                                         case 2:
1332                                                                 if (*PQgetvalue(result, i, 2) == 'A')
1333                                                                         list_rule = true;
1334                                                                 break;
1335                                                         case 3:
1336                                                                 if (*PQgetvalue(result, i, 2) == 'R')
1337                                                                         list_rule = true;
1338                                                                 break;
1339                                                 }
1340                                                 if (!list_rule)
1341                                                         continue;
1342
1343                                                 if (!have_heading)
1344                                                 {
1345                                                         switch (category)
1346                                                         {
1347                                                                 case 0:
1348                                                                         printfPQExpBuffer(&buf, _("Rules:"));
1349                                                                         break;
1350                                                                 case 1:
1351                                                                         printfPQExpBuffer(&buf, _("Disabled rules:"));
1352                                                                         break;
1353                                                                 case 2:
1354                                                                         printfPQExpBuffer(&buf, _("Rules firing always:"));
1355                                                                         break;
1356                                                                 case 3:
1357                                                                         printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1358                                                                         break;
1359                                                         }
1360                                                         printTableAddFooter(&cont, buf.data);
1361                                                         have_heading = true;
1362                                                 }
1363
1364                                                 /* Everything after "CREATE RULE" is echoed verbatim */
1365                                                 ruledef = PQgetvalue(result, i, 1);
1366                                                 ruledef += 12;
1367                                                 printfPQExpBuffer(&buf, "    %s", ruledef);
1368                                                 printTableAddFooter(&cont, buf.data);
1369                                         }
1370                                 }
1371                         }
1372                         PQclear(result);
1373                 }
1374
1375                 /* print triggers (but ignore foreign-key triggers) */
1376                 if (tableinfo.triggers)
1377                 {
1378                         printfPQExpBuffer(&buf,
1379                                                           "SELECT t.tgname, "
1380                                                           "pg_catalog.pg_get_triggerdef(t.oid), "
1381                                                           "t.tgenabled\n"
1382                                                           "FROM pg_catalog.pg_trigger t\n"
1383                                                           "WHERE t.tgrelid = '%s' AND ",
1384                                                           oid);
1385                         if (pset.sversion >= 80300)
1386                                 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
1387                         else
1388                                 appendPQExpBuffer(&buf,
1389                                                                   "(NOT tgisconstraint "
1390                                                                   " OR NOT EXISTS"
1391                                                                   "  (SELECT 1 FROM pg_catalog.pg_depend d "
1392                                                                   "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
1393                                                                   "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
1394                         appendPQExpBuffer(&buf, "\nORDER BY 1");
1395
1396                         result = PSQLexec(buf.data, false);
1397                         if (!result)
1398                                 goto error_return;
1399                         else
1400                                 tuples = PQntuples(result);
1401
1402                         if (tuples > 0)
1403                         {
1404                                 bool            have_heading;
1405                                 int                     category;
1406
1407                                 /*
1408                                  * split the output into 4 different categories. Enabled triggers,
1409                                  * disabled triggers and the two special ALWAYS and REPLICA
1410                                  * configurations.
1411                                  */
1412                                 for (category = 0; category < 4; category++)
1413                                 {
1414                                         have_heading = false;
1415                                         for (i = 0; i < tuples; i++)
1416                                         {
1417                                                 bool            list_trigger;
1418                                                 const char *tgdef;
1419                                                 const char *usingpos;
1420                                                 const char *tgenabled;
1421
1422                                                 /* Check if this trigger falls into the current category */
1423                                                 tgenabled = PQgetvalue(result, i, 2);
1424                                                 list_trigger = false;
1425                                                 switch (category)
1426                                                 {
1427                                                         case 0:
1428                                                                 if (*tgenabled == 'O' || *tgenabled == 't')
1429                                                                         list_trigger = true;
1430                                                                 break;
1431                                                         case 1:
1432                                                                 if (*tgenabled == 'D' || *tgenabled == 'f')
1433                                                                         list_trigger = true;
1434                                                                 break;
1435                                                         case 2:
1436                                                                 if (*tgenabled == 'A')
1437                                                                         list_trigger = true;
1438                                                                 break;
1439                                                         case 3:
1440                                                                 if (*tgenabled == 'R')
1441                                                                         list_trigger = true;
1442                                                                 break;
1443                                                 }
1444                                                 if (list_trigger == false)
1445                                                         continue;
1446
1447                                                 /* Print the category heading once */
1448                                                 if (have_heading == false)
1449                                                 {
1450                                                         switch (category)
1451                                                         {
1452                                                                 case 0:
1453                                                                         printfPQExpBuffer(&buf, _("Triggers:"));
1454                                                                         break;
1455                                                                 case 1:
1456                                                                         printfPQExpBuffer(&buf, _("Disabled triggers:"));
1457                                                                         break;
1458                                                                 case 2:
1459                                                                         printfPQExpBuffer(&buf, _("Triggers firing always:"));
1460                                                                         break;
1461                                                                 case 3:
1462                                                                         printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
1463                                                                         break;
1464
1465                                                         }
1466                                                         printTableAddFooter(&cont, buf.data);
1467                                                         have_heading = true;
1468                                                 }
1469
1470                                                 /* Everything after "TRIGGER" is echoed verbatim */
1471                                                 tgdef = PQgetvalue(result, i, 1);
1472                                                 usingpos = strstr(tgdef, " TRIGGER ");
1473                                                 if (usingpos)
1474                                                         tgdef = usingpos + 9;
1475
1476                                                 printfPQExpBuffer(&buf, "    %s", tgdef);
1477                                                 printTableAddFooter(&cont, buf.data);
1478                                         }
1479                                 }
1480                         }
1481                         PQclear(result);
1482                 }
1483
1484                 /* print inherited tables */
1485                 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);
1486
1487                 result = PSQLexec(buf.data, false);
1488                 if (!result)
1489                         goto error_return;
1490                 else
1491                         tuples = PQntuples(result);
1492
1493                 for (i = 0; i < tuples; i++)
1494                 {
1495                         const char *s = _("Inherits");
1496
1497                         if (i == 0)
1498                                 printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
1499                         else
1500                                 printfPQExpBuffer(&buf, "%*s  %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
1501                         if (i < tuples - 1)
1502                                 appendPQExpBuffer(&buf, ",");
1503
1504                         printTableAddFooter(&cont, buf.data);
1505                 }
1506                 PQclear(result);
1507
1508                 if (verbose)
1509                 {
1510                         const char *s = _("Has OIDs");
1511
1512                         printfPQExpBuffer(&buf, "%s: %s", s,
1513                                                           (tableinfo.hasoids ? _("yes") : _("no")));
1514                         printTableAddFooter(&cont, buf.data);
1515                 }
1516
1517                 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
1518                                                           true);
1519         }
1520
1521         printTable(&cont, pset.queryFout, pset.logfile);
1522         printTableCleanup(&cont);
1523
1524         retval = true;
1525
1526 error_return:
1527
1528         /* clean up */
1529         printTableCleanup(&cont);
1530         termPQExpBuffer(&buf);
1531         termPQExpBuffer(&title);
1532         termPQExpBuffer(&tmpbuf);
1533
1534         if (show_modifiers)
1535         {
1536                 for (ptr = modifiers; *ptr; ptr++)
1537                         free(*ptr);
1538                 free(modifiers);
1539         }
1540
1541         if (view_def)
1542                 free(view_def);
1543
1544         if (res)
1545                 PQclear(res);
1546
1547         return retval;
1548 }
1549
1550 /*
1551  * Add a tablespace description to a footer.  If 'newline' is true, it is added
1552  * in a new line; otherwise it's appended to the current value of the last
1553  * footer.
1554  */
1555 static void
1556 add_tablespace_footer(printTableContent *const cont, char relkind,
1557                                           Oid tablespace, const bool newline)
1558 {
1559         /* relkinds for which we support tablespaces */
1560         if (relkind == 'r' || relkind == 'i')
1561         {
1562                 /*
1563                  * We ignore the database default tablespace so that users not using
1564                  * tablespaces don't need to know about them.  This case also covers
1565                  * pre-8.0 servers, for which tablespace will always be 0.
1566                  */
1567                 if (tablespace != 0)
1568                 {
1569                         PGresult   *result = NULL;
1570                         PQExpBufferData buf;
1571
1572                         initPQExpBuffer(&buf);
1573                         printfPQExpBuffer(&buf,
1574                                                           "SELECT spcname FROM pg_catalog.pg_tablespace\n"
1575                                                           "WHERE oid = '%u'", tablespace);
1576                         result = PSQLexec(buf.data, false);
1577                         if (!result)
1578                                 return;
1579                         /* Should always be the case, but.... */
1580                         if (PQntuples(result) > 0)
1581                         {
1582                                 if (newline)
1583                                 {
1584                                         /* Add the tablespace as a new footer */
1585                                         printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
1586                                                                           PQgetvalue(result, 0, 0));
1587                                         printTableAddFooter(cont, buf.data);
1588                                 }
1589                                 else
1590                                 {
1591                                         /* Append the tablespace to the latest footer */
1592                                         printfPQExpBuffer(&buf, "%s", cont->footer->data);
1593                                         /* translator: before this string there's an index
1594                                          * description like '"foo_pkey" PRIMARY KEY, btree (a)' */
1595                                         appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
1596                                                                           PQgetvalue(result, 0, 0));
1597                                         printTableSetFooter(cont, buf.data);
1598                                 }
1599                         }
1600                         PQclear(result);
1601                         termPQExpBuffer(&buf);
1602                 }
1603         }
1604 }
1605
1606 /*
1607  * \du or \dg
1608  *
1609  * Describes roles.  Any schema portion of the pattern is ignored.
1610  */
1611 bool
1612 describeRoles(const char *pattern, bool verbose)
1613 {
1614         PQExpBufferData buf;
1615         PGresult   *res;
1616         printTableContent cont;
1617         printTableOpt myopt = pset.popt.topt;
1618         int                     ncols = 3;
1619         int                     nrows = 0;
1620         int                     i;
1621         int                     conns;
1622         const char      align = 'l';
1623         char      **attr;
1624
1625         initPQExpBuffer(&buf);
1626
1627         if (pset.sversion >= 80100)
1628         {
1629                 printfPQExpBuffer(&buf,
1630                                              "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
1631                                                  "  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
1632                                                  "  r.rolconnlimit,\n"
1633                                              "  ARRAY(SELECT b.rolname\n"
1634                                              "        FROM pg_catalog.pg_auth_members m\n"
1635                                              "        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
1636                                              "        WHERE m.member = r.oid) as memberof");
1637
1638                 if (verbose && pset.sversion >= 80200)
1639                 {
1640                         appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
1641                         ncols++;
1642                 }
1643
1644                 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
1645
1646                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1647                                                           NULL, "r.rolname", NULL, NULL);
1648     }
1649     else
1650     {
1651             printfPQExpBuffer(&buf,
1652                                           "SELECT u.usename AS rolname,\n"
1653                                           "  u.usesuper AS rolsuper,\n"
1654                                           "  true AS rolinherit, false AS rolcreaterole,\n"
1655                                           "  u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
1656                                           "  -1 AS rolconnlimit,\n"
1657                                           "  ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
1658                                           "\nFROM pg_catalog.pg_user u\n");
1659
1660                 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1661                                                           NULL, "u.usename", NULL, NULL);
1662     }
1663
1664         appendPQExpBuffer(&buf, "ORDER BY 1;");
1665
1666         res = PSQLexec(buf.data, false);
1667         if (!res)
1668                 return false;
1669
1670         nrows = PQntuples(res);
1671         attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
1672
1673         printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
1674
1675         printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
1676         printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
1677         printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
1678
1679         if (verbose && pset.sversion >= 80200)
1680                 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
1681
1682         for (i = 0; i < nrows; i++)
1683         {
1684                 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
1685
1686                 resetPQExpBuffer(&buf);
1687                 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
1688                         add_role_attribute(&buf, _("Superuser"));
1689
1690                 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
1691                         add_role_attribute(&buf, _("No inheritance"));
1692
1693                 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1694                         add_role_attribute(&buf, _("Create role"));
1695
1696                 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
1697                         add_role_attribute(&buf, _("Create DB"));
1698
1699                 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
1700                         add_role_attribute(&buf, _("Cannot login"));
1701
1702                 conns = atoi(PQgetvalue(res, i, 6));
1703                 if (conns >= 0)
1704                 {
1705                         if (buf.len > 0)
1706                                 appendPQExpBufferStr(&buf, "\n");
1707
1708                         if (conns == 0)
1709                                 appendPQExpBuffer(&buf, _("No connections"));
1710                         else if (conns == 1)
1711                                 appendPQExpBuffer(&buf, _("1 connection"));
1712                         else
1713                                 appendPQExpBuffer(&buf, _("%d connections"), conns);
1714                 }
1715
1716                 attr[i] = pg_strdup(buf.data);
1717
1718                 printTableAddCell(&cont, attr[i], false);
1719
1720                 printTableAddCell(&cont, PQgetvalue(res, i, 7), false);
1721
1722                 if (verbose && pset.sversion >= 80200)
1723                         printTableAddCell(&cont, PQgetvalue(res, i, 8), false);
1724         }
1725         termPQExpBuffer(&buf);
1726
1727         printTable(&cont, pset.queryFout, pset.logfile);
1728         printTableCleanup(&cont);
1729
1730         for (i = 0; i < nrows; i++)
1731                 free(attr[i]);
1732         free(attr);
1733
1734         PQclear(res);
1735         return true;
1736 }
1737
1738 void
1739 add_role_attribute(PQExpBuffer buf, const char *const str)
1740 {
1741         if (buf->len > 0)
1742                 appendPQExpBufferStr(buf, "\n");
1743
1744         appendPQExpBufferStr(buf, str);
1745 }
1746
1747
1748 /*
1749  * listTables()
1750  *
1751  * handler for \d, \dt, etc.
1752  *
1753  * tabtypes is an array of characters, specifying what info is desired:
1754  * t - tables
1755  * i - indexes
1756  * v - views
1757  * s - sequences
1758  * S - system tables (pg_catalog)
1759  * (any order of the above is fine)
1760  */
1761 bool
1762 listTables(const char *tabtypes, const char *pattern, bool verbose)
1763 {
1764         bool            showTables = strchr(tabtypes, 't') != NULL;
1765         bool            showIndexes = strchr(tabtypes, 'i') != NULL;
1766         bool            showViews = strchr(tabtypes, 'v') != NULL;
1767         bool            showSeq = strchr(tabtypes, 's') != NULL;
1768         bool            showSystem = strchr(tabtypes, 'S') != NULL;
1769
1770         PQExpBufferData buf;
1771         PGresult   *res;
1772         printQueryOpt myopt = pset.popt;
1773         static const bool trans_columns[] = {false, false, true, false, false, false};
1774
1775         if (!(showTables || showIndexes || showViews || showSeq))
1776                 showTables = showViews = showSeq = true;
1777
1778         initPQExpBuffer(&buf);
1779
1780         /*
1781          * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
1782          * for backwards compatibility.
1783          */
1784         printfPQExpBuffer(&buf,
1785                                           "SELECT n.nspname as \"%s\",\n"
1786                                           "  c.relname as \"%s\",\n"
1787                                           "  CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' END as \"%s\",\n"
1788                                           "  pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
1789                                           gettext_noop("Schema"),
1790                                           gettext_noop("Name"),
1791                                           gettext_noop("table"),
1792                                           gettext_noop("view"),
1793                                           gettext_noop("index"),
1794                                           gettext_noop("sequence"),
1795                                           gettext_noop("special"),
1796                                           gettext_noop("Type"),
1797                                           gettext_noop("Owner"));
1798
1799         if (showIndexes)
1800                 appendPQExpBuffer(&buf,
1801                                                   ",\n c2.relname as \"%s\"",
1802                                                   gettext_noop("Table"));
1803
1804         if (verbose && pset.sversion >= 80100)
1805                 appendPQExpBuffer(&buf,
1806                                                   ",\n  pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
1807                                                   gettext_noop("Size"));
1808         if (verbose)
1809                 appendPQExpBuffer(&buf,
1810                           ",\n  pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
1811                                                   gettext_noop("Description"));
1812
1813         appendPQExpBuffer(&buf,
1814                                           "\nFROM pg_catalog.pg_class c"
1815          "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
1816         if (showIndexes)
1817                 appendPQExpBuffer(&buf,
1818                          "\n     LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
1819                    "\n     LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
1820
1821         appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
1822         if (showTables)
1823                 appendPQExpBuffer(&buf, "'r',");
1824         if (showViews)
1825                 appendPQExpBuffer(&buf, "'v',");
1826         if (showIndexes)
1827                 appendPQExpBuffer(&buf, "'i',");
1828         if (showSeq)
1829                 appendPQExpBuffer(&buf, "'S',");
1830         if (showSystem && showTables)
1831                 appendPQExpBuffer(&buf, "'s',");
1832         appendPQExpBuffer(&buf, "''");          /* dummy */
1833         appendPQExpBuffer(&buf, ")\n");
1834
1835         /*
1836          * If showSystem is specified, show only system objects (those in
1837          * pg_catalog).  Otherwise, suppress system objects, including those in
1838          * pg_catalog and pg_toast.  (We don't want to hide temp tables though.)
1839          */
1840         if (showSystem)
1841                 appendPQExpBuffer(&buf,
1842                                                   "  AND n.nspname = 'pg_catalog'\n");
1843         else
1844                 appendPQExpBuffer(&buf,
1845                                                   "  AND n.nspname <> 'pg_catalog'\n"
1846                                                   "  AND n.nspname !~ '^pg_toast'\n");
1847
1848         processSQLNamePattern(pset.db, &buf, pattern, true, false,
1849                                                   "n.nspname", "c.relname", NULL,
1850                                                   "pg_catalog.pg_table_is_visible(c.oid)");
1851
1852         appendPQExpBuffer(&buf, "ORDER BY 1,2;");
1853
1854         res = PSQLexec(buf.data, false);
1855         termPQExpBuffer(&buf);
1856         if (!res)
1857                 return false;
1858
1859         if (PQntuples(res) == 0 && !pset.quiet)
1860         {
1861                 if (pattern)
1862                         fprintf(pset.queryFout, _("No matching relations found.\n"));
1863                 else
1864                         fprintf(pset.queryFout, _("No relations found.\n"));
1865         }
1866         else
1867         {
1868                 myopt.nullPrint = NULL;
1869                 myopt.title = _("List of relations");
1870                 myopt.trans_headers = true;
1871                 myopt.trans_columns = trans_columns;
1872
1873                 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1874         }
1875
1876         PQclear(res);
1877         return true;
1878 }
1879
1880
1881 /*
1882  * \dD
1883  *
1884  * Describes domains.
1885  */
1886 bool
1887 listDomains(const char *pattern)
1888 {
1889         PQExpBufferData buf;
1890         PGresult   *res;
1891         printQueryOpt myopt = pset.popt;
1892
1893         initPQExpBuffer(&buf);
1894
1895         printfPQExpBuffer(&buf,
1896                                           "SELECT n.nspname as \"%s\",\n"
1897                                           "       t.typname as \"%s\",\n"
1898          "       pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
1899                                           "       CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
1900         "            WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
1901                                           "            WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
1902                                           "            ELSE ''\n"
1903                                           "       END as \"%s\",\n"
1904                         "       pg_catalog.pg_get_constraintdef(r.oid, true) as \"%s\"\n"
1905                                           "FROM pg_catalog.pg_type t\n"
1906            "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
1907                   "     LEFT JOIN pg_catalog.pg_constraint r ON t.oid = r.contypid\n"
1908                                           "WHERE t.typtype = 'd'\n",
1909                                           gettext_noop("Schema"),
1910                                           gettext_noop("Name"),
1911                                           gettext_noop("Type"),
1912                                           gettext_noop("Modifier"),
1913                                           gettext_noop("Check"));
1914
1915         processSQLNamePattern(pset.db, &buf, pattern, true, false,
1916                                                   "n.nspname", "t.typname", NULL,
1917                                                   "pg_catalog.pg_type_is_visible(t.oid)");
1918
1919         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1920
1921         res = PSQLexec(buf.data, false);
1922         termPQExpBuffer(&buf);
1923         if (!res)
1924                 return false;
1925
1926         myopt.nullPrint = NULL;
1927         myopt.title = _("List of domains");
1928         myopt.trans_headers = true;
1929
1930         printQuery(res, &myopt, pset.queryFout, pset.logfile);
1931
1932         PQclear(res);
1933         return true;
1934 }
1935
1936 /*
1937  * \dc
1938  *
1939  * Describes conversions.
1940  */
1941 bool
1942 listConversions(const char *pattern)
1943 {
1944         PQExpBufferData buf;
1945         PGresult   *res;
1946         printQueryOpt myopt = pset.popt;
1947         static const bool trans_columns[] = {false, false, false, false, true};
1948
1949         initPQExpBuffer(&buf);
1950
1951         printfPQExpBuffer(&buf,
1952                                           "SELECT n.nspname AS \"%s\",\n"
1953                                           "       c.conname AS \"%s\",\n"
1954            "       pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
1955                 "       pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
1956                                           "       CASE WHEN c.condefault THEN '%s'\n"
1957                                           "       ELSE '%s' END AS \"%s\"\n"
1958                            "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
1959                                           "WHERE n.oid = c.connamespace\n",
1960                                           gettext_noop("Schema"),
1961                                           gettext_noop("Name"),
1962                                           gettext_noop("Source"),
1963                                           gettext_noop("Destination"),
1964                                           gettext_noop("yes"), gettext_noop("no"),
1965                                           gettext_noop("Default?"));
1966
1967         processSQLNamePattern(pset.db, &buf, pattern, true, false,
1968                                                   "n.nspname", "c.conname", NULL,
1969                                                   "pg_catalog.pg_conversion_is_visible(c.oid)");
1970
1971         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1972
1973         res = PSQLexec(buf.data, false);
1974         termPQExpBuffer(&buf);
1975         if (!res)
1976                 return false;
1977
1978         myopt.nullPrint = NULL;
1979         myopt.title = _("List of conversions");
1980         myopt.trans_headers = true;
1981         myopt.trans_columns = trans_columns;
1982
1983         printQuery(res, &myopt, pset.queryFout, pset.logfile);
1984
1985         PQclear(res);
1986         return true;
1987 }
1988
1989 /*
1990  * \dC
1991  *
1992  * Describes casts.
1993  */
1994 bool
1995 listCasts(const char *pattern)
1996 {
1997         PQExpBufferData buf;
1998         PGresult   *res;
1999         printQueryOpt myopt = pset.popt;
2000         static const bool trans_columns[] = {false, false, false, true};
2001
2002         initPQExpBuffer(&buf);
2003         /*
2004          * We need left join here for binary casts.  Also note that we don't
2005          * attempt to localize '(binary compatible)', because there's too much
2006          * risk of gettext translating a function name that happens to match
2007          * some string in the PO database.
2008          */
2009         printfPQExpBuffer(&buf,
2010                            "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2011                            "       pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2012                                           "       CASE WHEN castfunc = 0 THEN '(binary compatible)'\n"
2013                                           "            ELSE p.proname\n"
2014                                           "       END as \"%s\",\n"
2015                                           "       CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2016                                           "            WHEN c.castcontext = 'a' THEN '%s'\n"
2017                                           "            ELSE '%s'\n"
2018                                           "       END as \"%s\"\n"
2019                                  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2020                                           "     ON c.castfunc = p.oid\n"
2021                                           "ORDER BY 1, 2",
2022                                           gettext_noop("Source type"),
2023                                           gettext_noop("Target type"),
2024                                           gettext_noop("Function"),
2025                                           gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2026                                           gettext_noop("Implicit?"));
2027
2028         res = PSQLexec(buf.data, false);
2029         termPQExpBuffer(&buf);
2030         if (!res)
2031                 return false;
2032
2033         myopt.nullPrint = NULL;
2034         myopt.title = _("List of casts");
2035         myopt.trans_headers = true;
2036         myopt.trans_columns = trans_columns;
2037
2038         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2039
2040         PQclear(res);
2041         return true;
2042 }
2043
2044 /*
2045  * \dn
2046  *
2047  * Describes schemas (namespaces)
2048  */
2049 bool
2050 listSchemas(const char *pattern, bool verbose)
2051 {
2052         PQExpBufferData buf;
2053         PGresult   *res;
2054         printQueryOpt myopt = pset.popt;
2055
2056         initPQExpBuffer(&buf);
2057         printfPQExpBuffer(&buf,
2058                                           "SELECT n.nspname AS \"%s\",\n"
2059                                           "  pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2060                                           gettext_noop("Name"),
2061                                           gettext_noop("Owner"));
2062
2063         if (verbose)
2064                 appendPQExpBuffer(&buf,
2065                                                   ",\n  n.nspacl as \"%s\","
2066                          "  pg_catalog.obj_description(n.oid, 'pg_namespace') as \"%s\"",
2067                                                   gettext_noop("Access privileges"),
2068                                                   gettext_noop("Description"));
2069
2070         appendPQExpBuffer(&buf,
2071                           "\nFROM pg_catalog.pg_namespace n\n"
2072                                           "WHERE        (n.nspname !~ '^pg_temp_' OR\n"
2073                    "             n.nspname = (pg_catalog.current_schemas(true))[1])\n");                /* temp schema is first */
2074
2075         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2076                                                   NULL, "n.nspname", NULL,
2077                                                   NULL);
2078
2079         appendPQExpBuffer(&buf, "ORDER BY 1;");
2080
2081         res = PSQLexec(buf.data, false);
2082         termPQExpBuffer(&buf);
2083         if (!res)
2084                 return false;
2085
2086         myopt.nullPrint = NULL;
2087         myopt.title = _("List of schemas");
2088         myopt.trans_headers = true;
2089
2090         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2091
2092         PQclear(res);
2093         return true;
2094 }
2095
2096
2097 /*
2098  * \dFp
2099  * list text search parsers
2100  */
2101 bool
2102 listTSParsers(const char *pattern, bool verbose)
2103 {
2104         PQExpBufferData buf;
2105         PGresult   *res;
2106         printQueryOpt myopt = pset.popt;
2107
2108         if (pset.sversion < 80300)
2109         {
2110                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2111                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
2112                 return true;
2113         }
2114
2115         if (verbose)
2116                 return listTSParsersVerbose(pattern);
2117
2118         initPQExpBuffer(&buf);
2119
2120         printfPQExpBuffer(&buf,
2121                                           "SELECT \n"
2122                                           "  n.nspname as \"%s\",\n"
2123                                           "  p.prsname as \"%s\",\n"
2124                         "  pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2125                                           "FROM pg_catalog.pg_ts_parser p \n"
2126                    "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2127                                           gettext_noop("Schema"),
2128                                           gettext_noop("Name"),
2129                                           gettext_noop("Description")
2130                 );
2131
2132         processSQLNamePattern(pset.db, &buf, pattern, false, false,
2133                                                   "n.nspname", "p.prsname", NULL,
2134                                                   "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2135
2136         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2137
2138         res = PSQLexec(buf.data, false);
2139         termPQExpBuffer(&buf);
2140         if (!res)
2141                 return false;
2142
2143         myopt.nullPrint = NULL;
2144         myopt.title = _("List of text search parsers");
2145         myopt.trans_headers = true;
2146
2147         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2148
2149         PQclear(res);
2150         return true;
2151 }
2152
2153 /*
2154  * full description of parsers
2155  */
2156 static bool
2157 listTSParsersVerbose(const char *pattern)
2158 {
2159         PQExpBufferData buf;
2160         PGresult   *res;
2161         int                     i;
2162
2163         initPQExpBuffer(&buf);
2164
2165         printfPQExpBuffer(&buf,
2166                                           "SELECT p.oid, \n"
2167                                           "  n.nspname, \n"
2168                                           "  p.prsname \n"
2169                                           "FROM pg_catalog.pg_ts_parser p\n"
2170                         "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2171                 );
2172
2173         processSQLNamePattern(pset.db, &buf, pattern, false, false,
2174                                                   "n.nspname", "p.prsname", NULL,
2175                                                   "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2176
2177         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2178
2179         res = PSQLexec(buf.data, false);
2180         termPQExpBuffer(&buf);
2181         if (!res)
2182                 return false;
2183
2184         if (PQntuples(res) == 0)
2185         {
2186                 if (!pset.quiet)
2187                         fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
2188                                         pattern);
2189                 PQclear(res);
2190                 return false;
2191         }
2192
2193         for (i = 0; i < PQntuples(res); i++)
2194         {
2195                 const char *oid;
2196                 const char *nspname = NULL;
2197                 const char *prsname;
2198
2199                 oid = PQgetvalue(res, i, 0);
2200                 if (!PQgetisnull(res, i, 1))
2201                         nspname = PQgetvalue(res, i, 1);
2202                 prsname = PQgetvalue(res, i, 2);
2203
2204                 if (!describeOneTSParser(oid, nspname, prsname))
2205                 {
2206                         PQclear(res);
2207                         return false;
2208                 }
2209
2210                 if (cancel_pressed)
2211                 {
2212                         PQclear(res);
2213                         return false;
2214                 }
2215         }
2216
2217         PQclear(res);
2218         return true;
2219 }
2220
2221 static bool
2222 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
2223 {
2224         PQExpBufferData buf;
2225         PGresult   *res;
2226         char            title[1024];
2227         printQueryOpt myopt = pset.popt;
2228         static const bool trans_columns[] = {true, false, false};
2229
2230         initPQExpBuffer(&buf);
2231
2232         printfPQExpBuffer(&buf,
2233                                           "SELECT '%s' AS \"%s\", \n"
2234                                           "   p.prsstart::pg_catalog.regproc AS \"%s\", \n"
2235                   "   pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
2236                                           " FROM pg_catalog.pg_ts_parser p \n"
2237                                           " WHERE p.oid = '%s' \n"
2238                                           "UNION ALL \n"
2239                                           "SELECT '%s', \n"
2240                                           "   p.prstoken::pg_catalog.regproc, \n"
2241                                         "   pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
2242                                           " FROM pg_catalog.pg_ts_parser p \n"
2243                                           " WHERE p.oid = '%s' \n"
2244                                           "UNION ALL \n"
2245                                           "SELECT '%s', \n"
2246                                           "   p.prsend::pg_catalog.regproc, \n"
2247                                           "   pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
2248                                           " FROM pg_catalog.pg_ts_parser p \n"
2249                                           " WHERE p.oid = '%s' \n"
2250                                           "UNION ALL \n"
2251                                           "SELECT '%s', \n"
2252                                           "   p.prsheadline::pg_catalog.regproc, \n"
2253                                  "   pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
2254                                           " FROM pg_catalog.pg_ts_parser p \n"
2255                                           " WHERE p.oid = '%s' \n"
2256                                           "UNION ALL \n"
2257                                           "SELECT '%s', \n"
2258                                           "   p.prslextype::pg_catalog.regproc, \n"
2259                                   "   pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
2260                                           " FROM pg_catalog.pg_ts_parser p \n"
2261                                           " WHERE p.oid = '%s' \n",
2262                                           gettext_noop("Start parse"),
2263                                           gettext_noop("Method"),
2264                                           gettext_noop("Function"),
2265                                           gettext_noop("Description"),
2266                                           oid,
2267                                           gettext_noop("Get next token"),
2268                                           oid,
2269                                           gettext_noop("End parse"),
2270                                           oid,
2271                                           gettext_noop("Get headline"),
2272                                           oid,
2273                                           gettext_noop("Get token types"),
2274                                           oid);
2275
2276         res = PSQLexec(buf.data, false);
2277         termPQExpBuffer(&buf);
2278         if (!res)
2279                 return false;
2280
2281         myopt.nullPrint = NULL;
2282         if (nspname)
2283                 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
2284         else
2285                 sprintf(title, _("Text search parser \"%s\""), prsname);
2286         myopt.title = title;
2287         myopt.footers = NULL;
2288         myopt.default_footer = false;
2289         myopt.trans_headers = true;
2290         myopt.trans_columns = trans_columns;
2291
2292         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2293
2294         PQclear(res);
2295
2296         initPQExpBuffer(&buf);
2297
2298         printfPQExpBuffer(&buf,
2299                                           "SELECT t.alias as \"%s\", \n"
2300                                           "  t.description as \"%s\" \n"
2301                           "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
2302                                           "ORDER BY 1;",
2303                                           gettext_noop("Token name"),
2304                                           gettext_noop("Description"),
2305                                           oid);
2306
2307         res = PSQLexec(buf.data, false);
2308         termPQExpBuffer(&buf);
2309         if (!res)
2310                 return false;
2311
2312         myopt.nullPrint = NULL;
2313         if (nspname)
2314                 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
2315         else
2316                 sprintf(title, _("Token types for parser \"%s\""), prsname);
2317         myopt.title = title;
2318         myopt.footers = NULL;
2319         myopt.default_footer = true;
2320         myopt.trans_headers = true;
2321         myopt.trans_columns = NULL;
2322
2323         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2324
2325         PQclear(res);
2326         return true;
2327 }
2328
2329
2330 /*
2331  * \dFd
2332  * list text search dictionaries
2333  */
2334 bool
2335 listTSDictionaries(const char *pattern, bool verbose)
2336 {
2337         PQExpBufferData buf;
2338         PGresult   *res;
2339         printQueryOpt myopt = pset.popt;
2340
2341         if (pset.sversion < 80300)
2342         {
2343                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2344                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
2345                 return true;
2346         }
2347
2348         initPQExpBuffer(&buf);
2349
2350         printfPQExpBuffer(&buf,
2351                                           "SELECT \n"
2352                                           "  n.nspname as \"%s\",\n"
2353                                           "  d.dictname as \"%s\",\n",
2354                                           gettext_noop("Schema"),
2355                                           gettext_noop("Name"));
2356
2357         if (verbose)
2358         {
2359                 appendPQExpBuffer(&buf,
2360                                                   "  ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
2361                                                   "    pg_catalog.pg_ts_template t \n"
2362                                                   "                      LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
2363                                                   "                      WHERE d.dicttemplate = t.oid ) AS  \"%s\", \n"
2364                                                   "  d.dictinitoption as \"%s\", \n",
2365                                                   gettext_noop("Template"),
2366                                                   gettext_noop("Init options"));
2367         }
2368
2369         appendPQExpBuffer(&buf,
2370                          "  pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
2371                                           gettext_noop("Description"));
2372
2373         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
2374                  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
2375
2376         processSQLNamePattern(pset.db, &buf, pattern, false, false,
2377                                                   "n.nspname", "d.dictname", NULL,
2378                                                   "pg_catalog.pg_ts_dict_is_visible(d.oid)");
2379
2380         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2381
2382         res = PSQLexec(buf.data, false);
2383         termPQExpBuffer(&buf);
2384         if (!res)
2385                 return false;
2386
2387         myopt.nullPrint = NULL;
2388         myopt.title = _("List of text search dictionaries");
2389         myopt.trans_headers = true;
2390
2391         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2392
2393         PQclear(res);
2394         return true;
2395 }
2396
2397
2398 /*
2399  * \dFt
2400  * list text search templates
2401  */
2402 bool
2403 listTSTemplates(const char *pattern, bool verbose)
2404 {
2405         PQExpBufferData buf;
2406         PGresult   *res;
2407         printQueryOpt myopt = pset.popt;
2408
2409         if (pset.sversion < 80300)
2410         {
2411                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2412                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
2413                 return true;
2414         }
2415
2416         initPQExpBuffer(&buf);
2417
2418         if (verbose)
2419                 printfPQExpBuffer(&buf,
2420                                                   "SELECT \n"
2421                                                   "  n.nspname AS \"%s\",\n"
2422                                                   "  t.tmplname AS \"%s\",\n"
2423                                                   "  t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
2424                                                   "  t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
2425                  "  pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2426                                                   gettext_noop("Schema"),
2427                                                   gettext_noop("Name"),
2428                                                   gettext_noop("Init"),
2429                                                   gettext_noop("Lexize"),
2430                                                   gettext_noop("Description"));
2431         else
2432                 printfPQExpBuffer(&buf,
2433                                                   "SELECT \n"
2434                                                   "  n.nspname AS \"%s\",\n"
2435                                                   "  t.tmplname AS \"%s\",\n"
2436                  "  pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2437                                                   gettext_noop("Schema"),
2438                                                   gettext_noop("Name"),
2439                                                   gettext_noop("Description"));
2440
2441         appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
2442                  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
2443
2444         processSQLNamePattern(pset.db, &buf, pattern, false, false,
2445                                                   "n.nspname", "t.tmplname", NULL,
2446                                                   "pg_catalog.pg_ts_template_is_visible(t.oid)");
2447
2448         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2449
2450         res = PSQLexec(buf.data, false);
2451         termPQExpBuffer(&buf);
2452         if (!res)
2453                 return false;
2454
2455         myopt.nullPrint = NULL;
2456         myopt.title = _("List of text search templates");
2457         myopt.trans_headers = true;
2458
2459         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2460
2461         PQclear(res);
2462         return true;
2463 }
2464
2465
2466 /*
2467  * \dF
2468  * list text search configurations
2469  */
2470 bool
2471 listTSConfigs(const char *pattern, bool verbose)
2472 {
2473         PQExpBufferData buf;
2474         PGresult   *res;
2475         printQueryOpt myopt = pset.popt;
2476
2477         if (pset.sversion < 80300)
2478         {
2479                 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2480                                 pset.sversion / 10000, (pset.sversion / 100) % 100);
2481                 return true;
2482         }
2483
2484         if (verbose)
2485                 return listTSConfigsVerbose(pattern);
2486
2487         initPQExpBuffer(&buf);
2488
2489         printfPQExpBuffer(&buf,
2490                                           "SELECT \n"
2491                                           "   n.nspname as \"%s\",\n"
2492                                           "   c.cfgname as \"%s\",\n"
2493                    "   pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
2494                                           "FROM pg_catalog.pg_ts_config c\n"
2495                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
2496                                           gettext_noop("Schema"),
2497                                           gettext_noop("Name"),
2498                                           gettext_noop("Description")
2499                 );
2500
2501         processSQLNamePattern(pset.db, &buf, pattern, false, false,
2502                                                   "n.nspname", "c.cfgname", NULL,
2503                                                   "pg_catalog.pg_ts_config_is_visible(c.oid)");
2504
2505         appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2506
2507         res = PSQLexec(buf.data, false);
2508         termPQExpBuffer(&buf);
2509         if (!res)
2510                 return false;
2511
2512         myopt.nullPrint = NULL;
2513         myopt.title = _("List of text search configurations");
2514         myopt.trans_headers = true;
2515
2516         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2517
2518         PQclear(res);
2519         return true;
2520 }
2521
2522 static bool
2523 listTSConfigsVerbose(const char *pattern)
2524 {
2525         PQExpBufferData buf;
2526         PGresult   *res;
2527         int                     i;
2528
2529         initPQExpBuffer(&buf);
2530
2531         printfPQExpBuffer(&buf,
2532                                           "SELECT c.oid, c.cfgname,\n"
2533                                           "   n.nspname, \n"
2534                                           "   p.prsname, \n"
2535                                           "   np.nspname as pnspname \n"
2536                                           "FROM pg_catalog.pg_ts_config c \n"
2537            "   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
2538                                           " pg_catalog.pg_ts_parser p \n"
2539           "   LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
2540                                           "WHERE  p.oid = c.cfgparser\n"
2541                 );
2542
2543         processSQLNamePattern(pset.db, &buf, pattern, true, false,
2544                                                   "n.nspname", "c.cfgname", NULL,
2545                                                   "pg_catalog.pg_ts_config_is_visible(c.oid)");
2546
2547         appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
2548
2549         res = PSQLexec(buf.data, false);
2550         termPQExpBuffer(&buf);
2551         if (!res)
2552                 return false;
2553
2554         if (PQntuples(res) == 0)
2555         {
2556                 if (!pset.quiet)
2557                         fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
2558                                         pattern);
2559                 PQclear(res);
2560                 return false;
2561         }
2562
2563         for (i = 0; i < PQntuples(res); i++)
2564         {
2565                 const char *oid;
2566                 const char *cfgname;
2567                 const char *nspname = NULL;
2568                 const char *prsname;
2569                 const char *pnspname = NULL;
2570
2571                 oid = PQgetvalue(res, i, 0);
2572                 cfgname = PQgetvalue(res, i, 1);
2573                 if (!PQgetisnull(res, i, 2))
2574                         nspname = PQgetvalue(res, i, 2);
2575                 prsname = PQgetvalue(res, i, 3);
2576                 if (!PQgetisnull(res, i, 4))
2577                         pnspname = PQgetvalue(res, i, 4);
2578
2579                 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
2580                 {
2581                         PQclear(res);
2582                         return false;
2583                 }
2584
2585                 if (cancel_pressed)
2586                 {
2587                         PQclear(res);
2588                         return false;
2589                 }
2590         }
2591
2592         PQclear(res);
2593         return true;
2594 }
2595
2596 static bool
2597 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
2598                                         const char *pnspname, const char *prsname)
2599 {
2600         PQExpBufferData buf,
2601                                 title;
2602         PGresult   *res;
2603         printQueryOpt myopt = pset.popt;
2604
2605         initPQExpBuffer(&buf);
2606
2607         printfPQExpBuffer(&buf,
2608                                           "SELECT \n"
2609                                           "  ( SELECT t.alias FROM \n"
2610                                           "    pg_catalog.ts_token_type(c.cfgparser) AS t \n"
2611                                           "    WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
2612                                           "  pg_catalog.btrim( \n"
2613                                   "    ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
2614                                           "           FROM pg_catalog.pg_ts_config_map AS mm \n"
2615                                           "           WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
2616                                           "           ORDER BY mapcfg, maptokentype, mapseqno \n"
2617                                           "    ) :: pg_catalog.text , \n"
2618                                           "  '{}') AS \"%s\" \n"
2619          "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
2620                                           "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
2621                                           "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
2622                                           "ORDER BY 1",
2623                                           gettext_noop("Token"),
2624                                           gettext_noop("Dictionaries"),
2625                                           oid);
2626
2627         res = PSQLexec(buf.data, false);
2628         termPQExpBuffer(&buf);
2629         if (!res)
2630                 return false;
2631
2632         initPQExpBuffer(&title);
2633
2634         if (nspname)
2635                 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
2636                                                   nspname, cfgname);
2637         else
2638                 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
2639                                                   cfgname);
2640
2641         if (pnspname)
2642                 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
2643                                                   pnspname, prsname);
2644         else
2645                 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
2646                                                   prsname);
2647
2648         myopt.nullPrint = NULL;
2649         myopt.title = title.data;
2650         myopt.footers = NULL;
2651         myopt.default_footer = false;
2652         myopt.trans_headers = true;
2653
2654         printQuery(res, &myopt, pset.queryFout, pset.logfile);
2655
2656         termPQExpBuffer(&title);
2657
2658         PQclear(res);
2659         return true;
2660 }