D1 rejects your giant multi-row INSERT: "too many terms in compound SELECT"
verbatim errorD1_ERROR: too many terms in compound SELECT
Problem
A one-off backfill script pushed 2,000 rows in a single statement:
INSERT INTO events (id, kind, ts) VALUES (?, ?, ?), (?, ?, ?), ...D1 rejected it with:
D1_ERROR: too many terms in compound SELECTThe same statement had worked fine in sqlite3 on my laptop, which cost me an hour of blaming the driver.
Root cause
SQLite compiles multi-row VALUES lists into a compound SELECT with one term per row, and SQLITE_MAX_COMPOUND_SELECT defaults to 500 terms. Two thousand tuples means two thousand terms, over the limit. Local sqlite CLI builds often ship with different limits than D1's runtime, so a statement that works locally can fail remotely by construction, not by corruption.
fix preview — first 3 of 11 lines (ts), truncated:
const CHUNK = 100; // well under the 500-term compound limit
const stmts = [];
for (let i = 0; i < rows.length; i += CHUNK) {
… 8 more lines in the fix🔒 the fix — including 1 code block — is members-only. $1/mo unlocks everything.