Debian Codegraph @

src/bin/psql/describe.c

raw
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"
36 #include "fe_utils/string_utils.h"
37 #include "settings.h"
38
39 static const char *map_typename_pattern(const char *pattern);
40 static bool describeOneTableDetails(const char *schemaname,
41 const char *relationname,
42 const char *oid,
43 bool verbose);
44 static void add_tablespace_footer(printTableContent *const cont, char relkind,
45 Oid tablespace, const bool newline);
46 static void add_role_attribute(PQExpBuffer buf, const char *const str);
47 static bool listTSParsersVerbose(const char *pattern);
48 static bool describeOneTSParser(const char *oid, const char *nspname,
49 const char *prsname);
50 static bool listTSConfigsVerbose(const char *pattern);
51 static bool describeOneTSConfig(const char *oid, const char *nspname,
52 const char *cfgname,
53 const char *pnspname, const char *prsname);
54 static void printACLColumn(PQExpBuffer buf, const char *colname);
55 static bool listOneExtensionContents(const char *extname, const char *oid);
56 static 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 */
77 bool
78 describeAggregates(const char *pattern, bool verbose, bool showSystem)
79 {
80 PQExpBufferData buf;
81 PGresult *res;
82 printQueryOpt myopt = pset.popt;
83
84 initPQExpBuffer(&buf);
85
86 printfPQExpBuffer(&buf,
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)
100 appendPQExpBuffer(&buf,
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
107 appendPQExpBuffer(&buf,
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 {
123 termPQExpBuffer(&buf);
124 return false;
125 }
126
127 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
128
129 res = PSQLexec(buf.data);
130 termPQExpBuffer(&buf);
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 */
147 bool
148 describeAccessMethods(const char *pattern, bool verbose)
149 {
150 PQExpBufferData buf;
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.",
160 formatPGVersionNumber(pset.sversion, false,
161 sverbuf, sizeof(sverbuf)));
162 return true;
163 }
164
165 initPQExpBuffer(&buf);
166
167 printfPQExpBuffer(&buf,
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 {
180 appendPQExpBuffer(&buf,
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
187 appendPQExpBufferStr(&buf,
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 {
195 termPQExpBuffer(&buf);
196 return false;
197 }
198
199 appendPQExpBufferStr(&buf, "ORDER BY 1;");
200
201 res = PSQLexec(buf.data);
202 termPQExpBuffer(&buf);
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 */
221 bool
222 describeTablespaces(const char *pattern, bool verbose)
223 {
224 PQExpBufferData buf;
225 PGresult *res;
226 printQueryOpt myopt = pset.popt;
227
228 initPQExpBuffer(&buf);
229
230 printfPQExpBuffer(&buf,
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");
242 appendPQExpBuffer(&buf,
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
251 appendPQExpBufferStr(&buf,
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 {
259 termPQExpBuffer(&buf);
260 return false;
261 }
262
263 appendPQExpBufferStr(&buf, "ORDER BY 1;");
264
265 res = PSQLexec(buf.data);
266 termPQExpBuffer(&buf);
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 */
294 bool
295 describeFunctions(const char *functypes, const char *func_pattern,
296 char **arg_patterns, int num_arg_patterns,
297 bool verbose, bool showSystem)
298 {
299 const char *df_options = "anptwSx+";
300 bool showAggregate = strchr(functypes, 'a') != NULL;
301 bool showNormal = strchr(functypes, 'n') != NULL;
302 bool showProcedure = strchr(functypes, 'p') != NULL;
303 bool showTrigger = strchr(functypes, 't') != NULL;
304 bool showWindow = strchr(functypes, 'w') != NULL;
305 bool have_where;
306 PQExpBufferData buf;
307 PGresult *res;
308 printQueryOpt myopt = pset.popt;
309 static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, true, false, false, false, false};
310
311 /* No "Parallel" column before 9.6 */
312 static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, true, false, false, false, false};
313
314 if (strlen(functypes) != strspn(functypes, df_options))
315 {
316 pg_log_error("\\df only takes [%s] as options", df_options);
317 return true;
318 }
319
320 if (showProcedure && pset.sversion < 110000)
321 {
322 char sverbuf[32];
323
324 pg_log_error("\\df does not take a \"%c\" option with server version %s",
325 'p',
326 formatPGVersionNumber(pset.sversion, false,
327 sverbuf, sizeof(sverbuf)));
328 return true;
329 }
330
331 if (!showAggregate && !showNormal && !showProcedure && !showTrigger && !showWindow)
332 {
333 showAggregate = showNormal = showTrigger = showWindow = true;
334 if (pset.sversion >= 110000)
335 showProcedure = true;
336 }
337
338 initPQExpBuffer(&buf);
339
340 printfPQExpBuffer(&buf,
341 "SELECT n.nspname as \"%s\",\n"
342 " p.proname as \"%s\",\n",
343 gettext_noop("Schema"),
344 gettext_noop("Name"));
345
346 if (pset.sversion >= 110000)
347 appendPQExpBuffer(&buf,
348 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
349 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
350 " CASE p.prokind\n"
351 " WHEN " CppAsString2(PROKIND_AGGREGATE) " THEN '%s'\n"
352 " WHEN " CppAsString2(PROKIND_WINDOW) " THEN '%s'\n"
353 " WHEN " CppAsString2(PROKIND_PROCEDURE) " THEN '%s'\n"
354 " ELSE '%s'\n"
355 " END as \"%s\"",
356 gettext_noop("Result data type"),
357 gettext_noop("Argument data types"),
358 /* translator: "agg" is short for "aggregate" */
359 gettext_noop("agg"),
360 gettext_noop("window"),
361 gettext_noop("proc"),
362 gettext_noop("func"),
363 gettext_noop("Type"));
364 else
365 appendPQExpBuffer(&buf,
366 " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
367 " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
368 " CASE\n"
369 " WHEN p.proisagg THEN '%s'\n"
370 " WHEN p.proiswindow THEN '%s'\n"
371 " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
372 " ELSE '%s'\n"
373 " END as \"%s\"",
374 gettext_noop("Result data type"),
375 gettext_noop("Argument data types"),
376 /* translator: "agg" is short for "aggregate" */
377 gettext_noop("agg"),
378 gettext_noop("window"),
379 gettext_noop("trigger"),
380 gettext_noop("func"),
381 gettext_noop("Type"));
382
383 if (verbose)
384 {
385 appendPQExpBuffer(&buf,
386 ",\n CASE\n"
387 " WHEN p.provolatile = "
388 CppAsString2(PROVOLATILE_IMMUTABLE) " THEN '%s'\n"
389 " WHEN p.provolatile = "
390 CppAsString2(PROVOLATILE_STABLE) " THEN '%s'\n"
391 " WHEN p.provolatile = "
392 CppAsString2(PROVOLATILE_VOLATILE) " THEN '%s'\n"
393 " END as \"%s\"",
394 gettext_noop("immutable"),
395 gettext_noop("stable"),
396 gettext_noop("volatile"),
397 gettext_noop("Volatility"));
398 if (pset.sversion >= 90600)
399 appendPQExpBuffer(&buf,
400 ",\n CASE\n"
401 " WHEN p.proparallel = "
402 CppAsString2(PROPARALLEL_RESTRICTED) " THEN '%s'\n"
403 " WHEN p.proparallel = "
404 CppAsString2(PROPARALLEL_SAFE) " THEN '%s'\n"
405 " WHEN p.proparallel = "
406 CppAsString2(PROPARALLEL_UNSAFE) " THEN '%s'\n"
407 " END as \"%s\"",
408 gettext_noop("restricted"),
409 gettext_noop("safe"),
410 gettext_noop("unsafe"),
411 gettext_noop("Parallel"));
412 appendPQExpBuffer(&buf,
413 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
414 ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\""
415 ",\n CASE WHEN p.proleakproof THEN '%s' ELSE '%s' END as \"%s\"",
416 gettext_noop("Owner"),
417 gettext_noop("definer"),
418 gettext_noop("invoker"),
419 gettext_noop("Security"),
420 gettext_noop("yes"),
421 gettext_noop("no"),
422 gettext_noop("Leakproof?"));
423 appendPQExpBufferStr(&buf, ",\n ");
424 printACLColumn(&buf, "p.proacl");
425 appendPQExpBuffer(&buf,
426 ",\n l.lanname as \"%s\"",
427 gettext_noop("Language"));
428 appendPQExpBuffer(&buf,
429 ",\n CASE WHEN l.lanname IN ('internal', 'c') THEN p.prosrc END as \"%s\"",
430 gettext_noop("Internal name"));
431 appendPQExpBuffer(&buf,
432 ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
433 gettext_noop("Description"));
434 }
435
436 appendPQExpBufferStr(&buf,
437 "\nFROM pg_catalog.pg_proc p"
438 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
439
440 for (int i = 0; i < num_arg_patterns; i++)
441 {
442 appendPQExpBuffer(&buf,
443 " LEFT JOIN pg_catalog.pg_type t%d ON t%d.oid = p.proargtypes[%d]\n"
444 " LEFT JOIN pg_catalog.pg_namespace nt%d ON nt%d.oid = t%d.typnamespace\n",
445 i, i, i, i, i, i);
446 }
447
448 if (verbose)
449 appendPQExpBufferStr(&buf,
450 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
451
452 have_where = false;
453
454 /* filter by function type, if requested */
455 if (showNormal && showAggregate && showProcedure && showTrigger && showWindow)
456 /* Do nothing */ ;
457 else if (showNormal)
458 {
459 if (!showAggregate)
460 {
461 if (have_where)
462 appendPQExpBufferStr(&buf, " AND ");
463 else
464 {
465 appendPQExpBufferStr(&buf, "WHERE ");
466 have_where = true;
467 }
468 if (pset.sversion >= 110000)
469 appendPQExpBufferStr(&buf, "p.prokind <> "
470 CppAsString2(PROKIND_AGGREGATE) "\n");
471 else
472 appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
473 }
474 if (!showProcedure && pset.sversion >= 110000)
475 {
476 if (have_where)
477 appendPQExpBufferStr(&buf, " AND ");
478 else
479 {
480 appendPQExpBufferStr(&buf, "WHERE ");
481 have_where = true;
482 }
483 appendPQExpBufferStr(&buf, "p.prokind <> "
484 CppAsString2(PROKIND_PROCEDURE) "\n");
485 }
486 if (!showTrigger)
487 {
488 if (have_where)
489 appendPQExpBufferStr(&buf, " AND ");
490 else
491 {
492 appendPQExpBufferStr(&buf, "WHERE ");
493 have_where = true;
494 }
495 appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
496 }
497 if (!showWindow)
498 {
499 if (have_where)
500 appendPQExpBufferStr(&buf, " AND ");
501 else
502 {
503 appendPQExpBufferStr(&buf, "WHERE ");
504 have_where = true;
505 }
506 if (pset.sversion >= 110000)
507 appendPQExpBufferStr(&buf, "p.prokind <> "
508 CppAsString2(PROKIND_WINDOW) "\n");
509 else
510 appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
511 }
512 }
513 else
514 {
515 bool needs_or = false;
516
517 appendPQExpBufferStr(&buf, "WHERE (\n ");
518 have_where = true;
519 /* Note: at least one of these must be true ... */
520 if (showAggregate)
521 {
522 if (pset.sversion >= 110000)
523 appendPQExpBufferStr(&buf, "p.prokind = "
524 CppAsString2(PROKIND_AGGREGATE) "\n");
525 else
526 appendPQExpBufferStr(&buf, "p.proisagg\n");
527 needs_or = true;
528 }
529 if (showTrigger)
530 {
531 if (needs_or)
532 appendPQExpBufferStr(&buf, " OR ");
533 appendPQExpBufferStr(&buf,
534 "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
535 needs_or = true;
536 }
537 if (showProcedure)
538 {
539 if (needs_or)
540 appendPQExpBufferStr(&buf, " OR ");
541 appendPQExpBufferStr(&buf, "p.prokind = "
542 CppAsString2(PROKIND_PROCEDURE) "\n");
543 needs_or = true;
544 }
545 if (showWindow)
546 {
547 if (needs_or)
548 appendPQExpBufferStr(&buf, " OR ");
549 if (pset.sversion >= 110000)
550 appendPQExpBufferStr(&buf, "p.prokind = "
551 CppAsString2(PROKIND_WINDOW) "\n");
552 else
553 appendPQExpBufferStr(&buf, "p.proiswindow\n");
554 }
555 appendPQExpBufferStr(&buf, " )\n");
556 }
557
558 if (!validateSQLNamePattern(&buf, func_pattern, have_where, false,
559 "n.nspname", "p.proname", NULL,
560 "pg_catalog.pg_function_is_visible(p.oid)",
561 NULL, 3))
562 goto error_return;
563
564 for (int i = 0; i < num_arg_patterns; i++)
565 {
566 if (strcmp(arg_patterns[i], "-") != 0)
567 {
568 /*
569 * Match type-name patterns against either internal or external
570 * name, like \dT. Unlike \dT, there seems no reason to
571 * discriminate against arrays or composite types.
572 */
573 char nspname[64];
574 char typname[64];
575 char ft[64];
576 char tiv[64];
577
578 snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
579 snprintf(typname, sizeof(typname), "t%d.typname", i);
580 snprintf(ft, sizeof(ft),
581 "pg_catalog.format_type(t%d.oid, NULL)", i);
582 snprintf(tiv, sizeof(tiv),
583 "pg_catalog.pg_type_is_visible(t%d.oid)", i);
584 if (!validateSQLNamePattern(&buf,
585 map_typename_pattern(arg_patterns[i]),
586 true, false,
587 nspname, typname, ft, tiv,
588 NULL, 3))
589 goto error_return;
590 }
591 else
592 {
593 /* "-" pattern specifies no such parameter */
594 appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
595 }
596 }
597
598 if (!showSystem && !func_pattern)
599 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
600 " AND n.nspname <> 'information_schema'\n");
601
602 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
603
604 res = PSQLexec(buf.data);
605 termPQExpBuffer(&buf);
606 if (!res)
607 return false;
608
609 myopt.title = _("List of functions");
610 myopt.translate_header = true;
611 if (pset.sversion >= 90600)
612 {
613 myopt.translate_columns = translate_columns;
614 myopt.n_translate_columns = lengthof(translate_columns);
615 }
616 else
617 {
618 myopt.translate_columns = translate_columns_pre_96;
619 myopt.n_translate_columns = lengthof(translate_columns_pre_96);
620 }
621
622 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
623
624 PQclear(res);
625 return true;
626
627 error_return:
628 termPQExpBuffer(&buf);
629 return false;
630 }
631
632
633
634 /*
635 * \dT
636 * describe types
637 */
638 bool
639 describeTypes(const char *pattern, bool verbose, bool showSystem)
640 {
641 PQExpBufferData buf;
642 PGresult *res;
643 printQueryOpt myopt = pset.popt;
644
645 initPQExpBuffer(&buf);
646
647 printfPQExpBuffer(&buf,
648 "SELECT n.nspname as \"%s\",\n"
649 " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
650 gettext_noop("Schema"),
651 gettext_noop("Name"));
652 if (verbose)
653 {
654 appendPQExpBuffer(&buf,
655 " t.typname AS \"%s\",\n"
656 " CASE WHEN t.typrelid != 0\n"
657 " THEN CAST('tuple' AS pg_catalog.text)\n"
658 " WHEN t.typlen < 0\n"
659 " THEN CAST('var' AS pg_catalog.text)\n"
660 " ELSE CAST(t.typlen AS pg_catalog.text)\n"
661 " END AS \"%s\",\n"
662 " pg_catalog.array_to_string(\n"
663 " ARRAY(\n"
664 " SELECT e.enumlabel\n"
665 " FROM pg_catalog.pg_enum e\n"
666 " WHERE e.enumtypid = t.oid\n"
667 " ORDER BY e.enumsortorder\n"
668 " ),\n"
669 " E'\\n'\n"
670 " ) AS \"%s\",\n"
671 " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
672 gettext_noop("Internal name"),
673 gettext_noop("Size"),
674 gettext_noop("Elements"),
675 gettext_noop("Owner"));
676 printACLColumn(&buf, "t.typacl");
677 appendPQExpBufferStr(&buf, ",\n ");
678 }
679
680 appendPQExpBuffer(&buf,
681 " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
682 gettext_noop("Description"));
683
684 appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
685 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
686
687 /*
688 * do not include complex types (typrelid!=0) unless they are standalone
689 * composite types
690 */
691 appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
692 appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
693 " FROM pg_catalog.pg_class c "
694 "WHERE c.oid = t.typrelid))\n");
695
696 /*
697 * do not include array types unless the pattern contains []
698 */
699 if (pattern == NULL || strstr(pattern, "[]") == NULL)
700 appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
701
702 if (!showSystem && !pattern)
703 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
704 " AND n.nspname <> 'information_schema'\n");
705
706 /* Match name pattern against either internal or external name */
707 if (!validateSQLNamePattern(&buf, map_typename_pattern(pattern),
708 true, false,
709 "n.nspname", "t.typname",
710 "pg_catalog.format_type(t.oid, NULL)",
711 "pg_catalog.pg_type_is_visible(t.oid)",
712 NULL, 3))
713 {
714 termPQExpBuffer(&buf);
715 return false;
716 }
717
718 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
719
720 res = PSQLexec(buf.data);
721 termPQExpBuffer(&buf);
722 if (!res)
723 return false;
724
725 myopt.title = _("List of data types");
726 myopt.translate_header = true;
727
728 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
729
730 PQclear(res);
731 return true;
732 }
733
734 /*
735 * Map some variant type names accepted by the backend grammar into
736 * canonical type names.
737 *
738 * Helper for \dT and other functions that take typename patterns.
739 * This doesn't completely mask the fact that these names are special;
740 * for example, a pattern of "dec*" won't magically match "numeric".
741 * But it goes a long way to reduce the surprise factor.
742 */
743 static const char *
744 map_typename_pattern(const char *pattern)
745 {
746 static const char *const typename_map[] = {
747 /*
748 * These names are accepted by gram.y, although they are neither the
749 * "real" name seen in pg_type nor the canonical name printed by
750 * format_type().
751 */
752 "decimal", "numeric",
753 "float", "double precision",
754 "int", "integer",
755
756 /*
757 * We also have to map the array names for cases where the canonical
758 * name is different from what pg_type says.
759 */
760 "bool[]", "boolean[]",
761 "decimal[]", "numeric[]",
762 "float[]", "double precision[]",
763 "float4[]", "real[]",
764 "float8[]", "double precision[]",
765 "int[]", "integer[]",
766 "int2[]", "smallint[]",
767 "int4[]", "integer[]",
768 "int8[]", "bigint[]",
769 "time[]", "time without time zone[]",
770 "timetz[]", "time with time zone[]",
771 "timestamp[]", "timestamp without time zone[]",
772 "timestamptz[]", "timestamp with time zone[]",
773 "varbit[]", "bit varying[]",
774 "varchar[]", "character varying[]",
775 NULL
776 };
777
778 if (pattern == NULL)
779 return NULL;
780 for (int i = 0; typename_map[i] != NULL; i += 2)
781 {
782 if (pg_strcasecmp(pattern, typename_map[i]) == 0)
783 return typename_map[i + 1];
784 }
785 return pattern;
786 }
787
788
789 /*
790 * \do
791 * Describe operators
792 */
793 bool
794 describeOperators(const char *oper_pattern,
795 char **arg_patterns, int num_arg_patterns,
796 bool verbose, bool showSystem)
797 {
798 PQExpBufferData buf;
799 PGresult *res;
800 printQueryOpt myopt = pset.popt;
801 static const bool translate_columns[] = {false, false, false, false, false, false, true, false};
802
803 initPQExpBuffer(&buf);
804
805 /*
806 * Note: before Postgres 9.1, we did not assign comments to any built-in
807 * operators, preferring to let the comment on the underlying function
808 * suffice. The coalesce() on the obj_description() calls below supports
809 * this convention by providing a fallback lookup of a comment on the
810 * operator's function. Since 9.1 there is a policy that every built-in
811 * operator should have a comment; so the coalesce() is no longer
812 * necessary so far as built-in operators are concerned. We keep it
813 * anyway, for now, because third-party modules may still be following the
814 * old convention.
815 *
816 * The support for postfix operators in this query is dead code as of
817 * Postgres 14, but we need to keep it for as long as we support talking
818 * to pre-v14 servers.
819 */
820
821 printfPQExpBuffer(&buf,
822 "SELECT n.nspname as \"%s\",\n"
823 " o.oprname AS \"%s\",\n"
824 " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
825 " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
826 " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
827 gettext_noop("Schema"),
828 gettext_noop("Name"),
829 gettext_noop("Left arg type"),
830 gettext_noop("Right arg type"),
831 gettext_noop("Result type"));
832
833 if (verbose)
834 appendPQExpBuffer(&buf,
835 " o.oprcode AS \"%s\",\n"
836 " CASE WHEN p.proleakproof THEN '%s' ELSE '%s' END AS \"%s\",\n",
837 gettext_noop("Function"),
838 gettext_noop("yes"),
839 gettext_noop("no"),
840 gettext_noop("Leakproof?"));
841
842 appendPQExpBuffer(&buf,
843 " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
844 " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
845 "FROM pg_catalog.pg_operator o\n"
846 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
847 gettext_noop("Description"));
848
849 if (num_arg_patterns >= 2)
850 {
851 num_arg_patterns = 2; /* ignore any additional arguments */
852 appendPQExpBufferStr(&buf,
853 " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprleft\n"
854 " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n"
855 " LEFT JOIN pg_catalog.pg_type t1 ON t1.oid = o.oprright\n"
856 " LEFT JOIN pg_catalog.pg_namespace nt1 ON nt1.oid = t1.typnamespace\n");
857 }
858 else if (num_arg_patterns == 1)
859 {
860 appendPQExpBufferStr(&buf,
861 " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprright\n"
862 " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n");
863 }
864
865 if (verbose)
866 appendPQExpBufferStr(&buf,
867 " LEFT JOIN pg_catalog.pg_proc p ON p.oid = o.oprcode\n");
868
869 if (!showSystem && !oper_pattern)
870 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
871 " AND n.nspname <> 'information_schema'\n");
872
873 if (!validateSQLNamePattern(&buf, oper_pattern,
874 !showSystem && !oper_pattern, true,
875 "n.nspname", "o.oprname", NULL,
876 "pg_catalog.pg_operator_is_visible(o.oid)",
877 NULL, 3))
878 goto error_return;
879
880 if (num_arg_patterns == 1)
881 appendPQExpBufferStr(&buf, " AND o.oprleft = 0\n");
882
883 for (int i = 0; i < num_arg_patterns; i++)
884 {
885 if (strcmp(arg_patterns[i], "-") != 0)
886 {
887 /*
888 * Match type-name patterns against either internal or external
889 * name, like \dT. Unlike \dT, there seems no reason to
890 * discriminate against arrays or composite types.
891 */
892 char nspname[64];
893 char typname[64];
894 char ft[64];
895 char tiv[64];
896
897 snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
898 snprintf(typname, sizeof(typname), "t%d.typname", i);
899 snprintf(ft, sizeof(ft),
900 "pg_catalog.format_type(t%d.oid, NULL)", i);
901 snprintf(tiv, sizeof(tiv),
902 "pg_catalog.pg_type_is_visible(t%d.oid)", i);
903 if (!validateSQLNamePattern(&buf,
904 map_typename_pattern(arg_patterns[i]),
905 true, false,
906 nspname, typname, ft, tiv,
907 NULL, 3))
908 goto error_return;
909 }
910 else
911 {
912 /* "-" pattern specifies no such parameter */
913 appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
914 }
915 }
916
917 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
918
919 res = PSQLexec(buf.data);
920 termPQExpBuffer(&buf);
921 if (!res)
922 return false;
923
924 myopt.title = _("List of operators");
925 myopt.translate_header = true;
926 myopt.translate_columns = translate_columns;
927 myopt.n_translate_columns = lengthof(translate_columns);
928
929 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
930
931 PQclear(res);
932 return true;
933
934 error_return:
935 termPQExpBuffer(&buf);
936 return false;
937 }
938
939
940 /*
941 * listAllDbs
942 *
943 * for \l, \list, and -l switch
944 */
945 bool
946 listAllDbs(const char *pattern, bool verbose)
947 {
948 PGresult *res;
949 PQExpBufferData buf;
950 printQueryOpt myopt = pset.popt;
951
952 initPQExpBuffer(&buf);
953
954 printfPQExpBuffer(&buf,
955 "SELECT\n"
956 " d.datname as \"%s\",\n"
957 " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
958 " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
959 gettext_noop("Name"),
960 gettext_noop("Owner"),
961 gettext_noop("Encoding"));
962 if (pset.sversion >= 150000)
963 appendPQExpBuffer(&buf,
964 " CASE d.datlocprovider "
965 "WHEN " CppAsString2(COLLPROVIDER_BUILTIN) " THEN 'builtin' "
966 "WHEN " CppAsString2(COLLPROVIDER_LIBC) " THEN 'libc' "
967 "WHEN " CppAsString2(COLLPROVIDER_ICU) " THEN 'icu' "
968 "END AS \"%s\",\n",
969 gettext_noop("Locale Provider"));
970 else
971 appendPQExpBuffer(&buf,
972 " 'libc' AS \"%s\",\n",
973 gettext_noop("Locale Provider"));
974 appendPQExpBuffer(&buf,
975 " d.datcollate as \"%s\",\n"
976 " d.datctype as \"%s\",\n",
977 gettext_noop("Collate"),
978 gettext_noop("Ctype"));
979 if (pset.sversion >= 170000)
980 appendPQExpBuffer(&buf,
981 " d.datlocale as \"%s\",\n",
982 gettext_noop("Locale"));
983 else if (pset.sversion >= 150000)
984 appendPQExpBuffer(&buf,
985 " d.daticulocale as \"%s\",\n",
986 gettext_noop("Locale"));
987 else
988 appendPQExpBuffer(&buf,
989 " NULL as \"%s\",\n",
990 gettext_noop("Locale"));
991 if (pset.sversion >= 160000)
992 appendPQExpBuffer(&buf,
993 " d.daticurules as \"%s\",\n",
994 gettext_noop("ICU Rules"));
995 else
996 appendPQExpBuffer(&buf,
997 " NULL as \"%s\",\n",
998 gettext_noop("ICU Rules"));
999 appendPQExpBufferStr(&buf, " ");
1000 printACLColumn(&buf, "d.datacl");
1001 if (verbose)
1002 {
1003 appendPQExpBuffer(&buf,
1004 ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
1005 " %s"
1006 " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
1007 " ELSE 'No Access'\n"
1008 " END as \"%s\""
1009 ",\n t.spcname as \"%s\""
1010 ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
1011 pset.sversion >= 100000 ? "OR pg_catalog.pg_has_role('pg_read_all_stats', 'USAGE')\n" : "",
1012 gettext_noop("Size"),
1013 gettext_noop("Tablespace"),
1014 gettext_noop("Description"));
1015 }
1016 appendPQExpBufferStr(&buf,
1017 "\nFROM pg_catalog.pg_database d\n");
1018 if (verbose)
1019 appendPQExpBufferStr(&buf,
1020 " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
1021
1022 if (pattern)
1023 {
1024 if (!validateSQLNamePattern(&buf, pattern, false, false,
1025 NULL, "d.datname", NULL, NULL,
1026 NULL, 1))
1027 {
1028 termPQExpBuffer(&buf);
1029 return false;
1030 }
1031 }
1032
1033 appendPQExpBufferStr(&buf, "ORDER BY 1;");
1034 res = PSQLexec(buf.data);
1035 termPQExpBuffer(&buf);
1036 if (!res)
1037 return false;
1038
1039 myopt.title = _("List of databases");
1040 myopt.translate_header = true;
1041
1042 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1043
1044 PQclear(res);
1045 return true;
1046 }
1047
1048
1049 /*
1050 * List Tables' Grant/Revoke Permissions
1051 * \z (now also \dp -- perhaps more mnemonic)
1052 */
1053 bool
1054 permissionsList(const char *pattern, bool showSystem)
1055 {
1056 PQExpBufferData buf;
1057 PGresult *res;
1058 printQueryOpt myopt = pset.popt;
1059 static const bool translate_columns[] = {false, false, true, false, false, false};
1060
1061 initPQExpBuffer(&buf);
1062
1063 /*
1064 * we ignore indexes and toast tables since they have no meaningful rights
1065 */
1066 printfPQExpBuffer(&buf,
1067 "SELECT n.nspname as \"%s\",\n"
1068 " c.relname as \"%s\",\n"
1069 " CASE c.relkind"
1070 " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
1071 " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
1072 " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
1073 " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
1074 " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
1075 " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
1076 " END as \"%s\",\n"
1077 " ",
1078 gettext_noop("Schema"),
1079 gettext_noop("Name"),
1080 gettext_noop("table"),
1081 gettext_noop("view"),
1082 gettext_noop("materialized view"),
1083 gettext_noop("sequence"),
1084 gettext_noop("foreign table"),
1085 gettext_noop("partitioned table"),
1086 gettext_noop("Type"));
1087
1088 printACLColumn(&buf, "c.relacl");
1089
1090 /*
1091 * The formatting of attacl should match printACLColumn(). However, we
1092 * need no special case for an empty attacl, because the backend always
1093 * optimizes that back to NULL.
1094 */
1095 appendPQExpBuffer(&buf,
1096 ",\n pg_catalog.array_to_string(ARRAY(\n"
1097 " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
1098 " FROM pg_catalog.pg_attribute a\n"
1099 " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
1100 " ), E'\\n') AS \"%s\"",
1101 gettext_noop("Column privileges"));
1102
1103 if (pset.sversion >= 90500 && pset.sversion < 100000)
1104 appendPQExpBuffer(&buf,
1105 ",\n pg_catalog.array_to_string(ARRAY(\n"
1106 " SELECT polname\n"
1107 " || CASE WHEN polcmd != '*' THEN\n"
1108 " E' (' || polcmd::pg_catalog.text || E'):'\n"
1109 " ELSE E':'\n"
1110 " END\n"
1111 " || CASE WHEN polqual IS NOT NULL THEN\n"
1112 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1113 " ELSE E''\n"
1114 " END\n"
1115 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1116 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1117 " ELSE E''\n"
1118 " END"
1119 " || CASE WHEN polroles <> '{0}' THEN\n"
1120 " E'\\n to: ' || pg_catalog.array_to_string(\n"
1121 " ARRAY(\n"
1122 " SELECT rolname\n"
1123 " FROM pg_catalog.pg_roles\n"
1124 " WHERE oid = ANY (polroles)\n"
1125 " ORDER BY 1\n"
1126 " ), E', ')\n"
1127 " ELSE E''\n"
1128 " END\n"
1129 " FROM pg_catalog.pg_policy pol\n"
1130 " WHERE polrelid = c.oid), E'\\n')\n"
1131 " AS \"%s\"",
1132 gettext_noop("Policies"));
1133
1134 if (pset.sversion >= 100000)
1135 appendPQExpBuffer(&buf,
1136 ",\n pg_catalog.array_to_string(ARRAY(\n"
1137 " SELECT polname\n"
1138 " || CASE WHEN NOT polpermissive THEN\n"
1139 " E' (RESTRICTIVE)'\n"
1140 " ELSE '' END\n"
1141 " || CASE WHEN polcmd != '*' THEN\n"
1142 " E' (' || polcmd::pg_catalog.text || E'):'\n"
1143 " ELSE E':'\n"
1144 " END\n"
1145 " || CASE WHEN polqual IS NOT NULL THEN\n"
1146 " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1147 " ELSE E''\n"
1148 " END\n"
1149 " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1150 " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1151 " ELSE E''\n"
1152 " END"
1153 " || CASE WHEN polroles <> '{0}' THEN\n"
1154 " E'\\n to: ' || pg_catalog.array_to_string(\n"
1155 " ARRAY(\n"
1156 " SELECT rolname\n"
1157 " FROM pg_catalog.pg_roles\n"
1158 " WHERE oid = ANY (polroles)\n"
1159 " ORDER BY 1\n"
1160 " ), E', ')\n"
1161 " ELSE E''\n"
1162 " END\n"
1163 " FROM pg_catalog.pg_policy pol\n"
1164 " WHERE polrelid = c.oid), E'\\n')\n"
1165 " AS \"%s\"",
1166 gettext_noop("Policies"));
1167
1168 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
1169 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1170 "WHERE c.relkind IN ("
1171 CppAsString2(RELKIND_RELATION) ","
1172 CppAsString2(RELKIND_VIEW) ","
1173 CppAsString2(RELKIND_MATVIEW) ","
1174 CppAsString2(RELKIND_SEQUENCE) ","
1175 CppAsString2(RELKIND_FOREIGN_TABLE) ","
1176 CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
1177
1178 if (!showSystem && !pattern)
1179 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1180 " AND n.nspname <> 'information_schema'\n");
1181
1182 if (!validateSQLNamePattern(&buf, pattern, true, false,
1183 "n.nspname", "c.relname", NULL,
1184 "pg_catalog.pg_table_is_visible(c.oid)",
1185 NULL, 3))
1186 goto error_return;
1187
1188 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
1189
1190 res = PSQLexec(buf.data);
1191 if (!res)
1192 goto error_return;
1193
1194 printfPQExpBuffer(&buf, _("Access privileges"));
1195 myopt.title = buf.data;
1196 myopt.translate_header = true;
1197 myopt.translate_columns = translate_columns;
1198 myopt.n_translate_columns = lengthof(translate_columns);
1199
1200 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1201
1202 termPQExpBuffer(&buf);
1203 PQclear(res);
1204 return true;
1205
1206 error_return:
1207 termPQExpBuffer(&buf);
1208 return false;
1209 }
1210
1211
1212 /*
1213 * \ddp
1214 *
1215 * List Default ACLs. The pattern can match either schema or role name.
1216 */
1217 bool
1218 listDefaultACLs(const char *pattern)
1219 {
1220 PQExpBufferData buf;
1221 PGresult *res;
1222 printQueryOpt myopt = pset.popt;
1223 static const bool translate_columns[] = {false, false, true, false};
1224
1225 initPQExpBuffer(&buf);
1226
1227 printfPQExpBuffer(&buf,
1228 "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1229 " n.nspname AS \"%s\",\n"
1230 " CASE d.defaclobjtype "
1231 " WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s'"
1232 " WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
1233 " ",
1234 gettext_noop("Owner"),
1235 gettext_noop("Schema"),
1236 DEFACLOBJ_RELATION,
1237 gettext_noop("table"),
1238 DEFACLOBJ_SEQUENCE,
1239 gettext_noop("sequence"),
1240 DEFACLOBJ_FUNCTION,
1241 gettext_noop("function"),
1242 DEFACLOBJ_TYPE,
1243 gettext_noop("type"),
1244 DEFACLOBJ_NAMESPACE,
1245 gettext_noop("schema"),
1246 DEFACLOBJ_LARGEOBJECT,
1247 gettext_noop("large object"),
1248 gettext_noop("Type"));
1249
1250 printACLColumn(&buf, "d.defaclacl");
1251
1252 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1253 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1254
1255 if (!validateSQLNamePattern(&buf, pattern, false, false,
1256 NULL,
1257 "n.nspname",
1258 "pg_catalog.pg_get_userbyid(d.defaclrole)",
1259 NULL,
1260 NULL, 3))
1261 goto error_return;
1262
1263 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1264
1265 res = PSQLexec(buf.data);
1266 if (!res)
1267 goto error_return;
1268
1269 printfPQExpBuffer(&buf, _("Default access privileges"));
1270 myopt.title = buf.data;
1271 myopt.translate_header = true;
1272 myopt.translate_columns = translate_columns;
1273 myopt.n_translate_columns = lengthof(translate_columns);
1274
1275 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1276
1277 termPQExpBuffer(&buf);
1278 PQclear(res);
1279 return true;
1280
1281 error_return:
1282 termPQExpBuffer(&buf);
1283 return false;
1284 }
1285
1286
1287 /*
1288 * Get object comments
1289 *
1290 * \dd [foo]
1291 *
1292 * Note: This command only lists comments for object types which do not have
1293 * their comments displayed by their own backslash commands. The following
1294 * types of objects will be displayed: constraint, operator class,
1295 * operator family, rule, and trigger.
1296 *
1297 */
1298 bool
1299 objectDescription(const char *pattern, bool showSystem)
1300 {
1301 PQExpBufferData buf;
1302 PGresult *res;
1303 printQueryOpt myopt = pset.popt;
1304 static const bool translate_columns[] = {false, false, true, false};
1305
1306 initPQExpBuffer(&buf);
1307
1308 appendPQExpBuffer(&buf,
1309 "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
1310 "FROM (\n",
1311 gettext_noop("Schema"),
1312 gettext_noop("Name"),
1313 gettext_noop("Object"),
1314 gettext_noop("Description"));
1315
1316 /* Table constraint descriptions */
1317 appendPQExpBuffer(&buf,
1318 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1319 " n.nspname as nspname,\n"
1320 " CAST(pgc.conname AS pg_catalog.text) as name,"
1321 " CAST('%s' AS pg_catalog.text) as object\n"
1322 " FROM pg_catalog.pg_constraint pgc\n"
1323 " JOIN pg_catalog.pg_class c "
1324 "ON c.oid = pgc.conrelid\n"
1325 " LEFT JOIN pg_catalog.pg_namespace n "
1326 " ON n.oid = c.relnamespace\n",
1327 gettext_noop("table constraint"));
1328
1329 if (!showSystem && !pattern)
1330 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1331 " AND n.nspname <> 'information_schema'\n");
1332
1333 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1334 false, "n.nspname", "pgc.conname", NULL,
1335 "pg_catalog.pg_table_is_visible(c.oid)",
1336 NULL, 3))
1337 goto error_return;
1338
1339 /* Domain constraint descriptions */
1340 appendPQExpBuffer(&buf,
1341 "UNION ALL\n"
1342 " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1343 " n.nspname as nspname,\n"
1344 " CAST(pgc.conname AS pg_catalog.text) as name,"
1345 " CAST('%s' AS pg_catalog.text) as object\n"
1346 " FROM pg_catalog.pg_constraint pgc\n"
1347 " JOIN pg_catalog.pg_type t "
1348 "ON t.oid = pgc.contypid\n"
1349 " LEFT JOIN pg_catalog.pg_namespace n "
1350 " ON n.oid = t.typnamespace\n",
1351 gettext_noop("domain constraint"));
1352
1353 if (!showSystem && !pattern)
1354 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1355 " AND n.nspname <> 'information_schema'\n");
1356
1357 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1358 false, "n.nspname", "pgc.conname", NULL,
1359 "pg_catalog.pg_type_is_visible(t.oid)",
1360 NULL, 3))
1361 goto error_return;
1362
1363 /* Operator class descriptions */
1364 appendPQExpBuffer(&buf,
1365 "UNION ALL\n"
1366 " SELECT o.oid as oid, o.tableoid as tableoid,\n"
1367 " n.nspname as nspname,\n"
1368 " CAST(o.opcname AS pg_catalog.text) as name,\n"
1369 " CAST('%s' AS pg_catalog.text) as object\n"
1370 " FROM pg_catalog.pg_opclass o\n"
1371 " JOIN pg_catalog.pg_am am ON "
1372 "o.opcmethod = am.oid\n"
1373 " JOIN pg_catalog.pg_namespace n ON "
1374 "n.oid = o.opcnamespace\n",
1375 gettext_noop("operator class"));
1376
1377 if (!showSystem && !pattern)
1378 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1379 " AND n.nspname <> 'information_schema'\n");
1380
1381 if (!validateSQLNamePattern(&buf, pattern, true, false,
1382 "n.nspname", "o.opcname", NULL,
1383 "pg_catalog.pg_opclass_is_visible(o.oid)",
1384 NULL, 3))
1385 goto error_return;
1386
1387 /* Operator family descriptions */
1388 appendPQExpBuffer(&buf,
1389 "UNION ALL\n"
1390 " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1391 " n.nspname as nspname,\n"
1392 " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1393 " CAST('%s' AS pg_catalog.text) as object\n"
1394 " FROM pg_catalog.pg_opfamily opf\n"
1395 " JOIN pg_catalog.pg_am am "
1396 "ON opf.opfmethod = am.oid\n"
1397 " JOIN pg_catalog.pg_namespace n "
1398 "ON opf.opfnamespace = n.oid\n",
1399 gettext_noop("operator family"));
1400
1401 if (!showSystem && !pattern)
1402 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1403 " AND n.nspname <> 'information_schema'\n");
1404
1405 if (!validateSQLNamePattern(&buf, pattern, true, false,
1406 "n.nspname", "opf.opfname", NULL,
1407 "pg_catalog.pg_opfamily_is_visible(opf.oid)",
1408 NULL, 3))
1409 goto error_return;
1410
1411 /* Rule descriptions (ignore rules for views) */
1412 appendPQExpBuffer(&buf,
1413 "UNION ALL\n"
1414 " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1415 " n.nspname as nspname,\n"
1416 " CAST(r.rulename AS pg_catalog.text) as name,"
1417 " CAST('%s' AS pg_catalog.text) as object\n"
1418 " FROM pg_catalog.pg_rewrite r\n"
1419 " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1420 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1421 " WHERE r.rulename != '_RETURN'\n",
1422 gettext_noop("rule"));
1423
1424 if (!showSystem && !pattern)
1425 appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1426 " AND n.nspname <> 'information_schema'\n");
1427
1428 if (!validateSQLNamePattern(&buf, pattern, true, false,
1429 "n.nspname", "r.rulename", NULL,
1430 "pg_catalog.pg_table_is_visible(c.oid)",
1431 NULL, 3))
1432 goto error_return;
1433
1434 /* Trigger descriptions */
1435 appendPQExpBuffer(&buf,
1436 "UNION ALL\n"
1437 " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1438 " n.nspname as nspname,\n"
1439 " CAST(t.tgname AS pg_catalog.text) as name,"
1440 " CAST('%s' AS pg_catalog.text) as object\n"
1441 " FROM pg_catalog.pg_trigger t\n"
1442 " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1443 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1444 gettext_noop("trigger"));
1445
1446 if (!showSystem && !pattern)
1447 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1448 " AND n.nspname <> 'information_schema'\n");
1449
1450 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1451 "n.nspname", "t.tgname", NULL,
1452 "pg_catalog.pg_table_is_visible(c.oid)",
1453 NULL, 3))
1454 goto error_return;
1455
1456 appendPQExpBufferStr(&buf,
1457 ") AS tt\n"
1458 " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1459
1460 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1461
1462 res = PSQLexec(buf.data);
1463 termPQExpBuffer(&buf);
1464 if (!res)
1465 return false;
1466
1467 myopt.title = _("Object descriptions");
1468 myopt.translate_header = true;
1469 myopt.translate_columns = translate_columns;
1470 myopt.n_translate_columns = lengthof(translate_columns);
1471
1472 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1473
1474 PQclear(res);
1475 return true;
1476
1477 error_return:
1478 termPQExpBuffer(&buf);
1479 return false;
1480 }
1481
1482
1483 /*
1484 * describeTableDetails (for \d)
1485 *
1486 * This routine finds the tables to be displayed, and calls
1487 * describeOneTableDetails for each one.
1488 *
1489 * verbose: if true, this is \d+
1490 */
1491 bool
1492 describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1493 {
1494 PQExpBufferData buf;
1495 PGresult *res;
1496 int i;
1497
1498 initPQExpBuffer(&buf);
1499
1500 printfPQExpBuffer(&buf,
1501 "SELECT c.oid,\n"
1502 " n.nspname,\n"
1503 " c.relname\n"
1504 "FROM pg_catalog.pg_class c\n"
1505 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1506
1507 if (!showSystem && !pattern)
1508 appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1509 " AND n.nspname <> 'information_schema'\n");
1510
1511 if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1512 "n.nspname", "c.relname", NULL,
1513 "pg_catalog.pg_table_is_visible(c.oid)",
1514 NULL, 3))
1515 {
1516 termPQExpBuffer(&buf);
1517 return false;
1518 }
1519
1520 appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1521
1522 res = PSQLexec(buf.data);
1523 termPQExpBuffer(&buf);
1524 if (!res)
1525 return false;
1526
1527 if (PQntuples(res) == 0)
1528 {
1529 if (!pset.quiet)
1530 {
1531 if (pattern)
1532 pg_log_error("Did not find any relation named \"%s\".",
1533 pattern);
1534 else
1535 pg_log_error("Did not find any relations.");
1536 }
1537 PQclear(res);
1538 return false;
1539 }
1540
1541 for (i = 0; i < PQntuples(res); i++)
1542 {
1543 const char *oid;
1544 const char *nspname;
1545 const char *relname;
1546
1547 oid = PQgetvalue(res, i, 0);
1548 nspname = PQgetvalue(res, i, 1);
1549 relname = PQgetvalue(res, i, 2);
1550
1551 if (!describeOneTableDetails(nspname, relname, oid, verbose))
1552 {
1553 PQclear(res);
1554 return false;
1555 }
1556 if (cancel_pressed)
1557 {
1558 PQclear(res);
1559 return false;
1560 }
1561 }
1562
1563 PQclear(res);
1564 return true;
1565 }
1566
1567 /*
1568 * describeOneTableDetails (for \d)
1569 *
1570 * Unfortunately, the information presented here is so complicated that it
1571 * cannot be done in a single query. So we have to assemble the printed table
1572 * by hand and pass it to the underlying printTable() function.
1573 */
1574 static bool
1575 describeOneTableDetails(const char *schemaname,
1576 const char *relationname,
1577 const char *oid,
1578 bool verbose)
1579 {
1580 bool retval = false;
1581 PQExpBufferData buf;
1582 PGresult *res = NULL;
1583 printTableOpt myopt = pset.popt.topt;
1584 printTableContent cont;
1585 bool printTableInitialized = false;
1586 int i;
1587 char *view_def = NULL;
1588 char *headers[12];
1589 PQExpBufferData title;
1590 PQExpBufferData tmpbuf;
1591 int cols;
1592 int attname_col = -1, /* column indexes in "res" */
1593 atttype_col = -1,
1594 attrdef_col = -1,
1595 attnotnull_col = -1,
1596 attcoll_col = -1,
1597 attidentity_col = -1,
1598 attgenerated_col = -1,
1599 isindexkey_col = -1,
1600 indexdef_col = -1,
1601 fdwopts_col = -1,
1602 attstorage_col = -1,
1603 attcompression_col = -1,
1604 attstattarget_col = -1,
1605 attdescr_col = -1;
1606 int numrows;
1607 struct
1608 {
1609 int16 checks;
1610 char relkind;
1611 bool hasindex;
1612 bool hasrules;
1613 bool hastriggers;
1614 bool rowsecurity;
1615 bool forcerowsecurity;
1616 bool hasoids;
1617 bool ispartition;
1618 Oid tablespace;
1619 char *reloptions;
1620 char *reloftype;
1621 char relpersistence;
1622 char relreplident;
1623 char *relam;
1624 } tableinfo;
1625 bool show_column_details = false;
1626
1627 myopt.default_footer = false;
1628 /* This output looks confusing in expanded mode. */
1629 myopt.expanded = false;
1630
1631 initPQExpBuffer(&buf);
1632 initPQExpBuffer(&title);
1633 initPQExpBuffer(&tmpbuf);
1634
1635 /* Get general table info */
1636 if (pset.sversion >= 120000)
1637 {
1638 printfPQExpBuffer(&buf,
1639 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1640 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1641 "false AS relhasoids, c.relispartition, %s, c.reltablespace, "
1642 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1643 "c.relpersistence, c.relreplident, am.amname\n"
1644 "FROM pg_catalog.pg_class c\n "
1645 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1646 "LEFT JOIN pg_catalog.pg_am am ON (c.relam = am.oid)\n"
1647 "WHERE c.oid = '%s';",
1648 (verbose ?
1649 "pg_catalog.array_to_string(c.reloptions || "
1650 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1651 : "''"),
1652 oid);
1653 }
1654 else if (pset.sversion >= 100000)
1655 {
1656 printfPQExpBuffer(&buf,
1657 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1658 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1659 "c.relhasoids, c.relispartition, %s, c.reltablespace, "
1660 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1661 "c.relpersistence, c.relreplident\n"
1662 "FROM pg_catalog.pg_class c\n "
1663 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1664 "WHERE c.oid = '%s';",
1665 (verbose ?
1666 "pg_catalog.array_to_string(c.reloptions || "
1667 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1668 : "''"),
1669 oid);
1670 }
1671 else if (pset.sversion >= 90500)
1672 {
1673 printfPQExpBuffer(&buf,
1674 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1675 "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1676 "c.relhasoids, false as relispartition, %s, c.reltablespace, "
1677 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1678 "c.relpersistence, c.relreplident\n"
1679 "FROM pg_catalog.pg_class c\n "
1680 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1681 "WHERE c.oid = '%s';",
1682 (verbose ?
1683 "pg_catalog.array_to_string(c.reloptions || "
1684 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1685 : "''"),
1686 oid);
1687 }
1688 else if (pset.sversion >= 90400)
1689 {
1690 printfPQExpBuffer(&buf,
1691 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1692 "c.relhastriggers, false, false, c.relhasoids, "
1693 "false as relispartition, %s, c.reltablespace, "
1694 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1695 "c.relpersistence, c.relreplident\n"
1696 "FROM pg_catalog.pg_class c\n "
1697 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1698 "WHERE c.oid = '%s';",
1699 (verbose ?
1700 "pg_catalog.array_to_string(c.reloptions || "
1701 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1702 : "''"),
1703 oid);
1704 }
1705 else
1706 {
1707 printfPQExpBuffer(&buf,
1708 "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1709 "c.relhastriggers, false, false, c.relhasoids, "
1710 "false as relispartition, %s, c.reltablespace, "
1711 "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1712 "c.relpersistence\n"
1713 "FROM pg_catalog.pg_class c\n "
1714 "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1715 "WHERE c.oid = '%s';",
1716 (verbose ?
1717 "pg_catalog.array_to_string(c.reloptions || "
1718 "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1719 : "''"),
1720 oid);
1721 }
1722
1723 res = PSQLexec(buf.data);
1724 if (!res)
1725 goto error_return;
1726
1727 /* Did we get anything? */
1728 if (PQntuples(res) == 0)
1729 {
1730 if (!pset.quiet)
1731 pg_log_error("Did not find any relation with OID %s.", oid);
1732 goto error_return;
1733 }
1734
1735 tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1736 tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1737 tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1738 tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1739 tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1740 tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1741 tableinfo.forcerowsecurity = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1742 tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 7), "t") == 0;
1743 tableinfo.ispartition = strcmp(PQgetvalue(res, 0, 8), "t") == 0;
1744 tableinfo.reloptions = pg_strdup(PQgetvalue(res, 0, 9));
1745 tableinfo.tablespace = atooid(PQgetvalue(res, 0, 10));
1746 tableinfo.reloftype = (strcmp(PQgetvalue(res, 0, 11), "") != 0) ?
1747 pg_strdup(PQgetvalue(res, 0, 11)) : NULL;
1748 tableinfo.relpersistence = *(PQgetvalue(res, 0, 12));
1749 tableinfo.relreplident = (pset.sversion >= 90400) ?
1750 *(PQgetvalue(res, 0, 13)) : 'd';
1751 if (pset.sversion >= 120000)
1752 tableinfo.relam = PQgetisnull(res, 0, 14) ?
1753 NULL : pg_strdup(PQgetvalue(res, 0, 14));
1754 else
1755 tableinfo.relam = NULL;
1756 PQclear(res);
1757 res = NULL;
1758
1759 /*
1760 * If it's a sequence, deal with it here separately.
1761 */
1762 if (tableinfo.relkind == RELKIND_SEQUENCE)
1763 {
1764 PGresult *result = NULL;
1765 printQueryOpt myopt = pset.popt;
1766 char *footers[2] = {NULL, NULL};
1767
1768 if (pset.sversion >= 100000)
1769 {
1770 printfPQExpBuffer(&buf,
1771 "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n"
1772 " seqstart AS \"%s\",\n"
1773 " seqmin AS \"%s\",\n"
1774 " seqmax AS \"%s\",\n"
1775 " seqincrement AS \"%s\",\n"
1776 " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n"
1777 " seqcache AS \"%s\"\n",
1778 gettext_noop("Type"),
1779 gettext_noop("Start"),
1780 gettext_noop("Minimum"),
1781 gettext_noop("Maximum"),
1782 gettext_noop("Increment"),
1783 gettext_noop("yes"),
1784 gettext_noop("no"),
1785 gettext_noop("Cycles?"),
1786 gettext_noop("Cache"));
1787 appendPQExpBuffer(&buf,
1788 "FROM pg_catalog.pg_sequence\n"
1789 "WHERE seqrelid = '%s';",
1790 oid);
1791 }
1792 else
1793 {
1794 printfPQExpBuffer(&buf,
1795 "SELECT 'bigint' AS \"%s\",\n"
1796 " start_value AS \"%s\",\n"
1797 " min_value AS \"%s\",\n"
1798 " max_value AS \"%s\",\n"
1799 " increment_by AS \"%s\",\n"
1800 " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n"
1801 " cache_value AS \"%s\"\n",
1802 gettext_noop("Type"),
1803 gettext_noop("Start"),
1804 gettext_noop("Minimum"),
1805 gettext_noop("Maximum"),
1806 gettext_noop("Increment"),
1807 gettext_noop("yes"),
1808 gettext_noop("no"),
1809 gettext_noop("Cycles?"),
1810 gettext_noop("Cache"));
1811 appendPQExpBuffer(&buf, "FROM %s", fmtId(schemaname));
1812 /* must be separate because fmtId isn't reentrant */
1813 appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1814 }
1815
1816 res = PSQLexec(buf.data);
1817 if (!res)
1818 goto error_return;
1819
1820 /* Get the column that owns this sequence */
1821 printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1822 "\n pg_catalog.quote_ident(relname) || '.' ||"
1823 "\n pg_catalog.quote_ident(attname),"
1824 "\n d.deptype"
1825 "\nFROM pg_catalog.pg_class c"
1826 "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1827 "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1828 "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1829 "\n a.attrelid=c.oid AND"
1830 "\n a.attnum=d.refobjsubid)"
1831 "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1832 "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1833 "\n AND d.objid='%s'"
1834 "\n AND d.deptype IN ('a', 'i')",
1835 oid);
1836
1837 result = PSQLexec(buf.data);
1838
1839 /*
1840 * If we get no rows back, don't show anything (obviously). We should
1841 * never get more than one row back, but if we do, just ignore it and
1842 * don't print anything.
1843 */
1844 if (!result)
1845 goto error_return;
1846 else if (PQntuples(result) == 1)
1847 {
1848 switch (PQgetvalue(result, 0, 1)[0])
1849 {
1850 case 'a':
1851 footers[0] = psprintf(_("Owned by: %s"),
1852 PQgetvalue(result, 0, 0));
1853 break;
1854 case 'i':
1855 footers[0] = psprintf(_("Sequence for identity column: %s"),
1856 PQgetvalue(result, 0, 0));
1857 break;
1858 }
1859 }
1860 PQclear(result);
1861
1862 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
1863 printfPQExpBuffer(&title, _("Unlogged sequence \"%s.%s\""),
1864 schemaname, relationname);
1865 else
1866 printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1867 schemaname, relationname);
1868
1869 myopt.footers = footers;
1870 myopt.topt.default_footer = false;
1871 myopt.title = title.data;
1872 myopt.translate_header = true;
1873
1874 printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1875
1876 free(footers[0]);
1877
1878 retval = true;
1879 goto error_return; /* not an error, just return early */
1880 }
1881
1882 /* Identify whether we should print collation, nullable, default vals */
1883 if (tableinfo.relkind == RELKIND_RELATION ||
1884 tableinfo.relkind == RELKIND_VIEW ||
1885 tableinfo.relkind == RELKIND_MATVIEW ||
1886 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1887 tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1888 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1889 show_column_details = true;
1890
1891 /*
1892 * Get per-column info
1893 *
1894 * Since the set of query columns we need varies depending on relkind and
1895 * server version, we compute all the column numbers on-the-fly. Column
1896 * number variables for columns not fetched are left as -1; this avoids
1897 * duplicative test logic below.
1898 */
1899 cols = 0;
1900 printfPQExpBuffer(&buf, "SELECT a.attname");
1901 attname_col = cols++;
1902 appendPQExpBufferStr(&buf, ",\n pg_catalog.format_type(a.atttypid, a.atttypmod)");
1903 atttype_col = cols++;
1904
1905 if (show_column_details)
1906 {
1907 /* use "pretty" mode for expression to avoid excessive parentheses */
1908 appendPQExpBufferStr(&buf,
1909 ",\n (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid, true)"
1910 "\n FROM pg_catalog.pg_attrdef d"
1911 "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)"
1912 ",\n a.attnotnull");
1913 attrdef_col = cols++;
1914 attnotnull_col = cols++;
1915 appendPQExpBufferStr(&buf, ",\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1916 " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1917 attcoll_col = cols++;
1918 if (pset.sversion >= 100000)
1919 appendPQExpBufferStr(&buf, ",\n a.attidentity");
1920 else
1921 appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attidentity");
1922 attidentity_col = cols++;
1923 if (pset.sversion >= 120000)
1924 appendPQExpBufferStr(&buf, ",\n a.attgenerated");
1925 else
1926 appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attgenerated");
1927 attgenerated_col = cols++;
1928 }
1929 if (tableinfo.relkind == RELKIND_INDEX ||
1930 tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1931 {
1932 if (pset.sversion >= 110000)
1933 {
1934 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",
1935 oid,
1936 gettext_noop("yes"),
1937 gettext_noop("no"));
1938 isindexkey_col = cols++;
1939 }
1940 appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1941 indexdef_col = cols++;
1942 }
1943 /* FDW options for foreign table column */
1944 if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
1945 {
1946 appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1947 " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1948 " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1949 fdwopts_col = cols++;
1950 }
1951 if (verbose)
1952 {
1953 appendPQExpBufferStr(&buf, ",\n a.attstorage");
1954 attstorage_col = cols++;
1955
1956 /* compression info, if relevant to relkind */
1957 if (pset.sversion >= 140000 &&
1958 !pset.hide_compression &&
1959 (tableinfo.relkind == RELKIND_RELATION ||
1960 tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
1961 tableinfo.relkind == RELKIND_MATVIEW))
1962 {
1963 appendPQExpBufferStr(&buf, ",\n a.attcompression AS attcompression");
1964 attcompression_col = cols++;
1965 }
1966
1967 /* stats target, if relevant to relkind */
1968 if (tableinfo.relkind == RELKIND_RELATION ||
1969 tableinfo.relkind == RELKIND_INDEX ||
1970 tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1971 tableinfo.relkind == RELKIND_MATVIEW ||
1972 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1973 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1974 {
1975 appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1976 attstattarget_col = cols++;
1977 }
1978
1979 /*
1980 * In 9.0+, we have column comments for: relations, views, composite
1981 * types, and foreign tables (cf. CommentObject() in comment.c).
1982 */
1983 if (tableinfo.relkind == RELKIND_RELATION ||
1984 tableinfo.relkind == RELKIND_VIEW ||
1985 tableinfo.relkind == RELKIND_MATVIEW ||
1986 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1987 tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1988 tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1989 {
1990 appendPQExpBufferStr(&buf, ",\n pg_catalog.col_description(a.attrelid, a.attnum)");
1991 attdescr_col = cols++;
1992 }
1993 }
1994
1995 appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1996 appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1997 appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1998
1999 res = PSQLexec(buf.data);
2000 if (!res)
2001 goto error_return;
2002 numrows = PQntuples(res);
2003
2004 /* Make title */
2005 switch (tableinfo.relkind)
2006 {
2007 case RELKIND_RELATION:
2008 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2009 printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
2010 schemaname, relationname);
2011 else
2012 printfPQExpBuffer(&title, _("Table \"%s.%s\""),
2013 schemaname, relationname);
2014 break;
2015 case RELKIND_VIEW:
2016 printfPQExpBuffer(&title, _("View \"%s.%s\""),
2017 schemaname, relationname);
2018 break;
2019 case RELKIND_MATVIEW:
2020 printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
2021 schemaname, relationname);
2022 break;
2023 case RELKIND_INDEX:
2024 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2025 printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
2026 schemaname, relationname);
2027 else
2028 printfPQExpBuffer(&title, _("Index \"%s.%s\""),
2029 schemaname, relationname);
2030 break;
2031 case RELKIND_PARTITIONED_INDEX:
2032 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2033 printfPQExpBuffer(&title, _("Unlogged partitioned index \"%s.%s\""),
2034 schemaname, relationname);
2035 else
2036 printfPQExpBuffer(&title, _("Partitioned index \"%s.%s\""),
2037 schemaname, relationname);
2038 break;
2039 case RELKIND_TOASTVALUE:
2040 printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
2041 schemaname, relationname);
2042 break;
2043 case RELKIND_COMPOSITE_TYPE:
2044 printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
2045 schemaname, relationname);
2046 break;
2047 case RELKIND_FOREIGN_TABLE:
2048 printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
2049 schemaname, relationname);
2050 break;
2051 case RELKIND_PARTITIONED_TABLE:
2052 if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2053 printfPQExpBuffer(&title, _("Unlogged partitioned table \"%s.%s\""),
2054 schemaname, relationname);
2055 else
2056 printfPQExpBuffer(&title, _("Partitioned table \"%s.%s\""),
2057 schemaname, relationname);
2058 break;
2059 default:
2060 /* untranslated unknown relkind */
2061 printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
2062 tableinfo.relkind, schemaname, relationname);
2063 break;
2064 }
2065
2066 /* Fill headers[] with the names of the columns we will output */
2067 cols = 0;
2068 headers[cols++] = gettext_noop("Column");
2069 headers[cols++] = gettext_noop("Type");
2070 if (show_column_details)
2071 {
2072 headers[cols++] = gettext_noop("Collation");
2073 headers[cols++] = gettext_noop("Nullable");
2074 headers[cols++] = gettext_noop("Default");
2075 }
2076 if (isindexkey_col >= 0)
2077 headers[cols++] = gettext_noop("Key?");
2078 if (indexdef_col >= 0)
2079 headers[cols++] = gettext_noop("Definition");
2080 if (fdwopts_col >= 0)
2081 headers[cols++] = gettext_noop("FDW options");
2082 if (attstorage_col >= 0)
2083 headers[cols++] = gettext_noop("Storage");
2084 if (attcompression_col >= 0)
2085 headers[cols++] = gettext_noop("Compression");
2086 if (attstattarget_col >= 0)
2087 headers[cols++] = gettext_noop("Stats target");
2088 if (attdescr_col >= 0)
2089 headers[cols++] = gettext_noop("Description");
2090
2091 Assert(cols <= lengthof(headers));
2092
2093 printTableInit(&cont, &myopt, title.data, cols, numrows);
2094 printTableInitialized = true;
2095
2096 for (i = 0; i < cols; i++)
2097 printTableAddHeader(&cont, headers[i], true, 'l');
2098
2099 /* Generate table cells to be printed */
2100 for (i = 0; i < numrows; i++)
2101 {
2102 /* Column */
2103 printTableAddCell(&cont, PQgetvalue(res, i, attname_col), false, false);
2104
2105 /* Type */
2106 printTableAddCell(&cont, PQgetvalue(res, i, atttype_col), false, false);
2107
2108 /* Collation, Nullable, Default */
2109 if (show_column_details)
2110 {
2111 char *identity;
2112 char *generated;
2113 char *default_str;
2114 bool mustfree = false;
2115
2116 printTableAddCell(&cont, PQgetvalue(res, i, attcoll_col), false, false);
2117
2118 printTableAddCell(&cont,
2119 strcmp(PQgetvalue(res, i, attnotnull_col), "t") == 0 ? "not null" : "",
2120 false, false);
2121
2122 identity = PQgetvalue(res, i, attidentity_col);
2123 generated = PQgetvalue(res, i, attgenerated_col);
2124
2125 if (identity[0] == ATTRIBUTE_IDENTITY_ALWAYS)
2126 default_str = "generated always as identity";
2127 else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
2128 default_str = "generated by default as identity";
2129 else if (generated[0] == ATTRIBUTE_GENERATED_STORED)
2130 {
2131 default_str = psprintf("generated always as (%s) stored",
2132 PQgetvalue(res, i, attrdef_col));
2133 mustfree = true;
2134 }
2135 else if (generated[0] == ATTRIBUTE_GENERATED_VIRTUAL)
2136 {
2137 default_str = psprintf("generated always as (%s)",
2138 PQgetvalue(res, i, attrdef_col));
2139 mustfree = true;
2140 }
2141 else
2142 default_str = PQgetvalue(res, i, attrdef_col);
2143
2144 printTableAddCell(&cont, default_str, false, mustfree);
2145 }
2146
2147 /* Info for index columns */
2148 if (isindexkey_col >= 0)
2149 printTableAddCell(&cont, PQgetvalue(res, i, isindexkey_col), true, false);
2150 if (indexdef_col >= 0)
2151 printTableAddCell(&cont, PQgetvalue(res, i, indexdef_col), false, false);
2152
2153 /* FDW options for foreign table columns */
2154 if (fdwopts_col >= 0)
2155 printTableAddCell(&cont, PQgetvalue(res, i, fdwopts_col), false, false);
2156
2157 /* Storage mode, if relevant */
2158 if (attstorage_col >= 0)
2159 {
2160 char *storage = PQgetvalue(res, i, attstorage_col);
2161
2162 /* these strings are literal in our syntax, so not translated. */
2163 printTableAddCell(&cont, (storage[0] == TYPSTORAGE_PLAIN ? "plain" :
2164 (storage[0] == TYPSTORAGE_MAIN ? "main" :
2165 (storage[0] == TYPSTORAGE_EXTENDED ? "extended" :
2166 (storage[0] == TYPSTORAGE_EXTERNAL ? "external" :
2167 "???")))),
2168 false, false);
2169 }
2170
2171 /* Column compression, if relevant */
2172 if (attcompression_col >= 0)
2173 {
2174 char *compression = PQgetvalue(res, i, attcompression_col);
2175
2176 /* these strings are literal in our syntax, so not translated. */
2177 printTableAddCell(&cont, (compression[0] == 'p' ? "pglz" :
2178 (compression[0] == 'l' ? "lz4" :
2179 (compression[0] == '\0' ? "" :
2180 "???"))),
2181 false, false);
2182 }
2183
2184 /* Statistics target, if the relkind supports this feature */
2185 if (attstattarget_col >= 0)
2186 printTableAddCell(&cont, PQgetvalue(res, i, attstattarget_col),
2187 false, false);
2188
2189 /* Column comments, if the relkind supports this feature */
2190 if (attdescr_col >= 0)
2191 printTableAddCell(&cont, PQgetvalue(res, i, attdescr_col),
2192 false, false);
2193 }
2194
2195 /* Make footers */
2196
2197 if (tableinfo.ispartition)
2198 {
2199 /* Footer information for a partition child table */
2200 PGresult *result;
2201
2202 printfPQExpBuffer(&buf,
2203 "SELECT inhparent::pg_catalog.regclass,\n"
2204 " pg_catalog.pg_get_expr(c.relpartbound, c.oid),\n ");
2205
2206 appendPQExpBufferStr(&buf,
2207 pset.sversion >= 140000 ? "inhdetachpending" :
2208 "false as inhdetachpending");
2209
2210 /* If verbose, also request the partition constraint definition */
2211 if (verbose)
2212 appendPQExpBufferStr(&buf,
2213 ",\n pg_catalog.pg_get_partition_constraintdef(c.oid)");
2214 appendPQExpBuffer(&buf,
2215 "\nFROM pg_catalog.pg_class c"
2216 " JOIN pg_catalog.pg_inherits i"
2217 " ON c.oid = inhrelid"
2218 "\nWHERE c.oid = '%s';", oid);
2219 result = PSQLexec(buf.data);
2220 if (!result)
2221 goto error_return;
2222
2223 if (PQntuples(result) > 0)
2224 {
2225 char *parent_name = PQgetvalue(result, 0, 0);
2226 char *partdef = PQgetvalue(result, 0, 1);
2227 char *detached = PQgetvalue(result, 0, 2);
2228
2229 printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s%s"), parent_name,
2230 partdef,
2231 strcmp(detached, "t") == 0 ? " DETACH PENDING" : "");
2232 printTableAddFooter(&cont, tmpbuf.data);
2233
2234 if (verbose)
2235 {
2236 char *partconstraintdef = NULL;
2237
2238 if (!PQgetisnull(result, 0, 3))
2239 partconstraintdef = PQgetvalue(result, 0, 3);
2240 /* If there isn't any constraint, show that explicitly */
2241 if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
2242 printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
2243 else
2244 printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"),
2245 partconstraintdef);
2246 printTableAddFooter(&cont, tmpbuf.data);
2247 }
2248 }
2249 PQclear(result);
2250 }
2251
2252 if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2253 {
2254 /* Footer information for a partitioned table (partitioning parent) */
2255 PGresult *result;
2256
2257 printfPQExpBuffer(&buf,
2258 "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
2259 oid);
2260 result = PSQLexec(buf.data);
2261 if (!result)
2262 goto error_return;
2263
2264 if (PQntuples(result) == 1)
2265 {
2266 char *partkeydef = PQgetvalue(result, 0, 0);
2267
2268 printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2269 printTableAddFooter(&cont, tmpbuf.data);
2270 }
2271 PQclear(result);
2272 }
2273
2274 if (tableinfo.relkind == RELKIND_TOASTVALUE)
2275 {
2276 /* For a TOAST table, print name of owning table */
2277 PGresult *result;
2278
2279 printfPQExpBuffer(&buf,
2280 "SELECT n.nspname, c.relname\n"
2281 "FROM pg_catalog.pg_class c"
2282 " JOIN pg_catalog.pg_namespace n"
2283 " ON n.oid = c.relnamespace\n"
2284 "WHERE reltoastrelid = '%s';", oid);
2285 result = PSQLexec(buf.data);
2286 if (!result)
2287 goto error_return;
2288
2289 if (PQntuples(result) == 1)
2290 {
2291 char *schemaname = PQgetvalue(result, 0, 0);
2292 char *relname = PQgetvalue(result, 0, 1);
2293
2294 printfPQExpBuffer(&tmpbuf, _("Owning table: \"%s.%s\""),
2295 schemaname, relname);
2296 printTableAddFooter(&cont, tmpbuf.data);
2297 }
2298 PQclear(result);
2299 }
2300
2301 if (tableinfo.relkind == RELKIND_INDEX ||
2302 tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
2303 {
2304 /* Footer information about an index */
2305 PGresult *result;
2306
2307 printfPQExpBuffer(&buf,
2308 "SELECT i.indisunique, i.indisprimary, i.indisclustered, "
2309 "i.indisvalid,\n"
2310 " (NOT i.indimmediate) AND "
2311 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2312 "WHERE conrelid = i.indrelid AND "
2313 "conindid = i.indexrelid AND "
2314 "contype IN (" CppAsString2(CONSTRAINT_PRIMARY) ","
2315 CppAsString2(CONSTRAINT_UNIQUE) ","
2316 CppAsString2(CONSTRAINT_EXCLUSION) ") AND "
2317 "condeferrable) AS condeferrable,\n"
2318 " (NOT i.indimmediate) AND "
2319 "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2320 "WHERE conrelid = i.indrelid AND "
2321 "conindid = i.indexrelid AND "
2322 "contype IN (" CppAsString2(CONSTRAINT_PRIMARY) ","
2323 CppAsString2(CONSTRAINT_UNIQUE) ","
2324 CppAsString2(CONSTRAINT_EXCLUSION) ") AND "
2325 "condeferred) AS condeferred,\n");
2326
2327 if (pset.sversion >= 90400)
2328 appendPQExpBufferStr(&buf, "i.indisreplident,\n");
2329 else
2330 appendPQExpBufferStr(&buf, "false AS indisreplident,\n");
2331
2332 if (pset.sversion >= 150000)
2333 appendPQExpBufferStr(&buf, "i.indnullsnotdistinct,\n");
2334 else
2335 appendPQExpBufferStr(&buf, "false AS indnullsnotdistinct,\n");
2336
2337 appendPQExpBuffer(&buf, " a.amname, c2.relname, "
2338 "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
2339 "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
2340 "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
2341 "AND i.indrelid = c2.oid;",
2342 oid);
2343
2344 result = PSQLexec(buf.data);
2345 if (!result)
2346 goto error_return;
2347 else if (PQntuples(result) != 1)
2348 {
2349 PQclear(result);
2350 goto error_return;
2351 }
2352 else
2353 {
2354 char *indisunique = PQgetvalue(result, 0, 0);
2355 char *indisprimary = PQgetvalue(result, 0, 1);
2356 char *indisclustered = PQgetvalue(result, 0, 2);
2357 char *indisvalid = PQgetvalue(result, 0, 3);
2358 char *deferrable = PQgetvalue(result, 0, 4);
2359 char *deferred = PQgetvalue(result, 0, 5);
2360 char *indisreplident = PQgetvalue(result, 0, 6);
2361 char *indnullsnotdistinct = PQgetvalue(result, 0, 7);
2362 char *indamname = PQgetvalue(result, 0, 8);
2363 char *indtable = PQgetvalue(result, 0, 9);
2364 char *indpred = PQgetvalue(result, 0, 10);
2365
2366 if (strcmp(indisprimary, "t") == 0)
2367 printfPQExpBuffer(&tmpbuf, _("primary key, "));
2368 else if (strcmp(indisunique, "t") == 0)
2369 {
2370 printfPQExpBuffer(&tmpbuf, _("unique"));
2371 if (strcmp(indnullsnotdistinct, "t") == 0)
2372 appendPQExpBufferStr(&tmpbuf, _(" nulls not distinct"));
2373 appendPQExpBufferStr(&tmpbuf, _(", "));
2374 }
2375 else
2376 resetPQExpBuffer(&tmpbuf);
2377 appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
2378
2379 /* we assume here that index and table are in same schema */
2380 appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
2381 schemaname, indtable);
2382
2383 if (strlen(indpred))
2384 appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
2385
2386 if (strcmp(indisclustered, "t") == 0)
2387 appendPQExpBufferStr(&tmpbuf, _(", clustered"));
2388
2389 if (strcmp(indisvalid, "t") != 0)
2390 appendPQExpBufferStr(&tmpbuf, _(", invalid"));
2391
2392 if (strcmp(deferrable, "t") == 0)
2393 appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
2394
2395 if (strcmp(deferred, "t") == 0)
2396 appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
2397
2398 if (strcmp(indisreplident, "t") == 0)
2399 appendPQExpBufferStr(&tmpbuf, _(", replica identity"));
2400
2401 printTableAddFooter(&cont, tmpbuf.data);
2402
2403 /*
2404 * If it's a partitioned index, we'll print the tablespace below
2405 */
2406 if (tableinfo.relkind == RELKIND_INDEX)
2407 add_tablespace_footer(&cont, tableinfo.relkind,
2408 tableinfo.tablespace, true);
2409 }
2410
2411 PQclear(result);
2412 }
2413 /* If you add relkinds here, see also "Finish printing..." stanza below */
2414 else if (tableinfo.relkind == RELKIND_RELATION ||
2415 tableinfo.relkind == RELKIND_MATVIEW ||
2416 tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2417 tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
2418 tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
2419 tableinfo.relkind == RELKIND_TOASTVALUE)
2420 {
2421 /* Footer information about a table */
2422 PGresult *result = NULL;
2423 int tuples = 0;
2424
2425 /* print indexes */
2426 if (tableinfo.hasindex)
2427 {
2428 printfPQExpBuffer(&buf,
2429 "SELECT c2.relname, i.indisprimary, i.indisunique, "
2430 "i.indisclustered, i.indisvalid, "
2431 "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "
2432 "pg_catalog.pg_get_constraintdef(con.oid, true), "
2433 "contype, condeferrable, condeferred");
2434 if (pset.sversion >= 90400)
2435 appendPQExpBufferStr(&buf, ", i.indisreplident");
2436 else
2437 appendPQExpBufferStr(&buf, ", false AS indisreplident");
2438 appendPQExpBufferStr(&buf, ", c2.reltablespace");
2439 if (pset.sversion >= 180000)
2440 appendPQExpBufferStr(&buf, ", con.conperiod");
2441 else
2442 appendPQExpBufferStr(&buf, ", false AS conperiod");
2443 appendPQExpBuffer(&buf,
2444 "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
2445 " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ("
2446 CppAsString2(CONSTRAINT_PRIMARY) ","
2447 CppAsString2(CONSTRAINT_UNIQUE) ","
2448 CppAsString2(CONSTRAINT_EXCLUSION) "))\n"
2449 "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2450 "ORDER BY i.indisprimary DESC, c2.relname;",
2451 oid);
2452 result = PSQLexec(buf.data);
2453 if (!result)
2454 goto error_return;
2455 else
2456 tuples = PQntuples(result);
2457
2458 if (tuples > 0)
2459 {
2460 printTableAddFooter(&cont, _("Indexes:"));
2461 for (i = 0; i < tuples; i++)
2462 {
2463 /* untranslated index name */
2464 printfPQExpBuffer(&buf, " \"%s\"",
2465 PQgetvalue(result, i, 0));
2466
2467 /*
2468 * If exclusion constraint or PK/UNIQUE constraint WITHOUT
2469 * OVERLAPS, print the constraintdef
2470 */
2471 if (strcmp(PQgetvalue(result, i, 7), "x") == 0 ||
2472 strcmp(PQgetvalue(result, i, 12), "t") == 0)
2473 {
2474 appendPQExpBuffer(&buf, " %s",
2475 PQgetvalue(result, i, 6));
2476 }
2477 else
2478 {
2479 const char *indexdef;
2480 const char *usingpos;
2481
2482 /* Label as primary key or unique (but not both) */
2483 if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2484 appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2485 else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2486 {
2487 if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2488 appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2489 else
2490 appendPQExpBufferStr(&buf, " UNIQUE,");
2491 }
2492
2493 /* Everything after "USING" is echoed verbatim */
2494 indexdef = PQgetvalue(result, i, 5);
2495 usingpos = strstr(indexdef, " USING ");
2496 if (usingpos)
2497 indexdef = usingpos + 7;
2498 appendPQExpBuffer(&buf, " %s", indexdef);
2499
2500 /* Need these for deferrable PK/UNIQUE indexes */
2501 if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2502 appendPQExpBufferStr(&buf, " DEFERRABLE");
2503
2504 if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2505 appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2506 }
2507
2508 /* Add these for all cases */
2509 if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2510 appendPQExpBufferStr(&buf, " CLUSTER");
2511
2512 if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2513 appendPQExpBufferStr(&buf, " INVALID");
2514
2515 if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2516 appendPQExpBufferStr(&buf, " REPLICA IDENTITY");
2517
2518 printTableAddFooter(&cont, buf.data);
2519
2520 /* Print tablespace of the index on the same line */
2521 add_tablespace_footer(&cont, RELKIND_INDEX,
2522 atooid(PQgetvalue(result, i, 11)),
2523 false);
2524 }
2525 }
2526 PQclear(result);
2527 }
2528
2529 /* print table (and column) check constraints */
2530 if (tableinfo.checks)
2531 {
2532 printfPQExpBuffer(&buf,
2533 "SELECT r.conname, "
2534 "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2535 "FROM pg_catalog.pg_constraint r\n"
2536 "WHERE r.conrelid = '%s' "
2537 "AND r.contype = " CppAsString2(CONSTRAINT_CHECK) "\n"
2538 "ORDER BY 1;",
2539 oid);
2540 result = PSQLexec(buf.data);
2541 if (!result)
2542 goto error_return;
2543 else
2544 tuples = PQntuples(result);
2545
2546 if (tuples > 0)
2547 {
2548 printTableAddFooter(&cont, _("Check constraints:"));
2549 for (i = 0; i < tuples; i++)
2550 {
2551 /* untranslated constraint name and def */
2552 printfPQExpBuffer(&buf, " \"%s\" %s",
2553 PQgetvalue(result, i, 0),
2554 PQgetvalue(result, i, 1));
2555
2556 printTableAddFooter(&cont, buf.data);
2557 }
2558 }
2559 PQclear(result);
2560 }
2561
2562 /* Print foreign-key constraints */
2563 if (pset.sversion >= 120000 &&
2564 (tableinfo.ispartition || tableinfo.relkind == RELKIND_PARTITIONED_TABLE))
2565 {
2566 /*
2567 * Put the constraints defined in this table first, followed by
2568 * the constraints defined in ancestor partitioned tables.
2569 */
2570 printfPQExpBuffer(&buf,
2571 "SELECT conrelid = '%s'::pg_catalog.regclass AS sametable,\n"
2572 " conname,\n"
2573 " pg_catalog.pg_get_constraintdef(oid, true) AS condef,\n"
2574 " conrelid::pg_catalog.regclass AS ontable\n"
2575 " FROM pg_catalog.pg_constraint,\n"
2576 " pg_catalog.pg_partition_ancestors('%s')\n"
2577 " WHERE conrelid = relid AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2578 "ORDER BY sametable DESC, conname;",
2579 oid, oid);
2580 }
2581 else
2582 {
2583 printfPQExpBuffer(&buf,
2584 "SELECT true as sametable, conname,\n"
2585 " pg_catalog.pg_get_constraintdef(r.oid, true) as condef,\n"
2586 " conrelid::pg_catalog.regclass AS ontable\n"
2587 "FROM pg_catalog.pg_constraint r\n"
2588 "WHERE r.conrelid = '%s' AND r.contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n",
2589 oid);
2590
2591 if (pset.sversion >= 120000)
2592 appendPQExpBufferStr(&buf, " AND conparentid = 0\n");
2593 appendPQExpBufferStr(&buf, "ORDER BY conname");
2594 }
2595
2596 result = PSQLexec(buf.data);
2597 if (!result)
2598 goto error_return;
2599 else
2600 tuples = PQntuples(result);
2601
2602 if (tuples > 0)
2603 {
2604 int i_sametable = PQfnumber(result, "sametable"),
2605 i_conname = PQfnumber(result, "conname"),
2606 i_condef = PQfnumber(result, "condef"),
2607 i_ontable = PQfnumber(result, "ontable");
2608
2609 printTableAddFooter(&cont, _("Foreign-key constraints:"));
2610 for (i = 0; i < tuples; i++)
2611 {
2612 /*
2613 * Print untranslated constraint name and definition. Use a
2614 * "TABLE tab" prefix when the constraint is defined in a
2615 * parent partitioned table.
2616 */
2617 if (strcmp(PQgetvalue(result, i, i_sametable), "f") == 0)
2618 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2619 PQgetvalue(result, i, i_ontable),
2620 PQgetvalue(result, i, i_conname),
2621 PQgetvalue(result, i, i_condef));
2622 else
2623 printfPQExpBuffer(&buf, " \"%s\" %s",
2624 PQgetvalue(result, i, i_conname),
2625 PQgetvalue(result, i, i_condef));
2626
2627 printTableAddFooter(&cont, buf.data);
2628 }
2629 }
2630 PQclear(result);
2631
2632 /* print incoming foreign-key references */
2633 if (pset.sversion >= 120000)
2634 {
2635 printfPQExpBuffer(&buf,
2636 "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2637 " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2638 " FROM pg_catalog.pg_constraint c\n"
2639 " WHERE confrelid IN (SELECT pg_catalog.pg_partition_ancestors('%s')\n"
2640 " UNION ALL VALUES ('%s'::pg_catalog.regclass))\n"
2641 " AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2642 "ORDER BY conname;",
2643 oid, oid);
2644 }
2645 else
2646 {
2647 printfPQExpBuffer(&buf,
2648 "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2649 " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2650 " FROM pg_catalog.pg_constraint\n"
2651 " WHERE confrelid = %s AND contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n"
2652 "ORDER BY conname;",
2653 oid);
2654 }
2655
2656 result = PSQLexec(buf.data);
2657 if (!result)
2658 goto error_return;
2659 else
2660 tuples = PQntuples(result);
2661
2662 if (tuples > 0)
2663 {
2664 int i_conname = PQfnumber(result, "conname"),
2665 i_ontable = PQfnumber(result, "ontable"),
2666 i_condef = PQfnumber(result, "condef");
2667
2668 printTableAddFooter(&cont, _("Referenced by:"));
2669 for (i = 0; i < tuples; i++)
2670 {
2671 printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2672 PQgetvalue(result, i, i_ontable),
2673 PQgetvalue(result, i, i_conname),
2674 PQgetvalue(result, i, i_condef));
2675
2676 printTableAddFooter(&cont, buf.data);
2677 }
2678 }
2679 PQclear(result);
2680
2681 /* print any row-level policies */
2682 if (pset.sversion >= 90500)
2683 {
2684 printfPQExpBuffer(&buf, "SELECT pol.polname,");
2685 if (pset.sversion >= 100000)
2686 appendPQExpBufferStr(&buf,
2687 " pol.polpermissive,\n");
2688 else
2689 appendPQExpBufferStr(&buf,
2690 " 't' as polpermissive,\n");
2691 appendPQExpBuffer(&buf,
2692 " 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"
2693 " pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2694 " pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2695 " CASE pol.polcmd\n"
2696 " WHEN 'r' THEN 'SELECT'\n"
2697 " WHEN 'a' THEN 'INSERT'\n"
2698 " WHEN 'w' THEN 'UPDATE'\n"
2699 " WHEN 'd' THEN 'DELETE'\n"
2700 " END AS cmd\n"
2701 "FROM pg_catalog.pg_policy pol\n"
2702 "WHERE pol.polrelid = '%s' ORDER BY 1;",
2703 oid);
2704
2705 result = PSQLexec(buf.data);
2706 if (!result)
2707 goto error_return;
2708 else
2709 tuples = PQntuples(result);
2710
2711 /*
2712 * Handle cases where RLS is enabled and there are policies, or
2713 * there aren't policies, or RLS isn't enabled but there are
2714 * policies
2715 */
2716 if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2717 printTableAddFooter(&cont, _("Policies:"));
2718
2719 if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2720 printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2721
2722 if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2723 printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2724
2725 if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2726 printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2727
2728 if (!tableinfo.rowsecurity && tuples > 0)
2729 printTableAddFooter(&cont, _("Policies (row security disabled):"));
2730
2731 /* Might be an empty set - that's ok */
2732 for (i = 0; i < tuples; i++)
2733 {
2734 printfPQExpBuffer(&buf, " POLICY \"%s\"",
2735 PQgetvalue(result, i, 0));
2736
2737 if (*(PQgetvalue(result, i, 1)) == 'f')
2738 appendPQExpBufferStr(&buf, " AS RESTRICTIVE");
2739
2740 if (!PQgetisnull(result, i, 5))
2741 appendPQExpBuffer(&buf, " FOR %s",
2742 PQgetvalue(result, i, 5));
2743
2744 if (!PQgetisnull(result, i, 2))
2745 {
2746 appendPQExpBuffer(&buf, "\n TO %s",
2747 PQgetvalue(result, i, 2));
2748 }
2749
2750 if (!PQgetisnull(result, i, 3))
2751 appendPQExpBuffer(&buf, "\n USING (%s)",
2752 PQgetvalue(result, i, 3));
2753
2754 if (!PQgetisnull(result, i, 4))
2755 appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2756 PQgetvalue(result, i, 4));
2757
2758 printTableAddFooter(&cont, buf.data);
2759 }
2760 PQclear(result);
2761 }
2762
2763 /* print any extended statistics */
2764 if (pset.sversion >= 140000)
2765 {
2766 printfPQExpBuffer(&buf,
2767 "SELECT oid, "
2768 "stxrelid::pg_catalog.regclass, "
2769 "stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS nsp, "
2770 "stxname,\n"
2771 "pg_catalog.pg_get_statisticsobjdef_columns(oid) AS columns,\n"
2772 " " CppAsString2(STATS_EXT_NDISTINCT) " = any(stxkind) AS ndist_enabled,\n"
2773 " " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(stxkind) AS deps_enabled,\n"
2774 " " CppAsString2(STATS_EXT_MCV) " = any(stxkind) AS mcv_enabled,\n"
2775 "stxstattarget\n"
2776 "FROM pg_catalog.pg_statistic_ext\n"
2777 "WHERE stxrelid = '%s'\n"
2778 "ORDER BY nsp, stxname;",
2779 oid);
2780
2781 result = PSQLexec(buf.data);
2782 if (!result)
2783 goto error_return;
2784 else
2785 tuples = PQntuples(result);
2786
2787 if (tuples > 0)
2788 {
2789 printTableAddFooter(&cont, _("Statistics objects:"));
2790
2791 for (i = 0; i < tuples; i++)
2792 {
2793 bool gotone = false;
2794 bool has_ndistinct;
2795 bool has_dependencies;
2796 bool has_mcv;
2797 bool has_all;
2798 bool has_some;
2799
2800 has_ndistinct = (strcmp(PQgetvalue(result, i, 5), "t") == 0);
2801 has_dependencies = (strcmp(PQgetvalue(result, i, 6), "t") == 0);
2802 has_mcv = (strcmp(PQgetvalue(result, i, 7), "t") == 0);
2803
2804 printfPQExpBuffer(&buf, " ");
2805
2806 /* statistics object name (qualified with namespace) */
2807 appendPQExpBuffer(&buf, "\"%s.%s\"",
2808 PQgetvalue(result, i, 2),
2809 PQgetvalue(result, i, 3));
2810
2811 /*
2812 * When printing kinds we ignore expression statistics,
2813 * which are used only internally and can't be specified
2814 * by user. We don't print the kinds when none are
2815 * specified (in which case it has to be statistics on a
2816 * single expr) or when all are specified (in which case
2817 * we assume it's expanded by CREATE STATISTICS).
2818 */
2819 has_all = (has_ndistinct && has_dependencies && has_mcv);
2820 has_some = (has_ndistinct || has_dependencies || has_mcv);
2821
2822 if (has_some && !has_all)
2823 {
2824 appendPQExpBufferStr(&buf, " (");
2825
2826 /* options */
2827 if (has_ndistinct)
2828 {
2829 appendPQExpBufferStr(&buf, "ndistinct");
2830 gotone = true;
2831 }
2832
2833 if (has_dependencies)
2834 {
2835 appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2836 gotone = true;
2837 }
2838
2839 if (has_mcv)
2840 {
2841 appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2842 }
2843
2844 appendPQExpBufferChar(&buf, ')');
2845 }
2846
2847 appendPQExpBuffer(&buf, " ON %s FROM %s",
2848 PQgetvalue(result, i, 4),
2849 PQgetvalue(result, i, 1));
2850
2851 /* Show the stats target if it's not default */
2852 if (!PQgetisnull(result, i, 8) &&
2853 strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2854 appendPQExpBuffer(&buf, "; STATISTICS %s",
2855 PQgetvalue(result, i, 8));
2856
2857 printTableAddFooter(&cont, buf.data);
2858 }
2859 }
2860 PQclear(result);
2861 }
2862 else if (pset.sversion >= 100000)
2863 {
2864 printfPQExpBuffer(&buf,
2865 "SELECT oid, "
2866 "stxrelid::pg_catalog.regclass, "
2867 "stxnamespace::pg_catalog.regnamespace AS nsp, "
2868 "stxname,\n"
2869 " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')\n"
2870 " FROM pg_catalog.unnest(stxkeys) s(attnum)\n"
2871 " JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND\n"
2872 " a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
2873 " " CppAsString2(STATS_EXT_NDISTINCT) " = any(stxkind) AS ndist_enabled,\n"
2874 " " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(stxkind) AS deps_enabled,\n"
2875 " " CppAsString2(STATS_EXT_MCV) " = any(stxkind) AS mcv_enabled,\n");
2876
2877 if (pset.sversion >= 130000)
2878 appendPQExpBufferStr(&buf, " stxstattarget\n");
2879 else
2880 appendPQExpBufferStr(&buf, " -1 AS stxstattarget\n");
2881 appendPQExpBuffer(&buf, "FROM pg_catalog.pg_statistic_ext\n"
2882 "WHERE stxrelid = '%s'\n"
2883 "ORDER BY 1;",
2884 oid);
2885
2886 result = PSQLexec(buf.data);
2887 if (!result)
2888 goto error_return;
2889 else
2890 tuples = PQntuples(result);
2891
2892 if (tuples > 0)
2893 {
2894 printTableAddFooter(&cont, _("Statistics objects:"));
2895
2896 for (i = 0; i < tuples; i++)
2897 {
2898 bool gotone = false;
2899
2900 printfPQExpBuffer(&buf, " ");
2901
2902 /* statistics object name (qualified with namespace) */
2903 appendPQExpBuffer(&buf, "\"%s.%s\" (",
2904 PQgetvalue(result, i, 2),
2905 PQgetvalue(result, i, 3));
2906
2907 /* options */
2908 if (strcmp(PQgetvalue(result, i, 5), "t") == 0)
2909 {
2910 appendPQExpBufferStr(&buf, "ndistinct");
2911 gotone = true;
2912 }
2913
2914 if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
2915 {
2916 appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2917 gotone = true;
2918 }
2919
2920 if (strcmp(PQgetvalue(result, i, 7), "t") == 0)
2921 {
2922 appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2923 }
2924
2925 appendPQExpBuffer(&buf, ") ON %s FROM %s",
2926 PQgetvalue(result, i, 4),
2927 PQgetvalue(result, i, 1));
2928
2929 /* Show the stats target if it's not default */
2930 if (strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2931 appendPQExpBuffer(&buf, "; STATISTICS %s",
2932 PQgetvalue(result, i, 8));
2933
2934 printTableAddFooter(&cont, buf.data);
2935 }
2936 }
2937 PQclear(result);
2938 }
2939
2940 /* print rules */
2941 if (tableinfo.hasrules && tableinfo.relkind != RELKIND_MATVIEW)
2942 {
2943 printfPQExpBuffer(&buf,
2944 "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2945 "ev_enabled\n"
2946 "FROM pg_catalog.pg_rewrite r\n"
2947 "WHERE r.ev_class = '%s' ORDER BY 1;",
2948 oid);
2949 result = PSQLexec(buf.data);
2950 if (!result)
2951 goto error_return;
2952 else
2953 tuples = PQntuples(result);
2954
2955 if (tuples > 0)
2956 {
2957 bool have_heading;
2958 int category;
2959
2960 for (category = 0; category < 4; category++)
2961 {
2962 have_heading = false;
2963
2964 for (i = 0; i < tuples; i++)
2965 {
2966 const char *ruledef;
2967 bool list_rule = false;
2968
2969 switch (category)
2970 {
2971 case 0:
2972 if (*PQgetvalue(result, i, 2) == 'O')
2973 list_rule = true;
2974 break;
2975 case 1:
2976 if (*PQgetvalue(result, i, 2) == 'D')
2977 list_rule = true;
2978 break;
2979 case 2:
2980 if (*PQgetvalue(result, i, 2) == 'A')
2981 list_rule = true;
2982 break;
2983 case 3:
2984 if (*PQgetvalue(result, i, 2) == 'R')
2985 list_rule = true;
2986 break;
2987 }
2988 if (!list_rule)
2989 continue;
2990
2991 if (!have_heading)
2992 {
2993 switch (category)
2994 {
2995 case 0:
2996 printfPQExpBuffer(&buf, _("Rules:"));
2997 break;
2998 case 1:
2999 printfPQExpBuffer(&buf, _("Disabled rules:"));
3000 break;
3001 case 2:
3002 printfPQExpBuffer(&buf, _("Rules firing always:"));
3003 break;
3004 case 3:
3005 printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
3006 break;
3007 }
3008 printTableAddFooter(&cont, buf.data);
3009 have_heading = true;
3010 }
3011
3012 /* Everything after "CREATE RULE" is echoed verbatim */
3013 ruledef = PQgetvalue(result, i, 1);
3014 ruledef += 12;
3015 printfPQExpBuffer(&buf, " %s", ruledef);
3016 printTableAddFooter(&cont, buf.data);
3017 }
3018 }
3019 }
3020 PQclear(result);
3021 }
3022
3023 /* print any publications */
3024 if (pset.sversion >= 100000)
3025 {
3026 if (pset.sversion >= 150000)
3027 {
3028 printfPQExpBuffer(&buf,
3029 "SELECT pubname\n"
3030 " , NULL\n"
3031 " , NULL\n"
3032 "FROM pg_catalog.pg_publication p\n"
3033 " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
3034 " JOIN pg_catalog.pg_class pc ON pc.relnamespace = pn.pnnspid\n"
3035 "WHERE pc.oid ='%s' and pg_catalog.pg_relation_is_publishable('%s')\n"
3036 "UNION\n"
3037 "SELECT pubname\n"
3038 " , pg_get_expr(pr.prqual, c.oid)\n"
3039 " , (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
3040 " (SELECT string_agg(attname, ', ')\n"
3041 " FROM pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
3042 " pg_catalog.pg_attribute\n"
3043 " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
3044 " ELSE NULL END) "
3045 "FROM pg_catalog.pg_publication p\n"
3046 " JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3047 " JOIN pg_catalog.pg_class c ON c.oid = pr.prrelid\n"
3048 "WHERE pr.prrelid = '%s'\n"
3049 "UNION\n"
3050 "SELECT pubname\n"
3051 " , NULL\n"
3052 " , NULL\n"
3053 "FROM pg_catalog.pg_publication p\n"
3054 "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3055 "ORDER BY 1;",
3056 oid, oid, oid, oid);
3057 }
3058 else
3059 {
3060 printfPQExpBuffer(&buf,
3061 "SELECT pubname\n"
3062 " , NULL\n"
3063 " , NULL\n"
3064 "FROM pg_catalog.pg_publication p\n"
3065 "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3066 "WHERE pr.prrelid = '%s'\n"
3067 "UNION ALL\n"
3068 "SELECT pubname\n"
3069 " , NULL\n"
3070 " , NULL\n"
3071 "FROM pg_catalog.pg_publication p\n"
3072 "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3073 "ORDER BY 1;",
3074 oid, oid);
3075 }
3076
3077 result = PSQLexec(buf.data);
3078 if (!result)
3079 goto error_return;
3080 else
3081 tuples = PQntuples(result);
3082
3083 if (tuples > 0)
3084 printTableAddFooter(&cont, _("Publications:"));
3085
3086 /* Might be an empty set - that's ok */
3087 for (i = 0; i < tuples; i++)
3088 {
3089 printfPQExpBuffer(&buf, " \"%s\"",
3090 PQgetvalue(result, i, 0));
3091
3092 /* column list (if any) */
3093 if (!PQgetisnull(result, i, 2))
3094 appendPQExpBuffer(&buf, " (%s)",
3095 PQgetvalue(result, i, 2));
3096
3097 /* row filter (if any) */
3098 if (!PQgetisnull(result, i, 1))
3099 appendPQExpBuffer(&buf, " WHERE %s",
3100 PQgetvalue(result, i, 1));
3101
3102 printTableAddFooter(&cont, buf.data);
3103 }
3104 PQclear(result);
3105 }
3106
3107 /*
3108 * If verbose, print NOT NULL constraints.
3109 */
3110 if (verbose)
3111 {
3112 printfPQExpBuffer(&buf,
3113 "SELECT c.conname, a.attname, c.connoinherit,\n"
3114 " c.conislocal, c.coninhcount <> 0,\n"
3115 " c.convalidated\n"
3116 "FROM pg_catalog.pg_constraint c JOIN\n"
3117 " pg_catalog.pg_attribute a ON\n"
3118 " (a.attrelid = c.conrelid AND a.attnum = c.conkey[1])\n"
3119 "WHERE c.contype = " CppAsString2(CONSTRAINT_NOTNULL) " AND\n"
3120 " c.conrelid = '%s'::pg_catalog.regclass\n"
3121 "ORDER BY a.attnum",
3122 oid);
3123
3124 result = PSQLexec(buf.data);
3125 if (!result)
3126 goto error_return;
3127 else
3128 tuples = PQntuples(result);
3129
3130 if (tuples > 0)
3131 printTableAddFooter(&cont, _("Not-null constraints:"));
3132
3133 /* Might be an empty set - that's ok */
3134 for (i = 0; i < tuples; i++)
3135 {
3136 bool islocal = PQgetvalue(result, i, 3)[0] == 't';
3137 bool inherited = PQgetvalue(result, i, 4)[0] == 't';
3138 bool validated = PQgetvalue(result, i, 5)[0] == 't';
3139
3140 printfPQExpBuffer(&buf, " \"%s\" NOT NULL \"%s\"%s%s",
3141 PQgetvalue(result, i, 0),
3142 PQgetvalue(result, i, 1),
3143 PQgetvalue(result, i, 2)[0] == 't' ?
3144 " NO INHERIT" :
3145 islocal && inherited ? _(" (local, inherited)") :
3146 inherited ? _(" (inherited)") : "",
3147 !validated ? " NOT VALID" : "");
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
3161 printfPQExpBuffer(&buf,
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 {
3185 printfPQExpBuffer(&buf,
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
3222 printfPQExpBuffer(&buf,
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)
3242 appendPQExpBufferStr(&buf,
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
3254 appendPQExpBuffer(&buf,
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 */
3405 printfPQExpBuffer(&buf,
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) */
3440 printfPQExpBuffer(&buf,
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)
3468 appendPQExpBufferChar(&buf, ',');
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)
3478 printfPQExpBuffer(&buf,
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)
3488 printfPQExpBuffer(&buf,
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
3498 printfPQExpBuffer(&buf,
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)
3560 appendPQExpBufferChar(&buf, ',');
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
3629 error_return:
3630
3631 /* clean up */
3632 if (printTableInitialized)
3633 printTableCleanup(&cont);
3634 termPQExpBuffer(&buf);
3635 termPQExpBuffer(&title);
3636 termPQExpBuffer(&tmpbuf);
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 */
3650 static void
3651 add_tablespace_footer(printTableContent *const cont, char relkind,
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;
3669 PQExpBufferData buf;
3670
3671 initPQExpBuffer(&buf);
3672 printfPQExpBuffer(&buf,
3673 "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3674 "WHERE oid = '%u';", tablespace);
3675 result = PSQLexec(buf.data);
3676 if (!result)
3677 {
3678 termPQExpBuffer(&buf);
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);
3705 termPQExpBuffer(&buf);
3706 }
3707 }
3708 }
3709
3710 /*
3711 * \du or \dg
3712 *
3713 * Describes roles. Any schema portion of the pattern is ignored.
3714 */
3715 bool
3716 describeRoles(const char *pattern, bool verbose, bool showSystem)
3717 {
3718 PQExpBufferData buf;
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
3731 initPQExpBuffer(&buf);
3732
3733 printfPQExpBuffer(&buf,
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 {
3759 termPQExpBuffer(&buf);
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
3784 resetPQExpBuffer(&buf);
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 "));
3827 appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
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 }
3837 termPQExpBuffer(&buf);
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
3850 static void
3851 add_role_attribute(PQExpBuffer buf, const char *const str)
3852 {
3853 if (buf->len > 0)
3854 appendPQExpBufferStr(buf, ", ");
3855
3856 appendPQExpBufferStr(buf, str);
3857 }
3858
3859 /*
3860 * \drds
3861 */
3862 bool
3863 listDbRoleSettings(const char *pattern, const char *pattern2)
3864 {
3865 PQExpBufferData buf;
3866 PGresult *res;
3867 printQueryOpt myopt = pset.popt;
3868 bool havewhere;
3869
3870 initPQExpBuffer(&buf);
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);
3890 termPQExpBuffer(&buf);
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
3922 error_return:
3923 termPQExpBuffer(&buf);
3924 return false;
3925 }
3926
3927 /*
3928 * \drg
3929 * Describes role grants.
3930 */
3931 bool
3932 describeRoleGrants(const char *pattern, bool showSystem)
3933 {
3934 PQExpBufferData buf;
3935 PGresult *res;
3936 printQueryOpt myopt = pset.popt;
3937
3938 initPQExpBuffer(&buf);
3939 printfPQExpBuffer(&buf,
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)
3946 appendPQExpBufferStr(&buf,
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
3951 appendPQExpBufferStr(&buf,
3952 " CASE WHEN pam.admin_option THEN 'ADMIN' END,\n"
3953 " CASE WHEN m.rolinherit THEN 'INHERIT' END,\n"
3954 " 'SET'\n");
3955
3956 appendPQExpBuffer(&buf,
3957 " ) AS \"%s\",\n"
3958 " g.rolname AS \"%s\"\n",
3959 gettext_noop("Options"),
3960 gettext_noop("Grantor"));
3961
3962 appendPQExpBufferStr(&buf,
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 {
3975 termPQExpBuffer(&buf);
3976 return false;
3977 }
3978
3979 appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;\n");
3980
3981 res = PSQLexec(buf.data);
3982 termPQExpBuffer(&buf);
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 */
4010 bool
4011 listTables(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;
4021 PQExpBufferData buf;
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
4034 initPQExpBuffer(&buf);
4035
4036 printfPQExpBuffer(&buf,
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 {
4068 appendPQExpBuffer(&buf,
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 */
4079 appendPQExpBuffer(&buf,
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))
4102 appendPQExpBuffer(&buf,
4103 ",\n am.amname as \"%s\"",
4104 gettext_noop("Access method"));
4105
4106 appendPQExpBuffer(&buf,
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
4113 appendPQExpBufferStr(&buf,
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))
4119 appendPQExpBufferStr(&buf,
4120 "\n LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam");
4121
4122 if (showIndexes)
4123 appendPQExpBufferStr(&buf,
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 {
4163 termPQExpBuffer(&buf);
4164 return false;
4165 }
4166
4167 appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
4168
4169 res = PSQLexec(buf.data);
4170 termPQExpBuffer(&buf);
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 */
4265 bool
4266 listPartitionedTables(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;
4271 PQExpBufferData buf;
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.",
4287 formatPGVersionNumber(pset.sversion, false,
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
4307 initPQExpBuffer(&buf);
4308
4309 printfPQExpBuffer(&buf,
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 {
4319 appendPQExpBuffer(&buf,
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)
4332 appendPQExpBuffer(&buf,
4333 ",\n inh.inhparent::pg_catalog.regclass as \"%s\"",
4334 gettext_noop("Parent name"));
4335
4336 if (showIndexes)
4337 appendPQExpBuffer(&buf,
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 {
4352 appendPQExpBuffer(&buf,
4353 ",\n s.dps as \"%s\"",
4354 gettext_noop("Leaf partition size"));
4355 appendPQExpBuffer(&buf,
4356 ",\n s.tps as \"%s\"",
4357 gettext_noop("Total size"));
4358 }
4359 else
4360 /* Sizes of all partitions are considered in this case. */
4361 appendPQExpBuffer(&buf,
4362 ",\n s.tps as \"%s\"",
4363 gettext_noop("Total size"));
4364
4365 appendPQExpBuffer(&buf,
4366 ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
4367 gettext_noop("Description"));
4368 }
4369
4370 appendPQExpBufferStr(&buf,
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)
4375 appendPQExpBufferStr(&buf,
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)
4380 appendPQExpBufferStr(&buf,
4381 "\n LEFT JOIN pg_catalog.pg_inherits inh ON c.oid = inh.inhrelid");
4382
4383 if (verbose)
4384 {
4385 appendPQExpBufferStr(&buf,
4386 "\n LEFT JOIN pg_catalog.pg_am am ON c.relam = am.oid");
4387
4388 if (pset.sversion < 120000)
4389 {
4390 appendPQExpBufferStr(&buf,
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 */
4409 appendPQExpBufferStr(&buf,
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 {
4441 termPQExpBuffer(&buf);
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);
4450 termPQExpBuffer(&buf);
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 */
4475 bool
4476 listLanguages(const char *pattern, bool verbose, bool showSystem)
4477 {
4478 PQExpBufferData buf;
4479 PGresult *res;
4480 printQueryOpt myopt = pset.popt;
4481
4482 initPQExpBuffer(&buf);
4483
4484 printfPQExpBuffer(&buf,
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 {
4494 appendPQExpBuffer(&buf,
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
4506 appendPQExpBuffer(&buf,
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 {
4520 termPQExpBuffer(&buf);
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);
4532 termPQExpBuffer(&buf);
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 */
4551 bool
4552 listDomains(const char *pattern, bool verbose, bool showSystem)
4553 {
4554 PQExpBufferData buf;
4555 PGresult *res;
4556 printQueryOpt myopt = pset.popt;
4557
4558 initPQExpBuffer(&buf);
4559
4560 printfPQExpBuffer(&buf,
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");
4583 appendPQExpBuffer(&buf,
4584 ",\n d.description as \"%s\"",
4585 gettext_noop("Description"));
4586 }
4587
4588 appendPQExpBufferStr(&buf,
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)
4593 appendPQExpBufferStr(&buf,
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 {
4609 termPQExpBuffer(&buf);
4610 return false;
4611 }
4612
4613 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4614
4615 res = PSQLexec(buf.data);
4616 termPQExpBuffer(&buf);
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 */
4634 bool
4635 listConversions(const char *pattern, bool verbose, bool showSystem)
4636 {
4637 PQExpBufferData buf;
4638 PGresult *res;
4639 printQueryOpt myopt = pset.popt;
4640 static const bool translate_columns[] =
4641 {false, false, false, false, true, false};
4642
4643 initPQExpBuffer(&buf);
4644
4645 printfPQExpBuffer(&buf,
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)
4660 appendPQExpBuffer(&buf,
4661 ",\n d.description AS \"%s\"",
4662 gettext_noop("Description"));
4663
4664 appendPQExpBufferStr(&buf,
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)
4670 appendPQExpBufferStr(&buf,
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 {
4687 termPQExpBuffer(&buf);
4688 return false;
4689 }
4690
4691 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4692
4693 res = PSQLexec(buf.data);
4694 termPQExpBuffer(&buf);
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 */
4714 bool
4715 describeConfigurationParameters(const char *pattern, bool verbose,
4716 bool showSystem)
4717 {
4718 PQExpBufferData buf;
4719 PGresult *res;
4720 printQueryOpt myopt = pset.popt;
4721
4722 initPQExpBuffer(&buf);
4723 printfPQExpBuffer(&buf,
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 {
4731 appendPQExpBuffer(&buf,
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)
4745 appendPQExpBufferStr(&buf,
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);
4761 termPQExpBuffer(&buf);
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 */
4782 bool
4783 listEventTriggers(const char *pattern, bool verbose)
4784 {
4785 PQExpBufferData buf;
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.",
4796 formatPGVersionNumber(pset.sversion, false,
4797 sverbuf, sizeof(sverbuf)));
4798 return true;
4799 }
4800
4801 initPQExpBuffer(&buf);
4802
4803 printfPQExpBuffer(&buf,
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)
4825 appendPQExpBuffer(&buf,
4826 ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
4827 gettext_noop("Description"));
4828 appendPQExpBufferStr(&buf,
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 {
4835 termPQExpBuffer(&buf);
4836 return false;
4837 }
4838
4839 appendPQExpBufferStr(&buf, "ORDER BY 1");
4840
4841 res = PSQLexec(buf.data);
4842 termPQExpBuffer(&buf);
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 */
4862 bool
4863 listExtendedStats(const char *pattern)
4864 {
4865 PQExpBufferData buf;
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.",
4874 formatPGVersionNumber(pset.sversion, false,
4875 sverbuf, sizeof(sverbuf)));
4876 return true;
4877 }
4878
4879 initPQExpBuffer(&buf);
4880 printfPQExpBuffer(&buf,
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)
4888 appendPQExpBuffer(&buf,
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
4894 appendPQExpBuffer(&buf,
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
4905 appendPQExpBuffer(&buf,
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 {
4918 appendPQExpBuffer(&buf,
4919 ",\nCASE WHEN " CppAsString2(STATS_EXT_MCV) " = any(es.stxkind) THEN 'defined' \n"
4920 "END AS \"%s\" ",
4921 gettext_noop("MCV"));
4922 }
4923
4924 appendPQExpBufferStr(&buf,
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 {
4933 termPQExpBuffer(&buf);
4934 return false;
4935 }
4936
4937 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4938
4939 res = PSQLexec(buf.data);
4940 termPQExpBuffer(&buf);
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 */
4958 bool
4959 listCasts(const char *pattern, bool verbose)
4960 {
4961 PQExpBufferData buf;
4962 PGresult *res;
4963 printQueryOpt myopt = pset.popt;
4964 static const bool translate_columns[] = {false, false, false, true, true, false};
4965
4966 initPQExpBuffer(&buf);
4967
4968 printfPQExpBuffer(&buf,
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 */
4979 appendPQExpBuffer(&buf,
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
4988 appendPQExpBuffer(&buf,
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)
5001 appendPQExpBuffer(&buf,
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 */
5015 appendPQExpBufferStr(&buf,
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)
5028 appendPQExpBufferStr(&buf,
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);
5058 termPQExpBuffer(&buf);
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
5072 error_return:
5073 termPQExpBuffer(&buf);
5074 return false;
5075 }
5076
5077 /*
5078 * \dO
5079 *
5080 * Describes collations.
5081 */
5082 bool
5083 listCollations(const char *pattern, bool verbose, bool showSystem)
5084 {
5085 PQExpBufferData buf;
5086 PGresult *res;
5087 printQueryOpt myopt = pset.popt;
5088 static const bool translate_columns[] = {false, false, false, false, false, false, false, true, false};
5089
5090 initPQExpBuffer(&buf);
5091
5092 printfPQExpBuffer(&buf,
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)
5100 appendPQExpBuffer(&buf,
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
5109 appendPQExpBuffer(&buf,
5110 " 'libc' AS \"%s\",\n",
5111 gettext_noop("Provider"));
5112
5113 appendPQExpBuffer(&buf,
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)
5120 appendPQExpBuffer(&buf,
5121 " c.colllocale AS \"%s\",\n",
5122 gettext_noop("Locale"));
5123 else if (pset.sversion >= 150000)
5124 appendPQExpBuffer(&buf,
5125 " c.colliculocale AS \"%s\",\n",
5126 gettext_noop("Locale"));
5127 else
5128 appendPQExpBuffer(&buf,
5129 " c.collcollate AS \"%s\",\n",
5130 gettext_noop("Locale"));
5131
5132 if (pset.sversion >= 160000)
5133 appendPQExpBuffer(&buf,
5134 " c.collicurules AS \"%s\",\n",
5135 gettext_noop("ICU Rules"));
5136 else
5137 appendPQExpBuffer(&buf,
5138 " NULL AS \"%s\",\n",
5139 gettext_noop("ICU Rules"));
5140
5141 if (pset.sversion >= 120000)
5142 appendPQExpBuffer(&buf,
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
5147 appendPQExpBuffer(&buf,
5148 " '%s' AS \"%s\"",
5149 gettext_noop("yes"),
5150 gettext_noop("Deterministic?"));
5151
5152 if (verbose)
5153 appendPQExpBuffer(&buf,
5154 ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
5155 gettext_noop("Description"));
5156
5157 appendPQExpBufferStr(&buf,
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 {
5178 termPQExpBuffer(&buf);
5179 return false;
5180 }
5181
5182 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5183
5184 res = PSQLexec(buf.data);
5185 termPQExpBuffer(&buf);
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 */
5205 bool
5206 listSchemas(const char *pattern, bool verbose, bool showSystem)
5207 {
5208 PQExpBufferData buf;
5209 PGresult *res;
5210 printQueryOpt myopt = pset.popt;
5211 int pub_schema_tuples = 0;
5212 char **footers = NULL;
5213
5214 initPQExpBuffer(&buf);
5215 printfPQExpBuffer(&buf,
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");
5225 appendPQExpBuffer(&buf,
5226 ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
5227 gettext_noop("Description"));
5228 }
5229
5230 appendPQExpBufferStr(&buf,
5231 "\nFROM pg_catalog.pg_namespace n\n");
5232
5233 if (!showSystem && !pattern)
5234 appendPQExpBufferStr(&buf,
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
5258 printfPQExpBuffer(&buf,
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
5300 termPQExpBuffer(&buf);
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
5316 error_return:
5317 termPQExpBuffer(&buf);
5318 return false;
5319 }
5320
5321
5322 /*
5323 * \dFp
5324 * list text search parsers
5325 */
5326 bool
5327 listTSParsers(const char *pattern, bool verbose)
5328 {
5329 PQExpBufferData buf;
5330 PGresult *res;
5331 printQueryOpt myopt = pset.popt;
5332
5333 if (verbose)
5334 return listTSParsersVerbose(pattern);
5335
5336 initPQExpBuffer(&buf);
5337
5338 printfPQExpBuffer(&buf,
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 {
5355 termPQExpBuffer(&buf);
5356 return false;
5357 }
5358
5359 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5360
5361 res = PSQLexec(buf.data);
5362 termPQExpBuffer(&buf);
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 */
5378 static bool
5379 listTSParsersVerbose(const char *pattern)
5380 {
5381 PQExpBufferData buf;
5382 PGresult *res;
5383 int i;
5384
5385 initPQExpBuffer(&buf);
5386
5387 printfPQExpBuffer(&buf,
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 {
5400 termPQExpBuffer(&buf);
5401 return false;
5402 }
5403
5404 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5405
5406 res = PSQLexec(buf.data);
5407 termPQExpBuffer(&buf);
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
5453 static bool
5454 describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
5455 {
5456 PQExpBufferData buf;
5457 PGresult *res;
5458 PQExpBufferData title;
5459 printQueryOpt myopt = pset.popt;
5460 static const bool translate_columns[] = {true, false, false};
5461
5462 initPQExpBuffer(&buf);
5463
5464 printfPQExpBuffer(&buf,
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);
5509 termPQExpBuffer(&buf);
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
5530 initPQExpBuffer(&buf);
5531
5532 printfPQExpBuffer(&buf,
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);
5542 termPQExpBuffer(&buf);
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 */
5573 bool
5574 listTSDictionaries(const char *pattern, bool verbose)
5575 {
5576 PQExpBufferData buf;
5577 PGresult *res;
5578 printQueryOpt myopt = pset.popt;
5579
5580 initPQExpBuffer(&buf);
5581
5582 printfPQExpBuffer(&buf,
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 {
5591 appendPQExpBuffer(&buf,
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
5601 appendPQExpBuffer(&buf,
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 {
5613 termPQExpBuffer(&buf);
5614 return false;
5615 }
5616
5617 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5618
5619 res = PSQLexec(buf.data);
5620 termPQExpBuffer(&buf);
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 */
5638 bool
5639 listTSTemplates(const char *pattern, bool verbose)
5640 {
5641 PQExpBufferData buf;
5642 PGresult *res;
5643 printQueryOpt myopt = pset.popt;
5644
5645 initPQExpBuffer(&buf);
5646
5647 if (verbose)
5648 printfPQExpBuffer(&buf,
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
5661 printfPQExpBuffer(&buf,
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 {
5678 termPQExpBuffer(&buf);
5679 return false;
5680 }
5681
5682 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5683
5684 res = PSQLexec(buf.data);
5685 termPQExpBuffer(&buf);
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 */
5703 bool
5704 listTSConfigs(const char *pattern, bool verbose)
5705 {
5706 PQExpBufferData buf;
5707 PGresult *res;
5708 printQueryOpt myopt = pset.popt;
5709
5710 if (verbose)
5711 return listTSConfigsVerbose(pattern);
5712
5713 initPQExpBuffer(&buf);
5714
5715 printfPQExpBuffer(&buf,
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 {
5732 termPQExpBuffer(&buf);
5733 return false;
5734 }
5735
5736 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5737
5738 res = PSQLexec(buf.data);
5739 termPQExpBuffer(&buf);
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
5752 static bool
5753 listTSConfigsVerbose(const char *pattern)
5754 {
5755 PQExpBufferData buf;
5756 PGresult *res;
5757 int i;
5758
5759 initPQExpBuffer(&buf);
5760
5761 printfPQExpBuffer(&buf,
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 {
5778 termPQExpBuffer(&buf);
5779 return false;
5780 }
5781
5782 appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
5783
5784 res = PSQLexec(buf.data);
5785 termPQExpBuffer(&buf);
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
5836 static bool
5837 describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
5838 const char *pnspname, const char *prsname)
5839 {
5840 PQExpBufferData buf,
5841 title;
5842 PGresult *res;
5843 printQueryOpt myopt = pset.popt;
5844
5845 initPQExpBuffer(&buf);
5846
5847 printfPQExpBuffer(&buf,
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);
5868 termPQExpBuffer(&buf);
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 */
5907 bool
5908 listForeignDataWrappers(const char *pattern, bool verbose)
5909 {
5910 PQExpBufferData buf;
5911 PGresult *res;
5912 printQueryOpt myopt = pset.popt;
5913
5914 initPQExpBuffer(&buf);
5915 printfPQExpBuffer(&buf,
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");
5929 appendPQExpBuffer(&buf,
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)
5944 appendPQExpBufferStr(&buf,
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 {
5953 termPQExpBuffer(&buf);
5954 return false;
5955 }
5956
5957 appendPQExpBufferStr(&buf, "ORDER BY 1;");
5958
5959 res = PSQLexec(buf.data);
5960 termPQExpBuffer(&buf);
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 */
5978 bool
5979 listForeignServers(const char *pattern, bool verbose)
5980 {
5981 PQExpBufferData buf;
5982 PGresult *res;
5983 printQueryOpt myopt = pset.popt;
5984
5985 initPQExpBuffer(&buf);
5986 printfPQExpBuffer(&buf,
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");
5998 appendPQExpBuffer(&buf,
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
6015 appendPQExpBufferStr(&buf,
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)
6020 appendPQExpBufferStr(&buf,
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 {
6029 termPQExpBuffer(&buf);
6030 return false;
6031 }
6032
6033 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6034
6035 res = PSQLexec(buf.data);
6036 termPQExpBuffer(&buf);
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 */
6054 bool
6055 listUserMappings(const char *pattern, bool verbose)
6056 {
6057 PQExpBufferData buf;
6058 PGresult *res;
6059 printQueryOpt myopt = pset.popt;
6060
6061 initPQExpBuffer(&buf);
6062 printfPQExpBuffer(&buf,
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)
6069 appendPQExpBuffer(&buf,
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 {
6084 termPQExpBuffer(&buf);
6085 return false;
6086 }
6087
6088 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6089
6090 res = PSQLexec(buf.data);
6091 termPQExpBuffer(&buf);
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 */
6109 bool
6110 listForeignTables(const char *pattern, bool verbose)
6111 {
6112 PQExpBufferData buf;
6113 PGresult *res;
6114 printQueryOpt myopt = pset.popt;
6115
6116 initPQExpBuffer(&buf);
6117 printfPQExpBuffer(&buf,
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)
6126 appendPQExpBuffer(&buf,
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
6137 appendPQExpBufferStr(&buf,
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)
6146 appendPQExpBufferStr(&buf,
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 {
6156 termPQExpBuffer(&buf);
6157 return false;
6158 }
6159
6160 appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6161
6162 res = PSQLexec(buf.data);
6163 termPQExpBuffer(&buf);
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 */
6181 bool
6182 listExtensions(const char *pattern)
6183 {
6184 PQExpBufferData buf;
6185 PGresult *res;
6186 printQueryOpt myopt = pset.popt;
6187
6188 initPQExpBuffer(&buf);
6189 printfPQExpBuffer(&buf,
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 "AND d.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass "
6197 "LEFT JOIN pg_catalog.pg_available_extensions() ae(name, default_version, comment) ON ae.name = e.extname\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 {
6210 termPQExpBuffer(&buf);
6211 return false;
6212 }
6213
6214 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6215
6216 res = PSQLexec(buf.data);
6217 termPQExpBuffer(&buf);
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 */
6235 bool
6236 listExtensionContents(const char *pattern)
6237 {
6238 PQExpBufferData buf;
6239 PGresult *res;
6240 int i;
6241
6242 initPQExpBuffer(&buf);
6243 printfPQExpBuffer(&buf,
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 {
6253 termPQExpBuffer(&buf);
6254 return false;
6255 }
6256
6257 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6258
6259 res = PSQLexec(buf.data);
6260 termPQExpBuffer(&buf);
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
6302 static bool
6303 listOneExtensionContents(const char *extname, const char *oid)
6304 {
6305 PQExpBufferData buf;
6306 PGresult *res;
6307 PQExpBufferData title;
6308 printQueryOpt myopt = pset.popt;
6309
6310 initPQExpBuffer(&buf);
6311 printfPQExpBuffer(&buf,
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);
6320 termPQExpBuffer(&buf);
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 */
6346 static bool
6347 validateSQLNamePattern(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
6388 error_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 */
6399 bool
6400 listPublications(const char *pattern)
6401 {
6402 PQExpBufferData buf;
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.",
6412 formatPGVersionNumber(pset.sversion, false,
6413 sverbuf, sizeof(sverbuf)));
6414 return true;
6415 }
6416
6417 initPQExpBuffer(&buf);
6418
6419 printfPQExpBuffer(&buf,
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)
6433 appendPQExpBuffer(&buf,
6434 ",\n pubtruncate AS \"%s\"",
6435 gettext_noop("Truncates"));
6436 if (pset.sversion >= 180000)
6437 appendPQExpBuffer(&buf,
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)
6446 appendPQExpBuffer(&buf,
6447 ",\n pubviaroot AS \"%s\"",
6448 gettext_noop("Via root"));
6449
6450 appendPQExpBufferStr(&buf,
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 {
6458 termPQExpBuffer(&buf);
6459 return false;
6460 }
6461
6462 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6463
6464 res = PSQLexec(buf.data);
6465 termPQExpBuffer(&buf);
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 */
6484 static bool
6485 addFooterToPublicationDesc(PQExpBuffer buf, const char *footermsg,
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 */
6530 bool
6531 describePublications(const char *pattern)
6532 {
6533 PQExpBufferData buf;
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.",
6548 formatPGVersionNumber(pset.sversion, false,
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
6557 initPQExpBuffer(&buf);
6558
6559 printfPQExpBuffer(&buf,
6560 "SELECT oid, pubname,\n"
6561 " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
6562 " puballtables, pubinsert, pubupdate, pubdelete");
6563 if (has_pubtruncate)
6564 appendPQExpBufferStr(&buf,
6565 ", pubtruncate");
6566 else
6567 appendPQExpBufferStr(&buf,
6568 ", false AS pubtruncate");
6569
6570 if (has_pubgencols)
6571 appendPQExpBuffer(&buf,
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
6580 appendPQExpBufferStr(&buf,
6581 ", 'none' AS pubgencols");
6582
6583 if (has_pubviaroot)
6584 appendPQExpBufferStr(&buf,
6585 ", pubviaroot");
6586 else
6587 appendPQExpBufferStr(&buf,
6588 ", false AS pubviaroot");
6589
6590 appendPQExpBufferStr(&buf,
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 {
6598 termPQExpBuffer(&buf);
6599 return false;
6600 }
6601
6602 appendPQExpBufferStr(&buf, "ORDER BY 2;");
6603
6604 res = PSQLexec(buf.data);
6605 if (!res)
6606 {
6607 termPQExpBuffer(&buf);
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
6622 termPQExpBuffer(&buf);
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 */
6675 printfPQExpBuffer(&buf,
6676 "SELECT n.nspname, c.relname");
6677 if (pset.sversion >= 150000)
6678 {
6679 appendPQExpBufferStr(&buf,
6680 ", pg_get_expr(pr.prqual, c.oid)");
6681 appendPQExpBufferStr(&buf,
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
6692 appendPQExpBufferStr(&buf,
6693 ", NULL, NULL");
6694 appendPQExpBuffer(&buf,
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 */
6708 printfPQExpBuffer(&buf,
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
6726 termPQExpBuffer(&buf);
6727 PQclear(res);
6728
6729 return true;
6730
6731 error_return:
6732 printTableCleanup(&cont);
6733 PQclear(res);
6734 termPQExpBuffer(&buf);
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 */
6745 bool
6746 describeSubscriptions(const char *pattern, bool verbose)
6747 {
6748 PQExpBufferData buf;
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.",
6760 formatPGVersionNumber(pset.sversion, false,
6761 sverbuf, sizeof(sverbuf)));
6762 return true;
6763 }
6764
6765 initPQExpBuffer(&buf);
6766
6767 printfPQExpBuffer(&buf,
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 {
6782 appendPQExpBuffer(&buf,
6783 ", subbinary AS \"%s\"\n",
6784 gettext_noop("Binary"));
6785
6786 if (pset.sversion >= 160000)
6787 appendPQExpBuffer(&buf,
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
6795 appendPQExpBuffer(&buf,
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)
6802 appendPQExpBuffer(&buf,
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)
6809 appendPQExpBuffer(&buf,
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)
6818 appendPQExpBuffer(&buf,
6819 ", subfailover AS \"%s\"\n",
6820 gettext_noop("Failover"));
6821
6822 appendPQExpBuffer(&buf,
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)
6830 appendPQExpBuffer(&buf,
6831 ", subskiplsn AS \"%s\"\n",
6832 gettext_noop("Skip LSN"));
6833 }
6834
6835 /* Only display subscriptions in current database. */
6836 appendPQExpBufferStr(&buf,
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 {
6847 termPQExpBuffer(&buf);
6848 return false;
6849 }
6850
6851 appendPQExpBufferStr(&buf, "ORDER BY 1;");
6852
6853 res = PSQLexec(buf.data);
6854 termPQExpBuffer(&buf);
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 */
6879 static void
6880 printACLColumn(PQExpBuffer buf, const char *colname)
6881 {
6882 appendPQExpBuffer(buf,
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 */
6897 bool
6898 listOperatorClasses(const char *access_method_pattern,
6899 const char *type_pattern, bool verbose)
6900 {
6901 PQExpBufferData buf;
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
6907 initPQExpBuffer(&buf);
6908
6909 printfPQExpBuffer(&buf,
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)
6935 appendPQExpBuffer(&buf,
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"));
6944 appendPQExpBufferStr(&buf,
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)
6951 appendPQExpBufferStr(&buf,
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);
6973 termPQExpBuffer(&buf);
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
6987 error_return:
6988 termPQExpBuffer(&buf);
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 */
6998 bool
6999 listOperatorFamilies(const char *access_method_pattern,
7000 const char *type_pattern, bool verbose)
7001 {
7002 PQExpBufferData buf;
7003 PGresult *res;
7004 printQueryOpt myopt = pset.popt;
7005 bool have_where = false;
7006 static const bool translate_columns[] = {false, false, false, false};
7007
7008 initPQExpBuffer(&buf);
7009
7010 printfPQExpBuffer(&buf,
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)
7026 appendPQExpBuffer(&buf,
7027 ",\n pg_catalog.pg_get_userbyid(f.opfowner) AS \"%s\"\n",
7028 gettext_noop("Owner"));
7029 appendPQExpBufferStr(&buf,
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 {
7041 appendPQExpBuffer(&buf,
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);
7061 termPQExpBuffer(&buf);
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
7075 error_return:
7076 termPQExpBuffer(&buf);
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 */
7087 bool
7088 listOpFamilyOperators(const char *access_method_pattern,
7089 const char *family_pattern, bool verbose)
7090 {
7091 PQExpBufferData buf;
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
7098 initPQExpBuffer(&buf);
7099
7100 printfPQExpBuffer(&buf,
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)
7123 appendPQExpBuffer(&buf,
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?"));
7133 appendPQExpBufferStr(&buf,
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)
7139 appendPQExpBufferStr(&buf,
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);
7168 termPQExpBuffer(&buf);
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
7182 error_return:
7183 termPQExpBuffer(&buf);
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 */
7194 bool
7195 listOpFamilyFunctions(const char *access_method_pattern,
7196 const char *family_pattern, bool verbose)
7197 {
7198 PQExpBufferData buf;
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
7204 initPQExpBuffer(&buf);
7205
7206 printfPQExpBuffer(&buf,
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)
7224 appendPQExpBuffer(&buf,
7225 ", p.proname AS \"%s\"\n",
7226 gettext_noop("Function"));
7227 else
7228 appendPQExpBuffer(&buf,
7229 ", ap.amproc::pg_catalog.regprocedure AS \"%s\"\n",
7230 gettext_noop("Function"));
7231
7232 appendPQExpBufferStr(&buf,
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);
7260 termPQExpBuffer(&buf);
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
7274 error_return:
7275 termPQExpBuffer(&buf);
7276 return false;
7277 }
7278
7279 /*
7280 * \dl or \lo_list
7281 * Lists large objects
7282 */
7283 bool
7284 listLargeObjects(bool verbose)
7285 {
7286 PQExpBufferData buf;
7287 PGresult *res;
7288 printQueryOpt myopt = pset.popt;
7289
7290 initPQExpBuffer(&buf);
7291
7292 printfPQExpBuffer(&buf,
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
7304 appendPQExpBuffer(&buf,
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);
7311 termPQExpBuffer(&buf);
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 }