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 1os3VV-000378-EO for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Nov 2022 14:53:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1os3VT-00048V-RT for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Nov 2022 14:53:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1os3VT-00046U-Fh for pgsql-hackers@lists.postgresql.org; Mon, 07 Nov 2022 14:53:47 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1os3VQ-0002BS-Uc for pgsql-hackers@lists.postgresql.org; Mon, 07 Nov 2022 14:53:46 +0000 Received: by mail-wr1-x432.google.com with SMTP id l14so16623492wrw.2 for ; Mon, 07 Nov 2022 06:53:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ljxQr2vkZGpOMKAnQd2bqXoRprdENF7CCUg7pRqt1Kk=; b=NVVd2Qch/aLQASqR8b5Lcce5nF/0j1JTjxPgenbap6yakpgRvHbzqtJwNy+STKisg3 8zU7wt06jTfMw2ImFlcxXf8Sg6sdeON5l87cN0Ru/zBn8BD5Wz9c/2b7DtwVBMQoIhyM h5lKUvUZjJxqXL1mL9DbZd2wu2dPkC2LYJ6QA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ljxQr2vkZGpOMKAnQd2bqXoRprdENF7CCUg7pRqt1Kk=; b=MAdK70oxMQwZJanlszugsHwtcDHNJy3TE8X6x4hJmn1wgxm9ffBHFdgK7vSyrnhgj9 jtqJwQOfNUJxPNEJvZ8i6kXSQLGMOyIqDIvSOUWT0sd71woHg7p1ecuavrt4YsXqWxKh UdGr3ci3dBVc4L79dWXXizoC1fw1eW0+8u8bx4zm+FOb4IO759EeA1QfnDz9ChPlcoYi g9kXa9CIa/brD5sZ2m09r3wrrxGIFc9XxynjYnw21PW9l5A2baXihMlL54r5S0Euod0u gBgx8xS6tzrhon/It4PD0CRLwVCCzpfDdFW7IRmfl7JXwyg3cHMzD9qnob+4pywSvfib /TYA== X-Gm-Message-State: ACrzQf0hmBfkpOhP0oOX04aAio7Kph79E4UDeOcvn9yaXOHlZC3ytFFk qXuOiJ1OattlcGDh5/JHZ/mYjQ== X-Google-Smtp-Source: AMsMyM4GAru/rDv9gX79E/71P2crHk4z1ZlRSQE8+NZezgfDy1Po2bPYnafuvjU/A2FX6dRmkexs4w== X-Received: by 2002:a5d:4f0d:0:b0:236:562b:f67a with SMTP id c13-20020a5d4f0d000000b00236562bf67amr584881wru.562.1667832822209; Mon, 07 Nov 2022 06:53:42 -0800 (PST) Received: from aivenlaptop.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id bw14-20020a0560001f8e00b00240dcd4d1cesm3173893wrb.105.2022.11.07.06.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 06:53:41 -0800 (PST) From: Ronan Dunklau To: David Rowley , Justin Pryzby , Pavel Luzanov Cc: Tom Lane , Richard Guo , pgsql-hackers@lists.postgresql.org, Ranier Vilela Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Date: Mon, 07 Nov 2022 15:53:42 +0100 Message-ID: <2202180.iZASKD2KPV@aivenlaptop> In-Reply-To: <9f61ddbf-2989-1536-b31e-6459370a6baa@postgrespro.ru> References: <3163474.aeNJFYEL58@aivenronan> <9f61ddbf-2989-1536-b31e-6459370a6baa@postgrespro.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Le samedi 5 novembre 2022, 09:51:23 CET Pavel Luzanov a =E9crit : > While playing with the patch I found a situation where the performance > may be degraded compared to previous versions. >=20 > 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. Hello, In your exact use case, the combo incremental-sort + Index scan is evaluate= d=20 to cost more than doing a full sort + seqscan.=20 If you try for example to create an index on (b, a) and group by b, you wil= l=20 get the expected behaviour: ro=3D# create index on t (b, a); CREATE INDEX ro=3D# explain select b, array_agg(c order by c) from t group by b; QUERY PLAN = =20 =2D------------------------------------------------------------------------= =2D--------------- GroupAggregate (cost=3D10.64..120926.80 rows=3D9970 width=3D36) Group Key: b -> Incremental Sort (cost=3D10.64..115802.17 rows=3D1000000 width=3D6) Sort Key: b, c Presorted Key: b -> Index Scan using t_b_a_idx on t (cost=3D0.42..47604.12=20 rows=3D1000000 width=3D6) (6 rows) I think we can trace that back to incremental sort being pessimistic about= =20 it's performance. If you try the same query, but with set enable_seqscan = =3D off,=20 you will get a full sort over an index scan: QUERY PLAN = =20 =2D------------------------------------------------------------------------= =2D--------------- GroupAggregate (cost=3D154944.94..162446.19 rows=3D100 width=3D34) Group Key: a -> Sort (cost=3D154944.94..157444.94 rows=3D1000000 width=3D4) Sort Key: a, c -> Index Scan using t_a_b_idx on t (cost=3D0.42..41612.60=20 rows=3D1000000 width=3D4) (5 rows) This probably comes from the overly pessimistic behaviour that the number o= f=20 tuples per group will be 1.5 times as much as we should estimate: /* * Estimate average cost of sorting of one group where presorted=20 keys are * equal. Incremental sort is sensitive to distribution of tuples=20 to the * groups, where we're relying on quite rough assumptions. Thus,=20 we're * pessimistic about incremental sort performance and increase its=20 average * group size by half. */ I can't see why an incrementalsort could be more expensive than a full sort= ,=20 using the same presorted path. It looks to me that in that case we should=20 always prefer an incrementalsort. Maybe we should bound incremental sorts c= ost=20 to make sure they are never more expensive than the full sort ? Also, prior to this commit I don't think it made a real difference, because= =20 worst case scenario we would have missed an incremental sort, which we didn= 't=20 have beforehand. But with this patch, we may actually replace a "hidden"=20 incremental sort which was done in the agg codepath by a full sort.=20 Best regards, =2D- Ronan Dunklau