12 #include "catalog/pg_class_d.h"
65 static const struct option long_options[] = {
92 my_opts->
quiet =
false;
95 my_opts->
nodb =
false;
100 my_opts->
port = NULL;
106 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
111 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
113 puts(
"oid2name (PostgreSQL) " PG_VERSION);
119 while ((
c =
getopt_long(argc, argv,
"d:f:h:H:io:p:qsSt:U:x", long_options, &optindex)) != -1)
156 my_opts->
quiet =
true;
193 pg_log_error(
"too many command-line arguments (first is \"%s\")",
203 printf(
"%s helps examining the file structure used by PostgreSQL.\n\n"
207 " -f, --filenode=FILENODE show info for table with given file node\n"
208 " -i, --indexes show indexes and sequences too\n"
209 " -o, --oid=OID show info for table with given OID\n"
210 " -q, --quiet quiet (don't show headers)\n"
211 " -s, --tablespaces show all tablespaces\n"
212 " -S, --system-objects show system objects too\n"
213 " -t, --table=TABLE show info for named table\n"
214 " -V, --version output version information, then exit\n"
215 " -x, --extended extended (show additional columns)\n"
216 " -?, --help show this help, then exit\n"
217 "\nConnection options:\n"
218 " -d, --dbname=DBNAME database to connect to\n"
219 " -h, --host=HOSTNAME database server host or socket directory\n"
220 " -H (same as -h, deprecated)\n"
221 " -p, --port=PORT database server port number\n"
222 " -U, --username=USERNAME connect as specified database user\n"
223 "\nThe default action is to show all database OIDs.\n\n"
224 "Report bugs to <%s>.\n"
225 "%s home page: <%s>\n",
308 #define PARAMS_ARRAY_SIZE 7
313 keywords[0] =
"host";
315 keywords[1] =
"port";
317 keywords[2] =
"user";
319 keywords[3] =
"password";
321 keywords[4] =
"dbname";
323 keywords[5] =
"fallback_application_name";
332 pg_fatal(
"could not connect to database %s",
403 length = (
int *)
pg_malloc(
sizeof(
int) * nfields);
404 for (
j = 0;
j < nfields;
j++)
407 for (
i = 0;
i < nrows;
i++)
409 for (
j = 0;
j < nfields;
j++)
420 for (
j = 0, l = 0;
j < nfields;
j++)
434 for (
i = 0;
i < nrows;
i++)
436 for (
j = 0;
j < nfields;
j++)
458 "SELECT d.oid AS \"Oid\", datname AS \"Database Name\", "
459 "spcname AS \"Tablespace\" FROM pg_catalog.pg_database d JOIN pg_catalog.pg_tablespace t ON "
460 "(dattablespace = t.oid) ORDER BY 2");
472 char *addfields =
",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
475 "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s "
476 "FROM pg_catalog.pg_class c "
477 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
478 " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),"
479 " pg_catalog.pg_tablespace t "
484 " WHEN reltablespace <> 0 THEN reltablespace"
485 " ELSE dattablespace"
488 opts->extended ? addfields :
"",
491 opts->systables ?
"" :
"n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname !~ '^pg_toast' AND");
509 bool written =
false;
510 char *addfields =
",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
518 qualifiers = (
char *)
pg_malloc(strlen(comma_oids) + strlen(comma_tables) +
519 strlen(comma_filenumbers) + 80);
522 if (
opts->oids->num > 0)
524 ptr +=
sprintf(ptr,
"c.oid IN (%s)", comma_oids);
527 if (
opts->filenumbers->num > 0)
531 ptr +=
sprintf(ptr,
"pg_catalog.pg_relation_filenode(c.oid) IN (%s)",
535 if (
opts->tables->num > 0)
539 sprintf(ptr,
"c.relname ~~ ANY (ARRAY[%s])", comma_tables);
543 free(comma_filenumbers);
546 todo =
psprintf(
"SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
547 "FROM pg_catalog.pg_class c\n"
548 " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
549 " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),\n"
550 " pg_catalog.pg_tablespace t\n"
557 " WHEN reltablespace <> 0 THEN reltablespace\n"
558 " ELSE dattablespace\n"
561 "ORDER BY relname\n",
562 opts->extended ? addfields :
"",
576 "SELECT oid AS \"Oid\", spcname as \"Tablespace Name\"\n"
577 "FROM pg_catalog.pg_tablespace");
601 if (my_opts->
dbname == NULL)
603 my_opts->
dbname =
"postgres";
604 my_opts->
nodb =
true;
612 printf(
"All tablespaces:\n");
645 printf(
"All databases:\n");
static Datum values[MAXATTR]
#define ALWAYS_SECURE_SEARCH_PATH_SQL
static void PGresult * res
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
int PQconnectionNeedsPassword(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
int PQntuples(const PGresult *res)
char * PQfname(const PGresult *res, int field_num)
PGresult * PQexec(PGconn *conn, const char *query)
size_t PQescapeString(char *to, const char *from, size_t length)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
int PQnfields(const PGresult *res)
void * pg_realloc(void *ptr, size_t size)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_error_detail(...)
PGconn * sql_conn(struct options *my_opts)
void sql_exec_searchtables(PGconn *conn, struct options *opts)
static void help(const char *progname)
int main(int argc, char **argv)
int sql_exec(PGconn *conn, const char *todo, bool quiet)
void add_one_elt(char *eltname, eary *eary)
void get_opts(int argc, char **argv, struct options *my_opts)
#define PARAMS_ARRAY_SIZE
void sql_exec_dumpalltbspc(PGconn *conn, struct options *opts)
char * get_comma_elts(eary *eary)
void sql_exec_dumpalldbs(PGconn *conn, struct options *opts)
void sql_exec_dumpalltables(PGconn *conn, struct options *opts)
static AmcheckOptions opts
PGDLLIMPORT char * optarg
const char * get_progname(const char *argv0)
char * psprintf(const char *fmt,...)
char * simple_prompt(const char *prompt, bool echo)