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$$;
根據(jù)表的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$$;
補(bǔ)充:postgresql 13 數(shù)據(jù)庫(kù) 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 的值

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- pgsql 如何刪除仍有活動(dòng)鏈接的數(shù)據(jù)庫(kù)
- pgsql的UUID生成函數(shù)實(shí)例
- pgsql 如何手動(dòng)觸發(fā)歸檔
- PGSQL實(shí)現(xiàn)判斷一個(gè)空值字段,并將NULL值修改為其它值
- pgsql 實(shí)現(xiàn)用戶自定義表結(jié)構(gòu)信息獲取
- pgsql鎖表后kill進(jìn)程的操作
- PGSQL 實(shí)現(xiàn)把字符串轉(zhuǎn)換成double類型(to_number())
- pgsql添加自增序列、設(shè)置表某個(gè)字段自增操作
- pgsql之pg_stat_replication的使用詳解
相關(guān)文章
解決PostgreSQL服務(wù)啟動(dòng)后占用100% CPU卡死的問(wèn)題
前文書(shū)說(shuō)到,今天耗費(fèi)了九牛二虎之力,終于馴服了NTFS權(quán)限安裝好了PostgreSQL,卻不曾想,服務(wù)啟動(dòng)后,新的狀況又出現(xiàn)了。2009-08-08
PostgreSQL pg_archivecleanup與清理archivelog的操作
這篇文章主要介紹了PostgreSQL pg_archivecleanup與清理archivelog的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
PostgreSQL自動(dòng)更新時(shí)間戳實(shí)例代碼
最近有這么一個(gè)工程,需要使用postgresql數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)中的好幾個(gè)表中都需要時(shí)間戳這個(gè)字段,這篇文章主要給大家介紹了關(guān)于PostgreSQL自動(dòng)更新時(shí)間戳的相關(guān)資料,需要的朋友可以參考下2021-11-11
PostgreSQL的外部數(shù)據(jù)封裝器fdw用法
這篇文章主要介紹了PostgreSQL的外部數(shù)據(jù)封裝器fdw用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
淺析postgresql 數(shù)據(jù)庫(kù) TimescaleDB 修改分區(qū)時(shí)間范圍
這篇文章主要介紹了淺析postgresql 數(shù)據(jù)庫(kù) TimescaleDB 修改分區(qū)時(shí)間范圍,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
教你如何在Centos8-stream安裝PostgreSQL13
這篇文章主要介紹了Centos8-stream安裝PostgreSQL13,初始化PostgreSQL需要先創(chuàng)建postgresql儲(chǔ)存目錄,啟動(dòng)postgresql數(shù)據(jù)庫(kù),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
postgresql 中的幾個(gè) timeout參數(shù) 用法說(shuō)明
這篇文章主要介紹了postgresql中的幾個(gè)timeout參數(shù)用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
PostgreSQL對(duì)GROUP BY子句使用常量的特殊限制詳解
這篇文章主要介紹了PostgreSQL對(duì)GROUP BY子句使用常量的特殊限制詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

