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