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-2014, PostgreSQL Global Development Group
11 * src/bin/psql/describe.c
13 #include "postgres_fe.h"
17 #include "catalog/pg_default_acl.h"
21 #include "dumputils.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, false);
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, pset.logfile);
133 * Takes an optional regexp to select particular tablespaces
136 describeTablespaces(const char *pattern, bool verbose)
140 printQueryOpt myopt = pset.popt;
142 if (pset.sversion < 80000)
144 psql_error("The server (version %d.%d) does not support tablespaces.\n",
145 pset.sversion / 10000, (pset.sversion / 100) % 100);
149 initPQExpBuffer(&buf);
151 if (pset.sversion >= 90200)
152 printfPQExpBuffer(&buf,
153 "SELECT spcname AS \"%s\",\n"
154 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
155 " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
156 gettext_noop("Name"),
157 gettext_noop("Owner"),
158 gettext_noop("Location"));
160 printfPQExpBuffer(&buf,
161 "SELECT spcname AS \"%s\",\n"
162 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
163 " spclocation AS \"%s\"",
164 gettext_noop("Name"),
165 gettext_noop("Owner"),
166 gettext_noop("Location"));
170 appendPQExpBufferStr(&buf, ",\n ");
171 printACLColumn(&buf, "spcacl");
174 if (verbose && pset.sversion >= 80200)
175 appendPQExpBuffer(&buf,
176 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
177 gettext_noop("Description"));
179 appendPQExpBufferStr(&buf,
180 "\nFROM pg_catalog.pg_tablespace\n");
182 processSQLNamePattern(pset.db, &buf, pattern, false, false,
183 NULL, "spcname", NULL,
186 appendPQExpBufferStr(&buf, "ORDER BY 1;");
188 res = PSQLexec(buf.data, false);
189 termPQExpBuffer(&buf);
193 myopt.nullPrint = NULL;
194 myopt.title = _("List of tablespaces");
195 myopt.translate_header = true;
197 printQuery(res, &myopt, pset.queryFout, pset.logfile);
205 * Takes an optional regexp to select particular functions.
207 * As with \d, you can specify the kinds of functions you want:
214 * and you can mix and match these in any order.
217 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
219 bool showAggregate = strchr(functypes, 'a') != NULL;
220 bool showNormal = strchr(functypes, 'n') != NULL;
221 bool showTrigger = strchr(functypes, 't') != NULL;
222 bool showWindow = strchr(functypes, 'w') != NULL;
226 printQueryOpt myopt = pset.popt;
227 static const bool translate_columns[] = {false, false, false, false, true, true, true, false, false, false, false};
229 if (strlen(functypes) != strspn(functypes, "antwS+"))
231 psql_error("\\df only takes [antwS+] as options\n");
235 if (showWindow && pset.sversion < 80400)
237 psql_error("\\df does not take a \"w\" option with server version %d.%d\n",
238 pset.sversion / 10000, (pset.sversion / 100) % 100);
242 if (!showAggregate && !showNormal && !showTrigger && !showWindow)
244 showAggregate = showNormal = showTrigger = true;
245 if (pset.sversion >= 80400)
249 initPQExpBuffer(&buf);
251 printfPQExpBuffer(&buf,
252 "SELECT n.nspname as \"%s\",\n"
253 " p.proname as \"%s\",\n",
254 gettext_noop("Schema"),
255 gettext_noop("Name"));
257 if (pset.sversion >= 80400)
258 appendPQExpBuffer(&buf,
259 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
260 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
262 " WHEN p.proisagg THEN '%s'\n"
263 " WHEN p.proiswindow THEN '%s'\n"
264 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
267 gettext_noop("Result data type"),
268 gettext_noop("Argument data types"),
269 /* translator: "agg" is short for "aggregate" */
271 gettext_noop("window"),
272 gettext_noop("trigger"),
273 gettext_noop("normal"),
274 gettext_noop("Type"));
275 else if (pset.sversion >= 80100)
276 appendPQExpBuffer(&buf,
277 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
278 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
279 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
280 " pg_catalog.array_to_string(ARRAY(\n"
283 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
284 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
285 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
286 " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
289 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
290 " ELSE p.proargnames[s.i] || ' ' \n"
292 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
294 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
297 " pg_catalog.array_to_string(ARRAY(\n"
300 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
301 " ELSE p.proargnames[s.i+1] || ' '\n"
303 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
305 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
309 " WHEN p.proisagg THEN '%s'\n"
310 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
313 gettext_noop("Result data type"),
314 gettext_noop("Argument data types"),
315 /* translator: "agg" is short for "aggregate" */
317 gettext_noop("trigger"),
318 gettext_noop("normal"),
319 gettext_noop("Type"));
321 appendPQExpBuffer(&buf,
322 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
323 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
324 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
326 " WHEN p.proisagg THEN '%s'\n"
327 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
330 gettext_noop("Result data type"),
331 gettext_noop("Argument data types"),
332 /* translator: "agg" is short for "aggregate" */
334 gettext_noop("trigger"),
335 gettext_noop("normal"),
336 gettext_noop("Type"));
339 appendPQExpBuffer(&buf,
340 ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\""
342 " WHEN p.provolatile = 'i' THEN '%s'\n"
343 " WHEN p.provolatile = 's' THEN '%s'\n"
344 " WHEN p.provolatile = 'v' THEN '%s'\n"
346 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
347 " l.lanname as \"%s\",\n"
348 " p.prosrc as \"%s\",\n"
349 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
350 gettext_noop("definer"),
351 gettext_noop("invoker"),
352 gettext_noop("Security"),
353 gettext_noop("immutable"),
354 gettext_noop("stable"),
355 gettext_noop("volatile"),
356 gettext_noop("Volatility"),
357 gettext_noop("Owner"),
358 gettext_noop("Language"),
359 gettext_noop("Source code"),
360 gettext_noop("Description"));
362 appendPQExpBufferStr(&buf,
363 "\nFROM pg_catalog.pg_proc p"
364 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
367 appendPQExpBufferStr(&buf,
368 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
372 /* filter by function type, if requested */
373 if (showNormal && showAggregate && showTrigger && showWindow)
380 appendPQExpBufferStr(&buf, " AND ");
383 appendPQExpBufferStr(&buf, "WHERE ");
386 appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
391 appendPQExpBufferStr(&buf, " AND ");
394 appendPQExpBufferStr(&buf, "WHERE ");
397 appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
399 if (!showWindow && pset.sversion >= 80400)
402 appendPQExpBufferStr(&buf, " AND ");
405 appendPQExpBufferStr(&buf, "WHERE ");
408 appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
413 bool needs_or = false;
415 appendPQExpBufferStr(&buf, "WHERE (\n ");
417 /* Note: at least one of these must be true ... */
420 appendPQExpBufferStr(&buf, "p.proisagg\n");
426 appendPQExpBufferStr(&buf, " OR ");
427 appendPQExpBufferStr(&buf,
428 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
434 appendPQExpBufferStr(&buf, " OR ");
435 appendPQExpBufferStr(&buf, "p.proiswindow\n");
438 appendPQExpBufferStr(&buf, " )\n");
441 processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
442 "n.nspname", "p.proname", NULL,
443 "pg_catalog.pg_function_is_visible(p.oid)");
445 if (!showSystem && !pattern)
446 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
447 " AND n.nspname <> 'information_schema'\n");
449 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
451 res = PSQLexec(buf.data, false);
452 termPQExpBuffer(&buf);
456 myopt.nullPrint = NULL;
457 myopt.title = _("List of functions");
458 myopt.translate_header = true;
459 myopt.translate_columns = translate_columns;
460 myopt.n_translate_columns = lengthof(translate_columns);
462 printQuery(res, &myopt, pset.queryFout, pset.logfile);
475 describeTypes(const char *pattern, bool verbose, bool showSystem)
479 printQueryOpt myopt = pset.popt;
481 initPQExpBuffer(&buf);
483 printfPQExpBuffer(&buf,
484 "SELECT n.nspname as \"%s\",\n"
485 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
486 gettext_noop("Schema"),
487 gettext_noop("Name"));
489 appendPQExpBuffer(&buf,
490 " t.typname AS \"%s\",\n"
491 " CASE WHEN t.typrelid != 0\n"
492 " THEN CAST('tuple' AS pg_catalog.text)\n"
493 " WHEN t.typlen < 0\n"
494 " THEN CAST('var' AS pg_catalog.text)\n"
495 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
497 gettext_noop("Internal name"),
498 gettext_noop("Size"));
499 if (verbose && pset.sversion >= 80300)
501 appendPQExpBufferStr(&buf,
502 " pg_catalog.array_to_string(\n"
504 " SELECT e.enumlabel\n"
505 " FROM pg_catalog.pg_enum e\n"
506 " WHERE e.enumtypid = t.oid\n");
508 if (pset.sversion >= 90100)
509 appendPQExpBufferStr(&buf,
510 " ORDER BY e.enumsortorder\n");
512 appendPQExpBufferStr(&buf,
513 " ORDER BY e.oid\n");
515 appendPQExpBuffer(&buf,
519 gettext_noop("Elements"));
521 if (verbose && pset.sversion >= 90200)
523 printACLColumn(&buf, "t.typacl");
524 appendPQExpBufferStr(&buf, ",\n ");
527 appendPQExpBuffer(&buf,
528 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
529 gettext_noop("Description"));
531 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
532 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
535 * do not include complex types (typrelid!=0) unless they are standalone
538 appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
539 appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
540 "WHERE c.oid = t.typrelid))\n");
543 * do not include array types (before 8.3 we have to use the assumption
544 * that their names start with underscore)
546 if (pset.sversion >= 80300)
547 appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
549 appendPQExpBufferStr(&buf, " AND t.typname !~ '^_'\n");
551 if (!showSystem && !pattern)
552 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
553 " AND n.nspname <> 'information_schema'\n");
555 /* Match name pattern against either internal or external name */
556 processSQLNamePattern(pset.db, &buf, pattern, true, false,
557 "n.nspname", "t.typname",
558 "pg_catalog.format_type(t.oid, NULL)",
559 "pg_catalog.pg_type_is_visible(t.oid)");
561 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
563 res = PSQLexec(buf.data, false);
564 termPQExpBuffer(&buf);
568 myopt.nullPrint = NULL;
569 myopt.title = _("List of data types");
570 myopt.translate_header = true;
572 printQuery(res, &myopt, pset.queryFout, pset.logfile);
583 describeOperators(const char *pattern, bool verbose, bool showSystem)
587 printQueryOpt myopt = pset.popt;
589 initPQExpBuffer(&buf);
592 * Note: before Postgres 9.1, we did not assign comments to any built-in
593 * operators, preferring to let the comment on the underlying function
594 * suffice. The coalesce() on the obj_description() calls below supports
595 * this convention by providing a fallback lookup of a comment on the
596 * operator's function. As of 9.1 there is a policy that every built-in
597 * operator should have a comment; so the coalesce() is no longer
598 * necessary so far as built-in operators are concerned. We keep it
599 * anyway, for now, because (1) third-party modules may still be following
600 * the old convention, and (2) we'd need to do it anyway when talking to a
604 printfPQExpBuffer(&buf,
605 "SELECT n.nspname as \"%s\",\n"
606 " o.oprname AS \"%s\",\n"
607 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
608 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
609 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
610 gettext_noop("Schema"),
611 gettext_noop("Name"),
612 gettext_noop("Left arg type"),
613 gettext_noop("Right arg type"),
614 gettext_noop("Result type"));
617 appendPQExpBuffer(&buf,
618 " o.oprcode AS \"%s\",\n",
619 gettext_noop("Function"));
621 appendPQExpBuffer(&buf,
622 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
623 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
624 "FROM pg_catalog.pg_operator o\n"
625 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
626 gettext_noop("Description"));
628 if (!showSystem && !pattern)
629 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
630 " AND n.nspname <> 'information_schema'\n");
632 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
633 "n.nspname", "o.oprname", NULL,
634 "pg_catalog.pg_operator_is_visible(o.oid)");
636 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
638 res = PSQLexec(buf.data, false);
639 termPQExpBuffer(&buf);
643 myopt.nullPrint = NULL;
644 myopt.title = _("List of operators");
645 myopt.translate_header = true;
647 printQuery(res, &myopt, pset.queryFout, pset.logfile);
657 * for \l, \list, and -l switch
660 listAllDbs(const char *pattern, bool verbose)
664 printQueryOpt myopt = pset.popt;
666 initPQExpBuffer(&buf);
668 printfPQExpBuffer(&buf,
669 "SELECT d.datname as \"%s\",\n"
670 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
671 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
672 gettext_noop("Name"),
673 gettext_noop("Owner"),
674 gettext_noop("Encoding"));
675 if (pset.sversion >= 80400)
676 appendPQExpBuffer(&buf,
677 " d.datcollate as \"%s\",\n"
678 " d.datctype as \"%s\",\n",
679 gettext_noop("Collate"),
680 gettext_noop("Ctype"));
681 appendPQExpBufferStr(&buf, " ");
682 printACLColumn(&buf, "d.datacl");
683 if (verbose && pset.sversion >= 80200)
684 appendPQExpBuffer(&buf,
685 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
686 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
687 " ELSE 'No Access'\n"
689 gettext_noop("Size"));
690 if (verbose && pset.sversion >= 80000)
691 appendPQExpBuffer(&buf,
692 ",\n t.spcname as \"%s\"",
693 gettext_noop("Tablespace"));
694 if (verbose && pset.sversion >= 80200)
695 appendPQExpBuffer(&buf,
696 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
697 gettext_noop("Description"));
698 appendPQExpBufferStr(&buf,
699 "\nFROM pg_catalog.pg_database d\n");
700 if (verbose && pset.sversion >= 80000)
701 appendPQExpBufferStr(&buf,
702 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
705 processSQLNamePattern(pset.db, &buf, pattern, false, false,
706 NULL, "d.datname", NULL, NULL);
708 appendPQExpBufferStr(&buf, "ORDER BY 1;");
709 res = PSQLexec(buf.data, false);
710 termPQExpBuffer(&buf);
714 myopt.nullPrint = NULL;
715 myopt.title = _("List of databases");
716 myopt.translate_header = true;
718 printQuery(res, &myopt, pset.queryFout, pset.logfile);
726 * List Tables' Grant/Revoke Permissions
727 * \z (now also \dp -- perhaps more mnemonic)
730 permissionsList(const char *pattern)
734 printQueryOpt myopt = pset.popt;
735 static const bool translate_columns[] = {false, false, true, false, false};
737 initPQExpBuffer(&buf);
740 * we ignore indexes and toast tables since they have no meaningful rights
742 printfPQExpBuffer(&buf,
743 "SELECT n.nspname as \"%s\",\n"
744 " c.relname as \"%s\",\n"
746 " WHEN 'r' THEN '%s'"
747 " WHEN 'v' THEN '%s'"
748 " WHEN 'm' THEN '%s'"
749 " WHEN 'S' THEN '%s'"
750 " WHEN 'f' THEN '%s'"
753 gettext_noop("Schema"),
754 gettext_noop("Name"),
755 gettext_noop("table"),
756 gettext_noop("view"),
757 gettext_noop("materialized view"),
758 gettext_noop("sequence"),
759 gettext_noop("foreign table"),
760 gettext_noop("Type"));
762 printACLColumn(&buf, "c.relacl");
764 if (pset.sversion >= 80400)
765 appendPQExpBuffer(&buf,
766 ",\n pg_catalog.array_to_string(ARRAY(\n"
767 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
768 " FROM pg_catalog.pg_attribute a\n"
769 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
770 " ), E'\\n') AS \"%s\"",
771 gettext_noop("Column access privileges"));
773 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
774 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
775 "WHERE c.relkind IN ('r', 'v', 'm', 'S', 'f')\n");
778 * Unless a schema pattern is specified, we suppress system and temp
779 * tables, since they normally aren't very interesting from a permissions
780 * point of view. You can see 'em by explicit request though, eg with \z
783 processSQLNamePattern(pset.db, &buf, pattern, true, false,
784 "n.nspname", "c.relname", NULL,
785 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
787 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
789 res = PSQLexec(buf.data, false);
792 termPQExpBuffer(&buf);
796 myopt.nullPrint = NULL;
797 printfPQExpBuffer(&buf, _("Access privileges"));
798 myopt.title = buf.data;
799 myopt.translate_header = true;
800 myopt.translate_columns = translate_columns;
801 myopt.n_translate_columns = lengthof(translate_columns);
803 printQuery(res, &myopt, pset.queryFout, pset.logfile);
805 termPQExpBuffer(&buf);
814 * List Default ACLs. The pattern can match either schema or role name.
817 listDefaultACLs(const char *pattern)
821 printQueryOpt myopt = pset.popt;
822 static const bool translate_columns[] = {false, false, true, false};
824 if (pset.sversion < 90000)
826 psql_error("The server (version %d.%d) does not support altering default privileges.\n",
827 pset.sversion / 10000, (pset.sversion / 100) % 100);
831 initPQExpBuffer(&buf);
833 printfPQExpBuffer(&buf,
834 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
835 " n.nspname AS \"%s\",\n"
836 " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
838 gettext_noop("Owner"),
839 gettext_noop("Schema"),
841 gettext_noop("table"),
843 gettext_noop("sequence"),
845 gettext_noop("function"),
847 gettext_noop("type"),
848 gettext_noop("Type"));
850 printACLColumn(&buf, "d.defaclacl");
852 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
853 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
855 processSQLNamePattern(pset.db, &buf, pattern, false, false,
858 "pg_catalog.pg_get_userbyid(d.defaclrole)",
861 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
863 res = PSQLexec(buf.data, false);
866 termPQExpBuffer(&buf);
870 myopt.nullPrint = NULL;
871 printfPQExpBuffer(&buf, _("Default access privileges"));
872 myopt.title = buf.data;
873 myopt.translate_header = true;
874 myopt.translate_columns = translate_columns;
875 myopt.n_translate_columns = lengthof(translate_columns);
877 printQuery(res, &myopt, pset.queryFout, pset.logfile);
879 termPQExpBuffer(&buf);
886 * Get object comments
890 * Note: This command only lists comments for object types which do not have
891 * their comments displayed by their own backslash commands. The following
892 * types of objects will be displayed: constraint, operator class,
893 * operator family, rule, and trigger.
897 objectDescription(const char *pattern, bool showSystem)
901 printQueryOpt myopt = pset.popt;
902 static const bool translate_columns[] = {false, false, true, false};
904 initPQExpBuffer(&buf);
906 appendPQExpBuffer(&buf,
907 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
909 gettext_noop("Schema"),
910 gettext_noop("Name"),
911 gettext_noop("Object"),
912 gettext_noop("Description"));
914 /* Constraint descriptions */
915 appendPQExpBuffer(&buf,
916 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
917 " n.nspname as nspname,\n"
918 " CAST(pgc.conname AS pg_catalog.text) as name,"
919 " CAST('%s' AS pg_catalog.text) as object\n"
920 " FROM pg_catalog.pg_constraint pgc\n"
921 " JOIN pg_catalog.pg_class c "
922 "ON c.oid = pgc.conrelid\n"
923 " LEFT JOIN pg_catalog.pg_namespace n "
924 " ON n.oid = c.relnamespace\n",
925 gettext_noop("constraint"));
927 if (!showSystem && !pattern)
928 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
929 " AND n.nspname <> 'information_schema'\n");
931 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
932 false, "n.nspname", "pgc.conname", NULL,
933 "pg_catalog.pg_table_is_visible(c.oid)");
936 * pg_opclass.opcmethod only available in 8.3+
938 if (pset.sversion >= 80300)
940 /* Operator class descriptions */
941 appendPQExpBuffer(&buf,
943 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
944 " n.nspname as nspname,\n"
945 " CAST(o.opcname AS pg_catalog.text) as name,\n"
946 " CAST('%s' AS pg_catalog.text) as object\n"
947 " FROM pg_catalog.pg_opclass o\n"
948 " JOIN pg_catalog.pg_am am ON "
949 "o.opcmethod = am.oid\n"
950 " JOIN pg_catalog.pg_namespace n ON "
951 "n.oid = o.opcnamespace\n",
952 gettext_noop("operator class"));
954 if (!showSystem && !pattern)
955 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
956 " AND n.nspname <> 'information_schema'\n");
958 processSQLNamePattern(pset.db, &buf, pattern, true, false,
959 "n.nspname", "o.opcname", NULL,
960 "pg_catalog.pg_opclass_is_visible(o.oid)");
964 * although operator family comments have been around since 8.3,
965 * pg_opfamily_is_visible is only available in 9.2+
967 if (pset.sversion >= 90200)
969 /* Operator family descriptions */
970 appendPQExpBuffer(&buf,
972 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
973 " n.nspname as nspname,\n"
974 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
975 " CAST('%s' AS pg_catalog.text) as object\n"
976 " FROM pg_catalog.pg_opfamily opf\n"
977 " JOIN pg_catalog.pg_am am "
978 "ON opf.opfmethod = am.oid\n"
979 " JOIN pg_catalog.pg_namespace n "
980 "ON opf.opfnamespace = n.oid\n",
981 gettext_noop("operator family"));
983 if (!showSystem && !pattern)
984 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
985 " AND n.nspname <> 'information_schema'\n");
987 processSQLNamePattern(pset.db, &buf, pattern, true, false,
988 "n.nspname", "opf.opfname", NULL,
989 "pg_catalog.pg_opfamily_is_visible(opf.oid)");
992 /* Rule descriptions (ignore rules for views) */
993 appendPQExpBuffer(&buf,
995 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
996 " n.nspname as nspname,\n"
997 " CAST(r.rulename AS pg_catalog.text) as name,"
998 " CAST('%s' AS pg_catalog.text) as object\n"
999 " FROM pg_catalog.pg_rewrite r\n"
1000 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1001 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1002 " WHERE r.rulename != '_RETURN'\n",
1003 gettext_noop("rule"));
1005 if (!showSystem && !pattern)
1006 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1007 " AND n.nspname <> 'information_schema'\n");
1009 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1010 "n.nspname", "r.rulename", NULL,
1011 "pg_catalog.pg_table_is_visible(c.oid)");
1013 /* Trigger descriptions */
1014 appendPQExpBuffer(&buf,
1016 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1017 " n.nspname as nspname,\n"
1018 " CAST(t.tgname AS pg_catalog.text) as name,"
1019 " CAST('%s' AS pg_catalog.text) as object\n"
1020 " FROM pg_catalog.pg_trigger t\n"
1021 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1022 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1023 gettext_noop("trigger"));
1025 if (!showSystem && !pattern)
1026 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1027 " AND n.nspname <> 'information_schema'\n");
1029 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1030 "n.nspname", "t.tgname", NULL,
1031 "pg_catalog.pg_table_is_visible(c.oid)");
1033 appendPQExpBufferStr(&buf,
1035 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1037 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1039 res = PSQLexec(buf.data, false);
1040 termPQExpBuffer(&buf);
1044 myopt.nullPrint = NULL;
1045 myopt.title = _("Object descriptions");
1046 myopt.translate_header = true;
1047 myopt.translate_columns = translate_columns;
1048 myopt.n_translate_columns = lengthof(translate_columns);
1050 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1058 * describeTableDetails (for \d)
1060 * This routine finds the tables to be displayed, and calls
1061 * describeOneTableDetails for each one.
1063 * verbose: if true, this is \d+
1066 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1068 PQExpBufferData buf;
1072 initPQExpBuffer(&buf);
1074 printfPQExpBuffer(&buf,
1078 "FROM pg_catalog.pg_class c\n"
1079 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1081 if (!showSystem && !pattern)
1082 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1083 " AND n.nspname <> 'information_schema'\n");
1085 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1086 "n.nspname", "c.relname", NULL,
1087 "pg_catalog.pg_table_is_visible(c.oid)");
1089 appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1091 res = PSQLexec(buf.data, false);
1092 termPQExpBuffer(&buf);
1096 if (PQntuples(res) == 0)
1099 psql_error("Did not find any relation named \"%s\".\n",
1105 for (i = 0; i < PQntuples(res); i++)
1108 const char *nspname;
1109 const char *relname;
1111 oid = PQgetvalue(res, i, 0);
1112 nspname = PQgetvalue(res, i, 1);
1113 relname = PQgetvalue(res, i, 2);
1115 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1132 * describeOneTableDetails (for \d)
1134 * Unfortunately, the information presented here is so complicated that it
1135 * cannot be done in a single query. So we have to assemble the printed table
1136 * by hand and pass it to the underlying printTable() function.
1139 describeOneTableDetails(const char *schemaname,
1140 const char *relationname,
1144 PQExpBufferData buf;
1145 PGresult *res = NULL;
1146 printTableOpt myopt = pset.popt.topt;
1147 printTableContent cont;
1148 bool printTableInitialized = false;
1150 char *view_def = NULL;
1152 char **seq_values = NULL;
1153 char **modifiers = NULL;
1155 PQExpBufferData title;
1156 PQExpBufferData tmpbuf;
1170 char relpersistence;
1173 bool show_modifiers = false;
1178 myopt.default_footer = false;
1179 /* This output looks confusing in expanded mode. */
1180 myopt.expanded = false;
1182 initPQExpBuffer(&buf);
1183 initPQExpBuffer(&title);
1184 initPQExpBuffer(&tmpbuf);
1186 /* Get general table info */
1187 if (pset.sversion >= 90400)
1189 printfPQExpBuffer(&buf,
1190 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1191 "c.relhastriggers, c.relhasoids, "
1192 "%s, c.reltablespace, "
1193 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1194 "c.relpersistence, c.relreplident\n"
1195 "FROM pg_catalog.pg_class c\n "
1196 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1197 "WHERE c.oid = '%s';",
1199 "pg_catalog.array_to_string(c.reloptions || "
1200 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1204 else if (pset.sversion >= 90100)
1206 printfPQExpBuffer(&buf,
1207 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1208 "c.relhastriggers, c.relhasoids, "
1209 "%s, c.reltablespace, "
1210 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1211 "c.relpersistence\n"
1212 "FROM pg_catalog.pg_class c\n "
1213 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1214 "WHERE c.oid = '%s';",
1216 "pg_catalog.array_to_string(c.reloptions || "
1217 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1221 else if (pset.sversion >= 90000)
1223 printfPQExpBuffer(&buf,
1224 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1225 "c.relhastriggers, c.relhasoids, "
1226 "%s, c.reltablespace, "
1227 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1228 "FROM pg_catalog.pg_class c\n "
1229 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1230 "WHERE c.oid = '%s';",
1232 "pg_catalog.array_to_string(c.reloptions || "
1233 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1237 else if (pset.sversion >= 80400)
1239 printfPQExpBuffer(&buf,
1240 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1241 "c.relhastriggers, c.relhasoids, "
1242 "%s, c.reltablespace\n"
1243 "FROM pg_catalog.pg_class c\n "
1244 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1245 "WHERE c.oid = '%s';",
1247 "pg_catalog.array_to_string(c.reloptions || "
1248 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1252 else if (pset.sversion >= 80200)
1254 printfPQExpBuffer(&buf,
1255 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1256 "reltriggers <> 0, relhasoids, "
1257 "%s, reltablespace\n"
1258 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1260 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1263 else if (pset.sversion >= 80000)
1265 printfPQExpBuffer(&buf,
1266 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1267 "reltriggers <> 0, relhasoids, "
1268 "'', reltablespace\n"
1269 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1274 printfPQExpBuffer(&buf,
1275 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1276 "reltriggers <> 0, relhasoids, "
1278 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1282 res = PSQLexec(buf.data, false);
1286 /* Did we get anything? */
1287 if (PQntuples(res) == 0)
1290 psql_error("Did not find any relation with OID %s.\n", oid);
1294 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1295 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1296 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1297 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1298 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1299 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1300 tableinfo.reloptions = (pset.sversion >= 80200) ?
1301 pg_strdup(PQgetvalue(res, 0, 6)) : NULL;
1302 tableinfo.tablespace = (pset.sversion >= 80000) ?
1303 atooid(PQgetvalue(res, 0, 7)) : 0;
1304 tableinfo.reloftype = (pset.sversion >= 90000 &&
1305 strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
1306 pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
1307 tableinfo.relpersistence = (pset.sversion >= 90100) ?
1308 *(PQgetvalue(res, 0, 9)) : 0;
1309 tableinfo.relreplident = (pset.sversion >= 90400) ?
1310 *(PQgetvalue(res, 0, 10)) : 'd';
1315 * If it's a sequence, fetch its values and store into an array that will
1318 if (tableinfo.relkind == 'S')
1320 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1321 /* must be separate because fmtId isn't reentrant */
1322 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1324 res = PSQLexec(buf.data, false);
1328 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1330 for (i = 0; i < PQnfields(res); i++)
1331 seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1332 seq_values[i] = NULL;
1341 * You need to modify value of "firstvcol" which will be defined below if
1342 * you are adding column(s) preceding to verbose-only columns.
1344 printfPQExpBuffer(&buf, "SELECT a.attname,");
1345 appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1346 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1347 "\n FROM pg_catalog.pg_attrdef d"
1348 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1349 "\n a.attnotnull, a.attnum,");
1350 if (pset.sversion >= 90100)
1351 appendPQExpBufferStr(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1352 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1354 appendPQExpBufferStr(&buf, "\n NULL AS attcollation");
1355 if (tableinfo.relkind == 'i')
1356 appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1358 appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
1359 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1360 appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1361 " '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1362 " pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1364 appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
1367 appendPQExpBufferStr(&buf, ",\n a.attstorage");
1368 appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1371 * In 9.0+, we have column comments for: relations, views, composite
1372 * types, and foreign tables (c.f. CommentObject() in comment.c).
1374 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1375 tableinfo.relkind == 'm' ||
1376 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1377 appendPQExpBufferStr(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1380 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1381 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1382 appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1384 res = PSQLexec(buf.data, false);
1387 numrows = PQntuples(res);
1390 switch (tableinfo.relkind)
1393 if (tableinfo.relpersistence == 'u')
1394 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1395 schemaname, relationname);
1397 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1398 schemaname, relationname);
1401 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1402 schemaname, relationname);
1405 if (tableinfo.relpersistence == 'u')
1406 printfPQExpBuffer(&title, _("Unlogged materialized view \"%s.%s\""),
1407 schemaname, relationname);
1409 printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
1410 schemaname, relationname);
1413 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1414 schemaname, relationname);
1417 if (tableinfo.relpersistence == 'u')
1418 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1419 schemaname, relationname);
1421 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1422 schemaname, relationname);
1425 /* not used as of 8.2, but keep it for backwards compatibility */
1426 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1427 schemaname, relationname);
1430 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1431 schemaname, relationname);
1434 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1435 schemaname, relationname);
1438 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1439 schemaname, relationname);
1442 /* untranslated unknown relkind */
1443 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1444 tableinfo.relkind, schemaname, relationname);
1448 /* Set the number of columns, and their names */
1449 headers[0] = gettext_noop("Column");
1450 headers[1] = gettext_noop("Type");
1453 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1454 tableinfo.relkind == 'm' ||
1455 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1457 show_modifiers = true;
1458 headers[cols++] = gettext_noop("Modifiers");
1459 modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers));
1462 if (tableinfo.relkind == 'S')
1463 headers[cols++] = gettext_noop("Value");
1465 if (tableinfo.relkind == 'i')
1466 headers[cols++] = gettext_noop("Definition");
1468 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1469 headers[cols++] = gettext_noop("FDW Options");
1473 headers[cols++] = gettext_noop("Storage");
1474 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1475 tableinfo.relkind == 'f')
1476 headers[cols++] = gettext_noop("Stats target");
1477 /* Column comments, if the relkind supports this feature. */
1478 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1479 tableinfo.relkind == 'm' ||
1480 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1481 headers[cols++] = gettext_noop("Description");
1484 printTableInit(&cont, &myopt, title.data, cols, numrows);
1485 printTableInitialized = true;
1487 for (i = 0; i < cols; i++)
1488 printTableAddHeader(&cont, headers[i], true, 'l');
1490 /* Check if table is a view or materialized view */
1491 if ((tableinfo.relkind == 'v' || tableinfo.relkind == 'm') && verbose)
1495 printfPQExpBuffer(&buf,
1496 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1498 result = PSQLexec(buf.data, false);
1502 if (PQntuples(result) > 0)
1503 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1508 /* Generate table cells to be printed */
1509 for (i = 0; i < numrows; i++)
1512 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1515 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1517 /* Modifiers: collate, not null, default */
1520 resetPQExpBuffer(&tmpbuf);
1522 if (!PQgetisnull(res, i, 5))
1525 appendPQExpBufferStr(&tmpbuf, " ");
1526 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1527 PQgetvalue(res, i, 5));
1530 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1533 appendPQExpBufferStr(&tmpbuf, " ");
1534 appendPQExpBufferStr(&tmpbuf, _("not null"));
1537 /* handle "default" here */
1538 /* (note: above we cut off the 'default' string at 128) */
1539 if (strlen(PQgetvalue(res, i, 2)) != 0)
1542 appendPQExpBufferStr(&tmpbuf, " ");
1543 /* translator: default values of column definitions */
1544 appendPQExpBuffer(&tmpbuf, _("default %s"),
1545 PQgetvalue(res, i, 2));
1548 modifiers[i] = pg_strdup(tmpbuf.data);
1549 printTableAddCell(&cont, modifiers[i], false, false);
1552 /* Value: for sequences only */
1553 if (tableinfo.relkind == 'S')
1554 printTableAddCell(&cont, seq_values[i], false, false);
1556 /* Expression for index column */
1557 if (tableinfo.relkind == 'i')
1558 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1560 /* FDW options for foreign table column, only for 9.2 or later */
1561 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1562 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1564 /* Storage and Description */
1568 char *storage = PQgetvalue(res, i, firstvcol);
1570 /* these strings are literal in our syntax, so not translated. */
1571 printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1572 (storage[0] == 'm' ? "main" :
1573 (storage[0] == 'x' ? "extended" :
1574 (storage[0] == 'e' ? "external" :
1578 /* Statistics target, if the relkind supports this feature */
1579 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1580 tableinfo.relkind == 'f')
1582 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1586 /* Column comments, if the relkind supports this feature. */
1587 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1588 tableinfo.relkind == 'm' ||
1589 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1590 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1596 if (tableinfo.relkind == 'i')
1598 /* Footer information about an index */
1601 printfPQExpBuffer(&buf,
1602 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1603 if (pset.sversion >= 80200)
1604 appendPQExpBufferStr(&buf, "i.indisvalid,\n");
1606 appendPQExpBufferStr(&buf, "true AS indisvalid,\n");
1607 if (pset.sversion >= 90000)
1608 appendPQExpBufferStr(&buf,
1609 " (NOT i.indimmediate) AND "
1610 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1611 "WHERE conrelid = i.indrelid AND "
1612 "conindid = i.indexrelid AND "
1613 "contype IN ('p','u','x') AND "
1614 "condeferrable) AS condeferrable,\n"
1615 " (NOT i.indimmediate) AND "
1616 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1617 "WHERE conrelid = i.indrelid AND "
1618 "conindid = i.indexrelid AND "
1619 "contype IN ('p','u','x') AND "
1620 "condeferred) AS condeferred,\n");
1622 appendPQExpBufferStr(&buf,
1623 " false AS condeferrable, false AS condeferred,\n");
1625 if (pset.sversion >= 90400)
1626 appendPQExpBuffer(&buf, "i.indisreplident,\n");
1628 appendPQExpBuffer(&buf, "false AS indisreplident,\n");
1630 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
1631 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1632 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1633 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1634 "AND i.indrelid = c2.oid;",
1637 result = PSQLexec(buf.data, false);
1640 else if (PQntuples(result) != 1)
1647 char *indisunique = PQgetvalue(result, 0, 0);
1648 char *indisprimary = PQgetvalue(result, 0, 1);
1649 char *indisclustered = PQgetvalue(result, 0, 2);
1650 char *indisvalid = PQgetvalue(result, 0, 3);
1651 char *deferrable = PQgetvalue(result, 0, 4);
1652 char *deferred = PQgetvalue(result, 0, 5);
1653 char *indisreplident = PQgetvalue(result, 0, 6);
1654 char *indamname = PQgetvalue(result, 0, 7);
1655 char *indtable = PQgetvalue(result, 0, 8);
1656 char *indpred = PQgetvalue(result, 0, 9);
1658 if (strcmp(indisprimary, "t") == 0)
1659 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1660 else if (strcmp(indisunique, "t") == 0)
1661 printfPQExpBuffer(&tmpbuf, _("unique, "));
1663 resetPQExpBuffer(&tmpbuf);
1664 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1666 /* we assume here that index and table are in same schema */
1667 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1668 schemaname, indtable);
1670 if (strlen(indpred))
1671 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1673 if (strcmp(indisclustered, "t") == 0)
1674 appendPQExpBufferStr(&tmpbuf, _(", clustered"));
1676 if (strcmp(indisvalid, "t") != 0)
1677 appendPQExpBufferStr(&tmpbuf, _(", invalid"));
1679 if (strcmp(deferrable, "t") == 0)
1680 appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
1682 if (strcmp(deferred, "t") == 0)
1683 appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
1685 if (strcmp(indisreplident, "t") == 0)
1686 appendPQExpBuffer(&tmpbuf, _(", replica identity"));
1688 printTableAddFooter(&cont, tmpbuf.data);
1689 add_tablespace_footer(&cont, tableinfo.relkind,
1690 tableinfo.tablespace, true);
1695 else if (tableinfo.relkind == 'S')
1697 /* Footer information about a sequence */
1698 PGresult *result = NULL;
1700 /* Get the column that owns this sequence */
1701 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1702 "\n pg_catalog.quote_ident(relname) || '.' ||"
1703 "\n pg_catalog.quote_ident(attname)"
1704 "\nFROM pg_catalog.pg_class c"
1705 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1706 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1707 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1708 "\n a.attrelid=c.oid AND"
1709 "\n a.attnum=d.refobjsubid)"
1710 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1711 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1713 "\n AND d.deptype='a'",
1716 result = PSQLexec(buf.data, false);
1719 else if (PQntuples(result) == 1)
1721 printfPQExpBuffer(&buf, _("Owned by: %s"),
1722 PQgetvalue(result, 0, 0));
1723 printTableAddFooter(&cont, buf.data);
1727 * If we get no rows back, don't show anything (obviously). We should
1728 * never get more than one row back, but if we do, just ignore it and
1729 * don't print anything.
1733 else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1734 tableinfo.relkind == 'f')
1736 /* Footer information about a table */
1737 PGresult *result = NULL;
1741 if (tableinfo.hasindex)
1743 printfPQExpBuffer(&buf,
1744 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1745 if (pset.sversion >= 80200)
1746 appendPQExpBufferStr(&buf, "i.indisvalid, ");
1748 appendPQExpBufferStr(&buf, "true as indisvalid, ");
1749 appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
1750 if (pset.sversion >= 90000)
1751 appendPQExpBufferStr(&buf,
1752 "pg_catalog.pg_get_constraintdef(con.oid, true), "
1753 "contype, condeferrable, condeferred");
1755 appendPQExpBufferStr(&buf,
1756 "null AS constraintdef, null AS contype, "
1757 "false AS condeferrable, false AS condeferred");
1758 if (pset.sversion >= 90400)
1759 appendPQExpBufferStr(&buf, ", i.indisreplident");
1761 appendPQExpBufferStr(&buf, ", false AS indisreplident");
1762 if (pset.sversion >= 80000)
1763 appendPQExpBufferStr(&buf, ", c2.reltablespace");
1764 appendPQExpBufferStr(&buf,
1765 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1766 if (pset.sversion >= 90000)
1767 appendPQExpBufferStr(&buf,
1768 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1769 appendPQExpBuffer(&buf,
1770 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1771 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
1773 result = PSQLexec(buf.data, false);
1777 tuples = PQntuples(result);
1781 printTableAddFooter(&cont, _("Indexes:"));
1782 for (i = 0; i < tuples; i++)
1784 /* untranslated index name */
1785 printfPQExpBuffer(&buf, " \"%s\"",
1786 PQgetvalue(result, i, 0));
1788 /* If exclusion constraint, print the constraintdef */
1789 if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1791 appendPQExpBuffer(&buf, " %s",
1792 PQgetvalue(result, i, 6));
1796 const char *indexdef;
1797 const char *usingpos;
1799 /* Label as primary key or unique (but not both) */
1800 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1801 appendPQExpBufferStr(&buf, " PRIMARY KEY,");
1802 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1804 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1805 appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
1807 appendPQExpBufferStr(&buf, " UNIQUE,");
1810 /* Everything after "USING" is echoed verbatim */
1811 indexdef = PQgetvalue(result, i, 5);
1812 usingpos = strstr(indexdef, " USING ");
1814 indexdef = usingpos + 7;
1815 appendPQExpBuffer(&buf, " %s", indexdef);
1817 /* Need these for deferrable PK/UNIQUE indexes */
1818 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1819 appendPQExpBufferStr(&buf, " DEFERRABLE");
1821 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1822 appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
1825 /* Add these for all cases */
1826 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1827 appendPQExpBufferStr(&buf, " CLUSTER");
1829 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1830 appendPQExpBufferStr(&buf, " INVALID");
1832 if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
1833 appendPQExpBuffer(&buf, " REPLICA IDENTITY");
1835 printTableAddFooter(&cont, buf.data);
1837 /* Print tablespace of the index on the same line */
1838 if (pset.sversion >= 80000)
1839 add_tablespace_footer(&cont, 'i',
1840 atooid(PQgetvalue(result, i, 11)),
1847 /* print table (and column) check constraints */
1848 if (tableinfo.checks)
1850 printfPQExpBuffer(&buf,
1851 "SELECT r.conname, "
1852 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1853 "FROM pg_catalog.pg_constraint r\n"
1854 "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
1857 result = PSQLexec(buf.data, false);
1861 tuples = PQntuples(result);
1865 printTableAddFooter(&cont, _("Check constraints:"));
1866 for (i = 0; i < tuples; i++)
1868 /* untranslated contraint name and def */
1869 printfPQExpBuffer(&buf, " \"%s\" %s",
1870 PQgetvalue(result, i, 0),
1871 PQgetvalue(result, i, 1));
1873 printTableAddFooter(&cont, buf.data);
1879 /* print foreign-key constraints (there are none if no triggers) */
1880 if (tableinfo.hastriggers)
1882 printfPQExpBuffer(&buf,
1884 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1885 "FROM pg_catalog.pg_constraint r\n"
1886 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
1888 result = PSQLexec(buf.data, false);
1892 tuples = PQntuples(result);
1896 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1897 for (i = 0; i < tuples; i++)
1899 /* untranslated constraint name and def */
1900 printfPQExpBuffer(&buf, " \"%s\" %s",
1901 PQgetvalue(result, i, 0),
1902 PQgetvalue(result, i, 1));
1904 printTableAddFooter(&cont, buf.data);
1910 /* print incoming foreign-key references (none if no triggers) */
1911 if (tableinfo.hastriggers)
1913 printfPQExpBuffer(&buf,
1914 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1915 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1916 "FROM pg_catalog.pg_constraint c\n"
1917 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
1919 result = PSQLexec(buf.data, false);
1923 tuples = PQntuples(result);
1927 printTableAddFooter(&cont, _("Referenced by:"));
1928 for (i = 0; i < tuples; i++)
1930 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
1931 PQgetvalue(result, i, 1),
1932 PQgetvalue(result, i, 0),
1933 PQgetvalue(result, i, 2));
1935 printTableAddFooter(&cont, buf.data);
1942 if (tableinfo.hasrules && tableinfo.relkind != 'm')
1944 if (pset.sversion >= 80300)
1946 printfPQExpBuffer(&buf,
1947 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1949 "FROM pg_catalog.pg_rewrite r\n"
1950 "WHERE r.ev_class = '%s' ORDER BY 1;",
1955 printfPQExpBuffer(&buf,
1956 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1957 "'O'::char AS ev_enabled\n"
1958 "FROM pg_catalog.pg_rewrite r\n"
1959 "WHERE r.ev_class = '%s' ORDER BY 1;",
1962 result = PSQLexec(buf.data, false);
1966 tuples = PQntuples(result);
1973 for (category = 0; category < 4; category++)
1975 have_heading = false;
1977 for (i = 0; i < tuples; i++)
1979 const char *ruledef;
1980 bool list_rule = false;
1985 if (*PQgetvalue(result, i, 2) == 'O')
1989 if (*PQgetvalue(result, i, 2) == 'D')
1993 if (*PQgetvalue(result, i, 2) == 'A')
1997 if (*PQgetvalue(result, i, 2) == 'R')
2009 printfPQExpBuffer(&buf, _("Rules:"));
2012 printfPQExpBuffer(&buf, _("Disabled rules:"));
2015 printfPQExpBuffer(&buf, _("Rules firing always:"));
2018 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
2021 printTableAddFooter(&cont, buf.data);
2022 have_heading = true;
2025 /* Everything after "CREATE RULE" is echoed verbatim */
2026 ruledef = PQgetvalue(result, i, 1);
2028 printfPQExpBuffer(&buf, " %s", ruledef);
2029 printTableAddFooter(&cont, buf.data);
2039 PGresult *result = NULL;
2041 /* Footer information about a view */
2042 printTableAddFooter(&cont, _("View definition:"));
2043 printTableAddFooter(&cont, view_def);
2046 if (tableinfo.hasrules)
2048 printfPQExpBuffer(&buf,
2049 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
2050 "FROM pg_catalog.pg_rewrite r\n"
2051 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
2053 result = PSQLexec(buf.data, false);
2057 if (PQntuples(result) > 0)
2059 printTableAddFooter(&cont, _("Rules:"));
2060 for (i = 0; i < PQntuples(result); i++)
2062 const char *ruledef;
2064 /* Everything after "CREATE RULE" is echoed verbatim */
2065 ruledef = PQgetvalue(result, i, 1);
2068 printfPQExpBuffer(&buf, " %s", ruledef);
2069 printTableAddFooter(&cont, buf.data);
2077 * Print triggers next, if any (but only user-defined triggers). This
2078 * could apply to either a table or a view.
2080 if (tableinfo.hastriggers)
2085 printfPQExpBuffer(&buf,
2087 "pg_catalog.pg_get_triggerdef(t.oid%s), "
2089 "FROM pg_catalog.pg_trigger t\n"
2090 "WHERE t.tgrelid = '%s' AND ",
2091 (pset.sversion >= 90000 ? ", true" : ""),
2093 if (pset.sversion >= 90000)
2094 appendPQExpBufferStr(&buf, "NOT t.tgisinternal");
2095 else if (pset.sversion >= 80300)
2096 appendPQExpBufferStr(&buf, "t.tgconstraint = 0");
2098 appendPQExpBufferStr(&buf,
2099 "(NOT tgisconstraint "
2101 " (SELECT 1 FROM pg_catalog.pg_depend d "
2102 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2103 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2104 appendPQExpBufferStr(&buf, "\nORDER BY 1;");
2106 result = PSQLexec(buf.data, false);
2110 tuples = PQntuples(result);
2118 * split the output into 4 different categories. Enabled triggers,
2119 * disabled triggers and the two special ALWAYS and REPLICA
2122 for (category = 0; category < 4; category++)
2124 have_heading = false;
2125 for (i = 0; i < tuples; i++)
2129 const char *usingpos;
2130 const char *tgenabled;
2133 * Check if this trigger falls into the current category
2135 tgenabled = PQgetvalue(result, i, 2);
2136 list_trigger = false;
2140 if (*tgenabled == 'O' || *tgenabled == 't')
2141 list_trigger = true;
2144 if (*tgenabled == 'D' || *tgenabled == 'f')
2145 list_trigger = true;
2148 if (*tgenabled == 'A')
2149 list_trigger = true;
2152 if (*tgenabled == 'R')
2153 list_trigger = true;
2156 if (list_trigger == false)
2159 /* Print the category heading once */
2160 if (have_heading == false)
2165 printfPQExpBuffer(&buf, _("Triggers:"));
2168 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2171 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2174 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2178 printTableAddFooter(&cont, buf.data);
2179 have_heading = true;
2182 /* Everything after "TRIGGER" is echoed verbatim */
2183 tgdef = PQgetvalue(result, i, 1);
2184 usingpos = strstr(tgdef, " TRIGGER ");
2186 tgdef = usingpos + 9;
2188 printfPQExpBuffer(&buf, " %s", tgdef);
2189 printTableAddFooter(&cont, buf.data);
2197 * Finish printing the footer information about a table.
2199 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
2200 tableinfo.relkind == 'f')
2205 /* print foreign server name */
2206 if (tableinfo.relkind == 'f')
2210 /* Footer information about foreign table */
2211 printfPQExpBuffer(&buf,
2212 "SELECT s.srvname,\n"
2213 " array_to_string(ARRAY(SELECT "
2214 " quote_ident(option_name) || ' ' || "
2215 " quote_literal(option_value) FROM "
2216 " pg_options_to_table(ftoptions)), ', ') "
2217 "FROM pg_catalog.pg_foreign_table f,\n"
2218 " pg_catalog.pg_foreign_server s\n"
2219 "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2221 result = PSQLexec(buf.data, false);
2224 else if (PQntuples(result) != 1)
2230 /* Print server name */
2231 printfPQExpBuffer(&buf, "Server: %s",
2232 PQgetvalue(result, 0, 0));
2233 printTableAddFooter(&cont, buf.data);
2235 /* Print per-table FDW options, if any */
2236 ftoptions = PQgetvalue(result, 0, 1);
2237 if (ftoptions && ftoptions[0] != '\0')
2239 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2240 printTableAddFooter(&cont, buf.data);
2245 /* print inherited tables */
2246 printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '%s' ORDER BY inhseqno;", oid);
2248 result = PSQLexec(buf.data, false);
2253 const char *s = _("Inherits");
2254 int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2256 tuples = PQntuples(result);
2258 for (i = 0; i < tuples; i++)
2261 printfPQExpBuffer(&buf, "%s: %s",
2262 s, PQgetvalue(result, i, 0));
2264 printfPQExpBuffer(&buf, "%*s %s",
2265 sw, "", PQgetvalue(result, i, 0));
2267 appendPQExpBufferStr(&buf, ",");
2269 printTableAddFooter(&cont, buf.data);
2275 /* print child tables */
2276 if (pset.sversion >= 80300)
2277 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.oid::pg_catalog.regclass::pg_catalog.text;", oid);
2279 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);
2281 result = PSQLexec(buf.data, false);
2285 tuples = PQntuples(result);
2289 /* print the number of child tables, if any */
2292 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2293 printTableAddFooter(&cont, buf.data);
2298 /* display the list of child tables */
2299 const char *ct = _("Child tables");
2300 int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2302 for (i = 0; i < tuples; i++)
2305 printfPQExpBuffer(&buf, "%s: %s",
2306 ct, PQgetvalue(result, i, 0));
2308 printfPQExpBuffer(&buf, "%*s %s",
2309 ctw, "", PQgetvalue(result, i, 0));
2311 appendPQExpBufferChar(&buf, ',');
2313 printTableAddFooter(&cont, buf.data);
2319 if (tableinfo.reloftype)
2321 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2322 printTableAddFooter(&cont, buf.data);
2325 if ((tableinfo.relkind == 'r' || tableinfo.relkind == 'm') &&
2326 tableinfo.relreplident != 'd' && tableinfo.relreplident != 'i')
2328 const char *s = _("Replica Identity");
2330 printfPQExpBuffer(&buf, "%s: %s",
2332 tableinfo.relreplident == 'n' ? "NOTHING" : "FULL");
2333 printTableAddFooter(&cont, buf.data);
2336 /* OIDs, if verbose and not a materialized view */
2337 if (verbose && tableinfo.relkind != 'm')
2339 const char *s = _("Has OIDs");
2341 printfPQExpBuffer(&buf, "%s: %s", s,
2342 (tableinfo.hasoids ? _("yes") : _("no")));
2343 printTableAddFooter(&cont, buf.data);
2346 /* Tablespace info */
2347 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2351 /* reloptions, if verbose */
2353 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2355 const char *t = _("Options");
2357 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
2358 printTableAddFooter(&cont, buf.data);
2361 printTable(&cont, pset.queryFout, pset.logfile);
2362 printTableCleanup(&cont);
2369 if (printTableInitialized)
2370 printTableCleanup(&cont);
2371 termPQExpBuffer(&buf);
2372 termPQExpBuffer(&title);
2373 termPQExpBuffer(&tmpbuf);
2377 for (ptr = seq_values; *ptr; ptr++)
2384 for (ptr = modifiers; *ptr; ptr++)
2399 * Add a tablespace description to a footer. If 'newline' is true, it is added
2400 * in a new line; otherwise it's appended to the current value of the last
2404 add_tablespace_footer(printTableContent *const cont, char relkind,
2405 Oid tablespace, const bool newline)
2407 /* relkinds for which we support tablespaces */
2408 if (relkind == 'r' || relkind == 'm' || relkind == 'i')
2411 * We ignore the database default tablespace so that users not using
2412 * tablespaces don't need to know about them. This case also covers
2413 * pre-8.0 servers, for which tablespace will always be 0.
2415 if (tablespace != 0)
2417 PGresult *result = NULL;
2418 PQExpBufferData buf;
2420 initPQExpBuffer(&buf);
2421 printfPQExpBuffer(&buf,
2422 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2423 "WHERE oid = '%u';", tablespace);
2424 result = PSQLexec(buf.data, false);
2427 /* Should always be the case, but.... */
2428 if (PQntuples(result) > 0)
2432 /* Add the tablespace as a new footer */
2433 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2434 PQgetvalue(result, 0, 0));
2435 printTableAddFooter(cont, buf.data);
2439 /* Append the tablespace to the latest footer */
2440 printfPQExpBuffer(&buf, "%s", cont->footer->data);
2443 translator: before this string there's an index description like
2444 '"foo_pkey" PRIMARY KEY, btree (a)' */
2445 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2446 PQgetvalue(result, 0, 0));
2447 printTableSetFooter(cont, buf.data);
2451 termPQExpBuffer(&buf);
2459 * Describes roles. Any schema portion of the pattern is ignored.
2462 describeRoles(const char *pattern, bool verbose)
2464 PQExpBufferData buf;
2466 printTableContent cont;
2467 printTableOpt myopt = pset.popt.topt;
2472 const char align = 'l';
2475 myopt.default_footer = false;
2477 initPQExpBuffer(&buf);
2479 if (pset.sversion >= 80100)
2481 printfPQExpBuffer(&buf,
2482 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2483 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2484 " r.rolconnlimit, r.rolvaliduntil,\n"
2485 " ARRAY(SELECT b.rolname\n"
2486 " FROM pg_catalog.pg_auth_members m\n"
2487 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2488 " WHERE m.member = r.oid) as memberof");
2490 if (verbose && pset.sversion >= 80200)
2492 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2495 if (pset.sversion >= 90100)
2497 appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2500 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2502 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2503 NULL, "r.rolname", NULL, NULL);
2507 printfPQExpBuffer(&buf,
2508 "SELECT u.usename AS rolname,\n"
2509 " u.usesuper AS rolsuper,\n"
2510 " true AS rolinherit, false AS rolcreaterole,\n"
2511 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2512 " -1 AS rolconnlimit,"
2513 " u.valuntil as rolvaliduntil,\n"
2514 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2515 "\nFROM pg_catalog.pg_user u\n");
2517 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2518 NULL, "u.usename", NULL, NULL);
2521 appendPQExpBufferStr(&buf, "ORDER BY 1;");
2523 res = PSQLexec(buf.data, false);
2527 nrows = PQntuples(res);
2528 attr = pg_malloc0((nrows + 1) * sizeof(*attr));
2530 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2532 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2533 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2534 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2536 if (verbose && pset.sversion >= 80200)
2537 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2539 for (i = 0; i < nrows; i++)
2541 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2543 resetPQExpBuffer(&buf);
2544 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2545 add_role_attribute(&buf, _("Superuser"));
2547 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2548 add_role_attribute(&buf, _("No inheritance"));
2550 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2551 add_role_attribute(&buf, _("Create role"));
2553 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2554 add_role_attribute(&buf, _("Create DB"));
2556 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2557 add_role_attribute(&buf, _("Cannot login"));
2559 if (pset.sversion >= 90100)
2560 if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2561 add_role_attribute(&buf, _("Replication"));
2563 conns = atoi(PQgetvalue(res, i, 6));
2567 appendPQExpBufferChar(&buf, '\n');
2570 appendPQExpBufferStr(&buf, _("No connections"));
2572 appendPQExpBuffer(&buf, ngettext("%d connection",
2578 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2581 appendPQExpBufferStr(&buf, "\n");
2582 appendPQExpBufferStr(&buf, _("Password valid until "));
2583 appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
2586 attr[i] = pg_strdup(buf.data);
2588 printTableAddCell(&cont, attr[i], false, false);
2590 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2592 if (verbose && pset.sversion >= 80200)
2593 printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
2595 termPQExpBuffer(&buf);
2597 printTable(&cont, pset.queryFout, pset.logfile);
2598 printTableCleanup(&cont);
2600 for (i = 0; i < nrows; i++)
2609 add_role_attribute(PQExpBuffer buf, const char *const str)
2612 appendPQExpBufferStr(buf, ", ");
2614 appendPQExpBufferStr(buf, str);
2621 listDbRoleSettings(const char *pattern, const char *pattern2)
2623 PQExpBufferData buf;
2625 printQueryOpt myopt = pset.popt;
2627 initPQExpBuffer(&buf);
2629 if (pset.sversion >= 90000)
2633 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
2634 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2635 "FROM pg_db_role_setting AS s\n"
2636 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2637 "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2638 gettext_noop("Role"),
2639 gettext_noop("Database"),
2640 gettext_noop("Settings"));
2641 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2642 NULL, "pg_roles.rolname", NULL, NULL);
2643 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2644 NULL, "pg_database.datname", NULL, NULL);
2645 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
2649 fprintf(pset.queryFout,
2650 _("No per-database role settings support in this server version.\n"));
2654 res = PSQLexec(buf.data, false);
2658 if (PQntuples(res) == 0 && !pset.quiet)
2661 fprintf(pset.queryFout, _("No matching settings found.\n"));
2663 fprintf(pset.queryFout, _("No settings found.\n"));
2667 myopt.nullPrint = NULL;
2668 myopt.title = _("List of settings");
2669 myopt.translate_header = true;
2671 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2675 resetPQExpBuffer(&buf);
2683 * handler for \dt, \di, etc.
2685 * tabtypes is an array of characters, specifying what info is desired:
2689 * m - materialized views
2691 * E - foreign table (Note: different from 'f', the relkind value)
2692 * (any order of the above is fine)
2693 * If tabtypes is empty, we default to \dtvsE.
2696 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2698 bool showTables = strchr(tabtypes, 't') != NULL;
2699 bool showIndexes = strchr(tabtypes, 'i') != NULL;
2700 bool showViews = strchr(tabtypes, 'v') != NULL;
2701 bool showMatViews = strchr(tabtypes, 'm') != NULL;
2702 bool showSeq = strchr(tabtypes, 's') != NULL;
2703 bool showForeign = strchr(tabtypes, 'E') != NULL;
2705 PQExpBufferData buf;
2707 printQueryOpt myopt = pset.popt;
2708 static const bool translate_columns[] = {false, false, true, false, false, false, false};
2710 if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
2711 showTables = showViews = showMatViews = showSeq = showForeign = true;
2713 initPQExpBuffer(&buf);
2716 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2717 * for backwards compatibility.
2719 printfPQExpBuffer(&buf,
2720 "SELECT n.nspname as \"%s\",\n"
2721 " c.relname as \"%s\",\n"
2723 " WHEN 'r' THEN '%s'"
2724 " WHEN 'v' THEN '%s'"
2725 " WHEN 'm' THEN '%s'"
2726 " WHEN 'i' THEN '%s'"
2727 " WHEN 'S' THEN '%s'"
2728 " WHEN 's' THEN '%s'"
2729 " WHEN 'f' THEN '%s'"
2731 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2732 gettext_noop("Schema"),
2733 gettext_noop("Name"),
2734 gettext_noop("table"),
2735 gettext_noop("view"),
2736 gettext_noop("materialized view"),
2737 gettext_noop("index"),
2738 gettext_noop("sequence"),
2739 gettext_noop("special"),
2740 gettext_noop("foreign table"),
2741 gettext_noop("Type"),
2742 gettext_noop("Owner"));
2745 appendPQExpBuffer(&buf,
2746 ",\n c2.relname as \"%s\"",
2747 gettext_noop("Table"));
2752 * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
2753 * size of a table, including FSM, VM and TOAST tables.
2755 if (pset.sversion >= 90000)
2756 appendPQExpBuffer(&buf,
2757 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
2758 gettext_noop("Size"));
2759 else if (pset.sversion >= 80100)
2760 appendPQExpBuffer(&buf,
2761 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2762 gettext_noop("Size"));
2764 appendPQExpBuffer(&buf,
2765 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2766 gettext_noop("Description"));
2769 appendPQExpBufferStr(&buf,
2770 "\nFROM pg_catalog.pg_class c"
2771 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2773 appendPQExpBufferStr(&buf,
2774 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2775 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2777 appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
2779 appendPQExpBufferStr(&buf, "'r',");
2781 appendPQExpBufferStr(&buf, "'v',");
2783 appendPQExpBufferStr(&buf, "'m',");
2785 appendPQExpBufferStr(&buf, "'i',");
2787 appendPQExpBufferStr(&buf, "'S',");
2788 if (showSystem || pattern)
2789 appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2792 appendPQExpBufferStr(&buf, "'f',");
2794 appendPQExpBufferStr(&buf, "''"); /* dummy */
2795 appendPQExpBufferStr(&buf, ")\n");
2797 if (!showSystem && !pattern)
2798 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
2799 " AND n.nspname <> 'information_schema'\n");
2802 * TOAST objects are suppressed unconditionally. Since we don't provide
2803 * any way to select relkind 't' above, we would never show toast tables
2804 * in any case; it seems a bit confusing to allow their indexes to be
2805 * shown. Use plain \d if you really need to look at a TOAST table/index.
2807 appendPQExpBufferStr(&buf, " AND n.nspname !~ '^pg_toast'\n");
2809 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2810 "n.nspname", "c.relname", NULL,
2811 "pg_catalog.pg_table_is_visible(c.oid)");
2813 appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
2815 res = PSQLexec(buf.data, false);
2816 termPQExpBuffer(&buf);
2820 if (PQntuples(res) == 0 && !pset.quiet)
2823 fprintf(pset.queryFout, _("No matching relations found.\n"));
2825 fprintf(pset.queryFout, _("No relations found.\n"));
2829 myopt.nullPrint = NULL;
2830 myopt.title = _("List of relations");
2831 myopt.translate_header = true;
2832 myopt.translate_columns = translate_columns;
2833 myopt.n_translate_columns = lengthof(translate_columns);
2835 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2846 * Describes languages.
2849 listLanguages(const char *pattern, bool verbose, bool showSystem)
2851 PQExpBufferData buf;
2853 printQueryOpt myopt = pset.popt;
2855 initPQExpBuffer(&buf);
2857 printfPQExpBuffer(&buf,
2858 "SELECT l.lanname AS \"%s\",\n",
2859 gettext_noop("Name"));
2860 if (pset.sversion >= 80300)
2861 appendPQExpBuffer(&buf,
2862 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
2863 gettext_noop("Owner"));
2865 appendPQExpBuffer(&buf,
2866 " l.lanpltrusted AS \"%s\"",
2867 gettext_noop("Trusted"));
2871 appendPQExpBuffer(&buf,
2872 ",\n NOT l.lanispl AS \"%s\",\n"
2873 " l.lanplcallfoid::regprocedure AS \"%s\",\n"
2874 " l.lanvalidator::regprocedure AS \"%s\",\n ",
2875 gettext_noop("Internal Language"),
2876 gettext_noop("Call Handler"),
2877 gettext_noop("Validator"));
2878 if (pset.sversion >= 90000)
2879 appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
2880 gettext_noop("Inline Handler"));
2881 printACLColumn(&buf, "l.lanacl");
2884 appendPQExpBuffer(&buf,
2885 ",\n d.description AS \"%s\""
2886 "\nFROM pg_catalog.pg_language l\n"
2887 "LEFT JOIN pg_catalog.pg_description d\n"
2888 " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
2889 " AND d.objsubid = 0\n",
2890 gettext_noop("Description"));
2893 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2894 NULL, "l.lanname", NULL, NULL);
2896 if (!showSystem && !pattern)
2897 appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
2900 appendPQExpBufferStr(&buf, "ORDER BY 1;");
2902 res = PSQLexec(buf.data, false);
2903 termPQExpBuffer(&buf);
2907 myopt.nullPrint = NULL;
2908 myopt.title = _("List of languages");
2909 myopt.translate_header = true;
2911 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2921 * Describes domains.
2924 listDomains(const char *pattern, bool verbose, bool showSystem)
2926 PQExpBufferData buf;
2928 printQueryOpt myopt = pset.popt;
2930 initPQExpBuffer(&buf);
2932 printfPQExpBuffer(&buf,
2933 "SELECT n.nspname as \"%s\",\n"
2934 " t.typname as \"%s\",\n"
2935 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2937 gettext_noop("Schema"),
2938 gettext_noop("Name"),
2939 gettext_noop("Type"));
2941 if (pset.sversion >= 90100)
2942 appendPQExpBufferStr(&buf,
2943 " COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
2944 " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
2945 appendPQExpBuffer(&buf,
2946 " CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
2947 " CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
2949 " pg_catalog.array_to_string(ARRAY(\n"
2950 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2951 " ), ' ') as \"%s\"",
2952 gettext_noop("Modifier"),
2953 gettext_noop("Check"));
2957 if (pset.sversion >= 90200)
2959 appendPQExpBufferStr(&buf, ",\n ");
2960 printACLColumn(&buf, "t.typacl");
2962 appendPQExpBuffer(&buf,
2963 ",\n d.description as \"%s\"",
2964 gettext_noop("Description"));
2967 appendPQExpBufferStr(&buf,
2968 "\nFROM pg_catalog.pg_type t\n"
2969 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
2972 appendPQExpBufferStr(&buf,
2973 " LEFT JOIN pg_catalog.pg_description d "
2974 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
2975 "AND d.objsubid = 0\n");
2977 appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
2979 if (!showSystem && !pattern)
2980 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
2981 " AND n.nspname <> 'information_schema'\n");
2983 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2984 "n.nspname", "t.typname", NULL,
2985 "pg_catalog.pg_type_is_visible(t.oid)");
2987 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
2989 res = PSQLexec(buf.data, false);
2990 termPQExpBuffer(&buf);
2994 myopt.nullPrint = NULL;
2995 myopt.title = _("List of domains");
2996 myopt.translate_header = true;
2998 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3007 * Describes conversions.
3010 listConversions(const char *pattern, bool verbose, bool showSystem)
3012 PQExpBufferData buf;
3014 printQueryOpt myopt = pset.popt;
3015 static const bool translate_columns[] =
3016 {false, false, false, false, true, false};
3018 initPQExpBuffer(&buf);
3020 printfPQExpBuffer(&buf,
3021 "SELECT n.nspname AS \"%s\",\n"
3022 " c.conname AS \"%s\",\n"
3023 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3024 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3025 " CASE WHEN c.condefault THEN '%s'\n"
3026 " ELSE '%s' END AS \"%s\"",
3027 gettext_noop("Schema"),
3028 gettext_noop("Name"),
3029 gettext_noop("Source"),
3030 gettext_noop("Destination"),
3031 gettext_noop("yes"), gettext_noop("no"),
3032 gettext_noop("Default?"));
3035 appendPQExpBuffer(&buf,
3036 ",\n d.description AS \"%s\"",
3037 gettext_noop("Description"));
3039 appendPQExpBufferStr(&buf,
3040 "\nFROM pg_catalog.pg_conversion c\n"
3041 " JOIN pg_catalog.pg_namespace n "
3042 "ON n.oid = c.connamespace\n");
3045 appendPQExpBufferStr(&buf,
3046 "LEFT JOIN pg_catalog.pg_description d "
3047 "ON d.classoid = c.tableoid\n"
3048 " AND d.objoid = c.oid "
3049 "AND d.objsubid = 0\n");
3051 appendPQExpBufferStr(&buf, "WHERE true\n");
3053 if (!showSystem && !pattern)
3054 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3055 " AND n.nspname <> 'information_schema'\n");
3057 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3058 "n.nspname", "c.conname", NULL,
3059 "pg_catalog.pg_conversion_is_visible(c.oid)");
3061 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3063 res = PSQLexec(buf.data, false);
3064 termPQExpBuffer(&buf);
3068 myopt.nullPrint = NULL;
3069 myopt.title = _("List of conversions");
3070 myopt.translate_header = true;
3071 myopt.translate_columns = translate_columns;
3072 myopt.n_translate_columns = lengthof(translate_columns);
3074 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3083 * Describes Event Triggers.
3086 listEventTriggers(const char *pattern, bool verbose)
3088 PQExpBufferData buf;
3090 printQueryOpt myopt = pset.popt;
3091 static const bool translate_columns[] =
3092 {false, false, false, true, false, false, false};
3094 initPQExpBuffer(&buf);
3096 printfPQExpBuffer(&buf,
3097 "SELECT evtname as \"%s\", "
3098 "evtevent as \"%s\", "
3099 "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3100 " case evtenabled when 'O' then '%s'"
3101 " when 'R' then '%s'"
3102 " when 'A' then '%s'"
3103 " when 'D' then '%s' end as \"%s\",\n"
3104 " e.evtfoid::pg_catalog.regproc as \"%s\", "
3105 "pg_catalog.array_to_string(array(select x"
3106 " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3107 gettext_noop("Name"),
3108 gettext_noop("Event"),
3109 gettext_noop("Owner"),
3110 gettext_noop("enabled"),
3111 gettext_noop("replica"),
3112 gettext_noop("always"),
3113 gettext_noop("disabled"),
3114 gettext_noop("Enabled"),
3115 gettext_noop("Procedure"),
3116 gettext_noop("Tags"));
3118 appendPQExpBuffer(&buf,
3119 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3120 gettext_noop("Description"));
3121 appendPQExpBufferStr(&buf,
3122 "\nFROM pg_catalog.pg_event_trigger e ");
3124 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3125 NULL, "evtname", NULL, NULL);
3127 appendPQExpBufferStr(&buf, "ORDER BY 1");
3129 res = PSQLexec(buf.data, false);
3130 termPQExpBuffer(&buf);
3134 myopt.nullPrint = NULL;
3135 myopt.title = _("List of event triggers");
3136 myopt.translate_header = true;
3137 myopt.translate_columns = translate_columns;
3138 myopt.n_translate_columns = lengthof(translate_columns);
3140 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3152 listCasts(const char *pattern, bool verbose)
3154 PQExpBufferData buf;
3156 printQueryOpt myopt = pset.popt;
3157 static const bool translate_columns[] = {false, false, false, true, false};
3159 initPQExpBuffer(&buf);
3162 * We need a left join to pg_proc for binary casts; the others are just
3163 * paranoia. Also note that we don't attempt to localize '(binary
3164 * coercible)', because there's too much risk of gettext translating a
3165 * function name that happens to match some string in the PO database.
3167 printfPQExpBuffer(&buf,
3168 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3169 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3170 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3173 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3174 " WHEN c.castcontext = 'a' THEN '%s'\n"
3177 gettext_noop("Source type"),
3178 gettext_noop("Target type"),
3179 gettext_noop("Function"),
3181 gettext_noop("in assignment"),
3182 gettext_noop("yes"),
3183 gettext_noop("Implicit?"));
3186 appendPQExpBuffer(&buf,
3187 ",\n d.description AS \"%s\"\n",
3188 gettext_noop("Description"));
3190 appendPQExpBufferStr(&buf,
3191 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3192 " ON c.castfunc = p.oid\n"
3193 " LEFT JOIN pg_catalog.pg_type ts\n"
3194 " ON c.castsource = ts.oid\n"
3195 " LEFT JOIN pg_catalog.pg_namespace ns\n"
3196 " ON ns.oid = ts.typnamespace\n"
3197 " LEFT JOIN pg_catalog.pg_type tt\n"
3198 " ON c.casttarget = tt.oid\n"
3199 " LEFT JOIN pg_catalog.pg_namespace nt\n"
3200 " ON nt.oid = tt.typnamespace\n");
3203 appendPQExpBufferStr(&buf,
3204 " LEFT JOIN pg_catalog.pg_description d\n"
3205 " ON d.classoid = c.tableoid AND d.objoid = "
3206 "c.oid AND d.objsubid = 0\n");
3208 appendPQExpBufferStr(&buf, "WHERE ( (true");
3211 * Match name pattern against either internal or external name of either
3212 * castsource or casttarget
3214 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3215 "ns.nspname", "ts.typname",
3216 "pg_catalog.format_type(ts.oid, NULL)",
3217 "pg_catalog.pg_type_is_visible(ts.oid)");
3219 appendPQExpBufferStr(&buf, ") OR (true");
3221 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3222 "nt.nspname", "tt.typname",
3223 "pg_catalog.format_type(tt.oid, NULL)",
3224 "pg_catalog.pg_type_is_visible(tt.oid)");
3226 appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3228 res = PSQLexec(buf.data, false);
3229 termPQExpBuffer(&buf);
3233 myopt.nullPrint = NULL;
3234 myopt.title = _("List of casts");
3235 myopt.translate_header = true;
3236 myopt.translate_columns = translate_columns;
3237 myopt.n_translate_columns = lengthof(translate_columns);
3239 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3248 * Describes collations.
3251 listCollations(const char *pattern, bool verbose, bool showSystem)
3253 PQExpBufferData buf;
3255 printQueryOpt myopt = pset.popt;
3256 static const bool translate_columns[] = {false, false, false, false, false};
3258 if (pset.sversion < 90100)
3260 psql_error("The server (version %d.%d) does not support collations.\n",
3261 pset.sversion / 10000, (pset.sversion / 100) % 100);
3265 initPQExpBuffer(&buf);
3267 printfPQExpBuffer(&buf,
3268 "SELECT n.nspname AS \"%s\",\n"
3269 " c.collname AS \"%s\",\n"
3270 " c.collcollate AS \"%s\",\n"
3271 " c.collctype AS \"%s\"",
3272 gettext_noop("Schema"),
3273 gettext_noop("Name"),
3274 gettext_noop("Collate"),
3275 gettext_noop("Ctype"));
3278 appendPQExpBuffer(&buf,
3279 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3280 gettext_noop("Description"));
3282 appendPQExpBufferStr(&buf,
3283 "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3284 "WHERE n.oid = c.collnamespace\n");
3286 if (!showSystem && !pattern)
3287 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3288 " AND n.nspname <> 'information_schema'\n");
3291 * Hide collations that aren't usable in the current database's encoding.
3292 * If you think to change this, note that pg_collation_is_visible rejects
3293 * unusable collations, so you will need to hack name pattern processing
3294 * somehow to avoid inconsistent behavior.
3296 appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3298 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3299 "n.nspname", "c.collname", NULL,
3300 "pg_catalog.pg_collation_is_visible(c.oid)");
3302 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3304 res = PSQLexec(buf.data, false);
3305 termPQExpBuffer(&buf);
3309 myopt.nullPrint = NULL;
3310 myopt.title = _("List of collations");
3311 myopt.translate_header = true;
3312 myopt.translate_columns = translate_columns;
3313 myopt.n_translate_columns = lengthof(translate_columns);
3315 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3324 * Describes schemas (namespaces)
3327 listSchemas(const char *pattern, bool verbose, bool showSystem)
3329 PQExpBufferData buf;
3331 printQueryOpt myopt = pset.popt;
3333 initPQExpBuffer(&buf);
3334 printfPQExpBuffer(&buf,
3335 "SELECT n.nspname AS \"%s\",\n"
3336 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3337 gettext_noop("Name"),
3338 gettext_noop("Owner"));
3342 appendPQExpBufferStr(&buf, ",\n ");
3343 printACLColumn(&buf, "n.nspacl");
3344 appendPQExpBuffer(&buf,
3345 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3346 gettext_noop("Description"));
3349 appendPQExpBuffer(&buf,
3350 "\nFROM pg_catalog.pg_namespace n\n");
3352 if (!showSystem && !pattern)
3353 appendPQExpBufferStr(&buf,
3354 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3356 processSQLNamePattern(pset.db, &buf, pattern,
3357 !showSystem && !pattern, false,
3358 NULL, "n.nspname", NULL,
3361 appendPQExpBufferStr(&buf, "ORDER BY 1;");
3363 res = PSQLexec(buf.data, false);
3364 termPQExpBuffer(&buf);
3368 myopt.nullPrint = NULL;
3369 myopt.title = _("List of schemas");
3370 myopt.translate_header = true;
3372 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3381 * list text search parsers
3384 listTSParsers(const char *pattern, bool verbose)
3386 PQExpBufferData buf;
3388 printQueryOpt myopt = pset.popt;
3390 if (pset.sversion < 80300)
3392 psql_error("The server (version %d.%d) does not support full text search.\n",
3393 pset.sversion / 10000, (pset.sversion / 100) % 100);
3398 return listTSParsersVerbose(pattern);
3400 initPQExpBuffer(&buf);
3402 printfPQExpBuffer(&buf,
3404 " n.nspname as \"%s\",\n"
3405 " p.prsname as \"%s\",\n"
3406 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3407 "FROM pg_catalog.pg_ts_parser p \n"
3408 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3409 gettext_noop("Schema"),
3410 gettext_noop("Name"),
3411 gettext_noop("Description")
3414 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3415 "n.nspname", "p.prsname", NULL,
3416 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3418 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3420 res = PSQLexec(buf.data, false);
3421 termPQExpBuffer(&buf);
3425 myopt.nullPrint = NULL;
3426 myopt.title = _("List of text search parsers");
3427 myopt.translate_header = true;
3429 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3436 * full description of parsers
3439 listTSParsersVerbose(const char *pattern)
3441 PQExpBufferData buf;
3445 initPQExpBuffer(&buf);
3447 printfPQExpBuffer(&buf,
3451 "FROM pg_catalog.pg_ts_parser p\n"
3452 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3455 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3456 "n.nspname", "p.prsname", NULL,
3457 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3459 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3461 res = PSQLexec(buf.data, false);
3462 termPQExpBuffer(&buf);
3466 if (PQntuples(res) == 0)
3469 psql_error("Did not find any text search parser named \"%s\".\n",
3475 for (i = 0; i < PQntuples(res); i++)
3478 const char *nspname = NULL;
3479 const char *prsname;
3481 oid = PQgetvalue(res, i, 0);
3482 if (!PQgetisnull(res, i, 1))
3483 nspname = PQgetvalue(res, i, 1);
3484 prsname = PQgetvalue(res, i, 2);
3486 if (!describeOneTSParser(oid, nspname, prsname))
3504 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3506 PQExpBufferData buf;
3509 printQueryOpt myopt = pset.popt;
3510 static const bool translate_columns[] = {true, false, false};
3512 initPQExpBuffer(&buf);
3514 printfPQExpBuffer(&buf,
3515 "SELECT '%s' AS \"%s\", \n"
3516 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3517 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3518 " FROM pg_catalog.pg_ts_parser p \n"
3519 " WHERE p.oid = '%s' \n"
3522 " p.prstoken::pg_catalog.regproc, \n"
3523 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3524 " FROM pg_catalog.pg_ts_parser p \n"
3525 " WHERE p.oid = '%s' \n"
3528 " p.prsend::pg_catalog.regproc, \n"
3529 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3530 " FROM pg_catalog.pg_ts_parser p \n"
3531 " WHERE p.oid = '%s' \n"
3534 " p.prsheadline::pg_catalog.regproc, \n"
3535 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3536 " FROM pg_catalog.pg_ts_parser p \n"
3537 " WHERE p.oid = '%s' \n"
3540 " p.prslextype::pg_catalog.regproc, \n"
3541 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3542 " FROM pg_catalog.pg_ts_parser p \n"
3543 " WHERE p.oid = '%s';",
3544 gettext_noop("Start parse"),
3545 gettext_noop("Method"),
3546 gettext_noop("Function"),
3547 gettext_noop("Description"),
3549 gettext_noop("Get next token"),
3551 gettext_noop("End parse"),
3553 gettext_noop("Get headline"),
3555 gettext_noop("Get token types"),
3558 res = PSQLexec(buf.data, false);
3559 termPQExpBuffer(&buf);
3563 myopt.nullPrint = NULL;
3565 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3567 sprintf(title, _("Text search parser \"%s\""), prsname);
3568 myopt.title = title;
3569 myopt.footers = NULL;
3570 myopt.topt.default_footer = false;
3571 myopt.translate_header = true;
3572 myopt.translate_columns = translate_columns;
3573 myopt.n_translate_columns = lengthof(translate_columns);
3575 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3579 initPQExpBuffer(&buf);
3581 printfPQExpBuffer(&buf,
3582 "SELECT t.alias as \"%s\", \n"
3583 " t.description as \"%s\" \n"
3584 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3586 gettext_noop("Token name"),
3587 gettext_noop("Description"),
3590 res = PSQLexec(buf.data, false);
3591 termPQExpBuffer(&buf);
3595 myopt.nullPrint = NULL;
3597 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3599 sprintf(title, _("Token types for parser \"%s\""), prsname);
3600 myopt.title = title;
3601 myopt.footers = NULL;
3602 myopt.topt.default_footer = true;
3603 myopt.translate_header = true;
3604 myopt.translate_columns = NULL;
3605 myopt.n_translate_columns = 0;
3607 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3616 * list text search dictionaries
3619 listTSDictionaries(const char *pattern, bool verbose)
3621 PQExpBufferData buf;
3623 printQueryOpt myopt = pset.popt;
3625 if (pset.sversion < 80300)
3627 psql_error("The server (version %d.%d) does not support full text search.\n",
3628 pset.sversion / 10000, (pset.sversion / 100) % 100);
3632 initPQExpBuffer(&buf);
3634 printfPQExpBuffer(&buf,
3636 " n.nspname as \"%s\",\n"
3637 " d.dictname as \"%s\",\n",
3638 gettext_noop("Schema"),
3639 gettext_noop("Name"));
3643 appendPQExpBuffer(&buf,
3644 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3645 " pg_catalog.pg_ts_template t \n"
3646 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3647 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
3648 " d.dictinitoption as \"%s\", \n",
3649 gettext_noop("Template"),
3650 gettext_noop("Init options"));
3653 appendPQExpBuffer(&buf,
3654 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3655 gettext_noop("Description"));
3657 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3658 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3660 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3661 "n.nspname", "d.dictname", NULL,
3662 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3664 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3666 res = PSQLexec(buf.data, false);
3667 termPQExpBuffer(&buf);
3671 myopt.nullPrint = NULL;
3672 myopt.title = _("List of text search dictionaries");
3673 myopt.translate_header = true;
3675 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3684 * list text search templates
3687 listTSTemplates(const char *pattern, bool verbose)
3689 PQExpBufferData buf;
3691 printQueryOpt myopt = pset.popt;
3693 if (pset.sversion < 80300)
3695 psql_error("The server (version %d.%d) does not support full text search.\n",
3696 pset.sversion / 10000, (pset.sversion / 100) % 100);
3700 initPQExpBuffer(&buf);
3703 printfPQExpBuffer(&buf,
3705 " n.nspname AS \"%s\",\n"
3706 " t.tmplname AS \"%s\",\n"
3707 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3708 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3709 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3710 gettext_noop("Schema"),
3711 gettext_noop("Name"),
3712 gettext_noop("Init"),
3713 gettext_noop("Lexize"),
3714 gettext_noop("Description"));
3716 printfPQExpBuffer(&buf,
3718 " n.nspname AS \"%s\",\n"
3719 " t.tmplname AS \"%s\",\n"
3720 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3721 gettext_noop("Schema"),
3722 gettext_noop("Name"),
3723 gettext_noop("Description"));
3725 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
3726 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3728 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3729 "n.nspname", "t.tmplname", NULL,
3730 "pg_catalog.pg_ts_template_is_visible(t.oid)");
3732 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3734 res = PSQLexec(buf.data, false);
3735 termPQExpBuffer(&buf);
3739 myopt.nullPrint = NULL;
3740 myopt.title = _("List of text search templates");
3741 myopt.translate_header = true;
3743 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3752 * list text search configurations
3755 listTSConfigs(const char *pattern, bool verbose)
3757 PQExpBufferData buf;
3759 printQueryOpt myopt = pset.popt;
3761 if (pset.sversion < 80300)
3763 psql_error("The server (version %d.%d) does not support full text search.\n",
3764 pset.sversion / 10000, (pset.sversion / 100) % 100);
3769 return listTSConfigsVerbose(pattern);
3771 initPQExpBuffer(&buf);
3773 printfPQExpBuffer(&buf,
3775 " n.nspname as \"%s\",\n"
3776 " c.cfgname as \"%s\",\n"
3777 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3778 "FROM pg_catalog.pg_ts_config c\n"
3779 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3780 gettext_noop("Schema"),
3781 gettext_noop("Name"),
3782 gettext_noop("Description")
3785 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3786 "n.nspname", "c.cfgname", NULL,
3787 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3789 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3791 res = PSQLexec(buf.data, false);
3792 termPQExpBuffer(&buf);
3796 myopt.nullPrint = NULL;
3797 myopt.title = _("List of text search configurations");
3798 myopt.translate_header = true;
3800 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3807 listTSConfigsVerbose(const char *pattern)
3809 PQExpBufferData buf;
3813 initPQExpBuffer(&buf);
3815 printfPQExpBuffer(&buf,
3816 "SELECT c.oid, c.cfgname,\n"
3819 " np.nspname as pnspname \n"
3820 "FROM pg_catalog.pg_ts_config c \n"
3821 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3822 " pg_catalog.pg_ts_parser p \n"
3823 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3824 "WHERE p.oid = c.cfgparser\n"
3827 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3828 "n.nspname", "c.cfgname", NULL,
3829 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3831 appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
3833 res = PSQLexec(buf.data, false);
3834 termPQExpBuffer(&buf);
3838 if (PQntuples(res) == 0)
3841 psql_error("Did not find any text search configuration named \"%s\".\n",
3847 for (i = 0; i < PQntuples(res); i++)
3850 const char *cfgname;
3851 const char *nspname = NULL;
3852 const char *prsname;
3853 const char *pnspname = NULL;
3855 oid = PQgetvalue(res, i, 0);
3856 cfgname = PQgetvalue(res, i, 1);
3857 if (!PQgetisnull(res, i, 2))
3858 nspname = PQgetvalue(res, i, 2);
3859 prsname = PQgetvalue(res, i, 3);
3860 if (!PQgetisnull(res, i, 4))
3861 pnspname = PQgetvalue(res, i, 4);
3863 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
3881 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3882 const char *pnspname, const char *prsname)
3884 PQExpBufferData buf,
3887 printQueryOpt myopt = pset.popt;
3889 initPQExpBuffer(&buf);
3891 printfPQExpBuffer(&buf,
3893 " ( SELECT t.alias FROM \n"
3894 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3895 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3896 " pg_catalog.btrim( \n"
3897 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3898 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3899 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3900 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3901 " ) :: pg_catalog.text , \n"
3902 " '{}') AS \"%s\" \n"
3903 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3904 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3905 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3907 gettext_noop("Token"),
3908 gettext_noop("Dictionaries"),
3911 res = PSQLexec(buf.data, false);
3912 termPQExpBuffer(&buf);
3916 initPQExpBuffer(&title);
3919 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3922 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3926 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3929 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3932 myopt.nullPrint = NULL;
3933 myopt.title = title.data;
3934 myopt.footers = NULL;
3935 myopt.topt.default_footer = false;
3936 myopt.translate_header = true;
3938 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3940 termPQExpBuffer(&title);
3950 * Describes foreign-data wrappers
3953 listForeignDataWrappers(const char *pattern, bool verbose)
3955 PQExpBufferData buf;
3957 printQueryOpt myopt = pset.popt;
3959 if (pset.sversion < 80400)
3961 psql_error("The server (version %d.%d) does not support foreign-data wrappers.\n",
3962 pset.sversion / 10000, (pset.sversion / 100) % 100);
3966 initPQExpBuffer(&buf);
3967 printfPQExpBuffer(&buf,
3968 "SELECT fdw.fdwname AS \"%s\",\n"
3969 " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
3970 gettext_noop("Name"),
3971 gettext_noop("Owner"));
3972 if (pset.sversion >= 90100)
3973 appendPQExpBuffer(&buf,
3974 " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
3975 gettext_noop("Handler"));
3976 appendPQExpBuffer(&buf,
3977 " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
3978 gettext_noop("Validator"));
3982 appendPQExpBufferStr(&buf, ",\n ");
3983 printACLColumn(&buf, "fdwacl");
3984 appendPQExpBuffer(&buf,
3985 ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
3986 " '(' || array_to_string(ARRAY(SELECT "
3987 " quote_ident(option_name) || ' ' || "
3988 " quote_literal(option_value) FROM "
3989 " pg_options_to_table(fdwoptions)), ', ') || ')' "
3991 gettext_noop("FDW Options"));
3993 if (pset.sversion >= 90100)
3994 appendPQExpBuffer(&buf,
3995 ",\n d.description AS \"%s\" ",
3996 gettext_noop("Description"));
3999 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
4001 if (verbose && pset.sversion >= 90100)
4002 appendPQExpBufferStr(&buf,
4003 "LEFT JOIN pg_catalog.pg_description d\n"
4004 " ON d.classoid = fdw.tableoid "
4005 "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
4007 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4008 NULL, "fdwname", NULL, NULL);
4010 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4012 res = PSQLexec(buf.data, false);
4013 termPQExpBuffer(&buf);
4017 myopt.nullPrint = NULL;
4018 myopt.title = _("List of foreign-data wrappers");
4019 myopt.translate_header = true;
4021 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4030 * Describes foreign servers.
4033 listForeignServers(const char *pattern, bool verbose)
4035 PQExpBufferData buf;
4037 printQueryOpt myopt = pset.popt;
4039 if (pset.sversion < 80400)
4041 psql_error("The server (version %d.%d) does not support foreign servers.\n",
4042 pset.sversion / 10000, (pset.sversion / 100) % 100);
4046 initPQExpBuffer(&buf);
4047 printfPQExpBuffer(&buf,
4048 "SELECT s.srvname AS \"%s\",\n"
4049 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
4050 " f.fdwname AS \"%s\"",
4051 gettext_noop("Name"),
4052 gettext_noop("Owner"),
4053 gettext_noop("Foreign-data wrapper"));
4057 appendPQExpBufferStr(&buf, ",\n ");
4058 printACLColumn(&buf, "s.srvacl");
4059 appendPQExpBuffer(&buf,
4061 " s.srvtype AS \"%s\",\n"
4062 " s.srvversion AS \"%s\",\n"
4063 " CASE WHEN srvoptions IS NULL THEN '' ELSE "
4064 " '(' || array_to_string(ARRAY(SELECT "
4065 " quote_ident(option_name) || ' ' || "
4066 " quote_literal(option_value) FROM "
4067 " pg_options_to_table(srvoptions)), ', ') || ')' "
4069 " d.description AS \"%s\"",
4070 gettext_noop("Type"),
4071 gettext_noop("Version"),
4072 gettext_noop("FDW Options"),
4073 gettext_noop("Description"));
4076 appendPQExpBufferStr(&buf,
4077 "\nFROM pg_catalog.pg_foreign_server s\n"
4078 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
4081 appendPQExpBufferStr(&buf,
4082 "LEFT JOIN pg_description d\n "
4083 "ON d.classoid = s.tableoid AND d.objoid = s.oid "
4084 "AND d.objsubid = 0\n");
4086 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4087 NULL, "s.srvname", NULL, NULL);
4089 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4091 res = PSQLexec(buf.data, false);
4092 termPQExpBuffer(&buf);
4096 myopt.nullPrint = NULL;
4097 myopt.title = _("List of foreign servers");
4098 myopt.translate_header = true;
4100 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4109 * Describes user mappings.
4112 listUserMappings(const char *pattern, bool verbose)
4114 PQExpBufferData buf;
4116 printQueryOpt myopt = pset.popt;
4118 if (pset.sversion < 80400)
4120 psql_error("The server (version %d.%d) does not support user mappings.\n",
4121 pset.sversion / 10000, (pset.sversion / 100) % 100);
4125 initPQExpBuffer(&buf);
4126 printfPQExpBuffer(&buf,
4127 "SELECT um.srvname AS \"%s\",\n"
4128 " um.usename AS \"%s\"",
4129 gettext_noop("Server"),
4130 gettext_noop("User name"));
4133 appendPQExpBuffer(&buf,
4134 ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4135 " '(' || array_to_string(ARRAY(SELECT "
4136 " quote_ident(option_name) || ' ' || "
4137 " quote_literal(option_value) FROM "
4138 " pg_options_to_table(umoptions)), ', ') || ')' "
4140 gettext_noop("FDW Options"));
4142 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4144 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4145 NULL, "um.srvname", "um.usename", NULL);
4147 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4149 res = PSQLexec(buf.data, false);
4150 termPQExpBuffer(&buf);
4154 myopt.nullPrint = NULL;
4155 myopt.title = _("List of user mappings");
4156 myopt.translate_header = true;
4158 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4167 * Describes foreign tables.
4170 listForeignTables(const char *pattern, bool verbose)
4172 PQExpBufferData buf;
4174 printQueryOpt myopt = pset.popt;
4176 if (pset.sversion < 90100)
4178 psql_error("The server (version %d.%d) does not support foreign tables.\n",
4179 pset.sversion / 10000, (pset.sversion / 100) % 100);
4183 initPQExpBuffer(&buf);
4184 printfPQExpBuffer(&buf,
4185 "SELECT n.nspname AS \"%s\",\n"
4186 " c.relname AS \"%s\",\n"
4187 " s.srvname AS \"%s\"",
4188 gettext_noop("Schema"),
4189 gettext_noop("Table"),
4190 gettext_noop("Server"));
4193 appendPQExpBuffer(&buf,
4194 ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4195 " '(' || array_to_string(ARRAY(SELECT "
4196 " quote_ident(option_name) || ' ' || "
4197 " quote_literal(option_value) FROM "
4198 " pg_options_to_table(ftoptions)), ', ') || ')' "
4200 " d.description AS \"%s\"",
4201 gettext_noop("FDW Options"),
4202 gettext_noop("Description"));
4204 appendPQExpBufferStr(&buf,
4205 "\nFROM pg_catalog.pg_foreign_table ft\n"
4206 " INNER JOIN pg_catalog.pg_class c"
4207 " ON c.oid = ft.ftrelid\n"
4208 " INNER JOIN pg_catalog.pg_namespace n"
4209 " ON n.oid = c.relnamespace\n"
4210 " INNER JOIN pg_catalog.pg_foreign_server s"
4211 " ON s.oid = ft.ftserver\n");
4213 appendPQExpBufferStr(&buf,
4214 " LEFT JOIN pg_catalog.pg_description d\n"
4215 " ON d.classoid = c.tableoid AND "
4216 "d.objoid = c.oid AND d.objsubid = 0\n");
4218 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4219 NULL, "n.nspname", "c.relname", NULL);
4221 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4223 res = PSQLexec(buf.data, false);
4224 termPQExpBuffer(&buf);
4228 myopt.nullPrint = NULL;
4229 myopt.title = _("List of foreign tables");
4230 myopt.translate_header = true;
4232 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4241 * Briefly describes installed extensions.
4244 listExtensions(const char *pattern)
4246 PQExpBufferData buf;
4248 printQueryOpt myopt = pset.popt;
4250 if (pset.sversion < 90100)
4252 psql_error("The server (version %d.%d) does not support extensions.\n",
4253 pset.sversion / 10000, (pset.sversion / 100) % 100);
4257 initPQExpBuffer(&buf);
4258 printfPQExpBuffer(&buf,
4259 "SELECT e.extname AS \"%s\", "
4260 "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4261 "FROM pg_catalog.pg_extension e "
4262 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4263 "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4264 "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4265 gettext_noop("Name"),
4266 gettext_noop("Version"),
4267 gettext_noop("Schema"),
4268 gettext_noop("Description"));
4270 processSQLNamePattern(pset.db, &buf, pattern,
4272 NULL, "e.extname", NULL,
4275 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4277 res = PSQLexec(buf.data, false);
4278 termPQExpBuffer(&buf);
4282 myopt.nullPrint = NULL;
4283 myopt.title = _("List of installed extensions");
4284 myopt.translate_header = true;
4286 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4295 * List contents of installed extensions.
4298 listExtensionContents(const char *pattern)
4300 PQExpBufferData buf;
4304 if (pset.sversion < 90100)
4306 psql_error("The server (version %d.%d) does not support extensions.\n",
4307 pset.sversion / 10000, (pset.sversion / 100) % 100);
4311 initPQExpBuffer(&buf);
4312 printfPQExpBuffer(&buf,
4313 "SELECT e.extname, e.oid\n"
4314 "FROM pg_catalog.pg_extension e\n");
4316 processSQLNamePattern(pset.db, &buf, pattern,
4318 NULL, "e.extname", NULL,
4321 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4323 res = PSQLexec(buf.data, false);
4324 termPQExpBuffer(&buf);
4328 if (PQntuples(res) == 0)
4333 psql_error("Did not find any extension named \"%s\".\n",
4336 psql_error("Did not find any extensions.\n");
4342 for (i = 0; i < PQntuples(res); i++)
4344 const char *extname;
4347 extname = PQgetvalue(res, i, 0);
4348 oid = PQgetvalue(res, i, 1);
4350 if (!listOneExtensionContents(extname, oid))
4367 listOneExtensionContents(const char *extname, const char *oid)
4369 PQExpBufferData buf;
4372 printQueryOpt myopt = pset.popt;
4374 initPQExpBuffer(&buf);
4375 printfPQExpBuffer(&buf,
4376 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4377 "FROM pg_catalog.pg_depend\n"
4378 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4380 gettext_noop("Object Description"),
4383 res = PSQLexec(buf.data, false);
4384 termPQExpBuffer(&buf);
4388 myopt.nullPrint = NULL;
4389 snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4390 myopt.title = title;
4391 myopt.translate_header = true;
4393 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4402 * Helper function for consistently formatting ACL (privilege) columns.
4403 * The proper targetlist entry is appended to buf. Note lack of any
4404 * whitespace or comma decoration.
4407 printACLColumn(PQExpBuffer buf, const char *colname)
4409 if (pset.sversion >= 80100)
4410 appendPQExpBuffer(buf,
4411 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4412 colname, gettext_noop("Access privileges"));
4414 appendPQExpBuffer(buf,
4415 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4416 colname, gettext_noop("Access privileges"));