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 1pID48-0007yS-SV for pgsql-general@arkaria.postgresql.org; Wed, 18 Jan 2023 18:21:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pID46-0004FH-OX for pgsql-general@arkaria.postgresql.org; Wed, 18 Jan 2023 18:21:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pID46-0004F8-E8 for pgsql-general@lists.postgresql.org; Wed, 18 Jan 2023 18:21:38 +0000 Received: from mout-u-107.mailbox.org ([2001:67c:2050:101:465::107]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pID43-0005g5-8H for pgsql-general@lists.postgresql.org; Wed, 18 Jan 2023 18:21:38 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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 4NxvGd6Y1dz9sTR; Wed, 18 Jan 2023 19:21:29 +0100 (CET) Date: Wed, 18 Jan 2023 19:21:29 +0100 (CET) From: Erik Wienhold To: "Dirschel, Steve" , "pgsql-general@lists.postgresql.org" Message-ID: <1307202419.41046.1674066089443@office.mailbox.org> In-Reply-To: References: 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: quoted-printable X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4NxvGd6Y1dz9sTR List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 18/01/2023 18:03 CET Dirschel, Steve wrote: > > The plan changes: > > Sort (cost=3D9382.94..9382.97 rows=3D12 width=3D169) > Sort Key: lmq1.priority DESC, lmq1.request_time > -> Bitmap Heap Scan on lm_queue lmq1 (cost=3D4572.59..9382.73 rows=3D12 w= idth=3D169) > Recheck Cond: ((client_name)::text =3D 'WLCASES'::text) > Filter: ((NOT (alternatives: SubPlan 1 or hashed SubPlan 2)) OR (((proces= s_type)::text =3D ANY ('{UNLOCK,"UNLOCK RERUN"}'::text[])) AND (lm_id =3D '= 0'::numeric) AND ((host_name)::text =3D 'WLLOADB'::text) AND (hold_flag =3D= 'Y'::bpchar) AND ((host_name)::text =3D 'WLLOADB'::text) AND (status_code = =3D '1'::numeric))) > -> Bitmap Index Scan on ix_lm_chl (cost=3D0.00..4572.58 rows=3D25 width= =3D0) > Index Cond: ((client_name)::text =3D 'WLCASES'::text) > SubPlan 1 > -> Bitmap Heap Scan on lm_queue lmq2 (cost=3D164.44..188.42 rows=3D1 widt= h=3D0) > Recheck Cond: ((lmq1.collection_name)::text =3D (collection_name)::text) > Filter: ((lm_id <> '0'::numeric) AND ((process_type)::text <> 'EXTRACT'::= text)) > -> Bitmap Index Scan on ix_lm_cc (cost=3D0.00..164.44 rows=3D6 width=3D0) > Index Cond: ((collection_name)::text =3D (lmq1.collection_name)::text) > SubPlan 2 > -> Seq Scan on lm_queue lmq2_1 (cost=3D0.00..124999.49 rows=3D25 width=3D= 32) > Filter: ((lm_id <> '0'::numeric) AND ((process_type)::text <> 'EXTRACT'::= text)) > > In the original plan above I believe the query drives off index ix_lm_chl > applying both client_name =3D WLCASES and host_name =3D WLLOADB to the in= dex cond. > In the plan directly above I believe it also drives off index ix_lm_chl b= ut it > is only applying client_name =3D WLCASES to the index cond. > > If AND=E2=80=99s are applied first then why wouldn=E2=80=99t the modified= query apply both > client_name =3D WLCASES and host_name =3D WLLOADB to the index cond? Is i= t because > those are moved below the OR condition? Yes because those two conditions are combined with OR and that's handled by= the bitmap scans in your second execution plan. See also: https://www.postgresql.org/docs/14/indexes-bitmap-scans.html -- Erik