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

Go to the source code of this file.

Functions

static 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)
 
bool listOperatorClasses (const char *access_method_pattern, const char *type_pattern, bool verbose)
 
bool listOperatorFamilies (const char *access_method_pattern, const char *type_pattern, bool verbose)
 
bool listOpFamilyOperators (const char *access_method_pattern, const char *family_pattern, bool verbose)
 
bool listOpFamilyFunctions (const char *access_method_pattern, const char *family_pattern, bool verbose)
 

Function Documentation

◆ add_role_attribute()

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

Definition at line 3586 of file describe.c.

References appendPQExpBufferStr(), and PQExpBufferData::len.

Referenced by describeRoles().

3587 {
3588  if (buf->len > 0)
3589  appendPQExpBufferStr(buf, ", ");
3590 
3591  appendPQExpBufferStr(buf, str);
3592 }
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 3361 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().

3363 {
3364  /* relkinds for which we support tablespaces */
3365  if (relkind == RELKIND_RELATION ||
3366  relkind == RELKIND_MATVIEW ||
3367  relkind == RELKIND_INDEX ||
3368  relkind == RELKIND_PARTITIONED_TABLE ||
3369  relkind == RELKIND_PARTITIONED_INDEX ||
3370  relkind == RELKIND_TOASTVALUE)
3371  {
3372  /*
3373  * We ignore the database default tablespace so that users not using
3374  * tablespaces don't need to know about them. This case also covers
3375  * pre-8.0 servers, for which tablespace will always be 0.
3376  */
3377  if (tablespace != 0)
3378  {
3379  PGresult *result = NULL;
3381 
3382  initPQExpBuffer(&buf);
3383  printfPQExpBuffer(&buf,
3384  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3385  "WHERE oid = '%u';", tablespace);
3386  result = PSQLexec(buf.data);
3387  if (!result)
3388  {
3389  termPQExpBuffer(&buf);
3390  return;
3391  }
3392  /* Should always be the case, but.... */
3393  if (PQntuples(result) > 0)
3394  {
3395  if (newline)
3396  {
3397  /* Add the tablespace as a new footer */
3398  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3399  PQgetvalue(result, 0, 0));
3400  printTableAddFooter(cont, buf.data);
3401  }
3402  else
3403  {
3404  /* Append the tablespace to the latest footer */
3405  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3406 
3407  /*-------
3408  translator: before this string there's an index description like
3409  '"foo_pkey" PRIMARY KEY, btree (a)' */
3410  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3411  PQgetvalue(result, 0, 0));
3412  printTableSetFooter(cont, buf.data);
3413  }
3414  }
3415  PQclear(result);
3416  termPQExpBuffer(&buf);
3417  }
3418  }
3419 }
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3207
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
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:3182
printTableFooter * footer
Definition: print.h:161
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * data
Definition: print.h:139
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:536

◆ describeAccessMethods()

bool describeAccessMethods ( const char *  pattern,
bool  verbose 
)

Definition at line 149 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().

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

◆ describeAggregates()

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

Definition at line 63 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().

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

◆ describeFunctions()

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

Definition at line 315 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().

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

◆ describeOneTableDetails()

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

Definition at line 1449 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, relname, resetPQExpBuffer(), _psqlSettings::sversion, tablespace, termPQExpBuffer(), printQueryOpt::title, tmpbuf, printQueryOpt::topt, and printQueryOpt::translate_header.

Referenced by describeTableDetails().

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

◆ describeOneTSConfig()

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

Definition at line 5203 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().

5205 {
5207  title;
5208  PGresult *res;
5209  printQueryOpt myopt = pset.popt;
5210 
5211  initPQExpBuffer(&buf);
5212 
5213  printfPQExpBuffer(&buf,
5214  "SELECT\n"
5215  " ( SELECT t.alias FROM\n"
5216  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
5217  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
5218  " pg_catalog.btrim(\n"
5219  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
5220  " FROM pg_catalog.pg_ts_config_map AS mm\n"
5221  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
5222  " ORDER BY mapcfg, maptokentype, mapseqno\n"
5223  " ) :: pg_catalog.text,\n"
5224  " '{}') AS \"%s\"\n"
5225  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
5226  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
5227  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
5228  "ORDER BY 1;",
5229  gettext_noop("Token"),
5230  gettext_noop("Dictionaries"),
5231  oid);
5232 
5233  res = PSQLexec(buf.data);
5234  termPQExpBuffer(&buf);
5235  if (!res)
5236  return false;
5237 
5238  initPQExpBuffer(&title);
5239 
5240  if (nspname)
5241  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
5242  nspname, cfgname);
5243  else
5244  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
5245  cfgname);
5246 
5247  if (pnspname)
5248  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
5249  pnspname, prsname);
5250  else
5251  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
5252  prsname);
5253 
5254  myopt.nullPrint = NULL;
5255  myopt.title = title.data;
5256  myopt.footers = NULL;
5257  myopt.topt.default_footer = false;
5258  myopt.translate_header = true;
5259 
5260  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5261 
5262  termPQExpBuffer(&title);
5263 
5264  PQclear(res);
5265  return true;
5266 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
printTableOpt topt
Definition: print.h:169
#define gettext_noop(x)
Definition: c.h:1137
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:173
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
char ** footers
Definition: print.h:172
bool default_footer
Definition: print.h:113
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:536

◆ describeOneTSParser()

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

Definition at line 4808 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().

4809 {
4811  PGresult *res;
4812  PQExpBufferData title;
4813  printQueryOpt myopt = pset.popt;
4814  static const bool translate_columns[] = {true, false, false};
4815 
4816  initPQExpBuffer(&buf);
4817 
4818  printfPQExpBuffer(&buf,
4819  "SELECT '%s' AS \"%s\",\n"
4820  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4821  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4822  " FROM pg_catalog.pg_ts_parser p\n"
4823  " WHERE p.oid = '%s'\n"
4824  "UNION ALL\n"
4825  "SELECT '%s',\n"
4826  " p.prstoken::pg_catalog.regproc,\n"
4827  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4828  " FROM pg_catalog.pg_ts_parser p\n"
4829  " WHERE p.oid = '%s'\n"
4830  "UNION ALL\n"
4831  "SELECT '%s',\n"
4832  " p.prsend::pg_catalog.regproc,\n"
4833  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4834  " FROM pg_catalog.pg_ts_parser p\n"
4835  " WHERE p.oid = '%s'\n"
4836  "UNION ALL\n"
4837  "SELECT '%s',\n"
4838  " p.prsheadline::pg_catalog.regproc,\n"
4839  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4840  " FROM pg_catalog.pg_ts_parser p\n"
4841  " WHERE p.oid = '%s'\n"
4842  "UNION ALL\n"
4843  "SELECT '%s',\n"
4844  " p.prslextype::pg_catalog.regproc,\n"
4845  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4846  " FROM pg_catalog.pg_ts_parser p\n"
4847  " WHERE p.oid = '%s';",
4848  gettext_noop("Start parse"),
4849  gettext_noop("Method"),
4850  gettext_noop("Function"),
4851  gettext_noop("Description"),
4852  oid,
4853  gettext_noop("Get next token"),
4854  oid,
4855  gettext_noop("End parse"),
4856  oid,
4857  gettext_noop("Get headline"),
4858  oid,
4859  gettext_noop("Get token types"),
4860  oid);
4861 
4862  res = PSQLexec(buf.data);
4863  termPQExpBuffer(&buf);
4864  if (!res)
4865  return false;
4866 
4867  myopt.nullPrint = NULL;
4868  initPQExpBuffer(&title);
4869  if (nspname)
4870  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4871  nspname, prsname);
4872  else
4873  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4874  myopt.title = title.data;
4875  myopt.footers = NULL;
4876  myopt.topt.default_footer = false;
4877  myopt.translate_header = true;
4878  myopt.translate_columns = translate_columns;
4879  myopt.n_translate_columns = lengthof(translate_columns);
4880 
4881  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4882 
4883  PQclear(res);
4884 
4885  initPQExpBuffer(&buf);
4886 
4887  printfPQExpBuffer(&buf,
4888  "SELECT t.alias as \"%s\",\n"
4889  " t.description as \"%s\"\n"
4890  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4891  "ORDER BY 1;",
4892  gettext_noop("Token name"),
4893  gettext_noop("Description"),
4894  oid);
4895 
4896  res = PSQLexec(buf.data);
4897  termPQExpBuffer(&buf);
4898  if (!res)
4899  return false;
4900 
4901  myopt.nullPrint = NULL;
4902  if (nspname)
4903  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4904  nspname, prsname);
4905  else
4906  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4907  myopt.title = title.data;
4908  myopt.footers = NULL;
4909  myopt.topt.default_footer = true;
4910  myopt.translate_header = true;
4911  myopt.translate_columns = NULL;
4912  myopt.n_translate_columns = 0;
4913 
4914  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4915 
4916  termPQExpBuffer(&title);
4917  PQclear(res);
4918  return true;
4919 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
printTableOpt topt
Definition: print.h:169
#define gettext_noop(x)
Definition: c.h:1137
FILE * queryFout
Definition: settings.h:84
#define lengthof(array)
Definition: c.h:675
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:173
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
char ** footers
Definition: print.h:172
bool default_footer
Definition: print.h:113
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:176
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
const bool * translate_columns
Definition: print.h:174
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:536

◆ describeOperators()

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

Definition at line 783 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().

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

◆ describePublications()

bool describePublications ( const char *  pattern)

Definition at line 5824 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().

5825 {
5827  int i;
5828  PGresult *res;
5829  bool has_pubtruncate;
5830  bool has_pubviaroot;
5831 
5832  if (pset.sversion < 100000)
5833  {
5834  char sverbuf[32];
5835 
5836  pg_log_error("The server (version %s) does not support publications.",
5838  sverbuf, sizeof(sverbuf)));
5839  return true;
5840  }
5841 
5842  has_pubtruncate = (pset.sversion >= 110000);
5843  has_pubviaroot = (pset.sversion >= 130000);
5844 
5845  initPQExpBuffer(&buf);
5846 
5847  printfPQExpBuffer(&buf,
5848  "SELECT oid, pubname,\n"
5849  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
5850  " puballtables, pubinsert, pubupdate, pubdelete");
5851  if (has_pubtruncate)
5852  appendPQExpBufferStr(&buf,
5853  ", pubtruncate");
5854  if (has_pubviaroot)
5855  appendPQExpBufferStr(&buf,
5856  ", pubviaroot");
5857  appendPQExpBufferStr(&buf,
5858  "\nFROM pg_catalog.pg_publication\n");
5859 
5860  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5861  NULL, "pubname", NULL,
5862  NULL);
5863 
5864  appendPQExpBufferStr(&buf, "ORDER BY 2;");
5865 
5866  res = PSQLexec(buf.data);
5867  if (!res)
5868  {
5869  termPQExpBuffer(&buf);
5870  return false;
5871  }
5872 
5873  if (PQntuples(res) == 0)
5874  {
5875  if (!pset.quiet)
5876  {
5877  if (pattern)
5878  pg_log_error("Did not find any publication named \"%s\".",
5879  pattern);
5880  else
5881  pg_log_error("Did not find any publications.");
5882  }
5883 
5884  termPQExpBuffer(&buf);
5885  PQclear(res);
5886  return false;
5887  }
5888 
5889  for (i = 0; i < PQntuples(res); i++)
5890  {
5891  const char align = 'l';
5892  int ncols = 5;
5893  int nrows = 1;
5894  int tables = 0;
5895  PGresult *tabres;
5896  char *pubid = PQgetvalue(res, i, 0);
5897  char *pubname = PQgetvalue(res, i, 1);
5898  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
5899  int j;
5900  PQExpBufferData title;
5901  printTableOpt myopt = pset.popt.topt;
5902  printTableContent cont;
5903 
5904  if (has_pubtruncate)
5905  ncols++;
5906  if (has_pubviaroot)
5907  ncols++;
5908 
5909  initPQExpBuffer(&title);
5910  printfPQExpBuffer(&title, _("Publication %s"), pubname);
5911  printTableInit(&cont, &myopt, title.data, ncols, nrows);
5912 
5913  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
5914  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
5915  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
5916  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
5917  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
5918  if (has_pubtruncate)
5919  printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
5920  if (has_pubviaroot)
5921  printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
5922 
5923  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
5924  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
5925  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
5926  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
5927  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
5928  if (has_pubtruncate)
5929  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
5930  if (has_pubviaroot)
5931  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
5932 
5933  if (!puballtables)
5934  {
5935  printfPQExpBuffer(&buf,
5936  "SELECT n.nspname, c.relname\n"
5937  "FROM pg_catalog.pg_class c,\n"
5938  " pg_catalog.pg_namespace n,\n"
5939  " pg_catalog.pg_publication_rel pr\n"
5940  "WHERE c.relnamespace = n.oid\n"
5941  " AND c.oid = pr.prrelid\n"
5942  " AND pr.prpubid = '%s'\n"
5943  "ORDER BY 1,2", pubid);
5944 
5945  tabres = PSQLexec(buf.data);
5946  if (!tabres)
5947  {
5948  printTableCleanup(&cont);
5949  PQclear(res);
5950  termPQExpBuffer(&buf);
5951  termPQExpBuffer(&title);
5952  return false;
5953  }
5954  else
5955  tables = PQntuples(tabres);
5956 
5957  if (tables > 0)
5958  printTableAddFooter(&cont, _("Tables:"));
5959 
5960  for (j = 0; j < tables; j++)
5961  {
5962  printfPQExpBuffer(&buf, " \"%s.%s\"",
5963  PQgetvalue(tabres, j, 0),
5964  PQgetvalue(tabres, j, 1));
5965 
5966  printTableAddFooter(&cont, buf.data);
5967  }
5968  PQclear(tabres);
5969  }
5970 
5971  printTable(&cont, pset.queryFout, false, pset.logfile);
5972  printTableCleanup(&cont);
5973 
5974  termPQExpBuffer(&title);
5975  }
5976 
5977  termPQExpBuffer(&buf);
5978  PQclear(res);
5979 
5980  return true;
5981 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PsqlSettings pset
Definition: startup.c:32
void printTableCleanup(printTableContent *const content)
Definition: print.c:3225
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:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
printTableOpt topt
Definition: print.h:169
#define gettext_noop(x)
Definition: c.h:1137
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:3094
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3134
static char * buf
Definition: pg_test_fsync.c:67
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3314
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3182
FILE * logfile
Definition: settings.h:116
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:3057
int i
#define _(x)
Definition: elog.c:88
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:536

