Files
supabase/apps/docs
JerryandNik Richers f1e56e127f fix(docs): broadcast Realtime DELETE events to the record topic (#53579)
Closes #53584

## Problem

Two Realtime guides attach `realtime.broadcast_changes` triggers that
fire on `DELETE`:

- `apps/docs/content/guides/realtime/broadcast.mdx` —
`your_table_changes`, used by a trigger declared `AFTER INSERT OR UPDATE
OR DELETE`
- `apps/docs/content/guides/realtime/getting_started.mdx` —
`broadcast_message_changes`, used by a trigger declared `AFTER INSERT OR
UPDATE OR DELETE`

Both build the broadcast topic from the new row only:

```sql
'topic:' || NEW.id::text
'room:' || NEW.room_id::text || ':messages'
```

On `DELETE`, `NEW` is `NULL`, so the concatenation produces a `NULL`
topic. `realtime.broadcast_changes` passes that to `realtime.send`,
which inserts into `realtime.messages`. In `supabase/realtime`,
`realtime.messages.topic` is `text not null`, and `realtime.send` wraps
its insert in an exception handler that downgrades the failure to a
warning:

```sql
EXCEPTION WHEN OTHERS THEN
  RAISE WARNING 'WarnSendingBroadcastMessage: %', SQLERRM;
```

So the trigger succeeds, logs only a warning, and no `DELETE` message
reaches subscribers. The sibling guide already handles this correctly:
`subscribing-to-database-changes.mdx` uses `coalesce(NEW.id, OLD.id)`.

## Fix

Use `coalesce(NEW.id, OLD.id)` in `broadcast.mdx` and
`coalesce(NEW.room_id, OLD.room_id)` in `getting_started.mdx`, matching
`subscribing-to-database-changes.mdx` so all three guides teach the same
pattern.

The `realtime.send` example in `getting_started.mdx` is unchanged
because its trigger is `AFTER INSERT` only, where `NEW` is always
present.

One caveat: `coalesce` falls back to `OLD` whenever `NEW` is null,
including an `UPDATE` that clears a nullable `room_id`. That broadcasts
to the former room, where the `CASE` form would emit a null topic and
send nothing. The example reads as if a message always belongs to a
room; for a schema where `room_id` can be cleared, use `CASE WHEN TG_OP
= 'DELETE' THEN OLD.room_id ELSE NEW.room_id END`.

## Verification

I tested the exact edited SQL blocks against
`supabase/postgres:17.6.1.143`, using the real
`realtime.broadcast_changes` and `realtime.send` definitions from
`supabase/realtime` and a `realtime.messages` table that keeps the
production `topic text not null` constraint.

Before the fix, running `INSERT`, `UPDATE`, then `DELETE` on the
`getting_started.mdx` snippet:

```text
WARNING:  WarnSendingBroadcastMessage: syntax error at or near "NULL"
 event  |      topic      |   op
--------+-----------------+--------
 INSERT | room:7:messages | INSERT
 UPDATE | room:7:messages | UPDATE
(2 rows)
```

After the fix, running insert, update body, update `room_id` to `NULL`,
move to room 9, then delete:

```text
 event  |      topic      |   op   | new_room | old_room
--------+-----------------+--------+----------+----------
 INSERT | room:7:messages | INSERT | 7        |
 UPDATE | room:7:messages | UPDATE | 7        | 7
 UPDATE | room:7:messages | UPDATE |          | 7
 UPDATE | room:9:messages | UPDATE | 9        |
 DELETE | room:9:messages | DELETE |          | 9
(5 rows)
```

The `room_id` to `NULL` update follows the caveat above; `INSERT`, a
normal `UPDATE`, a room move, and `DELETE` all use the expected topic.
The `broadcast.mdx` snippet produces `INSERT`/`UPDATE`/`DELETE` messages
with `topic:<id>`.

Prettier passes on both files.

## Authorship and review

This PR is written by me. AI assisted with drafting and with setting up
the test harness, but I reviewed and audited every change myself and I
stand behind it.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

---------

Co-authored-by: Nik Richers <nrichers@gmail.com>
2026-11-22 14:35:06 -07:00
..
2026-11-20 14:07:00 +01:00
2026-07-01 12:59:00 +02:00

Supabase Docs

This directory contains the Next.js app for supabase.com/docs.

Guides live in content/ as MDX. Reference pages are generated from the specs in spec/: OpenAPI, SDKSpec, ConfigSpec, and CLISpec. Some sections come from other repositories.

Where to go