Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions packages/pglite-tools/src/pgDumpModFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ 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,
) => 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<T extends PgDumpMod = PgDumpMod> = (
Expand Down
12 changes: 6 additions & 6 deletions packages/pglite-tools/src/pg_dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ async function execPgDump({
args: string[]
}): Promise<ExecResult> {
let pgdump_write, pgdump_read
let exitStatus = 0
let exitCode = 0
let stderrOutput: string = ''
let stdoutOutput: string = ''
const emscriptenOpts: Partial<PgDumpMod> = {
arguments: args,
noExitRuntime: false,
print: (text) => {
stdoutOutput += text
Expand All @@ -49,7 +48,7 @@ async function execPgDump({
stderrOutput += text
},
onExit: (status: number) => {
exitStatus = status
exitCode = status
},
preRun: [
(mod: PgDumpMod) => {
Expand Down Expand Up @@ -83,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
}
Expand All @@ -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,
Expand Down
153 changes: 87 additions & 66 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -214,12 +214,12 @@ export class PGlite
const extensionInitFns: Array<() => Promise<void>> = []

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()] : []),
]
Expand Down Expand Up @@ -332,6 +332,20 @@ 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'
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.PGCLIENTENCODING = 'UTF8'
// mod.ENV.PGDATABASE = 'template1'
// mod.ENV.PG_COLOR = 'always'
},
],
}
Expand Down Expand Up @@ -387,66 +401,73 @@ export class PGlite
this.mod = await PostgresModFactory(emscriptenOpts)

// set the write callback
this.#pglite_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
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_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.log(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._set_read_write_cbs(this.#pglite_read, this.#pglite_write)
this.mod._pgl_startup(args)

// Sync the filesystem from any previous store
await this.fs!.initialSyncFs()
Expand Down Expand Up @@ -576,8 +597,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) {
Expand Down Expand Up @@ -671,7 +692,7 @@ export class PGlite
}

// execute the message
mod._interactive_one(message.length, message[0])
mod._pgl_interactive_one(message.length, message[0])

this.#outputData = []

Expand Down
8 changes: 4 additions & 4 deletions packages/pglite/src/postgresMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ 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<string, Promise<Blob | null>>
_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
_pgl_startup: (args?: string[]) => number
addFunction: (
cb: (ptr: any, length: number) => void,
signature: string,
) => number
removeFunction: (f: number) => void
callMain: (args?: string[]) => number
}

type PostgresFactory<T extends PostgresMod = PostgresMod> = (
Expand Down
2 changes: 1 addition & 1 deletion postgres-pglite
Submodule postgres-pglite updated 82 files
+67 −0 build-libpglite.sh
+27 −11 build-pglite.sh
+0 −62 docker_rc.sh
+0 −43 extra/pg_ivm.sh
+0 −25 extra/pg_uuidv7.sh
+0 −27 extra/vector.sh
+0 −338 pglite-wasm/build.sh
+63 −0 pglite-wasm/builder/libpglite.dockerfile
+2 −3 pglite-wasm/included.pglite.imports
+0 −189 pglite-wasm/native.sh
+44 −209 pglite-wasm/pg_main.c
+1 −1 pglite-wasm/pg_proto.c
+6 −8 pglite-wasm/pgl_interactive_one.c
+3 −35 pglite-wasm/pgl_mains.c
+16 −16 pglite/includes/pglite-comm.h
+0 −0 postgresql-REL_17_5_WASM.patched
+2 −2 src/Makefile.shlib
+19 −47 src/backend/Makefile
+1 −1 src/backend/access/nbtree/nbtutils.c
+2 −2 src/backend/access/transam/xact.c
+0 −4 src/backend/access/transam/xlogarchive.c
+2 −2 src/backend/bootstrap/bootstrap.c
+1 −1 src/backend/catalog/index.c
+1 −1 src/backend/commands/async.c
+1 −1 src/backend/commands/dbcommands.c
+5 −5 src/backend/commands/event_trigger.c
+1 −1 src/backend/libpq/be-secure.c
+1 −1 src/backend/port/posix_sema.c
+1 −1 src/backend/postmaster/checkpointer.c
+2 −2 src/backend/storage/file/fd.c
+2 −2 src/backend/storage/ipc/ipc.c
+1 −1 src/backend/storage/ipc/procsignal.c
+1 −1 src/backend/storage/ipc/signalfuncs.c
+1 −1 src/backend/storage/ipc/sinvaladt.c
+2 −6 src/backend/storage/lmgr/proc.c
+1 −1 src/backend/tcop/postgres.c
+1 −1 src/backend/tcop/utility.c
+1 −1 src/backend/utils/adt/ruleutils.c
+2 −2 src/backend/utils/error/elog.c
+5 −9 src/backend/utils/init/miscinit.c
+2 −2 src/backend/utils/init/postinit.c
+1 −1 src/backend/utils/misc/guc.c
+1 −6 src/backend/utils/misc/timeout.c
+5 −9 src/bin/initdb/initdb.c
+0 −2 src/bin/pg_config/pg_config.c
+24 −2 src/bin/pg_config/run_pg_config.mjs
+2 −12 src/bin/pg_ctl/pg_ctl.c
+2 −8 src/bin/pg_dump/pg_dump.c
+1 −1 src/bin/pg_dump/pg_dumpall.c
+1 −1 src/bin/pg_resetwal/pg_resetwal.c
+1 −2 src/bin/pg_upgrade/parallel.c
+0 −5 src/bin/psql/command.c
+2 −2 src/common/pg_get_line.c
+1 −1 src/include/bootstrap/bootstrap.h
+0 −5 src/include/fe_utils/string_utils.h
+0 −4 src/include/fmgr.h
+0 −14 src/include/port/emscripten.h
+3 −4 src/include/port/pg_pthread.h
+0 −262 src/include/port/sdk_port.h
+0 −229 src/include/port/wasi.h
+0 −12 src/include/port/wasm_common.h
+4 −4 src/include/storage/dsm_impl.h
+1 −1 src/include/storage/ipc.h
+1 −1 src/include/utils/elog.h
+1 −1 src/interfaces/libpq/fe-auth.c
+1 −11 src/interfaces/libpq/fe-misc.c
+1 −1 src/interfaces/libpq/fe-secure.c
+2 −2 src/interfaces/libpq/legacy-pqsignal.c
+1 −1 src/makefiles/Makefile.emscripten
+0 −5 src/port/getopt.c
+1 −1 src/port/getpeereid.c
+1 −18 src/port/pthread_barrier_wait.c
+0 −9 src/test/regress/pg_regress.c
+0 −663 wasm-build.sh
+0 −137 wasm-build/build-ext.sh
+0 −437 wasm-build/build-pgcore.sh
+0 −28 wasm-build/extension.sh
+0 −262 wasm-build/include/sdk_port.h
+0 −152 wasm-build/linkimports.sh
+0 −154 wasm-build/pack_extension.py
+0 −152 wasm-build/sdk.sh
+0 −404 wasm-build/sdk_port-wasi/sdk_port-wasi.c