PolarDB 开源版通过pg_rational插件支持Stern-Brocot trees , 实现高效自定义顺序和调整顺序需求

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力. 本文将介绍PolarDB 开源版通过pg_rational插件支持Stern-Brocot trees , 实现高效自定义顺序和调整顺序需求.

背景

PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.

本文将介绍PolarDB 开源版通过pg_rational插件支持Stern-Brocot trees , 实现高效自定义顺序和调整顺序需求.

测试环境为macos+docker, polardb部署请参考:

pg_rational for PolarDB

pg_rational扩展使用 Stern-Brocot 树找到有效的中间点作为最低项的分数。它可以根据任何实际应用的需要继续在分数之间进行更深的拆分。实现高效自定义顺序和调整顺序需求.

pg_rational特性:

  • Stores fractions in exactly 64 bits (same size as float)
  • Written in C for high performance
  • Detects and halts arithmetic overflow for correctness
  • Uses native CPU instructions for fast overflow detection
  • Defers GCD calculation until requested or absolutely required
  • Supports btree and hash indices
  • Implements Stern-Brocot trees for finding intermediate points
  • Coercion from integer/bigint/tuple
  • Custom aggregate

1、安装pg_rational

git clone --depth 1 https://github.com/begriffs/pg_rational  
  
cd pg_rational/  
  
USE_PGXS=1 make  
  
USE_PGXS=1 make install  
  
export PGHOST=127.0.0.1  
  
USE_PGXS=1 make installcheck  
/home/postgres/tmp_basedir_polardb_pg_1100_bld/lib/pgxs/src/makefiles/../../src/test/regress/pg_regress --inputdir=./ --bindir='/home/postgres/tmp_basedir_polardb_pg_1100_bld/bin'      --dbname=contrib_regression pg_rational_test  
(using postmaster on 127.0.0.1, default port)  
============== dropping database "contrib_regression" ==============  
DROP DATABASE  
============== creating database "contrib_regression" ==============  
CREATE DATABASE  
ALTER DATABASE  
============== running regression test queries        ==============  
test pg_rational_test             ... ok  
  
  
==========================================================  
 All 1 tests passed.   
  
 POLARDB:  
 All 1 tests, 0 tests in ignore, 0 tests in polar ignore.   
==========================================================  

2、加载pg_rational插件

psql  
psql (11.9)  
Type "help" for help.  
  
postgres=# create extension pg_rational;  
CREATE EXTENSION  

3、基本操作, pg_rational可以和浮点、整型相互转换.

-- fractions are precise  
-- this would not work with a float type  
select 1::rational / 3 * 3 = 1;  
-- => t  
  
-- provides the usual operations, e.g.  
select '1/3'::rational + '2/7';  
-- => 13/21  
  
-- helper "ratt' type to coerce from tuples  
select 1 + (i,i+1)::ratt from generate_series(1,5) as i;  
-- => 3/2, 5/3, 7/4, 9/5, 11/6  
  
-- simplify if desired  
select rational_simplify('36/12');  
-- => 3/1  
  
-- convert float to rational  
select 0.263157894737::float::rational;  
-- => 5/19  
  
-- convert rational to float  
select '-1/2'::rational::float;  
-- => -0.5  

4、调整顺序测试, 不需要指定值, 只需要指定你要插入到哪两个rational value之间, pg_rational扩展使用 Stern-Brocot 树找到有效的中间点作为最低项的分数。从而实现了快速的顺序调整.

postgres=# create sequence todos_seq;  
CREATE SEQUENCE  
  
  
postgres=# create table todos (  
  prio rational unique  
    default nextval('todos_seq')::float8::rational,  
  what text not null  
);  
CREATE TABLE  
postgres=# insert into todos (what) values  
postgres-#   ('install extension'),  
postgres-#   ('read about it'),  
postgres-#   ('try it'),  
postgres-#   ('profit?');  
INSERT 0 4  
postgres=#   
  
-- put "try" between "install" and "read"  
  
postgres=# select * from todos order by prio asc;  
 prio |       what          
------+-------------------  
 1/1  | install extension  
 2/1  | read about it  
 3/1  | try it  
 4/1  | profit?  
(4 rows)  
  
-- put "read" back between "install" and "try"  
  
postgres=# update todos  
postgres-# set prio = rational_intermediate(1,2)   -- 1为install extension, 2为read about it. 根据Stern-Brocot 树找到1,2之间的3/2分数.   
postgres-# where prio = 3;  
UPDATE 1  
postgres=# select * from todos order by prio asc;  
 prio |       what          
------+-------------------  
 1/1  | install extension  
 3/2  | try it  
 2/1  | read about it  
 4/1  | profit?  
(4 rows)  
  
postgres=# update todos  
postgres-# set prio = rational_intermediate(1,'3/2')   -- 1为install extension, 3/2为try it. 根据Stern-Brocot 树找到1,3/2之间的4/3分数.   
postgres-# where prio = 2;  
UPDATE 1  
postgres=# select * from todos order by prio asc;  
 prio |       what          
------+-------------------  
 1/1  | install extension  
 4/3  | read about it  
 3/2  | try it  
 4/1  | profit?  
(4 rows)  

参考

https://github.com/begriffs/pg_rational

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源PolarDB开发者大会:拥抱开源 | 成就开源》
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源PolarDB开发者大会:拥抱开源 | 成就开源》
18 3
|
7天前
|
运维 关系型数据库 分布式数据库
「合肥 * 讯飞」4 月 19 日 PolarDB 开源数据库沙龙,报名中!
4月19日周五,PolarDB开源社区联合科大讯飞共同举办开源数据库技术沙龙,本次沙龙我们邀请了众多数据库领域的专家,期待大家的参与!
「合肥 * 讯飞」4 月 19 日 PolarDB 开源数据库沙龙,报名中!
|
1月前
|
关系型数据库 分布式数据库 PolarDB
稳健前行:PolarDB开源社区调研开始啦!
PolarDB开源社区调研持续进行中!我们会重视每一位开发者的反馈,对提供建设性建议的开发者将会提供精美周边礼品!欢迎大家参与!
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源》
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源》
16 1
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源》
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源》
21 4
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源》
电子书阅读分享《PolarDB开发者大会:拥抱开源 | 成就开源》
16 1
|
3月前
|
关系型数据库 MySQL 数据处理
MySQL vs. PostgreSQL:选择适合你的开源数据库
在当今信息时代,开源数据库成为许多企业和开发者的首选。本文将比较两个主流的开源数据库——MySQL和PostgreSQL,分析它们的特点、优势和适用场景,以帮助读者做出明智的选择。
|
3月前
|
弹性计算 关系型数据库 数据库
开源PostgreSQL在倚天ECS上的最佳优化实践
本文基于倚天ECS硬件平台,以自顶向下的方式从上层应用、到基础软件,再到底层芯片硬件,通过应用与芯片的硬件特性的亲和性分析,实现PostgreSQL与倚天芯片软硬协同的深度优化,充分使能倚天硬件性能,帮助开源PostgreSQL应用实现性能提升。
|
4月前
|
关系型数据库 数据库 PostgreSQL
PostgreSQL【应用 01】使用Vector插件实现向量相似度查询(Docker部署的PostgreSQL安装pgvector插件说明)和Milvus向量库对比
PostgreSQL【应用 01】使用Vector插件实现向量相似度查询(Docker部署的PostgreSQL安装pgvector插件说明)和Milvus向量库对比
169 1
|
4月前
|
关系型数据库 数据库 PostgreSQL
Docker【应用 03】给Docker部署的PostgreSQL数据库安装PostGIS插件(安装流程及问题说明)
Docker【应用 03】给Docker部署的PostgreSQL数据库安装PostGIS插件(安装流程及问题说明)
143 0

热门文章

最新文章

相关产品

  • 云原生数据库 PolarDB