Skip to content

Commit 31dd51b

Browse files
authored
Merge pull request #493 from supabase/or/schema-multi-tenant-context
Context loading optimization for schema based multi-tenant workloads
2 parents d11f9a0 + 213b343 commit 31dd51b

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

sql/load_sql_context.sql

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
with search_path_oids(schema_oid) as (
22
select y::regnamespace::oid from unnest(current_schemas(false)) x(y)
33
),
4-
schemas_(oid, name) as (
4+
5+
schemas_with_privilege(oid, name) as (
6+
-- Schema segregated multi-tenant environments need a shortcut to
7+
-- exclude schemas without USAGE permission so context reload can ignore
8+
-- irreleavnt tenants
9+
-- https://github.com/supabase/pg_graphql/pull/480
510
select
6-
pn.oid::int, pn.nspname::text
11+
pn.oid::int,
12+
pn.nspname::text
713
from
814
pg_namespace pn
9-
-- filter to current schemas only
10-
join search_path_oids cur_schemas(oid)
11-
on pn.oid = cur_schemas.oid
1215
where
1316
pg_catalog.has_schema_privilege(
1417
current_user,
1518
pn.oid,
1619
'USAGE'
1720
)
21+
),
22+
23+
schemas_(oid, name) as (
24+
select
25+
pn.oid,
26+
pn.name
27+
from
28+
schemas_with_privilege pn
29+
-- filter to current schemas only
30+
join search_path_oids cur_schemas(oid)
31+
on pn.oid = cur_schemas.oid
1832
)
33+
1934
select
2035
jsonb_build_object(
2136
'config', jsonb_build_object(
@@ -54,6 +69,8 @@ select
5469
pg_enum pe
5570
join pg_type pt
5671
on pt.oid = pe.enumtypid
72+
join schemas_with_privilege swp
73+
on pt.typnamespace = swp.oid
5774
group by
5875
pt.oid
5976
)
@@ -93,6 +110,8 @@ select
93110
)
94111
from
95112
pg_type pt
113+
join schemas_with_privilege swp
114+
on pt.typnamespace = swp.oid
96115
left join pg_class tabs
97116
on pt.typrelid = tabs.oid
98117
),
@@ -111,6 +130,8 @@ select
111130
pg_type pt
112131
join pg_class tabs
113132
on pt.typrelid = tabs.oid
133+
join schemas_with_privilege swp
134+
on pt.typnamespace = swp.oid
114135
where
115136
pt.typcategory = 'C'
116137
and tabs.relkind = 'c'
@@ -198,6 +219,8 @@ select
198219
)
199220
from
200221
schemas_ pn
222+
join schemas_with_privilege swp
223+
on pn.oid = swp.oid
201224
),
202225
jsonb_build_object()
203226
),
@@ -419,5 +442,4 @@ select
419442
),
420443
jsonb_build_array()
421444
)
422-
423445
)

0 commit comments

Comments
 (0)