「users」テーブルに RLS ポリシーを適用し、次のレコードのみtenant_id=2
が取得されることを期待しています。
CREATE TABLE "users" ("name" text UNIQUE NOT NULL, "tenant_id" int NOT NULL DEFAULT current_setting('app.current_tenant')::int);
--Enable Row Security Policies
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE users FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation_policy ON users USING (tenant_id = current_setting('app.current_tenant')::int);
--Set "111" as the current tenant.
SET app.current_tenant TO 1;
INSERT INTO users VALUES ('admin');
INSERT INTO users VALUES ('bob');
--Set "222" as the current tenant.
SET app.current_tenant TO 2;
INSERT INTO users VALUES ('alice');
--Data output
SELECT * FROM users;
しかし、私は結果にすべてのユーザーを取得します:
name tenant_id
admin 1
bob 1
alice 2
なぜこうなった?
これが私が立ち往生しているもののdbFiddleです: https://www.db-fiddle.com/f/iFktvVsDNYKggUNT2oDJBV/0