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:1199
#define lengthof(array)
Definition: c.h:791
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:6182
#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:100
FILE * logfile
Definition: settings.h:131
FILE * queryFout
Definition: settings.h:93
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 4564 of file describe.c.

4566 {
4568  PGresult *res;
4569  printQueryOpt myopt = pset.popt;
4570 
4571  initPQExpBuffer(&buf);
4573  "SELECT s.name AS \"%s\", "
4574  "pg_catalog.current_setting(s.name) AS \"%s\"",
4575  gettext_noop("Parameter"),
4576  gettext_noop("Value"));
4577 
4578  if (verbose)
4579  {
4581  ", s.vartype AS \"%s\", s.context AS \"%s\", ",
4582  gettext_noop("Type"),
4583  gettext_noop("Context"));
4584  if (pset.sversion >= 150000)
4585  printACLColumn(&buf, "p.paracl");
4586  else
4587  appendPQExpBuffer(&buf, "NULL AS \"%s\"",
4588  gettext_noop("Access privileges"));
4589  }
4590 
4591  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_settings s\n");
4592 
4593  if (verbose && pset.sversion >= 150000)
4595  " LEFT JOIN pg_catalog.pg_parameter_acl p\n"
4596  " ON pg_catalog.lower(s.name) = p.parname\n");
4597 
4598  if (pattern)
4599  processSQLNamePattern(pset.db, &buf, pattern,
4600  false, false,
4601  NULL, "pg_catalog.lower(s.name)", NULL,
4602  NULL, NULL, NULL);
4603  else
4604  appendPQExpBufferStr(&buf, "WHERE s.source <> 'default' AND\n"
4605  " s.setting IS DISTINCT FROM s.boot_val\n");
4606 
4607  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4608 
4609  res = PSQLexec(buf.data);
4610  termPQExpBuffer(&buf);
4611  if (!res)
4612  return false;
4613 
4614  if (pattern)
4615  myopt.title = _("List of configuration parameters");
4616  else
4617  myopt.title = _("List of non-default configuration parameters");
4618  myopt.translate_header = true;
4619 
4620  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4621 
4622  PQclear(res);
4623  return true;
4624 }
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6677
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:91

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 6357 of file describe.c.

6358 {
6360  int i;
6361  PGresult *res;
6362  bool has_pubtruncate;
6363  bool has_pubviaroot;
6364 
6365  PQExpBufferData title;
6366  printTableContent cont;
6367 
6368  if (pset.sversion < 100000)
6369  {
6370  char sverbuf[32];
6371 
6372  pg_log_error("The server (version %s) does not support publications.",
6374  sverbuf, sizeof(sverbuf)));
6375  return true;
6376  }
6377 
6378  has_pubtruncate = (pset.sversion >= 110000);
6379  has_pubviaroot = (pset.sversion >= 130000);
6380 
6381  initPQExpBuffer(&buf);
6382 
6384  "SELECT oid, pubname,\n"
6385  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
6386  " puballtables, pubinsert, pubupdate, pubdelete");
6387  if (has_pubtruncate)
6389  ", pubtruncate");
6390  if (has_pubviaroot)
6392  ", pubviaroot");
6394  "\nFROM pg_catalog.pg_publication\n");
6395 
6396  if (!validateSQLNamePattern(&buf, pattern, false, false,
6397  NULL, "pubname", NULL,
6398  NULL,
6399  NULL, 1))
6400  {
6401  termPQExpBuffer(&buf);
6402  return false;
6403  }
6404 
6405  appendPQExpBufferStr(&buf, "ORDER BY 2;");
6406 
6407  res = PSQLexec(buf.data);
6408  if (!res)
6409  {
6410  termPQExpBuffer(&buf);
6411  return false;
6412  }
6413 
6414  if (PQntuples(res) == 0)
6415  {
6416  if (!pset.quiet)
6417  {
6418  if (pattern)
6419  pg_log_error("Did not find any publication named \"%s\".",
6420  pattern);
6421  else
6422  pg_log_error("Did not find any publications.");
6423  }
6424 
6425  termPQExpBuffer(&buf);
6426  PQclear(res);
6427  return false;
6428  }
6429 
6430  for (i = 0; i < PQntuples(res); i++)
6431  {
6432  const char align = 'l';
6433  int ncols = 5;
6434  int nrows = 1;
6435  char *pubid = PQgetvalue(res, i, 0);
6436  char *pubname = PQgetvalue(res, i, 1);
6437  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
6438  printTableOpt myopt = pset.popt.topt;
6439 
6440  if (has_pubtruncate)
6441  ncols++;
6442  if (has_pubviaroot)
6443  ncols++;
6444 
6445  initPQExpBuffer(&title);
6446  printfPQExpBuffer(&title, _("Publication %s"), pubname);
6447  printTableInit(&cont, &myopt, title.data, ncols, nrows);
6448 
6449  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
6450  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
6451  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
6452  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
6453  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
6454  if (has_pubtruncate)
6455  printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
6456  if (has_pubviaroot)
6457  printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
6458 
6459  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
6460  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
6461  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
6462  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
6463  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
6464  if (has_pubtruncate)
6465  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
6466  if (has_pubviaroot)
6467  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
6468 
6469  if (!puballtables)
6470  {
6471  /* Get the tables for the specified publication */
6473  "SELECT n.nspname, c.relname");
6474  if (pset.sversion >= 150000)
6475  {
6477  ", pg_get_expr(pr.prqual, c.oid)");
6479  ", (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
6480  " pg_catalog.array_to_string("
6481  " ARRAY(SELECT attname\n"
6482  " FROM\n"
6483  " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
6484  " pg_catalog.pg_attribute\n"
6485  " WHERE attrelid = c.oid AND attnum = prattrs[s]), ', ')\n"
6486  " ELSE NULL END)");
6487  }
6488  else
6490  ", NULL, NULL");
6492  "\nFROM pg_catalog.pg_class c,\n"
6493  " pg_catalog.pg_namespace n,\n"
6494  " pg_catalog.pg_publication_rel pr\n"
6495  "WHERE c.relnamespace = n.oid\n"
6496  " AND c.oid = pr.prrelid\n"
6497  " AND pr.prpubid = '%s'\n"
6498  "ORDER BY 1,2", pubid);
6499  if (!addFooterToPublicationDesc(&buf, _("Tables:"), false, &cont))
6500  goto error_return;
6501 
6502  if (pset.sversion >= 150000)
6503  {
6504  /* Get the schemas for the specified publication */
6506  "SELECT n.nspname\n"
6507  "FROM pg_catalog.pg_namespace n\n"
6508  " JOIN pg_catalog.pg_publication_namespace pn ON n.oid = pn.pnnspid\n"
6509  "WHERE pn.pnpubid = '%s'\n"
6510  "ORDER BY 1", pubid);
6511  if (!addFooterToPublicationDesc(&buf, _("Tables from schemas:"),
6512  true, &cont))
6513  goto error_return;
6514  }
6515  }
6516 
6517  printTable(&cont, pset.queryFout, false, pset.logfile);
6518  printTableCleanup(&cont);
6519 
6520  termPQExpBuffer(&title);
6521  }
6522 
6523  termPQExpBuffer(&buf);
6524  PQclear(res);
6525 
6526  return true;
6527 
6528 error_return:
6529  printTableCleanup(&cont);
6530  PQclear(res);
6531  termPQExpBuffer(&buf);
6532  termPQExpBuffer(&title);
6533  return false;
6534 }
static bool addFooterToPublicationDesc(PQExpBuffer buf, const char *footermsg, bool as_schema, printTableContent *const cont)
Definition: describe.c:6311
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 3838 of file describe.c.

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

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 3622 of file describe.c.

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

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

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:330

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 4808 of file describe.c.

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

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 4926 of file describe.c.

4927 {
4929  PGresult *res;
4930  printQueryOpt myopt = pset.popt;
4931  static const bool translate_columns[] = {false, false, false, false, false, false, false, true, false};
4932 
4933  initPQExpBuffer(&buf);
4934 
4936  "SELECT\n"
4937  " n.nspname AS \"%s\",\n"
4938  " c.collname AS \"%s\",\n",
4939  gettext_noop("Schema"),
4940  gettext_noop("Name"));
4941 
4942  if (pset.sversion >= 100000)
4944  " CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'b' THEN 'builtin' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\",\n",
4945  gettext_noop("Provider"));
4946  else
4948  " 'libc' AS \"%s\",\n",
4949  gettext_noop("Provider"));
4950 
4952  " c.collcollate AS \"%s\",\n"
4953  " c.collctype AS \"%s\",\n",
4954  gettext_noop("Collate"),
4955  gettext_noop("Ctype"));
4956 
4957  if (pset.sversion >= 170000)
4959  " c.colllocale AS \"%s\",\n",
4960  gettext_noop("Locale"));
4961  else if (pset.sversion >= 150000)
4963  " c.colliculocale AS \"%s\",\n",
4964  gettext_noop("Locale"));
4965  else
4967  " c.collcollate AS \"%s\",\n",
4968  gettext_noop("Locale"));
4969 
4970  if (pset.sversion >= 160000)
4972  " c.collicurules AS \"%s\",\n",
4973  gettext_noop("ICU Rules"));
4974  else
4976  " NULL AS \"%s\",\n",
4977  gettext_noop("ICU Rules"));
4978 
4979  if (pset.sversion >= 120000)
4981  " CASE WHEN c.collisdeterministic THEN '%s' ELSE '%s' END AS \"%s\"",
4982  gettext_noop("yes"), gettext_noop("no"),
4983  gettext_noop("Deterministic?"));
4984  else
4986  " '%s' AS \"%s\"",
4987  gettext_noop("yes"),
4988  gettext_noop("Deterministic?"));
4989 
4990  if (verbose)
4992  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
4993  gettext_noop("Description"));
4994 
4996  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
4997  "WHERE n.oid = c.collnamespace\n");
4998 
4999  if (!showSystem && !pattern)
5000  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
5001  " AND n.nspname <> 'information_schema'\n");
5002 
5003  /*
5004  * Hide collations that aren't usable in the current database's encoding.
5005  * If you think to change this, note that pg_collation_is_visible rejects
5006  * unusable collations, so you will need to hack name pattern processing
5007  * somehow to avoid inconsistent behavior.
5008  */
5009  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
5010 
5011  if (!validateSQLNamePattern(&buf, pattern, true, false,
5012  "n.nspname", "c.collname", NULL,
5013  "pg_catalog.pg_collation_is_visible(c.oid)",
5014  NULL, 3))
5015  {
5016  termPQExpBuffer(&buf);
5017  return false;
5018  }
5019 
5020  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5021 
5022  res = PSQLexec(buf.data);
5023  termPQExpBuffer(&buf);
5024  if (!res)
5025  return false;
5026 
5027  myopt.title = _("List of collations");
5028  myopt.translate_header = true;
5029  myopt.translate_columns = translate_columns;
5030  myopt.n_translate_columns = lengthof(translate_columns);
5031 
5032  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5033 
5034  PQclear(res);
5035  return true;
5036 }

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 4484 of file describe.c.

4485 {
4487  PGresult *res;
4488  printQueryOpt myopt = pset.popt;
4489  static const bool translate_columns[] =
4490  {false, false, false, false, true, false};
4491 
4492  initPQExpBuffer(&buf);
4493 
4495  "SELECT n.nspname AS \"%s\",\n"
4496  " c.conname AS \"%s\",\n"
4497  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
4498  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
4499  " CASE WHEN c.condefault THEN '%s'\n"
4500  " ELSE '%s' END AS \"%s\"",
4501  gettext_noop("Schema"),
4502  gettext_noop("Name"),
4503  gettext_noop("Source"),
4504  gettext_noop("Destination"),
4505  gettext_noop("yes"), gettext_noop("no"),
4506  gettext_noop("Default?"));
4507 
4508  if (verbose)
4510  ",\n d.description AS \"%s\"",
4511  gettext_noop("Description"));
4512 
4514  "\nFROM pg_catalog.pg_conversion c\n"
4515  " JOIN pg_catalog.pg_namespace n "
4516  "ON n.oid = c.connamespace\n");
4517 
4518  if (verbose)
4520  "LEFT JOIN pg_catalog.pg_description d "
4521  "ON d.classoid = c.tableoid\n"
4522  " AND d.objoid = c.oid "
4523  "AND d.objsubid = 0\n");
4524 
4525  appendPQExpBufferStr(&buf, "WHERE true\n");
4526 
4527  if (!showSystem && !pattern)
4528  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4529  " AND n.nspname <> 'information_schema'\n");
4530 
4531  if (!validateSQLNamePattern(&buf, pattern, true, false,
4532  "n.nspname", "c.conname", NULL,
4533  "pg_catalog.pg_conversion_is_visible(c.oid)",
4534  NULL, 3))
4535  {
4536  termPQExpBuffer(&buf);
4537  return false;
4538  }
4539 
4540  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4541 
4542  res = PSQLexec(buf.data);
4543  termPQExpBuffer(&buf);
4544  if (!res)
4545  return false;
4546 
4547  myopt.title = _("List of conversions");
4548  myopt.translate_header = true;
4549  myopt.translate_columns = translate_columns;
4550  myopt.n_translate_columns = lengthof(translate_columns);
4551 
4552  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4553 
4554  PQclear(res);
4555  return true;
4556 }

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 3769 of file describe.c.

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

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 4401 of file describe.c.

4402 {
4404  PGresult *res;
4405  printQueryOpt myopt = pset.popt;
4406 
4407  initPQExpBuffer(&buf);
4408 
4410  "SELECT n.nspname as \"%s\",\n"
4411  " t.typname as \"%s\",\n"
4412  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
4413  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
4414  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n"
4415  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
4416  " t.typdefault as \"%s\",\n"
4417  " pg_catalog.array_to_string(ARRAY(\n"
4418  " 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"
4419  " ), ' ') as \"%s\"",
4420  gettext_noop("Schema"),
4421  gettext_noop("Name"),
4422  gettext_noop("Type"),
4423  gettext_noop("Collation"),
4424  gettext_noop("Nullable"),
4425  gettext_noop("Default"),
4426  gettext_noop("Check"));
4427 
4428  if (verbose)
4429  {
4430  appendPQExpBufferStr(&buf, ",\n ");
4431  printACLColumn(&buf, "t.typacl");
4433  ",\n d.description as \"%s\"",
4434  gettext_noop("Description"));
4435  }
4436 
4438  "\nFROM pg_catalog.pg_type t\n"
4439  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
4440 
4441  if (verbose)
4443  " LEFT JOIN pg_catalog.pg_description d "
4444  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
4445  "AND d.objsubid = 0\n");
4446 
4447  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
4448 
4449  if (!showSystem && !pattern)
4450  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4451  " AND n.nspname <> 'information_schema'\n");
4452 
4453  if (!validateSQLNamePattern(&buf, pattern, true, false,
4454  "n.nspname", "t.typname", NULL,
4455  "pg_catalog.pg_type_is_visible(t.oid)",
4456  NULL, 3))
4457  {
4458  termPQExpBuffer(&buf);
4459  return false;
4460  }
4461 
4462  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4463 
4464  res = PSQLexec(buf.data);
4465  termPQExpBuffer(&buf);
4466  if (!res)
4467  return false;
4468 
4469  myopt.title = _("List of domains");
4470  myopt.translate_header = true;
4471 
4472  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4473 
4474  PQclear(res);
4475  return true;
4476 }

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 4632 of file describe.c.

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

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 4712 of file describe.c.

4713 {
4715  PGresult *res;
4716  printQueryOpt myopt = pset.popt;
4717 
4718  if (pset.sversion < 100000)
4719  {
4720  char sverbuf[32];
4721 
4722  pg_log_error("The server (version %s) does not support extended statistics.",
4724  sverbuf, sizeof(sverbuf)));
4725  return true;
4726  }
4727 
4728  initPQExpBuffer(&buf);
4730  "SELECT \n"
4731  "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS \"%s\", \n"
4732  "es.stxname AS \"%s\", \n",
4733  gettext_noop("Schema"),
4734  gettext_noop("Name"));
4735 
4736  if (pset.sversion >= 140000)
4738  "pg_catalog.format('%%s FROM %%s', \n"
4739  " pg_catalog.pg_get_statisticsobjdef_columns(es.oid), \n"
4740  " es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4741  gettext_noop("Definition"));
4742  else
4744  "pg_catalog.format('%%s FROM %%s', \n"
4745  " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(a.attname),', ') \n"
4746  " FROM pg_catalog.unnest(es.stxkeys) s(attnum) \n"
4747  " JOIN pg_catalog.pg_attribute a \n"
4748  " ON (es.stxrelid = a.attrelid \n"
4749  " AND a.attnum = s.attnum \n"
4750  " AND NOT a.attisdropped)), \n"
4751  "es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4752  gettext_noop("Definition"));
4753 
4755  ",\nCASE WHEN 'd' = any(es.stxkind) THEN 'defined' \n"
4756  "END AS \"%s\", \n"
4757  "CASE WHEN 'f' = any(es.stxkind) THEN 'defined' \n"
4758  "END AS \"%s\"",
4759  gettext_noop("Ndistinct"),
4760  gettext_noop("Dependencies"));
4761 
4762  /*
4763  * Include the MCV statistics kind.
4764  */
4765  if (pset.sversion >= 120000)
4766  {
4768  ",\nCASE WHEN 'm' = any(es.stxkind) THEN 'defined' \n"
4769  "END AS \"%s\" ",
4770  gettext_noop("MCV"));
4771  }
4772 
4774  " \nFROM pg_catalog.pg_statistic_ext es \n");
4775 
4776  if (!validateSQLNamePattern(&buf, pattern,
4777  false, false,
4778  "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text", "es.stxname",
4779  NULL, "pg_catalog.pg_statistics_obj_is_visible(es.oid)",
4780  NULL, 3))
4781  {
4782  termPQExpBuffer(&buf);
4783  return false;
4784  }
4785 
4786  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4787 
4788  res = PSQLexec(buf.data);
4789  termPQExpBuffer(&buf);
4790  if (!res)
4791  return false;
4792 
4793  myopt.title = _("List of extended statistics");
4794  myopt.translate_header = true;
4795 
4796  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4797 
4798  PQclear(res);
4799  return true;
4800 }

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 6071 of file describe.c.

6072 {
6074  PGresult *res;
6075  int i;
6076 
6077  initPQExpBuffer(&buf);
6079  "SELECT e.extname, e.oid\n"
6080  "FROM pg_catalog.pg_extension e\n");
6081 
6082  if (!validateSQLNamePattern(&buf, pattern,
6083  false, false,
6084  NULL, "e.extname", NULL,
6085  NULL,
6086  NULL, 1))
6087  {
6088  termPQExpBuffer(&buf);
6089  return false;
6090  }
6091 
6092  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6093 
6094  res = PSQLexec(buf.data);
6095  termPQExpBuffer(&buf);
6096  if (!res)
6097  return false;
6098 
6099  if (PQntuples(res) == 0)
6100  {
6101  if (!pset.quiet)
6102  {
6103  if (pattern)
6104  pg_log_error("Did not find any extension named \"%s\".",
6105  pattern);
6106  else
6107  pg_log_error("Did not find any extensions.");
6108  }
6109  PQclear(res);
6110  return false;
6111  }
6112 
6113  for (i = 0; i < PQntuples(res); i++)
6114  {
6115  const char *extname;
6116  const char *oid;
6117 
6118  extname = PQgetvalue(res, i, 0);
6119  oid = PQgetvalue(res, i, 1);
6120 
6121  if (!listOneExtensionContents(extname, oid))
6122  {
6123  PQclear(res);
6124  return false;
6125  }
6126  if (cancel_pressed)
6127  {
6128  PQclear(res);
6129  return false;
6130  }
6131  }
6132 
6133  PQclear(res);
6134  return true;
6135 }
static bool listOneExtensionContents(const char *extname, const char *oid)
Definition: describe.c:6138

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 6020 of file describe.c.

6021 {
6023  PGresult *res;
6024  printQueryOpt myopt = pset.popt;
6025 
6026  initPQExpBuffer(&buf);
6028  "SELECT e.extname AS \"%s\", "
6029  "e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"
6030  "FROM pg_catalog.pg_extension e "
6031  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "
6032  "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "
6033  "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
6034  gettext_noop("Name"),
6035  gettext_noop("Version"),
6036  gettext_noop("Schema"),
6037  gettext_noop("Description"));
6038 
6039  if (!validateSQLNamePattern(&buf, pattern,
6040  false, false,
6041  NULL, "e.extname", NULL,
6042  NULL,
6043  NULL, 1))
6044  {
6045  termPQExpBuffer(&buf);
6046  return false;
6047  }
6048 
6049  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6050 
6051  res = PSQLexec(buf.data);
6052  termPQExpBuffer(&buf);
6053  if (!res)
6054  return false;
6055 
6056  myopt.title = _("List of installed extensions");
6057  myopt.translate_header = true;
6058 
6059  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6060 
6061  PQclear(res);
6062  return true;
6063 }

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 5746 of file describe.c.

