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),那就无法插入新行了。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL 是一个功能强大的开源关系型数据库管理系统,其数据类型设计非常灵活,可以根据需要进行扩展和更改。随着版本的更新,PostgreSQL 不断优化数据类型,以适应更复杂的数据处理需求。这里列举了一些关键的数据类型变化: 1. **新的数据类型**:新版本可能会引入新的数据类型,如JSONB(更高效的 JSON 数据类型)、数组数据类型(比如-jsonb[])等,以支持更复杂的非结构化数据存储。 2. **类型改进**:旧有的数据类型可能会得到增强,例如 timestamp 类型可能提供了更多的精度选项,或者 inet 类型的范围和功能有所增加。 3. **兼容性和向后兼容性**:虽然引入新特性,但通常会尽可能保持向后兼容,以减少用户迁移数据库的麻烦。不过,在升级过程,可能会有不兼容的数据类型转换规则,用户需要谨慎处理。 4. **类型安全性提升**:PostgreSQL 会定期检查并修复潜在的数据类型安全问题,确保不同类型的数据不会意外混合。 5. **类型转换规则**:随着时间的推移,可能会调整或优化默认的类型转换策略,以提高性能或避免潜在的问题。 6. **类型别名和隐式转换**:为了简化查询,可能会添加新的类型别名,并允许在某些情况下进行隐式类型转换。 如果你具体想知道某个版本或特定的变化,可以查阅PostgreSQL的官方文档或查看那个版本的变更日志(Change Logs)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值