PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

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

Definition at line 3208 of file describe.c.

References appendPQExpBufferStr(), and PQExpBufferData::len.

Referenced by describeRoles().

3209 {
3210  if (buf->len > 0)
3211  appendPQExpBufferStr(buf, ", ");
3212 
3213  appendPQExpBufferStr(buf, str);
3214 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static void add_tablespace_footer ( printTableContent *const  cont,
char  relkind,
Oid  tablespace,
const bool  newline 
)
static

Definition at line 2985 of file describe.c.

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

Referenced by describeOneTableDetails().

2987 {
2988  /* relkinds for which we support tablespaces */
2989  if (relkind == RELKIND_RELATION ||
2990  relkind == RELKIND_MATVIEW ||
2991  relkind == RELKIND_INDEX ||
2992  relkind == RELKIND_PARTITIONED_TABLE)
2993  {
2994  /*
2995  * We ignore the database default tablespace so that users not using
2996  * tablespaces don't need to know about them. This case also covers
2997  * pre-8.0 servers, for which tablespace will always be 0.
2998  */
2999  if (tablespace != 0)
3000  {
3001  PGresult *result = NULL;
3003 
3004  initPQExpBuffer(&buf);
3005  printfPQExpBuffer(&buf,
3006  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3007  "WHERE oid = '%u';", tablespace);
3008  result = PSQLexec(buf.data);
3009  if (!result)
3010  {
3011  termPQExpBuffer(&buf);
3012  return;
3013  }
3014  /* Should always be the case, but.... */
3015  if (PQntuples(result) > 0)
3016  {
3017  if (newline)
3018  {
3019  /* Add the tablespace as a new footer */
3020  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3021  PQgetvalue(result, 0, 0));
3022  printTableAddFooter(cont, buf.data);
3023  }
3024  else
3025  {
3026  /* Append the tablespace to the latest footer */
3027  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3028 
3029  /*-------
3030  translator: before this string there's an index description like
3031  '"foo_pkey" PRIMARY KEY, btree (a)' */
3032  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3033  PQgetvalue(result, 0, 0));
3034  printTableSetFooter(cont, buf.data);
3035  }
3036  }
3037  PQclear(result);
3038  termPQExpBuffer(&buf);
3039  }
3040  }
3041 }
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3081
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:3067
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
#define RELKIND_MATVIEW
Definition: pg_class.h:165
return result
Definition: formatting.c:1633
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
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:66
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:3056
printTableFooter * footer
Definition: print.h:157
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
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:642
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, NULL, 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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
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:642
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, NULL, 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:139
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:66
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:642
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, NULL, 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 = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
357  " ELSE '%s'\n"
358  " END as \"%s\"",
359  gettext_noop("Result data type"),
360  gettext_noop("Argument data types"),
361  /* translator: "agg" is short for "aggregate" */
362  gettext_noop("agg"),
363  gettext_noop("window"),
364  gettext_noop("trigger"),
365  gettext_noop("normal"),
366  gettext_noop("Type"));
367  else if (pset.sversion >= 80100)
368  appendPQExpBuffer(&buf,
369  " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
370  " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
371  " CASE WHEN proallargtypes IS NOT NULL THEN\n"
372  " pg_catalog.array_to_string(ARRAY(\n"
373  " SELECT\n"
374  " CASE\n"
375  " WHEN p.proargmodes[s.i] = 'i' THEN ''\n"
376  " WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '\n"
377  " WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '\n"
378  " WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '\n"
379  " END ||\n"
380  " CASE\n"
381  " WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''\n"
382  " ELSE p.proargnames[s.i] || ' '\n"
383  " END ||\n"
384  " pg_catalog.format_type(p.proallargtypes[s.i], NULL)\n"
385  " FROM\n"
386  " pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)\n"
387  " ), ', ')\n"
388  " ELSE\n"
389  " pg_catalog.array_to_string(ARRAY(\n"
390  " SELECT\n"
391  " CASE\n"
392  " WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''\n"
393  " ELSE p.proargnames[s.i+1] || ' '\n"
394  " END ||\n"
395  " pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"
396  " FROM\n"
397  " pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
398  " ), ', ')\n"
399  " END AS \"%s\",\n"
400  " CASE\n"
401  " WHEN p.proisagg THEN '%s'\n"
402  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
403  " ELSE '%s'\n"
404  " END AS \"%s\"",
405  gettext_noop("Result data type"),
406  gettext_noop("Argument data types"),
407  /* translator: "agg" is short for "aggregate" */
408  gettext_noop("agg"),
409  gettext_noop("trigger"),
410  gettext_noop("normal"),
411  gettext_noop("Type"));
412  else
413  appendPQExpBuffer(&buf,
414  " CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
415  " pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
416  " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
417  " CASE\n"
418  " WHEN p.proisagg THEN '%s'\n"
419  " WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
420  " ELSE '%s'\n"
421  " END AS \"%s\"",
422  gettext_noop("Result data type"),
423  gettext_noop("Argument data types"),
424  /* translator: "agg" is short for "aggregate" */
425  gettext_noop("agg"),
426  gettext_noop("trigger"),
427  gettext_noop("normal"),
428  gettext_noop("Type"));
429 
430  if (verbose)
431  {
432  appendPQExpBuffer(&buf,
433  ",\n CASE\n"
434  " WHEN p.provolatile = 'i' THEN '%s'\n"
435  " WHEN p.provolatile = 's' THEN '%s'\n"
436  " WHEN p.provolatile = 'v' THEN '%s'\n"
437  " END as \"%s\"",
438  gettext_noop("immutable"),
439  gettext_noop("stable"),
440  gettext_noop("volatile"),
441  gettext_noop("Volatility"));
442  if (pset.sversion >= 90600)
443  appendPQExpBuffer(&buf,
444  ",\n CASE\n"
445  " WHEN p.proparallel = 'r' THEN '%s'\n"
446  " WHEN p.proparallel = 's' THEN '%s'\n"
447  " WHEN p.proparallel = 'u' THEN '%s'\n"
448  " END as \"%s\"",
449  gettext_noop("restricted"),
450  gettext_noop("safe"),
451  gettext_noop("unsafe"),
452  gettext_noop("Parallel"));
453  appendPQExpBuffer(&buf,
454  ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\""
455  ",\n CASE WHEN prosecdef THEN '%s' ELSE '%s' END AS \"%s\"",
456  gettext_noop("Owner"),
457  gettext_noop("definer"),
458  gettext_noop("invoker"),
459  gettext_noop("Security"));
460  appendPQExpBufferStr(&buf, ",\n ");
461  printACLColumn(&buf, "p.proacl");
462  appendPQExpBuffer(&buf,
463  ",\n l.lanname as \"%s\""
464  ",\n p.prosrc as \"%s\""
465  ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
466  gettext_noop("Language"),
467  gettext_noop("Source code"),
468  gettext_noop("Description"));
469  }
470 
472  "\nFROM pg_catalog.pg_proc p"
473  "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
474 
475  if (verbose)
477  " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
478 
479  have_where = false;
480 
481  /* filter by function type, if requested */
482  if (showNormal && showAggregate && showTrigger && showWindow)
483  /* Do nothing */ ;
484  else if (showNormal)
485  {
486  if (!showAggregate)
487  {
488  if (have_where)
489  appendPQExpBufferStr(&buf, " AND ");
490  else
491  {
492  appendPQExpBufferStr(&buf, "WHERE ");
493  have_where = true;
494  }
495  appendPQExpBufferStr(&buf, "NOT p.proisagg\n");
496  }
497  if (!showTrigger)
498  {
499  if (have_where)
500  appendPQExpBufferStr(&buf, " AND ");
501  else
502  {
503  appendPQExpBufferStr(&buf, "WHERE ");
504  have_where = true;
505  }
506  appendPQExpBufferStr(&buf, "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n");
507  }
508  if (!showWindow && pset.sversion >= 80400)
509  {
510  if (have_where)
511  appendPQExpBufferStr(&buf, " AND ");
512  else
513  {
514  appendPQExpBufferStr(&buf, "WHERE ");
515  have_where = true;
516  }
517  appendPQExpBufferStr(&buf, "NOT p.proiswindow\n");
518  }
519  }
520  else
521  {
522  bool needs_or = false;
523 
524  appendPQExpBufferStr(&buf, "WHERE (\n ");
525  have_where = true;
526  /* Note: at least one of these must be true ... */
527  if (showAggregate)
528  {
529  appendPQExpBufferStr(&buf, "p.proisagg\n");
530  needs_or = true;
531  }
532  if (showTrigger)
533  {
534  if (needs_or)
535  appendPQExpBufferStr(&buf, " OR ");
537  "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n");
538  needs_or = true;
539  }
540  if (showWindow)
541  {
542  if (needs_or)
543  appendPQExpBufferStr(&buf, " OR ");
544  appendPQExpBufferStr(&buf, "p.proiswindow\n");
545  needs_or = true;
546  }
547  appendPQExpBufferStr(&buf, " )\n");
548  }
549 
550  processSQLNamePattern(pset.db, &buf, pattern, have_where, false,
551  "n.nspname", "p.proname", NULL,
552  "pg_catalog.pg_function_is_visible(p.oid)");
553 
554  if (!showSystem && !pattern)
555  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
556  " AND n.nspname <> 'information_schema'\n");
557 
558  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
559 
560  res = PSQLexec(buf.data);
561  termPQExpBuffer(&buf);
562  if (!res)
563  return false;
564 
565  myopt.nullPrint = NULL;
566  myopt.title = _("List of functions");
567  myopt.translate_header = true;
568  if (pset.sversion >= 90600)
569  {
570  myopt.translate_columns = translate_columns;
571  myopt.n_translate_columns = lengthof(translate_columns);
572  }
573  else
574  {
575  myopt.translate_columns = translate_columns_pre_96;
576  myopt.n_translate_columns = lengthof(translate_columns_pre_96);
577  }
578 
579  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
580 
581  PQclear(res);
582  return true;
583 }
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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
const bool * translate_columns
Definition: print.h:170
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5364
#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:642
static bool describeOneTableDetails ( const char *  schemaname,
const char *  relationname,
const char *  oid,
bool  verbose 
)
static

Definition at line 1370 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(), free, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, NULL, pg_malloc(), pg_strdup(), pg_wcswidth(), _psqlSettings::popt, PQclear(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), printfPQExpBuffer(), printTable(), printTableAddCell(), printTableAddFooter(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, RELKIND_COMPOSITE_TYPE, RELKIND_FOREIGN_TABLE, RELKIND_INDEX, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_TOASTVALUE, RELKIND_VIEW, resetPQExpBuffer(), result, _psqlSettings::sversion, tablespace, termPQExpBuffer(), tmpbuf, and printQueryOpt::topt.

Referenced by describeTableDetails().

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

Definition at line 4533 of file describe.c.

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

Referenced by listTSConfigsVerbose().

4535 {
4537  title;
4538  PGresult *res;
4539  printQueryOpt myopt = pset.popt;
4540 
4541  initPQExpBuffer(&buf);
4542 
4543  printfPQExpBuffer(&buf,
4544  "SELECT\n"
4545  " ( SELECT t.alias FROM\n"
4546  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
4547  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
4548  " pg_catalog.btrim(\n"
4549  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
4550  " FROM pg_catalog.pg_ts_config_map AS mm\n"
4551  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
4552  " ORDER BY mapcfg, maptokentype, mapseqno\n"
4553  " ) :: pg_catalog.text,\n"
4554  " '{}') AS \"%s\"\n"
4555  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
4556  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
4557  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
4558  "ORDER BY 1;",
4559  gettext_noop("Token"),
4560  gettext_noop("Dictionaries"),
4561  oid);
4562 
4563  res = PSQLexec(buf.data);
4564  termPQExpBuffer(&buf);
4565  if (!res)
4566  return false;
4567 
4568  initPQExpBuffer(&title);
4569 
4570  if (nspname)
4571  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4572  nspname, cfgname);
4573  else
4574  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4575  cfgname);
4576 
4577  if (pnspname)
4578  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4579  pnspname, prsname);
4580  else
4581  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4582  prsname);
4583 
4584  myopt.nullPrint = NULL;
4585  myopt.title = title.data;
4586  myopt.footers = NULL;
4587  myopt.topt.default_footer = false;
4588  myopt.translate_header = true;
4589 
4590  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4591 
4592  termPQExpBuffer(&title);
4593 
4594  PQclear(res);
4595  return true;
4596 }
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:139
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:66
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
char ** footers
Definition: print.h:168
bool default_footer
Definition: print.h:110
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:642
static bool describeOneTSParser ( const char *  oid,
const char *  nspname,
const char *  prsname 
)
static

Definition at line 4138 of file describe.c.

References _, buf, PQExpBufferData::data, printTableOpt::default_footer, printQueryOpt::footers, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, NULL, 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().

4139 {
4141  PGresult *res;
4142  PQExpBufferData title;
4143  printQueryOpt myopt = pset.popt;
4144  static const bool translate_columns[] = {true, false, false};
4145 
4146  initPQExpBuffer(&buf);
4147 
4148  printfPQExpBuffer(&buf,
4149  "SELECT '%s' AS \"%s\",\n"
4150  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4151  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4152  " FROM pg_catalog.pg_ts_parser p\n"
4153  " WHERE p.oid = '%s'\n"
4154  "UNION ALL\n"
4155  "SELECT '%s',\n"
4156  " p.prstoken::pg_catalog.regproc,\n"
4157  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4158  " FROM pg_catalog.pg_ts_parser p\n"
4159  " WHERE p.oid = '%s'\n"
4160  "UNION ALL\n"
4161  "SELECT '%s',\n"
4162  " p.prsend::pg_catalog.regproc,\n"
4163  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4164  " FROM pg_catalog.pg_ts_parser p\n"
4165  " WHERE p.oid = '%s'\n"
4166  "UNION ALL\n"
4167  "SELECT '%s',\n"
4168  " p.prsheadline::pg_catalog.regproc,\n"
4169  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4170  " FROM pg_catalog.pg_ts_parser p\n"
4171  " WHERE p.oid = '%s'\n"
4172  "UNION ALL\n"
4173  "SELECT '%s',\n"
4174  " p.prslextype::pg_catalog.regproc,\n"
4175  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4176  " FROM pg_catalog.pg_ts_parser p\n"
4177  " WHERE p.oid = '%s';",
4178  gettext_noop("Start parse"),
4179  gettext_noop("Method"),
4180  gettext_noop("Function"),
4181  gettext_noop("Description"),
4182  oid,
4183  gettext_noop("Get next token"),
4184  oid,
4185  gettext_noop("End parse"),
4186  oid,
4187  gettext_noop("Get headline"),
4188  oid,
4189  gettext_noop("Get token types"),
4190  oid);
4191 
4192  res = PSQLexec(buf.data);
4193  termPQExpBuffer(&buf);
4194  if (!res)
4195  return false;
4196 
4197  myopt.nullPrint = NULL;
4198  initPQExpBuffer(&title);
4199  if (nspname)
4200  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4201  nspname, prsname);
4202  else
4203  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4204  myopt.title = title.data;
4205  myopt.footers = NULL;
4206  myopt.topt.default_footer = false;
4207  myopt.translate_header = true;
4208  myopt.translate_columns = translate_columns;
4209  myopt.n_translate_columns = lengthof(translate_columns);
4210 
4211  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4212 
4213  PQclear(res);
4214 
4215  initPQExpBuffer(&buf);
4216 
4217  printfPQExpBuffer(&buf,
4218  "SELECT t.alias as \"%s\",\n"
4219  " t.description as \"%s\"\n"
4220  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4221  "ORDER BY 1;",
4222  gettext_noop("Token name"),
4223  gettext_noop("Description"),
4224  oid);
4225 
4226  res = PSQLexec(buf.data);
4227  termPQExpBuffer(&buf);
4228  if (!res)
4229  return false;
4230 
4231  myopt.nullPrint = NULL;
4232  if (nspname)
4233  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4234  nspname, prsname);
4235  else
4236  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4237  myopt.title = title.data;
4238  myopt.footers = NULL;
4239  myopt.topt.default_footer = true;
4240  myopt.translate_header = true;
4241  myopt.translate_columns = NULL;
4242  myopt.n_translate_columns = 0;
4243 
4244  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4245 
4246  termPQExpBuffer(&title);
4247  PQclear(res);
4248  return true;
4249 }
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:139
FILE * queryFout
Definition: settings.h:84
#define lengthof(array)
Definition: c.h:562
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
char ** footers
Definition: print.h:168
bool default_footer
Definition: print.h:110
#define NULL
Definition: c.h:229
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:3285
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:642
bool describeOperators ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 708 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 5146 of file describe.c.

References _, appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, NULL, _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().

5147 {
5149  int i;
5150  PGresult *res;
5151 
5152  if (pset.sversion < 100000)
5153  {
5154  char sverbuf[32];
5155 
5156  psql_error("The server (version %s) does not support publications.\n",
5158  sverbuf, sizeof(sverbuf)));
5159  return true;
5160  }
5161 
5162  initPQExpBuffer(&buf);
5163 
5164  printfPQExpBuffer(&buf,
5165  "SELECT oid, pubname,\n"
5166  " pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
5167  " puballtables, pubinsert, pubupdate, pubdelete\n"
5168  "FROM pg_catalog.pg_publication\n");
5169 
5170  processSQLNamePattern(pset.db, &buf, pattern, false, false,
5171  NULL, "pubname", NULL,
5172  NULL);
5173 
5174  appendPQExpBufferStr(&buf, "ORDER BY 2;");
5175 
5176  res = PSQLexec(buf.data);
5177  if (!res)
5178  {
5179  termPQExpBuffer(&buf);
5180  return false;
5181  }
5182 
5183  if (PQntuples(res) == 0)
5184  {
5185  if (!pset.quiet)
5186  {
5187  if (pattern)
5188  psql_error("Did not find any publication named \"%s\".\n",
5189  pattern);
5190  else
5191  psql_error("Did not find any publications.\n");
5192  }
5193 
5194  termPQExpBuffer(&buf);
5195  PQclear(res);
5196  return false;
5197  }
5198 
5199  for (i = 0; i < PQntuples(res); i++)
5200  {
5201  const char align = 'l';
5202  int ncols = 5;
5203  int nrows = 1;
5204  int tables = 0;
5205  PGresult *tabres;
5206  char *pubid = PQgetvalue(res, i, 0);
5207  char *pubname = PQgetvalue(res, i, 1);
5208  bool puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
5209  int j;
5210  PQExpBufferData title;
5211  printTableOpt myopt = pset.popt.topt;
5212  printTableContent cont;
5213 
5214  initPQExpBuffer(&title);
5215  printfPQExpBuffer(&title, _("Publication %s"), pubname);
5216  printTableInit(&cont, &myopt, title.data, ncols, nrows);
5217 
5218  printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
5219  printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
5220  printTableAddHeader(&cont, gettext_noop("Inserts"), true, align);
5221  printTableAddHeader(&cont, gettext_noop("Updates"), true, align);
5222  printTableAddHeader(&cont, gettext_noop("Deletes"), true, align);
5223 
5224  printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
5225  printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
5226  printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
5227  printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
5228  printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
5229 
5230  if (!puballtables)
5231  {
5232  printfPQExpBuffer(&buf,
5233  "SELECT n.nspname, c.relname\n"
5234  "FROM pg_catalog.pg_class c,\n"
5235  " pg_catalog.pg_namespace n,\n"
5236  " pg_catalog.pg_publication_rel pr\n"
5237  "WHERE c.relnamespace = n.oid\n"
5238  " AND c.oid = pr.prrelid\n"
5239  " AND pr.prpubid = '%s'\n"
5240  "ORDER BY 1,2", pubid);
5241 
5242  tabres = PSQLexec(buf.data);
5243  if (!tabres)
5244  {
5245  printTableCleanup(&cont);
5246  PQclear(res);
5247  termPQExpBuffer(&buf);
5248  termPQExpBuffer(&title);
5249  return false;
5250  }
5251  else
5252  tables = PQntuples(tabres);
5253 
5254  if (tables > 0)
5255  printTableAddFooter(&cont, _("Tables:"));
5256 
5257  for (j = 0; j < tables; j++)
5258  {
5259  printfPQExpBuffer(&buf, " \"%s.%s\"",
5260  PQgetvalue(tabres, j, 0),
5261  PQgetvalue(tabres, j, 1));
5262 
5263  printTableAddFooter(&cont, buf.data);
5264  }
5265  PQclear(tabres);
5266  }
5267 
5268  printTable(&cont, pset.queryFout, false, pset.logfile);
5269  printTableCleanup(&cont);
5270 
5271  termPQExpBuffer(&title);
5272  }
5273 
5274  termPQExpBuffer(&buf);
5275  PQclear(res);
5276 
5277  return true;
5278 }
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:3099
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:139
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:2673
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:2968
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3008
static char * buf
Definition: pg_test_fsync.c:66
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3188
void psql_error(const char *fmt,...)
Definition: common.c:220
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3056
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
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:2931
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:642
bool describeRoles ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3049 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, NULL, 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().

3050 {
3052  PGresult *res;
3053  printTableContent cont;
3054  printTableOpt myopt = pset.popt.topt;
3055  int ncols = 3;
3056  int nrows = 0;
3057  int i;
3058  int conns;
3059  const char align = 'l';
3060  char **attr;
3061 
3062  myopt.default_footer = false;
3063 
3064  initPQExpBuffer(&buf);
3065 
3066  if (pset.sversion >= 80100)
3067  {
3068  printfPQExpBuffer(&buf,
3069  "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3070  " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3071  " r.rolconnlimit, r.rolvaliduntil,\n"
3072  " ARRAY(SELECT b.rolname\n"
3073  " FROM pg_catalog.pg_auth_members m\n"
3074  " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n"
3075  " WHERE m.member = r.oid) as memberof");
3076 
3077  if (verbose && pset.sversion >= 80200)
3078  {
3079  appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description");
3080  ncols++;
3081  }
3082  if (pset.sversion >= 90100)
3083  {
3084  appendPQExpBufferStr(&buf, "\n, r.rolreplication");
3085  }
3086 
3087  if (pset.sversion >= 90500)
3088  {
3089  appendPQExpBufferStr(&buf, "\n, r.rolbypassrls");
3090  }
3091 
3092  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_roles r\n");
3093 
3094  if (!showSystem && !pattern)
3095  appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n");
3096 
3097  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3098  NULL, "r.rolname", NULL, NULL);
3099  }
3100  else
3101  {
3102  printfPQExpBuffer(&buf,
3103  "SELECT u.usename AS rolname,\n"
3104  " u.usesuper AS rolsuper,\n"
3105  " true AS rolinherit, false AS rolcreaterole,\n"
3106  " u.usecreatedb AS rolcreatedb, true AS rolcanlogin,\n"
3107  " -1 AS rolconnlimit,"
3108  " u.valuntil as rolvaliduntil,\n"
3109  " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as memberof"
3110  "\nFROM pg_catalog.pg_user u\n");
3111 
3112  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3113  NULL, "u.usename", NULL, NULL);
3114  }
3115 
3116  appendPQExpBufferStr(&buf, "ORDER BY 1;");
3117 
3118  res = PSQLexec(buf.data);
3119  if (!res)
3120  return false;
3121 
3122  nrows = PQntuples(res);
3123  attr = pg_malloc0((nrows + 1) * sizeof(*attr));
3124 
3125  printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
3126 
3127  printTableAddHeader(&cont, gettext_noop("Role name"), true, align);
3128  printTableAddHeader(&cont, gettext_noop("Attributes"), true, align);
3129  printTableAddHeader(&cont, gettext_noop("Member of"), true, align);
3130 
3131  if (verbose && pset.sversion >= 80200)
3132  printTableAddHeader(&cont, gettext_noop("Description"), true, align);
3133 
3134  for (i = 0; i < nrows; i++)
3135  {
3136  printTableAddCell(&cont, PQgetvalue(res, i, 0), false, false);
3137 
3138  resetPQExpBuffer(&buf);
3139  if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
3140  add_role_attribute(&buf, _("Superuser"));
3141 
3142  if (strcmp(PQgetvalue(res, i, 2), "t") != 0)
3143  add_role_attribute(&buf, _("No inheritance"));
3144 
3145  if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
3146  add_role_attribute(&buf, _("Create role"));
3147 
3148  if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
3149  add_role_attribute(&buf, _("Create DB"));
3150 
3151  if (strcmp(PQgetvalue(res, i, 5), "t") != 0)
3152  add_role_attribute(&buf, _("Cannot login"));
3153 
3154  if (pset.sversion >= 90100)
3155  if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0)
3156  add_role_attribute(&buf, _("Replication"));
3157 
3158  if (pset.sversion >= 90500)
3159  if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0)
3160  add_role_attribute(&buf, _("Bypass RLS"));
3161 
3162  conns = atoi(PQgetvalue(res, i, 6));
3163  if (conns >= 0)
3164  {
3165  if (buf.len > 0)
3166  appendPQExpBufferChar(&buf, '\n');
3167 
3168  if (conns == 0)
3169  appendPQExpBufferStr(&buf, _("No connections"));
3170  else
3171  appendPQExpBuffer(&buf, ngettext("%d connection",
3172  "%d connections",
3173  conns),
3174  conns);
3175  }
3176 
3177  if (strcmp(PQgetvalue(res, i, 7), "") != 0)
3178  {
3179  if (buf.len > 0)
3180  appendPQExpBufferChar(&buf, '\n');
3181  appendPQExpBufferStr(&buf, _("Password valid until "));
3182  appendPQExpBufferStr(&buf, PQgetvalue(res, i, 7));
3183  }
3184 
3185  attr[i] = pg_strdup(buf.data);
3186 
3187  printTableAddCell(&cont, attr[i], false, false);
3188 
3189  printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
3190 
3191  if (verbose && pset.sversion >= 80200)
3192  printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
3193  }
3194  termPQExpBuffer(&buf);
3195 
3196  printTable(&cont, pset.queryFout, false, pset.logfile);
3197  printTableCleanup(&cont);
3198 
3199  for (i = 0; i < nrows; i++)
3200  free(attr[i]);
3201  free(attr);
3202 
3203  PQclear(res);
3204  return true;
3205 }
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:3099
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:139
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:2673
static void add_role_attribute(PQExpBuffer buf, const char *const str)
Definition: describe.c:3208
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:2968
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:3008
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
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:3188
static int verbose
Definition: pg_basebackup.c:84
#define ngettext(s, p, n)
Definition: c.h:127
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:65
bool default_footer
Definition: print.h:110
#define NULL
Definition: c.h:229
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:2931
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:642
bool describeSubscriptions ( const char *  pattern,
bool  verbose 
)

Definition at line 5287 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, NULL, 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().

5288 {
5290  PGresult *res;
5291  printQueryOpt myopt = pset.popt;
5292  static const bool translate_columns[] = {false, false, false, false,
5293  false, false};
5294 
5295  if (pset.sversion < 100000)
5296  {
5297  char sverbuf[32];
5298 
5299  psql_error("The server (version %s) does not support subscriptions.\n",
5301  sverbuf, sizeof(sverbuf)));
5302  return true;
5303  }
5304 
5305  initPQExpBuffer(&buf);
5306 
5307  printfPQExpBuffer(&buf,
5308  "SELECT subname AS \"%s\"\n"
5309  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
5310  ", subenabled AS \"%s\"\n"
5311  ", subpublications AS \"%s\"\n",
5312  gettext_noop("Name"),
5313  gettext_noop("Owner"),
5314  gettext_noop("Enabled"),
5315  gettext_noop("Publication"));
5316 
5317  if (verbose)
5318  {
5319  appendPQExpBuffer(&buf,
5320  ", subsynccommit AS \"%s\"\n"
5321  ", subconninfo AS \"%s\"\n",
5322  gettext_noop("Synchronous commit"),
5323  gettext_noop("Conninfo"));
5324  }
5325 
5326  /* Only display subscriptions in current database. */
5327  appendPQExpBufferStr(&buf,
5328  "FROM pg_catalog.pg_subscription\n"
5329  "WHERE subdbid = (SELECT oid\n"
5330  " FROM pg_catalog.pg_database\n"
5331  " WHERE datname = current_database())");
5332 
5333  processSQLNamePattern(pset.db, &buf, pattern, true, false,
5334  NULL, "subname", NULL,
5335  NULL);
5336 
5337  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5338 
5339  res = PSQLexec(buf.data);
5340  termPQExpBuffer(&buf);
5341  if (!res)
5342  return false;
5343 
5344  myopt.nullPrint = NULL;
5345  myopt.title = _("List of subscriptions");
5346  myopt.translate_header = true;
5347  myopt.translate_columns = translate_columns;
5348  myopt.n_translate_columns = lengthof(translate_columns);
5349 
5350  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5351 
5352  PQclear(res);
5353  return true;
5354 }
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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
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:642
bool describeTableDetails ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 1292 of file describe.c.

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

Referenced by exec_command_d().

1293 {
1295  PGresult *res;
1296  int i;
1297 
1298  initPQExpBuffer(&buf);
1299 
1300  printfPQExpBuffer(&buf,
1301  "SELECT c.oid,\n"
1302  " n.nspname,\n"
1303  " c.relname\n"
1304  "FROM pg_catalog.pg_class c\n"
1305  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1306 
1307  if (!showSystem && !pattern)
1308  appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n"
1309  " AND n.nspname <> 'information_schema'\n");
1310 
1311  processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false,
1312  "n.nspname", "c.relname", NULL,
1313  "pg_catalog.pg_table_is_visible(c.oid)");
1314 
1315  appendPQExpBufferStr(&buf, "ORDER BY 2, 3;");
1316 
1317  res = PSQLexec(buf.data);
1318  termPQExpBuffer(&buf);
1319  if (!res)
1320  return false;
1321 
1322  if (PQntuples(res) == 0)
1323  {
1324  if (!pset.quiet)
1325  {
1326  if (pattern)
1327  psql_error("Did not find any relation named \"%s\".\n",
1328  pattern);
1329  else
1330  psql_error("Did not find any relations.\n");
1331  }
1332  PQclear(res);
1333  return false;
1334  }
1335 
1336  for (i = 0; i < PQntuples(res); i++)
1337  {
1338  const char *oid;
1339  const char *nspname;
1340  const char *relname;
1341 
1342  oid = PQgetvalue(res, i, 0);
1343  nspname = PQgetvalue(res, i, 1);
1344  relname = PQgetvalue(res, i, 2);
1345 
1346  if (!describeOneTableDetails(nspname, relname, oid, verbose))
1347  {
1348  PQclear(res);
1349  return false;
1350  }
1351  if (cancel_pressed)
1352  {
1353  PQclear(res);
1354  return false;
1355  }
1356  }
1357 
1358  PQclear(res);
1359  return true;
1360 }
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:3067
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:2673
volatile bool cancel_pressed
Definition: print.c:46
static char * buf
Definition: pg_test_fsync.c:66
static bool describeOneTableDetails(const char *schemaname, const char *relationname, const char *oid, bool verbose)
Definition: describe.c:1370
static int verbose
Definition: pg_basebackup.c:84
void psql_error(const char *fmt,...)
Definition: common.c:220
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
int i
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:642
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, NULL, 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:139
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:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5364
#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:642
bool describeTypes ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 592 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, CppAsString2, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, NULL, 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().

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

Definition at line 785 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, NULL, 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().

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

Definition at line 3770 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, NULL, 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().

3771 {
3773  PGresult *res;
3774  printQueryOpt myopt = pset.popt;
3775  static const bool translate_columns[] = {false, false, false, true, false};
3776 
3777  initPQExpBuffer(&buf);
3778 
3779  /*
3780  * We need a left join to pg_proc for binary casts; the others are just
3781  * paranoia. Also note that we don't attempt to localize '(binary
3782  * coercible)', because there's too much risk of gettext translating a
3783  * function name that happens to match some string in the PO database.
3784  */
3785  printfPQExpBuffer(&buf,
3786  "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
3787  " pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
3788  " CASE WHEN castfunc = 0 THEN '(binary coercible)'\n"
3789  " ELSE p.proname\n"
3790  " END as \"%s\",\n"
3791  " CASE WHEN c.castcontext = 'e' THEN '%s'\n"
3792  " WHEN c.castcontext = 'a' THEN '%s'\n"
3793  " ELSE '%s'\n"
3794  " END as \"%s\"",
3795  gettext_noop("Source type"),
3796  gettext_noop("Target type"),
3797  gettext_noop("Function"),
3798  gettext_noop("no"),
3799  gettext_noop("in assignment"),
3800  gettext_noop("yes"),
3801  gettext_noop("Implicit?"));
3802 
3803  if (verbose)
3804  appendPQExpBuffer(&buf,
3805  ",\n d.description AS \"%s\"\n",
3806  gettext_noop("Description"));
3807 
3808  appendPQExpBufferStr(&buf,
3809  "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
3810  " ON c.castfunc = p.oid\n"
3811  " LEFT JOIN pg_catalog.pg_type ts\n"
3812  " ON c.castsource = ts.oid\n"
3813  " LEFT JOIN pg_catalog.pg_namespace ns\n"
3814  " ON ns.oid = ts.typnamespace\n"
3815  " LEFT JOIN pg_catalog.pg_type tt\n"
3816  " ON c.casttarget = tt.oid\n"
3817  " LEFT JOIN pg_catalog.pg_namespace nt\n"
3818  " ON nt.oid = tt.typnamespace\n");
3819 
3820  if (verbose)
3821  appendPQExpBufferStr(&buf,
3822  " LEFT JOIN pg_catalog.pg_description d\n"
3823  " ON d.classoid = c.tableoid AND d.objoid = "
3824  "c.oid AND d.objsubid = 0\n");
3825 
3826  appendPQExpBufferStr(&buf, "WHERE ( (true");
3827 
3828  /*
3829  * Match name pattern against either internal or external name of either
3830  * castsource or casttarget
3831  */
3832  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3833  "ns.nspname", "ts.typname",
3834  "pg_catalog.format_type(ts.oid, NULL)",
3835  "pg_catalog.pg_type_is_visible(ts.oid)");
3836 
3837  appendPQExpBufferStr(&buf, ") OR (true");
3838 
3839  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3840  "nt.nspname", "tt.typname",
3841  "pg_catalog.format_type(tt.oid, NULL)",
3842  "pg_catalog.pg_type_is_visible(tt.oid)");
3843 
3844  appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;");
3845 
3846  res = PSQLexec(buf.data);
3847  termPQExpBuffer(&buf);
3848  if (!res)
3849  return false;
3850 
3851  myopt.nullPrint = NULL;
3852  myopt.title = _("List of casts");
3853  myopt.translate_header = true;
3854  myopt.translate_columns = translate_columns;
3855  myopt.n_translate_columns = lengthof(translate_columns);
3856 
3857  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3858 
3859  PQclear(res);
3860  return true;
3861 }
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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
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:642
bool listCollations ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3869 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, NULL, 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().

3870 {
3872  PGresult *res;
3873  printQueryOpt myopt = pset.popt;
3874  static const bool translate_columns[] = {false, false, false, false, false, false};
3875 
3876  if (pset.sversion < 90100)
3877  {
3878  char sverbuf[32];
3879 
3880  psql_error("The server (version %s) does not support collations.\n",
3882  sverbuf, sizeof(sverbuf)));
3883  return true;
3884  }
3885 
3886  initPQExpBuffer(&buf);
3887 
3888  printfPQExpBuffer(&buf,
3889  "SELECT n.nspname AS \"%s\",\n"
3890  " c.collname AS \"%s\",\n"
3891  " c.collcollate AS \"%s\",\n"
3892  " c.collctype AS \"%s\"",
3893  gettext_noop("Schema"),
3894  gettext_noop("Name"),
3895  gettext_noop("Collate"),
3896  gettext_noop("Ctype"));
3897 
3898  if (pset.sversion >= 100000)
3899  appendPQExpBuffer(&buf,
3900  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
3901  gettext_noop("Provider"));
3902 
3903  if (verbose)
3904  appendPQExpBuffer(&buf,
3905  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3906  gettext_noop("Description"));
3907 
3908  appendPQExpBufferStr(&buf,
3909  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3910  "WHERE n.oid = c.collnamespace\n");
3911 
3912  if (!showSystem && !pattern)
3913  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3914  " AND n.nspname <> 'information_schema'\n");
3915 
3916  /*
3917  * Hide collations that aren't usable in the current database's encoding.
3918  * If you think to change this, note that pg_collation_is_visible rejects
3919  * unusable collations, so you will need to hack name pattern processing
3920  * somehow to avoid inconsistent behavior.
3921  */
3922  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3923 
3924  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3925  "n.nspname", "c.collname", NULL,
3926  "pg_catalog.pg_collation_is_visible(c.oid)");
3927 
3928  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3929 
3930  res = PSQLexec(buf.data);
3931  termPQExpBuffer(&buf);
3932  if (!res)
3933  return false;
3934 
3935  myopt.nullPrint = NULL;
3936  myopt.title = _("List of collations");
3937  myopt.translate_header = true;
3938  myopt.translate_columns = translate_columns;
3939  myopt.n_translate_columns = lengthof(translate_columns);
3940 
3941  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3942 
3943  PQclear(res);
3944  return true;
3945 }
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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
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:642
bool listConversions ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3628 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, NULL, 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().

3629 {
3631  PGresult *res;
3632  printQueryOpt myopt = pset.popt;
3633  static const bool translate_columns[] =
3634  {false, false, false, false, true, false};
3635 
3636  initPQExpBuffer(&buf);
3637 
3638  printfPQExpBuffer(&buf,
3639  "SELECT n.nspname AS \"%s\",\n"
3640  " c.conname AS \"%s\",\n"
3641  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3642  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3643  " CASE WHEN c.condefault THEN '%s'\n"
3644  " ELSE '%s' END AS \"%s\"",
3645  gettext_noop("Schema"),
3646  gettext_noop("Name"),
3647  gettext_noop("Source"),
3648  gettext_noop("Destination"),
3649  gettext_noop("yes"), gettext_noop("no"),
3650  gettext_noop("Default?"));
3651 
3652  if (verbose)
3653  appendPQExpBuffer(&buf,
3654  ",\n d.description AS \"%s\"",
3655  gettext_noop("Description"));
3656 
3657  appendPQExpBufferStr(&buf,
3658  "\nFROM pg_catalog.pg_conversion c\n"
3659  " JOIN pg_catalog.pg_namespace n "
3660  "ON n.oid = c.connamespace\n");
3661 
3662  if (verbose)
3663  appendPQExpBufferStr(&buf,
3664  "LEFT JOIN pg_catalog.pg_description d "
3665  "ON d.classoid = c.tableoid\n"
3666  " AND d.objoid = c.oid "
3667  "AND d.objsubid = 0\n");
3668 
3669  appendPQExpBufferStr(&buf, "WHERE true\n");
3670 
3671  if (!showSystem && !pattern)
3672  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3673  " AND n.nspname <> 'information_schema'\n");
3674 
3675  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3676  "n.nspname", "c.conname", NULL,
3677  "pg_catalog.pg_conversion_is_visible(c.oid)");
3678 
3679  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3680 
3681  res = PSQLexec(buf.data);
3682  termPQExpBuffer(&buf);
3683  if (!res)
3684  return false;
3685 
3686  myopt.nullPrint = NULL;
3687  myopt.title = _("List of conversions");
3688  myopt.translate_header = true;
3689  myopt.translate_columns = translate_columns;
3690  myopt.n_translate_columns = lengthof(translate_columns);
3691 
3692  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3693 
3694  PQclear(res);
3695  return true;
3696 }
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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
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:642
bool listDbRoleSettings ( const char *  pattern,
const char *  pattern2 
)

Definition at line 3220 of file describe.c.

References _, appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, formatPGVersionNumber(), gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, NULL, 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().

3221 {
3223  PGresult *res;
3224  printQueryOpt myopt = pset.popt;
3225  bool havewhere;
3226 
3227  if (pset.sversion < 90000)
3228  {
3229  char sverbuf[32];
3230 
3231  psql_error("The server (version %s) does not support per-database role settings.\n",
3233  sverbuf, sizeof(sverbuf)));
3234  return true;
3235  }
3236 
3237  initPQExpBuffer(&buf);
3238 
3239  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3240  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3241  "FROM pg_catalog.pg_db_role_setting s\n"
3242  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3243  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3244  gettext_noop("Role"),
3245  gettext_noop("Database"),
3246  gettext_noop("Settings"));
3247  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3248  NULL, "r.rolname", NULL, NULL);
3249  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3250  NULL, "d.datname", NULL, NULL);
3251  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3252 
3253  res = PSQLexec(buf.data);
3254  termPQExpBuffer(&buf);
3255  if (!res)
3256  return false;
3257 
3258  /*
3259  * Most functions in this file are content to print an empty table when
3260  * there are no matching objects. We intentionally deviate from that
3261  * here, but only in !quiet mode, because of the possibility that the user
3262  * is confused about what the two pattern arguments mean.
3263  */
3264  if (PQntuples(res) == 0 && !pset.quiet)
3265  {
3266  if (pattern && pattern2)
3267  psql_error("Did not find any settings for role \"%s\" and database \"%s\".\n",
3268  pattern, pattern2);
3269  else if (pattern)
3270  psql_error("Did not find any settings for role \"%s\".\n",
3271  pattern);
3272  else
3273  psql_error("Did not find any settings.\n");
3274  }
3275  else
3276  {
3277  myopt.nullPrint = NULL;
3278  myopt.title = _("List of settings");
3279  myopt.translate_header = true;
3280 
3281  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3282  }
3283 
3284  PQclear(res);
3285  return true;
3286 }
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:139
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:2673
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
#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:642
bool listDefaultACLs ( const char *  pattern)

Definition at line 1015 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, NULL, 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().

1016 {
1018  PGresult *res;
1019  printQueryOpt myopt = pset.popt;
1020  static const bool translate_columns[] = {false, false, true, false};
1021 
1022  if (pset.sversion < 90000)
1023  {
1024  char sverbuf[32];
1025 
1026  psql_error("The server (version %s) does not support altering default privileges.\n",
1028  sverbuf, sizeof(sverbuf)));
1029  return true;
1030  }
1031 
1032  initPQExpBuffer(&buf);
1033 
1034  printfPQExpBuffer(&buf,
1035  "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"
1036  " n.nspname AS \"%s\",\n"
1037  " 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"
1038  " ",
1039  gettext_noop("Owner"),
1040  gettext_noop("Schema"),
1042  gettext_noop("table"),
1044  gettext_noop("sequence"),
1046  gettext_noop("function"),
1048  gettext_noop("type"),
1050  gettext_noop("schema"),
1051  gettext_noop("Type"));
1052 
1053  printACLColumn(&buf, "d.defaclacl");
1054 
1055  appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_default_acl d\n"
1056  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n");
1057 
1058  processSQLNamePattern(pset.db, &buf, pattern, false, false,
1059  NULL,
1060  "n.nspname",
1061  "pg_catalog.pg_get_userbyid(d.defaclrole)",
1062  NULL);
1063 
1064  appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3;");
1065 
1066  res = PSQLexec(buf.data);
1067  if (!res)
1068  {
1069  termPQExpBuffer(&buf);
1070  return false;
1071  }
1072 
1073  myopt.nullPrint = NULL;
1074  printfPQExpBuffer(&buf, _("Default access privileges"));
1075  myopt.title = buf.data;
1076  myopt.translate_header = true;
1077  myopt.translate_columns = translate_columns;
1078  myopt.n_translate_columns = lengthof(translate_columns);
1079 
1080  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
1081 
1082  termPQExpBuffer(&buf);
1083  PQclear(res);
1084  return true;
1085 }
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:139
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:562
#define DEFACLOBJ_SEQUENCE
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
#define DEFACLOBJ_TYPE
void psql_error(const char *fmt,...)
Definition: common.c:220
FILE * logfile
Definition: settings.h:114
#define DEFACLOBJ_NAMESPACE
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define DEFACLOBJ_RELATION
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
#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:3285
const bool * translate_columns
Definition: print.h:170
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5364
#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:642
bool listDomains ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3542 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), _psqlSettings::logfile, NULL, 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().

