Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a8f555

Browse files
committed
Add -q option to oid2name. Add sample session to README.
1 parent0f865e1 commit5a8f555

File tree

2 files changed

+111
-55
lines changed

2 files changed

+111
-55
lines changed

‎contrib/oid2name/README.oid2name

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
With version 7.1 of PostgreSQL server, the old naming scheme for
2-
databases and tables (in $PGDATA/base) has changed. The databases
3-
are put in folders for their OID in pg_database and the tables in
4-
that folder are named for their OIDs in pg_class. This app connects
5-
to the database (you can specify host, port, user, pass etc to
6-
connect to a host other than localhost) and extracts the OID and
7-
table name information. It has 4 ways it can be run:
1+
This utility allows administrators to view the file structure used by
2+
PostgreSQL. Databases are placed in directories based on their OIDs in
3+
pg_database, and the tables in that directory are named by their OIDs,
4+
stored in pg_class.relfilenode. Oid2name connects to the database and
5+
extracts the OID and table name information.
86

9-
pg_oid2name
7+
---------------------------------------------------------------------------
8+
9+
It can be used in four ways:
10+
11+
12+
oid2name
1013

1114
This will connect to the template1 database and display all databases
12-
in the system.
15+
in the system:
1316

14-
$./pg_oid2name
17+
$oid2name
1518
All databases:
1619
---------------------------------
1720
18720 = test1
@@ -21,52 +24,88 @@ table name information. It has 4 ways it can be run:
2124
18735 = postgres
2225
18736 = cssi
2326

24-
pg_oid2name -d test [-x]
2527

26-
This connects to the databasetestand shows all tables and their OIDs.
28+
oid2name -dtest[-x]
2729

28-
$ ./pg_oid2name -d test
30+
This connects to the database test and shows all tables and their OIDs:
31+
32+
$ oid2name -d test
2933
All tables from database "test":
3034
---------------------------------
3135
18766 = dns
3236
18737 = ips
3337
18722 = testdate
3438

35-
pg_oid2name -d test -o 18737 or
36-
pg_oid2name -d test -t testdate
39+
40+
oid2name -d test -o 18737
41+
oid2name -d test -t testdate
3742

3843
This will connect to the database test and display the table name for oid
39-
18737 and the oid for table name testdaterespectivly.
44+
18737 and the oid for table name testdaterespectively:
4045

41-
$./pg_oid2name -d test -o 18737
46+
$oid2name -d test -o 18737
4247
Tablename of oid 18737 from database "test":
4348
---------------------------------
4449
18737 = ips
4550

4651

47-
$./pg_oid2name -d test -t testdate
52+
$oid2name -d test -t testdate
4853
Oid of table testdate from database "test":
49-
_______________________________
54+
---------------------------------
5055
18722 = testdate
5156

57+
---------------------------------------------------------------------------
58+
59+
Sample session:
60+
61+
$ cd /u/pg/data/base
62+
$ oid2name
63+
All databases:
64+
---------------------------------
65+
16817 = test2
66+
16578 = x
67+
16756 = test
68+
1 = template1
69+
16569 = template0
70+
16818 = test3
71+
16811 = floattest
72+
73+
$ cd 16756
74+
$ ls 1873*
75+
18730 18731 18732 18735 18736 18737 18738 18739
76+
77+
$ oid2name -d test -o 18737
78+
Tablename of oid 18737 from database "test":
79+
---------------------------------
80+
18737 = ips
81+
82+
$ oid2name -d test -t ips
83+
Oid of table ips from database "test":
84+
---------------------------------
85+
18737 = ips
86+
87+
$ du * | while read SIZE OID
88+
> do
89+
>echo "$SIZE `oid2name -q -d test -o $OID`"
90+
> done
91+
24 18737 = ips
92+
36 18722 = cities
93+
...
94+
95+
$ du * | while read SIZE OID
96+
> do
97+
>echo "$SIZE `oid2name -q -d test -o $OID`"
98+
> done |
99+
> sort -rn
100+
2048 19324 = bigtable
101+
1950 23903 = customers
102+
...
103+
104+
105+
---------------------------------------------------------------------------
52106

53-
There should be a decent amount of error handling in the app, a lot of it
54-
dealt with via the postgres function calls.
55-
56-
$ ./pg_oid2name -d nothere -t testdate
57-
Oid of table testdate from database "nothere":
58-
_______________________________
59-
Connection to database 'nothere' failed.
60-
FATAL 1: Database "nothere" does not exist in the system catalog.
61-
62-
$ ./pg_oid2name -d test -t nothere
63-
Oid of table nothere from database "test":
64-
_______________________________
65-
No tables with that name found
66-
67-
68107
Mail me with any problems or additions you would like to see. Clearing
69-
house for the code will be at: http://www.crimelabs.net
108+
house for the code will be at: http://www.crimelabs.net
70109

71110
b. palmer, bpalmer@crimelabs.net
72111

‎contrib/oid2name/oid2name.c

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ struct options
2121
intgettable;
2222
intgetoid;
2323

24+
intquiet;
25+
2426
intsystables;
2527

2628
intremotehost;
@@ -59,6 +61,8 @@ get_opts(int argc, char **argv, struct options * my_opts)
5961
my_opts->gettable=0;
6062
my_opts->getoid=0;
6163

64+
my_opts->quiet=0;
65+
6266
my_opts->systables=0;
6367

6468
my_opts->remotehost=0;
@@ -67,7 +71,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
6771
my_opts->remotepass=0;
6872

