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 1oukLP-0007xn-4b for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Nov 2022 01:02:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oukLN-0005CI-Tn for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Nov 2022 01:02:29 +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 1oukLN-0005C7-II for pgsql-hackers@lists.postgresql.org; Tue, 15 Nov 2022 01:02:29 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oukLL-0003wY-29 for pgsql-hackers@postgresql.org; Tue, 15 Nov 2022 01:02:29 +0000 Received: by mail-pf1-x435.google.com with SMTP id 140so11217202pfz.6 for ; Mon, 14 Nov 2022 17:02:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=CCMFSFN7lSI6owBoKhxbkl8L+cfQtO1XD6FfO79xTgE=; b=DUbWAaj1h/ngSXo3cGYZjtjQx9TFmcuC4uxcy7eIvEsc6FU8Ll6kzrH5eIIhRp+QSk e4pBWoJZfvjrxjUPSmJ2couDsBYtKEvDazD20eY7TGH8GLHMuhf9OgpgkhXbrhIVlw1f 50KjCHXR+hPSoinjMXUZnb5h2OCsXxBvJZ347h2KZxP9IJwP/MPracCgWDpZm2ocCLKj fMw6EwcQEXwJ9jrSxIQSiQVGmlQmcOib8fgTh+IL6jMA7SRG8H3bBiBjIf3gUAeDIGNi C6pEFzatlmXWySS8lgJdnmzTOimVZIgFcpV9UsnDttpLn4XJj2jSeanaJ1B/XLRtAvir g1lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CCMFSFN7lSI6owBoKhxbkl8L+cfQtO1XD6FfO79xTgE=; b=3L80yxRBwRViFt8Wxu6+vQE8R52Xod8a8u5iQUfVM676JBN2Nb6yPhcicuXowGAVh2 ised00i6AWKBucm8YslOW64KzUuRSiY0d8MK2iwtcdqhaksp4MxayQmctNOVO15/5aq2 Q416J7tpvWZSb5c0+KQIHy40Td+VB5jC1u0F34XlEC3+1+WKogc6AiRnZ2XeGM/EFklN gRB/aHaT795ql4gpNpr1w9wSA6ZuxcxNvaG6a+KLb97hHYy2kOz4zesi+AHlKw/yYHtu ixD0CE1LNJxZr1Ma+5yr+4ThcESmSGLhZV2V+iYlfVlzd5mTCy2t5tRBzXNJ9oLUOzPu no7w== X-Gm-Message-State: ANoB5plcA1k4RPHj/2hRGN+7WmHHI+BpzON7F0OfHmh2UPowA4p5tuRp tIf4yh22MgUot+GFzkfm4yn3XleDq2xDrJETweA= X-Google-Smtp-Source: AA0mqf7aZBsr7Cwj7JM4DwTsr2RrG1rjBjtGQlsPNindn+YAxKIIRPUtYptxpZ2B4AWflGbEHdYCdh8ZgMOmmawF4iE= X-Received: by 2002:a63:195c:0:b0:46f:b6e1:1966 with SMTP id 28-20020a63195c000000b0046fb6e11966mr13519861pgz.625.1668474144995; Mon, 14 Nov 2022 17:02:24 -0800 (PST) MIME-Version: 1.0 References: <247851.1668293143@sss.pgh.pa.us> In-Reply-To: <247851.1668293143@sss.pgh.pa.us> From: Richard Guo Date: Tue, 15 Nov 2022 09:02:13 +0800 Message-ID: Subject: Re: A new strategy for pull-up correlated ANY_SUBLINK To: Tom Lane Cc: Andy Fan , pgsql-hackers Content-Type: multipart/alternative; boundary="000000000000babf0d05ed77e8b9" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000babf0d05ed77e8b9 Content-Type: text/plain; charset="UTF-8" On Sun, Nov 13, 2022 at 6:45 AM Tom Lane wrote: > Looking again at that contain_vars_of_level restriction, I think the > reason for it was just to avoid making a FROM subquery that has outer > references, and the reason we needed to avoid that was merely that we > didn't have LATERAL at the time. So I experimented with the attached. > It seems to work, in that we don't get wrong answers from any of the > small number of places that are affected. (I wonder though whether > those test cases still test what they were intended to, particularly > the postgres_fdw one. We might have to try to hack them some more > to not get affected by this optimization.) Could do with more test > cases, no doubt. Hmm, it seems there were discussions about this change before, such as in [1]. > One thing I'm not at all clear about is whether we need to restrict > the optimization so that it doesn't occur if the subquery contains > outer references falling outside available_rels. I think that that > case is covered by is_simple_subquery() deciding later to not pull up > the subquery based on LATERAL restrictions, but maybe that misses > something. I think we need to do this, otherwise we'd encounter the problem described in [2]. In short, the problem is that the constraints imposed by LATERAL references may make us fail to find any legal join order. As an example, consider explain select * from A where exists (select * from B where A.i in (select C.i from C where C.j = B.j)); ERROR: failed to build any 3-way joins [1] https://www.postgresql.org/message-id/flat/CAN_9JTx7N%2BCxEQLnu_uHxx%2BEscSgxLLuNgaZT6Sjvdpt7toy3w%40mail.gmail.com [2] https://www.postgresql.org/message-id/CAMbWs49cvkF9akbomz_fCCKS=D5TY=4KGHEQcfHPZCXS1GVhkA@mail.gmail.com Thanks Richard --000000000000babf0d05ed77e8b9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Sun, Nov 13, 2022 at 6:45 AM Tom Lane = <tgl@sss.pgh.pa.us> wrote:
Looking again at that contain_vars_of_level restriction, I think the
reason for it was just to avoid making a FROM subquery that has outer
references, and the reason we needed to avoid that was merely that we
didn't have LATERAL at the time.=C2=A0 So I experimented with the attac= hed.
It seems to work, in that we don't get wrong answers from any of the small number of places that are affected.=C2=A0 (I wonder though whether those test cases still test what they were intended to, particularly
the postgres_fdw one.=C2=A0 We might have to try to hack them some more
to not get affected by this optimization.)=C2=A0 Could do with more test cases, no doubt.
=C2=A0
Hmm, it seems there were= discussions about this change before, such as
in [1].
=C2= =A0
One thing I'm not at all clear about is whether we need to restrict
the optimization so that it doesn't occur if the subquery contains
outer references falling outside available_rels.=C2=A0 I think that that case is covered by is_simple_subquery() deciding later to not pull up
the subquery based on LATERAL restrictions, but maybe that misses
something.
=C2=A0
I think we need to do this, ot= herwise we'd encounter the problem
described in [2].=C2=A0 In short,= the problem is that the constraints imposed
by LATERAL references may m= ake us fail to find any legal join order.=C2=A0 As
an example, consider<= br>
explain select * from A where exists
=C2=A0 =C2=A0 (select * from= B where A.i in (select C.i from C where C.j =3D B.j));
ERROR: =C2=A0fai= led to build any 3-way joins

[1] https://www.postgresql.org/message-id/flat/CAN_9JTx7N%2BC= xEQLnu_uHxx%2BEscSgxLLuNgaZT6Sjvdpt7toy3w%40mail.gmail.com

[2] <= a href=3D"https://www.postgresql.org/message-id/CAMbWs49cvkF9akbomz_fCCKS= =3DD5TY=3D4KGHEQcfHPZCXS1GVhkA@mail.gmail.com">https://www.postgresql.org/m= essage-id/CAMbWs49cvkF9akbomz_fCCKS=3DD5TY=3D4KGHEQcfHPZCXS1GVhkA@mail.gmai= l.com

Thanks
Richard
--000000000000babf0d05ed77e8b9--