oid2name — 解析 PostgreSQL 数据目录中的 OID 与文件节点
oid2name [option...]
oid2name 是一个帮助管理员查看 PostgreSQL 所用文件结构的实用程序。要使用它,你需要熟悉数据库文件结构,相关内容见 Chapter 66。
名称“oid2name”带有历史色彩,实际上相当容易误导,因为使用它的大多数时候,你真正关心的其实是表的 filenode 编号(也就是数据库目录中可见的文件名)。请务必理解表 OID 与表 filenode 之间的区别!
oid2name 连接到目标数据库并提取 OID、filenode 和/或表名信息。你也可以让它显示数据库 OID 或表空间 OID。
oid2name 接受以下命令行参数:
-f filenode--filenode=filenode显示 filenode 为 filenode 的表的信息。
-i--indexes在列表中包含索引与序列。
-o oid--oid=oid显示 OID 为 oid 的表的信息。
-q--quiet省略标题(便于编写脚本)。
-s--tablespaces显示表空间 OID。
-S--system-objects包含系统对象(information_schema、pg_toast 与 pg_catalog 模式中的对象)。
-t tablename_pattern--table=tablename_pattern显示匹配 tablename_pattern 的表的信息。
-V--version打印 oid2name 版本并退出。
-x--extended为显示的每个对象输出更多信息:表空间名、模式名与 OID。
-?--help显示关于 oid2name 命令行参数的帮助并退出。
oid2name 还接受以下用于连接参数的命令行参数:
-d database--dbname=database要连接的数据库。
-h host--host=host数据库服务器的主机名。
-H host数据库服务器的主机名。自 PostgreSQL 12 起,该参数的使用已被弃用。
-p port--port=port数据库服务器的端口。
-U username--username=username用于连接的用户名。
要显示特定的表,请使用 -o、-f 和/或 -t 选择要显示的表。-o 接受一个 OID,-f 接受一个 filenode,-t 接受一个表名(实际上是一个 LIKE 模式,因此可以使用 foo% 这类写法)。这些选项想用多少个都可以,列表将包含被任意选项匹配的所有对象。但请注意,这些选项只能显示 -d 所给数据库中的对象。
如果你不给出 -o、-f 或 -t 中的任何一个,但给出了 -d,它将列出 -d 所指定数据库中的所有表。在这种模式下,-S 与 -i 选项控制列出哪些内容。
如果你连 -d 也不给,它将显示数据库 OID 的列表。你也可以给出 -s 以获得表空间列表。
PGHOSTPGPORTPGUSER默认连接参数。
与大多数其他 PostgreSQL 实用程序一样,该实用程序也使用 libpq 支持的环境变量(参见 Section 32.15)。
环境变量 PG_COLOR 指定诊断消息是否使用颜色。可取值为 always、auto 与 never。
oid2name 需要一个正在运行且系统目录未损坏的数据库服务器。因此,它对于从灾难性数据库损坏中恢复的用途十分有限。
$ # 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
$ # What file is 155173?
$ 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
B. Palmer <bpalmer@crimelabs.net>