Replies: 1 comment 3 replies
-
Streaming Query Results in PGliteCurrently PGlite does not have built-in streaming support, but here are workarounds: Option 1: Cursor-based paginationconst BATCH_SIZE = 1000;
let offset = 0;
while (true) {
const { rows } = await db.query(
`SELECT * FROM large_table LIMIT ${BATCH_SIZE} OFFSET ${offset}`
);
if (rows.length === 0) break;
for (const row of rows) {
// Process each row
await processRow(row);
}
offset += BATCH_SIZE;
}Option 2: Keyset pagination (better for large datasets)let lastId = 0;
while (true) {
const { rows } = await db.query(
`SELECT * FROM large_table WHERE id > $1 ORDER BY id LIMIT 1000`,
[lastId]
);
if (rows.length === 0) break;
for (const row of rows) {
await processRow(row);
}
lastId = rows[rows.length - 1].id;
}Option 3: Use live queries for reactive updatesimport { live } from "@electric-sql/pglite/live";
const { rows, unsubscribe } = await db.live.query(
`SELECT * FROM table WHERE processed = false LIMIT 100`
);For true streaming, consider opening a feature request - PostgreSQL supports cursors which could potentially be exposed. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
For memory considerations, I'm searching for a way to stream a query result (onData callback or async iterator) without getting the whole result into memory...
Is there a way to do this in pglite?
Beta Was this translation helpful? Give feedback.
All reactions