The last layer that knows who owns the data
All four previous lessons rest on one thing: somebody remembering to write the right clause. A WHERE clause needs an author, a shared Expression needs a caller, a DTO needs a declaration. Row Level Security is the one layer that needs nobody to remember anything — it runs inside the database, after every query, including a query typed into psql by hand at 3am.
How it works, briefly: you declare a policy on a table, and Postgres adds that policy's condition to every query touching the table. Not "check, then refuse" but a row that fails the condition does not exist as far as that query is concerned. SELECT count(*) returns a smaller number; UPDATE updates nothing; DELETE deletes nothing.
What makes it categorically different from the previous four:
| Layer | Protects | Does not protect |
|---|---|---|
A WHERE clause | The endpoint where somebody wrote it | The second endpoint, background jobs, migration scripts |
| Deny-by-default in the pipeline | Requests arriving over HTTP | Everything that does not arrive over HTTP |
A shared Expression | Wherever somebody calls it | Wherever somebody forgot |
| RLS | Every query against that table, from every source | See the final step — there are three bypasses |
And here is the part that must be said plainly, because it is this lesson's biggest trap: RLS is the second layer, not the first. Enabling RLS and then dropping the WHERE clause from your code is a mistake: you lose the ability to read the rule from application code, you lose control over performance (the policy joins your query plan), and you place all authorisation in a mechanism that one misconfigured connection turns off entirely.
Comments
Commenting needs an account with at least one completed lesson. That condition is what keeps this thread worth reading: every point belongs to someone who can be asked back, and reputation accrues over time.
You can still read every comment below without an account. Signing in brings you back to this exact spot, not to the top of the page.
Loading comments…