Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tM9Xh-002D3R-H6 for pgsql-bugs@arkaria.postgresql.org; Fri, 13 Dec 2024 17:33:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tM9Xe-009ohm-3a for pgsql-bugs@arkaria.postgresql.org; Fri, 13 Dec 2024 17:33:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tM97p-009S8n-52 for pgsql-bugs@lists.postgresql.org; Fri, 13 Dec 2024 17:06:50 +0000 Received: from mail.nh2.me ([2a01:4f8:1c0c:8227::1]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tM97m-002fsG-Cg for pgsql-bugs@lists.postgresql.org; Fri, 13 Dec 2024 17:06:49 +0000 Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nh2.me; s=mail; t=1734109604; bh=Z+rU+9zKwKuWHo/h3iL4/dD4tgF7wgOBy3OA7Y+Zfx4=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=iNd2LkIbAibW/GiFYH3rLbeq5nHTh0ygHUCvGEKq3TRe8LmE/wIFVlulz994lbO/O twvzlte8huP0nF9ZNVBy79XdMuUotJn67qIoN5J5qSvf0c5Xy97WZVR/md/vwIWXhd dZBKqIYeAtYPcO+HOxxc7/lGgeP3tCc2sHJ/SRKjxldmWQqYc3O0cjoeCgR3flq16f LwNgPY8i4H5b8ted3U+XAuJfSIXXAbROe74vTGOibURYEEjPBFsc6KAoydgXuID9Js RdLqOgD0x4J7HW3Z5JJQnKN3lxRCBUUC1U3+drXIQfI6JrmKyTJ8eE/Lj28JI/OEoK ME9AcaIQxlE2Q== Date: Fri, 13 Dec 2024 18:06:43 +0100 MIME-Version: 1.0 Subject: Re: "memory exhausted" in query parser/simplifier for many nested parentheses To: Greg Sabino Mullane Cc: Tom Lane , pgsql-bugs@lists.postgresql.org, ruben@benaco.com, =?UTF-8?Q?Niklas_Hamb=C3=BCchen?= References: <161892c0-65fc-42a6-8af9-b098879decd4@nh2.me> <654489.1734019548@sss.pgh.pa.us> From: =?UTF-8?Q?Niklas_Hamb=C3=BCchen?= Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Greg, thanks for testing the parser behaviour of "x in (A, B, ...)", it is useful to know that. > queries this large are not going to be caused by checkboxes being clicked. This is how I encountered it: Doing a selection of 10k files (equivalent to the Shift-click range-selection in most GUI file managers), and using them in a query. > We cannot simply push the parser to accept anything, regardless of the impact on other parts of the system. I'm not suggesting to remove limits regardless of impact. There are good limits and bad limits: I think the happy path is when programs scale to the users' machines and tasks, constrained by limits that achieve specific goals. Many of postgres's limits from https://www.postgresql.org/docs/17/runtime-config-resource.html achieve the goal of not having queries blow up unreasonably. They are good limits, set to sensible defaults by developers, and tunable by users to fit their workloads. Same for most of Linux's limits, such as `ulimit`. In contrast, the impact of `YYMAXDEPTH` on postgres looks like a historical accident. * It was set to `YMAXDEPTH 500` by Richard Stallman in _1988_ in Bison commit f791019c "entered into RCS" (thus probably even older). * Then changed to `YYMAXDEPTH 10000` in 1993. So we're looking at a > 35 years old hardcode; a bad limit that probably should always have been a runtime-configurable parameter. Maybe in 1993 "10000 of something" was a huge limit, but it isn't today. Most modern parsers today do not hardcode key aspects such as nesting depth, because what's "a lot" of course depends on the application, and it often isn't 4 or 500 or 10000. I also doesn't fit well into what Postgres usually delivers: As you showed, postgres by default supports "millions of things" in queries, and it really feels like a surprise bug that this limit is much lower when you write the query in an equally long but slightly different way, because that specific syntax code path goes through a library with hardcodes from 1993. So I think the ideal solution would be: * Make `YYMAXDEPTH` configurable at run-time (it always should have been), and add a postgres config for it with a good default. Of course it's not for me to judge whether this is a high priority topic for postgres, but I think it would be fair to classify it as a "one of the tools we use hardcodes a silly limit that doesn't fit to our other limits and thus triggers user surprise" bug, as opposed to "postgres authors designed it this way". Greetings, Niklas