Add -q option to oid2name. Add sample session to README.
This commit is contained in:
parent
0f865e17e2
commit
5a8f555e41
@ -1,17 +1,20 @@
|
|||||||
With version 7.1 of PostgreSQL server, the old naming scheme for
|
This utility allows administrators to view the file structure used by
|
||||||
databases and tables (in $PGDATA/base) has changed. The databases
|
PostgreSQL. Databases are placed in directories based on their OIDs in
|
||||||
are put in folders for their OID in pg_database and the tables in
|
pg_database, and the tables in that directory are named by their OIDs,
|
||||||
that folder are named for their OIDs in pg_class. This app connects
|
stored in pg_class.relfilenode. Oid2name connects to the database and
|
||||||
to the database (you can specify host, port, user, pass etc to
|
extracts the OID and table name information.
|
||||||
connect to a host other than localhost) and extracts the OID and
|
|
||||||
table name information. It has 4 ways it can be run:
|
|
||||||
|
|
||||||
pg_oid2name
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
It can be used in four ways:
|
||||||
|
|
||||||
|
|
||||||
|
oid2name
|
||||||
|
|
||||||
This will connect to the template1 database and display all databases
|
This will connect to the template1 database and display all databases
|
||||||
in the system.
|
in the system:
|
||||||
|
|
||||||
$ ./pg_oid2name
|
$ oid2name
|
||||||
All databases:
|
All databases:
|
||||||
---------------------------------
|
---------------------------------
|
||||||
18720 = test1
|
18720 = test1
|
||||||
@ -21,49 +24,85 @@ table name information. It has 4 ways it can be run:
|
|||||||
18735 = postgres
|
18735 = postgres
|
||||||
18736 = cssi
|
18736 = cssi
|
||||||
|
|
||||||
pg_oid2name -d test [-x]
|
|
||||||
|
|
||||||
This connects to the database test and shows all tables and their OIDs.
|
oid2name -d test [-x]
|
||||||
|
|
||||||
$ ./pg_oid2name -d test
|
This connects to the database test and shows all tables and their OIDs:
|
||||||
|
|
||||||
|
$ oid2name -d test
|
||||||
All tables from database "test":
|
All tables from database "test":
|
||||||
---------------------------------
|
---------------------------------
|
||||||
18766 = dns
|
18766 = dns
|
||||||
18737 = ips
|
18737 = ips
|
||||||
18722 = testdate
|
18722 = testdate
|
||||||
|
|
||||||
pg_oid2name -d test -o 18737 or
|
|
||||||
pg_oid2name -d test -t testdate
|
oid2name -d test -o 18737
|
||||||
|
oid2name -d test -t testdate
|
||||||
|
|
||||||
This will connect to the database test and display the table name for oid
|
This will connect to the database test and display the table name for oid
|
||||||
18737 and the oid for table name testdate respectivly.
|
18737 and the oid for table name testdate respectively:
|
||||||
|
|
||||||
$ ./pg_oid2name -d test -o 18737
|
$ oid2name -d test -o 18737
|
||||||
Tablename of oid 18737 from database "test":
|
Tablename of oid 18737 from database "test":
|
||||||
---------------------------------
|
---------------------------------
|
||||||
18737 = ips
|
18737 = ips
|
||||||
|
|
||||||
|
|
||||||
$ ./pg_oid2name -d test -t testdate
|
$ oid2name -d test -t testdate
|
||||||
Oid of table testdate from database "test":
|
Oid of table testdate from database "test":
|
||||||
_______________________________
|
---------------------------------
|
||||||
18722 = testdate
|
18722 = testdate
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
There should be a decent amount of error handling in the app, a lot of it
|
Sample session:
|
||||||
dealt with via the postgres function calls.
|
|
||||||
|
|
||||||
$ ./pg_oid2name -d nothere -t testdate
|
$ cd /u/pg/data/base
|
||||||
Oid of table testdate from database "nothere":
|
$ oid2name
|
||||||
_______________________________
|
All databases:
|
||||||
Connection to database 'nothere' failed.
|
---------------------------------
|
||||||
FATAL 1: Database "nothere" does not exist in the system catalog.
|
16817 = test2
|
||||||
|
16578 = x
|
||||||
|
16756 = test
|
||||||
|
1 = template1
|
||||||
|
16569 = template0
|
||||||
|
16818 = test3
|
||||||
|
16811 = floattest
|
||||||
|
|
||||||
$ ./pg_oid2name -d test -t nothere
|
$ cd 16756
|
||||||
Oid of table nothere from database "test":
|
$ ls 1873*
|
||||||
_______________________________
|
18730 18731 18732 18735 18736 18737 18738 18739
|
||||||
No tables with that name found
|
|
||||||
|
|
||||||
|
$ oid2name -d test -o 18737
|
||||||
|
Tablename of oid 18737 from database "test":
|
||||||
|
---------------------------------
|
||||||
|
18737 = ips
|
||||||
|
|
||||||
|
$ oid2name -d test -t ips
|
||||||
|
Oid of table ips from database "test":
|
||||||
|
---------------------------------
|
||||||
|
18737 = ips
|
||||||
|
|
||||||
|
$ du * | while read SIZE OID
|
||||||
|
> do
|
||||||
|
> echo "$SIZE `oid2name -q -d test -o $OID`"
|
||||||
|
> done
|
||||||
|
24 18737 = ips
|
||||||
|
36 18722 = cities
|
||||||
|
...
|
||||||
|
|
||||||
|
$ du * | while read SIZE OID
|
||||||
|
> do
|
||||||
|
> echo "$SIZE `oid2name -q -d test -o $OID`"
|
||||||
|
> done |
|
||||||
|
> sort -rn
|
||||||
|
2048 19324 = bigtable
|
||||||
|
1950 23903 = customers
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
Mail me with any problems or additions you would like to see. Clearing
|
Mail me with any problems or additions you would like to see. Clearing
|
||||||
house for the code will be at: http://www.crimelabs.net
|
house for the code will be at: http://www.crimelabs.net
|
||||||
|
@ -21,6 +21,8 @@ struct options
|
|||||||
int gettable;
|
int gettable;
|
||||||
int getoid;
|
int getoid;
|
||||||
|
|
||||||
|
int quiet;
|
||||||
|
|
||||||
int systables;
|
int systables;
|
||||||
|
|
||||||
int remotehost;
|
int remotehost;
|
||||||
@ -59,6 +61,8 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
my_opts->gettable = 0;
|
my_opts->gettable = 0;
|
||||||
my_opts->getoid = 0;
|
my_opts->getoid = 0;
|
||||||
|
|
||||||
|
my_opts->quiet = 0;
|
||||||
|
|
||||||
my_opts->systables = 0;
|
my_opts->systables = 0;
|
||||||
|
|
||||||
my_opts->remotehost = 0;
|
my_opts->remotehost = 0;
|
||||||
@ -67,7 +71,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
my_opts->remotepass = 0;
|
my_opts->remotepass = 0;
|
||||||
|
|
||||||
/* get opts */
|
/* get opts */
|
||||||
while ((c = getopt(argc, argv, "H:p:U:P:d:t:o:xh?")) != -1)
|
while ((c = getopt(argc, argv, "H:p:U:P:d:t:o:qxh?")) != -1)
|
||||||
{
|
{
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
@ -82,13 +86,13 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
/* make sure we set the database first */
|
/* make sure we set the database first */
|
||||||
if (!my_opts->getdatabase)
|
if (!my_opts->getdatabase)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Sorry, but you must specify a database to dump from.\n");
|
fprintf(stderr, "You must specify a database to dump from.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
/* make sure we don't try to do a -o also */
|
/* make sure we don't try to do a -o also */
|
||||||
if (my_opts->getoid)
|
if (my_opts->getoid)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Sorry, you can only specify either oid or table\n");
|
fprintf(stderr, "You can only specify either oid or table\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,13 +106,13 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
/* make sure we set the database first */
|
/* make sure we set the database first */
|
||||||
if (!my_opts->getdatabase)
|
if (!my_opts->getdatabase)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Sorry, but you must specify a database to dump from.\n");
|
fprintf(stderr, "You must specify a database to dump from.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
/* make sure we don't try to do a -t also */
|
/* make sure we don't try to do a -t also */
|
||||||
if (my_opts->gettable)
|
if (my_opts->gettable)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Sorry, you can only specify either oid or table\n");
|
fprintf(stderr, "You can only specify either oid or table\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +121,10 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'q':
|
||||||
|
my_opts->quiet = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
/* host to connect to */
|
/* host to connect to */
|
||||||
case 'H':
|
case 'H':
|
||||||
my_opts->remotehost = 1;
|
my_opts->remotehost = 1;
|
||||||
@ -149,17 +157,18 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
/* help! (ugly in code for easier editing) */
|
/* help! (ugly in code for easier editing) */
|
||||||
case '?':
|
case '?':
|
||||||
case 'h':
|
case 'h':
|
||||||
fprintf(stderr, "\n\
|
fprintf(stderr, "\
|
||||||
Usage: pg_oid2name [-d database [-x] ] [-t table | -o oid]\n\
|
Usage: pg_oid2name [-d database [-x] ] [-t table | -o oid]\n\
|
||||||
default action display all databases\n\
|
default action display all databases\n\
|
||||||
-d database database to oid2name\n\
|
-d database database to oid2name\n\
|
||||||
-x display system tables\n\
|
-x display system tables\n\
|
||||||
-t table | -o oid search for table name (-t) or\n\
|
-t table | -o oid search for table name (-t) or\n\
|
||||||
oid (-o) in -d database\n\
|
oid (-o) in -d database\n\
|
||||||
|
-q quiet\n\
|
||||||
-H host connect to remote host\n\
|
-H host connect to remote host\n\
|
||||||
-p port host port to connect to\n\
|
-p port host port to connect to\n\
|
||||||
-U username username to connect with\n\
|
-U username username to connect with\n\
|
||||||
-P password password for username\n\n\
|
-P password password for username\n\
|
||||||
");
|
");
|
||||||
exit(1);
|
exit(1);
|
||||||
break;
|
break;
|
||||||
@ -401,10 +410,12 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
/* display all the tables in the database */
|
/* display all the tables in the database */
|
||||||
if (my_opts->getdatabase & my_opts->gettable)
|
if (my_opts->getdatabase & my_opts->gettable)
|
||||||
|
{
|
||||||
|
if (!my_opts->quiet)
|
||||||
{
|
{
|
||||||
printf("Oid of table %s from database \"%s\":\n", my_opts->_tbname, my_opts->_dbname);
|
printf("Oid of table %s from database \"%s\":\n", my_opts->_tbname, my_opts->_dbname);
|
||||||
printf("_______________________________\n");
|
printf("---------------------------------\n");
|
||||||
|
}
|
||||||
pgconn = sql_conn(my_opts->_dbname, my_opts);
|
pgconn = sql_conn(my_opts->_dbname, my_opts);
|
||||||
sql_exec_searchtable(pgconn, my_opts->_tbname);
|
sql_exec_searchtable(pgconn, my_opts->_tbname);
|
||||||
PQfinish(pgconn);
|
PQfinish(pgconn);
|
||||||
@ -414,10 +425,12 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
/* search for the tablename of the given OID */
|
/* search for the tablename of the given OID */
|
||||||
if (my_opts->getdatabase & my_opts->getoid)
|
if (my_opts->getdatabase & my_opts->getoid)
|
||||||
|
{
|
||||||
|
if (!my_opts->quiet)
|
||||||
{
|
{
|
||||||
printf("Tablename of oid %i from database \"%s\":\n", my_opts->_oid, my_opts->_dbname);
|
printf("Tablename of oid %i from database \"%s\":\n", my_opts->_oid, my_opts->_dbname);
|
||||||
printf("---------------------------------\n");
|
printf("---------------------------------\n");
|
||||||
|
}
|
||||||
pgconn = sql_conn(my_opts->_dbname, my_opts);
|
pgconn = sql_conn(my_opts->_dbname, my_opts);
|
||||||
sql_exec_searchoid(pgconn, my_opts->_oid);
|
sql_exec_searchoid(pgconn, my_opts->_oid);
|
||||||
PQfinish(pgconn);
|
PQfinish(pgconn);
|
||||||
@ -427,10 +440,12 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
/* search for the oid for the given tablename */
|
/* search for the oid for the given tablename */
|
||||||
if (my_opts->getdatabase)
|
if (my_opts->getdatabase)
|
||||||
|
{
|
||||||
|
if (!my_opts->quiet)
|
||||||
{
|
{
|
||||||
printf("All tables from database \"%s\":\n", my_opts->_dbname);
|
printf("All tables from database \"%s\":\n", my_opts->_dbname);
|
||||||
printf("---------------------------------\n");
|
printf("---------------------------------\n");
|
||||||
|
}
|
||||||
pgconn = sql_conn(my_opts->_dbname, my_opts);
|
pgconn = sql_conn(my_opts->_dbname, my_opts);
|
||||||
sql_exec_dumptable(pgconn, my_opts->systables);
|
sql_exec_dumptable(pgconn, my_opts->systables);
|
||||||
PQfinish(pgconn);
|
PQfinish(pgconn);
|
||||||
@ -439,9 +454,11 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* display all the databases for the server we are connected to.. */
|
/* display all the databases for the server we are connected to.. */
|
||||||
|
if (!my_opts->quiet)
|
||||||
|
{
|
||||||
printf("All databases:\n");
|
printf("All databases:\n");
|
||||||
printf("---------------------------------\n");
|
printf("---------------------------------\n");
|
||||||
|
}
|
||||||
pgconn = sql_conn("template1", my_opts);
|
pgconn = sql_conn("template1", my_opts);
|
||||||
sql_exec_dumpdb(pgconn);
|
sql_exec_dumpdb(pgconn);
|
||||||
PQfinish(pgconn);
|
PQfinish(pgconn);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user