◆ describeRoles()

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

Definition at line 3427 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().

3428 {
3430  PGresult *res;
3431  printTableContent cont;
3432  printTableOpt myopt = pset.popt.topt;
3433  int ncols = 3;
3434  int nrows = 0;
3435  int i;
3436  int conns;
3437  const char align = 'l';
3438  char **attr;
3439 
3440  myopt.default_footer = false;
3441 
3442  initPQExpBuffer(&buf);
3443 
3444  if (pset.sversion >= 80100)
3445  {
3446  printfPQExpBuffer(&buf,
3447  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3448  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3449  " r.rolconnlimit, r.rolvaliduntil,\n"
3450  " ARRAY(SELECT b.rolname\n"
3451  " FROM pg_catalog.pg_auth_members m\n"
3452  " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
3453  " WHERE m.member = r.oid) as memberof");
3454 
3455  if (verbose && pset.sversion >= 80200)
3456  {
3457  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3458  ncols++;
3459  }
3460  if (pset.sversion >= 90100)
3461  {
3462  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3463  }
3464 
3465  if (pset.sversion >= 90500)
3466  {
3467  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3468  }
3469 
3470  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3471 
3472  if (!showSystem && !pattern)
3473  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3474 
3475  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3476  NULL, "r.rolname", NULL, NULL);
3477  }
3478  else
3479  {
3480  printfPQExpBuffer(&buf,
3481  "SELECT u.usename AS rolname,\n"
3482  " u.usesuper AS rolsuper,\n"
3483  " true AS rolinherit, false AS rolcreaterole,\n"
3484  " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
3485  " -1 AS rolconnlimit,"
3486  " u.valuntil as rolvaliduntil,\n"
3487  " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
3488  "\nFROM pg_catalog.pg_user u\n");
3489 
3490  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3491  NULL, "u.usename", NULL, NULL);
3492  }
3493 
3494  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3495 
3496  res = PSQLexec(buf.data);
3497  if (!res)
3498  return false;
3499 
3500  nrows = PQntuples(res);
3501  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3502 
3503  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3504 
3505  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3506  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3507  printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
3508 
3509  if (verbose && pset.sversion >= 80200)
3510  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3511 
3512  for (i = 0; i < nrows; i++)
3513  {
3514  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3515 
3516  resetPQExpBuffer(&buf);
3517  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3518  add_role_attribute(&buf, _("Superuser"));
3519 
3520  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3521  add_role_attribute(&buf, _("No inheritance"));
3522 
3523  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3524  add_role_attribute(&buf, _("Create role"));
3525 
3526  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3527  add_role_attribute(&buf, _("Create DB"));
3528 
3529  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3530  add_role_attribute(&buf, _("Cannot login"));
3531 
3532  if (pset.sversion >= 90100)
3533  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3534  add_role_attribute(&buf, _("Replication"));
3535 
3536  if (pset.sversion >= 90500)
3537  if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
3538  add_role_attribute(&buf, _("Bypass RLS"));
3539 
3540  conns = atoi(PQgetvalue(res, i, 6));
3541  if (conns >= 0)
3542  {
3543  if (buf.len > 0)
3544  appendPQExpBufferChar(&buf, '\n');
3545 
3546  if (conns == 0)
3547  appendPQExpBufferStr(&buf, _("No connections"));
3548  else
3549  appendPQExpBuffer(&buf, ngettext("%d connection",
3550  "%d connections",
3551  conns),
3552  conns);
3553  }
3554 
3555  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3556  {
3557  if (buf.len > 0)
3558  appendPQExpBufferChar(&buf, '\n');
3559  appendPQExpBufferStr(&buf, _("Password valid until "));
3560  appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3561  }
3562 
3563  attr[i] = pg_strdup(buf.data);
3564 
3565  printTableAddCell(&cont, attr[i], false, false);
3566 
3567  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3568 
3569  if (verbose && pset.sversion >= 80200)
3570  printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
3571  }
3572  termPQExpBuffer(&buf);
3573 
3574  printTable(&cont, pset.queryFout, false, pset.logfile);
3575  printTableCleanup(&cont);
3576 
3577  for (i = 0; i < nrows; i++)
3578  free(attr[i]);
3579  free(attr);
3580 
3581  PQclear(res);
3582  return true;
3583 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PsqlSettings pset
Definition: startup.c:32
void printTableCleanup(printTableContent *const content)
Definition: print.c:3225
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:169
#define gettext_noop(x)
Definition: c.h:1137
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:3586
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3094
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:3134
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:3314
static int verbose
#define ngettext(s, p, n)
Definition: c.h:1123
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
bool default_footer
Definition: print.h:113
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:3057
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:536

◆ describeSubscriptions()

bool describeSubscriptions ( const char *  pattern,
bool  verbose 
)

Definition at line 5990 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().

5991 {
5993  PGresult *res;
5994  printQueryOpt myopt = pset.popt;
5995  static const bool translate_columns[] = {false, false, false, false,
5996  false, false, false, false};
5997 
5998  if (pset.sversion < 100000)
5999  {
6000  char sverbuf[32];
6001 
6002  pg_log_error("The server (version %s) does not support subscriptions.",
6004  sverbuf, sizeof(sverbuf)));
6005  return true;
6006  }
6007 
6008  initPQExpBuffer(&buf);
6009 
6010  printfPQExpBuffer(&buf,
6011  "SELECT subname AS \"%s\"\n"
6012  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
6013  ", subenabled AS \"%s\"\n"
6014  ", subpublications AS \"%s\"\n",
6015  gettext_noop("Name"),
6016  gettext_noop("Owner"),
6017  gettext_noop("Enabled"),
6018  gettext_noop("Publication"));
6019 
6020  if (verbose)
6021  {
6022  /* Binary mode and streaming are only supported in v14 and higher */
6023  if (pset.sversion >= 140000)
6024  appendPQExpBuffer(&buf,
6025  ", subbinary AS \"%s\"\n"
6026  ", substream AS \"%s\"\n",
6027  gettext_noop("Binary"),
6028  gettext_noop("Streaming"));
6029 
6030  appendPQExpBuffer(&buf,
6031  ", subsynccommit AS \"%s\"\n"
6032  ", subconninfo AS \"%s\"\n",
6033  gettext_noop("Synchronous commit"),
6034  gettext_noop("Conninfo"));
6035  }
6036 
6037  /* Only display subscriptions in current database. */
6038  appendPQExpBufferStr(&buf,
6039  "FROM pg_catalog.pg_subscription\n"
6040  "WHERE subdbid = (SELECT oid\n"
6041  " FROM pg_catalog.pg_database\n"
6042  " WHERE datname = pg_catalog.current_database())");
6043 
6044  processSQLNamePattern(pset.db, &buf, pattern, true, false,
6045  NULL, "subname", NULL,
6046  NULL);
6047 
6048  appendPQExpBufferStr(&buf, "ORDER BY 1;");
6049 
6050  res = PSQLexec(buf.data);
6051  termPQExpBuffer(&buf);
6052  if (!res)
6053  return false;
6054 
6055  myopt.nullPrint = NULL;
6056  myopt.title = _("List of subscriptions");
6057  myopt.translate_header = true;
6058  myopt.translate_columns = translate_columns;
6059  myopt.n_translate_columns = lengthof(translate_columns);
6060 
6061  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
6062 
6063  PQclear(res);
6064  return true;
6065 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1137
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:675
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:173
static int verbose
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:176
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
const bool * translate_columns
Definition: print.h:174
#define _(x)
Definition: elog.c:88
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:536

◆ describeTableDetails()

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

Definition at line 1371 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().

1372 {
1374  PGresult *res;
1375  int i;
1376 
1377  initPQExpBuffer(&buf);
1378 
1379  printfPQExpBuffer(&buf,
1380  "SELECT c.oid,\n"
1381  " n.nspname,\n"
1382  " c.relname\n"
1383  "FROM pg_catalog.pg_class c\n"
1384  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1385 
1386  if (!showSystem && !pattern)
1387  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1388  " AND n.nspname <> 'information_schema'\n");
1389 
1390  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1391  "n.nspname", "c.relname", NULL,
1392  "pg_catalog.pg_table_is_visible(c.oid)");
1393 
1394  appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1395 
1396  res = PSQLexec(buf.data);
1397  termPQExpBuffer(&buf);
1398  if (!res)
1399  return false;
1400 
1401  if (PQntuples(res) == 0)
1402  {
1403  if (!pset.quiet)
1404  {
1405  if (pattern)
1406  pg_log_error("Did not find any relation named \"%s\".",
1407  pattern);
1408  else
1409  pg_log_error("Did not find any relations.");
1410  }
1411  PQclear(res);
1412  return false;
1413  }
1414 
1415  for (i = 0; i < PQntuples(res); i++)
1416  {
1417  const char *oid;
1418  const char *nspname;
1419  const char *relname;
1420 
1421  oid = PQgetvalue(res, i, 0);
1422  nspname = PQgetvalue(res, i, 1);
1423  relname = PQgetvalue(res, i, 2);
1424 
1425  if (!describeOneTableDetails(nspname, relname, oid, verbose))
1426  {
1427  PQclear(res);
1428  return false;
1429  }
1430  if (cancel_pressed)
1431  {
1432  PQclear(res);
1433  return false;
1434  }
1435  }
1436 
1437  PQclear(res);
1438  return true;
1439 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PsqlSettings pset
Definition: startup.c:32
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:80
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:38
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
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:1449
static int verbose
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
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:536

◆ describeTablespaces()

bool describeTablespaces ( const char *  pattern,
bool  verbose 
)

Definition at line 219 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().

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

◆ describeTypes()

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

Definition at line 667 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().

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

◆ listAllDbs()

bool listAllDbs ( const char *  pattern,
bool  verbose 
)

Definition at line 864 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().

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

◆ listCasts()

bool listCasts ( const char *  pattern,
bool  verbose 
)

Definition at line 4406 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().

4407 {
4409  PGresult *res;
4410  printQueryOpt myopt = pset.popt;
4411  static const bool translate_columns[] = {false, false, false, true, false};
4412 
4413  initPQExpBuffer(&buf);
4414 
4415  printfPQExpBuffer(&buf,
4416  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
4417  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n",
4418  gettext_noop("Source type"),
4419  gettext_noop("Target type"));
4420 
4421  /*
4422  * We don't attempt to localize '(binary coercible)' or '(with inout)',
4423  * because there's too much risk of gettext translating a function name
4424  * that happens to match some string in the PO database.
4425  */
4426  if (pset.sversion >= 80400)
4427  appendPQExpBuffer(&buf,
4428  " CASE WHEN c.castmethod = '%c' THEN '(binary coercible)'\n"
4429  " WHEN c.castmethod = '%c' THEN '(with inout)'\n"
4430  " ELSE p.proname\n"
4431  " END AS \"%s\",\n",
4432  COERCION_METHOD_BINARY,
4433  COERCION_METHOD_INOUT,
4434  gettext_noop("Function"));
4435  else
4436  appendPQExpBuffer(&buf,
4437  " CASE WHEN c.castfunc = 0 THEN '(binary coercible)'\n"
4438  " ELSE p.proname\n"
4439  " END AS \"%s\",\n",
4440  gettext_noop("Function"));
4441 
4442  appendPQExpBuffer(&buf,
4443  " CASE WHEN c.castcontext = '%c' THEN '%s'\n"
4444  " WHEN c.castcontext = '%c' THEN '%s'\n"
4445  " ELSE '%s'\n"
4446  " END AS \"%s\"",
4447  COERCION_CODE_EXPLICIT,
4448  gettext_noop("no"),
4449  COERCION_CODE_ASSIGNMENT,
4450  gettext_noop("in assignment"),
4451  gettext_noop("yes"),
4452  gettext_noop("Implicit?"));
4453 
4454  if (verbose)
4455  appendPQExpBuffer(&buf,
4456  ",\n d.description AS \"%s\"",
4457  gettext_noop("Description"));
4458 
4459  /*
4460  * We need a left join to pg_proc for binary casts; the others are just
4461  * paranoia.
4462  */
4463  appendPQExpBufferStr(&buf,
4464  "\nFROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
4465  " ON c.castfunc = p.oid\n"
4466  " LEFT JOIN pg_catalog.pg_type ts\n"
4467  " ON c.castsource = ts.oid\n"
4468  " LEFT JOIN pg_catalog.pg_namespace ns\n"
4469  " ON ns.oid = ts.typnamespace\n"
4470  " LEFT JOIN pg_catalog.pg_type tt\n"
4471  " ON c.casttarget = tt.oid\n"
4472  " LEFT JOIN pg_catalog.pg_namespace nt\n"
4473  " ON nt.oid = tt.typnamespace\n");
4474 
4475  if (verbose)
4476  appendPQExpBufferStr(&buf,
4477  " LEFT JOIN pg_catalog.pg_description d\n"
4478  " ON d.classoid = c.tableoid AND d.objoid = "
4479  "c.oid AND d.objsubid = 0\n");
4480 
4481  appendPQExpBufferStr(&buf, "WHERE ( (true");
4482 
4483  /*
4484  * Match name pattern against either internal or external name of either
4485  * castsource or casttarget
4486  */
4487  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4488  "ns.nspname", "ts.typname",
4489  "pg_catalog.format_type(ts.oid, NULL)",
4490  "pg_catalog.pg_type_is_visible(ts.oid)");
4491 
4492  appendPQExpBufferStr(&buf, ") OR (true");
4493 
4494  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4495  "nt.nspname", "tt.typname",
4496  "pg_catalog.format_type(tt.oid, NULL)",
4497  "pg_catalog.pg_type_is_visible(tt.oid)");
4498 
4499  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
4500 
4501  res = PSQLexec(buf.data);
4502  termPQExpBuffer(&buf);
4503  if (!res)
4504  return false;
4505 
4506  myopt.nullPrint = NULL;
4507  myopt.title = _("List of casts");
4508  myopt.translate_header = true;
4509  myopt.translate_columns = translate_columns;
4510  myopt.n_translate_columns = lengthof(translate_columns);
4511 
4512  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4513 
4514  PQclear(res);
4515  return true;
4516 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
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:1137
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:675
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:173
static int verbose
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:176
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
const bool * translate_columns
Definition: print.h:174
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:536

◆ listCollations()

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

Definition at line 4524 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().

4525 {
4527  PGresult *res;
4528  printQueryOpt myopt = pset.popt;
4529  static const bool translate_columns[] = {false, false, false, false, false, true, false};
4530 
4531  if (pset.sversion < 90100)
4532  {
4533  char sverbuf[32];
4534 
4535  pg_log_error("The server (version %s) does not support collations.",
4537  sverbuf, sizeof(sverbuf)));
4538  return true;
4539  }
4540 
4541  initPQExpBuffer(&buf);
4542 
4543  printfPQExpBuffer(&buf,
4544  "SELECT n.nspname AS \"%s\",\n"
4545  " c.collname AS \"%s\",\n"
4546  " c.collcollate AS \"%s\",\n"
4547  " c.collctype AS \"%s\"",
4548  gettext_noop("Schema"),
4549  gettext_noop("Name"),
4550  gettext_noop("Collate"),
4551  gettext_noop("Ctype"));
4552 
4553  if (pset.sversion >= 100000)
4554  appendPQExpBuffer(&buf,
4555  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
4556  gettext_noop("Provider"));
4557  else
4558  appendPQExpBuffer(&buf,
4559  ",\n 'libc' AS \"%s\"",
4560  gettext_noop("Provider"));
4561 
4562  if (pset.sversion >= 120000)
4563  appendPQExpBuffer(&buf,
4564  ",\n CASE WHEN c.collisdeterministic THEN '%s' ELSE '%s' END AS \"%s\"",
4565  gettext_noop("yes"), gettext_noop("no"),
4566  gettext_noop("Deterministic?"));
4567  else
4568  appendPQExpBuffer(&buf,
4569  ",\n '%s' AS \"%s\"",
4570  gettext_noop("yes"),
4571  gettext_noop("Deterministic?"));
4572 
4573  if (verbose)
4574  appendPQExpBuffer(&buf,
4575  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
4576  gettext_noop("Description"));
4577 
4578  appendPQExpBufferStr(&buf,
4579  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
4580  "WHERE n.oid = c.collnamespace\n");
4581 
4582  if (!showSystem && !pattern)
4583  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4584  " AND n.nspname <> 'information_schema'\n");
4585 
4586  /*
4587  * Hide collations that aren't usable in the current database's encoding.
4588  * If you think to change this, note that pg_collation_is_visible rejects
4589  * unusable collations, so you will need to hack name pattern processing
4590  * somehow to avoid inconsistent behavior.
4591  */
4592  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
4593 
4594  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4595  "n.nspname", "c.collname", NULL,
4596  "pg_catalog.pg_collation_is_visible(c.oid)");
4597 
4598  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4599 
4600  res = PSQLexec(buf.data);
4601  termPQExpBuffer(&buf);
4602  if (!res)
4603  return false;
4604 
4605  myopt.nullPrint = NULL;
4606  myopt.title = _("List of collations");
4607  myopt.translate_header = true;
4608  myopt.translate_columns = translate_columns;
4609  myopt.n_translate_columns = lengthof(translate_columns);
4610 
4611  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4612 
4613  PQclear(res);
4614  return true;
4615 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1137
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:675
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:173
static int verbose
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:176
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
const bool * translate_columns
Definition: print.h:174
#define _(x)
Definition: elog.c:88
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:536

◆ listConversions()

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

Definition at line 4264 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().

