← Back to Field Notes

FIELD NOTE 015 / SUPABASE RAG INFRASTRUCTURE

Supabase for AI: pgvector, RLS, and Production RAG Infrastructure

A practical guide to using Supabase as the data layer for production RAG systems: pgvector setup, row-level security for multi-tenant retrieval, realtime, and edge functions.

By Harrison Ndeke · Published September 20, 2026 · Updated September 20, 2026 · 12 min read

Wordless dark basalt systems illustration of a Postgres and pgvector core surrounded by copper row-level-security boundary gates, with tenant-isolated data rows passing through distinct filtered channels toward a single query result.

RAG DATA LAYER

PGVECTOR → RLS → EDGE FUNCTIONS

In this article
  1. Direct answer
  2. Key takeaways
  3. pgvector setup
  4. RLS for RAG
  5. Realtime & edge
  6. Production checklist

DIRECT ANSWERSupabase lets a RAG system hold vector embeddings, relational metadata, and access policy in one Postgres database via the pgvector extension, instead of stitching together a separate dedicated vector store. Row-level security (RLS) enforces tenant and document-level access at the database layer, so a query can only ever return vectors the requesting user is actually permitted to see — enforced by Postgres itself, not by application code that can be bypassed by a bug.

Key takeaways

  1. One database, not two: pgvector adds vector similarity search to Postgres, so embeddings live beside the relational data they describe.
  2. RLS enforces access control at the data layer: a compromised application server cannot retrieve rows a policy denies.
  3. Metadata filtering happens in SQL, not in application logic: combine vector similarity with a `WHERE` clause on tenant, permission, or freshness columns in one query.
  4. Edge functions handle embedding close to the write: generate an embedding on document insert instead of a separate batch job.
  5. This is an infrastructure choice, not a RAG-quality guarantee: chunking and retrieval tuning still determine answer accuracy.

What does a pgvector setup look like?

pgvector is a Postgres extension that adds a vector column type and similarity-search operators. A minimal RAG table stores the chunk text, its embedding, and the metadata needed for access control and freshness checks.

create extension if not exists vector;

create table document_chunks (
  id uuid primary key default gen_random_uuid(),
  tenant_id uuid not null references tenants(id),
  document_id uuid not null references documents(id),
  content text not null,
  embedding vector(1536),
  source_url text,
  last_verified_at timestamptz not null default now()
);

create index on document_chunks
  using ivfflat (embedding vector_cosine_ops) with (lists = 100);

The index type and list count affect query speed versus recall — Supabase's own AI & Vectors documentation covers index tuning in more depth than fits here; the defaults are a reasonable starting point, not a final answer for high-volume production traffic.

How does row-level security apply to a RAG pipeline?

Without RLS, "only return chunks this user can see" is application logic — a filter added to a query, which means it can be forgotten in one code path and not another. With RLS, the policy is attached to the table itself and Postgres enforces it on every query, regardless of which application code issued it.

alter table document_chunks enable row level security;

create policy tenant_isolation on document_chunks
  for select
  using (tenant_id = (auth.jwt() ->> 'tenant_id')::uuid);

For multi-tenant RAG — several customers' documents in one database — this closes the most common and most dangerous failure mode: a retrieval query that accidentally returns another tenant's data because an application-level filter was missing on one endpoint. The same pattern extends to document-level permissions, not just tenant isolation, by joining against a permissions table inside the policy.

Where do realtime and edge functions fit?

Two Supabase features are useful in a RAG pipeline beyond storage. Edge Functions can run the embedding step at the point of ingestion — a document is inserted, a database webhook triggers an Edge Function, the function calls an embedding model and writes the vector back — so ingestion and embedding stay in one event-driven path instead of a separate batch job that can drift out of sync with the source documents. Realtime subscriptions let a frontend reflect ingestion status (queued, embedding, indexed) without polling, which matters for any RAG admin UI where documents are uploaded and processed asynchronously.

What should be in place before this goes to production?

ConcernWhy it matters
RLS policies tested under multiple rolesA policy that looks correct can still leak data for an edge-case role or a service-account bypass.
Index tuning for query volumeDefault ivfflat settings may not hold acceptable latency at production query volume.
Embedding model version tracked per rowChanging embedding models without re-embedding existing rows silently breaks similarity search.
Source freshness metadataWithout a last-verified timestamp, retrieval can't distinguish a current answer from a stale one.
Connection pooling configuredHigh-concurrency RAG query load needs Supabase's pooler (pgbouncer) tuned, not just direct connections.

Executive summary

Supabase's advantage for RAG infrastructure isn't that pgvector is a uniquely powerful vector search engine — dedicated vector databases can outperform it at very large scale — it's that access control, relational metadata, and vector search live in the same transactional database with the same security model. For most production RAG systems below extreme scale, that consolidation removes an entire class of application-layer bugs around access control and data consistency.

Related services and reading

About the author

Harrison Ndeke is an AI automation developer in Nairobi building production RAG pipelines on Supabase pgvector with row-level security and source-freshness checks. Public work includes the Standout4Growth RAG chatbot.

Sources, scope, and limitations

Primary source: Supabase AI & Vectors documentation and Supabase Row Level Security documentation. Index configuration, connection pooling limits, and pricing tiers change — verify current specifics against Supabase's documentation before committing to a scale plan.

WHAT SHOULD YOU DO NEXT?

If your RAG system serves more than one customer or user role from the same document store, check today whether access control is enforced in application code or in the database. If it's application code only, send Harrison the schema to scope an RLS-based fix before it becomes an incident.

WhatsApp