From cd20be58c019a2a96e0b1016f9a89ef770ee0ef1 Mon Sep 17 00:00:00 2001 From: tudor Date: Wed, 5 Nov 2025 16:20:07 +0100 Subject: [PATCH 01/22] update submodule; improve CI --- .github/workflows/build_and_test.yml | 4 ++-- postgres-pglite | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index b39b96047..3f913505b 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -164,7 +164,7 @@ jobs: with: issue-number: ${{ github.event.pull_request.number }} comment-author: 'github-actions[bot]' - body-includes: '- PGlite:' + body-includes: '- PGlite with node:' - name: Create or update build outputs comment uses: peter-evans/create-or-update-comment@v4 @@ -174,7 +174,7 @@ jobs: comment-id: ${{ steps.fc.outputs.comment-id }} issue-number: ${{ github.event.pull_request.number }} body: | - - PGlite: ${{ steps.upload-pglite-package.outputs.artifact-url }} + - PGlite with node v${{ matrix.node }}: ${{ steps.upload-pglite-package.outputs.artifact-url }} edit-mode: append build-and-test-pglite-dependents: diff --git a/postgres-pglite b/postgres-pglite index f5f1005d5..6b2bddbcf 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit f5f1005d52e073c4e9fd7f67295bb7df7ac47552 +Subproject commit 6b2bddbcf9700eaefba4b67298fc6ae23c6a62f9 From 7556a6668841a05cbd5c674fd92366f39ce87d49 Mon Sep 17 00:00:00 2001 From: tudor Date: Wed, 5 Nov 2025 17:18:15 +0100 Subject: [PATCH 02/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 6b2bddbcf..4eef39ef3 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 6b2bddbcf9700eaefba4b67298fc6ae23c6a62f9 +Subproject commit 4eef39ef3e6064c8c46a2370ea28b9b2f879fd79 From ab17e07889e202bea5d606f2ea4deff95eec27ed Mon Sep 17 00:00:00 2001 From: tudor Date: Thu, 6 Nov 2025 10:54:04 +0100 Subject: [PATCH 03/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 4eef39ef3..86e00c2ac 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 4eef39ef3e6064c8c46a2370ea28b9b2f879fd79 +Subproject commit 86e00c2ac9005f21ffdbf2dafe0d2e01a3575d89 From a2788f7c4a9622dfd3029431719ab531ce76f1bc Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 10 Nov 2025 14:16:23 +0100 Subject: [PATCH 04/22] pg_dump improvements; update submodule --- packages/pglite-tools/src/pgDumpModFactory.ts | 2 +- packages/pglite-tools/src/pg_dump.ts | 10 +++++----- postgres-pglite | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/pglite-tools/src/pgDumpModFactory.ts b/packages/pglite-tools/src/pgDumpModFactory.ts index dadc2e7a3..67bf8b212 100644 --- a/packages/pglite-tools/src/pgDumpModFactory.ts +++ b/packages/pglite-tools/src/pgDumpModFactory.ts @@ -28,10 +28,10 @@ export interface PgDumpMod signature: string, ) => number removeFunction: (f: number) => void - _main: (args: string[]) => number onExit: (status: number) => void print: (test: string) => void printErr: (text: string) => void + callMain: (args?: string[]) => number } type PgDumpFactory = ( diff --git a/packages/pglite-tools/src/pg_dump.ts b/packages/pglite-tools/src/pg_dump.ts index 5e07d5f4e..6aa1e1515 100644 --- a/packages/pglite-tools/src/pg_dump.ts +++ b/packages/pglite-tools/src/pg_dump.ts @@ -36,11 +36,10 @@ async function execPgDump({ args: string[] }): Promise { let pgdump_write, pgdump_read - let exitStatus = 0 + let exitCode = 0 let stderrOutput: string = '' let stdoutOutput: string = '' const emscriptenOpts: Partial = { - arguments: args, noExitRuntime: false, print: (text) => { stdoutOutput += text @@ -49,7 +48,7 @@ async function execPgDump({ stderrOutput += text }, onExit: (status: number) => { - exitStatus = status + exitCode = status }, preRun: [ (mod: PgDumpMod) => { @@ -92,13 +91,14 @@ async function execPgDump({ } const mod = await PgDumpModFactory(emscriptenOpts) + mod.callMain(args) let fileContents = '' - if (!exitStatus) { + if (!exitCode) { fileContents = mod.FS.readFile(dumpFilePath, { encoding: 'utf8' }) } return { - exitCode: exitStatus, + exitCode, fileContents, stderr: stderrOutput, stdout: stdoutOutput, diff --git a/postgres-pglite b/postgres-pglite index 013e66b26..6f4ab87eb 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 013e66b260a97b6c31501528f74bfbb55985a961 +Subproject commit 6f4ab87eb44cb723cc26043fbbbdcc6f696f6467 From e919f7b7fb477a7dc68897948cd8abfd70b40a85 Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 11 Nov 2025 11:51:31 +0100 Subject: [PATCH 05/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 6f4ab87eb..4d0d09783 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 6f4ab87eb44cb723cc26043fbbbdcc6f696f6467 +Subproject commit 4d0d0978337943fbfd2b1d9adbd46c0fda1c6209 From 20cc91191a44e9b3d5b68f02c895f06725d2b29d Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 11 Nov 2025 15:41:36 +0100 Subject: [PATCH 06/22] cleanup --- packages/pglite/src/postgresMod.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/pglite/src/postgresMod.ts b/packages/pglite/src/postgresMod.ts index 2a3e8ee1e..d0f4252be 100644 --- a/packages/pglite/src/postgresMod.ts +++ b/packages/pglite/src/postgresMod.ts @@ -20,7 +20,6 @@ export interface PostgresMod preRun: Array<{ (mod: PostgresMod): void }> postRun: Array<{ (mod: PostgresMod): void }> FS: FS - FD_BUFFER_MAX: number WASM_PREFIX: string INITIAL_MEMORY: number pg_extensions: Record> From c28028a3dd4aa29d01bee90902ba861965f8e90e Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 11 Nov 2025 15:41:47 +0100 Subject: [PATCH 07/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 4d0d09783..56edaa5ef 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 4d0d0978337943fbfd2b1d9adbd46c0fda1c6209 +Subproject commit 56edaa5ef394dbd85fa82394238cc27ae3f60665 From 647b5c708b278e7485bd5b78fd9494279d94745a Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 11 Nov 2025 17:03:27 +0100 Subject: [PATCH 08/22] refactor --- packages/pglite-tools/src/pgDumpModFactory.ts | 2 +- packages/pglite-tools/src/pg_dump.ts | 2 +- packages/pglite/src/pglite.ts | 4 ++-- packages/pglite/src/postgresMod.ts | 5 ++--- postgres-pglite | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/pglite-tools/src/pgDumpModFactory.ts b/packages/pglite-tools/src/pgDumpModFactory.ts index 67bf8b212..67dd3f37e 100644 --- a/packages/pglite-tools/src/pgDumpModFactory.ts +++ b/packages/pglite-tools/src/pgDumpModFactory.ts @@ -22,7 +22,7 @@ export interface PgDumpMod FS: FS WASM_PREFIX: string INITIAL_MEMORY: number - _set_read_write_cbs: (read_cb: number, write_cb: number) => void + _pgl_set_rw_cbs: (read_cb: number, write_cb: number) => void addFunction: ( cb: (ptr: any, length: number) => void, signature: string, diff --git a/packages/pglite-tools/src/pg_dump.ts b/packages/pglite-tools/src/pg_dump.ts index 6aa1e1515..a948a61da 100644 --- a/packages/pglite-tools/src/pg_dump.ts +++ b/packages/pglite-tools/src/pg_dump.ts @@ -82,7 +82,7 @@ async function execPgDump({ return length }, 'iii') - mod._set_read_write_cbs(pgdump_read, pgdump_write) + mod._pgl_set_rw_cbs(pgdump_read, pgdump_write) // default $HOME in emscripten is /home/web_user mod.FS.chmod('/home/web_user/.pgpass', 0o0600) // https://www.postgresql.org/docs/current/libpq-pgpass.html } diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index c0da6cba8..e9e6c0ec3 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -446,7 +446,7 @@ export class PGlite return length }, 'iii') - this.mod._set_read_write_cbs(this.#pglite_read, this.#pglite_write) + this.mod._pgl_set_rw_cbs(this.#pglite_read, this.#pglite_write) // Sync the filesystem from any previous store await this.fs!.initialSyncFs() @@ -671,7 +671,7 @@ export class PGlite } // execute the message - mod._interactive_one(message.length, message[0]) + mod._pgl_interactive_one(message.length, message[0]) this.#outputData = [] diff --git a/packages/pglite/src/postgresMod.ts b/packages/pglite/src/postgresMod.ts index d0f4252be..5e89f591d 100644 --- a/packages/pglite/src/postgresMod.ts +++ b/packages/pglite/src/postgresMod.ts @@ -26,9 +26,8 @@ export interface PostgresMod _pgl_initdb: () => number _pgl_backend: () => void _pgl_shutdown: () => void - _interactive_write: (msgLength: number) => void - _interactive_one: (length: number, peek: number) => void - _set_read_write_cbs: (read_cb: number, write_cb: number) => void + _pgl_interactive_one: (length: number, peek: number) => void + _pgl_set_rw_cbs: (read_cb: number, write_cb: number) => void addFunction: ( cb: (ptr: any, length: number) => void, signature: string, diff --git a/postgres-pglite b/postgres-pglite index 56edaa5ef..b694e97ef 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 56edaa5ef394dbd85fa82394238cc27ae3f60665 +Subproject commit b694e97efed2e40492386ab4dddb11381e9c7b41 From 185f0456b083164666f27b8c3c48f8c4e7120614 Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 11 Nov 2025 17:44:33 +0100 Subject: [PATCH 09/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index b694e97ef..6491173f5 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit b694e97efed2e40492386ab4dddb11381e9c7b41 +Subproject commit 6491173f5fc551fd99d876f5917a0461528ce40a From 41ad77e31d3c1d0ad9dcc6174481f2284264468e Mon Sep 17 00:00:00 2001 From: tudor Date: Wed, 12 Nov 2025 10:32:42 +0100 Subject: [PATCH 10/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 6491173f5..54486f9aa 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 6491173f5fc551fd99d876f5917a0461528ce40a +Subproject commit 54486f9aaec073052cdc5538256ed1d33553761c From c68e149ddaef3520683ec87b7406c870181aea14 Mon Sep 17 00:00:00 2001 From: tudor Date: Wed, 12 Nov 2025 12:50:48 +0100 Subject: [PATCH 11/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 54486f9aa..07571d195 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 54486f9aaec073052cdc5538256ed1d33553761c +Subproject commit 07571d195d0bfb252a3158c2cf2d42e76e790a06 From 2f829300a5ec9961c47b80b938de6388aeff6daf Mon Sep 17 00:00:00 2001 From: tudor Date: Wed, 12 Nov 2025 15:29:04 +0100 Subject: [PATCH 12/22] update submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 07571d195..622a30e96 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 07571d195d0bfb252a3158c2cf2d42e76e790a06 +Subproject commit 622a30e96d80fd42bcba19ada7fa979f8338626a From 445c04dc824687dea65f2235536676874fb5efdf Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 16:40:03 +0100 Subject: [PATCH 13/22] do not automatically call main; refactorings --- packages/pglite/src/pglite.ts | 36 +++++++++++++++++++----------- packages/pglite/src/postgresMod.ts | 1 + postgres-pglite | 2 +- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index e9e6c0ec3..225e4d96d 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -76,14 +76,14 @@ export class PGlite #globalNotifyListeners = new Set<(channel: string, payload: string) => void>() // receive data from wasm - #pglite_write: number = -1 + #pglite_socket_write: number = -1 #currentResults: BackendMessage[] = [] #currentThrowOnError: boolean = false #currentOnNotice: ((notice: NoticeMessage) => void) | undefined // send data to wasm - #pglite_read: number = -1 + #pglite_socket_read: number = -1 // buffer that holds the data to be sent to wasm #outputData: any = [] // read index in the buffer @@ -333,6 +333,18 @@ export class PGlite mod.FS.registerDevice(devId, devOpt) mod.FS.mkdev('/dev/blob', devId) }, + // (mod: any) => { + // mod.ENV.MODE = "REACT" + // mod.ENV.PGDATA = PGDATA + // mod.ENV.PREFIX = WASM_PREFIX + // mod.ENV.PGUSER = options.username ?? 'postgres' + // mod.ENV.PGDATABASE = options.database ?? 'template1' + // mod.ENV.LC_CTYPE = 'en_US.UTF-8' + // mod.ENV.TZ = 'UTC' + // mod.ENV.PGTZ = 'UTC' + // mod.ENV.PGDATABASE = 'template1' + // mod.ENV.PG_COLOR = 'always' + // } ], } @@ -386,8 +398,8 @@ export class PGlite // Load the database engine this.mod = await PostgresModFactory(emscriptenOpts) - // set the write callback - this.#pglite_write = this.mod.addFunction((ptr: any, length: number) => { + // set the write callback + this.#pglite_socket_write = this.mod.addFunction((ptr: any, length: number) => { let bytes try { bytes = this.mod!.HEAPU8.subarray(ptr, ptr + length) @@ -400,9 +412,7 @@ export class PGlite }) if (this.#keepRawResponse) { const copied = bytes.slice() - let requiredSize = this.#writeOffset + copied.length - if (requiredSize > this.#inputData.length) { const newSize = this.#inputData.length + @@ -415,17 +425,15 @@ export class PGlite newBuffer.set(this.#inputData.subarray(0, this.#writeOffset)) this.#inputData = newBuffer } - this.#inputData.set(copied, this.#writeOffset) this.#writeOffset += copied.length - return this.#inputData.length } return length }, 'iii') // set the read callback - this.#pglite_read = this.mod.addFunction((ptr: any, max_length: number) => { + this.#pglite_socket_read = this.mod.addFunction((ptr: any, max_length: number) => { // copy current data to wasm buffer let length = this.#outputData.length - this.#readOffset if (length > max_length) { @@ -441,12 +449,14 @@ export class PGlite ) this.#readOffset += length } catch (e) { - console.log(e) + console.error(e) } return length }, 'iii') - this.mod._pgl_set_rw_cbs(this.#pglite_read, this.#pglite_write) + this.mod._pgl_set_rw_cbs(this.#pglite_socket_read, this.#pglite_socket_write) + + this.mod.callMain(args) // Sync the filesystem from any previous store await this.fs!.initialSyncFs() @@ -576,8 +586,8 @@ export class PGlite try { await this.execProtocol(serialize.end()) this.mod!._pgl_shutdown() - this.mod!.removeFunction(this.#pglite_read) - this.mod!.removeFunction(this.#pglite_write) + this.mod!.removeFunction(this.#pglite_socket_read) + this.mod!.removeFunction(this.#pglite_socket_write) } catch (e) { const err = e as { name: string; status: number } if (err.name === 'ExitStatus' && err.status === 0) { diff --git a/packages/pglite/src/postgresMod.ts b/packages/pglite/src/postgresMod.ts index 5e89f591d..9eb410f8e 100644 --- a/packages/pglite/src/postgresMod.ts +++ b/packages/pglite/src/postgresMod.ts @@ -33,6 +33,7 @@ export interface PostgresMod signature: string, ) => number removeFunction: (f: number) => void + callMain: (args?: string[]) => number } type PostgresFactory = ( diff --git a/postgres-pglite b/postgres-pglite index 622a30e96..ccef4a751 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 622a30e96d80fd42bcba19ada7fa979f8338626a +Subproject commit ccef4a751d4dd317426d8a84fa149858bb86c849 From c064d18913cb9bced1927ff1b8a781d641716cac Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 17:02:43 +0100 Subject: [PATCH 14/22] more cleanup; update submodule --- packages/pglite/src/pglite.ts | 150 ++++++++++++++++++---------------- postgres-pglite | 2 +- 2 files changed, 81 insertions(+), 71 deletions(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index 225e4d96d..cb88992de 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -214,12 +214,12 @@ export class PGlite const extensionInitFns: Array<() => Promise> = [] const args = [ - `PGDATA=${PGDATA}`, - `PREFIX=${WASM_PREFIX}`, - `PGUSER=${options.username ?? 'postgres'}`, - `PGDATABASE=${options.database ?? 'template1'}`, - 'MODE=REACT', - 'REPL=N', + // `PGDATA=${PGDATA}`, + // `PREFIX=${WASM_PREFIX}`, + // `PGUSER=${options.username ?? 'postgres'}`, + // `PGDATABASE=${options.database ?? 'template1'}`, + // 'MODE=REACT', + // 'REPL=N', // "-F", // Disable fsync (TODO: Only for in-memory mode?) ...(this.debug ? ['-d', this.debug.toString()] : []), ] @@ -333,18 +333,19 @@ export class PGlite mod.FS.registerDevice(devId, devOpt) mod.FS.mkdev('/dev/blob', devId) }, - // (mod: any) => { - // mod.ENV.MODE = "REACT" - // mod.ENV.PGDATA = PGDATA - // mod.ENV.PREFIX = WASM_PREFIX - // mod.ENV.PGUSER = options.username ?? 'postgres' - // mod.ENV.PGDATABASE = options.database ?? 'template1' - // mod.ENV.LC_CTYPE = 'en_US.UTF-8' - // mod.ENV.TZ = 'UTC' - // mod.ENV.PGTZ = 'UTC' - // mod.ENV.PGDATABASE = 'template1' - // mod.ENV.PG_COLOR = 'always' - // } + (mod: any) => { + mod.ENV.MODE = 'REACT' + mod.ENV.PGDATA = PGDATA + mod.ENV.PREFIX = WASM_PREFIX + mod.ENV.PGUSER = options.username ?? 'postgres' + mod.ENV.PGDATABASE = options.database ?? 'template1' + mod.REPL = 'N' + // mod.ENV.LC_CTYPE = 'en_US.UTF-8' + // mod.ENV.TZ = 'UTC' + // mod.ENV.PGTZ = 'UTC' + // mod.ENV.PGDATABASE = 'template1' + // mod.ENV.PG_COLOR = 'always' + }, ], } @@ -398,63 +399,72 @@ export class PGlite // Load the database engine this.mod = await PostgresModFactory(emscriptenOpts) - // set the write callback - this.#pglite_socket_write = this.mod.addFunction((ptr: any, length: number) => { - let bytes - try { - bytes = this.mod!.HEAPU8.subarray(ptr, ptr + length) - } catch (e: any) { - console.error('error', e) - throw e - } - this.#protocolParser.parse(bytes, (msg) => { - this.#parse(msg) - }) - if (this.#keepRawResponse) { - const copied = bytes.slice() - let requiredSize = this.#writeOffset + copied.length - if (requiredSize > this.#inputData.length) { - const newSize = - this.#inputData.length + - (this.#inputData.length >> 1) + - requiredSize - if (requiredSize > PGlite.MAX_BUFFER_SIZE) { - requiredSize = PGlite.MAX_BUFFER_SIZE + // set the write callback + this.#pglite_socket_write = this.mod.addFunction( + (ptr: any, length: number) => { + let bytes + try { + bytes = this.mod!.HEAPU8.subarray(ptr, ptr + length) + } catch (e: any) { + console.error('error', e) + throw e + } + this.#protocolParser.parse(bytes, (msg) => { + this.#parse(msg) + }) + if (this.#keepRawResponse) { + const copied = bytes.slice() + let requiredSize = this.#writeOffset + copied.length + if (requiredSize > this.#inputData.length) { + const newSize = + this.#inputData.length + + (this.#inputData.length >> 1) + + requiredSize + if (requiredSize > PGlite.MAX_BUFFER_SIZE) { + requiredSize = PGlite.MAX_BUFFER_SIZE + } + const newBuffer = new Uint8Array(newSize) + newBuffer.set(this.#inputData.subarray(0, this.#writeOffset)) + this.#inputData = newBuffer } - const newBuffer = new Uint8Array(newSize) - newBuffer.set(this.#inputData.subarray(0, this.#writeOffset)) - this.#inputData = newBuffer + this.#inputData.set(copied, this.#writeOffset) + this.#writeOffset += copied.length + return this.#inputData.length } - this.#inputData.set(copied, this.#writeOffset) - this.#writeOffset += copied.length - return this.#inputData.length - } - return length - }, 'iii') + return length + }, + 'iii', + ) // set the read callback - this.#pglite_socket_read = this.mod.addFunction((ptr: any, max_length: number) => { - // copy current data to wasm buffer - let length = this.#outputData.length - this.#readOffset - if (length > max_length) { - length = max_length - } - try { - this.mod!.HEAP8.set( - (this.#outputData as Uint8Array).subarray( - this.#readOffset, - this.#readOffset + length, - ), - ptr, - ) - this.#readOffset += length - } catch (e) { - console.error(e) - } - return length - }, 'iii') + this.#pglite_socket_read = this.mod.addFunction( + (ptr: any, max_length: number) => { + // copy current data to wasm buffer + let length = this.#outputData.length - this.#readOffset + if (length > max_length) { + length = max_length + } + try { + this.mod!.HEAP8.set( + (this.#outputData as Uint8Array).subarray( + this.#readOffset, + this.#readOffset + length, + ), + ptr, + ) + this.#readOffset += length + } catch (e) { + console.error(e) + } + return length + }, + 'iii', + ) - this.mod._pgl_set_rw_cbs(this.#pglite_socket_read, this.#pglite_socket_write) + this.mod._pgl_set_rw_cbs( + this.#pglite_socket_read, + this.#pglite_socket_write, + ) this.mod.callMain(args) diff --git a/postgres-pglite b/postgres-pglite index ccef4a751..6c44712ba 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit ccef4a751d4dd317426d8a84fa149858bb86c849 +Subproject commit 6c44712baf9e2de2a6171467ad738124f8cea8d4 From b6d3526227b28383868eb197edb6674871922764 Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 17:33:36 +0100 Subject: [PATCH 15/22] cleanup --- packages/pglite/src/pglite.ts | 6 +++--- postgres-pglite | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index cb88992de..bc57543e9 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -340,9 +340,9 @@ export class PGlite mod.ENV.PGUSER = options.username ?? 'postgres' mod.ENV.PGDATABASE = options.database ?? 'template1' mod.REPL = 'N' - // mod.ENV.LC_CTYPE = 'en_US.UTF-8' - // mod.ENV.TZ = 'UTC' - // mod.ENV.PGTZ = 'UTC' + mod.ENV.LC_CTYPE = 'en_US.UTF-8' + mod.ENV.TZ = 'UTC' + mod.ENV.PGTZ = 'UTC' // mod.ENV.PGDATABASE = 'template1' // mod.ENV.PG_COLOR = 'always' }, diff --git a/postgres-pglite b/postgres-pglite index 6c44712ba..7443a2487 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 6c44712baf9e2de2a6171467ad738124f8cea8d4 +Subproject commit 7443a2487f424f3bb1905dd2cf7f9006d26fe49d From abce6441bb05708a9c4630ca570d5b57af49fc27 Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 18:05:06 +0100 Subject: [PATCH 16/22] moved env vars to module in TS --- packages/pglite/src/pglite.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index bc57543e9..51913f60f 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -343,6 +343,7 @@ export class PGlite mod.ENV.LC_CTYPE = 'en_US.UTF-8' mod.ENV.TZ = 'UTC' mod.ENV.PGTZ = 'UTC' + // mod.ENV.PGCLIENTENCODING = 'UTF8' // mod.ENV.PGDATABASE = 'template1' // mod.ENV.PG_COLOR = 'always' }, From cb017cd5832102ccd7e516f248b859ffddf25531 Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 18:25:01 +0100 Subject: [PATCH 17/22] cleanup --- packages/pglite/src/pglite.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index 51913f60f..5043d37ca 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -332,6 +332,7 @@ export class PGlite } mod.FS.registerDevice(devId, devOpt) mod.FS.mkdev('/dev/blob', devId) + // mod.FS.mkdir('/tmp') && mod.FS.chmod('/tmp', 0o700) }, (mod: any) => { mod.ENV.MODE = 'REACT' @@ -345,7 +346,7 @@ export class PGlite mod.ENV.PGTZ = 'UTC' // mod.ENV.PGCLIENTENCODING = 'UTF8' // mod.ENV.PGDATABASE = 'template1' - // mod.ENV.PG_COLOR = 'always' + mod.ENV.PG_COLOR = 'always' }, ], } From 93941a535e6edfac0a1c8c376fd8cf6aff4e34a0 Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 18:45:12 +0100 Subject: [PATCH 18/22] tests fix --- packages/pglite/src/pglite.ts | 2 +- postgres-pglite | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index 5043d37ca..fc56016c2 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -346,7 +346,7 @@ export class PGlite mod.ENV.PGTZ = 'UTC' // mod.ENV.PGCLIENTENCODING = 'UTF8' // mod.ENV.PGDATABASE = 'template1' - mod.ENV.PG_COLOR = 'always' + // mod.ENV.PG_COLOR = 'always' }, ], } diff --git a/postgres-pglite b/postgres-pglite index 7443a2487..774ddc487 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 7443a2487f424f3bb1905dd2cf7f9006d26fe49d +Subproject commit 774ddc487b43d7b3d2c6980f88ef56bb620e68ca From 7270d06d536f7a5907106cd5a1f896e192c29882 Mon Sep 17 00:00:00 2001 From: tudor Date: Mon, 17 Nov 2025 22:43:58 +0100 Subject: [PATCH 19/22] submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index 774ddc487..5c4562fbd 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 774ddc487b43d7b3d2c6980f88ef56bb620e68ca +Subproject commit 5c4562fbd276f955eca59e7fc8f5d9025a11593a From cd53ca264f9e143e9efe32888529ba127fb7f03a Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 18 Nov 2025 10:21:14 +0100 Subject: [PATCH 20/22] more env vars --- packages/pglite/src/pglite.ts | 3 +-- postgres-pglite | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index fc56016c2..54e648c1a 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -340,11 +340,10 @@ export class PGlite mod.ENV.PREFIX = WASM_PREFIX mod.ENV.PGUSER = options.username ?? 'postgres' mod.ENV.PGDATABASE = options.database ?? 'template1' - mod.REPL = 'N' mod.ENV.LC_CTYPE = 'en_US.UTF-8' mod.ENV.TZ = 'UTC' mod.ENV.PGTZ = 'UTC' - // mod.ENV.PGCLIENTENCODING = 'UTF8' + mod.ENV.PGCLIENTENCODING = 'UTF8' // mod.ENV.PGDATABASE = 'template1' // mod.ENV.PG_COLOR = 'always' }, diff --git a/postgres-pglite b/postgres-pglite index 5c4562fbd..a263c5c63 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit 5c4562fbd276f955eca59e7fc8f5d9025a11593a +Subproject commit a263c5c63e9d4e9175709a3db82bc00447e9f789 From e90f3759a528c803ace614e42676cb1c44ac5510 Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 18 Nov 2025 10:37:33 +0100 Subject: [PATCH 21/22] rename pglite main to pgl_startup --- packages/pglite/src/pglite.ts | 2 +- packages/pglite/src/postgresMod.ts | 1 + postgres-pglite | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index 54e648c1a..8c3416fa7 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -467,7 +467,7 @@ export class PGlite this.#pglite_socket_write, ) - this.mod.callMain(args) + this.mod._pgl_startup(args) // Sync the filesystem from any previous store await this.fs!.initialSyncFs() diff --git a/packages/pglite/src/postgresMod.ts b/packages/pglite/src/postgresMod.ts index 9eb410f8e..3acca50da 100644 --- a/packages/pglite/src/postgresMod.ts +++ b/packages/pglite/src/postgresMod.ts @@ -28,6 +28,7 @@ export interface PostgresMod _pgl_shutdown: () => void _pgl_interactive_one: (length: number, peek: number) => void _pgl_set_rw_cbs: (read_cb: number, write_cb: number) => void + _pgl_startup: (args?: string[]) => number addFunction: ( cb: (ptr: any, length: number) => void, signature: string, diff --git a/postgres-pglite b/postgres-pglite index a263c5c63..bdf499f72 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit a263c5c63e9d4e9175709a3db82bc00447e9f789 +Subproject commit bdf499f724b81d3c40f8ed4ba5b3ed8b24189564 From 86cee660aa1166233f51d63e6c86103effbb6541 Mon Sep 17 00:00:00 2001 From: tudor Date: Tue, 18 Nov 2025 13:37:21 +0100 Subject: [PATCH 22/22] submodule --- postgres-pglite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres-pglite b/postgres-pglite index bdf499f72..1d88faa52 160000 --- a/postgres-pglite +++ b/postgres-pglite @@ -1 +1 @@ -Subproject commit bdf499f724b81d3c40f8ed4ba5b3ed8b24189564 +Subproject commit 1d88faa526bd2e3256a397241b0eef9b76651048