Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

realtime: fix code example on trigger (#34485)

Filipe Cabaço5bdd8605fcc0
diff --git a/apps/docs/content/guides/realtime/subscribing-to-database-changes.mdx b/apps/docs/content/guides/realtime/subscribing-to-database-changes.mdx
index 138d27cc53c..d15ec3d4bbc 100644
--- a/apps/docs/content/guides/realtime/subscribing-to-database-changes.mdx
+++ b/apps/docs/content/guides/realtime/subscribing-to-database-changes.mdx
@@ -34,19 +34,19 @@ Let's create a function that we can call any time a record is created, updated,
{/* prettier-ignore */}
```sql
-create or replace function public.your_table_changes()
-returns trigger
+create or replace function public.your_table_changes()
+returns trigger
language plpgsql
as $$
begin
perform realtime.broadcast_changes(
- 'topic:' || NEW.id::text, -- topic - the topic to which we're broadcasting
- TG_OP, -- event - the event that triggered the function
- TG_OP, -- operation - the operation that triggered the function
- TG_TABLE_NAME, -- table - the table that caused the trigger
- TG_TABLE_SCHEMA, -- schema - the schema of the table that caused the trigger
- NEW, -- new record - the record after the change
- OLD -- old record - the record before the change
+ 'topic:' || coalesce(NEW.topic, OLD.topic) ::text, -- topic - the topic to which we're broadcasting
+ TG_OP, -- event - the event that triggered the function
+ TG_OP, -- operation - the operation that triggered the function
+ TG_TABLE_NAME, -- table - the table that caused the trigger
+ TG_TABLE_SCHEMA, -- schema - the schema of the table that caused the trigger
+ NEW, -- new record - the record after the change
+ OLD -- old record - the record before the change
);
return null;
end;
@@ -60,7 +60,7 @@ Let's set up a trigger so the function is executed after any changes to the tabl
{/* prettier-ignore */}
```sql
create trigger handle_your_table_changes
-after insert or update or delete
+after insert or update or delete
on public.your_table
for each row
execute function your_table_changes ();