PostgreSQL Source Code  git master
describe.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include "catalog/pg_attribute.h"
#include "catalog/pg_class.h"
#include "catalog/pg_default_acl.h"
#include "fe_utils/string_utils.h"
#include "common.h"
#include "describe.h"
#include "fe_utils/mbprint.h"
#include "fe_utils/print.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 listLanguages (const char *pattern, bool verbose, bool showSystem)
 
bool listDomains (const char *pattern, bool verbose, bool showSystem)
 
bool listConversions (const char *pattern, bool verbose, bool showSystem)
 
bool listEventTriggers (const char *pattern, bool verbose)
 
bool listCasts (const char *pattern, bool verbose)
 
bool listCollations (const char *pattern, bool verbose, bool showSystem)
 
bool listSchemas (const char *pattern, bool verbose, bool showSystem)
 
bool listTSParsers (const char *pattern, bool verbose)
 
bool listTSDictionaries (const char *pattern, bool verbose)
 
bool listTSTemplates (const char *pattern, bool verbose)
 
bool listTSConfigs (const char *pattern, bool verbose)
 
bool listForeignDataWrappers (const char *pattern, bool verbose)
 
bool listForeignServers (const char *pattern, bool verbose)
 
bool listUserMappings (const char *pattern, bool verbose)
 
bool listForeignTables (const char *pattern, bool verbose)
 
bool listExtensions (const char *pattern)
 
bool listExtensionContents (const char *pattern)
 
bool listPublications (const char *pattern)
 
bool describePublications (const char *pattern)
 
bool describeSubscriptions (const char *pattern, bool verbose)
 

Function Documentation

◆ add_role_attribute()

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

Definition at line 3268 of file describe.c.

References appendPQExpBufferStr(), and PQExpBufferData::len.

Referenced by describeRoles().

3269 {
3270  if (buf->len > 0)
3271  appendPQExpBufferStr(buf, ", ");
3272 
3273  appendPQExpBufferStr(buf, str);
3274 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385

◆ add_tablespace_footer()

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

Definition at line 3045 of file describe.c.

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

Referenced by describeOneTableDetails().

3047 {
3048  /* relkinds for which we support tablespaces */
3049  if (relkind == RELKIND_RELATION ||
3050  relkind == RELKIND_MATVIEW ||
3051  relkind == RELKIND_INDEX ||
3052  relkind == RELKIND_PARTITIONED_TABLE)
3053  {
3054  /*
3055  * We ignore the database default tablespace so that users not using
3056  * tablespaces don't need to know about them. This case also covers
3057  * pre-8.0 servers, for which tablespace will always be 0.
3058  */
3059  if (tablespace != 0)
3060  {
3061  PGresult *result = NULL;
3063 
3064  initPQExpBuffer(&buf);
3065  printfPQExpBuffer(&buf,
3066  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3067  "WHERE oid = '%u';", tablespace);
3068  result = PSQLexec(buf.data);
3069  if (!result)
3070  {
3071  termPQExpBuffer(&buf);
3072  return;
3073  }
3074  /* Should always be the case, but.... */
3075  if (PQntuples(result) > 0)
3076  {
3077  if (newline)
3078  {
3079  /* Add the tablespace as a new footer */
3080  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3081  PQgetvalue(result, 0, 0));
3082  printTableAddFooter(cont, buf.data);
3083  }
3084  else
3085  {
3086  /* Append the tablespace to the latest footer */
3087  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3088 
3089  /*-------
3090  translator: before this string there's an index description like
3091  '"foo_pkey" PRIMARY KEY, btree (a)' */
3092  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3093  PQgetvalue(result, 0, 0));
3094  printTableSetFooter(cont, buf.data);
3095  }
3096  }
3097  PQclear(result);
3098  termPQExpBuffer(&buf);
3099  }
3100  }
3101 }
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3083
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
#define RELKIND_MATVIEW
Definition: pg_class.h:165
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static chr newline(void)
Definition: regc_lex.c:1137
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
char * tablespace
Definition: pgbench.c:143
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3058
printTableFooter * footer
Definition: print.h:157
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * data
Definition: print.h:135
#define RELKIND_INDEX
Definition: pg_class.h:161
#define RELKIND_RELATION
Definition: pg_class.h:160
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeAccessMethods()

bool describeAccessMethods ( const char *  pattern,
bool  verbose 
)

Definition at line 140 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, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

141 {
143  PGresult *res;
144  printQueryOpt myopt = pset.popt;
145  static const bool translate_columns[] = {false, true, false, false};
146 
147  if (pset.sversion < 90600)
148  {
149  char sverbuf[32];
150 
151  psql_error("The server (version %s) does not support access methods.\n",
153  sverbuf, sizeof(sverbuf)));
154  return true;
155  }
156 
157  initPQExpBuffer(&buf);
158 
159  printfPQExpBuffer(&buf,
160  "SELECT amname AS \"%s\",\n"
161  " CASE amtype"
162  " WHEN 'i' THEN '%s'"
163  " END AS \"%s\"",
164  gettext_noop("Name"),
165  gettext_noop("Index"),
166  gettext_noop("Type"));
167 
168  if (verbose)
169  {
170  appendPQExpBuffer(&buf,
171  ",\n amhandler AS \"%s\",\n"
172  " pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
173  gettext_noop("Handler"),
174  gettext_noop("Description"));
175  }
176 
178  "\nFROM pg_catalog.pg_am\n");
179 
180  processSQLNamePattern(pset.db, &buf, pattern, false, false,
181  NULL, "amname", NULL,
182  NULL);
183 
184  appendPQExpBufferStr(&buf, "ORDER BY 1;");
185 
186  res = PSQLexec(buf.data);
187  termPQExpBuffer(&buf);
188  if (!res)
189  return false;
190 
191  myopt.nullPrint = NULL;
192  myopt.title = _("List of access methods");
193  myopt.translate_header = true;
194  myopt.translate_columns = translate_columns;
195  myopt.n_translate_columns = lengthof(translate_columns);
196 
197  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
198 
199  PQclear(res);
200  return true;
201 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeAggregates()

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

Definition at line 62 of file describe.c.

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

Referenced by exec_command_d().

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

◆ describeFunctions()

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

