pg_sequence
是一个 系统目录 表,它与序列在 pg_class 中的主条目结合,存储有关序列的信息。
pg_sequence
在 PostgreSQL 10 中添加。
备注
此更改将与序列关联的大部分元数据从其关联的 关系 移动到此 系统目录 表中。这意味着 ALTER SEQUENCE
更新现在完全是事务性的。
该 系统目录 视图 pg_sequences
以更易访问的形式显示序列信息,并且列名与以前在 序列关系 中存在的列名匹配。
有关更改的更多详细信息,请参阅 PostgreSQL 10 发行说明。
按PostgreSQL版本定义
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的博客文章