Repository navigation
Description
Bug report
- I confirm this is a bug with Supabase, not with my own application.
- I searched issues and PRs for this behavior.
Describe the bug
A client subscribes to Postgres Changes with a filter like status=eq.pending on an enum column. Later a migration runs ALTER TYPE order_status RENAME VALUE 'pending' TO 'queued'. From then on every change to that table makes the list_changes poll fail, and no subscriber of the project gets any change, for any table, while that subscription exists. The slot has already moved past the changes the failed poll read, so they are lost.
The same happens when a filtered column changes type and the stored value no longer fits it. ref=eq.abc breaks after ALTER COLUMN ref TYPE integer, and paid=is.true breaks after a boolean column becomes text.
To reproduce
This uses the WALRUS setup from test/walrus, with setup.sql and fixtures.sql loaded for seed_uuid, clear_wal and the walrus view.
select 1 from pg_create_logical_replication_slot('realtime', 'wal2json', false);
create type public.order_status as enum ('pending', 'shipped');
create table public.orders(id int primary key, status public.order_status);
create table public.notes(id int primary key, body text);
insert into realtime.subscription(subscription_id, entity, claims, filters)
values
(seed_uuid(1), 'public.orders',
jsonb_build_object('role', 'authenticated', 'sub', seed_uuid(1)::text),
array[('status', 'eq', 'pending', false)]::realtime.user_defined_filter[]),
(seed_uuid(2), 'public.notes',
jsonb_build_object('role', 'authenticated', 'sub', seed_uuid(2)::text),
null);
select clear_wal();
alter type public.order_status rename value 'pending' to 'queued';
insert into public.orders values (1, 'queued');
insert into public.notes values (1, 'unrelated');
select rec, subscription_ids, errors from walrus;
select count(*) as left_in_slot from pg_logical_slot_peek_changes('realtime', null, null);Expected behavior
The subscription on public.notes gets its insert. The filter on public.orders can no longer match a row, so at most that one subscription stops getting changes.
Actual behavior
ERROR: invalid input value for enum order_status: "pending"
QUERY: select 'queued'::order_status = ('pending'::order_status)
CONTEXT: PL/pgSQL function realtime.check_equality_op(realtime.equality_op,regtype,text,text,boolean) line 53 at EXECUTE
SQL function "is_visible_through_filters" statement 1
PL/pgSQL function realtime.apply_rls(jsonb,integer) line 199 at FOR over SELECT rows
left_in_slot is 0 after the error, so the insert into public.notes is gone too.
Cause
subscription_check_filters checks the filter value against the column type once, when the subscription is created. For every change, is_visible_through_filters calls check_equality_op, which casts the stored value to the current type of the column in dynamic SQL, in priv/repo/tenant_schema/realtime/functions/check_equality_op.sql. Nothing catches the error, so it leaves the subscription loop in apply_rls and aborts the poll.
#2098 shows the same failure for filters that subscription_check_filters accepts but check_equality_op can't evaluate, and #2099 rejects those at subscribe time. A subscribe time check can't catch this case, because the filter was valid when the client subscribed. #2361 was the same kind of failure for array and jsonb values.
I have a fix with WALRUS and integration tests and will open a PR for it.
System information
main at eb66ff47, on supabase/postgres:17.12.0.000 and supabase/postgres:15.20.0.000.
Activity