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

References appendPQExpBufferStr(), and PQExpBufferData::len.

Referenced by describeRoles().

3244 {
3245  if (buf->len > 0)
3246  appendPQExpBufferStr(buf, ", ");
3247 
3248  appendPQExpBufferStr(buf, str);
3249 }
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 3020 of file describe.c.

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

Referenced by describeOneTableDetails().

3022 {
3023  /* relkinds for which we support tablespaces */
3024  if (relkind == RELKIND_RELATION ||
3025  relkind == RELKIND_MATVIEW ||
3026  relkind == RELKIND_INDEX ||
3027  relkind == RELKIND_PARTITIONED_TABLE)
3028  {
3029  /*
3030  * We ignore the database default tablespace so that users not using
3031  * tablespaces don't need to know about them. This case also covers
3032  * pre-8.0 servers, for which tablespace will always be 0.
3033  */
3034  if (tablespace != 0)
3035  {
3036  PGresult *result = NULL;
3038 
3039  initPQExpBuffer(&buf);
3040  printfPQExpBuffer(&buf,
3041  "SELECT spcname FROM pg_catalog.pg_tablespace\n"
3042  "WHERE oid = '%u';", tablespace);
3043  result = PSQLexec(buf.data);
3044  if (!result)
3045  {
3046  termPQExpBuffer(&buf);
3047  return;
3048  }
3049  /* Should always be the case, but.... */
3050  if (PQntuples(result) > 0)
3051  {
3052  if (newline)
3053  {
3054  /* Add the tablespace as a new footer */
3055  printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
3056  PQgetvalue(result, 0, 0));
3057  printTableAddFooter(cont, buf.data);
3058  }
3059  else
3060  {
3061  /* Append the tablespace to the latest footer */
3062  printfPQExpBuffer(&buf, "%s", cont->footer->data);
3063 
3064  /*-------
3065  translator: before this string there's an index description like
3066  '"foo_pkey" PRIMARY KEY, btree (a)' */
3067  appendPQExpBuffer(&buf, _(", tablespace \"%s\""),
3068  PQgetvalue(result, 0, 0));
3069  printTableSetFooter(cont, buf.data);
3070  }
3071  }
3072  PQclear(result);
3073  termPQExpBuffer(&buf);
3074  }
3075  }
3076 }
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3083
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
#define RELKIND_MATVIEW
Definition: pg_class.h:165
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static chr newline(void)
Definition: regc_lex.c:1137
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
char * tablespace
Definition: pgbench.c:146
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3058
printTableFooter * footer
Definition: print.h:157
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * data
Definition: print.h:135
#define RELKIND_INDEX
Definition: pg_class.h:161
#define RELKIND_RELATION
Definition: pg_class.h:160
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool describeAccessMethods ( const char *  pattern,
bool  verbose 
)

Definition at line 140 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 62 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 303 of file describe.c.

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

Referenced by exec_command_d().

