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-2013, 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 >= 80200)
76 appendPQExpBuffer(&buf,
77 " CASE WHEN p.pronargs = 0\n"
78 " THEN CAST('*' AS pg_catalog.text)\n"
80 " pg_catalog.array_to_string(ARRAY(\n"
82 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
84 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
87 gettext_noop("Argument data types"));
89 appendPQExpBuffer(&buf,
90 " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
91 gettext_noop("Argument data types"));
93 appendPQExpBuffer(&buf,
94 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
95 "FROM pg_catalog.pg_proc p\n"
96 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
98 gettext_noop("Description"));
100 if (!showSystem && !pattern)
101 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
102 " AND n.nspname <> 'information_schema'\n");
104 processSQLNamePattern(pset.db, &buf, pattern, true, false,
105 "n.nspname", "p.proname", NULL,
106 "pg_catalog.pg_function_is_visible(p.oid)");
108 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
110 res = PSQLexec(buf.data, false);
111 termPQExpBuffer(&buf);
115 myopt.nullPrint = NULL;
116 myopt.title = _("List of aggregate functions");
117 myopt.translate_header = true;
119 printQuery(res, &myopt, pset.queryFout, pset.logfile);
126 * Takes an optional regexp to select particular tablespaces
129 describeTablespaces(const char *pattern, bool verbose)
133 printQueryOpt myopt = pset.popt;
135 if (pset.sversion < 80000)
137 psql_error("The server (version %d.%d) does not support tablespaces.\n",
138 pset.sversion / 10000, (pset.sversion / 100) % 100);
142 initPQExpBuffer(&buf);
144 if (pset.sversion >= 90200)
145 printfPQExpBuffer(&buf,
146 "SELECT spcname AS \"%s\",\n"
147 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
148 " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
149 gettext_noop("Name"),
150 gettext_noop("Owner"),
151 gettext_noop("Location"));
153 printfPQExpBuffer(&buf,
154 "SELECT spcname AS \"%s\",\n"
155 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
156 " spclocation AS \"%s\"",
157 gettext_noop("Name"),
158 gettext_noop("Owner"),
159 gettext_noop("Location"));
163 appendPQExpBuffer(&buf, ",\n ");
164 printACLColumn(&buf, "spcacl");
167 if (verbose && pset.sversion >= 80200)
168 appendPQExpBuffer(&buf,
169 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
170 gettext_noop("Description"));
172 appendPQExpBuffer(&buf,
173 "\nFROM pg_catalog.pg_tablespace\n");
175 processSQLNamePattern(pset.db, &buf, pattern, false, false,
176 NULL, "spcname", NULL,
179 appendPQExpBuffer(&buf, "ORDER BY 1;");
181 res = PSQLexec(buf.data, false);
182 termPQExpBuffer(&buf);
186 myopt.nullPrint = NULL;
187 myopt.title = _("List of tablespaces");
188 myopt.translate_header = true;
190 printQuery(res, &myopt, pset.queryFout, pset.logfile);
198 * Takes an optional regexp to select particular functions.
200 * As with \d, you can specify the kinds of functions you want:
207 * and you can mix and match these in any order.
210 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
212 bool showAggregate = strchr(functypes, 'a') != NULL;
213 bool showNormal = strchr(functypes, 'n') != NULL;
214 bool showTrigger = strchr(functypes, 't') != NULL;
215 bool showWindow = strchr(functypes, 'w') != NULL;
219 printQueryOpt myopt = pset.popt;
220 static const bool translate_columns[] = {false, false, false, false, true, true, false, false, false, false};
222 if (strlen(functypes) != strspn(functypes, "antwS+"))
224 psql_error("\\df only takes [antwS+] as options\n");
228 if (showWindow && pset.sversion < 80400)
230 psql_error("\\df does not take a \"w\" option with server version %d.%d\n",
231 pset.sversion / 10000, (pset.sversion / 100) % 100);
235 if (!showAggregate && !showNormal && !showTrigger && !showWindow)
237 showAggregate = showNormal = showTrigger = true;
238 if (pset.sversion >= 80400)
242 initPQExpBuffer(&buf);
244 printfPQExpBuffer(&buf,
245 "SELECT n.nspname as \"%s\",\n"
246 " p.proname as \"%s\",\n",
247 gettext_noop("Schema"),
248 gettext_noop("Name"));
250 if (pset.sversion >= 80400)
251 appendPQExpBuffer(&buf,
252 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
253 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
255 " WHEN p.proisagg THEN '%s'\n"
256 " WHEN p.proiswindow THEN '%s'\n"
257 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
260 gettext_noop("Result data type"),
261 gettext_noop("Argument data types"),
262 /* translator: "agg" is short for "aggregate" */
264 gettext_noop("window"),
265 gettext_noop("trigger"),
266 gettext_noop("normal"),
267 gettext_noop("Type"));
268 else if (pset.sversion >= 80100)
269 appendPQExpBuffer(&buf,
270 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
271 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
272 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
273 " pg_catalog.array_to_string(ARRAY(\n"
276 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
277 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
278 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
279 " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
282 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
283 " ELSE p.proargnames[s.i] || ' ' \n"
285 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
287 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
290 " pg_catalog.array_to_string(ARRAY(\n"
293 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
294 " ELSE p.proargnames[s.i+1] || ' '\n"
296 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
298 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
302 " WHEN p.proisagg THEN '%s'\n"
303 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
306 gettext_noop("Result data type"),
307 gettext_noop("Argument data types"),
308 /* translator: "agg" is short for "aggregate" */
310 gettext_noop("trigger"),
311 gettext_noop("normal"),
312 gettext_noop("Type"));
314 appendPQExpBuffer(&buf,
315 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
316 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
317 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
319 " WHEN p.proisagg THEN '%s'\n"
320 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
323 gettext_noop("Result data type"),
324 gettext_noop("Argument data types"),
325 /* translator: "agg" is short for "aggregate" */
327 gettext_noop("trigger"),
328 gettext_noop("normal"),
329 gettext_noop("Type"));
332 appendPQExpBuffer(&buf,
333 ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\""
335 " WHEN p.provolatile = 'i' THEN '%s'\n"
336 " WHEN p.provolatile = 's' THEN '%s'\n"
337 " WHEN p.provolatile = 'v' THEN '%s'\n"
339 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
340 " l.lanname as \"%s\",\n"
341 " p.prosrc as \"%s\",\n"
342 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
343 gettext_noop("definer"),
344 gettext_noop("invoker"),
345 gettext_noop("Security"),
346 gettext_noop("immutable"),
347 gettext_noop("stable"),
348 gettext_noop("volatile"),
349 gettext_noop("Volatility"),
350 gettext_noop("Owner"),
351 gettext_noop("Language"),
352 gettext_noop("Source code"),
353 gettext_noop("Description"));
355 appendPQExpBuffer(&buf,
356 "\nFROM pg_catalog.pg_proc p"
357 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
360 appendPQExpBuffer(&buf,
361 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
365 /* filter by function type, if requested */
366 if (showNormal && showAggregate && showTrigger && showWindow)
373 appendPQExpBuffer(&buf, " AND ");
376 appendPQExpBuffer(&buf, "WHERE ");
379 appendPQExpBuffer(&buf, "NOT p.proisagg\n");
384 appendPQExpBuffer(&buf, " AND ");
387 appendPQExpBuffer(&buf, "WHERE ");
390 appendPQExpBuffer(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
392 if (!showWindow && pset.sversion >= 80400)
395 appendPQExpBuffer(&buf, " AND ");
398 appendPQExpBuffer(&buf, "WHERE ");
401 appendPQExpBuffer(&buf, "NOT p.proiswindow\n");
406 bool needs_or = false;
408 appendPQExpBuffer(&buf, "WHERE (\n ");
410 /* Note: at least one of these must be true ... */
413 appendPQExpBuffer(&buf, "p.proisagg\n");
419 appendPQExpBuffer(&buf, " OR ");
420 appendPQExpBuffer(&buf,
421 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
427 appendPQExpBuffer(&buf, " OR ");
428 appendPQExpBuffer(&buf, "p.proiswindow\n");
431 appendPQExpBuffer(&buf, " )\n");
434 processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
435 "n.nspname", "p.proname", NULL,
436 "pg_catalog.pg_function_is_visible(p.oid)");
438 if (!showSystem && !pattern)
439 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
440 " AND n.nspname <> 'information_schema'\n");
442 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
444 res = PSQLexec(buf.data, false);
445 termPQExpBuffer(&buf);
449 myopt.nullPrint = NULL;
450 myopt.title = _("List of functions");
451 myopt.translate_header = true;
452 myopt.translate_columns = translate_columns;
454 printQuery(res, &myopt, pset.queryFout, pset.logfile);
467 describeTypes(const char *pattern, bool verbose, bool showSystem)
471 printQueryOpt myopt = pset.popt;
473 initPQExpBuffer(&buf);
475 printfPQExpBuffer(&buf,
476 "SELECT n.nspname as \"%s\",\n"
477 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
478 gettext_noop("Schema"),
479 gettext_noop("Name"));
481 appendPQExpBuffer(&buf,
482 " t.typname AS \"%s\",\n"
483 " CASE WHEN t.typrelid != 0\n"
484 " THEN CAST('tuple' AS pg_catalog.text)\n"
485 " WHEN t.typlen < 0\n"
486 " THEN CAST('var' AS pg_catalog.text)\n"
487 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
489 gettext_noop("Internal name"),
490 gettext_noop("Size"));
491 if (verbose && pset.sversion >= 80300)
493 appendPQExpBuffer(&buf,
494 " pg_catalog.array_to_string(\n"
496 " SELECT e.enumlabel\n"
497 " FROM pg_catalog.pg_enum e\n"
498 " WHERE e.enumtypid = t.oid\n");
500 if (pset.sversion >= 90100)
501 appendPQExpBuffer(&buf,
502 " ORDER BY e.enumsortorder\n");
504 appendPQExpBuffer(&buf,
505 " ORDER BY e.oid\n");
507 appendPQExpBuffer(&buf,
511 gettext_noop("Elements"));
513 if (verbose && pset.sversion >= 90200)
515 printACLColumn(&buf, "t.typacl");
516 appendPQExpBuffer(&buf, ",\n ");
519 appendPQExpBuffer(&buf,
520 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
521 gettext_noop("Description"));
523 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
524 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
527 * do not include complex types (typrelid!=0) unless they are standalone
530 appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
531 appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
532 "WHERE c.oid = t.typrelid))\n");
535 * do not include array types (before 8.3 we have to use the assumption
536 * that their names start with underscore)
538 if (pset.sversion >= 80300)
539 appendPQExpBuffer(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
541 appendPQExpBuffer(&buf, " AND t.typname !~ '^_'\n");
543 if (!showSystem && !pattern)
544 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
545 " AND n.nspname <> 'information_schema'\n");
547 /* Match name pattern against either internal or external name */
548 processSQLNamePattern(pset.db, &buf, pattern, true, false,
549 "n.nspname", "t.typname",
550 "pg_catalog.format_type(t.oid, NULL)",
551 "pg_catalog.pg_type_is_visible(t.oid)");
553 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
555 res = PSQLexec(buf.data, false);
556 termPQExpBuffer(&buf);
560 myopt.nullPrint = NULL;
561 myopt.title = _("List of data types");
562 myopt.translate_header = true;
564 printQuery(res, &myopt, pset.queryFout, pset.logfile);
574 describeOperators(const char *pattern, bool showSystem)
578 printQueryOpt myopt = pset.popt;
580 initPQExpBuffer(&buf);
583 * Note: before Postgres 9.1, we did not assign comments to any built-in
584 * operators, preferring to let the comment on the underlying function
585 * suffice. The coalesce() on the obj_description() calls below supports
586 * this convention by providing a fallback lookup of a comment on the
587 * operator's function. As of 9.1 there is a policy that every built-in
588 * operator should have a comment; so the coalesce() is no longer
589 * necessary so far as built-in operators are concerned. We keep it
590 * anyway, for now, because (1) third-party modules may still be following
591 * the old convention, and (2) we'd need to do it anyway when talking to a
595 printfPQExpBuffer(&buf,
596 "SELECT n.nspname as \"%s\",\n"
597 " o.oprname AS \"%s\",\n"
598 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
599 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
600 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
601 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
602 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
603 "FROM pg_catalog.pg_operator o\n"
604 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
605 gettext_noop("Schema"),
606 gettext_noop("Name"),
607 gettext_noop("Left arg type"),
608 gettext_noop("Right arg type"),
609 gettext_noop("Result type"),
610 gettext_noop("Description"));
612 if (!showSystem && !pattern)
613 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
614 " AND n.nspname <> 'information_schema'\n");
616 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
617 "n.nspname", "o.oprname", NULL,
618 "pg_catalog.pg_operator_is_visible(o.oid)");
620 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
622 res = PSQLexec(buf.data, false);
623 termPQExpBuffer(&buf);
627 myopt.nullPrint = NULL;
628 myopt.title = _("List of operators");
629 myopt.translate_header = true;
631 printQuery(res, &myopt, pset.queryFout, pset.logfile);
641 * for \l, \list, and -l switch
644 listAllDbs(bool verbose)
648 printQueryOpt myopt = pset.popt;
650 initPQExpBuffer(&buf);
652 printfPQExpBuffer(&buf,
653 "SELECT d.datname as \"%s\",\n"
654 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
655 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
656 gettext_noop("Name"),
657 gettext_noop("Owner"),
658 gettext_noop("Encoding"));
659 if (pset.sversion >= 80400)
660 appendPQExpBuffer(&buf,
661 " d.datcollate as \"%s\",\n"
662 " d.datctype as \"%s\",\n",
663 gettext_noop("Collate"),
664 gettext_noop("Ctype"));
665 appendPQExpBuffer(&buf, " ");
666 printACLColumn(&buf, "d.datacl");
667 if (verbose && pset.sversion >= 80200)
668 appendPQExpBuffer(&buf,
669 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
670 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
671 " ELSE 'No Access'\n"
673 gettext_noop("Size"));
674 if (verbose && pset.sversion >= 80000)
675 appendPQExpBuffer(&buf,
676 ",\n t.spcname as \"%s\"",
677 gettext_noop("Tablespace"));
678 if (verbose && pset.sversion >= 80200)
679 appendPQExpBuffer(&buf,
680 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
681 gettext_noop("Description"));
682 appendPQExpBuffer(&buf,
683 "\nFROM pg_catalog.pg_database d\n");
684 if (verbose && pset.sversion >= 80000)
685 appendPQExpBuffer(&buf,
686 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
687 appendPQExpBuffer(&buf, "ORDER BY 1;");
688 res = PSQLexec(buf.data, false);
689 termPQExpBuffer(&buf);
693 myopt.nullPrint = NULL;
694 myopt.title = _("List of databases");
695 myopt.translate_header = true;
697 printQuery(res, &myopt, pset.queryFout, pset.logfile);
705 * List Tables' Grant/Revoke Permissions
706 * \z (now also \dp -- perhaps more mnemonic)
709 permissionsList(const char *pattern)
713 printQueryOpt myopt = pset.popt;
714 static const bool translate_columns[] = {false, false, true, false, false};
716 initPQExpBuffer(&buf);
719 * we ignore indexes and toast tables since they have no meaningful rights
721 printfPQExpBuffer(&buf,
722 "SELECT n.nspname as \"%s\",\n"
723 " c.relname as \"%s\",\n"
724 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
726 gettext_noop("Schema"),
727 gettext_noop("Name"),
728 gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
729 gettext_noop("foreign table"),
730 gettext_noop("Type"));
732 printACLColumn(&buf, "c.relacl");
734 if (pset.sversion >= 80400)
735 appendPQExpBuffer(&buf,
736 ",\n pg_catalog.array_to_string(ARRAY(\n"
737 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
738 " FROM pg_catalog.pg_attribute a\n"
739 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
740 " ), E'\\n') AS \"%s\"",
741 gettext_noop("Column access privileges"));
743 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_class c\n"
744 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
745 "WHERE c.relkind IN ('r', 'v', 'S', 'f')\n");
748 * Unless a schema pattern is specified, we suppress system and temp
749 * tables, since they normally aren't very interesting from a permissions
750 * point of view. You can see 'em by explicit request though, eg with \z
753 processSQLNamePattern(pset.db, &buf, pattern, true, false,
754 "n.nspname", "c.relname", NULL,
755 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
757 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
759 res = PSQLexec(buf.data, false);
762 termPQExpBuffer(&buf);
766 myopt.nullPrint = NULL;
767 printfPQExpBuffer(&buf, _("Access privileges"));
768 myopt.title = buf.data;
769 myopt.translate_header = true;
770 myopt.translate_columns = translate_columns;
772 printQuery(res, &myopt, pset.queryFout, pset.logfile);
774 termPQExpBuffer(&buf);
783 * List Default ACLs. The pattern can match either schema or role name.
786 listDefaultACLs(const char *pattern)
790 printQueryOpt myopt = pset.popt;
791 static const bool translate_columns[] = {false, false, true, false};
793 if (pset.sversion < 90000)
795 psql_error("The server (version %d.%d) does not support altering default privileges.\n",
796 pset.sversion / 10000, (pset.sversion / 100) % 100);
800 initPQExpBuffer(&buf);
802 printfPQExpBuffer(&buf,
803 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
804 " n.nspname AS \"%s\",\n"
805 " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
807 gettext_noop("Owner"),
808 gettext_noop("Schema"),
810 gettext_noop("table"),
812 gettext_noop("sequence"),
814 gettext_noop("function"),
816 gettext_noop("type"),
817 gettext_noop("Type"));
819 printACLColumn(&buf, "d.defaclacl");
821 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
822 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
824 processSQLNamePattern(pset.db, &buf, pattern, false, false,
827 "pg_catalog.pg_get_userbyid(d.defaclrole)",
830 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
832 res = PSQLexec(buf.data, false);
835 termPQExpBuffer(&buf);
839 myopt.nullPrint = NULL;
840 printfPQExpBuffer(&buf, _("Default access privileges"));
841 myopt.title = buf.data;
842 myopt.translate_header = true;
843 myopt.translate_columns = translate_columns;
845 printQuery(res, &myopt, pset.queryFout, pset.logfile);
847 termPQExpBuffer(&buf);
854 * Get object comments
858 * Note: This command only lists comments for object types which do not have
859 * their comments displayed by their own backslash commands. The following
860 * types of objects will be displayed: constraint, operator class,
861 * operator family, rule, and trigger.
865 objectDescription(const char *pattern, bool showSystem)
869 printQueryOpt myopt = pset.popt;
870 static const bool translate_columns[] = {false, false, true, false};
872 initPQExpBuffer(&buf);
874 appendPQExpBuffer(&buf,
875 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
877 gettext_noop("Schema"),
878 gettext_noop("Name"),
879 gettext_noop("Object"),
880 gettext_noop("Description"));
882 /* Constraint descriptions */
883 appendPQExpBuffer(&buf,
884 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
885 " n.nspname as nspname,\n"
886 " CAST(pgc.conname AS pg_catalog.text) as name,"
887 " CAST('%s' AS pg_catalog.text) as object\n"
888 " FROM pg_catalog.pg_constraint pgc\n"
889 " JOIN pg_catalog.pg_class c "
890 "ON c.oid = pgc.conrelid\n"
891 " LEFT JOIN pg_catalog.pg_namespace n "
892 " ON n.oid = c.relnamespace\n",
893 gettext_noop("constraint"));
895 if (!showSystem && !pattern)
896 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
897 " AND n.nspname <> 'information_schema'\n");
899 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
900 false, "n.nspname", "pgc.conname", NULL,
901 "pg_catalog.pg_table_is_visible(c.oid)");
904 * pg_opclass.opcmethod only available in 8.3+
906 if (pset.sversion >= 80300)
908 /* Operator class descriptions */
909 appendPQExpBuffer(&buf,
911 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
912 " n.nspname as nspname,\n"
913 " CAST(o.opcname AS pg_catalog.text) as name,\n"
914 " CAST('%s' AS pg_catalog.text) as object\n"
915 " FROM pg_catalog.pg_opclass o\n"
916 " JOIN pg_catalog.pg_am am ON "
917 "o.opcmethod = am.oid\n"
918 " JOIN pg_catalog.pg_namespace n ON "
919 "n.oid = o.opcnamespace\n",
920 gettext_noop("operator class"));
922 if (!showSystem && !pattern)
923 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
924 " AND n.nspname <> 'information_schema'\n");
926 processSQLNamePattern(pset.db, &buf, pattern, true, false,
927 "n.nspname", "o.opcname", NULL,
928 "pg_catalog.pg_opclass_is_visible(o.oid)");
932 * although operator family comments have been around since 8.3,
933 * pg_opfamily_is_visible is only available in 9.2+
935 if (pset.sversion >= 90200)
937 /* Operator family descriptions */
938 appendPQExpBuffer(&buf,
940 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
941 " n.nspname as nspname,\n"
942 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
943 " CAST('%s' AS pg_catalog.text) as object\n"
944 " FROM pg_catalog.pg_opfamily opf\n"
945 " JOIN pg_catalog.pg_am am "
946 "ON opf.opfmethod = am.oid\n"
947 " JOIN pg_catalog.pg_namespace n "
948 "ON opf.opfnamespace = n.oid\n",
949 gettext_noop("operator family"));
951 if (!showSystem && !pattern)
952 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
953 " AND n.nspname <> 'information_schema'\n");
955 processSQLNamePattern(pset.db, &buf, pattern, true, false,
956 "n.nspname", "opf.opfname", NULL,
957 "pg_catalog.pg_opfamily_is_visible(opf.oid)");
960 /* Rule descriptions (ignore rules for views) */
961 appendPQExpBuffer(&buf,
963 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
964 " n.nspname as nspname,\n"
965 " CAST(r.rulename AS pg_catalog.text) as name,"
966 " CAST('%s' AS pg_catalog.text) as object\n"
967 " FROM pg_catalog.pg_rewrite r\n"
968 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
969 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
970 " WHERE r.rulename != '_RETURN'\n",
971 gettext_noop("rule"));
973 if (!showSystem && !pattern)
974 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
975 " AND n.nspname <> 'information_schema'\n");
977 processSQLNamePattern(pset.db, &buf, pattern, true, false,
978 "n.nspname", "r.rulename", NULL,
979 "pg_catalog.pg_table_is_visible(c.oid)");
981 /* Trigger descriptions */
982 appendPQExpBuffer(&buf,
984 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
985 " n.nspname as nspname,\n"
986 " CAST(t.tgname AS pg_catalog.text) as name,"
987 " CAST('%s' AS pg_catalog.text) as object\n"
988 " FROM pg_catalog.pg_trigger t\n"
989 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
990 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
991 gettext_noop("trigger"));
993 if (!showSystem && !pattern)
994 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
995 " AND n.nspname <> 'information_schema'\n");
997 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
998 "n.nspname", "t.tgname", NULL,
999 "pg_catalog.pg_table_is_visible(c.oid)");
1001 appendPQExpBuffer(&buf,
1003 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1005 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
1007 res = PSQLexec(buf.data, false);
1008 termPQExpBuffer(&buf);
1012 myopt.nullPrint = NULL;
1013 myopt.title = _("Object descriptions");
1014 myopt.translate_header = true;
1015 myopt.translate_columns = translate_columns;
1017 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1025 * describeTableDetails (for \d)
1027 * This routine finds the tables to be displayed, and calls
1028 * describeOneTableDetails for each one.
1030 * verbose: if true, this is \d+
1033 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1035 PQExpBufferData buf;
1039 initPQExpBuffer(&buf);
1041 printfPQExpBuffer(&buf,
1045 "FROM pg_catalog.pg_class c\n"
1046 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1048 if (!showSystem && !pattern)
1049 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1050 " AND n.nspname <> 'information_schema'\n");
1052 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1053 "n.nspname", "c.relname", NULL,
1054 "pg_catalog.pg_table_is_visible(c.oid)");
1056 appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
1058 res = PSQLexec(buf.data, false);
1059 termPQExpBuffer(&buf);
1063 if (PQntuples(res) == 0)
1066 psql_error("Did not find any relation named \"%s\".\n",
1072 for (i = 0; i < PQntuples(res); i++)
1075 const char *nspname;
1076 const char *relname;
1078 oid = PQgetvalue(res, i, 0);
1079 nspname = PQgetvalue(res, i, 1);
1080 relname = PQgetvalue(res, i, 2);
1082 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1099 * describeOneTableDetails (for \d)
1101 * Unfortunately, the information presented here is so complicated that it
1102 * cannot be done in a single query. So we have to assemble the printed table
1103 * by hand and pass it to the underlying printTable() function.
1106 describeOneTableDetails(const char *schemaname,
1107 const char *relationname,
1111 PQExpBufferData buf;
1112 PGresult *res = NULL;
1113 printTableOpt myopt = pset.popt.topt;
1114 printTableContent cont;
1115 bool printTableInitialized = false;
1117 char *view_def = NULL;
1119 char **seq_values = NULL;
1120 char **modifiers = NULL;
1122 PQExpBufferData title;
1123 PQExpBufferData tmpbuf;
1137 char relpersistence;
1139 bool show_modifiers = false;
1144 myopt.default_footer = false;
1145 /* This output looks confusing in expanded mode. */
1146 myopt.expanded = false;
1148 initPQExpBuffer(&buf);
1149 initPQExpBuffer(&title);
1150 initPQExpBuffer(&tmpbuf);
1152 /* Get general table info */
1153 if (pset.sversion >= 90100)
1155 printfPQExpBuffer(&buf,
1156 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1157 "c.relhastriggers, c.relhasoids, "
1158 "%s, c.reltablespace, "
1159 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1160 "c.relpersistence\n"
1161 "FROM pg_catalog.pg_class c\n "
1162 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1163 "WHERE c.oid = '%s';",
1165 "pg_catalog.array_to_string(c.reloptions || "
1166 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1170 else if (pset.sversion >= 90000)
1172 printfPQExpBuffer(&buf,
1173 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1174 "c.relhastriggers, c.relhasoids, "
1175 "%s, c.reltablespace, "
1176 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1177 "FROM pg_catalog.pg_class c\n "
1178 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1179 "WHERE c.oid = '%s';",
1181 "pg_catalog.array_to_string(c.reloptions || "
1182 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1186 else if (pset.sversion >= 80400)
1188 printfPQExpBuffer(&buf,
1189 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1190 "c.relhastriggers, c.relhasoids, "
1191 "%s, c.reltablespace\n"
1192 "FROM pg_catalog.pg_class c\n "
1193 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1194 "WHERE c.oid = '%s';",
1196 "pg_catalog.array_to_string(c.reloptions || "
1197 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1201 else if (pset.sversion >= 80200)
1203 printfPQExpBuffer(&buf,
1204 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1205 "reltriggers <> 0, relhasoids, "
1206 "%s, reltablespace\n"
1207 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1209 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1212 else if (pset.sversion >= 80000)
1214 printfPQExpBuffer(&buf,
1215 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1216 "reltriggers <> 0, relhasoids, "
1217 "'', reltablespace\n"
1218 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1223 printfPQExpBuffer(&buf,
1224 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1225 "reltriggers <> 0, relhasoids, "
1227 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1231 res = PSQLexec(buf.data, false);
1235 /* Did we get anything? */
1236 if (PQntuples(res) == 0)
1239 psql_error("Did not find any relation with OID %s.\n", oid);
1243 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1244 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1245 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1246 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1247 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1248 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1249 tableinfo.reloptions = (pset.sversion >= 80200) ?
1250 pg_strdup(PQgetvalue(res, 0, 6)) : NULL;
1251 tableinfo.tablespace = (pset.sversion >= 80000) ?
1252 atooid(PQgetvalue(res, 0, 7)) : 0;
1253 tableinfo.reloftype = (pset.sversion >= 90000 &&
1254 strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
1255 pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
1256 tableinfo.relpersistence = (pset.sversion >= 90100) ?
1257 *(PQgetvalue(res, 0, 9)) : 0;
1262 * If it's a sequence, fetch its values and store into an array that will
1265 if (tableinfo.relkind == 'S')
1267 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1268 /* must be separate because fmtId isn't reentrant */
1269 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1271 res = PSQLexec(buf.data, false);
1275 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1277 for (i = 0; i < PQnfields(res); i++)
1278 seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1279 seq_values[i] = NULL;
1288 * You need to modify value of "firstvcol" which will be defined below if
1289 * you are adding column(s) preceding to verbose-only columns.
1291 printfPQExpBuffer(&buf, "SELECT a.attname,");
1292 appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1293 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1294 "\n FROM pg_catalog.pg_attrdef d"
1295 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1296 "\n a.attnotnull, a.attnum,");
1297 if (pset.sversion >= 90100)
1298 appendPQExpBuffer(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1299 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1301 appendPQExpBuffer(&buf, "\n NULL AS attcollation");
1302 if (tableinfo.relkind == 'i')
1303 appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1305 appendPQExpBuffer(&buf, ",\n NULL AS indexdef");
1306 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1307 appendPQExpBuffer(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1308 " '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1309 " pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1311 appendPQExpBuffer(&buf, ",\n NULL AS attfdwoptions");
1314 appendPQExpBuffer(&buf, ",\n a.attstorage");
1315 appendPQExpBuffer(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1318 * In 9.0+, we have column comments for: relations, views, composite
1319 * types, and foreign tables (c.f. CommentObject() in comment.c).
1321 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1322 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1323 appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1326 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
1327 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1328 appendPQExpBuffer(&buf, "\nORDER BY a.attnum;");
1330 res = PSQLexec(buf.data, false);
1333 numrows = PQntuples(res);
1336 switch (tableinfo.relkind)
1339 if (tableinfo.relpersistence == 'u')
1340 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1341 schemaname, relationname);
1343 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1344 schemaname, relationname);
1347 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1348 schemaname, relationname);
1351 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1352 schemaname, relationname);
1355 if (tableinfo.relpersistence == 'u')
1356 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1357 schemaname, relationname);
1359 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1360 schemaname, relationname);
1363 /* not used as of 8.2, but keep it for backwards compatibility */
1364 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1365 schemaname, relationname);
1368 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1369 schemaname, relationname);
1372 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1373 schemaname, relationname);
1376 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1377 schemaname, relationname);
1380 /* untranslated unknown relkind */
1381 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1382 tableinfo.relkind, schemaname, relationname);
1386 /* Set the number of columns, and their names */
1387 headers[0] = gettext_noop("Column");
1388 headers[1] = gettext_noop("Type");
1391 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1392 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1394 show_modifiers = true;
1395 headers[cols++] = gettext_noop("Modifiers");
1396 modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers));
1399 if (tableinfo.relkind == 'S')
1400 headers[cols++] = gettext_noop("Value");
1402 if (tableinfo.relkind == 'i')
1403 headers[cols++] = gettext_noop("Definition");
1405 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1406 headers[cols++] = gettext_noop("FDW Options");
1410 headers[cols++] = gettext_noop("Storage");
1411 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1412 headers[cols++] = gettext_noop("Stats target");
1413 /* Column comments, if the relkind supports this feature. */
1414 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1415 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1416 headers[cols++] = gettext_noop("Description");
1419 printTableInit(&cont, &myopt, title.data, cols, numrows);
1420 printTableInitialized = true;
1422 for (i = 0; i < cols; i++)
1423 printTableAddHeader(&cont, headers[i], true, 'l');
1425 /* Check if table is a view */
1426 if (tableinfo.relkind == 'v' && verbose)
1430 printfPQExpBuffer(&buf,
1431 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1433 result = PSQLexec(buf.data, false);
1437 if (PQntuples(result) > 0)
1438 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1443 /* Generate table cells to be printed */
1444 for (i = 0; i < numrows; i++)
1447 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1450 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1452 /* Modifiers: collate, not null, default */
1455 resetPQExpBuffer(&tmpbuf);
1457 if (!PQgetisnull(res, i, 5))
1460 appendPQExpBufferStr(&tmpbuf, " ");
1461 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1462 PQgetvalue(res, i, 5));
1465 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1468 appendPQExpBufferStr(&tmpbuf, " ");
1469 appendPQExpBufferStr(&tmpbuf, _("not null"));
1472 /* handle "default" here */
1473 /* (note: above we cut off the 'default' string at 128) */
1474 if (strlen(PQgetvalue(res, i, 2)) != 0)
1477 appendPQExpBufferStr(&tmpbuf, " ");
1478 /* translator: default values of column definitions */
1479 appendPQExpBuffer(&tmpbuf, _("default %s"),
1480 PQgetvalue(res, i, 2));
1483 modifiers[i] = pg_strdup(tmpbuf.data);
1484 printTableAddCell(&cont, modifiers[i], false, false);
1487 /* Value: for sequences only */
1488 if (tableinfo.relkind == 'S')
1489 printTableAddCell(&cont, seq_values[i], false, false);
1491 /* Expression for index column */
1492 if (tableinfo.relkind == 'i')
1493 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1495 /* FDW options for foreign table column, only for 9.2 or later */
1496 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1497 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1499 /* Storage and Description */
1503 char *storage = PQgetvalue(res, i, firstvcol);
1505 /* these strings are literal in our syntax, so not translated. */
1506 printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1507 (storage[0] == 'm' ? "main" :
1508 (storage[0] == 'x' ? "extended" :
1509 (storage[0] == 'e' ? "external" :
1513 /* Statistics target, if the relkind supports this feature */
1514 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1516 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1520 /* Column comments, if the relkind supports this feature. */
1521 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1522 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1523 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1529 if (tableinfo.relkind == 'i')
1531 /* Footer information about an index */
1534 printfPQExpBuffer(&buf,
1535 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1536 if (pset.sversion >= 80200)
1537 appendPQExpBuffer(&buf, "i.indisvalid,\n");
1539 appendPQExpBuffer(&buf, "true AS indisvalid,\n");
1540 if (pset.sversion >= 90000)
1541 appendPQExpBuffer(&buf,
1542 " (NOT i.indimmediate) AND "
1543 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1544 "WHERE conrelid = i.indrelid AND "
1545 "conindid = i.indexrelid AND "
1546 "contype IN ('p','u','x') AND "
1547 "condeferrable) AS condeferrable,\n"
1548 " (NOT i.indimmediate) AND "
1549 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1550 "WHERE conrelid = i.indrelid AND "
1551 "conindid = i.indexrelid AND "
1552 "contype IN ('p','u','x') AND "
1553 "condeferred) AS condeferred,\n");
1555 appendPQExpBuffer(&buf,
1556 " false AS condeferrable, false AS condeferred,\n");
1557 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
1558 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1559 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1560 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1561 "AND i.indrelid = c2.oid;",
1564 result = PSQLexec(buf.data, false);
1567 else if (PQntuples(result) != 1)
1574 char *indisunique = PQgetvalue(result, 0, 0);
1575 char *indisprimary = PQgetvalue(result, 0, 1);
1576 char *indisclustered = PQgetvalue(result, 0, 2);
1577 char *indisvalid = PQgetvalue(result, 0, 3);
1578 char *deferrable = PQgetvalue(result, 0, 4);
1579 char *deferred = PQgetvalue(result, 0, 5);
1580 char *indamname = PQgetvalue(result, 0, 6);
1581 char *indtable = PQgetvalue(result, 0, 7);
1582 char *indpred = PQgetvalue(result, 0, 8);
1584 if (strcmp(indisprimary, "t") == 0)
1585 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1586 else if (strcmp(indisunique, "t") == 0)
1587 printfPQExpBuffer(&tmpbuf, _("unique, "));
1589 resetPQExpBuffer(&tmpbuf);
1590 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1592 /* we assume here that index and table are in same schema */
1593 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1594 schemaname, indtable);
1596 if (strlen(indpred))
1597 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1599 if (strcmp(indisclustered, "t") == 0)
1600 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1602 if (strcmp(indisvalid, "t") != 0)
1603 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1605 if (strcmp(deferrable, "t") == 0)
1606 appendPQExpBuffer(&tmpbuf, _(", deferrable"));
1608 if (strcmp(deferred, "t") == 0)
1609 appendPQExpBuffer(&tmpbuf, _(", initially deferred"));
1611 printTableAddFooter(&cont, tmpbuf.data);
1612 add_tablespace_footer(&cont, tableinfo.relkind,
1613 tableinfo.tablespace, true);
1620 PGresult *result = NULL;
1622 /* Footer information about a view */
1623 printTableAddFooter(&cont, _("View definition:"));
1624 printTableAddFooter(&cont, view_def);
1627 if (tableinfo.hasrules)
1629 printfPQExpBuffer(&buf,
1630 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1631 "FROM pg_catalog.pg_rewrite r\n"
1632 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
1634 result = PSQLexec(buf.data, false);
1638 if (PQntuples(result) > 0)
1640 printTableAddFooter(&cont, _("Rules:"));
1641 for (i = 0; i < PQntuples(result); i++)
1643 const char *ruledef;
1645 /* Everything after "CREATE RULE" is echoed verbatim */
1646 ruledef = PQgetvalue(result, i, 1);
1649 printfPQExpBuffer(&buf, " %s", ruledef);
1650 printTableAddFooter(&cont, buf.data);
1656 else if (tableinfo.relkind == 'S')
1658 /* Footer information about a sequence */
1659 PGresult *result = NULL;
1661 /* Get the column that owns this sequence */
1662 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1663 "\n pg_catalog.quote_ident(relname) || '.' ||"
1664 "\n pg_catalog.quote_ident(attname)"
1665 "\nFROM pg_catalog.pg_class c"
1666 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1667 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1668 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1669 "\n a.attrelid=c.oid AND"
1670 "\n a.attnum=d.refobjsubid)"
1671 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1672 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1674 "\n AND d.deptype='a'",
1677 result = PSQLexec(buf.data, false);
1680 else if (PQntuples(result) == 1)
1682 printfPQExpBuffer(&buf, _("Owned by: %s"),
1683 PQgetvalue(result, 0, 0));
1684 printTableAddFooter(&cont, buf.data);
1688 * If we get no rows back, don't show anything (obviously). We should
1689 * never get more than one row back, but if we do, just ignore it and
1690 * don't print anything.
1694 else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1696 /* Footer information about a table */
1697 PGresult *result = NULL;
1701 if (tableinfo.hasindex)
1703 printfPQExpBuffer(&buf,
1704 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1705 if (pset.sversion >= 80200)
1706 appendPQExpBuffer(&buf, "i.indisvalid, ");
1708 appendPQExpBuffer(&buf, "true as indisvalid, ");
1709 appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
1710 if (pset.sversion >= 90000)
1711 appendPQExpBuffer(&buf,
1712 "pg_catalog.pg_get_constraintdef(con.oid, true), "
1713 "contype, condeferrable, condeferred");
1715 appendPQExpBuffer(&buf,
1716 "null AS constraintdef, null AS contype, "
1717 "false AS condeferrable, false AS condeferred");
1718 if (pset.sversion >= 80000)
1719 appendPQExpBuffer(&buf, ", c2.reltablespace");
1720 appendPQExpBuffer(&buf,
1721 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1722 if (pset.sversion >= 90000)
1723 appendPQExpBuffer(&buf,
1724 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1725 appendPQExpBuffer(&buf,
1726 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1727 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
1729 result = PSQLexec(buf.data, false);
1733 tuples = PQntuples(result);
1737 printTableAddFooter(&cont, _("Indexes:"));
1738 for (i = 0; i < tuples; i++)
1740 /* untranslated index name */
1741 printfPQExpBuffer(&buf, " \"%s\"",
1742 PQgetvalue(result, i, 0));
1744 /* If exclusion constraint, print the constraintdef */
1745 if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1747 appendPQExpBuffer(&buf, " %s",
1748 PQgetvalue(result, i, 6));
1752 const char *indexdef;
1753 const char *usingpos;
1755 /* Label as primary key or unique (but not both) */
1756 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1757 appendPQExpBuffer(&buf, " PRIMARY KEY,");
1758 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1760 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1761 appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
1763 appendPQExpBuffer(&buf, " UNIQUE,");
1766 /* Everything after "USING" is echoed verbatim */
1767 indexdef = PQgetvalue(result, i, 5);
1768 usingpos = strstr(indexdef, " USING ");
1770 indexdef = usingpos + 7;
1771 appendPQExpBuffer(&buf, " %s", indexdef);
1773 /* Need these for deferrable PK/UNIQUE indexes */
1774 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1775 appendPQExpBuffer(&buf, " DEFERRABLE");
1777 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1778 appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
1781 /* Add these for all cases */
1782 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1783 appendPQExpBuffer(&buf, " CLUSTER");
1785 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1786 appendPQExpBuffer(&buf, " INVALID");
1788 printTableAddFooter(&cont, buf.data);
1790 /* Print tablespace of the index on the same line */
1791 if (pset.sversion >= 80000)
1792 add_tablespace_footer(&cont, 'i',
1793 atooid(PQgetvalue(result, i, 10)),
1800 /* print table (and column) check constraints */
1801 if (tableinfo.checks)
1803 printfPQExpBuffer(&buf,
1804 "SELECT r.conname, "
1805 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1806 "FROM pg_catalog.pg_constraint r\n"
1807 "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
1810 result = PSQLexec(buf.data, false);
1814 tuples = PQntuples(result);
1818 printTableAddFooter(&cont, _("Check constraints:"));
1819 for (i = 0; i < tuples; i++)
1821 /* untranslated contraint name and def */
1822 printfPQExpBuffer(&buf, " \"%s\" %s",
1823 PQgetvalue(result, i, 0),
1824 PQgetvalue(result, i, 1));
1826 printTableAddFooter(&cont, buf.data);
1832 /* print foreign-key constraints (there are none if no triggers) */
1833 if (tableinfo.hastriggers)
1835 printfPQExpBuffer(&buf,
1837 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1838 "FROM pg_catalog.pg_constraint r\n"
1839 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
1841 result = PSQLexec(buf.data, false);
1845 tuples = PQntuples(result);
1849 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1850 for (i = 0; i < tuples; i++)
1852 /* untranslated constraint name and def */
1853 printfPQExpBuffer(&buf, " \"%s\" %s",
1854 PQgetvalue(result, i, 0),
1855 PQgetvalue(result, i, 1));
1857 printTableAddFooter(&cont, buf.data);
1863 /* print incoming foreign-key references (none if no triggers) */
1864 if (tableinfo.hastriggers)
1866 printfPQExpBuffer(&buf,
1867 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1868 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1869 "FROM pg_catalog.pg_constraint c\n"
1870 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
1872 result = PSQLexec(buf.data, false);
1876 tuples = PQntuples(result);
1880 printTableAddFooter(&cont, _("Referenced by:"));
1881 for (i = 0; i < tuples; i++)
1883 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
1884 PQgetvalue(result, i, 1),
1885 PQgetvalue(result, i, 0),
1886 PQgetvalue(result, i, 2));
1888 printTableAddFooter(&cont, buf.data);
1895 if (tableinfo.hasrules)
1897 if (pset.sversion >= 80300)
1899 printfPQExpBuffer(&buf,
1900 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1902 "FROM pg_catalog.pg_rewrite r\n"
1903 "WHERE r.ev_class = '%s' ORDER BY 1;",
1908 printfPQExpBuffer(&buf,
1909 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1910 "'O'::char AS ev_enabled\n"
1911 "FROM pg_catalog.pg_rewrite r\n"
1912 "WHERE r.ev_class = '%s' ORDER BY 1;",
1915 result = PSQLexec(buf.data, false);
1919 tuples = PQntuples(result);
1926 for (category = 0; category < 4; category++)
1928 have_heading = false;
1930 for (i = 0; i < tuples; i++)
1932 const char *ruledef;
1933 bool list_rule = false;
1938 if (*PQgetvalue(result, i, 2) == 'O')
1942 if (*PQgetvalue(result, i, 2) == 'D')
1946 if (*PQgetvalue(result, i, 2) == 'A')
1950 if (*PQgetvalue(result, i, 2) == 'R')
1962 printfPQExpBuffer(&buf, _("Rules:"));
1965 printfPQExpBuffer(&buf, _("Disabled rules:"));
1968 printfPQExpBuffer(&buf, _("Rules firing always:"));
1971 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1974 printTableAddFooter(&cont, buf.data);
1975 have_heading = true;
1978 /* Everything after "CREATE RULE" is echoed verbatim */
1979 ruledef = PQgetvalue(result, i, 1);
1981 printfPQExpBuffer(&buf, " %s", ruledef);
1982 printTableAddFooter(&cont, buf.data);
1991 * Print triggers next, if any (but only user-defined triggers). This
1992 * could apply to either a table or a view.
1994 if (tableinfo.hastriggers)
1999 printfPQExpBuffer(&buf,
2001 "pg_catalog.pg_get_triggerdef(t.oid%s), "
2003 "FROM pg_catalog.pg_trigger t\n"
2004 "WHERE t.tgrelid = '%s' AND ",
2005 (pset.sversion >= 90000 ? ", true" : ""),
2007 if (pset.sversion >= 90000)
2008 appendPQExpBuffer(&buf, "NOT t.tgisinternal");
2009 else if (pset.sversion >= 80300)
2010 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
2012 appendPQExpBuffer(&buf,
2013 "(NOT tgisconstraint "
2015 " (SELECT 1 FROM pg_catalog.pg_depend d "
2016 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2017 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2018 appendPQExpBuffer(&buf, "\nORDER BY 1;");
2020 result = PSQLexec(buf.data, false);
2024 tuples = PQntuples(result);
2032 * split the output into 4 different categories. Enabled triggers,
2033 * disabled triggers and the two special ALWAYS and REPLICA
2036 for (category = 0; category < 4; category++)
2038 have_heading = false;
2039 for (i = 0; i < tuples; i++)
2043 const char *usingpos;
2044 const char *tgenabled;
2047 * Check if this trigger falls into the current category
2049 tgenabled = PQgetvalue(result, i, 2);
2050 list_trigger = false;
2054 if (*tgenabled == 'O' || *tgenabled == 't')
2055 list_trigger = true;
2058 if (*tgenabled == 'D' || *tgenabled == 'f')
2059 list_trigger = true;
2062 if (*tgenabled == 'A')
2063 list_trigger = true;
2066 if (*tgenabled == 'R')
2067 list_trigger = true;
2070 if (list_trigger == false)
2073 /* Print the category heading once */
2074 if (have_heading == false)
2079 printfPQExpBuffer(&buf, _("Triggers:"));
2082 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2085 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2088 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2092 printTableAddFooter(&cont, buf.data);
2093 have_heading = true;
2096 /* Everything after "TRIGGER" is echoed verbatim */
2097 tgdef = PQgetvalue(result, i, 1);
2098 usingpos = strstr(tgdef, " TRIGGER ");
2100 tgdef = usingpos + 9;
2102 printfPQExpBuffer(&buf, " %s", tgdef);
2103 printTableAddFooter(&cont, buf.data);
2111 * Finish printing the footer information about a table.
2113 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
2118 /* print foreign server name */
2119 if (tableinfo.relkind == 'f')
2123 /* Footer information about foreign table */
2124 printfPQExpBuffer(&buf,
2125 "SELECT s.srvname,\n"
2126 " array_to_string(ARRAY(SELECT "
2127 " quote_ident(option_name) || ' ' || "
2128 " quote_literal(option_value) FROM "
2129 " pg_options_to_table(ftoptions)), ', ') "
2130 "FROM pg_catalog.pg_foreign_table f,\n"
2131 " pg_catalog.pg_foreign_server s\n"
2132 "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2134 result = PSQLexec(buf.data, false);
2137 else if (PQntuples(result) != 1)
2143 /* Print server name */
2144 printfPQExpBuffer(&buf, "Server: %s",
2145 PQgetvalue(result, 0, 0));
2146 printTableAddFooter(&cont, buf.data);
2148 /* Print per-table FDW options, if any */
2149 ftoptions = PQgetvalue(result, 0, 1);
2150 if (ftoptions && ftoptions[0] != '\0')
2152 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2153 printTableAddFooter(&cont, buf.data);
2158 /* print inherited tables */
2159 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);
2161 result = PSQLexec(buf.data, false);
2166 const char *s = _("Inherits");
2167 int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2169 tuples = PQntuples(result);
2171 for (i = 0; i < tuples; i++)
2174 printfPQExpBuffer(&buf, "%s: %s",
2175 s, PQgetvalue(result, i, 0));
2177 printfPQExpBuffer(&buf, "%*s %s",
2178 sw, "", PQgetvalue(result, i, 0));
2180 appendPQExpBuffer(&buf, ",");
2182 printTableAddFooter(&cont, buf.data);
2188 /* print child tables */
2189 if (pset.sversion >= 80300)
2190 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);
2192 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);
2194 result = PSQLexec(buf.data, false);
2198 tuples = PQntuples(result);
2202 /* print the number of child tables, if any */
2205 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2206 printTableAddFooter(&cont, buf.data);
2211 /* display the list of child tables */
2212 const char *ct = _("Child tables");
2213 int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2215 for (i = 0; i < tuples; i++)
2218 printfPQExpBuffer(&buf, "%s: %s",
2219 ct, PQgetvalue(result, i, 0));
2221 printfPQExpBuffer(&buf, "%*s %s",
2222 ctw, "", PQgetvalue(result, i, 0));
2224 appendPQExpBuffer(&buf, ",");
2226 printTableAddFooter(&cont, buf.data);
2232 if (tableinfo.reloftype)
2234 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2235 printTableAddFooter(&cont, buf.data);
2238 /* OIDs, if verbose */
2241 const char *s = _("Has OIDs");
2243 printfPQExpBuffer(&buf, "%s: %s", s,
2244 (tableinfo.hasoids ? _("yes") : _("no")));
2245 printTableAddFooter(&cont, buf.data);
2248 /* Tablespace info */
2249 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2253 /* reloptions, if verbose */
2255 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2257 const char *t = _("Options");
2259 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
2260 printTableAddFooter(&cont, buf.data);
2263 printTable(&cont, pset.queryFout, pset.logfile);
2264 printTableCleanup(&cont);
2271 if (printTableInitialized)
2272 printTableCleanup(&cont);
2273 termPQExpBuffer(&buf);
2274 termPQExpBuffer(&title);
2275 termPQExpBuffer(&tmpbuf);
2279 for (ptr = seq_values; *ptr; ptr++)
2286 for (ptr = modifiers; *ptr; ptr++)
2301 * Add a tablespace description to a footer. If 'newline' is true, it is added
2302 * in a new line; otherwise it's appended to the current value of the last
2306 add_tablespace_footer(printTableContent *const cont, char relkind,
2307 Oid tablespace, const bool newline)
2309 /* relkinds for which we support tablespaces */
2310 if (relkind == 'r' || relkind == 'i')
2313 * We ignore the database default tablespace so that users not using
2314 * tablespaces don't need to know about them. This case also covers
2315 * pre-8.0 servers, for which tablespace will always be 0.
2317 if (tablespace != 0)
2319 PGresult *result = NULL;
2320 PQExpBufferData buf;
2322 initPQExpBuffer(&buf);
2323 printfPQExpBuffer(&buf,
2324 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2325 "WHERE oid = '%u';", tablespace);
2326 result = PSQLexec(buf.data, false);
2329 /* Should always be the case, but.... */
2330 if (PQntuples(result) > 0)
2334 /* Add the tablespace as a new footer */
2335 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2336 PQgetvalue(result, 0, 0));
2337 printTableAddFooter(cont, buf.data);
2341 /* Append the tablespace to the latest footer */
2342 printfPQExpBuffer(&buf, "%s", cont->footer->data);
2345 * translator: before this string there's an index
2346 * description like '"foo_pkey" PRIMARY KEY, btree (a)'
2348 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2349 PQgetvalue(result, 0, 0));
2350 printTableSetFooter(cont, buf.data);
2354 termPQExpBuffer(&buf);
2362 * Describes roles. Any schema portion of the pattern is ignored.
2365 describeRoles(const char *pattern, bool verbose)
2367 PQExpBufferData buf;
2369 printTableContent cont;
2370 printTableOpt myopt = pset.popt.topt;
2375 const char align = 'l';
2378 myopt.default_footer = false;
2380 initPQExpBuffer(&buf);
2382 if (pset.sversion >= 80100)
2384 printfPQExpBuffer(&buf,
2385 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2386 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2387 " r.rolconnlimit, r.rolvaliduntil,\n"
2388 " ARRAY(SELECT b.rolname\n"
2389 " FROM pg_catalog.pg_auth_members m\n"
2390 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2391 " WHERE m.member = r.oid) as memberof");
2393 if (verbose && pset.sversion >= 80200)
2395 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2398 if (pset.sversion >= 90100)
2400 appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2403 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2405 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2406 NULL, "r.rolname", NULL, NULL);
2410 printfPQExpBuffer(&buf,
2411 "SELECT u.usename AS rolname,\n"
2412 " u.usesuper AS rolsuper,\n"
2413 " true AS rolinherit, false AS rolcreaterole,\n"
2414 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2415 " -1 AS rolconnlimit,"
2416 " u.valuntil as rolvaliduntil,\n"
2417 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2418 "\nFROM pg_catalog.pg_user u\n");
2420 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2421 NULL, "u.usename", NULL, NULL);
2424 appendPQExpBuffer(&buf, "ORDER BY 1;");
2426 res = PSQLexec(buf.data, false);
2430 nrows = PQntuples(res);
2431 attr = pg_malloc0((nrows + 1) * sizeof(*attr));
2433 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2435 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2436 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2437 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2439 if (verbose && pset.sversion >= 80200)
2440 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2442 for (i = 0; i < nrows; i++)
2444 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2446 resetPQExpBuffer(&buf);
2447 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2448 add_role_attribute(&buf, _("Superuser"));
2450 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2451 add_role_attribute(&buf, _("No inheritance"));
2453 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2454 add_role_attribute(&buf, _("Create role"));
2456 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2457 add_role_attribute(&buf, _("Create DB"));
2459 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2460 add_role_attribute(&buf, _("Cannot login"));
2462 if (pset.sversion >= 90100)
2463 if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2464 add_role_attribute(&buf, _("Replication"));
2466 conns = atoi(PQgetvalue(res, i, 6));
2470 appendPQExpBufferStr(&buf, "\n");
2473 appendPQExpBuffer(&buf, _("No connections"));
2475 appendPQExpBuffer(&buf, ngettext("%d connection",
2481 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2484 appendPQExpBufferStr(&buf, "\n");
2485 appendPQExpBufferStr(&buf, _("Password valid until "));
2486 appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
2489 attr[i] = pg_strdup(buf.data);
2491 printTableAddCell(&cont, attr[i], false, false);
2493 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2495 if (verbose && pset.sversion >= 80200)
2496 printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
2498 termPQExpBuffer(&buf);
2500 printTable(&cont, pset.queryFout, pset.logfile);
2501 printTableCleanup(&cont);
2503 for (i = 0; i < nrows; i++)
2512 add_role_attribute(PQExpBuffer buf, const char *const str)
2515 appendPQExpBufferStr(buf, ", ");
2517 appendPQExpBufferStr(buf, str);
2524 listDbRoleSettings(const char *pattern, const char *pattern2)
2526 PQExpBufferData buf;
2528 printQueryOpt myopt = pset.popt;
2530 initPQExpBuffer(&buf);
2532 if (pset.sversion >= 90000)
2536 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
2537 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2538 "FROM pg_db_role_setting AS s\n"
2539 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2540 "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2541 gettext_noop("Role"),
2542 gettext_noop("Database"),
2543 gettext_noop("Settings"));
2544 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2545 NULL, "pg_roles.rolname", NULL, NULL);
2546 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2547 NULL, "pg_database.datname", NULL, NULL);
2548 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
2552 fprintf(pset.queryFout,
2553 _("No per-database role settings support in this server version.\n"));
2557 res = PSQLexec(buf.data, false);
2561 if (PQntuples(res) == 0 && !pset.quiet)
2564 fprintf(pset.queryFout, _("No matching settings found.\n"));
2566 fprintf(pset.queryFout, _("No settings found.\n"));
2570 myopt.nullPrint = NULL;
2571 myopt.title = _("List of settings");
2572 myopt.translate_header = true;
2574 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2578 resetPQExpBuffer(&buf);
2586 * handler for \dt, \di, etc.
2588 * tabtypes is an array of characters, specifying what info is desired:
2593 * E - foreign table (Note: different from 'f', the relkind value)
2594 * (any order of the above is fine)
2595 * If tabtypes is empty, we default to \dtvsE.
2598 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2600 bool showTables = strchr(tabtypes, 't') != NULL;
2601 bool showIndexes = strchr(tabtypes, 'i') != NULL;
2602 bool showViews = strchr(tabtypes, 'v') != NULL;
2603 bool showSeq = strchr(tabtypes, 's') != NULL;
2604 bool showForeign = strchr(tabtypes, 'E') != NULL;
2606 PQExpBufferData buf;
2608 printQueryOpt myopt = pset.popt;
2609 static const bool translate_columns[] = {false, false, true, false, false, false, false};
2611 if (!(showTables || showIndexes || showViews || showSeq || showForeign))
2612 showTables = showViews = showSeq = showForeign = true;
2614 initPQExpBuffer(&buf);
2617 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2618 * for backwards compatibility.
2620 printfPQExpBuffer(&buf,
2621 "SELECT n.nspname as \"%s\",\n"
2622 " c.relname as \"%s\",\n"
2623 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
2624 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2625 gettext_noop("Schema"),
2626 gettext_noop("Name"),
2627 gettext_noop("table"),
2628 gettext_noop("view"),
2629 gettext_noop("index"),
2630 gettext_noop("sequence"),
2631 gettext_noop("special"),
2632 gettext_noop("foreign table"),
2633 gettext_noop("Type"),
2634 gettext_noop("Owner"));
2637 appendPQExpBuffer(&buf,
2638 ",\n c2.relname as \"%s\"",
2639 gettext_noop("Table"));
2644 * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
2645 * size of a table, including FSM, VM and TOAST tables.
2647 if (pset.sversion >= 90000)
2648 appendPQExpBuffer(&buf,
2649 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
2650 gettext_noop("Size"));
2651 else if (pset.sversion >= 80100)
2652 appendPQExpBuffer(&buf,
2653 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2654 gettext_noop("Size"));
2656 appendPQExpBuffer(&buf,
2657 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2658 gettext_noop("Description"));
2661 appendPQExpBuffer(&buf,
2662 "\nFROM pg_catalog.pg_class c"
2663 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2665 appendPQExpBuffer(&buf,
2666 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2667 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2669 appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
2671 appendPQExpBuffer(&buf, "'r',");
2673 appendPQExpBuffer(&buf, "'v',");
2675 appendPQExpBuffer(&buf, "'i',");
2677 appendPQExpBuffer(&buf, "'S',");
2678 if (showSystem || pattern)
2679 appendPQExpBuffer(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2682 appendPQExpBuffer(&buf, "'f',");
2684 appendPQExpBuffer(&buf, "''"); /* dummy */
2685 appendPQExpBuffer(&buf, ")\n");
2687 if (!showSystem && !pattern)
2688 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2689 " AND n.nspname <> 'information_schema'\n");
2692 * TOAST objects are suppressed unconditionally. Since we don't provide
2693 * any way to select relkind 't' above, we would never show toast tables
2694 * in any case; it seems a bit confusing to allow their indexes to be
2695 * shown. Use plain \d if you really need to look at a TOAST table/index.
2697 appendPQExpBuffer(&buf, " AND n.nspname !~ '^pg_toast'\n");
2699 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2700 "n.nspname", "c.relname", NULL,
2701 "pg_catalog.pg_table_is_visible(c.oid)");
2703 appendPQExpBuffer(&buf, "ORDER BY 1,2;");
2705 res = PSQLexec(buf.data, false);
2706 termPQExpBuffer(&buf);
2710 if (PQntuples(res) == 0 && !pset.quiet)
2713 fprintf(pset.queryFout, _("No matching relations found.\n"));
2715 fprintf(pset.queryFout, _("No relations found.\n"));
2719 myopt.nullPrint = NULL;
2720 myopt.title = _("List of relations");
2721 myopt.translate_header = true;
2722 myopt.translate_columns = translate_columns;
2724 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2735 * Describes languages.
2738 listLanguages(const char *pattern, bool verbose, bool showSystem)
2740 PQExpBufferData buf;
2742 printQueryOpt myopt = pset.popt;
2744 initPQExpBuffer(&buf);
2746 printfPQExpBuffer(&buf,
2747 "SELECT l.lanname AS \"%s\",\n",
2748 gettext_noop("Name"));
2749 if (pset.sversion >= 80300)
2750 appendPQExpBuffer(&buf,
2751 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
2752 gettext_noop("Owner"));
2754 appendPQExpBuffer(&buf,
2755 " l.lanpltrusted AS \"%s\"",
2756 gettext_noop("Trusted"));
2760 appendPQExpBuffer(&buf,
2761 ",\n NOT l.lanispl AS \"%s\",\n"
2762 " l.lanplcallfoid::regprocedure AS \"%s\",\n"
2763 " l.lanvalidator::regprocedure AS \"%s\",\n ",
2764 gettext_noop("Internal Language"),
2765 gettext_noop("Call Handler"),
2766 gettext_noop("Validator"));
2767 if (pset.sversion >= 90000)
2768 appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
2769 gettext_noop("Inline Handler"));
2770 printACLColumn(&buf, "l.lanacl");
2773 appendPQExpBuffer(&buf,
2774 ",\n d.description AS \"%s\""
2775 "\nFROM pg_catalog.pg_language l\n"
2776 "LEFT JOIN pg_catalog.pg_description d\n"
2777 " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
2778 " AND d.objsubid = 0\n",
2779 gettext_noop("Description"));
2782 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2783 NULL, "l.lanname", NULL, NULL);
2785 if (!showSystem && !pattern)
2786 appendPQExpBuffer(&buf, "WHERE l.lanplcallfoid != 0\n");
2789 appendPQExpBuffer(&buf, "ORDER BY 1;");
2791 res = PSQLexec(buf.data, false);
2792 termPQExpBuffer(&buf);
2796 myopt.nullPrint = NULL;
2797 myopt.title = _("List of languages");
2798 myopt.translate_header = true;
2800 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2810 * Describes domains.
2813 listDomains(const char *pattern, bool verbose, bool showSystem)
2815 PQExpBufferData buf;
2817 printQueryOpt myopt = pset.popt;
2819 initPQExpBuffer(&buf);
2821 printfPQExpBuffer(&buf,
2822 "SELECT n.nspname as \"%s\",\n"
2823 " t.typname as \"%s\",\n"
2824 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2826 gettext_noop("Schema"),
2827 gettext_noop("Name"),
2828 gettext_noop("Type"));
2830 if (pset.sversion >= 90100)
2831 appendPQExpBuffer(&buf,
2832 " COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
2833 " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
2834 appendPQExpBuffer(&buf,
2835 " CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
2836 " CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
2838 " pg_catalog.array_to_string(ARRAY(\n"
2839 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2840 " ), ' ') as \"%s\"",
2841 gettext_noop("Modifier"),
2842 gettext_noop("Check"));
2846 if (pset.sversion >= 90200)
2848 appendPQExpBuffer(&buf, ",\n ");
2849 printACLColumn(&buf, "t.typacl");
2851 appendPQExpBuffer(&buf,
2852 ",\n d.description as \"%s\"",
2853 gettext_noop("Description"));
2856 appendPQExpBuffer(&buf,
2857 "\nFROM pg_catalog.pg_type t\n"
2858 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
2861 appendPQExpBuffer(&buf,
2862 " LEFT JOIN pg_catalog.pg_description d "
2863 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
2864 "AND d.objsubid = 0\n");
2866 appendPQExpBuffer(&buf, "WHERE t.typtype = 'd'\n");
2868 if (!showSystem && !pattern)
2869 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2870 " AND n.nspname <> 'information_schema'\n");
2872 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2873 "n.nspname", "t.typname", NULL,
2874 "pg_catalog.pg_type_is_visible(t.oid)");
2876 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2878 res = PSQLexec(buf.data, false);
2879 termPQExpBuffer(&buf);
2883 myopt.nullPrint = NULL;
2884 myopt.title = _("List of domains");
2885 myopt.translate_header = true;
2887 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2896 * Describes conversions.
2899 listConversions(const char *pattern, bool verbose, bool showSystem)
2901 PQExpBufferData buf;
2903 printQueryOpt myopt = pset.popt;
2904 static const bool translate_columns[] = {false, false, false, false, true};
2906 initPQExpBuffer(&buf);
2908 printfPQExpBuffer(&buf,
2909 "SELECT n.nspname AS \"%s\",\n"
2910 " c.conname AS \"%s\",\n"
2911 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2912 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2913 " CASE WHEN c.condefault THEN '%s'\n"
2914 " ELSE '%s' END AS \"%s\"",
2915 gettext_noop("Schema"),
2916 gettext_noop("Name"),
2917 gettext_noop("Source"),
2918 gettext_noop("Destination"),
2919 gettext_noop("yes"), gettext_noop("no"),
2920 gettext_noop("Default?"));
2923 appendPQExpBuffer(&buf,
2924 ",\n d.description AS \"%s\"",
2925 gettext_noop("Description"));
2927 appendPQExpBuffer(&buf,
2928 "\nFROM pg_catalog.pg_conversion c\n"
2929 " JOIN pg_catalog.pg_namespace n "
2930 "ON n.oid = c.connamespace\n");
2933 appendPQExpBuffer(&buf,
2934 "LEFT JOIN pg_catalog.pg_description d "
2935 "ON d.classoid = c.tableoid\n"
2936 " AND d.objoid = c.oid "
2937 "AND d.objsubid = 0\n");
2939 appendPQExpBuffer(&buf, "WHERE true\n");
2941 if (!showSystem && !pattern)
2942 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2943 " AND n.nspname <> 'information_schema'\n");
2945 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2946 "n.nspname", "c.conname", NULL,
2947 "pg_catalog.pg_conversion_is_visible(c.oid)");
2949 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2951 res = PSQLexec(buf.data, false);
2952 termPQExpBuffer(&buf);
2956 myopt.nullPrint = NULL;
2957 myopt.title = _("List of conversions");
2958 myopt.translate_header = true;
2959 myopt.translate_columns = translate_columns;
2961 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2970 * Describes Event Triggers.
2973 listEventTriggers(const char *pattern, bool verbose)
2975 PQExpBufferData buf;
2977 printQueryOpt myopt = pset.popt;
2978 static const bool translate_columns[] =
2979 {false, false, false, true, false, false, false};
2981 initPQExpBuffer(&buf);
2983 printfPQExpBuffer(&buf,
2984 "select evtname as \"%s\", "
2985 "evtevent as \"%s\", "
2986 "pg_catalog.pg_get_userbyid(e.evtowner) AS \"%s\", "
2987 "case evtenabled when 'O' then 'enabled' "
2988 " when 'R' then 'replica' "
2989 " when 'A' then 'always' "
2990 " when 'D' then 'disabled' end as \"%s\", "
2991 "e.evtfoid::regproc as \"%s\", "
2992 "array_to_string(array(select x "
2993 " from unnest(evttags) as t(x)), ', ') as \"%s\" ",
2994 gettext_noop("Name"),
2995 gettext_noop("Event"),
2996 gettext_noop("Owner"),
2997 gettext_noop("Enabled"),
2998 gettext_noop("Procedure"),
2999 gettext_noop("Tags"));
3001 appendPQExpBuffer(&buf,
3002 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3003 gettext_noop("Description"));
3004 appendPQExpBuffer(&buf,
3005 "\nFROM pg_event_trigger e ");
3007 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3008 NULL, "evtname", NULL, NULL);
3010 appendPQExpBuffer(&buf, "ORDER BY 1");
3012 res = PSQLexec(buf.data, false);
3013 termPQExpBuffer(&buf);
3017 myopt.nullPrint = NULL;
3018 myopt.title = _("List of event triggers");
3019 myopt.translate_header = true;
3020 myopt.translate_columns = translate_columns;
3022 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3034 listCasts(const char *pattern, bool verbose)
3036 PQExpBufferData buf;
3038 printQueryOpt myopt = pset.popt;
3039 static const bool translate_columns[] = {false, false, false, true};
3041 initPQExpBuffer(&buf);
3044 * We need a left join to pg_proc for binary casts; the others are just
3045 * paranoia. Also note that we don't attempt to localize '(binary
3046 * coercible)', because there's too much risk of gettext translating a
3047 * function name that happens to match some string in the PO database.
3049 printfPQExpBuffer(&buf,
3050 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3051 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3052 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3055 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3056 " WHEN c.castcontext = 'a' THEN '%s'\n"
3059 gettext_noop("Source type"),
3060 gettext_noop("Target type"),
3061 gettext_noop("Function"),
3063 gettext_noop("in assignment"),
3064 gettext_noop("yes"),
3065 gettext_noop("Implicit?"));
3068 appendPQExpBuffer(&buf,
3069 ",\n d.description AS \"%s\"\n",
3070 gettext_noop("Description"));
3072 appendPQExpBuffer(&buf,
3073 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3074 " ON c.castfunc = p.oid\n"
3075 " LEFT JOIN pg_catalog.pg_type ts\n"
3076 " ON c.castsource = ts.oid\n"
3077 " LEFT JOIN pg_catalog.pg_namespace ns\n"
3078 " ON ns.oid = ts.typnamespace\n"
3079 " LEFT JOIN pg_catalog.pg_type tt\n"
3080 " ON c.casttarget = tt.oid\n"
3081 " LEFT JOIN pg_catalog.pg_namespace nt\n"
3082 " ON nt.oid = tt.typnamespace\n");
3085 appendPQExpBuffer(&buf,
3086 " LEFT JOIN pg_catalog.pg_description d\n"
3087 " ON d.classoid = c.tableoid AND d.objoid = "
3088 "c.oid AND d.objsubid = 0\n");
3090 appendPQExpBuffer(&buf, "WHERE ( (true");
3093 * Match name pattern against either internal or external name of either
3094 * castsource or casttarget
3096 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3097 "ns.nspname", "ts.typname",
3098 "pg_catalog.format_type(ts.oid, NULL)",
3099 "pg_catalog.pg_type_is_visible(ts.oid)");
3101 appendPQExpBuffer(&buf, ") OR (true");
3103 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3104 "nt.nspname", "tt.typname",
3105 "pg_catalog.format_type(tt.oid, NULL)",
3106 "pg_catalog.pg_type_is_visible(tt.oid)");
3108 appendPQExpBuffer(&buf, ") )\nORDER BY 1, 2;");
3110 res = PSQLexec(buf.data, false);
3111 termPQExpBuffer(&buf);
3115 myopt.nullPrint = NULL;
3116 myopt.title = _("List of casts");
3117 myopt.translate_header = true;
3118 myopt.translate_columns = translate_columns;
3120 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3129 * Describes collations.
3132 listCollations(const char *pattern, bool verbose, bool showSystem)
3134 PQExpBufferData buf;
3136 printQueryOpt myopt = pset.popt;
3137 static const bool translate_columns[] = {false, false, false, false, false};
3139 if (pset.sversion < 90100)
3141 psql_error("The server (version %d.%d) does not support collations.\n",
3142 pset.sversion / 10000, (pset.sversion / 100) % 100);
3146 initPQExpBuffer(&buf);
3148 printfPQExpBuffer(&buf,
3149 "SELECT n.nspname AS \"%s\",\n"
3150 " c.collname AS \"%s\",\n"
3151 " c.collcollate AS \"%s\",\n"
3152 " c.collctype AS \"%s\"",
3153 gettext_noop("Schema"),
3154 gettext_noop("Name"),
3155 gettext_noop("Collate"),
3156 gettext_noop("Ctype"));
3159 appendPQExpBuffer(&buf,
3160 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3161 gettext_noop("Description"));
3163 appendPQExpBuffer(&buf,
3164 "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3165 "WHERE n.oid = c.collnamespace\n");
3167 if (!showSystem && !pattern)
3168 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
3169 " AND n.nspname <> 'information_schema'\n");
3172 * Hide collations that aren't usable in the current database's encoding.
3173 * If you think to change this, note that pg_collation_is_visible rejects
3174 * unusable collations, so you will need to hack name pattern processing
3175 * somehow to avoid inconsistent behavior.
3177 appendPQExpBuffer(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3179 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3180 "n.nspname", "c.collname", NULL,
3181 "pg_catalog.pg_collation_is_visible(c.oid)");
3183 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3185 res = PSQLexec(buf.data, false);
3186 termPQExpBuffer(&buf);
3190 myopt.nullPrint = NULL;
3191 myopt.title = _("List of collations");
3192 myopt.translate_header = true;
3193 myopt.translate_columns = translate_columns;
3195 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3204 * Describes schemas (namespaces)
3207 listSchemas(const char *pattern, bool verbose, bool showSystem)
3209 PQExpBufferData buf;
3211 printQueryOpt myopt = pset.popt;
3213 initPQExpBuffer(&buf);
3214 printfPQExpBuffer(&buf,
3215 "SELECT n.nspname AS \"%s\",\n"
3216 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3217 gettext_noop("Name"),
3218 gettext_noop("Owner"));
3222 appendPQExpBuffer(&buf, ",\n ");
3223 printACLColumn(&buf, "n.nspacl");
3224 appendPQExpBuffer(&buf,
3225 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3226 gettext_noop("Description"));
3229 appendPQExpBuffer(&buf,
3230 "\nFROM pg_catalog.pg_namespace n\n");
3232 if (!showSystem && !pattern)
3233 appendPQExpBuffer(&buf,
3234 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3236 processSQLNamePattern(pset.db, &buf, pattern,
3237 !showSystem && !pattern, false,
3238 NULL, "n.nspname", NULL,
3241 appendPQExpBuffer(&buf, "ORDER BY 1;");
3243 res = PSQLexec(buf.data, false);
3244 termPQExpBuffer(&buf);
3248 myopt.nullPrint = NULL;
3249 myopt.title = _("List of schemas");
3250 myopt.translate_header = true;
3252 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3261 * list text search parsers
3264 listTSParsers(const char *pattern, bool verbose)
3266 PQExpBufferData buf;
3268 printQueryOpt myopt = pset.popt;
3270 if (pset.sversion < 80300)
3272 psql_error("The server (version %d.%d) does not support full text search.\n",
3273 pset.sversion / 10000, (pset.sversion / 100) % 100);
3278 return listTSParsersVerbose(pattern);
3280 initPQExpBuffer(&buf);
3282 printfPQExpBuffer(&buf,
3284 " n.nspname as \"%s\",\n"
3285 " p.prsname as \"%s\",\n"
3286 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3287 "FROM pg_catalog.pg_ts_parser p \n"
3288 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3289 gettext_noop("Schema"),
3290 gettext_noop("Name"),
3291 gettext_noop("Description")
3294 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3295 "n.nspname", "p.prsname", NULL,
3296 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3298 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3300 res = PSQLexec(buf.data, false);
3301 termPQExpBuffer(&buf);
3305 myopt.nullPrint = NULL;
3306 myopt.title = _("List of text search parsers");
3307 myopt.translate_header = true;
3309 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3316 * full description of parsers
3319 listTSParsersVerbose(const char *pattern)
3321 PQExpBufferData buf;
3325 initPQExpBuffer(&buf);
3327 printfPQExpBuffer(&buf,
3331 "FROM pg_catalog.pg_ts_parser p\n"
3332 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3335 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3336 "n.nspname", "p.prsname", NULL,
3337 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3339 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3341 res = PSQLexec(buf.data, false);
3342 termPQExpBuffer(&buf);
3346 if (PQntuples(res) == 0)
3349 psql_error("Did not find any text search parser named \"%s\".\n",
3355 for (i = 0; i < PQntuples(res); i++)
3358 const char *nspname = NULL;
3359 const char *prsname;
3361 oid = PQgetvalue(res, i, 0);
3362 if (!PQgetisnull(res, i, 1))
3363 nspname = PQgetvalue(res, i, 1);
3364 prsname = PQgetvalue(res, i, 2);
3366 if (!describeOneTSParser(oid, nspname, prsname))
3384 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3386 PQExpBufferData buf;
3389 printQueryOpt myopt = pset.popt;
3390 static const bool translate_columns[] = {true, false, false};
3392 initPQExpBuffer(&buf);
3394 printfPQExpBuffer(&buf,
3395 "SELECT '%s' AS \"%s\", \n"
3396 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3397 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3398 " FROM pg_catalog.pg_ts_parser p \n"
3399 " WHERE p.oid = '%s' \n"
3402 " p.prstoken::pg_catalog.regproc, \n"
3403 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3404 " FROM pg_catalog.pg_ts_parser p \n"
3405 " WHERE p.oid = '%s' \n"
3408 " p.prsend::pg_catalog.regproc, \n"
3409 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3410 " FROM pg_catalog.pg_ts_parser p \n"
3411 " WHERE p.oid = '%s' \n"
3414 " p.prsheadline::pg_catalog.regproc, \n"
3415 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3416 " FROM pg_catalog.pg_ts_parser p \n"
3417 " WHERE p.oid = '%s' \n"
3420 " p.prslextype::pg_catalog.regproc, \n"
3421 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3422 " FROM pg_catalog.pg_ts_parser p \n"
3423 " WHERE p.oid = '%s';",
3424 gettext_noop("Start parse"),
3425 gettext_noop("Method"),
3426 gettext_noop("Function"),
3427 gettext_noop("Description"),
3429 gettext_noop("Get next token"),
3431 gettext_noop("End parse"),
3433 gettext_noop("Get headline"),
3435 gettext_noop("Get token types"),
3438 res = PSQLexec(buf.data, false);
3439 termPQExpBuffer(&buf);
3443 myopt.nullPrint = NULL;
3445 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3447 sprintf(title, _("Text search parser \"%s\""), prsname);
3448 myopt.title = title;
3449 myopt.footers = NULL;
3450 myopt.topt.default_footer = false;
3451 myopt.translate_header = true;
3452 myopt.translate_columns = translate_columns;
3454 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3458 initPQExpBuffer(&buf);
3460 printfPQExpBuffer(&buf,
3461 "SELECT t.alias as \"%s\", \n"
3462 " t.description as \"%s\" \n"
3463 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3465 gettext_noop("Token name"),
3466 gettext_noop("Description"),
3469 res = PSQLexec(buf.data, false);
3470 termPQExpBuffer(&buf);
3474 myopt.nullPrint = NULL;
3476 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3478 sprintf(title, _("Token types for parser \"%s\""), prsname);
3479 myopt.title = title;
3480 myopt.footers = NULL;
3481 myopt.topt.default_footer = true;
3482 myopt.translate_header = true;
3483 myopt.translate_columns = NULL;
3485 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3494 * list text search dictionaries
3497 listTSDictionaries(const char *pattern, bool verbose)
3499 PQExpBufferData buf;
3501 printQueryOpt myopt = pset.popt;
3503 if (pset.sversion < 80300)
3505 psql_error("The server (version %d.%d) does not support full text search.\n",
3506 pset.sversion / 10000, (pset.sversion / 100) % 100);
3510 initPQExpBuffer(&buf);
3512 printfPQExpBuffer(&buf,
3514 " n.nspname as \"%s\",\n"
3515 " d.dictname as \"%s\",\n",
3516 gettext_noop("Schema"),
3517 gettext_noop("Name"));
3521 appendPQExpBuffer(&buf,
3522 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3523 " pg_catalog.pg_ts_template t \n"
3524 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3525 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
3526 " d.dictinitoption as \"%s\", \n",
3527 gettext_noop("Template"),
3528 gettext_noop("Init options"));
3531 appendPQExpBuffer(&buf,
3532 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3533 gettext_noop("Description"));
3535 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3536 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3538 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3539 "n.nspname", "d.dictname", NULL,
3540 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3542 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3544 res = PSQLexec(buf.data, false);
3545 termPQExpBuffer(&buf);
3549 myopt.nullPrint = NULL;
3550 myopt.title = _("List of text search dictionaries");
3551 myopt.translate_header = true;
3553 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3562 * list text search templates
3565 listTSTemplates(const char *pattern, bool verbose)
3567 PQExpBufferData buf;
3569 printQueryOpt myopt = pset.popt;
3571 if (pset.sversion < 80300)
3573 psql_error("The server (version %d.%d) does not support full text search.\n",
3574 pset.sversion / 10000, (pset.sversion / 100) % 100);
3578 initPQExpBuffer(&buf);
3581 printfPQExpBuffer(&buf,
3583 " n.nspname AS \"%s\",\n"
3584 " t.tmplname AS \"%s\",\n"
3585 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3586 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3587 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3588 gettext_noop("Schema"),
3589 gettext_noop("Name"),
3590 gettext_noop("Init"),
3591 gettext_noop("Lexize"),
3592 gettext_noop("Description"));
3594 printfPQExpBuffer(&buf,
3596 " n.nspname AS \"%s\",\n"
3597 " t.tmplname AS \"%s\",\n"
3598 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3599 gettext_noop("Schema"),
3600 gettext_noop("Name"),
3601 gettext_noop("Description"));
3603 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
3604 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3606 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3607 "n.nspname", "t.tmplname", NULL,
3608 "pg_catalog.pg_ts_template_is_visible(t.oid)");
3610 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3612 res = PSQLexec(buf.data, false);
3613 termPQExpBuffer(&buf);
3617 myopt.nullPrint = NULL;
3618 myopt.title = _("List of text search templates");
3619 myopt.translate_header = true;
3621 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3630 * list text search configurations
3633 listTSConfigs(const char *pattern, bool verbose)
3635 PQExpBufferData buf;
3637 printQueryOpt myopt = pset.popt;
3639 if (pset.sversion < 80300)
3641 psql_error("The server (version %d.%d) does not support full text search.\n",
3642 pset.sversion / 10000, (pset.sversion / 100) % 100);
3647 return listTSConfigsVerbose(pattern);
3649 initPQExpBuffer(&buf);
3651 printfPQExpBuffer(&buf,
3653 " n.nspname as \"%s\",\n"
3654 " c.cfgname as \"%s\",\n"
3655 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3656 "FROM pg_catalog.pg_ts_config c\n"
3657 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3658 gettext_noop("Schema"),
3659 gettext_noop("Name"),
3660 gettext_noop("Description")
3663 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3664 "n.nspname", "c.cfgname", NULL,
3665 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3667 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3669 res = PSQLexec(buf.data, false);
3670 termPQExpBuffer(&buf);
3674 myopt.nullPrint = NULL;
3675 myopt.title = _("List of text search configurations");
3676 myopt.translate_header = true;
3678 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3685 listTSConfigsVerbose(const char *pattern)
3687 PQExpBufferData buf;
3691 initPQExpBuffer(&buf);
3693 printfPQExpBuffer(&buf,
3694 "SELECT c.oid, c.cfgname,\n"
3697 " np.nspname as pnspname \n"
3698 "FROM pg_catalog.pg_ts_config c \n"
3699 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3700 " pg_catalog.pg_ts_parser p \n"
3701 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3702 "WHERE p.oid = c.cfgparser\n"
3705 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3706 "n.nspname", "c.cfgname", NULL,
3707 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3709 appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
3711 res = PSQLexec(buf.data, false);
3712 termPQExpBuffer(&buf);
3716 if (PQntuples(res) == 0)
3719 psql_error("Did not find any text search configuration named \"%s\".\n",
3725 for (i = 0; i < PQntuples(res); i++)
3728 const char *cfgname;
3729 const char *nspname = NULL;
3730 const char *prsname;
3731 const char *pnspname = NULL;
3733 oid = PQgetvalue(res, i, 0);
3734 cfgname = PQgetvalue(res, i, 1);
3735 if (!PQgetisnull(res, i, 2))
3736 nspname = PQgetvalue(res, i, 2);
3737 prsname = PQgetvalue(res, i, 3);
3738 if (!PQgetisnull(res, i, 4))
3739 pnspname = PQgetvalue(res, i, 4);
3741 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
3759 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3760 const char *pnspname, const char *prsname)
3762 PQExpBufferData buf,
3765 printQueryOpt myopt = pset.popt;
3767 initPQExpBuffer(&buf);
3769 printfPQExpBuffer(&buf,
3771 " ( SELECT t.alias FROM \n"
3772 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3773 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3774 " pg_catalog.btrim( \n"
3775 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3776 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3777 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3778 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3779 " ) :: pg_catalog.text , \n"
3780 " '{}') AS \"%s\" \n"
3781 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3782 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3783 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3785 gettext_noop("Token"),
3786 gettext_noop("Dictionaries"),
3789 res = PSQLexec(buf.data, false);
3790 termPQExpBuffer(&buf);
3794 initPQExpBuffer(&title);
3797 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3800 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3804 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3807 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3810 myopt.nullPrint = NULL;
3811 myopt.title = title.data;
3812 myopt.footers = NULL;
3813 myopt.topt.default_footer = false;
3814 myopt.translate_header = true;
3816 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3818 termPQExpBuffer(&title);
3828 * Describes foreign-data wrappers
3831 listForeignDataWrappers(const char *pattern, bool verbose)
3833 PQExpBufferData buf;
3835 printQueryOpt myopt = pset.popt;
3837 if (pset.sversion < 80400)
3839 psql_error("The server (version %d.%d) does not support foreign-data wrappers.\n",
3840 pset.sversion / 10000, (pset.sversion / 100) % 100);
3844 initPQExpBuffer(&buf);
3845 printfPQExpBuffer(&buf,
3846 "SELECT fdw.fdwname AS \"%s\",\n"
3847 " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
3848 gettext_noop("Name"),
3849 gettext_noop("Owner"));
3850 if (pset.sversion >= 90100)
3851 appendPQExpBuffer(&buf,
3852 " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
3853 gettext_noop("Handler"));
3854 appendPQExpBuffer(&buf,
3855 " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
3856 gettext_noop("Validator"));
3860 appendPQExpBuffer(&buf, ",\n ");
3861 printACLColumn(&buf, "fdwacl");
3862 appendPQExpBuffer(&buf,
3863 ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
3864 " '(' || array_to_string(ARRAY(SELECT "
3865 " quote_ident(option_name) || ' ' || "
3866 " quote_literal(option_value) FROM "
3867 " pg_options_to_table(fdwoptions)), ', ') || ')' "
3869 gettext_noop("FDW Options"));
3871 if (pset.sversion >= 90100)
3872 appendPQExpBuffer(&buf,
3873 ",\n d.description AS \"%s\" ",
3874 gettext_noop("Description"));
3877 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
3879 if (verbose && pset.sversion >= 90100)
3880 appendPQExpBuffer(&buf,
3881 "LEFT JOIN pg_catalog.pg_description d\n"
3882 " ON d.classoid = fdw.tableoid "
3883 "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
3885 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3886 NULL, "fdwname", NULL, NULL);
3888 appendPQExpBuffer(&buf, "ORDER BY 1;");
3890 res = PSQLexec(buf.data, false);
3891 termPQExpBuffer(&buf);
3895 myopt.nullPrint = NULL;
3896 myopt.title = _("List of foreign-data wrappers");
3897 myopt.translate_header = true;
3899 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3908 * Describes foreign servers.
3911 listForeignServers(const char *pattern, bool verbose)
3913 PQExpBufferData buf;
3915 printQueryOpt myopt = pset.popt;
3917 if (pset.sversion < 80400)
3919 psql_error("The server (version %d.%d) does not support foreign servers.\n",
3920 pset.sversion / 10000, (pset.sversion / 100) % 100);
3924 initPQExpBuffer(&buf);
3925 printfPQExpBuffer(&buf,
3926 "SELECT s.srvname AS \"%s\",\n"
3927 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3928 " f.fdwname AS \"%s\"",
3929 gettext_noop("Name"),
3930 gettext_noop("Owner"),
3931 gettext_noop("Foreign-data wrapper"));
3935 appendPQExpBuffer(&buf, ",\n ");
3936 printACLColumn(&buf, "s.srvacl");
3937 appendPQExpBuffer(&buf,
3939 " s.srvtype AS \"%s\",\n"
3940 " s.srvversion AS \"%s\",\n"
3941 " CASE WHEN srvoptions IS NULL THEN '' ELSE "
3942 " '(' || array_to_string(ARRAY(SELECT "
3943 " quote_ident(option_name) || ' ' || "
3944 " quote_literal(option_value) FROM "
3945 " pg_options_to_table(srvoptions)), ', ') || ')' "
3947 " d.description AS \"%s\"",
3948 gettext_noop("Type"),
3949 gettext_noop("Version"),
3950 gettext_noop("FDW Options"),
3951 gettext_noop("Description"));
3954 appendPQExpBuffer(&buf,
3955 "\nFROM pg_catalog.pg_foreign_server s\n"
3956 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3959 appendPQExpBuffer(&buf,
3960 "LEFT JOIN pg_description d\n "
3961 "ON d.classoid = s.tableoid AND d.objoid = s.oid "
3962 "AND d.objsubid = 0\n");
3964 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3965 NULL, "s.srvname", NULL, NULL);
3967 appendPQExpBuffer(&buf, "ORDER BY 1;");
3969 res = PSQLexec(buf.data, false);
3970 termPQExpBuffer(&buf);
3974 myopt.nullPrint = NULL;
3975 myopt.title = _("List of foreign servers");
3976 myopt.translate_header = true;
3978 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3987 * Describes user mappings.
3990 listUserMappings(const char *pattern, bool verbose)
3992 PQExpBufferData buf;
3994 printQueryOpt myopt = pset.popt;
3996 if (pset.sversion < 80400)
3998 psql_error("The server (version %d.%d) does not support user mappings.\n",
3999 pset.sversion / 10000, (pset.sversion / 100) % 100);
4003 initPQExpBuffer(&buf);
4004 printfPQExpBuffer(&buf,
4005 "SELECT um.srvname AS \"%s\",\n"
4006 " um.usename AS \"%s\"",
4007 gettext_noop("Server"),
4008 gettext_noop("User name"));
4011 appendPQExpBuffer(&buf,
4012 ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4013 " '(' || array_to_string(ARRAY(SELECT "
4014 " quote_ident(option_name) || ' ' || "
4015 " quote_literal(option_value) FROM "
4016 " pg_options_to_table(umoptions)), ', ') || ')' "
4018 gettext_noop("FDW Options"));
4020 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4022 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4023 NULL, "um.srvname", "um.usename", NULL);
4025 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4027 res = PSQLexec(buf.data, false);
4028 termPQExpBuffer(&buf);
4032 myopt.nullPrint = NULL;
4033 myopt.title = _("List of user mappings");
4034 myopt.translate_header = true;
4036 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4045 * Describes foreign tables.
4048 listForeignTables(const char *pattern, bool verbose)
4050 PQExpBufferData buf;
4052 printQueryOpt myopt = pset.popt;
4054 if (pset.sversion < 90100)
4056 psql_error("The server (version %d.%d) does not support foreign tables.\n",
4057 pset.sversion / 10000, (pset.sversion / 100) % 100);
4061 initPQExpBuffer(&buf);
4062 printfPQExpBuffer(&buf,
4063 "SELECT n.nspname AS \"%s\",\n"
4064 " c.relname AS \"%s\",\n"
4065 " s.srvname AS \"%s\"",
4066 gettext_noop("Schema"),
4067 gettext_noop("Table"),
4068 gettext_noop("Server"));
4071 appendPQExpBuffer(&buf,
4072 ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4073 " '(' || array_to_string(ARRAY(SELECT "
4074 " quote_ident(option_name) || ' ' || "
4075 " quote_literal(option_value) FROM "
4076 " pg_options_to_table(ftoptions)), ', ') || ')' "
4078 " d.description AS \"%s\"",
4079 gettext_noop("FDW Options"),
4080 gettext_noop("Description"));
4082 appendPQExpBuffer(&buf,
4083 "\nFROM pg_catalog.pg_foreign_table ft\n"
4084 " INNER JOIN pg_catalog.pg_class c"
4085 " ON c.oid = ft.ftrelid\n"
4086 " INNER JOIN pg_catalog.pg_namespace n"
4087 " ON n.oid = c.relnamespace\n"
4088 " INNER JOIN pg_catalog.pg_foreign_server s"
4089 " ON s.oid = ft.ftserver\n");
4091 appendPQExpBuffer(&buf,
4092 " LEFT JOIN pg_catalog.pg_description d\n"
4093 " ON d.classoid = c.tableoid AND "
4094 "d.objoid = c.oid AND d.objsubid = 0\n");
4096 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4097 NULL, "n.nspname", "c.relname", NULL);
4099 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4101 res = PSQLexec(buf.data, false);
4102 termPQExpBuffer(&buf);
4106 myopt.nullPrint = NULL;
4107 myopt.title = _("List of foreign tables");
4108 myopt.translate_header = true;
4110 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4119 * Briefly describes installed extensions.
4122 listExtensions(const char *pattern)
4124 PQExpBufferData buf;
4126 printQueryOpt myopt = pset.popt;
4128 if (pset.sversion < 90100)
4130 psql_error("The server (version %d.%d) does not support extensions.\n",
4131 pset.sversion / 10000, (pset.sversion / 100) % 100);
4135 initPQExpBuffer(&buf);
4136 printfPQExpBuffer(&buf,
4137 "SELECT e.extname AS \"%s\", "
4138 "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4139 "FROM pg_catalog.pg_extension e "
4140 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4141 "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4142 "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4143 gettext_noop("Name"),
4144 gettext_noop("Version"),
4145 gettext_noop("Schema"),
4146 gettext_noop("Description"));
4148 processSQLNamePattern(pset.db, &buf, pattern,
4150 NULL, "e.extname", NULL,
4153 appendPQExpBuffer(&buf, "ORDER BY 1;");
4155 res = PSQLexec(buf.data, false);
4156 termPQExpBuffer(&buf);
4160 myopt.nullPrint = NULL;
4161 myopt.title = _("List of installed extensions");
4162 myopt.translate_header = true;
4164 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4173 * List contents of installed extensions.
4176 listExtensionContents(const char *pattern)
4178 PQExpBufferData buf;
4182 if (pset.sversion < 90100)
4184 psql_error("The server (version %d.%d) does not support extensions.\n",
4185 pset.sversion / 10000, (pset.sversion / 100) % 100);
4189 initPQExpBuffer(&buf);
4190 printfPQExpBuffer(&buf,
4191 "SELECT e.extname, e.oid\n"
4192 "FROM pg_catalog.pg_extension e\n");
4194 processSQLNamePattern(pset.db, &buf, pattern,
4196 NULL, "e.extname", NULL,
4199 appendPQExpBuffer(&buf, "ORDER BY 1;");
4201 res = PSQLexec(buf.data, false);
4202 termPQExpBuffer(&buf);
4206 if (PQntuples(res) == 0)
4211 psql_error("Did not find any extension named \"%s\".\n",
4214 psql_error("Did not find any extensions.\n");
4220 for (i = 0; i < PQntuples(res); i++)
4222 const char *extname;
4225 extname = PQgetvalue(res, i, 0);
4226 oid = PQgetvalue(res, i, 1);
4228 if (!listOneExtensionContents(extname, oid))
4245 listOneExtensionContents(const char *extname, const char *oid)
4247 PQExpBufferData buf;
4250 printQueryOpt myopt = pset.popt;
4252 initPQExpBuffer(&buf);
4253 printfPQExpBuffer(&buf,
4254 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4255 "FROM pg_catalog.pg_depend\n"
4256 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4258 gettext_noop("Object Description"),
4261 res = PSQLexec(buf.data, false);
4262 termPQExpBuffer(&buf);
4266 myopt.nullPrint = NULL;
4267 snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4268 myopt.title = title;
4269 myopt.translate_header = true;
4271 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4280 * Helper function for consistently formatting ACL (privilege) columns.
4281 * The proper targetlist entry is appended to buf. Note lack of any
4282 * whitespace or comma decoration.
4285 printACLColumn(PQExpBuffer buf, const char *colname)
4287 if (pset.sversion >= 80100)
4288 appendPQExpBuffer(buf,
4289 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4290 colname, gettext_noop("Access privileges"));
4292 appendPQExpBuffer(buf,
4293 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4294 colname, gettext_noop("Access privileges"));