Definition at line 303 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, _psqlSettings::popt, PQclear(), printACLColumn(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

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

◆ describeOneTableDetails()

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

Definition at line 1372 of file describe.c.

References _, add_tablespace_footer(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), atooid, ATTRIBUTE_IDENTITY_ALWAYS, ATTRIBUTE_IDENTITY_BY_DEFAULT, buf, CppAsString2, PQExpBufferData::data, printTableOpt::default_footer, _psqlSettings::encoding, printTableOpt::expanded, fmtId(), printQueryOpt::footers, free, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, pg_strdup(), pg_wcswidth(), _psqlSettings::popt, PQclear(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), printfPQExpBuffer(), printQuery(), printTable(), printTableAddCell(), printTableAddFooter(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, psprintf(), psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, RELKIND_COMPOSITE_TYPE, RELKIND_FOREIGN_TABLE, RELKIND_INDEX, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_TOASTVALUE, RELKIND_VIEW, resetPQExpBuffer(), _psqlSettings::sversion, tablespace, termPQExpBuffer(), printQueryOpt::title, tmpbuf, printQueryOpt::topt, and printQueryOpt::translate_header.

Referenced by describeTableDetails().

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

◆ describeOneTSConfig()

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

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

4595 {
4597  title;
4598  PGresult *res;
4599  printQueryOpt myopt = pset.popt;
4600 
4601  initPQExpBuffer(&buf);
4602 
4603  printfPQExpBuffer(&buf,
4604  "SELECT\n"
4605  " ( SELECT t.alias FROM\n"
4606  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
4607  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
4608  " pg_catalog.btrim(\n"
4609  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
4610  " FROM pg_catalog.pg_ts_config_map AS mm\n"
4611  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
4612  " ORDER BY mapcfg, maptokentype, mapseqno\n"
4613  " ) :: pg_catalog.text,\n"
4614  " '{}') AS \"%s\"\n"
4615  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
4616  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
4617  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
4618  "ORDER BY 1;",
4619  gettext_noop("Token"),
4620  gettext_noop("Dictionaries"),
4621  oid);
4622 
4623  res = PSQLexec(buf.data);
4624  termPQExpBuffer(&buf);
4625  if (!res)
4626  return false;
4627 
4628  initPQExpBuffer(&title);
4629 
4630  if (nspname)
4631  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4632  nspname, cfgname);
4633  else
4634  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4635  cfgname);
4636 
4637  if (pnspname)
4638  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4639  pnspname, prsname);
4640  else
4641  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4642  prsname);
4643 
4644  myopt.nullPrint = NULL;
4645  myopt.title = title.data;
4646  myopt.footers = NULL;
4647  myopt.topt.default_footer = false;
4648  myopt.translate_header = true;
4649 
4650  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4651 
4652  termPQExpBuffer(&title);
4653 
4654  PQclear(res);
4655  return true;
4656 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
printTableOpt topt
Definition: print.h:165
#define gettext_noop(x)
Definition: c.h:991
FILE * queryFout
Definition: settings.h:84
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
char ** footers
Definition: print.h:168
bool default_footer
Definition: print.h:110
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeOneTSParser()

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

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

4199 {
4201  PGresult *res;
4202  PQExpBufferData title;
4203  printQueryOpt myopt = pset.popt;
4204  static const bool translate_columns[] = {true, false, false};
4205 
4206  initPQExpBuffer(&buf);
4207 
4208  printfPQExpBuffer(&buf,
4209  "SELECT '%s' AS \"%s\",\n"
4210  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4211  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4212  " FROM pg_catalog.pg_ts_parser p\n"
4213  " WHERE p.oid = '%s'\n"
4214  "UNION ALL\n"
4215  "SELECT '%s',\n"
4216  " p.prstoken::pg_catalog.regproc,\n"
4217  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4218  " FROM pg_catalog.pg_ts_parser p\n"
4219  " WHERE p.oid = '%s'\n"
4220  "UNION ALL\n"
4221  "SELECT '%s',\n"
4222  " p.prsend::pg_catalog.regproc,\n"
4223  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4224  " FROM pg_catalog.pg_ts_parser p\n"
4225  " WHERE p.oid = '%s'\n"
4226  "UNION ALL\n"
4227  "SELECT '%s',\n"
4228  " p.prsheadline::pg_catalog.regproc,\n"
4229  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4230  " FROM pg_catalog.pg_ts_parser p\n"
4231  " WHERE p.oid = '%s'\n"
4232  "UNION ALL\n"
4233  "SELECT '%s',\n"
4234  " p.prslextype::pg_catalog.regproc,\n"
4235  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4236  " FROM pg_catalog.pg_ts_parser p\n"
4237  " WHERE p.oid = '%s';",
4238  gettext_noop("Start parse"),
4239  gettext_noop("Method"),
4240  gettext_noop("Function"),
4241  gettext_noop("Description"),
4242  oid,
4243  gettext_noop("Get next token"),
4244  oid,
4245  gettext_noop("End parse"),
4246  oid,
4247  gettext_noop("Get headline"),
4248  oid,
4249  gettext_noop("Get token types"),
4250  oid);
4251 
4252  res = PSQLexec(buf.data);
4253  termPQExpBuffer(&buf);
4254  if (!res)
4255  return false;
4256 
4257  myopt.nullPrint = NULL;
4258  initPQExpBuffer(&title);
4259  if (nspname)
4260  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4261  nspname, prsname);
4262  else
4263  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4264  myopt.title = title.data;
4265  myopt.footers = NULL;
4266  myopt.topt.default_footer = false;
4267  myopt.translate_header = true;
4268  myopt.translate_columns = translate_columns;
4269  myopt.n_translate_columns = lengthof(translate_columns);
4270 
4271  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4272 
4273  PQclear(res);
4274 
4275  initPQExpBuffer(&buf);
4276 
4277  printfPQExpBuffer(&buf,
4278  "SELECT t.alias as \"%s\",\n"
4279  " t.description as \"%s\"\n"
4280  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4281  "ORDER BY 1;",
4282  gettext_noop("Token name"),
4283  gettext_noop("Description"),
4284  oid);
4285 
4286  res = PSQLexec(buf.data);
4287  termPQExpBuffer(&buf);
4288  if (!res)
4289  return false;
4290 
4291  myopt.nullPrint = NULL;
4292  if (nspname)
4293  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4294  nspname, prsname);
4295  else
4296  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4297  myopt.title = title.data;
4298  myopt.footers = NULL;
4299  myopt.topt.default_footer = true;
4300  myopt.translate_header = true;
4301  myopt.translate_columns = NULL;
4302  myopt.n_translate_columns = 0;
4303 
4304  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4305 
4306  termPQExpBuffer(&title);
4307  PQclear(res);
4308  return true;
4309 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
printTableOpt topt
Definition: print.h:165
#define gettext_noop(x)
Definition: c.h:991
FILE * queryFout
Definition: settings.h:84
#define lengthof(array)
Definition: c.h:610
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
char ** footers
Definition: print.h:168
bool default_footer
Definition: print.h:110
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeOperators()

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

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

711 {
713  PGresult *res;
714  printQueryOpt myopt = pset.popt;
715 
716  initPQExpBuffer(&buf);
717 
718  /*
719  * Note: before Postgres 9.1, we did not assign comments to any built-in
720  * operators, preferring to let the comment on the underlying function
721  * suffice. The coalesce() on the obj_description() calls below supports
722  * this convention by providing a fallback lookup of a comment on the
723  * operator's function. As of 9.1 there is a policy that every built-in
724  * operator should have a comment; so the coalesce() is no longer
725  * necessary so far as built-in operators are concerned. We keep it
726  * anyway, for now, because (1) third-party modules may still be following
727  * the old convention, and (2) we'd need to do it anyway when talking to a
728  * pre-9.1 server.
729  */
730 
731  printfPQExpBuffer(&buf,
732  "SELECT n.nspname as \"%s\",\n"
733  " o.oprname AS \"%s\",\n"
734  " CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
735  " CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
736  " pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n",
737  gettext_noop("Schema"),
738  gettext_noop("Name"),
739  gettext_noop("Left arg type"),
740  gettext_noop("Right arg type"),
741  gettext_noop("Result type"));
742 
743  if (verbose)
744  appendPQExpBuffer(&buf,
745  " o.oprcode AS \"%s\",\n",
746  gettext_noop("Function"));
747 
748  appendPQExpBuffer(&buf,
749  " coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
750  " pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
751  "FROM pg_catalog.pg_operator o\n"
752  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
753  gettext_noop("Description"));
754 
755  if (!showSystem && !pattern)
756  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
757  " AND n.nspname <> 'information_schema'\n");
758 
759  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true,
760  "n.nspname", "o.oprname", NULL,
761  "pg_catalog.pg_operator_is_visible(o.oid)");
762 
763  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;");
764 
765  res = PSQLexec(buf.data);
766  termPQExpBuffer(&buf);
767  if (!res)
768  return false;
769 
770  myopt.nullPrint = NULL;
771  myopt.title = _("List of operators");
772  myopt.translate_header = true;
773 
774  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
775 
776  PQclear(res);
777  return true;
778 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describePublications()

bool describePublications ( const char *  pattern)

Definition at line 5206 of file describe.c.

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

Referenced by exec_command_d().

5207 {
5209  int i;
5210  PGresult *res;
5211 
5212  if (pset.sversion < 100000)
5213  {
5214  char sverbuf[32];
5215 
5216  psql_error("The server (version %s) does not support publications.\n",
5218  sverbuf, sizeof(sverbuf)));
5219  return true;
5220  }
5221 
5222  initPQExpBuffer(&buf);
5223 
5224  printfPQExpBuffer(&buf,
5225  "SELECT oid, pubname,\n"
5226  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
5227  " puballtables, pubinsert, pubupdate, pubdelete\n"
5228  "FROM pg_catalog.pg_publication\n");
5229 
5230  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5231  NULL, "pubname", NULL,
5232  NULL);
5233 
5234  appendPQExpBufferStr(&buf, "ORDER BY 2;");
5235 
5236  res = PSQLexec(buf.data);
5237  if (!res)
5238  {
5239  termPQExpBuffer(&buf);
5240  return false;
5241  }
5242 
5243  if (PQntuples(res) == 0)
5244  {
5245  if (!pset.quiet)
5246  {
5247  if (pattern)
5248  psql_error("Did not find any publication named \"%s\".\n",
5249  pattern);
5250  else
5251  psql_error("Did not find any publications.\n");
5252  }
5253 
5254  termPQExpBuffer(&buf);
5255  PQclear(res);
5256  return false;
5257  }
5258 
5259  for (i = 0; i < PQntuples(res); i++)
5260  {
5261  const char align = 'l';
5262  int ncols = 5;
5263  int nrows = 1;
5264  int tables = 0;
5265  PGresult *tabres;
5266  char *pubid = PQgetvalue(res, i, 0);
5267  char *pubname = PQgetvalue(res, i, 1);
5268  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
5269  int j;
5270  PQExpBufferData title;
5271  printTableOpt myopt = pset.popt.topt;
5272  printTableContent cont;
5273 
5274  initPQExpBuffer(&title);
5275  printfPQExpBuffer(&title, _("Publication %s"), pubname);
5276  printTableInit(&cont, &myopt, title.data, ncols, nrows);
5277 
5278  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
5279  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
5280  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
5281  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
5282  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
5283 
5284  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
5285  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
5286  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
5287  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
5288  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
5289 
5290  if (!puballtables)
5291  {
5292  printfPQExpBuffer(&buf,
5293  "SELECT n.nspname, c.relname\n"
5294  "FROM pg_catalog.pg_class c,\n"
5295  " pg_catalog.pg_namespace n,\n"
5296  " pg_catalog.pg_publication_rel pr\n"
5297  "WHERE c.relnamespace = n.oid\n"
5298  " AND c.oid = pr.prrelid\n"
5299  " AND pr.prpubid = '%s'\n"
5300  "ORDER BY 1,2", pubid);
5301 
5302  tabres = PSQLexec(buf.data);
5303  if (!tabres)
5304  {
5305  printTableCleanup(&cont);
5306  PQclear(res);
5307  termPQExpBuffer(&buf);
5308  termPQExpBuffer(&title);
5309  return false;
5310  }
5311  else
5312  tables = PQntuples(tabres);
5313 
5314  if (tables > 0)
5315  printTableAddFooter(&cont, _("Tables:"));
5316 
5317  for (j = 0; j < tables; j++)
5318  {
5319  printfPQExpBuffer(&buf, " \"%s.%s\"",
5320  PQgetvalue(tabres, j, 0),
5321  PQgetvalue(tabres, j, 1));
5322 
5323  printTableAddFooter(&cont, buf.data);
5324  }
5325  PQclear(tabres);
5326  }
5327 
5328  printTable(&cont, pset.queryFout, false, pset.logfile);
5329  printTableCleanup(&cont);
5330 
5331  termPQExpBuffer(&title);
5332  }
5333 
5334  termPQExpBuffer(&buf);
5335  PQclear(res);
5336 
5337  return true;
5338 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PsqlSettings pset
Definition: startup.c:33
void printTableCleanup(printTableContent *const content)
Definition: print.c:3101
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
printTableOpt topt
Definition: print.h:165
#define gettext_noop(x)
Definition: c.h:991
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:833
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:2970
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3010
static char * buf
Definition: pg_test_fsync.c:67
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3190
void psql_error(const char *fmt,...)
Definition: common.c:221
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3058
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
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:2933
int i
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeRoles()

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

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

3110 {
3112  PGresult *res;
3113  printTableContent cont;
3114  printTableOpt myopt = pset.popt.topt;
3115  int ncols = 3;
3116  int nrows = 0;
3117  int i;
3118  int conns;
3119  const char align = 'l';
3120  char **attr;
3121 
3122  myopt.default_footer = false;
3123 
3124  initPQExpBuffer(&buf);
3125 
3126  if (pset.sversion >= 80100)
3127  {
3128  printfPQExpBuffer(&buf,
3129  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3130  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3131  " r.rolconnlimit, r.rolvaliduntil,\n"
3132  " ARRAY(SELECT b.rolname\n"
3133  " FROM pg_catalog.pg_auth_members m\n"
3134  " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
3135  " WHERE m.member = r.oid) as memberof");
3136 
3137  if (verbose && pset.sversion >= 80200)
3138  {
3139  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3140  ncols++;
3141  }
3142  if (pset.sversion >= 90100)
3143  {
3144  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3145  }
3146 
3147  if (pset.sversion >= 90500)
3148  {
3149  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3150  }
3151 
3152  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3153 
3154  if (!showSystem && !pattern)
3155  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3156 
3157  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3158  NULL, "r.rolname", NULL, NULL);
3159  }
3160  else
3161  {
3162  printfPQExpBuffer(&buf,
3163  "SELECT u.usename AS rolname,\n"
3164  " u.usesuper AS rolsuper,\n"
3165  " true AS rolinherit, false AS rolcreaterole,\n"
3166  " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
3167  " -1 AS rolconnlimit,"
3168  " u.valuntil as rolvaliduntil,\n"
3169  " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
3170  "\nFROM pg_catalog.pg_user u\n");
3171 
3172  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3173  NULL, "u.usename", NULL, NULL);
3174  }
3175 
3176  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3177 
3178  res = PSQLexec(buf.data);
3179  if (!res)
3180  return false;
3181 
3182  nrows = PQntuples(res);
3183  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3184 
3185  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3186 
3187  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3188  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3189  printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
3190 
3191  if (verbose && pset.sversion >= 80200)
3192  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3193 
3194  for (i = 0; i < nrows; i++)
3195  {
3196  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3197 
3198  resetPQExpBuffer(&buf);
3199  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3200  add_role_attribute(&buf, _("Superuser"));
3201 
3202  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3203  add_role_attribute(&buf, _("No inheritance"));
3204 
3205  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3206  add_role_attribute(&buf, _("Create role"));
3207 
3208  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3209  add_role_attribute(&buf, _("Create DB"));
3210 
3211  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3212  add_role_attribute(&buf, _("Cannot login"));
3213 
3214  if (pset.sversion >= 90100)
3215  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3216  add_role_attribute(&buf, _("Replication"));
3217 
3218  if (pset.sversion >= 90500)
3219  if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
3220  add_role_attribute(&buf, _("Bypass RLS"));
3221 
3222  conns = atoi(PQgetvalue(res, i, 6));
3223  if (conns >= 0)
3224  {
3225  if (buf.len > 0)
3226  appendPQExpBufferChar(&buf, '\n');
3227 
3228  if (conns == 0)
3229  appendPQExpBufferStr(&buf, _("No connections"));
3230  else
3231  appendPQExpBuffer(&buf, ngettext("%d connection",
3232  "%d connections",
3233  conns),
3234  conns);
3235  }
3236 
3237  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3238  {
3239  if (buf.len > 0)
3240  appendPQExpBufferChar(&buf, '\n');
3241  appendPQExpBufferStr(&buf, _("Password valid until "));
3242  appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3243  }
3244 
3245  attr[i] = pg_strdup(buf.data);
3246 
3247  printTableAddCell(&cont, attr[i], false, false);
3248 
3249  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3250 
3251  if (verbose && pset.sversion >= 80200)
3252  printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
3253  }
3254  termPQExpBuffer(&buf);
3255 
3256  printTable(&cont, pset.queryFout, false, pset.logfile);
3257  printTableCleanup(&cont);
3258 
3259  for (i = 0; i < nrows; i++)
3260  free(attr[i]);
3261  free(attr);
3262 
3263  PQclear(res);
3264  return true;
3265 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PsqlSettings pset
Definition: startup.c:33
void printTableCleanup(printTableContent *const content)
Definition: print.c:3101
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
printTableOpt topt
Definition: print.h:165
#define gettext_noop(x)
Definition: c.h:991
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:833
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static void add_role_attribute(PQExpBuffer buf, const char *const str)
Definition: describe.c:3268
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:2970
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:3010
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
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:3190
static int verbose
Definition: pg_basebackup.c:85
#define ngettext(s, p, n)
Definition: c.h:977
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
bool default_footer
Definition: print.h:110
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:2933
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeSubscriptions()

bool describeSubscriptions ( const char *  pattern,
bool  verbose 
)

Definition at line 5347 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, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

5348 {
5350  PGresult *res;
5351  printQueryOpt myopt = pset.popt;
5352  static const bool translate_columns[] = {false, false, false, false,
5353  false, false};
5354 
5355  if (pset.sversion < 100000)
5356  {
5357  char sverbuf[32];
5358 
5359  psql_error("The server (version %s) does not support subscriptions.\n",
5361  sverbuf, sizeof(sverbuf)));
5362  return true;
5363  }
5364 
5365  initPQExpBuffer(&buf);
5366 
5367  printfPQExpBuffer(&buf,
5368  "SELECT subname AS \"%s\"\n"
5369  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
5370  ", subenabled AS \"%s\"\n"
5371  ", subpublications AS \"%s\"\n",
5372  gettext_noop("Name"),
5373  gettext_noop("Owner"),
5374  gettext_noop("Enabled"),
5375  gettext_noop("Publication"));
5376 
5377  if (verbose)
5378  {
5379  appendPQExpBuffer(&buf,
5380  ", subsynccommit AS \"%s\"\n"
5381  ", subconninfo AS \"%s\"\n",
5382  gettext_noop("Synchronous commit"),
5383  gettext_noop("Conninfo"));
5384  }
5385 
5386  /* Only display subscriptions in current database. */
5387  appendPQExpBufferStr(&buf,
5388  "FROM pg_catalog.pg_subscription\n"
5389  "WHERE subdbid = (SELECT oid\n"
5390  " FROM pg_catalog.pg_database\n"
5391  " WHERE datname = pg_catalog.current_database())");
5392 
5393  processSQLNamePattern(pset.db, &buf, pattern, true, false,
5394  NULL, "subname", NULL,
5395  NULL);
5396 
5397  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5398 
5399  res = PSQLexec(buf.data);
5400  termPQExpBuffer(&buf);
5401  if (!res)
5402  return false;
5403 
5404  myopt.nullPrint = NULL;
5405  myopt.title = _("List of subscriptions");
5406  myopt.translate_header = true;
5407  myopt.translate_columns = translate_columns;
5408  myopt.n_translate_columns = lengthof(translate_columns);
5409 
5410  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5411 
5412  PQclear(res);
5413  return true;
5414 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeTableDetails()

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

Definition at line 1294 of file describe.c.

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

Referenced by exec_command_d().

1295 {
1297  PGresult *res;
1298  int i;
1299 
1300  initPQExpBuffer(&buf);
1301 
1302  printfPQExpBuffer(&buf,
1303  "SELECT c.oid,\n"
1304  " n.nspname,\n"
1305  " c.relname\n"
1306  "FROM pg_catalog.pg_class c\n"
1307  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1308 
1309  if (!showSystem && !pattern)
1310  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1311  " AND n.nspname <> 'information_schema'\n");
1312 
1313  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1314  "n.nspname", "c.relname", NULL,
1315  "pg_catalog.pg_table_is_visible(c.oid)");
1316 
1317  appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1318 
1319  res = PSQLexec(buf.data);
1320  termPQExpBuffer(&buf);
1321  if (!res)
1322  return false;
1323 
1324  if (PQntuples(res) == 0)
1325  {
1326  if (!pset.quiet)
1327  {
1328  if (pattern)
1329  psql_error("Did not find any relation named \"%s\".\n",
1330  pattern);
1331  else
1332  psql_error("Did not find any relations.\n");
1333  }
1334  PQclear(res);
1335  return false;
1336  }
1337 
1338  for (i = 0; i < PQntuples(res); i++)
1339  {
1340  const char *oid;
1341  const char *nspname;
1342  const char *relname;
1343 
1344  oid = PQgetvalue(res, i, 0);
1345  nspname = PQgetvalue(res, i, 1);
1346  relname = PQgetvalue(res, i, 2);
1347 
1348  if (!describeOneTableDetails(nspname, relname, oid, verbose))
1349  {
1350  PQclear(res);
1351  return false;
1352  }
1353  if (cancel_pressed)
1354  {
1355  PQclear(res);
1356  return false;
1357  }
1358  }
1359 
1360  PQclear(res);
1361  return true;
1362 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PsqlSettings pset
Definition: startup.c:33
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
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:833
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
volatile bool cancel_pressed
Definition: print.c:46
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:1372
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int i
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeTablespaces()

bool describeTablespaces ( const char *  pattern,
bool  verbose 
)

Definition at line 208 of file describe.c.

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

Referenced by exec_command_d().

209 {
211  PGresult *res;
212  printQueryOpt myopt = pset.popt;
213 
214  if (pset.sversion < 80000)
215  {
216  char sverbuf[32];
217 
218  psql_error("The server (version %s) does not support tablespaces.\n",
220  sverbuf, sizeof(sverbuf)));
221  return true;
222  }
223 
224  initPQExpBuffer(&buf);
225 
226  if (pset.sversion >= 90200)
227  printfPQExpBuffer(&buf,
228  "SELECT spcname AS \"%s\",\n"
229  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
230  " pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
231  gettext_noop("Name"),
232  gettext_noop("Owner"),
233  gettext_noop("Location"));
234  else
235  printfPQExpBuffer(&buf,
236  "SELECT spcname AS \"%s\",\n"
237  " pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
238  " spclocation AS \"%s\"",
239  gettext_noop("Name"),
240  gettext_noop("Owner"),
241  gettext_noop("Location"));
242 
243  if (verbose)
244  {
245  appendPQExpBufferStr(&buf, ",\n ");
246  printACLColumn(&buf, "spcacl");
247  }
248 
249  if (verbose && pset.sversion >= 90000)
250  appendPQExpBuffer(&buf,
251  ",\n spcoptions AS \"%s\"",
252  gettext_noop("Options"));
253 
254  if (verbose && pset.sversion >= 90200)
255  appendPQExpBuffer(&buf,
256  ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS \"%s\"",
257  gettext_noop("Size"));
258 
259  if (verbose && pset.sversion >= 80200)
260  appendPQExpBuffer(&buf,
261  ",\n pg_catalog.shobj_description(oid, 'pg_tablespace') AS \"%s\"",
262  gettext_noop("Description"));
263 
265  "\nFROM pg_catalog.pg_tablespace\n");
266 
267  processSQLNamePattern(pset.db, &buf, pattern, false, false,
268  NULL, "spcname", NULL,
269  NULL);
270 
271  appendPQExpBufferStr(&buf, "ORDER BY 1;");
272 
273  res = PSQLexec(buf.data);
274  termPQExpBuffer(&buf);
275  if (!res)
276  return false;
277 
278  myopt.nullPrint = NULL;
279  myopt.title = _("List of tablespaces");
280  myopt.translate_header = true;
281 
282  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
283 
284  PQclear(res);
285  return true;
286 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5424
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ describeTypes()

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

Definition at line 594 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, RELKIND_COMPOSITE_TYPE, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, and printQueryOpt::translate_header.

Referenced by exec_command_d().

595 {
597  PGresult *res;
598  printQueryOpt myopt = pset.popt;
599 
600  initPQExpBuffer(&buf);
601 
602  printfPQExpBuffer(&buf,
603  "SELECT n.nspname as \"%s\",\n"
604  " pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
605  gettext_noop("Schema"),
606  gettext_noop("Name"));
607  if (verbose)
608  appendPQExpBuffer(&buf,
609  " t.typname AS \"%s\",\n"
610  " CASE WHEN t.typrelid != 0\n"
611  " THEN CAST('tuple' AS pg_catalog.text)\n"
612  " WHEN t.typlen < 0\n"
613  " THEN CAST('var' AS pg_catalog.text)\n"
614  " ELSE CAST(t.typlen AS pg_catalog.text)\n"
615  " END AS \"%s\",\n",
616  gettext_noop("Internal name"),
617  gettext_noop("Size"));
618  if (verbose && pset.sversion >= 80300)
619  {
621  " pg_catalog.array_to_string(\n"
622  " ARRAY(\n"
623  " SELECT e.enumlabel\n"
624  " FROM pg_catalog.pg_enum e\n"
625  " WHERE e.enumtypid = t.oid\n");
626 
627  if (pset.sversion >= 90100)
629  " ORDER BY e.enumsortorder\n");
630  else
632  " ORDER BY e.oid\n");
633 
634  appendPQExpBuffer(&buf,
635  " ),\n"
636  " E'\\n'\n"
637  " ) AS \"%s\",\n",
638  gettext_noop("Elements"));
639  }
640  if (verbose)
641  {
642  appendPQExpBuffer(&buf,
643  " pg_catalog.pg_get_userbyid(t.typowner) AS \"%s\",\n",
644  gettext_noop("Owner"));
645  }
646  if (verbose && pset.sversion >= 90200)
647  {
648  printACLColumn(&buf, "t.typacl");
649  appendPQExpBufferStr(&buf, ",\n ");
650  }
651 
652  appendPQExpBuffer(&buf,
653  " pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
654  gettext_noop("Description"));
655 
656  appendPQExpBufferStr(&buf, "FROM pg_catalog.pg_type t\n"
657  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
658 
659  /*
660  * do not include complex types (typrelid!=0) unless they are standalone
661  * composite types
662  */
663  appendPQExpBufferStr(&buf, "WHERE (t.typrelid = 0 ");
664  appendPQExpBufferStr(&buf, "OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
665  " FROM pg_catalog.pg_class c "
666  "WHERE c.oid = t.typrelid))\n");
667 
668  /*
669  * do not include array types (before 8.3 we have to use the assumption
670  * that their names start with underscore)
671  */
672  if (pset.sversion >= 80300)
673  appendPQExpBufferStr(&buf, " AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n");
674  else
675  appendPQExpBufferStr(&buf, " AND t.typname !~ '^_'\n");
676 
677  if (!showSystem && !pattern)
678  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
679  " AND n.nspname <> 'information_schema'\n");
680 
681  /* Match name pattern against either internal or external name */
682  processSQLNamePattern(pset.db, &buf, pattern, true, false,
683  "n.nspname", "t.typname",
684  "pg_catalog.format_type(t.oid, NULL)",
685  "pg_catalog.pg_type_is_visible(t.oid)");
686 
687  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
688 
689  res = PSQLexec(buf.data);
690  termPQExpBuffer(&buf);
691  if (!res)
692  return false;
693 
694  myopt.nullPrint = NULL;
695  myopt.title = _("List of data types");
696  myopt.translate_header = true;
697 
698  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
699 
700  PQclear(res);
701  return true;
702 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define RELKIND_COMPOSITE_TYPE
Definition: pg_class.h:166
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
#define CppAsString2(x)
Definition: c.h:216
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5424
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listAllDbs()

