当前位置:网站首页>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 .
边栏推荐
- No .egg-info directory found in xxx\pip-pip-egg-info-mq
- MySql 中应该如何将多行数据转为多列数据
- 屏:框贴、0贴合、全贴合
- How to expand and repartition the C disk?
- DaemonSet
- This section is intended to supplement
- Modulenotfounderror: no module named 'pip' solution
- 此章节用于补充
- 6. Combined data type
- :app:checkDebugAarMetadata 2 issues were found when checking AAR metadata: 2 issues were found when
猜你喜欢

Apache dolphin scheduler 2.x nanny level source code analysis, China Mobile engineers uncover the whole process of service scheduling and start

Examples of financial tasks: real-time and offline approval of three scenarios and five optimizations of Apache dolphin scheduler in Xinwang bank

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

Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !

Become an Apache contributor, so easy!

Opencv learning drawing shapes and text

Idea -- use @slf4j to print logs

Leetcode 1184:公交站间的距离

NFT数字藏品开发:数字藏品助力企业发展

倒计时2日!基于 Apache DolphinScheduler&TiDB 的交叉开发实践,从编写到调度让你大幅提升效率
随机推荐
Curl post request on the server, using postman tool for parameter conversion
Unity3d asynchronous loading of scenes and progress bar loading
Leetcode 1184: distance between bus stops
WCF 入门教程二
Why can't extern compile variables decorated with const?
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】
Qt:模态非模态、文本框、按钮、单行输入框
NFT digital collection system development: activating digital cultural heritage
Yolov6 target detection practice: training your own data set (video tutorial)
Drools (3): drools basic syntax (1)
How to expand and repartition the C disk?
Drools (4): drools basic syntax (2)
Opencv learn resize and crop
中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问
Opencv learning warp Perspective
从XSS Playload 学习浏览器解码
6. Backup and recovery of MySQL database
此章节用于补充2
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
redis-migrate-tool迁移报错。