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 1m0IFd-0005vj-ET for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jul 2021 06:38:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0IFb-00009n-0A for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jul 2021 06:38:39 +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 1m0IFa-00009d-4j for pgsql-hackers@lists.postgresql.org; Mon, 05 Jul 2021 06:38:38 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m0IFX-0002S7-CR for pgsql-hackers@lists.postgresql.org; Mon, 05 Jul 2021 06:38:36 +0000 Received: by mail-wr1-x42f.google.com with SMTP id t15so17272252wry.11 for ; Sun, 04 Jul 2021 23:38:35 -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=RfDewtezMMmMhKKCJdIffa0iabQsIi/K4GJCMGquwJ0=; b=ondhd83Bg0TfdBgnQYmY1InRecurEyHEzwGtkd43/tGN+tjgxS1wZmV7jFZvYqEXuE BpWB8GABp4YO1r0tJRXXGh1Mf79H07qx0w3Z/BFJQRA4zjZ5k6gsTMfvChFajMizI+dq Aatfwz6lKk2+oyuo40DJdz9c/CcJ8qgcial1g= 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=RfDewtezMMmMhKKCJdIffa0iabQsIi/K4GJCMGquwJ0=; b=EXbO9AY7u0Bb99yNVtV/WUJ2l1fU4zk3e1ma9nClFszmXn2MBoEUHGU/BML1LH0Mc0 sF/d21yztQXcqru8YNop/g5mP624/EWVBmZmhirWyY6Y/E2hkaCCFkirx4F9nw4e1Uuq hRISBnaQnA+ZWwFfQVMeoIQGYOl4ql68Vx8Rx+Ibo6xjYsa4UnOJpySPCLFUCnvFH18S /34hxb5iOYWE0ocK7gTSg8oqhTUsaACstUVFz0YEnjV2v9GQ8DvX3Kecx9GjNxifexnJ 5TEhM0ENYVmdNmN7ML1WzAfHlBfqUS3u6lVAFHK7OJJKEl0gLbwWdbv7SmdFhK3JpprN h7WQ== X-Gm-Message-State: AOAM5333ylCjalac+7Rup9wmEdDJIYtNxnt6AmD10VQDXVWcbkyhftbN wk23Q7cjord2Ewxbg8xl55SFCg== X-Google-Smtp-Source: ABdhPJz94bj/og1WZNG07ozjApa06lIO6XGQPCQiToz3pmulcnCs3yQEP09rdy6m2ZCr6sk2tA4K6A== X-Received: by 2002:a5d:56cd:: with SMTP id m13mr13836030wrw.181.1625467113742; Sun, 04 Jul 2021 23:38:33 -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 p15sm10784199wmq.43.2021.07.04.23.38.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jul 2021 23:38:33 -0700 (PDT) From: Ronan Dunklau To: David Rowley Cc: PostgreSQL Developers Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Date: Mon, 05 Jul 2021 08:38:28 +0200 Message-ID: <2311666.Sa1gP0IYip@aivenronan> Organization: aiven In-Reply-To: References: <5348877.kVHUhDTtjH@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 vendredi 2 juillet 2021, 10:39:44 CEST David Rowley a =E9crit : > On Fri, 2 Jul 2021 at 19:54, Ronan Dunklau wrote: > > 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 acceptab= le > > ? >=20 > That's a good question. There was an argument in [1] that mentions > that there might be a group of people who rely on aggregation being > done in a certain order where they're not specifying an ORDER BY > clause in the aggregate. If that group of people exists, then it's > possible they might be upset in the scenario that you describe. >=20 > I also think that it's going to be pretty hard to make significant > gains in this area if we are too scared to make changes to undefined > behaviour. You wouldn't have to look too hard in the pgsql-general > mailing list to find someone complaining that their query output is in > the wrong order on some query that does not have an ORDER BY. We > pretty much always tell those people that the order is undefined > without an ORDER BY. I'm not too sure why Tom in [1] classes the ORDER > BY aggregate people any differently. We'll be stuck forever here and > in many other areas if we're too scared to change the order of > aggregation. You could argue that something like parallelism has > changed that for people already. I think the multi-batch Hash > Aggregate code could also change this. I would agree with that. >=20 > > 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 ? >=20 > It was pretty basic. I just created a table with two columns and no > index and did something like SELECT a,SUM(b ORDER BY b) from ab GROUP > BY 1; the new code will include a Sort due to lack of any index and > the old code would have done a sort inside nodeAgg.c. I imagine that > the overhead comes from the fact that in the patched version nodeAgg.c > must ask its subnode (nodeSort.c) for the next tuple each time, > whereas unpatched nodeAgg.c already has all the tuples in a tuplestore > and can fetch them very quickly in a tight loop. Ok, I reproduced that case, just not using a group by: by adding the group = by=20 a sort node is added in both cases (master and your patch), except that wit= h=20 your patch we sort on both keys and that doesn't really incur a performance= =20 penalty.=20 I think the overhead occurs because in the ExecAgg case, we use the=20 tuplesort_*_datum API as an optimization when we have a single column as an= =20 input, which the ExecSort code doesn't. Maybe it would be worth it to try t= o=20 use that API in sort nodes too, when it can be done.=20 >=20 > David >=20 > [1] https://www.postgresql.org/message-id/6538.1522096067%40sss.pgh.pa.us =2D-=20 Ronan Dunklau