受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发版本: devel

oid2name

oid2name — 解析 PostgreSQL 数据目录中的 OID 和 filenode

Synopsis

oid2name [option...]

描述

oid2name是一个帮助管理员检查 PostgreSQL 所用文件结构的实用程序。要使用它, 你需要熟悉数据库文件结构,这在Chapter 63中有说明。

Note

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_schemapg_toastpg_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来获取表空间列表。

环境

PGHOST
PGPORT
PGUSER

默认连接参数。

与大多数其他PostgreSQL实用程序一样,该工具也使用 libpq支持的环境变量(见Section 32.15)。

环境变量PG_COLOR指定是否在诊断消息中使用颜色。 可能的值为alwaysautonever

注解

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

提交更正

如果您发现文档中有不正确的内容、与您使用特定功能的经验不符或需要进一步说明,请使用此表单来报告文档问题。