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 1pKzVl-0004fE-SD for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Jan 2023 10:29:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pKzVk-00025p-GX for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Jan 2023 10:29:40 +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 1pKzVk-00025f-5c for pgsql-hackers@lists.postgresql.org; Thu, 26 Jan 2023 10:29:40 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pKzVh-0000BF-M4 for pgsql-hackers@lists.postgresql.org; Thu, 26 Jan 2023 10:29:39 +0000 Received: by mail-ej1-x634.google.com with SMTP id rl14so3889502ejb.2 for ; Thu, 26 Jan 2023 02:29:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=9t25aIVoU2OhO1g7IBpmwNlBZbxjHu9P9oPhRgpTAx4=; b=CH/WBq/JGpk1jRrdl1Hj2JhWeqo3mBQ9Jju67eX80uKwHGlZX9EufmJ/FOLuy53avD v9xhmwc5qQkMZsWQezDtYy6mtPZMVwpi3mOuAWw2WenBSBGz23sQP0Jwo0OF68MtGasS T57RlR//SMV2oVXhAMZtgw0jrut029ItP087+QKmSA3xoxOmnYc7Qgu5cmzBrzMg/aE/ BrHGZWdSo/UNsp+Bgu8lTi9nuDBJ/4Kcema01mgAe7c68UXeWEXPSm5QCZxUf7y/cxv2 zM4GyguNEis0DpOpCwULN7n12pS/LM/A85RiDDcbqvqxMRixHHqE0yhf6iP2G241FYIb YoiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9t25aIVoU2OhO1g7IBpmwNlBZbxjHu9P9oPhRgpTAx4=; b=1HBNx29yqzmd7vD1aRhl+Jc2WeRIgdTMCXHIfYU8JckgTBFXEQfap0g8NlRFD5CrD3 u9D5OqLE8z/e9ND4sk8H5Y+hg+04++yCxSK04c4MrWVfoxgSdTyr/qjsNUf08YDCZykD vUBAr7xmbmcTTlWy0/tKCTw5A3m2oT7yVDsTc/hT/T3fhnliIvhQ5/zc+kywH7A6yqBw hDa3HyLpfuRRLeyuIYQa/dPS1rjkDTpi5XFIOofqzZY+OJoRF93Ol1Fy1vQ91sWxOngo K6L4lpzVUxvSy4KtaeIv14IUgERje0ZMYgm4lgrzqDndM/z4GAn8DjSodHfacOAXMwO5 UfHg== X-Gm-Message-State: AFqh2kol1oogKHWRukhaYzUn28hOG19Aw7aBVVY2Hr1wfqeKHnTssTE7 4WzQr+LttGd3IgMVwwfN1K2NOSp0UMXFAzqzfXLj3lGkhpyG/Rxv4awLbvb8oSY6YVFBIlBkjAI FnN7po5lBbnFKnpPOrk+lo5X++xiQ3i7kahGwJU5fuxaAfAmpKRHBI5D/QErR5bfaY/Sbtlon85 muqjJeKoUzIoDJKPoU2r2TizGGDBHjEdjlKa7K2ODeEKJF6/7Qu7xK7BZDKuNDNF6DgYQVJt051 cjR2d/Dp1LhbW880UyEP2mqrm8AVzSZ/O/oPismJWfx4tR4n299UTFVGtaYaflf/qrFnXusmmWk X-Google-Smtp-Source: AMrXdXsk8x9PB0LLaJDpDgxjXTEOlYtbBJC4qQXUDFzN8C4RVDhxUP6zHXgG6+CAxciGZTVW7rlBzPdFYVfSGUuN4oc= X-Received: by 2002:a17:907:b16:b0:871:2202:10fd with SMTP id h22-20020a1709070b1600b00871220210fdmr5091958ejl.176.1674728976753; Thu, 26 Jan 2023 02:29:36 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: John Naylor Date: Thu, 26 Jan 2023 17:29:25 +0700 Message-ID: Subject: Re: Considering additional sort specialisation functions for PG16 To: David Rowley Cc: PostgreSQL Developers , Thomas Munro Content-Type: multipart/alternative; boundary="000000000000c1656905f3283967" X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c1656905f3283967 Content-Type: text/plain; charset="UTF-8" On Tue, Aug 23, 2022 at 1:13 PM John Naylor wrote: > > On Tue, Aug 23, 2022 at 11:24 AM David Rowley wrote: > > > > On Tue, 23 Aug 2022 at 15:22, John Naylor wrote: > > > Did you happen to see > > > > > > https://www.postgresql.org/message-id/CAFBsxsFhq8VUSkUL5YO17cFXbCPwtbbxBu%2Bd9MFrrsssfDXm3Q%40mail.gmail.com > > > > I missed that. It looks like a much more promising idea than what I > > came up with. I've not looked at your code yet, but I'm interested and > > will aim to look soon. > > Note that I haven't actually implemented this idea yet, just tried to > model the effects by lobotomizing the current comparators. I think > it's worth pursuing and will try to come back to it this cycle, but if > you or anyone else wants to try, that's fine of course. Coming back to this, I wanted to sketch out this idea in a bit more detail. Have two memtuple arrays, one for first sortkey null and one for first sortkey non-null: - Qsort the non-null array, including whatever specialization is available. Existing specialized comparators could ignore nulls (and their ordering) taking less space in the binary. - Only if there is more than one sort key, qsort the null array. Ideally at some point we would have a method of ignoring the first sortkey (this is an existing opportunity that applies elsewhere as well). - To handle two arrays, grow_memtuples() would need some adjustment, as would any callers that read the final result of an in-memory sort -- they would need to retrieve the tuples starting with the appropriate array depending on NULLS FIRST/LAST behavior. I believe external merges wouldn't have to do anything different, since when writing out the tapes, we read from the arrays in the right order. (One could extend this idea further and have two pools of tapes for null and non-null first sortkey, that are merged separately, in the right order. That sounds like quite a bit more complexity than is worth, however.) -- John Naylor EDB: http://www.enterprisedb.com --000000000000c1656905f3283967 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Tue, Aug 23, 2022 at 1:13 PM John = Naylor <john.naylor@ente= rprisedb.com> wrote:
>
> On Tue, Aug 23, 2022 at 11:24 A= M David Rowley <dgrowleyml@gmail= .com> wrote:
> >
> > On Tue, 23 Aug 2022 at 15:22,= John Naylor <john.naylo= r@enterprisedb.com> wrote:
> > > Did you happen to see> > >
> > > https://www.postgresql.org/message-id/CAFBsxsFhq8VUSkUL5YO17cFXbCPwtb= bxBu%2Bd9MFrrsssfDXm3Q%40mail.gmail.com
> >
> > I mis= sed that.=C2=A0 It looks like a much more promising idea than what I
>= ; > came up with. I've not looked at your code yet, but I'm inte= rested and
> > will aim to look soon.
>
> Note that I = haven't actually implemented this idea yet, just tried to
> model= the effects by lobotomizing the current comparators. I think
> it= 9;s worth pursuing and will try to come back to it this cycle, but if
&g= t; you or anyone else wants to try, that's fine of course.

Comin= g back to this, I wanted to sketch out this idea in=C2=A0a bit more detail.=

Have two memtuple arrays, one for first sortkey null and one for fi= rst sortkey non-null:
- Qsort the non-null array, including whatever spe= cialization is available. Existing specialized comparators could ignore nul= ls (and their ordering) taking less space in the binary.
- Only if there= is more than one sort key, qsort the null array. Ideally at some point we = would have a method of ignoring the first sortkey (this is an existing oppo= rtunity that applies elsewhere as well).
- To handle two arrays, grow_me= mtuples() would need some adjustment, as would any callers that read the fi= nal result of an in-memory sort -- they would need to retrieve the tuples s= tarting with the appropriate array depending on NULLS FIRST/LAST behavior.<= br>
I believe external merges wouldn't have to do anything different= , since when writing out the tapes, we read from the arrays in the right or= der.

(One could extend thi= s idea further and have two pools of tapes for null and non-null first sort= key, that are merged separately, in the right order. That sounds like quite= a bit more complexity than is worth, however.)

--
John Naylor
EDB: http://www.enterprisedb.com
--000000000000c1656905f3283967--