Skip to content

Instantly share code, notes, and snippets.

@calebmer

calebmer/test.md Secret

Created May 6, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebmer/96c334a26e4f2350d067924e08b7b245 to your computer and use it in GitHub Desktop.
Save calebmer/96c334a26e4f2350d067924e08b7b245 to your computer and use it in GitHub Desktop.

Without RLS Policy

 EXPLAIN (ANALYZE, BUFFERS)
  SELECT id, author_id, published_at, content
    FROM post
   WHERE group_id = 1 AND
         (EXISTS (SELECT 1
                    FROM group_member
                   WHERE group_member.account_id = current_setting('connect.account_id', false)::INT AND
                         group_member.group_id = post.group_id))
ORDER BY published_at DESC, id
   LIMIT 5
  OFFSET 42;
 Limit  (cost=627.41..627.42 rows=5 width=172) (actual time=0.507..0.509 rows=5 loops=1)
   Buffers: shared hit=618
   ->  Sort  (cost=627.30..627.45 rows=60 width=172) (actual time=0.499..0.503 rows=47 loops=1)
         Sort Key: post.published_at DESC, post.id
         Sort Method: quicksort  Memory: 40kB
         Buffers: shared hit=618
         ->  Nested Loop  (cost=0.16..625.53 rows=60 width=172) (actual time=0.409..0.446 rows=60 loops=1)
               Buffers: shared hit=618
               ->  Index Only Scan using group_member_pkey on group_member  (cost=0.16..8.18 rows=1 width=4) (actual time=0.016..0.017 rows=1 loops=1)
                     Index Cond: ((account_id = (current_setting('connect.account_id'::text, false))::integer) AND (group_id = 1))
                     Heap Fetches: 1
                     Buffers: shared hit=2
               ->  Seq Scan on post  (cost=0.00..616.75 rows=60 width=176) (actual time=0.391..0.415 rows=60 loops=1)
                     Filter: (group_id = 1)
                     Buffers: shared hit=616
 Planning Time: 0.221 ms
 Execution Time: 0.965 ms

With RLS Policy

 EXPLAIN (ANALYZE, BUFFERS)
  SELECT post.id, post.author_id, post.published_at, post.content
    FROM post
   WHERE post.group_id = 1
ORDER BY post.published_at DESC, post.id
   LIMIT 5
  OFFSET 42;
 Limit  (cost=1108.36..1108.36 rows=1 width=172) (actual time=0.743..0.746 rows=5 loops=1)
   Buffers: shared hit=618
   ->  Sort  (cost=1108.29..1108.36 rows=30 width=172) (actual time=0.735..0.739 rows=47 loops=1)
         Sort Key: post.published_at DESC, post.id
         Sort Method: quicksort  Memory: 40kB
         Buffers: shared hit=618
         ->  Seq Scan on post  (cost=0.00..1107.55 rows=30 width=172) (actual time=0.573..0.661 rows=60 loops=1)
               Filter: ((group_id = 1) AND (alternatives: SubPlan 1 or hashed SubPlan 2))
               Buffers: shared hit=618
               SubPlan 1
                 ->  Index Only Scan using group_member_pkey on group_member  (cost=0.16..8.18 rows=1 width=0) (never executed)
                       Index Cond: ((account_id = (current_setting('connect.account_id'::text, false))::integer) AND (group_id = post.group_id))
                       Heap Fetches: 0
               SubPlan 2
                 ->  Bitmap Heap Scan on group_member group_member_1  (cost=4.23..14.84 rows=9 width=4) (actual time=0.010..0.011 rows=2 loops=1)
                       Recheck Cond: (account_id = (current_setting('connect.account_id'::text, false))::integer)
                       Heap Blocks: exact=1
                       Buffers: shared hit=2
                       ->  Bitmap Index Scan on group_member_pkey  (cost=0.00..4.23 rows=9 width=0) (actual time=0.006..0.006 rows=2 loops=1)
                             Index Cond: (account_id = (current_setting('connect.account_id'::text, false))::integer)
                             Buffers: shared hit=1
 Planning Time: 0.534 ms
 Execution Time: 0.848 ms

With RLS Policy and set enable_bitmapscan=off

 EXPLAIN (ANALYZE, BUFFERS)
  SELECT post.id, post.author_id, post.published_at, post.content
    FROM post
   WHERE post.group_id = 1
ORDER BY post.published_at DESC, post.id
   LIMIT 5
  OFFSET 42;
 Limit  (cost=1108.36..1108.36 rows=1 width=172) (actual time=0.520..0.522 rows=5 loops=1)
   Buffers: shared hit=618
   ->  Sort  (cost=1108.29..1108.36 rows=30 width=172) (actual time=0.512..0.516 rows=47 loops=1)
         Sort Key: post.published_at DESC, post.id
         Sort Method: quicksort  Memory: 40kB
         Buffers: shared hit=618
         ->  Seq Scan on post  (cost=0.00..1107.55 rows=30 width=172) (actual time=0.414..0.456 rows=60 loops=1)
               Filter: ((group_id = 1) AND (alternatives: SubPlan 1 or hashed SubPlan 2))
               Buffers: shared hit=618
               SubPlan 1
                 ->  Index Only Scan using group_member_pkey on group_member  (cost=0.16..8.18 rows=1 width=0) (never executed)
                       Index Cond: ((account_id = (current_setting('connect.account_id'::text, false))::integer) AND (group_id = post.group_id))
                       Heap Fetches: 0
               SubPlan 2
                 ->  Index Only Scan using group_member_pkey on group_member group_member_1  (cost=0.16..32.32 rows=9 width=4) (actual time=0.016..0.017 rows=2 loops=1)
                       Index Cond: (account_id = (current_setting('connect.account_id'::text, false))::integer)
                       Heap Fetches: 2
                       Buffers: shared hit=2
 Planning Time: 0.396 ms
 Execution Time: 0.614 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment