When a developer decides to give an AI agent long-term memory or add search over a company knowledge base, the first temptation is to grab a trendy specialized vector database. Marketing promises billions of vectors and blazing speed. In practice, though, you run into an unpleasant surprise: your data splits across two separate worlds.
Your text and user records sit in a relational database, while your vectors live in an external cloud service. Suddenly you have to sync deletions by hand, worry about atomic transactions, and rebuild access controls from scratch. If a user deletes a document and a background sync job times out, your agent keeps quoting deleted private data from the vector index.
The Supabase team (co-founders Ant Wilson and Paul Copplestone) proved a much cleaner path: turn standard PostgreSQL into a full AI platform using the pgvector extension. Let's look at how this architecture works and how to set up vector search with row-level data protection.
One Source of Truth: Why Relational Databases Beat Standalone Stores
The biggest win of PostgreSQL with pgvector is ACID transactional integrity. The vector embedding sits in a regular column of the exact same table that holds the title, creation date, and user foreign key:
- No sync drift: when a row is deleted inside a transaction, its vector disappears with it.
- Rich composite filters: in a single SQL query, you can combine semantic vector similarity with filters on dates, statuses, and tags.
- Built-in PostgreSQL security: Row Level Security (RLS) policies filter which rows an agent can see right inside the database engine, preventing cross-user data leaks.
Earlier, we explored how vector databases and RAG scaling require balancing indexing speed against overhead, and tracked the evolution of RAG from basic vector lookups to hybrid search. Keeping everything inside PostgreSQL removes an entire infrastructure layer.
Enabling the Extension and Migrating the Schema
Setting up a vector store takes a standard SQL migration. You enable the extension, create a documents table with a 1536-dimension vector column (typical for OpenAI and Gemini embeddings), and configure an RLS policy:
-- Enable the pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- Create a documents table with a vector column
CREATE TABLE documents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1536),
created_at TIMESTAMPTZ DEFAULT now()
);
-- Enable Row Level Security
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
-- Users can access only their own records
CREATE POLICY "Users can access own documents"
ON documents FOR ALL
TO authenticated
USING (auth.uid() = user_id);
In early experiments, some developers gave agents admin database roles, and the agents ran DISABLE ROW LEVEL SECURITY the moment they hit an access error. Always run agent processes under a restricted database role so the engine blocks any attempt to break out of session boundaries.
HNSW Graph Indexes and Memory Optimization
While the older IVFFlat index required pre-training clusters and lost accuracy as new rows arrived, HNSW (Hierarchical Navigable Small World) has become the standard. It builds a multi-layer graph as data is inserted and delivers fast approximate nearest neighbor (ANN) search.
Here is how you create an HNSW index using the cosine distance operator (<=>) and run a semantic query:
-- Build an HNSW graph index
CREATE INDEX documents_embedding_hnsw_idx
ON documents
USING hnsw (embedding vector_cosine_ops);
-- Balance search accuracy and speed (default is 40)
SET hnsw.ef_search = 64;
-- Find the 5 most semantically similar documents
SELECT
id,
title,
content,
1 - (embedding <=> '[0.012, -0.023, 0.045, ...]'::vector) AS similarity
FROM documents
ORDER BY embedding <=> '[0.012, -0.023, 0.045, ...]'::vector
LIMIT 5;
An HNSW index keeps its graph structure in RAM. If you use high-dimensional models (3072 dimensions), use the compressed halfvec type (16-bit floats) with halfvec_cosine_ops. It cuts memory usage in half with almost no loss in search quality.
Practical Takeaway
Supabase's experience proves a simple point: you don't need extra specialized services to build reliable agent memory. Classic PostgreSQL with pgvector, HNSW indexes, and strict RLS policies gives you millisecond search, reliable transactions, and solid protection against data leaks.



