Skip to content

Subqueries with tagged_subqueries not re-evaluating when dependent table changes #3769

@JohannesBin

Description

@JohannesBin

Environment

  • Electric server: electricsql/electric:latest (self-hosted on Railway)
  • Feature flags: ELECTRIC_FEATURE_FLAGS=allow_subqueries,tagged_subqueries
  • Client packages:
    • @electric-sql/pglite-sync: 0.4.1
    • @electric-sql/pglite: 0.3.15
    • @electric-sql/client: 1.4.0
  • Using syncShapeToTable from @electric-sql/pglite-sync

Description

We're building a multi-tenant app where users belong to workspaces. We need to sync profiles of all users in the same workspace as the current user.

Our WHERE clause for the profiles shape uses a subquery:

id IN (SELECT user_id FROM workspace_members WHERE workspace_id = 'known_workspace_id')

The issue: When a new user joins the workspace (new row inserted into workspace_members), their profile does NOT sync to existing workspace members - even with tagged_subqueries enabled, even after client-side refresh.

Steps to Reproduce

  1. User A creates a workspace and starts syncing profiles
  2. User B (already has a profile) is added to User A's workspace via direct SQL insert
  3. User A's workspace_members shape updates (shows 5 members)
  4. User A's profiles shape does NOT update (still shows 4 profiles)
  5. Client detects mismatch, triggers refresh (stops sync, clears table, clears Electric metadata, restarts)
  6. Even after refresh, the new profile is not returned

Relevant logs

[collections] workspace_members row count after sync: 5
[collections] profiles row count after sync: 5
[collections] useWorkspaceSettings: querying profiles for IDs: Array(5)
[collections] useWorkspaceSettings: profilesResult returned: Array(4)  // missing the new member!
[collections] useWorkspaceSettings: missing 1 profiles, refreshing...
[collections] refreshTables called for: profiles
[collections] stopped sync for profiles (refresh)
[collections] cleared profiles for refresh
[collections] cleared Electric metadata for profiles
[collections] restarting sync for profiles
[collections] initial sync complete for profiles
[collections] profilesResult returned: Array(4)  // STILL missing after fresh sync!

Schema (simplified)

CREATE TABLE profiles (
  id UUID PRIMARY KEY,
  email TEXT
);

CREATE TABLE workspace_members (
  id UUID PRIMARY KEY,
  workspace_id UUID NOT NULL,
  user_id UUID NOT NULL REFERENCES profiles(id),
  role TEXT NOT NULL
);

Questions

Per RFC #2931 and @balegas's comment, tagged_subqueries should "handle reactivity if any data arrives in the inner query." Our inner query is workspace_members WHERE workspace_id = 'X', and when new rows are inserted, the outer profiles shape should update.

  1. Does tagged_subqueries actually re-evaluate subqueries when dependent tables change?
  2. Why does a fresh shape request (new sync after clearing metadata) still not return the newly matching row?
  3. Is the subquery result cached somewhere on the Electric server that persists across client reconnects?

Thanks for any guidance!

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