Postgres & Supabase Backend Setup
One schema, run once, works identically on Supabase's managed Postgres or your own Neon, RDS, CockroachDB, or self hosted instance. Every change is logged as a full row with an owner, so global rows reach everyone and per user rows reach only their owner. The changelog itself is closed to direct reads. What differs is how a change reaches the browser. Supabase does that part for you and everyone else needs one small bridge process, given in full below.
The schema on this page is shared, but the client side adapter is not. Pick based on where your database actually lives, not on "I am using Postgres." createSupabaseAdapter() talks directly to Supabase's own managed Realtime WebSocket service. It has nothing to connect to on Neon, RDS, CockroachDB, or a self hosted instance, because none of those run an equivalent service. If your database is Supabase, use createSupabaseAdapter() and Step 5 below. If it is anything else, use createWebSocketAdapter(), createSocketIOAdapter() or createSseAdapter() and Step 6 below. There is no scenario where createSupabaseAdapter() is the right choice for a non Supabase database.
- An existing Flux app already wired up on the client side, with the Supabase adapter chosen (Supabase path) or a WebSocket, Socket.IO or SSE adapter chosen (any other Postgres path).
- A Postgres database: a Supabase project, or your own Neon, RDS, CockroachDB or self hosted instance.
- Enough privileges on that database to create tables, functions, triggers and to revoke grants.
- A primary key column named id on every table you will track.
- A decision for every table you will track: is it GLOBAL (the same rows for everyone, safe to show to any visitor) or PER USER (rows belong to one owner)?
The changelog table
user_id column records who is allowed to read each entry. The block ends by enabling RLS and revoking every grant, so the table cannot be read through the REST API even with the anon key.Flux subscribes to your tracked tables, never to flux_changelog itself. If you added the changelog to the supabase_realtime publication and something of yours subscribes to it directly, the revokes above will stop those events. Run check g in Step 9 before deploying.
Global rows versus per user rows
flux_resolve_owner() at the moment the row changes. A global row has a null user_id. It is the same for every client, so pricing tables, blog posts and currency rates belong here, and a logged out visitor may catch up on them. A per user row stores its owner's uuid. Only that user's catch up and live push ever receive it, so projects, profiles and anything private belong here. If a per user row's owner cannot be resolved, it gets the all zero nobody uuid, which matches no real user, so the row is never shared by mistake.flux_is_global_table(). The owner function and the upgrade step both read from it, so the list can never disagree with itself.auth.uid() and returns rows where user_id is null or equals the caller. A logged out caller therefore sees global rows only. On a bridge backend the listener does the same job at broadcast time, using the owner carried in the NOTIFY.Rows of a global table go to every client that can call the delta function, and each entry carries the full row. If a table holds anything not meant for every visitor, such as unpublished drafts or internal settings, do not list it in flux_is_global_table(). Give it an owner rule instead, or do not track it. The owner function raises an error for any table without a rule, so a table you forgot fails on its first write instead of leaking. Keep that behavior. For child tables, resolve the owner through the parent, as project_domains does above.
The trigger: full rows, one entry per row
Each mutation replaces that row's entry, so flux_changelog holds at most one entry per entity. If you need an audit trail of every intermediate change, this schema is not it. Keep a separate append only log alongside it. One consequence to know: if a row's owner changes, the entry carries only the new owner, so the previous owner is never told the row left. Design owner rules so a row's owner does not change.
Do not add a public SELECT policy to flux_changelog, and do not grant it back. Entries hold full row snapshots and would expose every logged change. RLS on with no policy and no grants is correct. The SECURITY DEFINER functions are the only sanctioned read path, and they filter by caller themselves.
The trigger raises an error when a row has no id, which aborts that write. Write once to every tracked table in a staging project before enabling this in production.
Per user filtering is only as correct as your owner rules. A wrong case in flux_resolve_owner() shows one user's rows to another, so test each table with two different accounts before shipping.
What actually differs: reaching the browser, not the schema
LISTEN and NOTIFY only reach other database connections. Supabase closes that gap for you: enable Replication on a table and its managed Realtime service broadcasts every change over a WebSocket it already runs, filtered per subscriber by RLS. Every other provider gives you the database half only. The bridge from NOTIFY to a client connection is something you build once, as a small always on process. Step 5 covers Supabase and Step 6 covers everyone else.On Supabase: enable Replication, then install the delta functions
createSupabaseAdapter() subscribes to the Realtime channel and needs nothing more from you for live events. Catch up on reconnect uses three functions: the single table delta, a batch version that serves every table in one HTTP request, and the reconcile function that confirms deletions the delta window cannot see. The delta functions return their answer as the RPC result, so it reaches only the user who asked.flux_changelog has RLS enabled and every grant revoked by design. An INVOKER version runs as the calling role and is refused by RLS. The call either errors or returns an empty rows array, which a client would read as "nothing changed." Use SECURITY DEFINER as shown, keep the explicit GRANT EXECUTE to authenticated, and let the function filter by auth.uid() itself.
Older versions of these functions returned nothing and broadcast the reply with realtime.send on a topic shared by every client subscribed to that table. Anyone could listen in on other users' replies. The current functions return the rows as the RPC result instead, so no shared topic exists. The reply also carries the uid it was built for, so an adapter can discard a reply that was prepared for a different session. Installing this step drops the old signatures, and with them the broadcast path. Between running the SQL and deploying the matching client, an older client cannot catch up and falls back to refetching.
Supabase's postgres_changes broadcasts are filtered per subscriber by the RLS policies that already govern that table, so a client only receives changes to rows it could SELECT. That is a real advantage over the bridge in Step 6, where you filter by owner yourself. Keep real RLS policies on your tracked tables, because the changelog filtering only covers catch up.
On any other Postgres: the bridge process, in full
LISTEN connection and translating each NOTIFY into a call against the registry your WebSocket, Socket.IO or SSE page already built. It reads the owner from the entry and passes it along, so a per user row reaches only its owner's connections and a global row reaches everyone. The trigger from Step 3 already sends the NOTIFY this process listens for.If your DATABASE_URL points at a transaction mode pooler (PgBouncer, Supabase's pooler on port 6543, RDS Proxy in transaction mode), LISTEN silently does not work through it. The pooler can hand your session's connection to another client between transactions, so notifications never reach the one that issued LISTEN. The listener needs its own connection string pointed at the database directly (session mode, port 5432).
The Supabase delta functions above are not used on this path. Your WebSocket, Socket.IO or SSE server answers catch up itself, by running the same query as flux_request_delta against flux_changelog with the connected user's id: rows where user_id is null or user_id = that user, ordered by changed_at, answering too old past the retention window. The server connects as a trusted role, so it must apply that filter itself. Reconcile is the flux_reconcile query run under that user's identity.
Retention
flux_changelog_cleanup refuses to keep less than 14 days, so a scheduler passing a smaller number cannot break catch up. The function is also locked to the database owner. Without that lock, anyone with the anon key could empty the changelog.Upgrading from an older schema
Testing your setup
auth.uid(), that reconcile reports missing ids, and that the privileges and RLS are what this page promises.authenticated, not a client bug.One listener process, not one per server instance. If you run several copies of your backend behind a load balancer, run the Step 6 bridge in only one of them, or each replica will LISTEN and broadcast every change again.
Listener reconnect. The minimal error handler only logs a dropped connection. Production code should reconnect with backoff and issue LISTEN flux_changes again. Single state dedup softens the blow, because catch up on reconnect fetches each row's current entry, but live push stops silently in the meantime.
Owner rules are your access control. Whether a client sees a row is decided by flux_resolve_owner() and flux_is_global_table(), on both the Supabase path and the bridge path. Review both whenever you attach the trigger to a new table.
Lock down every flux function. SECURITY DEFINER functions in the public schema are callable as RPCs by default. Run check e in Step 9 after every change. Only the delta functions and reconcile should be open to signed in users.
Server clock only. Timestamps and the sync anchor come from the database clock. The five second overlap in server_now exists so a transaction that commits late is delivered again on the next catch up. Applying a row twice is harmless. A transaction that stays open longer than the overlap before committing can still be missed, so keep transactions on tracked tables short.
Rate of catch up. The batch function caps at 50 tables per call, 2000 entries per table and 10000 entries per call. Past that the answer is too old and the client refetches, which is the correct outcome.
This page covers the database and delivery layer only. For the server code the Step 6 bridge forwards into, see getting_started/websocket, getting_started/socketio or getting_started/sse. For the deeper explanation of sync anchors and delta catch up across every transport, see core_concepts/sync_anchors.