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