当前位置:网站首页>昆仑分布式数据库Sequence功能及其实现机制
昆仑分布式数据库Sequence功能及其实现机制
2022-06-22 21:23:00 【KunlunBase 昆仑数据库】
昆仑分布式数据库(下文简称昆仑或者昆仑数据库)的计算节点源自PostgreSQL,因此继承了PostgreSQL的Sequence功能,本文介绍昆仑分布式数据库的Sequence的功能用法、用例和实现。
用法和用例
昆仑数据库的Sequence与MySQL的autoincrement(自增列)相比,其功能更加强大和灵活。主要体现在以下几方面:
1. 昆仑的Sequence与表是多对多的关系,而MySQL的自增列与表是1对1关系
具体来说,昆仑数据库的每个表可以有任意数量的sequence列使用相同或者不同的sequence产生序列值;
并且每一个sequence可以被任意多个表的任意多个列使用来产生ID值。
而MySQL的每个表最多只能有一个自增列并且这个自增列只能被这个表使用(这是废话,但是为了内容对称还是要提一下)。
2. 可以在任何时候调整sequence的初始值,最大值,步长,范围等属性然后继续使用,然后sequence就会按照新的属性产生新序列值。
3. 不依赖索引,清空表后序列值不回绕。
4. 昆仑数据库集群多个计算节点直接或者间接使用同一个sequence 都可以产生全局唯一的序列值。
让我们看一个例子,首先创建一个表t1,t1的主键列serial类型标明它使用一个隐式创建的sequence来产生字段值,所以插入时候可以不为它指定字段值。
create table t1(a serial primary key, b int);
然后创建sequence seq_b,准备用seq_b来产生字段值。创建时可以可选地指定sequence的属性,不指定就使用默认值。
create sequence seq_b;先执行这个语句插入9行,显式调用seq_b产生字段值。
insert into t1(b) values(nextval('seq_b'));可以看到t1的数据如下:
select*from t1;
然后创建表t2,它的b和c列都使用seq_b产生缺省字段值,并且其主键列也适用隐式sequence来产生字段值。
create table t2(a serial primary key, b int default nextval('seq_b'), c int default nextval('seq_b'));由于t2的所有字段都有缺省值,所以用如下语句插入表t2 3行。
insert into t2 default values;查看t2的数据,可以看到每行b和c字段是使用seq_b依次产生的字段值,并且从seq_b上次产生的9之后开始产生序列值。
select*from t2;
最后,还可以使用 select nextval('seq_b'); 这样的语句来直接产生序列值。
修改sequence元数据及其他
可以使用ALTER SEQUENCE 语句来修改sequence的属性,也可以使用ALTER TABLE ... ALTER COLUMN ... SET seqoptions 语句来修改列的隐式sequence的属性。
还可以使用上述alter table语句restart一个sequence。并且可以使用lastval()函数获得sequence上次返回的值。

sequence实现
昆仑数据库的sequence实现继承了PostgreSQL原有的sequence机制。
为了使sequence数据具备容灾能力并且能够被任意数量的计算节点同时使用,因此sequence的与序列值分发有关的数值数据存储在存储节点的mysql.sequences表中,每行对应一个sequence。
一个sequence的元数据具体存储在哪个存储集群中,是在创建sequence时由计算节点动态分配的。
sequence的其他元数据存储在计算节点,可以使用下面的语句查看sequence在计算节点中的元数据:
select t2.relname, t2.oid, seqstart, seqincrement, seqmax, seqmin, seqcache, seqcycle from pg_sequence t1, pg_class t2 where t1.seqrelid = t2.oid;
可以看到 t1和t2的主键列的隐式sequence分别是t1_a_seq和 t2_a_seq,还有显式创建的seq_b ,这些sequence的数值元数据所在的存储集群分别是1,2,1。
同时,可以看到sequence的基本元数据也存储在pg_class元数据表中,而其特有属性存储在pg_sequence表中。
分别连接到编号为1和2的shard查看这3个sequence在这两个存储集群的mysql.sequence表中的数值元数据,可以看到以下信息:


当首次使用一个sequence或者其预约的数值范围用尽时,一个计算节点CN就会通过其cluster_log_applier进程到这个sequence所在的存储集群中去reserve (curval, cur_val + max(10, seqcache)) 这个范围的字段值,然后CN使用这个reserve的范围来为这个sequence分发序列值,直到再次用尽。
这样,即使有多个计算节点使用同一个sequence来分发序列值,仍然可以保持高性能并且保持所有计算节点分发的序列值都唯一。
结语
通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低。
为了提升业务系统性能,优化用户体验,可以通过做主从复制来减轻主数据库的负载。
而且如果主数据库宕机,可快速将业务系统切换到从数据库上,可避免数据丢失。
项目已开源
【GitHub:】
https://github.com/zettadb
【Gitee:】
https://gitee.com/zettadb
THE END
边栏推荐
- js读取剪切板的图片
- Common operations of sourcetree version management
- 使用smart-doc自动生成接口文档
- How to use swagger2
- 事务系统的隔离级别
- Web Caching Technology
- OJ daily practice - spanning 2020
- 异步FIFO
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- [arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
猜你喜欢

程序员接私活兼职选择

【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片

Spark RDD Programming Guide(2.4.3)

OJ daily practice - spanning 2020

mysql主从同步及其分库分表基本流程

What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?

07 项目成本管理

MySQL8.0轻松完成GTID主从复制

Future alternatives to IPv4! Read the advantages, features and address types of IPv6
![[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip](/img/32/88321db57afb50ccc096d687ff9c41.png)
[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip
随机推荐
js防止PC端复制正确的链接
冒泡排序 指针
Enjoy high-performance computing! Here comes the Tianyi cloud HPC solution
OJ daily practice - delete word suffixes
Considerations for using redisson to operate distributed queues
Synchronization circuit and cross clock domain circuit design 2 -- cross clock domain transmission (FIFO) of multi bit signals
Dip1000,1 of D
JSBridge
Asynchronous FIFO
阻止别人使用浏览器调试
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Leakcanary source code (2)
Autoincrement attribute of sqlserver replication table
C language -- 17 function introduction
China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
Learning the interpretable representation of quantum entanglement, the depth generation model can be directly applied to other physical systems
[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip
MySQL master-slave synchronization and its basic process of database and table division
Spark SQL accessing JSON and JDBC data sources
Optimization - linear programming