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.180 2008/07/15 03:16:03 momjian 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.translate_header = 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.translate_header = 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.translate_header = 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.translate_header = 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.translate_header = 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.translate_header = 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 translate_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.translate_header = true;
550 myopt.translate_columns = translate_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 translate_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.translate_header = true;
716 myopt.translate_columns = translate_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 **seq_values = NULL;
816 char **modifiers = NULL;
818 PQExpBufferData title;
819 PQExpBufferData tmpbuf;
832 bool show_modifiers = false;
837 /* This output looks confusing in expanded mode. */
838 myopt.expanded = false;
840 initPQExpBuffer(&buf);
841 initPQExpBuffer(&title);
842 initPQExpBuffer(&tmpbuf);
844 /* Get general table info */
845 printfPQExpBuffer(&buf,
846 "SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules, "
848 "FROM pg_catalog.pg_class WHERE oid = '%s'",
849 (pset.sversion >= 80000 ? ", reltablespace" : ""),
851 res = PSQLexec(buf.data, false);
855 /* Did we get anything? */
856 if (PQntuples(res) == 0)
859 fprintf(stderr, _("Did not find any relation with OID %s.\n"),
864 tableinfo.checks = atoi(PQgetvalue(res, 0, 2));
865 tableinfo.triggers = atoi(PQgetvalue(res, 0, 3));
866 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
867 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 0), "t") == 0;
868 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
869 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
870 tableinfo.tablespace = (pset.sversion >= 80000) ?
871 atooid(PQgetvalue(res, 0, 6)) : 0;
875 * This is used to get the values of a sequence and store it in an
876 * array that will be used later.
878 if (tableinfo.relkind == 'S')
882 #define SEQ_NUM_COLS 10
883 printfPQExpBuffer(&buf,
884 "SELECT sequence_name, last_value, \n"
885 " start_value, increment_by, \n"
886 " max_value, min_value, cache_value, \n"
887 " log_cnt, is_cycled, is_called \n"
891 result = PSQLexec(buf.data, false);
895 seq_values = pg_malloc_zero((SEQ_NUM_COLS+1) * sizeof(*seq_values));
897 for (i = 0; i < SEQ_NUM_COLS; i++)
898 seq_values[i] = pg_strdup(PQgetvalue(result, 0, i));
903 /* Get column info (index requires additional checks) */
904 printfPQExpBuffer(&buf, "SELECT a.attname,");
905 appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
906 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
907 "\n FROM pg_catalog.pg_attrdef d"
908 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
909 "\n a.attnotnull, a.attnum");
911 appendPQExpBuffer(&buf, ", a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)");
912 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
913 if (tableinfo.relkind == 'i')
914 appendPQExpBuffer(&buf, ", pg_catalog.pg_index i");
915 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
916 if (tableinfo.relkind == 'i')
917 appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid");
918 appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
920 res = PSQLexec(buf.data, false);
923 numrows = PQntuples(res);
926 switch (tableinfo.relkind)
929 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
930 schemaname, relationname);
933 printfPQExpBuffer(&title, _("View \"%s.%s\""),
934 schemaname, relationname);
937 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
938 schemaname, relationname);
941 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
942 schemaname, relationname);
945 /* not used as of 8.2, but keep it for backwards compatibility */
946 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
947 schemaname, relationname);
950 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
951 schemaname, relationname);
954 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
955 schemaname, relationname);
958 /* untranslated unknown relkind */
959 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
960 tableinfo.relkind, schemaname, relationname);
964 /* Set the number of columns, and their names */
966 headers[0] = gettext_noop("Column");
967 headers[1] = gettext_noop("Type");
969 if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v')
971 show_modifiers = true;
972 headers[cols++] = gettext_noop("Modifiers");
973 modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
976 if (tableinfo.relkind == 'S')
977 headers[cols++] = gettext_noop("Value");
981 headers[cols++] = gettext_noop("Storage");
982 headers[cols++] = gettext_noop("Description");
985 printTableInit(&cont, &myopt, title.data, cols, numrows);
987 for (i = 0; i < cols; i++)
988 printTableAddHeader(&cont, headers[i], true, 'l');
990 /* Check if table is a view */
991 if (tableinfo.relkind == 'v')
995 printfPQExpBuffer(&buf,
996 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
998 result = PSQLexec(buf.data, false);
1002 if (PQntuples(result) > 0)
1003 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1008 /* Generate table cells to be printed */
1009 for (i = 0; i < numrows; i++)
1012 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
1015 printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
1017 /* A special 'Value' column for sequences */
1018 if (tableinfo.relkind == 'S')
1019 printTableAddCell(&cont, seq_values[i], false);
1021 /* Extra: not null and default */
1024 resetPQExpBuffer(&tmpbuf);
1025 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1026 appendPQExpBufferStr(&tmpbuf, _("not null"));
1028 /* handle "default" here */
1029 /* (note: above we cut off the 'default' string at 128) */
1030 if (strlen(PQgetvalue(res, i, 2)) != 0)
1033 appendPQExpBufferStr(&tmpbuf, " ");
1034 /* translator: default values of column definitions */
1035 appendPQExpBuffer(&tmpbuf, _("default %s"),
1036 PQgetvalue(res, i, 2));
1039 modifiers[i] = pg_strdup(tmpbuf.data);
1040 printTableAddCell(&cont, modifiers[i], false);
1043 /* Storage and Description */
1046 char *storage = PQgetvalue(res, i, 5);
1047 /* these strings are literal in our syntax, so not translated. */
1048 printTableAddCell(&cont, (storage[0]=='p' ? "plain" :
1049 (storage[0]=='m' ? "main" :
1050 (storage[0]=='x' ? "extended" :
1051 (storage[0]=='e' ? "external" :
1054 printTableAddCell(&cont, PQgetvalue(res, i, 6), false);
1059 if (tableinfo.relkind == 'i')
1061 /* Footer information about an index */
1064 printfPQExpBuffer(&buf,
1065 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1066 if (pset.sversion >= 80200)
1067 appendPQExpBuffer(&buf, "i.indisvalid, ");
1069 appendPQExpBuffer(&buf, "true as indisvalid, ");
1070 appendPQExpBuffer(&buf, "a.amname, c2.relname,\n"
1071 " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1072 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1073 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1074 "AND i.indrelid = c2.oid",
1077 result = PSQLexec(buf.data, false);
1080 else if (PQntuples(result) != 1)
1087 char *indisunique = PQgetvalue(result, 0, 0);
1088 char *indisprimary = PQgetvalue(result, 0, 1);
1089 char *indisclustered = PQgetvalue(result, 0, 2);
1090 char *indisvalid = PQgetvalue(result, 0, 3);
1091 char *indamname = PQgetvalue(result, 0, 4);
1092 char *indtable = PQgetvalue(result, 0, 5);
1093 char *indpred = PQgetvalue(result, 0, 6);
1095 if (strcmp(indisprimary, "t") == 0)
1096 printfPQExpBuffer(&tmpbuf, _("primary key, "));
1097 else if (strcmp(indisunique, "t") == 0)
1098 printfPQExpBuffer(&tmpbuf, _("unique, "));
1100 resetPQExpBuffer(&tmpbuf);
1101 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
1103 /* we assume here that index and table are in same schema */
1104 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
1105 schemaname, indtable);
1107 if (strlen(indpred))
1108 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
1110 if (strcmp(indisclustered, "t") == 0)
1111 appendPQExpBuffer(&tmpbuf, _(", clustered"));
1113 if (strcmp(indisvalid, "t") != 0)
1114 appendPQExpBuffer(&tmpbuf, _(", invalid"));
1116 printTableAddFooter(&cont, tmpbuf.data);
1117 add_tablespace_footer(&cont, tableinfo.relkind,
1118 tableinfo.tablespace, true);
1125 PGresult *result = NULL;
1127 /* Footer information about a view */
1128 printTableAddFooter(&cont, _("View definition:"));
1129 printTableAddFooter(&cont, view_def);
1132 if (tableinfo.hasrules)
1134 printfPQExpBuffer(&buf,
1135 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1136 "FROM pg_catalog.pg_rewrite r\n"
1137 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1",
1139 result = PSQLexec(buf.data, false);
1143 if (PQntuples(result) > 0)
1145 printTableAddFooter(&cont, _("Rules:"));
1146 for (i = 0; i < PQntuples(result); i++)
1148 const char *ruledef;
1150 /* Everything after "CREATE RULE" is echoed verbatim */
1151 ruledef = PQgetvalue(result, i, 1);
1154 printfPQExpBuffer(&buf, " %s", ruledef);
1155 printTableAddFooter(&cont, buf.data);
1161 else if (tableinfo.relkind == 'r')
1163 /* Footer information about a table */
1164 PGresult *result = NULL;
1168 if (tableinfo.hasindex)
1170 printfPQExpBuffer(&buf,
1171 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1172 if (pset.sversion >= 80200)
1173 appendPQExpBuffer(&buf, "i.indisvalid, ");
1175 appendPQExpBuffer(&buf, "true as indisvalid, ");
1176 appendPQExpBuffer(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)");
1177 if (pset.sversion >= 80000)
1178 appendPQExpBuffer(&buf, ", c2.reltablespace");
1179 appendPQExpBuffer(&buf,
1180 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
1181 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1182 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
1184 result = PSQLexec(buf.data, false);
1188 tuples = PQntuples(result);
1192 printTableAddFooter(&cont, _("Indexes:"));
1193 for (i = 0; i < tuples; i++)
1195 const char *indexdef;
1196 const char *usingpos;
1198 /* untranslated index name */
1199 printfPQExpBuffer(&buf, " \"%s\"",
1200 PQgetvalue(result, i, 0));
1202 /* Label as primary key or unique (but not both) */
1203 appendPQExpBuffer(&buf,
1204 strcmp(PQgetvalue(result, i, 1), "t") == 0
1206 (strcmp(PQgetvalue(result, i, 2), "t") == 0
1209 /* Everything after "USING" is echoed verbatim */
1210 indexdef = PQgetvalue(result, i, 5);
1211 usingpos = strstr(indexdef, " USING ");
1213 indexdef = usingpos + 7;
1215 appendPQExpBuffer(&buf, " %s", indexdef);
1217 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
1218 appendPQExpBuffer(&buf, " CLUSTER");
1220 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
1221 appendPQExpBuffer(&buf, " INVALID");
1223 printTableAddFooter(&cont, buf.data);
1225 /* Print tablespace of the index on the same line */
1226 if (pset.sversion >= 80000)
1227 add_tablespace_footer(&cont, 'i',
1228 atooid(PQgetvalue(result, i, 6)),
1235 /* print table (and column) check constraints */
1236 if (tableinfo.checks)
1238 printfPQExpBuffer(&buf,
1239 "SELECT r.conname, "
1240 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1241 "FROM pg_catalog.pg_constraint r\n"
1242 "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1",
1244 result = PSQLexec(buf.data, false);
1248 tuples = PQntuples(result);
1252 printTableAddFooter(&cont, _("Check constraints:"));
1253 for (i = 0; i < tuples; i++)
1255 /* untranslated contraint name and def */
1256 printfPQExpBuffer(&buf, " \"%s\" %s",
1257 PQgetvalue(result, i, 0),
1258 PQgetvalue(result, i, 1));
1260 printTableAddFooter(&cont, buf.data);
1266 /* print foreign-key constraints (there are none if no triggers) */
1267 if (tableinfo.triggers)
1269 printfPQExpBuffer(&buf,
1271 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1272 "FROM pg_catalog.pg_constraint r\n"
1273 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1",
1275 result = PSQLexec(buf.data, false);
1279 tuples = PQntuples(result);
1283 printTableAddFooter(&cont, _("Foreign-key constraints:"));
1284 for (i = 0; i < tuples; i++)
1286 /* untranslated constraint name and def */
1287 printfPQExpBuffer(&buf, " \"%s\" %s",
1288 PQgetvalue(result, i, 0),
1289 PQgetvalue(result, i, 1));
1291 printTableAddFooter(&cont, buf.data);
1297 /* print incoming foreign-key references (none if no triggers) */
1298 if (tableinfo.triggers)
1300 printfPQExpBuffer(&buf,
1301 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1302 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1303 "FROM pg_catalog.pg_constraint c\n"
1304 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
1306 result = PSQLexec(buf.data, false);
1310 tuples = PQntuples(result);
1314 printTableAddFooter(&cont, _("Referenced by:"));
1315 for (i = 0; i < tuples; i++)
1317 /* translator: the first %s is a FK name, the following are
1318 * a table name and the FK definition */
1319 printfPQExpBuffer(&buf, _(" \"%s\" IN %s %s"),
1320 PQgetvalue(result, i, 0),
1321 PQgetvalue(result, i, 1),
1322 PQgetvalue(result, i, 2));
1324 printTableAddFooter(&cont, buf.data);
1331 if (tableinfo.hasrules)
1333 if (pset.sversion >= 80300)
1335 printfPQExpBuffer(&buf,
1336 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1338 "FROM pg_catalog.pg_rewrite r\n"
1339 "WHERE r.ev_class = '%s' ORDER BY 1",
1344 printfPQExpBuffer(&buf,
1345 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1346 "'O'::char AS ev_enabled\n"
1347 "FROM pg_catalog.pg_rewrite r\n"
1348 "WHERE r.ev_class = '%s' ORDER BY 1",
1351 result = PSQLexec(buf.data, false);
1355 tuples = PQntuples(result);
1362 for (category = 0; category < 4; category++)
1364 have_heading = false;
1366 for (i = 0; i < tuples; i++)
1368 const char *ruledef;
1369 bool list_rule = false;
1374 if (*PQgetvalue(result, i, 2) == 'O')
1378 if (*PQgetvalue(result, i, 2) == 'D')
1382 if (*PQgetvalue(result, i, 2) == 'A')
1386 if (*PQgetvalue(result, i, 2) == 'R')
1398 printfPQExpBuffer(&buf, _("Rules:"));
1401 printfPQExpBuffer(&buf, _("Disabled rules:"));
1404 printfPQExpBuffer(&buf, _("Rules firing always:"));
1407 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1410 printTableAddFooter(&cont, buf.data);
1411 have_heading = true;
1414 /* Everything after "CREATE RULE" is echoed verbatim */
1415 ruledef = PQgetvalue(result, i, 1);
1417 printfPQExpBuffer(&buf, " %s", ruledef);
1418 printTableAddFooter(&cont, buf.data);
1425 /* print triggers (but ignore foreign-key triggers) */
1426 if (tableinfo.triggers)
1428 printfPQExpBuffer(&buf,
1430 "pg_catalog.pg_get_triggerdef(t.oid), "
1432 "FROM pg_catalog.pg_trigger t\n"
1433 "WHERE t.tgrelid = '%s' AND ",
1435 if (pset.sversion >= 80300)
1436 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
1438 appendPQExpBuffer(&buf,
1439 "(NOT tgisconstraint "
1441 " (SELECT 1 FROM pg_catalog.pg_depend d "
1442 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
1443 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
1444 appendPQExpBuffer(&buf, "\nORDER BY 1");
1446 result = PSQLexec(buf.data, false);
1450 tuples = PQntuples(result);
1458 * split the output into 4 different categories. Enabled triggers,
1459 * disabled triggers and the two special ALWAYS and REPLICA
1462 for (category = 0; category < 4; category++)
1464 have_heading = false;
1465 for (i = 0; i < tuples; i++)
1469 const char *usingpos;
1470 const char *tgenabled;
1472 /* Check if this trigger falls into the current category */
1473 tgenabled = PQgetvalue(result, i, 2);
1474 list_trigger = false;
1478 if (*tgenabled == 'O' || *tgenabled == 't')
1479 list_trigger = true;
1482 if (*tgenabled == 'D' || *tgenabled == 'f')
1483 list_trigger = true;
1486 if (*tgenabled == 'A')
1487 list_trigger = true;
1490 if (*tgenabled == 'R')
1491 list_trigger = true;
1494 if (list_trigger == false)
1497 /* Print the category heading once */
1498 if (have_heading == false)
1503 printfPQExpBuffer(&buf, _("Triggers:"));
1506 printfPQExpBuffer(&buf, _("Disabled triggers:"));
1509 printfPQExpBuffer(&buf, _("Triggers firing always:"));
1512 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
1516 printTableAddFooter(&cont, buf.data);
1517 have_heading = true;
1520 /* Everything after "TRIGGER" is echoed verbatim */
1521 tgdef = PQgetvalue(result, i, 1);
1522 usingpos = strstr(tgdef, " TRIGGER ");
1524 tgdef = usingpos + 9;
1526 printfPQExpBuffer(&buf, " %s", tgdef);
1527 printTableAddFooter(&cont, buf.data);
1534 /* print inherited tables */
1535 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);
1537 result = PSQLexec(buf.data, false);
1541 tuples = PQntuples(result);
1543 for (i = 0; i < tuples; i++)
1545 const char *s = _("Inherits");
1548 printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
1550 printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
1552 appendPQExpBuffer(&buf, ",");
1554 printTableAddFooter(&cont, buf.data);
1560 const char *s = _("Has OIDs");
1562 printfPQExpBuffer(&buf, "%s: %s", s,
1563 (tableinfo.hasoids ? _("yes") : _("no")));
1564 printTableAddFooter(&cont, buf.data);
1567 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
1571 printTable(&cont, pset.queryFout, pset.logfile);
1572 printTableCleanup(&cont);
1579 printTableCleanup(&cont);
1580 termPQExpBuffer(&buf);
1581 termPQExpBuffer(&title);
1582 termPQExpBuffer(&tmpbuf);
1584 if (tableinfo.relkind == 'S')
1586 for (ptr = seq_values; *ptr; ptr++)
1593 for (ptr = modifiers; *ptr; ptr++)
1608 * Add a tablespace description to a footer. If 'newline' is true, it is added
1609 * in a new line; otherwise it's appended to the current value of the last
1613 add_tablespace_footer(printTableContent *const cont, char relkind,
1614 Oid tablespace, const bool newline)
1616 /* relkinds for which we support tablespaces */
1617 if (relkind == 'r' || relkind == 'i')
1620 * We ignore the database default tablespace so that users not using
1621 * tablespaces don't need to know about them. This case also covers
1622 * pre-8.0 servers, for which tablespace will always be 0.
1624 if (tablespace != 0)
1626 PGresult *result = NULL;
1627 PQExpBufferData buf;
1629 initPQExpBuffer(&buf);
1630 printfPQExpBuffer(&buf,
1631 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
1632 "WHERE oid = '%u'", tablespace);
1633 result = PSQLexec(buf.data, false);
1636 /* Should always be the case, but.... */
1637 if (PQntuples(result) > 0)
1641 /* Add the tablespace as a new footer */
1642 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
1643 PQgetvalue(result, 0, 0));
1644 printTableAddFooter(cont, buf.data);
1648 /* Append the tablespace to the latest footer */
1649 printfPQExpBuffer(&buf, "%s", cont->footer->data);
1650 /* translator: before this string there's an index
1651 * description like '"foo_pkey" PRIMARY KEY, btree (a)' */
1652 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
1653 PQgetvalue(result, 0, 0));
1654 printTableSetFooter(cont, buf.data);
1658 termPQExpBuffer(&buf);
1666 * Describes roles. Any schema portion of the pattern is ignored.
1669 describeRoles(const char *pattern, bool verbose)
1671 PQExpBufferData buf;
1673 printTableContent cont;
1674 printTableOpt myopt = pset.popt.topt;
1679 const char align = 'l';
1682 initPQExpBuffer(&buf);
1684 if (pset.sversion >= 80100)
1686 printfPQExpBuffer(&buf,
1687 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
1688 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
1689 " r.rolconnlimit,\n"
1690 " ARRAY(SELECT b.rolname\n"
1691 " FROM pg_catalog.pg_auth_members m\n"
1692 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
1693 " WHERE m.member = r.oid) as memberof");
1695 if (verbose && pset.sversion >= 80200)
1697 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
1701 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
1703 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1704 NULL, "r.rolname", NULL, NULL);
1708 printfPQExpBuffer(&buf,
1709 "SELECT u.usename AS rolname,\n"
1710 " u.usesuper AS rolsuper,\n"
1711 " true AS rolinherit, false AS rolcreaterole,\n"
1712 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
1713 " -1 AS rolconnlimit,\n"
1714 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
1715 "\nFROM pg_catalog.pg_user u\n");
1717 processSQLNamePattern(pset.db, &buf, pattern, false, false,
1718 NULL, "u.usename", NULL, NULL);
1721 appendPQExpBuffer(&buf, "ORDER BY 1;");
1723 res = PSQLexec(buf.data, false);
1727 nrows = PQntuples(res);
1728 attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
1730 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
1732 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
1733 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
1734 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
1736 if (verbose && pset.sversion >= 80200)
1737 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
1739 for (i = 0; i < nrows; i++)
1741 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
1743 resetPQExpBuffer(&buf);
1744 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
1745 add_role_attribute(&buf, _("Superuser"));
1747 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
1748 add_role_attribute(&buf, _("No inheritance"));
1750 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
1751 add_role_attribute(&buf, _("Create role"));
1753 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
1754 add_role_attribute(&buf, _("Create DB"));
1756 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
1757 add_role_attribute(&buf, _("Cannot login"));
1759 conns = atoi(PQgetvalue(res, i, 6));
1763 appendPQExpBufferStr(&buf, "\n");
1766 appendPQExpBuffer(&buf, _("No connections"));
1767 else if (conns == 1)
1768 appendPQExpBuffer(&buf, _("1 connection"));
1770 appendPQExpBuffer(&buf, _("%d connections"), conns);
1773 attr[i] = pg_strdup(buf.data);
1775 printTableAddCell(&cont, attr[i], false);
1777 printTableAddCell(&cont, PQgetvalue(res, i, 7), false);
1779 if (verbose && pset.sversion >= 80200)
1780 printTableAddCell(&cont, PQgetvalue(res, i, 8), false);
1782 termPQExpBuffer(&buf);
1784 printTable(&cont, pset.queryFout, pset.logfile);
1785 printTableCleanup(&cont);
1787 for (i = 0; i < nrows; i++)
1796 add_role_attribute(PQExpBuffer buf, const char *const str)
1799 appendPQExpBufferStr(buf, "\n");
1801 appendPQExpBufferStr(buf, str);
1808 * handler for \d, \dt, etc.
1810 * tabtypes is an array of characters, specifying what info is desired:
1815 * S - system tables (pg_catalog)
1816 * (any order of the above is fine)
1819 listTables(const char *tabtypes, const char *pattern, bool verbose)
1821 bool showTables = strchr(tabtypes, 't') != NULL;
1822 bool showIndexes = strchr(tabtypes, 'i') != NULL;
1823 bool showViews = strchr(tabtypes, 'v') != NULL;
1824 bool showSeq = strchr(tabtypes, 's') != NULL;
1825 bool showSystem = strchr(tabtypes, 'S') != NULL;
1827 PQExpBufferData buf;
1829 printQueryOpt myopt = pset.popt;
1830 static const bool translate_columns[] = {false, false, true, false, false, false};
1832 if (!(showTables || showIndexes || showViews || showSeq))
1833 showTables = showViews = showSeq = true;
1835 initPQExpBuffer(&buf);
1838 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
1839 * for backwards compatibility.
1841 printfPQExpBuffer(&buf,
1842 "SELECT n.nspname as \"%s\",\n"
1843 " c.relname as \"%s\",\n"
1844 " 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"
1845 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
1846 gettext_noop("Schema"),
1847 gettext_noop("Name"),
1848 gettext_noop("table"),
1849 gettext_noop("view"),
1850 gettext_noop("index"),
1851 gettext_noop("sequence"),
1852 gettext_noop("special"),
1853 gettext_noop("Type"),
1854 gettext_noop("Owner"));
1857 appendPQExpBuffer(&buf,
1858 ",\n c2.relname as \"%s\"",
1859 gettext_noop("Table"));
1861 if (verbose && pset.sversion >= 80100)
1862 appendPQExpBuffer(&buf,
1863 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
1864 gettext_noop("Size"));
1866 appendPQExpBuffer(&buf,
1867 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
1868 gettext_noop("Description"));
1870 appendPQExpBuffer(&buf,
1871 "\nFROM pg_catalog.pg_class c"
1872 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
1874 appendPQExpBuffer(&buf,
1875 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
1876 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
1878 appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
1880 appendPQExpBuffer(&buf, "'r',");
1882 appendPQExpBuffer(&buf, "'v',");
1884 appendPQExpBuffer(&buf, "'i',");
1886 appendPQExpBuffer(&buf, "'S',");
1887 if (showSystem && showTables)
1888 appendPQExpBuffer(&buf, "'s',");
1889 appendPQExpBuffer(&buf, "''"); /* dummy */
1890 appendPQExpBuffer(&buf, ")\n");
1893 * If showSystem is specified, show only system objects (those in
1894 * pg_catalog). Otherwise, suppress system objects, including those in
1895 * pg_catalog and pg_toast. (We don't want to hide temp tables though.)
1898 appendPQExpBuffer(&buf,
1899 " AND n.nspname = 'pg_catalog'\n");
1901 appendPQExpBuffer(&buf,
1902 " AND n.nspname <> 'pg_catalog'\n"
1903 " AND n.nspname !~ '^pg_toast'\n");
1905 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1906 "n.nspname", "c.relname", NULL,
1907 "pg_catalog.pg_table_is_visible(c.oid)");
1909 appendPQExpBuffer(&buf, "ORDER BY 1,2;");
1911 res = PSQLexec(buf.data, false);
1912 termPQExpBuffer(&buf);
1916 if (PQntuples(res) == 0 && !pset.quiet)
1919 fprintf(pset.queryFout, _("No matching relations found.\n"));
1921 fprintf(pset.queryFout, _("No relations found.\n"));
1925 myopt.nullPrint = NULL;
1926 myopt.title = _("List of relations");
1927 myopt.translate_header = true;
1928 myopt.translate_columns = translate_columns;
1930 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1941 * Describes domains.
1944 listDomains(const char *pattern)
1946 PQExpBufferData buf;
1948 printQueryOpt myopt = pset.popt;
1950 initPQExpBuffer(&buf);
1952 printfPQExpBuffer(&buf,
1953 "SELECT n.nspname as \"%s\",\n"
1954 " t.typname as \"%s\",\n"
1955 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
1956 " CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
1957 " WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
1958 " WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
1961 " pg_catalog.pg_get_constraintdef(r.oid, true) as \"%s\"\n"
1962 "FROM pg_catalog.pg_type t\n"
1963 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
1964 " LEFT JOIN pg_catalog.pg_constraint r ON t.oid = r.contypid\n"
1965 "WHERE t.typtype = 'd'\n",
1966 gettext_noop("Schema"),
1967 gettext_noop("Name"),
1968 gettext_noop("Type"),
1969 gettext_noop("Modifier"),
1970 gettext_noop("Check"));
1972 processSQLNamePattern(pset.db, &buf, pattern, true, false,
1973 "n.nspname", "t.typname", NULL,
1974 "pg_catalog.pg_type_is_visible(t.oid)");
1976 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1978 res = PSQLexec(buf.data, false);
1979 termPQExpBuffer(&buf);
1983 myopt.nullPrint = NULL;
1984 myopt.title = _("List of domains");
1985 myopt.translate_header = true;
1987 printQuery(res, &myopt, pset.queryFout, pset.logfile);
1996 * Describes conversions.
1999 listConversions(const char *pattern)
2001 PQExpBufferData buf;
2003 printQueryOpt myopt = pset.popt;
2004 static const bool translate_columns[] = {false, false, false, false, true};
2006 initPQExpBuffer(&buf);
2008 printfPQExpBuffer(&buf,
2009 "SELECT n.nspname AS \"%s\",\n"
2010 " c.conname AS \"%s\",\n"
2011 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2012 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2013 " CASE WHEN c.condefault THEN '%s'\n"
2014 " ELSE '%s' END AS \"%s\"\n"
2015 "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
2016 "WHERE n.oid = c.connamespace\n",
2017 gettext_noop("Schema"),
2018 gettext_noop("Name"),
2019 gettext_noop("Source"),
2020 gettext_noop("Destination"),
2021 gettext_noop("yes"), gettext_noop("no"),
2022 gettext_noop("Default?"));
2024 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2025 "n.nspname", "c.conname", NULL,
2026 "pg_catalog.pg_conversion_is_visible(c.oid)");
2028 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2030 res = PSQLexec(buf.data, false);
2031 termPQExpBuffer(&buf);
2035 myopt.nullPrint = NULL;
2036 myopt.title = _("List of conversions");
2037 myopt.translate_header = true;
2038 myopt.translate_columns = translate_columns;
2040 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2052 listCasts(const char *pattern)
2054 PQExpBufferData buf;
2056 printQueryOpt myopt = pset.popt;
2057 static const bool translate_columns[] = {false, false, false, true};
2059 initPQExpBuffer(&buf);
2061 * We need left join here for binary casts. Also note that we don't
2062 * attempt to localize '(binary coercible)', because there's too much
2063 * risk of gettext translating a function name that happens to match
2064 * some string in the PO database.
2066 printfPQExpBuffer(&buf,
2067 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2068 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2069 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
2072 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2073 " WHEN c.castcontext = 'a' THEN '%s'\n"
2076 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2077 " ON c.castfunc = p.oid\n"
2079 gettext_noop("Source type"),
2080 gettext_noop("Target type"),
2081 gettext_noop("Function"),
2082 gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2083 gettext_noop("Implicit?"));
2085 res = PSQLexec(buf.data, false);
2086 termPQExpBuffer(&buf);
2090 myopt.nullPrint = NULL;
2091 myopt.title = _("List of casts");
2092 myopt.translate_header = true;
2093 myopt.translate_columns = translate_columns;
2095 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2104 * Describes schemas (namespaces)
2107 listSchemas(const char *pattern, bool verbose)
2109 PQExpBufferData buf;
2111 printQueryOpt myopt = pset.popt;
2113 initPQExpBuffer(&buf);
2114 printfPQExpBuffer(&buf,
2115 "SELECT n.nspname AS \"%s\",\n"
2116 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2117 gettext_noop("Name"),
2118 gettext_noop("Owner"));
2121 appendPQExpBuffer(&buf,
2122 ",\n n.nspacl as \"%s\","
2123 " pg_catalog.obj_description(n.oid, 'pg_namespace') as \"%s\"",
2124 gettext_noop("Access privileges"),
2125 gettext_noop("Description"));
2127 appendPQExpBuffer(&buf,
2128 "\nFROM pg_catalog.pg_namespace n\n"
2129 "WHERE (n.nspname !~ '^pg_temp_' OR\n"
2130 " n.nspname = (pg_catalog.current_schemas(true))[1])\n"); /* temp schema is first */
2132 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2133 NULL, "n.nspname", NULL,
2136 appendPQExpBuffer(&buf, "ORDER BY 1;");
2138 res = PSQLexec(buf.data, false);
2139 termPQExpBuffer(&buf);
2143 myopt.nullPrint = NULL;
2144 myopt.title = _("List of schemas");
2145 myopt.translate_header = true;
2147 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2156 * list text search parsers
2159 listTSParsers(const char *pattern, bool verbose)
2161 PQExpBufferData buf;
2163 printQueryOpt myopt = pset.popt;
2165 if (pset.sversion < 80300)
2167 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2168 pset.sversion / 10000, (pset.sversion / 100) % 100);
2173 return listTSParsersVerbose(pattern);
2175 initPQExpBuffer(&buf);
2177 printfPQExpBuffer(&buf,
2179 " n.nspname as \"%s\",\n"
2180 " p.prsname as \"%s\",\n"
2181 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2182 "FROM pg_catalog.pg_ts_parser p \n"
2183 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2184 gettext_noop("Schema"),
2185 gettext_noop("Name"),
2186 gettext_noop("Description")
2189 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2190 "n.nspname", "p.prsname", NULL,
2191 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2193 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2195 res = PSQLexec(buf.data, false);
2196 termPQExpBuffer(&buf);
2200 myopt.nullPrint = NULL;
2201 myopt.title = _("List of text search parsers");
2202 myopt.translate_header = true;
2204 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2211 * full description of parsers
2214 listTSParsersVerbose(const char *pattern)
2216 PQExpBufferData buf;
2220 initPQExpBuffer(&buf);
2222 printfPQExpBuffer(&buf,
2226 "FROM pg_catalog.pg_ts_parser p\n"
2227 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2230 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2231 "n.nspname", "p.prsname", NULL,
2232 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2234 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2236 res = PSQLexec(buf.data, false);
2237 termPQExpBuffer(&buf);
2241 if (PQntuples(res) == 0)
2244 fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
2250 for (i = 0; i < PQntuples(res); i++)
2253 const char *nspname = NULL;
2254 const char *prsname;
2256 oid = PQgetvalue(res, i, 0);
2257 if (!PQgetisnull(res, i, 1))
2258 nspname = PQgetvalue(res, i, 1);
2259 prsname = PQgetvalue(res, i, 2);
2261 if (!describeOneTSParser(oid, nspname, prsname))
2279 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
2281 PQExpBufferData buf;
2284 printQueryOpt myopt = pset.popt;
2285 static const bool translate_columns[] = {true, false, false};
2287 initPQExpBuffer(&buf);
2289 printfPQExpBuffer(&buf,
2290 "SELECT '%s' AS \"%s\", \n"
2291 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
2292 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
2293 " FROM pg_catalog.pg_ts_parser p \n"
2294 " WHERE p.oid = '%s' \n"
2297 " p.prstoken::pg_catalog.regproc, \n"
2298 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
2299 " FROM pg_catalog.pg_ts_parser p \n"
2300 " WHERE p.oid = '%s' \n"
2303 " p.prsend::pg_catalog.regproc, \n"
2304 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
2305 " FROM pg_catalog.pg_ts_parser p \n"
2306 " WHERE p.oid = '%s' \n"
2309 " p.prsheadline::pg_catalog.regproc, \n"
2310 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
2311 " FROM pg_catalog.pg_ts_parser p \n"
2312 " WHERE p.oid = '%s' \n"
2315 " p.prslextype::pg_catalog.regproc, \n"
2316 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
2317 " FROM pg_catalog.pg_ts_parser p \n"
2318 " WHERE p.oid = '%s' \n",
2319 gettext_noop("Start parse"),
2320 gettext_noop("Method"),
2321 gettext_noop("Function"),
2322 gettext_noop("Description"),
2324 gettext_noop("Get next token"),
2326 gettext_noop("End parse"),
2328 gettext_noop("Get headline"),
2330 gettext_noop("Get token types"),
2333 res = PSQLexec(buf.data, false);
2334 termPQExpBuffer(&buf);
2338 myopt.nullPrint = NULL;
2340 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
2342 sprintf(title, _("Text search parser \"%s\""), prsname);
2343 myopt.title = title;
2344 myopt.footers = NULL;
2345 myopt.default_footer = false;
2346 myopt.translate_header = true;
2347 myopt.translate_columns = translate_columns;
2349 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2353 initPQExpBuffer(&buf);
2355 printfPQExpBuffer(&buf,
2356 "SELECT t.alias as \"%s\", \n"
2357 " t.description as \"%s\" \n"
2358 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
2360 gettext_noop("Token name"),
2361 gettext_noop("Description"),
2364 res = PSQLexec(buf.data, false);
2365 termPQExpBuffer(&buf);
2369 myopt.nullPrint = NULL;
2371 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
2373 sprintf(title, _("Token types for parser \"%s\""), prsname);
2374 myopt.title = title;
2375 myopt.footers = NULL;
2376 myopt.default_footer = true;
2377 myopt.translate_header = true;
2378 myopt.translate_columns = NULL;
2380 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2389 * list text search dictionaries
2392 listTSDictionaries(const char *pattern, bool verbose)
2394 PQExpBufferData buf;
2396 printQueryOpt myopt = pset.popt;
2398 if (pset.sversion < 80300)
2400 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2401 pset.sversion / 10000, (pset.sversion / 100) % 100);
2405 initPQExpBuffer(&buf);
2407 printfPQExpBuffer(&buf,
2409 " n.nspname as \"%s\",\n"
2410 " d.dictname as \"%s\",\n",
2411 gettext_noop("Schema"),
2412 gettext_noop("Name"));
2416 appendPQExpBuffer(&buf,
2417 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
2418 " pg_catalog.pg_ts_template t \n"
2419 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
2420 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
2421 " d.dictinitoption as \"%s\", \n",
2422 gettext_noop("Template"),
2423 gettext_noop("Init options"));
2426 appendPQExpBuffer(&buf,
2427 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
2428 gettext_noop("Description"));
2430 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
2431 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
2433 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2434 "n.nspname", "d.dictname", NULL,
2435 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
2437 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2439 res = PSQLexec(buf.data, false);
2440 termPQExpBuffer(&buf);
2444 myopt.nullPrint = NULL;
2445 myopt.title = _("List of text search dictionaries");
2446 myopt.translate_header = true;
2448 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2457 * list text search templates
2460 listTSTemplates(const char *pattern, bool verbose)
2462 PQExpBufferData buf;
2464 printQueryOpt myopt = pset.popt;
2466 if (pset.sversion < 80300)
2468 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2469 pset.sversion / 10000, (pset.sversion / 100) % 100);
2473 initPQExpBuffer(&buf);
2476 printfPQExpBuffer(&buf,
2478 " n.nspname AS \"%s\",\n"
2479 " t.tmplname AS \"%s\",\n"
2480 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
2481 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
2482 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2483 gettext_noop("Schema"),
2484 gettext_noop("Name"),
2485 gettext_noop("Init"),
2486 gettext_noop("Lexize"),
2487 gettext_noop("Description"));
2489 printfPQExpBuffer(&buf,
2491 " n.nspname AS \"%s\",\n"
2492 " t.tmplname AS \"%s\",\n"
2493 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2494 gettext_noop("Schema"),
2495 gettext_noop("Name"),
2496 gettext_noop("Description"));
2498 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
2499 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
2501 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2502 "n.nspname", "t.tmplname", NULL,
2503 "pg_catalog.pg_ts_template_is_visible(t.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 templates");
2514 myopt.translate_header = true;
2516 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2525 * list text search configurations
2528 listTSConfigs(const char *pattern, bool verbose)
2530 PQExpBufferData buf;
2532 printQueryOpt myopt = pset.popt;
2534 if (pset.sversion < 80300)
2536 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2537 pset.sversion / 10000, (pset.sversion / 100) % 100);
2542 return listTSConfigsVerbose(pattern);
2544 initPQExpBuffer(&buf);
2546 printfPQExpBuffer(&buf,
2548 " n.nspname as \"%s\",\n"
2549 " c.cfgname as \"%s\",\n"
2550 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
2551 "FROM pg_catalog.pg_ts_config c\n"
2552 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
2553 gettext_noop("Schema"),
2554 gettext_noop("Name"),
2555 gettext_noop("Description")
2558 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2559 "n.nspname", "c.cfgname", NULL,
2560 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2562 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2564 res = PSQLexec(buf.data, false);
2565 termPQExpBuffer(&buf);
2569 myopt.nullPrint = NULL;
2570 myopt.title = _("List of text search configurations");
2571 myopt.translate_header = true;
2573 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2580 listTSConfigsVerbose(const char *pattern)
2582 PQExpBufferData buf;
2586 initPQExpBuffer(&buf);
2588 printfPQExpBuffer(&buf,
2589 "SELECT c.oid, c.cfgname,\n"
2592 " np.nspname as pnspname \n"
2593 "FROM pg_catalog.pg_ts_config c \n"
2594 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
2595 " pg_catalog.pg_ts_parser p \n"
2596 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
2597 "WHERE p.oid = c.cfgparser\n"
2600 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2601 "n.nspname", "c.cfgname", NULL,
2602 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2604 appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
2606 res = PSQLexec(buf.data, false);
2607 termPQExpBuffer(&buf);
2611 if (PQntuples(res) == 0)
2614 fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
2620 for (i = 0; i < PQntuples(res); i++)
2623 const char *cfgname;
2624 const char *nspname = NULL;
2625 const char *prsname;
2626 const char *pnspname = NULL;
2628 oid = PQgetvalue(res, i, 0);
2629 cfgname = PQgetvalue(res, i, 1);
2630 if (!PQgetisnull(res, i, 2))
2631 nspname = PQgetvalue(res, i, 2);
2632 prsname = PQgetvalue(res, i, 3);
2633 if (!PQgetisnull(res, i, 4))
2634 pnspname = PQgetvalue(res, i, 4);
2636 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
2654 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
2655 const char *pnspname, const char *prsname)
2657 PQExpBufferData buf,
2660 printQueryOpt myopt = pset.popt;
2662 initPQExpBuffer(&buf);
2664 printfPQExpBuffer(&buf,
2666 " ( SELECT t.alias FROM \n"
2667 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
2668 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
2669 " pg_catalog.btrim( \n"
2670 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
2671 " FROM pg_catalog.pg_ts_config_map AS mm \n"
2672 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
2673 " ORDER BY mapcfg, maptokentype, mapseqno \n"
2674 " ) :: pg_catalog.text , \n"
2675 " '{}') AS \"%s\" \n"
2676 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
2677 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
2678 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
2680 gettext_noop("Token"),
2681 gettext_noop("Dictionaries"),
2684 res = PSQLexec(buf.data, false);
2685 termPQExpBuffer(&buf);
2689 initPQExpBuffer(&title);
2692 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
2695 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
2699 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
2702 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
2705 myopt.nullPrint = NULL;
2706 myopt.title = title.data;
2707 myopt.footers = NULL;
2708 myopt.default_footer = false;
2709 myopt.translate_header = true;
2711 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2713 termPQExpBuffer(&title);