/* display extra columns */ case'x':
my_opts->extended = true; break;
default: /* getopt_long already emitted a complaint */
pg_log_error_hint("Try \"%s --help\" for more information.", progname); exit(1);
}
}
if (optind < argc)
{
pg_log_error("too many command-line arguments (first is \"%s\")",
argv[optind]);
pg_log_error_hint("Try \"%s --help\" for more information.", progname); exit(1);
}
}
staticvoid
help(constchar *progname)
{
printf("%s helps examining the file structure used by PostgreSQL.\n\n" "Usage:\n" " %s [OPTION]...\n" "\nOptions:\n" " -f, --filenode=FILENODE show info for table with given file node\n" " -i, --indexes show indexes and sequences too\n" " -o, --oid=OID show info for table with given OID\n" " -q, --quiet quiet (don't show headers)\n" " -s, --tablespaces show all tablespaces\n" " -S, --system-objects show system objects too\n" " -t, --table=TABLE show info for named table\n" " -V, --version output version information, then exit\n" " -x, --extended extended (show additional columns)\n" " -?, --help show this help, then exit\n" "\nConnection options:\n" " -d, --dbname=DBNAME database to connect to\n" " -h, --host=HOSTNAME database server host or socket directory\n" " -H (same as -h, deprecated)\n" " -p, --port=PORT database server port number\n" " -U, --username=USERNAME connect as specified database user\n" "\nThe default action is to show all database OIDs.\n\n" "Report bugs to <%s>.\n" "%s home page: <%s>\n",
progname, progname, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL);
}
/* *get_comma_elts * *Returntheelementsofanearyasa(freshlyallocated)singlestring,in *singlequotes,separatedbycommasandproperlyescapedforinsertioninan *SQLstatement.
*/ char *
get_comma_elts(eary *eary)
{ char *ret,
*ptr; int i,
length = 0;
if (eary->num == 0) return pg_strdup("");
/* *PQescapeStringwants2*length+1bytesofbreathspace.Addtwo *charsperelementforthesinglequotesandoneforthecomma.
*/ for (i = 0; i < eary->num; i++)
length += strlen(eary->array[i]);
for (i = 0; i < eary->num; i++)
{ if (i != 0)
sprintf(ptr++, ",");
sprintf(ptr++, "'");
ptr += PQescapeString(ptr, eary->array[i], strlen(eary->array[i]));
sprintf(ptr++, "'");
}
/* check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD)
{
pg_log_error("%s", PQerrorMessage(conn));
PQfinish(conn); exit(1);
}
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL); if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
pg_log_error("could not clear \"search_path\": %s",
PQerrorMessage(conn));
PQclear(res);
PQfinish(conn); exit(1);
}
PQclear(res);
int nfields; int nrows; int i,
j,
l; int *length; char *pad;
/* make the call */
res = PQexec(conn, todo);
/* check and deal with errors */ if (!res || PQresultStatus(res) > 2)
{
pg_log_error("query failed: %s", PQerrorMessage(conn));
pg_log_error_detail("Query was: %s", todo);
PQclear(res);
PQfinish(conn); exit(1);
}
/* get the number of fields */
nrows = PQntuples(res);
nfields = PQnfields(res);
/* for each field, get the needed width */
length = (int *) pg_malloc(sizeof(int) * nfields); for (j = 0; j < nfields; j++)
length[j] = strlen(PQfname(res, j));
for (i = 0; i < nrows; i++)
{ for (j = 0; j < nfields; j++)
{
l = strlen(PQgetvalue(res, i, j)); if (l > length[j])
length[j] = strlen(PQgetvalue(res, i, j));
}
}
/* print a header */ if (!quiet)
{ for (j = 0, l = 0; j < nfields; j++)
{
fprintf(stdout, "%*s", length[j] + 2, PQfname(res, j));
l += length[j] + 2;
}
fprintf(stdout, "\n");
pad = (char *) pg_malloc(l + 1);
memset(pad, '-', l);
pad[l] = '\0';
fprintf(stdout, "%s\n", pad);
free(pad);
}
/* for each row, dump the information */ for (i = 0; i < nrows; i++)
{ for (j = 0; j < nfields; j++)
fprintf(stdout, "%*s", length[j] + 2, PQgetvalue(res, i, j));
fprintf(stdout, "\n");
}
/* get the oid and database name from the system pg_database table */
snprintf(todo, sizeof(todo), "SELECT d.oid AS \"Oid\", datname AS \"Database Name\", " "spcname AS \"Tablespace\" FROM pg_catalog.pg_database d JOIN pg_catalog.pg_tablespace t ON " "(dattablespace = t.oid) ORDER BY 2");
sql_exec(conn, todo, opts->quiet);
}
/* *Dumpalltables,indexesandsequencesinthecurrentdatabase.
*/ void
sql_exec_dumpalltables(PGconn *conn, struct options *opts)
{ char todo[1024]; char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
snprintf(todo, sizeof(todo), "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s " "FROM pg_catalog.pg_class c " " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database()," " pg_catalog.pg_tablespace t " "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
CppAsString2(RELKIND_MATVIEW) "%s%s) AND " " %s" " t.oid = CASE" " WHEN reltablespace <> 0 THEN reltablespace" " ELSE dattablespace" " END " "ORDER BY relname",
opts->extended ? addfields : "",
opts->indexes ? "," CppAsString2(RELKIND_INDEX) "," CppAsString2(RELKIND_SEQUENCE) : "",
opts->systables ? "," CppAsString2(RELKIND_TOASTVALUE) : "",
opts->systables ? "" : "n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname !~ '^pg_toast' AND");
sql_exec(conn, todo, opts->quiet);
}
/* *Showoid,filenumber,name,schemaandtablespaceforeachofthe *givenobjectsinthecurrentdatabase.
*/ void
sql_exec_searchtables(PGconn *conn, struct options *opts)
{ char *todo; char *qualifiers,
*ptr; char *comma_oids,
*comma_filenumbers,
*comma_tables; bool written = false; char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
/* get tables qualifiers, whether names, filenumbers, or OIDs */
comma_oids = get_comma_elts(opts->oids);
comma_tables = get_comma_elts(opts->tables);
comma_filenumbers = get_comma_elts(opts->filenumbers);
/* display only tablespaces */ if (my_opts->tablespaces)
{ if (!my_opts->quiet)
printf("All tablespaces:\n");
sql_exec_dumpalltbspc(pgconn, my_opts);
PQfinish(pgconn); exit(0);
}
/* display the given elements in the database */ if (my_opts->oids->num > 0 ||
my_opts->tables->num > 0 ||
my_opts->filenumbers->num > 0)
{ if (!my_opts->quiet)
printf("From database \"%s\":\n", my_opts->dbname);
sql_exec_searchtables(pgconn, my_opts);
PQfinish(pgconn); exit(0);
}
/* no elements given; dump the given database */ if (my_opts->dbname && !my_opts->nodb)
{ if (!my_opts->quiet)
printf("From database \"%s\":\n", my_opts->dbname);
sql_exec_dumpalltables(pgconn, my_opts);
PQfinish(pgconn); exit(0);
}
/* no database either; dump all databases */ if (!my_opts->quiet)
printf("All databases:\n");
sql_exec_dumpalldbs(pgconn, my_opts);
PQfinish(pgconn); return0;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.