oid2name是一个帮助管理员检查PostgreSQL使用的文件结构的工具程序。要使用 这个工具,你必须熟悉数据库文件结构,这在第 59 章有所描述.
注意: "oid2name"这个名字是由于历史原因而起的,但是实际上有相当的误导性。因为大多数你使用它的时候, 你真正关心的是‘filenode numbers’(它们是数据库目录中那些可见的文件名)。请确保你明白表的OID和表filenodes之间的区别
oid2name 连接到一个目标数据库并且提取OID,文件节点,和/或表名信息。你也可以展示数据库的OID和表空间的OID。
oid2name接收下面的命令行参数:
显示文件节点是 filenode的表的信息
在这个列表中包含索引和序列
显示OID是 oid的表的信息
省略表头(对脚本很有用)
展示表空间的OID
包含系统对象(那些在 information_schema, pg_toast 和 pg_catalog模式中的对象)
显示匹配tablename_pattern的表信息
打印 oid2name 版本并退出.
列出每一个被展示的对象的更多信息: 表空间名, 模式名和OID
显示关于 oid2name命令行参数的帮助信息和参数,并退出
oid2name也接受下面的命令行参数作为连接参数:
连接的目标数据库
数据库服务器的地址
数据库服务器的端口
连接数据库的用户
密码(但是不建议这么做,因为把密码放在命令行存在安全隐患)
要显示特定的表, 选择要显示的数据库使用 -o, -f 和/或 -t。 -o 需要一个 OID, -f 需要一个 filenode, -t 需要一个表名 (事实上,它是一个 LIKE 模式的, 所以你可以使用类似 foo%的参数). 只要你喜欢,你可以使用许多这样的选项,输出列表将包含被其中任何一个选项所匹配的所有对象。但是请注意这些选项只能显示-d参数指定的数据库里的对象。
如果你没有提供-o, -f 或 -t中的任何参数, 但是给了 -d参数, 它将列出以-d指定的数据库中的所有的表。在这种模式下,-S 和 -i选项控制着列表的显示内容.
如果你也没有指定 -d 它将展示数据库列表的 OID. 或者你可以通过-s 参数去得到表空间的列表
$ # 数据库服务器上都有哪些数据库?
$ 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
$ # 好,让我们进入alvherre数据库的目录
$ cd $PGDATA/base/17228
$ # 获取缺省表空间的前十个数据库对象,并且以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
$ #我想知道155173这个文件是什么
$ oid2name -d alvherre -f 155173
From database "alvherre":
Filenode Table Name
----------------------
155173 accounts
$ # 你也可以请求更多的对象
$ oid2name -d alvherre -f 155173 -f 1155291
From database "alvherre":
Filenode Table Name
-------------------------
155173 accounts
1155291 accounts_pkey
$ #你可以混合这些选项,通过-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
$ # 显示每个数据库对象所占的磁盘空间
$ 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
...
$ # 和上面一样,但是按照大小排序
$ 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
...
$ # 如果你想看表空间里有什么,使用 pg_tblspc 目录
$ cd $PGDATA/pg_tblspc
$ oid2name -s
All tablespaces:
Oid Tablespace Name
-------------------------
1663 pg_default
1664 pg_global
155151 fastdisk
155152 bigdisk
$ #数据库在 "fastdisk"表空间都有什么?
$ ls -d 155151/*
155151/17228/ 155151/PG_VERSION
$ # 这个数据库 17228 又是什么?
$ oid2name
All databases:
Oid Database Name Tablespace
----------------------------------
17228 alvherre pg_default
17255 regression pg_default
17227 template0 pg_default
1 template1 pg_default
$ # 让我们看看这个数据库在表空间里都有什么
$ cd 155151/17228
$ ls -l
total 0
-rw------- 1 postgres postgres 0 sep 13 23:20 155156
$ # 这个是一个很小的表,但是它是什么表呢?
$ oid2name -d alvherre -f 155156
From database "alvherre":
Filenode Table Name
----------------------
155156 foo