PostgreSQL Source Code  git master
describe.c File Reference
#include "postgres_fe.h"
#include <ctype.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 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)
 
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 *pattern, bool verbose, bool showSystem)
 
bool describeTypes (const char *pattern, bool verbose, bool showSystem)
 
bool describeOperators (const char *pattern, bool verbose, bool showSystem)
 
bool listAllDbs (const char *pattern, bool verbose)
 
bool permissionsList (const char *pattern)
 
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 listEventTriggers (const char *pattern, bool verbose)
 
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)
 
bool describePublications (const char *pattern)
 
bool describeSubscriptions (const char *pattern, bool verbose)
 

Function Documentation

◆ add_role_attribute()

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

Definition at line 3557 of file describe.c.

References appendPQExpBufferStr(), and PQExpBufferData::len.

Referenced by describeRoles().

3558 {
3559  if (buf->len > 0)
3560  appendPQExpBufferStr(buf, ", ");
3561 
3562  appendPQExpBufferStr(buf, str);
3563 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369

◆ add_tablespace_footer()

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

Definition at line 3332 of file describe.c.

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

Referenced by describeOneTableDetails().

3334 {
3335  /* relkinds for which we support tablespaces */
3336  if (relkind == RELKIND_RELATION ||
3337  relkind == RELKIND_MATVIEW ||
3338  relkind == RELKIND_INDEX ||
3339  relkind == RELKIND_PARTITIONED_TABLE ||
3340  relkind == RELKIND_PARTITIONED_INDEX ||
3341  relkind == RELKIND_TOASTVALUE)
3342  {
3343  /*
3344  * We ignore the database default tablespace so that users not using
3345  * tablespaces don't need to know about them. This case also covers
3346  * pre-8.0 servers, for which tablespace will always be 0.
3347  */
3348  if (tablespace != 0)
3349  {
3350  PGresult *result = NULL;
3352 
3353  initPQExpBuffer(&buf);
3354  printfPQExpBuffer(&buf,
3355  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3356  "WHERE oid = '%u';", tablespace);
3357  result = PSQLexec(buf.data);
3358  if (!result)
3359  {
3360  termPQExpBuffer(&buf);
3361  return;
3362  }
3363  /* Should always be the case, but.... */
3364  if (PQntuples(result) > 0)
3365  {
3366  if (newline)
3367  {
3368  /* Add the tablespace as a new footer */
3369  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3370  PQgetvalue(result, 0, 0));
3371  printTableAddFooter(cont, buf.data);
3372  }
3373  else
3374  {
3375  /* Append the tablespace to the latest footer */
3376  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3377 
3378  /*-------
3379  translator: before this string there's an index description like
3380  '"foo_pkey" PRIMARY KEY, btree (a)' */
3381  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3382  PQgetvalue(result, 0, 0));
3383  printTableSetFooter(cont, buf.data);
3384  }
3385  }
3386  PQclear(result);
3387  termPQExpBuffer(&buf);
3388  }
3389  }
3390 }
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3224
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
static chr newline(void)
Definition: regc_lex.c:1138
char relkind
Definition: pg_class.h:81
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
char * tablespace
Definition: pgbench.c:189
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3199
printTableFooter * footer
Definition: print.h:159
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * data
Definition: print.h:137
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeAccessMethods()

bool describeAccessMethods ( const char *  pattern,
bool  verbose 
)

Definition at line 148 of file describe.c.

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

Referenced by exec_command_d().

149 {
151  PGresult *res;
152  printQueryOpt myopt = pset.popt;
153  static const bool translate_columns[] = {false, true, false, false};
154 
155  if (pset.sversion < 90600)
156  {
157  char sverbuf[32];
158 
159  pg_log_error("The server (version %s) does not support access methods.",
161  sverbuf, sizeof(sverbuf)));
162  return true;
163  }
164 
165  initPQExpBuffer(&buf);
166 
167  printfPQExpBuffer(&buf,
168  "SELECT amname AS \"%s\",\n"
169  " CASE amtype"
170  " WHEN 'i' THEN '%s'"
171  " WHEN 't' THEN '%s'"
172  " END AS \"%s\"",
173  gettext_noop("Name"),
174  gettext_noop("Index"),
175  gettext_noop("Table"),
176  gettext_noop("Type"));
177 
178  if (verbose)
179  {
180  appendPQExpBuffer(&buf,
181  ",\n amhandler AS \"%s\",\n"
182  " pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
183  gettext_noop("Handler"),
184  gettext_noop("Description"));
185  }
186 
188  "\nFROM pg_catalog.pg_am\n");
189 
190  processSQLNamePattern(pset.db, &buf, pattern, false, false,
191  NULL, "amname", NULL,
192  NULL);
193 
194  appendPQExpBufferStr(&buf, "ORDER BY 1;");
195 
196  res = PSQLexec(buf.data);
197  termPQExpBuffer(&buf);
198  if (!res)
199  return false;
200 
201  myopt.nullPrint = NULL;
202  myopt.title = _("List of access methods");
203  myopt.translate_header = true;
204  myopt.translate_columns = translate_columns;
205  myopt.n_translate_columns = lengthof(translate_columns);
206 
207  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
208 
209  PQclear(res);
210  return true;
211 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeAggregates()

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

Definition at line 62 of file describe.c.

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

Referenced by exec_command_d().

63 {
65  PGresult *res;
66  printQueryOpt myopt = pset.popt;
67 
68  initPQExpBuffer(&buf);
69 
70  printfPQExpBuffer(&buf,
71  "SELECT n.nspname as \"%s\",\n"
72  " p.proname AS \"%s\",\n"
73  " pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
74  gettext_noop("Schema"),
75  gettext_noop("Name"),
76  gettext_noop("Result data type"));
77 
78  if (pset.sversion >= 80400)
79  appendPQExpBuffer(&buf,
80  " CASE WHEN p.pronargs = 0\n"
81  " THEN CAST('*' AS pg_catalog.text)\n"
82  " ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"
83  " END AS \"%s\",\n",
84  gettext_noop("Argument data types"));
85  else if (pset.sversion >= 80200)
86  appendPQExpBuffer(&buf,
87  " CASE WHEN p.pronargs = 0\n"
88  " THEN CAST('*' AS pg_catalog.text)\n"
89  " ELSE\n"
90  " pg_catalog.array_to_string(ARRAY(\n"
91  " SELECT\n"
92  " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
93  " FROM\n"
94  " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
95  " ), ', ')\n"
96  " END AS \"%s\",\n",
97  gettext_noop("Argument data types"));
98  else
99  appendPQExpBuffer(&buf,
100  " pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
101  gettext_noop("Argument data types"));
102 
103  if (pset.sversion >= 110000)
104  appendPQExpBuffer(&buf,
105  " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
106  "FROM pg_catalog.pg_proc p\n"
107  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
108  "WHERE p.prokind = 'a'\n",
109  gettext_noop("Description"));
110  else
111  appendPQExpBuffer(&buf,
112  " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
113  "FROM pg_catalog.pg_proc p\n"
114  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
115  "WHERE p.proisagg\n",
116  gettext_noop("Description"));
117 
118  if (!showSystem && !pattern)
119  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
120  " AND n.nspname <> 'information_schema'\n");
121 
122  processSQLNamePattern(pset.db, &buf, pattern, true, false,
123  "n.nspname", "p.proname", NULL,
124  "pg_catalog.pg_function_is_visible(p.oid)");
125 
126  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
127 
128  res = PSQLexec(buf.data);
129  termPQExpBuffer(&buf);
130  if (!res)
131  return false;
132 
133  myopt.nullPrint = NULL;
134  myopt.title = _("List of aggregate functions");
135  myopt.translate_header = true;
136 
137  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
138 
139  PQclear(res);
140  return true;
141 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeFunctions()

bool describeFunctions ( const char *  functypes,
const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 314 of file describe.c.

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

Referenced by exec_command_d().

315 {
316  bool showAggregate = strchr(functypes, 'a') != NULL;
317  bool showNormal = strchr(functypes, 'n') != NULL;
318  bool showProcedure = strchr(functypes, 'p') != NULL;
319  bool showTrigger = strchr(functypes, 't') != NULL;
320  bool showWindow = strchr(functypes, 'w') != NULL;
321  bool have_where;
323  PGresult *res;
324  printQueryOpt myopt = pset.popt;
325  static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, false, false, false, false};
326 
327  /* No "Parallel" column before 9.6 */
328  static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, false, false, false, false};
329 
330  if (strlen(functypes) != strspn(functypes, "anptwS+"))
331  {
332  pg_log_error("\\df only takes [anptwS+] as options");
333  return true;
334  }
335 
336  if (showProcedure && pset.sversion < 110000)
337  {
338  char sverbuf[32];
339 
340  pg_log_error("\\df does not take a \"%c\" option with server version %s",
341  'p',
343  sverbuf, sizeof(sverbuf)));
344  return true;
345  }
346 
347  if (showWindow && pset.sversion < 80400)
348  {
349  char sverbuf[32];
350 
351  pg_log_error("\\df does not take a \"%c\" option with server version %s",
352  'w',
354  sverbuf, sizeof(sverbuf)));
355  return true;
356  }
357 
358  if (!showAggregate && !showNormal && !showProcedure && !showTrigger && !showWindow)
359  {
360  showAggregate = showNormal = showTrigger = true;
361  if (pset.sversion >= 110000)
362  showProcedure = true;
363  if (pset.sversion >= 80400)
364  showWindow = true;
365  }
366 
367  initPQExpBuffer(&buf);
368 
369  printfPQExpBuffer(&buf,
370  "SELECT n.nspname as \"%s\",\n"
371  " p.proname as \"%s\",\n",
372  gettext_noop("Schema"),
373  gettext_noop("Name"));
374 
375  if (pset.sversion >= 110000)
376  appendPQExpBuffer(&buf,
377  " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
378  " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
379  " CASE p.prokind\n"
380  " WHEN 'a' THEN '%s'\n"
381  " WHEN 'w' THEN '%s'\n"
382  " WHEN 'p' THEN '%s'\n"
383  " ELSE '%s'\n"
384  " END as \"%s\"",
385  gettext_noop("Result data type"),
386  gettext_noop("Argument data types"),
387  /* translator: "agg" is short for "aggregate" */
388  gettext_noop("agg"),
389  gettext_noop("window"),
390  gettext_noop("proc"),
391  gettext_noop("func"),
392  gettext_noop("Type"));
393  else if (pset.sversion >= 80400)
394  appendPQExpBuffer(&buf,
395  " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
396  " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
397  " CASE\n"
398  " WHEN p.proisagg THEN '%s'\n"
399  " WHEN p.proiswindow THEN '%s'\n"
400  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
401  " ELSE '%s'\n"
402  " END as \"%s\"",
403  gettext_noop("Result data type"),
404  gettext_noop("Argument data types"),
405  /* translator: "agg" is short for "aggregate" */
406  gettext_noop("agg"),
407  gettext_noop("window"),
408  gettext_noop("trigger"),
409  gettext_noop("func"),
410  gettext_noop("Type"));
411  else if (pset.sversion >= 80100)
412  appendPQExpBuffer(&buf,
413  " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
414  " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
415  " CASE WHEN proallargtypes IS NOT NULL THEN\n"
416  " pg_catalog.array_to_string(ARRAY(\n"
417  " SELECT\n"
418  " CASE\n"
419  " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
420  " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
421  " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
422  " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
423  " END ||\n"
424  " CASE\n"
425  " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
426  " ELSE p.proargnames[s.i] || ' '\n"
427  " END ||\n"
428  " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
429  " FROM\n"
430  " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
431  " ), ', ')\n"
432  " ELSE\n"
433  " pg_catalog.array_to_string(ARRAY(\n"
434  " SELECT\n"
435  " CASE\n"
436  " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
437  " ELSE p.proargnames[s.i+1] || ' '\n"
438  " END ||\n"
439  " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
440  " FROM\n"
441  " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
442  " ), ', ')\n"
443  " END AS \"%s\",\n"
444  " CASE\n"
445  " WHEN p.proisagg THEN '%s'\n"
446  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
447  " ELSE '%s'\n"
448  " END AS \"%s\"",
449  gettext_noop("Result data type"),
450  gettext_noop("Argument data types"),
451  /* translator: "agg" is short for "aggregate" */
452  gettext_noop("agg"),
453  gettext_noop("trigger"),
454  gettext_noop("func"),
455  gettext_noop("Type"));
456  else
457  appendPQExpBuffer(&buf,
458  " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
459  " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
460  " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
461  " CASE\n"
462  " WHEN p.proisagg THEN '%s'\n"
463  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
464  " ELSE '%s'\n"
465  " END AS \"%s\"",
466  gettext_noop("Result data type"),
467  gettext_noop("Argument data types"),
468  /* translator: "agg" is short for "aggregate" */
469  gettext_noop("agg"),
470  gettext_noop("trigger"),
471  gettext_noop("func"),
472  gettext_noop("Type"));
473 
474  if (verbose)
475  {
476  appendPQExpBuffer(&buf,
477  ",\n CASE\n"
478  " WHEN p.provolatile = 'i' THEN '%s'\n"
479  " WHEN p.provolatile = 's' THEN '%s'\n"
480  " WHEN p.provolatile = 'v' THEN '%s'\n"
481  " END as \"%s\"",
482  gettext_noop("immutable"),
483  gettext_noop("stable"),
484  gettext_noop("volatile"),
485  gettext_noop("Volatility"));
486  if (pset.sversion >= 90600)
487  appendPQExpBuffer(&buf,
488  ",\n CASE\n"
489  " WHEN p.proparallel = 'r' THEN '%s'\n"
490  " WHEN p.proparallel = 's' THEN '%s'\n"
491  " WHEN p.proparallel = 'u' THEN '%s'\n"
492  " END as \"%s\"",
493  gettext_noop("restricted"),
494  gettext_noop("safe"),
495  gettext_noop("unsafe"),
496  gettext_noop("Parallel"));
497  appendPQExpBuffer(&buf,
498  ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
499  ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\"",
500  gettext_noop("Owner"),
501  gettext_noop("definer"),
502  gettext_noop("invoker"),
503  gettext_noop("Security"));
504  appendPQExpBufferStr(&buf, ",\n ");
505  printACLColumn(&buf, "p.proacl");
506  appendPQExpBuffer(&buf,
507  ",\n l.lanname as \"%s\""
508  ",\n p.prosrc as \"%s\""
509  ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
510  gettext_noop("Language"),
511  gettext_noop("Source code"),
512  gettext_noop("Description"));
513  }
514 
516  "\nFROM pg_catalog.pg_proc p"
517  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
518 
519  if (verbose)
521  " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
522 
523  have_where = false;
524 
525  /* filter by function type, if requested */
526  if (showNormal && showAggregate && showProcedure && showTrigger && showWindow)
527  /* Do nothing */ ;
528  else if (showNormal)
529  {
530  if (!showAggregate)
531  {
532  if (have_where)
533  appendPQExpBufferStr(&buf, " AND ");
534  else
535  {
536  appendPQExpBufferStr(&buf, "WHERE ");
537  have_where = true;
538  }
539  if (pset.sversion >= 110000)
540  appendPQExpBufferStr(&buf, "p.prokind <> 'a'\n");
541  else
542  appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
543  }
544  if (!showProcedure && pset.sversion >= 110000)
545  {
546  if (have_where)
547  appendPQExpBufferStr(&buf, " AND ");
548  else
549  {
550  appendPQExpBufferStr(&buf, "WHERE ");
551  have_where = true;
552  }
553  appendPQExpBufferStr(&buf, "p.prokind <> 'p'\n");
554  }
555  if (!showTrigger)
556  {
557  if (have_where)
558  appendPQExpBufferStr(&buf, " AND ");
559  else
560  {
561  appendPQExpBufferStr(&buf, "WHERE ");
562  have_where = true;
563  }
564  appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
565  }
566  if (!showWindow && pset.sversion >= 80400)
567  {
568  if (have_where)
569  appendPQExpBufferStr(&buf, " AND ");
570  else
571  {
572  appendPQExpBufferStr(&buf, "WHERE ");
573  have_where = true;
574  }
575  if (pset.sversion >= 110000)
576  appendPQExpBufferStr(&buf, "p.prokind <> 'w'\n");
577  else
578  appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
579  }
580  }
581  else
582  {
583  bool needs_or = false;
584 
585  appendPQExpBufferStr(&buf, "WHERE (\n ");
586  have_where = true;
587  /* Note: at least one of these must be true ... */
588  if (showAggregate)
589  {
590  if (pset.sversion >= 110000)
591  appendPQExpBufferStr(&buf, "p.prokind = 'a'\n");
592  else
593  appendPQExpBufferStr(&buf, "p.proisagg\n");
594  needs_or = true;
595  }
596  if (showTrigger)
597  {
598  if (needs_or)
599  appendPQExpBufferStr(&buf, " OR ");
601  "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
602  needs_or = true;
603  }
604  if (showProcedure)
605  {
606  if (needs_or)
607  appendPQExpBufferStr(&buf, " OR ");
608  appendPQExpBufferStr(&buf, "p.prokind = 'p'\n");
609  needs_or = true;
610  }
611  if (showWindow)
612  {
613  if (needs_or)
614  appendPQExpBufferStr(&buf, " OR ");
615  if (pset.sversion >= 110000)
616  appendPQExpBufferStr(&buf, "p.prokind = 'w'\n");
617  else
618  appendPQExpBufferStr(&buf, "p.proiswindow\n");
619  needs_or = true;
620  }
621  appendPQExpBufferStr(&buf, " )\n");
622  }
623 
624  processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
625  "n.nspname", "p.proname", NULL,
626  "pg_catalog.pg_function_is_visible(p.oid)");
627 
628  if (!showSystem && !pattern)
629  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
630  " AND n.nspname <> 'information_schema'\n");
631 
632  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
633 
634  res = PSQLexec(buf.data);
635  termPQExpBuffer(&buf);
636  if (!res)
637  return false;
638 
639  myopt.nullPrint = NULL;
640  myopt.title = _("List of functions");
641  myopt.translate_header = true;
642  if (pset.sversion >= 90600)
643  {
644  myopt.translate_columns = translate_columns;
645  myopt.n_translate_columns = lengthof(translate_columns);
646  }
647  else
648  {
649  myopt.translate_columns = translate_columns_pre_96;
650  myopt.n_translate_columns = lengthof(translate_columns_pre_96);
651  }
652 
653  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
654 
655  PQclear(res);
656  return true;
657 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6007
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeOneTableDetails()

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

