Skip to content

Normalize deeply nested expressions without a PG_TRY block per level - #21

Merged
devin-ai-integration[bot] merged 5 commits into
18-constructivefrom
devin/1795671760-normalize-depth
Nov 26, 2026
Merged

devin-ai-integration[bot] merged 5 commits into
18-constructivefrom
devin/1795671760-normalize-depth

Conversation

@pyramation

@pyramation pyramation commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

In WebAssembly builds pg_query_normalize runs out of the host engine's native stack at about 770 chained terms (SELECT 1+1+…) on a fresh instance, while pg_query_parse handles several thousand.

Cause: const_record_walker wraps raw_expression_tree_walker in PG_TRY for every node it has no case for. Emscripten implements setjmp by routing every call made from a function that contains one through a JavaScript invoke_* trampoline, so each level of the expression costs wasm → JS → wasm frames on V8's ~1 MB native stack:

at invoke_iiii (libpg-query.js)
at wasm-function[471]
at wasm-function[497]
at invoke_iiii (libpg-query.js)
...

V8 only compiles cheaper wrappers for those crossings after 1,000 calls on an instance (--wasm-wrapper-tiering-budget). That is why a warmed-up instance reached about 1,640 terms, and why libpg-query-node 18.2.x, which replaces the instance after an overflow, sees 770 again where 18.1.5 kept its warm (but corrupt) instance.

Change:

  • The PG_TRY block moves into its own pg_noinline function, const_record_walker_ignore_errors, so const_record_walker itself no longer contains a setjmp.
  • Node types that raw_expression_tree_walker is known to handle and that make up nested expressions (A_Expr, BoolExpr, SubLink, FuncCall, List, TypeCast, CaseExpr, JoinExpr, …) call it directly. Everything else still goes through the PG_TRY path, so unknown node types are ignored as before and stack depth errors still propagate.

Result on a fresh instance (Node 24, 32 MB STACK_SIZE, unchanged): normalize handles 4,000 chained terms, up from about 770. No memory or stack setting changes. Native builds skip one sigsetjmp per expression node.

The test commit (normalize 3000 chained terms in test/wasm_depth.c) is pushed first and fails without the fix.


libpg-query and parser opened this PR for Dan
Implemented in session Audit WASM memory history and verify full-stack depth
Review with Devin · Open in Devin Desktop


@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


The change introduces a single new test, test/wasm_depth.c, which drives the WASM parser with deeply nested expression chains to verify stack-overflow guards and parameter normalization behavior. No production code paths are modified; the test's buffer arithmetic and error paths were checked and found correct. The only noted fragility is the test's heuristic of counting $ characters in normalized output as a proxy for parameter count, which could mislead future maintainers if the grammar output format changes.

Files Change
test/wasm_depth.c New WASM depth-limit regression test validating normalization and stack-overflow handling.

Reviewed commit: b8f5945

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 66a7a719-2ab5-4dd9-a155-f1b191e7074e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@devin-ai-integration
devin-ai-integration Bot changed the base branch from devin/1795668138-wasm-full-stack to 18-constructive November 26, 2026 06:00
@devin-ai-integration
devin-ai-integration Bot merged commit 00bf861 into 18-constructive Nov 26, 2026
49 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant