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-2008, PostgreSQL Global Development Group
11 * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.175 2008/07/03 15:59:55 petere Exp $
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);
43 * Handlers for various slash commands displaying some sort of list
44 * of things in the database.
46 * Note: try to format the queries to look nice in -E output.
52 * Takes an optional regexp to select particular aggregates
55 describeAggregates(const char *pattern, bool verbose)
59 printQueryOpt myopt = pset.popt;
61 initPQExpBuffer(&buf);
63 printfPQExpBuffer(&buf,
64 "SELECT n.nspname as \"%s\",\n"
65 " p.proname AS \"%s\",\n"
66 " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
67 gettext_noop("Schema"),
69 gettext_noop("Result data type"));
71 if (pset.sversion >= 80200)
72 appendPQExpBuffer(&buf,
73 " CASE WHEN p.pronargs = 0\n"
74 " THEN CAST('*' AS pg_catalog.text)\n"
76 " pg_catalog.array_to_string(ARRAY(\n"
78 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
80 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
83 gettext_noop("Argument data types"));
85 appendPQExpBuffer(&buf,
86 " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
87 gettext_noop("Argument data types"));
89 appendPQExpBuffer(&buf,
90 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
91 "FROM pg_catalog.pg_proc p\n"
92 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
94 gettext_noop("Description"));
96 processSQLNamePattern(pset.db, &buf, pattern, true, false,
97 "n.nspname", "p.proname", NULL,
98 "pg_catalog.pg_function_is_visible(p.oid)");
100 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
102 res = PSQLexec(buf.data, false);
103 termPQExpBuffer(&buf);
107 myopt.nullPrint = NULL;
108 myopt.title = _("List of aggregate functions");
109 myopt.trans_headers = true;
111 printQuery(res, &myopt, pset.queryFout, pset.logfile);
118 * Takes an optional regexp to select particular tablespaces
121 describeTablespaces(const char *pattern, bool verbose)
125 printQueryOpt myopt = pset.popt;
127 if (pset.sversion < 80000)
129 fprintf(stderr, _("The server (version %d.%d) does not support tablespaces.\n"),
130 pset.sversion / 10000, (pset.sversion / 100) % 100);
134 initPQExpBuffer(&buf);
136 printfPQExpBuffer(&buf,
137 "SELECT spcname AS \"%s\",\n"
138 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
139 " spclocation AS \"%s\"",
140 gettext_noop("Name"),
141 gettext_noop("Owner"),
142 gettext_noop("Location"));
145 appendPQExpBuffer(&buf,
146 ",\n spcacl AS \"%s\"",
147 gettext_noop("Access privileges"));
148 if (verbose && pset.sversion >= 80200)
149 appendPQExpBuffer(&buf,
150 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
151 gettext_noop("Description"));
153 appendPQExpBuffer(&buf,
154 "\nFROM pg_catalog.pg_tablespace\n");
156 processSQLNamePattern(pset.db, &buf, pattern, false, false,
157 NULL, "spcname", NULL,
160 appendPQExpBuffer(&buf, "ORDER BY 1;");
162 res = PSQLexec(buf.data, false);
163 termPQExpBuffer(&buf);
167 myopt.nullPrint = NULL;
168 myopt.title = _("List of tablespaces");
169 myopt.trans_headers = true;
171 printQuery(res, &myopt, pset.queryFout, pset.logfile);
179 * Takes an optional regexp to select particular functions
182 describeFunctions(const char *pattern, bool verbose)
186 printQueryOpt myopt = pset.popt;
188 initPQExpBuffer(&buf);
190 printfPQExpBuffer(&buf,
191 "SELECT n.nspname as \"%s\",\n"
192 " p.proname as \"%s\",\n"
193 " CASE WHEN p.proretset THEN 'setof ' ELSE '' END ||\n"
194 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n",
195 gettext_noop("Schema"),
196 gettext_noop("Name"),
197 gettext_noop("Result data type"));
199 if (pset.sversion >= 80100)
200 appendPQExpBuffer(&buf,
201 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
202 " pg_catalog.array_to_string(ARRAY(\n"
205 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
206 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
207 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
210 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
211 " ELSE p.proargnames[s.i] || ' ' \n"
213 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
215 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
218 " pg_catalog.array_to_string(ARRAY(\n"
221 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
222 " ELSE p.proargnames[s.i+1] || ' '\n"
224 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
226 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
229 gettext_noop("Argument data types"));
231 appendPQExpBuffer(&buf,
232 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\"",
233 gettext_noop("Argument data types"));
236 appendPQExpBuffer(&buf,
238 " WHEN p.provolatile = 'i' THEN 'immutable'\n"
239 " WHEN p.provolatile = 's' THEN 'stable'\n"
240 " WHEN p.provolatile = 'v' THEN 'volatile'\n"
242 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
243 " l.lanname as \"%s\",\n"
244 " p.prosrc as \"%s\",\n"
245 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
246 gettext_noop("Volatility"),
247 gettext_noop("Owner"),
248 gettext_noop("Language"),
249 gettext_noop("Source code"),
250 gettext_noop("Description"));
252 appendPQExpBuffer(&buf,
253 "\nFROM pg_catalog.pg_proc p"
254 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
257 appendPQExpBuffer(&buf,
258 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
261 * we skip in/out funcs by excluding functions that take or return cstring
263 appendPQExpBuffer(&buf,
264 "WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
265 " AND p.proargtypes[0] IS DISTINCT FROM 'pg_catalog.cstring'::pg_catalog.regtype\n"
266 " AND NOT p.proisagg\n");
268 processSQLNamePattern(pset.db, &buf, pattern, true, false,
269 "n.nspname", "p.proname", NULL,
270 "pg_catalog.pg_function_is_visible(p.oid)");
272 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
274 res = PSQLexec(buf.data, false);
275 termPQExpBuffer(&buf);
279 myopt.nullPrint = NULL;
280 myopt.title = _("List of functions");
281 myopt.trans_headers = true;
283 printQuery(res, &myopt, pset.queryFout, pset.logfile);
296 describeTypes(const char *pattern, bool verbose)
300 printQueryOpt myopt = pset.popt;
302 initPQExpBuffer(&buf);
304 printfPQExpBuffer(&buf,
305 "SELECT n.nspname as \"%s\",\n"
306 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
307 gettext_noop("Schema"),
308 gettext_noop("Name"));
310 appendPQExpBuffer(&buf,
311 " t.typname AS \"%s\",\n"
312 " CASE WHEN t.typrelid != 0\n"
313 " THEN CAST('tuple' AS pg_catalog.text)\n"
314 " WHEN t.typlen < 0\n"
315 " THEN CAST('var' AS pg_catalog.text)\n"
316 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
318 gettext_noop("Internal name"),
319 gettext_noop("Size"));
320 if (verbose && pset.sversion >= 80300)
321 appendPQExpBuffer(&buf,
322 " pg_catalog.array_to_string(\n"
324 " SELECT e.enumlabel\n"
325 " FROM pg_catalog.pg_enum e\n"
326 " WHERE e.enumtypid = t.oid\n"
331 gettext_noop("Elements"));
333 appendPQExpBuffer(&buf,
334 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
335 gettext_noop("Description"));
337 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
338 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
341 * do not include complex types (typrelid!=0) unless they are standalone
344 appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
345 appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
346 "WHERE c.oid = t.typrelid))\n");
348 * do not include array types (before 8.3 we have to use the assumption
349 * that their names start with underscore)
351 if (pset.sversion >= 80300)
352 appendPQExpBuffer(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
354 appendPQExpBuffer(&buf, " AND t.typname !~ '^_'\n");
356 /* Match name pattern against either internal or external name */
357 processSQLNamePattern(pset.db, &buf, pattern, true, false,
358 "n.nspname", "t.typname",
359 "pg_catalog.format_type(t.oid, NULL)",
360 "pg_catalog.pg_type_is_visible(t.oid)");
362 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
364 res = PSQLexec(buf.data, false);
365 termPQExpBuffer(&buf);
369 myopt.nullPrint = NULL;
370 myopt.title = _("List of data types");
371 myopt.trans_headers = true;
373 printQuery(res, &myopt, pset.queryFout, pset.logfile);
383 describeOperators(const char *pattern)
387 printQueryOpt myopt = pset.popt;
389 initPQExpBuffer(&buf);
391 printfPQExpBuffer(&buf,
392 "SELECT n.nspname as \"%s\",\n"
393 " o.oprname AS \"%s\",\n"
394 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
395 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
396 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
397 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
398 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
399 "FROM pg_catalog.pg_operator o\n"
400 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
401 gettext_noop("Schema"),
402 gettext_noop("Name"),
403 gettext_noop("Left arg type"),
404 gettext_noop("Right arg type"),
405 gettext_noop("Result type"),
406 gettext_noop("Description"));
408 processSQLNamePattern(pset.db, &buf, pattern, false, true,
409 "n.nspname", "o.oprname", NULL,
410 "pg_catalog.pg_operator_is_visible(o.oid)");
412 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
414 res = PSQLexec(buf.data, false);
415 termPQExpBuffer(&buf);
419 myopt.nullPrint = NULL;
420 myopt.title = _("List of operators");
421 myopt.trans_headers = true;
423 printQuery(res, &myopt, pset.queryFout, pset.logfile);
433 * for \l, \list, and -l switch
436 listAllDbs(bool verbose)
440 printQueryOpt myopt = pset.popt;
442 initPQExpBuffer(&buf);
444 printfPQExpBuffer(&buf,
445 "SELECT d.datname as \"%s\",\n"
446 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
447 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n"
448 " d.datacl as \"%s\"",
449 gettext_noop("Name"),
450 gettext_noop("Owner"),
451 gettext_noop("Encoding"),
452 gettext_noop("Access Privileges"));
453 if (verbose && pset.sversion >= 80200)
454 appendPQExpBuffer(&buf,
455 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
456 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
457 " ELSE 'No Access'\n"
459 gettext_noop("Size"));
460 if (verbose && pset.sversion >= 80000)
461 appendPQExpBuffer(&buf,
462 ",\n t.spcname as \"%s\"",
463 gettext_noop("Tablespace"));
464 if (verbose && pset.sversion >= 80200)
465 appendPQExpBuffer(&buf,
466 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
467 gettext_noop("Description"));
468 appendPQExpBuffer(&buf,
469 "\nFROM pg_catalog.pg_database d\n");
470 if (verbose && pset.sversion >= 80000)
471 appendPQExpBuffer(&buf,
472 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
473 appendPQExpBuffer(&buf, "ORDER BY 1;");
474 res = PSQLexec(buf.data, false);
475 termPQExpBuffer(&buf);
479 myopt.nullPrint = NULL;
480 myopt.title = _("List of databases");
481 myopt.trans_headers = true;
483 printQuery(res, &myopt, pset.queryFout, pset.logfile);
491 * List Tables Grant/Revoke Permissions
492 * \z (now also \dp -- perhaps more mnemonic)
495 permissionsList(const char *pattern)
499 printQueryOpt myopt = pset.popt;
500 static const bool trans_columns[] = {false, false, true, false};
502 initPQExpBuffer(&buf);
505 * we ignore indexes and toast tables since they have no meaningful rights
507 printfPQExpBuffer(&buf,
508 "SELECT n.nspname as \"%s\",\n"
509 " c.relname as \"%s\",\n"
510 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' END as \"%s\",\n",
511 gettext_noop("Schema"),
512 gettext_noop("Name"),
513 gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
514 gettext_noop("Type"));
516 if (pset.sversion >= 80100)
517 appendPQExpBuffer(&buf, " pg_catalog.array_to_string(c.relacl, E'\\n') as \"%s\"\n",
518 gettext_noop("Access privileges"));
520 appendPQExpBuffer(&buf, " pg_catalog.array_to_string(c.relacl, '\\n') as \"%s\"\n",
521 gettext_noop("Access privileges"));
523 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_class c\n"
524 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
525 "WHERE c.relkind IN ('r', 'v', 'S')\n");
528 * Unless a schema pattern is specified, we suppress system and temp
529 * tables, since they normally aren't very interesting from a permissions
530 * point of view. You can see 'em by explicit request though, eg with \z
533 processSQLNamePattern(pset.db, &buf, pattern, true, false,
534 "n.nspname", "c.relname", NULL,
535 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
537 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
539 res = PSQLexec(buf.data, false);
542 termPQExpBuffer(&buf);
546 myopt.nullPrint = NULL;
547 printfPQExpBuffer(&buf, _("Access privileges"));
548 myopt.title = buf.data;
549 myopt.trans_headers = true;
550 myopt.trans_columns = trans_columns;
552 printQuery(res, &myopt, pset.queryFout, pset.logfile);
554 termPQExpBuffer(&buf);
562 * Get object comments
566 * Note: This only lists things that actually have a description. For complete
567 * lists of things, there are other \d? commands.
570 objectDescription(const char *pattern)
574 printQueryOpt myopt = pset.popt;
575 static const bool trans_columns[] = {false, false, true, false};
577 initPQExpBuffer(&buf);
579 appendPQExpBuffer(&buf,
580 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
582 gettext_noop("Schema"),
583 gettext_noop("Name"),
584 gettext_noop("Object"),
585 gettext_noop("Description"));
587 /* Aggregate descriptions */
588 appendPQExpBuffer(&buf,
589 " SELECT p.oid as oid, p.tableoid as tableoid,\n"
590 " n.nspname as nspname,\n"
591 " CAST(p.proname AS pg_catalog.text) as name,"
592 " CAST('%s' AS pg_catalog.text) as object\n"
593 " FROM pg_catalog.pg_proc p\n"
594 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
595 " WHERE p.proisagg\n",
596 gettext_noop("aggregate"));
597 processSQLNamePattern(pset.db, &buf, pattern, true, false,
598 "n.nspname", "p.proname", NULL,
599 "pg_catalog.pg_function_is_visible(p.oid)");
601 /* Function descriptions (except in/outs for datatypes) */
602 appendPQExpBuffer(&buf,
604 " SELECT p.oid as oid, p.tableoid as tableoid,\n"
605 " n.nspname as nspname,\n"
606 " CAST(p.proname AS pg_catalog.text) as name,"
607 " CAST('%s' AS pg_catalog.text) as object\n"
608 " FROM pg_catalog.pg_proc p\n"
609 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
611 " WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
612 " AND (p.proargtypes[0] IS NULL\n"
613 " OR p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)\n"
614 " AND NOT p.proisagg\n",
615 gettext_noop("function"));
616 processSQLNamePattern(pset.db, &buf, pattern, true, false,
617 "n.nspname", "p.proname", NULL,
618 "pg_catalog.pg_function_is_visible(p.oid)");
620 /* Operator descriptions (only if operator has its own comment) */
621 appendPQExpBuffer(&buf,
623 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
624 " n.nspname as nspname,\n"
625 " CAST(o.oprname AS pg_catalog.text) as name,"
626 " CAST('%s' AS pg_catalog.text) as object\n"
627 " FROM pg_catalog.pg_operator o\n"
628 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
629 gettext_noop("operator"));
630 processSQLNamePattern(pset.db, &buf, pattern, false, false,
631 "n.nspname", "o.oprname", NULL,
632 "pg_catalog.pg_operator_is_visible(o.oid)");
634 /* Type description */
635 appendPQExpBuffer(&buf,
637 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
638 " n.nspname as nspname,\n"
639 " pg_catalog.format_type(t.oid, NULL) as name,"
640 " CAST('%s' AS pg_catalog.text) as object\n"
641 " FROM pg_catalog.pg_type t\n"
642 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n",
643 gettext_noop("data type"));
644 processSQLNamePattern(pset.db, &buf, pattern, false, false,
645 "n.nspname", "pg_catalog.format_type(t.oid, NULL)",
647 "pg_catalog.pg_type_is_visible(t.oid)");
649 /* Relation (tables, views, indexes, sequences) descriptions */
650 appendPQExpBuffer(&buf,
652 " SELECT c.oid as oid, c.tableoid as tableoid,\n"
653 " n.nspname as nspname,\n"
654 " CAST(c.relname AS pg_catalog.text) as name,\n"
656 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' END"
657 " AS pg_catalog.text) as object\n"
658 " FROM pg_catalog.pg_class c\n"
659 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
660 " WHERE c.relkind IN ('r', 'v', 'i', 'S')\n",
661 gettext_noop("table"),
662 gettext_noop("view"),
663 gettext_noop("index"),
664 gettext_noop("sequence"));
665 processSQLNamePattern(pset.db, &buf, pattern, true, false,
666 "n.nspname", "c.relname", NULL,
667 "pg_catalog.pg_table_is_visible(c.oid)");
669 /* Rule description (ignore rules for views) */
670 appendPQExpBuffer(&buf,
672 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
673 " n.nspname as nspname,\n"
674 " CAST(r.rulename AS pg_catalog.text) as name,"
675 " CAST('%s' AS pg_catalog.text) as object\n"
676 " FROM pg_catalog.pg_rewrite r\n"
677 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
678 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
679 " WHERE r.rulename != '_RETURN'\n",
680 gettext_noop("rule"));
681 /* XXX not sure what to do about visibility rule here? */
682 processSQLNamePattern(pset.db, &buf, pattern, true, false,
683 "n.nspname", "r.rulename", NULL,
684 "pg_catalog.pg_table_is_visible(c.oid)");
686 /* Trigger description */
687 appendPQExpBuffer(&buf,
689 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
690 " n.nspname as nspname,\n"
691 " CAST(t.tgname AS pg_catalog.text) as name,"
692 " CAST('%s' AS pg_catalog.text) as object\n"
693 " FROM pg_catalog.pg_trigger t\n"
694 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
695 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
696 gettext_noop("trigger"));
697 /* XXX not sure what to do about visibility rule here? */
698 processSQLNamePattern(pset.db, &buf, pattern, false, false,
699 "n.nspname", "t.tgname", NULL,
700 "pg_catalog.pg_table_is_visible(c.oid)");
702 appendPQExpBuffer(&buf,
704 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
706 appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
708 res = PSQLexec(buf.data, false);
709 termPQExpBuffer(&buf);
713 myopt.nullPrint = NULL;
714 myopt.title = _("Object descriptions");
715 myopt.trans_headers = true;
716 myopt.trans_columns = trans_columns;
718 printQuery(res, &myopt, pset.queryFout, pset.logfile);
726 * describeTableDetails (for \d)
728 * This routine finds the tables to be displayed, and calls
729 * describeOneTableDetails for each one.
731 * verbose: if true, this is \d+
734 describeTableDetails(const char *pattern, bool verbose)
740 initPQExpBuffer(&buf);
742 printfPQExpBuffer(&buf,
746 "FROM pg_catalog.pg_class c\n"
747 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
749 processSQLNamePattern(pset.db, &buf, pattern, false, false,
750 "n.nspname", "c.relname", NULL,
751 "pg_catalog.pg_table_is_visible(c.oid)");
753 appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
755 res = PSQLexec(buf.data, false);
756 termPQExpBuffer(&buf);
760 if (PQntuples(res) == 0)
763 fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
769 for (i = 0; i < PQntuples(res); i++)
775 oid = PQgetvalue(res, i, 0);
776 nspname = PQgetvalue(res, i, 1);
777 relname = PQgetvalue(res, i, 2);
779 if (!describeOneTableDetails(nspname, relname, oid, verbose))
796 * describeOneTableDetails (for \d)
798 * Unfortunately, the information presented here is so complicated that it
799 * cannot be done in a single query. So we have to assemble the printed table
800 * by hand and pass it to the underlying printTable() function.
803 describeOneTableDetails(const char *schemaname,
804 const char *relationname,
809 PGresult *res = NULL;
810 printTableOpt myopt = pset.popt.topt;
811 printTableContent cont;
813 char *view_def = NULL;
815 char **modifiers = NULL;
817 PQExpBufferData title;
818 PQExpBufferData tmpbuf;
831 bool show_modifiers = false;
836 /* This output looks confusing in expanded mode. */
837 myopt.expanded = false;
839 initPQExpBuffer(&buf);
840 initPQExpBuffer(&title);
841 initPQExpBuffer(&tmpbuf);
843 /* Get general table info */
844 printfPQExpBuffer(&buf,
845 "SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules, "
847 "FROM pg_catalog.pg_class WHERE oid = '%s'",
848 (pset.sversion >= 80000 ? ", reltablespace" : ""),
850 res = PSQLexec(buf.data, false);
854 /* Did we get anything? */
855 if (PQntuples(res) == 0)
858 fprintf(stderr, _("Did not find any relation with OID %s.\n"),
863 tableinfo.checks = atoi(PQgetvalue(res, 0, 2));
864 tableinfo.triggers = atoi(PQgetvalue(res, 0, 3));
865 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
866 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 0), "t") == 0;
867 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
868 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
869 tableinfo.tablespace = (pset.sversion >= 80000) ?
870 atooid(PQgetvalue(res, 0, 6)) : 0;
873 /* Get column info (index requires additional checks) */
874 printfPQExpBuffer(&buf, "SELECT a.attname,");
875 appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
876 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
877 "\n FROM pg_catalog.pg_attrdef d"
878 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
879 "\n a.attnotnull, a.attnum");
881 appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
882 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
883 if (tableinfo.relkind == 'i')
884 appendPQExpBuffer(&buf, ", pg_catalog.pg_index i");
885 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
886 if (tableinfo.relkind == 'i')
887 appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid");
888 appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
890 res = PSQLexec(buf.data, false);
893 numrows = PQntuples(res);
896 switch (tableinfo.relkind)
899 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
900 schemaname, relationname);
903 printfPQExpBuffer(&title, _("View \"%s.%s\""),
904 schemaname, relationname);
907 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
908 schemaname, relationname);
911 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
912 schemaname, relationname);
915 /* not used as of 8.2, but keep it for backwards compatibility */
916 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
917 schemaname, relationname);
920 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
921 schemaname, relationname);
924 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
925 schemaname, relationname);
928 /* untranslated unknown relkind */
929 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
930 tableinfo.relkind, schemaname, relationname);
934 /* Set the number of columns, and their names */
936 headers[0] = "Column";
939 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v')
941 show_modifiers = true;
942 headers[cols++] = "Modifiers";
943 modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
947 headers[cols++] = "Description";
949 printTableInit(&cont, &myopt, title.data, cols, numrows);
951 for (i = 0; i < cols; i++)
952 printTableAddHeader(&cont, headers[i], true, 'l');
954 /* Check if table is a view */
955 if (tableinfo.relkind == 'v')
959 printfPQExpBuffer(&buf,
960 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
962 result = PSQLexec(buf.data, false);
966 if (PQntuples(result) > 0)
967 view_def = pg_strdup(PQgetvalue(result, 0, 0));
972 /* Generate table cells to be printed */
973 for (i = 0; i < numrows; i++)
976 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
979 printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
981 /* Extra: not null and default */
984 resetPQExpBuffer(&tmpbuf);
985 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
986 appendPQExpBufferStr(&tmpbuf, _("not null"));
988 /* handle "default" here */
989 /* (note: above we cut off the 'default' string at 128) */
990 if (strlen(PQgetvalue(res, i, 2)) != 0)
993 appendPQExpBufferStr(&tmpbuf, " ");
994 /* translator: default values of column definitions */
995 appendPQExpBuffer(&tmpbuf, _("default %s"),
996 PQgetvalue(res, i, 2));
999 modifiers[i] = pg_strdup(tmpbuf.data);
1000 printTableAddCell(&cont, modifiers[i], false);
1005 printTableAddCell(&cont, PQgetvalue(res, i, 5), false);
1009 if (tableinfo.relkind == 'i')
1011 /* Footer information about an index */
1014 printfPQExpBuffer(&buf,
1015 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1016 if (pset.sversion >= 80200)
1017 appendPQExpBuffer(&buf, "i.indisvalid, ");
1019 appendPQExpBuffer(&buf, "true as indisvalid, ");
1020 appendPQExpBuffer(&buf, "a.amname, c2.relname,\n"
1021 " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1022 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1023 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1024 "AND i.indrelid = c2.oid",
1027 result = PSQLexec(buf.data, false);
1030 else if (PQntuples(result) != 1)
1037 char *indisunique = PQgetvalue(result, 0, 0);
1038 char *indisprimary = PQgetvalue(result, 0, 1);
1039 char *indisclustered = PQgetvalue(result, 0, 2);
1040 char *indisvalid = PQgetvalue(result, 0, 3);
1041 char *indamname = PQgetvalue(result, 0, 4);
1042 char *indtable = PQgetvalue(result, 0, 5);
1043 char *indpred = PQgetvalue(result, 0, 6);
1045 if (strcmp(indisprimary, "t") == 0)
1046 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1047 else if (strcmp(indisunique, "t") == 0)
1048 printfPQExpBuffer(&tmpbuf, _("unique, "));
1050 resetPQExpBuffer(&tmpbuf);
1051 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1053 /* we assume here that index and table are in same schema */
1054 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1055 schemaname, indtable);
1057 if (strlen(indpred))
1058 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1060 if (strcmp(indisclustered, "t") == 0)
1061 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1063 if (strcmp(indisvalid, "t") != 0)
1064 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1066 printTableAddFooter(&cont, tmpbuf.data);
1067 add_tablespace_footer(&cont, tableinfo.relkind,
1068 tableinfo.tablespace, true);
1075 PGresult *result = NULL;
1077 /* Footer information about a view */
1078 printTableAddFooter(&cont, _("View definition:"));
1079 printTableAddFooter(&cont, view_def);
1082 if (tableinfo.hasrules)
1084 printfPQExpBuffer(&buf,
1085 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1086 "FROM pg_catalog.pg_rewrite r\n"
1087 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1",
1089 result = PSQLexec(buf.data, false);
1093 if (PQntuples(result) > 0)
1095 printTableAddFooter(&cont, _("Rules:"));
1096 for (i = 0; i < PQntuples(result); i++)
1098 const char *ruledef;
1100 /* Everything after "CREATE RULE" is echoed verbatim */
1101 ruledef = PQgetvalue(result, i, 1);
1104 printfPQExpBuffer(&buf, " %s", ruledef);
1105 printTableAddFooter(&cont, buf.data);
1111 else if (tableinfo.relkind == 'r')
1113 /* Footer information about a table */
1114 PGresult *result = NULL;
1118 if (tableinfo.hasindex)
1120 printfPQExpBuffer(&buf,
1121 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1122 if (pset.sversion >= 80200)
1123 appendPQExpBuffer(&buf, "i.indisvalid, ");
1125 appendPQExpBuffer(&buf, "true as indisvalid, ");
1126 appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)");
1127 if (pset.sversion >= 80000)
1128 appendPQExpBuffer(&buf, ", c2.reltablespace");
1129 appendPQExpBuffer(&buf,
1130 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
1131 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1132 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
1134 result = PSQLexec(buf.data, false);
1138 tuples = PQntuples(result);
1142 printTableAddFooter(&cont, _("Indexes:"));
1143 for (i = 0; i < tuples; i++)
1145 const char *indexdef;
1146 const char *usingpos;
1148 /* untranslated index name */
1149 printfPQExpBuffer(&buf, " \"%s\"",
1150 PQgetvalue(result, i, 0));
1152 /* Label as primary key or unique (but not both) */
1153 appendPQExpBuffer(&buf,
1154 strcmp(PQgetvalue(result, i, 1), "t") == 0
1156 (strcmp(PQgetvalue(result, i, 2), "t") == 0
1159 /* Everything after "USING" is echoed verbatim */
1160 indexdef = PQgetvalue(result, i, 5);
1161 usingpos = strstr(indexdef, " USING ");
1163 indexdef = usingpos + 7;
1165 appendPQExpBuffer(&buf, " %s", indexdef);
1167 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1168 appendPQExpBuffer(&buf, " CLUSTER");
1170 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1171 appendPQExpBuffer(&buf, " INVALID");
1173 printTableAddFooter(&cont, buf.data);
1175 /* Print tablespace of the index on the same line */
1176 if (pset.sversion >= 80000)
1177 add_tablespace_footer(&cont, 'i',
1178 atooid(PQgetvalue(result, i, 6)),
1185 /* print table (and column) check constraints */
1186 if (tableinfo.checks)
1188 printfPQExpBuffer(&buf,
1189 "SELECT r.conname, "
1190 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1191 "FROM pg_catalog.pg_constraint r\n"
1192 "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1",
1194 result = PSQLexec(buf.data, false);
1198 tuples = PQntuples(result);
1202 printTableAddFooter(&cont, _("Check constraints:"));
1203 for (i = 0; i < tuples; i++)
1205 /* untranslated contraint name and def */
1206 printfPQExpBuffer(&buf, " \"%s\" %s",
1207 PQgetvalue(result, i, 0),
1208 PQgetvalue(result, i, 1));
1210 printTableAddFooter(&cont, buf.data);
1216 /* print foreign-key constraints (there are none if no triggers) */
1217 if (tableinfo.triggers)
1219 printfPQExpBuffer(&buf,
1221 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1222 "FROM pg_catalog.pg_constraint r\n"
1223 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1",
1225 result = PSQLexec(buf.data, false);
1229 tuples = PQntuples(result);
1233 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1234 for (i = 0; i < tuples; i++)
1236 /* untranslated constraint name and def */
1237 printfPQExpBuffer(&buf, " \"%s\" %s",
1238 PQgetvalue(result, i, 0),
1239 PQgetvalue(result, i, 1));
1241 printTableAddFooter(&cont, buf.data);
1247 /* print incoming foreign-key references (none if no triggers) */
1248 if (tableinfo.triggers)
1250 printfPQExpBuffer(&buf,
1251 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1252 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1253 "FROM pg_catalog.pg_constraint c\n"
1254 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
1256 result = PSQLexec(buf.data, false);
1260 tuples = PQntuples(result);
1264 printTableAddFooter(&cont, _("Referenced by:"));
1265 for (i = 0; i < tuples; i++)
1267 /* translator: the first %s is a FK name, the following are
1268 * a table name and the FK definition */
1269 printfPQExpBuffer(&buf, _(" \"%s\" IN %s %s"),
1270 PQgetvalue(result, i, 0),
1271 PQgetvalue(result, i, 1),
1272 PQgetvalue(result, i, 2));
1274 printTableAddFooter(&cont, buf.data);
1281 if (tableinfo.hasrules)
1283 if (pset.sversion >= 80300)
1285 printfPQExpBuffer(&buf,
1286 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1288 "FROM pg_catalog.pg_rewrite r\n"
1289 "WHERE r.ev_class = '%s' ORDER BY 1",
1294 printfPQExpBuffer(&buf,
1295 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1296 "'O'::char AS ev_enabled\n"
1297 "FROM pg_catalog.pg_rewrite r\n"
1298 "WHERE r.ev_class = '%s' ORDER BY 1",
1301 result = PSQLexec(buf.data, false);
1305 tuples = PQntuples(result);
1312 for (category = 0; category < 4; category++)
1314 have_heading = false;
1316 for (i = 0; i < tuples; i++)
1318 const char *ruledef;
1319 bool list_rule = false;
1324 if (*PQgetvalue(result, i, 2) == 'O')
1328 if (*PQgetvalue(result, i, 2) == 'D')
1332 if (*PQgetvalue(result, i, 2) == 'A')
1336 if (*PQgetvalue(result, i, 2) == 'R')
1348 printfPQExpBuffer(&buf, _("Rules:"));
1351 printfPQExpBuffer(&buf, _("Disabled rules:"));
1354 printfPQExpBuffer(&buf, _("Rules firing always:"));
1357 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1360 printTableAddFooter(&cont, buf.data);
1361 have_heading = true;
1364 /* Everything after "CREATE RULE" is echoed verbatim */
1365 ruledef = PQgetvalue(result, i, 1);
1367 printfPQExpBuffer(&buf, " %s", ruledef);
1368 printTableAddFooter(&cont, buf.data);
1375 /* print triggers (but ignore foreign-key triggers) */
1376 if (tableinfo.triggers)
1378 printfPQExpBuffer(&buf,
1380 "pg_catalog.pg_get_triggerdef(t.oid), "
1382 "FROM pg_catalog.pg_trigger t\n"
1383 "WHERE t.tgrelid = '%s' AND ",
1385 if (pset.sversion >= 80300)
1386 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
1388 appendPQExpBuffer(&buf,
1389 "(NOT tgisconstraint "
1391 " (SELECT 1 FROM pg_catalog.pg_depend d "
1392 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
1393 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
1394 appendPQExpBuffer(&buf, "\nORDER BY 1");
1396 result = PSQLexec(buf.data, false);
1400 tuples = PQntuples(result);
1408 * split the output into 4 different categories. Enabled triggers,
1409 * disabled triggers and the two special ALWAYS and REPLICA
1412 for (category = 0; category < 4; category++)
1414 have_heading = false;
1415 for (i = 0; i < tuples; i++)
1419 const char *usingpos;
1420 const char *tgenabled;
1422 /* Check if this trigger falls into the current category */
1423 tgenabled = PQgetvalue(result, i, 2);
1424 list_trigger = false;
1428 if (*tgenabled == 'O' || *tgenabled == 't')
1429 list_trigger = true;
1432 if (*tgenabled == 'D' || *tgenabled == 'f')
1433 list_trigger = true;
1436 if (*tgenabled == 'A')
1437 list_trigger = true;
1440 if (*tgenabled == 'R')
1441 list_trigger = true;
1444 if (list_trigger == false)
1447 /* Print the category heading once */
1448 if (have_heading == false)
1453 printfPQExpBuffer(&buf, _("Triggers:"));
1456 printfPQExpBuffer(&buf, _("Disabled triggers:"));
1459 printfPQExpBuffer(&buf, _("Triggers firing always:"));
1462 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
1466 printTableAddFooter(&cont, buf.data);
1467 have_heading = true;
1470 /* Everything after "TRIGGER" is echoed verbatim */
1471 tgdef = PQgetvalue(result, i, 1);
1472 usingpos = strstr(tgdef, " TRIGGER ");
1474 tgdef = usingpos + 9;
1476 printfPQExpBuffer(&buf, " %s", tgdef);
1477 printTableAddFooter(&cont, buf.data);
1484 /* print inherited tables */
1485 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);
1487 result = PSQLexec(buf.data, false);
1491 tuples = PQntuples(result);
1493 for (i = 0; i < tuples; i++)
1495 const char *s = _("Inherits");
1498 printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
1500 printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
1502 appendPQExpBuffer(&buf, ",");
1504 printTableAddFooter(&cont, buf.data);
1510 const char *s = _("Has OIDs");
1512 printfPQExpBuffer(&buf, "%s: %s", s,
1513 (tableinfo.hasoids ? _("yes") : _("no")));
1514 printTableAddFooter(&cont, buf.data);
1517 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
1521 printTable(&cont, pset.queryFout, pset.logfile);
1522 printTableCleanup(&cont);
1529 printTableCleanup(&cont);
1530 termPQExpBuffer(&buf);
1531 termPQExpBuffer(&title);
1532 termPQExpBuffer(&tmpbuf);
1536 for (ptr = modifiers; *ptr; ptr++)
1551 * Add a tablespace description to a footer. If 'newline' is true, it is added
1552 * in a new line; otherwise it's appended to the current value of the last
1556 add_tablespace_footer(printTableContent *const cont, char relkind,
1557 Oid tablespace, const bool newline)
1559 /* relkinds for which we support tablespaces */
1560 if (relkind == 'r' || relkind == 'i')
1563 * We ignore the database default tablespace so that users not using
1564 * tablespaces don't need to know about them. This case also covers
1565 * pre-8.0 servers, for which tablespace will always be 0.
1567 if (tablespace != 0)
1569 PGresult *result = NULL;
1570 PQExpBufferData buf;
1572 initPQExpBuffer(&buf);
1573 printfPQExpBuffer(&buf,
1574 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
1575 "WHERE oid = '%u'", tablespace);
1576 result = PSQLexec(buf.data, false);
1579 /* Should always be the case, but.... */
1580 if (PQntuples(result) > 0)
1584 /* Add the tablespace as a new footer */
1585 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
1586 PQgetvalue(result, 0, 0));
1587 printTableAddFooter(cont, buf.data);
1591 /* Append the tablespace to the latest footer */
1592 printfPQExpBuffer(&buf, "%s", cont->footer->data);
1593 /* translator: before this string there's an index
1594 * description like '"foo_pkey" PRIMARY KEY, btree (a)' */
1595 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
1596 PQgetvalue(result, 0, 0));
1597 printTableSetFooter(cont, buf.data);
1601 termPQExpBuffer(&buf);
1609 * Describes roles. Any schema portion of the pattern is ignored.
1612 describeRoles(const char *pattern, bool verbose)
1614 PQExpBufferData buf;
1616 printTableContent cont;
1617 printTableOpt myopt = pset.popt.topt;
1622 const char align = 'l';
1625 initPQExpBuffer(&buf);
1627 if (pset.sversion >= 80100)
1629 printfPQExpBuffer(&buf,
1630 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
1631 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
1632 " r.rolconnlimit,\n"
1633 " ARRAY(SELECT b.rolname\n"
1634 " FROM pg_catalog.pg_auth_members m\n"
1635 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
1636 " WHERE m.member = r.oid) as memberof");
1638 if (verbose && pset.sversion >= 80200)
1640 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
1644 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
1646 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1647 NULL, "r.rolname", NULL, NULL);
1651 printfPQExpBuffer(&buf,
1652 "SELECT u.usename AS rolname,\n"
1653 " u.usesuper AS rolsuper,\n"
1654 " true AS rolinherit, false AS rolcreaterole,\n"
1655 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
1656 " -1 AS rolconnlimit,\n"
1657 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
1658 "\nFROM pg_catalog.pg_user u\n");
1660 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1661 NULL, "u.usename", NULL, NULL);
1664 appendPQExpBuffer(&buf, "ORDER BY 1;");
1666 res = PSQLexec(buf.data, false);
1670 nrows = PQntuples(res);
1671 attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
1673 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
1675 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
1676 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
1677 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
1679 if (verbose && pset.sversion >= 80200)
1680 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
1682 for (i = 0; i < nrows; i++)
1684 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
1686 resetPQExpBuffer(&buf);
1687 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
1688 add_role_attribute(&buf, _("Superuser"));
1690 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
1691 add_role_attribute(&buf, _("No inheritance"));
1693 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1694 add_role_attribute(&buf, _("Create role"));
1696 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
1697 add_role_attribute(&buf, _("Create DB"));
1699 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
1700 add_role_attribute(&buf, _("Cannot login"));
1702 conns = atoi(PQgetvalue(res, i, 6));
1706 appendPQExpBufferStr(&buf, "\n");
1709 appendPQExpBuffer(&buf, _("No connections"));
1710 else if (conns == 1)
1711 appendPQExpBuffer(&buf, _("1 connection"));
1713 appendPQExpBuffer(&buf, _("%d connections"), conns);
1716 attr[i] = pg_strdup(buf.data);
1718 printTableAddCell(&cont, attr[i], false);
1720 printTableAddCell(&cont, PQgetvalue(res, i, 7), false);
1722 if (verbose && pset.sversion >= 80200)
1723 printTableAddCell(&cont, PQgetvalue(res, i, 8), false);
1725 termPQExpBuffer(&buf);
1727 printTable(&cont, pset.queryFout, pset.logfile);
1728 printTableCleanup(&cont);
1730 for (i = 0; i < nrows; i++)
1739 add_role_attribute(PQExpBuffer buf, const char *const str)
1742 appendPQExpBufferStr(buf, "\n");
1744 appendPQExpBufferStr(buf, str);
1751 * handler for \d, \dt, etc.
1753 * tabtypes is an array of characters, specifying what info is desired:
1758 * S - system tables (pg_catalog)
1759 * (any order of the above is fine)
1762 listTables(const char *tabtypes, const char *pattern, bool verbose)
1764 bool showTables = strchr(tabtypes, 't') != NULL;
1765 bool showIndexes = strchr(tabtypes, 'i') != NULL;
1766 bool showViews = strchr(tabtypes, 'v') != NULL;
1767 bool showSeq = strchr(tabtypes, 's') != NULL;
1768 bool showSystem = strchr(tabtypes, 'S') != NULL;
1770 PQExpBufferData buf;
1772 printQueryOpt myopt = pset.popt;
1773 static const bool trans_columns[] = {false, false, true, false, false, false};
1775 if (!(showTables || showIndexes || showViews || showSeq))
1776 showTables = showViews = showSeq = true;
1778 initPQExpBuffer(&buf);
1781 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
1782 * for backwards compatibility.
1784 printfPQExpBuffer(&buf,
1785 "SELECT n.nspname as \"%s\",\n"
1786 " c.relname as \"%s\",\n"
1787 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' END as \"%s\",\n"
1788 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
1789 gettext_noop("Schema"),
1790 gettext_noop("Name"),
1791 gettext_noop("table"),
1792 gettext_noop("view"),
1793 gettext_noop("index"),
1794 gettext_noop("sequence"),
1795 gettext_noop("special"),
1796 gettext_noop("Type"),
1797 gettext_noop("Owner"));
1800 appendPQExpBuffer(&buf,
1801 ",\n c2.relname as \"%s\"",
1802 gettext_noop("Table"));
1804 if (verbose && pset.sversion >= 80100)
1805 appendPQExpBuffer(&buf,
1806 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
1807 gettext_noop("Size"));
1809 appendPQExpBuffer(&buf,
1810 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
1811 gettext_noop("Description"));
1813 appendPQExpBuffer(&buf,
1814 "\nFROM pg_catalog.pg_class c"
1815 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
1817 appendPQExpBuffer(&buf,
1818 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
1819 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
1821 appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
1823 appendPQExpBuffer(&buf, "'r',");
1825 appendPQExpBuffer(&buf, "'v',");
1827 appendPQExpBuffer(&buf, "'i',");
1829 appendPQExpBuffer(&buf, "'S',");
1830 if (showSystem && showTables)
1831 appendPQExpBuffer(&buf, "'s',");
1832 appendPQExpBuffer(&buf, "''"); /* dummy */
1833 appendPQExpBuffer(&buf, ")\n");
1836 * If showSystem is specified, show only system objects (those in
1837 * pg_catalog). Otherwise, suppress system objects, including those in
1838 * pg_catalog and pg_toast. (We don't want to hide temp tables though.)
1841 appendPQExpBuffer(&buf,
1842 " AND n.nspname = 'pg_catalog'\n");
1844 appendPQExpBuffer(&buf,
1845 " AND n.nspname <> 'pg_catalog'\n"
1846 " AND n.nspname !~ '^pg_toast'\n");
1848 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1849 "n.nspname", "c.relname", NULL,
1850 "pg_catalog.pg_table_is_visible(c.oid)");
1852 appendPQExpBuffer(&buf, "ORDER BY 1,2;");
1854 res = PSQLexec(buf.data, false);
1855 termPQExpBuffer(&buf);
1859 if (PQntuples(res) == 0 && !pset.quiet)
1862 fprintf(pset.queryFout, _("No matching relations found.\n"));
1864 fprintf(pset.queryFout, _("No relations found.\n"));
1868 myopt.nullPrint = NULL;
1869 myopt.title = _("List of relations");
1870 myopt.trans_headers = true;
1871 myopt.trans_columns = trans_columns;
1873 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1884 * Describes domains.
1887 listDomains(const char *pattern)
1889 PQExpBufferData buf;
1891 printQueryOpt myopt = pset.popt;
1893 initPQExpBuffer(&buf);
1895 printfPQExpBuffer(&buf,
1896 "SELECT n.nspname as \"%s\",\n"
1897 " t.typname as \"%s\",\n"
1898 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
1899 " CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
1900 " WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
1901 " WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
1904 " pg_catalog.pg_get_constraintdef(r.oid, true) as \"%s\"\n"
1905 "FROM pg_catalog.pg_type t\n"
1906 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
1907 " LEFT JOIN pg_catalog.pg_constraint r ON t.oid = r.contypid\n"
1908 "WHERE t.typtype = 'd'\n",
1909 gettext_noop("Schema"),
1910 gettext_noop("Name"),
1911 gettext_noop("Type"),
1912 gettext_noop("Modifier"),
1913 gettext_noop("Check"));
1915 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1916 "n.nspname", "t.typname", NULL,
1917 "pg_catalog.pg_type_is_visible(t.oid)");
1919 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1921 res = PSQLexec(buf.data, false);
1922 termPQExpBuffer(&buf);
1926 myopt.nullPrint = NULL;
1927 myopt.title = _("List of domains");
1928 myopt.trans_headers = true;
1930 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1939 * Describes conversions.
1942 listConversions(const char *pattern)
1944 PQExpBufferData buf;
1946 printQueryOpt myopt = pset.popt;
1947 static const bool trans_columns[] = {false, false, false, false, true};
1949 initPQExpBuffer(&buf);
1951 printfPQExpBuffer(&buf,
1952 "SELECT n.nspname AS \"%s\",\n"
1953 " c.conname AS \"%s\",\n"
1954 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
1955 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
1956 " CASE WHEN c.condefault THEN '%s'\n"
1957 " ELSE '%s' END AS \"%s\"\n"
1958 "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
1959 "WHERE n.oid = c.connamespace\n",
1960 gettext_noop("Schema"),
1961 gettext_noop("Name"),
1962 gettext_noop("Source"),
1963 gettext_noop("Destination"),
1964 gettext_noop("yes"), gettext_noop("no"),
1965 gettext_noop("Default?"));
1967 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1968 "n.nspname", "c.conname", NULL,
1969 "pg_catalog.pg_conversion_is_visible(c.oid)");
1971 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1973 res = PSQLexec(buf.data, false);
1974 termPQExpBuffer(&buf);
1978 myopt.nullPrint = NULL;
1979 myopt.title = _("List of conversions");
1980 myopt.trans_headers = true;
1981 myopt.trans_columns = trans_columns;
1983 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1995 listCasts(const char *pattern)
1997 PQExpBufferData buf;
1999 printQueryOpt myopt = pset.popt;
2000 static const bool trans_columns[] = {false, false, false, true};
2002 initPQExpBuffer(&buf);
2004 * We need left join here for binary casts. Also note that we don't
2005 * attempt to localize '(binary compatible)', because there's too much
2006 * risk of gettext translating a function name that happens to match
2007 * some string in the PO database.
2009 printfPQExpBuffer(&buf,
2010 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2011 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2012 " CASE WHEN castfunc = 0 THEN '(binary compatible)'\n"
2015 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2016 " WHEN c.castcontext = 'a' THEN '%s'\n"
2019 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2020 " ON c.castfunc = p.oid\n"
2022 gettext_noop("Source type"),
2023 gettext_noop("Target type"),
2024 gettext_noop("Function"),
2025 gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2026 gettext_noop("Implicit?"));
2028 res = PSQLexec(buf.data, false);
2029 termPQExpBuffer(&buf);
2033 myopt.nullPrint = NULL;
2034 myopt.title = _("List of casts");
2035 myopt.trans_headers = true;
2036 myopt.trans_columns = trans_columns;
2038 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2047 * Describes schemas (namespaces)
2050 listSchemas(const char *pattern, bool verbose)
2052 PQExpBufferData buf;
2054 printQueryOpt myopt = pset.popt;
2056 initPQExpBuffer(&buf);
2057 printfPQExpBuffer(&buf,
2058 "SELECT n.nspname AS \"%s\",\n"
2059 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2060 gettext_noop("Name"),
2061 gettext_noop("Owner"));
2064 appendPQExpBuffer(&buf,
2065 ",\n n.nspacl as \"%s\","
2066 " pg_catalog.obj_description(n.oid, 'pg_namespace') as \"%s\"",
2067 gettext_noop("Access privileges"),
2068 gettext_noop("Description"));
2070 appendPQExpBuffer(&buf,
2071 "\nFROM pg_catalog.pg_namespace n\n"
2072 "WHERE (n.nspname !~ '^pg_temp_' OR\n"
2073 " n.nspname = (pg_catalog.current_schemas(true))[1])\n"); /* temp schema is first */
2075 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2076 NULL, "n.nspname", NULL,
2079 appendPQExpBuffer(&buf, "ORDER BY 1;");
2081 res = PSQLexec(buf.data, false);
2082 termPQExpBuffer(&buf);
2086 myopt.nullPrint = NULL;
2087 myopt.title = _("List of schemas");
2088 myopt.trans_headers = true;
2090 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2099 * list text search parsers
2102 listTSParsers(const char *pattern, bool verbose)
2104 PQExpBufferData buf;
2106 printQueryOpt myopt = pset.popt;
2108 if (pset.sversion < 80300)
2110 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2111 pset.sversion / 10000, (pset.sversion / 100) % 100);
2116 return listTSParsersVerbose(pattern);
2118 initPQExpBuffer(&buf);
2120 printfPQExpBuffer(&buf,
2122 " n.nspname as \"%s\",\n"
2123 " p.prsname as \"%s\",\n"
2124 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2125 "FROM pg_catalog.pg_ts_parser p \n"
2126 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2127 gettext_noop("Schema"),
2128 gettext_noop("Name"),
2129 gettext_noop("Description")
2132 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2133 "n.nspname", "p.prsname", NULL,
2134 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2136 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2138 res = PSQLexec(buf.data, false);
2139 termPQExpBuffer(&buf);
2143 myopt.nullPrint = NULL;
2144 myopt.title = _("List of text search parsers");
2145 myopt.trans_headers = true;
2147 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2154 * full description of parsers
2157 listTSParsersVerbose(const char *pattern)
2159 PQExpBufferData buf;
2163 initPQExpBuffer(&buf);
2165 printfPQExpBuffer(&buf,
2169 "FROM pg_catalog.pg_ts_parser p\n"
2170 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2173 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2174 "n.nspname", "p.prsname", NULL,
2175 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2177 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2179 res = PSQLexec(buf.data, false);
2180 termPQExpBuffer(&buf);
2184 if (PQntuples(res) == 0)
2187 fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
2193 for (i = 0; i < PQntuples(res); i++)
2196 const char *nspname = NULL;
2197 const char *prsname;
2199 oid = PQgetvalue(res, i, 0);
2200 if (!PQgetisnull(res, i, 1))
2201 nspname = PQgetvalue(res, i, 1);
2202 prsname = PQgetvalue(res, i, 2);
2204 if (!describeOneTSParser(oid, nspname, prsname))
2222 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
2224 PQExpBufferData buf;
2227 printQueryOpt myopt = pset.popt;
2228 static const bool trans_columns[] = {true, false, false};
2230 initPQExpBuffer(&buf);
2232 printfPQExpBuffer(&buf,
2233 "SELECT '%s' AS \"%s\", \n"
2234 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
2235 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
2236 " FROM pg_catalog.pg_ts_parser p \n"
2237 " WHERE p.oid = '%s' \n"
2240 " p.prstoken::pg_catalog.regproc, \n"
2241 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
2242 " FROM pg_catalog.pg_ts_parser p \n"
2243 " WHERE p.oid = '%s' \n"
2246 " p.prsend::pg_catalog.regproc, \n"
2247 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
2248 " FROM pg_catalog.pg_ts_parser p \n"
2249 " WHERE p.oid = '%s' \n"
2252 " p.prsheadline::pg_catalog.regproc, \n"
2253 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
2254 " FROM pg_catalog.pg_ts_parser p \n"
2255 " WHERE p.oid = '%s' \n"
2258 " p.prslextype::pg_catalog.regproc, \n"
2259 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
2260 " FROM pg_catalog.pg_ts_parser p \n"
2261 " WHERE p.oid = '%s' \n",
2262 gettext_noop("Start parse"),
2263 gettext_noop("Method"),
2264 gettext_noop("Function"),
2265 gettext_noop("Description"),
2267 gettext_noop("Get next token"),
2269 gettext_noop("End parse"),
2271 gettext_noop("Get headline"),
2273 gettext_noop("Get token types"),
2276 res = PSQLexec(buf.data, false);
2277 termPQExpBuffer(&buf);
2281 myopt.nullPrint = NULL;
2283 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
2285 sprintf(title, _("Text search parser \"%s\""), prsname);
2286 myopt.title = title;
2287 myopt.footers = NULL;
2288 myopt.default_footer = false;
2289 myopt.trans_headers = true;
2290 myopt.trans_columns = trans_columns;
2292 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2296 initPQExpBuffer(&buf);
2298 printfPQExpBuffer(&buf,
2299 "SELECT t.alias as \"%s\", \n"
2300 " t.description as \"%s\" \n"
2301 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
2303 gettext_noop("Token name"),
2304 gettext_noop("Description"),
2307 res = PSQLexec(buf.data, false);
2308 termPQExpBuffer(&buf);
2312 myopt.nullPrint = NULL;
2314 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
2316 sprintf(title, _("Token types for parser \"%s\""), prsname);
2317 myopt.title = title;
2318 myopt.footers = NULL;
2319 myopt.default_footer = true;
2320 myopt.trans_headers = true;
2321 myopt.trans_columns = NULL;
2323 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2332 * list text search dictionaries
2335 listTSDictionaries(const char *pattern, bool verbose)
2337 PQExpBufferData buf;
2339 printQueryOpt myopt = pset.popt;
2341 if (pset.sversion < 80300)
2343 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2344 pset.sversion / 10000, (pset.sversion / 100) % 100);
2348 initPQExpBuffer(&buf);
2350 printfPQExpBuffer(&buf,
2352 " n.nspname as \"%s\",\n"
2353 " d.dictname as \"%s\",\n",
2354 gettext_noop("Schema"),
2355 gettext_noop("Name"));
2359 appendPQExpBuffer(&buf,
2360 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
2361 " pg_catalog.pg_ts_template t \n"
2362 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
2363 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
2364 " d.dictinitoption as \"%s\", \n",
2365 gettext_noop("Template"),
2366 gettext_noop("Init options"));
2369 appendPQExpBuffer(&buf,
2370 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
2371 gettext_noop("Description"));
2373 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
2374 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
2376 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2377 "n.nspname", "d.dictname", NULL,
2378 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
2380 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2382 res = PSQLexec(buf.data, false);
2383 termPQExpBuffer(&buf);
2387 myopt.nullPrint = NULL;
2388 myopt.title = _("List of text search dictionaries");
2389 myopt.trans_headers = true;
2391 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2400 * list text search templates
2403 listTSTemplates(const char *pattern, bool verbose)
2405 PQExpBufferData buf;
2407 printQueryOpt myopt = pset.popt;
2409 if (pset.sversion < 80300)
2411 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2412 pset.sversion / 10000, (pset.sversion / 100) % 100);
2416 initPQExpBuffer(&buf);
2419 printfPQExpBuffer(&buf,
2421 " n.nspname AS \"%s\",\n"
2422 " t.tmplname AS \"%s\",\n"
2423 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
2424 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
2425 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2426 gettext_noop("Schema"),
2427 gettext_noop("Name"),
2428 gettext_noop("Init"),
2429 gettext_noop("Lexize"),
2430 gettext_noop("Description"));
2432 printfPQExpBuffer(&buf,
2434 " n.nspname AS \"%s\",\n"
2435 " t.tmplname AS \"%s\",\n"
2436 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2437 gettext_noop("Schema"),
2438 gettext_noop("Name"),
2439 gettext_noop("Description"));
2441 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
2442 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
2444 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2445 "n.nspname", "t.tmplname", NULL,
2446 "pg_catalog.pg_ts_template_is_visible(t.oid)");
2448 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2450 res = PSQLexec(buf.data, false);
2451 termPQExpBuffer(&buf);
2455 myopt.nullPrint = NULL;
2456 myopt.title = _("List of text search templates");
2457 myopt.trans_headers = true;
2459 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2468 * list text search configurations
2471 listTSConfigs(const char *pattern, bool verbose)
2473 PQExpBufferData buf;
2475 printQueryOpt myopt = pset.popt;
2477 if (pset.sversion < 80300)
2479 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2480 pset.sversion / 10000, (pset.sversion / 100) % 100);
2485 return listTSConfigsVerbose(pattern);
2487 initPQExpBuffer(&buf);
2489 printfPQExpBuffer(&buf,
2491 " n.nspname as \"%s\",\n"
2492 " c.cfgname as \"%s\",\n"
2493 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
2494 "FROM pg_catalog.pg_ts_config c\n"
2495 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
2496 gettext_noop("Schema"),
2497 gettext_noop("Name"),
2498 gettext_noop("Description")
2501 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2502 "n.nspname", "c.cfgname", NULL,
2503 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2505 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2507 res = PSQLexec(buf.data, false);
2508 termPQExpBuffer(&buf);
2512 myopt.nullPrint = NULL;
2513 myopt.title = _("List of text search configurations");
2514 myopt.trans_headers = true;
2516 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2523 listTSConfigsVerbose(const char *pattern)
2525 PQExpBufferData buf;
2529 initPQExpBuffer(&buf);
2531 printfPQExpBuffer(&buf,
2532 "SELECT c.oid, c.cfgname,\n"
2535 " np.nspname as pnspname \n"
2536 "FROM pg_catalog.pg_ts_config c \n"
2537 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
2538 " pg_catalog.pg_ts_parser p \n"
2539 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
2540 "WHERE p.oid = c.cfgparser\n"
2543 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2544 "n.nspname", "c.cfgname", NULL,
2545 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2547 appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
2549 res = PSQLexec(buf.data, false);
2550 termPQExpBuffer(&buf);
2554 if (PQntuples(res) == 0)
2557 fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
2563 for (i = 0; i < PQntuples(res); i++)
2566 const char *cfgname;
2567 const char *nspname = NULL;
2568 const char *prsname;
2569 const char *pnspname = NULL;
2571 oid = PQgetvalue(res, i, 0);
2572 cfgname = PQgetvalue(res, i, 1);
2573 if (!PQgetisnull(res, i, 2))
2574 nspname = PQgetvalue(res, i, 2);
2575 prsname = PQgetvalue(res, i, 3);
2576 if (!PQgetisnull(res, i, 4))
2577 pnspname = PQgetvalue(res, i, 4);
2579 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
2597 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
2598 const char *pnspname, const char *prsname)
2600 PQExpBufferData buf,
2603 printQueryOpt myopt = pset.popt;
2605 initPQExpBuffer(&buf);
2607 printfPQExpBuffer(&buf,
2609 " ( SELECT t.alias FROM \n"
2610 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
2611 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
2612 " pg_catalog.btrim( \n"
2613 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
2614 " FROM pg_catalog.pg_ts_config_map AS mm \n"
2615 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
2616 " ORDER BY mapcfg, maptokentype, mapseqno \n"
2617 " ) :: pg_catalog.text , \n"
2618 " '{}') AS \"%s\" \n"
2619 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
2620 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
2621 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
2623 gettext_noop("Token"),
2624 gettext_noop("Dictionaries"),
2627 res = PSQLexec(buf.data, false);
2628 termPQExpBuffer(&buf);
2632 initPQExpBuffer(&title);
2635 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
2638 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
2642 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
2645 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
2648 myopt.nullPrint = NULL;
2649 myopt.title = title.data;
2650 myopt.footers = NULL;
2651 myopt.default_footer = false;
2652 myopt.trans_headers = true;
2654 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2656 termPQExpBuffer(&title);