PostgreSQL Source Code  git master
oid2name.c File Reference
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
#include "common/connect.h"
#include "common/logging.h"
#include "common/string.h"
#include "getopt_long.h"
#include "libpq-fe.h"
#include "pg_getopt.h"
Include dependency graph for oid2name.c:

Go to the source code of this file.

Data Structures

struct  eary
 
struct  options
 

Macros

#define PARAMS_ARRAY_SIZE   7
 

Functions

static void help (const char *progname)
 
void get_opts (int, char **, struct options *)
 
void add_one_elt (char *eltname, eary *eary)
 
char * get_comma_elts (eary *eary)
 
PGconnsql_conn (struct options *)
 
int sql_exec (PGconn *, const char *sql, bool quiet)
 
void sql_exec_dumpalldbs (PGconn *, struct options *)
 
void sql_exec_dumpalltables (PGconn *, struct options *)
 
void sql_exec_searchtables (PGconn *, struct options *)
 
void sql_exec_dumpalltbspc (PGconn *, struct options *)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ PARAMS_ARRAY_SIZE

#define PARAMS_ARRAY_SIZE   7

Referenced by sql_conn().

Function Documentation

◆ add_one_elt()

void add_one_elt ( char *  eltname,
eary eary 
)

Definition at line 234 of file oid2name.c.

References eary::alloc, eary::array, eary::num, pg_malloc(), pg_realloc(), and pg_strdup().

Referenced by get_opts().

