Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- migrate:up

CREATE OR REPLACE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
cmd record;
BEGIN
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
LOOP
IF cmd.command_tag IN (
'CREATE SCHEMA', 'ALTER SCHEMA'
, 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
, 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
, 'CREATE VIEW', 'ALTER VIEW'
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
, 'CREATE FUNCTION', 'ALTER FUNCTION'
, 'CREATE TYPE', 'ALTER TYPE'
, 'CREATE RULE'
, 'COMMENT'
)
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
-- also exclude any objects inside Supabase schemas
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add more schemas here... but for now, realtime was the one that was triggering notifications frequently.

-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')

@laurenceisla laurenceisla Sep 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I used:

    OR (
      -- we validate if the trigger is created on a temporary table (relpersistence = 't')
      cmd.command_tag = 'CREATE TRIGGER'
      AND NOT EXISTS (
        SELECT 1
        FROM pg_class c JOIN pg_trigger t ON c.oid = t.tgrelid
        WHERE t.oid = cmd.objid AND c.relpersistence = 't'
      )
    )

But this doesn't work for pg_event_trigger_dropped_objects (the trigger is already dropped). The current implementation does a similar job, so I kept it the same for both event triggers.

THEN
NOTIFY pgrst, 'reload schema';
END IF;
END LOOP;
END; $$;


CREATE OR REPLACE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
obj record;
BEGIN
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
LOOP
IF obj.object_type IN (
'schema'
, 'table'
, 'foreign table'
, 'view'
, 'materialized view'
, 'function'
, 'type'
, 'rule'
)
AND obj.is_temporary IS false -- no pg_temp objects
-- also exclude any objects inside Supabase schemas
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
END LOOP;
END; $$;

-- migrate:down
11 changes: 8 additions & 3 deletions migrations/schema-15.sql
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,15 @@ BEGIN
, 'CREATE VIEW', 'ALTER VIEW'
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
, 'CREATE FUNCTION', 'ALTER FUNCTION'
, 'CREATE TRIGGER'
, 'CREATE TYPE', 'ALTER TYPE'
, 'CREATE RULE'
, 'COMMENT'
)
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
AND cmd.schema_name is distinct from 'pg_temp'
-- also exclude any objects inside Supabase schemas
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
Expand All @@ -372,11 +374,14 @@ BEGIN
, 'view'
, 'materialized view'
, 'function'
, 'trigger'
, 'type'
, 'rule'
)
AND obj.is_temporary IS false -- no pg_temp objects
-- also exclude any objects inside Supabase schemas
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
Expand Down
11 changes: 8 additions & 3 deletions migrations/schema-17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,15 @@ BEGIN
, 'CREATE VIEW', 'ALTER VIEW'
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
, 'CREATE FUNCTION', 'ALTER FUNCTION'
, 'CREATE TRIGGER'
, 'CREATE TYPE', 'ALTER TYPE'
, 'CREATE RULE'
, 'COMMENT'
)
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
AND cmd.schema_name is distinct from 'pg_temp'
-- also exclude any objects inside Supabase schemas
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
Expand All @@ -373,11 +375,14 @@ BEGIN
, 'view'
, 'materialized view'
, 'function'
, 'trigger'
, 'type'
, 'rule'
)
AND obj.is_temporary IS false -- no pg_temp objects
-- also exclude any objects inside Supabase schemas
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
Expand Down
11 changes: 8 additions & 3 deletions migrations/schema-orioledb-17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,15 @@ BEGIN
, 'CREATE VIEW', 'ALTER VIEW'
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
, 'CREATE FUNCTION', 'ALTER FUNCTION'
, 'CREATE TRIGGER'
, 'CREATE TYPE', 'ALTER TYPE'
, 'CREATE RULE'
, 'COMMENT'
)
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
AND cmd.schema_name is distinct from 'pg_temp'
-- also exclude any objects inside Supabase schemas
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
Expand All @@ -387,11 +389,14 @@ BEGIN
, 'view'
, 'materialized view'
, 'function'
, 'trigger'
, 'type'
, 'rule'
)
AND obj.is_temporary IS false -- no pg_temp objects
-- also exclude any objects inside Supabase schemas
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
THEN
NOTIFY pgrst, 'reload schema';
END IF;
Expand Down
Loading