
create role test_user1;
create table test_tbl(rolname name)with oids;
insert into test_tbl values('Fujitsu');
grant all on test_tbl to test_user1;

Alter table test_tbl enable row level security;
create policy test_tbl_policy on test_tbl for select using(pg_has_role(oid, 'select'));

set role test_user1;
explain SELECT (u_grantor.rolname) AS grantor,                                                                                
(grantee.rolname) AS grantee                                                                                      
    FROM  test_tbl u_grantor,                                                                                                                                  
          ( SELECT test_tbl.oid,                                                                                                                              
                   test_tbl.rolname                                                                                                                            
                   FROM test_tbl                                                                                                                                
            UNION ALL                                                                                                                                        
                SELECT (0)::oid AS oid,                                                                                                                          
                       'PUBLIC'::name) grantee(oid, rolname)                                                                                                        
    WHERE (pg_has_role(u_grantor.oid, 'USAGE'::text) OR pg_has_role(grantee.oid, 'USAGE'::text) OR (grantee.rolname = 'PUBLIC'::name));


--create policy test_tbl_policy on test_tbl for select using(true); -- works fine as it doesn't expand_securiy_qual second time for test_tbl.