Skip to content

Commit 9dcbbc3

Browse files
committed
fix: pgrst watch triggers firing up PostgREST notifications unnecessarily
- When a trigger is created on a temporary table - When objects are created or dropped inside Supabase schemas: auth, realtime, _realtime, storage
1 parent d04eb94 commit 9dcbbc3

4 files changed

Lines changed: 87 additions & 9 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- migrate:up
2+
3+
CREATE OR REPLACE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
4+
LANGUAGE plpgsql
5+
AS $$
6+
DECLARE
7+
cmd record;
8+
BEGIN
9+
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
10+
LOOP
11+
IF cmd.command_tag IN (
12+
'CREATE SCHEMA', 'ALTER SCHEMA'
13+
, 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
14+
, 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
15+
, 'CREATE VIEW', 'ALTER VIEW'
16+
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
17+
, 'CREATE FUNCTION', 'ALTER FUNCTION'
18+
, 'CREATE TYPE', 'ALTER TYPE'
19+
, 'CREATE RULE'
20+
, 'COMMENT'
21+
)
22+
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
23+
-- also exclude any objects inside Supabase schemas
24+
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
25+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
26+
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
27+
THEN
28+
NOTIFY pgrst, 'reload schema';
29+
END IF;
30+
END LOOP;
31+
END; $$;
32+
33+
34+
CREATE OR REPLACE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
35+
LANGUAGE plpgsql
36+
AS $$
37+
DECLARE
38+
obj record;
39+
BEGIN
40+
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
41+
LOOP
42+
IF obj.object_type IN (
43+
'schema'
44+
, 'table'
45+
, 'foreign table'
46+
, 'view'
47+
, 'materialized view'
48+
, 'function'
49+
, 'type'
50+
, 'rule'
51+
)
52+
AND obj.is_temporary IS false -- no pg_temp objects
53+
-- also exclude any objects inside Supabase schemas
54+
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
55+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
56+
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
57+
THEN
58+
NOTIFY pgrst, 'reload schema';
59+
END IF;
60+
END LOOP;
61+
END; $$;
62+
63+
-- migrate:down

‎migrations/schema-15.sql‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,15 @@ BEGIN
339339
, 'CREATE VIEW', 'ALTER VIEW'
340340
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
341341
, 'CREATE FUNCTION', 'ALTER FUNCTION'
342-
, 'CREATE TRIGGER'
343342
, 'CREATE TYPE', 'ALTER TYPE'
344343
, 'CREATE RULE'
345344
, 'COMMENT'
346345
)
347346
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
348-
AND cmd.schema_name is distinct from 'pg_temp'
347+
-- also exclude any objects inside Supabase schemas
348+
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
349+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
350+
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
349351
THEN
350352
NOTIFY pgrst, 'reload schema';
351353
END IF;
@@ -372,11 +374,14 @@ BEGIN
372374
, 'view'
373375
, 'materialized view'
374376
, 'function'
375-
, 'trigger'
376377
, 'type'
377378
, 'rule'
378379
)
379380
AND obj.is_temporary IS false -- no pg_temp objects
381+
-- also exclude any objects inside Supabase schemas
382+
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
383+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
384+
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
380385
THEN
381386
NOTIFY pgrst, 'reload schema';
382387
END IF;

‎migrations/schema-17.sql‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,15 @@ BEGIN
340340
, 'CREATE VIEW', 'ALTER VIEW'
341341
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
342342
, 'CREATE FUNCTION', 'ALTER FUNCTION'
343-
, 'CREATE TRIGGER'
344343
, 'CREATE TYPE', 'ALTER TYPE'
345344
, 'CREATE RULE'
346345
, 'COMMENT'
347346
)
348347
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
349-
AND cmd.schema_name is distinct from 'pg_temp'
348+
-- also exclude any objects inside Supabase schemas
349+
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
350+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
351+
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
350352
THEN
351353
NOTIFY pgrst, 'reload schema';
352354
END IF;
@@ -373,11 +375,14 @@ BEGIN
373375
, 'view'
374376
, 'materialized view'
375377
, 'function'
376-
, 'trigger'
377378
, 'type'
378379
, 'rule'
379380
)
380381
AND obj.is_temporary IS false -- no pg_temp objects
382+
-- also exclude any objects inside Supabase schemas
383+
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
384+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
385+
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
381386
THEN
382387
NOTIFY pgrst, 'reload schema';
383388
END IF;

‎migrations/schema-orioledb-17.sql‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,15 @@ BEGIN
354354
, 'CREATE VIEW', 'ALTER VIEW'
355355
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
356356
, 'CREATE FUNCTION', 'ALTER FUNCTION'
357-
, 'CREATE TRIGGER'
358357
, 'CREATE TYPE', 'ALTER TYPE'
359358
, 'CREATE RULE'
360359
, 'COMMENT'
361360
)
362361
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
363-
AND cmd.schema_name is distinct from 'pg_temp'
362+
-- also exclude any objects inside Supabase schemas
363+
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
364+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
365+
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
364366
THEN
365367
NOTIFY pgrst, 'reload schema';
366368
END IF;
@@ -387,11 +389,14 @@ BEGIN
387389
, 'view'
388390
, 'materialized view'
389391
, 'function'
390-
, 'trigger'
391392
, 'type'
392393
, 'rule'
393394
)
394395
AND obj.is_temporary IS false -- no pg_temp objects
396+
-- also exclude any objects inside Supabase schemas
397+
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
398+
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
399+
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
395400
THEN
396401
NOTIFY pgrst, 'reload schema';
397402
END IF;

0 commit comments

Comments
 (0)