跳转到主要内容

pg_sorted_heap

带 zone map 剪枝和内置向量搜索的有序堆表访问方法

pg_sorted_heap · 带 zone map 剪枝和内置向量搜索的有序堆表访问方法

概览

ID扩展软件包版本类别许可证编程语言
2550pg_sorted_heappg_sorted_heap0.14.0OLAPPostgreSQLC
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--sLd--否是是是否否
sorted_hnsw.shared_cache requires shared_preload_libraries=pg_sorted_heap.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.14.019 18 17 16 15pg_sorted_heap-
RPMPIGSTY0.14.019 18 17 16 15pg_sorted_heap_$v-
DEBPIGSTY0.14.019 18 17 16 15postgresql-$v-pg-sorted-heap-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
el8.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
el9.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
el9.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
el10.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
el10.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
d12.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
d12.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
d13.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
d13.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
u22.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
u22.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
u24.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
u24.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
u26.x86_64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
u26.aarch64N/APIGSTY 0.14.0PIGSTY 0.14.0PIGSTY 0.14.0N/A
软件包版本平台仓库大小下载
pg_sorted_heap_180.14.0el8.x86_64pigsty206.0 KiBpg_sorted_heap_18-0.14.0-1PGSTY.el8.x86_64.rpm
pg_sorted_heap_180.14.0el8.aarch64pigsty190.2 KiBpg_sorted_heap_18-0.14.0-1PGSTY.el8.aarch64.rpm
pg_sorted_heap_180.14.0el9.x86_64pigsty204.5 KiBpg_sorted_heap_18-0.14.0-1PGSTY.el9.x86_64.rpm
pg_sorted_heap_180.14.0el9.aarch64pigsty195.4 KiBpg_sorted_heap_18-0.14.0-1PGSTY.el9.aarch64.rpm
pg_sorted_heap_180.14.0el10.x86_64pigsty209.1 KiBpg_sorted_heap_18-0.14.0-1PGSTY.el10.x86_64.rpm
pg_sorted_heap_180.14.0el10.aarch64pigsty202.6 KiBpg_sorted_heap_18-0.14.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-sorted-heap0.14.0d12.x86_64pigsty724.4 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-sorted-heap0.14.0d12.aarch64pigsty711.8 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-sorted-heap0.14.0d13.x86_64pigsty726.9 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-sorted-heap0.14.0d13.aarch64pigsty715.4 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-sorted-heap0.14.0u22.x86_64pigsty758.6 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-sorted-heap0.14.0u22.aarch64pigsty754.4 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-sorted-heap0.14.0u24.x86_64pigsty745.1 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-sorted-heap0.14.0u24.aarch64pigsty742.1 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-sorted-heap0.14.0u26.x86_64pigsty746.6 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-sorted-heap0.14.0u26.aarch64pigsty737.0 KiBpostgresql-18-pg-sorted-heap_0.14.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_sorted_heap_170.14.0el8.x86_64pigsty206.2 KiBpg_sorted_heap_17-0.14.0-1PGSTY.el8.x86_64.rpm
pg_sorted_heap_170.14.0el8.aarch64pigsty190.0 KiBpg_sorted_heap_17-0.14.0-1PGSTY.el8.aarch64.rpm
pg_sorted_heap_170.14.0el9.x86_64pigsty204.4 KiBpg_sorted_heap_17-0.14.0-1PGSTY.el9.x86_64.rpm
pg_sorted_heap_170.14.0el9.aarch64pigsty195.5 KiBpg_sorted_heap_17-0.14.0-1PGSTY.el9.aarch64.rpm
pg_sorted_heap_170.14.0el10.x86_64pigsty209.4 KiBpg_sorted_heap_17-0.14.0-1PGSTY.el10.x86_64.rpm
pg_sorted_heap_170.14.0el10.aarch64pigsty202.2 KiBpg_sorted_heap_17-0.14.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-sorted-heap0.14.0d12.x86_64pigsty723.9 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-sorted-heap0.14.0d12.aarch64pigsty711.2 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-sorted-heap0.14.0d13.x86_64pigsty726.2 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-sorted-heap0.14.0d13.aarch64pigsty714.2 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-sorted-heap0.14.0u22.x86_64pigsty806.0 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-sorted-heap0.14.0u22.aarch64pigsty802.4 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-sorted-heap0.14.0u24.x86_64pigsty744.1 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-sorted-heap0.14.0u24.aarch64pigsty741.7 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-sorted-heap0.14.0u26.x86_64pigsty745.5 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-sorted-heap0.14.0u26.aarch64pigsty736.0 KiBpostgresql-17-pg-sorted-heap_0.14.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_sorted_heap_160.14.0el8.x86_64pigsty206.3 KiBpg_sorted_heap_16-0.14.0-1PGSTY.el8.x86_64.rpm
pg_sorted_heap_160.14.0el8.aarch64pigsty190.3 KiBpg_sorted_heap_16-0.14.0-1PGSTY.el8.aarch64.rpm
pg_sorted_heap_160.14.0el9.x86_64pigsty204.5 KiBpg_sorted_heap_16-0.14.0-1PGSTY.el9.x86_64.rpm
pg_sorted_heap_160.14.0el9.aarch64pigsty195.5 KiBpg_sorted_heap_16-0.14.0-1PGSTY.el9.aarch64.rpm
pg_sorted_heap_160.14.0el10.x86_64pigsty209.5 KiBpg_sorted_heap_16-0.14.0-1PGSTY.el10.x86_64.rpm
pg_sorted_heap_160.14.0el10.aarch64pigsty202.3 KiBpg_sorted_heap_16-0.14.0-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-sorted-heap0.14.0d12.x86_64pigsty723.7 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-sorted-heap0.14.0d12.aarch64pigsty711.4 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-sorted-heap0.14.0d13.x86_64pigsty726.1 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~trixie_amd64.deb
postgresql-16-pg-sorted-heap0.14.0d13.aarch64pigsty714.5 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~trixie_arm64.deb
postgresql-16-pg-sorted-heap0.14.0u22.x86_64pigsty802.2 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~jammy_amd64.deb
postgresql-16-pg-sorted-heap0.14.0u22.aarch64pigsty798.5 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~jammy_arm64.deb
postgresql-16-pg-sorted-heap0.14.0u24.x86_64pigsty743.9 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~noble_amd64.deb
postgresql-16-pg-sorted-heap0.14.0u24.aarch64pigsty741.7 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~noble_arm64.deb
postgresql-16-pg-sorted-heap0.14.0u26.x86_64pigsty745.6 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~resolute_amd64.deb
postgresql-16-pg-sorted-heap0.14.0u26.aarch64pigsty735.9 KiBpostgresql-16-pg-sorted-heap_0.14.0-1PGSTY~resolute_arm64.deb