5747 {
5749  PGresult *res;
5750  printQueryOpt myopt = pset.popt;
5751 
5752  initPQExpBuffer(&buf);
5754  "SELECT fdw.fdwname AS \"%s\",\n"
5755  " pg_catalog.pg_get_userbyid(fdw.fdwowner) AS \"%s\",\n"
5756  " fdw.fdwhandler::pg_catalog.regproc AS \"%s\",\n"
5757  " fdw.fdwvalidator::pg_catalog.regproc AS \"%s\"",
5758  gettext_noop("Name"),
5759  gettext_noop("Owner"),
5760  gettext_noop("Handler"),
5761  gettext_noop("Validator"));
5762 
5763  if (verbose)
5764  {
5765  appendPQExpBufferStr(&buf, ",\n ");
5766  printACLColumn(&buf, "fdwacl");
5768  ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
5769  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5770  " pg_catalog.quote_ident(option_name) || ' ' || "
5771  " pg_catalog.quote_literal(option_value) FROM "
5772  " pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
5773  " END AS \"%s\""
5774  ",\n d.description AS \"%s\" ",
5775  gettext_noop("FDW options"),
5776  gettext_noop("Description"));
5777  }
5778 
5779  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_foreign_data_wrapper fdw\n");
5780 
5781  if (verbose)
5783  "LEFT JOIN pg_catalog.pg_description d\n"
5784  " ON d.classoid = fdw.tableoid "
5785  "AND d.objoid = fdw.oid AND d.objsubid = 0\n");
5786 
5787  if (!validateSQLNamePattern(&buf, pattern, false, false,
5788  NULL, "fdwname", NULL, NULL,
5789  NULL, 1))
5790  {
5791  termPQExpBuffer(&buf);
5792  return false;
5793  }
5794 
5795  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5796 
5797  res = PSQLexec(buf.data);
5798  termPQExpBuffer(&buf);
5799  if (!res)
5800  return false;
5801 
5802  myopt.title = _("List of foreign-data wrappers");
5803  myopt.translate_header = true;
5804 
5805  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5806 
5807  PQclear(res);
5808  return true;
5809 }

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 5817 of file describe.c.

5818 {
5820  PGresult *res;
5821  printQueryOpt myopt = pset.popt;
5822 
5823  initPQExpBuffer(&buf);
5825  "SELECT s.srvname AS \"%s\",\n"
5826  " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n"
5827  " f.fdwname AS \"%s\"",
5828  gettext_noop("Name"),
5829  gettext_noop("Owner"),
5830  gettext_noop("Foreign-data wrapper"));
5831 
5832  if (verbose)
5833  {
5834  appendPQExpBufferStr(&buf, ",\n ");
5835  printACLColumn(&buf, "s.srvacl");
5837  ",\n"
5838  " s.srvtype AS \"%s\",\n"
5839  " s.srvversion AS \"%s\",\n"
5840  " CASE WHEN srvoptions IS NULL THEN '' ELSE "
5841  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5842  " pg_catalog.quote_ident(option_name) || ' ' || "
5843  " pg_catalog.quote_literal(option_value) FROM "
5844  " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
5845  " END AS \"%s\",\n"
5846  " d.description AS \"%s\"",
5847  gettext_noop("Type"),
5848  gettext_noop("Version"),
5849  gettext_noop("FDW options"),
5850  gettext_noop("Description"));
5851  }
5852 
5854  "\nFROM pg_catalog.pg_foreign_server s\n"
5855  " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n");
5856 
5857  if (verbose)
5859  "LEFT JOIN pg_catalog.pg_description d\n "
5860  "ON d.classoid = s.tableoid AND d.objoid = s.oid "
5861  "AND d.objsubid = 0\n");
5862 
5863  if (!validateSQLNamePattern(&buf, pattern, false, false,
5864  NULL, "s.srvname", NULL, NULL,
5865  NULL, 1))
5866  {
5867  termPQExpBuffer(&buf);
5868  return false;
5869  }
5870 
5871  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5872 
5873  res = PSQLexec(buf.data);
5874  termPQExpBuffer(&buf);
5875  if (!res)
5876  return false;
5877 
5878  myopt.title = _("List of foreign servers");
5879  myopt.translate_header = true;
5880 
5881  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5882 
5883  PQclear(res);
5884  return true;
5885 }

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 5948 of file describe.c.

5949 {
5951  PGresult *res;
5952  printQueryOpt myopt = pset.popt;
5953 
5954  initPQExpBuffer(&buf);
5956  "SELECT n.nspname AS \"%s\",\n"
5957  " c.relname AS \"%s\",\n"
5958  " s.srvname AS \"%s\"",
5959  gettext_noop("Schema"),
5960  gettext_noop("Table"),
5961  gettext_noop("Server"));
5962 
5963  if (verbose)
5965  ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
5966  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5967  " pg_catalog.quote_ident(option_name) || ' ' || "
5968  " pg_catalog.quote_literal(option_value) FROM "
5969  " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
5970  " END AS \"%s\",\n"
5971  " d.description AS \"%s\"",
5972  gettext_noop("FDW options"),
5973  gettext_noop("Description"));
5974 
5976  "\nFROM pg_catalog.pg_foreign_table ft\n"
5977  " INNER JOIN pg_catalog.pg_class c"
5978  " ON c.oid = ft.ftrelid\n"
5979  " INNER JOIN pg_catalog.pg_namespace n"
5980  " ON n.oid = c.relnamespace\n"
5981  " INNER JOIN pg_catalog.pg_foreign_server s"
5982  " ON s.oid = ft.ftserver\n");
5983  if (verbose)
5985  " LEFT JOIN pg_catalog.pg_description d\n"
5986  " ON d.classoid = c.tableoid AND "
5987  "d.objoid = c.oid AND d.objsubid = 0\n");
5988 
5989  if (!validateSQLNamePattern(&buf, pattern, false, false,
5990  "n.nspname", "c.relname", NULL,
5991  "pg_catalog.pg_table_is_visible(c.oid)",
5992  NULL, 3))
5993  {
5994  termPQExpBuffer(&buf);
5995  return false;
5996  }
5997 
5998  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5999 
6000  res = PSQLexec(buf.data);
6001  termPQExpBuffer(&buf);
6002  if (!res)
6003  return false;
6004 
6005  myopt.title = _("List of foreign tables");
6006  myopt.translate_header = true;
6007 
6008  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6009 
6010  PQclear(res);
6011  return true;
6012 }

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 4325 of file describe.c.

4326 {
4328  PGresult *res;
4329  printQueryOpt myopt = pset.popt;
4330 
4331  initPQExpBuffer(&buf);
4332 
4334  "SELECT l.lanname AS \"%s\",\n"
4335  " pg_catalog.pg_get_userbyid(l.lanowner) as \"%s\",\n"
4336  " l.lanpltrusted AS \"%s\"",
4337  gettext_noop("Name"),
4338  gettext_noop("Owner"),
4339  gettext_noop("Trusted"));
4340 
4341  if (verbose)
4342  {
4344  ",\n NOT l.lanispl AS \"%s\",\n"
4345  " l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
4346  " l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n "
4347  "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
4348  gettext_noop("Internal language"),
4349  gettext_noop("Call handler"),
4350  gettext_noop("Validator"),
4351  gettext_noop("Inline handler"));
4352  printACLColumn(&buf, "l.lanacl");
4353  }
4354 
4356  ",\n d.description AS \"%s\""
4357  "\nFROM pg_catalog.pg_language l\n"
4358  "LEFT JOIN pg_catalog.pg_description d\n"
4359  " ON d.classoid = l.tableoid AND d.objoid = l.oid\n"
4360  " AND d.objsubid = 0\n",
4361  gettext_noop("Description"));
4362 
4363  if (pattern)
4364  {
4365  if (!validateSQLNamePattern(&buf, pattern, false, false,
4366  NULL, "l.lanname", NULL, NULL,
4367  NULL, 2))
4368  {
4369  termPQExpBuffer(&buf);
4370  return false;
4371  }
4372  }
4373 
4374  if (!showSystem && !pattern)
4375  appendPQExpBufferStr(&buf, "WHERE l.lanplcallfoid != 0\n");
4376 
4377 
4378  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4379 
4380  res = PSQLexec(buf.data);
4381  termPQExpBuffer(&buf);
4382  if (!res)
4383  return false;
4384 
4385  myopt.title = _("List of languages");
4386  myopt.translate_header = true;
4387 
4388  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4389 
4390  PQclear(res);
4391  return true;
4392 }

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 7072 of file describe.c.

7073 {
7075  PGresult *res;
7076  printQueryOpt myopt = pset.popt;
7077 
7078  initPQExpBuffer(&buf);
7079 
7081  "SELECT oid as \"%s\",\n"
7082  " pg_catalog.pg_get_userbyid(lomowner) as \"%s\",\n ",
7083  gettext_noop("ID"),
7084  gettext_noop("Owner"));
7085 
7086  if (verbose)
7087  {
7088  printACLColumn(&buf, "lomacl");
7089  appendPQExpBufferStr(&buf, ",\n ");
7090  }
7091 
7093  "pg_catalog.obj_description(oid, 'pg_largeobject') as \"%s\"\n"
7094  "FROM pg_catalog.pg_largeobject_metadata\n"
7095  "ORDER BY oid",
7096  gettext_noop("Description"));
7097 
7098  res = PSQLexec(buf.data);
7099  termPQExpBuffer(&buf);
7100  if (!res)
7101  return false;
7102 
7103  myopt.title = _("Large objects");
7104  myopt.translate_header = true;
7105 
7106  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
7107 
7108  PQclear(res);
7109  return true;
7110 }

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 6695 of file describe.c.

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

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 6796 of file describe.c.

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

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 6983 of file describe.c.

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

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 6885 of file describe.c.

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

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 4115 of file describe.c.

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

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 6235 of file describe.c.

6236 {
6238  PGresult *res;
6239  printQueryOpt myopt = pset.popt;
6240  static const bool translate_columns[] = {false, false, false, false, false, false, false, false};
6241 
6242  if (pset.sversion < 100000)
6243  {
6244  char sverbuf[32];
6245 
6246  pg_log_error("The server (version %s) does not support publications.",
6248  sverbuf, sizeof(sverbuf)));
6249  return true;
6250  }
6251 
6252  initPQExpBuffer(&buf);
6253 
6255  "SELECT pubname AS \"%s\",\n"
6256  " pg_catalog.pg_get_userbyid(pubowner) AS \"%s\",\n"
6257  " puballtables AS \"%s\",\n"
6258  " pubinsert AS \"%s\",\n"
6259  " pubupdate AS \"%s\",\n"
6260  " pubdelete AS \"%s\"",
6261  gettext_noop("Name"),
6262  gettext_noop("Owner"),
6263  gettext_noop("All tables"),
6264  gettext_noop("Inserts"),
6265  gettext_noop("Updates"),
6266  gettext_noop("Deletes"));
6267  if (pset.sversion >= 110000)
6269  ",\n pubtruncate AS \"%s\"",
6270  gettext_noop("Truncates"));
6271  if (pset.sversion >= 130000)
6273  ",\n pubviaroot AS \"%s\"",
6274  gettext_noop("Via root"));
6275 
6277  "\nFROM pg_catalog.pg_publication\n");
6278 
6279  if (!validateSQLNamePattern(&buf, pattern, false, false,
6280  NULL, "pubname", NULL,
6281  NULL,
6282  NULL, 1))
6283  {
6284  termPQExpBuffer(&buf);
6285  return false;
6286  }
6287 
6288  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6289 
6290  res = PSQLexec(buf.data);
6291  termPQExpBuffer(&buf);
6292  if (!res)
6293  return false;
6294 
6295  myopt.title = _("List of publications");
6296  myopt.translate_header = true;
6297  myopt.translate_columns = translate_columns;
6298  myopt.n_translate_columns = lengthof(translate_columns);
6299 
6300  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6301 
6302  PQclear(res);
6303 
6304  return true;
6305 }

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 5044 of file describe.c.