235 {
236  if (eary->alloc == 0)
237  {
238  eary ->alloc = 8;
239  eary ->array = (char **) pg_malloc(8 * sizeof(char *));
240  }
241  else if (eary->num >= eary->alloc)
242  {
243  eary ->alloc *= 2;
244  eary ->array = (char **) pg_realloc(eary->array,
245  eary->alloc * sizeof(char *));
246  }
247 
248  eary ->array[eary->num] = pg_strdup(eltname);
249  eary ->num++;
250 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char ** array
Definition: oid2name.c:23
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
int alloc
Definition: oid2name.c:25
int num
Definition: oid2name.c:24

◆ get_comma_elts()

char * get_comma_elts ( eary eary)

Definition at line 260 of file oid2name.c.

References eary::array, i, eary::num, pg_malloc(), pg_strdup(), PQescapeString(), and sprintf.

Referenced by sql_exec_searchtables().

261 {
262  char *ret,
263  *ptr;
264  int i,
265  length = 0;
266 
267  if (eary->num == 0)
268  return pg_strdup("");
269 
270  /*
271  * PQescapeString wants 2 * length + 1 bytes of breath space. Add two
272  * chars per element for the single quotes and one for the comma.
273  */
274  for (i = 0; i < eary->num; i++)
275  length += strlen(eary->array[i]);
276 
277  ret = (char *) pg_malloc(length * 2 + 4 * eary->num);
278  ptr = ret;
279 
280  for (i = 0; i < eary->num; i++)
281  {
282  if (i != 0)
283  sprintf(ptr++, ",");
284  sprintf(ptr++, "'");
285  ptr += PQescapeString(ptr, eary->array[i], strlen(eary->array[i]));
286  sprintf(ptr++, "'");
287  }
288 
289  return ret;
290 }
size_t PQescapeString(char *to, const char *from, size_t length)
Definition: fe-exec.c:3931
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char ** array
Definition: oid2name.c:23
#define sprintf
Definition: port.h:218
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int i
int num
Definition: oid2name.c:24

◆ get_opts()

void get_opts ( int  argc,
char **  argv,
struct options my_opts 
)

Definition at line 63 of file oid2name.c.

References _, add_one_elt(), options::dbname, options::extended, options::filenodes, fprintf, get_progname(), getopt_long(), help(), options::hostname, options::indexes, no_argument, options::nodb, options::oids, optarg, optind, pg_logging_init(), pg_strdup(), options::port, options::progname, options::quiet, required_argument, options::systables, options::tables, options::tablespaces, and options::username.

Referenced by main().

64 {
65  static struct option long_options[] = {
66  {"dbname", required_argument, NULL, 'd'},
67  {"host", required_argument, NULL, 'h'},
68  {"host", required_argument, NULL, 'H'}, /* deprecated */
69  {"filenode", required_argument, NULL, 'f'},
70  {"indexes", no_argument, NULL, 'i'},
71  {"oid", required_argument, NULL, 'o'},
72  {"port", required_argument, NULL, 'p'},
73  {"quiet", no_argument, NULL, 'q'},
74  {"tablespaces", no_argument, NULL, 's'},
75  {"system-objects", no_argument, NULL, 'S'},
76  {"table", required_argument, NULL, 't'},
77  {"username", required_argument, NULL, 'U'},
78  {"version", no_argument, NULL, 'V'},
79  {"extended", no_argument, NULL, 'x'},
80  {"help", no_argument, NULL, '?'},
81  {NULL, 0, NULL, 0}
82  };
83 
84  int c;
85  const char *progname;
86  int optindex;
87 
88  pg_logging_init(argv[0]);
89  progname = get_progname(argv[0]);
90 
91  /* set the defaults */
92  my_opts->quiet = false;
93  my_opts->systables = false;
94  my_opts->indexes = false;
95  my_opts->nodb = false;
96  my_opts->extended = false;
97  my_opts->tablespaces = false;
98  my_opts->dbname = NULL;
99  my_opts->hostname = NULL;
100  my_opts->port = NULL;
101  my_opts->username = NULL;
102  my_opts->progname = progname;
103 
104  if (argc > 1)
105  {
106  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
107  {
108  help(progname);
109  exit(0);
110  }
111  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
112  {
113  puts("oid2name (PostgreSQL) " PG_VERSION);
114  exit(0);
115  }
116  }
117 
118  /* get opts */
119  while ((c = getopt_long(argc, argv, "d:f:h:H:io:p:qsSt:U:x", long_options, &optindex)) != -1)
120  {
121  switch (c)
122  {
123  /* specify the database */
124  case 'd':
125  my_opts->dbname = pg_strdup(optarg);
126  break;
127 
128  /* specify one filenode to show */
129  case 'f':
130  add_one_elt(optarg, my_opts->filenodes);
131  break;
132 
133  /* host to connect to */
134  case 'H': /* deprecated */
135  case 'h':
136  my_opts->hostname = pg_strdup(optarg);
137  break;
138 
139  /* also display indexes */
140  case 'i':
141  my_opts->indexes = true;
142  break;
143 
144  /* specify one Oid to show */
145  case 'o':
146  add_one_elt(optarg, my_opts->oids);
147  break;
148 
149  /* port to connect to on remote host */
150  case 'p':
151  my_opts->port = pg_strdup(optarg);
152  break;
153 
154  /* don't show headers */
155  case 'q':
156  my_opts->quiet = true;
157  break;
158 
159  /* dump tablespaces only */
160  case 's':
161  my_opts->tablespaces = true;
162  break;
163 
164  /* display system tables */
165  case 'S':
166  my_opts->systables = true;
167  break;
168 
169  /* specify one tablename to show */
170  case 't':
171  add_one_elt(optarg, my_opts->tables);
172  break;
173 
174  /* username */
175  case 'U':
176  my_opts->username = pg_strdup(optarg);
177  break;
178 
179  /* display extra columns */
180  case 'x':
181  my_opts->extended = true;
182  break;
183 
184  default:
185  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
186  exit(1);
187  }
188  }
189 
190  if (optind < argc)
191  {
192  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
193  progname, argv[optind]);
194  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
195  exit(1);
196  }
197 }
const char * progname
Definition: main.c:46
const char * get_progname(const char *argv0)
Definition: path.c:453
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void pg_logging_init(const char *argv0)
Definition: logging.c:81
char * hostname
Definition: oid2name.c:43
bool nodb
Definition: oid2name.c:38
char * username
Definition: oid2name.c:45
#define fprintf
Definition: port.h:220
bool tablespaces
Definition: oid2name.c:40
static void help(const char *progname)
Definition: oid2name.c:200
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
eary * oids
Definition: oid2name.c:32
bool indexes
Definition: oid2name.c:37
char * c
bool systables
Definition: oid2name.c:36
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
eary * tables
Definition: oid2name.c:31
#define no_argument
Definition: getopt_long.h:24
void add_one_elt(char *eltname, eary *eary)
Definition: oid2name.c:234
const char * progname
Definition: oid2name.c:46
char * dbname
Definition: oid2name.c:42
bool quiet
Definition: oid2name.c:35
bool extended
Definition: oid2name.c:39
char * optarg
Definition: getopt.c:52
char * port
Definition: oid2name.c:44
eary * filenodes
Definition: oid2name.c:33
#define _(x)
Definition: elog.c:89