3543 {
3545  PGresult *res;
3546  printQueryOpt myopt = pset.popt;
3547 
3548  initPQExpBuffer(&buf);
3549 
3550  printfPQExpBuffer(&buf,
3551  "SELECT n.nspname as \"%s\",\n"
3552  " t.typname as \"%s\",\n"
3553  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
3554  gettext_noop("Schema"),
3555  gettext_noop("Name"),
3556  gettext_noop("Type"));
3557 
3558  if (pset.sversion >= 90100)
3559  appendPQExpBuffer(&buf,
3560  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3561  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
3562  gettext_noop("Collation"));
3563  appendPQExpBuffer(&buf,
3564  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
3565  " t.typdefault as \"%s\",\n"
3566  " pg_catalog.array_to_string(ARRAY(\n"
3567  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3568  " ), ' ') as \"%s\"",
3569  gettext_noop("Nullable"),
3570  gettext_noop("Default"),
3571  gettext_noop("Check"));
3572 
3573  if (verbose)
3574  {
3575  if (pset.sversion >= 90200)
3576  {
3577  appendPQExpBufferStr(&buf, ",\n ");
3578  printACLColumn(&buf, "t.typacl");
3579  }
3580  appendPQExpBuffer(&buf,
3581  ",\n d.description as \"%s\"",
3582  gettext_noop("Description"));
3583  }
3584 
3585  appendPQExpBufferStr(&buf,
3586  "\nFROM pg_catalog.pg_type t\n"
3587  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3588 
3589  if (verbose)
3590  appendPQExpBufferStr(&buf,
3591  " LEFT JOIN pg_catalog.pg_description d "
3592  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3593  "AND d.objsubid = 0\n");
3594 
3595  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3596 
3597  if (!showSystem && !pattern)
3598  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3599  " AND n.nspname <> 'information_schema'\n");
3600 
3601  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3602  "n.nspname", "t.typname", NULL,
3603  "pg_catalog.pg_type_is_visible(t.oid)");
3604 
3605  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3606 
3607  res = PSQLexec(buf.data);
3608  termPQExpBuffer(&buf);
3609  if (!res)
3610  return false;
3611 
3612  myopt.nullPrint = NULL;
3613  myopt.title = _("List of domains");
3614  myopt.translate_header = true;
3615 
3616  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3617 
3618  PQclear(res);
3619  return true;
3620 }
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:139
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:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5364
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:642
bool listEventTriggers ( const char *  pattern,
bool  verbose 
)

Definition at line 3704 of file describe.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), buf, PQExpBufferData::data, _psqlSettings::db, gettext_noop, initPQExpBuffer(), lengthof, _psqlSettings::logfile, printQueryOpt::n_translate_columns, NULL, 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().

3705 {
3707  PGresult *res;
3708  printQueryOpt myopt = pset.popt;
3709  static const bool translate_columns[] =
3710  {false, false, false, true, false, false, false};
3711 
3712  initPQExpBuffer(&buf);
3713 
3714  printfPQExpBuffer(&buf,
3715  "SELECT evtname as \"%s\", "
3716  "evtevent as \"%s\", "
3717  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3718  " case evtenabled when 'O' then '%s'"
3719  " when 'R' then '%s'"
3720  " when 'A' then '%s'"
3721  " when 'D' then '%s' end as \"%s\",\n"
3722  " e.evtfoid::pg_catalog.regproc as \"%s\", "
3723  "pg_catalog.array_to_string(array(select x"
3724  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3725  gettext_noop("Name"),
3726  gettext_noop("Event"),
3727  gettext_noop("Owner"),
3728  gettext_noop("enabled"),
3729  gettext_noop("replica"),
3730  gettext_noop("always"),
3731  gettext_noop("disabled"),
3732  gettext_noop("Enabled"),
3733  gettext_noop("Procedure"),
3734  gettext_noop("Tags"));
3735  if (verbose)
3736  appendPQExpBuffer(&buf,
3737  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3738  gettext_noop("Description"));
3739  appendPQExpBufferStr(&buf,
3740  "\nFROM pg_catalog.pg_event_trigger e ");
3741 
3742  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3743  NULL, "evtname", NULL, NULL);
3744 
3745  appendPQExpBufferStr(&buf, "ORDER BY 1");
3746 
3747  res = PSQLexec(buf.data);
3748  termPQExpBuffer(&buf);
3749  if (!res)
3750  return false;
3751 
3752  myopt.nullPrint = NULL;
3753  myopt.title = _("List of event triggers");
3754  myopt.translate_header = true;
3755  myopt.translate_columns = translate_columns;
3756  myopt.n_translate_columns = lengthof(translate_columns);
3757 
3758  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3759 
3760  PQclear(res);
3761  return true;
3762 }
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:139
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:562
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:84
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
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:3285
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:642
bool listExtensionContents ( const char *  pattern)

Definition at line 4966 of file describe.c.

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

Referenced by exec_command_d().

4967 {
4969  PGresult *res;
4970  int i;
4971 
4972  if (pset.sversion < 90100)
4973  {
4974  char sverbuf[32];
4975 
4976  psql_error("The server (version %s) does not support extensions.\n",
4978  sverbuf, sizeof(sverbuf)));
4979  return true;
4980  }
4981 
4982  initPQExpBuffer(&buf);
4983  printfPQExpBuffer(&buf,
4984  "SELECT e.extname, e.oid\n"
4985  "FROM pg_catalog.pg_extension e\n");
4986 
4987  processSQLNamePattern(pset.db, &buf, pattern,
4988  false, false,
4989  NULL, "e.extname", NULL,
4990  NULL);
4991 
4992  appendPQExpBufferStr(&buf, "ORDER BY 1;");
4993 
4994  res = PSQLexec(buf.data);
4995  termPQExpBuffer(&buf);
4996  if (!res)
4997  return false;
4998 
4999  if (PQntuples(res) == 0)
5000  {
5001  if (!pset.quiet)
5002  {
5003  if (pattern)
5004  psql_error("Did not