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 1orEtm-0006k6-NS for pgsql-hackers@arkaria.postgresql.org; Sat, 05 Nov 2022 08:51:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1orEtl-0001F5-DO for pgsql-hackers@arkaria.postgresql.org; Sat, 05 Nov 2022 08:51:29 +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 1orEtk-0001Ev-UQ for pgsql-hackers@lists.postgresql.org; Sat, 05 Nov 2022 08:51:29 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1orEti-0005ot-Ay for pgsql-hackers@lists.postgresql.org; Sat, 05 Nov 2022 08:51:28 +0000 Received: from [192.168.0.104] (unknown [62.217.188.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 137B121C453F; Sat, 5 Nov 2022 11:51:23 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1667638284; bh=/IqX1ZsUujd8StlFGT0qB2nsXzwjfa9csXIAw2dvd28=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=GbQ8v32G6yZvLoXr+sPqrC6XADsKR7ZqlSbrnFOgwJvVkfhaR6ga7/ZXbUJR4ebD6 TNkCuKATdzCabZBf5oz+/+JcsOKbGlT092Ob8wckdz3eNiS0W9wWgEWmZpr1YrMSsh VxlLJrXCKtwa3AxaEbppul/9U/3yfqMxZEc6dXQI= Message-ID: <9f61ddbf-2989-1536-b31e-6459370a6baa@postgrespro.ru> Date: Sat, 5 Nov 2022 11:51:23 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Content-Language: en-US To: David Rowley , Justin Pryzby Cc: Tom Lane , Richard Guo , Ronan Dunklau , pgsql-hackers@lists.postgresql.org, Ranier Vilela References: <3163474.aeNJFYEL58@aivenronan> <1138716.1659282591@sss.pgh.pa.us> <20220817015755.GB26426@telsasoft.com> From: Pavel Luzanov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, While playing with the patch I found a situation where the performance may be degraded compared to previous versions. The test case below. If you create a proper index for the query (a,c), version 16 wins. On my notebook, the query runs ~50% faster. But if there is no index (a,c), but only (a,b), in previous versions the planner uses it, but with this patch a full table scan is selected. create table t (a text, b text, c text); insert into t (a,b,c) select x,y,x from generate_series(1,100) as x, generate_series(1,10000) y; create index on t (a,b); vacuum analyze t; explain (analyze, buffers) select a, array_agg(c order by c) from t group by a; v 14.5                                                              QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------  GroupAggregate  (cost=0.42..46587.76 rows=100 width=34) (actual time=3.077..351.526 rows=100 loops=1)    Group Key: a    Buffers: shared hit=193387 read=2745    ->  Index Scan using t_a_b_idx on t  (cost=0.42..41586.51 rows=1000000 width=4) (actual time=0.014..155.095 rows=1000000 loops=1)          Buffers: shared hit=193387 read=2745  Planning:    Buffers: shared hit=9  Planning Time: 0.059 ms  Execution Time: 351.581 ms (9 rows) v 16                                                        QUERY PLAN ------------------------------------------------------------------------------------------------------------------------  GroupAggregate  (cost=128728.34..136229.59 rows=100 width=34) (actual time=262.930..572.915 rows=100 loops=1)    Group Key: a    Buffers: shared hit=5396, temp read=1950 written=1964    ->  Sort  (cost=128728.34..131228.34 rows=1000000 width=4) (actual time=259.423..434.105 rows=1000000 loops=1)          Sort Key: a, c          Sort Method: external merge  Disk: 15600kB          Buffers: shared hit=5396, temp read=1950 written=1964          ->  Seq Scan on t  (cost=0.00..15396.00 rows=1000000 width=4) (actual time=0.005..84.104 rows=1000000 loops=1)                Buffers: shared hit=5396  Planning:    Buffers: shared hit=9  Planning Time: 0.055 ms  Execution Time: 575.146 ms (13 rows) -- Pavel Luzanov Postgres Professional: https://postgrespro.com The Russian Postgres Company