Skip to content

Fingerprint: keep per-node functions out of _fingerprintNode's stack frame - #23

Merged
devin-ai-integration[bot] merged 9 commits into
18-constructivefrom
devin/1795677500-fingerprint-frames
Nov 26, 2026
Merged

devin-ai-integration[bot] merged 9 commits into
18-constructivefrom
devin/1795677500-fingerprint-frames

Conversation

@pyramation

@pyramation pyramation commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

In the WASM build on Node.js 20, once V8 has moved the module to its optimizing compiler (TurboFan), pg_query_fingerprint overflows the native stack at about 30 chained terms, 13 nested function calls or 6 nested subqueries. pg_query_normalize fingerprints the query too, so it stops at the same depths. Published 18.1.5 behaves the same; it is easy to miss because a fresh instance runs on the baseline compiler, where depths are in the thousands.

Cause: each _fingerprint<NodeType> function has exactly one caller, the switch in _fingerprintNode. Binaryen's one-caller inlining merges all ~260 of them into _fingerprintNode, and Node 20's TurboFan gives that single function a native frame of roughly 30 kB per level.

Fix: the generated switch calls each function through a pg_noinline helper that takes the function pointer, so nothing can be merged into _fingerprintNode:

case T_RangeVar:
  _fingerprintString(ctx, "RangeVar");
  _fingerprintCall((FingerprintFunc) _fingerprintRangeVar, ctx, obj, parent, field_name, depth);
  break;

scripts/generate_fingerprint_outfuncs.rb emits this; pg_query_fingerprint_conds.c is the regenerated output. The cast differs only in the pointee type of the node pointer, the same convention as Postgres' tree_walker_callback. Fingerprints are unchanged (make test passes). No memory, stack or link setting changes.

Measured with the libpg-query-node v18 build, deepest input handled (TurboFan only / baseline only):

Node fingerprint chained terms fingerprint nested subqueries normalize chained terms normalize nested calls
20 before 28 / 19,999+ 6 / 3,329 33 / 4,826 16 / 2,413
20 after 19,999+ / 19,999+ 3,329 / 3,329 3,584 / 4,826 1,791 / 2,413
24 after 19,999+ / 19,999+ 3,329 / 3,329 2,455 / 4,818 1,226 / 2,407

Parse depths are unchanged. A link-time alternative (--one-caller-inline-max-function-size) fixes fingerprint as well but changes inlining in the parser and lowered some parse depths, so it was rejected.

Test first: the first commit adds a fingerprint mode to test/wasm_depth.c (1,000 nested subqueries, 2,000 nested function calls) and runs it in build_wasm with node --no-liftoff; build_wasm failed on that commit and passes with 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.


This PR tightens the normalize/fingerprint depth handling in libpg_query: the recursion walker now guards the depth-limited recursion with a single ERRCODE_STATEMENT_TOO_COMPLEX check, while the WASM depth test exercises much deeper nesting and CI updates the fingerprint/overflow variants accordingly.

Files Change
src/pg_query_normalize.c Restructures the walker's error handling so per-node PG_TRY is bypassed for known node types and the depth limit raises a single ERRCODE_STATEMENT_TOO_COMPLEX guard.
test/wasm_depth.c Extends the depth test to deeper nesting, including buffer-sizing math for the generated input.
.github/workflows/ci.yml Adjusts the WASM test invocation for the new depth/fingerprint coverage.

Two candidate findings were reviewed and both fell below the confidence threshold for reporting; no review-worthy defects remain.

Reviewed commit: cd792c3

@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: 9be088d5-54ab-4abf-b33b-5318d728a7d1

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 merged commit 91d254f into 18-constructive Nov 26, 2026
35 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