-
-
Notifications
You must be signed in to change notification settings - Fork 268
fix: pgrst_ddl_watch triggering PostgREST notifications unnecessarily #2464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
laurenceisla
wants to merge
1
commit into
develop
Choose a base branch
from
laurence/pgrst-ddl-watch-fix
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
migrations/db/migrations/20260923021047_update_pgrst_watch_triggers.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') | ||
| -- 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.%') | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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,
realtimewas the one that was triggering notifications frequently.