PostgreSQL 數(shù)據(jù)類型介紹(五)OID的理解

系統(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è)對(duì)象(表、序列、索引 等)的oid

pg_class 存儲(chǔ)的都是這些對(duì)象的信息

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)
//有沒(méi)有覺(jué)得奇怪? 明明沒(méi)有oid這個(gè)字段,但是你執(zhí)行下面語(yǔ)句卻有結(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在哪兒?到底為什么會(huì)出現(xiàn)這種情況 ?

來(lái)看看postgres官網(wǎng)對(duì) 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不會(huì)添加到 用戶自己創(chuàng)建的表里,除非明確指定 WITH OIDS 或者 default_with_oids 打開(kāi)。
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 (對(duì)象標(biāo)識(shí)號(hà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è)表沒(méi)有用主鍵,這時(shí)候可以把oid充當(dāng)為主鍵使用,當(dāng)然這是沒(méi)辦法的辦法。

總結(jié): oid是給內(nèi)部表做標(biāo)識(shí)用的,不推薦使用。 建議將 default_with_oids 設(shè)置為off。 建表的時(shí)候,如果想使用主鍵,請(qǐng)自行建立。oid本身大小固定的,萬(wàn)一 行數(shù)超過(guò)了oid 的最大限制數(shù)(4 byte int),那就無(wú)法插入新行了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、源題QUESTION 1The instance abnormally terminates because ...
    貓貓_tomluo閱讀 1,755評(píng)論 0 2
  • 你要······將來(lái)才能過(guò)穩(wěn)定的生活 有多少人跟你說(shuō)過(guò)這句話?一個(gè),兩個(gè),三個(gè)四個(gè)? 誰(shuí)跟你說(shuō)過(guò)這句話?父母?老師...
    L小姐在路上閱讀 531評(píng)論 4 5
  • 〖每日拔拔草〗三度思維空性 1。今天中午婆婆因?yàn)橹蹲硬怀燥?,打了他一下,我覺(jué)得婆婆是在種下壞種子。其實(shí)看到婆婆這樣...
    lindacheng2017閱讀 327評(píng)論 0 1
  • 1、計(jì)算機(jī)三個(gè)關(guān)鍵部位:中央處理器CPU,內(nèi)存和I/O控制芯片; 2、一般將計(jì)算機(jī)本身的軟件成為系統(tǒng)軟件,以區(qū)別于...
    努力爬行中的蝸牛閱讀 211評(píng)論 0 0

友情鏈接更多精彩內(nèi)容