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 1m2wTX-0002yo-3Y for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Jul 2021 13:59:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m2wTU-0001kw-LN for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Jul 2021 13:59:56 +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 1m2wTU-0001ko-86 for pgsql-hackers@lists.postgresql.org; Mon, 12 Jul 2021 13:59:56 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m2wTQ-0008HH-Ll for pgsql-hackers@lists.postgresql.org; Mon, 12 Jul 2021 13:59:54 +0000 Received: by mail-wr1-x42d.google.com with SMTP id t5so11910086wrw.12 for ; Mon, 12 Jul 2021 06:59:52 -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=rGhLMhD6MPNzrCYg+mBzyR6OR5LJ++nni6NfMfugqCI=; b=qiEoONzUJb3mWVi/w21X78FImrkhU7vPX8EwXCtJr4bDSA7lGZ0dhdZdmRnyv9Dkrf e2b/HVLR1AmPHXUfGx5Took+eLx9T0KUnIu3sx02d/Yzo4CrBPA+XGySCAU4p9iDQjCL pgm8m6oPUCrGjAY56DkQ+Nx+XwAYlxh1xhvXk= 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=rGhLMhD6MPNzrCYg+mBzyR6OR5LJ++nni6NfMfugqCI=; b=WHsE/XgaKX1UR0Eyumb5wH5ucFynOxX0XIk3eXHdjebzzNFJFrEYhOqJllpZbo9xON W23R6W6A8qBgqOZZnAyPT+eruk2nui6CvED3fKb7EEByTsTVDzjyhaj0tRLDbFI/kl32 +kwndaeQeucNk1Tz/HN2UXQTcvH0HH6zjjFu07z5BO0Vsg0+19Oo42ojrs/w6tT8O5ok DN9znXY9g7HTyIesFSZaS+yXiVxW1WvPDXvbEzyftuZajGKbwQ26lyEp1b8zIhgSwGG0 jrE/gFfsiApb4rHPPkXZ7pE+0Dw5rYhlydoSuETf/GKzCKTPujFQh/zEUs7UlWuQVPmE qyLg== X-Gm-Message-State: AOAM533vXqpIY8/cPhB8H5qn9mLNBA0g74gPUkJmRAk0JicvwjvCGwi2 8m3n+IilY3EpXl/068h7/fy1gg== X-Google-Smtp-Source: ABdhPJwMrkpyAbGwqoi7mRFIDX+O8BFS9GcucbwcFMvWKx52oD4AhHCWhT26Esn2Ee7xxgq0GLVh8Q== X-Received: by 2002:a05:6000:2ce:: with SMTP id o14mr60346838wry.145.1626098391185; Mon, 12 Jul 2021 06:59:51 -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 a9sm14026773wrv.37.2021.07.12.06.59.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jul 2021 06:59:50 -0700 (PDT) From: Ronan Dunklau To: David Rowley Cc: James Coleman , PostgreSQL Developers , Ranier Vilela , Dilip Kumar Subject: Re: [PATCH] Use optimized single-datum tuplesort in ExecSort Date: Mon, 12 Jul 2021 15:59:42 +0200 Message-ID: <6987360.ZhFIMvCLOo@aivenronan> Organization: aiven In-Reply-To: References: <3177670.itZtoPt7T5@aivenronan> <3060002.hb0XKQ11pn@aivenronan> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart10617044.nNE7uE4vYN" Content-Transfer-Encoding: 7Bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --nextPart10617044.nNE7uE4vYN Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" Le lundi 12 juillet 2021, 15:11:17 CEST David Rowley a =E9crit : > On Wed, 7 Jul 2021 at 21:32, Ronan Dunklau wrote: > > In the meantime I fixed some formatting issues, please find attached a = new > > patch. >=20 > I started to look at this. Thank you ! I'm attaching a new version of the patch taking your remarks in= to=20 account. >=20 > First I wondered how often we might be able to apply this > optimisation, so I ran make check after adding some elog(NOTICE) calls > to output which method is going to be used just before we do the > tuplestore_begin_* calls. It looks like there are 614 instances of > Datum sorts and 4223 of tuple sorts. That's about 14.5% datum sorts. > 223 of the 614 are byval types and the other 391 are byref. Not that > the regression tests are a good reflection of the real world, but if > it were then that's quite a good number of cases to be able to > optimise. That's an interesting stat. >=20 > As for the patch, just a few things: >=20 > 1. Can you add the missing braces in this if condition and the else > condition that belongs to it. >=20 > + if (node->is_single_val) > + for (;;) > + { >=20 Done. > 2. I think it would nicer to name the new is_single_val field > "datumSort" instead. To me it seems more clear what it is for. Done. >=20 > 3. This seems to be a bug fix where byval datum sorts do not properly > handle bounded sorts. I think that maybe that should be fixed and > backpatched. I don't see anything that says Datum sorts can't be > bounded and if there were some restriction on that I'd expect > tuplesort_set_bound() to fail when the Tuplesortstate had been set up > with tuplesort_begin_datum(). I've kept this as-is for now, but I will remove it from my patch if it is=20 deemed worthy of back-patching in your other thread.=20 Regards, =2D-=20 Ronan Dunklau --nextPart10617044.nNE7uE4vYN Content-Disposition: attachment; filename="v5-0001-Allow-Sort-nodes-to-use-the-fast-single-datum-tuples.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="UTF-8"; name="v5-0001-Allow-Sort-nodes-to-use-the-fast-single-datum-tuples.patch" commit 91210952cf435995c9db6b539bf29f6fd744be70 Author: Ronan Dunklau Date: Tue Jul 6 16:34:31 2021 +0200 Use optimized single-datum tuplesort in ExecSort Previously, with optimization was only done in ExecAgg. Doing this optimization on regular sort nodes provides for a nice performance improvement, when the input / output tuple has only one attribute. diff --git a/src/backend/executor/nodeSort.c b/src/backend/executor/nodeSort.c index b99027e0d7..df4e79c6ba 100644 --- a/src/backend/executor/nodeSort.c +++ b/src/backend/executor/nodeSort.c @@ -29,6 +29,10 @@ * which saves the results in a temporary file or memory. After the * initial call, returns a tuple from the file with each call. * + * The tuplesort can either occur on the whole tuple (this is the nominal + * case) or, when the input / output tuple consists of only one attribute, + * we switch to the tuplesort_*_datum API, optimized for that specific case. + * * Conditions: * -- none. * @@ -86,31 +90,64 @@ ExecSort(PlanState *pstate) outerNode = outerPlanState(node); tupDesc = ExecGetResultType(outerNode); - tuplesortstate = tuplesort_begin_heap(tupDesc, - plannode->numCols, - plannode->sortColIdx, - plannode->sortOperators, - plannode->collations, - plannode->nullsFirst, - work_mem, - NULL, - node->randomAccess); + /* + * Switch to the tuplesort_*_datum interface when we have only one + * key, as it is much more efficient especially when the type is + * pass-by-value. + */ + if (tupDesc->natts == 1) + { + node->datumSort = true; + tuplesortstate = tuplesort_begin_datum(TupleDescAttr(tupDesc, 0)->atttypid, + plannode->sortOperators[0], + plannode->collations[0], + plannode->nullsFirst[0], + work_mem, + NULL, + node->randomAccess); + } + else + tuplesortstate = tuplesort_begin_heap(tupDesc, + plannode->numCols, + plannode->sortColIdx, + plannode->sortOperators, + plannode->collations, + plannode->nullsFirst, + work_mem, + NULL, + node->randomAccess); if (node->bounded) tuplesort_set_bound(tuplesortstate, node->bound); node->tuplesortstate = (void *) tuplesortstate; /* - * Scan the subplan and feed all the tuples to tuplesort. + * Scan the subplan and feed all the tuples to tuplesort, using either + * the _putdatum or _puttupleslot API as appropriate. */ - - for (;;) + if (node->datumSort) { - slot = ExecProcNode(outerNode); - - if (TupIsNull(slot)) - break; - - tuplesort_puttupleslot(tuplesortstate, slot); + for (;;) + { + slot = ExecProcNode(outerNode); + + if (TupIsNull(slot)) + break; + slot_getsomeattrs(slot, 1); + tuplesort_putdatum(tuplesortstate, + slot->tts_values[0], + slot->tts_isnull[0]); + } + } + else + { + for (;;) + { + slot = ExecProcNode(outerNode); + + if (TupIsNull(slot)) + break; + tuplesort_puttupleslot(tuplesortstate, slot); + } } /* @@ -150,9 +187,18 @@ ExecSort(PlanState *pstate) * next fetch from the tuplesort. */ slot = node->ss.ps.ps_ResultTupleSlot; - (void) tuplesort_gettupleslot(tuplesortstate, - ScanDirectionIsForward(dir), - false, slot, NULL); + if (node->datumSort) + { + ExecClearTuple(slot); + if (tuplesort_getdatum(tuplesortstate, ScanDirectionIsForward(dir), + &(slot->tts_values[0]), &(slot->tts_isnull[0]), NULL)) + ExecStoreVirtualTuple(slot); + } + else + (void) tuplesort_gettupleslot(tuplesortstate, + ScanDirectionIsForward(dir), + false, slot, NULL); + return slot; } @@ -191,6 +237,7 @@ ExecInitSort(Sort *node, EState *estate, int eflags) sortstate->bounded = false; sortstate->sort_Done = false; sortstate->tuplesortstate = NULL; + sortstate->datumSort = false; /* * Miscellaneous initialization diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 22972071ff..f785259da7 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -4773,6 +4773,14 @@ leader_takeover_tapes(Tuplesortstate *state) static void free_sort_tuple(Tuplesortstate *state, SortTuple *stup) { - FREEMEM(state, GetMemoryChunkSpace(stup->tuple)); - pfree(stup->tuple); + /* + * If the SortTuple is actually only a single Datum, which was not copied + * as it is a byval type, do not try to free it nor account for it in + * memory used. + */ + if (stup->tuple) + { + FREEMEM(state, GetMemoryChunkSpace(stup->tuple)); + pfree(stup->tuple); + } } diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 0ec5509e7e..89f944a9c4 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -2151,6 +2151,7 @@ typedef struct SortState int64 bound_Done; /* value of bound we did the sort with */ void *tuplesortstate; /* private state of tuplesort.c */ bool am_worker; /* are we a worker? */ + bool datumSort; /* are we using the single value optimization ? */ SharedSortInfo *shared_info; /* one entry per worker */ } SortState; --nextPart10617044.nNE7uE4vYN--