PostgreSQL Source Code  git master
describe.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include "catalog/pg_am.h"
#include "catalog/pg_attribute_d.h"
#include "catalog/pg_cast_d.h"
#include "catalog/pg_class_d.h"
#include "catalog/pg_default_acl_d.h"
#include "common.h"
#include "common/logging.h"
#include "describe.h"
#include "fe_utils/mbprint.h"
#include "fe_utils/print.h"
#include "fe_utils/string_utils.h"
#include "settings.h"
#include "variables.h"
Include dependency graph for describe.c:

Go to the source code of this file.

Functions

static const char * map_typename_pattern (const char *pattern)
 
static bool describeOneTableDetails (const char *schemaname, const char *relationname, const char *oid, bool verbose)
 
static void add_tablespace_footer (printTableContent *const cont, char relkind, Oid tablespace, const bool newline)
 
static void add_role_attribute (PQExpBuffer buf, const char *const str)
 
static bool listTSParsersVerbose (const char *pattern)
 
static bool describeOneTSParser (const char *oid, const char *nspname, const char *prsname)
 
static bool listTSConfigsVerbose (const char *pattern)
 
static bool describeOneTSConfig (const char *oid, const char *nspname, const char *cfgname, const char *pnspname, const char *prsname)
 
static void printACLColumn (PQExpBuffer buf, const char *colname)
 
static bool listOneExtensionContents (const char *extname, const char *oid)
 
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)
 
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 listAllDbs (const char *pattern, bool verbose)
 
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 describeRoles (const char *pattern, bool verbose, bool showSystem)
 
bool listDbRoleSettings (const char *pattern, const char *pattern2)
 
bool listTables (const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
 
bool listPartitionedTables (const char *reltypes, const char *pattern, bool verbose)
 
bool listLanguages (const char *pattern, bool verbose, bool showSystem)
 
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 listEventTriggers (const char *pattern, bool verbose)
 
bool listExtendedStats (const char *pattern)
 
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 listTSParsers (const char *pattern, bool verbose)
 
bool listTSDictionaries (const char *pattern, bool verbose)
 
bool listTSTemplates (const char *pattern, bool verbose)
 
bool listTSConfigs (const char *pattern, bool verbose)
 
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 listExtensions (const char *pattern)
 
bool listExtensionContents (const char *pattern)
 
bool listPublications (const char *pattern)
 
static bool addFooterToPublicationDesc (PQExpBuffer buf, const char *footermsg, bool as_schema, printTableContent *const cont)
 
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

◆ add_role_attribute()

static void add_role_attribute ( PQExpBuffer  buf,
const char *const  str 
)
static

Definition at line 3757 of file describe.c.

3758 {
3759  if (buf->len > 0)
3760  appendPQExpBufferStr(buf, ", ");
3761 
3763 }
static char * buf
Definition: pg_test_fsync.c:67
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367

References appendPQExpBufferStr(), buf, and generate_unaccent_rules::str.

Referenced by describeRoles().

◆ add_tablespace_footer()

static void add_tablespace_footer ( printTableContent *const  cont,
char  relkind,
Oid  tablespace,
const bool  newline 
)
static

Definition at line 3549 of file describe.c.

3551 {
3552  /* relkinds for which we support tablespaces */
3553  if (relkind == RELKIND_RELATION ||
3554  relkind == RELKIND_MATVIEW ||
3555  relkind == RELKIND_INDEX ||
3556  relkind == RELKIND_PARTITIONED_TABLE ||
3557  relkind == RELKIND_PARTITIONED_INDEX ||
3558  relkind == RELKIND_TOASTVALUE)
3559  {
3560  /*
3561  * We ignore the database default tablespace so that users not using
3562  * tablespaces don't need to know about them.
3563  */
3564  if (tablespace != 0)
3565  {
3566  PGresult *result = NULL;
3568 
3569  initPQExpBuffer(&buf);
3571  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3572  "WHERE oid = '%u';", tablespace);
3573  result = PSQLexec(buf.data);
3574  if (!result)
3575  {
3576  termPQExpBuffer(&buf);
3577  return;
3578  }
3579  /* Should always be the case, but.... */
3580  if (PQntuples(result) > 0)
3581  {
3582  if (newline)
3583  {
3584  /* Add the tablespace as a new footer */
3585  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3586  PQgetvalue(result, 0, 0));
3587  printTableAddFooter(cont, buf.data);
3588  }
3589  else
3590  {
3591  /* Append the tablespace to the latest footer */
3592  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3593 
3594  /*-------
3595  translator: before this string there's an index description like
3596  '"foo_pkey" PRIMARY KEY, btree (a)' */
3597  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3598  PQgetvalue(result, 0, 0));
3599  printTableSetFooter(cont, buf.data);
3600  }
3601  }
3602  PQclear(result);
3603  termPQExpBuffer(&buf);
3604  }
3605  }
3606 }
PGresult * PSQLexec(const char *query)
Definition: common.c:560
#define _(x)
Definition: elog.c:91
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3321
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3296
#define newline
Definition: indent_codes.h:35
char * tablespace
Definition: pgbench.c:226
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 termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
printTableFooter * footer
Definition: print.h:172
char * data
Definition: print.h:150

References _, appendPQExpBuffer(), buf, printTableFooter::data, printTableContent::footer, initPQExpBuffer(), newline, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), printTableAddFooter(), printTableSetFooter(), PSQLexec(), tablespace, and termPQExpBuffer().

Referenced by describeOneTableDetails().

◆ addFooterToPublicationDesc()

static bool addFooterToPublicationDesc ( PQExpBuffer  buf,
const char *  footermsg,
bool  as_schema,
printTableContent *const  cont 
)
static

Definition at line 6258 of file describe.c.

6260 {
6261  PGresult *res;
6262  int count = 0;
6263  int i = 0;
6264 
6265  res = PSQLexec(buf->data);
6266  if (!res)
6267  return false;
6268  else
6269  count = PQntuples(res);
6270 
6271  if (count > 0)
6272  printTableAddFooter(cont, footermsg);
6273 
6274  for (i = 0; i < count; i++)
6275  {
6276  if (as_schema)
6277  printfPQExpBuffer(buf, " \"%s\"", PQgetvalue(res, i, 0));
6278  else
6279  {
6280  printfPQExpBuffer(buf, " \"%s.%s\"", PQgetvalue(res, i, 0),
6281  PQgetvalue(res, i, 1));
6282 
6283  if (!PQgetisnull(res, i, 3))
6284  appendPQExpBuffer(buf, " (%s)", PQgetvalue(res, i, 3));
6285 
6286  if (!PQgetisnull(res, i, 2))
6287  appendPQExpBuffer(buf, " WHERE %s", PQgetvalue(res, i, 2));
6288  }
6289 
6290  printTableAddFooter(cont, buf->data);
6291  }
6292 
6293  PQclear(res);
6294  return true;
6295 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3730
int i
Definition: isn.c:73

References appendPQExpBuffer(), buf, i, PQclear(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), printTableAddFooter(), PSQLexec(), and res.

Referenced by describePublications().

◆ describeAccessMethods()

bool describeAccessMethods ( const char *  pattern,
bool  verbose 
)

Definition at line 142 of file describe.c.

143 {
145  PGresult *res;
146  printQueryOpt myopt = pset.popt;
147  static const bool translate_columns[] = {false, true, false, false};
148 
149  if (pset.sversion < 90600)
150  {
151  char sverbuf[32];
152 
153  pg_log_error("The server (version %s) does not support access methods.",
155  sverbuf, sizeof(sverbuf)));
156  return true;
157  }
158 
160 
162  "SELECT amname AS \"%s\",\n"
163  " CASE amtype"
164  " WHEN 'i' THEN '%s'"
165  " WHEN 't' THEN '%s'"
166  " END AS \"%s\"",
167  gettext_noop("Name"),
168  gettext_noop("Index"),
169  gettext_noop("Table"),
170  gettext_noop("Type"));
171 
172  if (verbose)
173  {
175  ",\n amhandler AS \"%s\",\n"
176  " pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
177  gettext_noop("Handler"),
178  gettext_noop("Description"));
179  }
180 
182  "\nFROM pg_catalog.pg_am\n");
183 
184  if (!validateSQLNamePattern(&buf, pattern, false, false,
185  NULL, "amname", NULL,
186  NULL,
187  NULL, 1))
188  {
190  return false;
191  }
192 
193  appendPQExpBufferStr(&buf, "ORDER BY 1;");
194 
195  res = PSQLexec(buf.data);
197  if (!res)
198  return false;
199 
200  myopt.nullPrint = NULL;
201  myopt.title = _("List of access methods");
202  myopt.translate_header = true;
203  myopt.translate_columns = translate_columns;
204  myopt.n_translate_columns = lengthof(translate_columns);
205 
206  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
207 
208  PQclear(res);
209  return true;
210 }
#define gettext_noop(x)
Definition: c.h:1186
#define lengthof(array)
Definition: c.h:772
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:6128
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3534
int verbose
#define pg_log_error(...)
Definition: logging.h:106
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
printQueryOpt popt
Definition: settings.h:91
FILE * logfile
Definition: settings.h:119
FILE * queryFout
Definition: settings.h:84
const bool * translate_columns
Definition: print.h:185
char * nullPrint
Definition: print.h:181
char * title
Definition: print.h:182
bool translate_header
Definition: print.h:184
int n_translate_columns
Definition: print.h:187

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, 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.nullPrint = NULL;
128  myopt.title = _("List of aggregate functions");
129  myopt.translate_header = true;
130 
131  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
132 
133  PQclear(res);
134  return true;
135 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, _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 4495 of file describe.c.

4497 {
4499  PGresult *res;
4500  printQueryOpt myopt = pset.popt;
4501 
4502  initPQExpBuffer(&buf);
4504  "SELECT s.name AS \"%s\", "
4505  "pg_catalog.current_setting(s.name) AS \"%s\"",
4506  gettext_noop("Parameter"),
4507  gettext_noop("Value"));
4508 
4509  if (verbose)
4510  {
4512  ", s.vartype AS \"%s\", s.context AS \"%s\", ",
4513  gettext_noop("Type"),
4514  gettext_noop("Context"));
4515  if (pset.sversion >= 150000)
4516  printACLColumn(&buf, "p.paracl");
4517  else
4518  appendPQExpBuffer(&buf, "NULL AS \"%s\"",
4519  gettext_noop("Access privileges"));
4520  }
4521 
4522  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_settings s\n");
4523 
4524  if (verbose && pset.sversion >= 150000)
4526  " LEFT JOIN pg_catalog.pg_parameter_acl p\n"
4527  " ON pg_catalog.lower(s.name) = p.parname\n");
4528 
4529  if (pattern)
4530  processSQLNamePattern(pset.db, &buf, pattern,
4531  false, false,
4532  NULL, "pg_catalog.lower(s.name)", NULL,
4533  NULL, NULL, NULL);
4534  else
4535  appendPQExpBufferStr(&buf, "WHERE s.source <> 'default' AND\n"
4536  " s.setting IS DISTINCT FROM s.boot_val\n");
4537 
4538  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4539 
4540  res = PSQLexec(buf.data);
4541  termPQExpBuffer(&buf);
4542  if (!res)
4543  return false;
4544 
4545  myopt.nullPrint = NULL;
4546  if (pattern)
4547  myopt.title = _("List of configuration parameters");
4548  else
4549  myopt.title = _("List of non-default configuration parameters");
4550  myopt.translate_header = true;
4551 
4552  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4553 
4554  PQclear(res);
4555  return true;
4556 }
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6612
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
Definition: string_utils.c:891
PGconn * db
Definition: settings.h:82

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, _psqlSettings::db, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, _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 291 of file describe.c.

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

◆ describeOneTableDetails()

static bool describeOneTableDetails ( const char *  schemaname,
const char *  relationname,
const char *  oid,
bool  verbose 
)
static

Definition at line 1529 of file describe.c.

1533 {
1534  bool retval = false;
1536  PGresult *res = NULL;
1537  printTableOpt myopt = pset.popt.topt;
1538  printTableContent cont;
1539  bool printTableInitialized = false;
1540  int i;
1541  char *view_def = NULL;
1542  char *headers[12];
1543  PQExpBufferData title;
1545  int cols;
1546  int attname_col = -1, /* column indexes in "res" */
1547  atttype_col = -1,
1548  attrdef_col = -1,
1549  attnotnull_col = -1,
1550  attcoll_col = -1,
1551  attidentity_col = -1,
1552  attgenerated_col = -1,
1553  isindexkey_col = -1,
1554  indexdef_col = -1,
1555  fdwopts_col = -1,
1556  attstorage_col = -1,
1557  attcompression_col = -1,
1558  attstattarget_col = -1,
1559  attdescr_col = -1;
1560  int numrows;
1561  struct
1562  {
1563  int16 checks;
1564  char relkind;
1565  bool hasindex;
1566  bool hasrules;
1567  bool hastriggers;
1568  bool rowsecurity;
1569  bool forcerowsecurity;
1570  bool hasoids;
1571  bool ispartition;
1572  Oid tablespace;
1573  char *reloptions;
1574  char *reloftype;
1575  char relpersistence;
1576  char relreplident;
1577  char *relam;
1578  } tableinfo;
1579  bool show_column_details = false;
1580 
1581  myopt.default_footer = false;
1582  /* This output looks confusing in expanded mode. */
1583  myopt.expanded = false;
1584 
1585  initPQExpBuffer(&buf);
1586  initPQExpBuffer(&title);
1588 
1589  /* Get general table info */
1590  if (pset.sversion >= 120000)
1591  {
1593  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1594  "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1595  "false AS relhasoids, c.relispartition, %s, c.reltablespace, "
1596  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1597  "c.relpersistence, c.relreplident, am.amname\n"
1598  "FROM pg_catalog.pg_class c\n "
1599  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1600  "LEFT JOIN pg_catalog.pg_am am ON (c.relam = am.oid)\n"
1601  "WHERE c.oid = '%s';",
1602  (verbose ?
1603  "pg_catalog.array_to_string(c.reloptions || "
1604  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1605  : "''"),
1606  oid);
1607  }
1608  else if (pset.sversion >= 100000)
1609  {
1611  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1612  "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1613  "c.relhasoids, c.relispartition, %s, c.reltablespace, "
1614  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1615  "c.relpersistence, c.relreplident\n"
1616  "FROM pg_catalog.pg_class c\n "
1617  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1618  "WHERE c.oid = '%s';",
1619  (verbose ?
1620  "pg_catalog.array_to_string(c.reloptions || "
1621  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1622  : "''"),
1623  oid);
1624  }
1625  else if (pset.sversion >= 90500)
1626  {
1628  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1629  "c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity, "
1630  "c.relhasoids, false as relispartition, %s, c.reltablespace, "
1631  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1632  "c.relpersistence, c.relreplident\n"
1633  "FROM pg_catalog.pg_class c\n "
1634  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1635  "WHERE c.oid = '%s';",
1636  (verbose ?
1637  "pg_catalog.array_to_string(c.reloptions || "
1638  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1639  : "''"),
1640  oid);
1641  }
1642  else if (pset.sversion >= 90400)
1643  {
1645  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1646  "c.relhastriggers, false, false, c.relhasoids, "
1647  "false as relispartition, %s, c.reltablespace, "
1648  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1649  "c.relpersistence, c.relreplident\n"
1650  "FROM pg_catalog.pg_class c\n "
1651  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1652  "WHERE c.oid = '%s';",
1653  (verbose ?
1654  "pg_catalog.array_to_string(c.reloptions || "
1655  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1656  : "''"),
1657  oid);
1658  }
1659  else
1660  {
1662  "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1663  "c.relhastriggers, false, false, c.relhasoids, "
1664  "false as relispartition, %s, c.reltablespace, "
1665  "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, "
1666  "c.relpersistence\n"
1667  "FROM pg_catalog.pg_class c\n "
1668  "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1669  "WHERE c.oid = '%s';",
1670  (verbose ?
1671  "pg_catalog.array_to_string(c.reloptions || "
1672  "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n"
1673  : "''"),
1674  oid);
1675  }
1676 
1677  res = PSQLexec(buf.data);
1678  if (!res)
1679  goto error_return;
1680 
1681  /* Did we get anything? */
1682  if (PQntuples(res) == 0)
1683  {
1684  if (!pset.quiet)
1685  pg_log_error("Did not find any relation with OID %s.", oid);
1686  goto error_return;
1687  }
1688 
1689  tableinfo.checks = atoi(PQgetvalue(res, 0, 0));
1690  tableinfo.relkind = *(PQgetvalue(res, 0, 1));
1691  tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 2), "t") == 0;
1692  tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 3), "t") == 0;
1693  tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
1694  tableinfo.rowsecurity = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
1695  tableinfo.forcerowsecurity = strcmp(PQgetvalue(res, 0, 6), "t") == 0;
1696  tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 7), "t") == 0;
1697  tableinfo.ispartition = strcmp(PQgetvalue(res, 0, 8), "t") == 0;
1698  tableinfo.reloptions = pg_strdup(PQgetvalue(res, 0, 9));
1699  tableinfo.tablespace = atooid(PQgetvalue(res, 0, 10));
1700  tableinfo.reloftype = (strcmp(PQgetvalue(res, 0, 11), "") != 0) ?
1701  pg_strdup(PQgetvalue(res, 0, 11)) : NULL;
1702  tableinfo.relpersistence = *(PQgetvalue(res, 0, 12));
1703  tableinfo.relreplident = (pset.sversion >= 90400) ?
1704  *(PQgetvalue(res, 0, 13)) : 'd';
1705  if (pset.sversion >= 120000)
1706  tableinfo.relam = PQgetisnull(res, 0, 14) ?
1707  (char *) NULL : pg_strdup(PQgetvalue(res, 0, 14));
1708  else
1709  tableinfo.relam = NULL;
1710  PQclear(res);
1711  res = NULL;
1712 
1713  /*
1714  * If it's a sequence, deal with it here separately.
1715  */
1716  if (tableinfo.relkind == RELKIND_SEQUENCE)
1717  {
1718  PGresult *result = NULL;
1719  printQueryOpt myopt = pset.popt;
1720  char *footers[2] = {NULL, NULL};
1721 
1722  if (pset.sversion >= 100000)
1723  {
1725  "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n"
1726  " seqstart AS \"%s\",\n"
1727  " seqmin AS \"%s\",\n"
1728  " seqmax AS \"%s\",\n"
1729  " seqincrement AS \"%s\",\n"
1730  " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n"
1731  " seqcache AS \"%s\"\n",
1732  gettext_noop("Type"),
1733  gettext_noop("Start"),
1734  gettext_noop("Minimum"),
1735  gettext_noop("Maximum"),
1736  gettext_noop("Increment"),
1737  gettext_noop("yes"),
1738  gettext_noop("no"),
1739  gettext_noop("Cycles?"),
1740  gettext_noop("Cache"));
1742  "FROM pg_catalog.pg_sequence\n"
1743  "WHERE seqrelid = '%s';",
1744  oid);
1745  }
1746  else
1747  {
1749  "SELECT 'bigint' AS \"%s\",\n"
1750  " start_value AS \"%s\",\n"
1751  " min_value AS \"%s\",\n"
1752  " max_value AS \"%s\",\n"
1753  " increment_by AS \"%s\",\n"
1754  " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n"
1755  " cache_value AS \"%s\"\n",
1756  gettext_noop("Type"),
1757  gettext_noop("Start"),
1758  gettext_noop("Minimum"),
1759  gettext_noop("Maximum"),
1760  gettext_noop("Increment"),
1761  gettext_noop("yes"),
1762  gettext_noop("no"),
1763  gettext_noop("Cycles?"),
1764  gettext_noop("Cache"));
1765  appendPQExpBuffer(&buf, "FROM %s", fmtId(schemaname));
1766  /* must be separate because fmtId isn't reentrant */
1767  appendPQExpBuffer(&buf, ".%s;", fmtId(relationname));
1768  }
1769 
1770  res = PSQLexec(buf.data);
1771  if (!res)
1772  goto error_return;
1773 
1774  /* Get the column that owns this sequence */
1775  printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1776  "\n pg_catalog.quote_ident(relname) || '.' ||"
1777  "\n pg_catalog.quote_ident(attname),"
1778  "\n d.deptype"
1779  "\nFROM pg_catalog.pg_class c"
1780  "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1781  "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1782  "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1783  "\n a.attrelid=c.oid AND"
1784  "\n a.attnum=d.refobjsubid)"
1785  "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1786  "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1787  "\n AND d.objid='%s'"
1788  "\n AND d.deptype IN ('a', 'i')",
1789  oid);
1790 
1791  result = PSQLexec(buf.data);
1792 
1793  /*
1794  * If we get no rows back, don't show anything (obviously). We should
1795  * never get more than one row back, but if we do, just ignore it and
1796  * don't print anything.
1797  */
1798  if (!result)
1799  goto error_return;
1800  else if (PQntuples(result) == 1)
1801  {
1802  switch (PQgetvalue(result, 0, 1)[0])
1803  {
1804  case 'a':
1805  footers[0] = psprintf(_("Owned by: %s"),
1806  PQgetvalue(result, 0, 0));
1807  break;
1808  case 'i':
1809  footers[0] = psprintf(_("Sequence for identity column: %s"),
1810  PQgetvalue(result, 0, 0));
1811  break;
1812  }
1813  }
1814  PQclear(result);
1815 
1816  if (tableinfo.relpersistence == 'u')
1817  printfPQExpBuffer(&title, _("Unlogged sequence \"%s.%s\""),
1818  schemaname, relationname);
1819  else
1820  printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
1821  schemaname, relationname);
1822 
1823  myopt.footers = footers;
1824  myopt.topt.default_footer = false;
1825  myopt.title = title.data;
1826  myopt.translate_header = true;
1827 
1828  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1829 
1830  free(footers[0]);
1831 
1832  retval = true;
1833  goto error_return; /* not an error, just return early */
1834  }
1835 
1836  /* Identify whether we should print collation, nullable, default vals */
1837  if (tableinfo.relkind == RELKIND_RELATION ||
1838  tableinfo.relkind == RELKIND_VIEW ||
1839  tableinfo.relkind == RELKIND_MATVIEW ||
1840  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1841  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1842  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1843  show_column_details = true;
1844 
1845  /*
1846  * Get per-column info
1847  *
1848  * Since the set of query columns we need varies depending on relkind and
1849  * server version, we compute all the column numbers on-the-fly. Column
1850  * number variables for columns not fetched are left as -1; this avoids
1851  * duplicative test logic below.
1852  */
1853  cols = 0;
1854  printfPQExpBuffer(&buf, "SELECT a.attname");
1855  attname_col = cols++;
1856  appendPQExpBufferStr(&buf, ",\n pg_catalog.format_type(a.atttypid, a.atttypmod)");
1857  atttype_col = cols++;
1858 
1859  if (show_column_details)
1860  {
1861  /* use "pretty" mode for expression to avoid excessive parentheses */
1863  ",\n (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid, true)"
1864  "\n FROM pg_catalog.pg_attrdef d"
1865  "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)"
1866  ",\n a.attnotnull");
1867  attrdef_col = cols++;
1868  attnotnull_col = cols++;
1869  appendPQExpBufferStr(&buf, ",\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1870  " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
1871  attcoll_col = cols++;
1872  if (pset.sversion >= 100000)
1873  appendPQExpBufferStr(&buf, ",\n a.attidentity");
1874  else
1875  appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attidentity");
1876  attidentity_col = cols++;
1877  if (pset.sversion >= 120000)
1878  appendPQExpBufferStr(&buf, ",\n a.attgenerated");
1879  else
1880  appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attgenerated");
1881  attgenerated_col = cols++;
1882  }
1883  if (tableinfo.relkind == RELKIND_INDEX ||
1884  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1885  {
1886  if (pset.sversion >= 110000)
1887  {
1888  appendPQExpBuffer(&buf, ",\n CASE WHEN a.attnum <= (SELECT i.indnkeyatts FROM pg_catalog.pg_index i WHERE i.indexrelid = '%s') THEN '%s' ELSE '%s' END AS is_key",
1889  oid,
1890  gettext_noop("yes"),
1891  gettext_noop("no"));
1892  isindexkey_col = cols++;
1893  }
1894  appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1895  indexdef_col = cols++;
1896  }
1897  /* FDW options for foreign table column */
1898  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
1899  {
1900  appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1901  " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1902  " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1903  fdwopts_col = cols++;
1904  }
1905  if (verbose)
1906  {
1907  appendPQExpBufferStr(&buf, ",\n a.attstorage");
1908  attstorage_col = cols++;
1909 
1910  /* compression info, if relevant to relkind */
1911  if (pset.sversion >= 140000 &&
1913  (tableinfo.relkind == RELKIND_RELATION ||
1914  tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
1915  tableinfo.relkind == RELKIND_MATVIEW))
1916  {
1917  appendPQExpBufferStr(&buf, ",\n a.attcompression AS attcompression");
1918  attcompression_col = cols++;
1919  }
1920 
1921  /* stats target, if relevant to relkind */
1922  if (tableinfo.relkind == RELKIND_RELATION ||
1923  tableinfo.relkind == RELKIND_INDEX ||
1924  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1925  tableinfo.relkind == RELKIND_MATVIEW ||
1926  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1927  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1928  {
1929  appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1930  attstattarget_col = cols++;
1931  }
1932 
1933  /*
1934  * In 9.0+, we have column comments for: relations, views, composite
1935  * types, and foreign tables (cf. CommentObject() in comment.c).
1936  */
1937  if (tableinfo.relkind == RELKIND_RELATION ||
1938  tableinfo.relkind == RELKIND_VIEW ||
1939  tableinfo.relkind == RELKIND_MATVIEW ||
1940  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1941  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1942  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1943  {
1944  appendPQExpBufferStr(&buf, ",\n pg_catalog.col_description(a.attrelid, a.attnum)");
1945  attdescr_col = cols++;
1946  }
1947  }
1948 
1949  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1950  appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1951  appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1952 
1953  res = PSQLexec(buf.data);
1954  if (!res)
1955  goto error_return;
1956  numrows = PQntuples(res);
1957 
1958  /* Make title */
1959  switch (tableinfo.relkind)
1960  {
1961  case RELKIND_RELATION:
1962  if (tableinfo.relpersistence == 'u')
1963  printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1964  schemaname, relationname);
1965  else
1966  printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1967  schemaname, relationname);
1968  break;
1969  case RELKIND_VIEW:
1970  printfPQExpBuffer(&title, _("View \"%s.%s\""),
1971  schemaname, relationname);
1972  break;
1973  case RELKIND_MATVIEW:
1974  if (tableinfo.relpersistence == 'u')
1975  printfPQExpBuffer(&title, _("Unlogged materialized view \"%s.%s\""),
1976  schemaname, relationname);
1977  else
1978  printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
1979  schemaname, relationname);
1980  break;
1981  case RELKIND_INDEX:
1982  if (tableinfo.relpersistence == 'u')
1983  printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1984  schemaname, relationname);
1985  else
1986  printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1987  schemaname, relationname);
1988  break;
1989  case RELKIND_PARTITIONED_INDEX:
1990  if (tableinfo.relpersistence == 'u')
1991  printfPQExpBuffer(&title, _("Unlogged partitioned index \"%s.%s\""),
1992  schemaname, relationname);
1993  else
1994  printfPQExpBuffer(&title, _("Partitioned index \"%s.%s\""),
1995  schemaname, relationname);
1996  break;
1997  case RELKIND_TOASTVALUE:
1998  printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1999  schemaname, relationname);
2000  break;
2001  case RELKIND_COMPOSITE_TYPE:
2002  printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
2003  schemaname, relationname);
2004  break;
2005  case RELKIND_FOREIGN_TABLE:
2006  printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
2007  schemaname, relationname);
2008  break;
2009  case RELKIND_PARTITIONED_TABLE:
2010  if (tableinfo.relpersistence == 'u')
2011  printfPQExpBuffer(&title, _("Unlogged partitioned table \"%s.%s\""),
2012  schemaname, relationname);
2013  else
2014  printfPQExpBuffer(&title, _("Partitioned table \"%s.%s\""),
2015  schemaname, relationname);
2016  break;
2017  default:
2018  /* untranslated unknown relkind */
2019  printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
2020  tableinfo.relkind, schemaname, relationname);
2021  break;
2022  }
2023 
2024  /* Fill headers[] with the names of the columns we will output */
2025  cols = 0;
2026  headers[cols++] = gettext_noop("Column");
2027  headers[cols++] = gettext_noop("Type");
2028  if (show_column_details)
2029  {
2030  headers[cols++] = gettext_noop("Collation");
2031  headers[cols++] = gettext_noop("Nullable");
2032  headers[cols++] = gettext_noop("Default");
2033  }
2034  if (isindexkey_col >= 0)
2035  headers[cols++] = gettext_noop("Key?");
2036  if (indexdef_col >= 0)
2037  headers[cols++] = gettext_noop("Definition");
2038  if (fdwopts_col >= 0)
2039  headers[cols++] = gettext_noop("FDW options");
2040  if (attstorage_col >= 0)
2041  headers[cols++] = gettext_noop("Storage");
2042  if (attcompression_col >= 0)
2043  headers[cols++] = gettext_noop("Compression");
2044  if (attstattarget_col >= 0)
2045  headers[cols++] = gettext_noop("Stats target");
2046  if (attdescr_col >= 0)
2047  headers[cols++] = gettext_noop("Description");
2048 
2049  Assert(cols <= lengthof(headers));
2050 
2051  printTableInit(&cont, &myopt, title.data, cols, numrows);
2052  printTableInitialized = true;
2053 
2054  for (i = 0; i < cols; i++)
2055  printTableAddHeader(&cont, headers[i], true, 'l');
2056 
2057  /* Generate table cells to be printed */
2058  for (i = 0; i < numrows; i++)
2059  {
2060  /* Column */
2061  printTableAddCell(&cont, PQgetvalue(res, i, attname_col), false, false);
2062 
2063  /* Type */
2064  printTableAddCell(&cont, PQgetvalue(res, i, atttype_col), false, false);
2065 
2066  /* Collation, Nullable, Default */
2067  if (show_column_details)
2068  {
2069  char *identity;
2070  char *generated;
2071  char *default_str;
2072  bool mustfree = false;
2073 
2074  printTableAddCell(&cont, PQgetvalue(res, i, attcoll_col), false, false);
2075 
2076  printTableAddCell(&cont,
2077  strcmp(PQgetvalue(res, i, attnotnull_col), "t") == 0 ? "not null" : "",
2078  false, false);
2079 
2080  identity = PQgetvalue(res, i, attidentity_col);
2081  generated = PQgetvalue(res, i, attgenerated_col);
2082 
2083  if (identity[0] == ATTRIBUTE_IDENTITY_ALWAYS)
2084  default_str = "generated always as identity";
2085  else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
2086  default_str = "generated by default as identity";
2087  else if (generated[0] == ATTRIBUTE_GENERATED_STORED)
2088  {
2089  default_str = psprintf("generated always as (%s) stored",
2090  PQgetvalue(res, i, attrdef_col));
2091  mustfree = true;
2092  }
2093  else
2094  default_str = PQgetvalue(res, i, attrdef_col);
2095 
2096  printTableAddCell(&cont, default_str, false, mustfree);
2097  }
2098 
2099  /* Info for index columns */
2100  if (isindexkey_col >= 0)
2101  printTableAddCell(&cont, PQgetvalue(res, i, isindexkey_col), true, false);
2102  if (indexdef_col >= 0)
2103  printTableAddCell(&cont, PQgetvalue(res, i, indexdef_col), false, false);
2104 
2105  /* FDW options for foreign table columns */
2106  if (fdwopts_col >= 0)
2107  printTableAddCell(&cont, PQgetvalue(res, i, fdwopts_col), false, false);
2108 
2109  /* Storage mode, if relevant */
2110  if (attstorage_col >= 0)
2111  {
2112  char *storage = PQgetvalue(res, i, attstorage_col);
2113 
2114  /* these strings are literal in our syntax, so not translated. */
2115  printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
2116  (storage[0] == 'm' ? "main" :
2117  (storage[0] == 'x' ? "extended" :
2118  (storage[0] == 'e' ? "external" :
2119  "???")))),
2120  false, false);
2121  }
2122 
2123  /* Column compression, if relevant */
2124  if (attcompression_col >= 0)
2125  {
2126  char *compression = PQgetvalue(res, i, attcompression_col);
2127 
2128  /* these strings are literal in our syntax, so not translated. */
2129  printTableAddCell(&cont, (compression[0] == 'p' ? "pglz" :
2130  (compression[0] == 'l' ? "lz4" :
2131  (compression[0] == '\0' ? "" :
2132  "???"))),
2133  false, false);
2134  }
2135 
2136  /* Statistics target, if the relkind supports this feature */
2137  if (attstattarget_col >= 0)
2138  printTableAddCell(&cont, PQgetvalue(res, i, attstattarget_col),
2139  false, false);
2140 
2141  /* Column comments, if the relkind supports this feature */
2142  if (attdescr_col >= 0)
2143  printTableAddCell(&cont, PQgetvalue(res, i, attdescr_col),
2144  false, false);
2145  }
2146 
2147  /* Make footers */
2148 
2149  if (tableinfo.ispartition)
2150  {
2151  /* Footer information for a partition child table */
2152  PGresult *result;
2153 
2155  "SELECT inhparent::pg_catalog.regclass,\n"
2156  " pg_catalog.pg_get_expr(c.relpartbound, c.oid),\n ");
2157 
2159  pset.sversion >= 140000 ? "inhdetachpending" :
2160  "false as inhdetachpending");
2161 
2162  /* If verbose, also request the partition constraint definition */
2163  if (verbose)
2165  ",\n pg_catalog.pg_get_partition_constraintdef(c.oid)");
2167  "\nFROM pg_catalog.pg_class c"
2168  " JOIN pg_catalog.pg_inherits i"
2169  " ON c.oid = inhrelid"
2170  "\nWHERE c.oid = '%s';", oid);
2171  result = PSQLexec(buf.data);
2172  if (!result)
2173  goto error_return;
2174 
2175  if (PQntuples(result) > 0)
2176  {
2177  char *parent_name = PQgetvalue(result, 0, 0);
2178  char *partdef = PQgetvalue(result, 0, 1);
2179  char *detached = PQgetvalue(result, 0, 2);
2180 
2181  printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s%s"), parent_name,
2182  partdef,
2183  strcmp(detached, "t") == 0 ? " DETACH PENDING" : "");
2185 
2186  if (verbose)
2187  {
2188  char *partconstraintdef = NULL;
2189 
2190  if (!PQgetisnull(result, 0, 3))
2191  partconstraintdef = PQgetvalue(result, 0, 3);
2192  /* If there isn't any constraint, show that explicitly */
2193  if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
2194  printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
2195  else
2196  printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"),
2197  partconstraintdef);
2199  }
2200  }
2201  PQclear(result);
2202  }
2203 
2204  if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2205  {
2206  /* Footer information for a partitioned table (partitioning parent) */
2207  PGresult *result;
2208 
2210  "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
2211  oid);
2212  result = PSQLexec(buf.data);
2213  if (!result)
2214  goto error_return;
2215 
2216  if (PQntuples(result) == 1)
2217  {
2218  char *partkeydef = PQgetvalue(result, 0, 0);
2219 
2220  printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2222  }
2223  PQclear(result);
2224  }
2225 
2226  if (tableinfo.relkind == RELKIND_TOASTVALUE)
2227  {
2228  /* For a TOAST table, print name of owning table */
2229  PGresult *result;
2230 
2232  "SELECT n.nspname, c.relname\n"
2233  "FROM pg_catalog.pg_class c"
2234  " JOIN pg_catalog.pg_namespace n"
2235  " ON n.oid = c.relnamespace\n"
2236  "WHERE reltoastrelid = '%s';", oid);
2237  result = PSQLexec(buf.data);
2238  if (!result)
2239  goto error_return;
2240 
2241  if (PQntuples(result) == 1)
2242  {
2243  char *schemaname = PQgetvalue(result, 0, 0);
2244  char *relname = PQgetvalue(result, 0, 1);
2245 
2246  printfPQExpBuffer(&tmpbuf, _("Owning table: \"%s.%s\""),
2247  schemaname, relname);
2249  }
2250  PQclear(result);
2251  }
2252 
2253  if (tableinfo.relkind == RELKIND_INDEX ||
2254  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
2255  {
2256  /* Footer information about an index */
2257  PGresult *result;
2258 
2260  "SELECT i.indisunique, i.indisprimary, i.indisclustered, "
2261  "i.indisvalid,\n"
2262  " (NOT i.indimmediate) AND "
2263  "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2264  "WHERE conrelid = i.indrelid AND "
2265  "conindid = i.indexrelid AND "
2266  "contype IN ('p','u','x') AND "
2267  "condeferrable) AS condeferrable,\n"
2268  " (NOT i.indimmediate) AND "
2269  "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2270  "WHERE conrelid = i.indrelid AND "
2271  "conindid = i.indexrelid AND "
2272  "contype IN ('p','u','x') AND "
2273  "condeferred) AS condeferred,\n");
2274 
2275  if (pset.sversion >= 90400)
2276  appendPQExpBufferStr(&buf, "i.indisreplident,\n");
2277  else
2278  appendPQExpBufferStr(&buf, "false AS indisreplident,\n");
2279 
2280  if (pset.sversion >= 150000)
2281  appendPQExpBufferStr(&buf, "i.indnullsnotdistinct,\n");
2282  else
2283  appendPQExpBufferStr(&buf, "false AS indnullsnotdistinct,\n");
2284 
2285  appendPQExpBuffer(&buf, " a.amname, c2.relname, "
2286  "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
2287  "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
2288  "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
2289  "AND i.indrelid = c2.oid;",
2290  oid);
2291 
2292  result = PSQLexec(buf.data);
2293  if (!result)
2294  goto error_return;
2295  else if (PQntuples(result) != 1)
2296  {
2297  PQclear(result);
2298  goto error_return;
2299  }
2300  else
2301  {
2302  char *indisunique = PQgetvalue(result, 0, 0);
2303  char *indisprimary = PQgetvalue(result, 0, 1);
2304  char *indisclustered = PQgetvalue(result, 0, 2);
2305  char *indisvalid = PQgetvalue(result, 0, 3);
2306  char *deferrable = PQgetvalue(result, 0, 4);
2307  char *deferred = PQgetvalue(result, 0, 5);
2308  char *indisreplident = PQgetvalue(result, 0, 6);
2309  char *indnullsnotdistinct = PQgetvalue(result, 0, 7);
2310  char *indamname = PQgetvalue(result, 0, 8);
2311  char *indtable = PQgetvalue(result, 0, 9);
2312  char *indpred = PQgetvalue(result, 0, 10);
2313 
2314  if (strcmp(indisprimary, "t") == 0)
2315  printfPQExpBuffer(&tmpbuf, _("primary key, "));
2316  else if (strcmp(indisunique, "t") == 0)
2317  {
2318  printfPQExpBuffer(&tmpbuf, _("unique"));
2319  if (strcmp(indnullsnotdistinct, "t") == 0)
2320  appendPQExpBufferStr(&tmpbuf, _(" nulls not distinct"));
2321  appendPQExpBufferStr(&tmpbuf, _(", "));
2322  }
2323  else
2325  appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
2326 
2327  /* we assume here that index and table are in same schema */
2328  appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
2329  schemaname, indtable);
2330 
2331  if (strlen(indpred))
2332  appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
2333 
2334  if (strcmp(indisclustered, "t") == 0)
2335  appendPQExpBufferStr(&tmpbuf, _(", clustered"));
2336 
2337  if (strcmp(indisvalid, "t") != 0)
2338  appendPQExpBufferStr(&tmpbuf, _(", invalid"));
2339 
2340  if (strcmp(deferrable, "t") == 0)
2341  appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
2342 
2343  if (strcmp(deferred, "t") == 0)
2344  appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
2345 
2346  if (strcmp(indisreplident, "t") == 0)
2347  appendPQExpBufferStr(&tmpbuf, _(", replica identity"));
2348 
2350 
2351  /*
2352  * If it's a partitioned index, we'll print the tablespace below
2353  */
2354  if (tableinfo.relkind == RELKIND_INDEX)
2355  add_tablespace_footer(&cont, tableinfo.relkind,
2356  tableinfo.tablespace, true);
2357  }
2358 
2359  PQclear(result);
2360  }
2361  /* If you add relkinds here, see also "Finish printing..." stanza below */
2362  else if (tableinfo.relkind == RELKIND_RELATION ||
2363  tableinfo.relkind == RELKIND_MATVIEW ||
2364  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2365  tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
2366  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
2367  tableinfo.relkind == RELKIND_TOASTVALUE)
2368  {
2369  /* Footer information about a table */
2370  PGresult *result = NULL;
2371  int tuples = 0;
2372 
2373  /* print indexes */
2374  if (tableinfo.hasindex)
2375  {
2377  "SELECT c2.relname, i.indisprimary, i.indisunique, "
2378  "i.indisclustered, i.indisvalid, "
2379  "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "
2380  "pg_catalog.pg_get_constraintdef(con.oid, true), "
2381  "contype, condeferrable, condeferred");
2382  if (pset.sversion >= 90400)
2383  appendPQExpBufferStr(&buf, ", i.indisreplident");
2384  else
2385  appendPQExpBufferStr(&buf, ", false AS indisreplident");
2386  appendPQExpBufferStr(&buf, ", c2.reltablespace");
2388  "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
2389  " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n"
2390  "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2391  "ORDER BY i.indisprimary DESC, c2.relname;",
2392  oid);
2393  result = PSQLexec(buf.data);
2394  if (!result)
2395  goto error_return;
2396  else
2397  tuples = PQntuples(result);
2398 
2399  if (tuples > 0)
2400  {
2401  printTableAddFooter(&cont, _("Indexes:"));
2402  for (i = 0; i < tuples; i++)
2403  {
2404  /* untranslated index name */
2405  printfPQExpBuffer(&buf, " \"%s\"",
2406  PQgetvalue(result, i, 0));
2407 
2408  /* If exclusion constraint, print the constraintdef */
2409  if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
2410  {
2411  appendPQExpBuffer(&buf, " %s",
2412  PQgetvalue(result, i, 6));
2413  }
2414  else
2415  {
2416  const char *indexdef;
2417  const char *usingpos;
2418 
2419  /* Label as primary key or unique (but not both) */
2420  if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2421  appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2422  else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2423  {
2424  if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2425  appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2426  else
2427  appendPQExpBufferStr(&buf, " UNIQUE,");
2428  }
2429 
2430  /* Everything after "USING" is echoed verbatim */
2431  indexdef = PQgetvalue(result, i, 5);
2432  usingpos = strstr(indexdef, " USING ");
2433  if (usingpos)
2434  indexdef = usingpos + 7;
2435  appendPQExpBuffer(&buf, " %s", indexdef);
2436 
2437  /* Need these for deferrable PK/UNIQUE indexes */
2438  if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2439  appendPQExpBufferStr(&buf, " DEFERRABLE");
2440 
2441  if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2442  appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2443  }
2444 
2445  /* Add these for all cases */
2446  if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2447  appendPQExpBufferStr(&buf, " CLUSTER");
2448 
2449  if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2450  appendPQExpBufferStr(&buf, " INVALID");
2451 
2452  if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2453  appendPQExpBufferStr(&buf, " REPLICA IDENTITY");
2454 
2455  printTableAddFooter(&cont, buf.data);
2456 
2457  /* Print tablespace of the index on the same line */
2458  add_tablespace_footer(&cont, RELKIND_INDEX,
2459  atooid(PQgetvalue(result, i, 11)),
2460  false);
2461  }
2462  }
2463  PQclear(result);
2464  }
2465 
2466  /* print table (and column) check constraints */
2467  if (tableinfo.checks)
2468  {
2470  "SELECT r.conname, "
2471  "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2472  "FROM pg_catalog.pg_constraint r\n"
2473  "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
2474  "ORDER BY 1;",
2475  oid);
2476  result = PSQLexec(buf.data);
2477  if (!result)
2478  goto error_return;
2479  else
2480  tuples = PQntuples(result);
2481 
2482  if (tuples > 0)
2483  {
2484  printTableAddFooter(&cont, _("Check constraints:"));
2485  for (i = 0; i < tuples; i++)
2486  {
2487  /* untranslated constraint name and def */
2488  printfPQExpBuffer(&buf, " \"%s\" %s",
2489  PQgetvalue(result, i, 0),
2490  PQgetvalue(result, i, 1));
2491 
2492  printTableAddFooter(&cont, buf.data);
2493  }
2494  }
2495  PQclear(result);
2496  }
2497 
2498  /*
2499  * Print foreign-key constraints (there are none if no triggers,
2500  * except if the table is partitioned, in which case the triggers
2501  * appear in the partitions)
2502  */
2503  if (tableinfo.hastriggers ||
2504  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2505  {
2506  if (pset.sversion >= 120000 &&
2507  (tableinfo.ispartition || tableinfo.relkind == RELKIND_PARTITIONED_TABLE))
2508  {
2509  /*
2510  * Put the constraints defined in this table first, followed
2511  * by the constraints defined in ancestor partitioned tables.
2512  */
2514  "SELECT conrelid = '%s'::pg_catalog.regclass AS sametable,\n"
2515  " conname,\n"
2516  " pg_catalog.pg_get_constraintdef(oid, true) AS condef,\n"
2517  " conrelid::pg_catalog.regclass AS ontable\n"
2518  " FROM pg_catalog.pg_constraint,\n"
2519  " pg_catalog.pg_partition_ancestors('%s')\n"
2520  " WHERE conrelid = relid AND contype = 'f' AND conparentid = 0\n"
2521  "ORDER BY sametable DESC, conname;",
2522  oid, oid);
2523  }
2524  else
2525  {
2527  "SELECT true as sametable, conname,\n"
2528  " pg_catalog.pg_get_constraintdef(r.oid, true) as condef,\n"
2529  " conrelid::pg_catalog.regclass AS ontable\n"
2530  "FROM pg_catalog.pg_constraint r\n"
2531  "WHERE r.conrelid = '%s' AND r.contype = 'f'\n",
2532  oid);
2533 
2534  if (pset.sversion >= 120000)
2535  appendPQExpBufferStr(&buf, " AND conparentid = 0\n");
2536  appendPQExpBufferStr(&buf, "ORDER BY conname");
2537  }
2538 
2539  result = PSQLexec(buf.data);
2540  if (!result)
2541  goto error_return;
2542  else
2543  tuples = PQntuples(result);
2544 
2545  if (tuples > 0)
2546  {
2547  int i_sametable = PQfnumber(result, "sametable"),
2548  i_conname = PQfnumber(result, "conname"),
2549  i_condef = PQfnumber(result, "condef"),
2550  i_ontable = PQfnumber(result, "ontable");
2551 
2552  printTableAddFooter(&cont, _("Foreign-key constraints:"));
2553  for (i = 0; i < tuples; i++)
2554  {
2555  /*
2556  * Print untranslated constraint name and definition. Use
2557  * a "TABLE tab" prefix when the constraint is defined in
2558  * a parent partitioned table.
2559  */
2560  if (strcmp(PQgetvalue(result, i, i_sametable), "f") == 0)
2561  printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2562  PQgetvalue(result, i, i_ontable),
2563  PQgetvalue(result, i, i_conname),
2564  PQgetvalue(result, i, i_condef));
2565  else
2566  printfPQExpBuffer(&buf, " \"%s\" %s",
2567  PQgetvalue(result, i, i_conname),
2568  PQgetvalue(result, i, i_condef));
2569 
2570  printTableAddFooter(&cont, buf.data);
2571  }
2572  }
2573  PQclear(result);
2574  }
2575 
2576  /* print incoming foreign-key references */
2577  if (tableinfo.hastriggers ||
2578  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2579  {
2580  if (pset.sversion >= 120000)
2581  {
2583  "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2584  " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2585  " FROM pg_catalog.pg_constraint c\n"
2586  " WHERE confrelid IN (SELECT pg_catalog.pg_partition_ancestors('%s')\n"
2587  " UNION ALL VALUES ('%s'::pg_catalog.regclass))\n"
2588  " AND contype = 'f' AND conparentid = 0\n"
2589  "ORDER BY conname;",
2590  oid, oid);
2591  }
2592  else
2593  {
2595  "SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2596  " pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2597  " FROM pg_catalog.pg_constraint\n"
2598  " WHERE confrelid = %s AND contype = 'f'\n"
2599  "ORDER BY conname;",
2600  oid);
2601  }
2602 
2603  result = PSQLexec(buf.data);
2604  if (!result)
2605  goto error_return;
2606  else
2607  tuples = PQntuples(result);
2608 
2609  if (tuples > 0)
2610  {
2611  int i_conname = PQfnumber(result, "conname"),
2612  i_ontable = PQfnumber(result, "ontable"),
2613  i_condef = PQfnumber(result, "condef");
2614 
2615  printTableAddFooter(&cont, _("Referenced by:"));
2616  for (i = 0; i < tuples; i++)
2617  {
2618  printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2619  PQgetvalue(result, i, i_ontable),
2620  PQgetvalue(result, i, i_conname),
2621  PQgetvalue(result, i, i_condef));
2622 
2623  printTableAddFooter(&cont, buf.data);
2624  }
2625  }
2626  PQclear(result);
2627  }
2628 
2629  /* print any row-level policies */
2630  if (pset.sversion >= 90500)
2631  {
2632  printfPQExpBuffer(&buf, "SELECT pol.polname,");
2633  if (pset.sversion >= 100000)
2635  " pol.polpermissive,\n");
2636  else
2638  " 't' as polpermissive,\n");
2640  " CASE WHEN pol.polroles = '{0}' THEN NULL ELSE pg_catalog.array_to_string(array(select rolname from pg_catalog.pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2641  " pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2642  " pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2643  " CASE pol.polcmd\n"
2644  " WHEN 'r' THEN 'SELECT'\n"
2645  " WHEN 'a' THEN 'INSERT'\n"
2646  " WHEN 'w' THEN 'UPDATE'\n"
2647  " WHEN 'd' THEN 'DELETE'\n"
2648  " END AS cmd\n"
2649  "FROM pg_catalog.pg_policy pol\n"
2650  "WHERE pol.polrelid = '%s' ORDER BY 1;",
2651  oid);
2652 
2653  result = PSQLexec(buf.data);
2654  if (!result)
2655  goto error_return;
2656  else
2657  tuples = PQntuples(result);
2658 
2659  /*
2660  * Handle cases where RLS is enabled and there are policies, or
2661  * there aren't policies, or RLS isn't enabled but there are
2662  * policies
2663  */
2664  if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2665  printTableAddFooter(&cont, _("Policies:"));
2666 
2667  if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2668  printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2669 
2670  if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2671  printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2672 
2673  if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2674  printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2675 
2676  if (!tableinfo.rowsecurity && tuples > 0)
2677  printTableAddFooter(&cont, _("Policies (row security disabled):"));
2678 
2679  /* Might be an empty set - that's ok */
2680  for (i = 0; i < tuples; i++)
2681  {
2682  printfPQExpBuffer(&buf, " POLICY \"%s\"",
2683  PQgetvalue(result, i, 0));
2684 
2685  if (*(PQgetvalue(result, i, 1)) == 'f')
2686  appendPQExpBufferStr(&buf, " AS RESTRICTIVE");
2687 
2688  if (!PQgetisnull(result, i, 5))
2689  appendPQExpBuffer(&buf, " FOR %s",
2690  PQgetvalue(result, i, 5));
2691 
2692  if (!PQgetisnull(result, i, 2))
2693  {
2694  appendPQExpBuffer(&buf, "\n TO %s",
2695  PQgetvalue(result, i, 2));
2696  }
2697 
2698  if (!PQgetisnull(result, i, 3))
2699  appendPQExpBuffer(&buf, "\n USING (%s)",
2700  PQgetvalue(result, i, 3));
2701 
2702  if (!PQgetisnull(result, i, 4))
2703  appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2704  PQgetvalue(result, i, 4));
2705 
2706  printTableAddFooter(&cont, buf.data);
2707  }
2708  PQclear(result);
2709  }
2710 
2711  /* print any extended statistics */
2712  if (pset.sversion >= 140000)
2713  {
2715  "SELECT oid, "
2716  "stxrelid::pg_catalog.regclass, "
2717  "stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS nsp, "
2718  "stxname,\n"
2719  "pg_catalog.pg_get_statisticsobjdef_columns(oid) AS columns,\n"
2720  " 'd' = any(stxkind) AS ndist_enabled,\n"
2721  " 'f' = any(stxkind) AS deps_enabled,\n"
2722  " 'm' = any(stxkind) AS mcv_enabled,\n"
2723  "stxstattarget\n"
2724  "FROM pg_catalog.pg_statistic_ext\n"
2725  "WHERE stxrelid = '%s'\n"
2726  "ORDER BY nsp, stxname;",
2727  oid);
2728 
2729  result = PSQLexec(buf.data);
2730  if (!result)
2731  goto error_return;
2732  else
2733  tuples = PQntuples(result);
2734 
2735  if (tuples > 0)
2736  {
2737  printTableAddFooter(&cont, _("Statistics objects:"));
2738 
2739  for (i = 0; i < tuples; i++)
2740  {
2741  bool gotone = false;
2742  bool has_ndistinct;
2743  bool has_dependencies;
2744  bool has_mcv;
2745  bool has_all;
2746  bool has_some;
2747 
2748  has_ndistinct = (strcmp(PQgetvalue(result, i, 5), "t") == 0);
2749  has_dependencies = (strcmp(PQgetvalue(result, i, 6), "t") == 0);
2750  has_mcv = (strcmp(PQgetvalue(result, i, 7), "t") == 0);
2751 
2752  printfPQExpBuffer(&buf, " ");
2753 
2754  /* statistics object name (qualified with namespace) */
2755  appendPQExpBuffer(&buf, "\"%s.%s\"",
2756  PQgetvalue(result, i, 2),
2757  PQgetvalue(result, i, 3));
2758 
2759  /*
2760  * When printing kinds we ignore expression statistics,
2761  * which are used only internally and can't be specified
2762  * by user. We don't print the kinds when none are
2763  * specified (in which case it has to be statistics on a
2764  * single expr) or when all are specified (in which case
2765  * we assume it's expanded by CREATE STATISTICS).
2766  */
2767  has_all = (has_ndistinct && has_dependencies && has_mcv);
2768  has_some = (has_ndistinct || has_dependencies || has_mcv);
2769 
2770  if (has_some && !has_all)
2771  {
2772  appendPQExpBufferStr(&buf, " (");
2773 
2774  /* options */
2775  if (has_ndistinct)
2776  {
2777  appendPQExpBufferStr(&buf, "ndistinct");
2778  gotone = true;
2779  }
2780 
2781  if (has_dependencies)
2782  {
2783  appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2784  gotone = true;
2785  }
2786 
2787  if (has_mcv)
2788  {
2789  appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2790  }
2791 
2792  appendPQExpBufferChar(&buf, ')');
2793  }
2794 
2795  appendPQExpBuffer(&buf, " ON %s FROM %s",
2796  PQgetvalue(result, i, 4),
2797  PQgetvalue(result, i, 1));
2798 
2799  /* Show the stats target if it's not default */
2800  if (strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2801  appendPQExpBuffer(&buf, "; STATISTICS %s",
2802  PQgetvalue(result, i, 8));
2803 
2804  printTableAddFooter(&cont, buf.data);
2805  }
2806  }
2807  PQclear(result);
2808  }
2809  else if (pset.sversion >= 100000)
2810  {
2812  "SELECT oid, "
2813  "stxrelid::pg_catalog.regclass, "
2814  "stxnamespace::pg_catalog.regnamespace AS nsp, "
2815  "stxname,\n"
2816  " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')\n"
2817  " FROM pg_catalog.unnest(stxkeys) s(attnum)\n"
2818  " JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND\n"
2819  " a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
2820  " 'd' = any(stxkind) AS ndist_enabled,\n"
2821  " 'f' = any(stxkind) AS deps_enabled,\n"
2822  " 'm' = any(stxkind) AS mcv_enabled,\n");
2823 
2824  if (pset.sversion >= 130000)
2825  appendPQExpBufferStr(&buf, " stxstattarget\n");
2826  else
2827  appendPQExpBufferStr(&buf, " -1 AS stxstattarget\n");
2828  appendPQExpBuffer(&buf, "FROM pg_catalog.pg_statistic_ext\n"
2829  "WHERE stxrelid = '%s'\n"
2830  "ORDER BY 1;",
2831  oid);
2832 
2833  result = PSQLexec(buf.data);
2834  if (!result)
2835  goto error_return;
2836  else
2837  tuples = PQntuples(result);
2838 
2839  if (tuples > 0)
2840  {
2841  printTableAddFooter(&cont, _("Statistics objects:"));
2842 
2843  for (i = 0; i < tuples; i++)
2844  {
2845  bool gotone = false;
2846 
2847  printfPQExpBuffer(&buf, " ");
2848 
2849  /* statistics object name (qualified with namespace) */
2850  appendPQExpBuffer(&buf, "\"%s.%s\" (",
2851  PQgetvalue(result, i, 2),
2852  PQgetvalue(result, i, 3));
2853 
2854  /* options */
2855  if (strcmp(PQgetvalue(result, i, 5), "t") == 0)
2856  {
2857  appendPQExpBufferStr(&buf, "ndistinct");
2858  gotone = true;
2859  }
2860 
2861  if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
2862  {
2863  appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2864  gotone = true;
2865  }
2866 
2867  if (strcmp(PQgetvalue(result, i, 7), "t") == 0)
2868  {
2869  appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : "");
2870  }
2871 
2872  appendPQExpBuffer(&buf, ") ON %s FROM %s",
2873  PQgetvalue(result, i, 4),
2874  PQgetvalue(result, i, 1));
2875 
2876  /* Show the stats target if it's not default */
2877  if (strcmp(PQgetvalue(result, i, 8), "-1") != 0)
2878  appendPQExpBuffer(&buf, "; STATISTICS %s",
2879  PQgetvalue(result, i, 8));
2880 
2881  printTableAddFooter(&cont, buf.data);
2882  }
2883  }
2884  PQclear(result);
2885  }
2886 
2887  /* print rules */
2888  if (tableinfo.hasrules && tableinfo.relkind != RELKIND_MATVIEW)
2889  {
2891  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2892  "ev_enabled\n"
2893  "FROM pg_catalog.pg_rewrite r\n"
2894  "WHERE r.ev_class = '%s' ORDER BY 1;",
2895  oid);
2896  result = PSQLexec(buf.data);
2897  if (!result)
2898  goto error_return;
2899  else
2900  tuples = PQntuples(result);
2901 
2902  if (tuples > 0)
2903  {
2904  bool have_heading;
2905  int category;
2906 
2907  for (category = 0; category < 4; category++)
2908  {
2909  have_heading = false;
2910 
2911  for (i = 0; i < tuples; i++)
2912  {
2913  const char *ruledef;
2914  bool list_rule = false;
2915 
2916  switch (category)
2917  {
2918  case 0:
2919  if (*PQgetvalue(result, i, 2) == 'O')
2920  list_rule = true;
2921  break;
2922  case 1:
2923  if (*PQgetvalue(result, i, 2) == 'D')
2924  list_rule = true;
2925  break;
2926  case 2:
2927  if (*PQgetvalue(result, i, 2) == 'A')
2928  list_rule = true;
2929  break;
2930  case 3:
2931  if (*PQgetvalue(result, i, 2) == 'R')
2932  list_rule = true;
2933  break;
2934  }
2935  if (!list_rule)
2936  continue;
2937 
2938  if (!have_heading)
2939  {
2940  switch (category)
2941  {
2942  case 0:
2943  printfPQExpBuffer(&buf, _("Rules:"));
2944  break;
2945  case 1:
2946  printfPQExpBuffer(&buf, _("Disabled rules:"));
2947  break;
2948  case 2:
2949  printfPQExpBuffer(&buf, _("Rules firing always:"));
2950  break;
2951  case 3:
2952  printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
2953  break;
2954  }
2955  printTableAddFooter(&cont, buf.data);
2956  have_heading = true;
2957  }
2958 
2959  /* Everything after "CREATE RULE" is echoed verbatim */
2960  ruledef = PQgetvalue(result, i, 1);
2961  ruledef += 12;
2962  printfPQExpBuffer(&buf, " %s", ruledef);
2963  printTableAddFooter(&cont, buf.data);
2964  }
2965  }
2966  }
2967  PQclear(result);
2968  }
2969 
2970  /* print any publications */
2971  if (pset.sversion >= 100000)
2972  {
2973  if (pset.sversion >= 150000)
2974  {
2976  "SELECT pubname\n"
2977  " , NULL\n"
2978  " , NULL\n"
2979  "FROM pg_catalog.pg_publication p\n"
2980  " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
2981  " JOIN pg_catalog.pg_class pc ON pc.relnamespace = pn.pnnspid\n"
2982  "WHERE pc.oid ='%s' and pg_catalog.pg_relation_is_publishable('%s')\n"
2983  "UNION\n"
2984  "SELECT pubname\n"
2985  " , pg_get_expr(pr.prqual, c.oid)\n"
2986  " , (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
2987  " (SELECT string_agg(attname, ', ')\n"
2988  " FROM pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
2989  " pg_catalog.pg_attribute\n"
2990  " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
2991  " ELSE NULL END) "
2992  "FROM pg_catalog.pg_publication p\n"
2993  " JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
2994  " JOIN pg_catalog.pg_class c ON c.oid = pr.prrelid\n"
2995  "WHERE pr.prrelid = '%s'\n"
2996  "UNION\n"
2997  "SELECT pubname\n"
2998  " , NULL\n"
2999  " , NULL\n"
3000  "FROM pg_catalog.pg_publication p\n"
3001  "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3002  "ORDER BY 1;",
3003  oid, oid, oid, oid);
3004  }
3005  else
3006  {
3008  "SELECT pubname\n"
3009  " , NULL\n"
3010  " , NULL\n"
3011  "FROM pg_catalog.pg_publication p\n"
3012  "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
3013  "WHERE pr.prrelid = '%s'\n"
3014  "UNION ALL\n"
3015  "SELECT pubname\n"
3016  " , NULL\n"
3017  " , NULL\n"
3018  "FROM pg_catalog.pg_publication p\n"
3019  "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
3020  "ORDER BY 1;",
3021  oid, oid);
3022  }
3023 
3024  result = PSQLexec(buf.data);
3025  if (!result)
3026  goto error_return;
3027  else
3028  tuples = PQntuples(result);
3029 
3030  if (tuples > 0)
3031  printTableAddFooter(&cont, _("Publications:"));
3032 
3033  /* Might be an empty set - that's ok */
3034  for (i = 0; i < tuples; i++)
3035  {
3036  printfPQExpBuffer(&buf, " \"%s\"",
3037  PQgetvalue(result, i, 0));
3038 
3039  /* column list (if any) */
3040  if (!PQgetisnull(result, i, 2))
3041  appendPQExpBuffer(&buf, " (%s)",
3042  PQgetvalue(result, i, 2));
3043 
3044  /* row filter (if any) */
3045  if (!PQgetisnull(result, i, 1))
3046  appendPQExpBuffer(&buf, " WHERE %s",
3047  PQgetvalue(result, i, 1));
3048 
3049  printTableAddFooter(&cont, buf.data);
3050  }
3051  PQclear(result);
3052  }
3053  }
3054 
3055  /* Get view_def if table is a view or materialized view */
3056  if ((tableinfo.relkind == RELKIND_VIEW ||
3057  tableinfo.relkind == RELKIND_MATVIEW) && verbose)
3058  {
3059  PGresult *result;
3060 
3062  "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
3063  oid);
3064  result = PSQLexec(buf.data);
3065  if (!result)
3066  goto error_return;
3067 
3068  if (PQntuples(result) > 0)
3069  view_def = pg_strdup(PQgetvalue(result, 0, 0));
3070 
3071  PQclear(result);
3072  }
3073 
3074  if (view_def)
3075  {
3076  PGresult *result = NULL;
3077 
3078  /* Footer information about a view */
3079  printTableAddFooter(&cont, _("View definition:"));
3080  printTableAddFooter(&cont, view_def);
3081 
3082  /* print rules */
3083  if (tableinfo.hasrules)
3084  {
3086  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
3087  "FROM pg_catalog.pg_rewrite r\n"
3088  "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
3089  oid);
3090  result = PSQLexec(buf.data);
3091  if (!result)
3092  goto error_return;
3093 
3094  if (PQntuples(result) > 0)
3095  {
3096  printTableAddFooter(&cont, _("Rules:"));
3097  for (i = 0; i < PQntuples(result); i++)
3098  {
3099  const char *ruledef;
3100 
3101  /* Everything after "CREATE RULE" is echoed verbatim */
3102  ruledef = PQgetvalue(result, i, 1);
3103  ruledef += 12;
3104 
3105  printfPQExpBuffer(&buf, " %s", ruledef);
3106  printTableAddFooter(&cont, buf.data);
3107  }
3108  }
3109  PQclear(result);
3110  }
3111  }
3112 
3113  /*
3114  * Print triggers next, if any (but only user-defined triggers). This
3115  * could apply to either a table or a view.
3116  */
3117  if (tableinfo.hastriggers)
3118  {
3119  PGresult *result;
3120  int tuples;
3121 
3123  "SELECT t.tgname, "
3124  "pg_catalog.pg_get_triggerdef(t.oid, true), "
3125  "t.tgenabled, t.tgisinternal,\n");
3126 
3127  /*
3128  * Detect whether each trigger is inherited, and if so, get the name
3129  * of the topmost table it's inherited from. We have no easy way to
3130  * do that pre-v13, for lack of the tgparentid column. Even with
3131  * tgparentid, a straightforward search for the topmost parent would
3132  * require a recursive CTE, which seems unduly expensive. We cheat a
3133  * bit by assuming parent triggers will match by tgname; then, joining
3134  * with pg_partition_ancestors() allows the planner to make use of
3135  * pg_trigger_tgrelid_tgname_index if it wishes. We ensure we find
3136  * the correct topmost parent by stopping at the first-in-partition-
3137  * ancestry-order trigger that has tgparentid = 0. (There might be
3138  * unrelated, non-inherited triggers with the same name further up the
3139  * stack, so this is important.)
3140  */
3141  if (pset.sversion >= 130000)
3143  " CASE WHEN t.tgparentid != 0 THEN\n"
3144  " (SELECT u.tgrelid::pg_catalog.regclass\n"
3145  " FROM pg_catalog.pg_trigger AS u,\n"
3146  " pg_catalog.pg_partition_ancestors(t.tgrelid) WITH ORDINALITY AS a(relid, depth)\n"
3147  " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid\n"
3148  " AND u.tgparentid = 0\n"
3149  " ORDER BY a.depth LIMIT 1)\n"
3150  " END AS parent\n");
3151  else
3152  appendPQExpBufferStr(&buf, " NULL AS parent\n");
3153 
3155  "FROM pg_catalog.pg_trigger t\n"
3156  "WHERE t.tgrelid = '%s' AND ",
3157  oid);
3158 
3159  /*
3160  * tgisinternal is set true for inherited triggers of partitions in
3161  * servers between v11 and v14, though these must still be shown to
3162  * the user. So we use another property that is true for such
3163  * inherited triggers to avoid them being hidden, which is their
3164  * dependence on another trigger.
3165  */
3166  if (pset.sversion >= 110000 && pset.sversion < 150000)
3167  appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D') \n"
3168  " OR EXISTS (SELECT 1 FROM pg_catalog.pg_depend WHERE objid = t.oid \n"
3169  " AND refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass))");
3170  else
3171  /* display/warn about disabled internal triggers */
3172  appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
3173  appendPQExpBufferStr(&buf, "\nORDER BY 1;");
3174 
3175  result = PSQLexec(buf.data);
3176  if (!result)
3177  goto error_return;
3178  else
3179  tuples = PQntuples(result);
3180 
3181  if (tuples > 0)
3182  {
3183  bool have_heading;
3184  int category;
3185 
3186  /*
3187  * split the output into 4 different categories. Enabled triggers,
3188  * disabled triggers and the two special ALWAYS and REPLICA
3189  * configurations.
3190  */
3191  for (category = 0; category <= 4; category++)
3192  {
3193  have_heading = false;
3194  for (i = 0; i < tuples; i++)
3195  {
3196  bool list_trigger;
3197  const char *tgdef;
3198  const char *usingpos;
3199  const char *tgenabled;
3200  const char *tgisinternal;
3201 
3202  /*
3203  * Check if this trigger falls into the current category
3204  */
3205  tgenabled = PQgetvalue(result, i, 2);
3206  tgisinternal = PQgetvalue(result, i, 3);
3207  list_trigger = false;
3208  switch (category)
3209  {
3210  case 0:
3211  if (*tgenabled == 'O' || *tgenabled == 't')
3212  list_trigger = true;
3213  break;
3214  case 1:
3215  if ((*tgenabled == 'D' || *tgenabled == 'f') &&
3216  *tgisinternal == 'f')
3217  list_trigger = true;
3218  break;
3219  case 2:
3220  if ((*tgenabled == 'D' || *tgenabled == 'f') &&
3221  *tgisinternal == 't')
3222  list_trigger = true;
3223  break;
3224  case 3:
3225  if (*tgenabled == 'A')
3226  list_trigger = true;
3227  break;
3228  case 4:
3229  if (*tgenabled == 'R')
3230  list_trigger = true;
3231  break;
3232  }
3233  if (list_trigger == false)
3234  continue;
3235 
3236  /* Print the category heading once */
3237  if (have_heading == false)
3238  {
3239  switch (category)
3240  {
3241  case 0:
3242  printfPQExpBuffer(&buf, _("Triggers:"));
3243  break;
3244  case 1:
3245  printfPQExpBuffer(&buf, _("Disabled user triggers:"));
3246  break;
3247  case 2:
3248  printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
3249  break;
3250  case 3:
3251  printfPQExpBuffer(&buf, _("Triggers firing always:"));
3252  break;
3253  case 4:
3254  printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
3255  break;
3256  }
3257  printTableAddFooter(&cont, buf.data);
3258  have_heading = true;
3259  }
3260 
3261  /* Everything after "TRIGGER" is echoed verbatim */
3262  tgdef = PQgetvalue(result, i, 1);
3263  usingpos = strstr(tgdef, " TRIGGER ");
3264  if (usingpos)
3265  tgdef = usingpos + 9;
3266 
3267  printfPQExpBuffer(&buf, " %s", tgdef);
3268 
3269  /* Visually distinguish inherited triggers */
3270  if (!PQgetisnull(result, i, 4))
3271  appendPQExpBuffer(&buf, ", ON TABLE %s",
3272  PQgetvalue(result, i, 4));
3273 
3274  printTableAddFooter(&cont, buf.data);
3275  }
3276  }
3277  }
3278  PQclear(result);
3279  }
3280 
3281  /*
3282  * Finish printing the footer information about a table.
3283  */
3284  if (tableinfo.relkind == RELKIND_RELATION ||
3285  tableinfo.relkind == RELKIND_MATVIEW ||
3286  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
3287  tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3288  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
3289  tableinfo.relkind == RELKIND_TOASTVALUE)
3290  {
3291  bool is_partitioned;
3292  PGresult *result;
3293  int tuples;
3294 
3295  /* simplify some repeated tests below */
3296  is_partitioned = (tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3297  tableinfo.relkind == RELKIND_PARTITIONED_INDEX);
3298 
3299  /* print foreign server name */
3300  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
3301  {
3302  char *ftoptions;
3303 
3304  /* Footer information about foreign table */
3306  "SELECT s.srvname,\n"
3307  " pg_catalog.array_to_string(ARRAY(\n"
3308  " SELECT pg_catalog.quote_ident(option_name)"
3309  " || ' ' || pg_catalog.quote_literal(option_value)\n"
3310  " FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
3311  "FROM pg_catalog.pg_foreign_table f,\n"
3312  " pg_catalog.pg_foreign_server s\n"
3313  "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
3314  oid);
3315  result = PSQLexec(buf.data);
3316  if (!result)
3317  goto error_return;
3318  else if (PQntuples(result) != 1)
3319  {
3320  PQclear(result);
3321  goto error_return;
3322  }
3323 
3324  /* Print server name */
3325  printfPQExpBuffer(&buf, _("Server: %s"),
3326  PQgetvalue(result, 0, 0));
3327  printTableAddFooter(&cont, buf.data);
3328 
3329  /* Print per-table FDW options, if any */
3330  ftoptions = PQgetvalue(result, 0, 1);
3331  if (ftoptions && ftoptions[0] != '\0')
3332  {
3333  printfPQExpBuffer(&buf, _("FDW options: (%s)"), ftoptions);
3334  printTableAddFooter(&cont, buf.data);
3335  }
3336  PQclear(result);
3337  }
3338 
3339  /* print tables inherited from (exclude partitioned parents) */
3341  "SELECT c.oid::pg_catalog.regclass\n"
3342  "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3343  "WHERE c.oid = i.inhparent AND i.inhrelid = '%s'\n"
3344  " AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_TABLE)
3345  " AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_INDEX)
3346  "\nORDER BY inhseqno;",
3347  oid);
3348 
3349  result = PSQLexec(buf.data);
3350  if (!result)
3351  goto error_return;
3352  else
3353  {
3354  const char *s = _("Inherits");
3355  int sw = pg_wcswidth(s, strlen(s), pset.encoding);
3356 
3357  tuples = PQntuples(result);
3358 
3359  for (i = 0; i < tuples; i++)
3360  {
3361  if (i == 0)
3362  printfPQExpBuffer(&buf, "%s: %s",
3363  s, PQgetvalue(result, i, 0));
3364  else
3365  printfPQExpBuffer(&buf, "%*s %s",
3366  sw, "", PQgetvalue(result, i, 0));
3367  if (i < tuples - 1)
3368  appendPQExpBufferChar(&buf, ',');
3369 
3370  printTableAddFooter(&cont, buf.data);
3371  }
3372 
3373  PQclear(result);
3374  }
3375 
3376  /* print child tables (with additional info if partitions) */
3377  if (pset.sversion >= 140000)
3379  "SELECT c.oid::pg_catalog.regclass, c.relkind,"
3380  " inhdetachpending,"
3381  " pg_catalog.pg_get_expr(c.relpartbound, c.oid)\n"
3382  "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3383  "WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3384  "ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
3385  " c.oid::pg_catalog.regclass::pg_catalog.text;",
3386  oid);
3387  else if (pset.sversion >= 100000)
3389  "SELECT c.oid::pg_catalog.regclass, c.relkind,"
3390  " false AS inhdetachpending,"
3391  " pg_catalog.pg_get_expr(c.relpartbound, c.oid)\n"
3392  "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3393  "WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3394  "ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
3395  " c.oid::pg_catalog.regclass::pg_catalog.text;",
3396  oid);
3397  else
3399  "SELECT c.oid::pg_catalog.regclass, c.relkind,"
3400  " false AS inhdetachpending, NULL\n"
3401  "FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3402  "WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3403  "ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;",
3404  oid);
3405 
3406  result = PSQLexec(buf.data);
3407  if (!result)
3408  goto error_return;
3409  tuples = PQntuples(result);
3410 
3411  /*
3412  * For a partitioned table with no partitions, always print the number
3413  * of partitions as zero, even when verbose output is expected.
3414  * Otherwise, we will not print "Partitions" section for a partitioned
3415  * table without any partitions.
3416  */
3417  if (is_partitioned && tuples == 0)
3418  {
3419  printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
3420  printTableAddFooter(&cont, buf.data);
3421  }
3422  else if (!verbose)
3423  {
3424  /* print the number of child tables, if any */
3425  if (tuples > 0)
3426  {
3427  if (is_partitioned)
3428  printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
3429  else
3430  printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
3431  printTableAddFooter(&cont, buf.data);
3432  }
3433  }
3434  else
3435  {
3436  /* display the list of child tables */
3437  const char *ct = is_partitioned ? _("Partitions") : _("Child tables");
3438  int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
3439 
3440  for (i = 0; i < tuples; i++)
3441  {
3442  char child_relkind = *PQgetvalue(result, i, 1);
3443 
3444  if (i == 0)
3445  printfPQExpBuffer(&buf, "%s: %s",
3446  ct, PQgetvalue(result, i, 0));
3447  else
3448  printfPQExpBuffer(&buf, "%*s %s",
3449  ctw, "", PQgetvalue(result, i, 0));
3450  if (!PQgetisnull(result, i, 3))
3451  appendPQExpBuffer(&buf, " %s", PQgetvalue(result, i, 3));
3452  if (child_relkind == RELKIND_PARTITIONED_TABLE ||
3453  child_relkind == RELKIND_PARTITIONED_INDEX)
3454  appendPQExpBufferStr(&buf, ", PARTITIONED");
3455  else if (child_relkind == RELKIND_FOREIGN_TABLE)
3456  appendPQExpBufferStr(&buf, ", FOREIGN");
3457  if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
3458  appendPQExpBufferStr(&buf, " (DETACH PENDING)");
3459  if (i < tuples - 1)
3460  appendPQExpBufferChar(&buf, ',');
3461 
3462  printTableAddFooter(&cont, buf.data);
3463  }
3464  }
3465  PQclear(result);
3466 
3467  /* Table type */
3468  if (tableinfo.reloftype)
3469  {
3470  printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
3471  printTableAddFooter(&cont, buf.data);
3472  }
3473 
3474  if (verbose &&
3475  (tableinfo.relkind == RELKIND_RELATION ||
3476  tableinfo.relkind == RELKIND_MATVIEW) &&
3477 
3478  /*
3479  * No need to display default values; we already display a REPLICA
3480  * IDENTITY marker on indexes.
3481  */
3482  tableinfo.relreplident != 'i' &&
3483  ((strcmp(schemaname, "pg_catalog") != 0 && tableinfo.relreplident != 'd') ||
3484  (strcmp(schemaname, "pg_catalog") == 0 && tableinfo.relreplident != 'n')))
3485  {
3486  const char *s = _("Replica Identity");
3487 
3488  printfPQExpBuffer(&buf, "%s: %s",
3489  s,
3490  tableinfo.relreplident == 'f' ? "FULL" :
3491  tableinfo.relreplident == 'n' ? "NOTHING" :
3492  "???");
3493 
3494  printTableAddFooter(&cont, buf.data);
3495  }
3496 
3497  /* OIDs, if verbose and not a materialized view */
3498  if (verbose && tableinfo.relkind != RELKIND_MATVIEW && tableinfo.hasoids)
3499  printTableAddFooter(&cont, _("Has OIDs: yes"));
3500 
3501  /* Tablespace info */
3502  add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
3503  true);
3504 
3505  /* Access method info */
3506  if (verbose && tableinfo.relam != NULL && !pset.hide_tableam)
3507  {
3508  printfPQExpBuffer(&buf, _("Access method: %s"), tableinfo.relam);
3509  printTableAddFooter(&cont, buf.data);
3510  }
3511  }
3512 
3513  /* reloptions, if verbose */
3514  if (verbose &&
3515  tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
3516  {
3517  const char *t = _("Options");
3518 
3519  printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
3520  printTableAddFooter(&cont, buf.data);
3521  }
3522 
3523  printTable(&cont, pset.queryFout, false, pset.logfile);
3524 
3525  retval = true;
3526 
3527 error_return:
3528 
3529  /* clean up */
3530  if (printTableInitialized)
3531  printTableCleanup(&cont);
3532  termPQExpBuffer(&buf);
3533  termPQExpBuffer(&title);
3535 
3536  free(view_def);
3537 
3538  PQclear(res);
3539 
3540  return retval;
3541 }
signed short int16
Definition: c.h:477
#define CppAsString2(x)
Definition: c.h:311
static void add_tablespace_footer(printTableContent *const cont, char relkind, Oid tablespace, const bool newline)
Definition: describe.c:3549
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3418
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3171
void printTableCleanup(printTableContent *const content)
Definition: print.c:3339
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3248
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3428
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3208
#define free(a)
Definition: header.h:65
#define storage
Definition: indent_codes.h:68
Assert(fmt[strlen(fmt) - 1] !='\n')
int pg_wcswidth(const char *pwcs, size_t len, int encoding)
Definition: mbprint.c:177
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
#define atooid(x)
Definition: postgres_ext.h:42
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
bool hide_tableam
Definition: settings.h:141
int encoding
Definition: settings.h:83
bool hide_compression
Definition: settings.h:140
printTableOpt topt
Definition: print.h:180
char ** footers
Definition: print.h:183
unsigned short int expanded
Definition: print.h:111
bool default_footer
Definition: print.h:124
static StringInfoData tmpbuf
Definition: walsender.c:160

