系統(tǒng)表, 系統(tǒng)表之間基本上都是以oid關(guān)聯(lián). 例如pg_attrdef.adrelid 關(guān)聯(lián) pg_class.oid
- 先介紹下oid的使用:
以系統(tǒng)表 pg_class為例,查看下postgres里各個(gè)對象(表、序列、索引 等)的oid
pg_class 存儲的都是這些對象的信息
postgres=# \d pg_class // 列出pg_class表的所有字段。
Table "pg_catalog.pg_class"
Column | Type | Modifiers
---------------------+-----------+-----------
relname | name | not null
relnamespace | oid | not null
reltype | oid | not null
reloftype | oid | not null
relowner | oid | not null
relam | oid | not null
relfilenode | oid | not null
reltablespace | oid | not null
relpages | integer | not null
reltuples | real | not null
relallvisible | integer | not null
reltoastrelid | oid | not null
relhasindex | boolean | not null
relisshared | boolean | not null
relpersistence | "char" | not null
relkind | "char" | not null
relnatts | smallint | not null
relchecks | smallint | not null
relhasoids | boolean | not null
relhaspkey | boolean | not null
relhasrules | boolean | not null
relhastriggers | boolean | not null
relhassubclass | boolean | not null
relrowsecurity | boolean | not null
relforcerowsecurity | boolean | not null
relispopulated | boolean | not null
relreplident | "char" | not null
relfrozenxid | xid | not null
relminmxid | xid | not null
relacl | aclitem[] |
reloptions | text[] |
Indexes:
"pg_class_oid_index" UNIQUE, btree (oid)
"pg_class_relname_nsp_index" UNIQUE, btree (relname, relnamespace)
"pg_class_tblspc_relfilenode_index" btree (reltablespace, relfilenode)
//有沒有覺得奇怪? 明明沒有oid這個(gè)字段,但是你執(zhí)行下面語句卻有結(jié)果,這是為什么呢 ?
postgres=# select oid from pg_class limit 5 ;
oid
-------
2671
2672
16399
16405
16407
(5 rows)
同時(shí)在 \d pg_class 命令下還有這個(gè)描述
** "pg_class_oid_index" UNIQUE, btree (oid)**
oid 上有 btree索引,并且有唯一約束 pg_class_oid_index 。
也證明了存在oid
那oid在哪兒?到底為什么會出現(xiàn)這種情況 ?
來看看postgres官網(wǎng)對 oid的介紹:
1.Object identifiers (OIDs) are used internally by PostgreSQL as primary keys for various system tables.
這里表明了 oid 是內(nèi)部使用,并作為系統(tǒng)表的主鍵。
2.OIDs are not added to user-created tables, unless WITH OIDS is specified when the table is created, or the default_with_oids configuration variable is enabled.
oid不會添加到 用戶自己創(chuàng)建的表里,除非明確指定 WITH OIDS 或者 default_with_oids 打開。
3.Type oid represents an object identifier. There are also several alias types for oid: regproc, regprocedure, regoper, regoperator, regclass, regtype, regrole, regnamespace, regconfig, and regdictionary. Table 8-24 shows an overview.
oid代表著object identifier (對象標(biāo)識號). oid 還有一些別名: regproc, regprocedure, regoper, regoperator, regclass, regtype, regrole, regnamespace, regconfig, and regdictionary
4.The oid type is currently implemented as an unsigned four-byte integer. Therefore, it is not large enough to provide database-wide uniqueness in large databases, or even in large individual tables. So, using a user-created table's OID column as a primary key is discouraged. OIDs are best used only for references to system tables.
oid類型是unsigned four-byte integer,因此還不是足夠大,不建議用作用戶表的主鍵,最佳用處是作為系統(tǒng)表的主鍵。
根據(jù)stackoverflow的高票用戶的回答:
*OIDs basically give you a built-in, globally unique id for every row, contained in a system column (as opposed to a user-space column). That's handy for tables where you don't have a primary key, have duplicate rows, etc. For example, if you have a table with two identical rows, and you want to delete the oldest of the two, you could do that using the oid column.
In my experience, the feature is generally unused in most postgres-backed applications (probably in part because they're non-standard), and their use is essentially deprecated:
In PostgreSQL 8.1 default_with_oids is off by default; in prior versions of PostgreSQL, it was on by default.
The use of OIDs in user tables is considered deprecated, so most installations should leave this variable disabled. Applications that require OIDs for a particular table should specify WITH OIDS when creating the table. This variable can be enabled for compatibility with old applications that do not follow this behavior.
大意是你要是有個(gè)表沒有用主鍵,這時(shí)候可以把oid充當(dāng)為主鍵使用,當(dāng)然這是沒辦法的辦法。
總結(jié): oid是給內(nèi)部表做標(biāo)識用的,不推薦使用。 建議將 default_with_oids 設(shè)置為off。 建表的時(shí)候,如果想使用主鍵,請自行建立。oid本身大小固定的,萬一 行數(shù)超過了oid 的最大限制數(shù)(4 byte int),那就無法插入新行了。