oid2nameは、管理者がPostgreSQLで使用されるファイル構造を確認することを補助するユーティリティプログラムです。 使用できるようにするためには、第53章で説明されるデータベースファイル構造についての知識が必要です。
注意: "oid2name"という名前は歴史的なものであり、これを使用する場合のほとんどでは、本当はテーブルのファイルノード番号(これはデータベースディレクトリ内で可視なファイル名)が関係しますので、実際誤解されやすいものです。 テーブルのOIDとテーブルファイルノードの違いを確実に理解してください。
oid2nameは対象データベースに接続し、OID、ファイルノード、テーブル名情報を抽出します。 また、データベースOIDまたはテーブル空間OIDを示すようにさせることもできます。 このプログラムは、表F-13に示す多数のコマンドラインスイッチにより制御されます。
表 F-13. oid2nameのスイッチ
| スイッチ | 説明 |
|---|---|
| -o oid | oidというOIDを持つテーブルの情報を表示します。 |
| -f filenode | filenodeというファイルノードを持つテーブルの情報を表示します。 |
| -t tablename_pattern | tablename_patternに一致するテーブル(複数可)の情報を表示します。 |
| -s | テーブル空間OIDを表示します。 |
| -S | システムオブジェクト(information_schema、pg_toast、pg_catalogスキーマ内に存在するもの)を含めます。 |
| -i | 一覧にインデックスおよびシーケンスを含めます。 |
| -x | 表示対象の各オブジェクトに関してさらに情報を表示します。テーブル空間名、スキーマ名、OID。 |
| -q | ヘッダを省略します。(スクリプト処理に適しています) |
| -d database | 接続データベース |
| -H host | データベースサーバのホスト |
| -p port | データベースサーバのポート |
| -U username | 接続ユーザ名 |
特定のテーブルを表示するために、-o、-f、-tを使用して表示するテーブルを選択してください。 -oはOIDを、-fはファイルノードを、-tはテーブル名(実際はLIKEパターンです。ですのでfoo%などが使用できます)を引数として取ります。 これらのスイッチを必要なだけ使用することができます。 一覧には、スイッチのいずれかで一致したオブジェクトがすべて含まれます。 しかしこれらのオプションでは、-dで指定したデータベース内に存在するオブジェクトしか表示しないことに注意してください。
-o、-f、-tのいずれも指定せずに-dを指定した場合、-dで指定したデータベース上のすべてのテーブルを列挙します。 -dを指定しない場合、データベースの一覧を示します。 このモードでは、-Sおよび-iスイッチが何を列挙するかを制御します。
-dも指定しなかった場合、データベースOIDの一覧を示します。 他にも-sを指定してテーブル空間の一覧を得ることもできます。
$ # what's in this database server, anyway?
$ oid2name
All databases:
Oid Database Name Tablespace
----------------------------------
17228 alvherre pg_default
17255 regression pg_default
17227 template0 pg_default
1 template1 pg_default
$ oid2name -s
All tablespaces:
Oid Tablespace Name
-------------------------
1663 pg_default
1664 pg_global
155151 fastdisk
155152 bigdisk
$ # OK, let's look into database alvherre
$ cd $PGDATA/base/17228
$ # get top 10 db objects in the default tablespace, ordered by size
$ ls -lS * | head -10
-rw------- 1 alvherre alvherre 136536064 sep 14 09:51 155173
-rw------- 1 alvherre alvherre 17965056 sep 14 09:51 1155291
-rw------- 1 alvherre alvherre 1204224 sep 14 09:51 16717
-rw------- 1 alvherre alvherre 581632 sep 6 17:51 1255
-rw------- 1 alvherre alvherre 237568 sep 14 09:50 16674
-rw------- 1 alvherre alvherre 212992 sep 14 09:51 1249
-rw------- 1 alvherre alvherre 204800 sep 14 09:51 16684
-rw------- 1 alvherre alvherre 196608 sep 14 09:50 16700
-rw------- 1 alvherre alvherre 163840 sep 14 09:50 16699
-rw------- 1 alvherre alvherre 122880 sep 6 17:51 16751
$ # I wonder what file 155173 is ...
$ oid2name -d alvherre -f 155173
From database "alvherre":
Filenode Table Name
----------------------
155173 accounts
$ # you can ask for more than one object
$ oid2name -d alvherre -f 155173 -f 1155291
From database "alvherre":
Filenode Table Name
-------------------------
155173 accounts
1155291 accounts_pkey
$ # you can mix the options, and get more details with -x
$ oid2name -d alvherre -t accounts -f 1155291 -x
From database "alvherre":
Filenode Table Name Oid Schema Tablespace
------------------------------------------------------
155173 accounts 155173 public pg_default
1155291 accounts_pkey 1155291 public pg_default
$ # show disk space for every db object
$ du [0-9]* |
> while read SIZE FILENODE
> do
> echo "$SIZE `oid2name -q -d alvherre -i -f $FILENODE`"
> done
16 1155287 branches_pkey
16 1155289 tellers_pkey
17561 1155291 accounts_pkey
...
$ # same, but sort by size
$ du [0-9]* | sort -rn | while read SIZE FN
> do
> echo "$SIZE `oid2name -q -d alvherre -f $FN`"
> done
133466 155173 accounts
17561 1155291 accounts_pkey
1177 16717 pg_proc_proname_args_nsp_index
...
$ # If you want to see what's in tablespaces, use the pg_tblspc directory
$ cd $PGDATA/pg_tblspc
$ oid2name -s
All tablespaces:
Oid Tablespace Name
-------------------------
1663 pg_default
1664 pg_global
155151 fastdisk
155152 bigdisk
$ # what databases have objects in tablespace "fastdisk"?
$ ls -d 155151/*
155151/17228/ 155151/PG_VERSION
$ # Oh, what was database 17228 again?
$ oid2name
All databases:
Oid Database Name Tablespace
----------------------------------
17228 alvherre pg_default
17255 regression pg_default
17227 template0 pg_default
1 template1 pg_default
$ # Let's see what objects does this database have in the tablespace.
$ cd 155151/17228
$ ls -l
total 0
-rw------- 1 postgres postgres 0 sep 13 23:20 155156
$ # OK, this is a pretty small table ... but which one is it?
$ oid2name -d alvherre -f 155156
From database "alvherre":
Filenode Table Name
----------------------
155156 foo
oid2nameは破損のないシステムカタログで実行中のデータベースサーバが必要です。 したがって、破滅的にデータベースが破損したような状況からの復旧での使用には制限されています。
B. Palmer <bpalmer@crimelabs.net>