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.96) (envelope-from ) id 1wLJa5-001hJI-0K for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2026 11:41:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wLJa2-009Eth-2H for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2026 11:41:18 +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.96) (envelope-from ) id 1wLJa2-009EtY-1C for pgsql-hackers@lists.postgresql.org; Fri, 08 May 2026 11:41:18 +0000 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wLJZv-00000001De0-0X25 for pgsql-hackers@lists.postgresql.org; Fri, 08 May 2026 11:41:17 +0000 Received: by mail-qk1-x743.google.com with SMTP id af79cd13be357-8eb22d4de2fso38797485a.2 for ; Fri, 08 May 2026 04:41:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778240469; cv=none; d=google.com; s=arc-20240605; b=cNggYSWG6iaaPwWOupFwGLtIGiKKVWkjnAZx+VnxqBB3/8nsstOThet7TwMbfQky4u iWmKEiv8ebx7wzbCYg94FUuqBghJoqF2slsfe2bUk+Sg8BJMw5TEyWVTyWOEYrNaeWje SCPRWuYsW3HLMhd7PMF3xHRuTrnr7ZN47lTifHprl0kpgeB/N01hWhK1oidZ+0LcqwRZ gJbW9WmtGaT25MMyY3NHLQwpwGOFw8W/gPivDAHXMM1QX0yqenyCT2XXFyYZu5ijLr3m j/zvnE2ql8IsTzgsPyZo+a6NOwt4Hid3Z7NKy3GMsPODpJog5v8CyrYfHXQVlORod7aE BkFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=yVa7HqoGxMFDlSuHABaZzIrUN41sDp9MosyiwSTdCZA=; fh=SBZAERjrZuOUxHtECFpqDj03GAdIuUJH4qm6jIvIACc=; b=fpYuEvXY/dFO60GZsnGJIGgkEl2dr5YXdCIiXIHNuncMYzfN7ybP/ekg0b10Tyzmph 42xy3bEojAxYVgmDK6joLvVhwvcBVz1HwI8i/tdQ/CUNqdT6qXzE65SUOYSbysg2Wm1z qy3vdMpWcvdz9IUxOVHEwcESWNlZKyrYY/zSBhX7UWBlkGKS0lTSIJctHEDjcPfJx0tD Ty4sGjPU/gn3GwnFrEZDxwxZ/yAe0h4LLnnMpxr44ipbeRYkG2Ve8K6U3TFK9QBHqyBS jDiR90hJ9f8J19xRW2mKGLxsRXT+/cFPHbXmo7HDq9HnsJ4z+O4zZWRXJF1d7cQKRivN g4VA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778240469; x=1778845269; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=yVa7HqoGxMFDlSuHABaZzIrUN41sDp9MosyiwSTdCZA=; b=CQC8hfqP5HDRNNtV79fUAaZ+vlnlKpim2M02KHSuMtnxmlWDYUwJY/I+yoNk5RUTIF N568RCpFLHa+q212dfpO20NUQ7GJb9bLrMmSgjwdP6+48AIfQHawQgoR/QR2DAw7prp9 3rP8wRUdbl41eorVB+Y5JlDAmj2Q6gVw6Vp45drkpbk2BCnx2Foi5z+VyfI8WDGWAYO7 or23jkRWtAl/4NnlLgnsIvICILS/i2Cr3cqa/AxVhW6SSz5gp47Z/5isQzFCK+CsvGMY BCbPt/kF7dy05U9zSBXZXXbC0gqcxVb89/3OFIs2vWdrCF77wEttFNIVkxfMQ2zsgntI 06pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778240469; x=1778845269; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yVa7HqoGxMFDlSuHABaZzIrUN41sDp9MosyiwSTdCZA=; b=DLF3aERooGKRigO3qfP7jwSWTClkaOG2FC9WE4G0jPXxDcx06hiG3w+3GOPMOo2ntw /f2RQxlqnjh1f3n3wGLc8oRM4Wc4ClGpi+dg1+blFDpMqT8jSkQ2sM7iRin6Wp/yBbJF +0mCJGuYQ58aRL7pYCQF3qlQwgFXmoXc4AhENhXH8vjcDHTNwDGkHKpxWIB3xHuy+nFc qvXyQ/+Ldyqydhe1RQC2BYJ/4/pneF2wLOlDzSjHwOlyz8D3Lk9MXQLeDjwcF1VYUYwh s8ql2uQHyCENW4mKwG7xhPMASKRoqyYdqJgGxBy7NgPlDqyoBLnPRcPITdd7iFYEVcUd /aZw== X-Forwarded-Encrypted: i=1; AFNElJ9yyOcGPlHI7YSKWG7dDfwqs2ZzLtg3ItBNMMp2PxE+nhzyvte2UTWsxin58gIYcj9hfkuSkevFmhRvYiSH@lists.postgresql.org X-Gm-Message-State: AOJu0YxRocStVFzOj/eyNNGe83DGxoVOs73bf0snfxpwguGDS/b8KcdC y/dMbOOXK+D3HhVeWXz6i3tl9HYBiXMZEiWuXyCG/mcBnkZ3JrCqo5Tfmlab+U6vcT4COQs1VMv rdT+mmWxjNxGhA7aeD7QAZbQ/8lKSeV8= X-Gm-Gg: Acq92OG1eufvu2+muNtWjuujTSpNLcTiKDphy4INBpQsFwHs6EKk+bLKdZ3hEwr5/FU l0nDMi3nlPTgkiOSK9J5CnsQnCGr8/7kvbC+lVeWx8u2kNsPweqiXqyyyjrR7FjA0UDdTh8kES9 /eiMggrgu6hdaOqxfeoWck2Isih6ZPhOehlf8pbxmiYx1MMxSdDqAMf20nMFIw4P0a/jjXvbUeH k/viMmF5sgyUM/eVQArMPyh8pzKTIvD9rEKKvWV1jlzXMZtMx3U/hQ8gkuaRS8Ix+AMWXsigdaG 3pk3qVE= X-Received: by 2002:a05:6214:19c5:b0:8ac:a205:f118 with SMTP id 6a1803df08f44-8bc462028c9mr131371236d6.8.1778240469140; Fri, 08 May 2026 04:41:09 -0700 (PDT) MIME-Version: 1.0 References: <0b1f670d-b39d-4966-bf32-f0d502ebc564@postgrespro.ru> <46bc4eaf-58c5-42ab-8041-d3380a0768de@postgrespro.ru> <9b041978-06e3-4a50-8a5d-dacbb054f23e@tantorlabs.com> <8d7cbbf6-ddc0-4696-8af2-a68d740e14f2@postgrespro.ru> <6078f7e1-4b29-4b31-bd28-b84149e404e4@postgrespro.ru> <975a3736-a8b5-49b3-8009-4d4e86867aa1@postgrespro.ru> <0adbf75a-2059-4bb7-b878-c33892dbc1f0@tantorlabs.com> <300d7bb0-345c-4c0b-a0ef-4de573fcc94b@postgrespro.ru> <32446f52-1b18-499a-a77b-d6b66ad0f4cf@tantorlabs.com> <87a6fcbf-929a-408a-9471-28be3080254a@postgrespro.ru> <4e9d279c-83c1-42d3-b74d-7366ecd7f085@tantorlabs.com> <386165c3-3c01-4a85-b618-7919e64a52de@postgrespro.ru> In-Reply-To: From: solaimurugan vellaipandiyan Date: Thu, 26 Jun 2025 21:10:53 +0530 X-Gm-Features: AVHnY4IQghJjQ0DMG86QjH1dU-FEltxxAMU7SLjw3h1CWsGCStueb0_eaw8MbUI Message-ID: Subject: Re: pull-up subquery if JOIN-ON contains refs to upper-query To: Alena Rybakina Cc: Ilia Evdokimov , David Rowley , Ranier Vilela , PostgreSQL Hackers , Peter Petrov Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Alena, I tested the latest patch on current master and verified the planner behavior for the EXISTS pull-up cases described in the thread. Before applying the patch, the following query produced an EXISTS(SubPlan) plan: EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) SELECT * FROM ta WHERE EXISTS ( SELECT * FROM tb JOIN tc ON ta.id = tb.id ); Plan before patch: Seq Scan on ta Filter: EXISTS(SubPlan exists_1) After applying the patch, the same query is transformed into: Nested Loop Semi Join with Index Only Scan on tb_pkey, so the pull-up optimization is now applied correctly for JOIN ON clauses referencing outer Vars. I also tested some additional cases mentioned in the discussion: 1. NOT EXISTS case planner generated Nested Loop Anti Join as expected. 2 . Constant qual case (WHERE tc.aid = 1) planner produced an optimized Hash Join plan and avoided EXISTS(SubPlan). 3. ANY array condition tb.id = ANY('{1}'::int[]) planner successfully used Index Only Scan and generated an optimized join plan. In all tested cases, query results remained correct and I did not observe incorrect transformations during my testing. Overall the patch behavior looks good from my side and the planner now behaves consistently for these EXISTS pull-up scenarios. Regards, Solaimurugan V