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 1tIP0E-00E73m-MC for pgsql-committers@arkaria.postgresql.org; Tue, 03 Dec 2024 09:15:30 +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 1tIP0C-008gKa-33 for pgsql-committers@arkaria.postgresql.org; Tue, 03 Dec 2024 09:15:29 +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 1tIP0B-008gKS-RY for pgsql-committers@lists.postgresql.org; Tue, 03 Dec 2024 09:15:29 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tIP09-000o10-Q2 for pgsql-committers@lists.postgresql.org; Tue, 03 Dec 2024 09:15:28 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-21572ca3cccso22233315ad.2 for ; Tue, 03 Dec 2024 01:15:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733217323; x=1733822123; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=p5yKP4CFUBPvLxsjJKQyHRBrFywG4zREYXhs05T127s=; b=Ie1UqvYxH+C/4TuFgGSfglgnbjVniRDbde9afSIilN93GE4C2gHLseFRd5gPAjzV5r ACwayIUSHRQWgjaqt70Eb4bAFhfWJ2lT4aVbU7Hbz/QPo9ohHXNRqCOLB4fMdedCEIR/ eRIcunUtI3YMbr2tHq/+TSRqyV8uRaVMRcxrAT99TxUu077T4JtlWrreXeZxJOPF93cv 1h8yzTjpdh4UTsNoJ/kYdGY/nWaUziDmcsNtinaDLLQ5PztvF5ugxdlB6fvDH5pz62PQ JQY4jjazF0AQjk3CTJGoc0VVsLQ2vYKkezM6GyNwSc4OezS6PvCVSZWNT4sMcyT1L9mq 9JJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733217323; x=1733822123; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p5yKP4CFUBPvLxsjJKQyHRBrFywG4zREYXhs05T127s=; b=EeI8oKTSYiWGRBO6Nrn6v3oiM1Vf1+oH7ghdPkp4YmDltZnwmeaNYs/txvpd0T4dJM oTcnsx/ZlqGkDiuhXrFn2IwRdtsm7xCUQ6Fy+kY9BGydsziAJTyEN7fwCS4jSaZ6Od07 rcLP0UkABAO+A6TcGPEAVueo5ekbal+y6j/3SvFAXVF0+M3OpaPvp0K8nzmSK5Y3x2DH iKqbDJd6p9qAkGWfN5ZpmRDW7+g9v8YFsT1RLSBxbUNyQQ1/IvnNrgD3BZXo6I7J4DuH F7Mv0k7T1jUo2p+oS2qPWExo1kzXoGaOMJ4eAZtQNks/fk4sXBX9z+moM73UkVukQe4O va2A== X-Forwarded-Encrypted: i=1; AJvYcCWQ8WhnTYBEq/rpU65pW6TDjus62pO6mpSdaUMDvSPLKlW+eBWKjfOD/hdMuqE3KaMkbS0zVWacyO8TGuq6MuKM@lists.postgresql.org X-Gm-Message-State: AOJu0YxqhfELsonk1WJa4FNefNaBulyuRFe7nZQj8jesxpI23uvmEX6t mygtIVjGlN+cq0kns9kHrLXU8m+AxACXqBFBvTxsmD65M5pOeS5c X-Gm-Gg: ASbGncuqELENlYtJ4jAmgAxd4IGQ4ok5QZ2Sgvk5dMzAIPQbTt7WyHvDHY+TVYSEO31 6rGeu+J6PZLilfm/+App4MRB9bJY7qNQUJvookuGcFVWZu3EEiXzggDK8PGmWmpgkhcNqmdphxI ilGzd6g6Ek1spXi70OwPFfcMp+RM9HKbb18OxkgafDZpbKfho4sT/X5wgVCoc3c0+K3jwl/UsJY mjuF5oPMiEehXkeP/6K+q2Fmlt26l84f9spunO0VfzAyKlRMzHEhuna5CQSb2aRJ11WXKYEswxw 9XEEu9AKd1h1nlFfaWluHfTibA== X-Google-Smtp-Source: AGHT+IH1ZejqCI3/H7zcY6uxNWvGyuh78qmglZHSF7RjwrOd8fdnXOLfYNKqMxB7JwOv1d7+ZndJ1A== X-Received: by 2002:a17:902:ea05:b0:215:b058:28a5 with SMTP id d9443c01a7336-215bd1ce4eamr22730455ad.18.1733217323147; Tue, 03 Dec 2024 01:15:23 -0800 (PST) Received: from [10.10.20.106] (node-omi.pool-125-26.dynamic.totinternet.net. [125.26.124.170]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21527a5306fsm87786485ad.109.2024.12.03.01.15.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 03 Dec 2024 01:15:22 -0800 (PST) Message-ID: <7d8175ca-82bb-4878-8b25-7edb124fac21@gmail.com> Date: Tue, 3 Dec 2024 16:15:20 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: pgsql: Avoid mislabeling of lateral references when pulling up a subque To: Tom Lane , pgsql-committers@lists.postgresql.org References: Content-Language: en-AU From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/29/24 05:33, Tom Lane wrote: > Avoid mislabeling of lateral references when pulling up a subquery. > > If we are pulling up a subquery that's under an outer join, and > the subquery's target list contains a strict expression that uses > both a subquery variable and a lateral-reference variable, it's okay > to pull up the expression without wrapping it in a PlaceHolderVar. > That's safe because if the subquery variable is forced to NULL > by the outer join, the expression result will come out as NULL too, > so we don't have to force that outcome by evaluating the expression > below the outer join. It'd be correct to wrap in a PHV, but that can > lead to very significantly worse plans, since we'd then have to use > a nestloop plan to pass down the lateral reference to where the > expression will be evaluated. Pardon the noise, but I'm curious why the optimiser must choose NestLoop in the case of lateral reference. It would be nice to provide alternatives. Because now we have some corner cases. For example, with pull-up correlated subqueries, we've got one degraded case. Look the following: DROP TABLE IF EXISTS t1,t2; CREATE TABLE t1(x int, y int); CREATE TABLE t2(x int, y int); INSERT INTO t1 (x,y) SELECT gs,-gs FROM generate_series(1,1E4) AS gs; ANALYZE t1,t2; EXPLAIN (ANALYZE, COSTS ON) SELECT t1.* FROM t1 LEFT JOIN LATERAL ( SELECT t3.* FROM t1 t3 WHERE t3.x=t1.x) AS t4 ON (t4.y IN (SELECT y FROM t2 WHERE t4.x=t2.x)); In previous versions Postgres executed this plan in milliseconds: Hash Left Join Hash Cond: (t1.x = t3.x) -> Seq Scan on t1 -> Hash -> Seq Scan on t1 t3 Filter: (ANY (y = (SubPlan 1).col1)) SubPlan 1 -> Seq Scan on t2 Filter: (t3.x = x) Planning Time: 0.175 ms Execution Time: 6.396 ms But now we have seconds: Nested Loop Left Join -> Seq Scan on t1 -> Nested Loop Semi Join Join Filter: ((t3.x = t2.x) AND (t3.y = t2.y)) -> Seq Scan on t1 t3 Filter: (x = t1.x) -> Seq Scan on t2 Planning Time: 1.309 ms Execution Time: 6780.217 ms Correlated subquery pull-up is a nice optimisation, of course. So, why not let optimiser try a HashJoin like that (not a really generated plan, just my imagination): Hash Left Join Hash Cond: (t1.x = t3.x) -> Seq Scan on t1 Hash -> Nested Loop Semi Join Join Filter: ((t3.x = t2.x) AND (t3.y = t2.y)) -> Seq Scan on t1 t3 -> Seq Scan on t2 Does the optimiser have some internal limits to let such a path? -- regards, Andrei Lepikhov