PostgreSQL sharding for Oracle, SQL Server, DB2, Sybase

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介:

Oracle 12c支持sharding,但是对于低版本,如何实现水平分库呢?
在写PostgreSQL 水平分库方案时,想到一招。何不利用PostgreSQL的分片技术来实现对Oracle的分片呢?
分库技术架构和实践请参考:
http://blog.163.com/digoal@126/blog/static/16387704020161239252998/

如果要做到对Oracle用户完全透明,需要满足几个条件:

  1. PostgreSQL必须支持Oracle的SQL语法,这一点 EnterpriseDB 可以满足需求。
  2. PostgreSQL必须支持Oracle的存储过程和函数,以及包。这一点 EnterpriseDB 可以满足需求。
    如果用户愿意修改不兼容的SQL和函数,使用社区版本的 PostgreSQL 就能满足分片需求了。

分片架构如下:
可以支持几乎任何数据库的分片。
_2
分为两层:

  1. 上层为PostgreSQL 或 EnterpriseDB(如果需要兼容Oracle特殊语法),在上层节点中,需要存储表的定义,路由算法,函数,存储过程,视图,序列等全局数据。
    上层的PostgreSQL数据库可以有1个或者多个,最少1个。
  2. 下层为数据分片节点,可以是任何数据库品种,譬如图中所述的Oracle, DB2, Sybase, SQL Server。在分片节点中,存储数据分片,维度表(用户可以自定义维度表的副本数)。

