Skip to content

[Pg] Fix RangeError in mergeQueries for large batch inserts#5996

Open
skvarda wants to merge 1 commit into
drizzle-team:mainfrom
skvarda:fix/merge-queries-large-params
Open

[Pg] Fix RangeError in mergeQueries for large batch inserts#5996
skvarda wants to merge 1 commit into
drizzle-team:mainfrom
skvarda:fix/merge-queries-large-params

Conversation

@skvarda

@skvarda skvarda commented Jul 9, 2026

Copy link
Copy Markdown

Summary

Fixes #5994

mergeQueries in drizzle-orm/src/sql/sql.ts used result.params.push(...query.params) to concatenate params from sub-queries. The spread operator internally uses Function.prototype.apply, which has a hard argument limit in V8 (~125k on Node 22). When a nested SQL chunk accumulates params through recursive buildQueryFromSourceParams calls (e.g. a batch insert with ~8k rows x 15 cols = ~120k params), the outer mergeQueries call spreads the entire accumulated array and throws RangeError: Maximum call stack size exceeded.

Change

Replaced the spread operator with a for loop for both params and typings arrays in mergeQueries:

// Before (crashes on large arrays):
result.params.push(...query.params);

// After (no apply limit):
for (let i = 0; i < query.params.length; i++) {
    result.params.push(query.params[i]);
}

This eliminates the Function.prototype.apply argument-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.ts which builds a nested SQL with 200k params and verifies sqlToQuery() does not throw RangeError. This test fails before the fix and passes after.

All 567 existing unit tests continue to pass.

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
@skvarda skvarda force-pushed the fix/merge-queries-large-params branch from 417c345 to e4e12a6 Compare July 9, 2026 20:17
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.

Batch insert crashes with RangeError when row-count × column-count exceeds ~8 k rows × 15 cols (~120 k params)

1 participant