1. 程式人生 > >PostgreSQL 資料型別介紹(五)OID的理解

PostgreSQL 資料型別介紹(五)OID的理解

系統表, 系統表之間基本上都是以oid關聯. 例如pg_attrdef.adrelid 關聯 pg_class.oid

  • 先介紹下oid的使用:
    以系統表 pg_class為例,檢視下postgres裡各個物件(表、序列、索引 等)的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這個欄位,但是你執行下面語句卻有結果,這是為什麼呢 ? postgres=# select oid from pg_class limit 5 ; oid ------- 2671 2672 16399 16405 16407 (5 rows) 同時在 \d pg_class 命令下還有這個描述 ** "pg_class_oid_index" UNIQUE, btree (oid)** oid 上有 btree索引,並且有唯一約束 pg_class_oid_index 。 也證明了存在oid

那oid在哪兒?到底為什麼會出現這種情況 ?

來看看postgres官網對 oid的介紹:

1.Object identifiers (OIDs) are used internally by PostgreSQL as primary keys for various system tables. 這裡表明了 oid 是內部使用,並作為系統表的主鍵。

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不會新增到 使用者自己建立的表裡,除非明確指定 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 (物件標識號). 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,因此還不是足夠大,不建議用作使用者表的主鍵,最佳用處是作為系統表的主鍵。

根據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.

大意是你要是有個表沒有用主鍵,這時候可以把oid充當為主鍵使用,當然這是沒辦法的辦法。

總結: oid是給內部表做標識用的,不推薦使用。 建議將 default_with_oids 設定為off。 建表的時候,如果想使用主鍵,請自行建立。oid本身大小固定的,萬一 行數超過了oid 的最大限制數(4 byte int),那就無法插入新行了。