Database / Supabase Intermediate to Advanced Interview Questions
How do you troubleshoot a Postgres query that performs well in the SQL editor but slowly through the REST API?
The two paths often aren't running the same actual query, which is the first thing to verify rather than assume. In the SQL editor, you frequently run as a privileged role with no RLS applied; through the REST API, PostgREST runs the query as the authenticated (or anon) role, meaning every RLS policy on the table is folded into the query plan — a plan that can look completely different from the unfiltered version you tested manually.
The fix is to reproduce the real conditions: run EXPLAIN ANALYZE in the SQL editor as the actual role the API uses (set role authenticated; first), with the same RLS policies active, rather than testing as a superuser. This often reveals that a policy condition — especially one calling auth.uid() unwrapped, or joining out to another table to check a permission — is what's actually driving the slow plan, not the query's own filters.
Beyond RLS, also check whether PostgREST's default row limit or an unindexed order by is silently forcing a large sort, and confirm the API request isn't requesting a much larger page size or a deeper embedded resource (a joined related table) than the manual test did — both are easy to overlook when comparing a hand-written query to what the client SDK actually generated.
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...
