Repository navigation
Return pgvector distances from ClickHouse - #422
Draft
iskakaushik wants to merge 1 commit into
Draft
iskakaushik wants to merge 1 commit into
iskakaushik wants to merge 1 commit into
Conversation
iskakaushik
force-pushed
the
vector-sort-values
branch
from
October 15, 2026 23:34
742a30d to
b7ebce7
Compare
This was referenced Oct 15, 2026
iskakaushik
force-pushed
the
vector-sort-values
branch
2 times, most recently
from
October 15, 2026 23:44
93116c3 to
f352e17
Compare
iskakaushik
force-pushed
the
vector-sort-values
branch
from
October 16, 2026 15:20
f352e17 to
84b2e9d
Compare
iskakaushik
force-pushed
the
vector-sort-values
branch
from
October 16, 2026 17:01
84b2e9d to
3de7a26
Compare
iskakaushik
force-pushed
the
vector-sort-values
branch
from
October 19, 2026 15:02
3de7a26 to
a437541
Compare
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`.
iskakaushik
force-pushed
the
vector-sort-values
branch
from
October 19, 2026 21:03
a437541 to
d9cece7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
SELECT idSELECT id, embRemote SQL before and after:
Part of the vector search stack; it sits on #421.
How it works
sort_scan_tlist()insrc/fdw.cgives it an explicitfdw_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.setrefsthen 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
to_char()asformatDateTime(), whose output follows ClickHouse's time zone, so taking their values from ClickHouse would change results.WHEREconditions are still fetched, as inSELECT category, id, ..., becausefdw_recheck_qualsneeds them for EPQ.partitioning_binary.sql, so this PR adds no copy of it.vector_pushdownalternates, where the Remote SQL now selects the distance.