6973
/* get opts */
70-
while ((c=getopt(argc,argv,"H:p:U:P:d:t:o:xh?"))!=-1)
74+
while ((c=getopt(argc,argv,"H:p:U:P:d:t:o:qxh?"))!=-1)
7175
{
7276
switch (c)
7377
{
@@ -82,13 +86,13 @@ get_opts(int argc, char **argv, struct options * my_opts)
8286
/* make sure we set the database first */
8387
if (!my_opts->getdatabase)
8488
{
85-
fprintf(stderr,"Sorry, but you must specify a database to dump from.\n");
89+
fprintf(stderr,"You must specify a database to dump from.\n");
8690
exit(1);
8791
}
8892
/* make sure we don't try to do a -o also */
8993
if (my_opts->getoid)
9094
{
91-
fprintf(stderr,"Sorry, you can only specify either oid or table\n");
95+
fprintf(stderr,"You can only specify either oid or table\n");
9296
exit(1);
9397
}
9498

@@ -102,13 +106,13 @@ get_opts(int argc, char **argv, struct options * my_opts)
102106
/* make sure we set the database first */
103107
if (!my_opts->getdatabase)
104108
{
105-
fprintf(stderr,"Sorry, but you must specify a database to dump from.\n");
109+
fprintf(stderr,"You must specify a database to dump from.\n");
106110
exit(1);
107111
}
108112
/* make sure we don't try to do a -t also */
109113
if (my_opts->gettable)
110114
{
111-
fprintf(stderr,"Sorry, you can only specify either oid or table\n");
115+
fprintf(stderr,"You can only specify either oid or table\n");
112116
exit(1);
113117
}
114118

@@ -117,6 +121,10 @@ get_opts(int argc, char **argv, struct options * my_opts)
117121

118122
break;
119123

124+
case'q':
125+
my_opts->quiet=1;
126+
break;
127+
120128
/* host to connect to */
121129
case'H':
122130
my_opts->remotehost=1;
@@ -149,17 +157,18 @@ get_opts(int argc, char **argv, struct options * my_opts)
149157
/* help! (ugly in code for easier editing) */
150158
case'?':
151159
case'h':
152-
fprintf(stderr,"\n\
153-
Usage: pg_oid2name [-d database [-x] ] [-t table | -o oid]\n\
160+
fprintf(stderr,"\
161+
Usage: pg_oid2name [-d database [-x] ] [-t table | -o oid]\n\
154162
default action display all databases\n\
155163
-d database database to oid2name\n\
156164
-x display system tables\n\
157165
-t table | -o oid search for table name (-t) or\n\
158166
oid (-o) in -d database\n\
167+
-q quiet\n\
159168
-H host connect to remote host\n\
160169
-p port host port to connect to\n\
161170
-U username username to connect with\n\
162-
-P password password for username\n\n\
171+
-P password password for username\n\
163172
");
164173
exit(1);
165174
break;
@@ -402,9 +411,11 @@ main(int argc, char **argv)
402411
/* display all the tables in the database */
403412
if (my_opts->getdatabase&my_opts->gettable)
404413
{
405-
printf("Oid of table %s from database \"%s\":\n",my_opts->_tbname,my_opts->_dbname);
406-
printf("_______________________________\n");
407-
414+
if (!my_opts->quiet)
415+
{
416+
printf("Oid of table %s from database \"%s\":\n",my_opts->_tbname,my_opts->_dbname);
417+
printf("---------------------------------\n");
418+
}
408419
pgconn=sql_conn(my_opts->_dbname,my_opts);
409420
sql_exec_searchtable(pgconn,my_opts->_tbname);
410421
PQfinish(pgconn);
@@ -415,9 +426,11 @@ main(int argc, char **argv)
415426
/* search for the tablename of the given OID */
416427
if (my_opts->getdatabase&my_opts->getoid)
417428
{
418-
printf("Tablename of oid %i from database \"%s\":\n",my_opts->_oid,my_opts->_dbname);
419-
printf("---------------------------------\n");
420-
429+
if (!my_opts->quiet)
430+
{
431+
printf("Tablename of oid %i from database \"%s\":\n",my_opts->_oid,my_opts->_dbname);
432+
printf("---------------------------------\n");
433+
}
421434
pgconn=sql_conn(my_opts->_dbname,my_opts);
422435
sql_exec_searchoid(pgconn,my_opts->_oid);
423436
PQfinish(pgconn);
@@ -428,9 +441,11 @@ main(int argc, char **argv)
428441
/* search for the oid for the given tablename */
429442
if (my_opts->getdatabase)
430443
{
431-
printf("All tables from database \"%s\":\n",my_opts->_dbname);
432-
printf("---------------------------------\n");
433-
444+
if (!my_opts->quiet)
445+
{
446+
printf("All tables from database \"%s\":\n",my_opts->_dbname);
447+
printf("---------------------------------\n");
448+
}
434449
pgconn=sql_conn(my_opts->_dbname,my_opts);
435450
sql_exec_dumptable(pgconn,my_opts->systables);
436451
PQfinish(pgconn);
@@ -439,9 +454,11 @@ main(int argc, char **argv)
439454
}
440455

441456
/* display all the databases for the server we are connected to.. */
442-
printf("All databases:\n");
443-
printf("---------------------------------\n");
444-
457+
if (!my_opts->quiet)
458+
{
459+
printf("All databases:\n");
460+
printf("---------------------------------\n");
461+
}
445462
pgconn=sql_conn("template1",my_opts);
446463
sql_exec_dumpdb(pgconn);
447464
PQfinish(pgconn);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp