Skip to content

Commit f1a6be4

Browse files
committed
Implement write checkpoint workaround for Aurora Postgres.
1 parent 1c99a4d commit f1a6be4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/service-core/src/util/utils.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,13 @@ export async function createWriteCheckpoint(
119119
bucketStorage: storage.BucketStorageFactory,
120120
user_id: string
121121
): Promise<bigint> {
122-
const [{ lsn }] = pgwireRows(
123-
await retriedQuery(db, `SELECT pg_logical_emit_message(false, 'powersync', 'ping') as lsn`)
124-
);
122+
// On most Postgres versions, pg_logical_emit_message() returns the correct LSN.
123+
// However, on Aurora (Postgres compatible), it can return an entirely different LSN,
124+
// causing the write checkpoints to never be replicated back to the client.
125+
// For those, we need to use pg_current_wal_lsn() instead.
126+
const [{ lsn }] = pgwireRows(await retriedQuery(db, `SELECT pg_current_wal_lsn() as lsn`));
127+
128+
await retriedQuery(db, `SELECT pg_logical_emit_message(false, 'powersync', 'ping')`);
125129

126130
const id = await bucketStorage.createWriteCheckpoint(user_id, { '1': lsn });
127131
logger.info(`Write checkpoint 2: ${JSON.stringify({ lsn, id: String(id) })}`);

0 commit comments

Comments
 (0)