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-2009, PostgreSQL Global Development Group
13 #include "postgres_fe.h"
19 #include "dumputils.h"
23 #include "variables.h"
26 static bool describeOneTableDetails(const char *schemaname
,
27 const char *relationname
,
30 static void add_tablespace_footer(printTableContent
*const cont
, char relkind
,
31 Oid tablespace
, const bool newline
);
32 static void add_role_attribute(PQExpBuffer buf
, const char *const str
);
33 static bool listTSParsersVerbose(const char *pattern
);
34 static bool describeOneTSParser(const char *oid
, const char *nspname
,
36 static bool listTSConfigsVerbose(const char *pattern
);
37 static bool describeOneTSConfig(const char *oid
, const char *nspname
,
39 const char *pnspname
, const char *prsname
);
40 static void printACLColumn(PQExpBuffer buf
, const char *colname
);
44 * Handlers for various slash commands displaying some sort of list
45 * of things in the database.
47 * Note: try to format the queries to look nice in -E output.
53 * Takes an optional regexp to select particular aggregates
56 describeAggregates(const char *pattern
, bool verbose
, bool showSystem
)
60 printQueryOpt myopt
= pset
.popt
;
62 initPQExpBuffer(&buf
);
64 printfPQExpBuffer(&buf
,
65 "SELECT n.nspname as \"%s\",\n"
66 " p.proname AS \"%s\",\n"
67 " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
68 gettext_noop("Schema"),
70 gettext_noop("Result data type"));
72 if (pset
.sversion
>= 80200)
73 appendPQExpBuffer(&buf
,
74 " CASE WHEN p.pronargs = 0\n"
75 " THEN CAST('*' AS pg_catalog.text)\n"
77 " pg_catalog.array_to_string(ARRAY(\n"
79 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
81 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
84 gettext_noop("Argument data types"));
86 appendPQExpBuffer(&buf
,
87 " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
88 gettext_noop("Argument data types"));
90 appendPQExpBuffer(&buf
,
91 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
92 "FROM pg_catalog.pg_proc p\n"
93 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
95 gettext_noop("Description"));
97 if (!showSystem
&& !pattern
)
98 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
99 " AND n.nspname <> 'information_schema'\n");
101 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
102 "n.nspname", "p.proname", NULL
,
103 "pg_catalog.pg_function_is_visible(p.oid)");
105 appendPQExpBuffer(&buf
, "ORDER BY 1, 2, 4;");
107 res
= PSQLexec(buf
.data
, false);
108 termPQExpBuffer(&buf
);
112 myopt
.nullPrint
= NULL
;
113 myopt
.title
= _("List of aggregate functions");
114 myopt
.translate_header
= true;
116 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
123 * Takes an optional regexp to select particular tablespaces
126 describeTablespaces(const char *pattern
, bool verbose
)
130 printQueryOpt myopt
= pset
.popt
;
132 if (pset
.sversion
< 80000)
134 fprintf(stderr
, _("The server (version %d.%d) does not support tablespaces.\n"),
135 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
139 initPQExpBuffer(&buf
);
141 printfPQExpBuffer(&buf
,
142 "SELECT spcname AS \"%s\",\n"
143 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
144 " spclocation AS \"%s\"",
145 gettext_noop("Name"),
146 gettext_noop("Owner"),
147 gettext_noop("Location"));
151 appendPQExpBuffer(&buf
, ",\n ");
152 printACLColumn(&buf
, "spcacl");
155 if (verbose
&& pset
.sversion
>= 80200)
156 appendPQExpBuffer(&buf
,
157 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
158 gettext_noop("Description"));
160 appendPQExpBuffer(&buf
,
161 "\nFROM pg_catalog.pg_tablespace\n");
163 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
164 NULL
, "spcname", NULL
,
167 appendPQExpBuffer(&buf
, "ORDER BY 1;");
169 res
= PSQLexec(buf
.data
, false);
170 termPQExpBuffer(&buf
);
174 myopt
.nullPrint
= NULL
;
175 myopt
.title
= _("List of tablespaces");
176 myopt
.translate_header
= true;
178 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
186 * Takes an optional regexp to select particular functions.
188 * As with \d, you can specify the kinds of functions you want:
195 * and you can mix and match these in any order.
198 describeFunctions(const char *functypes
, const char *pattern
, bool verbose
, bool showSystem
)
200 bool showAggregate
= strchr(functypes
, 'a') != NULL
;
201 bool showNormal
= strchr(functypes
, 'n') != NULL
;
202 bool showTrigger
= strchr(functypes
, 't') != NULL
;
203 bool showWindow
= strchr(functypes
, 'w') != NULL
;
207 printQueryOpt myopt
= pset
.popt
;
208 static const bool translate_columns
[] = {false, false, false, false, true, true, false, false, false, false};
210 if (strlen(functypes
) != strspn(functypes
, "antwS+"))
212 fprintf(stderr
, _("\\df only takes [antwS+] as options\n"));
216 if (showWindow
&& pset
.sversion
< 80400)
218 fprintf(stderr
, _("\\df does not take a \"w\" option with server version %d.%d\n"),
219 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
223 if (!showAggregate
&& !showNormal
&& !showTrigger
&& !showWindow
)
225 showAggregate
= showNormal
= showTrigger
= true;
226 if (pset
.sversion
>= 80400)
230 initPQExpBuffer(&buf
);
232 printfPQExpBuffer(&buf
,
233 "SELECT n.nspname as \"%s\",\n"
234 " p.proname as \"%s\",\n",
235 gettext_noop("Schema"),
236 gettext_noop("Name"));
238 if (pset
.sversion
>= 80400)
239 appendPQExpBuffer(&buf
,
240 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
241 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
243 " WHEN p.proisagg THEN '%s'\n"
244 " WHEN p.proiswindow THEN '%s'\n"
245 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
248 gettext_noop("Result data type"),
249 gettext_noop("Argument data types"),
250 /* translator: "agg" is short for "aggregate" */
252 gettext_noop("window"),
253 gettext_noop("trigger"),
254 gettext_noop("normal"),
255 gettext_noop("Type"));
256 else if (pset
.sversion
>= 80100)
257 appendPQExpBuffer(&buf
,
258 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
259 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
260 " CASE WHEN proallargtypes IS NOT NULL THEN\n"
261 " pg_catalog.array_to_string(ARRAY(\n"
264 " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
265 " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
266 " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
267 " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
270 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
271 " ELSE p.proargnames[s.i] || ' ' \n"
273 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
275 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
278 " pg_catalog.array_to_string(ARRAY(\n"
281 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
282 " ELSE p.proargnames[s.i+1] || ' '\n"
284 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
286 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
290 " WHEN p.proisagg THEN '%s'\n"
291 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
294 gettext_noop("Result data type"),
295 gettext_noop("Argument data types"),
296 /* translator: "agg" is short for "aggregate" */
298 gettext_noop("trigger"),
299 gettext_noop("normal"),
300 gettext_noop("Type"));
302 appendPQExpBuffer(&buf
,
303 " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
304 " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
305 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
307 " WHEN p.proisagg THEN '%s'\n"
308 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
311 gettext_noop("Result data type"),
312 gettext_noop("Argument data types"),
313 /* translator: "agg" is short for "aggregate" */
315 gettext_noop("trigger"),
316 gettext_noop("normal"),
317 gettext_noop("Type"));
320 appendPQExpBuffer(&buf
,
322 " WHEN p.provolatile = 'i' THEN '%s'\n"
323 " WHEN p.provolatile = 's' THEN '%s'\n"
324 " WHEN p.provolatile = 'v' THEN '%s'\n"
326 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
327 " l.lanname as \"%s\",\n"
328 " p.prosrc as \"%s\",\n"
329 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
330 gettext_noop("immutable"),
331 gettext_noop("stable"),
332 gettext_noop("volatile"),
333 gettext_noop("Volatility"),
334 gettext_noop("Owner"),
335 gettext_noop("Language"),
336 gettext_noop("Source code"),
337 gettext_noop("Description"));
339 appendPQExpBuffer(&buf
,
340 "\nFROM pg_catalog.pg_proc p"
341 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
344 appendPQExpBuffer(&buf
,
345 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
349 /* filter by function type, if requested */
350 if (showNormal
&& showAggregate
&& showTrigger
&& showWindow
)
357 appendPQExpBuffer(&buf
, " AND ");
360 appendPQExpBuffer(&buf
, "WHERE ");
363 appendPQExpBuffer(&buf
, "NOT p.proisagg\n");
368 appendPQExpBuffer(&buf
, " AND ");
371 appendPQExpBuffer(&buf
, "WHERE ");
374 appendPQExpBuffer(&buf
, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
376 if (!showWindow
&& pset
.sversion
>= 80400)
379 appendPQExpBuffer(&buf
, " AND ");
382 appendPQExpBuffer(&buf
, "WHERE ");
385 appendPQExpBuffer(&buf
, "NOT p.proiswindow\n");
390 bool needs_or
= false;
392 appendPQExpBuffer(&buf
, "WHERE (\n ");
394 /* Note: at least one of these must be true ... */
397 appendPQExpBuffer(&buf
, "p.proisagg\n");
403 appendPQExpBuffer(&buf
, " OR ");
404 appendPQExpBuffer(&buf
,
405 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
411 appendPQExpBuffer(&buf
, " OR ");
412 appendPQExpBuffer(&buf
, "p.proiswindow\n");
415 appendPQExpBuffer(&buf
, " )\n");
418 processSQLNamePattern(pset
.db
, &buf
, pattern
, have_where
, true,
419 "n.nspname", "p.proname", NULL
,
420 "pg_catalog.pg_function_is_visible(p.oid)");
422 if (!showSystem
&& !pattern
)
423 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
424 " AND n.nspname <> 'information_schema'\n");
426 appendPQExpBuffer(&buf
, "ORDER BY 1, 2, 4;");
428 res
= PSQLexec(buf
.data
, false);
429 termPQExpBuffer(&buf
);
433 myopt
.nullPrint
= NULL
;
434 myopt
.title
= _("List of functions");
435 myopt
.translate_header
= true;
436 myopt
.translate_columns
= translate_columns
;
438 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
451 describeTypes(const char *pattern
, bool verbose
, bool showSystem
)
455 printQueryOpt myopt
= pset
.popt
;
457 initPQExpBuffer(&buf
);
459 printfPQExpBuffer(&buf
,
460 "SELECT n.nspname as \"%s\",\n"
461 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
462 gettext_noop("Schema"),
463 gettext_noop("Name"));
465 appendPQExpBuffer(&buf
,
466 " t.typname AS \"%s\",\n"
467 " CASE WHEN t.typrelid != 0\n"
468 " THEN CAST('tuple' AS pg_catalog.text)\n"
469 " WHEN t.typlen < 0\n"
470 " THEN CAST('var' AS pg_catalog.text)\n"
471 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
473 gettext_noop("Internal name"),
474 gettext_noop("Size"));
475 if (verbose
&& pset
.sversion
>= 80300)
476 appendPQExpBuffer(&buf
,
477 " pg_catalog.array_to_string(\n"
479 " SELECT e.enumlabel\n"
480 " FROM pg_catalog.pg_enum e\n"
481 " WHERE e.enumtypid = t.oid\n"
486 gettext_noop("Elements"));
488 appendPQExpBuffer(&buf
,
489 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
490 gettext_noop("Description"));
492 appendPQExpBuffer(&buf
, "FROM pg_catalog.pg_type t\n"
493 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
496 * do not include complex types (typrelid!=0) unless they are standalone
499 appendPQExpBuffer(&buf
, "WHERE (t.typrelid = 0 ");
500 appendPQExpBuffer(&buf
, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
501 "WHERE c.oid = t.typrelid))\n");
504 * do not include array types (before 8.3 we have to use the assumption
505 * that their names start with underscore)
507 if (pset
.sversion
>= 80300)
508 appendPQExpBuffer(&buf
, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
510 appendPQExpBuffer(&buf
, " AND t.typname !~ '^_'\n");
512 if (!showSystem
&& !pattern
)
513 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
514 " AND n.nspname <> 'information_schema'\n");
516 /* Match name pattern against either internal or external name */
517 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
518 "n.nspname", "t.typname",
519 "pg_catalog.format_type(t.oid, NULL)",
520 "pg_catalog.pg_type_is_visible(t.oid)");
522 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
524 res
= PSQLexec(buf
.data
, false);
525 termPQExpBuffer(&buf
);
529 myopt
.nullPrint
= NULL
;
530 myopt
.title
= _("List of data types");
531 myopt
.translate_header
= true;
533 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
543 describeOperators(const char *pattern
, bool showSystem
)
547 printQueryOpt myopt
= pset
.popt
;
549 initPQExpBuffer(&buf
);
551 printfPQExpBuffer(&buf
,
552 "SELECT n.nspname as \"%s\",\n"
553 " o.oprname AS \"%s\",\n"
554 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
555 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
556 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
557 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
558 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
559 "FROM pg_catalog.pg_operator o\n"
560 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
561 gettext_noop("Schema"),
562 gettext_noop("Name"),
563 gettext_noop("Left arg type"),
564 gettext_noop("Right arg type"),
565 gettext_noop("Result type"),
566 gettext_noop("Description"));
568 if (!showSystem
&& !pattern
)
569 appendPQExpBuffer(&buf
, "WHERE n.nspname <> 'pg_catalog'\n"
570 " AND n.nspname <> 'information_schema'\n");
572 processSQLNamePattern(pset
.db
, &buf
, pattern
, !showSystem
&& !pattern
, true,
573 "n.nspname", "o.oprname", NULL
,
574 "pg_catalog.pg_operator_is_visible(o.oid)");
576 appendPQExpBuffer(&buf
, "ORDER BY 1, 2, 3, 4;");
578 res
= PSQLexec(buf
.data
, false);
579 termPQExpBuffer(&buf
);
583 myopt
.nullPrint
= NULL
;
584 myopt
.title
= _("List of operators");
585 myopt
.translate_header
= true;
587 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
597 * for \l, \list, and -l switch
600 listAllDbs(bool verbose
)
604 printQueryOpt myopt
= pset
.popt
;
606 initPQExpBuffer(&buf
);
608 printfPQExpBuffer(&buf
,
609 "SELECT d.datname as \"%s\",\n"
610 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
611 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
612 gettext_noop("Name"),
613 gettext_noop("Owner"),
614 gettext_noop("Encoding"));
615 if (pset
.sversion
>= 80400)
616 appendPQExpBuffer(&buf
,
617 " d.datcollate as \"%s\",\n"
618 " d.datctype as \"%s\",\n",
619 gettext_noop("Collation"),
620 gettext_noop("Ctype"));
621 appendPQExpBuffer(&buf
, " ");
622 printACLColumn(&buf
, "d.datacl");
623 if (verbose
&& pset
.sversion
>= 80200)
624 appendPQExpBuffer(&buf
,
625 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
626 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
627 " ELSE 'No Access'\n"
629 gettext_noop("Size"));
630 if (verbose
&& pset
.sversion
>= 80000)
631 appendPQExpBuffer(&buf
,
632 ",\n t.spcname as \"%s\"",
633 gettext_noop("Tablespace"));
634 if (verbose
&& pset
.sversion
>= 80200)
635 appendPQExpBuffer(&buf
,
636 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
637 gettext_noop("Description"));
638 appendPQExpBuffer(&buf
,
639 "\nFROM pg_catalog.pg_database d\n");
640 if (verbose
&& pset
.sversion
>= 80000)
641 appendPQExpBuffer(&buf
,
642 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
643 appendPQExpBuffer(&buf
, "ORDER BY 1;");
644 res
= PSQLexec(buf
.data
, false);
645 termPQExpBuffer(&buf
);
649 myopt
.nullPrint
= NULL
;
650 myopt
.title
= _("List of databases");
651 myopt
.translate_header
= true;
653 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
661 * List Tables' Grant/Revoke Permissions
662 * \z (now also \dp -- perhaps more mnemonic)
665 permissionsList(const char *pattern
)
669 printQueryOpt myopt
= pset
.popt
;
670 static const bool translate_columns
[] = {false, false, true, false, false};
672 initPQExpBuffer(&buf
);
675 * we ignore indexes and toast tables since they have no meaningful rights
677 printfPQExpBuffer(&buf
,
678 "SELECT n.nspname as \"%s\",\n"
679 " c.relname as \"%s\",\n"
680 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' END as \"%s\",\n"
682 gettext_noop("Schema"),
683 gettext_noop("Name"),
684 gettext_noop("table"), gettext_noop("view"), gettext_noop("sequence"),
685 gettext_noop("Type"));
687 printACLColumn(&buf
, "c.relacl");
689 if (pset
.sversion
>= 80400)
690 appendPQExpBuffer(&buf
,
691 ",\n pg_catalog.array_to_string(ARRAY(\n"
692 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
693 " FROM pg_catalog.pg_attribute a\n"
694 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
695 " ), E'\\n') AS \"%s\"",
696 gettext_noop("Column access privileges"));
698 appendPQExpBuffer(&buf
, "\nFROM pg_catalog.pg_class c\n"
699 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
700 "WHERE c.relkind IN ('r', 'v', 'S')\n");
703 * Unless a schema pattern is specified, we suppress system and temp
704 * tables, since they normally aren't very interesting from a permissions
705 * point of view. You can see 'em by explicit request though, eg with \z
708 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
709 "n.nspname", "c.relname", NULL
,
710 "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)");
712 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
714 res
= PSQLexec(buf
.data
, false);
717 termPQExpBuffer(&buf
);
721 myopt
.nullPrint
= NULL
;
722 printfPQExpBuffer(&buf
, _("Access privileges"));
723 myopt
.title
= buf
.data
;
724 myopt
.translate_header
= true;
725 myopt
.translate_columns
= translate_columns
;
727 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
729 termPQExpBuffer(&buf
);
737 * Get object comments
741 * Note: This only lists things that actually have a description. For complete
742 * lists of things, there are other \d? commands.
745 objectDescription(const char *pattern
, bool showSystem
)
749 printQueryOpt myopt
= pset
.popt
;
750 static const bool translate_columns
[] = {false, false, true, false};
752 initPQExpBuffer(&buf
);
754 appendPQExpBuffer(&buf
,
755 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
757 gettext_noop("Schema"),
758 gettext_noop("Name"),
759 gettext_noop("Object"),
760 gettext_noop("Description"));
762 /* Aggregate descriptions */
763 appendPQExpBuffer(&buf
,
764 " SELECT p.oid as oid, p.tableoid as tableoid,\n"
765 " n.nspname as nspname,\n"
766 " CAST(p.proname AS pg_catalog.text) as name,"
767 " CAST('%s' AS pg_catalog.text) as object\n"
768 " FROM pg_catalog.pg_proc p\n"
769 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
770 " WHERE p.proisagg\n",
771 gettext_noop("aggregate"));
773 if (!showSystem
&& !pattern
)
774 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
775 " AND n.nspname <> 'information_schema'\n");
777 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
778 "n.nspname", "p.proname", NULL
,
779 "pg_catalog.pg_function_is_visible(p.oid)");
781 /* Function descriptions */
782 appendPQExpBuffer(&buf
,
784 " SELECT p.oid as oid, p.tableoid as tableoid,\n"
785 " n.nspname as nspname,\n"
786 " CAST(p.proname AS pg_catalog.text) as name,"
787 " CAST('%s' AS pg_catalog.text) as object\n"
788 " FROM pg_catalog.pg_proc p\n"
789 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
790 " WHERE NOT p.proisagg\n",
791 gettext_noop("function"));
793 if (!showSystem
&& !pattern
)
794 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
795 " AND n.nspname <> 'information_schema'\n");
797 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
798 "n.nspname", "p.proname", NULL
,
799 "pg_catalog.pg_function_is_visible(p.oid)");
801 /* Operator descriptions (only if operator has its own comment) */
802 appendPQExpBuffer(&buf
,
804 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
805 " n.nspname as nspname,\n"
806 " CAST(o.oprname AS pg_catalog.text) as name,"
807 " CAST('%s' AS pg_catalog.text) as object\n"
808 " FROM pg_catalog.pg_operator o\n"
809 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
810 gettext_noop("operator"));
812 if (!showSystem
&& !pattern
)
813 appendPQExpBuffer(&buf
, "WHERE n.nspname <> 'pg_catalog'\n"
814 " AND n.nspname <> 'information_schema'\n");
816 processSQLNamePattern(pset
.db
, &buf
, pattern
, !showSystem
&& !pattern
, false,
817 "n.nspname", "o.oprname", NULL
,
818 "pg_catalog.pg_operator_is_visible(o.oid)");
820 /* Type description */
821 appendPQExpBuffer(&buf
,
823 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
824 " n.nspname as nspname,\n"
825 " pg_catalog.format_type(t.oid, NULL) as name,"
826 " CAST('%s' AS pg_catalog.text) as object\n"
827 " FROM pg_catalog.pg_type t\n"
828 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n",
829 gettext_noop("data type"));
831 if (!showSystem
&& !pattern
)
832 appendPQExpBuffer(&buf
, "WHERE n.nspname <> 'pg_catalog'\n"
833 " AND n.nspname <> 'information_schema'\n");
835 processSQLNamePattern(pset
.db
, &buf
, pattern
, !showSystem
&& !pattern
, false,
836 "n.nspname", "pg_catalog.format_type(t.oid, NULL)",
838 "pg_catalog.pg_type_is_visible(t.oid)");
840 /* Relation (tables, views, indexes, sequences) descriptions */
841 appendPQExpBuffer(&buf
,
843 " SELECT c.oid as oid, c.tableoid as tableoid,\n"
844 " n.nspname as nspname,\n"
845 " CAST(c.relname AS pg_catalog.text) as name,\n"
847 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' END"
848 " AS pg_catalog.text) as object\n"
849 " FROM pg_catalog.pg_class c\n"
850 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
851 " WHERE c.relkind IN ('r', 'v', 'i', 'S')\n",
852 gettext_noop("table"),
853 gettext_noop("view"),
854 gettext_noop("index"),
855 gettext_noop("sequence"));
857 if (!showSystem
&& !pattern
)
858 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
859 " AND n.nspname <> 'information_schema'\n");
861 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
862 "n.nspname", "c.relname", NULL
,
863 "pg_catalog.pg_table_is_visible(c.oid)");
865 /* Rule description (ignore rules for views) */
866 appendPQExpBuffer(&buf
,
868 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
869 " n.nspname as nspname,\n"
870 " CAST(r.rulename AS pg_catalog.text) as name,"
871 " CAST('%s' AS pg_catalog.text) as object\n"
872 " FROM pg_catalog.pg_rewrite r\n"
873 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
874 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
875 " WHERE r.rulename != '_RETURN'\n",
876 gettext_noop("rule"));
878 if (!showSystem
&& !pattern
)
879 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
880 " AND n.nspname <> 'information_schema'\n");
882 /* XXX not sure what to do about visibility rule here? */
883 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
884 "n.nspname", "r.rulename", NULL
,
885 "pg_catalog.pg_table_is_visible(c.oid)");
887 /* Trigger description */
888 appendPQExpBuffer(&buf
,
890 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
891 " n.nspname as nspname,\n"
892 " CAST(t.tgname AS pg_catalog.text) as name,"
893 " CAST('%s' AS pg_catalog.text) as object\n"
894 " FROM pg_catalog.pg_trigger t\n"
895 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
896 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
897 gettext_noop("trigger"));
899 if (!showSystem
&& !pattern
)
900 appendPQExpBuffer(&buf
, "WHERE n.nspname <> 'pg_catalog'\n"
901 " AND n.nspname <> 'information_schema'\n");
903 /* XXX not sure what to do about visibility rule here? */
904 processSQLNamePattern(pset
.db
, &buf
, pattern
, !showSystem
&& !pattern
, false,
905 "n.nspname", "t.tgname", NULL
,
906 "pg_catalog.pg_table_is_visible(c.oid)");
908 appendPQExpBuffer(&buf
,
910 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
912 appendPQExpBuffer(&buf
, "ORDER BY 1, 2, 3;");
914 res
= PSQLexec(buf
.data
, false);
915 termPQExpBuffer(&buf
);
919 myopt
.nullPrint
= NULL
;
920 myopt
.title
= _("Object descriptions");
921 myopt
.translate_header
= true;
922 myopt
.translate_columns
= translate_columns
;
924 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
932 * describeTableDetails (for \d)
934 * This routine finds the tables to be displayed, and calls
935 * describeOneTableDetails for each one.
937 * verbose: if true, this is \d+
940 describeTableDetails(const char *pattern
, bool verbose
, bool showSystem
)
946 initPQExpBuffer(&buf
);
948 printfPQExpBuffer(&buf
,
952 "FROM pg_catalog.pg_class c\n"
953 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
955 if (!showSystem
&& !pattern
)
956 appendPQExpBuffer(&buf
, "WHERE n.nspname <> 'pg_catalog'\n"
957 " AND n.nspname <> 'information_schema'\n");
959 processSQLNamePattern(pset
.db
, &buf
, pattern
, !showSystem
&& !pattern
, false,
960 "n.nspname", "c.relname", NULL
,
961 "pg_catalog.pg_table_is_visible(c.oid)");
963 appendPQExpBuffer(&buf
, "ORDER BY 2, 3;");
965 res
= PSQLexec(buf
.data
, false);
966 termPQExpBuffer(&buf
);
970 if (PQntuples(res
) == 0)
973 fprintf(stderr
, _("Did not find any relation named \"%s\".\n"),
979 for (i
= 0; i
< PQntuples(res
); i
++)
985 oid
= PQgetvalue(res
, i
, 0);
986 nspname
= PQgetvalue(res
, i
, 1);
987 relname
= PQgetvalue(res
, i
, 2);
989 if (!describeOneTableDetails(nspname
, relname
, oid
, verbose
))
1006 * describeOneTableDetails (for \d)
1008 * Unfortunately, the information presented here is so complicated that it
1009 * cannot be done in a single query. So we have to assemble the printed table
1010 * by hand and pass it to the underlying printTable() function.
1013 describeOneTableDetails(const char *schemaname
,
1014 const char *relationname
,
1018 PQExpBufferData buf
;
1019 PGresult
*res
= NULL
;
1020 printTableOpt myopt
= pset
.popt
.topt
;
1021 printTableContent cont
;
1022 bool printTableInitialized
= false;
1024 char *view_def
= NULL
;
1026 char **seq_values
= NULL
;
1027 char **modifiers
= NULL
;
1029 PQExpBufferData title
;
1030 PQExpBufferData tmpbuf
;
1044 bool show_modifiers
= false;
1049 /* This output looks confusing in expanded mode. */
1050 myopt
.expanded
= false;
1052 initPQExpBuffer(&buf
);
1053 initPQExpBuffer(&title
);
1054 initPQExpBuffer(&tmpbuf
);
1056 /* Get general table info */
1057 if (pset
.sversion
>= 80400)
1059 printfPQExpBuffer(&buf
,
1060 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1061 "c.relhastriggers, c.relhasoids, "
1062 "%s, c.reltablespace\n"
1063 "FROM pg_catalog.pg_class c\n "
1064 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1065 "WHERE c.oid = '%s'\n",
1067 "pg_catalog.array_to_string(c.reloptions || "
1068 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1072 else if (pset
.sversion
>= 80200)
1074 printfPQExpBuffer(&buf
,
1075 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1076 "reltriggers <> 0, relhasoids, "
1077 "%s, reltablespace\n"
1078 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1080 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1083 else if (pset
.sversion
>= 80000)
1085 printfPQExpBuffer(&buf
,
1086 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1087 "reltriggers <> 0, relhasoids, "
1088 "'', reltablespace\n"
1089 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1094 printfPQExpBuffer(&buf
,
1095 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1096 "reltriggers <> 0, relhasoids, "
1098 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1102 res
= PSQLexec(buf
.data
, false);
1106 /* Did we get anything? */
1107 if (PQntuples(res
) == 0)
1110 fprintf(stderr
, _("Did not find any relation with OID %s.\n"),
1115 tableinfo
.checks
= atoi(PQgetvalue(res
, 0, 0));
1116 tableinfo
.relkind
= *(PQgetvalue(res
, 0, 1));
1117 tableinfo
.hasindex
= strcmp(PQgetvalue(res
, 0, 2), "t") == 0;
1118 tableinfo
.hasrules
= strcmp(PQgetvalue(res
, 0, 3), "t") == 0;
1119 tableinfo
.hastriggers
= strcmp(PQgetvalue(res
, 0, 4), "t") == 0;
1120 tableinfo
.hasoids
= strcmp(PQgetvalue(res
, 0, 5), "t") == 0;
1121 tableinfo
.reloptions
= pset
.sversion
>= 80200 ?
1122 strdup(PQgetvalue(res
, 0, 6)) : 0;
1123 tableinfo
.tablespace
= (pset
.sversion
>= 80000) ?
1124 atooid(PQgetvalue(res
, 0, 7)) : 0;
1129 * If it's a sequence, fetch its values and store into an array that will
1132 if (tableinfo
.relkind
== 'S')
1136 #define SEQ_NUM_COLS 10
1137 printfPQExpBuffer(&buf
,
1138 "SELECT sequence_name, last_value,\n"
1139 " start_value, increment_by,\n"
1140 " max_value, min_value, cache_value,\n"
1141 " log_cnt, is_cycled, is_called\n"
1144 /* must be separate because fmtId isn't reentrant */
1145 appendPQExpBuffer(&buf
, ".%s", fmtId(relationname
));
1147 result
= PSQLexec(buf
.data
, false);
1151 seq_values
= pg_malloc_zero((SEQ_NUM_COLS
+ 1) * sizeof(*seq_values
));
1153 for (i
= 0; i
< SEQ_NUM_COLS
; i
++)
1154 seq_values
[i
] = pg_strdup(PQgetvalue(result
, 0, i
));
1159 /* Get column info */
1160 printfPQExpBuffer(&buf
, "SELECT a.attname,");
1161 appendPQExpBuffer(&buf
, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
1162 "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
1163 "\n FROM pg_catalog.pg_attrdef d"
1164 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1165 "\n a.attnotnull, a.attnum");
1166 if (tableinfo
.relkind
== 'i')
1167 appendPQExpBuffer(&buf
, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1169 appendPQExpBuffer(&buf
, ",\n a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)");
1170 appendPQExpBuffer(&buf
, "\nFROM pg_catalog.pg_attribute a");
1171 appendPQExpBuffer(&buf
, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid
);
1172 appendPQExpBuffer(&buf
, "\nORDER BY a.attnum");
1174 res
= PSQLexec(buf
.data
, false);
1177 numrows
= PQntuples(res
);
1180 switch (tableinfo
.relkind
)
1183 printfPQExpBuffer(&title
, _("Table \"%s.%s\""),
1184 schemaname
, relationname
);
1187 printfPQExpBuffer(&title
, _("View \"%s.%s\""),
1188 schemaname
, relationname
);
1191 printfPQExpBuffer(&title
, _("Sequence \"%s.%s\""),
1192 schemaname
, relationname
);
1195 printfPQExpBuffer(&title
, _("Index \"%s.%s\""),
1196 schemaname
, relationname
);
1199 /* not used as of 8.2, but keep it for backwards compatibility */
1200 printfPQExpBuffer(&title
, _("Special relation \"%s.%s\""),
1201 schemaname
, relationname
);
1204 printfPQExpBuffer(&title
, _("TOAST table \"%s.%s\""),
1205 schemaname
, relationname
);
1208 printfPQExpBuffer(&title
, _("Composite type \"%s.%s\""),
1209 schemaname
, relationname
);
1212 /* untranslated unknown relkind */
1213 printfPQExpBuffer(&title
, "?%c? \"%s.%s\"",
1214 tableinfo
.relkind
, schemaname
, relationname
);
1218 /* Set the number of columns, and their names */
1219 headers
[0] = gettext_noop("Column");
1220 headers
[1] = gettext_noop("Type");
1223 if (tableinfo
.relkind
== 'r' || tableinfo
.relkind
== 'v')
1225 show_modifiers
= true;
1226 headers
[cols
++] = gettext_noop("Modifiers");
1227 modifiers
= pg_malloc_zero((numrows
+ 1) * sizeof(*modifiers
));
1230 if (tableinfo
.relkind
== 'S')
1231 headers
[cols
++] = gettext_noop("Value");
1233 if (tableinfo
.relkind
== 'i')
1234 headers
[cols
++] = gettext_noop("Definition");
1238 headers
[cols
++] = gettext_noop("Storage");
1239 headers
[cols
++] = gettext_noop("Description");
1242 printTableInit(&cont
, &myopt
, title
.data
, cols
, numrows
);
1243 printTableInitialized
= true;
1245 for (i
= 0; i
< cols
; i
++)
1246 printTableAddHeader(&cont
, headers
[i
], true, 'l');
1248 /* Check if table is a view */
1249 if (tableinfo
.relkind
== 'v')
1253 printfPQExpBuffer(&buf
,
1254 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
1256 result
= PSQLexec(buf
.data
, false);
1260 if (PQntuples(result
) > 0)
1261 view_def
= pg_strdup(PQgetvalue(result
, 0, 0));
1266 /* Generate table cells to be printed */
1267 for (i
= 0; i
< numrows
; i
++)
1270 printTableAddCell(&cont
, PQgetvalue(res
, i
, 0), false);
1273 printTableAddCell(&cont
, PQgetvalue(res
, i
, 1), false);
1275 /* Modifiers: not null and default */
1278 resetPQExpBuffer(&tmpbuf
);
1279 if (strcmp(PQgetvalue(res
, i
, 3), "t") == 0)
1280 appendPQExpBufferStr(&tmpbuf
, _("not null"));
1282 /* handle "default" here */
1283 /* (note: above we cut off the 'default' string at 128) */
1284 if (strlen(PQgetvalue(res
, i
, 2)) != 0)
1287 appendPQExpBufferStr(&tmpbuf
, " ");
1288 /* translator: default values of column definitions */
1289 appendPQExpBuffer(&tmpbuf
, _("default %s"),
1290 PQgetvalue(res
, i
, 2));
1293 modifiers
[i
] = pg_strdup(tmpbuf
.data
);
1294 printTableAddCell(&cont
, modifiers
[i
], false);
1297 /* Value: for sequences only */
1298 if (tableinfo
.relkind
== 'S')
1299 printTableAddCell(&cont
, seq_values
[i
], false);
1301 /* Expression for index column */
1302 if (tableinfo
.relkind
== 'i')
1303 printTableAddCell(&cont
, PQgetvalue(res
, i
, 5), false);
1305 /* Storage and Description */
1308 int firstvcol
= (tableinfo
.relkind
== 'i' ? 6 : 5);
1309 char *storage
= PQgetvalue(res
, i
, firstvcol
);
1311 /* these strings are literal in our syntax, so not translated. */
1312 printTableAddCell(&cont
, (storage
[0] == 'p' ? "plain" :
1313 (storage
[0] == 'm' ? "main" :
1314 (storage
[0] == 'x' ? "extended" :
1315 (storage
[0] == 'e' ? "external" :
1318 printTableAddCell(&cont
, PQgetvalue(res
, i
, firstvcol
+ 1), false);
1323 if (tableinfo
.relkind
== 'i')
1325 /* Footer information about an index */
1328 printfPQExpBuffer(&buf
,
1329 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1330 if (pset
.sversion
>= 80200)
1331 appendPQExpBuffer(&buf
, "i.indisvalid, ");
1333 appendPQExpBuffer(&buf
, "true as indisvalid, ");
1334 appendPQExpBuffer(&buf
, "a.amname, c2.relname,\n"
1335 " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
1336 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
1337 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
1338 "AND i.indrelid = c2.oid",
1341 result
= PSQLexec(buf
.data
, false);
1344 else if (PQntuples(result
) != 1)
1351 char *indisunique
= PQgetvalue(result
, 0, 0);
1352 char *indisprimary
= PQgetvalue(result
, 0, 1);
1353 char *indisclustered
= PQgetvalue(result
, 0, 2);
1354 char *indisvalid
= PQgetvalue(result
, 0, 3);
1355 char *indamname
= PQgetvalue(result
, 0, 4);
1356 char *indtable
= PQgetvalue(result
, 0, 5);
1357 char *indpred
= PQgetvalue(result
, 0, 6);
1359 if (strcmp(indisprimary
, "t") == 0)
1360 printfPQExpBuffer(&tmpbuf
, _("primary key, "));
1361 else if (strcmp(indisunique
, "t") == 0)
1362 printfPQExpBuffer(&tmpbuf
, _("unique, "));
1364 resetPQExpBuffer(&tmpbuf
);
1365 appendPQExpBuffer(&tmpbuf
, "%s, ", indamname
);
1367 /* we assume here that index and table are in same schema */
1368 appendPQExpBuffer(&tmpbuf
, _("for table \"%s.%s\""),
1369 schemaname
, indtable
);
1371 if (strlen(indpred
))
1372 appendPQExpBuffer(&tmpbuf
, _(", predicate (%s)"), indpred
);
1374 if (strcmp(indisclustered
, "t") == 0)
1375 appendPQExpBuffer(&tmpbuf
, _(", clustered"));
1377 if (strcmp(indisvalid
, "t") != 0)
1378 appendPQExpBuffer(&tmpbuf
, _(", invalid"));
1380 printTableAddFooter(&cont
, tmpbuf
.data
);
1381 add_tablespace_footer(&cont
, tableinfo
.relkind
,
1382 tableinfo
.tablespace
, true);
1389 PGresult
*result
= NULL
;
1391 /* Footer information about a view */
1392 printTableAddFooter(&cont
, _("View definition:"));
1393 printTableAddFooter(&cont
, view_def
);
1396 if (tableinfo
.hasrules
)
1398 printfPQExpBuffer(&buf
,
1399 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1400 "FROM pg_catalog.pg_rewrite r\n"
1401 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1",
1403 result
= PSQLexec(buf
.data
, false);
1407 if (PQntuples(result
) > 0)
1409 printTableAddFooter(&cont
, _("Rules:"));
1410 for (i
= 0; i
< PQntuples(result
); i
++)
1412 const char *ruledef
;
1414 /* Everything after "CREATE RULE" is echoed verbatim */
1415 ruledef
= PQgetvalue(result
, i
, 1);
1418 printfPQExpBuffer(&buf
, " %s", ruledef
);
1419 printTableAddFooter(&cont
, buf
.data
);
1425 else if (tableinfo
.relkind
== 'r')
1427 /* Footer information about a table */
1428 PGresult
*result
= NULL
;
1432 if (tableinfo
.hasindex
)
1434 printfPQExpBuffer(&buf
,
1435 "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
1436 if (pset
.sversion
>= 80200)
1437 appendPQExpBuffer(&buf
, "i.indisvalid, ");
1439 appendPQExpBuffer(&buf
, "true as indisvalid, ");
1440 appendPQExpBuffer(&buf
, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)");
1441 if (pset
.sversion
>= 80000)
1442 appendPQExpBuffer(&buf
, ", c2.reltablespace");
1443 appendPQExpBuffer(&buf
,
1444 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
1445 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1446 "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
1448 result
= PSQLexec(buf
.data
, false);
1452 tuples
= PQntuples(result
);
1456 printTableAddFooter(&cont
, _("Indexes:"));
1457 for (i
= 0; i
< tuples
; i
++)
1459 const char *indexdef
;
1460 const char *usingpos
;
1462 /* untranslated index name */
1463 printfPQExpBuffer(&buf
, " \"%s\"",
1464 PQgetvalue(result
, i
, 0));
1466 /* Label as primary key or unique (but not both) */
1467 appendPQExpBuffer(&buf
,
1468 strcmp(PQgetvalue(result
, i
, 1), "t") == 0
1470 (strcmp(PQgetvalue(result
, i
, 2), "t") == 0
1473 /* Everything after "USING" is echoed verbatim */
1474 indexdef
= PQgetvalue(result
, i
, 5);
1475 usingpos
= strstr(indexdef
, " USING ");
1477 indexdef
= usingpos
+ 7;
1479 appendPQExpBuffer(&buf
, " %s", indexdef
);
1481 if (strcmp(PQgetvalue(result
, i
, 3), "t") == 0)
1482 appendPQExpBuffer(&buf
, " CLUSTER");
1484 if (strcmp(PQgetvalue(result
, i
, 4), "t") != 0)
1485 appendPQExpBuffer(&buf
, " INVALID");
1487 printTableAddFooter(&cont
, buf
.data
);
1489 /* Print tablespace of the index on the same line */
1490 if (pset
.sversion
>= 80000)
1491 add_tablespace_footer(&cont
, 'i',
1492 atooid(PQgetvalue(result
, i
, 6)),
1499 /* print table (and column) check constraints */
1500 if (tableinfo
.checks
)
1502 printfPQExpBuffer(&buf
,
1503 "SELECT r.conname, "
1504 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1505 "FROM pg_catalog.pg_constraint r\n"
1506 "WHERE r.conrelid = '%s' AND r.contype = 'c'\nORDER BY 1",
1508 result
= PSQLexec(buf
.data
, false);
1512 tuples
= PQntuples(result
);
1516 printTableAddFooter(&cont
, _("Check constraints:"));
1517 for (i
= 0; i
< tuples
; i
++)
1519 /* untranslated contraint name and def */
1520 printfPQExpBuffer(&buf
, " \"%s\" %s",
1521 PQgetvalue(result
, i
, 0),
1522 PQgetvalue(result
, i
, 1));
1524 printTableAddFooter(&cont
, buf
.data
);
1530 /* print foreign-key constraints (there are none if no triggers) */
1531 if (tableinfo
.hastriggers
)
1533 printfPQExpBuffer(&buf
,
1535 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
1536 "FROM pg_catalog.pg_constraint r\n"
1537 "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1",
1539 result
= PSQLexec(buf
.data
, false);
1543 tuples
= PQntuples(result
);
1547 printTableAddFooter(&cont
, _("Foreign-key constraints:"));
1548 for (i
= 0; i
< tuples
; i
++)
1550 /* untranslated constraint name and def */
1551 printfPQExpBuffer(&buf
, " \"%s\" %s",
1552 PQgetvalue(result
, i
, 0),
1553 PQgetvalue(result
, i
, 1));
1555 printTableAddFooter(&cont
, buf
.data
);
1561 /* print incoming foreign-key references (none if no triggers) */
1562 if (tableinfo
.hastriggers
)
1564 printfPQExpBuffer(&buf
,
1565 "SELECT conname, conrelid::pg_catalog.regclass,\n"
1566 " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
1567 "FROM pg_catalog.pg_constraint c\n"
1568 "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
1570 result
= PSQLexec(buf
.data
, false);
1574 tuples
= PQntuples(result
);
1578 printTableAddFooter(&cont
, _("Referenced by:"));
1579 for (i
= 0; i
< tuples
; i
++)
1581 printfPQExpBuffer(&buf
, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
1582 PQgetvalue(result
, i
, 1),
1583 PQgetvalue(result
, i
, 0),
1584 PQgetvalue(result
, i
, 2));
1586 printTableAddFooter(&cont
, buf
.data
);
1593 if (tableinfo
.hasrules
)
1595 if (pset
.sversion
>= 80300)
1597 printfPQExpBuffer(&buf
,
1598 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1600 "FROM pg_catalog.pg_rewrite r\n"
1601 "WHERE r.ev_class = '%s' ORDER BY 1",
1606 printfPQExpBuffer(&buf
,
1607 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
1608 "'O'::char AS ev_enabled\n"
1609 "FROM pg_catalog.pg_rewrite r\n"
1610 "WHERE r.ev_class = '%s' ORDER BY 1",
1613 result
= PSQLexec(buf
.data
, false);
1617 tuples
= PQntuples(result
);
1624 for (category
= 0; category
< 4; category
++)
1626 have_heading
= false;
1628 for (i
= 0; i
< tuples
; i
++)
1630 const char *ruledef
;
1631 bool list_rule
= false;
1636 if (*PQgetvalue(result
, i
, 2) == 'O')
1640 if (*PQgetvalue(result
, i
, 2) == 'D')
1644 if (*PQgetvalue(result
, i
, 2) == 'A')
1648 if (*PQgetvalue(result
, i
, 2) == 'R')
1660 printfPQExpBuffer(&buf
, _("Rules:"));
1663 printfPQExpBuffer(&buf
, _("Disabled rules:"));
1666 printfPQExpBuffer(&buf
, _("Rules firing always:"));
1669 printfPQExpBuffer(&buf
, _("Rules firing on replica only:"));
1672 printTableAddFooter(&cont
, buf
.data
);
1673 have_heading
= true;
1676 /* Everything after "CREATE RULE" is echoed verbatim */
1677 ruledef
= PQgetvalue(result
, i
, 1);
1679 printfPQExpBuffer(&buf
, " %s", ruledef
);
1680 printTableAddFooter(&cont
, buf
.data
);
1687 /* print triggers (but ignore foreign-key triggers) */
1688 if (tableinfo
.hastriggers
)
1690 printfPQExpBuffer(&buf
,
1692 "pg_catalog.pg_get_triggerdef(t.oid), "
1694 "FROM pg_catalog.pg_trigger t\n"
1695 "WHERE t.tgrelid = '%s' AND ",
1697 if (pset
.sversion
>= 80300)
1698 appendPQExpBuffer(&buf
, "t.tgconstraint = 0");
1700 appendPQExpBuffer(&buf
,
1701 "(NOT tgisconstraint "
1703 " (SELECT 1 FROM pg_catalog.pg_depend d "
1704 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
1705 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
1706 appendPQExpBuffer(&buf
, "\nORDER BY 1");
1708 result
= PSQLexec(buf
.data
, false);
1712 tuples
= PQntuples(result
);
1720 * split the output into 4 different categories. Enabled
1721 * triggers, disabled triggers and the two special ALWAYS and
1722 * REPLICA configurations.
1724 for (category
= 0; category
< 4; category
++)
1726 have_heading
= false;
1727 for (i
= 0; i
< tuples
; i
++)
1731 const char *usingpos
;
1732 const char *tgenabled
;
1735 * Check if this trigger falls into the current
1738 tgenabled
= PQgetvalue(result
, i
, 2);
1739 list_trigger
= false;
1743 if (*tgenabled
== 'O' || *tgenabled
== 't')
1744 list_trigger
= true;
1747 if (*tgenabled
== 'D' || *tgenabled
== 'f')
1748 list_trigger
= true;
1751 if (*tgenabled
== 'A')
1752 list_trigger
= true;
1755 if (*tgenabled
== 'R')
1756 list_trigger
= true;
1759 if (list_trigger
== false)
1762 /* Print the category heading once */
1763 if (have_heading
== false)
1768 printfPQExpBuffer(&buf
, _("Triggers:"));
1771 printfPQExpBuffer(&buf
, _("Disabled triggers:"));
1774 printfPQExpBuffer(&buf
, _("Triggers firing always:"));
1777 printfPQExpBuffer(&buf
, _("Triggers firing on replica only:"));
1781 printTableAddFooter(&cont
, buf
.data
);
1782 have_heading
= true;
1785 /* Everything after "TRIGGER" is echoed verbatim */
1786 tgdef
= PQgetvalue(result
, i
, 1);
1787 usingpos
= strstr(tgdef
, " TRIGGER ");
1789 tgdef
= usingpos
+ 9;
1791 printfPQExpBuffer(&buf
, " %s", tgdef
);
1792 printTableAddFooter(&cont
, buf
.data
);
1799 /* print inherited tables */
1800 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
);
1802 result
= PSQLexec(buf
.data
, false);
1806 tuples
= PQntuples(result
);
1808 for (i
= 0; i
< tuples
; i
++)
1810 const char *s
= _("Inherits");
1813 printfPQExpBuffer(&buf
, "%s: %s", s
, PQgetvalue(result
, i
, 0));
1815 printfPQExpBuffer(&buf
, "%*s %s", (int) strlen(s
), "", PQgetvalue(result
, i
, 0));
1817 appendPQExpBuffer(&buf
, ",");
1819 printTableAddFooter(&cont
, buf
.data
);
1823 /* print child tables */
1824 printfPQExpBuffer(&buf
, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '%s' ORDER BY c.oid::pg_catalog.regclass;", oid
);
1826 result
= PSQLexec(buf
.data
, false);
1830 tuples
= PQntuples(result
);
1834 /* print the number of child tables, if any */
1837 printfPQExpBuffer(&buf
, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples
);
1838 printTableAddFooter(&cont
, buf
.data
);
1843 /* display the list of child tables */
1844 const char *ct
= _("Child tables");
1846 for (i
= 0; i
< tuples
; i
++)
1849 printfPQExpBuffer(&buf
, "%s: %s",
1850 ct
, PQgetvalue(result
, i
, 0));
1852 printfPQExpBuffer(&buf
, "%*s %s",
1853 (int) strlen(ct
), "",
1854 PQgetvalue(result
, i
, 0));
1856 appendPQExpBuffer(&buf
, ",");
1858 printTableAddFooter(&cont
, buf
.data
);
1863 /* OIDs and options */
1866 const char *s
= _("Has OIDs");
1868 printfPQExpBuffer(&buf
, "%s: %s", s
,
1869 (tableinfo
.hasoids
? _("yes") : _("no")));
1870 printTableAddFooter(&cont
, buf
.data
);
1872 /* print reloptions */
1873 if (pset
.sversion
>= 80200)
1875 if (tableinfo
.reloptions
&& tableinfo
.reloptions
[0] != '\0')
1877 const char *t
= _("Options");
1879 printfPQExpBuffer(&buf
, "%s: %s", t
,
1880 tableinfo
.reloptions
);
1881 printTableAddFooter(&cont
, buf
.data
);
1886 add_tablespace_footer(&cont
, tableinfo
.relkind
, tableinfo
.tablespace
,
1890 printTable(&cont
, pset
.queryFout
, pset
.logfile
);
1891 printTableCleanup(&cont
);
1898 if (printTableInitialized
)
1899 printTableCleanup(&cont
);
1900 termPQExpBuffer(&buf
);
1901 termPQExpBuffer(&title
);
1902 termPQExpBuffer(&tmpbuf
);
1906 for (ptr
= seq_values
; *ptr
; ptr
++)
1913 for (ptr
= modifiers
; *ptr
; ptr
++)
1928 * Add a tablespace description to a footer. If 'newline' is true, it is added
1929 * in a new line; otherwise it's appended to the current value of the last
1933 add_tablespace_footer(printTableContent
*const cont
, char relkind
,
1934 Oid tablespace
, const bool newline
)
1936 /* relkinds for which we support tablespaces */
1937 if (relkind
== 'r' || relkind
== 'i')
1940 * We ignore the database default tablespace so that users not using
1941 * tablespaces don't need to know about them. This case also covers
1942 * pre-8.0 servers, for which tablespace will always be 0.
1944 if (tablespace
!= 0)
1946 PGresult
*result
= NULL
;
1947 PQExpBufferData buf
;
1949 initPQExpBuffer(&buf
);
1950 printfPQExpBuffer(&buf
,
1951 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
1952 "WHERE oid = '%u'", tablespace
);
1953 result
= PSQLexec(buf
.data
, false);
1956 /* Should always be the case, but.... */
1957 if (PQntuples(result
) > 0)
1961 /* Add the tablespace as a new footer */
1962 printfPQExpBuffer(&buf
, _("Tablespace: \"%s\""),
1963 PQgetvalue(result
, 0, 0));
1964 printTableAddFooter(cont
, buf
.data
);
1968 /* Append the tablespace to the latest footer */
1969 printfPQExpBuffer(&buf
, "%s", cont
->footer
->data
);
1972 * translator: before this string there's an index
1973 * description like '"foo_pkey" PRIMARY KEY, btree (a)'
1975 appendPQExpBuffer(&buf
, _(", tablespace \"%s\""),
1976 PQgetvalue(result
, 0, 0));
1977 printTableSetFooter(cont
, buf
.data
);
1981 termPQExpBuffer(&buf
);
1989 * Describes roles. Any schema portion of the pattern is ignored.
1992 describeRoles(const char *pattern
, bool verbose
)
1994 PQExpBufferData buf
;
1996 printTableContent cont
;
1997 printTableOpt myopt
= pset
.popt
.topt
;
2002 const char align
= 'l';
2005 initPQExpBuffer(&buf
);
2007 if (pset
.sversion
>= 80100)
2009 printfPQExpBuffer(&buf
,
2010 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2011 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2012 " r.rolconnlimit,\n"
2013 " ARRAY(SELECT b.rolname\n"
2014 " FROM pg_catalog.pg_auth_members m\n"
2015 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2016 " WHERE m.member = r.oid) as memberof");
2018 if (verbose
&& pset
.sversion
>= 80200)
2020 appendPQExpBufferStr(&buf
, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2024 appendPQExpBufferStr(&buf
, "\nFROM pg_catalog.pg_roles r\n");
2026 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2027 NULL
, "r.rolname", NULL
, NULL
);
2031 printfPQExpBuffer(&buf
,
2032 "SELECT u.usename AS rolname,\n"
2033 " u.usesuper AS rolsuper,\n"
2034 " true AS rolinherit, false AS rolcreaterole,\n"
2035 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2036 " -1 AS rolconnlimit,\n"
2037 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2038 "\nFROM pg_catalog.pg_user u\n");
2040 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2041 NULL
, "u.usename", NULL
, NULL
);
2044 appendPQExpBuffer(&buf
, "ORDER BY 1;");
2046 res
= PSQLexec(buf
.data
, false);
2050 nrows
= PQntuples(res
);
2051 attr
= pg_malloc_zero((nrows
+ 1) * sizeof(*attr
));
2053 printTableInit(&cont
, &myopt
, _("List of roles"), ncols
, nrows
);
2055 printTableAddHeader(&cont
, gettext_noop("Role name"), true, align
);
2056 printTableAddHeader(&cont
, gettext_noop("Attributes"), true, align
);
2057 printTableAddHeader(&cont
, gettext_noop("Member of"), true, align
);
2059 if (verbose
&& pset
.sversion
>= 80200)
2060 printTableAddHeader(&cont
, gettext_noop("Description"), true, align
);
2062 for (i
= 0; i
< nrows
; i
++)
2064 printTableAddCell(&cont
, PQgetvalue(res
, i
, 0), false);
2066 resetPQExpBuffer(&buf
);
2067 if (strcmp(PQgetvalue(res
, i
, 1), "t") == 0)
2068 add_role_attribute(&buf
, _("Superuser"));
2070 if (strcmp(PQgetvalue(res
, i
, 2), "t") != 0)
2071 add_role_attribute(&buf
, _("No inheritance"));
2073 if (strcmp(PQgetvalue(res
, i
, 3), "t") == 0)
2074 add_role_attribute(&buf
, _("Create role"));
2076 if (strcmp(PQgetvalue(res
, i
, 4), "t") == 0)
2077 add_role_attribute(&buf
, _("Create DB"));
2079 if (strcmp(PQgetvalue(res
, i
, 5), "t") != 0)
2080 add_role_attribute(&buf
, _("Cannot login"));
2082 conns
= atoi(PQgetvalue(res
, i
, 6));
2086 appendPQExpBufferStr(&buf
, "\n");
2089 appendPQExpBuffer(&buf
, _("No connections"));
2091 appendPQExpBuffer(&buf
, ngettext("%d connection",
2097 attr
[i
] = pg_strdup(buf
.data
);
2099 printTableAddCell(&cont
, attr
[i
], false);
2101 printTableAddCell(&cont
, PQgetvalue(res
, i
, 7), false);
2103 if (verbose
&& pset
.sversion
>= 80200)
2104 printTableAddCell(&cont
, PQgetvalue(res
, i
, 8), false);
2106 termPQExpBuffer(&buf
);
2108 printTable(&cont
, pset
.queryFout
, pset
.logfile
);
2109 printTableCleanup(&cont
);
2111 for (i
= 0; i
< nrows
; i
++)
2120 add_role_attribute(PQExpBuffer buf
, const char *const str
)
2123 appendPQExpBufferStr(buf
, "\n");
2125 appendPQExpBufferStr(buf
, str
);
2132 * handler for \dt, \di, etc.
2134 * tabtypes is an array of characters, specifying what info is desired:
2139 * (any order of the above is fine)
2140 * If tabtypes is empty, we default to \dtvs.
2143 listTables(const char *tabtypes
, const char *pattern
, bool verbose
, bool showSystem
)
2145 bool showTables
= strchr(tabtypes
, 't') != NULL
;
2146 bool showIndexes
= strchr(tabtypes
, 'i') != NULL
;
2147 bool showViews
= strchr(tabtypes
, 'v') != NULL
;
2148 bool showSeq
= strchr(tabtypes
, 's') != NULL
;
2150 PQExpBufferData buf
;
2152 printQueryOpt myopt
= pset
.popt
;
2153 static const bool translate_columns
[] = {false, false, true, false, false, false, false};
2155 if (!(showTables
|| showIndexes
|| showViews
|| showSeq
))
2156 showTables
= showViews
= showSeq
= true;
2158 initPQExpBuffer(&buf
);
2161 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2162 * for backwards compatibility.
2164 printfPQExpBuffer(&buf
,
2165 "SELECT n.nspname as \"%s\",\n"
2166 " c.relname as \"%s\",\n"
2167 " 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"
2168 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2169 gettext_noop("Schema"),
2170 gettext_noop("Name"),
2171 gettext_noop("table"),
2172 gettext_noop("view"),
2173 gettext_noop("index"),
2174 gettext_noop("sequence"),
2175 gettext_noop("special"),
2176 gettext_noop("Type"),
2177 gettext_noop("Owner"));
2180 appendPQExpBuffer(&buf
,
2181 ",\n c2.relname as \"%s\"",
2182 gettext_noop("Table"));
2184 if (verbose
&& pset
.sversion
>= 80100)
2185 appendPQExpBuffer(&buf
,
2186 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2187 gettext_noop("Size"));
2189 appendPQExpBuffer(&buf
,
2190 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2191 gettext_noop("Description"));
2193 appendPQExpBuffer(&buf
,
2194 "\nFROM pg_catalog.pg_class c"
2195 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2197 appendPQExpBuffer(&buf
,
2198 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2199 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2201 appendPQExpBuffer(&buf
, "\nWHERE c.relkind IN (");
2203 appendPQExpBuffer(&buf
, "'r',");
2205 appendPQExpBuffer(&buf
, "'v',");
2207 appendPQExpBuffer(&buf
, "'i',");
2209 appendPQExpBuffer(&buf
, "'S',");
2210 if (showSystem
|| pattern
)
2211 appendPQExpBuffer(&buf
, "'s',"); /* was RELKIND_SPECIAL in <=
2213 appendPQExpBuffer(&buf
, "''"); /* dummy */
2214 appendPQExpBuffer(&buf
, ")\n");
2216 if (!showSystem
&& !pattern
)
2217 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
2218 " AND n.nspname <> 'information_schema'\n");
2221 * TOAST objects are suppressed unconditionally. Since we don't provide
2222 * any way to select relkind 't' above, we would never show toast tables
2223 * in any case; it seems a bit confusing to allow their indexes to be
2224 * shown. Use plain \d if you really need to look at a TOAST table/index.
2226 appendPQExpBuffer(&buf
, " AND n.nspname !~ '^pg_toast'\n");
2228 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2229 "n.nspname", "c.relname", NULL
,
2230 "pg_catalog.pg_table_is_visible(c.oid)");
2232 appendPQExpBuffer(&buf
, "ORDER BY 1,2;");
2234 res
= PSQLexec(buf
.data
, false);
2235 termPQExpBuffer(&buf
);
2239 if (PQntuples(res
) == 0 && !pset
.quiet
)
2242 fprintf(pset
.queryFout
, _("No matching relations found.\n"));
2244 fprintf(pset
.queryFout
, _("No relations found.\n"));
2248 myopt
.nullPrint
= NULL
;
2249 myopt
.title
= _("List of relations");
2250 myopt
.translate_header
= true;
2251 myopt
.translate_columns
= translate_columns
;
2253 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2264 * Describes domains.
2267 listDomains(const char *pattern
, bool showSystem
)
2269 PQExpBufferData buf
;
2271 printQueryOpt myopt
= pset
.popt
;
2273 initPQExpBuffer(&buf
);
2275 printfPQExpBuffer(&buf
,
2276 "SELECT n.nspname as \"%s\",\n"
2277 " t.typname as \"%s\",\n"
2278 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2279 " CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
2280 " WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
2281 " WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
2284 " pg_catalog.array_to_string(ARRAY(\n"
2285 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2286 " ), ' ') as \"%s\"\n"
2287 "FROM pg_catalog.pg_type t\n"
2288 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
2289 "WHERE t.typtype = 'd'\n",
2290 gettext_noop("Schema"),
2291 gettext_noop("Name"),
2292 gettext_noop("Type"),
2293 gettext_noop("Modifier"),
2294 gettext_noop("Check"));
2296 if (!showSystem
&& !pattern
)
2297 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
2298 " AND n.nspname <> 'information_schema'\n");
2300 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2301 "n.nspname", "t.typname", NULL
,
2302 "pg_catalog.pg_type_is_visible(t.oid)");
2304 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2306 res
= PSQLexec(buf
.data
, false);
2307 termPQExpBuffer(&buf
);
2311 myopt
.nullPrint
= NULL
;
2312 myopt
.title
= _("List of domains");
2313 myopt
.translate_header
= true;
2315 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2324 * Describes conversions.
2327 listConversions(const char *pattern
, bool showSystem
)
2329 PQExpBufferData buf
;
2331 printQueryOpt myopt
= pset
.popt
;
2332 static const bool translate_columns
[] = {false, false, false, false, true};
2334 initPQExpBuffer(&buf
);
2336 printfPQExpBuffer(&buf
,
2337 "SELECT n.nspname AS \"%s\",\n"
2338 " c.conname AS \"%s\",\n"
2339 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2340 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2341 " CASE WHEN c.condefault THEN '%s'\n"
2342 " ELSE '%s' END AS \"%s\"\n"
2343 "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
2344 "WHERE n.oid = c.connamespace\n",
2345 gettext_noop("Schema"),
2346 gettext_noop("Name"),
2347 gettext_noop("Source"),
2348 gettext_noop("Destination"),
2349 gettext_noop("yes"), gettext_noop("no"),
2350 gettext_noop("Default?"));
2352 if (!showSystem
&& !pattern
)
2353 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
2354 " AND n.nspname <> 'information_schema'\n");
2356 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2357 "n.nspname", "c.conname", NULL
,
2358 "pg_catalog.pg_conversion_is_visible(c.oid)");
2360 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2362 res
= PSQLexec(buf
.data
, false);
2363 termPQExpBuffer(&buf
);
2367 myopt
.nullPrint
= NULL
;
2368 myopt
.title
= _("List of conversions");
2369 myopt
.translate_header
= true;
2370 myopt
.translate_columns
= translate_columns
;
2372 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2384 listCasts(const char *pattern
)
2386 PQExpBufferData buf
;
2388 printQueryOpt myopt
= pset
.popt
;
2389 static const bool translate_columns
[] = {false, false, false, true};
2391 initPQExpBuffer(&buf
);
2394 * We need a left join to pg_proc for binary casts; the others are just
2395 * paranoia. Also note that we don't attempt to localize '(binary
2396 * coercible)', because there's too much risk of gettext translating a
2397 * function name that happens to match some string in the PO database.
2399 printfPQExpBuffer(&buf
,
2400 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2401 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2402 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
2405 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2406 " WHEN c.castcontext = 'a' THEN '%s'\n"
2409 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2410 " ON c.castfunc = p.oid\n"
2411 " LEFT JOIN pg_catalog.pg_type ts\n"
2412 " ON c.castsource = ts.oid\n"
2413 " LEFT JOIN pg_catalog.pg_namespace ns\n"
2414 " ON ns.oid = ts.typnamespace\n"
2415 " LEFT JOIN pg_catalog.pg_type tt\n"
2416 " ON c.casttarget = tt.oid\n"
2417 " LEFT JOIN pg_catalog.pg_namespace nt\n"
2418 " ON nt.oid = tt.typnamespace\n"
2420 gettext_noop("Source type"),
2421 gettext_noop("Target type"),
2422 gettext_noop("Function"),
2423 gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2424 gettext_noop("Implicit?"));
2427 * Match name pattern against either internal or external name of either
2428 * castsource or casttarget
2430 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2431 "ns.nspname", "ts.typname",
2432 "pg_catalog.format_type(ts.oid, NULL)",
2433 "pg_catalog.pg_type_is_visible(ts.oid)");
2435 appendPQExpBuffer(&buf
, ") OR (true");
2437 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2438 "nt.nspname", "tt.typname",
2439 "pg_catalog.format_type(tt.oid, NULL)",
2440 "pg_catalog.pg_type_is_visible(tt.oid)");
2442 appendPQExpBuffer(&buf
, ")\nORDER BY 1, 2;");
2444 res
= PSQLexec(buf
.data
, false);
2445 termPQExpBuffer(&buf
);
2449 myopt
.nullPrint
= NULL
;
2450 myopt
.title
= _("List of casts");
2451 myopt
.translate_header
= true;
2452 myopt
.translate_columns
= translate_columns
;
2454 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2463 * Describes schemas (namespaces)
2466 listSchemas(const char *pattern
, bool verbose
)
2468 PQExpBufferData buf
;
2470 printQueryOpt myopt
= pset
.popt
;
2472 initPQExpBuffer(&buf
);
2473 printfPQExpBuffer(&buf
,
2474 "SELECT n.nspname AS \"%s\",\n"
2475 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2476 gettext_noop("Name"),
2477 gettext_noop("Owner"));
2481 appendPQExpBuffer(&buf
, ",\n ");
2482 printACLColumn(&buf
, "n.nspacl");
2483 appendPQExpBuffer(&buf
,
2484 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
2485 gettext_noop("Description"));
2488 appendPQExpBuffer(&buf
,
2489 "\nFROM pg_catalog.pg_namespace n\n"
2490 "WHERE (n.nspname !~ '^pg_temp_' OR\n"
2491 " n.nspname = (pg_catalog.current_schemas(true))[1])\n"); /* temp schema is first */
2493 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2494 NULL
, "n.nspname", NULL
,
2497 appendPQExpBuffer(&buf
, "ORDER BY 1;");
2499 res
= PSQLexec(buf
.data
, false);
2500 termPQExpBuffer(&buf
);
2504 myopt
.nullPrint
= NULL
;
2505 myopt
.title
= _("List of schemas");
2506 myopt
.translate_header
= true;
2508 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2517 * list text search parsers
2520 listTSParsers(const char *pattern
, bool verbose
)
2522 PQExpBufferData buf
;
2524 printQueryOpt myopt
= pset
.popt
;
2526 if (pset
.sversion
< 80300)
2528 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2529 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2534 return listTSParsersVerbose(pattern
);
2536 initPQExpBuffer(&buf
);
2538 printfPQExpBuffer(&buf
,
2540 " n.nspname as \"%s\",\n"
2541 " p.prsname as \"%s\",\n"
2542 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2543 "FROM pg_catalog.pg_ts_parser p \n"
2544 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2545 gettext_noop("Schema"),
2546 gettext_noop("Name"),
2547 gettext_noop("Description")
2550 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2551 "n.nspname", "p.prsname", NULL
,
2552 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2554 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2556 res
= PSQLexec(buf
.data
, false);
2557 termPQExpBuffer(&buf
);
2561 myopt
.nullPrint
= NULL
;
2562 myopt
.title
= _("List of text search parsers");
2563 myopt
.translate_header
= true;
2565 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2572 * full description of parsers
2575 listTSParsersVerbose(const char *pattern
)
2577 PQExpBufferData buf
;
2581 initPQExpBuffer(&buf
);
2583 printfPQExpBuffer(&buf
,
2587 "FROM pg_catalog.pg_ts_parser p\n"
2588 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2591 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2592 "n.nspname", "p.prsname", NULL
,
2593 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2595 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2597 res
= PSQLexec(buf
.data
, false);
2598 termPQExpBuffer(&buf
);
2602 if (PQntuples(res
) == 0)
2605 fprintf(stderr
, _("Did not find any text search parser named \"%s\".\n"),
2611 for (i
= 0; i
< PQntuples(res
); i
++)
2614 const char *nspname
= NULL
;
2615 const char *prsname
;
2617 oid
= PQgetvalue(res
, i
, 0);
2618 if (!PQgetisnull(res
, i
, 1))
2619 nspname
= PQgetvalue(res
, i
, 1);
2620 prsname
= PQgetvalue(res
, i
, 2);
2622 if (!describeOneTSParser(oid
, nspname
, prsname
))
2640 describeOneTSParser(const char *oid
, const char *nspname
, const char *prsname
)
2642 PQExpBufferData buf
;
2645 printQueryOpt myopt
= pset
.popt
;
2646 static const bool translate_columns
[] = {true, false, false};
2648 initPQExpBuffer(&buf
);
2650 printfPQExpBuffer(&buf
,
2651 "SELECT '%s' AS \"%s\", \n"
2652 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
2653 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
2654 " FROM pg_catalog.pg_ts_parser p \n"
2655 " WHERE p.oid = '%s' \n"
2658 " p.prstoken::pg_catalog.regproc, \n"
2659 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
2660 " FROM pg_catalog.pg_ts_parser p \n"
2661 " WHERE p.oid = '%s' \n"
2664 " p.prsend::pg_catalog.regproc, \n"
2665 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
2666 " FROM pg_catalog.pg_ts_parser p \n"
2667 " WHERE p.oid = '%s' \n"
2670 " p.prsheadline::pg_catalog.regproc, \n"
2671 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
2672 " FROM pg_catalog.pg_ts_parser p \n"
2673 " WHERE p.oid = '%s' \n"
2676 " p.prslextype::pg_catalog.regproc, \n"
2677 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
2678 " FROM pg_catalog.pg_ts_parser p \n"
2679 " WHERE p.oid = '%s' \n",
2680 gettext_noop("Start parse"),
2681 gettext_noop("Method"),
2682 gettext_noop("Function"),
2683 gettext_noop("Description"),
2685 gettext_noop("Get next token"),
2687 gettext_noop("End parse"),
2689 gettext_noop("Get headline"),
2691 gettext_noop("Get token types"),
2694 res
= PSQLexec(buf
.data
, false);
2695 termPQExpBuffer(&buf
);
2699 myopt
.nullPrint
= NULL
;
2701 sprintf(title
, _("Text search parser \"%s.%s\""), nspname
, prsname
);
2703 sprintf(title
, _("Text search parser \"%s\""), prsname
);
2704 myopt
.title
= title
;
2705 myopt
.footers
= NULL
;
2706 myopt
.default_footer
= false;
2707 myopt
.translate_header
= true;
2708 myopt
.translate_columns
= translate_columns
;
2710 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2714 initPQExpBuffer(&buf
);
2716 printfPQExpBuffer(&buf
,
2717 "SELECT t.alias as \"%s\", \n"
2718 " t.description as \"%s\" \n"
2719 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
2721 gettext_noop("Token name"),
2722 gettext_noop("Description"),
2725 res
= PSQLexec(buf
.data
, false);
2726 termPQExpBuffer(&buf
);
2730 myopt
.nullPrint
= NULL
;
2732 sprintf(title
, _("Token types for parser \"%s.%s\""), nspname
, prsname
);
2734 sprintf(title
, _("Token types for parser \"%s\""), prsname
);
2735 myopt
.title
= title
;
2736 myopt
.footers
= NULL
;
2737 myopt
.default_footer
= true;
2738 myopt
.translate_header
= true;
2739 myopt
.translate_columns
= NULL
;
2741 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2750 * list text search dictionaries
2753 listTSDictionaries(const char *pattern
, bool verbose
)
2755 PQExpBufferData buf
;
2757 printQueryOpt myopt
= pset
.popt
;
2759 if (pset
.sversion
< 80300)
2761 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2762 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2766 initPQExpBuffer(&buf
);
2768 printfPQExpBuffer(&buf
,
2770 " n.nspname as \"%s\",\n"
2771 " d.dictname as \"%s\",\n",
2772 gettext_noop("Schema"),
2773 gettext_noop("Name"));
2777 appendPQExpBuffer(&buf
,
2778 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
2779 " pg_catalog.pg_ts_template t \n"
2780 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
2781 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
2782 " d.dictinitoption as \"%s\", \n",
2783 gettext_noop("Template"),
2784 gettext_noop("Init options"));
2787 appendPQExpBuffer(&buf
,
2788 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
2789 gettext_noop("Description"));
2791 appendPQExpBuffer(&buf
, "FROM pg_catalog.pg_ts_dict d\n"
2792 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
2794 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2795 "n.nspname", "d.dictname", NULL
,
2796 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
2798 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2800 res
= PSQLexec(buf
.data
, false);
2801 termPQExpBuffer(&buf
);
2805 myopt
.nullPrint
= NULL
;
2806 myopt
.title
= _("List of text search dictionaries");
2807 myopt
.translate_header
= true;
2809 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2818 * list text search templates
2821 listTSTemplates(const char *pattern
, bool verbose
)
2823 PQExpBufferData buf
;
2825 printQueryOpt myopt
= pset
.popt
;
2827 if (pset
.sversion
< 80300)
2829 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2830 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2834 initPQExpBuffer(&buf
);
2837 printfPQExpBuffer(&buf
,
2839 " n.nspname AS \"%s\",\n"
2840 " t.tmplname AS \"%s\",\n"
2841 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
2842 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
2843 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2844 gettext_noop("Schema"),
2845 gettext_noop("Name"),
2846 gettext_noop("Init"),
2847 gettext_noop("Lexize"),
2848 gettext_noop("Description"));
2850 printfPQExpBuffer(&buf
,
2852 " n.nspname AS \"%s\",\n"
2853 " t.tmplname AS \"%s\",\n"
2854 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2855 gettext_noop("Schema"),
2856 gettext_noop("Name"),
2857 gettext_noop("Description"));
2859 appendPQExpBuffer(&buf
, "FROM pg_catalog.pg_ts_template t\n"
2860 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
2862 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2863 "n.nspname", "t.tmplname", NULL
,
2864 "pg_catalog.pg_ts_template_is_visible(t.oid)");
2866 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2868 res
= PSQLexec(buf
.data
, false);
2869 termPQExpBuffer(&buf
);
2873 myopt
.nullPrint
= NULL
;
2874 myopt
.title
= _("List of text search templates");
2875 myopt
.translate_header
= true;
2877 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2886 * list text search configurations
2889 listTSConfigs(const char *pattern
, bool verbose
)
2891 PQExpBufferData buf
;
2893 printQueryOpt myopt
= pset
.popt
;
2895 if (pset
.sversion
< 80300)
2897 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2898 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2903 return listTSConfigsVerbose(pattern
);
2905 initPQExpBuffer(&buf
);
2907 printfPQExpBuffer(&buf
,
2909 " n.nspname as \"%s\",\n"
2910 " c.cfgname as \"%s\",\n"
2911 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
2912 "FROM pg_catalog.pg_ts_config c\n"
2913 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
2914 gettext_noop("Schema"),
2915 gettext_noop("Name"),
2916 gettext_noop("Description")
2919 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2920 "n.nspname", "c.cfgname", NULL
,
2921 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2923 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2925 res
= PSQLexec(buf
.data
, false);
2926 termPQExpBuffer(&buf
);
2930 myopt
.nullPrint
= NULL
;
2931 myopt
.title
= _("List of text search configurations");
2932 myopt
.translate_header
= true;
2934 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2941 listTSConfigsVerbose(const char *pattern
)
2943 PQExpBufferData buf
;
2947 initPQExpBuffer(&buf
);
2949 printfPQExpBuffer(&buf
,
2950 "SELECT c.oid, c.cfgname,\n"
2953 " np.nspname as pnspname \n"
2954 "FROM pg_catalog.pg_ts_config c \n"
2955 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
2956 " pg_catalog.pg_ts_parser p \n"
2957 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
2958 "WHERE p.oid = c.cfgparser\n"
2961 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2962 "n.nspname", "c.cfgname", NULL
,
2963 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2965 appendPQExpBuffer(&buf
, "ORDER BY 3, 2;");
2967 res
= PSQLexec(buf
.data
, false);
2968 termPQExpBuffer(&buf
);
2972 if (PQntuples(res
) == 0)
2975 fprintf(stderr
, _("Did not find any text search configuration named \"%s\".\n"),
2981 for (i
= 0; i
< PQntuples(res
); i
++)
2984 const char *cfgname
;
2985 const char *nspname
= NULL
;
2986 const char *prsname
;
2987 const char *pnspname
= NULL
;
2989 oid
= PQgetvalue(res
, i
, 0);
2990 cfgname
= PQgetvalue(res
, i
, 1);
2991 if (!PQgetisnull(res
, i
, 2))
2992 nspname
= PQgetvalue(res
, i
, 2);
2993 prsname
= PQgetvalue(res
, i
, 3);
2994 if (!PQgetisnull(res
, i
, 4))
2995 pnspname
= PQgetvalue(res
, i
, 4);
2997 if (!describeOneTSConfig(oid
, nspname
, cfgname
, pnspname
, prsname
))
3015 describeOneTSConfig(const char *oid
, const char *nspname
, const char *cfgname
,
3016 const char *pnspname
, const char *prsname
)
3018 PQExpBufferData buf
,
3021 printQueryOpt myopt
= pset
.popt
;
3023 initPQExpBuffer(&buf
);
3025 printfPQExpBuffer(&buf
,
3027 " ( SELECT t.alias FROM \n"
3028 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3029 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3030 " pg_catalog.btrim( \n"
3031 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3032 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3033 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3034 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3035 " ) :: pg_catalog.text , \n"
3036 " '{}') AS \"%s\" \n"
3037 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3038 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3039 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3041 gettext_noop("Token"),
3042 gettext_noop("Dictionaries"),
3045 res
= PSQLexec(buf
.data
, false);
3046 termPQExpBuffer(&buf
);
3050 initPQExpBuffer(&title
);
3053 appendPQExpBuffer(&title
, _("Text search configuration \"%s.%s\""),
3056 appendPQExpBuffer(&title
, _("Text search configuration \"%s\""),
3060 appendPQExpBuffer(&title
, _("\nParser: \"%s.%s\""),
3063 appendPQExpBuffer(&title
, _("\nParser: \"%s\""),
3066 myopt
.nullPrint
= NULL
;
3067 myopt
.title
= title
.data
;
3068 myopt
.footers
= NULL
;
3069 myopt
.default_footer
= false;
3070 myopt
.translate_header
= true;
3072 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3074 termPQExpBuffer(&title
);
3084 * Describes foreign-data wrappers
3087 listForeignDataWrappers(const char *pattern
, bool verbose
)
3089 PQExpBufferData buf
;
3091 printQueryOpt myopt
= pset
.popt
;
3093 if (pset
.sversion
< 80400)
3095 fprintf(stderr
, _("The server (version %d.%d) does not support foreign-data wrappers.\n"),
3096 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
3100 initPQExpBuffer(&buf
);
3101 printfPQExpBuffer(&buf
,
3102 "SELECT fdwname AS \"%s\",\n"
3103 " pg_catalog.pg_get_userbyid(fdwowner) AS \"%s\",\n"
3104 " fdwvalidator::pg_catalog.regproc AS \"%s\"",
3105 gettext_noop("Name"),
3106 gettext_noop("Owner"),
3107 gettext_noop("Validator"));
3111 appendPQExpBuffer(&buf
, ",\n ");
3112 printACLColumn(&buf
, "fdwacl");
3113 appendPQExpBuffer(&buf
,
3114 ",\n fdwoptions AS \"%s\"",
3115 gettext_noop("Options"));
3118 appendPQExpBuffer(&buf
, "\nFROM pg_catalog.pg_foreign_data_wrapper\n");
3120 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
3121 NULL
, "fdwname", NULL
, NULL
);
3123 appendPQExpBuffer(&buf
, "ORDER BY 1;");
3125 res
= PSQLexec(buf
.data
, false);
3126 termPQExpBuffer(&buf
);
3130 myopt
.nullPrint
= NULL
;
3131 myopt
.title
= _("List of foreign-data wrappers");
3132 myopt
.translate_header
= true;
3134 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3143 * Describes foreign servers.
3146 listForeignServers(const char *pattern
, bool verbose
)
3148 PQExpBufferData buf
;
3150 printQueryOpt myopt
= pset
.popt
;
3152 if (pset
.sversion
< 80400)
3154 fprintf(stderr
, _("The server (version %d.%d) does not support foreign servers.\n"),
3155 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
3159 initPQExpBuffer(&buf
);
3160 printfPQExpBuffer(&buf
,
3161 "SELECT s.srvname AS \"%s\",\n"
3162 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3163 " f.fdwname AS \"%s\"",
3164 gettext_noop("Name"),
3165 gettext_noop("Owner"),
3166 gettext_noop("Foreign-data wrapper"));
3170 appendPQExpBuffer(&buf
, ",\n ");
3171 printACLColumn(&buf
, "s.srvacl");
3172 appendPQExpBuffer(&buf
,
3174 " s.srvtype AS \"%s\",\n"
3175 " s.srvversion AS \"%s\",\n"
3176 " s.srvoptions AS \"%s\"",
3177 gettext_noop("Type"),
3178 gettext_noop("Version"),
3179 gettext_noop("Options"));
3182 appendPQExpBuffer(&buf
,
3183 "\nFROM pg_catalog.pg_foreign_server s\n"
3184 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3186 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
3187 NULL
, "s.srvname", NULL
, NULL
);
3189 appendPQExpBuffer(&buf
, "ORDER BY 1;");
3191 res
= PSQLexec(buf
.data
, false);
3192 termPQExpBuffer(&buf
);
3196 myopt
.nullPrint
= NULL
;
3197 myopt
.title
= _("List of foreign servers");
3198 myopt
.translate_header
= true;
3200 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3209 * Describes user mappings.
3212 listUserMappings(const char *pattern
, bool verbose
)
3214 PQExpBufferData buf
;
3216 printQueryOpt myopt
= pset
.popt
;
3218 if (pset
.sversion
< 80400)
3220 fprintf(stderr
, _("The server (version %d.%d) does not support user mappings.\n"),
3221 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
3225 initPQExpBuffer(&buf
);
3226 printfPQExpBuffer(&buf
,
3227 "SELECT um.srvname AS \"%s\",\n"
3228 " um.usename AS \"%s\"",
3229 gettext_noop("Server"),
3230 gettext_noop("User name"));
3233 appendPQExpBuffer(&buf
,
3234 ",\n um.umoptions AS \"%s\"",
3235 gettext_noop("Options"));
3237 appendPQExpBuffer(&buf
, "\nFROM pg_catalog.pg_user_mappings um\n");
3239 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
3240 NULL
, "um.srvname", "um.usename", NULL
);
3242 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
3244 res
= PSQLexec(buf
.data
, false);
3245 termPQExpBuffer(&buf
);
3249 myopt
.nullPrint
= NULL
;
3250 myopt
.title
= _("List of user mappings");
3251 myopt
.translate_header
= true;
3253 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3262 * Helper function for consistently formatting ACL (privilege) columns.
3263 * The proper targetlist entry is appended to buf. Note lack of any
3264 * whitespace or comma decoration.
3267 printACLColumn(PQExpBuffer buf
, const char *colname
)
3269 if (pset
.sversion
>= 80100)
3270 appendPQExpBuffer(buf
,
3271 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
3272 colname
, gettext_noop("Access privileges"));
3274 appendPQExpBuffer(buf
,
3275 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
3276 colname
, gettext_noop("Access privileges"));