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 1rBMJ0-00AY6Z-BW for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Dec 2023 21:53:14 +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 1rBMIy-002kN0-7X for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Dec 2023 21:53:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBMIx-002kMr-Vf for pgsql-bugs@lists.postgresql.org; Thu, 07 Dec 2023 21:53:11 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBMIv-009JKf-5L for pgsql-bugs@lists.postgresql.org; Thu, 07 Dec 2023 21:53:10 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 3B7Lr7wo1320818; Thu, 7 Dec 2023 16:53:07 -0500 From: Tom Lane To: dastapov@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18234: Nested Loop joint strategy is ignored for a tiny table joined with UNION ALL of two filtered parts In-reply-to: <18234-571586e70bd0658d@postgresql.org> References: <18234-571586e70bd0658d@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 07 Dec 2023 18:50:01 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1320816.1701985987.1@sss.pgh.pa.us> Date: Thu, 07 Dec 2023 16:53:07 -0500 Message-ID: <1320817.1701985987@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > Summary of the issue: for a (5-row recordset) JOIN (massive partitioned > recordset indexed by id) USING (id), the (Nested Loop over 5 values) > strategy is completely ignored, and Hash Join or Merge Join is done instead, > which does SeqScan over the "massive recordset". > Reproduction in DB Fiddle: > https://www.db-fiddle.com/f/sJUUWNgW7pqPWcJwihVoj5/1 (this demonstrates both > the bad behaviour and a way to work around it) We are generally not too happy with non-self-contained bug reports. Once that DB Fiddle entry disappears, this bug report will be useless. However ... > 1)The massive recordset on the right side of the JOIN must come from the > UNION ALL of two parts, both of which have a filter, like this view in my > reproduction: > create view vw_broken as > select id from huge where filter_out > union all > select id from medium where filter_out; I suspect the WHERE clauses trigger the problem because the resulting sub-selects can't be pulled up to become an "appendrel", per is_safe_append_member: * It's only safe to pull up the child if its jointree contains exactly * one RTE, else the AppendRelInfo data structure breaks. The one base RTE * could be buried in several levels of FromExpr, however. Also, if the * child's jointree is completely empty, we can pull up because * pull_up_simple_subquery will insert a single RTE_RESULT RTE instead. * * Also, the child can't have any WHERE quals because there's no place to * put them in an appendrel. (This is a bit annoying...) That means the sub-selects will be planned independently and there's no chance to consider the nestloop-with-inner-indexscan plan you are hoping for. This is a longstanding wart, but improving matters would require some fairly painstaking work. The "appendrel" mechanism is core to both traditional inheritance and partitioning; I don't recommend trying to blow it up and start over. I vaguely recall previous discussions that identified some semantic issues with trying to just attach WHERE clauses to appendrel members, but it was a long time ago and the details escape me. regards, tom lane