源码

pig build pkg pg_sorted_heap;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install pg_sorted_heap;		# install by package name

pig install pg_sorted_heap -v 18;   # install for PG 18
pig install pg_sorted_heap -v 17;   # install for PG 17
pig install pg_sorted_heap -v 16;   # install for PG 16

将扩展加入 shared_preload_libraries,详见配置说明。

shared_preload_libraries = 'pg_sorted_heap';

在数据库中创建扩展。

CREATE EXTENSION pg_sorted_heap;

用法

pg_sorted_heap 增加了 sorted_heap 表访问方法、按页 zone-map 裁剪、维护辅助函数、内置 svec/hsvec 向量类型、集成进 planner 的 sorted_hnsw 索引访问方法,以及稳定版 GraphRAG 封装。上游文档说明当前发布接口支持 PostgreSQL 16、17 和 18。

Sorted Heap 表

在带有主键的表上使用 USING sorted_heap。通过 COPY 路径批量导入时会按主键排序;compaction 会对已有行做全局排序,并重建 zone map:

CREATE EXTENSION pg_sorted_heap;

CREATE TABLE events (
  ts timestamptz,
  src text,
  data jsonb,
  PRIMARY KEY (ts, src)
) USING sorted_heap;

COPY events FROM '/path/to/events.csv';

SELECT sorted_heap_compact('events'::regclass);

EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM events
WHERE ts BETWEEN '2026-01-01' AND '2026-01-02'
  AND src = 'sensor-42';

README 描述了针对主键谓词由 planner 注入的 SortedHeapScan 路径,以及在 heap block 粒度上的 zone-map 裁剪。

维护与观测

稳定维护函数包括:

SELECT sorted_heap_compact('events'::regclass);
CALL sorted_heap_compact_online('events'::regclass);

SELECT sorted_heap_merge('events'::regclass);
CALL sorted_heap_merge_online('events'::regclass);

