Repository navigation
Report vector index use for pgvector searches - #414
Conversation
296025b to
833db58
Compare
833db58 to
1f2ff5b
Compare
5ab7b67 to
4ded1d0
Compare
| makeString(sql.data), | ||
| retrieved_attrs, | ||
| settings, | ||
| chfdw_vector_plan_info(root, foreignrel, best_path, has_limit) |
There was a problem hiding this comment.
So we bear the overhead of the vector plan info anytime a query has a vector distance expression? And that overhead requires an additional query sent to ClickHouse unless the data is in the cache?
There was a problem hiding this comment.
Yes, but only for a scan sorted by a pgvector distance, and only on a cache miss: one small query per table and user every vector_metadata_ttl seconds (60 by default). Other queries pay nothing. I can't skip it when warnings are off, because EXPLAIN also shows the index name and the plan doesn't know whether it will be explained. I documented the cost in the Vector Index Use section instead.
|
|
||
| DefineCustomBoolVariable( | ||
| "pg_clickhouse.vector_planner_warnings", | ||
| "Warn when a vector search cannot use a ClickHouse vector index.", |
There was a problem hiding this comment.
Might we want to extend this to support any index in the future?
There was a problem hiding this comment.
Possibly, but I kept the name. The checks and reasons are specific to vector similarity indexes, and someone may want to silence them apart from warnings about other indexes. If we add those later, they can get their own GUC, or this one can be folded into it.
|
|
||
| chfdw_remote_table_name(relid, &db, &table); | ||
|
|
||
| /* A failed lookup must not fail the query, so run it in a subtransaction */ |
There was a problem hiding this comment.
So we call this function and start a sub transaction any time the planner only when ordering by a pgvector column and when EXPLAINing the plan or explaining the failure to use an index in a warning? How does it know when it can't use an index?
There was a problem hiding this comment.
Yes. The planner calls it only when a scan is sorted by a pgvector distance, and it starts a subtransaction only on a cache miss, so once per table and user per TTL. It decides that an index can't be used in fallback_reason(), from the query shape (pushdown, LIMIT, sort keys, direction) and the indexes that ClickHouse reports in system.data_skipping_indices. I added the cost to the docs.
69fcdad to
d03489b
Compare
e908ec9 to
8fd2b10
Compare
8fd2b10 to
bd0c835
Compare
ClickHouse serves a nearest neighbor search from a vector similarity index only for some query shapes: a single ascending sort by the index's distance function, with a `LIMIT` no higher than `max_limit_for_vector_search_queries`, on ClickHouse 25.8 or later. Otherwise it compares every row, and nothing tells the user. So teach the planner to recognize these searches. For a scan sorted by a pgvector distance on a foreign table column, `chfdw_vector_plan_info()` checks the query shape against the indexes ClickHouse reports in `system.data_skipping_indices` and records the result in `fdw_private`. `EXPLAIN` then shows the distance function, the index, and, when no index applies, why not as `Vector Index Not Used`. The planner also warns in that case, with the reason in the `DETAIL`, once per server for old servers. Set `pg_clickhouse.vector_planner_warnings` to false to silence the warnings. pg_clickhouse stores no index metadata. It reads it from ClickHouse in a subtransaction, so that a failure, such as an unreachable server, leaves the search unclassified rather than failing the query. It does so only when planning such a search, and caches the result per backend for `pg_clickhouse.vector_metadata_ttl` seconds. Since only vector metadata is cached, `clickhouse_refresh_vector_metadata()` clears the cache. The new function requires extension version 0.12. ClickHouse has no `unquoteIdentifier()` in any version we test, so the query strips the backticks it puts around column names that need them with `replaceRegexpAll()`. Move the text-row query out of `chfdw_construct_create_tables()` into `chfdw_run_text_query()` and use it for both, rather than add a second implementation. Also export `chfdw_remote_table_name()` and `chfdw_rel_user_mapping()` from `deparse.c` for the metadata lookup, silence the warnings in `vector_pushdown.sql`, which tests pushdown rather than index use, and add `vector_pushdown_3.out` for ClickHouse 23.8 through 25.3, where `EXPLAIN` reports that the server is too old for indexes. Adapt `vector_distance()` to the operators that deparse as ClickHouse functions. Co-authored-by: Philip Dubé <159546+serprex@users.noreply.github.com>
bd0c835 to
45e260a
Compare
What
ClickHouse serves a nearest neighbor search from a vector similarity index only for some query shapes. Otherwise it compares every row, and nothing tells the user. This PR makes pg_clickhouse report which case a pgvector search falls into:
It adds:
Vector Search,Vector Index, andVector Index Not Usedlines inEXPLAINDETAIL, silenced bypg_clickhouse.vector_planner_warnings = falsepg_clickhouse.vector_metadata_ttl(default 60 seconds) andclickhouse_refresh_vector_metadata()for the index metadata cacheHow it works
src/vector_plan.c:chfdw_vector_plan_info()runs inclickhouseGetForeignPlan()for a base rel whose first query pathkey is a pgvector distance on one of its columns. It checks, in order: whether the ORDER BY pushed down, the server version (25.8+), whether aLIMITpushed down, the number of sort keys, the direction (<#>sorts asdotProduct DESC), an index on the column with the same metric, andmax_limit_for_vector_search_queries. The result goes in a newfdw_privateslot after the settings slot, forEXPLAIN.src/vector_meta.c: readssystem.data_skipping_indicesand the effectivemax_limit_for_vector_search_queriesin one query. It runs in an internal subtransaction, so any error, including an unreachable server, leaves the search unclassified instead of failing planning. Results are cached per backend inCacheMemoryContext, keyed by foreign table and user. The lookup happens only when planning a scan sorted by a pgvector distance, and only on a cache miss, so it costs one small query per table and user per TTL. ClickHouse has nounquoteIdentifier()in any version CI tests, so the query strips the backticks it puts around some column names withreplaceRegexpAll().clickhouse_refresh_vector_metadata().EXPLAINand the warning. It never changes the remote SQL or the results.Notes for review
chfdw_run_text_query()now lives inpglink.candchfdw_construct_create_tables()uses it too, rather than keeping two copies.sql/pg_clickhouse--0.11--0.12.sql. The CHANGELOG section is still titled v0.11.1; it should probably become v0.12.0, but I left that to you.warned_old_servers). The others are issued at each planning, like other planner warnings.vector_pushdown.sqlturns the warnings off, since it tests pushdown, and gainsvector_pushdown_3.outfor ClickHouse 23.8–25.3, whereEXPLAINreports that the server is too old for indexes.vector_index_use.sqlcreates its indexes directly in ClickHouse and covers each fallback reason, cache refresh with a long TTL, a column name ClickHouse quotes in backticks, and a server that is down.