References _, add_tablespace_footer(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), Assert(), atooid, buf, CppAsString2, StringInfoData::data, PQExpBufferData::data, printTableOpt::default_footer, _psqlSettings::encoding, printTableOpt::expanded, fmtId(), printQueryOpt::footers, free, gettext_noop, _psqlSettings::hide_compression, _psqlSettings::hide_tableam, i, initPQExpBuffer(), lengthof, _psqlSettings::logfile, pg_log_error, pg_strdup(), pg_wcswidth(), _psqlSettings::popt, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), printQuery(), printTable(), printTableAddCell(), printTableAddFooter(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, psprintf(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, relname, res, resetPQExpBuffer(), storage, _psqlSettings::sversion, tablespace, termPQExpBuffer(), printQueryOpt::title, tmpbuf, printQueryOpt::topt, printQueryOpt::translate_header, and verbose.

Referenced by describeTableDetails().

◆ describeOneTSConfig()

static bool describeOneTSConfig ( const char *  oid,
const char *  nspname,
const char *  cfgname,
const char *  pnspname,
const char *  prsname 
)
static

Definition at line 5614 of file describe.c.

5616 {
5618  title;
5619  PGresult *res;
5620  printQueryOpt myopt = pset.popt;
5621 
5622  initPQExpBuffer(&buf);
5623 
5625  "SELECT\n"
5626  " ( SELECT t.alias FROM\n"
5627  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
5628  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
5629  " pg_catalog.btrim(\n"
5630  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
5631  " FROM pg_catalog.pg_ts_config_map AS mm\n"
5632  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
5633  " ORDER BY mapcfg, maptokentype, mapseqno\n"
5634  " ) :: pg_catalog.text,\n"
5635  " '{}') AS \"%s\"\n"
5636  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
5637  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
5638  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
5639  "ORDER BY 1;",
5640  gettext_noop("Token"),
5641  gettext_noop("Dictionaries"),
5642  oid);
5643 
5644  res = PSQLexec(buf.data);
5645  termPQExpBuffer(&buf);
5646  if (!res)
5647  return false;
5648 
5649  initPQExpBuffer(&title);
5650 
5651  if (nspname)
5652  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
5653  nspname, cfgname);
5654  else
5655  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
5656  cfgname);
5657 
5658  if (pnspname)
5659  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
5660  pnspname, prsname);
5661  else
5662  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
5663  prsname);
5664 
5665  myopt.nullPrint = NULL;
5666  myopt.title = title.data;
5667  myopt.footers = NULL;
5668  myopt.topt.default_footer = false;
5669  myopt.translate_header = true;
5670 
5671  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5672 
5673  termPQExpBuffer(&title);
5674 
5675  PQclear(res);
5676  return true;
5677 }

