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 1m0NOR-0000th-Db for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jul 2021 12:08:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0NOQ-0001AH-8o for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jul 2021 12:08:06 +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 1m0NOP-0001A9-Rp for pgsql-hackers@lists.postgresql.org; Mon, 05 Jul 2021 12:08:06 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m0NOM-00054I-64 for pgsql-hackers@lists.postgresql.org; Mon, 05 Jul 2021 12:08:04 +0000 Received: by mail-wm1-x330.google.com with SMTP id j39-20020a05600c1c27b029020028e48b8fso5594648wms.0 for ; Mon, 05 Jul 2021 05:08:01 -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=V2ICJDWPG1vjsM7u5FUySwInlkwvF8oorp+b5dXkd9Y=; b=k4BWEZ1NeTfUZ6MyACtzwiQZOs3TODw+ckIaa48cHyIKF/w8nMXKN1gBE6gkYw9Tr2 avd7EE5vltetWKLMhU1DFxg6aoXyIY2lo3yBpNEpSbpm3/PDyaAfhDmSonz/imMIaRaL b3XRf5S/McKx0yYOFydj5yNBmz82S/5EbGZag= 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=V2ICJDWPG1vjsM7u5FUySwInlkwvF8oorp+b5dXkd9Y=; b=djDQ4e4G5K6qAHMy9HiP0Z5yra4LC5+6BoK6xdpU2fccXiMrl1wGNekKY73XBMQr5R WyueDEb4htUdbzpg/leOc+Gd1kXSSLfO5arvp+NXB+gAqYaG978Gyx/Wh7BI3zYAnLBz My13rtY5lPCMz5QhW9uJu1eVALPEUChzuxlKvQdbQZEGRUQst6m+KgD9ak7zZ1HF2T0B IHljzVLWp6FfuXeohjdAoQizOuBEgO1tev2HpKOFpPWf/C95h8sT+CVRMAZNXRPD1mbO Eo0wdmy2rPjHAs6UNd2kjTNBiWuEU3Qi5ELJZxheQlBrcGO/yEYr5xm0bHAjCFrXhOhy aHsw== X-Gm-Message-State: AOAM531fTwFd1QdLVb0AuWYVK5VFpklMtXqzIBqiMflUxfpp/b7oiAOv qGk7ADDB1DrHDdk/l60AjamWrw== X-Google-Smtp-Source: ABdhPJzUPK/XojXfnJte6to+S4f0cVc0Wtx3BXb4jCoVgmm8P3sUXPJoQu+pj2YA3MYhZVRU7XlN+w== X-Received: by 2002:a1c:1f12:: with SMTP id f18mr14542369wmf.183.1625486879946; Mon, 05 Jul 2021 05:07:59 -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 o8sm11372070wrx.28.2021.07.05.05.07.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 05:07:26 -0700 (PDT) From: Ronan Dunklau To: David Rowley , pgsql-hackers@lists.postgresql.org Cc: PostgreSQL Developers Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Date: Mon, 05 Jul 2021 14:07:05 +0200 Message-ID: <4480689.ObhdGn8bVM@aivenronan> Organization: aiven In-Reply-To: <2311666.Sa1gP0IYip@aivenronan> References: <2311666.Sa1gP0IYip@aivenronan> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart3227936.8YSYcEGHcz" 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. --nextPart3227936.8YSYcEGHcz Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" > Ok, I reproduced that case, just not using a group by: by adding the group > by a sort node is added in both cases (master and your patch), except that > with your patch we sort on both keys and that doesn't really incur a > performance penalty. > > I think the overhead occurs because in the ExecAgg case, we use the > tuplesort_*_datum API as an optimization when we have a single column as an > input, which the ExecSort code doesn't. Maybe it would be worth it to try to > use that API in sort nodes too, when it can be done. Please find attached a POC patch to do just that. The switch to the single-datum tuplesort is done when there is only one attribute, it is byval (to avoid having to deal with copy of the references everywhere) and we are not in bound mode (to also avoid having to move things around). A naive run on make check pass on this, but I may have overlooked things. Should I add this separately to the commitfest ? For the record, the times I got on my laptop, on master VS david's patch VS both. Values are an average of 100 runs, as reported by pgbench --no-vacuum -f -t 100. There is a good amount of noise, but the simple "select one ordered column case" seems worth the optimization. Only shared_buffers and work_mem have been set to 2GB each. Setup 1: single table, 1 000 000 tuples, no index CREATE TABLE tbench ( a int, b int ); INSERT INTO tbench (a, b) SELECT a, b FROM generate_series(1, 100) a, generate_series(1, 10000) b; Test 1: Single-column ordered select (order by b since the table is already sorted by a) select b from tbench order by b; master: 303.661ms with mine: 148.571ms Test 2: Ordered sum (using b so that the input is not presorted) select sum(b order by b) from tbench; master: 112.379ms with david's patch: 144.469ms with david's patch + mine: 97ms Test 3: Ordered sum + group by select b, sum(a order by a) from tbench GROUP BY b; master: 316.117ms with david's patch: 297.079 with david's patch + mine: 294.601 Setup 2: same as before, but adding an index on (b, a) CREATE INDEX ON tbench (b, a); Test 2: Ordered sum: select sum(a order by a) from tbench; master: 111.847 ms with david's patch: 48.088 with david's patch + mine: 47.678 ms Test 3: Ordered sum + group by: select a, sum(b order by b) from tbench GROUP BY a; master: 76.873 ms with david's patch: 61.105 with david's patch + mine: 62.672 ms -- Ronan Dunklau --nextPart3227936.8YSYcEGHcz Content-Disposition: attachment; filename="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="0001-Allow-Sort-nodes-to-use-the-fast-single-datum-tuples.patch"