Skip to content

Return pgvector distances from ClickHouse - #422

Draft
iskakaushik wants to merge 1 commit into
fix-float-sort-nullsfrom
vector-sort-values
Draft

iskakaushik wants to merge 1 commit into
fix-float-sort-nullsfrom
vector-sort-values

Conversation

@iskakaushik

@iskakaushik iskakaushik commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

What

Makes indexed nearest neighbor searches about ten times faster. A search used to fetch each row's vector, only so that Postgres could compute the distance for the scan output after ClickHouse had already sorted by it. Reading the vector column keeps ClickHouse from answering from its vector similarity index alone.

On 100,000 1024-dimension vectors (the #424 benchmark), an indexed top-10 search took:

p50
ClickHouse, SELECT id 4 ms
ClickHouse, SELECT id, emb 40 ms
pg_clickhouse before 42 ms
pg_clickhouse after 3 ms

Remote SQL before and after:

SELECT id, v FROM items ORDER BY cosineDistance(v, ...) ASC NULLS LAST LIMIT 10
SELECT id, cosineDistance(v, ...) FROM items ORDER BY cosineDistance(v, ...) ASC NULLS LAST LIMIT 10

Part of the vector search stack; it sits on #421.

How it works

  • When a base relation scan sorts by a pgvector distance, sort_scan_tlist() in src/fdw.c gives it an explicit fdw_scan_tlist. That list holds the distance plus the columns the scan otherwise needs: those in the relation's target, the plan's target list, and its local and recheck conditions. setrefs then matches the output's distance expression to the scan tlist entry, so Postgres reads the value instead of computing it.
  • deparseSelectSql() deparses an explicit target list for such a scan.
  • clickhouseBeginForeignScan() takes the tuple descriptor from the scan slot whenever there's a scan tlist, as it already does for joins.

Notes for review

  • Only pgvector distances. ClickHouse computes them as pgvector does, up to float rounding. Other sort expressions stay local: some push down as approximations, such as to_char() as formatDateTime(), whose output follows ClickHouse's time zone, so taking their values from ClickHouse would change results.
  • Fallbacks. Parameterized paths, and scans that need whole-row or system columns, keep fetching columns.
  • Recheck columns. Columns in pushed-down WHERE conditions are still fetched, as in SELECT category, id, ..., because fdw_recheck_quals needs them for EPQ.
  • Tests. A local join above a distance-sorted scan, whose plan target list names every column, which exercises the new code path. A partitionwise join that outputs no columns is already covered by main's partitioning_binary.sql, so this PR adds no copy of it.
  • Expected output. Most of the diff is vector_pushdown alternates, where the Remote SQL now selects the distance.

@iskakaushik
iskakaushik added this pull request to stack #425 October 15, 2026 23:33
@iskakaushik
iskakaushik force-pushed the vector-sort-values branch 2 times, most recently from 93116c3 to f352e17 Compare October 15, 2026 23:44
@theory theory added enhancement New feature or request pushdown Improvements to query pushdown data types Improve data type support labels Oct 19, 2026
A nearest neighbor search fetched each row's vector so that Postgres could
compute the distance for the scan output, even though ClickHouse had
already sorted by it. Reading the vector column keeps ClickHouse from
serving the search from a vector similarity index alone. On 100,000
1024-dimension vectors, an indexed top-10 search took 4 ms in ClickHouse,
but 40 ms when it also returned the vectors, as through pg_clickhouse.

So when a base relation scan sorts by a pgvector distance, give it an
explicit scan target list, `fdw_scan_tlist`, with the distance and the
columns it otherwise needs: those in the relation's target and the plan's
target list, and those in its local and recheck conditions. ClickHouse
then returns the distance instead of the vector, and the same search
through pg_clickhouse takes 3 ms. ClickHouse computes distances as
pgvector does, up to float rounding. Other sort expressions stay local,
since ClickHouse sorts by approximations of some of them, such as
`to_char()`. Scans with parameterized paths, or that need whole-row or
system columns, still fetch the columns.

`deparseSelectSql()` deparses the explicit target list for such a scan,
and `clickhouseBeginForeignScan()` takes the scan's tuple descriptor from
its slot, as it does for joins.

Test a local join above a distance-sorted scan, whose plan target list
names every column. A partitionwise join that outputs no columns is
already tested in `partitioning_binary.sql`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data types Improve data type support enhancement New feature or request pushdown Improvements to query pushdown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants