On this page
Note
oid2name
oid2name —解析 PostgreSQL 数据目录中的 OID 和文件节点
Synopsis
oid2name
[ option
...]
Description
oid2name 是一个 Util,可以帮助 Management 员检查 PostgreSQL 使用的文件结构。要使用它,您需要熟悉Chapter 68中描述的数据库文件结构。
Note
名称“ oid2name”是历史名称,实际上是相当误导的,因为在大多数情况下,使用 oid2name 时,您实际上会担心表的文件节点号(在数据库目录中可见的文件名)。确保您了解表 OID 与表文件节点之间的区别!
oid2name 连接到目标数据库,并提取 OID,文件节点和/或表名信息。您也可以显示数据库 OID 或表空间 OID。
Options
oid2name 接受以下命令行参数:
-f
filenode
- 显示带有 filenode *
filenode
*的表的信息
- 显示带有 filenode *
-i
- 在清单中包括索引和序列
-o
oid
- 显示具有 OID *
oid
*的表的信息
- 显示具有 OID *
-q
- 省略标题(用于脚本)
-s
- 显示表空间 OID
-S
- 包括系统对象(在
information_schema
,pg_toast
和pg_catalog
模式中的对象)
- 包括系统对象(在
-t
tablename_pattern
- 显示匹配表的信息*
tablename_pattern
*
- 显示匹配表的信息*
-V
--version
- 打印 oid2name 版本并退出。
-x
- 显示有关显示的每个对象的更多信息:表空间名称,模式名称和 OID
-?
--help
- 显示有关 oid2name 命令行参数的帮助,然后退出。
oid2name 还接受以下命令行参数作为连接参数:
-d
database
- 要连接的数据库
-H
host
- 数据库服务器的主机
-p
port
- 数据库服务器的端口
-U
username
- 连接的用户名
-P
password
- 密码(不建议使用,将其放在命令行上是安全隐患)
要显示特定表,请使用-o
,-f
和/或-t
选择要显示的表。 -o
具有 OID,-f
具有文件节点,-t
具有表名(实际上,它是LIKE
模式,因此可以使用foo%
之类的东西)。您可以根据需要使用任意多个这些选项,并且清单中将包含所有与任何选项匹配的对象。但是请注意,这些选项只能显示-d
给定的数据库中的对象。
如果您不给出-o
,-f
或-t
中的任何一个,但是给出-d
,它将列出数据库中以-d
命名的所有表。在此模式下,-S
和-i
选项控制列出的内容。
如果您也不给出-d
,它将显示数据库 OID 的列表。或者,您可以给-s
以获取表空间列表。
Notes
oid2name 要求运行的数据库服务器具有不损坏的系统目录。因此,从灾难性数据库损坏情况中恢复时,它仅具有有限的用途。
Examples
$ # 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
Author
B.帕默<bpalmer@crimelabs.net>