PostgreSQL oid2name

2021-09-16 17:39 更新

oid2name — 解析一個PostgreSQL數(shù)據(jù)目錄中的 OID 和文件結點。

大綱

oid2name [option...]

描述

oid2name是一個幫助管理員檢查被 PostgreSQL 使用的文件結構的工具程序。要使用它,你需要熟悉數(shù)據(jù)庫文件結構(見第 68 章)。

注意

名稱oid2name是有歷史原因的,它確實有些誤導性,因為在你使用它的大部分時間里,你實際關心的是表的文件結點編號(在數(shù)據(jù)目錄中是可見的文件名)。請確定你理解表 OID 和表文件結點之間的區(qū)別!

oid2name連接到一個目標數(shù)據(jù)庫并且抽取 OID、文件節(jié)點或者表名信息。你也可以讓它顯示數(shù)據(jù)庫 OID 或表空間 OID。

選項

oid2name接受下列命令行參數(shù):

-f filenode
--filenode=filenode

顯示具有文件結點的表的信息filenode.

-i
--indexes

在列表中包括索引和序列.

-o oid
--oid=oid

顯示具有OID的表的信息 oid.

-q
--quiet

忽略頭部(用于腳本).

-s
--tablespaces

顯示表空間 OIDs.

-S
--system-objects

包括系統(tǒng)對象(位于 information_schema, pg_toastpg_catalog 模式).

-t tablename_pattern
--table=tablename_pattern

顯示匹配的表的信息 tablename_pattern.

-V
--version

打印oid2name版本并退出。

-x
--extended

顯示關于每個對象所展現(xiàn)的更多信息: 表空間名, 模式名,以及 OID。

-?
--help

顯示有關oid2name命令行參數(shù)的幫助并退出。

oid2name也接受下列用于連接參數(shù)的命令行參數(shù):

-d database
--dbname=database

要連接的數(shù)據(jù)庫。

-h host
--host=host

數(shù)據(jù)庫服務器的主機。

-H host

數(shù)據(jù)庫服務器的主機。這個參數(shù)在 PostgreSQL 12中已經不推薦 。

-p port
--port=port

數(shù)據(jù)庫服務器的端口。

-U username
--username=username

用于連接的用戶名。

要顯示特定表,通過使用-o-f-t選擇要顯示哪個表。 -o采用一個 OID, -f采用一個文件節(jié)點, 而-t采用一個表名(實際上,它是一個LIKE模式,因此你可以用諸如 foo%之類的東西)。 這些選項你想用多少就用多少,最后的列舉將包括所有匹配任意一個這些選項的對象。但是注意這些選項只能顯示由-d給定的數(shù)據(jù)庫中的對象。

如果你沒有給出任何-o、-f或者-t,但是給出了-d,它將列出由-d指定的數(shù)據(jù)庫中的所有表。在這種模式下,-S-i選項控制什么會被列出。

如果你也沒有給出-d,它將顯示一個數(shù)據(jù)庫 OID 的列表。你也可以給出-s來得到一個表空間列表。

環(huán)境

PGHOST
PGPORT
PGUSER

默認連接參數(shù)。

與大多數(shù)其他 PostgreSQL的實用程序一樣,這個實用程序也使用libpq 支持的環(huán)境變量 (參見 第 33.14 節(jié))。

環(huán)境變量PG_COLOR指定是否在診斷消息中使用顏色。 可能的值是always、autonever。

注解

oid2name要求一個運行著的數(shù)據(jù)庫服務器并且其系統(tǒng)目錄沒有損壞。因此它對于數(shù)據(jù)庫損壞的情況用處有限。

例子

$ # 在這個數(shù)據(jù)庫服務器中到底有什么?
$ 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,讓我們看看數(shù)據(jù)庫 alvherre 里面
$ cd $PGDATA/base/17228

$ # 得到默認表空間中前十個數(shù)據(jù)庫對象,按尺寸排序
$ 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 得到更多細節(jié)
$ 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

$ # 為每個數(shù)據(jù)庫對象顯示磁盤空間
$ 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

$ # 哪些數(shù)據(jù)庫在表空間 "fastdisk" 中有對象?
$ ls -d 155151/*
155151/17228/  155151/PG_VERSION

$ # 噢,什么是數(shù)據(jù)庫 17228 ?
$ oid2name
All databases:
    Oid  Database Name  Tablespace
----------------------------------
  17228       alvherre  pg_default
  17255     regression  pg_default
  17227      template0  pg_default
      1      template1  pg_default

$ # 讓我們看這個數(shù)據(jù)庫在該表空間中有哪些對象。
$ cd 155151/17228
$ ls -l
total 0
-rw-------  1 postgres postgres 0 sep 13 23:20 155156

$ # OK,這是個很小的表,但是哪一個是它?
$ oid2name -d alvherre -f 155156
From database "alvherre":
  Filenode  Table Name
----------------------
    155156         foo

 


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號