pg_sequence 是一个 系统目录 表,它存储有关序列的信息,并与序列在 pg_class 中的主条目相结合。
pg_sequence 在 PostgreSQL 10 中被添加。
备注
此更改将与序列关联的许多元数据从其关联的 关系 移动到此 系统目录 表中。这意味着 ALTER SEQUENCE 更新现在是完全事务性的。
系统目录 视图 pg_sequences 以更易于访问的方式显示序列信息,并且列名与之前在 序列关系 中存在的名称匹配。
有关此更改的更多详细信息,请参阅 PostgreSQL 10 发行说明。
按 PostgreSQL 版本定义
pg_sequence (PostgreSQL 19)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" PRIMARY KEY, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 18)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" PRIMARY KEY, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 17)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" PRIMARY KEY, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 16)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" PRIMARY KEY, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 15)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" PRIMARY KEY, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 14)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" PRIMARY KEY, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 13)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" UNIQUE, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 12)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" UNIQUE, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 11)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" UNIQUE, btree (seqrelid)
文档:pg_sequence
pg_sequence (PostgreSQL 10)
Table "pg_catalog.pg_sequence"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
seqrelid | oid | | not null |
seqtypid | oid | | not null |
seqstart | bigint | | not null |
seqincrement | bigint | | not null |
seqmax | bigint | | not null |
seqmin | bigint | | not null |
seqcache | bigint | | not null |
seqcycle | boolean | | not null |
Indexes:
"pg_sequence_seqrelid_index" UNIQUE, btree (seqrelid)
文档:pg_sequence
变更历史
自 PostgreSQL 10 添加以来,此表未被修改。
- PostgreSQL 10
- 添加于(提交 1753b1b0)
示例
在 PostgreSQL 9.6 及更早版本中,与序列相关的元数据存储在其关联的 关系 中,例如:
postgres=# CREATE SEQUENCE foo; CREATE SEQUENCE
postgres=# SELECT * from public.foo; -[ RECORD 1 ]-+-------------------- sequence_name | foo last_value | 1 start_value | 1 increment_by | 1 max_value | 9223372036854775807 min_value | 1 cache_value | 1 log_cnt | 0 is_cycled | f is_called | f
从 PostgreSQL 10 开始,序列关系 只包含由 nextval() 函数修改的字段(last_value、log_cnt 和 is_called),例如:
postgres=# CREATE SEQUENCE foo;
CREATE SEQUENCE
postgres=# SELECT * from public.foo;
last_value | log_cnt | is_called
------------+---------+-----------
1 | 0 | f
(1 row)
其余元数据存储在 pg_sequence 中,例如:
postgres=# SELECT * FROM pg_sequence WHERE seqrelid='foo'::REGCLASS; -[ RECORD 1 ]+-------------------- seqrelid | 16455 seqtypid | 20 seqstart | 1 seqincrement | 1 seqmax | 9223372036854775807 seqmin | 1 seqcache | 1 seqcycle | f
参考资料
- PostgreSQL 文档: pg_sequence
有用链接
- 单次检查序列状态 - 2019 年 Luca Ferrari 的博客文章
