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 1u018L-00A740-81 for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Apr 2025 16:40:09 +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 1u018I-00Fu1N-VL for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Apr 2025 16:40:06 +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 1u018I-00Fu1D-El for pgsql-hackers@lists.postgresql.org; Wed, 02 Apr 2025 16:40:06 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u018C-003061-1A for pgsql-hackers@lists.postgresql.org; Wed, 02 Apr 2025 16:40:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1743611998; bh=Hm3lO2S/Rpph50kQXG0gOZNrmPDG0WM+0uWYAa7R6kw=; h=Message-ID:Date:User-Agent:Subject:From:To:Cc:References: In-Reply-To:From; b=PEBWoFg0y8TOJar/3NWFzzDgm4Xk9LUqKpe1CZ56QAApKw2dCcJjgy53Ci90+QdgX bbd4VEjmvgO0XYmdsByZBZY2i1WDOZh+kd4w7FNt7/BOqSBghF309HW9/A6Y89dcG5 GULQIdi1JI1fuaDfxgjh3rIidkwKxlBkCR47JuoqKsBRl248/+Vfx4oAaKHfZuaQf+ bke7OjwfZeF9/BQjXF6DCh335ZB0Jyj4dkFRHcQVQyaHUZo9i0Y5ms2IxHTn/jID1U Sva51A0ddbGfpfobql2V2XOEqATmEkNwRFp84sh0I6O28DKVx2nBqXsMm408nXm80F 9BPBlrbrCDRhQ== Received: from [10.4.12.74] (unknown [93.174.131.141]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.rybakina@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 932776070C; Wed, 2 Apr 2025 19:39:58 +0300 (MSK) Message-ID: Date: Wed, 2 Apr 2025 19:39:58 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: pull-up subquery if JOIN-ON contains refs to upper-query From: Alena Rybakina To: PostgreSQL Hackers , Peter Petrov , Ilia Evdokimov Cc: Ranier Vilela 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> Content-Language: en-US In-Reply-To: <300d7bb0-345c-4c0b-a0ef-4de573fcc94b@postgrespro.ru> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/04/02 11:47:00 #27849864 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi! My colleague reviewed my patch and gave feedback on how to improve it - for some queries with data types that I did not consider, pull-up is not applied, although it should. Some of them: EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT 1   FROM ta  WHERE EXISTS (SELECT 1                  FROM tb                  JOIN tc                    ON ta.id = tb.id                   AND tb.id = ANY('{1}'::int[])               );                                 QUERY PLAN --------------------------------------------------------------------------  Seq Scan on ta (actual rows=1.00 loops=1)    Filter: EXISTS(SubPlan 1)    Rows Removed by Filter: 1    SubPlan 1      ->  Nested Loop (actual rows=0.50 loops=2)            ->  Seq Scan on tb (actual rows=0.50 loops=2)                  Filter: ((id = ANY ('{1}'::integer[])) AND (ta.id = id))                  Rows Removed by Filter: 2            ->  Seq Scan on tc (actual rows=1.00 loops=1) EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)  SELECT 1    FROM ta   WHERE EXISTS (SELECT 1                   FROM tb                   JOIN tc                     ON ta.id = tb.id                    AND tb.is_active                );                        QUERY PLAN ---------------------------------------------------------  Seq Scan on ta (actual rows=2.00 loops=1)    Filter: EXISTS(SubPlan 1)    SubPlan 1      ->  Nested Loop (actual rows=1.00 loops=2)            ->  Seq Scan on tb (actual rows=1.00 loops=2)                  Filter: (is_active AND (ta.id = id))                  Rows Removed by Filter: 0            ->  Seq Scan on tc (actual rows=1.00 loops=2) EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)  SELECT 1    FROM ta   WHERE EXISTS (SELECT 1                   FROM tb                   JOIN tc                     ON ta.id = tb.id                    AND tb.is_active IS NOT NULL                );                                    QUERY PLAN --------------------------------------------------------------------  Seq Scan on ta (actual rows=2.00 loops=1)    Filter: EXISTS(SubPlan 1)     SubPlan 1        ->  Nested Loop (actual rows=1.00 loops=2)              ->  Seq Scan on tb (actual rows=1.00 loops=2)                    Filter: ((is_active IS NOT NULL) AND (ta.id = id))                    Rows Removed by Filter: 0              ->  Seq Scan on tc (actual rows=1.00 loops=2) UPDATE tb SET is_active = NULL WHERE id = 2; EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)  SELECT 1    FROM ta   WHERE EXISTS (SELECT 1                   FROM tb                   JOIN tc                     ON ta.id = tb.id                    AND tb.is_active IS NULL                );                               QUERY PLAN ----------------------------------------------------------------  Seq Scan on ta (actual rows=1.00 loops=1)    Filter: EXISTS(SubPlan 1)    Rows Removed by Filter: 1    SubPlan 1      ->  Nested Loop (actual rows=0.50 loops=2)            ->  Seq Scan on tb (actual rows=0.50 loops=2)                  Filter: ((is_active IS NULL) AND (ta.id = id))                  Rows Removed by Filter: 4            ->  Seq Scan on tc (actual rows=1.00 loops=1) I see that I need to add a walker that, when traversing the tree, determines whether there are conditions under which pull-up is impossible - the presence of volatility of functions and other restrictions, and leave the transformation for the var objects that I added before, I described it here. Unfortunately, I need a few days to implement this and need time for a review, and I think I will not have time to do this before the code freeze, so I am moving this to the next commitfest and not changing the status "awaiting the author". On 11.02.2025 18:59, Alena Rybakina wrote: > On 10.02.2025 23:51, Ilia Evdokimov wrote: >> >> On 09.02.2025 18:14, Alena Rybakina wrote: >>> Hi! I found another example where the transformation worked >>> incorrectly and reconsidered the idea. >>> >>> As for conversion of exists_sublink_to_ANY, we need to get the >>> flattened implicit-AND list of clauses and pull out the chunks of >>> the WHERE clause that belong to the parent query, >>> since we are called halfway through the parent's >>> preprocess_expression() and earlier steps of preprocess_expression() >>> wouldn't get applied to the pulled-up stuff unless we do them here. >>> We also do some processing for vars depending on which side the var >>> is on - if it's in a subquery, we only need to lower its level >>> (varlevel) because subquery will be flatted, while >>> for other vars that belong to the parent query, we need to do >>> preparation to pull up the sub-select into top range table. >>> >>> For those expressions that we couldn't assign to either list, we >>> define newWhere and apply both cases. >>> >> >> When I run 'make -C contrib/ check', tests of postgres_fdw extension >> failed. I might be wrong, but you should be careful with LIMIT. >> > Thank you for the review, I'm working on it. > Sorry for not responding, but I will fix this bug after I update the code based on the comments above. Thank you for noticing and writing to me, your feedback is very important. -- Regards, Alena Rybakina Postgres Professional