5045 {
5047  PGresult *res;
5048  printQueryOpt myopt = pset.popt;
5049  int pub_schema_tuples = 0;
5050  char **footers = NULL;
5051 
5052  initPQExpBuffer(&buf);
5054  "SELECT n.nspname AS \"%s\",\n"
5055  " pg_catalog.pg_get_userbyid(n.nspowner) AS \"%s\"",
5056  gettext_noop("Name"),
5057  gettext_noop("Owner"));
5058 
5059  if (verbose)
5060  {
5061  appendPQExpBufferStr(&buf, ",\n ");
5062  printACLColumn(&buf, "n.nspacl");
5064  ",\n pg_catalog.obj_description(n.oid, 'pg_namespace') AS \"%s\"",
5065  gettext_noop("Description"));
5066  }
5067 
5069  "\nFROM pg_catalog.pg_namespace n\n");
5070 
5071  if (!showSystem && !pattern)
5073  "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n");
5074 
5075  if (!validateSQLNamePattern(&buf, pattern,
5076  !showSystem && !pattern, false,
5077  NULL, "n.nspname", NULL,
5078  NULL,
5079  NULL, 2))
5080  goto error_return;
5081 
5082  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5083 
5084  res = PSQLexec(buf.data);
5085  if (!res)
5086  goto error_return;
5087 
5088  myopt.title = _("List of schemas");
5089  myopt.translate_header = true;
5090 
5091  if (pattern && pset.sversion >= 150000)
5092  {
5093  PGresult *result;
5094  int i;
5095 
5097  "SELECT pubname \n"
5098  "FROM pg_catalog.pg_publication p\n"
5099  " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
5100  " JOIN pg_catalog.pg_namespace n ON n.oid = pn.pnnspid \n"
5101  "WHERE n.nspname = '%s'\n"
5102  "ORDER BY 1",
5103  pattern);
5104  result = PSQLexec(buf.data);
5105  if (!result)
5106  goto error_return;
5107  else
5108  pub_schema_tuples = PQntuples(result);
5109 
5110  if (pub_schema_tuples > 0)
5111  {
5112  /*
5113  * Allocate memory for footers. Size of footers will be 1 (for
5114  * storing "Publications:" string) + publication schema mapping
5115  * count + 1 (for storing NULL).
5116  */
5117  footers = (char **) pg_malloc((1 + pub_schema_tuples + 1) * sizeof(char *));
5118  footers[0] = pg_strdup(_("Publications:"));
5119 
5120  /* Might be an empty set - that's ok */
5121  for (i = 0; i < pub_schema_tuples; i++)
5122  {
5123  printfPQExpBuffer(&buf, " \"%s\"",
5124  PQgetvalue(result, i, 0));
5125 
5126  footers[i + 1] = pg_strdup(buf.data);
5127  }
5128 
5129  footers[i + 1] = NULL;
5130  myopt.footers = footers;
5131  }
5132 
5133  PQclear(result);
5134  }
5135 
5136  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5137 
5138  termPQExpBuffer(&buf);
5139  PQclear(res);
5140 
5141  /* Free the memory allocated for the footer */
5142  if (footers)
5143  {
5144  char **footer = NULL;
5145 
5146  for (footer = footers; *footer; footer++)
5147  pg_free(*footer);
5148 
5149  pg_free(footers);
5150  }
5151 
5152  return true;
5153 
5154 error_return:
5155  termPQExpBuffer(&buf);
5156  return false;
5157 }
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 3917 of file describe.c.

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

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 5542 of file describe.c.

5543 {
5545  PGresult *res;
5546  printQueryOpt myopt = pset.popt;
5547 
5548  if (verbose)
5549  return listTSConfigsVerbose(pattern);
5550 
5551  initPQExpBuffer(&buf);
5552 
5554  "SELECT\n"
5555  " n.nspname as \"%s\",\n"
5556  " c.cfgname as \"%s\",\n"
5557  " pg_catalog.obj_description(c.oid, 'pg_ts_config') as \"%s\"\n"
5558  "FROM pg_catalog.pg_ts_config c\n"
5559  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace\n",
5560  gettext_noop("Schema"),
5561  gettext_noop("Name"),
5562  gettext_noop("Description")
5563  );
5564 
5565  if (!validateSQLNamePattern(&buf, pattern, false, false,
5566  "n.nspname", "c.cfgname", NULL,
5567  "pg_catalog.pg_ts_config_is_visible(c.oid)",
5568  NULL, 3))
5569  {
5570  termPQExpBuffer(&buf);
5571  return false;
5572  }
5573 
5574  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5575 
5576  res = PSQLexec(buf.data);
5577  termPQExpBuffer(&buf);
5578  if (!res)
5579  return false;
5580 
5581  myopt.title = _("List of text search configurations");
5582  myopt.translate_header = true;
5583 
5584  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5585 
5586  PQclear(res);
5587  return true;
5588 }
static bool listTSConfigsVerbose(const char *pattern)
Definition: describe.c:5591

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 5412 of file describe.c.

5413 {
5415  PGresult *res;
5416  printQueryOpt myopt = pset.popt;
5417 
5418  initPQExpBuffer(&buf);
5419 
5421  "SELECT\n"
5422  " n.nspname as \"%s\",\n"
5423  " d.dictname as \"%s\",\n",
5424  gettext_noop("Schema"),
5425  gettext_noop("Name"));
5426 
5427  if (verbose)
5428  {
5430  " ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM\n"
5431  " pg_catalog.pg_ts_template t\n"
5432  " LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace\n"
5433  " WHERE d.dicttemplate = t.oid ) AS \"%s\",\n"
5434  " d.dictinitoption as \"%s\",\n",
5435  gettext_noop("Template"),
5436  gettext_noop("Init options"));
5437  }
5438 
5440  " pg_catalog.obj_description(d.oid, 'pg_ts_dict') as \"%s\"\n",
5441  gettext_noop("Description"));
5442 
5443  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_dict d\n"
5444  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace\n");
5445 
5446  if (!validateSQLNamePattern(&buf, pattern, false, false,
5447  "n.nspname", "d.dictname", NULL,
5448  "pg_catalog.pg_ts_dict_is_visible(d.oid)",
5449  NULL, 3))
5450  {
5451  termPQExpBuffer(&buf);
5452  return false;
5453  }
5454 
5455  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5456 
5457  res = PSQLexec(buf.data);
5458  termPQExpBuffer(&buf);
5459  if (!res)
5460  return false;
5461 
5462  myopt.title = _("List of text search dictionaries");
5463  myopt.translate_header = true;
5464 
5465  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5466 
5467  PQclear(res);
5468  return true;
5469 }

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 5165 of file describe.c.

5166 {
5168  PGresult *res;
5169  printQueryOpt myopt = pset.popt;
5170 
5171  if (verbose)
5172  return listTSParsersVerbose(pattern);
5173 
5174  initPQExpBuffer(&buf);
5175 
5177  "SELECT\n"
5178  " n.nspname as \"%s\",\n"
5179  " p.prsname as \"%s\",\n"
5180  " pg_catalog.obj_description(p.oid, 'pg_ts_parser') as \"%s\"\n"
5181  "FROM pg_catalog.pg_ts_parser p\n"
5182  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.prsnamespace\n",
5183  gettext_noop("Schema"),
5184  gettext_noop("Name"),
5185  gettext_noop("Description")
5186  );
5187 
5188  if (!validateSQLNamePattern(&buf, pattern, false, false,
5189  "n.nspname", "p.prsname", NULL,
5190  "pg_catalog.pg_ts_parser_is_visible(p.oid)",
5191  NULL, 3))
5192  {
5193  termPQExpBuffer(&buf);
5194  return false;
5195  }
5196 
5197  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5198 
5199  res = PSQLexec(buf.data);
5200  termPQExpBuffer(&buf);
5201  if (!res)
5202  return false;
5203 
5204  myopt.title = _("List of text search parsers");
5205  myopt.translate_header = true;
5206 
5207  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5208 
5209  PQclear(res);
5210  return true;
5211 }
static bool listTSParsersVerbose(const char *pattern)
Definition: describe.c:5217

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 5477 of file describe.c.

5478 {
5480  PGresult *res;
5481  printQueryOpt myopt = pset.popt;
5482 
5483  initPQExpBuffer(&buf);
5484 
5485  if (verbose)
5487  "SELECT\n"
5488  " n.nspname AS \"%s\",\n"
5489  " t.tmplname AS \"%s\",\n"
5490  " t.tmplinit::pg_catalog.regproc AS \"%s\",\n"
5491  " t.tmpllexize::pg_catalog.regproc AS \"%s\",\n"
5492  " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
5493  gettext_noop("Schema"),
5494  gettext_noop("Name"),
5495  gettext_noop("Init"),
5496  gettext_noop("Lexize"),
5497  gettext_noop("Description"));
5498  else
5500  "SELECT\n"
5501  " n.nspname AS \"%s\",\n"
5502  " t.tmplname AS \"%s\",\n"
5503  " pg_catalog.obj_description(t.oid, 'pg_ts_template') AS \"%s\"\n",
5504  gettext_noop("Schema"),
5505  gettext_noop("Name"),
5506  gettext_noop("Description"));
5507 
5508  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_ts_template t\n"
5509  "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace\n");
5510 
5511  if (!validateSQLNamePattern(&buf, pattern, false, false,
5512  "n.nspname", "t.tmplname", NULL,
5513  "pg_catalog.pg_ts_template_is_visible(t.oid)",
5514  NULL, 3))
5515  {
5516  termPQExpBuffer(&buf);
5517  return false;
5518  }
5519 
5520  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5521 
5522  res = PSQLexec(buf.data);
5523  termPQExpBuffer(&buf);
5524  if (!res)
5525  return false;
5526 
5527  myopt.title = _("List of text search templates");
5528  myopt.translate_header = true;
5529 
5530  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5531 
5532  PQclear(res);
5533  return true;
5534 }

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 5893 of file describe.c.

5894 {
5896  PGresult *res;
5897  printQueryOpt myopt = pset.popt;
5898 
5899  initPQExpBuffer(&buf);
5901  "SELECT um.srvname AS \"%s\",\n"
5902  " um.usename AS \"%s\"",
5903  gettext_noop("Server"),
5904  gettext_noop("User name"));
5905 
5906  if (verbose)
5908  ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
5909  " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
5910  " pg_catalog.quote_ident(option_name) || ' ' || "
5911  " pg_catalog.quote_literal(option_value) FROM "
5912  " pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
5913  " END AS \"%s\"",
5914  gettext_noop("FDW options"));
5915 
5916  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_user_mappings um\n");
5917 
5918  if (!validateSQLNamePattern(&buf, pattern, false, false,
5919  NULL, "um.srvname", "um.usename", NULL,
5920  NULL, 1))
5921  {
5922  termPQExpBuffer(&buf);
5923  return false;
5924  }
5925 
5926  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
5927 
5928  res = PSQLexec(buf.data);
5929  termPQExpBuffer(&buf);
5930  if (!res)
5931  return false;
5932 
5933  myopt.title = _("List of user mappings");
5934  myopt.translate_header = true;
5935 
5936  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5937 
5938  PQclear(res);
5939  return true;
5940 }

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().