From 0b6d1610f1506c0943e35641291a0fb08588132c Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Thu, 2 Oct 2025 18:21:16 -0300 Subject: [PATCH] iterative_scan is a session-level parameter --- .../db/migrate/20251002171950_upgrade_pgvector080.rb | 2 -- plugins/discourse-ai/lib/embeddings/schema.rb | 11 +++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb b/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb index 1e50f420409..86a241cf2c9 100644 --- a/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb +++ b/plugins/discourse-ai/db/migrate/20251002171950_upgrade_pgvector080.rb @@ -9,8 +9,6 @@ class UpgradePgvector080 < ActiveRecord::Migration[8.0] if Gem::Version.new(installed_version) < Gem::Version.new(minimum_target_version) DB.exec("ALTER EXTENSION vector UPDATE TO '0.8.0';") end - - DB.exec("ALTER ROLE CURRENT_ROLE SET hnsw.iterative_scan = relaxed_order;") end def down diff --git a/plugins/discourse-ai/lib/embeddings/schema.rb b/plugins/discourse-ai/lib/embeddings/schema.rb index 0f37247be96..b7a9f73818e 100644 --- a/plugins/discourse-ai/lib/embeddings/schema.rb +++ b/plugins/discourse-ai/lib/embeddings/schema.rb @@ -180,6 +180,9 @@ module DiscourseAi ActiveRecord::Base.transaction do DB.exec(before_query) if before_query.present? + + DB.exec(hnsw_scan_relaxed_order) + builder.query( query_embedding: embedding, candidates_limit: candidates_limit, @@ -273,6 +276,9 @@ module DiscourseAi ActiveRecord::Base.transaction do DB.exec(before_query) if before_query.present? + + DB.exec(hnsw_scan_relaxed_order) + builder.query(query_params) end rescue PG::Error => e @@ -313,6 +319,11 @@ module DiscourseAi "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;" + end + delegate :dimensions, :pg_function, to: :vector_def end end