Prompt
need search with row level security
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
Got it — if you need search that respects row-level security (RLS), the right implementation depends on what stack you’re using.
The core idea
You should never search unrestricted data first and filter later.
Instead, apply RLS at query time so each user only searches rows they’re allowed to see.
Common patterns
1) Database-level RLS
If your DB supports it (e.g. PostgreSQL):
- Enable RLS on the table
- Write policies based on
user_id,tenant_id, roles, etc. - Run search queries normally; the DB filters rows automatically
Example:
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
CREATE POLICY doc_access
ON documents
USING (
tenant_id = current_setting('app.tenant_id')::uuid
);
Then your search query:
SELECT *
FROM documents
WHERE to_tsvector('english', content) @@ plainto_tsquery('invoice');
Only rows allowed by the policy are returned.
2) Search index with filtered access
If you use Elasticsearch/OpenSearch:
- Index documents with access metadata like
tenant_id,allowed_users,roles - Include a filter in every search query
- Enforce the filter server-side, not in the client
Example filter:
{
"query": {
"bool": {
"must": [
{ "match": { "content": "invoice" } }
],
"filter": [
{ "term": { "tenant_id": "abc123" } }
]
}
}
}
3) App-layer filtering only
This is acceptable only if:
- the dataset is small, or
- you fully trust the app layer and never expose raw search results
But this is weaker than DB-enforced RLS.
Recommended approach
- Best security: DB RLS + search query on same DB
- Best scale/search quality: Search engine + mandatory access filters
- Avoid: fetching all matches and filtering in memory
If you want, I can help you with a concrete implementation for one of these:
- PostgreSQL + full-text search + RLS
- Elasticsearch/OpenSearch + tenant/user filtering
- Supabase RLS search
- Django / Rails / Node / Prisma example
If you tell me your stack, I’ll draft the exact query and policy.