304 {
305  bool showAggregate = strchr(functypes, 'a') != NULL;
306  bool showNormal = strchr(functypes, 'n') != NULL;
307  bool showTrigger = strchr(functypes, 't') != NULL;
308  bool showWindow = strchr(functypes, 'w') != NULL;
309  bool have_where;
311  PGresult *res;
312  printQueryOpt myopt = pset.popt;
313  static const bool translate_columns[] = {false, false, false, false, true, true, true, false, true, false, false, false, false};
314 
315  /* No "Parallel" column before 9.6 */
316  static const bool translate_columns_pre_96[] = {false, false, false, false, true, true, false, true, false, false, false, false};
317 
318  if (strlen(functypes) != strspn(functypes, "antwS+"))
319  {
320  psql_error("\\df only takes [antwS+] as options\n");
321  return true;
322  }
323 
324  if (showWindow && pset.sversion < 80400)
325  {
326  char sverbuf[32];
327 
328  psql_error("\\df does not take a \"w\" option with server version %s\n",
330  sverbuf, sizeof(sverbuf)));
331  return true;
332  }
333 
334  if (!showAggregate && !showNormal && !showTrigger && !showWindow)
335  {
336  showAggregate = showNormal = showTrigger = true;
337  if (pset.sversion >= 80400)
338  showWindow = true;
339  }
340 
341  initPQExpBuffer(&buf);
342 
343  printfPQExpBuffer(&buf,
344  "SELECT n.nspname as \"%s\",\n"
345  " p.proname as \"%s\",\n",
346  gettext_noop("Schema"),
347  gettext_noop("Name"));
348 
349  if (pset.sversion >= 80400)
350  appendPQExpBuffer(&buf,
351  " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
352  " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
353  " CASE\n"
354  " WHEN p.proisagg THEN '%s'\n"
355  " WHEN p.proiswindow THEN '%s'\n"
356  " WHEN p.prorettype = '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:556
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5399
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690
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(), printQueryOpt::footers, free, gettext_noop, i, initPQExpBuffer(), _psqlSettings::logfile, pg_strdup(), pg_wcswidth(), _psqlSettings::popt, PQclear(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), printfPQExpBuffer(), printQuery(), printTable(), printTableAddCell(), printTableAddFooter(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, psprintf(), psql_error(), PSQLexec(), _psqlSettings::queryFout, _psqlSettings::quiet, RELKIND_COMPOSITE_TYPE, RELKIND_FOREIGN_TABLE, RELKIND_INDEX, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_TOASTVALUE, RELKIND_VIEW, resetPQExpBuffer(), _psqlSettings::sversion, tablespace, termPQExpBuffer(), printQueryOpt::title, tmpbuf, printQueryOpt::topt, and printQueryOpt::translate_header.

Referenced by describeTableDetails().

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

Definition at line 4568 of file describe.c.

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

Referenced by listTSConfigsVerbose().

4570 {
4572  title;
4573  PGresult *res;
4574  printQueryOpt myopt = pset.popt;
4575 
4576  initPQExpBuffer(&buf);
4577 
4578  printfPQExpBuffer(&buf,
4579  "SELECT\n"
4580  " ( SELECT t.alias FROM\n"
4581  " pg_catalog.ts_token_type(c.cfgparser) AS t\n"
4582  " WHERE t.tokid = m.maptokentype ) AS \"%s\",\n"
4583  " pg_catalog.btrim(\n"
4584  " ARRAY( SELECT mm.mapdict::pg_catalog.regdictionary\n"
4585  " FROM pg_catalog.pg_ts_config_map AS mm\n"
4586  " WHERE mm.mapcfg = m.mapcfg AND mm.maptokentype = m.maptokentype\n"
4587  " ORDER BY mapcfg, maptokentype, mapseqno\n"
4588  " ) :: pg_catalog.text,\n"
4589  " '{}') AS \"%s\"\n"
4590  "FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m\n"
4591  "WHERE c.oid = '%s' AND m.mapcfg = c.oid\n"
4592  "GROUP BY m.mapcfg, m.maptokentype, c.cfgparser\n"
4593  "ORDER BY 1;",
4594  gettext_noop("Token"),
4595  gettext_noop("Dictionaries"),
4596  oid);
4597 
4598  res = PSQLexec(buf.data);
4599  termPQExpBuffer(&buf);
4600  if (!res)
4601  return false;
4602 
4603  initPQExpBuffer(&title);
4604 
4605  if (nspname)
4606  appendPQExpBuffer(&title, _("Text search configuration \"%s.%s\""),
4607  nspname, cfgname);
4608  else
4609  appendPQExpBuffer(&title, _("Text search configuration \"%s\""),
4610  cfgname);
4611 
4612  if (pnspname)
4613  appendPQExpBuffer(&title, _("\nParser: \"%s.%s\""),
4614  pnspname, prsname);
4615  else
4616  appendPQExpBuffer(&title, _("\nParser: \"%s\""),
4617  prsname);
4618 
4619  myopt.nullPrint = NULL;
4620  myopt.title = title.data;
4621  myopt.footers = NULL;
4622  myopt.topt.default_footer = false;
4623  myopt.translate_header = true;
4624 
4625  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4626 
4627  termPQExpBuffer(&title);
4628 
4629  PQclear(res);
4630  return true;
4631 }
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:67
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
char ** footers
Definition: print.h:168
bool default_footer
Definition: print.h:110
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
static bool describeOneTSParser ( const char *  oid,
const char *  nspname,
const char *  prsname 
)
static

Definition at line 4173 of file describe.c.

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

Referenced by listTSParsersVerbose().

4174 {
4176  PGresult *res;
4177  PQExpBufferData title;
4178  printQueryOpt myopt = pset.popt;
4179  static const bool translate_columns[] = {true, false, false};
4180 
4181  initPQExpBuffer(&buf);
4182 
4183  printfPQExpBuffer(&buf,
4184  "SELECT '%s' AS \"%s\",\n"
4185  " p.prsstart::pg_catalog.regproc AS \"%s\",\n"
4186  " pg_catalog.obj_description(p.prsstart, 'pg_proc') as \"%s\"\n"
4187  " FROM pg_catalog.pg_ts_parser p\n"
4188  " WHERE p.oid = '%s'\n"
4189  "UNION ALL\n"
4190  "SELECT '%s',\n"
4191  " p.prstoken::pg_catalog.regproc,\n"
4192  " pg_catalog.obj_description(p.prstoken, 'pg_proc')\n"
4193  " FROM pg_catalog.pg_ts_parser p\n"
4194  " WHERE p.oid = '%s'\n"
4195  "UNION ALL\n"
4196  "SELECT '%s',\n"
4197  " p.prsend::pg_catalog.regproc,\n"
4198  " pg_catalog.obj_description(p.prsend, 'pg_proc')\n"
4199  " FROM pg_catalog.pg_ts_parser p\n"
4200  " WHERE p.oid = '%s'\n"
4201  "UNION ALL\n"
4202  "SELECT '%s',\n"
4203  " p.prsheadline::pg_catalog.regproc,\n"
4204  " pg_catalog.obj_description(p.prsheadline, 'pg_proc')\n"
4205  " FROM pg_catalog.pg_ts_parser p\n"
4206  " WHERE p.oid = '%s'\n"
4207  "UNION ALL\n"
4208  "SELECT '%s',\n"
4209  " p.prslextype::pg_catalog.regproc,\n"
4210  " pg_catalog.obj_description(p.prslextype, 'pg_proc')\n"
4211  " FROM pg_catalog.pg_ts_parser p\n"
4212  " WHERE p.oid = '%s';",
4213  gettext_noop("Start parse"),
4214  gettext_noop("Method"),
4215  gettext_noop("Function"),
4216  gettext_noop("Description"),
4217  oid,
4218  gettext_noop("Get next token"),
4219  oid,
4220  gettext_noop("End parse"),
4221  oid,
4222  gettext_noop("Get headline"),
4223  oid,
4224  gettext_noop("Get token types"),
4225  oid);
4226 
4227  res = PSQLexec(buf.data);
4228  termPQExpBuffer(&buf);
4229  if (!res)
4230  return false;
4231 
4232  myopt.nullPrint = NULL;
4233  initPQExpBuffer(&title);
4234  if (nspname)
4235  printfPQExpBuffer(&title, _("Text search parser \"%s.%s\""),
4236  nspname, prsname);
4237  else
4238  printfPQExpBuffer(&title, _("Text search parser \"%s\""), prsname);
4239  myopt.title = title.data;
4240  myopt.footers = NULL;
4241  myopt.topt.default_footer = false;
4242  myopt.translate_header = true;
4243  myopt.translate_columns = translate_columns;
4244  myopt.n_translate_columns = lengthof(translate_columns);
4245 
4246  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4247 
4248  PQclear(res);
4249 
4250  initPQExpBuffer(&buf);
4251 
4252  printfPQExpBuffer(&buf,
4253  "SELECT t.alias as \"%s\",\n"
4254  " t.description as \"%s\"\n"
4255  "FROM pg_catalog.ts_token_type( '%s'::pg_catalog.oid ) as t\n"
4256  "ORDER BY 1;",
4257  gettext_noop("Token name"),
4258  gettext_noop("Description"),
4259  oid);
4260 
4261  res = PSQLexec(buf.data);
4262  termPQExpBuffer(&buf);
4263  if (!res)
4264  return false;
4265 
4266  myopt.nullPrint = NULL;
4267  if (nspname)
4268  printfPQExpBuffer(&title, _("Token types for parser \"%s.%s\""),
4269  nspname, prsname);
4270  else
4271  printfPQExpBuffer(&title, _("Token types for parser \"%s\""), prsname);
4272  myopt.title = title.data;
4273  myopt.footers = NULL;
4274  myopt.topt.default_footer = true;
4275  myopt.translate_header = true;
4276  myopt.translate_columns = NULL;
4277  myopt.n_translate_columns = 0;
4278 
4279  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
4280 
4281  termPQExpBuffer(&title);
4282  PQclear(res);
4283  return true;
4284 }
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:556
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
char ** footers
Definition: print.h:168
bool default_footer
Definition: print.h:110
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
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, 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:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool describePublications ( const char *  pattern)

Definition at line 5181 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 3084 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 5322 of file describe.c.

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

Referenced by exec_command_d().

5323 {
5325  PGresult *res;
5326  printQueryOpt myopt = pset.popt;
5327  static const bool translate_columns[] = {false, false, false, false,
5328  false, false};
5329 
5330  if (pset.sversion < 100000)
5331  {
5332  char sverbuf[32];
5333 
5334  psql_error("The server (version %s) does not support subscriptions.\n",
5336  sverbuf, sizeof(sverbuf)));
5337  return true;
5338  }
5339 
5340  initPQExpBuffer(&buf);
5341 
5342  printfPQExpBuffer(&buf,
5343  "SELECT subname AS \"%s\"\n"
5344  ", pg_catalog.pg_get_userbyid(subowner) AS \"%s\"\n"
5345  ", subenabled AS \"%s\"\n"
5346  ", subpublications AS \"%s\"\n",
5347  gettext_noop("Name"),
5348  gettext_noop("Owner"),
5349  gettext_noop("Enabled"),
5350  gettext_noop("Publication"));
5351 
5352  if (verbose)
5353  {
5354  appendPQExpBuffer(&buf,
5355  ", subsynccommit AS \"%s\"\n"
5356  ", subconninfo AS \"%s\"\n",
5357  gettext_noop("Synchronous commit"),
5358  gettext_noop("Conninfo"));
5359  }
5360 
5361  /* Only display subscriptions in current database. */
5362  appendPQExpBufferStr(&buf,
5363  "FROM pg_catalog.pg_subscription\n"
5364  "WHERE subdbid = (SELECT oid\n"
5365  " FROM pg_catalog.pg_database\n"
5366  " WHERE datname = current_database())");
5367 
5368  processSQLNamePattern(pset.db, &buf, pattern, true, false,
5369  NULL, "subname", NULL,
5370  NULL);
5371 
5372  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5373 
5374  res = PSQLexec(buf.data);
5375  termPQExpBuffer(&buf);
5376  if (!res)
5377  return false;
5378 
5379  myopt.nullPrint = NULL;
5380  myopt.title = _("List of subscriptions");
5381  myopt.translate_header = true;
5382  myopt.translate_columns = translate_columns;
5383  myopt.n_translate_columns = lengthof(translate_columns);
5384 
5385  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
5386 
5387  PQclear(res);
5388  return true;
5389 }
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:556
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690
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(), 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:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule)
Definition: string_utils.c:833
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
volatile bool cancel_pressed
Definition: print.c:46
static char * buf
Definition: pg_test_fsync.c:67
static bool describeOneTableDetails(const char *schemaname, const char *relationname, const char *oid, bool verbose)
Definition: describe.c:1370
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int i
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool describeTablespaces ( const char *  pattern,
bool  verbose 
)

Definition at line 208 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 3805 of file describe.c.

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

Referenced by exec_command_d().

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

Definition at line 3904 of file describe.c.

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

Referenced by exec_command_d().

3905 {
3907  PGresult *res;
3908  printQueryOpt myopt = pset.popt;
3909  static const bool translate_columns[] = {false, false, false, false, false, false};
3910 
3911  if (pset.sversion < 90100)
3912  {
3913  char sverbuf[32];
3914 
3915  psql_error("The server (version %s) does not support collations.\n",
3917  sverbuf, sizeof(sverbuf)));
3918  return true;
3919  }
3920 
3921  initPQExpBuffer(&buf);
3922 
3923  printfPQExpBuffer(&buf,
3924  "SELECT n.nspname AS \"%s\",\n"
3925  " c.collname AS \"%s\",\n"
3926  " c.collcollate AS \"%s\",\n"
3927  " c.collctype AS \"%s\"",
3928  gettext_noop("Schema"),
3929  gettext_noop("Name"),
3930  gettext_noop("Collate"),
3931  gettext_noop("Ctype"));
3932 
3933  if (pset.sversion >= 100000)
3934  appendPQExpBuffer(&buf,
3935  ",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
3936  gettext_noop("Provider"));
3937 
3938  if (verbose)
3939  appendPQExpBuffer(&buf,
3940  ",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
3941  gettext_noop("Description"));
3942 
3943  appendPQExpBufferStr(&buf,
3944  "\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"
3945  "WHERE n.oid = c.collnamespace\n");
3946 
3947  if (!showSystem && !pattern)
3948  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3949  " AND n.nspname <> 'information_schema'\n");
3950 
3951  /*
3952  * Hide collations that aren't usable in the current database's encoding.
3953  * If you think to change this, note that pg_collation_is_visible rejects
3954  * unusable collations, so you will need to hack name pattern processing
3955  * somehow to avoid inconsistent behavior.
3956  */
3957  appendPQExpBufferStr(&buf, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n");
3958 
3959  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3960  "n.nspname", "c.collname", NULL,
3961  "pg_catalog.pg_collation_is_visible(c.oid)");
3962 
3963  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3964 
3965  res = PSQLexec(buf.data);
3966  termPQExpBuffer(&buf);
3967  if (!res)
3968  return false;
3969 
3970  myopt.nullPrint = NULL;
3971  myopt.title = _("List of collations");
3972  myopt.translate_header = true;
3973  myopt.translate_columns = translate_columns;
3974  myopt.n_translate_columns = lengthof(translate_columns);
3975 
3976  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3977 
3978  PQclear(res);
3979  return true;
3980 }
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:556
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool listConversions ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3663 of file describe.c.

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

Referenced by exec_command_d().

3664 {
3666  PGresult *res;
3667  printQueryOpt myopt = pset.popt;
3668  static const bool translate_columns[] =
3669  {false, false, false, false, true, false};
3670 
3671  initPQExpBuffer(&buf);
3672 
3673  printfPQExpBuffer(&buf,
3674  "SELECT n.nspname AS \"%s\",\n"
3675  " c.conname AS \"%s\",\n"
3676  " pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
3677  " pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
3678  " CASE WHEN c.condefault THEN '%s'\n"
3679  " ELSE '%s' END AS \"%s\"",
3680  gettext_noop("Schema"),
3681  gettext_noop("Name"),
3682  gettext_noop("Source"),
3683  gettext_noop("Destination"),
3684  gettext_noop("yes"), gettext_noop("no"),
3685  gettext_noop("Default?"));
3686 
3687  if (verbose)
3688  appendPQExpBuffer(&buf,
3689  ",\n d.description AS \"%s\"",
3690  gettext_noop("Description"));
3691 
3692  appendPQExpBufferStr(&buf,
3693  "\nFROM pg_catalog.pg_conversion c\n"
3694  " JOIN pg_catalog.pg_namespace n "
3695  "ON n.oid = c.connamespace\n");
3696 
3697  if (verbose)
3698  appendPQExpBufferStr(&buf,
3699  "LEFT JOIN pg_catalog.pg_description d "
3700  "ON d.classoid = c.tableoid\n"
3701  " AND d.objoid = c.oid "
3702  "AND d.objsubid = 0\n");
3703 
3704  appendPQExpBufferStr(&buf, "WHERE true\n");
3705 
3706  if (!showSystem && !pattern)
3707  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3708  " AND n.nspname <> 'information_schema'\n");
3709 
3710  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3711  "n.nspname", "c.conname", NULL,
3712  "pg_catalog.pg_conversion_is_visible(c.oid)");
3713 
3714  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3715 
3716  res = PSQLexec(buf.data);
3717  termPQExpBuffer(&buf);
3718  if (!res)
3719  return false;
3720 
3721  myopt.nullPrint = NULL;
3722  myopt.title = _("List of conversions");
3723  myopt.translate_header = true;
3724  myopt.translate_columns = translate_columns;
3725  myopt.n_translate_columns = lengthof(translate_columns);
3726 
3727  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3728 
3729  PQclear(res);
3730  return true;
3731 }
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:556
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool listDbRoleSettings ( const char *  pattern,
const char *  pattern2 
)

