Repository navigation
Create ClickHouse vector indexes with CREATE INDEX - #418
iskakaushik wants to merge 1 commit into
Conversation
a4fea6f to
a35680c
Compare
a35680c to
b5a622e
Compare
2dac370 to
232b80e
Compare
232b80e to
cf68996
Compare
cf68996 to
ae115ac
Compare
Let users create ClickHouse vector similarity indexes with ordinary Postgres DDL: ```sql CREATE INDEX items_embedding ON items USING clickhouse (embedding vector_cosine_ops) WITH (quantization = 'bf16'); ``` A `ProcessUtility` hook in the new `src/vector_ddl.c` intercepts `CREATE INDEX ... USING clickhouse` on pg_clickhouse foreign tables and runs `ALTER TABLE ... ADD INDEX ... TYPE vector_similarity(...)` on ClickHouse, then materializes the index for existing data. The operator class selects the distance function, the column's `vector(n)` type modifier the dimensions, and `WITH` the quantization and HNSW build parameters. `CONCURRENTLY` returns without waiting for the build. Columns without a vector operator class are ignored, since ClickHouse can filter on any column. Other access methods on foreign tables still fail, with a hint to use `clickhouse`. The hook also handles `DROP INDEX`, `REINDEX INDEX`, and `REINDEX TABLE`. Postgres knows nothing of these indexes, so the hook finds an index by name among the foreign tables in the schemas on the `search_path`, using the cached metadata that `vector_meta.c` reads from ClickHouse. It locks each candidate table before reading it, in case a concurrent `DROP` removes it. It refuses a name that indexes on two different remote tables share, and checks every name and owner in a `DROP INDEX` list before changing ClickHouse, since ClickHouse DDL cannot roll back. Names Postgres resolves itself, including a missing schema with `IF EXISTS`, go to Postgres as before. Quote index names and options as ClickHouse reads them, rather than as Postgres would, so that a backslash survives. Only the owner of the foreign table may run these commands, and they require ClickHouse 25.8 or later.
ae115ac to
9845a88
Compare
|
|
||
| /* Quote an identifier for ClickHouse, which escapes backslashes in quotes too */ | ||
| static const char* | ||
| ch_ident(const char* name) { |
There was a problem hiding this comment.
can use pgch_quote_ch_ident instead
theory
left a comment
There was a problem hiding this comment.
Not a complete review, just a few comments. Will review properly once no longer a draft.
|
|
||
| ##### Vector Indexes | ||
|
|
||
| Use `CREATE INDEX ... USING clickhouse` on a foreign table to create a |
There was a problem hiding this comment.
| Use `CREATE INDEX ... USING clickhouse` on a foreign table to create a | |
| Use `CREATE INDEX ... USING clickhouse` on a `clickhouse_fdw` foreign table to create a |
| ClickHouse [vector similarity index] and build it for existing data. The | ||
| operator class selects the distance function: | ||
|
|
||
| ```pgsql |
There was a problem hiding this comment.
| ```pgsql | |
| ```sql |
| pg_clickhouse ignores columns without a vector operator class, since | ||
| ClickHouse can filter on any column. |
| These commands work only once pg_clickhouse is loaded, so set | ||
| [`session_preload_libraries`](#session_preload_libraries) or run `LOAD | ||
| 'pg_clickhouse'` first. ClickHouse DDL is not transactional: rolling back a |
There was a problem hiding this comment.
| These commands work only once pg_clickhouse is loaded, so set | |
| [`session_preload_libraries`](#session_preload_libraries) or run `LOAD | |
| 'pg_clickhouse'` first. ClickHouse DDL is not transactional: rolling back a | |
| These commands work only once pg_clickhouse is loaded, so [preload it](#preloading), first. ClickHouse DDL is not transactional: rolling back a |
| These commands work only once pg_clickhouse is loaded, so set | ||
| [`session_preload_libraries`](#session_preload_libraries) or run `LOAD | ||
| 'pg_clickhouse'` first. ClickHouse DDL is not transactional: rolling back a |
There was a problem hiding this comment.
| These commands work only once pg_clickhouse is loaded, so set | |
| [`session_preload_libraries`](#session_preload_libraries) or run `LOAD | |
| 'pg_clickhouse'` first. ClickHouse DDL is not transactional: rolling back a | |
| These commands work only once pg_clickhouse is loaded, so [preload it](#preloading), first. ClickHouse DDL is not transactional: rolling back a |
| < '{25,8}'::int[] AS old_ch \gset | ||
| \set ECHO all | ||
| \if :old_ch | ||
| \echo 'SKIP: vector similarity indexes require ClickHouse 25.8' |
There was a problem hiding this comment.
Should drop the user mapping and server.
| -- The operator class picks the metric, WITH the build options, and the column | ||
| -- type the dimensions. Other columns are ignored: ClickHouse filters on any. |
There was a problem hiding this comment.
I cannot parse this comment.
| -- type the dimensions. Other columns are ignored: ClickHouse filters on any. | ||
| CREATE INDEX ddl_docs_v ON ddl_docs USING clickhouse (id, v vector_cosine_ops) | ||
| WITH (quantization = 'f32', m = 16, ef_construction = 64); | ||
| NOTICE: pg_clickhouse: ignoring non-vector index column |
There was a problem hiding this comment.
What column? Both the columns in the table appear to be used in the CREATE INDEX statement, so why would it ignore one?
| CREATE INDEX ddl_docs_v ON ddl_docs USING clickhouse (id, v vector_cosine_ops) | ||
| WITH (quantization = 'f32', m = 16, ef_construction = 64); | ||
| NOTICE: pg_clickhouse: ignoring non-vector index column | ||
| SELECT * FROM ch_indexes; |
There was a problem hiding this comment.
I guess we can't show the indexes on the foreign table without another hook.
| Foreign Scan on ddl_docs | ||
| Vector Search: cosineDistance on v | ||
| Vector Index: ddl_docs_v |
What
Lets users create ClickHouse vector similarity indexes with ordinary Postgres DDL:
Part of the vector search stack; it sits on #414.
How it works
src/vector_ddl.cinstalls aProcessUtilityhook. ForCREATE INDEX ... USING clickhouseon a pg_clickhouse foreign table, it runsALTER TABLE ... ADD INDEX ... TYPE vector_similarity('hnsw', <metric>, <dims>[, <quantization>, <m>, <ef_construction>])on ClickHouse, thenMATERIALIZE INDEXwithmutations_sync = 2.CONCURRENTLYusesmutations_sync = 0.vector_l2_ops,vector_cosine_ops,vector_ip_ops), and the column'svector(n)type modifier sets the dimensions, unlessWITH (dimensions = n)is given. Columns without a vector operator class get a NOTICE and are ignored.DROP INDEXandREINDEX INDEXfind an index by name in the cached ClickHouse metadata from Report vector index use for pgvector searches #414, among the foreign tables in the schemas on thesearch_path. A name that Postgres resolves itself, including a missing schema withIF EXISTS, goes to Postgres untouched.clickhouse.Notes for review
DROP INDEXlist is checked completely, every name and every owner, before anything changes on ClickHouse. A list that mixes ClickHouse and Postgres indexes is an error.DROPremoves it. A name that indexes on two different remote tables share is an error. Two foreign tables of one remote table share its indexes.quote_literal_cstr(), which can produceE''strings and handles backslashes differently.dotProductindexes require 26.8.session_preload_librariesorLOAD); the docs say so. Without it, Postgres reports its generic "not supported for foreign tables" error.lookup_index()may contact every ClickHouse server reachable through foreign tables on thesearch_pathwhen an index name is unknown to Postgres.alter_remote_table()skipped releasing its connection lease, so each such error caught in PL/pgSQL opened another connection, held until the transaction ended (one socket per error). It now releases the lease inPG_FINALLY.