Skip to content

pg_sorted_heap

Sorted heap table AM with zone map scan pruning and built-in vector search

pg_sorted_heap · Sorted heap table AM with zone map scan pruning and built-in vector search

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2550pg_sorted_heappg_sorted_heap0.14.0OLAPPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd--NoYesYesYesnono
sorted_hnsw.shared_cache requires shared_preload_libraries=pg_sorted_heap.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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

Source

pig build pkg pg_sorted_heap;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with 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

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_sorted_heap';

Create this extension with:

CREATE EXTENSION pg_sorted_heap;

Usage

Sources: pg_sorted_heap README, stable API, SQL API, control file.

pg_sorted_heap adds the sorted_heap table access method, per-page zone-map pruning, maintenance helpers, built-in svec/hsvec vector types, a planner-integrated sorted_hnsw index AM, and stable GraphRAG wrappers. Upstream documents PostgreSQL 16, 17, and 18 support for the current release surface.

Sorted Heap Tables

Use USING sorted_heap on tables with a primary key. Bulk loads are sorted by primary key on the COPY path, and compaction globally sorts existing rows while rebuilding the 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';

The README describes planner-injected SortedHeapScan paths for primary-key predicates and zone-map pruning at the heap-block level.

Maintenance And Observability

Stable maintenance functions include:

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);

Partition helpers operate on concrete sorted-heap leaves under a parent:

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);

The stable vector API includes svec(dim) for float32 vectors, hsvec(dim) for float16 vectors, and the sorted_hnsw index AM:

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;

For compact base-table storage, use hsvec and the matching 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);

The shared decoded graph cache is controlled by sorted_hnsw.shared_cache. Upstream examples note that using it requires preloading the extension:

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

GraphRAG

The stable fact-shaped GraphRAG entry point expects facts clustered by (entity_id, relation_id, target_id) or a registered alias mapping:

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'
);

Register alternate fact column names once:

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'
);

For routed or tenant-sharded fact tables, use sorted_heap_graph_route(...) and inspect routing with sorted_heap_graph_route_plan(...).

Stable GUCs

  • sorted_heap.enable_scan_pruning: enable sorted-heap custom scan pruning; default on.
  • sorted_heap.vacuum_rebuild_zonemap: rebuild zone maps during VACUUM; default off.
  • sorted_heap.lazy_update: defer eager zone-map update maintenance; default off.
  • sorted_hnsw.ef_search: runtime HNSW search breadth; default 64.
  • sorted_hnsw.shared_cache: shared decoded graph cache when preloaded; default on.
  • sorted_hnsw.sq8: SQ8 decoded cache representation; default on.
  • sorted_hnsw.build_sq8: low-memory index build mode; default off.

Caveats

  • sorted_heap.lazy_update = on trades scan pruning for faster update-heavy workloads until compaction or merge restores pruning.
  • sorted_hnsw.shared_cache should be used with shared_preload_libraries = 'pg_sorted_heap'.
  • Planner-integrated sorted_hnsw ordered scans require LIMIT; the SQL API says they are not chosen when there is no limit or when LIMIT > sorted_hnsw.ef_search.
  • The lower-level GraphRAG and legacy/manual ANN helpers remain documented, but the stable application-facing API is the compact surface in docs/api-stable.md.

Table reader