bool listAllDbs ( const char *  pattern,
bool  verbose 
)

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

788 {
789  PGresult *res;
791  printQueryOpt myopt = pset.popt;
792 
793  initPQExpBuffer(&buf);
794 
795  printfPQExpBuffer(&buf,
796  "SELECT d.datname as \"%s\",\n"
797  " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"
798  " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
799  gettext_noop("Name"),
800  gettext_noop("Owner"),
801  gettext_noop("Encoding"));
802  if (pset.sversion >= 80400)
803  appendPQExpBuffer(&buf,
804  " d.datcollate as \"%s\",\n"
805  " d.datctype as \"%s\",\n",
806  gettext_noop("Collate"),
807  gettext_noop("Ctype"));
808  appendPQExpBufferStr(&buf, " ");
809  printACLColumn(&buf, "d.datacl");
810  if (verbose && pset.sversion >= 80200)
811  appendPQExpBuffer(&buf,
812  ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
813  " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
814  " ELSE 'No Access'\n"
815  " END as \"%s\"",
816  gettext_noop("Size"));
817  if (verbose && pset.sversion >= 80000)
818  appendPQExpBuffer(&buf,
819  ",\n t.spcname as \"%s\"",
820  gettext_noop("Tablespace"));
821  if (verbose && pset.sversion >= 80200)
822  appendPQExpBuffer(&buf,
823  ",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
824  gettext_noop("Description"));
826  "\nFROM pg_catalog.pg_database d\n");
827  if (verbose && pset.sversion >= 80000)
829  " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
830 
831  if (pattern)
832  processSQLNamePattern(pset.db, &buf, pattern, false, false,
833  NULL, "d.datname", NULL, NULL);
834 
835  appendPQExpBufferStr(&buf, "ORDER BY 1;");
836  res = PSQLexec(buf.data);
837  termPQExpBuffer(&buf);
838  if (!res)
839  return false;
840 
841  myopt.nullPrint = NULL;
842  myopt.title = _("List of databases");
843  myopt.translate_header = true;
844 
845  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
846 
847  PQclear(res);
848  return true;
849 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5424
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listCasts()

bool listCasts ( const char *  pattern,
bool  verbose 
)

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

3831 {
3833  PGresult *res;
3834  printQueryOpt myopt = pset.popt;
3835  static const bool translate_columns[] = {false, false, false, true, false};
3836 
3837  initPQExpBuffer(&buf);
3838 
3839  /*
3840  * We need a left join to pg_proc for binary casts; the others are just
3841  * paranoia. Also note that we don't attempt to localize '(binary
3842  * coercible)', because there's too much risk of gettext translating a
3843  * function name that happens to match some string in the PO database.
3844  */
3845  printfPQExpBuffer(&buf,
3846  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3847  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3848  " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3849  " ELSE p.proname\n"
3850  " END as \"%s\",\n"
3851  " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3852  " WHEN c.castcontext = 'a' THEN '%s'\n"
3853  " ELSE '%s'\n"
3854  " END as \"%s\"",
3855  gettext_noop("Source type"),
3856  gettext_noop("Target type"),
3857  gettext_noop("Function"),
3858  gettext_noop("no"),
3859  gettext_noop("in assignment"),
3860  gettext_noop("yes"),
3861  gettext_noop("Implicit?"));
3862 
3863  if (verbose)
3864  appendPQExpBuffer(&buf,
3865  ",\n d.description AS \"%s\"\n",
3866  gettext_noop("Description"));
3867 
3868  appendPQExpBufferStr(&buf,
3869  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3870  " ON c.castfunc = p.oid\n"
3871  " LEFT JOIN pg_catalog.pg_type ts\n"
3872  " ON c.castsource = ts.oid\n"
3873  " LEFT JOIN pg_catalog.pg_namespace ns\n"
3874  " ON ns.oid = ts.typnamespace\n"
3875  " LEFT JOIN pg_catalog.pg_type tt\n"
3876  " ON c.casttarget = tt.oid\n"
3877  " LEFT JOIN pg_catalog.pg_namespace nt\n"
3878  " ON nt.oid = tt.typnamespace\n");
3879 
3880  if (verbose)
3881  appendPQExpBufferStr(&buf,
3882  " LEFT JOIN pg_catalog.pg_description d\n"
3883  " ON d.classoid = c.tableoid AND d.objoid = "
3884  "c.oid AND d.objsubid = 0\n");
3885 
3886  appendPQExpBufferStr(&buf, "WHERE ( (true");
3887 
3888  /*
3889  * Match name pattern against either internal or external name of either
3890  * castsource or casttarget
3891  */
3892  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3893  "ns.nspname", "ts.typname",
3894  "pg_catalog.format_type(ts.oid, NULL)",
3895  "pg_catalog.pg_type_is_visible(ts.oid)");
3896 
3897  appendPQExpBufferStr(&buf, ") OR (true");
3898 
3899  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3900  "nt.nspname", "tt.typname",
3901  "pg_catalog.format_type(tt.oid, NULL)",
3902  "pg_catalog.pg_type_is_visible(tt.oid)");
3903 
3904  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3905 
3906  res = PSQLexec(buf.data);
3907  termPQExpBuffer(&buf);
3908  if (!res)
3909  return false;
3910 
3911  myopt.nullPrint = NULL;
3912  myopt.title = _("List of casts");
3913  myopt.translate_header = true;
3914  myopt.translate_columns = translate_columns;
3915  myopt.n_translate_columns = lengthof(translate_columns);
3916 
3917  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3918 
3919  PQclear(res);
3920  return true;
3921 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listCollations()

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

Definition at line 3929 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, _psqlSettings::popt, PQclear(), printfPQExpBuffer(), printQuery(), processSQLNamePattern(), pset, psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::sversion, termPQExpBuffer(), printQueryOpt::title, printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by exec_command_d().

3930 {
3932  PGresult *res;
3933  printQueryOpt myopt = pset.popt;
3934  static const bool translate_columns[] = {false, false, false, false, false, false};
3935 
3936  if (pset.sversion < 90100)
3937  {
3938  char sverbuf[32];
3939 
3940  psql_error("The server (version %s) does not support collations.\n",
3942  sverbuf, sizeof(sverbuf)));
3943  return true;
3944  }
3945 
3946  initPQExpBuffer(&buf);
3947 
3948  printfPQExpBuffer(&buf,
3949  "SELECT n.nspname AS \"%s\",\n"
3950  " c.collname AS \"%s\",\n"
3951  " c.collcollate AS \"%s\",\n"
3952  " c.collctype AS \"%s\"",
3953  gettext_noop("Schema"),
3954  gettext_noop("Name"),
3955  gettext_noop("Collate"),
3956  gettext_noop("Ctype"));
3957 
3958  if (pset.sversion >= 100000)
3959  appendPQExpBuffer(&buf,
3960  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
3961  gettext_noop("Provider"));
3962 
3963  if (verbose)
3964  appendPQExpBuffer(&buf,
3965  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3966  gettext_noop("Description"));
3967 
3968  appendPQExpBufferStr(&buf,
3969  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3970  "WHERE n.oid = c.collnamespace\n");
3971 
3972  if (!showSystem && !pattern)
3973  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3974  " AND n.nspname <> 'information_schema'\n");
3975 
3976  /*
3977  * Hide collations that aren't usable in the current database's encoding.
3978  * If you think to change this, note that pg_collation_is_visible rejects
3979  * unusable collations, so you will need to hack name pattern processing
3980  * somehow to avoid inconsistent behavior.
3981  */
3982  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3983 
3984  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3985  "n.nspname", "c.collname", NULL,
3986  "pg_catalog.pg_collation_is_visible(c.oid)");
3987 
3988  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3989 
3990  res = PSQLexec(buf.data);
3991  termPQExpBuffer(&buf);
3992  if (!res)
3993  return false;
3994 
3995  myopt.nullPrint = NULL;
3996  myopt.title = _("List of collations");
3997  myopt.translate_header = true;
3998  myopt.translate_columns = translate_columns;
3999  myopt.n_translate_columns = lengthof(translate_columns);
4000 
4001  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4002 
4003  PQclear(res);
4004  return true;
4005 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listConversions()

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

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

3689 {
3691  PGresult *res;
3692  printQueryOpt myopt = pset.popt;
3693  static const bool translate_columns[] =
3694  {false, false, false, false, true, false};
3695 
3696  initPQExpBuffer(&buf);
3697 
3698  printfPQExpBuffer(&buf,
3699  "SELECT n.nspname AS \"%s\",\n"
3700  " c.conname AS \"%s\",\n"
3701  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3702  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3703  " CASE WHEN c.condefault THEN '%s'\n"
3704  " ELSE '%s' END AS \"%s\"",
3705  gettext_noop("Schema"),
3706  gettext_noop("Name"),
3707  gettext_noop("Source"),
3708  gettext_noop("Destination"),
3709  gettext_noop("yes"), gettext_noop("no"),
3710  gettext_noop("Default?"));
3711 
3712  if (verbose)
3713  appendPQExpBuffer(&buf,
3714  ",\n d.description AS \"%s\"",
3715  gettext_noop("Description"));
3716 
3717  appendPQExpBufferStr(&buf,
3718  "\nFROM pg_catalog.pg_conversion c\n"
3719  " JOIN pg_catalog.pg_namespace n "
3720  "ON n.oid = c.connamespace\n");
3721 
3722  if (verbose)
3723  appendPQExpBufferStr(&buf,
3724  "LEFT JOIN pg_catalog.pg_description d "
3725  "ON d.classoid = c.tableoid\n"
3726  " AND d.objoid = c.oid "
3727  "AND d.objsubid = 0\n");
3728 
3729  appendPQExpBufferStr(&buf, "WHERE true\n");
3730 
3731  if (!showSystem && !pattern)
3732  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3733  " AND n.nspname <> 'information_schema'\n");
3734 
3735  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3736  "n.nspname", "c.conname", NULL,
3737  "pg_catalog.pg_conversion_is_visible(c.oid)");
3738 
3739  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3740 
3741  res = PSQLexec(buf.data);
3742  termPQExpBuffer(&buf);
3743  if (!res)
3744  return false;
3745 
3746  myopt.nullPrint = NULL;
3747  myopt.title = _("List of conversions");
3748  myopt.translate_header = true;
3749  myopt.translate_columns = translate_columns;
3750  myopt.n_translate_columns = lengthof(translate_columns);
3751 
3752  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3753 
3754  PQclear(res);
3755  return true;
3756 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listDbRoleSettings()

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

Definition at line 3280 of file describe.c.

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

Referenced by exec_command_d().

3281 {
3283  PGresult *res;
3284  printQueryOpt myopt = pset.popt;
3285  bool havewhere;
3286 
3287  if (pset.sversion < 90000)
3288  {
3289  char sverbuf[32];
3290 
3291  psql_error("The server (version %s) does not support per-database role settings.\n",
3293  sverbuf, sizeof(sverbuf)));
3294  return true;
3295  }
3296 
3297  initPQExpBuffer(&buf);
3298 
3299  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3300  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3301  "FROM pg_catalog.pg_db_role_setting s\n"
3302  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3303  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3304  gettext_noop("Role"),
3305  gettext_noop("Database"),
3306  gettext_noop("Settings"));
3307  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3308  NULL, "r.rolname", NULL, NULL);
3309  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3310  NULL, "d.datname", NULL, NULL);
3311  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3312 
3313  res = PSQLexec(buf.data);
3314  termPQExpBuffer(&buf);
3315  if (!res)
3316  return false;
3317 
3318  /*
3319  * Most functions in this file are content to print an empty table when
3320  * there are no matching objects. We intentionally deviate from that
3321  * here, but only in !quiet mode, because of the possibility that the user
3322  * is confused about what the two pattern arguments mean.
3323  */
3324  if (PQntuples(res) == 0 && !pset.quiet)
3325  {
3326  if (pattern && pattern2)
3327  psql_error("Did not find any settings for role \"%s\" and database \"%s\".\n",
3328  pattern, pattern2);
3329  else if (pattern)
3330  psql_error("Did not find any settings for role \"%s\".\n",
3331  pattern);
3332  else
3333  psql_error("Did not find any settings.\n");
3334  }
3335  else
3336  {
3337  myopt.nullPrint = NULL;
3338  myopt.title = _("List of settings");
3339  myopt.translate_header = true;
3340 
3341  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3342  }
3343 
3344  PQclear(res);
3345  return true;
3346 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listDefaultACLs()

bool listDefaultACLs ( const char *  pattern)

Definition at line 1017 of file describe.c.

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

Referenced by exec_command_d().

1018 {
1020  PGresult *res;
1021  printQueryOpt myopt = pset.popt;
1022  static const bool translate_columns[] = {false, false, true, false};
1023 
1024  if (pset.sversion < 90000)
1025  {
1026  char sverbuf[32];
1027 
1028  psql_error("The server (version %s) does not support altering default privileges.\n",
1030  sverbuf, sizeof(sverbuf)));
1031  return true;
1032  }
1033 
1034  initPQExpBuffer(&buf);
1035 
1036  printfPQExpBuffer(&buf,
1037  "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1038  " n.nspname AS \"%s\",\n"
1039  " 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"
1040  " ",
1041  gettext_noop("Owner"),
1042  gettext_noop("Schema"),
1044  gettext_noop("table"),
1046  gettext_noop("sequence"),
1048  gettext_noop("function"),
1050  gettext_noop("type"),
1052  gettext_noop("schema"),
1053  gettext_noop("Type"));
1054 
1055  printACLColumn(&buf, "d.defaclacl");
1056 
1057  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1058  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1059 
1060  processSQLNamePattern(pset.db, &buf, pattern, false, false,
1061  NULL,
1062  "n.nspname",
1063  "pg_catalog.pg_get_userbyid(d.defaclrole)",
1064  NULL);
1065 
1066  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1067 
1068  res = PSQLexec(buf.data);
1069  if (!res)
1070  {
1071  termPQExpBuffer(&buf);
1072  return false;
1073  }
1074 
1075  myopt.nullPrint = NULL;
1076  printfPQExpBuffer(&buf, _("Default access privileges"));
1077  myopt.title = buf.data;
1078  myopt.translate_header = true;
1079  myopt.translate_columns = translate_columns;
1080  myopt.n_translate_columns = lengthof(translate_columns);
1081 
1082  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1083 
1084  termPQExpBuffer(&buf);
1085  PQclear(res);
1086  return true;
1087 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
#define DEFACLOBJ_SEQUENCE
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
#define DEFACLOBJ_TYPE
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
#define DEFACLOBJ_NAMESPACE
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DEFACLOBJ_RELATION
char * title
Definition: print.h:167
#define DEFACLOBJ_FUNCTION
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5424
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listDomains()

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

Definition at line 3602 of file describe.c.

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

Referenced by exec_command_d().

3603 {
3605  PGresult *res;
3606  printQueryOpt myopt = pset.popt;
3607 
3608  initPQExpBuffer(&buf);
3609 
3610  printfPQExpBuffer(&buf,
3611  "SELECT n.nspname as \"%s\",\n"
3612  " t.typname as \"%s\",\n"
3613  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
3614  gettext_noop("Schema"),
3615  gettext_noop("Name"),
3616  gettext_noop("Type"));
3617 
3618  if (pset.sversion >= 90100)
3619  appendPQExpBuffer(&buf,
3620  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3621  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
3622  gettext_noop("Collation"));
3623  appendPQExpBuffer(&buf,
3624  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
3625  " t.typdefault as \"%s\",\n"
3626  " pg_catalog.array_to_string(ARRAY(\n"
3627  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3628  " ), ' ') as \"%s\"",
3629  gettext_noop("Nullable"),
3630  gettext_noop("Default"),
3631  gettext_noop("Check"));
3632 
3633  if (verbose)
3634  {
3635  if (pset.sversion >= 90200)
3636  {
3637  appendPQExpBufferStr(&buf, ",\n ");
3638  printACLColumn(&buf, "t.typacl");
3639  }
3640  appendPQExpBuffer(&buf,
3641  ",\n d.description as \"%s\"",
3642  gettext_noop("Description"));
3643  }
3644 
3645  appendPQExpBufferStr(&buf,
3646  "\nFROM pg_catalog.pg_type t\n"
3647  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3648 
3649  if (verbose)
3650  appendPQExpBufferStr(&buf,
3651  " LEFT JOIN pg_catalog.pg_description d "
3652  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3653  "AND d.objsubid = 0\n");
3654 
3655  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3656 
3657  if (!showSystem && !pattern)
3658  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3659  " AND n.nspname <> 'information_schema'\n");
3660 
3661  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3662  "n.nspname", "t.typname", NULL,
3663  "pg_catalog.pg_type_is_visible(t.oid)");
3664 
3665  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3666 
3667  res = PSQLexec(buf.data);
3668  termPQExpBuffer(&buf);
3669  if (!res)
3670  return false;
3671 
3672  myopt.nullPrint = NULL;
3673  myopt.title = _("List of domains");
3674  myopt.translate_header = true;
3675 
3676  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3677 
3678  PQclear(res);
3679  return true;
3680 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5424
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listEventTriggers()

bool listEventTriggers ( const char *  pattern,
bool  verbose 
)

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

3765 {
3767  PGresult *res;
3768  printQueryOpt myopt = pset.popt;
3769  static const bool translate_columns[] =
3770  {false, false, false, true, false, false, false};
3771 
3772  initPQExpBuffer(&buf);
3773 
3774  printfPQExpBuffer(&buf,
3775  "SELECT evtname as \"%s\", "
3776  "evtevent as \"%s\", "
3777  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3778  " case evtenabled when 'O' then '%s'"
3779  " when 'R' then '%s'"
3780  " when 'A' then '%s'"
3781  " when 'D' then '%s' end as \"%s\",\n"
3782  " e.evtfoid::pg_catalog.regproc as \"%s\", "
3783  "pg_catalog.array_to_string(array(select x"
3784  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3785  gettext_noop("Name"),
3786  gettext_noop("Event"),
3787  gettext_noop("Owner"),
3788  gettext_noop("enabled"),
3789  gettext_noop("replica"),
3790  gettext_noop("always"),
3791  gettext_noop("disabled"),
3792  gettext_noop("Enabled"),
3793  gettext_noop("Procedure"),
3794  gettext_noop("Tags"));
3795  if (verbose)
3796  appendPQExpBuffer(&buf,
3797  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3798  gettext_noop("Description"));
3799  appendPQExpBufferStr(&buf,
3800  "\nFROM pg_catalog.pg_event_trigger e ");
3801 
3802  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3803  NULL, "evtname", NULL, NULL);
3804 
3805  appendPQExpBufferStr(&buf, "ORDER BY 1");
3806 
3807  res = PSQLexec(buf.data);
3808  termPQExpBuffer(&buf);
3809  if (!res)
3810  return false;
3811 
3812  myopt.nullPrint = NULL;
3813  myopt.title = _("List of event triggers");
3814  myopt.translate_header = true;
3815  myopt.translate_columns = translate_columns;
3816  myopt.n_translate_columns = lengthof(translate_columns);
3817 
3818  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3819 
3820  PQclear(res);
3821  return true;
3822 }
PGconn * db
Definition: settings.h:82
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define gettext_noop(x)
Definition: c.h:991
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:833
#define lengthof(array)
Definition: c.h:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ listExtensionContents()

bool listExtensionContents ( const char *  pattern)

Definition at line 5026 of file describe.c.

References appendPQExpBufferStr(), buf, cancel_pressed, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), i, initPQExpBuffer(), listOneExtensionContents(), PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), processSQLNamePattern(), pset, psql_error(), PSQLexec(), _psqlSettings::quiet, _psqlSettings::sversion, and termPQExpBuffer().

Referenced by exec_command_d().

5027 {
5029  PGresult *res;
5030  int i;
5031 
5032  if (pset.sversion < 90100)
5033  {
5034  char sverbuf[32];
5035 
5036  psql_error("The server (version %s) does not support extensions.\n",
5038  sverbuf, sizeof(sverbuf)));
5039  return true;
5040  }
5041 
5042  initPQExpBuffer(&buf);
5043  printfPQExpBuffer(&buf,
5044  "SELECT e.extname, e.oid\n"
5045  "FROM pg_catalog.pg_extension e\n");
5046 
5047  processSQLNamePattern(pset.db, &buf, pattern,
5048  false, false,
5049  NULL, "e.extname", NULL,
5050  NULL);
5051 
5052  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5053 
5054  res = PSQLexec(buf.data);
5055  termPQExpBuffer(&buf);
5056  if (!res)
5057  return false;
5058 
5059  if (PQntuples(res) == 0)
5060  {
5061  if (!pset.quiet)
5062  {
5063  if (pattern)
5064  psql_error("Did not find any extension named \"%s\".\n",
5065  pattern);
5066  else
5067