Definition at line 1444 of file describe.c.

References _, add_tablespace_footer(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), Assert, atooid, buf, CppAsString2, PQExpBufferData::data, printTableOpt::default_footer, _psqlSettings::encoding, printTableOpt::expanded, fmtId(), printQueryOpt::footers, free, gettext_noop, _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, relkind, relname, relpersistence, relreplident, resetPQExpBuffer(), _psqlSettings::sversion, tablespace, termPQExpBuffer(), printQueryOpt::title, tmpbuf, printQueryOpt::topt, and printQueryOpt::translate_header.

Referenced by describeTableDetails().

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

◆ describeOneTSConfig()

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

Definition at line 5158 of file describe.c.

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, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::topt, and printQueryOpt::translate_header.

Referenced by listTSConfigsVerbose().

5160 {
5162  title;
5163  PGresult *res;
5164  printQueryOpt myopt = pset.popt;
5165 
5166  initPQExpBuffer(&buf);
5167 
5168  printfPQExpBuffer(&buf,
5169  "SELECT\n"
5170  " ( SELECT t.alias FROM\n"
5171  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
5172  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
5173  " pg_catalog.btrim(\n"
5174  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
5175  " FROM pg_catalog.pg_ts_config_map AS mm\n"
5176  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
5177  " ORDER BY mapcfg, maptokentype, mapseqno\n"
5178  " ) :: pg_catalog.text,\n"
5179  " '{}') AS \"%s\"\n"
5180  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
5181  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
5182  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
5183  "ORDER BY 1;",
5184  gettext_noop("Token"),
5185  gettext_noop("Dictionaries"),
5186  oid);
5187 
5188  res = PSQLexec(buf.data);
5189  termPQExpBuffer(&buf);
5190  if (!res)
5191  return false;
5192 
5193  initPQExpBuffer(&title);
5194 
5195  if (nspname)
5196  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
5197  nspname, cfgname);
5198  else
5199  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
5200  cfgname);
5201 
5202  if (pnspname)
5203  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
5204  pnspname, prsname);
5205  else
5206  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
5207  prsname);
5208 
5209  myopt.nullPrint = NULL;
5210  myopt.title = title.data;
5211  myopt.footers = NULL;
5212  myopt.topt.default_footer = false;
5213  myopt.translate_header = true;
5214 
5215  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5216 
5217  termPQExpBuffer(&title);
5218 
5219  PQclear(res);
5220  return true;
5221 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
printTableOpt topt
Definition: print.h:167
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
char ** footers
Definition: print.h:170
bool default_footer
Definition: print.h:111
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeOneTSParser()

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

Definition at line 4763 of file describe.c.

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, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::topt, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by listTSParsersVerbose().

4764 {
4766  PGresult *res;
4767  PQExpBufferData title;
4768  printQueryOpt myopt = pset.popt;
4769  static const bool translate_columns[] = {true, false, false};
4770 
4771  initPQExpBuffer(&buf);
4772 
4773  printfPQExpBuffer(&buf,
4774  "SELECT '%s' AS \"%s\",\n"
4775  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4776  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4777  " FROM pg_catalog.pg_ts_parser p\n"
4778  " WHERE p.oid = '%s'\n"
4779  "UNION ALL\n"
4780  "SELECT '%s',\n"
4781  " p.prstoken::pg_catalog.regproc,\n"
4782  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4783  " FROM pg_catalog.pg_ts_parser p\n"
4784  " WHERE p.oid = '%s'\n"
4785  "UNION ALL\n"
4786  "SELECT '%s',\n"
4787  " p.prsend::pg_catalog.regproc,\n"
4788  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4789  " FROM pg_catalog.pg_ts_parser p\n"
4790  " WHERE p.oid = '%s'\n"
4791  "UNION ALL\n"
4792  "SELECT '%s',\n"
4793  " p.prsheadline::pg_catalog.regproc,\n"
4794  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4795  " FROM pg_catalog.pg_ts_parser p\n"
4796  " WHERE p.oid = '%s'\n"
4797  "UNION ALL\n"
4798  "SELECT '%s',\n"
4799  " p.prslextype::pg_catalog.regproc,\n"
4800  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4801  " FROM pg_catalog.pg_ts_parser p\n"
4802  " WHERE p.oid = '%s';",
4803  gettext_noop("Start parse"),
4804  gettext_noop("Method"),
4805  gettext_noop("Function"),
4806  gettext_noop("Description"),
4807  oid,
4808  gettext_noop("Get next token"),
4809  oid,
4810  gettext_noop("End parse"),
4811  oid,
4812  gettext_noop("Get headline"),
4813  oid,
4814  gettext_noop("Get token types"),
4815  oid);
4816 
4817  res = PSQLexec(buf.data);
4818  termPQExpBuffer(&buf);
4819  if (!res)
4820  return false;
4821 
4822  myopt.nullPrint = NULL;
4823  initPQExpBuffer(&title);
4824  if (nspname)
4825  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4826  nspname, prsname);
4827  else
4828  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4829  myopt.title = title.data;
4830  myopt.footers = NULL;
4831  myopt.topt.default_footer = false;
4832  myopt.translate_header = true;
4833  myopt.translate_columns = translate_columns;
4834  myopt.n_translate_columns = lengthof(translate_columns);
4835 
4836  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4837 
4838  PQclear(res);
4839 
4840  initPQExpBuffer(&buf);
4841 
4842  printfPQExpBuffer(&buf,
4843  "SELECT t.alias as \"%s\",\n"
4844  " t.description as \"%s\"\n"
4845  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4846  "ORDER BY 1;",
4847  gettext_noop("Token name"),
4848  gettext_noop("Description"),
4849  oid);
4850 
4851  res = PSQLexec(buf.data);
4852  termPQExpBuffer(&buf);
4853  if (!res)
4854  return false;
4855 
4856  myopt.nullPrint = NULL;
4857  if (nspname)
4858  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4859  nspname, prsname);
4860  else
4861  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4862  myopt.title = title.data;
4863  myopt.footers = NULL;
4864  myopt.topt.default_footer = true;
4865  myopt.translate_header = true;
4866  myopt.translate_columns = NULL;
4867  myopt.n_translate_columns = 0;
4868 
4869  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4870 
4871  termPQExpBuffer(&title);
4872  PQclear(res);
4873  return true;
4874 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
printTableOpt topt
Definition: print.h:167
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
#define lengthof(array)
Definition: c.h:669
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
char ** footers
Definition: print.h:170
bool default_footer
Definition: print.h:111
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeOperators()

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

Definition at line 782 of file describe.c.

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

Referenced by exec_command_d().

783 {
785  PGresult *res;
786  printQueryOpt myopt = pset.popt;
787 
788  initPQExpBuffer(&buf);
789 
790  /*
791  * Note: before Postgres 9.1, we did not assign comments to any built-in
792  * operators, preferring to let the comment on the underlying function
793  * suffice. The coalesce() on the obj_description() calls below supports
794  * this convention by providing a fallback lookup of a comment on the
795  * operator's function. As of 9.1 there is a policy that every built-in
796  * operator should have a comment; so the coalesce() is no longer
797  * necessary so far as built-in operators are concerned. We keep it
798  * anyway, for now, because (1) third-party modules may still be following
799  * the old convention, and (2) we'd need to do it anyway when talking to a
800  * pre-9.1 server.
801  */
802 
803  printfPQExpBuffer(&buf,
804  "SELECT n.nspname as \"%s\",\n"
805  " o.oprname AS \"%s\",\n"
806  " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
807  " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
808  " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
809  gettext_noop("Schema"),
810  gettext_noop("Name"),
811  gettext_noop("Left arg type"),
812  gettext_noop("Right arg type"),
813  gettext_noop("Result type"));
814 
815  if (verbose)
816  appendPQExpBuffer(&buf,
817  " o.oprcode AS \"%s\",\n",
818  gettext_noop("Function"));
819 
820  appendPQExpBuffer(&buf,
821  " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
822  " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
823  "FROM pg_catalog.pg_operator o\n"
824  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
825  gettext_noop("Description"));
826 
827  if (!showSystem && !pattern)
828  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
829  " AND n.nspname <> 'information_schema'\n");
830 
831  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
832  "n.nspname", "o.oprname", NULL,
833  "pg_catalog.pg_operator_is_visible(o.oid)");
834 
835  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
836 
837  res = PSQLexec(buf.data);
838  termPQExpBuffer(&buf);
839  if (!res)
840  return false;
841 
842  myopt.nullPrint = NULL;
843  myopt.title = _("List of operators");
844  myopt.translate_header = true;
845 
846  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
847 
848  PQclear(res);
849  return true;
850 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describePublications()

bool describePublications ( const char *  pattern)

Definition at line 5775 of file describe.c.

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

Referenced by exec_command_d().

5776 {
5778  int i;
5779  PGresult *res;
5780  bool has_pubtruncate;
5781 
5782  if (pset.sversion < 100000)
5783  {
5784  char sverbuf[32];
5785 
5786  pg_log_error("The server (version %s) does not support publications.",
5788  sverbuf, sizeof(sverbuf)));
5789  return true;
5790  }
5791 
5792  has_pubtruncate = (pset.sversion >= 110000);
5793 
5794  initPQExpBuffer(&buf);
5795 
5796  printfPQExpBuffer(&buf,
5797  "SELECT oid, pubname,\n"
5798  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
5799  " puballtables, pubinsert, pubupdate, pubdelete");
5800  if (has_pubtruncate)
5801  appendPQExpBufferStr(&buf,
5802  ", pubtruncate");
5803  appendPQExpBufferStr(&buf,
5804  "\nFROM pg_catalog.pg_publication\n");
5805 
5806  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5807  NULL, "pubname", NULL,
5808  NULL);
5809 
5810  appendPQExpBufferStr(&buf, "ORDER BY 2;");
5811 
5812  res = PSQLexec(buf.data);
5813  if (!res)
5814  {
5815  termPQExpBuffer(&buf);
5816  return false;
5817  }
5818 
5819  if (PQntuples(res) == 0)
5820  {
5821  if (!pset.quiet)
5822  {
5823  if (pattern)
5824  pg_log_error("Did not find any publication named \"%s\".",
5825  pattern);
5826  else
5827  pg_log_error("Did not find any publications.");
5828  }
5829 
5830  termPQExpBuffer(&buf);
5831  PQclear(res);
5832  return false;
5833  }
5834 
5835  for (i = 0; i < PQntuples(res); i++)
5836  {
5837  const char align = 'l';
5838  int ncols = 5;
5839  int nrows = 1;
5840  int tables = 0;
5841  PGresult *tabres;
5842  char *pubid = PQgetvalue(res, i, 0);
5843  char *pubname = PQgetvalue(res, i, 1);
5844  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
5845  int j;
5846  PQExpBufferData title;
5847  printTableOpt myopt = pset.popt.topt;
5848  printTableContent cont;
5849 
5850  if (has_pubtruncate)
5851  ncols++;
5852 
5853  initPQExpBuffer(&title);
5854  printfPQExpBuffer(&title, _("Publication %s"), pubname);
5855  printTableInit(&cont, &myopt, title.data, ncols, nrows);
5856 
5857  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
5858  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
5859  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
5860  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
5861  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
5862  if (has_pubtruncate)
5863  printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
5864 
5865  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
5866  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
5867  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
5868  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
5869  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
5870  if (has_pubtruncate)
5871  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
5872 
5873  if (!puballtables)
5874  {
5875  printfPQExpBuffer(&buf,
5876  "SELECT n.nspname, c.relname\n"
5877  "FROM pg_catalog.pg_class c,\n"
5878  " pg_catalog.pg_namespace n,\n"
5879  " pg_catalog.pg_publication_rel pr\n"
5880  "WHERE c.relnamespace = n.oid\n"
5881  " AND c.oid = pr.prrelid\n"
5882  " AND pr.prpubid = '%s'\n"
5883  "ORDER BY 1,2", pubid);
5884 
5885  tabres = PSQLexec(buf.data);
5886  if (!tabres)
5887  {
5888  printTableCleanup(&cont);
5889  PQclear(res);
5890  termPQExpBuffer(&buf);
5891  termPQExpBuffer(&title);
5892  return false;
5893  }
5894  else
5895  tables = PQntuples(tabres);
5896 
5897  if (tables > 0)
5898  printTableAddFooter(&cont, _("Tables:"));
5899 
5900  for (j = 0; j < tables; j++)
5901  {
5902  printfPQExpBuffer(&buf, " \"%s.%s\"",
5903  PQgetvalue(tabres, j, 0),
5904  PQgetvalue(tabres, j, 1));
5905 
5906  printTableAddFooter(&cont, buf.data);
5907  }
5908  PQclear(tabres);
5909  }
5910 
5911  printTable(&cont, pset.queryFout, false, pset.logfile);
5912  printTableCleanup(&cont);
5913 
5914  termPQExpBuffer(&title);
5915  }
5916 
5917  termPQExpBuffer(&buf);
5918  PQclear(res);
5919 
5920  return true;
5921 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PsqlSettings pset
Definition: startup.c:31
void printTableCleanup(printTableContent *const content)
Definition: print.c:3242
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
printTableOpt topt
Definition: print.h:167
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3111
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3151
static char * buf
Definition: pg_test_fsync.c:67
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3331
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3199
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
printQueryOpt popt
Definition: settings.h:91
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3074
int i
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeRoles()

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

Definition at line 3398 of file describe.c.

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

Referenced by exec_command_d().

