PostgreSQL序列获取最大值

4

如何获取Postgres序列的最大值和最小值?

我使用以下语句创建了该序列:

create sequence seqtest increment 1 minvalue 0 maxvalue 20;

我尝试了这个查询 select max_value from seqtest,但是出现了错误。
ERROR:  column "max_value" does not exist
LINE 1: select max_value from seqtest;
HINT:  Perhaps you meant to reference the column "seqtest.last_value".

select * from seqtest 的输出结果

test=# select * from seqtest;

-[ RECORD 1 ]-
last_value | 0
log_cnt    | 0
is_called  | f

PostgreSQL 10?.. 我假设。 - Vao Tsun
4个回答

5
t=# create sequence seqtest increment 1 minvalue 0 maxvalue 20;
CREATE SEQUENCE
t=# select * from pg_sequence where seqrelid = 'seqtest'::regclass;
 seqrelid | seqtypid | seqstart | seqincrement | seqmax | seqmin | seqcache | seqcycle
----------+----------+----------+--------------+--------+--------+----------+----------
    16479 |       20 |        0 |            1 |     20 |      0 |        1 | f
(1 row)

PostgreSQL 10引入了新的目录:https://www.postgresql.org/docs/10/static/catalog-pg-sequence.html

此外: https://www.postgresql.org/docs/current/static/release-10.html

.将序列元数据字段移动到新的pg_sequence系统目录中(Peter Eisentraut)。

现在,序列关系仅存储可以由nextval()修改的字段,即last_value、log_cnt和is_called。其他序列属性,例如起始值和增量,存储在pg_sequence目录的相应行中。


1
选择 * 从 pg_sequences,注意是带有 's' 的,而不是 pg_sequence。你会得到更多的信息。 - ccleve

3

2

或者,可以使用psql提示符使用命令

\d

来实现。

postgres=# \d seqtest
                    Sequence "public.seqtest"
  Type  | Start | Minimum | Maximum | Increment | Cycles? | Cache 
--------+-------+---------+---------+-----------+---------+-------
 bigint |     0 |       0 |      20 |         1 | no      |     1

0

但是这个查询以某种方式显示seqmax=9223372036854775807(2^63-1),无论序列类型如何。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接