注意,如果要支持函数,必须将原数据库的函数转换为PostgreSQL的函数,在PostgreSQL中可以使用plpgsql语言来实现,包括自治事务也能实现
(参考 http://blog.163.com/digoal@126/blog/static/163877040201613982063/ )。
如果使用EnterpriseDB,则大多数的Oracle函数语法都兼容,用户可以不需要修改,直接使用。

以Oracle为例,介绍实施步骤:
.1. 安装Oracle数据节点,这里假设有4台Oracle数据库,分别为db0,db1,db2,db3。
.2. 安装一台PostgreSQL 9.5+ 以及 oracle_fdw插件。

插件位置:http://pgxn.org/dist/oracle_fdw/  
内含详细说明,推荐阅读。  
http://blog.163.com/digoal@126/blog/static/163877040201181505331588/  
安装好后,设置正确的 NLS_LANG 环境变量(<language>_<territory>.<charset> (for example  AMERICAN_AMERICA.AL32UTF8)),重启数据库。  

.3. 配置oracle数据库监听,以及主机防火墙,允许PostgreSQL数据库访问Oracle数据库。
.4. 在PostgreSQL数据库中创建所有数据节点的foreign server, 本例需要4个foreign server, user mapping。
例如 (请使用正确的 IP,端口和sid, username, password替换) :

master=# create extension oracle_fdw;    
master=# create server db0 foreign data wrapper oracle_fdw OPTIONS (dbserver '//ip:port/sid');    
master=# create server db0 foreign data wrapper oracle_fdw OPTIONS (dbserver '//ip:port/sid');    
master=# create server db0 foreign data wrapper oracle_fdw OPTIONS (dbserver '//ip:port/sid');    
master=# create server db0 foreign data wrapper oracle_fdw OPTIONS (dbserver '//ip:port/sid');    
master=# create user mapping for postgres server db0 options (user 'username', password 'pwd');    
master=# create user mapping for postgres server db1 options (user 'username', password 'pwd');    
master=# create user mapping for postgres server db2 options (user 'username', password 'pwd');    
master=# create user mapping for postgres server db3 options (user 'username', password 'pwd');    

.5. 规划表分区的分布列,如果分布列不是INT类型,可以使用hash函数转换为INT。按abs(mod(column,4))的值计算分布规则。
.6. 在所有的数据节点db[0-3],创建需要分片的表,以及分布列的 check 约束。
例如:

on db0:  
create table tbl ( id int primary key , info varchar2(32), crt_time date, check (abs(mod(id,4))=0));  
on db1:  
create table tbl ( id int primary key , info varchar2(32), crt_time date, check (abs(mod(id,4))=1));  
on db2:  
create table tbl ( id int primary key , info varchar2(32), crt_time date, check (abs(mod(id,4))=2));  
on db3:  
create table tbl ( id int primary key , info varchar2(32), crt_time date, check (abs(mod(id,4))=3));  

.7. 规划维度表的副本数,本文例子假设维度表有2个副本,分别放在db0, db1。
.8. 在数据节点db0, db1创建维度表。
例如:

on db0:  
create table test ( id int primary key, info varchar2(32), crt_time date);  
on db1:  
create table test ( id int primary key, info varchar2(32), crt_time date);  

.9. 在PostgreSQL节点,创建分片表的外部表,必须包含CHECN约束。必须制定KEY,否则不能写。

create FOREIGN table tbl0 (id int OPTIONS (key 'true') , info varchar(32), crt_time timestamp without time zone) server db0 options (table 'tbl', schema 'username');  
create FOREIGN table tbl1 (id int OPTIONS (key 'true') , info varchar(32), crt_time timestamp without time zone) server db1 options (table 'tbl', schema 'username');  
create FOREIGN table tbl2 (id int OPTIONS (key 'true') , info varchar(32), crt_time timestamp without time zone) server db2 options (table 'tbl', schema 'username');  
create FOREIGN table tbl3 (id int OPTIONS (key 'true') , info varchar(32), crt_time timestamp without time zone) server db3 options (table 'tbl', schema 'username');  
alter foreign table tbl0 add constraint ck_tbl0 check (abs(mod(id,4))=0);    
alter foreign table tbl1 add constraint ck_tbl1 check (abs(mod(id,4))=1);    
alter foreign table tbl2 add constraint ck_tbl2 check (abs(mod(id,4))=2);    
alter foreign table tbl3 add constraint ck_tbl3 check (abs(mod(id,4))=3);    

.10. 在PostgreSQL节点,创建维度表的外部表

 create FOREIGN table test0 (id int OPTIONS (key 'true'), info varchar(32), crt_time timestamp without time zone) server db0 options (table 'test', schema 'username');  
 create FOREIGN table test1 (id int OPTIONS (key 'true'), info varchar(32), crt_time timestamp without time zone) server db1 options (table 'test', schema 'username');  

.11. 在PostgreSQL节点,创建分片表的父表,设置继承关系,触发器函数,触发器。

create table tbl (id int primary key, info varchar(32), crt_time timestamp without time zone);  
alter foreign table tbl0 inherit tbl;    
alter foreign table tbl1 inherit tbl;    
alter foreign table tbl2 inherit tbl;    
alter foreign table tbl3 inherit tbl;    
create or replace function f_tbl_ins() returns trigger as 
$$
    
declare    
begin    
  case abs(mod(NEW.id, 4))     
    when 0 then    
      insert into tbl0 (id, info, crt_time) values (NEW.*);    
    when 1 then    
      insert into tbl1 (id, info, crt_time) values (NEW.*);    
    when 2 then    
      insert into tbl2 (id, info, crt_time) values (NEW.*);    
    when 3 then    
      insert into tbl3 (id, info, crt_time) values (NEW.*);    
    else    
      return null;    
  end case;    
    return null;    
end;    

$$
 language plpgsql;    
create trigger tg1 before insert on tbl for each row execute procedure f_tbl_ins();    

.12. 在PostgreSQL节点,创建维度表的父表,设置继承关系,触发器函数,触发器。

create table test (id int primary key, info varchar(32), crt_time timestamp without time zone);  
alter foreign table test0 inherit test;    
-- 在不同的master节点,设置不同的继承,从而实现均衡查询的目的,目前PG内核还不支持维度表的负载均衡。  
create or replace function f_test_iud() returns trigger as 
$$
    
declare    
begin    
  case TG_OP  
    when 'INSERT' then  
      insert into test0 (id, info, crt_time) values (NEW.*);    
      insert into test1 (id, info, crt_time) values (NEW.*);    
    when 'UPDATE' then  
      update test0 set id=NEW.id,info=NEW.info,crt_time=NEW.crt_time where id=OLD.id and info=OLD.info and crt_time=OLD.crt_time;  
      update test1 set id=NEW.id,info=NEW.info,crt_time=NEW.crt_time where id=OLD.id and info=OLD.info and crt_time=OLD.crt_time;  
    when 'DELETE' then  
      delete from test0 where id=OLD.id and info=OLD.info and crt_time=OLD.crt_time;  
      delete from test1 where id=OLD.id and info=OLD.info and crt_time=OLD.crt_time;  
  end case;  
    return null;    
end;    

$$
 language plpgsql;    
create trigger tg1 before insert or update or delete on test for each row execute procedure f_test_iud();    

现在,你可以测试这些表的插入,查询,更新,删除,JOIN。以及分布式事务。
插入tbl这个分片表时,会根据ID计算一个模值,插入到对应的分片节点。
更新,删除,查询时,如果提供了ID的模值,则会选择对应的子节点查询。
对于维度表test,查询时会自动查询test0, 更新,删除,插入则会在test0,test1同时操作 (非并行)。

使用这种方法给其他数据库做sharding, 除了EDB对Oracle兼容性比较好,其他的兼容性都需要用户去验证。
但是不管怎么样,用户可以获得如下好处:
ACID
分布式事务
跨库JOIN
主节点和数据节点都支持水平扩展
prepared statement
支持存储过程和函数

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
5天前
|
SQL Oracle 关系型数据库
【YashanDB知识库】yashandb执行包含带oracle dblink表的sql时性能差
【YashanDB知识库】yashandb执行包含带oracle dblink表的sql时性能差
|
2月前
|
SQL Oracle 关系型数据库
如何在 Oracle 中配置和使用 SQL Profiles 来优化查询性能?
在 Oracle 数据库中,SQL Profiles 是优化查询性能的工具,通过提供额外统计信息帮助生成更有效的执行计划。配置和使用步骤包括:1. 启用自动 SQL 调优;2. 手动创建 SQL Profile,涉及收集、执行调优任务、查看报告及应用建议;3. 验证效果;4. 使用 `DBA_SQL_PROFILES` 视图管理 Profile。
|
5月前
|
SQL 监控 Oracle
Oracle SQL性能优化全面指南
在数据库管理领域,Oracle SQL性能优化是确保数据库高效运行和数据查询速度的关键
|
5月前
|
SQL 存储 Oracle
Oracle数据库SQL语句详解与应用指南
在数字化时代,数据库已成为各类企业和组织不可或缺的核心组件。Oracle数据库作为业界领先的数据库管理系统之一,广泛应用于各种业务场景。掌握Oracle数据库的SQL语句是数据库管理员、开发人员及运维人员的基本技能。本文将详细介绍Oracle数据库SQL语句的基本概念、语法、应用及最佳实践。一、Or
153 3
|
5月前
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
143 1
|
6月前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
1202 2
|
8月前
|
开发框架 Oracle 关系型数据库
ABP框架使用Oracle数据库,并实现从SQLServer中进行数据迁移的处理
ABP框架使用Oracle数据库,并实现从SQLServer中进行数据迁移的处理
|
7月前
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
792 0
|
8月前
|
SQL 存储 Oracle
TDengine 3.3.2.0 发布:新增 UDT 及 Oracle、SQL Server 数据接入
**TDengine 3.3.2.0 发布摘要** - 开源与企业版均强化性能,提升WebSocket、stmt模式写入与查询效率,解决死锁,增强列显示。 - taos-explorer支持geometry和varbinary类型。 - 企业版引入UDT,允许自定义数据转换。 - 新增Oracle和SQL Server数据接入。 - 数据同步优化,支持压缩,提升元数据同步速度,错误信息细化,支持表名修改。 - 扩展跨平台支持,包括麒麟、Euler、Anolis OS等。
195 0

相关产品

  • 云原生数据库 PolarDB
  • 推荐镜像

    更多