Line data Source code
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 48 : describeAggregates(const char *pattern, bool verbose, bool showSystem)
79 : {
80 : PQExpBufferData buf;
81 : PGresult *res;
82 48 : printQueryOpt myopt = pset.popt;
83 :
84 48 : initPQExpBuffer(&buf);
85 :
86 48 : 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 48 : if (pset.sversion >= 110000)
100 48 : 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 0 : 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 48 : if (!showSystem && !pattern)
115 0 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
116 : " AND n.nspname <> 'information_schema'\n");
117 :
118 48 : 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 24 : termPQExpBuffer(&buf);
124 24 : return false;
125 : }
126 :
127 24 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
128 :
129 24 : res = PSQLexec(buf.data);
130 24 : termPQExpBuffer(&buf);
131 24 : if (!res)
132 0 : return false;
133 :
134 24 : myopt.title = _("List of aggregate functions");
135 24 : myopt.translate_header = true;
136 :
137 24 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
138 :
139 24 : PQclear(res);
140 24 : return true;
141 : }
142 :
143 : /*
144 : * \dA
145 : * Takes an optional regexp to select particular access methods
146 : */
147 : bool
148 78 : describeAccessMethods(const char *pattern, bool verbose)
149 : {
150 : PQExpBufferData buf;
151 : PGresult *res;
152 78 : printQueryOpt myopt = pset.popt;
153 : static const bool translate_columns[] = {false, true, false, false};
154 :
155 78 : if (pset.sversion < 90600)
156 : {
157 : char sverbuf[32];
158 :
159 0 : pg_log_error("The server (version %s) does not support access methods.",
160 : formatPGVersionNumber(pset.sversion, false,
161 : sverbuf, sizeof(sverbuf)));
162 0 : return true;
163 : }
164 :
165 78 : initPQExpBuffer(&buf);
166 :
167 78 : 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 78 : if (verbose)
179 : {
180 24 : 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 78 : appendPQExpBufferStr(&buf,
188 : "\nFROM pg_catalog.pg_am\n");
189 :
190 78 : if (!validateSQLNamePattern(&buf, pattern, false, false,
191 : NULL, "amname", NULL,
192 : NULL,
193 : NULL, 1))
194 : {
195 18 : termPQExpBuffer(&buf);
196 18 : return false;
197 : }
198 :
199 60 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
200 :
201 60 : res = PSQLexec(buf.data);
202 60 : termPQExpBuffer(&buf);
203 60 : if (!res)
204 0 : return false;
205 :
206 60 : myopt.title = _("List of access methods");
207 60 : myopt.translate_header = true;
208 60 : myopt.translate_columns = translate_columns;
209 60 : myopt.n_translate_columns = lengthof(translate_columns);
210 :
211 60 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
212 :
213 60 : PQclear(res);
214 60 : return true;
215 : }
216 :
217 : /*
218 : * \db
219 : * Takes an optional regexp to select particular tablespaces
220 : */
221 : bool
222 24 : describeTablespaces(const char *pattern, bool verbose)
223 : {
224 : PQExpBufferData buf;
225 : PGresult *res;
226 24 : printQueryOpt myopt = pset.popt;
227 :
228 24 : initPQExpBuffer(&buf);
229 :
230 24 : 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 24 : if (verbose)
239 : {
240 0 : appendPQExpBufferStr(&buf, ",\n ");
241 0 : printACLColumn(&buf, "spcacl");
242 0 : 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 24 : appendPQExpBufferStr(&buf,
252 : "\nFROM pg_catalog.pg_tablespace\n");
253 :
254 24 : if (!validateSQLNamePattern(&buf, pattern, false, false,
255 : NULL, "spcname", NULL,
256 : NULL,
257 : NULL, 1))
258 : {
259 18 : termPQExpBuffer(&buf);
260 18 : return false;
261 : }
262 :
263 6 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
264 :
265 6 : res = PSQLexec(buf.data);
266 6 : termPQExpBuffer(&buf);
267 6 : if (!res)
268 0 : return false;
269 :
270 6 : myopt.title = _("List of tablespaces");
271 6 : myopt.translate_header = true;
272 :
273 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
274 :
275 6 : PQclear(res);
276 6 : 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 298 : describeFunctions(const char *functypes, const char *func_pattern,
296 : char **arg_patterns, int num_arg_patterns,
297 : bool verbose, bool showSystem)
298 : {
299 298 : bool showAggregate = strchr(functypes, 'a') != NULL;
300 298 : bool showNormal = strchr(functypes, 'n') != NULL;
301 298 : bool showProcedure = strchr(functypes, 'p') != NULL;
302 298 : bool showTrigger = strchr(functypes, 't') != NULL;
303 298 : bool showWindow = strchr(functypes, 'w') != NULL;
304 : bool have_where;
305 : PQExpBufferData buf;
306 : PGresult *res;
307 298 : printQueryOpt myopt = pset.popt;
308 : static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, true, false, false, false, false};
309 :
310 : /* No "Parallel" column before 9.6 */
311 : static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, true, false, false, false, false};
312 :
313 298 : if (strlen(functypes) != strspn(functypes, "anptwSx+"))
314 : {
315 0 : pg_log_error("\\df only takes [anptwSx+] as options");
316 0 : return true;
317 : }
318 :
319 298 : if (showProcedure && pset.sversion < 110000)
320 : {
321 : char sverbuf[32];
322 :
323 0 : pg_log_error("\\df does not take a \"%c\" option with server version %s",
324 : 'p',
325 : formatPGVersionNumber(pset.sversion, false,
326 : sverbuf, sizeof(sverbuf)));
327 0 : return true;
328 : }
329 :
330 298 : if (!showAggregate && !showNormal && !showProcedure && !showTrigger && !showWindow)
331 : {
332 280 : showAggregate = showNormal = showTrigger = showWindow = true;
333 280 : if (pset.sversion >= 110000)
334 280 : showProcedure = true;
335 : }
336 :
337 298 : initPQExpBuffer(&buf);
338 :
339 298 : printfPQExpBuffer(&buf,
340 : "SELECT n.nspname as \"%s\",\n"
341 : " p.proname as \"%s\",\n",
342 : gettext_noop("Schema"),
343 : gettext_noop("Name"));
344 :
345 298 : if (pset.sversion >= 110000)
346 298 : appendPQExpBuffer(&buf,
347 : " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
348 : " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
349 : " CASE p.prokind\n"
350 : " WHEN " CppAsString2(PROKIND_AGGREGATE) " THEN '%s'\n"
351 : " WHEN " CppAsString2(PROKIND_WINDOW) " THEN '%s'\n"
352 : " WHEN " CppAsString2(PROKIND_PROCEDURE) " THEN '%s'\n"
353 : " ELSE '%s'\n"
354 : " END as \"%s\"",
355 : gettext_noop("Result data type"),
356 : gettext_noop("Argument data types"),
357 : /* translator: "agg" is short for "aggregate" */
358 : gettext_noop("agg"),
359 : gettext_noop("window"),
360 : gettext_noop("proc"),
361 : gettext_noop("func"),
362 : gettext_noop("Type"));
363 : else
364 0 : appendPQExpBuffer(&buf,
365 : " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
366 : " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
367 : " CASE\n"
368 : " WHEN p.proisagg THEN '%s'\n"
369 : " WHEN p.proiswindow THEN '%s'\n"
370 : " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
371 : " ELSE '%s'\n"
372 : " END as \"%s\"",
373 : gettext_noop("Result data type"),
374 : gettext_noop("Argument data types"),
375 : /* translator: "agg" is short for "aggregate" */
376 : gettext_noop("agg"),
377 : gettext_noop("window"),
378 : gettext_noop("trigger"),
379 : gettext_noop("func"),
380 : gettext_noop("Type"));
381 :
382 298 : if (verbose)
383 : {
384 12 : appendPQExpBuffer(&buf,
385 : ",\n CASE\n"
386 : " WHEN p.provolatile = "
387 : CppAsString2(PROVOLATILE_IMMUTABLE) " THEN '%s'\n"
388 : " WHEN p.provolatile = "
389 : CppAsString2(PROVOLATILE_STABLE) " THEN '%s'\n"
390 : " WHEN p.provolatile = "
391 : CppAsString2(PROVOLATILE_VOLATILE) " THEN '%s'\n"
392 : " END as \"%s\"",
393 : gettext_noop("immutable"),
394 : gettext_noop("stable"),
395 : gettext_noop("volatile"),
396 : gettext_noop("Volatility"));
397 12 : if (pset.sversion >= 90600)
398 12 : appendPQExpBuffer(&buf,
399 : ",\n CASE\n"
400 : " WHEN p.proparallel = "
401 : CppAsString2(PROPARALLEL_RESTRICTED) " THEN '%s'\n"
402 : " WHEN p.proparallel = "
403 : CppAsString2(PROPARALLEL_SAFE) " THEN '%s'\n"
404 : " WHEN p.proparallel = "
405 : CppAsString2(PROPARALLEL_UNSAFE) " THEN '%s'\n"
406 : " END as \"%s\"",
407 : gettext_noop("restricted"),
408 : gettext_noop("safe"),
409 : gettext_noop("unsafe"),
410 : gettext_noop("Parallel"));
411 12 : appendPQExpBuffer(&buf,
412 : ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
413 : ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\""
414 : ",\n CASE WHEN p.proleakproof THEN '%s' ELSE '%s' END as \"%s\"",
415 : gettext_noop("Owner"),
416 : gettext_noop("definer"),
417 : gettext_noop("invoker"),
418 : gettext_noop("Security"),
419 : gettext_noop("yes"),
420 : gettext_noop("no"),
421 : gettext_noop("Leakproof?"));
422 12 : appendPQExpBufferStr(&buf, ",\n ");
423 12 : printACLColumn(&buf, "p.proacl");
424 12 : appendPQExpBuffer(&buf,
425 : ",\n l.lanname as \"%s\"",
426 : gettext_noop("Language"));
427 12 : appendPQExpBuffer(&buf,
428 : ",\n CASE WHEN l.lanname IN ('internal', 'c') THEN p.prosrc END as \"%s\"",
429 : gettext_noop("Internal name"));
430 12 : appendPQExpBuffer(&buf,
431 : ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
432 : gettext_noop("Description"));
433 : }
434 :
435 298 : appendPQExpBufferStr(&buf,
436 : "\nFROM pg_catalog.pg_proc p"
437 : "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
438 :
439 364 : for (int i = 0; i < num_arg_patterns; i++)
440 : {
441 66 : appendPQExpBuffer(&buf,
442 : " LEFT JOIN pg_catalog.pg_type t%d ON t%d.oid = p.proargtypes[%d]\n"
443 : " LEFT JOIN pg_catalog.pg_namespace nt%d ON nt%d.oid = t%d.typnamespace\n",
444 : i, i, i, i, i, i);
445 : }
446 :
447 298 : if (verbose)
448 12 : appendPQExpBufferStr(&buf,
449 : " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
450 :
451 298 : have_where = false;
452 :
453 : /* filter by function type, if requested */
454 298 : if (showNormal && showAggregate && showProcedure && showTrigger && showWindow)
455 : /* Do nothing */ ;
456 18 : else if (showNormal)
457 : {
458 6 : if (!showAggregate)
459 : {
460 6 : if (have_where)
461 0 : appendPQExpBufferStr(&buf, " AND ");
462 : else
463 : {
464 6 : appendPQExpBufferStr(&buf, "WHERE ");
465 6 : have_where = true;
466 : }
467 6 : if (pset.sversion >= 110000)
468 6 : appendPQExpBufferStr(&buf, "p.prokind <> "
469 : CppAsString2(PROKIND_AGGREGATE) "\n");
470 : else
471 0 : appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
472 : }
473 6 : if (!showProcedure && pset.sversion >= 110000)
474 : {
475 6 : if (have_where)
476 6 : appendPQExpBufferStr(&buf, " AND ");
477 : else
478 : {
479 0 : appendPQExpBufferStr(&buf, "WHERE ");
480 0 : have_where = true;
481 : }
482 6 : appendPQExpBufferStr(&buf, "p.prokind <> "
483 : CppAsString2(PROKIND_PROCEDURE) "\n");
484 : }
485 6 : if (!showTrigger)
486 : {
487 6 : if (have_where)
488 6 : appendPQExpBufferStr(&buf, " AND ");
489 : else
490 : {
491 0 : appendPQExpBufferStr(&buf, "WHERE ");
492 0 : have_where = true;
493 : }
494 6 : appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
495 : }
496 6 : if (!showWindow)
497 : {
498 6 : if (have_where)
499 6 : appendPQExpBufferStr(&buf, " AND ");
500 : else
501 : {
502 0 : appendPQExpBufferStr(&buf, "WHERE ");
503 0 : have_where = true;
504 : }
505 6 : if (pset.sversion >= 110000)
506 6 : appendPQExpBufferStr(&buf, "p.prokind <> "
507 : CppAsString2(PROKIND_WINDOW) "\n");
508 : else
509 0 : appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
510 : }
511 : }
512 : else
513 : {
514 12 : bool needs_or = false;
515 :
516 12 : appendPQExpBufferStr(&buf, "WHERE (\n ");
517 12 : have_where = true;
518 : /* Note: at least one of these must be true ... */
519 12 : if (showAggregate)
520 : {
521 6 : if (pset.sversion >= 110000)
522 6 : appendPQExpBufferStr(&buf, "p.prokind = "
523 : CppAsString2(PROKIND_AGGREGATE) "\n");
524 : else
525 0 : appendPQExpBufferStr(&buf, "p.proisagg\n");
526 6 : needs_or = true;
527 : }
528 12 : if (showTrigger)
529 : {
530 0 : if (needs_or)
531 0 : appendPQExpBufferStr(&buf, " OR ");
532 0 : appendPQExpBufferStr(&buf,
533 : "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
534 0 : needs_or = true;
535 : }
536 12 : if (showProcedure)
537 : {
538 6 : if (needs_or)
539 0 : appendPQExpBufferStr(&buf, " OR ");
540 6 : appendPQExpBufferStr(&buf, "p.prokind = "
541 : CppAsString2(PROKIND_PROCEDURE) "\n");
542 6 : needs_or = true;
543 : }
544 12 : if (showWindow)
545 : {
546 0 : if (needs_or)
547 0 : appendPQExpBufferStr(&buf, " OR ");
548 0 : if (pset.sversion >= 110000)
549 0 : appendPQExpBufferStr(&buf, "p.prokind = "
550 : CppAsString2(PROKIND_WINDOW) "\n");
551 : else
552 0 : appendPQExpBufferStr(&buf, "p.proiswindow\n");
553 : }
554 12 : appendPQExpBufferStr(&buf, " )\n");
555 : }
556 :
557 298 : if (!validateSQLNamePattern(&buf, func_pattern, have_where, false,
558 : "n.nspname", "p.proname", NULL,
559 : "pg_catalog.pg_function_is_visible(p.oid)",
560 : NULL, 3))
561 24 : goto error_return;
562 :
563 340 : for (int i = 0; i < num_arg_patterns; i++)
564 : {
565 66 : if (strcmp(arg_patterns[i], "-") != 0)
566 : {
567 : /*
568 : * Match type-name patterns against either internal or external
569 : * name, like \dT. Unlike \dT, there seems no reason to
570 : * discriminate against arrays or composite types.
571 : */
572 : char nspname[64];
573 : char typname[64];
574 : char ft[64];
575 : char tiv[64];
576 :
577 60 : snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
578 60 : snprintf(typname, sizeof(typname), "t%d.typname", i);
579 60 : snprintf(ft, sizeof(ft),
580 : "pg_catalog.format_type(t%d.oid, NULL)", i);
581 60 : snprintf(tiv, sizeof(tiv),
582 : "pg_catalog.pg_type_is_visible(t%d.oid)", i);
583 60 : if (!validateSQLNamePattern(&buf,
584 60 : map_typename_pattern(arg_patterns[i]),
585 : true, false,
586 : nspname, typname, ft, tiv,
587 : NULL, 3))
588 0 : goto error_return;
589 : }
590 : else
591 : {
592 : /* "-" pattern specifies no such parameter */
593 6 : appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
594 : }
595 : }
596 :
597 274 : if (!showSystem && !func_pattern)
598 2 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
599 : " AND n.nspname <> 'information_schema'\n");
600 :
601 274 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
602 :
603 274 : res = PSQLexec(buf.data);
604 274 : termPQExpBuffer(&buf);
605 274 : if (!res)
606 0 : return false;
607 :
608 274 : myopt.title = _("List of functions");
609 274 : myopt.translate_header = true;
610 274 : if (pset.sversion >= 90600)
611 : {
612 274 : myopt.translate_columns = translate_columns;
613 274 : myopt.n_translate_columns = lengthof(translate_columns);
614 : }
615 : else
616 : {
617 0 : myopt.translate_columns = translate_columns_pre_96;
618 0 : myopt.n_translate_columns = lengthof(translate_columns_pre_96);
619 : }
620 :
621 274 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
622 :
623 274 : PQclear(res);
624 274 : return true;
625 :
626 24 : error_return:
627 24 : termPQExpBuffer(&buf);
628 24 : return false;
629 : }
630 :
631 :
632 :
633 : /*
634 : * \dT
635 : * describe types
636 : */
637 : bool
638 68 : describeTypes(const char *pattern, bool verbose, bool showSystem)
639 : {
640 : PQExpBufferData buf;
641 : PGresult *res;
642 68 : printQueryOpt myopt = pset.popt;
643 :
644 68 : initPQExpBuffer(&buf);
645 :
646 68 : printfPQExpBuffer(&buf,
647 : "SELECT n.nspname as \"%s\",\n"
648 : " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
649 : gettext_noop("Schema"),
650 : gettext_noop("Name"));
651 68 : if (verbose)
652 : {
653 12 : appendPQExpBuffer(&buf,
654 : " t.typname AS \"%s\",\n"
655 : " CASE WHEN t.typrelid != 0\n"
656 : " THEN CAST('tuple' AS pg_catalog.text)\n"
657 : " WHEN t.typlen < 0\n"
658 : " THEN CAST('var' AS pg_catalog.text)\n"
659 : " ELSE CAST(t.typlen AS pg_catalog.text)\n"
660 : " END AS \"%s\",\n"
661 : " pg_catalog.array_to_string(\n"
662 : " ARRAY(\n"
663 : " SELECT e.enumlabel\n"
664 : " FROM pg_catalog.pg_enum e\n"
665 : " WHERE e.enumtypid = t.oid\n"
666 : " ORDER BY e.enumsortorder\n"
667 : " ),\n"
668 : " E'\\n'\n"
669 : " ) AS \"%s\",\n"
670 : " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
671 : gettext_noop("Internal name"),
672 : gettext_noop("Size"),
673 : gettext_noop("Elements"),
674 : gettext_noop("Owner"));
675 12 : printACLColumn(&buf, "t.typacl");
676 12 : appendPQExpBufferStr(&buf, ",\n ");
677 : }
678 :
679 68 : appendPQExpBuffer(&buf,
680 : " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
681 : gettext_noop("Description"));
682 :
683 68 : appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
684 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
685 :
686 : /*
687 : * do not include complex types (typrelid!=0) unless they are standalone
688 : * composite types
689 : */
690 68 : appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
691 68 : appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
692 : " FROM pg_catalog.pg_class c "
693 : "WHERE c.oid = t.typrelid))\n");
694 :
695 : /*
696 : * do not include array types unless the pattern contains []
697 : */
698 68 : if (pattern == NULL || strstr(pattern, "[]") == NULL)
699 68 : appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
700 :
701 68 : if (!showSystem && !pattern)
702 6 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
703 : " AND n.nspname <> 'information_schema'\n");
704 :
705 : /* Match name pattern against either internal or external name */
706 68 : if (!validateSQLNamePattern(&buf, map_typename_pattern(pattern),
707 : true, false,
708 : "n.nspname", "t.typname",
709 : "pg_catalog.format_type(t.oid, NULL)",
710 : "pg_catalog.pg_type_is_visible(t.oid)",
711 : NULL, 3))
712 : {
713 24 : termPQExpBuffer(&buf);
714 24 : return false;
715 : }
716 :
717 44 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
718 :
719 44 : res = PSQLexec(buf.data);
720 44 : termPQExpBuffer(&buf);
721 44 : if (!res)
722 0 : return false;
723 :
724 44 : myopt.title = _("List of data types");
725 44 : myopt.translate_header = true;
726 :
727 44 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
728 :
729 44 : PQclear(res);
730 44 : return true;
731 : }
732 :
733 : /*
734 : * Map some variant type names accepted by the backend grammar into
735 : * canonical type names.
736 : *
737 : * Helper for \dT and other functions that take typename patterns.
738 : * This doesn't completely mask the fact that these names are special;
739 : * for example, a pattern of "dec*" won't magically match "numeric".
740 : * But it goes a long way to reduce the surprise factor.
741 : */
742 : static const char *
743 146 : map_typename_pattern(const char *pattern)
744 : {
745 : static const char *const typename_map[] = {
746 : /*
747 : * These names are accepted by gram.y, although they are neither the
748 : * "real" name seen in pg_type nor the canonical name printed by
749 : * format_type().
750 : */
751 : "decimal", "numeric",
752 : "float", "double precision",
753 : "int", "integer",
754 :
755 : /*
756 : * We also have to map the array names for cases where the canonical
757 : * name is different from what pg_type says.
758 : */
759 : "bool[]", "boolean[]",
760 : "decimal[]", "numeric[]",
761 : "float[]", "double precision[]",
762 : "float4[]", "real[]",
763 : "float8[]", "double precision[]",
764 : "int[]", "integer[]",
765 : "int2[]", "smallint[]",
766 : "int4[]", "integer[]",
767 : "int8[]", "bigint[]",
768 : "time[]", "time without time zone[]",
769 : "timetz[]", "time with time zone[]",
770 : "timestamp[]", "timestamp without time zone[]",
771 : "timestamptz[]", "timestamp with time zone[]",
772 : "varbit[]", "bit varying[]",
773 : "varchar[]", "character varying[]",
774 : NULL
775 : };
776 :
777 146 : if (pattern == NULL)
778 6 : return NULL;
779 2660 : for (int i = 0; typename_map[i] != NULL; i += 2)
780 : {
781 2520 : if (pg_strcasecmp(pattern, typename_map[i]) == 0)
782 0 : return typename_map[i + 1];
783 : }
784 140 : return pattern;
785 : }
786 :
787 :
788 : /*
789 : * \do
790 : * Describe operators
791 : */
792 : bool
793 62 : describeOperators(const char *oper_pattern,
794 : char **arg_patterns, int num_arg_patterns,
795 : bool verbose, bool showSystem)
796 : {
797 : PQExpBufferData buf;
798 : PGresult *res;
799 62 : printQueryOpt myopt = pset.popt;
800 : static const bool translate_columns[] = {false, false, false, false, false, false, true, false};
801 :
802 62 : initPQExpBuffer(&buf);
803 :
804 : /*
805 : * Note: before Postgres 9.1, we did not assign comments to any built-in
806 : * operators, preferring to let the comment on the underlying function
807 : * suffice. The coalesce() on the obj_description() calls below supports
808 : * this convention by providing a fallback lookup of a comment on the
809 : * operator's function. Since 9.1 there is a policy that every built-in
810 : * operator should have a comment; so the coalesce() is no longer
811 : * necessary so far as built-in operators are concerned. We keep it
812 : * anyway, for now, because third-party modules may still be following the
813 : * old convention.
814 : *
815 : * The support for postfix operators in this query is dead code as of
816 : * Postgres 14, but we need to keep it for as long as we support talking
817 : * to pre-v14 servers.
818 : */
819 :
820 62 : printfPQExpBuffer(&buf,
821 : "SELECT n.nspname as \"%s\",\n"
822 : " o.oprname AS \"%s\",\n"
823 : " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
824 : " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
825 : " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
826 : gettext_noop("Schema"),
827 : gettext_noop("Name"),
828 : gettext_noop("Left arg type"),
829 : gettext_noop("Right arg type"),
830 : gettext_noop("Result type"));
831 :
832 62 : if (verbose)
833 0 : appendPQExpBuffer(&buf,
834 : " o.oprcode AS \"%s\",\n"
835 : " CASE WHEN p.proleakproof THEN '%s' ELSE '%s' END AS \"%s\",\n",
836 : gettext_noop("Function"),
837 : gettext_noop("yes"),
838 : gettext_noop("no"),
839 : gettext_noop("Leakproof?"));
840 :
841 62 : appendPQExpBuffer(&buf,
842 : " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
843 : " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
844 : "FROM pg_catalog.pg_operator o\n"
845 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
846 : gettext_noop("Description"));
847 :
848 62 : if (num_arg_patterns >= 2)
849 : {
850 6 : num_arg_patterns = 2; /* ignore any additional arguments */
851 6 : appendPQExpBufferStr(&buf,
852 : " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprleft\n"
853 : " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n"
854 : " LEFT JOIN pg_catalog.pg_type t1 ON t1.oid = o.oprright\n"
855 : " LEFT JOIN pg_catalog.pg_namespace nt1 ON nt1.oid = t1.typnamespace\n");
856 : }
857 56 : else if (num_arg_patterns == 1)
858 : {
859 6 : appendPQExpBufferStr(&buf,
860 : " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprright\n"
861 : " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n");
862 : }
863 :
864 62 : if (verbose)
865 0 : appendPQExpBufferStr(&buf,
866 : " LEFT JOIN pg_catalog.pg_proc p ON p.oid = o.oprcode\n");
867 :
868 62 : if (!showSystem && !oper_pattern)
869 2 : appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
870 : " AND n.nspname <> 'information_schema'\n");
871 :
872 62 : if (!validateSQLNamePattern(&buf, oper_pattern,
873 62 : !showSystem && !oper_pattern, true,
874 : "n.nspname", "o.oprname", NULL,
875 : "pg_catalog.pg_operator_is_visible(o.oid)",
876 : NULL, 3))
877 24 : goto error_return;
878 :
879 38 : if (num_arg_patterns == 1)
880 6 : appendPQExpBufferStr(&buf, " AND o.oprleft = 0\n");
881 :
882 56 : for (int i = 0; i < num_arg_patterns; i++)
883 : {
884 18 : if (strcmp(arg_patterns[i], "-") != 0)
885 : {
886 : /*
887 : * Match type-name patterns against either internal or external
888 : * name, like \dT. Unlike \dT, there seems no reason to
889 : * discriminate against arrays or composite types.
890 : */
891 : char nspname[64];
892 : char typname[64];
893 : char ft[64];
894 : char tiv[64];
895 :
896 18 : snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
897 18 : snprintf(typname, sizeof(typname), "t%d.typname", i);
898 18 : snprintf(ft, sizeof(ft),
899 : "pg_catalog.format_type(t%d.oid, NULL)", i);
900 18 : snprintf(tiv, sizeof(tiv),
901 : "pg_catalog.pg_type_is_visible(t%d.oid)", i);
902 18 : if (!validateSQLNamePattern(&buf,
903 18 : map_typename_pattern(arg_patterns[i]),
904 : true, false,
905 : nspname, typname, ft, tiv,
906 : NULL, 3))
907 0 : goto error_return;
908 : }
909 : else
910 : {
911 : /* "-" pattern specifies no such parameter */
912 0 : appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
913 : }
914 : }
915 :
916 38 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
917 :
918 38 : res = PSQLexec(buf.data);
919 38 : termPQExpBuffer(&buf);
920 38 : if (!res)
921 0 : return false;
922 :
923 38 : myopt.title = _("List of operators");
924 38 : myopt.translate_header = true;
925 38 : myopt.translate_columns = translate_columns;
926 38 : myopt.n_translate_columns = lengthof(translate_columns);
927 :
928 38 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
929 :
930 38 : PQclear(res);
931 38 : return true;
932 :
933 24 : error_return:
934 24 : termPQExpBuffer(&buf);
935 24 : return false;
936 : }
937 :
938 :
939 : /*
940 : * listAllDbs
941 : *
942 : * for \l, \list, and -l switch
943 : */
944 : bool
945 0 : listAllDbs(const char *pattern, bool verbose)
946 : {
947 : PGresult *res;
948 : PQExpBufferData buf;
949 0 : printQueryOpt myopt = pset.popt;
950 :
951 0 : initPQExpBuffer(&buf);
952 :
953 0 : printfPQExpBuffer(&buf,
954 : "SELECT\n"
955 : " d.datname as \"%s\",\n"
956 : " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
957 : " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
958 : gettext_noop("Name"),
959 : gettext_noop("Owner"),
960 : gettext_noop("Encoding"));
961 0 : if (pset.sversion >= 150000)
962 0 : appendPQExpBuffer(&buf,
963 : " CASE d.datlocprovider "
964 : "WHEN " CppAsString2(COLLPROVIDER_BUILTIN) " THEN 'builtin' "
965 : "WHEN " CppAsString2(COLLPROVIDER_LIBC) " THEN 'libc' "
966 : "WHEN " CppAsString2(COLLPROVIDER_ICU) " THEN 'icu' "
967 : "END AS \"%s\",\n",
968 : gettext_noop("Locale Provider"));
969 : else
970 0 : appendPQExpBuffer(&buf,
971 : " 'libc' AS \"%s\",\n",
972 : gettext_noop("Locale Provider"));
973 0 : appendPQExpBuffer(&buf,
974 : " d.datcollate as \"%s\",\n"
975 : " d.datctype as \"%s\",\n",
976 : gettext_noop("Collate"),
977 : gettext_noop("Ctype"));
978 0 : if (pset.sversion >= 170000)
979 0 : appendPQExpBuffer(&buf,
980 : " d.datlocale as \"%s\",\n",
981 : gettext_noop("Locale"));
982 0 : else if (pset.sversion >= 150000)
983 0 : appendPQExpBuffer(&buf,
984 : " d.daticulocale as \"%s\",\n",
985 : gettext_noop("Locale"));
986 : else
987 0 : appendPQExpBuffer(&buf,
988 : " NULL as \"%s\",\n",
989 : gettext_noop("Locale"));
990 0 : if (pset.sversion >= 160000)
991 0 : appendPQExpBuffer(&buf,
992 : " d.daticurules as \"%s\",\n",
993 : gettext_noop("ICU Rules"));
994 : else
995 0 : appendPQExpBuffer(&buf,
996 : " NULL as \"%s\",\n",
997 : gettext_noop("ICU Rules"));
998 0 : appendPQExpBufferStr(&buf, " ");
999 0 : printACLColumn(&buf, "d.datacl");
1000 0 : if (verbose)
1001 0 : appendPQExpBuffer(&buf,
1002 : ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
1003 : " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
1004 : " ELSE 'No Access'\n"
1005 : " END as \"%s\""
1006 : ",\n t.spcname as \"%s\""
1007 : ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
1008 : gettext_noop("Size"),
1009 : gettext_noop("Tablespace"),
1010 : gettext_noop("Description"));
1011 0 : appendPQExpBufferStr(&buf,
1012 : "\nFROM pg_catalog.pg_database d\n");
1013 0 : if (verbose)
1014 0 : appendPQExpBufferStr(&buf,
1015 : " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
1016 :
1017 0 : if (pattern)
1018 : {
1019 0 : if (!validateSQLNamePattern(&buf, pattern, false, false,
1020 : NULL, "d.datname", NULL, NULL,
1021 : NULL, 1))
1022 : {
1023 0 : termPQExpBuffer(&buf);
1024 0 : return false;
1025 : }
1026 : }
1027 :
1028 0 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
1029 0 : res = PSQLexec(buf.data);
1030 0 : termPQExpBuffer(&buf);
1031 0 : if (!res)
1032 0 : return false;
1033 :
1034 0 : myopt.title = _("List of databases");
1035 0 : myopt.translate_header = true;
1036 :
1037 0 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1038 :
1039 0 : PQclear(res);
1040 0 : return true;
1041 : }
1042 :
1043 :
1044 : /*
1045 : * List Tables' Grant/Revoke Permissions
1046 : * \z (now also \dp -- perhaps more mnemonic)
1047 : */
1048 : bool
1049 96 : permissionsList(const char *pattern, bool showSystem)
1050 : {
1051 : PQExpBufferData buf;
1052 : PGresult *res;
1053 96 : printQueryOpt myopt = pset.popt;
1054 : static const bool translate_columns[] = {false, false, true, false, false, false};
1055 :
1056 96 : initPQExpBuffer(&buf);
1057 :
1058 : /*
1059 : * we ignore indexes and toast tables since they have no meaningful rights
1060 : */
1061 96 : printfPQExpBuffer(&buf,
1062 : "SELECT n.nspname as \"%s\",\n"
1063 : " c.relname as \"%s\",\n"
1064 : " CASE c.relkind"
1065 : " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
1066 : " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
1067 : " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
1068 : " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
1069 : " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
1070 : " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
1071 : " END as \"%s\",\n"
1072 : " ",
1073 : gettext_noop("Schema"),
1074 : gettext_noop("Name"),
1075 : gettext_noop("table"),
1076 : gettext_noop("view"),
1077 : gettext_noop("materialized view"),
1078 : gettext_noop("sequence"),
1079 : gettext_noop("foreign table"),
1080 : gettext_noop("partitioned table"),
1081 : gettext_noop("Type"));
1082 :
1083 96 : printACLColumn(&buf, "c.relacl");
1084 :
1085 : /*
1086 : * The formatting of attacl should match printACLColumn(). However, we
1087 : * need no special case for an empty attacl, because the backend always
1088 : * optimizes that back to NULL.
1089 : */
1090 96 : appendPQExpBuffer(&buf,
1091 : ",\n pg_catalog.array_to_string(ARRAY(\n"
1092 : " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
1093 : " FROM pg_catalog.pg_attribute a\n"
1094 : " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
1095 : " ), E'\\n') AS \"%s\"",
1096 : gettext_noop("Column privileges"));
1097 :
1098 96 : if (pset.sversion >= 90500 && pset.sversion < 100000)
1099 0 : appendPQExpBuffer(&buf,
1100 : ",\n pg_catalog.array_to_string(ARRAY(\n"
1101 : " SELECT polname\n"
1102 : " || CASE WHEN polcmd != '*' THEN\n"
1103 : " E' (' || polcmd::pg_catalog.text || E'):'\n"
1104 : " ELSE E':'\n"
1105 : " END\n"
1106 : " || CASE WHEN polqual IS NOT NULL THEN\n"
1107 : " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1108 : " ELSE E''\n"
1109 : " END\n"
1110 : " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1111 : " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1112 : " ELSE E''\n"
1113 : " END"
1114 : " || CASE WHEN polroles <> '{0}' THEN\n"
1115 : " E'\\n to: ' || pg_catalog.array_to_string(\n"
1116 : " ARRAY(\n"
1117 : " SELECT rolname\n"
1118 : " FROM pg_catalog.pg_roles\n"
1119 : " WHERE oid = ANY (polroles)\n"
1120 : " ORDER BY 1\n"
1121 : " ), E', ')\n"
1122 : " ELSE E''\n"
1123 : " END\n"
1124 : " FROM pg_catalog.pg_policy pol\n"
1125 : " WHERE polrelid = c.oid), E'\\n')\n"
1126 : " AS \"%s\"",
1127 : gettext_noop("Policies"));
1128 :
1129 96 : if (pset.sversion >= 100000)
1130 96 : appendPQExpBuffer(&buf,
1131 : ",\n pg_catalog.array_to_string(ARRAY(\n"
1132 : " SELECT polname\n"
1133 : " || CASE WHEN NOT polpermissive THEN\n"
1134 : " E' (RESTRICTIVE)'\n"
1135 : " ELSE '' END\n"
1136 : " || CASE WHEN polcmd != '*' THEN\n"
1137 : " E' (' || polcmd::pg_catalog.text || E'):'\n"
1138 : " ELSE E':'\n"
1139 : " END\n"
1140 : " || CASE WHEN polqual IS NOT NULL THEN\n"
1141 : " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1142 : " ELSE E''\n"
1143 : " END\n"
1144 : " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1145 : " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1146 : " ELSE E''\n"
1147 : " END"
1148 : " || CASE WHEN polroles <> '{0}' THEN\n"
1149 : " E'\\n to: ' || pg_catalog.array_to_string(\n"
1150 : " ARRAY(\n"
1151 : " SELECT rolname\n"
1152 : " FROM pg_catalog.pg_roles\n"
1153 : " WHERE oid = ANY (polroles)\n"
1154 : " ORDER BY 1\n"
1155 : " ), E', ')\n"
1156 : " ELSE E''\n"
1157 : " END\n"
1158 : " FROM pg_catalog.pg_policy pol\n"
1159 : " WHERE polrelid = c.oid), E'\\n')\n"
1160 : " AS \"%s\"",
1161 : gettext_noop("Policies"));
1162 :
1163 96 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
1164 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1165 : "WHERE c.relkind IN ("
1166 : CppAsString2(RELKIND_RELATION) ","
1167 : CppAsString2(RELKIND_VIEW) ","
1168 : CppAsString2(RELKIND_MATVIEW) ","
1169 : CppAsString2(RELKIND_SEQUENCE) ","
1170 : CppAsString2(RELKIND_FOREIGN_TABLE) ","
1171 : CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
1172 :
1173 96 : if (!showSystem && !pattern)
1174 6 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1175 : " AND n.nspname <> 'information_schema'\n");
1176 :
1177 96 : if (!validateSQLNamePattern(&buf, pattern, true, false,
1178 : "n.nspname", "c.relname", NULL,
1179 : "pg_catalog.pg_table_is_visible(c.oid)",
1180 : NULL, 3))
1181 24 : goto error_return;
1182 :
1183 72 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
1184 :
1185 72 : res = PSQLexec(buf.data);
1186 72 : if (!res)
1187 0 : goto error_return;
1188 :
1189 72 : printfPQExpBuffer(&buf, _("Access privileges"));
1190 72 : myopt.title = buf.data;
1191 72 : myopt.translate_header = true;
1192 72 : myopt.translate_columns = translate_columns;
1193 72 : myopt.n_translate_columns = lengthof(translate_columns);
1194 :
1195 72 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1196 :
1197 72 : termPQExpBuffer(&buf);
1198 72 : PQclear(res);
1199 72 : return true;
1200 :
1201 24 : error_return:
1202 24 : termPQExpBuffer(&buf);
1203 24 : return false;
1204 : }
1205 :
1206 :
1207 : /*
1208 : * \ddp
1209 : *
1210 : * List Default ACLs. The pattern can match either schema or role name.
1211 : */
1212 : bool
1213 36 : listDefaultACLs(const char *pattern)
1214 : {
1215 : PQExpBufferData buf;
1216 : PGresult *res;
1217 36 : printQueryOpt myopt = pset.popt;
1218 : static const bool translate_columns[] = {false, false, true, false};
1219 :
1220 36 : initPQExpBuffer(&buf);
1221 :
1222 36 : printfPQExpBuffer(&buf,
1223 : "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1224 : " n.nspname AS \"%s\",\n"
1225 : " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
1226 : " ",
1227 : gettext_noop("Owner"),
1228 : gettext_noop("Schema"),
1229 : DEFACLOBJ_RELATION,
1230 : gettext_noop("table"),
1231 : DEFACLOBJ_SEQUENCE,
1232 : gettext_noop("sequence"),
1233 : DEFACLOBJ_FUNCTION,
1234 : gettext_noop("function"),
1235 : DEFACLOBJ_TYPE,
1236 : gettext_noop("type"),
1237 : DEFACLOBJ_NAMESPACE,
1238 : gettext_noop("schema"),
1239 : gettext_noop("Type"));
1240 :
1241 36 : printACLColumn(&buf, "d.defaclacl");
1242 :
1243 36 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1244 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1245 :
1246 36 : if (!validateSQLNamePattern(&buf, pattern, false, false,
1247 : NULL,
1248 : "n.nspname",
1249 : "pg_catalog.pg_get_userbyid(d.defaclrole)",
1250 : NULL,
1251 : NULL, 3))
1252 24 : goto error_return;
1253 :
1254 12 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1255 :
1256 12 : res = PSQLexec(buf.data);
1257 12 : if (!res)
1258 0 : goto error_return;
1259 :
1260 12 : printfPQExpBuffer(&buf, _("Default access privileges"));
1261 12 : myopt.title = buf.data;
1262 12 : myopt.translate_header = true;
1263 12 : myopt.translate_columns = translate_columns;
1264 12 : myopt.n_translate_columns = lengthof(translate_columns);
1265 :
1266 12 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1267 :
1268 12 : termPQExpBuffer(&buf);
1269 12 : PQclear(res);
1270 12 : return true;
1271 :
1272 24 : error_return:
1273 24 : termPQExpBuffer(&buf);
1274 24 : return false;
1275 : }
1276 :
1277 :
1278 : /*
1279 : * Get object comments
1280 : *
1281 : * \dd [foo]
1282 : *
1283 : * Note: This command only lists comments for object types which do not have
1284 : * their comments displayed by their own backslash commands. The following
1285 : * types of objects will be displayed: constraint, operator class,
1286 : * operator family, rule, and trigger.
1287 : *
1288 : */
1289 : bool
1290 42 : objectDescription(const char *pattern, bool showSystem)
1291 : {
1292 : PQExpBufferData buf;
1293 : PGresult *res;
1294 42 : printQueryOpt myopt = pset.popt;
1295 : static const bool translate_columns[] = {false, false, true, false};
1296 :
1297 42 : initPQExpBuffer(&buf);
1298 :
1299 42 : appendPQExpBuffer(&buf,
1300 : "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
1301 : "FROM (\n",
1302 : gettext_noop("Schema"),
1303 : gettext_noop("Name"),
1304 : gettext_noop("Object"),
1305 : gettext_noop("Description"));
1306 :
1307 : /* Table constraint descriptions */
1308 42 : appendPQExpBuffer(&buf,
1309 : " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1310 : " n.nspname as nspname,\n"
1311 : " CAST(pgc.conname AS pg_catalog.text) as name,"
1312 : " CAST('%s' AS pg_catalog.text) as object\n"
1313 : " FROM pg_catalog.pg_constraint pgc\n"
1314 : " JOIN pg_catalog.pg_class c "
1315 : "ON c.oid = pgc.conrelid\n"
1316 : " LEFT JOIN pg_catalog.pg_namespace n "
1317 : " ON n.oid = c.relnamespace\n",
1318 : gettext_noop("table constraint"));
1319 :
1320 42 : if (!showSystem && !pattern)
1321 0 : appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1322 : " AND n.nspname <> 'information_schema'\n");
1323 :
1324 42 : if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1325 : false, "n.nspname", "pgc.conname", NULL,
1326 : "pg_catalog.pg_table_is_visible(c.oid)",
1327 : NULL, 3))
1328 24 : goto error_return;
1329 :
1330 : /* Domain constraint descriptions */
1331 18 : appendPQExpBuffer(&buf,
1332 : "UNION ALL\n"
1333 : " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1334 : " n.nspname as nspname,\n"
1335 : " CAST(pgc.conname AS pg_catalog.text) as name,"
1336 : " CAST('%s' AS pg_catalog.text) as object\n"
1337 : " FROM pg_catalog.pg_constraint pgc\n"
1338 : " JOIN pg_catalog.pg_type t "
1339 : "ON t.oid = pgc.contypid\n"
1340 : " LEFT JOIN pg_catalog.pg_namespace n "
1341 : " ON n.oid = t.typnamespace\n",
1342 : gettext_noop("domain constraint"));
1343 :
1344 18 : if (!showSystem && !pattern)
1345 0 : appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1346 : " AND n.nspname <> 'information_schema'\n");
1347 :
1348 18 : if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1349 : false, "n.nspname", "pgc.conname", NULL,
1350 : "pg_catalog.pg_type_is_visible(t.oid)",
1351 : NULL, 3))
1352 0 : goto error_return;
1353 :
1354 : /* Operator class descriptions */
1355 18 : appendPQExpBuffer(&buf,
1356 : "UNION ALL\n"
1357 : " SELECT o.oid as oid, o.tableoid as tableoid,\n"
1358 : " n.nspname as nspname,\n"
1359 : " CAST(o.opcname AS pg_catalog.text) as name,\n"
1360 : " CAST('%s' AS pg_catalog.text) as object\n"
1361 : " FROM pg_catalog.pg_opclass o\n"
1362 : " JOIN pg_catalog.pg_am am ON "
1363 : "o.opcmethod = am.oid\n"
1364 : " JOIN pg_catalog.pg_namespace n ON "
1365 : "n.oid = o.opcnamespace\n",
1366 : gettext_noop("operator class"));
1367 :
1368 18 : if (!showSystem && !pattern)
1369 0 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1370 : " AND n.nspname <> 'information_schema'\n");
1371 :
1372 18 : if (!validateSQLNamePattern(&buf, pattern, true, false,
1373 : "n.nspname", "o.opcname", NULL,
1374 : "pg_catalog.pg_opclass_is_visible(o.oid)",
1375 : NULL, 3))
1376 0 : goto error_return;
1377 :
1378 : /* Operator family descriptions */
1379 18 : appendPQExpBuffer(&buf,
1380 : "UNION ALL\n"
1381 : " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1382 : " n.nspname as nspname,\n"
1383 : " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1384 : " CAST('%s' AS pg_catalog.text) as object\n"
1385 : " FROM pg_catalog.pg_opfamily opf\n"
1386 : " JOIN pg_catalog.pg_am am "
1387 : "ON opf.opfmethod = am.oid\n"
1388 : " JOIN pg_catalog.pg_namespace n "
1389 : "ON opf.opfnamespace = n.oid\n",
1390 : gettext_noop("operator family"));
1391 :
1392 18 : if (!showSystem && !pattern)
1393 0 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1394 : " AND n.nspname <> 'information_schema'\n");
1395 :
1396 18 : if (!validateSQLNamePattern(&buf, pattern, true, false,
1397 : "n.nspname", "opf.opfname", NULL,
1398 : "pg_catalog.pg_opfamily_is_visible(opf.oid)",
1399 : NULL, 3))
1400 0 : goto error_return;
1401 :
1402 : /* Rule descriptions (ignore rules for views) */
1403 18 : appendPQExpBuffer(&buf,
1404 : "UNION ALL\n"
1405 : " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1406 : " n.nspname as nspname,\n"
1407 : " CAST(r.rulename AS pg_catalog.text) as name,"
1408 : " CAST('%s' AS pg_catalog.text) as object\n"
1409 : " FROM pg_catalog.pg_rewrite r\n"
1410 : " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1411 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1412 : " WHERE r.rulename != '_RETURN'\n",
1413 : gettext_noop("rule"));
1414 :
1415 18 : if (!showSystem && !pattern)
1416 0 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1417 : " AND n.nspname <> 'information_schema'\n");
1418 :
1419 18 : if (!validateSQLNamePattern(&buf, pattern, true, false,
1420 : "n.nspname", "r.rulename", NULL,
1421 : "pg_catalog.pg_table_is_visible(c.oid)",
1422 : NULL, 3))
1423 0 : goto error_return;
1424 :
1425 : /* Trigger descriptions */
1426 18 : appendPQExpBuffer(&buf,
1427 : "UNION ALL\n"
1428 : " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1429 : " n.nspname as nspname,\n"
1430 : " CAST(t.tgname AS pg_catalog.text) as name,"
1431 : " CAST('%s' AS pg_catalog.text) as object\n"
1432 : " FROM pg_catalog.pg_trigger t\n"
1433 : " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1434 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1435 : gettext_noop("trigger"));
1436 :
1437 18 : if (!showSystem && !pattern)
1438 0 : appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1439 : " AND n.nspname <> 'information_schema'\n");
1440 :
1441 18 : if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1442 : "n.nspname", "t.tgname", NULL,
1443 : "pg_catalog.pg_table_is_visible(c.oid)",
1444 : NULL, 3))
1445 0 : goto error_return;
1446 :
1447 18 : appendPQExpBufferStr(&buf,
1448 : ") AS tt\n"
1449 : " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1450 :
1451 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1452 :
1453 18 : res = PSQLexec(buf.data);
1454 18 : termPQExpBuffer(&buf);
1455 18 : if (!res)
1456 0 : return false;
1457 :
1458 18 : myopt.title = _("Object descriptions");
1459 18 : myopt.translate_header = true;
1460 18 : myopt.translate_columns = translate_columns;
1461 18 : myopt.n_translate_columns = lengthof(translate_columns);
1462 :
1463 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1464 :
1465 18 : PQclear(res);
1466 18 : return true;
1467 :
1468 24 : error_return:
1469 24 : termPQExpBuffer(&buf);
1470 24 : return false;
1471 : }
1472 :
1473 :
1474 : /*
1475 : * describeTableDetails (for \d)
1476 : *
1477 : * This routine finds the tables to be displayed, and calls
1478 : * describeOneTableDetails for each one.
1479 : *
1480 : * verbose: if true, this is \d+
1481 : */
1482 : bool
1483 3926 : describeTableDetails(const char *pattern, bool verbose, bool showSystem)
1484 : {
1485 : PQExpBufferData buf;
1486 : PGresult *res;
1487 : int i;
1488 :
1489 3926 : initPQExpBuffer(&buf);
1490 :
1491 3926 : printfPQExpBuffer(&buf,
1492 : "SELECT c.oid,\n"
1493 : " n.nspname,\n"
1494 : " c.relname\n"
1495 : "FROM pg_catalog.pg_class c\n"
1496 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1497 :
1498 3926 : if (!showSystem && !pattern)
1499 0 : appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1500 : " AND n.nspname <> 'information_schema'\n");
1501 :
1502 3926 : if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1503 : "n.nspname", "c.relname", NULL,
1504 : "pg_catalog.pg_table_is_visible(c.oid)",
1505 : NULL, 3))
1506 : {
1507 0 : termPQExpBuffer(&buf);
1508 0 : return false;
1509 : }
1510 :
1511 3926 : appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1512 :
1513 3926 : res = PSQLexec(buf.data);
1514 3926 : termPQExpBuffer(&buf);
1515 3926 : if (!res)
1516 0 : return false;
1517 :
1518 3926 : if (PQntuples(res) == 0)
1519 : {
1520 26 : if (!pset.quiet)
1521 : {
1522 0 : if (pattern)
1523 0 : pg_log_error("Did not find any relation named \"%s\".",
1524 : pattern);
1525 : else
1526 0 : pg_log_error("Did not find any relations.");
1527 : }
1528 26 : PQclear(res);
1529 26 : return false;
1530 : }
1531 :
1532 7908 : for (i = 0; i < PQntuples(res); i++)
1533 : {
1534 : const char *oid;
1535 : const char *nspname;
1536 : const char *relname;
1537 :
1538 4008 : oid = PQgetvalue(res, i, 0);
1539 4008 : nspname = PQgetvalue(res, i, 1);
1540 4008 : relname = PQgetvalue(res, i, 2);
1541 :
1542 4008 : if (!describeOneTableDetails(nspname, relname, oid, verbose))
1543 : {
1544 0 : PQclear(res);
1545 0 : return false;
1546 : }
1547 4008 : if (cancel_pressed)
1548 : {
1549 0 : PQclear(res);
1550 0 : return false;
1551 : }
1552 : }
1553 :
1554 3900 : PQclear(res);
1555 3900 : return true;
1556 : }
1557 :
1558 : /*
1559 : * describeOneTableDetails (for \d)
1560 : *
1561 : * Unfortunately, the information presented here is so complicated that it
1562 : * cannot be done in a single query. So we have to assemble the printed table
1563 : * by hand and pass it to the underlying printTable() function.
1564 : */
1565 : static bool
1566 4008 : describeOneTableDetails(const char *schemaname,
1567 : const char *relationname,
1568 : const char *oid,
1569 : bool verbose)
1570 : {
1571 4008 : bool retval = false;
1572 : PQExpBufferData buf;
1573 4008 : PGresult *res = NULL;
1574 4008 : printTableOpt myopt = pset.popt.topt;
1575 : printTableContent cont;
1576 4008 : bool printTableInitialized = false;
1577 : int i;
1578 4008 : char *view_def = NULL;
1579 : char *headers[12];
1580 : PQExpBufferData title;
1581 : PQExpBufferData tmpbuf;
1582 : int cols;
1583 4008 : int attname_col = -1, /* column indexes in "res" */
1584 4008 : atttype_col = -1,
1585 4008 : attrdef_col = -1,
1586 4008 : attnotnull_col = -1,
1587 4008 : attcoll_col = -1,
1588 4008 : attidentity_col = -1,
1589 4008 : attgenerated_col = -1,
1590 4008 : isindexkey_col = -1,
1591 4008 : indexdef_col = -1,
1592 4008 : fdwopts_col = -1,
1593 4008 : attstorage_col = -1,
1594 4008 : attcompression_col = -1,
1595 4008 : attstattarget_col = -1,
1596 4008 : attdescr_col = -1;
1597 : int numrows;
1598 : struct
1599 : {
1600 : int16 checks;
1601 : char relkind;
1602 : bool hasindex;
1603 : bool hasrules;
1604 : bool hastriggers;
1605 : bool rowsecurity;
1606 : bool forcerowsecurity;
1607 : bool hasoids;
1608 : bool ispartition;
1609 : Oid tablespace;
1610 : char *reloptions;
1611 : char *reloftype;
1612 : char relpersistence;
1613 : char relreplident;
1614 : char *relam;
1615 : } tableinfo;
1616 4008 : bool show_column_details = false;
1617 :
1618 4008 : myopt.default_footer = false;
1619 : /* This output looks confusing in expanded mode. */
1620 4008 : myopt.expanded = false;
1621 :
1622 4008 : initPQExpBuffer(&buf);
1623 4008 : initPQExpBuffer(&title);
1624 4008 : initPQExpBuffer(&tmpbuf);
1625 :
1626 : /* Get general table info */
1627 4008 : if (pset.sversion >= 120000)
1628 : {
1629 4008 : printfPQExpBuffer(&buf,
1630 : "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1631 : "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1632 : "false AS relhasoids, c.relispartition, %s, c.reltablespace, "
1633 : "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1634 : "c.relpersistence, c.relreplident, am.amname\n"
1635 : "FROM pg_catalog.pg_class c\n "
1636 : "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1637 : "LEFT JOIN pg_catalog.pg_am am ON (c.relam = am.oid)\n"
1638 : "WHERE c.oid = '%s';",
1639 : (verbose ?
1640 : "pg_catalog.array_to_string(c.reloptions || "
1641 : "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1642 : : "''"),
1643 : oid);
1644 : }
1645 0 : else if (pset.sversion >= 100000)
1646 : {
1647 0 : printfPQExpBuffer(&buf,
1648 : "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1649 : "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1650 : "c.relhasoids, c.relispartition, %s, c.reltablespace, "
1651 : "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1652 : "c.relpersistence, c.relreplident\n"
1653 : "FROM pg_catalog.pg_class c\n "
1654 : "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1655 : "WHERE c.oid = '%s';",
1656 : (verbose ?
1657 : "pg_catalog.array_to_string(c.reloptions || "
1658 : "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1659 : : "''"),
1660 : oid);
1661 : }
1662 0 : else if (pset.sversion >= 90500)
1663 : {
1664 0 : printfPQExpBuffer(&buf,
1665 : "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1666 : "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1667 : "c.relhasoids, false as relispartition, %s, c.reltablespace, "
1668 : "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1669 : "c.relpersistence, c.relreplident\n"
1670 : "FROM pg_catalog.pg_class c\n "
1671 : "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1672 : "WHERE c.oid = '%s';",
1673 : (verbose ?
1674 : "pg_catalog.array_to_string(c.reloptions || "
1675 : "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1676 : : "''"),
1677 : oid);
1678 : }
1679 0 : else if (pset.sversion >= 90400)
1680 : {
1681 0 : printfPQExpBuffer(&buf,
1682 : "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1683 : "c.relhastriggers, false, false, c.relhasoids, "
1684 : "false as relispartition, %s, c.reltablespace, "
1685 : "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1686 : "c.relpersistence, c.relreplident\n"
1687 : "FROM pg_catalog.pg_class c\n "
1688 : "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1689 : "WHERE c.oid = '%s';",
1690 : (verbose ?
1691 : "pg_catalog.array_to_string(c.reloptions || "
1692 : "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1693 : : "''"),
1694 : oid);
1695 : }
1696 : else
1697 : {
1698 0 : printfPQExpBuffer(&buf,
1699 : "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1700 : "c.relhastriggers, false, false, c.relhasoids, "
1701 : "false as relispartition, %s, c.reltablespace, "
1702 : "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1703 : "c.relpersistence\n"
1704 : "FROM pg_catalog.pg_class c\n "
1705 : "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1706 : "WHERE c.oid = '%s';",
1707 : (verbose ?
1708 : "pg_catalog.array_to_string(c.reloptions || "
1709 : "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1710 : : "''"),
1711 : oid);
1712 : }
1713 :
1714 4008 : res = PSQLexec(buf.data);
1715 4008 : if (!res)
1716 0 : goto error_return;
1717 :
1718 : /* Did we get anything? */
1719 4008 : if (PQntuples(res) == 0)
1720 : {
1721 0 : if (!pset.quiet)
1722 0 : pg_log_error("Did not find any relation with OID %s.", oid);
1723 0 : goto error_return;
1724 : }
1725 :
1726 4008 : tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1727 4008 : tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1728 4008 : tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1729 4008 : tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1730 4008 : tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1731 4008 : tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1732 4008 : tableinfo.forcerowsecurity = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1733 4008 : tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 7), "t") == 0;
1734 4008 : tableinfo.ispartition = strcmp(PQgetvalue(res, 0, 8), "t") == 0;
1735 4008 : tableinfo.reloptions = pg_strdup(PQgetvalue(res, 0, 9));
1736 4008 : tableinfo.tablespace = atooid(PQgetvalue(res, 0, 10));
1737 4008 : tableinfo.reloftype = (strcmp(PQgetvalue(res, 0, 11), "") != 0) ?
1738 4008 : pg_strdup(PQgetvalue(res, 0, 11)) : NULL;
1739 4008 : tableinfo.relpersistence = *(PQgetvalue(res, 0, 12));
1740 8016 : tableinfo.relreplident = (pset.sversion >= 90400) ?
1741 4008 : *(PQgetvalue(res, 0, 13)) : 'd';
1742 4008 : if (pset.sversion >= 120000)
1743 8016 : tableinfo.relam = PQgetisnull(res, 0, 14) ?
1744 4008 : NULL : pg_strdup(PQgetvalue(res, 0, 14));
1745 : else
1746 0 : tableinfo.relam = NULL;
1747 4008 : PQclear(res);
1748 4008 : res = NULL;
1749 :
1750 : /*
1751 : * If it's a sequence, deal with it here separately.
1752 : */
1753 4008 : if (tableinfo.relkind == RELKIND_SEQUENCE)
1754 : {
1755 192 : PGresult *result = NULL;
1756 192 : printQueryOpt myopt = pset.popt;
1757 192 : char *footers[2] = {NULL, NULL};
1758 :
1759 192 : if (pset.sversion >= 100000)
1760 : {
1761 192 : printfPQExpBuffer(&buf,
1762 : "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n"
1763 : " seqstart AS \"%s\",\n"
1764 : " seqmin AS \"%s\",\n"
1765 : " seqmax AS \"%s\",\n"
1766 : " seqincrement AS \"%s\",\n"
1767 : " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n"
1768 : " seqcache AS \"%s\"\n",
1769 : gettext_noop("Type"),
1770 : gettext_noop("Start"),
1771 : gettext_noop("Minimum"),
1772 : gettext_noop("Maximum"),
1773 : gettext_noop("Increment"),
1774 : gettext_noop("yes"),
1775 : gettext_noop("no"),
1776 : gettext_noop("Cycles?"),
1777 : gettext_noop("Cache"));
1778 192 : appendPQExpBuffer(&buf,
1779 : "FROM pg_catalog.pg_sequence\n"
1780 : "WHERE seqrelid = '%s';",
1781 : oid);
1782 : }
1783 : else
1784 : {
1785 0 : printfPQExpBuffer(&buf,
1786 : "SELECT 'bigint' AS \"%s\",\n"
1787 : " start_value AS \"%s\",\n"
1788 : " min_value AS \"%s\",\n"
1789 : " max_value AS \"%s\",\n"
1790 : " increment_by AS \"%s\",\n"
1791 : " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n"
1792 : " cache_value AS \"%s\"\n",
1793 : gettext_noop("Type"),
1794 : gettext_noop("Start"),
1795 : gettext_noop("Minimum"),
1796 : gettext_noop("Maximum"),
1797 : gettext_noop("Increment"),
1798 : gettext_noop("yes"),
1799 : gettext_noop("no"),
1800 : gettext_noop("Cycles?"),
1801 : gettext_noop("Cache"));
1802 0 : appendPQExpBuffer(&buf, "FROM %s", fmtId(schemaname));
1803 : /* must be separate because fmtId isn't reentrant */
1804 0 : appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1805 : }
1806 :
1807 192 : res = PSQLexec(buf.data);
1808 192 : if (!res)
1809 0 : goto error_return;
1810 :
1811 : /* Get the column that owns this sequence */
1812 192 : printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1813 : "\n pg_catalog.quote_ident(relname) || '.' ||"
1814 : "\n pg_catalog.quote_ident(attname),"
1815 : "\n d.deptype"
1816 : "\nFROM pg_catalog.pg_class c"
1817 : "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1818 : "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1819 : "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1820 : "\n a.attrelid=c.oid AND"
1821 : "\n a.attnum=d.refobjsubid)"
1822 : "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1823 : "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1824 : "\n AND d.objid='%s'"
1825 : "\n AND d.deptype IN ('a', 'i')",
1826 : oid);
1827 :
1828 192 : result = PSQLexec(buf.data);
1829 :
1830 : /*
1831 : * If we get no rows back, don't show anything (obviously). We should
1832 : * never get more than one row back, but if we do, just ignore it and
1833 : * don't print anything.
1834 : */
1835 192 : if (!result)
1836 0 : goto error_return;
1837 192 : else if (PQntuples(result) == 1)
1838 : {
1839 174 : switch (PQgetvalue(result, 0, 1)[0])
1840 : {
1841 120 : case 'a':
1842 120 : footers[0] = psprintf(_("Owned by: %s"),
1843 : PQgetvalue(result, 0, 0));
1844 120 : break;
1845 54 : case 'i':
1846 54 : footers[0] = psprintf(_("Sequence for identity column: %s"),
1847 : PQgetvalue(result, 0, 0));
1848 54 : break;
1849 : }
1850 18 : }
1851 192 : PQclear(result);
1852 :
1853 192 : if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
1854 30 : printfPQExpBuffer(&title, _("Unlogged sequence \"%s.%s\""),
1855 : schemaname, relationname);
1856 : else
1857 162 : printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1858 : schemaname, relationname);
1859 :
1860 192 : myopt.footers = footers;
1861 192 : myopt.topt.default_footer = false;
1862 192 : myopt.title = title.data;
1863 192 : myopt.translate_header = true;
1864 :
1865 192 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1866 :
1867 192 : free(footers[0]);
1868 :
1869 192 : retval = true;
1870 192 : goto error_return; /* not an error, just return early */
1871 : }
1872 :
1873 : /* Identify whether we should print collation, nullable, default vals */
1874 3816 : if (tableinfo.relkind == RELKIND_RELATION ||
1875 1378 : tableinfo.relkind == RELKIND_VIEW ||
1876 1008 : tableinfo.relkind == RELKIND_MATVIEW ||
1877 948 : tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1878 750 : tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1879 672 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1880 3422 : show_column_details = true;
1881 :
1882 : /*
1883 : * Get per-column info
1884 : *
1885 : * Since the set of query columns we need varies depending on relkind and
1886 : * server version, we compute all the column numbers on-the-fly. Column
1887 : * number variables for columns not fetched are left as -1; this avoids
1888 : * duplicative test logic below.
1889 : */
1890 3816 : cols = 0;
1891 3816 : printfPQExpBuffer(&buf, "SELECT a.attname");
1892 3816 : attname_col = cols++;
1893 3816 : appendPQExpBufferStr(&buf, ",\n pg_catalog.format_type(a.atttypid, a.atttypmod)");
1894 3816 : atttype_col = cols++;
1895 :
1896 3816 : if (show_column_details)
1897 : {
1898 : /* use "pretty" mode for expression to avoid excessive parentheses */
1899 3422 : appendPQExpBufferStr(&buf,
1900 : ",\n (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid, true)"
1901 : "\n FROM pg_catalog.pg_attrdef d"
1902 : "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)"
1903 : ",\n a.attnotnull");
1904 3422 : attrdef_col = cols++;
1905 3422 : attnotnull_col = cols++;
1906 3422 : appendPQExpBufferStr(&buf, ",\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1907 : " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1908 3422 : attcoll_col = cols++;
1909 3422 : if (pset.sversion >= 100000)
1910 3422 : appendPQExpBufferStr(&buf, ",\n a.attidentity");
1911 : else
1912 0 : appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attidentity");
1913 3422 : attidentity_col = cols++;
1914 3422 : if (pset.sversion >= 120000)
1915 3422 : appendPQExpBufferStr(&buf, ",\n a.attgenerated");
1916 : else
1917 0 : appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attgenerated");
1918 3422 : attgenerated_col = cols++;
1919 : }
1920 3816 : if (tableinfo.relkind == RELKIND_INDEX ||
1921 3560 : tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1922 : {
1923 388 : if (pset.sversion >= 110000)
1924 : {
1925 388 : appendPQExpBuffer(&buf, ",\n CASE WHEN a.attnum <= (SELECT i.indnkeyatts FROM pg_catalog.pg_index i WHERE i.indexrelid = '%s') THEN '%s' ELSE '%s' END AS is_key",
1926 : oid,
1927 : gettext_noop("yes"),
1928 : gettext_noop("no"));
1929 388 : isindexkey_col = cols++;
1930 : }
1931 388 : appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1932 388 : indexdef_col = cols++;
1933 : }
1934 : /* FDW options for foreign table column */
1935 3816 : if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
1936 : {
1937 198 : appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1938 : " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1939 : " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1940 198 : fdwopts_col = cols++;
1941 : }
1942 3816 : if (verbose)
1943 : {
1944 1688 : appendPQExpBufferStr(&buf, ",\n a.attstorage");
1945 1688 : attstorage_col = cols++;
1946 :
1947 : /* compression info, if relevant to relkind */
1948 1688 : if (pset.sversion >= 140000 &&
1949 1688 : !pset.hide_compression &&
1950 78 : (tableinfo.relkind == RELKIND_RELATION ||
1951 12 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
1952 12 : tableinfo.relkind == RELKIND_MATVIEW))
1953 : {
1954 78 : appendPQExpBufferStr(&buf, ",\n a.attcompression AS attcompression");
1955 78 : attcompression_col = cols++;
1956 : }
1957 :
1958 : /* stats target, if relevant to relkind */
1959 1688 : if (tableinfo.relkind == RELKIND_RELATION ||
1960 712 : tableinfo.relkind == RELKIND_INDEX ||
1961 678 : tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1962 672 : tableinfo.relkind == RELKIND_MATVIEW ||
1963 612 : tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1964 462 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1965 : {
1966 1336 : appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1967 1336 : attstattarget_col = cols++;
1968 : }
1969 :
1970 : /*
1971 : * In 9.0+, we have column comments for: relations, views, composite
1972 : * types, and foreign tables (cf. CommentObject() in comment.c).
1973 : */
1974 1688 : if (tableinfo.relkind == RELKIND_RELATION ||
1975 712 : tableinfo.relkind == RELKIND_VIEW ||
1976 360 : tableinfo.relkind == RELKIND_MATVIEW ||
1977 300 : tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1978 150 : tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1979 150 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1980 : {
1981 1648 : appendPQExpBufferStr(&buf, ",\n pg_catalog.col_description(a.attrelid, a.attnum)");
1982 1648 : attdescr_col = cols++;
1983 : }
1984 : }
1985 :
1986 3816 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1987 3816 : appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1988 3816 : appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1989 :
1990 3816 : res = PSQLexec(buf.data);
1991 3816 : if (!res)
1992 0 : goto error_return;
1993 3816 : numrows = PQntuples(res);
1994 :
1995 : /* Make title */
1996 3816 : switch (tableinfo.relkind)
1997 : {
1998 2438 : case RELKIND_RELATION:
1999 2438 : if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2000 18 : printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
2001 : schemaname, relationname);
2002 : else
2003 2420 : printfPQExpBuffer(&title, _("Table \"%s.%s\""),
2004 : schemaname, relationname);
2005 2438 : break;
2006 370 : case RELKIND_VIEW:
2007 370 : printfPQExpBuffer(&title, _("View \"%s.%s\""),
2008 : schemaname, relationname);
2009 370 : break;
2010 60 : case RELKIND_MATVIEW:
2011 60 : printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
2012 : schemaname, relationname);
2013 60 : break;
2014 256 : case RELKIND_INDEX:
2015 256 : if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2016 0 : printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
2017 : schemaname, relationname);
2018 : else
2019 256 : printfPQExpBuffer(&title, _("Index \"%s.%s\""),
2020 : schemaname, relationname);
2021 256 : break;
2022 132 : case RELKIND_PARTITIONED_INDEX:
2023 132 : if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2024 0 : printfPQExpBuffer(&title, _("Unlogged partitioned index \"%s.%s\""),
2025 : schemaname, relationname);
2026 : else
2027 132 : printfPQExpBuffer(&title, _("Partitioned index \"%s.%s\""),
2028 : schemaname, relationname);
2029 132 : break;
2030 6 : case RELKIND_TOASTVALUE:
2031 6 : printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
2032 : schemaname, relationname);
2033 6 : break;
2034 78 : case RELKIND_COMPOSITE_TYPE:
2035 78 : printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
2036 : schemaname, relationname);
2037 78 : break;
2038 198 : case RELKIND_FOREIGN_TABLE:
2039 198 : printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
2040 : schemaname, relationname);
2041 198 : break;
2042 278 : case RELKIND_PARTITIONED_TABLE:
2043 278 : if (tableinfo.relpersistence == RELPERSISTENCE_UNLOGGED)
2044 0 : printfPQExpBuffer(&title, _("Unlogged partitioned table \"%s.%s\""),
2045 : schemaname, relationname);
2046 : else
2047 278 : printfPQExpBuffer(&title, _("Partitioned table \"%s.%s\""),
2048 : schemaname, relationname);
2049 278 : break;
2050 0 : default:
2051 : /* untranslated unknown relkind */
2052 0 : printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
2053 0 : tableinfo.relkind, schemaname, relationname);
2054 0 : break;
2055 : }
2056 :
2057 : /* Fill headers[] with the names of the columns we will output */
2058 3816 : cols = 0;
2059 3816 : headers[cols++] = gettext_noop("Column");
2060 3816 : headers[cols++] = gettext_noop("Type");
2061 3816 : if (show_column_details)
2062 : {
2063 3422 : headers[cols++] = gettext_noop("Collation");
2064 3422 : headers[cols++] = gettext_noop("Nullable");
2065 3422 : headers[cols++] = gettext_noop("Default");
2066 : }
2067 3816 : if (isindexkey_col >= 0)
2068 388 : headers[cols++] = gettext_noop("Key?");
2069 3816 : if (indexdef_col >= 0)
2070 388 : headers[cols++] = gettext_noop("Definition");
2071 3816 : if (fdwopts_col >= 0)
2072 198 : headers[cols++] = gettext_noop("FDW options");
2073 3816 : if (attstorage_col >= 0)
2074 1688 : headers[cols++] = gettext_noop("Storage");
2075 3816 : if (attcompression_col >= 0)
2076 78 : headers[cols++] = gettext_noop("Compression");
2077 3816 : if (attstattarget_col >= 0)
2078 1336 : headers[cols++] = gettext_noop("Stats target");
2079 3816 : if (attdescr_col >= 0)
2080 1648 : headers[cols++] = gettext_noop("Description");
2081 :
2082 : Assert(cols <= lengthof(headers));
2083 :
2084 3816 : printTableInit(&cont, &myopt, title.data, cols, numrows);
2085 3816 : printTableInitialized = true;
2086 :
2087 27438 : for (i = 0; i < cols; i++)
2088 23622 : printTableAddHeader(&cont, headers[i], true, 'l');
2089 :
2090 : /* Generate table cells to be printed */
2091 12916 : for (i = 0; i < numrows; i++)
2092 : {
2093 : /* Column */
2094 9100 : printTableAddCell(&cont, PQgetvalue(res, i, attname_col), false, false);
2095 :
2096 : /* Type */
2097 9100 : printTableAddCell(&cont, PQgetvalue(res, i, atttype_col), false, false);
2098 :
2099 : /* Collation, Nullable, Default */
2100 9100 : if (show_column_details)
2101 : {
2102 : char *identity;
2103 : char *generated;
2104 : char *default_str;
2105 8632 : bool mustfree = false;
2106 :
2107 8632 : printTableAddCell(&cont, PQgetvalue(res, i, attcoll_col), false, false);
2108 :
2109 8632 : printTableAddCell(&cont,
2110 8632 : strcmp(PQgetvalue(res, i, attnotnull_col), "t") == 0 ? "not null" : "",
2111 : false, false);
2112 :
2113 8632 : identity = PQgetvalue(res, i, attidentity_col);
2114 8632 : generated = PQgetvalue(res, i, attgenerated_col);
2115 :
2116 8632 : if (identity[0] == ATTRIBUTE_IDENTITY_ALWAYS)
2117 66 : default_str = "generated always as identity";
2118 8566 : else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
2119 24 : default_str = "generated by default as identity";
2120 8542 : else if (generated[0] == ATTRIBUTE_GENERATED_STORED)
2121 : {
2122 232 : default_str = psprintf("generated always as (%s) stored",
2123 : PQgetvalue(res, i, attrdef_col));
2124 232 : mustfree = true;
2125 : }
2126 8310 : else if (generated[0] == ATTRIBUTE_GENERATED_VIRTUAL)
2127 : {
2128 192 : default_str = psprintf("generated always as (%s)",
2129 : PQgetvalue(res, i, attrdef_col));
2130 192 : mustfree = true;
2131 : }
2132 : else
2133 8118 : default_str = PQgetvalue(res, i, attrdef_col);
2134 :
2135 8632 : printTableAddCell(&cont, default_str, false, mustfree);
2136 : }
2137 :
2138 : /* Info for index columns */
2139 9100 : if (isindexkey_col >= 0)
2140 450 : printTableAddCell(&cont, PQgetvalue(res, i, isindexkey_col), true, false);
2141 9100 : if (indexdef_col >= 0)
2142 450 : printTableAddCell(&cont, PQgetvalue(res, i, indexdef_col), false, false);
2143 :
2144 : /* FDW options for foreign table columns */
2145 9100 : if (fdwopts_col >= 0)
2146 750 : printTableAddCell(&cont, PQgetvalue(res, i, fdwopts_col), false, false);
2147 :
2148 : /* Storage mode, if relevant */
2149 9100 : if (attstorage_col >= 0)
2150 : {
2151 4108 : char *storage = PQgetvalue(res, i, attstorage_col);
2152 :
2153 : /* these strings are literal in our syntax, so not translated. */
2154 5350 : printTableAddCell(&cont, (storage[0] == TYPSTORAGE_PLAIN ? "plain" :
2155 2340 : (storage[0] == TYPSTORAGE_MAIN ? "main" :
2156 1140 : (storage[0] == TYPSTORAGE_EXTENDED ? "extended" :
2157 42 : (storage[0] == TYPSTORAGE_EXTERNAL ? "external" :
2158 : "???")))),
2159 : false, false);
2160 : }
2161 :
2162 : /* Column compression, if relevant */
2163 9100 : if (attcompression_col >= 0)
2164 : {
2165 78 : char *compression = PQgetvalue(res, i, attcompression_col);
2166 :
2167 : /* these strings are literal in our syntax, so not translated. */
2168 138 : printTableAddCell(&cont, (compression[0] == 'p' ? "pglz" :
2169 96 : (compression[0] == 'l' ? "lz4" :
2170 36 : (compression[0] == '\0' ? "" :
2171 : "???"))),
2172 : false, false);
2173 : }
2174 :
2175 : /* Statistics target, if the relkind supports this feature */
2176 9100 : if (attstattarget_col >= 0)
2177 3176 : printTableAddCell(&cont, PQgetvalue(res, i, attstattarget_col),
2178 : false, false);
2179 :
2180 : /* Column comments, if the relkind supports this feature */
2181 9100 : if (attdescr_col >= 0)
2182 4036 : printTableAddCell(&cont, PQgetvalue(res, i, attdescr_col),
2183 : false, false);
2184 : }
2185 :
2186 : /* Make footers */
2187 :
2188 3816 : if (tableinfo.ispartition)
2189 : {
2190 : /* Footer information for a partition child table */
2191 : PGresult *result;
2192 :
2193 518 : printfPQExpBuffer(&buf,
2194 : "SELECT inhparent::pg_catalog.regclass,\n"
2195 : " pg_catalog.pg_get_expr(c.relpartbound, c.oid),\n ");
2196 :
2197 518 : appendPQExpBufferStr(&buf,
2198 518 : pset.sversion >= 140000 ? "inhdetachpending" :
2199 : "false as inhdetachpending");
2200 :
2201 : /* If verbose, also request the partition constraint definition */
2202 518 : if (verbose)
2203 170 : appendPQExpBufferStr(&buf,
2204 : ",\n pg_catalog.pg_get_partition_constraintdef(c.oid)");
2205 518 : appendPQExpBuffer(&buf,
2206 : "\nFROM pg_catalog.pg_class c"
2207 : " JOIN pg_catalog.pg_inherits i"
2208 : " ON c.oid = inhrelid"
2209 : "\nWHERE c.oid = '%s';", oid);
2210 518 : result = PSQLexec(buf.data);
2211 518 : if (!result)
2212 0 : goto error_return;
2213 :
2214 518 : if (PQntuples(result) > 0)
2215 : {
2216 518 : char *parent_name = PQgetvalue(result, 0, 0);
2217 518 : char *partdef = PQgetvalue(result, 0, 1);
2218 518 : char *detached = PQgetvalue(result, 0, 2);
2219 :
2220 518 : printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s%s"), parent_name,
2221 : partdef,
2222 518 : strcmp(detached, "t") == 0 ? " DETACH PENDING" : "");
2223 518 : printTableAddFooter(&cont, tmpbuf.data);
2224 :
2225 518 : if (verbose)
2226 : {
2227 170 : char *partconstraintdef = NULL;
2228 :
2229 170 : if (!PQgetisnull(result, 0, 3))
2230 152 : partconstraintdef = PQgetvalue(result, 0, 3);
2231 : /* If there isn't any constraint, show that explicitly */
2232 170 : if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
2233 18 : printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
2234 : else
2235 152 : printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"),
2236 : partconstraintdef);
2237 170 : printTableAddFooter(&cont, tmpbuf.data);
2238 : }
2239 : }
2240 518 : PQclear(result);
2241 : }
2242 :
2243 3816 : if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2244 : {
2245 : /* Footer information for a partitioned table (partitioning parent) */
2246 : PGresult *result;
2247 :
2248 278 : printfPQExpBuffer(&buf,
2249 : "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
2250 : oid);
2251 278 : result = PSQLexec(buf.data);
2252 278 : if (!result)
2253 0 : goto error_return;
2254 :
2255 278 : if (PQntuples(result) == 1)
2256 : {
2257 278 : char *partkeydef = PQgetvalue(result, 0, 0);
2258 :
2259 278 : printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2260 278 : printTableAddFooter(&cont, tmpbuf.data);
2261 : }
2262 278 : PQclear(result);
2263 : }
2264 :
2265 3816 : if (tableinfo.relkind == RELKIND_TOASTVALUE)
2266 : {
2267 : /* For a TOAST table, print name of owning table */
2268 : PGresult *result;
2269 :
2270 6 : printfPQExpBuffer(&buf,
2271 : "SELECT n.nspname, c.relname\n"
2272 : "FROM pg_catalog.pg_class c"
2273 : " JOIN pg_catalog.pg_namespace n"
2274 : " ON n.oid = c.relnamespace\n"
2275 : "WHERE reltoastrelid = '%s';", oid);
2276 6 : result = PSQLexec(buf.data);
2277 6 : if (!result)
2278 0 : goto error_return;
2279 :
2280 6 : if (PQntuples(result) == 1)
2281 : {
2282 6 : char *schemaname = PQgetvalue(result, 0, 0);
2283 6 : char *relname = PQgetvalue(result, 0, 1);
2284 :
2285 6 : printfPQExpBuffer(&tmpbuf, _("Owning table: \"%s.%s\""),
2286 : schemaname, relname);
2287 6 : printTableAddFooter(&cont, tmpbuf.data);
2288 : }
2289 6 : PQclear(result);
2290 : }
2291 :
2292 3816 : if (tableinfo.relkind == RELKIND_INDEX ||
2293 3560 : tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
2294 388 : {
2295 : /* Footer information about an index */
2296 : PGresult *result;
2297 :
2298 388 : printfPQExpBuffer(&buf,
2299 : "SELECT i.indisunique, i.indisprimary, i.indisclustered, "
2300 : "i.indisvalid,\n"
2301 : " (NOT i.indimmediate) AND "
2302 : "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2303 : "WHERE conrelid = i.indrelid AND "
2304 : "conindid = i.indexrelid AND "
2305 : "contype IN (" CppAsString2(CONSTRAINT_PRIMARY) ","
2306 : CppAsString2(CONSTRAINT_UNIQUE) ","
2307 : CppAsString2(CONSTRAINT_EXCLUSION) ") AND "
2308 : "condeferrable) AS condeferrable,\n"
2309 : " (NOT i.indimmediate) AND "
2310 : "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2311 : "WHERE conrelid = i.indrelid AND "
2312 : "conindid = i.indexrelid AND "
2313 : "contype IN (" CppAsString2(CONSTRAINT_PRIMARY) ","
2314 : CppAsString2(CONSTRAINT_UNIQUE) ","
2315 : CppAsString2(CONSTRAINT_EXCLUSION) ") AND "
2316 : "condeferred) AS condeferred,\n");
2317 :
2318 388 : if (pset.sversion >= 90400)
2319 388 : appendPQExpBufferStr(&buf, "i.indisreplident,\n");
2320 : else
2321 0 : appendPQExpBufferStr(&buf, "false AS indisreplident,\n");
2322 :
2323 388 : if (pset.sversion >= 150000)
2324 388 : appendPQExpBufferStr(&buf, "i.indnullsnotdistinct,\n");
2325 : else
2326 0 : appendPQExpBufferStr(&buf, "false AS indnullsnotdistinct,\n");
2327 :
2328 388 : appendPQExpBuffer(&buf, " a.amname, c2.relname, "
2329 : "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
2330 : "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
2331 : "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
2332 : "AND i.indrelid = c2.oid;",
2333 : oid);
2334 :
2335 388 : result = PSQLexec(buf.data);
2336 388 : if (!result)
2337 0 : goto error_return;
2338 388 : else if (PQntuples(result) != 1)
2339 : {
2340 0 : PQclear(result);
2341 0 : goto error_return;
2342 : }
2343 : else
2344 : {
2345 388 : char *indisunique = PQgetvalue(result, 0, 0);
2346 388 : char *indisprimary = PQgetvalue(result, 0, 1);
2347 388 : char *indisclustered = PQgetvalue(result, 0, 2);
2348 388 : char *indisvalid = PQgetvalue(result, 0, 3);
2349 388 : char *deferrable = PQgetvalue(result, 0, 4);
2350 388 : char *deferred = PQgetvalue(result, 0, 5);
2351 388 : char *indisreplident = PQgetvalue(result, 0, 6);
2352 388 : char *indnullsnotdistinct = PQgetvalue(result, 0, 7);
2353 388 : char *indamname = PQgetvalue(result, 0, 8);
2354 388 : char *indtable = PQgetvalue(result, 0, 9);
2355 388 : char *indpred = PQgetvalue(result, 0, 10);
2356 :
2357 388 : if (strcmp(indisprimary, "t") == 0)
2358 84 : printfPQExpBuffer(&tmpbuf, _("primary key, "));
2359 304 : else if (strcmp(indisunique, "t") == 0)
2360 : {
2361 102 : printfPQExpBuffer(&tmpbuf, _("unique"));
2362 102 : if (strcmp(indnullsnotdistinct, "t") == 0)
2363 6 : appendPQExpBufferStr(&tmpbuf, _(" nulls not distinct"));
2364 102 : appendPQExpBufferStr(&tmpbuf, _(", "));
2365 : }
2366 : else
2367 202 : resetPQExpBuffer(&tmpbuf);
2368 388 : appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
2369 :
2370 : /* we assume here that index and table are in same schema */
2371 388 : appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
2372 : schemaname, indtable);
2373 :
2374 388 : if (strlen(indpred))
2375 0 : appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
2376 :
2377 388 : if (strcmp(indisclustered, "t") == 0)
2378 0 : appendPQExpBufferStr(&tmpbuf, _(", clustered"));
2379 :
2380 388 : if (strcmp(indisvalid, "t") != 0)
2381 0 : appendPQExpBufferStr(&tmpbuf, _(", invalid"));
2382 :
2383 388 : if (strcmp(deferrable, "t") == 0)
2384 0 : appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
2385 :
2386 388 : if (strcmp(deferred, "t") == 0)
2387 0 : appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
2388 :
2389 388 : if (strcmp(indisreplident, "t") == 0)
2390 0 : appendPQExpBufferStr(&tmpbuf, _(", replica identity"));
2391 :
2392 388 : printTableAddFooter(&cont, tmpbuf.data);
2393 :
2394 : /*
2395 : * If it's a partitioned index, we'll print the tablespace below
2396 : */
2397 388 : if (tableinfo.relkind == RELKIND_INDEX)
2398 256 : add_tablespace_footer(&cont, tableinfo.relkind,
2399 : tableinfo.tablespace, true);
2400 : }
2401 :
2402 388 : PQclear(result);
2403 : }
2404 : /* If you add relkinds here, see also "Finish printing..." stanza below */
2405 3428 : else if (tableinfo.relkind == RELKIND_RELATION ||
2406 990 : tableinfo.relkind == RELKIND_MATVIEW ||
2407 930 : tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2408 732 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
2409 454 : tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
2410 454 : tableinfo.relkind == RELKIND_TOASTVALUE)
2411 : {
2412 : /* Footer information about a table */
2413 2980 : PGresult *result = NULL;
2414 2980 : int tuples = 0;
2415 :
2416 : /* print indexes */
2417 2980 : if (tableinfo.hasindex)
2418 : {
2419 970 : printfPQExpBuffer(&buf,
2420 : "SELECT c2.relname, i.indisprimary, i.indisunique, "
2421 : "i.indisclustered, i.indisvalid, "
2422 : "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "
2423 : "pg_catalog.pg_get_constraintdef(con.oid, true), "
2424 : "contype, condeferrable, condeferred");
2425 970 : if (pset.sversion >= 90400)
2426 970 : appendPQExpBufferStr(&buf, ", i.indisreplident");
2427 : else
2428 0 : appendPQExpBufferStr(&buf, ", false AS indisreplident");
2429 970 : appendPQExpBufferStr(&buf, ", c2.reltablespace");
2430 970 : if (pset.sversion >= 180000)
2431 970 : appendPQExpBufferStr(&buf, ", con.conperiod");
2432 : else
2433 0 : appendPQExpBufferStr(&buf, ", false AS conperiod");
2434 970 : appendPQExpBuffer(&buf,
2435 : "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
2436 : " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ("
2437 : CppAsString2(CONSTRAINT_PRIMARY) ","
2438 : CppAsString2(CONSTRAINT_UNIQUE) ","
2439 : CppAsString2(CONSTRAINT_EXCLUSION) "))\n"
2440 : "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2441 : "ORDER BY i.indisprimary DESC, c2.relname;",
2442 : oid);
2443 970 : result = PSQLexec(buf.data);
2444 970 : if (!result)
2445 0 : goto error_return;
2446 : else
2447 970 : tuples = PQntuples(result);
2448 :
2449 970 : if (tuples > 0)
2450 : {
2451 928 : printTableAddFooter(&cont, _("Indexes:"));
2452 2416 : for (i = 0; i < tuples; i++)
2453 : {
2454 : /* untranslated index name */
2455 1488 : printfPQExpBuffer(&buf, " \"%s\"",
2456 : PQgetvalue(result, i, 0));
2457 :
2458 : /*
2459 : * If exclusion constraint or PK/UNIQUE constraint WITHOUT
2460 : * OVERLAPS, print the constraintdef
2461 : */
2462 1488 : if (strcmp(PQgetvalue(result, i, 7), "x") == 0 ||
2463 1434 : strcmp(PQgetvalue(result, i, 12), "t") == 0)
2464 : {
2465 148 : appendPQExpBuffer(&buf, " %s",
2466 : PQgetvalue(result, i, 6));
2467 : }
2468 : else
2469 : {
2470 : const char *indexdef;
2471 : const char *usingpos;
2472 :
2473 : /* Label as primary key or unique (but not both) */
2474 1340 : if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2475 422 : appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2476 918 : else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2477 : {
2478 336 : if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2479 150 : appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2480 : else
2481 186 : appendPQExpBufferStr(&buf, " UNIQUE,");
2482 : }
2483 :
2484 : /* Everything after "USING" is echoed verbatim */
2485 1340 : indexdef = PQgetvalue(result, i, 5);
2486 1340 : usingpos = strstr(indexdef, " USING ");
2487 1340 : if (usingpos)
2488 1340 : indexdef = usingpos + 7;
2489 1340 : appendPQExpBuffer(&buf, " %s", indexdef);
2490 :
2491 : /* Need these for deferrable PK/UNIQUE indexes */
2492 1340 : if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2493 48 : appendPQExpBufferStr(&buf, " DEFERRABLE");
2494 :
2495 1340 : if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2496 18 : appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2497 : }
2498 :
2499 : /* Add these for all cases */
2500 1488 : if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2501 0 : appendPQExpBufferStr(&buf, " CLUSTER");
2502 :
2503 1488 : if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2504 42 : appendPQExpBufferStr(&buf, " INVALID");
2505 :
2506 1488 : if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2507 60 : appendPQExpBufferStr(&buf, " REPLICA IDENTITY");
2508 :
2509 1488 : printTableAddFooter(&cont, buf.data);
2510 :
2511 : /* Print tablespace of the index on the same line */
2512 1488 : add_tablespace_footer(&cont, RELKIND_INDEX,
2513 1488 : atooid(PQgetvalue(result, i, 11)),
2514 : false);
2515 : }
2516 : }
2517 970 : PQclear(result);
2518 : }
2519 :
2520 : /* print table (and column) check constraints */
2521 2980 : if (tableinfo.checks)
2522 : {
2523 414 : printfPQExpBuffer(&buf,
2524 : "SELECT r.conname, "
2525 : "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2526 : "FROM pg_catalog.pg_constraint r\n"
2527 : "WHERE r.conrelid = '%s' "
2528 : "AND r.contype = " CppAsString2(CONSTRAINT_CHECK) "\n"
2529 : "ORDER BY 1;",
2530 : oid);
2531 414 : result = PSQLexec(buf.data);
2532 414 : if (!result)
2533 0 : goto error_return;
2534 : else
2535 414 : tuples = PQntuples(result);
2536 :
2537 414 : if (tuples > 0)
2538 : {
2539 414 : printTableAddFooter(&cont, _("Check constraints:"));
2540 1062 : for (i = 0; i < tuples; i++)
2541 : {
2542 : /* untranslated constraint name and def */
2543 648 : printfPQExpBuffer(&buf, " \"%s\" %s",
2544 : PQgetvalue(result, i, 0),
2545 : PQgetvalue(result, i, 1));
2546 :
2547 648 : printTableAddFooter(&cont, buf.data);
2548 : }
2549 : }
2550 414 : PQclear(result);
2551 : }
2552 :
2553 : /*
2554 : * Print foreign-key constraints (there are none if no triggers,
2555 : * except if the table is partitioned, in which case the triggers
2556 : * appear in the partitions)
2557 : */
2558 2980 : if (tableinfo.hastriggers ||
2559 2702 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2560 : {
2561 514 : if (pset.sversion >= 120000 &&
2562 514 : (tableinfo.ispartition || tableinfo.relkind == RELKIND_PARTITIONED_TABLE))
2563 : {
2564 : /*
2565 : * Put the constraints defined in this table first, followed
2566 : * by the constraints defined in ancestor partitioned tables.
2567 : */
2568 332 : printfPQExpBuffer(&buf,
2569 : "SELECT conrelid = '%s'::pg_catalog.regclass AS sametable,\n"
2570 : " conname,\n"
2571 : " pg_catalog.pg_get_constraintdef(oid, true) AS condef,\n"
2572 : " conrelid::pg_catalog.regclass AS ontable\n"
2573 : " FROM pg_catalog.pg_constraint,\n"
2574 : " pg_catalog.pg_partition_ancestors('%s')\n"
2575 : " WHERE conrelid = relid AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2576 : "ORDER BY sametable DESC, conname;",
2577 : oid, oid);
2578 : }
2579 : else
2580 : {
2581 182 : printfPQExpBuffer(&buf,
2582 : "SELECT true as sametable, conname,\n"
2583 : " pg_catalog.pg_get_constraintdef(r.oid, true) as condef,\n"
2584 : " conrelid::pg_catalog.regclass AS ontable\n"
2585 : "FROM pg_catalog.pg_constraint r\n"
2586 : "WHERE r.conrelid = '%s' AND r.contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n",
2587 : oid);
2588 :
2589 182 : if (pset.sversion >= 120000)
2590 182 : appendPQExpBufferStr(&buf, " AND conparentid = 0\n");
2591 182 : appendPQExpBufferStr(&buf, "ORDER BY conname");
2592 : }
2593 :
2594 514 : result = PSQLexec(buf.data);
2595 514 : if (!result)
2596 0 : goto error_return;
2597 : else
2598 514 : tuples = PQntuples(result);
2599 :
2600 514 : if (tuples > 0)
2601 : {
2602 182 : int i_sametable = PQfnumber(result, "sametable"),
2603 182 : i_conname = PQfnumber(result, "conname"),
2604 182 : i_condef = PQfnumber(result, "condef"),
2605 182 : i_ontable = PQfnumber(result, "ontable");
2606 :
2607 182 : printTableAddFooter(&cont, _("Foreign-key constraints:"));
2608 400 : for (i = 0; i < tuples; i++)
2609 : {
2610 : /*
2611 : * Print untranslated constraint name and definition. Use
2612 : * a "TABLE tab" prefix when the constraint is defined in
2613 : * a parent partitioned table.
2614 : */
2615 218 : if (strcmp(PQgetvalue(result, i, i_sametable), "f") == 0)
2616 90 : printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2617 : PQgetvalue(result, i, i_ontable),
2618 : PQgetvalue(result, i, i_conname),
2619 : PQgetvalue(result, i, i_condef));
2620 : else
2621 128 : printfPQExpBuffer(&buf, " \"%s\" %s",
2622 : PQgetvalue(result, i, i_conname),
2623 : PQgetvalue(result, i, i_condef));
2624 :
2625 218 : printTableAddFooter(&cont, buf.data);
2626 : }
2627 : }
2628 514 : PQclear(result);
2629 : }
2630 :
2631 : /* print incoming foreign-key references */
2632 2980 : if (tableinfo.hastriggers ||
2633 2702 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2634 : {
2635 514 : if (pset.sversion >= 120000)
2636 : {
2637 514 : printfPQExpBuffer(&buf,
2638 : "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2639 : " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2640 : " FROM pg_catalog.pg_constraint c\n"
2641 : " WHERE confrelid IN (SELECT pg_catalog.pg_partition_ancestors('%s')\n"
2642 : " UNION ALL VALUES ('%s'::pg_catalog.regclass))\n"
2643 : " AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2644 : "ORDER BY conname;",
2645 : oid, oid);
2646 : }
2647 : else
2648 : {
2649 0 : printfPQExpBuffer(&buf,
2650 : "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2651 : " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2652 : " FROM pg_catalog.pg_constraint\n"
2653 : " WHERE confrelid = %s AND contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n"
2654 : "ORDER BY conname;",
2655 : oid);
2656 : }
2657 :
2658 514 : result = PSQLexec(buf.data);
2659 514 : if (!result)
2660 0 : goto error_return;
2661 : else
2662 514 : tuples = PQntuples(result);
2663 :
2664 514 : if (tuples > 0)
2665 : {
2666 60 : int i_conname = PQfnumber(result, "conname"),
2667 60 : i_ontable = PQfnumber(result, "ontable"),
2668 60 : i_condef = PQfnumber(result, "condef");
2669 :
2670 60 : printTableAddFooter(&cont, _("Referenced by:"));
2671 120 : for (i = 0; i < tuples; i++)
2672 : {
2673 60 : printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2674 : PQgetvalue(result, i, i_ontable),
2675 : PQgetvalue(result, i, i_conname),
2676 : PQgetvalue(result, i, i_condef));
2677 :
2678 60 : printTableAddFooter(&cont, buf.data);
2679 : }
2680 : }
2681 514 : PQclear(result);
2682 : }
2683 :
2684 : /* print any row-level policies */
2685 2980 : if (pset.sversion >= 90500)
2686 : {
2687 2980 : printfPQExpBuffer(&buf, "SELECT pol.polname,");
2688 2980 : if (pset.sversion >= 100000)
2689 2980 : appendPQExpBufferStr(&buf,
2690 : " pol.polpermissive,\n");
2691 : else
2692 0 : appendPQExpBufferStr(&buf,
2693 : " 't' as polpermissive,\n");
2694 2980 : appendPQExpBuffer(&buf,
2695 : " CASE WHEN pol.polroles = '{0}' THEN NULL ELSE pg_catalog.array_to_string(array(select rolname from pg_catalog.pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2696 : " pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2697 : " pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2698 : " CASE pol.polcmd\n"
2699 : " WHEN 'r' THEN 'SELECT'\n"
2700 : " WHEN 'a' THEN 'INSERT'\n"
2701 : " WHEN 'w' THEN 'UPDATE'\n"
2702 : " WHEN 'd' THEN 'DELETE'\n"
2703 : " END AS cmd\n"
2704 : "FROM pg_catalog.pg_policy pol\n"
2705 : "WHERE pol.polrelid = '%s' ORDER BY 1;",
2706 : oid);
2707 :
2708 2980 : result = PSQLexec(buf.data);
2709 2980 : if (!result)
2710 0 : goto error_return;
2711 : else
2712 2980 : tuples = PQntuples(result);
2713 :
2714 : /*
2715 : * Handle cases where RLS is enabled and there are policies, or
2716 : * there aren't policies, or RLS isn't enabled but there are
2717 : * policies
2718 : */
2719 2980 : if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2720 12 : printTableAddFooter(&cont, _("Policies:"));
2721 :
2722 2980 : if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2723 0 : printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2724 :
2725 2980 : if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2726 0 : printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2727 :
2728 2980 : if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2729 0 : printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2730 :
2731 2980 : if (!tableinfo.rowsecurity && tuples > 0)
2732 0 : printTableAddFooter(&cont, _("Policies (row security disabled):"));
2733 :
2734 : /* Might be an empty set - that's ok */
2735 3010 : for (i = 0; i < tuples; i++)
2736 : {
2737 30 : printfPQExpBuffer(&buf, " POLICY \"%s\"",
2738 : PQgetvalue(result, i, 0));
2739 :
2740 30 : if (*(PQgetvalue(result, i, 1)) == 'f')
2741 18 : appendPQExpBufferStr(&buf, " AS RESTRICTIVE");
2742 :
2743 30 : if (!PQgetisnull(result, i, 5))
2744 0 : appendPQExpBuffer(&buf, " FOR %s",
2745 : PQgetvalue(result, i, 5));
2746 :
2747 30 : if (!PQgetisnull(result, i, 2))
2748 : {
2749 18 : appendPQExpBuffer(&buf, "\n TO %s",
2750 : PQgetvalue(result, i, 2));
2751 : }
2752 :
2753 30 : if (!PQgetisnull(result, i, 3))
2754 30 : appendPQExpBuffer(&buf, "\n USING (%s)",
2755 : PQgetvalue(result, i, 3));
2756 :
2757 30 : if (!PQgetisnull(result, i, 4))
2758 0 : appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2759 : PQgetvalue(result, i, 4));
2760 :
2761 30 : printTableAddFooter(&cont, buf.data);
2762 : }
2763 2980 : PQclear(result);
2764 : }
2765 :
2766 : /* print any extended statistics */
2767 2980 : if (pset.sversion >= 140000)
2768 : {
2769 2980 : printfPQExpBuffer(&buf,
2770 : "SELECT oid, "
2771 : "stxrelid::pg_catalog.regclass, "
2772 : "stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS nsp, "
2773 : "stxname,\n"
2774 : "pg_catalog.pg_get_statisticsobjdef_columns(oid) AS columns,\n"
2775 : " " CppAsString2(STATS_EXT_NDISTINCT) " = any(stxkind) AS ndist_enabled,\n"
2776 : " " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(stxkind) AS deps_enabled,\n"
2777 : " " CppAsString2(STATS_EXT_MCV) " = any(stxkind) AS mcv_enabled,\n"
2778 : "stxstattarget\n"
2779 : "FROM pg_catalog.pg_statistic_ext\n"
2780 : "WHERE stxrelid = '%s'\n"
2781 : "ORDER BY nsp, stxname;",
2782 : oid);
2783 :
2784 2980 : result = PSQLexec(buf.data);
2785 2980 : if (!result)
2786 0 : goto error_return;
2787 : else
2788 2980 : tuples = PQntuples(result);
2789 :
2790 2980 : if (tuples > 0)
2791 : {
2792 54 : printTableAddFooter(&cont, _("Statistics objects:"));
2793 :
2794 126 : for (i = 0; i < tuples; i++)
2795 : {
2796 72 : bool gotone = false;
2797 : bool has_ndistinct;
2798 : bool has_dependencies;
2799 : bool has_mcv;
2800 : bool has_all;
2801 : bool has_some;
2802 :
2803 72 : has_ndistinct = (strcmp(PQgetvalue(result, i, 5), "t") == 0);
2804 72 : has_dependencies = (strcmp(PQgetvalue(result, i, 6), "t") == 0);
2805 72 : has_mcv = (strcmp(PQgetvalue(result, i, 7), "t") == 0);
2806 :
2807 72 : printfPQExpBuffer(&buf, " ");
2808 :
2809 : /* statistics object name (qualified with namespace) */
2810 72 : appendPQExpBuffer(&buf, "\"%s.%s\"",
2811 : PQgetvalue(result, i, 2),
2812 : PQgetvalue(result, i, 3));
2813 :
2814 : /*
2815 : * When printing kinds we ignore expression statistics,
2816 : * which are used only internally and can't be specified
2817 : * by user. We don't print the kinds when none are
2818 : * specified (in which case it has to be statistics on a
2819 : * single expr) or when all are specified (in which case
2820 : * we assume it's expanded by CREATE STATISTICS).
2821 : */
2822 72 : has_all = (has_ndistinct && has_dependencies && has_mcv);
2823 72 : has_some = (has_ndistinct || has_dependencies || has_mcv);
2824 :
2825 72 : if (has_some && !has_all)
2826 : {
2827 0 : appendPQExpBufferStr(&buf, " (");
2828 :
2829 : /* options */
2830 0 : if (has_ndistinct)
2831 : {
2832 0 : appendPQExpBufferStr(&buf, "ndistinct");
2833 0 : gotone = true;
2834 : }
2835 :
2836 0 : if (has_dependencies)
2837 : {
2838 0 : appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2839 0 : gotone = true;
2840 : }
2841 :
2842 0 : if (has_mcv)
2843 : {
2844 0 : appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2845 : }
2846 :
2847 0 : appendPQExpBufferChar(&buf, ')');
2848 : }
2849 :
2850 72 : appendPQExpBuffer(&buf, " ON %s FROM %s",
2851 : PQgetvalue(result, i, 4),
2852 : PQgetvalue(result, i, 1));
2853 :
2854 : /* Show the stats target if it's not default */
2855 72 : if (!PQgetisnull(result, i, 8) &&
2856 6 : strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2857 6 : appendPQExpBuffer(&buf, "; STATISTICS %s",
2858 : PQgetvalue(result, i, 8));
2859 :
2860 72 : printTableAddFooter(&cont, buf.data);
2861 : }
2862 : }
2863 2980 : PQclear(result);
2864 : }
2865 0 : else if (pset.sversion >= 100000)
2866 : {
2867 0 : printfPQExpBuffer(&buf,
2868 : "SELECT oid, "
2869 : "stxrelid::pg_catalog.regclass, "
2870 : "stxnamespace::pg_catalog.regnamespace AS nsp, "
2871 : "stxname,\n"
2872 : " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')\n"
2873 : " FROM pg_catalog.unnest(stxkeys) s(attnum)\n"
2874 : " JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND\n"
2875 : " a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
2876 : " " CppAsString2(STATS_EXT_NDISTINCT) " = any(stxkind) AS ndist_enabled,\n"
2877 : " " CppAsString2(STATS_EXT_DEPENDENCIES) " = any(stxkind) AS deps_enabled,\n"
2878 : " " CppAsString2(STATS_EXT_MCV) " = any(stxkind) AS mcv_enabled,\n");
2879 :
2880 0 : if (pset.sversion >= 130000)
2881 0 : appendPQExpBufferStr(&buf, " stxstattarget\n");
2882 : else
2883 0 : appendPQExpBufferStr(&buf, " -1 AS stxstattarget\n");
2884 0 : appendPQExpBuffer(&buf, "FROM pg_catalog.pg_statistic_ext\n"
2885 : "WHERE stxrelid = '%s'\n"
2886 : "ORDER BY 1;",
2887 : oid);
2888 :
2889 0 : result = PSQLexec(buf.data);
2890 0 : if (!result)
2891 0 : goto error_return;
2892 : else
2893 0 : tuples = PQntuples(result);
2894 :
2895 0 : if (tuples > 0)
2896 : {
2897 0 : printTableAddFooter(&cont, _("Statistics objects:"));
2898 :
2899 0 : for (i = 0; i < tuples; i++)
2900 : {
2901 0 : bool gotone = false;
2902 :
2903 0 : printfPQExpBuffer(&buf, " ");
2904 :
2905 : /* statistics object name (qualified with namespace) */
2906 0 : appendPQExpBuffer(&buf, "\"%s.%s\" (",
2907 : PQgetvalue(result, i, 2),
2908 : PQgetvalue(result, i, 3));
2909 :
2910 : /* options */
2911 0 : if (strcmp(PQgetvalue(result, i, 5), "t") == 0)
2912 : {
2913 0 : appendPQExpBufferStr(&buf, "ndistinct");
2914 0 : gotone = true;
2915 : }
2916 :
2917 0 : if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
2918 : {
2919 0 : appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2920 0 : gotone = true;
2921 : }
2922 :
2923 0 : if (strcmp(PQgetvalue(result, i, 7), "t") == 0)
2924 : {
2925 0 : appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2926 : }
2927 :
2928 0 : appendPQExpBuffer(&buf, ") ON %s FROM %s",
2929 : PQgetvalue(result, i, 4),
2930 : PQgetvalue(result, i, 1));
2931 :
2932 : /* Show the stats target if it's not default */
2933 0 : if (strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2934 0 : appendPQExpBuffer(&buf, "; STATISTICS %s",
2935 : PQgetvalue(result, i, 8));
2936 :
2937 0 : printTableAddFooter(&cont, buf.data);
2938 : }
2939 : }
2940 0 : PQclear(result);
2941 : }
2942 :
2943 : /* print rules */
2944 2980 : if (tableinfo.hasrules && tableinfo.relkind != RELKIND_MATVIEW)
2945 : {
2946 36 : printfPQExpBuffer(&buf,
2947 : "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2948 : "ev_enabled\n"
2949 : "FROM pg_catalog.pg_rewrite r\n"
2950 : "WHERE r.ev_class = '%s' ORDER BY 1;",
2951 : oid);
2952 36 : result = PSQLexec(buf.data);
2953 36 : if (!result)
2954 0 : goto error_return;
2955 : else
2956 36 : tuples = PQntuples(result);
2957 :
2958 36 : if (tuples > 0)
2959 : {
2960 : bool have_heading;
2961 : int category;
2962 :
2963 180 : for (category = 0; category < 4; category++)
2964 : {
2965 144 : have_heading = false;
2966 :
2967 528 : for (i = 0; i < tuples; i++)
2968 : {
2969 : const char *ruledef;
2970 384 : bool list_rule = false;
2971 :
2972 384 : switch (category)
2973 : {
2974 96 : case 0:
2975 96 : if (*PQgetvalue(result, i, 2) == 'O')
2976 96 : list_rule = true;
2977 96 : break;
2978 96 : case 1:
2979 96 : if (*PQgetvalue(result, i, 2) == 'D')
2980 0 : list_rule = true;
2981 96 : break;
2982 96 : case 2:
2983 96 : if (*PQgetvalue(result, i, 2) == 'A')
2984 0 : list_rule = true;
2985 96 : break;
2986 96 : case 3:
2987 96 : if (*PQgetvalue(result, i, 2) == 'R')
2988 0 : list_rule = true;
2989 96 : break;
2990 : }
2991 384 : if (!list_rule)
2992 288 : continue;
2993 :
2994 96 : if (!have_heading)
2995 : {
2996 36 : switch (category)
2997 : {
2998 36 : case 0:
2999 36 : printfPQExpBuffer(&buf, _("Rules:"));
3000 36 : break;
3001 0 : case 1:
3002 0 : printfPQExpBuffer(&buf, _("Disabled rules:"));
3003 0 : break;
3004 0 : case 2:
3005 0 : printfPQExpBuffer(&buf, _("Rules firing always:"));
3006 0 : break;
3007 0 : case 3:
3008 0 : printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
3009 0 : break;
3010 : }
3011 36 : printTableAddFooter(&cont, buf.data);
3012 36 : have_heading = true;
3013 : }
3014 :
3015 : /* Everything after "CREATE RULE" is echoed verbatim */
3016 96 : ruledef = PQgetvalue(result, i, 1);
3017 96 : ruledef += 12;
3018 96 : printfPQExpBuffer(&buf, " %s", ruledef);
3019 96 : printTableAddFooter(&cont, buf.data);
3020 : }
3021 : }
3022 : }
3023 36 : PQclear(result);
3024 : }
3025 :
3026 : /* print any publications */
3027 2980 : if (pset.sversion >= 100000)
3028 : {
3029 2980 : if (pset.sversion >= 150000)
3030 : {
3031 2980 : printfPQExpBuffer(&buf,
3032 : "SELECT pubname\n"
3033 : " , NULL\n"
3034 : " , NULL\n"
3035 : "FROM pg_catalog.pg_publication p\n"
3036 : " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
3037 : " JOIN pg_catalog.pg_class pc ON pc.relnamespace = pn.pnnspid\n"
3038 : "WHERE pc.oid ='%s' and pg_catalog.pg_relation_is_publishable('%s')\n"
3039 : "UNION\n"
3040 : "SELECT pubname\n"
3041 : " , pg_get_expr(pr.prqual, c.oid)\n"
3042 : " , (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
3043 : " (SELECT string_agg(attname, ', ')\n"
3044 : " FROM pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
3045 : " pg_catalog.pg_attribute\n"
3046 : " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
3047 : " ELSE NULL END) "
3048 : "FROM pg_catalog.pg_publication p\n"
3049 : " JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3050 : " JOIN pg_catalog.pg_class c ON c.oid = pr.prrelid\n"
3051 : "WHERE pr.prrelid = '%s'\n"
3052 : "UNION\n"
3053 : "SELECT pubname\n"
3054 : " , NULL\n"
3055 : " , NULL\n"
3056 : "FROM pg_catalog.pg_publication p\n"
3057 : "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3058 : "ORDER BY 1;",
3059 : oid, oid, oid, oid);
3060 : }
3061 : else
3062 : {
3063 0 : printfPQExpBuffer(&buf,
3064 : "SELECT pubname\n"
3065 : " , NULL\n"
3066 : " , NULL\n"
3067 : "FROM pg_catalog.pg_publication p\n"
3068 : "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3069 : "WHERE pr.prrelid = '%s'\n"
3070 : "UNION ALL\n"
3071 : "SELECT pubname\n"
3072 : " , NULL\n"
3073 : " , NULL\n"
3074 : "FROM pg_catalog.pg_publication p\n"
3075 : "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3076 : "ORDER BY 1;",
3077 : oid, oid);
3078 : }
3079 :
3080 2980 : result = PSQLexec(buf.data);
3081 2980 : if (!result)
3082 0 : goto error_return;
3083 : else
3084 2980 : tuples = PQntuples(result);
3085 :
3086 2980 : if (tuples > 0)
3087 72 : printTableAddFooter(&cont, _("Publications:"));
3088 :
3089 : /* Might be an empty set - that's ok */
3090 3088 : for (i = 0; i < tuples; i++)
3091 : {
3092 108 : printfPQExpBuffer(&buf, " \"%s\"",
3093 : PQgetvalue(result, i, 0));
3094 :
3095 : /* column list (if any) */
3096 108 : if (!PQgetisnull(result, i, 2))
3097 24 : appendPQExpBuffer(&buf, " (%s)",
3098 : PQgetvalue(result, i, 2));
3099 :
3100 : /* row filter (if any) */
3101 108 : if (!PQgetisnull(result, i, 1))
3102 24 : appendPQExpBuffer(&buf, " WHERE %s",
3103 : PQgetvalue(result, i, 1));
3104 :
3105 108 : printTableAddFooter(&cont, buf.data);
3106 : }
3107 2980 : PQclear(result);
3108 : }
3109 :
3110 : /*
3111 : * If verbose, print NOT NULL constraints.
3112 : */
3113 2980 : if (verbose)
3114 : {
3115 1296 : printfPQExpBuffer(&buf,
3116 : "SELECT c.conname, a.attname, c.connoinherit,\n"
3117 : " c.conislocal, c.coninhcount <> 0\n"
3118 : "FROM pg_catalog.pg_constraint c JOIN\n"
3119 : " pg_catalog.pg_attribute a ON\n"
3120 : " (a.attrelid = c.conrelid AND a.attnum = c.conkey[1])\n"
3121 : "WHERE c.contype = " CppAsString2(CONSTRAINT_NOTNULL) " AND\n"
3122 : " c.conrelid = '%s'::pg_catalog.regclass\n"
3123 : "ORDER BY a.attnum",
3124 : oid);
3125 :
3126 1296 : result = PSQLexec(buf.data);
3127 1296 : if (!result)
3128 0 : goto error_return;
3129 : else
3130 1296 : tuples = PQntuples(result);
3131 :
3132 1296 : if (tuples > 0)
3133 764 : printTableAddFooter(&cont, _("Not-null constraints:"));
3134 :
3135 : /* Might be an empty set - that's ok */
3136 2272 : for (i = 0; i < tuples; i++)
3137 : {
3138 976 : bool islocal = PQgetvalue(result, i, 3)[0] == 't';
3139 976 : bool inherited = PQgetvalue(result, i, 4)[0] == 't';
3140 :
3141 976 : printfPQExpBuffer(&buf, " \"%s\" NOT NULL \"%s\"%s",
3142 : PQgetvalue(result, i, 0),
3143 : PQgetvalue(result, i, 1),
3144 976 : PQgetvalue(result, i, 2)[0] == 't' ?
3145 : " NO INHERIT" :
3146 1790 : islocal && inherited ? _(" (local, inherited)") :
3147 832 : inherited ? _(" (inherited)") : "");
3148 :
3149 976 : printTableAddFooter(&cont, buf.data);
3150 : }
3151 1296 : PQclear(result);
3152 : }
3153 : }
3154 :
3155 : /* Get view_def if table is a view or materialized view */
3156 3816 : if ((tableinfo.relkind == RELKIND_VIEW ||
3157 3816 : tableinfo.relkind == RELKIND_MATVIEW) && verbose)
3158 : {
3159 : PGresult *result;
3160 :
3161 412 : printfPQExpBuffer(&buf,
3162 : "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
3163 : oid);
3164 412 : result = PSQLexec(buf.data);
3165 412 : if (!result)
3166 0 : goto error_return;
3167 :
3168 412 : if (PQntuples(result) > 0)
3169 412 : view_def = pg_strdup(PQgetvalue(result, 0, 0));
3170 :
3171 412 : PQclear(result);
3172 : }
3173 :
3174 3816 : if (view_def)
3175 : {
3176 412 : PGresult *result = NULL;
3177 :
3178 : /* Footer information about a view */
3179 412 : printTableAddFooter(&cont, _("View definition:"));
3180 412 : printTableAddFooter(&cont, view_def);
3181 :
3182 : /* print rules */
3183 412 : if (tableinfo.hasrules)
3184 : {
3185 412 : 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 412 : result = PSQLexec(buf.data);
3191 412 : if (!result)
3192 0 : goto error_return;
3193 :
3194 412 : if (PQntuples(result) > 0)
3195 : {
3196 12 : printTableAddFooter(&cont, _("Rules:"));
3197 24 : for (i = 0; i < PQntuples(result); i++)
3198 : {
3199 : const char *ruledef;
3200 :
3201 : /* Everything after "CREATE RULE" is echoed verbatim */
3202 12 : ruledef = PQgetvalue(result, i, 1);
3203 12 : ruledef += 12;
3204 :
3205 12 : printfPQExpBuffer(&buf, " %s", ruledef);
3206 12 : printTableAddFooter(&cont, buf.data);
3207 : }
3208 : }
3209 412 : 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 3816 : if (tableinfo.hastriggers)
3218 : {
3219 : PGresult *result;
3220 : int tuples;
3221 :
3222 290 : 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 290 : if (pset.sversion >= 130000)
3242 290 : 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 0 : appendPQExpBufferStr(&buf, " NULL AS parent\n");
3253 :
3254 290 : 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 290 : if (pset.sversion >= 110000 && pset.sversion < 150000)
3267 0 : 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 290 : appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
3273 290 : appendPQExpBufferStr(&buf, "\nORDER BY 1;");
3274 :
3275 290 : result = PSQLexec(buf.data);
3276 290 : if (!result)
3277 0 : goto error_return;
3278 : else
3279 290 : tuples = PQntuples(result);
3280 :
3281 290 : 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 216 : for (category = 0; category <= 4; category++)
3292 : {
3293 180 : have_heading = false;
3294 810 : 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 630 : tgenabled = PQgetvalue(result, i, 2);
3306 630 : tgisinternal = PQgetvalue(result, i, 3);
3307 630 : list_trigger = false;
3308 630 : switch (category)
3309 : {
3310 126 : case 0:
3311 126 : if (*tgenabled == 'O' || *tgenabled == 't')
3312 126 : list_trigger = true;
3313 126 : break;
3314 126 : case 1:
3315 126 : if ((*tgenabled == 'D' || *tgenabled == 'f') &&
3316 0 : *tgisinternal == 'f')
3317 0 : list_trigger = true;
3318 126 : break;
3319 126 : case 2:
3320 126 : if ((*tgenabled == 'D' || *tgenabled == 'f') &&
3321 0 : *tgisinternal == 't')
3322 0 : list_trigger = true;
3323 126 : break;
3324 126 : case 3:
3325 126 : if (*tgenabled == 'A')
3326 0 : list_trigger = true;
3327 126 : break;
3328 126 : case 4:
3329 126 : if (*tgenabled == 'R')
3330 0 : list_trigger = true;
3331 126 : break;
3332 : }
3333 630 : if (list_trigger == false)
3334 504 : continue;
3335 :
3336 : /* Print the category heading once */
3337 126 : if (have_heading == false)
3338 : {
3339 36 : switch (category)
3340 : {
3341 36 : case 0:
3342 36 : printfPQExpBuffer(&buf, _("Triggers:"));
3343 36 : break;
3344 0 : case 1:
3345 0 : printfPQExpBuffer(&buf, _("Disabled user triggers:"));
3346 0 : break;
3347 0 : case 2:
3348 0 : printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
3349 0 : break;
3350 0 : case 3:
3351 0 : printfPQExpBuffer(&buf, _("Triggers firing always:"));
3352 0 : break;
3353 0 : case 4:
3354 0 : printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
3355 0 : break;
3356 : }
3357 36 : printTableAddFooter(&cont, buf.data);
3358 36 : have_heading = true;
3359 : }
3360 :
3361 : /* Everything after "TRIGGER" is echoed verbatim */
3362 126 : tgdef = PQgetvalue(result, i, 1);
3363 126 : usingpos = strstr(tgdef, " TRIGGER ");
3364 126 : if (usingpos)
3365 126 : tgdef = usingpos + 9;
3366 :
3367 126 : printfPQExpBuffer(&buf, " %s", tgdef);
3368 :
3369 : /* Visually distinguish inherited triggers */
3370 126 : if (!PQgetisnull(result, i, 4))
3371 12 : appendPQExpBuffer(&buf, ", ON TABLE %s",
3372 : PQgetvalue(result, i, 4));
3373 :
3374 126 : printTableAddFooter(&cont, buf.data);
3375 : }
3376 : }
3377 : }
3378 290 : PQclear(result);
3379 : }
3380 :
3381 : /*
3382 : * Finish printing the footer information about a table.
3383 : */
3384 3816 : if (tableinfo.relkind == RELKIND_RELATION ||
3385 1378 : tableinfo.relkind == RELKIND_MATVIEW ||
3386 1318 : tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
3387 1120 : tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3388 842 : tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
3389 710 : tableinfo.relkind == RELKIND_TOASTVALUE)
3390 : {
3391 : bool is_partitioned;
3392 : PGresult *result;
3393 : int tuples;
3394 :
3395 : /* simplify some repeated tests below */
3396 5946 : is_partitioned = (tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3397 2834 : tableinfo.relkind == RELKIND_PARTITIONED_INDEX);
3398 :
3399 : /* print foreign server name */
3400 3112 : if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
3401 : {
3402 : char *ftoptions;
3403 :
3404 : /* Footer information about foreign table */
3405 198 : 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 198 : result = PSQLexec(buf.data);
3416 198 : if (!result)
3417 0 : goto error_return;
3418 198 : else if (PQntuples(result) != 1)
3419 : {
3420 0 : PQclear(result);
3421 0 : goto error_return;
3422 : }
3423 :
3424 : /* Print server name */
3425 198 : printfPQExpBuffer(&buf, _("Server: %s"),
3426 : PQgetvalue(result, 0, 0));
3427 198 : printTableAddFooter(&cont, buf.data);
3428 :
3429 : /* Print per-table FDW options, if any */
3430 198 : ftoptions = PQgetvalue(result, 0, 1);
3431 198 : if (ftoptions && ftoptions[0] != '\0')
3432 : {
3433 174 : printfPQExpBuffer(&buf, _("FDW options: (%s)"), ftoptions);
3434 174 : printTableAddFooter(&cont, buf.data);
3435 : }
3436 198 : PQclear(result);
3437 : }
3438 :
3439 : /* print tables inherited from (exclude partitioned parents) */
3440 3112 : 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 3112 : result = PSQLexec(buf.data);
3450 3112 : if (!result)
3451 0 : goto error_return;
3452 : else
3453 : {
3454 3112 : const char *s = _("Inherits");
3455 3112 : int sw = pg_wcswidth(s, strlen(s), pset.encoding);
3456 :
3457 3112 : tuples = PQntuples(result);
3458 :
3459 3808 : for (i = 0; i < tuples; i++)
3460 : {
3461 696 : if (i == 0)
3462 546 : printfPQExpBuffer(&buf, "%s: %s",
3463 : s, PQgetvalue(result, i, 0));
3464 : else
3465 150 : printfPQExpBuffer(&buf, "%*s %s",
3466 : sw, "", PQgetvalue(result, i, 0));
3467 696 : if (i < tuples - 1)
3468 150 : appendPQExpBufferChar(&buf, ',');
3469 :
3470 696 : printTableAddFooter(&cont, buf.data);
3471 : }
3472 :
3473 3112 : PQclear(result);
3474 : }
3475 :
3476 : /* print child tables (with additional info if partitions) */
3477 3112 : if (pset.sversion >= 140000)
3478 3112 : 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 0 : else if (pset.sversion >= 100000)
3488 0 : 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 0 : 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 3112 : result = PSQLexec(buf.data);
3507 3112 : if (!result)
3508 0 : goto error_return;
3509 3112 : 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 3112 : if (is_partitioned && tuples == 0)
3518 : {
3519 66 : printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
3520 66 : printTableAddFooter(&cont, buf.data);
3521 : }
3522 3046 : else if (!verbose)
3523 : {
3524 : /* print the number of child tables, if any */
3525 1780 : if (tuples > 0)
3526 : {
3527 366 : if (is_partitioned)
3528 264 : printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
3529 : else
3530 102 : printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
3531 366 : printTableAddFooter(&cont, buf.data);
3532 : }
3533 : }
3534 : else
3535 : {
3536 : /* display the list of child tables */
3537 1266 : const char *ct = is_partitioned ? _("Partitions") : _("Child tables");
3538 1266 : int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
3539 :
3540 1774 : for (i = 0; i < tuples; i++)
3541 : {
3542 508 : char child_relkind = *PQgetvalue(result, i, 1);
3543 :
3544 508 : if (i == 0)
3545 308 : printfPQExpBuffer(&buf, "%s: %s",
3546 : ct, PQgetvalue(result, i, 0));
3547 : else
3548 200 : printfPQExpBuffer(&buf, "%*s %s",
3549 : ctw, "", PQgetvalue(result, i, 0));
3550 508 : if (!PQgetisnull(result, i, 3))
3551 196 : appendPQExpBuffer(&buf, " %s", PQgetvalue(result, i, 3));
3552 508 : if (child_relkind == RELKIND_PARTITIONED_TABLE ||
3553 : child_relkind == RELKIND_PARTITIONED_INDEX)
3554 24 : appendPQExpBufferStr(&buf, ", PARTITIONED");
3555 484 : else if (child_relkind == RELKIND_FOREIGN_TABLE)
3556 108 : appendPQExpBufferStr(&buf, ", FOREIGN");
3557 508 : if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
3558 0 : appendPQExpBufferStr(&buf, " (DETACH PENDING)");
3559 508 : if (i < tuples - 1)
3560 200 : appendPQExpBufferChar(&buf, ',');
3561 :
3562 508 : printTableAddFooter(&cont, buf.data);
3563 : }
3564 : }
3565 3112 : PQclear(result);
3566 :
3567 : /* Table type */
3568 3112 : if (tableinfo.reloftype)
3569 : {
3570 60 : printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
3571 60 : printTableAddFooter(&cont, buf.data);
3572 : }
3573 :
3574 3112 : if (verbose &&
3575 1302 : (tableinfo.relkind == RELKIND_RELATION ||
3576 326 : 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 1036 : tableinfo.relreplident != REPLICA_IDENTITY_INDEX &&
3583 1030 : ((strcmp(schemaname, "pg_catalog") != 0 &&
3584 1030 : tableinfo.relreplident != REPLICA_IDENTITY_DEFAULT) ||
3585 1024 : (strcmp(schemaname, "pg_catalog") == 0 &&
3586 0 : tableinfo.relreplident != REPLICA_IDENTITY_NOTHING)))
3587 : {
3588 6 : const char *s = _("Replica Identity");
3589 :
3590 6 : printfPQExpBuffer(&buf, "%s: %s",
3591 : s,
3592 6 : tableinfo.relreplident == REPLICA_IDENTITY_FULL ? "FULL" :
3593 0 : tableinfo.relreplident == REPLICA_IDENTITY_DEFAULT ? "NOTHING" :
3594 : "???");
3595 :
3596 6 : printTableAddFooter(&cont, buf.data);
3597 : }
3598 :
3599 : /* OIDs, if verbose and not a materialized view */
3600 3112 : if (verbose && tableinfo.relkind != RELKIND_MATVIEW && tableinfo.hasoids)
3601 0 : printTableAddFooter(&cont, _("Has OIDs: yes"));
3602 :
3603 : /* Tablespace info */
3604 3112 : add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
3605 : true);
3606 :
3607 : /* Access method info */
3608 3112 : if (verbose && tableinfo.relam != NULL && !pset.hide_tableam)
3609 : {
3610 12 : printfPQExpBuffer(&buf, _("Access method: %s"), tableinfo.relam);
3611 12 : printTableAddFooter(&cont, buf.data);
3612 : }
3613 : }
3614 :
3615 : /* reloptions, if verbose */
3616 3816 : if (verbose &&
3617 1688 : tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
3618 : {
3619 34 : const char *t = _("Options");
3620 :
3621 34 : printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
3622 34 : printTableAddFooter(&cont, buf.data);
3623 : }
3624 :
3625 3816 : printTable(&cont, pset.queryFout, false, pset.logfile);
3626 :
3627 3816 : retval = true;
3628 :
3629 4008 : error_return:
3630 :
3631 : /* clean up */
3632 4008 : if (printTableInitialized)
3633 3816 : printTableCleanup(&cont);
3634 4008 : termPQExpBuffer(&buf);
3635 4008 : termPQExpBuffer(&title);
3636 4008 : termPQExpBuffer(&tmpbuf);
3637 :
3638 4008 : free(view_def);
3639 :
3640 4008 : PQclear(res);
3641 :
3642 4008 : 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 4856 : add_tablespace_footer(printTableContent *const cont, char relkind,
3652 : Oid tablespace, const bool newline)
3653 : {
3654 : /* relkinds for which we support tablespaces */
3655 4856 : if (relkind == RELKIND_RELATION ||
3656 2358 : relkind == RELKIND_MATVIEW ||
3657 614 : relkind == RELKIND_INDEX ||
3658 336 : relkind == RELKIND_PARTITIONED_TABLE ||
3659 204 : 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 4658 : if (tablespace != 0)
3667 : {
3668 204 : PGresult *result = NULL;
3669 : PQExpBufferData buf;
3670 :
3671 204 : initPQExpBuffer(&buf);
3672 204 : printfPQExpBuffer(&buf,
3673 : "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3674 : "WHERE oid = '%u';", tablespace);
3675 204 : result = PSQLexec(buf.data);
3676 204 : if (!result)
3677 : {
3678 0 : termPQExpBuffer(&buf);
3679 0 : return;
3680 : }
3681 : /* Should always be the case, but.... */
3682 204 : if (PQntuples(result) > 0)
3683 : {
3684 204 : if (newline)
3685 : {
3686 : /* Add the tablespace as a new footer */
3687 174 : printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3688 : PQgetvalue(result, 0, 0));
3689 174 : printTableAddFooter(cont, buf.data);
3690 : }
3691 : else
3692 : {
3693 : /* Append the tablespace to the latest footer */
3694 30 : 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 30 : appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3700 : PQgetvalue(result, 0, 0));
3701 30 : printTableSetFooter(cont, buf.data);
3702 : }
3703 : }
3704 204 : PQclear(result);
3705 204 : 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 30 : describeRoles(const char *pattern, bool verbose, bool showSystem)
3717 : {
3718 : PQExpBufferData buf;
3719 : PGresult *res;
3720 : printTableContent cont;
3721 30 : printTableOpt myopt = pset.popt.topt;
3722 30 : int ncols = 2;
3723 30 : int nrows = 0;
3724 : int i;
3725 : int conns;
3726 30 : const char align = 'l';
3727 : char **attr;
3728 :
3729 30 : myopt.default_footer = false;
3730 :
3731 30 : initPQExpBuffer(&buf);
3732 :
3733 30 : 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 30 : if (verbose)
3739 : {
3740 0 : appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3741 0 : ncols++;
3742 : }
3743 30 : appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3744 :
3745 30 : if (pset.sversion >= 90500)
3746 : {
3747 30 : appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3748 : }
3749 :
3750 30 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3751 :
3752 30 : if (!showSystem && !pattern)
3753 0 : appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3754 :
3755 30 : if (!validateSQLNamePattern(&buf, pattern, false, false,
3756 : NULL, "r.rolname", NULL, NULL,
3757 : NULL, 1))
3758 : {
3759 18 : termPQExpBuffer(&buf);
3760 18 : return false;
3761 : }
3762 :
3763 12 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
3764 :
3765 12 : res = PSQLexec(buf.data);
3766 12 : if (!res)
3767 0 : return false;
3768 :
3769 12 : nrows = PQntuples(res);
3770 12 : attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3771 :
3772 12 : printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3773 :
3774 12 : printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3775 12 : printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3776 :
3777 12 : if (verbose)
3778 0 : printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3779 :
3780 30 : for (i = 0; i < nrows; i++)
3781 : {
3782 18 : printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3783 :
3784 18 : resetPQExpBuffer(&buf);
3785 18 : if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3786 0 : add_role_attribute(&buf, _("Superuser"));
3787 :
3788 18 : if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3789 0 : add_role_attribute(&buf, _("No inheritance"));
3790 :
3791 18 : if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3792 0 : add_role_attribute(&buf, _("Create role"));
3793 :
3794 18 : if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3795 0 : add_role_attribute(&buf, _("Create DB"));
3796 :
3797 18 : if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3798 18 : add_role_attribute(&buf, _("Cannot login"));
3799 :
3800 18 : if (strcmp(PQgetvalue(res, i, (verbose ? 9 : 8)), "t") == 0)
3801 0 : add_role_attribute(&buf, _("Replication"));
3802 :
3803 18 : if (pset.sversion >= 90500)
3804 18 : if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3805 0 : add_role_attribute(&buf, _("Bypass RLS"));
3806 :
3807 18 : conns = atoi(PQgetvalue(res, i, 6));
3808 18 : if (conns >= 0)
3809 : {
3810 0 : if (buf.len > 0)
3811 0 : appendPQExpBufferChar(&buf, '\n');
3812 :
3813 0 : if (conns == 0)
3814 0 : appendPQExpBufferStr(&buf, _("No connections"));
3815 : else
3816 0 : appendPQExpBuffer(&buf, ngettext("%d connection",
3817 : "%d connections",
3818 : conns),
3819 : conns);
3820 : }
3821 :
3822 18 : if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3823 : {
3824 0 : if (buf.len > 0)
3825 0 : appendPQExpBufferChar(&buf, '\n');
3826 0 : appendPQExpBufferStr(&buf, _("Password valid until "));
3827 0 : appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3828 : }
3829 :
3830 18 : attr[i] = pg_strdup(buf.data);
3831 :
3832 18 : printTableAddCell(&cont, attr[i], false, false);
3833 :
3834 18 : if (verbose)
3835 0 : printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3836 : }
3837 12 : termPQExpBuffer(&buf);
3838 :
3839 12 : printTable(&cont, pset.queryFout, false, pset.logfile);
3840 12 : printTableCleanup(&cont);
3841 :
3842 30 : for (i = 0; i < nrows; i++)
3843 18 : free(attr[i]);
3844 12 : free(attr);
3845 :
3846 12 : PQclear(res);
3847 12 : return true;
3848 : }
3849 :
3850 : static void
3851 18 : add_role_attribute(PQExpBuffer buf, const char *const str)
3852 : {
3853 18 : if (buf->len > 0)
3854 0 : appendPQExpBufferStr(buf, ", ");
3855 :
3856 18 : appendPQExpBufferStr(buf, str);
3857 18 : }
3858 :
3859 : /*
3860 : * \drds
3861 : */
3862 : bool
3863 26 : listDbRoleSettings(const char *pattern, const char *pattern2)
3864 : {
3865 : PQExpBufferData buf;
3866 : PGresult *res;
3867 26 : printQueryOpt myopt = pset.popt;
3868 : bool havewhere;
3869 :
3870 26 : initPQExpBuffer(&buf);
3871 :
3872 26 : 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 26 : if (!validateSQLNamePattern(&buf, pattern, false, false,
3881 : NULL, "r.rolname", NULL, NULL, &havewhere, 1))
3882 18 : goto error_return;
3883 8 : if (!validateSQLNamePattern(&buf, pattern2, havewhere, false,
3884 : NULL, "d.datname", NULL, NULL,
3885 : NULL, 1))
3886 0 : goto error_return;
3887 8 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3888 :
3889 8 : res = PSQLexec(buf.data);
3890 8 : termPQExpBuffer(&buf);
3891 8 : if (!res)
3892 0 : 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 8 : if (PQntuples(res) == 0 && !pset.quiet)
3901 : {
3902 2 : if (pattern && pattern2)
3903 0 : pg_log_error("Did not find any settings for role \"%s\" and database \"%s\".",
3904 : pattern, pattern2);
3905 2 : else if (pattern)
3906 0 : pg_log_error("Did not find any settings for role \"%s\".",
3907 : pattern);
3908 : else
3909 2 : pg_log_error("Did not find any settings.");
3910 : }
3911 : else
3912 : {
3913 6 : myopt.title = _("List of settings");
3914 6 : myopt.translate_header = true;
3915 :
3916 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3917 : }
3918 :
3919 8 : PQclear(res);
3920 8 : return true;
3921 :
3922 18 : error_return:
3923 18 : termPQExpBuffer(&buf);
3924 18 : return false;
3925 : }
3926 :
3927 : /*
3928 : * \drg
3929 : * Describes role grants.
3930 : */
3931 : bool
3932 6 : describeRoleGrants(const char *pattern, bool showSystem)
3933 : {
3934 : PQExpBufferData buf;
3935 : PGresult *res;
3936 6 : printQueryOpt myopt = pset.popt;
3937 :
3938 6 : initPQExpBuffer(&buf);
3939 6 : 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 6 : if (pset.sversion >= 160000)
3946 6 : 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 0 : 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 6 : appendPQExpBuffer(&buf,
3957 : " ) AS \"%s\",\n"
3958 : " g.rolname AS \"%s\"\n",
3959 : gettext_noop("Options"),
3960 : gettext_noop("Grantor"));
3961 :
3962 6 : 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 6 : if (!showSystem && !pattern)
3969 0 : appendPQExpBufferStr(&buf, "WHERE m.rolname !~ '^pg_'\n");
3970 :
3971 6 : if (!validateSQLNamePattern(&buf, pattern, false, false,
3972 : NULL, "m.rolname", NULL, NULL,
3973 : NULL, 1))
3974 : {
3975 0 : termPQExpBuffer(&buf);
3976 0 : return false;
3977 : }
3978 :
3979 6 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;\n");
3980 :
3981 6 : res = PSQLexec(buf.data);
3982 6 : termPQExpBuffer(&buf);
3983 6 : if (!res)
3984 0 : return false;
3985 :
3986 6 : myopt.title = _("List of role grants");
3987 6 : myopt.translate_header = true;
3988 :
3989 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3990 :
3991 6 : PQclear(res);
3992 6 : 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 312 : listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
4012 : {
4013 312 : bool showTables = strchr(tabtypes, 't') != NULL;
4014 312 : bool showIndexes = strchr(tabtypes, 'i') != NULL;
4015 312 : bool showViews = strchr(tabtypes, 'v') != NULL;
4016 312 : bool showMatViews = strchr(tabtypes, 'm') != NULL;
4017 312 : bool showSeq = strchr(tabtypes, 's') != NULL;
4018 312 : bool showForeign = strchr(tabtypes, 'E') != NULL;
4019 :
4020 : int ntypes;
4021 : PQExpBufferData buf;
4022 : PGresult *res;
4023 312 : printQueryOpt myopt = pset.popt;
4024 : int cols_so_far;
4025 312 : bool translate_columns[] = {false, false, true, false, false, false, false, false, false};
4026 :
4027 : /* Count the number of explicitly-requested relation types */
4028 312 : ntypes = showTables + showIndexes + showViews + showMatViews +
4029 312 : showSeq + showForeign;
4030 : /* If none, we default to \dtvmsE (but see also command.c) */
4031 312 : if (ntypes == 0)
4032 0 : showTables = showViews = showMatViews = showSeq = showForeign = true;
4033 :
4034 312 : initPQExpBuffer(&buf);
4035 :
4036 312 : 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 312 : cols_so_far = 4;
4065 :
4066 312 : if (showIndexes)
4067 : {
4068 42 : appendPQExpBuffer(&buf,
4069 : ",\n c2.relname as \"%s\"",
4070 : gettext_noop("Table"));
4071 42 : cols_so_far++;
4072 : }
4073 :
4074 312 : if (verbose)
4075 : {
4076 : /*
4077 : * Show whether a relation is permanent, temporary, or unlogged.
4078 : */
4079 36 : 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 36 : 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 36 : if (pset.sversion >= 120000 && !pset.hide_tableam &&
4101 12 : (showTables || showMatViews || showIndexes))
4102 18 : appendPQExpBuffer(&buf,
4103 : ",\n am.amname as \"%s\"",
4104 : gettext_noop("Access method"));
4105 :
4106 36 : 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 312 : 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 312 : if (pset.sversion >= 120000 && !pset.hide_tableam &&
4118 12 : (showTables || showMatViews || showIndexes))
4119 18 : appendPQExpBufferStr(&buf,
4120 : "\n LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam");
4121 :
4122 312 : if (showIndexes)
4123 42 : 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 312 : appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
4128 312 : if (showTables)
4129 : {
4130 114 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_RELATION) ","
4131 : CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
4132 : /* with 'S' or a pattern, allow 't' to match TOAST tables too */
4133 114 : if (showSystem || pattern)
4134 90 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_TOASTVALUE) ",");
4135 : }
4136 312 : if (showViews)
4137 66 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_VIEW) ",");
4138 312 : if (showMatViews)
4139 66 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_MATVIEW) ",");
4140 312 : if (showIndexes)
4141 42 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_INDEX) ","
4142 : CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
4143 312 : if (showSeq)
4144 60 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_SEQUENCE) ",");
4145 312 : if (showSystem || pattern)
4146 276 : appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL */
4147 312 : if (showForeign)
4148 36 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_FOREIGN_TABLE) ",");
4149 :
4150 312 : appendPQExpBufferStr(&buf, "''"); /* dummy */
4151 312 : appendPQExpBufferStr(&buf, ")\n");
4152 :
4153 312 : if (!showSystem && !pattern)
4154 36 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4155 : " AND n.nspname !~ '^pg_toast'\n"
4156 : " AND n.nspname <> 'information_schema'\n");
4157 :
4158 312 : 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 162 : termPQExpBuffer(&buf);
4164 162 : return false;
4165 : }
4166 :
4167 150 : appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
4168 :
4169 150 : res = PSQLexec(buf.data);
4170 150 : termPQExpBuffer(&buf);
4171 150 : if (!res)
4172 0 : 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 150 : if (PQntuples(res) == 0 && !pset.quiet)
4180 : {
4181 0 : if (pattern)
4182 : {
4183 0 : if (ntypes != 1)
4184 0 : pg_log_error("Did not find any relations named \"%s\".",
4185 : pattern);
4186 0 : else if (showTables)
4187 0 : pg_log_error("Did not find any tables named \"%s\".",
4188 : pattern);
4189 0 : else if (showIndexes)
4190 0 : pg_log_error("Did not find any indexes named \"%s\".",
4191 : pattern);
4192 0 : else if (showViews)
4193 0 : pg_log_error("Did not find any views named \"%s\".",
4194 : pattern);
4195 0 : else if (showMatViews)
4196 0 : pg_log_error("Did not find any materialized views named \"%s\".",
4197 : pattern);
4198 0 : else if (showSeq)
4199 0 : pg_log_error("Did not find any sequences named \"%s\".",
4200 : pattern);
4201 0 : else if (showForeign)
4202 0 : pg_log_error("Did not find any foreign tables named \"%s\".",
4203 : pattern);
4204 : else /* should not get here */
4205 0 : pg_log_error_internal("Did not find any ??? named \"%s\".",
4206 : pattern);
4207 : }
4208 : else
4209 : {
4210 0 : if (ntypes != 1)
4211 0 : pg_log_error("Did not find any relations.");
4212 0 : else if (showTables)
4213 0 : pg_log_error("Did not find any tables.");
4214 0 : else if (showIndexes)
4215 0 : pg_log_error("Did not find any indexes.");
4216 0 : else if (showViews)
4217 0 : pg_log_error("Did not find any views.");
4218 0 : else if (showMatViews)
4219 0 : pg_log_error("Did not find any materialized views.");
4220 0 : else if (showSeq)
4221 0 : pg_log_error("Did not find any sequences.");
4222 0 : else if (showForeign)
4223 0 : pg_log_error("Did not find any foreign tables.");
4224 : else /* should not get here */
4225 0 : pg_log_error_internal("Did not find any ??? relations.");
4226 : }
4227 : }
4228 : else
4229 : {
4230 150 : myopt.title =
4231 282 : (ntypes != 1) ? _("List of relations") :
4232 216 : (showTables) ? _("List of tables") :
4233 150 : (showIndexes) ? _("List of indexes") :
4234 108 : (showViews) ? _("List of views") :
4235 60 : (showMatViews) ? _("List of materialized views") :
4236 18 : (showSeq) ? _("List of sequences") :
4237 0 : (showForeign) ? _("List of foreign tables") :
4238 : "List of ???"; /* should not get here */
4239 150 : myopt.translate_header = true;
4240 150 : myopt.translate_columns = translate_columns;
4241 150 : myopt.n_translate_columns = lengthof(translate_columns);
4242 :
4243 150 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4244 : }
4245 :
4246 150 : PQclear(res);
4247 150 : 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 108 : listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
4267 : {
4268 108 : bool showTables = strchr(reltypes, 't') != NULL;
4269 108 : bool showIndexes = strchr(reltypes, 'i') != NULL;
4270 108 : bool showNested = strchr(reltypes, 'n') != NULL;
4271 : PQExpBufferData buf;
4272 : PQExpBufferData title;
4273 : PGresult *res;
4274 108 : printQueryOpt myopt = pset.popt;
4275 108 : bool translate_columns[] = {false, false, false, false, false, false, false, false, false, false};
4276 : const char *tabletitle;
4277 108 : bool mixed_output = false;
4278 :
4279 : /*
4280 : * Note: Declarative table partitioning is only supported as of Pg 10.0.
4281 : */
4282 108 : if (pset.sversion < 100000)
4283 : {
4284 : char sverbuf[32];
4285 :
4286 0 : pg_log_error("The server (version %s) does not support declarative table partitioning.",
4287 : formatPGVersionNumber(pset.sversion, false,
4288 : sverbuf, sizeof(sverbuf)));
4289 0 : return true;
4290 : }
4291 :
4292 : /* If no relation kind was selected, show them all */
4293 108 : if (!showTables && !showIndexes)
4294 72 : showTables = showIndexes = true;
4295 :
4296 108 : if (showIndexes && !showTables)
4297 18 : tabletitle = _("List of partitioned indexes"); /* \dPi */
4298 90 : else if (showTables && !showIndexes)
4299 18 : tabletitle = _("List of partitioned tables"); /* \dPt */
4300 : else
4301 : {
4302 : /* show all kinds */
4303 72 : tabletitle = _("List of partitioned relations");
4304 72 : mixed_output = true;
4305 : }
4306 :
4307 108 : initPQExpBuffer(&buf);
4308 :
4309 108 : 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 108 : if (mixed_output)
4318 : {
4319 72 : 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 72 : translate_columns[3] = true;
4329 : }
4330 :
4331 108 : if (showNested || pattern)
4332 90 : appendPQExpBuffer(&buf,
4333 : ",\n inh.inhparent::pg_catalog.regclass as \"%s\"",
4334 : gettext_noop("Parent name"));
4335 :
4336 108 : if (showIndexes)
4337 90 : appendPQExpBuffer(&buf,
4338 : ",\n c2.oid::pg_catalog.regclass as \"%s\"",
4339 : gettext_noop("Table"));
4340 :
4341 108 : if (verbose)
4342 : {
4343 : /*
4344 : * Table access methods were introduced in v12, and can be set on
4345 : * partitioned tables since v17.
4346 : */
4347 0 : appendPQExpBuffer(&buf, ",\n am.amname as \"%s\"",
4348 : gettext_noop("Access method"));
4349 :
4350 0 : if (showNested)
4351 : {
4352 0 : appendPQExpBuffer(&buf,
4353 : ",\n s.dps as \"%s\"",
4354 : gettext_noop("Leaf partition size"));
4355 0 : 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 0 : appendPQExpBuffer(&buf,
4362 : ",\n s.tps as \"%s\"",
4363 : gettext_noop("Total size"));
4364 :
4365 0 : appendPQExpBuffer(&buf,
4366 : ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
4367 : gettext_noop("Description"));
4368 : }
4369 :
4370 108 : 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 108 : if (showIndexes)
4375 90 : 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 108 : if (showNested || pattern)
4380 90 : appendPQExpBufferStr(&buf,
4381 : "\n LEFT JOIN pg_catalog.pg_inherits inh ON c.oid = inh.inhrelid");
4382 :
4383 108 : if (verbose)
4384 : {
4385 0 : appendPQExpBufferStr(&buf,
4386 : "\n LEFT JOIN pg_catalog.pg_am am ON c.relam = am.oid");
4387 :
4388 0 : if (pset.sversion < 120000)
4389 : {
4390 0 : 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 0 : 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 108 : appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
4421 108 : if (showTables)
4422 90 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
4423 108 : if (showIndexes)
4424 90 : appendPQExpBufferStr(&buf, CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
4425 108 : appendPQExpBufferStr(&buf, "''"); /* dummy */
4426 108 : appendPQExpBufferStr(&buf, ")\n");
4427 :
4428 108 : appendPQExpBufferStr(&buf, !showNested && !pattern ?
4429 : " AND NOT c.relispartition\n" : "");
4430 :
4431 108 : if (!pattern)
4432 36 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4433 : " AND n.nspname !~ '^pg_toast'\n"
4434 : " AND n.nspname <> 'information_schema'\n");
4435 :
4436 108 : 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 24 : termPQExpBuffer(&buf);
4442 24 : return false;
4443 : }
4444 :
4445 144 : appendPQExpBuffer(&buf, "ORDER BY \"Schema\", %s%s\"Name\";",
4446 : mixed_output ? "\"Type\" DESC, " : "",
4447 60 : showNested || pattern ? "\"Parent name\" NULLS FIRST, " : "");
4448 :
4449 84 : res = PSQLexec(buf.data);
4450 84 : termPQExpBuffer(&buf);
4451 84 : if (!res)
4452 0 : return false;
4453 :
4454 84 : initPQExpBuffer(&title);
4455 84 : appendPQExpBufferStr(&title, tabletitle);
4456 :
4457 84 : myopt.title = title.data;
4458 84 : myopt.translate_header = true;
4459 84 : myopt.translate_columns = translate_columns;
4460 84 : myopt.n_translate_columns = lengthof(translate_columns);
4461 :
4462 84 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4463 :
4464 84 : termPQExpBuffer(&title);
4465 :
4466 84 : PQclear(res);
4467 84 : return true;
4468 : }
4469 :
4470 : /*
4471 : * \dL
4472 : *
4473 : * Describes languages.
4474 : */
4475 : bool
4476 30 : listLanguages(const char *pattern, bool verbose, bool showSystem)
4477 : {
4478 : PQExpBufferData buf;
4479 : PGresult *res;
4480 30 : printQueryOpt myopt = pset.popt;
4481 :
4482 30 : initPQExpBuffer(&buf);
4483 :
4484 30 : 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 30 : if (verbose)
4493 : {
4494 0 : 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 0 : printACLColumn(&buf, "l.lanacl");
4504 : }
4505 :
4506 30 : 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 30 : if (pattern)
4515 : {
4516 30 : if (!validateSQLNamePattern(&buf, pattern, false, false,
4517 : NULL, "l.lanname", NULL, NULL,
4518 : NULL, 2))
4519 : {
4520 24 : termPQExpBuffer(&buf);
4521 24 : return false;
4522 : }
4523 : }
4524 :
4525 6 : if (!showSystem && !pattern)
4526 0 : appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
4527 :
4528 :
4529 6 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
4530 :
4531 6 : res = PSQLexec(buf.data);
4532 6 : termPQExpBuffer(&buf);
4533 6 : if (!res)
4534 0 : return false;
4535 :
4536 6 : myopt.title = _("List of languages");
4537 6 : myopt.translate_header = true;
4538 :
4539 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4540 :
4541 6 : PQclear(res);
4542 6 : return true;
4543 : }
4544 :
4545 :
4546 : /*
4547 : * \dD
4548 : *
4549 : * Describes domains.
4550 : */
4551 : bool
4552 60 : listDomains(const char *pattern, bool verbose, bool showSystem)
4553 : {
4554 : PQExpBufferData buf;
4555 : PGresult *res;
4556 60 : printQueryOpt myopt = pset.popt;
4557 :
4558 60 : initPQExpBuffer(&buf);
4559 :
4560 60 : 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 60 : if (verbose)
4580 : {
4581 6 : appendPQExpBufferStr(&buf, ",\n ");
4582 6 : printACLColumn(&buf, "t.typacl");
4583 6 : appendPQExpBuffer(&buf,
4584 : ",\n d.description as \"%s\"",
4585 : gettext_noop("Description"));
4586 : }
4587 :
4588 60 : 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 60 : if (verbose)
4593 6 : 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 60 : appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
4599 :
4600 60 : if (!showSystem && !pattern)
4601 0 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4602 : " AND n.nspname <> 'information_schema'\n");
4603 :
4604 60 : 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 24 : termPQExpBuffer(&buf);
4610 24 : return false;
4611 : }
4612 :
4613 36 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4614 :
4615 36 : res = PSQLexec(buf.data);
4616 36 : termPQExpBuffer(&buf);
4617 36 : if (!res)
4618 0 : return false;
4619 :
4620 36 : myopt.title = _("List of domains");
4621 36 : myopt.translate_header = true;
4622 :
4623 36 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4624 :
4625 36 : PQclear(res);
4626 36 : return true;
4627 : }
4628 :
4629 : /*
4630 : * \dc
4631 : *
4632 : * Describes conversions.
4633 : */
4634 : bool
4635 42 : listConversions(const char *pattern, bool verbose, bool showSystem)
4636 : {
4637 : PQExpBufferData buf;
4638 : PGresult *res;
4639 42 : printQueryOpt myopt = pset.popt;
4640 : static const bool translate_columns[] =
4641 : {false, false, false, false, true, false};
4642 :
4643 42 : initPQExpBuffer(&buf);
4644 :
4645 42 : 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 42 : if (verbose)
4660 0 : appendPQExpBuffer(&buf,
4661 : ",\n d.description AS \"%s\"",
4662 : gettext_noop("Description"));
4663 :
4664 42 : 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 42 : if (verbose)
4670 0 : 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 42 : appendPQExpBufferStr(&buf, "WHERE true\n");
4677 :
4678 42 : if (!showSystem && !pattern)
4679 0 : appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4680 : " AND n.nspname <> 'information_schema'\n");
4681 :
4682 42 : 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 24 : termPQExpBuffer(&buf);
4688 24 : return false;
4689 : }
4690 :
4691 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4692 :
4693 18 : res = PSQLexec(buf.data);
4694 18 : termPQExpBuffer(&buf);
4695 18 : if (!res)
4696 0 : return false;
4697 :
4698 18 : myopt.title = _("List of conversions");
4699 18 : myopt.translate_header = true;
4700 18 : myopt.translate_columns = translate_columns;
4701 18 : myopt.n_translate_columns = lengthof(translate_columns);
4702 :
4703 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4704 :
4705 18 : PQclear(res);
4706 18 : return true;
4707 : }
4708 :
4709 : /*
4710 : * \dconfig
4711 : *
4712 : * Describes configuration parameters.
4713 : */
4714 : bool
4715 12 : describeConfigurationParameters(const char *pattern, bool verbose,
4716 : bool showSystem)
4717 : {
4718 : PQExpBufferData buf;
4719 : PGresult *res;
4720 12 : printQueryOpt myopt = pset.popt;
4721 :
4722 12 : initPQExpBuffer(&buf);
4723 12 : 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 12 : if (verbose)
4730 : {
4731 6 : appendPQExpBuffer(&buf,
4732 : ", s.vartype AS \"%s\", s.context AS \"%s\", ",
4733 : gettext_noop("Type"),
4734 : gettext_noop("Context"));
4735 6 : if (pset.sversion >= 150000)
4736 6 : printACLColumn(&buf, "p.paracl");
4737 : else
4738 0 : appendPQExpBuffer(&buf, "NULL AS \"%s\"",
4739 : gettext_noop("Access privileges"));
4740 : }
4741 :
4742 12 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_settings s\n");
4743 :
4744 12 : if (verbose && pset.sversion >= 150000)
4745 6 : appendPQExpBufferStr(&buf,
4746 : " LEFT JOIN pg_catalog.pg_parameter_acl p\n"
4747 : " ON pg_catalog.lower(s.name) = p.parname\n");
4748 :
4749 12 : if (pattern)
4750 12 : processSQLNamePattern(pset.db, &buf, pattern,
4751 : false, false,
4752 : NULL, "pg_catalog.lower(s.name)", NULL,
4753 : NULL, NULL, NULL);
4754 : else
4755 0 : appendPQExpBufferStr(&buf, "WHERE s.source <> 'default' AND\n"
4756 : " s.setting IS DISTINCT FROM s.boot_val\n");
4757 :
4758 12 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
4759 :
4760 12 : res = PSQLexec(buf.data);
4761 12 : termPQExpBuffer(&buf);
4762 12 : if (!res)
4763 0 : return false;
4764 :
4765 12 : if (pattern)
4766 12 : myopt.title = _("List of configuration parameters");
4767 : else
4768 0 : myopt.title = _("List of non-default configuration parameters");
4769 12 : myopt.translate_header = true;
4770 :
4771 12 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4772 :
4773 12 : PQclear(res);
4774 12 : return true;
4775 : }
4776 :
4777 : /*
4778 : * \dy
4779 : *
4780 : * Describes Event Triggers.
4781 : */
4782 : bool
4783 24 : listEventTriggers(const char *pattern, bool verbose)
4784 : {
4785 : PQExpBufferData buf;
4786 : PGresult *res;
4787 24 : printQueryOpt myopt = pset.popt;
4788 : static const bool translate_columns[] =
4789 : {false, false, false, true, false, false, false};
4790 :
4791 24 : if (pset.sversion < 90300)
4792 : {
4793 : char sverbuf[32];
4794 :
4795 0 : pg_log_error("The server (version %s) does not support event triggers.",
4796 : formatPGVersionNumber(pset.sversion, false,
4797 : sverbuf, sizeof(sverbuf)));
4798 0 : return true;
4799 : }
4800 :
4801 24 : initPQExpBuffer(&buf);
4802 :
4803 24 : 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 24 : if (verbose)
4825 0 : appendPQExpBuffer(&buf,
4826 : ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
4827 : gettext_noop("Description"));
4828 24 : appendPQExpBufferStr(&buf,
4829 : "\nFROM pg_catalog.pg_event_trigger e ");
4830 :
4831 24 : if (!validateSQLNamePattern(&buf, pattern, false, false,
4832 : NULL, "evtname", NULL, NULL,
4833 : NULL, 1))
4834 : {
4835 18 : termPQExpBuffer(&buf);
4836 18 : return false;
4837 : }
4838 :
4839 6 : appendPQExpBufferStr(&buf, "ORDER BY 1");
4840 :
4841 6 : res = PSQLexec(buf.data);
4842 6 : termPQExpBuffer(&buf);
4843 6 : if (!res)
4844 0 : return false;
4845 :
4846 6 : myopt.title = _("List of event triggers");
4847 6 : myopt.translate_header = true;
4848 6 : myopt.translate_columns = translate_columns;
4849 6 : myopt.n_translate_columns = lengthof(translate_columns);
4850 :
4851 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4852 :
4853 6 : PQclear(res);
4854 6 : return true;
4855 : }
4856 :
4857 : /*
4858 : * \dX
4859 : *
4860 : * Describes extended statistics.
4861 : */
4862 : bool
4863 102 : listExtendedStats(const char *pattern)
4864 : {
4865 : PQExpBufferData buf;
4866 : PGresult *res;
4867 102 : printQueryOpt myopt = pset.popt;
4868 :
4869 102 : if (pset.sversion < 100000)
4870 : {
4871 : char sverbuf[32];
4872 :
4873 0 : pg_log_error("The server (version %s) does not support extended statistics.",
4874 : formatPGVersionNumber(pset.sversion, false,
4875 : sverbuf, sizeof(sverbuf)));
4876 0 : return true;
4877 : }
4878 :
4879 102 : initPQExpBuffer(&buf);
4880 102 : 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 102 : if (pset.sversion >= 140000)
4888 102 : 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 0 : 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 102 : 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 102 : if (pset.sversion >= 120000)
4917 : {
4918 102 : 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 102 : appendPQExpBufferStr(&buf,
4925 : " \nFROM pg_catalog.pg_statistic_ext es \n");
4926 :
4927 102 : 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 24 : termPQExpBuffer(&buf);
4934 24 : return false;
4935 : }
4936 :
4937 78 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4938 :
4939 78 : res = PSQLexec(buf.data);
4940 78 : termPQExpBuffer(&buf);
4941 78 : if (!res)
4942 0 : return false;
4943 :
4944 78 : myopt.title = _("List of extended statistics");
4945 78 : myopt.translate_header = true;
4946 :
4947 78 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4948 :
4949 78 : PQclear(res);
4950 78 : return true;
4951 : }
4952 :
4953 : /*
4954 : * \dC
4955 : *
4956 : * Describes casts.
4957 : */
4958 : bool
4959 42 : listCasts(const char *pattern, bool verbose)
4960 : {
4961 : PQExpBufferData buf;
4962 : PGresult *res;
4963 42 : printQueryOpt myopt = pset.popt;
4964 : static const bool translate_columns[] = {false, false, false, true, true, false};
4965 :
4966 42 : initPQExpBuffer(&buf);
4967 :
4968 42 : 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 42 : 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 42 : 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 42 : if (verbose)
5001 0 : 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 42 : 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 42 : if (verbose)
5028 0 : 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 42 : appendPQExpBufferStr(&buf, "WHERE ( (true");
5034 :
5035 : /*
5036 : * Match name pattern against either internal or external name of either
5037 : * castsource or casttarget
5038 : */
5039 42 : 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 24 : goto error_return;
5045 :
5046 18 : appendPQExpBufferStr(&buf, ") OR (true");
5047 :
5048 18 : 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 0 : goto error_return;
5054 :
5055 18 : appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
5056 :
5057 18 : res = PSQLexec(buf.data);
5058 18 : termPQExpBuffer(&buf);
5059 18 : if (!res)
5060 0 : return false;
5061 :
5062 18 : myopt.title = _("List of casts");
5063 18 : myopt.translate_header = true;
5064 18 : myopt.translate_columns = translate_columns;
5065 18 : myopt.n_translate_columns = lengthof(translate_columns);
5066 :
5067 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5068 :
5069 18 : PQclear(res);
5070 18 : return true;
5071 :
5072 24 : error_return:
5073 24 : termPQExpBuffer(&buf);
5074 24 : return false;
5075 : }
5076 :
5077 : /*
5078 : * \dO
5079 : *
5080 : * Describes collations.
5081 : */
5082 : bool
5083 42 : listCollations(const char *pattern, bool verbose, bool showSystem)
5084 : {
5085 : PQExpBufferData buf;
5086 : PGresult *res;
5087 42 : printQueryOpt myopt = pset.popt;
5088 : static const bool translate_columns[] = {false, false, false, false, false, false, false, true, false};
5089 :
5090 42 : initPQExpBuffer(&buf);
5091 :
5092 42 : 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 42 : if (pset.sversion >= 100000)
5100 42 : 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 0 : appendPQExpBuffer(&buf,
5110 : " 'libc' AS \"%s\",\n",
5111 : gettext_noop("Provider"));
5112 :
5113 42 : appendPQExpBuffer(&buf,
5114 : " c.collcollate AS \"%s\",\n"
5115 : " c.collctype AS \"%s\",\n",
5116 : gettext_noop("Collate"),
5117 : gettext_noop("Ctype"));
5118 :
5119 42 : if (pset.sversion >= 170000)
5120 42 : appendPQExpBuffer(&buf,
5121 : " c.colllocale AS \"%s\",\n",
5122 : gettext_noop("Locale"));
5123 0 : else if (pset.sversion >= 150000)
5124 0 : appendPQExpBuffer(&buf,
5125 : " c.colliculocale AS \"%s\",\n",
5126 : gettext_noop("Locale"));
5127 : else
5128 0 : appendPQExpBuffer(&buf,
5129 : " c.collcollate AS \"%s\",\n",
5130 : gettext_noop("Locale"));
5131 :
5132 42 : if (pset.sversion >= 160000)
5133 42 : appendPQExpBuffer(&buf,
5134 : " c.collicurules AS \"%s\",\n",
5135 : gettext_noop("ICU Rules"));
5136 : else
5137 0 : appendPQExpBuffer(&buf,
5138 : " NULL AS \"%s\",\n",
5139 : gettext_noop("ICU Rules"));
5140 :
5141 42 : if (pset.sversion >= 120000)
5142 42 : 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 0 : appendPQExpBuffer(&buf,
5148 : " '%s' AS \"%s\"",
5149 : gettext_noop("yes"),
5150 : gettext_noop("Deterministic?"));
5151 :
5152 42 : if (verbose)
5153 0 : appendPQExpBuffer(&buf,
5154 : ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
5155 : gettext_noop("Description"));
5156 :
5157 42 : appendPQExpBufferStr(&buf,
5158 : "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
5159 : "WHERE n.oid = c.collnamespace\n");
5160 :
5161 42 : if (!showSystem && !pattern)
5162 0 : 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 42 : appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
5172 :
5173 42 : 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 24 : termPQExpBuffer(&buf);
5179 24 : return false;
5180 : }
5181 :
5182 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5183 :
5184 18 : res = PSQLexec(buf.data);
5185 18 : termPQExpBuffer(&buf);
5186 18 : if (!res)
5187 0 : return false;
5188 :
5189 18 : myopt.title = _("List of collations");
5190 18 : myopt.translate_header = true;
5191 18 : myopt.translate_columns = translate_columns;
5192 18 : myopt.n_translate_columns = lengthof(translate_columns);
5193 :
5194 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5195 :
5196 18 : PQclear(res);
5197 18 : return true;
5198 : }
5199 :
5200 : /*
5201 : * \dn
5202 : *
5203 : * Describes schemas (namespaces)
5204 : */
5205 : bool
5206 24 : listSchemas(const char *pattern, bool verbose, bool showSystem)
5207 : {
5208 : PQExpBufferData buf;
5209 : PGresult *res;
5210 24 : printQueryOpt myopt = pset.popt;
5211 24 : int pub_schema_tuples = 0;
5212 24 : char **footers = NULL;
5213 :
5214 24 : initPQExpBuffer(&buf);
5215 24 : 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 24 : if (verbose)
5222 : {
5223 0 : appendPQExpBufferStr(&buf, ",\n ");
5224 0 : printACLColumn(&buf, "n.nspacl");
5225 0 : appendPQExpBuffer(&buf,
5226 : ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
5227 : gettext_noop("Description"));
5228 : }
5229 :
5230 24 : appendPQExpBufferStr(&buf,
5231 : "\nFROM pg_catalog.pg_namespace n\n");
5232 :
5233 24 : if (!showSystem && !pattern)
5234 0 : appendPQExpBufferStr(&buf,
5235 : "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
5236 :
5237 24 : if (!validateSQLNamePattern(&buf, pattern,
5238 24 : !showSystem && !pattern, false,
5239 : NULL, "n.nspname", NULL,
5240 : NULL,
5241 : NULL, 2))
5242 18 : goto error_return;
5243 :
5244 6 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
5245 :
5246 6 : res = PSQLexec(buf.data);
5247 6 : if (!res)
5248 0 : goto error_return;
5249 :
5250 6 : myopt.title = _("List of schemas");
5251 6 : myopt.translate_header = true;
5252 :
5253 6 : if (pattern && pset.sversion >= 150000)
5254 : {
5255 : PGresult *result;
5256 : int i;
5257 :
5258 6 : 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 6 : result = PSQLexec(buf.data);
5267 6 : if (!result)
5268 0 : goto error_return;
5269 : else
5270 6 : pub_schema_tuples = PQntuples(result);
5271 :
5272 6 : 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 0 : footers = (char **) pg_malloc((1 + pub_schema_tuples + 1) * sizeof(char *));
5280 0 : footers[0] = pg_strdup(_("Publications:"));
5281 :
5282 : /* Might be an empty set - that's ok */
5283 0 : for (i = 0; i < pub_schema_tuples; i++)
5284 : {
5285 0 : printfPQExpBuffer(&buf, " \"%s\"",
5286 : PQgetvalue(result, i, 0));
5287 :
5288 0 : footers[i + 1] = pg_strdup(buf.data);
5289 : }
5290 :
5291 0 : footers[i + 1] = NULL;
5292 0 : myopt.footers = footers;
5293 : }
5294 :
5295 6 : PQclear(result);
5296 : }
5297 :
5298 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5299 :
5300 6 : termPQExpBuffer(&buf);
5301 6 : PQclear(res);
5302 :
5303 : /* Free the memory allocated for the footer */
5304 6 : if (footers)
5305 : {
5306 0 : char **footer = NULL;
5307 :
5308 0 : for (footer = footers; *footer; footer++)
5309 0 : pg_free(*footer);
5310 :
5311 0 : pg_free(footers);
5312 : }
5313 :
5314 6 : return true;
5315 :
5316 18 : error_return:
5317 18 : termPQExpBuffer(&buf);
5318 18 : return false;
5319 : }
5320 :
5321 :
5322 : /*
5323 : * \dFp
5324 : * list text search parsers
5325 : */
5326 : bool
5327 42 : listTSParsers(const char *pattern, bool verbose)
5328 : {
5329 : PQExpBufferData buf;
5330 : PGresult *res;
5331 42 : printQueryOpt myopt = pset.popt;
5332 :
5333 42 : if (verbose)
5334 0 : return listTSParsersVerbose(pattern);
5335 :
5336 42 : initPQExpBuffer(&buf);
5337 :
5338 42 : 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 42 : 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 24 : termPQExpBuffer(&buf);
5356 24 : return false;
5357 : }
5358 :
5359 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5360 :
5361 18 : res = PSQLexec(buf.data);
5362 18 : termPQExpBuffer(&buf);
5363 18 : if (!res)
5364 0 : return false;
5365 :
5366 18 : myopt.title = _("List of text search parsers");
5367 18 : myopt.translate_header = true;
5368 :
5369 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5370 :
5371 18 : PQclear(res);
5372 18 : return true;
5373 : }
5374 :
5375 : /*
5376 : * full description of parsers
5377 : */
5378 : static bool
5379 0 : listTSParsersVerbose(const char *pattern)
5380 : {
5381 : PQExpBufferData buf;
5382 : PGresult *res;
5383 : int i;
5384 :
5385 0 : initPQExpBuffer(&buf);
5386 :
5387 0 : 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 0 : 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 0 : termPQExpBuffer(&buf);
5401 0 : return false;
5402 : }
5403 :
5404 0 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5405 :
5406 0 : res = PSQLexec(buf.data);
5407 0 : termPQExpBuffer(&buf);
5408 0 : if (!res)
5409 0 : return false;
5410 :
5411 0 : if (PQntuples(res) == 0)
5412 : {
5413 0 : if (!pset.quiet)
5414 : {
5415 0 : if (pattern)
5416 0 : pg_log_error("Did not find any text search parser named \"%s\".",
5417 : pattern);
5418 : else
5419 0 : pg_log_error("Did not find any text search parsers.");
5420 : }
5421 0 : PQclear(res);
5422 0 : return false;
5423 : }
5424 :
5425 0 : for (i = 0; i < PQntuples(res); i++)
5426 : {
5427 : const char *oid;
5428 0 : const char *nspname = NULL;
5429 : const char *prsname;
5430 :
5431 0 : oid = PQgetvalue(res, i, 0);
5432 0 : if (!PQgetisnull(res, i, 1))
5433 0 : nspname = PQgetvalue(res, i, 1);
5434 0 : prsname = PQgetvalue(res, i, 2);
5435 :
5436 0 : if (!describeOneTSParser(oid, nspname, prsname))
5437 : {
5438 0 : PQclear(res);
5439 0 : return false;
5440 : }
5441 :
5442 0 : if (cancel_pressed)
5443 : {
5444 0 : PQclear(res);
5445 0 : return false;
5446 : }
5447 : }
5448 :
5449 0 : PQclear(res);
5450 0 : return true;
5451 : }
5452 :
5453 : static bool
5454 0 : describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
5455 : {
5456 : PQExpBufferData buf;
5457 : PGresult *res;
5458 : PQExpBufferData title;
5459 0 : printQueryOpt myopt = pset.popt;
5460 : static const bool translate_columns[] = {true, false, false};
5461 :
5462 0 : initPQExpBuffer(&buf);
5463 :
5464 0 : 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 0 : res = PSQLexec(buf.data);
5509 0 : termPQExpBuffer(&buf);
5510 0 : if (!res)
5511 0 : return false;
5512 :
5513 0 : initPQExpBuffer(&title);
5514 0 : if (nspname)
5515 0 : printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
5516 : nspname, prsname);
5517 : else
5518 0 : printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
5519 0 : myopt.title = title.data;
5520 0 : myopt.footers = NULL;
5521 0 : myopt.topt.default_footer = false;
5522 0 : myopt.translate_header = true;
5523 0 : myopt.translate_columns = translate_columns;
5524 0 : myopt.n_translate_columns = lengthof(translate_columns);
5525 :
5526 0 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5527 :
5528 0 : PQclear(res);
5529 :
5530 0 : initPQExpBuffer(&buf);
5531 :
5532 0 : 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 0 : res = PSQLexec(buf.data);
5542 0 : termPQExpBuffer(&buf);
5543 0 : if (!res)
5544 : {
5545 0 : termPQExpBuffer(&title);
5546 0 : return false;
5547 : }
5548 :
5549 0 : if (nspname)
5550 0 : printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
5551 : nspname, prsname);
5552 : else
5553 0 : printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
5554 0 : myopt.title = title.data;
5555 0 : myopt.footers = NULL;
5556 0 : myopt.topt.default_footer = true;
5557 0 : myopt.translate_header = true;
5558 0 : myopt.translate_columns = NULL;
5559 0 : myopt.n_translate_columns = 0;
5560 :
5561 0 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5562 :
5563 0 : termPQExpBuffer(&title);
5564 0 : PQclear(res);
5565 0 : return true;
5566 : }
5567 :
5568 :
5569 : /*
5570 : * \dFd
5571 : * list text search dictionaries
5572 : */
5573 : bool
5574 42 : listTSDictionaries(const char *pattern, bool verbose)
5575 : {
5576 : PQExpBufferData buf;
5577 : PGresult *res;
5578 42 : printQueryOpt myopt = pset.popt;
5579 :
5580 42 : initPQExpBuffer(&buf);
5581 :
5582 42 : 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 42 : if (verbose)
5590 : {
5591 0 : 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 42 : appendPQExpBuffer(&buf,
5602 : " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
5603 : gettext_noop("Description"));
5604 :
5605 42 : 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 42 : 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 24 : termPQExpBuffer(&buf);
5614 24 : return false;
5615 : }
5616 :
5617 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5618 :
5619 18 : res = PSQLexec(buf.data);
5620 18 : termPQExpBuffer(&buf);
5621 18 : if (!res)
5622 0 : return false;
5623 :
5624 18 : myopt.title = _("List of text search dictionaries");
5625 18 : myopt.translate_header = true;
5626 :
5627 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5628 :
5629 18 : PQclear(res);
5630 18 : return true;
5631 : }
5632 :
5633 :
5634 : /*
5635 : * \dFt
5636 : * list text search templates
5637 : */
5638 : bool
5639 42 : listTSTemplates(const char *pattern, bool verbose)
5640 : {
5641 : PQExpBufferData buf;
5642 : PGresult *res;
5643 42 : printQueryOpt myopt = pset.popt;
5644 :
5645 42 : initPQExpBuffer(&buf);
5646 :
5647 42 : if (verbose)
5648 0 : 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 42 : 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 42 : 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 42 : 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 24 : termPQExpBuffer(&buf);
5679 24 : return false;
5680 : }
5681 :
5682 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5683 :
5684 18 : res = PSQLexec(buf.data);
5685 18 : termPQExpBuffer(&buf);
5686 18 : if (!res)
5687 0 : return false;
5688 :
5689 18 : myopt.title = _("List of text search templates");
5690 18 : myopt.translate_header = true;
5691 :
5692 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5693 :
5694 18 : PQclear(res);
5695 18 : return true;
5696 : }
5697 :
5698 :
5699 : /*
5700 : * \dF
5701 : * list text search configurations
5702 : */
5703 : bool
5704 42 : listTSConfigs(const char *pattern, bool verbose)
5705 : {
5706 : PQExpBufferData buf;
5707 : PGresult *res;
5708 42 : printQueryOpt myopt = pset.popt;
5709 :
5710 42 : if (verbose)
5711 0 : return listTSConfigsVerbose(pattern);
5712 :
5713 42 : initPQExpBuffer(&buf);
5714 :
5715 42 : 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 42 : 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 24 : termPQExpBuffer(&buf);
5733 24 : return false;
5734 : }
5735 :
5736 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5737 :
5738 18 : res = PSQLexec(buf.data);
5739 18 : termPQExpBuffer(&buf);
5740 18 : if (!res)
5741 0 : return false;
5742 :
5743 18 : myopt.title = _("List of text search configurations");
5744 18 : myopt.translate_header = true;
5745 :
5746 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5747 :
5748 18 : PQclear(res);
5749 18 : return true;
5750 : }
5751 :
5752 : static bool
5753 0 : listTSConfigsVerbose(const char *pattern)
5754 : {
5755 : PQExpBufferData buf;
5756 : PGresult *res;
5757 : int i;
5758 :
5759 0 : initPQExpBuffer(&buf);
5760 :
5761 0 : 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 0 : 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 0 : termPQExpBuffer(&buf);
5779 0 : return false;
5780 : }
5781 :
5782 0 : appendPQExpBufferStr(&buf, "ORDER BY 3, 2;");
5783 :
5784 0 : res = PSQLexec(buf.data);
5785 0 : termPQExpBuffer(&buf);
5786 0 : if (!res)
5787 0 : return false;
5788 :
5789 0 : if (PQntuples(res) == 0)
5790 : {
5791 0 : if (!pset.quiet)
5792 : {
5793 0 : if (pattern)
5794 0 : pg_log_error("Did not find any text search configuration named \"%s\".",
5795 : pattern);
5796 : else
5797 0 : pg_log_error("Did not find any text search configurations.");
5798 : }
5799 0 : PQclear(res);
5800 0 : return false;
5801 : }
5802 :
5803 0 : for (i = 0; i < PQntuples(res); i++)
5804 : {
5805 : const char *oid;
5806 : const char *cfgname;
5807 0 : const char *nspname = NULL;
5808 : const char *prsname;
5809 0 : const char *pnspname = NULL;
5810 :
5811 0 : oid = PQgetvalue(res, i, 0);
5812 0 : cfgname = PQgetvalue(res, i, 1);
5813 0 : if (!PQgetisnull(res, i, 2))
5814 0 : nspname = PQgetvalue(res, i, 2);
5815 0 : prsname = PQgetvalue(res, i, 3);
5816 0 : if (!PQgetisnull(res, i, 4))
5817 0 : pnspname = PQgetvalue(res, i, 4);
5818 :
5819 0 : if (!describeOneTSConfig(oid, nspname, cfgname, pnspname, prsname))
5820 : {
5821 0 : PQclear(res);
5822 0 : return false;
5823 : }
5824 :
5825 0 : if (cancel_pressed)
5826 : {
5827 0 : PQclear(res);
5828 0 : return false;
5829 : }
5830 : }
5831 :
5832 0 : PQclear(res);
5833 0 : return true;
5834 : }
5835 :
5836 : static bool
5837 0 : 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 0 : printQueryOpt myopt = pset.popt;
5844 :
5845 0 : initPQExpBuffer(&buf);
5846 :
5847 0 : 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 0 : res = PSQLexec(buf.data);
5868 0 : termPQExpBuffer(&buf);
5869 0 : if (!res)
5870 0 : return false;
5871 :
5872 0 : initPQExpBuffer(&title);
5873 :
5874 0 : if (nspname)
5875 0 : appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
5876 : nspname, cfgname);
5877 : else
5878 0 : appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
5879 : cfgname);
5880 :
5881 0 : if (pnspname)
5882 0 : appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
5883 : pnspname, prsname);
5884 : else
5885 0 : appendPQExpBuffer(&title, _("\nParser: \"%s\""),
5886 : prsname);
5887 :
5888 0 : myopt.title = title.data;
5889 0 : myopt.footers = NULL;
5890 0 : myopt.topt.default_footer = false;
5891 0 : myopt.translate_header = true;
5892 :
5893 0 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5894 :
5895 0 : termPQExpBuffer(&title);
5896 :
5897 0 : PQclear(res);
5898 0 : return true;
5899 : }
5900 :
5901 :
5902 : /*
5903 : * \dew
5904 : *
5905 : * Describes foreign-data wrappers
5906 : */
5907 : bool
5908 114 : listForeignDataWrappers(const char *pattern, bool verbose)
5909 : {
5910 : PQExpBufferData buf;
5911 : PGresult *res;
5912 114 : printQueryOpt myopt = pset.popt;
5913 :
5914 114 : initPQExpBuffer(&buf);
5915 114 : 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 114 : if (verbose)
5926 : {
5927 84 : appendPQExpBufferStr(&buf, ",\n ");
5928 84 : printACLColumn(&buf, "fdwacl");
5929 84 : 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 114 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
5942 :
5943 114 : if (verbose)
5944 84 : 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 114 : if (!validateSQLNamePattern(&buf, pattern, false, false,
5950 : NULL, "fdwname", NULL, NULL,
5951 : NULL, 1))
5952 : {
5953 18 : termPQExpBuffer(&buf);
5954 18 : return false;
5955 : }
5956 :
5957 96 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
5958 :
5959 96 : res = PSQLexec(buf.data);
5960 96 : termPQExpBuffer(&buf);
5961 96 : if (!res)
5962 0 : return false;
5963 :
5964 96 : myopt.title = _("List of foreign-data wrappers");
5965 96 : myopt.translate_header = true;
5966 :
5967 96 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5968 :
5969 96 : PQclear(res);
5970 96 : return true;
5971 : }
5972 :
5973 : /*
5974 : * \des
5975 : *
5976 : * Describes foreign servers.
5977 : */
5978 : bool
5979 120 : listForeignServers(const char *pattern, bool verbose)
5980 : {
5981 : PQExpBufferData buf;
5982 : PGresult *res;
5983 120 : printQueryOpt myopt = pset.popt;
5984 :
5985 120 : initPQExpBuffer(&buf);
5986 120 : 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 120 : if (verbose)
5995 : {
5996 48 : appendPQExpBufferStr(&buf, ",\n ");
5997 48 : printACLColumn(&buf, "s.srvacl");
5998 48 : 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 120 : 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 120 : if (verbose)
6020 48 : 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 120 : if (!validateSQLNamePattern(&buf, pattern, false, false,
6026 : NULL, "s.srvname", NULL, NULL,
6027 : NULL, 1))
6028 : {
6029 42 : termPQExpBuffer(&buf);
6030 42 : return false;
6031 : }
6032 :
6033 78 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
6034 :
6035 78 : res = PSQLexec(buf.data);
6036 78 : termPQExpBuffer(&buf);
6037 78 : if (!res)
6038 0 : return false;
6039 :
6040 78 : myopt.title = _("List of foreign servers");
6041 78 : myopt.translate_header = true;
6042 :
6043 78 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6044 :
6045 78 : PQclear(res);
6046 78 : return true;
6047 : }
6048 :
6049 : /*
6050 : * \deu
6051 : *
6052 : * Describes user mappings.
6053 : */
6054 : bool
6055 60 : listUserMappings(const char *pattern, bool verbose)
6056 : {
6057 : PQExpBufferData buf;
6058 : PGresult *res;
6059 60 : printQueryOpt myopt = pset.popt;
6060 :
6061 60 : initPQExpBuffer(&buf);
6062 60 : 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 60 : if (verbose)
6069 36 : 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 60 : appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
6079 :
6080 60 : if (!validateSQLNamePattern(&buf, pattern, false, false,
6081 : NULL, "um.srvname", "um.usename", NULL,
6082 : NULL, 1))
6083 : {
6084 0 : termPQExpBuffer(&buf);
6085 0 : return false;
6086 : }
6087 :
6088 60 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6089 :
6090 60 : res = PSQLexec(buf.data);
6091 60 : termPQExpBuffer(&buf);
6092 60 : if (!res)
6093 0 : return false;
6094 :
6095 60 : myopt.title = _("List of user mappings");
6096 60 : myopt.translate_header = true;
6097 :
6098 60 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6099 :
6100 60 : PQclear(res);
6101 60 : return true;
6102 : }
6103 :
6104 : /*
6105 : * \det
6106 : *
6107 : * Describes foreign tables.
6108 : */
6109 : bool
6110 18 : listForeignTables(const char *pattern, bool verbose)
6111 : {
6112 : PQExpBufferData buf;
6113 : PGresult *res;
6114 18 : printQueryOpt myopt = pset.popt;
6115 :
6116 18 : initPQExpBuffer(&buf);
6117 18 : 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 18 : if (verbose)
6126 18 : 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 18 : 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 18 : if (verbose)
6146 18 : 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 18 : 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 0 : termPQExpBuffer(&buf);
6157 0 : return false;
6158 : }
6159 :
6160 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6161 :
6162 18 : res = PSQLexec(buf.data);
6163 18 : termPQExpBuffer(&buf);
6164 18 : if (!res)
6165 0 : return false;
6166 :
6167 18 : myopt.title = _("List of foreign tables");
6168 18 : myopt.translate_header = true;
6169 :
6170 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6171 :
6172 18 : PQclear(res);
6173 18 : return true;
6174 : }
6175 :
6176 : /*
6177 : * \dx
6178 : *
6179 : * Briefly describes installed extensions.
6180 : */
6181 : bool
6182 24 : listExtensions(const char *pattern)
6183 : {
6184 : PQExpBufferData buf;
6185 : PGresult *res;
6186 24 : printQueryOpt myopt = pset.popt;
6187 :
6188 24 : initPQExpBuffer(&buf);
6189 24 : printfPQExpBuffer(&buf,
6190 : "SELECT e.extname AS \"%s\", "
6191 : "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
6192 : "FROM pg_catalog.pg_extension e "
6193 : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
6194 : "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
6195 : "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
6196 : gettext_noop("Name"),
6197 : gettext_noop("Version"),
6198 : gettext_noop("Schema"),
6199 : gettext_noop("Description"));
6200 :
6201 24 : if (!validateSQLNamePattern(&buf, pattern,
6202 : false, false,
6203 : NULL, "e.extname", NULL,
6204 : NULL,
6205 : NULL, 1))
6206 : {
6207 18 : termPQExpBuffer(&buf);
6208 18 : return false;
6209 : }
6210 :
6211 6 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
6212 :
6213 6 : res = PSQLexec(buf.data);
6214 6 : termPQExpBuffer(&buf);
6215 6 : if (!res)
6216 0 : return false;
6217 :
6218 6 : myopt.title = _("List of installed extensions");
6219 6 : myopt.translate_header = true;
6220 :
6221 6 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6222 :
6223 6 : PQclear(res);
6224 6 : return true;
6225 : }
6226 :
6227 : /*
6228 : * \dx+
6229 : *
6230 : * List contents of installed extensions.
6231 : */
6232 : bool
6233 32 : listExtensionContents(const char *pattern)
6234 : {
6235 : PQExpBufferData buf;
6236 : PGresult *res;
6237 : int i;
6238 :
6239 32 : initPQExpBuffer(&buf);
6240 32 : printfPQExpBuffer(&buf,
6241 : "SELECT e.extname, e.oid\n"
6242 : "FROM pg_catalog.pg_extension e\n");
6243 :
6244 32 : if (!validateSQLNamePattern(&buf, pattern,
6245 : false, false,
6246 : NULL, "e.extname", NULL,
6247 : NULL,
6248 : NULL, 1))
6249 : {
6250 0 : termPQExpBuffer(&buf);
6251 0 : return false;
6252 : }
6253 :
6254 32 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
6255 :
6256 32 : res = PSQLexec(buf.data);
6257 32 : termPQExpBuffer(&buf);
6258 32 : if (!res)
6259 0 : return false;
6260 :
6261 32 : if (PQntuples(res) == 0)
6262 : {
6263 0 : if (!pset.quiet)
6264 : {
6265 0 : if (pattern)
6266 0 : pg_log_error("Did not find any extension named \"%s\".",
6267 : pattern);
6268 : else
6269 0 : pg_log_error("Did not find any extensions.");
6270 : }
6271 0 : PQclear(res);
6272 0 : return false;
6273 : }
6274 :
6275 64 : for (i = 0; i < PQntuples(res); i++)
6276 : {
6277 : const char *extname;
6278 : const char *oid;
6279 :
6280 32 : extname = PQgetvalue(res, i, 0);
6281 32 : oid = PQgetvalue(res, i, 1);
6282 :
6283 32 : if (!listOneExtensionContents(extname, oid))
6284 : {
6285 0 : PQclear(res);
6286 0 : return false;
6287 : }
6288 32 : if (cancel_pressed)
6289 : {
6290 0 : PQclear(res);
6291 0 : return false;
6292 : }
6293 : }
6294 :
6295 32 : PQclear(res);
6296 32 : return true;
6297 : }
6298 :
6299 : static bool
6300 32 : listOneExtensionContents(const char *extname, const char *oid)
6301 : {
6302 : PQExpBufferData buf;
6303 : PGresult *res;
6304 : PQExpBufferData title;
6305 32 : printQueryOpt myopt = pset.popt;
6306 :
6307 32 : initPQExpBuffer(&buf);
6308 32 : printfPQExpBuffer(&buf,
6309 : "SELECT pg_catalog.pg_describe_object(classid, objid, 0) AS \"%s\"\n"
6310 : "FROM pg_catalog.pg_depend\n"
6311 : "WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND refobjid = '%s' AND deptype = 'e'\n"
6312 : "ORDER BY 1;",
6313 : gettext_noop("Object description"),
6314 : oid);
6315 :
6316 32 : res = PSQLexec(buf.data);
6317 32 : termPQExpBuffer(&buf);
6318 32 : if (!res)
6319 0 : return false;
6320 :
6321 32 : initPQExpBuffer(&title);
6322 32 : printfPQExpBuffer(&title, _("Objects in extension \"%s\""), extname);
6323 32 : myopt.title = title.data;
6324 32 : myopt.translate_header = true;
6325 :
6326 32 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6327 :
6328 32 : termPQExpBuffer(&title);
6329 32 : PQclear(res);
6330 32 : return true;
6331 : }
6332 :
6333 : /*
6334 : * validateSQLNamePattern
6335 : *
6336 : * Wrapper around string_utils's processSQLNamePattern which also checks the
6337 : * pattern's validity. In addition to that function's parameters, takes a
6338 : * 'maxparts' parameter specifying the maximum number of dotted names the
6339 : * pattern is allowed to have, and a 'added_clause' parameter that returns by
6340 : * reference whether a clause was added to 'buf'. Returns whether the pattern
6341 : * passed validation, after logging any errors.
6342 : */
6343 : static bool
6344 6976 : validateSQLNamePattern(PQExpBuffer buf, const char *pattern, bool have_where,
6345 : bool force_escape, const char *schemavar,
6346 : const char *namevar, const char *altnamevar,
6347 : const char *visibilityrule, bool *added_clause,
6348 : int maxparts)
6349 : {
6350 : PQExpBufferData dbbuf;
6351 : int dotcnt;
6352 : bool added;
6353 :
6354 6976 : initPQExpBuffer(&dbbuf);
6355 6976 : added = processSQLNamePattern(pset.db, buf, pattern, have_where, force_escape,
6356 : schemavar, namevar, altnamevar,
6357 : visibilityrule, &dbbuf, &dotcnt);
6358 6976 : if (added_clause != NULL)
6359 170 : *added_clause = added;
6360 :
6361 6976 : if (dotcnt >= maxparts)
6362 : {
6363 438 : pg_log_error("improper qualified name (too many dotted names): %s",
6364 : pattern);
6365 438 : goto error_return;
6366 : }
6367 :
6368 6538 : if (maxparts > 1 && dotcnt == maxparts - 1)
6369 : {
6370 618 : if (PQdb(pset.db) == NULL)
6371 : {
6372 0 : pg_log_error("You are currently not connected to a database.");
6373 0 : goto error_return;
6374 : }
6375 618 : if (strcmp(PQdb(pset.db), dbbuf.data) != 0)
6376 : {
6377 450 : pg_log_error("cross-database references are not implemented: %s",
6378 : pattern);
6379 450 : goto error_return;
6380 : }
6381 : }
6382 6088 : termPQExpBuffer(&dbbuf);
6383 6088 : return true;
6384 :
6385 888 : error_return:
6386 888 : termPQExpBuffer(&dbbuf);
6387 888 : return false;
6388 : }
6389 :
6390 : /*
6391 : * \dRp
6392 : * Lists publications.
6393 : *
6394 : * Takes an optional regexp to select particular publications
6395 : */
6396 : bool
6397 48 : listPublications(const char *pattern)
6398 : {
6399 : PQExpBufferData buf;
6400 : PGresult *res;
6401 48 : printQueryOpt myopt = pset.popt;
6402 : static const bool translate_columns[] = {false, false, false, false, false, false, false, false, false};
6403 :
6404 48 : if (pset.sversion < 100000)
6405 : {
6406 : char sverbuf[32];
6407 :
6408 0 : pg_log_error("The server (version %s) does not support publications.",
6409 : formatPGVersionNumber(pset.sversion, false,
6410 : sverbuf, sizeof(sverbuf)));
6411 0 : return true;
6412 : }
6413 :
6414 48 : initPQExpBuffer(&buf);
6415 :
6416 48 : printfPQExpBuffer(&buf,
6417 : "SELECT pubname AS \"%s\",\n"
6418 : " pg_catalog.pg_get_userbyid(pubowner) AS \"%s\",\n"
6419 : " puballtables AS \"%s\",\n"
6420 : " pubinsert AS \"%s\",\n"
6421 : " pubupdate AS \"%s\",\n"
6422 : " pubdelete AS \"%s\"",
6423 : gettext_noop("Name"),
6424 : gettext_noop("Owner"),
6425 : gettext_noop("All tables"),
6426 : gettext_noop("Inserts"),
6427 : gettext_noop("Updates"),
6428 : gettext_noop("Deletes"));
6429 48 : if (pset.sversion >= 110000)
6430 48 : appendPQExpBuffer(&buf,
6431 : ",\n pubtruncate AS \"%s\"",
6432 : gettext_noop("Truncates"));
6433 48 : if (pset.sversion >= 180000)
6434 48 : appendPQExpBuffer(&buf,
6435 : ",\n (CASE pubgencols\n"
6436 : " WHEN '%c' THEN 'none'\n"
6437 : " WHEN '%c' THEN 'stored'\n"
6438 : " END) AS \"%s\"",
6439 : PUBLISH_GENCOLS_NONE,
6440 : PUBLISH_GENCOLS_STORED,
6441 : gettext_noop("Generated columns"));
6442 48 : if (pset.sversion >= 130000)
6443 48 : appendPQExpBuffer(&buf,
6444 : ",\n pubviaroot AS \"%s\"",
6445 : gettext_noop("Via root"));
6446 :
6447 48 : appendPQExpBufferStr(&buf,
6448 : "\nFROM pg_catalog.pg_publication\n");
6449 :
6450 48 : if (!validateSQLNamePattern(&buf, pattern, false, false,
6451 : NULL, "pubname", NULL,
6452 : NULL,
6453 : NULL, 1))
6454 : {
6455 18 : termPQExpBuffer(&buf);
6456 18 : return false;
6457 : }
6458 :
6459 30 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
6460 :
6461 30 : res = PSQLexec(buf.data);
6462 30 : termPQExpBuffer(&buf);
6463 30 : if (!res)
6464 0 : return false;
6465 :
6466 30 : myopt.title = _("List of publications");
6467 30 : myopt.translate_header = true;
6468 30 : myopt.translate_columns = translate_columns;
6469 30 : myopt.n_translate_columns = lengthof(translate_columns);
6470 :
6471 30 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6472 :
6473 30 : PQclear(res);
6474 :
6475 30 : return true;
6476 : }
6477 :
6478 : /*
6479 : * Add footer to publication description.
6480 : */
6481 : static bool
6482 624 : addFooterToPublicationDesc(PQExpBuffer buf, const char *footermsg,
6483 : bool as_schema, printTableContent *const cont)
6484 : {
6485 : PGresult *res;
6486 624 : int count = 0;
6487 624 : int i = 0;
6488 :
6489 624 : res = PSQLexec(buf->data);
6490 624 : if (!res)
6491 0 : return false;
6492 : else
6493 624 : count = PQntuples(res);
6494 :
6495 624 : if (count > 0)
6496 330 : printTableAddFooter(cont, footermsg);
6497 :
6498 1080 : for (i = 0; i < count; i++)
6499 : {
6500 456 : if (as_schema)
6501 234 : printfPQExpBuffer(buf, " \"%s\"", PQgetvalue(res, i, 0));
6502 : else
6503 : {
6504 222 : printfPQExpBuffer(buf, " \"%s.%s\"", PQgetvalue(res, i, 0),
6505 : PQgetvalue(res, i, 1));
6506 :
6507 222 : if (!PQgetisnull(res, i, 3))
6508 42 : appendPQExpBuffer(buf, " (%s)", PQgetvalue(res, i, 3));
6509 :
6510 222 : if (!PQgetisnull(res, i, 2))
6511 54 : appendPQExpBuffer(buf, " WHERE %s", PQgetvalue(res, i, 2));
6512 : }
6513 :
6514 456 : printTableAddFooter(cont, buf->data);
6515 : }
6516 :
6517 624 : PQclear(res);
6518 624 : return true;
6519 : }
6520 :
6521 : /*
6522 : * \dRp+
6523 : * Describes publications including the contents.
6524 : *
6525 : * Takes an optional regexp to select particular publications
6526 : */
6527 : bool
6528 336 : describePublications(const char *pattern)
6529 : {
6530 : PQExpBufferData buf;
6531 : int i;
6532 : PGresult *res;
6533 : bool has_pubtruncate;
6534 : bool has_pubgencols;
6535 : bool has_pubviaroot;
6536 :
6537 : PQExpBufferData title;
6538 : printTableContent cont;
6539 :
6540 336 : if (pset.sversion < 100000)
6541 : {
6542 : char sverbuf[32];
6543 :
6544 0 : pg_log_error("The server (version %s) does not support publications.",
6545 : formatPGVersionNumber(pset.sversion, false,
6546 : sverbuf, sizeof(sverbuf)));
6547 0 : return true;
6548 : }
6549 :
6550 336 : has_pubtruncate = (pset.sversion >= 110000);
6551 336 : has_pubgencols = (pset.sversion >= 180000);
6552 336 : has_pubviaroot = (pset.sversion >= 130000);
6553 :
6554 336 : initPQExpBuffer(&buf);
6555 :
6556 336 : printfPQExpBuffer(&buf,
6557 : "SELECT oid, pubname,\n"
6558 : " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
6559 : " puballtables, pubinsert, pubupdate, pubdelete");
6560 336 : if (has_pubtruncate)
6561 336 : appendPQExpBufferStr(&buf,
6562 : ", pubtruncate");
6563 : else
6564 0 : appendPQExpBufferStr(&buf,
6565 : ", false AS pubtruncate");
6566 :
6567 336 : if (has_pubgencols)
6568 336 : appendPQExpBuffer(&buf,
6569 : ", (CASE pubgencols\n"
6570 : " WHEN '%c' THEN 'none'\n"
6571 : " WHEN '%c' THEN 'stored'\n"
6572 : " END) AS \"%s\"\n",
6573 : PUBLISH_GENCOLS_NONE,
6574 : PUBLISH_GENCOLS_STORED,
6575 : gettext_noop("Generated columns"));
6576 : else
6577 0 : appendPQExpBufferStr(&buf,
6578 : ", 'none' AS pubgencols");
6579 :
6580 336 : if (has_pubviaroot)
6581 336 : appendPQExpBufferStr(&buf,
6582 : ", pubviaroot");
6583 : else
6584 0 : appendPQExpBufferStr(&buf,
6585 : ", false AS pubviaroot");
6586 :
6587 336 : appendPQExpBufferStr(&buf,
6588 : "\nFROM pg_catalog.pg_publication\n");
6589 :
6590 336 : if (!validateSQLNamePattern(&buf, pattern, false, false,
6591 : NULL, "pubname", NULL,
6592 : NULL,
6593 : NULL, 1))
6594 : {
6595 0 : termPQExpBuffer(&buf);
6596 0 : return false;
6597 : }
6598 :
6599 336 : appendPQExpBufferStr(&buf, "ORDER BY 2;");
6600 :
6601 336 : res = PSQLexec(buf.data);
6602 336 : if (!res)
6603 : {
6604 0 : termPQExpBuffer(&buf);
6605 0 : return false;
6606 : }
6607 :
6608 336 : if (PQntuples(res) == 0)
6609 : {
6610 0 : if (!pset.quiet)
6611 : {
6612 0 : if (pattern)
6613 0 : pg_log_error("Did not find any publication named \"%s\".",
6614 : pattern);
6615 : else
6616 0 : pg_log_error("Did not find any publications.");
6617 : }
6618 :
6619 0 : termPQExpBuffer(&buf);
6620 0 : PQclear(res);
6621 0 : return false;
6622 : }
6623 :
6624 672 : for (i = 0; i < PQntuples(res); i++)
6625 : {
6626 336 : const char align = 'l';
6627 336 : int ncols = 5;
6628 336 : int nrows = 1;
6629 336 : char *pubid = PQgetvalue(res, i, 0);
6630 336 : char *pubname = PQgetvalue(res, i, 1);
6631 336 : bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
6632 336 : printTableOpt myopt = pset.popt.topt;
6633 :
6634 336 : if (has_pubtruncate)
6635 336 : ncols++;
6636 336 : if (has_pubgencols)
6637 336 : ncols++;
6638 336 : if (has_pubviaroot)
6639 336 : ncols++;
6640 :
6641 336 : initPQExpBuffer(&title);
6642 336 : printfPQExpBuffer(&title, _("Publication %s"), pubname);
6643 336 : printTableInit(&cont, &myopt, title.data, ncols, nrows);
6644 :
6645 336 : printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
6646 336 : printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
6647 336 : printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
6648 336 : printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
6649 336 : printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
6650 336 : if (has_pubtruncate)
6651 336 : printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
6652 336 : if (has_pubgencols)
6653 336 : printTableAddHeader(&cont, gettext_noop("Generated columns"), true, align);
6654 336 : if (has_pubviaroot)
6655 336 : printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
6656 :
6657 336 : printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
6658 336 : printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
6659 336 : printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
6660 336 : printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
6661 336 : printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
6662 336 : if (has_pubtruncate)
6663 336 : printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
6664 336 : if (has_pubgencols)
6665 336 : printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
6666 336 : if (has_pubviaroot)
6667 336 : printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
6668 :
6669 336 : if (!puballtables)
6670 : {
6671 : /* Get the tables for the specified publication */
6672 312 : printfPQExpBuffer(&buf,
6673 : "SELECT n.nspname, c.relname");
6674 312 : if (pset.sversion >= 150000)
6675 : {
6676 312 : appendPQExpBufferStr(&buf,
6677 : ", pg_get_expr(pr.prqual, c.oid)");
6678 312 : appendPQExpBufferStr(&buf,
6679 : ", (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
6680 : " pg_catalog.array_to_string("
6681 : " ARRAY(SELECT attname\n"
6682 : " FROM\n"
6683 : " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
6684 : " pg_catalog.pg_attribute\n"
6685 : " WHERE attrelid = c.oid AND attnum = prattrs[s]), ', ')\n"
6686 : " ELSE NULL END)");
6687 : }
6688 : else
6689 0 : appendPQExpBufferStr(&buf,
6690 : ", NULL, NULL");
6691 312 : appendPQExpBuffer(&buf,
6692 : "\nFROM pg_catalog.pg_class c,\n"
6693 : " pg_catalog.pg_namespace n,\n"
6694 : " pg_catalog.pg_publication_rel pr\n"
6695 : "WHERE c.relnamespace = n.oid\n"
6696 : " AND c.oid = pr.prrelid\n"
6697 : " AND pr.prpubid = '%s'\n"
6698 : "ORDER BY 1,2", pubid);
6699 312 : if (!addFooterToPublicationDesc(&buf, _("Tables:"), false, &cont))
6700 0 : goto error_return;
6701 :
6702 312 : if (pset.sversion >= 150000)
6703 : {
6704 : /* Get the schemas for the specified publication */
6705 312 : printfPQExpBuffer(&buf,
6706 : "SELECT n.nspname\n"
6707 : "FROM pg_catalog.pg_namespace n\n"
6708 : " JOIN pg_catalog.pg_publication_namespace pn ON n.oid = pn.pnnspid\n"
6709 : "WHERE pn.pnpubid = '%s'\n"
6710 : "ORDER BY 1", pubid);
6711 312 : if (!addFooterToPublicationDesc(&buf, _("Tables from schemas:"),
6712 : true, &cont))
6713 0 : goto error_return;
6714 : }
6715 : }
6716 :
6717 336 : printTable(&cont, pset.queryFout, false, pset.logfile);
6718 336 : printTableCleanup(&cont);
6719 :
6720 336 : termPQExpBuffer(&title);
6721 : }
6722 :
6723 336 : termPQExpBuffer(&buf);
6724 336 : PQclear(res);
6725 :
6726 336 : return true;
6727 :
6728 0 : error_return:
6729 0 : printTableCleanup(&cont);
6730 0 : PQclear(res);
6731 0 : termPQExpBuffer(&buf);
6732 0 : termPQExpBuffer(&title);
6733 0 : return false;
6734 : }
6735 :
6736 : /*
6737 : * \dRs
6738 : * Describes subscriptions.
6739 : *
6740 : * Takes an optional regexp to select particular subscriptions
6741 : */
6742 : bool
6743 150 : describeSubscriptions(const char *pattern, bool verbose)
6744 : {
6745 : PQExpBufferData buf;
6746 : PGresult *res;
6747 150 : printQueryOpt myopt = pset.popt;
6748 : static const bool translate_columns[] = {false, false, false, false,
6749 : false, false, false, false, false, false, false, false, false, false,
6750 : false};
6751 :
6752 150 : if (pset.sversion < 100000)
6753 : {
6754 : char sverbuf[32];
6755 :
6756 0 : pg_log_error("The server (version %s) does not support subscriptions.",
6757 : formatPGVersionNumber(pset.sversion, false,
6758 : sverbuf, sizeof(sverbuf)));
6759 0 : return true;
6760 : }
6761 :
6762 150 : initPQExpBuffer(&buf);
6763 :
6764 150 : printfPQExpBuffer(&buf,
6765 : "SELECT subname AS \"%s\"\n"
6766 : ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
6767 : ", subenabled AS \"%s\"\n"
6768 : ", subpublications AS \"%s\"\n",
6769 : gettext_noop("Name"),
6770 : gettext_noop("Owner"),
6771 : gettext_noop("Enabled"),
6772 : gettext_noop("Publication"));
6773 :
6774 150 : if (verbose)
6775 : {
6776 : /* Binary mode and streaming are only supported in v14 and higher */
6777 114 : if (pset.sversion >= 140000)
6778 : {
6779 114 : appendPQExpBuffer(&buf,
6780 : ", subbinary AS \"%s\"\n",
6781 : gettext_noop("Binary"));
6782 :
6783 114 : if (pset.sversion >= 160000)
6784 114 : appendPQExpBuffer(&buf,
6785 : ", (CASE substream\n"
6786 : " WHEN " CppAsString2(LOGICALREP_STREAM_OFF) " THEN 'off'\n"
6787 : " WHEN " CppAsString2(LOGICALREP_STREAM_ON) " THEN 'on'\n"
6788 : " WHEN " CppAsString2(LOGICALREP_STREAM_PARALLEL) " THEN 'parallel'\n"
6789 : " END) AS \"%s\"\n",
6790 : gettext_noop("Streaming"));
6791 : else
6792 0 : appendPQExpBuffer(&buf,
6793 : ", substream AS \"%s\"\n",
6794 : gettext_noop("Streaming"));
6795 : }
6796 :
6797 : /* Two_phase and disable_on_error are only supported in v15 and higher */
6798 114 : if (pset.sversion >= 150000)
6799 114 : appendPQExpBuffer(&buf,
6800 : ", subtwophasestate AS \"%s\"\n"
6801 : ", subdisableonerr AS \"%s\"\n",
6802 : gettext_noop("Two-phase commit"),
6803 : gettext_noop("Disable on error"));
6804 :
6805 114 : if (pset.sversion >= 160000)
6806 114 : appendPQExpBuffer(&buf,
6807 : ", suborigin AS \"%s\"\n"
6808 : ", subpasswordrequired AS \"%s\"\n"
6809 : ", subrunasowner AS \"%s\"\n",
6810 : gettext_noop("Origin"),
6811 : gettext_noop("Password required"),
6812 : gettext_noop("Run as owner?"));
6813 :
6814 114 : if (pset.sversion >= 170000)
6815 114 : appendPQExpBuffer(&buf,
6816 : ", subfailover AS \"%s\"\n",
6817 : gettext_noop("Failover"));
6818 :
6819 114 : appendPQExpBuffer(&buf,
6820 : ", subsynccommit AS \"%s\"\n"
6821 : ", subconninfo AS \"%s\"\n",
6822 : gettext_noop("Synchronous commit"),
6823 : gettext_noop("Conninfo"));
6824 :
6825 : /* Skip LSN is only supported in v15 and higher */
6826 114 : if (pset.sversion >= 150000)
6827 114 : appendPQExpBuffer(&buf,
6828 : ", subskiplsn AS \"%s\"\n",
6829 : gettext_noop("Skip LSN"));
6830 : }
6831 :
6832 : /* Only display subscriptions in current database. */
6833 150 : appendPQExpBufferStr(&buf,
6834 : "FROM pg_catalog.pg_subscription\n"
6835 : "WHERE subdbid = (SELECT oid\n"
6836 : " FROM pg_catalog.pg_database\n"
6837 : " WHERE datname = pg_catalog.current_database())");
6838 :
6839 150 : if (!validateSQLNamePattern(&buf, pattern, true, false,
6840 : NULL, "subname", NULL,
6841 : NULL,
6842 : NULL, 1))
6843 : {
6844 18 : termPQExpBuffer(&buf);
6845 18 : return false;
6846 : }
6847 :
6848 132 : appendPQExpBufferStr(&buf, "ORDER BY 1;");
6849 :
6850 132 : res = PSQLexec(buf.data);
6851 132 : termPQExpBuffer(&buf);
6852 132 : if (!res)
6853 0 : return false;
6854 :
6855 132 : myopt.title = _("List of subscriptions");
6856 132 : myopt.translate_header = true;
6857 132 : myopt.translate_columns = translate_columns;
6858 132 : myopt.n_translate_columns = lengthof(translate_columns);
6859 :
6860 132 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6861 :
6862 132 : PQclear(res);
6863 132 : return true;
6864 : }
6865 :
6866 : /*
6867 : * printACLColumn
6868 : *
6869 : * Helper function for consistently formatting ACL (privilege) columns.
6870 : * The proper targetlist entry is appended to buf. Note lack of any
6871 : * whitespace or comma decoration.
6872 : *
6873 : * If you change this, see also the handling of attacl in permissionsList(),
6874 : * which can't conveniently use this code.
6875 : */
6876 : static void
6877 306 : printACLColumn(PQExpBuffer buf, const char *colname)
6878 : {
6879 306 : appendPQExpBuffer(buf,
6880 : "CASE"
6881 : " WHEN pg_catalog.array_length(%s, 1) = 0 THEN '%s'"
6882 : " ELSE pg_catalog.array_to_string(%s, E'\\n')"
6883 : " END AS \"%s\"",
6884 : colname, gettext_noop("(none)"),
6885 : colname, gettext_noop("Access privileges"));
6886 306 : }
6887 :
6888 : /*
6889 : * \dAc
6890 : * Lists operator classes
6891 : *
6892 : * Takes optional regexps to filter by index access method and input data type.
6893 : */
6894 : bool
6895 30 : listOperatorClasses(const char *access_method_pattern,
6896 : const char *type_pattern, bool verbose)
6897 : {
6898 : PQExpBufferData buf;
6899 : PGresult *res;
6900 30 : printQueryOpt myopt = pset.popt;
6901 30 : bool have_where = false;
6902 : static const bool translate_columns[] = {false, false, false, false, false, false, false};
6903 :
6904 30 : initPQExpBuffer(&buf);
6905 :
6906 30 : printfPQExpBuffer(&buf,
6907 : "SELECT\n"
6908 : " am.amname AS \"%s\",\n"
6909 : " pg_catalog.format_type(c.opcintype, NULL) AS \"%s\",\n"
6910 : " CASE\n"
6911 : " WHEN c.opckeytype <> 0 AND c.opckeytype <> c.opcintype\n"
6912 : " THEN pg_catalog.format_type(c.opckeytype, NULL)\n"
6913 : " ELSE NULL\n"
6914 : " END AS \"%s\",\n"
6915 : " CASE\n"
6916 : " WHEN pg_catalog.pg_opclass_is_visible(c.oid)\n"
6917 : " THEN pg_catalog.format('%%I', c.opcname)\n"
6918 : " ELSE pg_catalog.format('%%I.%%I', n.nspname, c.opcname)\n"
6919 : " END AS \"%s\",\n"
6920 : " (CASE WHEN c.opcdefault\n"
6921 : " THEN '%s'\n"
6922 : " ELSE '%s'\n"
6923 : " END) AS \"%s\"",
6924 : gettext_noop("AM"),
6925 : gettext_noop("Input type"),
6926 : gettext_noop("Storage type"),
6927 : gettext_noop("Operator class"),
6928 : gettext_noop("yes"),
6929 : gettext_noop("no"),
6930 : gettext_noop("Default?"));
6931 30 : if (verbose)
6932 0 : appendPQExpBuffer(&buf,
6933 : ",\n CASE\n"
6934 : " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
6935 : " THEN pg_catalog.format('%%I', of.opfname)\n"
6936 : " ELSE pg_catalog.format('%%I.%%I', ofn.nspname, of.opfname)\n"
6937 : " END AS \"%s\",\n"
6938 : " pg_catalog.pg_get_userbyid(c.opcowner) AS \"%s\"\n",
6939 : gettext_noop("Operator family"),
6940 : gettext_noop("Owner"));
6941 30 : appendPQExpBufferStr(&buf,
6942 : "\nFROM pg_catalog.pg_opclass c\n"
6943 : " LEFT JOIN pg_catalog.pg_am am on am.oid = c.opcmethod\n"
6944 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.opcnamespace\n"
6945 : " LEFT JOIN pg_catalog.pg_type t ON t.oid = c.opcintype\n"
6946 : " LEFT JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace\n");
6947 30 : if (verbose)
6948 0 : appendPQExpBufferStr(&buf,
6949 : " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = c.opcfamily\n"
6950 : " LEFT JOIN pg_catalog.pg_namespace ofn ON ofn.oid = of.opfnamespace\n");
6951 :
6952 30 : if (access_method_pattern)
6953 30 : if (!validateSQLNamePattern(&buf, access_method_pattern,
6954 : false, false, NULL, "am.amname", NULL, NULL,
6955 : &have_where, 1))
6956 18 : goto error_return;
6957 12 : if (type_pattern)
6958 : {
6959 : /* Match type name pattern against either internal or external name */
6960 6 : if (!validateSQLNamePattern(&buf, type_pattern, have_where, false,
6961 : "tn.nspname", "t.typname",
6962 : "pg_catalog.format_type(t.oid, NULL)",
6963 : "pg_catalog.pg_type_is_visible(t.oid)",
6964 : NULL, 3))
6965 0 : goto error_return;
6966 : }
6967 :
6968 12 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
6969 12 : res = PSQLexec(buf.data);
6970 12 : termPQExpBuffer(&buf);
6971 12 : if (!res)
6972 0 : return false;
6973 :
6974 12 : myopt.title = _("List of operator classes");
6975 12 : myopt.translate_header = true;
6976 12 : myopt.translate_columns = translate_columns;
6977 12 : myopt.n_translate_columns = lengthof(translate_columns);
6978 :
6979 12 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6980 :
6981 12 : PQclear(res);
6982 12 : return true;
6983 :
6984 18 : error_return:
6985 18 : termPQExpBuffer(&buf);
6986 18 : return false;
6987 : }
6988 :
6989 : /*
6990 : * \dAf
6991 : * Lists operator families
6992 : *
6993 : * Takes optional regexps to filter by index access method and input data type.
6994 : */
6995 : bool
6996 36 : listOperatorFamilies(const char *access_method_pattern,
6997 : const char *type_pattern, bool verbose)
6998 : {
6999 : PQExpBufferData buf;
7000 : PGresult *res;
7001 36 : printQueryOpt myopt = pset.popt;
7002 36 : bool have_where = false;
7003 : static const bool translate_columns[] = {false, false, false, false};
7004 :
7005 36 : initPQExpBuffer(&buf);
7006 :
7007 36 : printfPQExpBuffer(&buf,
7008 : "SELECT\n"
7009 : " am.amname AS \"%s\",\n"
7010 : " CASE\n"
7011 : " WHEN pg_catalog.pg_opfamily_is_visible(f.oid)\n"
7012 : " THEN pg_catalog.format('%%I', f.opfname)\n"
7013 : " ELSE pg_catalog.format('%%I.%%I', n.nspname, f.opfname)\n"
7014 : " END AS \"%s\",\n"
7015 : " (SELECT\n"
7016 : " pg_catalog.string_agg(pg_catalog.format_type(oc.opcintype, NULL), ', ')\n"
7017 : " FROM pg_catalog.pg_opclass oc\n"
7018 : " WHERE oc.opcfamily = f.oid) \"%s\"",
7019 : gettext_noop("AM"),
7020 : gettext_noop("Operator family"),
7021 : gettext_noop("Applicable types"));
7022 36 : if (verbose)
7023 0 : appendPQExpBuffer(&buf,
7024 : ",\n pg_catalog.pg_get_userbyid(f.opfowner) AS \"%s\"\n",
7025 : gettext_noop("Owner"));
7026 36 : appendPQExpBufferStr(&buf,
7027 : "\nFROM pg_catalog.pg_opfamily f\n"
7028 : " LEFT JOIN pg_catalog.pg_am am on am.oid = f.opfmethod\n"
7029 : " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = f.opfnamespace\n");
7030 :
7031 36 : if (access_method_pattern)
7032 36 : if (!validateSQLNamePattern(&buf, access_method_pattern,
7033 : false, false, NULL, "am.amname", NULL, NULL,
7034 : &have_where, 1))
7035 18 : goto error_return;
7036 18 : if (type_pattern)
7037 : {
7038 6 : appendPQExpBuffer(&buf,
7039 : " %s EXISTS (\n"
7040 : " SELECT 1\n"
7041 : " FROM pg_catalog.pg_type t\n"
7042 : " JOIN pg_catalog.pg_opclass oc ON oc.opcintype = t.oid\n"
7043 : " LEFT JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace\n"
7044 : " WHERE oc.opcfamily = f.oid\n",
7045 6 : have_where ? "AND" : "WHERE");
7046 : /* Match type name pattern against either internal or external name */
7047 6 : if (!validateSQLNamePattern(&buf, type_pattern, true, false,
7048 : "tn.nspname", "t.typname",
7049 : "pg_catalog.format_type(t.oid, NULL)",
7050 : "pg_catalog.pg_type_is_visible(t.oid)",
7051 : NULL, 3))
7052 0 : goto error_return;
7053 6 : appendPQExpBufferStr(&buf, " )\n");
7054 : }
7055 :
7056 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
7057 18 : res = PSQLexec(buf.data);
7058 18 : termPQExpBuffer(&buf);
7059 18 : if (!res)
7060 0 : return false;
7061 :
7062 18 : myopt.title = _("List of operator families");
7063 18 : myopt.translate_header = true;
7064 18 : myopt.translate_columns = translate_columns;
7065 18 : myopt.n_translate_columns = lengthof(translate_columns);
7066 :
7067 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7068 :
7069 18 : PQclear(res);
7070 18 : return true;
7071 :
7072 18 : error_return:
7073 18 : termPQExpBuffer(&buf);
7074 18 : return false;
7075 : }
7076 :
7077 : /*
7078 : * \dAo
7079 : * Lists operators of operator families
7080 : *
7081 : * Takes optional regexps to filter by index access method and operator
7082 : * family.
7083 : */
7084 : bool
7085 36 : listOpFamilyOperators(const char *access_method_pattern,
7086 : const char *family_pattern, bool verbose)
7087 : {
7088 : PQExpBufferData buf;
7089 : PGresult *res;
7090 36 : printQueryOpt myopt = pset.popt;
7091 36 : bool have_where = false;
7092 :
7093 : static const bool translate_columns[] = {false, false, false, false, false, false, true};
7094 :
7095 36 : initPQExpBuffer(&buf);
7096 :
7097 36 : printfPQExpBuffer(&buf,
7098 : "SELECT\n"
7099 : " am.amname AS \"%s\",\n"
7100 : " CASE\n"
7101 : " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
7102 : " THEN pg_catalog.format('%%I', of.opfname)\n"
7103 : " ELSE pg_catalog.format('%%I.%%I', nsf.nspname, of.opfname)\n"
7104 : " END AS \"%s\",\n"
7105 : " o.amopopr::pg_catalog.regoperator AS \"%s\"\n,"
7106 : " o.amopstrategy AS \"%s\",\n"
7107 : " CASE o.amoppurpose\n"
7108 : " WHEN " CppAsString2(AMOP_ORDER) " THEN '%s'\n"
7109 : " WHEN " CppAsString2(AMOP_SEARCH) " THEN '%s'\n"
7110 : " END AS \"%s\"\n",
7111 : gettext_noop("AM"),
7112 : gettext_noop("Operator family"),
7113 : gettext_noop("Operator"),
7114 : gettext_noop("Strategy"),
7115 : gettext_noop("ordering"),
7116 : gettext_noop("search"),
7117 : gettext_noop("Purpose"));
7118 :
7119 36 : if (verbose)
7120 6 : appendPQExpBuffer(&buf,
7121 : ", ofs.opfname AS \"%s\",\n"
7122 : " CASE\n"
7123 : " WHEN p.proleakproof THEN '%s'\n"
7124 : " ELSE '%s'\n"
7125 : " END AS \"%s\"\n",
7126 : gettext_noop("Sort opfamily"),
7127 : gettext_noop("yes"),
7128 : gettext_noop("no"),
7129 : gettext_noop("Leakproof?"));
7130 36 : appendPQExpBufferStr(&buf,
7131 : "FROM pg_catalog.pg_amop o\n"
7132 : " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = o.amopfamily\n"
7133 : " LEFT JOIN pg_catalog.pg_am am ON am.oid = of.opfmethod AND am.oid = o.amopmethod\n"
7134 : " LEFT JOIN pg_catalog.pg_namespace nsf ON of.opfnamespace = nsf.oid\n");
7135 36 : if (verbose)
7136 6 : appendPQExpBufferStr(&buf,
7137 : " LEFT JOIN pg_catalog.pg_opfamily ofs ON ofs.oid = o.amopsortfamily\n"
7138 : " LEFT JOIN pg_catalog.pg_operator op ON op.oid = o.amopopr\n"
7139 : " LEFT JOIN pg_catalog.pg_proc p ON p.oid = op.oprcode\n");
7140 :
7141 36 : if (access_method_pattern)
7142 : {
7143 36 : if (!validateSQLNamePattern(&buf, access_method_pattern,
7144 : false, false, NULL, "am.amname",
7145 : NULL, NULL,
7146 : &have_where, 1))
7147 18 : goto error_return;
7148 : }
7149 :
7150 18 : if (family_pattern)
7151 : {
7152 12 : if (!validateSQLNamePattern(&buf, family_pattern, have_where, false,
7153 : "nsf.nspname", "of.opfname", NULL, NULL,
7154 : NULL, 3))
7155 0 : goto error_return;
7156 : }
7157 :
7158 18 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2,\n"
7159 : " o.amoplefttype = o.amoprighttype DESC,\n"
7160 : " pg_catalog.format_type(o.amoplefttype, NULL),\n"
7161 : " pg_catalog.format_type(o.amoprighttype, NULL),\n"
7162 : " o.amopstrategy;");
7163 :
7164 18 : res = PSQLexec(buf.data);
7165 18 : termPQExpBuffer(&buf);
7166 18 : if (!res)
7167 0 : return false;
7168 :
7169 18 : myopt.title = _("List of operators of operator families");
7170 18 : myopt.translate_header = true;
7171 18 : myopt.translate_columns = translate_columns;
7172 18 : myopt.n_translate_columns = lengthof(translate_columns);
7173 :
7174 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7175 :
7176 18 : PQclear(res);
7177 18 : return true;
7178 :
7179 18 : error_return:
7180 18 : termPQExpBuffer(&buf);
7181 18 : return false;
7182 : }
7183 :
7184 : /*
7185 : * \dAp
7186 : * Lists support functions of operator families
7187 : *
7188 : * Takes optional regexps to filter by index access method and operator
7189 : * family.
7190 : */
7191 : bool
7192 42 : listOpFamilyFunctions(const char *access_method_pattern,
7193 : const char *family_pattern, bool verbose)
7194 : {
7195 : PQExpBufferData buf;
7196 : PGresult *res;
7197 42 : printQueryOpt myopt = pset.popt;
7198 42 : bool have_where = false;
7199 : static const bool translate_columns[] = {false, false, false, false, false, false};
7200 :
7201 42 : initPQExpBuffer(&buf);
7202 :
7203 42 : printfPQExpBuffer(&buf,
7204 : "SELECT\n"
7205 : " am.amname AS \"%s\",\n"
7206 : " CASE\n"
7207 : " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
7208 : " THEN pg_catalog.format('%%I', of.opfname)\n"
7209 : " ELSE pg_catalog.format('%%I.%%I', ns.nspname, of.opfname)\n"
7210 : " END AS \"%s\",\n"
7211 : " pg_catalog.format_type(ap.amproclefttype, NULL) AS \"%s\",\n"
7212 : " pg_catalog.format_type(ap.amprocrighttype, NULL) AS \"%s\",\n"
7213 : " ap.amprocnum AS \"%s\"\n",
7214 : gettext_noop("AM"),
7215 : gettext_noop("Operator family"),
7216 : gettext_noop("Registered left type"),
7217 : gettext_noop("Registered right type"),
7218 : gettext_noop("Number"));
7219 :
7220 42 : if (!verbose)
7221 30 : appendPQExpBuffer(&buf,
7222 : ", p.proname AS \"%s\"\n",
7223 : gettext_noop("Function"));
7224 : else
7225 12 : appendPQExpBuffer(&buf,
7226 : ", ap.amproc::pg_catalog.regprocedure AS \"%s\"\n",
7227 : gettext_noop("Function"));
7228 :
7229 42 : appendPQExpBufferStr(&buf,
7230 : "FROM pg_catalog.pg_amproc ap\n"
7231 : " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = ap.amprocfamily\n"
7232 : " LEFT JOIN pg_catalog.pg_am am ON am.oid = of.opfmethod\n"
7233 : " LEFT JOIN pg_catalog.pg_namespace ns ON of.opfnamespace = ns.oid\n"
7234 : " LEFT JOIN pg_catalog.pg_proc p ON ap.amproc = p.oid\n");
7235 :
7236 42 : if (access_method_pattern)
7237 : {
7238 42 : if (!validateSQLNamePattern(&buf, access_method_pattern,
7239 : false, false, NULL, "am.amname",
7240 : NULL, NULL,
7241 : &have_where, 1))
7242 18 : goto error_return;
7243 : }
7244 24 : if (family_pattern)
7245 : {
7246 18 : if (!validateSQLNamePattern(&buf, family_pattern, have_where, false,
7247 : "ns.nspname", "of.opfname", NULL, NULL,
7248 : NULL, 3))
7249 0 : goto error_return;
7250 : }
7251 :
7252 24 : appendPQExpBufferStr(&buf, "ORDER BY 1, 2,\n"
7253 : " ap.amproclefttype = ap.amprocrighttype DESC,\n"
7254 : " 3, 4, 5;");
7255 :
7256 24 : res = PSQLexec(buf.data);
7257 24 : termPQExpBuffer(&buf);
7258 24 : if (!res)
7259 0 : return false;
7260 :
7261 24 : myopt.title = _("List of support functions of operator families");
7262 24 : myopt.translate_header = true;
7263 24 : myopt.translate_columns = translate_columns;
7264 24 : myopt.n_translate_columns = lengthof(translate_columns);
7265 :
7266 24 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7267 :
7268 24 : PQclear(res);
7269 24 : return true;
7270 :
7271 18 : error_return:
7272 18 : termPQExpBuffer(&buf);
7273 18 : return false;
7274 : }
7275 :
7276 : /*
7277 : * \dl or \lo_list
7278 : * Lists large objects
7279 : */
7280 : bool
7281 18 : listLargeObjects(bool verbose)
7282 : {
7283 : PQExpBufferData buf;
7284 : PGresult *res;
7285 18 : printQueryOpt myopt = pset.popt;
7286 :
7287 18 : initPQExpBuffer(&buf);
7288 :
7289 18 : printfPQExpBuffer(&buf,
7290 : "SELECT oid as \"%s\",\n"
7291 : " pg_catalog.pg_get_userbyid(lomowner) as \"%s\",\n ",
7292 : gettext_noop("ID"),
7293 : gettext_noop("Owner"));
7294 :
7295 18 : if (verbose)
7296 : {
7297 6 : printACLColumn(&buf, "lomacl");
7298 6 : appendPQExpBufferStr(&buf, ",\n ");
7299 : }
7300 :
7301 18 : appendPQExpBuffer(&buf,
7302 : "pg_catalog.obj_description(oid, 'pg_largeobject') as \"%s\"\n"
7303 : "FROM pg_catalog.pg_largeobject_metadata\n"
7304 : "ORDER BY oid",
7305 : gettext_noop("Description"));
7306 :
7307 18 : res = PSQLexec(buf.data);
7308 18 : termPQExpBuffer(&buf);
7309 18 : if (!res)
7310 0 : return false;
7311 :
7312 18 : myopt.title = _("Large objects");
7313 18 : myopt.translate_header = true;
7314 :
7315 18 : printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7316 :
7317 18 : PQclear(res);
7318 18 : return true;
7319 : }
|