3399 {
3401  PGresult *res;
3402  printTableContent cont;
3403  printTableOpt myopt = pset.popt.topt;
3404  int ncols = 3;
3405  int nrows = 0;
3406  int i;
3407  int conns;
3408  const char align = 'l';
3409  char **attr;
3410 
3411  myopt.default_footer = false;
3412 
3413  initPQExpBuffer(&buf);
3414 
3415  if (pset.sversion >= 80100)
3416  {
3417  printfPQExpBuffer(&buf,
3418  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3419  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3420  " r.rolconnlimit, r.rolvaliduntil,\n"
3421  " ARRAY(SELECT b.rolname\n"
3422  " FROM pg_catalog.pg_auth_members m\n"
3423  " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
3424  " WHERE m.member = r.oid) as memberof");
3425 
3426  if (verbose && pset.sversion >= 80200)
3427  {
3428  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3429  ncols++;
3430  }
3431  if (pset.sversion >= 90100)
3432  {
3433  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3434  }
3435 
3436  if (pset.sversion >= 90500)
3437  {
3438  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3439  }
3440 
3441  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3442 
3443  if (!showSystem && !pattern)
3444  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3445 
3446  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3447  NULL, "r.rolname", NULL, NULL);
3448  }
3449  else
3450  {
3451  printfPQExpBuffer(&buf,
3452  "SELECT u.usename AS rolname,\n"
3453  " u.usesuper AS rolsuper,\n"
3454  " true AS rolinherit, false AS rolcreaterole,\n"
3455  " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
3456  " -1 AS rolconnlimit,"
3457  " u.valuntil as rolvaliduntil,\n"
3458  " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
3459  "\nFROM pg_catalog.pg_user u\n");
3460 
3461  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3462  NULL, "u.usename", NULL, NULL);
3463  }
3464 
3465  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3466 
3467  res = PSQLexec(buf.data);
3468  if (!res)
3469  return false;
3470 
3471  nrows = PQntuples(res);
3472  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3473 
3474  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3475 
3476  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3477  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3478  printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
3479 
3480  if (verbose && pset.sversion >= 80200)
3481  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3482 
3483  for (i = 0; i < nrows; i++)
3484  {
3485  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3486 
3487  resetPQExpBuffer(&buf);
3488  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3489  add_role_attribute(&buf, _("Superuser"));
3490 
3491  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3492  add_role_attribute(&buf, _("No inheritance"));
3493 
3494  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3495  add_role_attribute(&buf, _("Create role"));
3496 
3497  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3498  add_role_attribute(&buf, _("Create DB"));
3499 
3500  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3501  add_role_attribute(&buf, _("Cannot login"));
3502 
3503  if (pset.sversion >= 90100)
3504  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3505  add_role_attribute(&buf, _("Replication"));
3506 
3507  if (pset.sversion >= 90500)
3508  if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
3509  add_role_attribute(&buf, _("Bypass RLS"));
3510 
3511  conns = atoi(PQgetvalue(res, i, 6));
3512  if (conns >= 0)
3513  {
3514  if (buf.len > 0)
3515  appendPQExpBufferChar(&buf, '\n');
3516 
3517  if (conns == 0)
3518  appendPQExpBufferStr(&buf, _("No connections"));
3519  else
3520  appendPQExpBuffer(&buf, ngettext("%d connection",
3521  "%d connections",
3522  conns),
3523  conns);
3524  }
3525 
3526  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3527  {
3528  if (buf.len > 0)
3529  appendPQExpBufferChar(&buf, '\n');
3530  appendPQExpBufferStr(&buf, _("Password valid until "));
3531  appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3532  }
3533 
3534  attr[i] = pg_strdup(buf.data);
3535 
3536  printTableAddCell(&cont, attr[i], false, false);
3537 
3538  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3539 
3540  if (verbose && pset.sversion >= 80200)
3541  printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
3542  }
3543  termPQExpBuffer(&buf);
3544 
3545  printTable(&cont, pset.queryFout, false, pset.logfile);
3546  printTableCleanup(&cont);
3547 
3548  for (i = 0; i < nrows; i++)
3549  free(attr[i]);
3550  free(attr);
3551 
3552  PQclear(res);
3553  return true;
3554 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PsqlSettings pset
Definition: startup.c:31
void printTableCleanup(printTableContent *const content)
Definition: print.c:3242
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
printTableOpt topt
Definition: print.h:167
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
static void add_role_attribute(PQExpBuffer buf, const char *const str)
Definition: describe.c:3557
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3111
static PGconn ** conns
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3151
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3331
static int verbose
#define ngettext(s, p, n)
Definition: c.h:1134
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
bool default_footer
Definition: print.h:111
printQueryOpt popt
Definition: settings.h:91
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3074
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeSubscriptions()

bool describeSubscriptions ( const char *  pattern,
bool  verbose 
)

Definition at line 5930 of file describe.c.

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

Referenced by exec_command_d().

5931 {
5933  PGresult *res;
5934  printQueryOpt myopt = pset.popt;
5935  static const bool translate_columns[] = {false, false, false, false,
5936  false, false};
5937 
5938  if (pset.sversion < 100000)
5939  {
5940  char sverbuf[32];
5941 
5942  pg_log_error("The server (version %s) does not support subscriptions.",
5944  sverbuf, sizeof(sverbuf)));
5945  return true;
5946  }
5947 
5948  initPQExpBuffer(&buf);
5949 
5950  printfPQExpBuffer(&buf,
5951  "SELECT subname AS \"%s\"\n"
5952  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
5953  ", subenabled AS \"%s\"\n"
5954  ", subpublications AS \"%s\"\n",
5955  gettext_noop("Name"),
5956  gettext_noop("Owner"),
5957  gettext_noop("Enabled"),
5958  gettext_noop("Publication"));
5959 
5960  if (verbose)
5961  {
5962  appendPQExpBuffer(&buf,
5963  ", subsynccommit AS \"%s\"\n"
5964  ", subconninfo AS \"%s\"\n",
5965  gettext_noop("Synchronous commit"),
5966  gettext_noop("Conninfo"));
5967  }
5968 
5969  /* Only display subscriptions in current database. */
5970  appendPQExpBufferStr(&buf,
5971  "FROM pg_catalog.pg_subscription\n"
5972  "WHERE subdbid = (SELECT oid\n"
5973  " FROM pg_catalog.pg_database\n"
5974  " WHERE datname = pg_catalog.current_database())");
5975 
5976  processSQLNamePattern(pset.db, &buf, pattern, true, false,
5977  NULL, "subname", NULL,
5978  NULL);
5979 
5980  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5981 
5982  res = PSQLexec(buf.data);
5983  termPQExpBuffer(&buf);
5984  if (!res)
5985  return false;
5986 
5987  myopt.nullPrint = NULL;
5988  myopt.title = _("List of subscriptions");
5989  myopt.translate_header = true;
5990  myopt.translate_columns = translate_columns;
5991  myopt.n_translate_columns = lengthof(translate_columns);
5992 
5993  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5994 
5995  PQclear(res);
5996  return true;
5997 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeTableDetails()

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

Definition at line 1366 of file describe.c.

References appendPQExpBufferStr(), buf, cancel_pressed, PQExpBufferData::data, _psqlSettings::db, describeOneTableDetails(), i, initPQExpBuffer(), pg_log_error, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), processSQLNamePattern(), pset, PSQLexec(), _psqlSettings::quiet, relname, and termPQExpBuffer().

Referenced by exec_command_d().

1367 {
1369  PGresult *res;
1370  int i;
1371 
1372  initPQExpBuffer(&buf);
1373 
1374  printfPQExpBuffer(&buf,
1375  "SELECT c.oid,\n"
1376  " n.nspname,\n"
1377  " c.relname\n"
1378  "FROM pg_catalog.pg_class c\n"
1379  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1380 
1381  if (!showSystem && !pattern)
1382  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1383  " AND n.nspname <> 'information_schema'\n");
1384 
1385  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1386  "n.nspname", "c.relname", NULL,
1387  "pg_catalog.pg_table_is_visible(c.oid)");
1388 
1389  appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1390 
1391  res = PSQLexec(buf.data);
1392  termPQExpBuffer(&buf);
1393  if (!res)
1394  return false;
1395 
1396  if (PQntuples(res) == 0)
1397  {
1398  if (!pset.quiet)
1399  {
1400  if (pattern)
1401  pg_log_error("Did not find any relation named \"%s\".",
1402  pattern);
1403  else
1404  pg_log_error("Did not find any relations.");
1405  }
1406  PQclear(res);
1407  return false;
1408  }
1409 
1410  for (i = 0; i < PQntuples(res); i++)
1411  {
1412  const char *oid;
1413  const char *nspname;
1414  const char *relname;
1415 
1416  oid = PQgetvalue(res, i, 0);
1417  nspname = PQgetvalue(res, i, 1);
1418  relname = PQgetvalue(res, i, 2);
1419 
1420  if (!describeOneTableDetails(nspname, relname, oid, verbose))
1421  {
1422  PQclear(res);
1423  return false;
1424  }
1425  if (cancel_pressed)
1426  {
1427  PQclear(res);
1428  return false;
1429  }
1430  }
1431 
1432  PQclear(res);
1433  return true;
1434 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PsqlSettings pset
Definition: startup.c:31
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
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)
Definition: string_utils.c:827
NameData relname
Definition: pg_class.h:35
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
volatile bool cancel_pressed
Definition: print.c:44
static char * buf
Definition: pg_test_fsync.c:67
static bool describeOneTableDetails(const char *schemaname, const char *relationname, const char *oid, bool verbose)
Definition: describe.c:1444
static int verbose
void PQclear(PGresult *res)
Definition: fe-exec.c:694
int i
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeTablespaces()

bool describeTablespaces ( const char *  pattern,
bool  verbose 
)

Definition at line 218 of file describe.c.

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

Referenced by exec_command_d().

219 {
221  PGresult *res;
222  printQueryOpt myopt = pset.popt;
223 
224  if (pset.sversion < 80000)
225  {
226  char sverbuf[32];
227 
228  pg_log_info("The server (version %s) does not support tablespaces.",
230  sverbuf, sizeof(sverbuf)));
231  return true;
232  }
233 
234  initPQExpBuffer(&buf);
235 
236  if (pset.sversion >= 90200)
237  printfPQExpBuffer(&buf,
238  "SELECT spcname AS \"%s\",\n"
239  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
240  " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
241  gettext_noop("Name"),
242  gettext_noop("Owner"),
243  gettext_noop("Location"));
244  else
245  printfPQExpBuffer(&buf,
246  "SELECT spcname AS \"%s\",\n"
247  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
248  " spclocation AS \"%s\"",
249  gettext_noop("Name"),
250  gettext_noop("Owner"),
251  gettext_noop("Location"));
252 
253  if (verbose)
254  {
255  appendPQExpBufferStr(&buf, ",\n ");
256  printACLColumn(&buf, "spcacl");
257  }
258 
259  if (verbose && pset.sversion >= 90000)
260  appendPQExpBuffer(&buf,
261  ",\n spcoptions AS \"%s\"",
262  gettext_noop("Options"));
263 
264  if (verbose && pset.sversion >= 90200)
265  appendPQExpBuffer(&buf,
266  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\"",
267  gettext_noop("Size"));
268 
269  if (verbose && pset.sversion >= 80200)
270  appendPQExpBuffer(&buf,
271  ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
272  gettext_noop("Description"));
273 
275  "\nFROM pg_catalog.pg_tablespace\n");
276 
277  processSQLNamePattern(pset.db, &buf, pattern, false, false,
278  NULL, "spcname", NULL,
279  NULL);
280 
281  appendPQExpBufferStr(&buf, "ORDER BY 1;");
282 
283  res = PSQLexec(buf.data);
284  termPQExpBuffer(&buf);
285  if (!res)
286  return false;
287 
288  myopt.nullPrint = NULL;
289  myopt.title = _("List of tablespaces");
290  myopt.translate_header = true;
291 
292  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
293 
294  PQclear(res);
295  return true;
296 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6007
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
#define pg_log_info(...)
Definition: logging.h:87
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ describeTypes()

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

Definition at line 666 of file describe.c.

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

Referenced by exec_command_d().

667 {
669  PGresult *res;
670  printQueryOpt myopt = pset.popt;
671 
672  initPQExpBuffer(&buf);
673 
674  printfPQExpBuffer(&buf,
675  "SELECT n.nspname as \"%s\",\n"
676  " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
677  gettext_noop("Schema"),
678  gettext_noop("Name"));
679  if (verbose)
680  appendPQExpBuffer(&buf,
681  " t.typname AS \"%s\",\n"
682  " CASE WHEN t.typrelid != 0\n"
683  " THEN CAST('tuple' AS pg_catalog.text)\n"
684  " WHEN t.typlen < 0\n"
685  " THEN CAST('var' AS pg_catalog.text)\n"
686  " ELSE CAST(t.typlen AS pg_catalog.text)\n"
687  " END AS \"%s\",\n",
688  gettext_noop("Internal name"),
689  gettext_noop("Size"));
690  if (verbose && pset.sversion >= 80300)
691  {
693  " pg_catalog.array_to_string(\n"
694  " ARRAY(\n"
695  " SELECT e.enumlabel\n"
696  " FROM pg_catalog.pg_enum e\n"
697  " WHERE e.enumtypid = t.oid\n");
698 
699  if (pset.sversion >= 90100)
701  " ORDER BY e.enumsortorder\n");
702  else
704  " ORDER BY e.oid\n");
705 
706  appendPQExpBuffer(&buf,
707  " ),\n"
708  " E'\\n'\n"
709  " ) AS \"%s\",\n",
710  gettext_noop("Elements"));
711  }
712  if (verbose)
713  {
714  appendPQExpBuffer(&buf,
715  " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
716  gettext_noop("Owner"));
717  }
718  if (verbose && pset.sversion >= 90200)
719  {
720  printACLColumn(&buf, "t.typacl");
721  appendPQExpBufferStr(&buf, ",\n ");
722  }
723 
724  appendPQExpBuffer(&buf,
725  " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
726  gettext_noop("Description"));
727 
728  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
729  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
730 
731  /*
732  * do not include complex types (typrelid!=0) unless they are standalone
733  * composite types
734  */
735  appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
736  appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
737  " FROM pg_catalog.pg_class c "
738  "WHERE c.oid = t.typrelid))\n");
739 
740  /*
741  * do not include array types (before 8.3 we have to use the assumption
742  * that their names start with underscore)
743  */
744  if (pset.sversion >= 80300)
745  appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
746  else
747  appendPQExpBufferStr(&buf, " AND t.typname !~ '^_'\n");
748 
749  if (!showSystem && !pattern)
750  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
751  " AND n.nspname <> 'information_schema'\n");
752 
753  /* Match name pattern against either internal or external name */
754  processSQLNamePattern(pset.db, &buf, pattern, true, false,
755  "n.nspname", "t.typname",
756  "pg_catalog.format_type(t.oid, NULL)",
757  "pg_catalog.pg_type_is_visible(t.oid)");
758 
759  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
760 
761  res = PSQLexec(buf.data);
762  termPQExpBuffer(&buf);
763  if (!res)
764  return false;
765 
766  myopt.nullPrint = NULL;
767  myopt.title = _("List of data types");
768  myopt.translate_header = true;
769 
770  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
771 
772  PQclear(res);
773  return true;
774 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
#define CppAsString2(x)
Definition: c.h:224
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6007
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listAllDbs()

bool listAllDbs ( const char *  pattern,
bool  verbose 
)

Definition at line 859 of file describe.c.

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

Referenced by exec_command_list(), and main().

860 {
861  PGresult *res;
863  printQueryOpt myopt = pset.popt;
864 
865  initPQExpBuffer(&buf);
866 
867  printfPQExpBuffer(&buf,
868  "SELECT d.datname as \"%s\",\n"
869  " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
870  " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
871  gettext_noop("Name"),
872  gettext_noop("Owner"),
873  gettext_noop("Encoding"));
874  if (pset.sversion >= 80400)
875  appendPQExpBuffer(&buf,
876  " d.datcollate as \"%s\",\n"
877  " d.datctype as \"%s\",\n",
878  gettext_noop("Collate"),
879  gettext_noop("Ctype"));
880  appendPQExpBufferStr(&buf, " ");
881  printACLColumn(&buf, "d.datacl");
882  if (verbose && pset.sversion >= 80200)
883  appendPQExpBuffer(&buf,
884  ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
885  " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
886  " ELSE 'No Access'\n"
887  " END as \"%s\"",
888  gettext_noop("Size"));
889  if (verbose && pset.sversion >= 80000)
890  appendPQExpBuffer(&buf,
891  ",\n t.spcname as \"%s\"",
892  gettext_noop("Tablespace"));
893  if (verbose && pset.sversion >= 80200)
894  appendPQExpBuffer(&buf,
895  ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
896  gettext_noop("Description"));
898  "\nFROM pg_catalog.pg_database d\n");
899  if (verbose && pset.sversion >= 80000)
901  " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
902 
903  if (pattern)
904  processSQLNamePattern(pset.db, &buf, pattern, false, false,
905  NULL, "d.datname", NULL, NULL);
906 
907  appendPQExpBufferStr(&buf, "ORDER BY 1;");
908  res = PSQLexec(buf.data);
909  termPQExpBuffer(&buf);
910  if (!res)
911  return false;
912 
913  myopt.nullPrint = NULL;
914  myopt.title = _("List of databases");
915  myopt.translate_header = true;
916 
917  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
918 
919  PQclear(res);
920  return true;
921 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6007
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listCasts()

bool listCasts ( const char *  pattern,
bool  verbose 
)

Definition at line 4361 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, PSQLexec(), _psqlSettings::queryFout, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

4362 {
4364  PGresult *res;
4365  printQueryOpt myopt = pset.popt;
4366  static const bool translate_columns[] = {false, false, false, true, false};
4367 
4368  initPQExpBuffer(&buf);
4369 
4370  printfPQExpBuffer(&buf,
4371  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
4372  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n",
4373  gettext_noop("Source type"),
4374  gettext_noop("Target type"));
4375 
4376  /*
4377  * We don't attempt to localize '(binary coercible)' or '(with inout)',
4378  * because there's too much risk of gettext translating a function name
4379  * that happens to match some string in the PO database.
4380  */
4381  if (pset.sversion >= 80400)
4382  appendPQExpBuffer(&buf,
4383  " CASE WHEN c.castmethod = '%c' THEN '(binary coercible)'\n"
4384  " WHEN c.castmethod = '%c' THEN '(with inout)'\n"
4385  " ELSE p.proname\n"
4386  " END AS \"%s\",\n",
4387  COERCION_METHOD_BINARY,
4388  COERCION_METHOD_INOUT,
4389  gettext_noop("Function"));
4390  else
4391  appendPQExpBuffer(&buf,
4392  " CASE WHEN c.castfunc = 0 THEN '(binary coercible)'\n"
4393  " ELSE p.proname\n"
4394  " END AS \"%s\",\n",
4395  gettext_noop("Function"));
4396 
4397  appendPQExpBuffer(&buf,
4398  " CASE WHEN c.castcontext = '%c' THEN '%s'\n"
4399  " WHEN c.castcontext = '%c' THEN '%s'\n"
4400  " ELSE '%s'\n"
4401  " END AS \"%s\"",
4402  COERCION_CODE_EXPLICIT,
4403  gettext_noop("no"),
4404  COERCION_CODE_ASSIGNMENT,
4405  gettext_noop("in assignment"),
4406  gettext_noop("yes"),
4407  gettext_noop("Implicit?"));
4408 
4409  if (verbose)
4410  appendPQExpBuffer(&buf,
4411  ",\n d.description AS \"%s\"",
4412  gettext_noop("Description"));
4413 
4414  /*
4415  * We need a left join to pg_proc for binary casts; the others are just
4416  * paranoia.
4417  */
4418  appendPQExpBufferStr(&buf,
4419  "\nFROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
4420  " ON c.castfunc = p.oid\n"
4421  " LEFT JOIN pg_catalog.pg_type ts\n"
4422  " ON c.castsource = ts.oid\n"
4423  " LEFT JOIN pg_catalog.pg_namespace ns\n"
4424  " ON ns.oid = ts.typnamespace\n"
4425  " LEFT JOIN pg_catalog.pg_type tt\n"
4426  " ON c.casttarget = tt.oid\n"
4427  " LEFT JOIN pg_catalog.pg_namespace nt\n"
4428  " ON nt.oid = tt.typnamespace\n");
4429 
4430  if (verbose)
4431  appendPQExpBufferStr(&buf,
4432  " LEFT JOIN pg_catalog.pg_description d\n"
4433  " ON d.classoid = c.tableoid AND d.objoid = "
4434  "c.oid AND d.objsubid = 0\n");
4435 
4436  appendPQExpBufferStr(&buf, "WHERE ( (true");
4437 
4438  /*
4439  * Match name pattern against either internal or external name of either
4440  * castsource or casttarget
4441  */
4442  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4443  "ns.nspname", "ts.typname",
4444  "pg_catalog.format_type(ts.oid, NULL)",
4445  "pg_catalog.pg_type_is_visible(ts.oid)");
4446 
4447  appendPQExpBufferStr(&buf, ") OR (true");
4448 
4449  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4450  "nt.nspname", "tt.typname",
4451  "pg_catalog.format_type(tt.oid, NULL)",
4452  "pg_catalog.pg_type_is_visible(tt.oid)");
4453 
4454  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
4455 
4456  res = PSQLexec(buf.data);
4457  termPQExpBuffer(&buf);
4458  if (!res)
4459  return false;
4460 
4461  myopt.nullPrint = NULL;
4462  myopt.title = _("List of casts");
4463  myopt.translate_header = true;
4464  myopt.translate_columns = translate_columns;
4465  myopt.n_translate_columns = lengthof(translate_columns);
4466 
4467  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4468 
4469  PQclear(res);
4470  return true;
4471 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listCollations()

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

Definition at line 4479 of file describe.c.

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

Referenced by exec_command_d().

4480 {
4482  PGresult *res;
4483  printQueryOpt myopt = pset.popt;
4484  static const bool translate_columns[] = {false, false, false, false, false, true, false};
4485 
4486  if (pset.sversion < 90100)
4487  {
4488  char sverbuf[32];
4489 
4490  pg_log_error("The server (version %s) does not support collations.",
4492  sverbuf, sizeof(sverbuf)));
4493  return true;
4494  }
4495 
4496  initPQExpBuffer(&buf);
4497 
4498  printfPQExpBuffer(&buf,
4499  "SELECT n.nspname AS \"%s\",\n"
4500  " c.collname AS \"%s\",\n"
4501  " c.collcollate AS \"%s\",\n"
4502  " c.collctype AS \"%s\"",
4503  gettext_noop("Schema"),
4504  gettext_noop("Name"),
4505  gettext_noop("Collate"),
4506  gettext_noop("Ctype"));
4507 
4508  if (pset.sversion >= 100000)
4509  appendPQExpBuffer(&buf,
4510  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
4511  gettext_noop("Provider"));
4512  else
4513  appendPQExpBuffer(&buf,
4514  ",\n 'libc' AS \"%s\"",
4515  gettext_noop("Provider"));
4516 
4517  if (pset.sversion >= 120000)
4518  appendPQExpBuffer(&buf,
4519  ",\n CASE WHEN c.collisdeterministic THEN '%s' ELSE '%s' END AS \"%s\"",
4520  gettext_noop("yes"), gettext_noop("no"),
4521  gettext_noop("Deterministic?"));
4522  else
4523  appendPQExpBuffer(&buf,
4524  ",\n '%s' AS \"%s\"",
4525  gettext_noop("yes"),
4526  gettext_noop("Deterministic?"));
4527 
4528  if (verbose)
4529  appendPQExpBuffer(&buf,
4530  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
4531  gettext_noop("Description"));
4532 
4533  appendPQExpBufferStr(&buf,
4534  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
4535  "WHERE n.oid = c.collnamespace\n");
4536 
4537  if (!showSystem && !pattern)
4538  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4539  " AND n.nspname <> 'information_schema'\n");
4540 
4541  /*
4542  * Hide collations that aren't usable in the current database's encoding.
4543  * If you think to change this, note that pg_collation_is_visible rejects
4544  * unusable collations, so you will need to hack name pattern processing
4545  * somehow to avoid inconsistent behavior.
4546  */
4547  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
4548 
4549  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4550  "n.nspname", "c.collname", NULL,
4551  "pg_catalog.pg_collation_is_visible(c.oid)");
4552 
4553  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4554 
4555  res = PSQLexec(buf.data);
4556  termPQExpBuffer(&buf);
4557  if (!res)
4558  return false;
4559 
4560  myopt.nullPrint = NULL;
4561  myopt.title = _("List of collations");
4562  myopt.translate_header = true;
4563  myopt.translate_columns = translate_columns;
4564  myopt.n_translate_columns = lengthof(translate_columns);
4565 
4566  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4567 
4568  PQclear(res);
4569  return true;
4570 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listConversions()

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

Definition at line 4219 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, PSQLexec(), _psqlSettings::queryFout, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

4220 {
4222  PGresult *res;
4223  printQueryOpt myopt = pset.popt;
4224  static const bool translate_columns[] =
4225  {false, false, false, false, true, false};
4226 
4227  initPQExpBuffer(&buf);
4228 
4229  printfPQExpBuffer(&buf,
4230  "SELECT n.nspname AS \"%s\",\n"
4231  " c.conname AS \"%s\",\n"
4232  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
4233  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
4234  " CASE WHEN c.condefault THEN '%s'\n"
4235  " ELSE '%s' END AS \"%s\"",
4236  gettext_noop("Schema"),
4237  gettext_noop("Name"),
4238  gettext_noop("Source"),
4239  gettext_noop("Destination"),
4240  gettext_noop("yes"), gettext_noop("no"),
4241  gettext_noop("Default?"));
4242 
4243  if (verbose)
4244  appendPQExpBuffer(&buf,
4245  ",\n d.description AS \"%s\"",
4246  gettext_noop("Description"));
4247 
4248  appendPQExpBufferStr(&buf,
4249  "\nFROM pg_catalog.pg_conversion c\n"
4250  " JOIN pg_catalog.pg_namespace n "
4251  "ON n.oid = c.connamespace\n");
4252 
4253  if (verbose)
4254  appendPQExpBufferStr(&buf,
4255  "LEFT JOIN pg_catalog.pg_description d "
4256  "ON d.classoid = c.tableoid\n"
4257  " AND d.objoid = c.oid "
4258  "AND d.objsubid = 0\n");
4259 
4260  appendPQExpBufferStr(&buf, "WHERE true\n");
4261 
4262  if (!showSystem && !pattern)
4263  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4264  " AND n.nspname <> 'information_schema'\n");
4265 
4266  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4267  "n.nspname", "c.conname", NULL,
4268  "pg_catalog.pg_conversion_is_visible(c.oid)");
4269 
4270  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4271 
4272  res = PSQLexec(buf.data);
4273  termPQExpBuffer(&buf);
4274  if (!res)
4275  return false;
4276 
4277  myopt.nullPrint = NULL;
4278  myopt.title = _("List of conversions");
4279  myopt.translate_header = true;
4280  myopt.translate_columns = translate_columns;
4281  myopt.n_translate_columns = lengthof(translate_columns);
4282 
4283  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4284 
4285  PQclear(res);
4286  return true;
4287 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
static int verbose
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listDbRoleSettings()

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

Definition at line 3569 of file describe.c.

References _, appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, printQueryOpt::nullPrint, pg_log_error, _psqlSettings::popt, PQclear(), PQntuples(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, and printQueryOpt::translate_header.

Referenced by exec_command_d().

3570 {
3572  PGresult *res;
3573  printQueryOpt myopt = pset.popt;
3574  bool havewhere;
3575 
3576  if (pset.sversion < 90000)
3577  {
3578  char sverbuf[32];
3579 
3580  pg_log_error("The server (version %s) does not support per-database role settings.",
3582  sverbuf, sizeof(sverbuf)));
3583  return true;
3584  }
3585 
3586  initPQExpBuffer(&buf);
3587 
3588  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3589  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3590  "FROM pg_catalog.pg_db_role_setting s\n"
3591  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3592  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3593  gettext_noop("Role"),
3594  gettext_noop("Database"),
3595  gettext_noop("Settings"));
3596  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3597  NULL, "r.rolname", NULL, NULL);
3598  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3599  NULL, "d.datname", NULL, NULL);
3600  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3601 
3602  res = PSQLexec(buf.data);
3603  termPQExpBuffer(&buf);
3604  if (!res)
3605  return false;
3606 
3607  /*
3608  * Most functions in this file are content to print an empty table when
3609  * there are no matching objects. We intentionally deviate from that
3610  * here, but only in !quiet mode, because of the possibility that the user
3611  * is confused about what the two pattern arguments mean.
3612  */
3613  if (PQntuples(res) == 0 && !pset.quiet)
3614  {
3615  if (pattern && pattern2)
3616  pg_log_error("Did not find any settings for role \"%s\" and database \"%s\".",
3617  pattern, pattern2);
3618  else if (pattern)
3619  pg_log_error("Did not find any settings for role \"%s\".",
3620  pattern);
3621  else
3622  pg_log_error("Did not find any settings.");
3623  }
3624  else
3625  {
3626  myopt.nullPrint = NULL;
3627  myopt.title = _("List of settings");
3628  myopt.translate_header = true;
3629 
3630  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3631  }
3632 
3633  PQclear(res);
3634  return true;
3635 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listDefaultACLs()

bool listDefaultACLs ( const char *  pattern)

Definition at line 1089 of file describe.c.

References _, appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, printQueryOpt::nullPrint, pg_log_error, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, PSQLexec(), _psqlSettings::queryFout, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

1090 {
1092  PGresult *res;
1093  printQueryOpt myopt = pset.popt;
1094  static const bool translate_columns[] = {false, false, true, false};
1095 
1096  if (pset.sversion < 90000)
1097  {
1098  char sverbuf[32];
1099 
1100  pg_log_error("The server (version %s) does not support altering default privileges.",
1102  sverbuf, sizeof(sverbuf)));
1103  return true;
1104  }
1105 
1106  initPQExpBuffer(&buf);
1107 
1108  printfPQExpBuffer(&buf,
1109  "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1110  " n.nspname AS \"%s\",\n"
1111  " 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"
1112  " ",
1113  gettext_noop("Owner"),
1114  gettext_noop("Schema"),
1115  DEFACLOBJ_RELATION,
1116  gettext_noop("table"),
1117  DEFACLOBJ_SEQUENCE,
1118  gettext_noop("sequence"),
1119  DEFACLOBJ_FUNCTION,
1120  gettext_noop("function"),
1121  DEFACLOBJ_TYPE,
1122  gettext_noop("type"),
1123  DEFACLOBJ_NAMESPACE,
1124  gettext_noop("schema"),
1125  gettext_noop("Type"));
1126 
1127  printACLColumn(&buf, "d.defaclacl");
1128 
1129  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1130  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1131 
1132  processSQLNamePattern(pset.db, &buf, pattern, false, false,
1133  NULL,
1134  "n.nspname",
1135  "pg_catalog.pg_get_userbyid(d.defaclrole)",
1136  NULL);
1137 
1138  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1139 
1140  res = PSQLexec(buf.data);
1141  if (!res)
1142  {
1143  termPQExpBuffer(&buf);
1144  return false;
1145  }
1146 
1147  myopt.nullPrint = NULL;
1148  printfPQExpBuffer(&buf, _("Default access privileges"));
1149  myopt.title = buf.data;
1150  myopt.translate_header = true;
1151  myopt.translate_columns = translate_columns;
1152  myopt.n_translate_columns = lengthof(translate_columns);
1153 
1154  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1155 
1156  termPQExpBuffer(&buf);
1157  PQclear(res);
1158  return true;
1159 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:168
PsqlSettings pset
Definition: startup.c:31
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1148
FILE * queryFout
Definition: settings.h:84
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)
Definition: string_utils.c:827
#define lengthof(array)
Definition: c.h:669
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:171
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:169
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:174
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const bool * translate_columns
Definition: print.h:172
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:6007
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGresult * PSQLexec(const char *query)
Definition: common.c:546

◆ listDomains()

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

Definition at line 4133 of file describe.c.

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

Referenced by exec_command_d().

4134 {
4136  PGresult *res;
4137  printQueryOpt myopt = pset.popt;
4138 
4139  initPQExpBuffer(&buf);
4140 
4141  printfPQExpBuffer(&buf,
4142  "SELECT n.nspname as \"%s\",\n"
4143  " t.typname as \"%s\",\n"
4144  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
4145  gettext_noop("Schema"),
4146  gettext_noop("Name"),
4147  gettext_noop("Type"));
4148 
4149  if (pset.sversion >= 90100)
4150  appendPQExpBuffer(&buf,
4151  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
4152  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
4153  gettext_noop("Collation"));
4154  appendPQExpBuffer(&buf,
4155  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
4156  " t.typdefault as \"%s\",\n"
4157  " pg_catalog.array_to_string(ARRAY(\n"
4158  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
4159  " ), ' ') as \"%s\"",
4160  gettext_noop("Nullable"),
4161  gettext_noop("Default"),
4162  gettext_noop("Check"));
4163 
4164  if (verbose)
4165  {
4166  if (pset.sversion >= 90200)
4167  {
4168  appendPQExpBufferStr(&buf, ",\n ");
4169