pgsql批量修改sequences的start方式

发布时间:2022-04-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了pgsql批量修改sequences的start方式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

改为指定值

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 最大值是多少?

os: centos 7.8.2003

db: postgresql 13.0

版本

# 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方式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:pgsql