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