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

References appendPQExpBufferStr(), and PQExpBufferData::len.

Referenced by describeRoles().

3276 {
3277  if (buf->len > 0)
3278  appendPQExpBufferStr(buf, ", ");
3279 
3280  appendPQExpBufferStr(buf, str);
3281 }
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 3052 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().

3054 {
3055  /* relkinds for which we support tablespaces */
3056  if (relkind == RELKIND_RELATION ||
3057  relkind == RELKIND_MATVIEW ||
3058  relkind == RELKIND_INDEX ||
3059  relkind == RELKIND_PARTITIONED_TABLE)
3060  {
3061  /*
3062  * We ignore the database default tablespace so that users not using
3063  * tablespaces don't need to know about them. This case also covers
3064  * pre-8.0 servers, for which tablespace will always be 0.
3065  */
3066  if (tablespace != 0)
3067  {
3068  PGresult *result = NULL;
3070 
3071  initPQExpBuffer(&buf);
3072  printfPQExpBuffer(&buf,
3073  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3074  "WHERE oid = '%u';", tablespace);
3075  result = PSQLexec(buf.data);
3076  if (!result)
3077  {
3078  termPQExpBuffer(&buf);
3079  return;
3080  }
3081  /* Should always be the case, but.... */
3082  if (PQntuples(result) > 0)
3083  {
3084  if (newline)
3085  {
3086  /* Add the tablespace as a new footer */
3087  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3088  PQgetvalue(result, 0, 0));
3089  printTableAddFooter(cont, buf.data);
3090  }
3091  else
3092  {
3093  /* Append the tablespace to the latest footer */
3094  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3095 
3096  /*-------
3097  translator: before this string there's an index description like
3098  '"foo_pkey" PRIMARY KEY, btree (a)' */
3099  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3100  PQgetvalue(result, 0, 0));
3101  printTableSetFooter(cont, buf.data);
3102  }
3103  }
3104  PQclear(result);
3105  termPQExpBuffer(&buf);
3106  }
3107  }
3108 }
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:146
#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:1025
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:618
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:1025
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:1025
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:618
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:5434
#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_INDEX, 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  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1710  appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
1711  else
1712  appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
1713  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200)
1714  appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1715  " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1716  " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1717  else
1718  appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
1719  if (verbose)
1720  {
1721  appendPQExpBufferStr(&buf, ",\n a.attstorage");
1722  appendPQExpBufferStr(&buf, ",\n CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget");
1723 
1724  /*
1725  * In 9.0+, we have column comments for: relations, views, composite
1726  * types, and foreign tables (cf. CommentObject() in comment.c).
1727  */
1728  if (tableinfo.relkind == RELKIND_RELATION ||
1729  tableinfo.relkind == RELKIND_VIEW ||
1730  tableinfo.relkind == RELKIND_MATVIEW ||
1731  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1732  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1733  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1734  appendPQExpBufferStr(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
1735  }
1736 
1737  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a");
1738  appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1739  appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;");
1740 
1741  res = PSQLexec(buf.data);
1742  if (!res)
1743  goto error_return;
1744  numrows = PQntuples(res);
1745 
1746  /* Make title */
1747  switch (tableinfo.relkind)
1748  {
1749  case RELKIND_RELATION:
1750  if (tableinfo.relpersistence == 'u')
1751  printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1752  schemaname, relationname);
1753  else
1754  printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1755  schemaname, relationname);
1756  break;
1757  case RELKIND_VIEW:
1758  printfPQExpBuffer(&title, _("View \"%s.%s\""),
1759  schemaname, relationname);
1760  break;
1761  case RELKIND_MATVIEW:
1762  if (tableinfo.relpersistence == 'u')
1763  printfPQExpBuffer(&title, _("Unlogged materialized view \"%s.%s\""),
1764  schemaname, relationname);
1765  else
1766  printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""),
1767  schemaname, relationname);
1768  break;
1769  case RELKIND_INDEX:
1771  if (tableinfo.relpersistence == 'u')
1772  printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""),
1773  schemaname, relationname);
1774  else
1775  printfPQExpBuffer(&title, _("Index \"%s.%s\""),
1776  schemaname, relationname);
1777  break;
1778  case 's':
1779  /* not used as of 8.2, but keep it for backwards compatibility */
1780  printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
1781  schemaname, relationname);
1782  break;
1783  case RELKIND_TOASTVALUE:
1784  printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
1785  schemaname, relationname);
1786  break;
1788  printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
1789  schemaname, relationname);
1790  break;
1791  case RELKIND_FOREIGN_TABLE:
1792  printfPQExpBuffer(&title, _("Foreign table \"%s.%s\""),
1793  schemaname, relationname);
1794  break;
1796  if (tableinfo.relpersistence == 'u')
1797  printfPQExpBuffer(&title, _("Unlogged table \"%s.%s\""),
1798  schemaname, relationname);
1799  else
1800  printfPQExpBuffer(&title, _("Table \"%s.%s\""),
1801  schemaname, relationname);
1802  break;
1803  default:
1804  /* untranslated unknown relkind */
1805  printfPQExpBuffer(&title, "?%c? \"%s.%s\"",
1806  tableinfo.relkind, schemaname, relationname);
1807  break;
1808  }
1809 
1810  /* Set the number of columns, and their names */
1811  headers[0] = gettext_noop("Column");
1812  headers[1] = gettext_noop("Type");
1813  cols = 2;
1814 
1815  if (tableinfo.relkind == RELKIND_RELATION ||
1816  tableinfo.relkind == RELKIND_VIEW ||
1817  tableinfo.relkind == RELKIND_MATVIEW ||
1818  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1819  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1820  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1821  {
1822  headers[cols++] = gettext_noop("Collation");
1823  headers[cols++] = gettext_noop("Nullable");
1824  headers[cols++] = gettext_noop("Default");
1825  show_column_details = true;
1826  }
1827 
1828  if (tableinfo.relkind == RELKIND_INDEX ||
1829  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1830  headers[cols++] = gettext_noop("Definition");
1831 
1832  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200)
1833  headers[cols++] = gettext_noop("FDW options");
1834 
1835  if (verbose)
1836  {
1837  headers[cols++] = gettext_noop("Storage");
1838  if (tableinfo.relkind == RELKIND_RELATION ||
1839  tableinfo.relkind == RELKIND_INDEX ||
1840  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1841  tableinfo.relkind == RELKIND_MATVIEW ||
1842  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1843  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1844  headers[cols++] = gettext_noop("Stats target");
1845  /* Column comments, if the relkind supports this feature. */
1846  if (tableinfo.relkind == RELKIND_RELATION ||
1847  tableinfo.relkind == RELKIND_VIEW ||
1848  tableinfo.relkind == RELKIND_MATVIEW ||
1849  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1850  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1851  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1852  headers[cols++] = gettext_noop("Description");
1853  }
1854 
1855  printTableInit(&cont, &myopt, title.data, cols, numrows);
1856  printTableInitialized = true;
1857 
1858  for (i = 0; i < cols; i++)
1859  printTableAddHeader(&cont, headers[i], true, 'l');
1860 
1861  /* Get view_def if table is a view or materialized view */
1862  if ((tableinfo.relkind == RELKIND_VIEW ||
1863  tableinfo.relkind == RELKIND_MATVIEW) && verbose)
1864  {
1865  PGresult *result;
1866 
1867  printfPQExpBuffer(&buf,
1868  "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true);",
1869  oid);
1870  result = PSQLexec(buf.data);
1871  if (!result)
1872  goto error_return;
1873 
1874  if (PQntuples(result) > 0)
1875  view_def = pg_strdup(PQgetvalue(result, 0, 0));
1876 
1877  PQclear(result);
1878  }
1879 
1880  /* Generate table cells to be printed */
1881  for (i = 0; i < numrows; i++)
1882  {
1883  /* Column */
1884  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
1885 
1886  /* Type */
1887  printTableAddCell(&cont, PQgetvalue(res, i, 1), false, false);
1888 
1889  /* Collation, Nullable, Default */
1890  if (show_column_details)
1891  {
1892  char *identity;
1893  char *default_str = "";
1894 
1895  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
1896 
1897  printTableAddCell(&cont, strcmp(PQgetvalue(res, i, 3), "t") == 0 ? "not null" : "", false, false);
1898 
1899  identity = PQgetvalue(res, i, 6);
1900 
1901  if (!identity[0])
1902  /* (note: above we cut off the 'default' string at 128) */
1903  default_str = PQgetvalue(res, i, 2);
1904  else if (identity[0] == ATTRIBUTE_IDENTITY_ALWAYS)
1905  default_str = "generated always as identity";
1906  else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
1907  default_str = "generated by default as identity";
1908 
1909  printTableAddCell(&cont, default_str, false, false);
1910  }
1911 
1912  /* Expression for index column */
1913  if (tableinfo.relkind == RELKIND_INDEX ||
1914  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
1915  printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
1916 
1917  /* FDW options for foreign table column, only for 9.2 or later */
1918  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200)
1919  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
1920 
1921  /* Storage and Description */
1922  if (verbose)
1923  {
1924  int firstvcol = 9;
1925  char *storage = PQgetvalue(res, i, firstvcol);
1926 
1927  /* these strings are literal in our syntax, so not translated. */
1928  printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
1929  (storage[0] == 'm' ? "main" :
1930  (storage[0] == 'x' ? "extended" :
1931  (storage[0] == 'e' ? "external" :
1932  "???")))),
1933  false, false);
1934 
1935  /* Statistics target, if the relkind supports this feature */
1936  if (tableinfo.relkind == RELKIND_RELATION ||
1937  tableinfo.relkind == RELKIND_INDEX ||
1938  tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
1939  tableinfo.relkind == RELKIND_MATVIEW ||
1940  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1941  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1942  {
1943  printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1),
1944  false, false);
1945  }
1946 
1947  /* Column comments, if the relkind supports this feature. */
1948  if (tableinfo.relkind == RELKIND_RELATION ||
1949  tableinfo.relkind == RELKIND_VIEW ||
1950  tableinfo.relkind == RELKIND_MATVIEW ||
1951  tableinfo.relkind == RELKIND_COMPOSITE_TYPE ||
1952  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
1953  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
1954  printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 2),
1955  false, false);
1956  }
1957  }
1958 
1959  /* Make footers */
1960  if (pset.sversion >= 100000)
1961  {
1962  /* Get the partition information */
1963  PGresult *result;
1964  char *parent_name;
1965  char *partdef;
1966  char *partconstraintdef = NULL;
1967 
1968  printfPQExpBuffer(&buf,
1969  "SELECT inhparent::pg_catalog.regclass,\n"
1970  " pg_catalog.pg_get_expr(c.relpartbound, inhrelid)");
1971  /* If verbose, also request the partition constraint definition */
1972  if (verbose)
1973  appendPQExpBuffer(&buf,
1974  ",\n pg_catalog.pg_get_partition_constraintdef(inhrelid)");
1975  appendPQExpBuffer(&buf,
1976  "\nFROM pg_catalog.pg_class c"
1977  " JOIN pg_catalog.pg_inherits i"
1978  " ON c.oid = inhrelid"
1979  "\nWHERE c.oid = '%s' AND c.relispartition;", oid);
1980  result = PSQLexec(buf.data);
1981  if (!result)
1982  goto error_return;
1983 
1984  if (PQntuples(result) > 0)
1985  {
1986  parent_name = PQgetvalue(result, 0, 0);
1987  partdef = PQgetvalue(result, 0, 1);
1988 
1989  if (PQnfields(result) == 3 && !PQgetisnull(result, 0, 2))
1990  partconstraintdef = PQgetvalue(result, 0, 2);
1991 
1992  printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s"), parent_name,
1993  partdef);
1994  printTableAddFooter(&cont, tmpbuf.data);
1995 
1996  if (verbose)
1997  {
1998  /* If there isn't any constraint, show that explicitly */
1999  if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
2000  printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
2001  else
2002  printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"),
2003  partconstraintdef);
2004  printTableAddFooter(&cont, tmpbuf.data);
2005  }
2006 
2007  PQclear(result);
2008  }
2009  }
2010 
2011  if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2012  {
2013  /* Get the partition key information */
2014  PGresult *result;
2015  char *partkeydef;
2016 
2017  printfPQExpBuffer(&buf,
2018  "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
2019  oid);
2020  result = PSQLexec(buf.data);
2021  if (!result || PQntuples(result) != 1)
2022  goto error_return;
2023 
2024  partkeydef = PQgetvalue(result, 0, 0);
2025  printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2026  printTableAddFooter(&cont, tmpbuf.data);
2027  PQclear(result);
2028  }
2029 
2030  if (tableinfo.relkind == RELKIND_INDEX ||
2031  tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
2032  {
2033  /* Footer information about an index */
2034  PGresult *result;
2035 
2036  printfPQExpBuffer(&buf,
2037  "SELECT i.indisunique, i.indisprimary, i.indisclustered, ");
2038  if (pset.sversion >= 80200)
2039  appendPQExpBufferStr(&buf, "i.indisvalid,\n");
2040  else
2041  appendPQExpBufferStr(&buf, "true AS indisvalid,\n");
2042  if (pset.sversion >= 90000)
2043  appendPQExpBufferStr(&buf,
2044  " (NOT i.indimmediate) AND "
2045  "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2046  "WHERE conrelid = i.indrelid AND "
2047  "conindid = i.indexrelid AND "
2048  "contype IN ('p','u','x') AND "
2049  "condeferrable) AS condeferrable,\n"
2050  " (NOT i.indimmediate) AND "
2051  "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
2052  "WHERE conrelid = i.indrelid AND "
2053  "conindid = i.indexrelid AND "
2054  "contype IN ('p','u','x') AND "
2055  "condeferred) AS condeferred,\n");
2056  else
2057  appendPQExpBufferStr(&buf,
2058  " false AS condeferrable, false AS condeferred,\n");
2059 
2060  if (pset.sversion >= 90400)
2061  appendPQExpBuffer(&buf, "i.indisreplident,\n");
2062  else
2063  appendPQExpBuffer(&buf, "false AS indisreplident,\n");
2064 
2065  appendPQExpBuffer(&buf, " a.amname, c2.relname, "
2066  "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
2067  "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
2068  "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
2069  "AND i.indrelid = c2.oid;",
2070  oid);
2071 
2072  result = PSQLexec(buf.data);
2073  if (!result)
2074  goto error_return;
2075  else if (PQntuples(result) != 1)
2076  {
2077  PQclear(result);
2078  goto error_return;
2079  }
2080  else
2081  {
2082  char *indisunique = PQgetvalue(result, 0, 0);
2083  char *indisprimary = PQgetvalue(result, 0, 1);
2084  char *indisclustered = PQgetvalue(result, 0, 2);
2085  char *indisvalid = PQgetvalue(result, 0, 3);
2086  char *deferrable = PQgetvalue(result, 0, 4);
2087  char *deferred = PQgetvalue(result, 0, 5);
2088  char *indisreplident = PQgetvalue(result, 0, 6);
2089  char *indamname = PQgetvalue(result, 0, 7);
2090  char *indtable = PQgetvalue(result, 0, 8);
2091  char *indpred = PQgetvalue(result, 0, 9);
2092 
2093  if (strcmp(indisprimary, "t") == 0)
2094  printfPQExpBuffer(&tmpbuf, _("primary key, "));
2095  else if (strcmp(indisunique, "t") == 0)
2096  printfPQExpBuffer(&tmpbuf, _("unique, "));
2097  else
2098  resetPQExpBuffer(&tmpbuf);
2099  appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
2100 
2101  /* we assume here that index and table are in same schema */
2102  appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
2103  schemaname, indtable);
2104 
2105  if (strlen(indpred))
2106  appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
2107 
2108  if (strcmp(indisclustered, "t") == 0)
2109  appendPQExpBufferStr(&tmpbuf, _(", clustered"));
2110 
2111  if (strcmp(indisvalid, "t") != 0)
2112  appendPQExpBufferStr(&tmpbuf, _(", invalid"));
2113 
2114  if (strcmp(deferrable, "t") == 0)
2115  appendPQExpBufferStr(&tmpbuf, _(", deferrable"));
2116 
2117  if (strcmp(deferred, "t") == 0)
2118  appendPQExpBufferStr(&tmpbuf, _(", initially deferred"));
2119 
2120  if (strcmp(indisreplident, "t") == 0)
2121  appendPQExpBuffer(&tmpbuf, _(", replica identity"));
2122 
2123  printTableAddFooter(&cont, tmpbuf.data);
2124  add_tablespace_footer(&cont, tableinfo.relkind,
2125  tableinfo.tablespace, true);
2126  }
2127 
2128  PQclear(result);
2129  }
2130  else if (tableinfo.relkind == RELKIND_RELATION ||
2131  tableinfo.relkind == RELKIND_MATVIEW ||
2132  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2133  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2134  {
2135  /* Footer information about a table */
2136  PGresult *result = NULL;
2137  int tuples = 0;
2138 
2139  /* print indexes */
2140  if (tableinfo.hasindex)
2141  {
2142  printfPQExpBuffer(&buf,
2143  "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, ");
2144  if (pset.sversion >= 80200)
2145  appendPQExpBufferStr(&buf, "i.indisvalid, ");
2146  else
2147  appendPQExpBufferStr(&buf, "true as indisvalid, ");
2148  appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n ");
2149  if (pset.sversion >= 90000)
2150  appendPQExpBufferStr(&buf,
2151  "pg_catalog.pg_get_constraintdef(con.oid, true), "
2152  "contype, condeferrable, condeferred");
2153  else
2154  appendPQExpBufferStr(&buf,
2155  "null AS constraintdef, null AS contype, "
2156  "false AS condeferrable, false AS condeferred");
2157  if (pset.sversion >= 90400)
2158  appendPQExpBufferStr(&buf, ", i.indisreplident");
2159  else
2160  appendPQExpBufferStr(&buf, ", false AS indisreplident");
2161  if (pset.sversion >= 80000)
2162  appendPQExpBufferStr(&buf, ", c2.reltablespace");
2163  appendPQExpBufferStr(&buf,
2164  "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n");
2165  if (pset.sversion >= 90000)
2166  appendPQExpBufferStr(&buf,
2167  " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n");
2168  appendPQExpBuffer(&buf,
2169  "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
2170  "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;",
2171  oid);
2172  result = PSQLexec(buf.data);
2173  if (!result)
2174  goto error_return;
2175  else
2176  tuples = PQntuples(result);
2177 
2178  if (tuples > 0)
2179  {
2180  printTableAddFooter(&cont, _("Indexes:"));
2181  for (i = 0; i < tuples; i++)
2182  {
2183  /* untranslated index name */
2184  printfPQExpBuffer(&buf, " \"%s\"",
2185  PQgetvalue(result, i, 0));
2186 
2187  /* If exclusion constraint, print the constraintdef */
2188  if (strcmp(PQgetvalue(result, i, 7), "x") == 0)
2189  {
2190  appendPQExpBuffer(&buf, " %s",
2191  PQgetvalue(result, i, 6));
2192  }
2193  else
2194  {
2195  const char *indexdef;
2196  const char *usingpos;
2197 
2198  /* Label as primary key or unique (but not both) */
2199  if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
2200  appendPQExpBufferStr(&buf, " PRIMARY KEY,");
2201  else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
2202  {
2203  if (strcmp(PQgetvalue(result, i, 7), "u") == 0)
2204  appendPQExpBufferStr(&buf, " UNIQUE CONSTRAINT,");
2205  else
2206  appendPQExpBufferStr(&buf, " UNIQUE,");
2207  }
2208 
2209  /* Everything after "USING" is echoed verbatim */
2210  indexdef = PQgetvalue(result, i, 5);
2211  usingpos = strstr(indexdef, " USING ");
2212  if (usingpos)
2213  indexdef = usingpos + 7;
2214  appendPQExpBuffer(&buf, " %s", indexdef);
2215 
2216  /* Need these for deferrable PK/UNIQUE indexes */
2217  if (strcmp(PQgetvalue(result, i, 8), "t") == 0)
2218  appendPQExpBufferStr(&buf, " DEFERRABLE");
2219 
2220  if (strcmp(PQgetvalue(result, i, 9), "t") == 0)
2221  appendPQExpBufferStr(&buf, " INITIALLY DEFERRED");
2222  }
2223 
2224  /* Add these for all cases */
2225  if (strcmp(PQgetvalue(result, i, 3), "t") == 0)
2226  appendPQExpBufferStr(&buf, " CLUSTER");
2227 
2228  if (strcmp(PQgetvalue(result, i, 4), "t") != 0)
2229  appendPQExpBufferStr(&buf, " INVALID");
2230 
2231  if (strcmp(PQgetvalue(result, i, 10), "t") == 0)
2232  appendPQExpBuffer(&buf, " REPLICA IDENTITY");
2233 
2234  printTableAddFooter(&cont, buf.data);
2235 
2236  /* Print tablespace of the index on the same line */
2237  if (pset.sversion >= 80000)
2239  atooid(PQgetvalue(result, i, 11)),
2240  false);
2241  }
2242  }
2243  PQclear(result);
2244  }
2245 
2246  /* print table (and column) check constraints */
2247  if (tableinfo.checks)
2248  {
2249  printfPQExpBuffer(&buf,
2250  "SELECT r.conname, "
2251  "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
2252  "FROM pg_catalog.pg_constraint r\n"
2253  "WHERE r.conrelid = '%s' AND r.contype = 'c'\n"
2254  "ORDER BY 1;",
2255  oid);
2256  result = PSQLexec(buf.data);
2257  if (!result)
2258  goto error_return;
2259  else
2260  tuples = PQntuples(result);
2261 
2262  if (tuples > 0)
2263  {
2264  printTableAddFooter(&cont, _("Check constraints:"));
2265  for (i = 0; i < tuples; i++)
2266  {
2267  /* untranslated constraint name and def */
2268  printfPQExpBuffer(&buf, " \"%s\" %s",
2269  PQgetvalue(result, i, 0),
2270  PQgetvalue(result, i, 1));
2271 
2272  printTableAddFooter(&cont, buf.data);
2273  }
2274  }
2275  PQclear(result);
2276  }
2277 
2278  /* print foreign-key constraints (there are none if no triggers) */
2279  if (tableinfo.hastriggers)
2280  {
2281  printfPQExpBuffer(&buf,
2282  "SELECT conname,\n"
2283  " pg_catalog.pg_get_constraintdef(r.oid, true) as condef\n"
2284  "FROM pg_catalog.pg_constraint r\n"
2285  "WHERE r.conrelid = '%s' AND r.contype = 'f' ORDER BY 1;",
2286  oid);
2287  result = PSQLexec(buf.data);
2288  if (!result)
2289  goto error_return;
2290  else
2291  tuples = PQntuples(result);
2292 
2293  if (tuples > 0)
2294  {
2295  printTableAddFooter(&cont, _("Foreign-key constraints:"));
2296  for (i = 0; i < tuples; i++)
2297  {
2298  /* untranslated constraint name and def */
2299  printfPQExpBuffer(&buf, " \"%s\" %s",
2300  PQgetvalue(result, i, 0),
2301  PQgetvalue(result, i, 1));
2302 
2303  printTableAddFooter(&cont, buf.data);
2304  }
2305  }
2306  PQclear(result);
2307  }
2308 
2309  /* print incoming foreign-key references (none if no triggers) */
2310  if (tableinfo.hastriggers)
2311  {
2312  printfPQExpBuffer(&buf,
2313  "SELECT conname, conrelid::pg_catalog.regclass,\n"
2314  " pg_catalog.pg_get_constraintdef(c.oid, true) as condef\n"
2315  "FROM pg_catalog.pg_constraint c\n"
2316  "WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1;",
2317  oid);
2318  result = PSQLexec(buf.data);
2319  if (!result)
2320  goto error_return;
2321  else
2322  tuples = PQntuples(result);
2323 
2324  if (tuples > 0)
2325  {
2326  printTableAddFooter(&cont, _("Referenced by:"));
2327  for (i = 0; i < tuples; i++)
2328  {
2329  printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2330  PQgetvalue(result, i, 1),
2331  PQgetvalue(result, i, 0),
2332  PQgetvalue(result, i, 2));
2333 
2334  printTableAddFooter(&cont, buf.data);
2335  }
2336  }
2337  PQclear(result);
2338  }
2339 
2340  /* print any row-level policies */
2341  if (pset.sversion >= 90500)
2342  {
2343  printfPQExpBuffer(&buf, "SELECT pol.polname,");
2344  if (pset.sversion >= 100000)
2345  appendPQExpBuffer(&buf,
2346  " pol.polpermissive,\n");
2347  else
2348  appendPQExpBuffer(&buf,
2349  " 't' as polpermissive,\n");
2350  appendPQExpBuffer(&buf,
2351  " 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"
2352  " pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
2353  " pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
2354  " CASE pol.polcmd\n"
2355  " WHEN 'r' THEN 'SELECT'\n"
2356  " WHEN 'a' THEN 'INSERT'\n"
2357  " WHEN 'w' THEN 'UPDATE'\n"
2358  " WHEN 'd' THEN 'DELETE'\n"
2359  " END AS cmd\n"
2360  "FROM pg_catalog.pg_policy pol\n"
2361  "WHERE pol.polrelid = '%s' ORDER BY 1;",
2362  oid);
2363 
2364  result = PSQLexec(buf.data);
2365  if (!result)
2366  goto error_return;
2367  else
2368  tuples = PQntuples(result);
2369 
2370  /*
2371  * Handle cases where RLS is enabled and there are policies, or
2372  * there aren't policies, or RLS isn't enabled but there are
2373  * policies
2374  */
2375  if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples > 0)
2376  printTableAddFooter(&cont, _("Policies:"));
2377 
2378  if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples > 0)
2379  printTableAddFooter(&cont, _("Policies (forced row security enabled):"));
2380 
2381  if (tableinfo.rowsecurity && !tableinfo.forcerowsecurity && tuples == 0)
2382  printTableAddFooter(&cont, _("Policies (row security enabled): (none)"));
2383 
2384  if (tableinfo.rowsecurity && tableinfo.forcerowsecurity && tuples == 0)
2385  printTableAddFooter(&cont, _("Policies (forced row security enabled): (none)"));
2386 
2387  if (!tableinfo.rowsecurity && tuples > 0)
2388  printTableAddFooter(&cont, _("Policies (row security disabled):"));
2389 
2390  /* Might be an empty set - that's ok */
2391  for (i = 0; i < tuples; i++)
2392  {
2393  printfPQExpBuffer(&buf, " POLICY \"%s\"",
2394  PQgetvalue(result, i, 0));
2395 
2396  if (*(PQgetvalue(result, i, 1)) == 'f')
2397  appendPQExpBuffer(&buf, " AS RESTRICTIVE");
2398 
2399  if (!PQgetisnull(result, i, 5))
2400  appendPQExpBuffer(&buf, " FOR %s",
2401  PQgetvalue(result, i, 5));
2402 
2403  if (!PQgetisnull(result, i, 2))
2404  {
2405  appendPQExpBuffer(&buf, "\n TO %s",
2406  PQgetvalue(result, i, 2));
2407  }
2408 
2409  if (!PQgetisnull(result, i, 3))
2410  appendPQExpBuffer(&buf, "\n USING (%s)",
2411  PQgetvalue(result, i, 3));
2412 
2413  if (!PQgetisnull(result, i, 4))
2414  appendPQExpBuffer(&buf, "\n WITH CHECK (%s)",
2415  PQgetvalue(result, i, 4));
2416 
2417  printTableAddFooter(&cont, buf.data);
2418 
2419  }
2420  PQclear(result);
2421  }
2422 
2423  /* print any extended statistics */
2424  if (pset.sversion >= 100000)
2425  {
2426  printfPQExpBuffer(&buf,
2427  "SELECT oid, "
2428  "stxrelid::pg_catalog.regclass, "
2429  "stxnamespace::pg_catalog.regnamespace AS nsp, "
2430  "stxname,\n"
2431  " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')\n"
2432  " FROM pg_catalog.unnest(stxkeys) s(attnum)\n"
2433  " JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND\n"
2434  " a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
2435  " 'd' = any(stxkind) AS ndist_enabled,\n"
2436  " 'f' = any(stxkind) AS deps_enabled\n"
2437  "FROM pg_catalog.pg_statistic_ext stat "
2438  "WHERE stxrelid = '%s'\n"
2439  "ORDER BY 1;",
2440  oid);
2441 
2442  result = PSQLexec(buf.data);
2443  if (!result)
2444  goto error_return;
2445  else
2446  tuples = PQntuples(result);
2447 
2448  if (tuples > 0)
2449  {
2450  printTableAddFooter(&cont, _("Statistics objects:"));
2451 
2452  for (i = 0; i < tuples; i++)
2453  {
2454  bool gotone = false;
2455 
2456  printfPQExpBuffer(&buf, " ");
2457 
2458  /* statistics object name (qualified with namespace) */
2459  appendPQExpBuffer(&buf, "\"%s\".\"%s\" (",
2460  PQgetvalue(result, i, 2),
2461  PQgetvalue(result, i, 3));
2462 
2463  /* options */
2464  if (strcmp(PQgetvalue(result, i, 5), "t") == 0)
2465  {
2466  appendPQExpBufferStr(&buf, "ndistinct");
2467  gotone = true;
2468  }
2469 
2470  if (strcmp(PQgetvalue(result, i, 6), "t") == 0)
2471  {
2472  appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : "");
2473  }
2474 
2475  appendPQExpBuffer(&buf, ") ON %s FROM %s",
2476  PQgetvalue(result, i, 4),
2477  PQgetvalue(result, i, 1));
2478 
2479  printTableAddFooter(&cont, buf.data);
2480  }
2481  }
2482  PQclear(result);
2483  }
2484 
2485  /* print rules */
2486  if (tableinfo.hasrules && tableinfo.relkind != RELKIND_MATVIEW)
2487  {
2488  if (pset.sversion >= 80300)
2489  {
2490  printfPQExpBuffer(&buf,
2491  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2492  "ev_enabled\n"
2493  "FROM pg_catalog.pg_rewrite r\n"
2494  "WHERE r.ev_class = '%s' ORDER BY 1;",
2495  oid);
2496  }
2497  else
2498  {
2499  printfPQExpBuffer(&buf,
2500  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true)), "
2501  "'O' AS ev_enabled\n"
2502  "FROM pg_catalog.pg_rewrite r\n"
2503  "WHERE r.ev_class = '%s' ORDER BY 1;",
2504  oid);
2505  }
2506  result = PSQLexec(buf.data);
2507  if (!result)
2508  goto error_return;
2509  else
2510  tuples = PQntuples(result);
2511 
2512  if (tuples > 0)
2513  {
2514  bool have_heading;
2515  int category;
2516 
2517  for (category = 0; category < 4; category++)
2518  {
2519  have_heading = false;
2520 
2521  for (i = 0; i < tuples; i++)
2522  {
2523  const char *ruledef;
2524  bool list_rule = false;
2525 
2526  switch (category)
2527  {
2528  case 0:
2529  if (*PQgetvalue(result, i, 2) == 'O')
2530  list_rule = true;
2531  break;
2532  case 1:
2533  if (*PQgetvalue(result, i, 2) == 'D')
2534  list_rule = true;
2535  break;
2536  case 2:
2537  if (*PQgetvalue(result, i, 2) == 'A')
2538  list_rule = true;
2539  break;
2540  case 3:
2541  if (*PQgetvalue(result, i, 2) == 'R')
2542  list_rule = true;
2543  break;
2544  }
2545  if (!list_rule)
2546  continue;
2547 
2548  if (!have_heading)
2549  {
2550  switch (category)
2551  {
2552  case 0:
2553  printfPQExpBuffer(&buf, _("Rules:"));
2554  break;
2555  case 1:
2556  printfPQExpBuffer(&buf, _("Disabled rules:"));
2557  break;
2558  case 2:
2559  printfPQExpBuffer(&buf, _("Rules firing always:"));
2560  break;
2561  case 3:
2562  printfPQExpBuffer(&buf, _("Rules firing on replica only:"));
2563  break;
2564  }
2565  printTableAddFooter(&cont, buf.data);
2566  have_heading = true;
2567  }
2568 
2569  /* Everything after "CREATE RULE" is echoed verbatim */
2570  ruledef = PQgetvalue(result, i, 1);
2571  ruledef += 12;
2572  printfPQExpBuffer(&buf, " %s", ruledef);
2573  printTableAddFooter(&cont, buf.data);
2574  }
2575  }
2576  }
2577  PQclear(result);
2578  }
2579 
2580  /* print any publications */
2581  if (pset.sversion >= 100000)
2582  {
2583  printfPQExpBuffer(&buf,
2584  "SELECT pubname\n"
2585  "FROM pg_catalog.pg_publication p\n"
2586  "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
2587  "WHERE pr.prrelid = '%s'\n"
2588  "UNION ALL\n"
2589  "SELECT pubname\n"
2590  "FROM pg_catalog.pg_publication p\n"
2591  "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n"
2592  "ORDER BY 1;",
2593  oid, oid);
2594 
2595  result = PSQLexec(buf.data);
2596  if (!result)
2597  goto error_return;
2598  else
2599  tuples = PQntuples(result);
2600 
2601  if (tuples > 0)
2602  printTableAddFooter(&cont, _("Publications:"));
2603 
2604  /* Might be an empty set - that's ok */
2605  for (i = 0; i < tuples; i++)
2606  {
2607  printfPQExpBuffer(&buf, " \"%s\"",
2608  PQgetvalue(result, i, 0));
2609 
2610  printTableAddFooter(&cont, buf.data);
2611  }
2612  PQclear(result);
2613  }
2614  }
2615 
2616  if (view_def)
2617  {
2618  PGresult *result = NULL;
2619 
2620  /* Footer information about a view */
2621  printTableAddFooter(&cont, _("View definition:"));
2622  printTableAddFooter(&cont, view_def);
2623 
2624  /* print rules */
2625  if (tableinfo.hasrules)
2626  {
2627  printfPQExpBuffer(&buf,
2628  "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
2629  "FROM pg_catalog.pg_rewrite r\n"
2630  "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN' ORDER BY 1;",
2631  oid);
2632  result = PSQLexec(buf.data);
2633  if (!result)
2634  goto error_return;
2635 
2636  if (PQntuples(result) > 0)
2637  {
2638  printTableAddFooter(&cont, _("Rules:"));
2639  for (i = 0; i < PQntuples(result); i++)
2640  {
2641  const char *ruledef;
2642 
2643  /* Everything after "CREATE RULE" is echoed verbatim */
2644  ruledef = PQgetvalue(result, i, 1);
2645  ruledef += 12;
2646 
2647  printfPQExpBuffer(&buf, " %s", ruledef);
2648  printTableAddFooter(&cont, buf.data);
2649  }
2650  }
2651  PQclear(result);
2652  }
2653  }
2654 
2655  /*
2656  * Print triggers next, if any (but only user-defined triggers). This
2657  * could apply to either a table or a view.
2658  */
2659  if (tableinfo.hastriggers)
2660  {
2661  PGresult *result;
2662  int tuples;
2663 
2664  printfPQExpBuffer(&buf,
2665  "SELECT t.tgname, "
2666  "pg_catalog.pg_get_triggerdef(t.oid%s), "
2667  "t.tgenabled, %s\n"
2668  "FROM pg_catalog.pg_trigger t\n"
2669  "WHERE t.tgrelid = '%s' AND ",
2670  (pset.sversion >= 90000 ? ", true" : ""),
2671  (pset.sversion >= 90000 ? "t.tgisinternal" :
2672  pset.sversion >= 80300 ?
2673  "t.tgconstraint <> 0 AS tgisinternal" :
2674  "false AS tgisinternal"), oid);
2675  if (pset.sversion >= 90000)
2676  /* display/warn about disabled internal triggers */
2677  appendPQExpBuffer(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D'))");
2678  else if (pset.sversion >= 80300)
2679  appendPQExpBufferStr(&buf, "(t.tgconstraint = 0 OR (t.tgconstraint <> 0 AND t.tgenabled = 'D'))");
2680  else
2681  appendPQExpBufferStr(&buf,
2682  "(NOT tgisconstraint "
2683  " OR NOT EXISTS"
2684  " (SELECT 1 FROM pg_catalog.pg_depend d "
2685  " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
2686  " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))");
2687  appendPQExpBufferStr(&buf, "\nORDER BY 1;");
2688 
2689  result = PSQLexec(buf.data);
2690  if (!result)
2691  goto error_return;
2692  else
2693  tuples = PQntuples(result);
2694 
2695  if (tuples > 0)
2696  {
2697  bool have_heading;
2698  int category;
2699 
2700  /*
2701  * split the output into 4 different categories. Enabled triggers,
2702  * disabled triggers and the two special ALWAYS and REPLICA
2703  * configurations.
2704  */
2705  for (category = 0; category <= 4; category++)
2706  {
2707  have_heading = false;
2708  for (i = 0; i < tuples; i++)
2709  {
2710  bool list_trigger;
2711  const char *tgdef;
2712  const char *usingpos;
2713  const char *tgenabled;
2714  const char *tgisinternal;
2715 
2716  /*
2717  * Check if this trigger falls into the current category
2718  */
2719  tgenabled = PQgetvalue(result, i, 2);
2720  tgisinternal = PQgetvalue(result, i, 3);
2721  list_trigger = false;
2722  switch (category)
2723  {
2724  case 0:
2725  if (*tgenabled == 'O' || *tgenabled == 't')
2726  list_trigger = true;
2727  break;
2728  case 1:
2729  if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2730  *tgisinternal == 'f')
2731  list_trigger = true;
2732  break;
2733  case 2:
2734  if ((*tgenabled == 'D' || *tgenabled == 'f') &&
2735  *tgisinternal == 't')
2736  list_trigger = true;
2737  break;
2738  case 3:
2739  if (*tgenabled == 'A')
2740  list_trigger = true;
2741  break;
2742  case 4:
2743  if (*tgenabled == 'R')
2744  list_trigger = true;
2745  break;
2746  }
2747  if (list_trigger == false)
2748  continue;
2749 
2750  /* Print the category heading once */
2751  if (have_heading == false)
2752  {
2753  switch (category)
2754  {
2755  case 0:
2756  printfPQExpBuffer(&buf, _("Triggers:"));
2757  break;
2758  case 1:
2759  if (pset.sversion >= 80300)
2760  printfPQExpBuffer(&buf, _("Disabled user triggers:"));
2761  else
2762  printfPQExpBuffer(&buf, _("Disabled triggers:"));
2763  break;
2764  case 2:
2765  printfPQExpBuffer(&buf, _("Disabled internal triggers:"));
2766  break;
2767  case 3:
2768  printfPQExpBuffer(&buf, _("Triggers firing always:"));
2769  break;
2770  case 4:
2771  printfPQExpBuffer(&buf, _("Triggers firing on replica only:"));
2772  break;
2773 
2774  }
2775  printTableAddFooter(&cont, buf.data);
2776  have_heading = true;
2777  }
2778 
2779  /* Everything after "TRIGGER" is echoed verbatim */
2780  tgdef = PQgetvalue(result, i, 1);
2781  usingpos = strstr(tgdef, " TRIGGER ");
2782  if (usingpos)
2783  tgdef = usingpos + 9;
2784 
2785  printfPQExpBuffer(&buf, " %s", tgdef);
2786  printTableAddFooter(&cont, buf.data);
2787  }
2788  }
2789  }
2790  PQclear(result);
2791  }
2792 
2793  /*
2794  * Finish printing the footer information about a table.
2795  */
2796  if (tableinfo.relkind == RELKIND_RELATION ||
2797  tableinfo.relkind == RELKIND_MATVIEW ||
2798  tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
2799  tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2800  {
2801  PGresult *result;
2802  int tuples;
2803 
2804  /* print foreign server name */
2805  if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
2806  {
2807  char *ftoptions;
2808 
2809  /* Footer information about foreign table */
2810  printfPQExpBuffer(&buf,
2811  "SELECT s.srvname,\n"
2812  " pg_catalog.array_to_string(ARRAY(\n"
2813  " SELECT pg_catalog.quote_ident(option_name)"
2814  " || ' ' || pg_catalog.quote_literal(option_value)\n"
2815  " FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
2816  "FROM pg_catalog.pg_foreign_table f,\n"
2817  " pg_catalog.pg_foreign_server s\n"
2818  "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
2819  oid);
2820  result = PSQLexec(buf.data);
2821  if (!result)
2822  goto error_return;
2823  else if (PQntuples(result) != 1)
2824  {
2825  PQclear(result);
2826  goto error_return;
2827  }
2828 
2829  /* Print server name */
2830  printfPQExpBuffer(&buf, _("Server: %s"),
2831  PQgetvalue(result, 0, 0));
2832  printTableAddFooter(&cont, buf.data);
2833 
2834  /* Print per-table FDW options, if any */
2835  ftoptions = PQgetvalue(result, 0, 1);
2836  if (ftoptions && ftoptions[0] != '\0')
2837  {
2838  printfPQExpBuffer(&buf, _("FDW options: (%s)"), ftoptions);
2839  printTableAddFooter(&cont, buf.data);
2840  }
2841  PQclear(result);
2842  }
2843 
2844  /* print inherited tables (exclude, if parent is a partitioned table) */
2845  printfPQExpBuffer(&buf,
2846  "SELECT c.oid::pg_catalog.regclass"
2847  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2848  " WHERE c.oid=i.inhparent AND i.inhrelid = '%s'"
2849  " AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_TABLE)
2850  " ORDER BY inhseqno;", oid);
2851 
2852  result = PSQLexec(buf.data);
2853  if (!result)
2854  goto error_return;
2855  else
2856  {
2857  const char *s = _("Inherits");
2858  int sw = pg_wcswidth(s, strlen(s), pset.encoding);
2859 
2860  tuples = PQntuples(result);
2861 
2862  for (i = 0; i < tuples; i++)
2863  {
2864  if (i == 0)
2865  printfPQExpBuffer(&buf, "%s: %s",
2866  s, PQgetvalue(result, i, 0));
2867  else
2868  printfPQExpBuffer(&buf, "%*s %s",
2869  sw, "", PQgetvalue(result, i, 0));
2870  if (i < tuples - 1)
2871  appendPQExpBufferChar(&buf, ',');
2872 
2873  printTableAddFooter(&cont, buf.data);
2874  }
2875 
2876  PQclear(result);
2877  }
2878 
2879  /* print child tables (with additional info if partitions) */
2880  if (pset.sversion >= 100000)
2881  printfPQExpBuffer(&buf,
2882  "SELECT c.oid::pg_catalog.regclass,"
2883  " pg_catalog.pg_get_expr(c.relpartbound, c.oid),"
2884  " c.relkind"
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 pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
2888  " c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
2889  else if (pset.sversion >= 80300)
2890  printfPQExpBuffer(&buf,
2891  "SELECT c.oid::pg_catalog.regclass"
2892  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2893  " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
2894  " ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
2895  else
2896  printfPQExpBuffer(&buf,
2897  "SELECT c.oid::pg_catalog.regclass"
2898  " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
2899  " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
2900  " ORDER BY c.relname;", oid);
2901 
2902  result = PSQLexec(buf.data);
2903  if (!result)
2904  goto error_return;
2905  else
2906  tuples = PQntuples(result);
2907 
2908  /*
2909  * For a partitioned table with no partitions, always print the number
2910  * of partitions as zero, even when verbose output is expected.
2911  * Otherwise, we will not print "Partitions" section for a partitioned
2912  * table without any partitions.
2913  */
2914  if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE && tuples == 0)
2915  {
2916  printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
2917  printTableAddFooter(&cont, buf.data);
2918  }
2919  else if (!verbose)
2920  {
2921  /* print the number of child tables, if any */
2922  if (tuples > 0)
2923  {
2924  if (tableinfo.relkind != RELKIND_PARTITIONED_TABLE)
2925  printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
2926  else
2927  printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
2928  printTableAddFooter(&cont, buf.data);
2929  }
2930  }
2931  else
2932  {
2933  /* display the list of child tables */
2934  const char *ct = (tableinfo.relkind != RELKIND_PARTITIONED_TABLE) ?
2935  _("Child tables") : _("Partitions");
2936  int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
2937 
2938  for (i = 0; i < tuples; i++)
2939  {
2940  if (tableinfo.relkind != RELKIND_PARTITIONED_TABLE)
2941  {
2942  if (i == 0)
2943  printfPQExpBuffer(&buf, "%s: %s",
2944  ct, PQgetvalue(result, i, 0));
2945  else
2946  printfPQExpBuffer(&buf, "%*s %s",
2947  ctw, "", PQgetvalue(result, i, 0));
2948  }
2949  else
2950  {
2951  char *partitioned_note;
2952 
2953  if (*PQgetvalue(result, i, 2) == RELKIND_PARTITIONED_TABLE)
2954  partitioned_note = ", PARTITIONED";
2955  else
2956  partitioned_note = "";
2957 
2958  if (i == 0)
2959  printfPQExpBuffer(&buf, "%s: %s %s%s",
2960  ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
2961  partitioned_note);
2962  else
2963  printfPQExpBuffer(&buf, "%*s %s %s%s",
2964  ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
2965  partitioned_note);
2966  }
2967  if (i < tuples - 1)
2968  appendPQExpBufferChar(&buf, ',');
2969 
2970  printTableAddFooter(&cont, buf.data);
2971  }
2972  }
2973  PQclear(result);
2974 
2975  /* Table type */
2976  if (tableinfo.reloftype)
2977  {
2978  printfPQExpBuffer(&buf, _("Typed table of type: %s"), tableinfo.reloftype);
2979  printTableAddFooter(&cont, buf.data);
2980  }
2981 
2982  if (verbose &&
2983  (tableinfo.relkind == RELKIND_RELATION ||
2984  tableinfo.relkind == RELKIND_MATVIEW) &&
2985 
2986  /*
2987  * No need to display default values; we already display a REPLICA
2988  * IDENTITY marker on indexes.
2989  */
2990  tableinfo.relreplident != 'i' &&
2991  ((strcmp(schemaname, "pg_catalog") != 0 && tableinfo.relreplident != 'd') ||
2992  (strcmp(schemaname, "pg_catalog") == 0 && tableinfo.relreplident != 'n')))
2993  {
2994  const char *s = _("Replica Identity");
2995 
2996  printfPQExpBuffer(&buf, "%s: %s",
2997  s,
2998  tableinfo.relreplident == 'f' ? "FULL" :
2999  tableinfo.relreplident == 'n' ? "NOTHING" :
3000  "???");
3001 
3002  printTableAddFooter(&cont, buf.data);
3003  }
3004 
3005  /* OIDs, if verbose and not a materialized view */
3006  if (verbose && tableinfo.relkind != RELKIND_MATVIEW && tableinfo.hasoids)
3007  printTableAddFooter(&cont, _("Has OIDs: yes"));
3008 
3009  /* Tablespace info */
3010  add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
3011  true);
3012  }
3013 
3014  /* reloptions, if verbose */
3015  if (verbose &&
3016  tableinfo.reloptions && tableinfo.reloptions[0] != '\0')
3017  {
3018  const char *t = _("Options");
3019 
3020  printfPQExpBuffer(&buf, "%s: %s", t, tableinfo.reloptions);
3021  printTableAddFooter(&cont, buf.data);
3022  }
3023 
3024  printTable(&cont, pset.queryFout, false, pset.logfile);
3025 
3026  retval = true;
3027 
3028 error_return:
3029 
3030  /* clean up */
3031  if (printTableInitialized)
3032  printTableCleanup(&cont);
3033  termPQExpBuffer(&buf);
3034  termPQExpBuffer(&title);
3035  termPQExpBuffer(&tmpbuf);
3036 
3037  if (view_def)
3038  free(view_def);
3039 
3040  if (res)
3041  PQclear(res);
3042 
3043  return retval;
3044 }
signed short int16
Definition: c.h:301
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:1025
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:146
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
#define RELKIND_PARTITIONED_INDEX
Definition: pg_class.h:169
static int verbose
Definition: pg_basebackup.c:85
#define CppAsString2(x)
Definition: c.h:224
#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:3052
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:163
#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 4603 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().

4605 {
4607  title;
4608  PGresult *res;
4609  printQueryOpt myopt = pset.popt;
4610 
4611  initPQExpBuffer(&buf);
4612 
4613  printfPQExpBuffer(&buf,
4614  "SELECT\n"
4615  " ( SELECT t.alias FROM\n"
4616  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
4617  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
4618  " pg_catalog.btrim(\n"
4619  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
4620  " FROM pg_catalog.pg_ts_config_map AS mm\n"
4621  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
4622  " ORDER BY mapcfg, maptokentype, mapseqno\n"
4623  " ) :: pg_catalog.text,\n"
4624  " '{}') AS \"%s\"\n"
4625  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
4626  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
4627  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
4628  "ORDER BY 1;",
4629  gettext_noop("Token"),
4630  gettext_noop("Dictionaries"),
4631  oid);
4632 
4633  res = PSQLexec(buf.data);
4634  termPQExpBuffer(&buf);
4635  if (!res)
4636  return false;
4637 
4638  initPQExpBuffer(&title);
4639 
4640  if (nspname)
4641  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4642  nspname, cfgname);
4643  else
4644  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4645  cfgname);
4646 
4647  if (pnspname)
4648  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4649  pnspname, prsname);
4650  else
4651  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4652  prsname);
4653 
4654  myopt.nullPrint = NULL;
4655  myopt.title = title.data;
4656  myopt.footers = NULL;
4657  myopt.topt.default_footer = false;
4658  myopt.translate_header = true;
4659 
4660  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4661 
4662  termPQExpBuffer(&title);
4663 
4664  PQclear(res);
4665  return true;
4666 }
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:1025
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 4208 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().

