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 1m5MAl-0005Dx-NP for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Jul 2021 05:50:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m5MAk-00066H-B0 for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Jul 2021 05:50:34 +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 1m5MAk-000669-1N for pgsql-hackers@lists.postgresql.org; Mon, 19 Jul 2021 05:50:34 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m5MAh-0004ID-S3 for pgsql-hackers@lists.postgresql.org; Mon, 19 Jul 2021 05:50:33 +0000 Received: by mail-wm1-x333.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso12014479wmh.4 for ; Sun, 18 Jul 2021 22:50:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=from:to:cc:subject:date:message-id:organization:in-reply-to :references:mime-version:content-transfer-encoding; bh=qqUM6wuFFDMfn4DNnJeWt0TwwSa/W2oRQVzGb8NJnas=; b=RrWbDZca5ddh28a4z1xIwRBFZ7pX8/V80PlJvrQvNR9NleSSCt2BHocqEkh8f/7/rp 7nOkt9jerIA6XjBMVXuYPlBMJ3KDHh8hdLf0TCegKq4GKK9BeDcLGmnNAmqExRcfh541 4p0GHL8rnyYHLCt3Z7QT/zN0bEQQbwQtrlPhA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:organization :in-reply-to:references:mime-version:content-transfer-encoding; bh=qqUM6wuFFDMfn4DNnJeWt0TwwSa/W2oRQVzGb8NJnas=; b=EBq1aKx0evsaOTDHbI7tgRwSsiGkcIdoFtk3aCZ9vZmjZpr82c+tOpnVroulOOs/t6 wQDTBgsPYjgZfNlaJFRUaYI9pFOH0uQhifRDBhdxF8dxZqHSNpUDU/qwAvi8TVzHH8v6 WBycAHpfo1w5bBOWbIvoGo6ieOL+sMbn80BDn75n3qqm0+EWw2OCV38aVcbxlH5uiW88 SMw+JR1suWEEu+u+oFAs51kgqSjvdnDEL4UWbyBG+9dKC0dn7rhvAmJ0yKuzkyarRtnw 1x+T+3iaUTaKR7f54IcXaFPrqiIr9zs5PdXUST2oztAQMv2Q4rWeNcFQm3gHSuD01QSr is4g== X-Gm-Message-State: AOAM532JUDfHp0k3E8ZtYZ6tiHVNkvG3euQaXpx2eVPNeay3rkVLLlue qnMQPV3fkXRScTwmAMDDE1rYkw== X-Google-Smtp-Source: ABdhPJzgfWMGrPTMe6h2OfKLAVEIcDPvRNJng9RlUXdk9KsYJgMenBkJOSR0te0+uCXw9eaFhp4F/g== X-Received: by 2002:a7b:cb92:: with SMTP id m18mr24300276wmi.29.1626673828209; Sun, 18 Jul 2021 22:50:28 -0700 (PDT) Received: from aivenronan.localnet (static-176-158-121-96.ftth.abo.bbox.fr. [176.158.121.96]) by smtp.gmail.com with ESMTPSA id l14sm18123146wrs.22.2021.07.18.22.50.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Jul 2021 22:50:27 -0700 (PDT) From: Ronan Dunklau To: James Coleman , David Rowley Cc: Ranier Vilela , PostgreSQL Developers , Dilip Kumar Subject: Re: [PATCH] Use optimized single-datum tuplesort in ExecSort Date: Mon, 19 Jul 2021 07:50:16 +0200 Message-ID: <2866025.21dbZ4J8xM@aivenronan> Organization: aiven In-Reply-To: References: <3177670.itZtoPt7T5@aivenronan> 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 17 juillet 2021, 10:36:09 CEST David Rowley a =E9crit : > On Sat, 17 Jul 2021 at 01:14, James Coleman wrote: > > The only remaining question I have is whether or not costing needs to > > change, given the very significant speedup for datum sort. >=20 > I'm looking at cost_tuplesort and the only thing that I think might > make sense would be to adjust how the input_bytes value is calculated. > For now, that's done with the following function that's used in quite > a number of places. >=20 > static double > relation_byte_size(double tuples, int width) > { > return tuples * (MAXALIGN(width) + MAXALIGN(SizeofHeapTupleHeader)); > } >=20 > It seems, at least in the case of Sort, that using SizeofHeapTupleHead > is just always wrong as it should be SizeofMinimalTupleHeader. I know > that's also the case for Memoize too. I've not checked the other > locations. >=20 > The only thing I can really see that we might do would be not add the > MAXALIGN(SizeofHeapTupleHeader) when there's just a single column. > We'd need to pass down the number of attributes from > create_sort_path() so we'd know when and when not to add that. I'm not > saying that we should do this. I'm just saying that I don't really see > what else we might do. >=20 > I can imagine another patch might just want to do a complete overhaul > of all locations that use relation_byte_size(). There are various > things that function just does not account for. e.g, the fact that we > allocate chunks in powers of 2 and that there's a chunk header added > on. Of course, "width" is just an estimate, so maybe trying to > calculate something too precisely wouldn't be too wise. However, > there's a bit of a chicken and the egg problem there as there'd be > little incentive to improve "width" unless we started making more > accurate use of the value. >=20 > Anyway, none of the above take into account that the Datum sort is > just a little faster, The only thing that exists in the existing cost > modal that we could use to adjust the cost of an in memory sort is the > comparison_cost. The problem there is that the comparison is exactly > the same in both Datum and Tuple sorts. The only thing that really > changes between Datum and Tuple sort is the fact that we don't make a > MinimalTuple when doing a Datum sort. The cost modal, unfortunately, > does not account for that. That kinda makes me think that we should > do nothing as if we start to account for making MemoryTuples then > we'll just penalise Tuple sorts and that might cause someone to be > upset. >=20 Thank you for taking the time to perform that analysis. I agree with you an= d=20 tt looks to me that if we were to start accounting for it, we would have to= =20 make the change almost transparent for tuple sorts so that it stays roughly= =20 the same, which is impossible since we don't apply the comparison cost to a= ll=20 tuples but only to the number of tuples we actually expect to compare. On the other hand, if we don't change the sorting cost and it just ends up= =20 being faster in some cases I doubt anyone would complain. =2D-=20 Ronan Dunklau