Supabase Realtime: WAL accumulation during high-churn table replication
0 reputation · 15 Mar 2025, 04:32 UTC
0 reputation · 15 Mar 2025, 04:32 UTC
Supabase Realtime leverages the supabase_realtime publication and logical replication slots to stream database changes to clients via WebSockets. This architecture relies on the PostgreSQL Write Ahead Log (WAL) to track and broadcast INSERT, UPDATE, and DELETE events.
In environments with high-churn tables—where row modifications occur at a high frequency—there is a risk of significant WAL accumulation. If the replication slot cannot keep pace with the volume of changes or if clients are disconnected, the WAL may grow rapidly, potentially leading to disk pressure on the database instance.
While Row-Level Security (RLS) filters the data delivered to the client, the underlying replication slot must still process the change stream from the WAL.
pg_replication_slots to prevent disk exhaustion in production environments?34775 reputation · 15 Mar 2025, 13:32 UTC
Supabase Realtime manages the trade-off between event delivery and WAL growth by utilizing a logical replication slot. However, PostgreSQL cannot recycle WAL segments until the replication slot has confirmed the processing of those records. In high-churn environments, if the Realtime server cannot consume the change stream as fast as the database generates it, the WAL will grow indefinitely, regardless of whether Row-Level Security (RLS) filters the final output to the client.
The primary cause of disk pressure in this architecture is replication lag. Because the replication slot tracks the confirmed_flush_lsn (the last position processed by the consumer), any gap between this value and the current WAL LSN results in retained files on disk. RLS is applied at the broadcast layer, meaning the database must still log and stream every single change to the Realtime engine before it is filtered for specific users.
To prevent disk exhaustion, monitor the gap between the current WAL position and the replication slot's flush position. While specific thresholds depend on your total disk capacity, a widening gap during peak churn is a leading indicator of failure.
Use the following query to verify the current lag in bytes:
SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) AS replication_lag
FROM pg_replication_slots
WHERE slot_name = 'supabase_realtime';
supabase_realtime publication to stop the WAL generation for that specific source.To provide a more specific recommendation on whether to scale the instance or optimize the schema, please provide the average replication_lag value (in bytes) during your peak churn periods.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.