Database / Supabase basics Interview Questions
Explain the internal working of Row Level Security policy evaluation in Postgres?
When RLS is enabled on a table, Postgres doesn't run policies as a separate pass after fetching rows — it rewrites the query plan so that each policy's USING expression is folded in as an additional filter condition, conceptually similar to an implicit AND clause appended to the query's WHERE clause, but applied per-command-type.
Each operation type has its own expression: USING controls which existing rows are visible for SELECT, UPDATE, and DELETE, while WITH CHECK validates rows being inserted or the new values of an update, preventing a user from writing a row that would immediately violate the policy that would otherwise hide it. If a table has multiple permissive policies for the same operation, Postgres combines them with OR — a row is visible if it satisfies any one of them — whereas restrictive policies (declared with AS RESTRICTIVE) are combined with AND and narrow access further on top of the permissive set.
Because this expression becomes part of the query plan, Postgres's optimizer can use indexes referenced inside the policy condition itself; a policy like user_id = auth.uid() on an indexed user_id column performs like any other indexed filter, which is why RLS-heavy tables should have their policy columns indexed just as deliberately as any other frequently filtered column.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
