[Pg] Fix RangeError in mergeQueries for large batch inserts#5996
Open
skvarda wants to merge 1 commit into
Open
[Pg] Fix RangeError in mergeQueries for large batch inserts#5996skvarda wants to merge 1 commit into
skvarda wants to merge 1 commit into
Conversation
Replace spread operator (push(...arr)) with for-loop in mergeQueries to avoid V8's Function.prototype.apply argument limit (~125k on Node 22). This fixes crashes when batch inserts accumulate >120k params through recursive buildQueryFromSourceParams calls. Fixes drizzle-team#5994
417c345 to
e4e12a6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5994
mergeQueriesindrizzle-orm/src/sql/sql.tsusedresult.params.push(...query.params)to concatenate params from sub-queries. The spread operator internally usesFunction.prototype.apply, which has a hard argument limit in V8 (~125k on Node 22). When a nested SQL chunk accumulates params through recursivebuildQueryFromSourceParamscalls (e.g. a batch insert with ~8k rows x 15 cols = ~120k params), the outermergeQueriescall spreads the entire accumulated array and throwsRangeError: Maximum call stack size exceeded.Change
Replaced the spread operator with a
forloop for bothparamsandtypingsarrays inmergeQueries:This eliminates the
Function.prototype.applyargument-count limit and also removes the O(depth)-copies-per-param behaviour, making param collection O(total_params) instead of O(total_params x nesting_depth).Test
Added
drizzle-orm/tests/merge-queries.test.tswhich builds a nested SQL with 200k params and verifiessqlToQuery()does not throwRangeError. This test fails before the fix and passes after.All 567 existing unit tests continue to pass.