From fd8b6b5eec4aa5e005cd70d11765b7e9eae177d8 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 02:19:54 +0000 Subject: [PATCH 1/2] Size the stack depth limit from the WebAssembly stack in Emscripten builds --- src/pg_query.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/pg_query.c b/src/pg_query.c index 90e9f0bb2..631276bfb 100644 --- a/src/pg_query.c +++ b/src/pg_query.c @@ -21,6 +21,11 @@ #include #endif +#if defined(__EMSCRIPTEN__) +/* For emscripten_stack_get_end() */ +#include +#endif + #if defined(_WIN32) /* For GetCurrentThreadStackLimits(); port/win32.h already sets _WIN32_WINNT. */ #include @@ -28,6 +33,16 @@ #include +#if defined(__EMSCRIPTEN__) +/* + * WebAssembly code also runs on the host engine's native stack (about 1MB in + * Node and browsers), which overflows before 2MB of WebAssembly stack is used. + */ +#define PG_QUERY_MAX_STACK_DEPTH_KB 1024 +#else +#define PG_QUERY_MAX_STACK_DEPTH_KB 2048 +#endif + const char* progname = "pg_query"; __thread sig_atomic_t pg_query_initialized = 0; @@ -169,6 +184,9 @@ static char *get_lowest_stackaddr(void) pg_query_lowest_stackaddr = (char *) low; } +#elif defined(__EMSCRIPTEN__) + /* WebAssembly's stack is a region in linear memory that grows down */ + pg_query_lowest_stackaddr = (char *) emscripten_stack_get_end(); #elif defined(HAVE_PTHREAD) && defined(__linux__) { pthread_attr_t attr; @@ -253,9 +271,9 @@ static void set_max_stack_depth(void) if (new_limit <= 100) return; - /* Clamp at 2MB, like Postgres auto-detection logic. */ - if (new_limit > 2048) - new_limit = 2048; + /* Clamp at 2MB (1MB in WebAssembly), like Postgres auto-detection logic. */ + if (new_limit > PG_QUERY_MAX_STACK_DEPTH_KB) + new_limit = PG_QUERY_MAX_STACK_DEPTH_KB; max_stack_depth = (int) new_limit; assign_max_stack_depth((int) new_limit, NULL); From e92283de0a4e3225e4b307e12d801a6f3099c18f Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 02:31:57 +0000 Subject: [PATCH 2/2] Use a 512kB stack depth limit in WebAssembly --- src/pg_query.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pg_query.c b/src/pg_query.c index 631276bfb..6eefbd3d6 100644 --- a/src/pg_query.c +++ b/src/pg_query.c @@ -36,9 +36,10 @@ #if defined(__EMSCRIPTEN__) /* * WebAssembly code also runs on the host engine's native stack (about 1MB in - * Node and browsers), which overflows before 2MB of WebAssembly stack is used. + * Node and browsers), where its frames are larger than on the WebAssembly + * stack, so a larger limit overflows the native stack before it is reached. */ -#define PG_QUERY_MAX_STACK_DEPTH_KB 1024 +#define PG_QUERY_MAX_STACK_DEPTH_KB 512 #else #define PG_QUERY_MAX_STACK_DEPTH_KB 2048 #endif @@ -271,7 +272,7 @@ static void set_max_stack_depth(void) if (new_limit <= 100) return; - /* Clamp at 2MB (1MB in WebAssembly), like Postgres auto-detection logic. */ + /* Clamp at 2MB (512kB in WebAssembly), like Postgres auto-detection logic. */ if (new_limit > PG_QUERY_MAX_STACK_DEPTH_KB) new_limit = PG_QUERY_MAX_STACK_DEPTH_KB;