Menu

Postgres RLS for shared documents: three policies and the index that makes them fast
📰

Postgres RLS for shared documents: three policies and the index that makes them fast

DEV Community: postgres·Revercue·2 days ago
#8lvvhvN8
#devtopostgresql#devto#dev#documents#policy
Long article (1497 words) — no AI summary yet.
Reading 0:00
15s threshold

The failure is quiet. One missing policy, or one API route connecting as the table owner, and a client's browser can read another client's documents. Nothing throws. The tests pass, because the tests use one user.

Row-level security is the smallest thing that makes multi-tenant sharing safe in Postgres, and it is easier than its reputation. Three policies, one stable function, and one index cover most document-sharing products.

The two tables

A document, and a membership row that says who can see it. Nothing clever:

create table documents ( id uuid primary key default gen_random_uuid ( ) , owner_id uuid not null references users ( id ) , title text not null , body jsonb not null default '{}' , revision int not null default 0 , share_version int not null default 0 , created_at timestamptz not null default now ( ) ) ;

create table document_members ( document_id uuid not null references documents ( id ) on delete cascade , user_id uuid not null references users ( id ) , role text not null check ( role in (…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.