Skip to content

Conversation

stevensJourney
Copy link
Collaborator

@stevensJourney stevensJourney commented Jan 28, 2025

Overview

This PR adds support for using the same Postgres server as both the replication source and sync bucket storage. The same database is also supported, but this should be avoided if possible. This change aims to simplify self-hosting for certain users.

Currently, using the same server is not recommended due to the risk of WAL feedback loops. The Postgres replication module consumes a replication slot using the pgoutput plugin and emits keepalive events via pg_logical_emit_message. On older versions of Postgres, the contents of these keepalive events cannot be read, so the module assumes that any empty replication slot event is a keepalive event.

A feedback loop occurs as follows:

  • The replicator writes data to the cluster, advancing the WAL LSN (Write-Ahead Log Log Sequence Number).
  • This WAL progression generates a replication slot event.
  • The event is misinterpreted as a keepalive event, prompting another write to the cluster.
  • The cycle repeats.

Starting with Postgres 14, the pgoutput plugin provides access to the contents of logical replication messages. This allows the module to properly distinguish between keepalive events (which we emit) and other external WAL events, preventing feedback loops.

To maintain compatibility, an explicit error will be thrown if the same Postgres cluster is used for both the replication source and sync bucket storage on versions below 14. This ensures:

Postgres < 14 remains supported if separate clusters are used.
Postgres 14+ is required if using the same cluster for both purposes.

Additional Items

Some minor Postgres cleanup is also included here. This includes:

  • Cleanup for some Postgres persisted batch queries

Some bugs were identified:

During testing a bug was observed with the Postgres active checkpoint notifications. A crash could occur if a client subscribed to the notification stream after a connection had timed out. This issue has been resolved by cleaning up the listener flow and connection configuration.

Copy link

changeset-bot bot commented Jan 28, 2025

🦋 Changeset detected

Latest commit: 834b990

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@powersync/service-module-postgres-storage Minor
@powersync/service-module-postgres Minor
@powersync/service-module-mongodb-storage Minor
@powersync/service-core Minor
@powersync/service-module-mongodb Patch
@powersync/service-module-mysql Patch
@powersync/service-image Patch
@powersync/service-core-tests Patch
test-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@rkistner rkistner left a comment

Choose a reason for hiding this comment

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

Apart from the mongodb system identifier, I'm happy with the changes. It looks like the MongoDB one is not really used, but if it's there I think we need to make sure the implementation is working properly.

@stevensJourney stevensJourney changed the title [Postgres Storage] Ability to use the same cluster [Postgres Storage] Ability to use the same server Jan 29, 2025
@stevensJourney stevensJourney marked this pull request as ready for review January 30, 2025 14:28
@stevensJourney stevensJourney merged commit 23fb49f into main Feb 3, 2025
15 checks passed
@stevensJourney stevensJourney deleted the pg-storage-2 branch February 3, 2025 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants