Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postgres): Handle vector extension creation properly (#1561) #1743

Merged

Conversation

AIFlowML
Copy link
Collaborator

@AIFlowML AIFlowML commented Jan 3, 2025

PR: Fix PostgreSQL Vector Extension Issues (#1561)

Problem

The PostgreSQL adapter was encountering a "type vector does not exist" error when trying to create tables that use the vector type. This was happening because:

  1. The vector extension was being created in the extensions schema
  2. The vector type wasn't accessible in the public schema where tables were being created

Solution

We implemented a robust fix that:

  1. Creates the extensions schema if it doesn't exist
  2. Properly handles the vector extension creation in the extensions schema
  3. Adds the extensions schema to the search path before table creation

Changes Made

-- Create extensions schema first
CREATE SCHEMA IF NOT EXISTS extensions;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_extension
        WHERE extname = 'vector'
    ) THEN
        CREATE EXTENSION vector
        SCHEMA extensions;
    END IF;
END $$;

-- Add extensions schema to search path
SET search_path TO public, extensions;

Testing and Verification

Test Environment

  • Docker container using pgvector/pgvector:pg15 image
  • Clean PostgreSQL instance with pgvector support
  • Database: eliza_test

Test Scenarios

  1. Initial Installation Test

    • ✅ Schema creation successful
    • ✅ Vector extension created without errors
    • ✅ All tables created with correct vector type
    • ✅ All indexes created successfully
    • ✅ Transaction committed successfully
  2. Idempotency Test (Second Run)

    • ✅ No duplicate key errors
    • ✅ Proper handling of existing objects with NOTICE messages
    • ✅ Vector extension checks working correctly
    • ✅ Transaction committed successfully

Test Results

-- First Run Output
CREATE SCHEMA
DO
CREATE EXTENSION
SET
[... tables and indexes created successfully ...]
COMMIT

-- Second Run Output
NOTICE: schema "extensions" already exists, skipping
CREATE SCHEMA
DO
[... proper "already exists" notices ...]
COMMIT

Impact

This fix:

Notes for Reviewers

  • The solution follows PostgreSQL best practices for extension management
  • Schema search path is explicitly set to ensure type availability
  • Implementation is idempotent and safe to run multiple times
  • Tested with pgvector-enabled PostgreSQL instance
  • No changes required to application code

Required Environment

  • PostgreSQL 15 or later
  • pgvector extension must be available (e.g., using pgvector/pgvector:pg15 image)
  • Proper permissions to create schemas and extensions

- Add proper checks for vector extension existence

- Create extensions schema if not exists

- Add extensions schema to search path

- Ensure idempotent schema creation
@odilitime odilitime changed the base branch from main to develop January 3, 2025 14:30
@odilitime
Copy link
Collaborator

does this work with postgres 14, 15 and 16?

@AIFlowML
Copy link
Collaborator Author

AIFlowML commented Jan 3, 2025

does this work with postgres 14, 15 and 16?

15 or later. The test was a 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants