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

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

背景

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

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

测试环境为macos+docker, polardb部署请参考如何用 PolarDB 证明巴菲特的投资理念 - 包括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.   
==========================================================  
  1. 加载pg_rational插件

psql  
psql (11.9)  
Type "help" for help.  
  
postgres=# create extension pg_rational;  
CREATE EXTENSION  
  1. 基本操作, 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  
  1. 调整顺序测试, 不需要指定值, 只需要指定你要插入到哪两个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月前
|
SQL 关系型数据库 数据库
一文熟悉PolarDB-PG 分区表核心特性
在 PolarDB-PG 数据库中,分区表 (Partitioned Table) 使您能够将非常大的表分解为更小且更易于管理的部分,这个部分称为分区 (Partition) 。 每个分区都是一个独立的对象,具有自己的名称和可选的存储特性。本文首先简单的介绍了分区表策略以及它的优势特点,然后介绍了PolarDB-PG 分区表支持的查询优化特性,最后介绍了分区表上的本地索引和全局索引,从而帮助用户对PolarDB-PG 分区表有一个全面的了解。
|
2月前
|
存储 SQL 数据管理
阿里云数据库 SelectDB 内核 Apache Doris 如何基于自增列满足高效字典编码等典型场景需求|Deep Dive 系列
自增列的实现,使得 Apache Doris 可以在处理大规模时展示出更高的稳定性和可靠性。通过自增列,用户能够高效进行字典编码,显著提升了字符串精确去重以及查询的性能。使用自增列作为主键来存储明细数据,可以完美的解决明细数据更新的问题。同时,基于自增列,用户可以实现高效的分页机制,轻松应对深分页场景,有效过滤掉大量非必需数据,从而减轻数据库的负载压力,为用户带来了更加流畅和高效的数据处理体验。
|
5月前
|
SQL 关系型数据库 C语言
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
45 0
|
12月前
|
存储 安全 前端开发
PostgreSQL 12 文档: 部分 VII. 内部
部分 VII. 内部 这一部分包含PostgreSQL开发者可能用到的各类信息。 目录
75 0
|
存储 并行计算 Cloud Native
PolarDB 开源版通过pg_rational插件支持Stern-Brocot trees , 实现高效自定义顺序和调整顺序需求
PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力. 本文将介绍PolarDB 开源版通过pg_rational插件支持Stern-Brocot trees , 实现高效自定义顺序和调整顺序需求.
163 0
|
SQL 弹性计算 关系型数据库
PostgreSQL 12 preview - CTE 增强,支持用户语法层控制 materialized 优化
标签 PostgreSQL , CTE , materialized , not materialized , push down 背景 PostgreSQL with 语法,能跑非常复杂的SQL逻辑,包括递归,多语句物化计算等。 在12以前的版本中,WITH中的每一个CTE(common table express),都是直接进行物化的,也就是说外层的条件不会推到CTE(物化节点)里
837 0
|
关系型数据库 MySQL 数据库
PostgreSQL的学习心得和知识总结(二十五)|语法级自上而下完美实现MySQL数据库的 字段默认值的自动插入更新 的实现方案
本人CSDN博主 孤傲小二~阿沐,本文《PostgreSQL的学习心得和知识总结(二十五)|语法级自上而下完美实现MySQL数据库的 字段默认值的自动插入更新 的实现方案》来自于我在CSDN的同名文档
|
SQL 存储 关系型数据库
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之9 - parallel 自定义并行聚合
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan parallel index scan
556 0