Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mutQo-00035W-1S for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Dec 2021 09:40:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mutQl-00081R-OX for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Dec 2021 09:40:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1mutQl-00081I-DV for pgsql-bugs@lists.postgresql.org; Wed, 08 Dec 2021 09:40:07 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1mutQi-0006wR-Eo for pgsql-bugs@lists.postgresql.org; Wed, 08 Dec 2021 09:40:07 +0000 Received: by mail-wm1-x32f.google.com with SMTP id o19-20020a1c7513000000b0033a93202467so1355563wmc.2 for ; Wed, 08 Dec 2021 01:40:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=0sXWVlJuLVyMa2eZf7GXPSc9MFRqRvOpg0qQez3yFl8=; b=S4NrfeK3ULr/itNMgM2qSf/kVRCjtY3spQ6gcQ8hy+7EY8KVmk/sIedmOPWTPtewbD ViQYm6THBw9oLWz2u2KQ522ge+faeMD46sALxfduYwC1OqvSdoAeImLWa2CPWCVWtXrs F+JpqbUPYaERdq5Z5Kza7HYrRiJBSQhgjTa74WArUq2PrRm2lIfPhmbKK6Ew0kTZvdA8 VdmCjkMRA6r35i7BHALUuqAM+Xn2gGzzriRdOmaTxd14SuOLw0KzB57sAs4VZZuzzSwh M7vvJcsJqfe+l9mIZQo1DBH0V3Jv8rRfCHTjpjd5UqatwXqT7leS1MiXYXCAKEWG+kim f+Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=0sXWVlJuLVyMa2eZf7GXPSc9MFRqRvOpg0qQez3yFl8=; b=6J7hh9cujs5zinlTwEH9vMyYImi9zzUMMw0qKAPlJf7pupDvv9PIVWlIiO5EDqTQWA HJzwFTsHX4jr19QS/Xr0qjgwXmsro/dJgnqpMISufcdeRraxxXK94rQPuN1zrcY2p6Od 1SW5UJhY1t+FH5LXQqhLtTf1BDV4lFU3rEKwmqbLjoY1VXVpSAJDrIs3oFoQKIt1ZGTu IW39etNKqGr/815iNmyRaYvXwtSPOvpdJMp1gZj3o8xUo1CU03FZbGgONRHDE27eDOSw 0PTX7dKAsOsoF6JY1TLQ8TjQS80sDYeotF6oPA5RgWfnnLn/sNQvvUf+V3QJYe1vpUUE JPlQ== X-Gm-Message-State: AOAM530SoDKCkHZ2ePn5qO5eP23bbXLdVeMQzlO7DN5e3o4Vpbqa3ynH r6+f2KgntSMWeT77sw+Gl+M= X-Google-Smtp-Source: ABdhPJxyOTOH+Fvjy2obcSS+XotFHuhdqKlZZlf4alsz71Jtauple5AXUI3/cvQK6KuMC8HnrL06/Q== X-Received: by 2002:a05:600c:4108:: with SMTP id j8mr14491814wmi.139.1638956402858; Wed, 08 Dec 2021 01:40:02 -0800 (PST) Received: from localhost (dslb-178-012-041-031.178.012.pools.vodafone-ip.de. [178.12.41.31]) by smtp.gmail.com with ESMTPSA id p14sm5042418wms.29.2021.12.08.01.40.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Dec 2021 01:40:02 -0800 (PST) Date: Wed, 8 Dec 2021 10:41:02 +0100 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Tom Lane Cc: 253540651@qq.com, pgsql-bugs@lists.postgresql.org, Peter Eisentraut Subject: Re: BUG #17318: ERROR: AddressSanitizer: SEGV on unknown address in optimizer Message-ID: <20211208094102.qrxyu2u2426e4wy5@localhost> References: <17318-2eb65a3a611d2368@postgresql.org> <3048015.1638802600@sss.pgh.pa.us> <20211207125925.jps4l6tr7htd6h3z@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="6q7mnkietnguam5x" Content-Disposition: inline In-Reply-To: <20211207125925.jps4l6tr7htd6h3z@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --6q7mnkietnguam5x Content-Type: text/plain; charset=us-ascii Content-Disposition: inline > On Tue, Dec 07, 2021 at 01:59:25PM +0100, Dmitry Dolgov wrote: > > On Mon, Dec 06, 2021 at 09:56:40AM -0500, Tom Lane wrote: > > PG Bug reporting form writes: > > > WITH RECURSIVE x ( x ) AS ( SELECT 1 UNION ALL SELECT x FROM LATERAL ( ( > > > SELECT * FROM ( ( SELECT 4 AS x ) UNION ALL ( SELECT 5 AS x ) ) AS x WHERE x > > > BETWEEN 1 AND 2 AND x < ( SELECT 3 GROUP BY DISTINCT ROLLUP ( x , x ) , > > > ROLLUP ( x , x ) ) ) UNION ALL ( SELECT ( SELECT x LIMIT 1 ) FROM x OFFSET 0 > > > LIMIT 5 ) ) AS x GROUP BY ROLLUP ( ( x , x , x ) , ( ( SELECT TRIM ( > > > TRAILING ' ' FROM SUBSTRING ( VERSION ( ) FROM '^[^0-9]*' ) ) WHERE ( x IS > > > NOT NULL ) ) , x ) ) ) CYCLE x SET BOOLEAN USING VALUES SELECT FROM x GROUP > > > BY DISTINCT CUBE ( x , x , x ) ; > > > > I simplified this to > > > > WITH RECURSIVE x ( x ) AS > > ( SELECT 1 > > UNION ALL > > SELECT x FROM > > ( > > SELECT 4 AS x > > UNION ALL > > SELECT x FROM x > > ) AS x > > ) > > CYCLE x SET b USING v > > SELECT * FROM x > > ; > > > > and now I'm not sure whether to consider this an optimizer bug > > or failure to detect an unsupported case. Our SELECT ref page > > says > > > > Both the SEARCH and the CYCLE clause are only valid for recursive WITH > > queries. The with_query must be a UNION (or UNION ALL) of two SELECT > > (or equivalent) commands (no nested UNIONs). > > > > This WITH query sure looks like nested UNIONs to me, so either > > that restriction is stated incorrectly, or it's being enforced > > inadequately. If the former, we have an optimizer problem. > > Looking through the original thread [1] it seems "nested UNIONs" means > constructions like "foo UNION bar UNION baz", which is indeed handled in > parse_cte. The existing restrictions probably have to be extended to > cover cases like here as well. Probably the most straightforward way to extend validation for such queries would be to add some sort of flag for catching nested UNIONs in CteState and verify presence of UNION and CYCLE in checkWellFormedSelectStmt. Not sure if it's a right way to fix the problem, but it passes the tests. One note, in the thread [1] Kyotaro Horiguchi is investigating similar issue and has proposed another patch to fix it, but it doesn't catch the problematic query here. [1]: https://www.postgresql.org/message-id/20211207.172546.1329872704555357126.horikyota.ntt%40gmail.com --6q7mnkietnguam5x Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Prevent-nested-UNIONs-in-CTE-with-CYCLE-clause.patch"