4265 {
4267  PGresult *res;
4268  printQueryOpt myopt = pset.popt;
4269  static const bool translate_columns[] =
4270  {false, false, false, false, true, false};
4271 
4272  initPQExpBuffer(&buf);
4273 
4274  printfPQExpBuffer(&buf,
4275  "SELECT n.nspname AS \"%s\",\n"
4276  " c.conname AS \"%s\",\n"
4277  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
4278  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
4279  " CASE WHEN c.condefault THEN '%s'\n"
4280  " ELSE '%s' END AS \"%s\"",
4281  gettext_noop("Schema"),
4282  gettext_noop("Name"),
4283  gettext_noop("Source"),
4284  gettext_noop("Destination"),
4285  gettext_noop("yes"), gettext_noop("no"),
4286  gettext_noop("Default?"));
4287 
4288  if (verbose)
4289  appendPQExpBuffer(&buf,
4290  ",\n d.description AS \"%s\"",
4291  gettext_noop("Description"));
4292 
4293  appendPQExpBufferStr(&buf,
4294  "\nFROM pg_catalog.pg_conversion c\n"
4295  " JOIN pg_catalog.pg_namespace n "
4296  "ON n.oid = c.connamespace\n");
4297 
4298  if (verbose)
4299  appendPQExpBufferStr(&buf,
4300  "LEFT JOIN pg_catalog.pg_description d "
4301  "ON d.classoid = c.tableoid\n"
4302  " AND d.objoid = c.oid "
4303  "AND d.objsubid = 0\n");
4304 
4305  appendPQExpBufferStr(&buf, "WHERE true\n");
4306 
4307  if (!showSystem && !pattern)
4308  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
4309  " AND n.nspname <> 'information_schema'\n");
4310 
4311  processSQLNamePattern(pset.db, &buf, pattern, true, false,
4312  "n.nspname", "c.conname", NULL,
4313  "pg_catalog.pg_conversion_is_visible(c.oid)");
4314 
4315  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
4316 
4317  res = PSQLexec(buf.data);
4318  termPQExpBuffer(&buf);
4319  if (!res)
4320  return false;
4321 
4322  myopt.nullPrint = NULL;
4323  myopt.title = _("List of conversions");
4324  myopt.translate_header = true;
4325  myopt.translate_columns = translate_columns;
4326  myopt.n_translate_columns = lengthof(translate_columns);
4327 
4328  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4329 
4330  PQclear(res);
4331  return true;
4332 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
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:1137
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:675
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:173
static int verbose
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:176
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
const bool * translate_columns
Definition: print.h:174
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGresult * PSQLexec(const char *query)
Definition: common.c:536

◆ listDbRoleSettings()

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

Definition at line 3598 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().

3599 {
3601  PGresult *res;
3602  printQueryOpt myopt = pset.popt;
3603  bool havewhere;
3604 
3605  if (pset.sversion < 90000)
3606  {
3607  char sverbuf[32];
3608 
3609  pg_log_error("The server (version %s) does not support per-database role settings.",
3611  sverbuf, sizeof(sverbuf)));
3612  return true;
3613  }
3614 
3615  initPQExpBuffer(&buf);
3616 
3617  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3618  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3619  "FROM pg_catalog.pg_db_role_setting s\n"
3620  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3621  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3622  gettext_noop("Role"),
3623  gettext_noop("Database"),
3624  gettext_noop("Settings"));
3625  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3626  NULL, "r.rolname", NULL, NULL);
3627  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3628  NULL, "d.datname", NULL, NULL);
3629  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3630 
3631  res = PSQLexec(buf.data);
3632  termPQExpBuffer(&buf);
3633  if (!res)
3634  return false;
3635 
3636  /*
3637  * Most functions in this file are content to print an empty table when
3638  * there are no matching objects. We intentionally deviate from that
3639  * here, but only in !quiet mode, because of the possibility that the user
3640  * is confused about what the two pattern arguments mean.
3641  */
3642  if (PQntuples(res) == 0 && !pset.quiet)
3643  {
3644  if (pattern && pattern2)
3645  pg_log_error("Did not find any settings for role \"%s\" and database \"%s\".",
3646  pattern, pattern2);
3647  else if (pattern)
3648  pg_log_error("Did not find any settings for role \"%s\".",
3649  pattern);
3650  else
3651  pg_log_error("Did not find any settings.");
3652  }
3653  else
3654  {
3655  myopt.nullPrint = NULL;
3656  myopt.title = _("List of settings");
3657  myopt.translate_header = true;
3658 
3659  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3660  }
3661 
3662  PQclear(res);
3663  return true;
3664 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define gettext_noop(x)
Definition: c.h:1137
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:173
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3417
#define _(x)
Definition: elog.c:88
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:536

◆ listDefaultACLs()

bool listDefaultACLs ( const char *  pattern)

Definition at line 1094 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().

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

◆ listDomains()