pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #1015: Proposal: Optional local-scaling construction mode for HNSW
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #1015: Proposal: Optional local-scaling construction mode for HNSW
@ 2026-08-11 03:15  "LeoWannaZzz (@LeoWannaZzz)" <noreply+LeoWannaZzz@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: LeoWannaZzz (@LeoWannaZzz) @ 2026-08-11 03:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

# Proposal: Optional Local-Scaling Graph Construction for pgvector HNSW

> Public implementation: <https://github.com/LeoWannaZzz/pgvector-lsg;
>
> Current research base: pgvector 0.8.0

## Summary

We have implemented Unified Local-Scaling Graph (LSG) as a lightweight, opt-in construction enhancement inside pgvector's existing HNSW access method. A user can try it by adding a single index option, `use_lsg=true`; the two advanced parameters have defaults and do not need to be tuned.

The proposal does not add a new index access method, query operator or query syntax. Native HNSW remains the default, and the LSG-specific branch and tuple extension are used only by an index explicitly built with LSG. Query scans continue to use the existing operator distance and HNSW scan interface, with no local-scaling calculation added to the query path.

In our current static benchmarks, this small opt-in change reached the same or higher best observed Recall@10 than native HNSW on all four evaluated datasets, with higher final recall or earlier convergence on several of them. The principal static-index tradeoff is additional construction time, which is paid once for an index that is then queried repeatedly. Runtime INSERT currently also has an additional cost, which we disclose below.

Before doing additional upstream-specific development, we would like to ask whether this feature shape and storage direction are acceptable to the maintainers.

## Research background and intent

LSG is described in our ICDE 2025 paper:

> Hongya Wang, Wenlong Wu, Cong Luo, Aobei Bian, Chunguang Meng, Yishuo Wu and Ji Sun. [“Boosting Accuracy and Efficiency for Vector Retrieval with Local Scaling Graph.”](https://doi.org/10.1109/ICDE65448.2025.00032) IEEE ICDE 2025, pp. 336–348.

Our earlier algorithm validation used a Faiss LSG reference implementation. This project explores a different integration boundary: bringing the same local-scaling idea into PostgreSQL through pgvector's existing HNSW implementation, including index-local configuration, disk pages, WAL, parallel build, INSERT and VACUUM behavior.

We believe the research result is useful and that the current pgvector experiments justify exploring it on more vector platforms. Our purpose in contacting pgvector is therefore not just to publish another standalone implementation, but to ask whether a small, maintainable pgvector integration could be useful to its users. If the maintainers are interested, we are willing to adapt both the design and implementation to the project's expectations.

## Motivation

HNSW uses the operator-class distance directly when selecting graph connections. On datasets with uneven local density, the same raw distance can represent different neighborhood scales. Our implementation uses a per-element local statistic, `mu`, to normalize construction distance:

```text
d_lsg(a, b) = d(a, b)^(2 * alpha) / (mu_a * mu_b)^alpha
```

The default `alpha = 0.5` gives `d(a, b) / sqrt(mu_a * mu_b)`. This changes graph construction and maintenance only; it does not introduce a new SQL distance operator.

The current Unified LSG layout uses:

- the existing HNSW access method and scan path;
- an approximately 1% sampled Level 1 graph for navigation and local-scale estimation;
- all indexed elements on Level 0;
- the sampled element with the largest local scale as the entry point;
- LSG distance for Level 0 graph construction and the original operator distance for Level 1 and user queries.

## Proposed user interface

The minimal user interface is one opt-in switch:

```sql
CREATE INDEX items_embedding_lsg_idx
ON items USING hnsw (embedding vector_l2_ops)
WITH (use_lsg = true);
```

Two index-local advanced options are available for experiments or tuning, but are not required for normal use:

```sql
CREATE INDEX items_embedding_lsg_idx
ON items USING hnsw (embedding vector_l2_ops)
WITH (
    use_lsg = true,
    lsg_alpha = 0.5,
    lsg_top_k = 10
);
```

The defaults are:

| Option | Default | Meaning |
|:---|---:|:---|
| `use_lsg` | `false` | Keep native HNSW unless explicitly enabled |
| `lsg_alpha` | `0.5` | Local-scaling exponent |
| `lsg_top_k` | `10` | Number of sampled neighbors used for `mu` |

The reloptions are stored on each index. Build, INSERT, UPDATE and VACUUM read the configuration of the specific index rather than session-global state. Setting only `use_lsg=true` uses `lsg_alpha=0.5` and `lsg_top_k=10`.

## Isolation from native HNSW

The implementation is deliberately guarded so that disabling LSG preserves the native HNSW path:

- `use_lsg=false` remains the default;
- native HNSW element tuples keep their original size and vector offset;
- native indexes do not store an LSG trailer or metapage feature;
- vector, halfvec, bit and sparsevec continue through their native HNSW paths;
- existing HNSW parallel build, scan, INSERT, VACUUM and WAL mechanisms are reused;
- the LSG build and maintenance branches are entered only for an index whose physical metapage identifies the LSG format.

The current preview rejects `use_lsg=true` for non-vector operator classes with a deterministic error. It does not alter native support for those types. The public preview includes regression and lifecycle tests specifically covering this isolation rather than relying only on the default value as an assumption.

## Storage and lifecycle

Each LSG element needs its local-scale value during on-disk build, runtime INSERT and VACUUM graph repair. The current design therefore uses:

- an independent metapage feature flag for the LSG physical format;
- an 8-byte `avgdis` trailer after the existing vector payload for LSG tuples only;
- tuple-length and feature validation before reading the trailer;
- `memcpy` for the potentially unaligned trailer value;
- the existing Generic WAL and buffer-locking paths for page changes.

Changing an existing index between native HNSW and LSG requires rebuilding the index, since graph connections and per-element local statistics must be recomputed. Users decide when to rebuild based on their own application-level retrieval-quality requirements. Project documentation can provide empirical operational guidance, while physical format conversion remains an explicit, user-directed rebuild operation.

## Preliminary evidence

Our current experiments compare native HNSW and Unified LSG with the same vector type, dataset, `m`, `ef_construction`, worker count and `ef_search` sweep.

The following earlier static benchmark is preliminary and is included only to show the observed direction before asking for upstream guidance:

| Dataset | Native HNSW best observed Recall@10 | Unified LSG best observed Recall@10 | Observed convergence point |
|:---|---:|---:|:---|
| Audio | 1.00 | 1.00 | LSG reached 1.00 at `ef_search=100`; HNSW at 200 |
| Enron | 0.98 | 0.99 | LSG reached 0.99 at 300; HNSW reached 0.98 at 1000 |
| SIFT-1M | 1.00 | 1.00 | HNSW reached its maximum earlier in this run |
| GloVe-1M | 0.96 | 0.98 | LSG reached 0.98 at 700; HNSW reached 0.96 at 1000 |

We also tested cumulative INSERT from 50% to 100% of four datasets after fixing two metric-consistency bugs in the on-disk construction path. At the 100% checkpoint with `ef_search=1000`, Recall@10 was:

| Dataset | Unified LSG | Native HNSW |
|:---|---:|---:|
| Audio | 1.000 | 1.000 |
| Enron | 0.994 | 0.995 |
| GloVe-1M | 0.945 | 0.915 |
| SIFT-1M | 0.976 | 0.998 |

These dynamic results show that the implementation no longer has the earlier catastrophic post-INSERT recall failure. They do not establish that LSG is always better: SIFT remains 0.022 below its same-snapshot LSG REINDEX reference at the final checkpoint, and runtime INSERT is currently more expensive than native HNSW. For read-heavy or build-once workloads, the additional static construction cost is paid when the index is built rather than on every query. We will provide any additional benchmark design requested by maintainers after agreeing on the feature direction.

## Correctness work already present in the public preview

The repository includes regression and TAP coverage for:

- index-local reloptions and invalid values;
- native versus LSG tuple-format isolation;
- serial and parallel LSG builds, including the on-disk phase;
- minimum build size, duplicate handling and memory limits;
- INSERT, UPDATE, DELETE/VACUUM and REINDEX;
- entry-point replacement;
- concurrent INSERT and VACUUM;
- restart and physical WAL replica replay;
- native HNSW behavior for vector, halfvec, bit and sparsevec.

The public code is a research preview based on pgvector 0.8.0, not yet a patch against the latest upstream master. We intentionally want maintainer feedback before deciding the exact upstream port and compatibility scope.

## Current scope and willingness to follow up

The first implementation enables LSG only for `vector_l2_ops`. We chose a narrow initial scope so the algorithm, persistence and lifecycle behavior could be validated without changing unrelated HNSW modes.

If the maintainers are open to the proposal, we are willing to actively follow up on the work needed for upstream quality, including:

- rebasing and adapting the implementation to the latest pgvector master;
- supporting additional vector formats and distance operator classes where the local-scaling definition is appropriate;
- changing the reloptions or physical layout to match maintainer preferences;
- adding requested PostgreSQL-version, operating-system, architecture, concurrency and upgrade tests;
- restructuring or splitting the patch in the form preferred by maintainers.

We would treat maintainer guidance as the source of scope for that follow-up work rather than asking upstream to accept the current research snapshot unchanged.

## Questions for maintainers

1. Would pgvector consider an optional, default-off local-scaling construction mode inside the existing HNSW access method?
2. Is a first version limited to `vector + L2` an acceptable starting boundary if we commit to following up on additional distances, formats and upstream compatibility requested for acceptance?
3. Is an LSG-only element trailer plus a metapage feature flag an acceptable persistence direction, or would you prefer another layout and versioning strategy?

We would appreciate direction on these design questions before preparing a full patch against the latest upstream branch.


^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #1015: Proposal: Optional local-scaling construction mode for HNSW
@ 2026-08-19 14:15  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-08-19 14:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @LeoWannaZzz, thanks for sharing / the interest in contributing. The overall approach / design looks good, but I don't think the benefit is worth the complexity.

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #1015: Proposal: Optional local-scaling construction mode for HNSW
@ 2026-08-19 14:15  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-08-19 14:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-08-19 14:15 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-11 03:15 [pgvector/pgvector] issue #1015: Proposal: Optional local-scaling construction mode for HNSW "LeoWannaZzz (@LeoWannaZzz)" <noreply+LeoWannaZzz@github.com>
2026-08-19 14:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-08-19 14:15 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox