Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0428840

Browse files
committed
Here is a patch bringing oid2name into the 21st century.
Alvaro Herrera
1 parent a5713ec commit 0428840

File tree

2 files changed

+531
-393
lines changed

2 files changed

+531
-393
lines changed

contrib/oid2name/README.oid2name

+159-105
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,180 @@
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 original
4-
OIDs, stored in pg_class.relfilenode. Oid2name connects to the database
5-
and extracts the OID and table name information.
6-
7-
---------------------------------------------------------------------------
8-
9-
It can be used in four ways:
10-
11-
12-
oid2name
13-
14-
This will connect to the template1 database and display all databases
15-
in the system:
16-
17-
$ oid2name
18-
All databases:
19-
---------------------------------
20-
18720 = test1
21-
1 = template1
22-
18719 = template0
23-
18721 = test
24-
18735 = postgres
25-
18736 = cssi
26-
27-
28-
oid2name -d test [-x]
29-
30-
This connects to the database test and shows all tables and their OIDs:
31-
32-
$ oid2name -d test
33-
All tables from database "test":
34-
---------------------------------
35-
18766 = dns
36-
18737 = ips
37-
18722 = testdate
38-
39-
40-
oid2name -d test -o 18737
41-
oid2name -d test -t testdate
42-
43-
This will connect to the database test and display the table name for oid
44-
18737 and the oid for table name testdate respectively:
45-
46-
$ oid2name -d test -o 18737
47-
Tablename of oid 18737 from database "test":
48-
---------------------------------
49-
18737 = ips
50-
51-
52-
$ oid2name -d test -t testdate
53-
Oid of table testdate from database "test":
54-
---------------------------------
55-
18722 = testdate
56-
57-
Keep in mind tables over one gigabyte will be split into separate files
58-
with numeric file extensions.
1+
This utility allows administrators to examine the file structure used by
2+
PostgreSQL.
3+
4+
Databases are placed in directories named after their OIDs in pg_database,
5+
and the table files within a database's directory are named by "filenode"
6+
numbers, which are stored in pg_class.relfilenode.
7+
8+
Note that while a table's filenode often matches its OID, this is *not*
9+
necessarily the case; some operations, like TRUNCATE, REINDEX, CLUSTER
10+
and some forms of ALTER TABLE, can change the filenode while preserving
11+
the OID. Avoid assuming that filenode and table OID are the same.
12+
13+
When a table exceeds 1Gb, it is divided into gigabyte-sized "segments".
14+
The first segment's file name is the same as the filenode; subsequent
15+
segments are named filenode.1, filenode.2, etc.
16+
17+
Tablespaces make the scenario more complicated. Each non-default
18+
tablespace has a symlink inside the pg_tblspc directory, which points to
19+
the physical tablespace directory (as specified in its CREATE TABLESPACE
20+
command). The symlink is named after the tablespace's OID. Inside the
21+
physical tablespace directory there is another directory for each database
22+
that has elements in the tablespace, named after the database's OID.
23+
Tables within that directory follow the filenode naming scheme. The
24+
"pg_default" tablespace is not addressed via pg_tblspc, but corresponds to
25+
$PGDATA/base.
26+
27+
Oid2name connects to the database and extracts OID, filenode, and table
28+
name information. You can also have it show database OIDs and tablespace
29+
OIDs.
30+
31+
When displaying specific tables, you can select which tables to show by
32+
using -o, -f and -t. The first switch takes an OID, the second takes
33+
a filenode, and the third takes a tablename (actually, it's a LIKE
34+
pattern, so you can use things like "foo%"). Note that you can use as many
35+
of these switches as you like, and the listing will include all objects
36+
matched by any of the switches. Also note that these switches can only
37+
show objects in the database given in -d.
38+
39+
If you don't give any of -o, -f or -t it will dump all the tables in the
40+
database given in -d. If you don't give -d, it will show a database
41+
listing. Alternatively you can give -s to get a tablespace listing.
42+
43+
Additional switches:
44+
-i include indexes and sequences in the database listing.
45+
-x display more information about each object shown:
46+
tablespace name, schema name, OID.
47+
-S also show system objects
48+
(those in information_schema, pg_toast and pg_catalog schemas)
49+
-q don't display headers
50+
(useful for scripting)
5951

6052
---------------------------------------------------------------------------
6153

6254
Sample session:
6355

64-
$ cd /u/pg/data/base
6556
$ oid2name
6657
All databases:
67-
---------------------------------
68-
16817 = test2
69-
16578 = x
70-
16756 = test
71-
1 = template1
72-
16569 = template0
73-
16818 = test3
74-
16811 = floattest
75-
76-
$ cd 16756
77-
$ ls 1873*
78-
18730 18731 18732 18735 18736 18737 18738 18739
79-
80-
$ oid2name -d test -o 18737
81-
Tablename of oid 18737 from database "test":
82-
---------------------------------
83-
18737 = ips
84-
85-
$ oid2name -d test -t ips
86-
Oid of table ips from database "test":
87-
---------------------------------
88-
18737 = ips
58+
Oid Database Name Tablespace
59+
----------------------------------
60+
17228 alvherre pg_default
61+
17255 regression pg_default
62+
17227 template0 pg_default
63+
1 template1 pg_default
64+
65+
$ oid2name -s
66+
All tablespaces:
67+
Oid Tablespace Name
68+
-------------------------
69+
1663 pg_default
70+
1664 pg_global
71+
155151 fastdisk
72+
155152 bigdisk
73+
74+
$ cd $PGDATA/17228
75+
76+
$ # get top 10 db objects in the default tablespace, ordered by size
77+
$ ls -lS * | head -10
78+
-rw------- 1 alvherre alvherre 136536064 sep 14 09:51 155173
79+
-rw------- 1 alvherre alvherre 17965056 sep 14 09:51 1155291
80+
-rw------- 1 alvherre alvherre 1204224 sep 14 09:51 16717
81+
-rw------- 1 alvherre alvherre 581632 sep 6 17:51 1255
82+
-rw------- 1 alvherre alvherre 237568 sep 14 09:50 16674
83+
-rw------- 1 alvherre alvherre 212992 sep 14 09:51 1249
84+
-rw------- 1 alvherre alvherre 204800 sep 14 09:51 16684
85+
-rw------- 1 alvherre alvherre 196608 sep 14 09:50 16700
86+
-rw------- 1 alvherre alvherre 163840 sep 14 09:50 16699
87+
-rw------- 1 alvherre alvherre 122880 sep 6 17:51 16751
88+
89+
$ oid2name -d alvherre -f 155173
90+
From database "alvherre":
91+
Filenode Table Name
92+
----------------------
93+
155173 accounts
94+
95+
$ # you can ask for more than one object
96+
$ oid2name -d alvherre -f 155173 -f 1155291
97+
From database "alvherre":
98+
Filenode Table Name
99+
-------------------------
100+
155173 accounts
101+
1155291 accounts_pkey
102+
103+
$ # you can also mix the options, and have more details
104+
$ oid2name -d alvherre -t accounts -f 1155291 -x
105+
From database "alvherre":
106+
Filenode Table Name Oid Schema Tablespace
107+
------------------------------------------------------
108+
155173 accounts 155173 public pg_default
109+
1155291 accounts_pkey 1155291 public pg_default
89110

90111
$ # show disk space for every db object
91-
$ du * | while read SIZE OID
112+
$ du [0-9]* |
113+
> while read SIZE FILENODE
92114
> do
93-
> echo "$SIZE `oid2name -q -d test -o $OID`"
115+
> echo "$SIZE `oid2name -q -d alvherre -i -f $FILENODE`"
94116
> done
95-
24 18737 = ips
96-
36 18722 = cities
117+
16 1155287 branches_pkey
118+
16 1155289 tellers_pkey
119+
17561 1155291 accounts_pkey
97120
...
98121

99-
$ # same as above, but sort by largest first
100-
$ du * | while read SIZE OID
122+
$ # same, but sort by size
123+
$ du [0-9]* | sort -rn | while read SIZE FN
101124
> do
102-
> echo "$SIZE `oid2name -q -d test -o $OID`"
103-
> done |
104-
> sort -rn
105-
2048 19324 = bigtable
106-
1950 23903 = customers
125+
> echo "$SIZE `oid2name -q -d alvherre -f $FN`"
126+
> done
127+
133466 155173 accounts
128+
17561 1155291 accounts_pkey
129+
1177 16717 pg_proc_proname_args_nsp_index
107130
...
108131

109-
$ # show disk usage per database
110-
$ cd /u/pg/data/base
111-
$ du -s * |
112-
> while read SIZE OID
113-
> do
114-
> echo "$SIZE `aspg oid2name -q | grep ^$OID' '`"
115-
> done |
116-
> sort -rn
117-
2256 18721 = test
118-
2135 18735 = postgres
119-
..
132+
$ # If you want to see what's in tablespaces, use the pg_tblspc directory
133+
$ cd $PGDATA/pg_tblspc
134+
$ oid2name -s
135+
All tablespaces:
136+
Oid Tablespace Name
137+
-------------------------
138+
1663 pg_default
139+
1664 pg_global
140+
155151 fastdisk
141+
155152 bigdisk
142+
143+
$ # what databases have objects in tablespace "fastdisk"?
144+
$ ls -d 155151/*
145+
155151/17228/ 155151/PG_VERSION
146+
147+
$ # Oh, what was database 17228 again?
148+
$ oid2name
149+
All databases:
150+
Oid Database Name Tablespace
151+
----------------------------------
152+
17228 alvherre pg_default
153+
17255 regression pg_default
154+
17227 template0 pg_default
155+
1 template1 pg_default
156+
157+
$ # Let's see what objects does this database have in the tablespace.
158+
$ cd 155151/17228
159+
$ ls -l
160+
total 0
161+
-rw------- 1 postgres postgres 0 sep 13 23:20 155156
162+
163+
$ # OK, this is a pretty small table ... but which one is it?
164+
$ oid2name -d alvherre -f 155156
165+
From database "alvherre":
166+
Filenode Table Name
167+
----------------------
168+
155156 foo
169+
170+
$ # end of sample session.
171+
172+
---------------------------------------------------------------------------
120173

121-
This can be done in psql with:
174+
You can also get approximate size data for each object using psql. For
175+
example,
122176

123-
test=> SELECT relpages, relfilenode, relname FROM pg_class ORDER BY relpages DESC;
177+
SELECT relpages, relfilenode, relname FROM pg_class ORDER BY relpages DESC;
124178

125179
Each page is typically 8k. Relpages is updated by VACUUM.
126180

0 commit comments

Comments
 (0)