Nested BETWEEN makes parse analysis take exponential time and memory, and it can't be cancelled until analysis ends =================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a2347b-f1c0-7ea5-aa38-2ad8659ae892 Filed: 2026-12-02 02:01:07 UTC Severity: 25/100 Blamed commit: 34af082f95aa6adb8af5fbd4da46bd4c3c176856 ## Summary Nested BETWEEN expressions make parse analysis take exponential time and memory, and the work cannot be cancelled until parse analysis finishes: statement_timeout and pg_cancel_backend() only take effect afterwards. Reported on pgsql-bugs on 2026-10-14 (BUG #19758); no replies at the time of filing. ## Reproduction ```sql SET statement_timeout = '100ms'; SELECT 1 WHERE (((((((((((((((((((( true BETWEEN true AND false) BETWEEN true AND false) ... nested 20 deep ...); ``` About 1.6 s and 1.6 GB RSS are used before the timeout fires. Seen on master and 18.6. ## Analysis transformAExprBetween() copies the raw left operand for each comparison it builds: two copies for [NOT] BETWEEN and more for SYMMETRIC. Each nesting level therefore doubles the work (four times for SYMMETRIC). transformExprRecurse() has no interrupt check, so cancellation is not seen until parse analysis ends. The report suggests adding CHECK_FOR_INTERRUPTS() in transformExprRecurse() as a mitigation. ## Blame Unconfirmed. The filed blame, 34af082f95aa ("Represent BETWEEN as a special node type in raw parse trees"), introduced transformAExprBetween(); the operand duplication may be older (the grammar expanded BETWEEN before that commit). ## Branches Long-standing; present on all supported branches.