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 1jmIh8-0004Sm-8S for pgsql-performance@arkaria.postgresql.org; Fri, 19 Jun 2020 15:12:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jmIh7-00067O-3k for pgsql-performance@arkaria.postgresql.org; Fri, 19 Jun 2020 15:12:41 +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 1jmIh6-00067H-Rj for pgsql-performance@lists.postgresql.org; Fri, 19 Jun 2020 15:12:40 +0000 Received: from mx.zeyos.com ([88.99.153.70]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jmIh0-0005QX-7K for pgsql-performance@postgresql.org; Fri, 19 Jun 2020 15:12:40 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 7F3285FB26 for ; Fri, 19 Jun 2020 17:12:32 +0200 (CEST) Authentication-Results: mx.zeyos.com (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=zeyos.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zeyos.com; h= content-transfer-encoding:content-type:content-type:mime-version :to:from:from:subject:subject:date:date; s=dkim; t=1592579552; x=1593443553; bh=Obiy5HKK1GSyV7OgkvsMuALAlgXMAsPQOt4jeGYyBoQ=; b= dMJUSlMvbcQhuWsvf8mxx74NV5TVJp7gK2CWODyec9GmVqS4b8WkPypaaqZZRY3A yD/PsK2CPw0JXY+WcDZXmog8YzJXksrbRhbdJNBeYWi7wu4dOI9k15sGV62X/drp BA/63WhdPUm6ULGf+dAFxVMipl2XEvpOYqmaqmHfYHw= X-Virus-Scanned: Debian amavisd-new at mx.zeyos.com Received: from mx.zeyos.com ([127.0.0.1]) by mx.zeyos.com (mx.zeyos.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 69eocfV9HSHU for ; Fri, 19 Jun 2020 17:12:32 +0200 (CEST) Received: from cloud.zeyos.com (unknown [81.171.8.203]) by mx.zeyos.com (Postfix) with ESMTPSA id 553115FB22 for ; Fri, 19 Jun 2020 17:12:31 +0200 (CEST) Date: Fri, 19 Jun 2020 17:12:31 +0200 Subject: Unclamped row estimates whith OR-ed subplans From: "Benjamin Coutu" To: pgsql-performance@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20200619151232.7F3285FB26@mx.zeyos.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello,=0A=0Aplease consider the following SQL query:=0A=0ASELECT * FROM "tr= ansactions" WHERE =0A=09"account" IN (SELECT "ID" FROM "accounts" WHERE "na= me" ~~* '%test%') OR=0A=09"contract" IN (SELECT "ID" FROM "contracts" WHERE= "name" ~~* '%test%')=0A=0AThis yields the following plan on Postgres 11:= =0A=0ASeq Scan on transactions (cost=3D67.21..171458.03 rows=3D1301316 wid= th=3D1206)=0A Filter: ((hashed SubPlan 1) OR (hashed SubPlan 2))=0A SubPl= an 1=0A -> Bitmap Heap Scan on accounts (cost=3D33.36..61.16 rows=3D46= width=3D4)=0A Recheck Cond: ((name)::text ~~* '%test%'::text)=0A = -> Bitmap Index Scan on s_accounts (cost=3D0.00..33.35 rows=3D46= width=3D0)=0A Index Cond: ((name)::text ~~* '%test%'::text)= =0A SubPlan 2=0A -> Seq Scan on contracts (cost=3D0.00..5.93 rows=3D5= width=3D4)=0A Filter: ((name)::text ~~* '%test%'::text)=0A=0ASo t= he where clause of this query has just two subplans OR-ed together, one is = estimated to yield 46 rows and one is estimated to yield 5 rows.=0AI'd expe= ct the total rows for the seqscan to be estimated at 46 then, following the= logic that rows_seqscan =3D max(rows_subplan1, rows_subplan2). As you can = see, the optimizer estimates a whopping 1301316 rows instead.=0A=0AI am abs= olutely aware that those are hashed sub plans below a seqscan and that Post= gres therefore has to scan all tuples of the table. But the problem is that= upper nodes (which are excluded from this example for simplicity) think th= ey will receive 1301316 rows from the seqscan, when in fact they will proba= bly only see a hand full, which the planner could have (easily?) deduced by= taking the greater of the two subplan row estimates.=0A=0AWhat am I missin= g, or is this perhaps a shortfall of the planner?=0A=0AThanks,=0A=0ABen=0A= =0A-- =0A=0ABejamin Coutu=0Aben.coutu@zeyos.com=0A=0AZeyOS GmbH & Co. KG=0A= http://www.zeyos.com