SELECT sorted_heap_rebuild_zonemap('events'::regclass);
SELECT sorted_heap_zonemap_stats('events'::regclass);

分区辅助函数作用于父表下具体的 sorted-heap 叶子分区:

SELECT * FROM sorted_heap_partition_status('events_parent'::regclass);
SELECT * FROM sorted_heap_partition_maintenance_plan('events_parent'::regclass, 'compact');
SELECT * FROM sorted_heap_compact_partitions('events_parent'::regclass);

向量搜索

稳定向量 API 包括 float32 向量 svec(dim)、float16 向量 hsvec(dim),以及 sorted_hnsw 索引访问方法:

CREATE TABLE documents (
  id bigserial PRIMARY KEY,
  embedding svec(384),
  content text
);

CREATE INDEX documents_embedding_idx
ON documents USING sorted_hnsw (embedding)
WITH (m = 16, ef_construction = 200);

SET sorted_hnsw.ef_search = 96;

SELECT id, content
FROM documents
ORDER BY embedding <=> '[0.1,0.2,0.3]'::svec
LIMIT 10;

如果希望基表存储更紧凑,可以使用 hsvec 和对应的 operator class:

CREATE TABLE documents_compact (
  id bigserial PRIMARY KEY,
  embedding hsvec(384),
  content text
);

CREATE INDEX documents_compact_embedding_idx
ON documents_compact USING sorted_hnsw (embedding hsvec_cosine_ops)
WITH (m = 16, ef_construction = 200);

共享解码图缓存由 sorted_hnsw.shared_cache 控制。上游示例特别说明,使用它需要预加载该扩展:

shared_preload_libraries = 'pg_sorted_heap'
SET sorted_hnsw.shared_cache = on;

GraphRAG

稳定版 fact-shaped GraphRAG 入口要求事实数据按 (entity_id, relation_id, target_id) 聚簇,或先注册一组别名映射:

CREATE TABLE facts (
  entity_id int4,
  relation_id int2,
  target_id int4,
  embedding svec(384),
  payload text,
  PRIMARY KEY (entity_id, relation_id, target_id)
) USING sorted_heap;

CREATE INDEX facts_embedding_idx
ON facts USING sorted_hnsw (embedding)
WITH (m = 24, ef_construction = 200);

SET sorted_hnsw.ef_search = 128;

SELECT *
FROM sorted_heap_graph_rag(
  'facts'::regclass,
  '[0.1,0.2,0.3]'::svec,
  relation_path := ARRAY[1, 2],
  ann_k := 64,
  top_k := 10,
  score_mode := 'path'
);

如果事实表使用了不同的列名,可以注册一次替代映射:

SELECT sorted_heap_graph_register(
  'facts_alias'::regclass,
  entity_column := 'src_id',
  relation_column := 'edge_type',
  target_column := 'dst_id',
  embedding_column := 'vec',
  payload_column := 'body'
);

对于按路由或租户分片的事实表,可使用 sorted_heap_graph_route(...),并通过 sorted_heap_graph_route_plan(...) 查看路由计划。

稳定 GUC

  • sorted_heap.enable_scan_pruning:启用 sorted-heap custom scan 裁剪;默认 on。
  • sorted_heap.vacuum_rebuild_zonemap:在 VACUUM 期间重建 zone map;默认 off。
  • sorted_heap.lazy_update:推迟即时 zone-map 更新维护;默认 off。
  • sorted_hnsw.ef_search:运行时 HNSW 搜索宽度;默认 64。
  • sorted_hnsw.shared_cache:预加载后使用的共享解码图缓存;默认 on。
  • sorted_hnsw.sq8:SQ8 解码缓存表示;默认 on。
  • sorted_hnsw.build_sq8:低内存索引构建模式;默认 off。

注意事项

  • sorted_heap.lazy_update = on 会牺牲 scan pruning,换取 update-heavy 工作负载下更快的更新,直到 compaction 或 merge 恢复裁剪效果。
  • sorted_hnsw.shared_cache 应与 shared_preload_libraries = 'pg_sorted_heap' 配合使用。
  • planner 集成的 sorted_hnsw 有序扫描需要 LIMIT;SQL API 说明,在没有 limit 或 LIMIT > sorted_hnsw.ef_search 时不会选择它们。
  • 底层 GraphRAG 与旧式/手工 ANN 辅助函数仍有文档,但稳定的应用侧 API 是 docs/api-stable.md 中的精简接口。

表格阅读器