A practical pattern for tenant-scoped retrieval with PostgreSQL, pgvector, row-level security, and integration tests.
I've spent about twenty years building software. A pattern worth watching for is a security check that exists in the first request path but gets missed when someone adds a second one. RAG makes that mistake particularly easy: vector search can return a highly relevant passage that the current user has no right to read.
The failure scenario below is hypothetical, it is not a report of a production incident.
Suppose you're building a knowledge assistant for several companies. Company A uploads contracts and internal policies; Company B uploads support documents. Both use the same application, database, and vector index. The first search endpoint checks organization membership and filters by tenant_id , so the demo looks fine.
Later, someone adds an agent tool and writes a second vector query. They copy the similarity-search part but forget the tenant filter.…