Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VHr2u-00027h-CX for pgsql-performance@arkaria.postgresql.org; Fri, 06 Sep 2013 08:05:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VHr2t-0006Hm-T3 for pgsql-performance@arkaria.postgresql.org; Fri, 06 Sep 2013 08:05:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VHr2t-0006Hd-5M for pgsql-performance@postgresql.org; Fri, 06 Sep 2013 08:05:35 +0000 Received: from mail.plaintext.sk ([85.248.156.6] helo=shiva.plaintext.sk) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VHr2l-000770-31 for pgsql-performance@postgresql.org; Fri, 06 Sep 2013 08:05:34 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by shiva.plaintext.sk (Postfix) with ESMTP id AD02926D1D; Fri, 6 Sep 2013 10:05:23 +0200 (CEST) X-Virus-Scanned: amavisd-new at plaintext.sk Received: from shiva.plaintext.sk ([127.0.0.1]) by localhost (shiva.plaintext.sk [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7HDTz2wlZ8hu; Fri, 6 Sep 2013 10:05:22 +0200 (CEST) Received: from shiva.plaintext.sk (shiva.plaintext.sk [85.248.156.6]) by shiva.plaintext.sk (Postfix) with ESMTP id 9C9B126CCA; Fri, 6 Sep 2013 10:05:22 +0200 (CEST) Date: Fri, 6 Sep 2013 09:05:22 +0100 (GMT+01:00) From: =?utf-8?Q?=C4=BDubom=C3=ADr_Varga?= To: pgsql-performance@postgresql.org Cc: info Message-ID: <1186425372.701378454722489.JavaMail.root@shiva> Subject: planner and having clausule MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Mailer: Zimbra 5.0.16_GA_2921.UBUNTU8_64 (ZimbraWebClient - FF3.0 (Linux)/5.0.16_GA_2921.UBUNTU8_64) X-Pg-Spam-Score: -4.3 (----) 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 Hi. I have one query which possibly is not optimized by planner (not using = index for aggregate having clause restriction): explain SELECT stocktaking_id FROM t_weighting GROUP BY stocktaking_id HAVING MIN(stat_item_start) BETWEEN '2013-08-01' AND '2013-09-01'; with result: "HashAggregate (cost=3D59782.43..59787.39 rows=3D248 width=3D32)" " Filter: ((min(stat_item_start) >=3D '2013-08-01 00:00:00'::timestamp wit= hout time zone) AND (min(stat_item_start) <=3D '2013-09-01 00:00:00'::times= tamp without time zone))" " -> Seq Scan on t_weighting (cost=3D0.00..49002.39 rows=3D1437339 width= =3D32)" I have probably an obvious tough, that query will touch only rows with stat= _item_start values only within given constrains in having clause. If (and o= nly if) planner have some info that MIN and MAX aggregate functions could r= eturn only one of values that comes into them, it can search only rows with= in given constraints in having part of select. Something like this: explain SELECT stocktaking_id FROM t_weighting --added restriction by hand: WHERE stat_item_start BETWEEN '2013-08-01' AND '2013-09-01' GROUP BY stocktaking_id HAVING MIN(stat_item_start) BETWEEN '2013-08-01' AND '2013-09-01'; with result: "HashAggregate (cost=3D8.45..8.47 rows=3D1 width=3D32)" " Filter: ((min(stat_item_start) >=3D '2013-08-01 00:00:00'::timestamp wit= hout time zone) AND (min(stat_item_start) <=3D '2013-09-01 00:00:00'::times= tamp without time zone))" " -> Index Scan using idx_t_weighting_stat_item_start on t_weighting (co= st=3D0.00..8.44 rows=3D1 width=3D32)" " Index Cond: ((stat_item_start >=3D '2013-08-01 00:00:00'::timestam= p without time zone) AND (stat_item_start <=3D '2013-09-01 00:00:00'::times= tamp without time zone))" Is this optimization by planner possible, or it is already have been done o= n newer DB version (I am using PostgreSQL 8.4.13)? IMHO it should be added = into planner if possible for all built in aggregate functions. Best regards, -- Ing. =C4=BDubom=C3=ADr Varga +421 (0)908 541 700 varga@plaintext.sk www.plaintext.sk --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance