PostgreSQL Source Code  git master
describe.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool describeAggregates (const char *pattern, bool verbose, bool showSystem)
 
bool describeAccessMethods (const char *pattern, bool verbose)
 
bool describeTablespaces (const char *pattern, bool verbose)
 
bool describeFunctions (const char *functypes, const char *func_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
 
bool describeTypes (const char *pattern, bool verbose, bool showSystem)
 
bool describeOperators (const char *oper_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
 
bool describeRoles (const char *pattern, bool verbose, bool showSystem)
 
bool listDbRoleSettings (const char *pattern, const char *pattern2)
 
bool describeRoleGrants (const char *pattern, bool showSystem)
 
bool permissionsList (const char *pattern, bool showSystem)
 
bool listDefaultACLs (const char *pattern)
 
bool objectDescription (const char *pattern, bool showSystem)
 
bool describeTableDetails (const char *pattern, bool verbose, bool showSystem)
 
bool listTSConfigs (const char *pattern, bool verbose)
 
bool listTSParsers (const char *pattern, bool verbose)
 
bool listTSDictionaries (const char *pattern, bool verbose)
 
bool listTSTemplates (const char *pattern, bool verbose)
 
bool listAllDbs (const char *pattern, bool verbose)
 
bool listTables (const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
 
bool listPartitionedTables (const char *reltypes, const char *pattern, bool verbose)
 
bool listDomains (const char *pattern, bool verbose, bool showSystem)
 
bool listConversions (const char *pattern, bool verbose, bool showSystem)
 
bool describeConfigurationParameters (const char *pattern, bool verbose, bool showSystem)
 
bool listCasts (const char *pattern, bool verbose)
 
bool listCollations (const char *pattern, bool verbose, bool showSystem)
 
bool listSchemas (const char *pattern, bool verbose, bool showSystem)
 
bool listForeignDataWrappers (const char *pattern, bool verbose)
 
bool listForeignServers (const char *pattern, bool verbose)
 
bool listUserMappings (const char *pattern, bool verbose)
 
bool listForeignTables (const char *pattern, bool verbose)
 
bool listLanguages (const char *pattern, bool verbose, bool showSystem)
 
bool listExtensions (const char *pattern)
 
bool listExtensionContents (const char *pattern)
 
bool listExtendedStats (const char *pattern)
 
bool listEventTriggers (const char *pattern, bool verbose)
 
bool listPublications (const char *pattern)
 
bool describePublications (const char *pattern)
 
bool describeSubscriptions (const char *pattern, bool verbose)
 
bool listOperatorClasses (const char *access_method_pattern, const char *type_pattern, bool verbose)
 
bool listOperatorFamilies (const char *access_method_pattern, const char *type_pattern, bool verbose)
 
bool listOpFamilyOperators (const char *access_method_pattern, const char *family_pattern, bool verbose)
 
bool listOpFamilyFunctions (const char *access_method_pattern, const char *family_pattern, bool verbose)
 
bool listLargeObjects (bool verbose)
 

Function Documentation

◆ describeAccessMethods()

bool describeAccessMethods ( const char *  pattern,
bool  verbose 
)

Definition at line 141 of file describe.c.

142 {
144  PGresult *res;
145  printQueryOpt myopt = pset.popt;
146  static const bool translate_columns[] = {false, true, false, false};
147 
148  if (pset.sversion < 90600)
149  {
150  char sverbuf[32];
151 
152  pg_log_error("The server (version %s) does not support access methods.",
154  sverbuf, sizeof(sverbuf)));
155  return true;
156  }
157 
159 
161  "SELECT amname AS \"%s\",\n"
162  " CASE amtype"
163  " WHEN 'i' THEN '%s'"
164  " WHEN 't' THEN '%s'"
165  " END AS \"%s\"",
166  gettext_noop("Name"),
167  gettext_noop("Index"),
168  gettext_noop("Table"),
169  gettext_noop("Type"));
170 
171  if (verbose)
172  {
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 
181  "\nFROM pg_catalog.pg_am\n");
182 
183  if (!validateSQLNamePattern(&buf, pattern, false, false,
184  NULL, "amname", NULL,
185  NULL,
186  NULL, 1))
187  {
189  return false;
190  }
191 
192  appendPQExpBufferStr(&buf, "ORDER BY 1;");
193 
194  res = PSQLexec(buf.data);
196  if (!res)
197  return false;
198 
199  myopt.title = _("List of access methods");
200  myopt.translate_header = true;
201  myopt.translate_columns = translate_columns;
202  myopt.n_translate_columns = lengthof(translate_columns);
203 
204  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
205 
206  PQclear(res);
207  return true;
208 }
PGresult * PSQLexec(const char *query)
Definition: common.c:620
#define gettext_noop(x)
Definition: c.h:1196
#define lengthof(array)
Definition: c.h:788
static bool validateSQLNamePattern(PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, bool *added_clause, int maxparts)
Definition: describe.c:6164
#define _(x)
Definition: elog.c:90
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3549
int verbose
#define pg_log_error(...)
Definition: logging.h:106
static char * buf
Definition: pg_test_fsync.c:73
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
printQueryOpt popt
Definition: settings.h:91
FILE * logfile
Definition: settings.h:120
FILE * queryFout
Definition: settings.h:84
const bool * translate_columns
Definition: print.h:190
char * title
Definition: print.h:187
bool translate_header
Definition: print.h:189
int n_translate_columns
Definition: print.h:192

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ describeAggregates()

bool describeAggregates ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 71 of file describe.c.

72 {
74  PGresult *res;
75  printQueryOpt myopt = pset.popt;
76 
78 
80  "SELECT n.nspname as \"%s\",\n"
81  " p.proname AS \"%s\",\n"
82  " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n"
83  " CASE WHEN p.pronargs = 0\n"
84  " THEN CAST('*' AS pg_catalog.text)\n"
85  " ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"
86  " END AS \"%s\",\n",
87  gettext_noop("Schema"),
88  gettext_noop("Name"),
89  gettext_noop("Result data type"),
90  gettext_noop("Argument data types"));
91 
92  if (pset.sversion >= 110000)
94  " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
95  "FROM pg_catalog.pg_proc p\n"
96  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
97  "WHERE p.prokind = 'a'\n",
98  gettext_noop("Description"));
99  else
101  " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
102  "FROM pg_catalog.pg_proc p\n"
103  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
104  "WHERE p.proisagg\n",
105  gettext_noop("Description"));
106 
107  if (!showSystem && !pattern)
108  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
109  " AND n.nspname <> 'information_schema'\n");
110 
111  if (!validateSQLNamePattern(&buf, pattern, true, false,
112  "n.nspname", "p.proname", NULL,
113  "pg_catalog.pg_function_is_visible(p.oid)",
114  NULL, 3))
115  {
117  return false;
118  }
119 
120  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
121 
122  res = PSQLexec(buf.data);
124  if (!res)
125  return false;
126 
127  myopt.title = _("List of aggregate functions");
128  myopt.translate_header = true;
129 
130  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
131 
132  PQclear(res);
133  return true;
134 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ describeConfigurationParameters()

bool describeConfigurationParameters ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 4546 of file describe.c.

4548 {
4550  PGresult *res;
4551  printQueryOpt myopt = pset.popt;
4552 
4553  initPQExpBuffer(&buf);
4555  "SELECT s.name AS \"%s\", "
4556  "pg_catalog.current_setting(s.name) AS \"%s\"",
4557  gettext_noop("Parameter"),
4558  gettext_noop("Value"));
4559 
4560  if (verbose)
4561  {
4563  ", s.vartype AS \"%s\", s.context AS \"%s\", ",
4564  gettext_noop("Type"),
4565  gettext_noop("Context"));
4566  if (pset.sversion >= 150000)
4567  printACLColumn(&buf, "p.paracl");
4568  else
4569  appendPQExpBuffer(&buf, "NULL AS \"%s\"",
4570  gettext_noop("Access privileges"));
4571  }
4572 
4573  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_settings s\n");
4574 
4575  if (verbose && pset.sversion >= 150000)
4577  " LEFT JOIN pg_catalog.pg_parameter_acl p\n"
4578  " ON pg_catalog.lower(s.name) = p.parname\n");
4579 
4580  if (pattern)
4581  processSQLNamePattern(pset.db, &buf, pattern,
4582  false, false,
4583  NULL, "pg_catalog.lower(s.name)", NULL,
4584  NULL, NULL, NULL);
4585  else
4586  appendPQExpBufferStr(&buf, "WHERE s.source <> 'default' AND\n"
4587  " s.setting IS DISTINCT FROM s.boot_val\n");
4588 
4589  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4590 
4591  res = PSQLexec(buf.data);
4592  termPQExpBuffer(&buf);
4593  if (!res)
4594  return false;
4595 
4596  if (pattern)
4597  myopt.title = _("List of configuration parameters");
4598  else
4599  myopt.title = _("List of non-default configuration parameters");
4600  myopt.translate_header = true;
4601 
4602  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4603 
4604  PQclear(res);
4605  return true;
4606 }
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6659
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
Definition: string_utils.c:891
PGconn * db
Definition: settings.h:82

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, _psqlSettings::db, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and verbose.

Referenced by exec_command_d().

◆ describeFunctions()

bool describeFunctions ( const char *  functypes,
const char *  func_pattern,
char **  arg_patterns,
int  num_arg_patterns,
bool  verbose,
bool  showSystem 
)

Definition at line 288 of file describe.c.

291 {
292  bool showAggregate = strchr(functypes, 'a') != NULL;
293  bool showNormal = strchr(functypes, 'n') != NULL;
294  bool showProcedure = strchr(functypes, 'p') != NULL;
295  bool showTrigger = strchr(functypes, 't') != NULL;
296  bool showWindow = strchr(functypes, 'w') != NULL;
297  bool have_where;
299  PGresult *res;
300  printQueryOpt myopt = pset.popt;
301  static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, false, false, false, false};
302 
303  /* No "Parallel" column before 9.6 */
304  static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, false, false, false, false};
305 
306  if (strlen(functypes) != strspn(functypes, "anptwS+"))
307  {
308  pg_log_error("\\df only takes [anptwS+] as options");
309  return true;
310  }
311 
312  if (showProcedure && pset.sversion < 110000)
313  {
314  char sverbuf[32];
315 
316  pg_log_error("\\df does not take a \"%c\" option with server version %s",
317  'p',
319  sverbuf, sizeof(sverbuf)));
320  return true;
321  }
322 
323  if (!showAggregate && !showNormal && !showProcedure && !showTrigger && !showWindow)
324  {
325  showAggregate = showNormal = showTrigger = showWindow = true;
326  if (pset.sversion >= 110000)
327  showProcedure = true;
328  }
329 
331 
333  "SELECT n.nspname as \"%s\",\n"
334  " p.proname as \"%s\",\n",
335  gettext_noop("Schema"),
336  gettext_noop("Name"));
337 
338  if (pset.sversion >= 110000)
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 'a' THEN '%s'\n"
344  " WHEN 'w' THEN '%s'\n"
345  " WHEN 'p' 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
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  if (verbose)
376  {
378  ",\n CASE\n"
379  " WHEN p.provolatile = 'i' THEN '%s'\n"
380  " WHEN p.provolatile = 's' THEN '%s'\n"
381  " WHEN p.provolatile = 'v' THEN '%s'\n"
382  " END as \"%s\"",
383  gettext_noop("immutable"),
384  gettext_noop("stable"),
385  gettext_noop("volatile"),
386  gettext_noop("Volatility"));
387  if (pset.sversion >= 90600)
389  ",\n CASE\n"
390  " WHEN p.proparallel = 'r' THEN '%s'\n"
391  " WHEN p.proparallel = 's' THEN '%s'\n"
392  " WHEN p.proparallel = 'u' THEN '%s'\n"
393  " END as \"%s\"",
394  gettext_noop("restricted"),
395  gettext_noop("safe"),
396  gettext_noop("unsafe"),
397  gettext_noop("Parallel"));
399  ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
400  ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\"",
401  gettext_noop("Owner"),
402  gettext_noop("definer"),
403  gettext_noop("invoker"),
404  gettext_noop("Security"));
405  appendPQExpBufferStr(&buf, ",\n ");
406  printACLColumn(&buf, "p.proacl");
408  ",\n l.lanname as \"%s\"",
409  gettext_noop("Language"));
411  ",\n CASE WHEN l.lanname IN ('internal', 'c') THEN p.prosrc END as \"%s\"",
412  gettext_noop("Internal name"));
414  ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
415  gettext_noop("Description"));
416  }
417 
419  "\nFROM pg_catalog.pg_proc p"
420  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
421 
422  for (int i = 0; i < num_arg_patterns; i++)
423  {
425  " LEFT JOIN pg_catalog.pg_type t%d ON t%d.oid = p.proargtypes[%d]\n"
426  " LEFT JOIN pg_catalog.pg_namespace nt%d ON nt%d.oid = t%d.typnamespace\n",
427  i, i, i, i, i, i);
428  }
429 
430  if (verbose)
432  " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
433 
434  have_where = false;
435 
436  /* filter by function type, if requested */
437  if (showNormal && showAggregate && showProcedure && showTrigger && showWindow)
438  /* Do nothing */ ;
439  else if (showNormal)
440  {
441  if (!showAggregate)
442  {
443  if (have_where)
444  appendPQExpBufferStr(&buf, " AND ");
445  else
446  {
447  appendPQExpBufferStr(&buf, "WHERE ");
448  have_where = true;
449  }
450  if (pset.sversion >= 110000)
451  appendPQExpBufferStr(&buf, "p.prokind <> 'a'\n");
452  else
453  appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
454  }
455  if (!showProcedure && pset.sversion >= 110000)
456  {
457  if (have_where)
458  appendPQExpBufferStr(&buf, " AND ");
459  else
460  {
461  appendPQExpBufferStr(&buf, "WHERE ");
462  have_where = true;
463  }
464  appendPQExpBufferStr(&buf, "p.prokind <> 'p'\n");
465  }
466  if (!showTrigger)
467  {
468  if (have_where)
469  appendPQExpBufferStr(&buf, " AND ");
470  else
471  {
472  appendPQExpBufferStr(&buf, "WHERE ");
473  have_where = true;
474  }
475  appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
476  }
477  if (!showWindow)
478  {
479  if (have_where)
480  appendPQExpBufferStr(&buf, " AND ");
481  else
482  {
483  appendPQExpBufferStr(&buf, "WHERE ");
484  have_where = true;
485  }
486  if (pset.sversion >= 110000)
487  appendPQExpBufferStr(&buf, "p.prokind <> 'w'\n");
488  else
489  appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
490  }
491  }
492  else
493  {
494  bool needs_or = false;
495 
496  appendPQExpBufferStr(&buf, "WHERE (\n ");
497  have_where = true;
498  /* Note: at least one of these must be true ... */
499  if (showAggregate)
500  {
501  if (pset.sversion >= 110000)
502  appendPQExpBufferStr(&buf, "p.prokind = 'a'\n");
503  else
504  appendPQExpBufferStr(&buf, "p.proisagg\n");
505  needs_or = true;
506  }
507  if (showTrigger)
508  {
509  if (needs_or)
510  appendPQExpBufferStr(&buf, " OR ");
512  "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
513  needs_or = true;
514  }
515  if (showProcedure)
516  {
517  if (needs_or)
518  appendPQExpBufferStr(&buf, " OR ");
519  appendPQExpBufferStr(&buf, "p.prokind = 'p'\n");
520  needs_or = true;
521  }
522  if (showWindow)
523  {
524  if (needs_or)
525  appendPQExpBufferStr(&buf, " OR ");
526  if (pset.sversion >= 110000)
527  appendPQExpBufferStr(&buf, "p.prokind = 'w'\n");
528  else
529  appendPQExpBufferStr(&buf, "p.proiswindow\n");
530  }
531  appendPQExpBufferStr(&buf, " )\n");
532  }
533 
534  if (!validateSQLNamePattern(&buf, func_pattern, have_where, false,
535  "n.nspname", "p.proname", NULL,
536  "pg_catalog.pg_function_is_visible(p.oid)",
537  NULL, 3))
538  goto error_return;
539 
540  for (int i = 0; i < num_arg_patterns; i++)
541  {
542  if (strcmp(arg_patterns[i], "-") != 0)
543  {
544  /*
545  * Match type-name patterns against either internal or external
546  * name, like \dT. Unlike \dT, there seems no reason to
547  * discriminate against arrays or composite types.
548  */
549  char nspname[64];
550  char typname[64];
551  char ft[64];
552  char tiv[64];
553 
554  snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
555  snprintf(typname, sizeof(typname), "t%d.typname", i);
556  snprintf(ft, sizeof(ft),
557  "pg_catalog.format_type(t%d.oid, NULL)", i);
558  snprintf(tiv, sizeof(tiv),
559  "pg_catalog.pg_type_is_visible(t%d.oid)", i);
561  map_typename_pattern(arg_patterns[i]),
562  true, false,
563  nspname, typname, ft, tiv,
564  NULL, 3))
565  goto error_return;
566  }
567  else
568  {
569  /* "-" pattern specifies no such parameter */
570  appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
571  }
572  }
573 
574  if (!showSystem && !func_pattern)
575  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
576  " AND n.nspname <> 'information_schema'\n");
577 
578  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
579 
580  res = PSQLexec(buf.data);
582  if (!res)
583  return false;
584 
585  myopt.title = _("List of functions");
586  myopt.translate_header = true;
587  if (pset.sversion >= 90600)
588  {
589  myopt.translate_columns = translate_columns;
590  myopt.n_translate_columns = lengthof(translate_columns);
591  }
592  else
593  {
594  myopt.translate_columns = translate_columns_pre_96;
595  myopt.n_translate_columns = lengthof(translate_columns_pre_96);
596  }
597 
598  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
599 
600  PQclear(res);
601  return true;
602 
603 error_return:
605  return false;
606 }
static const char * map_typename_pattern(const char *pattern)
Definition: describe.c:720
int i
Definition: isn.c:73
NameData typname
Definition: pg_type.h:41
#define snprintf
Definition: port.h:238

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, i, initPQExpBuffer(), lengthof, _psqlSettings::logfile, map_typename_pattern(), printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, snprintf, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, typname, validateSQLNamePattern(), and verbose.

Referenced by exec_command_dfo().

◆ describeOperators()

bool describeOperators ( const char *  oper_pattern,
char **  arg_patterns,
int  num_arg_patterns,
bool  verbose,
bool  showSystem 
)

Definition at line 770 of file describe.c.

773 {
775  PGresult *res;
776  printQueryOpt myopt = pset.popt;
777 
779 
780  /*
781  * Note: before Postgres 9.1, we did not assign comments to any built-in
782  * operators, preferring to let the comment on the underlying function
783  * suffice. The coalesce() on the obj_description() calls below supports
784  * this convention by providing a fallback lookup of a comment on the
785  * operator's function. Since 9.1 there is a policy that every built-in
786  * operator should have a comment; so the coalesce() is no longer
787  * necessary so far as built-in operators are concerned. We keep it
788  * anyway, for now, because third-party modules may still be following the
789  * old convention.
790  *
791  * The support for postfix operators in this query is dead code as of
792  * Postgres 14, but we need to keep it for as long as we support talking
793  * to pre-v14 servers.
794  */
795 
797  "SELECT n.nspname as \"%s\",\n"
798  " o.oprname AS \"%s\",\n"
799  " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
800  " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
801  " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
802  gettext_noop("Schema"),
803  gettext_noop("Name"),
804  gettext_noop("Left arg type"),
805  gettext_noop("Right arg type"),
806  gettext_noop("Result type"));
807 
808  if (verbose)
810  " o.oprcode AS \"%s\",\n",
811  gettext_noop("Function"));
812 
814  " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
815  " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
816  "FROM pg_catalog.pg_operator o\n"
817  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
818  gettext_noop("Description"));
819 
820  if (num_arg_patterns >= 2)
821  {
822  num_arg_patterns = 2; /* ignore any additional arguments */
824  " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprleft\n"
825  " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n"
826  " LEFT JOIN pg_catalog.pg_type t1 ON t1.oid = o.oprright\n"
827  " LEFT JOIN pg_catalog.pg_namespace nt1 ON nt1.oid = t1.typnamespace\n");
828  }
829  else if (num_arg_patterns == 1)
830  {
832  " LEFT JOIN pg_catalog.pg_type t0 ON t0.oid = o.oprright\n"
833  " LEFT JOIN pg_catalog.pg_namespace nt0 ON nt0.oid = t0.typnamespace\n");
834  }
835 
836  if (!showSystem && !oper_pattern)
837  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
838  " AND n.nspname <> 'information_schema'\n");
839 
840  if (!validateSQLNamePattern(&buf, oper_pattern,
841  !showSystem && !oper_pattern, true,
842  "n.nspname", "o.oprname", NULL,
843  "pg_catalog.pg_operator_is_visible(o.oid)",
844  NULL, 3))
845  goto error_return;
846 
847  if (num_arg_patterns == 1)
848  appendPQExpBufferStr(&buf, " AND o.oprleft = 0\n");
849 
850  for (int i = 0; i < num_arg_patterns; i++)
851  {
852  if (strcmp(arg_patterns[i], "-") != 0)
853  {
854  /*
855  * Match type-name patterns against either internal or external
856  * name, like \dT. Unlike \dT, there seems no reason to
857  * discriminate against arrays or composite types.
858  */
859  char nspname[64];
860  char typname[64];
861  char ft[64];
862  char tiv[64];
863 
864  snprintf(nspname, sizeof(nspname), "nt%d.nspname", i);
865  snprintf(typname, sizeof(typname), "t%d.typname", i);
866  snprintf(ft, sizeof(ft),
867  "pg_catalog.format_type(t%d.oid, NULL)", i);
868  snprintf(tiv, sizeof(tiv),
869  "pg_catalog.pg_type_is_visible(t%d.oid)", i);
871  map_typename_pattern(arg_patterns[i]),
872  true, false,
873  nspname, typname, ft, tiv,
874  NULL, 3))
875  goto error_return;
876  }
877  else
878  {
879  /* "-" pattern specifies no such parameter */
880  appendPQExpBuffer(&buf, " AND t%d.typname IS NULL\n", i);
881  }
882  }
883 
884  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
885 
886  res = PSQLexec(buf.data);
888  if (!res)
889  return false;
890 
891  myopt.title = _("List of operators");
892  myopt.translate_header = true;
893 
894  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
895 
896  PQclear(res);
897  return true;
898 
899 error_return:
901  return false;
902 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, map_typename_pattern(), _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, snprintf, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, typname, validateSQLNamePattern(), and verbose.

Referenced by exec_command_dfo().

◆ describePublications()

bool describePublications ( const char *  pattern)

Definition at line 6339 of file describe.c.

6340 {
6342  int i;
6343  PGresult *res;
6344  bool has_pubtruncate;
6345  bool has_pubviaroot;
6346 
6347  PQExpBufferData title;
6348  printTableContent cont;
6349 
6350  if (pset.sversion < 100000)
6351  {
6352  char sverbuf[32];
6353 
6354  pg_log_error("The server (version %s) does not support publications.",
6356  sverbuf, sizeof(sverbuf)));
6357  return true;
6358  }
6359 
6360  has_pubtruncate = (pset.sversion >= 110000);
6361  has_pubviaroot = (pset.sversion >= 130000);
6362 
6363  initPQExpBuffer(&buf);
6364 
6366  "SELECT oid, pubname,\n"
6367  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
6368  " puballtables, pubinsert, pubupdate, pubdelete");
6369  if (has_pubtruncate)
6371  ", pubtruncate");
6372  if (has_pubviaroot)
6374  ", pubviaroot");
6376  "\nFROM pg_catalog.pg_publication\n");
6377 
6378  if (!validateSQLNamePattern(&buf, pattern, false, false,
6379  NULL, "pubname", NULL,
6380  NULL,
6381  NULL, 1))
6382  {
6383  termPQExpBuffer(&buf);
6384  return false;
6385  }
6386 
6387  appendPQExpBufferStr(&buf, "ORDER BY 2;");
6388 
6389  res = PSQLexec(buf.data);
6390  if (!res)
6391  {
6392  termPQExpBuffer(&buf);
6393  return false;
6394  }
6395 
6396  if (PQntuples(res) == 0)
6397  {
6398  if (!pset.quiet)
6399  {
6400  if (pattern)
6401  pg_log_error("Did not find any publication named \"%s\".",
6402  pattern);
6403  else
6404  pg_log_error("Did not find any publications.");
6405  }
6406 
6407  termPQExpBuffer(&buf);
6408  PQclear(res);
6409  return false;
6410  }
6411 
6412  for (i = 0; i < PQntuples(res); i++)
6413  {
6414  const char align = 'l';
6415  int ncols = 5;
6416  int nrows = 1;
6417  char *pubid = PQgetvalue(res, i, 0);
6418  char *pubname = PQgetvalue(res, i, 1);
6419  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
6420  printTableOpt myopt = pset.popt.topt;
6421 
6422  if (has_pubtruncate)
6423  ncols++;
6424  if (has_pubviaroot)
6425  ncols++;
6426 
6427  initPQExpBuffer(&title);
6428  printfPQExpBuffer(&title, _("Publication %s"), pubname);
6429  printTableInit(&cont, &myopt, title.data, ncols, nrows);
6430 
6431  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
6432  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
6433  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
6434  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
6435  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
6436  if (has_pubtruncate)
6437  printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
6438  if (has_pubviaroot)
6439  printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
6440 
6441  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
6442  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
6443  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
6444  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
6445  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
6446  if (has_pubtruncate)
6447  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
6448  if (has_pubviaroot)
6449  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
6450 
6451  if (!puballtables)
6452  {
6453  /* Get the tables for the specified publication */
6455  "SELECT n.nspname, c.relname");
6456  if (pset.sversion >= 150000)
6457  {
6459  ", pg_get_expr(pr.prqual, c.oid)");
6461  ", (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
6462  " pg_catalog.array_to_string("
6463  " ARRAY(SELECT attname\n"
6464  " FROM\n"
6465  " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
6466  " pg_catalog.pg_attribute\n"
6467  " WHERE attrelid = c.oid AND attnum = prattrs[s]), ', ')\n"
6468  " ELSE NULL END)");
6469  }
6470  else
6472  ", NULL, NULL");
6474  "\nFROM pg_catalog.pg_class c,\n"
6475  " pg_catalog.pg_namespace n,\n"
6476  " pg_catalog.pg_publication_rel pr\n"
6477  "WHERE c.relnamespace = n.oid\n"
6478  " AND c.oid = pr.prrelid\n"
6479  " AND pr.prpubid = '%s'\n"
6480  "ORDER BY 1,2", pubid);
6481  if (!addFooterToPublicationDesc(&buf, _("Tables:"), false, &cont))
6482  goto error_return;
6483 
6484  if (pset.sversion >= 150000)
6485  {
6486  /* Get the schemas for the specified publication */
6488  "SELECT n.nspname\n"
6489  "FROM pg_catalog.pg_namespace n\n"
6490  " JOIN pg_catalog.pg_publication_namespace pn ON n.oid = pn.pnnspid\n"
6491  "WHERE pn.pnpubid = '%s'\n"
6492  "ORDER BY 1", pubid);
6493  if (!addFooterToPublicationDesc(&buf, _("Tables from schemas:"),
6494  true, &cont))
6495  goto error_return;
6496  }
6497  }
6498 
6499  printTable(&cont, pset.queryFout, false, pset.logfile);
6500  printTableCleanup(&cont);
6501 
6502  termPQExpBuffer(&title);
6503  }
6504 
6505  termPQExpBuffer(&buf);
6506  PQclear(res);
6507 
6508  return true;
6509 
6510 error_return:
6511  printTableCleanup(&cont);
6512  PQclear(res);
6513  termPQExpBuffer(&buf);
6514  termPQExpBuffer(&title);
6515  return false;
6516 }
static bool addFooterToPublicationDesc(PQExpBuffer buf, const char *footermsg, bool as_schema, printTableContent *const cont)
Definition: describe.c:6293
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3172
void printTableCleanup(printTableContent *const content)
Definition: print.c:3353
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3260
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3443
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3220
printTableOpt topt
Definition: print.h:185

References _, addFooterToPublicationDesc(), appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, formatPGVersionNumber(), gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, pg_log_error, _psqlSettings::popt, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), printTable(), printTableAddCell(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::topt, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ describeRoleGrants()

bool describeRoleGrants ( const char *  pattern,
bool  showSystem 
)

Definition at line 3830 of file describe.c.

3831 {
3833  PGresult *res;
3834  printQueryOpt myopt = pset.popt;
3835 
3836  initPQExpBuffer(&buf);
3838  "SELECT m.rolname AS \"%s\", r.rolname AS \"%s\",\n"
3839  " pg_catalog.concat_ws(', ',\n",
3840  gettext_noop("Role name"),
3841  gettext_noop("Member of"));
3842 
3843  if (pset.sversion >= 160000)
3845  " CASE WHEN pam.admin_option THEN 'ADMIN' END,\n"
3846  " CASE WHEN pam.inherit_option THEN 'INHERIT' END,\n"
3847  " CASE WHEN pam.set_option THEN 'SET' END\n");
3848  else
3850  " CASE WHEN pam.admin_option THEN 'ADMIN' END,\n"
3851  " CASE WHEN m.rolinherit THEN 'INHERIT' END,\n"
3852  " 'SET'\n");
3853 
3855  " ) AS \"%s\",\n"
3856  " g.rolname AS \"%s\"\n",
3857  gettext_noop("Options"),
3858  gettext_noop("Grantor"));
3859 
3861  "FROM pg_catalog.pg_roles m\n"
3862  " JOIN pg_catalog.pg_auth_members pam ON (pam.member = m.oid)\n"
3863  " LEFT JOIN pg_catalog.pg_roles r ON (pam.roleid = r.oid)\n"
3864  " LEFT JOIN pg_catalog.pg_roles g ON (pam.grantor = g.oid)\n");
3865 
3866  if (!showSystem && !pattern)
3867  appendPQExpBufferStr(&buf, "WHERE m.rolname !~ '^pg_'\n");
3868 
3869  if (!validateSQLNamePattern(&buf, pattern, false, false,
3870  NULL, "m.rolname", NULL, NULL,
3871  NULL, 1))
3872  {
3873  termPQExpBuffer(&buf);
3874  return false;
3875  }
3876 
3877  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;\n");
3878 
3879  res = PSQLexec(buf.data);
3880  termPQExpBuffer(&buf);
3881  if (!res)
3882  return false;
3883 
3884  myopt.title = _("List of role grants");
3885  myopt.translate_header = true;
3886 
3887  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3888 
3889  PQclear(res);
3890  return true;
3891 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ describeRoles()

bool describeRoles ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3614 of file describe.c.

3615 {
3617  PGresult *res;
3618  printTableContent cont;
3619  printTableOpt myopt = pset.popt.topt;
3620  int ncols = 2;
3621  int nrows = 0;
3622  int i;
3623  int conns;
3624  const char align = 'l';
3625  char **attr;
3626 
3627  myopt.default_footer = false;
3628 
3629  initPQExpBuffer(&buf);
3630 
3632  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3633  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3634  " r.rolconnlimit, r.rolvaliduntil");
3635 
3636  if (verbose)
3637  {
3638  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3639  ncols++;
3640  }
3641  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3642 
3643  if (pset.sversion >= 90500)
3644  {
3645  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3646  }
3647 
3648  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3649 
3650  if (!showSystem && !pattern)
3651  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3652 
3653  if (!validateSQLNamePattern(&buf, pattern, false, false,
3654  NULL, "r.rolname", NULL, NULL,
3655  NULL, 1))
3656  {
3657  termPQExpBuffer(&buf);
3658  return false;
3659  }
3660 
3661  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3662 
3663  res = PSQLexec(buf.data);
3664  if (!res)
3665  return false;
3666 
3667  nrows = PQntuples(res);
3668  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3669 
3670  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3671 
3672  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3673  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3674 
3675  if (verbose)
3676  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3677 
3678  for (i = 0; i < nrows; i++)
3679  {
3680  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3681 
3683  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3684  add_role_attribute(&buf, _("Superuser"));
3685 
3686  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3687  add_role_attribute(&buf, _("No inheritance"));
3688 
3689  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3690  add_role_attribute(&buf, _("Create role"));
3691 
3692  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3693  add_role_attribute(&buf, _("Create DB"));
3694 
3695  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3696  add_role_attribute(&buf, _("Cannot login"));
3697 
3698  if (strcmp(PQgetvalue(res, i, (verbose ? 9 : 8)), "t") == 0)
3699  add_role_attribute(&buf, _("Replication"));
3700 
3701  if (pset.sversion >= 90500)
3702  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3703  add_role_attribute(&buf, _("Bypass RLS"));
3704 
3705  conns = atoi(PQgetvalue(res, i, 6));
3706  if (conns >= 0)
3707  {
3708  if (buf.len > 0)
3709  appendPQExpBufferChar(&buf, '\n');
3710 
3711  if (conns == 0)
3712  appendPQExpBufferStr(&buf, _("No connections"));
3713  else
3714  appendPQExpBuffer(&buf, ngettext("%d connection",
3715  "%d connections",
3716  conns),
3717  conns);
3718  }
3719 
3720  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3721  {
3722  if (buf.len > 0)
3723  appendPQExpBufferChar(&buf, '\n');
3724  appendPQExpBufferStr(&buf, _("Password valid until "));
3726  }
3727 
3728  attr[i] = pg_strdup(buf.data);
3729 
3730  printTableAddCell(&cont, attr[i], false, false);
3731 
3732  if (verbose)
3733  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3734  }
3735  termPQExpBuffer(&buf);
3736 
3737  printTable(&cont, pset.queryFout, false, pset.logfile);
3738  printTableCleanup(&cont);
3739 
3740  for (i = 0; i < nrows; i++)
3741  free(attr[i]);
3742  free(attr);
3743 
3744  PQclear(res);
3745  return true;
3746 }
#define ngettext(s, p, n)
Definition: c.h:1181
static void add_role_attribute(PQExpBuffer buf, const char *const str)
Definition: describe.c:3749
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
static IsoConnInfo * conns
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
bool default_footer
Definition: print.h:129

References _, add_role_attribute(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), buf, conns, printTableOpt::default_footer, free, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, ngettext, pg_malloc0(), pg_strdup(), _psqlSettings::popt, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), printTable(), printTableAddCell(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, PSQLexec(), _psqlSettings::queryFout, res, resetPQExpBuffer(), _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::topt, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ describeSubscriptions()

bool describeSubscriptions ( const char *  pattern,
bool  verbose 
)

Definition at line 6525 of file describe.c.

6526 {
6528  PGresult *res;
6529  printQueryOpt myopt = pset.popt;
6530  static const bool translate_columns[] = {false, false, false, false,
6531  false, false, false, false, false, false, false, false, false, false,
6532  false};
6533 
6534  if (pset.sversion < 100000)
6535  {
6536  char sverbuf[32];
6537 
6538  pg_log_error("The server (version %s) does not support subscriptions.",
6540  sverbuf, sizeof(sverbuf)));
6541  return true;
6542  }
6543 
6544  initPQExpBuffer(&buf);
6545 
6547  "SELECT subname AS \"%s\"\n"
6548  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
6549  ", subenabled AS \"%s\"\n"
6550  ", subpublications AS \"%s\"\n",
6551  gettext_noop("Name"),
6552  gettext_noop("Owner"),
6553  gettext_noop("Enabled"),
6554  gettext_noop("Publication"));
6555 
6556  if (verbose)
6557  {
6558  /* Binary mode and streaming are only supported in v14 and higher */
6559  if (pset.sversion >= 140000)
6560  {
6562  ", subbinary AS \"%s\"\n",
6563  gettext_noop("Binary"));
6564 
6565  if (pset.sversion >= 160000)
6567  ", (CASE substream\n"
6568  " WHEN 'f' THEN 'off'\n"
6569  " WHEN 't' THEN 'on'\n"
6570  " WHEN 'p' THEN 'parallel'\n"
6571  " END) AS \"%s\"\n",
6572  gettext_noop("Streaming"));
6573  else
6575  ", substream AS \"%s\"\n",
6576  gettext_noop("Streaming"));
6577  }
6578 
6579  /* Two_phase and disable_on_error are only supported in v15 and higher */
6580  if (pset.sversion >= 150000)
6582  ", subtwophasestate AS \"%s\"\n"
6583  ", subdisableonerr AS \"%s\"\n",
6584  gettext_noop("Two-phase commit"),
6585  gettext_noop("Disable on error"));
6586 
6587  if (pset.sversion >= 160000)
6589  ", suborigin AS \"%s\"\n"
6590  ", subpasswordrequired AS \"%s\"\n"
6591  ", subrunasowner AS \"%s\"\n",
6592  gettext_noop("Origin"),
6593  gettext_noop("Password required"),
6594  gettext_noop("Run as owner?"));
6595 
6596  if (pset.sversion >= 170000)
6598  ", subfailover AS \"%s\"\n",
6599  gettext_noop("Failover"));
6600 
6602  ", subsynccommit AS \"%s\"\n"
6603  ", subconninfo AS \"%s\"\n",
6604  gettext_noop("Synchronous commit"),
6605  gettext_noop("Conninfo"));
6606 
6607  /* Skip LSN is only supported in v15 and higher */
6608  if (pset.sversion >= 150000)
6610  ", subskiplsn AS \"%s\"\n",
6611  gettext_noop("Skip LSN"));
6612  }
6613 
6614  /* Only display subscriptions in current database. */
6616  "FROM pg_catalog.pg_subscription\n"
6617  "WHERE subdbid = (SELECT oid\n"
6618  " FROM pg_catalog.pg_database\n"
6619  " WHERE datname = pg_catalog.current_database())");
6620 
6621  if (!validateSQLNamePattern(&buf, pattern, true, false,
6622  NULL, "subname", NULL,
6623  NULL,
6624  NULL, 1))
6625  {
6626  termPQExpBuffer(&buf);
6627  return false;
6628  }
6629 
6630  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6631 
6632  res = PSQLexec(buf.data);
6633  termPQExpBuffer(&buf);
6634  if (!res)
6635  return false;
6636 
6637  myopt.title = _("List of subscriptions");
6638  myopt.translate_header = true;
6639  myopt.translate_columns = translate_columns;
6640  myopt.n_translate_columns = lengthof(translate_columns);
6641 
6642  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6643 
6644  PQclear(res);
6645  return true;
6646 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ describeTableDetails()

bool describeTableDetails ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 1445 of file describe.c.

1446 {
1448  PGresult *res;
1449  int i;
1450 
1451  initPQExpBuffer(&buf);
1452 
1454  "SELECT c.oid,\n"
1455  " n.nspname,\n"
1456  " c.relname\n"
1457  "FROM pg_catalog.pg_class c\n"
1458  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1459 
1460  if (!showSystem && !pattern)
1461  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1462  " AND n.nspname <> 'information_schema'\n");
1463 
1464  if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1465  "n.nspname", "c.relname", NULL,
1466  "pg_catalog.pg_table_is_visible(c.oid)",
1467  NULL, 3))
1468  {
1469  termPQExpBuffer(&buf);
1470  return false;
1471  }
1472 
1473  appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1474 
1475  res = PSQLexec(buf.data);
1476  termPQExpBuffer(&buf);
1477  if (!res)
1478  return false;
1479 
1480  if (PQntuples(res) == 0)
1481  {
1482  if (!pset.quiet)
1483  {
1484  if (pattern)
1485  pg_log_error("Did not find any relation named \"%s\".",
1486  pattern);
1487  else
1488  pg_log_error("Did not find any relations.");
1489  }
1490  PQclear(res);
1491  return false;
1492  }
1493 
1494  for (i = 0; i < PQntuples(res); i++)
1495  {
1496  const char *oid;
1497  const char *nspname;
1498  const char *relname;
1499 
1500  oid = PQgetvalue(res, i, 0);
1501  nspname = PQgetvalue(res, i, 1);
1502  relname = PQgetvalue(res, i, 2);
1503 
1504  if (!describeOneTableDetails(nspname, relname, oid, verbose))
1505  {
1506  PQclear(res);
1507  return false;
1508  }
1509  if (cancel_pressed)
1510  {
1511  PQclear(res);
1512  return false;
1513  }
1514  }
1515 
1516  PQclear(res);
1517  return true;
1518 }
static bool describeOneTableDetails(const char *schemaname, const char *relationname, const char *oid, bool verbose)
Definition: describe.c:1528
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
NameData relname
Definition: pg_class.h:38

References appendPQExpBufferStr(), buf, cancel_pressed, describeOneTableDetails(), i, initPQExpBuffer(), pg_log_error, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), pset, PSQLexec(), _psqlSettings::quiet, relname, res, termPQExpBuffer(), validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ describeTablespaces()

bool describeTablespaces ( const char *  pattern,
bool  verbose 
)

Definition at line 215 of file describe.c.

216 {
218  PGresult *res;
219  printQueryOpt myopt = pset.popt;
220 
222 
224  "SELECT spcname AS \"%s\",\n"
225  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
226  " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
227  gettext_noop("Name"),
228  gettext_noop("Owner"),
229  gettext_noop("Location"));
230 
231  if (verbose)
232  {
233  appendPQExpBufferStr(&buf, ",\n ");
234  printACLColumn(&buf, "spcacl");
236  ",\n spcoptions AS \"%s\""
237  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\""
238  ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
239  gettext_noop("Options"),
240  gettext_noop("Size"),
241  gettext_noop("Description"));
242  }
243 
245  "\nFROM pg_catalog.pg_tablespace\n");
246 
247  if (!validateSQLNamePattern(&buf, pattern, false, false,
248  NULL, "spcname", NULL,
249  NULL,
250  NULL, 1))
251  {
253  return false;
254  }
255 
256  appendPQExpBufferStr(&buf, "ORDER BY 1;");
257 
258  res = PSQLexec(buf.data);
260  if (!res)
261  return false;
262 
263  myopt.title = _("List of tablespaces");
264  myopt.translate_header = true;
265 
266  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
267 
268  PQclear(res);
269  return true;
270 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ describeTypes()

bool describeTypes ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 615 of file describe.c.

616 {
618  PGresult *res;
619  printQueryOpt myopt = pset.popt;
620 
622 
624  "SELECT n.nspname as \"%s\",\n"
625  " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
626  gettext_noop("Schema"),
627  gettext_noop("Name"));
628  if (verbose)
629  {
631  " t.typname AS \"%s\",\n"
632  " CASE WHEN t.typrelid != 0\n"
633  " THEN CAST('tuple' AS pg_catalog.text)\n"
634  " WHEN t.typlen < 0\n"
635  " THEN CAST('var' AS pg_catalog.text)\n"
636  " ELSE CAST(t.typlen AS pg_catalog.text)\n"
637  " END AS \"%s\",\n"
638  " pg_catalog.array_to_string(\n"
639  " ARRAY(\n"
640  " SELECT e.enumlabel\n"
641  " FROM pg_catalog.pg_enum e\n"
642  " WHERE e.enumtypid = t.oid\n"
643  " ORDER BY e.enumsortorder\n"
644  " ),\n"
645  " E'\\n'\n"
646  " ) AS \"%s\",\n"
647  " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
648  gettext_noop("Internal name"),
649  gettext_noop("Size"),
650  gettext_noop("Elements"),
651  gettext_noop("Owner"));
652  printACLColumn(&buf, "t.typacl");
653  appendPQExpBufferStr(&buf, ",\n ");
654  }
655 
657  " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
658  gettext_noop("Description"));
659 
660  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
661  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
662 
663  /*
664  * do not include complex types (typrelid!=0) unless they are standalone
665  * composite types
666  */
667  appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
668  appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
669  " FROM pg_catalog.pg_class c "
670  "WHERE c.oid = t.typrelid))\n");
671 
672  /*
673  * do not include array types unless the pattern contains []
674  */
675  if (pattern == NULL || strstr(pattern, "[]") == NULL)
676  appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
677 
678  if (!showSystem && !pattern)
679  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
680  " AND n.nspname <> 'information_schema'\n");
681 
682  /* Match name pattern against either internal or external name */
684  true, false,
685  "n.nspname", "t.typname",
686  "pg_catalog.format_type(t.oid, NULL)",
687  "pg_catalog.pg_type_is_visible(t.oid)",
688  NULL, 3))
689  {
691  return false;
692  }
693 
694  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
695 
696  res = PSQLexec(buf.data);
698  if (!res)
699  return false;
700 
701  myopt.title = _("List of data types");
702  myopt.translate_header = true;
703 
704  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
705 
706  PQclear(res);
707  return true;
708 }
#define CppAsString2(x)
Definition: c.h:327

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, CppAsString2, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, map_typename_pattern(), _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listAllDbs()

bool listAllDbs ( const char *  pattern,
bool  verbose 
)

Definition at line 911 of file describe.c.

912 {
913  PGresult *res;
915  printQueryOpt myopt = pset.popt;
916 
918 
920  "SELECT\n"
921  " d.datname as \"%s\",\n"
922  " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
923  " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
924  gettext_noop("Name"),
925  gettext_noop("Owner"),
926  gettext_noop("Encoding"));
927  if (pset.sversion >= 150000)
929  " CASE d.datlocprovider WHEN 'b' THEN 'builtin' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\",\n",
930  gettext_noop("Locale Provider"));
931  else
933  " 'libc' AS \"%s\",\n",
934  gettext_noop("Locale Provider"));
936  " d.datcollate as \"%s\",\n"
937  " d.datctype as \"%s\",\n",
938  gettext_noop("Collate"),
939  gettext_noop("Ctype"));
940  if (pset.sversion >= 170000)
942  " d.datlocale as \"%s\",\n",
943  gettext_noop("Locale"));
944  else if (pset.sversion >= 150000)
946  " d.daticulocale as \"%s\",\n",
947  gettext_noop("Locale"));
948  else
950  " NULL as \"%s\",\n",
951  gettext_noop("Locale"));
952  if (pset.sversion >= 160000)
954  " d.daticurules as \"%s\",\n",
955  gettext_noop("ICU Rules"));
956  else
958  " NULL as \"%s\",\n",
959  gettext_noop("ICU Rules"));
960  appendPQExpBufferStr(&buf, " ");
961  printACLColumn(&buf, "d.datacl");
962  if (verbose)
964  ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
965  " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
966  " ELSE 'No Access'\n"
967  " END as \"%s\""
968  ",\n t.spcname as \"%s\""
969  ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
970  gettext_noop("Size"),
971  gettext_noop("Tablespace"),
972  gettext_noop("Description"));
974  "\nFROM pg_catalog.pg_database d\n");
975  if (verbose)
977  " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
978 
979  if (pattern)
980  {
981  if (!validateSQLNamePattern(&buf, pattern, false, false,
982  NULL, "d.datname", NULL, NULL,
983  NULL, 1))
984  {
986  return false;
987  }
988  }
989 
990  appendPQExpBufferStr(&buf, "ORDER BY 1;");
991  res = PSQLexec(buf.data);
993  if (!res)
994  return false;
995 
996  myopt.title = _("List of databases");
997  myopt.translate_header = true;
998 
999  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1000 
1001  PQclear(res);
1002  return true;
1003 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_list(), and main().

◆ listCasts()

bool listCasts ( const char *  pattern,
bool  verbose 
)

Definition at line 4790 of file describe.c.

4791 {
4793  PGresult *res;
4794  printQueryOpt myopt = pset.popt;
4795  static const bool translate_columns[] = {false, false, false, true, false};
4796 
4797  initPQExpBuffer(&buf);
4798 
4800  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
4801  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n",
4802  gettext_noop("Source type"),
4803  gettext_noop("Target type"));
4804 
4805  /*
4806  * We don't attempt to localize '(binary coercible)' or '(with inout)',
4807  * because there's too much risk of gettext translating a function name
4808  * that happens to match some string in the PO database.
4809  */
4811  " CASE WHEN c.castmethod = '%c' THEN '(binary coercible)'\n"
4812  " WHEN c.castmethod = '%c' THEN '(with inout)'\n"
4813  " ELSE p.proname\n"
4814  " END AS \"%s\",\n",
4815  COERCION_METHOD_BINARY,
4816  COERCION_METHOD_INOUT,
4817  gettext_noop("Function"));
4818 
4820  " CASE WHEN c.castcontext = '%c' THEN '%s'\n"
4821  " WHEN c.castcontext = '%c' THEN '%s'\n"
4822  " ELSE '%s'\n"
4823  " END AS \"%s\"",
4824  COERCION_CODE_EXPLICIT,
4825  gettext_noop("no"),
4826  COERCION_CODE_ASSIGNMENT,
4827  gettext_noop("in assignment"),
4828  gettext_noop("yes"),
4829  gettext_noop("Implicit?"));
4830 
4831  if (verbose)
4833  ",\n d.description AS \"%s\"",
4834  gettext_noop("Description"));
4835 
4836  /*
4837  * We need a left join to pg_proc for binary casts; the others are just
4838  * paranoia.
4839  */
4841  "\nFROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
4842  " ON c.castfunc = p.oid\n"
4843  " LEFT JOIN pg_catalog.pg_type ts\n"
4844  " ON c.castsource = ts.oid\n"
4845  " LEFT JOIN pg_catalog.pg_namespace ns\n"
4846  " ON ns.oid = ts.typnamespace\n"
4847  " LEFT JOIN pg_catalog.pg_type tt\n"
4848  " ON c.casttarget = tt.oid\n"
4849  " LEFT JOIN pg_catalog.pg_namespace nt\n"
4850  " ON nt.oid = tt.typnamespace\n");
4851 
4852  if (verbose)
4854  " LEFT JOIN pg_catalog.pg_description d\n"
4855  " ON d.classoid = c.tableoid AND d.objoid = "
4856  "c.oid AND d.objsubid = 0\n");
4857 
4858  appendPQExpBufferStr(&buf, "WHERE ( (true");
4859 
4860  /*
4861  * Match name pattern against either internal or external name of either
4862  * castsource or casttarget
4863  */
4864  if (!validateSQLNamePattern(&buf, pattern, true, false,
4865  "ns.nspname", "ts.typname",
4866  "pg_catalog.format_type(ts.oid, NULL)",
4867  "pg_catalog.pg_type_is_visible(ts.oid)",
4868  NULL, 3))
4869  goto error_return;
4870 
4871  appendPQExpBufferStr(&buf, ") OR (true");
4872 
4873  if (!validateSQLNamePattern(&buf, pattern, true, false,
4874  "nt.nspname", "tt.typname",
4875  "pg_catalog.format_type(tt.oid, NULL)",
4876  "pg_catalog.pg_type_is_visible(tt.oid)",
4877  NULL, 3))
4878  goto error_return;
4879 
4880  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
4881 
4882  res = PSQLexec(buf.data);
4883  termPQExpBuffer(&buf);
4884  if (!res)
4885  return false;
4886 
4887  myopt.title = _("List of casts");
4888  myopt.translate_header = true;
4889  myopt.translate_columns = translate_columns;
4890  myopt.n_translate_columns = lengthof(translate_columns);
4891 
4892  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4893 
4894  PQclear(res);
4895  return true;
4896 
4897 error_return:
4898  termPQExpBuffer(&buf);
4899  return false;
4900 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listCollations()

bool listCollations ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 4908 of file describe.c.

4909 {
4911  PGresult *res;
4912  printQueryOpt myopt = pset.popt;
4913  static const bool translate_columns[] = {false, false, false, false, false, false, false, true, false};
4914 
4915  initPQExpBuffer(&buf);
4916 
4918  "SELECT\n"
4919  " n.nspname AS \"%s\",\n"
4920  " c.collname AS \"%s\",\n",
4921  gettext_noop("Schema"),
4922  gettext_noop("Name"));
4923 
4924  if (pset.sversion >= 100000)
4926  " CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'b' THEN 'builtin' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\",\n",
4927  gettext_noop("Provider"));
4928  else
4930  " 'libc' AS \"%s\",\n",
4931  gettext_noop("Provider"));
4932 
4934  " c.collcollate AS \"%s\",\n"
4935  " c.collctype AS \"%s\",\n",
4936  gettext_noop("Collate"),
4937  gettext_noop("Ctype"));
4938 
4939  if (pset.sversion >= 170000)
4941  " c.colllocale AS \"%s\",\n",
4942  gettext_noop("Locale"));
4943  else if (pset.sversion >= 150000)
4945  " c.colliculocale AS \"%s\",\n",
4946  gettext_noop("Locale"));
4947  else
4949  " c.collcollate AS \"%s\",\n",
4950  gettext_noop("Locale"));
4951 
4952  if (pset.sversion >= 160000)
4954  " c.collicurules AS \"%s\",\n",
4955  gettext_noop("ICU Rules"));
4956  else
4958  " NULL AS \"%s\",\n",
4959  gettext_noop("ICU Rules"));
4960 
4961  if (pset.sversion >= 120000)
4963  " CASE WHEN c.collisdeterministic THEN '%s' ELSE '%s' END AS \"%s\"",
4964  gettext_noop("yes"), gettext_noop("no"),
4965  gettext_noop("Deterministic?"));
4966  else
4968  " '%s' AS \"%s\"",
4969  gettext_noop("yes"),
4970  gettext_noop("Deterministic?"));
4971 
4972  if (verbose)
4974  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
4975  gettext_noop("Description"));
4976 
4978  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
4979  "WHERE n.oid = c.collnamespace\n");
4980 
4981  if (!showSystem && !pattern)
4982  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4983  " AND n.nspname <> 'information_schema'\n");
4984 
4985  /*
4986  * Hide collations that aren't usable in the current database's encoding.
4987  * If you think to change this, note that pg_collation_is_visible rejects
4988  * unusable collations, so you will need to hack name pattern processing
4989  * somehow to avoid inconsistent behavior.
4990  */
4991  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
4992 
4993  if (!validateSQLNamePattern(&buf, pattern, true, false,
4994  "n.nspname", "c.collname", NULL,
4995  "pg_catalog.pg_collation_is_visible(c.oid)",
4996  NULL, 3))
4997  {
4998  termPQExpBuffer(&buf);
4999  return false;
5000  }
5001 
5002  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5003 
5004  res = PSQLexec(buf.data);
5005  termPQExpBuffer(&buf);
5006  if (!res)
5007  return false;
5008 
5009  myopt.title = _("List of collations");
5010  myopt.translate_header = true;
5011  myopt.translate_columns = translate_columns;
5012  myopt.n_translate_columns = lengthof(translate_columns);
5013 
5014  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5015 
5016  PQclear(res);
5017  return true;
5018 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listConversions()

bool listConversions ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 4466 of file describe.c.

4467 {
4469  PGresult *res;
4470  printQueryOpt myopt = pset.popt;
4471  static const bool translate_columns[] =
4472  {false, false, false, false, true, false};
4473 
4474  initPQExpBuffer(&buf);
4475 
4477  "SELECT n.nspname AS \"%s\",\n"
4478  " c.conname AS \"%s\",\n"
4479  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
4480  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
4481  " CASE WHEN c.condefault THEN '%s'\n"
4482  " ELSE '%s' END AS \"%s\"",
4483  gettext_noop("Schema"),
4484  gettext_noop("Name"),
4485  gettext_noop("Source"),
4486  gettext_noop("Destination"),
4487  gettext_noop("yes"), gettext_noop("no"),
4488  gettext_noop("Default?"));
4489 
4490  if (verbose)
4492  ",\n d.description AS \"%s\"",
4493  gettext_noop("Description"));
4494 
4496  "\nFROM pg_catalog.pg_conversion c\n"
4497  " JOIN pg_catalog.pg_namespace n "
4498  "ON n.oid = c.connamespace\n");
4499 
4500  if (verbose)
4502  "LEFT JOIN pg_catalog.pg_description d "
4503  "ON d.classoid = c.tableoid\n"
4504  " AND d.objoid = c.oid "
4505  "AND d.objsubid = 0\n");
4506 
4507  appendPQExpBufferStr(&buf, "WHERE true\n");
4508 
4509  if (!showSystem && !pattern)
4510  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4511  " AND n.nspname <> 'information_schema'\n");
4512 
4513  if (!validateSQLNamePattern(&buf, pattern, true, false,
4514  "n.nspname", "c.conname", NULL,
4515  "pg_catalog.pg_conversion_is_visible(c.oid)",
4516  NULL, 3))
4517  {
4518  termPQExpBuffer(&buf);
4519  return false;
4520  }
4521 
4522  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4523 
4524  res = PSQLexec(buf.data);
4525  termPQExpBuffer(&buf);
4526  if (!res)
4527  return false;
4528 
4529  myopt.title = _("List of conversions");
4530  myopt.translate_header = true;
4531  myopt.translate_columns = translate_columns;
4532  myopt.n_translate_columns = lengthof(translate_columns);
4533 
4534  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4535 
4536  PQclear(res);
4537  return true;
4538 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listDbRoleSettings()

bool listDbRoleSettings ( const char *  pattern,
const char *  pattern2 
)

Definition at line 3761 of file describe.c.

3762 {
3764  PGresult *res;
3765  printQueryOpt myopt = pset.popt;
3766  bool havewhere;
3767 
3768  initPQExpBuffer(&buf);
3769 
3770  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3771  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3772  "FROM pg_catalog.pg_db_role_setting s\n"
3773  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3774  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3775  gettext_noop("Role"),
3776  gettext_noop("Database"),
3777  gettext_noop("Settings"));
3778  if (!validateSQLNamePattern(&buf, pattern, false, false,
3779  NULL, "r.rolname", NULL, NULL, &havewhere, 1))
3780  goto error_return;
3781  if (!validateSQLNamePattern(&buf, pattern2, havewhere, false,
3782  NULL, "d.datname", NULL, NULL,
3783  NULL, 1))
3784  goto error_return;
3785  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3786 
3787  res = PSQLexec(buf.data);
3788  termPQExpBuffer(&buf);
3789  if (!res)
3790  return false;
3791 
3792  /*
3793  * Most functions in this file are content to print an empty table when
3794  * there are no matching objects. We intentionally deviate from that
3795  * here, but only in !quiet mode, because of the possibility that the user
3796  * is confused about what the two pattern arguments mean.
3797  */
3798  if (PQntuples(res) == 0 && !pset.quiet)
3799  {
3800  if (pattern && pattern2)
3801  pg_log_error("Did not find any settings for role \"%s\" and database \"%s\".",
3802  pattern, pattern2);
3803  else if (pattern)
3804  pg_log_error("Did not find any settings for role \"%s\".",
3805  pattern);
3806  else
3807  pg_log_error("Did not find any settings.");
3808  }
3809  else
3810  {
3811  myopt.title = _("List of settings");
3812  myopt.translate_header = true;
3813 
3814  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3815  }
3816 
3817  PQclear(res);
3818  return true;
3819 
3820 error_return:
3821  termPQExpBuffer(&buf);
3822  return false;
3823 }

References _, appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, pg_log_error, _psqlSettings::popt, PQclear(), PQntuples(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ listDefaultACLs()

bool listDefaultACLs ( const char *  pattern)

Definition at line 1175 of file describe.c.

1176 {
1178  PGresult *res;
1179  printQueryOpt myopt = pset.popt;
1180  static const bool translate_columns[] = {false, false, true, false};
1181 
1182  initPQExpBuffer(&buf);
1183 
1185  "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1186  " n.nspname AS \"%s\",\n"
1187  " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"
1188  " ",
1189  gettext_noop("Owner"),
1190  gettext_noop("Schema"),
1191  DEFACLOBJ_RELATION,
1192  gettext_noop("table"),
1193  DEFACLOBJ_SEQUENCE,
1194  gettext_noop("sequence"),
1195  DEFACLOBJ_FUNCTION,
1196  gettext_noop("function"),
1197  DEFACLOBJ_TYPE,
1198  gettext_noop("type"),
1199  DEFACLOBJ_NAMESPACE,
1200  gettext_noop("schema"),
1201  gettext_noop("Type"));
1202 
1203  printACLColumn(&buf, "d.defaclacl");
1204 
1205  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1206  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1207 
1208  if (!validateSQLNamePattern(&buf, pattern, false, false,
1209  NULL,
1210  "n.nspname",
1211  "pg_catalog.pg_get_userbyid(d.defaclrole)",
1212  NULL,
1213  NULL, 3))
1214  goto error_return;
1215 
1216  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1217 
1218  res = PSQLexec(buf.data);
1219  if (!res)
1220  goto error_return;
1221 
1222  printfPQExpBuffer(&buf, _("Default access privileges"));
1223  myopt.title = buf.data;
1224  myopt.translate_header = true;
1225  myopt.translate_columns = translate_columns;
1226  myopt.n_translate_columns = lengthof(translate_columns);
1227 
1228  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1229 
1230  termPQExpBuffer(&buf);
1231  PQclear(res);
1232  return true;
1233 
1234 error_return:
1235  termPQExpBuffer(&buf);
1236  return false;
1237 }

References _, appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ listDomains()

bool listDomains ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 4383 of file describe.c.

4384 {
4386  PGresult *res;
4387  printQueryOpt myopt = pset.popt;
4388 
4389  initPQExpBuffer(&buf);
4390 
4392  "SELECT n.nspname as \"%s\",\n"
4393  " t.typname as \"%s\",\n"
4394  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
4395  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
4396  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n"
4397  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
4398  " t.typdefault as \"%s\",\n"
4399  " pg_catalog.array_to_string(ARRAY(\n"
4400  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid AND r.contype = 'c' ORDER BY r.conname\n"
4401  " ), ' ') as \"%s\"",
4402  gettext_noop("Schema"),
4403  gettext_noop("Name"),
4404  gettext_noop("Type"),
4405  gettext_noop("Collation"),
4406  gettext_noop("Nullable"),
4407  gettext_noop("Default"),
4408  gettext_noop("Check"));
4409 
4410  if (verbose)
4411  {
4412  appendPQExpBufferStr(&buf, ",\n ");
4413  printACLColumn(&buf, "t.typacl");
4415  ",\n d.description as \"%s\"",
4416  gettext_noop("Description"));
4417  }
4418 
4420  "\nFROM pg_catalog.pg_type t\n"
4421  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
4422 
4423  if (verbose)
4425  " LEFT JOIN pg_catalog.pg_description d "
4426  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
4427  "AND d.objsubid = 0\n");
4428 
4429  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
4430 
4431  if (!showSystem && !pattern)
4432  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4433  " AND n.nspname <> 'information_schema'\n");
4434 
4435  if (!validateSQLNamePattern(&buf, pattern, true, false,
4436  "n.nspname", "t.typname", NULL,
4437  "pg_catalog.pg_type_is_visible(t.oid)",
4438  NULL, 3))
4439  {
4440  termPQExpBuffer(&buf);
4441  return false;
4442  }
4443 
4444  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4445 
4446  res = PSQLexec(buf.data);
4447  termPQExpBuffer(&buf);
4448  if (!res)
4449  return false;
4450 
4451  myopt.title = _("List of domains");
4452  myopt.translate_header = true;
4453 
4454  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4455 
4456  PQclear(res);
4457  return true;
4458 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listEventTriggers()

bool listEventTriggers ( const char *  pattern,
bool  verbose 
)

Definition at line 4614 of file describe.c.

4615 {
4617  PGresult *res;
4618  printQueryOpt myopt = pset.popt;
4619  static const bool translate_columns[] =
4620  {false, false, false, true, false, false, false};
4621 
4622  if (pset.sversion < 90300)
4623  {
4624  char sverbuf[32];
4625 
4626  pg_log_error("The server (version %s) does not support event triggers.",
4628  sverbuf, sizeof(sverbuf)));
4629  return true;
4630  }
4631 
4632  initPQExpBuffer(&buf);
4633 
4635  "SELECT evtname as \"%s\", "
4636  "evtevent as \"%s\", "
4637  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
4638  " case evtenabled when 'O' then '%s'"
4639  " when 'R' then '%s'"
4640  " when 'A' then '%s'"
4641  " when 'D' then '%s' end as \"%s\",\n"
4642  " e.evtfoid::pg_catalog.regproc as \"%s\", "
4643  "pg_catalog.array_to_string(array(select x"
4644  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
4645  gettext_noop("Name"),
4646  gettext_noop("Event"),
4647  gettext_noop("Owner"),
4648  gettext_noop("enabled"),
4649  gettext_noop("replica"),
4650  gettext_noop("always"),
4651  gettext_noop("disabled"),
4652  gettext_noop("Enabled"),
4653  gettext_noop("Function"),
4654  gettext_noop("Tags"));
4655  if (verbose)
4657  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
4658  gettext_noop("Description"));
4660  "\nFROM pg_catalog.pg_event_trigger e ");
4661 
4662  if (!validateSQLNamePattern(&buf, pattern, false, false,
4663  NULL, "evtname", NULL, NULL,
4664  NULL, 1))
4665  {
4666  termPQExpBuffer(&buf);
4667  return false;
4668  }
4669 
4670  appendPQExpBufferStr(&buf, "ORDER BY 1");
4671 
4672  res = PSQLexec(buf.data);
4673  termPQExpBuffer(&buf);
4674  if (!res)
4675  return false;
4676 
4677  myopt.title = _("List of event triggers");
4678  myopt.translate_header = true;
4679  myopt.translate_columns = translate_columns;
4680  myopt.n_translate_columns = lengthof(translate_columns);
4681 
4682  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4683 
4684  PQclear(res);
4685  return true;
4686 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listExtendedStats()

bool listExtendedStats ( const char *  pattern)

Definition at line 4694 of file describe.c.

4695 {
4697  PGresult *res;
4698  printQueryOpt myopt = pset.popt;
4699 
4700  if (pset.sversion < 100000)
4701  {
4702  char sverbuf[32];
4703 
4704  pg_log_error("The server (version %s) does not support extended statistics.",
4706  sverbuf, sizeof(sverbuf)));
4707  return true;
4708  }
4709 
4710  initPQExpBuffer(&buf);
4712  "SELECT \n"
4713  "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS \"%s\", \n"
4714  "es.stxname AS \"%s\", \n",
4715  gettext_noop("Schema"),
4716  gettext_noop("Name"));
4717 
4718  if (pset.sversion >= 140000)
4720  "pg_catalog.format('%%s FROM %%s', \n"
4721  " pg_catalog.pg_get_statisticsobjdef_columns(es.oid), \n"
4722  " es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4723  gettext_noop("Definition"));
4724  else
4726  "pg_catalog.format('%%s FROM %%s', \n"
4727  " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(a.attname),', ') \n"
4728  " FROM pg_catalog.unnest(es.stxkeys) s(attnum) \n"
4729  " JOIN pg_catalog.pg_attribute a \n"
4730  " ON (es.stxrelid = a.attrelid \n"
4731  " AND a.attnum = s.attnum \n"
4732  " AND NOT a.attisdropped)), \n"
4733  "es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4734  gettext_noop("Definition"));
4735 
4737  ",\nCASE WHEN 'd' = any(es.stxkind) THEN 'defined' \n"
4738  "END AS \"%s\", \n"
4739  "CASE WHEN 'f' = any(es.stxkind) THEN 'defined' \n"
4740  "END AS \"%s\"",
4741  gettext_noop("Ndistinct"),
4742  gettext_noop("Dependencies"));
4743 
4744  /*
4745  * Include the MCV statistics kind.
4746  */
4747  if (pset.sversion >= 120000)
4748  {
4750  ",\nCASE WHEN 'm' = any(es.stxkind) THEN 'defined' \n"
4751  "END AS \"%s\" ",
4752  gettext_noop("MCV"));
4753  }
4754 
4756  " \nFROM pg_catalog.pg_statistic_ext es \n");
4757 
4758  if (!validateSQLNamePattern(&buf, pattern,
4759  false, false,
4760  "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text", "es.stxname",
4761  NULL, "pg_catalog.pg_statistics_obj_is_visible(es.oid)",
4762  NULL, 3))
4763  {
4764  termPQExpBuffer(&buf);
4765  return false;
4766  }
4767 
4768  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4769 
4770  res = PSQLexec(buf.data);
4771  termPQExpBuffer(&buf);
4772  if (!res)
4773  return false;
4774 
4775  myopt.title = _("List of extended statistics");
4776  myopt.translate_header = true;
4777 
4778  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4779 
4780  PQclear(res);
4781  return true;
4782 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, pg_log_error, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ listExtensionContents()

bool listExtensionContents ( const char *  pattern)

Definition at line 6053 of file describe.c.

6054 {
6056  PGresult *res;
6057  int i;
6058 
6059  initPQExpBuffer(&buf);
6061  "SELECT e.extname, e.oid\n"
6062  "FROM pg_catalog.pg_extension e\n");
6063 
6064  if (!validateSQLNamePattern(&buf, pattern,
6065  false, false,
6066  NULL, "e.extname", NULL,
6067  NULL,
6068  NULL, 1))
6069  {
6070  termPQExpBuffer(&buf);
6071  return false;
6072  }
6073 
6074  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6075 
6076  res = PSQLexec(buf.data);
6077  termPQExpBuffer(&buf);
6078  if (!res)
6079  return false;
6080 
6081  if (PQntuples(res) == 0)
6082  {
6083  if (!pset.quiet)
6084  {
6085  if (pattern)
6086  pg_log_error("Did not find any extension named \"%s\".",
6087  pattern);
6088  else
6089  pg_log_error("Did not find any extensions.");
6090  }
6091  PQclear(res);
6092  return false;
6093  }
6094 
6095  for (i = 0; i < PQntuples(res); i++)
6096  {
6097  const char *extname;
6098  const char *oid;
6099 
6100  extname = PQgetvalue(res, i, 0);
6101  oid = PQgetvalue(res, i, 1);
6102 
6103  if (!listOneExtensionContents(extname, oid))
6104  {
6105  PQclear(res);
6106  return false;
6107  }
6108  if (cancel_pressed)
6109  {
6110  PQclear(res);
6111  return false;
6112  }
6113  }
6114 
6115  PQclear(res);
6116  return true;
6117 }
static bool listOneExtensionContents(const char *extname, const char *oid)
Definition: describe.c:6120

References appendPQExpBufferStr(), buf, cancel_pressed, i, initPQExpBuffer(), listOneExtensionContents(), pg_log_error, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), pset, PSQLexec(), _psqlSettings::quiet, res, termPQExpBuffer(), and validateSQLNamePattern().

Referenced by exec_command_d().

◆ listExtensions()

bool listExtensions ( const char *  pattern)

Definition at line 6002 of file describe.c.

6003 {
6005  PGresult *res;
6006  printQueryOpt myopt = pset.popt;
6007 
6008  initPQExpBuffer(&buf);
6010  "SELECT e.extname AS \"%s\", "
6011  "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
6012  "FROM pg_catalog.pg_extension e "
6013  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
6014  "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
6015  "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
6016  gettext_noop("Name"),
6017  gettext_noop("Version"),
6018  gettext_noop("Schema"),
6019  gettext_noop("Description"));
6020 
6021  if (!validateSQLNamePattern(&buf, pattern,
6022  false, false,
6023  NULL, "e.extname", NULL,
6024  NULL,
6025  NULL, 1))
6026  {
6027  termPQExpBuffer(&buf);
6028  return false;
6029  }
6030 
6031  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6032 
6033  res = PSQLexec(buf.data);
6034  termPQExpBuffer(&buf);
6035  if (!res)
6036  return false;
6037 
6038  myopt.title = _("List of installed extensions");
6039  myopt.translate_header = true;
6040 
6041  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6042 
6043  PQclear(res);
6044  return true;
6045 }

References _, appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ listForeignDataWrappers()

bool listForeignDataWrappers ( const char *  pattern,
bool  verbose 
)

Definition at line 5728 of file describe.c.

5729 {
5731  PGresult *res;
5732  printQueryOpt myopt = pset.popt;
5733 
5734  initPQExpBuffer(&buf);
5736  "SELECT fdw.fdwname AS \"%s\",\n"
5737  " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n"
5738  " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n"
5739  " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
5740  gettext_noop("Name"),
5741  gettext_noop("Owner"),
5742  gettext_noop("Handler"),
5743  gettext_noop("Validator"));
5744 
5745  if (verbose)
5746  {
5747  appendPQExpBufferStr(&buf, ",\n ");
5748  printACLColumn(&buf, "fdwacl");
5750  ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
5751  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5752  " pg_catalog.quote_ident(option_name) || ' ' || "
5753  " pg_catalog.quote_literal(option_value) FROM "
5754  " pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
5755  " END AS \"%s\""
5756  ",\n d.description AS \"%s\" ",
5757  gettext_noop("FDW options"),
5758  gettext_noop("Description"));
5759  }
5760 
5761  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
5762 
5763  if (verbose)
5765  "LEFT JOIN pg_catalog.pg_description d\n"
5766  " ON d.classoid = fdw.tableoid "
5767  "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
5768 
5769  if (!validateSQLNamePattern(&buf, pattern, false, false,
5770  NULL, "fdwname", NULL, NULL,
5771  NULL, 1))
5772  {
5773  termPQExpBuffer(&buf);
5774  return false;
5775  }
5776 
5777  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5778 
5779  res = PSQLexec(buf.data);
5780  termPQExpBuffer(&buf);
5781  if (!res)
5782  return false;
5783 
5784  myopt.title = _("List of foreign-data wrappers");
5785  myopt.translate_header = true;
5786 
5787  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5788 
5789  PQclear(res);
5790  return true;
5791 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listForeignServers()

bool listForeignServers ( const char *  pattern,
bool  verbose 
)

Definition at line 5799 of file describe.c.

5800 {
5802  PGresult *res;
5803  printQueryOpt myopt = pset.popt;
5804 
5805  initPQExpBuffer(&buf);
5807  "SELECT s.srvname AS \"%s\",\n"
5808  " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
5809  " f.fdwname AS \"%s\"",
5810  gettext_noop("Name"),
5811  gettext_noop("Owner"),
5812  gettext_noop("Foreign-data wrapper"));
5813 
5814  if (verbose)
5815  {
5816  appendPQExpBufferStr(&buf, ",\n ");
5817  printACLColumn(&buf, "s.srvacl");
5819  ",\n"
5820  " s.srvtype AS \"%s\",\n"
5821  " s.srvversion AS \"%s\",\n"
5822  " CASE WHEN srvoptions IS NULL THEN '' ELSE "
5823  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5824  " pg_catalog.quote_ident(option_name) || ' ' || "
5825  " pg_catalog.quote_literal(option_value) FROM "
5826  " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
5827  " END AS \"%s\",\n"
5828  " d.description AS \"%s\"",
5829  gettext_noop("Type"),
5830  gettext_noop("Version"),
5831  gettext_noop("FDW options"),
5832  gettext_noop("Description"));
5833  }
5834 
5836  "\nFROM pg_catalog.pg_foreign_server s\n"
5837  " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
5838 
5839  if (verbose)
5841  "LEFT JOIN pg_catalog.pg_description d\n "
5842  "ON d.classoid = s.tableoid AND d.objoid = s.oid "
5843  "AND d.objsubid = 0\n");
5844 
5845  if (!validateSQLNamePattern(&buf, pattern, false, false,
5846  NULL, "s.srvname", NULL, NULL,
5847  NULL, 1))
5848  {
5849  termPQExpBuffer(&buf);
5850  return false;
5851  }
5852 
5853  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5854 
5855  res = PSQLexec(buf.data);
5856  termPQExpBuffer(&buf);
5857  if (!res)
5858  return false;
5859 
5860  myopt.title = _("List of foreign servers");
5861  myopt.translate_header = true;
5862 
5863  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5864 
5865  PQclear(res);
5866  return true;
5867 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listForeignTables()

bool listForeignTables ( const char *  pattern,
bool  verbose 
)

Definition at line 5930 of file describe.c.

5931 {
5933  PGresult *res;
5934  printQueryOpt myopt = pset.popt;
5935 
5936  initPQExpBuffer(&buf);
5938  "SELECT n.nspname AS \"%s\",\n"
5939  " c.relname AS \"%s\",\n"
5940  " s.srvname AS \"%s\"",
5941  gettext_noop("Schema"),
5942  gettext_noop("Table"),
5943  gettext_noop("Server"));
5944 
5945  if (verbose)
5947  ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
5948  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5949  " pg_catalog.quote_ident(option_name) || ' ' || "
5950  " pg_catalog.quote_literal(option_value) FROM "
5951  " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
5952  " END AS \"%s\",\n"
5953  " d.description AS \"%s\"",
5954  gettext_noop("FDW options"),
5955  gettext_noop("Description"));
5956 
5958  "\nFROM pg_catalog.pg_foreign_table ft\n"
5959  " INNER JOIN pg_catalog.pg_class c"
5960  " ON c.oid = ft.ftrelid\n"
5961  " INNER JOIN pg_catalog.pg_namespace n"
5962  " ON n.oid = c.relnamespace\n"
5963  " INNER JOIN pg_catalog.pg_foreign_server s"
5964  " ON s.oid = ft.ftserver\n");
5965  if (verbose)
5967  " LEFT JOIN pg_catalog.pg_description d\n"
5968  " ON d.classoid = c.tableoid AND "
5969  "d.objoid = c.oid AND d.objsubid = 0\n");
5970 
5971  if (!validateSQLNamePattern(&buf, pattern, false, false,
5972  "n.nspname", "c.relname", NULL,
5973  "pg_catalog.pg_table_is_visible(c.oid)",
5974  NULL, 3))
5975  {
5976  termPQExpBuffer(&buf);
5977  return false;
5978  }
5979 
5980  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5981 
5982  res = PSQLexec(buf.data);
5983  termPQExpBuffer(&buf);
5984  if (!res)
5985  return false;
5986 
5987  myopt.title = _("List of foreign tables");
5988  myopt.translate_header = true;
5989 
5990  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5991 
5992  PQclear(res);
5993  return true;
5994 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listLanguages()

bool listLanguages ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 4307 of file describe.c.

4308 {
4310  PGresult *res;
4311  printQueryOpt myopt = pset.popt;
4312 
4313  initPQExpBuffer(&buf);
4314 
4316  "SELECT l.lanname AS \"%s\",\n"
4317  " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n"
4318  " l.lanpltrusted AS \"%s\"",
4319  gettext_noop("Name"),
4320  gettext_noop("Owner"),
4321  gettext_noop("Trusted"));
4322 
4323  if (verbose)
4324  {
4326  ",\n NOT l.lanispl AS \"%s\",\n"
4327  " l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
4328  " l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n "
4329  "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
4330  gettext_noop("Internal language"),
4331  gettext_noop("Call handler"),
4332  gettext_noop("Validator"),
4333  gettext_noop("Inline handler"));
4334  printACLColumn(&buf, "l.lanacl");
4335  }
4336 
4338  ",\n d.description AS \"%s\""
4339  "\nFROM pg_catalog.pg_language l\n"
4340  "LEFT JOIN pg_catalog.pg_description d\n"
4341  " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
4342  " AND d.objsubid = 0\n",
4343  gettext_noop("Description"));
4344 
4345  if (pattern)
4346  {
4347  if (!validateSQLNamePattern(&buf, pattern, false, false,
4348  NULL, "l.lanname", NULL, NULL,
4349  NULL, 2))
4350  {
4351  termPQExpBuffer(&buf);
4352  return false;
4353  }
4354  }
4355 
4356  if (!showSystem && !pattern)
4357  appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
4358 
4359 
4360  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4361 
4362  res = PSQLexec(buf.data);
4363  termPQExpBuffer(&buf);
4364  if (!res)
4365  return false;
4366 
4367  myopt.title = _("List of languages");
4368  myopt.translate_header = true;
4369 
4370  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4371 
4372  PQclear(res);
4373  return true;
4374 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listLargeObjects()

bool listLargeObjects ( bool  verbose)

Definition at line 7054 of file describe.c.

7055 {
7057  PGresult *res;
7058  printQueryOpt myopt = pset.popt;
7059 
7060  initPQExpBuffer(&buf);
7061 
7063  "SELECT oid as \"%s\",\n"
7064  " pg_catalog.pg_get_userbyid(lomowner) as \"%s\",\n ",
7065  gettext_noop("ID"),
7066  gettext_noop("Owner"));
7067 
7068  if (verbose)
7069  {
7070  printACLColumn(&buf, "lomacl");
7071  appendPQExpBufferStr(&buf, ",\n ");
7072  }
7073 
7075  "pg_catalog.obj_description(oid, 'pg_largeobject') as \"%s\"\n"
7076  "FROM pg_catalog.pg_largeobject_metadata\n"
7077  "ORDER BY oid",
7078  gettext_noop("Description"));
7079 
7080  res = PSQLexec(buf.data);
7081  termPQExpBuffer(&buf);
7082  if (!res)
7083  return false;
7084 
7085  myopt.title = _("Large objects");
7086  myopt.translate_header = true;
7087 
7088  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7089 
7090  PQclear(res);
7091  return true;
7092 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, and verbose.

Referenced by exec_command_d(), and exec_command_lo().

◆ listOperatorClasses()

bool listOperatorClasses ( const char *  access_method_pattern,
const char *  type_pattern,
bool  verbose 
)

Definition at line 6677 of file describe.c.

6679 {
6681  PGresult *res;
6682  printQueryOpt myopt = pset.popt;
6683  bool have_where = false;
6684  static const bool translate_columns[] = {false, false, false, false, false, false, false};
6685 
6686  initPQExpBuffer(&buf);
6687 
6689  "SELECT\n"
6690  " am.amname AS \"%s\",\n"
6691  " pg_catalog.format_type(c.opcintype, NULL) AS \"%s\",\n"
6692  " CASE\n"
6693  " WHEN c.opckeytype <> 0 AND c.opckeytype <> c.opcintype\n"
6694  " THEN pg_catalog.format_type(c.opckeytype, NULL)\n"
6695  " ELSE NULL\n"
6696  " END AS \"%s\",\n"
6697  " CASE\n"
6698  " WHEN pg_catalog.pg_opclass_is_visible(c.oid)\n"
6699  " THEN pg_catalog.format('%%I', c.opcname)\n"
6700  " ELSE pg_catalog.format('%%I.%%I', n.nspname, c.opcname)\n"
6701  " END AS \"%s\",\n"
6702  " (CASE WHEN c.opcdefault\n"
6703  " THEN '%s'\n"
6704  " ELSE '%s'\n"
6705  " END) AS \"%s\"",
6706  gettext_noop("AM"),
6707  gettext_noop("Input type"),
6708  gettext_noop("Storage type"),
6709  gettext_noop("Operator class"),
6710  gettext_noop("yes"),
6711  gettext_noop("no"),
6712  gettext_noop("Default?"));
6713  if (verbose)
6715  ",\n CASE\n"
6716  " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
6717  " THEN pg_catalog.format('%%I', of.opfname)\n"
6718  " ELSE pg_catalog.format('%%I.%%I', ofn.nspname, of.opfname)\n"
6719  " END AS \"%s\",\n"
6720  " pg_catalog.pg_get_userbyid(c.opcowner) AS \"%s\"\n",
6721  gettext_noop("Operator family"),
6722  gettext_noop("Owner"));
6724  "\nFROM pg_catalog.pg_opclass c\n"
6725  " LEFT JOIN pg_catalog.pg_am am on am.oid = c.opcmethod\n"
6726  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.opcnamespace\n"
6727  " LEFT JOIN pg_catalog.pg_type t ON t.oid = c.opcintype\n"
6728  " LEFT JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace\n");
6729  if (verbose)
6731  " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = c.opcfamily\n"
6732  " LEFT JOIN pg_catalog.pg_namespace ofn ON ofn.oid = of.opfnamespace\n");
6733 
6734  if (access_method_pattern)
6735  if (!validateSQLNamePattern(&buf, access_method_pattern,
6736  false, false, NULL, "am.amname", NULL, NULL,
6737  &have_where, 1))
6738  goto error_return;
6739  if (type_pattern)
6740  {
6741  /* Match type name pattern against either internal or external name */
6742  if (!validateSQLNamePattern(&buf, type_pattern, have_where, false,
6743  "tn.nspname", "t.typname",
6744  "pg_catalog.format_type(t.oid, NULL)",
6745  "pg_catalog.pg_type_is_visible(t.oid)",
6746  NULL, 3))
6747  goto error_return;
6748  }
6749 
6750  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
6751  res = PSQLexec(buf.data);
6752  termPQExpBuffer(&buf);
6753  if (!res)
6754  return false;
6755 
6756  myopt.title = _("List of operator classes");
6757  myopt.translate_header = true;
6758  myopt.translate_columns = translate_columns;
6759  myopt.n_translate_columns = lengthof(translate_columns);
6760 
6761  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6762 
6763  PQclear(res);
6764  return true;
6765 
6766 error_return:
6767  termPQExpBuffer(&buf);
6768  return false;
6769 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listOperatorFamilies()

bool listOperatorFamilies ( const char *  access_method_pattern,
const char *  type_pattern,
bool  verbose 
)

Definition at line 6778 of file describe.c.

6780 {
6782  PGresult *res;
6783  printQueryOpt myopt = pset.popt;
6784  bool have_where = false;
6785  static const bool translate_columns[] = {false, false, false, false};
6786 
6787  initPQExpBuffer(&buf);
6788 
6790  "SELECT\n"
6791  " am.amname AS \"%s\",\n"
6792  " CASE\n"
6793  " WHEN pg_catalog.pg_opfamily_is_visible(f.oid)\n"
6794  " THEN pg_catalog.format('%%I', f.opfname)\n"
6795  " ELSE pg_catalog.format('%%I.%%I', n.nspname, f.opfname)\n"
6796  " END AS \"%s\",\n"
6797  " (SELECT\n"
6798  " pg_catalog.string_agg(pg_catalog.format_type(oc.opcintype, NULL), ', ')\n"
6799  " FROM pg_catalog.pg_opclass oc\n"
6800  " WHERE oc.opcfamily = f.oid) \"%s\"",
6801  gettext_noop("AM"),
6802  gettext_noop("Operator family"),
6803  gettext_noop("Applicable types"));
6804  if (verbose)
6806  ",\n pg_catalog.pg_get_userbyid(f.opfowner) AS \"%s\"\n",
6807  gettext_noop("Owner"));
6809  "\nFROM pg_catalog.pg_opfamily f\n"
6810  " LEFT JOIN pg_catalog.pg_am am on am.oid = f.opfmethod\n"
6811  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = f.opfnamespace\n");
6812 
6813  if (access_method_pattern)
6814  if (!validateSQLNamePattern(&buf, access_method_pattern,
6815  false, false, NULL, "am.amname", NULL, NULL,
6816  &have_where, 1))
6817  goto error_return;
6818  if (type_pattern)
6819  {
6821  " %s EXISTS (\n"
6822  " SELECT 1\n"
6823  " FROM pg_catalog.pg_type t\n"
6824  " JOIN pg_catalog.pg_opclass oc ON oc.opcintype = t.oid\n"
6825  " LEFT JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace\n"
6826  " WHERE oc.opcfamily = f.oid\n",
6827  have_where ? "AND" : "WHERE");
6828  /* Match type name pattern against either internal or external name */
6829  if (!validateSQLNamePattern(&buf, type_pattern, true, false,
6830  "tn.nspname", "t.typname",
6831  "pg_catalog.format_type(t.oid, NULL)",
6832  "pg_catalog.pg_type_is_visible(t.oid)",
6833  NULL, 3))
6834  goto error_return;
6835  appendPQExpBufferStr(&buf, " )\n");
6836  }
6837 
6838  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
6839  res = PSQLexec(buf.data);
6840  termPQExpBuffer(&buf);
6841  if (!res)
6842  return false;
6843 
6844  myopt.title = _("List of operator families");
6845  myopt.translate_header = true;
6846  myopt.translate_columns = translate_columns;
6847  myopt.n_translate_columns = lengthof(translate_columns);
6848 
6849  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6850 
6851  PQclear(res);
6852  return true;
6853 
6854 error_return:
6855  termPQExpBuffer(&buf);
6856  return false;
6857 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listOpFamilyFunctions()

bool listOpFamilyFunctions ( const char *  access_method_pattern,
const char *  family_pattern,
bool  verbose 
)

Definition at line 6965 of file describe.c.

6967 {
6969  PGresult *res;
6970  printQueryOpt myopt = pset.popt;
6971  bool have_where = false;
6972  static const bool translate_columns[] = {false, false, false, false, false, false};
6973 
6974  initPQExpBuffer(&buf);
6975 
6977  "SELECT\n"
6978  " am.amname AS \"%s\",\n"
6979  " CASE\n"
6980  " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
6981  " THEN pg_catalog.format('%%I', of.opfname)\n"
6982  " ELSE pg_catalog.format('%%I.%%I', ns.nspname, of.opfname)\n"
6983  " END AS \"%s\",\n"
6984  " pg_catalog.format_type(ap.amproclefttype, NULL) AS \"%s\",\n"
6985  " pg_catalog.format_type(ap.amprocrighttype, NULL) AS \"%s\",\n"
6986  " ap.amprocnum AS \"%s\"\n",
6987  gettext_noop("AM"),
6988  gettext_noop("Operator family"),
6989  gettext_noop("Registered left type"),
6990  gettext_noop("Registered right type"),
6991  gettext_noop("Number"));
6992 
6993  if (!verbose)
6995  ", p.proname AS \"%s\"\n",
6996  gettext_noop("Function"));
6997  else
6999  ", ap.amproc::pg_catalog.regprocedure AS \"%s\"\n",
7000  gettext_noop("Function"));
7001 
7003  "FROM pg_catalog.pg_amproc ap\n"
7004  " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = ap.amprocfamily\n"
7005  " LEFT JOIN pg_catalog.pg_am am ON am.oid = of.opfmethod\n"
7006  " LEFT JOIN pg_catalog.pg_namespace ns ON of.opfnamespace = ns.oid\n"
7007  " LEFT JOIN pg_catalog.pg_proc p ON ap.amproc = p.oid\n");
7008 
7009  if (access_method_pattern)
7010  {
7011  if (!validateSQLNamePattern(&buf, access_method_pattern,
7012  false, false, NULL, "am.amname",
7013  NULL, NULL,
7014  &have_where, 1))
7015  goto error_return;
7016  }
7017  if (family_pattern)
7018  {
7019  if (!validateSQLNamePattern(&buf, family_pattern, have_where, false,
7020  "ns.nspname", "of.opfname", NULL, NULL,
7021  NULL, 3))
7022  goto error_return;
7023  }
7024 
7025  appendPQExpBufferStr(&buf, "ORDER BY 1, 2,\n"
7026  " ap.amproclefttype = ap.amprocrighttype DESC,\n"
7027  " 3, 4, 5;");
7028 
7029  res = PSQLexec(buf.data);
7030  termPQExpBuffer(&buf);
7031  if (!res)
7032  return false;
7033 
7034  myopt.title = _("List of support functions of operator families");
7035  myopt.translate_header = true;
7036  myopt.translate_columns = translate_columns;
7037  myopt.n_translate_columns = lengthof(translate_columns);
7038 
7039  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7040 
7041  PQclear(res);
7042  return true;
7043 
7044 error_return:
7045  termPQExpBuffer(&buf);
7046  return false;
7047 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listOpFamilyOperators()

bool listOpFamilyOperators ( const char *  access_method_pattern,
const char *  family_pattern,
bool  verbose 
)

Definition at line 6867 of file describe.c.

6869 {
6871  PGresult *res;
6872  printQueryOpt myopt = pset.popt;
6873  bool have_where = false;
6874 
6875  static const bool translate_columns[] = {false, false, false, false, false, false};
6876 
6877  initPQExpBuffer(&buf);
6878 
6880  "SELECT\n"
6881  " am.amname AS \"%s\",\n"
6882  " CASE\n"
6883  " WHEN pg_catalog.pg_opfamily_is_visible(of.oid)\n"
6884  " THEN pg_catalog.format('%%I', of.opfname)\n"
6885  " ELSE pg_catalog.format('%%I.%%I', nsf.nspname, of.opfname)\n"
6886  " END AS \"%s\",\n"
6887  " o.amopopr::pg_catalog.regoperator AS \"%s\"\n,"
6888  " o.amopstrategy AS \"%s\",\n"
6889  " CASE o.amoppurpose\n"
6890  " WHEN 'o' THEN '%s'\n"
6891  " WHEN 's' THEN '%s'\n"
6892  " END AS \"%s\"\n",
6893  gettext_noop("AM"),
6894  gettext_noop("Operator family"),
6895  gettext_noop("Operator"),
6896  gettext_noop("Strategy"),
6897  gettext_noop("ordering"),
6898  gettext_noop("search"),
6899  gettext_noop("Purpose"));
6900 
6901  if (verbose)
6903  ", ofs.opfname AS \"%s\"\n",
6904  gettext_noop("Sort opfamily"));
6906  "FROM pg_catalog.pg_amop o\n"
6907  " LEFT JOIN pg_catalog.pg_opfamily of ON of.oid = o.amopfamily\n"
6908  " LEFT JOIN pg_catalog.pg_am am ON am.oid = of.opfmethod AND am.oid = o.amopmethod\n"
6909  " LEFT JOIN pg_catalog.pg_namespace nsf ON of.opfnamespace = nsf.oid\n");
6910  if (verbose)
6912  " LEFT JOIN pg_catalog.pg_opfamily ofs ON ofs.oid = o.amopsortfamily\n");
6913 
6914  if (access_method_pattern)
6915  {
6916  if (!validateSQLNamePattern(&buf, access_method_pattern,
6917  false, false, NULL, "am.amname",
6918  NULL, NULL,
6919  &have_where, 1))
6920  goto error_return;
6921  }
6922 
6923  if (family_pattern)
6924  {
6925  if (!validateSQLNamePattern(&buf, family_pattern, have_where, false,
6926  "nsf.nspname", "of.opfname", NULL, NULL,
6927  NULL, 3))
6928  goto error_return;
6929  }
6930 
6931  appendPQExpBufferStr(&buf, "ORDER BY 1, 2,\n"
6932  " o.amoplefttype = o.amoprighttype DESC,\n"
6933  " pg_catalog.format_type(o.amoplefttype, NULL),\n"
6934  " pg_catalog.format_type(o.amoprighttype, NULL),\n"
6935  " o.amopstrategy;");
6936 
6937  res = PSQLexec(buf.data);
6938  termPQExpBuffer(&buf);
6939  if (!res)
6940  return false;
6941 
6942  myopt.title = _("List of operators of operator families");
6943  myopt.translate_header = true;
6944  myopt.translate_columns = translate_columns;
6945  myopt.n_translate_columns = lengthof(translate_columns);
6946 
6947  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6948 
6949  PQclear(res);
6950  return true;
6951 
6952 error_return:
6953  termPQExpBuffer(&buf);
6954  return false;
6955 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listPartitionedTables()

bool listPartitionedTables ( const char *  reltypes,
const char *  pattern,
bool  verbose 
)

Definition at line 4107 of file describe.c.

4108 {
4109  bool showTables = strchr(reltypes, 't') != NULL;
4110  bool showIndexes = strchr(reltypes, 'i') != NULL;
4111  bool showNested = strchr(reltypes, 'n') != NULL;
4113  PQExpBufferData title;
4114  PGresult *res;
4115  printQueryOpt myopt = pset.popt;
4116  bool translate_columns[] = {false, false, false, false, false, false, false, false, false};
4117  const char *tabletitle;
4118  bool mixed_output = false;
4119 
4120  /*
4121  * Note: Declarative table partitioning is only supported as of Pg 10.0.
4122  */
4123  if (pset.sversion < 100000)
4124  {
4125  char sverbuf[32];
4126 
4127  pg_log_error("The server (version %s) does not support declarative table partitioning.",
4129  sverbuf, sizeof(sverbuf)));
4130  return true;
4131  }
4132 
4133  /* If no relation kind was selected, show them all */
4134  if (!showTables && !showIndexes)
4135  showTables = showIndexes = true;
4136 
4137  if (showIndexes && !showTables)
4138  tabletitle = _("List of partitioned indexes"); /* \dPi */
4139  else if (showTables && !showIndexes)
4140  tabletitle = _("List of partitioned tables"); /* \dPt */
4141  else
4142  {
4143  /* show all kinds */
4144  tabletitle = _("List of partitioned relations");
4145  mixed_output = true;
4146  }
4147 
4148  initPQExpBuffer(&buf);
4149 
4151  "SELECT n.nspname as \"%s\",\n"
4152  " c.relname as \"%s\",\n"
4153  " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
4154  gettext_noop("Schema"),
4155  gettext_noop("Name"),
4156  gettext_noop("Owner"));
4157 
4158  if (mixed_output)
4159  {
4161  ",\n CASE c.relkind"
4162  " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
4163  " WHEN " CppAsString2(RELKIND_PARTITIONED_INDEX) " THEN '%s'"
4164  " END as \"%s\"",
4165  gettext_noop("partitioned table"),
4166  gettext_noop("partitioned index"),
4167  gettext_noop("Type"));
4168 
4169  translate_columns[3] = true;
4170  }
4171 
4172  if (showNested || pattern)
4174  ",\n inh.inhparent::pg_catalog.regclass as \"%s\"",
4175  gettext_noop("Parent name"));
4176 
4177  if (showIndexes)
4179  ",\n c2.oid::pg_catalog.regclass as \"%s\"",
4180  gettext_noop("Table"));
4181 
4182  if (verbose)
4183  {
4184  if (showNested)
4185  {
4187  ",\n s.dps as \"%s\"",
4188  gettext_noop("Leaf partition size"));
4190  ",\n s.tps as \"%s\"",
4191  gettext_noop("Total size"));
4192  }
4193  else
4194  /* Sizes of all partitions are considered in this case. */
4196  ",\n s.tps as \"%s\"",
4197  gettext_noop("Total size"));
4198 
4200  ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
4201  gettext_noop("Description"));
4202  }
4203 
4205  "\nFROM pg_catalog.pg_class c"
4206  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
4207 
4208  if (showIndexes)
4210  "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
4211  "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
4212 
4213  if (showNested || pattern)
4215  "\n LEFT JOIN pg_catalog.pg_inherits inh ON c.oid = inh.inhrelid");
4216 
4217  if (verbose)
4218  {
4219  if (pset.sversion < 120000)
4220  {
4222  ",\n LATERAL (WITH RECURSIVE d\n"
4223  " AS (SELECT inhrelid AS oid, 1 AS level\n"
4224  " FROM pg_catalog.pg_inherits\n"
4225  " WHERE inhparent = c.oid\n"
4226  " UNION ALL\n"
4227  " SELECT inhrelid, level + 1\n"
4228  " FROM pg_catalog.pg_inherits i\n"
4229  " JOIN d ON i.inhparent = d.oid)\n"
4230  " SELECT pg_catalog.pg_size_pretty(sum(pg_catalog.pg_table_size("
4231  "d.oid))) AS tps,\n"
4232  " pg_catalog.pg_size_pretty(sum("
4233  "\n CASE WHEN d.level = 1"
4234  " THEN pg_catalog.pg_table_size(d.oid) ELSE 0 END)) AS dps\n"
4235  " FROM d) s");
4236  }
4237  else
4238  {
4239  /* PostgreSQL 12 has pg_partition_tree function */
4241  ",\n LATERAL (SELECT pg_catalog.pg_size_pretty(sum("
4242  "\n CASE WHEN ppt.isleaf AND ppt.level = 1"
4243  "\n THEN pg_catalog.pg_table_size(ppt.relid)"
4244  " ELSE 0 END)) AS dps"
4245  ",\n pg_catalog.pg_size_pretty(sum("
4246  "pg_catalog.pg_table_size(ppt.relid))) AS tps"
4247  "\n FROM pg_catalog.pg_partition_tree(c.oid) ppt) s");
4248  }
4249  }
4250 
4251  appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
4252  if (showTables)
4253  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
4254  if (showIndexes)
4255  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
4256  appendPQExpBufferStr(&buf, "''"); /* dummy */
4257  appendPQExpBufferStr(&buf, ")\n");
4258 
4259  appendPQExpBufferStr(&buf, !showNested && !pattern ?
4260  " AND NOT c.relispartition\n" : "");
4261 
4262  if (!pattern)
4263  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4264  " AND n.nspname !~ '^pg_toast'\n"
4265  " AND n.nspname <> 'information_schema'\n");
4266 
4267  if (!validateSQLNamePattern(&buf, pattern, true, false,
4268  "n.nspname", "c.relname", NULL,
4269  "pg_catalog.pg_table_is_visible(c.oid)",
4270  NULL, 3))
4271  {
4272  termPQExpBuffer(&buf);
4273  return false;
4274  }
4275 
4276  appendPQExpBuffer(&buf, "ORDER BY \"Schema\", %s%s\"Name\";",
4277  mixed_output ? "\"Type\" DESC, " : "",
4278  showNested || pattern ? "\"Parent name\" NULLS FIRST, " : "");
4279 
4280  res = PSQLexec(buf.data);
4281  termPQExpBuffer(&buf);
4282  if (!res)
4283  return false;
4284 
4285  initPQExpBuffer(&title);
4286  appendPQExpBufferStr(&title, tabletitle);
4287 
4288  myopt.title = title.data;
4289  myopt.translate_header = true;
4290  myopt.translate_columns = translate_columns;
4291  myopt.n_translate_columns = lengthof(translate_columns);
4292 
4293  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4294 
4295  termPQExpBuffer(&title);
4296 
4297  PQclear(res);
4298  return true;
4299 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, CppAsString2, PQExpBufferData::data, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listPublications()

bool listPublications ( const char *  pattern)

Definition at line 6217 of file describe.c.

6218 {
6220  PGresult *res;
6221  printQueryOpt myopt = pset.popt;
6222  static const bool translate_columns[] = {false, false, false, false, false, false, false, false};
6223 
6224  if (pset.sversion < 100000)
6225  {
6226  char sverbuf[32];
6227 
6228  pg_log_error("The server (version %s) does not support publications.",
6230  sverbuf, sizeof(sverbuf)));
6231  return true;
6232  }
6233 
6234  initPQExpBuffer(&buf);
6235 
6237  "SELECT pubname AS \"%s\",\n"
6238  " pg_catalog.pg_get_userbyid(pubowner) AS \"%s\",\n"
6239  " puballtables AS \"%s\",\n"
6240  " pubinsert AS \"%s\",\n"
6241  " pubupdate AS \"%s\",\n"
6242  " pubdelete AS \"%s\"",
6243  gettext_noop("Name"),
6244  gettext_noop("Owner"),
6245  gettext_noop("All tables"),
6246  gettext_noop("Inserts"),
6247  gettext_noop("Updates"),
6248  gettext_noop("Deletes"));
6249  if (pset.sversion >= 110000)
6251  ",\n pubtruncate AS \"%s\"",
6252  gettext_noop("Truncates"));
6253  if (pset.sversion >= 130000)
6255  ",\n pubviaroot AS \"%s\"",
6256  gettext_noop("Via root"));
6257 
6259  "\nFROM pg_catalog.pg_publication\n");
6260 
6261  if (!validateSQLNamePattern(&buf, pattern, false, false,
6262  NULL, "pubname", NULL,
6263  NULL,
6264  NULL, 1))
6265  {
6266  termPQExpBuffer(&buf);
6267  return false;
6268  }
6269 
6270  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6271 
6272  res = PSQLexec(buf.data);
6273  termPQExpBuffer(&buf);
6274  if (!res)
6275  return false;
6276 
6277  myopt.title = _("List of publications");
6278  myopt.translate_header = true;
6279  myopt.translate_columns = translate_columns;
6280  myopt.n_translate_columns = lengthof(translate_columns);
6281 
6282  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6283 
6284  PQclear(res);
6285 
6286  return true;
6287 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ listSchemas()

bool listSchemas ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 5026 of file describe.c.

5027 {
5029  PGresult *res;
5030  printQueryOpt myopt = pset.popt;
5031  int pub_schema_tuples = 0;
5032  char **footers = NULL;
5033 
5034  initPQExpBuffer(&buf);
5036  "SELECT n.nspname AS \"%s\",\n"
5037  " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
5038  gettext_noop("Name"),
5039  gettext_noop("Owner"));
5040 
5041  if (verbose)
5042  {
5043  appendPQExpBufferStr(&buf, ",\n ");
5044  printACLColumn(&buf, "n.nspacl");
5046  ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
5047  gettext_noop("Description"));
5048  }
5049 
5051  "\nFROM pg_catalog.pg_namespace n\n");
5052 
5053  if (!showSystem && !pattern)
5055  "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
5056 
5057  if (!validateSQLNamePattern(&buf, pattern,
5058  !showSystem && !pattern, false,
5059  NULL, "n.nspname", NULL,
5060  NULL,
5061  NULL, 2))
5062  goto error_return;
5063 
5064  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5065 
5066  res = PSQLexec(buf.data);
5067  if (!res)
5068  goto error_return;
5069 
5070  myopt.title = _("List of schemas");
5071  myopt.translate_header = true;
5072 
5073  if (pattern && pset.sversion >= 150000)
5074  {
5075  PGresult *result;
5076  int i;
5077 
5079  "SELECT pubname \n"
5080  "FROM pg_catalog.pg_publication p\n"
5081  " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
5082  " JOIN pg_catalog.pg_namespace n ON n.oid = pn.pnnspid \n"
5083  "WHERE n.nspname = '%s'\n"
5084  "ORDER BY 1",
5085  pattern);
5086  result = PSQLexec(buf.data);
5087  if (!result)
5088  goto error_return;
5089  else
5090  pub_schema_tuples = PQntuples(result);
5091 
5092  if (pub_schema_tuples > 0)
5093  {
5094  /*
5095  * Allocate memory for footers. Size of footers will be 1 (for
5096  * storing "Publications:" string) + publication schema mapping
5097  * count + 1 (for storing NULL).
5098  */
5099  footers = (char **) pg_malloc((1 + pub_schema_tuples + 1) * sizeof(char *));
5100  footers[0] = pg_strdup(_("Publications:"));
5101 
5102  /* Might be an empty set - that's ok */
5103  for (i = 0; i < pub_schema_tuples; i++)
5104  {
5105  printfPQExpBuffer(&buf, " \"%s\"",
5106  PQgetvalue(result, i, 0));
5107 
5108  footers[i + 1] = pg_strdup(buf.data);
5109  }
5110 
5111  footers[i + 1] = NULL;
5112  myopt.footers = footers;
5113  }
5114 
5115  PQclear(result);
5116  }
5117 
5118  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5119 
5120  termPQExpBuffer(&buf);
5121  PQclear(res);
5122 
5123  /* Free the memory allocated for the footer */
5124  if (footers)
5125  {
5126  char **footer = NULL;
5127 
5128  for (footer = footers; *footer; footer++)
5129  pg_free(*footer);
5130 
5131  pg_free(footers);
5132  }
5133 
5134  return true;
5135 
5136 error_return:
5137  termPQExpBuffer(&buf);
5138  return false;
5139 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char ** footers
Definition: print.h:188

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, printQueryOpt::footers, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, pg_free(), pg_malloc(), pg_strdup(), _psqlSettings::popt, PQclear(), PQgetvalue(), PQntuples(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listTables()

bool listTables ( const char *  tabtypes,
const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3909 of file describe.c.

3910 {
3911  bool showTables = strchr(tabtypes, 't') != NULL;
3912  bool showIndexes = strchr(tabtypes, 'i') != NULL;
3913  bool showViews = strchr(tabtypes, 'v') != NULL;
3914  bool showMatViews = strchr(tabtypes, 'm') != NULL;
3915  bool showSeq = strchr(tabtypes, 's') != NULL;
3916  bool showForeign = strchr(tabtypes, 'E') != NULL;
3917 
3919  PGresult *res;
3920  printQueryOpt myopt = pset.popt;
3921  int cols_so_far;
3922  bool translate_columns[] = {false, false, true, false, false, false, false, false, false};
3923 
3924  /* If tabtypes is empty, we default to \dtvmsE (but see also command.c) */
3925  if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
3926  showTables = showViews = showMatViews = showSeq = showForeign = true;
3927 
3928  initPQExpBuffer(&buf);
3929 
3931  "SELECT n.nspname as \"%s\",\n"
3932  " c.relname as \"%s\",\n"
3933  " CASE c.relkind"
3934  " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
3935  " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
3936  " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
3937  " WHEN " CppAsString2(RELKIND_INDEX) " THEN '%s'"
3938  " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
3939  " WHEN " CppAsString2(RELKIND_TOASTVALUE) " THEN '%s'"
3940  " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
3941  " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
3942  " WHEN " CppAsString2(RELKIND_PARTITIONED_INDEX) " THEN '%s'"
3943  " END as \"%s\",\n"
3944  " pg_catalog.pg_get_userbyid(c.relowner) as \"%s\"",
3945  gettext_noop("Schema"),
3946  gettext_noop("Name"),
3947  gettext_noop("table"),
3948  gettext_noop("view"),
3949  gettext_noop("materialized view"),
3950  gettext_noop("index"),
3951  gettext_noop("sequence"),
3952  gettext_noop("TOAST table"),
3953  gettext_noop("foreign table"),
3954  gettext_noop("partitioned table"),
3955  gettext_noop("partitioned index"),
3956  gettext_noop("Type"),
3957  gettext_noop("Owner"));
3958  cols_so_far = 4;
3959 
3960  if (showIndexes)
3961  {
3963  ",\n c2.relname as \"%s\"",
3964  gettext_noop("Table"));
3965  cols_so_far++;
3966  }
3967 
3968  if (verbose)
3969  {
3970  /*
3971  * Show whether a relation is permanent, temporary, or unlogged.
3972  */
3974  ",\n CASE c.relpersistence WHEN 'p' THEN '%s' WHEN 't' THEN '%s' WHEN 'u' THEN '%s' END as \"%s\"",
3975  gettext_noop("permanent"),
3976  gettext_noop("temporary"),
3977  gettext_noop("unlogged"),
3978  gettext_noop("Persistence"));
3979  translate_columns[cols_so_far] = true;
3980 
3981  /*
3982  * We don't bother to count cols_so_far below here, as there's no need
3983  * to; this might change with future additions to the output columns.
3984  */
3985 
3986  /*
3987  * Access methods exist for tables, materialized views and indexes.
3988  * This has been introduced in PostgreSQL 12 for tables.
3989  */
3990  if (pset.sversion >= 120000 && !pset.hide_tableam &&
3991  (showTables || showMatViews || showIndexes))
3993  ",\n am.amname as \"%s\"",
3994  gettext_noop("Access method"));
3995 
3997  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\""
3998  ",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
3999  gettext_noop("Size"),
4000  gettext_noop("Description"));
4001  }
4002 
4004  "\nFROM pg_catalog.pg_class c"
4005  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
4006 
4007  if (pset.sversion >= 120000 && !pset.hide_tableam &&
4008  (showTables || showMatViews || showIndexes))
4010  "\n LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam");
4011 
4012  if (showIndexes)
4014  "\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
4015  "\n LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
4016 
4017  appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
4018  if (showTables)
4019  {
4020  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_RELATION) ","
4021  CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
4022  /* with 'S' or a pattern, allow 't' to match TOAST tables too */
4023  if (showSystem || pattern)
4024  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_TOASTVALUE) ",");
4025  }
4026  if (showViews)
4027  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_VIEW) ",");
4028  if (showMatViews)
4029  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_MATVIEW) ",");
4030  if (showIndexes)
4031  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_INDEX) ","
4032  CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
4033  if (showSeq)
4034  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_SEQUENCE) ",");
4035  if (showSystem || pattern)
4036  appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL */
4037  if (showForeign)
4038  appendPQExpBufferStr(&buf, CppAsString2(RELKIND_FOREIGN_TABLE) ",");
4039 
4040  appendPQExpBufferStr(&buf, "''"); /* dummy */
4041  appendPQExpBufferStr(&buf, ")\n");
4042 
4043  if (!showSystem && !pattern)
4044  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4045  " AND n.nspname !~ '^pg_toast'\n"
4046  " AND n.nspname <> 'information_schema'\n");
4047 
4048  if (!validateSQLNamePattern(&buf, pattern, true, false,
4049  "n.nspname", "c.relname", NULL,
4050  "pg_catalog.pg_table_is_visible(c.oid)",
4051  NULL, 3))
4052  {
4053  termPQExpBuffer(&buf);
4054  return false;
4055  }
4056 
4057  appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
4058 
4059  res = PSQLexec(buf.data);
4060  termPQExpBuffer(&buf);
4061  if (!res)
4062  return false;
4063 
4064  /*
4065  * Most functions in this file are content to print an empty table when
4066  * there are no matching objects. We intentionally deviate from that
4067  * here, but only in !quiet mode, for historical reasons.
4068  */
4069  if (PQntuples(res) == 0 && !pset.quiet)
4070  {
4071  if (pattern)
4072  pg_log_error("Did not find any relation named \"%s\".",
4073  pattern);
4074  else
4075  pg_log_error("Did not find any relations.");
4076  }
4077  else
4078  {
4079  myopt.title = _("List of relations");
4080  myopt.translate_header = true;
4081  myopt.translate_columns = translate_columns;
4082  myopt.n_translate_columns = lengthof(translate_columns);
4083 
4084  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4085  }
4086 
4087  PQclear(res);
4088  return true;
4089 }
bool hide_tableam
Definition: settings.h:142

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, CppAsString2, gettext_noop, _psqlSettings::hide_tableam, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, pg_log_error, _psqlSettings::popt, PQclear(), PQntuples(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listTSConfigs()

bool listTSConfigs ( const char *  pattern,
bool  verbose 
)

Definition at line 5524 of file describe.c.

5525 {
5527  PGresult *res;
5528  printQueryOpt myopt = pset.popt;
5529 
5530  if (verbose)
5531  return listTSConfigsVerbose(pattern);
5532 
5533  initPQExpBuffer(&buf);
5534 
5536  "SELECT\n"
5537  " n.nspname as \"%s\",\n"
5538  " c.cfgname as \"%s\",\n"
5539  " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
5540  "FROM pg_catalog.pg_ts_config c\n"
5541  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace\n",
5542  gettext_noop("Schema"),
5543  gettext_noop("Name"),
5544  gettext_noop("Description")
5545  );
5546 
5547  if (!validateSQLNamePattern(&buf, pattern, false, false,
5548  "n.nspname", "c.cfgname", NULL,
5549  "pg_catalog.pg_ts_config_is_visible(c.oid)",
5550  NULL, 3))
5551  {
5552  termPQExpBuffer(&buf);
5553  return false;
5554  }
5555 
5556  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5557 
5558  res = PSQLexec(buf.data);
5559  termPQExpBuffer(&buf);
5560  if (!res)
5561  return false;
5562 
5563  myopt.title = _("List of text search configurations");
5564  myopt.translate_header = true;
5565 
5566  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5567 
5568  PQclear(res);
5569  return true;
5570 }
static bool listTSConfigsVerbose(const char *pattern)
Definition: describe.c:5573

References _, appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), listTSConfigsVerbose(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listTSDictionaries()

bool listTSDictionaries ( const char *  pattern,
bool  verbose 
)

Definition at line 5394 of file describe.c.

5395 {
5397  PGresult *res;
5398  printQueryOpt myopt = pset.popt;
5399 
5400  initPQExpBuffer(&buf);
5401 
5403  "SELECT\n"
5404  " n.nspname as \"%s\",\n"
5405  " d.dictname as \"%s\",\n",
5406  gettext_noop("Schema"),
5407  gettext_noop("Name"));
5408 
5409  if (verbose)
5410  {
5412  " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM\n"
5413  " pg_catalog.pg_ts_template t\n"
5414  " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace\n"
5415  " WHERE d.dicttemplate = t.oid ) AS \"%s\",\n"
5416  " d.dictinitoption as \"%s\",\n",
5417  gettext_noop("Template"),
5418  gettext_noop("Init options"));
5419  }
5420 
5422  " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
5423  gettext_noop("Description"));
5424 
5425  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
5426  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
5427 
5428  if (!validateSQLNamePattern(&buf, pattern, false, false,
5429  "n.nspname", "d.dictname", NULL,
5430  "pg_catalog.pg_ts_dict_is_visible(d.oid)",
5431  NULL, 3))
5432  {
5433  termPQExpBuffer(&buf);
5434  return false;
5435  }
5436 
5437  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5438 
5439  res = PSQLexec(buf.data);
5440  termPQExpBuffer(&buf);
5441  if (!res)
5442  return false;
5443 
5444  myopt.title = _("List of text search dictionaries");
5445  myopt.translate_header = true;
5446 
5447  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5448 
5449  PQclear(res);
5450  return true;
5451 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listTSParsers()

bool listTSParsers ( const char *  pattern,
bool  verbose 
)

Definition at line 5147 of file describe.c.

5148 {
5150  PGresult *res;
5151  printQueryOpt myopt = pset.popt;
5152 
5153  if (verbose)
5154  return listTSParsersVerbose(pattern);
5155 
5156  initPQExpBuffer(&buf);
5157 
5159  "SELECT\n"
5160  " n.nspname as \"%s\",\n"
5161  " p.prsname as \"%s\",\n"
5162  " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
5163  "FROM pg_catalog.pg_ts_parser p\n"
5164  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
5165  gettext_noop("Schema"),
5166  gettext_noop("Name"),
5167  gettext_noop("Description")
5168  );
5169 
5170  if (!validateSQLNamePattern(&buf, pattern, false, false,
5171  "n.nspname", "p.prsname", NULL,
5172  "pg_catalog.pg_ts_parser_is_visible(p.oid)",
5173  NULL, 3))
5174  {
5175  termPQExpBuffer(&buf);
5176  return false;
5177  }
5178 
5179  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5180 
5181  res = PSQLexec(buf.data);
5182  termPQExpBuffer(&buf);
5183  if (!res)
5184  return false;
5185 
5186  myopt.title = _("List of text search parsers");
5187  myopt.translate_header = true;
5188 
5189  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5190 
5191  PQclear(res);
5192  return true;
5193 }
static bool listTSParsersVerbose(const char *pattern)
Definition: describe.c:5199

References _, appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), listTSParsersVerbose(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listTSTemplates()

bool listTSTemplates ( const char *  pattern,
bool  verbose 
)

Definition at line 5459 of file describe.c.

5460 {
5462  PGresult *res;
5463  printQueryOpt myopt = pset.popt;
5464 
5465  initPQExpBuffer(&buf);
5466 
5467  if (verbose)
5469  "SELECT\n"
5470  " n.nspname AS \"%s\",\n"
5471  " t.tmplname AS \"%s\",\n"
5472  " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
5473  " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
5474  " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
5475  gettext_noop("Schema"),
5476  gettext_noop("Name"),
5477  gettext_noop("Init"),
5478  gettext_noop("Lexize"),
5479  gettext_noop("Description"));
5480  else
5482  "SELECT\n"
5483  " n.nspname AS \"%s\",\n"
5484  " t.tmplname AS \"%s\",\n"
5485  " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
5486  gettext_noop("Schema"),
5487  gettext_noop("Name"),
5488  gettext_noop("Description"));
5489 
5490  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
5491  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
5492 
5493  if (!validateSQLNamePattern(&buf, pattern, false, false,
5494  "n.nspname", "t.tmplname", NULL,
5495  "pg_catalog.pg_ts_template_is_visible(t.oid)",
5496  NULL, 3))
5497  {
5498  termPQExpBuffer(&buf);
5499  return false;
5500  }
5501 
5502  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5503 
5504  res = PSQLexec(buf.data);
5505  termPQExpBuffer(&buf);
5506  if (!res)
5507  return false;
5508 
5509  myopt.title = _("List of text search templates");
5510  myopt.translate_header = true;
5511 
5512  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5513 
5514  PQclear(res);
5515  return true;
5516 }

References _, appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ listUserMappings()

bool listUserMappings ( const char *  pattern,
bool  verbose 
)

Definition at line 5875 of file describe.c.

5876 {
5878  PGresult *res;
5879  printQueryOpt myopt = pset.popt;
5880 
5881  initPQExpBuffer(&buf);
5883  "SELECT um.srvname AS \"%s\",\n"
5884  " um.usename AS \"%s\"",
5885  gettext_noop("Server"),
5886  gettext_noop("User name"));
5887 
5888  if (verbose)
5890  ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
5891  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5892  " pg_catalog.quote_ident(option_name) || ' ' || "
5893  " pg_catalog.quote_literal(option_value) FROM "
5894  " pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
5895  " END AS \"%s\"",
5896  gettext_noop("FDW options"));
5897 
5898  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
5899 
5900  if (!validateSQLNamePattern(&buf, pattern, false, false,
5901  NULL, "um.srvname", "um.usename", NULL,
5902  NULL, 1))
5903  {
5904  termPQExpBuffer(&buf);
5905  return false;
5906  }
5907 
5908  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5909 
5910  res = PSQLexec(buf.data);
5911  termPQExpBuffer(&buf);
5912  if (!res)
5913  return false;
5914 
5915  myopt.title = _("List of user mappings");
5916  myopt.translate_header = true;
5917 
5918  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5919 
5920  PQclear(res);
5921  return true;
5922 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_header, validateSQLNamePattern(), and verbose.

Referenced by exec_command_d().

◆ objectDescription()

bool objectDescription ( const char *  pattern,
bool  showSystem 
)

Definition at line 1252 of file describe.c.

1253 {
1255  PGresult *res;
1256  printQueryOpt myopt = pset.popt;
1257  static const bool translate_columns[] = {false, false, true, false};
1258 
1259  initPQExpBuffer(&buf);
1260 
1262  "SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
1263  "FROM (\n",
1264  gettext_noop("Schema"),
1265  gettext_noop("Name"),
1266  gettext_noop("Object"),
1267  gettext_noop("Description"));
1268 
1269  /* Table constraint descriptions */
1271  " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1272  " n.nspname as nspname,\n"
1273  " CAST(pgc.conname AS pg_catalog.text) as name,"
1274  " CAST('%s' AS pg_catalog.text) as object\n"
1275  " FROM pg_catalog.pg_constraint pgc\n"
1276  " JOIN pg_catalog.pg_class c "
1277  "ON c.oid = pgc.conrelid\n"
1278  " LEFT JOIN pg_catalog.pg_namespace n "
1279  " ON n.oid = c.relnamespace\n",
1280  gettext_noop("table constraint"));
1281 
1282  if (!showSystem && !pattern)
1283  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1284  " AND n.nspname <> 'information_schema'\n");
1285 
1286  if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1287  false, "n.nspname", "pgc.conname", NULL,
1288  "pg_catalog.pg_table_is_visible(c.oid)",
1289  NULL, 3))
1290  goto error_return;
1291 
1292  /* Domain constraint descriptions */
1294  "UNION ALL\n"
1295  " SELECT pgc.oid as oid, pgc.tableoid AS tableoid,\n"
1296  " n.nspname as nspname,\n"
1297  " CAST(pgc.conname AS pg_catalog.text) as name,"
1298  " CAST('%s' AS pg_catalog.text) as object\n"
1299  " FROM pg_catalog.pg_constraint pgc\n"
1300  " JOIN pg_catalog.pg_type t "
1301  "ON t.oid = pgc.contypid\n"
1302  " LEFT JOIN pg_catalog.pg_namespace n "
1303  " ON n.oid = t.typnamespace\n",
1304  gettext_noop("domain constraint"));
1305 
1306  if (!showSystem && !pattern)
1307  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1308  " AND n.nspname <> 'information_schema'\n");
1309 
1310  if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern,
1311  false, "n.nspname", "pgc.conname", NULL,
1312  "pg_catalog.pg_type_is_visible(t.oid)",
1313  NULL, 3))
1314  goto error_return;
1315 
1316  /* Operator class descriptions */
1318  "UNION ALL\n"
1319  " SELECT o.oid as oid, o.tableoid as tableoid,\n"
1320  " n.nspname as nspname,\n"
1321  " CAST(o.opcname AS pg_catalog.text) as name,\n"
1322  " CAST('%s' AS pg_catalog.text) as object\n"
1323  " FROM pg_catalog.pg_opclass o\n"
1324  " JOIN pg_catalog.pg_am am ON "
1325  "o.opcmethod = am.oid\n"
1326  " JOIN pg_catalog.pg_namespace n ON "
1327  "n.oid = o.opcnamespace\n",
1328  gettext_noop("operator class"));
1329 
1330  if (!showSystem && !pattern)
1331  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1332  " AND n.nspname <> 'information_schema'\n");
1333 
1334  if (!validateSQLNamePattern(&buf, pattern, true, false,
1335  "n.nspname", "o.opcname", NULL,
1336  "pg_catalog.pg_opclass_is_visible(o.oid)",
1337  NULL, 3))
1338  goto error_return;
1339 
1340  /* Operator family descriptions */
1342  "UNION ALL\n"
1343  " SELECT opf.oid as oid, opf.tableoid as tableoid,\n"
1344  " n.nspname as nspname,\n"
1345  " CAST(opf.opfname AS pg_catalog.text) AS name,\n"
1346  " CAST('%s' AS pg_catalog.text) as object\n"
1347  " FROM pg_catalog.pg_opfamily opf\n"
1348  " JOIN pg_catalog.pg_am am "
1349  "ON opf.opfmethod = am.oid\n"
1350  " JOIN pg_catalog.pg_namespace n "
1351  "ON opf.opfnamespace = n.oid\n",
1352  gettext_noop("operator family"));
1353 
1354  if (!showSystem && !pattern)
1355  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1356  " AND n.nspname <> 'information_schema'\n");
1357 
1358  if (!validateSQLNamePattern(&buf, pattern, true, false,
1359  "n.nspname", "opf.opfname", NULL,
1360  "pg_catalog.pg_opfamily_is_visible(opf.oid)",
1361  NULL, 3))
1362  goto error_return;
1363 
1364  /* Rule descriptions (ignore rules for views) */
1366  "UNION ALL\n"
1367  " SELECT r.oid as oid, r.tableoid as tableoid,\n"
1368  " n.nspname as nspname,\n"
1369  " CAST(r.rulename AS pg_catalog.text) as name,"
1370  " CAST('%s' AS pg_catalog.text) as object\n"
1371  " FROM pg_catalog.pg_rewrite r\n"
1372  " JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
1373  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1374  " WHERE r.rulename != '_RETURN'\n",
1375  gettext_noop("rule"));
1376 
1377  if (!showSystem && !pattern)
1378  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1379  " AND n.nspname <> 'information_schema'\n");
1380 
1381  if (!validateSQLNamePattern(&buf, pattern, true, false,
1382  "n.nspname", "r.rulename", NULL,
1383  "pg_catalog.pg_table_is_visible(c.oid)",
1384  NULL, 3))
1385  goto error_return;
1386 
1387  /* Trigger descriptions */
1389  "UNION ALL\n"
1390  " SELECT t.oid as oid, t.tableoid as tableoid,\n"
1391  " n.nspname as nspname,\n"
1392  " CAST(t.tgname AS pg_catalog.text) as name,"
1393  " CAST('%s' AS pg_catalog.text) as object\n"
1394  " FROM pg_catalog.pg_trigger t\n"
1395  " JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
1396  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
1397  gettext_noop("trigger"));
1398 
1399  if (!showSystem && !pattern)
1400  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1401  " AND n.nspname <> 'information_schema'\n");
1402 
1403  if (!validateSQLNamePattern(&buf, pattern, !showSystem && !pattern, false,
1404  "n.nspname", "t.tgname", NULL,
1405  "pg_catalog.pg_table_is_visible(c.oid)",
1406  NULL, 3))
1407  goto error_return;
1408 
1410  ") AS tt\n"
1411  " JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
1412 
1413  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1414 
1415  res = PSQLexec(buf.data);
1416  termPQExpBuffer(&buf);
1417  if (!res)
1418  return false;
1419 
1420  myopt.title = _("Object descriptions");
1421  myopt.translate_header = true;
1422  myopt.translate_columns = translate_columns;
1423  myopt.n_translate_columns = lengthof(translate_columns);
1424 
1425  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1426 
1427  PQclear(res);
1428  return true;
1429 
1430 error_return:
1431  termPQExpBuffer(&buf);
1432  return false;
1433 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d().

◆ permissionsList()

bool permissionsList ( const char *  pattern,
bool  showSystem 
)

Definition at line 1011 of file describe.c.

1012 {
1014  PGresult *res;
1015  printQueryOpt myopt = pset.popt;
1016  static const bool translate_columns[] = {false, false, true, false, false, false};
1017 
1018  initPQExpBuffer(&buf);
1019 
1020  /*
1021  * we ignore indexes and toast tables since they have no meaningful rights
1022  */
1024  "SELECT n.nspname as \"%s\",\n"
1025  " c.relname as \"%s\",\n"
1026  " CASE c.relkind"
1027  " WHEN " CppAsString2(RELKIND_RELATION) " THEN '%s'"
1028  " WHEN " CppAsString2(RELKIND_VIEW) " THEN '%s'"
1029  " WHEN " CppAsString2(RELKIND_MATVIEW) " THEN '%s'"
1030  " WHEN " CppAsString2(RELKIND_SEQUENCE) " THEN '%s'"
1031  " WHEN " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN '%s'"
1032  " WHEN " CppAsString2(RELKIND_PARTITIONED_TABLE) " THEN '%s'"
1033  " END as \"%s\",\n"
1034  " ",
1035  gettext_noop("Schema"),
1036  gettext_noop("Name"),
1037  gettext_noop("table"),
1038  gettext_noop("view"),
1039  gettext_noop("materialized view"),
1040  gettext_noop("sequence"),
1041  gettext_noop("foreign table"),
1042  gettext_noop("partitioned table"),
1043  gettext_noop("Type"));
1044 
1045  printACLColumn(&buf, "c.relacl");
1046 
1047  /*
1048  * The formatting of attacl should match printACLColumn(). However, we
1049  * need no special case for an empty attacl, because the backend always
1050  * optimizes that back to NULL.
1051  */
1053  ",\n pg_catalog.array_to_string(ARRAY(\n"
1054  " SELECT attname || E':\\n ' || pg_catalog.array_to_string(attacl, E'\\n ')\n"
1055  " FROM pg_catalog.pg_attribute a\n"
1056  " WHERE attrelid = c.oid AND NOT attisdropped AND attacl IS NOT NULL\n"
1057  " ), E'\\n') AS \"%s\"",
1058  gettext_noop("Column privileges"));
1059 
1060  if (pset.sversion >= 90500 && pset.sversion < 100000)
1062  ",\n pg_catalog.array_to_string(ARRAY(\n"
1063  " SELECT polname\n"
1064  " || CASE WHEN polcmd != '*' THEN\n"
1065  " E' (' || polcmd::pg_catalog.text || E'):'\n"
1066  " ELSE E':'\n"
1067  " END\n"
1068  " || CASE WHEN polqual IS NOT NULL THEN\n"
1069  " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1070  " ELSE E''\n"
1071  " END\n"
1072  " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1073  " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1074  " ELSE E''\n"
1075  " END"
1076  " || CASE WHEN polroles <> '{0}' THEN\n"
1077  " E'\\n to: ' || pg_catalog.array_to_string(\n"
1078  " ARRAY(\n"
1079  " SELECT rolname\n"
1080  " FROM pg_catalog.pg_roles\n"
1081  " WHERE oid = ANY (polroles)\n"
1082  " ORDER BY 1\n"
1083  " ), E', ')\n"
1084  " ELSE E''\n"
1085  " END\n"
1086  " FROM pg_catalog.pg_policy pol\n"
1087  " WHERE polrelid = c.oid), E'\\n')\n"
1088  " AS \"%s\"",
1089  gettext_noop("Policies"));
1090 
1091  if (pset.sversion >= 100000)
1093  ",\n pg_catalog.array_to_string(ARRAY(\n"
1094  " SELECT polname\n"
1095  " || CASE WHEN NOT polpermissive THEN\n"
1096  " E' (RESTRICTIVE)'\n"
1097  " ELSE '' END\n"
1098  " || CASE WHEN polcmd != '*' THEN\n"
1099  " E' (' || polcmd::pg_catalog.text || E'):'\n"
1100  " ELSE E':'\n"
1101  " END\n"
1102  " || CASE WHEN polqual IS NOT NULL THEN\n"
1103  " E'\\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)\n"
1104  " ELSE E''\n"
1105  " END\n"
1106  " || CASE WHEN polwithcheck IS NOT NULL THEN\n"
1107  " E'\\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)\n"
1108  " ELSE E''\n"
1109  " END"
1110  " || CASE WHEN polroles <> '{0}' THEN\n"
1111  " E'\\n to: ' || pg_catalog.array_to_string(\n"
1112  " ARRAY(\n"
1113  " SELECT rolname\n"
1114  " FROM pg_catalog.pg_roles\n"
1115  " WHERE oid = ANY (polroles)\n"
1116  " ORDER BY 1\n"
1117  " ), E', ')\n"
1118  " ELSE E''\n"
1119  " END\n"
1120  " FROM pg_catalog.pg_policy pol\n"
1121  " WHERE polrelid = c.oid), E'\\n')\n"
1122  " AS \"%s\"",
1123  gettext_noop("Policies"));
1124 
1125  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c\n"
1126  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
1127  "WHERE c.relkind IN ("
1128  CppAsString2(RELKIND_RELATION) ","
1129  CppAsString2(RELKIND_VIEW) ","
1130  CppAsString2(RELKIND_MATVIEW) ","
1131  CppAsString2(RELKIND_SEQUENCE) ","
1132  CppAsString2(RELKIND_FOREIGN_TABLE) ","
1133  CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
1134 
1135  if (!showSystem && !pattern)
1136  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1137  " AND n.nspname <> 'information_schema'\n");
1138 
1139  if (!validateSQLNamePattern(&buf, pattern, true, false,
1140  "n.nspname", "c.relname", NULL,
1141  "pg_catalog.pg_table_is_visible(c.oid)",
1142  NULL, 3))
1143  goto error_return;
1144 
1145  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
1146 
1147  res = PSQLexec(buf.data);
1148  if (!res)
1149  goto error_return;
1150 
1151  printfPQExpBuffer(&buf, _("Access privileges"));
1152  myopt.title = buf.data;
1153  myopt.translate_header = true;
1154  myopt.translate_columns = translate_columns;
1155  myopt.n_translate_columns = lengthof(translate_columns);
1156 
1157  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1158 
1159  termPQExpBuffer(&buf);
1160  PQclear(res);
1161  return true;
1162 
1163 error_return:
1164  termPQExpBuffer(&buf);
1165  return false;
1166 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, CppAsString2, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, printQueryOpt::translate_header, and validateSQLNamePattern().

Referenced by exec_command_d(), and exec_command_z().