RowID
1. 目的
IvorySQL提供了兼容Oracle RowID的功能。RowID是一种伪列,在创建表时由数据库自动生成,对于数据库中的每一行,RowID 伪列返回该行的地址。
RowID 应当具备以下特性:
1. 逻辑地标识每一行,且值唯一 |
2. 可以通过ROWID快速查询和修改表的其他列,自身不能被插入和修改 |
3. 用户可以控制是否开启此功能 |
2. 功能开启
IvorySQL提供了多种方式来开启RowID功能。
2.1. 通过GUC参数开启
在IvorySQL 的兼容Oracle 模式下,可以通过 set ivorysql.default_with_rowids to on 来开启RowID,这个参数默认值为off。打开后创建的表,默认就带有了RowID列,可以通过 \d+ 表名 来查看。
ivorysql=# show ivorysql.default_with_rowids;
ivorysql.default_with_rowids
------------------------------
off
(1 row)
ivorysql=# create table t(a int);
CREATE TABLE
ivorysql=# \d+ t
Table "public.t"
Column | Type | Collation | Nullable | Default | Invisible | Storage | Compression | Stats target | Description
--------+-----------------+-----------+----------+---------+-----------+---------+-------------+--------------+-------------
a | pg_catalog.int4 | | | | | plain | | |
Access method: heap
2.2. 通过建表语句中增加 WITH ROWID 选项开启
用户可以选择在需要的表上带有这个选项,没有WITH ROWID选项,将会创建一个普通的表。
ivorysql=# create table t2(a int) with rowid;
CREATE TABLE
ivorysql=# \d+ t2
Table "public.t2"
Column | Type | Collation | Nullable | Default | Invisible | Storage | Compression | Stats target | Description
--------+-----------------+-----------+----------+---------+-----------+---------+-------------+--------------+-------------
a | pg_catalog.int4 | | | | | plain | | |
Indexes:
"t2_16432_rowid_idx" btree (rowid)
Access method: heap
Has ROWID: yes
2.3. 通过对现有表ALTER TABLE … SET WITH ROWID开启
这种方式允许当一个普通表需要使用 ROWID 时,通过ATLER 命令去添加ROWID 列。 也可以通过ALTER TABLE … SET WITHOUT ROWID命令去除RowID。
ivorysql=# create table t3(a int);
CREATE TABLE
ivorysql=# alter table t3 set with rowid;
ALTER TABLE
ivorysql=# \d+ t3;
Table "public.t3"
Column | Type | Collation | Nullable | Default | Invisible | Storage | Compression | Stats target | Description
--------+-----------------+-----------+----------+---------+-----------+---------+-------------+--------------+-------------
a | pg_catalog.int4 | | | | | plain | | |
Access method: heap
Has ROWID: yes