psql backward compatibility fix
[PostgreSQL.git] / src / bin / psql / describe.c
blobe89b99d6c1c04f89138cc9511353d7cb2521ae00
1 /*
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
11 * $PostgreSQL$
13 #include "postgres_fe.h"
15 #include <ctype.h>
17 #include "common.h"
18 #include "describe.h"
19 #include "dumputils.h"
20 #include "mbprint.h"
21 #include "print.h"
22 #include "settings.h"
23 #include "variables.h"
26 static bool describeOneTableDetails(const char *schemaname,
27 const char *relationname,
28 const char *oid,
29 bool verbose);
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,
35 const char *prsname);
36 static bool listTSConfigsVerbose(const char *pattern);
37 static bool describeOneTSConfig(const char *oid, const char *nspname,
38 const char *cfgname,
39 const char *pnspname, const char *prsname);
40 static void printACLColumn(PQExpBuffer buf, const char *colname);
43 /*----------------
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.
48 *----------------
52 /* \da
53 * Takes an optional regexp to select particular aggregates
55 bool
56 describeAggregates(const char *pattern, bool verbose, bool showSystem)
58 PQExpBufferData buf;
59 PGresult *res;
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"),
69 gettext_noop("Name"),
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"
76 " ELSE\n"
77 " pg_catalog.array_to_string(ARRAY(\n"
78 " SELECT\n"
79 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
80 " FROM\n"
81 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
82 " ), ', ')\n"
83 " END AS \"%s\",\n",
84 gettext_noop("Argument data types"));
85 else
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"
94 "WHERE p.proisagg\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);
109 if (!res)
110 return false;
112 myopt.nullPrint = NULL;
113 myopt.title = _("List of aggregate functions");
114 myopt.translate_header = true;
116 printQuery(res, &myopt, pset.queryFout, pset.logfile);
118 PQclear(res);
119 return true;
122 /* \db
123 * Takes an optional regexp to select particular tablespaces
125 bool
126 describeTablespaces(const char *pattern, bool verbose)
128 PQExpBufferData buf;
129 PGresult *res;
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);
136 return true;
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"));
149 if (verbose)
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,
165 NULL);
167 appendPQExpBuffer(&buf, "ORDER BY 1;");
169 res = PSQLexec(buf.data, false);
170 termPQExpBuffer(&buf);
171 if (!res)
172 return false;
174 myopt.nullPrint = NULL;
175 myopt.title = _("List of tablespaces");
176 myopt.translate_header = true;
178 printQuery(res, &myopt, pset.queryFout, pset.logfile);
180 PQclear(res);
181 return true;
185 /* \df
186 * Takes an optional regexp to select particular functions.
188 * As with \d, you can specify the kinds of functions you want:
190 * a for aggregates
191 * n for normal
192 * t for trigger
193 * w for window
195 * and you can mix and match these in any order.
197 bool
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;
204 bool have_where;
205 PQExpBufferData buf;
206 PGresult *res;
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"));
213 return true;
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);
220 return true;
223 if (!showAggregate && !showNormal && !showTrigger && !showWindow)
225 showAggregate = showNormal = showTrigger = true;
226 if (pset.sversion >= 80400)
227 showWindow = true;
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"
242 " CASE\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"
246 " ELSE '%s'\n"
247 "END as \"%s\"",
248 gettext_noop("Result data type"),
249 gettext_noop("Argument data types"),
250 /* translator: "agg" is short for "aggregate" */
251 gettext_noop("agg"),
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"
262 " SELECT\n"
263 " CASE\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"
268 " END ||\n"
269 " CASE\n"
270 " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
271 " ELSE p.proargnames[s.i] || ' ' \n"
272 " END ||\n"
273 " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
274 " FROM\n"
275 " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
276 " ), ', ')\n"
277 " ELSE\n"
278 " pg_catalog.array_to_string(ARRAY(\n"
279 " SELECT\n"
280 " CASE\n"
281 " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
282 " ELSE p.proargnames[s.i+1] || ' '\n"
283 " END ||\n"
284 " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
285 " FROM\n"
286 " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
287 " ), ', ')\n"
288 " END AS \"%s\",\n"
289 " CASE\n"
290 " WHEN p.proisagg THEN '%s'\n"
291 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
292 " ELSE '%s'\n"
293 " END AS \"%s\"",
294 gettext_noop("Result data type"),
295 gettext_noop("Argument data types"),
296 /* translator: "agg" is short for "aggregate" */
297 gettext_noop("agg"),
298 gettext_noop("trigger"),
299 gettext_noop("normal"),
300 gettext_noop("Type"));
301 else
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"
306 " CASE\n"
307 " WHEN p.proisagg THEN '%s'\n"
308 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
309 " ELSE '%s'\n"
310 " END AS \"%s\"",
311 gettext_noop("Result data type"),
312 gettext_noop("Argument data types"),
313 /* translator: "agg" is short for "aggregate" */
314 gettext_noop("agg"),
315 gettext_noop("trigger"),
316 gettext_noop("normal"),
317 gettext_noop("Type"));
319 if (verbose)
320 appendPQExpBuffer(&buf,
321 ",\n CASE\n"
322 " WHEN p.provolatile = 'i' THEN '%s'\n"
323 " WHEN p.provolatile = 's' THEN '%s'\n"
324 " WHEN p.provolatile = 'v' THEN '%s'\n"
325 "END as \"%s\""
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");
343 if (verbose)
344 appendPQExpBuffer(&buf,
345 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
347 have_where = false;
349 /* filter by function type, if requested */
350 if (showNormal && showAggregate && showTrigger && showWindow)
351 /* Do nothing */ ;
352 else if (showNormal)
354 if (!showAggregate)
356 if (have_where)
357 appendPQExpBuffer(&buf, " AND ");
358 else
360 appendPQExpBuffer(&buf, "WHERE ");
361 have_where = true;
363 appendPQExpBuffer(&buf, "NOT p.proisagg\n");
365 if (!showTrigger)
367 if (have_where)
368 appendPQExpBuffer(&buf, " AND ");
369 else
371 appendPQExpBuffer(&buf, "WHERE ");
372 have_where = true;
374 appendPQExpBuffer(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
376 if (!showWindow && pset.sversion >= 80400)
378 if (have_where)
379 appendPQExpBuffer(&buf, " AND ");
380 else
382 appendPQExpBuffer(&buf, "WHERE ");
383 have_where = true;
385 appendPQExpBuffer(&buf, "NOT p.proiswindow\n");
388 else
390 bool needs_or = false;
392 appendPQExpBuffer(&buf, "WHERE (\n ");
393 have_where = true;
394 /* Note: at least one of these must be true ... */
395 if (showAggregate)
397 appendPQExpBuffer(&buf, "p.proisagg\n");
398 needs_or = true;
400 if (showTrigger)
402 if (needs_or)
403 appendPQExpBuffer(&buf, " OR ");
404 appendPQExpBuffer(&buf,
405 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
406 needs_or = true;
408 if (showWindow)
410 if (needs_or)
411 appendPQExpBuffer(&buf, " OR ");
412 appendPQExpBuffer(&buf, "p.proiswindow\n");
413 needs_or = true;
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);
430 if (!res)
431 return false;
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);
440 PQclear(res);
441 return true;
447 * \dT
448 * describe types
450 bool
451 describeTypes(const char *pattern, bool verbose, bool showSystem)
453 PQExpBufferData buf;
454 PGresult *res;
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"));
464 if (verbose)
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"
472 " END AS \"%s\",\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"
478 " ARRAY(\n"
479 " SELECT e.enumlabel\n"
480 " FROM pg_catalog.pg_enum e\n"
481 " WHERE e.enumtypid = t.oid\n"
482 " ORDER BY e.oid\n"
483 " ),\n"
484 " E'\\n'\n"
485 " ) AS \"%s\",\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
497 * composite types
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");
509 else
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);
526 if (!res)
527 return false;
529 myopt.nullPrint = NULL;
530 myopt.title = _("List of data types");
531 myopt.translate_header = true;
533 printQuery(res, &myopt, pset.queryFout, pset.logfile);
535 PQclear(res);
536 return true;
540 /* \do
542 bool
543 describeOperators(const char *pattern, bool showSystem)
545 PQExpBufferData buf;
546 PGresult *res;
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);
580 if (!res)
581 return false;
583 myopt.nullPrint = NULL;
584 myopt.title = _("List of operators");
585 myopt.translate_header = true;
587 printQuery(res, &myopt, pset.queryFout, pset.logfile);
589 PQclear(res);
590 return true;
595 * listAllDbs
597 * for \l, \list, and -l switch
599 bool
600 listAllDbs(bool verbose)
602 PGresult *res;
603 PQExpBufferData buf;
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"
628 " END as \"%s\"",
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);
646 if (!res)
647 return false;
649 myopt.nullPrint = NULL;
650 myopt.title = _("List of databases");
651 myopt.translate_header = true;
653 printQuery(res, &myopt, pset.queryFout, pset.logfile);
655 PQclear(res);
656 return true;
661 * List Tables' Grant/Revoke Permissions
662 * \z (now also \dp -- perhaps more mnemonic)
664 bool
665 permissionsList(const char *pattern)
667 PQExpBufferData buf;
668 PGresult *res;
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"
681 " ",
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
706 * pg_catalog.*
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);
715 if (!res)
717 termPQExpBuffer(&buf);
718 return false;
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);
730 PQclear(res);
731 return true;
737 * Get object comments
739 * \dd [foo]
741 * Note: This only lists things that actually have a description. For complete
742 * lists of things, there are other \d? commands.
744 bool
745 objectDescription(const char *pattern, bool showSystem)
747 PQExpBufferData buf;
748 PGresult *res;
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"
756 "FROM (\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,
783 "UNION ALL\n"
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,
803 "UNION ALL\n"
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,
822 "UNION ALL\n"
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)",
837 NULL,
838 "pg_catalog.pg_type_is_visible(t.oid)");
840 /* Relation (tables, views, indexes, sequences) descriptions */
841 appendPQExpBuffer(&buf,
842 "UNION ALL\n"
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"
846 " CAST(\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,
867 "UNION ALL\n"
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,
889 "UNION ALL\n"
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,
909 ") AS tt\n"
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);
916 if (!res)
917 return false;
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);
926 PQclear(res);
927 return true;
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+
939 bool
940 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
942 PQExpBufferData buf;
943 PGresult *res;
944 int i;
946 initPQExpBuffer(&buf);
948 printfPQExpBuffer(&buf,
949 "SELECT c.oid,\n"
950 " n.nspname,\n"
951 " c.relname\n"
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);
967 if (!res)
968 return false;
970 if (PQntuples(res) == 0)
972 if (!pset.quiet)
973 fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
974 pattern);
975 PQclear(res);
976 return false;
979 for (i = 0; i < PQntuples(res); i++)
981 const char *oid;
982 const char *nspname;
983 const char *relname;
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))
991 PQclear(res);
992 return false;
994 if (cancel_pressed)
996 PQclear(res);
997 return false;
1001 PQclear(res);
1002 return true;
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.
1012 static bool
1013 describeOneTableDetails(const char *schemaname,
1014 const char *relationname,
1015 const char *oid,
1016 bool verbose)
1018 PQExpBufferData buf;
1019 PGresult *res = NULL;
1020 printTableOpt myopt = pset.popt.topt;
1021 printTableContent cont;
1022 bool printTableInitialized = false;
1023 int i;
1024 char *view_def = NULL;
1025 char *headers[6];
1026 char **seq_values = NULL;
1027 char **modifiers = NULL;
1028 char **ptr;
1029 PQExpBufferData title;
1030 PQExpBufferData tmpbuf;
1031 int cols;
1032 int numrows = 0;
1033 struct
1035 int16 checks;
1036 char relkind;
1037 bool hasindex;
1038 bool hasrules;
1039 bool hastriggers;
1040 bool hasoids;
1041 Oid tablespace;
1042 char *reloptions;
1043 } tableinfo;
1044 bool show_modifiers = false;
1045 bool retval;
1047 retval = 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",
1066 (verbose ?
1067 "pg_catalog.array_to_string(c.reloptions || "
1068 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1069 : "''"),
1070 oid);
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'",
1079 (verbose ?
1080 "pg_catalog.array_to_string(reloptions, E', ')" : "''"),
1081 oid);
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'",
1090 oid);
1092 else
1094 printfPQExpBuffer(&buf,
1095 "SELECT relchecks, relkind, relhasindex, relhasrules, "
1096 "reltriggers <> 0, relhasoids, "
1097 "'', ''\n"
1098 "FROM pg_catalog.pg_class WHERE oid = '%s'",
1099 oid);
1102 res = PSQLexec(buf.data, false);
1103 if (!res)
1104 goto error_return;
1106 /* Did we get anything? */
1107 if (PQntuples(res) == 0)
1109 if (!pset.quiet)
1110 fprintf(stderr, _("Did not find any relation with OID %s.\n"),
1111 oid);
1112 goto error_return;
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;
1125 PQclear(res);
1126 res = NULL;
1129 * If it's a sequence, fetch its values and store into an array that will
1130 * be used later.
1132 if (tableinfo.relkind == 'S')
1134 PGresult *result;
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"
1142 "FROM %s",
1143 fmtId(schemaname));
1144 /* must be separate because fmtId isn't reentrant */
1145 appendPQExpBuffer(&buf, ".%s", fmtId(relationname));
1147 result = PSQLexec(buf.data, false);
1148 if (!result)
1149 goto error_return;
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));
1156 PQclear(result);
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");
1168 if (verbose)
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);
1175 if (!res)
1176 goto error_return;
1177 numrows = PQntuples(res);
1179 /* Make title */
1180 switch (tableinfo.relkind)
1182 case 'r':
1183 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1184 schemaname, relationname);
1185 break;
1186 case 'v':
1187 printfPQExpBuffer(&title, _("View \"%s.%s\""),
1188 schemaname, relationname);
1189 break;
1190 case 'S':
1191 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1192 schemaname, relationname);
1193 break;
1194 case 'i':
1195 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1196 schemaname, relationname);
1197 break;
1198 case 's':
1199 /* not used as of 8.2, but keep it for backwards compatibility */
1200 printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1201 schemaname, relationname);
1202 break;
1203 case 't':
1204 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1205 schemaname, relationname);
1206 break;
1207 case 'c':
1208 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1209 schemaname, relationname);
1210 break;
1211 default:
1212 /* untranslated unknown relkind */
1213 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1214 tableinfo.relkind, schemaname, relationname);
1215 break;
1218 /* Set the number of columns, and their names */
1219 headers[0] = gettext_noop("Column");
1220 headers[1] = gettext_noop("Type");
1221 cols = 2;
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");
1236 if (verbose)
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')
1251 PGresult *result;
1253 printfPQExpBuffer(&buf,
1254 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)",
1255 oid);
1256 result = PSQLexec(buf.data, false);
1257 if (!result)
1258 goto error_return;
1260 if (PQntuples(result) > 0)
1261 view_def = pg_strdup(PQgetvalue(result, 0, 0));
1263 PQclear(result);
1266 /* Generate table cells to be printed */
1267 for (i = 0; i < numrows; i++)
1269 /* Column */
1270 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
1272 /* Type */
1273 printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
1275 /* Modifiers: not null and default */
1276 if (show_modifiers)
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)
1286 if (tmpbuf.len > 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 */
1306 if (verbose)
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" :
1316 "???")))),
1317 false);
1318 printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1), false);
1322 /* Make footers */
1323 if (tableinfo.relkind == 'i')
1325 /* Footer information about an index */
1326 PGresult *result;
1328 printfPQExpBuffer(&buf,
1329 "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
1330 if (pset.sversion >= 80200)
1331 appendPQExpBuffer(&buf, "i.indisvalid, ");
1332 else
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",
1339 oid);
1341 result = PSQLexec(buf.data, false);
1342 if (!result)
1343 goto error_return;
1344 else if (PQntuples(result) != 1)
1346 PQclear(result);
1347 goto error_return;
1349 else
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, "));
1363 else
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);
1385 PQclear(result);
1387 else if (view_def)
1389 PGresult *result = NULL;
1391 /* Footer information about a view */
1392 printTableAddFooter(&cont, _("View definition:"));
1393 printTableAddFooter(&cont, view_def);
1395 /* print rules */
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",
1402 oid);
1403 result = PSQLexec(buf.data, false);
1404 if (!result)
1405 goto error_return;
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);
1416 ruledef += 12;
1418 printfPQExpBuffer(&buf, " %s", ruledef);
1419 printTableAddFooter(&cont, buf.data);
1422 PQclear(result);
1425 else if (tableinfo.relkind == 'r')
1427 /* Footer information about a table */
1428 PGresult *result = NULL;
1429 int tuples = 0;
1431 /* print indexes */
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, ");
1438 else
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",
1447 oid);
1448 result = PSQLexec(buf.data, false);
1449 if (!result)
1450 goto error_return;
1451 else
1452 tuples = PQntuples(result);
1454 if (tuples > 0)
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
1469 ? " PRIMARY KEY," :
1470 (strcmp(PQgetvalue(result, i, 2), "t") == 0
1471 ? " UNIQUE,"
1472 : ""));
1473 /* Everything after "USING" is echoed verbatim */
1474 indexdef = PQgetvalue(result, i, 5);
1475 usingpos = strstr(indexdef, " USING ");
1476 if (usingpos)
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)),
1493 false);
1496 PQclear(result);
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",
1507 oid);
1508 result = PSQLexec(buf.data, false);
1509 if (!result)
1510 goto error_return;
1511 else
1512 tuples = PQntuples(result);
1514 if (tuples > 0)
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);
1527 PQclear(result);
1530 /* print foreign-key constraints (there are none if no triggers) */
1531 if (tableinfo.hastriggers)
1533 printfPQExpBuffer(&buf,
1534 "SELECT conname,\n"
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",
1538 oid);
1539 result = PSQLexec(buf.data, false);
1540 if (!result)
1541 goto error_return;
1542 else
1543 tuples = PQntuples(result);
1545 if (tuples > 0)
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);
1558 PQclear(result);
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",
1569 oid);
1570 result = PSQLexec(buf.data, false);
1571 if (!result)
1572 goto error_return;
1573 else
1574 tuples = PQntuples(result);
1576 if (tuples > 0)
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);
1589 PQclear(result);
1592 /* print rules */
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)), "
1599 "ev_enabled\n"
1600 "FROM pg_catalog.pg_rewrite r\n"
1601 "WHERE r.ev_class = '%s' ORDER BY 1",
1602 oid);
1604 else
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",
1611 oid);
1613 result = PSQLexec(buf.data, false);
1614 if (!result)
1615 goto error_return;
1616 else
1617 tuples = PQntuples(result);
1619 if (tuples > 0)
1621 bool have_heading;
1622 int category;
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;
1633 switch (category)
1635 case 0:
1636 if (*PQgetvalue(result, i, 2) == 'O')
1637 list_rule = true;
1638 break;
1639 case 1:
1640 if (*PQgetvalue(result, i, 2) == 'D')
1641 list_rule = true;
1642 break;
1643 case 2:
1644 if (*PQgetvalue(result, i, 2) == 'A')
1645 list_rule = true;
1646 break;
1647 case 3:
1648 if (*PQgetvalue(result, i, 2) == 'R')
1649 list_rule = true;
1650 break;
1652 if (!list_rule)
1653 continue;
1655 if (!have_heading)
1657 switch (category)
1659 case 0:
1660 printfPQExpBuffer(&buf, _("Rules:"));
1661 break;
1662 case 1:
1663 printfPQExpBuffer(&buf, _("Disabled rules:"));
1664 break;
1665 case 2:
1666 printfPQExpBuffer(&buf, _("Rules firing always:"));
1667 break;
1668 case 3:
1669 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
1670 break;
1672 printTableAddFooter(&cont, buf.data);
1673 have_heading = true;
1676 /* Everything after "CREATE RULE" is echoed verbatim */
1677 ruledef = PQgetvalue(result, i, 1);
1678 ruledef += 12;
1679 printfPQExpBuffer(&buf, " %s", ruledef);
1680 printTableAddFooter(&cont, buf.data);
1684 PQclear(result);
1687 /* print triggers (but ignore foreign-key triggers) */
1688 if (tableinfo.hastriggers)
1690 printfPQExpBuffer(&buf,
1691 "SELECT t.tgname, "
1692 "pg_catalog.pg_get_triggerdef(t.oid), "
1693 "t.tgenabled\n"
1694 "FROM pg_catalog.pg_trigger t\n"
1695 "WHERE t.tgrelid = '%s' AND ",
1696 oid);
1697 if (pset.sversion >= 80300)
1698 appendPQExpBuffer(&buf, "t.tgconstraint = 0");
1699 else
1700 appendPQExpBuffer(&buf,
1701 "(NOT tgisconstraint "
1702 " OR NOT EXISTS"
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);
1709 if (!result)
1710 goto error_return;
1711 else
1712 tuples = PQntuples(result);
1714 if (tuples > 0)
1716 bool have_heading;
1717 int category;
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++)
1729 bool list_trigger;
1730 const char *tgdef;
1731 const char *usingpos;
1732 const char *tgenabled;
1735 * Check if this trigger falls into the current
1736 * category
1738 tgenabled = PQgetvalue(result, i, 2);
1739 list_trigger = false;
1740 switch (category)
1742 case 0:
1743 if (*tgenabled == 'O' || *tgenabled == 't')
1744 list_trigger = true;
1745 break;
1746 case 1:
1747 if (*tgenabled == 'D' || *tgenabled == 'f')
1748 list_trigger = true;
1749 break;
1750 case 2:
1751 if (*tgenabled == 'A')
1752 list_trigger = true;
1753 break;
1754 case 3:
1755 if (*tgenabled == 'R')
1756 list_trigger = true;
1757 break;
1759 if (list_trigger == false)
1760 continue;
1762 /* Print the category heading once */
1763 if (have_heading == false)
1765 switch (category)
1767 case 0:
1768 printfPQExpBuffer(&buf, _("Triggers:"));
1769 break;
1770 case 1:
1771 printfPQExpBuffer(&buf, _("Disabled triggers:"));
1772 break;
1773 case 2:
1774 printfPQExpBuffer(&buf, _("Triggers firing always:"));
1775 break;
1776 case 3:
1777 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
1778 break;
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 ");
1788 if (usingpos)
1789 tgdef = usingpos + 9;
1791 printfPQExpBuffer(&buf, " %s", tgdef);
1792 printTableAddFooter(&cont, buf.data);
1796 PQclear(result);
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);
1803 if (!result)
1804 goto error_return;
1805 else
1806 tuples = PQntuples(result);
1808 for (i = 0; i < tuples; i++)
1810 const char *s = _("Inherits");
1812 if (i == 0)
1813 printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
1814 else
1815 printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
1816 if (i < tuples - 1)
1817 appendPQExpBuffer(&buf, ",");
1819 printTableAddFooter(&cont, buf.data);
1821 PQclear(result);
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);
1826 else
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);
1830 if (!result)
1831 goto error_return;
1832 else
1833 tuples = PQntuples(result);
1835 if (!verbose)
1837 /* print the number of child tables, if any */
1838 if (tuples > 0)
1840 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
1841 printTableAddFooter(&cont, buf.data);
1844 else
1846 /* display the list of child tables */
1847 const char *ct = _("Child tables");
1849 for (i = 0; i < tuples; i++)
1851 if (i == 0)
1852 printfPQExpBuffer(&buf, "%s: %s",
1853 ct, PQgetvalue(result, i, 0));
1854 else
1855 printfPQExpBuffer(&buf, "%*s %s",
1856 (int) strlen(ct), "",
1857 PQgetvalue(result, i, 0));
1858 if (i < tuples - 1)
1859 appendPQExpBuffer(&buf, ",");
1861 printTableAddFooter(&cont, buf.data);
1864 PQclear(result);
1866 /* OIDs and options */
1867 if (verbose)
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,
1890 true);
1893 printTable(&cont, pset.queryFout, pset.logfile);
1894 printTableCleanup(&cont);
1896 retval = true;
1898 error_return:
1900 /* clean up */
1901 if (printTableInitialized)
1902 printTableCleanup(&cont);
1903 termPQExpBuffer(&buf);
1904 termPQExpBuffer(&title);
1905 termPQExpBuffer(&tmpbuf);
1907 if (seq_values)
1909 for (ptr = seq_values; *ptr; ptr++)
1910 free(*ptr);
1911 free(seq_values);
1914 if (modifiers)
1916 for (ptr = modifiers; *ptr; ptr++)
1917 free(*ptr);
1918 free(modifiers);
1921 if (view_def)
1922 free(view_def);
1924 if (res)
1925 PQclear(res);
1927 return retval;
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
1933 * footer.
1935 static void
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);
1957 if (!result)
1958 return;
1959 /* Should always be the case, but.... */
1960 if (PQntuples(result) > 0)
1962 if (newline)
1964 /* Add the tablespace as a new footer */
1965 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
1966 PQgetvalue(result, 0, 0));
1967 printTableAddFooter(cont, buf.data);
1969 else
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);
1983 PQclear(result);
1984 termPQExpBuffer(&buf);
1990 * \du or \dg
1992 * Describes roles. Any schema portion of the pattern is ignored.
1994 bool
1995 describeRoles(const char *pattern, bool verbose)
1997 PQExpBufferData buf;
1998 PGresult *res;
1999 printTableContent cont;
2000 printTableOpt myopt = pset.popt.topt;
2001 int ncols = 3;
2002 int nrows = 0;
2003 int i;
2004 int conns;
2005 const char align = 'l';
2006 char **attr;
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");
2024 ncols++;
2027 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2029 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2030 NULL, "r.rolname", NULL, NULL);
2032 else
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);
2050 if (!res)
2051 return 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));
2086 if (conns >= 0)
2088 if (buf.len > 0)
2089 appendPQExpBufferStr(&buf, "\n");
2091 if (conns == 0)
2092 appendPQExpBuffer(&buf, _("No connections"));
2093 else
2094 appendPQExpBuffer(&buf, ngettext("%d connection",
2095 "%d connections",
2096 conns),
2097 conns);
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++)
2115 free(attr[i]);
2116 free(attr);
2118 PQclear(res);
2119 return true;
2122 static void
2123 add_role_attribute(PQExpBuffer buf, const char *const str)
2125 if (buf->len > 0)
2126 appendPQExpBufferStr(buf, "\n");
2128 appendPQExpBufferStr(buf, str);
2133 * listTables()
2135 * handler for \dt, \di, etc.
2137 * tabtypes is an array of characters, specifying what info is desired:
2138 * t - tables
2139 * i - indexes
2140 * v - views
2141 * s - sequences
2142 * (any order of the above is fine)
2143 * If tabtypes is empty, we default to \dtvs.
2145 bool
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;
2154 PGresult *res;
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"));
2182 if (showIndexes)
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"));
2191 if (verbose)
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");
2199 if (showIndexes)
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 (");
2205 if (showTables)
2206 appendPQExpBuffer(&buf, "'r',");
2207 if (showViews)
2208 appendPQExpBuffer(&buf, "'v',");
2209 if (showIndexes)
2210 appendPQExpBuffer(&buf, "'i',");
2211 if (showSeq)
2212 appendPQExpBuffer(&buf, "'S',");
2213 if (showSystem || pattern)
2214 appendPQExpBuffer(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2215 * 8.1 */
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);
2239 if (!res)
2240 return false;
2242 if (PQntuples(res) == 0 && !pset.quiet)
2244 if (pattern)
2245 fprintf(pset.queryFout, _("No matching relations found.\n"));
2246 else
2247 fprintf(pset.queryFout, _("No relations found.\n"));
2249 else
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);
2259 PQclear(res);
2260 return true;
2265 * \dD
2267 * Describes domains.
2269 bool
2270 listDomains(const char *pattern, bool showSystem)
2272 PQExpBufferData buf;
2273 PGresult *res;
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"
2285 " ELSE ''\n"
2286 " END as \"%s\",\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);
2311 if (!res)
2312 return false;
2314 myopt.nullPrint = NULL;
2315 myopt.title = _("List of domains");
2316 myopt.translate_header = true;
2318 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2320 PQclear(res);
2321 return true;
2325 * \dc
2327 * Describes conversions.
2329 bool
2330 listConversions(const char *pattern, bool showSystem)
2332 PQExpBufferData buf;
2333 PGresult *res;
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);
2367 if (!res)
2368 return false;
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);
2377 PQclear(res);
2378 return true;
2382 * \dC
2384 * Describes casts.
2386 bool
2387 listCasts(const char *pattern)
2389 PQExpBufferData buf;
2390 PGresult *res;
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"
2406 " ELSE p.proname\n"
2407 " END as \"%s\",\n"
2408 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2409 " WHEN c.castcontext = 'a' THEN '%s'\n"
2410 " ELSE '%s'\n"
2411 " END as \"%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"
2422 "WHERE (true",
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);
2449 if (!res)
2450 return false;
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);
2459 PQclear(res);
2460 return true;
2464 * \dn
2466 * Describes schemas (namespaces)
2468 bool
2469 listSchemas(const char *pattern, bool verbose)
2471 PQExpBufferData buf;
2472 PGresult *res;
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"));
2482 if (verbose)
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,
2498 NULL);
2500 appendPQExpBuffer(&buf, "ORDER BY 1;");
2502 res = PSQLexec(buf.data, false);
2503 termPQExpBuffer(&buf);
2504 if (!res)
2505 return false;
2507 myopt.nullPrint = NULL;
2508 myopt.title = _("List of schemas");
2509 myopt.translate_header = true;
2511 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2513 PQclear(res);
2514 return true;
2519 * \dFp
2520 * list text search parsers
2522 bool
2523 listTSParsers(const char *pattern, bool verbose)
2525 PQExpBufferData buf;
2526 PGresult *res;
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);
2533 return true;
2536 if (verbose)
2537 return listTSParsersVerbose(pattern);
2539 initPQExpBuffer(&buf);
2541 printfPQExpBuffer(&buf,
2542 "SELECT \n"
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);
2561 if (!res)
2562 return false;
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);
2570 PQclear(res);
2571 return true;
2575 * full description of parsers
2577 static bool
2578 listTSParsersVerbose(const char *pattern)
2580 PQExpBufferData buf;
2581 PGresult *res;
2582 int i;
2584 initPQExpBuffer(&buf);
2586 printfPQExpBuffer(&buf,
2587 "SELECT p.oid, \n"
2588 " n.nspname, \n"
2589 " p.prsname \n"
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);
2602 if (!res)
2603 return false;
2605 if (PQntuples(res) == 0)
2607 if (!pset.quiet)
2608 fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
2609 pattern);
2610 PQclear(res);
2611 return false;
2614 for (i = 0; i < PQntuples(res); i++)
2616 const char *oid;
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))
2627 PQclear(res);
2628 return false;
2631 if (cancel_pressed)
2633 PQclear(res);
2634 return false;
2638 PQclear(res);
2639 return true;
2642 static bool
2643 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
2645 PQExpBufferData buf;
2646 PGresult *res;
2647 char title[1024];
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"
2659 "UNION ALL \n"
2660 "SELECT '%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"
2665 "UNION ALL \n"
2666 "SELECT '%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"
2671 "UNION ALL \n"
2672 "SELECT '%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"
2677 "UNION ALL \n"
2678 "SELECT '%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"),
2687 oid,
2688 gettext_noop("Get next token"),
2689 oid,
2690 gettext_noop("End parse"),
2691 oid,
2692 gettext_noop("Get headline"),
2693 oid,
2694 gettext_noop("Get token types"),
2695 oid);
2697 res = PSQLexec(buf.data, false);
2698 termPQExpBuffer(&buf);
2699 if (!res)
2700 return false;
2702 myopt.nullPrint = NULL;
2703 if (nspname)
2704 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
2705 else
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);
2715 PQclear(res);
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"
2723 "ORDER BY 1;",
2724 gettext_noop("Token name"),
2725 gettext_noop("Description"),
2726 oid);
2728 res = PSQLexec(buf.data, false);
2729 termPQExpBuffer(&buf);
2730 if (!res)
2731 return false;
2733 myopt.nullPrint = NULL;
2734 if (nspname)
2735 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
2736 else
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);
2746 PQclear(res);
2747 return true;
2752 * \dFd
2753 * list text search dictionaries
2755 bool
2756 listTSDictionaries(const char *pattern, bool verbose)
2758 PQExpBufferData buf;
2759 PGresult *res;
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);
2766 return true;
2769 initPQExpBuffer(&buf);
2771 printfPQExpBuffer(&buf,
2772 "SELECT \n"
2773 " n.nspname as \"%s\",\n"
2774 " d.dictname as \"%s\",\n",
2775 gettext_noop("Schema"),
2776 gettext_noop("Name"));
2778 if (verbose)
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);
2805 if (!res)
2806 return false;
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);
2814 PQclear(res);
2815 return true;
2820 * \dFt
2821 * list text search templates
2823 bool
2824 listTSTemplates(const char *pattern, bool verbose)
2826 PQExpBufferData buf;
2827 PGresult *res;
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);
2834 return true;
2837 initPQExpBuffer(&buf);
2839 if (verbose)
2840 printfPQExpBuffer(&buf,
2841 "SELECT \n"
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"));
2852 else
2853 printfPQExpBuffer(&buf,
2854 "SELECT \n"
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);
2873 if (!res)
2874 return false;
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);
2882 PQclear(res);
2883 return true;
2888 * \dF
2889 * list text search configurations
2891 bool
2892 listTSConfigs(const char *pattern, bool verbose)
2894 PQExpBufferData buf;
2895 PGresult *res;
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);
2902 return true;
2905 if (verbose)
2906 return listTSConfigsVerbose(pattern);
2908 initPQExpBuffer(&buf);
2910 printfPQExpBuffer(&buf,
2911 "SELECT \n"
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);
2930 if (!res)
2931 return false;
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);
2939 PQclear(res);
2940 return true;
2943 static bool
2944 listTSConfigsVerbose(const char *pattern)
2946 PQExpBufferData buf;
2947 PGresult *res;
2948 int i;
2950 initPQExpBuffer(&buf);
2952 printfPQExpBuffer(&buf,
2953 "SELECT c.oid, c.cfgname,\n"
2954 " n.nspname, \n"
2955 " p.prsname, \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);
2972 if (!res)
2973 return false;
2975 if (PQntuples(res) == 0)
2977 if (!pset.quiet)
2978 fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
2979 pattern);
2980 PQclear(res);
2981 return false;
2984 for (i = 0; i < PQntuples(res); i++)
2986 const char *oid;
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))
3002 PQclear(res);
3003 return false;
3006 if (cancel_pressed)
3008 PQclear(res);
3009 return false;
3013 PQclear(res);
3014 return true;
3017 static bool
3018 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3019 const char *pnspname, const char *prsname)
3021 PQExpBufferData buf,
3022 title;
3023 PGresult *res;
3024 printQueryOpt myopt = pset.popt;
3026 initPQExpBuffer(&buf);
3028 printfPQExpBuffer(&buf,
3029 "SELECT \n"
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"
3043 "ORDER BY 1",
3044 gettext_noop("Token"),
3045 gettext_noop("Dictionaries"),
3046 oid);
3048 res = PSQLexec(buf.data, false);
3049 termPQExpBuffer(&buf);
3050 if (!res)
3051 return false;
3053 initPQExpBuffer(&title);
3055 if (nspname)
3056 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3057 nspname, cfgname);
3058 else
3059 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3060 cfgname);
3062 if (pnspname)
3063 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3064 pnspname, prsname);
3065 else
3066 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3067 prsname);
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);
3079 PQclear(res);
3080 return true;
3085 * \dew
3087 * Describes foreign-data wrappers
3089 bool
3090 listForeignDataWrappers(const char *pattern, bool verbose)
3092 PQExpBufferData buf;
3093 PGresult *res;
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);
3100 return true;
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"));
3112 if (verbose)
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);
3130 if (!res)
3131 return false;
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);
3139 PQclear(res);
3140 return true;
3144 * \des
3146 * Describes foreign servers.
3148 bool
3149 listForeignServers(const char *pattern, bool verbose)
3151 PQExpBufferData buf;
3152 PGresult *res;
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);
3159 return true;
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"));
3171 if (verbose)
3173 appendPQExpBuffer(&buf, ",\n ");
3174 printACLColumn(&buf, "s.srvacl");
3175 appendPQExpBuffer(&buf,
3176 ",\n"
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);
3196 if (!res)
3197 return false;
3199 myopt.nullPrint = NULL;
3200 myopt.title = _("List of foreign servers");
3201 myopt.translate_header = true;
3203 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3205 PQclear(res);
3206 return true;
3210 * \deu
3212 * Describes user mappings.
3214 bool
3215 listUserMappings(const char *pattern, bool verbose)
3217 PQExpBufferData buf;
3218 PGresult *res;
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);
3225 return true;
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"));
3235 if (verbose)
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);
3249 if (!res)
3250 return false;
3252 myopt.nullPrint = NULL;
3253 myopt.title = _("List of user mappings");
3254 myopt.translate_header = true;
3256 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3258 PQclear(res);
3259 return true;
3263 * printACLColumn
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.
3269 static void
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"));
3276 else
3277 appendPQExpBuffer(buf,
3278 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
3279 colname, gettext_noop("Access privileges"));