Skip to content

A platform for community discussion. Free, open, simple.

DEV: Revert PG Vector 0.8.0 upgrade (#35220)

Roman Rizzi0e512ec08bd2
diff --git a/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb b/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb
index 86a241cf2c9..7dc2c20df1f 100644
--- a/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb
+++ b/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb
@@ -2,13 +2,7 @@
class UpgradePgvector080 < ActiveRecord::Migration[8.0]
def up
- minimum_target_version = "0.8.0"
- installed_version =
- DB.query_single("SELECT extversion FROM pg_extension WHERE extname = 'vector';").first
-
- if Gem::Version.new(installed_version) < Gem::Version.new(minimum_target_version)
- DB.exec("ALTER EXTENSION vector UPDATE TO '0.8.0';")
- end
+ # No-op. Migration is failing with "must be owner of extension vector"
end
def down
diff --git a/plugins/discourse-ai/lib/embeddings/schema.rb b/plugins/discourse-ai/lib/embeddings/schema.rb
index 5e4ba9fa8dc..0635f3d331b 100644
--- a/plugins/discourse-ai/lib/embeddings/schema.rb
+++ b/plugins/discourse-ai/lib/embeddings/schema.rb
@@ -15,6 +15,8 @@ module DiscourseAi
EMBEDDING_TARGETS = %w[topics posts document_fragments]
EMBEDDING_TABLES = [TOPICS_TABLE, POSTS_TABLE, RAG_DOCS_TABLE]
+ DEFAULT_HNSW_EF_SEARCH = 40
+
MissingEmbeddingError = Class.new(StandardError)
class << self
@@ -135,6 +137,8 @@ module DiscourseAi
end
def asymmetric_similarity_search(embedding, limit:, offset:)
+ before_query = hnsw_search_workaround(limit)
+
builder = DB.build(<<~SQL)
WITH candidates AS (
SELECT
@@ -175,7 +179,7 @@ module DiscourseAi
end
ActiveRecord::Base.transaction do
- DB.exec(hnsw_scan_relaxed_order)
+ DB.exec(before_query) if before_query.present?
builder.query(
query_embedding: embedding,
@@ -191,6 +195,7 @@ module DiscourseAi
def symmetric_similarity_search(record, age_penalty: 0.0)
limit = 200
+ before_query = hnsw_search_workaround(limit)
# For topics table, we can apply age penalty. For other tables, ignore the penalty.
use_age_penalty = age_penalty > 0.0 && table == TOPICS_TABLE
@@ -268,7 +273,7 @@ module DiscourseAi
end
ActiveRecord::Base.transaction do
- DB.exec(hnsw_scan_relaxed_order)
+ DB.exec(before_query) if before_query.present?
builder.query(query_params)
end
@@ -303,6 +308,13 @@ module DiscourseAi
private
+ def hnsw_search_workaround(limit)
+ threshold = limit * 2
+
+ return "" if threshold < DEFAULT_HNSW_EF_SEARCH
+ "SET LOCAL hnsw.ef_search = #{threshold};"
+ end
+
def hnsw_scan_relaxed_order
# https://github.com/pgvector/pgvector?tab=readme-ov-file#iterative-index-scans
"SET LOCAL hnsw.iterative_scan = relaxed_order;"