PostgreSQL Source Code  git master
describe.c
Go to the documentation of this file.
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-2018, PostgreSQL Global Development Group
10  *
11  * src/bin/psql/describe.c
12  */
13 #include "postgres_fe.h"
14 
15 #include <ctype.h>
16 
17 #include "catalog/pg_attribute.h"
18 #include "catalog/pg_class.h"
19 #include "catalog/pg_default_acl.h"
20 #include "fe_utils/string_utils.h"
21 
22 #include "common.h"
23 #include "describe.h"
24 #include "fe_utils/mbprint.h"
25 #include "fe_utils/print.h"
26 #include "settings.h"
27 #include "variables.h"
28 
29 
30 static bool describeOneTableDetails(const char *schemaname,
31  const char *relationname,
32  const char *oid,
33  bool verbose);
34 static void add_tablespace_footer(printTableContent *const cont, char relkind,
35  Oid tablespace, const bool newline);
36 static void add_role_attribute(PQExpBuffer buf, const char *const str);
37 static bool listTSParsersVerbose(const char *pattern);
38 static bool describeOneTSParser(const char *oid, const char *nspname,
39  const char *prsname);
40 static bool listTSConfigsVerbose(const char *pattern);
41 static bool describeOneTSConfig(const char *oid, const char *nspname,
42  const char *cfgname,
43  const char *pnspname, const char *prsname);
44 static void printACLColumn(PQExpBuffer buf, const char *colname);
45 static bool listOneExtensionContents(const char *extname, const char *oid);
46 
47 
48 /*----------------
49  * Handlers for various slash commands displaying some sort of list
50  * of things in the database.
51  *
52  * Note: try to format the queries to look nice in -E output.
53  *----------------
54  */
55 
56 
57 /*
58  * \da
59  * Takes an optional regexp to select particular aggregates
60  */
61 bool
62 describeAggregates(const char *pattern, bool verbose, bool showSystem)
63 {
65  PGresult *res;
66  printQueryOpt myopt = pset.popt;
67 
68  initPQExpBuffer(&buf);
69 
70  printfPQExpBuffer(&buf,
71  "SELECT n.nspname as \"%s\",\n"
72  " p.proname AS \"%s\",\n"
73  " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
74  gettext_noop("Schema"),
75  gettext_noop("Name"),
76  gettext_noop("Result data type"));
77 
78  if (pset.sversion >= 80400)
79  appendPQExpBuffer(&buf,
80  " CASE WHEN p.pronargs = 0\n"
81  " THEN CAST('*' AS pg_catalog.text)\n"
82  " ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"
83  " END AS \"%s\",\n",
84  gettext_noop("Argument data types"));
85  else if (pset.sversion >= 80200)
86  appendPQExpBuffer(&buf,
87  " CASE WHEN p.pronargs = 0\n"
88  " THEN CAST('*' AS pg_catalog.text)\n"
89  " ELSE\n"
90  " pg_catalog.array_to_string(ARRAY(\n"
91  " SELECT\n"
92  " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
93  " FROM\n"
94  " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
95  " ), ', ')\n"
96  " END AS \"%s\",\n",
97  gettext_noop("Argument data types"));
98  else
99  appendPQExpBuffer(&buf,
100  " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
101  gettext_noop("Argument data types"));
102 
103  appendPQExpBuffer(&buf,
104  " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
105  "FROM pg_catalog.pg_proc p\n"
106  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
107  "WHERE p.proisagg\n",
108  gettext_noop("Description"));
109 
110  if (!showSystem && !pattern)
111  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
112  " AND n.nspname <> 'information_schema'\n");
113 
114  processSQLNamePattern(pset.db, &buf, pattern, true, false,
115  "n.nspname", "p.proname", NULL,
116  "pg_catalog.pg_function_is_visible(p.oid)");
117 
118  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
119 
120  res = PSQLexec(buf.data);
121  termPQExpBuffer(&buf);
122  if (!res)
123  return false;
124 
125  myopt.nullPrint = NULL;
126  myopt.title = _("List of aggregate functions");
127  myopt.translate_header = true;
128 
129  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
130 
131  PQclear(res);
132  return true;
133 }
134 
135 /*
136  * \dA
137  * Takes an optional regexp to select particular access methods
138  */
139 bool
140 describeAccessMethods(const char *pattern, bool verbose)
141 {
143  PGresult *res;
144  printQueryOpt myopt = pset.popt;
145  static const bool translate_columns[] = {false, true, false, false};
146 
147  if (pset.sversion < 90600)
148  {
149  char sverbuf[32];
150 
151  psql_error("The server (version %s) does not support access methods.\n",
153  sverbuf, sizeof(sverbuf)));
154  return true;
155  }
156 
157  initPQExpBuffer(&buf);
158 
159  printfPQExpBuffer(&buf,
160  "SELECT amname AS \"%s\",\n"
161  " CASE amtype"
162  " WHEN 'i' THEN '%s'"
163  " END AS \"%s\"",
164  gettext_noop("Name"),
165  gettext_noop("Index"),
166  gettext_noop("Type"));
167 
168  if (verbose)
169  {
170  appendPQExpBuffer(&buf,
171  ",\n amhandler AS \"%s\",\n"
172  " pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
173  gettext_noop("Handler"),
174  gettext_noop("Description"));
175  }
176 
178  "\nFROM pg_catalog.pg_am\n");
179 
180  processSQLNamePattern(pset.db, &buf, pattern, false, false,
181  NULL, "amname", NULL,
182  NULL);
183 
184  appendPQExpBufferStr(&buf, "ORDER BY 1;");
185 
186  res = PSQLexec(buf.data);
187  termPQExpBuffer(&buf);
188  if (!res)
189  return false;
190 
191  myopt.nullPrint = NULL;
192  myopt.title = _("List of access methods");
193  myopt.translate_header = true;
194  myopt.translate_columns = translate_columns;
195  myopt.n_translate_columns = lengthof(translate_columns);
196 
197  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
198 
199  PQclear(res);
200  return true;
201 }
202 
203 /*
204  * \db
205  * Takes an optional regexp to select particular tablespaces
206  */
207 bool
208 describeTablespaces(const char *pattern, bool verbose)
209 {
211  PGresult *res;
212  printQueryOpt myopt = pset.popt;
213 
214  if (pset.sversion < 80000)
215  {
216  char sverbuf[32];
217 
218  psql_error("The server (version %s) does not support tablespaces.\n",
220  sverbuf, sizeof(sverbuf)));
221  return true;
222  }
223 
224  initPQExpBuffer(&buf);
225 
226  if (pset.sversion >= 90200)
227  printfPQExpBuffer(&buf,
228  "SELECT spcname AS \"%s\",\n"
229  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
230  " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
231  gettext_noop("Name"),
232  gettext_noop("Owner"),
233  gettext_noop("Location"));
234  else
235  printfPQExpBuffer(&buf,
236  "SELECT spcname AS \"%s\",\n"
237  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
238  " spclocation AS \"%s\"",
239  gettext_noop("Name"),
240  gettext_noop("Owner"),
241  gettext_noop("Location"));
242 
243  if (verbose)
244  {
245  appendPQExpBufferStr(&buf, ",\n ");
246  printACLColumn(&buf, "spcacl");
247  }
248 
249  if (verbose && pset.sversion >= 90000)
250  appendPQExpBuffer(&buf,
251  ",\n spcoptions AS \"%s\"",
252  gettext_noop("Options"));
253 
254  if (verbose && pset.sversion >= 90200)
255  appendPQExpBuffer(&buf,
256  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\"",
257  gettext_noop("Size"));
258 
259  if (verbose && pset.sversion >= 80200)
260  appendPQExpBuffer(&buf,
261  ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
262  gettext_noop("Description"));
263 
265  "\nFROM pg_catalog.pg_tablespace\n");
266 
267  processSQLNamePattern(pset.db, &buf, pattern, false, false,
268  NULL, "spcname", NULL,
269  NULL);
270 
271  appendPQExpBufferStr(&buf, "ORDER BY 1;");
272 
273  res = PSQLexec(buf.data);
274  termPQExpBuffer(&buf);
275  if (!res)
276  return false;
277 
278  myopt.nullPrint = NULL;
279  myopt.title = _("List of tablespaces");
280  myopt.translate_header = true;
281 
282  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
283 
284  PQclear(res);
285  return true;
286 }
287 
288 
289 /*
290  * \df
291  * Takes an optional regexp to select particular functions.
292  *
293  * As with \d, you can specify the kinds of functions you want:
294  *
295  * a for aggregates
296  * n for normal
297  * t for trigger
298  * w for window
299  *
300  * and you can mix and match these in any order.
301  */
302 bool
303 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
304 {
305  bool showAggregate = strchr(functypes, 'a') != NULL;
306  bool showNormal = strchr(functypes, 'n') != NULL;
307  bool showTrigger = strchr(functypes, 't') != NULL;
308  bool showWindow = strchr(functypes, 'w') != NULL;
309  bool have_where;
311  PGresult *res;
312  printQueryOpt myopt = pset.popt;
313  static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, false, false, false, false};
314 
315  /* No "Parallel" column before 9.6 */
316  static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, false, false, false, false};
317 
318  if (strlen(functypes) != strspn(functypes, "antwS+"))
319  {
320  psql_error("\\df only takes [antwS+] as options\n");
321  return true;
322  }
323 
324  if (showWindow && pset.sversion < 80400)
325  {
326  char sverbuf[32];
327 
328  psql_error("\\df does not take a \"w\" option with server version %s\n",
330  sverbuf, sizeof(sverbuf)));
331  return true;
332  }
333 
334  if (!showAggregate && !showNormal && !showTrigger && !showWindow)
335  {
336  showAggregate = showNormal = showTrigger = true;
337  if (pset.sversion >= 80400)
338  showWindow = true;
339  }
340 
341  initPQExpBuffer(&buf);
342 
343  printfPQExpBuffer(&buf,
344  "SELECT n.nspname as \"%s\",\n"
345  " p.proname as \"%s\",\n",
346  gettext_noop("Schema"),
347  gettext_noop("Name"));
348 
349  if (pset.sversion >= 80400)
350  appendPQExpBuffer(&buf,
351  " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
352  " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
353  " CASE\n"
354  " WHEN p.proisagg THEN '%s'\n"
355  " WHEN p.proiswindow THEN '%s'\n"
356  " WHEN p.prorettype = 0 THEN '%s'\n"
357  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
358  " ELSE '%s'\n"
359  " END as \"%s\"",
360  gettext_noop("Result data type"),
361  gettext_noop("Argument data types"),
362  /* translator: "agg" is short for "aggregate" */
363  gettext_noop("agg"),
364  gettext_noop("window"),
365  gettext_noop("proc"),
366  gettext_noop("trigger"),
367  gettext_noop("func"),
368  gettext_noop("Type"));
369  else if (pset.sversion >= 80100)
370  appendPQExpBuffer(&buf,
371  " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
372  " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
373  " CASE WHEN proallargtypes IS NOT NULL THEN\n"
374  " pg_catalog.array_to_string(ARRAY(\n"
375  " SELECT\n"
376  " CASE\n"
377  " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
378  " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
379  " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
380  " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
381  " END ||\n"
382  " CASE\n"
383  " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
384  " ELSE p.proargnames[s.i] || ' '\n"
385  " END ||\n"
386  " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
387  " FROM\n"
388  " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
389  " ), ', ')\n"
390  " ELSE\n"
391  " pg_catalog.array_to_string(ARRAY(\n"
392  " SELECT\n"
393  " CASE\n"
394  " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
395  " ELSE p.proargnames[s.i+1] || ' '\n"
396  " END ||\n"
397  " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
398  " FROM\n"
399  " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
400  " ), ', ')\n"
401  " END AS \"%s\",\n"
402  " CASE\n"
403  " WHEN p.proisagg THEN '%s'\n"
404  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
405  " ELSE '%s'\n"
406  " END AS \"%s\"",
407  gettext_noop("Result data type"),
408  gettext_noop("Argument data types"),
409  /* translator: "agg" is short for "aggregate" */
410  gettext_noop("agg"),
411  gettext_noop("trigger"),
412  gettext_noop("func"),
413  gettext_noop("Type"));
414  else
415  appendPQExpBuffer(&buf,
416  " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
417  " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
418  " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
419  " CASE\n"
420  " WHEN p.proisagg THEN '%s'\n"
421  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
422  " ELSE '%s'\n"
423  " END AS \"%s\"",
424  gettext_noop("Result data type"),
425  gettext_noop("Argument data types"),
426  /* translator: "agg" is short for "aggregate" */
427  gettext_noop("agg"),
428  gettext_noop("trigger"),
429  gettext_noop("func"),
430  gettext_noop("Type"));
431 
432  if (verbose)
433  {
434  appendPQExpBuffer(&buf,
435  ",\n CASE\n"
436  " WHEN p.provolatile = 'i' THEN '%s'\n"
437  " WHEN p.provolatile = 's' THEN '%s'\n"
438  " WHEN p.provolatile = 'v' THEN '%s'\n"
439  " END as \"%s\"",
440  gettext_noop("immutable"),
441  gettext_noop("stable"),
442  gettext_noop("volatile"),
443  gettext_noop("Volatility"));
444  if (pset.sversion >= 90600)
445  appendPQExpBuffer(&buf,
446  ",\n CASE\n"
447  " WHEN p.proparallel = 'r' THEN '%s'\n"
448  " WHEN p.proparallel = 's' THEN '%s'\n"
449  " WHEN p.proparallel = 'u' THEN '%s'\n"
450  " END as \"%s\"",
451  gettext_noop("restricted"),
452  gettext_noop("safe"),
453  gettext_noop("unsafe"),
454  gettext_noop("Parallel"));
455  appendPQExpBuffer(&buf,
456  ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
457  ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\"",
458  gettext_noop("Owner"),
459  gettext_noop("definer"),
460  gettext_noop("invoker"),
461  gettext_noop("Security"));
462  appendPQExpBufferStr(&buf, ",\n ");
463  printACLColumn(&buf, "p.proacl");
464  appendPQExpBuffer(&buf,
465  ",\n l.lanname as \"%s\""
466  ",\n p.prosrc as \"%s\""
467  ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
468  gettext_noop("Language"),
469  gettext_noop("Source code"),
470  gettext_noop("Description"));
471  }
472 
474  "\nFROM pg_catalog.pg_proc p"
475  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
476 
477  if (verbose)
479  " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
480 
481  have_where = false;
482 
483  /* filter by function type, if requested */
484  if (showNormal && showAggregate && showTrigger && showWindow)
485  /* Do nothing */ ;
486  else if (showNormal)
487  {
488  if (!showAggregate)
489  {
490  if (have_where)
491  appendPQExpBufferStr(&buf, " AND ");
492  else
493  {
494  appendPQExpBufferStr(&buf, "WHERE ");
495  have_where = true;
496  }
497  appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
498  }
499  if (!showTrigger)
500  {
501  if (have_where)
502  appendPQExpBufferStr(&buf, " AND ");
503  else
504  {
505  appendPQExpBufferStr(&buf, "WHERE ");
506  have_where = true;
507  }
508  appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
509  }
510  if (!showWindow && pset.sversion >= 80400)
511  {
512  if (have_where)
513  appendPQExpBufferStr(&buf, " AND ");
514  else
515  {
516  appendPQExpBufferStr(&buf, "WHERE ");
517  have_where = true;
518  }
519  appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
520  }
521  }
522  else
523  {
524  bool needs_or = false;
525 
526  appendPQExpBufferStr(&buf, "WHERE (\n ");
527  have_where = true;
528  /* Note: at least one of these must be true ... */
529  if (showAggregate)
530  {
531  appendPQExpBufferStr(&buf, "p.proisagg\n");
532  needs_or = true;
533  }
534  if (showTrigger)
535  {
536  if (needs_or)
537  appendPQExpBufferStr(&buf, " OR ");
539  "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
540  needs_or = true;
541  }
542  if (showWindow)
543  {
544  if (needs_or)
545  appendPQExpBufferStr(&buf, " OR ");
546  appendPQExpBufferStr(&buf, "p.proiswindow\n");
547  needs_or = true;
548  }
549  appendPQExpBufferStr(&buf, " )\n");
550  }
551 
552  processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
553  "n.nspname", "p.proname", NULL,
554  "pg_catalog.pg_function_is_visible(p.oid)");
555 
556  if (!showSystem && !pattern)
557  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
558  " AND n.nspname <> 'information_schema'\n");
559 
560  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
561 
562  res = PSQLexec(buf.data);
563  termPQExpBuffer(&buf);
564  if (!res)
565  return false;
566 
567  myopt.nullPrint = NULL;
568  myopt.title = _("List of functions");
569  myopt.translate_header = true;
570  if (pset.sversion >= 90600)
571  {
572  myopt.translate_columns = translate_columns;
573  myopt.n_translate_columns = lengthof(translate_columns);
574  }
575  else
576  {
577  myopt.translate_columns = translate_columns_pre_96;
578  myopt.n_translate_columns = lengthof(translate_columns_pre_96);
579  }
580 
581  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
582 
583  PQclear(res);
584  return true;
585 }
586 
587 
588 
589 /*
590  * \dT
591  * describe types
592  */
593 bool
594 describeTypes(const char *pattern, bool verbose, bool showSystem)
595 {
597  PGresult *res;
598  printQueryOpt myopt = pset.popt;
599 
600  initPQExpBuffer(&buf);
601 
602  printfPQExpBuffer(&buf,
603  "SELECT n.nspname as \"%s\",\n"
604  " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
605  gettext_noop("Schema"),
606  gettext_noop("Name"));
607  if (verbose)
608  appendPQExpBuffer(&buf,
609  " t.typname AS \"%s\",\n"
610  " CASE WHEN t.typrelid != 0\n"
611  " THEN CAST('tuple' AS pg_catalog.text)\n"
612  " WHEN t.typlen < 0\n"
613  " THEN CAST('var' AS pg_catalog.text)\n"
614  " ELSE CAST(t.typlen AS pg_catalog.text)\n"
615  " END AS \"%s\",\n",
616  gettext_noop("Internal name"),
617  gettext_noop("Size"));
618  if (verbose && pset.sversion >= 80300)
619  {
621  " pg_catalog.array_to_string(\n"
622  " ARRAY(\n"
623  " SELECT e.enumlabel\n"
624  " FROM pg_catalog.pg_enum e\n"
625  " WHERE e.enumtypid = t.oid\n");
626 
627  if (pset.sversion >= 90100)
629  " ORDER BY e.enumsortorder\n");
630  else
632  " ORDER BY e.oid\n");
633 
634  appendPQExpBuffer(&buf,
635  " ),\n"
636  " E'\\n'\n"
637  " ) AS \"%s\",\n",
638  gettext_noop("Elements"));
639  }
640  if (verbose)
641  {
642  appendPQExpBuffer(&buf,
643  " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
644  gettext_noop("Owner"));
645  }
646  if (verbose && pset.sversion >= 90200)
647  {
648  printACLColumn(&buf, "t.typacl");
649  appendPQExpBufferStr(&buf, ",\n ");
650  }
651 
652  appendPQExpBuffer(&buf,
653  " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
654  gettext_noop("Description"));
655 
656  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
657  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
658 
659  /*
660  * do not include complex types (typrelid!=0) unless they are standalone
661  * composite types
662  */
663  appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
664  appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
665  " FROM pg_catalog.pg_class c "
666  "WHERE c.oid = t.typrelid))\n");
667 
668  /*
669  * do not include array types (before 8.3 we have to use the assumption
670  * that their names start with underscore)
671  */
672  if (pset.sversion >= 80300)
673  appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
674  else
675  appendPQExpBufferStr(&buf, " AND t.typname !~ '^_'\n");
676 
677  if (!showSystem && !pattern)
678  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
679  " AND n.nspname <> 'information_schema'\n");
680 
681  /* Match name pattern against either internal or external name */
682  processSQLNamePattern(pset.db, &buf, pattern, true, false,
683  "n.nspname", "t.typname",
684  "pg_catalog.format_type(t.oid, NULL)",
685  "pg_catalog.pg_type_is_visible(t.oid)");
686 
687  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
688 
689  res = PSQLexec(buf.data);
690  termPQExpBuffer(&buf);
691  if (!res)
692  return false;
693 
694  myopt.nullPrint = NULL;
695  myopt.title = _("List of data types");
696  myopt.translate_header = true;
697 
698  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
699 
700  PQclear(res);
701  return true;
702 }
703 
704 
705 /*
706  * \do
707  * Describe operators
708  */
709 bool
710 describeOperators(const char *pattern, bool verbose, bool showSystem)
711 {
713  PGresult *res;
714  printQueryOpt myopt = pset.popt;
715 
716  initPQExpBuffer(&buf);
717 
718  /*
719  * Note: before Postgres 9.1, we did not assign comments to any built-in
720  * operators, preferring to let the comment on the underlying function
721  * suffice. The coalesce() on the obj_description() calls below supports
722  * this convention by providing a fallback lookup of a comment on the
723  * operator's function. As of 9.1 there is a policy that every built-in
724  * operator should have a comment; so the coalesce() is no longer
725  * necessary so far as built-in operators are concerned. We keep it
726  * anyway, for now, because (1) third-party modules may still be following
727  * the old convention, and (2) we'd need to do it anyway when talking to a
728  * pre-9.1 server.
729  */
730 
731  printfPQExpBuffer(&buf,
732  "SELECT n.nspname as \"%s\",\n"
733  " o.oprname AS \"%s\",\n"
734  " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
735  " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
736  " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
737  gettext_noop("Schema"),
738  gettext_noop("Name"),
739  gettext_noop("Left arg type"),
740  gettext_noop("Right arg type"),
741  gettext_noop("Result type"));
742 
743  if (verbose)
744  appendPQExpBuffer(&buf,
745  " o.oprcode AS \"%s\",\n",
746  gettext_noop("Function"));
747 
748  appendPQExpBuffer(&buf,
749  " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
750  " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
751  "FROM pg_catalog.pg_operator o\n"
752  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
753  gettext_noop("Description"));
754 
755  if (!showSystem && !pattern)
756  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
757  " AND n.nspname <> 'information_schema'\n");
758 
759  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
760  "n.nspname", "o.oprname", NULL,
761  "pg_catalog.pg_operator_is_visible(o.oid)");
762 
763  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
764 
765  res = PSQLexec(buf.data);
766  termPQExpBuffer(&buf);
767  if (!res)
768  return false;
769 
770  myopt.nullPrint = NULL;
771  myopt.title = _("List of operators");
772  myopt.translate_header = true;
773 
774  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
775 
776  PQclear(res);
777  return true;
778 }
779 
780 
781 /*
782  * listAllDbs
783  *
784  * for \l, \list, and -l switch
785  */
786 bool
787 listAllDbs(const char *pattern, bool verbose)
788 {
789  PGresult *res;
791  printQueryOpt myopt = pset.popt;
792 
793  initPQExpBuffer(&buf);
794 
795  printfPQExpBuffer(&buf,
796  "SELECT d.datname as \"%s\",\n"
797  " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
798  " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
799  gettext_noop("Name"),
800  gettext_noop("Owner"),
801  gettext_noop("Encoding"));
802  if (pset.sversion >= 80400)
803  appendPQExpBuffer(&buf,
804  " d.datcollate as \"%s\",\n"
805  " d.datctype as \"%s\",\n",
806  gettext_noop("Collate"),
807  gettext_noop("Ctype"));
808  appendPQExpBufferStr(&buf, " ");
809  printACLColumn(&buf, "d.datacl");
810  if (verbose && pset.sversion >= 80200)
811  appendPQExpBuffer(&buf,
812  ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
813  " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
814  " ELSE 'No Access'\n"
815  " END as \"%s\"",
816  gettext_noop("Size"));
817  if (verbose && pset.sversion >= 80000)
818  appendPQExpBuffer(&buf,
819  ",\n t.spcname as \"%s\"",
820  gettext_noop("Tablespace"));
821  if (verbose && pset.sversion >= 80200)
822  appendPQExpBuffer(&buf,
823  ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
824  gettext_noop("Description"));
826  "\nFROM pg_catalog.pg_database d\n");
827  if (verbose && pset.sversion >= 80000)
829  " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
830 
831  if (pattern)
832  processSQLNamePattern(pset.db, &buf, pattern, false, false,
833  NULL, "d.datname", NULL, NULL);
834 
835  appendPQExpBufferStr(&buf, "ORDER BY 1;");
836  res = PSQLexec(buf.data);
837  termPQExpBuffer(&buf);
838  if (!res)
839  return false;
840 
841  myopt.nullPrint = NULL;
842  myopt.title = _("List of databases");
843  myopt.translate_header = true;
844 
845  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
846 
847  PQclear(res);
848  return true;
849 }
850 
851 
852 /*
853  * List Tables' Grant/Revoke Permissions
854  * \z (now also \dp -- perhaps more mnemonic)
855  */
856 bool
857 permissionsList(const char *pattern)
858 {
860  PGresult *res;
861  printQueryOpt myopt = pset.popt;
862  static const bool translate_columns[] = {false, false, true, false, false, false};
863 
864  initPQExpBuffer(&buf);
865 
866  /*
867  * we ignore indexes and toast tables since they have no meaningful rights
868  */
869  printfPQExpBuffer(&buf,
870  "SELECT n.nspname as \"%s\",\n"
871  " c.relname as \"%s\",\n"
872  " CASE c.relkind"
873  " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
874  " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
875  " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
876  " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
877  " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
878  " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
879  " END as \"%s\",\n"
880  " ",
881  gettext_noop("Schema"),
882  gettext_noop("Name"),
883  gettext_noop("table"),
884  gettext_noop("view"),
885  gettext_noop("materialized view"),
886  gettext_noop("sequence"),
887  gettext_noop("foreign table"),
888  gettext_noop("table"), /* partitioned table */
889  gettext_noop("Type"));
890 
891  printACLColumn(&buf, "c.relacl");
892 
893  if (pset.sversion >= 80400)
894  appendPQExpBuffer(&buf,
895  ",\n pg_catalog.array_to_string(ARRAY(\n"
896  " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
897  " FROM pg_catalog.pg_attribute a\n"
898  " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
899  " ), E'\\n') AS \"%s\"",
900  gettext_noop("Column privileges"));
901 
902  if (pset.sversion >= 90500 && pset.sversion < 100000)
903  appendPQExpBuffer(&buf,
904  ",\n pg_catalog.array_to_string(ARRAY(\n"
905  " SELECT polname\n"
906  " || CASE WHEN polcmd != '*' THEN\n"
907  " E' (' || polcmd || E'):'\n"
908  " ELSE E':'\n"
909  " END\n"
910  " || CASE WHEN polqual IS NOT NULL THEN\n"
911  " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
912  " ELSE E''\n"
913  " END\n"
914  " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
915  " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
916  " ELSE E''\n"
917  " END"
918  " || CASE WHEN polroles <> '{0}' THEN\n"
919  " E'\\n to: ' || pg_catalog.array_to_string(\n"
920  " ARRAY(\n"
921  " SELECT rolname\n"
922  " FROM pg_catalog.pg_roles\n"
923  " WHERE oid = ANY (polroles)\n"
924  " ORDER BY 1\n"
925  " ), E', ')\n"
926  " ELSE E''\n"
927  " END\n"
928  " FROM pg_catalog.pg_policy pol\n"
929  " WHERE polrelid = c.oid), E'\\n')\n"
930  " AS \"%s\"",
931  gettext_noop("Policies"));
932 
933  if (pset.sversion >= 100000)
934  appendPQExpBuffer(&buf,
935  ",\n pg_catalog.array_to_string(ARRAY(\n"
936  " SELECT polname\n"
937  " || CASE WHEN NOT polpermissive THEN\n"
938  " E' (RESTRICTIVE)'\n"
939  " ELSE '' END\n"
940  " || CASE WHEN polcmd != '*' THEN\n"
941  " E' (' || polcmd || E'):'\n"
942  " ELSE E':'\n"
943  " END\n"
944  " || CASE WHEN polqual IS NOT NULL THEN\n"
945  " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
946  " ELSE E''\n"
947  " END\n"
948  " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
949  " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
950  " ELSE E''\n"
951  " END"
952  " || CASE WHEN polroles <> '{0}' THEN\n"
953  " E'\\n to: ' || pg_catalog.array_to_string(\n"
954  " ARRAY(\n"
955  " SELECT rolname\n"
956  " FROM pg_catalog.pg_roles\n"
957  " WHERE oid = ANY (polroles)\n"
958  " ORDER BY 1\n"
959  " ), E', ')\n"
960  " ELSE E''\n"
961  " END\n"
962  " FROM pg_catalog.pg_policy pol\n"
963  " WHERE polrelid = c.oid), E'\\n')\n"
964  " AS \"%s\"",
965  gettext_noop("Policies"));
966 
967  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
968  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
969  "WHERE c.relkind IN ("
976 
977  /*
978  * Unless a schema pattern is specified, we suppress system and temp
979  * tables, since they normally aren't very interesting from a permissions
980  * point of view. You can see 'em by explicit request though, eg with \z
981  * pg_catalog.*
982  */
983  processSQLNamePattern(pset.db, &buf, pattern, true, false,
984  "n.nspname", "c.relname", NULL,
985  "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
986 
987  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
988 
989  res = PSQLexec(buf.data);
990  if (!res)
991  {
992  termPQExpBuffer(&buf);
993  return false;
994  }
995 
996  myopt.nullPrint = NULL;
997  printfPQExpBuffer(&buf, _("Access privileges"));
998  myopt.title = buf.data;
999  myopt.translate_header = true;
1000  myopt.translate_columns = translate_columns;
1001  myopt.n_translate_columns = lengthof(translate_columns);
1002 
1003  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1004 
1005  termPQExpBuffer(&buf);
1006  PQclear(res);
1007  return true;
1008 }
1009 
1010 
1011 /*
1012  * \ddp
1013  *
1014  * List Default ACLs. The pattern can match either schema or role name.
1015  */
1016 bool
1017 listDefaultACLs(const char *pattern)
1018 {
1020  PGresult *res;
1021  printQueryOpt myopt = pset.popt;
1022  static const bool translate_columns[] = {false, false, true, false};
1023 
1024  if (pset.sversion < 90000)
1025  {
1026  char sverbuf[32];
1027 
1028  psql_error("The server (version %s) does not support altering default privileges.\n",
1030  sverbuf, sizeof(sverbuf)));
1031  return true;
1032  }
1033 
1034  initPQExpBuffer(&buf);
1035 
1036  printfPQExpBuffer(&buf,
1037  "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1038  " n.nspname AS \"%s\",\n"
1039  " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
1040  " ",
1041  gettext_noop("Owner"),
1042  gettext_noop("Schema"),
1044  gettext_noop("table"),
1046  gettext_noop("sequence"),
1048  gettext_noop("function"),
1050  gettext_noop("type"),
1052  gettext_noop("schema"),
1053  gettext_noop("Type"));
1054 
1055  printACLColumn(&buf, "d.defaclacl");
1056 
1057  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1058  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1059 
1060  processSQLNamePattern(pset.db, &buf, pattern, false, false,
1061  NULL,
1062  "n.nspname",
1063  "pg_catalog.pg_get_userbyid(d.defaclrole)",
1064  NULL);
1065 
1066  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1067 
1068  res = PSQLexec(buf.data);
1069  if (!res)
1070  {
1071  termPQExpBuffer(&buf);
1072  return false;
1073  }
1074 
1075  myopt.nullPrint = NULL;
1076  printfPQExpBuffer(&buf, _("Default access privileges"));
1077  myopt.title = buf.data;
1078  myopt.translate_header = true;
1079  myopt.translate_columns = translate_columns;
1080  myopt.n_translate_columns = lengthof(translate_columns);
1081 
1082  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1083 
1084  termPQExpBuffer(&buf);
1085  PQclear(res);
1086  return true;
1087 }
1088 
1089 
1090 /*
1091  * Get object comments
1092  *
1093  * \dd [foo]
1094  *
1095  * Note: This command only lists comments for object types which do not have
1096  * their comments displayed by their own backslash commands. The following
1097  * types of objects will be displayed: constraint, operator class,
1098  * operator family, rule, and trigger.
1099  *
1100  */
1101 bool
1102 objectDescription(const char *pattern, bool showSystem)
1103 {
1105  PGresult *res;
1106  printQueryOpt myopt = pset.popt;
1107  static const bool translate_columns[] = {false, false, true, false};
1108 
1109  initPQExpBuffer(&buf);
1110 
1111  appendPQExpBuffer(&buf,
1112  "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
1113  "FROM (\n",
1114  gettext_noop("Schema"),
1115  gettext_noop("Name"),
1116  gettext_noop("Object"),
1117  gettext_noop("Description"));
1118 
1119  /* Table constraint descriptions */
1120  appendPQExpBuffer(&buf,
1121  " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1122  " n.nspname as nspname,\n"
1123  " CAST(pgc.conname AS pg_catalog.text) as name,"
1124  " CAST('%s' AS pg_catalog.text) as object\n"
1125  " FROM pg_catalog.pg_constraint pgc\n"
1126  " JOIN pg_catalog.pg_class c "
1127  "ON c.oid = pgc.conrelid\n"
1128  " LEFT JOIN pg_catalog.pg_namespace n "
1129  " ON n.oid = c.relnamespace\n",
1130  gettext_noop("table constraint"));
1131 
1132  if (!showSystem && !pattern)
1133  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1134  " AND n.nspname <> 'information_schema'\n");
1135 
1136  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
1137  false, "n.nspname", "pgc.conname", NULL,
1138  "pg_catalog.pg_table_is_visible(c.oid)");
1139 
1140  /* Domain constraint descriptions */
1141  appendPQExpBuffer(&buf,
1142  "UNION ALL\n"
1143  " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1144  " n.nspname as nspname,\n"
1145  " CAST(pgc.conname AS pg_catalog.text) as name,"
1146  " CAST('%s' AS pg_catalog.text) as object\n"
1147  " FROM pg_catalog.pg_constraint pgc\n"
1148  " JOIN pg_catalog.pg_type t "
1149  "ON t.oid = pgc.contypid\n"
1150  " LEFT JOIN pg_catalog.pg_namespace n "
1151  " ON n.oid = t.typnamespace\n",
1152  gettext_noop("domain constraint"));
1153 
1154  if (!showSystem && !pattern)
1155  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1156  " AND n.nspname <> 'information_schema'\n");
1157 
1158  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
1159  false, "n.nspname", "pgc.conname", NULL,
1160  "pg_catalog.pg_type_is_visible(t.oid)");
1161 
1162 
1163  /*
1164  * pg_opclass.opcmethod only available in 8.3+
1165  */
1166  if (pset.sversion >= 80300)
1167  {
1168  /* Operator class descriptions */
1169  appendPQExpBuffer(&buf,
1170  "UNION ALL\n"
1171  " SELECT o.oid as oid, o.tableoid as tableoid,\n"
1172  " n.nspname as nspname,\n"
1173  " CAST(o.opcname AS pg_catalog.text) as name,\n"
1174  " CAST('%s' AS pg_catalog.text) as object\n"
1175  " FROM pg_catalog.pg_opclass o\n"
1176  " JOIN pg_catalog.pg_am am ON "
1177  "o.opcmethod = am.oid\n"
1178  " JOIN pg_catalog.pg_namespace n ON "
1179  "n.oid = o.opcnamespace\n",
1180  gettext_noop("operator class"));
1181 
1182  if (!showSystem && !pattern)
1183  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1184  " AND n.nspname <> 'information_schema'\n");
1185 
1186  processSQLNamePattern(pset.db, &buf, pattern, true, false,
1187  "n.nspname", "o.opcname", NULL,
1188  "pg_catalog.pg_opclass_is_visible(o.oid)");
1189  }
1190 
1191  /*
1192  * although operator family comments have been around since 8.3,
1193  * pg_opfamily_is_visible is only available in 9.2+
1194  */
1195  if (pset.sversion >= 90200)
1196  {
1197  /* Operator family descriptions */
1198  appendPQExpBuffer(&buf,
1199  "UNION ALL\n"
1200  " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1201  " n.nspname as nspname,\n"
1202  " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1203  " CAST('%s' AS pg_catalog.text) as object\n"
1204  " FROM pg_catalog.pg_opfamily opf\n"
1205  " JOIN pg_catalog.pg_am am "
1206  "ON opf.opfmethod = am.oid\n"
1207  " JOIN pg_catalog.pg_namespace n "
1208  "ON opf.opfnamespace = n.oid\n",
1209  gettext_noop("operator family"));
1210 
1211  if (!showSystem && !pattern)
1212  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1213  " AND n.nspname <> 'information_schema'\n");
1214 
1215  processSQLNamePattern(pset.db, &buf, pattern, true, false,
1216  "n.nspname", "opf.opfname", NULL,
1217  "pg_catalog.pg_opfamily_is_visible(opf.oid)");
1218  }
1219 
1220  /* Rule descriptions (ignore rules for views) */
1221  appendPQExpBuffer(&buf,
1222  "UNION ALL\n"
1223  " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1224  " n.nspname as nspname,\n"
1225  " CAST(r.rulename AS pg_catalog.text) as name,"
1226  " CAST('%s' AS pg_catalog.text) as object\n"
1227  " FROM pg_catalog.pg_rewrite r\n"
1228  " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1229  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1230  " WHERE r.rulename != '_RETURN'\n",
1231  gettext_noop("rule"));
1232 
1233  if (!showSystem && !pattern)
1234  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1235  " AND n.nspname <> 'information_schema'\n");
1236 
1237  processSQLNamePattern(pset.db, &buf, pattern, true, false,
1238  "n.nspname", "r.rulename", NULL,
1239  "pg_catalog.pg_table_is_visible(c.oid)");
1240 
1241  /* Trigger descriptions */
1242  appendPQExpBuffer(&buf,
1243  "UNION ALL\n"
1244  " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1245  " n.nspname as nspname,\n"
1246  " CAST(t.tgname AS pg_catalog.text) as name,"
1247  " CAST('%s' AS pg_catalog.text) as object\n"
1248  " FROM pg_catalog.pg_trigger t\n"
1249  " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1250  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1251  gettext_noop("trigger"));
1252 
1253  if (!showSystem && !pattern)
1254  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1255  " AND n.nspname <> 'information_schema'\n");
1256 
1257  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1258  "n.nspname", "t.tgname", NULL,
1259  "pg_catalog.pg_table_is_visible(c.oid)");
1260 
1261  appendPQExpBufferStr(&buf,
1262  ") AS tt\n"
1263  " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1264 
1265  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1266 
1267  res = PSQLexec(buf.data);
1268  termPQExpBuffer(&buf);
1269  if (!res)
1270  return false;
1271 
1272  myopt.nullPrint = NULL;
1273  myopt.title = _("Object descriptions");
1274  myopt.translate_header = true;
1275  myopt.translate_columns = translate_columns;
1276  myopt.n_translate_columns = lengthof(translate_columns);
1277 
1278  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1279 
1280  PQclear(res);
1281  return true;
1282 }
1283 
1284 
1285 /*
1286  * describeTableDetails (for \d)
1287  *
1288  * This routine finds the tables to be displayed, and calls
1289  * describeOneTableDetails for each one.
1290  *
1291  * verbose: if true, this is \d+
1292  */
1293 bool
1294 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1295 {
1297  PGresult *res;
1298  int i;
1299 
1300  initPQExpBuffer(&buf);
1301 
1302  printfPQExpBuffer(&buf,
1303  "SELECT c.oid,\n"
1304  " n.nspname,\n"
1305  " c.relname\n"
1306  "FROM pg_catalog.pg_class c\n"
1307  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1308 
1309  if (!showSystem && !pattern)
1310  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1311  " AND n.nspname <> 'information_schema'\n");
1312 
1313  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1314  "n.nspname", "c.relname", NULL,
1315  "pg_catalog.pg_table_is_visible(c.oid)");
1316 
1317  appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1318 
1319  res = PSQLexec(buf.data);
1320  termPQExpBuffer(&buf);
1321  if (!res)
1322  return false;
1323 
1324  if (PQntuples(res) == 0)
1325  {
1326  if (!pset.quiet)
1327  {
1328  if (pattern)
1329  psql_error("Did not find any relation named \"%s\".\n",
1330  pattern);
1331  else
1332  psql_error("Did not find any relations.\n");
1333  }
1334  PQclear(res);
1335  return false;
1336  }
1337 
1338  for (i = 0; i < PQntuples(res); i++)
1339  {
1340  const char *oid;
1341  const char *nspname;
1342  const char *relname;
1343 
1344  oid = PQgetvalue(res, i, 0);
1345  nspname = PQgetvalue(res, i, 1);
1346  relname = PQgetvalue(res, i, 2);
1347 
1348  if (!describeOneTableDetails(nspname, relname, oid, verbose))
1349  {
1350  PQclear(res);
1351  return false;
1352  }
1353  if (cancel_pressed)
1354  {
1355  PQclear(res);
1356  return false;
1357  }
1358  }
1359 
1360  PQclear(res);
1361  return true;
1362 }
1363 
1364 /*
1365  * describeOneTableDetails (for \d)
1366  *
1367  * Unfortunately, the information presented here is so complicated that it
1368  * cannot be done in a single query. So we have to assemble the printed table
1369  * by hand and pass it to the underlying printTable() function.
1370  */
1371 static bool
1372 describeOneTableDetails(const char *schemaname,
1373  const char *relationname,
1374  const char *oid,
1375  bool verbose)
1376 {
1378  PGresult *res = NULL;
1379  printTableOpt myopt = pset.popt.topt;
1380  printTableContent cont;
1381  bool printTableInitialized = false;
1382  int i;
1383  char *view_def = NULL;
1384  char *headers[11];
1385  PQExpBufferData title;
1387  int cols;
1388  int numrows = 0;
1389  struct
1390  {
1391  int16 checks;
1392  char relkind;
1393  bool hasindex;
1394  bool hasrules;
1395  bool hastriggers;
1396  bool rowsecurity;
1397  bool forcerowsecurity;
1398  bool hasoids;
1399  Oid tablespace;
1400  char *reloptions;
1401  char *reloftype;
1402  char relpersistence;
1403  char relreplident;
1404  } tableinfo;
1405  bool show_column_details = false;
1406  bool retval;
1407 
1408  retval = false;
1409 
1410  myopt.default_footer = false;
1411  /* This output looks confusing in expanded mode. */
1412  myopt.expanded = false;
1413 
1414  initPQExpBuffer(&buf);
1415  initPQExpBuffer(&title);
1416  initPQExpBuffer(&tmpbuf);
1417 
1418  /* Get general table info */
1419  if (pset.sversion >= 90500)
1420  {
1421  printfPQExpBuffer(&buf,
1422  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1423  "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1424  "c.relhasoids, %s, c.reltablespace, "
1425  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1426  "c.relpersistence, c.relreplident\n"
1427  "FROM pg_catalog.pg_class c\n "
1428  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1429  "WHERE c.oid = '%s';",
1430  (verbose ?
1431  "pg_catalog.array_to_string(c.reloptions || "
1432  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1433  : "''"),
1434  oid);
1435  }
1436  else if (pset.sversion >= 90400)
1437  {
1438  printfPQExpBuffer(&buf,
1439  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1440  "c.relhastriggers, false, false, c.relhasoids, "
1441  "%s, c.reltablespace, "
1442  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1443  "c.relpersistence, c.relreplident\n"
1444  "FROM pg_catalog.pg_class c\n "
1445  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1446  "WHERE c.oid = '%s';",
1447  (verbose ?
1448  "pg_catalog.array_to_string(c.reloptions || "
1449  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1450  : "''"),
1451  oid);
1452  }
1453  else if (pset.sversion >= 90100)
1454  {
1455  printfPQExpBuffer(&buf,
1456  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1457  "c.relhastriggers, false, false, c.relhasoids, "
1458  "%s, c.reltablespace, "
1459  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1460  "c.relpersistence\n"
1461  "FROM pg_catalog.pg_class c\n "
1462  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1463  "WHERE c.oid = '%s';",
1464  (verbose ?
1465  "pg_catalog.array_to_string(c.reloptions || "
1466  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1467  : "''"),
1468  oid);
1469  }
1470  else if (pset.sversion >= 90000)
1471  {
1472  printfPQExpBuffer(&buf,
1473  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1474  "c.relhastriggers, false, false, c.relhasoids, "
1475  "%s, c.reltablespace, "
1476  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1477  "FROM pg_catalog.pg_class c\n "
1478  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1479  "WHERE c.oid = '%s';",
1480  (verbose ?
1481  "pg_catalog.array_to_string(c.reloptions || "
1482  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1483  : "''"),
1484  oid);
1485  }
1486  else if (pset.sversion >= 80400)
1487  {
1488  printfPQExpBuffer(&buf,
1489  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1490  "c.relhastriggers, false, false, c.relhasoids, "
1491  "%s, c.reltablespace\n"
1492  "FROM pg_catalog.pg_class c\n "
1493  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1494  "WHERE c.oid = '%s';",
1495  (verbose ?
1496  "pg_catalog.array_to_string(c.reloptions || "
1497  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1498  : "''"),
1499  oid);
1500  }
1501  else if (pset.sversion >= 80200)
1502  {
1503  printfPQExpBuffer(&buf,
1504  "SELECT relchecks, relkind, relhasindex, relhasrules, "
1505  "reltriggers <> 0, false, false, relhasoids, "
1506  "%s, reltablespace\n"
1507  "FROM pg_catalog.pg_class WHERE oid = '%s';",
1508  (verbose ?
1509  "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1510  oid);
1511  }
1512  else if (pset.sversion >= 80000)
1513  {
1514  printfPQExpBuffer(&buf,
1515  "SELECT relchecks, relkind, relhasindex, relhasrules, "
1516  "reltriggers <> 0, false, false, relhasoids, "
1517  "'', reltablespace\n"
1518  "FROM pg_catalog.pg_class WHERE oid = '%s';",
1519  oid);
1520  }
1521  else
1522  {
1523  printfPQExpBuffer(&buf,
1524  "SELECT relchecks, relkind, relhasindex, relhasrules, "
1525  "reltriggers <> 0, false, false, relhasoids, "
1526  "'', ''\n"
1527  "FROM pg_catalog.pg_class WHERE oid = '%s';",
1528  oid);
1529  }
1530 
1531  res = PSQLexec(buf.data);
1532  if (!res)
1533  goto error_return;
1534 
1535  /* Did we get anything? */
1536  if (PQntuples(res) == 0)
1537  {
1538  if (!pset.quiet)
1539  psql_error("Did not find any relation with OID %s.\n", oid);
1540  goto error_return;
1541  }
1542 
1543  tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1544  tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1545  tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1546  tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1547  tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1548  tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1549  tableinfo.forcerowsecurity = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1550  tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 7), "t") == 0;
1551  tableinfo.reloptions = (pset.sversion >= 80200) ?
1552  pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
1553  tableinfo.tablespace = (pset.sversion >= 80000) ?
1554  atooid(PQgetvalue(res, 0, 9)) : 0;
1555  tableinfo.reloftype = (pset.sversion >= 90000 &&
1556  strcmp(PQgetvalue(res, 0, 10), "") != 0) ?
1557  pg_strdup(PQgetvalue(res, 0, 10)) : NULL;
1558  tableinfo.relpersistence = (pset.sversion >= 90100) ?
1559  *(PQgetvalue(res, 0, 11)) : 0;
1560  tableinfo.relreplident = (pset.sversion >= 90400) ?
1561  *(PQgetvalue(res, 0, 12)) : 'd';
1562  PQclear(res);
1563  res = NULL;
1564 
1565  /*
1566  * If it's a sequence, deal with it here separately.
1567  */
1568  if (tableinfo.relkind == RELKIND_SEQUENCE)
1569  {
1570  PGresult *result = NULL;
1571  printQueryOpt myopt = pset.popt;
1572  char *footers[2] = {NULL, NULL};
1573 
1574  if (pset.sversion >= 100000)
1575  {
1576  printfPQExpBuffer(&buf,
1577  "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n"
1578  " seqstart AS \"%s\",\n"
1579  " seqmin AS \"%s\",\n"
1580  " seqmax AS \"%s\",\n"
1581  " seqincrement AS \"%s\",\n"
1582  " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n"
1583  " seqcache AS \"%s\"\n",
1584  gettext_noop("Type"),
1585  gettext_noop("Start"),
1586  gettext_noop("Minimum"),
1587  gettext_noop("Maximum"),
1588  gettext_noop("Increment"),
1589  gettext_noop("yes"),
1590  gettext_noop("no"),
1591  gettext_noop("Cycles?"),
1592  gettext_noop("Cache"));
1593  appendPQExpBuffer(&buf,
1594  "FROM pg_catalog.pg_sequence\n"
1595  "WHERE seqrelid = '%s';",
1596  oid);
1597  }
1598  else
1599  {
1600  printfPQExpBuffer(&buf,
1601  "SELECT 'bigint' AS \"%s\",\n"
1602  " start_value AS \"%s\",\n"
1603  " min_value AS \"%s\",\n"
1604  " max_value AS \"%s\",\n"
1605  " increment_by AS \"%s\",\n"
1606  " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n"
1607  " cache_value AS \"%s\"\n",
1608  gettext_noop("Type"),
1609  gettext_noop("Start"),
1610  gettext_noop("Minimum"),
1611  gettext_noop("Maximum"),
1612  gettext_noop("Increment"),
1613  gettext_noop("yes"),
1614  gettext_noop("no"),
1615  gettext_noop("Cycles?"),
1616  gettext_noop("Cache"));
1617  appendPQExpBuffer(&buf, "FROM %s", fmtId(schemaname));
1618  /* must be separate because fmtId isn't reentrant */
1619  appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1620  }
1621 
1622  res = PSQLexec(buf.data);
1623  if (!res)
1624  goto error_return;
1625 
1626  /* Footer information about a sequence */
1627 
1628  /* Get the column that owns this sequence */
1629  printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1630  "\n pg_catalog.quote_ident(relname) || '.' ||"
1631  "\n pg_catalog.quote_ident(attname),"
1632  "\n d.deptype"
1633  "\nFROM pg_catalog.pg_class c"
1634  "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1635  "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1636  "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1637  "\n a.attrelid=c.oid AND"
1638  "\n a.attnum=d.refobjsubid)"
1639  "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1640  "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1641  "\n AND d.objid='%s'"
1642  "\n AND d.deptype IN ('a', 'i')",
1643  oid);
1644 
1645  result = PSQLexec(buf.data);
1646 
1647  /*
1648  * If we get no rows back, don't show anything (obviously). We should
1649  * never get more than one row back, but if we do, just ignore it and
1650  * don't print anything.
1651  */
1652  if (!result)
1653  goto error_return;
1654  else if (PQntuples(result) == 1)
1655  {
1656  switch (PQgetvalue(result, 0, 1)[0])
1657  {
1658  case 'a':
1659  footers[0] = psprintf(_("Owned by: %s"),
1660  PQgetvalue(result, 0, 0));
1661  break;
1662  case 'i':
1663  footers[0] = psprintf(_("Sequence for identity column: %s"),
1664  PQgetvalue(result, 0, 0));
1665  break;
1666  }
1667  }
1668  PQclear(result);
1669 
1670  printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1671  schemaname, relationname);
1672 
1673  myopt.footers = footers;
1674  myopt.topt.default_footer = false;
1675  myopt.title = title.data;
1676  myopt.translate_header = true;
1677 
1678  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1679 
1680  if (footers[0])
1681  free(footers[0]);
1682 
1683  retval = true;
1684  goto error_return; /* not an error, just return early */
1685  }
1686 
1687  /*
1688  * Get column info
1689  *
1690  * You need to modify value of "firstvcol" which will be defined below if
1691  * you are adding column(s) preceding to verbose-only columns.
1692  */
1693  printfPQExpBuffer(&buf, "SELECT a.attname,");
1694  appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1695  "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1696  "\n FROM pg_catalog.pg_attrdef d"
1697  "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1698  "\n a.attnotnull, a.attnum,");
1699  if (pset.sversion >= 90100)
1700  appendPQExpBufferStr(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1701  " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1702  else
1703  appendPQExpBufferStr(&buf, "\n NULL AS attcollation");
1704  if (pset.sversion >= 100000)
1705  appendPQExpBufferStr(&buf, ",\n a.attidentity");
1706  else
1707  appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attidentity");
1708  if (tableinfo.relkind == RELKIND_INDEX ||
1709  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1710  appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1711  else
1712  appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
1713  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200)
1714  appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1715  " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1716  " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1717  else
1718  appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
1719  if (verbose)
1720  {
1721  appendPQExpBufferStr(&buf, ",\n a.attstorage");
1722  appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1723 
1724  /*
1725  * In 9.0+, we have column comments for: relations, views, composite
1726  * types, and foreign tables (cf. CommentObject() in comment.c).
1727  */
1728  if (tableinfo.relkind == RELKIND_RELATION ||
1729  tableinfo.relkind == RELKIND_VIEW ||
1730  tableinfo.relkind == RELKIND_MATVIEW ||
1731  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1732  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1733  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1734  appendPQExpBufferStr(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1735  }
1736 
1737  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1738  appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1739  appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1740 
1741  res = PSQLexec(buf.data);
1742  if (!res)
1743  goto error_return;
1744  numrows = PQntuples(res);
1745 
1746  /* Make title */
1747  switch (tableinfo.relkind)
1748  {
1749  case RELKIND_RELATION:
1750  if (tableinfo.relpersistence == 'u')
1751  printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1752  schemaname, relationname);
1753  else
1754  printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1755  schemaname, relationname);
1756  break;
1757  case RELKIND_VIEW:
1758  printfPQExpBuffer(&title, _("View \"%s.%s\""),
1759  schemaname, relationname);
1760  break;
1761  case RELKIND_MATVIEW:
1762  if (tableinfo.relpersistence == 'u')
1763  printfPQExpBuffer(&title, _("Unlogged materialized view \"%s.%s\""),
1764  schemaname, relationname);
1765  else
1766  printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
1767  schemaname, relationname);
1768  break;
1769  case RELKIND_INDEX:
1771  if (tableinfo.relpersistence == 'u')
1772  printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1773  schemaname, relationname);
1774  else
1775  printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1776  schemaname, relationname);
1777  break;
1778  case 's':
1779  /* not used as of 8.2, but keep it for backwards compatibility */
1780  printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1781  schemaname, relationname);
1782  break;
1783  case RELKIND_TOASTVALUE:
1784  printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1785  schemaname, relationname);
1786  break;
1788  printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1789  schemaname, relationname);
1790  break;
1791  case RELKIND_FOREIGN_TABLE:
1792  printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1793  schemaname, relationname);
1794  break;
1796  if (tableinfo.relpersistence == 'u')
1797  printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1798  schemaname, relationname);
1799  else
1800  printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1801  schemaname, relationname);
1802  break;
1803  default:
1804  /* untranslated unknown relkind */
1805  printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1806  tableinfo.relkind, schemaname, relationname);
1807  break;
1808  }
1809 
1810  /* Set the number of columns, and their names */
1811  headers[0] = gettext_noop("Column");
1812  headers[1] = gettext_noop("Type");
1813  cols = 2;
1814 
1815  if (tableinfo.relkind == RELKIND_RELATION ||
1816  tableinfo.relkind == RELKIND_VIEW ||
1817  tableinfo.relkind == RELKIND_MATVIEW ||
1818  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1819  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1820  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1821  {
1822  headers[cols++] = gettext_noop("Collation");
1823  headers[cols++] = gettext_noop("Nullable");
1824  headers[cols++] = gettext_noop("Default");
1825  show_column_details = true;
1826  }
1827 
1828  if (tableinfo.relkind == RELKIND_INDEX ||
1829  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1830  headers[cols++] = gettext_noop("Definition");
1831 
1832  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200)
1833  headers[cols++] = gettext_noop("FDW options");
1834 
1835  if (verbose)
1836  {
1837  headers[cols++] = gettext_noop("Storage");
1838  if (tableinfo.relkind == RELKIND_RELATION ||
1839  tableinfo.relkind == RELKIND_INDEX ||
1840  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1841  tableinfo.relkind == RELKIND_MATVIEW ||
1842  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1843  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1844  headers[cols++] = gettext_noop("Stats target");
1845  /* Column comments, if the relkind supports this feature. */
1846  if (tableinfo.relkind == RELKIND_RELATION ||
1847  tableinfo.relkind == RELKIND_VIEW ||
1848  tableinfo.relkind == RELKIND_MATVIEW ||
1849  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1850  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1851  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1852  headers[cols++] = gettext_noop("Description");
1853  }
1854 
1855  printTableInit(&cont, &myopt, title.data, cols, numrows);
1856  printTableInitialized = true;
1857 
1858  for (i = 0; i < cols; i++)
1859  printTableAddHeader(&cont, headers[i], true, 'l');
1860 
1861  /* Get view_def if table is a view or materialized view */
1862  if ((tableinfo.relkind == RELKIND_VIEW ||
1863  tableinfo.relkind == RELKIND_MATVIEW) && verbose)
1864  {
1865  PGresult *result;
1866 
1867  printfPQExpBuffer(&buf,
1868  "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1869  oid);
1870  result = PSQLexec(buf.data);
1871  if (!result)
1872  goto error_return;
1873 
1874  if (PQntuples(result) > 0)
1875  view_def = pg_strdup(PQgetvalue(result, 0, 0));
1876 
1877  PQclear(result);
1878  }
1879 
1880  /* Generate table cells to be printed */
1881  for (i = 0; i < numrows; i++)
1882  {
1883  /* Column */
1884  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1885 
1886  /* Type */
1887  printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1888 
1889  /* Collation, Nullable, Default */
1890  if (show_column_details)
1891  {
1892  char *identity;
1893  char *default_str = "";
1894 
1895  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
1896 
1897  printTableAddCell(&cont, strcmp(PQgetvalue(res, i, 3), "t") == 0 ? "not null" : "", false, false);
1898 
1899  identity = PQgetvalue(res, i, 6);
1900 
1901  if (!identity[0])
1902  /* (note: above we cut off the 'default' string at 128) */
1903  default_str = PQgetvalue(res, i, 2);
1904  else if (identity[0] == ATTRIBUTE_IDENTITY_ALWAYS)
1905  default_str = "generated always as identity";
1906  else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
1907  default_str = "generated by default as identity";
1908 
1909  printTableAddCell(&cont, default_str, false, false);
1910  }
1911 
1912  /* Expression for index column */
1913  if (tableinfo.relkind == RELKIND_INDEX ||
1914  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1915  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1916 
1917  /* FDW options for foreign table column, only for 9.2 or later */
1918  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200)
1919  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
1920 
1921  /* Storage and Description */
1922  if (verbose)
1923  {
1924  int firstvcol = 9;
1925  char *storage = PQgetvalue(res, i, firstvcol);
1926 
1927  /* these strings are literal in our syntax, so not translated. */
1928  printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1929  (storage[0] == 'm' ? "main" :
1930  (storage[0] == 'x' ? "extended" :
1931  (storage[0] == 'e' ? "external" :
1932  "???")))),
1933  false, false);
1934 
1935  /* Statistics target, if the relkind supports this feature */
1936  if (tableinfo.relkind == RELKIND_RELATION ||
1937  tableinfo.relkind == RELKIND_INDEX ||
1938  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1939  tableinfo.relkind == RELKIND_MATVIEW ||
1940  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1941  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1942  {
1943  printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1944  false, false);
1945  }
1946 
1947  /* Column comments, if the relkind supports this feature. */
1948  if (tableinfo.relkind == RELKIND_RELATION ||
1949  tableinfo.relkind == RELKIND_VIEW ||
1950  tableinfo.relkind == RELKIND_MATVIEW ||
1951  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1952  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1953  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1954  printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1955  false, false);
1956  }
1957  }
1958 
1959  /* Make footers */
1960  if (pset.sversion >= 100000)
1961  {
1962  /* Get the partition information */
1963  PGresult *result;
1964  char *parent_name;
1965  char *partdef;
1966  char *partconstraintdef = NULL;
1967 
1968  printfPQExpBuffer(&buf,
1969  "SELECT inhparent::pg_catalog.regclass,\n"
1970  " pg_catalog.pg_get_expr(c.relpartbound, inhrelid)");
1971  /* If verbose, also request the partition constraint definition */
1972  if (verbose)
1973  appendPQExpBuffer(&buf,
1974  ",\n pg_catalog.pg_get_partition_constraintdef(inhrelid)");
1975  appendPQExpBuffer(&buf,
1976  "\nFROM pg_catalog.pg_class c"
1977  " JOIN pg_catalog.pg_inherits i"
1978  " ON c.oid = inhrelid"
1979  "\nWHERE c.oid = '%s' AND c.relispartition;", oid);
1980  result = PSQLexec(buf.data);
1981  if (!result)
1982  goto error_return;
1983 
1984  if (PQntuples(result) > 0)
1985  {
1986  parent_name = PQgetvalue(result, 0, 0);
1987  partdef = PQgetvalue(result, 0, 1);
1988 
1989  if (PQnfields(result) == 3 && !PQgetisnull(result, 0, 2))
1990  partconstraintdef = PQgetvalue(result, 0, 2);
1991 
1992  printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s"), parent_name,
1993  partdef);
1994  printTableAddFooter(&cont, tmpbuf.data);
1995 
1996  if (verbose)
1997  {
1998  /* If there isn't any constraint, show that explicitly */
1999  if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
2000  printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
2001  else
2002  printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"),
2003  partconstraintdef);
2004  printTableAddFooter(&cont, tmpbuf.data);
2005  }
2006 
2007  PQclear(result);
2008  }
2009  }
2010 
2011  if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2012  {
2013  /* Get the partition key information */
2014  PGresult *result;
2015  char *partkeydef;
2016 
2017  printfPQExpBuffer(&buf,
2018  "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
2019  oid);
2020  result = PSQLexec(buf.data);
2021  if (!result || PQntuples(result) != 1)
2022  goto error_return;
2023 
2024  partkeydef = PQgetvalue(result, 0, 0);
2025  printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2026  printTableAddFooter(&cont, tmpbuf.data);
2027  PQclear(result);
2028  }
2029 
2030  if (tableinfo.relkind == RELKIND_INDEX ||
2031  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
2032  {
2033  /* Footer information about an index */
2034  PGresult *result;
2035 
2036  printfPQExpBuffer(&buf,
2037  "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
2038  if (pset.sversion >= 80200)
2039  appendPQExpBufferStr(&buf, "i.indisvalid,\n");
2040  else
2041  appendPQExpBufferStr(&buf, "true AS indisvalid,\n");
2042  if (pset.sversion >= 90000)
2043  appendPQExpBufferStr(&buf,
2044  " (NOT i.indimmediate) AND "
2045  "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2046  "WHERE conrelid = i.indrelid AND "
2047  "conindid = i.indexrelid AND "
2048  "contype IN ('p','u','x') AND "
2049  "condeferrable) AS condeferrable,\n"
2050  " (NOT i.indimmediate) AND "
2051  "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2052  "WHERE conrelid = i.indrelid AND "
2053  "conindid = i.indexrelid AND "
2054  "contype IN ('p','u','x') AND "
2055  "condeferred) AS condeferred,\n");
2056  else
2057  appendPQExpBufferStr(&buf,
2058  " false AS condeferrable, false AS condeferred,\n");
2059 
2060  if (pset.sversion >= 90400)
2061  appendPQExpBuffer(&buf, "i.indisreplident,\n");
2062  else
2063  appendPQExpBuffer(&buf, "false AS indisreplident,\n");
2064 
2065  appendPQExpBuffer(&buf, " a.amname, c2.relname, "
2066  "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
2067  "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
2068  "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
2069  "AND i.indrelid = c2.oid;",
2070  oid);
2071 
2072  result = PSQLexec(buf.data);
2073  if (!result)
2074  goto error_return;
2075  else if (PQntuples(result) != 1)
2076  {
2077  PQclear(result);
2078  goto error_return;
2079  }
2080  else
2081  {
2082  char *indisunique = PQgetvalue(result, 0, 0);
2083  char *indisprimary = PQgetvalue(result, 0, 1);
2084  char *indisclustered = PQgetvalue(result, 0, 2);
2085  char *indisvalid = PQgetvalue(result, 0, 3);
2086  char *deferrable = PQgetvalue(result, 0, 4);
2087  char *deferred = PQgetvalue(result, 0, 5);
2088  char *indisreplident = PQgetvalue(result, 0, 6);
2089  char *indamname = PQgetvalue(result, 0, 7);
2090  char *indtable = PQgetvalue(result, 0, 8);
2091  char *indpred = PQgetvalue(result, 0, 9);
2092 
2093  if (strcmp(indisprimary, "t") == 0)
2094  printfPQExpBuffer(&tmpbuf, _("primary key, "));
2095  else if (strcmp(indisunique, "t") == 0)
2096  printfPQExpBuffer(&tmpbuf, _("unique, "));
2097  else
2098  resetPQExpBuffer(&tmpbuf);
2099  appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
2100 
2101  /* we assume here that index and table are in same schema */
2102  appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
2103  schemaname, indtable);
2104 
2105  if (strlen(indpred))
2106  appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
2107 
2108  if (strcmp(indisclustered, "t") == 0)
2109  appendPQExpBufferStr(&tmpbuf, _(", clustered"));
2110 
2111  if (strcmp(indisvalid, "t") != 0)
2112  appendPQExpBufferStr(&tmpbuf, _(", invalid"));
2113 
2114  if (strcmp(deferrable, "t") == 0)
2115  appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
2116 
2117  if (strcmp(deferred, "t") == 0)
2118  appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
2119 
2120  if (strcmp(indisreplident, "t") == 0)
2121  appendPQExpBuffer(&tmpbuf, _(", replica identity"));
2122 
2123  printTableAddFooter(&cont, tmpbuf.data);
2124  add_tablespace_footer(&cont, tableinfo.relkind,
2125  tableinfo.tablespace, true);
2126  }
2127 
2128  PQclear(result);
2129  }
2130  else if (tableinfo.relkind == RELKIND_RELATION ||
2131  tableinfo.relkind == RELKIND_MATVIEW ||
2132  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2133  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2134  {
2135  /* Footer information about a table */
2136  PGresult *result = NULL;
2137  int tuples = 0;
2138 
2139  /* print indexes */
2140  if (tableinfo.hasindex)
2141  {
2142  printfPQExpBuffer(&buf,
2143  "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
2144  if (pset.sversion >= 80200)
2145  appendPQExpBufferStr(&buf, "i.indisvalid, ");
2146  else
2147  appendPQExpBufferStr(&buf, "true as indisvalid, ");
2148  appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
2149  if (pset.sversion >= 90000)
2150  appendPQExpBufferStr(&buf,
2151  "pg_catalog.pg_get_constraintdef(con.oid, true), "
2152  "contype, condeferrable, condeferred");
2153  else
2154  appendPQExpBufferStr(&buf,
2155  "null AS constraintdef, null AS contype, "
2156  "false AS condeferrable, false AS condeferred");
2157  if (pset.sversion >= 90400)
2158  appendPQExpBufferStr(&buf, ", i.indisreplident");
2159  else
2160  appendPQExpBufferStr(&buf, ", false AS indisreplident");
2161  if (pset.sversion >= 80000)
2162  appendPQExpBufferStr(&buf, ", c2.reltablespace");
2163  appendPQExpBufferStr(&buf,
2164  "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
2165  if (pset.sversion >= 90000)
2166  appendPQExpBufferStr(&buf,
2167  " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
2168  appendPQExpBuffer(&buf,
2169  "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2170  "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
2171  oid);
2172  result = PSQLexec(buf.data);
2173  if (!result)
2174  goto error_return;
2175  else
2176  tuples = PQntuples(result);
2177 
2178  if (tuples > 0)
2179  {
2180  printTableAddFooter(&cont, _("Indexes:"));
2181  for (i = 0; i < tuples; i++)
2182  {
2183  /* untranslated index name */
2184  printfPQExpBuffer(&buf, " \"%s\"",
2185  PQgetvalue(result, i, 0));
2186 
2187  /* If exclusion constraint, print the constraintdef */
2188  if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
2189  {
2190  appendPQExpBuffer(&buf, " %s",
2191  PQgetvalue(result, i, 6));
2192  }
2193  else
2194  {
2195  const char *indexdef;
2196  const char *usingpos;
2197 
2198  /* Label as primary key or unique (but not both) */
2199  if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2200  appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2201  else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2202  {
2203  if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2204  appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2205  else
2206  appendPQExpBufferStr(&buf, " UNIQUE,");
2207  }
2208 
2209  /* Everything after "USING" is echoed verbatim */
2210  indexdef = PQgetvalue(result, i, 5);
2211  usingpos = strstr(indexdef, " USING ");
2212  if (usingpos)
2213  indexdef = usingpos + 7;
2214  appendPQExpBuffer(&buf, " %s", indexdef);
2215 
2216  /* Need these for deferrable PK/UNIQUE indexes */
2217  if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2218  appendPQExpBufferStr(&buf, " DEFERRABLE");
2219 
2220  if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2221  appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2222  }
2223 
2224  /* Add these for all cases */
2225  if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2226  appendPQExpBufferStr(&buf, " CLUSTER");
2227 
2228  if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2229  appendPQExpBufferStr(&buf, " INVALID");
2230 
2231  if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2232  appendPQExpBuffer(&buf, " REPLICA IDENTITY");
2233 
2234  printTableAddFooter(&cont, buf.data);
2235 
2236  /* Print tablespace of the index on the same line */
2237  if (pset.sversion >= 80000)
2239  atooid(PQgetvalue(result, i, 11)),
2240  false);
2241  }
2242  }
2243  PQclear(result);
2244  }
2245 
2246  /* print table (and column) check constraints */
2247  if (tableinfo.checks)
2248  {
2249  printfPQExpBuffer(&buf,
2250  "SELECT r.conname, "
2251  "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2252  "FROM pg_catalog.pg_constraint r\n"
2253  "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
2254  "ORDER BY 1;",
2255  oid);
2256  result = PSQLexec(buf.data);
2257  if (!result)
2258  goto error_return;
2259  else
2260  tuples = PQntuples(result);
2261 
2262  if (tuples > 0)
2263  {
2264  printTableAddFooter(&cont, _("Check constraints:"));
2265  for (i = 0; i < tuples; i++)
2266  {
2267  /* untranslated constraint name and def */
2268  printfPQExpBuffer(&buf, " \"%s\" %s",
2269  PQgetvalue(result, i, 0),
2270  PQgetvalue(result, i, 1));
2271 
2272  printTableAddFooter(&cont, buf.data);
2273  }
2274  }
2275  PQclear(result);
2276  }
2277 
2278  /* print foreign-key constraints (there are none if no triggers) */
2279  if (tableinfo.hastriggers)
2280  {
2281  printfPQExpBuffer(&buf,
2282  "SELECT conname,\n"
2283  " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
2284  "FROM pg_catalog.pg_constraint r\n"
2285  "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
2286  oid);
2287  result = PSQLexec(buf.data);
2288  if (!result)
2289  goto error_return;
2290  else
2291  tuples = PQntuples(result);
2292 
2293  if (tuples > 0)
2294  {
2295  printTableAddFooter(&cont, _("Foreign-key constraints:"));
2296  for (i = 0; i < tuples; i++)
2297  {
2298  /* untranslated constraint name and def */
2299  printfPQExpBuffer(&buf, " \"%s\" %s",
2300  PQgetvalue(result, i, 0),
2301  PQgetvalue(result, i, 1));
2302 
2303  printTableAddFooter(&cont, buf.data);
2304  }
2305  }
2306  PQclear(result);
2307  }
2308 
2309  /* print incoming foreign-key references (none if no triggers) */
2310  if (tableinfo.hastriggers)
2311  {
2312  printfPQExpBuffer(&buf,
2313  "SELECT conname, conrelid::pg_catalog.regclass,\n"
2314  " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
2315  "FROM pg_catalog.pg_constraint c\n"
2316  "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
2317  oid);
2318  result = PSQLexec(buf.data);
2319  if (!result)
2320  goto error_return;
2321  else
2322  tuples = PQntuples(result);
2323 
2324  if (tuples > 0)
2325  {
2326  printTableAddFooter(&cont, _("Referenced by:"));
2327  for (i = 0; i < tuples; i++)
2328  {
2329  printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2330  PQgetvalue(result, i, 1),
2331  PQgetvalue(result, i, 0),
2332  PQgetvalue(result, i, 2));
2333 
2334  printTableAddFooter(&cont, buf.data);
2335  }
2336  }
2337  PQclear(result);
2338  }
2339 
2340  /* print any row-level policies */
2341  if (pset.sversion >= 90500)
2342  {
2343  printfPQExpBuffer(&buf, "SELECT pol.polname,");
2344  if (pset.sversion >= 100000)
2345  appendPQExpBuffer(&buf,
2346  " pol.polpermissive,\n");
2347  else
2348  appendPQExpBuffer(&buf,
2349  " 't' as polpermissive,\n");
2350  appendPQExpBuffer(&buf,
2351  " CASE WHEN pol.polroles = '{0}' THEN NULL ELSE pg_catalog.array_to_string(array(select rolname from pg_catalog.pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2352  " pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2353  " pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2354  " CASE pol.polcmd\n"
2355  " WHEN 'r' THEN 'SELECT'\n"
2356  " WHEN 'a' THEN 'INSERT'\n"
2357  " WHEN 'w' THEN 'UPDATE'\n"
2358  " WHEN 'd' THEN 'DELETE'\n"
2359  " END AS cmd\n"
2360  "FROM pg_catalog.pg_policy pol\n"
2361  "WHERE pol.polrelid = '%s' ORDER BY 1;",
2362  oid);
2363 
2364  result = PSQLexec(buf.data);
2365  if (!result)
2366  goto error_return;
2367  else
2368  tuples = PQntuples(result);
2369 
2370  /*
2371  * Handle cases where RLS is enabled and there are policies, or
2372  * there aren't policies, or RLS isn't enabled but there are
2373  * policies
2374  */
2375  if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2376  printTableAddFooter(&cont, _("Policies:"));
2377 
2378  if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2379  printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2380 
2381  if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2382  printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2383 
2384  if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2385  printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2386 
2387  if (!tableinfo.rowsecurity && tuples > 0)
2388  printTableAddFooter(&cont, _("Policies (row security disabled):"));
2389 
2390  /* Might be an empty set - that's ok */
2391  for (i = 0; i < tuples; i++)
2392  {
2393  printfPQExpBuffer(&buf, " POLICY \"%s\"",
2394  PQgetvalue(result, i, 0));
2395 
2396  if (*(PQgetvalue(result, i, 1)) == 'f')
2397  appendPQExpBuffer(&buf, " AS RESTRICTIVE");
2398 
2399  if (!PQgetisnull(result, i, 5))
2400  appendPQExpBuffer(&buf, " FOR %s",
2401  PQgetvalue(result, i, 5));
2402 
2403  if (!PQgetisnull(result, i, 2))
2404  {
2405  appendPQExpBuffer(&buf, "\n TO %s",
2406  PQgetvalue(result, i, 2));
2407  }
2408 
2409  if (!PQgetisnull(result, i, 3))
2410  appendPQExpBuffer(&buf, "\n USING (%s)",
2411  PQgetvalue(result, i, 3));
2412 
2413  if (!PQgetisnull(result, i, 4))
2414  appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2415  PQgetvalue(result, i, 4));
2416 
2417  printTableAddFooter(&cont, buf.data);
2418 
2419  }
2420  PQclear(result);
2421  }
2422 
2423  /* print any extended statistics */
2424  if (pset.sversion >= 100000)
2425  {
2426  printfPQExpBuffer(&buf,
2427  "SELECT oid, "
2428  "stxrelid::pg_catalog.regclass, "
2429  "stxnamespace::pg_catalog.regnamespace AS nsp, "
2430  "stxname,\n"
2431  " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')\n"
2432  " FROM pg_catalog.unnest(stxkeys) s(attnum)\n"
2433  " JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND\n"
2434  " a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
2435  " 'd' = any(stxkind) AS ndist_enabled,\n"
2436  " 'f' = any(stxkind) AS deps_enabled\n"
2437  "FROM pg_catalog.pg_statistic_ext stat "
2438  "WHERE stxrelid = '%s'\n"
2439  "ORDER BY 1;",
2440  oid);
2441 
2442  result = PSQLexec(buf.data);
2443  if (!result)
2444  goto error_return;
2445  else
2446  tuples = PQntuples(result);
2447 
2448  if (tuples > 0)
2449  {
2450  printTableAddFooter(&cont, _("Statistics objects:"));
2451 
2452  for (i = 0; i < tuples; i++)
2453  {
2454  bool gotone = false;
2455 
2456  printfPQExpBuffer(&buf, " ");
2457 
2458  /* statistics object name (qualified with namespace) */
2459  appendPQExpBuffer(&buf, "\"%s\".\"%s\" (",
2460  PQgetvalue(result, i, 2),
2461  PQgetvalue(result, i, 3));
2462 
2463  /* options */
2464  if (strcmp(PQgetvalue(result, i, 5), "t") == 0)
2465  {
2466  appendPQExpBufferStr(&buf, "ndistinct");
2467  gotone = true;
2468  }
2469 
2470  if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
2471  {
2472  appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2473  }
2474 
2475  appendPQExpBuffer(&buf, ") ON %s FROM %s",
2476  PQgetvalue(result, i, 4),
2477  PQgetvalue(result, i, 1));
2478 
2479  printTableAddFooter(&cont, buf.data);
2480  }
2481  }
2482  PQclear(result);
2483  }
2484 
2485  /* print rules */
2486  if (tableinfo.hasrules && tableinfo.relkind != RELKIND_MATVIEW)
2487  {
2488  if (pset.sversion >= 80300)
2489  {
2490  printfPQExpBuffer(&buf,
2491  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2492  "ev_enabled\n"
2493  "FROM pg_catalog.pg_rewrite r\n"
2494  "WHERE r.ev_class = '%s' ORDER BY 1;",
2495  oid);
2496  }
2497  else
2498  {
2499  printfPQExpBuffer(&buf,
2500  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2501  "'O' AS ev_enabled\n"
2502  "FROM pg_catalog.pg_rewrite r\n"
2503  "WHERE r.ev_class = '%s' ORDER BY 1;",
2504  oid);
2505  }
2506  result = PSQLexec(buf.data);
2507  if (!result)
2508  goto error_return;
2509  else
2510  tuples = PQntuples(result);
2511 
2512  if (tuples > 0)
2513  {
2514  bool have_heading;
2515  int category;
2516 
2517  for (category = 0; category < 4; category++)
2518  {
2519  have_heading = false;
2520 
2521  for (i = 0; i < tuples; i++)
2522  {
2523  const char *ruledef;
2524  bool list_rule = false;
2525 
2526  switch (category)
2527  {
2528  case 0:
2529  if (*PQgetvalue(result, i, 2) == 'O')
2530  list_rule = true;
2531  break;
2532  case 1:
2533  if (*PQgetvalue(result, i, 2) == 'D')
2534  list_rule = true;
2535  break;
2536  case 2:
2537  if (*PQgetvalue(result, i, 2) == 'A')
2538  list_rule = true;
2539  break;
2540  case 3:
2541  if (*PQgetvalue(result, i, 2) == 'R')
2542  list_rule = true;
2543  break;
2544  }
2545  if (!list_rule)
2546  continue;
2547 
2548  if (!have_heading)
2549  {
2550  switch (category)
2551  {
2552  case 0:
2553  printfPQExpBuffer(&buf, _("Rules:"));
2554  break;
2555  case 1:
2556  printfPQExpBuffer(&buf, _("Disabled rules:"));
2557  break;
2558  case 2:
2559  printfPQExpBuffer(&buf, _("Rules firing always:"));
2560  break;
2561  case 3:
2562  printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
2563  break;
2564  }
2565  printTableAddFooter(&cont, buf.data);
2566  have_heading = true;
2567  }
2568 
2569  /* Everything after "CREATE RULE" is echoed verbatim */
2570  ruledef = PQgetvalue(result, i, 1);
2571  ruledef += 12;
2572  printfPQExpBuffer(&buf, " %s", ruledef);
2573  printTableAddFooter(&cont, buf.data);
2574  }
2575  }
2576  }
2577  PQclear(result);
2578  }
2579 
2580  /* print any publications */
2581  if (pset.sversion >= 100000)
2582  {
2583  printfPQExpBuffer(&buf,
2584  "SELECT pubname\n"
2585  "FROM pg_catalog.pg_publication p\n"
2586  "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
2587  "WHERE pr.prrelid = '%s'\n"
2588  "UNION ALL\n"
2589  "SELECT pubname\n"
2590  "FROM pg_catalog.pg_publication p\n"
2591  "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
2592  "ORDER BY 1;",
2593  oid, oid);
2594 
2595  result = PSQLexec(buf.data);
2596  if (!result)
2597  goto error_return;
2598  else
2599  tuples = PQntuples(result);
2600 
2601  if (tuples > 0)
2602  printTableAddFooter(&cont, _("Publications:"));
2603 
2604  /* Might be an empty set - that's ok */
2605  for (i = 0; i < tuples; i++)
2606  {
2607  printfPQExpBuffer(&buf, " \"%s\"",
2608  PQgetvalue(result, i, 0));
2609 
2610  printTableAddFooter(&cont, buf.data);
2611  }
2612  PQclear(result);
2613  }
2614  }
2615 
2616  if (view_def)
2617  {
2618  PGresult *result = NULL;
2619 
2620  /* Footer information about a view */
2621  printTableAddFooter(&cont, _("View definition:"));
2622  printTableAddFooter(&cont, view_def);
2623 
2624  /* print rules */
2625  if (tableinfo.hasrules)
2626  {
2627  printfPQExpBuffer(&buf,
2628  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
2629  "FROM pg_catalog.pg_rewrite r\n"
2630  "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
2631  oid);
2632  result = PSQLexec(buf.data);
2633  if (!result)
2634  goto error_return;
2635 
2636  if (PQntuples(result) > 0)
2637  {
2638  printTableAddFooter(&cont, _("Rules:"));
2639  for (i = 0; i < PQntuples(result); i++)
2640  {
2641  const char *ruledef;
2642 
2643  /* Everything after "CREATE RULE" is echoed verbatim */
2644  ruledef = PQgetvalue(result, i, 1);
2645  ruledef += 12;
2646 
2647  printfPQExpBuffer(&buf, " %s", ruledef);
2648  printTableAddFooter(&cont, buf.data);
2649  }
2650  }
2651  PQclear(result);
2652  }
2653  }
2654 
2655  /*
2656  * Print triggers next, if any (but only user-defined triggers). This
2657  * could apply to either a table or a view.
2658  */
2659  if (tableinfo.hastriggers)
2660  {
2661  PGresult *result;
2662  int tuples;
2663 
2664  printfPQExpBuffer(&buf,
2665  "SELECT t.tgname, "
2666  "pg_catalog.pg_get_triggerdef(t.oid%s), "
2667  "t.tgenabled, %s\n"
2668  "FROM pg_catalog.pg_trigger t\n"
2669  "WHERE t.tgrelid = '%s' AND ",
2670  (pset.sversion >= 90000 ? ", true" : ""),
2671  (pset.sversion >= 90000 ? "t.tgisinternal" :
2672  pset.sversion >= 80300 ?
2673  "t.tgconstraint <> 0 AS tgisinternal" :
2674  "false AS tgisinternal"), oid);
2675  if (pset.sversion >= 90000)
2676  /* display/warn about disabled internal triggers */
2677  appendPQExpBuffer(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
2678  else if (pset.sversion >= 80300)
2679  appendPQExpBufferStr(&buf, "(t.tgconstraint = 0 OR (t.tgconstraint <> 0 AND t.tgenabled = 'D'))");
2680  else
2681  appendPQExpBufferStr(&buf,
2682  "(NOT tgisconstraint "
2683  " OR NOT EXISTS"
2684  " (SELECT 1 FROM pg_catalog.pg_depend d "
2685  " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2686  " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2687  appendPQExpBufferStr(&buf, "\nORDER BY 1;");
2688 
2689  result = PSQLexec(buf.data);
2690  if (!result)
2691  goto error_return;
2692  else
2693  tuples = PQntuples(result);
2694 
2695  if (tuples > 0)
2696  {
2697  bool have_heading;
2698  int category;
2699 
2700  /*
2701  * split the output into 4 different categories. Enabled triggers,
2702  * disabled triggers and the two special ALWAYS and REPLICA
2703  * configurations.
2704  */
2705  for (category = 0; category <= 4; category++)
2706  {
2707  have_heading = false;
2708  for (i = 0; i < tuples; i++)
2709  {
2710  bool list_trigger;
2711  const char *tgdef;
2712  const char *usingpos;
2713  const char *tgenabled;
2714  const char *tgisinternal;
2715 
2716  /*
2717  * Check if this trigger falls into the current category
2718  */
2719  tgenabled = PQgetvalue(result, i, 2);
2720  tgisinternal = PQgetvalue(result, i, 3);
2721  list_trigger = false;
2722  switch (category)
2723  {
2724  case 0:
2725  if (*tgenabled == 'O' || *tgenabled == 't')
2726  list_trigger = true;
2727  break;
2728  case 1:
2729  if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2730  *tgisinternal == 'f')
2731  list_trigger = true;
2732  break;
2733  case 2:
2734  if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2735  *tgisinternal == 't')
2736  list_trigger = true;
2737  break;
2738  case 3:
2739  if (*tgenabled == 'A')
2740  list_trigger = true;
2741  break;
2742  case 4:
2743  if (*tgenabled == 'R')
2744  list_trigger = true;
2745  break;
2746  }
2747  if (list_trigger == false)
2748  continue;
2749 
2750  /* Print the category heading once */
2751  if (have_heading == false)
2752  {
2753  switch (category)
2754  {
2755  case 0:
2756  printfPQExpBuffer(&buf, _("Triggers:"));
2757  break;
2758  case 1:
2759  if (pset.sversion >= 80300)
2760  printfPQExpBuffer(&buf, _("Disabled user triggers:"));
2761  else
2762  printfPQExpBuffer(&buf, _("Disabled triggers:"));
2763  break;
2764  case 2:
2765  printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
2766  break;
2767  case 3:
2768  printfPQExpBuffer(&buf, _("Triggers firing always:"));
2769  break;
2770  case 4:
2771  printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2772  break;
2773 
2774  }
2775  printTableAddFooter(&cont, buf.data);
2776  have_heading = true;
2777  }
2778 
2779  /* Everything after "TRIGGER" is echoed verbatim */
2780  tgdef = PQgetvalue(result, i, 1);
2781  usingpos = strstr(tgdef, " TRIGGER ");
2782  if (usingpos)
2783  tgdef = usingpos + 9;
2784 
2785  printfPQExpBuffer(&buf, " %s", tgdef);
2786  printTableAddFooter(&cont, buf.data);
2787  }
2788  }
2789  }
2790  PQclear(result);
2791  }
2792 
2793  /*
2794  * Finish printing the footer information about a table.
2795  */
2796  if (tableinfo.relkind == RELKIND_RELATION ||
2797  tableinfo.relkind == RELKIND_MATVIEW ||
2798  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2799  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2800  {
2801  PGresult *result;
2802  int tuples;
2803 
2804  /* print foreign server name */
2805  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
2806  {
2807  char *ftoptions;
2808 
2809  /* Footer information about foreign table */
2810  printfPQExpBuffer(&buf,
2811  "SELECT s.srvname,\n"
2812  " pg_catalog.array_to_string(ARRAY(\n"
2813  " SELECT pg_catalog.quote_ident(option_name)"
2814  " || ' ' || pg_catalog.quote_literal(option_value)\n"
2815  " FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
2816  "FROM pg_catalog.pg_foreign_table f,\n"
2817  " pg_catalog.pg_foreign_server s\n"
2818  "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
2819  oid);
2820  result = PSQLexec(buf.data);
2821  if (!result)
2822  goto error_return;
2823  else if (PQntuples(result) != 1)
2824  {
2825  PQclear(result);
2826  goto error_return;
2827  }
2828 
2829  /* Print server name */
2830  printfPQExpBuffer(&buf, _("Server: %s"),
2831  PQgetvalue(result, 0, 0));
2832  printTableAddFooter(&cont, buf.data);
2833 
2834  /* Print per-table FDW options, if any */
2835  ftoptions = PQgetvalue(result, 0, 1);
2836  if (ftoptions && ftoptions[0] != '\0')
2837  {
2838  printfPQExpBuffer(&buf, _("FDW options: (%s)"), ftoptions);
2839  printTableAddFooter(&cont, buf.data);
2840  }
2841  PQclear(result);
2842  }
2843 
2844  /* print inherited tables (exclude, if parent is a partitioned table) */
2845  printfPQExpBuffer(&buf,
2846  "SELECT c.oid::pg_catalog.regclass"
2847  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2848  " WHERE c.oid=i.inhparent AND i.inhrelid = '%s'"
2849  " AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_TABLE)
2850  " ORDER BY inhseqno;", oid);
2851 
2852  result = PSQLexec(buf.data);
2853  if (!result)
2854  goto error_return;
2855  else
2856  {
2857  const char *s = _("Inherits");
2858  int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2859 
2860  tuples = PQntuples(result);
2861 
2862  for (i = 0; i < tuples; i++)
2863  {
2864  if (i == 0)
2865  printfPQExpBuffer(&buf, "%s: %s",
2866  s, PQgetvalue(result, i, 0));
2867  else
2868  printfPQExpBuffer(&buf, "%*s %s",
2869  sw, "", PQgetvalue(result, i, 0));
2870  if (i < tuples - 1)
2871  appendPQExpBufferChar(&buf, ',');
2872 
2873  printTableAddFooter(&cont, buf.data);
2874  }
2875 
2876  PQclear(result);
2877  }
2878 
2879  /* print child tables (with additional info if partitions) */
2880  if (pset.sversion >= 100000)
2881  printfPQExpBuffer(&buf,
2882  "SELECT c.oid::pg_catalog.regclass,"
2883  " pg_catalog.pg_get_expr(c.relpartbound, c.oid),"
2884  " c.relkind"
2885  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2886  " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
2887  " ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
2888  " c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
2889  else if (pset.sversion >= 80300)
2890  printfPQExpBuffer(&buf,
2891  "SELECT c.oid::pg_catalog.regclass"
2892  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2893  " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
2894  " ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
2895  else
2896  printfPQExpBuffer(&buf,
2897  "SELECT c.oid::pg_catalog.regclass"
2898  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2899  " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
2900  " ORDER BY c.relname;", oid);
2901 
2902  result = PSQLexec(buf.data);
2903  if (!result)
2904  goto error_return;
2905  else
2906  tuples = PQntuples(result);
2907 
2908  /*
2909  * For a partitioned table with no partitions, always print the number
2910  * of partitions as zero, even when verbose output is expected.
2911  * Otherwise, we will not print "Partitions" section for a partitioned
2912  * table without any partitions.
2913  */
2914  if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE && tuples == 0)
2915  {
2916  printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
2917  printTableAddFooter(&cont, buf.data);
2918  }
2919  else if (!verbose)
2920  {
2921  /* print the number of child tables, if any */
2922  if (tuples > 0)
2923  {
2924  if (tableinfo.relkind != RELKIND_PARTITIONED_TABLE)
2925  printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2926  else
2927  printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
2928  printTableAddFooter(&cont, buf.data);
2929  }
2930  }
2931  else
2932  {
2933  /* display the list of child tables */
2934  const char *ct = (tableinfo.relkind != RELKIND_PARTITIONED_TABLE) ?
2935  _("Child tables") : _("Partitions");
2936  int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2937 
2938  for (i = 0; i < tuples; i++)
2939  {
2940  if (tableinfo.relkind != RELKIND_PARTITIONED_TABLE)
2941  {
2942  if (i == 0)
2943  printfPQExpBuffer(&buf, "%s: %s",
2944  ct, PQgetvalue(result, i, 0));
2945  else
2946  printfPQExpBuffer(&buf, "%*s %s",
2947  ctw, "", PQgetvalue(result, i, 0));
2948  }
2949  else
2950  {
2951  char *partitioned_note;
2952 
2953  if (*PQgetvalue(result, i, 2) == RELKIND_PARTITIONED_TABLE)
2954  partitioned_note = ", PARTITIONED";
2955  else
2956  partitioned_note = "";
2957 
2958  if (i == 0)
2959  printfPQExpBuffer(&buf, "%s: %s %s%s",
2960  ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
2961  partitioned_note);
2962  else
2963  printfPQExpBuffer(&buf, "%*s %s %s%s",
2964  ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
2965  partitioned_note);
2966  }
2967  if (i < tuples - 1)
2968  appendPQExpBufferChar(&buf, ',');
2969 
2970  printTableAddFooter(&cont, buf.data);
2971  }
2972  }
2973  PQclear(result);
2974 
2975  /* Table type */
2976  if (tableinfo.reloftype)
2977  {
2978  printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2979  printTableAddFooter(&cont, buf.data);
2980  }
2981 
2982  if (verbose &&
2983  (tableinfo.relkind == RELKIND_RELATION ||
2984  tableinfo.relkind == RELKIND_MATVIEW) &&
2985 
2986  /*
2987  * No need to display default values; we already display a REPLICA
2988  * IDENTITY marker on indexes.
2989  */
2990  tableinfo.relreplident != 'i' &&
2991  ((strcmp(schemaname, "pg_catalog") != 0 && tableinfo.relreplident != 'd') ||
2992  (strcmp(schemaname, "pg_catalog") == 0 && tableinfo.relreplident != 'n')))
2993  {
2994  const char *s = _("Replica Identity");
2995 
2996  printfPQExpBuffer(&buf, "%s: %s",
2997  s,
2998  tableinfo.relreplident == 'f' ? "FULL" :
2999  tableinfo.relreplident == 'n' ? "NOTHING" :
3000  "???");
3001 
3002  printTableAddFooter(&cont, buf.data);
3003  }
3004 
3005  /* OIDs, if verbose and not a materialized view */
3006  if (verbose && tableinfo.relkind != RELKIND_MATVIEW && tableinfo.hasoids)
3007  printTableAddFooter(&cont, _("Has OIDs: yes"));
3008 
3009  /* Tablespace info */
3010  add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
3011  true);
3012  }
3013 
3014  /* reloptions, if verbose */
3015  if (verbose &&
3016  tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
3017  {
3018  const char *t = _("Options");
3019 
3020  printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
3021  printTableAddFooter(&cont, buf.data);
3022  }
3023 
3024  printTable(&cont, pset.queryFout, false, pset.logfile);
3025 
3026  retval = true;
3027 
3028 error_return:
3029 
3030  /* clean up */
3031  if (printTableInitialized)
3032  printTableCleanup(&cont);
3033  termPQExpBuffer(&buf);
3034  termPQExpBuffer(&title);
3035  termPQExpBuffer(&tmpbuf);
3036 
3037  if (view_def)
3038  free(view_def);
3039 
3040  if (res)
3041  PQclear(res);
3042 
3043  return retval;
3044 }
3045 
3046 /*
3047  * Add a tablespace description to a footer. If 'newline' is true, it is added
3048  * in a new line; otherwise it's appended to the current value of the last
3049  * footer.
3050  */
3051 static void
3052 add_tablespace_footer(printTableContent *const cont, char relkind,
3053  Oid tablespace, const bool newline)
3054 {
3055  /* relkinds for which we support tablespaces */
3056  if (relkind == RELKIND_RELATION ||
3057  relkind == RELKIND_MATVIEW ||
3058  relkind == RELKIND_INDEX ||
3059  relkind == RELKIND_PARTITIONED_TABLE)
3060  {
3061  /*
3062  * We ignore the database default tablespace so that users not using
3063  * tablespaces don't need to know about them. This case also covers
3064  * pre-8.0 servers, for which tablespace will always be 0.
3065  */
3066  if (tablespace != 0)
3067  {
3068  PGresult *result = NULL;
3070 
3071  initPQExpBuffer(&buf);
3072  printfPQExpBuffer(&buf,
3073  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3074  "WHERE oid = '%u';", tablespace);
3075  result = PSQLexec(buf.data);
3076  if (!result)
3077  {
3078  termPQExpBuffer(&buf);
3079  return;
3080  }
3081  /* Should always be the case, but.... */
3082  if (PQntuples(result) > 0)
3083  {
3084  if (newline)
3085  {
3086  /* Add the tablespace as a new footer */
3087  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3088  PQgetvalue(result, 0, 0));
3089  printTableAddFooter(cont, buf.data);
3090  }
3091  else
3092  {
3093  /* Append the tablespace to the latest footer */
3094  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3095 
3096  /*-------
3097  translator: before this string there's an index description like
3098  '"foo_pkey" PRIMARY KEY, btree (a)' */
3099  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3100  PQgetvalue(result, 0, 0));
3101  printTableSetFooter(cont, buf.data);
3102  }
3103  }
3104  PQclear(result);
3105  termPQExpBuffer(&buf);
3106  }
3107  }
3108 }
3109 
3110 /*
3111  * \du or \dg
3112  *
3113  * Describes roles. Any schema portion of the pattern is ignored.
3114  */
3115 bool
3116 describeRoles(const char *pattern, bool verbose, bool showSystem)
3117 {
3119  PGresult *res;
3120  printTableContent cont;
3121  printTableOpt myopt = pset.popt.topt;
3122  int ncols = 3;
3123  int nrows = 0;
3124  int i;
3125  int conns;
3126  const char align = 'l';
3127  char **attr;
3128 
3129  myopt.default_footer = false;
3130 
3131  initPQExpBuffer(&buf);
3132 
3133  if (pset.sversion >= 80100)
3134  {
3135  printfPQExpBuffer(&buf,
3136  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3137  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3138  " r.rolconnlimit, r.rolvaliduntil,\n"
3139  " ARRAY(SELECT b.rolname\n"
3140  " FROM pg_catalog.pg_auth_members m\n"
3141  " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
3142  " WHERE m.member = r.oid) as memberof");
3143 
3144  if (verbose && pset.sversion >= 80200)
3145  {
3146  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3147  ncols++;
3148  }
3149  if (pset.sversion >= 90100)
3150  {
3151  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3152  }
3153 
3154  if (pset.sversion >= 90500)
3155  {
3156  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3157  }
3158 
3159  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3160 
3161  if (!showSystem && !pattern)
3162  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3163 
3164  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3165  NULL, "r.rolname", NULL, NULL);
3166  }
3167  else
3168  {
3169  printfPQExpBuffer(&buf,
3170  "SELECT u.usename AS rolname,\n"
3171  " u.usesuper AS rolsuper,\n"
3172  " true AS rolinherit, false AS rolcreaterole,\n"
3173  " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
3174  " -1 AS rolconnlimit,"
3175  " u.valuntil as rolvaliduntil,\n"
3176  " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
3177  "\nFROM pg_catalog.pg_user u\n");
3178 
3179  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3180  NULL, "u.usename", NULL, NULL);
3181  }
3182 
3183  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3184 
3185  res = PSQLexec(buf.data);
3186  if (!res)
3187  return false;
3188 
3189  nrows = PQntuples(res);
3190  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3191 
3192  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3193 
3194  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3195  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3196  printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
3197 
3198  if (verbose && pset.sversion >= 80200)
3199  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3200 
3201  for (i = 0; i < nrows; i++)
3202  {
3203  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3204 
3205  resetPQExpBuffer(&buf);
3206  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3207  add_role_attribute(&buf, _("Superuser"));
3208 
3209  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3210  add_role_attribute(&buf, _("No inheritance"));
3211 
3212  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3213  add_role_attribute(&buf, _("Create role"));
3214 
3215  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3216  add_role_attribute(&buf, _("Create DB"));
3217 
3218  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3219  add_role_attribute(&buf, _("Cannot login"));
3220 
3221  if (pset.sversion >= 90100)
3222  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3223  add_role_attribute(&buf, _("Replication"));
3224 
3225  if (pset.sversion >= 90500)
3226  if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
3227  add_role_attribute(&buf, _("Bypass RLS"));
3228 
3229  conns = atoi(PQgetvalue(res, i, 6));
3230  if (conns >= 0)
3231  {
3232  if (buf.len > 0)
3233  appendPQExpBufferChar(&buf, '\n');
3234 
3235  if (conns == 0)
3236  appendPQExpBufferStr(&buf, _("No connections"));
3237  else
3238  appendPQExpBuffer(&buf, ngettext("%d connection",
3239  "%d connections",
3240  conns),
3241  conns);
3242  }
3243 
3244  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3245  {
3246  if (buf.len > 0)
3247  appendPQExpBufferChar(&buf, '\n');
3248  appendPQExpBufferStr(&buf, _("Password valid until "));
3249  appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3250  }
3251 
3252  attr[i] = pg_strdup(buf.data);
3253 
3254  printTableAddCell(&cont, attr[i], false, false);
3255 
3256  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3257 
3258  if (verbose && pset.sversion >= 80200)
3259  printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
3260  }
3261  termPQExpBuffer(&buf);
3262 
3263  printTable(&cont, pset.queryFout, false, pset.logfile);
3264  printTableCleanup(&cont);
3265 
3266  for (i = 0; i < nrows; i++)
3267  free(attr[i]);
3268  free(attr);
3269 
3270  PQclear(res);
3271  return true;
3272 }
3273 
3274 static void
3276 {
3277  if (buf->len > 0)
3278  appendPQExpBufferStr(buf, ", ");
3279 
3280  appendPQExpBufferStr(buf, str);
3281 }
3282 
3283 /*
3284  * \drds
3285  */
3286 bool
3287 listDbRoleSettings(const char *pattern, const char *pattern2)
3288 {
3290  PGresult *res;
3291  printQueryOpt myopt = pset.popt;
3292  bool havewhere;
3293 
3294  if (pset.sversion < 90000)
3295  {
3296  char sverbuf[32];
3297 
3298  psql_error("The server (version %s) does not support per-database role settings.\n",
3300  sverbuf, sizeof(sverbuf)));
3301  return true;
3302  }
3303 
3304  initPQExpBuffer(&buf);
3305 
3306  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3307  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3308  "FROM pg_catalog.pg_db_role_setting s\n"
3309  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3310  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3311  gettext_noop("Role"),
3312  gettext_noop("Database"),
3313  gettext_noop("Settings"));
3314  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3315  NULL, "r.rolname", NULL, NULL);
3316  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3317  NULL, "d.datname", NULL, NULL);
3318  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3319 
3320  res = PSQLexec(buf.data);
3321  termPQExpBuffer(&buf);
3322  if (!res)
3323  return false;
3324 
3325  /*
3326  * Most functions in this file are content to print an empty table when
3327  * there are no matching objects. We intentionally deviate from that
3328  * here, but only in !quiet mode, because of the possibility that the user
3329  * is confused about what the two pattern arguments mean.
3330  */
3331  if (PQntuples(res) == 0 && !pset.quiet)
3332  {
3333  if (pattern && pattern2)
3334  psql_error("Did not find any settings for role \"%s\" and database \"%s\".\n",
3335  pattern, pattern2);
3336  else if (pattern)
3337  psql_error("Did not find any settings for role \"%s\".\n",
3338  pattern);
3339  else
3340  psql_error("Did not find any settings.\n");
3341  }
3342  else
3343  {
3344  myopt.nullPrint = NULL;
3345  myopt.title = _("List of settings");
3346  myopt.translate_header = true;
3347 
3348  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3349  }
3350 
3351  PQclear(res);
3352  return true;
3353 }
3354 
3355 
3356 /*
3357  * listTables()
3358  *
3359  * handler for \dt, \di, etc.
3360  *
3361  * tabtypes is an array of characters, specifying what info is desired:
3362  * t - tables
3363  * i - indexes
3364  * v - views
3365  * m - materialized views
3366  * s - sequences
3367  * E - foreign table (Note: different from 'f', the relkind value)
3368  * (any order of the above is fine)
3369  */
3370 bool
3371 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
3372 {
3373  bool showTables = strchr(tabtypes, 't') != NULL;
3374  bool showIndexes = strchr(tabtypes, 'i') != NULL;
3375  bool showViews = strchr(tabtypes, 'v') != NULL;
3376  bool showMatViews = strchr(tabtypes, 'm') != NULL;
3377  bool showSeq = strchr(tabtypes, 's') != NULL;
3378  bool showForeign = strchr(tabtypes, 'E') != NULL;
3379 
3381  PGresult *res;
3382  printQueryOpt myopt = pset.popt;
3383  static const bool translate_columns[] = {false, false, true, false, false, false, false};
3384 
3385  /* If tabtypes is empty, we default to \dtvmsE (but see also command.c) */
3386  if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
3387  showTables = showViews = showMatViews = showSeq = showForeign = true;
3388 
3389  initPQExpBuffer(&buf);
3390 
3391  /*
3392  * Note: as of Pg 8.2, we no longer use relkind 's' (special), but we keep
3393  * it here for backwards compatibility.
3394  */
3395  printfPQExpBuffer(&buf,
3396  "SELECT n.nspname as \"%s\",\n"
3397  " c.relname as \"%s\",\n"
3398  " CASE c.relkind"
3399  " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
3400  " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
3401  " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
3402  " WHEN " CppAsString2(RELKIND_INDEX) " THEN '%s'"
3403  " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
3404  " WHEN 's' THEN '%s'"
3405  " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
3406  " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
3407  " WHEN " CppAsString2(RELKIND_PARTITIONED_INDEX) " THEN '%s'"
3408  " END as \"%s\",\n"
3409  " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
3410  gettext_noop("Schema"),
3411  gettext_noop("Name"),
3412  gettext_noop("table"),
3413  gettext_noop("view"),
3414  gettext_noop("materialized view"),
3415  gettext_noop("index"),
3416  gettext_noop("sequence"),
3417  gettext_noop("special"),
3418  gettext_noop("foreign table"),
3419  gettext_noop("table"), /* partitioned table */
3420  gettext_noop("index"), /* partitioned index */
3421  gettext_noop("Type"),
3422  gettext_noop("Owner"));
3423 
3424  if (showIndexes)
3425  appendPQExpBuffer(&buf,
3426  ",\n c2.relname as \"%s\"",
3427  gettext_noop("Table"));
3428 
3429  if (verbose)
3430  {
3431  /*
3432  * As of PostgreSQL 9.0, use pg_table_size() to show a more accurate
3433  * size of a table, including FSM, VM and TOAST tables.
3434  */
3435  if (pset.sversion >= 90000)
3436  appendPQExpBuffer(&buf,
3437  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
3438  gettext_noop("Size"));
3439  else if (pset.sversion >= 80100)
3440  appendPQExpBuffer(&buf,
3441  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
3442  gettext_noop("Size"));
3443 
3444  appendPQExpBuffer(&buf,
3445  ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
3446  gettext_noop("Description"));
3447  }
3448 
3449  appendPQExpBufferStr(&buf,
3450  "\nFROM pg_catalog.pg_class c"
3451  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
3452  if (showIndexes)
3453  appendPQExpBufferStr(&buf,
3454  "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
3455  "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
3456 
3457  appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
3458  if (showTables)
3461  if (showViews)
3463  if (showMatViews)
3465  if (showIndexes)
3468  if (showSeq)
3470  if (showSystem || pattern)
3471  appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL */
3472  if (showForeign)
3474 
3475  appendPQExpBufferStr(&buf, "''"); /* dummy */
3476  appendPQExpBufferStr(&buf, ")\n");
3477 
3478  if (!showSystem && !pattern)
3479  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3480  " AND n.nspname <> 'information_schema'\n");
3481 
3482  /*
3483  * TOAST objects are suppressed unconditionally. Since we don't provide
3484  * any way to select RELKIND_TOASTVALUE above, we would never show toast
3485  * tables in any case; it seems a bit confusing to allow their indexes to
3486  * be shown. Use plain \d if you really need to look at a TOAST
3487  * table/index.
3488  */
3489  appendPQExpBufferStr(&buf, " AND n.nspname !~ '^pg_toast'\n");
3490 
3491  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3492  "n.nspname", "c.relname", NULL,
3493  "pg_catalog.pg_table_is_visible(c.oid)");
3494 
3495  appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
3496 
3497  res = PSQLexec(buf.data);
3498  termPQExpBuffer(&buf);
3499  if (!res)
3500  return false;
3501 
3502  /*
3503  * Most functions in this file are content to print an empty table when
3504  * there are no matching objects. We intentionally deviate from that
3505  * here, but only in !quiet mode, for historical reasons.
3506  */
3507  if (PQntuples(res) == 0 && !pset.quiet)
3508  {
3509  if (pattern)
3510  psql_error("Did not find any relation named \"%s\".\n",
3511  pattern);
3512  else
3513  psql_error("Did not find any relations.\n");
3514  }
3515  else
3516  {
3517  myopt.nullPrint = NULL;
3518  myopt.title = _("List of relations");
3519  myopt.translate_header = true;
3520  myopt.translate_columns = translate_columns;
3521  myopt.n_translate_columns = lengthof(translate_columns);
3522 
3523  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3524  }
3525 
3526  PQclear(res);
3527  return true;
3528 }
3529 
3530 
3531 /*
3532  * \dL
3533  *
3534  * Describes languages.
3535  */
3536 bool
3537 listLanguages(const char *pattern, bool verbose, bool showSystem)
3538 {
3540  PGresult *res;
3541  printQueryOpt myopt = pset.popt;
3542 
3543  initPQExpBuffer(&buf);
3544 
3545  printfPQExpBuffer(&buf,
3546  "SELECT l.lanname AS \"%s\",\n",
3547  gettext_noop("Name"));
3548  if (pset.sversion >= 80300)
3549  appendPQExpBuffer(&buf,
3550  " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
3551  gettext_noop("Owner"));
3552 
3553  appendPQExpBuffer(&buf,
3554  " l.lanpltrusted AS \"%s\"",
3555  gettext_noop("Trusted"));
3556 
3557  if (verbose)
3558  {
3559  appendPQExpBuffer(&buf,
3560  ",\n NOT l.lanispl AS \"%s\",\n"
3561  " l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
3562  " l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n ",
3563  gettext_noop("Internal language"),
3564  gettext_noop("Call handler"),
3565  gettext_noop("Validator"));
3566  if (pset.sversion >= 90000)
3567  appendPQExpBuffer(&buf, "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
3568  gettext_noop("Inline handler"));
3569  printACLColumn(&buf, "l.lanacl");
3570  }
3571 
3572  appendPQExpBuffer(&buf,
3573  ",\n d.description AS \"%s\""
3574  "\nFROM pg_catalog.pg_language l\n"
3575  "LEFT JOIN pg_catalog.pg_description d\n"
3576  " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
3577  " AND d.objsubid = 0\n",
3578  gettext_noop("Description"));
3579 
3580  if (pattern)
3581  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3582  NULL, "l.lanname", NULL, NULL);
3583 
3584  if (!showSystem && !pattern)
3585  appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
3586 
3587 
3588  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3589 
3590  res = PSQLexec(buf.data);
3591  termPQExpBuffer(&buf);
3592  if (!res)
3593  return false;
3594 
3595  myopt.nullPrint = NULL;
3596  myopt.title = _("List of languages");
3597  myopt.translate_header = true;
3598 
3599  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3600 
3601  PQclear(res);
3602  return true;
3603 }
3604 
3605 
3606 /*
3607  * \dD
3608  *
3609  * Describes domains.
3610  */
3611 bool
3612 listDomains(const char *pattern, bool verbose, bool showSystem)
3613 {
3615  PGresult *res;
3616  printQueryOpt myopt = pset.popt;
3617 
3618  initPQExpBuffer(&buf);
3619 
3620  printfPQExpBuffer(&buf,
3621  "SELECT n.nspname as \"%s\",\n"
3622  " t.typname as \"%s\",\n"
3623  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
3624  gettext_noop("Schema"),
3625  gettext_noop("Name"),
3626  gettext_noop("Type"));
3627 
3628  if (pset.sversion >= 90100)
3629  appendPQExpBuffer(&buf,
3630  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3631  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
3632  gettext_noop("Collation"));
3633  appendPQExpBuffer(&buf,
3634  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
3635  " t.typdefault as \"%s\",\n"
3636  " pg_catalog.array_to_string(ARRAY(\n"
3637  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3638  " ), ' ') as \"%s\"",
3639  gettext_noop("Nullable"),
3640  gettext_noop("Default"),
3641  gettext_noop("Check"));
3642 
3643  if (verbose)
3644  {
3645  if (pset.sversion >= 90200)
3646  {
3647  appendPQExpBufferStr(&buf, ",\n ");
3648  printACLColumn(&buf, "t.typacl");
3649  }
3650  appendPQExpBuffer(&buf,
3651  ",\n d.description as \"%s\"",
3652  gettext_noop("Description"));
3653  }
3654 
3655  appendPQExpBufferStr(&buf,
3656  "\nFROM pg_catalog.pg_type t\n"
3657  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3658 
3659  if (verbose)
3660  appendPQExpBufferStr(&buf,
3661  " LEFT JOIN pg_catalog.pg_description d "
3662  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3663  "AND d.objsubid = 0\n");
3664 
3665  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3666 
3667  if (!showSystem && !pattern)
3668  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3669  " AND n.nspname <> 'information_schema'\n");
3670 
3671  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3672  "n.nspname", "t.typname", NULL,
3673  "pg_catalog.pg_type_is_visible(t.oid)");
3674 
3675  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3676 
3677  res = PSQLexec(buf.data);
3678  termPQExpBuffer(&buf);
3679  if (!res)
3680  return false;
3681 
3682  myopt.nullPrint = NULL;
3683  myopt.title = _("List of domains");
3684  myopt.translate_header = true;
3685 
3686  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3687 
3688  PQclear(res);
3689  return true;
3690 }
3691 
3692 /*
3693  * \dc
3694  *
3695  * Describes conversions.
3696  */
3697 bool
3698 listConversions(const char *pattern, bool verbose, bool showSystem)
3699 {
3701  PGresult *res;
3702  printQueryOpt myopt = pset.popt;
3703  static const bool translate_columns[] =
3704  {false, false, false, false, true, false};
3705 
3706  initPQExpBuffer(&buf);
3707 
3708  printfPQExpBuffer(&buf,
3709  "SELECT n.nspname AS \"%s\",\n"
3710  " c.conname AS \"%s\",\n"
3711  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3712  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3713  " CASE WHEN c.condefault THEN '%s'\n"
3714  " ELSE '%s' END AS \"%s\"",
3715  gettext_noop("Schema"),
3716  gettext_noop("Name"),
3717  gettext_noop("Source"),
3718  gettext_noop("Destination"),
3719  gettext_noop("yes"), gettext_noop("no"),
3720  gettext_noop("Default?"));
3721 
3722  if (verbose)
3723  appendPQExpBuffer(&buf,
3724  ",\n d.description AS \"%s\"",
3725  gettext_noop("Description"));
3726 
3727  appendPQExpBufferStr(&buf,
3728  "\nFROM pg_catalog.pg_conversion c\n"
3729  " JOIN pg_catalog.pg_namespace n "
3730  "ON n.oid = c.connamespace\n");
3731 
3732  if (verbose)
3733  appendPQExpBufferStr(&buf,
3734  "LEFT JOIN pg_catalog.pg_description d "
3735  "ON d.classoid = c.tableoid\n"
3736  " AND d.objoid = c.oid "
3737  "AND d.objsubid = 0\n");
3738 
3739  appendPQExpBufferStr(&buf, "WHERE true\n");
3740 
3741  if (!showSystem && !pattern)
3742  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3743  " AND n.nspname <> 'information_schema'\n");
3744 
3745  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3746  "n.nspname", "c.conname", NULL,
3747  "pg_catalog.pg_conversion_is_visible(c.oid)");
3748 
3749  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3750 
3751  res = PSQLexec(buf.data);
3752  termPQExpBuffer(&buf);
3753  if (!res)
3754  return false;
3755 
3756  myopt.nullPrint = NULL;
3757  myopt.title = _("List of conversions");
3758  myopt.translate_header = true;
3759  myopt.translate_columns = translate_columns;
3760  myopt.n_translate_columns = lengthof(translate_columns);
3761 
3762  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3763 
3764  PQclear(res);
3765  return true;
3766 }
3767 
3768 /*
3769  * \dy
3770  *
3771  * Describes Event Triggers.
3772  */
3773 bool
3774 listEventTriggers(const char *pattern, bool verbose)
3775 {
3777  PGresult *res;
3778  printQueryOpt myopt = pset.popt;
3779  static const bool translate_columns[] =
3780  {false, false, false, true, false, false, false};
3781 
3782  initPQExpBuffer(&buf);
3783 
3784  printfPQExpBuffer(&buf,
3785  "SELECT evtname as \"%s\", "
3786  "evtevent as \"%s\", "
3787  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3788  " case evtenabled when 'O' then '%s'"
3789  " when 'R' then '%s'"
3790  " when 'A' then '%s'"
3791  " when 'D' then '%s' end as \"%s\",\n"
3792  " e.evtfoid::pg_catalog.regproc as \"%s\", "
3793  "pg_catalog.array_to_string(array(select x"
3794  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3795  gettext_noop("Name"),
3796  gettext_noop("Event"),
3797  gettext_noop("Owner"),
3798  gettext_noop("enabled"),
3799  gettext_noop("replica"),
3800  gettext_noop("always"),
3801  gettext_noop("disabled"),
3802  gettext_noop("Enabled"),
3803  gettext_noop("Procedure"),
3804  gettext_noop("Tags"));
3805  if (verbose)
3806  appendPQExpBuffer(&buf,
3807  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3808  gettext_noop("Description"));
3809  appendPQExpBufferStr(&buf,
3810  "\nFROM pg_catalog.pg_event_trigger e ");
3811 
3812  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3813  NULL, "evtname", NULL, NULL);
3814 
3815  appendPQExpBufferStr(&buf, "ORDER BY 1");
3816 
3817  res = PSQLexec(buf.data);
3818  termPQExpBuffer(&buf);
3819  if (!res)
3820  return false;
3821 
3822  myopt.nullPrint = NULL;
3823  myopt.title = _("List of event triggers");
3824  myopt.translate_header = true;
3825  myopt.translate_columns = translate_columns;
3826  myopt.n_translate_columns = lengthof(translate_columns);
3827 
3828  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3829 
3830  PQclear(res);
3831  return true;
3832 }
3833 
3834 /*
3835  * \dC
3836  *
3837  * Describes casts.
3838  */
3839 bool
3840 listCasts(const char *pattern, bool verbose)
3841 {
3843  PGresult *res;
3844  printQueryOpt myopt = pset.popt;
3845  static const bool translate_columns[] = {false, false, false, true, false};
3846 
3847  initPQExpBuffer(&buf);
3848 
3849  /*
3850  * We need a left join to pg_proc for binary casts; the others are just
3851  * paranoia. Also note that we don't attempt to localize '(binary
3852  * coercible)', because there's too much risk of gettext translating a
3853  * function name that happens to match some string in the PO database.
3854  */
3855  printfPQExpBuffer(&buf,
3856  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3857  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3858  " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3859  " ELSE p.proname\n"
3860  " END as \"%s\",\n"
3861  " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3862  " WHEN c.castcontext = 'a' THEN '%s'\n"
3863  " ELSE '%s'\n"
3864  " END as \"%s\"",
3865  gettext_noop("Source type"),
3866  gettext_noop("Target type"),
3867  gettext_noop("Function"),
3868  gettext_noop("no"),
3869  gettext_noop("in assignment"),
3870  gettext_noop("yes"),
3871  gettext_noop("Implicit?"));
3872 
3873  if (verbose)
3874  appendPQExpBuffer(&buf,
3875  ",\n d.description AS \"%s\"\n",
3876  gettext_noop("Description"));
3877 
3878  appendPQExpBufferStr(&buf,
3879  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3880  " ON c.castfunc = p.oid\n"
3881  " LEFT JOIN pg_catalog.pg_type ts\n"
3882  " ON c.castsource = ts.oid\n"
3883  " LEFT JOIN pg_catalog.pg_namespace ns\n"
3884  " ON ns.oid = ts.typnamespace\n"
3885  " LEFT JOIN pg_catalog.pg_type tt\n"
3886  " ON c.casttarget = tt.oid\n"
3887  " LEFT JOIN pg_catalog.pg_namespace nt\n"
3888  " ON nt.oid = tt.typnamespace\n");
3889 
3890  if (verbose)
3891  appendPQExpBufferStr(&buf,
3892  " LEFT JOIN pg_catalog.pg_description d\n"
3893  " ON d.classoid = c.tableoid AND d.objoid = "
3894  "c.oid AND d.objsubid = 0\n");
3895 
3896  appendPQExpBufferStr(&buf, "WHERE ( (true");
3897 
3898  /*
3899  * Match name pattern against either internal or external name of either
3900  * castsource or casttarget
3901  */
3902  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3903  "ns.nspname", "ts.typname",
3904  "pg_catalog.format_type(ts.oid, NULL)",
3905  "pg_catalog.pg_type_is_visible(ts.oid)");
3906 
3907  appendPQExpBufferStr(&buf, ") OR (true");
3908 
3909  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3910  "nt.nspname", "tt.typname",
3911  "pg_catalog.format_type(tt.oid, NULL)",
3912  "pg_catalog.pg_type_is_visible(tt.oid)");
3913 
3914  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3915 
3916  res = PSQLexec(buf.data);
3917  termPQExpBuffer(&buf);
3918  if (!res)
3919  return false;
3920 
3921  myopt.nullPrint = NULL;
3922  myopt.title = _("List of casts");
3923  myopt.translate_header = true;
3924  myopt.translate_columns = translate_columns;
3925  myopt.n_translate_columns = lengthof(translate_columns);
3926 
3927  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3928 
3929  PQclear(res);
3930  return true;
3931 }
3932 
3933 /*
3934  * \dO
3935  *
3936  * Describes collations.
3937  */
3938 bool
3939 listCollations(const char *pattern, bool verbose, bool showSystem)
3940 {
3942  PGresult *res;
3943  printQueryOpt myopt = pset.popt;
3944  static const bool translate_columns[] = {false, false, false, false, false, false};
3945 
3946  if (pset.sversion < 90100)
3947  {
3948  char sverbuf[32];
3949 
3950  psql_error("The server (version %s) does not support collations.\n",
3952  sverbuf, sizeof(sverbuf)));
3953  return true;
3954  }
3955 
3956  initPQExpBuffer(&buf);
3957 
3958  printfPQExpBuffer(&buf,
3959  "SELECT n.nspname AS \"%s\",\n"
3960  " c.collname AS \"%s\",\n"
3961  " c.collcollate AS \"%s\",\n"
3962  " c.collctype AS \"%s\"",
3963  gettext_noop("Schema"),
3964  gettext_noop("Name"),
3965  gettext_noop("Collate"),
3966  gettext_noop("Ctype"));
3967 
3968  if (pset.sversion >= 100000)
3969  appendPQExpBuffer(&buf,
3970  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
3971  gettext_noop("Provider"));
3972 
3973  if (verbose)
3974  appendPQExpBuffer(&buf,
3975  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3976  gettext_noop("Description"));
3977 
3978  appendPQExpBufferStr(&buf,
3979  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3980  "WHERE n.oid = c.collnamespace\n");
3981 
3982  if (!showSystem && !pattern)
3983  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3984  " AND n.nspname <> 'information_schema'\n");
3985 
3986  /*
3987  * Hide collations that aren't usable in the current database's encoding.
3988  * If you think to change this, note that pg_collation_is_visible rejects
3989  * unusable collations, so you will need to hack name pattern processing
3990  * somehow to avoid inconsistent behavior.
3991  */
3992  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3993 
3994  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3995  "n.nspname", "c.collname", NULL,
3996  "pg_catalog.pg_collation_is_visible(c.oid)");
3997 
3998  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3999 
4000  res = PSQLexec(buf.data);
4001  termPQExpBuffer(&buf);
4002  if (!res)
4003  return false;
4004 
4005  myopt.nullPrint = NULL;
4006  myopt.title = _("List of collations");
4007  myopt.translate_header = true;
4008  myopt.translate_columns = translate_columns;
4009  myopt.n_translate_columns = lengthof(translate_columns);
4010 
4011  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4012 
4013  PQclear(res);
4014  return true;
4015 }
4016 
4017 /*
4018  * \dn
4019  *
4020  * Describes schemas (namespaces)
4021  */
4022 bool
4023 listSchemas(const char *pattern, bool verbose, bool showSystem)
4024 {
4026  PGresult *res;
4027  printQueryOpt myopt = pset.popt;
4028 
4029  initPQExpBuffer(&buf);
4030  printfPQExpBuffer(&buf,
4031  "SELECT n.nspname AS \"%s\",\n"
4032  " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
4033  gettext_noop("Name"),
4034  gettext_noop("Owner"));
4035 
4036  if (verbose)
4037  {
4038  appendPQExpBufferStr(&buf, ",\n ");
4039  printACLColumn(&buf, "n.nspacl");
4040  appendPQExpBuffer(&buf,
4041  ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
4042  gettext_noop("Description"));
4043  }
4044 
4045  appendPQExpBuffer(&buf,
4046  "\nFROM pg_catalog.pg_namespace n\n");
4047 
4048  if (!showSystem && !pattern)
4049  appendPQExpBufferStr(&buf,
4050  "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
4051 
4052  processSQLNamePattern(pset.db, &buf, pattern,
4053  !showSystem && !pattern, false,
4054  NULL, "n.nspname", NULL,
4055  NULL);
4056 
4057  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4058 
4059  res = PSQLexec(buf.data);
4060  termPQExpBuffer(&buf);
4061  if (!res)
4062  return false;
4063 
4064  myopt.nullPrint = NULL;
4065  myopt.title = _("List of schemas");
4066  myopt.translate_header = true;
4067 
4068  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4069 
4070  PQclear(res);
4071  return true;
4072 }
4073 
4074 
4075 /*
4076  * \dFp
4077  * list text search parsers
4078  */
4079 bool
4080 listTSParsers(const char *pattern, bool verbose)
4081 {
4083  PGresult *res;
4084  printQueryOpt myopt = pset.popt;
4085 
4086  if (pset.sversion < 80300)
4087  {
4088  char sverbuf[32];
4089 
4090  psql_error("The server (version %s) does not support full text search.\n",
4092  sverbuf, sizeof(sverbuf)));
4093  return true;
4094  }
4095 
4096  if (verbose)
4097  return listTSParsersVerbose(pattern);
4098 
4099  initPQExpBuffer(&buf);
4100 
4101  printfPQExpBuffer(&buf,
4102  "SELECT\n"
4103  " n.nspname as \"%s\",\n"
4104  " p.prsname as \"%s\",\n"
4105  " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
4106  "FROM pg_catalog.pg_ts_parser p\n"
4107  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
4108  gettext_noop("Schema"),
4109  gettext_noop("Name"),
4110  gettext_noop("Description")
4111  );
4112 
4113  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4114  "n.nspname", "p.prsname", NULL,
4115  "pg_catalog.pg_ts_parser_is_visible(p.oid)");
4116 
4117  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4118 
4119  res = PSQLexec(buf.data);
4120  termPQExpBuffer(&buf);
4121  if (!res)
4122  return false;
4123 
4124  myopt.nullPrint = NULL;
4125  myopt.title = _("List of text search parsers");
4126  myopt.translate_header = true;
4127 
4128  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4129 
4130  PQclear(res);
4131  return true;
4132 }
4133 
4134 /*
4135  * full description of parsers
4136  */
4137 static bool
4138 listTSParsersVerbose(const char *pattern)
4139 {
4141  PGresult *res;
4142  int i;
4143 
4144  initPQExpBuffer(&buf);
4145 
4146  printfPQExpBuffer(&buf,
4147  "SELECT p.oid,\n"
4148  " n.nspname,\n"
4149  " p.prsname\n"
4150  "FROM pg_catalog.pg_ts_parser p\n"
4151  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
4152  );
4153 
4154  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4155  "n.nspname", "p.prsname", NULL,
4156  "pg_catalog.pg_ts_parser_is_visible(p.oid)");
4157 
4158  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4159 
4160  res = PSQLexec(buf.data);
4161  termPQExpBuffer(&buf);
4162  if (!res)
4163  return false;
4164 
4165  if (PQntuples(res) == 0)
4166  {
4167  if (!pset.quiet)
4168  {
4169  if (pattern)
4170  psql_error("Did not find any text search parser named \"%s\".\n",
4171  pattern);
4172  else
4173  psql_error("Did not find any text search parsers.\n");
4174  }
4175  PQclear(res);
4176  return false;
4177  }
4178 
4179  for (i = 0; i < PQntuples(res); i++)
4180  {
4181  const char *oid;
4182  const char *nspname = NULL;
4183  const char *prsname;
4184 
4185  oid = PQgetvalue(res, i, 0);
4186  if (!PQgetisnull(res, i, 1))
4187  nspname = PQgetvalue(res, i, 1);
4188  prsname = PQgetvalue(res, i, 2);
4189 
4190  if (!describeOneTSParser(oid, nspname, prsname))
4191  {
4192  PQclear(res);
4193  return false;
4194  }
4195 
4196  if (cancel_pressed)
4197  {
4198  PQclear(res);
4199  return false;
4200  }
4201  }
4202 
4203  PQclear(res);
4204  return true;
4205 }
4206 
4207 static bool
4208 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
4209 {
4211  PGresult *res;
4212  PQExpBufferData title;
4213  printQueryOpt myopt = pset.popt;
4214  static const bool translate_columns[] = {true, false, false};
4215 
4216  initPQExpBuffer(&buf);
4217 
4218  printfPQExpBuffer(&buf,
4219  "SELECT '%s' AS \"%s\",\n"
4220  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4221  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4222  " FROM pg_catalog.pg_ts_parser p\n"
4223  " WHERE p.oid = '%s'\n"
4224  "UNION ALL\n"
4225  "SELECT '%s',\n"
4226  " p.prstoken::pg_catalog.regproc,\n"
4227  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4228  " FROM pg_catalog.pg_ts_parser p\n"
4229  " WHERE p.oid = '%s'\n"
4230  "UNION ALL\n"
4231  "SELECT '%s',\n"
4232  " p.prsend::pg_catalog.regproc,\n"
4233  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4234  " FROM pg_catalog.pg_ts_parser p\n"
4235  " WHERE p.oid = '%s'\n"
4236  "UNION ALL\n"
4237  "SELECT '%s',\n"
4238  " p.prsheadline::pg_catalog.regproc,\n"
4239  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4240  " FROM pg_catalog.pg_ts_parser p\n"
4241  " WHERE p.oid = '%s'\n"
4242  "UNION ALL\n"
4243  "SELECT '%s',\n"
4244  " p.prslextype::pg_catalog.regproc,\n"
4245  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4246  " FROM pg_catalog.pg_ts_parser p\n"
4247  " WHERE p.oid = '%s';",
4248  gettext_noop("Start parse"),
4249  gettext_noop("Method"),
4250  gettext_noop("Function"),
4251  gettext_noop("Description"),
4252  oid,
4253  gettext_noop("Get next token"),
4254  oid,
4255  gettext_noop("End parse"),
4256  oid,
4257  gettext_noop("Get headline"),
4258  oid,
4259  gettext_noop("Get token types"),
4260  oid);
4261 
4262  res = PSQLexec(buf.data);
4263  termPQExpBuffer(&buf);
4264  if (!res)
4265  return false;
4266 
4267  myopt.nullPrint = NULL;
4268  initPQExpBuffer(&title);
4269  if (nspname)
4270  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4271  nspname, prsname);
4272  else
4273  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4274  myopt.title = title.data;
4275  myopt.footers = NULL;
4276  myopt.topt.default_footer = false;
4277  myopt.translate_header = true;
4278  myopt.translate_columns = translate_columns;
4279  myopt.n_translate_columns = lengthof(translate_columns);
4280 
4281  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4282 
4283  PQclear(res);
4284 
4285  initPQExpBuffer(&buf);
4286 
4287  printfPQExpBuffer(&buf,
4288  "SELECT t.alias as \"%s\",\n"
4289  " t.description as \"%s\"\n"
4290  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4291  "ORDER BY 1;",
4292  gettext_noop("Token name"),
4293  gettext_noop("Description"),
4294  oid);
4295 
4296  res = PSQLexec(buf.data);
4297  termPQExpBuffer(&buf);
4298  if (!res)
4299  return false;
4300 
4301  myopt.nullPrint = NULL;
4302  if (nspname)
4303  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4304  nspname, prsname);
4305  else
4306  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4307  myopt.title = title.data;
4308  myopt.footers = NULL;
4309  myopt.topt.default_footer = true;
4310  myopt.translate_header = true;
4311  myopt.translate_columns = NULL;
4312  myopt.n_translate_columns = 0;
4313 
4314  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4315 
4316  termPQExpBuffer(&title);
4317  PQclear(res);
4318  return true;
4319 }
4320 
4321 
4322 /*
4323  * \dFd
4324  * list text search dictionaries
4325  */
4326 bool
4327 listTSDictionaries(const char *pattern, bool verbose)
4328 {
4330  PGresult *res;
4331  printQueryOpt myopt = pset.popt;
4332 
4333  if (pset.sversion < 80300)
4334  {
4335  char sverbuf[32];
4336 
4337  psql_error("The server (version %s) does not support full text search.\n",
4339  sverbuf, sizeof(sverbuf)));
4340  return true;
4341  }
4342 
4343  initPQExpBuffer(&buf);
4344 
4345  printfPQExpBuffer(&buf,
4346  "SELECT\n"
4347  " n.nspname as \"%s\",\n"
4348  " d.dictname as \"%s\",\n",
4349  gettext_noop("Schema"),
4350  gettext_noop("Name"));
4351 
4352  if (verbose)
4353  {
4354  appendPQExpBuffer(&buf,
4355  " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM\n"
4356  " pg_catalog.pg_ts_template t\n"
4357  " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace\n"
4358  " WHERE d.dicttemplate = t.oid ) AS \"%s\",\n"
4359  " d.dictinitoption as \"%s\",\n",
4360  gettext_noop("Template"),
4361  gettext_noop("Init options"));
4362  }
4363 
4364  appendPQExpBuffer(&buf,
4365  " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
4366  gettext_noop("Description"));
4367 
4368  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
4369  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
4370 
4371  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4372  "n.nspname", "d.dictname", NULL,
4373  "pg_catalog.pg_ts_dict_is_visible(d.oid)");
4374 
4375  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4376 
4377  res = PSQLexec(buf.data);
4378  termPQExpBuffer(&buf);
4379  if (!res)
4380  return false;
4381 
4382  myopt.nullPrint = NULL;
4383  myopt.title = _("List of text search dictionaries");
4384  myopt.translate_header = true;
4385 
4386  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4387 
4388  PQclear(res);
4389  return true;
4390 }
4391 
4392 
4393 /*
4394  * \dFt
4395  * list text search templates
4396  */
4397 bool
4398 listTSTemplates(const char *pattern, bool verbose)
4399 {
4401  PGresult *res;
4402  printQueryOpt myopt = pset.popt;
4403 
4404  if (pset.sversion < 80300)
4405  {
4406  char sverbuf[32];
4407 
4408  psql_error("The server (version %s) does not support full text search.\n",
4410  sverbuf, sizeof(sverbuf)));
4411  return true;
4412  }
4413 
4414  initPQExpBuffer(&buf);
4415 
4416  if (verbose)
4417  printfPQExpBuffer(&buf,
4418  "SELECT\n"
4419  " n.nspname AS \"%s\",\n"
4420  " t.tmplname AS \"%s\",\n"
4421  " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
4422  " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
4423  " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
4424  gettext_noop("Schema"),
4425  gettext_noop("Name"),
4426  gettext_noop("Init"),
4427  gettext_noop("Lexize"),
4428  gettext_noop("Description"));
4429  else
4430  printfPQExpBuffer(&buf,
4431  "SELECT\n"
4432  " n.nspname AS \"%s\",\n"
4433  " t.tmplname AS \"%s\",\n"
4434  " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
4435  gettext_noop("Schema"),
4436  gettext_noop("Name"),
4437  gettext_noop("Description"));
4438 
4439  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
4440  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
4441 
4442  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4443  "n.nspname", "t.tmplname", NULL,
4444  "pg_catalog.pg_ts_template_is_visible(t.oid)");
4445 
4446  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4447 
4448  res = PSQLexec(buf.data);
4449  termPQExpBuffer(&buf);
4450  if (!res)
4451  return false;
4452 
4453  myopt.nullPrint = NULL;
4454  myopt.title = _("List of text search templates");
4455  myopt.translate_header = true;
4456 
4457  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4458 
4459  PQclear(res);
4460  return true;
4461 }
4462 
4463 
4464 /*
4465  * \dF
4466  * list text search configurations
4467  */
4468 bool
4469 listTSConfigs(const char *pattern, bool verbose)
4470 {
4472  PGresult *res;
4473  printQueryOpt myopt = pset.popt;
4474 
4475  if (pset.sversion < 80300)
4476  {
4477  char sverbuf[32];
4478 
4479  psql_error("The server (version %s) does not support full text search.\n",
4481  sverbuf, sizeof(sverbuf)));
4482  return true;
4483  }
4484 
4485  if (verbose)
4486  return listTSConfigsVerbose(pattern);
4487 
4488  initPQExpBuffer(&buf);
4489 
4490  printfPQExpBuffer(&buf,
4491  "SELECT\n"
4492  " n.nspname as \"%s\",\n"
4493  " c.cfgname as \"%s\",\n"
4494  " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
4495  "FROM pg_catalog.pg_ts_config c\n"
4496  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace\n",
4497  gettext_noop("Schema"),
4498  gettext_noop("Name"),
4499  gettext_noop("Description")
4500  );
4501 
4502  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4503  "n.nspname", "c.cfgname", NULL,
4504  "pg_catalog.pg_ts_config_is_visible(c.oid)");
4505 
4506  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4507 
4508  res = PSQLexec(buf.data);
4509  termPQExpBuffer(&buf);
4510  if (!res)
4511  return false;
4512 
4513  myopt.nullPrint = NULL;
4514  myopt.title = _("List of text search configurations");
4515  myopt.translate_header = true;
4516 
4517  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4518 
4519  PQclear(res);
4520  return true;
4521 }
4522 
4523 static bool
4524 listTSConfigsVerbose(const char *pattern)
4525 {
4527  PGresult *res;
4528  int i;
4529 
4530  initPQExpBuffer(&buf);
4531 
4532  printfPQExpBuffer(&buf,
4533  "SELECT c.oid, c.cfgname,\n"
4534  " n.nspname,\n"
4535  " p.prsname,\n"
4536  " np.nspname as pnspname\n"
4537  "FROM pg_catalog.pg_ts_config c\n"
4538  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace,\n"
4539  " pg_catalog.pg_ts_parser p\n"
4540  " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace\n"
4541  "WHERE p.oid = c.cfgparser\n"
4542  );
4543 
4544  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4545  "n.nspname", "c.cfgname", NULL,
4546  "pg_catalog.pg_ts_config_is_visible(c.oid)");
4547 
4548  appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
4549 
4550  res = PSQLexec(buf.data);
4551  termPQExpBuffer(&buf);
4552  if (!res)
4553  return false;
4554 
4555  if (PQntuples(res) == 0)
4556  {
4557  if (!pset.quiet)
4558  {
4559  if (pattern)
4560  psql_error("Did not find any text search configuration named \"%s\".\n",
4561  pattern);
4562  else
4563  psql_error("Did not find any text search configurations.\n");
4564  }
4565  PQclear(res);
4566  return false;
4567  }
4568 
4569  for (i = 0; i < PQntuples(res); i++)
4570  {
4571  const char *oid;
4572  const char *cfgname;
4573  const char *nspname = NULL;
4574  const char *prsname;
4575  const char *pnspname = NULL;
4576 
4577  oid = PQgetvalue(res, i, 0);
4578  cfgname = PQgetvalue(res, i, 1);
4579  if (!PQgetisnull(res, i, 2))
4580  nspname = PQgetvalue(res, i, 2);
4581  prsname = PQgetvalue(res, i, 3);
4582  if (!PQgetisnull(res, i, 4))
4583  pnspname = PQgetvalue(res, i, 4);
4584 
4585  if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
4586  {
4587  PQclear(res);
4588  return false;
4589  }
4590 
4591  if (cancel_pressed)
4592  {
4593  PQclear(res);
4594  return false;
4595  }
4596  }
4597 
4598  PQclear(res);
4599  return true;
4600 }
4601 
4602 static bool
4603 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
4604  const char *pnspname, const char *prsname)
4605 {
4607  title;
4608  PGresult *res;
4609  printQueryOpt myopt = pset.popt;
4610 
4611  initPQExpBuffer(&buf);
4612 
4613  printfPQExpBuffer(&buf,
4614  "SELECT\n"
4615  " ( SELECT t.alias FROM\n"
4616  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
4617  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
4618  " pg_catalog.btrim(\n"
4619  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
4620  " FROM pg_catalog.pg_ts_config_map AS mm\n"
4621  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
4622  " ORDER BY mapcfg, maptokentype, mapseqno\n"
4623  " ) :: pg_catalog.text,\n"
4624  " '{}') AS \"%s\"\n"
4625  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
4626  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
4627  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
4628  "ORDER BY 1;",
4629  gettext_noop("Token"),
4630  gettext_noop("Dictionaries"),
4631  oid);
4632 
4633  res = PSQLexec(buf.data);
4634  termPQExpBuffer(&buf);
4635  if (!res)
4636  return false;
4637 
4638  initPQExpBuffer(&title);
4639 
4640  if (nspname)
4641  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4642  nspname, cfgname);
4643  else
4644  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4645  cfgname);
4646 
4647  if (pnspname)
4648  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4649  pnspname, prsname);
4650  else
4651  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4652  prsname);
4653 
4654  myopt.nullPrint = NULL;
4655  myopt.title = title.data;
4656  myopt.footers = NULL;
4657  myopt.topt.default_footer = false;
4658  myopt.translate_header = true;
4659 
4660  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4661 
4662  termPQExpBuffer(&title);
4663 
4664  PQclear(res);
4665  return true;
4666 }
4667 
4668 
4669 /*
4670  * \dew
4671  *
4672  * Describes foreign-data wrappers
4673  */
4674 bool
4675 listForeignDataWrappers(const char *pattern, bool verbose)
4676 {
4678  PGresult *res;
4679  printQueryOpt myopt = pset.popt;
4680 
4681  if (pset.sversion < 80400)
4682  {
4683  char sverbuf[32];
4684 
4685  psql_error("The server (version %s) does not support foreign-data wrappers.\n",
4687  sverbuf, sizeof(sverbuf)));
4688  return true;
4689  }
4690 
4691  initPQExpBuffer(&buf);
4692  printfPQExpBuffer(&buf,
4693  "SELECT fdw.fdwname AS \"%s\",\n"
4694  " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
4695  gettext_noop("Name"),
4696  gettext_noop("Owner"));
4697  if (pset.sversion >= 90100)
4698  appendPQExpBuffer(&buf,
4699  " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
4700  gettext_noop("Handler"));
4701  appendPQExpBuffer(&buf,
4702  " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
4703  gettext_noop("Validator"));
4704 
4705  if (verbose)
4706  {
4707  appendPQExpBufferStr(&buf, ",\n ");
4708  printACLColumn(&buf, "fdwacl");
4709  appendPQExpBuffer(&buf,
4710  ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
4711  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4712  " pg_catalog.quote_ident(option_name) || ' ' || "
4713  " pg_catalog.quote_literal(option_value) FROM "
4714  " pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
4715  " END AS \"%s\"",
4716  gettext_noop("FDW options"));
4717 
4718  if (pset.sversion >= 90100)
4719  appendPQExpBuffer(&buf,
4720  ",\n d.description AS \"%s\" ",
4721  gettext_noop("Description"));
4722  }
4723 
4724  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
4725 
4726  if (verbose && pset.sversion >= 90100)
4727  appendPQExpBufferStr(&buf,
4728  "LEFT JOIN pg_catalog.pg_description d\n"
4729  " ON d.classoid = fdw.tableoid "
4730  "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
4731 
4732  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4733  NULL, "fdwname", NULL, NULL);
4734 
4735  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4736 
4737  res = PSQLexec(buf.data);
4738  termPQExpBuffer(&buf);
4739  if (!res)
4740  return false;
4741 
4742  myopt.nullPrint = NULL;
4743  myopt.title = _("List of foreign-data wrappers");
4744  myopt.translate_header = true;
4745 
4746  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4747 
4748  PQclear(res);
4749  return true;
4750 }
4751 
4752 /*
4753  * \des
4754  *
4755  * Describes foreign servers.
4756  */
4757 bool
4758 listForeignServers(const char *pattern, bool verbose)
4759 {
4761  PGresult *res;
4762  printQueryOpt myopt = pset.popt;
4763 
4764  if (pset.sversion < 80400)
4765  {
4766  char sverbuf[32];
4767 
4768  psql_error("The server (version %s) does not support foreign servers.\n",
4770  sverbuf, sizeof(sverbuf)));
4771  return true;
4772  }
4773 
4774  initPQExpBuffer(&buf);
4775  printfPQExpBuffer(&buf,
4776  "SELECT s.srvname AS \"%s\",\n"
4777  " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
4778  " f.fdwname AS \"%s\"",
4779  gettext_noop("Name"),
4780  gettext_noop("Owner"),
4781  gettext_noop("Foreign-data wrapper"));
4782 
4783  if (verbose)
4784  {
4785  appendPQExpBufferStr(&buf, ",\n ");
4786  printACLColumn(&buf, "s.srvacl");
4787  appendPQExpBuffer(&buf,
4788  ",\n"
4789  " s.srvtype AS \"%s\",\n"
4790  " s.srvversion AS \"%s\",\n"
4791  " CASE WHEN srvoptions IS NULL THEN '' ELSE "
4792  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4793  " pg_catalog.quote_ident(option_name) || ' ' || "
4794  " pg_catalog.quote_literal(option_value) FROM "
4795  " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
4796  " END AS \"%s\",\n"
4797  " d.description AS \"%s\"",
4798  gettext_noop("Type"),
4799  gettext_noop("Version"),
4800  gettext_noop("FDW options"),
4801  gettext_noop("Description"));
4802  }
4803 
4804  appendPQExpBufferStr(&buf,
4805  "\nFROM pg_catalog.pg_foreign_server s\n"
4806  " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
4807 
4808  if (verbose)
4809  appendPQExpBufferStr(&buf,
4810  "LEFT JOIN pg_catalog.pg_description d\n "
4811  "ON d.classoid = s.tableoid AND d.objoid = s.oid "
4812  "AND d.objsubid = 0\n");
4813 
4814  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4815  NULL, "s.srvname", NULL, NULL);
4816 
4817  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4818 
4819  res = PSQLexec(buf.data);
4820  termPQExpBuffer(&buf);
4821  if (!res)
4822  return false;
4823 
4824  myopt.nullPrint = NULL;
4825  myopt.title = _("List of foreign servers");
4826  myopt.translate_header = true;
4827 
4828  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4829 
4830  PQclear(res);
4831  return true;
4832 }
4833 
4834 /*
4835  * \deu
4836  *
4837  * Describes user mappings.
4838  */
4839 bool
4840 listUserMappings(const char *pattern, bool verbose)
4841 {
4843  PGresult *res;
4844  printQueryOpt myopt = pset.popt;
4845 
4846  if (pset.sversion < 80400)
4847  {
4848  char sverbuf[32];
4849 
4850  psql_error("The server (version %s) does not support user mappings.\n",
4852  sverbuf, sizeof(sverbuf)));
4853  return true;
4854  }
4855 
4856  initPQExpBuffer(&buf);
4857  printfPQExpBuffer(&buf,
4858  "SELECT um.srvname AS \"%s\",\n"
4859  " um.usename AS \"%s\"",
4860  gettext_noop("Server"),
4861  gettext_noop("User name"));
4862 
4863  if (verbose)
4864  appendPQExpBuffer(&buf,
4865  ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4866  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4867  " pg_catalog.quote_ident(option_name) || ' ' || "
4868  " pg_catalog.quote_literal(option_value) FROM "
4869  " pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
4870  " END AS \"%s\"",
4871  gettext_noop("FDW options"));
4872 
4873  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4874 
4875  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4876  NULL, "um.srvname", "um.usename", NULL);
4877 
4878  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4879 
4880  res = PSQLexec(buf.data);
4881  termPQExpBuffer(&buf);
4882  if (!res)
4883  return false;
4884 
4885  myopt.nullPrint = NULL;
4886  myopt.title = _("List of user mappings");
4887  myopt.translate_header = true;
4888 
4889  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4890 
4891  PQclear(res);
4892  return true;
4893 }
4894 
4895 /*
4896  * \det
4897  *
4898  * Describes foreign tables.
4899  */
4900 bool
4901 listForeignTables(const char *pattern, bool verbose)
4902 {
4904  PGresult *res;
4905  printQueryOpt myopt = pset.popt;
4906 
4907  if (pset.sversion < 90100)
4908  {
4909  char sverbuf[32];
4910 
4911  psql_error("The server (version %s) does not support foreign tables.\n",
4913  sverbuf, sizeof(sverbuf)));
4914  return true;
4915  }
4916 
4917  initPQExpBuffer(&buf);
4918  printfPQExpBuffer(&buf,
4919  "SELECT n.nspname AS \"%s\",\n"
4920  " c.relname AS \"%s\",\n"
4921  " s.srvname AS \"%s\"",
4922  gettext_noop("Schema"),
4923  gettext_noop("Table"),
4924  gettext_noop("Server"));
4925 
4926  if (verbose)
4927  appendPQExpBuffer(&buf,
4928  ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4929  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4930  " pg_catalog.quote_ident(option_name) || ' ' || "
4931  " pg_catalog.quote_literal(option_value) FROM "
4932  " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
4933  " END AS \"%s\",\n"
4934  " d.description AS \"%s\"",
4935  gettext_noop("FDW options"),
4936  gettext_noop("Description"));
4937 
4938  appendPQExpBufferStr(&buf,
4939  "\nFROM pg_catalog.pg_foreign_table ft\n"
4940  " INNER JOIN pg_catalog.pg_class c"
4941  " ON c.oid = ft.ftrelid\n"
4942  " INNER JOIN pg_catalog.pg_namespace n"
4943  " ON n.oid = c.relnamespace\n"
4944  " INNER JOIN pg_catalog.pg_foreign_server s"
4945  " ON s.oid = ft.ftserver\n");
4946  if (verbose)
4947  appendPQExpBufferStr(&buf,
4948  " LEFT JOIN pg_catalog.pg_description d\n"
4949  " ON d.classoid = c.tableoid AND "
4950  "d.objoid = c.oid AND d.objsubid = 0\n");
4951 
4952  processSQLNamePattern(pset.db, &buf, pattern, false, false,
4953  "n.nspname", "c.relname", NULL,
4954  "pg_catalog.pg_table_is_visible(c.oid)");
4955 
4956  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4957 
4958  res = PSQLexec(buf.data);
4959  termPQExpBuffer(&buf);
4960  if (!res)
4961  return false;
4962 
4963  myopt.nullPrint = NULL;
4964  myopt.title = _("List of foreign tables");
4965  myopt.translate_header = true;
4966 
4967  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4968 
4969  PQclear(res);
4970  return true;
4971 }
4972 
4973 /*
4974  * \dx
4975  *
4976  * Briefly describes installed extensions.
4977  */
4978 bool
4979 listExtensions(const char *pattern)
4980 {
4982  PGresult *res;
4983  printQueryOpt myopt = pset.popt;
4984 
4985  if (pset.sversion < 90100)
4986  {
4987  char sverbuf[32];
4988 
4989  psql_error("The server (version %s) does not support extensions.\n",
4991  sverbuf, sizeof(sverbuf)));
4992  return true;
4993  }
4994 
4995  initPQExpBuffer(&buf);
4996  printfPQExpBuffer(&buf,
4997  "SELECT e.extname AS \"%s\", "
4998  "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4999  "FROM pg_catalog.pg_extension e "
5000  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
5001  "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
5002  "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
5003  gettext_noop("Name"),
5004  gettext_noop("Version"),
5005  gettext_noop("Schema"),
5006  gettext_noop("Description"));
5007 
5008  processSQLNamePattern(pset.db, &buf, pattern,
5009  false, false,
5010  NULL, "e.extname", NULL,
5011  NULL);
5012 
5013  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5014 
5015  res = PSQLexec(buf.data);
5016  termPQExpBuffer(&buf);
5017  if (!res)
5018  return false;
5019 
5020  myopt.nullPrint = NULL;
5021  myopt.title = _("List of installed extensions");
5022  myopt.translate_header = true;
5023 
5024  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5025 
5026  PQclear(res);
5027  return true;
5028 }
5029 
5030 /*
5031  * \dx+
5032  *
5033  * List contents of installed extensions.
5034  */
5035 bool
5036 listExtensionContents(const char *pattern)
5037 {
5039  PGresult *res;
5040  int i;
5041 
5042  if (pset.sversion < 90100)
5043  {
5044  char sverbuf[32];
5045 
5046  psql_error("The server (version %s) does not support extensions.\n",
5048  sverbuf, sizeof(sverbuf)));
5049  return true;
5050  }
5051 
5052  initPQExpBuffer(&buf);
5053  printfPQExpBuffer(&buf,
5054  "SELECT e.extname, e.oid\n"
5055  "FROM pg_catalog.pg_extension e\n");
5056 
5057  processSQLNamePattern(pset.db, &buf, pattern,
5058  false, false,
5059  NULL, "e.extname", NULL,
5060  NULL);
5061 
5062  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5063 
5064  res = PSQLexec(buf.data);
5065  termPQExpBuffer(&buf);
5066  if (!res)
5067  return false;
5068 
5069  if (PQntuples(res) == 0)
5070  {
5071  if (!pset.quiet)
5072  {
5073  if (pattern)
5074  psql_error("Did not find any extension named \"%s\".\n",
5075  pattern);
5076  else
5077  psql_error("Did not find any extensions.\n");
5078  }
5079  PQclear(res);
5080  return false;
5081  }
5082 
5083  for (i = 0; i < PQntuples(res); i++)
5084  {
5085  const char *extname;
5086  const char *oid;
5087 
5088  extname = PQgetvalue(res, i, 0);
5089  oid = PQgetvalue(res, i, 1);
5090 
5091  if (!listOneExtensionContents(extname, oid))
5092  {
5093  PQclear(res);
5094  return false;
5095  }
5096  if (cancel_pressed)
5097  {
5098  PQclear(res);
5099  return false;
5100  }
5101  }
5102 
5103  PQclear(res);
5104  return true;
5105 }
5106 
5107 static bool
5108 listOneExtensionContents(const char *extname, const char *oid)
5109 {
5111  PGresult *res;
5112  PQExpBufferData title;
5113  printQueryOpt myopt = pset.popt;
5114 
5115  initPQExpBuffer(&buf);
5116  printfPQExpBuffer(&buf,
5117  "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
5118  "FROM pg_catalog.pg_depend\n"
5119  "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
5120  "ORDER BY 1;",
5121  gettext_noop("Object description"),
5122  oid);
5123 
5124  res = PSQLexec(buf.data);
5125  termPQExpBuffer(&buf);
5126  if (!res)
5127  return false;
5128 
5129  myopt.nullPrint = NULL;
5130  initPQExpBuffer(&title);
5131  printfPQExpBuffer(&title, _("Objects in extension \"%s\""), extname);
5132  myopt.title = title.data;
5133  myopt.translate_header = true;
5134 
5135  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5136 
5137  termPQExpBuffer(&title);
5138  PQclear(res);
5139  return true;
5140 }
5141 
5142 /*
5143  * \dRp
5144  * Lists publications.
5145  *
5146  * Takes an optional regexp to select particular publications
5147  */
5148 bool
5149 listPublications(const char *pattern)
5150 {
5152  PGresult *res;
5153  printQueryOpt myopt = pset.popt;
5154  static const bool translate_columns[] = {false, false, false, false, false, false};
5155 
5156  if (pset.sversion < 100000)
5157  {
5158  char sverbuf[32];
5159 
5160  psql_error("The server (version %s) does not support publications.\n",
5162  sverbuf, sizeof(sverbuf)));
5163  return true;
5164  }
5165 
5166  initPQExpBuffer(&buf);
5167 
5168  printfPQExpBuffer(&buf,
5169  "SELECT pubname AS \"%s\",\n"
5170  " pg_catalog.pg_get_userbyid(pubowner) AS \"%s\",\n"
5171  " puballtables AS \"%s\",\n"
5172  " pubinsert AS \"%s\",\n"
5173  " pubupdate AS \"%s\",\n"
5174  " pubdelete AS \"%s\"\n",
5175  gettext_noop("Name"),
5176  gettext_noop("Owner"),
5177  gettext_noop("All tables"),
5178  gettext_noop("Inserts"),
5179  gettext_noop("Updates"),
5180  gettext_noop("Deletes"));
5181 
5182  appendPQExpBufferStr(&buf,
5183  "\nFROM pg_catalog.pg_publication\n");
5184 
5185  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5186  NULL, "pubname", NULL,
5187  NULL);
5188 
5189  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5190 
5191  res = PSQLexec(buf.data);
5192  termPQExpBuffer(&buf);
5193  if (!res)
5194  return false;
5195 
5196  myopt.nullPrint = NULL;
5197  myopt.title = _("List of publications");
5198  myopt.translate_header = true;
5199  myopt.translate_columns = translate_columns;
5200  myopt.n_translate_columns = lengthof(translate_columns);
5201 
5202  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5203 
5204  PQclear(res);
5205 
5206  return true;
5207 }
5208 
5209 /*
5210  * \dRp+
5211  * Describes publications including the contents.
5212  *
5213  * Takes an optional regexp to select particular publications
5214  */
5215 bool
5216 describePublications(const char *pattern)
5217 {
5219  int i;
5220  PGresult *res;
5221 
5222  if (pset.sversion < 100000)
5223  {
5224  char sverbuf[32];
5225 
5226  psql_error("The server (version %s) does not support publications.\n",
5228  sverbuf, sizeof(sverbuf)));
5229  return true;
5230  }
5231 
5232  initPQExpBuffer(&buf);
5233 
5234  printfPQExpBuffer(&buf,
5235  "SELECT oid, pubname,\n"
5236  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
5237  " puballtables, pubinsert, pubupdate, pubdelete\n"
5238  "FROM pg_catalog.pg_publication\n");
5239 
5240  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5241  NULL, "pubname", NULL,
5242  NULL);
5243 
5244  appendPQExpBufferStr(&buf, "ORDER BY 2;");
5245 
5246  res = PSQLexec(buf.data);
5247  if (!res)
5248  {
5249  termPQExpBuffer(&buf);
5250  return false;
5251  }
5252 
5253  if (PQntuples(res) == 0)
5254  {
5255  if (!pset.quiet)
5256  {
5257  if (pattern)
5258  psql_error("Did not find any publication named \"%s\".\n",
5259  pattern);
5260  else
5261  psql_error("Did not find any publications.\n");
5262  }
5263 
5264  termPQExpBuffer(&buf);
5265  PQclear(res);
5266  return false;
5267  }
5268 
5269  for (i = 0; i < PQntuples(res); i++)
5270  {
5271  const char align = 'l';
5272  int ncols = 5;
5273  int nrows = 1;
5274  int tables = 0;
5275  PGresult *tabres;
5276  char *pubid = PQgetvalue(res, i, 0);
5277  char *pubname = PQgetvalue(res, i, 1);
5278  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
5279  int j;
5280  PQExpBufferData title;
5281  printTableOpt myopt = pset.popt.topt;
5282  printTableContent cont;
5283 
5284  initPQExpBuffer(&title);
5285  printfPQExpBuffer(&title, _("Publication %s"), pubname);
5286  printTableInit(&cont, &myopt, title.data, ncols, nrows);
5287 
5288  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
5289  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
5290  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
5291  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
5292  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
5293 
5294  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
5295  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
5296  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
5297  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
5298  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
5299 
5300  if (!puballtables)
5301  {
5302  printfPQExpBuffer(&buf,
5303  "SELECT n.nspname, c.relname\n"
5304  "FROM pg_catalog.pg_class c,\n"
5305  " pg_catalog.pg_namespace n,\n"
5306  " pg_catalog.pg_publication_rel pr\n"
5307  "WHERE c.relnamespace = n.oid\n"
5308  " AND c.oid = pr.prrelid\n"
5309  " AND pr.prpubid = '%s'\n"
5310  "ORDER BY 1,2", pubid);
5311 
5312  tabres = PSQLexec(buf.data);
5313  if (!tabres)
5314  {
5315  printTableCleanup(&cont);
5316  PQclear(res);
5317  termPQExpBuffer(&buf);
5318  termPQExpBuffer(&title);
5319  return false;
5320  }
5321  else
5322  tables = PQntuples(tabres);
5323 
5324  if (tables > 0)
5325  printTableAddFooter(&cont, _("Tables:"));
5326 
5327  for (j = 0; j < tables; j++)
5328  {
5329  printfPQExpBuffer(&buf, " \"%s.%s\"",
5330  PQgetvalue(tabres, j, 0),
5331  PQgetvalue(tabres, j, 1));
5332 
5333  printTableAddFooter(&cont, buf.data);
5334  }
5335  PQclear(tabres);
5336  }
5337 
5338  printTable(&cont, pset.queryFout, false, pset.logfile);
5339  printTableCleanup(&cont);
5340 
5341  termPQExpBuffer(&title);
5342  }
5343 
5344  termPQExpBuffer(&buf);
5345  PQclear(res);
5346 
5347  return true;
5348 }
5349 
5350 /*
5351  * \dRs
5352  * Describes subscriptions.
5353  *
5354  * Takes an optional regexp to select particular subscriptions
5355  */
5356 bool
5357 describeSubscriptions(const char *pattern, bool verbose)
5358 {
5360  PGresult *res;
5361  printQueryOpt myopt = pset.popt;
5362  static const bool translate_columns[] = {false, false, false, false,
5363  false, false};
5364 
5365  if (pset.sversion < 100000)
5366  {
5367  char sverbuf[32];
5368 
5369  psql_error("The server (version %s) does not support subscriptions.\n",
5371  sverbuf, sizeof(sverbuf)));
5372  return true;
5373  }
5374 
5375  initPQExpBuffer(&buf);
5376 
5377  printfPQExpBuffer(&buf,
5378  "SELECT subname AS \"%s\"\n"
5379  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
5380  ", subenabled AS \"%s\"\n"
5381  ", subpublications AS \"%s\"\n",
5382  gettext_noop("Name"),
5383  gettext_noop("Owner"),
5384  gettext_noop("Enabled"),
5385  gettext_noop("Publication"));
5386 
5387  if (verbose)
5388  {
5389  appendPQExpBuffer(&buf,
5390  ", subsynccommit AS \"%s\"\n"
5391  ", subconninfo AS \"%s\"\n",
5392  gettext_noop("Synchronous commit"),
5393  gettext_noop("Conninfo"));
5394  }
5395 
5396  /* Only display subscriptions in current database. */
5397  appendPQExpBufferStr(&buf,
5398  "FROM pg_catalog.pg_subscription\n"
5399  "WHERE subdbid = (SELECT oid\n"
5400  " FROM pg_catalog.pg_database\n"
5401  " WHERE datname = pg_catalog.current_database())");
5402 
5403  processSQLNamePattern(pset.db, &buf, pattern, true, false,
5404  NULL, "subname", NULL,
5405  NULL);
5406 
5407  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5408 
5409  res = PSQLexec(buf.data);
5410  termPQExpBuffer(&buf);
5411  if (!res)
5412  return false;
5413 
5414  myopt.nullPrint = NULL;
5415  myopt.title = _("List of subscriptions");
5416  myopt.translate_header = true;
5417  myopt.translate_columns = translate_columns;
5418  myopt.n_translate_columns = lengthof(translate_columns);
5419 
5420  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5421 
5422  PQclear(res);
5423  return true;
5424 }
5425 
5426 /*
5427  * printACLColumn
5428  *
5429  * Helper function for consistently formatting ACL (privilege) columns.
5430  * The proper targetlist entry is appended to buf. Note lack of any
5431  * whitespace or comma decoration.
5432  */
5433 static void
5434 printACLColumn(PQExpBuffer buf, const char *colname)
5435 {
5436  if (pset.sversion >= 80100)
5437  appendPQExpBuffer(buf,
5438  "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
5439  colname, gettext_noop("Access privileges"));
5440  else
5441  appendPQExpBuffer(buf,
5442  "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
5443  colname, gettext_noop("Access privileges"));
5444 }
signed short int16
Definition: c.h:301
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3083
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
static bool listTSParsersVerbose(const char *pattern)
Definition: describe.c:4138
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static bool describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname, const char *pnspname, const char *prsname)
Definition: describe.c:4603
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void printTableCleanup(printTableContent *const content)
Definition: print.c:3101
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
bool listTSConfigs(const char *pattern, bool verbose)
Definition: describe.c:4469
bool objectDescription(const char *pattern, bool showSystem)
Definition: describe.c:1102
bool listExtensionContents(const char *pattern)
Definition: describe.c:5036
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * psprintf(const char *fmt,...)
Definition: