Goal: test row policies, expose a mutable-context weakness, and fix direct tenant access.
The setup supplied an empty, separate orders fixture and two non-owner logins with SELECT/INSERT plus sequence access. It did not add rows or enable RLS. Connect as postgres to beer_db; -U selects the role, -d the database. Expect beer_db=#. Our tests distinguish correct filtering from trustworthy tenant identity.
Goal: create a visible baseline for two tenants.
Insert Acme’s Wile E. Coyote and Road Runner orders plus Globex’s Hank Scorpio order. The omitted serial order_id is generated by the sequence. Expect INSERT 0 3: the last number counts inserted rows. We keep these three rows unchanged after all write tests.
Goal: read the exact table’s current RLS flags.
orders cast to regclass resolves its catalog identity; this avoids confusing a same-named table in another schema. relrowsecurity enables row-policy enforcement; relforcerowsecurity extends it to an ordinary owner. Expect f | f, meaning both false. Table-level SELECT still permits all rows at this point.
Goal: enable the row-policy gate before granting visibility through a policy.
Expect ALTER TABLE. With RLS enabled and no applicable policy, ordinary non-owner reads see zero rows even if they hold SELECT. Owners normally bypass RLS, and superusers/BYPASSRLS always do. The next step checks default-deny as a tenant rather than as postgres.
Goal: observe default-deny against a table known to contain three rows.
Exit to the shell and reconnect as tenant_acme. The ordered SELECT must return (0 rows) without a permission error. A successful empty result is deliberate filtering; it does not mean the fixture lost its data. The table grant was supplied in setup.
Goal: introduce a policy whose visibility depends on supplied context.
Reconnect as postgres and create the default ALL policy. USING admits rows only when tenant_id equals current_setting('app.tenant_id', true). The second argument permits a never-set key to return NULL; equality with NULL does not pass the policy. Without explicit WITH CHECK, this ALL policy reuses USING for writes. Expect CREATE POLICY. This is a context mechanism, not tenant authentication.
Goal: compare missing context with Acme context in one new tenant session.
Reconnect as Acme and count before SET: expect visible_without_context = 0. SET supplies a custom session value; it does not change the SQL role. After setting acme, expect only IDs 1/2 with Wile E. Coyote 129.99 and Road Runner 45.00. ORDER BY order_id makes the result stable.
Goal: test the other direction with the same policy.
Reconnect as Globex and set its context to globex. Expect exactly ID 3, Hank Scorpio, 998.50. The new client has its own settings; Acme’s previous value does not move to it. This proves filtering with the stated context; the later forgery test checks whether the caller can lie.
Goal: verify this ALL policy also constrains writes under the current context.
Still with Globex context, attempt an Acme INSERT. Expect new row violates row-level security policy for table "orders". The implicit write check rejects it and stores no row. This standalone autocommit failure does not abort later commands; a serial sequence value can still be consumed by a failed insert, so gaps are not proof of stored rows.
Goal: show that a custom setting is not proof of tenant identity.
Still logged in as tenant_globex, temporarily claim Acme with SET LOCAL inside BEGIN. Expect Acme’s two rows: the first policy trusts the value supplied by its caller. COMMIT restores the earlier session value globex, so the final query must show Hank alone. Transaction scoping controls lifetime; it does not authenticate a tenant.
Goal: compare superuser visibility with tenant visibility.
Reconnect as postgres and query the unchanged rows in ID order. Expect all three, without a tenant setting. The superuser bypass is separate from the table-owner exemption; FORCE cannot override it. FORCE and BYPASSRLS role administration are explained in Learn, not exercised here.
Goal: replace the mutable context with an identity the tenant cannot choose.
Run this as postgres after the superuser query. CASE current_user maps each permitted SQL role to its fixed tenant; ELSE NULL denies other ordinary roles. The omitted WITH CHECK reuses this expression for our default ALL policy. Expect ALTER POLICY. These tenant roles have no membership allowing them to assume each other; privileged role administration remains outside this experiment.
Goal: prove changing the setting cannot change this login’s tenant, and allowed writes still work.
Try the forged setting again as Globex: the role-bound policy must still return only Hank. The Acme insert must raise the RLS error. After that standalone failure, BEGIN a new transaction and insert a Globex row; expect one returned row and INSERT 0 1. ROLLBACK removes the demonstration row, and the final visible count must be 1. This proves selective access rather than denying everything.