Definition at line 3255 of file describe.c.

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

Referenced by exec_command_d().

3256 {
3258  PGresult *res;
3259  printQueryOpt myopt = pset.popt;
3260  bool havewhere;
3261 
3262  if (pset.sversion < 90000)
3263  {
3264  char sverbuf[32];
3265 
3266  psql_error("The server (version %s) does not support per-database role settings.\n",
3268  sverbuf, sizeof(sverbuf)));
3269  return true;
3270  }
3271 
3272  initPQExpBuffer(&buf);
3273 
3274  printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
3275  "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
3276  "FROM pg_catalog.pg_db_role_setting s\n"
3277  "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
3278  "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
3279  gettext_noop("Role"),
3280  gettext_noop("Database"),
3281  gettext_noop("Settings"));
3282  havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
3283  NULL, "r.rolname", NULL, NULL);
3284  processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
3285  NULL, "d.datname", NULL, NULL);
3286  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3287 
3288  res = PSQLexec(buf.data);
3289  termPQExpBuffer(&buf);
3290  if (!res)
3291  return false;
3292 
3293  /*
3294  * Most functions in this file are content to print an empty table when
3295  * there are no matching objects. We intentionally deviate from that
3296  * here, but only in !quiet mode, because of the possibility that the user
3297  * is confused about what the two pattern arguments mean.
3298  */
3299  if (PQntuples(res) == 0 && !pset.quiet)
3300  {
3301  if (pattern && pattern2)
3302  psql_error("Did not find any settings for role \"%s\" and database \"%s\".\n",
3303  pattern, pattern2);
3304  else if (pattern)
3305  psql_error("Did not find any settings for role \"%s\".\n",
3306  pattern);
3307  else
3308  psql_error("Did not find any settings.\n");
3309  }
3310  else
3311  {
3312  myopt.nullPrint = NULL;
3313  myopt.title = _("List of settings");
3314  myopt.translate_header = true;
3315 
3316  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3317  }
3318 
3319  PQclear(res);
3320  return true;
3321 }
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:2724
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690
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, 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:556
#define DEFACLOBJ_SEQUENCE
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
#define DEFACLOBJ_TYPE
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
#define DEFACLOBJ_NAMESPACE
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DEFACLOBJ_RELATION
char * title
Definition: print.h:167
#define DEFACLOBJ_FUNCTION
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5399
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool listDomains ( const char *  pattern,
bool  verbose,
bool  showSystem 
)

Definition at line 3577 of file describe.c.

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

Referenced by exec_command_d().

3578 {
3580  PGresult *res;
3581  printQueryOpt myopt = pset.popt;
3582 
3583  initPQExpBuffer(&buf);
3584 
3585  printfPQExpBuffer(&buf,
3586  "SELECT n.nspname as \"%s\",\n"
3587  " t.typname as \"%s\",\n"
3588  " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
3589  gettext_noop("Schema"),
3590  gettext_noop("Name"),
3591  gettext_noop("Type"));
3592 
3593  if (pset.sversion >= 90100)
3594  appendPQExpBuffer(&buf,
3595  " (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
3596  " WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
3597  gettext_noop("Collation"));
3598  appendPQExpBuffer(&buf,
3599  " CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"
3600  " t.typdefault as \"%s\",\n"
3601  " pg_catalog.array_to_string(ARRAY(\n"
3602  " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
3603  " ), ' ') as \"%s\"",
3604  gettext_noop("Nullable"),
3605  gettext_noop("Default"),
3606  gettext_noop("Check"));
3607 
3608  if (verbose)
3609  {
3610  if (pset.sversion >= 90200)
3611  {
3612  appendPQExpBufferStr(&buf, ",\n ");
3613  printACLColumn(&buf, "t.typacl");
3614  }
3615  appendPQExpBuffer(&buf,
3616  ",\n d.description as \"%s\"",
3617  gettext_noop("Description"));
3618  }
3619 
3620  appendPQExpBufferStr(&buf,
3621  "\nFROM pg_catalog.pg_type t\n"
3622  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
3623 
3624  if (verbose)
3625  appendPQExpBufferStr(&buf,
3626  " LEFT JOIN pg_catalog.pg_description d "
3627  "ON d.classoid = t.tableoid AND d.objoid = t.oid "
3628  "AND d.objsubid = 0\n");
3629 
3630  appendPQExpBufferStr(&buf, "WHERE t.typtype = 'd'\n");
3631 
3632  if (!showSystem && !pattern)
3633  appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
3634  " AND n.nspname <> 'information_schema'\n");
3635 
3636  processSQLNamePattern(pset.db, &buf, pattern, true, false,
3637  "n.nspname", "t.typname", NULL,
3638  "pg_catalog.pg_type_is_visible(t.oid)");
3639 
3640  appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
3641 
3642  res = PSQLexec(buf.data);
3643  termPQExpBuffer(&buf);
3644  if (!res)
3645  return false;
3646 
3647  myopt.nullPrint = NULL;
3648  myopt.title = _("List of domains");
3649  myopt.translate_header = true;
3650 
3651  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3652 
3653  PQclear(res);
3654  return true;
3655 }
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:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
static void printACLColumn(PQExpBuffer buf, const char *colname)
Definition: describe.c:5399
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool listEventTriggers ( const char *  pattern,
bool  verbose 
)

