2 * psql - the PostgreSQL interactive terminal
4 * Support for the various \d ("describe") commands. Note that the current
5 * expectation is that all functions in this file will succeed when working
6 * with servers of versions 7.4 and up. It's okay to omit irrelevant
7 * information for an old server, but not to fail outright.
9 * Copyright (c) 2000-2014, PostgreSQL Global Development Group
11 * src/bin/psql/describe.c
13 #include "postgres_fe.h"
17 #include "catalog/pg_default_acl.h"
21 #include "dumputils.h"
25 #include "variables.h"
28 static bool describeOneTableDetails(const char *schemaname,
29 const char *relationname,
32 static void add_tablespace_footer(printTableContent *const cont, char relkind,
33 Oid tablespace, const bool newline);
34 static void add_role_attribute(PQExpBuffer buf, const char *const str);
35 static bool listTSParsersVerbose(const char *pattern);
36 static bool describeOneTSParser(const char *oid, const char *nspname,
38 static bool listTSConfigsVerbose(const char *pattern);
39 static bool describeOneTSConfig(const char *oid, const char *nspname,
41 const char *pnspname, const char *prsname);
42 static void printACLColumn(PQExpBuffer buf, const char *colname);
43 static bool listOneExtensionContents(const char *extname, const char *oid);
47 * Handlers for various slash commands displaying some sort of list
48 * of things in the database.
50 * Note: try to format the queries to look nice in -E output.
56 * Takes an optional regexp to select particular aggregates
59 describeAggregates(const char *pattern, bool verbose, bool showSystem)
63 printQueryOpt myopt = pset.popt;
65 initPQExpBuffer(&buf);
67 printfPQExpBuffer(&buf,
68 "SELECT n.nspname as \"%s\",\n"
69 " p.proname AS \"%s\",\n"
70 " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
71 gettext_noop("Schema"),
73 gettext_noop("Result data type"));
75 if (pset.sversion >= 80400)
76 appendPQExpBuffer(&buf,
77 " CASE WHEN p.pronargs = 0\n"
78 " THEN CAST('*' AS pg_catalog.text)\n"
79 " ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"
81 gettext_noop("Argument data types"));
82 else if (pset.sversion >= 80200)
83 appendPQExpBuffer(&buf,
84 " CASE WHEN p.pronargs = 0\n"
85 " THEN CAST('*' AS pg_catalog.text)\n"
87 " pg_catalog.array_to_string(ARRAY(\n"
89 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
91 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
94 gettext_noop("Argument data types"));
96 appendPQExpBuffer(&buf,
97 " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
98 gettext_noop("Argument data types"));
100 appendPQExpBuffer(&buf,
101 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
102 "FROM pg_catalog.pg_proc p\n"
103 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
104 "WHERE p.proisagg\n",
105 gettext_noop("Description"));
107 if (!showSystem && !pattern)
108 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
109 " AND n.nspname <> 'information_schema'\n");
111 processSQLNamePattern(pset.db, &buf, pattern, true, false,
112 "n.nspname", "p.proname", NULL,
113 "pg_catalog.pg_function_is_visible(p.oid)");
115 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
117 res = PSQLexec(buf.data);
118 termPQExpBuffer(&buf);
122 myopt.nullPrint = NULL;
123 myopt.title = _("List of aggregate functions");
124 myopt.translate_header = true;
126 printQuery(res, &myopt, pset.queryFout, pset.logfile);
133 * Takes an optional regexp to select particular tablespaces
136 describeTablespaces(const char *pattern, bool verbose)
140 printQueryOpt myopt = pset.popt;
142 if (pset.sversion < 80000)
144 psql_error("The server (version %d.%d) does not support tablespaces.\n",
145 pset.sversion / 10000, (pset.sversion / 100) % 100);
149 initPQExpBuffer(&buf);
151 if (pset.sversion >= 90200)
152 printfPQExpBuffer(&buf,
153 "SELECT spcname AS \"%s\",\n"
154 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
155 " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
156 gettext_noop("Name"),
157 gettext_noop("Owner"),
158 gettext_noop("Location"));
160 printfPQExpBuffer(&buf,
161 "SELECT spcname AS \"%s\",\n"
162 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
163 " spclocation AS \"%s\"",
164 gettext_noop("Name"),
165 gettext_noop("Owner"),
166 gettext_noop("Location"));
170 appendPQExpBufferStr(&buf, ",\n ");
171 printACLColumn(&buf, "spcacl");
174 if (verbose && pset.sversion >= 90000)
175 appendPQExpBuffer(&buf,
176 ",\n spcoptions AS \"%s\"",
177 gettext_noop("Options"));
179 if (verbose && pset.sversion >= 90200)
180 appendPQExpBuffer(&buf,
181 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\"",
182 gettext_noop("Size"));
184 if (verbose && pset.sversion >= 80200)
185 appendPQExpBuffer(&buf,
186 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
187 gettext_noop("Description"));
189 appendPQExpBufferStr(&buf,
190 "\nFROM pg_catalog.pg_tablespace\n");
192 processSQLNamePattern(pset.db, &buf, pattern, false, false,
193 NULL, "spcname", NULL,
196 appendPQExpBufferStr(&buf, "ORDER BY 1;");
198 res = PSQLexec(buf.data);
199 termPQExpBuffer(&buf);
203 myopt.nullPrint = NULL;
204 myopt.title = _("List of tablespaces");
205 myopt.translate_header = true;
207 printQuery(res, &myopt, pset.queryFout, pset.logfile);
215 * Takes an optional regexp to select particular functions.
217 * As with \d, you can specify the kinds of functions you want:
224 * and you can mix and match these in any order.
227 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
229 bool showAggregate = strchr(functypes, 'a') != NULL;
230 bool showNormal = strchr(functypes, 'n') != NULL;
231 bool showTrigger = strchr(functypes, 't') != NULL;
232 bool showWindow = strchr(functypes, 'w') != NULL;
236 printQueryOpt myopt = pset.popt;
237 static const bool translate_columns[] = {false, false, false, false, true, true, true, false, false, false, false};
239 if (strlen(functypes) != strspn(functypes, "antwS+"))
241 psql_error("\\df only takes [antwS+] as options\n");
245 if (showWindow && pset.sversion < 80400)
247 psql_error("\\df does not take a \"w\" option with server version %d.%d\n",
248 pset.sversion / 10000, (pset.sversion / 100) % 100);
252 if (!showAggregate && !showNormal && !showTrigger && !showWindow)
254 showAggregate = showNormal = showTrigger = true;
255 if (pset.sversion >= 80400)
259 initPQExpBuffer(&buf);
261 printfPQExpBuffer(&buf,
262 "SELECT n.nspname as \"%s\",\n"
263 " p.proname as \"%s\",\n",
264 gettext_noop("Schema"),
265 gettext_noop("Name"));
267 if (pset.sversion >= 80400)
268 appendPQExpBuffer(&buf,
269 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
270 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
272 " WHEN p.proisagg THEN '%s'\n"
273 " WHEN p.proiswindow THEN '%s'\n"
274 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
277 gettext_noop("Result data type"),
278 gettext_noop("Argument data types"),
279 /* translator: "agg" is short for "aggregate" */
281 gettext_noop("window"),
282 gettext_noop("trigger"),
283 gettext_noop("normal"),
284 gettext_noop("Type"));
285 else if (pset.sversion >= 80100)
286 appendPQExpBuffer(&buf,
287 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
288 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
289 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
290 " pg_catalog.array_to_string(ARRAY(\n"
293 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
294 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
295 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
296 " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
299 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
300 " ELSE p.proargnames[s.i] || ' ' \n"
302 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
304 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
307 " pg_catalog.array_to_string(ARRAY(\n"
310 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
311 " ELSE p.proargnames[s.i+1] || ' '\n"
313 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
315 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\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"));
331 appendPQExpBuffer(&buf,
332 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
333 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
334 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
336 " WHEN p.proisagg THEN '%s'\n"
337 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
340 gettext_noop("Result data type"),
341 gettext_noop("Argument data types"),
342 /* translator: "agg" is short for "aggregate" */
344 gettext_noop("trigger"),
345 gettext_noop("normal"),
346 gettext_noop("Type"));
349 appendPQExpBuffer(&buf,
350 ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\""
352 " WHEN p.provolatile = 'i' THEN '%s'\n"
353 " WHEN p.provolatile = 's' THEN '%s'\n"
354 " WHEN p.provolatile = 'v' THEN '%s'\n"
356 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
357 " l.lanname as \"%s\",\n"
358 " p.prosrc as \"%s\",\n"
359 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
360 gettext_noop("definer"),
361 gettext_noop("invoker"),
362 gettext_noop("Security"),
363 gettext_noop("immutable"),
364 gettext_noop("stable"),
365 gettext_noop("volatile"),
366 gettext_noop("Volatility"),
367 gettext_noop("Owner"),
368 gettext_noop("Language"),
369 gettext_noop("Source code"),
370 gettext_noop("Description"));
372 appendPQExpBufferStr(&buf,
373 "\nFROM pg_catalog.pg_proc p"
374 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
377 appendPQExpBufferStr(&buf,
378 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
382 /* filter by function type, if requested */
383 if (showNormal && showAggregate && showTrigger && showWindow)
390 appendPQExpBufferStr(&buf, " AND ");
393 appendPQExpBufferStr(&buf, "WHERE ");
396 appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
401 appendPQExpBufferStr(&buf, " AND ");
404 appendPQExpBufferStr(&buf, "WHERE ");
407 appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
409 if (!showWindow && pset.sversion >= 80400)
412 appendPQExpBufferStr(&buf, " AND ");
415 appendPQExpBufferStr(&buf, "WHERE ");
418 appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
423 bool needs_or = false;
425 appendPQExpBufferStr(&buf, "WHERE (\n ");
427 /* Note: at least one of these must be true ... */
430 appendPQExpBufferStr(&buf, "p.proisagg\n");
436 appendPQExpBufferStr(&buf, " OR ");
437 appendPQExpBufferStr(&buf,
438 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
444 appendPQExpBufferStr(&buf, " OR ");
445 appendPQExpBufferStr(&buf, "p.proiswindow\n");
448 appendPQExpBufferStr(&buf, " )\n");
451 processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
452 "n.nspname", "p.proname", NULL,
453 "pg_catalog.pg_function_is_visible(p.oid)");
455 if (!showSystem && !pattern)
456 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
457 " AND n.nspname <> 'information_schema'\n");
459 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
461 res = PSQLexec(buf.data);
462 termPQExpBuffer(&buf);
466 myopt.nullPrint = NULL;
467 myopt.title = _("List of functions");
468 myopt.translate_header = true;
469 myopt.translate_columns = translate_columns;
470 myopt.n_translate_columns = lengthof(translate_columns);
472 printQuery(res, &myopt, pset.queryFout, pset.logfile);
485 describeTypes(const char *pattern, bool verbose, bool showSystem)
489 printQueryOpt myopt = pset.popt;
491 initPQExpBuffer(&buf);
493 printfPQExpBuffer(&buf,
494 "SELECT n.nspname as \"%s\",\n"
495 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
496 gettext_noop("Schema"),
497 gettext_noop("Name"));
499 appendPQExpBuffer(&buf,
500 " t.typname AS \"%s\",\n"
501 " CASE WHEN t.typrelid != 0\n"
502 " THEN CAST('tuple' AS pg_catalog.text)\n"
503 " WHEN t.typlen < 0\n"
504 " THEN CAST('var' AS pg_catalog.text)\n"
505 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
507 gettext_noop("Internal name"),
508 gettext_noop("Size"));
509 if (verbose && pset.sversion >= 80300)
511 appendPQExpBufferStr(&buf,
512 " pg_catalog.array_to_string(\n"
514 " SELECT e.enumlabel\n"
515 " FROM pg_catalog.pg_enum e\n"
516 " WHERE e.enumtypid = t.oid\n");
518 if (pset.sversion >= 90100)
519 appendPQExpBufferStr(&buf,
520 " ORDER BY e.enumsortorder\n");
522 appendPQExpBufferStr(&buf,
523 " ORDER BY e.oid\n");
525 appendPQExpBuffer(&buf,
529 gettext_noop("Elements"));
531 if (verbose && pset.sversion >= 90200)
533 printACLColumn(&buf, "t.typacl");
534 appendPQExpBufferStr(&buf, ",\n ");
537 appendPQExpBuffer(&buf,
538 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
539 gettext_noop("Description"));
541 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
542 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
545 * do not include complex types (typrelid!=0) unless they are standalone
548 appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
549 appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
550 "WHERE c.oid = t.typrelid))\n");
553 * do not include array types (before 8.3 we have to use the assumption
554 * that their names start with underscore)
556 if (pset.sversion >= 80300)
557 appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
559 appendPQExpBufferStr(&buf, " AND t.typname !~ '^_'\n");
561 if (!showSystem && !pattern)
562 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
563 " AND n.nspname <> 'information_schema'\n");
565 /* Match name pattern against either internal or external name */
566 processSQLNamePattern(pset.db, &buf, pattern, true, false,
567 "n.nspname", "t.typname",
568 "pg_catalog.format_type(t.oid, NULL)",
569 "pg_catalog.pg_type_is_visible(t.oid)");
571 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
573 res = PSQLexec(buf.data);
574 termPQExpBuffer(&buf);
578 myopt.nullPrint = NULL;
579 myopt.title = _("List of data types");
580 myopt.translate_header = true;
582 printQuery(res, &myopt, pset.queryFout, pset.logfile);
593 describeOperators(const char *pattern, bool verbose, bool showSystem)
597 printQueryOpt myopt = pset.popt;
599 initPQExpBuffer(&buf);
602 * Note: before Postgres 9.1, we did not assign comments to any built-in
603 * operators, preferring to let the comment on the underlying function
604 * suffice. The coalesce() on the obj_description() calls below supports
605 * this convention by providing a fallback lookup of a comment on the
606 * operator's function. As of 9.1 there is a policy that every built-in
607 * operator should have a comment; so the coalesce() is no longer
608 * necessary so far as built-in operators are concerned. We keep it
609 * anyway, for now, because (1) third-party modules may still be following
610 * the old convention, and (2) we'd need to do it anyway when talking to a
614 printfPQExpBuffer(&buf,
615 "SELECT n.nspname as \"%s\",\n"
616 " o.oprname AS \"%s\",\n"
617 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
618 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
619 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
620 gettext_noop("Schema"),
621 gettext_noop("Name"),
622 gettext_noop("Left arg type"),
623 gettext_noop("Right arg type"),
624 gettext_noop("Result type"));
627 appendPQExpBuffer(&buf,
628 " o.oprcode AS \"%s\",\n",
629 gettext_noop("Function"));
631 appendPQExpBuffer(&buf,
632 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
633 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
634 "FROM pg_catalog.pg_operator o\n"
635 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
636 gettext_noop("Description"));
638 if (!showSystem && !pattern)
639 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
640 " AND n.nspname <> 'information_schema'\n");
642 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
643 "n.nspname", "o.oprname", NULL,
644 "pg_catalog.pg_operator_is_visible(o.oid)");
646 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
648 res = PSQLexec(buf.data);
649 termPQExpBuffer(&buf);
653 myopt.nullPrint = NULL;
654 myopt.title = _("List of operators");
655 myopt.translate_header = true;
657 printQuery(res, &myopt, pset.queryFout, pset.logfile);
667 * for \l, \list, and -l switch
670 listAllDbs(const char *pattern, bool verbose)
674 printQueryOpt myopt = pset.popt;
676 initPQExpBuffer(&buf);
678 printfPQExpBuffer(&buf,
679 "SELECT d.datname as \"%s\",\n"
680 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
681 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
682 gettext_noop("Name"),
683 gettext_noop("Owner"),
684 gettext_noop("Encoding"));
685 if (pset.sversion >= 80400)
686 appendPQExpBuffer(&buf,
687 " d.datcollate as \"%s\",\n"
688 " d.datctype as \"%s\",\n",
689 gettext_noop("Collate"),
690 gettext_noop("Ctype"));
691 appendPQExpBufferStr(&buf, " ");
692 printACLColumn(&buf, "d.datacl");
693 if (verbose && pset.sversion >= 80200)
694 appendPQExpBuffer(&buf,
695 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
696 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
697 " ELSE 'No Access'\n"
699 gettext_noop("Size"));
700 if (verbose && pset.sversion >= 80000)
701 appendPQExpBuffer(&buf,
702 ",\n t.spcname as \"%s\"",
703 gettext_noop("Tablespace"));
704 if (verbose && pset.sversion >= 80200)
705 appendPQExpBuffer(&buf,
706 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
707 gettext_noop("Description"));
708 appendPQExpBufferStr(&buf,
709 "\nFROM pg_catalog.pg_database d\n");
710 if (verbose && pset.sversion >= 80000)
711 appendPQExpBufferStr(&buf,
712 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
715 processSQLNamePattern(pset.db, &buf, pattern, false, false,
716 NULL, "d.datname", NULL, NULL);
718 appendPQExpBufferStr(&buf, "ORDER BY 1;");
719 res = PSQLexec(buf.data);
720 termPQExpBuffer(&buf);
724 myopt.nullPrint = NULL;
725 myopt.title = _("List of databases");
726 myopt.translate_header = true;
728 printQuery(res, &myopt, pset.queryFout, pset.logfile);
736 * List Tables' Grant/Revoke Permissions
737 * \z (now also \dp -- perhaps more mnemonic)
740 permissionsList(const char *pattern)
744 printQueryOpt myopt = pset.popt;
745 static const bool translate_columns[] = {false, false, true, false, false, false};
747 initPQExpBuffer(&buf);
750 * we ignore indexes and toast tables since they have no meaningful rights
752 printfPQExpBuffer(&buf,
753 "SELECT n.nspname as \"%s\",\n"
754 " c.relname as \"%s\",\n"
756 " WHEN 'r' THEN '%s'"
757 " WHEN 'v' THEN '%s'"
758 " WHEN 'm' THEN '%s'"
759 " WHEN 'S' THEN '%s'"
760 " WHEN 'f' THEN '%s'"
763 gettext_noop("Schema"),
764 gettext_noop("Name"),
765 gettext_noop("table"),
766 gettext_noop("view"),
767 gettext_noop("materialized view"),
768 gettext_noop("sequence"),
769 gettext_noop("foreign table"),
770 gettext_noop("Type"));
772 printACLColumn(&buf, "c.relacl");
774 if (pset.sversion >= 80400)
775 appendPQExpBuffer(&buf,
776 ",\n pg_catalog.array_to_string(ARRAY(\n"
777 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
778 " FROM pg_catalog.pg_attribute a\n"
779 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
780 " ), E'\\n') AS \"%s\"",
781 gettext_noop("Column privileges"));
783 if (pset.sversion >= 90500)
784 appendPQExpBuffer(&buf,
785 ",\n pg_catalog.array_to_string(ARRAY(\n"
787 " || CASE WHEN polcmd IS NOT NULL THEN\n"
788 " E' (' || polcmd || E')'\n"
791 " || CASE WHEN polqual IS NOT NULL THEN\n"
792 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
795 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
796 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
799 " || CASE WHEN polroles <> '{0}' THEN\n"
800 " E'\\n to: ' || pg_catalog.array_to_string(\n"
803 " FROM pg_catalog.pg_roles\n"
804 " WHERE oid = ANY (polroles)\n"
809 " FROM pg_catalog.pg_policy pol\n"
810 " WHERE polrelid = c.oid), E'\\n')\n"
812 gettext_noop("Policies"));
814 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
815 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
816 "WHERE c.relkind IN ('r', 'v', 'm', 'S', 'f')\n");
819 * Unless a schema pattern is specified, we suppress system and temp
820 * tables, since they normally aren't very interesting from a permissions
821 * point of view. You can see 'em by explicit request though, eg with \z
824 processSQLNamePattern(pset.db, &buf, pattern, true, false,
825 "n.nspname", "c.relname", NULL,
826 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
828 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
830 res = PSQLexec(buf.data);
833 termPQExpBuffer(&buf);
837 myopt.nullPrint = NULL;
838 printfPQExpBuffer(&buf, _("Access privileges"));
839 myopt.title = buf.data;
840 myopt.translate_header = true;
841 myopt.translate_columns = translate_columns;
842 myopt.n_translate_columns = lengthof(translate_columns);
844 printQuery(res, &myopt, pset.queryFout, pset.logfile);
846 termPQExpBuffer(&buf);
855 * List Default ACLs. The pattern can match either schema or role name.
858 listDefaultACLs(const char *pattern)
862 printQueryOpt myopt = pset.popt;
863 static const bool translate_columns[] = {false, false, true, false};
865 if (pset.sversion < 90000)
867 psql_error("The server (version %d.%d) does not support altering default privileges.\n",
868 pset.sversion / 10000, (pset.sversion / 100) % 100);
872 initPQExpBuffer(&buf);
874 printfPQExpBuffer(&buf,
875 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
876 " n.nspname AS \"%s\",\n"
877 " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
879 gettext_noop("Owner"),
880 gettext_noop("Schema"),
882 gettext_noop("table"),
884 gettext_noop("sequence"),
886 gettext_noop("function"),
888 gettext_noop("type"),
889 gettext_noop("Type"));
891 printACLColumn(&buf, "d.defaclacl");
893 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
894 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
896 processSQLNamePattern(pset.db, &buf, pattern, false, false,
899 "pg_catalog.pg_get_userbyid(d.defaclrole)",
902 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
904 res = PSQLexec(buf.data);
907 termPQExpBuffer(&buf);
911 myopt.nullPrint = NULL;
912 printfPQExpBuffer(&buf, _("Default access privileges"));
913 myopt.title = buf.data;
914 myopt.translate_header = true;
915 myopt.translate_columns = translate_columns;
916 myopt.n_translate_columns = lengthof(translate_columns);
918 printQuery(res, &myopt, pset.queryFout, pset.logfile);
920 termPQExpBuffer(&buf);
927 * Get object comments
931 * Note: This command only lists comments for object types which do not have
932 * their comments displayed by their own backslash commands. The following
933 * types of objects will be displayed: constraint, operator class,
934 * operator family, rule, and trigger.
938 objectDescription(const char *pattern, bool showSystem)
942 printQueryOpt myopt = pset.popt;
943 static const bool translate_columns[] = {false, false, true, false};
945 initPQExpBuffer(&buf);
947 appendPQExpBuffer(&buf,
948 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
950 gettext_noop("Schema"),
951 gettext_noop("Name"),
952 gettext_noop("Object"),
953 gettext_noop("Description"));
955 /* Constraint descriptions */
956 appendPQExpBuffer(&buf,
957 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
958 " n.nspname as nspname,\n"
959 " CAST(pgc.conname AS pg_catalog.text) as name,"
960 " CAST('%s' AS pg_catalog.text) as object\n"
961 " FROM pg_catalog.pg_constraint pgc\n"
962 " JOIN pg_catalog.pg_class c "
963 "ON c.oid = pgc.conrelid\n"
964 " LEFT JOIN pg_catalog.pg_namespace n "
965 " ON n.oid = c.relnamespace\n",
966 gettext_noop("constraint"));
968 if (!showSystem && !pattern)
969 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
970 " AND n.nspname <> 'information_schema'\n");
972 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern,
973 false, "n.nspname", "pgc.conname", NULL,
974 "pg_catalog.pg_table_is_visible(c.oid)");
977 * pg_opclass.opcmethod only available in 8.3+
979 if (pset.sversion >= 80300)
981 /* Operator class descriptions */
982 appendPQExpBuffer(&buf,
984 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
985 " n.nspname as nspname,\n"
986 " CAST(o.opcname AS pg_catalog.text) as name,\n"
987 " CAST('%s' AS pg_catalog.text) as object\n"
988 " FROM pg_catalog.pg_opclass o\n"
989 " JOIN pg_catalog.pg_am am ON "
990 "o.opcmethod = am.oid\n"
991 " JOIN pg_catalog.pg_namespace n ON "
992 "n.oid = o.opcnamespace\n",
993 gettext_noop("operator class"));
995 if (!showSystem && !pattern)
996 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
997 " AND n.nspname <> 'information_schema'\n");
999 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1000 "n.nspname", "o.opcname", NULL,
1001 "pg_catalog.pg_opclass_is_visible(o.oid)");
1005 * although operator family comments have been around since 8.3,
1006 * pg_opfamily_is_visible is only available in 9.2+
1008 if (pset.sversion >= 90200)
1010 /* Operator family descriptions */
1011 appendPQExpBuffer(&buf,
1013 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1014 " n.nspname as nspname,\n"
1015 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1016 " CAST('%s' AS pg_catalog.text) as object\n"
1017 " FROM pg_catalog.pg_opfamily opf\n"
1018 " JOIN pg_catalog.pg_am am "
1019 "ON opf.opfmethod = am.oid\n"
1020 " JOIN pg_catalog.pg_namespace n "
1021 "ON opf.opfnamespace = n.oid\n",
1022 gettext_noop("operator family"));
1024 if (!showSystem && !pattern)
1025 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1026 " AND n.nspname <> 'information_schema'\n");
1028 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1029 "n.nspname", "opf.opfname", NULL,
1030 "pg_catalog.pg_opfamily_is_visible(opf.oid)");
1033 /* Rule descriptions (ignore rules for views) */
1034 appendPQExpBuffer(&buf,
1036 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1037 " n.nspname as nspname,\n"
1038 " CAST(r.rulename AS pg_catalog.text) as name,"
1039 " CAST('%s' AS pg_catalog.text) as object\n"
1040 " FROM pg_catalog.pg_rewrite r\n"
1041 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1042 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1043 " WHERE r.rulename != '_RETURN'\n",
1044 gettext_noop("rule"));
1046 if (!showSystem && !pattern)
1047 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1048 " AND n.nspname <> 'information_schema'\n");
1050 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1051 "n.nspname", "r.rulename", NULL,
1052 "pg_catalog.pg_table_is_visible(c.oid)");
1054 /* Trigger descriptions */
1055 appendPQExpBuffer(&buf,
1057 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1058 " n.nspname as nspname,\n"
1059 " CAST(t.tgname AS pg_catalog.text) as name,"
1060 " CAST('%s' AS pg_catalog.text) as object\n"
1061 " FROM pg_catalog.pg_trigger t\n"
1062 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1063 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1064 gettext_noop("trigger"));
1066 if (!showSystem && !pattern)
1067 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1068 " AND n.nspname <> 'information_schema'\n");
1070 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1071 "n.nspname", "t.tgname", NULL,
1072 "pg_catalog.pg_table_is_visible(c.oid)");
1074 appendPQExpBufferStr(&buf,
1076 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1078 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1080 res = PSQLexec(buf.data);
1081 termPQExpBuffer(&buf);
1085 myopt.nullPrint = NULL;
1086 myopt.title = _("Object descriptions");
1087 myopt.translate_header = true;
1088 myopt.translate_columns = translate_columns;
1089 myopt.n_translate_columns = lengthof(translate_columns);
1091 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1099 * describeTableDetails (for \d)
1101 * This routine finds the tables to be displayed, and calls
1102 * describeOneTableDetails for each one.
1104 * verbose: if true, this is \d+
1107 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1109 PQExpBufferData buf;
1113 initPQExpBuffer(&buf);
1115 printfPQExpBuffer(&buf,
1119 "FROM pg_catalog.pg_class c\n"
1120 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1122 if (!showSystem && !pattern)
1123 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1124 " AND n.nspname <> 'information_schema'\n");
1126 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1127 "n.nspname", "c.relname", NULL,
1128 "pg_catalog.pg_table_is_visible(c.oid)");
1130 appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1132 res = PSQLexec(buf.data);
1133 termPQExpBuffer(&buf);
1137 if (PQntuples(res) == 0)
1140 psql_error("Did not find any relation named \"%s\".\n",
1146 for (i = 0; i < PQntuples(res); i++)
1149 const char *nspname;
1150 const char *relname;
1152 oid = PQgetvalue(res, i, 0);
1153 nspname = PQgetvalue(res, i, 1);
1154 relname = PQgetvalue(res, i, 2);
1156 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1173 * describeOneTableDetails (for \d)
1175 * Unfortunately, the information presented here is so complicated that it
1176 * cannot be done in a single query. So we have to assemble the printed table
1177 * by hand and pass it to the underlying printTable() function.
1180 describeOneTableDetails(const char *schemaname,
1181 const char *relationname,
1185 PQExpBufferData buf;
1186 PGresult *res = NULL;
1187 printTableOpt myopt = pset.popt.topt;
1188 printTableContent cont;
1189 bool printTableInitialized = false;
1191 char *view_def = NULL;
1193 char **seq_values = NULL;
1194 char **modifiers = NULL;
1196 PQExpBufferData title;
1197 PQExpBufferData tmpbuf;
1212 char relpersistence;
1215 bool show_modifiers = false;
1220 myopt.default_footer = false;
1221 /* This output looks confusing in expanded mode. */
1222 myopt.expanded = false;
1224 initPQExpBuffer(&buf);
1225 initPQExpBuffer(&title);
1226 initPQExpBuffer(&tmpbuf);
1228 /* Get general table info */
1229 if (pset.sversion >= 90500)
1231 printfPQExpBuffer(&buf,
1232 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1233 "c.relhastriggers, c.relrowsecurity, c.relhasoids, "
1234 "%s, c.reltablespace, "
1235 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1236 "c.relpersistence, c.relreplident\n"
1237 "FROM pg_catalog.pg_class c\n "
1238 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1239 "WHERE c.oid = '%s';",
1241 "pg_catalog.array_to_string(c.reloptions || "
1242 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1246 else if (pset.sversion >= 90400)
1248 printfPQExpBuffer(&buf,
1249 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1250 "c.relhastriggers, false, c.relhasoids, "
1251 "%s, c.reltablespace, "
1252 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1253 "c.relpersistence, c.relreplident\n"
1254 "FROM pg_catalog.pg_class c\n "
1255 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1256 "WHERE c.oid = '%s';",
1258 "pg_catalog.array_to_string(c.reloptions || "
1259 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1263 else if (pset.sversion >= 90100)
1265 printfPQExpBuffer(&buf,
1266 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1267 "c.relhastriggers, false, c.relhasoids, "
1268 "%s, c.reltablespace, "
1269 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1270 "c.relpersistence\n"
1271 "FROM pg_catalog.pg_class c\n "
1272 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1273 "WHERE c.oid = '%s';",
1275 "pg_catalog.array_to_string(c.reloptions || "
1276 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1280 else if (pset.sversion >= 90000)
1282 printfPQExpBuffer(&buf,
1283 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1284 "c.relhastriggers, false, c.relhasoids, "
1285 "%s, c.reltablespace, "
1286 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1287 "FROM pg_catalog.pg_class c\n "
1288 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1289 "WHERE c.oid = '%s';",
1291 "pg_catalog.array_to_string(c.reloptions || "
1292 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1296 else if (pset.sversion >= 80400)
1298 printfPQExpBuffer(&buf,
1299 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1300 "c.relhastriggers, false, c.relhasoids, "
1301 "%s, c.reltablespace\n"
1302 "FROM pg_catalog.pg_class c\n "
1303 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1304 "WHERE c.oid = '%s';",
1306 "pg_catalog.array_to_string(c.reloptions || "
1307 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1311 else if (pset.sversion >= 80200)
1313 printfPQExpBuffer(&buf,
1314 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1315 "reltriggers <> 0, false, relhasoids, "
1316 "%s, reltablespace\n"
1317 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1319 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1322 else if (pset.sversion >= 80000)
1324 printfPQExpBuffer(&buf,
1325 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1326 "reltriggers <> 0, false, relhasoids, "
1327 "'', reltablespace\n"
1328 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1333 printfPQExpBuffer(&buf,
1334 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1335 "reltriggers <> 0, false, relhasoids, "
1337 "FROM pg_catalog.pg_class WHERE oid = '%s';",
1341 res = PSQLexec(buf.data);
1345 /* Did we get anything? */
1346 if (PQntuples(res) == 0)
1349 psql_error("Did not find any relation with OID %s.\n", oid);
1353 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1354 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1355 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1356 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1357 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1358 tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1359 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1360 tableinfo.reloptions = (pset.sversion >= 80200) ?
1361 pg_strdup(PQgetvalue(res, 0, 7)) : NULL;
1362 tableinfo.tablespace = (pset.sversion >= 80000) ?
1363 atooid(PQgetvalue(res, 0, 8)) : 0;
1364 tableinfo.reloftype = (pset.sversion >= 90000 &&
1365 strcmp(PQgetvalue(res, 0, 9), "") != 0) ?
1366 pg_strdup(PQgetvalue(res, 0, 9)) : NULL;
1367 tableinfo.relpersistence = (pset.sversion >= 90100) ?
1368 *(PQgetvalue(res, 0, 10)) : 0;
1369 tableinfo.relreplident = (pset.sversion >= 90400) ?
1370 *(PQgetvalue(res, 0, 11)) : 'd';
1375 * If it's a sequence, fetch its values and store into an array that will
1378 if (tableinfo.relkind == 'S')
1380 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1381 /* must be separate because fmtId isn't reentrant */
1382 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1384 res = PSQLexec(buf.data);
1388 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1390 for (i = 0; i < PQnfields(res); i++)
1391 seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1392 seq_values[i] = NULL;
1401 * You need to modify value of "firstvcol" which will be defined below if
1402 * you are adding column(s) preceding to verbose-only columns.
1404 printfPQExpBuffer(&buf, "SELECT a.attname,");
1405 appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1406 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1407 "\n FROM pg_catalog.pg_attrdef d"
1408 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1409 "\n a.attnotnull, a.attnum,");
1410 if (pset.sversion >= 90100)
1411 appendPQExpBufferStr(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1412 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1414 appendPQExpBufferStr(&buf, "\n NULL AS attcollation");
1415 if (tableinfo.relkind == 'i')
1416 appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1418 appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
1419 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1420 appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1421 " '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1422 " pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1424 appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
1427 appendPQExpBufferStr(&buf, ",\n a.attstorage");
1428 appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1431 * In 9.0+, we have column comments for: relations, views, composite
1432 * types, and foreign tables (c.f. CommentObject() in comment.c).
1434 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1435 tableinfo.relkind == 'm' ||
1436 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1437 appendPQExpBufferStr(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1440 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1441 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1442 appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1444 res = PSQLexec(buf.data);
1447 numrows = PQntuples(res);
1450 switch (tableinfo.relkind)
1453 if (tableinfo.relpersistence == 'u')
1454 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1455 schemaname, relationname);
1457 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1458 schemaname, relationname);
1461 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1462 schemaname, relationname);
1465 if (tableinfo.relpersistence == 'u')
1466 printfPQExpBuffer(&title, _("Unlogged materialized view \"%s.%s\""),
1467 schemaname, relationname);
1469 printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
1470 schemaname, relationname);
1473 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1474 schemaname, relationname);
1477 if (tableinfo.relpersistence == 'u')
1478 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1479 schemaname, relationname);
1481 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1482 schemaname, relationname);
1485 /* not used as of 8.2, but keep it for backwards compatibility */
1486 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1487 schemaname, relationname);
1490 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1491 schemaname, relationname);
1494 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1495 schemaname, relationname);
1498 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1499 schemaname, relationname);
1502 /* untranslated unknown relkind */
1503 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1504 tableinfo.relkind, schemaname, relationname);
1508 /* Set the number of columns, and their names */
1509 headers[0] = gettext_noop("Column");
1510 headers[1] = gettext_noop("Type");
1513 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1514 tableinfo.relkind == 'm' ||
1515 tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
1517 show_modifiers = true;
1518 headers[cols++] = gettext_noop("Modifiers");
1519 modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers));
1522 if (tableinfo.relkind == 'S')
1523 headers[cols++] = gettext_noop("Value");
1525 if (tableinfo.relkind == 'i')
1526 headers[cols++] = gettext_noop("Definition");
1528 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1529 headers[cols++] = gettext_noop("FDW Options");
1533 headers[cols++] = gettext_noop("Storage");
1534 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1535 tableinfo.relkind == 'f')
1536 headers[cols++] = gettext_noop("Stats target");
1537 /* Column comments, if the relkind supports this feature. */
1538 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1539 tableinfo.relkind == 'm' ||
1540 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1541 headers[cols++] = gettext_noop("Description");
1544 printTableInit(&cont, &myopt, title.data, cols, numrows);
1545 printTableInitialized = true;
1547 for (i = 0; i < cols; i++)
1548 printTableAddHeader(&cont, headers[i], true, 'l');
1550 /* Check if table is a view or materialized view */
1551 if ((tableinfo.relkind == 'v' || tableinfo.relkind == 'm') && verbose)
1555 printfPQExpBuffer(&buf,
1556 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1558 result = PSQLexec(buf.data);
1562 if (PQntuples(result) > 0)
1563 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1568 /* Generate table cells to be printed */
1569 for (i = 0; i < numrows; i++)
1572 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1575 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1577 /* Modifiers: collate, not null, default */
1580 resetPQExpBuffer(&tmpbuf);
1582 if (!PQgetisnull(res, i, 5))
1585 appendPQExpBufferStr(&tmpbuf, " ");
1586 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1587 PQgetvalue(res, i, 5));
1590 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1593 appendPQExpBufferStr(&tmpbuf, " ");
1594 appendPQExpBufferStr(&tmpbuf, _("not null"));
1597 /* handle "default" here */
1598 /* (note: above we cut off the 'default' string at 128) */
1599 if (strlen(PQgetvalue(res, i, 2)) != 0)
1602 appendPQExpBufferStr(&tmpbuf, " ");
1603 /* translator: default values of column definitions */
1604 appendPQExpBuffer(&tmpbuf, _("default %s"),
1605 PQgetvalue(res, i, 2));
1608 modifiers[i] = pg_strdup(tmpbuf.data);
1609 printTableAddCell(&cont, modifiers[i], false, false);
1612 /* Value: for sequences only */
1613 if (tableinfo.relkind == 'S')
1614 printTableAddCell(&cont, seq_values[i], false, false);
1616 /* Expression for index column */
1617 if (tableinfo.relkind == 'i')
1618 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1620 /* FDW options for foreign table column, only for 9.2 or later */
1621 if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
1622 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1624 /* Storage and Description */
1628 char *storage = PQgetvalue(res, i, firstvcol);
1630 /* these strings are literal in our syntax, so not translated. */
1631 printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1632 (storage[0] == 'm' ? "main" :
1633 (storage[0] == 'x' ? "extended" :
1634 (storage[0] == 'e' ? "external" :
1638 /* Statistics target, if the relkind supports this feature */
1639 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1640 tableinfo.relkind == 'f')
1642 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1646 /* Column comments, if the relkind supports this feature. */
1647 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1648 tableinfo.relkind == 'm' ||
1649 tableinfo.relkind == 'c' || tableinfo.relkind == 'f')
1650 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1656 if (tableinfo.relkind == 'i')
1658 /* Footer information about an index */
1661 printfPQExpBuffer(&buf,
1662 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1663 if (pset.sversion >= 80200)
1664 appendPQExpBufferStr(&buf, "i.indisvalid,\n");
1666 appendPQExpBufferStr(&buf, "true AS indisvalid,\n");
1667 if (pset.sversion >= 90000)
1668 appendPQExpBufferStr(&buf,
1669 " (NOT i.indimmediate) AND "
1670 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1671 "WHERE conrelid = i.indrelid AND "
1672 "conindid = i.indexrelid AND "
1673 "contype IN ('p','u','x') AND "
1674 "condeferrable) AS condeferrable,\n"
1675 " (NOT i.indimmediate) AND "
1676 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1677 "WHERE conrelid = i.indrelid AND "
1678 "conindid = i.indexrelid AND "
1679 "contype IN ('p','u','x') AND "
1680 "condeferred) AS condeferred,\n");
1682 appendPQExpBufferStr(&buf,
1683 " false AS condeferrable, false AS condeferred,\n");
1685 if (pset.sversion >= 90400)
1686 appendPQExpBuffer(&buf, "i.indisreplident,\n");
1688 appendPQExpBuffer(&buf, "false AS indisreplident,\n");
1690 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
1691 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1692 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1693 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1694 "AND i.indrelid = c2.oid;",
1697 result = PSQLexec(buf.data);
1700 else if (PQntuples(result) != 1)
1707 char *indisunique = PQgetvalue(result, 0, 0);
1708 char *indisprimary = PQgetvalue(result, 0, 1);
1709 char *indisclustered = PQgetvalue(result, 0, 2);
1710 char *indisvalid = PQgetvalue(result, 0, 3);
1711 char *deferrable = PQgetvalue(result, 0, 4);
1712 char *deferred = PQgetvalue(result, 0, 5);
1713 char *indisreplident = PQgetvalue(result, 0, 6);
1714 char *indamname = PQgetvalue(result, 0, 7);
1715 char *indtable = PQgetvalue(result, 0, 8);
1716 char *indpred = PQgetvalue(result, 0, 9);
1718 if (strcmp(indisprimary, "t") == 0)
1719 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1720 else if (strcmp(indisunique, "t") == 0)
1721 printfPQExpBuffer(&tmpbuf, _("unique, "));
1723 resetPQExpBuffer(&tmpbuf);
1724 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1726 /* we assume here that index and table are in same schema */
1727 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1728 schemaname, indtable);
1730 if (strlen(indpred))
1731 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1733 if (strcmp(indisclustered, "t") == 0)
1734 appendPQExpBufferStr(&tmpbuf, _(", clustered"));
1736 if (strcmp(indisvalid, "t") != 0)
1737 appendPQExpBufferStr(&tmpbuf, _(", invalid"));
1739 if (strcmp(deferrable, "t") == 0)
1740 appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
1742 if (strcmp(deferred, "t") == 0)
1743 appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
1745 if (strcmp(indisreplident, "t") == 0)
1746 appendPQExpBuffer(&tmpbuf, _(", replica identity"));
1748 printTableAddFooter(&cont, tmpbuf.data);
1749 add_tablespace_footer(&cont, tableinfo.relkind,
1750 tableinfo.tablespace, true);
1755 else if (tableinfo.relkind == 'S')
1757 /* Footer information about a sequence */
1758 PGresult *result = NULL;
1760 /* Get the column that owns this sequence */
1761 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1762 "\n pg_catalog.quote_ident(relname) || '.' ||"
1763 "\n pg_catalog.quote_ident(attname)"
1764 "\nFROM pg_catalog.pg_class c"
1765 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1766 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1767 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1768 "\n a.attrelid=c.oid AND"
1769 "\n a.attnum=d.refobjsubid)"
1770 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1771 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1773 "\n AND d.deptype='a'",
1776 result = PSQLexec(buf.data);
1779 else if (PQntuples(result) == 1)
1781 printfPQExpBuffer(&buf, _("Owned by: %s"),
1782 PQgetvalue(result, 0, 0));
1783 printTableAddFooter(&cont, buf.data);
1787 * If we get no rows back, don't show anything (obviously). We should
1788 * never get more than one row back, but if we do, just ignore it and
1789 * don't print anything.
1793 else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
1794 tableinfo.relkind == 'f')
1796 /* Footer information about a table */
1797 PGresult *result = NULL;
1801 if (tableinfo.hasindex)
1803 printfPQExpBuffer(&buf,
1804 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1805 if (pset.sversion >= 80200)
1806 appendPQExpBufferStr(&buf, "i.indisvalid, ");
1808 appendPQExpBufferStr(&buf, "true as indisvalid, ");
1809 appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
1810 if (pset.sversion >= 90000)
1811 appendPQExpBufferStr(&buf,
1812 "pg_catalog.pg_get_constraintdef(con.oid, true), "
1813 "contype, condeferrable, condeferred");
1815 appendPQExpBufferStr(&buf,
1816 "null AS constraintdef, null AS contype, "
1817 "false AS condeferrable, false AS condeferred");
1818 if (pset.sversion >= 90400)
1819 appendPQExpBufferStr(&buf, ", i.indisreplident");
1821 appendPQExpBufferStr(&buf, ", false AS indisreplident");
1822 if (pset.sversion >= 80000)
1823 appendPQExpBufferStr(&buf, ", c2.reltablespace");
1824 appendPQExpBufferStr(&buf,
1825 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1826 if (pset.sversion >= 90000)
1827 appendPQExpBufferStr(&buf,
1828 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1829 appendPQExpBuffer(&buf,
1830 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1831 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
1833 result = PSQLexec(buf.data);
1837 tuples = PQntuples(result);
1841 printTableAddFooter(&cont, _("Indexes:"));
1842 for (i = 0; i < tuples; i++)
1844 /* untranslated index name */
1845 printfPQExpBuffer(&buf, " \"%s\"",
1846 PQgetvalue(result, i, 0));
1848 /* If exclusion constraint, print the constraintdef */
1849 if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1851 appendPQExpBuffer(&buf, " %s",
1852 PQgetvalue(result, i, 6));
1856 const char *indexdef;
1857 const char *usingpos;
1859 /* Label as primary key or unique (but not both) */
1860 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1861 appendPQExpBufferStr(&buf, " PRIMARY KEY,");
1862 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1864 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1865 appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
1867 appendPQExpBufferStr(&buf, " UNIQUE,");
1870 /* Everything after "USING" is echoed verbatim */
1871 indexdef = PQgetvalue(result, i, 5);
1872 usingpos = strstr(indexdef, " USING ");
1874 indexdef = usingpos + 7;
1875 appendPQExpBuffer(&buf, " %s", indexdef);
1877 /* Need these for deferrable PK/UNIQUE indexes */
1878 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1879 appendPQExpBufferStr(&buf, " DEFERRABLE");
1881 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1882 appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
1885 /* Add these for all cases */
1886 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1887 appendPQExpBufferStr(&buf, " CLUSTER");
1889 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1890 appendPQExpBufferStr(&buf, " INVALID");
1892 if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
1893 appendPQExpBuffer(&buf, " REPLICA IDENTITY");
1895 printTableAddFooter(&cont, buf.data);
1897 /* Print tablespace of the index on the same line */
1898 if (pset.sversion >= 80000)
1899 add_tablespace_footer(&cont, 'i',
1900 atooid(PQgetvalue(result, i, 11)),
1907 /* print table (and column) check constraints */
1908 if (tableinfo.checks)
1910 printfPQExpBuffer(&buf,
1911 "SELECT r.conname, "
1912 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1913 "FROM pg_catalog.pg_constraint r\n"
1914 "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
1917 result = PSQLexec(buf.data);
1921 tuples = PQntuples(result);
1925 printTableAddFooter(&cont, _("Check constraints:"));
1926 for (i = 0; i < tuples; i++)
1928 /* untranslated contraint name and def */
1929 printfPQExpBuffer(&buf, " \"%s\" %s",
1930 PQgetvalue(result, i, 0),
1931 PQgetvalue(result, i, 1));
1933 printTableAddFooter(&cont, buf.data);
1939 /* print foreign-key constraints (there are none if no triggers) */
1940 if (tableinfo.hastriggers)
1942 printfPQExpBuffer(&buf,
1944 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1945 "FROM pg_catalog.pg_constraint r\n"
1946 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
1948 result = PSQLexec(buf.data);
1952 tuples = PQntuples(result);
1956 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1957 for (i = 0; i < tuples; i++)
1959 /* untranslated constraint name and def */
1960 printfPQExpBuffer(&buf, " \"%s\" %s",
1961 PQgetvalue(result, i, 0),
1962 PQgetvalue(result, i, 1));
1964 printTableAddFooter(&cont, buf.data);
1970 /* print incoming foreign-key references (none if no triggers) */
1971 if (tableinfo.hastriggers)
1973 printfPQExpBuffer(&buf,
1974 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1975 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1976 "FROM pg_catalog.pg_constraint c\n"
1977 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
1979 result = PSQLexec(buf.data);
1983 tuples = PQntuples(result);
1987 printTableAddFooter(&cont, _("Referenced by:"));
1988 for (i = 0; i < tuples; i++)
1990 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
1991 PQgetvalue(result, i, 1),
1992 PQgetvalue(result, i, 0),
1993 PQgetvalue(result, i, 2));
1995 printTableAddFooter(&cont, buf.data);
2001 /* print any row-level policies */
2002 if (pset.sversion >= 90500)
2004 printfPQExpBuffer(&buf,
2005 "SELECT pol.polname,\n"
2006 "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE array_to_string(array(select rolname from pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2007 "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2008 "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2009 "CASE pol.polcmd \n"
2010 "WHEN 'r' THEN 'SELECT'\n"
2011 "WHEN 'u' THEN 'UPDATE'\n"
2012 "WHEN 'a' THEN 'INSERT'\n"
2013 "WHEN 'd' THEN 'DELETE'\n"
2015 "FROM pg_catalog.pg_policy pol\n"
2016 "WHERE pol.polrelid = '%s' ORDER BY 1;",
2019 result = PSQLexec(buf.data);
2023 tuples = PQntuples(result);
2026 * Handle cases where RLS is enabled and there are policies,
2027 * or there aren't policies, or RLS isn't enabled but there
2030 if (tableinfo.rowsecurity && tuples > 0)
2031 printTableAddFooter(&cont, _("Policies:"));
2033 if (tableinfo.rowsecurity && tuples == 0)
2034 printTableAddFooter(&cont, _("Policies (Row Security Enabled): (None)"));
2036 if (!tableinfo.rowsecurity && tuples > 0)
2037 printTableAddFooter(&cont, _("Policies (Row Security Disabled):"));
2039 /* Might be an empty set - that's ok */
2040 for (i = 0; i < tuples; i++)
2042 printfPQExpBuffer(&buf, " POLICY \"%s\"",
2043 PQgetvalue(result, i, 0));
2045 if (!PQgetisnull(result, i, 4))
2046 appendPQExpBuffer(&buf, " FOR %s",
2047 PQgetvalue(result, i, 4));
2049 if (!PQgetisnull(result, i, 1))
2051 appendPQExpBuffer(&buf, "\n TO %s",
2052 PQgetvalue(result, i, 1));
2055 if (!PQgetisnull(result, i, 2))
2056 appendPQExpBuffer(&buf, "\n USING %s",
2057 PQgetvalue(result, i, 2));
2059 if (!PQgetisnull(result, i, 3))
2060 appendPQExpBuffer(&buf, "\n WITH CHECK %s",
2061 PQgetvalue(result, i, 3));
2063 printTableAddFooter(&cont, buf.data);
2070 if (tableinfo.hasrules && tableinfo.relkind != 'm')
2072 if (pset.sversion >= 80300)
2074 printfPQExpBuffer(&buf,
2075 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2077 "FROM pg_catalog.pg_rewrite r\n"
2078 "WHERE r.ev_class = '%s' ORDER BY 1;",
2083 printfPQExpBuffer(&buf,
2084 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2085 "'O'::char AS ev_enabled\n"
2086 "FROM pg_catalog.pg_rewrite r\n"
2087 "WHERE r.ev_class = '%s' ORDER BY 1;",
2090 result = PSQLexec(buf.data);
2094 tuples = PQntuples(result);
2101 for (category = 0; category < 4; category++)
2103 have_heading = false;
2105 for (i = 0; i < tuples; i++)
2107 const char *ruledef;
2108 bool list_rule = false;
2113 if (*PQgetvalue(result, i, 2) == 'O')
2117 if (*PQgetvalue(result, i, 2) == 'D')
2121 if (*PQgetvalue(result, i, 2) == 'A')
2125 if (*PQgetvalue(result, i, 2) == 'R')
2137 printfPQExpBuffer(&buf, _("Rules:"));
2140 printfPQExpBuffer(&buf, _("Disabled rules:"));
2143 printfPQExpBuffer(&buf, _("Rules firing always:"));
2146 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
2149 printTableAddFooter(&cont, buf.data);
2150 have_heading = true;
2153 /* Everything after "CREATE RULE" is echoed verbatim */
2154 ruledef = PQgetvalue(result, i, 1);
2156 printfPQExpBuffer(&buf, " %s", ruledef);
2157 printTableAddFooter(&cont, buf.data);
2167 PGresult *result = NULL;
2169 /* Footer information about a view */
2170 printTableAddFooter(&cont, _("View definition:"));
2171 printTableAddFooter(&cont, view_def);
2174 if (tableinfo.hasrules)
2176 printfPQExpBuffer(&buf,
2177 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
2178 "FROM pg_catalog.pg_rewrite r\n"
2179 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
2181 result = PSQLexec(buf.data);
2185 if (PQntuples(result) > 0)
2187 printTableAddFooter(&cont, _("Rules:"));
2188 for (i = 0; i < PQntuples(result); i++)
2190 const char *ruledef;
2192 /* Everything after "CREATE RULE" is echoed verbatim */
2193 ruledef = PQgetvalue(result, i, 1);
2196 printfPQExpBuffer(&buf, " %s", ruledef);
2197 printTableAddFooter(&cont, buf.data);
2205 * Print triggers next, if any (but only user-defined triggers). This
2206 * could apply to either a table or a view.
2208 if (tableinfo.hastriggers)
2213 printfPQExpBuffer(&buf,
2215 "pg_catalog.pg_get_triggerdef(t.oid%s), "
2217 "FROM pg_catalog.pg_trigger t\n"
2218 "WHERE t.tgrelid = '%s' AND ",
2219 (pset.sversion >= 90000 ? ", true" : ""),
2220 (pset.sversion >= 90000 ? "t.tgisinternal" :
2221 pset.sversion >= 80300 ?
2222 "t.tgconstraint <> 0 AS tgisinternal" :
2223 "false AS tgisinternal"), oid);
2224 if (pset.sversion >= 90000)
2225 /* display/warn about disabled internal triggers */
2226 appendPQExpBuffer(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
2227 else if (pset.sversion >= 80300)
2228 appendPQExpBufferStr(&buf, "(t.tgconstraint = 0 OR (t.tgconstraint <> 0 AND t.tgenabled = 'D'))");
2230 appendPQExpBufferStr(&buf,
2231 "(NOT tgisconstraint "
2233 " (SELECT 1 FROM pg_catalog.pg_depend d "
2234 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2235 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2236 appendPQExpBufferStr(&buf, "\nORDER BY 1;");
2238 result = PSQLexec(buf.data);
2242 tuples = PQntuples(result);
2250 * split the output into 4 different categories. Enabled triggers,
2251 * disabled triggers and the two special ALWAYS and REPLICA
2254 for (category = 0; category <= 4; category++)
2256 have_heading = false;
2257 for (i = 0; i < tuples; i++)
2261 const char *usingpos;
2262 const char *tgenabled;
2263 const char *tgisinternal;
2266 * Check if this trigger falls into the current category
2268 tgenabled = PQgetvalue(result, i, 2);
2269 tgisinternal = PQgetvalue(result, i, 3);
2270 list_trigger = false;
2274 if (*tgenabled == 'O' || *tgenabled == 't')
2275 list_trigger = true;
2278 if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2279 *tgisinternal == 'f')
2280 list_trigger = true;
2283 if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2284 *tgisinternal == 't')
2285 list_trigger = true;
2288 if (*tgenabled == 'A')
2289 list_trigger = true;
2292 if (*tgenabled == 'R')
2293 list_trigger = true;
2296 if (list_trigger == false)
2299 /* Print the category heading once */
2300 if (have_heading == false)
2305 printfPQExpBuffer(&buf, _("Triggers:"));
2308 if (pset.sversion >= 80300)
2309 printfPQExpBuffer(&buf, _("Disabled user triggers:"));
2311 printfPQExpBuffer(&buf, _("Disabled triggers:"));
2314 printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
2317 printfPQExpBuffer(&buf, _("Triggers firing always:"));
2320 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2324 printTableAddFooter(&cont, buf.data);
2325 have_heading = true;
2328 /* Everything after "TRIGGER" is echoed verbatim */
2329 tgdef = PQgetvalue(result, i, 1);
2330 usingpos = strstr(tgdef, " TRIGGER ");
2332 tgdef = usingpos + 9;
2334 printfPQExpBuffer(&buf, " %s", tgdef);
2335 printTableAddFooter(&cont, buf.data);
2343 * Finish printing the footer information about a table.
2345 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'm' ||
2346 tableinfo.relkind == 'f')
2351 /* print foreign server name */
2352 if (tableinfo.relkind == 'f')
2356 /* Footer information about foreign table */
2357 printfPQExpBuffer(&buf,
2358 "SELECT s.srvname,\n"
2359 " array_to_string(ARRAY(SELECT "
2360 " quote_ident(option_name) || ' ' || "
2361 " quote_literal(option_value) FROM "
2362 " pg_options_to_table(ftoptions)), ', ') "
2363 "FROM pg_catalog.pg_foreign_table f,\n"
2364 " pg_catalog.pg_foreign_server s\n"
2365 "WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2367 result = PSQLexec(buf.data);
2370 else if (PQntuples(result) != 1)
2376 /* Print server name */
2377 printfPQExpBuffer(&buf, "Server: %s",
2378 PQgetvalue(result, 0, 0));
2379 printTableAddFooter(&cont, buf.data);
2381 /* Print per-table FDW options, if any */
2382 ftoptions = PQgetvalue(result, 0, 1);
2383 if (ftoptions && ftoptions[0] != '\0')
2385 printfPQExpBuffer(&buf, "FDW Options: (%s)", ftoptions);
2386 printTableAddFooter(&cont, buf.data);
2391 /* print inherited tables */
2392 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);
2394 result = PSQLexec(buf.data);
2399 const char *s = _("Inherits");
2400 int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2402 tuples = PQntuples(result);
2404 for (i = 0; i < tuples; i++)
2407 printfPQExpBuffer(&buf, "%s: %s",
2408 s, PQgetvalue(result, i, 0));
2410 printfPQExpBuffer(&buf, "%*s %s",
2411 sw, "", PQgetvalue(result, i, 0));
2413 appendPQExpBufferStr(&buf, ",");
2415 printTableAddFooter(&cont, buf.data);
2421 /* print child tables */
2422 if (pset.sversion >= 80300)
2423 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);
2425 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);
2427 result = PSQLexec(buf.data);
2431 tuples = PQntuples(result);
2435 /* print the number of child tables, if any */
2438 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2439 printTableAddFooter(&cont, buf.data);
2444 /* display the list of child tables */
2445 const char *ct = _("Child tables");
2446 int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2448 for (i = 0; i < tuples; i++)
2451 printfPQExpBuffer(&buf, "%s: %s",
2452 ct, PQgetvalue(result, i, 0));
2454 printfPQExpBuffer(&buf, "%*s %s",
2455 ctw, "", PQgetvalue(result, i, 0));
2457 appendPQExpBufferChar(&buf, ',');
2459 printTableAddFooter(&cont, buf.data);
2465 if (tableinfo.reloftype)
2467 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2468 printTableAddFooter(&cont, buf.data);
2471 if (verbose && (tableinfo.relkind == 'r' || tableinfo.relkind == 'm') &&
2474 * No need to display default values; we already display a REPLICA
2475 * IDENTITY marker on indexes.
2477 tableinfo.relreplident != 'i' &&
2478 ((strcmp(schemaname, "pg_catalog") != 0 && tableinfo.relreplident != 'd') ||
2479 (strcmp(schemaname, "pg_catalog") == 0 && tableinfo.relreplident != 'n')))
2481 const char *s = _("Replica Identity");
2483 printfPQExpBuffer(&buf, "%s: %s",
2485 tableinfo.relreplident == 'f' ? "FULL" :
2486 tableinfo.relreplident == 'n' ? "NOTHING" :
2489 printTableAddFooter(&cont, buf.data);
2492 /* OIDs, if verbose and not a materialized view */
2493 if (verbose && tableinfo.relkind != 'm' && tableinfo.hasoids)
2494 printTableAddFooter(&cont, _("Has OIDs: yes"));
2496 /* Tablespace info */
2497 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2501 /* reloptions, if verbose */
2503 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2505 const char *t = _("Options");
2507 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
2508 printTableAddFooter(&cont, buf.data);
2511 printTable(&cont, pset.queryFout, pset.logfile);
2512 printTableCleanup(&cont);
2519 if (printTableInitialized)
2520 printTableCleanup(&cont);
2521 termPQExpBuffer(&buf);
2522 termPQExpBuffer(&title);
2523 termPQExpBuffer(&tmpbuf);
2527 for (ptr = seq_values; *ptr; ptr++)
2534 for (ptr = modifiers; *ptr; ptr++)
2549 * Add a tablespace description to a footer. If 'newline' is true, it is added
2550 * in a new line; otherwise it's appended to the current value of the last
2554 add_tablespace_footer(printTableContent *const cont, char relkind,
2555 Oid tablespace, const bool newline)
2557 /* relkinds for which we support tablespaces */
2558 if (relkind == 'r' || relkind == 'm' || relkind == 'i')
2561 * We ignore the database default tablespace so that users not using
2562 * tablespaces don't need to know about them. This case also covers
2563 * pre-8.0 servers, for which tablespace will always be 0.
2565 if (tablespace != 0)
2567 PGresult *result = NULL;
2568 PQExpBufferData buf;
2570 initPQExpBuffer(&buf);
2571 printfPQExpBuffer(&buf,
2572 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2573 "WHERE oid = '%u';", tablespace);
2574 result = PSQLexec(buf.data);
2577 /* Should always be the case, but.... */
2578 if (PQntuples(result) > 0)
2582 /* Add the tablespace as a new footer */
2583 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2584 PQgetvalue(result, 0, 0));
2585 printTableAddFooter(cont, buf.data);
2589 /* Append the tablespace to the latest footer */
2590 printfPQExpBuffer(&buf, "%s", cont->footer->data);
2593 translator: before this string there's an index description like
2594 '"foo_pkey" PRIMARY KEY, btree (a)' */
2595 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2596 PQgetvalue(result, 0, 0));
2597 printTableSetFooter(cont, buf.data);
2601 termPQExpBuffer(&buf);
2609 * Describes roles. Any schema portion of the pattern is ignored.
2612 describeRoles(const char *pattern, bool verbose)
2614 PQExpBufferData buf;
2616 printTableContent cont;
2617 printTableOpt myopt = pset.popt.topt;
2622 const char align = 'l';
2625 myopt.default_footer = false;
2627 initPQExpBuffer(&buf);
2629 if (pset.sversion >= 80100)
2631 printfPQExpBuffer(&buf,
2632 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2633 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2634 " r.rolconnlimit, r.rolvaliduntil,\n"
2635 " ARRAY(SELECT b.rolname\n"
2636 " FROM pg_catalog.pg_auth_members m\n"
2637 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2638 " WHERE m.member = r.oid) as memberof");
2640 if (verbose && pset.sversion >= 80200)
2642 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2645 if (pset.sversion >= 90100)
2647 appendPQExpBufferStr(&buf, "\n, r.rolreplication");
2650 if (pset.sversion >= 90500)
2652 appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
2655 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2657 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2658 NULL, "r.rolname", NULL, NULL);
2662 printfPQExpBuffer(&buf,
2663 "SELECT u.usename AS rolname,\n"
2664 " u.usesuper AS rolsuper,\n"
2665 " true AS rolinherit, false AS rolcreaterole,\n"
2666 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2667 " -1 AS rolconnlimit,"
2668 " u.valuntil as rolvaliduntil,\n"
2669 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2670 "\nFROM pg_catalog.pg_user u\n");
2672 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2673 NULL, "u.usename", NULL, NULL);
2676 appendPQExpBufferStr(&buf, "ORDER BY 1;");
2678 res = PSQLexec(buf.data);
2682 nrows = PQntuples(res);
2683 attr = pg_malloc0((nrows + 1) * sizeof(*attr));
2685 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2687 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2688 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2689 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2691 if (verbose && pset.sversion >= 80200)
2692 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2694 for (i = 0; i < nrows; i++)
2696 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2698 resetPQExpBuffer(&buf);
2699 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2700 add_role_attribute(&buf, _("Superuser"));
2702 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2703 add_role_attribute(&buf, _("No inheritance"));
2705 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2706 add_role_attribute(&buf, _("Create role"));
2708 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2709 add_role_attribute(&buf, _("Create DB"));
2711 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2712 add_role_attribute(&buf, _("Cannot login"));
2714 if (pset.sversion >= 90100)
2715 if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
2716 add_role_attribute(&buf, _("Replication"));
2718 if (pset.sversion >= 90500)
2719 if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
2720 add_role_attribute(&buf, _("Bypass RLS"));
2722 conns = atoi(PQgetvalue(res, i, 6));
2726 appendPQExpBufferChar(&buf, '\n');
2729 appendPQExpBufferStr(&buf, _("No connections"));
2731 appendPQExpBuffer(&buf, ngettext("%d connection",
2737 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
2740 appendPQExpBufferStr(&buf, "\n");
2741 appendPQExpBufferStr(&buf, _("Password valid until "));
2742 appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
2745 attr[i] = pg_strdup(buf.data);
2747 printTableAddCell(&cont, attr[i], false, false);
2749 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2751 if (verbose && pset.sversion >= 80200)
2752 printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
2754 termPQExpBuffer(&buf);
2756 printTable(&cont, pset.queryFout, pset.logfile);
2757 printTableCleanup(&cont);
2759 for (i = 0; i < nrows; i++)
2768 add_role_attribute(PQExpBuffer buf, const char *const str)
2771 appendPQExpBufferStr(buf, ", ");
2773 appendPQExpBufferStr(buf, str);
2780 listDbRoleSettings(const char *pattern, const char *pattern2)
2782 PQExpBufferData buf;
2784 printQueryOpt myopt = pset.popt;
2786 initPQExpBuffer(&buf);
2788 if (pset.sversion >= 90000)
2792 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
2793 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2794 "FROM pg_db_role_setting AS s\n"
2795 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2796 "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2797 gettext_noop("Role"),
2798 gettext_noop("Database"),
2799 gettext_noop("Settings"));
2800 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2801 NULL, "pg_roles.rolname", NULL, NULL);
2802 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2803 NULL, "pg_database.datname", NULL, NULL);
2804 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
2808 fprintf(pset.queryFout,
2809 _("No per-database role settings support in this server version.\n"));
2813 res = PSQLexec(buf.data);
2817 if (PQntuples(res) == 0 && !pset.quiet)
2820 fprintf(pset.queryFout, _("No matching settings found.\n"));
2822 fprintf(pset.queryFout, _("No settings found.\n"));
2826 myopt.nullPrint = NULL;
2827 myopt.title = _("List of settings");
2828 myopt.translate_header = true;
2830 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2834 resetPQExpBuffer(&buf);
2842 * handler for \dt, \di, etc.
2844 * tabtypes is an array of characters, specifying what info is desired:
2848 * m - materialized views
2850 * E - foreign table (Note: different from 'f', the relkind value)
2851 * (any order of the above is fine)
2852 * If tabtypes is empty, we default to \dtvsE.
2855 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2857 bool showTables = strchr(tabtypes, 't') != NULL;
2858 bool showIndexes = strchr(tabtypes, 'i') != NULL;
2859 bool showViews = strchr(tabtypes, 'v') != NULL;
2860 bool showMatViews = strchr(tabtypes, 'm') != NULL;
2861 bool showSeq = strchr(tabtypes, 's') != NULL;
2862 bool showForeign = strchr(tabtypes, 'E') != NULL;
2864 PQExpBufferData buf;
2866 printQueryOpt myopt = pset.popt;
2867 static const bool translate_columns[] = {false, false, true, false, false, false, false};
2869 if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
2870 showTables = showViews = showMatViews = showSeq = showForeign = true;
2872 initPQExpBuffer(&buf);
2875 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2876 * for backwards compatibility.
2878 printfPQExpBuffer(&buf,
2879 "SELECT n.nspname as \"%s\",\n"
2880 " c.relname as \"%s\",\n"
2882 " WHEN 'r' THEN '%s'"
2883 " WHEN 'v' THEN '%s'"
2884 " WHEN 'm' THEN '%s'"
2885 " WHEN 'i' THEN '%s'"
2886 " WHEN 'S' THEN '%s'"
2887 " WHEN 's' THEN '%s'"
2888 " WHEN 'f' THEN '%s'"
2890 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2891 gettext_noop("Schema"),
2892 gettext_noop("Name"),
2893 gettext_noop("table"),
2894 gettext_noop("view"),
2895 gettext_noop("materialized view"),
2896 gettext_noop("index"),
2897 gettext_noop("sequence"),
2898 gettext_noop("special"),
2899 gettext_noop("foreign table"),
2900 gettext_noop("Type"),
2901 gettext_noop("Owner"));
2904 appendPQExpBuffer(&buf,
2905 ",\n c2.relname as \"%s\"",
2906 gettext_noop("Table"));
2911 * As of PostgreSQL 9.0, use pg_table_size() to show a more acurate
2912 * size of a table, including FSM, VM and TOAST tables.
2914 if (pset.sversion >= 90000)
2915 appendPQExpBuffer(&buf,
2916 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
2917 gettext_noop("Size"));
2918 else if (pset.sversion >= 80100)
2919 appendPQExpBuffer(&buf,
2920 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2921 gettext_noop("Size"));
2923 appendPQExpBuffer(&buf,
2924 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2925 gettext_noop("Description"));
2928 appendPQExpBufferStr(&buf,
2929 "\nFROM pg_catalog.pg_class c"
2930 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2932 appendPQExpBufferStr(&buf,
2933 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2934 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2936 appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
2938 appendPQExpBufferStr(&buf, "'r',");
2940 appendPQExpBufferStr(&buf, "'v',");
2942 appendPQExpBufferStr(&buf, "'m',");
2944 appendPQExpBufferStr(&buf, "'i',");
2946 appendPQExpBufferStr(&buf, "'S',");
2947 if (showSystem || pattern)
2948 appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2951 appendPQExpBufferStr(&buf, "'f',");
2953 appendPQExpBufferStr(&buf, "''"); /* dummy */
2954 appendPQExpBufferStr(&buf, ")\n");
2956 if (!showSystem && !pattern)
2957 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
2958 " AND n.nspname <> 'information_schema'\n");
2961 * TOAST objects are suppressed unconditionally. Since we don't provide
2962 * any way to select relkind 't' above, we would never show toast tables
2963 * in any case; it seems a bit confusing to allow their indexes to be
2964 * shown. Use plain \d if you really need to look at a TOAST table/index.
2966 appendPQExpBufferStr(&buf, " AND n.nspname !~ '^pg_toast'\n");
2968 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2969 "n.nspname", "c.relname", NULL,
2970 "pg_catalog.pg_table_is_visible(c.oid)");
2972 appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
2974 res = PSQLexec(buf.data);
2975 termPQExpBuffer(&buf);
2979 if (PQntuples(res) == 0 && !pset.quiet)
2982 fprintf(pset.queryFout, _("No matching relations found.\n"));
2984 fprintf(pset.queryFout, _("No relations found.\n"));
2988 myopt.nullPrint = NULL;
2989 myopt.title = _("List of relations");
2990 myopt.translate_header = true;
2991 myopt.translate_columns = translate_columns;
2992 myopt.n_translate_columns = lengthof(translate_columns);
2994 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3005 * Describes languages.
3008 listLanguages(const char *pattern, bool verbose, bool showSystem)
3010 PQExpBufferData buf;
3012 printQueryOpt myopt = pset.popt;
3014 initPQExpBuffer(&buf);
3016 printfPQExpBuffer(&buf,
3017 "SELECT l.lanname AS \"%s\",\n",
3018 gettext_noop("Name"));
3019 if (pset.sversion >= 80300)
3020 appendPQExpBuffer(&buf,
3021 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
3022 gettext_noop("Owner"));
3024 appendPQExpBuffer(&buf,
3025 " l.lanpltrusted AS \"%s\"",
3026 gettext_noop("Trusted"));
3030 appendPQExpBuffer(&buf,
3031 ",\n NOT l.lanispl AS \"%s\",\n"
3032 " l.lanplcallfoid::regprocedure AS \"%s\",\n"
3033 " l.lanvalidator::regprocedure AS \"%s\",\n ",
3034 gettext_noop("Internal Language"),
3035 gettext_noop("Call Handler"),
3036 gettext_noop("Validator"));
3037 if (pset.sversion >= 90000)
3038 appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
3039 gettext_noop("Inline Handler"));
3040 printACLColumn(&buf, "l.lanacl");
3043 appendPQExpBuffer(&buf,
3044 ",\n d.description AS \"%s\""
3045 "\nFROM pg_catalog.pg_language l\n"
3046 "LEFT JOIN pg_catalog.pg_description d\n"
3047 " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
3048 " AND d.objsubid = 0\n",
3049 gettext_noop("Description"));
3052 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3053 NULL, "l.lanname", NULL, NULL);
3055 if (!showSystem && !pattern)
3056 appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
3059 appendPQExpBufferStr(&buf, "ORDER BY 1;");
3061 res = PSQLexec(buf.data);
3062 termPQExpBuffer(&buf);
3066 myopt.nullPrint = NULL;
3067 myopt.title = _("List of languages");
3068 myopt.translate_header = true;
3070 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3080 * Describes domains.
3083 listDomains(const char *pattern, bool verbose, bool showSystem)
3085 PQExpBufferData buf;
3087 printQueryOpt myopt = pset.popt;
3089 initPQExpBuffer(&buf);
3091 printfPQExpBuffer(&buf,
3092 "SELECT n.nspname as \"%s\",\n"
3093 " t.typname as \"%s\",\n"
3094 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
3096 gettext_noop("Schema"),
3097 gettext_noop("Name"),
3098 gettext_noop("Type"));
3100 if (pset.sversion >= 90100)
3101 appendPQExpBufferStr(&buf,
3102 " COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3103 " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
3104 appendPQExpBuffer(&buf,
3105 " CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
3106 " CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
3108 " pg_catalog.array_to_string(ARRAY(\n"
3109 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3110 " ), ' ') as \"%s\"",
3111 gettext_noop("Modifier"),
3112 gettext_noop("Check"));
3116 if (pset.sversion >= 90200)
3118 appendPQExpBufferStr(&buf, ",\n ");
3119 printACLColumn(&buf, "t.typacl");
3121 appendPQExpBuffer(&buf,
3122 ",\n d.description as \"%s\"",
3123 gettext_noop("Description"));
3126 appendPQExpBufferStr(&buf,
3127 "\nFROM pg_catalog.pg_type t\n"
3128 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3131 appendPQExpBufferStr(&buf,
3132 " LEFT JOIN pg_catalog.pg_description d "
3133 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3134 "AND d.objsubid = 0\n");
3136 appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3138 if (!showSystem && !pattern)
3139 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3140 " AND n.nspname <> 'information_schema'\n");
3142 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3143 "n.nspname", "t.typname", NULL,
3144 "pg_catalog.pg_type_is_visible(t.oid)");
3146 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3148 res = PSQLexec(buf.data);
3149 termPQExpBuffer(&buf);
3153 myopt.nullPrint = NULL;
3154 myopt.title = _("List of domains");
3155 myopt.translate_header = true;
3157 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3166 * Describes conversions.
3169 listConversions(const char *pattern, bool verbose, bool showSystem)
3171 PQExpBufferData buf;
3173 printQueryOpt myopt = pset.popt;
3174 static const bool translate_columns[] =
3175 {false, false, false, false, true, false};
3177 initPQExpBuffer(&buf);
3179 printfPQExpBuffer(&buf,
3180 "SELECT n.nspname AS \"%s\",\n"
3181 " c.conname AS \"%s\",\n"
3182 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3183 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3184 " CASE WHEN c.condefault THEN '%s'\n"
3185 " ELSE '%s' END AS \"%s\"",
3186 gettext_noop("Schema"),
3187 gettext_noop("Name"),
3188 gettext_noop("Source"),
3189 gettext_noop("Destination"),
3190 gettext_noop("yes"), gettext_noop("no"),
3191 gettext_noop("Default?"));
3194 appendPQExpBuffer(&buf,
3195 ",\n d.description AS \"%s\"",
3196 gettext_noop("Description"));
3198 appendPQExpBufferStr(&buf,
3199 "\nFROM pg_catalog.pg_conversion c\n"
3200 " JOIN pg_catalog.pg_namespace n "
3201 "ON n.oid = c.connamespace\n");
3204 appendPQExpBufferStr(&buf,
3205 "LEFT JOIN pg_catalog.pg_description d "
3206 "ON d.classoid = c.tableoid\n"
3207 " AND d.objoid = c.oid "
3208 "AND d.objsubid = 0\n");
3210 appendPQExpBufferStr(&buf, "WHERE true\n");
3212 if (!showSystem && !pattern)
3213 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3214 " AND n.nspname <> 'information_schema'\n");
3216 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3217 "n.nspname", "c.conname", NULL,
3218 "pg_catalog.pg_conversion_is_visible(c.oid)");
3220 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3222 res = PSQLexec(buf.data);
3223 termPQExpBuffer(&buf);
3227 myopt.nullPrint = NULL;
3228 myopt.title = _("List of conversions");
3229 myopt.translate_header = true;
3230 myopt.translate_columns = translate_columns;
3231 myopt.n_translate_columns = lengthof(translate_columns);
3233 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3242 * Describes Event Triggers.
3245 listEventTriggers(const char *pattern, bool verbose)
3247 PQExpBufferData buf;
3249 printQueryOpt myopt = pset.popt;
3250 static const bool translate_columns[] =
3251 {false, false, false, true, false, false, false};
3253 initPQExpBuffer(&buf);
3255 printfPQExpBuffer(&buf,
3256 "SELECT evtname as \"%s\", "
3257 "evtevent as \"%s\", "
3258 "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3259 " case evtenabled when 'O' then '%s'"
3260 " when 'R' then '%s'"
3261 " when 'A' then '%s'"
3262 " when 'D' then '%s' end as \"%s\",\n"
3263 " e.evtfoid::pg_catalog.regproc as \"%s\", "
3264 "pg_catalog.array_to_string(array(select x"
3265 " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3266 gettext_noop("Name"),
3267 gettext_noop("Event"),
3268 gettext_noop("Owner"),
3269 gettext_noop("enabled"),
3270 gettext_noop("replica"),
3271 gettext_noop("always"),
3272 gettext_noop("disabled"),
3273 gettext_noop("Enabled"),
3274 gettext_noop("Procedure"),
3275 gettext_noop("Tags"));
3277 appendPQExpBuffer(&buf,
3278 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3279 gettext_noop("Description"));
3280 appendPQExpBufferStr(&buf,
3281 "\nFROM pg_catalog.pg_event_trigger e ");
3283 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3284 NULL, "evtname", NULL, NULL);
3286 appendPQExpBufferStr(&buf, "ORDER BY 1");
3288 res = PSQLexec(buf.data);
3289 termPQExpBuffer(&buf);
3293 myopt.nullPrint = NULL;
3294 myopt.title = _("List of event triggers");
3295 myopt.translate_header = true;
3296 myopt.translate_columns = translate_columns;
3297 myopt.n_translate_columns = lengthof(translate_columns);
3299 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3311 listCasts(const char *pattern, bool verbose)
3313 PQExpBufferData buf;
3315 printQueryOpt myopt = pset.popt;
3316 static const bool translate_columns[] = {false, false, false, true, false};
3318 initPQExpBuffer(&buf);
3321 * We need a left join to pg_proc for binary casts; the others are just
3322 * paranoia. Also note that we don't attempt to localize '(binary
3323 * coercible)', because there's too much risk of gettext translating a
3324 * function name that happens to match some string in the PO database.
3326 printfPQExpBuffer(&buf,
3327 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3328 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3329 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3332 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3333 " WHEN c.castcontext = 'a' THEN '%s'\n"
3336 gettext_noop("Source type"),
3337 gettext_noop("Target type"),
3338 gettext_noop("Function"),
3340 gettext_noop("in assignment"),
3341 gettext_noop("yes"),
3342 gettext_noop("Implicit?"));
3345 appendPQExpBuffer(&buf,
3346 ",\n d.description AS \"%s\"\n",
3347 gettext_noop("Description"));
3349 appendPQExpBufferStr(&buf,
3350 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3351 " ON c.castfunc = p.oid\n"
3352 " LEFT JOIN pg_catalog.pg_type ts\n"
3353 " ON c.castsource = ts.oid\n"
3354 " LEFT JOIN pg_catalog.pg_namespace ns\n"
3355 " ON ns.oid = ts.typnamespace\n"
3356 " LEFT JOIN pg_catalog.pg_type tt\n"
3357 " ON c.casttarget = tt.oid\n"
3358 " LEFT JOIN pg_catalog.pg_namespace nt\n"
3359 " ON nt.oid = tt.typnamespace\n");
3362 appendPQExpBufferStr(&buf,
3363 " LEFT JOIN pg_catalog.pg_description d\n"
3364 " ON d.classoid = c.tableoid AND d.objoid = "
3365 "c.oid AND d.objsubid = 0\n");
3367 appendPQExpBufferStr(&buf, "WHERE ( (true");
3370 * Match name pattern against either internal or external name of either
3371 * castsource or casttarget
3373 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3374 "ns.nspname", "ts.typname",
3375 "pg_catalog.format_type(ts.oid, NULL)",
3376 "pg_catalog.pg_type_is_visible(ts.oid)");
3378 appendPQExpBufferStr(&buf, ") OR (true");
3380 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3381 "nt.nspname", "tt.typname",
3382 "pg_catalog.format_type(tt.oid, NULL)",
3383 "pg_catalog.pg_type_is_visible(tt.oid)");
3385 appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3387 res = PSQLexec(buf.data);
3388 termPQExpBuffer(&buf);
3392 myopt.nullPrint = NULL;
3393 myopt.title = _("List of casts");
3394 myopt.translate_header = true;
3395 myopt.translate_columns = translate_columns;
3396 myopt.n_translate_columns = lengthof(translate_columns);
3398 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3407 * Describes collations.
3410 listCollations(const char *pattern, bool verbose, bool showSystem)
3412 PQExpBufferData buf;
3414 printQueryOpt myopt = pset.popt;
3415 static const bool translate_columns[] = {false, false, false, false, false};
3417 if (pset.sversion < 90100)
3419 psql_error("The server (version %d.%d) does not support collations.\n",
3420 pset.sversion / 10000, (pset.sversion / 100) % 100);
3424 initPQExpBuffer(&buf);
3426 printfPQExpBuffer(&buf,
3427 "SELECT n.nspname AS \"%s\",\n"
3428 " c.collname AS \"%s\",\n"
3429 " c.collcollate AS \"%s\",\n"
3430 " c.collctype AS \"%s\"",
3431 gettext_noop("Schema"),
3432 gettext_noop("Name"),
3433 gettext_noop("Collate"),
3434 gettext_noop("Ctype"));
3437 appendPQExpBuffer(&buf,
3438 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3439 gettext_noop("Description"));
3441 appendPQExpBufferStr(&buf,
3442 "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3443 "WHERE n.oid = c.collnamespace\n");
3445 if (!showSystem && !pattern)
3446 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3447 " AND n.nspname <> 'information_schema'\n");
3450 * Hide collations that aren't usable in the current database's encoding.
3451 * If you think to change this, note that pg_collation_is_visible rejects
3452 * unusable collations, so you will need to hack name pattern processing
3453 * somehow to avoid inconsistent behavior.
3455 appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3457 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3458 "n.nspname", "c.collname", NULL,
3459 "pg_catalog.pg_collation_is_visible(c.oid)");
3461 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3463 res = PSQLexec(buf.data);
3464 termPQExpBuffer(&buf);
3468 myopt.nullPrint = NULL;
3469 myopt.title = _("List of collations");
3470 myopt.translate_header = true;
3471 myopt.translate_columns = translate_columns;
3472 myopt.n_translate_columns = lengthof(translate_columns);
3474 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3483 * Describes schemas (namespaces)
3486 listSchemas(const char *pattern, bool verbose, bool showSystem)
3488 PQExpBufferData buf;
3490 printQueryOpt myopt = pset.popt;
3492 initPQExpBuffer(&buf);
3493 printfPQExpBuffer(&buf,
3494 "SELECT n.nspname AS \"%s\",\n"
3495 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
3496 gettext_noop("Name"),
3497 gettext_noop("Owner"));
3501 appendPQExpBufferStr(&buf, ",\n ");
3502 printACLColumn(&buf, "n.nspacl");
3503 appendPQExpBuffer(&buf,
3504 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
3505 gettext_noop("Description"));
3508 appendPQExpBuffer(&buf,
3509 "\nFROM pg_catalog.pg_namespace n\n");
3511 if (!showSystem && !pattern)
3512 appendPQExpBufferStr(&buf,
3513 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
3515 processSQLNamePattern(pset.db, &buf, pattern,
3516 !showSystem && !pattern, false,
3517 NULL, "n.nspname", NULL,
3520 appendPQExpBufferStr(&buf, "ORDER BY 1;");
3522 res = PSQLexec(buf.data);
3523 termPQExpBuffer(&buf);
3527 myopt.nullPrint = NULL;
3528 myopt.title = _("List of schemas");
3529 myopt.translate_header = true;
3531 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3540 * list text search parsers
3543 listTSParsers(const char *pattern, bool verbose)
3545 PQExpBufferData buf;
3547 printQueryOpt myopt = pset.popt;
3549 if (pset.sversion < 80300)
3551 psql_error("The server (version %d.%d) does not support full text search.\n",
3552 pset.sversion / 10000, (pset.sversion / 100) % 100);
3557 return listTSParsersVerbose(pattern);
3559 initPQExpBuffer(&buf);
3561 printfPQExpBuffer(&buf,
3563 " n.nspname as \"%s\",\n"
3564 " p.prsname as \"%s\",\n"
3565 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
3566 "FROM pg_catalog.pg_ts_parser p \n"
3567 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
3568 gettext_noop("Schema"),
3569 gettext_noop("Name"),
3570 gettext_noop("Description")
3573 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3574 "n.nspname", "p.prsname", NULL,
3575 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3577 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3579 res = PSQLexec(buf.data);
3580 termPQExpBuffer(&buf);
3584 myopt.nullPrint = NULL;
3585 myopt.title = _("List of text search parsers");
3586 myopt.translate_header = true;
3588 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3595 * full description of parsers
3598 listTSParsersVerbose(const char *pattern)
3600 PQExpBufferData buf;
3604 initPQExpBuffer(&buf);
3606 printfPQExpBuffer(&buf,
3610 "FROM pg_catalog.pg_ts_parser p\n"
3611 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
3614 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3615 "n.nspname", "p.prsname", NULL,
3616 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
3618 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3620 res = PSQLexec(buf.data);
3621 termPQExpBuffer(&buf);
3625 if (PQntuples(res) == 0)
3628 psql_error("Did not find any text search parser named \"%s\".\n",
3634 for (i = 0; i < PQntuples(res); i++)
3637 const char *nspname = NULL;
3638 const char *prsname;
3640 oid = PQgetvalue(res, i, 0);
3641 if (!PQgetisnull(res, i, 1))
3642 nspname = PQgetvalue(res, i, 1);
3643 prsname = PQgetvalue(res, i, 2);
3645 if (!describeOneTSParser(oid, nspname, prsname))
3663 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3665 PQExpBufferData buf;
3668 printQueryOpt myopt = pset.popt;
3669 static const bool translate_columns[] = {true, false, false};
3671 initPQExpBuffer(&buf);
3673 printfPQExpBuffer(&buf,
3674 "SELECT '%s' AS \"%s\", \n"
3675 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3676 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3677 " FROM pg_catalog.pg_ts_parser p \n"
3678 " WHERE p.oid = '%s' \n"
3681 " p.prstoken::pg_catalog.regproc, \n"
3682 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3683 " FROM pg_catalog.pg_ts_parser p \n"
3684 " WHERE p.oid = '%s' \n"
3687 " p.prsend::pg_catalog.regproc, \n"
3688 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3689 " FROM pg_catalog.pg_ts_parser p \n"
3690 " WHERE p.oid = '%s' \n"
3693 " p.prsheadline::pg_catalog.regproc, \n"
3694 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3695 " FROM pg_catalog.pg_ts_parser p \n"
3696 " WHERE p.oid = '%s' \n"
3699 " p.prslextype::pg_catalog.regproc, \n"
3700 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3701 " FROM pg_catalog.pg_ts_parser p \n"
3702 " WHERE p.oid = '%s';",
3703 gettext_noop("Start parse"),
3704 gettext_noop("Method"),
3705 gettext_noop("Function"),
3706 gettext_noop("Description"),
3708 gettext_noop("Get next token"),
3710 gettext_noop("End parse"),
3712 gettext_noop("Get headline"),
3714 gettext_noop("Get token types"),
3717 res = PSQLexec(buf.data);
3718 termPQExpBuffer(&buf);
3722 myopt.nullPrint = NULL;
3724 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3726 sprintf(title, _("Text search parser \"%s\""), prsname);
3727 myopt.title = title;
3728 myopt.footers = NULL;
3729 myopt.topt.default_footer = false;
3730 myopt.translate_header = true;
3731 myopt.translate_columns = translate_columns;
3732 myopt.n_translate_columns = lengthof(translate_columns);
3734 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3738 initPQExpBuffer(&buf);
3740 printfPQExpBuffer(&buf,
3741 "SELECT t.alias as \"%s\", \n"
3742 " t.description as \"%s\" \n"
3743 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3745 gettext_noop("Token name"),
3746 gettext_noop("Description"),
3749 res = PSQLexec(buf.data);
3750 termPQExpBuffer(&buf);
3754 myopt.nullPrint = NULL;
3756 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3758 sprintf(title, _("Token types for parser \"%s\""), prsname);
3759 myopt.title = title;
3760 myopt.footers = NULL;
3761 myopt.topt.default_footer = true;
3762 myopt.translate_header = true;
3763 myopt.translate_columns = NULL;
3764 myopt.n_translate_columns = 0;
3766 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3775 * list text search dictionaries
3778 listTSDictionaries(const char *pattern, bool verbose)
3780 PQExpBufferData buf;
3782 printQueryOpt myopt = pset.popt;
3784 if (pset.sversion < 80300)
3786 psql_error("The server (version %d.%d) does not support full text search.\n",
3787 pset.sversion / 10000, (pset.sversion / 100) % 100);
3791 initPQExpBuffer(&buf);
3793 printfPQExpBuffer(&buf,
3795 " n.nspname as \"%s\",\n"
3796 " d.dictname as \"%s\",\n",
3797 gettext_noop("Schema"),
3798 gettext_noop("Name"));
3802 appendPQExpBuffer(&buf,
3803 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3804 " pg_catalog.pg_ts_template t \n"
3805 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3806 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
3807 " d.dictinitoption as \"%s\", \n",
3808 gettext_noop("Template"),
3809 gettext_noop("Init options"));
3812 appendPQExpBuffer(&buf,
3813 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3814 gettext_noop("Description"));
3816 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3817 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3819 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3820 "n.nspname", "d.dictname", NULL,
3821 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3823 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3825 res = PSQLexec(buf.data);
3826 termPQExpBuffer(&buf);
3830 myopt.nullPrint = NULL;
3831 myopt.title = _("List of text search dictionaries");
3832 myopt.translate_header = true;
3834 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3843 * list text search templates
3846 listTSTemplates(const char *pattern, bool verbose)
3848 PQExpBufferData buf;
3850 printQueryOpt myopt = pset.popt;
3852 if (pset.sversion < 80300)
3854 psql_error("The server (version %d.%d) does not support full text search.\n",
3855 pset.sversion / 10000, (pset.sversion / 100) % 100);
3859 initPQExpBuffer(&buf);
3862 printfPQExpBuffer(&buf,
3864 " n.nspname AS \"%s\",\n"
3865 " t.tmplname AS \"%s\",\n"
3866 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3867 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3868 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3869 gettext_noop("Schema"),
3870 gettext_noop("Name"),
3871 gettext_noop("Init"),
3872 gettext_noop("Lexize"),
3873 gettext_noop("Description"));
3875 printfPQExpBuffer(&buf,
3877 " n.nspname AS \"%s\",\n"
3878 " t.tmplname AS \"%s\",\n"
3879 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3880 gettext_noop("Schema"),
3881 gettext_noop("Name"),
3882 gettext_noop("Description"));
3884 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
3885 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3887 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3888 "n.nspname", "t.tmplname", NULL,
3889 "pg_catalog.pg_ts_template_is_visible(t.oid)");
3891 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3893 res = PSQLexec(buf.data);
3894 termPQExpBuffer(&buf);
3898 myopt.nullPrint = NULL;
3899 myopt.title = _("List of text search templates");
3900 myopt.translate_header = true;
3902 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3911 * list text search configurations
3914 listTSConfigs(const char *pattern, bool verbose)
3916 PQExpBufferData buf;
3918 printQueryOpt myopt = pset.popt;
3920 if (pset.sversion < 80300)
3922 psql_error("The server (version %d.%d) does not support full text search.\n",
3923 pset.sversion / 10000, (pset.sversion / 100) % 100);
3928 return listTSConfigsVerbose(pattern);
3930 initPQExpBuffer(&buf);
3932 printfPQExpBuffer(&buf,
3934 " n.nspname as \"%s\",\n"
3935 " c.cfgname as \"%s\",\n"
3936 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3937 "FROM pg_catalog.pg_ts_config c\n"
3938 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3939 gettext_noop("Schema"),
3940 gettext_noop("Name"),
3941 gettext_noop("Description")
3944 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3945 "n.nspname", "c.cfgname", NULL,
3946 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3948 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3950 res = PSQLexec(buf.data);
3951 termPQExpBuffer(&buf);
3955 myopt.nullPrint = NULL;
3956 myopt.title = _("List of text search configurations");
3957 myopt.translate_header = true;
3959 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3966 listTSConfigsVerbose(const char *pattern)
3968 PQExpBufferData buf;
3972 initPQExpBuffer(&buf);
3974 printfPQExpBuffer(&buf,
3975 "SELECT c.oid, c.cfgname,\n"
3978 " np.nspname as pnspname \n"
3979 "FROM pg_catalog.pg_ts_config c \n"
3980 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3981 " pg_catalog.pg_ts_parser p \n"
3982 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3983 "WHERE p.oid = c.cfgparser\n"
3986 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3987 "n.nspname", "c.cfgname", NULL,
3988 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3990 appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
3992 res = PSQLexec(buf.data);
3993 termPQExpBuffer(&buf);
3997 if (PQntuples(res) == 0)
4000 psql_error("Did not find any text search configuration named \"%s\".\n",
4006 for (i = 0; i < PQntuples(res); i++)
4009 const char *cfgname;
4010 const char *nspname = NULL;
4011 const char *prsname;
4012 const char *pnspname = NULL;
4014 oid = PQgetvalue(res, i, 0);
4015 cfgname = PQgetvalue(res, i, 1);
4016 if (!PQgetisnull(res, i, 2))
4017 nspname = PQgetvalue(res, i, 2);
4018 prsname = PQgetvalue(res, i, 3);
4019 if (!PQgetisnull(res, i, 4))
4020 pnspname = PQgetvalue(res, i, 4);
4022 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
4040 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
4041 const char *pnspname, const char *prsname)
4043 PQExpBufferData buf,
4046 printQueryOpt myopt = pset.popt;
4048 initPQExpBuffer(&buf);
4050 printfPQExpBuffer(&buf,
4052 " ( SELECT t.alias FROM \n"
4053 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
4054 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
4055 " pg_catalog.btrim( \n"
4056 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
4057 " FROM pg_catalog.pg_ts_config_map AS mm \n"
4058 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
4059 " ORDER BY mapcfg, maptokentype, mapseqno \n"
4060 " ) :: pg_catalog.text , \n"
4061 " '{}') AS \"%s\" \n"
4062 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
4063 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
4064 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
4066 gettext_noop("Token"),
4067 gettext_noop("Dictionaries"),
4070 res = PSQLexec(buf.data);
4071 termPQExpBuffer(&buf);
4075 initPQExpBuffer(&title);
4078 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4081 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4085 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4088 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4091 myopt.nullPrint = NULL;
4092 myopt.title = title.data;
4093 myopt.footers = NULL;
4094 myopt.topt.default_footer = false;
4095 myopt.translate_header = true;
4097 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4099 termPQExpBuffer(&title);
4109 * Describes foreign-data wrappers
4112 listForeignDataWrappers(const char *pattern, bool verbose)
4114 PQExpBufferData buf;
4116 printQueryOpt myopt = pset.popt;
4118 if (pset.sversion < 80400)
4120 psql_error("The server (version %d.%d) does not support foreign-data wrappers.\n",
4121 pset.sversion / 10000, (pset.sversion / 100) % 100);
4125 initPQExpBuffer(&buf);
4126 printfPQExpBuffer(&buf,
4127 "SELECT fdw.fdwname AS \"%s\",\n"
4128 " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n",
4129 gettext_noop("Name"),
4130 gettext_noop("Owner"));
4131 if (pset.sversion >= 90100)
4132 appendPQExpBuffer(&buf,
4133 " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n",
4134 gettext_noop("Handler"));
4135 appendPQExpBuffer(&buf,
4136 " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
4137 gettext_noop("Validator"));
4141 appendPQExpBufferStr(&buf, ",\n ");
4142 printACLColumn(&buf, "fdwacl");
4143 appendPQExpBuffer(&buf,
4144 ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
4145 " '(' || array_to_string(ARRAY(SELECT "
4146 " quote_ident(option_name) || ' ' || "
4147 " quote_literal(option_value) FROM "
4148 " pg_options_to_table(fdwoptions)), ', ') || ')' "
4150 gettext_noop("FDW Options"));
4152 if (pset.sversion >= 90100)
4153 appendPQExpBuffer(&buf,
4154 ",\n d.description AS \"%s\" ",
4155 gettext_noop("Description"));
4158 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
4160 if (verbose && pset.sversion >= 90100)
4161 appendPQExpBufferStr(&buf,
4162 "LEFT JOIN pg_catalog.pg_description d\n"
4163 " ON d.classoid = fdw.tableoid "
4164 "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
4166 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4167 NULL, "fdwname", NULL, NULL);
4169 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4171 res = PSQLexec(buf.data);
4172 termPQExpBuffer(&buf);
4176 myopt.nullPrint = NULL;
4177 myopt.title = _("List of foreign-data wrappers");
4178 myopt.translate_header = true;
4180 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4189 * Describes foreign servers.
4192 listForeignServers(const char *pattern, bool verbose)
4194 PQExpBufferData buf;
4196 printQueryOpt myopt = pset.popt;
4198 if (pset.sversion < 80400)
4200 psql_error("The server (version %d.%d) does not support foreign servers.\n",
4201 pset.sversion / 10000, (pset.sversion / 100) % 100);
4205 initPQExpBuffer(&buf);
4206 printfPQExpBuffer(&buf,
4207 "SELECT s.srvname AS \"%s\",\n"
4208 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
4209 " f.fdwname AS \"%s\"",
4210 gettext_noop("Name"),
4211 gettext_noop("Owner"),
4212 gettext_noop("Foreign-data wrapper"));
4216 appendPQExpBufferStr(&buf, ",\n ");
4217 printACLColumn(&buf, "s.srvacl");
4218 appendPQExpBuffer(&buf,
4220 " s.srvtype AS \"%s\",\n"
4221 " s.srvversion AS \"%s\",\n"
4222 " CASE WHEN srvoptions IS NULL THEN '' ELSE "
4223 " '(' || array_to_string(ARRAY(SELECT "
4224 " quote_ident(option_name) || ' ' || "
4225 " quote_literal(option_value) FROM "
4226 " pg_options_to_table(srvoptions)), ', ') || ')' "
4228 " d.description AS \"%s\"",
4229 gettext_noop("Type"),
4230 gettext_noop("Version"),
4231 gettext_noop("FDW Options"),
4232 gettext_noop("Description"));
4235 appendPQExpBufferStr(&buf,
4236 "\nFROM pg_catalog.pg_foreign_server s\n"
4237 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
4240 appendPQExpBufferStr(&buf,
4241 "LEFT JOIN pg_description d\n "
4242 "ON d.classoid = s.tableoid AND d.objoid = s.oid "
4243 "AND d.objsubid = 0\n");
4245 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4246 NULL, "s.srvname", NULL, NULL);
4248 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4250 res = PSQLexec(buf.data);
4251 termPQExpBuffer(&buf);
4255 myopt.nullPrint = NULL;
4256 myopt.title = _("List of foreign servers");
4257 myopt.translate_header = true;
4259 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4268 * Describes user mappings.
4271 listUserMappings(const char *pattern, bool verbose)
4273 PQExpBufferData buf;
4275 printQueryOpt myopt = pset.popt;
4277 if (pset.sversion < 80400)
4279 psql_error("The server (version %d.%d) does not support user mappings.\n",
4280 pset.sversion / 10000, (pset.sversion / 100) % 100);
4284 initPQExpBuffer(&buf);
4285 printfPQExpBuffer(&buf,
4286 "SELECT um.srvname AS \"%s\",\n"
4287 " um.usename AS \"%s\"",
4288 gettext_noop("Server"),
4289 gettext_noop("User name"));
4292 appendPQExpBuffer(&buf,
4293 ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4294 " '(' || array_to_string(ARRAY(SELECT "
4295 " quote_ident(option_name) || ' ' || "
4296 " quote_literal(option_value) FROM "
4297 " pg_options_to_table(umoptions)), ', ') || ')' "
4299 gettext_noop("FDW Options"));
4301 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
4303 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4304 NULL, "um.srvname", "um.usename", NULL);
4306 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4308 res = PSQLexec(buf.data);
4309 termPQExpBuffer(&buf);
4313 myopt.nullPrint = NULL;
4314 myopt.title = _("List of user mappings");
4315 myopt.translate_header = true;
4317 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4326 * Describes foreign tables.
4329 listForeignTables(const char *pattern, bool verbose)
4331 PQExpBufferData buf;
4333 printQueryOpt myopt = pset.popt;
4335 if (pset.sversion < 90100)
4337 psql_error("The server (version %d.%d) does not support foreign tables.\n",
4338 pset.sversion / 10000, (pset.sversion / 100) % 100);
4342 initPQExpBuffer(&buf);
4343 printfPQExpBuffer(&buf,
4344 "SELECT n.nspname AS \"%s\",\n"
4345 " c.relname AS \"%s\",\n"
4346 " s.srvname AS \"%s\"",
4347 gettext_noop("Schema"),
4348 gettext_noop("Table"),
4349 gettext_noop("Server"));
4352 appendPQExpBuffer(&buf,
4353 ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4354 " '(' || array_to_string(ARRAY(SELECT "
4355 " quote_ident(option_name) || ' ' || "
4356 " quote_literal(option_value) FROM "
4357 " pg_options_to_table(ftoptions)), ', ') || ')' "
4359 " d.description AS \"%s\"",
4360 gettext_noop("FDW Options"),
4361 gettext_noop("Description"));
4363 appendPQExpBufferStr(&buf,
4364 "\nFROM pg_catalog.pg_foreign_table ft\n"
4365 " INNER JOIN pg_catalog.pg_class c"
4366 " ON c.oid = ft.ftrelid\n"
4367 " INNER JOIN pg_catalog.pg_namespace n"
4368 " ON n.oid = c.relnamespace\n"
4369 " INNER JOIN pg_catalog.pg_foreign_server s"
4370 " ON s.oid = ft.ftserver\n");
4372 appendPQExpBufferStr(&buf,
4373 " LEFT JOIN pg_catalog.pg_description d\n"
4374 " ON d.classoid = c.tableoid AND "
4375 "d.objoid = c.oid AND d.objsubid = 0\n");
4377 processSQLNamePattern(pset.db, &buf, pattern, false, false,
4378 NULL, "n.nspname", "c.relname", NULL);
4380 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4382 res = PSQLexec(buf.data);
4383 termPQExpBuffer(&buf);
4387 myopt.nullPrint = NULL;
4388 myopt.title = _("List of foreign tables");
4389 myopt.translate_header = true;
4391 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4400 * Briefly describes installed extensions.
4403 listExtensions(const char *pattern)
4405 PQExpBufferData buf;
4407 printQueryOpt myopt = pset.popt;
4409 if (pset.sversion < 90100)
4411 psql_error("The server (version %d.%d) does not support extensions.\n",
4412 pset.sversion / 10000, (pset.sversion / 100) % 100);
4416 initPQExpBuffer(&buf);
4417 printfPQExpBuffer(&buf,
4418 "SELECT e.extname AS \"%s\", "
4419 "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
4420 "FROM pg_catalog.pg_extension e "
4421 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
4422 "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
4423 "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
4424 gettext_noop("Name"),
4425 gettext_noop("Version"),
4426 gettext_noop("Schema"),
4427 gettext_noop("Description"));
4429 processSQLNamePattern(pset.db, &buf, pattern,
4431 NULL, "e.extname", NULL,
4434 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4436 res = PSQLexec(buf.data);
4437 termPQExpBuffer(&buf);
4441 myopt.nullPrint = NULL;
4442 myopt.title = _("List of installed extensions");
4443 myopt.translate_header = true;
4445 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4454 * List contents of installed extensions.
4457 listExtensionContents(const char *pattern)
4459 PQExpBufferData buf;
4463 if (pset.sversion < 90100)
4465 psql_error("The server (version %d.%d) does not support extensions.\n",
4466 pset.sversion / 10000, (pset.sversion / 100) % 100);
4470 initPQExpBuffer(&buf);
4471 printfPQExpBuffer(&buf,
4472 "SELECT e.extname, e.oid\n"
4473 "FROM pg_catalog.pg_extension e\n");
4475 processSQLNamePattern(pset.db, &buf, pattern,
4477 NULL, "e.extname", NULL,
4480 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4482 res = PSQLexec(buf.data);
4483 termPQExpBuffer(&buf);
4487 if (PQntuples(res) == 0)
4492 psql_error("Did not find any extension named \"%s\".\n",
4495 psql_error("Did not find any extensions.\n");
4501 for (i = 0; i < PQntuples(res); i++)
4503 const char *extname;
4506 extname = PQgetvalue(res, i, 0);
4507 oid = PQgetvalue(res, i, 1);
4509 if (!listOneExtensionContents(extname, oid))
4526 listOneExtensionContents(const char *extname, const char *oid)
4528 PQExpBufferData buf;
4531 printQueryOpt myopt = pset.popt;
4533 initPQExpBuffer(&buf);
4534 printfPQExpBuffer(&buf,
4535 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
4536 "FROM pg_catalog.pg_depend\n"
4537 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
4539 gettext_noop("Object Description"),
4542 res = PSQLexec(buf.data);
4543 termPQExpBuffer(&buf);
4547 myopt.nullPrint = NULL;
4548 snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
4549 myopt.title = title;
4550 myopt.translate_header = true;
4552 printQuery(res, &myopt, pset.queryFout, pset.logfile);
4561 * Helper function for consistently formatting ACL (privilege) columns.
4562 * The proper targetlist entry is appended to buf. Note lack of any
4563 * whitespace or comma decoration.
4566 printACLColumn(PQExpBuffer buf, const char *colname)
4568 if (pset.sversion >= 80100)
4569 appendPQExpBuffer(buf,
4570 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
4571 colname, gettext_noop("Access privileges"));
4573 appendPQExpBuffer(buf,
4574 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
4575 colname, gettext_noop("Access privileges"));