-- A plan has one request, but every caller/key still needs its own immutable -- idempotency proof when it deduplicates against an already submitted plan. CREATE TABLE operation_submissions ( tenant_id uuid NOT NULL, principal text NOT NULL, idempotency_key text NOT NULL, request_hash text NOT NULL, operation_id uuid NOT NULL, PRIMARY KEY(tenant_id,principal,idempotency_key), FOREIGN KEY(tenant_id,operation_id) REFERENCES approval_requests(tenant_id,operation_id) ); -- Backfill old proofs with explicit transaction-local tenant context, including -- FORCE-RLS tables. The migration identity does not need a BYPASSRLS exception. DO $$ DECLARE tenant uuid; BEGIN FOR tenant IN SELECT tenant_id FROM tenants LOOP PERFORM set_config('app.tenant_id',tenant::text,true); INSERT INTO operation_submissions SELECT tenant_id,principal,idempotency_key,request_hash,operation_id FROM approval_requests; END LOOP; PERFORM set_config('app.tenant_id','',true); END $$; ALTER TABLE operation_submissions ENABLE ROW LEVEL SECURITY; ALTER TABLE operation_submissions FORCE ROW LEVEL SECURITY; CREATE POLICY tenant_scope ON operation_submissions USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true)); GRANT SELECT, INSERT ON operation_submissions TO rmm_app;