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