datasketches

PostgreSQL 近似分析摘要数据结构与聚合函数

概览

扩展包名版本分类许可证语言
datasketches1.7.0FUNCApache-2.0C++
ID扩展名BinLibLoadCreateTrustReloc模式
4690datasketches-

Built against Apache DataSketches C++ core 5.0.0.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.7.01817161514datasketches-
RPMPIGSTY1.7.01817161514datasketches_$v-
DEBPIGSTY1.7.01817161514postgresql-$v-datasketches-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
d13.x86_64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
d13.aarch64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
u22.x86_64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
u22.aarch64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
u24.x86_64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
u24.aarch64
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0
PIGSTY 1.7.0

构建

您可以使用 pig build 命令构建 datasketches 扩展的 RPM / DEB 包:

pig build pkg datasketches         # 构建 RPM / DEB 包

安装

您可以直接安装 datasketches 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install datasketches;          # 当前活跃 PG 版本安装
pig ext install -y datasketches -v 18  # PG 18
pig ext install -y datasketches -v 17  # PG 17
pig ext install -y datasketches -v 16  # PG 16
pig ext install -y datasketches -v 15  # PG 15
pig ext install -y datasketches -v 14  # PG 14
dnf install -y datasketches_18       # PG 18
dnf install -y datasketches_17       # PG 17
dnf install -y datasketches_16       # PG 16
dnf install -y datasketches_15       # PG 15
dnf install -y datasketches_14       # PG 14
apt install -y postgresql-18-datasketches   # PG 18
apt install -y postgresql-17-datasketches   # PG 17
apt install -y postgresql-16-datasketches   # PG 16
apt install -y postgresql-15-datasketches   # PG 15
apt install -y postgresql-14-datasketches   # PG 14

创建扩展

CREATE EXTENSION datasketches;

用法

来源: README, Apache DataSketches 网站 PostgreSQL 扩展,用于近似分析草图与聚合。

CREATE EXTENSION datasketches;

该扩展支持 CPC、HLL、Theta、Array Of Doubles、KLL、Quantiles 以及 Frequent Strings 草图。

草图类型

  • CPC 用于紧凑的去重计数。
  • HLL 用于 HyperLogLog 风格的去重计数。
  • Theta 用于去重计数,并支持 union、intersection 和 A-not-B 等集合运算。
  • Array Of Doubles 用于每个键对应一个 double 数组的 tuple sketch。
  • KLL 用于分位数、排名、PMF 和 CDF 估计。
  • Quantiles 草图用于分布估计。
  • Frequent strings 用于按计数或权重追踪最重项。

示例

SELECT cpc_sketch_to_string(cpc_sketch_build(1));
SELECT cpc_sketch_distinct(id) FROM random_ints_100m;
SELECT cpc_sketch_get_estimate(cpc_sketch_union(sketch)) FROM cpc_sketch_test;
SELECT theta_sketch_get_estimate(theta_sketch_union(sketch)) FROM theta_sketch_test;
SELECT theta_sketch_get_estimate(theta_sketch_intersection(sketch1, sketch2)) FROM theta_set_op_test;
SELECT hll_sketch_get_estimate(hll_sketch_union(sketch)) FROM hll_sketch_test;
SELECT hll_sketch_get_estimate(hll_sketch_union(hll_sketch_build(1), hll_sketch_build(2)));
SELECT kll_float_sketch_get_quantile(kll_float_sketch_merge(sketch), 0.5) FROM kll_float_sketch_test;
SELECT frequent_strings_sketch_result_no_false_negatives(frequent_strings_sketch_build(9, value), 1000000) FROM zipf_1p1_8k_100m;

核心操作

  • 使用 *_sketch_build(...) 构建草图。
  • 使用 *_sketch_union(...)*_sketch_merge(...) 以及各类草图特定的集合运算辅助函数进行合并或聚合。
  • 使用 *_sketch_get_estimate(...) 以及 kll_float_sketch_get_quantile(...) 等函数读取估计值。

说明

  • README 说明该扩展面向 PostgreSQL 9.6 及以上版本,并依赖 Boost 1.75 和 DataSketches C++ core 5.0.0 或更高版本。
  • 上游示例强调的是面向数据立方体的增量分析,而不是普通聚合的精确替代品。

最后修改 2026-04-14: update extension catalog (fa7cf58)