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 1pHZeP-0007wN-JO for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Jan 2023 00:16:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pHZeM-000325-PW for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Jan 2023 00:16:26 +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 1pHZeM-00031w-Dp for pgsql-hackers@lists.postgresql.org; Tue, 17 Jan 2023 00:16:26 +0000 Received: from mail-qk1-x72c.google.com ([2607:f8b0:4864:20::72c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pHZeJ-0005hZ-UX for pgsql-hackers@lists.postgresql.org; Tue, 17 Jan 2023 00:16:25 +0000 Received: by mail-qk1-x72c.google.com with SMTP id pa22so15261219qkn.9 for ; Mon, 16 Jan 2023 16:16:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=6k+oPMZ7oVGj5/L8swYVqGy85g1NW8JcqjbvSMKIu7M=; b=XzYisLM+zXqLHH7q7XzcxZiwCJDpjzRD+8QVxcKOnzOayGHVWdEDRdGeb3am1FCZnc G7mP88Vk2DyxxQMB/qowiR9HUpU0nqdqAxKybu5AFaswz2OSIw9kyGeuizrNA6artbHg H4fxqlf1vnmT7tvTIRYlxwa1uGilIqPIzrqKOpOUXG+YjN0yyhKnFuCJhEPHilFWyRFa AfbF8vysGticDqFqYZAkg7HBpUjXxQJTvAlqBsaeZIpTgj+arzz2ggm1rpWP/gULhQWe +gX3JEBSuKFFmNS7BVk0BHgtSoCsucb3tMgXfhD1+LK4PhE+TiZGkV7HFPdW6EAAKPgq ppmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6k+oPMZ7oVGj5/L8swYVqGy85g1NW8JcqjbvSMKIu7M=; b=zKY98DHxkCqx0QATJnRKUwcv7a053KegEab4pcPAWKWEHjCaWXcJORKWdcKDdvQ1UZ 0A6ArugiaSoA+Ukq3j5jrru0hrMmCHH+B2ZsACkqvPqLis2+juQO+DSbTcQs1P3UNr8x zjgFMxljx8Z6zzBqlUSeTNAd/ZJUYccFEtSdPzBN4zNZbunl/wY97UPCKROVbaBFY0+R rzLuYQDVgJr6RCwyNiN7F9Kkq0SuLd0AllTbE7xVgHlpeMjc5p0dInoGT5KhMV5qT2fB 1VWLiWV9mpru4hZkT+kUwcCf3jOdgQcFW5Z6TmCfQSNT99ojm03z3fOt4aK/6sQzKcpA BrhA== X-Gm-Message-State: AFqh2koJaG0x+b53rqNuO8Z0FODDCDOq/1j/xFmc1dtTXHfVym2ZEUgv T+hOUgYAuSQlGrgfTPo8TG9bpFDlEIkSvlxEC2Q= X-Google-Smtp-Source: AMrXdXuIsSP0nWfYE8ngN3I+TmYRycQDTjqIlKyQUajZOPmh0WyQyqSWktdzpy1KlvboJtKB/pjIgNT5EgX6TIkZfDg= X-Received: by 2002:a05:620a:535d:b0:706:94f2:bd66 with SMTP id op29-20020a05620a535d00b0070694f2bd66mr47227qkn.493.1673914582110; Mon, 16 Jan 2023 16:16:22 -0800 (PST) MIME-Version: 1.0 References: <3163474.aeNJFYEL58@aivenronan> <9f61ddbf-2989-1536-b31e-6459370a6baa@postgrespro.ru> <2202180.iZASKD2KPV@aivenlaptop> <416296.1673411524@sss.pgh.pa.us> In-Reply-To: From: Dean Rasheed Date: Tue, 17 Jan 2023 00:16:10 +0000 Message-ID: Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates To: David Rowley Cc: Tom Lane , Richard Guo , Ronan Dunklau , Justin Pryzby , Pavel Luzanov , pgsql-hackers@lists.postgresql.org, Ranier Vilela Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 11 Jan 2023 at 05:24, David Rowley wrote: > > I'm wondering if 1349d279 should have just never opted to presort > Aggrefs which have volatile functions so that the existing behaviour > of unordered output is given always and nobody is fooled into thinking > this works correctly only to be disappointed later when they add some > other aggregate to their query or if we should fix both. Certainly, > it seems much easier to do the former. > I took a look at this, and I agree that the best solution is probably to have make_pathkeys_for_groupagg() ignore Aggrefs that contain volatile functions. Not only is that the simplest solution, preserving the old behaviour, I think it's required for correctness. Aside from the fact that I don't think such aggregates would benefit from the optimisation introduced by 1349d279, I think it would be incorrect if there was more than one such aggregate having the same sort expression, because I think that volatile sorting should be evaluated separately for each aggregate. For example: SELECT string_agg(a::text, ',' ORDER BY random()), string_agg(a::text, ',' ORDER BY random()) FROM generate_series(1,3) s(a); string_agg | string_agg ------------+------------ 2,1,3 | 3,2,1 (1 row) so pre-sorting wouldn't be right (or at least it would change existing behaviour in a surprising way). Regards, Dean