Thread: [9.5] next question: rls and indexes

[9.5] next question: rls and indexes

From
Andreas Kretschmer
Date:
hi @ll,


my table:


test=*# \d rls_test
   Table "public.rls_test"
 Column |  Type   | Modifiers
--------+---------+-----------
 id     | integer |
 name   | text    |
 txt    | text    |
Indexes:
    "idx_name" btree (name)
Policies:
    POLICY "my_policy" FOR ALL
      USING (name = ("current_user"())::text)

test=*# commit;
COMMIT
Time: 0,581 ms
test=# \c - name_66;
You are now connected to database "test" as user "name_66".
test=> explain select * from rls_test ;
                         QUERY PLAN
-------------------------------------------------------------
 Seq Scan on rls_test  (cost=0.00..630.00 rows=91 width=335)
   Filter: (name = ("current_user"())::text)
(2 rows)

Time: 1,531 ms
test=*> set enable_Seqscan to off;
SET
Time: 0,073 ms
test=*> explain select * from rls_test ;
                                  QUERY PLAN
-------------------------------------------------------------------------------
 Seq Scan on rls_test  (cost=10000000000.00..10000000630.00 rows=91
width=335)
   Filter: (name = ("current_user"())::text)
(2 rows)



As you can see, the index isn't in use, but i think, in this case PG
should use it.

I can expand the where with a where, so PG is using the index:


test=*> explain select * from rls_test where name = current_user;
                               QUERY PLAN
------------------------------------------------------------------------
 Bitmap Heap Scan on rls_test  (cost=4.97..232.90 rows=1 width=335)
   Recheck Cond: (name = ("current_user"())::text)
   Filter: (name = ("current_user"())::text)
   ->  Bitmap Index Scan on idx_name  (cost=0.00..4.97 rows=91 width=0)
         Index Cond: (name = ("current_user"())::text)
(5 rows)



USING in a policy isn't using the avalable index.
Bug or Feature?


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°


Re: [9.5] next question: rls and indexes

From
Dean Rasheed
Date:
On 15 July 2015 at 09:34, Andreas Kretschmer <akretschmer@spamfence.net> wrote:
> test=> explain select * from rls_test ;
>                          QUERY PLAN
> -------------------------------------------------------------
>  Seq Scan on rls_test  (cost=0.00..630.00 rows=91 width=335)
>    Filter: (name = ("current_user"())::text)
> (2 rows)
>
> As you can see, the index isn't in use, but i think, in this case PG
> should use it.
>
> Bug or Feature?
>

Yes, that's a bug. It should indeed use the index for queries like
that. Debugging it reveals that the reason it didn't is a collation
mismatch which caused it to believe there were no suitable indexes to
use.

Fortunately this has been fixed (post 9.5 alpha 1) -- see
http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=808ea8fc7bb259ddd810353719cac66e85a608c8
-- and the same query on HEAD now does use the index.

Thanks for testing.

Regards,
Dean