Skip to content

WPB-29574: migrate client store to PostGreSQL - #5647

Open
blackheaven wants to merge 24 commits into
developfrom
gdifolco/WPB-29574-migration-postgres-client-store
Open

blackheaven wants to merge 24 commits into
developfrom
gdifolco/WPB-29574-migration-postgres-client-store

Conversation

@blackheaven

@blackheaven blackheaven commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

https://wearezeta.atlassian.net/browse/WPB-29574

Checklist

  • Add a new entry in an appropriate subdirectory of changelog.d
  • Read and follow the PR guidelines

@blackheaven
blackheaven requested review from a team as code owners October 23, 2026 12:47
@zebot zebot added the ok-to-test Approved for running tests in CI, overrides not-ok-to-test if both labels exist label Oct 23, 2026
@blackheaven
blackheaven force-pushed the gdifolco/WPB-29574-migration-postgres-client-store branch 2 times, most recently from ad296d7 to 782b39b Compare October 23, 2026 13:27
@blackheaven
blackheaven added this pull request to stack #5650 October 23, 2026 13:35
@blackheaven
blackheaven force-pushed the gdifolco/WPB-29574-migration-postgres-client-store branch 2 times, most recently from b04f3fa to 46217df Compare October 29, 2026 17:41
@blackheaven
blackheaven force-pushed the gdifolco/WPB-29574-migration-postgres-client-store branch 3 times, most recently from d4c0205 to 8068c53 Compare November 10, 2026 08:37
@blackheaven
blackheaven force-pushed the gdifolco/WPB-29574-migration-postgres-client-store branch 3 times, most recently from 162c188 to c826048 Compare November 17, 2026 14:06
@blackheaven
blackheaven force-pushed the gdifolco/WPB-29574-migration-postgres-client-store branch 4 times, most recently from dde7b62 to 8cff7fd Compare December 3, 2026 08:50
@battermann battermann self-assigned this Dec 3, 2026
…ore tables

The client-store migration creates clients/prekeys/mls_public_keys with CREATE
TABLE IF NOT EXISTS and never alters an existing table; document that operators
with stale tables from an older brig-on-PostgreSQL era must reconcile or drop
them before enabling the migration. postgres-schema.sql already carries all
three table blocks (verified against 20261023090000-client-store.sql).
- Aggregate multiple MLS signature schemes per client with Map.unionWith (<>) in groupMLSPublicKeysByUsers and add Wire.ClientStore.PostgresSpec.
- Reject postgresMigration.clients: migration-to-postgresql at brig startup (Brig.App.newEnv) and fail loudly in Brig.CanonicalInterpreter and removeClientPrekeys.
- Guard hack/helm_vars/common.yaml.gotmpl against PREFERRED_STORE=migration-to-postgresql for clients.
- Update config-options.md (remove non-existent migrateClients, fix client-store migration SQL filename) and add clients: postgresql to gundeck.integration.yaml after #5663 rebase.
@blackheaven
blackheaven force-pushed the gdifolco/WPB-29574-migration-postgres-client-store branch from 8cff7fd to 5179937 Compare December 3, 2026 21:07
@battermann
battermann requested a lite review from Copilot December 4, 2026 07:19

@battermann battermann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical concurrency bug remains in prekey claiming: SKIP LOCKED can incorrectly trigger fallback to the last-resort key while regular prekeys are still available.

1 open issue
What changed in this PR

Adds PostgreSQL support for Brig’s client store, including clients, prekeys, and MLS public keys.

Changes:

  • Implements PostgreSQL storage, schema, migrations, and type marshalling.
  • Wires client-store configuration through services and deployment templates.
  • Adds tests, documentation, and a changelog entry.
File Description
services/​gundeck/​gundeck.integration.yaml Adds client-store migration configuration.
services/​galley/​galley.integration.yaml Adds client-store migration configuration.
services/​brig/​test/​integration/​API/​User/​Client.hs Supports PostgreSQL prekey deletion in integration tests.
services/​brig/​src/​Brig/​CanonicalInterpreter.hs Selects the Cassandra or PostgreSQL client-store implementation.
services/​brig/​src/​Brig/​App.hs Rejects unsupported client-store migration modes.
services/​brig/​brig.integration.yaml Enables the PostgreSQL client store for integration tests.
services/​background-worker/​test/​Test/​Wire/​Util.hs Updates test migration configuration.
services/​background-worker/​test/​Test/​Wire/​BackendNotificationPusherSpec.hs Updates backend notification test configuration.
services/​background-worker/​background-worker.integration.yaml Adds client-store migration configuration.
postgres-schema.sql Adds client-store tables and constraints to the PostgreSQL schema.
libs/​wire-subsystems/​wire-subsystems.cabal Registers the PostgreSQL client-store implementation and tests.
libs/​wire-subsystems/​test/​unit/​Wire/​ClientStore/​PostgresSpec.hs Adds unit tests for PostgreSQL client-store behavior.
libs/​wire-subsystems/​src/​Wire/​PostgresMigrationOpts.hs Adds the client-store migration option.
libs/​wire-subsystems/​src/​Wire/​ClientStore/​Postgres.hs Implements PostgreSQL client-store operations.
libs/​wire-subsystems/​postgres-migrations/​20261130101500-client-store.sql Creates the PostgreSQL client-store tables.
libs/​wire-api/​test/​unit/​Test/​Wire/​API/​Roundtrip/​PostgresMarshall.hs Adds PostgreSQL marshalling round-trip tests.
libs/​wire-api/​src/​Wire/​API/​User/​Client/​Prekey.hs Adds PostgreSQL prekey marshalling conversions.
libs/​wire-api/​src/​Wire/​API/​User/​Client.hs Adds PostgreSQL client type and capability marshalling.
libs/​wire-api/​src/​Wire/​API/​MLS/​CipherSuite.hs Adds PostgreSQL signature-scheme marshalling.
hack/​helm_vars/​wire-server/​values.yaml.gotmpl Renders client-store configuration in the Wire Server values template.
hack/​helm_vars/​common.yaml.gotmpl Sets client-store configuration defaults.
docs/​src/​developer/​reference/​config-options.md Documents client-store configuration and migration limitations.
charts/​wire-server/​values.yaml Adds the default client-store chart setting.
changelog.d/​5-internal/​WPB-29574-migration-postgres-client-store Records the PostgreSQL client-store migration.

Review effort: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical active finding shows that deleting a client leaves orphaned MLS public-key rows, causing persistent data and future duplicate-key issues.

1 open issue
1 resolved since last review

Review effort: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The change retains a critical concurrent-claim race that can serve a last-resort key while regular keys remain, and it also rejects duplicate prekey IDs with a PostgreSQL error instead of Cassandra-compatible behavior.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

High severity Retry regular-key claim before falling back to last-resort key

libs/​wire-subsystems/​src/​Wire/​ClientStore/​Postgres.hs:462

Under PostgreSQL's READ COMMITTED, two concurrent claims can choose the same regular row in this subquery. The second statement waits on FOR UPDATE; after the first transaction deletes the row, its outer DELETE can return no row, and claimPrekeyImpl then falls back directly to selectLastPrekeyStatement, serving the last-resort key even though other regular prekeys remain. Retry/reselect the regular-key claim after an empty result (or use a concurrency-safe claim transaction) before falling back to the last-resort key.

Medium severity Deduplicate prekeys before PostgreSQL upsert

libs/​wire-subsystems/​src/​Wire/​ClientStore/​Postgres.hs:146

checkPrekeyBundle validates each bundle but does not require distinct IDs, and the public schema accepts arbitrary prekey arrays. Cassandra handles duplicate IDs by issuing separate inserts (the last value wins), whereas this single INSERT ... SELECT ... ON CONFLICT DO UPDATE raises PostgreSQL's cardinality-violation error when the input contains the same (user_id, client_id, key) more than once. A malformed or buggy client can therefore turn an otherwise accepted update/registration into a server error; deduplicate the bundles with Cassandra-compatible last-write-wins semantics before batching, or insert them individually.

1 resolved since last review

Review effort: Lite

@battermann

Copy link
Copy Markdown
Contributor

Deduplicate prekeys before PostgreSQL upsert

this is not critical. it is unlikely that clients send multiple keys with the same ID in one request. if they do the request will fail, that is it. it does not affect any other parts of the system.

@battermann

Copy link
Copy Markdown
Contributor

Retry regular-key claim before falling back to last-resort key

this is not 100% correct. I tested this and verified the following:

  • session A deletes the only regular prekey in an open transaction
  • session B waits on that row
  • when A rolls back, the deletion is undone
  • so B claims the regular prekey
  • once B commits, only the last-resort prekey remains

I think this is correct.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test Approved for running tests in CI, overrides not-ok-to-test if both labels exist

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants