From 94b974617bc7fb451271750d8d80767bfc92d30d Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:43:51 +0000 Subject: [PATCH 01/13] test(wasm): parse 18.1.5's depths and report V8 stack overflows as stack depth errors --- versions/18/test/wasm-recovery.test.js | 71 +++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index 5f50a19..e51f2fd 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -45,6 +45,53 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { } }); + // 18.1.5 parsed these; 18.2.7's 512kB WASM limit rejected them. + it('parses nesting that 18.1.5 parsed', async () => { + await loadModule(); + const nested = { + 'chained operators': 'SELECT 1' + '+1'.repeat(4000), + 'nested subqueries': 'SELECT ' + '(SELECT '.repeat(1500) + '1' + ')'.repeat(1500), + 'nested function calls': 'SELECT ' + 'f('.repeat(3500) + '1' + ')'.repeat(3500), + }; + for (const [name, sql] of Object.entries(nested)) { + assert.equal(parseSync(sql).stmts.length, 1, name); + } + const blocks = { + 'nested IF blocks': ['IF true THEN ', 'NULL;', ' END IF;'], + 'nested BEGIN blocks': ['BEGIN ', 'NULL;', ' END;'], + 'nested loops': ['LOOP ', 'EXIT;', ' END LOOP;'], + }; + for (const [name, [open, body, close]] of Object.entries(blocks)) { + const sql = 'CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN ' + + open.repeat(1000) + body + close.repeat(1000) + ' END $$'; + assert.equal(lib.parsePlPgSQLSync(sql).length, 1, name); + } + }); + + // V8's native stack can run out inside a WASM call before libpg_query's own + // check, e.g. in normalize, whose frames are larger there. + it('reports a stack depth error, not a RangeError, for nesting too deep to parse', async () => { + await loadModule(); + const subqueries = (n) => 'SELECT ' + '(SELECT '.repeat(n) + '1' + ')'.repeat(n); + const tooDeep = [ + ['parseSync', DEEP], + ['parseSync', subqueries(3000)], + ['normalizeSync', DEEP], + ['normalizeSync', subqueries(1000)], + ['fingerprintSync', subqueries(3000)], + ]; + for (const [name, sql] of tooDeep) { + assert.throws(() => lib[name](sql), (error) => { + assert.ok(error instanceof lib.SqlError, `${name}: ${error}`); + assert.equal(error.message, 'stack depth limit exceeded'); + return true; + }); + assert.equal(parseSync('SELECT 1').stmts.length, 1); + } + await assert.rejects(lib.normalize(DEEP), lib.SqlError); + assert.equal((await parse('SELECT 1')).stmts.length, 1); + }); + it('frees replaced WASM heaps even when callers keep the errors', () => { const script = `const lib = require(${JSON.stringify(require.resolve('../wasm/index.cjs'))}); (async () => { @@ -206,7 +253,7 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { }); // Every entry point, with the next WASM call made to throw: a RangeError -// stands for a stack overflow, a non-Error object for Emscripten's ExitStatus. +// stands for a trap or a stack overflow, a non-Error object for Emscripten's ExitStatus. describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => { const PLPGSQL = 'CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END $$ LANGUAGE plpgsql'; const entryPoints = [ @@ -281,7 +328,7 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => await isolated.loadModule(); const loadsBefore = loads; for (let i = 0; i < 60; i++) { - throwNext = new RangeError('Maximum call stack size exceeded'); + throwNext = new RangeError('WASM trap'); assert.throws(() => isolated.parseSync('SELECT 1'), RangeError); const failed = used; assert.equal(isolated.parseSync('SELECT 1').stmts.length, 1); @@ -292,10 +339,10 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => it('makes loadModule() and queued calls wait again when a queued call breaks the fresh instance', async () => { await isolated.loadModule(); - throwNext = new RangeError('Maximum call stack size exceeded'); + throwNext = new RangeError('WASM trap'); assert.throws(() => isolated.parseSync('SELECT 1'), RangeError); const failing = isolated.parse('SELECT 1'); - throwNext = new RangeError('Maximum call stack size exceeded'); + throwNext = new RangeError('WASM trap'); const ready = isolated.loadModule(); const after = isolated.parse('SELECT 1'); await assert.rejects(failing, RangeError); @@ -308,10 +355,10 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => it('makes loadModule() wait again when a call queued ahead of it breaks the fresh instance', async () => { await isolated.loadModule(); - throwNext = new RangeError('Maximum call stack size exceeded'); + throwNext = new RangeError('WASM trap'); assert.throws(() => isolated.parseSync('SELECT 1'), RangeError); const failing = isolated.parse('SELECT 1'); - throwNext = new RangeError('Maximum call stack size exceeded'); + throwNext = new RangeError('WASM trap'); const ready = isolated.loadModule(); await assert.rejects(failing, RangeError); await ready; @@ -342,10 +389,18 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => const skip = typeof isolated[name] !== 'function' && 'not in this build'; it(`${name}: rethrows the original Error and recovers`, { skip }, async () => { - const thrown = new RangeError('Maximum call stack size exceeded'); + const thrown = new RangeError('WASM trap'); assert.equal(await failOnce(name, query, thrown), thrown); }); + it(`${name}: reports a V8 stack overflow as a stack depth error and recovers`, { skip }, async () => { + const thrown = new RangeError('Maximum call stack size exceeded'); + const caught = await failOnce(name, query, thrown); + assert.ok(caught instanceof isolated.SqlError); + assert.equal(caught.message, 'stack depth limit exceeded'); + assert.equal(caught.cause, thrown); + }); + it(`${name}: wraps a non-Error throw in an Error and recovers`, { skip }, async () => { const thrown = { name: 'ExitStatus', message: 'Program terminated with exit(1)', status: 1 }; const caught = await failOnce(name, query, thrown); @@ -368,7 +423,7 @@ function loadTrapping(failLoad) { if (trapHeapSize === undefined) return parseRaw(...callArgs); if (trapHeapSize) module.HEAPU8 = { length: trapHeapSize }; trapHeapSize = undefined; - throw new RangeError('Maximum call stack size exceeded'); + throw new RangeError('WASM trap'); }; }, failLoad); isolated.trapNext = (heapSize = 0) => { trapHeapSize = heapSize; }; From 0699b7b3bffec1dec002e6a1c0ad57d86c9b5915 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:49:08 +0000 Subject: [PATCH 02/13] test(wasm): check parsePlPgSQL's function list and drop a fingerprint case that now parses --- versions/18/test/wasm-recovery.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index e51f2fd..df94486 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -64,7 +64,7 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { for (const [name, [open, body, close]] of Object.entries(blocks)) { const sql = 'CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN ' + open.repeat(1000) + body + close.repeat(1000) + ' END $$'; - assert.equal(lib.parsePlPgSQLSync(sql).length, 1, name); + assert.equal(lib.parsePlPgSQLSync(sql).plpgsql_funcs.length, 1, name); } }); @@ -78,7 +78,6 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { ['parseSync', subqueries(3000)], ['normalizeSync', DEEP], ['normalizeSync', subqueries(1000)], - ['fingerprintSync', subqueries(3000)], ]; for (const [name, sql] of tooDeep) { assert.throws(() => lib[name](sql), (error) => { From 22fd7607efb8f8a1f159a8ba9771fda37d841f15 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:49:08 +0000 Subject: [PATCH 03/13] fix(wasm): report host stack overflows as stack depth errors and use the full WASM stack --- templates/full/index.ts | 14 +++++++++++++- versions/18/Makefile | 4 ++-- versions/18/package.json | 4 ++-- versions/18/src/index.ts | 14 +++++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/templates/full/index.ts b/templates/full/index.ts index 9e254ce..d40bb06 100644 --- a/templates/full/index.ts +++ b/templates/full/index.ts @@ -198,7 +198,15 @@ function invalidateModule(error: unknown): void { wasmModule = undefined; } -// An exception out of a WASM call (V8 stack overflow on deeply nested SQL, a +// The host engine's native stack (V8 and JSC: RangeError, Firefox: +// InternalError) can run out inside a WASM call before libpg_query's own +// stack depth check, which only measures the WASM stack. +function isStackOverflow(error: Error): boolean { + return (error instanceof RangeError && /call stack/i.test(error.message)) + || (error.name === 'InternalError' && /too much recursion/i.test(error.message)); +} + +// An exception out of a WASM call (host stack overflow on deeply nested SQL, a // trap, or exit() once the heap limit is hit) skips libpg_query's cleanup and // leaves the instance corrupt, so replace it with a fresh one. function callWasm(fn: () => number): number { @@ -210,6 +218,10 @@ function callWasm(fn: () => number): number { if (proc) proc.exitCode = exitCode; if (error instanceof Error) { invalidateModule(error); + if (isStackOverflow(error)) { + const message = 'stack depth limit exceeded'; + throw Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause: error }); + } throw error; } const reason = typeof error === 'object' && error !== null diff --git a/versions/18/Makefile b/versions/18/Makefile index 4197615..d1b33ab 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -18,8 +18,8 @@ WASM_STACK_SIZE ?= 33554432 # libpg_query source, pinned to an exact commit so a release can be rebuilt # byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git -LIBPG_QUERY_TAG := 18-constructive -LIBPG_QUERY_COMMIT := 7791971be1fc169d3f1b0d1ec887dc3f134ad134 +LIBPG_QUERY_TAG := devin/1795668138-wasm-full-stack +LIBPG_QUERY_COMMIT := f975bb56d6b93b9ce23135da66cee81308ec3a98 CACHE_DIR := .cache diff --git a/versions/18/package.json b/versions/18/package.json index c10b4bf..7eb2be3 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -14,8 +14,8 @@ "pgVersion": "18", "distTag": "pg18", "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", - "libpgQueryTag": "18-constructive", - "libpgQueryCommit": "7791971be1fc169d3f1b0d1ec887dc3f134ad134", + "libpgQueryTag": "devin/1795668138-wasm-full-stack", + "libpgQueryCommit": "f975bb56d6b93b9ce23135da66cee81308ec3a98", "fullApi": true }, "files": [ diff --git a/versions/18/src/index.ts b/versions/18/src/index.ts index 5c2a9a1..89646cd 100644 --- a/versions/18/src/index.ts +++ b/versions/18/src/index.ts @@ -205,7 +205,15 @@ function invalidateModule(error: unknown): void { wasmModule = undefined; } -// An exception out of a WASM call (V8 stack overflow on deeply nested SQL, a +// The host engine's native stack (V8 and JSC: RangeError, Firefox: +// InternalError) can run out inside a WASM call before libpg_query's own +// stack depth check, which only measures the WASM stack. +function isStackOverflow(error: Error): boolean { + return (error instanceof RangeError && /call stack/i.test(error.message)) + || (error.name === 'InternalError' && /too much recursion/i.test(error.message)); +} + +// An exception out of a WASM call (host stack overflow on deeply nested SQL, a // trap, or exit() once the heap limit is hit) skips libpg_query's cleanup and // leaves the instance corrupt, so replace it with a fresh one. function callWasm(fn: () => number): number { @@ -217,6 +225,10 @@ function callWasm(fn: () => number): number { if (proc) proc.exitCode = exitCode; if (error instanceof Error) { invalidateModule(error); + if (isStackOverflow(error)) { + const message = 'stack depth limit exceeded'; + throw Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause: error }); + } throw error; } const reason = typeof error === 'object' && error !== null From 09c299f04078cdad4f3bf81f3753b60ef573d069 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:52:53 +0000 Subject: [PATCH 04/13] test(wasm): keep the stack depth errors in the heap-release test --- versions/18/test/wasm-recovery.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index df94486..32ba1c5 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -99,8 +99,9 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { gc(); gc(); const base = process.memoryUsage().external; const deep = 'SELECT 1' + '+1'.repeat(100000); + const kept = []; for (let i = 0; i < 30; i++) { - try { lib.parseSync(deep); } catch {} + try { lib.parseSync(deep); } catch (error) { kept.push(error); } await lib.loadModule(); } const huge = 'SELECT 1;\\n'.repeat(5000000); From cbdb2a1f627b3605edeffd77f7e52831bdaf643a Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:52:53 +0000 Subject: [PATCH 05/13] fix(wasm): format the overflow's stack so a kept error doesn't pin the dropped instance --- templates/full/index.ts | 2 ++ versions/18/src/index.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/templates/full/index.ts b/templates/full/index.ts index d40bb06..537f186 100644 --- a/templates/full/index.ts +++ b/templates/full/index.ts @@ -220,6 +220,8 @@ function callWasm(fn: () => number): number { invalidateModule(error); if (isStackOverflow(error)) { const message = 'stack depth limit exceeded'; + // Until its stack is formatted, the error's WASM frames keep the dropped instance alive. + void error.stack; throw Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause: error }); } throw error; diff --git a/versions/18/src/index.ts b/versions/18/src/index.ts index 89646cd..edb7a02 100644 --- a/versions/18/src/index.ts +++ b/versions/18/src/index.ts @@ -227,6 +227,8 @@ function callWasm(fn: () => number): number { invalidateModule(error); if (isStackOverflow(error)) { const message = 'stack depth limit exceeded'; + // Until its stack is formatted, the error's WASM frames keep the dropped instance alive. + void error.stack; throw Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause: error }); } throw error; From 02aca435bb7a5c4d3596cf2e96eb828b720f33c6 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 05:36:30 +0000 Subject: [PATCH 06/13] test(release-diff): allow 18.2.7's capped depth limits; lower SQL depths that overflow V8 on macOS --- templates/full/index.ts | 14 ++++---- versions/18/release-diff.allow.json | 44 +++++++++++++++++++++++++- versions/18/src/index.ts | 14 ++++---- versions/18/test/wasm-recovery.test.js | 7 ++-- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/templates/full/index.ts b/templates/full/index.ts index 537f186..baff522 100644 --- a/templates/full/index.ts +++ b/templates/full/index.ts @@ -206,6 +206,13 @@ function isStackOverflow(error: Error): boolean { || (error.name === 'InternalError' && /too much recursion/i.test(error.message)); } +function stackDepthError(cause: Error): SqlError { + const message = 'stack depth limit exceeded'; + // Until its stack is formatted, the error's WASM frames keep the dropped instance alive. + void cause.stack; + return Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause }); +} + // An exception out of a WASM call (host stack overflow on deeply nested SQL, a // trap, or exit() once the heap limit is hit) skips libpg_query's cleanup and // leaves the instance corrupt, so replace it with a fresh one. @@ -218,12 +225,7 @@ function callWasm(fn: () => number): number { if (proc) proc.exitCode = exitCode; if (error instanceof Error) { invalidateModule(error); - if (isStackOverflow(error)) { - const message = 'stack depth limit exceeded'; - // Until its stack is formatted, the error's WASM frames keep the dropped instance alive. - void error.stack; - throw Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause: error }); - } + if (isStackOverflow(error)) throw stackDepthError(error); throw error; } const reason = typeof error === 'object' && error !== null diff --git a/versions/18/release-diff.allow.json b/versions/18/release-diff.allow.json index 6c1ea86..20d8b73 100644 --- a/versions/18/release-diff.allow.json +++ b/versions/18/release-diff.allow.json @@ -1,3 +1,45 @@ { - "allow": [] + "allow": [ + { + "id": [ + "edge:*:parse", + "edge:*:normalize", + "edge:*:fingerprint" + ], + "baseline": "18.2.7", + "published": "stack depth limit exceeded", + "candidate": "ok", + "reason": "18.2.7 capped the stack depth check at 512 kB of the WASM stack; the check now uses the whole stack, so deep SQL parses again as in 18.1.5" + }, + { + "id": [ + "edge:*:parse", + "edge:*:scan" + ], + "baseline": "18.2.7", + "published": "stack depth limit exceeded", + "candidate": "RangeError: Maximum call stack size exceeded", + "reason": "Same change: the candidate parses these now, and the RangeError is this script's JSON.stringify overflowing on the very deep parse tree" + }, + { + "id": "edge:plpgsql-*:parsePlPgSQL", + "baseline": "18.2.7", + "published": "Error: stack depth limit exceeded", + "candidate": "ok", + "reason": "The 512 kB cap stopped PL/pgSQL at about 350 nested blocks; without it they parse again as in 18.1.5" + }, + { + "id": [ + "edge:*:parse", + "edge:*:normalize", + "edge:*:fingerprint", + "edge:*:scan", + "edge:plpgsql-*:parsePlPgSQL" + ], + "baseline": "18.2.7", + "published": "RangeError: Maximum call stack size exceeded", + "candidate": "stack depth limit exceeded", + "reason": "A host stack overflow inside the parser or JSON.parse is now reported as a stack depth error instead of a raw RangeError" + } + ] } diff --git a/versions/18/src/index.ts b/versions/18/src/index.ts index edb7a02..f646d9e 100644 --- a/versions/18/src/index.ts +++ b/versions/18/src/index.ts @@ -213,6 +213,13 @@ function isStackOverflow(error: Error): boolean { || (error.name === 'InternalError' && /too much recursion/i.test(error.message)); } +function stackDepthError(cause: Error): SqlError { + const message = 'stack depth limit exceeded'; + // Until its stack is formatted, the error's WASM frames keep the dropped instance alive. + void cause.stack; + return Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause }); +} + // An exception out of a WASM call (host stack overflow on deeply nested SQL, a // trap, or exit() once the heap limit is hit) skips libpg_query's cleanup and // leaves the instance corrupt, so replace it with a fresh one. @@ -225,12 +232,7 @@ function callWasm(fn: () => number): number { if (proc) proc.exitCode = exitCode; if (error instanceof Error) { invalidateModule(error); - if (isStackOverflow(error)) { - const message = 'stack depth limit exceeded'; - // Until its stack is formatted, the error's WASM frames keep the dropped instance alive. - void error.stack; - throw Object.assign(new SqlError(message, { message, cursorPosition: 0 }), { cause: error }); - } + if (isStackOverflow(error)) throw stackDepthError(error); throw error; } const reason = typeof error === 'object' && error !== null diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index 32ba1c5..28c7369 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -50,11 +50,10 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { await loadModule(); const nested = { 'chained operators': 'SELECT 1' + '+1'.repeat(4000), - 'nested subqueries': 'SELECT ' + '(SELECT '.repeat(1500) + '1' + ')'.repeat(1500), - 'nested function calls': 'SELECT ' + 'f('.repeat(3500) + '1' + ')'.repeat(3500), + 'nested function calls': 'SELECT ' + 'f('.repeat(3200) + '1' + ')'.repeat(3200), }; for (const [name, sql] of Object.entries(nested)) { - assert.equal(parseSync(sql).stmts.length, 1, name); + assert.doesNotThrow(() => assert.equal(parseSync(sql).stmts.length, 1), name); } const blocks = { 'nested IF blocks': ['IF true THEN ', 'NULL;', ' END IF;'], @@ -64,7 +63,7 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { for (const [name, [open, body, close]] of Object.entries(blocks)) { const sql = 'CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN ' + open.repeat(1000) + body + close.repeat(1000) + ' END $$'; - assert.equal(lib.parsePlPgSQLSync(sql).plpgsql_funcs.length, 1, name); + assert.doesNotThrow(() => assert.equal(lib.parsePlPgSQLSync(sql).plpgsql_funcs.length, 1), name); } }); From ffc70bd75f73cd076ff7b5cb68df2d6fb7770123 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 05:49:20 +0000 Subject: [PATCH 07/13] test(wasm): normalize deep nesting on an instance that replaced an overflowed one --- versions/18/test/wasm-recovery.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index 28c7369..b721f60 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -67,6 +67,19 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { } }); + // Each nesting level of normalize used to cross from WASM into JS and back, + // which V8 only makes cheap once an instance is warm: a fresh instance, like + // the one that replaces an overflowed one, stopped at about 770 terms. + it('normalizes deep nesting on an instance that just replaced an overflowed one', async () => { + await loadModule(); + const chained = 'SELECT 1' + '+1'.repeat(3000); + const normalized = 'SELECT $1' + Array.from({ length: 3000 }, (_, i) => `+$${i + 2}`).join(''); + for (let i = 0; i < 3; i++) { + assert.throws(() => lib.normalizeSync(DEEP), /stack depth limit exceeded/); + assert.equal(lib.normalizeSync(chained), normalized); + } + }); + // V8's native stack can run out inside a WASM call before libpg_query's own // check, e.g. in normalize, whose frames are larger there. it('reports a stack depth error, not a RangeError, for nesting too deep to parse', async () => { From 63fc46c04600b4312f96de53e9e74af347a91ef3 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 05:51:42 +0000 Subject: [PATCH 08/13] fix(wasm): pin libpg_query with normalize off the per-level PG_TRY path --- versions/18/Makefile | 4 ++-- versions/18/package.json | 4 ++-- versions/18/test/wasm-recovery.test.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/versions/18/Makefile b/versions/18/Makefile index d1b33ab..359ffde 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -18,8 +18,8 @@ WASM_STACK_SIZE ?= 33554432 # libpg_query source, pinned to an exact commit so a release can be rebuilt # byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git -LIBPG_QUERY_TAG := devin/1795668138-wasm-full-stack -LIBPG_QUERY_COMMIT := f975bb56d6b93b9ce23135da66cee81308ec3a98 +LIBPG_QUERY_TAG := devin/1795671760-normalize-depth +LIBPG_QUERY_COMMIT := 18457636e47930e5aedaf915087616ca7dd14056 CACHE_DIR := .cache diff --git a/versions/18/package.json b/versions/18/package.json index 7eb2be3..cc33494 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -14,8 +14,8 @@ "pgVersion": "18", "distTag": "pg18", "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", - "libpgQueryTag": "devin/1795668138-wasm-full-stack", - "libpgQueryCommit": "f975bb56d6b93b9ce23135da66cee81308ec3a98", + "libpgQueryTag": "devin/1795671760-normalize-depth", + "libpgQueryCommit": "18457636e47930e5aedaf915087616ca7dd14056", "fullApi": true }, "files": [ diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index b721f60..90099c8 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -89,7 +89,7 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { ['parseSync', DEEP], ['parseSync', subqueries(3000)], ['normalizeSync', DEEP], - ['normalizeSync', subqueries(1000)], + ['normalizeSync', subqueries(3000)], ]; for (const [name, sql] of tooDeep) { assert.throws(() => lib[name](sql), (error) => { From 03f49a2f905e6ca54a45451a6e6a6fbbd9b47453 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 06:00:14 +0000 Subject: [PATCH 09/13] chore(v18): pin libpg_query to 18-constructive f9f1b67 (constructive-io/libpg_query#20) --- versions/18/Makefile | 4 ++-- versions/18/package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/versions/18/Makefile b/versions/18/Makefile index d1b33ab..1674ee4 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -18,8 +18,8 @@ WASM_STACK_SIZE ?= 33554432 # libpg_query source, pinned to an exact commit so a release can be rebuilt # byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git -LIBPG_QUERY_TAG := devin/1795668138-wasm-full-stack -LIBPG_QUERY_COMMIT := f975bb56d6b93b9ce23135da66cee81308ec3a98 +LIBPG_QUERY_TAG := 18-constructive +LIBPG_QUERY_COMMIT := f9f1b676f629a831bd2c5f7b34a2dc41d9cdf967 CACHE_DIR := .cache diff --git a/versions/18/package.json b/versions/18/package.json index 7eb2be3..8601dc3 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -14,8 +14,8 @@ "pgVersion": "18", "distTag": "pg18", "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", - "libpgQueryTag": "devin/1795668138-wasm-full-stack", - "libpgQueryCommit": "f975bb56d6b93b9ce23135da66cee81308ec3a98", + "libpgQueryTag": "18-constructive", + "libpgQueryCommit": "f9f1b676f629a831bd2c5f7b34a2dc41d9cdf967", "fullApi": true }, "files": [ From c59f3b664a4b003b89428567b2804eca488ad49b Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 07:31:06 +0000 Subject: [PATCH 10/13] Pin libpg_query to the fingerprint frame fix --- versions/18/Makefile | 4 ++-- versions/18/package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/versions/18/Makefile b/versions/18/Makefile index 359ffde..c0a4309 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -18,8 +18,8 @@ WASM_STACK_SIZE ?= 33554432 # libpg_query source, pinned to an exact commit so a release can be rebuilt # byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git -LIBPG_QUERY_TAG := devin/1795671760-normalize-depth -LIBPG_QUERY_COMMIT := 18457636e47930e5aedaf915087616ca7dd14056 +LIBPG_QUERY_TAG := devin/1795677500-fingerprint-frames +LIBPG_QUERY_COMMIT := 847f6c095076388664b8cde6ea6e2b86fadb039d CACHE_DIR := .cache diff --git a/versions/18/package.json b/versions/18/package.json index cc33494..f716be2 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -14,8 +14,8 @@ "pgVersion": "18", "distTag": "pg18", "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", - "libpgQueryTag": "devin/1795671760-normalize-depth", - "libpgQueryCommit": "18457636e47930e5aedaf915087616ca7dd14056", + "libpgQueryTag": "devin/1795677500-fingerprint-frames", + "libpgQueryCommit": "847f6c095076388664b8cde6ea6e2b86fadb039d", "fullApi": true }, "files": [ From 577b528c80d5aa6d4e643f3eb28f2420eedd9ed6 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 07:41:07 +0000 Subject: [PATCH 11/13] test(v18): run the fingerprint and normalize depth tests on Node 20 and raise the normalize limits --- .github/workflows/ci.yml | 1 - versions/18/test/limits.test.js | 17 ++++++----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index afdd71f..31962ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,6 @@ jobs: - { name: 'v16', path: 'versions/16', version: '16' } - { name: 'v17', path: 'versions/17', version: '17' } - { name: 'v18', path: 'versions/18', version: '18' } - # Node 20 cannot run the fingerprint and normalize depth tests. include: - { os: ubuntu-latest, node: '22.x', package: { name: 'v18', path: 'versions/18', version: '18' } } fail-fast: false diff --git a/versions/18/test/limits.test.js b/versions/18/test/limits.test.js index 38fa793..908fdef 100644 --- a/versions/18/test/limits.test.js +++ b/versions/18/test/limits.test.js @@ -10,8 +10,8 @@ const list = (item, separator) => (n) => Array.from({ length: n }, (_, i) => ite // fails here whatever the last release did. Raise a number when the build // gains depth; lower one only for a change the release notes describe. const SQL = { - 'chained +': [(n) => 'SELECT 1' + '+1'.repeat(n), { parse: 4000, fingerprint: 5000, normalize: 500 }], - 'chained ||': [(n) => "SELECT 'a'" + " || 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 500 }], + 'chained +': [(n) => 'SELECT 1' + '+1'.repeat(n), { parse: 4000, fingerprint: 5000, normalize: 1500 }], + 'chained ||': [(n) => "SELECT 'a'" + " || 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1500 }], 'chained AND': [(n) => 'SELECT 1 WHERE ' + list(() => 'a = 1', ' AND ')(n), { parse: 5000, fingerprint: 5000, normalize: 5000 }], 'chained OR': [(n) => 'SELECT 1 WHERE ' + list(() => 'a = 1', ' OR ')(n), { parse: 5000, fingerprint: 5000, normalize: 5000 }], 'chained casts': [(n) => 'SELECT 1' + '::int'.repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1000 }], @@ -19,8 +19,8 @@ const SQL = { 'chained unary minus': [(n) => 'SELECT ' + '- '.repeat(n) + 'a', { parse: 2500, fingerprint: 4000, normalize: 1000 }], 'chained NOT': [(n) => 'SELECT ' + 'NOT '.repeat(n) + 'true', { parse: 2500, fingerprint: 4000, normalize: 500 }], 'nested parentheses': [(n) => 'SELECT ' + nest('(', '1', ')')(n), { parse: 4000, fingerprint: 4000, normalize: 4000 }], - 'nested function calls': [(n) => 'SELECT ' + nest('f(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }], - 'nested COALESCE': [(n) => 'SELECT ' + nest('COALESCE(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }], + 'nested function calls': [(n) => 'SELECT ' + nest('f(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 750 }], + 'nested COALESCE': [(n) => 'SELECT ' + nest('COALESCE(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 750 }], 'nested ROW': [(n) => 'SELECT ' + nest('ROW(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }], 'nested arrays': [(n) => 'SELECT ' + nest('ARRAY[', '1', ']')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }], 'nested CASE': [(n) => 'SELECT ' + nest('CASE WHEN a THEN ', '1', ' END')(n), { parse: 1000, fingerprint: 1000, normalize: 1000 }], @@ -42,11 +42,6 @@ const PLPGSQL = { const plpgsql = (body) => `CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ DECLARE x int; BEGIN ${body} END $$`; -// Node 20 runs out of stack in fingerprint and normalize at a few dozen -// levels once V8 has optimized them, on every release including 18.1.5. -const node20 = Number(process.versions.node.split('.')[0]) < 22 && - 'Node 20 overflows its stack in optimized fingerprint and normalize code'; - describe('Depth every exported function handles', { timeout: 300000 }, () => { before(async () => { await lib.loadModule(); @@ -59,13 +54,13 @@ describe('Depth every exported function handles', { timeout: 300000 }, () => { assert.equal((await lib.parse(sql)).stmts.length, 1); }); - it(`fingerprint and fingerprintSync: ${shape}, ${depth.fingerprint} deep`, { skip: node20 }, async () => { + it(`fingerprint and fingerprintSync: ${shape}, ${depth.fingerprint} deep`, async () => { const sql = make(depth.fingerprint); assert.match(lib.fingerprintSync(sql), /^[0-9a-f]{16}$/); assert.match(await lib.fingerprint(sql), /^[0-9a-f]{16}$/); }); - it(`normalize and normalizeSync: ${shape}, ${depth.normalize} deep`, { skip: node20 }, async () => { + it(`normalize and normalizeSync: ${shape}, ${depth.normalize} deep`, async () => { const sql = make(depth.normalize); assert.ok(lib.normalizeSync(sql).length > 0); assert.ok((await lib.normalize(sql)).length > 0); From fcb0d4ba89b8a7695e853a3cc7900c0736d3151f Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 08:07:21 +0000 Subject: [PATCH 12/13] chore(v18): pin libpg_query to the fingerprint fix merged with 18-constructive --- versions/18/Makefile | 2 +- versions/18/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/versions/18/Makefile b/versions/18/Makefile index c0a4309..b3bd452 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -19,7 +19,7 @@ WASM_STACK_SIZE ?= 33554432 # byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git LIBPG_QUERY_TAG := devin/1795677500-fingerprint-frames -LIBPG_QUERY_COMMIT := 847f6c095076388664b8cde6ea6e2b86fadb039d +LIBPG_QUERY_COMMIT := 6a095a06087c62a04d7ccb5bc608173138b59f5c CACHE_DIR := .cache diff --git a/versions/18/package.json b/versions/18/package.json index 248a405..3b6d78d 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -15,7 +15,7 @@ "distTag": "pg18", "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", "libpgQueryTag": "devin/1795677500-fingerprint-frames", - "libpgQueryCommit": "847f6c095076388664b8cde6ea6e2b86fadb039d", + "libpgQueryCommit": "6a095a06087c62a04d7ccb5bc608173138b59f5c", "fullApi": true }, "files": [ From 28f947aaa031892219c2269a8ca63f53869042ee Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 08:43:20 +0000 Subject: [PATCH 13/13] chore(v18): pin libpg_query to 18-constructive with both depth fixes --- versions/18/Makefile | 4 ++-- versions/18/package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/versions/18/Makefile b/versions/18/Makefile index b3bd452..ddd63c7 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -18,8 +18,8 @@ WASM_STACK_SIZE ?= 33554432 # libpg_query source, pinned to an exact commit so a release can be rebuilt # byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git -LIBPG_QUERY_TAG := devin/1795677500-fingerprint-frames -LIBPG_QUERY_COMMIT := 6a095a06087c62a04d7ccb5bc608173138b59f5c +LIBPG_QUERY_TAG := 18-constructive +LIBPG_QUERY_COMMIT := 91d254f764b270a02f8445d3b49030eb4fda0302 CACHE_DIR := .cache diff --git a/versions/18/package.json b/versions/18/package.json index 866a7b6..ff3a87b 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -14,8 +14,8 @@ "pgVersion": "18", "distTag": "pg18", "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", - "libpgQueryTag": "devin/1795677500-fingerprint-frames", - "libpgQueryCommit": "6a095a06087c62a04d7ccb5bc608173138b59f5c", + "libpgQueryTag": "18-constructive", + "libpgQueryCommit": "91d254f764b270a02f8445d3b49030eb4fda0302", "fullApi": true }, "files": [