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 1qebvl-007GJS-EB for pgsql-general@arkaria.postgresql.org; Fri, 08 Sep 2023 13:53:53 +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 1qebvk-00Aog3-7f for pgsql-general@arkaria.postgresql.org; Fri, 08 Sep 2023 13:53:52 +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 1qebvj-00AofZ-SY for pgsql-general@lists.postgresql.org; Fri, 08 Sep 2023 13:53:51 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qebvf-003tua-Iw for pgsql-general@lists.postgresql.org; Fri, 08 Sep 2023 13:53:51 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 388DrXS11871320; Fri, 8 Sep 2023 09:53:34 -0400 From: Tom Lane To: gzh cc: pgsql-general@lists.postgresql.org Subject: Re: ERROR: stack depth limit exceeded In-reply-to: <1211c4ba.2822.18a72d975fc.Coremail.gzhcoder@126.com> References: <1e6b9056.492e.18a6e9a1798.Coremail.gzhcoder@126.com> <1694898.1694093396@sss.pgh.pa.us> <1211c4ba.2822.18a72d975fc.Coremail.gzhcoder@126.com> Comments: In-reply-to gzh message dated "Fri, 08 Sep 2023 11:32:48 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1871318.1694181213.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 08 Sep 2023 09:53:33 -0400 Message-ID: <1871319.1694181213@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk gzh writes: > In the Release Notes for PostgreSQL 12.14, we saw the following change: > https://www.postgresql.org/docs/release/12.14/ >> Add recursion and looping defenses in subquery pullup (Tom Lane) >> A contrived query can result in deep recursion and unreasonable amounts= of time spent trying to flatten subqueries. A proper fix for that seems u= nduly invasive for a back-patch, but we can at least add stack depth check= s and an interrupt check to allow the query to be cancelled. > Our understanding is that this change will cause some complex SQL statem= ents = > that were previously not reporting errors to report errors in the new ve= rsion. = The key word there is "contrived". You are not going to hit this limit without intentionally trying. The example that led to adding this check was a synthetic query with 10000 UNION ALL branches: https://www.postgresql.org/message-id/flat/703c09a2-08f3-d2ec-b33d-dbecd62= 428b8%40postgrespro.ru Also notice that the query misbehaved before this patch, too, by consuming excessive RAM. regards, tom lane