Skip to content

Commit 96c4047

Browse files
authored
Merge pull request #94 from powersync-ja/reduce-memory-usage
Reduce memory usage of slot health check
2 parents 27efd52 + 6364c02 commit 96c4047

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

.changeset/young-rockets-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/service-core': patch
3+
---
4+
5+
Fix "JavaScript heap out of memory" on startup (slot health check)

packages/service-core/src/replication/WalStream.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,22 @@ export class WalStream {
209209
// We peek a large number of changes here, to make it more likely to pick up replication slot errors.
210210
// For example, "publication does not exist" only occurs here if the peek actually includes changes related
211211
// to the slot.
212-
await this.connections.pool.query({
213-
statement: `SELECT *
214-
FROM pg_catalog.pg_logical_slot_peek_binary_changes($1, NULL, 1000, 'proto_version', '1',
215-
'publication_names', $2)`,
212+
logger.info(`Checking ${slotName}`);
213+
214+
// The actual results can be quite large, so we don't actually return everything
215+
// due to memory and processing overhead that would create.
216+
const cursor = await this.connections.pool.stream({
217+
statement: `SELECT 1 FROM pg_catalog.pg_logical_slot_peek_binary_changes($1, NULL, 1000, 'proto_version', '1', 'publication_names', $2)`,
216218
params: [
217219
{ type: 'varchar', value: slotName },
218220
{ type: 'varchar', value: this.publication_name }
219221
]
220222
});
223+
224+
for await (let _chunk of cursor) {
225+
// No-op, just exhaust the cursor
226+
}
227+
221228
// Success
222229
logger.info(`Slot ${slotName} appears healthy`);
223230
return { needsInitialSync: false };

0 commit comments

Comments
 (0)