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