Skip to content

Create ClickHouse vector indexes with CREATE INDEX - #418

Draft
iskakaushik wants to merge 1 commit into
mainfrom
vector-index-ddl
Draft

iskakaushik wants to merge 1 commit into
mainfrom
vector-index-ddl

Conversation

@iskakaushik

@iskakaushik iskakaushik commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

What

Lets users create ClickHouse vector similarity indexes with ordinary Postgres DDL:

CREATE INDEX items_embedding ON items USING clickhouse (embedding vector_cosine_ops)
  WITH (quantization = 'bf16', m = 32, ef_construction = 128);
REINDEX INDEX items_embedding;
DROP INDEX items_embedding;

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

How it works

  • src/vector_ddl.c installs a ProcessUtility hook. For CREATE INDEX ... USING clickhouse on a pg_clickhouse foreign table, it runs ALTER TABLE ... ADD INDEX ... TYPE vector_similarity('hnsw', <metric>, <dims>[, <quantization>, <m>, <ef_construction>]) on ClickHouse, then MATERIALIZE INDEX with mutations_sync = 2. CONCURRENTLY uses mutations_sync = 0.
  • The operator class selects the metric (vector_l2_ops, vector_cosine_ops, vector_ip_ops), and the column's vector(n) type modifier sets the dimensions, unless WITH (dimensions = n) is given. Columns without a vector operator class get a NOTICE and are ignored.
  • Postgres stores nothing. DROP INDEX and REINDEX INDEX find 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 the search_path. A name that Postgres resolves itself, including a missing schema with IF EXISTS, goes to Postgres untouched.
  • Other access methods on foreign tables still fail, now with a hint to use clickhouse.

Notes for review

  • Non-transactional DDL. ClickHouse DDL can't roll back, so a DROP INDEX list is checked completely, every name and every owner, before anything changes on ClickHouse. A list that mixes ClickHouse and Postgres indexes is an error.
  • Name lookup. Each candidate table is locked before it's read, in case a concurrent DROP removes it. A name that indexes on two different remote tables share is an error. Two foreign tables of one remote table share its indexes.
  • Quoting. Index names and options are quoted the way ClickHouse reads them, not with quote_literal_cstr(), which can produce E'' strings and handles backslashes differently.
  • Ownership and versions. Only the foreign table's owner may run these commands, and they require ClickHouse 25.8. dotProduct indexes require 26.8.
  • Limitation. The hook works only once the library is loaded (session_preload_libraries or LOAD); the docs say so. Without it, Postgres reports its generic "not supported for foreign tables" error.
  • Open question. lookup_index() may contact every ClickHouse server reachable through foreign tables on the search_path when an index name is unknown to Postgres.
  • Fixed a connection leak: when ClickHouse rejected the DDL, 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 in PG_FINALLY.

@iskakaushik
iskakaushik added this pull request to stack #425 October 15, 2026 23:33
@iskakaushik
iskakaushik force-pushed the vector-index-ddl branch 2 times, most recently from 2dac370 to 232b80e Compare October 16, 2026 15:20
@theory theory added enhancement New feature or request pushdown Improvements to query pushdown data types Improve data type support labels Oct 19, 2026
Base automatically changed from vector-index-use to main October 19, 2026 20:47
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.
Comment thread src/vector_ddl.c

/* Quote an identifier for ClickHouse, which escapes backslashes in quotes too */
static const char*
ch_ident(const char* name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use pgch_quote_ch_ident instead

@theory theory left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a complete review, just a few comments. Will review properly once no longer a draft.

Comment thread doc/pg_clickhouse.md

##### Vector Indexes

Use `CREATE INDEX ... USING clickhouse` on a foreign table to create a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment thread doc/pg_clickhouse.md
ClickHouse [vector similarity index] and build it for existing data. The
operator class selects the distance function:

```pgsql

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```pgsql
```sql

Comment thread doc/pg_clickhouse.md
Comment on lines +1999 to +2000
pg_clickhouse ignores columns without a vector operator class, since
ClickHouse can filter on any column.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

Comment thread doc/pg_clickhouse.md
Comment on lines +2009 to +2011
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment thread doc/pg_clickhouse.md
Comment on lines +2009 to +2011
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should drop the user mapping and server.

Comment on lines +42 to +43
-- The operator class picks the metric, WITH the build options, and the column
-- type the dimensions. Other columns are ignored: ClickHouse filters on any.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can't show the indexes on the foreign table without another hook.

Comment on lines +57 to +59
Foreign Scan on ddl_docs
Vector Search: cosineDistance on v
Vector Index: ddl_docs_v

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the Remote 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.

3 participants