Will this RLS policy be slow?


alter policy "Authenticated users can comment on followed/owned blogs"
on "public"."post_comments"
to authenticated
with check (
(EXISTS ( SELECT 1
FROM ((posts p
JOIN blogs b ON ((b.id = p.blog_id)))
LEFT JOIN blog_followers f ON (((f.blog_id = b.id) AND (f.user_id = ( SELECT auth.uid() AS uid)))))
WHERE ((p.id = post_comments.post_id) AND ((f.user_id IS NOT NULL) OR (b.owner_id = ( SELECT auth.uid() AS uid))))))
);

alter policy "Authenticated users can comment on followed/owned blogs"
on "public"."post_comments"
to authenticated
with check (
(EXISTS ( SELECT 1
FROM ((posts p
JOIN blogs b ON ((b.id = p.blog_id)))
LEFT JOIN blog_followers f ON (((f.blog_id = b.id) AND (f.user_id = ( SELECT auth.uid() AS uid)))))
WHERE ((p.id = post_comments.post_id) AND ((f.user_id IS NOT NULL) OR (b.owner_id = ( SELECT auth.uid() AS uid))))))
);
I have this RLS policy, and a few more like this. Will this be slow in the long run? Should I change this, if so, how?
1 Reply
Thijs
ThijsOP3h ago
CREATE POLICY "Authenticated users can comment on followed or owned blogs"
ON public.post_comments
FOR INSERT
TO authenticated
WITH CHECK (
(
-- gebruiker is de eigenaar van de blog
(SELECT owner_id FROM blogs
WHERE id = (
SELECT blog_id FROM posts WHERE id = post_comments.post_id
)
) = auth.uid()
)
OR
(
-- gebruiker volgt de blog
EXISTS (
SELECT 1 FROM blog_followers
WHERE user_id = auth.uid()
AND blog_id = (
SELECT blog_id FROM posts WHERE id = post_comments.post_id
)
)
)
);
CREATE POLICY "Authenticated users can comment on followed or owned blogs"
ON public.post_comments
FOR INSERT
TO authenticated
WITH CHECK (
(
-- gebruiker is de eigenaar van de blog
(SELECT owner_id FROM blogs
WHERE id = (
SELECT blog_id FROM posts WHERE id = post_comments.post_id
)
) = auth.uid()
)
OR
(
-- gebruiker volgt de blog
EXISTS (
SELECT 1 FROM blog_followers
WHERE user_id = auth.uid()
AND blog_id = (
SELECT blog_id FROM posts WHERE id = post_comments.post_id
)
)
)
);
Think I got it

Did you find this page helpful?