4209 {
4211  PGresult *res;
4212  PQExpBufferData title;
4213  printQueryOpt myopt = pset.popt;
4214  static const bool translate_columns[] = {true, false, false};
4215 
4216  initPQExpBuffer(&buf);
4217 
4218  printfPQExpBuffer(&buf,
4219  "SELECT '%s' AS \"%s\",\n"
4220  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4221  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4222  " FROM pg_catalog.pg_ts_parser p\n"
4223  " WHERE p.oid = '%s'\n"
4224  "UNION ALL\n"
4225  "SELECT '%s',\n"
4226  " p.prstoken::pg_catalog.regproc,\n"
4227  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4228  " FROM pg_catalog.pg_ts_parser p\n"
4229  " WHERE p.oid = '%s'\n"
4230  "UNION ALL\n"
4231  "SELECT '%s',\n"
4232  " p.prsend::pg_catalog.regproc,\n"
4233  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4234  " FROM pg_catalog.pg_ts_parser p\n"
4235  " WHERE p.oid = '%s'\n"
4236  "UNION ALL\n"
4237  "SELECT '%s',\n"
4238  " p.prsheadline::pg_catalog.regproc,\n"
4239  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4240  " FROM pg_catalog.pg_ts_parser p\n"
4241  " WHERE p.oid = '%s'\n"
4242  "UNION ALL\n"
4243  "SELECT '%s',\n"
4244  " p.prslextype::pg_catalog.regproc,\n"
4245  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4246  " FROM pg_catalog.pg_ts_parser p\n"
4247  " WHERE p.oid = '%s';",
4248  gettext_noop("Start parse"),
4249  gettext_noop("Method"),
4250  gettext_noop("Function"),
4251  gettext_noop("Description"),
4252  oid,
4253  gettext_noop("Get next token"),
4254  oid,
4255  gettext_noop("End parse"),
4256  oid,
4257  gettext_noop("Get headline"),
4258  oid,
4259  gettext_noop("Get token types"),
4260  oid);
4261 
4262  res = PSQLexec(buf.data);
4263  termPQExpBuffer(&buf);
4264  if (!res)
4265  return false;
4266 
4267  myopt.nullPrint = NULL;
4268  initPQExpBuffer(&title);
4269  if (nspname)
4270  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4271  nspname, prsname);
4272  else
4273  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4274  myopt.title = title.data;
4275  myopt.footers = NULL;
4276  myopt.topt.default_footer = false;
4277  myopt.translate_header = true;
4278  myopt.translate_columns = translate_columns;
4279  myopt.n_translate_columns = lengthof(translate_columns);
4280 
4281  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4282 
4283  PQclear(res);
4284 
4285  initPQExpBuffer(&buf);
4286 
4287  printfPQExpBuffer(&buf,
4288  "SELECT t.alias as \"%s\",\n"
4289  " t.description as \"%s\"\n"
4290  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4291  "ORDER BY 1;",
4292  gettext_noop("Token name"),
4293  gettext_noop("Description"),
4294  oid);
4295 
4296  res = PSQLexec(buf.data);
4297  termPQExpBuffer(&buf);
4298  if (!res)
4299  return false;
4300 
4301  myopt.nullPrint = NULL;
4302  if (nspname)
4303  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4304  nspname, prsname);
4305  else
4306  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4307  myopt.title = title.data;
4308  myopt.footers = NULL;
4309  myopt.topt.default_footer = true;
4310  myopt.translate_header = true;
4311  myopt.translate_columns = NULL;
4312  myopt.n_translate_columns = 0;
4313 
4314  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4315 
4316  termPQExpBuffer(&title);
4317  PQclear(res);
4318  return true;
4319 }
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:1025
FILE * queryFout
Definition: settings.h:84
#define lengthof(array)
Definition: c.h:618
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:1025
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 5216 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().

5217 {
5219  int i;
5220  PGresult *res;
5221 
5222  if (pset.sversion < 100000)
5223  {
5224  char sverbuf[32];
5225 
5226  psql_error("The server (version %s) does not support publications.\n",
5228  sverbuf, sizeof(sverbuf)));
5229  return true;
5230  }
5231 
5232  initPQExpBuffer(&buf);
5233 
5234  printfPQExpBuffer(&buf,
5235  "SELECT oid, pubname,\n"
5236  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
5237  " puballtables, pubinsert, pubupdate, pubdelete\n"
5238  "FROM pg_catalog.pg_publication\n");
5239 
5240  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5241  NULL, "pubname", NULL,
5242  NULL);
5243 
5244  appendPQExpBufferStr(&buf, "ORDER BY 2;");
5245 
5246  res = PSQLexec(buf.data);
5247  if (!res)
5248  {
5249  termPQExpBuffer(&buf);
5250  return false;
5251  }
5252 
5253  if (PQntuples(res) == 0)
5254  {
5255  if (!pset.quiet)
5256  {
5257  if (pattern)
5258  psql_error("Did not find any publication named \"%s\".\n",
5259  pattern);
5260  else
5261  psql_error("Did not find any publications.\n");
5262  }
5263 
5264  termPQExpBuffer(&buf);
5265  PQclear(res);
5266  return false;
5267  }
5268 
5269  for (i = 0; i < PQntuples(res); i++)
5270  {
5271  const char align = 'l';
5272  int ncols = 5;
5273  int nrows = 1;
5274  int tables = 0;
5275  PGresult *tabres;
5276  char *pubid = PQgetvalue(res, i, 0);
5277  char *pubname = PQgetvalue(res, i, 1);
5278  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
5279  int j;
5280  PQExpBufferData title;
5281  printTableOpt myopt = pset.popt.topt;
5282  printTableContent cont;
5283 
5284  initPQExpBuffer(&title);
5285  printfPQExpBuffer(&title, _("Publication %s"), pubname);
5286  printTableInit(&cont, &myopt, title.data, ncols, nrows);
5287 
5288  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
5289  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
5290  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
5291  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
5292  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
5293 
5294  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
5295  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
5296  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
5297  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
5298  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
5299 
5300  if (!puballtables)
5301  {
5302  printfPQExpBuffer(&buf,
5303  "SELECT n.nspname, c.relname\n"
5304  "FROM pg_catalog.pg_class c,\n"
5305  " pg_catalog.pg_namespace n,\n"
5306  " pg_catalog.pg_publication_rel pr\n"
5307  "WHERE c.relnamespace = n.oid\n"
5308  " AND c.oid = pr.prrelid\n"
5309  " AND pr.prpubid = '%s'\n"
5310  "ORDER BY 1,2", pubid);
5311 
5312  tabres = PSQLexec(buf.data);
5313  if (!tabres)
5314  {
5315  printTableCleanup(&cont);
5316  PQclear(res);
5317  termPQExpBuffer(&buf);
5318  termPQExpBuffer(&title);
5319  return false;
5320  }
5321  else
5322  tables = PQntuples(tabres);
5323 
5324  if (tables > 0)
5325  printTableAddFooter(&cont, _("Tables:"));
5326 
5327  for (j = 0; j < tables; j++)
5328  {
5329  printfPQExpBuffer(&buf, " \"%s.%s\"",
5330  PQgetvalue(tabres, j, 0),
5331  PQgetvalue(tabres, j, 1));
5332 
5333  printTableAddFooter(&cont, buf.data);
5334  }
5335  PQclear(tabres);
5336  }
5337 
5338  printTable(&cont, pset.queryFout, false, pset.logfile);
5339  printTableCleanup(&cont);
5340 
5341  termPQExpBuffer(&title);
5342  }
5343 
5344  termPQExpBuffer(&buf);
5345  PQclear(res);
5346 
5347  return true;
5348 }
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:1025
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 3116 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().

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

5358 {
5360  PGresult *res;
5361  printQueryOpt myopt = pset.popt;
5362  static const bool translate_columns[] = {false, false, false, false,
5363  false, false};
5364 
5365  if (pset.sversion < 100000)
5366  {
5367  char sverbuf[32];
5368 
5369  psql_error("The server (version %s) does not support subscriptions.\n",
5371  sverbuf, sizeof(sverbuf)));
5372  return true;
5373  }
5374 
5375  initPQExpBuffer(&buf);
5376 
5377  printfPQExpBuffer(&buf,
5378  "SELECT subname AS \"%s\"\n"
5379  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
5380  ", subenabled AS \"%s\"\n"
5381  ", subpublications AS \"%s\"\n",
5382  gettext_noop("Name"),
5383  gettext_noop("Owner"),
5384  gettext_noop("Enabled"),
5385  gettext_noop("Publication"));
5386 
5387  if (verbose)
5388  {
5389  appendPQExpBuffer(&buf,
5390  ", subsynccommit AS \"%s\"\n"
5391  ", subconninfo AS \"%s\"\n",
5392  gettext_noop("Synchronous commit"),
5393  gettext_noop("Conninfo"));
5394  }
5395 
5396  /* Only display subscriptions in current database. */
5397  appendPQExpBufferStr(&buf,
5398  "FROM pg_catalog.pg_subscription\n"
5399  "WHERE subdbid = (SELECT oid\n"
5400  " FROM pg_catalog.pg_database\n"
5401  " WHERE datname = pg_catalog.current_database())");
5402 
5403  processSQLNamePattern(pset.db, &buf, pattern, true, false,
5404  NULL, "subname", NULL,
5405  NULL);
5406 
5407  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5408 
5409  res = PSQLexec(buf.data);
5410  termPQExpBuffer(&buf);
5411  if (!res)
5412  return false;
5413 
5414  myopt.nullPrint = NULL;
5415  myopt.title = _("List of subscriptions");
5416  myopt.translate_header = true;
5417  myopt.translate_columns = translate_columns;
5418  myopt.n_translate_columns = lengthof(translate_columns);
5419 
5420  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5421 
5422  PQclear(res);
5423  return true;
5424 }
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:1025
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:618
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:1025
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:5434
#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:1025
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:224
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:5434
#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:1025
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:5434
#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 3840 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().

3841 {
3843  PGresult *res;
3844  printQueryOpt myopt = pset.popt;
3845  static const bool translate_columns[] = {false, false, false, true, false};
3846 
3847  initPQExpBuffer(&buf);
3848 
3849  /*
3850  * We need a left join to pg_proc for binary casts; the others are just
3851  * paranoia. Also note that we don't attempt to localize '(binary
3852  * coercible)', because there's too much risk of gettext translating a
3853  * function name that happens to match some string in the PO database.
3854  */
3855  printfPQExpBuffer(&buf,
3856  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3857  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3858  " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3859  " ELSE p.proname\n"
3860  " END as \"%s\",\n"
3861  " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3862  " WHEN c.castcontext = 'a' THEN '%s'\n"
3863  " ELSE '%s'\n"
3864  " END as \"%s\"",
3865  gettext_noop("Source type"),
3866  gettext_noop("Target type"),
3867  gettext_noop("Function"),
3868  gettext_noop("no"),
3869  gettext_noop("in assignment"),
3870  gettext_noop("yes"),
3871  gettext_noop("Implicit?"));
3872 
3873  if (verbose)
3874  appendPQExpBuffer(&buf,
3875  ",\n d.description AS \"%s\"\n",
3876  gettext_noop("Description"));
3877 
3878  appendPQExpBufferStr(&buf,
3879  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3880  " ON c.castfunc = p.oid\n"
3881  " LEFT JOIN pg_catalog.pg_type ts\n"
3882  " ON c.castsource = ts.oid\n"
3883  " LEFT JOIN pg_catalog.pg_namespace ns\n"
3884  " ON ns.oid = ts.typnamespace\n"
3885  " LEFT JOIN pg_catalog.pg_type tt\n"
3886  " ON c.casttarget = tt.oid\n"
3887  " LEFT JOIN pg_catalog.pg_namespace nt\n"
3888  " ON nt.oid = tt.typnamespace\n");
3889 
3890  if (verbose)
3891  appendPQExpBufferStr(&buf,
3892  " LEFT JOIN pg_catalog.pg_description d\n"
3893  " ON d.classoid = c.tableoid AND d.objoid = "
3894  "c.oid AND d.objsubid = 0\n");
3895 
3896  appendPQExpBufferStr(&buf, "WHERE ( (true");
3897 
3898  /*
3899  * Match name pattern against either internal or external name of either
3900  * castsource or casttarget
3901  */
3902  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3903  "ns.nspname", "ts.typname",
3904  "pg_catalog.format_type(ts.oid, NULL)",
3905  "pg_catalog.pg_type_is_visible(ts.oid)");
3906 
3907  appendPQExpBufferStr(&buf, ") OR (true");
3908 
3909  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3910  "nt.nspname", "tt.typname",
3911  "pg_catalog.format_type(tt.oid, NULL)",
3912  "pg_catalog.pg_type_is_visible(tt.oid)");
3913 
3914  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3915 
3916  res = PSQLexec(buf.data);
3917  termPQExpBuffer(&buf);
3918  if (!res)
3919  return false;
3920 
3921  myopt.nullPrint = NULL;
3922  myopt.title = _("List of casts");
3923  myopt.translate_header = true;
3924  myopt.translate_columns = translate_columns;
3925  myopt.n_translate_columns = lengthof(translate_columns);
3926 
3927  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3928 
3929  PQclear(res);
3930  return true;
3931 }
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:1025
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:618
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 3939 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().

3940 {
3942  PGresult *res;
3943  printQueryOpt myopt = pset.popt;
3944  static const bool translate_columns[] = {false, false, false, false, false, false};
3945 
3946  if (pset.sversion < 90100)
3947  {
3948  char sverbuf[32];
3949 
3950  psql_error("The server (version %s) does not support collations.\n",
3952  sverbuf, sizeof(sverbuf)));
3953  return true;
3954  }
3955 
3956  initPQExpBuffer(&buf);
3957 
3958  printfPQExpBuffer(&buf,
3959  "SELECT n.nspname AS \"%s\",\n"
3960  " c.collname AS \"%s\",\n"
3961  " c.collcollate AS \"%s\",\n"
3962  " c.collctype AS \"%s\"",
3963  gettext_noop("Schema"),
3964  gettext_noop("Name"),
3965  gettext_noop("Collate"),
3966  gettext_noop("Ctype"));
3967 
3968  if (pset.sversion >= 100000)
3969  appendPQExpBuffer(&buf,
3970  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
3971  gettext_noop("Provider"));
3972 
3973  if (verbose)
3974  appendPQExpBuffer(&buf,
3975  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3976  gettext_noop("Description"));
3977 
3978  appendPQExpBufferStr(&buf,
3979  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3980  "WHERE n.oid = c.collnamespace\n");
3981 
3982  if (!showSystem && !pattern)
3983  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3984  " AND n.nspname <> 'information_schema'\n");
3985 
3986  /*
3987  * Hide collations that aren't usable in the current database's encoding.
3988  * If you think to change this, note that pg_collation_is_visible rejects
3989  * unusable collations, so you will need to hack name pattern processing
3990  * somehow to avoid inconsistent behavior.
3991  */
3992  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3993 
3994  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3995  "n.nspname", "c.collname", NULL,
3996  "pg_catalog.pg_collation_is_visible(c.oid)");
3997 
3998  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3999 
4000  res = PSQLexec(buf.data);
4001  termPQExpBuffer(&buf);
4002  if (!res)
4003  return false;
4004 
4005  myopt.nullPrint = NULL;
4006  myopt.title = _("List of collations");
4007  myopt.translate_header = true;
4008  myopt.translate_columns = translate_columns;
4009  myopt.n_translate_columns = lengthof(translate_columns);
4010 
4011  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4012 
4013  PQclear(res);
4014  return true;
4015 }
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:1025
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:618
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 3698 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().

3699 {
3701  PGresult *res;
3702  printQueryOpt myopt = pset.popt;
3703  static const bool translate_columns[] =
3704  {false, false, false, false, true, false};
3705 
3706  initPQExpBuffer(&buf);
3707 
3708  printfPQExpBuffer(&buf,
3709  "SELECT n.nspname AS \"%s\",\n"
3710  " c.conname AS \"%s\",\n"
3711  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3712  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3713  " CASE WHEN c.condefault THEN '%s'\n"
3714  " ELSE '%s' END AS \"%s\"",
3715  gettext_noop("Schema"),
3716  gettext_noop("Name"),
3717  gettext_noop("Source"),
3718  gettext_noop("Destination"),
3719  gettext_noop("yes"), gettext_noop("no"),
3720  gettext_noop("Default?"));
3721 
3722  if (verbose)
3723  appendPQExpBuffer(&buf,
3724  ",\n d.description AS \"%s\"",
3725  gettext_noop("Description"));
3726 
3727  appendPQExpBufferStr(&buf,
3728  "\nFROM pg_catalog.pg_conversion c\n"
3729  " JOIN pg_catalog.pg_namespace n "
3730  "ON n.oid = c.connamespace\n");
3731 
3732  if (verbose)
3733  appendPQExpBufferStr(&buf,
3734  "LEFT JOIN pg_catalog.pg_description d "
3735  "ON d.classoid = c.tableoid\n"
3736  " AND d.objoid = c.oid "
3737  "AND d.objsubid = 0\n");
3738 
3739  appendPQExpBufferStr(&buf, "WHERE true\n");
3740 
3741  if (!showSystem && !pattern)
3742  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3743  " AND n.nspname <> 'information_schema'\n");
3744 
3745  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3746  "n.nspname", "c.conname", NULL,
3747  "pg_catalog.pg_conversion_is_visible(c.oid)");
3748 
3749  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3750 
3751  res = PSQLexec(buf.data);
3752  termPQExpBuffer(&buf);
3753  if (!res)
3754  return false;
3755 
3756  myopt.nullPrint = NULL;
3757  myopt.title = _("List of conversions");
3758  myopt.translate_header = true;
3759  myopt.translate_columns = translate_columns;
3760  myopt.n_translate_columns = lengthof(translate_columns);
3761 
3762  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3763 
3764  PQclear(res);
3765  return true;
3766 }
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:1025
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:618
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 3287 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().

3288 {
3290  PGresult *res;
3291  printQueryOpt myopt = pset.popt;
3292  bool havewhere;
3293 
3294  if (pset.sversion < 90000)
3295  {
3296  char sverbuf[32];
3297 
3298  psql_error("The server (version %s) does not support per-database role settings.\n",
3300  sverbuf, sizeof(sverbuf)));
3301  return true;
3302  }
3303 
3304  initPQExpBuffer(&buf);
3305 
3306  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3307  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3308  "FROM pg_catalog.pg_db_role_setting s\n"
3309  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3310  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3311  gettext_noop("Role"),
3312  gettext_noop("Database"),
3313  gettext_noop("Settings"));
3314  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3315  NULL, "r.rolname", NULL, NULL);
3316  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3317  NULL, "d.datname", NULL, NULL);
3318  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3319 
3320  res = PSQLexec(buf.data);
3321  termPQExpBuffer(&buf);
3322  if (!res)
3323  return false;
3324 
3325  /*
3326  * Most functions in this file are content to print an empty table when
3327  * there are no matching objects. We intentionally deviate from that
3328  * here, but only in !quiet mode, because of the possibility that the user
3329  * is confused about what the two pattern arguments mean.
3330  */
3331  if (PQntuples(res) == 0 && !pset.quiet)
3332  {
3333  if (pattern && pattern2)
3334  psql_error("Did not find any settings for role \"%s\" and database \"%s\".\n",
3335  pattern, pattern2);
3336  else if (pattern)
3337  psql_error("Did not find any settings for role \"%s\".\n",
3338  pattern);
3339  else
3340  psql_error("Did not find any settings.\n");
3341  }
3342  else
3343  {
3344  myopt.nullPrint = NULL;
3345  myopt.title = _("List of settings");
3346  myopt.translate_header = true;
3347 
3348  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3349  }
3350 
3351  PQclear(res);
3352  return true;
3353 }
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:1025
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:1025
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:618
#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:5434
#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 3612 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().

3613 {
3615  PGresult *res;
3616  printQueryOpt myopt = pset.popt;
3617 
3618  initPQExpBuffer(&buf);
3619 
3620  printfPQExpBuffer(&buf,
3621  "SELECT n.nspname as \"%s\",\n"
3622  " t.typname as \"%s\",\n"
3623  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
3624  gettext_noop("Schema"),
3625  gettext_noop("Name"),
3626  gettext_noop("Type"));
3627 
3628  if (pset.sversion >= 90100)
3629  appendPQExpBuffer(&buf,
3630  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3631  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
3632  gettext_noop("Collation"));
3633  appendPQExpBuffer(&buf,
3634  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
3635  " t.typdefault as \"%s\",\n"
3636  " pg_catalog.array_to_string(ARRAY(\n"
3637  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3638  " ), ' ') as \"%s\"",
3639  gettext_noop("Nullable"),
3640  gettext_noop("Default"),
3641  gettext_noop("Check"));
3642 
3643  if (verbose)
3644  {
3645  if (pset.sversion >= 90200)
3646  {
3647  appendPQExpBufferStr(&buf, ",\n ");
3648  printACLColumn(&buf, "t.typacl");
3649  }
3650  appendPQExpBuffer(&buf,
3651  ",\n d.description as \"%s\"",
3652  gettext_noop("Description"));
3653  }
3654 
3655  appendPQExpBufferStr(&buf,
3656  "\nFROM pg_catalog.pg_type t\n"
3657  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3658 
3659  if (verbose)
3660  appendPQExpBufferStr(&buf,
3661  " LEFT JOIN pg_catalog.pg_description d "
3662  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3663  "AND d.objsubid = 0\n");
3664 
3665  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3666 
3667  if (!showSystem && !pattern)
3668  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3669  " AND n.nspname <> 'information_schema'\n");
3670 
3671  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3672  "n.nspname", "t.typname", NULL,
3673  "pg_catalog.pg_type_is_visible(t.oid)");
3674 
3675  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3676 
3677  res = PSQLexec(buf.data);
3678  termPQExpBuffer(&buf);
3679  if (!res)
3680  return false;
3681 
3682  myopt.nullPrint = NULL;
3683  myopt.title = _("List of domains");
3684  myopt.translate_header = true;
3685 
3686  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3687 
3688  PQclear(res);
3689  return true;
3690 }
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:1025
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:5434
#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 3774 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().

3775 {
3777  PGresult *res;
3778  printQueryOpt myopt = pset.popt;
3779  static const bool translate_columns[] =
3780  {false, false, false, true, false, false, false};
3781 
3782  initPQExpBuffer(&buf);
3783 
3784  printfPQExpBuffer(&buf,
3785  "SELECT evtname as \"%s\", "
3786  "evtevent as \"%s\", "
3787  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3788  " case evtenabled when 'O' then '%s'"
3789  " when 'R' then '%s'"
3790  " when 'A' then '%s'"
3791  " when 'D' then '%s' end as \"%s\",\n"
3792  " e.evtfoid::pg_catalog.regproc as \"%s\", "
3793  "pg_catalog.array_to_string(array(select x"
3794  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3795  gettext_noop("Name"),
3796  gettext_noop("Event"),
3797  gettext_noop("Owner"),
3798  gettext_noop("enabled"),
3799  gettext_noop("replica"),
3800  gettext_noop("always"),
3801  gettext_noop("disabled"),
3802  gettext_noop("Enabled"),
3803  gettext_noop("Procedure"),
3804  gettext_noop("Tags"));
3805  if (verbose)
3806  appendPQExpBuffer(&buf,
3807  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3808  gettext_noop("Description"));
3809  appendPQExpBufferStr(&buf,
3810  "\nFROM pg_catalog.pg_event_trigger e ");
3811 
3812  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3813  NULL, "evtname", NULL, NULL);
3814 
3815  appendPQExpBufferStr(&buf, "ORDER BY 1");
3816 
3817  res = PSQLexec(buf.data);
3818  termPQExpBuffer(&buf);
3819  if (!res)
3820  return false;
3821 
3822  myopt.nullPrint = NULL;
3823  myopt.title = _("List of event triggers");
3824  myopt.translate_header = true;
3825  myopt.translate_columns = translate_columns;
3826  myopt.n_translate_columns = lengthof(translate_columns);
3827 
3828  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3829 
3830  PQclear(res);
3831  return true;
3832 }
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:1025
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:618
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 5036 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().

5037 {
5039  PGresult *res;
5040  int i;
5041 
5042  if (pset.sversion < 90100)
5043  {
5044  char sverbuf[32];
5045 
5046  psql_error("The server (version %s) does not support extensions.\n",
5048  sverbuf, sizeof(sverbuf)));
5049  return true;
5050  }
5051 
5052  initPQExpBuffer(&buf);
5053  printfPQExpBuffer(&buf,
5054  "SELECT e.extname, e.oid\n"
5055  "FROM pg_catalog.pg_extension e\n");
5056 
5057  processSQLNamePattern(pset.db, &buf, pattern,
5058  false, false,
5059  NULL, "e.extname", NULL,
5060  NULL);
5061 
5062  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5063 
5064  res = PSQLexec(buf.data);