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-2012, 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,
334 " WHEN p.provolatile = 'i' THEN '%s'\n"
335 " WHEN p.provolatile = 's' THEN '%s'\n"
336 " WHEN p.provolatile = 'v' THEN '%s'\n"
338 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
339 " l.lanname as \"%s\",\n"
340 " p.prosrc as \"%s\",\n"
341 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
342 gettext_noop("immutable"),
343 gettext_noop("stable"),
344 gettext_noop("volatile"),
345 gettext_noop("Volatility"),
346 gettext_noop("Owner"),
347 gettext_noop("Language"),
348 gettext_noop("Source code"),
349 gettext_noop("Description"));
351 appendPQExpBuffer(&buf,
352 "\nFROM pg_catalog.pg_proc p"
353 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
356 appendPQExpBuffer(&buf,
357 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
361 /* filter by function type, if requested */
362 if (showNormal && showAggregate && showTrigger && showWindow)
369 appendPQExpBuffer(&buf, " AND ");
372 appendPQExpBuffer(&buf, "WHERE ");
375 appendPQExpBuffer(&buf, "NOT p.proisagg\n");
380 appendPQExpBuffer(&buf, " AND ");
383 appendPQExpBuffer(&buf, "WHERE ");
386 appendPQExpBuffer(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
388 if (!showWindow && pset.sversion >= 80400)
391 appendPQExpBuffer(&buf, " AND ");
394 appendPQExpBuffer(&buf, "WHERE ");
397 appendPQExpBuffer(&buf, "NOT p.proiswindow\n");
402 bool needs_or = false;
404 appendPQExpBuffer(&buf, "WHERE (\n ");
406 /* Note: at least one of these must be true ... */
409 appendPQExpBuffer(&buf, "p.proisagg\n");
415 appendPQExpBuffer(&buf, " OR ");
416 appendPQExpBuffer(&buf,
417 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
423 appendPQExpBuffer(&buf, " OR ");
424 appendPQExpBuffer(&buf, "p.proiswindow\n");
427 appendPQExpBuffer(&buf, " )\n");
430 processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
431 "n.nspname", "p.proname", NULL,
432 "pg_catalog.pg_function_is_visible(p.oid)");
434 if (!showSystem && !pattern)
435 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
436 " AND n.nspname <> 'information_schema'\n");
438 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
440 res = PSQLexec(buf.data, false);
441 termPQExpBuffer(&buf);
445 myopt.nullPrint = NULL;
446 myopt.title = _("List of functions");
447 myopt.translate_header = true;
448 myopt.translate_columns = translate_columns;
450 printQuery(res, &myopt, pset.queryFout, pset.logfile);
463 describeTypes(const char *pattern, bool verbose, bool showSystem)
467 printQueryOpt myopt = pset.popt;
469 initPQExpBuffer(&buf);
471 printfPQExpBuffer(&buf,
472 "SELECT n.nspname as \"%s\",\n"
473 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
474 gettext_noop("Schema"),
475 gettext_noop("Name"));
477 appendPQExpBuffer(&buf,
478 " t.typname AS \"%s\",\n"
479 " CASE WHEN t.typrelid != 0\n"
480 " THEN CAST('tuple' AS pg_catalog.text)\n"
481 " WHEN t.typlen < 0\n"
482 " THEN CAST('var' AS pg_catalog.text)\n"
483 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
485 gettext_noop("Internal name"),
486 gettext_noop("Size"));
487 if (verbose && pset.sversion >= 80300)
489 appendPQExpBuffer(&buf,
490 " pg_catalog.array_to_string(\n"
492 " SELECT e.enumlabel\n"
493 " FROM pg_catalog.pg_enum e\n"
494 " WHERE e.enumtypid = t.oid\n");
496 if (pset.sversion >= 90100)
497 appendPQExpBuffer(&buf,
498 " ORDER BY e.enumsortorder\n");
500 appendPQExpBuffer(&buf,
501 " ORDER BY e.oid\n");
503 appendPQExpBuffer(&buf,
507 gettext_noop("Elements"));
509 if (verbose && pset.sversion >= 90200)
511 printACLColumn(&buf, "t.typacl");
512 appendPQExpBuffer(&buf, ",\n ");
515 appendPQExpBuffer(&buf,
516 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
517 gettext_noop("Description"));
519 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
520 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
523 * do not include complex types (typrelid!=0) unless they are standalone
526 appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
527 appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
528 "WHERE c.oid = t.typrelid))\n");
531 * do not include array types (before 8.3 we have to use the assumption
532 * that their names start with underscore)
534 if (pset.sversion >= 80300)
535 appendPQExpBuffer(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
537 appendPQExpBuffer(&buf, " AND t.typname !~ '^_'\n");
539 if (!showSystem && !pattern)
540 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
541 " AND n.nspname <> 'information_schema'\n");
543 /* Match name pattern against either internal or external name */
544 processSQLNamePattern(pset.db, &buf, pattern, true, false,
545 "n.nspname", "t.typname",
546 "pg_catalog.format_type(t.oid, NULL)",
547 "pg_catalog.pg_type_is_visible(t.oid)");
549 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
551 res = PSQLexec(buf.data, false);
552 termPQExpBuffer(&buf);
556 myopt.nullPrint = NULL;
557 myopt.title = _("List of data types");
558 myopt.translate_header = true;
560 printQuery(res, &myopt, pset.queryFout, pset.logfile);
570 describeOperators(const char *pattern, bool showSystem)
574 printQueryOpt myopt = pset.popt;
576 initPQExpBuffer(&buf);
579 * Note: before Postgres 9.1, we did not assign comments to any built-in
580 * operators, preferring to let the comment on the underlying function
581 * suffice. The coalesce() on the obj_description() calls below supports
582 * this convention by providing a fallback lookup of a comment on the
583 * operator's function. As of 9.1 there is a policy that every built-in
584 * operator should have a comment; so the coalesce() is no longer
585 * necessary so far as built-in operators are concerned. We keep it
586 * anyway, for now, because (1) third-party modules may still be following
587 * the old convention, and (2) we'd need to do it anyway when talking to a
591 printfPQExpBuffer(&buf,
592 "SELECT n.nspname as \"%s\",\n"
593 " o.oprname AS \"%s\",\n"
594 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
595 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
596 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
597 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
598 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
599 "FROM pg_catalog.pg_operator o\n"
600 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
601 gettext_noop("Schema"),
602 gettext_noop("Name"),
603 gettext_noop("Left arg type"),
604 gettext_noop("Right arg type"),
605 gettext_noop("Result type"),
606 gettext_noop("Description"));
608 if (!showSystem && !pattern)
609 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
610 " AND n.nspname <> 'information_schema'\n");
612 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
613 "n.nspname", "o.oprname", NULL,
614 "pg_catalog.pg_operator_is_visible(o.oid)");
616 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
618 res = PSQLexec(buf.data, false);
619 termPQExpBuffer(&buf);
623 myopt.nullPrint = NULL;
624 myopt.title = _("List of operators");
625 myopt.translate_header = true;
627 printQuery(res, &myopt, pset.queryFout, pset.logfile);
637 * for \l, \list, and -l switch
640 listAllDbs(bool verbose)
644 printQueryOpt myopt = pset.popt;
646 initPQExpBuffer(&buf);
648 printfPQExpBuffer(&buf,
649 "SELECT d.datname as \"%s\",\n"
650 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
651 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
652 gettext_noop("Name"),
653 gettext_noop("Owner"),
654 gettext_noop("Encoding"));
655 if (pset.sversion >= 80400)
656 appendPQExpBuffer(&buf,
657 " d.datcollate as \"%s\",\n"
658 " d.datctype as \"%s\",\n",
659 gettext_noop("Collate"),
660 gettext_noop("Ctype"));
661 appendPQExpBuffer(&buf, " ");
662 printACLColumn(&buf, "d.datacl");
663 if (verbose && pset.sversion >= 80200)
664 appendPQExpBuffer(&buf,
665 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
666 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
667 " ELSE 'No Access'\n"
669 gettext_noop("Size"));
670 if (verbose && pset.sversion >= 80000)
671 appendPQExpBuffer(&buf,
672 ",\n t.spcname as \"%s\"",
673 gettext_noop("Tablespace"));
674 if (verbose && pset.sversion >= 80200)
675 appendPQExpBuffer(&buf,
676 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
677 gettext_noop("Description"));
678 appendPQExpBuffer(&buf,
679 "\nFROM pg_catalog.pg_database d\n");
680 if (verbose && pset.sversion >= 80000)
681 appendPQExpBuffer(&buf,
682 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
683 appendPQExpBuffer(&buf, "ORDER BY 1;");
684 res = PSQLexec(buf.data, false);
685 termPQExpBuffer(&buf);
689 myopt.nullPrint = NULL;
690 myopt.title = _("List of databases");
691 myopt.translate_header = true;
693 printQuery(res, &myopt, pset.queryFout, pset.logfile);
701 * List Tables' Grant/Revoke Permissions
702 * \z (now also \dp -- perhaps more mnemonic)
705 permissionsList(const char *pattern)
709 printQueryOpt myopt = pset.popt;
710 static const bool translate_columns[] = {false, false, true, false, false};
712 initPQExpBuffer(&buf);
715 * we ignore indexes and toast tables since they have no meaningful rights
717 printfPQExpBuffer(&buf,
718 "SELECT n.nspname as \"%s\",\n"
719 " c.relname as \"%s\",\n"
720 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
722 gettext_noop("Schema"),
723 gettext_noop("Name"),
724 gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
725 gettext_noop("foreign table"),
726 gettext_noop("Type"));
728 printACLColumn(&buf, "c.relacl");
730 if (pset.sversion >= 80400)
731 appendPQExpBuffer(&buf,
732 ",\n pg_catalog.array_to_string(ARRAY(\n"
733 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
734 " FROM pg_catalog.pg_attribute a\n"
735 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
736 " ), E'\\n') AS \"%s\"",
737 gettext_noop("Column access privileges"));
739 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_class c\n"
740 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
741 "WHERE c.relkind IN ('r', 'v', 'S', 'f')\n");
744 * Unless a schema pattern is specified, we suppress system and temp
745 * tables, since they normally aren't very interesting from a permissions
746 * point of view. You can see 'em by explicit request though, eg with \z
749 processSQLNamePattern(pset.db, &buf, pattern, true, false,
750 "n.nspname", "c.relname", NULL,
751 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
753 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
755 res = PSQLexec(buf.data, false);
758 termPQExpBuffer(&buf);
762 myopt.nullPrint = NULL;
763 printfPQExpBuffer(&buf, _("Access privileges"));
764 myopt.title = buf.data;
765 myopt.translate_header = true;
766 myopt.translate_columns = translate_columns;
768 printQuery(res, &myopt, pset.queryFout, pset.logfile);
770 termPQExpBuffer(&buf);
779 * List Default ACLs. The pattern can match either schema or role name.
782 listDefaultACLs(const char *pattern)
786 printQueryOpt myopt = pset.popt;
787 static const bool translate_columns[] = {false, false, true, false};
789 if (pset.sversion < 90000)
791 psql_error("The server (version %d.%d) does not support altering default privileges.\n",
792 pset.sversion / 10000, (pset.sversion / 100) % 100);
796 initPQExpBuffer(&buf);
798 printfPQExpBuffer(&buf,
799 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
800 " n.nspname AS \"%s\",\n"
801 " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
803 gettext_noop("Owner"),
804 gettext_noop("Schema"),
806 gettext_noop("table"),
808 gettext_noop("sequence"),
810 gettext_noop("function"),
812 gettext_noop("type"),
813 gettext_noop("Type"));
815 printACLColumn(&buf, "d.defaclacl");
817 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
818 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
820 processSQLNamePattern(pset.db, &buf, pattern, false, false,
823 "pg_catalog.pg_get_userbyid(d.defaclrole)",
826 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
828 res = PSQLexec(buf.data, false);
831 termPQExpBuffer(&buf);
835 myopt.nullPrint = NULL;
836 printfPQExpBuffer(&buf, _("Default access privileges"));
837 myopt.title = buf.data;
838 myopt.translate_header = true;
839 myopt.translate_columns = translate_columns;
841 printQuery(res, &myopt, pset.queryFout, pset.logfile);
843 termPQExpBuffer(&buf);
850 * Get object comments
854 * Note: This command only lists comments for object types which do not have
855 * their comments displayed by their own backslash commands. The following
856 * types of objects will be displayed: constraint, operator class,
857 * operator family, rule, and trigger.
861 objectDescription(const char *pattern, bool showSystem)
865 printQueryOpt myopt = pset.popt;
866 static const bool translate_columns[] = {false, false, true, false};
868 initPQExpBuffer(&buf);
870 appendPQExpBuffer(&buf,
871 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
873 gettext_noop("Schema"),
874 gettext_noop("Name"),
875 gettext_noop("Object"),
876 gettext_noop("Description"));
878 /* Constraint descriptions */
879 appendPQExpBuffer(&buf,
880 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
881 " n.nspname as nspname,\n"
882 " CAST(pgc.conname AS pg_catalog.text) as name,"
883 " CAST('%s' AS pg_catalog.text) as object\n"
884 " FROM pg_catalog.pg_constraint pgc\n"
885 " JOIN pg_catalog.pg_class c "
886 "ON c.oid = pgc.conrelid\n"
887 " LEFT JOIN pg_catalog.pg_namespace n "
888 " ON n.oid = c.relnamespace\n",
889 gettext_noop("constraint"));
891 if (!showSystem && !pattern)
892 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
893 " AND n.nspname <> 'information_schema'\n");
895 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
896 false, "n.nspname", "pgc.conname", NULL,
897 "pg_catalog.pg_table_is_visible(c.oid)");
900 * pg_opclass.opcmethod only available in 8.3+
902 if (pset.sversion >= 80300)
904 /* Operator class descriptions */
905 appendPQExpBuffer(&buf,
907 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
908 " n.nspname as nspname,\n"
909 " CAST(o.opcname AS pg_catalog.text) as name,\n"
910 " CAST('%s' AS pg_catalog.text) as object\n"
911 " FROM pg_catalog.pg_opclass o\n"
912 " JOIN pg_catalog.pg_am am ON "
913 "o.opcmethod = am.oid\n"
914 " JOIN pg_catalog.pg_namespace n ON "
915 "n.oid = o.opcnamespace\n",
916 gettext_noop("operator class"));
918 if (!showSystem && !pattern)
919 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
920 " AND n.nspname <> 'information_schema'\n");
922 processSQLNamePattern(pset.db, &buf, pattern, true, false,
923 "n.nspname", "o.opcname", NULL,
924 "pg_catalog.pg_opclass_is_visible(o.oid)");
928 * although operator family comments have been around since 8.3,
929 * pg_opfamily_is_visible is only available in 9.2+
931 if (pset.sversion >= 90200)
933 /* Operator family descriptions */
934 appendPQExpBuffer(&buf,
936 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
937 " n.nspname as nspname,\n"
938 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
939 " CAST('%s' AS pg_catalog.text) as object\n"
940 " FROM pg_catalog.pg_opfamily opf\n"
941 " JOIN pg_catalog.pg_am am "
942 "ON opf.opfmethod = am.oid\n"
943 " JOIN pg_catalog.pg_namespace n "
944 "ON opf.opfnamespace = n.oid\n",
945 gettext_noop("operator family"));
947 if (!showSystem && !pattern)
948 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
949 " AND n.nspname <> 'information_schema'\n");
951 processSQLNamePattern(pset.db, &buf, pattern, true, false,
952 "n.nspname", "opf.opfname", NULL,
953 "pg_catalog.pg_opfamily_is_visible(opf.oid)");
956 /* Rule descriptions (ignore rules for views) */
957 appendPQExpBuffer(&buf,
959 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
960 " n.nspname as nspname,\n"
961 " CAST(r.rulename AS pg_catalog.text) as name,"
962 " CAST('%s' AS pg_catalog.text) as object\n"
963 " FROM pg_catalog.pg_rewrite r\n"
964 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
965 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
966 " WHERE r.rulename != '_RETURN'\n",
967 gettext_noop("rule"));
969 if (!showSystem && !pattern)
970 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
971 " AND n.nspname <> 'information_schema'\n");
973 processSQLNamePattern(pset.db, &buf, pattern, true, false,
974 "n.nspname", "r.rulename", NULL,
975 "pg_catalog.pg_table_is_visible(c.oid)");
977 /* Trigger descriptions */
978 appendPQExpBuffer(&buf,
980 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
981 " n.nspname as nspname,\n"
982 " CAST(t.tgname AS pg_catalog.text) as name,"
983 " CAST('%s' AS pg_catalog.text) as object\n"
984 " FROM pg_catalog.pg_trigger t\n"
985 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
986 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
987 gettext_noop("trigger"));
989 if (!showSystem && !pattern)
990 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
991 " AND n.nspname <> 'information_schema'\n");
993 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
994 "n.nspname", "t.tgname", NULL,
995 "pg_catalog.pg_table_is_visible(c.oid)");
997 appendPQExpBuffer(&buf,
999 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1001 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
1003 res = PSQLexec(buf.data, false);
1004 termPQExpBuffer(&buf);
1008 myopt.nullPrint = NULL;
1009 myopt.title = _("Object descriptions");
1010 myopt.translate_header = true;
1011 myopt.translate_columns = translate_columns;
1013 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1021 * describeTableDetails (for \d)
1023 * This routine finds the tables to be displayed, and calls
1024 * describeOneTableDetails for each one.
1026 * verbose: if true, this is \d+
1029 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1031 PQExpBufferData buf;
1035 initPQExpBuffer(&buf);
1037 printfPQExpBuffer(&buf,
1041 "FROM pg_catalog.pg_class c\n"
1042 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1044 if (!showSystem && !pattern)
1045 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1046 " AND n.nspname <> 'information_schema'\n");
1048 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1049 "n.nspname", "c.relname", NULL,
1050 "pg_catalog.pg_table_is_visible(c.oid)");
1052 appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
1054 res = PSQLexec(buf.data, false);
1055 termPQExpBuffer(&buf);
1059 if (PQntuples(res) == 0)
1062 psql_error("Did not find any relation named \"%s\".\n",
1068 for (i = 0; i < PQntuples(res); i++)
1071 const char *nspname;
1072 const char *relname;
1074 oid = PQgetvalue(res, i, 0);
1075 nspname = PQgetvalue(res, i, 1);
1076 relname = PQgetvalue(res, i, 2);
1078 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1095 * describeOneTableDetails (for \d)
1097 * Unfortunately, the information presented here is so complicated that it
1098 * cannot be done in a single query. So we have to assemble the printed table
1099 * by hand and pass it to the underlying printTable() function.
1102 describeOneTableDetails(const char *schemaname,
1103 const char *relationname,
1107 PQExpBufferData buf;
1108 PGresult *res = NULL;
1109 printTableOpt myopt = pset.popt.topt;
1110 printTableContent cont;
1111 bool printTableInitialized = false;
1113 char *view_def = NULL;
1115 char **seq_values = NULL;
1116 char **modifiers = NULL;
1118 PQExpBufferData title;
1119 PQExpBufferData tmpbuf;
1133 char relpersistence;
1135 bool show_modifiers = false;
1140 myopt.default_footer = false;
1141 /* This output looks confusing in expanded mode. */
1142 myopt.expanded = false;
1144 initPQExpBuffer(&buf);
1145 initPQExpBuffer(&title);
1146 initPQExpBuffer(&tmpbuf);
1148 /* Get general table info */
1149 if (pset.sversion >= 90100)
1151 printfPQExpBuffer(&buf,
1152 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1153 "c.relhastriggers, c.relhasoids, "
1154 "%s, c.reltablespace, "
1155 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1156 "c.relpersistence\n"
1157 "FROM pg_catalog.pg_class c\n "
1158 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1159 "WHERE c.oid = '%s';",
1161 "pg_catalog.array_to_string(c.reloptions || "
1162 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1166 else if (pset.sversion >= 90000)
1168 printfPQExpBuffer(&buf,
1169 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1170 "c.relhastriggers, c.relhasoids, "
1171 "%s, c.reltablespace, "
1172 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1173 "FROM pg_catalog.pg_class c\n "
1174 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1175 "WHERE c.oid = '%s';",
1177 "pg_catalog.array_to_string(c.reloptions || "
1178 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1182 else if (pset.sversion >= 80400)
1184 printfPQExpBuffer(&buf,
1185 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1186 "c.relhastriggers, c.relhasoids, "
1187 "%s, c.reltablespace\n"
1188 "FROM pg_catalog.pg_class c\n "
1189 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1190 "WHERE c.oid = '%s';",
1192 "pg_catalog.array_to_string(c.reloptions || "
1193 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1197 else if (pset.sversion >= 80200)
1199 printfPQExpBuffer(&buf,
1200 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1201 "reltriggers <> 0, relhasoids, "
1202 "%s, reltablespace\n"
1203 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1205 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1208 else if (pset.sversion >= 80000)
1210 printfPQExpBuffer(&buf,
1211 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1212 "reltriggers <> 0, relhasoids, "
1213 "'', reltablespace\n"
1214 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1219 printfPQExpBuffer(&buf,
1220 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1221 "reltriggers <> 0, relhasoids, "
1223 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1227 res = PSQLexec(buf.data, false);
1231 /* Did we get anything? */
1232 if (PQntuples(res) == 0)
1235 psql_error("Did not find any relation with OID %s.\n", oid);
1239 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1240 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1241 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1242 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1243 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1244 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1245 tableinfo.reloptions = (pset.sversion >= 80200) ?
1246 pg_strdup(PQgetvalue(res, 0, 6)) : NULL;
1247 tableinfo.tablespace = (pset.sversion >= 80000) ?
1248 atooid(PQgetvalue(res, 0, 7)) : 0;
1249 tableinfo.reloftype = (pset.sversion >= 90000 &&
1250 strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
1251 pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
1252 tableinfo.relpersistence = (pset.sversion >= 90100) ?
1253 *(PQgetvalue(res, 0, 9)) : 0;
1258 * If it's a sequence, fetch its values and store into an array that will
1261 if (tableinfo.relkind == 'S')
1263 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1264 /* must be separate because fmtId isn't reentrant */
1265 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1267 res = PSQLexec(buf.data, false);
1271 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1273 for (i = 0; i < PQnfields(res); i++)
1274 seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1275 seq_values[i] = NULL;
1284 * You need to modify value of "firstvcol" which will be defined below if
1285 * you are adding column(s) preceding to verbose-only columns.
1287 printfPQExpBuffer(&buf, "SELECT a.attname,");
1288 appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1289 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1290 "\n FROM pg_catalog.pg_attrdef d"
1291 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1292 "\n a.attnotnull, a.attnum,");
1293 if (pset.sversion >= 90100)
1294 appendPQExpBuffer(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1295 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1297 appendPQExpBuffer(&buf, "\n NULL AS attcollation");
1298 if (tableinfo.relkind == 'i')
1299 appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1301 appendPQExpBuffer(&buf, ",\n NULL AS indexdef");
1302 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1303 appendPQExpBuffer(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1304 " '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1305 " pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1307 appendPQExpBuffer(&buf, ",\n NULL AS attfdwoptions");
1310 appendPQExpBuffer(&buf, ",\n a.attstorage");
1311 appendPQExpBuffer(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1314 * In 9.0+, we have column comments for: relations, views, composite
1315 * types, and foreign tables (c.f. CommentObject() in comment.c).
1317 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1318 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1319 appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1322 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
1323 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1324 appendPQExpBuffer(&buf, "\nORDER BY a.attnum;");
1326 res = PSQLexec(buf.data, false);
1329 numrows = PQntuples(res);
1332 switch (tableinfo.relkind)
1335 if (tableinfo.relpersistence == 'u')
1336 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1337 schemaname, relationname);
1339 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1340 schemaname, relationname);
1343 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1344 schemaname, relationname);
1347 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1348 schemaname, relationname);
1351 if (tableinfo.relpersistence == 'u')
1352 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1353 schemaname, relationname);
1355 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1356 schemaname, relationname);
1359 /* not used as of 8.2, but keep it for backwards compatibility */
1360 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1361 schemaname, relationname);
1364 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1365 schemaname, relationname);
1368 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1369 schemaname, relationname);
1372 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1373 schemaname, relationname);
1376 /* untranslated unknown relkind */
1377 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1378 tableinfo.relkind, schemaname, relationname);
1382 /* Set the number of columns, and their names */
1383 headers[0] = gettext_noop("Column");
1384 headers[1] = gettext_noop("Type");
1387 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1388 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1390 show_modifiers = true;
1391 headers[cols++] = gettext_noop("Modifiers");
1392 modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers));
1395 if (tableinfo.relkind == 'S')
1396 headers[cols++] = gettext_noop("Value");
1398 if (tableinfo.relkind == 'i')
1399 headers[cols++] = gettext_noop("Definition");
1401 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1402 headers[cols++] = gettext_noop("FDW Options");
1406 headers[cols++] = gettext_noop("Storage");
1407 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1408 headers[cols++] = gettext_noop("Stats target");
1409 /* Column comments, if the relkind supports this feature. */
1410 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1411 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1412 headers[cols++] = gettext_noop("Description");
1415 printTableInit(&cont, &myopt, title.data, cols, numrows);
1416 printTableInitialized = true;
1418 for (i = 0; i < cols; i++)
1419 printTableAddHeader(&cont, headers[i], true, 'l');
1421 /* Check if table is a view */
1422 if (tableinfo.relkind == 'v' && verbose)
1426 printfPQExpBuffer(&buf,
1427 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1429 result = PSQLexec(buf.data, false);
1433 if (PQntuples(result) > 0)
1434 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1439 /* Generate table cells to be printed */
1440 for (i = 0; i < numrows; i++)
1443 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1446 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1448 /* Modifiers: collate, not null, default */
1451 resetPQExpBuffer(&tmpbuf);
1453 if (!PQgetisnull(res, i, 5))
1456 appendPQExpBufferStr(&tmpbuf, " ");
1457 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1458 PQgetvalue(res, i, 5));
1461 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1464 appendPQExpBufferStr(&tmpbuf, " ");
1465 appendPQExpBufferStr(&tmpbuf, _("not null"));
1468 /* handle "default" here */
1469 /* (note: above we cut off the 'default' string at 128) */
1470 if (strlen(PQgetvalue(res, i, 2)) != 0)
1473 appendPQExpBufferStr(&tmpbuf, " ");
1474 /* translator: default values of column definitions */
1475 appendPQExpBuffer(&tmpbuf, _("default %s"),
1476 PQgetvalue(res, i, 2));
1479 modifiers[i] = pg_strdup(tmpbuf.data);
1480 printTableAddCell(&cont, modifiers[i], false, false);
1483 /* Value: for sequences only */
1484 if (tableinfo.relkind == 'S')
1485 printTableAddCell(&cont, seq_values[i], false, false);
1487 /* Expression for index column */
1488 if (tableinfo.relkind == 'i')
1489 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1491 /* FDW options for foreign table column, only for 9.2 or later */
1492 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1493 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1495 /* Storage and Description */
1499 char *storage = PQgetvalue(res, i, firstvcol);
1501 /* these strings are literal in our syntax, so not translated. */
1502 printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1503 (storage[0] == 'm' ? "main" :
1504 (storage[0] == 'x' ? "extended" :
1505 (storage[0] == 'e' ? "external" :
1509 /* Statistics target, if the relkind supports this feature */
1510 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1512 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1516 /* Column comments, if the relkind supports this feature. */
1517 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1518 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1519 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1525 if (tableinfo.relkind == 'i')
1527 /* Footer information about an index */
1530 printfPQExpBuffer(&buf,
1531 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1532 if (pset.sversion >= 80200)
1533 appendPQExpBuffer(&buf, "i.indisvalid,\n");
1535 appendPQExpBuffer(&buf, "true AS indisvalid,\n");
1536 if (pset.sversion >= 90000)
1537 appendPQExpBuffer(&buf,
1538 " (NOT i.indimmediate) AND "
1539 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1540 "WHERE conrelid = i.indrelid AND "
1541 "conindid = i.indexrelid AND "
1542 "contype IN ('p','u','x') AND "
1543 "condeferrable) AS condeferrable,\n"
1544 " (NOT i.indimmediate) AND "
1545 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1546 "WHERE conrelid = i.indrelid AND "
1547 "conindid = i.indexrelid AND "
1548 "contype IN ('p','u','x') AND "
1549 "condeferred) AS condeferred,\n");
1551 appendPQExpBuffer(&buf,
1552 " false AS condeferrable, false AS condeferred,\n");
1553 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
1554 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1555 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1556 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1557 "AND i.indrelid = c2.oid;",
1560 result = PSQLexec(buf.data, false);
1563 else if (PQntuples(result) != 1)
1570 char *indisunique = PQgetvalue(result, 0, 0);
1571 char *indisprimary = PQgetvalue(result, 0, 1);
1572 char *indisclustered = PQgetvalue(result, 0, 2);
1573 char *indisvalid = PQgetvalue(result, 0, 3);
1574 char *deferrable = PQgetvalue(result, 0, 4);
1575 char *deferred = PQgetvalue(result, 0, 5);
1576 char *indamname = PQgetvalue(result, 0, 6);
1577 char *indtable = PQgetvalue(result, 0, 7);
1578 char *indpred = PQgetvalue(result, 0, 8);
1580 if (strcmp(indisprimary, "t") == 0)
1581 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1582 else if (strcmp(indisunique, "t") == 0)
1583 printfPQExpBuffer(&tmpbuf, _("unique, "));
1585 resetPQExpBuffer(&tmpbuf);
1586 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1588 /* we assume here that index and table are in same schema */
1589 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1590 schemaname, indtable);
1592 if (strlen(indpred))
1593 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1595 if (strcmp(indisclustered, "t") == 0)
1596 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1598 if (strcmp(indisvalid, "t") != 0)
1599 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1601 if (strcmp(deferrable, "t") == 0)
1602 appendPQExpBuffer(&tmpbuf, _(", deferrable"));
1604 if (strcmp(deferred, "t") == 0)
1605 appendPQExpBuffer(&tmpbuf, _(", initially deferred"));
1607 printTableAddFooter(&cont, tmpbuf.data);
1608 add_tablespace_footer(&cont, tableinfo.relkind,
1609 tableinfo.tablespace, true);
1616 PGresult *result = NULL;
1618 /* Footer information about a view */
1619 printTableAddFooter(&cont, _("View definition:"));
1620 printTableAddFooter(&cont, view_def);
1623 if (tableinfo.hasrules)
1625 printfPQExpBuffer(&buf,
1626 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1627 "FROM pg_catalog.pg_rewrite r\n"
1628 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
1630 result = PSQLexec(buf.data, false);
1634 if (PQntuples(result) > 0)
1636 printTableAddFooter(&cont, _("Rules:"));
1637 for (i = 0; i < PQntuples(result); i++)
1639 const char *ruledef;
1641 /* Everything after "CREATE RULE" is echoed verbatim */
1642 ruledef = PQgetvalue(result, i, 1);
1645 printfPQExpBuffer(&buf, " %s", ruledef);
1646 printTableAddFooter(&cont, buf.data);
1652 else if (tableinfo.relkind == 'S')
1654 /* Footer information about a sequence */
1655 PGresult *result = NULL;
1657 /* Get the column that owns this sequence */
1658 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1659 "\n pg_catalog.quote_ident(relname) || '.' ||"
1660 "\n pg_catalog.quote_ident(attname)"
1661 "\nFROM pg_catalog.pg_class c"
1662 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1663 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1664 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1665 "\n a.attrelid=c.oid AND"
1666 "\n a.attnum=d.refobjsubid)"
1667 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1668 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1670 "\n AND d.deptype='a'",
1673 result = PSQLexec(buf.data, false);
1676 else if (PQntuples(result) == 1)
1678 printfPQExpBuffer(&buf, _("Owned by: %s"),
1679 PQgetvalue(result, 0, 0));
1680 printTableAddFooter(&cont, buf.data);
1684 * If we get no rows back, don't show anything (obviously). We should
1685 * never get more than one row back, but if we do, just ignore it and
1686 * don't print anything.
1690 else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1692 /* Footer information about a table */
1693 PGresult *result = NULL;
1697 if (tableinfo.hasindex)
1699 printfPQExpBuffer(&buf,
1700 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1701 if (pset.sversion >= 80200)
1702 appendPQExpBuffer(&buf, "i.indisvalid, ");
1704 appendPQExpBuffer(&buf, "true as indisvalid, ");
1705 appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
1706 if (pset.sversion >= 90000)
1707 appendPQExpBuffer(&buf,
1708 "pg_catalog.pg_get_constraintdef(con.oid, true), "
1709 "contype, condeferrable, condeferred");
1711 appendPQExpBuffer(&buf,
1712 "null AS constraintdef, null AS contype, "
1713 "false AS condeferrable, false AS condeferred");
1714 if (pset.sversion >= 80000)
1715 appendPQExpBuffer(&buf, ", c2.reltablespace");
1716 appendPQExpBuffer(&buf,
1717 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1718 if (pset.sversion >= 90000)
1719 appendPQExpBuffer(&buf,
1720 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1721 appendPQExpBuffer(&buf,
1722 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1723 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
1725 result = PSQLexec(buf.data, false);
1729 tuples = PQntuples(result);
1733 printTableAddFooter(&cont, _("Indexes:"));
1734 for (i = 0; i < tuples; i++)
1736 /* untranslated index name */
1737 printfPQExpBuffer(&buf, " \"%s\"",
1738 PQgetvalue(result, i, 0));
1740 /* If exclusion constraint, print the constraintdef */
1741 if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1743 appendPQExpBuffer(&buf, " %s",
1744 PQgetvalue(result, i, 6));
1748 const char *indexdef;
1749 const char *usingpos;
1751 /* Label as primary key or unique (but not both) */
1752 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1753 appendPQExpBuffer(&buf, " PRIMARY KEY,");
1754 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1756 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1757 appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
1759 appendPQExpBuffer(&buf, " UNIQUE,");
1762 /* Everything after "USING" is echoed verbatim */
1763 indexdef = PQgetvalue(result, i, 5);
1764 usingpos = strstr(indexdef, " USING ");
1766 indexdef = usingpos + 7;
1767 appendPQExpBuffer(&buf, " %s", indexdef);
1769 /* Need these for deferrable PK/UNIQUE indexes */
1770 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1771 appendPQExpBuffer(&buf, " DEFERRABLE");
1773 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1774 appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
1777 /* Add these for all cases */
1778 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1779 appendPQExpBuffer(&buf, " CLUSTER");
1781 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1782 appendPQExpBuffer(&buf, " INVALID");
1784 printTableAddFooter(&cont, buf.data);
1786 /* Print tablespace of the index on the same line */
1787 if (pset.sversion >= 80000)
1788 add_tablespace_footer(&cont, 'i',
1789 atooid(PQgetvalue(result, i, 10)),
1796 /* print table (and column) check constraints */
1797 if (tableinfo.checks)
1799 printfPQExpBuffer(&buf,
1800 "SELECT r.conname, "
1801 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1802 "FROM pg_catalog.pg_constraint r\n"
1803 "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
1806 result = PSQLexec(buf.data, false);
1810 tuples = PQntuples(result);
1814 printTableAddFooter(&cont, _("Check constraints:"));
1815 for (i = 0; i < tuples; i++)
1817 /* untranslated contraint name and def */
1818 printfPQExpBuffer(&buf, " \"%s\" %s",
1819 PQgetvalue(result, i, 0),
1820 PQgetvalue(result, i, 1));
1822 printTableAddFooter(&cont, buf.data);
1828 /* print foreign-key constraints (there are none if no triggers) */
1829 if (tableinfo.hastriggers)
1831 printfPQExpBuffer(&buf,
1833 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1834 "FROM pg_catalog.pg_constraint r\n"
1835 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
1837 result = PSQLexec(buf.data, false);
1841 tuples = PQntuples(result);
1845 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1846 for (i = 0; i < tuples; i++)
1848 /* untranslated constraint name and def */
1849 printfPQExpBuffer(&buf, " \"%s\" %s",
1850 PQgetvalue(result, i, 0),
1851 PQgetvalue(result, i, 1));
1853 printTableAddFooter(&cont, buf.data);
1859 /* print incoming foreign-key references (none if no triggers) */
1860 if (tableinfo.hastriggers)
1862 printfPQExpBuffer(&buf,
1863 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1864 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1865 "FROM pg_catalog.pg_constraint c\n"
1866 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
1868 result = PSQLexec(buf.data, false);
1872 tuples = PQntuples(result);
1876 printTableAddFooter(&cont, _("Referenced by:"));
1877 for (i = 0; i < tuples; i++)
1879 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
1880 PQgetvalue(result, i, 1),
1881 PQgetvalue(result, i, 0),
1882 PQgetvalue(result, i, 2));
1884 printTableAddFooter(&cont, buf.data);
1891 if (tableinfo.hasrules)
1893 if (pset.sversion >= 80300)
1895 printfPQExpBuffer(&buf,
1896 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1898 "FROM pg_catalog.pg_rewrite r\n"
1899 "WHERE r.ev_class = '%s' ORDER BY 1;",
1904 printfPQExpBuffer(&buf,
1905 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1906 "'O'::char AS ev_enabled\n"
1907 "FROM pg_catalog.pg_rewrite r\n"
1908 "WHERE r.ev_class = '%s' ORDER BY 1;",
1911 result = PSQLexec(buf.data, false);
1915 tuples = PQntuples(result);
1922 for (category = 0; category < 4; category++)
1924 have_heading = false;
1926 for (i = 0; i < tuples; i++)
1928 const char *ruledef;
1929 bool list_rule = false;
1934 if (*PQgetvalue(result, i, 2) == 'O')
1938 if (*PQgetvalue(result, i, 2) == 'D')
1942 if (*PQgetvalue(result, i, 2) == 'A')
1946 if (*PQgetvalue(result, i, 2) == 'R')
1958 printfPQExpBuffer(&buf, _("Rules:"));
1961 printfPQExpBuffer(&buf, _("Disabled rules:"));
1964 printfPQExpBuffer(&buf, _("Rules firing always:"));
1967 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1970 printTableAddFooter(&cont, buf.data);
1971 have_heading = true;
1974 /* Everything after "CREATE RULE" is echoed verbatim */
1975 ruledef = PQgetvalue(result, i, 1);
1977 printfPQExpBuffer(&buf, " %s", ruledef);
1978 printTableAddFooter(&cont, buf.data);
1987 * Print triggers next, if any (but only user-defined triggers). This
1988 * could apply to either a table or a view.
1990 if (tableinfo.hastriggers)
1995 printfPQExpBuffer(&buf,
1997 "pg_catalog.pg_get_triggerdef(t.oid%s), "
1999 "FROM pg_catalog.pg_trigger t\n"
2000 "WHERE t.tgrelid = '%s' AND ",
2001 (pset.sversion >= 90000 ? ", true" : ""),
2003 if (pset.sversion >= 90000)
2004 appendPQExpBuffer(&buf, "NOT t.tgisinternal");
2005 else if (pset.sversion >= 80300)
2006 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
2008 appendPQExpBuffer(&buf,
2009 "(NOT tgisconstraint "
2011 " (SELECT 1 FROM pg_catalog.pg_depend d "
2012 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2013 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2014 appendPQExpBuffer(&buf, "\nORDER BY 1;");
2016 result = PSQLexec(buf.data, false);
2020 tuples = PQntuples(result);
2028 * split the output into 4 different categories. Enabled triggers,
2029 * disabled triggers and the two special ALWAYS and REPLICA
2032 for (category = 0; category < 4; category++)
2034 have_heading = false;
2035 for (i = 0; i < tuples; i++)
2039 const char *usingpos;
2040 const char *tgenabled;
2043 * Check if this trigger falls into the current category
2045 tgenabled = PQgetvalue(result, i, 2);
2046 list_trigger = false;
2050 if (*tgenabled == 'O' || *tgenabled == 't')
2051 list_trigger = true;
2054 if (*tgenabled == 'D' || *tgenabled == 'f')
2055 list_trigger = true;
2058 if (*tgenabled == 'A')
2059 list_trigger = true;
2062 if (*tgenabled == 'R')
2063 list_trigger = true;
2066 if (list_trigger == false)
2069 /* Print the category heading once */
2070 if (have_heading == false)
2075 printfPQExpBuffer(&buf, _("Triggers:"));
2078 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2081 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2084 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2088 printTableAddFooter(&cont, buf.data);
2089 have_heading = true;
2092 /* Everything after "TRIGGER" is echoed verbatim */
2093 tgdef = PQgetvalue(result, i, 1);
2094 usingpos = strstr(tgdef, " TRIGGER ");
2096 tgdef = usingpos + 9;
2098 printfPQExpBuffer(&buf, " %s", tgdef);
2099 printTableAddFooter(&cont, buf.data);
2107 * Finish printing the footer information about a table.
2109 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
2114 /* print foreign server name */
2115 if (tableinfo.relkind == 'f')
2119 /* Footer information about foreign table */
2120 printfPQExpBuffer(&buf,
2121 "SELECT s.srvname,\n"
2122 " array_to_string(ARRAY(SELECT "
2123 " quote_ident(option_name) || ' ' || "
2124 " quote_literal(option_value) FROM "
2125 " pg_options_to_table(ftoptions)), ', ') "
2126 "FROM pg_catalog.pg_foreign_table f,\n"
2127 " pg_catalog.pg_foreign_server s\n"
2128 "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2130 result = PSQLexec(buf.data, false);
2133 else if (PQntuples(result) != 1)
2139 /* Print server name */
2140 printfPQExpBuffer(&buf, "Server: %s",
2141 PQgetvalue(result, 0, 0));
2142 printTableAddFooter(&cont, buf.data);
2144 /* Print per-table FDW options, if any */
2145 ftoptions = PQgetvalue(result, 0, 1);
2146 if (ftoptions && ftoptions[0] != '\0')
2148 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2149 printTableAddFooter(&cont, buf.data);
2154 /* print inherited tables */
2155 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);
2157 result = PSQLexec(buf.data, false);
2162 const char *s = _("Inherits");
2163 int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2165 tuples = PQntuples(result);
2167 for (i = 0; i < tuples; i++)
2170 printfPQExpBuffer(&buf, "%s: %s",
2171 s, PQgetvalue(result, i, 0));
2173 printfPQExpBuffer(&buf, "%*s %s",
2174 sw, "", PQgetvalue(result, i, 0));
2176 appendPQExpBuffer(&buf, ",");
2178 printTableAddFooter(&cont, buf.data);
2184 /* print child tables */
2185 if (pset.sversion >= 80300)
2186 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);
2188 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);
2190 result = PSQLexec(buf.data, false);
2194 tuples = PQntuples(result);
2198 /* print the number of child tables, if any */
2201 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2202 printTableAddFooter(&cont, buf.data);
2207 /* display the list of child tables */
2208 const char *ct = _("Child tables");
2209 int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2211 for (i = 0; i < tuples; i++)
2214 printfPQExpBuffer(&buf, "%s: %s",
2215 ct, PQgetvalue(result, i, 0));
2217 printfPQExpBuffer(&buf, "%*s %s",
2218 ctw, "", PQgetvalue(result, i, 0));
2220 appendPQExpBuffer(&buf, ",");
2222 printTableAddFooter(&cont, buf.data);
2228 if (tableinfo.reloftype)
2230 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2231 printTableAddFooter(&cont, buf.data);
2234 /* OIDs, if verbose */
2237 const char *s = _("Has OIDs");
2239 printfPQExpBuffer(&buf, "%s: %s", s,
2240 (tableinfo.hasoids ? _("yes") : _("no")));
2241 printTableAddFooter(&cont, buf.data);
2244 /* Tablespace info */
2245 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2249 /* reloptions, if verbose */
2251 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2253 const char *t = _("Options");
2255 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
2256 printTableAddFooter(&cont, buf.data);
2259 printTable(&cont, pset.queryFout, pset.logfile);
2260 printTableCleanup(&cont);
2267 if (printTableInitialized)
2268 printTableCleanup(&cont);
2269 termPQExpBuffer(&buf);
2270 termPQExpBuffer(&title);
2271 termPQExpBuffer(&tmpbuf);
2275 for (ptr = seq_values; *ptr; ptr++)
2282 for (ptr = modifiers; *ptr; ptr++)
2297 * Add a tablespace description to a footer. If 'newline' is true, it is added
2298 * in a new line; otherwise it's appended to the current value of the last
2302 add_tablespace_footer(printTableContent *const cont, char relkind,
2303 Oid tablespace, const bool newline)
2305 /* relkinds for which we support tablespaces */
2306 if (relkind == 'r' || relkind == 'i')
2309 * We ignore the database default tablespace so that users not using
2310 * tablespaces don't need to know about them. This case also covers
2311 * pre-8.0 servers, for which tablespace will always be 0.
2313 if (tablespace != 0)
2315 PGresult *result = NULL;
2316 PQExpBufferData buf;
2318 initPQExpBuffer(&buf);
2319 printfPQExpBuffer(&buf,
2320 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2321 "WHERE oid = '%u';", tablespace);
2322 result = PSQLexec(buf.data, false);
2325 /* Should always be the case, but.... */
2326 if (PQntuples(result) > 0)
2330 /* Add the tablespace as a new footer */
2331 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2332 PQgetvalue(result, 0, 0));
2333 printTableAddFooter(cont, buf.data);
2337 /* Append the tablespace to the latest footer */
2338 printfPQExpBuffer(&buf, "%s", cont->footer->data);
2341 * translator: before this string there's an index
2342 * description like '"foo_pkey" PRIMARY KEY, btree (a)'
2344 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2345 PQgetvalue(result, 0, 0));
2346 printTableSetFooter(cont, buf.data);
2350 termPQExpBuffer(&buf);
2358 * Describes roles. Any schema portion of the pattern is ignored.
2361 describeRoles(const char *pattern, bool verbose)
2363 PQExpBufferData buf;
2365 printTableContent cont;
2366 printTableOpt myopt = pset.popt.topt;
2371 const char align = 'l';
2374 myopt.default_footer = false;
2376 initPQExpBuffer(&buf);
2378 if (pset.sversion >= 80100)
2380 printfPQExpBuffer(&buf,
2381 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2382 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2383 " r.rolconnlimit, r.rolvaliduntil,\n"
2384 " ARRAY(SELECT b.rolname\n"
2385 " FROM pg_catalog.pg_auth_members m\n"
2386 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2387 " WHERE m.member = r.oid) as memberof");
2389 if (verbose && pset.sversion >= 80200)
2391 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2394 if (pset.sversion >= 90100)
2396 appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2399 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2401 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2402 NULL, "r.rolname", NULL, NULL);
2406 printfPQExpBuffer(&buf,
2407 "SELECT u.usename AS rolname,\n"
2408 " u.usesuper AS rolsuper,\n"
2409 " true AS rolinherit, false AS rolcreaterole,\n"
2410 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2411 " -1 AS rolconnlimit,"
2412 " u.valuntil as rolvaliduntil,\n"
2413 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2414 "\nFROM pg_catalog.pg_user u\n");
2416 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2417 NULL, "u.usename", NULL, NULL);
2420 appendPQExpBuffer(&buf, "ORDER BY 1;");
2422 res = PSQLexec(buf.data, false);
2426 nrows = PQntuples(res);
2427 attr = pg_malloc0((nrows + 1) * sizeof(*attr));
2429 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2431 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2432 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2433 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2435 if (verbose && pset.sversion >= 80200)
2436 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2438 for (i = 0; i < nrows; i++)
2440 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2442 resetPQExpBuffer(&buf);
2443 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2444 add_role_attribute(&buf, _("Superuser"));
2446 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2447 add_role_attribute(&buf, _("No inheritance"));
2449 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2450 add_role_attribute(&buf, _("Create role"));
2452 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2453 add_role_attribute(&buf, _("Create DB"));
2455 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2456 add_role_attribute(&buf, _("Cannot login"));
2458 if (pset.sversion >= 90100)
2459 if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2460 add_role_attribute(&buf, _("Replication"));
2462 conns = atoi(PQgetvalue(res, i, 6));
2466 appendPQExpBufferStr(&buf, "\n");
2469 appendPQExpBuffer(&buf, _("No connections"));
2471 appendPQExpBuffer(&buf, ngettext("%d connection",
2477 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2480 appendPQExpBufferStr(&buf, "\n");
2481 appendPQExpBufferStr(&buf, _("Password valid until "));
2482 appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
2485 attr[i] = pg_strdup(buf.data);
2487 printTableAddCell(&cont, attr[i], false, false);
2489 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2491 if (verbose && pset.sversion >= 80200)
2492 printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
2494 termPQExpBuffer(&buf);
2496 printTable(&cont, pset.queryFout, pset.logfile);
2497 printTableCleanup(&cont);
2499 for (i = 0; i < nrows; i++)
2508 add_role_attribute(PQExpBuffer buf, const char *const str)
2511 appendPQExpBufferStr(buf, ", ");
2513 appendPQExpBufferStr(buf, str);
2520 listDbRoleSettings(const char *pattern, const char *pattern2)
2522 PQExpBufferData buf;
2524 printQueryOpt myopt = pset.popt;
2526 initPQExpBuffer(&buf);
2528 if (pset.sversion >= 90000)
2532 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
2533 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2534 "FROM pg_db_role_setting AS s\n"
2535 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2536 "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2537 gettext_noop("Role"),
2538 gettext_noop("Database"),
2539 gettext_noop("Settings"));
2540 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2541 NULL, "pg_roles.rolname", NULL, NULL);
2542 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2543 NULL, "pg_database.datname", NULL, NULL);
2544 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
2548 fprintf(pset.queryFout,
2549 _("No per-database role settings support in this server version.\n"));
2553 res = PSQLexec(buf.data, false);
2557 if (PQntuples(res) == 0 && !pset.quiet)
2560 fprintf(pset.queryFout, _("No matching settings found.\n"));
2562 fprintf(pset.queryFout, _("No settings found.\n"));
2566 myopt.nullPrint = NULL;
2567 myopt.title = _("List of settings");
2568 myopt.translate_header = true;
2570 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2574 resetPQExpBuffer(&buf);
2582 * handler for \dt, \di, etc.
2584 * tabtypes is an array of characters, specifying what info is desired:
2589 * E - foreign table (Note: different from 'f', the relkind value)
2590 * (any order of the above is fine)
2591 * If tabtypes is empty, we default to \dtvsE.
2594 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2596 bool showTables = strchr(tabtypes, 't') != NULL;
2597 bool showIndexes = strchr(tabtypes, 'i') != NULL;
2598 bool showViews = strchr(tabtypes, 'v') != NULL;
2599 bool showSeq = strchr(tabtypes, 's') != NULL;
2600 bool showForeign = strchr(tabtypes, 'E') != NULL;
2602 PQExpBufferData buf;
2604 printQueryOpt myopt = pset.popt;
2605 static const bool translate_columns[] = {false, false, true, false, false, false, false};
2607 if (!(showTables || showIndexes || showViews || showSeq || showForeign))
2608 showTables = showViews = showSeq = showForeign = true;
2610 initPQExpBuffer(&buf);
2613 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2614 * for backwards compatibility.
2616 printfPQExpBuffer(&buf,
2617 "SELECT n.nspname as \"%s\",\n"
2618 " c.relname as \"%s\",\n"
2619 " 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"
2620 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2621 gettext_noop("Schema"),
2622 gettext_noop("Name"),
2623 gettext_noop("table"),
2624 gettext_noop("view"),
2625 gettext_noop("index"),
2626 gettext_noop("sequence"),
2627 gettext_noop("special"),
2628 gettext_noop("foreign table"),
2629 gettext_noop("Type"),
2630 gettext_noop("Owner"));
2633 appendPQExpBuffer(&buf,
2634 ",\n c2.relname as \"%s\"",
2635 gettext_noop("Table"));
2640 * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
2641 * size of a table, including FSM, VM and TOAST tables.
2643 if (pset.sversion >= 90000)
2644 appendPQExpBuffer(&buf,
2645 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
2646 gettext_noop("Size"));
2647 else if (pset.sversion >= 80100)
2648 appendPQExpBuffer(&buf,
2649 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2650 gettext_noop("Size"));
2652 appendPQExpBuffer(&buf,
2653 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2654 gettext_noop("Description"));
2657 appendPQExpBuffer(&buf,
2658 "\nFROM pg_catalog.pg_class c"
2659 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2661 appendPQExpBuffer(&buf,
2662 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2663 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2665 appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
2667 appendPQExpBuffer(&buf, "'r',");
2669 appendPQExpBuffer(&buf, "'v',");
2671 appendPQExpBuffer(&buf, "'i',");
2673 appendPQExpBuffer(&buf, "'S',");
2674 if (showSystem || pattern)
2675 appendPQExpBuffer(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2678 appendPQExpBuffer(&buf, "'f',");
2680 appendPQExpBuffer(&buf, "''"); /* dummy */
2681 appendPQExpBuffer(&buf, ")\n");
2683 if (!showSystem && !pattern)
2684 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2685 " AND n.nspname <> 'information_schema'\n");
2688 * TOAST objects are suppressed unconditionally. Since we don't provide
2689 * any way to select relkind 't' above, we would never show toast tables
2690 * in any case; it seems a bit confusing to allow their indexes to be
2691 * shown. Use plain \d if you really need to look at a TOAST table/index.
2693 appendPQExpBuffer(&buf, " AND n.nspname !~ '^pg_toast'\n");
2695 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2696 "n.nspname", "c.relname", NULL,
2697 "pg_catalog.pg_table_is_visible(c.oid)");
2699 appendPQExpBuffer(&buf, "ORDER BY 1,2;");
2701 res = PSQLexec(buf.data, false);
2702 termPQExpBuffer(&buf);
2706 if (PQntuples(res) == 0 && !pset.quiet)
2709 fprintf(pset.queryFout, _("No matching relations found.\n"));
2711 fprintf(pset.queryFout, _("No relations found.\n"));
2715 myopt.nullPrint = NULL;
2716 myopt.title = _("List of relations");
2717 myopt.translate_header = true;
2718 myopt.translate_columns = translate_columns;
2720 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2731 * Describes languages.
2734 listLanguages(const char *pattern, bool verbose, bool showSystem)
2736 PQExpBufferData buf;
2738 printQueryOpt myopt = pset.popt;
2740 initPQExpBuffer(&buf);
2742 printfPQExpBuffer(&buf,
2743 "SELECT l.lanname AS \"%s\",\n",
2744 gettext_noop("Name"));
2745 if (pset.sversion >= 80300)
2746 appendPQExpBuffer(&buf,
2747 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
2748 gettext_noop("Owner"));
2750 appendPQExpBuffer(&buf,
2751 " l.lanpltrusted AS \"%s\"",
2752 gettext_noop("Trusted"));
2756 appendPQExpBuffer(&buf,
2757 ",\n NOT l.lanispl AS \"%s\",\n"
2758 " l.lanplcallfoid::regprocedure AS \"%s\",\n"
2759 " l.lanvalidator::regprocedure AS \"%s\",\n ",
2760 gettext_noop("Internal Language"),
2761 gettext_noop("Call Handler"),
2762 gettext_noop("Validator"));
2763 if (pset.sversion >= 90000)
2764 appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
2765 gettext_noop("Inline Handler"));
2766 printACLColumn(&buf, "l.lanacl");
2769 appendPQExpBuffer(&buf,
2770 ",\n d.description AS \"%s\""
2771 "\nFROM pg_catalog.pg_language l\n"
2772 "LEFT JOIN pg_catalog.pg_description d\n"
2773 " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
2774 " AND d.objsubid = 0\n",
2775 gettext_noop("Description"));
2778 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2779 NULL, "l.lanname", NULL, NULL);
2781 if (!showSystem && !pattern)
2782 appendPQExpBuffer(&buf, "WHERE l.lanplcallfoid != 0\n");
2785 appendPQExpBuffer(&buf, "ORDER BY 1;");
2787 res = PSQLexec(buf.data, false);
2788 termPQExpBuffer(&buf);
2792 myopt.nullPrint = NULL;
2793 myopt.title = _("List of languages");
2794 myopt.translate_header = true;
2796 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2806 * Describes domains.
2809 listDomains(const char *pattern, bool verbose, bool showSystem)
2811 PQExpBufferData buf;
2813 printQueryOpt myopt = pset.popt;
2815 initPQExpBuffer(&buf);
2817 printfPQExpBuffer(&buf,
2818 "SELECT n.nspname as \"%s\",\n"
2819 " t.typname as \"%s\",\n"
2820 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2822 gettext_noop("Schema"),
2823 gettext_noop("Name"),
2824 gettext_noop("Type"));
2826 if (pset.sversion >= 90100)
2827 appendPQExpBuffer(&buf,
2828 " COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
2829 " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
2830 appendPQExpBuffer(&buf,
2831 " CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
2832 " CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
2834 " pg_catalog.array_to_string(ARRAY(\n"
2835 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2836 " ), ' ') as \"%s\"",
2837 gettext_noop("Modifier"),
2838 gettext_noop("Check"));
2842 if (pset.sversion >= 90200)
2844 appendPQExpBuffer(&buf, ",\n ");
2845 printACLColumn(&buf, "t.typacl");
2847 appendPQExpBuffer(&buf,
2848 ",\n d.description as \"%s\"",
2849 gettext_noop("Description"));
2852 appendPQExpBuffer(&buf,
2853 "\nFROM pg_catalog.pg_type t\n"
2854 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
2857 appendPQExpBuffer(&buf,
2858 " LEFT JOIN pg_catalog.pg_description d "
2859 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
2860 "AND d.objsubid = 0\n");
2862 appendPQExpBuffer(&buf, "WHERE t.typtype = 'd'\n");
2864 if (!showSystem && !pattern)
2865 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2866 " AND n.nspname <> 'information_schema'\n");
2868 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2869 "n.nspname", "t.typname", NULL,
2870 "pg_catalog.pg_type_is_visible(t.oid)");
2872 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2874 res = PSQLexec(buf.data, false);
2875 termPQExpBuffer(&buf);
2879 myopt.nullPrint = NULL;
2880 myopt.title = _("List of domains");
2881 myopt.translate_header = true;
2883 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2892 * Describes conversions.
2895 listConversions(const char *pattern, bool verbose, bool showSystem)
2897 PQExpBufferData buf;
2899 printQueryOpt myopt = pset.popt;
2900 static const bool translate_columns[] = {false, false, false, false, true};
2902 initPQExpBuffer(&buf);
2904 printfPQExpBuffer(&buf,
2905 "SELECT n.nspname AS \"%s\",\n"
2906 " c.conname AS \"%s\",\n"
2907 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2908 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2909 " CASE WHEN c.condefault THEN '%s'\n"
2910 " ELSE '%s' END AS \"%s\"",
2911 gettext_noop("Schema"),
2912 gettext_noop("Name"),
2913 gettext_noop("Source"),
2914 gettext_noop("Destination"),
2915 gettext_noop("yes"), gettext_noop("no"),
2916 gettext_noop("Default?"));
2919 appendPQExpBuffer(&buf,
2920 ",\n d.description AS \"%s\"",
2921 gettext_noop("Description"));
2923 appendPQExpBuffer(&buf,
2924 "\nFROM pg_catalog.pg_conversion c\n"
2925 " JOIN pg_catalog.pg_namespace n "
2926 "ON n.oid = c.connamespace\n");
2929 appendPQExpBuffer(&buf,
2930 "LEFT JOIN pg_catalog.pg_description d "
2931 "ON d.classoid = c.tableoid\n"
2932 " AND d.objoid = c.oid "
2933 "AND d.objsubid = 0\n");
2935 appendPQExpBuffer(&buf, "WHERE true\n");
2937 if (!showSystem && !pattern)
2938 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2939 " AND n.nspname <> 'information_schema'\n");
2941 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2942 "n.nspname", "c.conname", NULL,
2943 "pg_catalog.pg_conversion_is_visible(c.oid)");
2945 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2947 res = PSQLexec(buf.data, false);
2948 termPQExpBuffer(&buf);
2952 myopt.nullPrint = NULL;
2953 myopt.title = _("List of conversions");
2954 myopt.translate_header = true;
2955 myopt.translate_columns = translate_columns;
2957 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2966 * Describes Event Triggers.
2969 listEventTriggers(const char *pattern, bool verbose)
2971 PQExpBufferData buf;
2973 printQueryOpt myopt = pset.popt;
2974 static const bool translate_columns[] =
2975 {false, false, false, true, false, false, false};
2977 initPQExpBuffer(&buf);
2979 printfPQExpBuffer(&buf,
2980 "select evtname as \"%s\", "
2981 "evtevent as \"%s\", "
2982 "pg_catalog.pg_get_userbyid(e.evtowner) AS \"%s\", "
2983 "case evtenabled when 'O' then 'enabled' "
2984 " when 'R' then 'replica' "
2985 " when 'A' then 'always' "
2986 " when 'D' then 'disabled' end as \"%s\", "
2987 "e.evtfoid::regproc as \"%s\", "
2988 "array_to_string(array(select x "
2989 " from unnest(evttags) as t(x)), ', ') as \"%s\" ",
2990 gettext_noop("Name"),
2991 gettext_noop("Event"),
2992 gettext_noop("Owner"),
2993 gettext_noop("Enabled"),
2994 gettext_noop("Procedure"),
2995 gettext_noop("Tags"));
2997 appendPQExpBuffer(&buf,
2998 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
2999 gettext_noop("Description"));
3000 appendPQExpBuffer(&buf,
3001 "\nFROM pg_event_trigger e ");
3003 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3004 NULL, "evtname", NULL, NULL);
3006 appendPQExpBuffer(&buf, "ORDER BY 1");
3008 res = PSQLexec(buf.data, false);
3009 termPQExpBuffer(&buf);
3013 myopt.nullPrint = NULL;
3014 myopt.title = _("List of event triggers");
3015 myopt.translate_header = true;
3016 myopt.translate_columns = translate_columns;
3018 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3030 listCasts(const char *pattern, bool verbose)
3032 PQExpBufferData buf;
3034 printQueryOpt myopt = pset.popt;
3035 static const bool translate_columns[] = {false, false, false, true};
3037 initPQExpBuffer(&buf);
3040 * We need a left join to pg_proc for binary casts; the others are just
3041 * paranoia. Also note that we don't attempt to localize '(binary
3042 * coercible)', because there's too much risk of gettext translating a
3043 * function name that happens to match some string in the PO database.
3045 printfPQExpBuffer(&buf,
3046 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3047 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3048 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3051 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3052 " WHEN c.castcontext = 'a' THEN '%s'\n"
3055 gettext_noop("Source type"),
3056 gettext_noop("Target type"),
3057 gettext_noop("Function"),
3059 gettext_noop("in assignment"),
3060 gettext_noop("yes"),
3061 gettext_noop("Implicit?"));
3064 appendPQExpBuffer(&buf,
3065 ",\n d.description AS \"%s\"\n",
3066 gettext_noop("Description"));
3068 appendPQExpBuffer(&buf,
3069 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3070 " ON c.castfunc = p.oid\n"
3071 " LEFT JOIN pg_catalog.pg_type ts\n"
3072 " ON c.castsource = ts.oid\n"
3073 " LEFT JOIN pg_catalog.pg_namespace ns\n"
3074 " ON ns.oid = ts.typnamespace\n"
3075 " LEFT JOIN pg_catalog.pg_type tt\n"
3076 " ON c.casttarget = tt.oid\n"
3077 " LEFT JOIN pg_catalog.pg_namespace nt\n"
3078 " ON nt.oid = tt.typnamespace\n");
3081 appendPQExpBuffer(&buf,
3082 " LEFT JOIN pg_catalog.pg_description d\n"
3083 " ON d.classoid = c.tableoid AND d.objoid = "
3084 "c.oid AND d.objsubid = 0\n");
3086 appendPQExpBuffer(&buf, "WHERE ( (true");
3089 * Match name pattern against either internal or external name of either
3090 * castsource or casttarget
3092 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3093 "ns.nspname", "ts.typname",
3094 "pg_catalog.format_type(ts.oid, NULL)",
3095 "pg_catalog.pg_type_is_visible(ts.oid)");
3097 appendPQExpBuffer(&buf, ") OR (true");
3099 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3100 "nt.nspname", "tt.typname",
3101 "pg_catalog.format_type(tt.oid, NULL)",
3102 "pg_catalog.pg_type_is_visible(tt.oid)");
3104 appendPQExpBuffer(&buf, ") )\nORDER BY 1, 2;");
3106 res = PSQLexec(buf.data, false);
3107 termPQExpBuffer(&buf);
3111 myopt.nullPrint = NULL;
3112 myopt.title = _("List of casts");
3113 myopt.translate_header = true;
3114 myopt.translate_columns = translate_columns;
3116 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3125 * Describes collations.
3128 listCollations(const char *pattern, bool verbose, bool showSystem)
3130 PQExpBufferData buf;
3132 printQueryOpt myopt = pset.popt;
3133 static const bool translate_columns[] = {false, false, false, false, false};
3135 if (pset.sversion < 90100)
3137 psql_error("The server (version %d.%d) does not support collations.\n",
3138 pset.sversion / 10000, (pset.sversion / 100) % 100);
3142 initPQExpBuffer(&buf);
3144 printfPQExpBuffer(&buf,
3145 "SELECT n.nspname AS \"%s\",\n"
3146 " c.collname AS \"%s\",\n"
3147 " c.collcollate AS \"%s\",\n"
3148 " c.collctype AS \"%s\"",
3149 gettext_noop("Schema"),
3150 gettext_noop("Name"),
3151 gettext_noop("Collate"),
3152 gettext_noop("Ctype"));
3155 appendPQExpBuffer(&buf,
3156 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3157 gettext_noop("Description"));
3159 appendPQExpBuffer(&buf,
3160 "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3161 "WHERE n.oid = c.collnamespace\n");
3163 if (!showSystem && !pattern)
3164 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
3165 " AND n.nspname <> 'information_schema'\n");
3168 * Hide collations that aren't usable in the current database's encoding.
3169 * If you think to change this, note that pg_collation_is_visible rejects
3170 * unusable collations, so you will need to hack name pattern processing
3171 * somehow to avoid inconsistent behavior.
3173 appendPQExpBuffer(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3175 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3176 "n.nspname", "c.collname", NULL,
3177 "pg_catalog.pg_collation_is_visible(c.oid)");
3179 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3181 res = PSQLexec(buf.data, false);
3182 termPQExpBuffer(&buf);
3186 myopt.nullPrint = NULL;
3187 myopt.title = _("List of collations");
3188 myopt.translate_header = true;
3189 myopt.translate_columns = translate_columns;
3191 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3200 * Describes schemas (namespaces)
3203 listSchemas(const char *pattern, bool verbose, bool showSystem)
3205 PQExpBufferData buf;
3207 printQueryOpt myopt = pset.popt;
3209 initPQExpBuffer(&buf);
3210 printfPQExpBuffer(&buf,
3211 "SELECT n.nspname AS \"%s\",\n"
3212 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3213 gettext_noop("Name"),
3214 gettext_noop("Owner"));
3218 appendPQExpBuffer(&buf, ",\n ");
3219 printACLColumn(&buf, "n.nspacl");
3220 appendPQExpBuffer(&buf,
3221 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3222 gettext_noop("Description"));
3225 appendPQExpBuffer(&buf,
3226 "\nFROM pg_catalog.pg_namespace n\n");
3228 if (!showSystem && !pattern)
3229 appendPQExpBuffer(&buf,
3230 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3232 processSQLNamePattern(pset.db, &buf, pattern,
3233 !showSystem && !pattern, false,
3234 NULL, "n.nspname", NULL,
3237 appendPQExpBuffer(&buf, "ORDER BY 1;");
3239 res = PSQLexec(buf.data, false);
3240 termPQExpBuffer(&buf);
3244 myopt.nullPrint = NULL;
3245 myopt.title = _("List of schemas");
3246 myopt.translate_header = true;
3248 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3257 * list text search parsers
3260 listTSParsers(const char *pattern, bool verbose)
3262 PQExpBufferData buf;
3264 printQueryOpt myopt = pset.popt;
3266 if (pset.sversion < 80300)
3268 psql_error("The server (version %d.%d) does not support full text search.\n",
3269 pset.sversion / 10000, (pset.sversion / 100) % 100);
3274 return listTSParsersVerbose(pattern);
3276 initPQExpBuffer(&buf);
3278 printfPQExpBuffer(&buf,
3280 " n.nspname as \"%s\",\n"
3281 " p.prsname as \"%s\",\n"
3282 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3283 "FROM pg_catalog.pg_ts_parser p \n"
3284 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3285 gettext_noop("Schema"),
3286 gettext_noop("Name"),
3287 gettext_noop("Description")
3290 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3291 "n.nspname", "p.prsname", NULL,
3292 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3294 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3296 res = PSQLexec(buf.data, false);
3297 termPQExpBuffer(&buf);
3301 myopt.nullPrint = NULL;
3302 myopt.title = _("List of text search parsers");
3303 myopt.translate_header = true;
3305 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3312 * full description of parsers
3315 listTSParsersVerbose(const char *pattern)
3317 PQExpBufferData buf;
3321 initPQExpBuffer(&buf);
3323 printfPQExpBuffer(&buf,
3327 "FROM pg_catalog.pg_ts_parser p\n"
3328 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3331 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3332 "n.nspname", "p.prsname", NULL,
3333 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3335 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3337 res = PSQLexec(buf.data, false);
3338 termPQExpBuffer(&buf);
3342 if (PQntuples(res) == 0)
3345 psql_error("Did not find any text search parser named \"%s\".\n",
3351 for (i = 0; i < PQntuples(res); i++)
3354 const char *nspname = NULL;
3355 const char *prsname;
3357 oid = PQgetvalue(res, i, 0);
3358 if (!PQgetisnull(res, i, 1))
3359 nspname = PQgetvalue(res, i, 1);
3360 prsname = PQgetvalue(res, i, 2);
3362 if (!describeOneTSParser(oid, nspname, prsname))
3380 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3382 PQExpBufferData buf;
3385 printQueryOpt myopt = pset.popt;
3386 static const bool translate_columns[] = {true, false, false};
3388 initPQExpBuffer(&buf);
3390 printfPQExpBuffer(&buf,
3391 "SELECT '%s' AS \"%s\", \n"
3392 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3393 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3394 " FROM pg_catalog.pg_ts_parser p \n"
3395 " WHERE p.oid = '%s' \n"
3398 " p.prstoken::pg_catalog.regproc, \n"
3399 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3400 " FROM pg_catalog.pg_ts_parser p \n"
3401 " WHERE p.oid = '%s' \n"
3404 " p.prsend::pg_catalog.regproc, \n"
3405 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3406 " FROM pg_catalog.pg_ts_parser p \n"
3407 " WHERE p.oid = '%s' \n"
3410 " p.prsheadline::pg_catalog.regproc, \n"
3411 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3412 " FROM pg_catalog.pg_ts_parser p \n"
3413 " WHERE p.oid = '%s' \n"
3416 " p.prslextype::pg_catalog.regproc, \n"
3417 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3418 " FROM pg_catalog.pg_ts_parser p \n"
3419 " WHERE p.oid = '%s';",
3420 gettext_noop("Start parse"),
3421 gettext_noop("Method"),
3422 gettext_noop("Function"),
3423 gettext_noop("Description"),
3425 gettext_noop("Get next token"),
3427 gettext_noop("End parse"),
3429 gettext_noop("Get headline"),
3431 gettext_noop("Get token types"),
3434 res = PSQLexec(buf.data, false);
3435 termPQExpBuffer(&buf);
3439 myopt.nullPrint = NULL;
3441 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3443 sprintf(title, _("Text search parser \"%s\""), prsname);
3444 myopt.title = title;
3445 myopt.footers = NULL;
3446 myopt.topt.default_footer = false;
3447 myopt.translate_header = true;
3448 myopt.translate_columns = translate_columns;
3450 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3454 initPQExpBuffer(&buf);
3456 printfPQExpBuffer(&buf,
3457 "SELECT t.alias as \"%s\", \n"
3458 " t.description as \"%s\" \n"
3459 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3461 gettext_noop("Token name"),
3462 gettext_noop("Description"),
3465 res = PSQLexec(buf.data, false);
3466 termPQExpBuffer(&buf);
3470 myopt.nullPrint = NULL;
3472 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3474 sprintf(title, _("Token types for parser \"%s\""), prsname);
3475 myopt.title = title;
3476 myopt.footers = NULL;
3477 myopt.topt.default_footer = true;
3478 myopt.translate_header = true;
3479 myopt.translate_columns = NULL;
3481 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3490 * list text search dictionaries
3493 listTSDictionaries(const char *pattern, bool verbose)
3495 PQExpBufferData buf;
3497 printQueryOpt myopt = pset.popt;
3499 if (pset.sversion < 80300)
3501 psql_error("The server (version %d.%d) does not support full text search.\n",
3502 pset.sversion / 10000, (pset.sversion / 100) % 100);
3506 initPQExpBuffer(&buf);
3508 printfPQExpBuffer(&buf,
3510 " n.nspname as \"%s\",\n"
3511 " d.dictname as \"%s\",\n",
3512 gettext_noop("Schema"),
3513 gettext_noop("Name"));
3517 appendPQExpBuffer(&buf,
3518 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3519 " pg_catalog.pg_ts_template t \n"
3520 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3521 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
3522 " d.dictinitoption as \"%s\", \n",
3523 gettext_noop("Template"),
3524 gettext_noop("Init options"));
3527 appendPQExpBuffer(&buf,
3528 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3529 gettext_noop("Description"));
3531 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3532 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3534 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3535 "n.nspname", "d.dictname", NULL,
3536 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3538 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3540 res = PSQLexec(buf.data, false);
3541 termPQExpBuffer(&buf);
3545 myopt.nullPrint = NULL;
3546 myopt.title = _("List of text search dictionaries");
3547 myopt.translate_header = true;
3549 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3558 * list text search templates
3561 listTSTemplates(const char *pattern, bool verbose)
3563 PQExpBufferData buf;
3565 printQueryOpt myopt = pset.popt;
3567 if (pset.sversion < 80300)
3569 psql_error("The server (version %d.%d) does not support full text search.\n",
3570 pset.sversion / 10000, (pset.sversion / 100) % 100);
3574 initPQExpBuffer(&buf);
3577 printfPQExpBuffer(&buf,
3579 " n.nspname AS \"%s\",\n"
3580 " t.tmplname AS \"%s\",\n"
3581 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3582 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3583 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3584 gettext_noop("Schema"),
3585 gettext_noop("Name"),
3586 gettext_noop("Init"),
3587 gettext_noop("Lexize"),
3588 gettext_noop("Description"));
3590 printfPQExpBuffer(&buf,
3592 " n.nspname AS \"%s\",\n"
3593 " t.tmplname AS \"%s\",\n"
3594 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3595 gettext_noop("Schema"),
3596 gettext_noop("Name"),
3597 gettext_noop("Description"));
3599 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
3600 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3602 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3603 "n.nspname", "t.tmplname", NULL,
3604 "pg_catalog.pg_ts_template_is_visible(t.oid)");
3606 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3608 res = PSQLexec(buf.data, false);
3609 termPQExpBuffer(&buf);
3613 myopt.nullPrint = NULL;
3614 myopt.title = _("List of text search templates");
3615 myopt.translate_header = true;
3617 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3626 * list text search configurations
3629 listTSConfigs(const char *pattern, bool verbose)
3631 PQExpBufferData buf;
3633 printQueryOpt myopt = pset.popt;
3635 if (pset.sversion < 80300)
3637 psql_error("The server (version %d.%d) does not support full text search.\n",
3638 pset.sversion / 10000, (pset.sversion / 100) % 100);
3643 return listTSConfigsVerbose(pattern);
3645 initPQExpBuffer(&buf);
3647 printfPQExpBuffer(&buf,
3649 " n.nspname as \"%s\",\n"
3650 " c.cfgname as \"%s\",\n"
3651 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3652 "FROM pg_catalog.pg_ts_config c\n"
3653 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3654 gettext_noop("Schema"),
3655 gettext_noop("Name"),
3656 gettext_noop("Description")
3659 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3660 "n.nspname", "c.cfgname", NULL,
3661 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3663 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3665 res = PSQLexec(buf.data, false);
3666 termPQExpBuffer(&buf);
3670 myopt.nullPrint = NULL;
3671 myopt.title = _("List of text search configurations");
3672 myopt.translate_header = true;
3674 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3681 listTSConfigsVerbose(const char *pattern)
3683 PQExpBufferData buf;
3687 initPQExpBuffer(&buf);
3689 printfPQExpBuffer(&buf,
3690 "SELECT c.oid, c.cfgname,\n"
3693 " np.nspname as pnspname \n"
3694 "FROM pg_catalog.pg_ts_config c \n"
3695 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3696 " pg_catalog.pg_ts_parser p \n"
3697 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3698 "WHERE p.oid = c.cfgparser\n"
3701 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3702 "n.nspname", "c.cfgname", NULL,
3703 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3705 appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
3707 res = PSQLexec(buf.data, false);
3708 termPQExpBuffer(&buf);
3712 if (PQntuples(res) == 0)
3715 psql_error("Did not find any text search configuration named \"%s\".\n",
3721 for (i = 0; i < PQntuples(res); i++)
3724 const char *cfgname;
3725 const char *nspname = NULL;
3726 const char *prsname;
3727 const char *pnspname = NULL;
3729 oid = PQgetvalue(res, i, 0);
3730 cfgname = PQgetvalue(res, i, 1);
3731 if (!PQgetisnull(res, i, 2))
3732 nspname = PQgetvalue(res, i, 2);
3733 prsname = PQgetvalue(res, i, 3);
3734 if (!PQgetisnull(res, i, 4))
3735 pnspname = PQgetvalue(res, i, 4);
3737 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
3755 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3756 const char *pnspname, const char *prsname)
3758 PQExpBufferData buf,
3761 printQueryOpt myopt = pset.popt;
3763 initPQExpBuffer(&buf);
3765 printfPQExpBuffer(&buf,
3767 " ( SELECT t.alias FROM \n"
3768 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3769 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3770 " pg_catalog.btrim( \n"
3771 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3772 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3773 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3774 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3775 " ) :: pg_catalog.text , \n"
3776 " '{}') AS \"%s\" \n"
3777 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3778 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3779 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3781 gettext_noop("Token"),
3782 gettext_noop("Dictionaries"),
3785 res = PSQLexec(buf.data, false);
3786 termPQExpBuffer(&buf);
3790 initPQExpBuffer(&title);
3793 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3796 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3800 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3803 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3806 myopt.nullPrint = NULL;
3807 myopt.title = title.data;
3808 myopt.footers = NULL;
3809 myopt.topt.default_footer = false;
3810 myopt.translate_header = true;
3812 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3814 termPQExpBuffer(&title);
3824 * Describes foreign-data wrappers
3827 listForeignDataWrappers(const char *pattern, bool verbose)
3829 PQExpBufferData buf;
3831 printQueryOpt myopt = pset.popt;
3833 if (pset.sversion < 80400)
3835 psql_error("The server (version %d.%d) does not support foreign-data wrappers.\n",
3836 pset.sversion / 10000, (pset.sversion / 100) % 100);
3840 initPQExpBuffer(&buf);
3841 printfPQExpBuffer(&buf,
3842 "SELECT fdw.fdwname AS \"%s\",\n"
3843 " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
3844 gettext_noop("Name"),
3845 gettext_noop("Owner"));
3846 if (pset.sversion >= 90100)
3847 appendPQExpBuffer(&buf,
3848 " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
3849 gettext_noop("Handler"));
3850 appendPQExpBuffer(&buf,
3851 " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
3852 gettext_noop("Validator"));
3856 appendPQExpBuffer(&buf, ",\n ");
3857 printACLColumn(&buf, "fdwacl");
3858 appendPQExpBuffer(&buf,
3859 ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
3860 " '(' || array_to_string(ARRAY(SELECT "
3861 " quote_ident(option_name) || ' ' || "
3862 " quote_literal(option_value) FROM "
3863 " pg_options_to_table(fdwoptions)), ', ') || ')' "
3865 gettext_noop("FDW Options"));
3867 if (pset.sversion >= 90100)
3868 appendPQExpBuffer(&buf,
3869 ",\n d.description AS \"%s\" ",
3870 gettext_noop("Description"));
3873 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
3875 if (verbose && pset.sversion >= 90100)
3876 appendPQExpBuffer(&buf,
3877 "LEFT JOIN pg_catalog.pg_description d\n"
3878 " ON d.classoid = fdw.tableoid "
3879 "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
3881 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3882 NULL, "fdwname", NULL, NULL);
3884 appendPQExpBuffer(&buf, "ORDER BY 1;");
3886 res = PSQLexec(buf.data, false);
3887 termPQExpBuffer(&buf);
3891 myopt.nullPrint = NULL;
3892 myopt.title = _("List of foreign-data wrappers");
3893 myopt.translate_header = true;
3895 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3904 * Describes foreign servers.
3907 listForeignServers(const char *pattern, bool verbose)
3909 PQExpBufferData buf;
3911 printQueryOpt myopt = pset.popt;
3913 if (pset.sversion < 80400)
3915 psql_error("The server (version %d.%d) does not support foreign servers.\n",
3916 pset.sversion / 10000, (pset.sversion / 100) % 100);
3920 initPQExpBuffer(&buf);
3921 printfPQExpBuffer(&buf,
3922 "SELECT s.srvname AS \"%s\",\n"
3923 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3924 " f.fdwname AS \"%s\"",
3925 gettext_noop("Name"),
3926 gettext_noop("Owner"),
3927 gettext_noop("Foreign-data wrapper"));
3931 appendPQExpBuffer(&buf, ",\n ");
3932 printACLColumn(&buf, "s.srvacl");
3933 appendPQExpBuffer(&buf,
3935 " s.srvtype AS \"%s\",\n"
3936 " s.srvversion AS \"%s\",\n"
3937 " CASE WHEN srvoptions IS NULL THEN '' ELSE "
3938 " '(' || array_to_string(ARRAY(SELECT "
3939 " quote_ident(option_name) || ' ' || "
3940 " quote_literal(option_value) FROM "
3941 " pg_options_to_table(srvoptions)), ', ') || ')' "
3943 " d.description AS \"%s\"",
3944 gettext_noop("Type"),
3945 gettext_noop("Version"),
3946 gettext_noop("FDW Options"),
3947 gettext_noop("Description"));
3950 appendPQExpBuffer(&buf,
3951 "\nFROM pg_catalog.pg_foreign_server s\n"
3952 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3955 appendPQExpBuffer(&buf,
3956 "LEFT JOIN pg_description d\n "
3957 "ON d.classoid = s.tableoid AND d.objoid = s.oid "
3958 "AND d.objsubid = 0\n");
3960 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3961 NULL, "s.srvname", NULL, NULL);
3963 appendPQExpBuffer(&buf, "ORDER BY 1;");
3965 res = PSQLexec(buf.data, false);
3966 termPQExpBuffer(&buf);
3970 myopt.nullPrint = NULL;
3971 myopt.title = _("List of foreign servers");
3972 myopt.translate_header = true;
3974 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3983 * Describes user mappings.
3986 listUserMappings(const char *pattern, bool verbose)
3988 PQExpBufferData buf;
3990 printQueryOpt myopt = pset.popt;
3992 if (pset.sversion < 80400)
3994 psql_error("The server (version %d.%d) does not support user mappings.\n",
3995 pset.sversion / 10000, (pset.sversion / 100) % 100);
3999 initPQExpBuffer(&buf);
4000 printfPQExpBuffer(&buf,
4001 "SELECT um.srvname AS \"%s\",\n"
4002 " um.usename AS \"%s\"",
4003 gettext_noop("Server"),
4004 gettext_noop("User name"));
4007 appendPQExpBuffer(&buf,
4008 ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4009 " '(' || array_to_string(ARRAY(SELECT "
4010 " quote_ident(option_name) || ' ' || "
4011 " quote_literal(option_value) FROM "
4012 " pg_options_to_table(umoptions)), ', ') || ')' "
4014 gettext_noop("FDW Options"));
4016 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4018 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4019 NULL, "um.srvname", "um.usename", NULL);
4021 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4023 res = PSQLexec(buf.data, false);
4024 termPQExpBuffer(&buf);
4028 myopt.nullPrint = NULL;
4029 myopt.title = _("List of user mappings");
4030 myopt.translate_header = true;
4032 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4041 * Describes foreign tables.
4044 listForeignTables(const char *pattern, bool verbose)
4046 PQExpBufferData buf;
4048 printQueryOpt myopt = pset.popt;
4050 if (pset.sversion < 90100)
4052 psql_error("The server (version %d.%d) does not support foreign tables.\n",
4053 pset.sversion / 10000, (pset.sversion / 100) % 100);
4057 initPQExpBuffer(&buf);
4058 printfPQExpBuffer(&buf,
4059 "SELECT n.nspname AS \"%s\",\n"
4060 " c.relname AS \"%s\",\n"
4061 " s.srvname AS \"%s\"",
4062 gettext_noop("Schema"),
4063 gettext_noop("Table"),
4064 gettext_noop("Server"));
4067 appendPQExpBuffer(&buf,
4068 ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4069 " '(' || array_to_string(ARRAY(SELECT "
4070 " quote_ident(option_name) || ' ' || "
4071 " quote_literal(option_value) FROM "
4072 " pg_options_to_table(ftoptions)), ', ') || ')' "
4074 " d.description AS \"%s\"",
4075 gettext_noop("FDW Options"),
4076 gettext_noop("Description"));
4078 appendPQExpBuffer(&buf,
4079 "\nFROM pg_catalog.pg_foreign_table ft\n"
4080 " INNER JOIN pg_catalog.pg_class c"
4081 " ON c.oid = ft.ftrelid\n"
4082 " INNER JOIN pg_catalog.pg_namespace n"
4083 " ON n.oid = c.relnamespace\n"
4084 " INNER JOIN pg_catalog.pg_foreign_server s"
4085 " ON s.oid = ft.ftserver\n");
4087 appendPQExpBuffer(&buf,
4088 " LEFT JOIN pg_catalog.pg_description d\n"
4089 " ON d.classoid = c.tableoid AND "
4090 "d.objoid = c.oid AND d.objsubid = 0\n");
4092 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4093 NULL, "n.nspname", "c.relname", NULL);
4095 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
4097 res = PSQLexec(buf.data, false);
4098 termPQExpBuffer(&buf);
4102 myopt.nullPrint = NULL;
4103 myopt.title = _("List of foreign tables");
4104 myopt.translate_header = true;
4106 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4115 * Briefly describes installed extensions.
4118 listExtensions(const char *pattern)
4120 PQExpBufferData buf;
4122 printQueryOpt myopt = pset.popt;
4124 if (pset.sversion < 90100)
4126 psql_error("The server (version %d.%d) does not support extensions.\n",
4127 pset.sversion / 10000, (pset.sversion / 100) % 100);
4131 initPQExpBuffer(&buf);
4132 printfPQExpBuffer(&buf,
4133 "SELECT e.extname AS \"%s\", "
4134 "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4135 "FROM pg_catalog.pg_extension e "
4136 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4137 "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4138 "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4139 gettext_noop("Name"),
4140 gettext_noop("Version"),
4141 gettext_noop("Schema"),
4142 gettext_noop("Description"));
4144 processSQLNamePattern(pset.db, &buf, pattern,
4146 NULL, "e.extname", NULL,
4149 appendPQExpBuffer(&buf, "ORDER BY 1;");
4151 res = PSQLexec(buf.data, false);
4152 termPQExpBuffer(&buf);
4156 myopt.nullPrint = NULL;
4157 myopt.title = _("List of installed extensions");
4158 myopt.translate_header = true;
4160 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4169 * List contents of installed extensions.
4172 listExtensionContents(const char *pattern)
4174 PQExpBufferData buf;
4178 if (pset.sversion < 90100)
4180 psql_error("The server (version %d.%d) does not support extensions.\n",
4181 pset.sversion / 10000, (pset.sversion / 100) % 100);
4185 initPQExpBuffer(&buf);
4186 printfPQExpBuffer(&buf,
4187 "SELECT e.extname, e.oid\n"
4188 "FROM pg_catalog.pg_extension e\n");
4190 processSQLNamePattern(pset.db, &buf, pattern,
4192 NULL, "e.extname", NULL,
4195 appendPQExpBuffer(&buf, "ORDER BY 1;");
4197 res = PSQLexec(buf.data, false);
4198 termPQExpBuffer(&buf);
4202 if (PQntuples(res) == 0)
4207 psql_error("Did not find any extension named \"%s\".\n",
4210 psql_error("Did not find any extensions.\n");
4216 for (i = 0; i < PQntuples(res); i++)
4218 const char *extname;
4221 extname = PQgetvalue(res, i, 0);
4222 oid = PQgetvalue(res, i, 1);
4224 if (!listOneExtensionContents(extname, oid))
4241 listOneExtensionContents(const char *extname, const char *oid)
4243 PQExpBufferData buf;
4246 printQueryOpt myopt = pset.popt;
4248 initPQExpBuffer(&buf);
4249 printfPQExpBuffer(&buf,
4250 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4251 "FROM pg_catalog.pg_depend\n"
4252 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4254 gettext_noop("Object Description"),
4257 res = PSQLexec(buf.data, false);
4258 termPQExpBuffer(&buf);
4262 myopt.nullPrint = NULL;
4263 snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4264 myopt.title = title;
4265 myopt.translate_header = true;
4267 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4276 * Helper function for consistently formatting ACL (privilege) columns.
4277 * The proper targetlist entry is appended to buf. Note lack of any
4278 * whitespace or comma decoration.
4281 printACLColumn(PQExpBuffer buf, const char *colname)
4283 if (pset.sversion >= 80100)
4284 appendPQExpBuffer(buf,
4285 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4286 colname, gettext_noop("Access privileges"));
4288 appendPQExpBuffer(buf,
4289 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4290 colname, gettext_noop("Access privileges"));