Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pICxE-0007RM-U0 for pgsql-general@arkaria.postgresql.org; Wed, 18 Jan 2023 18:14:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pICxD-00053S-J7 for pgsql-general@arkaria.postgresql.org; Wed, 18 Jan 2023 18:14:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pICxD-00053J-94 for pgsql-general@lists.postgresql.org; Wed, 18 Jan 2023 18:14:31 +0000 Received: from mout-u-107.mailbox.org ([2001:67c:2050:101:465::107]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pICx6-0001VT-NV for pgsql-general@lists.postgresql.org; Wed, 18 Jan 2023 18:14:30 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4Nxv6F1g9bz9sPy; Wed, 18 Jan 2023 19:14:13 +0100 (CET) Date: Wed, 18 Jan 2023 19:14:12 +0100 (CET) From: Erik Wienhold To: Tom Lane , "Dirschel, Steve" Cc: "pgsql-general@lists.postgresql.org" Message-ID: <521374201.40360.1674065652451@office.mailbox.org> In-Reply-To: <3635301.1674064606@sss.pgh.pa.us> References: <3635301.1674064606@sss.pgh.pa.us> Subject: Re: Interpreting postgres execution plan along with AND/OR precedence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4Nxv6F1g9bz9sPy List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 18/01/2023 18:56 CET Tom Lane wrote: > > "Dirschel, Steve" writes: > > Filter: ... (alternatives: SubPlan 1 or hashed SubPlan 2) ... > > SubPlan 1 > > -> Index Scan using ix_lm_cc on lm_queue lmq2 (cost=0.40..177.93 rows=1 width=0) > > Index Cond: ((collection_name)::text = (lmq1.collection_name)::text) > > Filter: ((lm_id <> '0'::numeric) AND ((process_type)::text <> 'EXTRACT'::text)) > > SubPlan 2 > > -> Seq Scan on lm_queue lmq2_1 (cost=0.00..124999.06 rows=12 width=32) > > Filter: ((lm_id <> '0'::numeric) AND ((process_type)::text <> 'EXTRACT'::text)) > > > I understand SubPlan 1 above- it is joining into the NOT EXISTS via the > > lmq1.COLLECTION_NAME = lmq2.COLLECTION_NAME and then applying the other > > filtering inside the NOT EXISTS. But I don't understand SubPlan 2. > > Given the filter conditions under SubPlan 2 it is also coming from the > > NOT EXISTS because that is where LM_ID <> 0 and PROCESS_TYPE <> EXTRACT > > exist but I don't understand the scenario where this query would need to > > use SubPlan 2. Would anyone be able to explain under what condition(s) > > SubPlan 2 would get executed? > > The key is the "alternatives:" notation. SubPlan 1 can be used in a > "retail" fashion by invoking it once per outer row, passing a new > value of lmq1.collection_name each time. SubPlan 2 is meant to be > invoked just once, and its output (ie, all the relevant values of > lmq2.collection_name) will be loaded into an in-memory hash table > which is then probed for each outer row. At the point where these > subplans are made, we don't have enough information about how many > outer rows there will be to decide which way is better, so we create > both subplans and postpone the decision till execution. That's all > just related to the EXISTS clause, though. > > (Since v14 we don't do it like that anymore, so that this confusing > EXPLAIN notation is gone.) EXPLAIN ANALYZE shows which subplan gets executed. Look for "never executed". -- Erik