@pgxsinkit/pgwasm-c 0.5.2 → 0.6.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +54 -28
- package/README.md +20 -4
- package/artefacts/amcheck.tar.gz +0 -0
- package/artefacts/initdb.js +1 -1
- package/artefacts/initdb.wasm +0 -0
- package/artefacts/postgres.data +7101 -6906
- package/artefacts/postgres.js +1 -1
- package/artefacts/postgres.wasm +0 -0
- package/artefacts/prepopulated.tar.gz +0 -0
- package/dist/artefact-pins.d.ts +22 -22
- package/dist/artefact-pins.d.ts.map +1 -1
- package/dist/artefacts.d.ts.map +1 -1
- package/dist/build.d.ts +5 -4
- package/dist/build.d.ts.map +1 -1
- package/dist/host/emscripten.d.ts +13 -13
- package/dist/host/emscripten.d.ts.map +1 -1
- package/dist/host/initdb.d.ts.map +1 -1
- package/dist/host/postgres-instance.d.ts +1 -1
- package/dist/host/vfs-mount.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +99 -41
- package/dist/index.js.map +10 -9
- package/dist/postgres-version.d.ts +18 -0
- package/dist/postgres-version.d.ts.map +1 -0
- package/dist/prepopulated.js.map +1 -1
- package/package.json +2 -2
- package/src/artefact-pins.ts +14 -14
- package/src/artefacts.ts +23 -4
- package/src/build.ts +20 -6
- package/src/host/emscripten.ts +13 -13
- package/src/host/initdb.ts +7 -5
- package/src/host/postgres-instance.ts +17 -17
- package/src/host/vfs-mount.ts +8 -1
- package/src/index.ts +1 -0
- package/src/postgres-version.ts +34 -0
package/src/artefact-pins.ts
CHANGED
|
@@ -12,25 +12,25 @@
|
|
|
12
12
|
/** The release the files are assets of. */
|
|
13
13
|
export const ARTEFACT_RELEASE = {
|
|
14
14
|
repository: "pgxsinkit/pgwasm-postgres",
|
|
15
|
-
tag: "
|
|
16
|
-
name: "pgwasm-postgres
|
|
17
|
-
commit: "
|
|
18
|
-
upstream: { tag: "
|
|
19
|
-
dataFormat:
|
|
15
|
+
tag: "19.0.0-rc.2",
|
|
16
|
+
name: "pgwasm-postgres 19.0.0-rc.2",
|
|
17
|
+
commit: "528131bf65a410e6a533a3cf67e69abe2a122842",
|
|
18
|
+
upstream: { tag: "REL_19_RC2", commit: "dffcca2e3c81178c64d4c1f44543a3507d0b7508" },
|
|
19
|
+
dataFormat: 2,
|
|
20
20
|
builderImage:
|
|
21
|
-
"ghcr.io/pgxsinkit/pgwasm-builder@sha256:
|
|
22
|
-
manifestSha256: "
|
|
21
|
+
"ghcr.io/pgxsinkit/pgwasm-builder@sha256:cbb378a50de43cd2fbd8718b72ce4c7595ffe421148a3c32435906f096bc5130",
|
|
22
|
+
manifestSha256: "aa13280c998ac707210025f77fdf0c1afd9f7970d6f11b03d70757cbf9006d3b",
|
|
23
23
|
} as const;
|
|
24
24
|
|
|
25
25
|
/** Every pinned file, by its name in `artefacts/` (the release asset of the same name). */
|
|
26
26
|
export const ARTEFACT_FILES = {
|
|
27
|
-
"amcheck.tar.gz": { bytes:
|
|
28
|
-
"initdb.js": { bytes:
|
|
29
|
-
"initdb.wasm": { bytes:
|
|
30
|
-
"postgres.data": { bytes:
|
|
31
|
-
"postgres.js": { bytes:
|
|
32
|
-
"postgres.wasm": { bytes:
|
|
33
|
-
"prepopulated.tar.gz": { bytes:
|
|
27
|
+
"amcheck.tar.gz": { bytes: 21906, sha256: "1af3d942e088a8a8317d46861b56603dd6fe38e5d9d7ae26e809efdf1f784f5d" },
|
|
28
|
+
"initdb.js": { bytes: 105130, sha256: "cf3fff4109879f3be1ba321f19b126c4f8d518d9909846927d3104b1ec0a8129" },
|
|
29
|
+
"initdb.wasm": { bytes: 308968, sha256: "2c9bb1c3ad3267b4ae5d21c1bc9ebfaf608ecfbb2e14e671ad4ed0a24657edb3" },
|
|
30
|
+
"postgres.data": { bytes: 6332734, sha256: "eaa28b912167539490fedb7f37ccecfd4d569919b16da7708b2aad62442bde98" },
|
|
31
|
+
"postgres.js": { bytes: 337357, sha256: "6c6c65b7fcbc3651f8bb38a4536216b186eb8cd57bca5fca1ce712a1fcc83009" },
|
|
32
|
+
"postgres.wasm": { bytes: 9483613, sha256: "9d3b5b288cd1e0dc5d7adcb4b7a17de0d6dd6f5545c1fa350b98f67f3ed15306" },
|
|
33
|
+
"prepopulated.tar.gz": { bytes: 4474382, sha256: "7f0c6af78341f4bbbd80030e2131a86ee19791864f192a82f5d4ad49ee7d969f" },
|
|
34
34
|
} as const;
|
|
35
35
|
|
|
36
36
|
export type ArtefactName = keyof typeof ARTEFACT_FILES;
|
package/src/artefacts.ts
CHANGED
|
@@ -12,14 +12,24 @@ import createInitdbModule from "../artefacts/initdb.js";
|
|
|
12
12
|
import createPostgresModule from "../artefacts/postgres.js";
|
|
13
13
|
import { ARTEFACT_FILES } from "./artefact-pins";
|
|
14
14
|
|
|
15
|
+
let postgresWasm: URL | undefined;
|
|
16
|
+
let fsBundle: URL | undefined;
|
|
17
|
+
let initdbWasm: URL | undefined;
|
|
18
|
+
|
|
15
19
|
/** The URLs of the files the C build loads, for fetching and compiling them ahead of a boot. */
|
|
16
20
|
export const cBuildArtefacts = {
|
|
17
21
|
/** The Postgres WebAssembly module. */
|
|
18
|
-
postgresWasm:
|
|
22
|
+
get postgresWasm(): URL {
|
|
23
|
+
return (postgresWasm ??= new URL("../artefacts/postgres.wasm", import.meta.url));
|
|
24
|
+
},
|
|
19
25
|
/** The filesystem bundle (share/, lib/, the ICU data) the Postgres module mounts. */
|
|
20
|
-
fsBundle:
|
|
26
|
+
get fsBundle(): URL {
|
|
27
|
+
return (fsBundle ??= new URL("../artefacts/postgres.data", import.meta.url));
|
|
28
|
+
},
|
|
21
29
|
/** The initdb WebAssembly module, used when a data directory is created. */
|
|
22
|
-
initdbWasm:
|
|
30
|
+
get initdbWasm(): URL {
|
|
31
|
+
return (initdbWasm ??= new URL("../artefacts/initdb.wasm", import.meta.url));
|
|
32
|
+
},
|
|
23
33
|
} as const;
|
|
24
34
|
|
|
25
35
|
/**
|
|
@@ -41,6 +51,15 @@ export interface CBuildArtefactSet {
|
|
|
41
51
|
export const pinnedCBuildArtefacts: CBuildArtefactSet = {
|
|
42
52
|
createPostgresModule,
|
|
43
53
|
createInitdbModule,
|
|
44
|
-
|
|
54
|
+
// Forward lazily: spreading cBuildArtefacts would invoke its getters during import.
|
|
55
|
+
get postgresWasm() {
|
|
56
|
+
return cBuildArtefacts.postgresWasm;
|
|
57
|
+
},
|
|
58
|
+
get fsBundle() {
|
|
59
|
+
return cBuildArtefacts.fsBundle;
|
|
60
|
+
},
|
|
61
|
+
get initdbWasm() {
|
|
62
|
+
return cBuildArtefacts.initdbWasm;
|
|
63
|
+
},
|
|
45
64
|
fsBundleBytes: ARTEFACT_FILES["postgres.data"].bytes,
|
|
46
65
|
};
|
package/src/build.ts
CHANGED
|
@@ -24,16 +24,18 @@ import { fetchExtensionBundle, installExtensionFiles } from "./host/extensions";
|
|
|
24
24
|
import { runInitdb } from "./host/initdb";
|
|
25
25
|
import { mountFor, type StorageMount } from "./host/mounts";
|
|
26
26
|
import { DEFAULT_START_PARAMS, PostgresInstance } from "./host/postgres-instance";
|
|
27
|
+
import { checkPostgresVersion } from "./postgres-version";
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* The C build's identity, recorded in every data directory it creates (ADR-0063). Its release and data
|
|
30
31
|
* format are the pinned pgwasm-postgres release's (`bun run pgwasm:pin` writes both; ADR-0064): the
|
|
31
|
-
* release is the build's name in version()
|
|
32
|
+
* release is the build's name in version(). Changing formats is an explicit fresh-store adoption;
|
|
33
|
+
* existing directories and backups still have to match this build's format and PostgreSQL major.
|
|
32
34
|
*/
|
|
33
35
|
export const C_BUILD_IDENTITY: BuildIdentity = {
|
|
34
36
|
name: "c",
|
|
35
37
|
dataFormat: ARTEFACT_RELEASE.dataFormat,
|
|
36
|
-
//
|
|
38
|
+
// Unmarked directories are the C build's only when their PG_VERSION also matches (checked below).
|
|
37
39
|
claimsUnmarkedDirectories: true,
|
|
38
40
|
release: ARTEFACT_RELEASE.name,
|
|
39
41
|
};
|
|
@@ -158,7 +160,13 @@ class CMountedDataDirectory implements MountedDataDirectory {
|
|
|
158
160
|
}
|
|
159
161
|
|
|
160
162
|
async readFile(path: string): Promise<Uint8Array | undefined> {
|
|
161
|
-
|
|
163
|
+
const bytes = readDataDirFile(this.#FS, path);
|
|
164
|
+
// Shared boot checks a marked directory's build/format itself. Old unmarked directories have no
|
|
165
|
+
// such guard, so refuse another PostgreSQL major before they can reach startup or any write.
|
|
166
|
+
if (path === "/PG_VERSION" && bytes !== undefined && readDataDirFile(this.#FS, "/PGWASM_BUILD") === undefined) {
|
|
167
|
+
checkPostgresVersion(bytes);
|
|
168
|
+
}
|
|
169
|
+
return bytes;
|
|
162
170
|
}
|
|
163
171
|
|
|
164
172
|
async createCluster(): Promise<void> {
|
|
@@ -191,6 +199,11 @@ class CMountedDataDirectory implements MountedDataDirectory {
|
|
|
191
199
|
}
|
|
192
200
|
|
|
193
201
|
async writeEntries(entries: readonly DataDirEntry[]): Promise<void> {
|
|
202
|
+
// A legacy backup can have no build marker. Check its PostgreSQL version before writing even its
|
|
203
|
+
// first member: NODEFS writes immediately, and a later startup refusal cannot undo a partial restore.
|
|
204
|
+
const versions = entries.filter((entry) => entry.path === "/PG_VERSION" && entry.type === "file");
|
|
205
|
+
if (versions.length === 0) checkPostgresVersion(undefined);
|
|
206
|
+
for (const version of versions) checkPostgresVersion(version.data);
|
|
194
207
|
writeDataDirEntries(this.#FS, entries);
|
|
195
208
|
}
|
|
196
209
|
|
|
@@ -203,6 +216,7 @@ class CMountedDataDirectory implements MountedDataDirectory {
|
|
|
203
216
|
}
|
|
204
217
|
|
|
205
218
|
async start(options: StartOptions): Promise<RunningPostgres> {
|
|
219
|
+
checkPostgresVersion(readDataDirFile(this.#FS, "/PG_VERSION"));
|
|
206
220
|
const log = (...args: unknown[]) => {
|
|
207
221
|
if (this.#request.debug > 0) console.log(...args);
|
|
208
222
|
};
|
|
@@ -235,9 +249,9 @@ export function createCBuild(options: CBuildOptions = {}): PostgresBuild {
|
|
|
235
249
|
/**
|
|
236
250
|
* @internal A C build on the given artefacts, recording the given identity: what {@link createCBuild}
|
|
237
251
|
* builds on the pinned release's. Not exported from the package; the IndexedDB browser lane reaches it
|
|
238
|
-
* from source to boot
|
|
239
|
-
*
|
|
240
|
-
*
|
|
252
|
+
* from source to boot earlier compatible releases' glue and files, then checks that the current build
|
|
253
|
+
* can reopen their stores (tests/e2e/pgwasm-idb/continuity-builds.ts). PG19's format-2 list starts empty:
|
|
254
|
+
* format-1 PG18 stores are explicitly refused, and future entries must preserve the PG19 host ABI too.
|
|
241
255
|
*/
|
|
242
256
|
export function createCBuildFrom(
|
|
243
257
|
identity: BuildIdentity,
|
package/src/host/emscripten.ts
CHANGED
|
@@ -124,27 +124,27 @@ export interface EmscriptenModuleBase {
|
|
|
124
124
|
stringToUTF8OnStack(value: string): number;
|
|
125
125
|
_fopen(path: number, mode: number): number;
|
|
126
126
|
_fclose(stream: number): number;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
_pgwasm_set_system_fn(fn: number): void;
|
|
128
|
+
_pgwasm_set_popen_fn(fn: number): void;
|
|
129
|
+
_pgwasm_set_pclose_fn(fn: number): void;
|
|
130
|
+
_pgwasm_freopen(path: number, mode: number, stream: number): number;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
/** The Postgres module (`postgres.js`). */
|
|
134
134
|
export interface PostgresModule extends EmscriptenModuleBase {
|
|
135
135
|
readonly PROXYFS: EmscriptenFilesystemType;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
136
|
+
_pgwasm_set_rw_cbs(readCallback: number, writeCallback: number): void;
|
|
137
|
+
_pgwasm_pq_flush(): void;
|
|
138
|
+
_pgwasm_setPostgresActive(value: number): number;
|
|
139
|
+
_pgwasm_startPostgres(): void;
|
|
140
|
+
_pgwasm_getMyProcPort(): number;
|
|
141
|
+
_pgwasm_sendConnData(): void;
|
|
142
|
+
_pgwasm_run_atexit_funcs(): void;
|
|
143
|
+
_pgwasm_setPostgresExitStatus(status: number): number;
|
|
144
144
|
_PostgresMainLoopOnce(): void;
|
|
145
145
|
_PostgresMainLongJmp(): void;
|
|
146
146
|
_PostgresSendReadyForQueryIfNecessary(): void;
|
|
147
|
-
_ProcessStartupPacket(port: number
|
|
147
|
+
_ProcessStartupPacket(port: number): number;
|
|
148
148
|
_pq_buffer_remaining_data(): number;
|
|
149
149
|
_emscripten_force_exit(status: number): void;
|
|
150
150
|
}
|
package/src/host/initdb.ts
CHANGED
|
@@ -120,17 +120,19 @@ export async function runInitdb({
|
|
|
120
120
|
env["HOME"] = "/home/postgres";
|
|
121
121
|
env["USER"] = "postgres";
|
|
122
122
|
env["LOGNAME"] = "postgres";
|
|
123
|
+
// initdb writes the cluster's timezone defaults; match the backend even on a non-UTC host.
|
|
124
|
+
env["TZ"] = "UTC";
|
|
123
125
|
env["ICU_DATA"] = ICU_DATA_PATH;
|
|
124
126
|
},
|
|
125
127
|
(module) => {
|
|
126
128
|
module.onRuntimeInitialized = () => {
|
|
127
|
-
module.
|
|
129
|
+
module._pgwasm_set_system_fn(
|
|
128
130
|
module.addFunction((command: number) => {
|
|
129
131
|
postgresArgs = commandArgs(module, command);
|
|
130
132
|
return callPgMain(postgresArgs);
|
|
131
133
|
}, "pi"),
|
|
132
134
|
);
|
|
133
|
-
module.
|
|
135
|
+
module._pgwasm_set_popen_fn(
|
|
134
136
|
module.addFunction((command: number, mode: number) => {
|
|
135
137
|
const openMode = module.UTF8ToString(mode);
|
|
136
138
|
postgresArgs = commandArgs(module, command);
|
|
@@ -145,7 +147,7 @@ export async function runInitdb({
|
|
|
145
147
|
throw new Error(`Unexpected popen mode ${openMode}`);
|
|
146
148
|
}, "ppi"),
|
|
147
149
|
);
|
|
148
|
-
module.
|
|
150
|
+
module._pgwasm_set_pclose_fn(
|
|
149
151
|
module.addFunction((stream: number) => {
|
|
150
152
|
if (stream === initdbStdinFd || stream === initdbStdoutFd) {
|
|
151
153
|
// A popen in "w" mode runs postgres' main once initdb has written its input.
|
|
@@ -159,8 +161,8 @@ export async function runInitdb({
|
|
|
159
161
|
}, "pi"),
|
|
160
162
|
);
|
|
161
163
|
// The backend reads initdb's output and writes initdb's input through two files.
|
|
162
|
-
pg.
|
|
163
|
-
pg.
|
|
164
|
+
pg._pgwasm_freopen(pg.stringToUTF8OnStack(PG_STDIN_PATH), pg.stringToUTF8OnStack("r"), 0);
|
|
165
|
+
pg._pgwasm_freopen(pg.stringToUTF8OnStack(PG_STDOUT_PATH), pg.stringToUTF8OnStack("w"), 1);
|
|
164
166
|
initdbStdinFd = module._fopen(module.stringToUTF8OnStack(PG_STDOUT_PATH), module.stringToUTF8OnStack("r"));
|
|
165
167
|
initdbStdoutFd = module._fopen(module.stringToUTF8OnStack(PG_STDIN_PATH), module.stringToUTF8OnStack("w"));
|
|
166
168
|
};
|
|
@@ -8,9 +8,9 @@ import type { EmscriptenStream, ModuleOverrides, PostgresModule } from "./emscri
|
|
|
8
8
|
import { preservingExitCode } from "./exit-code";
|
|
9
9
|
import { ICU_DATA_PATH, INITDB_EXE_PATH, LOCALE_LIST_PATH, PGDATA, POSTGRES_EXE_PATH } from "./paths";
|
|
10
10
|
|
|
11
|
-
/** Postgres' error longjmp, intercepted by the host: keep in sync with
|
|
11
|
+
/** Postgres' error longjmp, intercepted by the host: keep in sync with pgwasmc.c's POSTGRES_MAIN_LONGJMP. */
|
|
12
12
|
const POSTGRES_MAIN_LONGJMP = 100;
|
|
13
|
-
/** The exit status single-user mode reports when it started and stays alive: the C build's `
|
|
13
|
+
/** The exit status single-user mode reports when it started and stays alive: the C build's `PGWASM_EXIT_ALIVE`. */
|
|
14
14
|
const PGWASM_EXIT_ALIVE = 99;
|
|
15
15
|
const INITIAL_MEMORY_PAGES = 2048;
|
|
16
16
|
const MAXIMUM_MEMORY_PAGES = 32768;
|
|
@@ -42,7 +42,7 @@ export const DEFAULT_START_PARAMS: readonly string[] = [
|
|
|
42
42
|
/**
|
|
43
43
|
* Whether `error` is one of the exceptions the Emscripten runtime throws on purpose to unwind the wasm
|
|
44
44
|
* stack back to the main loop, which handles them:
|
|
45
|
-
* - `'unwind'` from emscripten_exit_with_live_runtime(): how
|
|
45
|
+
* - `'unwind'` from emscripten_exit_with_live_runtime(): how pgwasm_longjmp (pgwasmc.c) delivers Postgres'
|
|
46
46
|
* siglongjmp to its main-loop error handler, and how a Terminate message ends the loop;
|
|
47
47
|
* - what an emscripten-mode longjmp throws, should one pass every invoke_* frame: an instance of the
|
|
48
48
|
* glue's (unexported) `EmscriptenSjLj` class on Emscripten 6.0.10 (pgwasm-postgres 18.6.1 onwards), a
|
|
@@ -264,19 +264,19 @@ export class PostgresInstance {
|
|
|
264
264
|
#onRuntimeInitialized(): void {
|
|
265
265
|
const mod = this.module;
|
|
266
266
|
// system() never runs a command: it is logged and fails.
|
|
267
|
-
mod.
|
|
267
|
+
mod._pgwasm_set_system_fn(
|
|
268
268
|
this.#addFunction((command: number) => {
|
|
269
269
|
this.#log(`Postgres tried to execute ${mod.UTF8ToString(command)}, returning 1.`);
|
|
270
270
|
return 1;
|
|
271
271
|
}, "pi"),
|
|
272
272
|
);
|
|
273
|
-
mod.
|
|
273
|
+
mod._pgwasm_set_popen_fn(
|
|
274
274
|
this.#addFunction((command: number, mode: number) => {
|
|
275
275
|
this.#externalCommandStream = this.#handleExternalCommand(mod.UTF8ToString(command), mod.UTF8ToString(mode));
|
|
276
276
|
return this.#externalCommandStream;
|
|
277
277
|
}, "ppp"),
|
|
278
278
|
);
|
|
279
|
-
mod.
|
|
279
|
+
mod._pgwasm_set_pclose_fn(
|
|
280
280
|
this.#addFunction((stream: number) => {
|
|
281
281
|
if (stream !== this.#externalCommandStream) throw new Error(`Unhandled pclose ${stream}`);
|
|
282
282
|
mod._fclose(stream);
|
|
@@ -295,7 +295,7 @@ export class PostgresInstance {
|
|
|
295
295
|
this.#readOffset += length;
|
|
296
296
|
return length;
|
|
297
297
|
}, "iii");
|
|
298
|
-
mod.
|
|
298
|
+
mod._pgwasm_set_rw_cbs(read, write);
|
|
299
299
|
}
|
|
300
300
|
|
|
301
301
|
#handleExternalCommand(command: string, mode: string): number {
|
|
@@ -359,16 +359,16 @@ export class PostgresInstance {
|
|
|
359
359
|
/** Start Postgres in single-user mode on the data directory. */
|
|
360
360
|
startSingleMode(startParams: readonly string[]): void {
|
|
361
361
|
const mod = this.module;
|
|
362
|
-
mod.
|
|
362
|
+
mod._pgwasm_setPostgresActive(1);
|
|
363
363
|
this.callMain([...startParams, "-D", PGDATA, mod.ENV["PGDATABASE"] ?? "postgres"]);
|
|
364
|
-
const exitStatus = mod.
|
|
364
|
+
const exitStatus = mod._pgwasm_setPostgresExitStatus(-3);
|
|
365
365
|
if (exitStatus !== PGWASM_EXIT_ALIVE) {
|
|
366
366
|
const output = this.#stderr.join("\n").trim();
|
|
367
367
|
throw new Error(
|
|
368
368
|
`Postgres failed to start (single-user mode exit status ${exitStatus})${output ? `:\n${output}` : ""}`,
|
|
369
369
|
);
|
|
370
370
|
}
|
|
371
|
-
mod.
|
|
371
|
+
mod._pgwasm_startPostgres();
|
|
372
372
|
}
|
|
373
373
|
|
|
374
374
|
/**
|
|
@@ -422,11 +422,11 @@ export class PostgresInstance {
|
|
|
422
422
|
|
|
423
423
|
#processStartupPacket(): void {
|
|
424
424
|
const mod = this.module;
|
|
425
|
-
const result = this.#call(() => mod._ProcessStartupPacket(mod.
|
|
425
|
+
const result = this.#call(() => mod._ProcessStartupPacket(mod._pgwasm_getMyProcPort()));
|
|
426
426
|
if (result !== 0) throw new Error("Cannot process the startup packet");
|
|
427
427
|
this.#call(() => {
|
|
428
|
-
mod.
|
|
429
|
-
mod.
|
|
428
|
+
mod._pgwasm_sendConnData();
|
|
429
|
+
mod._pgwasm_pq_flush();
|
|
430
430
|
});
|
|
431
431
|
}
|
|
432
432
|
|
|
@@ -443,7 +443,7 @@ export class PostgresInstance {
|
|
|
443
443
|
// every frame it abandoned (see ShadowStack).
|
|
444
444
|
this.#stack.restore(stack);
|
|
445
445
|
if (!isEmscriptenUnwind(error)) throw error;
|
|
446
|
-
if (mod.
|
|
446
|
+
if (mod._pgwasm_setPostgresExitStatus(-2) === POSTGRES_MAIN_LONGJMP) {
|
|
447
447
|
// Postgres raised an error: its siglongjmp into main was intercepted, and its error handling
|
|
448
448
|
// runs here before the loop resumes with the rest of the batch.
|
|
449
449
|
this.#call(() => mod._PostgresMainLongJmp());
|
|
@@ -452,7 +452,7 @@ export class PostgresInstance {
|
|
|
452
452
|
}
|
|
453
453
|
this.#call(() => {
|
|
454
454
|
mod._PostgresSendReadyForQueryIfNecessary();
|
|
455
|
-
mod.
|
|
455
|
+
mod._pgwasm_pq_flush();
|
|
456
456
|
});
|
|
457
457
|
} catch (error) {
|
|
458
458
|
this.#failed = true;
|
|
@@ -465,8 +465,8 @@ export class PostgresInstance {
|
|
|
465
465
|
const mod = this.module;
|
|
466
466
|
try {
|
|
467
467
|
preservingExitCode(() => {
|
|
468
|
-
mod.
|
|
469
|
-
this.#call(() => mod.
|
|
468
|
+
mod._pgwasm_setPostgresActive(0);
|
|
469
|
+
this.#call(() => mod._pgwasm_run_atexit_funcs());
|
|
470
470
|
});
|
|
471
471
|
} catch (error) {
|
|
472
472
|
const exit = error as { name?: unknown; status?: unknown };
|
package/src/host/vfs-mount.ts
CHANGED
|
@@ -10,6 +10,8 @@ interface NodeAttributes {
|
|
|
10
10
|
mode?: number;
|
|
11
11
|
size?: number;
|
|
12
12
|
timestamp?: number;
|
|
13
|
+
atime?: number;
|
|
14
|
+
mtime?: number;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
/** A Uint8Array over exactly the bytes of `buffer` (Emscripten hands the heap as an Int8Array). */
|
|
@@ -99,7 +101,12 @@ export function createVfsFilesystem(module: PostgresModule, vfs: BaseFilesystem)
|
|
|
99
101
|
() => {
|
|
100
102
|
if (attr.mode !== undefined) vfs.chmod(path, attr.mode);
|
|
101
103
|
if (attr.size !== undefined) vfs.truncate(path, attr.size);
|
|
102
|
-
|
|
104
|
+
const atime = attr.atime ?? attr.timestamp;
|
|
105
|
+
const mtime = attr.mtime ?? attr.timestamp;
|
|
106
|
+
if (atime !== undefined || mtime !== undefined) {
|
|
107
|
+
const previous = atime === undefined || mtime === undefined ? vfs.lstat(path) : undefined;
|
|
108
|
+
vfs.utimes(path, atime ?? previous!.atime, mtime ?? previous!.mtime);
|
|
109
|
+
}
|
|
103
110
|
},
|
|
104
111
|
"setattr",
|
|
105
112
|
path,
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { cBuildArtefacts } from "./artefacts";
|
|
2
2
|
export { C_BUILD_IDENTITY, cBuild, createCBuild, type CBuildAssets, type CBuildOptions } from "./build";
|
|
3
3
|
export { idbDatabaseName } from "./host/mounts/idb";
|
|
4
|
+
export { PostgresVersionMismatchError } from "./postgres-version";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { PgwasmError } from "@pgxsinkit/pgwasm";
|
|
2
|
+
|
|
3
|
+
import { ARTEFACT_RELEASE } from "./artefact-pins";
|
|
4
|
+
|
|
5
|
+
/** PostgreSQL's major data-directory version, independent of pgwasm's build-marker format. */
|
|
6
|
+
export const C_POSTGRES_MAJOR = /^REL_(\d+)_/.exec(ARTEFACT_RELEASE.upstream.tag)?.[1];
|
|
7
|
+
|
|
8
|
+
/** The C host cannot open this PostgreSQL major; retrying cannot make the directory compatible. */
|
|
9
|
+
export class PostgresVersionMismatchError extends PgwasmError {
|
|
10
|
+
override name = "PostgresVersionMismatchError";
|
|
11
|
+
readonly expected: string;
|
|
12
|
+
readonly found: string | undefined;
|
|
13
|
+
/** Structured fields retained alongside the stable error name by the worker bridge. */
|
|
14
|
+
readonly detail: { readonly expected: string; readonly found: string | undefined };
|
|
15
|
+
|
|
16
|
+
constructor(expected: string, found: string | undefined) {
|
|
17
|
+
super(
|
|
18
|
+
`This C build requires PostgreSQL ${expected}, but the data directory's PG_VERSION is ` +
|
|
19
|
+
`${found === undefined ? "missing" : JSON.stringify(found)}. Use a separately named fresh PostgreSQL ${expected} store; ` +
|
|
20
|
+
"the existing directory is not migrated or replaced.",
|
|
21
|
+
);
|
|
22
|
+
this.expected = expected;
|
|
23
|
+
this.found = found;
|
|
24
|
+
this.detail = { expected, found };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Refuse another major before the C host writes a restored image or starts a mounted cluster. */
|
|
29
|
+
export function checkPostgresVersion(bytes: Uint8Array | undefined): void {
|
|
30
|
+
if (C_POSTGRES_MAJOR === undefined) throw new Error("The pinned C build has no PostgreSQL major version.");
|
|
31
|
+
const found = bytes === undefined ? undefined : new TextDecoder().decode(bytes).trim();
|
|
32
|
+
if (found === C_POSTGRES_MAJOR) return;
|
|
33
|
+
throw new PostgresVersionMismatchError(C_POSTGRES_MAJOR, found);
|
|
34
|
+
}
|