AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/db): Move `vector` extension to `extensions` schema (#11902)
Reinier van der Leer1b562cbe49a9
diff --git a/autogpt_platform/backend/migrations/20260129150000_move_vector_extension_to_extensions_schema/migration.sql b/autogpt_platform/backend/migrations/20260129150000_move_vector_extension_to_extensions_schema/migration.sqlnew file mode 100644index 0000000000..b4a986fb43--- /dev/null+++ b/autogpt_platform/backend/migrations/20260129150000_move_vector_extension_to_extensions_schema/migration.sql@@ -0,0 +1,83 @@+-- Move pgvector extension from application schema to extensions schema.+-- The extensions schema is in the default search_path, so ::vector casts+-- in raw SQL queries will resolve correctly without needing to set+-- search_path per connection. This fixes intermittent "type vector does not exist"+-- errors that occur when pooled connections don't have the right search_path.++-- Also set the database default search_path to include the application schema,+-- so that raw SQL queries can find application tables/types without explicit+-- schema prefix.++-- Step 1: Ensure extensions schema exists+CREATE SCHEMA IF NOT EXISTS extensions;++-- Step 2: Move pgvector from current schema to extensions schema (if possible)+DO $$+DECLARE+ vector_schema text;+BEGIN+ -- Check where vector extension currently lives+ SELECT n.nspname INTO vector_schema+ FROM pg_extension e+ JOIN pg_namespace n ON e.extnamespace = n.oid+ WHERE e.extname = 'vector';++ IF vector_schema IS NULL THEN+ -- Not installed yet; create in extensions schema+ CREATE EXTENSION IF NOT EXISTS vector SCHEMA extensions;+ ELSIF vector_schema != 'extensions' THEN+ -- Installed in wrong schema; try to move it+ -- This may fail on managed platforms like Supabase where the extension+ -- is owned by a system user. In that case, we'll include the current+ -- vector schema in the search_path instead (handled in Step 3).+ BEGIN+ RAISE NOTICE 'Moving pgvector from schema "%" to "extensions"', vector_schema;+ ALTER EXTENSION vector SET SCHEMA extensions;+ EXCEPTION WHEN insufficient_privilege THEN+ RAISE NOTICE 'Cannot move pgvector (insufficient privileges). Will include schema "%" in search_path instead.', vector_schema;+ END;+ ELSE+ RAISE NOTICE 'pgvector already in extensions schema, nothing to do';+ END IF;+END $$;++-- Step 3: Update role-level search_path to include the application schema+-- and the schema where pgvector lives. Role-level settings take precedence over+-- database-level settings, which is important for managed platforms like Supabase+-- that may have existing role-level search_path configurations.+-- The default PostgreSQL search_path is: "$user", public+-- We add the extensions schema and (if different) the schema where pgvector+-- actually lives, so that ::vector type casts resolve correctly.+DO $$+DECLARE+ app_schema text := current_schema();+ vector_schema text;+ ext_schemas text := 'extensions';+BEGIN+ -- Find where pgvector currently lives (after Step 2's move-or-fail)+ SELECT n.nspname INTO vector_schema+ FROM pg_extension e+ JOIN pg_namespace n ON e.extnamespace = n.oid+ WHERE e.extname = 'vector';++ -- If vector is in a schema not already covered, add it to the search_path+ IF vector_schema IS NOT NULL+ AND vector_schema != 'extensions'+ AND vector_schema != 'public'+ AND vector_schema != app_schema+ THEN+ ext_schemas := format('extensions, %I', vector_schema);+ END IF;++ IF app_schema = 'public' THEN+ EXECUTE format(+ 'ALTER ROLE %I SET search_path TO "$user", public, %s',+ current_user, ext_schemas+ );+ ELSE+ EXECUTE format(+ 'ALTER ROLE %I SET search_path TO "$user", %I, public, %s',+ current_user, app_schema, ext_schemas+ );+ END IF;+END $$;diff --git a/autogpt_platform/backend/migrations/20260605120000_fix_vector_extension_search_path/migration.sql b/autogpt_platform/backend/migrations/20260605120000_fix_vector_extension_search_path/migration.sqlnew file mode 100644index 0000000000..7918ec0d17--- /dev/null+++ b/autogpt_platform/backend/migrations/20260605120000_fix_vector_extension_search_path/migration.sql@@ -0,0 +1,49 @@+-- Make the pgvector `vector` type resolvable on DIRECT (non-pooled) connections.+--+-- Background:+-- The previous migration (20260129150000) moved pgvector into the `extensions`+-- schema and set a role-level search_path of:+-- "$user", platform, public, extensions+-- That works on POOLED (Supavisor) connections used by dev/prod: the pooler+-- ignores the per-connection `?schema=platform` parameter and falls back to the+-- role-level search_path, which includes `extensions`.+--+-- It does NOT work on DIRECT connections (local dev, CI, self-hosted without+-- Supavisor). A direct connection honors `?schema=platform`, which sets a+-- SESSION-level `search_path = "platform"`. A session-level search_path overrides+-- the role-level one, so `extensions` is not visible and unqualified `::vector`+-- casts fail with: type "vector" does not exist.+--+-- Fix:+-- Move the `vector` extension into the application schema (`platform`), the only+-- schema guaranteed to be on the search_path of BOTH connection types (direct:+-- forced by `?schema=platform`; pooled: included in the role-level search_path).+-- The role-level search_path set by the previous migration already covers pooled+-- connections, so it does not need to be touched here.+DO $$+DECLARE+ app_schema text := current_schema();+ vector_schema text;+BEGIN+ SELECT n.nspname INTO vector_schema+ FROM pg_extension e+ JOIN pg_namespace n ON e.extnamespace = n.oid+ WHERE e.extname = 'vector';++ IF vector_schema IS NULL THEN+ -- Not installed yet: create it directly in the application schema.+ EXECUTE format('CREATE EXTENSION IF NOT EXISTS vector SCHEMA %I', app_schema);+ ELSIF vector_schema <> app_schema THEN+ -- Move it into the application schema. May fail on managed platforms (e.g.+ -- Supabase) where the extension is owned by a system role; that is fine,+ -- because those use pooled connections that resolve the type via the+ -- role-level search_path established by migration 20260129150000.+ BEGIN+ RAISE NOTICE 'Moving pgvector from schema "%" to "%"', vector_schema, app_schema;+ EXECUTE format('ALTER EXTENSION vector SET SCHEMA %I', app_schema);+ EXCEPTION WHEN insufficient_privilege THEN+ RAISE NOTICE 'Cannot move pgvector into "%" (insufficient privileges); '+ 'pooled connections will resolve it via the role-level search_path.', app_schema;+ END;+ END IF;+END $$;