◆ help()

static void help ( const char *  progname)
static

Definition at line 200 of file oid2name.c.

References printf.

Referenced by get_opts().

201 {
202  printf("%s helps examining the file structure used by PostgreSQL.\n\n"
203  "Usage:\n"
204  " %s [OPTION]...\n"
205  "\nOptions:\n"
206  " -f, --filenode=FILENODE show info for table with given file node\n"
207  " -i, --indexes show indexes and sequences too\n"
208  " -o, --oid=OID show info for table with given OID\n"
209  " -q, --quiet quiet (don't show headers)\n"
210  " -s, --tablespaces show all tablespaces\n"
211  " -S, --system-objects show system objects too\n"
212  " -t, --table=TABLE show info for named table\n"
213  " -V, --version output version information, then exit\n"
214  " -x, --extended extended (show additional columns)\n"
215  " -?, --help show this help, then exit\n"
216  "\nConnection options:\n"
217  " -d, --dbname=DBNAME database to connect to\n"
218  " -h, --host=HOSTNAME database server host or socket directory\n"
219  " -H same as -h, deprecated option\n"
220  " -p, --port=PORT database server port number\n"
221  " -U, --username=USERNAME connect as specified database user\n"
222  "\nThe default action is to show all database OIDs.\n\n"
223  "Report bugs to <%s>.\n"
224  "%s home page: <%s>\n",
225  progname, progname, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL);
226 }
const char * progname
Definition: main.c:46
#define printf(...)
Definition: port.h:222

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 584 of file oid2name.c.

References eary::alloc, options::dbname, options::filenodes, get_opts(), options::nodb, eary::num, options::oids, pg_malloc(), PQfinish(), printf, options::quiet, sql_conn(), sql_exec_dumpalldbs(), sql_exec_dumpalltables(), sql_exec_dumpalltbspc(), sql_exec_searchtables(), options::tables, and options::tablespaces.

Referenced by aes_cbc_decrypt(), DoubleMetaphone(), and update_spins_per_delay().

585 {
586  struct options *my_opts;
587  PGconn *pgconn;
588 
589  my_opts = (struct options *) pg_malloc(sizeof(struct options));
590 
591  my_opts->oids = (eary *) pg_malloc(sizeof(eary));
592  my_opts->tables = (eary *) pg_malloc(sizeof(eary));
593  my_opts->filenodes = (eary *) pg_malloc(sizeof(eary));
594 
595  my_opts->oids->num = my_opts->oids->alloc = 0;
596  my_opts->tables->num = my_opts->tables->alloc = 0;
597  my_opts->filenodes->num = my_opts->filenodes->alloc = 0;
598 
599  /* parse the opts */
600  get_opts(argc, argv, my_opts);
601 
602  if (my_opts->dbname == NULL)
603  {
604  my_opts->dbname = "postgres";
605  my_opts->nodb = true;
606  }
607  pgconn = sql_conn(my_opts);
608 
609  /* display only tablespaces */
610  if (my_opts->tablespaces)
611  {
612  if (!my_opts->quiet)
613  printf("All tablespaces:\n");
614  sql_exec_dumpalltbspc(pgconn, my_opts);
615 
616  PQfinish(pgconn);
617  exit(0);
618  }
619 
620  /* display the given elements in the database */
621  if (my_opts->oids->num > 0 ||
622  my_opts->tables->num > 0 ||
623  my_opts->filenodes->num > 0)
624  {
625  if (!my_opts->quiet)
626  printf("From database \"%s\":\n", my_opts->dbname);
627  sql_exec_searchtables(pgconn, my_opts);
628 
629  PQfinish(pgconn);
630  exit(0);
631  }
632 
633  /* no elements given; dump the given database */
634  if (my_opts->dbname && !my_opts->nodb)
635  {
636  if (!my_opts->quiet)
637  printf("From database \"%s\":\n", my_opts->dbname);
638  sql_exec_dumpalltables(pgconn, my_opts);
639 
640  PQfinish(pgconn);
641  exit(0);
642  }
643 
644  /* no database either; dump all databases */
645  if (!my_opts->quiet)
646  printf("All databases:\n");
647  sql_exec_dumpalldbs(pgconn, my_opts);
648 
649  PQfinish(pgconn);
650  return 0;
651 }
Definition: oid2name.c:21
void sql_exec_dumpalltbspc(PGconn *, struct options *)
Definition: oid2name.c:572
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
PGconn * sql_conn(struct options *)
Definition: oid2name.c:294
void get_opts(int, char **, struct options *)
Definition: oid2name.c:63
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
#define printf(...)
Definition: port.h:222
bool nodb
Definition: oid2name.c:38
bool tablespaces
Definition: oid2name.c:40
void sql_exec_dumpalldbs(PGconn *, struct options *)
Definition: oid2name.c:454
eary * oids
Definition: oid2name.c:32
eary * tables
Definition: oid2name.c:31
char * dbname
Definition: oid2name.c:42
bool quiet
Definition: oid2name.c:35
int alloc
Definition: oid2name.c:25
void sql_exec_searchtables(PGconn *, struct options *)
Definition: oid2name.c:503
eary * filenodes
Definition: oid2name.c:33
int num
Definition: oid2name.c:24
void sql_exec_dumpalltables(PGconn *, struct options *)
Definition: oid2name.c:471

