PostgreSQL Source Code git master
describe.c
Go to the documentation of this file.
1/*
2 * psql - the PostgreSQL interactive terminal
3 *
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 9.2 and up. It's okay to omit irrelevant
7 * information for an old server, but not to fail outright. (But failing
8 * against a pre-9.2 server is allowed.)
9 *
10 * Copyright (c) 2000-2025, PostgreSQL Global Development Group
11 *
12 * src/bin/psql/describe.c
13 */
14#include "postgres_fe.h"
15
16#include <ctype.h>
17
18#include "catalog/pg_am_d.h"
19#include "catalog/pg_amop_d.h"
20#include "catalog/pg_attribute_d.h"
21#include "catalog/pg_cast_d.h"
22#include "catalog/pg_class_d.h"
23#include "catalog/pg_collation_d.h"
24#include "catalog/pg_constraint_d.h"
25#include "catalog/pg_default_acl_d.h"
26#include "catalog/pg_proc_d.h"
27#include "catalog/pg_publication_d.h"
28#include "catalog/pg_statistic_ext_d.h"
29#include "catalog/pg_subscription_d.h"
30#include "catalog/pg_type_d.h"
31#include "common.h"
32#include "common/logging.h"
33#include "describe.h"
34#include "fe_utils/mbprint.h"
35#include "fe_utils/print.h"
37#include "settings.h"
38
39static const char *map_typename_pattern(const char *pattern);
40static bool describeOneTableDetails(const char *schemaname,
41 const char *relationname,
42 const char *oid,
43 bool verbose);
44static void add_tablespace_footer(printTableContent *const cont, char relkind,
45 Oid tablespace, const bool newline);
46static void add_role_attribute(PQExpBuffer buf, const char *const str);
47static bool listTSParsersVerbose(const char *pattern);
48static bool describeOneTSParser(const char *oid, const char *nspname,
49 const char *prsname);
50static bool listTSConfigsVerbose(const char *pattern);
51static bool describeOneTSConfig(const char *oid, const char *nspname,
52 const char *cfgname,
53 const char *pnspname, const char *prsname);
54static void printACLColumn(PQExpBuffer buf, const char *colname);
55static bool listOneExtensionContents(const char *extname, const char *oid);
56static bool validateSQLNamePattern(PQExpBuffer buf, const char *pattern,
57 bool have_where, bool force_escape,
58 const char *schemavar, const char *namevar,
59 const char *altnamevar,
60 const char *visibilityrule,
61 bool *added_clause, int maxparts);
62
63
64/*----------------
65 * Handlers for various slash commands displaying some sort of list
66 * of things in the database.
67 *
68 * Note: try to format the queries to look nice in -E output.
69 *----------------
70 */
71
72
73/*
74 * \da
75 * Takes an optional regexp to select particular aggregates
76 */
77bool
78describeAggregates(const char *pattern, bool verbose, bool showSystem)
79{
81 PGresult *res;
82 printQueryOpt myopt = pset.popt;
83
85
87 "SELECT n.nspname as \"%s\",\n"
88 " p.proname AS \"%s\",\n"
89 " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n"
90 " CASE WHEN p.pronargs = 0\n"
91 " THEN CAST('*' AS pg_catalog.text)\n"
92 " ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"
93 " END AS \"%s\",\n",
94 gettext_noop("Schema"),
95 gettext_noop("Name"),
96 gettext_noop("Result data type"),
97 gettext_noop("Argument data types"));
98
99 if (pset.sversion >= 110000)
101 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
102 "FROM pg_catalog.pg_proc p\n"
103 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
104 "WHERE p.prokind = " CppAsString2(PROKIND_AGGREGATE) "\n",
105 gettext_noop("Description"));
106 else
108 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
109 "FROM pg_catalog.pg_proc p\n"
110 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
111 "WHERE p.proisagg\n",
112 gettext_noop("Description"));
113
114 if (!showSystem && !pattern)
115 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
116 " AND n.nspname <> 'information_schema'\n");
117
118 if (!validateSQLNamePattern(&buf, pattern, true, false,
119 "n.nspname", "p.proname", NULL,
120 "pg_catalog.pg_function_is_visible(p.oid)",
121 NULL, 3))
122 {
124 return false;
125 }
126
127 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
128
129 res = PSQLexec(buf.data);
131 if (!res)
132 return false;
133
134 myopt.title = _("List of aggregate functions");
135 myopt.translate_header = true;
136
137 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
138
139 PQclear(res);
140 return true;
141}
142
143/*
144 * \dA
145 * Takes an optional regexp to select particular access methods
146 */
147bool
148describeAccessMethods(const char *pattern, bool verbose)
149{
151 PGresult *res;
152 printQueryOpt myopt = pset.popt;
153 static const bool translate_columns[] = {false, true, false, false};
154
155 if (pset.sversion < 90600)
156 {
157 char sverbuf[32];
158
159 pg_log_error("The server (version %s) does not support access methods.",
161 sverbuf, sizeof(sverbuf)));
162 return true;
163 }
164
166
168 "SELECT amname AS \"%s\",\n"
169 " CASE amtype"
170 " WHEN " CppAsString2(AMTYPE_INDEX) " THEN '%s'"
171 " WHEN " CppAsString2(AMTYPE_TABLE) " THEN '%s'"
172 " END AS \"%s\"",
173 gettext_noop("Name"),
174 gettext_noop("Index"),
175 gettext_noop("Table"),
176 gettext_noop("Type"));
177
178 if (verbose)
179 {
181 ",\n amhandler AS \"%s\",\n"
182 " pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
183 gettext_noop("Handler"),
184 gettext_noop("Description"));
185 }
186
188 "\nFROM pg_catalog.pg_am\n");
189
190 if (!validateSQLNamePattern(&buf, pattern, false, false,
191 NULL, "amname", NULL,
192 NULL,
193 NULL, 1))
194 {
196 return false;
197 }
198
199 appendPQExpBufferStr(&buf, "ORDER BY 1;");
200
201 res = PSQLexec(buf.data);
203 if (!res)
204 return false;
205
206 myopt.title = _("List of access methods");
207 myopt.translate_header = true;
208 myopt.translate_columns = translate_columns;
209 myopt.n_translate_columns = lengthof(translate_columns);
210
211 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
212
213 PQclear(res);
214 return true;
215}
216
217/*
218 * \db
219 * Takes an optional regexp to select particular tablespaces
220 */
221bool
222describeTablespaces(const char *pattern, bool verbose)
223{
225 PGresult *res;
226 printQueryOpt myopt = pset.popt;
227
229
231 "SELECT spcname AS \"%s\",\n"
232 " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
233 " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
234 gettext_noop("Name"),
235 gettext_noop("Owner"),
236 gettext_noop("Location"));
237
238 if (verbose)
239 {
240 appendPQExpBufferStr(&buf, ",\n ");
241 printACLColumn(&buf, "spcacl");
243 ",\n spcoptions AS \"%s\""
244 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\""
245 ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
246 gettext_noop("Options"),
247 gettext_noop("Size"),
248 gettext_noop("Description"));
249 }
250
252 "\nFROM pg_catalog.pg_tablespace\n");
253
254 if (!validateSQLNamePattern(&buf, pattern, false, false,
255 NULL, "spcname", NULL,
256 NULL,
257 NULL, 1))
258 {
260 return false;
261 }
262
263 appendPQExpBufferStr(&buf, "ORDER BY 1;");
264
265 res = PSQLexec(buf.data);
267 if (!res)
268 return false;
269
270 myopt.title = _("List of tablespaces");
271 myopt.translate_header = true;
272
273 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
274
275 PQclear(res);
276 return true;
277}
278
279
280/*
281 * \df
282 * Takes an optional regexp to select particular functions.
283 *
284 * As with \d, you can specify the kinds of functions you want:
285 *
286 * a for aggregates
287 * n for normal
288 * p for procedure
289 * t for trigger
290 * w for window
291 *
292 * and you can mix and match these in any order.
293 */
294bool
295describeFunctions(const char *functypes, const char *func_pattern,
296 char **arg_patterns, int num_arg_patterns,
297 bool verbose, bool showSystem)
298{
299 bool showAggregate = strchr(functypes, 'a') != NULL;
300 bool showNormal = strchr(functypes, 'n') != NULL;
301 bool showProcedure = strchr(functypes, 'p') != NULL;
302 bool showTrigger = strchr(functypes, 't') != NULL;
303 bool showWindow = strchr(functypes, 'w') != NULL;
304 bool have_where;
306 PGresult *res;
307 printQueryOpt myopt = pset.popt;
308 static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, true, false, false, false, false};
309
310 /* No "Parallel" column before 9.6 */
311 static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, true, false, false, false, false};
312
313 if (strlen(functypes) != strspn(functypes, "anptwSx+"))
314 {
315 pg_log_error("\\df only takes [anptwSx+] as options");
316 return true;
317 }
318
319 if (showProcedure && pset.sversion < 110000)
320 {
321 char sverbuf[32];
322
323 pg_log_error("\\df does not take a \"%c\" option with server version %s",
324 'p',
326 sverbuf, sizeof(sverbuf)));
327 return true;
328 }
329
330 if (!showAggregate && !showNormal && !showProcedure && !showTrigger && !showWindow)
331 {
332 showAggregate = showNormal = showTrigger = showWindow = true;
333 if (pset.sversion >= 110000)
334 showProcedure = true;
335 }
336
338
340 "SELECT n.nspname as \"%s\",\n"
341 " p.proname as \"%s\",\n",
342 gettext_noop("Schema"),
343 gettext_noop("Name"));
344
345 if (pset.sversion >= 110000)
347 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
348 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
349 " CASE p.prokind\n"
350 " WHEN " CppAsString2(PROKIND_AGGREGATE) " THEN '%s'\n"
351 " WHEN " CppAsString2(PROKIND_WINDOW) " THEN '%s'\n"
352 " WHEN " CppAsString2(PROKIND_PROCEDURE) " THEN '%s'\n"
353 " ELSE '%s'\n"
354 " END as \"%s\"",
355 gettext_noop("Result data type"),
356 gettext_noop("Argument data types"),
357 /* translator: "agg" is short for "aggregate" */
358 gettext_noop("agg"),
359 gettext_noop("window"),
360 gettext_noop("proc"),
361 gettext_noop("func"),
362 gettext_noop("Type"));
363 else
365 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
366 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
367 " CASE\n"
368 " WHEN p.proisagg THEN '%s'\n"
369 " WHEN p.proiswindow THEN '%s'\n"
370 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
371 " ELSE '%s'\n"
372 " END as \"%s\"",
373 gettext_noop("Result data type"),
374 gettext_noop("Argument data types"),
375 /* translator: "agg" is short for "aggregate" */
376 gettext_noop("agg"),
377 gettext_noop("window"),
378 gettext_noop("trigger"),
379 gettext_noop("func"),
380 gettext_noop("Type"));
381
382 if (verbose)
383 {
385 ",\n CASE\n"
386 " WHEN p.provolatile = "
387 CppAsString2(PROVOLATILE_IMMUTABLE) " THEN '%s'\n"
388 " WHEN p.provolatile = "
389 CppAsString2(PROVOLATILE_STABLE) " THEN '%s'\n"
390 " WHEN p.provolatile = "
391 CppAsString2(PROVOLATILE_VOLATILE) " THEN '%s'\n"
392 " END as \"%s\"",
393 gettext_noop("immutable"),
394 gettext_noop("stable"),
395 gettext_noop("volatile"),
396 gettext_noop("Volatility"));
397 if (pset.sversion >= 90600)
399 ",\n CASE\n"
400 " WHEN p.proparallel = "
401 CppAsString2(PROPARALLEL_RESTRICTED) " THEN '%s'\n"
402 " WHEN p.proparallel = "
403 CppAsString2(PROPARALLEL_SAFE) " THEN '%s'\n"
404 " WHEN p.proparallel = "
405 CppAsString2(PROPARALLEL_UNSAFE) " THEN '%s'\n"
406 " END as \"%s\"",
407 gettext_noop("restricted"),
408 gettext_noop("safe"),
409 gettext_noop("unsafe"),
410 gettext_noop("Parallel"));
412 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
413 ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\""
414 ",\n CASE WHEN p.proleakproof THEN '%s' ELSE '%s' END as \"%s\"",
415 gettext_noop("Owner"),
416 gettext_noop("definer"),
417 gettext_noop("invoker"),
418 gettext_noop("Security"),
419 gettext_noop("yes"),
420 gettext_noop("no"),
421 gettext_noop("Leakproof?"));
422 appendPQExpBufferStr(&buf, ",\n ");
423 printACLColumn(&buf, "p.proacl");
425 ",\n l.lanname as \"%s\"",
426 gettext_noop("Language"));
428 ",\n CASE WHEN l.lanname IN ('internal', 'c') THEN p.prosrc END as \"%s\"",
429 gettext_noop("Internal name"));
431 ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
432 gettext_noop("Description"));
433 }
434
436 "\nFROM pg_catalog.pg_proc p"
437 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
438
439 for (int i = 0; i < num_arg_patterns; i++)
440 {
442 " LEFT JOIN pg_catalog.pg_type t%d ON t%d.oid = p.proargtypes[%d]\n"
443 " LEFT JOIN pg_catalog.pg_namespace nt%d ON nt%d.oid = t%d.typnamespace\n",
444 i, i, i, i, i, i);
445 }
446
447 if (verbose)
449 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
450
451 have_where = false;
452
453 /* filter by function type, if requested */
454 if (showNormal && showAggregate && showProcedure && showTrigger && showWindow)
455 /* Do nothing */ ;
456 else if (showNormal)
457 {
458 if (!showAggregate)
459 {
460 if (have_where)
461 appendPQExpBufferStr(&buf, " AND ");
462 else
463 {
464 appendPQExpBufferStr(&buf, "WHERE ");
465 have_where = true;
466 }
467 if (pset.sversion >= 110000)
468 appendPQExpBufferStr(&buf, "p.prokind <> "
469 CppAsString2(PROKIND_AGGREGATE) "\n");
470 else
471 appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
472 }
473 if (!showProcedure && pset.sversion >= 110000)
474 {
475 if (have_where)
476 appendPQExpBufferStr(&buf, " AND ");
477 else
478 {
479 appendPQExpBufferStr(&buf, "WHERE ");
480 have_where = true;
481 }
482 appendPQExpBufferStr(&buf, "p.prokind <> "
483 CppAsString2(PROKIND_PROCEDURE) "\n");
484 }
485 if (!showTrigger)
486 {
487 if (have_where)
488 appendPQExpBufferStr(&buf, " AND ");
489 else
490 {
491 appendPQExpBufferStr(&buf, "WHERE ");
492 have_where = true;
493 }
494 appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
495 }
496 if (!showWindow)
497 {
498 if (have_where)
499 appendPQExpBufferStr(&buf, " AND ");
500 else
501 {
502 appendPQExpBufferStr(&buf, "WHERE ");
503 have_where = true;
504 }
505 if (pset.sversion >= 110000)
506 appendPQExpBufferStr(&buf, "p.prokind <> "
507 CppAsString2(PROKIND_WINDOW) "\n");
508 else
509 appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
510 }
511 }
512 else
513 {
514 bool needs_or = false;
515
516 appendPQExpBufferStr(&buf, "WHERE (\n ");
517 have_where = true;
518 /* Note: at least one of these must be true ... */
519 if (showAggregate)
520 {
521 if (pset.sversion >= 110000)
522 appendPQExpBufferStr(&buf, "p.prokind = "
523 CppAsString2(PROKIND_AGGREGATE) "\n");
524 else
525 appendPQExpBufferStr(&buf, "p.proisagg\n");
526 needs_or = true;
527 }
528 if (showTrigger)
529 {
530 if (needs_or)
531 appendPQExpBufferStr(&buf, " OR ");
533 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
534 needs_or = true;
535 }
536 if (showProcedure)
537 {
538 if (needs_or)
539 appendPQExpBufferStr(&buf, " OR ");
540 appendPQExpBufferStr(&buf, "p.prokind = "
541 CppAsString2(PROKIND_PROCEDURE) "\n");
542 needs_or = true;
543 }
544 if (showWindow)
545 {
546 if (needs_or)
547 appendPQExpBufferStr(&buf, " OR ");
548 if (pset.sversion >= 110000)
549 appendPQExpBufferStr(&buf, "p.prokind = "
550 CppAsString2(PROKIND_WINDOW) "\n");
551 else
552 appendPQExpBufferStr(&buf, "p.proiswindow\n");
553 }
554 appendPQExpBufferStr(&buf, " )\n");
555 }
556
557 if (!validateSQLNamePattern(&buf, func_pattern, have_where, false,
558 "n.nspname", "p.proname", NULL,
559 "pg_catalog.pg_function_is_visible(p.oid)",
560 NULL, 3))
561 goto error_return;
562
563 for (int i = 0; i < num_arg_patterns; i++)
564 {
565 if (strcmp(arg_patterns[i], "-") != 0)
566 {
567 /*
568 * Match type-name patterns against either internal or external
569 * name, like \dT. Unlike \dT, there seems no reason to
570 * discriminate against arrays or composite types.
571 */
572 char nspname[64];
573 char typname[64];
574 char ft[64];
575 char tiv[64];
576
577 snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
578 snprintf(typname, sizeof(typname), "t%d.typname", i);
579 snprintf(ft, sizeof(ft),
580 "pg_catalog.format_type(t%d.oid, NULL)", i);
581 snprintf(tiv, sizeof(tiv),
582 "pg_catalog.pg_type_is_visible(t%d.oid)", i);
584 map_typename_pattern(arg_patterns[i]),
585 true, false,
586 nspname, typname, ft, tiv,
587 NULL, 3))
588 goto error_return;
589 }
590 else
591 {
592 /* "-" pattern specifies no such parameter */
593 appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
594 }
595 }
596
597 if (!showSystem && !func_pattern)
598 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
599 " AND n.nspname <> 'information_schema'\n");
600
601 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
602
603 res = PSQLexec(buf.data);
605 if (!res)
606 return false;
607
608 myopt.title = _("List of functions");
609 myopt.translate_header = true;
610 if (pset.sversion >= 90600)
611 {
612 myopt.translate_columns = translate_columns;
613 myopt.n_translate_columns = lengthof(translate_columns);
614 }
615 else
616 {
617 myopt.translate_columns = translate_columns_pre_96;
618 myopt.n_translate_columns = lengthof(translate_columns_pre_96);
619 }
620
621 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
622
623 PQclear(res);
624 return true;
625
626error_return:
628 return false;
629}
630
631
632
633/*
634 * \dT
635 * describe types
636 */
637bool
638describeTypes(const char *pattern, bool verbose, bool showSystem)
639{
641 PGresult *res;
642 printQueryOpt myopt = pset.popt;
643
645
647 "SELECT n.nspname as \"%s\",\n"
648 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
649 gettext_noop("Schema"),
650 gettext_noop("Name"));
651 if (verbose)
652 {
654 " t.typname AS \"%s\",\n"
655 " CASE WHEN t.typrelid != 0\n"
656 " THEN CAST('tuple' AS pg_catalog.text)\n"
657 " WHEN t.typlen < 0\n"
658 " THEN CAST('var' AS pg_catalog.text)\n"
659 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
660 " END AS \"%s\",\n"
661 " pg_catalog.array_to_string(\n"
662 " ARRAY(\n"
663 " SELECT e.enumlabel\n"
664 " FROM pg_catalog.pg_enum e\n"
665 " WHERE e.enumtypid = t.oid\n"
666 " ORDER BY e.enumsortorder\n"
667 " ),\n"
668 " E'\\n'\n"
669 " ) AS \"%s\",\n"
670 " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
671 gettext_noop("Internal name"),
672 gettext_noop("Size"),
673 gettext_noop("Elements"),
674 gettext_noop("Owner"));
675 printACLColumn(&buf, "t.typacl");
676 appendPQExpBufferStr(&buf, ",\n ");
677 }
678
680 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
681 gettext_noop("Description"));
682
683 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
684 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
685
686 /*
687 * do not include complex types (typrelid!=0) unless they are standalone
688 * composite types
689 */
690 appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
691 appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
692 " FROM pg_catalog.pg_class c "
693 "WHERE c.oid = t.typrelid))\n");
694
695 /*
696 * do not include array types unless the pattern contains []
697 */
698 if (pattern == NULL || strstr(pattern, "[]") == NULL)
699 appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
700
701 if (!showSystem && !pattern)
702 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
703 " AND n.nspname <> 'information_schema'\n");
704
705 /* Match name pattern against either internal or external name */
707 true, false,
708 "n.nspname", "t.typname",
709 "pg_catalog.format_type(t.oid, NULL)",
710 "pg_catalog.pg_type_is_visible(t.oid)",
711 NULL, 3))
712 {
714 return false;
715 }
716
717 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
718
719 res = PSQLexec(buf.data);
721 if (!res)
722 return false;
723
724 myopt.title = _("List of data types");
725 myopt.translate_header = true;
726
727 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
728
729 PQclear(res);
730 return true;
731}
732
733/*
734 * Map some variant type names accepted by the backend grammar into
735 * canonical type names.
736 *
737 * Helper for \dT and other functions that take typename patterns.
738 * This doesn't completely mask the fact that these names are special;
739 * for example, a pattern of "dec*" won't magically match "numeric".
740 * But it goes a long way to reduce the surprise factor.
741 */
742static const char *
743map_typename_pattern(const char *pattern)
744{
745 static const char *const typename_map[] = {
746 /*
747 * These names are accepted by gram.y, although they are neither the
748 * "real" name seen in pg_type nor the canonical name printed by
749 * format_type().
750 */
751 "decimal", "numeric",
752 "float", "double precision",
753 "int", "integer",
754
755 /*
756 * We also have to map the array names for cases where the canonical
757 * name is different from what pg_type says.
758 */
759 "bool[]", "boolean[]",
760 "decimal[]", "numeric[]",
761 "float[]", "double precision[]",
762 "float4[]", "real[]",
763 "float8[]", "double precision[]",
764 "int[]", "integer[]",
765 "int2[]", "smallint[]",
766 "int4[]", "integer[]",
767 "int8[]", "bigint[]",
768 "time[]", "time without time zone[]",
769 "timetz[]", "time with time zone[]",
770 "timestamp[]", "timestamp without time zone[]",
771 "timestamptz[]", "timestamp with time zone[]",
772 "varbit[]", "bit varying[]",
773 "varchar[]", "character varying[]",
774 NULL
775 };
776
777 if (pattern == NULL)
778 return NULL;
779 for (int i = 0; typename_map[i] != NULL; i += 2)
780 {
781 if (pg_strcasecmp(pattern, typename_map[i]) == 0)
782 return typename_map[i + 1];
783 }
784 return pattern;
785}
786
787
788/*
789 * \do
790 * Describe operators
791 */
792bool
793describeOperators(const char *oper_pattern,
794 char **arg_patterns, int num_arg_patterns,
795 bool verbose, bool showSystem)
796{
798 PGresult *res;
799 printQueryOpt myopt = pset.popt;
800 static const bool translate_columns[] = {false, false, false, false, false, false, true, false};
801
803
804 /*
805 * Note: before Postgres 9.1, we did not assign comments to any built-in
806 * operators, preferring to let the comment on the underlying function
807 * suffice. The coalesce() on the obj_description() calls below supports
808 * this convention by providing a fallback lookup of a comment on the
809 * operator's function. Since 9.1 there is a policy that every built-in
810 * operator should have a comment; so the coalesce() is no longer
811 * necessary so far as built-in operators are concerned. We keep it
812 * anyway, for now, because third-party modules may still be following the
813 * old convention.
814 *
815 * The support for postfix operators in this query is dead code as of
816 * Postgres 14, but we need to keep it for as long as we support talking
817 * to pre-v14 servers.
818 */
819
821 "SELECT n.nspname as \"%s\",\n"
822 " o.oprname AS \"%s\",\n"
823 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
824 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
825 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
826 gettext_noop("Schema"),
827 gettext_noop("Name"),
828 gettext_noop("Left arg type"),
829 gettext_noop("Right arg type"),
830 gettext_noop("Result type"));
831
832 if (verbose)
834 " o.oprcode AS \"%s\",\n"
835 " CASE WHEN p.proleakproof THEN '%s' ELSE '%s' END AS \"%s\",\n",
836 gettext_noop("Function"),
837 gettext_noop("yes"),
838 gettext_noop("no"),
839 gettext_noop("Leakproof?"));
840
842 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
843 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
844 "FROM pg_catalog.pg_operator o\n"
845 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
846 gettext_noop("Description"));
847
848 if (num_arg_patterns >= 2)
849 {
850 num_arg_patterns = 2; /* ignore any additional arguments */
852 " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprleft\n"
853 " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n"
854 " LEFT JOIN pg_catalog.pg_type t1 ON t1.oid = o.oprright\n"
855 " LEFT JOIN pg_catalog.pg_namespace nt1 ON nt1.oid = t1.typnamespace\n");
856 }
857 else if (num_arg_patterns == 1)
858 {
860 " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprright\n"
861 " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n");
862 }
863
864 if (verbose)
866 " LEFT JOIN pg_catalog.pg_proc p ON p.oid = o.oprcode\n");
867
868 if (!showSystem && !oper_pattern)
869 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
870 " AND n.nspname <> 'information_schema'\n");
871
872 if (!validateSQLNamePattern(&buf, oper_pattern,
873 !showSystem && !oper_pattern, true,
874 "n.nspname", "o.oprname", NULL,
875 "pg_catalog.pg_operator_is_visible(o.oid)",
876 NULL, 3))
877 goto error_return;
878
879 if (num_arg_patterns == 1)
880 appendPQExpBufferStr(&buf, " AND o.oprleft = 0\n");
881
882 for (int i = 0; i < num_arg_patterns; i++)
883 {
884 if (strcmp(arg_patterns[i], "-") != 0)
885 {
886 /*
887 * Match type-name patterns against either internal or external
888 * name, like \dT. Unlike \dT, there seems no reason to
889 * discriminate against arrays or composite types.
890 */
891 char nspname[64];
892 char typname[64];
893 char ft[64];
894 char tiv[64];
895
896 snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
897 snprintf(typname, sizeof(typname), "t%d.typname", i);
898 snprintf(ft, sizeof(ft),
899 "pg_catalog.format_type(t%d.oid, NULL)", i);
900 snprintf(tiv, sizeof(tiv),
901 "pg_catalog.pg_type_is_visible(t%d.oid)", i);
903 map_typename_pattern(arg_patterns[i]),
904 true, false,
905 nspname, typname, ft, tiv,
906 NULL, 3))
907 goto error_return;
908 }
909 else
910 {
911 /* "-" pattern specifies no such parameter */
912 appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
913 }
914 }
915
916 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
917
918 res = PSQLexec(buf.data);
920 if (!res)
921 return false;
922
923 myopt.title = _("List of operators");
924 myopt.translate_header = true;
925 myopt.translate_columns = translate_columns;
926 myopt.n_translate_columns = lengthof(translate_columns);
927
928 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
929
930 PQclear(res);
931 return true;
932
933error_return:
935 return false;
936}
937
938
939/*
940 * listAllDbs
941 *
942 * for \l, \list, and -l switch
943 */
944bool
945listAllDbs(const char *pattern, bool verbose)
946{
947 PGresult *res;
949 printQueryOpt myopt = pset.popt;
950
952
954 "SELECT\n"
955 " d.datname as \"%s\",\n"
956 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
957 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
958 gettext_noop("Name"),
959 gettext_noop("Owner"),
960 gettext_noop("Encoding"));
961 if (pset.sversion >= 150000)
963 " CASE d.datlocprovider "
964 "WHEN " CppAsString2(COLLPROVIDER_BUILTIN) " THEN 'builtin' "
965 "WHEN " CppAsString2(COLLPROVIDER_LIBC) " THEN 'libc' "
966 "WHEN " CppAsString2(COLLPROVIDER_ICU) " THEN 'icu' "
967 "END AS \"%s\",\n",
968 gettext_noop("Locale Provider"));
969 else
971 " 'libc' AS \"%s\",\n",
972 gettext_noop("Locale Provider"));
974 " d.datcollate as \"%s\",\n"
975 " d.datctype as \"%s\",\n",
976 gettext_noop("Collate"),
977 gettext_noop("Ctype"));
978 if (pset.sversion >= 170000)
980 " d.datlocale as \"%s\",\n",
981 gettext_noop("Locale"));
982 else if (pset.sversion >= 150000)
984 " d.daticulocale as \"%s\",\n",
985 gettext_noop("Locale"));
986 else
988 " NULL as \"%s\",\n",
989 gettext_noop("Locale"));
990 if (pset.sversion >= 160000)
992 " d.daticurules as \"%s\",\n",
993 gettext_noop("ICU Rules"));
994 else
996 " NULL as \"%s\",\n",
997 gettext_noop("ICU Rules"));
999 printACLColumn(&buf, "d.datacl");
1000 if (verbose)
1002 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
1003 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
1004 " ELSE 'No Access'\n"
1005 " END as \"%s\""
1006 ",\n t.spcname as \"%s\""
1007 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
1008 gettext_noop("Size"),
1009 gettext_noop("Tablespace"),
1010 gettext_noop("Description"));
1012 "\nFROM pg_catalog.pg_database d\n");
1013 if (verbose)
1015 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
1016
1017 if (pattern)
1018 {
1019 if (!validateSQLNamePattern(&buf, pattern, false, false,
1020 NULL, "d.datname", NULL, NULL,
1021 NULL, 1))
1022 {
1024 return false;
1025 }
1026 }
1027
1028 appendPQExpBufferStr(&buf, "ORDER BY 1;");
1029 res = PSQLexec(buf.data);
1031 if (!res)
1032 return false;
1033
1034 myopt.title = _("List of databases");
1035 myopt.translate_header = true;
1036
1037 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1038
1039 PQclear(res);
1040 return true;
1041}
1042
1043
1044/*
1045 * List Tables' Grant/Revoke Permissions
1046 * \z (now also \dp -- perhaps more mnemonic)
1047 */
1048bool
1049permissionsList(const char *pattern, bool showSystem)
1050{
1052 PGresult *res;
1053 printQueryOpt myopt = pset.popt;
1054 static const bool translate_columns[] = {false, false, true, false, false, false};
1055
1057
1058 /*
1059 * we ignore indexes and toast tables since they have no meaningful rights
1060 */
1062 "SELECT n.nspname as \"%s\",\n"
1063 " c.relname as \"%s\",\n"
1064 " CASE c.relkind"
1065 " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
1066 " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
1067 " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
1068 " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
1069 " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
1070 " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
1071 " END as \"%s\",\n"
1072 " ",
1073 gettext_noop("Schema"),
1074 gettext_noop("Name"),
1075 gettext_noop("table"),
1076 gettext_noop("view"),
1077 gettext_noop("materialized view"),
1078 gettext_noop("sequence"),
1079 gettext_noop("foreign table"),
1080 gettext_noop("partitioned table"),
1081 gettext_noop("Type"));
1082
1083 printACLColumn(&buf, "c.relacl");
1084
1085 /*
1086 * The formatting of attacl should match printACLColumn(). However, we
1087 * need no special case for an empty attacl, because the backend always
1088 * optimizes that back to NULL.
1089 */
1091 ",\n pg_catalog.array_to_string(ARRAY(\n"
1092 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
1093 " FROM pg_catalog.pg_attribute a\n"
1094 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
1095 " ), E'\\n') AS \"%s\"",
1096 gettext_noop("Column privileges"));
1097
1098 if (pset.sversion >= 90500 && pset.sversion < 100000)
1100 ",\n pg_catalog.array_to_string(ARRAY(\n"
1101 " SELECT polname\n"
1102 " || CASE WHEN polcmd != '*' THEN\n"
1103 " E' (' || polcmd::pg_catalog.text || E'):'\n"
1104 " ELSE E':'\n"
1105 " END\n"
1106 " || CASE WHEN polqual IS NOT NULL THEN\n"
1107 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1108 " ELSE E''\n"
1109 " END\n"
1110 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1111 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1112 " ELSE E''\n"
1113 " END"
1114 " || CASE WHEN polroles <> '{0}' THEN\n"
1115 " E'\\n to: ' || pg_catalog.array_to_string(\n"
1116 " ARRAY(\n"
1117 " SELECT rolname\n"
1118 " FROM pg_catalog.pg_roles\n"
1119 " WHERE oid = ANY (polroles)\n"
1120 " ORDER BY 1\n"
1121 " ), E', ')\n"
1122 " ELSE E''\n"
1123 " END\n"
1124 " FROM pg_catalog.pg_policy pol\n"
1125 " WHERE polrelid = c.oid), E'\\n')\n"
1126 " AS \"%s\"",
1127 gettext_noop("Policies"));
1128
1129 if (pset.sversion >= 100000)
1131 ",\n pg_catalog.array_to_string(ARRAY(\n"
1132 " SELECT polname\n"
1133 " || CASE WHEN NOT polpermissive THEN\n"
1134 " E' (RESTRICTIVE)'\n"
1135 " ELSE '' END\n"
1136 " || CASE WHEN polcmd != '*' THEN\n"
1137 " E' (' || polcmd::pg_catalog.text || E'):'\n"
1138 " ELSE E':'\n"
1139 " END\n"
1140 " || CASE WHEN polqual IS NOT NULL THEN\n"
1141 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1142 " ELSE E''\n"
1143 " END\n"
1144 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1145 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1146 " ELSE E''\n"
1147 " END"
1148 " || CASE WHEN polroles <> '{0}' THEN\n"
1149 " E'\\n to: ' || pg_catalog.array_to_string(\n"
1150 " ARRAY(\n"
1151 " SELECT rolname\n"
1152 " FROM pg_catalog.pg_roles\n"
1153 " WHERE oid = ANY (polroles)\n"
1154 " ORDER BY 1\n"
1155 " ), E', ')\n"
1156 " ELSE E''\n"
1157 " END\n"
1158 " FROM pg_catalog.pg_policy pol\n"
1159 " WHERE polrelid = c.oid), E'\\n')\n"
1160 " AS \"%s\"",
1161 gettext_noop("Policies"));
1162
1163 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
1164 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1165 "WHERE c.relkind IN ("
1166 CppAsString2(RELKIND_RELATION) ","
1167 CppAsString2(RELKIND_VIEW) ","
1168 CppAsString2(RELKIND_MATVIEW) ","
1169 CppAsString2(RELKIND_SEQUENCE) ","
1170 CppAsString2(RELKIND_FOREIGN_TABLE) ","
1171 CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
1172
1173 if (!showSystem && !pattern)
1174 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1175 " AND n.nspname <> 'information_schema'\n");
1176
1177 if (!validateSQLNamePattern(&buf, pattern, true, false,
1178 "n.nspname", "c.relname", NULL,
1179 "pg_catalog.pg_table_is_visible(c.oid)",
1180 NULL, 3))
1181 goto error_return;
1182
1183 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
1184
1185 res = PSQLexec(buf.data);
1186 if (!res)
1187 goto error_return;
1188
1189 printfPQExpBuffer(&buf, _("Access privileges"));
1190 myopt.title = buf.data;
1191 myopt.translate_header = true;
1192 myopt.translate_columns = translate_columns;
1193 myopt.n_translate_columns = lengthof(translate_columns);
1194
1195 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1196
1198 PQclear(res);
1199 return true;
1200
1201error_return:
1203 return false;
1204}
1205
1206
1207/*
1208 * \ddp
1209 *
1210 * List Default ACLs. The pattern can match either schema or role name.
1211 */
1212bool
1213listDefaultACLs(const char *pattern)
1214{
1216 PGresult *res;
1217 printQueryOpt myopt = pset.popt;
1218 static const bool translate_columns[] = {false, false, true, false};
1219
1221
1223 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1224 " n.nspname AS \"%s\",\n"
1225 " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
1226 " ",
1227 gettext_noop("Owner"),
1228 gettext_noop("Schema"),
1229 DEFACLOBJ_RELATION,
1230 gettext_noop("table"),
1231 DEFACLOBJ_SEQUENCE,
1232 gettext_noop("sequence"),
1233 DEFACLOBJ_FUNCTION,
1234 gettext_noop("function"),
1235 DEFACLOBJ_TYPE,
1236 gettext_noop("type"),
1237 DEFACLOBJ_NAMESPACE,
1238 gettext_noop("schema"),
1239 gettext_noop("Type"));
1240
1241 printACLColumn(&buf, "d.defaclacl");
1242
1243 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1244 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1245
1246 if (!validateSQLNamePattern(&buf, pattern, false, false,
1247 NULL,
1248 "n.nspname",
1249 "pg_catalog.pg_get_userbyid(d.defaclrole)",
1250 NULL,
1251 NULL, 3))
1252 goto error_return;
1253
1254 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1255
1256 res = PSQLexec(buf.data);
1257 if (!res)
1258 goto error_return;
1259
1260 printfPQExpBuffer(&buf, _("Default access privileges"));
1261 myopt.title = buf.data;
1262 myopt.translate_header = true;
1263 myopt.translate_columns = translate_columns;
1264 myopt.n_translate_columns = lengthof(translate_columns);
1265
1266 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1267
1269 PQclear(res);
1270 return true;
1271
1272error_return:
1274 return false;
1275}
1276
1277
1278/*
1279 * Get object comments
1280 *
1281 * \dd [foo]
1282 *
1283 * Note: This command only lists comments for object types which do not have
1284 * their comments displayed by their own backslash commands. The following
1285 * types of objects will be displayed: constraint, operator class,
1286 * operator family, rule, and trigger.
1287 *
1288 */
1289bool
1290objectDescription(const char *pattern, bool showSystem)
1291{
1293 PGresult *res;
1294 printQueryOpt myopt = pset.popt;
1295 static const bool translate_columns[] = {false, false, true, false};
1296
1298
1300 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
1301 "FROM (\n",
1302 gettext_noop("Schema"),
1303 gettext_noop("Name"),
1304 gettext_noop("Object"),
1305 gettext_noop("Description"));
1306
1307 /* Table constraint descriptions */
1309 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1310 " n.nspname as nspname,\n"
1311 " CAST(pgc.conname AS pg_catalog.text) as name,"
1312 " CAST('%s' AS pg_catalog.text) as object\n"
1313 " FROM pg_catalog.pg_constraint pgc\n"
1314 " JOIN pg_catalog.pg_class c "
1315 "ON c.oid = pgc.conrelid\n"
1316 " LEFT JOIN pg_catalog.pg_namespace n "
1317 " ON n.oid = c.relnamespace\n",
1318 gettext_noop("table constraint"));
1319
1320 if (!showSystem && !pattern)
1321 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1322 " AND n.nspname <> 'information_schema'\n");
1323
1324 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1325 false, "n.nspname", "pgc.conname", NULL,
1326 "pg_catalog.pg_table_is_visible(c.oid)",
1327 NULL, 3))
1328 goto error_return;
1329
1330 /* Domain constraint descriptions */
1332 "UNION ALL\n"
1333 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1334 " n.nspname as nspname,\n"
1335 " CAST(pgc.conname AS pg_catalog.text) as name,"
1336 " CAST('%s' AS pg_catalog.text) as object\n"
1337 " FROM pg_catalog.pg_constraint pgc\n"
1338 " JOIN pg_catalog.pg_type t "
1339 "ON t.oid = pgc.contypid\n"
1340 " LEFT JOIN pg_catalog.pg_namespace n "
1341 " ON n.oid = t.typnamespace\n",
1342 gettext_noop("domain constraint"));
1343
1344 if (!showSystem && !pattern)
1345 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1346 " AND n.nspname <> 'information_schema'\n");
1347
1348 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1349 false, "n.nspname", "pgc.conname", NULL,
1350 "pg_catalog.pg_type_is_visible(t.oid)",
1351 NULL, 3))
1352 goto error_return;
1353
1354 /* Operator class descriptions */
1356 "UNION ALL\n"
1357 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
1358 " n.nspname as nspname,\n"
1359 " CAST(o.opcname AS pg_catalog.text) as name,\n"
1360 " CAST('%s' AS pg_catalog.text) as object\n"
1361 " FROM pg_catalog.pg_opclass o\n"
1362 " JOIN pg_catalog.pg_am am ON "
1363 "o.opcmethod = am.oid\n"
1364 " JOIN pg_catalog.pg_namespace n ON "
1365 "n.oid = o.opcnamespace\n",
1366 gettext_noop("operator class"));
1367
1368 if (!showSystem && !pattern)
1369 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1370 " AND n.nspname <> 'information_schema'\n");
1371
1372 if (!validateSQLNamePattern(&buf, pattern, true, false,
1373 "n.nspname", "o.opcname", NULL,
1374 "pg_catalog.pg_opclass_is_visible(o.oid)",
1375 NULL, 3))
1376 goto error_return;
1377
1378 /* Operator family descriptions */
1380 "UNION ALL\n"
1381 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1382 " n.nspname as nspname,\n"
1383 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1384 " CAST('%s' AS pg_catalog.text) as object\n"
1385 " FROM pg_catalog.pg_opfamily opf\n"
1386 " JOIN pg_catalog.pg_am am "
1387 "ON opf.opfmethod = am.oid\n"
1388 " JOIN pg_catalog.pg_namespace n "
1389 "ON opf.opfnamespace = n.oid\n",
1390 gettext_noop("operator family"));
1391
1392 if (!showSystem && !pattern)
1393 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1394 " AND n.nspname <> 'information_schema'\n");
1395
1396 if (!validateSQLNamePattern(&buf, pattern, true, false,
1397 "n.nspname", "opf.opfname", NULL,
1398 "pg_catalog.pg_opfamily_is_visible(opf.oid)",
1399 NULL, 3))
1400 goto error_return;
1401
1402 /* Rule descriptions (ignore rules for views) */
1404 "UNION ALL\n"
1405 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1406 " n.nspname as nspname,\n"
1407 " CAST(r.rulename AS pg_catalog.text) as name,"
1408 " CAST('%s' AS pg_catalog.text) as object\n"
1409 " FROM pg_catalog.pg_rewrite r\n"
1410 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1411 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1412 " WHERE r.rulename != '_RETURN'\n",
1413 gettext_noop("rule"));
1414
1415 if (!showSystem && !pattern)
1416 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1417 " AND n.nspname <> 'information_schema'\n");
1418
1419 if (!validateSQLNamePattern(&buf, pattern, true, false,
1420 "n.nspname", "r.rulename", NULL,
1421 "pg_catalog.pg_table_is_visible(c.oid)",
1422 NULL, 3))
1423 goto error_return;
1424
1425 /* Trigger descriptions */
1427 "UNION ALL\n"
1428 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1429 " n.nspname as nspname,\n"
1430 " CAST(t.tgname AS pg_catalog.text) as name,"
1431 " CAST('%s' AS pg_catalog.text) as object\n"
1432 " FROM pg_catalog.pg_trigger t\n"
1433 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1434 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1435 gettext_noop("trigger"));
1436
1437 if (!showSystem && !pattern)
1438 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1439 " AND n.nspname <> 'information_schema'\n");
1440
1441 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1442 "n.nspname", "t.tgname", NULL,
1443 "pg_catalog.pg_table_is_visible(c.oid)",
1444 NULL, 3))
1445 goto error_return;
1446
1448 ") AS tt\n"
1449 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1450
1451 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1452
1453 res = PSQLexec(buf.data);
1455 if (!res)
1456 return false;
1457
1458 myopt.title = _("Object descriptions");
1459 myopt.translate_header = true;
1460 myopt.translate_columns = translate_columns;
1461 myopt.n_translate_columns = lengthof(translate_columns);
1462
1463 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1464
1465 PQclear(res);
1466 return true;
1467
1468error_return:
1470 return false;
1471}
1472
1473
1474/*
1475 * describeTableDetails (for \d)
1476 *
1477 * This routine finds the tables to be displayed, and calls
1478 * describeOneTableDetails for each one.
1479 *
1480 * verbose: if true, this is \d+
1481 */
1482bool
1483describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1484{
1486 PGresult *res;
1487 int i;
1488
1490
1492 "SELECT c.oid,\n"
1493 " n.nspname,\n"
1494 " c.relname\n"
1495 "FROM pg_catalog.pg_class c\n"
1496 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1497
1498 if (!showSystem && !pattern)
1499 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1500 " AND n.nspname <> 'information_schema'\n");
1501
1502 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1503 "n.nspname", "c.relname", NULL,
1504 "pg_catalog.pg_table_is_visible(c.oid)",
1505 NULL, 3))
1506 {
1508 return false;
1509 }
1510
1511 appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1512
1513 res = PSQLexec(buf.data);
1515 if (!res)
1516 return false;
1517
1518 if (PQntuples(res) == 0)
1519 {
1520 if (!pset.quiet)
1521 {
1522 if (pattern)
1523 pg_log_error("Did not find any relation named \"%s\".",
1524 pattern);
1525 else
1526 pg_log_error("Did not find any relations.");
1527 }
1528 PQclear(res);
1529 return false;
1530 }
1531
1532 for (i = 0; i < PQntuples(res); i++)
1533 {
1534 const char *oid;
1535 const char *nspname;
1536 const char *relname;
1537
1538 oid = PQgetvalue(res, i, 0);
1539 nspname = PQgetvalue(res, i, 1);
1540 relname = PQgetvalue(res, i, 2);
1541
1542 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1543 {
1544 PQclear(res);
1545 return false;
1546 }
1547 if (cancel_pressed)
1548 {
1549 PQclear(res);
1550 return false;
1551 }
1552 }
1553
1554 PQclear(res);
1555 return true;
1556}
1557
1558/*
1559 * describeOneTableDetails (for \d)
1560 *
1561 * Unfortunately, the information presented here is so complicated that it
1562 * cannot be done in a single query. So we have to assemble the printed table
1563 * by hand and pass it to the underlying printTable() function.
1564 */
1565static bool
1566describeOneTableDetails(const char *schemaname,
1567 const char *relationname,
1568 const char *oid,
1569 bool verbose)
1570{
1571 bool retval = false;
1573 PGresult *res = NULL;
1574 printTableOpt myopt = pset.popt.topt;
1575 printTableContent cont;
1576 bool printTableInitialized = false;
1577 int i;
1578 char *view_def = NULL;
1579 char *headers[12];
1580 PQExpBufferData title;
1582 int cols;
1583 int attname_col = -1, /* column indexes in "res" */
1584 atttype_col = -1,
1585 attrdef_col = -1,
1586 attnotnull_col = -1,
1587 attcoll_col = -1,
1588 attidentity_col = -1,
1589 attgenerated_col = -1,
1590 isindexkey_col = -1,
1591 indexdef_col = -1,
1592 fdwopts_col = -1,
1593 attstorage_col = -1,
1594 attcompression_col = -1,
1595 attstattarget_col = -1,
1596 attdescr_col = -1;
1597 int numrows;
1598 struct
1599 {
1600 int16 checks;
1601 char relkind;
1602 bool hasindex;
1603 bool hasrules;
1604 bool hastriggers;
1605 bool rowsecurity;
1606 bool forcerowsecurity;
1607 bool hasoids;
1608 bool ispartition;
1610 char *reloptions;
1611 char *reloftype;
1612 char relpersistence;
1613 char relreplident;
1614 char *relam;
1615 } tableinfo;
1616 bool show_column_details = false;
1617
1618 myopt.default_footer = false;
1619 /* This output looks confusing in expanded mode. */
1620 myopt.expanded = false;
1621
1623 initPQExpBuffer(&title);
1625
1626 /* Get general table info */
1627 if (pset.sversion >= 120000)
1628 {
1630 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1631 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1632 "false AS relhasoids, c.relispartition, %s, c.reltablespace, "
1633 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1634 "c.relpersistence, c.relreplident, am.amname\n"
1635 "FROM pg_catalog.pg_class c\n "
1636 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1637 "LEFT JOIN pg_catalog.pg_am am ON (c.relam = am.oid)\n"
1638 "WHERE c.oid = '%s';",
1639 (verbose ?
1640 "pg_catalog.array_to_string(c.reloptions || "
1641 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1642 : "''"),
1643 oid);
1644 }
1645 else if (pset.sversion >= 100000)
1646 {
1648 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1649 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1650 "c.relhasoids, c.relispartition, %s, c.reltablespace, "
1651 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1652 "c.relpersistence, c.relreplident\n"
1653 "FROM pg_catalog.pg_class c\n "
1654 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1655 "WHERE c.oid = '%s';",
1656 (verbose ?
1657 "pg_catalog.array_to_string(c.reloptions || "
1658 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1659 : "''"),
1660 oid);
1661 }
1662 else if (pset.sversion >= 90500)
1663 {
1665 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1666 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1667 "c.relhasoids, false as relispartition, %s, c.reltablespace, "
1668 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1669 "c.relpersistence, c.relreplident\n"
1670 "FROM pg_catalog.pg_class c\n "
1671 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1672 "WHERE c.oid = '%s';",
1673 (verbose ?
1674 "pg_catalog.array_to_string(c.reloptions || "
1675 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1676 : "''"),
1677 oid);
1678 }
1679 else if (pset.sversion >= 90400)
1680 {
1682 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1683 "c.relhastriggers, false, false, c.relhasoids, "
1684 "false as relispartition, %s, c.reltablespace, "
1685 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1686 "c.relpersistence, c.relreplident\n"
1687 "FROM pg_catalog.pg_class c\n "
1688 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1689 "WHERE c.oid = '%s';",
1690 (verbose ?
1691 "pg_catalog.array_to_string(c.reloptions || "
1692 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1693 : "''"),
1694 oid);
1695 }
1696 else
1697 {
1699 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1700 "c.relhastriggers, false, false, c.relhasoids, "
1701 "false as relispartition, %s, c.reltablespace, "
1702 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1703 "c.relpersistence\n"
1704 "FROM pg_catalog.pg_class c\n "
1705 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1706 "WHERE c.oid = '%s';",
1707 (verbose ?
1708 "pg_catalog.array_to_string(c.reloptions || "
1709 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1710 : "''"),
1711 oid);
1712 }
1713
1714 res = PSQLexec(buf.data);
1715 if (!res)
1716 goto error_return;
1717
1718 /* Did we get anything? */
1719 if (PQntuples(res) == 0)
1720 {
1721 if (!pset.quiet)
1722 pg_log_error("Did not find any relation with OID %s.", oid);
1723 goto error_return;
1724 }
1725
1726 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1727 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1728 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1729 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1730 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1731 tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1732 tableinfo.forcerowsecurity = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1733 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 7), "t") == 0;
1734 tableinfo.ispartition = strcmp(PQgetvalue(res, 0, 8), "t") == 0;
1735 tableinfo.reloptions = pg_strdup(PQgetvalue(res, 0, 9));
1736 tableinfo.tablespace = atooid(PQgetvalue(res, 0, 10));
1737 tableinfo.reloftype = (strcmp(PQgetvalue(res, 0, 11), "") != 0) ?
1738 pg_strdup(PQgetvalue(res, 0, 11)) : NULL;
1739 tableinfo.relpersistence = *(PQgetvalue(res, 0, 12));
1740 tableinfo.relreplident = (pset.sversion >= 90400) ?
1741 *(PQgetvalue(res, 0, 13)) : 'd';
1742 if (pset.sversion >= 120000)
1743 tableinfo.relam = PQgetisnull(res, 0, 14) ?
1744 NULL : pg_strdup(PQgetvalue(res, 0, 14));
1745 else
1746 tableinfo.relam = NULL;
1747 PQclear(res);
1748 res = NULL;
1749
1750 /*
1751 * If it's a sequence, deal with it here separately.
1752 */
1753 if (tableinfo.relkind == RELKIND_SEQUENCE)
1754 {
1755 PGresult *result = NULL;
1756 printQueryOpt myopt = pset.popt;
1757 char *footers[2] = {NULL, NULL};
1758
1759 if (pset.sversion >= 100000)
1760 {
1762 "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n"
1763 " seqstart AS \"%s\",\n"
1764 " seqmin AS \"%s\",\n"
1765 " seqmax AS \"%s\",\n"
1766 " seqincrement AS \"%s\",\n"
1767 " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n"
1768 " seqcache AS \"%s\"\n",
1769 gettext_noop("Type"),
1770 gettext_noop("Start"),
1771 gettext_noop("Minimum"),
1772 gettext_noop("Maximum"),
1773 gettext_noop("Increment"),
1774 gettext_noop("yes"),
1775 gettext_noop("no"),
1776 gettext_noop("Cycles?"),
1777 gettext_noop("Cache"));
1779 "FROM pg_catalog.pg_sequence\n"
1780 "WHERE seqrelid = '%s';",
1781 oid);
1782 }
1783 else
1784 {
1786 "SELECT 'bigint' AS \"%s\",\n"
1787 " start_value AS \"%s\",\n"
1788 " min_value AS \"%s\",\n"
1789 " max_value AS \"%s\",\n"
1790 " increment_by AS \"%s\",\n"
1791 " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n"
1792 " cache_value AS \"%s\"\n",
1793 gettext_noop("Type"),
1794 gettext_noop("Start"),
1795 gettext_noop("Minimum"),
1796 gettext_noop("Maximum"),
1797 gettext_noop("Increment"),
1798 gettext_noop("yes"),
1799 gettext_noop("no"),
1800 gettext_noop("Cycles?"),
1801 gettext_noop("Cache"));
1802 appendPQExpBuffer(&buf, "FROM %s", fmtId(schemaname));
1803 /* must be separate because fmtId isn't reentrant */
1804 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1805 }
1806
1807 res = PSQLexec(buf.data);
1808 if (!res)
1809 goto error_return;
1810
1811 /* Get the column that owns this sequence */
1812 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1813 "\n pg_catalog.quote_ident(relname) || '.' ||"
1814 "\n pg_catalog.quote_ident(attname),"
1815 "\n d.deptype"
1816 "\nFROM pg_catalog.pg_class c"
1817 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1818 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1819 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1820 "\n a.attrelid=c.oid AND"
1821 "\n a.attnum=d.refobjsubid)"
1822 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1823 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1824 "\n AND d.objid='%s'"
1825 "\n AND d.deptype IN ('a', 'i')",
1826 oid);
1827
1828 result = PSQLexec(buf.data);
1829
1830 /*
1831 * If we get no rows back, don't show anything (obviously). We should
1832 * never get more than one row back, but if we do, just ignore it and
1833 * don't print anything.
1834 */
1835 if (!result)
1836 goto error_return;
1837 else if (PQntuples(result) == 1)
1838 {
1839 switch (PQgetvalue(result, 0, 1)[0])
1840 {
1841 case 'a':
1842 footers[0] = psprintf(_("Owned by: %s"),
1843 PQgetvalue(result, 0, 0));
1844 break;
1845 case 'i':
1846 footers[0] = psprintf(_("Sequence for identity column: %s"),
1847 PQgetvalue(result, 0, 0));
1848 break;
1849 }
1850 }
1851 PQclear(result);
1852
1853 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
1854 printfPQExpBuffer(&title, _("Unlogged sequence \"%s.%s\""),
1855 schemaname, relationname);
1856 else
1857 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1858 schemaname, relationname);
1859
1860 myopt.footers = footers;
1861 myopt.topt.default_footer = false;
1862 myopt.title = title.data;
1863 myopt.translate_header = true;
1864
1865 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1866
1867 free(footers[0]);
1868
1869 retval = true;
1870 goto error_return; /* not an error, just return early */
1871 }
1872
1873 /* Identify whether we should print collation, nullable, default vals */
1874 if (tableinfo.relkind == RELKIND_RELATION ||
1875 tableinfo.relkind == RELKIND_VIEW ||
1876 tableinfo.relkind == RELKIND_MATVIEW ||
1877 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1878 tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1879 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1880 show_column_details = true;
1881
1882 /*
1883 * Get per-column info
1884 *
1885 * Since the set of query columns we need varies depending on relkind and
1886 * server version, we compute all the column numbers on-the-fly. Column
1887 * number variables for columns not fetched are left as -1; this avoids
1888 * duplicative test logic below.
1889 */
1890 cols = 0;
1891 printfPQExpBuffer(&buf, "SELECT a.attname");
1892 attname_col = cols++;
1893 appendPQExpBufferStr(&buf, ",\n pg_catalog.format_type(a.atttypid, a.atttypmod)");
1894 atttype_col = cols++;
1895
1896 if (show_column_details)
1897 {
1898 /* use "pretty" mode for expression to avoid excessive parentheses */
1900 ",\n (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid, true)"
1901 "\n FROM pg_catalog.pg_attrdef d"
1902 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)"
1903 ",\n a.attnotnull");
1904 attrdef_col = cols++;
1905 attnotnull_col = cols++;
1906 appendPQExpBufferStr(&buf, ",\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1907 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1908 attcoll_col = cols++;
1909 if (pset.sversion >= 100000)
1910 appendPQExpBufferStr(&buf, ",\n a.attidentity");
1911 else
1912 appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attidentity");
1913 attidentity_col = cols++;
1914 if (pset.sversion >= 120000)
1915 appendPQExpBufferStr(&buf, ",\n a.attgenerated");
1916 else
1917 appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attgenerated");
1918 attgenerated_col = cols++;
1919 }
1920 if (tableinfo.relkind == RELKIND_INDEX ||
1921 tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1922 {
1923 if (pset.sversion >= 110000)
1924 {
1925 appendPQExpBuffer(&buf, ",\n CASE WHEN a.attnum <= (SELECT i.indnkeyatts FROM pg_catalog.pg_index i WHERE i.indexrelid = '%s') THEN '%s' ELSE '%s' END AS is_key",
1926 oid,
1927 gettext_noop("yes"),
1928 gettext_noop("no"));
1929 isindexkey_col = cols++;
1930 }
1931 appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1932 indexdef_col = cols++;
1933 }
1934 /* FDW options for foreign table column */
1935 if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
1936 {
1937 appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1938 " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1939 " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1940 fdwopts_col = cols++;
1941 }
1942 if (verbose)
1943 {
1944 appendPQExpBufferStr(&buf, ",\n a.attstorage");
1945 attstorage_col = cols++;
1946
1947 /* compression info, if relevant to relkind */
1948 if (pset.sversion >= 140000 &&
1950 (tableinfo.relkind == RELKIND_RELATION ||
1951 tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
1952 tableinfo.relkind == RELKIND_MATVIEW))
1953 {
1954 appendPQExpBufferStr(&buf, ",\n a.attcompression AS attcompression");
1955 attcompression_col = cols++;
1956 }
1957
1958 /* stats target, if relevant to relkind */
1959 if (tableinfo.relkind == RELKIND_RELATION ||
1960 tableinfo.relkind == RELKIND_INDEX ||
1961 tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1962 tableinfo.relkind == RELKIND_MATVIEW ||
1963 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1964 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1965 {
1966 appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1967 attstattarget_col = cols++;
1968 }
1969
1970 /*
1971 * In 9.0+, we have column comments for: relations, views, composite
1972 * types, and foreign tables (cf. CommentObject() in comment.c).
1973 */
1974 if (tableinfo.relkind == RELKIND_RELATION ||
1975 tableinfo.relkind == RELKIND_VIEW ||
1976 tableinfo.relkind == RELKIND_MATVIEW ||
1977 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1978 tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1979 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1980 {
1981 appendPQExpBufferStr(&buf, ",\n pg_catalog.col_description(a.attrelid, a.attnum)");
1982 attdescr_col = cols++;
1983 }
1984 }
1985
1986 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1987 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1988 appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1989
1990 res = PSQLexec(buf.data);
1991 if (!res)
1992 goto error_return;
1993 numrows = PQntuples(res);
1994
1995 /* Make title */
1996 switch (tableinfo.relkind)
1997 {
1998 case RELKIND_RELATION:
1999 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2000 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
2001 schemaname, relationname);
2002 else
2003 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
2004 schemaname, relationname);
2005 break;
2006 case RELKIND_VIEW:
2007 printfPQExpBuffer(&title, _("View \"%s.%s\""),
2008 schemaname, relationname);
2009 break;
2010 case RELKIND_MATVIEW:
2011 printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
2012 schemaname, relationname);
2013 break;
2014 case RELKIND_INDEX:
2015 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2016 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
2017 schemaname, relationname);
2018 else
2019 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
2020 schemaname, relationname);
2021 break;
2022 case RELKIND_PARTITIONED_INDEX:
2023 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2024 printfPQExpBuffer(&title, _("Unlogged partitioned index \"%s.%s\""),
2025 schemaname, relationname);
2026 else
2027 printfPQExpBuffer(&title, _("Partitioned index \"%s.%s\""),
2028 schemaname, relationname);
2029 break;
2030 case RELKIND_TOASTVALUE:
2031 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
2032 schemaname, relationname);
2033 break;
2034 case RELKIND_COMPOSITE_TYPE:
2035 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
2036 schemaname, relationname);
2037 break;
2038 case RELKIND_FOREIGN_TABLE:
2039 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
2040 schemaname, relationname);
2041 break;
2042 case RELKIND_PARTITIONED_TABLE:
2043 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2044 printfPQExpBuffer(&title, _("Unlogged partitioned table \"%s.%s\""),
2045 schemaname, relationname);
2046 else
2047 printfPQExpBuffer(&title, _("Partitioned table \"%s.%s\""),
2048 schemaname, relationname);
2049 break;
2050 default:
2051 /* untranslated unknown relkind */
2052 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
2053 tableinfo.relkind, schemaname, relationname);
2054 break;
2055 }
2056
2057 /* Fill headers[] with the names of the columns we will output */
2058 cols = 0;
2059 headers[cols++] = gettext_noop("Column");
2060 headers[cols++] = gettext_noop("Type");
2061 if (show_column_details)
2062 {
2063 headers[cols++] = gettext_noop("Collation");
2064 headers[cols++] = gettext_noop("Nullable");
2065 headers[cols++] = gettext_noop("Default");
2066 }
2067 if (isindexkey_col >= 0)
2068 headers[cols++] = gettext_noop("Key?");
2069 if (indexdef_col >= 0)
2070 headers[cols++] = gettext_noop("Definition");
2071 if (fdwopts_col >= 0)
2072 headers[cols++] = gettext_noop("FDW options");
2073 if (attstorage_col >= 0)
2074 headers[cols++] = gettext_noop("Storage");
2075 if (attcompression_col >= 0)
2076 headers[cols++] = gettext_noop("Compression");
2077 if (attstattarget_col >= 0)
2078 headers[cols++] = gettext_noop("Stats target");
2079 if (attdescr_col >= 0)
2080 headers[cols++] = gettext_noop("Description");
2081
2082 Assert(cols <= lengthof(headers));
2083
2084 printTableInit(&cont, &myopt, title.data, cols, numrows);
2085 printTableInitialized = true;
2086
2087 for (i = 0; i < cols; i++)
2088 printTableAddHeader(&cont, headers[i], true, 'l');
2089
2090 /* Generate table cells to be printed */
2091 for (i = 0; i < numrows; i++)
2092 {
2093 /* Column */
2094 printTableAddCell(&cont, PQgetvalue(res, i, attname_col), false, false);
2095
2096 /* Type */
2097 printTableAddCell(&cont, PQgetvalue(res, i, atttype_col), false, false);
2098
2099 /* Collation, Nullable, Default */
2100 if (show_column_details)
2101 {
2102 char *identity;
2103 char *generated;
2104 char *default_str;
2105 bool mustfree = false;
2106
2107 printTableAddCell(&cont, PQgetvalue(res, i, attcoll_col), false, false);
2108
2109 printTableAddCell(&cont,
2110 strcmp(PQgetvalue(res, i, attnotnull_col), "t") == 0 ? "not null" : "",
2111 false, false);
2112
2113 identity = PQgetvalue(res, i, attidentity_col);
2114 generated = PQgetvalue(res, i, attgenerated_col);
2115
2116 if (identity[0] == ATTRIBUTE_IDENTITY_ALWAYS)
2117 default_str = "generated always as identity";
2118 else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
2119 default_str = "generated by default as identity";
2120 else if (generated[0] == ATTRIBUTE_GENERATED_STORED)
2121 {
2122 default_str = psprintf("generated always as (%s) stored",
2123 PQgetvalue(res, i, attrdef_col));
2124 mustfree = true;
2125 }
2126 else if (generated[0] == ATTRIBUTE_GENERATED_VIRTUAL)
2127 {
2128 default_str = psprintf("generated always as (%s)",
2129 PQgetvalue(res, i, attrdef_col));
2130 mustfree = true;
2131 }
2132 else
2133 default_str = PQgetvalue(res, i, attrdef_col);
2134
2135 printTableAddCell(&cont, default_str, false, mustfree);
2136 }
2137
2138 /* Info for index columns */
2139 if (isindexkey_col >= 0)
2140 printTableAddCell(&cont, PQgetvalue(res, i, isindexkey_col), true, false);
2141 if (indexdef_col >= 0)
2142 printTableAddCell(&cont, PQgetvalue(res, i, indexdef_col), false, false);
2143
2144 /* FDW options for foreign table columns */
2145 if (fdwopts_col >= 0)
2146 printTableAddCell(&cont, PQgetvalue(res, i, fdwopts_col), false, false);
2147
2148 /* Storage mode, if relevant */
2149 if (attstorage_col >= 0)
2150 {
2151 char *storage = PQgetvalue(res, i, attstorage_col);
2152
2153 /* these strings are literal in our syntax, so not translated. */
2154 printTableAddCell(&cont, (storage[0] == TYPSTORAGE_PLAIN ? "plain" :
2155 (storage[0] == TYPSTORAGE_MAIN ? "main" :
2156 (storage[0] == TYPSTORAGE_EXTENDED ? "extended" :
2157 (storage[0] == TYPSTORAGE_EXTERNAL ? "external" :
2158 "???")))),
2159 false, false);
2160 }
2161
2162 /* Column compression, if relevant */
2163 if (attcompression_col >= 0)
2164 {
2165 char *compression = PQgetvalue(res, i, attcompression_col);
2166
2167 /* these strings are literal in our syntax, so not translated. */
2168 printTableAddCell(&cont, (compression[0] == 'p' ? "pglz" :
2169 (compression[0] == 'l' ? "lz4" :
2170 (compression[0] == '\0' ? "" :
2171 "???"))),
2172 false, false);
2173 }
2174
2175 /* Statistics target, if the relkind supports this feature */
2176 if (attstattarget_col >= 0)
2177 printTableAddCell(&cont, PQgetvalue(res, i, attstattarget_col),
2178 false, false);
2179
2180 /* Column comments, if the relkind supports this feature */
2181 if (attdescr_col >= 0)
2182 printTableAddCell(&cont, PQgetvalue(res, i, attdescr_col),
2183 false, false);
2184 }
2185
2186 /* Make footers */
2187
2188 if (tableinfo.ispartition)
2189 {
2190 /* Footer information for a partition child table */
2191 PGresult *result;
2192
2194 "SELECT inhparent::pg_catalog.regclass,\n"
2195 " pg_catalog.pg_get_expr(c.relpartbound, c.oid),\n ");
2196
2198 pset.sversion >= 140000 ? "inhdetachpending" :
2199 "false as inhdetachpending");
2200
2201 /* If verbose, also request the partition constraint definition */
2202 if (verbose)
2204 ",\n pg_catalog.pg_get_partition_constraintdef(c.oid)");
2206 "\nFROM pg_catalog.pg_class c"
2207 " JOIN pg_catalog.pg_inherits i"
2208 " ON c.oid = inhrelid"
2209 "\nWHERE c.oid = '%s';", oid);
2210 result = PSQLexec(buf.data);
2211 if (!result)
2212 goto error_return;
2213
2214 if (PQntuples(result) > 0)
2215 {
2216 char *parent_name = PQgetvalue(result, 0, 0);
2217 char *partdef = PQgetvalue(result, 0, 1);
2218 char *detached = PQgetvalue(result, 0, 2);
2219
2220 printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s%s"), parent_name,
2221 partdef,
2222 strcmp(detached, "t") == 0 ? " DETACH PENDING" : "");
2224
2225 if (verbose)
2226 {
2227 char *partconstraintdef = NULL;
2228
2229 if (!PQgetisnull(result, 0, 3))
2230 partconstraintdef = PQgetvalue(result, 0, 3);
2231 /* If there isn't any constraint, show that explicitly */
2232 if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
2233 printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
2234 else
2235 printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"),
2236 partconstraintdef);
2238 }
2239 }
2240 PQclear(result);
2241 }
2242
2243 if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2244 {
2245 /* Footer information for a partitioned table (partitioning parent) */
2246 PGresult *result;
2247
2249 "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
2250 oid);
2251 result = PSQLexec(buf.data);
2252 if (!result)
2253 goto error_return;
2254
2255 if (PQntuples(result) == 1)
2256 {
2257 char *partkeydef = PQgetvalue(result, 0, 0);
2258
2259 printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2261 }
2262 PQclear(result);
2263 }
2264
2265 if (tableinfo.relkind == RELKIND_TOASTVALUE)
2266 {
2267 /* For a TOAST table, print name of owning table */
2268 PGresult *result;
2269
2271 "SELECT n.nspname, c.relname\n"
2272 "FROM pg_catalog.pg_class c"
2273 " JOIN pg_catalog.pg_namespace n"
2274 " ON n.oid = c.relnamespace\n"
2275 "WHERE reltoastrelid = '%s';", oid);
2276 result = PSQLexec(buf.data);
2277 if (!result)
2278 goto error_return;
2279
2280 if (PQntuples(result) == 1)
2281 {
2282 char *schemaname = PQgetvalue(result, 0, 0);
2283 char *relname = PQgetvalue(result, 0, 1);
2284
2285 printfPQExpBuffer(&tmpbuf, _("Owning table: \"%s.%s\""),
2286 schemaname, relname);
2288 }
2289 PQclear(result);
2290 }
2291
2292 if (tableinfo.relkind == RELKIND_INDEX ||
2293 tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
2294 {
2295 /* Footer information about an index */
2296 PGresult *result;
2297
2299 "SELECT i.indisunique, i.indisprimary, i.indisclustered, "
2300 "i.indisvalid,\n"
2301 " (NOT i.indimmediate) AND "
2302 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2303 "WHERE conrelid = i.indrelid AND "
2304 "conindid = i.indexrelid AND "
2305 "contype IN (" CppAsString2(CONSTRAINT_PRIMARY) ","
2306 CppAsString2(CONSTRAINT_UNIQUE) ","
2307 CppAsString2(CONSTRAINT_EXCLUSION) ") AND "
2308 "condeferrable) AS condeferrable,\n"
2309 " (NOT i.indimmediate) AND "
2310 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2311 "WHERE conrelid = i.indrelid AND "
2312 "conindid = i.indexrelid AND "
2313 "contype IN (" CppAsString2(CONSTRAINT_PRIMARY) ","
2314 CppAsString2(CONSTRAINT_UNIQUE) ","
2315 CppAsString2(CONSTRAINT_EXCLUSION) ") AND "
2316 "condeferred) AS condeferred,\n");
2317
2318 if (pset.sversion >= 90400)
2319 appendPQExpBufferStr(&buf, "i.indisreplident,\n");
2320 else
2321 appendPQExpBufferStr(&buf, "false AS indisreplident,\n");
2322
2323 if (pset.sversion >= 150000)
2324 appendPQExpBufferStr(&buf, "i.indnullsnotdistinct,\n");
2325 else
2326 appendPQExpBufferStr(&buf, "false AS indnullsnotdistinct,\n");
2327
2328 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
2329 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
2330 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
2331 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
2332 "AND i.indrelid = c2.oid;",
2333 oid);
2334
2335 result = PSQLexec(buf.data);
2336 if (!result)
2337 goto error_return;
2338 else if (PQntuples(result) != 1)
2339 {
2340 PQclear(result);
2341 goto error_return;
2342 }
2343 else
2344 {
2345 char *indisunique = PQgetvalue(result, 0, 0);
2346 char *indisprimary = PQgetvalue(result, 0, 1);
2347 char *indisclustered = PQgetvalue(result, 0, 2);
2348 char *indisvalid = PQgetvalue(result, 0, 3);
2349 char *deferrable = PQgetvalue(result, 0, 4);
2350 char *deferred = PQgetvalue(result, 0, 5);
2351 char *indisreplident = PQgetvalue(result, 0, 6);
2352 char *indnullsnotdistinct = PQgetvalue(result, 0, 7);
2353 char *indamname = PQgetvalue(result, 0, 8);
2354 char *indtable = PQgetvalue(result, 0, 9);
2355 char *indpred = PQgetvalue(result, 0, 10);
2356
2357 if (strcmp(indisprimary, "t") == 0)
2358 printfPQExpBuffer(&tmpbuf, _("primary key, "));
2359 else if (strcmp(indisunique, "t") == 0)
2360 {
2361 printfPQExpBuffer(&tmpbuf, _("unique"));
2362 if (strcmp(indnullsnotdistinct, "t") == 0)
2363 appendPQExpBufferStr(&tmpbuf, _(" nulls not distinct"));
2364 appendPQExpBufferStr(&tmpbuf, _(", "));
2365 }
2366 else
2368 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
2369
2370 /* we assume here that index and table are in same schema */
2371 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
2372 schemaname, indtable);
2373
2374 if (strlen(indpred))
2375 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
2376
2377 if (strcmp(indisclustered, "t") == 0)
2378 appendPQExpBufferStr(&tmpbuf, _(", clustered"));
2379
2380 if (strcmp(indisvalid, "t") != 0)
2381 appendPQExpBufferStr(&tmpbuf, _(", invalid"));
2382
2383 if (strcmp(deferrable, "t") == 0)
2384 appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
2385
2386 if (strcmp(deferred, "t") == 0)
2387 appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
2388
2389 if (strcmp(indisreplident, "t") == 0)
2390 appendPQExpBufferStr(&tmpbuf, _(", replica identity"));
2391
2393
2394 /*
2395 * If it's a partitioned index, we'll print the tablespace below
2396 */
2397 if (tableinfo.relkind == RELKIND_INDEX)
2398 add_tablespace_footer(&cont, tableinfo.relkind,
2399 tableinfo.tablespace, true);
2400 }
2401
2402 PQclear(result);
2403 }
2404 /* If you add relkinds here, see also "Finish printing..." stanza below */
2405 else if (tableinfo.relkind == RELKIND_RELATION ||
2406 tableinfo.relkind == RELKIND_MATVIEW ||
2407 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2408 tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
2409 tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
2410 tableinfo.relkind == RELKIND_TOASTVALUE)
2411 {
2412 /* Footer information about a table */
2413 PGresult *result = NULL;
2414 int tuples = 0;
2415
2416 /* print indexes */
2417 if (tableinfo.hasindex)
2418 {
2420 "SELECT c2.relname, i.indisprimary, i.indisunique, "
2421 "i.indisclustered, i.indisvalid, "
2422 "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "
2423 "pg_catalog.pg_get_constraintdef(con.oid, true), "
2424 "contype, condeferrable, condeferred");
2425 if (pset.sversion >= 90400)
2426 appendPQExpBufferStr(&buf, ", i.indisreplident");
2427 else
2428 appendPQExpBufferStr(&buf, ", false AS indisreplident");
2429 appendPQExpBufferStr(&buf, ", c2.reltablespace");
2430 if (pset.sversion >= 180000)
2431 appendPQExpBufferStr(&buf, ", con.conperiod");
2432 else
2433 appendPQExpBufferStr(&buf, ", false AS conperiod");
2435 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
2436 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ("
2437 CppAsString2(CONSTRAINT_PRIMARY) ","
2438 CppAsString2(CONSTRAINT_UNIQUE) ","
2439 CppAsString2(CONSTRAINT_EXCLUSION) "))\n"
2440 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2441 "ORDER BY i.indisprimary DESC, c2.relname;",
2442 oid);
2443 result = PSQLexec(buf.data);
2444 if (!result)
2445 goto error_return;
2446 else
2447 tuples = PQntuples(result);
2448
2449 if (tuples > 0)
2450 {
2451 printTableAddFooter(&cont, _("Indexes:"));
2452 for (i = 0; i < tuples; i++)
2453 {
2454 /* untranslated index name */
2455 printfPQExpBuffer(&buf, " \"%s\"",
2456 PQgetvalue(result, i, 0));
2457
2458 /*
2459 * If exclusion constraint or PK/UNIQUE constraint WITHOUT
2460 * OVERLAPS, print the constraintdef
2461 */
2462 if (strcmp(PQgetvalue(result, i, 7), "x") == 0 ||
2463 strcmp(PQgetvalue(result, i, 12), "t") == 0)
2464 {
2465 appendPQExpBuffer(&buf, " %s",
2466 PQgetvalue(result, i, 6));
2467 }
2468 else
2469 {
2470 const char *indexdef;
2471 const char *usingpos;
2472
2473 /* Label as primary key or unique (but not both) */
2474 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2475 appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2476 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2477 {
2478 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2479 appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2480 else
2481 appendPQExpBufferStr(&buf, " UNIQUE,");
2482 }
2483
2484 /* Everything after "USING" is echoed verbatim */
2485 indexdef = PQgetvalue(result, i, 5);
2486 usingpos = strstr(indexdef, " USING ");
2487 if (usingpos)
2488 indexdef = usingpos + 7;
2489 appendPQExpBuffer(&buf, " %s", indexdef);
2490
2491 /* Need these for deferrable PK/UNIQUE indexes */
2492 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2493 appendPQExpBufferStr(&buf, " DEFERRABLE");
2494
2495 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2496 appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2497 }
2498
2499 /* Add these for all cases */
2500 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2501 appendPQExpBufferStr(&buf, " CLUSTER");
2502
2503 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2504 appendPQExpBufferStr(&buf, " INVALID");
2505
2506 if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2507 appendPQExpBufferStr(&buf, " REPLICA IDENTITY");
2508
2509 printTableAddFooter(&cont, buf.data);
2510
2511 /* Print tablespace of the index on the same line */
2512 add_tablespace_footer(&cont, RELKIND_INDEX,
2513 atooid(PQgetvalue(result, i, 11)),
2514 false);
2515 }
2516 }
2517 PQclear(result);
2518 }
2519
2520 /* print table (and column) check constraints */
2521 if (tableinfo.checks)
2522 {
2524 "SELECT r.conname, "
2525 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2526 "FROM pg_catalog.pg_constraint r\n"
2527 "WHERE r.conrelid = '%s' "
2528 "AND r.contype = " CppAsString2(CONSTRAINT_CHECK) "\n"
2529 "ORDER BY 1;",
2530 oid);
2531 result = PSQLexec(buf.data);
2532 if (!result)
2533 goto error_return;
2534 else
2535 tuples = PQntuples(result);
2536
2537 if (tuples > 0)
2538 {
2539 printTableAddFooter(&cont, _("Check constraints:"));
2540 for (i = 0; i < tuples; i++)
2541 {
2542 /* untranslated constraint name and def */
2543 printfPQExpBuffer(&buf, " \"%s\" %s",
2544 PQgetvalue(result, i, 0),
2545 PQgetvalue(result, i, 1));
2546
2547 printTableAddFooter(&cont, buf.data);
2548 }
2549 }
2550 PQclear(result);
2551 }
2552
2553 /*
2554 * Print foreign-key constraints (there are none if no triggers,
2555 * except if the table is partitioned, in which case the triggers
2556 * appear in the partitions)
2557 */
2558 if (tableinfo.hastriggers ||
2559 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2560 {
2561 if (pset.sversion >= 120000 &&
2562 (tableinfo.ispartition || tableinfo.relkind == RELKIND_PARTITIONED_TABLE))
2563 {
2564 /*
2565 * Put the constraints defined in this table first, followed
2566 * by the constraints defined in ancestor partitioned tables.
2567 */
2569 "SELECT conrelid = '%s'::pg_catalog.regclass AS sametable,\n"
2570 " conname,\n"
2571 " pg_catalog.pg_get_constraintdef(oid, true) AS condef,\n"
2572 " conrelid::pg_catalog.regclass AS ontable\n"
2573 " FROM pg_catalog.pg_constraint,\n"
2574 " pg_catalog.pg_partition_ancestors('%s')\n"
2575 " WHERE conrelid = relid AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2576 "ORDER BY sametable DESC, conname;",
2577 oid, oid);
2578 }
2579 else
2580 {
2582 "SELECT true as sametable, conname,\n"
2583 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef,\n"
2584 " conrelid::pg_catalog.regclass AS ontable\n"
2585 "FROM pg_catalog.pg_constraint r\n"
2586 "WHERE r.conrelid = '%s' AND r.contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n",
2587 oid);
2588
2589 if (pset.sversion >= 120000)
2590 appendPQExpBufferStr(&buf, " AND conparentid = 0\n");
2591 appendPQExpBufferStr(&buf, "ORDER BY conname");
2592 }
2593
2594 result = PSQLexec(buf.data);
2595 if (!result)
2596 goto error_return;
2597 else
2598 tuples = PQntuples(result);
2599
2600 if (tuples > 0)
2601 {
2602 int i_sametable = PQfnumber(result, "sametable"),
2603 i_conname = PQfnumber(result, "conname"),
2604 i_condef = PQfnumber(result, "condef"),
2605 i_ontable = PQfnumber(result, "ontable");
2606
2607 printTableAddFooter(&cont, _("Foreign-key constraints:"));
2608 for (i = 0; i < tuples; i++)
2609 {
2610 /*
2611 * Print untranslated constraint name and definition. Use
2612 * a "TABLE tab" prefix when the constraint is defined in
2613 * a parent partitioned table.
2614 */
2615 if (strcmp(PQgetvalue(result, i, i_sametable), "f") == 0)
2616 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2617 PQgetvalue(result, i, i_ontable),
2618 PQgetvalue(result, i, i_conname),
2619 PQgetvalue(result, i, i_condef));
2620 else
2621 printfPQExpBuffer(&buf, " \"%s\" %s",
2622 PQgetvalue(result, i, i_conname),
2623 PQgetvalue(result, i, i_condef));
2624
2625 printTableAddFooter(&cont, buf.data);
2626 }
2627 }
2628 PQclear(result);
2629 }
2630
2631 /* print incoming foreign-key references */
2632 if (tableinfo.hastriggers ||
2633 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2634 {
2635 if (pset.sversion >= 120000)
2636 {
2638 "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2639 " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2640 " FROM pg_catalog.pg_constraint c\n"
2641 " WHERE confrelid IN (SELECT pg_catalog.pg_partition_ancestors('%s')\n"
2642 " UNION ALL VALUES ('%s'::pg_catalog.regclass))\n"
2643 " AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2644 "ORDER BY conname;",
2645 oid, oid);
2646 }
2647 else
2648 {
2650 "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2651 " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2652 " FROM pg_catalog.pg_constraint\n"
2653 " WHERE confrelid = %s AND contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n"
2654 "ORDER BY conname;",
2655 oid);
2656 }
2657
2658 result = PSQLexec(buf.data);
2659 if (!result)
2660 goto error_return;
2661 else
2662 tuples = PQntuples(result);
2663
2664 if (tuples > 0)
2665 {
2666 int i_conname = PQfnumber(result, "conname"),
2667 i_ontable = PQfnumber(result, "ontable"),
2668 i_condef = PQfnumber(result, "condef");
2669
2670 printTableAddFooter(&cont, _("Referenced by:"));
2671 for (i = 0; i < tuples; i++)
2672 {
2673 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2674 PQgetvalue(result, i, i_ontable),
2675 PQgetvalue(result, i, i_conname),
2676 PQgetvalue(result, i, i_condef));
2677
2678 printTableAddFooter(&cont, buf.data);
2679 }
2680 }
2681 PQclear(result);
2682 }
2683
2684 /* print any row-level policies */
2685 if (pset.sversion >= 90500)
2686 {
2687 printfPQExpBuffer(&buf, "SELECT pol.polname,");
2688 if (pset.sversion >= 100000)
2690 " pol.polpermissive,\n");
2691 else
2693 " 't' as polpermissive,\n");
2695 " CASE WHEN pol.polroles = '{0}' THEN NULL ELSE pg_catalog.array_to_string(array(select rolname from pg_catalog.pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2696 " pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2697 " pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2698 " CASE pol.polcmd\n"
2699 " WHEN 'r' THEN 'SELECT'\n"
2700 " WHEN 'a' THEN 'INSERT'\n"
2701 " WHEN 'w' THEN 'UPDATE'\n"
2702 " WHEN 'd' THEN 'DELETE'\n"
2703 " END AS cmd\n"
2704 "FROM pg_catalog.pg_policy pol\n"
2705 "WHERE pol.polrelid = '%s' ORDER BY 1;",
2706 oid);
2707
2708 result = PSQLexec(buf.data);
2709 if (!result)
2710 goto error_return;
2711 else
2712 tuples = PQntuples(result);
2713
2714 /*
2715 * Handle cases where RLS is enabled and there are policies, or
2716 * there aren't policies, or RLS isn't enabled but there are
2717 * policies
2718 */
2719 if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2720 printTableAddFooter(&cont, _("Policies:"));
2721
2722 if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2723 printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2724
2725 if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2726 printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2727
2728 if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2729 printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2730
2731 if (!tableinfo.rowsecurity && tuples > 0)
2732 printTableAddFooter(&cont, _("Policies (row security disabled):"));
2733
2734 /* Might be an empty set - that's ok */
2735 for (i = 0; i < tuples; i++)
2736 {
2737 printfPQExpBuffer(&buf, " POLICY \"%s\"",
2738 PQgetvalue(result, i, 0));
2739
2740 if (*(PQgetvalue(result, i, 1)) == 'f')
2741 appendPQExpBufferStr(&buf, " AS RESTRICTIVE");
2742
2743 if (!PQgetisnull(result, i, 5))
2744 appendPQExpBuffer(&buf, " FOR %s",
2745 PQgetvalue(result, i, 5));
2746
2747 if (!PQgetisnull(result, i, 2))
2748 {
2749 appendPQExpBuffer(&buf, "\n TO %s",
2750 PQgetvalue(result, i, 2));
2751 }
2752
2753 if (!PQgetisnull(result, i, 3))
2754 appendPQExpBuffer(&buf, "\n USING (%s)",
2755 PQgetvalue(result, i, 3));
2756
2757 if (!PQgetisnull(result, i, 4))
2758 appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2759 PQgetvalue(result, i, 4));
2760
2761 printTableAddFooter(&cont, buf.data);
2762 }
2763 PQclear(result);
2764 }
2765
2766 /* print any extended statistics */
2767 if (pset.sversion >= 140000)
2768 {
2770 "SELECT oid, "
2771 "stxrelid::pg_catalog.regclass, "
2772 "stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS nsp, "
2773 "stxname,\n"
2774 "pg_catalog.pg_get_statisticsobjdef_columns(oid) AS columns,\n"
2775 " " CppAsString2(STATS_EXT_NDISTINCT) " = any(stxkind) AS ndist_enabled,\n"
2776 " " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(stxkind) AS deps_enabled,\n"
2777 " " CppAsString2(STATS_EXT_MCV) " = any(stxkind) AS mcv_enabled,\n"
2778 "stxstattarget\n"
2779 "FROM pg_catalog.pg_statistic_ext\n"
2780 "WHERE stxrelid = '%s'\n"
2781 "ORDER BY nsp, stxname;",
2782 oid);
2783
2784 result = PSQLexec(buf.data);
2785 if (!result)
2786 goto error_return;
2787 else
2788 tuples = PQntuples(result);
2789
2790 if (tuples > 0)
2791 {
2792 printTableAddFooter(&cont, _("Statistics objects:"));
2793
2794 for (i = 0; i < tuples; i++)
2795 {
2796 bool gotone = false;
2797 bool has_ndistinct;
2798 bool has_dependencies;
2799 bool has_mcv;
2800 bool has_all;
2801 bool has_some;
2802
2803 has_ndistinct = (strcmp(PQgetvalue(result, i, 5), "t") == 0);
2804 has_dependencies = (strcmp(PQgetvalue(result, i, 6), "t") == 0);
2805 has_mcv = (strcmp(PQgetvalue(result, i, 7), "t") == 0);
2806
2807 printfPQExpBuffer(&buf, " ");
2808
2809 /* statistics object name (qualified with namespace) */
2810 appendPQExpBuffer(&buf, "\"%s.%s\"",
2811 PQgetvalue(result, i, 2),
2812 PQgetvalue(result, i, 3));
2813
2814 /*
2815 * When printing kinds we ignore expression statistics,
2816 * which are used only internally and can't be specified
2817 * by user. We don't print the kinds when none are
2818 * specified (in which case it has to be statistics on a
2819 * single expr) or when all are specified (in which case
2820 * we assume it's expanded by CREATE STATISTICS).
2821 */
2822 has_all = (has_ndistinct && has_dependencies && has_mcv);
2823 has_some = (has_ndistinct || has_dependencies || has_mcv);
2824
2825 if (has_some && !has_all)
2826 {
2827 appendPQExpBufferStr(&buf, " (");
2828
2829 /* options */
2830 if (has_ndistinct)
2831 {
2832 appendPQExpBufferStr(&buf, "ndistinct");
2833 gotone = true;
2834 }
2835
2836 if (has_dependencies)
2837 {
2838 appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2839 gotone = true;
2840 }
2841
2842 if (has_mcv)
2843 {
2844 appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2845 }
2846
2848 }
2849
2850 appendPQExpBuffer(&buf, " ON %s FROM %s",
2851 PQgetvalue(result, i, 4),
2852 PQgetvalue(result, i, 1));
2853
2854 /* Show the stats target if it's not default */
2855 if (!PQgetisnull(result, i, 8) &&
2856 strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2857 appendPQExpBuffer(&buf, "; STATISTICS %s",
2858 PQgetvalue(result, i, 8));
2859
2860 printTableAddFooter(&cont, buf.data);
2861 }
2862 }
2863 PQclear(result);
2864 }
2865 else if (pset.sversion >= 100000)
2866 {
2868 "SELECT oid, "
2869 "stxrelid::pg_catalog.regclass, "
2870 "stxnamespace::pg_catalog.regnamespace AS nsp, "
2871 "stxname,\n"
2872 " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')\n"
2873 " FROM pg_catalog.unnest(stxkeys) s(attnum)\n"
2874 " JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND\n"
2875 " a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
2876 " " CppAsString2(STATS_EXT_NDISTINCT) " = any(stxkind) AS ndist_enabled,\n"
2877 " " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(stxkind) AS deps_enabled,\n"
2878 " " CppAsString2(STATS_EXT_MCV) " = any(stxkind) AS mcv_enabled,\n");
2879
2880 if (pset.sversion >= 130000)
2881 appendPQExpBufferStr(&buf, " stxstattarget\n");
2882 else
2883 appendPQExpBufferStr(&buf, " -1 AS stxstattarget\n");
2884 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_statistic_ext\n"
2885 "WHERE stxrelid = '%s'\n"
2886 "ORDER BY 1;",
2887 oid);
2888
2889 result = PSQLexec(buf.data);
2890 if (!result)
2891 goto error_return;
2892 else
2893 tuples = PQntuples(result);
2894
2895 if (tuples > 0)
2896 {
2897 printTableAddFooter(&cont, _("Statistics objects:"));
2898
2899 for (i = 0; i < tuples; i++)
2900 {
2901 bool gotone = false;
2902
2903 printfPQExpBuffer(&buf, " ");
2904
2905 /* statistics object name (qualified with namespace) */
2906 appendPQExpBuffer(&buf, "\"%s.%s\" (",
2907 PQgetvalue(result, i, 2),
2908 PQgetvalue(result, i, 3));
2909
2910 /* options */
2911 if (strcmp(PQgetvalue(result, i, 5), "t") == 0)
2912 {
2913 appendPQExpBufferStr(&buf, "ndistinct");
2914 gotone = true;
2915 }
2916
2917 if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
2918 {
2919 appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2920 gotone = true;
2921 }
2922
2923 if (strcmp(PQgetvalue(result, i, 7), "t") == 0)
2924 {
2925 appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2926 }
2927
2928 appendPQExpBuffer(&buf, ") ON %s FROM %s",
2929 PQgetvalue(result, i, 4),
2930 PQgetvalue(result, i, 1));
2931
2932 /* Show the stats target if it's not default */
2933 if (strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2934 appendPQExpBuffer(&buf, "; STATISTICS %s",
2935 PQgetvalue(result, i, 8));
2936
2937 printTableAddFooter(&cont, buf.data);
2938 }
2939 }
2940 PQclear(result);
2941 }
2942
2943 /* print rules */
2944 if (tableinfo.hasrules && tableinfo.relkind != RELKIND_MATVIEW)
2945 {
2947 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2948 "ev_enabled\n"
2949 "FROM pg_catalog.pg_rewrite r\n"
2950 "WHERE r.ev_class = '%s' ORDER BY 1;",
2951 oid);
2952 result = PSQLexec(buf.data);
2953 if (!result)
2954 goto error_return;
2955 else
2956 tuples = PQntuples(result);
2957
2958 if (tuples > 0)
2959 {
2960 bool have_heading;
2961 int category;
2962
2963 for (category = 0; category < 4; category++)
2964 {
2965 have_heading = false;
2966
2967 for (i = 0; i < tuples; i++)
2968 {
2969 const char *ruledef;
2970 bool list_rule = false;
2971
2972 switch (category)
2973 {
2974 case 0:
2975 if (*PQgetvalue(result, i, 2) == 'O')
2976 list_rule = true;
2977 break;
2978 case 1:
2979 if (*PQgetvalue(result, i, 2) == 'D')
2980 list_rule = true;
2981 break;
2982 case 2:
2983 if (*PQgetvalue(result, i, 2) == 'A')
2984 list_rule = true;
2985 break;
2986 case 3:
2987 if (*PQgetvalue(result, i, 2) == 'R')
2988 list_rule = true;
2989 break;
2990 }
2991 if (!list_rule)
2992 continue;
2993
2994 if (!have_heading)
2995 {
2996 switch (category)
2997 {
2998 case 0:
2999 printfPQExpBuffer(&buf, _("Rules:"));
3000 break;
3001 case 1:
3002 printfPQExpBuffer(&buf, _("Disabled rules:"));
3003 break;
3004 case 2:
3005 printfPQExpBuffer(&buf, _("Rules firing always:"));
3006 break;
3007 case 3:
3008 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
3009 break;
3010 }
3011 printTableAddFooter(&cont, buf.data);
3012 have_heading = true;
3013 }
3014
3015 /* Everything after "CREATE RULE" is echoed verbatim */
3016 ruledef = PQgetvalue(result, i, 1);
3017 ruledef += 12;
3018 printfPQExpBuffer(&buf, " %s", ruledef);
3019 printTableAddFooter(&cont, buf.data);
3020 }
3021 }
3022 }
3023 PQclear(result);
3024 }
3025
3026 /* print any publications */
3027 if (pset.sversion >= 100000)
3028 {
3029 if (pset.sversion >= 150000)
3030 {
3032 "SELECT pubname\n"
3033 " , NULL\n"
3034 " , NULL\n"
3035 "FROM pg_catalog.pg_publication p\n"
3036 " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
3037 " JOIN pg_catalog.pg_class pc ON pc.relnamespace = pn.pnnspid\n"
3038 "WHERE pc.oid ='%s' and pg_catalog.pg_relation_is_publishable('%s')\n"
3039 "UNION\n"
3040 "SELECT pubname\n"
3041 " , pg_get_expr(pr.prqual, c.oid)\n"
3042 " , (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
3043 " (SELECT string_agg(attname, ', ')\n"
3044 " FROM pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
3045 " pg_catalog.pg_attribute\n"
3046 " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
3047 " ELSE NULL END) "
3048 "FROM pg_catalog.pg_publication p\n"
3049 " JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3050 " JOIN pg_catalog.pg_class c ON c.oid = pr.prrelid\n"
3051 "WHERE pr.prrelid = '%s'\n"
3052 "UNION\n"
3053 "SELECT pubname\n"
3054 " , NULL\n"
3055 " , NULL\n"
3056 "FROM pg_catalog.pg_publication p\n"
3057 "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3058 "ORDER BY 1;",
3059 oid, oid, oid, oid);
3060 }
3061 else
3062 {
3064 "SELECT pubname\n"
3065 " , NULL\n"
3066 " , NULL\n"
3067 "FROM pg_catalog.pg_publication p\n"
3068 "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3069 "WHERE pr.prrelid = '%s'\n"
3070 "UNION ALL\n"
3071 "SELECT pubname\n"
3072 " , NULL\n"
3073 " , NULL\n"
3074 "FROM pg_catalog.pg_publication p\n"
3075 "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3076 "ORDER BY 1;",
3077 oid, oid);
3078 }
3079
3080 result = PSQLexec(buf.data);
3081 if (!result)
3082 goto error_return;
3083 else
3084 tuples = PQntuples(result);
3085
3086 if (tuples > 0)
3087 printTableAddFooter(&cont, _("Publications:"));
3088
3089 /* Might be an empty set - that's ok */
3090 for (i = 0; i < tuples; i++)
3091 {
3092 printfPQExpBuffer(&buf, " \"%s\"",
3093 PQgetvalue(result, i, 0));
3094
3095 /* column list (if any) */
3096 if (!PQgetisnull(result, i, 2))
3097 appendPQExpBuffer(&buf, " (%s)",
3098 PQgetvalue(result, i, 2));
3099
3100 /* row filter (if any) */
3101 if (!PQgetisnull(result, i, 1))
3102 appendPQExpBuffer(&buf, " WHERE %s",
3103 PQgetvalue(result, i, 1));
3104
3105 printTableAddFooter(&cont, buf.data);
3106 }
3107 PQclear(result);
3108 }
3109
3110 /*
3111 * If verbose, print NOT NULL constraints.
3112 */
3113 if (verbose)
3114 {
3116 "SELECT c.conname, a.attname, c.connoinherit,\n"
3117 " c.conislocal, c.coninhcount <> 0\n"
3118 "FROM pg_catalog.pg_constraint c JOIN\n"
3119 " pg_catalog.pg_attribute a ON\n"
3120 " (a.attrelid = c.conrelid AND a.attnum = c.conkey[1])\n"
3121 "WHERE c.contype = " CppAsString2(CONSTRAINT_NOTNULL) " AND\n"
3122 " c.conrelid = '%s'::pg_catalog.regclass\n"
3123 "ORDER BY a.attnum",
3124 oid);
3125
3126 result = PSQLexec(buf.data);
3127 if (!result)
3128 goto error_return;
3129 else
3130 tuples = PQntuples(result);
3131
3132 if (tuples > 0)
3133 printTableAddFooter(&cont, _("Not-null constraints:"));
3134
3135 /* Might be an empty set - that's ok */
3136 for (i = 0; i < tuples; i++)
3137 {
3138 bool islocal = PQgetvalue(result, i, 3)[0] == 't';
3139 bool inherited = PQgetvalue(result, i, 4)[0] == 't';
3140
3141 printfPQExpBuffer(&buf, " \"%s\" NOT NULL \"%s\"%s",
3142 PQgetvalue(result, i, 0),
3143 PQgetvalue(result, i, 1),
3144 PQgetvalue(result, i, 2)[0] == 't' ?
3145 " NO INHERIT" :
3146 islocal && inherited ? _(" (local, inherited)") :
3147 inherited ? _(" (inherited)") : "");
3148
3149 printTableAddFooter(&cont, buf.data);
3150 }
3151 PQclear(result);
3152 }
3153 }
3154
3155 /* Get view_def if table is a view or materialized view */
3156 if ((tableinfo.relkind == RELKIND_VIEW ||
3157 tableinfo.relkind == RELKIND_MATVIEW) && verbose)
3158 {
3159 PGresult *result;
3160
3162 "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
3163 oid);
3164 result = PSQLexec(buf.data);
3165 if (!result)
3166 goto error_return;
3167
3168 if (PQntuples(result) > 0)
3169 view_def = pg_strdup(PQgetvalue(result, 0, 0));
3170
3171 PQclear(result);
3172 }
3173
3174 if (view_def)
3175 {
3176 PGresult *result = NULL;
3177
3178 /* Footer information about a view */
3179 printTableAddFooter(&cont, _("View definition:"));
3180 printTableAddFooter(&cont, view_def);
3181
3182 /* print rules */
3183 if (tableinfo.hasrules)
3184 {
3186 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
3187 "FROM pg_catalog.pg_rewrite r\n"
3188 "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
3189 oid);
3190 result = PSQLexec(buf.data);
3191 if (!result)
3192 goto error_return;
3193
3194 if (PQntuples(result) > 0)
3195 {
3196 printTableAddFooter(&cont, _("Rules:"));
3197 for (i = 0; i < PQntuples(result); i++)
3198 {
3199 const char *ruledef;
3200
3201 /* Everything after "CREATE RULE" is echoed verbatim */
3202 ruledef = PQgetvalue(result, i, 1);
3203 ruledef += 12;
3204
3205 printfPQExpBuffer(&buf, " %s", ruledef);
3206 printTableAddFooter(&cont, buf.data);
3207 }
3208 }
3209 PQclear(result);
3210 }
3211 }
3212
3213 /*
3214 * Print triggers next, if any (but only user-defined triggers). This
3215 * could apply to either a table or a view.
3216 */
3217 if (tableinfo.hastriggers)
3218 {
3219 PGresult *result;
3220 int tuples;
3221
3223 "SELECT t.tgname, "
3224 "pg_catalog.pg_get_triggerdef(t.oid, true), "
3225 "t.tgenabled, t.tgisinternal,\n");
3226
3227 /*
3228 * Detect whether each trigger is inherited, and if so, get the name
3229 * of the topmost table it's inherited from. We have no easy way to
3230 * do that pre-v13, for lack of the tgparentid column. Even with
3231 * tgparentid, a straightforward search for the topmost parent would
3232 * require a recursive CTE, which seems unduly expensive. We cheat a
3233 * bit by assuming parent triggers will match by tgname; then, joining
3234 * with pg_partition_ancestors() allows the planner to make use of
3235 * pg_trigger_tgrelid_tgname_index if it wishes. We ensure we find
3236 * the correct topmost parent by stopping at the first-in-partition-
3237 * ancestry-order trigger that has tgparentid = 0. (There might be
3238 * unrelated, non-inherited triggers with the same name further up the
3239 * stack, so this is important.)
3240 */
3241 if (pset.sversion >= 130000)
3243 " CASE WHEN t.tgparentid != 0 THEN\n"
3244 " (SELECT u.tgrelid::pg_catalog.regclass\n"
3245 " FROM pg_catalog.pg_trigger AS u,\n"
3246 " pg_catalog.pg_partition_ancestors(t.tgrelid) WITH ORDINALITY AS a(relid, depth)\n"
3247 " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid\n"
3248 " AND u.tgparentid = 0\n"
3249 " ORDER BY a.depth LIMIT 1)\n"
3250 " END AS parent\n");
3251 else
3252 appendPQExpBufferStr(&buf, " NULL AS parent\n");
3253
3255 "FROM pg_catalog.pg_trigger t\n"
3256 "WHERE t.tgrelid = '%s' AND ",
3257 oid);
3258
3259 /*
3260 * tgisinternal is set true for inherited triggers of partitions in
3261 * servers between v11 and v14, though these must still be shown to
3262 * the user. So we use another property that is true for such
3263 * inherited triggers to avoid them being hidden, which is their
3264 * dependence on another trigger.
3265 */
3266 if (pset.sversion >= 110000 && pset.sversion < 150000)
3267 appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D') \n"
3268 " OR EXISTS (SELECT 1 FROM pg_catalog.pg_depend WHERE objid = t.oid \n"
3269 " AND refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass))");
3270 else
3271 /* display/warn about disabled internal triggers */
3272 appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
3273 appendPQExpBufferStr(&buf, "\nORDER BY 1;");
3274
3275 result = PSQLexec(buf.data);
3276 if (!result)
3277 goto error_return;
3278 else
3279 tuples = PQntuples(result);
3280
3281 if (tuples > 0)
3282 {
3283 bool have_heading;
3284 int category;
3285
3286 /*
3287 * split the output into 4 different categories. Enabled triggers,
3288 * disabled triggers and the two special ALWAYS and REPLICA
3289 * configurations.
3290 */
3291 for (category = 0; category <= 4; category++)
3292 {
3293 have_heading = false;
3294 for (i = 0; i < tuples; i++)
3295 {
3296 bool list_trigger;
3297 const char *tgdef;
3298 const char *usingpos;
3299 const char *tgenabled;
3300 const char *tgisinternal;
3301
3302 /*
3303 * Check if this trigger falls into the current category
3304 */
3305 tgenabled = PQgetvalue(result, i, 2);
3306 tgisinternal = PQgetvalue(result, i, 3);
3307 list_trigger = false;
3308 switch (category)
3309 {
3310 case 0:
3311 if (*tgenabled == 'O' || *tgenabled == 't')
3312 list_trigger = true;
3313 break;
3314 case 1:
3315 if ((*tgenabled == 'D' || *tgenabled == 'f') &&
3316 *tgisinternal == 'f')
3317 list_trigger = true;
3318 break;
3319 case 2:
3320 if ((*tgenabled == 'D' || *tgenabled == 'f') &&
3321 *tgisinternal == 't')
3322 list_trigger = true;
3323 break;
3324 case 3:
3325 if (*tgenabled == 'A')
3326 list_trigger = true;
3327 break;
3328 case 4:
3329 if (*tgenabled == 'R')
3330 list_trigger = true;
3331 break;
3332 }
3333 if (list_trigger == false)
3334 continue;
3335
3336 /* Print the category heading once */
3337 if (have_heading == false)
3338 {
3339 switch (category)
3340 {
3341 case 0:
3342 printfPQExpBuffer(&buf, _("Triggers:"));
3343 break;
3344 case 1:
3345 printfPQExpBuffer(&buf, _("Disabled user triggers:"));
3346 break;
3347 case 2:
3348 printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
3349 break;
3350 case 3:
3351 printfPQExpBuffer(&buf, _("Triggers firing always:"));
3352 break;
3353 case 4:
3354 printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
3355 break;
3356 }
3357 printTableAddFooter(&cont, buf.data);
3358 have_heading = true;
3359 }
3360
3361 /* Everything after "TRIGGER" is echoed verbatim */
3362 tgdef = PQgetvalue(result, i, 1);
3363 usingpos = strstr(tgdef, " TRIGGER ");
3364 if (usingpos)
3365 tgdef = usingpos + 9;
3366
3367 printfPQExpBuffer(&buf, " %s", tgdef);
3368
3369 /* Visually distinguish inherited triggers */
3370 if (!PQgetisnull(result, i, 4))
3371 appendPQExpBuffer(&buf, ", ON TABLE %s",
3372 PQgetvalue(result, i, 4));
3373
3374 printTableAddFooter(&cont, buf.data);
3375 }
3376 }
3377 }
3378 PQclear(result);
3379 }
3380
3381 /*
3382 * Finish printing the footer information about a table.
3383 */
3384 if (tableinfo.relkind == RELKIND_RELATION ||
3385 tableinfo.relkind == RELKIND_MATVIEW ||
3386 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
3387 tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3388 tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
3389 tableinfo.relkind == RELKIND_TOASTVALUE)
3390 {
3391 bool is_partitioned;
3392 PGresult *result;
3393 int tuples;
3394
3395 /* simplify some repeated tests below */
3396 is_partitioned = (tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3397 tableinfo.relkind == RELKIND_PARTITIONED_INDEX);
3398
3399 /* print foreign server name */
3400 if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
3401 {
3402 char *ftoptions;
3403
3404 /* Footer information about foreign table */
3406 "SELECT s.srvname,\n"
3407 " pg_catalog.array_to_string(ARRAY(\n"
3408 " SELECT pg_catalog.quote_ident(option_name)"
3409 " || ' ' || pg_catalog.quote_literal(option_value)\n"
3410 " FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
3411 "FROM pg_catalog.pg_foreign_table f,\n"
3412 " pg_catalog.pg_foreign_server s\n"
3413 "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
3414 oid);
3415 result = PSQLexec(buf.data);
3416 if (!result)
3417 goto error_return;
3418 else if (PQntuples(result) != 1)
3419 {
3420 PQclear(result);
3421 goto error_return;
3422 }
3423
3424 /* Print server name */
3425 printfPQExpBuffer(&buf, _("Server: %s"),
3426 PQgetvalue(result, 0, 0));
3427 printTableAddFooter(&cont, buf.data);
3428
3429 /* Print per-table FDW options, if any */
3430 ftoptions = PQgetvalue(result, 0, 1);
3431 if (ftoptions && ftoptions[0] != '\0')
3432 {
3433 printfPQExpBuffer(&buf, _("FDW options: (%s)"), ftoptions);
3434 printTableAddFooter(&cont, buf.data);
3435 }
3436 PQclear(result);
3437 }
3438
3439 /* print tables inherited from (exclude partitioned parents) */
3441 "SELECT c.oid::pg_catalog.regclass\n"
3442 "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3443 "WHERE c.oid = i.inhparent AND i.inhrelid = '%s'\n"
3444 " AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_TABLE)
3445 " AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_INDEX)
3446 "\nORDER BY inhseqno;",
3447 oid);
3448
3449 result = PSQLexec(buf.data);
3450 if (!result)
3451 goto error_return;
3452 else
3453 {
3454 const char *s = _("Inherits");
3455 int sw = pg_wcswidth(s, strlen(s), pset.encoding);
3456
3457 tuples = PQntuples(result);
3458
3459 for (i = 0; i < tuples; i++)
3460 {
3461 if (i == 0)
3462 printfPQExpBuffer(&buf, "%s: %s",
3463 s, PQgetvalue(result, i, 0));
3464 else
3465 printfPQExpBuffer(&buf, "%*s %s",
3466 sw, "", PQgetvalue(result, i, 0));
3467 if (i < tuples - 1)
3469
3470 printTableAddFooter(&cont, buf.data);
3471 }
3472
3473 PQclear(result);
3474 }
3475
3476 /* print child tables (with additional info if partitions) */
3477 if (pset.sversion >= 140000)
3479 "SELECT c.oid::pg_catalog.regclass, c.relkind,"
3480 " inhdetachpending,"
3481 " pg_catalog.pg_get_expr(c.relpartbound, c.oid)\n"
3482 "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3483 "WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3484 "ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
3485 " c.oid::pg_catalog.regclass::pg_catalog.text;",
3486 oid);
3487 else if (pset.sversion >= 100000)
3489 "SELECT c.oid::pg_catalog.regclass, c.relkind,"
3490 " false AS inhdetachpending,"
3491 " pg_catalog.pg_get_expr(c.relpartbound, c.oid)\n"
3492 "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3493 "WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3494 "ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
3495 " c.oid::pg_catalog.regclass::pg_catalog.text;",
3496 oid);
3497 else
3499 "SELECT c.oid::pg_catalog.regclass, c.relkind,"
3500 " false AS inhdetachpending, NULL\n"
3501 "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3502 "WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3503 "ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;",
3504 oid);
3505
3506 result = PSQLexec(buf.data);
3507 if (!result)
3508 goto error_return;
3509 tuples = PQntuples(result);
3510
3511 /*
3512 * For a partitioned table with no partitions, always print the number
3513 * of partitions as zero, even when verbose output is expected.
3514 * Otherwise, we will not print "Partitions" section for a partitioned
3515 * table without any partitions.
3516 */
3517 if (is_partitioned && tuples == 0)
3518 {
3519 printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
3520 printTableAddFooter(&cont, buf.data);
3521 }
3522 else if (!verbose)
3523 {
3524 /* print the number of child tables, if any */
3525 if (tuples > 0)
3526 {
3527 if (is_partitioned)
3528 printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
3529 else
3530 printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
3531 printTableAddFooter(&cont, buf.data);
3532 }
3533 }
3534 else
3535 {
3536 /* display the list of child tables */
3537 const char *ct = is_partitioned ? _("Partitions") : _("Child tables");
3538 int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
3539
3540 for (i = 0; i < tuples; i++)
3541 {
3542 char child_relkind = *PQgetvalue(result, i, 1);
3543
3544 if (i == 0)
3545 printfPQExpBuffer(&buf, "%s: %s",
3546 ct, PQgetvalue(result, i, 0));
3547 else
3548 printfPQExpBuffer(&buf, "%*s %s",
3549 ctw, "", PQgetvalue(result, i, 0));
3550 if (!PQgetisnull(result, i, 3))
3551 appendPQExpBuffer(&buf, " %s", PQgetvalue(result, i, 3));
3552 if (child_relkind == RELKIND_PARTITIONED_TABLE ||
3553 child_relkind == RELKIND_PARTITIONED_INDEX)
3554 appendPQExpBufferStr(&buf, ", PARTITIONED");
3555 else if (child_relkind == RELKIND_FOREIGN_TABLE)
3556 appendPQExpBufferStr(&buf, ", FOREIGN");
3557 if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
3558 appendPQExpBufferStr(&buf, " (DETACH PENDING)");
3559 if (i < tuples - 1)
3561
3562 printTableAddFooter(&cont, buf.data);
3563 }
3564 }
3565 PQclear(result);
3566
3567 /* Table type */
3568 if (tableinfo.reloftype)
3569 {
3570 printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
3571 printTableAddFooter(&cont, buf.data);
3572 }
3573
3574 if (verbose &&
3575 (tableinfo.relkind == RELKIND_RELATION ||
3576 tableinfo.relkind == RELKIND_MATVIEW) &&
3577
3578 /*
3579 * No need to display default values; we already display a REPLICA
3580 * IDENTITY marker on indexes.
3581 */
3582 tableinfo.relreplident != REPLICA_IDENTITY_INDEX &&
3583 ((strcmp(schemaname, "pg_catalog") != 0 &&
3584 tableinfo.relreplident != REPLICA_IDENTITY_DEFAULT) ||
3585 (strcmp(schemaname, "pg_catalog") == 0 &&
3586 tableinfo.relreplident != REPLICA_IDENTITY_NOTHING)))
3587 {
3588 const char *s = _("Replica Identity");
3589
3590 printfPQExpBuffer(&buf, "%s: %s",
3591 s,
3592 tableinfo.relreplident == REPLICA_IDENTITY_FULL ? "FULL" :
3593 tableinfo.relreplident == REPLICA_IDENTITY_DEFAULT ? "NOTHING" :
3594 "???");
3595
3596 printTableAddFooter(&cont, buf.data);
3597 }
3598
3599 /* OIDs, if verbose and not a materialized view */
3600 if (verbose && tableinfo.relkind != RELKIND_MATVIEW && tableinfo.hasoids)
3601 printTableAddFooter(&cont, _("Has OIDs: yes"));
3602
3603 /* Tablespace info */
3604 add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
3605 true);
3606
3607 /* Access method info */
3608 if (verbose && tableinfo.relam != NULL && !pset.hide_tableam)
3609 {
3610 printfPQExpBuffer(&buf, _("Access method: %s"), tableinfo.relam);
3611 printTableAddFooter(&cont, buf.data);
3612 }
3613 }
3614
3615 /* reloptions, if verbose */
3616 if (verbose &&
3617 tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
3618 {
3619 const char *t = _("Options");
3620
3621 printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
3622 printTableAddFooter(&cont, buf.data);
3623 }
3624
3625 printTable(&cont, pset.queryFout, false, pset.logfile);
3626
3627 retval = true;
3628
3629error_return:
3630
3631 /* clean up */
3632 if (printTableInitialized)
3633 printTableCleanup(&cont);
3635 termPQExpBuffer(&title);
3637
3638 free(view_def);
3639
3640 PQclear(res);
3641
3642 return retval;
3643}
3644
3645/*
3646 * Add a tablespace description to a footer. If 'newline' is true, it is added
3647 * in a new line; otherwise it's appended to the current value of the last
3648 * footer.
3649 */
3650static void
3652 Oid tablespace, const bool newline)
3653{
3654 /* relkinds for which we support tablespaces */
3655 if (relkind == RELKIND_RELATION ||
3656 relkind == RELKIND_MATVIEW ||
3657 relkind == RELKIND_INDEX ||
3658 relkind == RELKIND_PARTITIONED_TABLE ||
3659 relkind == RELKIND_PARTITIONED_INDEX ||
3660 relkind == RELKIND_TOASTVALUE)
3661 {
3662 /*
3663 * We ignore the database default tablespace so that users not using
3664 * tablespaces don't need to know about them.
3665 */
3666 if (tablespace != 0)
3667 {
3668 PGresult *result = NULL;
3670
3673 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3674 "WHERE oid = '%u';", tablespace);
3675 result = PSQLexec(buf.data);
3676 if (!result)
3677 {
3679 return;
3680 }
3681 /* Should always be the case, but.... */
3682 if (PQntuples(result) > 0)
3683 {
3684 if (newline)
3685 {
3686 /* Add the tablespace as a new footer */
3687 printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3688 PQgetvalue(result, 0, 0));
3689 printTableAddFooter(cont, buf.data);
3690 }
3691 else
3692 {
3693 /* Append the tablespace to the latest footer */
3694 printfPQExpBuffer(&buf, "%s", cont->footer->data);
3695
3696 /*-------
3697 translator: before this string there's an index description like
3698 '"foo_pkey" PRIMARY KEY, btree (a)' */
3699 appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3700 PQgetvalue(result, 0, 0));
3701 printTableSetFooter(cont, buf.data);
3702 }
3703 }
3704 PQclear(result);
3706 }
3707 }
3708}
3709
3710/*
3711 * \du or \dg
3712 *
3713 * Describes roles. Any schema portion of the pattern is ignored.
3714 */
3715bool
3716describeRoles(const char *pattern, bool verbose, bool showSystem)
3717{
3719 PGresult *res;
3720 printTableContent cont;
3721 printTableOpt myopt = pset.popt.topt;
3722 int ncols = 2;
3723 int nrows = 0;
3724 int i;
3725 int conns;
3726 const char align = 'l';
3727 char **attr;
3728
3729 myopt.default_footer = false;
3730
3732
3734 "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3735 " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3736 " r.rolconnlimit, r.rolvaliduntil");
3737
3738 if (verbose)
3739 {
3740 appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3741 ncols++;
3742 }
3743 appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3744
3745 if (pset.sversion >= 90500)
3746 {
3747 appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3748 }
3749
3750 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3751
3752 if (!showSystem && !pattern)
3753 appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3754
3755 if (!validateSQLNamePattern(&buf, pattern, false, false,
3756 NULL, "r.rolname", NULL, NULL,
3757 NULL, 1))
3758 {
3760 return false;
3761 }
3762
3763 appendPQExpBufferStr(&buf, "ORDER BY 1;");
3764
3765 res = PSQLexec(buf.data);
3766 if (!res)
3767 return false;
3768
3769 nrows = PQntuples(res);
3770 attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3771
3772 printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3773
3774 printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3775 printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3776
3777 if (verbose)
3778 printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3779
3780 for (i = 0; i < nrows; i++)
3781 {
3782 printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3783
3785 if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3786 add_role_attribute(&buf, _("Superuser"));
3787
3788 if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3789 add_role_attribute(&buf, _("No inheritance"));
3790
3791 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3792 add_role_attribute(&buf, _("Create role"));
3793
3794 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3795 add_role_attribute(&buf, _("Create DB"));
3796
3797 if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3798 add_role_attribute(&buf, _("Cannot login"));
3799
3800 if (strcmp(PQgetvalue(res, i, (verbose ? 9 : 8)), "t") == 0)
3801 add_role_attribute(&buf, _("Replication"));
3802
3803 if (pset.sversion >= 90500)
3804 if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3805 add_role_attribute(&buf, _("Bypass RLS"));
3806
3807 conns = atoi(PQgetvalue(res, i, 6));
3808 if (conns >= 0)
3809 {
3810 if (buf.len > 0)
3811 appendPQExpBufferChar(&buf, '\n');
3812
3813 if (conns == 0)
3814 appendPQExpBufferStr(&buf, _("No connections"));
3815 else
3816 appendPQExpBuffer(&buf, ngettext("%d connection",
3817 "%d connections",
3818 conns),
3819 conns);
3820 }
3821
3822 if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3823 {
3824 if (buf.len > 0)
3825 appendPQExpBufferChar(&buf, '\n');
3826 appendPQExpBufferStr(&buf, _("Password valid until "));
3828 }
3829
3830 attr[i] = pg_strdup(buf.data);
3831
3832 printTableAddCell(&cont, attr[i], false, false);
3833
3834 if (verbose)
3835 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3836 }
3838
3839 printTable(&cont, pset.queryFout, false, pset.logfile);
3840 printTableCleanup(&cont);
3841
3842 for (i = 0; i < nrows; i++)
3843 free(attr[i]);
3844 free(attr);
3845
3846 PQclear(res);
3847 return true;
3848}
3849
3850static void
3852{
3853 if (buf->len > 0)
3855
3857}
3858
3859/*
3860 * \drds
3861 */
3862bool
3863listDbRoleSettings(const char *pattern, const char *pattern2)
3864{
3866 PGresult *res;
3867 printQueryOpt myopt = pset.popt;
3868 bool havewhere;
3869
3871
3872 printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3873 "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3874 "FROM pg_catalog.pg_db_role_setting s\n"
3875 "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3876 "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3877 gettext_noop("Role"),
3878 gettext_noop("Database"),
3879 gettext_noop("Settings"));
3880 if (!validateSQLNamePattern(&buf, pattern, false, false,
3881 NULL, "r.rolname", NULL, NULL, &havewhere, 1))
3882 goto error_return;
3883 if (!validateSQLNamePattern(&buf, pattern2, havewhere, false,
3884 NULL, "d.datname", NULL, NULL,
3885 NULL, 1))
3886 goto error_return;
3887 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3888
3889 res = PSQLexec(buf.data);
3891 if (!res)
3892 return false;
3893
3894 /*
3895 * Most functions in this file are content to print an empty table when
3896 * there are no matching objects. We intentionally deviate from that
3897 * here, but only in !quiet mode, because of the possibility that the user
3898 * is confused about what the two pattern arguments mean.
3899 */
3900 if (PQntuples(res) == 0 && !pset.quiet)
3901 {
3902 if (pattern && pattern2)
3903 pg_log_error("Did not find any settings for role \"%s\" and database \"%s\".",
3904 pattern, pattern2);
3905 else if (pattern)
3906 pg_log_error("Did not find any settings for role \"%s\".",
3907 pattern);
3908 else
3909 pg_log_error("Did not find any settings.");
3910 }
3911 else
3912 {
3913 myopt.title = _("List of settings");
3914 myopt.translate_header = true;
3915
3916 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3917 }
3918
3919 PQclear(res);
3920 return true;
3921
3922error_return:
3924 return false;
3925}
3926
3927/*
3928 * \drg
3929 * Describes role grants.
3930 */
3931bool
3932describeRoleGrants(const char *pattern, bool showSystem)
3933{
3935 PGresult *res;
3936 printQueryOpt myopt = pset.popt;
3937
3940 "SELECT m.rolname AS \"%s\", r.rolname AS \"%s\",\n"
3941 " pg_catalog.concat_ws(', ',\n",
3942 gettext_noop("Role name"),
3943 gettext_noop("Member of"));
3944
3945 if (pset.sversion >= 160000)
3947 " CASE WHEN pam.admin_option THEN 'ADMIN' END,\n"
3948 " CASE WHEN pam.inherit_option THEN 'INHERIT' END,\n"
3949 " CASE WHEN pam.set_option THEN 'SET' END\n");
3950 else
3952 " CASE WHEN pam.admin_option THEN 'ADMIN' END,\n"
3953 " CASE WHEN m.rolinherit THEN 'INHERIT' END,\n"
3954 " 'SET'\n");
3955
3957 " ) AS \"%s\",\n"
3958 " g.rolname AS \"%s\"\n",
3959 gettext_noop("Options"),
3960 gettext_noop("Grantor"));
3961
3963 "FROM pg_catalog.pg_roles m\n"
3964 " JOIN pg_catalog.pg_auth_members pam ON (pam.member = m.oid)\n"
3965 " LEFT JOIN pg_catalog.pg_roles r ON (pam.roleid = r.oid)\n"
3966 " LEFT JOIN pg_catalog.pg_roles g ON (pam.grantor = g.oid)\n");
3967
3968 if (!showSystem && !pattern)
3969 appendPQExpBufferStr(&buf, "WHERE m.rolname !~ '^pg_'\n");
3970
3971 if (!validateSQLNamePattern(&buf, pattern, false, false,
3972 NULL, "m.rolname", NULL, NULL,
3973 NULL, 1))
3974 {
3976 return false;
3977 }
3978
3979 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;\n");
3980
3981 res = PSQLexec(buf.data);
3983 if (!res)
3984 return false;
3985
3986 myopt.title = _("List of role grants");
3987 myopt.translate_header = true;
3988
3989 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3990
3991 PQclear(res);
3992 return true;
3993}
3994
3995
3996/*
3997 * listTables()
3998 *
3999 * handler for \dt, \di, etc.
4000 *
4001 * tabtypes is an array of characters, specifying what info is desired:
4002 * t - tables
4003 * i - indexes
4004 * v - views
4005 * m - materialized views
4006 * s - sequences
4007 * E - foreign table (Note: different from 'f', the relkind value)
4008 * (any order of the above is fine)
4009 */
4010bool
4011listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
4012{
4013 bool showTables = strchr(tabtypes, 't') != NULL;
4014 bool showIndexes = strchr(tabtypes, 'i') != NULL;
4015 bool showViews = strchr(tabtypes, 'v') != NULL;
4016 bool showMatViews = strchr(tabtypes, 'm') != NULL;
4017 bool showSeq = strchr(tabtypes, 's') != NULL;
4018 bool showForeign = strchr(tabtypes, 'E') != NULL;
4019
4020 int ntypes;
4022 PGresult *res;
4023 printQueryOpt myopt = pset.popt;
4024 int cols_so_far;
4025 bool translate_columns[] = {false, false, true, false, false, false, false, false, false};
4026
4027 /* Count the number of explicitly-requested relation types */
4028 ntypes = showTables + showIndexes + showViews + showMatViews +
4029 showSeq + showForeign;
4030 /* If none, we default to \dtvmsE (but see also command.c) */
4031 if (ntypes == 0)
4032 showTables = showViews = showMatViews = showSeq = showForeign = true;
4033
4035
4037 "SELECT n.nspname as \"%s\",\n"
4038 " c.relname as \"%s\",\n"
4039 " CASE c.relkind"
4040 " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
4041 " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
4042 " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
4043 " WHEN " CppAsString2(RELKIND_INDEX) " THEN '%s'"
4044 " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
4045 " WHEN " CppAsString2(RELKIND_TOASTVALUE) " THEN '%s'"
4046 " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
4047 " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
4048 " WHEN " CppAsString2(RELKIND_PARTITIONED_INDEX) " THEN '%s'"
4049 " END as \"%s\",\n"
4050 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
4051 gettext_noop("Schema"),
4052 gettext_noop("Name"),
4053 gettext_noop("table"),
4054 gettext_noop("view"),
4055 gettext_noop("materialized view"),
4056 gettext_noop("index"),
4057 gettext_noop("sequence"),
4058 gettext_noop("TOAST table"),
4059 gettext_noop("foreign table"),
4060 gettext_noop("partitioned table"),
4061 gettext_noop("partitioned index"),
4062 gettext_noop("Type"),
4063 gettext_noop("Owner"));
4064 cols_so_far = 4;
4065
4066 if (showIndexes)
4067 {
4069 ",\n c2.relname as \"%s\"",
4070 gettext_noop("Table"));
4071 cols_so_far++;
4072 }
4073
4074 if (verbose)
4075 {
4076 /*
4077 * Show whether a relation is permanent, temporary, or unlogged.
4078 */
4080 ",\n CASE c.relpersistence "
4081 "WHEN " CppAsString2(RELPERSISTENCE_PERMANENT) " THEN '%s' "
4082 "WHEN " CppAsString2(RELPERSISTENCE_TEMP) " THEN '%s' "
4083 "WHEN " CppAsString2(RELPERSISTENCE_UNLOGGED) " THEN '%s' "
4084 "END as \"%s\"",
4085 gettext_noop("permanent"),
4086 gettext_noop("temporary"),
4087 gettext_noop("unlogged"),
4088 gettext_noop("Persistence"));
4089 translate_columns[cols_so_far] = true;
4090
4091 /*
4092 * We don't bother to count cols_so_far below here, as there's no need
4093 * to; this might change with future additions to the output columns.
4094 */
4095
4096 /*
4097 * Access methods exist for tables, materialized views and indexes.
4098 * This has been introduced in PostgreSQL 12 for tables.
4099 */
4100 if (pset.sversion >= 120000 && !pset.hide_tableam &&
4101 (showTables || showMatViews || showIndexes))
4103 ",\n am.amname as \"%s\"",
4104 gettext_noop("Access method"));
4105
4107 ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\""
4108 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
4109 gettext_noop("Size"),
4110 gettext_noop("Description"));
4111 }
4112
4114 "\nFROM pg_catalog.pg_class c"
4115 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
4116
4117 if (pset.sversion >= 120000 && !pset.hide_tableam &&
4118 (showTables || showMatViews || showIndexes))
4120 "\n LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam");
4121
4122 if (showIndexes)
4124 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
4125 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
4126
4127 appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
4128 if (showTables)
4129 {
4130 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_RELATION) ","
4131 CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
4132 /* with 'S' or a pattern, allow 't' to match TOAST tables too */
4133 if (showSystem || pattern)
4134 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_TOASTVALUE) ",");
4135 }
4136 if (showViews)
4137 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_VIEW) ",");
4138 if (showMatViews)
4139 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_MATVIEW) ",");
4140 if (showIndexes)
4141 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_INDEX) ","
4142 CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
4143 if (showSeq)
4144 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_SEQUENCE) ",");
4145 if (showSystem || pattern)
4146 appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL */
4147 if (showForeign)
4148 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_FOREIGN_TABLE) ",");
4149
4150 appendPQExpBufferStr(&buf, "''"); /* dummy */
4151 appendPQExpBufferStr(&buf, ")\n");
4152
4153 if (!showSystem && !pattern)
4154 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4155 " AND n.nspname !~ '^pg_toast'\n"
4156 " AND n.nspname <> 'information_schema'\n");
4157
4158 if (!validateSQLNamePattern(&buf, pattern, true, false,
4159 "n.nspname", "c.relname", NULL,
4160 "pg_catalog.pg_table_is_visible(c.oid)",
4161 NULL, 3))
4162 {
4164 return false;
4165 }
4166
4167 appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
4168
4169 res = PSQLexec(buf.data);
4171 if (!res)
4172 return false;
4173
4174 /*
4175 * Most functions in this file are content to print an empty table when
4176 * there are no matching objects. We intentionally deviate from that
4177 * here, but only in !quiet mode, for historical reasons.
4178 */
4179 if (PQntuples(res) == 0 && !pset.quiet)
4180 {
4181 if (pattern)
4182 {
4183 if (ntypes != 1)
4184 pg_log_error("Did not find any relations named \"%s\".",
4185 pattern);
4186 else if (showTables)
4187 pg_log_error("Did not find any tables named \"%s\".",
4188 pattern);
4189 else if (showIndexes)
4190 pg_log_error("Did not find any indexes named \"%s\".",
4191 pattern);
4192 else if (showViews)
4193 pg_log_error("Did not find any views named \"%s\".",
4194 pattern);
4195 else if (showMatViews)
4196 pg_log_error("Did not find any materialized views named \"%s\".",
4197 pattern);
4198 else if (showSeq)
4199 pg_log_error("Did not find any sequences named \"%s\".",
4200 pattern);
4201 else if (showForeign)
4202 pg_log_error("Did not find any foreign tables named \"%s\".",
4203 pattern);
4204 else /* should not get here */
4205 pg_log_error_internal("Did not find any ??? named \"%s\".",
4206 pattern);
4207 }
4208 else
4209 {
4210 if (ntypes != 1)
4211 pg_log_error("Did not find any relations.");
4212 else if (showTables)
4213 pg_log_error("Did not find any tables.");
4214 else if (showIndexes)
4215 pg_log_error("Did not find any indexes.");
4216 else if (showViews)
4217 pg_log_error("Did not find any views.");
4218 else if (showMatViews)
4219 pg_log_error("Did not find any materialized views.");
4220 else if (showSeq)
4221 pg_log_error("Did not find any sequences.");
4222 else if (showForeign)
4223 pg_log_error("Did not find any foreign tables.");
4224 else /* should not get here */
4225 pg_log_error_internal("Did not find any ??? relations.");
4226 }
4227 }
4228 else
4229 {
4230 myopt.title =
4231 (ntypes != 1) ? _("List of relations") :
4232 (showTables) ? _("List of tables") :
4233 (showIndexes) ? _("List of indexes") :
4234 (showViews) ? _("List of views") :
4235 (showMatViews) ? _("List of materialized views") :
4236 (showSeq) ? _("List of sequences") :
4237 (showForeign) ? _("List of foreign tables") :
4238 "List of ???"; /* should not get here */
4239 myopt.translate_header = true;
4240 myopt.translate_columns = translate_columns;
4241 myopt.n_translate_columns = lengthof(translate_columns);
4242
4243 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4244 }
4245
4246 PQclear(res);
4247 return true;
4248}
4249
4250/*
4251 * \dP
4252 * Takes an optional regexp to select particular relations
4253 *
4254 * As with \d, you can specify the kinds of relations you want:
4255 *
4256 * t for tables
4257 * i for indexes
4258 *
4259 * And there's additional flags:
4260 *
4261 * n to list non-leaf partitioned tables
4262 *
4263 * and you can mix and match these in any order.
4264 */
4265bool
4266listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
4267{
4268 bool showTables = strchr(reltypes, 't') != NULL;
4269 bool showIndexes = strchr(reltypes, 'i') != NULL;
4270 bool showNested = strchr(reltypes, 'n') != NULL;
4272 PQExpBufferData title;
4273 PGresult *res;
4274 printQueryOpt myopt = pset.popt;
4275 bool translate_columns[] = {false, false, false, false, false, false, false, false, false, false};
4276 const char *tabletitle;
4277 bool mixed_output = false;
4278
4279 /*
4280 * Note: Declarative table partitioning is only supported as of Pg 10.0.
4281 */
4282 if (pset.sversion < 100000)
4283 {
4284 char sverbuf[32];
4285
4286 pg_log_error("The server (version %s) does not support declarative table partitioning.",
4288 sverbuf, sizeof(sverbuf)));
4289 return true;
4290 }
4291
4292 /* If no relation kind was selected, show them all */
4293 if (!showTables && !showIndexes)
4294 showTables = showIndexes = true;
4295
4296 if (showIndexes && !showTables)
4297 tabletitle = _("List of partitioned indexes"); /* \dPi */
4298 else if (showTables && !showIndexes)
4299 tabletitle = _("List of partitioned tables"); /* \dPt */
4300 else
4301 {
4302 /* show all kinds */
4303 tabletitle = _("List of partitioned relations");
4304 mixed_output = true;
4305 }
4306
4308
4310 "SELECT n.nspname as \"%s\",\n"
4311 " c.relname as \"%s\",\n"
4312 " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
4313 gettext_noop("Schema"),
4314 gettext_noop("Name"),
4315 gettext_noop("Owner"));
4316
4317 if (mixed_output)
4318 {
4320 ",\n CASE c.relkind"
4321 " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
4322 " WHEN " CppAsString2(RELKIND_PARTITIONED_INDEX) " THEN '%s'"
4323 " END as \"%s\"",
4324 gettext_noop("partitioned table"),
4325 gettext_noop("partitioned index"),
4326 gettext_noop("Type"));
4327
4328 translate_columns[3] = true;
4329 }
4330
4331 if (showNested || pattern)
4333 ",\n inh.inhparent::pg_catalog.regclass as \"%s\"",
4334 gettext_noop("Parent name"));
4335
4336 if (showIndexes)
4338 ",\n c2.oid::pg_catalog.regclass as \"%s\"",
4339 gettext_noop("Table"));
4340
4341 if (verbose)
4342 {
4343 /*
4344 * Table access methods were introduced in v12, and can be set on
4345 * partitioned tables since v17.
4346 */
4347 appendPQExpBuffer(&buf, ",\n am.amname as \"%s\"",
4348 gettext_noop("Access method"));
4349
4350 if (showNested)
4351 {
4353 ",\n s.dps as \"%s\"",
4354 gettext_noop("Leaf partition size"));
4356 ",\n s.tps as \"%s\"",
4357 gettext_noop("Total size"));
4358 }
4359 else
4360 /* Sizes of all partitions are considered in this case. */
4362 ",\n s.tps as \"%s\"",
4363 gettext_noop("Total size"));
4364
4366 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
4367 gettext_noop("Description"));
4368 }
4369
4371 "\nFROM pg_catalog.pg_class c"
4372 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
4373
4374 if (showIndexes)
4376 "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
4377 "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
4378
4379 if (showNested || pattern)
4381 "\n LEFT JOIN pg_catalog.pg_inherits inh ON c.oid = inh.inhrelid");
4382
4383 if (verbose)
4384 {
4386 "\n LEFT JOIN pg_catalog.pg_am am ON c.relam = am.oid");
4387
4388 if (pset.sversion < 120000)
4389 {
4391 ",\n LATERAL (WITH RECURSIVE d\n"
4392 " AS (SELECT inhrelid AS oid, 1 AS level\n"
4393 " FROM pg_catalog.pg_inherits\n"
4394 " WHERE inhparent = c.oid\n"
4395 " UNION ALL\n"
4396 " SELECT inhrelid, level + 1\n"
4397 " FROM pg_catalog.pg_inherits i\n"
4398 " JOIN d ON i.inhparent = d.oid)\n"
4399 " SELECT pg_catalog.pg_size_pretty(sum(pg_catalog.pg_table_size("
4400 "d.oid))) AS tps,\n"
4401 " pg_catalog.pg_size_pretty(sum("
4402 "\n CASE WHEN d.level = 1"
4403 " THEN pg_catalog.pg_table_size(d.oid) ELSE 0 END)) AS dps\n"
4404 " FROM d) s");
4405 }
4406 else
4407 {
4408 /* PostgreSQL 12 has pg_partition_tree function */
4410 ",\n LATERAL (SELECT pg_catalog.pg_size_pretty(sum("
4411 "\n CASE WHEN ppt.isleaf AND ppt.level = 1"
4412 "\n THEN pg_catalog.pg_table_size(ppt.relid)"
4413 " ELSE 0 END)) AS dps"
4414 ",\n pg_catalog.pg_size_pretty(sum("
4415 "pg_catalog.pg_table_size(ppt.relid))) AS tps"
4416 "\n FROM pg_catalog.pg_partition_tree(c.oid) ppt) s");
4417 }
4418 }
4419
4420 appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
4421 if (showTables)
4422 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
4423 if (showIndexes)
4424 appendPQExpBufferStr(&buf, CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
4425 appendPQExpBufferStr(&buf, "''"); /* dummy */
4426 appendPQExpBufferStr(&buf, ")\n");
4427
4428 appendPQExpBufferStr(&buf, !showNested && !pattern ?
4429 " AND NOT c.relispartition\n" : "");
4430
4431 if (!pattern)
4432 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4433 " AND n.nspname !~ '^pg_toast'\n"
4434 " AND n.nspname <> 'information_schema'\n");
4435
4436 if (!validateSQLNamePattern(&buf, pattern, true, false,
4437 "n.nspname", "c.relname", NULL,
4438 "pg_catalog.pg_table_is_visible(c.oid)",
4439 NULL, 3))
4440 {
4442 return false;
4443 }
4444
4445 appendPQExpBuffer(&buf, "ORDER BY \"Schema\", %s%s\"Name\";",
4446 mixed_output ? "\"Type\" DESC, " : "",
4447 showNested || pattern ? "\"Parent name\" NULLS FIRST, " : "");
4448
4449 res = PSQLexec(buf.data);
4451 if (!res)
4452 return false;
4453
4454 initPQExpBuffer(&title);
4455 appendPQExpBufferStr(&title, tabletitle);
4456
4457 myopt.title = title.data;
4458 myopt.translate_header = true;
4459 myopt.translate_columns = translate_columns;
4460 myopt.n_translate_columns = lengthof(translate_columns);
4461
4462 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4463
4464 termPQExpBuffer(&title);
4465
4466 PQclear(res);
4467 return true;
4468}
4469
4470/*
4471 * \dL
4472 *
4473 * Describes languages.
4474 */
4475bool
4476listLanguages(const char *pattern, bool verbose, bool showSystem)
4477{
4479 PGresult *res;
4480 printQueryOpt myopt = pset.popt;
4481
4483
4485 "SELECT l.lanname AS \"%s\",\n"
4486 " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n"
4487 " l.lanpltrusted AS \"%s\"",
4488 gettext_noop("Name"),
4489 gettext_noop("Owner"),
4490 gettext_noop("Trusted"));
4491
4492 if (verbose)
4493 {
4495 ",\n NOT l.lanispl AS \"%s\",\n"
4496 " l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
4497 " l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n "
4498 "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
4499 gettext_noop("Internal language"),
4500 gettext_noop("Call handler"),
4501 gettext_noop("Validator"),
4502 gettext_noop("Inline handler"));
4503 printACLColumn(&buf, "l.lanacl");
4504 }
4505
4507 ",\n d.description AS \"%s\""
4508 "\nFROM pg_catalog.pg_language l\n"
4509 "LEFT JOIN pg_catalog.pg_description d\n"
4510 " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
4511 " AND d.objsubid = 0\n",
4512 gettext_noop("Description"));
4513
4514 if (pattern)
4515 {
4516 if (!validateSQLNamePattern(&buf, pattern, false, false,
4517 NULL, "l.lanname", NULL, NULL,
4518 NULL, 2))
4519 {
4521 return false;
4522 }
4523 }
4524
4525 if (!showSystem && !pattern)
4526 appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
4527
4528
4529 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4530
4531 res = PSQLexec(buf.data);
4533 if (!res)
4534 return false;
4535
4536 myopt.title = _("List of languages");
4537 myopt.translate_header = true;
4538
4539 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4540
4541 PQclear(res);
4542 return true;
4543}
4544
4545
4546/*
4547 * \dD
4548 *
4549 * Describes domains.
4550 */
4551bool
4552listDomains(const char *pattern, bool verbose, bool showSystem)
4553{
4555 PGresult *res;
4556 printQueryOpt myopt = pset.popt;
4557
4559
4561 "SELECT n.nspname as \"%s\",\n"
4562 " t.typname as \"%s\",\n"
4563 " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
4564 " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
4565 " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n"
4566 " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
4567 " t.typdefault as \"%s\",\n"
4568 " pg_catalog.array_to_string(ARRAY(\n"
4569 " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid AND r.contype = " CppAsString2(CONSTRAINT_CHECK) " ORDER BY r.conname\n"
4570 " ), ' ') as \"%s\"",
4571 gettext_noop("Schema"),
4572 gettext_noop("Name"),
4573 gettext_noop("Type"),
4574 gettext_noop("Collation"),
4575 gettext_noop("Nullable"),
4576 gettext_noop("Default"),
4577 gettext_noop("Check"));
4578
4579 if (verbose)
4580 {
4581 appendPQExpBufferStr(&buf, ",\n ");
4582 printACLColumn(&buf, "t.typacl");
4584 ",\n d.description as \"%s\"",
4585 gettext_noop("Description"));
4586 }
4587
4589 "\nFROM pg_catalog.pg_type t\n"
4590 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
4591
4592 if (verbose)
4594 " LEFT JOIN pg_catalog.pg_description d "
4595 "ON d.classoid = t.tableoid AND d.objoid = t.oid "
4596 "AND d.objsubid = 0\n");
4597
4598 appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
4599
4600 if (!showSystem && !pattern)
4601 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4602 " AND n.nspname <> 'information_schema'\n");
4603
4604 if (!validateSQLNamePattern(&buf, pattern, true, false,
4605 "n.nspname", "t.typname", NULL,
4606 "pg_catalog.pg_type_is_visible(t.oid)",
4607 NULL, 3))
4608 {
4610 return false;
4611 }
4612
4613 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4614
4615 res = PSQLexec(buf.data);
4617 if (!res)
4618 return false;
4619
4620 myopt.title = _("List of domains");
4621 myopt.translate_header = true;
4622
4623 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4624
4625 PQclear(res);
4626 return true;
4627}
4628
4629/*
4630 * \dc
4631 *
4632 * Describes conversions.
4633 */
4634bool
4635listConversions(const char *pattern, bool verbose, bool showSystem)
4636{
4638 PGresult *res;
4639 printQueryOpt myopt = pset.popt;
4640 static const bool translate_columns[] =
4641 {false, false, false, false, true, false};
4642
4644
4646 "SELECT n.nspname AS \"%s\",\n"
4647 " c.conname AS \"%s\",\n"
4648 " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
4649 " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
4650 " CASE WHEN c.condefault THEN '%s'\n"
4651 " ELSE '%s' END AS \"%s\"",
4652 gettext_noop("Schema"),
4653 gettext_noop("Name"),
4654 gettext_noop("Source"),
4655 gettext_noop("Destination"),
4656 gettext_noop("yes"), gettext_noop("no"),
4657 gettext_noop("Default?"));
4658
4659 if (verbose)
4661 ",\n d.description AS \"%s\"",
4662 gettext_noop("Description"));
4663
4665 "\nFROM pg_catalog.pg_conversion c\n"
4666 " JOIN pg_catalog.pg_namespace n "
4667 "ON n.oid = c.connamespace\n");
4668
4669 if (verbose)
4671 "LEFT JOIN pg_catalog.pg_description d "
4672 "ON d.classoid = c.tableoid\n"
4673 " AND d.objoid = c.oid "
4674 "AND d.objsubid = 0\n");
4675
4676 appendPQExpBufferStr(&buf, "WHERE true\n");
4677
4678 if (!showSystem && !pattern)
4679 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4680 " AND n.nspname <> 'information_schema'\n");
4681
4682 if (!validateSQLNamePattern(&buf, pattern, true, false,
4683 "n.nspname", "c.conname", NULL,
4684 "pg_catalog.pg_conversion_is_visible(c.oid)",
4685 NULL, 3))
4686 {
4688 return false;
4689 }
4690
4691 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4692
4693 res = PSQLexec(buf.data);
4695 if (!res)
4696 return false;
4697
4698 myopt.title = _("List of conversions");
4699 myopt.translate_header = true;
4700 myopt.translate_columns = translate_columns;
4701 myopt.n_translate_columns = lengthof(translate_columns);
4702
4703 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4704
4705 PQclear(res);
4706 return true;
4707}
4708
4709/*
4710 * \dconfig
4711 *
4712 * Describes configuration parameters.
4713 */
4714bool
4716 bool showSystem)
4717{
4719 PGresult *res;
4720 printQueryOpt myopt = pset.popt;
4721
4724 "SELECT s.name AS \"%s\", "
4725 "pg_catalog.current_setting(s.name) AS \"%s\"",
4726 gettext_noop("Parameter"),
4727 gettext_noop("Value"));
4728
4729 if (verbose)
4730 {
4732 ", s.vartype AS \"%s\", s.context AS \"%s\", ",
4733 gettext_noop("Type"),
4734 gettext_noop("Context"));
4735 if (pset.sversion >= 150000)
4736 printACLColumn(&buf, "p.paracl");
4737 else
4738 appendPQExpBuffer(&buf, "NULL AS \"%s\"",
4739 gettext_noop("Access privileges"));
4740 }
4741
4742 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_settings s\n");
4743
4744 if (verbose && pset.sversion >= 150000)
4746 " LEFT JOIN pg_catalog.pg_parameter_acl p\n"
4747 " ON pg_catalog.lower(s.name) = p.parname\n");
4748
4749 if (pattern)
4750 processSQLNamePattern(pset.db, &buf, pattern,
4751 false, false,
4752 NULL, "pg_catalog.lower(s.name)", NULL,
4753 NULL, NULL, NULL);
4754 else
4755 appendPQExpBufferStr(&buf, "WHERE s.source <> 'default' AND\n"
4756 " s.setting IS DISTINCT FROM s.boot_val\n");
4757
4758 appendPQExpBufferStr(&buf, "ORDER BY 1;");
4759
4760 res = PSQLexec(buf.data);
4762 if (!res)
4763 return false;
4764
4765 if (pattern)
4766 myopt.title = _("List of configuration parameters");
4767 else
4768 myopt.title = _("List of non-default configuration parameters");
4769 myopt.translate_header = true;
4770
4771 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4772
4773 PQclear(res);
4774 return true;
4775}
4776
4777/*
4778 * \dy
4779 *
4780 * Describes Event Triggers.
4781 */
4782bool
4783listEventTriggers(const char *pattern, bool verbose)
4784{
4786 PGresult *res;
4787 printQueryOpt myopt = pset.popt;
4788 static const bool translate_columns[] =
4789 {false, false, false, true, false, false, false};
4790
4791 if (pset.sversion < 90300)
4792 {
4793 char sverbuf[32];
4794
4795 pg_log_error("The server (version %s) does not support event triggers.",
4797 sverbuf, sizeof(sverbuf)));
4798 return true;
4799 }
4800
4802
4804 "SELECT evtname as \"%s\", "
4805 "evtevent as \"%s\", "
4806 "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
4807 " case evtenabled when 'O' then '%s'"
4808 " when 'R' then '%s'"
4809 " when 'A' then '%s'"
4810 " when 'D' then '%s' end as \"%s\",\n"
4811 " e.evtfoid::pg_catalog.regproc as \"%s\", "
4812 "pg_catalog.array_to_string(array(select x"
4813 " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
4814 gettext_noop("Name"),
4815 gettext_noop("Event"),
4816 gettext_noop("Owner"),
4817 gettext_noop("enabled"),
4818 gettext_noop("replica"),
4819 gettext_noop("always"),
4820 gettext_noop("disabled"),
4821 gettext_noop("Enabled"),
4822 gettext_noop("Function"),
4823 gettext_noop("Tags"));
4824 if (verbose)
4826 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
4827 gettext_noop("Description"));
4829 "\nFROM pg_catalog.pg_event_trigger e ");
4830
4831 if (!validateSQLNamePattern(&buf, pattern, false, false,
4832 NULL, "evtname", NULL, NULL,
4833 NULL, 1))
4834 {
4836 return false;
4837 }
4838
4839 appendPQExpBufferStr(&buf, "ORDER BY 1");
4840
4841 res = PSQLexec(buf.data);
4843 if (!res)
4844 return false;
4845
4846 myopt.title = _("List of event triggers");
4847 myopt.translate_header = true;
4848 myopt.translate_columns = translate_columns;
4849 myopt.n_translate_columns = lengthof(translate_columns);
4850
4851 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4852
4853 PQclear(res);
4854 return true;
4855}
4856
4857/*
4858 * \dX
4859 *
4860 * Describes extended statistics.
4861 */
4862bool
4863listExtendedStats(const char *pattern)
4864{
4866 PGresult *res;
4867 printQueryOpt myopt = pset.popt;
4868
4869 if (pset.sversion < 100000)
4870 {
4871 char sverbuf[32];
4872
4873 pg_log_error("The server (version %s) does not support extended statistics.",
4875 sverbuf, sizeof(sverbuf)));
4876 return true;
4877 }
4878
4881 "SELECT \n"
4882 "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS \"%s\", \n"
4883 "es.stxname AS \"%s\", \n",
4884 gettext_noop("Schema"),
4885 gettext_noop("Name"));
4886
4887 if (pset.sversion >= 140000)
4889 "pg_catalog.format('%%s FROM %%s', \n"
4890 " pg_catalog.pg_get_statisticsobjdef_columns(es.oid), \n"
4891 " es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4892 gettext_noop("Definition"));
4893 else
4895 "pg_catalog.format('%%s FROM %%s', \n"
4896 " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(a.attname),', ') \n"
4897 " FROM pg_catalog.unnest(es.stxkeys) s(attnum) \n"
4898 " JOIN pg_catalog.pg_attribute a \n"
4899 " ON (es.stxrelid = a.attrelid \n"
4900 " AND a.attnum = s.attnum \n"
4901 " AND NOT a.attisdropped)), \n"
4902 "es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4903 gettext_noop("Definition"));
4904
4906 ",\nCASE WHEN " CppAsString2(STATS_EXT_NDISTINCT) " = any(es.stxkind) THEN 'defined' \n"
4907 "END AS \"%s\", \n"
4908 "CASE WHEN " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(es.stxkind) THEN 'defined' \n"
4909 "END AS \"%s\"",
4910 gettext_noop("Ndistinct"),
4911 gettext_noop("Dependencies"));
4912
4913 /*
4914 * Include the MCV statistics kind.
4915 */
4916 if (pset.sversion >= 120000)
4917 {
4919 ",\nCASE WHEN " CppAsString2(STATS_EXT_MCV) " = any(es.stxkind) THEN 'defined' \n"
4920 "END AS \"%s\" ",
4921 gettext_noop("MCV"));
4922 }
4923
4925 " \nFROM pg_catalog.pg_statistic_ext es \n");
4926
4927 if (!validateSQLNamePattern(&buf, pattern,
4928 false, false,
4929 "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text", "es.stxname",
4930 NULL, "pg_catalog.pg_statistics_obj_is_visible(es.oid)",
4931 NULL, 3))
4932 {
4934 return false;
4935 }
4936
4937 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4938
4939 res = PSQLexec(buf.data);
4941 if (!res)
4942 return false;
4943
4944 myopt.title = _("List of extended statistics");
4945 myopt.translate_header = true;
4946
4947 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4948
4949 PQclear(res);
4950 return true;
4951}
4952
4953/*
4954 * \dC
4955 *
4956 * Describes casts.
4957 */
4958bool
4959listCasts(const char *pattern, bool verbose)
4960{
4962 PGresult *res;
4963 printQueryOpt myopt = pset.popt;
4964 static const bool translate_columns[] = {false, false, false, true, true, false};
4965
4967
4969 "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
4970 " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n",
4971 gettext_noop("Source type"),
4972 gettext_noop("Target type"));
4973
4974 /*
4975 * We don't attempt to localize '(binary coercible)' or '(with inout)',
4976 * because there's too much risk of gettext translating a function name
4977 * that happens to match some string in the PO database.
4978 */
4980 " CASE WHEN c.castmethod = '%c' THEN '(binary coercible)'\n"
4981 " WHEN c.castmethod = '%c' THEN '(with inout)'\n"
4982 " ELSE p.proname\n"
4983 " END AS \"%s\",\n",
4984 COERCION_METHOD_BINARY,
4985 COERCION_METHOD_INOUT,
4986 gettext_noop("Function"));
4987
4989 " CASE WHEN c.castcontext = '%c' THEN '%s'\n"
4990 " WHEN c.castcontext = '%c' THEN '%s'\n"
4991 " ELSE '%s'\n"
4992 " END AS \"%s\"",
4993 COERCION_CODE_EXPLICIT,
4994 gettext_noop("no"),
4995 COERCION_CODE_ASSIGNMENT,
4996 gettext_noop("in assignment"),
4997 gettext_noop("yes"),
4998 gettext_noop("Implicit?"));
4999
5000 if (verbose)
5002 ",\n CASE WHEN p.proleakproof THEN '%s'\n"
5003 " ELSE '%s'\n"
5004 " END AS \"%s\",\n"
5005 " d.description AS \"%s\"",
5006 gettext_noop("yes"),
5007 gettext_noop("no"),
5008 gettext_noop("Leakproof?"),
5009 gettext_noop("Description"));
5010
5011 /*
5012 * We need a left join to pg_proc for binary casts; the others are just
5013 * paranoia.
5014 */
5016 "\nFROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
5017 " ON c.castfunc = p.oid\n"
5018 " LEFT JOIN pg_catalog.pg_type ts\n"
5019 " ON c.castsource = ts.oid\n"
5020 " LEFT JOIN pg_catalog.pg_namespace ns\n"
5021 " ON ns.oid = ts.typnamespace\n"
5022 " LEFT JOIN pg_catalog.pg_type tt\n"
5023 " ON c.casttarget = tt.oid\n"
5024 " LEFT JOIN pg_catalog.pg_namespace nt\n"
5025 " ON nt.oid = tt.typnamespace\n");
5026
5027 if (verbose)
5029 " LEFT JOIN pg_catalog.pg_description d\n"
5030 " ON d.classoid = c.tableoid AND d.objoid = "
5031 "c.oid AND d.objsubid = 0\n");
5032
5033 appendPQExpBufferStr(&buf, "WHERE ( (true");
5034
5035 /*
5036 * Match name pattern against either internal or external name of either
5037 * castsource or casttarget
5038 */
5039 if (!validateSQLNamePattern(&buf, pattern, true, false,
5040 "ns.nspname", "ts.typname",
5041 "pg_catalog.format_type(ts.oid, NULL)",
5042 "pg_catalog.pg_type_is_visible(ts.oid)",
5043 NULL, 3))
5044 goto error_return;
5045
5046 appendPQExpBufferStr(&buf, ") OR (true");
5047
5048 if (!validateSQLNamePattern(&buf, pattern, true, false,
5049 "nt.nspname", "tt.typname",
5050 "pg_catalog.format_type(tt.oid, NULL)",
5051 "pg_catalog.pg_type_is_visible(tt.oid)",
5052 NULL, 3))
5053 goto error_return;
5054
5055 appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
5056
5057 res = PSQLexec(buf.data);
5059 if (!res)
5060 return false;
5061
5062 myopt.title = _("List of casts");
5063 myopt.translate_header = true;
5064 myopt.translate_columns = translate_columns;
5065 myopt.n_translate_columns = lengthof(translate_columns);
5066
5067 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5068
5069 PQclear(res);
5070 return true;
5071
5072error_return:
5074 return false;
5075}
5076
5077/*
5078 * \dO
5079 *
5080 * Describes collations.
5081 */
5082bool
5083listCollations(const char *pattern, bool verbose, bool showSystem)
5084{
5086 PGresult *res;
5087 printQueryOpt myopt = pset.popt;
5088 static const bool translate_columns[] = {false, false, false, false, false, false, false, true, false};
5089
5091
5093 "SELECT\n"
5094 " n.nspname AS \"%s\",\n"
5095 " c.collname AS \"%s\",\n",
5096 gettext_noop("Schema"),
5097 gettext_noop("Name"));
5098
5099 if (pset.sversion >= 100000)
5101 " CASE c.collprovider "
5102 "WHEN " CppAsString2(COLLPROVIDER_DEFAULT) " THEN 'default' "
5103 "WHEN " CppAsString2(COLLPROVIDER_BUILTIN) " THEN 'builtin' "
5104 "WHEN " CppAsString2(COLLPROVIDER_LIBC) " THEN 'libc' "
5105 "WHEN " CppAsString2(COLLPROVIDER_ICU) " THEN 'icu' "
5106 "END AS \"%s\",\n",
5107 gettext_noop("Provider"));
5108 else
5110 " 'libc' AS \"%s\",\n",
5111 gettext_noop("Provider"));
5112
5114 " c.collcollate AS \"%s\",\n"
5115 " c.collctype AS \"%s\",\n",
5116 gettext_noop("Collate"),
5117 gettext_noop("Ctype"));
5118
5119 if (pset.sversion >= 170000)
5121 " c.colllocale AS \"%s\",\n",
5122 gettext_noop("Locale"));
5123 else if (pset.sversion >= 150000)
5125 " c.colliculocale AS \"%s\",\n",
5126 gettext_noop("Locale"));
5127 else
5129 " c.collcollate AS \"%s\",\n",
5130 gettext_noop("Locale"));
5131
5132 if (pset.sversion >= 160000)
5134 " c.collicurules AS \"%s\",\n",
5135 gettext_noop("ICU Rules"));
5136 else
5138 " NULL AS \"%s\",\n",
5139 gettext_noop("ICU Rules"));
5140
5141 if (pset.sversion >= 120000)
5143 " CASE WHEN c.collisdeterministic THEN '%s' ELSE '%s' END AS \"%s\"",
5144 gettext_noop("yes"), gettext_noop("no"),
5145 gettext_noop("Deterministic?"));
5146 else
5148 " '%s' AS \"%s\"",
5149 gettext_noop("yes"),
5150 gettext_noop("Deterministic?"));
5151
5152 if (verbose)
5154 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
5155 gettext_noop("Description"));
5156
5158 "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
5159 "WHERE n.oid = c.collnamespace\n");
5160
5161 if (!showSystem && !pattern)
5162 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
5163 " AND n.nspname <> 'information_schema'\n");
5164
5165 /*
5166 * Hide collations that aren't usable in the current database's encoding.
5167 * If you think to change this, note that pg_collation_is_visible rejects
5168 * unusable collations, so you will need to hack name pattern processing
5169 * somehow to avoid inconsistent behavior.
5170 */
5171 appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
5172
5173 if (!validateSQLNamePattern(&buf, pattern, true, false,
5174 "n.nspname", "c.collname", NULL,
5175 "pg_catalog.pg_collation_is_visible(c.oid)",
5176 NULL, 3))
5177 {
5179 return false;
5180 }
5181
5182 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5183
5184 res = PSQLexec(buf.data);
5186 if (!res)
5187 return false;
5188
5189 myopt.title = _("List of collations");
5190 myopt.translate_header = true;
5191 myopt.translate_columns = translate_columns;
5192 myopt.n_translate_columns = lengthof(translate_columns);
5193
5194 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5195
5196 PQclear(res);
5197 return true;
5198}
5199
5200/*
5201 * \dn
5202 *
5203 * Describes schemas (namespaces)
5204 */
5205bool
5206listSchemas(const char *pattern, bool verbose, bool showSystem)
5207{
5209 PGresult *res;
5210 printQueryOpt myopt = pset.popt;
5211 int pub_schema_tuples = 0;
5212 char **footers = NULL;
5213
5216 "SELECT n.nspname AS \"%s\",\n"
5217 " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
5218 gettext_noop("Name"),
5219 gettext_noop("Owner"));
5220
5221 if (verbose)
5222 {
5223 appendPQExpBufferStr(&buf, ",\n ");
5224 printACLColumn(&buf, "n.nspacl");
5226 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
5227 gettext_noop("Description"));
5228 }
5229
5231 "\nFROM pg_catalog.pg_namespace n\n");
5232
5233 if (!showSystem && !pattern)
5235 "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
5236
5237 if (!validateSQLNamePattern(&buf, pattern,
5238 !showSystem && !pattern, false,
5239 NULL, "n.nspname", NULL,
5240 NULL,
5241 NULL, 2))
5242 goto error_return;
5243
5244 appendPQExpBufferStr(&buf, "ORDER BY 1;");
5245
5246 res = PSQLexec(buf.data);
5247 if (!res)
5248 goto error_return;
5249
5250 myopt.title = _("List of schemas");
5251 myopt.translate_header = true;
5252
5253 if (pattern && pset.sversion >= 150000)
5254 {
5255 PGresult *result;
5256 int i;
5257
5259 "SELECT pubname \n"
5260 "FROM pg_catalog.pg_publication p\n"
5261 " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
5262 " JOIN pg_catalog.pg_namespace n ON n.oid = pn.pnnspid \n"
5263 "WHERE n.nspname = '%s'\n"
5264 "ORDER BY 1",
5265 pattern);
5266 result = PSQLexec(buf.data);
5267 if (!result)
5268 goto error_return;
5269 else
5270 pub_schema_tuples = PQntuples(result);
5271
5272 if (pub_schema_tuples > 0)
5273 {
5274 /*
5275 * Allocate memory for footers. Size of footers will be 1 (for
5276 * storing "Publications:" string) + publication schema mapping
5277 * count + 1 (for storing NULL).
5278 */
5279 footers = (char **) pg_malloc((1 + pub_schema_tuples + 1) * sizeof(char *));
5280 footers[0] = pg_strdup(_("Publications:"));
5281
5282 /* Might be an empty set - that's ok */
5283 for (i = 0; i < pub_schema_tuples; i++)
5284 {
5285 printfPQExpBuffer(&buf, " \"%s\"",
5286 PQgetvalue(result, i, 0));
5287
5288 footers[i + 1] = pg_strdup(buf.data);
5289 }
5290
5291 footers[i + 1] = NULL;
5292 myopt.footers = footers;
5293 }
5294
5295 PQclear(result);
5296 }
5297
5298 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5299
5301 PQclear(res);
5302
5303 /* Free the memory allocated for the footer */
5304 if (footers)
5305 {
5306 char **footer = NULL;
5307
5308 for (footer = footers; *footer; footer++)
5309 pg_free(*footer);
5310
5311 pg_free(footers);
5312 }
5313
5314 return true;
5315
5316error_return:
5318 return false;
5319}
5320
5321
5322/*
5323 * \dFp
5324 * list text search parsers
5325 */
5326bool
5327listTSParsers(const char *pattern, bool verbose)
5328{
5330 PGresult *res;
5331 printQueryOpt myopt = pset.popt;
5332
5333 if (verbose)
5334 return listTSParsersVerbose(pattern);
5335
5337
5339 "SELECT\n"
5340 " n.nspname as \"%s\",\n"
5341 " p.prsname as \"%s\",\n"
5342 " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
5343 "FROM pg_catalog.pg_ts_parser p\n"
5344 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
5345 gettext_noop("Schema"),
5346 gettext_noop("Name"),
5347 gettext_noop("Description")
5348 );
5349
5350 if (!validateSQLNamePattern(&buf, pattern, false, false,
5351 "n.nspname", "p.prsname", NULL,
5352 "pg_catalog.pg_ts_parser_is_visible(p.oid)",
5353 NULL, 3))
5354 {
5356 return false;
5357 }
5358
5359 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5360
5361 res = PSQLexec(buf.data);
5363 if (!res)
5364 return false;
5365
5366 myopt.title = _("List of text search parsers");
5367 myopt.translate_header = true;
5368
5369 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5370
5371 PQclear(res);
5372 return true;
5373}
5374
5375/*
5376 * full description of parsers
5377 */
5378static bool
5379listTSParsersVerbose(const char *pattern)
5380{
5382 PGresult *res;
5383 int i;
5384
5386
5388 "SELECT p.oid,\n"
5389 " n.nspname,\n"
5390 " p.prsname\n"
5391 "FROM pg_catalog.pg_ts_parser p\n"
5392 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n"
5393 );
5394
5395 if (!validateSQLNamePattern(&buf, pattern, false, false,
5396 "n.nspname", "p.prsname", NULL,
5397 "pg_catalog.pg_ts_parser_is_visible(p.oid)",
5398 NULL, 3))
5399 {
5401 return false;
5402 }
5403
5404 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5405
5406 res = PSQLexec(buf.data);
5408 if (!res)
5409 return false;
5410
5411 if (PQntuples(res) == 0)
5412 {
5413 if (!pset.quiet)
5414 {
5415 if (pattern)
5416 pg_log_error("Did not find any text search parser named \"%s\".",
5417 pattern);
5418 else
5419 pg_log_error("Did not find any text search parsers.");
5420 }
5421 PQclear(res);
5422 return false;
5423 }
5424
5425 for (i = 0; i < PQntuples(res); i++)
5426 {
5427 const char *oid;
5428 const char *nspname = NULL;
5429 const char *prsname;
5430
5431 oid = PQgetvalue(res, i, 0);
5432 if (!PQgetisnull(res, i, 1))
5433 nspname = PQgetvalue(res, i, 1);
5434 prsname = PQgetvalue(res, i, 2);
5435
5436 if (!describeOneTSParser(oid, nspname, prsname))
5437 {
5438 PQclear(res);
5439 return false;
5440 }
5441
5442 if (cancel_pressed)
5443 {
5444 PQclear(res);
5445 return false;
5446 }
5447 }
5448
5449 PQclear(res);
5450 return true;
5451}
5452
5453static bool
5454describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
5455{
5457 PGresult *res;
5458 PQExpBufferData title;
5459 printQueryOpt myopt = pset.popt;
5460 static const bool translate_columns[] = {true, false, false};
5461
5463
5465 "SELECT '%s' AS \"%s\",\n"
5466 " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
5467 " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
5468 " FROM pg_catalog.pg_ts_parser p\n"
5469 " WHERE p.oid = '%s'\n"
5470 "UNION ALL\n"
5471 "SELECT '%s',\n"
5472 " p.prstoken::pg_catalog.regproc,\n"
5473 " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
5474 " FROM pg_catalog.pg_ts_parser p\n"
5475 " WHERE p.oid = '%s'\n"
5476 "UNION ALL\n"
5477 "SELECT '%s',\n"
5478 " p.prsend::pg_catalog.regproc,\n"
5479 " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
5480 " FROM pg_catalog.pg_ts_parser p\n"
5481 " WHERE p.oid = '%s'\n"
5482 "UNION ALL\n"
5483 "SELECT '%s',\n"
5484 " p.prsheadline::pg_catalog.regproc,\n"
5485 " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
5486 " FROM pg_catalog.pg_ts_parser p\n"
5487 " WHERE p.oid = '%s'\n"
5488 "UNION ALL\n"
5489 "SELECT '%s',\n"
5490 " p.prslextype::pg_catalog.regproc,\n"
5491 " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
5492 " FROM pg_catalog.pg_ts_parser p\n"
5493 " WHERE p.oid = '%s';",
5494 gettext_noop("Start parse"),
5495 gettext_noop("Method"),
5496 gettext_noop("Function"),
5497 gettext_noop("Description"),
5498 oid,
5499 gettext_noop("Get next token"),
5500 oid,
5501 gettext_noop("End parse"),
5502 oid,
5503 gettext_noop("Get headline"),
5504 oid,
5505 gettext_noop("Get token types"),
5506 oid);
5507
5508 res = PSQLexec(buf.data);
5510 if (!res)
5511 return false;
5512
5513 initPQExpBuffer(&title);
5514 if (nspname)
5515 printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
5516 nspname, prsname);
5517 else
5518 printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
5519 myopt.title = title.data;
5520 myopt.footers = NULL;
5521 myopt.topt.default_footer = false;
5522 myopt.translate_header = true;
5523 myopt.translate_columns = translate_columns;
5524 myopt.n_translate_columns = lengthof(translate_columns);
5525
5526 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5527
5528 PQclear(res);
5529
5531
5533 "SELECT t.alias as \"%s\",\n"
5534 " t.description as \"%s\"\n"
5535 "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
5536 "ORDER BY 1;",
5537 gettext_noop("Token name"),
5538 gettext_noop("Description"),
5539 oid);
5540
5541 res = PSQLexec(buf.data);
5543 if (!res)
5544 {
5545 termPQExpBuffer(&title);
5546 return false;
5547 }
5548
5549 if (nspname)
5550 printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
5551 nspname, prsname);
5552 else
5553 printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
5554 myopt.title = title.data;
5555 myopt.footers = NULL;
5556 myopt.topt.default_footer = true;
5557 myopt.translate_header = true;
5558 myopt.translate_columns = NULL;
5559 myopt.n_translate_columns = 0;
5560
5561 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5562
5563 termPQExpBuffer(&title);
5564 PQclear(res);
5565 return true;
5566}
5567
5568
5569/*
5570 * \dFd
5571 * list text search dictionaries
5572 */
5573bool
5574listTSDictionaries(const char *pattern, bool verbose)
5575{
5577 PGresult *res;
5578 printQueryOpt myopt = pset.popt;
5579
5581
5583 "SELECT\n"
5584 " n.nspname as \"%s\",\n"
5585 " d.dictname as \"%s\",\n",
5586 gettext_noop("Schema"),
5587 gettext_noop("Name"));
5588
5589 if (verbose)
5590 {
5592 " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM\n"
5593 " pg_catalog.pg_ts_template t\n"
5594 " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace\n"
5595 " WHERE d.dicttemplate = t.oid ) AS \"%s\",\n"
5596 " d.dictinitoption as \"%s\",\n",
5597 gettext_noop("Template"),
5598 gettext_noop("Init options"));
5599 }
5600
5602 " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
5603 gettext_noop("Description"));
5604
5605 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
5606 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
5607
5608 if (!validateSQLNamePattern(&buf, pattern, false, false,
5609 "n.nspname", "d.dictname", NULL,
5610 "pg_catalog.pg_ts_dict_is_visible(d.oid)",
5611 NULL, 3))
5612 {
5614 return false;
5615 }
5616
5617 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5618
5619 res = PSQLexec(buf.data);
5621 if (!res)
5622 return false;
5623
5624 myopt.title = _("List of text search dictionaries");
5625 myopt.translate_header = true;
5626
5627 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5628
5629 PQclear(res);
5630 return true;
5631}
5632
5633
5634/*
5635 * \dFt
5636 * list text search templates
5637 */
5638bool
5639listTSTemplates(const char *pattern, bool verbose)
5640{
5642 PGresult *res;
5643 printQueryOpt myopt = pset.popt;
5644
5646
5647 if (verbose)
5649 "SELECT\n"
5650 " n.nspname AS \"%s\",\n"
5651 " t.tmplname AS \"%s\",\n"
5652 " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
5653 " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
5654 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
5655 gettext_noop("Schema"),
5656 gettext_noop("Name"),
5657 gettext_noop("Init"),
5658 gettext_noop("Lexize"),
5659 gettext_noop("Description"));
5660 else
5662 "SELECT\n"
5663 " n.nspname AS \"%s\",\n"
5664 " t.tmplname AS \"%s\",\n"
5665 " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
5666 gettext_noop("Schema"),
5667 gettext_noop("Name"),
5668 gettext_noop("Description"));
5669
5670 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
5671 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
5672
5673 if (!validateSQLNamePattern(&buf, pattern, false, false,
5674 "n.nspname", "t.tmplname", NULL,
5675 "pg_catalog.pg_ts_template_is_visible(t.oid)",
5676 NULL, 3))
5677 {
5679 return false;
5680 }
5681
5682 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5683
5684 res = PSQLexec(buf.data);
5686 if (!res)
5687 return false;
5688
5689 myopt.title = _("List of text search templates");
5690 myopt.translate_header = true;
5691
5692 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5693
5694 PQclear(res);
5695 return true;
5696}
5697
5698
5699/*
5700 * \dF
5701 * list text search configurations
5702 */
5703bool
5704listTSConfigs(const char *pattern, bool verbose)
5705{
5707 PGresult *res;
5708 printQueryOpt myopt = pset.popt;
5709
5710 if (verbose)
5711 return listTSConfigsVerbose(pattern);
5712
5714
5716 "SELECT\n"
5717 " n.nspname as \"%s\",\n"
5718 " c.cfgname as \"%s\",\n"
5719 " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
5720 "FROM pg_catalog.pg_ts_config c\n"
5721 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace\n",
5722 gettext_noop("Schema"),
5723 gettext_noop("Name"),
5724 gettext_noop("Description")
5725 );
5726
5727 if (!validateSQLNamePattern(&buf, pattern, false, false,
5728 "n.nspname", "c.cfgname", NULL,
5729 "pg_catalog.pg_ts_config_is_visible(c.oid)",
5730 NULL, 3))
5731 {
5733 return false;
5734 }
5735
5736 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5737
5738 res = PSQLexec(buf.data);
5740 if (!res)
5741 return false;
5742
5743 myopt.title = _("List of text search configurations");
5744 myopt.translate_header = true;
5745
5746 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5747
5748 PQclear(res);
5749 return true;
5750}
5751
5752static bool
5753listTSConfigsVerbose(const char *pattern)
5754{
5756 PGresult *res;
5757 int i;
5758
5760
5762 "SELECT c.oid, c.cfgname,\n"
5763 " n.nspname,\n"
5764 " p.prsname,\n"
5765 " np.nspname as pnspname\n"
5766 "FROM pg_catalog.pg_ts_config c\n"
5767 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace,\n"
5768 " pg_catalog.pg_ts_parser p\n"
5769 " LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.prsnamespace\n"
5770 "WHERE p.oid = c.cfgparser\n"
5771 );
5772
5773 if (!validateSQLNamePattern(&buf, pattern, true, false,
5774 "n.nspname", "c.cfgname", NULL,
5775 "pg_catalog.pg_ts_config_is_visible(c.oid)",
5776 NULL, 3))
5777 {
5779 return false;
5780 }
5781
5782 appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
5783
5784 res = PSQLexec(buf.data);
5786 if (!res)
5787 return false;
5788
5789 if (PQntuples(res) == 0)
5790 {
5791 if (!pset.quiet)
5792 {
5793 if (pattern)
5794 pg_log_error("Did not find any text search configuration named \"%s\".",
5795 pattern);
5796 else
5797 pg_log_error("Did not find any text search configurations.");
5798 }
5799 PQclear(res);
5800 return false;
5801 }
5802
5803 for (i = 0; i < PQntuples(res); i++)
5804 {
5805 const char *oid;
5806 const char *cfgname;
5807 const char *nspname = NULL;
5808 const char *prsname;
5809 const char *pnspname = NULL;
5810
5811 oid = PQgetvalue(res, i, 0);
5812 cfgname = PQgetvalue(res, i, 1);
5813 if (!PQgetisnull(res, i, 2))
5814 nspname = PQgetvalue(res, i, 2);
5815 prsname = PQgetvalue(res, i, 3);
5816 if (!PQgetisnull(res, i, 4))
5817 pnspname = PQgetvalue(res, i, 4);
5818
5819 if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
5820 {
5821 PQclear(res);
5822 return false;
5823 }
5824
5825 if (cancel_pressed)
5826 {
5827 PQclear(res);
5828 return false;
5829 }
5830 }
5831
5832 PQclear(res);
5833 return true;
5834}
5835
5836static bool
5837describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
5838 const char *pnspname, const char *prsname)
5839{
5841 title;
5842 PGresult *res;
5843 printQueryOpt myopt = pset.popt;
5844
5846
5848 "SELECT\n"
5849 " ( SELECT t.alias FROM\n"
5850 " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
5851 " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
5852 " pg_catalog.btrim(\n"
5853 " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
5854 " FROM pg_catalog.pg_ts_config_map AS mm\n"
5855 " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
5856 " ORDER BY mapcfg, maptokentype, mapseqno\n"
5857 " ) :: pg_catalog.text,\n"
5858 " '{}') AS \"%s\"\n"
5859 "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
5860 "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
5861 "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
5862 "ORDER BY 1;",
5863 gettext_noop("Token"),
5864 gettext_noop("Dictionaries"),
5865 oid);
5866
5867 res = PSQLexec(buf.data);
5869 if (!res)
5870 return false;
5871
5872 initPQExpBuffer(&title);
5873
5874 if (nspname)
5875 appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
5876 nspname, cfgname);
5877 else
5878 appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
5879 cfgname);
5880
5881 if (pnspname)
5882 appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
5883 pnspname, prsname);
5884 else
5885 appendPQExpBuffer(&title, _("\nParser: \"%s\""),
5886 prsname);
5887
5888 myopt.title = title.data;
5889 myopt.footers = NULL;
5890 myopt.topt.default_footer = false;
5891 myopt.translate_header = true;
5892
5893 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5894
5895 termPQExpBuffer(&title);
5896
5897 PQclear(res);
5898 return true;
5899}
5900
5901
5902/*
5903 * \dew
5904 *
5905 * Describes foreign-data wrappers
5906 */
5907bool
5908listForeignDataWrappers(const char *pattern, bool verbose)
5909{
5911 PGresult *res;
5912 printQueryOpt myopt = pset.popt;
5913
5916 "SELECT fdw.fdwname AS \"%s\",\n"
5917 " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n"
5918 " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n"
5919 " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
5920 gettext_noop("Name"),
5921 gettext_noop("Owner"),
5922 gettext_noop("Handler"),
5923 gettext_noop("Validator"));
5924
5925 if (verbose)
5926 {
5927 appendPQExpBufferStr(&buf, ",\n ");
5928 printACLColumn(&buf, "fdwacl");
5930 ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
5931 " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5932 " pg_catalog.quote_ident(option_name) || ' ' || "
5933 " pg_catalog.quote_literal(option_value) FROM "
5934 " pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
5935 " END AS \"%s\""
5936 ",\n d.description AS \"%s\" ",
5937 gettext_noop("FDW options"),
5938 gettext_noop("Description"));
5939 }
5940
5941 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
5942
5943 if (verbose)
5945 "LEFT JOIN pg_catalog.pg_description d\n"
5946 " ON d.classoid = fdw.tableoid "
5947 "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
5948
5949 if (!validateSQLNamePattern(&buf, pattern, false, false,
5950 NULL, "fdwname", NULL, NULL,
5951 NULL, 1))
5952 {
5954 return false;
5955 }
5956
5957 appendPQExpBufferStr(&buf, "ORDER BY 1;");
5958
5959 res = PSQLexec(buf.data);
5961 if (!res)
5962 return false;
5963
5964 myopt.title = _("List of foreign-data wrappers");
5965 myopt.translate_header = true;
5966
5967 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5968
5969 PQclear(res);
5970 return true;
5971}
5972
5973/*
5974 * \des
5975 *
5976 * Describes foreign servers.
5977 */
5978bool
5979listForeignServers(const char *pattern, bool verbose)
5980{
5982 PGresult *res;
5983 printQueryOpt myopt = pset.popt;
5984
5987 "SELECT s.srvname AS \"%s\",\n"
5988 " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
5989 " f.fdwname AS \"%s\"",
5990 gettext_noop("Name"),
5991 gettext_noop("Owner"),
5992 gettext_noop("Foreign-data wrapper"));
5993
5994 if (verbose)
5995 {
5996 appendPQExpBufferStr(&buf, ",\n ");
5997 printACLColumn(&buf, "s.srvacl");
5999 ",\n"
6000 " s.srvtype AS \"%s\",\n"
6001 " s.srvversion AS \"%s\",\n"
6002 " CASE WHEN srvoptions IS NULL THEN '' ELSE "
6003 " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
6004 " pg_catalog.quote_ident(option_name) || ' ' || "
6005 " pg_catalog.quote_literal(option_value) FROM "
6006 " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
6007 " END AS \"%s\",\n"
6008 " d.description AS \"%s\"",
6009 gettext_noop("Type"),
6010 gettext_noop("Version"),
6011 gettext_noop("FDW options"),
6012 gettext_noop("Description"));
6013 }
6014
6016 "\nFROM pg_catalog.pg_foreign_server s\n"
6017 " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
6018
6019 if (verbose)
6021 "LEFT JOIN pg_catalog.pg_description d\n "
6022 "ON d.classoid = s.tableoid AND d.objoid = s.oid "
6023 "AND d.objsubid = 0\n");
6024
6025 if (!validateSQLNamePattern(&buf, pattern, false, false,
6026 NULL, "s.srvname", NULL, NULL,
6027 NULL, 1))
6028 {
6030 return false;
6031 }
6032
6033 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6034
6035 res = PSQLexec(buf.data);
6037 if (!res)
6038 return false;
6039
6040 myopt.title = _("List of foreign servers");
6041 myopt.translate_header = true;
6042
6043 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6044
6045 PQclear(res);
6046 return true;
6047}
6048
6049/*
6050 * \deu
6051 *
6052 * Describes user mappings.
6053 */
6054bool
6055listUserMappings(const char *pattern, bool verbose)
6056{
6058 PGresult *res;
6059 printQueryOpt myopt = pset.popt;
6060
6063 "SELECT um.srvname AS \"%s\",\n"
6064 " um.usename AS \"%s\"",
6065 gettext_noop("Server"),
6066 gettext_noop("User name"));
6067
6068 if (verbose)
6070 ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
6071 " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
6072 " pg_catalog.quote_ident(option_name) || ' ' || "
6073 " pg_catalog.quote_literal(option_value) FROM "
6074 " pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
6075 " END AS \"%s\"",
6076 gettext_noop("FDW options"));
6077
6078 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
6079
6080 if (!validateSQLNamePattern(&buf, pattern, false, false,
6081 NULL, "um.srvname", "um.usename", NULL,
6082 NULL, 1))
6083 {
6085 return false;
6086 }
6087
6088 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6089
6090 res = PSQLexec(buf.data);
6092 if (!res)
6093 return false;
6094
6095 myopt.title = _("List of user mappings");
6096 myopt.translate_header = true;
6097
6098 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6099
6100 PQclear(res);
6101 return true;
6102}
6103
6104/*
6105 * \det
6106 *
6107 * Describes foreign tables.
6108 */
6109bool
6110listForeignTables(const char *pattern, bool verbose)
6111{
6113 PGresult *res;
6114 printQueryOpt myopt = pset.popt;
6115
6118 "SELECT n.nspname AS \"%s\",\n"
6119 " c.relname AS \"%s\",\n"
6120 " s.srvname AS \"%s\"",
6121 gettext_noop("Schema"),
6122 gettext_noop("Table"),
6123 gettext_noop("Server"));
6124
6125 if (verbose)
6127 ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
6128 " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
6129 " pg_catalog.quote_ident(option_name) || ' ' || "
6130 " pg_catalog.quote_literal(option_value) FROM "
6131 " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
6132 " END AS \"%s\",\n"
6133 " d.description AS \"%s\"",
6134 gettext_noop("FDW options"),
6135 gettext_noop("Description"));
6136
6138 "\nFROM pg_catalog.pg_foreign_table ft\n"
6139 " INNER JOIN pg_catalog.pg_class c"
6140 " ON c.oid = ft.ftrelid\n"
6141 " INNER JOIN pg_catalog.pg_namespace n"
6142 " ON n.oid = c.relnamespace\n"
6143 " INNER JOIN pg_catalog.pg_foreign_server s"
6144 " ON s.oid = ft.ftserver\n");
6145 if (verbose)
6147 " LEFT JOIN pg_catalog.pg_description d\n"
6148 " ON d.classoid = c.tableoid AND "
6149 "d.objoid = c.oid AND d.objsubid = 0\n");
6150
6151 if (!validateSQLNamePattern(&buf, pattern, false, false,
6152 "n.nspname", "c.relname", NULL,
6153 "pg_catalog.pg_table_is_visible(c.oid)",
6154 NULL, 3))
6155 {
6157 return false;
6158 }
6159
6160 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6161
6162 res = PSQLexec(buf.data);
6164 if (!res)
6165 return false;
6166
6167 myopt.title = _("List of foreign tables");
6168 myopt.translate_header = true;
6169
6170 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6171
6172 PQclear(res);
6173 return true;
6174}
6175
6176/*
6177 * \dx
6178 *
6179 * Briefly describes installed extensions.
6180 */
6181bool
6182listExtensions(const char *pattern)
6183{
6185 PGresult *res;
6186 printQueryOpt myopt = pset.popt;
6187
6190 "SELECT e.extname AS \"%s\", "
6191 "e.extversion AS \"%s\", ae.default_version AS \"%s\","
6192 "n.nspname AS \"%s\", d.description AS \"%s\"\n"
6193 "FROM pg_catalog.pg_extension e "
6194 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
6195 "LEFT JOIN pg_catalog.pg_description d ON d.objoid = e.oid "
6196 "LEFT JOIN pg_catalog.pg_available_extensions() ae(name, default_version, comment) ON ae.name = e.extname "
6197 "AND d.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
6198 gettext_noop("Name"),
6199 gettext_noop("Version"),
6200 gettext_noop("Default version"),
6201 gettext_noop("Schema"),
6202 gettext_noop("Description"));
6203
6204 if (!validateSQLNamePattern(&buf, pattern,
6205 false, false,
6206 NULL, "e.extname", NULL,
6207 NULL,
6208 NULL, 1))
6209 {
6211 return false;
6212 }
6213
6214 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6215
6216 res = PSQLexec(buf.data);
6218 if (!res)
6219 return false;
6220
6221 myopt.title = _("List of installed extensions");
6222 myopt.translate_header = true;
6223
6224 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6225
6226 PQclear(res);
6227 return true;
6228}
6229
6230/*
6231 * \dx+
6232 *
6233 * List contents of installed extensions.
6234 */
6235bool
6236listExtensionContents(const char *pattern)
6237{
6239 PGresult *res;
6240 int i;
6241
6244 "SELECT e.extname, e.oid\n"
6245 "FROM pg_catalog.pg_extension e\n");
6246
6247 if (!validateSQLNamePattern(&buf, pattern,
6248 false, false,
6249 NULL, "e.extname", NULL,
6250 NULL,
6251 NULL, 1))
6252 {
6254 return false;
6255 }
6256
6257 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6258
6259 res = PSQLexec(buf.data);
6261 if (!res)
6262 return false;
6263
6264 if (PQntuples(res) == 0)
6265 {
6266 if (!pset.quiet)
6267 {
6268 if (pattern)
6269 pg_log_error("Did not find any extension named \"%s\".",
6270 pattern);
6271 else
6272 pg_log_error("Did not find any extensions.");
6273 }
6274 PQclear(res);
6275 return false;
6276 }
6277
6278 for (i = 0; i < PQntuples(res); i++)
6279 {
6280 const char *extname;
6281 const char *oid;
6282
6283 extname = PQgetvalue(res, i, 0);
6284 oid = PQgetvalue(res, i, 1);
6285
6286 if (!listOneExtensionContents(extname, oid))
6287 {
6288 PQclear(res);
6289 return false;
6290 }
6291 if (cancel_pressed)
6292 {
6293 PQclear(res);
6294 return false;
6295 }
6296 }
6297
6298 PQclear(res);
6299 return true;
6300}
6301
6302static bool
6303listOneExtensionContents(const char *extname, const char *oid)
6304{
6306 PGresult *res;
6307 PQExpBufferData title;
6308 printQueryOpt myopt = pset.popt;
6309
6312 "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
6313 "FROM pg_catalog.pg_depend\n"
6314 "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
6315 "ORDER BY 1;",
6316 gettext_noop("Object description"),
6317 oid);
6318
6319 res = PSQLexec(buf.data);
6321 if (!res)
6322 return false;
6323
6324 initPQExpBuffer(&title);
6325 printfPQExpBuffer(&title, _("Objects in extension \"%s\""), extname);
6326 myopt.title = title.data;
6327 myopt.translate_header = true;
6328
6329 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6330
6331 termPQExpBuffer(&title);
6332 PQclear(res);
6333 return true;
6334}
6335
6336/*
6337 * validateSQLNamePattern
6338 *
6339 * Wrapper around string_utils's processSQLNamePattern which also checks the
6340 * pattern's validity. In addition to that function's parameters, takes a
6341 * 'maxparts' parameter specifying the maximum number of dotted names the
6342 * pattern is allowed to have, and a 'added_clause' parameter that returns by
6343 * reference whether a clause was added to 'buf'. Returns whether the pattern
6344 * passed validation, after logging any errors.
6345 */
6346static bool
6347validateSQLNamePattern(PQExpBuffer buf, const char *pattern, bool have_where,
6348 bool force_escape, const char *schemavar,
6349 const char *namevar, const char *altnamevar,
6350 const char *visibilityrule, bool *added_clause,
6351 int maxparts)
6352{
6353 PQExpBufferData dbbuf;
6354 int dotcnt;
6355 bool added;
6356
6357 initPQExpBuffer(&dbbuf);
6358 added = processSQLNamePattern(pset.db, buf, pattern, have_where, force_escape,
6359 schemavar, namevar, altnamevar,
6360 visibilityrule, &dbbuf, &dotcnt);
6361 if (added_clause != NULL)
6362 *added_clause = added;
6363
6364 if (dotcnt >= maxparts)
6365 {
6366 pg_log_error("improper qualified name (too many dotted names): %s",
6367 pattern);
6368 goto error_return;
6369 }
6370
6371 if (maxparts > 1 && dotcnt == maxparts - 1)
6372 {
6373 if (PQdb(pset.db) == NULL)
6374 {
6375 pg_log_error("You are currently not connected to a database.");
6376 goto error_return;
6377 }
6378 if (strcmp(PQdb(pset.db), dbbuf.data) != 0)
6379 {
6380 pg_log_error("cross-database references are not implemented: %s",
6381 pattern);
6382 goto error_return;
6383 }
6384 }
6385 termPQExpBuffer(&dbbuf);
6386 return true;
6387
6388error_return:
6389 termPQExpBuffer(&dbbuf);
6390 return false;
6391}
6392
6393/*
6394 * \dRp
6395 * Lists publications.
6396 *
6397 * Takes an optional regexp to select particular publications
6398 */
6399bool
6400listPublications(const char *pattern)
6401{
6403 PGresult *res;
6404 printQueryOpt myopt = pset.popt;
6405 static const bool translate_columns[] = {false, false, false, false, false, false, false, false, false};
6406
6407 if (pset.sversion < 100000)
6408 {
6409 char sverbuf[32];
6410
6411 pg_log_error("The server (version %s) does not support publications.",
6413 sverbuf, sizeof(sverbuf)));
6414 return true;
6415 }
6416
6418
6420 "SELECT pubname AS \"%s\",\n"
6421 " pg_catalog.pg_get_userbyid(pubowner) AS \"%s\",\n"
6422 " puballtables AS \"%s\",\n"
6423 " pubinsert AS \"%s\",\n"
6424 " pubupdate AS \"%s\",\n"
6425 " pubdelete AS \"%s\"",
6426 gettext_noop("Name"),
6427 gettext_noop("Owner"),
6428 gettext_noop("All tables"),
6429 gettext_noop("Inserts"),
6430 gettext_noop("Updates"),
6431 gettext_noop("Deletes"));
6432 if (pset.sversion >= 110000)
6434 ",\n pubtruncate AS \"%s\"",
6435 gettext_noop("Truncates"));
6436 if (pset.sversion >= 180000)
6438 ",\n (CASE pubgencols\n"
6439 " WHEN '%c' THEN 'none'\n"
6440 " WHEN '%c' THEN 'stored'\n"
6441 " END) AS \"%s\"",
6442 PUBLISH_GENCOLS_NONE,
6443 PUBLISH_GENCOLS_STORED,
6444 gettext_noop("Generated columns"));
6445 if (pset.sversion >= 130000)
6447 ",\n pubviaroot AS \"%s\"",
6448 gettext_noop("Via root"));
6449
6451 "\nFROM pg_catalog.pg_publication\n");
6452
6453 if (!validateSQLNamePattern(&buf, pattern, false, false,
6454 NULL, "pubname", NULL,
6455 NULL,
6456 NULL, 1))
6457 {
6459 return false;
6460 }
6461
6462 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6463
6464 res = PSQLexec(buf.data);
6466 if (!res)
6467 return false;
6468
6469 myopt.title = _("List of publications");
6470 myopt.translate_header = true;
6471 myopt.translate_columns = translate_columns;
6472 myopt.n_translate_columns = lengthof(translate_columns);
6473
6474 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6475
6476 PQclear(res);
6477
6478 return true;
6479}
6480
6481/*
6482 * Add footer to publication description.
6483 */
6484static bool
6486 bool as_schema, printTableContent *const cont)
6487{
6488 PGresult *res;
6489 int count = 0;
6490 int i = 0;
6491
6492 res = PSQLexec(buf->data);
6493 if (!res)
6494 return false;
6495 else
6496 count = PQntuples(res);
6497
6498 if (count > 0)
6499 printTableAddFooter(cont, footermsg);
6500
6501 for (i = 0; i < count; i++)
6502 {
6503 if (as_schema)
6504 printfPQExpBuffer(buf, " \"%s\"", PQgetvalue(res, i, 0));
6505 else
6506 {
6507 printfPQExpBuffer(buf, " \"%s.%s\"", PQgetvalue(res, i, 0),
6508 PQgetvalue(res, i, 1));
6509
6510 if (!PQgetisnull(res, i, 3))
6511 appendPQExpBuffer(buf, " (%s)", PQgetvalue(res, i, 3));
6512
6513 if (!PQgetisnull(res, i, 2))
6514 appendPQExpBuffer(buf, " WHERE %s", PQgetvalue(res, i, 2));
6515 }
6516
6517 printTableAddFooter(cont, buf->data);
6518 }
6519
6520 PQclear(res);
6521 return true;
6522}
6523
6524/*
6525 * \dRp+
6526 * Describes publications including the contents.
6527 *
6528 * Takes an optional regexp to select particular publications
6529 */
6530bool
6531describePublications(const char *pattern)
6532{
6534 int i;
6535 PGresult *res;
6536 bool has_pubtruncate;
6537 bool has_pubgencols;
6538 bool has_pubviaroot;
6539
6540 PQExpBufferData title;
6541 printTableContent cont;
6542
6543 if (pset.sversion < 100000)
6544 {
6545 char sverbuf[32];
6546
6547 pg_log_error("The server (version %s) does not support publications.",
6549 sverbuf, sizeof(sverbuf)));
6550 return true;
6551 }
6552
6553 has_pubtruncate = (pset.sversion >= 110000);
6554 has_pubgencols = (pset.sversion >= 180000);
6555 has_pubviaroot = (pset.sversion >= 130000);
6556
6558
6560 "SELECT oid, pubname,\n"
6561 " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
6562 " puballtables, pubinsert, pubupdate, pubdelete");
6563 if (has_pubtruncate)
6565 ", pubtruncate");
6566 else
6568 ", false AS pubtruncate");
6569
6570 if (has_pubgencols)
6572 ", (CASE pubgencols\n"
6573 " WHEN '%c' THEN 'none'\n"
6574 " WHEN '%c' THEN 'stored'\n"
6575 " END) AS \"%s\"\n",
6576 PUBLISH_GENCOLS_NONE,
6577 PUBLISH_GENCOLS_STORED,
6578 gettext_noop("Generated columns"));
6579 else
6581 ", 'none' AS pubgencols");
6582
6583 if (has_pubviaroot)
6585 ", pubviaroot");
6586 else
6588 ", false AS pubviaroot");
6589
6591 "\nFROM pg_catalog.pg_publication\n");
6592
6593 if (!validateSQLNamePattern(&buf, pattern, false, false,
6594 NULL, "pubname", NULL,
6595 NULL,
6596 NULL, 1))
6597 {
6599 return false;
6600 }
6601
6602 appendPQExpBufferStr(&buf, "ORDER BY 2;");
6603
6604 res = PSQLexec(buf.data);
6605 if (!res)
6606 {
6608 return false;
6609 }
6610
6611 if (PQntuples(res) == 0)
6612 {
6613 if (!pset.quiet)
6614 {
6615 if (pattern)
6616 pg_log_error("Did not find any publication named \"%s\".",
6617 pattern);
6618 else
6619 pg_log_error("Did not find any publications.");
6620 }
6621
6623 PQclear(res);
6624 return false;
6625 }
6626
6627 for (i = 0; i < PQntuples(res); i++)
6628 {
6629 const char align = 'l';
6630 int ncols = 5;
6631 int nrows = 1;
6632 char *pubid = PQgetvalue(res, i, 0);
6633 char *pubname = PQgetvalue(res, i, 1);
6634 bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
6635 printTableOpt myopt = pset.popt.topt;
6636
6637 if (has_pubtruncate)
6638 ncols++;
6639 if (has_pubgencols)
6640 ncols++;
6641 if (has_pubviaroot)
6642 ncols++;
6643
6644 initPQExpBuffer(&title);
6645 printfPQExpBuffer(&title, _("Publication %s"), pubname);
6646 printTableInit(&cont, &myopt, title.data, ncols, nrows);
6647
6648 printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
6649 printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
6650 printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
6651 printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
6652 printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
6653 if (has_pubtruncate)
6654 printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
6655 if (has_pubgencols)
6656 printTableAddHeader(&cont, gettext_noop("Generated columns"), true, align);
6657 if (has_pubviaroot)
6658 printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
6659
6660 printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
6661 printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
6662 printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
6663 printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
6664 printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
6665 if (has_pubtruncate)
6666 printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
6667 if (has_pubgencols)
6668 printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
6669 if (has_pubviaroot)
6670 printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
6671
6672 if (!puballtables)
6673 {
6674 /* Get the tables for the specified publication */
6676 "SELECT n.nspname, c.relname");
6677 if (pset.sversion >= 150000)
6678 {
6680 ", pg_get_expr(pr.prqual, c.oid)");
6682 ", (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
6683 " pg_catalog.array_to_string("
6684 " ARRAY(SELECT attname\n"
6685 " FROM\n"
6686 " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
6687 " pg_catalog.pg_attribute\n"
6688 " WHERE attrelid = c.oid AND attnum = prattrs[s]), ', ')\n"
6689 " ELSE NULL END)");
6690 }
6691 else
6693 ", NULL, NULL");
6695 "\nFROM pg_catalog.pg_class c,\n"
6696 " pg_catalog.pg_namespace n,\n"
6697 " pg_catalog.pg_publication_rel pr\n"
6698 "WHERE c.relnamespace = n.oid\n"
6699 " AND c.oid = pr.prrelid\n"
6700 " AND pr.prpubid = '%s'\n"
6701 "ORDER BY 1,2", pubid);
6702 if (!addFooterToPublicationDesc(&buf, _("Tables:"), false, &cont))
6703 goto error_return;
6704
6705 if (pset.sversion >= 150000)
6706 {
6707 /* Get the schemas for the specified publication */
6709 "SELECT n.nspname\n"
6710 "FROM pg_catalog.pg_namespace n\n"
6711 " JOIN pg_catalog.pg_publication_namespace pn ON n.oid = pn.pnnspid\n"
6712 "WHERE pn.pnpubid = '%s'\n"
6713 "ORDER BY 1", pubid);
6714 if (!addFooterToPublicationDesc(&buf, _("Tables from schemas:"),
6715 true, &cont))
6716 goto error_return;
6717 }
6718 }
6719
6720 printTable(&cont, pset.queryFout, false, pset.logfile);
6721 printTableCleanup(&cont);
6722
6723 termPQExpBuffer(&title);
6724 }
6725
6727 PQclear(res);
6728
6729 return true;
6730
6731error_return:
6732 printTableCleanup(&cont);
6733 PQclear(res);
6735 termPQExpBuffer(&title);
6736 return false;
6737}
6738
6739/*
6740 * \dRs
6741 * Describes subscriptions.
6742 *
6743 * Takes an optional regexp to select particular subscriptions
6744 */
6745bool
6746describeSubscriptions(const char *pattern, bool verbose)
6747{
6749 PGresult *res;
6750 printQueryOpt myopt = pset.popt;
6751 static const bool translate_columns[] = {false, false, false, false,
6752 false, false, false, false, false, false, false, false, false, false,
6753 false};
6754
6755 if (pset.sversion < 100000)
6756 {
6757 char sverbuf[32];
6758
6759 pg_log_error("The server (version %s) does not support subscriptions.",
6761 sverbuf, sizeof(sverbuf)));
6762 return true;
6763 }
6764
6766
6768 "SELECT subname AS \"%s\"\n"
6769 ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
6770 ", subenabled AS \"%s\"\n"
6771 ", subpublications AS \"%s\"\n",
6772 gettext_noop("Name"),
6773 gettext_noop("Owner"),
6774 gettext_noop("Enabled"),
6775 gettext_noop("Publication"));
6776
6777 if (verbose)
6778 {
6779 /* Binary mode and streaming are only supported in v14 and higher */
6780 if (pset.sversion >= 140000)
6781 {
6783 ", subbinary AS \"%s\"\n",
6784 gettext_noop("Binary"));
6785
6786 if (pset.sversion >= 160000)
6788 ", (CASE substream\n"
6789 " WHEN " CppAsString2(LOGICALREP_STREAM_OFF) " THEN 'off'\n"
6790 " WHEN " CppAsString2(LOGICALREP_STREAM_ON) " THEN 'on'\n"
6791 " WHEN " CppAsString2(LOGICALREP_STREAM_PARALLEL) " THEN 'parallel'\n"
6792 " END) AS \"%s\"\n",
6793 gettext_noop("Streaming"));
6794 else
6796 ", substream AS \"%s\"\n",
6797 gettext_noop("Streaming"));
6798 }
6799
6800 /* Two_phase and disable_on_error are only supported in v15 and higher */
6801 if (pset.sversion >= 150000)
6803 ", subtwophasestate AS \"%s\"\n"
6804 ", subdisableonerr AS \"%s\"\n",
6805 gettext_noop("Two-phase commit"),
6806 gettext_noop("Disable on error"));
6807
6808 if (pset.sversion >= 160000)
6810 ", suborigin AS \"%s\"\n"
6811 ", subpasswordrequired AS \"%s\"\n"
6812 ", subrunasowner AS \"%s\"\n",
6813 gettext_noop("Origin"),
6814 gettext_noop("Password required"),
6815 gettext_noop("Run as owner?"));
6816
6817 if (pset.sversion >= 170000)
6819 ", subfailover AS \"%s\"\n",
6820 gettext_noop("Failover"));
6821
6823 ", subsynccommit AS \"%s\"\n"
6824 ", subconninfo AS \"%s\"\n",
6825 gettext_noop("Synchronous commit"),
6826 gettext_noop("Conninfo"));
6827
6828 /* Skip LSN is only supported in v15 and higher */
6829 if (pset.sversion >= 150000)
6831 ", subskiplsn AS \"%s\"\n",
6832 gettext_noop("Skip LSN"));
6833 }
6834
6835 /* Only display subscriptions in current database. */
6837 "FROM pg_catalog.pg_subscription\n"
6838 "WHERE subdbid = (SELECT oid\n"
6839 " FROM pg_catalog.pg_database\n"
6840 " WHERE datname = pg_catalog.current_database())");
6841
6842 if (!validateSQLNamePattern(&buf, pattern, true, false,
6843 NULL, "subname", NULL,
6844 NULL,
6845 NULL, 1))
6846 {
6848 return false;
6849 }
6850
6851 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6852
6853 res = PSQLexec(buf.data);
6855 if (!res)
6856 return false;
6857
6858 myopt.title = _("List of subscriptions");
6859 myopt.translate_header = true;
6860 myopt.translate_columns = translate_columns;
6861 myopt.n_translate_columns = lengthof(translate_columns);
6862
6863 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6864
6865 PQclear(res);
6866 return true;
6867}
6868
6869/*
6870 * printACLColumn
6871 *
6872 * Helper function for consistently formatting ACL (privilege) columns.
6873 * The proper targetlist entry is appended to buf. Note lack of any
6874 * whitespace or comma decoration.
6875 *
6876 * If you change this, see also the handling of attacl in permissionsList(),
6877 * which can't conveniently use this code.
6878 */
6879static void
6880printACLColumn(PQExpBuffer buf, const char *colname)
6881{
6883 "CASE"
6884 " WHEN pg_catalog.array_length(%s, 1) = 0 THEN '%s'"
6885 " ELSE pg_catalog.array_to_string(%s, E'\\n')"
6886 " END AS \"%s\"",
6887 colname, gettext_noop("(none)"),
6888 colname, gettext_noop("Access privileges"));
6889}
6890
6891/*
6892 * \dAc
6893 * Lists operator classes
6894 *
6895 * Takes optional regexps to filter by index access method and input data type.
6896 */
6897bool
6898listOperatorClasses(const char *access_method_pattern,
6899 const char *type_pattern, bool verbose)
6900{
6902 PGresult *res;
6903 printQueryOpt myopt = pset.popt;
6904 bool have_where = false;
6905 static const bool translate_columns[] = {false, false, false, false, false, false, false};
6906
6908
6910 "SELECT\n"
6911 " am.amname AS \"%s\",\n"
6912 " pg_catalog.format_type(c.opcintype, NULL) AS \"%s\",\n"
6913 " CASE\n"
6914 " WHEN c.opckeytype <> 0 AND c.opckeytype <> c.opcintype\n"
6915 " THEN pg_catalog.format_type(c.opckeytype, NULL)\n"
6916 " ELSE NULL\n"
6917 " END AS \"%s\",\n"
6918 " CASE\n"
6919 " WHEN pg_catalog.pg_opclass_is_visible(c.oid)\n"
6920 " THEN pg_catalog.format('%%I', c.opcname)\n"
6921 " ELSE pg_catalog.format('%%I.%%I', n.nspname, c.opcname)\n"
6922 " END AS \"%s\",\n"
6923 " (CASE WHEN c.opcdefault\n"
6924 " THEN '%s'\n"
6925 " ELSE '%s'\n"
6926 " END) AS \"%s\"",
6927 gettext_noop("AM"),
6928 gettext_noop("Input type"),
6929 gettext_noop("Storage type"),
6930 gettext_noop("Operator class"),
6931 gettext_noop("yes"),
6932 gettext_noop("no"),
6933 gettext_noop("Default?"));
6934 if (verbose)
6936 ",\n CASE\n"
6937 " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
6938 " THEN pg_catalog.format('%%I', of.opfname)\n"
6939 " ELSE pg_catalog.format('%%I.%%I', ofn.nspname, of.opfname)\n"
6940 " END AS \"%s\",\n"
6941 " pg_catalog.pg_get_userbyid(c.opcowner) AS \"%s\"\n",
6942 gettext_noop("Operator family"),
6943 gettext_noop("Owner"));
6945 "\nFROM pg_catalog.pg_opclass c\n"
6946 " LEFT JOIN pg_catalog.pg_am am on am.oid = c.opcmethod\n"
6947 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.opcnamespace\n"
6948 " LEFT JOIN pg_catalog.pg_type t ON t.oid = c.opcintype\n"
6949 " LEFT JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace\n");
6950 if (verbose)
6952 " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = c.opcfamily\n"
6953 " LEFT JOIN pg_catalog.pg_namespace ofn ON ofn.oid = of.opfnamespace\n");
6954
6955 if (access_method_pattern)
6956 if (!validateSQLNamePattern(&buf, access_method_pattern,
6957 false, false, NULL, "am.amname", NULL, NULL,
6958 &have_where, 1))
6959 goto error_return;
6960 if (type_pattern)
6961 {
6962 /* Match type name pattern against either internal or external name */
6963 if (!validateSQLNamePattern(&buf, type_pattern, have_where, false,
6964 "tn.nspname", "t.typname",
6965 "pg_catalog.format_type(t.oid, NULL)",
6966 "pg_catalog.pg_type_is_visible(t.oid)",
6967 NULL, 3))
6968 goto error_return;
6969 }
6970
6971 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
6972 res = PSQLexec(buf.data);
6974 if (!res)
6975 return false;
6976
6977 myopt.title = _("List of operator classes");
6978 myopt.translate_header = true;
6979 myopt.translate_columns = translate_columns;
6980 myopt.n_translate_columns = lengthof(translate_columns);
6981
6982 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6983
6984 PQclear(res);
6985 return true;
6986
6987error_return:
6989 return false;
6990}
6991
6992/*
6993 * \dAf
6994 * Lists operator families
6995 *
6996 * Takes optional regexps to filter by index access method and input data type.
6997 */
6998bool
6999listOperatorFamilies(const char *access_method_pattern,
7000 const char *type_pattern, bool verbose)
7001{
7003 PGresult *res;
7004 printQueryOpt myopt = pset.popt;
7005 bool have_where = false;
7006 static const bool translate_columns[] = {false, false, false, false};
7007
7009
7011 "SELECT\n"
7012 " am.amname AS \"%s\",\n"
7013 " CASE\n"
7014 " WHEN pg_catalog.pg_opfamily_is_visible(f.oid)\n"
7015 " THEN pg_catalog.format('%%I', f.opfname)\n"
7016 " ELSE pg_catalog.format('%%I.%%I', n.nspname, f.opfname)\n"
7017 " END AS \"%s\",\n"
7018 " (SELECT\n"
7019 " pg_catalog.string_agg(pg_catalog.format_type(oc.opcintype, NULL), ', ')\n"
7020 " FROM pg_catalog.pg_opclass oc\n"
7021 " WHERE oc.opcfamily = f.oid) \"%s\"",
7022 gettext_noop("AM"),
7023 gettext_noop("Operator family"),
7024 gettext_noop("Applicable types"));
7025 if (verbose)
7027 ",\n pg_catalog.pg_get_userbyid(f.opfowner) AS \"%s\"\n",
7028 gettext_noop("Owner"));
7030 "\nFROM pg_catalog.pg_opfamily f\n"
7031 " LEFT JOIN pg_catalog.pg_am am on am.oid = f.opfmethod\n"
7032 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = f.opfnamespace\n");
7033
7034 if (access_method_pattern)
7035 if (!validateSQLNamePattern(&buf, access_method_pattern,
7036 false, false, NULL, "am.amname", NULL, NULL,
7037 &have_where, 1))
7038 goto error_return;
7039 if (type_pattern)
7040 {
7042 " %s EXISTS (\n"
7043 " SELECT 1\n"
7044 " FROM pg_catalog.pg_type t\n"
7045 " JOIN pg_catalog.pg_opclass oc ON oc.opcintype = t.oid\n"
7046 " LEFT JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace\n"
7047 " WHERE oc.opcfamily = f.oid\n",
7048 have_where ? "AND" : "WHERE");
7049 /* Match type name pattern against either internal or external name */
7050 if (!validateSQLNamePattern(&buf, type_pattern, true, false,
7051 "tn.nspname", "t.typname",
7052 "pg_catalog.format_type(t.oid, NULL)",
7053 "pg_catalog.pg_type_is_visible(t.oid)",
7054 NULL, 3))
7055 goto error_return;
7056 appendPQExpBufferStr(&buf, " )\n");
7057 }
7058
7059 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
7060 res = PSQLexec(buf.data);
7062 if (!res)
7063 return false;
7064
7065 myopt.title = _("List of operator families");
7066 myopt.translate_header = true;
7067 myopt.translate_columns = translate_columns;
7068 myopt.n_translate_columns = lengthof(translate_columns);
7069
7070 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7071
7072 PQclear(res);
7073 return true;
7074
7075error_return:
7077 return false;
7078}
7079
7080/*
7081 * \dAo
7082 * Lists operators of operator families
7083 *
7084 * Takes optional regexps to filter by index access method and operator
7085 * family.
7086 */
7087bool
7088listOpFamilyOperators(const char *access_method_pattern,
7089 const char *family_pattern, bool verbose)
7090{
7092 PGresult *res;
7093 printQueryOpt myopt = pset.popt;
7094 bool have_where = false;
7095
7096 static const bool translate_columns[] = {false, false, false, false, false, false, true};
7097
7099
7101 "SELECT\n"
7102 " am.amname AS \"%s\",\n"
7103 " CASE\n"
7104 " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
7105 " THEN pg_catalog.format('%%I', of.opfname)\n"
7106 " ELSE pg_catalog.format('%%I.%%I', nsf.nspname, of.opfname)\n"
7107 " END AS \"%s\",\n"
7108 " o.amopopr::pg_catalog.regoperator AS \"%s\"\n,"
7109 " o.amopstrategy AS \"%s\",\n"
7110 " CASE o.amoppurpose\n"
7111 " WHEN " CppAsString2(AMOP_ORDER) " THEN '%s'\n"
7112 " WHEN " CppAsString2(AMOP_SEARCH) " THEN '%s'\n"
7113 " END AS \"%s\"\n",
7114 gettext_noop("AM"),
7115 gettext_noop("Operator family"),
7116 gettext_noop("Operator"),
7117 gettext_noop("Strategy"),
7118 gettext_noop("ordering"),
7119 gettext_noop("search"),
7120 gettext_noop("Purpose"));
7121
7122 if (verbose)
7124 ", ofs.opfname AS \"%s\",\n"
7125 " CASE\n"
7126 " WHEN p.proleakproof THEN '%s'\n"
7127 " ELSE '%s'\n"
7128 " END AS \"%s\"\n",
7129 gettext_noop("Sort opfamily"),
7130 gettext_noop("yes"),
7131 gettext_noop("no"),
7132 gettext_noop("Leakproof?"));
7134 "FROM pg_catalog.pg_amop o\n"
7135 " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = o.amopfamily\n"
7136 " LEFT JOIN pg_catalog.pg_am am ON am.oid = of.opfmethod AND am.oid = o.amopmethod\n"
7137 " LEFT JOIN pg_catalog.pg_namespace nsf ON of.opfnamespace = nsf.oid\n");
7138 if (verbose)
7140 " LEFT JOIN pg_catalog.pg_opfamily ofs ON ofs.oid = o.amopsortfamily\n"
7141 " LEFT JOIN pg_catalog.pg_operator op ON op.oid = o.amopopr\n"
7142 " LEFT JOIN pg_catalog.pg_proc p ON p.oid = op.oprcode\n");
7143
7144 if (access_method_pattern)
7145 {
7146 if (!validateSQLNamePattern(&buf, access_method_pattern,
7147 false, false, NULL, "am.amname",
7148 NULL, NULL,
7149 &have_where, 1))
7150 goto error_return;
7151 }
7152
7153 if (family_pattern)
7154 {
7155 if (!validateSQLNamePattern(&buf, family_pattern, have_where, false,
7156 "nsf.nspname", "of.opfname", NULL, NULL,
7157 NULL, 3))
7158 goto error_return;
7159 }
7160
7161 appendPQExpBufferStr(&buf, "ORDER BY 1, 2,\n"
7162 " o.amoplefttype = o.amoprighttype DESC,\n"
7163 " pg_catalog.format_type(o.amoplefttype, NULL),\n"
7164 " pg_catalog.format_type(o.amoprighttype, NULL),\n"
7165 " o.amopstrategy;");
7166
7167 res = PSQLexec(buf.data);
7169 if (!res)
7170 return false;
7171
7172 myopt.title = _("List of operators of operator families");
7173 myopt.translate_header = true;
7174 myopt.translate_columns = translate_columns;
7175 myopt.n_translate_columns = lengthof(translate_columns);
7176
7177 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7178
7179 PQclear(res);
7180 return true;
7181
7182error_return:
7184 return false;
7185}
7186
7187/*
7188 * \dAp
7189 * Lists support functions of operator families
7190 *
7191 * Takes optional regexps to filter by index access method and operator
7192 * family.
7193 */
7194bool
7195listOpFamilyFunctions(const char *access_method_pattern,
7196 const char *family_pattern, bool verbose)
7197{
7199 PGresult *res;
7200 printQueryOpt myopt = pset.popt;
7201 bool have_where = false;
7202 static const bool translate_columns[] = {false, false, false, false, false, false};
7203
7205
7207 "SELECT\n"
7208 " am.amname AS \"%s\",\n"
7209 " CASE\n"
7210 " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
7211 " THEN pg_catalog.format('%%I', of.opfname)\n"
7212 " ELSE pg_catalog.format('%%I.%%I', ns.nspname, of.opfname)\n"
7213 " END AS \"%s\",\n"
7214 " pg_catalog.format_type(ap.amproclefttype, NULL) AS \"%s\",\n"
7215 " pg_catalog.format_type(ap.amprocrighttype, NULL) AS \"%s\",\n"
7216 " ap.amprocnum AS \"%s\"\n",
7217 gettext_noop("AM"),
7218 gettext_noop("Operator family"),
7219 gettext_noop("Registered left type"),
7220 gettext_noop("Registered right type"),
7221 gettext_noop("Number"));
7222
7223 if (!verbose)
7225 ", p.proname AS \"%s\"\n",
7226 gettext_noop("Function"));
7227 else
7229 ", ap.amproc::pg_catalog.regprocedure AS \"%s\"\n",
7230 gettext_noop("Function"));
7231
7233 "FROM pg_catalog.pg_amproc ap\n"
7234 " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = ap.amprocfamily\n"
7235 " LEFT JOIN pg_catalog.pg_am am ON am.oid = of.opfmethod\n"
7236 " LEFT JOIN pg_catalog.pg_namespace ns ON of.opfnamespace = ns.oid\n"
7237 " LEFT JOIN pg_catalog.pg_proc p ON ap.amproc = p.oid\n");
7238
7239 if (access_method_pattern)
7240 {
7241 if (!validateSQLNamePattern(&buf, access_method_pattern,
7242 false, false, NULL, "am.amname",
7243 NULL, NULL,
7244 &have_where, 1))
7245 goto error_return;
7246 }
7247 if (family_pattern)
7248 {
7249 if (!validateSQLNamePattern(&buf, family_pattern, have_where, false,
7250 "ns.nspname", "of.opfname", NULL, NULL,
7251 NULL, 3))
7252 goto error_return;
7253 }
7254
7255 appendPQExpBufferStr(&buf, "ORDER BY 1, 2,\n"
7256 " ap.amproclefttype = ap.amprocrighttype DESC,\n"
7257 " 3, 4, 5;");
7258
7259 res = PSQLexec(buf.data);
7261 if (!res)
7262 return false;
7263
7264 myopt.title = _("List of support functions of operator families");
7265 myopt.translate_header = true;
7266 myopt.translate_columns = translate_columns;
7267 myopt.n_translate_columns = lengthof(translate_columns);
7268
7269 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7270
7271 PQclear(res);
7272 return true;
7273
7274error_return:
7276 return false;
7277}
7278
7279/*
7280 * \dl or \lo_list
7281 * Lists large objects
7282 */
7283bool
7285{
7287 PGresult *res;
7288 printQueryOpt myopt = pset.popt;
7289
7291
7293 "SELECT oid as \"%s\",\n"
7294 " pg_catalog.pg_get_userbyid(lomowner) as \"%s\",\n ",
7295 gettext_noop("ID"),
7296 gettext_noop("Owner"));
7297
7298 if (verbose)
7299 {
7300 printACLColumn(&buf, "lomacl");
7301 appendPQExpBufferStr(&buf, ",\n ");
7302 }
7303
7305 "pg_catalog.obj_description(oid, 'pg_largeobject') as \"%s\"\n"
7306 "FROM pg_catalog.pg_largeobject_metadata\n"
7307 "ORDER BY oid",
7308 gettext_noop("Description"));
7309
7310 res = PSQLexec(buf.data);
7312 if (!res)
7313 return false;
7314
7315 myopt.title = _("Large objects");
7316 myopt.translate_header = true;
7317
7318 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7319
7320 PQclear(res);
7321 return true;
7322}
PGresult * PSQLexec(const char *query)
Definition: common.c:655
#define ngettext(s, p, n)
Definition: c.h:1152
#define gettext_noop(x)
Definition: c.h:1167
int16_t int16
Definition: c.h:497
#define CppAsString2(x)
Definition: c.h:363
#define lengthof(array)
Definition: c.h:759
bool listUserMappings(const char *pattern, bool verbose)
Definition: describe.c:6055
bool listTSConfigs(const char *pattern, bool verbose)
Definition: describe.c:5704
bool describeRoles(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3716
bool listOpFamilyFunctions(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:7195
bool listPublications(const char *pattern)
Definition: describe.c:6400
bool listTSParsers(const char *pattern, bool verbose)
Definition: describe.c:5327
bool listExtensionContents(const char *pattern)
Definition: describe.c:6236
bool describeAggregates(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:78
bool listForeignDataWrappers(const char *pattern, bool verbose)
Definition: describe.c:5908
bool listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
Definition: describe.c:4266
bool describeSubscriptions(const char *pattern, bool verbose)
Definition: describe.c:6746
bool describeRoleGrants(const char *pattern, bool showSystem)
Definition: describe.c:3932
bool listExtendedStats(const char *pattern)
Definition: describe.c:4863
bool describeTypes(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:638
bool listOperatorFamilies(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6999
bool listForeignServers(const char *pattern, bool verbose)
Definition: describe.c:5979
bool describeTableDetails(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:1483
bool listDomains(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4552
bool listTSDictionaries(const char *pattern, bool verbose)
Definition: describe.c:5574
bool listDbRoleSettings(const char *pattern, const char *pattern2)
Definition: describe.c:3863
bool describeFunctions(const char *functypes, const char *func_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
Definition: describe.c:295
static void add_role_attribute(PQExpBuffer buf, const char *const str)
Definition: describe.c:3851
bool listCollations(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:5083
bool listSchemas(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:5206
bool listExtensions(const char *pattern)
Definition: describe.c:6182
static bool describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname, const char *pnspname, const char *prsname)
Definition: describe.c:5837
static bool listOneExtensionContents(const char *extname, const char *oid)
Definition: describe.c:6303
static bool describeOneTableDetails(const char *schemaname, const char *relationname, const char *oid, bool verbose)
Definition: describe.c:1566
static bool listTSParsersVerbose(const char *pattern)
Definition: describe.c:5379
bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4011
bool listTSTemplates(const char *pattern, bool verbose)
Definition: describe.c:5639
bool describeTablespaces(const char *pattern, bool verbose)
Definition: describe.c:222
bool listCasts(const char *pattern, bool verbose)
Definition: describe.c:4959
bool listOpFamilyOperators(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:7088
bool describeOperators(const char *oper_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
Definition: describe.c:793
bool listOperatorClasses(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6898
static const char * map_typename_pattern(const char *pattern)
Definition: describe.c:743
bool listForeignTables(const char *pattern, bool verbose)
Definition: describe.c:6110
bool describeConfigurationParameters(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4715
bool listEventTriggers(const char *pattern, bool verbose)
Definition: describe.c:4783
bool listDefaultACLs(const char *pattern)
Definition: describe.c:1213
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6880
static bool addFooterToPublicationDesc(PQExpBuffer buf, const char *footermsg, bool as_schema, printTableContent *const cont)
Definition: describe.c:6485
static bool validateSQLNamePattern(PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, bool *added_clause, int maxparts)
Definition: describe.c:6347
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:945
bool listConversions(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4635
bool permissionsList(const char *pattern, bool showSystem)
Definition: describe.c:1049
bool describeAccessMethods(const char *pattern, bool verbose)
Definition: describe.c:148
static bool listTSConfigsVerbose(const char *pattern)
Definition: describe.c:5753
bool listLargeObjects(bool verbose)
Definition: describe.c:7284
static bool describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
Definition: describe.c:5454
static void add_tablespace_footer(printTableContent *const cont, char relkind, Oid tablespace, const bool newline)
Definition: describe.c:3651
bool listLanguages(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4476
bool describePublications(const char *pattern)
Definition: describe.c:6531
bool objectDescription(const char *pattern, bool showSystem)
Definition: describe.c:1290
#define _(x)
Definition: elog.c:90
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7381
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
void PQclear(PGresult *res)
Definition: fe-exec.c:721
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3172
void printTableCleanup(printTableContent *const content)
Definition: print.c:3353
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3549
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3260
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3335
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3443
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3310
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3220
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
Assert(PointerIsAligned(start, uint64))
const char * str
#define free(a)
Definition: header.h:65
#define storage
Definition: indent_codes.h:68
#define newline
Definition: indent_codes.h:35
int verbose
int i
Definition: isn.c:74
static IsoConnInfo * conns
#define pg_log_error_internal(...)
Definition: logging.h:160
#define pg_log_error(...)
Definition: logging.h:106
int pg_wcswidth(const char *pwcs, size_t len, int encoding)
Definition: mbprint.c:177
NameData relname
Definition: pg_class.h:38
static char * buf
Definition: pg_test_fsync.c:72
NameData typname
Definition: pg_type.h:41
static char * tablespace
Definition: pgbench.c:217
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define snprintf
Definition: port.h:239
unsigned int Oid
Definition: postgres_ext.h:30
#define atooid(x)
Definition: postgres_ext.h:41
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
PsqlSettings pset
Definition: startup.c:32
const char * fmtId(const char *rawid)
Definition: string_utils.c:248
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:313
printQueryOpt popt
Definition: settings.h:112
bool hide_tableam
Definition: settings.h:171
bool hide_compression
Definition: settings.h:170
FILE * logfile
Definition: settings.h:149
PGconn * db
Definition: settings.h:103
FILE * queryFout
Definition: settings.h:105
const bool * translate_columns
Definition: print.h:190
printTableOpt topt
Definition: print.h:185
char * title
Definition: print.h:187
char ** footers
Definition: print.h:188
bool translate_header
Definition: print.h:189
int n_translate_columns
Definition: print.h:192
printTableFooter * footer
Definition: print.h:177
char * data
Definition: print.h:155
unsigned short int expanded
Definition: print.h:114
bool default_footer
Definition: print.h:129
static StringInfoData tmpbuf
Definition: walsender.c:171