Skip to content

Live Queries: RLS context + subscription execution model #7

Description

@ox-harris

Context

We’ve identified a core tension between:

  • RLS implemented via session/connection state (e.g. current_setting('claims.user_id'))
  • long-lived live queries that may be multiplexed, deduped, or executed across pooled connections

This creates a mismatch where:

  • RLS assumes stable connection identity
  • live queries assume decoupled execution context

This can lead to:

  • connection fanout under RLS-heavy workloads
  • ambiguity in how identity is preserved across a live query lifecycle

Problem to solve

We need to define a clear and stable execution model for RLS within live queries that ensures:

  • no connection-per-subscription explosion
  • deterministic RLS behavior
  • no cross-user leakage
  • compatibility with pooled execution

Key question

What is the correct binding for RLS context in a live query system?

We need to decide whether RLS context should be:

Option A — Query-bound (recommended direction)

RLS is resolved at subscription creation time and becomes part of the query identity.

  • RLS context is attached to the live query definition
  • execution can move across pooled connections safely
  • no reliance on session state

Option B — Connection-bound

RLS continues to rely on session state (current_setting / connection identity)

  • requires sticky connections or session pinning
  • risks pool exhaustion under long-lived subscriptions

Option C — Hybrid (explicit context propagation)

RLS context is passed explicitly per execution tick

  • live query carries identity payload
  • engine injects context per WAL evaluation / re-evaluation
  • avoids session dependency but preserves dynamic identity

What we need to converge on

  • where RLS context lives (session vs query vs hybrid)
  • whether live queries are allowed to migrate across connections freely
  • whether subscription identity includes auth context
  • how we prevent connection exhaustion under RLS-heavy usage

Why this matters

This decision determines:

  • scalability of live queries under RLS workloads
  • correctness guarantees for multi-tenant systems
  • connection pool behavior under load

Once this is locked, implementation is straightforward and can be shipped quickly.


Open question

Given RLS-first architectures, what constraints or failure modes should we assume as non-negotiable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions