当前位置:网站首页>PostgreSQL sequence create alter nextval Curval numerical interval gap
PostgreSQL sequence create alter nextval Curval numerical interval gap
2022-07-26 07:21:00 【Jintian】
List of articles
Syntax
Suppose I want to create sequence The name is seq_for_user_id,:
-- New sequence
create sequence seq_for_user_id increment by 1 min value 1 start with 1 cache 1 cycle;
-- Modify the sequence
alter sequence if exists seq_for_user_id increment by 1 minvalue 1 start with 1 cache 100 cycle;
-- Common operations
select nextval('seq_for_user_id'); -- Get the next value
select currval('seq_for_user_id'); -- Check the current value ( The last value generated and obtained )
about nextval and currval function , If you report an error xxx The object does not exist in the current session , Can be in sequence name And before schema name.
Such as :select nextval('wuyujin1997_demodb.seq_for_user_id');
Document
– newly build sequence
https://www.postgresql.org/docs/current/sql-createsequence.html
CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name
[ AS data_type ]
[ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
[ OWNED BY { table_name.column_name | NONE } ]
– modify sequence
https://www.postgresql.org/docs/10/sql-altersequence.html
ALTER SEQUENCE [ IF EXISTS ] name
[ AS data_type ]
[ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ]
[ RESTART [ [ WITH ] restart ] ]
[ CACHE cache ] [ [ NO ] CYCLE ]
[ OWNED BY { table_name.column_name | NONE } ]
ALTER SEQUENCE [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
ALTER SEQUENCE [ IF EXISTS ] name RENAME TO new_name
ALTER SEQUENCE [ IF EXISTS ] name SET SCHEMA new_schema
– sequence Correlation function
https://www.postgresql.org/docs/current/functions-sequence.html
nextval(regclass) --> bigint
setval(regclass, bigint[, boolean]) --> bigint
currval(regclass) --> bigint
lastval(regclass) --> bigint

postgresql sequence gap The gap
https://www.postgresql.org/docs/current/functions-sequence.html mention :
Postgresql Of sequence Cannot be used to obtain “ There is no interval / Numerical sequence of gaps ”.
Caution
To avoid blocking concurrent transactions that obtain numbers from the same sequence,
the value obtained by nextval is not reclaimed for re-use if the calling transaction later aborts.
For the wrong “ Concurrent transactions from the same sequence Get the value in ” This caused a blockage , Those have been used nextval Value obtained by method , Even if the transaction is aborted, it will not be recycled .
This means that transaction aborts or database crashes can result in gaps in the sequence of assigned values.
This means that the transaction is aborted / Database crash can cause “ By the same sequence Generated numerical sequence ” Yes, there is a gap / Spaced .
That can happen without a transaction abort, too.
This phenomenon can also occur without transaction suspension .
For example an INSERT with an ON CONFLICT clause will compute the to-be-inserted tuple, including doing any required nextval calls,
before detecting any conflict that would cause it to follow the ON CONFLICT rule instead.
for instance , You wrote a insert...on conflict The sentence of , The partial values to be inserted are from sequence Generate .
Because of calling sequence nextval The latest value is generated , And then do it insert , If there is a conflict in the insertion action , Insert failed .
But it has been used nextval The generated value will not be recycled .
Thus, PostgreSQL sequence objects cannot be used to obtain “gapless” sequences.
therefore ,Postgresql Of sequence Cannot be used to obtain “ There is no interval / Numerical sequence of gaps ”.
Likewise, sequence state changes made by setval are immediately visible to other transactions, and are not undone if the calling transaction rolls back.
Same as , call setval Yes sequence The change caused by the state is also immediately visible to other transactions , And even if the transaction is rolled back , Status changes will not be canceled .
If the database cluster crashes before committing a transaction containing a nextval or setval call,
the sequence state change might not have made its way to persistent storage,
so that it is uncertain whether the sequence will have its original or updated state after the cluster restarts.
If the database cluster is “ Submit a containing nextval/setval Called transaction ” Collapsed before , Now sequence The state change of may not be persisted to the database server (database server-side), So when the database crashes - After reboot , I'm not sure sequence The value of is the old value before changing , Or the updated finger ( It depends on how tiny the database crashed , Whether the change is persistent to DB layer ).
This is harmless for usage of the sequence within the database, since other effects of uncommitted transactions will not be visible either.
This is harmless for using sequences in the database , Because other effects of uncommitted transactions will also be invisible .
However, if you wish to use a sequence value for persistent outside-the-database purposes,
make sure that the nextval call has been committed before doing so.
however , If you want to use sequence values for persistent off database purposes , Please make sure nextval The call was submitted before this operation .
边栏推荐
- IDEA快捷键
- 此章节用于补充
- Upgrade ecological proposition: what has Alibaba cloud brought to thousands of businesses?
- NFT digital collection development: digital art collection enabling public welfare platform
- NFT数字藏品开发:数字藏品助力企业发展
- 3.0.0 alpha blockbuster release! Nine new functions and new UI unlock new capabilities of dispatching system
- Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程
- Typora免费版下载安装
- PR字幕制作
- NFT数字藏品系统开发:文学+数字藏品的碰撞
猜你喜欢

NFT数字藏品系统开发:数字藏品赋予品牌新活力

配置Flask

pycharm常用快捷键

正则表达式规则以及常用的正则表达式

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary question record (acdgij)

DaemonSet

3.0.0 alpha blockbuster release! Nine new functions and new UI unlock new capabilities of dispatching system

Yolov5 improvements: add attention mechanism (video tutorial)

达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】

anaconda安装教程-手把手教你安装
随机推荐
Qt:列表框、表格、树形控件
0动态规划 LeetCode1567. 乘积为正数的最长子数组长度
China Unicom transformed the Apache dolphin scheduler resource center to realize the one-stop access of cross cluster call and data script of billing environment
【C语言】你真的了解printf吗?(printf典型易错,强烈建议收藏)
如何对C盘进行扩容重新分区?
Leetcode:749. isolate virus [Unicom component + priority queue + status representation]
Compose canvas custom circular progress bar
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]
NFT数字藏品系统开发:数字藏品赋予品牌新活力
NFT数字藏品系统开发:NFT数藏 的最佳数字营销策略有哪些
Unity3d asynchronous loading of scenes and progress bar loading
MySQL安装教程-手把手教你安装
3.0.0 alpha blockbuster release! Nine new functions and new UI unlock new capabilities of dispatching system
Download and install the free version of typora
NFT数字藏品系统开发:激活数字文化遗产
NFT digital collection system development: digital collections give new vitality to brands
From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.
数据平台调度升级改造 | 从Azkaban 平滑过度到 Apache DolphinScheduler 的操作实践
Become an Apache contributor, so easy!
成为 Apache 贡献者,So easy!