◆ sql_conn()

PGconn * sql_conn ( struct options my_opts)

Definition at line 294 of file oid2name.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, conn, CONNECTION_BAD, options::dbname, options::hostname, PARAMS_ARRAY_SIZE, password, pg_log_error, PGRES_TUPLES_OK, options::port, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), PQstatus(), options::progname, simple_prompt(), options::username, and values.

Referenced by main().

295 {
296  PGconn *conn;
297  char *password = NULL;
298  bool new_pass;
299  PGresult *res;
300 
301  /*
302  * Start the connection. Loop until we have a password if requested by
303  * backend.
304  */
305  do
306  {
307 #define PARAMS_ARRAY_SIZE 7
308 
309  const char *keywords[PARAMS_ARRAY_SIZE];
310  const char *values[PARAMS_ARRAY_SIZE];
311 
312  keywords[0] = "host";
313  values[0] = my_opts->hostname;
314  keywords[1] = "port";
315  values[1] = my_opts->port;
316  keywords[2] = "user";
317  values[2] = my_opts->username;
318  keywords[3] = "password";
319  values[3] = password;
320  keywords[4] = "dbname";
321  values[4] = my_opts->dbname;
322  keywords[5] = "fallback_application_name";
323  values[5] = my_opts->progname;
324  keywords[6] = NULL;
325  values[6] = NULL;
326 
327  new_pass = false;
328  conn = PQconnectdbParams(keywords, values, true);
329 
330  if (!conn)
331  {
332  pg_log_error("could not connect to database %s",
333  my_opts->dbname);
334  exit(1);
335  }
336 
337  if (PQstatus(conn) == CONNECTION_BAD &&
339  !password)
340  {
341  PQfinish(conn);
342  password = simple_prompt("Password: ", false);
343  new_pass = true;
344  }
345  } while (new_pass);
346 
347  /* check to see that the backend connection was successfully made */
348  if (PQstatus(conn) == CONNECTION_BAD)
349  {
350  pg_log_error("%s", PQerrorMessage(conn));
351  PQfinish(conn);
352  exit(1);
353  }
354 
356  if (PQresultStatus(res) != PGRES_TUPLES_OK)
357  {
358  pg_log_error("could not clear search_path: %s",
359  PQerrorMessage(conn));
360  PQclear(res);
361  PQfinish(conn);
362  exit(-1);
363  }
364  PQclear(res);
365 
366  /* return the conn if good */
367  return conn;
368 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
#define pg_log_error(...)
Definition: logging.h:80
#define PARAMS_ARRAY_SIZE
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
char * hostname
Definition: oid2name.c:43
char * username
Definition: oid2name.c:45
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:657
PGconn * conn
Definition: streamutil.c:54
static char * password
Definition: streamutil.c:53
const char * progname
Definition: oid2name.c:46
char * dbname
Definition: oid2name.c:42
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6795
char * port
Definition: oid2name.c:44
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6691

◆ sql_exec()

int sql_exec ( PGconn conn,
const char *  sql,
bool  quiet 
)

Definition at line 374 of file oid2name.c.

References fprintf, free, i, MemSet, pg_log_error, pg_malloc(), PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQfname(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), and generate_unaccent_rules::stdout.

Referenced by sql_exec_dumpalldbs(), sql_exec_dumpalltables(), sql_exec_dumpalltbspc(), and sql_exec_searchtables().

375 {
376  PGresult *res;
377 
378  int nfields;
379  int nrows;
380  int i,
381  j,
382  l;
383  int *length;
384  char *pad;
385 
386  /* make the call */
387  res = PQexec(conn, todo);
388 
389  /* check and deal with errors */
390  if (!res || PQresultStatus(res) > 2)
391  {
392  pg_log_error("query failed: %s", PQerrorMessage(conn));
393  pg_log_error("query was: %s", todo);
394 
395  PQclear(res);
396  PQfinish(conn);
397  exit(-1);
398  }
399 
400  /* get the number of fields */
401  nrows = PQntuples(res);
402  nfields = PQnfields(res);
403 
404  /* for each field, get the needed width */
405  length = (int *) pg_malloc(sizeof(int) * nfields);
406  for (j = 0; j < nfields; j++)
407  length[j] = strlen(PQfname(res, j));
408 
409  for (i = 0; i < nrows; i++)
410  {
411  for (j = 0; j < nfields; j++)
412  {
413  l = strlen(PQgetvalue(res, i, j));
414  if (l > length[j])
415  length[j] = strlen(PQgetvalue(res, i, j));
416  }
417  }
418 
419  /* print a header */
420  if (!quiet)
421  {
422  for (j = 0, l = 0; j < nfields; j++)
423  {
424  fprintf(stdout, "%*s", length[j] + 2, PQfname(res, j));
425  l += length[j] + 2;
426  }
427  fprintf(stdout, "\n");
428  pad = (char *) pg_malloc(l + 1);
429  MemSet(pad, '-', l);
430  pad[l] = '\0';
431  fprintf(stdout, "%s\n", pad);
432  free(pad);
433  }
434 
435  /* for each row, dump the information */
436  for (i = 0; i < nrows; i++)
437  {
438  for (j = 0; j < nfields; j++)
439  fprintf(stdout, "%*s", length[j] + 2, PQgetvalue(res, i, j));
440  fprintf(stdout, "\n");
441  }
442 
443  /* cleanup */
444  PQclear(res);
445  free(length);
446 
447  return 0;
448 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3334
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
#define MemSet(start, val, len)
Definition: c.h:1008
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
#define fprintf
Definition: port.h:220
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
bool quiet
Definition: oid2name.c:35
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ sql_exec_dumpalldbs()

void sql_exec_dumpalldbs ( PGconn conn,
struct options opts 
)

Definition at line 454 of file oid2name.c.

References options::quiet, snprintf, and sql_exec().

Referenced by main().

455 {
456  char todo[1024];
457 
458  /* get the oid and database name from the system pg_database table */
459  snprintf(todo, sizeof(todo),
460  "SELECT d.oid AS \"Oid\", datname AS \"Database Name\", "
461  "spcname AS \"Tablespace\" FROM pg_catalog.pg_database d JOIN pg_catalog.pg_tablespace t ON "
462  "(dattablespace = t.oid) ORDER BY 2");
463 
464  sql_exec(conn, todo, opts->quiet);
465 }
int sql_exec(PGconn *, const char *sql, bool quiet)
Definition: oid2name.c:374
bool quiet
Definition: oid2name.c:35
#define snprintf
Definition: port.h:216

◆ sql_exec_dumpalltables()

void sql_exec_dumpalltables ( PGconn conn,
struct options opts 
)

Definition at line 471 of file oid2name.c.

References CppAsString2, options::extended, options::indexes, options::quiet, snprintf, sql_exec(), and options::systables.

Referenced by main().

472 {
473  char todo[1024];
474  char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
475 
476  snprintf(todo, sizeof(todo),
477  "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s "
478  "FROM pg_catalog.pg_class c "
479  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
480  " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),"
481  " pg_catalog.pg_tablespace t "
482  "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
483  CppAsString2(RELKIND_MATVIEW) "%s%s) AND "
484  " %s"
485  " t.oid = CASE"
486  " WHEN reltablespace <> 0 THEN reltablespace"
487  " ELSE dattablespace"
488  " END "
489  "ORDER BY relname",
490  opts->extended ? addfields : "",
491  opts->indexes ? "," CppAsString2(RELKIND_INDEX) "," CppAsString2(RELKIND_SEQUENCE) : "",
492  opts->systables ? "," CppAsString2(RELKIND_TOASTVALUE) : "",
493  opts->systables ? "" : "n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname !~ '^pg_toast' AND");
494 
495  sql_exec(conn, todo, opts->quiet);
496 }
int sql_exec(PGconn *, const char *sql, bool quiet)
Definition: oid2name.c:374
bool indexes
Definition: oid2name.c:37
bool systables
Definition: oid2name.c:36
#define CppAsString2(x)
Definition: c.h:289
bool quiet
Definition: oid2name.c:35
bool extended
Definition: oid2name.c:39
#define snprintf
Definition: port.h:216

◆ sql_exec_dumpalltbspc()

void sql_exec_dumpalltbspc ( PGconn conn,
struct options opts 
)

Definition at line 572 of file oid2name.c.

References options::quiet, snprintf, and sql_exec().

Referenced by main().

573 {
574  char todo[1024];
575 
576  snprintf(todo, sizeof(todo),
577  "SELECT oid AS \"Oid\", spcname as \"Tablespace Name\"\n"
578  "FROM pg_catalog.pg_tablespace");
579 
580  sql_exec(conn, todo, opts->quiet);
581 }
int sql_exec(PGconn *, const char *sql, bool quiet)
Definition: oid2name.c:374
bool quiet
Definition: oid2name.c:35
#define snprintf
Definition: port.h:216

◆ sql_exec_searchtables()

void sql_exec_searchtables ( PGconn conn,
struct options opts 
)

Definition at line 503 of file oid2name.c.

References CppAsString2, options::extended, options::filenodes, free, get_comma_elts(), eary::num, options::oids, pg_malloc(), psprintf(), options::quiet, sprintf, sql_exec(), and options::tables.

Referenced by main().

504 {
505  char *todo;
506  char *qualifiers,
507  *ptr;
508  char *comma_oids,
509  *comma_filenodes,
510  *comma_tables;
511  bool written = false;
512  char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
513 
514  /* get tables qualifiers, whether names, filenodes, or OIDs */
515  comma_oids = get_comma_elts(opts->oids);
516  comma_tables = get_comma_elts(opts->tables);
517  comma_filenodes = get_comma_elts(opts->filenodes);
518 
519  /* 80 extra chars for SQL expression */
520  qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) +
521  strlen(comma_filenodes) + 80);
522  ptr = qualifiers;
523 
524  if (opts->oids->num > 0)
525  {
526  ptr += sprintf(ptr, "c.oid IN (%s)", comma_oids);
527  written = true;
528  }
529  if (opts->filenodes->num > 0)
530  {
531  if (written)
532  ptr += sprintf(ptr, " OR ");
533  ptr += sprintf(ptr, "pg_catalog.pg_relation_filenode(c.oid) IN (%s)", comma_filenodes);
534  written = true;
535  }
536  if (opts->tables->num > 0)
537  {
538  if (written)
539  ptr += sprintf(ptr, " OR ");
540  sprintf(ptr, "c.relname ~~ ANY (ARRAY[%s])", comma_tables);
541  }
542  free(comma_oids);
543  free(comma_tables);
544  free(comma_filenodes);
545 
546  /* now build the query */
547  todo = psprintf("SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
548  "FROM pg_catalog.pg_class c\n"
549  " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
550  " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),\n"
551  " pg_catalog.pg_tablespace t\n"
552  "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
553  CppAsString2(RELKIND_MATVIEW) ","
554  CppAsString2(RELKIND_INDEX) ","
555  CppAsString2(RELKIND_SEQUENCE) ","
556  CppAsString2(RELKIND_TOASTVALUE) ") AND\n"
557  " t.oid = CASE\n"
558  " WHEN reltablespace <> 0 THEN reltablespace\n"
559  " ELSE dattablespace\n"
560  " END AND\n"
561  " (%s)\n"
562  "ORDER BY relname\n",
563  opts->extended ? addfields : "",
564  qualifiers);
565 
566  free(qualifiers);
567 
568  sql_exec(conn, todo, opts->quiet);
569 }
int sql_exec(PGconn *, const char *sql, bool quiet)
Definition: oid2name.c:374
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define sprintf
Definition: port.h:218
eary * oids
Definition: oid2name.c:32
eary * tables
Definition: oid2name.c:31
#define CppAsString2(x)
Definition: c.h:289
#define free(a)
Definition: header.h:65
bool quiet
Definition: oid2name.c:35
bool extended
Definition: oid2name.c:39
char * get_comma_elts(eary *eary)
Definition: oid2name.c:260
eary * filenodes
Definition: oid2name.c:33
int num
Definition: oid2name.c:24