Skip to content

Commit fb95e66

Browse files
authored
Allow set initial memory size (#957)
1 parent 65fc101 commit fb95e66

5 files changed

Lines changed: 26 additions & 1 deletion

File tree

.changeset/dry-moons-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@electric-sql/pglite': patch
3+
---
4+
5+
Allow setting initial memory size.

packages/pglite/src/pglite.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,19 @@ export class PGlite
300300
fsBundleBuffer = buffer
301301
})
302302

303+
const wasmMemory = new WebAssembly.Memory({
304+
initial: options.initialMemory
305+
? options.initialMemory / (64 * 1024)
306+
: 2048,
307+
maximum: 32768,
308+
})
309+
303310
let emscriptenOpts: Partial<PostgresMod> = {
304311
thisProgram: postgresExePath,
305312
WASM_PREFIX,
306313
arguments: args,
307314
noExitRuntime: true,
315+
wasmMemory: wasmMemory,
308316
// Provide a stdin that returns EOF to avoid browser prompt
309317
stdin: () => null,
310318
print: (text: string) => {

packages/pglite/src/postgresMod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface PostgresMod
2222
thisProgram: string
2323
stdin: (() => number | null) | null
2424
FS: FS
25+
wasmMemory: WebAssembly.Memory
2526
PROXYFS: Emscripten.FileSystemType
2627
WASM_PREFIX: string
2728
pg_extensions: Record<string, Promise<Blob | null>>

packages/pglite/tests/basic.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,17 @@ await testEsmCjsAndDTC(async (importType) => {
651651
})
652652
})
653653

654+
it('initialMemory works', async () => {
655+
const wantedMemSize = 512 * 1024 * 1024
656+
const db = await PGlite.create({
657+
initialMemory: wantedMemSize,
658+
})
659+
660+
const instanceMemSize = db.Module.HEAPU8.buffer.byteLength
661+
662+
expect(instanceMemSize).toEqual(wantedMemSize)
663+
})
664+
654665
// this tests the parameter 'max_parallel_workers_per_gather=0',
655666
it('it shouldnt use parallel workers on gather', async () => {
656667
const db = await PGlite.create()

postgres-pglite

0 commit comments

Comments
 (0)