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 1m5Mpg-0006o5-0g for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Jul 2021 06:32:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m5Mpe-0002pj-8o for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Jul 2021 06:32:50 +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 1m5Mpd-0002pa-WE for pgsql-hackers@lists.postgresql.org; Mon, 19 Jul 2021 06:32:50 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m5Mpb-0004iZ-As for pgsql-hackers@lists.postgresql.org; Mon, 19 Jul 2021 06:32:49 +0000 Received: by mail-wr1-x42b.google.com with SMTP id d2so20599717wrn.0 for ; Sun, 18 Jul 2021 23:32:46 -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=+osvWkEMWwwl/Ht567dpgRAW8y2FsELezaRd/NRTYUk=; b=aol8hrEOOIo7UkbyB4nVZ3ldevxcWOsmFVWT2PdRe8de4zETP7m9iT5LRHZj+Q/zUt W5NQ+JjGo+FOitH/+hGXvvRRiVREhThAstnkFKjis9bSfFt8VFt4CiW5tNFfoS0ImE4F lEBLRNv6KE9My+GanX73GrEvS/duvx9o+n1nQ= 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=+osvWkEMWwwl/Ht567dpgRAW8y2FsELezaRd/NRTYUk=; b=K6QES34bmuCrz9oyRDJhMXcRf690xOgouAa6WvJzhHFXJAkp7JYXtuDCsHsnNoZZ2T m5AUHIIxKx6ihQrIL0VpTGUYevDpkHtQzJXVd+9/xHUhIWx1t5lG8g+8u0BjJKeO5tzs ILTnXLaw0pmJzth3Ck9h2lHcp45iXBikGv+dLv8am94em8GgpO2+nIEDeuFayW8HYRAt p0NtOIg2om7otABc0rDlwpEOCdbQFfr1RO0sepDxiJzss4wMsThajVEm4KFk0SNwzdKH +G6D2EB0nHv4tQIGZvyxetBM/Sz4yz1u+L8GlPW88ahIsPcyaxzGSOOgoFCOuOtIkmRX Xvrg== X-Gm-Message-State: AOAM532woGiylM6L8ULbBzeaDC4j6GJYvJf1maMEZZ2GYWtLx54f0dG4 JqO2g28yY0xIFmr8XuI8vBXYTw== X-Google-Smtp-Source: ABdhPJzMd1HQqOsnvDUK59NeFjOlrC1xX4lm/GpG+bYiAR2GEYYFRfbIo9975DtL4lFceUbaHqGJ9w== X-Received: by 2002:a5d:544f:: with SMTP id w15mr27782491wrv.151.1626676366111; Sun, 18 Jul 2021 23:32:46 -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 l14sm18232847wrs.22.2021.07.18.23.32.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Jul 2021 23:32:45 -0700 (PDT) From: Ronan Dunklau To: David Rowley Cc: PostgreSQL Developers , Ranier Vilela Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Date: Mon, 19 Jul 2021 08:32:34 +0200 Message-ID: <4285468.TQqHX9kgJM@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 > Looks like I did a sloppy job of that. I messed up the condition in > standard_qp_callback() that sets the ORDER BY aggregate pathkeys so > that it accidentally set them when there was an unsortable GROUP BY > clause, as highlighted by the postgres_fdw tests failing. I've now > added a comment to explain why the condition is the way it is so that > I don't forget again. > > Here's a cleaned-up version that passes make check-world. > I've noticed that when the ORDER BY is a grouping key (which to be honest doesn't seem to make much sense to me), the sort key is duplicated, as demonstrated by one of the modified tests (partition_aggregate.sql). This leads to additional sort nodes being added when there is no necessity to do so. In the case of sort and index pathes, the duplicate keys are not considered, I think the same should apply here. It means the logic for appending the order by pathkeys to the existing group by pathkeys would ideally need to remove the redundant group by keys from the order by keys, considering this example: regression=# explain select sum(unique1 order by ten, two), sum(unique1 order by four), sum(unique1 order by two, four) from tenk2 group by ten; QUERY PLAN ------------------------------------------------------------------------ GroupAggregate (cost=1109.39..1234.49 rows=10 width=28) Group Key: ten -> Sort (cost=1109.39..1134.39 rows=10000 width=16) Sort Key: ten, ten, two -> Seq Scan on tenk2 (cost=0.00..445.00 rows=10000 width=16) We would ideally like to sort on ten, two, four to satisfy the first and last aggref at once. Stripping the common prefix (ten) would eliminate this problem. Also, existing regression tests cover the first problem (order by a grouping key) but I feel like they should be extended with a case similar as the above to check which pathkeys are used in the "multiple ordered aggregates + group by" cases. -- Ronan Dunklau