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-2011, PostgreSQL Global Development Group
11 * src/bin/psql/describe.c
13 #include "postgres_fe.h"
19 #include "dumputils.h"
23 #include "variables.h"
26 static bool describeOneTableDetails(const char *schemaname,
27 const char *relationname,
30 static void add_tablespace_footer(printTableContent *const cont, char relkind,
31 Oid tablespace, const bool newline);
32 static void add_role_attribute(PQExpBuffer buf, const char *const str);
33 static bool listTSParsersVerbose(const char *pattern);
34 static bool describeOneTSParser(const char *oid, const char *nspname,
36 static bool listTSConfigsVerbose(const char *pattern);
37 static bool describeOneTSConfig(const char *oid, const char *nspname,
39 const char *pnspname, const char *prsname);
40 static void printACLColumn(PQExpBuffer buf, const char *colname);
41 static bool listOneExtensionContents(const char *extname, const char *oid);
45 * Handlers for various slash commands displaying some sort of list
46 * of things in the database.
48 * Note: try to format the queries to look nice in -E output.
54 * Takes an optional regexp to select particular aggregates
57 describeAggregates(const char *pattern, bool verbose, bool showSystem)
61 printQueryOpt myopt = pset.popt;
63 initPQExpBuffer(&buf);
65 printfPQExpBuffer(&buf,
66 "SELECT n.nspname as \"%s\",\n"
67 " p.proname AS \"%s\",\n"
68 " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
69 gettext_noop("Schema"),
71 gettext_noop("Result data type"));
73 if (pset.sversion >= 80200)
74 appendPQExpBuffer(&buf,
75 " CASE WHEN p.pronargs = 0\n"
76 " THEN CAST('*' AS pg_catalog.text)\n"
78 " pg_catalog.array_to_string(ARRAY(\n"
80 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
82 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
85 gettext_noop("Argument data types"));
87 appendPQExpBuffer(&buf,
88 " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
89 gettext_noop("Argument data types"));
91 appendPQExpBuffer(&buf,
92 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
93 "FROM pg_catalog.pg_proc p\n"
94 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
96 gettext_noop("Description"));
98 if (!showSystem && !pattern)
99 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
100 " AND n.nspname <> 'information_schema'\n");
102 processSQLNamePattern(pset.db, &buf, pattern, true, false,
103 "n.nspname", "p.proname", NULL,
104 "pg_catalog.pg_function_is_visible(p.oid)");
106 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
108 res = PSQLexec(buf.data, false);
109 termPQExpBuffer(&buf);
113 myopt.nullPrint = NULL;
114 myopt.title = _("List of aggregate functions");
115 myopt.translate_header = true;
117 printQuery(res, &myopt, pset.queryFout, pset.logfile);
124 * Takes an optional regexp to select particular tablespaces
127 describeTablespaces(const char *pattern, bool verbose)
131 printQueryOpt myopt = pset.popt;
133 if (pset.sversion < 80000)
135 fprintf(stderr, _("The server (version %d.%d) does not support tablespaces.\n"),
136 pset.sversion / 10000, (pset.sversion / 100) % 100);
140 initPQExpBuffer(&buf);
142 printfPQExpBuffer(&buf,
143 "SELECT spcname AS \"%s\",\n"
144 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
145 " spclocation AS \"%s\"",
146 gettext_noop("Name"),
147 gettext_noop("Owner"),
148 gettext_noop("Location"));
152 appendPQExpBuffer(&buf, ",\n ");
153 printACLColumn(&buf, "spcacl");
156 if (verbose && pset.sversion >= 80200)
157 appendPQExpBuffer(&buf,
158 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
159 gettext_noop("Description"));
161 appendPQExpBuffer(&buf,
162 "\nFROM pg_catalog.pg_tablespace\n");
164 processSQLNamePattern(pset.db, &buf, pattern, false, false,
165 NULL, "spcname", NULL,
168 appendPQExpBuffer(&buf, "ORDER BY 1;");
170 res = PSQLexec(buf.data, false);
171 termPQExpBuffer(&buf);
175 myopt.nullPrint = NULL;
176 myopt.title = _("List of tablespaces");
177 myopt.translate_header = true;
179 printQuery(res, &myopt, pset.queryFout, pset.logfile);
187 * Takes an optional regexp to select particular functions.
189 * As with \d, you can specify the kinds of functions you want:
196 * and you can mix and match these in any order.
199 describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
201 bool showAggregate = strchr(functypes, 'a') != NULL;
202 bool showNormal = strchr(functypes, 'n') != NULL;
203 bool showTrigger = strchr(functypes, 't') != NULL;
204 bool showWindow = strchr(functypes, 'w') != NULL;
208 printQueryOpt myopt = pset.popt;
209 static const bool translate_columns[] = {false, false, false, false, true, true, false, false, false, false};
211 if (strlen(functypes) != strspn(functypes, "antwS+"))
213 fprintf(stderr, _("\\df only takes [antwS+] as options\n"));
217 if (showWindow && pset.sversion < 80400)
219 fprintf(stderr, _("\\df does not take a \"w\" option with server version %d.%d\n"),
220 pset.sversion / 10000, (pset.sversion / 100) % 100);
224 if (!showAggregate && !showNormal && !showTrigger && !showWindow)
226 showAggregate = showNormal = showTrigger = true;
227 if (pset.sversion >= 80400)
231 initPQExpBuffer(&buf);
233 printfPQExpBuffer(&buf,
234 "SELECT n.nspname as \"%s\",\n"
235 " p.proname as \"%s\",\n",
236 gettext_noop("Schema"),
237 gettext_noop("Name"));
239 if (pset.sversion >= 80400)
240 appendPQExpBuffer(&buf,
241 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
242 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
244 " WHEN p.proisagg THEN '%s'\n"
245 " WHEN p.proiswindow THEN '%s'\n"
246 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
249 gettext_noop("Result data type"),
250 gettext_noop("Argument data types"),
251 /* translator: "agg" is short for "aggregate" */
253 gettext_noop("window"),
254 gettext_noop("trigger"),
255 gettext_noop("normal"),
256 gettext_noop("Type"));
257 else if (pset.sversion >= 80100)
258 appendPQExpBuffer(&buf,
259 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
260 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
261 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
262 " pg_catalog.array_to_string(ARRAY(\n"
265 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
266 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
267 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
268 " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
271 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
272 " ELSE p.proargnames[s.i] || ' ' \n"
274 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
276 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
279 " pg_catalog.array_to_string(ARRAY(\n"
282 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
283 " ELSE p.proargnames[s.i+1] || ' '\n"
285 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
287 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
291 " WHEN p.proisagg THEN '%s'\n"
292 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
295 gettext_noop("Result data type"),
296 gettext_noop("Argument data types"),
297 /* translator: "agg" is short for "aggregate" */
299 gettext_noop("trigger"),
300 gettext_noop("normal"),
301 gettext_noop("Type"));
303 appendPQExpBuffer(&buf,
304 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
305 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
306 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
308 " WHEN p.proisagg THEN '%s'\n"
309 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
312 gettext_noop("Result data type"),
313 gettext_noop("Argument data types"),
314 /* translator: "agg" is short for "aggregate" */
316 gettext_noop("trigger"),
317 gettext_noop("normal"),
318 gettext_noop("Type"));
321 appendPQExpBuffer(&buf,
323 " WHEN p.provolatile = 'i' THEN '%s'\n"
324 " WHEN p.provolatile = 's' THEN '%s'\n"
325 " WHEN p.provolatile = 'v' THEN '%s'\n"
327 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
328 " l.lanname as \"%s\",\n"
329 " p.prosrc as \"%s\",\n"
330 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
331 gettext_noop("immutable"),
332 gettext_noop("stable"),
333 gettext_noop("volatile"),
334 gettext_noop("Volatility"),
335 gettext_noop("Owner"),
336 gettext_noop("Language"),
337 gettext_noop("Source code"),
338 gettext_noop("Description"));
340 appendPQExpBuffer(&buf,
341 "\nFROM pg_catalog.pg_proc p"
342 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
345 appendPQExpBuffer(&buf,
346 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
350 /* filter by function type, if requested */
351 if (showNormal && showAggregate && showTrigger && showWindow)
358 appendPQExpBuffer(&buf, " AND ");
361 appendPQExpBuffer(&buf, "WHERE ");
364 appendPQExpBuffer(&buf, "NOT p.proisagg\n");
369 appendPQExpBuffer(&buf, " AND ");
372 appendPQExpBuffer(&buf, "WHERE ");
375 appendPQExpBuffer(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
377 if (!showWindow && pset.sversion >= 80400)
380 appendPQExpBuffer(&buf, " AND ");
383 appendPQExpBuffer(&buf, "WHERE ");
386 appendPQExpBuffer(&buf, "NOT p.proiswindow\n");
391 bool needs_or = false;
393 appendPQExpBuffer(&buf, "WHERE (\n ");
395 /* Note: at least one of these must be true ... */
398 appendPQExpBuffer(&buf, "p.proisagg\n");
404 appendPQExpBuffer(&buf, " OR ");
405 appendPQExpBuffer(&buf,
406 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
412 appendPQExpBuffer(&buf, " OR ");
413 appendPQExpBuffer(&buf, "p.proiswindow\n");
416 appendPQExpBuffer(&buf, " )\n");
419 processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
420 "n.nspname", "p.proname", NULL,
421 "pg_catalog.pg_function_is_visible(p.oid)");
423 if (!showSystem && !pattern)
424 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
425 " AND n.nspname <> 'information_schema'\n");
427 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
429 res = PSQLexec(buf.data, false);
430 termPQExpBuffer(&buf);
434 myopt.nullPrint = NULL;
435 myopt.title = _("List of functions");
436 myopt.translate_header = true;
437 myopt.translate_columns = translate_columns;
439 printQuery(res, &myopt, pset.queryFout, pset.logfile);
452 describeTypes(const char *pattern, bool verbose, bool showSystem)
456 printQueryOpt myopt = pset.popt;
458 initPQExpBuffer(&buf);
460 printfPQExpBuffer(&buf,
461 "SELECT n.nspname as \"%s\",\n"
462 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
463 gettext_noop("Schema"),
464 gettext_noop("Name"));
466 appendPQExpBuffer(&buf,
467 " t.typname AS \"%s\",\n"
468 " CASE WHEN t.typrelid != 0\n"
469 " THEN CAST('tuple' AS pg_catalog.text)\n"
470 " WHEN t.typlen < 0\n"
471 " THEN CAST('var' AS pg_catalog.text)\n"
472 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
474 gettext_noop("Internal name"),
475 gettext_noop("Size"));
476 if (verbose && pset.sversion >= 80300)
478 appendPQExpBuffer(&buf,
479 " pg_catalog.array_to_string(\n"
481 " SELECT e.enumlabel\n"
482 " FROM pg_catalog.pg_enum e\n"
483 " WHERE e.enumtypid = t.oid\n");
485 if (pset.sversion >= 90100)
486 appendPQExpBuffer(&buf,
487 " ORDER BY e.enumsortorder\n");
489 appendPQExpBuffer(&buf,
490 " ORDER BY e.oid\n");
492 appendPQExpBuffer(&buf,
496 gettext_noop("Elements"));
499 appendPQExpBuffer(&buf,
500 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
501 gettext_noop("Description"));
503 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
504 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
507 * do not include complex types (typrelid!=0) unless they are standalone
510 appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
511 appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
512 "WHERE c.oid = t.typrelid))\n");
515 * do not include array types (before 8.3 we have to use the assumption
516 * that their names start with underscore)
518 if (pset.sversion >= 80300)
519 appendPQExpBuffer(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
521 appendPQExpBuffer(&buf, " AND t.typname !~ '^_'\n");
523 if (!showSystem && !pattern)
524 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
525 " AND n.nspname <> 'information_schema'\n");
527 /* Match name pattern against either internal or external name */
528 processSQLNamePattern(pset.db, &buf, pattern, true, false,
529 "n.nspname", "t.typname",
530 "pg_catalog.format_type(t.oid, NULL)",
531 "pg_catalog.pg_type_is_visible(t.oid)");
533 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
535 res = PSQLexec(buf.data, false);
536 termPQExpBuffer(&buf);
540 myopt.nullPrint = NULL;
541 myopt.title = _("List of data types");
542 myopt.translate_header = true;
544 printQuery(res, &myopt, pset.queryFout, pset.logfile);
554 describeOperators(const char *pattern, bool showSystem)
558 printQueryOpt myopt = pset.popt;
560 initPQExpBuffer(&buf);
562 printfPQExpBuffer(&buf,
563 "SELECT n.nspname as \"%s\",\n"
564 " o.oprname AS \"%s\",\n"
565 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
566 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
567 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
568 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
569 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
570 "FROM pg_catalog.pg_operator o\n"
571 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
572 gettext_noop("Schema"),
573 gettext_noop("Name"),
574 gettext_noop("Left arg type"),
575 gettext_noop("Right arg type"),
576 gettext_noop("Result type"),
577 gettext_noop("Description"));
579 if (!showSystem && !pattern)
580 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
581 " AND n.nspname <> 'information_schema'\n");
583 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
584 "n.nspname", "o.oprname", NULL,
585 "pg_catalog.pg_operator_is_visible(o.oid)");
587 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
589 res = PSQLexec(buf.data, false);
590 termPQExpBuffer(&buf);
594 myopt.nullPrint = NULL;
595 myopt.title = _("List of operators");
596 myopt.translate_header = true;
598 printQuery(res, &myopt, pset.queryFout, pset.logfile);
608 * for \l, \list, and -l switch
611 listAllDbs(bool verbose)
615 printQueryOpt myopt = pset.popt;
617 initPQExpBuffer(&buf);
619 printfPQExpBuffer(&buf,
620 "SELECT d.datname as \"%s\",\n"
621 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
622 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
623 gettext_noop("Name"),
624 gettext_noop("Owner"),
625 gettext_noop("Encoding"));
626 if (pset.sversion >= 80400)
627 appendPQExpBuffer(&buf,
628 " d.datcollate as \"%s\",\n"
629 " d.datctype as \"%s\",\n",
630 gettext_noop("Collation"),
631 gettext_noop("Ctype"));
632 appendPQExpBuffer(&buf, " ");
633 printACLColumn(&buf, "d.datacl");
634 if (verbose && pset.sversion >= 80200)
635 appendPQExpBuffer(&buf,
636 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
637 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
638 " ELSE 'No Access'\n"
640 gettext_noop("Size"));
641 if (verbose && pset.sversion >= 80000)
642 appendPQExpBuffer(&buf,
643 ",\n t.spcname as \"%s\"",
644 gettext_noop("Tablespace"));
645 if (verbose && pset.sversion >= 80200)
646 appendPQExpBuffer(&buf,
647 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
648 gettext_noop("Description"));
649 appendPQExpBuffer(&buf,
650 "\nFROM pg_catalog.pg_database d\n");
651 if (verbose && pset.sversion >= 80000)
652 appendPQExpBuffer(&buf,
653 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
654 appendPQExpBuffer(&buf, "ORDER BY 1;");
655 res = PSQLexec(buf.data, false);
656 termPQExpBuffer(&buf);
660 myopt.nullPrint = NULL;
661 myopt.title = _("List of databases");
662 myopt.translate_header = true;
664 printQuery(res, &myopt, pset.queryFout, pset.logfile);
672 * List Tables' Grant/Revoke Permissions
673 * \z (now also \dp -- perhaps more mnemonic)
676 permissionsList(const char *pattern)
680 printQueryOpt myopt = pset.popt;
681 static const bool translate_columns[] = {false, false, true, false, false};
683 initPQExpBuffer(&buf);
686 * we ignore indexes and toast tables since they have no meaningful rights
688 printfPQExpBuffer(&buf,
689 "SELECT n.nspname as \"%s\",\n"
690 " c.relname as \"%s\",\n"
691 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
693 gettext_noop("Schema"),
694 gettext_noop("Name"),
695 gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
696 gettext_noop("foreign table"),
697 gettext_noop("Type"));
699 printACLColumn(&buf, "c.relacl");
701 if (pset.sversion >= 80400)
702 appendPQExpBuffer(&buf,
703 ",\n pg_catalog.array_to_string(ARRAY(\n"
704 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
705 " FROM pg_catalog.pg_attribute a\n"
706 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
707 " ), E'\\n') AS \"%s\"",
708 gettext_noop("Column access privileges"));
710 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_class c\n"
711 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
712 "WHERE c.relkind IN ('r', 'v', 'S', 'f')\n");
715 * Unless a schema pattern is specified, we suppress system and temp
716 * tables, since they normally aren't very interesting from a permissions
717 * point of view. You can see 'em by explicit request though, eg with \z
720 processSQLNamePattern(pset.db, &buf, pattern, true, false,
721 "n.nspname", "c.relname", NULL,
722 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
724 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
726 res = PSQLexec(buf.data, false);
729 termPQExpBuffer(&buf);
733 myopt.nullPrint = NULL;
734 printfPQExpBuffer(&buf, _("Access privileges"));
735 myopt.title = buf.data;
736 myopt.translate_header = true;
737 myopt.translate_columns = translate_columns;
739 printQuery(res, &myopt, pset.queryFout, pset.logfile);
741 termPQExpBuffer(&buf);
750 * List DefaultACLs. The pattern can match either schema or role name.
753 listDefaultACLs(const char *pattern)
757 printQueryOpt myopt = pset.popt;
758 static const bool translate_columns[] = {false, false, true, false};
760 if (pset.sversion < 90000)
762 fprintf(stderr, _("The server (version %d.%d) does not support altering default privileges.\n"),
763 pset.sversion / 10000, (pset.sversion / 100) % 100);
767 initPQExpBuffer(&buf);
769 printfPQExpBuffer(&buf,
770 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
771 " n.nspname AS \"%s\",\n"
772 " CASE d.defaclobjtype WHEN 'r' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END AS \"%s\",\n"
774 gettext_noop("Owner"),
775 gettext_noop("Schema"),
776 gettext_noop("table"),
777 gettext_noop("sequence"),
778 gettext_noop("function"),
779 gettext_noop("Type"));
781 printACLColumn(&buf, "d.defaclacl");
783 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
784 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
786 processSQLNamePattern(pset.db, &buf, pattern, false, false,
789 "pg_catalog.pg_get_userbyid(d.defaclrole)",
792 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
794 res = PSQLexec(buf.data, false);
797 termPQExpBuffer(&buf);
801 myopt.nullPrint = NULL;
802 printfPQExpBuffer(&buf, _("Default access privileges"));
803 myopt.title = buf.data;
804 myopt.translate_header = true;
805 myopt.translate_columns = translate_columns;
807 printQuery(res, &myopt, pset.queryFout, pset.logfile);
809 termPQExpBuffer(&buf);
816 * Get object comments
820 * Note: This only lists things that actually have a description. For complete
821 * lists of things, there are other \d? commands.
824 objectDescription(const char *pattern, bool showSystem)
828 printQueryOpt myopt = pset.popt;
829 static const bool translate_columns[] = {false, false, true, false};
831 initPQExpBuffer(&buf);
833 appendPQExpBuffer(&buf,
834 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
836 gettext_noop("Schema"),
837 gettext_noop("Name"),
838 gettext_noop("Object"),
839 gettext_noop("Description"));
841 /* Aggregate descriptions */
842 appendPQExpBuffer(&buf,
843 " SELECT p.oid as oid, p.tableoid as tableoid,\n"
844 " n.nspname as nspname,\n"
845 " CAST(p.proname AS pg_catalog.text) as name,"
846 " CAST('%s' AS pg_catalog.text) as object\n"
847 " FROM pg_catalog.pg_proc p\n"
848 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
849 " WHERE p.proisagg\n",
850 gettext_noop("aggregate"));
852 if (!showSystem && !pattern)
853 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
854 " AND n.nspname <> 'information_schema'\n");
856 processSQLNamePattern(pset.db, &buf, pattern, true, false,
857 "n.nspname", "p.proname", NULL,
858 "pg_catalog.pg_function_is_visible(p.oid)");
860 /* Function descriptions */
861 appendPQExpBuffer(&buf,
863 " SELECT p.oid as oid, p.tableoid as tableoid,\n"
864 " n.nspname as nspname,\n"
865 " CAST(p.proname AS pg_catalog.text) as name,"
866 " CAST('%s' AS pg_catalog.text) as object\n"
867 " FROM pg_catalog.pg_proc p\n"
868 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
869 " WHERE NOT p.proisagg\n",
870 gettext_noop("function"));
872 if (!showSystem && !pattern)
873 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
874 " AND n.nspname <> 'information_schema'\n");
876 processSQLNamePattern(pset.db, &buf, pattern, true, false,
877 "n.nspname", "p.proname", NULL,
878 "pg_catalog.pg_function_is_visible(p.oid)");
880 /* Operator descriptions (only if operator has its own comment) */
881 appendPQExpBuffer(&buf,
883 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
884 " n.nspname as nspname,\n"
885 " CAST(o.oprname AS pg_catalog.text) as name,"
886 " CAST('%s' AS pg_catalog.text) as object\n"
887 " FROM pg_catalog.pg_operator o\n"
888 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
889 gettext_noop("operator"));
891 if (!showSystem && !pattern)
892 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
893 " AND n.nspname <> 'information_schema'\n");
895 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
896 "n.nspname", "o.oprname", NULL,
897 "pg_catalog.pg_operator_is_visible(o.oid)");
899 /* Type description */
900 appendPQExpBuffer(&buf,
902 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
903 " n.nspname as nspname,\n"
904 " pg_catalog.format_type(t.oid, NULL) as name,"
905 " CAST('%s' AS pg_catalog.text) as object\n"
906 " FROM pg_catalog.pg_type t\n"
907 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n",
908 gettext_noop("data type"));
910 if (!showSystem && !pattern)
911 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
912 " AND n.nspname <> 'information_schema'\n");
914 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
915 "n.nspname", "pg_catalog.format_type(t.oid, NULL)",
917 "pg_catalog.pg_type_is_visible(t.oid)");
919 /* Relation (tables, views, indexes, sequences) descriptions */
920 appendPQExpBuffer(&buf,
922 " SELECT c.oid as oid, c.tableoid as tableoid,\n"
923 " n.nspname as nspname,\n"
924 " CAST(c.relname AS pg_catalog.text) as name,\n"
926 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 'f' THEN '%s' END"
927 " AS pg_catalog.text) as object\n"
928 " FROM pg_catalog.pg_class c\n"
929 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
930 " WHERE c.relkind IN ('r', 'v', 'i', 'S', 'f')\n",
931 gettext_noop("table"),
932 gettext_noop("view"),
933 gettext_noop("index"),
934 gettext_noop("sequence"),
935 gettext_noop("foreign table"));
937 if (!showSystem && !pattern)
938 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
939 " AND n.nspname <> 'information_schema'\n");
941 processSQLNamePattern(pset.db, &buf, pattern, true, false,
942 "n.nspname", "c.relname", NULL,
943 "pg_catalog.pg_table_is_visible(c.oid)");
945 /* Rule description (ignore rules for views) */
946 appendPQExpBuffer(&buf,
948 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
949 " n.nspname as nspname,\n"
950 " CAST(r.rulename AS pg_catalog.text) as name,"
951 " CAST('%s' AS pg_catalog.text) as object\n"
952 " FROM pg_catalog.pg_rewrite r\n"
953 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
954 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
955 " WHERE r.rulename != '_RETURN'\n",
956 gettext_noop("rule"));
958 if (!showSystem && !pattern)
959 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
960 " AND n.nspname <> 'information_schema'\n");
962 /* XXX not sure what to do about visibility rule here? */
963 processSQLNamePattern(pset.db, &buf, pattern, true, false,
964 "n.nspname", "r.rulename", NULL,
965 "pg_catalog.pg_table_is_visible(c.oid)");
967 /* Trigger description */
968 appendPQExpBuffer(&buf,
970 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
971 " n.nspname as nspname,\n"
972 " CAST(t.tgname AS pg_catalog.text) as name,"
973 " CAST('%s' AS pg_catalog.text) as object\n"
974 " FROM pg_catalog.pg_trigger t\n"
975 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
976 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
977 gettext_noop("trigger"));
979 if (!showSystem && !pattern)
980 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
981 " AND n.nspname <> 'information_schema'\n");
983 /* XXX not sure what to do about visibility rule here? */
984 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
985 "n.nspname", "t.tgname", NULL,
986 "pg_catalog.pg_table_is_visible(c.oid)");
988 appendPQExpBuffer(&buf,
990 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
992 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
994 res = PSQLexec(buf.data, false);
995 termPQExpBuffer(&buf);
999 myopt.nullPrint = NULL;
1000 myopt.title = _("Object descriptions");
1001 myopt.translate_header = true;
1002 myopt.translate_columns = translate_columns;
1004 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1012 * describeTableDetails (for \d)
1014 * This routine finds the tables to be displayed, and calls
1015 * describeOneTableDetails for each one.
1017 * verbose: if true, this is \d+
1020 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1022 PQExpBufferData buf;
1026 initPQExpBuffer(&buf);
1028 printfPQExpBuffer(&buf,
1032 "FROM pg_catalog.pg_class c\n"
1033 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1035 if (!showSystem && !pattern)
1036 appendPQExpBuffer(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1037 " AND n.nspname <> 'information_schema'\n");
1039 processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1040 "n.nspname", "c.relname", NULL,
1041 "pg_catalog.pg_table_is_visible(c.oid)");
1043 appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
1045 res = PSQLexec(buf.data, false);
1046 termPQExpBuffer(&buf);
1050 if (PQntuples(res) == 0)
1053 fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
1059 for (i = 0; i < PQntuples(res); i++)
1062 const char *nspname;
1063 const char *relname;
1065 oid = PQgetvalue(res, i, 0);
1066 nspname = PQgetvalue(res, i, 1);
1067 relname = PQgetvalue(res, i, 2);
1069 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1086 * describeOneTableDetails (for \d)
1088 * Unfortunately, the information presented here is so complicated that it
1089 * cannot be done in a single query. So we have to assemble the printed table
1090 * by hand and pass it to the underlying printTable() function.
1093 describeOneTableDetails(const char *schemaname,
1094 const char *relationname,
1098 PQExpBufferData buf;
1099 PGresult *res = NULL;
1100 printTableOpt myopt = pset.popt.topt;
1101 printTableContent cont;
1102 bool printTableInitialized = false;
1104 char *view_def = NULL;
1106 char **seq_values = NULL;
1107 char **modifiers = NULL;
1109 PQExpBufferData title;
1110 PQExpBufferData tmpbuf;
1124 char relpersistence;
1126 bool show_modifiers = false;
1131 /* This output looks confusing in expanded mode. */
1132 myopt.expanded = false;
1134 initPQExpBuffer(&buf);
1135 initPQExpBuffer(&title);
1136 initPQExpBuffer(&tmpbuf);
1138 /* Get general table info */
1139 if (pset.sversion >= 90100)
1141 printfPQExpBuffer(&buf,
1142 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1143 "c.relhastriggers, c.relhasoids, "
1144 "%s, c.reltablespace, "
1145 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1146 "c.relpersistence\n"
1147 "FROM pg_catalog.pg_class c\n "
1148 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1149 "WHERE c.oid = '%s'\n",
1151 "pg_catalog.array_to_string(c.reloptions || "
1152 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1156 else if (pset.sversion >= 90000)
1158 printfPQExpBuffer(&buf,
1159 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1160 "c.relhastriggers, c.relhasoids, "
1161 "%s, c.reltablespace, "
1162 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END\n"
1163 "FROM pg_catalog.pg_class c\n "
1164 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1165 "WHERE c.oid = '%s'\n",
1167 "pg_catalog.array_to_string(c.reloptions || "
1168 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1172 else if (pset.sversion >= 80400)
1174 printfPQExpBuffer(&buf,
1175 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1176 "c.relhastriggers, c.relhasoids, "
1177 "%s, c.reltablespace\n"
1178 "FROM pg_catalog.pg_class c\n "
1179 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1180 "WHERE c.oid = '%s'\n",
1182 "pg_catalog.array_to_string(c.reloptions || "
1183 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1187 else if (pset.sversion >= 80200)
1189 printfPQExpBuffer(&buf,
1190 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1191 "reltriggers <> 0, relhasoids, "
1192 "%s, reltablespace\n"
1193 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1195 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1198 else if (pset.sversion >= 80000)
1200 printfPQExpBuffer(&buf,
1201 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1202 "reltriggers <> 0, relhasoids, "
1203 "'', reltablespace\n"
1204 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1209 printfPQExpBuffer(&buf,
1210 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1211 "reltriggers <> 0, relhasoids, "
1213 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1217 res = PSQLexec(buf.data, false);
1221 /* Did we get anything? */
1222 if (PQntuples(res) == 0)
1225 fprintf(stderr, _("Did not find any relation with OID %s.\n"),
1230 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1231 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1232 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1233 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1234 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1235 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1236 tableinfo.reloptions = (pset.sversion >= 80200) ?
1237 strdup(PQgetvalue(res, 0, 6)) : 0;
1238 tableinfo.tablespace = (pset.sversion >= 80000) ?
1239 atooid(PQgetvalue(res, 0, 7)) : 0;
1240 tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
1241 strdup(PQgetvalue(res, 0, 8)) : 0;
1242 tableinfo.relpersistence = (pset.sversion >= 90100 && strcmp(PQgetvalue(res, 0, 9), "") != 0) ?
1243 PQgetvalue(res, 0, 9)[0] : 0;
1248 * If it's a sequence, fetch its values and store into an array that will
1251 if (tableinfo.relkind == 'S')
1253 printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname));
1254 /* must be separate because fmtId isn't reentrant */
1255 appendPQExpBuffer(&buf, ".%s", fmtId(relationname));
1257 res = PSQLexec(buf.data, false);
1261 seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values));
1263 for (i = 0; i < PQnfields(res); i++)
1264 seq_values[i] = pg_strdup(PQgetvalue(res, 0, i));
1265 seq_values[i] = NULL;
1271 /* Get column info */
1272 printfPQExpBuffer(&buf, "SELECT a.attname,");
1273 appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1274 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1275 "\n FROM pg_catalog.pg_attrdef d"
1276 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1277 "\n a.attnotnull, a.attnum");
1278 if (pset.sversion >= 90100)
1279 appendPQExpBuffer(&buf, ",\n (SELECT collname FROM pg_collation WHERE oid = a.attcollation AND collname <> 'default') AS attcollation");
1281 appendPQExpBuffer(&buf, ",\n NULL AS attcollation");
1282 if (tableinfo.relkind == 'i')
1283 appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1285 appendPQExpBuffer(&buf, ",\n a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)");
1286 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
1287 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1288 appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
1290 res = PSQLexec(buf.data, false);
1293 numrows = PQntuples(res);
1296 switch (tableinfo.relkind)
1299 if (tableinfo.relpersistence == 'u')
1300 printfPQExpBuffer(&title, _("Unlogged Table \"%s.%s\""),
1301 schemaname, relationname);
1303 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1304 schemaname, relationname);
1307 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1308 schemaname, relationname);
1311 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1312 schemaname, relationname);
1315 if (tableinfo.relpersistence == 'u')
1316 printfPQExpBuffer(&title, _("Unlogged Index \"%s.%s\""),
1317 schemaname, relationname);
1319 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1320 schemaname, relationname);
1323 /* not used as of 8.2, but keep it for backwards compatibility */
1324 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1325 schemaname, relationname);
1328 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1329 schemaname, relationname);
1332 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1333 schemaname, relationname);
1336 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1337 schemaname, relationname);
1340 /* untranslated unknown relkind */
1341 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1342 tableinfo.relkind, schemaname, relationname);
1346 /* Set the number of columns, and their names */
1347 headers[0] = gettext_noop("Column");
1348 headers[1] = gettext_noop("Type");
1351 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1352 tableinfo.relkind == 'f')
1354 show_modifiers = true;
1355 headers[cols++] = gettext_noop("Modifiers");
1356 modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
1359 if (tableinfo.relkind == 'S')
1360 headers[cols++] = gettext_noop("Value");
1362 if (tableinfo.relkind == 'i')
1363 headers[cols++] = gettext_noop("Definition");
1367 headers[cols++] = gettext_noop("Storage");
1368 headers[cols++] = gettext_noop("Description");
1371 printTableInit(&cont, &myopt, title.data, cols, numrows);
1372 printTableInitialized = true;
1374 for (i = 0; i < cols; i++)
1375 printTableAddHeader(&cont, headers[i], true, 'l');
1377 /* Check if table is a view */
1378 if (tableinfo.relkind == 'v' && verbose)
1382 printfPQExpBuffer(&buf,
1383 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
1385 result = PSQLexec(buf.data, false);
1389 if (PQntuples(result) > 0)
1390 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1395 /* Generate table cells to be printed */
1396 for (i = 0; i < numrows; i++)
1399 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1402 printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1404 /* Modifiers: collate, not null, default */
1407 resetPQExpBuffer(&tmpbuf);
1409 if (!PQgetisnull(res, i, 5))
1412 appendPQExpBufferStr(&tmpbuf, " ");
1413 appendPQExpBuffer(&tmpbuf, _("collate %s"),
1414 PQgetvalue(res, i, 5));
1417 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1420 appendPQExpBufferStr(&tmpbuf, " ");
1421 appendPQExpBufferStr(&tmpbuf, _("not null"));
1424 /* handle "default" here */
1425 /* (note: above we cut off the 'default' string at 128) */
1426 if (strlen(PQgetvalue(res, i, 2)) != 0)
1429 appendPQExpBufferStr(&tmpbuf, " ");
1430 /* translator: default values of column definitions */
1431 appendPQExpBuffer(&tmpbuf, _("default %s"),
1432 PQgetvalue(res, i, 2));
1435 modifiers[i] = pg_strdup(tmpbuf.data);
1436 printTableAddCell(&cont, modifiers[i], false, false);
1439 /* Value: for sequences only */
1440 if (tableinfo.relkind == 'S')
1441 printTableAddCell(&cont, seq_values[i], false, false);
1443 /* Expression for index column */
1444 if (tableinfo.relkind == 'i')
1445 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
1447 /* Storage and Description */
1450 int firstvcol = (tableinfo.relkind == 'i' ? 7 : 6);
1451 char *storage = PQgetvalue(res, i, firstvcol);
1453 /* these strings are literal in our syntax, so not translated. */
1454 printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1455 (storage[0] == 'm' ? "main" :
1456 (storage[0] == 'x' ? "extended" :
1457 (storage[0] == 'e' ? "external" :
1460 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1466 if (tableinfo.relkind == 'i')
1468 /* Footer information about an index */
1471 printfPQExpBuffer(&buf,
1472 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1473 if (pset.sversion >= 80200)
1474 appendPQExpBuffer(&buf, "i.indisvalid,\n");
1476 appendPQExpBuffer(&buf, "true AS indisvalid,\n");
1477 if (pset.sversion >= 90000)
1478 appendPQExpBuffer(&buf,
1479 " (NOT i.indimmediate) AND "
1480 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1481 "WHERE conrelid = i.indrelid AND "
1482 "conindid = i.indexrelid AND "
1483 "contype IN ('p','u','x') AND "
1484 "condeferrable) AS condeferrable,\n"
1485 " (NOT i.indimmediate) AND "
1486 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1487 "WHERE conrelid = i.indrelid AND "
1488 "conindid = i.indexrelid AND "
1489 "contype IN ('p','u','x') AND "
1490 "condeferred) AS condeferred,\n");
1492 appendPQExpBuffer(&buf,
1493 " false AS condeferrable, false AS condeferred,\n");
1494 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
1495 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1496 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1497 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1498 "AND i.indrelid = c2.oid",
1501 result = PSQLexec(buf.data, false);
1504 else if (PQntuples(result) != 1)
1511 char *indisunique = PQgetvalue(result, 0, 0);
1512 char *indisprimary = PQgetvalue(result, 0, 1);
1513 char *indisclustered = PQgetvalue(result, 0, 2);
1514 char *indisvalid = PQgetvalue(result, 0, 3);
1515 char *deferrable = PQgetvalue(result, 0, 4);
1516 char *deferred = PQgetvalue(result, 0, 5);
1517 char *indamname = PQgetvalue(result, 0, 6);
1518 char *indtable = PQgetvalue(result, 0, 7);
1519 char *indpred = PQgetvalue(result, 0, 8);
1521 if (strcmp(indisprimary, "t") == 0)
1522 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1523 else if (strcmp(indisunique, "t") == 0)
1524 printfPQExpBuffer(&tmpbuf, _("unique, "));
1526 resetPQExpBuffer(&tmpbuf);
1527 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1529 /* we assume here that index and table are in same schema */
1530 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1531 schemaname, indtable);
1533 if (strlen(indpred))
1534 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1536 if (strcmp(indisclustered, "t") == 0)
1537 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1539 if (strcmp(indisvalid, "t") != 0)
1540 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1542 if (strcmp(deferrable, "t") == 0)
1543 appendPQExpBuffer(&tmpbuf, _(", deferrable"));
1545 if (strcmp(deferred, "t") == 0)
1546 appendPQExpBuffer(&tmpbuf, _(", initially deferred"));
1548 printTableAddFooter(&cont, tmpbuf.data);
1549 add_tablespace_footer(&cont, tableinfo.relkind,
1550 tableinfo.tablespace, true);
1557 PGresult *result = NULL;
1559 /* Footer information about a view */
1560 printTableAddFooter(&cont, _("View definition:"));
1561 printTableAddFooter(&cont, view_def);
1564 if (tableinfo.hasrules)
1566 printfPQExpBuffer(&buf,
1567 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1568 "FROM pg_catalog.pg_rewrite r\n"
1569 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1",
1571 result = PSQLexec(buf.data, false);
1575 if (PQntuples(result) > 0)
1577 printTableAddFooter(&cont, _("Rules:"));
1578 for (i = 0; i < PQntuples(result); i++)
1580 const char *ruledef;
1582 /* Everything after "CREATE RULE" is echoed verbatim */
1583 ruledef = PQgetvalue(result, i, 1);
1586 printfPQExpBuffer(&buf, " %s", ruledef);
1587 printTableAddFooter(&cont, buf.data);
1593 else if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
1595 /* Footer information about a table */
1596 PGresult *result = NULL;
1600 if (tableinfo.hasindex)
1602 printfPQExpBuffer(&buf,
1603 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1604 if (pset.sversion >= 80200)
1605 appendPQExpBuffer(&buf, "i.indisvalid, ");
1607 appendPQExpBuffer(&buf, "true as indisvalid, ");
1608 appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
1609 if (pset.sversion >= 90000)
1610 appendPQExpBuffer(&buf,
1611 "pg_catalog.pg_get_constraintdef(con.oid, true), "
1612 "contype, condeferrable, condeferred");
1614 appendPQExpBuffer(&buf,
1615 "null AS constraintdef, null AS contype, "
1616 "false AS condeferrable, false AS condeferred");
1617 if (pset.sversion >= 80000)
1618 appendPQExpBuffer(&buf, ", c2.reltablespace");
1619 appendPQExpBuffer(&buf,
1620 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
1621 if (pset.sversion >= 90000)
1622 appendPQExpBuffer(&buf,
1623 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
1624 appendPQExpBuffer(&buf,
1625 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1626 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
1628 result = PSQLexec(buf.data, false);
1632 tuples = PQntuples(result);
1636 printTableAddFooter(&cont, _("Indexes:"));
1637 for (i = 0; i < tuples; i++)
1639 /* untranslated index name */
1640 printfPQExpBuffer(&buf, " \"%s\"",
1641 PQgetvalue(result, i, 0));
1643 /* If exclusion constraint, print the constraintdef */
1644 if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
1646 appendPQExpBuffer(&buf, " %s",
1647 PQgetvalue(result, i, 6));
1651 const char *indexdef;
1652 const char *usingpos;
1654 /* Label as primary key or unique (but not both) */
1655 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
1656 appendPQExpBuffer(&buf, " PRIMARY KEY,");
1657 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
1659 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
1660 appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
1662 appendPQExpBuffer(&buf, " UNIQUE,");
1665 /* Everything after "USING" is echoed verbatim */
1666 indexdef = PQgetvalue(result, i, 5);
1667 usingpos = strstr(indexdef, " USING ");
1669 indexdef = usingpos + 7;
1670 appendPQExpBuffer(&buf, " %s", indexdef);
1672 /* Need these for deferrable PK/UNIQUE indexes */
1673 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
1674 appendPQExpBuffer(&buf, " DEFERRABLE");
1676 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
1677 appendPQExpBuffer(&buf, " INITIALLY DEFERRED");
1680 /* Add these for all cases */
1681 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1682 appendPQExpBuffer(&buf, " CLUSTER");
1684 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1685 appendPQExpBuffer(&buf, " INVALID");
1687 printTableAddFooter(&cont, buf.data);
1689 /* Print tablespace of the index on the same line */
1690 if (pset.sversion >= 80000)
1691 add_tablespace_footer(&cont, 'i',
1692 atooid(PQgetvalue(result, i, 10)),
1699 /* print table (and column) check constraints */
1700 if (tableinfo.checks)
1702 printfPQExpBuffer(&buf,
1703 "SELECT r.conname, "
1704 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1705 "FROM pg_catalog.pg_constraint r\n"
1706 "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1",
1708 result = PSQLexec(buf.data, false);
1712 tuples = PQntuples(result);
1716 printTableAddFooter(&cont, _("Check constraints:"));
1717 for (i = 0; i < tuples; i++)
1719 /* untranslated contraint name and def */
1720 printfPQExpBuffer(&buf, " \"%s\" %s",
1721 PQgetvalue(result, i, 0),
1722 PQgetvalue(result, i, 1));
1724 printTableAddFooter(&cont, buf.data);
1730 /* print foreign-key constraints (there are none if no triggers) */
1731 if (tableinfo.hastriggers)
1733 printfPQExpBuffer(&buf,
1735 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n");
1736 if (pset.sversion >= 90100)
1737 appendPQExpBuffer(&buf, " ,convalidated\n");
1738 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_constraint r\n"
1739 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1",
1741 result = PSQLexec(buf.data, false);
1745 tuples = PQntuples(result);
1749 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1750 for (i = 0; i < tuples; i++)
1752 /* untranslated constraint name and def */
1753 printfPQExpBuffer(&buf, " \"%s\" %s",
1754 PQgetvalue(result, i, 0),
1755 PQgetvalue(result, i, 1));
1757 if (strcmp(PQgetvalue(result, i, 2), "f") == 0)
1758 appendPQExpBuffer(&buf, " NOT VALID");
1760 printTableAddFooter(&cont, buf.data);
1766 /* print incoming foreign-key references (none if no triggers) */
1767 if (tableinfo.hastriggers)
1769 printfPQExpBuffer(&buf,
1770 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1771 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1772 "FROM pg_catalog.pg_constraint c\n"
1773 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
1775 result = PSQLexec(buf.data, false);
1779 tuples = PQntuples(result);
1783 printTableAddFooter(&cont, _("Referenced by:"));
1784 for (i = 0; i < tuples; i++)
1786 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
1787 PQgetvalue(result, i, 1),
1788 PQgetvalue(result, i, 0),
1789 PQgetvalue(result, i, 2));
1791 printTableAddFooter(&cont, buf.data);
1798 if (tableinfo.hasrules)
1800 if (pset.sversion >= 80300)
1802 printfPQExpBuffer(&buf,
1803 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1805 "FROM pg_catalog.pg_rewrite r\n"
1806 "WHERE r.ev_class = '%s' ORDER BY 1",
1811 printfPQExpBuffer(&buf,
1812 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1813 "'O'::char AS ev_enabled\n"
1814 "FROM pg_catalog.pg_rewrite r\n"
1815 "WHERE r.ev_class = '%s' ORDER BY 1",
1818 result = PSQLexec(buf.data, false);
1822 tuples = PQntuples(result);
1829 for (category = 0; category < 4; category++)
1831 have_heading = false;
1833 for (i = 0; i < tuples; i++)
1835 const char *ruledef;
1836 bool list_rule = false;
1841 if (*PQgetvalue(result, i, 2) == 'O')
1845 if (*PQgetvalue(result, i, 2) == 'D')
1849 if (*PQgetvalue(result, i, 2) == 'A')
1853 if (*PQgetvalue(result, i, 2) == 'R')
1865 printfPQExpBuffer(&buf, _("Rules:"));
1868 printfPQExpBuffer(&buf, _("Disabled rules:"));
1871 printfPQExpBuffer(&buf, _("Rules firing always:"));
1874 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1877 printTableAddFooter(&cont, buf.data);
1878 have_heading = true;
1881 /* Everything after "CREATE RULE" is echoed verbatim */
1882 ruledef = PQgetvalue(result, i, 1);
1884 printfPQExpBuffer(&buf, " %s", ruledef);
1885 printTableAddFooter(&cont, buf.data);
1894 * Print triggers next, if any (but only user-defined triggers). This
1895 * could apply to either a table or a view.
1897 if (tableinfo.hastriggers)
1902 printfPQExpBuffer(&buf,
1904 "pg_catalog.pg_get_triggerdef(t.oid%s), "
1906 "FROM pg_catalog.pg_trigger t\n"
1907 "WHERE t.tgrelid = '%s' AND ",
1908 (pset.sversion >= 90000 ? ", true" : ""),
1910 if (pset.sversion >= 90000)
1911 appendPQExpBuffer(&buf, "NOT t.tgisinternal");
1912 else if (pset.sversion >= 80300)
1913 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
1915 appendPQExpBuffer(&buf,
1916 "(NOT tgisconstraint "
1918 " (SELECT 1 FROM pg_catalog.pg_depend d "
1919 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
1920 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
1921 appendPQExpBuffer(&buf, "\nORDER BY 1");
1923 result = PSQLexec(buf.data, false);
1927 tuples = PQntuples(result);
1935 * split the output into 4 different categories. Enabled
1936 * triggers, disabled triggers and the two special ALWAYS and
1937 * REPLICA configurations.
1939 for (category = 0; category < 4; category++)
1941 have_heading = false;
1942 for (i = 0; i < tuples; i++)
1946 const char *usingpos;
1947 const char *tgenabled;
1950 * Check if this trigger falls into the current
1953 tgenabled = PQgetvalue(result, i, 2);
1954 list_trigger = false;
1958 if (*tgenabled == 'O' || *tgenabled == 't')
1959 list_trigger = true;
1962 if (*tgenabled == 'D' || *tgenabled == 'f')
1963 list_trigger = true;
1966 if (*tgenabled == 'A')
1967 list_trigger = true;
1970 if (*tgenabled == 'R')
1971 list_trigger = true;
1974 if (list_trigger == false)
1977 /* Print the category heading once */
1978 if (have_heading == false)
1983 printfPQExpBuffer(&buf, _("Triggers:"));
1986 printfPQExpBuffer(&buf, _("Disabled triggers:"));
1989 printfPQExpBuffer(&buf, _("Triggers firing always:"));
1992 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
1996 printTableAddFooter(&cont, buf.data);
1997 have_heading = true;
2000 /* Everything after "TRIGGER" is echoed verbatim */
2001 tgdef = PQgetvalue(result, i, 1);
2002 usingpos = strstr(tgdef, " TRIGGER ");
2004 tgdef = usingpos + 9;
2006 printfPQExpBuffer(&buf, " %s", tgdef);
2007 printTableAddFooter(&cont, buf.data);
2015 * Finish printing the footer information about a table.
2017 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'f')
2022 /* print foreign server name */
2023 if (tableinfo.relkind == 'f')
2025 /* Footer information about foreign table */
2026 printfPQExpBuffer(&buf,
2027 "SELECT s.srvname\n"
2028 "FROM pg_catalog.pg_foreign_table f,\n"
2029 " pg_catalog.pg_foreign_server s\n"
2030 "WHERE f.ftrelid = %s AND s.oid = f.ftserver",
2032 result = PSQLexec(buf.data, false);
2035 else if (PQntuples(result) != 1)
2041 printfPQExpBuffer(&buf, "Server: %s",
2042 PQgetvalue(result, 0, 0));
2043 printTableAddFooter(&cont, buf.data);
2047 /* print inherited tables */
2048 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);
2050 result = PSQLexec(buf.data, false);
2054 tuples = PQntuples(result);
2056 for (i = 0; i < tuples; i++)
2058 const char *s = _("Inherits");
2061 printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
2063 printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
2065 appendPQExpBuffer(&buf, ",");
2067 printTableAddFooter(&cont, buf.data);
2071 /* print child tables */
2072 if (pset.sversion >= 80300)
2073 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);
2075 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);
2077 result = PSQLexec(buf.data, false);
2081 tuples = PQntuples(result);
2085 /* print the number of child tables, if any */
2088 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2089 printTableAddFooter(&cont, buf.data);
2094 /* display the list of child tables */
2095 const char *ct = _("Child tables");
2097 for (i = 0; i < tuples; i++)
2100 printfPQExpBuffer(&buf, "%s: %s",
2101 ct, PQgetvalue(result, i, 0));
2103 printfPQExpBuffer(&buf, "%*s %s",
2104 (int) strlen(ct), "",
2105 PQgetvalue(result, i, 0));
2107 appendPQExpBuffer(&buf, ",");
2109 printTableAddFooter(&cont, buf.data);
2115 if (tableinfo.reloftype)
2117 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2118 printTableAddFooter(&cont, buf.data);
2121 /* OIDs and options */
2124 const char *s = _("Has OIDs");
2126 printfPQExpBuffer(&buf, "%s: %s", s,
2127 (tableinfo.hasoids ? _("yes") : _("no")));
2128 printTableAddFooter(&cont, buf.data);
2130 /* print reloptions */
2131 if (pset.sversion >= 80200)
2133 if (tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
2135 const char *t = _("Options");
2137 printfPQExpBuffer(&buf, "%s: %s", t,
2138 tableinfo.reloptions);
2139 printTableAddFooter(&cont, buf.data);
2144 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
2148 printTable(&cont, pset.queryFout, pset.logfile);
2149 printTableCleanup(&cont);
2156 if (printTableInitialized)
2157 printTableCleanup(&cont);
2158 termPQExpBuffer(&buf);
2159 termPQExpBuffer(&title);
2160 termPQExpBuffer(&tmpbuf);
2164 for (ptr = seq_values; *ptr; ptr++)
2171 for (ptr = modifiers; *ptr; ptr++)
2186 * Add a tablespace description to a footer. If 'newline' is true, it is added
2187 * in a new line; otherwise it's appended to the current value of the last
2191 add_tablespace_footer(printTableContent *const cont, char relkind,
2192 Oid tablespace, const bool newline)
2194 /* relkinds for which we support tablespaces */
2195 if (relkind == 'r' || relkind == 'i')
2198 * We ignore the database default tablespace so that users not using
2199 * tablespaces don't need to know about them. This case also covers
2200 * pre-8.0 servers, for which tablespace will always be 0.
2202 if (tablespace != 0)
2204 PGresult *result = NULL;
2205 PQExpBufferData buf;
2207 initPQExpBuffer(&buf);
2208 printfPQExpBuffer(&buf,
2209 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
2210 "WHERE oid = '%u'", tablespace);
2211 result = PSQLexec(buf.data, false);
2214 /* Should always be the case, but.... */
2215 if (PQntuples(result) > 0)
2219 /* Add the tablespace as a new footer */
2220 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
2221 PQgetvalue(result, 0, 0));
2222 printTableAddFooter(cont, buf.data);
2226 /* Append the tablespace to the latest footer */
2227 printfPQExpBuffer(&buf, "%s", cont->footer->data);
2230 * translator: before this string there's an index
2231 * description like '"foo_pkey" PRIMARY KEY, btree (a)'
2233 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
2234 PQgetvalue(result, 0, 0));
2235 printTableSetFooter(cont, buf.data);
2239 termPQExpBuffer(&buf);
2247 * Describes roles. Any schema portion of the pattern is ignored.
2250 describeRoles(const char *pattern, bool verbose)
2252 PQExpBufferData buf;
2254 printTableContent cont;
2255 printTableOpt myopt = pset.popt.topt;
2260 const char align = 'l';
2263 initPQExpBuffer(&buf);
2265 if (pset.sversion >= 80100)
2267 printfPQExpBuffer(&buf,
2268 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2269 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2270 " r.rolconnlimit,\n"
2271 " ARRAY(SELECT b.rolname\n"
2272 " FROM pg_catalog.pg_auth_members m\n"
2273 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2274 " WHERE m.member = r.oid) as memberof");
2276 if (verbose && pset.sversion >= 80200)
2278 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2281 if (pset.sversion >= 90100)
2283 appendPQExpBufferStr(&buf,"\n, r.rolreplication");
2286 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2288 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2289 NULL, "r.rolname", NULL, NULL);
2293 printfPQExpBuffer(&buf,
2294 "SELECT u.usename AS rolname,\n"
2295 " u.usesuper AS rolsuper,\n"
2296 " true AS rolinherit, false AS rolcreaterole,\n"
2297 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2298 " -1 AS rolconnlimit,\n"
2299 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2300 "\nFROM pg_catalog.pg_user u\n");
2302 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2303 NULL, "u.usename", NULL, NULL);
2306 appendPQExpBuffer(&buf, "ORDER BY 1;");
2308 res = PSQLexec(buf.data, false);
2312 nrows = PQntuples(res);
2313 attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
2315 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2317 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2318 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2319 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2321 if (verbose && pset.sversion >= 80200)
2322 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2324 for (i = 0; i < nrows; i++)
2326 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
2328 resetPQExpBuffer(&buf);
2329 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2330 add_role_attribute(&buf, _("Superuser"));
2332 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2333 add_role_attribute(&buf, _("No inheritance"));
2335 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2336 add_role_attribute(&buf, _("Create role"));
2338 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2339 add_role_attribute(&buf, _("Create DB"));
2341 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2342 add_role_attribute(&buf, _("Cannot login"));
2344 if (pset.sversion >= 90100)
2345 if (strcmp(PQgetvalue(res, i, 8), "t") == 0)
2346 add_role_attribute(&buf, _("Replication"));
2348 conns = atoi(PQgetvalue(res, i, 6));
2352 appendPQExpBufferStr(&buf, "\n");
2355 appendPQExpBuffer(&buf, _("No connections"));
2357 appendPQExpBuffer(&buf, ngettext("%d connection",
2363 attr[i] = pg_strdup(buf.data);
2365 printTableAddCell(&cont, attr[i], false, false);
2367 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
2369 if (verbose && pset.sversion >= 80200)
2370 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
2372 termPQExpBuffer(&buf);
2374 printTable(&cont, pset.queryFout, pset.logfile);
2375 printTableCleanup(&cont);
2377 for (i = 0; i < nrows; i++)
2386 add_role_attribute(PQExpBuffer buf, const char *const str)
2389 appendPQExpBufferStr(buf, ", ");
2391 appendPQExpBufferStr(buf, str);
2398 listDbRoleSettings(const char *pattern, const char *pattern2)
2400 PQExpBufferData buf;
2402 printQueryOpt myopt = pset.popt;
2404 initPQExpBuffer(&buf);
2406 if (pset.sversion >= 90000)
2410 printfPQExpBuffer(&buf, "SELECT rolname AS role, datname AS database,\n"
2411 "pg_catalog.array_to_string(setconfig, E'\\n') AS settings\n"
2412 "FROM pg_db_role_setting AS s\n"
2413 "LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2414 "LEFT JOIN pg_roles ON pg_roles.oid = setrole\n");
2415 havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2416 NULL, "pg_roles.rolname", NULL, NULL);
2417 processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2418 NULL, "pg_database.datname", NULL, NULL);
2419 appendPQExpBufferStr(&buf, "ORDER BY role, database");
2423 fprintf(pset.queryFout,
2424 _("No per-database role settings support in this server version.\n"));
2428 res = PSQLexec(buf.data, false);
2432 if (PQntuples(res) == 0 && !pset.quiet)
2435 fprintf(pset.queryFout, _("No matching settings found.\n"));
2437 fprintf(pset.queryFout, _("No settings found.\n"));
2441 myopt.nullPrint = NULL;
2442 myopt.title = _("List of settings");
2443 myopt.translate_header = true;
2445 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2449 resetPQExpBuffer(&buf);
2457 * handler for \dt, \di, etc.
2459 * tabtypes is an array of characters, specifying what info is desired:
2464 * E - foreign table (Note: different from 'f', the relkind value)
2465 * (any order of the above is fine)
2466 * If tabtypes is empty, we default to \dtvsE.
2469 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2471 bool showTables = strchr(tabtypes, 't') != NULL;
2472 bool showIndexes = strchr(tabtypes, 'i') != NULL;
2473 bool showViews = strchr(tabtypes, 'v') != NULL;
2474 bool showSeq = strchr(tabtypes, 's') != NULL;
2475 bool showForeign = strchr(tabtypes, 'E') != NULL;
2477 PQExpBufferData buf;
2479 printQueryOpt myopt = pset.popt;
2480 static const bool translate_columns[] = {false, false, true, false, false, false, false};
2482 if (!(showTables || showIndexes || showViews || showSeq || showForeign))
2483 showTables = showViews = showSeq = showForeign = true;
2485 initPQExpBuffer(&buf);
2488 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2489 * for backwards compatibility.
2491 printfPQExpBuffer(&buf,
2492 "SELECT n.nspname as \"%s\",\n"
2493 " c.relname as \"%s\",\n"
2494 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' WHEN 'f' THEN '%s' END as \"%s\",\n"
2495 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2496 gettext_noop("Schema"),
2497 gettext_noop("Name"),
2498 gettext_noop("table"),
2499 gettext_noop("view"),
2500 gettext_noop("index"),
2501 gettext_noop("sequence"),
2502 gettext_noop("special"),
2503 gettext_noop("foreign table"),
2504 gettext_noop("Type"),
2505 gettext_noop("Owner"));
2508 appendPQExpBuffer(&buf,
2509 ",\n c2.relname as \"%s\"",
2510 gettext_noop("Table"));
2512 if (verbose && pset.sversion >= 80100)
2513 appendPQExpBuffer(&buf,
2514 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2515 gettext_noop("Size"));
2517 appendPQExpBuffer(&buf,
2518 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2519 gettext_noop("Description"));
2521 appendPQExpBuffer(&buf,
2522 "\nFROM pg_catalog.pg_class c"
2523 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2525 appendPQExpBuffer(&buf,
2526 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2527 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2529 appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
2531 appendPQExpBuffer(&buf, "'r',");
2533 appendPQExpBuffer(&buf, "'v',");
2535 appendPQExpBuffer(&buf, "'i',");
2537 appendPQExpBuffer(&buf, "'S',");
2538 if (showSystem || pattern)
2539 appendPQExpBuffer(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2542 appendPQExpBuffer(&buf, "'f',");
2544 appendPQExpBuffer(&buf, "''"); /* dummy */
2545 appendPQExpBuffer(&buf, ")\n");
2547 if (!showSystem && !pattern)
2548 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2549 " AND n.nspname <> 'information_schema'\n");
2552 * TOAST objects are suppressed unconditionally. Since we don't provide
2553 * any way to select relkind 't' above, we would never show toast tables
2554 * in any case; it seems a bit confusing to allow their indexes to be
2555 * shown. Use plain \d if you really need to look at a TOAST table/index.
2557 appendPQExpBuffer(&buf, " AND n.nspname !~ '^pg_toast'\n");
2559 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2560 "n.nspname", "c.relname", NULL,
2561 "pg_catalog.pg_table_is_visible(c.oid)");
2563 appendPQExpBuffer(&buf, "ORDER BY 1,2;");
2565 res = PSQLexec(buf.data, false);
2566 termPQExpBuffer(&buf);
2570 if (PQntuples(res) == 0 && !pset.quiet)
2573 fprintf(pset.queryFout, _("No matching relations found.\n"));
2575 fprintf(pset.queryFout, _("No relations found.\n"));
2579 myopt.nullPrint = NULL;
2580 myopt.title = _("List of relations");
2581 myopt.translate_header = true;
2582 myopt.translate_columns = translate_columns;
2584 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2595 * Describes languages.
2598 listLanguages(const char *pattern, bool verbose, bool showSystem)
2600 PQExpBufferData buf;
2602 printQueryOpt myopt = pset.popt;
2604 initPQExpBuffer(&buf);
2606 printfPQExpBuffer(&buf,
2607 "SELECT l.lanname AS \"%s\",\n",
2608 gettext_noop("Name"));
2609 if (pset.sversion >= 80300)
2610 appendPQExpBuffer(&buf,
2611 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n",
2612 gettext_noop("Owner"));
2614 appendPQExpBuffer(&buf,
2615 " l.lanpltrusted AS \"%s\"",
2616 gettext_noop("Trusted"));
2620 appendPQExpBuffer(&buf,
2621 ",\n NOT l.lanispl AS \"%s\",\n"
2622 " l.lanplcallfoid::regprocedure AS \"%s\",\n"
2623 " l.lanvalidator::regprocedure AS \"%s\",\n ",
2624 gettext_noop("Internal Language"),
2625 gettext_noop("Call Handler"),
2626 gettext_noop("Validator"));
2627 if (pset.sversion >= 90000)
2628 appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
2629 gettext_noop("Inline Handler"));
2630 printACLColumn(&buf, "l.lanacl");
2633 appendPQExpBuffer(&buf,
2634 "\nFROM pg_catalog.pg_language l\n");
2636 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2637 NULL, "l.lanname", NULL, NULL);
2639 if (!showSystem && !pattern)
2640 appendPQExpBuffer(&buf, "WHERE lanplcallfoid != 0\n");
2642 appendPQExpBuffer(&buf, "ORDER BY 1;");
2644 res = PSQLexec(buf.data, false);
2645 termPQExpBuffer(&buf);
2649 myopt.nullPrint = NULL;
2650 myopt.title = _("List of languages");
2651 myopt.translate_header = true;
2653 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2663 * Describes domains.
2666 listDomains(const char *pattern, bool showSystem)
2668 PQExpBufferData buf;
2670 printQueryOpt myopt = pset.popt;
2672 initPQExpBuffer(&buf);
2674 printfPQExpBuffer(&buf,
2675 "SELECT n.nspname as \"%s\",\n"
2676 " t.typname as \"%s\",\n"
2677 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2678 " CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
2679 " WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
2680 " WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
2683 " pg_catalog.array_to_string(ARRAY(\n"
2684 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2685 " ), ' ') as \"%s\"\n"
2686 "FROM pg_catalog.pg_type t\n"
2687 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
2688 "WHERE t.typtype = 'd'\n",
2689 gettext_noop("Schema"),
2690 gettext_noop("Name"),
2691 gettext_noop("Type"),
2692 gettext_noop("Modifier"),
2693 gettext_noop("Check"));
2695 if (!showSystem && !pattern)
2696 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2697 " AND n.nspname <> 'information_schema'\n");
2699 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2700 "n.nspname", "t.typname", NULL,
2701 "pg_catalog.pg_type_is_visible(t.oid)");
2703 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2705 res = PSQLexec(buf.data, false);
2706 termPQExpBuffer(&buf);
2710 myopt.nullPrint = NULL;
2711 myopt.title = _("List of domains");
2712 myopt.translate_header = true;
2714 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2723 * Describes conversions.
2726 listConversions(const char *pattern, bool showSystem)
2728 PQExpBufferData buf;
2730 printQueryOpt myopt = pset.popt;
2731 static const bool translate_columns[] = {false, false, false, false, true};
2733 initPQExpBuffer(&buf);
2735 printfPQExpBuffer(&buf,
2736 "SELECT n.nspname AS \"%s\",\n"
2737 " c.conname AS \"%s\",\n"
2738 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2739 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2740 " CASE WHEN c.condefault THEN '%s'\n"
2741 " ELSE '%s' END AS \"%s\"\n"
2742 "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
2743 "WHERE n.oid = c.connamespace\n",
2744 gettext_noop("Schema"),
2745 gettext_noop("Name"),
2746 gettext_noop("Source"),
2747 gettext_noop("Destination"),
2748 gettext_noop("yes"), gettext_noop("no"),
2749 gettext_noop("Default?"));
2751 if (!showSystem && !pattern)
2752 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2753 " AND n.nspname <> 'information_schema'\n");
2755 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2756 "n.nspname", "c.conname", NULL,
2757 "pg_catalog.pg_conversion_is_visible(c.oid)");
2759 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2761 res = PSQLexec(buf.data, false);
2762 termPQExpBuffer(&buf);
2766 myopt.nullPrint = NULL;
2767 myopt.title = _("List of conversions");
2768 myopt.translate_header = true;
2769 myopt.translate_columns = translate_columns;
2771 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2783 listCasts(const char *pattern)
2785 PQExpBufferData buf;
2787 printQueryOpt myopt = pset.popt;
2788 static const bool translate_columns[] = {false, false, false, true};
2790 initPQExpBuffer(&buf);
2793 * We need a left join to pg_proc for binary casts; the others are just
2794 * paranoia. Also note that we don't attempt to localize '(binary
2795 * coercible)', because there's too much risk of gettext translating a
2796 * function name that happens to match some string in the PO database.
2798 printfPQExpBuffer(&buf,
2799 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2800 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2801 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
2804 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2805 " WHEN c.castcontext = 'a' THEN '%s'\n"
2808 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2809 " ON c.castfunc = p.oid\n"
2810 " LEFT JOIN pg_catalog.pg_type ts\n"
2811 " ON c.castsource = ts.oid\n"
2812 " LEFT JOIN pg_catalog.pg_namespace ns\n"
2813 " ON ns.oid = ts.typnamespace\n"
2814 " LEFT JOIN pg_catalog.pg_type tt\n"
2815 " ON c.casttarget = tt.oid\n"
2816 " LEFT JOIN pg_catalog.pg_namespace nt\n"
2817 " ON nt.oid = tt.typnamespace\n"
2819 gettext_noop("Source type"),
2820 gettext_noop("Target type"),
2821 gettext_noop("Function"),
2822 gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2823 gettext_noop("Implicit?"));
2826 * Match name pattern against either internal or external name of either
2827 * castsource or casttarget
2829 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2830 "ns.nspname", "ts.typname",
2831 "pg_catalog.format_type(ts.oid, NULL)",
2832 "pg_catalog.pg_type_is_visible(ts.oid)");
2834 appendPQExpBuffer(&buf, ") OR (true");
2836 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2837 "nt.nspname", "tt.typname",
2838 "pg_catalog.format_type(tt.oid, NULL)",
2839 "pg_catalog.pg_type_is_visible(tt.oid)");
2841 appendPQExpBuffer(&buf, ")\nORDER BY 1, 2;");
2843 res = PSQLexec(buf.data, false);
2844 termPQExpBuffer(&buf);
2848 myopt.nullPrint = NULL;
2849 myopt.title = _("List of casts");
2850 myopt.translate_header = true;
2851 myopt.translate_columns = translate_columns;
2853 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2862 * Describes schemas (namespaces)
2865 listSchemas(const char *pattern, bool verbose, bool showSystem)
2867 PQExpBufferData buf;
2869 printQueryOpt myopt = pset.popt;
2871 initPQExpBuffer(&buf);
2872 printfPQExpBuffer(&buf,
2873 "SELECT n.nspname AS \"%s\",\n"
2874 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2875 gettext_noop("Name"),
2876 gettext_noop("Owner"));
2880 appendPQExpBuffer(&buf, ",\n ");
2881 printACLColumn(&buf, "n.nspacl");
2882 appendPQExpBuffer(&buf,
2883 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
2884 gettext_noop("Description"));
2887 appendPQExpBuffer(&buf,
2888 "\nFROM pg_catalog.pg_namespace n\n");
2890 if (!showSystem && !pattern)
2891 appendPQExpBuffer(&buf,
2892 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
2894 processSQLNamePattern(pset.db, &buf, pattern,
2895 !showSystem && !pattern, false,
2896 NULL, "n.nspname", NULL,
2899 appendPQExpBuffer(&buf, "ORDER BY 1;");
2901 res = PSQLexec(buf.data, false);
2902 termPQExpBuffer(&buf);
2906 myopt.nullPrint = NULL;
2907 myopt.title = _("List of schemas");
2908 myopt.translate_header = true;
2910 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2919 * list text search parsers
2922 listTSParsers(const char *pattern, bool verbose)
2924 PQExpBufferData buf;
2926 printQueryOpt myopt = pset.popt;
2928 if (pset.sversion < 80300)
2930 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2931 pset.sversion / 10000, (pset.sversion / 100) % 100);
2936 return listTSParsersVerbose(pattern);
2938 initPQExpBuffer(&buf);
2940 printfPQExpBuffer(&buf,
2942 " n.nspname as \"%s\",\n"
2943 " p.prsname as \"%s\",\n"
2944 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2945 "FROM pg_catalog.pg_ts_parser p \n"
2946 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2947 gettext_noop("Schema"),
2948 gettext_noop("Name"),
2949 gettext_noop("Description")
2952 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2953 "n.nspname", "p.prsname", NULL,
2954 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2956 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2958 res = PSQLexec(buf.data, false);
2959 termPQExpBuffer(&buf);
2963 myopt.nullPrint = NULL;
2964 myopt.title = _("List of text search parsers");
2965 myopt.translate_header = true;
2967 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2974 * full description of parsers
2977 listTSParsersVerbose(const char *pattern)
2979 PQExpBufferData buf;
2983 initPQExpBuffer(&buf);
2985 printfPQExpBuffer(&buf,
2989 "FROM pg_catalog.pg_ts_parser p\n"
2990 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2993 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2994 "n.nspname", "p.prsname", NULL,
2995 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2997 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2999 res = PSQLexec(buf.data, false);
3000 termPQExpBuffer(&buf);
3004 if (PQntuples(res) == 0)
3007 fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
3013 for (i = 0; i < PQntuples(res); i++)
3016 const char *nspname = NULL;
3017 const char *prsname;
3019 oid = PQgetvalue(res, i, 0);
3020 if (!PQgetisnull(res, i, 1))
3021 nspname = PQgetvalue(res, i, 1);
3022 prsname = PQgetvalue(res, i, 2);
3024 if (!describeOneTSParser(oid, nspname, prsname))
3042 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
3044 PQExpBufferData buf;
3047 printQueryOpt myopt = pset.popt;
3048 static const bool translate_columns[] = {true, false, false};
3050 initPQExpBuffer(&buf);
3052 printfPQExpBuffer(&buf,
3053 "SELECT '%s' AS \"%s\", \n"
3054 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
3055 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
3056 " FROM pg_catalog.pg_ts_parser p \n"
3057 " WHERE p.oid = '%s' \n"
3060 " p.prstoken::pg_catalog.regproc, \n"
3061 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
3062 " FROM pg_catalog.pg_ts_parser p \n"
3063 " WHERE p.oid = '%s' \n"
3066 " p.prsend::pg_catalog.regproc, \n"
3067 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
3068 " FROM pg_catalog.pg_ts_parser p \n"
3069 " WHERE p.oid = '%s' \n"
3072 " p.prsheadline::pg_catalog.regproc, \n"
3073 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
3074 " FROM pg_catalog.pg_ts_parser p \n"
3075 " WHERE p.oid = '%s' \n"
3078 " p.prslextype::pg_catalog.regproc, \n"
3079 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
3080 " FROM pg_catalog.pg_ts_parser p \n"
3081 " WHERE p.oid = '%s' \n",
3082 gettext_noop("Start parse"),
3083 gettext_noop("Method"),
3084 gettext_noop("Function"),
3085 gettext_noop("Description"),
3087 gettext_noop("Get next token"),
3089 gettext_noop("End parse"),
3091 gettext_noop("Get headline"),
3093 gettext_noop("Get token types"),
3096 res = PSQLexec(buf.data, false);
3097 termPQExpBuffer(&buf);
3101 myopt.nullPrint = NULL;
3103 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
3105 sprintf(title, _("Text search parser \"%s\""), prsname);
3106 myopt.title = title;
3107 myopt.footers = NULL;
3108 myopt.default_footer = false;
3109 myopt.translate_header = true;
3110 myopt.translate_columns = translate_columns;
3112 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3116 initPQExpBuffer(&buf);
3118 printfPQExpBuffer(&buf,
3119 "SELECT t.alias as \"%s\", \n"
3120 " t.description as \"%s\" \n"
3121 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
3123 gettext_noop("Token name"),
3124 gettext_noop("Description"),
3127 res = PSQLexec(buf.data, false);
3128 termPQExpBuffer(&buf);
3132 myopt.nullPrint = NULL;
3134 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
3136 sprintf(title, _("Token types for parser \"%s\""), prsname);
3137 myopt.title = title;
3138 myopt.footers = NULL;
3139 myopt.default_footer = true;
3140 myopt.translate_header = true;
3141 myopt.translate_columns = NULL;
3143 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3152 * list text search dictionaries
3155 listTSDictionaries(const char *pattern, bool verbose)
3157 PQExpBufferData buf;
3159 printQueryOpt myopt = pset.popt;
3161 if (pset.sversion < 80300)
3163 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3164 pset.sversion / 10000, (pset.sversion / 100) % 100);
3168 initPQExpBuffer(&buf);
3170 printfPQExpBuffer(&buf,
3172 " n.nspname as \"%s\",\n"
3173 " d.dictname as \"%s\",\n",
3174 gettext_noop("Schema"),
3175 gettext_noop("Name"));
3179 appendPQExpBuffer(&buf,
3180 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
3181 " pg_catalog.pg_ts_template t \n"
3182 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
3183 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
3184 " d.dictinitoption as \"%s\", \n",
3185 gettext_noop("Template"),
3186 gettext_noop("Init options"));
3189 appendPQExpBuffer(&buf,
3190 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
3191 gettext_noop("Description"));
3193 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
3194 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
3196 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3197 "n.nspname", "d.dictname", NULL,
3198 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
3200 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3202 res = PSQLexec(buf.data, false);
3203 termPQExpBuffer(&buf);
3207 myopt.nullPrint = NULL;
3208 myopt.title = _("List of text search dictionaries");
3209 myopt.translate_header = true;
3211 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3220 * list text search templates
3223 listTSTemplates(const char *pattern, bool verbose)
3225 PQExpBufferData buf;
3227 printQueryOpt myopt = pset.popt;
3229 if (pset.sversion < 80300)
3231 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3232 pset.sversion / 10000, (pset.sversion / 100) % 100);
3236 initPQExpBuffer(&buf);
3239 printfPQExpBuffer(&buf,
3241 " n.nspname AS \"%s\",\n"
3242 " t.tmplname AS \"%s\",\n"
3243 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
3244 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
3245 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3246 gettext_noop("Schema"),
3247 gettext_noop("Name"),
3248 gettext_noop("Init"),
3249 gettext_noop("Lexize"),
3250 gettext_noop("Description"));
3252 printfPQExpBuffer(&buf,
3254 " n.nspname AS \"%s\",\n"
3255 " t.tmplname AS \"%s\",\n"
3256 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
3257 gettext_noop("Schema"),
3258 gettext_noop("Name"),
3259 gettext_noop("Description"));
3261 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
3262 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
3264 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3265 "n.nspname", "t.tmplname", NULL,
3266 "pg_catalog.pg_ts_template_is_visible(t.oid)");
3268 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3270 res = PSQLexec(buf.data, false);
3271 termPQExpBuffer(&buf);
3275 myopt.nullPrint = NULL;
3276 myopt.title = _("List of text search templates");
3277 myopt.translate_header = true;
3279 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3288 * list text search configurations
3291 listTSConfigs(const char *pattern, bool verbose)
3293 PQExpBufferData buf;
3295 printQueryOpt myopt = pset.popt;
3297 if (pset.sversion < 80300)
3299 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
3300 pset.sversion / 10000, (pset.sversion / 100) % 100);
3305 return listTSConfigsVerbose(pattern);
3307 initPQExpBuffer(&buf);
3309 printfPQExpBuffer(&buf,
3311 " n.nspname as \"%s\",\n"
3312 " c.cfgname as \"%s\",\n"
3313 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
3314 "FROM pg_catalog.pg_ts_config c\n"
3315 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
3316 gettext_noop("Schema"),
3317 gettext_noop("Name"),
3318 gettext_noop("Description")
3321 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3322 "n.nspname", "c.cfgname", NULL,
3323 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3325 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3327 res = PSQLexec(buf.data, false);
3328 termPQExpBuffer(&buf);
3332 myopt.nullPrint = NULL;
3333 myopt.title = _("List of text search configurations");
3334 myopt.translate_header = true;
3336 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3343 listTSConfigsVerbose(const char *pattern)
3345 PQExpBufferData buf;
3349 initPQExpBuffer(&buf);
3351 printfPQExpBuffer(&buf,
3352 "SELECT c.oid, c.cfgname,\n"
3355 " np.nspname as pnspname \n"
3356 "FROM pg_catalog.pg_ts_config c \n"
3357 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
3358 " pg_catalog.pg_ts_parser p \n"
3359 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
3360 "WHERE p.oid = c.cfgparser\n"
3363 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3364 "n.nspname", "c.cfgname", NULL,
3365 "pg_catalog.pg_ts_config_is_visible(c.oid)");
3367 appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
3369 res = PSQLexec(buf.data, false);
3370 termPQExpBuffer(&buf);
3374 if (PQntuples(res) == 0)
3377 fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
3383 for (i = 0; i < PQntuples(res); i++)
3386 const char *cfgname;
3387 const char *nspname = NULL;
3388 const char *prsname;
3389 const char *pnspname = NULL;
3391 oid = PQgetvalue(res, i, 0);
3392 cfgname = PQgetvalue(res, i, 1);
3393 if (!PQgetisnull(res, i, 2))
3394 nspname = PQgetvalue(res, i, 2);
3395 prsname = PQgetvalue(res, i, 3);
3396 if (!PQgetisnull(res, i, 4))
3397 pnspname = PQgetvalue(res, i, 4);
3399 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
3417 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3418 const char *pnspname, const char *prsname)
3420 PQExpBufferData buf,
3423 printQueryOpt myopt = pset.popt;
3425 initPQExpBuffer(&buf);
3427 printfPQExpBuffer(&buf,
3429 " ( SELECT t.alias FROM \n"
3430 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3431 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3432 " pg_catalog.btrim( \n"
3433 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3434 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3435 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3436 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3437 " ) :: pg_catalog.text , \n"
3438 " '{}') AS \"%s\" \n"
3439 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3440 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3441 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3443 gettext_noop("Token"),
3444 gettext_noop("Dictionaries"),
3447 res = PSQLexec(buf.data, false);
3448 termPQExpBuffer(&buf);
3452 initPQExpBuffer(&title);
3455 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3458 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3462 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3465 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3468 myopt.nullPrint = NULL;
3469 myopt.title = title.data;
3470 myopt.footers = NULL;
3471 myopt.default_footer = false;
3472 myopt.translate_header = true;
3474 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3476 termPQExpBuffer(&title);
3486 * Describes foreign-data wrappers
3489 listForeignDataWrappers(const char *pattern, bool verbose)
3491 PQExpBufferData buf;
3493 printQueryOpt myopt = pset.popt;
3495 if (pset.sversion < 80400)
3497 fprintf(stderr, _("The server (version %d.%d) does not support foreign-data wrappers.\n"),
3498 pset.sversion / 10000, (pset.sversion / 100) % 100);
3502 initPQExpBuffer(&buf);
3503 printfPQExpBuffer(&buf,
3504 "SELECT fdwname AS \"%s\",\n"
3505 " pg_catalog.pg_get_userbyid(fdwowner) AS \"%s\",\n"
3506 " fdwvalidator::pg_catalog.regproc AS \"%s\"",
3507 gettext_noop("Name"),
3508 gettext_noop("Owner"),
3509 gettext_noop("Validator"));
3513 appendPQExpBuffer(&buf, ",\n ");
3514 printACLColumn(&buf, "fdwacl");
3515 appendPQExpBuffer(&buf,
3516 ",\n fdwoptions AS \"%s\"",
3517 gettext_noop("Options"));
3520 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper\n");
3522 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3523 NULL, "fdwname", NULL, NULL);
3525 appendPQExpBuffer(&buf, "ORDER BY 1;");
3527 res = PSQLexec(buf.data, false);
3528 termPQExpBuffer(&buf);
3532 myopt.nullPrint = NULL;
3533 myopt.title = _("List of foreign-data wrappers");
3534 myopt.translate_header = true;
3536 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3545 * Describes foreign servers.
3548 listForeignServers(const char *pattern, bool verbose)
3550 PQExpBufferData buf;
3552 printQueryOpt myopt = pset.popt;
3554 if (pset.sversion < 80400)
3556 fprintf(stderr, _("The server (version %d.%d) does not support foreign servers.\n"),
3557 pset.sversion / 10000, (pset.sversion / 100) % 100);
3561 initPQExpBuffer(&buf);
3562 printfPQExpBuffer(&buf,
3563 "SELECT s.srvname AS \"%s\",\n"
3564 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3565 " f.fdwname AS \"%s\"",
3566 gettext_noop("Name"),
3567 gettext_noop("Owner"),
3568 gettext_noop("Foreign-data wrapper"));
3572 appendPQExpBuffer(&buf, ",\n ");
3573 printACLColumn(&buf, "s.srvacl");
3574 appendPQExpBuffer(&buf,
3576 " s.srvtype AS \"%s\",\n"
3577 " s.srvversion AS \"%s\",\n"
3578 " s.srvoptions AS \"%s\"",
3579 gettext_noop("Type"),
3580 gettext_noop("Version"),
3581 gettext_noop("Options"));
3584 appendPQExpBuffer(&buf,
3585 "\nFROM pg_catalog.pg_foreign_server s\n"
3586 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3588 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3589 NULL, "s.srvname", NULL, NULL);
3591 appendPQExpBuffer(&buf, "ORDER BY 1;");
3593 res = PSQLexec(buf.data, false);
3594 termPQExpBuffer(&buf);
3598 myopt.nullPrint = NULL;
3599 myopt.title = _("List of foreign servers");
3600 myopt.translate_header = true;
3602 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3611 * Describes user mappings.
3614 listUserMappings(const char *pattern, bool verbose)
3616 PQExpBufferData buf;
3618 printQueryOpt myopt = pset.popt;
3620 if (pset.sversion < 80400)
3622 fprintf(stderr, _("The server (version %d.%d) does not support user mappings.\n"),
3623 pset.sversion / 10000, (pset.sversion / 100) % 100);
3627 initPQExpBuffer(&buf);
3628 printfPQExpBuffer(&buf,
3629 "SELECT um.srvname AS \"%s\",\n"
3630 " um.usename AS \"%s\"",
3631 gettext_noop("Server"),
3632 gettext_noop("User name"));
3635 appendPQExpBuffer(&buf,
3636 ",\n um.umoptions AS \"%s\"",
3637 gettext_noop("Options"));
3639 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
3641 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3642 NULL, "um.srvname", "um.usename", NULL);
3644 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3646 res = PSQLexec(buf.data, false);
3647 termPQExpBuffer(&buf);
3651 myopt.nullPrint = NULL;
3652 myopt.title = _("List of user mappings");
3653 myopt.translate_header = true;
3655 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3664 * Describes foreign tables.
3667 listForeignTables(const char *pattern, bool verbose)
3669 PQExpBufferData buf;
3671 printQueryOpt myopt = pset.popt;
3673 if (pset.sversion < 90100)
3675 fprintf(stderr, _("The server (version %d.%d) does not support foreign tables.\n"),
3676 pset.sversion / 10000, (pset.sversion / 100) % 100);
3680 initPQExpBuffer(&buf);
3681 printfPQExpBuffer(&buf,
3682 "SELECT n.nspname AS \"%s\",\n"
3683 " c.relname AS \"%s\",\n"
3684 " s.srvname AS \"%s\"",
3685 gettext_noop("Schema"),
3686 gettext_noop("Table"),
3687 gettext_noop("Server"));
3690 appendPQExpBuffer(&buf,
3691 ",\n ft.ftoptions AS \"%s\"",
3692 gettext_noop("Options"));
3694 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_table ft,");
3695 appendPQExpBuffer(&buf, "\n pg_catalog.pg_class c,");
3696 appendPQExpBuffer(&buf, "\n pg_catalog.pg_namespace n,");
3697 appendPQExpBuffer(&buf, "\n pg_catalog.pg_foreign_server s\n");
3698 appendPQExpBuffer(&buf, "\nWHERE c.oid = ft.ftrelid");
3699 appendPQExpBuffer(&buf, "\nAND s.oid = ft.ftserver\n");
3700 appendPQExpBuffer(&buf, "\nAND n.oid = c.relnamespace\n");
3702 processSQLNamePattern(pset.db, &buf, pattern, true, false,
3703 NULL, "n.nspname", "c.relname", NULL);
3705 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3707 res = PSQLexec(buf.data, false);
3708 termPQExpBuffer(&buf);
3712 myopt.nullPrint = NULL;
3713 myopt.title = _("List of foreign tables");
3714 myopt.translate_header = true;
3716 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3725 * Briefly describes installed extensions.
3728 listExtensions(const char *pattern)
3730 PQExpBufferData buf;
3732 printQueryOpt myopt = pset.popt;
3734 if (pset.sversion < 90100)
3736 fprintf(stderr, _("The server (version %d.%d) does not support extensions.\n"),
3737 pset.sversion / 10000, (pset.sversion / 100) % 100);
3741 initPQExpBuffer(&buf);
3742 printfPQExpBuffer(&buf,
3743 "SELECT e.extname AS \"%s\", "
3744 "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
3745 "FROM pg_catalog.pg_extension e "
3746 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
3747 "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
3748 "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
3749 gettext_noop("Name"),
3750 gettext_noop("Version"),
3751 gettext_noop("Schema"),
3752 gettext_noop("Description"));
3754 processSQLNamePattern(pset.db, &buf, pattern,
3756 NULL, "e.extname", NULL,
3759 appendPQExpBuffer(&buf, "ORDER BY 1;");
3761 res = PSQLexec(buf.data, false);
3762 termPQExpBuffer(&buf);
3766 myopt.nullPrint = NULL;
3767 myopt.title = _("List of installed extensions");
3768 myopt.translate_header = true;
3770 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3779 * List contents of installed extensions.
3782 listExtensionContents(const char *pattern)
3784 PQExpBufferData buf;
3788 if (pset.sversion < 90100)
3790 fprintf(stderr, _("The server (version %d.%d) does not support extensions.\n"),
3791 pset.sversion / 10000, (pset.sversion / 100) % 100);
3795 initPQExpBuffer(&buf);
3796 printfPQExpBuffer(&buf,
3797 "SELECT e.extname, e.oid\n"
3798 "FROM pg_catalog.pg_extension e\n");
3800 processSQLNamePattern(pset.db, &buf, pattern,
3802 NULL, "e.extname", NULL,
3805 appendPQExpBuffer(&buf, "ORDER BY 1;");
3807 res = PSQLexec(buf.data, false);
3808 termPQExpBuffer(&buf);
3812 if (PQntuples(res) == 0)
3817 fprintf(stderr, _("Did not find any extension named \"%s\".\n"),
3820 fprintf(stderr, _("Did not find any extensions.\n"));
3826 for (i = 0; i < PQntuples(res); i++)
3828 const char *extname;
3831 extname = PQgetvalue(res, i, 0);
3832 oid = PQgetvalue(res, i, 1);
3834 if (!listOneExtensionContents(extname, oid))
3851 listOneExtensionContents(const char *extname, const char *oid)
3853 PQExpBufferData buf;
3856 printQueryOpt myopt = pset.popt;
3858 initPQExpBuffer(&buf);
3859 printfPQExpBuffer(&buf,
3860 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
3861 "FROM pg_catalog.pg_depend\n"
3862 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
3864 gettext_noop("Object Description"),
3867 res = PSQLexec(buf.data, false);
3868 termPQExpBuffer(&buf);
3872 myopt.nullPrint = NULL;
3873 snprintf(title, sizeof(title), _("Objects in extension \"%s\""), extname);
3874 myopt.title = title;
3875 myopt.translate_header = true;
3877 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3886 * Helper function for consistently formatting ACL (privilege) columns.
3887 * The proper targetlist entry is appended to buf. Note lack of any
3888 * whitespace or comma decoration.
3891 printACLColumn(PQExpBuffer buf, const char *colname)
3893 if (pset.sversion >= 80100)
3894 appendPQExpBuffer(buf,
3895 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
3896 colname, gettext_noop("Access privileges"));
3898 appendPQExpBuffer(buf,
3899 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
3900 colname, gettext_noop("Access privileges"));