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 1lzE02-0003pn-OA for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Jul 2021 07:54:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lzE00-0001zr-K6 for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Jul 2021 07:54:08 +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 1lzE00-0001zj-Bd for pgsql-hackers@lists.postgresql.org; Fri, 02 Jul 2021 07:54:08 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lzDzy-0000MI-M2 for pgsql-hackers@lists.postgresql.org; Fri, 02 Jul 2021 07:54:07 +0000 Received: by mail-wr1-x433.google.com with SMTP id t6so1428266wrm.9 for ; Fri, 02 Jul 2021 00:54:06 -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=znXTGEZk9nR2yR+Y1IPsSKbwkvl9Nrs0WyZk2Xe3USc=; b=DrGU8HmoTqzBa3szN+BU72MA84MZPlPnQrGGL/5cIKssQLpDIlbb6j5uMJtemckaOs eisfXJ2Vx1Pnhr+Xh2N+Makkl5kslTFRe5Ifvam5QYAuXYyHJWYNsPLTe2RsrE0HmgNo mdJMIB+V32SZ4YwsbsfgYWlvUxbbvyIJg6CEc= 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=znXTGEZk9nR2yR+Y1IPsSKbwkvl9Nrs0WyZk2Xe3USc=; b=SELS422yaiIWeIY5yI5yuDDiEWMOe6QpfpTpb7cMa0WHj80Syxe0mmmXKh2tzlhsI/ t2R4RCtGXiRrZrvLJyR538avySue9zT+eOHEHZ53+5/Ah9GP8RhlvWGEjdtWK+WH5OYc M6B519hFBahRs/Kqk4AV0We2GxnRcmVJ3CR2awfTJzilhRB8MsFGwYyDj0HyvwGdtBN0 IFcA06GrKNqWQQ6hI8jEPxFTLkCn+uMub0Hh+a3QLU5G/RYhq+VxOo9rmj5sDoT8Zjlu RRXd0wkCdOylD+cdSj/RlRsiu5PYJfPNdW7W+A4jbnA8OQLTV2oxLSCajUonZOIgXat/ W0/Q== X-Gm-Message-State: AOAM5322PA9TdbkaU1kjzeLa5khLjUB+f/Ie6mrU+w9lsax6WFzaQmTO mDfop3dhEfxfMvmvvIYPSCvvvNJalrI+qMBY X-Google-Smtp-Source: ABdhPJxL7BiCAUFMsBv+OY4mNsVP5EyCPAYMQOk8Jm0uJq//6mTm0FJNR1YhIlgyIqtKMUUkwC06fQ== X-Received: by 2002:adf:e603:: with SMTP id p3mr4095979wrm.232.1625212444593; Fri, 02 Jul 2021 00:54:04 -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 b20sm2353718wmj.7.2021.07.02.00.53.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jul 2021 00:54:00 -0700 (PDT) From: Ronan Dunklau To: PostgreSQL Developers Cc: David Rowley Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Date: Fri, 02 Jul 2021 09:53:52 +0200 Message-ID: <5348877.kVHUhDTtjH@aivenronan> Organization: aiven In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > > This allows us to give presorted input to both aggregates in the following > case: > > SELECT agg(a ORDER BY a),agg2(a ORDER BY a,b) ... > > but just the first agg in this one: > > SELECT agg(a ORDER BY a),agg2(a ORDER BY c) ... I don't know if it's acceptable, but in the case where you add both an aggregate with an ORDER BY clause, and another aggregate without the clause, the output for the unordered one will change and use the same ordering, maybe suprising the unsuspecting user. Would that be acceptable ? > When testing the performance of all this I found that when a suitable > index exists to provide pre-sorted input for the aggregation that the > performance does improve. Unfortunately, it looks like things get more > complex when no index exists. In this case, since we're setting > pathkeys to tell the planner we need a plan that provides pre-sorted > input to the aggregates, the planner will add a sort below the > aggregate node. I initially didn't see any problem with that as it > just moves the sort to a Sort node rather than having it done > implicitly inside nodeAgg.c. The problem is, it just does not perform > as well. I guess this is because when the sort is done inside > nodeAgg.c that the transition function is called in a tight loop while > reading records back from the tuplestore. In the patched version, > there's an additional node transition in between nodeAgg and nodeSort > and that causes slower performance. For now, I'm not quite sure what > to do about that. We set the plan pathkeys well before we could > possibly decide if asking for pre-sorted input for the aggregates > would be a good idea or not. I was curious about the performance implication of that additional transition, and could not reproduce a signifcant difference. I may be doing something wrong: how did you highlight it ? Regards, -- Ronan Dunklau