合 PG之oid2name命令
简介
oid2name — 解析一个PostgreSQL数据目录中的 OID 和文件结点。
我们还有一个实用的客户端命令:oid2name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [03:46:41-postgres@sean-rh1:/var/lib/pgsql]$ oid2name --help oid2name helps examining the file structure used by PostgreSQL. Usage: oid2name [OPTION]... Options: -f, --filenode=FILENODE show info for table with given file node -i, --indexes show indexes and sequences too -o, --oid=OID show info for table with given OID -q, --quiet quiet (don't show headers) -s, --tablespaces show all tablespaces -S, --system-objects show system objects too -t, --table=TABLE show info for named table -V, --version output version information, then exit -x, --extended extended (show additional columns) -?, --help show this help, then exit Connection options: -d, --dbname=DBNAME database to connect to -h, --host=HOSTNAME database server host or socket directory -H same as -h, deprecated option -p, --port=PORT database server port number -U, --username=USERNAME connect as specified database user |
它的目的是用于帮助检查文件结构,它可以用于实现将常用的oid值到它对应的对象的名字之间的转化。支持的是哪些类型:
- 索引
- 表
- 表空间
- 系统对象
- 扩展信息
1、得到 filenode与oid的信息
看看下边的例子:
1 2 3 4 5 6 7 8 9 10 | [03:49:28-postgres@sean-rh1:/var/lib/pgsql]$ oid2name -f 2657 -x From database "postgres": Filenode Table Name Oid Schema Tablespace -------------------------------------------------------------- 2657 pg_attrdef_oid_index 2657 pg_catalog pg_default [03:53:49-postgres@sean-rh1:/var/lib/pgsql]$ oid2name -o 2657 -x From database "postgres": Filenode Table Name Oid Schema Tablespace -------------------------------------------------------------- 2657 pg_attrdef_oid_index 2657 pg_catalog pg_default |
这里-o 与 -f的结果是一样的。因为文件的relnodeid与oid是相同的。加上-x,会输出相关的扩展列,如schema, tablespace信息。
2、filenodeid与oid默认是一致的
未执行truncate, vacuum full操作时, 两者的值是一致的。
1 2 3 4 5 6 7 8 9 | mydb=# \! oid2name -t t From database "postgres": Filenode Table Name ---------------------- mydb=# \! oid2name -t t -d mydb From database "mydb": Filenode Table Name ---------------------- 16496 t |
t表原始的filenode是16496. 我们扩展一下:
1 2 3 4 5 | mydb=# \! oid2name -t t -d mydb -x From database "mydb": Filenode Table Name Oid Schema Tablespace ------------------------------------------------- 16496 t 16496 public pg_default |
也能看到oid值为16496。指定了database name: mydb是关键。否则找不到。
3、filenode与oid不一致的情形
执行vaccum full
1 2 3 4 5 6 7 | mydb=# vacuum full t; VACUUM mydb=# \! oid2name -t t -d mydb -x From database "mydb": Filenode Table Name Oid Schema Tablespace ------------------------------------------------- 16509 t 16496 public pg_default |
filenode从16496变到16509. Oid保持不变。
执行truncate
1 2 3 4 5 6 7 | mydb=# truncate t; TRUNCATE TABLE mydb=# \! oid2name -t t -d mydb -x From database "mydb": Filenode Table Name Oid Schema Tablespace ------------------------------------------------- 16513 t 16496 public pg_default |
Filenode又从16509变到16513
执行cluster
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | mydb=# \d t Table "public.t" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- id | integer | | not null | col2 | character varying(64) | | | Indexes: "t_pkey" PRIMARY KEY, btree (id) mydb=# cluster t using t_pkey; CLUSTER mydb=# \! oid2name -t t -d mydb -x From database "mydb": Filenode Table Name Oid Schema Tablespace ------------------------------------------------- 16516 t 16496 public pg_default |