postgresql序列获取max_value

时间:2017-11-29 11:22:35

标签: sql postgresql sequence

如何获得max_value和min_value 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

3 个答案:

答案 0 :(得分:3)

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)

Postgres 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。其他顺序   属性,例如起始值和增量,保存在a中   pg_sequence目录的相应行。

答案 1 :(得分:1)

或者,可以使用psql提示符使用该命令来实现 \ d

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

答案 2 :(得分:0)

select min_value, max_value from pg_sequences where sequencename = 'seqtest';

https://www.postgresql.org/docs/10/view-pg-sequences.html