2 * psql - the PostgreSQL interactive terminal
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.
9 * Copyright (c) 2000-2017, PostgreSQL Global Development Group
11 * src/bin/psql/describe.c
13 #include "postgres_fe.h"
17 #include "catalog/pg_default_acl.h"
18 #include "fe_utils/string_utils.h"
22 #include "fe_utils/mbprint.h"
23 #include "fe_utils/print.h"
25 #include "variables.h"
28 static bool describeOneTableDetails(const char *schemaname,
29 const char *relationname,
32 static void add_tablespace_footer(printTableContent *const cont, char relkind,
33 Oid tablespace, const bool newline);
34 static void add_role_attribute(PQExpBuffer buf, const char *const str);
35 static bool listTSParsersVerbose(const char *pattern);
36 static bool describeOneTSParser(const char *oid, const char *nspname,
38 static bool listTSConfigsVerbose(const char *pattern);
39 static bool describeOneTSConfig(const char *oid, const char *nspname,
41 const char *pnspname, const char *prsname);
42 static void printACLColumn(PQExpBuffer buf, const char *colname);
43 static bool listOneExtensionContents(const char *extname, const char *oid);
47 * Handlers for various slash commands displaying some sort of list
48 * of things in the database.
50 * Note: try to format the queries to look nice in -E output.
56 * Takes an optional regexp to select particular aggregates
59 describeAggregates(const char *pattern, bool verbose, bool showSystem)
63 printQueryOpt myopt = pset.popt;
65 initPQExpBuffer(&buf);
67 printfPQExpBuffer(&buf,
68 "SELECT n.nspname as \"%s\",\n"
69 " p.proname AS \"%s\",\n"
70 " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
71 gettext_noop("Schema"),
73 gettext_noop("Result data type"));
75 if (pset.sversion >= 80400)
76 appendPQExpBuffer(&buf,
77 " CASE WHEN p.pronargs = 0\n"
78 " THEN CAST('*' AS pg_catalog.text)\n"
79 " ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"
81 gettext_noop("Argument data types"));
82 else if (pset.sversion >= 80200)
83 appendPQExpBuffer(&buf,
84 " CASE WHEN p.pronargs = 0\n"
85 " THEN CAST('*' AS pg_catalog.text)\n"
87 " pg_catalog.array_to_string(ARRAY(\n"
89 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
91 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
94 gettext_noop("Argument data types"));
96 appendPQExpBuffer(&buf,
97 " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
98 gettext_noop("Argument data types"));
100 appendPQExpBuffer(&buf,
101 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
102 "FROM pg_catalog.pg_proc p\n"
103 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
104 "WHERE p.proisagg\n",
105 gettext_noop("Description"));
107 if (!showSystem && !pattern)
108 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
109 " AND n.nspname <> 'information_schema'\n");
111 processSQLNamePattern(pset.db, &buf, pattern, true, false,
112 "n.nspname", "p.proname", NULL,
113 "pg_catalog.pg_function_is_visible(p.oid)");
115 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
117 res = PSQLexec(buf.data);
118 termPQExpBuffer(&buf);
122 myopt.nullPrint = NULL;
123 myopt.title = _("List of aggregate functions");
124 myopt.translate_header = true;
126 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
133 * Takes an optional regexp to select particular access methods
136 describeAccessMethods(const char *pattern, bool verbose)
140 printQueryOpt myopt = pset.popt;
141 static const bool translate_columns[] = {false, true, false, false};
143 if (pset.sversion < 90600)
147 psql_error("The server (version %s) does not support access methods.\n",
148 formatPGVersionNumber(pset.sversion, false,
149 sverbuf, sizeof(sverbuf)));
153 initPQExpBuffer(&buf);
155 printfPQExpBuffer(&buf,
156 "SELECT amname AS \"%s\",\n"
158 " WHEN 'i' THEN '%s'"
160 gettext_noop("Name"),
161 gettext_noop("Index"),
162 gettext_noop("Type"));
166 appendPQExpBuffer(&buf,
167 ",\n amhandler AS \"%s\",\n"
168 " pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
169 gettext_noop("Handler"),
170 gettext_noop("Description"));
173 appendPQExpBufferStr(&buf,
174 "\nFROM pg_catalog.pg_am\n");
176 processSQLNamePattern(pset.db, &buf, pattern, false, false,
177 NULL, "amname", NULL,
180 appendPQExpBufferStr(&buf, "ORDER BY 1;");
182 res = PSQLexec(buf.data);
183 termPQExpBuffer(&buf);
187 myopt.nullPrint = NULL;
188 myopt.title = _("List of access methods");
189 myopt.translate_header = true;
190 myopt.translate_columns = translate_columns;
191 myopt.n_translate_columns = lengthof(translate_columns);
193 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
200 * Takes an optional regexp to select particular tablespaces
203 describeTablespaces(const char *pattern, bool verbose)
207 printQueryOpt myopt = pset.popt;
209 if (pset.sversion < 80000)
213 psql_error("The server (version %s) does not support tablespaces.\n",
214 formatPGVersionNumber(pset.sversion, false,
215 sverbuf, sizeof(sverbuf)));
219 initPQExpBuffer(&buf);
221 if (pset.sversion >= 90200)
222 printfPQExpBuffer(&buf,
223 "SELECT spcname AS \"%s\",\n"
224 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
225 " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
226 gettext_noop("Name"),
227 gettext_noop("Owner"),
228 gettext_noop("Location"));
230 printfPQExpBuffer(&buf,
231 "SELECT spcname AS \"%s\",\n"
232 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
233 " spclocation AS \"%s\"",
234 gettext_noop("Name"),
235 gettext_noop("Owner"),
236 gettext_noop("Location"));
240 appendPQExpBufferStr(&buf, ",\n ");
241 printACLColumn(&buf, "spcacl");
244 if (verbose && pset.sversion >= 90000)
245 appendPQExpBuffer(&buf,
246 ",\n spcoptions AS \"%s\"",
247 gettext_noop("Options"));
249 if (verbose && pset.sversion >= 90200)
250 appendPQExpBuffer(&buf,
251 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\"",
252 gettext_noop("Size"));
254 if (verbose && pset.sversion >= 80200)
255 appendPQExpBuffer(&buf,
256 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
257 gettext_noop("Description"));
259 appendPQExpBufferStr(&buf,
260 "\nFROM pg_catalog.pg_tablespace\n");
262 processSQLNamePattern(pset.db, &buf, pattern, false, false,
263 NULL, "spcname", NULL,
266 appendPQExpBufferStr(&buf, "ORDER BY 1;");
268 res = PSQLexec(buf.data);
269 termPQExpBuffer(&buf);
273 myopt.nullPrint = NULL;
274 myopt.title = _("List of tablespaces");
275 myopt.translate_header = true;
277 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
285 * Takes an optional regexp to select particular functions.
287 * As with \d, you can specify the kinds of functions you want:
294 * and you can mix and match these in any order.
297 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
299 bool showAggregate = strchr(functypes, 'a') != NULL;
300 bool showNormal = strchr(functypes, 'n') != NULL;
301 bool showTrigger = strchr(functypes, 't') != NULL;
302 bool showWindow = strchr(functypes, 'w') != NULL;
306 printQueryOpt myopt = pset.popt;
307 static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, false, false, false, false};
309 /* No "Parallel" column before 9.6 */
310 static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, false, false, false, false};
312 if (strlen(functypes) != strspn(functypes, "antwS+"))
314 psql_error("\\df only takes [antwS+] as options\n");
318 if (showWindow && pset.sversion < 80400)
322 psql_error("\\df does not take a \"w\" option with server version %s\n",
323 formatPGVersionNumber(pset.sversion, false,
324 sverbuf, sizeof(sverbuf)));
328 if (!showAggregate && !showNormal && !showTrigger && !showWindow)
330 showAggregate = showNormal = showTrigger = true;
331 if (pset.sversion >= 80400)
335 initPQExpBuffer(&buf);
337 printfPQExpBuffer(&buf,
338 "SELECT n.nspname as \"%s\",\n"
339 " p.proname as \"%s\",\n",
340 gettext_noop("Schema"),
341 gettext_noop("Name"));
343 if (pset.sversion >= 80400)
344 appendPQExpBuffer(&buf,
345 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
346 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
348 " WHEN p.proisagg THEN '%s'\n"
349 " WHEN p.proiswindow THEN '%s'\n"
350 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
353 gettext_noop("Result data type"),
354 gettext_noop("Argument data types"),
355 /* translator: "agg" is short for "aggregate" */
357 gettext_noop("window"),
358 gettext_noop("trigger"),
359 gettext_noop("normal"),
360 gettext_noop("Type"));
361 else if (pset.sversion >= 80100)
362 appendPQExpBuffer(&buf,
363 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
364 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
365 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
366 " pg_catalog.array_to_string(ARRAY(\n"
369 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
370 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
371 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
372 " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
375 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
376 " ELSE p.proargnames[s.i] || ' ' \n"
378 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
380 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
383 " pg_catalog.array_to_string(ARRAY(\n"
386 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
387 " ELSE p.proargnames[s.i+1] || ' '\n"
389 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
391 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
395 " WHEN p.proisagg THEN '%s'\n"
396 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
399 gettext_noop("Result data type"),
400 gettext_noop("Argument data types"),
401 /* translator: "agg" is short for "aggregate" */
403 gettext_noop("trigger"),
404 gettext_noop("normal"),
405 gettext_noop("Type"));
407 appendPQExpBuffer(&buf,
408 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
409 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
410 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
412 " WHEN p.proisagg THEN '%s'\n"
413 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
416 gettext_noop("Result data type"),
417 gettext_noop("Argument data types"),
418 /* translator: "agg" is short for "aggregate" */
420 gettext_noop("trigger"),
421 gettext_noop("normal"),
422 gettext_noop("Type"));
426 appendPQExpBuffer(&buf,
428 " WHEN p.provolatile = 'i' THEN '%s'\n"
429 " WHEN p.provolatile = 's' THEN '%s'\n"
430 " WHEN p.provolatile = 'v' THEN '%s'\n"
432 gettext_noop("immutable"),
433 gettext_noop("stable"),
434 gettext_noop("volatile"),
435 gettext_noop("Volatility"));
436 if (pset.sversion >= 90600)
437 appendPQExpBuffer(&buf,
439 " WHEN p.proparallel = 'r' THEN '%s'\n"
440 " WHEN p.proparallel = 's' THEN '%s'\n"
441 " WHEN p.proparallel = 'u' THEN '%s'\n"
443 gettext_noop("restricted"),
444 gettext_noop("safe"),
445 gettext_noop("unsafe"),
446 gettext_noop("Parallel"));
447 appendPQExpBuffer(&buf,
448 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
449 ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\"",
450 gettext_noop("Owner"),
451 gettext_noop("definer"),
452 gettext_noop("invoker"),
453 gettext_noop("Security"));
454 appendPQExpBufferStr(&buf, ",\n ");
455 printACLColumn(&buf, "p.proacl");
456 appendPQExpBuffer(&buf,
457 ",\n l.lanname as \"%s\""
458 ",\n p.prosrc as \"%s\""
459 ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
460 gettext_noop("Language"),
461 gettext_noop("Source code"),
462 gettext_noop("Description"));
465 appendPQExpBufferStr(&buf,
466 "\nFROM pg_catalog.pg_proc p"
467 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
470 appendPQExpBufferStr(&buf,
471 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
475 /* filter by function type, if requested */
476 if (showNormal && showAggregate && showTrigger && showWindow)
483 appendPQExpBufferStr(&buf, " AND ");
486 appendPQExpBufferStr(&buf, "WHERE ");
489 appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
494 appendPQExpBufferStr(&buf, " AND ");
497 appendPQExpBufferStr(&buf, "WHERE ");
500 appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
502 if (!showWindow && pset.sversion >= 80400)
505 appendPQExpBufferStr(&buf, " AND ");
508 appendPQExpBufferStr(&buf, "WHERE ");
511 appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
516 bool needs_or = false;
518 appendPQExpBufferStr(&buf, "WHERE (\n ");
520 /* Note: at least one of these must be true ... */
523 appendPQExpBufferStr(&buf, "p.proisagg\n");
529 appendPQExpBufferStr(&buf, " OR ");
530 appendPQExpBufferStr(&buf,
531 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
537 appendPQExpBufferStr(&buf, " OR ");
538 appendPQExpBufferStr(&buf, "p.proiswindow\n");
541 appendPQExpBufferStr(&buf, " )\n");
544 processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
545 "n.nspname", "p.proname", NULL,
546 "pg_catalog.pg_function_is_visible(p.oid)");
548 if (!showSystem && !pattern)
549 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
550 " AND n.nspname <> 'information_schema'\n");
552 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
554 res = PSQLexec(buf.data);
555 termPQExpBuffer(&buf);
559 myopt.nullPrint = NULL;
560 myopt.title = _("List of functions");
561 myopt.translate_header = true;
562 if (pset.sversion >= 90600)
564 myopt.translate_columns = translate_columns;
565 myopt.n_translate_columns = lengthof(translate_columns);
569 myopt.translate_columns = translate_columns_pre_96;
570 myopt.n_translate_columns = lengthof(translate_columns_pre_96);
573 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
586 describeTypes(const char *pattern, bool verbose, bool showSystem)
590 printQueryOpt myopt = pset.popt;
592 initPQExpBuffer(&buf);
594 printfPQExpBuffer(&buf,
595 "SELECT n.nspname as \"%s\",\n"
596 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
597 gettext_noop("Schema"),
598 gettext_noop("Name"));
600 appendPQExpBuffer(&buf,
601 " t.typname AS \"%s\",\n"
602 " CASE WHEN t.typrelid != 0\n"
603 " THEN CAST('tuple' AS pg_catalog.text)\n"
604 " WHEN t.typlen < 0\n"
605 " THEN CAST('var' AS pg_catalog.text)\n"
606 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
608 gettext_noop("Internal name"),
609 gettext_noop("Size"));
610 if (verbose && pset.sversion >= 80300)
612 appendPQExpBufferStr(&buf,
613 " pg_catalog.array_to_string(\n"
615 " SELECT e.enumlabel\n"
616 " FROM pg_catalog.pg_enum e\n"
617 " WHERE e.enumtypid = t.oid\n");
619 if (pset.sversion >= 90100)
620 appendPQExpBufferStr(&buf,
621 " ORDER BY e.enumsortorder\n");
623 appendPQExpBufferStr(&buf,
624 " ORDER BY e.oid\n");
626 appendPQExpBuffer(&buf,
630 gettext_noop("Elements"));
634 appendPQExpBuffer(&buf,
635 " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
636 gettext_noop("Owner"));
638 if (verbose && pset.sversion >= 90200)
640 printACLColumn(&buf, "t.typacl");
641 appendPQExpBufferStr(&buf, ",\n ");
644 appendPQExpBuffer(&buf,
645 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
646 gettext_noop("Description"));
648 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
649 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
652 * do not include complex types (typrelid!=0) unless they are standalone
655 appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
656 appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
657 "WHERE c.oid = t.typrelid))\n");
660 * do not include array types (before 8.3 we have to use the assumption
661 * that their names start with underscore)
663 if (pset.sversion >= 80300)
664 appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
666 appendPQExpBufferStr(&buf, " AND t.typname !~ '^_'\n");
668 if (!showSystem && !pattern)
669 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
670 " AND n.nspname <> 'information_schema'\n");
672 /* Match name pattern against either internal or external name */
673 processSQLNamePattern(pset.db, &buf, pattern, true, false,
674 "n.nspname", "t.typname",
675 "pg_catalog.format_type(t.oid, NULL)",
676 "pg_catalog.pg_type_is_visible(t.oid)");
678 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
680 res = PSQLexec(buf.data);
681 termPQExpBuffer(&buf);
685 myopt.nullPrint = NULL;
686 myopt.title = _("List of data types");
687 myopt.translate_header = true;
689 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
700 describeOperators(const char *pattern, bool verbose, bool showSystem)
704 printQueryOpt myopt = pset.popt;
706 initPQExpBuffer(&buf);
709 * Note: before Postgres 9.1, we did not assign comments to any built-in
710 * operators, preferring to let the comment on the underlying function
711 * suffice. The coalesce() on the obj_description() calls below supports
712 * this convention by providing a fallback lookup of a comment on the
713 * operator's function. As of 9.1 there is a policy that every built-in
714 * operator should have a comment; so the coalesce() is no longer
715 * necessary so far as built-in operators are concerned. We keep it
716 * anyway, for now, because (1) third-party modules may still be following
717 * the old convention, and (2) we'd need to do it anyway when talking to a
721 printfPQExpBuffer(&buf,
722 "SELECT n.nspname as \"%s\",\n"
723 " o.oprname AS \"%s\",\n"
724 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
725 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
726 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
727 gettext_noop("Schema"),
728 gettext_noop("Name"),
729 gettext_noop("Left arg type"),
730 gettext_noop("Right arg type"),
731 gettext_noop("Result type"));
734 appendPQExpBuffer(&buf,
735 " o.oprcode AS \"%s\",\n",
736 gettext_noop("Function"));
738 appendPQExpBuffer(&buf,
739 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
740 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
741 "FROM pg_catalog.pg_operator o\n"
742 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
743 gettext_noop("Description"));
745 if (!showSystem && !pattern)
746 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
747 " AND n.nspname <> 'information_schema'\n");
749 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
750 "n.nspname", "o.oprname", NULL,
751 "pg_catalog.pg_operator_is_visible(o.oid)");
753 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
755 res = PSQLexec(buf.data);
756 termPQExpBuffer(&buf);
760 myopt.nullPrint = NULL;
761 myopt.title = _("List of operators");
762 myopt.translate_header = true;
764 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
774 * for \l, \list, and -l switch
777 listAllDbs(const char *pattern, bool verbose)
781 printQueryOpt myopt = pset.popt;
783 initPQExpBuffer(&buf);
785 printfPQExpBuffer(&buf,
786 "SELECT d.datname as \"%s\",\n"
787 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
788 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
789 gettext_noop("Name"),
790 gettext_noop("Owner"),
791 gettext_noop("Encoding"));
792 if (pset.sversion >= 80400)
793 appendPQExpBuffer(&buf,
794 " d.datcollate as \"%s\",\n"
795 " d.datctype as \"%s\",\n",
796 gettext_noop("Collate"),
797 gettext_noop("Ctype"));
798 appendPQExpBufferStr(&buf, " ");
799 printACLColumn(&buf, "d.datacl");
800 if (verbose && pset.sversion >= 80200)
801 appendPQExpBuffer(&buf,
802 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
803 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
804 " ELSE 'No Access'\n"
806 gettext_noop("Size"));
807 if (verbose && pset.sversion >= 80000)
808 appendPQExpBuffer(&buf,
809 ",\n t.spcname as \"%s\"",
810 gettext_noop("Tablespace"));
811 if (verbose && pset.sversion >= 80200)
812 appendPQExpBuffer(&buf,
813 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
814 gettext_noop("Description"));
815 appendPQExpBufferStr(&buf,
816 "\nFROM pg_catalog.pg_database d\n");
817 if (verbose && pset.sversion >= 80000)
818 appendPQExpBufferStr(&buf,
819 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
822 processSQLNamePattern(pset.db, &buf, pattern, false, false,
823 NULL, "d.datname", NULL, NULL);
825 appendPQExpBufferStr(&buf, "ORDER BY 1;");
826 res = PSQLexec(buf.data);
827 termPQExpBuffer(&buf);
831 myopt.nullPrint = NULL;
832 myopt.title = _("List of databases");
833 myopt.translate_header = true;
835 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
843 * List Tables' Grant/Revoke Permissions
844 * \z (now also \dp -- perhaps more mnemonic)
847 permissionsList(const char *pattern)
851 printQueryOpt myopt = pset.popt;
852 static const bool translate_columns[] = {false, false, true, false, false, false};
854 initPQExpBuffer(&buf);
857 * we ignore indexes and toast tables since they have no meaningful rights
859 printfPQExpBuffer(&buf,
860 "SELECT n.nspname as \"%s\",\n"
861 " c.relname as \"%s\",\n"
863 " WHEN 'r' THEN '%s'"
864 " WHEN 'v' THEN '%s'"
865 " WHEN 'm' THEN '%s'"
866 " WHEN 'S' THEN '%s'"
867 " WHEN 'f' THEN '%s'"
868 " WHEN 'P' THEN '%s'"
871 gettext_noop("Schema"),
872 gettext_noop("Name"),
873 gettext_noop("table"),
874 gettext_noop("view"),
875 gettext_noop("materialized view"),
876 gettext_noop("sequence"),
877 gettext_noop("foreign table"),
878 gettext_noop("table"), /* partitioned table */
879 gettext_noop("Type"));
881 printACLColumn(&buf, "c.relacl");
883 if (pset.sversion >= 80400)
884 appendPQExpBuffer(&buf,
885 ",\n pg_catalog.array_to_string(ARRAY(\n"
886 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
887 " FROM pg_catalog.pg_attribute a\n"
888 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
889 " ), E'\\n') AS \"%s\"",
890 gettext_noop("Column privileges"));
892 if (pset.sversion >= 90500 && pset.sversion < 100000)
893 appendPQExpBuffer(&buf,
894 ",\n pg_catalog.array_to_string(ARRAY(\n"
896 " || CASE WHEN polcmd != '*' THEN\n"
897 " E' (' || polcmd || E'):'\n"
900 " || CASE WHEN polqual IS NOT NULL THEN\n"
901 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
904 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
905 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
908 " || CASE WHEN polroles <> '{0}' THEN\n"
909 " E'\\n to: ' || pg_catalog.array_to_string(\n"
912 " FROM pg_catalog.pg_roles\n"
913 " WHERE oid = ANY (polroles)\n"
918 " FROM pg_catalog.pg_policy pol\n"
919 " WHERE polrelid = c.oid), E'\\n')\n"
921 gettext_noop("Policies"));
923 if (pset.sversion >= 100000)
924 appendPQExpBuffer(&buf,
925 ",\n pg_catalog.array_to_string(ARRAY(\n"
927 " || CASE WHEN NOT polpermissive THEN\n"
928 " E' (RESTRICTIVE)'\n"
930 " || CASE WHEN polcmd != '*' THEN\n"
931 " E' (' || polcmd || E'):'\n"
934 " || CASE WHEN polqual IS NOT NULL THEN\n"
935 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
938 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
939 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
942 " || CASE WHEN polroles <> '{0}' THEN\n"
943 " E'\\n to: ' || pg_catalog.array_to_string(\n"
946 " FROM pg_catalog.pg_roles\n"
947 " WHERE oid = ANY (polroles)\n"
952 " FROM pg_catalog.pg_policy pol\n"
953 " WHERE polrelid = c.oid), E'\\n')\n"
955 gettext_noop("Policies"));
957 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
958 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
959 "WHERE c.relkind IN ('r', 'v', 'm', 'S', 'f', 'P')\n");
962 * Unless a schema pattern is specified, we suppress system and temp
963 * tables, since they normally aren't very interesting from a permissions
964 * point of view. You can see 'em by explicit request though, eg with \z
967 processSQLNamePattern(pset.db, &buf, pattern, true, false,
968 "n.nspname", "c.relname", NULL,
969 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
971 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
973 res = PSQLexec(buf.data);
976 termPQExpBuffer(&buf);
980 myopt.nullPrint = NULL;
981 printfPQExpBuffer(&buf, _("Access privileges"));
982 myopt.title = buf.data;
983 myopt.translate_header = true;
984 myopt.translate_columns = translate_columns;
985 myopt.n_translate_columns = lengthof(translate_columns);
987 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
989 termPQExpBuffer(&buf);
998 * List Default ACLs. The pattern can match either schema or role name.
1001 listDefaultACLs(const char *pattern)
1003 PQExpBufferData buf;
1005 printQueryOpt myopt = pset.popt;
1006 static const bool translate_columns[] = {false, false, true, false};
1008 if (pset.sversion < 90000)
1012 psql_error("The server (version %s) does not support altering default privileges.\n",
1013 formatPGVersionNumber(pset.sversion, false,
1014 sverbuf, sizeof(sverbuf)));
1018 initPQExpBuffer(&buf);
1020 printfPQExpBuffer(&buf,
1021 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1022 " n.nspname AS \"%s\",\n"
1023 " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
1025 gettext_noop("Owner"),
1026 gettext_noop("Schema"),
1028 gettext_noop("table"),
1030 gettext_noop("sequence"),
1032 gettext_noop("function"),
1034 gettext_noop("type"),
1035 gettext_noop("Type"));
1037 printACLColumn(&buf, "d.defaclacl");
1039 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1040 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1042 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1045 "pg_catalog.pg_get_userbyid(d.defaclrole)",
1048 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1050 res = PSQLexec(buf.data);
1053 termPQExpBuffer(&buf);
1057 myopt.nullPrint = NULL;
1058 printfPQExpBuffer(&buf, _("Default access privileges"));
1059 myopt.title = buf.data;
1060 myopt.translate_header = true;
1061 myopt.translate_columns = translate_columns;
1062 myopt.n_translate_columns = lengthof(translate_columns);
1064 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1066 termPQExpBuffer(&buf);
1073 * Get object comments
1077 * Note: This command only lists comments for object types which do not have
1078 * their comments displayed by their own backslash commands. The following
1079 * types of objects will be displayed: constraint, operator class,
1080 * operator family, rule, and trigger.
1084 objectDescription(const char *pattern, bool showSystem)
1086 PQExpBufferData buf;
1088 printQueryOpt myopt = pset.popt;
1089 static const bool translate_columns[] = {false, false, true, false};
1091 initPQExpBuffer(&buf);
1093 appendPQExpBuffer(&buf,
1094 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
1096 gettext_noop("Schema"),
1097 gettext_noop("Name"),
1098 gettext_noop("Object"),
1099 gettext_noop("Description"));
1101 /* Table constraint descriptions */
1102 appendPQExpBuffer(&buf,
1103 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1104 " n.nspname as nspname,\n"
1105 " CAST(pgc.conname AS pg_catalog.text) as name,"
1106 " CAST('%s' AS pg_catalog.text) as object\n"
1107 " FROM pg_catalog.pg_constraint pgc\n"
1108 " JOIN pg_catalog.pg_class c "
1109 "ON c.oid = pgc.conrelid\n"
1110 " LEFT JOIN pg_catalog.pg_namespace n "
1111 " ON n.oid = c.relnamespace\n",
1112 gettext_noop("table constraint"));
1114 if (!showSystem && !pattern)
1115 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1116 " AND n.nspname <> 'information_schema'\n");
1118 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
1119 false, "n.nspname", "pgc.conname", NULL,
1120 "pg_catalog.pg_table_is_visible(c.oid)");
1122 /* Domain constraint descriptions */
1123 appendPQExpBuffer(&buf,
1125 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1126 " n.nspname as nspname,\n"
1127 " CAST(pgc.conname AS pg_catalog.text) as name,"
1128 " CAST('%s' AS pg_catalog.text) as object\n"
1129 " FROM pg_catalog.pg_constraint pgc\n"
1130 " JOIN pg_catalog.pg_type t "
1131 "ON t.oid = pgc.contypid\n"
1132 " LEFT JOIN pg_catalog.pg_namespace n "
1133 " ON n.oid = t.typnamespace\n",
1134 gettext_noop("domain constraint"));
1136 if (!showSystem && !pattern)
1137 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1138 " AND n.nspname <> 'information_schema'\n");
1140 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
1141 false, "n.nspname", "pgc.conname", NULL,
1142 "pg_catalog.pg_type_is_visible(t.oid)");
1146 * pg_opclass.opcmethod only available in 8.3+
1148 if (pset.sversion >= 80300)
1150 /* Operator class descriptions */
1151 appendPQExpBuffer(&buf,
1153 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
1154 " n.nspname as nspname,\n"
1155 " CAST(o.opcname AS pg_catalog.text) as name,\n"
1156 " CAST('%s' AS pg_catalog.text) as object\n"
1157 " FROM pg_catalog.pg_opclass o\n"
1158 " JOIN pg_catalog.pg_am am ON "
1159 "o.opcmethod = am.oid\n"
1160 " JOIN pg_catalog.pg_namespace n ON "
1161 "n.oid = o.opcnamespace\n",
1162 gettext_noop("operator class"));
1164 if (!showSystem && !pattern)
1165 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1166 " AND n.nspname <> 'information_schema'\n");
1168 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1169 "n.nspname", "o.opcname", NULL,
1170 "pg_catalog.pg_opclass_is_visible(o.oid)");
1174 * although operator family comments have been around since 8.3,
1175 * pg_opfamily_is_visible is only available in 9.2+
1177 if (pset.sversion >= 90200)
1179 /* Operator family descriptions */
1180 appendPQExpBuffer(&buf,
1182 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1183 " n.nspname as nspname,\n"
1184 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1185 " CAST('%s' AS pg_catalog.text) as object\n"
1186 " FROM pg_catalog.pg_opfamily opf\n"
1187 " JOIN pg_catalog.pg_am am "
1188 "ON opf.opfmethod = am.oid\n"
1189 " JOIN pg_catalog.pg_namespace n "
1190 "ON opf.opfnamespace = n.oid\n",
1191 gettext_noop("operator family"));
1193 if (!showSystem && !pattern)
1194 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1195 " AND n.nspname <> 'information_schema'\n");
1197 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1198 "n.nspname", "opf.opfname", NULL,
1199 "pg_catalog.pg_opfamily_is_visible(opf.oid)");
1202 /* Rule descriptions (ignore rules for views) */
1203 appendPQExpBuffer(&buf,
1205 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1206 " n.nspname as nspname,\n"
1207 " CAST(r.rulename AS pg_catalog.text) as name,"
1208 " CAST('%s' AS pg_catalog.text) as object\n"
1209 " FROM pg_catalog.pg_rewrite r\n"
1210 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1211 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1212 " WHERE r.rulename != '_RETURN'\n",
1213 gettext_noop("rule"));
1215 if (!showSystem && !pattern)
1216 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1217 " AND n.nspname <> 'information_schema'\n");
1219 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1220 "n.nspname", "r.rulename", NULL,
1221 "pg_catalog.pg_table_is_visible(c.oid)");
1223 /* Trigger descriptions */
1224 appendPQExpBuffer(&buf,
1226 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1227 " n.nspname as nspname,\n"
1228 " CAST(t.tgname AS pg_catalog.text) as name,"
1229 " CAST('%s' AS pg_catalog.text) as object\n"
1230 " FROM pg_catalog.pg_trigger t\n"
1231 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1232 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1233 gettext_noop("trigger"));
1235 if (!showSystem && !pattern)
1236 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1237 " AND n.nspname <> 'information_schema'\n");
1239 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1240 "n.nspname", "t.tgname", NULL,
1241 "pg_catalog.pg_table_is_visible(c.oid)");
1243 appendPQExpBufferStr(&buf,
1245 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1247 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1249 res = PSQLexec(buf.data);
1250 termPQExpBuffer(&buf);
1254 myopt.nullPrint = NULL;
1255 myopt.title = _("Object descriptions");
1256 myopt.translate_header = true;
1257 myopt.translate_columns = translate_columns;
1258 myopt.n_translate_columns = lengthof(translate_columns);
1260 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1268 * describeTableDetails (for \d)
1270 * This routine finds the tables to be displayed, and calls
1271 * describeOneTableDetails for each one.
1273 * verbose: if true, this is \d+
1276 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1278 PQExpBufferData buf;
1282 initPQExpBuffer(&buf);
1284 printfPQExpBuffer(&buf,
1288 "FROM pg_catalog.pg_class c\n"
1289 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1291 if (!showSystem && !pattern)
1292 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1293 " AND n.nspname <> 'information_schema'\n");
1295 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1296 "n.nspname", "c.relname", NULL,
1297 "pg_catalog.pg_table_is_visible(c.oid)");
1299 appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1301 res = PSQLexec(buf.data);
1302 termPQExpBuffer(&buf);
1306 if (PQntuples(res) == 0)
1309 psql_error("Did not find any relation named \"%s\".\n",
1315 for (i = 0; i < PQntuples(res); i++)
1318 const char *nspname;
1319 const char *relname;
1321 oid = PQgetvalue(res, i, 0);
1322 nspname = PQgetvalue(res, i, 1);
1323 relname = PQgetvalue(res, i, 2);
1325 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1342 * describeOneTableDetails (for \d)
1344 * Unfortunately, the information presented here is so complicated that it
1345 * cannot be done in a single query. So we have to assemble the printed table
1346 * by hand and pass it to the underlying printTable() function.
1349 describeOneTableDetails(const char *schemaname,
1350 const char *relationname,
1354 PQExpBufferData buf;
1355 PGresult *res = NULL;
1356 printTableOpt myopt = pset.popt.topt;
1357 printTableContent cont;
1358 bool printTableInitialized = false;
1360 char *view_def = NULL;
1362 char **seq_values = NULL;
1364 PQExpBufferData title;
1365 PQExpBufferData tmpbuf;
1376 bool forcerowsecurity;
1381 char relpersistence;
1384 bool show_column_details = false;
1389 myopt.default_footer = false;
1390 /* This output looks confusing in expanded mode. */
1391 myopt.expanded = false;
1393 initPQExpBuffer(&buf);
1394 initPQExpBuffer(&title);
1395 initPQExpBuffer(&tmpbuf);
1397 /* Get general table info */
1398 if (pset.sversion >= 90500)
1400 printfPQExpBuffer(&buf,
1401 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1402 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1403 "c.relhasoids, %s, c.reltablespace, "
1404 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1405 "c.relpersistence, c.relreplident\n"
1406 "FROM pg_catalog.pg_class c\n "
1407 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1408 "WHERE c.oid = '%s';",
1410 "pg_catalog.array_to_string(c.reloptions || "
1411 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1415 else if (pset.sversion >= 90400)
1417 printfPQExpBuffer(&buf,
1418 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1419 "c.relhastriggers, false, false, c.relhasoids, "
1420 "%s, c.reltablespace, "
1421 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1422 "c.relpersistence, c.relreplident\n"
1423 "FROM pg_catalog.pg_class c\n "
1424 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1425 "WHERE c.oid = '%s';",
1427 "pg_catalog.array_to_string(c.reloptions || "
1428 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1432 else if (pset.sversion >= 90100)
1434 printfPQExpBuffer(&buf,
1435 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1436 "c.relhastriggers, false, false, c.relhasoids, "
1437 "%s, c.reltablespace, "
1438 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1439 "c.relpersistence\n"
1440 "FROM pg_catalog.pg_class c\n "
1441 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1442 "WHERE c.oid = '%s';",
1444 "pg_catalog.array_to_string(c.reloptions || "
1445 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1449 else if (pset.sversion >= 90000)
1451 printfPQExpBuffer(&buf,
1452 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1453 "c.relhastriggers, false, false, c.relhasoids, "
1454 "%s, c.reltablespace, "
1455 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1456 "FROM pg_catalog.pg_class c\n "
1457 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1458 "WHERE c.oid = '%s';",
1460 "pg_catalog.array_to_string(c.reloptions || "
1461 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1465 else if (pset.sversion >= 80400)
1467 printfPQExpBuffer(&buf,
1468 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1469 "c.relhastriggers, false, false, c.relhasoids, "
1470 "%s, c.reltablespace\n"
1471 "FROM pg_catalog.pg_class c\n "
1472 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1473 "WHERE c.oid = '%s';",
1475 "pg_catalog.array_to_string(c.reloptions || "
1476 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1480 else if (pset.sversion >= 80200)
1482 printfPQExpBuffer(&buf,
1483 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1484 "reltriggers <> 0, false, false, relhasoids, "
1485 "%s, reltablespace\n"
1486 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1488 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1491 else if (pset.sversion >= 80000)
1493 printfPQExpBuffer(&buf,
1494 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1495 "reltriggers <> 0, false, false, relhasoids, "
1496 "'', reltablespace\n"
1497 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1502 printfPQExpBuffer(&buf,
1503 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1504 "reltriggers <> 0, false, false, relhasoids, "
1506 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1510 res = PSQLexec(buf.data);
1514 /* Did we get anything? */
1515 if (PQntuples(res) == 0)
1518 psql_error("Did not find any relation with OID %s.\n", oid);
1522 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1523 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1524 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1525 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1526 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1527 tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1528 tableinfo.forcerowsecurity = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1529 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 7), "t") == 0;
1530 tableinfo.reloptions = (pset.sversion >= 80200) ?
1531 pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
1532 tableinfo.tablespace = (pset.sversion >= 80000) ?
1533 atooid(PQgetvalue(res, 0, 9)) : 0;
1534 tableinfo.reloftype = (pset.sversion >= 90000 &&
1535 strcmp(PQgetvalue(res, 0, 10), "") != 0) ?
1536 pg_strdup(PQgetvalue(res, 0, 10)) : NULL;
1537 tableinfo.relpersistence = (pset.sversion >= 90100) ?
1538 *(PQgetvalue(res, 0, 11)) : 0;
1539 tableinfo.relreplident = (pset.sversion >= 90400) ?
1540 *(PQgetvalue(res, 0, 12)) : 'd';
1545 * If it's a sequence, fetch its values and store into an array that will
1548 if (tableinfo.relkind == 'S')
1550 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1551 /* must be separate because fmtId isn't reentrant */
1552 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1554 res = PSQLexec(buf.data);
1558 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1560 for (i = 0; i < PQnfields(res); i++)
1561 seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1562 seq_values[i] = NULL;
1571 * You need to modify value of "firstvcol" which will be defined below if
1572 * you are adding column(s) preceding to verbose-only columns.
1574 printfPQExpBuffer(&buf, "SELECT a.attname,");
1575 appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1576 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1577 "\n FROM pg_catalog.pg_attrdef d"
1578 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1579 "\n a.attnotnull, a.attnum,");
1580 if (pset.sversion >= 90100)
1581 appendPQExpBufferStr(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1582 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1584 appendPQExpBufferStr(&buf, "\n NULL AS attcollation");
1585 if (tableinfo.relkind == 'i')
1586 appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1588 appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
1589 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1590 appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1591 " '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1592 " pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1594 appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
1597 appendPQExpBufferStr(&buf, ",\n a.attstorage");
1598 appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1601 * In 9.0+, we have column comments for: relations, views, composite
1602 * types, and foreign tables (c.f. CommentObject() in comment.c).
1604 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1605 tableinfo.relkind == 'm' || tableinfo.relkind == 'f' ||
1606 tableinfo.relkind == 'c' || tableinfo.relkind == 'P')
1607 appendPQExpBufferStr(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1610 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1611 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1612 appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1614 res = PSQLexec(buf.data);
1617 numrows = PQntuples(res);
1620 switch (tableinfo.relkind)
1623 if (tableinfo.relpersistence == 'u')
1624 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1625 schemaname, relationname);
1627 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1628 schemaname, relationname);
1631 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1632 schemaname, relationname);
1635 if (tableinfo.relpersistence == 'u')
1636 printfPQExpBuffer(&title, _("Unlogged materialized view \"%s.%s\""),
1637 schemaname, relationname);
1639 printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
1640 schemaname, relationname);
1643 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1644 schemaname, relationname);
1647 if (tableinfo.relpersistence == 'u')
1648 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1649 schemaname, relationname);
1651 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1652 schemaname, relationname);
1655 /* not used as of 8.2, but keep it for backwards compatibility */
1656 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1657 schemaname, relationname);
1660 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1661 schemaname, relationname);
1664 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1665 schemaname, relationname);
1668 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1669 schemaname, relationname);
1672 if (tableinfo.relpersistence == 'u')
1673 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1674 schemaname, relationname);
1676 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1677 schemaname, relationname);
1680 /* untranslated unknown relkind */
1681 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1682 tableinfo.relkind, schemaname, relationname);
1686 /* Set the number of columns, and their names */
1687 headers[0] = gettext_noop("Column");
1688 headers[1] = gettext_noop("Type");
1691 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1692 tableinfo.relkind == 'm' || tableinfo.relkind == 'f' ||
1693 tableinfo.relkind == 'c' || tableinfo.relkind == 'P')
1695 headers[cols++] = gettext_noop("Collation");
1696 headers[cols++] = gettext_noop("Nullable");
1697 headers[cols++] = gettext_noop("Default");
1698 show_column_details = true;
1701 if (tableinfo.relkind == 'S')
1702 headers[cols++] = gettext_noop("Value");
1704 if (tableinfo.relkind == 'i')
1705 headers[cols++] = gettext_noop("Definition");
1707 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1708 headers[cols++] = gettext_noop("FDW Options");
1712 headers[cols++] = gettext_noop("Storage");
1713 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1714 tableinfo.relkind == 'f' || tableinfo.relkind == 'P')
1715 headers[cols++] = gettext_noop("Stats target");
1716 /* Column comments, if the relkind supports this feature. */
1717 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1718 tableinfo.relkind == 'm' || tableinfo.relkind == 'c' ||
1719 tableinfo.relkind == 'f' || tableinfo.relkind == 'P')
1720 headers[cols++] = gettext_noop("Description");
1723 printTableInit(&cont, &myopt, title.data, cols, numrows);
1724 printTableInitialized = true;
1726 for (i = 0; i < cols; i++)
1727 printTableAddHeader(&cont, headers[i], true, 'l');
1729 /* Check if table is a view or materialized view */
1730 if ((tableinfo.relkind == 'v' || tableinfo.relkind == 'm') && verbose)
1734 printfPQExpBuffer(&buf,
1735 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1737 result = PSQLexec(buf.data);
1741 if (PQntuples(result) > 0)
1742 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1747 /* Generate table cells to be printed */
1748 for (i = 0; i < numrows; i++)
1751 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1754 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1756 /* Collation, Nullable, Default */
1757 if (show_column_details)
1759 printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
1761 printTableAddCell(&cont, strcmp(PQgetvalue(res, i, 3), "t") == 0 ? "not null" : "", false, false);
1763 /* (note: above we cut off the 'default' string at 128) */
1764 printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
1767 /* Value: for sequences only */
1768 if (tableinfo.relkind == 'S')
1769 printTableAddCell(&cont, seq_values[i], false, false);
1771 /* Expression for index column */
1772 if (tableinfo.relkind == 'i')
1773 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1775 /* FDW options for foreign table column, only for 9.2 or later */
1776 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1777 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1779 /* Storage and Description */
1783 char *storage = PQgetvalue(res, i, firstvcol);
1785 /* these strings are literal in our syntax, so not translated. */
1786 printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1787 (storage[0] == 'm' ? "main" :
1788 (storage[0] == 'x' ? "extended" :
1789 (storage[0] == 'e' ? "external" :
1793 /* Statistics target, if the relkind supports this feature */
1794 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1795 tableinfo.relkind == 'f' || tableinfo.relkind == 'P')
1797 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1801 /* Column comments, if the relkind supports this feature. */
1802 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1803 tableinfo.relkind == 'm' || tableinfo.relkind == 'c' ||
1804 tableinfo.relkind == 'f' || tableinfo.relkind == 'P')
1805 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1811 if (pset.sversion >= 100000)
1813 /* Get the partition information */
1818 printfPQExpBuffer(&buf,
1819 "SELECT inhparent::pg_catalog.regclass, pg_get_expr(c.relpartbound, inhrelid)"
1820 " FROM pg_catalog.pg_class c"
1821 " JOIN pg_catalog.pg_inherits"
1822 " ON c.oid = inhrelid"
1823 " WHERE c.oid = '%s' AND c.relispartition;", oid);
1824 result = PSQLexec(buf.data);
1828 if (PQntuples(result) > 0)
1830 parent_name = PQgetvalue(result, 0, 0);
1831 partdef = PQgetvalue(result, 0, 1);
1832 printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s"), parent_name,
1834 printTableAddFooter(&cont, tmpbuf.data);
1839 if (tableinfo.relkind == 'P')
1841 /* Get the partition key information */
1845 printfPQExpBuffer(&buf,
1846 "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
1848 result = PSQLexec(buf.data);
1849 if (!result || PQntuples(result) != 1)
1852 partkeydef = PQgetvalue(result, 0, 0);
1853 printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
1854 printTableAddFooter(&cont, tmpbuf.data);
1858 if (tableinfo.relkind == 'i')
1860 /* Footer information about an index */
1863 printfPQExpBuffer(&buf,
1864 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1865 if (pset.sversion >= 80200)
1866 appendPQExpBufferStr(&buf, "i.indisvalid,\n");
1868 appendPQExpBufferStr(&buf, "true AS indisvalid,\n");
1869 if (pset.sversion >= 90000)
1870 appendPQExpBufferStr(&buf,
1871 " (NOT i.indimmediate) AND "
1872 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1873 "WHERE conrelid = i.indrelid AND "
1874 "conindid = i.indexrelid AND "
1875 "contype IN ('p','u','x') AND "
1876 "condeferrable) AS condeferrable,\n"
1877 " (NOT i.indimmediate) AND "
1878 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1879 "WHERE conrelid = i.indrelid AND "
1880 "conindid = i.indexrelid AND "
1881 "contype IN ('p','u','x') AND "
1882 "condeferred) AS condeferred,\n");
1884 appendPQExpBufferStr(&buf,
1885 " false AS condeferrable, false AS condeferred,\n");
1887 if (pset.sversion >= 90400)
1888 appendPQExpBuffer(&buf, "i.indisreplident,\n");
1890 appendPQExpBuffer(&buf, "false AS indisreplident,\n");
1892 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
1893 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1894 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1895 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1896 "AND i.indrelid = c2.oid;",
1899 result = PSQLexec(buf.data);
1902 else if (PQntuples(result) != 1)
1909 char *indisunique = PQgetvalue(result, 0, 0);
1910 char *indisprimary = PQgetvalue(result, 0, 1);
1911 char *indisclustered = PQgetvalue(result, 0, 2);
1912 char *indisvalid = PQgetvalue(result, 0, 3);
1913 char *deferrable = PQgetvalue(result, 0, 4);
1914 char *deferred = PQgetvalue(result, 0, 5);
1915 char *indisreplident = PQgetvalue(result, 0, 6);
1916 char *indamname = PQgetvalue(result, 0, 7);
1917 char *indtable = PQgetvalue(result, 0, 8);
1918 char *indpred = PQgetvalue(result, 0, 9);
1920 if (strcmp(indisprimary, "t") == 0)
1921 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1922 else if (strcmp(indisunique, "t") == 0)
1923 printfPQExpBuffer(&tmpbuf, _("unique, "));
1925 resetPQExpBuffer(&tmpbuf);
1926 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1928 /* we assume here that index and table are in same schema */
1929 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1930 schemaname, indtable);
1932 if (strlen(indpred))
1933 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1935 if (strcmp(indisclustered, "t") == 0)
1936 appendPQExpBufferStr(&tmpbuf, _(", clustered"));
1938 if (strcmp(indisvalid, "t") != 0)
1939 appendPQExpBufferStr(&tmpbuf, _(", invalid"));
1941 if (strcmp(deferrable, "t") == 0)
1942 appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
1944 if (strcmp(deferred, "t") == 0)
1945 appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
1947 if (strcmp(indisreplident, "t") == 0)
1948 appendPQExpBuffer(&tmpbuf, _(", replica identity"));
1950 printTableAddFooter(&cont, tmpbuf.data);
1951 add_tablespace_footer(&cont, tableinfo.relkind,
1952 tableinfo.tablespace, true);
1957 else if (tableinfo.relkind == 'S')
1959 /* Footer information about a sequence */
1960 PGresult *result = NULL;
1962 /* Get the column that owns this sequence */
1963 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1964 "\n pg_catalog.quote_ident(relname) || '.' ||"
1965 "\n pg_catalog.quote_ident(attname)"
1966 "\nFROM pg_catalog.pg_class c"
1967 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1968 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1969 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1970 "\n a.attrelid=c.oid AND"
1971 "\n a.attnum=d.refobjsubid)"
1972 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1973 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1975 "\n AND d.deptype='a'",
1978 result = PSQLexec(buf.data);
1981 else if (PQntuples(result) == 1)
1983 printfPQExpBuffer(&buf, _("Owned by: %s"),
1984 PQgetvalue(result, 0, 0));
1985 printTableAddFooter(&cont, buf.data);
1989 * If we get no rows back, don't show anything (obviously). We should
1990 * never get more than one row back, but if we do, just ignore it and
1991 * don't print anything.
1995 else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1996 tableinfo.relkind == 'f' || tableinfo.relkind == 'P')
1998 /* Footer information about a table */
1999 PGresult *result = NULL;
2003 if (tableinfo.hasindex)
2005 printfPQExpBuffer(&buf,
2006 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
2007 if (pset.sversion >= 80200)
2008 appendPQExpBufferStr(&buf, "i.indisvalid, ");
2010 appendPQExpBufferStr(&buf, "true as indisvalid, ");
2011 appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
2012 if (pset.sversion >= 90000)
2013 appendPQExpBufferStr(&buf,
2014 "pg_catalog.pg_get_constraintdef(con.oid, true), "
2015 "contype, condeferrable, condeferred");
2017 appendPQExpBufferStr(&buf,
2018 "null AS constraintdef, null AS contype, "
2019 "false AS condeferrable, false AS condeferred");
2020 if (pset.sversion >= 90400)
2021 appendPQExpBufferStr(&buf, ", i.indisreplident");
2023 appendPQExpBufferStr(&buf, ", false AS indisreplident");
2024 if (pset.sversion >= 80000)
2025 appendPQExpBufferStr(&buf, ", c2.reltablespace");
2026 appendPQExpBufferStr(&buf,
2027 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
2028 if (pset.sversion >= 90000)
2029 appendPQExpBufferStr(&buf,
2030 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
2031 appendPQExpBuffer(&buf,
2032 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2033 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
2035 result = PSQLexec(buf.data);
2039 tuples = PQntuples(result);
2043 printTableAddFooter(&cont, _("Indexes:"));
2044 for (i = 0; i < tuples; i++)
2046 /* untranslated index name */
2047 printfPQExpBuffer(&buf, " \"%s\"",
2048 PQgetvalue(result, i, 0));
2050 /* If exclusion constraint, print the constraintdef */
2051 if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
2053 appendPQExpBuffer(&buf, " %s",
2054 PQgetvalue(result, i, 6));
2058 const char *indexdef;
2059 const char *usingpos;
2061 /* Label as primary key or unique (but not both) */
2062 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2063 appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2064 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2066 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2067 appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2069 appendPQExpBufferStr(&buf, " UNIQUE,");
2072 /* Everything after "USING" is echoed verbatim */
2073 indexdef = PQgetvalue(result, i, 5);
2074 usingpos = strstr(indexdef, " USING ");
2076 indexdef = usingpos + 7;
2077 appendPQExpBuffer(&buf, " %s", indexdef);
2079 /* Need these for deferrable PK/UNIQUE indexes */
2080 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2081 appendPQExpBufferStr(&buf, " DEFERRABLE");
2083 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2084 appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2087 /* Add these for all cases */
2088 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2089 appendPQExpBufferStr(&buf, " CLUSTER");
2091 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2092 appendPQExpBufferStr(&buf, " INVALID");
2094 if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2095 appendPQExpBuffer(&buf, " REPLICA IDENTITY");
2097 printTableAddFooter(&cont, buf.data);
2099 /* Print tablespace of the index on the same line */
2100 if (pset.sversion >= 80000)
2101 add_tablespace_footer(&cont, 'i',
2102 atooid(PQgetvalue(result, i, 11)),
2109 /* print table (and column) check constraints */
2110 if (tableinfo.checks)
2112 printfPQExpBuffer(&buf,
2113 "SELECT r.conname, "
2114 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2115 "FROM pg_catalog.pg_constraint r\n"
2116 "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
2119 result = PSQLexec(buf.data);
2123 tuples = PQntuples(result);
2127 printTableAddFooter(&cont, _("Check constraints:"));
2128 for (i = 0; i < tuples; i++)
2130 /* untranslated contraint name and def */
2131 printfPQExpBuffer(&buf, " \"%s\" %s",
2132 PQgetvalue(result, i, 0),
2133 PQgetvalue(result, i, 1));
2135 printTableAddFooter(&cont, buf.data);
2141 /* print foreign-key constraints (there are none if no triggers) */
2142 if (tableinfo.hastriggers)
2144 printfPQExpBuffer(&buf,
2146 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
2147 "FROM pg_catalog.pg_constraint r\n"
2148 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
2150 result = PSQLexec(buf.data);
2154 tuples = PQntuples(result);
2158 printTableAddFooter(&cont, _("Foreign-key constraints:"));
2159 for (i = 0; i < tuples; i++)
2161 /* untranslated constraint name and def */
2162 printfPQExpBuffer(&buf, " \"%s\" %s",
2163 PQgetvalue(result, i, 0),
2164 PQgetvalue(result, i, 1));
2166 printTableAddFooter(&cont, buf.data);
2172 /* print incoming foreign-key references (none if no triggers) */
2173 if (tableinfo.hastriggers)
2175 printfPQExpBuffer(&buf,
2176 "SELECT conname, conrelid::pg_catalog.regclass,\n"
2177 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
2178 "FROM pg_catalog.pg_constraint c\n"
2179 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
2181 result = PSQLexec(buf.data);
2185 tuples = PQntuples(result);
2189 printTableAddFooter(&cont, _("Referenced by:"));
2190 for (i = 0; i < tuples; i++)
2192 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2193 PQgetvalue(result, i, 1),
2194 PQgetvalue(result, i, 0),
2195 PQgetvalue(result, i, 2));
2197 printTableAddFooter(&cont, buf.data);
2203 /* print any row-level policies */
2204 if (pset.sversion >= 90500)
2206 if (pset.sversion >= 100000)
2207 printfPQExpBuffer(&buf,
2208 "SELECT pol.polname, pol.polpermissive,\n"
2209 "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE array_to_string(array(select rolname from pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2210 "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2211 "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2212 "CASE pol.polcmd \n"
2213 "WHEN 'r' THEN 'SELECT'\n"
2214 "WHEN 'a' THEN 'INSERT'\n"
2215 "WHEN 'w' THEN 'UPDATE'\n"
2216 "WHEN 'd' THEN 'DELETE'\n"
2218 "FROM pg_catalog.pg_policy pol\n"
2219 "WHERE pol.polrelid = '%s' ORDER BY 1;",
2222 printfPQExpBuffer(&buf,
2223 "SELECT pol.polname, 't' as polpermissive,\n"
2224 "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE array_to_string(array(select rolname from pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2225 "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2226 "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2227 "CASE pol.polcmd \n"
2228 "WHEN 'r' THEN 'SELECT'\n"
2229 "WHEN 'a' THEN 'INSERT'\n"
2230 "WHEN 'w' THEN 'UPDATE'\n"
2231 "WHEN 'd' THEN 'DELETE'\n"
2233 "FROM pg_catalog.pg_policy pol\n"
2234 "WHERE pol.polrelid = '%s' ORDER BY 1;",
2237 result = PSQLexec(buf.data);
2241 tuples = PQntuples(result);
2244 * Handle cases where RLS is enabled and there are policies, or
2245 * there aren't policies, or RLS isn't enabled but there are
2248 if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2249 printTableAddFooter(&cont, _("Policies:"));
2251 if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2252 printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2254 if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2255 printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2257 if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2258 printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2260 if (!tableinfo.rowsecurity && tuples > 0)
2261 printTableAddFooter(&cont, _("Policies (row security disabled):"));
2263 /* Might be an empty set - that's ok */
2264 for (i = 0; i < tuples; i++)
2266 printfPQExpBuffer(&buf, " POLICY \"%s\"",
2267 PQgetvalue(result, i, 0));
2269 if (*(PQgetvalue(result, i, 1)) == 'f')
2270 appendPQExpBuffer(&buf, " AS RESTRICTIVE");
2272 if (!PQgetisnull(result, i, 5))
2273 appendPQExpBuffer(&buf, " FOR %s",
2274 PQgetvalue(result, i, 5));
2276 if (!PQgetisnull(result, i, 2))
2278 appendPQExpBuffer(&buf, "\n TO %s",
2279 PQgetvalue(result, i, 2));
2282 if (!PQgetisnull(result, i, 3))
2283 appendPQExpBuffer(&buf, "\n USING (%s)",
2284 PQgetvalue(result, i, 3));
2286 if (!PQgetisnull(result, i, 4))
2287 appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2288 PQgetvalue(result, i, 4));
2290 printTableAddFooter(&cont, buf.data);
2297 if (tableinfo.hasrules && tableinfo.relkind != 'm')
2299 if (pset.sversion >= 80300)
2301 printfPQExpBuffer(&buf,
2302 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2304 "FROM pg_catalog.pg_rewrite r\n"
2305 "WHERE r.ev_class = '%s' ORDER BY 1;",
2310 printfPQExpBuffer(&buf,
2311 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2312 "'O'::char AS ev_enabled\n"
2313 "FROM pg_catalog.pg_rewrite r\n"
2314 "WHERE r.ev_class = '%s' ORDER BY 1;",
2317 result = PSQLexec(buf.data);
2321 tuples = PQntuples(result);
2328 for (category = 0; category < 4; category++)
2330 have_heading = false;
2332 for (i = 0; i < tuples; i++)
2334 const char *ruledef;
2335 bool list_rule = false;
2340 if (*PQgetvalue(result, i, 2) == 'O')
2344 if (*PQgetvalue(result, i, 2) == 'D')
2348 if (*PQgetvalue(result, i, 2) == 'A')
2352 if (*PQgetvalue(result, i, 2) == 'R')
2364 printfPQExpBuffer(&buf, _("Rules:"));
2367 printfPQExpBuffer(&buf, _("Disabled rules:"));
2370 printfPQExpBuffer(&buf, _("Rules firing always:"));
2373 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
2376 printTableAddFooter(&cont, buf.data);
2377 have_heading = true;
2380 /* Everything after "CREATE RULE" is echoed verbatim */
2381 ruledef = PQgetvalue(result, i, 1);
2383 printfPQExpBuffer(&buf, " %s", ruledef);
2384 printTableAddFooter(&cont, buf.data);
2394 PGresult *result = NULL;
2396 /* Footer information about a view */
2397 printTableAddFooter(&cont, _("View definition:"));
2398 printTableAddFooter(&cont, view_def);
2401 if (tableinfo.hasrules)
2403 printfPQExpBuffer(&buf,
2404 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
2405 "FROM pg_catalog.pg_rewrite r\n"
2406 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
2408 result = PSQLexec(buf.data);
2412 if (PQntuples(result) > 0)
2414 printTableAddFooter(&cont, _("Rules:"));
2415 for (i = 0; i < PQntuples(result); i++)
2417 const char *ruledef;
2419 /* Everything after "CREATE RULE" is echoed verbatim */
2420 ruledef = PQgetvalue(result, i, 1);
2423 printfPQExpBuffer(&buf, " %s", ruledef);
2424 printTableAddFooter(&cont, buf.data);
2432 * Print triggers next, if any (but only user-defined triggers). This
2433 * could apply to either a table or a view.
2435 if (tableinfo.hastriggers)
2440 printfPQExpBuffer(&buf,
2442 "pg_catalog.pg_get_triggerdef(t.oid%s), "
2444 "FROM pg_catalog.pg_trigger t\n"
2445 "WHERE t.tgrelid = '%s' AND ",
2446 (pset.sversion >= 90000 ? ", true" : ""),
2447 (pset.sversion >= 90000 ? "t.tgisinternal" :
2448 pset.sversion >= 80300 ?
2449 "t.tgconstraint <> 0 AS tgisinternal" :
2450 "false AS tgisinternal"), oid);
2451 if (pset.sversion >= 90000)
2452 /* display/warn about disabled internal triggers */
2453 appendPQExpBuffer(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
2454 else if (pset.sversion >= 80300)
2455 appendPQExpBufferStr(&buf, "(t.tgconstraint = 0 OR (t.tgconstraint <> 0 AND t.tgenabled = 'D'))");
2457 appendPQExpBufferStr(&buf,
2458 "(NOT tgisconstraint "
2460 " (SELECT 1 FROM pg_catalog.pg_depend d "
2461 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2462 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2463 appendPQExpBufferStr(&buf, "\nORDER BY 1;");
2465 result = PSQLexec(buf.data);
2469 tuples = PQntuples(result);
2477 * split the output into 4 different categories. Enabled triggers,
2478 * disabled triggers and the two special ALWAYS and REPLICA
2481 for (category = 0; category <= 4; category++)
2483 have_heading = false;
2484 for (i = 0; i < tuples; i++)
2488 const char *usingpos;
2489 const char *tgenabled;
2490 const char *tgisinternal;
2493 * Check if this trigger falls into the current category
2495 tgenabled = PQgetvalue(result, i, 2);
2496 tgisinternal = PQgetvalue(result, i, 3);
2497 list_trigger = false;
2501 if (*tgenabled == 'O' || *tgenabled == 't')
2502 list_trigger = true;
2505 if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2506 *tgisinternal == 'f')
2507 list_trigger = true;
2510 if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2511 *tgisinternal == 't')
2512 list_trigger = true;
2515 if (*tgenabled == 'A')
2516 list_trigger = true;
2519 if (*tgenabled == 'R')
2520 list_trigger = true;
2523 if (list_trigger == false)
2526 /* Print the category heading once */
2527 if (have_heading == false)
2532 printfPQExpBuffer(&buf, _("Triggers:"));
2535 if (pset.sversion >= 80300)
2536 printfPQExpBuffer(&buf, _("Disabled user triggers:"));
2538 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2541 printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
2544 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2547 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2551 printTableAddFooter(&cont, buf.data);
2552 have_heading = true;
2555 /* Everything after "TRIGGER" is echoed verbatim */
2556 tgdef = PQgetvalue(result, i, 1);
2557 usingpos = strstr(tgdef, " TRIGGER ");
2559 tgdef = usingpos + 9;
2561 printfPQExpBuffer(&buf, " %s", tgdef);
2562 printTableAddFooter(&cont, buf.data);
2570 * Finish printing the footer information about a table.
2572 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
2573 tableinfo.relkind == 'f' || tableinfo.relkind == 'P')
2578 /* print foreign server name */
2579 if (tableinfo.relkind == 'f')
2583 /* Footer information about foreign table */
2584 printfPQExpBuffer(&buf,
2585 "SELECT s.srvname,\n"
2586 " array_to_string(ARRAY(SELECT "
2587 " quote_ident(option_name) || ' ' || "
2588 " quote_literal(option_value) FROM "
2589 " pg_options_to_table(ftoptions)), ', ') "
2590 "FROM pg_catalog.pg_foreign_table f,\n"
2591 " pg_catalog.pg_foreign_server s\n"
2592 "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2594 result = PSQLexec(buf.data);
2597 else if (PQntuples(result) != 1)
2603 /* Print server name */
2604 printfPQExpBuffer(&buf, "Server: %s",
2605 PQgetvalue(result, 0, 0));
2606 printTableAddFooter(&cont, buf.data);
2608 /* Print per-table FDW options, if any */
2609 ftoptions = PQgetvalue(result, 0, 1);
2610 if (ftoptions && ftoptions[0] != '\0')
2612 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2613 printTableAddFooter(&cont, buf.data);
2618 /* print inherited tables (exclude, if parent is a partitioned table) */
2619 printfPQExpBuffer(&buf,
2620 "SELECT c.oid::pg_catalog.regclass"
2621 " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2622 " WHERE c.oid=i.inhparent AND i.inhrelid = '%s'"
2623 " AND c.relkind != 'P' ORDER BY inhseqno;", oid);
2625 result = PSQLexec(buf.data);
2630 const char *s = _("Inherits");
2631 int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2633 tuples = PQntuples(result);
2635 for (i = 0; i < tuples; i++)
2638 printfPQExpBuffer(&buf, "%s: %s",
2639 s, PQgetvalue(result, i, 0));
2641 printfPQExpBuffer(&buf, "%*s %s",
2642 sw, "", PQgetvalue(result, i, 0));
2644 appendPQExpBufferChar(&buf, ',');
2646 printTableAddFooter(&cont, buf.data);
2652 /* print child tables (with additional info if partitions) */
2653 if (pset.sversion >= 100000)
2654 printfPQExpBuffer(&buf,
2655 "SELECT c.oid::pg_catalog.regclass, pg_get_expr(c.relpartbound, c.oid)"
2656 " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2657 " WHERE c.oid=i.inhrelid AND"
2658 " i.inhparent = '%s' AND"
2659 " EXISTS (SELECT 1 FROM pg_class c WHERE c.oid = '%s')"
2660 " ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid, oid);
2661 else if (pset.sversion >= 80300)
2662 printfPQExpBuffer(&buf,
2663 "SELECT c.oid::pg_catalog.regclass"
2664 " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2665 " WHERE c.oid=i.inhrelid AND"
2666 " i.inhparent = '%s' AND"
2667 " EXISTS (SELECT 1 FROM pg_class c WHERE c.oid = '%s')"
2668 " ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid, oid);
2670 printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '%s' ORDER BY c.relname;", oid);
2672 result = PSQLexec(buf.data);
2676 tuples = PQntuples(result);
2680 /* print the number of child tables, if any */
2683 if (tableinfo.relkind != 'P')
2684 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2686 printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
2687 printTableAddFooter(&cont, buf.data);
2692 /* display the list of child tables */
2693 const char *ct = tableinfo.relkind != 'P' ? _("Child tables") : _("Partitions");
2694 int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2696 for (i = 0; i < tuples; i++)
2698 if (tableinfo.relkind != 'P')
2701 printfPQExpBuffer(&buf, "%s: %s",
2702 ct, PQgetvalue(result, i, 0));
2704 printfPQExpBuffer(&buf, "%*s %s",
2705 ctw, "", PQgetvalue(result, i, 0));
2710 printfPQExpBuffer(&buf, "%s: %s %s",
2711 ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1));
2713 printfPQExpBuffer(&buf, "%*s %s %s",
2714 ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1));
2717 appendPQExpBufferChar(&buf, ',');
2719 printTableAddFooter(&cont, buf.data);
2725 if (tableinfo.reloftype)
2727 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2728 printTableAddFooter(&cont, buf.data);
2731 if (verbose && (tableinfo.relkind == 'r' || tableinfo.relkind == 'm') &&
2734 * No need to display default values; we already display a REPLICA
2735 * IDENTITY marker on indexes.
2737 tableinfo.relreplident != 'i' &&
2738 ((strcmp(schemaname, "pg_catalog") != 0 && tableinfo.relreplident != 'd') ||
2739 (strcmp(schemaname, "pg_catalog") == 0 && tableinfo.relreplident != 'n')))
2741 const char *s = _("Replica Identity");
2743 printfPQExpBuffer(&buf, "%s: %s",
2745 tableinfo.relreplident == 'f' ? "FULL" :
2746 tableinfo.relreplident == 'n' ? "NOTHING" :
2749 printTableAddFooter(&cont, buf.data);
2752 /* OIDs, if verbose and not a materialized view */
2753 if (verbose && tableinfo.relkind != 'm' && tableinfo.hasoids)
2754 printTableAddFooter(&cont, _("Has OIDs: yes"));
2756 /* Tablespace info */
2757 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2761 /* reloptions, if verbose */
2763 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2765 const char *t = _("Options");
2767 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
2768 printTableAddFooter(&cont, buf.data);
2771 printTable(&cont, pset.queryFout, false, pset.logfile);
2778 if (printTableInitialized)
2779 printTableCleanup(&cont);
2780 termPQExpBuffer(&buf);
2781 termPQExpBuffer(&title);
2782 termPQExpBuffer(&tmpbuf);
2786 for (ptr = seq_values; *ptr; ptr++)
2801 * Add a tablespace description to a footer. If 'newline' is true, it is added
2802 * in a new line; otherwise it's appended to the current value of the last
2806 add_tablespace_footer(printTableContent *const cont, char relkind,
2807 Oid tablespace, const bool newline)
2809 /* relkinds for which we support tablespaces */
2810 if (relkind == 'r' || relkind == 'm' || relkind == 'i' || relkind == 'P')
2813 * We ignore the database default tablespace so that users not using
2814 * tablespaces don't need to know about them. This case also covers
2815 * pre-8.0 servers, for which tablespace will always be 0.
2817 if (tablespace != 0)
2819 PGresult *result = NULL;
2820 PQExpBufferData buf;
2822 initPQExpBuffer(&buf);
2823 printfPQExpBuffer(&buf,
2824 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2825 "WHERE oid = '%u';", tablespace);
2826 result = PSQLexec(buf.data);
2829 /* Should always be the case, but.... */
2830 if (PQntuples(result) > 0)
2834 /* Add the tablespace as a new footer */
2835 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2836 PQgetvalue(result, 0, 0));
2837 printTableAddFooter(cont, buf.data);
2841 /* Append the tablespace to the latest footer */
2842 printfPQExpBuffer(&buf, "%s", cont->footer->data);
2845 translator: before this string there's an index description like
2846 '"foo_pkey" PRIMARY KEY, btree (a)' */
2847 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2848 PQgetvalue(result, 0, 0));
2849 printTableSetFooter(cont, buf.data);
2853 termPQExpBuffer(&buf);
2861 * Describes roles. Any schema portion of the pattern is ignored.
2864 describeRoles(const char *pattern, bool verbose, bool showSystem)
2866 PQExpBufferData buf;
2868 printTableContent cont;
2869 printTableOpt myopt = pset.popt.topt;
2874 const char align = 'l';
2877 myopt.default_footer = false;
2879 initPQExpBuffer(&buf);
2881 if (pset.sversion >= 80100)
2883 printfPQExpBuffer(&buf,
2884 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2885 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2886 " r.rolconnlimit, r.rolvaliduntil,\n"
2887 " ARRAY(SELECT b.rolname\n"
2888 " FROM pg_catalog.pg_auth_members m\n"
2889 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2890 " WHERE m.member = r.oid) as memberof");
2892 if (verbose && pset.sversion >= 80200)
2894 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2897 if (pset.sversion >= 90100)
2899 appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2902 if (pset.sversion >= 90500)
2904 appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
2907 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2909 if (!showSystem && !pattern)
2910 appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
2912 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2913 NULL, "r.rolname", NULL, NULL);
2917 printfPQExpBuffer(&buf,
2918 "SELECT u.usename AS rolname,\n"
2919 " u.usesuper AS rolsuper,\n"
2920 " true AS rolinherit, false AS rolcreaterole,\n"
2921 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2922 " -1 AS rolconnlimit,"
2923 " u.valuntil as rolvaliduntil,\n"
2924 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2925 "\nFROM pg_catalog.pg_user u\n");
2927 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2928 NULL, "u.usename", NULL, NULL);
2931 appendPQExpBufferStr(&buf, "ORDER BY 1;");
2933 res = PSQLexec(buf.data);
2937 nrows = PQntuples(res);
2938 attr = pg_malloc0((nrows + 1) * sizeof(*attr));
2940 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2942 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2943 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2944 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2946 if (verbose && pset.sversion >= 80200)
2947 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2949 for (i = 0; i < nrows; i++)
2951 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2953 resetPQExpBuffer(&buf);
2954 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2955 add_role_attribute(&buf, _("Superuser"));
2957 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2958 add_role_attribute(&buf, _("No inheritance"));
2960 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2961 add_role_attribute(&buf, _("Create role"));
2963 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2964 add_role_attribute(&buf, _("Create DB"));
2966 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2967 add_role_attribute(&buf, _("Cannot login"));
2969 if (pset.sversion >= 90100)
2970 if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2971 add_role_attribute(&buf, _("Replication"));
2973 if (pset.sversion >= 90500)
2974 if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
2975 add_role_attribute(&buf, _("Bypass RLS"));
2977 conns = atoi(PQgetvalue(res, i, 6));
2981 appendPQExpBufferChar(&buf, '\n');
2984 appendPQExpBufferStr(&buf, _("No connections"));
2986 appendPQExpBuffer(&buf, ngettext("%d connection",
2992 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2995 appendPQExpBufferStr(&buf, "\n");
2996 appendPQExpBufferStr(&buf, _("Password valid until "));
2997 appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3000 attr[i] = pg_strdup(buf.data);
3002 printTableAddCell(&cont, attr[i], false, false);
3004 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3006 if (verbose && pset.sversion >= 80200)
3007 printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
3009 termPQExpBuffer(&buf);
3011 printTable(&cont, pset.queryFout, false, pset.logfile);
3012 printTableCleanup(&cont);
3014 for (i = 0; i < nrows; i++)
3023 add_role_attribute(PQExpBuffer buf, const char *const str)
3026 appendPQExpBufferStr(buf, ", ");
3028 appendPQExpBufferStr(buf, str);
3035 listDbRoleSettings(const char *pattern, const char *pattern2)
3037 PQExpBufferData buf;
3039 printQueryOpt myopt = pset.popt;
3041 initPQExpBuffer(&buf);
3043 if (pset.sversion >= 90000)
3047 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3048 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3049 "FROM pg_db_role_setting AS s\n"
3050 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
3051 "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
3052 gettext_noop("Role"),
3053 gettext_noop("Database"),
3054 gettext_noop("Settings"));
3055 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3056 NULL, "pg_roles.rolname", NULL, NULL);
3057 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3058 NULL, "pg_database.datname", NULL, NULL);
3059 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3063 fprintf(pset.queryFout,
3064 _("No per-database role settings support in this server version.\n"));
3068 res = PSQLexec(buf.data);
3072 if (PQntuples(res) == 0 && !pset.quiet)
3075 fprintf(pset.queryFout, _("No matching settings found.\n"));
3077 fprintf(pset.queryFout, _("No settings found.\n"));
3081 myopt.nullPrint = NULL;
3082 myopt.title = _("List of settings");
3083 myopt.translate_header = true;
3085 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3089 resetPQExpBuffer(&buf);
3097 * handler for \dt, \di, etc.
3099 * tabtypes is an array of characters, specifying what info is desired:
3103 * m - materialized views
3105 * E - foreign table (Note: different from 'f', the relkind value)
3106 * (any order of the above is fine)
3107 * If tabtypes is empty, we default to \dtvsE.
3110 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
3112 bool showTables = strchr(tabtypes, 't') != NULL;
3113 bool showIndexes = strchr(tabtypes, 'i') != NULL;
3114 bool showViews = strchr(tabtypes, 'v') != NULL;
3115 bool showMatViews = strchr(tabtypes, 'm') != NULL;
3116 bool showSeq = strchr(tabtypes, 's') != NULL;
3117 bool showForeign = strchr(tabtypes, 'E') != NULL;
3119 PQExpBufferData buf;
3121 printQueryOpt myopt = pset.popt;
3122 static const bool translate_columns[] = {false, false, true, false, false, false, false};
3124 if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
3125 showTables = showViews = showMatViews = showSeq = showForeign = true;
3127 initPQExpBuffer(&buf);
3130 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
3131 * for backwards compatibility.
3133 printfPQExpBuffer(&buf,
3134 "SELECT n.nspname as \"%s\",\n"
3135 " c.relname as \"%s\",\n"
3137 " WHEN 'r' THEN '%s'"
3138 " WHEN 'v' THEN '%s'"
3139 " WHEN 'm' THEN '%s'"
3140 " WHEN 'i' THEN '%s'"
3141 " WHEN 'S' THEN '%s'"
3142 " WHEN 's' THEN '%s'"
3143 " WHEN 'f' THEN '%s'"
3144 " WHEN 'P' THEN '%s'"
3146 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
3147 gettext_noop("Schema"),
3148 gettext_noop("Name"),
3149 gettext_noop("table"),
3150 gettext_noop("view"),
3151 gettext_noop("materialized view"),
3152 gettext_noop("index"),
3153 gettext_noop("sequence"),
3154 gettext_noop("special"),
3155 gettext_noop("foreign table"),
3156 gettext_noop("table"), /* partitioned table */
3157 gettext_noop("Type"),
3158 gettext_noop("Owner"));
3161 appendPQExpBuffer(&buf,
3162 ",\n c2.relname as \"%s\"",
3163 gettext_noop("Table"));
3168 * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
3169 * size of a table, including FSM, VM and TOAST tables.
3171 if (pset.sversion >= 90000)
3172 appendPQExpBuffer(&buf,
3173 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
3174 gettext_noop("Size"));
3175 else if (pset.sversion >= 80100)
3176 appendPQExpBuffer(&buf,
3177 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
3178 gettext_noop("Size"));
3180 appendPQExpBuffer(&buf,
3181 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
3182 gettext_noop("Description"));
3185 appendPQExpBufferStr(&buf,
3186 "\nFROM pg_catalog.pg_class c"
3187 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
3189 appendPQExpBufferStr(&buf,
3190 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
3191 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
3193 appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
3195 appendPQExpBufferStr(&buf, "'r', 'P',");
3197 appendPQExpBufferStr(&buf, "'v',");
3199 appendPQExpBufferStr(&buf, "'m',");
3201 appendPQExpBufferStr(&buf, "'i',");
3203 appendPQExpBufferStr(&buf, "'S',");
3204 if (showSystem || pattern)
3205 appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
3208 appendPQExpBufferStr(&buf, "'f',");
3210 appendPQExpBufferStr(&buf, "''"); /* dummy */
3211 appendPQExpBufferStr(&buf, ")\n");
3213 if (!showSystem && !pattern)
3214 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3215 " AND n.nspname <> 'information_schema'\n");
3218 * TOAST objects are suppressed unconditionally. Since we don't provide
3219 * any way to select relkind 't' above, we would never show toast tables
3220 * in any case; it seems a bit confusing to allow their indexes to be
3221 * shown. Use plain \d if you really need to look at a TOAST table/index.
3223 appendPQExpBufferStr(&buf, " AND n.nspname !~ '^pg_toast'\n");
3225 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3226 "n.nspname", "c.relname", NULL,
3227 "pg_catalog.pg_table_is_visible(c.oid)");
3229 appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
3231 res = PSQLexec(buf.data);
3232 termPQExpBuffer(&buf);
3236 if (PQntuples(res) == 0 && !pset.quiet)
3239 fprintf(pset.queryFout, _("No matching relations found.\n"));
3241 fprintf(pset.queryFout, _("No relations found.\n"));
3245 myopt.nullPrint = NULL;
3246 myopt.title = _("List of relations");
3247 myopt.translate_header = true;
3248 myopt.translate_columns = translate_columns;
3249 myopt.n_translate_columns = lengthof(translate_columns);
3251 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3262 * Describes languages.
3265 listLanguages(const char *pattern, bool verbose, bool showSystem)
3267 PQExpBufferData buf;
3269 printQueryOpt myopt = pset.popt;
3271 initPQExpBuffer(&buf);
3273 printfPQExpBuffer(&buf,
3274 "SELECT l.lanname AS \"%s\",\n",
3275 gettext_noop("Name"));
3276 if (pset.sversion >= 80300)
3277 appendPQExpBuffer(&buf,
3278 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
3279 gettext_noop("Owner"));
3281 appendPQExpBuffer(&buf,
3282 " l.lanpltrusted AS \"%s\"",
3283 gettext_noop("Trusted"));
3287 appendPQExpBuffer(&buf,
3288 ",\n NOT l.lanispl AS \"%s\",\n"
3289 " l.lanplcallfoid::regprocedure AS \"%s\",\n"
3290 " l.lanvalidator::regprocedure AS \"%s\",\n ",
3291 gettext_noop("Internal Language"),
3292 gettext_noop("Call Handler"),
3293 gettext_noop("Validator"));
3294 if (pset.sversion >= 90000)
3295 appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
3296 gettext_noop("Inline Handler"));
3297 printACLColumn(&buf, "l.lanacl");
3300 appendPQExpBuffer(&buf,
3301 ",\n d.description AS \"%s\""
3302 "\nFROM pg_catalog.pg_language l\n"
3303 "LEFT JOIN pg_catalog.pg_description d\n"
3304 " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
3305 " AND d.objsubid = 0\n",
3306 gettext_noop("Description"));
3309 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3310 NULL, "l.lanname", NULL, NULL);
3312 if (!showSystem && !pattern)
3313 appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
3316 appendPQExpBufferStr(&buf, "ORDER BY 1;");
3318 res = PSQLexec(buf.data);
3319 termPQExpBuffer(&buf);
3323 myopt.nullPrint = NULL;
3324 myopt.title = _("List of languages");
3325 myopt.translate_header = true;
3327 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3337 * Describes domains.
3340 listDomains(const char *pattern, bool verbose, bool showSystem)
3342 PQExpBufferData buf;
3344 printQueryOpt myopt = pset.popt;
3346 initPQExpBuffer(&buf);
3348 printfPQExpBuffer(&buf,
3349 "SELECT n.nspname as \"%s\",\n"
3350 " t.typname as \"%s\",\n"
3351 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
3352 gettext_noop("Schema"),
3353 gettext_noop("Name"),
3354 gettext_noop("Type"));
3356 if (pset.sversion >= 90100)
3357 appendPQExpBuffer(&buf,
3358 " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3359 " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
3360 gettext_noop("Collation"));
3361 appendPQExpBuffer(&buf,
3362 " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
3363 " t.typdefault as \"%s\",\n"
3364 " pg_catalog.array_to_string(ARRAY(\n"
3365 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3366 " ), ' ') as \"%s\"",
3367 gettext_noop("Nullable"),
3368 gettext_noop("Default"),
3369 gettext_noop("Check"));
3373 if (pset.sversion >= 90200)
3375 appendPQExpBufferStr(&buf, ",\n ");
3376 printACLColumn(&buf, "t.typacl");
3378 appendPQExpBuffer(&buf,
3379 ",\n d.description as \"%s\"",
3380 gettext_noop("Description"));
3383 appendPQExpBufferStr(&buf,
3384 "\nFROM pg_catalog.pg_type t\n"
3385 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3388 appendPQExpBufferStr(&buf,
3389 " LEFT JOIN pg_catalog.pg_description d "
3390 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3391 "AND d.objsubid = 0\n");
3393 appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3395 if (!showSystem && !pattern)
3396 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3397 " AND n.nspname <> 'information_schema'\n");
3399 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3400 "n.nspname", "t.typname", NULL,
3401 "pg_catalog.pg_type_is_visible(t.oid)");
3403 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3405 res = PSQLexec(buf.data);
3406 termPQExpBuffer(&buf);
3410 myopt.nullPrint = NULL;
3411 myopt.title = _("List of domains");
3412 myopt.translate_header = true;
3414 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3423 * Describes conversions.
3426 listConversions(const char *pattern, bool verbose, bool showSystem)
3428 PQExpBufferData buf;
3430 printQueryOpt myopt = pset.popt;
3431 static const bool translate_columns[] =
3432 {false, false, false, false, true, false};
3434 initPQExpBuffer(&buf);
3436 printfPQExpBuffer(&buf,
3437 "SELECT n.nspname AS \"%s\",\n"
3438 " c.conname AS \"%s\",\n"
3439 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3440 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3441 " CASE WHEN c.condefault THEN '%s'\n"
3442 " ELSE '%s' END AS \"%s\"",
3443 gettext_noop("Schema"),
3444 gettext_noop("Name"),
3445 gettext_noop("Source"),
3446 gettext_noop("Destination"),
3447 gettext_noop("yes"), gettext_noop("no"),
3448 gettext_noop("Default?"));
3451 appendPQExpBuffer(&buf,
3452 ",\n d.description AS \"%s\"",
3453 gettext_noop("Description"));
3455 appendPQExpBufferStr(&buf,
3456 "\nFROM pg_catalog.pg_conversion c\n"
3457 " JOIN pg_catalog.pg_namespace n "
3458 "ON n.oid = c.connamespace\n");
3461 appendPQExpBufferStr(&buf,
3462 "LEFT JOIN pg_catalog.pg_description d "
3463 "ON d.classoid = c.tableoid\n"
3464 " AND d.objoid = c.oid "
3465 "AND d.objsubid = 0\n");
3467 appendPQExpBufferStr(&buf, "WHERE true\n");
3469 if (!showSystem && !pattern)
3470 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3471 " AND n.nspname <> 'information_schema'\n");
3473 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3474 "n.nspname", "c.conname", NULL,
3475 "pg_catalog.pg_conversion_is_visible(c.oid)");
3477 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3479 res = PSQLexec(buf.data);
3480 termPQExpBuffer(&buf);
3484 myopt.nullPrint = NULL;
3485 myopt.title = _("List of conversions");
3486 myopt.translate_header = true;
3487 myopt.translate_columns = translate_columns;
3488 myopt.n_translate_columns = lengthof(translate_columns);
3490 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3499 * Describes Event Triggers.
3502 listEventTriggers(const char *pattern, bool verbose)
3504 PQExpBufferData buf;
3506 printQueryOpt myopt = pset.popt;
3507 static const bool translate_columns[] =
3508 {false, false, false, true, false, false, false};
3510 initPQExpBuffer(&buf);
3512 printfPQExpBuffer(&buf,
3513 "SELECT evtname as \"%s\", "
3514 "evtevent as \"%s\", "
3515 "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3516 " case evtenabled when 'O' then '%s'"
3517 " when 'R' then '%s'"
3518 " when 'A' then '%s'"
3519 " when 'D' then '%s' end as \"%s\",\n"
3520 " e.evtfoid::pg_catalog.regproc as \"%s\", "
3521 "pg_catalog.array_to_string(array(select x"
3522 " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3523 gettext_noop("Name"),
3524 gettext_noop("Event"),
3525 gettext_noop("Owner"),
3526 gettext_noop("enabled"),
3527 gettext_noop("replica"),
3528 gettext_noop("always"),
3529 gettext_noop("disabled"),
3530 gettext_noop("Enabled"),
3531 gettext_noop("Procedure"),
3532 gettext_noop("Tags"));
3534 appendPQExpBuffer(&buf,
3535 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3536 gettext_noop("Description"));
3537 appendPQExpBufferStr(&buf,
3538 "\nFROM pg_catalog.pg_event_trigger e ");
3540 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3541 NULL, "evtname", NULL, NULL);
3543 appendPQExpBufferStr(&buf, "ORDER BY 1");
3545 res = PSQLexec(buf.data);
3546 termPQExpBuffer(&buf);
3550 myopt.nullPrint = NULL;
3551 myopt.title = _("List of event triggers");
3552 myopt.translate_header = true;
3553 myopt.translate_columns = translate_columns;
3554 myopt.n_translate_columns = lengthof(translate_columns);
3556 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3568 listCasts(const char *pattern, bool verbose)
3570 PQExpBufferData buf;
3572 printQueryOpt myopt = pset.popt;
3573 static const bool translate_columns[] = {false, false, false, true, false};
3575 initPQExpBuffer(&buf);
3578 * We need a left join to pg_proc for binary casts; the others are just
3579 * paranoia. Also note that we don't attempt to localize '(binary
3580 * coercible)', because there's too much risk of gettext translating a
3581 * function name that happens to match some string in the PO database.
3583 printfPQExpBuffer(&buf,
3584 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3585 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3586 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3589 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3590 " WHEN c.castcontext = 'a' THEN '%s'\n"
3593 gettext_noop("Source type"),
3594 gettext_noop("Target type"),
3595 gettext_noop("Function"),
3597 gettext_noop("in assignment"),
3598 gettext_noop("yes"),
3599 gettext_noop("Implicit?"));
3602 appendPQExpBuffer(&buf,
3603 ",\n d.description AS \"%s\"\n",
3604 gettext_noop("Description"));
3606 appendPQExpBufferStr(&buf,
3607 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3608 " ON c.castfunc = p.oid\n"
3609 " LEFT JOIN pg_catalog.pg_type ts\n"
3610 " ON c.castsource = ts.oid\n"
3611 " LEFT JOIN pg_catalog.pg_namespace ns\n"
3612 " ON ns.oid = ts.typnamespace\n"
3613 " LEFT JOIN pg_catalog.pg_type tt\n"
3614 " ON c.casttarget = tt.oid\n"
3615 " LEFT JOIN pg_catalog.pg_namespace nt\n"
3616 " ON nt.oid = tt.typnamespace\n");
3619 appendPQExpBufferStr(&buf,
3620 " LEFT JOIN pg_catalog.pg_description d\n"
3621 " ON d.classoid = c.tableoid AND d.objoid = "
3622 "c.oid AND d.objsubid = 0\n");
3624 appendPQExpBufferStr(&buf, "WHERE ( (true");
3627 * Match name pattern against either internal or external name of either
3628 * castsource or casttarget
3630 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3631 "ns.nspname", "ts.typname",
3632 "pg_catalog.format_type(ts.oid, NULL)",
3633 "pg_catalog.pg_type_is_visible(ts.oid)");
3635 appendPQExpBufferStr(&buf, ") OR (true");
3637 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3638 "nt.nspname", "tt.typname",
3639 "pg_catalog.format_type(tt.oid, NULL)",
3640 "pg_catalog.pg_type_is_visible(tt.oid)");
3642 appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3644 res = PSQLexec(buf.data);
3645 termPQExpBuffer(&buf);
3649 myopt.nullPrint = NULL;
3650 myopt.title = _("List of casts");
3651 myopt.translate_header = true;
3652 myopt.translate_columns = translate_columns;
3653 myopt.n_translate_columns = lengthof(translate_columns);
3655 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3664 * Describes collations.
3667 listCollations(const char *pattern, bool verbose, bool showSystem)
3669 PQExpBufferData buf;
3671 printQueryOpt myopt = pset.popt;
3672 static const bool translate_columns[] = {false, false, false, false, false};
3674 if (pset.sversion < 90100)
3678 psql_error("The server (version %s) does not support collations.\n",
3679 formatPGVersionNumber(pset.sversion, false,
3680 sverbuf, sizeof(sverbuf)));
3684 initPQExpBuffer(&buf);
3686 printfPQExpBuffer(&buf,
3687 "SELECT n.nspname AS \"%s\",\n"
3688 " c.collname AS \"%s\",\n"
3689 " c.collcollate AS \"%s\",\n"
3690 " c.collctype AS \"%s\"",
3691 gettext_noop("Schema"),
3692 gettext_noop("Name"),
3693 gettext_noop("Collate"),
3694 gettext_noop("Ctype"));
3697 appendPQExpBuffer(&buf,
3698 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3699 gettext_noop("Description"));
3701 appendPQExpBufferStr(&buf,
3702 "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3703 "WHERE n.oid = c.collnamespace\n");
3705 if (!showSystem && !pattern)
3706 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3707 " AND n.nspname <> 'information_schema'\n");
3710 * Hide collations that aren't usable in the current database's encoding.
3711 * If you think to change this, note that pg_collation_is_visible rejects
3712 * unusable collations, so you will need to hack name pattern processing
3713 * somehow to avoid inconsistent behavior.
3715 appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3717 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3718 "n.nspname", "c.collname", NULL,
3719 "pg_catalog.pg_collation_is_visible(c.oid)");
3721 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3723 res = PSQLexec(buf.data);
3724 termPQExpBuffer(&buf);
3728 myopt.nullPrint = NULL;
3729 myopt.title = _("List of collations");
3730 myopt.translate_header = true;
3731 myopt.translate_columns = translate_columns;
3732 myopt.n_translate_columns = lengthof(translate_columns);
3734 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3743 * Describes schemas (namespaces)
3746 listSchemas(const char *pattern, bool verbose, bool showSystem)
3748 PQExpBufferData buf;
3750 printQueryOpt myopt = pset.popt;
3752 initPQExpBuffer(&buf);
3753 printfPQExpBuffer(&buf,
3754 "SELECT n.nspname AS \"%s\",\n"
3755 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3756 gettext_noop("Name"),
3757 gettext_noop("Owner"));
3761 appendPQExpBufferStr(&buf, ",\n ");
3762 printACLColumn(&buf, "n.nspacl");
3763 appendPQExpBuffer(&buf,
3764 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3765 gettext_noop("Description"));
3768 appendPQExpBuffer(&buf,
3769 "\nFROM pg_catalog.pg_namespace n\n");
3771 if (!showSystem && !pattern)
3772 appendPQExpBufferStr(&buf,
3773 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3775 processSQLNamePattern(pset.db, &buf, pattern,
3776 !showSystem && !pattern, false,
3777 NULL, "n.nspname", NULL,
3780 appendPQExpBufferStr(&buf, "ORDER BY 1;");
3782 res = PSQLexec(buf.data);
3783 termPQExpBuffer(&buf);
3787 myopt.nullPrint = NULL;
3788 myopt.title = _("List of schemas");
3789 myopt.translate_header = true;
3791 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3800 * list text search parsers
3803 listTSParsers(const char *pattern, bool verbose)
3805 PQExpBufferData buf;
3807 printQueryOpt myopt = pset.popt;
3809 if (pset.sversion < 80300)
3813 psql_error("The server (version %s) does not support full text search.\n",
3814 formatPGVersionNumber(pset.sversion, false,
3815 sverbuf, sizeof(sverbuf)));
3820 return listTSParsersVerbose(pattern);
3822 initPQExpBuffer(&buf);
3824 printfPQExpBuffer(&buf,
3826 " n.nspname as \"%s\",\n"
3827 " p.prsname as \"%s\",\n"
3828 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3829 "FROM pg_catalog.pg_ts_parser p \n"
3830 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3831 gettext_noop("Schema"),
3832 gettext_noop("Name"),
3833 gettext_noop("Description")
3836 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3837 "n.nspname", "p.prsname", NULL,
3838 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3840 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3842 res = PSQLexec(buf.data);
3843 termPQExpBuffer(&buf);
3847 myopt.nullPrint = NULL;
3848 myopt.title = _("List of text search parsers");
3849 myopt.translate_header = true;
3851 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3858 * full description of parsers
3861 listTSParsersVerbose(const char *pattern)
3863 PQExpBufferData buf;
3867 initPQExpBuffer(&buf);
3869 printfPQExpBuffer(&buf,
3873 "FROM pg_catalog.pg_ts_parser p\n"
3874 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3877 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3878 "n.nspname", "p.prsname", NULL,
3879 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3881 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3883 res = PSQLexec(buf.data);
3884 termPQExpBuffer(&buf);
3888 if (PQntuples(res) == 0)
3891 psql_error("Did not find any text search parser named \"%s\".\n",
3897 for (i = 0; i < PQntuples(res); i++)
3900 const char *nspname = NULL;
3901 const char *prsname;
3903 oid = PQgetvalue(res, i, 0);
3904 if (!PQgetisnull(res, i, 1))
3905 nspname = PQgetvalue(res, i, 1);
3906 prsname = PQgetvalue(res, i, 2);
3908 if (!describeOneTSParser(oid, nspname, prsname))
3926 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3928 PQExpBufferData buf;
3931 printQueryOpt myopt = pset.popt;
3932 static const bool translate_columns[] = {true, false, false};
3934 initPQExpBuffer(&buf);
3936 printfPQExpBuffer(&buf,
3937 "SELECT '%s' AS \"%s\", \n"
3938 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3939 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3940 " FROM pg_catalog.pg_ts_parser p \n"
3941 " WHERE p.oid = '%s' \n"
3944 " p.prstoken::pg_catalog.regproc, \n"
3945 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3946 " FROM pg_catalog.pg_ts_parser p \n"
3947 " WHERE p.oid = '%s' \n"
3950 " p.prsend::pg_catalog.regproc, \n"
3951 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3952 " FROM pg_catalog.pg_ts_parser p \n"
3953 " WHERE p.oid = '%s' \n"
3956 " p.prsheadline::pg_catalog.regproc, \n"
3957 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3958 " FROM pg_catalog.pg_ts_parser p \n"
3959 " WHERE p.oid = '%s' \n"
3962 " p.prslextype::pg_catalog.regproc, \n"
3963 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3964 " FROM pg_catalog.pg_ts_parser p \n"
3965 " WHERE p.oid = '%s';",
3966 gettext_noop("Start parse"),
3967 gettext_noop("Method"),
3968 gettext_noop("Function"),
3969 gettext_noop("Description"),
3971 gettext_noop("Get next token"),
3973 gettext_noop("End parse"),
3975 gettext_noop("Get headline"),
3977 gettext_noop("Get token types"),
3980 res = PSQLexec(buf.data);
3981 termPQExpBuffer(&buf);
3985 myopt.nullPrint = NULL;
3987 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3989 sprintf(title, _("Text search parser \"%s\""), prsname);
3990 myopt.title = title;
3991 myopt.footers = NULL;
3992 myopt.topt.default_footer = false;
3993 myopt.translate_header = true;
3994 myopt.translate_columns = translate_columns;
3995 myopt.n_translate_columns = lengthof(translate_columns);
3997 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4001 initPQExpBuffer(&buf);
4003 printfPQExpBuffer(&buf,
4004 "SELECT t.alias as \"%s\", \n"
4005 " t.description as \"%s\" \n"
4006 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
4008 gettext_noop("Token name"),
4009 gettext_noop("Description"),
4012 res = PSQLexec(buf.data);
4013 termPQExpBuffer(&buf);
4017 myopt.nullPrint = NULL;
4019 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
4021 sprintf(title, _("Token types for parser \"%s\""), prsname);
4022 myopt.title = title;
4023 myopt.footers = NULL;
4024 myopt.topt.default_footer = true;
4025 myopt.translate_header = true;
4026 myopt.translate_columns = NULL;
4027 myopt.n_translate_columns = 0;
4029 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4038 * list text search dictionaries
4041 listTSDictionaries(const char *pattern, bool verbose)
4043 PQExpBufferData buf;
4045 printQueryOpt myopt = pset.popt;
4047 if (pset.sversion < 80300)
4051 psql_error("The server (version %s) does not support full text search.\n",
4052 formatPGVersionNumber(pset.sversion, false,
4053 sverbuf, sizeof(sverbuf)));
4057 initPQExpBuffer(&buf);
4059 printfPQExpBuffer(&buf,
4061 " n.nspname as \"%s\",\n"
4062 " d.dictname as \"%s\",\n",
4063 gettext_noop("Schema"),
4064 gettext_noop("Name"));
4068 appendPQExpBuffer(&buf,
4069 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
4070 " pg_catalog.pg_ts_template t \n"
4071 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
4072 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
4073 " d.dictinitoption as \"%s\", \n",
4074 gettext_noop("Template"),
4075 gettext_noop("Init options"));
4078 appendPQExpBuffer(&buf,
4079 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
4080 gettext_noop("Description"));
4082 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
4083 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
4085 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4086 "n.nspname", "d.dictname", NULL,
4087 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
4089 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4091 res = PSQLexec(buf.data);
4092 termPQExpBuffer(&buf);
4096 myopt.nullPrint = NULL;
4097 myopt.title = _("List of text search dictionaries");
4098 myopt.translate_header = true;
4100 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4109 * list text search templates
4112 listTSTemplates(const char *pattern, bool verbose)
4114 PQExpBufferData buf;
4116 printQueryOpt myopt = pset.popt;
4118 if (pset.sversion < 80300)
4122 psql_error("The server (version %s) does not support full text search.\n",
4123 formatPGVersionNumber(pset.sversion, false,
4124 sverbuf, sizeof(sverbuf)));
4128 initPQExpBuffer(&buf);
4131 printfPQExpBuffer(&buf,
4133 " n.nspname AS \"%s\",\n"
4134 " t.tmplname AS \"%s\",\n"
4135 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
4136 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
4137 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
4138 gettext_noop("Schema"),
4139 gettext_noop("Name"),
4140 gettext_noop("Init"),
4141 gettext_noop("Lexize"),
4142 gettext_noop("Description"));
4144 printfPQExpBuffer(&buf,
4146 " n.nspname AS \"%s\",\n"
4147 " t.tmplname AS \"%s\",\n"
4148 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
4149 gettext_noop("Schema"),
4150 gettext_noop("Name"),
4151 gettext_noop("Description"));
4153 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
4154 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
4156 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4157 "n.nspname", "t.tmplname", NULL,
4158 "pg_catalog.pg_ts_template_is_visible(t.oid)");
4160 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4162 res = PSQLexec(buf.data);
4163 termPQExpBuffer(&buf);
4167 myopt.nullPrint = NULL;
4168 myopt.title = _("List of text search templates");
4169 myopt.translate_header = true;
4171 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4180 * list text search configurations
4183 listTSConfigs(const char *pattern, bool verbose)
4185 PQExpBufferData buf;
4187 printQueryOpt myopt = pset.popt;
4189 if (pset.sversion < 80300)
4193 psql_error("The server (version %s) does not support full text search.\n",
4194 formatPGVersionNumber(pset.sversion, false,
4195 sverbuf, sizeof(sverbuf)));
4200 return listTSConfigsVerbose(pattern);
4202 initPQExpBuffer(&buf);
4204 printfPQExpBuffer(&buf,
4206 " n.nspname as \"%s\",\n"
4207 " c.cfgname as \"%s\",\n"
4208 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
4209 "FROM pg_catalog.pg_ts_config c\n"
4210 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
4211 gettext_noop("Schema"),
4212 gettext_noop("Name"),
4213 gettext_noop("Description")
4216 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4217 "n.nspname", "c.cfgname", NULL,
4218 "pg_catalog.pg_ts_config_is_visible(c.oid)");
4220 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4222 res = PSQLexec(buf.data);
4223 termPQExpBuffer(&buf);
4227 myopt.nullPrint = NULL;
4228 myopt.title = _("List of text search configurations");
4229 myopt.translate_header = true;
4231 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4238 listTSConfigsVerbose(const char *pattern)
4240 PQExpBufferData buf;
4244 initPQExpBuffer(&buf);
4246 printfPQExpBuffer(&buf,
4247 "SELECT c.oid, c.cfgname,\n"
4250 " np.nspname as pnspname \n"
4251 "FROM pg_catalog.pg_ts_config c \n"
4252 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
4253 " pg_catalog.pg_ts_parser p \n"
4254 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
4255 "WHERE p.oid = c.cfgparser\n"
4258 processSQLNamePattern(pset.db, &buf, pattern, true, false,
4259 "n.nspname", "c.cfgname", NULL,
4260 "pg_catalog.pg_ts_config_is_visible(c.oid)");
4262 appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
4264 res = PSQLexec(buf.data);
4265 termPQExpBuffer(&buf);
4269 if (PQntuples(res) == 0)
4272 psql_error("Did not find any text search configuration named \"%s\".\n",
4278 for (i = 0; i < PQntuples(res); i++)
4281 const char *cfgname;
4282 const char *nspname = NULL;
4283 const char *prsname;
4284 const char *pnspname = NULL;
4286 oid = PQgetvalue(res, i, 0);
4287 cfgname = PQgetvalue(res, i, 1);
4288 if (!PQgetisnull(res, i, 2))
4289 nspname = PQgetvalue(res, i, 2);
4290 prsname = PQgetvalue(res, i, 3);
4291 if (!PQgetisnull(res, i, 4))
4292 pnspname = PQgetvalue(res, i, 4);
4294 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
4312 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
4313 const char *pnspname, const char *prsname)
4315 PQExpBufferData buf,
4318 printQueryOpt myopt = pset.popt;
4320 initPQExpBuffer(&buf);
4322 printfPQExpBuffer(&buf,
4324 " ( SELECT t.alias FROM \n"
4325 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
4326 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
4327 " pg_catalog.btrim( \n"
4328 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
4329 " FROM pg_catalog.pg_ts_config_map AS mm \n"
4330 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
4331 " ORDER BY mapcfg, maptokentype, mapseqno \n"
4332 " ) :: pg_catalog.text , \n"
4333 " '{}') AS \"%s\" \n"
4334 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
4335 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
4336 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
4338 gettext_noop("Token"),
4339 gettext_noop("Dictionaries"),
4342 res = PSQLexec(buf.data);
4343 termPQExpBuffer(&buf);
4347 initPQExpBuffer(&title);
4350 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4353 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4357 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4360 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4363 myopt.nullPrint = NULL;
4364 myopt.title = title.data;
4365 myopt.footers = NULL;
4366 myopt.topt.default_footer = false;
4367 myopt.translate_header = true;
4369 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4371 termPQExpBuffer(&title);
4381 * Describes foreign-data wrappers
4384 listForeignDataWrappers(const char *pattern, bool verbose)
4386 PQExpBufferData buf;
4388 printQueryOpt myopt = pset.popt;
4390 if (pset.sversion < 80400)
4394 psql_error("The server (version %s) does not support foreign-data wrappers.\n",
4395 formatPGVersionNumber(pset.sversion, false,
4396 sverbuf, sizeof(sverbuf)));
4400 initPQExpBuffer(&buf);
4401 printfPQExpBuffer(&buf,
4402 "SELECT fdw.fdwname AS \"%s\",\n"
4403 " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
4404 gettext_noop("Name"),
4405 gettext_noop("Owner"));
4406 if (pset.sversion >= 90100)
4407 appendPQExpBuffer(&buf,
4408 " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
4409 gettext_noop("Handler"));
4410 appendPQExpBuffer(&buf,
4411 " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
4412 gettext_noop("Validator"));
4416 appendPQExpBufferStr(&buf, ",\n ");
4417 printACLColumn(&buf, "fdwacl");
4418 appendPQExpBuffer(&buf,
4419 ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
4420 " '(' || array_to_string(ARRAY(SELECT "
4421 " quote_ident(option_name) || ' ' || "
4422 " quote_literal(option_value) FROM "
4423 " pg_options_to_table(fdwoptions)), ', ') || ')' "
4425 gettext_noop("FDW Options"));
4427 if (pset.sversion >= 90100)
4428 appendPQExpBuffer(&buf,
4429 ",\n d.description AS \"%s\" ",
4430 gettext_noop("Description"));
4433 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
4435 if (verbose && pset.sversion >= 90100)
4436 appendPQExpBufferStr(&buf,
4437 "LEFT JOIN pg_catalog.pg_description d\n"
4438 " ON d.classoid = fdw.tableoid "
4439 "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
4441 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4442 NULL, "fdwname", NULL, NULL);
4444 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4446 res = PSQLexec(buf.data);
4447 termPQExpBuffer(&buf);
4451 myopt.nullPrint = NULL;
4452 myopt.title = _("List of foreign-data wrappers");
4453 myopt.translate_header = true;
4455 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4464 * Describes foreign servers.
4467 listForeignServers(const char *pattern, bool verbose)
4469 PQExpBufferData buf;
4471 printQueryOpt myopt = pset.popt;
4473 if (pset.sversion < 80400)
4477 psql_error("The server (version %s) does not support foreign servers.\n",
4478 formatPGVersionNumber(pset.sversion, false,
4479 sverbuf, sizeof(sverbuf)));
4483 initPQExpBuffer(&buf);
4484 printfPQExpBuffer(&buf,
4485 "SELECT s.srvname AS \"%s\",\n"
4486 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
4487 " f.fdwname AS \"%s\"",
4488 gettext_noop("Name"),
4489 gettext_noop("Owner"),
4490 gettext_noop("Foreign-data wrapper"));
4494 appendPQExpBufferStr(&buf, ",\n ");
4495 printACLColumn(&buf, "s.srvacl");
4496 appendPQExpBuffer(&buf,
4498 " s.srvtype AS \"%s\",\n"
4499 " s.srvversion AS \"%s\",\n"
4500 " CASE WHEN srvoptions IS NULL THEN '' ELSE "
4501 " '(' || array_to_string(ARRAY(SELECT "
4502 " quote_ident(option_name) || ' ' || "
4503 " quote_literal(option_value) FROM "
4504 " pg_options_to_table(srvoptions)), ', ') || ')' "
4506 " d.description AS \"%s\"",
4507 gettext_noop("Type"),
4508 gettext_noop("Version"),
4509 gettext_noop("FDW Options"),
4510 gettext_noop("Description"));
4513 appendPQExpBufferStr(&buf,
4514 "\nFROM pg_catalog.pg_foreign_server s\n"
4515 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
4518 appendPQExpBufferStr(&buf,
4519 "LEFT JOIN pg_description d\n "
4520 "ON d.classoid = s.tableoid AND d.objoid = s.oid "
4521 "AND d.objsubid = 0\n");
4523 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4524 NULL, "s.srvname", NULL, NULL);
4526 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4528 res = PSQLexec(buf.data);
4529 termPQExpBuffer(&buf);
4533 myopt.nullPrint = NULL;
4534 myopt.title = _("List of foreign servers");
4535 myopt.translate_header = true;
4537 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4546 * Describes user mappings.
4549 listUserMappings(const char *pattern, bool verbose)
4551 PQExpBufferData buf;
4553 printQueryOpt myopt = pset.popt;
4555 if (pset.sversion < 80400)
4559 psql_error("The server (version %s) does not support user mappings.\n",
4560 formatPGVersionNumber(pset.sversion, false,
4561 sverbuf, sizeof(sverbuf)));
4565 initPQExpBuffer(&buf);
4566 printfPQExpBuffer(&buf,
4567 "SELECT um.srvname AS \"%s\",\n"
4568 " um.usename AS \"%s\"",
4569 gettext_noop("Server"),
4570 gettext_noop("User name"));
4573 appendPQExpBuffer(&buf,
4574 ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4575 " '(' || array_to_string(ARRAY(SELECT "
4576 " quote_ident(option_name) || ' ' || "
4577 " quote_literal(option_value) FROM "
4578 " pg_options_to_table(umoptions)), ', ') || ')' "
4580 gettext_noop("FDW Options"));
4582 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4584 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4585 NULL, "um.srvname", "um.usename", NULL);
4587 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4589 res = PSQLexec(buf.data);
4590 termPQExpBuffer(&buf);
4594 myopt.nullPrint = NULL;
4595 myopt.title = _("List of user mappings");
4596 myopt.translate_header = true;
4598 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4607 * Describes foreign tables.
4610 listForeignTables(const char *pattern, bool verbose)
4612 PQExpBufferData buf;
4614 printQueryOpt myopt = pset.popt;
4616 if (pset.sversion < 90100)
4620 psql_error("The server (version %s) does not support foreign tables.\n",
4621 formatPGVersionNumber(pset.sversion, false,
4622 sverbuf, sizeof(sverbuf)));
4626 initPQExpBuffer(&buf);
4627 printfPQExpBuffer(&buf,
4628 "SELECT n.nspname AS \"%s\",\n"
4629 " c.relname AS \"%s\",\n"
4630 " s.srvname AS \"%s\"",
4631 gettext_noop("Schema"),
4632 gettext_noop("Table"),
4633 gettext_noop("Server"));
4636 appendPQExpBuffer(&buf,
4637 ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4638 " '(' || array_to_string(ARRAY(SELECT "
4639 " quote_ident(option_name) || ' ' || "
4640 " quote_literal(option_value) FROM "
4641 " pg_options_to_table(ftoptions)), ', ') || ')' "
4643 " d.description AS \"%s\"",
4644 gettext_noop("FDW Options"),
4645 gettext_noop("Description"));
4647 appendPQExpBufferStr(&buf,
4648 "\nFROM pg_catalog.pg_foreign_table ft\n"
4649 " INNER JOIN pg_catalog.pg_class c"
4650 " ON c.oid = ft.ftrelid\n"
4651 " INNER JOIN pg_catalog.pg_namespace n"
4652 " ON n.oid = c.relnamespace\n"
4653 " INNER JOIN pg_catalog.pg_foreign_server s"
4654 " ON s.oid = ft.ftserver\n");
4656 appendPQExpBufferStr(&buf,
4657 " LEFT JOIN pg_catalog.pg_description d\n"
4658 " ON d.classoid = c.tableoid AND "
4659 "d.objoid = c.oid AND d.objsubid = 0\n");
4661 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4662 "n.nspname", "c.relname", NULL,
4663 "pg_catalog.pg_table_is_visible(c.oid)");
4665 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4667 res = PSQLexec(buf.data);
4668 termPQExpBuffer(&buf);
4672 myopt.nullPrint = NULL;
4673 myopt.title = _("List of foreign tables");
4674 myopt.translate_header = true;
4676 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4685 * Briefly describes installed extensions.
4688 listExtensions(const char *pattern)
4690 PQExpBufferData buf;
4692 printQueryOpt myopt = pset.popt;
4694 if (pset.sversion < 90100)
4698 psql_error("The server (version %s) does not support extensions.\n",
4699 formatPGVersionNumber(pset.sversion, false,
4700 sverbuf, sizeof(sverbuf)));
4704 initPQExpBuffer(&buf);
4705 printfPQExpBuffer(&buf,
4706 "SELECT e.extname AS \"%s\", "
4707 "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4708 "FROM pg_catalog.pg_extension e "
4709 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4710 "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4711 "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4712 gettext_noop("Name"),
4713 gettext_noop("Version"),
4714 gettext_noop("Schema"),
4715 gettext_noop("Description"));
4717 processSQLNamePattern(pset.db, &buf, pattern,
4719 NULL, "e.extname", NULL,
4722 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4724 res = PSQLexec(buf.data);
4725 termPQExpBuffer(&buf);
4729 myopt.nullPrint = NULL;
4730 myopt.title = _("List of installed extensions");
4731 myopt.translate_header = true;
4733 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4742 * List contents of installed extensions.
4745 listExtensionContents(const char *pattern)
4747 PQExpBufferData buf;
4751 if (pset.sversion < 90100)
4755 psql_error("The server (version %s) does not support extensions.\n",
4756 formatPGVersionNumber(pset.sversion, false,
4757 sverbuf, sizeof(sverbuf)));
4761 initPQExpBuffer(&buf);
4762 printfPQExpBuffer(&buf,
4763 "SELECT e.extname, e.oid\n"
4764 "FROM pg_catalog.pg_extension e\n");
4766 processSQLNamePattern(pset.db, &buf, pattern,
4768 NULL, "e.extname", NULL,
4771 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4773 res = PSQLexec(buf.data);
4774 termPQExpBuffer(&buf);
4778 if (PQntuples(res) == 0)
4783 psql_error("Did not find any extension named \"%s\".\n",
4786 psql_error("Did not find any extensions.\n");
4792 for (i = 0; i < PQntuples(res); i++)
4794 const char *extname;
4797 extname = PQgetvalue(res, i, 0);
4798 oid = PQgetvalue(res, i, 1);
4800 if (!listOneExtensionContents(extname, oid))
4817 listOneExtensionContents(const char *extname, const char *oid)
4819 PQExpBufferData buf;
4822 printQueryOpt myopt = pset.popt;
4824 initPQExpBuffer(&buf);
4825 printfPQExpBuffer(&buf,
4826 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4827 "FROM pg_catalog.pg_depend\n"
4828 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4830 gettext_noop("Object Description"),
4833 res = PSQLexec(buf.data);
4834 termPQExpBuffer(&buf);
4838 myopt.nullPrint = NULL;
4839 snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4840 myopt.title = title;
4841 myopt.translate_header = true;
4843 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4852 * Helper function for consistently formatting ACL (privilege) columns.
4853 * The proper targetlist entry is appended to buf. Note lack of any
4854 * whitespace or comma decoration.
4857 printACLColumn(PQExpBuffer buf, const char *colname)
4859 if (pset.sversion >= 80100)
4860 appendPQExpBuffer(buf,
4861 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4862 colname, gettext_noop("Access privileges"));
4864 appendPQExpBuffer(buf,
4865 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4866 colname, gettext_noop("Access privileges"));