-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest2d.sql
67 lines (51 loc) · 2.24 KB
/
test2d.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- Testing wrapping SQL around a security definer function getting role based on auth.uid() from a 2nd table.
-- Change using() between commented get_role() and (select(get_role()))
-- Note it is as silly function, but was reusing tables. It could have getting user's teams to match on rows.
drop table if exists rlstest;
create table
rlstest as
select x as id, 'name-' || x as name, uuid_generate_v4() as user_id, 'user' as role
from generate_series(1, 100000) x;
update rlstest set (user_id,role) = ('70225db6-b0ba-4116-9b08-6b25f33bb70a','admin') where id = 1;
alter table rlstest ENABLE ROW LEVEL SECURITY;
drop table if exists rlstest_roles;
create table
rlstest_roles as
select x as id, 'name-' || x as name, uuid_generate_v4() as user_id, 'user' as role
from generate_series(1, 1000) x;
update rlstest_roles set (user_id,role) = ('70225db6-b0ba-4116-9b08-6b25f33bb70a','admin') where id = 1;
alter table rlstest_roles ENABLE ROW LEVEL SECURITY;
CREATE OR REPLACE FUNCTION get_role()
RETURNS text as
$$
begin
return (select role from rlstest_roles where auth.uid() = user_id);
end;
$$ language plpgsql security definer;
create policy "rls_test_select" on rlstest
to authenticated
using (
--(select get_role())=role
get_role() =role --very slow!
);
set session role authenticated;
set request.jwt.claims to '{"role":"authenticated", "sub":"70225db6-b0ba-4116-9b08-6b25f33bb70a"}';
explain analyze SELECT count(*) FROM rlstest;
set session role postgres;
/*
with get_role = role
Aggregate (cost=16597.35..16597.36 rows=1 width=8) (actual time=173484.219..173484.220 rows=1 loops=1)
-> Seq Scan on rlstest (cost=0.00..16596.60 rows=300 width=0) (actual time=173484.213..173484.214 rows=1 loops=1)
Filter: (get_role() = role)
Rows Removed by Filter: 99999
Planning Time: 0.092 ms
Execution Time: 173484.300 ms !!!!!!!!!!!!!!!!!!!!
with (select get_role())=role
InitPlan 1 (returns $0)
-> Result (cost=0.00..0.26 rows=1 width=32) (actual time=2.047..2.047 rows=1 loops=1)
-> Seq Scan on rlstest (cost=0.00..2084.00 rows=100000 width=0) (actual time=12.425..12.425 rows=1 loops=1)
Filter: ($0 = role)
Rows Removed by Filter: 99999
Planning Time: 0.137 ms
Execution Time: 12.475 ms
*/