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 if (pset
.sversion
>= 80300)
1825 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::text;", oid
);
1827 printfPQExpBuffer(&buf
, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '%s' ORDER BY c.relname;", oid
);
1829 result
= PSQLexec(buf
.data
, false);
1833 tuples
= PQntuples(result
);
1837 /* print the number of child tables, if any */
1840 printfPQExpBuffer(&buf
, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples
);
1841 printTableAddFooter(&cont
, buf
.data
);
1846 /* display the list of child tables */
1847 const char *ct
= _("Child tables");
1849 for (i
= 0; i
< tuples
; i
++)
1852 printfPQExpBuffer(&buf
, "%s: %s",
1853 ct
, PQgetvalue(result
, i
, 0));
1855 printfPQExpBuffer(&buf
, "%*s %s",
1856 (int) strlen(ct
), "",
1857 PQgetvalue(result
, i
, 0));
1859 appendPQExpBuffer(&buf
, ",");
1861 printTableAddFooter(&cont
, buf
.data
);
1866 /* OIDs and options */
1869 const char *s
= _("Has OIDs");
1871 printfPQExpBuffer(&buf
, "%s: %s", s
,
1872 (tableinfo
.hasoids
? _("yes") : _("no")));
1873 printTableAddFooter(&cont
, buf
.data
);
1875 /* print reloptions */
1876 if (pset
.sversion
>= 80200)
1878 if (tableinfo
.reloptions
&& tableinfo
.reloptions
[0] != '\0')
1880 const char *t
= _("Options");
1882 printfPQExpBuffer(&buf
, "%s: %s", t
,
1883 tableinfo
.reloptions
);
1884 printTableAddFooter(&cont
, buf
.data
);
1889 add_tablespace_footer(&cont
, tableinfo
.relkind
, tableinfo
.tablespace
,
1893 printTable(&cont
, pset
.queryFout
, pset
.logfile
);
1894 printTableCleanup(&cont
);
1901 if (printTableInitialized
)
1902 printTableCleanup(&cont
);
1903 termPQExpBuffer(&buf
);
1904 termPQExpBuffer(&title
);
1905 termPQExpBuffer(&tmpbuf
);
1909 for (ptr
= seq_values
; *ptr
; ptr
++)
1916 for (ptr
= modifiers
; *ptr
; ptr
++)
1931 * Add a tablespace description to a footer. If 'newline' is true, it is added
1932 * in a new line; otherwise it's appended to the current value of the last
1936 add_tablespace_footer(printTableContent
*const cont
, char relkind
,
1937 Oid tablespace
, const bool newline
)
1939 /* relkinds for which we support tablespaces */
1940 if (relkind
== 'r' || relkind
== 'i')
1943 * We ignore the database default tablespace so that users not using
1944 * tablespaces don't need to know about them. This case also covers
1945 * pre-8.0 servers, for which tablespace will always be 0.
1947 if (tablespace
!= 0)
1949 PGresult
*result
= NULL
;
1950 PQExpBufferData buf
;
1952 initPQExpBuffer(&buf
);
1953 printfPQExpBuffer(&buf
,
1954 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
1955 "WHERE oid = '%u'", tablespace
);
1956 result
= PSQLexec(buf
.data
, false);
1959 /* Should always be the case, but.... */
1960 if (PQntuples(result
) > 0)
1964 /* Add the tablespace as a new footer */
1965 printfPQExpBuffer(&buf
, _("Tablespace: \"%s\""),
1966 PQgetvalue(result
, 0, 0));
1967 printTableAddFooter(cont
, buf
.data
);
1971 /* Append the tablespace to the latest footer */
1972 printfPQExpBuffer(&buf
, "%s", cont
->footer
->data
);
1975 * translator: before this string there's an index
1976 * description like '"foo_pkey" PRIMARY KEY, btree (a)'
1978 appendPQExpBuffer(&buf
, _(", tablespace \"%s\""),
1979 PQgetvalue(result
, 0, 0));
1980 printTableSetFooter(cont
, buf
.data
);
1984 termPQExpBuffer(&buf
);
1992 * Describes roles. Any schema portion of the pattern is ignored.
1995 describeRoles(const char *pattern
, bool verbose
)
1997 PQExpBufferData buf
;
1999 printTableContent cont
;
2000 printTableOpt myopt
= pset
.popt
.topt
;
2005 const char align
= 'l';
2008 initPQExpBuffer(&buf
);
2010 if (pset
.sversion
>= 80100)
2012 printfPQExpBuffer(&buf
,
2013 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2014 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2015 " r.rolconnlimit,\n"
2016 " ARRAY(SELECT b.rolname\n"
2017 " FROM pg_catalog.pg_auth_members m\n"
2018 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2019 " WHERE m.member = r.oid) as memberof");
2021 if (verbose
&& pset
.sversion
>= 80200)
2023 appendPQExpBufferStr(&buf
, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2027 appendPQExpBufferStr(&buf
, "\nFROM pg_catalog.pg_roles r\n");
2029 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2030 NULL
, "r.rolname", NULL
, NULL
);
2034 printfPQExpBuffer(&buf
,
2035 "SELECT u.usename AS rolname,\n"
2036 " u.usesuper AS rolsuper,\n"
2037 " true AS rolinherit, false AS rolcreaterole,\n"
2038 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2039 " -1 AS rolconnlimit,\n"
2040 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2041 "\nFROM pg_catalog.pg_user u\n");
2043 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2044 NULL
, "u.usename", NULL
, NULL
);
2047 appendPQExpBuffer(&buf
, "ORDER BY 1;");
2049 res
= PSQLexec(buf
.data
, false);
2053 nrows
= PQntuples(res
);
2054 attr
= pg_malloc_zero((nrows
+ 1) * sizeof(*attr
));
2056 printTableInit(&cont
, &myopt
, _("List of roles"), ncols
, nrows
);
2058 printTableAddHeader(&cont
, gettext_noop("Role name"), true, align
);
2059 printTableAddHeader(&cont
, gettext_noop("Attributes"), true, align
);
2060 printTableAddHeader(&cont
, gettext_noop("Member of"), true, align
);
2062 if (verbose
&& pset
.sversion
>= 80200)
2063 printTableAddHeader(&cont
, gettext_noop("Description"), true, align
);
2065 for (i
= 0; i
< nrows
; i
++)
2067 printTableAddCell(&cont
, PQgetvalue(res
, i
, 0), false);
2069 resetPQExpBuffer(&buf
);
2070 if (strcmp(PQgetvalue(res
, i
, 1), "t") == 0)
2071 add_role_attribute(&buf
, _("Superuser"));
2073 if (strcmp(PQgetvalue(res
, i
, 2), "t") != 0)
2074 add_role_attribute(&buf
, _("No inheritance"));
2076 if (strcmp(PQgetvalue(res
, i
, 3), "t") == 0)
2077 add_role_attribute(&buf
, _("Create role"));
2079 if (strcmp(PQgetvalue(res
, i
, 4), "t") == 0)
2080 add_role_attribute(&buf
, _("Create DB"));
2082 if (strcmp(PQgetvalue(res
, i
, 5), "t") != 0)
2083 add_role_attribute(&buf
, _("Cannot login"));
2085 conns
= atoi(PQgetvalue(res
, i
, 6));
2089 appendPQExpBufferStr(&buf
, "\n");
2092 appendPQExpBuffer(&buf
, _("No connections"));
2094 appendPQExpBuffer(&buf
, ngettext("%d connection",
2100 attr
[i
] = pg_strdup(buf
.data
);
2102 printTableAddCell(&cont
, attr
[i
], false);
2104 printTableAddCell(&cont
, PQgetvalue(res
, i
, 7), false);
2106 if (verbose
&& pset
.sversion
>= 80200)
2107 printTableAddCell(&cont
, PQgetvalue(res
, i
, 8), false);
2109 termPQExpBuffer(&buf
);
2111 printTable(&cont
, pset
.queryFout
, pset
.logfile
);
2112 printTableCleanup(&cont
);
2114 for (i
= 0; i
< nrows
; i
++)
2123 add_role_attribute(PQExpBuffer buf
, const char *const str
)
2126 appendPQExpBufferStr(buf
, "\n");
2128 appendPQExpBufferStr(buf
, str
);
2135 * handler for \dt, \di, etc.
2137 * tabtypes is an array of characters, specifying what info is desired:
2142 * (any order of the above is fine)
2143 * If tabtypes is empty, we default to \dtvs.
2146 listTables(const char *tabtypes
, const char *pattern
, bool verbose
, bool showSystem
)
2148 bool showTables
= strchr(tabtypes
, 't') != NULL
;
2149 bool showIndexes
= strchr(tabtypes
, 'i') != NULL
;
2150 bool showViews
= strchr(tabtypes
, 'v') != NULL
;
2151 bool showSeq
= strchr(tabtypes
, 's') != NULL
;
2153 PQExpBufferData buf
;
2155 printQueryOpt myopt
= pset
.popt
;
2156 static const bool translate_columns
[] = {false, false, true, false, false, false, false};
2158 if (!(showTables
|| showIndexes
|| showViews
|| showSeq
))
2159 showTables
= showViews
= showSeq
= true;
2161 initPQExpBuffer(&buf
);
2164 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2165 * for backwards compatibility.
2167 printfPQExpBuffer(&buf
,
2168 "SELECT n.nspname as \"%s\",\n"
2169 " c.relname as \"%s\",\n"
2170 " 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"
2171 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2172 gettext_noop("Schema"),
2173 gettext_noop("Name"),
2174 gettext_noop("table"),
2175 gettext_noop("view"),
2176 gettext_noop("index"),
2177 gettext_noop("sequence"),
2178 gettext_noop("special"),
2179 gettext_noop("Type"),
2180 gettext_noop("Owner"));
2183 appendPQExpBuffer(&buf
,
2184 ",\n c2.relname as \"%s\"",
2185 gettext_noop("Table"));
2187 if (verbose
&& pset
.sversion
>= 80100)
2188 appendPQExpBuffer(&buf
,
2189 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2190 gettext_noop("Size"));
2192 appendPQExpBuffer(&buf
,
2193 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2194 gettext_noop("Description"));
2196 appendPQExpBuffer(&buf
,
2197 "\nFROM pg_catalog.pg_class c"
2198 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2200 appendPQExpBuffer(&buf
,
2201 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2202 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2204 appendPQExpBuffer(&buf
, "\nWHERE c.relkind IN (");
2206 appendPQExpBuffer(&buf
, "'r',");
2208 appendPQExpBuffer(&buf
, "'v',");
2210 appendPQExpBuffer(&buf
, "'i',");
2212 appendPQExpBuffer(&buf
, "'S',");
2213 if (showSystem
|| pattern
)
2214 appendPQExpBuffer(&buf
, "'s',"); /* was RELKIND_SPECIAL in <=
2216 appendPQExpBuffer(&buf
, "''"); /* dummy */
2217 appendPQExpBuffer(&buf
, ")\n");
2219 if (!showSystem
&& !pattern
)
2220 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
2221 " AND n.nspname <> 'information_schema'\n");
2224 * TOAST objects are suppressed unconditionally. Since we don't provide
2225 * any way to select relkind 't' above, we would never show toast tables
2226 * in any case; it seems a bit confusing to allow their indexes to be
2227 * shown. Use plain \d if you really need to look at a TOAST table/index.
2229 appendPQExpBuffer(&buf
, " AND n.nspname !~ '^pg_toast'\n");
2231 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2232 "n.nspname", "c.relname", NULL
,
2233 "pg_catalog.pg_table_is_visible(c.oid)");
2235 appendPQExpBuffer(&buf
, "ORDER BY 1,2;");
2237 res
= PSQLexec(buf
.data
, false);
2238 termPQExpBuffer(&buf
);
2242 if (PQntuples(res
) == 0 && !pset
.quiet
)
2245 fprintf(pset
.queryFout
, _("No matching relations found.\n"));
2247 fprintf(pset
.queryFout
, _("No relations found.\n"));
2251 myopt
.nullPrint
= NULL
;
2252 myopt
.title
= _("List of relations");
2253 myopt
.translate_header
= true;
2254 myopt
.translate_columns
= translate_columns
;
2256 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2267 * Describes domains.
2270 listDomains(const char *pattern
, bool showSystem
)
2272 PQExpBufferData buf
;
2274 printQueryOpt myopt
= pset
.popt
;
2276 initPQExpBuffer(&buf
);
2278 printfPQExpBuffer(&buf
,
2279 "SELECT n.nspname as \"%s\",\n"
2280 " t.typname as \"%s\",\n"
2281 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2282 " CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
2283 " WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
2284 " WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
2287 " pg_catalog.array_to_string(ARRAY(\n"
2288 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2289 " ), ' ') as \"%s\"\n"
2290 "FROM pg_catalog.pg_type t\n"
2291 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
2292 "WHERE t.typtype = 'd'\n",
2293 gettext_noop("Schema"),
2294 gettext_noop("Name"),
2295 gettext_noop("Type"),
2296 gettext_noop("Modifier"),
2297 gettext_noop("Check"));
2299 if (!showSystem
&& !pattern
)
2300 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
2301 " AND n.nspname <> 'information_schema'\n");
2303 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2304 "n.nspname", "t.typname", NULL
,
2305 "pg_catalog.pg_type_is_visible(t.oid)");
2307 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2309 res
= PSQLexec(buf
.data
, false);
2310 termPQExpBuffer(&buf
);
2314 myopt
.nullPrint
= NULL
;
2315 myopt
.title
= _("List of domains");
2316 myopt
.translate_header
= true;
2318 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2327 * Describes conversions.
2330 listConversions(const char *pattern
, bool showSystem
)
2332 PQExpBufferData buf
;
2334 printQueryOpt myopt
= pset
.popt
;
2335 static const bool translate_columns
[] = {false, false, false, false, true};
2337 initPQExpBuffer(&buf
);
2339 printfPQExpBuffer(&buf
,
2340 "SELECT n.nspname AS \"%s\",\n"
2341 " c.conname AS \"%s\",\n"
2342 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2343 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2344 " CASE WHEN c.condefault THEN '%s'\n"
2345 " ELSE '%s' END AS \"%s\"\n"
2346 "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
2347 "WHERE n.oid = c.connamespace\n",
2348 gettext_noop("Schema"),
2349 gettext_noop("Name"),
2350 gettext_noop("Source"),
2351 gettext_noop("Destination"),
2352 gettext_noop("yes"), gettext_noop("no"),
2353 gettext_noop("Default?"));
2355 if (!showSystem
&& !pattern
)
2356 appendPQExpBuffer(&buf
, " AND n.nspname <> 'pg_catalog'\n"
2357 " AND n.nspname <> 'information_schema'\n");
2359 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2360 "n.nspname", "c.conname", NULL
,
2361 "pg_catalog.pg_conversion_is_visible(c.oid)");
2363 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2365 res
= PSQLexec(buf
.data
, false);
2366 termPQExpBuffer(&buf
);
2370 myopt
.nullPrint
= NULL
;
2371 myopt
.title
= _("List of conversions");
2372 myopt
.translate_header
= true;
2373 myopt
.translate_columns
= translate_columns
;
2375 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2387 listCasts(const char *pattern
)
2389 PQExpBufferData buf
;
2391 printQueryOpt myopt
= pset
.popt
;
2392 static const bool translate_columns
[] = {false, false, false, true};
2394 initPQExpBuffer(&buf
);
2397 * We need a left join to pg_proc for binary casts; the others are just
2398 * paranoia. Also note that we don't attempt to localize '(binary
2399 * coercible)', because there's too much risk of gettext translating a
2400 * function name that happens to match some string in the PO database.
2402 printfPQExpBuffer(&buf
,
2403 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2404 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2405 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
2408 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2409 " WHEN c.castcontext = 'a' THEN '%s'\n"
2412 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2413 " ON c.castfunc = p.oid\n"
2414 " LEFT JOIN pg_catalog.pg_type ts\n"
2415 " ON c.castsource = ts.oid\n"
2416 " LEFT JOIN pg_catalog.pg_namespace ns\n"
2417 " ON ns.oid = ts.typnamespace\n"
2418 " LEFT JOIN pg_catalog.pg_type tt\n"
2419 " ON c.casttarget = tt.oid\n"
2420 " LEFT JOIN pg_catalog.pg_namespace nt\n"
2421 " ON nt.oid = tt.typnamespace\n"
2423 gettext_noop("Source type"),
2424 gettext_noop("Target type"),
2425 gettext_noop("Function"),
2426 gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2427 gettext_noop("Implicit?"));
2430 * Match name pattern against either internal or external name of either
2431 * castsource or casttarget
2433 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2434 "ns.nspname", "ts.typname",
2435 "pg_catalog.format_type(ts.oid, NULL)",
2436 "pg_catalog.pg_type_is_visible(ts.oid)");
2438 appendPQExpBuffer(&buf
, ") OR (true");
2440 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2441 "nt.nspname", "tt.typname",
2442 "pg_catalog.format_type(tt.oid, NULL)",
2443 "pg_catalog.pg_type_is_visible(tt.oid)");
2445 appendPQExpBuffer(&buf
, ")\nORDER BY 1, 2;");
2447 res
= PSQLexec(buf
.data
, false);
2448 termPQExpBuffer(&buf
);
2452 myopt
.nullPrint
= NULL
;
2453 myopt
.title
= _("List of casts");
2454 myopt
.translate_header
= true;
2455 myopt
.translate_columns
= translate_columns
;
2457 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2466 * Describes schemas (namespaces)
2469 listSchemas(const char *pattern
, bool verbose
)
2471 PQExpBufferData buf
;
2473 printQueryOpt myopt
= pset
.popt
;
2475 initPQExpBuffer(&buf
);
2476 printfPQExpBuffer(&buf
,
2477 "SELECT n.nspname AS \"%s\",\n"
2478 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2479 gettext_noop("Name"),
2480 gettext_noop("Owner"));
2484 appendPQExpBuffer(&buf
, ",\n ");
2485 printACLColumn(&buf
, "n.nspacl");
2486 appendPQExpBuffer(&buf
,
2487 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
2488 gettext_noop("Description"));
2491 appendPQExpBuffer(&buf
,
2492 "\nFROM pg_catalog.pg_namespace n\n"
2493 "WHERE (n.nspname !~ '^pg_temp_' OR\n"
2494 " n.nspname = (pg_catalog.current_schemas(true))[1])\n"); /* temp schema is first */
2496 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2497 NULL
, "n.nspname", NULL
,
2500 appendPQExpBuffer(&buf
, "ORDER BY 1;");
2502 res
= PSQLexec(buf
.data
, false);
2503 termPQExpBuffer(&buf
);
2507 myopt
.nullPrint
= NULL
;
2508 myopt
.title
= _("List of schemas");
2509 myopt
.translate_header
= true;
2511 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2520 * list text search parsers
2523 listTSParsers(const char *pattern
, bool verbose
)
2525 PQExpBufferData buf
;
2527 printQueryOpt myopt
= pset
.popt
;
2529 if (pset
.sversion
< 80300)
2531 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2532 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2537 return listTSParsersVerbose(pattern
);
2539 initPQExpBuffer(&buf
);
2541 printfPQExpBuffer(&buf
,
2543 " n.nspname as \"%s\",\n"
2544 " p.prsname as \"%s\",\n"
2545 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2546 "FROM pg_catalog.pg_ts_parser p \n"
2547 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2548 gettext_noop("Schema"),
2549 gettext_noop("Name"),
2550 gettext_noop("Description")
2553 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2554 "n.nspname", "p.prsname", NULL
,
2555 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2557 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2559 res
= PSQLexec(buf
.data
, false);
2560 termPQExpBuffer(&buf
);
2564 myopt
.nullPrint
= NULL
;
2565 myopt
.title
= _("List of text search parsers");
2566 myopt
.translate_header
= true;
2568 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2575 * full description of parsers
2578 listTSParsersVerbose(const char *pattern
)
2580 PQExpBufferData buf
;
2584 initPQExpBuffer(&buf
);
2586 printfPQExpBuffer(&buf
,
2590 "FROM pg_catalog.pg_ts_parser p\n"
2591 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2594 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2595 "n.nspname", "p.prsname", NULL
,
2596 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2598 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2600 res
= PSQLexec(buf
.data
, false);
2601 termPQExpBuffer(&buf
);
2605 if (PQntuples(res
) == 0)
2608 fprintf(stderr
, _("Did not find any text search parser named \"%s\".\n"),
2614 for (i
= 0; i
< PQntuples(res
); i
++)
2617 const char *nspname
= NULL
;
2618 const char *prsname
;
2620 oid
= PQgetvalue(res
, i
, 0);
2621 if (!PQgetisnull(res
, i
, 1))
2622 nspname
= PQgetvalue(res
, i
, 1);
2623 prsname
= PQgetvalue(res
, i
, 2);
2625 if (!describeOneTSParser(oid
, nspname
, prsname
))
2643 describeOneTSParser(const char *oid
, const char *nspname
, const char *prsname
)
2645 PQExpBufferData buf
;
2648 printQueryOpt myopt
= pset
.popt
;
2649 static const bool translate_columns
[] = {true, false, false};
2651 initPQExpBuffer(&buf
);
2653 printfPQExpBuffer(&buf
,
2654 "SELECT '%s' AS \"%s\", \n"
2655 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
2656 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
2657 " FROM pg_catalog.pg_ts_parser p \n"
2658 " WHERE p.oid = '%s' \n"
2661 " p.prstoken::pg_catalog.regproc, \n"
2662 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
2663 " FROM pg_catalog.pg_ts_parser p \n"
2664 " WHERE p.oid = '%s' \n"
2667 " p.prsend::pg_catalog.regproc, \n"
2668 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
2669 " FROM pg_catalog.pg_ts_parser p \n"
2670 " WHERE p.oid = '%s' \n"
2673 " p.prsheadline::pg_catalog.regproc, \n"
2674 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
2675 " FROM pg_catalog.pg_ts_parser p \n"
2676 " WHERE p.oid = '%s' \n"
2679 " p.prslextype::pg_catalog.regproc, \n"
2680 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
2681 " FROM pg_catalog.pg_ts_parser p \n"
2682 " WHERE p.oid = '%s' \n",
2683 gettext_noop("Start parse"),
2684 gettext_noop("Method"),
2685 gettext_noop("Function"),
2686 gettext_noop("Description"),
2688 gettext_noop("Get next token"),
2690 gettext_noop("End parse"),
2692 gettext_noop("Get headline"),
2694 gettext_noop("Get token types"),
2697 res
= PSQLexec(buf
.data
, false);
2698 termPQExpBuffer(&buf
);
2702 myopt
.nullPrint
= NULL
;
2704 sprintf(title
, _("Text search parser \"%s.%s\""), nspname
, prsname
);
2706 sprintf(title
, _("Text search parser \"%s\""), prsname
);
2707 myopt
.title
= title
;
2708 myopt
.footers
= NULL
;
2709 myopt
.default_footer
= false;
2710 myopt
.translate_header
= true;
2711 myopt
.translate_columns
= translate_columns
;
2713 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2717 initPQExpBuffer(&buf
);
2719 printfPQExpBuffer(&buf
,
2720 "SELECT t.alias as \"%s\", \n"
2721 " t.description as \"%s\" \n"
2722 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
2724 gettext_noop("Token name"),
2725 gettext_noop("Description"),
2728 res
= PSQLexec(buf
.data
, false);
2729 termPQExpBuffer(&buf
);
2733 myopt
.nullPrint
= NULL
;
2735 sprintf(title
, _("Token types for parser \"%s.%s\""), nspname
, prsname
);
2737 sprintf(title
, _("Token types for parser \"%s\""), prsname
);
2738 myopt
.title
= title
;
2739 myopt
.footers
= NULL
;
2740 myopt
.default_footer
= true;
2741 myopt
.translate_header
= true;
2742 myopt
.translate_columns
= NULL
;
2744 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2753 * list text search dictionaries
2756 listTSDictionaries(const char *pattern
, bool verbose
)
2758 PQExpBufferData buf
;
2760 printQueryOpt myopt
= pset
.popt
;
2762 if (pset
.sversion
< 80300)
2764 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2765 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2769 initPQExpBuffer(&buf
);
2771 printfPQExpBuffer(&buf
,
2773 " n.nspname as \"%s\",\n"
2774 " d.dictname as \"%s\",\n",
2775 gettext_noop("Schema"),
2776 gettext_noop("Name"));
2780 appendPQExpBuffer(&buf
,
2781 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
2782 " pg_catalog.pg_ts_template t \n"
2783 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
2784 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
2785 " d.dictinitoption as \"%s\", \n",
2786 gettext_noop("Template"),
2787 gettext_noop("Init options"));
2790 appendPQExpBuffer(&buf
,
2791 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
2792 gettext_noop("Description"));
2794 appendPQExpBuffer(&buf
, "FROM pg_catalog.pg_ts_dict d\n"
2795 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
2797 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2798 "n.nspname", "d.dictname", NULL
,
2799 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
2801 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2803 res
= PSQLexec(buf
.data
, false);
2804 termPQExpBuffer(&buf
);
2808 myopt
.nullPrint
= NULL
;
2809 myopt
.title
= _("List of text search dictionaries");
2810 myopt
.translate_header
= true;
2812 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2821 * list text search templates
2824 listTSTemplates(const char *pattern
, bool verbose
)
2826 PQExpBufferData buf
;
2828 printQueryOpt myopt
= pset
.popt
;
2830 if (pset
.sversion
< 80300)
2832 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2833 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2837 initPQExpBuffer(&buf
);
2840 printfPQExpBuffer(&buf
,
2842 " n.nspname AS \"%s\",\n"
2843 " t.tmplname AS \"%s\",\n"
2844 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
2845 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
2846 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2847 gettext_noop("Schema"),
2848 gettext_noop("Name"),
2849 gettext_noop("Init"),
2850 gettext_noop("Lexize"),
2851 gettext_noop("Description"));
2853 printfPQExpBuffer(&buf
,
2855 " n.nspname AS \"%s\",\n"
2856 " t.tmplname AS \"%s\",\n"
2857 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2858 gettext_noop("Schema"),
2859 gettext_noop("Name"),
2860 gettext_noop("Description"));
2862 appendPQExpBuffer(&buf
, "FROM pg_catalog.pg_ts_template t\n"
2863 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
2865 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2866 "n.nspname", "t.tmplname", NULL
,
2867 "pg_catalog.pg_ts_template_is_visible(t.oid)");
2869 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2871 res
= PSQLexec(buf
.data
, false);
2872 termPQExpBuffer(&buf
);
2876 myopt
.nullPrint
= NULL
;
2877 myopt
.title
= _("List of text search templates");
2878 myopt
.translate_header
= true;
2880 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2889 * list text search configurations
2892 listTSConfigs(const char *pattern
, bool verbose
)
2894 PQExpBufferData buf
;
2896 printQueryOpt myopt
= pset
.popt
;
2898 if (pset
.sversion
< 80300)
2900 fprintf(stderr
, _("The server (version %d.%d) does not support full text search.\n"),
2901 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
2906 return listTSConfigsVerbose(pattern
);
2908 initPQExpBuffer(&buf
);
2910 printfPQExpBuffer(&buf
,
2912 " n.nspname as \"%s\",\n"
2913 " c.cfgname as \"%s\",\n"
2914 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
2915 "FROM pg_catalog.pg_ts_config c\n"
2916 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
2917 gettext_noop("Schema"),
2918 gettext_noop("Name"),
2919 gettext_noop("Description")
2922 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
2923 "n.nspname", "c.cfgname", NULL
,
2924 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2926 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
2928 res
= PSQLexec(buf
.data
, false);
2929 termPQExpBuffer(&buf
);
2933 myopt
.nullPrint
= NULL
;
2934 myopt
.title
= _("List of text search configurations");
2935 myopt
.translate_header
= true;
2937 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
2944 listTSConfigsVerbose(const char *pattern
)
2946 PQExpBufferData buf
;
2950 initPQExpBuffer(&buf
);
2952 printfPQExpBuffer(&buf
,
2953 "SELECT c.oid, c.cfgname,\n"
2956 " np.nspname as pnspname \n"
2957 "FROM pg_catalog.pg_ts_config c \n"
2958 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
2959 " pg_catalog.pg_ts_parser p \n"
2960 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
2961 "WHERE p.oid = c.cfgparser\n"
2964 processSQLNamePattern(pset
.db
, &buf
, pattern
, true, false,
2965 "n.nspname", "c.cfgname", NULL
,
2966 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2968 appendPQExpBuffer(&buf
, "ORDER BY 3, 2;");
2970 res
= PSQLexec(buf
.data
, false);
2971 termPQExpBuffer(&buf
);
2975 if (PQntuples(res
) == 0)
2978 fprintf(stderr
, _("Did not find any text search configuration named \"%s\".\n"),
2984 for (i
= 0; i
< PQntuples(res
); i
++)
2987 const char *cfgname
;
2988 const char *nspname
= NULL
;
2989 const char *prsname
;
2990 const char *pnspname
= NULL
;
2992 oid
= PQgetvalue(res
, i
, 0);
2993 cfgname
= PQgetvalue(res
, i
, 1);
2994 if (!PQgetisnull(res
, i
, 2))
2995 nspname
= PQgetvalue(res
, i
, 2);
2996 prsname
= PQgetvalue(res
, i
, 3);
2997 if (!PQgetisnull(res
, i
, 4))
2998 pnspname
= PQgetvalue(res
, i
, 4);
3000 if (!describeOneTSConfig(oid
, nspname
, cfgname
, pnspname
, prsname
))
3018 describeOneTSConfig(const char *oid
, const char *nspname
, const char *cfgname
,
3019 const char *pnspname
, const char *prsname
)
3021 PQExpBufferData buf
,
3024 printQueryOpt myopt
= pset
.popt
;
3026 initPQExpBuffer(&buf
);
3028 printfPQExpBuffer(&buf
,
3030 " ( SELECT t.alias FROM \n"
3031 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3032 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3033 " pg_catalog.btrim( \n"
3034 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3035 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3036 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3037 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3038 " ) :: pg_catalog.text , \n"
3039 " '{}') AS \"%s\" \n"
3040 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3041 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3042 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3044 gettext_noop("Token"),
3045 gettext_noop("Dictionaries"),
3048 res
= PSQLexec(buf
.data
, false);
3049 termPQExpBuffer(&buf
);
3053 initPQExpBuffer(&title
);
3056 appendPQExpBuffer(&title
, _("Text search configuration \"%s.%s\""),
3059 appendPQExpBuffer(&title
, _("Text search configuration \"%s\""),
3063 appendPQExpBuffer(&title
, _("\nParser: \"%s.%s\""),
3066 appendPQExpBuffer(&title
, _("\nParser: \"%s\""),
3069 myopt
.nullPrint
= NULL
;
3070 myopt
.title
= title
.data
;
3071 myopt
.footers
= NULL
;
3072 myopt
.default_footer
= false;
3073 myopt
.translate_header
= true;
3075 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3077 termPQExpBuffer(&title
);
3087 * Describes foreign-data wrappers
3090 listForeignDataWrappers(const char *pattern
, bool verbose
)
3092 PQExpBufferData buf
;
3094 printQueryOpt myopt
= pset
.popt
;
3096 if (pset
.sversion
< 80400)
3098 fprintf(stderr
, _("The server (version %d.%d) does not support foreign-data wrappers.\n"),
3099 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
3103 initPQExpBuffer(&buf
);
3104 printfPQExpBuffer(&buf
,
3105 "SELECT fdwname AS \"%s\",\n"
3106 " pg_catalog.pg_get_userbyid(fdwowner) AS \"%s\",\n"
3107 " fdwvalidator::pg_catalog.regproc AS \"%s\"",
3108 gettext_noop("Name"),
3109 gettext_noop("Owner"),
3110 gettext_noop("Validator"));
3114 appendPQExpBuffer(&buf
, ",\n ");
3115 printACLColumn(&buf
, "fdwacl");
3116 appendPQExpBuffer(&buf
,
3117 ",\n fdwoptions AS \"%s\"",
3118 gettext_noop("Options"));
3121 appendPQExpBuffer(&buf
, "\nFROM pg_catalog.pg_foreign_data_wrapper\n");
3123 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
3124 NULL
, "fdwname", NULL
, NULL
);
3126 appendPQExpBuffer(&buf
, "ORDER BY 1;");
3128 res
= PSQLexec(buf
.data
, false);
3129 termPQExpBuffer(&buf
);
3133 myopt
.nullPrint
= NULL
;
3134 myopt
.title
= _("List of foreign-data wrappers");
3135 myopt
.translate_header
= true;
3137 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3146 * Describes foreign servers.
3149 listForeignServers(const char *pattern
, bool verbose
)
3151 PQExpBufferData buf
;
3153 printQueryOpt myopt
= pset
.popt
;
3155 if (pset
.sversion
< 80400)
3157 fprintf(stderr
, _("The server (version %d.%d) does not support foreign servers.\n"),
3158 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
3162 initPQExpBuffer(&buf
);
3163 printfPQExpBuffer(&buf
,
3164 "SELECT s.srvname AS \"%s\",\n"
3165 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3166 " f.fdwname AS \"%s\"",
3167 gettext_noop("Name"),
3168 gettext_noop("Owner"),
3169 gettext_noop("Foreign-data wrapper"));
3173 appendPQExpBuffer(&buf
, ",\n ");
3174 printACLColumn(&buf
, "s.srvacl");
3175 appendPQExpBuffer(&buf
,
3177 " s.srvtype AS \"%s\",\n"
3178 " s.srvversion AS \"%s\",\n"
3179 " s.srvoptions AS \"%s\"",
3180 gettext_noop("Type"),
3181 gettext_noop("Version"),
3182 gettext_noop("Options"));
3185 appendPQExpBuffer(&buf
,
3186 "\nFROM pg_catalog.pg_foreign_server s\n"
3187 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3189 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
3190 NULL
, "s.srvname", NULL
, NULL
);
3192 appendPQExpBuffer(&buf
, "ORDER BY 1;");
3194 res
= PSQLexec(buf
.data
, false);
3195 termPQExpBuffer(&buf
);
3199 myopt
.nullPrint
= NULL
;
3200 myopt
.title
= _("List of foreign servers");
3201 myopt
.translate_header
= true;
3203 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3212 * Describes user mappings.
3215 listUserMappings(const char *pattern
, bool verbose
)
3217 PQExpBufferData buf
;
3219 printQueryOpt myopt
= pset
.popt
;
3221 if (pset
.sversion
< 80400)
3223 fprintf(stderr
, _("The server (version %d.%d) does not support user mappings.\n"),
3224 pset
.sversion
/ 10000, (pset
.sversion
/ 100) % 100);
3228 initPQExpBuffer(&buf
);
3229 printfPQExpBuffer(&buf
,
3230 "SELECT um.srvname AS \"%s\",\n"
3231 " um.usename AS \"%s\"",
3232 gettext_noop("Server"),
3233 gettext_noop("User name"));
3236 appendPQExpBuffer(&buf
,
3237 ",\n um.umoptions AS \"%s\"",
3238 gettext_noop("Options"));
3240 appendPQExpBuffer(&buf
, "\nFROM pg_catalog.pg_user_mappings um\n");
3242 processSQLNamePattern(pset
.db
, &buf
, pattern
, false, false,
3243 NULL
, "um.srvname", "um.usename", NULL
);
3245 appendPQExpBuffer(&buf
, "ORDER BY 1, 2;");
3247 res
= PSQLexec(buf
.data
, false);
3248 termPQExpBuffer(&buf
);
3252 myopt
.nullPrint
= NULL
;
3253 myopt
.title
= _("List of user mappings");
3254 myopt
.translate_header
= true;
3256 printQuery(res
, &myopt
, pset
.queryFout
, pset
.logfile
);
3265 * Helper function for consistently formatting ACL (privilege) columns.
3266 * The proper targetlist entry is appended to buf. Note lack of any
3267 * whitespace or comma decoration.
3270 printACLColumn(PQExpBuffer buf
, const char *colname
)
3272 if (pset
.sversion
>= 80100)
3273 appendPQExpBuffer(buf
,
3274 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
3275 colname
, gettext_noop("Access privileges"));
3277 appendPQExpBuffer(buf
,
3278 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
3279 colname
, gettext_noop("Access privileges"));