相思资源网 Design By www.200059.com

修改为指定值

DO $$DECLARE r record;
BEGIN
FOR r IN SELECT sequence_name FROM information_schema."sequences"
LOOP
 EXECUTE 'ALTER SEQUENCE '|| r.sequence_name ||' restart WITH 10000';
END LOOP;
END$$;

根据表的id修改

DO $$
DECLARE 
 r record;
 start_value integer := 0;
BEGIN
FOR r IN SELECT tablename||'_id_seq' AS sequence_name, tablename FROM pg_tables WHERE schemaname = 'public'
LOOP
 EXECUTE 'SELECT max(id)+1 AS max_value FROM ' || r.tablename INTO start_value;
 IF start_value IS NULL THEN start_value:= 1;
 END IF;
 RAISE NOTICE 'start_value % %', r.tablename,start_value;
 EXECUTE 'ALTER SEQUENCE '|| r.sequence_name ||' restart WITH ' || start_value;
END LOOP;
END$$;

补充:postgresql 13 数据库 sequence 的 maxvalue 最大值是多少"htmlcode">

# cat /etc/centos-release
CentOS Linux release 7.8.2003 (Core)
# su - postgres
Last login: Thu Oct 15 09:59:33 CST 2020 on pts/1

ppostgres@nodepg13-> psql -c "select version();"
             version             
---------------------------------------------------------------------------------------------------------
 PostgreSQL 13.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)

create sequence

$ psql

postgres=# create sequence seq_1;
CREATE SEQUENCE
postgres=# select c.relname,c.relkind,s.* from pg_class c,pg_sequence s where c.oid=s.seqrelid;
 relname | relkind | seqrelid | seqtypid | seqstart | seqincrement |  seqmax  | seqmin | seqcache | seqcycle 
---------+---------+----------+----------+----------+--------------+---------------------+--------+----------+----------
 seq_1 | S  | 40968 |  20 |  1 |   1 | 9223372036854775807 |  1 |  1 | f
(1 row)
seqmax = 9223372036854775807

maxvalue
NO MAXVALUE
The optional clause MAXVALUE maxvalue determines the maximum value for the sequence. If this clause is not supplied or NO MAXVALUE is specified, then default values will be used. The default for an ascending sequence is the maximum value of the data type. The default for a descending sequence is -1.

那就需要查看下 bigint 的值

pgsql批量修改sequences的start方式

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

标签:
pgsql,批量修改sequences,start

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“pgsql批量修改sequences的start方式”

暂无pgsql批量修改sequences的start方式的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。