Code review for patch to show definition of index columns in \d on index.
[PostgreSQL.git] / src / bin / psql / describe.c
blob3f1b402418b255f5c32048cd5b1c3ce1f366476e
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 printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '%s' ORDER BY c.oid::pg_catalog.regclass;", oid);
1826 result = PSQLexec(buf.data, false);
1827 if (!result)
1828 goto error_return;
1829 else
1830 tuples = PQntuples(result);
1832 if (!verbose)
1834 /* print the number of child tables, if any */
1835 if (tuples > 0)
1837 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
1838 printTableAddFooter(&cont, buf.data);
1841 else
1843 /* display the list of child tables */
1844 const char *ct = _("Child tables");
1846 for (i = 0; i < tuples; i++)
1848 if (i == 0)
1849 printfPQExpBuffer(&buf, "%s: %s",
1850 ct, PQgetvalue(result, i, 0));
1851 else
1852 printfPQExpBuffer(&buf, "%*s %s",
1853 (int) strlen(ct), "",
1854 PQgetvalue(result, i, 0));
1855 if (i < tuples - 1)
1856 appendPQExpBuffer(&buf, ",");
1858 printTableAddFooter(&cont, buf.data);
1861 PQclear(result);
1863 /* OIDs and options */
1864 if (verbose)
1866 const char *s = _("Has OIDs");
1868 printfPQExpBuffer(&buf, "%s: %s", s,
1869 (tableinfo.hasoids ? _("yes") : _("no")));
1870 printTableAddFooter(&cont, buf.data);
1872 /* print reloptions */
1873 if (pset.sversion >= 80200)
1875 if (tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
1877 const char *t = _("Options");
1879 printfPQExpBuffer(&buf, "%s: %s", t,
1880 tableinfo.reloptions);
1881 printTableAddFooter(&cont, buf.data);
1886 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
1887 true);
1890 printTable(&cont, pset.queryFout, pset.logfile);
1891 printTableCleanup(&cont);
1893 retval = true;
1895 error_return:
1897 /* clean up */
1898 if (printTableInitialized)
1899 printTableCleanup(&cont);
1900 termPQExpBuffer(&buf);
1901 termPQExpBuffer(&title);
1902 termPQExpBuffer(&tmpbuf);
1904 if (seq_values)
1906 for (ptr = seq_values; *ptr; ptr++)
1907 free(*ptr);
1908 free(seq_values);
1911 if (modifiers)
1913 for (ptr = modifiers; *ptr; ptr++)
1914 free(*ptr);
1915 free(modifiers);
1918 if (view_def)
1919 free(view_def);
1921 if (res)
1922 PQclear(res);
1924 return retval;
1928 * Add a tablespace description to a footer. If 'newline' is true, it is added
1929 * in a new line; otherwise it's appended to the current value of the last
1930 * footer.
1932 static void
1933 add_tablespace_footer(printTableContent *const cont, char relkind,
1934 Oid tablespace, const bool newline)
1936 /* relkinds for which we support tablespaces */
1937 if (relkind == 'r' || relkind == 'i')
1940 * We ignore the database default tablespace so that users not using
1941 * tablespaces don't need to know about them. This case also covers
1942 * pre-8.0 servers, for which tablespace will always be 0.
1944 if (tablespace != 0)
1946 PGresult *result = NULL;
1947 PQExpBufferData buf;
1949 initPQExpBuffer(&buf);
1950 printfPQExpBuffer(&buf,
1951 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
1952 "WHERE oid = '%u'", tablespace);
1953 result = PSQLexec(buf.data, false);
1954 if (!result)
1955 return;
1956 /* Should always be the case, but.... */
1957 if (PQntuples(result) > 0)
1959 if (newline)
1961 /* Add the tablespace as a new footer */
1962 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
1963 PQgetvalue(result, 0, 0));
1964 printTableAddFooter(cont, buf.data);
1966 else
1968 /* Append the tablespace to the latest footer */
1969 printfPQExpBuffer(&buf, "%s", cont->footer->data);
1972 * translator: before this string there's an index
1973 * description like '"foo_pkey" PRIMARY KEY, btree (a)'
1975 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
1976 PQgetvalue(result, 0, 0));
1977 printTableSetFooter(cont, buf.data);
1980 PQclear(result);
1981 termPQExpBuffer(&buf);
1987 * \du or \dg
1989 * Describes roles. Any schema portion of the pattern is ignored.
1991 bool
1992 describeRoles(const char *pattern, bool verbose)
1994 PQExpBufferData buf;
1995 PGresult *res;
1996 printTableContent cont;
1997 printTableOpt myopt = pset.popt.topt;
1998 int ncols = 3;
1999 int nrows = 0;
2000 int i;
2001 int conns;
2002 const char align = 'l';
2003 char **attr;
2005 initPQExpBuffer(&buf);
2007 if (pset.sversion >= 80100)
2009 printfPQExpBuffer(&buf,
2010 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
2011 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
2012 " r.rolconnlimit,\n"
2013 " ARRAY(SELECT b.rolname\n"
2014 " FROM pg_catalog.pg_auth_members m\n"
2015 " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
2016 " WHERE m.member = r.oid) as memberof");
2018 if (verbose && pset.sversion >= 80200)
2020 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
2021 ncols++;
2024 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
2026 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2027 NULL, "r.rolname", NULL, NULL);
2029 else
2031 printfPQExpBuffer(&buf,
2032 "SELECT u.usename AS rolname,\n"
2033 " u.usesuper AS rolsuper,\n"
2034 " true AS rolinherit, false AS rolcreaterole,\n"
2035 " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
2036 " -1 AS rolconnlimit,\n"
2037 " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
2038 "\nFROM pg_catalog.pg_user u\n");
2040 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2041 NULL, "u.usename", NULL, NULL);
2044 appendPQExpBuffer(&buf, "ORDER BY 1;");
2046 res = PSQLexec(buf.data, false);
2047 if (!res)
2048 return false;
2050 nrows = PQntuples(res);
2051 attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
2053 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
2055 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
2056 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
2057 printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
2059 if (verbose && pset.sversion >= 80200)
2060 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
2062 for (i = 0; i < nrows; i++)
2064 printTableAddCell(&cont, PQgetvalue(res, i, 0), false);
2066 resetPQExpBuffer(&buf);
2067 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
2068 add_role_attribute(&buf, _("Superuser"));
2070 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
2071 add_role_attribute(&buf, _("No inheritance"));
2073 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
2074 add_role_attribute(&buf, _("Create role"));
2076 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
2077 add_role_attribute(&buf, _("Create DB"));
2079 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
2080 add_role_attribute(&buf, _("Cannot login"));
2082 conns = atoi(PQgetvalue(res, i, 6));
2083 if (conns >= 0)
2085 if (buf.len > 0)
2086 appendPQExpBufferStr(&buf, "\n");
2088 if (conns == 0)
2089 appendPQExpBuffer(&buf, _("No connections"));
2090 else
2091 appendPQExpBuffer(&buf, ngettext("%d connection",
2092 "%d connections",
2093 conns),
2094 conns);
2097 attr[i] = pg_strdup(buf.data);
2099 printTableAddCell(&cont, attr[i], false);
2101 printTableAddCell(&cont, PQgetvalue(res, i, 7), false);
2103 if (verbose && pset.sversion >= 80200)
2104 printTableAddCell(&cont, PQgetvalue(res, i, 8), false);
2106 termPQExpBuffer(&buf);
2108 printTable(&cont, pset.queryFout, pset.logfile);
2109 printTableCleanup(&cont);
2111 for (i = 0; i < nrows; i++)
2112 free(attr[i]);
2113 free(attr);
2115 PQclear(res);
2116 return true;
2119 static void
2120 add_role_attribute(PQExpBuffer buf, const char *const str)
2122 if (buf->len > 0)
2123 appendPQExpBufferStr(buf, "\n");
2125 appendPQExpBufferStr(buf, str);
2130 * listTables()
2132 * handler for \dt, \di, etc.
2134 * tabtypes is an array of characters, specifying what info is desired:
2135 * t - tables
2136 * i - indexes
2137 * v - views
2138 * s - sequences
2139 * (any order of the above is fine)
2140 * If tabtypes is empty, we default to \dtvs.
2142 bool
2143 listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
2145 bool showTables = strchr(tabtypes, 't') != NULL;
2146 bool showIndexes = strchr(tabtypes, 'i') != NULL;
2147 bool showViews = strchr(tabtypes, 'v') != NULL;
2148 bool showSeq = strchr(tabtypes, 's') != NULL;
2150 PQExpBufferData buf;
2151 PGresult *res;
2152 printQueryOpt myopt = pset.popt;
2153 static const bool translate_columns[] = {false, false, true, false, false, false, false};
2155 if (!(showTables || showIndexes || showViews || showSeq))
2156 showTables = showViews = showSeq = true;
2158 initPQExpBuffer(&buf);
2161 * Note: as of Pg 8.2, we no longer use relkind 's', but we keep it here
2162 * for backwards compatibility.
2164 printfPQExpBuffer(&buf,
2165 "SELECT n.nspname as \"%s\",\n"
2166 " c.relname as \"%s\",\n"
2167 " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' END as \"%s\",\n"
2168 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
2169 gettext_noop("Schema"),
2170 gettext_noop("Name"),
2171 gettext_noop("table"),
2172 gettext_noop("view"),
2173 gettext_noop("index"),
2174 gettext_noop("sequence"),
2175 gettext_noop("special"),
2176 gettext_noop("Type"),
2177 gettext_noop("Owner"));
2179 if (showIndexes)
2180 appendPQExpBuffer(&buf,
2181 ",\n c2.relname as \"%s\"",
2182 gettext_noop("Table"));
2184 if (verbose && pset.sversion >= 80100)
2185 appendPQExpBuffer(&buf,
2186 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
2187 gettext_noop("Size"));
2188 if (verbose)
2189 appendPQExpBuffer(&buf,
2190 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
2191 gettext_noop("Description"));
2193 appendPQExpBuffer(&buf,
2194 "\nFROM pg_catalog.pg_class c"
2195 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
2196 if (showIndexes)
2197 appendPQExpBuffer(&buf,
2198 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
2199 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
2201 appendPQExpBuffer(&buf, "\nWHERE c.relkind IN (");
2202 if (showTables)
2203 appendPQExpBuffer(&buf, "'r',");
2204 if (showViews)
2205 appendPQExpBuffer(&buf, "'v',");
2206 if (showIndexes)
2207 appendPQExpBuffer(&buf, "'i',");
2208 if (showSeq)
2209 appendPQExpBuffer(&buf, "'S',");
2210 if (showSystem || pattern)
2211 appendPQExpBuffer(&buf, "'s',"); /* was RELKIND_SPECIAL in <=
2212 * 8.1 */
2213 appendPQExpBuffer(&buf, "''"); /* dummy */
2214 appendPQExpBuffer(&buf, ")\n");
2216 if (!showSystem && !pattern)
2217 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2218 " AND n.nspname <> 'information_schema'\n");
2221 * TOAST objects are suppressed unconditionally. Since we don't provide
2222 * any way to select relkind 't' above, we would never show toast tables
2223 * in any case; it seems a bit confusing to allow their indexes to be
2224 * shown. Use plain \d if you really need to look at a TOAST table/index.
2226 appendPQExpBuffer(&buf, " AND n.nspname !~ '^pg_toast'\n");
2228 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2229 "n.nspname", "c.relname", NULL,
2230 "pg_catalog.pg_table_is_visible(c.oid)");
2232 appendPQExpBuffer(&buf, "ORDER BY 1,2;");
2234 res = PSQLexec(buf.data, false);
2235 termPQExpBuffer(&buf);
2236 if (!res)
2237 return false;
2239 if (PQntuples(res) == 0 && !pset.quiet)
2241 if (pattern)
2242 fprintf(pset.queryFout, _("No matching relations found.\n"));
2243 else
2244 fprintf(pset.queryFout, _("No relations found.\n"));
2246 else
2248 myopt.nullPrint = NULL;
2249 myopt.title = _("List of relations");
2250 myopt.translate_header = true;
2251 myopt.translate_columns = translate_columns;
2253 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2256 PQclear(res);
2257 return true;
2262 * \dD
2264 * Describes domains.
2266 bool
2267 listDomains(const char *pattern, bool showSystem)
2269 PQExpBufferData buf;
2270 PGresult *res;
2271 printQueryOpt myopt = pset.popt;
2273 initPQExpBuffer(&buf);
2275 printfPQExpBuffer(&buf,
2276 "SELECT n.nspname as \"%s\",\n"
2277 " t.typname as \"%s\",\n"
2278 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2279 " CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
2280 " WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
2281 " WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
2282 " ELSE ''\n"
2283 " END as \"%s\",\n"
2284 " pg_catalog.array_to_string(ARRAY(\n"
2285 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
2286 " ), ' ') as \"%s\"\n"
2287 "FROM pg_catalog.pg_type t\n"
2288 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
2289 "WHERE t.typtype = 'd'\n",
2290 gettext_noop("Schema"),
2291 gettext_noop("Name"),
2292 gettext_noop("Type"),
2293 gettext_noop("Modifier"),
2294 gettext_noop("Check"));
2296 if (!showSystem && !pattern)
2297 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2298 " AND n.nspname <> 'information_schema'\n");
2300 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2301 "n.nspname", "t.typname", NULL,
2302 "pg_catalog.pg_type_is_visible(t.oid)");
2304 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2306 res = PSQLexec(buf.data, false);
2307 termPQExpBuffer(&buf);
2308 if (!res)
2309 return false;
2311 myopt.nullPrint = NULL;
2312 myopt.title = _("List of domains");
2313 myopt.translate_header = true;
2315 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2317 PQclear(res);
2318 return true;
2322 * \dc
2324 * Describes conversions.
2326 bool
2327 listConversions(const char *pattern, bool showSystem)
2329 PQExpBufferData buf;
2330 PGresult *res;
2331 printQueryOpt myopt = pset.popt;
2332 static const bool translate_columns[] = {false, false, false, false, true};
2334 initPQExpBuffer(&buf);
2336 printfPQExpBuffer(&buf,
2337 "SELECT n.nspname AS \"%s\",\n"
2338 " c.conname AS \"%s\",\n"
2339 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
2340 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
2341 " CASE WHEN c.condefault THEN '%s'\n"
2342 " ELSE '%s' END AS \"%s\"\n"
2343 "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
2344 "WHERE n.oid = c.connamespace\n",
2345 gettext_noop("Schema"),
2346 gettext_noop("Name"),
2347 gettext_noop("Source"),
2348 gettext_noop("Destination"),
2349 gettext_noop("yes"), gettext_noop("no"),
2350 gettext_noop("Default?"));
2352 if (!showSystem && !pattern)
2353 appendPQExpBuffer(&buf, " AND n.nspname <> 'pg_catalog'\n"
2354 " AND n.nspname <> 'information_schema'\n");
2356 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2357 "n.nspname", "c.conname", NULL,
2358 "pg_catalog.pg_conversion_is_visible(c.oid)");
2360 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2362 res = PSQLexec(buf.data, false);
2363 termPQExpBuffer(&buf);
2364 if (!res)
2365 return false;
2367 myopt.nullPrint = NULL;
2368 myopt.title = _("List of conversions");
2369 myopt.translate_header = true;
2370 myopt.translate_columns = translate_columns;
2372 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2374 PQclear(res);
2375 return true;
2379 * \dC
2381 * Describes casts.
2383 bool
2384 listCasts(const char *pattern)
2386 PQExpBufferData buf;
2387 PGresult *res;
2388 printQueryOpt myopt = pset.popt;
2389 static const bool translate_columns[] = {false, false, false, true};
2391 initPQExpBuffer(&buf);
2394 * We need a left join to pg_proc for binary casts; the others are just
2395 * paranoia. Also note that we don't attempt to localize '(binary
2396 * coercible)', because there's too much risk of gettext translating a
2397 * function name that happens to match some string in the PO database.
2399 printfPQExpBuffer(&buf,
2400 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
2401 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
2402 " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
2403 " ELSE p.proname\n"
2404 " END as \"%s\",\n"
2405 " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
2406 " WHEN c.castcontext = 'a' THEN '%s'\n"
2407 " ELSE '%s'\n"
2408 " END as \"%s\"\n"
2409 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
2410 " ON c.castfunc = p.oid\n"
2411 " LEFT JOIN pg_catalog.pg_type ts\n"
2412 " ON c.castsource = ts.oid\n"
2413 " LEFT JOIN pg_catalog.pg_namespace ns\n"
2414 " ON ns.oid = ts.typnamespace\n"
2415 " LEFT JOIN pg_catalog.pg_type tt\n"
2416 " ON c.casttarget = tt.oid\n"
2417 " LEFT JOIN pg_catalog.pg_namespace nt\n"
2418 " ON nt.oid = tt.typnamespace\n"
2419 "WHERE (true",
2420 gettext_noop("Source type"),
2421 gettext_noop("Target type"),
2422 gettext_noop("Function"),
2423 gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
2424 gettext_noop("Implicit?"));
2427 * Match name pattern against either internal or external name of either
2428 * castsource or casttarget
2430 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2431 "ns.nspname", "ts.typname",
2432 "pg_catalog.format_type(ts.oid, NULL)",
2433 "pg_catalog.pg_type_is_visible(ts.oid)");
2435 appendPQExpBuffer(&buf, ") OR (true");
2437 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2438 "nt.nspname", "tt.typname",
2439 "pg_catalog.format_type(tt.oid, NULL)",
2440 "pg_catalog.pg_type_is_visible(tt.oid)");
2442 appendPQExpBuffer(&buf, ")\nORDER BY 1, 2;");
2444 res = PSQLexec(buf.data, false);
2445 termPQExpBuffer(&buf);
2446 if (!res)
2447 return false;
2449 myopt.nullPrint = NULL;
2450 myopt.title = _("List of casts");
2451 myopt.translate_header = true;
2452 myopt.translate_columns = translate_columns;
2454 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2456 PQclear(res);
2457 return true;
2461 * \dn
2463 * Describes schemas (namespaces)
2465 bool
2466 listSchemas(const char *pattern, bool verbose)
2468 PQExpBufferData buf;
2469 PGresult *res;
2470 printQueryOpt myopt = pset.popt;
2472 initPQExpBuffer(&buf);
2473 printfPQExpBuffer(&buf,
2474 "SELECT n.nspname AS \"%s\",\n"
2475 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
2476 gettext_noop("Name"),
2477 gettext_noop("Owner"));
2479 if (verbose)
2481 appendPQExpBuffer(&buf, ",\n ");
2482 printACLColumn(&buf, "n.nspacl");
2483 appendPQExpBuffer(&buf,
2484 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
2485 gettext_noop("Description"));
2488 appendPQExpBuffer(&buf,
2489 "\nFROM pg_catalog.pg_namespace n\n"
2490 "WHERE (n.nspname !~ '^pg_temp_' OR\n"
2491 " n.nspname = (pg_catalog.current_schemas(true))[1])\n"); /* temp schema is first */
2493 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2494 NULL, "n.nspname", NULL,
2495 NULL);
2497 appendPQExpBuffer(&buf, "ORDER BY 1;");
2499 res = PSQLexec(buf.data, false);
2500 termPQExpBuffer(&buf);
2501 if (!res)
2502 return false;
2504 myopt.nullPrint = NULL;
2505 myopt.title = _("List of schemas");
2506 myopt.translate_header = true;
2508 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2510 PQclear(res);
2511 return true;
2516 * \dFp
2517 * list text search parsers
2519 bool
2520 listTSParsers(const char *pattern, bool verbose)
2522 PQExpBufferData buf;
2523 PGresult *res;
2524 printQueryOpt myopt = pset.popt;
2526 if (pset.sversion < 80300)
2528 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2529 pset.sversion / 10000, (pset.sversion / 100) % 100);
2530 return true;
2533 if (verbose)
2534 return listTSParsersVerbose(pattern);
2536 initPQExpBuffer(&buf);
2538 printfPQExpBuffer(&buf,
2539 "SELECT \n"
2540 " n.nspname as \"%s\",\n"
2541 " p.prsname as \"%s\",\n"
2542 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
2543 "FROM pg_catalog.pg_ts_parser p \n"
2544 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
2545 gettext_noop("Schema"),
2546 gettext_noop("Name"),
2547 gettext_noop("Description")
2550 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2551 "n.nspname", "p.prsname", NULL,
2552 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2554 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2556 res = PSQLexec(buf.data, false);
2557 termPQExpBuffer(&buf);
2558 if (!res)
2559 return false;
2561 myopt.nullPrint = NULL;
2562 myopt.title = _("List of text search parsers");
2563 myopt.translate_header = true;
2565 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2567 PQclear(res);
2568 return true;
2572 * full description of parsers
2574 static bool
2575 listTSParsersVerbose(const char *pattern)
2577 PQExpBufferData buf;
2578 PGresult *res;
2579 int i;
2581 initPQExpBuffer(&buf);
2583 printfPQExpBuffer(&buf,
2584 "SELECT p.oid, \n"
2585 " n.nspname, \n"
2586 " p.prsname \n"
2587 "FROM pg_catalog.pg_ts_parser p\n"
2588 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
2591 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2592 "n.nspname", "p.prsname", NULL,
2593 "pg_catalog.pg_ts_parser_is_visible(p.oid)");
2595 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2597 res = PSQLexec(buf.data, false);
2598 termPQExpBuffer(&buf);
2599 if (!res)
2600 return false;
2602 if (PQntuples(res) == 0)
2604 if (!pset.quiet)
2605 fprintf(stderr, _("Did not find any text search parser named \"%s\".\n"),
2606 pattern);
2607 PQclear(res);
2608 return false;
2611 for (i = 0; i < PQntuples(res); i++)
2613 const char *oid;
2614 const char *nspname = NULL;
2615 const char *prsname;
2617 oid = PQgetvalue(res, i, 0);
2618 if (!PQgetisnull(res, i, 1))
2619 nspname = PQgetvalue(res, i, 1);
2620 prsname = PQgetvalue(res, i, 2);
2622 if (!describeOneTSParser(oid, nspname, prsname))
2624 PQclear(res);
2625 return false;
2628 if (cancel_pressed)
2630 PQclear(res);
2631 return false;
2635 PQclear(res);
2636 return true;
2639 static bool
2640 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
2642 PQExpBufferData buf;
2643 PGresult *res;
2644 char title[1024];
2645 printQueryOpt myopt = pset.popt;
2646 static const bool translate_columns[] = {true, false, false};
2648 initPQExpBuffer(&buf);
2650 printfPQExpBuffer(&buf,
2651 "SELECT '%s' AS \"%s\", \n"
2652 " p.prsstart::pg_catalog.regproc AS \"%s\", \n"
2653 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\" \n"
2654 " FROM pg_catalog.pg_ts_parser p \n"
2655 " WHERE p.oid = '%s' \n"
2656 "UNION ALL \n"
2657 "SELECT '%s', \n"
2658 " p.prstoken::pg_catalog.regproc, \n"
2659 " pg_catalog.obj_description(p.prstoken, 'pg_proc') \n"
2660 " FROM pg_catalog.pg_ts_parser p \n"
2661 " WHERE p.oid = '%s' \n"
2662 "UNION ALL \n"
2663 "SELECT '%s', \n"
2664 " p.prsend::pg_catalog.regproc, \n"
2665 " pg_catalog.obj_description(p.prsend, 'pg_proc') \n"
2666 " FROM pg_catalog.pg_ts_parser p \n"
2667 " WHERE p.oid = '%s' \n"
2668 "UNION ALL \n"
2669 "SELECT '%s', \n"
2670 " p.prsheadline::pg_catalog.regproc, \n"
2671 " pg_catalog.obj_description(p.prsheadline, 'pg_proc') \n"
2672 " FROM pg_catalog.pg_ts_parser p \n"
2673 " WHERE p.oid = '%s' \n"
2674 "UNION ALL \n"
2675 "SELECT '%s', \n"
2676 " p.prslextype::pg_catalog.regproc, \n"
2677 " pg_catalog.obj_description(p.prslextype, 'pg_proc') \n"
2678 " FROM pg_catalog.pg_ts_parser p \n"
2679 " WHERE p.oid = '%s' \n",
2680 gettext_noop("Start parse"),
2681 gettext_noop("Method"),
2682 gettext_noop("Function"),
2683 gettext_noop("Description"),
2684 oid,
2685 gettext_noop("Get next token"),
2686 oid,
2687 gettext_noop("End parse"),
2688 oid,
2689 gettext_noop("Get headline"),
2690 oid,
2691 gettext_noop("Get token types"),
2692 oid);
2694 res = PSQLexec(buf.data, false);
2695 termPQExpBuffer(&buf);
2696 if (!res)
2697 return false;
2699 myopt.nullPrint = NULL;
2700 if (nspname)
2701 sprintf(title, _("Text search parser \"%s.%s\""), nspname, prsname);
2702 else
2703 sprintf(title, _("Text search parser \"%s\""), prsname);
2704 myopt.title = title;
2705 myopt.footers = NULL;
2706 myopt.default_footer = false;
2707 myopt.translate_header = true;
2708 myopt.translate_columns = translate_columns;
2710 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2712 PQclear(res);
2714 initPQExpBuffer(&buf);
2716 printfPQExpBuffer(&buf,
2717 "SELECT t.alias as \"%s\", \n"
2718 " t.description as \"%s\" \n"
2719 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t \n"
2720 "ORDER BY 1;",
2721 gettext_noop("Token name"),
2722 gettext_noop("Description"),
2723 oid);
2725 res = PSQLexec(buf.data, false);
2726 termPQExpBuffer(&buf);
2727 if (!res)
2728 return false;
2730 myopt.nullPrint = NULL;
2731 if (nspname)
2732 sprintf(title, _("Token types for parser \"%s.%s\""), nspname, prsname);
2733 else
2734 sprintf(title, _("Token types for parser \"%s\""), prsname);
2735 myopt.title = title;
2736 myopt.footers = NULL;
2737 myopt.default_footer = true;
2738 myopt.translate_header = true;
2739 myopt.translate_columns = NULL;
2741 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2743 PQclear(res);
2744 return true;
2749 * \dFd
2750 * list text search dictionaries
2752 bool
2753 listTSDictionaries(const char *pattern, bool verbose)
2755 PQExpBufferData buf;
2756 PGresult *res;
2757 printQueryOpt myopt = pset.popt;
2759 if (pset.sversion < 80300)
2761 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2762 pset.sversion / 10000, (pset.sversion / 100) % 100);
2763 return true;
2766 initPQExpBuffer(&buf);
2768 printfPQExpBuffer(&buf,
2769 "SELECT \n"
2770 " n.nspname as \"%s\",\n"
2771 " d.dictname as \"%s\",\n",
2772 gettext_noop("Schema"),
2773 gettext_noop("Name"));
2775 if (verbose)
2777 appendPQExpBuffer(&buf,
2778 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM \n"
2779 " pg_catalog.pg_ts_template t \n"
2780 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace \n"
2781 " WHERE d.dicttemplate = t.oid ) AS \"%s\", \n"
2782 " d.dictinitoption as \"%s\", \n",
2783 gettext_noop("Template"),
2784 gettext_noop("Init options"));
2787 appendPQExpBuffer(&buf,
2788 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
2789 gettext_noop("Description"));
2791 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_dict d\n"
2792 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
2794 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2795 "n.nspname", "d.dictname", NULL,
2796 "pg_catalog.pg_ts_dict_is_visible(d.oid)");
2798 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2800 res = PSQLexec(buf.data, false);
2801 termPQExpBuffer(&buf);
2802 if (!res)
2803 return false;
2805 myopt.nullPrint = NULL;
2806 myopt.title = _("List of text search dictionaries");
2807 myopt.translate_header = true;
2809 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2811 PQclear(res);
2812 return true;
2817 * \dFt
2818 * list text search templates
2820 bool
2821 listTSTemplates(const char *pattern, bool verbose)
2823 PQExpBufferData buf;
2824 PGresult *res;
2825 printQueryOpt myopt = pset.popt;
2827 if (pset.sversion < 80300)
2829 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2830 pset.sversion / 10000, (pset.sversion / 100) % 100);
2831 return true;
2834 initPQExpBuffer(&buf);
2836 if (verbose)
2837 printfPQExpBuffer(&buf,
2838 "SELECT \n"
2839 " n.nspname AS \"%s\",\n"
2840 " t.tmplname AS \"%s\",\n"
2841 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
2842 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
2843 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2844 gettext_noop("Schema"),
2845 gettext_noop("Name"),
2846 gettext_noop("Init"),
2847 gettext_noop("Lexize"),
2848 gettext_noop("Description"));
2849 else
2850 printfPQExpBuffer(&buf,
2851 "SELECT \n"
2852 " n.nspname AS \"%s\",\n"
2853 " t.tmplname AS \"%s\",\n"
2854 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
2855 gettext_noop("Schema"),
2856 gettext_noop("Name"),
2857 gettext_noop("Description"));
2859 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_ts_template t\n"
2860 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
2862 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2863 "n.nspname", "t.tmplname", NULL,
2864 "pg_catalog.pg_ts_template_is_visible(t.oid)");
2866 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2868 res = PSQLexec(buf.data, false);
2869 termPQExpBuffer(&buf);
2870 if (!res)
2871 return false;
2873 myopt.nullPrint = NULL;
2874 myopt.title = _("List of text search templates");
2875 myopt.translate_header = true;
2877 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2879 PQclear(res);
2880 return true;
2885 * \dF
2886 * list text search configurations
2888 bool
2889 listTSConfigs(const char *pattern, bool verbose)
2891 PQExpBufferData buf;
2892 PGresult *res;
2893 printQueryOpt myopt = pset.popt;
2895 if (pset.sversion < 80300)
2897 fprintf(stderr, _("The server (version %d.%d) does not support full text search.\n"),
2898 pset.sversion / 10000, (pset.sversion / 100) % 100);
2899 return true;
2902 if (verbose)
2903 return listTSConfigsVerbose(pattern);
2905 initPQExpBuffer(&buf);
2907 printfPQExpBuffer(&buf,
2908 "SELECT \n"
2909 " n.nspname as \"%s\",\n"
2910 " c.cfgname as \"%s\",\n"
2911 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
2912 "FROM pg_catalog.pg_ts_config c\n"
2913 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace \n",
2914 gettext_noop("Schema"),
2915 gettext_noop("Name"),
2916 gettext_noop("Description")
2919 processSQLNamePattern(pset.db, &buf, pattern, false, false,
2920 "n.nspname", "c.cfgname", NULL,
2921 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2923 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
2925 res = PSQLexec(buf.data, false);
2926 termPQExpBuffer(&buf);
2927 if (!res)
2928 return false;
2930 myopt.nullPrint = NULL;
2931 myopt.title = _("List of text search configurations");
2932 myopt.translate_header = true;
2934 printQuery(res, &myopt, pset.queryFout, pset.logfile);
2936 PQclear(res);
2937 return true;
2940 static bool
2941 listTSConfigsVerbose(const char *pattern)
2943 PQExpBufferData buf;
2944 PGresult *res;
2945 int i;
2947 initPQExpBuffer(&buf);
2949 printfPQExpBuffer(&buf,
2950 "SELECT c.oid, c.cfgname,\n"
2951 " n.nspname, \n"
2952 " p.prsname, \n"
2953 " np.nspname as pnspname \n"
2954 "FROM pg_catalog.pg_ts_config c \n"
2955 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace, \n"
2956 " pg_catalog.pg_ts_parser p \n"
2957 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace \n"
2958 "WHERE p.oid = c.cfgparser\n"
2961 processSQLNamePattern(pset.db, &buf, pattern, true, false,
2962 "n.nspname", "c.cfgname", NULL,
2963 "pg_catalog.pg_ts_config_is_visible(c.oid)");
2965 appendPQExpBuffer(&buf, "ORDER BY 3, 2;");
2967 res = PSQLexec(buf.data, false);
2968 termPQExpBuffer(&buf);
2969 if (!res)
2970 return false;
2972 if (PQntuples(res) == 0)
2974 if (!pset.quiet)
2975 fprintf(stderr, _("Did not find any text search configuration named \"%s\".\n"),
2976 pattern);
2977 PQclear(res);
2978 return false;
2981 for (i = 0; i < PQntuples(res); i++)
2983 const char *oid;
2984 const char *cfgname;
2985 const char *nspname = NULL;
2986 const char *prsname;
2987 const char *pnspname = NULL;
2989 oid = PQgetvalue(res, i, 0);
2990 cfgname = PQgetvalue(res, i, 1);
2991 if (!PQgetisnull(res, i, 2))
2992 nspname = PQgetvalue(res, i, 2);
2993 prsname = PQgetvalue(res, i, 3);
2994 if (!PQgetisnull(res, i, 4))
2995 pnspname = PQgetvalue(res, i, 4);
2997 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
2999 PQclear(res);
3000 return false;
3003 if (cancel_pressed)
3005 PQclear(res);
3006 return false;
3010 PQclear(res);
3011 return true;
3014 static bool
3015 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
3016 const char *pnspname, const char *prsname)
3018 PQExpBufferData buf,
3019 title;
3020 PGresult *res;
3021 printQueryOpt myopt = pset.popt;
3023 initPQExpBuffer(&buf);
3025 printfPQExpBuffer(&buf,
3026 "SELECT \n"
3027 " ( SELECT t.alias FROM \n"
3028 " pg_catalog.ts_token_type(c.cfgparser) AS t \n"
3029 " WHERE t.tokid = m.maptokentype ) AS \"%s\", \n"
3030 " pg_catalog.btrim( \n"
3031 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary \n"
3032 " FROM pg_catalog.pg_ts_config_map AS mm \n"
3033 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype \n"
3034 " ORDER BY mapcfg, maptokentype, mapseqno \n"
3035 " ) :: pg_catalog.text , \n"
3036 " '{}') AS \"%s\" \n"
3037 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m \n"
3038 "WHERE c.oid = '%s' AND m.mapcfg = c.oid \n"
3039 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser \n"
3040 "ORDER BY 1",
3041 gettext_noop("Token"),
3042 gettext_noop("Dictionaries"),
3043 oid);
3045 res = PSQLexec(buf.data, false);
3046 termPQExpBuffer(&buf);
3047 if (!res)
3048 return false;
3050 initPQExpBuffer(&title);
3052 if (nspname)
3053 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
3054 nspname, cfgname);
3055 else
3056 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
3057 cfgname);
3059 if (pnspname)
3060 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
3061 pnspname, prsname);
3062 else
3063 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
3064 prsname);
3066 myopt.nullPrint = NULL;
3067 myopt.title = title.data;
3068 myopt.footers = NULL;
3069 myopt.default_footer = false;
3070 myopt.translate_header = true;
3072 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3074 termPQExpBuffer(&title);
3076 PQclear(res);
3077 return true;
3082 * \dew
3084 * Describes foreign-data wrappers
3086 bool
3087 listForeignDataWrappers(const char *pattern, bool verbose)
3089 PQExpBufferData buf;
3090 PGresult *res;
3091 printQueryOpt myopt = pset.popt;
3093 if (pset.sversion < 80400)
3095 fprintf(stderr, _("The server (version %d.%d) does not support foreign-data wrappers.\n"),
3096 pset.sversion / 10000, (pset.sversion / 100) % 100);
3097 return true;
3100 initPQExpBuffer(&buf);
3101 printfPQExpBuffer(&buf,
3102 "SELECT fdwname AS \"%s\",\n"
3103 " pg_catalog.pg_get_userbyid(fdwowner) AS \"%s\",\n"
3104 " fdwvalidator::pg_catalog.regproc AS \"%s\"",
3105 gettext_noop("Name"),
3106 gettext_noop("Owner"),
3107 gettext_noop("Validator"));
3109 if (verbose)
3111 appendPQExpBuffer(&buf, ",\n ");
3112 printACLColumn(&buf, "fdwacl");
3113 appendPQExpBuffer(&buf,
3114 ",\n fdwoptions AS \"%s\"",
3115 gettext_noop("Options"));
3118 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper\n");
3120 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3121 NULL, "fdwname", NULL, NULL);
3123 appendPQExpBuffer(&buf, "ORDER BY 1;");
3125 res = PSQLexec(buf.data, false);
3126 termPQExpBuffer(&buf);
3127 if (!res)
3128 return false;
3130 myopt.nullPrint = NULL;
3131 myopt.title = _("List of foreign-data wrappers");
3132 myopt.translate_header = true;
3134 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3136 PQclear(res);
3137 return true;
3141 * \des
3143 * Describes foreign servers.
3145 bool
3146 listForeignServers(const char *pattern, bool verbose)
3148 PQExpBufferData buf;
3149 PGresult *res;
3150 printQueryOpt myopt = pset.popt;
3152 if (pset.sversion < 80400)
3154 fprintf(stderr, _("The server (version %d.%d) does not support foreign servers.\n"),
3155 pset.sversion / 10000, (pset.sversion / 100) % 100);
3156 return true;
3159 initPQExpBuffer(&buf);
3160 printfPQExpBuffer(&buf,
3161 "SELECT s.srvname AS \"%s\",\n"
3162 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
3163 " f.fdwname AS \"%s\"",
3164 gettext_noop("Name"),
3165 gettext_noop("Owner"),
3166 gettext_noop("Foreign-data wrapper"));
3168 if (verbose)
3170 appendPQExpBuffer(&buf, ",\n ");
3171 printACLColumn(&buf, "s.srvacl");
3172 appendPQExpBuffer(&buf,
3173 ",\n"
3174 " s.srvtype AS \"%s\",\n"
3175 " s.srvversion AS \"%s\",\n"
3176 " s.srvoptions AS \"%s\"",
3177 gettext_noop("Type"),
3178 gettext_noop("Version"),
3179 gettext_noop("Options"));
3182 appendPQExpBuffer(&buf,
3183 "\nFROM pg_catalog.pg_foreign_server s\n"
3184 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
3186 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3187 NULL, "s.srvname", NULL, NULL);
3189 appendPQExpBuffer(&buf, "ORDER BY 1;");
3191 res = PSQLexec(buf.data, false);
3192 termPQExpBuffer(&buf);
3193 if (!res)
3194 return false;
3196 myopt.nullPrint = NULL;
3197 myopt.title = _("List of foreign servers");
3198 myopt.translate_header = true;
3200 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3202 PQclear(res);
3203 return true;
3207 * \deu
3209 * Describes user mappings.
3211 bool
3212 listUserMappings(const char *pattern, bool verbose)
3214 PQExpBufferData buf;
3215 PGresult *res;
3216 printQueryOpt myopt = pset.popt;
3218 if (pset.sversion < 80400)
3220 fprintf(stderr, _("The server (version %d.%d) does not support user mappings.\n"),
3221 pset.sversion / 10000, (pset.sversion / 100) % 100);
3222 return true;
3225 initPQExpBuffer(&buf);
3226 printfPQExpBuffer(&buf,
3227 "SELECT um.srvname AS \"%s\",\n"
3228 " um.usename AS \"%s\"",
3229 gettext_noop("Server"),
3230 gettext_noop("User name"));
3232 if (verbose)
3233 appendPQExpBuffer(&buf,
3234 ",\n um.umoptions AS \"%s\"",
3235 gettext_noop("Options"));
3237 appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
3239 processSQLNamePattern(pset.db, &buf, pattern, false, false,
3240 NULL, "um.srvname", "um.usename", NULL);
3242 appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
3244 res = PSQLexec(buf.data, false);
3245 termPQExpBuffer(&buf);
3246 if (!res)
3247 return false;
3249 myopt.nullPrint = NULL;
3250 myopt.title = _("List of user mappings");
3251 myopt.translate_header = true;
3253 printQuery(res, &myopt, pset.queryFout, pset.logfile);
3255 PQclear(res);
3256 return true;
3260 * printACLColumn
3262 * Helper function for consistently formatting ACL (privilege) columns.
3263 * The proper targetlist entry is appended to buf. Note lack of any
3264 * whitespace or comma decoration.
3266 static void
3267 printACLColumn(PQExpBuffer buf, const char *colname)
3269 if (pset.sversion >= 80100)
3270 appendPQExpBuffer(buf,
3271 "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"",
3272 colname, gettext_noop("Access privileges"));
3273 else
3274 appendPQExpBuffer(buf,
3275 "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"",
3276 colname, gettext_noop("Access privileges"));