基于PostgreSQL的集群数据库-CitusDB
2016-06-11 13:53:27 阿炯

CitusDB 是一个基于最新 PostgreSQL 构建的分布式数据库,可对 PostgreSQL 数据库进行伸缩以适合大数据的处理。可在集群中进行自动分片和碎片复制,运行在云端或者混合系统中。数据库的查询可在集群中进行分布式处理,充分利用集群中每个节点的计算能力。CitusDB 可提升 PostgreSQL 的高并发性和 JSON 支持,可用作事务以及分析数据库场景。CitusDB 基于最新版本的 PG 数据库构建,是一个 PostgreSQL 的扩展,因此应用可以无缝的切换到 CitusDB 上。它提供了社区和企业两种版本,前者采用AGPLv3协议授权,后者是商业化产品。


CitusDB 将你的数据库在集群中进行分布,对你的应用来说 CitusDB 就像是一个单一节点的 PostgreSQL 服务,对应用来说是透明的。集群中可轻松添加节点。CitusDB 可将查询分布到集群中的每个节点,可用于快速处理查询以及并行处理。这比单一节点的 PG 数据库速度要快很多,理论上在20个节点的集群里运行速度是单一节点的 20 倍。


A Distributed Postgres Database

Citus turns Postgres into a distributed database. It enables you to transparently shard your data across multiple Postgres instances, then provides multiple executors to query across your entire dataset.

The result is that you can seamlessly scale out Postgres and still have all the great features that PostgreSQL provides.

Features

Hybrid Operational and Analytics Processing

Citus combines real-time inserts while powering both operational and analytical workloads by distributing the processing using multiple execution models.

Transparent sharding

Citus makes it easy to scale by transparently sharding your data across multiple Postgres instances.

Massively parallel processing

Citus distributes each query across the full cluster allowing you to take advantage of all processing power of your nodes.

Integrated row and columnar storage

Users can use row-based and column-based tables in the same database. Columnar storage enables superior compression and improves the performance of analytic workloads by reducing disk I/O times. Row storage allows Citus to achieve low latency for real-time workloads.

High Availability

By employing a shared nothing architecture and utilizing modular blocks (shards), similar to the Hadoop Distributed File System, we're able deliver high availability.

Your master node tracks all the worker nodes in the cluster while data is sharded and replicated across the cluster. Additional nodes can be added to the Citus block architecture at any time to increase capacity and processing power.

Each shard is replicated on multiple cluster nodes so the loss of a single node does not impact data availability, delivering high availability for PostgreSQL.

在 PostgreSQL 开源Citus 之前,PostgreSQL 的横向扩展受到不少阻碍。而通过 Citus,PostgreSQL 可以很容易地扩展到多个节点上,并智能地分配事务和 SQL 查询以获得大规模的并行性,为数据库提供更大的计算,内存和磁盘空间。Citus 背后的公司提供商业工具(及完全管理的 Citus 数据库),但这些工具只是简化了管理伸缩。Citus 的云计算负责人 Craig Kerstiens 表示,SaaS 创业公司或基于 PostgreSQL 的企业可以不仅可以实现简化,还不用担心以后的扩展问题。


最新版本:11.0
CitusData 于2022年6月推出了 Citus 11.0,并宣布将所有企业版的特性都进行开源。此前,Citus 在版本更新时通常会同步发布 2 个版本:开源版本和包含一些额外功能的企业版本。“但是,Citus 11.0 将只有一个版本,因为 Citus 扩展中的所有内容现在都是完全开源的!”公告指出,这意味着你现在可以在无阻塞写入的情况下重新平衡分片、跨集群管理角色、将租户隔离到他们自己的分片等等。“可以从任何节点查询你的 Citus 集群,从而创建真正分布式的 PostgreSQL 体验。”

Citus 是一个 PostgreSQL 的扩展,将 Postgres 转变为一个分布式数据库,因此你可以在任何规模下实现高性能。CitusData 于 2019 年被微软收购;官方表示,目前团队的重点已转向开源。“让 Citus 开源使你能够直接与开发者和社区互动、了解你运行的代码、避免锁定问题,而且它为每个人创造了更好的开发者体验。”

2021年,在 Citus 10 版本中已经开源了分片再平衡功能(shard rebalancer);这是 Citus 的一个重要组成部分,允许你通过将数据转移到新的节点来轻松扩展你的集群。且分片再平衡功能对性能也很有用,可以在集群中的所有节点之间平衡数据。现在作为 Citus 11.0 的一部分,其余的企业功能也全部开源,包括有:
使用逻辑复制重新平衡分片以避免阻塞写入
多用户支持(角色和授权传播、行级安全性)
多租户应用的租户隔离
对节点间身份验证的细粒度控制
通过连接池路由内部连接
数据加载的性能优化

此外,Citus 11 还带有一个重要的新功能 —— 自动模式和元数据同步。


以及一个隐藏的预览功能 —— 分布式表的触发器,用户可使用citus.enable_unsafe_triggers 设置启用触发器:

CREATE TABLE data (key text primary key, value jsonb);
SELECT create_distributed_table('data','key');

CREATE TABLE data_audit (operation text, key text, new_value jsonb, change_time timestamptz default now());
SELECT create_distributed_table('data_audit','key', colocate_with := 'data');

-- we know this function only writes to a co-located table using the same key
CREATE OR REPLACE FUNCTION audit_trigger()
RETURNS trigger
AS $$
DECLARE
BEGIN
    INSERT INTO data_audit VALUES (TG_OP, Coalesce(OLD.key, NEW.key), NEW.value);
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;

-- so, it is safe to enable triggers on distributed tables
SET citus.enable_unsafe_triggers TO on;

CREATE TRIGGER data_audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON data
FOR EACH ROW EXECUTE FUNCTION audit_trigger();

公告还称,新版 Citus 11 的发布标志着 Citus 正在进入新的领域。“想象一下,如果有一个 FOSS 工具可以将最新版本的 PostgreSQL 变成一个分布式数据库,它可以从单个节点向外扩展,在集群中路由或并行化查询,在任何规模下都有很高的性能,允许你把你的应用程序连接到任何节点,不间断地扩展,在 Azure 上单击几下即可获得集群或在任何环境中自行运行。此外,它可以满足极端数据密集型工作负载的要求。这就是 Citus 11 所提供的。”

更多详情可查看官方博客

最新版本:12.0
Citus 12.1 现已于2023年10月上旬发布,此版本增加了对 Postgres 16 的支持,主要更新内容包括:
从任意节点查询时的负载均衡;
分布式 Citus 集群中的 pg_stat_io;
支持 JSON_ARRAYAGG () 和 JSON_OBJECTAGG () 聚合;
支持 COPY FROM 的 DEFAULT 选项;
传播自定义 ICU 整理规则;
支持 Citus 外部表上的 TRUNCATE 触发器;
将新的 CREATE TABLE、VACUUM 和 ANALYZE 选项传播到 Citus 节点。
除支持 Postgres 16 之外,Citus 12.1 还包括:
授予数据库创建权限,使用 citus_schema_move () 进行分布式模式移动。
详情可查看发行公告


官方主页:https://www.citusdata.com/