References _, appendPQExpBuffer(), buf, PQExpBufferData::data, printTableOpt::default_footer, printQueryOpt::footers, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::topt, and printQueryOpt::translate_header.

Referenced by listTSConfigsVerbose().

◆ describeOneTSParser()

static bool describeOneTSParser ( const char *  oid,
const char *  nspname,
const char *  prsname 
)
static

Definition at line 5226 of file describe.c.

5227 {
5229  PGresult *res;
5230  PQExpBufferData title;
5231  printQueryOpt myopt = pset.popt;
5232  static const bool translate_columns[] = {true, false, false};
5233 
5234  initPQExpBuffer(&buf);
5235 
5237  "SELECT '%s' AS \"%s\",\n"
5238  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
5239  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
5240  " FROM pg_catalog.pg_ts_parser p\n"
5241  " WHERE p.oid = '%s'\n"
5242  "UNION ALL\n"
5243  "SELECT '%s',\n"
5244  " p.prstoken::pg_catalog.regproc,\n"
5245  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
5246  " FROM pg_catalog.pg_ts_parser p\n"
5247  " WHERE p.oid = '%s'\n"
5248  "UNION ALL\n"
5249  "SELECT '%s',\n"
5250  " p.prsend::pg_catalog.regproc,\n"
5251  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
5252  " FROM pg_catalog.pg_ts_parser p\n"
5253  " WHERE p.oid = '%s'\n"
5254  "UNION ALL\n"
5255  "SELECT '%s',\n"
5256  " p.prsheadline::pg_catalog.regproc,\n"
5257  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
5258  " FROM pg_catalog.pg_ts_parser p\n"
5259  " WHERE p.oid = '%s'\n"
5260  "UNION ALL\n"
5261  "SELECT '%s',\n"
5262  " p.prslextype::pg_catalog.regproc,\n"
5263  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
5264  " FROM pg_catalog.pg_ts_parser p\n"
5265  " WHERE p.oid = '%s';",
5266  gettext_noop("Start parse"),
5267  gettext_noop("Method"),
5268  gettext_noop("Function"),
5269  gettext_noop("Description"),
5270  oid,
5271  gettext_noop("Get next token"),
5272  oid,
5273  gettext_noop("End parse"),
5274  oid,
5275  gettext_noop("Get headline"),
5276  oid,
5277  gettext_noop("Get token types"),
5278  oid);
5279 
5280  res = PSQLexec(buf.data);
5281  termPQExpBuffer(&buf);
5282  if (!res)
5283  return false;
5284 
5285  myopt.nullPrint = NULL;
5286  initPQExpBuffer(&title);
5287  if (nspname)
5288  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
5289  nspname, prsname);
5290  else
5291  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
5292  myopt.title = title.data;
5293  myopt.footers = NULL;
5294  myopt.topt.default_footer = false;
5295  myopt.translate_header = true;
5296  myopt.translate_columns = translate_columns;
5297  myopt.n_translate_columns = lengthof(translate_columns);
5298 
5299  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5300 
5301  PQclear(res);
5302 
5303  initPQExpBuffer(&buf);
5304 
5306  "SELECT t.alias as \"%s\",\n"
5307  " t.description as \"%s\"\n"
5308  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
5309  "ORDER BY 1;",
5310  gettext_noop("Token name"),
5311  gettext_noop("Description"),
5312  oid);
5313 
5314  res = PSQLexec(buf.data);
5315  termPQExpBuffer(&buf);
5316  if (!res)
5317  {
5318  termPQExpBuffer(&title);
5319  return false;
5320  }
5321 
5322  myopt.nullPrint = NULL;
5323  if (nspname)
5324  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
5325  nspname, prsname);
5326  else
5327  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
5328  myopt.title = title.data;
5329  myopt.footers = NULL;
5330  myopt.topt.default_footer = true;
5331  myopt.translate_header = true;
5332  myopt.translate_columns = NULL;
5333  myopt.n_translate_columns = 0;
5334 
5335  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5336 
5337  termPQExpBuffer(&title);
5338  PQclear(res);
5339  return true;
5340 }

References _, buf, PQExpBufferData::data, printTableOpt::default_footer, printQueryOpt::footers, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, res, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::topt, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by listTSParsersVerbose().

◆ describeOperators()

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

Definition at line 775 of file describe.c.

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

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, map_typename_pattern(), printQueryOpt::nullPrint, _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 6304 of file describe.c.

6305 {
6307  int i;
6308  PGresult *res;
6309  bool has_pubtruncate;
6310  bool has_pubviaroot;
6311 
6312  PQExpBufferData title;
6313  printTableContent cont;
6314 
6315  if (pset.sversion < 100000)
6316  {
6317  char sverbuf[32];
6318 
6319  pg_log_error("The server (version %s) does not support publications.",
6321  sverbuf, sizeof(sverbuf)));
6322  return true;
6323  }
6324 
6325  has_pubtruncate = (pset.sversion >= 110000);
6326  has_pubviaroot = (pset.sversion >= 130000);
6327 
6328  initPQExpBuffer(&buf);
6329 
6331  "SELECT oid, pubname,\n"
6332  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
6333  " puballtables, pubinsert, pubupdate, pubdelete");
6334  if (has_pubtruncate)
6336  ", pubtruncate");
6337  if (has_pubviaroot)
6339  ", pubviaroot");
6341  "\nFROM pg_catalog.pg_publication\n");
6342 
6343  if (!validateSQLNamePattern(&buf, pattern, false, false,
6344  NULL, "pubname", NULL,
6345  NULL,
6346  NULL, 1))
6347  {
6348  termPQExpBuffer(&buf);
6349  return false;
6350  }
6351 
6352  appendPQExpBufferStr(&buf, "ORDER BY 2;");
6353 
6354  res = PSQLexec(buf.data);
6355  if (!res)
6356  {
6357  termPQExpBuffer(&buf);
6358  return false;
6359  }
6360 
6361  if (PQntuples(res) == 0)
6362  {
6363  if (!pset.quiet)
6364  {
6365  if (pattern)
6366  pg_log_error("Did not find any publication named \"%s\".",
6367  pattern);
6368  else
6369  pg_log_error("Did not find any publications.");
6370  }
6371 
6372  termPQExpBuffer(&buf);
6373  PQclear(res);
6374  return false;
6375  }
6376 
6377  for (i = 0; i < PQntuples(res); i++)
6378  {
6379  const char align = 'l';
6380  int ncols = 5;
6381  int nrows = 1;
6382  char *pubid = PQgetvalue(res, i, 0);
6383  char *pubname = PQgetvalue(res, i, 1);
6384  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
6385  printTableOpt myopt = pset.popt.topt;
6386 
6387  if (has_pubtruncate)
6388  ncols++;
6389  if (has_pubviaroot)
6390  ncols++;
6391 
6392  initPQExpBuffer(&title);
6393  printfPQExpBuffer(&title, _("Publication %s"), pubname);
6394  printTableInit(&cont, &myopt, title.data, ncols, nrows);
6395 
6396  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
6397  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
6398  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
6399  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
6400  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
6401  if (has_pubtruncate)
6402  printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
6403  if (has_pubviaroot)
6404  printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
6405 
6406  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
6407  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
6408  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
6409  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
6410  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
6411  if (has_pubtruncate)
6412  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
6413  if (has_pubviaroot)
6414  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
6415 
6416  if (!puballtables)
6417  {
6418  /* Get the tables for the specified publication */
6420  "SELECT n.nspname, c.relname");
6421  if (pset.sversion >= 150000)
6422  {
6424  ", pg_get_expr(pr.prqual, c.oid)");
6426  ", (CASE WHEN pr.prattrs IS NOT NULL THEN\n"
6427  " pg_catalog.array_to_string("
6428  " ARRAY(SELECT attname\n"
6429  " FROM\n"
6430  " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
6431  " pg_catalog.pg_attribute\n"
6432  " WHERE attrelid = c.oid AND attnum = prattrs[s]), ', ')\n"
6433  " ELSE NULL END)");
6434  }
6435  else
6437  ", NULL, NULL");
6439  "\nFROM pg_catalog.pg_class c,\n"
6440  " pg_catalog.pg_namespace n,\n"
6441  " pg_catalog.pg_publication_rel pr\n"
6442  "WHERE c.relnamespace = n.oid\n"
6443  " AND c.oid = pr.prrelid\n"
6444  " AND pr.prpubid = '%s'\n"
6445  "ORDER BY 1,2", pubid);
6446  if (!addFooterToPublicationDesc(&buf, _("Tables:"), false, &cont))
6447  goto error_return;
6448 
6449  if (pset.sversion >= 150000)
6450  {
6451  /* Get the schemas for the specified publication */
6453  "SELECT n.nspname\n"
6454  "FROM pg_catalog.pg_namespace n\n"
6455  " JOIN pg_catalog.pg_publication_namespace pn ON n.oid = pn.pnnspid\n"
6456  "WHERE pn.pnpubid = '%s'\n"
6457  "ORDER BY 1", pubid);
6458  if (!addFooterToPublicationDesc(&buf, _("Tables from schemas:"),
6459  true, &cont))
6460  goto error_return;
6461  }
6462  }
6463 
6464  printTable(&cont, pset.queryFout, false, pset.logfile);
6465  printTableCleanup(&cont);
6466 
6467  termPQExpBuffer(&title);
6468  }
6469 
6470  termPQExpBuffer(&buf);
6471  PQclear(res);
6472 
6473  return true;
6474 
6475 error_return:
6476  printTableCleanup(&cont);
6477  PQclear(res);
6478  termPQExpBuffer(&buf);
6479  termPQExpBuffer(&title);
6480  return false;
6481 }
static bool addFooterToPublicationDesc(PQExpBuffer buf, const char *footermsg, bool as_schema, printTableContent *const cont)
Definition: describe.c:6258

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

◆ describeRoles()

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

Definition at line 3614 of file describe.c.

3615 {
3617  PGresult *res;
3618  printTableContent cont;
3619  printTableOpt myopt = pset.popt.topt;
3620  int ncols = 3;
3621  int nrows = 0;
3622  int i;
3623  int conns;
3624  const char align = 'l';
3625  char **attr;
3626 
3627  myopt.default_footer = false;
3628 
3629  initPQExpBuffer(&buf);
3630 
3632  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3633  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3634  " r.rolconnlimit, r.rolvaliduntil,\n"
3635  " ARRAY(SELECT b.rolname\n"
3636  " FROM pg_catalog.pg_auth_members m\n"
3637  " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
3638  " WHERE m.member = r.oid) as memberof");
3639 
3640  if (verbose)
3641  {
3642  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3643  ncols++;
3644  }
3645  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3646 
3647  if (pset.sversion >= 90500)
3648  {
3649  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3650  }
3651 
3652  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3653 
3654  if (!showSystem && !pattern)
3655  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3656 
3657  if (!validateSQLNamePattern(&buf, pattern, false, false,
3658  NULL, "r.rolname", NULL, NULL,
3659  NULL, 1))
3660  {
3661  termPQExpBuffer(&buf);
3662  return false;
3663  }
3664 
3665  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3666 
3667  res = PSQLexec(buf.data);
3668  if (!res)
3669  return false;
3670 
3671  nrows = PQntuples(res);
3672  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3673 
3674  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3675 
3676  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3677  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3678  /* ignores implicit memberships from superuser & pg_database_owner */
3679  printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
3680 
3681  if (verbose)
3682  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3683 
3684  for (i = 0; i < nrows; i++)
3685  {
3686  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3687 
3689  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3690  add_role_attribute(&buf, _("Superuser"));
3691 
3692  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3693  add_role_attribute(&buf, _("No inheritance"));
3694 
3695  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3696  add_role_attribute(&buf, _("Create role"));
3697 
3698  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3699  add_role_attribute(&buf, _("Create DB"));
3700 
3701  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3702  add_role_attribute(&buf, _("Cannot login"));
3703 
3704  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3705  add_role_attribute(&buf, _("Replication"));
3706 
3707  if (pset.sversion >= 90500)
3708  if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
3709  add_role_attribute(&buf, _("Bypass RLS"));
3710 
3711  conns = atoi(PQgetvalue(res, i, 6));
3712  if (conns >= 0)
3713  {
3714  if (buf.len > 0)
3715  appendPQExpBufferChar(&buf, '\n');
3716 
3717  if (conns == 0)
3718  appendPQExpBufferStr(&buf, _("No connections"));
3719  else
3720  appendPQExpBuffer(&buf, ngettext("%d connection",
3721  "%d connections",
3722  conns),
3723  conns);
3724  }
3725 
3726  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3727  {
3728  if (buf.len > 0)
3729  appendPQExpBufferChar(&buf, '\n');
3730  appendPQExpBufferStr(&buf, _("Password valid until "));
3732  }
3733 
3734  attr[i] = pg_strdup(buf.data);
3735 
3736  printTableAddCell(&cont, attr[i], false, false);
3737 
3738  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3739 
3740  if (verbose)
3741  printTableAddCell(&cont, PQgetvalue(res, i, 9), 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:1171
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
static IsoConnInfo * conns

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

6491 {
6493  PGresult *res;
6494  printQueryOpt myopt = pset.popt;
6495  static const bool translate_columns[] = {false, false, false, false,
6496  false, false, false, false, false, false, false, false};
6497 
6498  if (pset.sversion < 100000)
6499  {
6500  char sverbuf[32];
6501 
6502  pg_log_error("The server (version %s) does not support subscriptions.",
6504  sverbuf, sizeof(sverbuf)));
6505  return true;
6506  }
6507 
6508  initPQExpBuffer(&buf);
6509 
6511  "SELECT subname AS \"%s\"\n"
6512  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
6513  ", subenabled AS \"%s\"\n"
6514  ", subpublications AS \"%s\"\n",
6515  gettext_noop("Name"),
6516  gettext_noop("Owner"),
6517  gettext_noop("Enabled"),
6518  gettext_noop("Publication"));
6519 
6520  if (verbose)
6521  {
6522  /* Binary mode and streaming are only supported in v14 and higher */
6523  if (pset.sversion >= 140000)
6524  {
6526  ", subbinary AS \"%s\"\n",
6527  gettext_noop("Binary"));
6528 
6529  if (pset.sversion >= 160000)
6531  ", (CASE substream\n"
6532  " WHEN 'f' THEN 'off'\n"
6533  " WHEN 't' THEN 'on'\n"
6534  " WHEN 'p' THEN 'parallel'\n"
6535  " END) AS \"%s\"\n",
6536  gettext_noop("Streaming"));
6537  else
6539  ", substream AS \"%s\"\n",
6540  gettext_noop("Streaming"));
6541  }
6542 
6543  /* Two_phase and disable_on_error are only supported in v15 and higher */
6544  if (pset.sversion >= 150000)
6546  ", subtwophasestate AS \"%s\"\n"
6547  ", subdisableonerr AS \"%s\"\n",
6548  gettext_noop("Two-phase commit"),
6549  gettext_noop("Disable on error"));
6550 
6551  if (pset.sversion >= 160000)
6553  ", suborigin AS \"%s\"\n",
6554  gettext_noop("Origin"));
6555 
6557  ", subsynccommit AS \"%s\"\n"
6558  ", subconninfo AS \"%s\"\n",
6559  gettext_noop("Synchronous commit"),
6560  gettext_noop("Conninfo"));
6561 
6562  /* Skip LSN is only supported in v15 and higher */
6563  if (pset.sversion >= 150000)
6565  ", subskiplsn AS \"%s\"\n",
6566  gettext_noop("Skip LSN"));
6567  }
6568 
6569  /* Only display subscriptions in current database. */
6571  "FROM pg_catalog.pg_subscription\n"
6572  "WHERE subdbid = (SELECT oid\n"
6573  " FROM pg_catalog.pg_database\n"
6574  " WHERE datname = pg_catalog.current_database())");
6575 
6576  if (!validateSQLNamePattern(&buf, pattern, true, false,
6577  NULL, "subname", NULL,
6578  NULL,
6579  NULL, 1))
6580  {
6581  termPQExpBuffer(&buf);
6582  return false;
6583  }
6584 
6585  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6586 
6587  res = PSQLexec(buf.data);
6588  termPQExpBuffer(&buf);
6589  if (!res)
6590  return false;
6591 
6592  myopt.nullPrint = NULL;
6593  myopt.title = _("List of subscriptions");
6594  myopt.translate_header = true;
6595  myopt.translate_columns = translate_columns;
6596  myopt.n_translate_columns = lengthof(translate_columns);
6597 
6598  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6599 
6600  PQclear(res);
6601  return true;
6602 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, 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 1446 of file describe.c.

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

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

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

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, _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 619 of file describe.c.

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

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, CppAsString2, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, map_typename_pattern(), printQueryOpt::nullPrint, _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 917 of file describe.c.

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

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, _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 4742 of file describe.c.

4743 {
4745  PGresult *res;
4746  printQueryOpt myopt = pset.popt;
4747  static const bool translate_columns[] = {false, false, false, true, false};
4748 
4749  initPQExpBuffer(&buf);
4750 
4752  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
4753  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n",
4754  gettext_noop("Source type"),
4755  gettext_noop("Target type"));
4756 
4757  /*
4758  * We don't attempt to localize '(binary coercible)' or '(with inout)',
4759  * because there's too much risk of gettext translating a function name
4760  * that happens to match some string in the PO database.
4761  */
4763  " CASE WHEN c.castmethod = '%c' THEN '(binary coercible)'\n"
4764  " WHEN c.castmethod = '%c' THEN '(with inout)'\n"
4765  " ELSE p.proname\n"
4766  " END AS \"%s\",\n",
4767  COERCION_METHOD_BINARY,
4768  COERCION_METHOD_INOUT,
4769  gettext_noop("Function"));
4770 
4772  " CASE WHEN c.castcontext = '%c' THEN '%s'\n"
4773  " WHEN c.castcontext = '%c' THEN '%s'\n"
4774  " ELSE '%s'\n"
4775  " END AS \"%s\"",
4776  COERCION_CODE_EXPLICIT,
4777  gettext_noop("no"),
4778  COERCION_CODE_ASSIGNMENT,
4779  gettext_noop("in assignment"),
4780  gettext_noop("yes"),
4781  gettext_noop("Implicit?"));
4782 
4783  if (verbose)
4785  ",\n d.description AS \"%s\"",
4786  gettext_noop("Description"));
4787 
4788  /*
4789  * We need a left join to pg_proc for binary casts; the others are just
4790  * paranoia.
4791  */
4793  "\nFROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
4794  " ON c.castfunc = p.oid\n"
4795  " LEFT JOIN pg_catalog.pg_type ts\n"
4796  " ON c.castsource = ts.oid\n"
4797  " LEFT JOIN pg_catalog.pg_namespace ns\n"
4798  " ON ns.oid = ts.typnamespace\n"
4799  " LEFT JOIN pg_catalog.pg_type tt\n"
4800  " ON c.casttarget = tt.oid\n"
4801  " LEFT JOIN pg_catalog.pg_namespace nt\n"
4802  " ON nt.oid = tt.typnamespace\n");
4803 
4804  if (verbose)
4806  " LEFT JOIN pg_catalog.pg_description d\n"
4807  " ON d.classoid = c.tableoid AND d.objoid = "
4808  "c.oid AND d.objsubid = 0\n");
4809 
4810  appendPQExpBufferStr(&buf, "WHERE ( (true");
4811 
4812  /*
4813  * Match name pattern against either internal or external name of either
4814  * castsource or casttarget
4815  */
4816  if (!validateSQLNamePattern(&buf, pattern, true, false,
4817  "ns.nspname", "ts.typname",
4818  "pg_catalog.format_type(ts.oid, NULL)",
4819  "pg_catalog.pg_type_is_visible(ts.oid)",
4820  NULL, 3))
4821  goto error_return;
4822 
4823  appendPQExpBufferStr(&buf, ") OR (true");
4824 
4825  if (!validateSQLNamePattern(&buf, pattern, true, false,
4826  "nt.nspname", "tt.typname",
4827  "pg_catalog.format_type(tt.oid, NULL)",
4828  "pg_catalog.pg_type_is_visible(tt.oid)",
4829  NULL, 3))
4830  goto error_return;
4831 
4832  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
4833 
4834  res = PSQLexec(buf.data);
4835  termPQExpBuffer(&buf);
4836  if (!res)
4837  return false;
4838 
4839  myopt.nullPrint = NULL;
4840  myopt.title = _("List of casts");
4841  myopt.translate_header = true;
4842  myopt.translate_columns = translate_columns;
4843  myopt.n_translate_columns = lengthof(translate_columns);
4844 
4845  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4846 
4847  PQclear(res);
4848  return true;
4849 
4850 error_return:
4851  termPQExpBuffer(&buf);
4852  return false;
4853 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, _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 4861 of file describe.c.

4862 {
4864  PGresult *res;
4865  printQueryOpt myopt = pset.popt;
4866  static const bool translate_columns[] = {false, false, false, false, false, false, false, true, false};
4867 
4868  initPQExpBuffer(&buf);
4869 
4871  "SELECT\n"
4872  " n.nspname AS \"%s\",\n"
4873  " c.collname AS \"%s\",\n",
4874  gettext_noop("Schema"),
4875  gettext_noop("Name"));
4876 
4877  if (pset.sversion >= 100000)
4879  " CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\",\n",
4880  gettext_noop("Provider"));
4881  else
4883  " 'libc' AS \"%s\",\n",
4884  gettext_noop("Provider"));
4885 
4887  " c.collcollate AS \"%s\",\n"
4888  " c.collctype AS \"%s\",\n",
4889  gettext_noop("Collate"),
4890  gettext_noop("Ctype"));
4891 
4892  if (pset.sversion >= 150000)
4894  " c.colliculocale AS \"%s\",\n",
4895  gettext_noop("ICU Locale"));
4896  else
4898  " c.collcollate AS \"%s\",\n",
4899  gettext_noop("ICU Locale"));
4900 
4901  if (pset.sversion >= 160000)
4903  " c.collicurules AS \"%s\",\n",
4904  gettext_noop("ICU Rules"));
4905  else
4907  " NULL AS \"%s\",\n",
4908  gettext_noop("ICU Rules"));
4909 
4910  if (pset.sversion >= 120000)
4912  " CASE WHEN c.collisdeterministic THEN '%s' ELSE '%s' END AS \"%s\"",
4913  gettext_noop("yes"), gettext_noop("no"),
4914  gettext_noop("Deterministic?"));
4915  else
4917  " '%s' AS \"%s\"",
4918  gettext_noop("yes"),
4919  gettext_noop("Deterministic?"));
4920 
4921  if (verbose)
4923  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
4924  gettext_noop("Description"));
4925 
4927  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
4928  "WHERE n.oid = c.collnamespace\n");
4929 
4930  if (!showSystem && !pattern)
4931  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4932  " AND n.nspname <> 'information_schema'\n");
4933 
4934  /*
4935  * Hide collations that aren't usable in the current database's encoding.
4936  * If you think to change this, note that pg_collation_is_visible rejects
4937  * unusable collations, so you will need to hack name pattern processing
4938  * somehow to avoid inconsistent behavior.
4939  */
4940  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
4941 
4942  if (!validateSQLNamePattern(&buf, pattern, true, false,
4943  "n.nspname", "c.collname", NULL,
4944  "pg_catalog.pg_collation_is_visible(c.oid)",
4945  NULL, 3))
4946  {
4947  termPQExpBuffer(&buf);
4948  return false;
4949  }
4950 
4951  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4952 
4953  res = PSQLexec(buf.data);
4954  termPQExpBuffer(&buf);
4955  if (!res)
4956  return false;
4957 
4958  myopt.nullPrint = NULL;
4959  myopt.title = _("List of collations");
4960  myopt.translate_header = true;
4961  myopt.translate_columns = translate_columns;
4962  myopt.n_translate_columns = lengthof(translate_columns);
4963 
4964  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4965 
4966  PQclear(res);
4967  return true;
4968 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, _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 4414 of file describe.c.

4415 {
4417  PGresult *res;
4418  printQueryOpt myopt = pset.popt;
4419  static const bool translate_columns[] =
4420  {false, false, false, false, true, false};
4421 
4422  initPQExpBuffer(&buf);
4423 
4425  "SELECT n.nspname AS \"%s\",\n"
4426  " c.conname AS \"%s\",\n"
4427  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
4428  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
4429  " CASE WHEN c.condefault THEN '%s'\n"
4430  " ELSE '%s' END AS \"%s\"",
4431  gettext_noop("Schema"),
4432  gettext_noop("Name"),
4433  gettext_noop("Source"),
4434  gettext_noop("Destination"),
4435  gettext_noop("yes"), gettext_noop("no"),
4436  gettext_noop("Default?"));
4437 
4438  if (verbose)
4440  ",\n d.description AS \"%s\"",
4441  gettext_noop("Description"));
4442 
4444  "\nFROM pg_catalog.pg_conversion c\n"
4445  " JOIN pg_catalog.pg_namespace n "
4446  "ON n.oid = c.connamespace\n");
4447 
4448  if (verbose)
4450  "LEFT JOIN pg_catalog.pg_description d "
4451  "ON d.classoid = c.tableoid\n"
4452  " AND d.objoid = c.oid "
4453  "AND d.objsubid = 0\n");
4454 
4455  appendPQExpBufferStr(&buf, "WHERE true\n");
4456 
4457  if (!showSystem && !pattern)
4458  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4459  " AND n.nspname <> 'information_schema'\n");
4460 
4461  if (!validateSQLNamePattern(&buf, pattern, true, false,
4462  "n.nspname", "c.conname", NULL,
4463  "pg_catalog.pg_conversion_is_visible(c.oid)",
4464  NULL, 3))
4465  {
4466  termPQExpBuffer(&buf);
4467  return false;
4468  }
4469 
4470  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4471 
4472  res = PSQLexec(buf.data);
4473  termPQExpBuffer(&buf);
4474  if (!res)
4475  return false;
4476 
4477  myopt.nullPrint = NULL;
4478  myopt.title = _("List of conversions");
4479  myopt.translate_header = true;
4480  myopt.translate_columns = translate_columns;
4481  myopt.n_translate_columns = lengthof(translate_columns);
4482 
4483  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4484 
4485  PQclear(res);
4486  return true;
4487 }

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

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

Referenced by exec_command_d().

◆ listDefaultACLs()

bool listDefaultACLs ( const char *  pattern)

Definition at line 1174 of file describe.c.

1175 {
1177  PGresult *res;
1178  printQueryOpt myopt = pset.popt;
1179  static const bool translate_columns[] = {false, false, true, false};
1180 
1181  initPQExpBuffer(&buf);
1182 
1184  "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1185  " n.nspname AS \"%s\",\n"
1186  " 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"
1187  " ",
1188  gettext_noop("Owner"),
1189  gettext_noop("Schema"),
1190  DEFACLOBJ_RELATION,
1191  gettext_noop("table"),
1192  DEFACLOBJ_SEQUENCE,
1193  gettext_noop("sequence"),
1194  DEFACLOBJ_FUNCTION,
1195  gettext_noop("function"),
1196  DEFACLOBJ_TYPE,
1197  gettext_noop("type"),
1198  DEFACLOBJ_NAMESPACE,
1199  gettext_noop("schema"),
1200  gettext_noop("Type"));
1201 
1202  printACLColumn(&buf, "d.defaclacl");
1203 
1204  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1205  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1206 
1207  if (!validateSQLNamePattern(&buf, pattern, false, false,
1208  NULL,
1209  "n.nspname",
1210  "pg_catalog.pg_get_userbyid(d.defaclrole)",
1211  NULL,
1212  NULL, 3))
1213  goto error_return;
1214 
1215  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1216 
1217  res = PSQLexec(buf.data);
1218  if (!res)
1219  goto error_return;
1220 
1221  myopt.nullPrint = NULL;
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, printQueryOpt::nullPrint, _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 4330 of file describe.c.

4331 {
4333  PGresult *res;
4334  printQueryOpt myopt = pset.popt;
4335 
4336  initPQExpBuffer(&buf);
4337 
4339  "SELECT n.nspname as \"%s\",\n"
4340  " t.typname as \"%s\",\n"
4341  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
4342  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
4343  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n"
4344  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
4345  " t.typdefault as \"%s\",\n"
4346  " pg_catalog.array_to_string(ARRAY(\n"
4347  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
4348  " ), ' ') as \"%s\"",
4349  gettext_noop("Schema"),
4350  gettext_noop("Name"),
4351  gettext_noop("Type"),
4352  gettext_noop("Collation"),
4353  gettext_noop("Nullable"),
4354  gettext_noop("Default"),
4355  gettext_noop("Check"));
4356 
4357  if (verbose)
4358  {
4359  appendPQExpBufferStr(&buf, ",\n ");
4360  printACLColumn(&buf, "t.typacl");
4362  ",\n d.description as \"%s\"",
4363  gettext_noop("Description"));
4364  }
4365 
4367  "\nFROM pg_catalog.pg_type t\n"
4368  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
4369 
4370  if (verbose)
4372  " LEFT JOIN pg_catalog.pg_description d "
4373  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
4374  "AND d.objsubid = 0\n");
4375 
4376  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
4377 
4378  if (!showSystem && !pattern)
4379  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4380  " AND n.nspname <> 'information_schema'\n");
4381 
4382  if (!validateSQLNamePattern(&buf, pattern, true, false,
4383  "n.nspname", "t.typname", NULL,
4384  "pg_catalog.pg_type_is_visible(t.oid)",
4385  NULL, 3))
4386  {
4387  termPQExpBuffer(&buf);
4388  return false;
4389  }
4390 
4391  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4392 
4393  res = PSQLexec(buf.data);
4394  termPQExpBuffer(&buf);
4395  if (!res)
4396  return false;
4397 
4398  myopt.nullPrint = NULL;
4399  myopt.title = _("List of domains");
4400  myopt.translate_header = true;
4401 
4402  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4403 
4404  PQclear(res);
4405  return true;
4406 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, _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 4564 of file describe.c.

4565 {
4567  PGresult *res;
4568  printQueryOpt myopt = pset.popt;
4569  static const bool translate_columns[] =
4570  {false, false, false, true, false, false, false};
4571 
4572  if (pset.sversion < 90300)
4573  {
4574  char sverbuf[32];
4575 
4576  pg_log_error("The server (version %s) does not support event triggers.",
4578  sverbuf, sizeof(sverbuf)));
4579  return true;
4580  }
4581 
4582  initPQExpBuffer(&buf);
4583 
4585  "SELECT evtname as \"%s\", "
4586  "evtevent as \"%s\", "
4587  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
4588  " case evtenabled when 'O' then '%s'"
4589  " when 'R' then '%s'"
4590  " when 'A' then '%s'"
4591  " when 'D' then '%s' end as \"%s\",\n"
4592  " e.evtfoid::pg_catalog.regproc as \"%s\", "
4593  "pg_catalog.array_to_string(array(select x"
4594  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
4595  gettext_noop("Name"),
4596  gettext_noop("Event"),
4597  gettext_noop("Owner"),
4598  gettext_noop("enabled"),
4599  gettext_noop("replica"),
4600  gettext_noop("always"),
4601  gettext_noop("disabled"),
4602  gettext_noop("Enabled"),
4603  gettext_noop("Function"),
4604  gettext_noop("Tags"));
4605  if (verbose)
4607  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
4608  gettext_noop("Description"));
4610  "\nFROM pg_catalog.pg_event_trigger e ");
4611 
4612  if (!validateSQLNamePattern(&buf, pattern, false, false,
4613  NULL, "evtname", NULL, NULL,
4614  NULL, 1))
4615  {
4616  termPQExpBuffer(&buf);
4617  return false;
4618  }
4619 
4620  appendPQExpBufferStr(&buf, "ORDER BY 1");
4621 
4622  res = PSQLexec(buf.data);
4623  termPQExpBuffer(&buf);
4624  if (!res)
4625  return false;
4626 
4627  myopt.nullPrint = NULL;
4628  myopt.title = _("List of event triggers");
4629  myopt.translate_header = true;
4630  myopt.translate_columns = translate_columns;
4631  myopt.n_translate_columns = lengthof(translate_columns);
4632 
4633  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4634 
4635  PQclear(res);
4636  return true;
4637 }

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, 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 4645 of file describe.c.

4646 {
4648  PGresult *res;
4649  printQueryOpt myopt = pset.popt;
4650 
4651  if (pset.sversion < 100000)
4652  {
4653  char sverbuf[32];
4654 
4655  pg_log_error("The server (version %s) does not support extended statistics.",
4657  sverbuf, sizeof(sverbuf)));
4658  return true;
4659  }
4660 
4661  initPQExpBuffer(&buf);
4663  "SELECT \n"
4664  "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text AS \"%s\", \n"
4665  "es.stxname AS \"%s\", \n",
4666  gettext_noop("Schema"),
4667  gettext_noop("Name"));
4668 
4669  if (pset.sversion >= 140000)
4671  "pg_catalog.format('%%s FROM %%s', \n"
4672  " pg_catalog.pg_get_statisticsobjdef_columns(es.oid), \n"
4673  " es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4674  gettext_noop("Definition"));
4675  else
4677  "pg_catalog.format('%%s FROM %%s', \n"
4678  " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(a.attname),', ') \n"
4679  " FROM pg_catalog.unnest(es.stxkeys) s(attnum) \n"
4680  " JOIN pg_catalog.pg_attribute a \n"
4681  " ON (es.stxrelid = a.attrelid \n"
4682  " AND a.attnum = s.attnum \n"
4683  " AND NOT a.attisdropped)), \n"
4684  "es.stxrelid::pg_catalog.regclass) AS \"%s\"",
4685  gettext_noop("Definition"));
4686 
4688  ",\nCASE WHEN 'd' = any(es.stxkind) THEN 'defined' \n"
4689  "END AS \"%s\", \n"
4690  "CASE WHEN 'f' = any(es.stxkind) THEN 'defined' \n"
4691  "END AS \"%s\"",
4692  gettext_noop("Ndistinct"),
4693  gettext_noop("Dependencies"));
4694 
4695  /*
4696  * Include the MCV statistics kind.
4697  */
4698  if (pset.sversion >= 120000)
4699  {
4701  ",\nCASE WHEN 'm' = any(es.stxkind) THEN 'defined' \n"
4702  "END AS \"%s\" ",
4703  gettext_noop("MCV"));
4704  }
4705 
4707  " \nFROM pg_catalog.pg_statistic_ext es \n");
4708 
4709  if (!validateSQLNamePattern(&buf, pattern,
4710  false, false,
4711  "es.stxnamespace::pg_catalog.regnamespace::pg_catalog.text", "es.stxname",
4712  NULL, "pg_catalog.pg_statistics_obj_is_visible(es.oid)",
4713  NULL, 3))
4714  {
4715  termPQExpBuffer(&buf);
4716  return false;
4717  }
4718 
4719  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4720 
4721  res = PSQLexec(buf.data);
4722  termPQExpBuffer(&buf);
4723  if (!res)
4724  return false;
4725 
4726  myopt.nullPrint = NULL;
4727  myopt.title = _("List of extended statistics");
4728  myopt.translate_header = true;
4729 
4730  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);