Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e8ony-0002QG-0Y for pgsql-performance@arkaria.postgresql.org; Sun, 29 Oct 2017 14:43:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e8onx-0004Ma-4k for pgsql-performance@arkaria.postgresql.org; Sun, 29 Oct 2017 14:43:13 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e8omE-0001JA-BH for pgsql-performance@postgresql.org; Sun, 29 Oct 2017 14:41:26 +0000 Received: from mx.zeyos.com ([88.99.153.70]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e8omB-0003H6-DZ for pgsql-performance@postgresql.org; Sun, 29 Oct 2017 14:41:25 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 7C7CE5FB08 for ; Sun, 29 Oct 2017 15:41:21 +0100 (CET) Authentication-Results: mx.zeyos.com (amavisd-new); dkim=pass 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:subject:subject:from:from:date:date; s=dkim; t=1509288081; x=1510152082; bh=HE0+08ApvTn9EArPHaD6D/gRFb7QYv9vh9Knn6RK1D4=; b= BdQffGQQDH2m7Q37WmJCQzABESC/vtW+K3SLhaTviYm9GP3meqBEfWio2p71RN6q LuUMbJRPtC7SNSo9uLQEX5q/i7X4KFkdTW1W0YhOQGLXpnYuGnHdwHmuTu+MFJ8+ c0GPsZVNDkahVvXZoTH4CrtTTzwjZBHx5098xmHWThI= 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 bqmU5uiVHYw0 for ; Sun, 29 Oct 2017 15:41:21 +0100 (CET) Received: from [81.171.8.203] (unknown [81.171.8.203]) by mx.zeyos.com (Postfix) with ESMTPSA id 390C85FAFE; Sun, 29 Oct 2017 15:41:20 +0100 (CET) Date: Sun, 29 Oct 2017 15:41:20 +0100 From: Benjamin Coutu Subject: Re: Cheaper subquery scan not considered unless offset 0 To: David Rowley Cc: postgres performance list , Tom Lane MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20171029144121.7C7CE5FB08@mx.zeyos.com> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org There is actually another separate issue here apart from that the planner o= bviously choosing the wrong plan as originally described in my last message= , a plan it knows to be more expensive based on cost estimates. Take a look at the way the filter condition is treated differently when est= imating the number of returned rows when applied in different nodes. Queries A/B: -> Finalize HashAggregate (cost=3D32879.78..33102.62 rows=3D22285 width= =3D12) (actual time=3D450.724..458.667 rows=3D15521 loops=3D1) Group Key: b.item Filter: (sum(b.amount) >=3D '1'::double precision) Rows Removed by Filter: 48277 -> Gather ... Query C: -> Subquery Scan on c (cost=3D32768.35..33269.76 rows=3D7428 width=3D12)= (actual time=3D456.591..475.204 rows=3D15521 loops=3D1) Filter: (c.stock >=3D '1'::double precision) Rows Removed by Filter: 48277 -> Finalize HashAggregate (cost=3D32768.35..32991.20 rows=3D22285= width=3D12) (actual time=3D456.582..468.124 rows=3D63798 loops=3D1) Group Key: b.item -> Gather ... Interestingly enough the subquery scan with query C correctly accounts for = the filter when estimating rows=3D7428, while A/B doesn't seem to account f= or the filter in the HasAggregate node (estimated rows=3D22285). This looks= like a bug. --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance