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 1tR9qd-007fZz-SM for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Dec 2024 12:53:48 +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 1tR9qd-009x6N-1g for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Dec 2024 12:53:46 +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 1tR9qc-009x6E-MX for pgsql-hackers@lists.postgresql.org; Fri, 27 Dec 2024 12:53:46 +0000 Received: from forward502a.mail.yandex.net ([178.154.239.82]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tR9qW-001s9U-7T for pgsql-hackers@lists.postgresql.org; Fri, 27 Dec 2024 12:53:45 +0000 Received: from mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net [IPv6:2a02:6b8:c0f:171e:0:640:d7a4:0]) by forward502a.mail.yandex.net (Yandex) with ESMTPS id 4203061D98; Fri, 27 Dec 2024 15:53:39 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id ZrZP902OimI0-Ro3LpKWo; Fri, 27 Dec 2024 15:53:38 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1735304018; bh=b7D0dkAxbZjMCBMwMeYXIICVaWcs+vAYKmdSWnl9ssI=; h=From:In-Reply-To:Cc:Date:References:To:Subject:Message-ID; b=jvzs27ugloA0sXNaj1uVHhZlMsPi8jWt6PPhYQSiaiMyKWs8f9QeSYzZ/InYQaCOi 7vsa1EjKFe7gC9i6HDDWsVyzHV5DRd9gLjzi3GVjCPu6RyTGcUVKP0T80mCqbL7Vgt s0qNbrtmg10BSIqYd1Se2KRFIukCGXIjHDSzEYZo= Authentication-Results: mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Message-ID: <9b041978-06e3-4a50-8a5d-dacbb054f23e@tantorlabs.com> Date: Fri, 27 Dec 2024 15:53:35 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Exists pull-up application with JoinExpr To: Alena Rybakina Cc: PostgreSQL Hackers , Ranier Vilela References: <0b1f670d-b39d-4966-bf32-f0d502ebc564@postgrespro.ru> <46bc4eaf-58c5-42ab-8041-d3380a0768de@postgrespro.ru> Content-Language: en-US From: Ilia Evdokimov In-Reply-To: <46bc4eaf-58c5-42ab-8041-d3380a0768de@postgrespro.ru> 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 Hi Alena, Thank you for your work on subqueries with JOIN. Have you considered the scenario where in subquery includes a qual like (tc.aid = 1)? When I tried executing those queries I receive different results. In my opinion, to prevent this, we should add filters for such quals within the loop 'foreach (lc, all_clauses)' EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) SELECT * FROM ta WHERE EXISTS (SELECT * FROM tb, tc WHERE ta.id = tb.id AND tc.aid = 1);                               QUERY PLAN ----------------------------------------------------------------------  Hash Join (actual rows=1 loops=1)    Hash Cond: (ta.id = tb.id)    Buffers: local hit=3    ->  Seq Scan on ta (actual rows=3 loops=1)          Buffers: local hit=1    ->  Hash (actual rows=3 loops=1)          Buckets: 4096  Batches: 1  Memory Usage: 33kB          Buffers: local hit=2          ->  HashAggregate (actual rows=3 loops=1)                Group Key: tb.id                Batches: 1  Memory Usage: 121kB                Buffers: local hit=2                ->  Nested Loop (actual rows=3 loops=1)                      Buffers: local hit=2                      ->  Seq Scan on tb (actual rows=3 loops=1)                            Buffers: local hit=1                      ->  Materialize (actual rows=1 loops=3)                            Storage: Memory  Maximum Storage: 17kB                            Buffers: local hit=1                            ->  Seq Scan on tc (actual rows=1 loops=1)                                  Filter: (aid = 1)                                  Rows Removed by Filter: 1                                  Buffers: local hit=1 (23 rows) ============================ EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) SELECT * FROM ta WHERE EXISTS (SELECT * FROM tb JOIN tc ON ta.id = tb.id WHERE tc.aid = 1);                                 QUERY PLAN ---------------------------------------------------------------------------  Seq Scan on ta (actual rows=1 loops=1)    Filter: EXISTS(SubPlan 1)    Rows Removed by Filter: 2    Buffers: local hit=6    SubPlan 1      ->  Nested Loop (actual rows=0 loops=3)            Buffers: local hit=5            ->  Index Only Scan using tb_pkey on tb (actual rows=0 loops=3)                  Index Cond: (id = ta.id)                  Heap Fetches: 1                  Buffers: local hit=4            ->  Seq Scan on tc (actual rows=1 loops=1)                  Filter: (aid = 1)                  Buffers: local hit=1 (14 rows) -- Best regards, Ilia Evdokimov, Tantor Labs LLC.