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 1oq4fF-0001Pt-Rf for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Nov 2022 03:43:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oq4eF-0006vu-K7 for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Nov 2022 03:42:39 +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 ) id 1oq4eF-0006vk-6g for pgsql-hackers@lists.postgresql.org; Wed, 02 Nov 2022 03:42:39 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oq4e7-0004I6-N5 for pgsql-hackers@postgresql.org; Wed, 02 Nov 2022 03:42:38 +0000 Received: from [192.168.1.203] (unknown [37.151.227.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id E7B3F21C1C38; Wed, 2 Nov 2022 06:42:29 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1667360550; bh=Uopi/iLH4Ei6JH3KMXtVciCJNb38jQcW93N9+zoYOJw=; h=Date:Subject:To:References:From:In-Reply-To; b=DRIJsYDb0VZ0birdi/hBbPdRWLQAiQuC1x9oaakv0sKf8Iu/w7sB0Mn+1cwVlmNvt ANGKkP/RI7k3egdEVEQSej41VftmvKpFmQ0vLjb6Y36iyMPGzaBJn4bXuR0ZhSOGhc Zq0OCrUgPAWav7gdyDrRaF8QaOKPKBO0jpiVO4g8= Message-ID: <1bca28ab-e962-b68a-80c1-988b34eb6195@postgrespro.ru> Date: Wed, 2 Nov 2022 09:42:28 +0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: A new strategy for pull-up correlated ANY_SUBLINK To: Andy Fan , pgsql-hackers References: Content-Language: en-US From: Andrey Lepikhov Organization: Postgres Professional In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/11/2022 09:02, Andy Fan wrote: > In the past we pull-up the ANY-sublink with 2 steps, the first step is to > pull up the sublink as a subquery, and the next step is to pull up the > subquery if it is allowed.  The benefits of this method are obvious, > pulling up the subquery has more requirements, even if we can just finish > the first step, we still get huge benefits. However the bad stuff happens > if varlevelsup = 1 involves, things fail at step 1. > > convert_ANY_sublink_to_join ... > >     if (contain_vars_of_level((Node *) subselect, 1)) >         return NULL; > > In this patch we distinguish the above case and try to pull-up it within > one step if it is helpful, It looks to me that what we need to do is just > transform it to EXIST-SUBLINK. Maybe code [1] would be useful for your purposes/tests. We implemented flattening of correlated subqueries for simple N-J case, but found out that in some cases the flattening isn't obvious the best solution - we haven't info about cardinality/cost estimations and can do worse. I guess, for more complex flattening procedure (with aggregate function in a targetlist of correlated subquery) situation can be even worse. Maybe your idea has such corner cases too ? [1] https://www.postgresql.org/message-id/flat/CALNJ-vTa5VgvV1NPRHnypdnbx-fhDu7vWp73EkMUbZRpNHTYQQ%40mail.gmail.com -- regards, Andrey Lepikhov Postgres Professional