Skip to content

fix: deliver Postgres Changes for partitioned tables published via the partition root - #2488

Open
mandar1045 wants to merge 10 commits into
supabase:mainfrom
mandar1045:fix/partitioned-tables-via-root
Open

mandar1045 wants to merge 10 commits into
supabase:mainfrom
mandar1045:fix/partitioned-tables-via-root

Conversation

@mandar1045

@mandar1045 mandar1045 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2487

With publish_via_partition_root = true, pg_publication_tables lists only the partitioned table, but wal2json reports each change under the leaf partition that holds the row. list_changes built add-tables from that view, so the leaf was filtered out and every change was dropped while the client stayed Subscribed.

list_changes and list_changes_sync now watch the leaf partitions of each published table, and report a leaf's change as the table it's published as before apply_rls. That's what pgoutput does with the same option, and it means RLS, column privileges and the payload table all use the parent. Without publish_via_partition_root the publication already lists the leaves, so nothing changes there.

  • New migration recreating list_changes and list_changes_sync with that change only
  • Regenerated tenant_schema and the pg15/pg17 dumps
  • Walrus test test_partitioned_table (both settings, both functions, RLS on the parent)
  • Integration tests for INSERT/UPDATE/DELETE on the parent, and for the default setting

…e partition root

With publish_via_partition_root, pg_publication_tables lists only the
partitioned table, but wal2json reports each change under the leaf
partition holding the row. list_changes built add-tables from that view,
so the change was filtered out and never delivered.

Watch the leaf partitions of each published table, and report a leaf's
change as the table it is published as before apply_rls, like pgoutput
does with the same option.

Fixes supabase#2487
@mandar1045
mandar1045 force-pushed the fix/partitioned-tables-via-root branch from f52b282 to 0742fae Compare November 3, 2026 15:05
# Conflicts:
#	lib/realtime/tenants/migrations.ex
#	priv/repo/tenant_db_dump_15.sql
#	priv/repo/tenant_db_dump_17.sql
The migration was never added to Realtime.Tenants.Migrations, so tenants
built from migrations (WALRUS --migrations, mix test tenant DBs) kept the
old list_changes and dropped changes from partitions published via the
partition root.

Register it, move it after the latest shipped migration (20261111120000)
so the list stays in version order, and record the version in the tenant
DB dumps.
@mandar1045
mandar1045 force-pushed the fix/partitioned-tables-via-root branch from ffd15d9 to ee6d589 Compare November 23, 2026 21:34
@edgurgel

edgurgel commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

What happens when the parent table has no primary key but the partitions have? Will this cause issues with the RLS check?

@edgurgel

edgurgel commented Dec 3, 2026

Copy link
Copy Markdown
Member

And I'm wondering if we first want to set-up a benchmark tool for walrus as this is a significant change 🤔

@mandar1045

Copy link
Copy Markdown
Contributor Author

The issue is that wal2json sends the partition's PK, so the RLS check runs against the parent using only id = 1. If another partition has a row with the same ID, it can match the wrong user's row and expose their data.

I'll push a fix to make partitions use the parent's PK. If the parent doesn't have one, it'll return the usual "no primary key" error. I'll also add a walrus test to cover this case.

For the benchmark, dev/bench/list_changes.exs from #2272 already covers it. the main branch takes 5.15s, while this PR takes 5.35s (around 3% slower). With the fix, it's back to 5.15s since the extra lookup only runs for partitioned tables.

wal2json sends the partition's own primary key. If the parent has no
primary key, the RLS check on the parent can match a row with the same
id in another partition, so a subscriber could get someone else's row.
Use the parent's key instead, which gives the usual no primary key
error when there isn't one.
@mandar1045

Copy link
Copy Markdown
Contributor Author

Pushed the fix, with a walrus test for it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres Changes drops partitioned-table changes when the publication uses publish_via_partition_root = true

2 participants