Definition at line 3739 of file describe.c.

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

Referenced by exec_command_d().

3740 {
3742  PGresult *res;
3743  printQueryOpt myopt = pset.popt;
3744  static const bool translate_columns[] =
3745  {false, false, false, true, false, false, false};
3746 
3747  initPQExpBuffer(&buf);
3748 
3749  printfPQExpBuffer(&buf,
3750  "SELECT evtname as \"%s\", "
3751  "evtevent as \"%s\", "
3752  "pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3753  " case evtenabled when 'O' then '%s'"
3754  " when 'R' then '%s'"
3755  " when 'A' then '%s'"
3756  " when 'D' then '%s' end as \"%s\",\n"
3757  " e.evtfoid::pg_catalog.regproc as \"%s\", "
3758  "pg_catalog.array_to_string(array(select x"
3759  " from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
3760  gettext_noop("Name"),
3761  gettext_noop("Event"),
3762  gettext_noop("Owner"),
3763  gettext_noop("enabled"),
3764  gettext_noop("replica"),
3765  gettext_noop("always"),
3766  gettext_noop("disabled"),
3767  gettext_noop("Enabled"),
3768  gettext_noop("Procedure"),
3769  gettext_noop("Tags"));
3770  if (verbose)
3771  appendPQExpBuffer(&buf,
3772  ",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
3773  gettext_noop("Description"));
3774  appendPQExpBufferStr(&buf,
3775  "\nFROM pg_catalog.pg_event_trigger e ");
3776 
3777  processSQLNamePattern(pset.db, &buf, pattern, false, false,
3778  NULL, "evtname", NULL, NULL);
3779 
3780  appendPQExpBufferStr(&buf, "ORDER BY 1");
3781 
3782  res = PSQLexec(buf.data);
3783  termPQExpBuffer(&buf);
3784  if (!res)
3785  return false;
3786 
3787  myopt.nullPrint = NULL;
3788  myopt.title = _("List of event triggers");
3789  myopt.translate_header = true;
3790  myopt.translate_columns = translate_columns;
3791  myopt.n_translate_columns = lengthof(translate_columns);
3792 
3793  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
3794 
3795  PQclear(res);
3796  return true;
3797 }
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:556
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
bool translate_header
Definition: print.h:169
static int verbose
Definition: pg_basebackup.c:85
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
int n_translate_columns
Definition: print.h:172
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
const bool * translate_columns
Definition: print.h:170
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690
bool listExtensionContents ( const char *  pattern)

Definition at line 5001 of file describe.c.

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

Referenced by exec_command_d().

5002 {
5004  PGresult *res;
5005  int i;
5006 
5007  if (pset.sversion < 90100)
5008  {
5009  char sverbuf[32];
5010 
5011  psql_error("The server (version %s) does not support extensions.\n",
5013  sverbuf, sizeof(sverbuf)));
5014  return true;
5015  }
5016 
5017  initPQExpBuffer(&buf);
5018  printfPQExpBuffer(&buf,
5019  "SELECT e.extname, e.oid\n"
5020  "FROM pg_catalog.pg_extension e\n");
5021 
5022  processSQLNamePattern(pset.db, &buf, pattern,
5023  false, false,
5024  NULL, "e.extname", NULL,
5025  NULL);
5026 
5027  appendPQExpBufferStr(&buf, "ORDER BY 1;");
5028 
5029  res = PSQLexec(buf.data);
5030  termPQExpBuffer(&buf);
5031  if (!res)
5032  return false;
5033 
5034  if (PQntuples(res) == 0)
5035  {
5036  if (!pset.quiet)
5037  {
5038  if (pattern)
5039  psql_error("Did not find any extension named \"%s\".\n",
5040  pattern);
5041  else
5042  psql_error("Did not find any extensions.\n");
5043  }
5044  PQclear(res);
5045  return false;
5046  }
5047 
5048  for (i = 0; i < PQntuples(res); i++)
5049  {
5050  const char *extname;
5051  const char *oid;
5052 
5053  extname = PQgetvalue(res, i, 0);
5054  oid = PQgetvalue(res, i, 1);
5055 
5056  if (!listOneExtensionContents(extname, oid))
5057  {
5058  PQclear(res);
5059  return false;
5060  }
5061  if (cancel_pressed)
5062  {
5063  PQclear(res);
5064  return false;