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 1nZcGK-0006lV-Qn for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Mar 2022 17:37:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nZcFK-0005Fj-9q for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Mar 2022 17:36:38 +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 1nZcFK-0005Fa-0B for pgsql-hackers@lists.postgresql.org; Wed, 30 Mar 2022 17:36:38 +0000 Received: from mail-qv1-f47.google.com ([209.85.219.47]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nZcFG-00027p-Vr for pgsql-hackers@lists.postgresql.org; Wed, 30 Mar 2022 17:36:36 +0000 Received: by mail-qv1-f47.google.com with SMTP id hu11so17413930qvb.7 for ; Wed, 30 Mar 2022 10:36:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=4QT1MqgynvVlBBkB6ogz8ryPdOikG6CQyLQLz5RHJqQ=; b=RrTja1o/GjAYGTtaT0nS1qW6+3Fwl6o9YheADOwcH8bIYFv/TAo6kB8d985xttXgP8 tQ3IgxEmzuywZb7VfS/oSeW3zAHUkp6Q4bbN/CBt/lgV1QYAJxqhwaQPKiXSV1WqUGec kIRpXFU12No8naNbHHGwDTohWPsywuEKatmQAMQVg39r9eoTCvuRq7x4Sz+DOfNqrwEQ 8q15lNC+4aiSIj1dschmRj7F9SdNkmDCyhT3JQDC2gI7TuYzFRJn21BiAxB54H91BePc dnD2I6BMe31erWAxoycaiWP66mPXoc8fiFElJjA0tcFDiJZqB+kdkan/Spo71DHg6oQn 3ItA== X-Gm-Message-State: AOAM530j9ywyugErZOalmKepC31AGt1BkXvOdByR7aXcryFKWaGz8PH/ NEs/Bz8q9UZuvW7asQKin3hBi4gHJAC8/lfmz5eI3Vw7 X-Google-Smtp-Source: ABdhPJzN3UuSsZqQtd8XeG8PCHtlI9qNsTE6+sIa2KAtDFdCHj5rMisP/0+tkrRCS4m+hUJ0jlZF2LzMIT2dW9jMjxo= X-Received: by 2002:ad4:5ca7:0:b0:441:2a2a:1a76 with SMTP id q7-20020ad45ca7000000b004412a2a1a76mr32145033qvh.66.1648661793530; Wed, 30 Mar 2022 10:36:33 -0700 (PDT) MIME-Version: 1.0 References: <4493999.1py09z8qHF@aivenronan> <3163474.aeNJFYEL58@aivenronan> In-Reply-To: <3163474.aeNJFYEL58@aivenronan> From: Greg Stark Date: Wed, 30 Mar 2022 13:35:57 -0400 Message-ID: Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates To: Ronan Dunklau Cc: David Rowley , PostgreSQL Developers , Ranier Vilela Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This patch is now failing in the sqljson regression test. It looks like it's just the ordering of the elements in json_arrayagg() calls which may actually be a faulty test that needs more ORDER BY clauses rather than any issues with the code. Nonetheless it's something that needs to be addressed before this patch could be applied. Given it's gotten some feedback from Ronan and this regression test failure I'll move it to Waiting on Author but we're near the end of the CF and it'll probably be moved forward soon. On Thu, 4 Nov 2021 at 04:00, Ronan Dunklau wrote: > > Le jeudi 22 juillet 2021, 10:42:49 CET Ronan Dunklau a =C3=A9crit : > > Le jeudi 22 juillet 2021, 09:38:50 CEST David Rowley a =C3=A9crit : > > > On Thu, 22 Jul 2021 at 02:01, Ronan Dunklau > wrote: > > > > I tested the 0001 patch against both HEAD and my proposed bugfix fo= r > > > > postgres_fdw. > > > > > > > > There is a problem that the ordered aggregate is not pushed down > > > > anymore. > > > > The underlying Sort node is correctly pushed down though. > > > > > > > > This comes from the fact that postgres_fdw grouping path never cont= ains > > > > any > > > > pathkey. Since the cost is fuzzily the same between the pushed-down > > > > aggregate and the locally performed one, the tie is broken against = the > > > > pathkeys. > > > > > > I think this might be more down to a lack of any penalty cost for > > > fetching foreign tuples. Looking at create_foreignscan_path(), I don'= t > > > see anything that adds anything to account for fetching the tuples > > > from the foreign server. If there was something like that then there'= d > > > be more of a preference to perform the remote aggregation so that > > > fewer rows must arrive from the remote server. > > > > > > I tested by adding: total_cost +=3D cpu_tuple_cost * rows * 100; in > > > create_foreignscan_path() and I got the plan with the remote > > > aggregation. That's a fairly large penalty of 1.0 per row. Much bigge= r > > > than parallel_tuple_cost's default value. > > > > > > I'm a bit undecided on how much this patch needs to get involved in > > > adjusting foreign scan costs. The problem is that we've given the > > > executor a new path to consider and nobody has done any proper > > > costings for the foreign scan so that it properly prefers paths that > > > have to pull fewer foreign tuples. This is a pretty similar problem > > > to what parallel_tuple_cost aims to fix. Also similar to how we had t= o > > > add APPEND_CPU_COST_MULTIPLIER to have partition-wise aggregates > > > prefer grouping at the partition level rather than at the partitioned > > > table level. > > > > > > > Ideally we would add the group pathkeys to the grouping path, but t= his > > > > would add an additional ORDER BY expression matching the GROUP BY. > > > > Moreover, some triaging of the pathkeys would be necessary since we= now > > > > mix the sort-in- aggref pathkeys with the group_pathkeys. > > > > > > I think you're talking about passing pathkeys into > > > create_foreign_upper_path in add_foreign_grouping_paths. If so, I > > > don't really see how it would be safe to add pathkeys to the foreign > > > grouping path. What if the foreign server did a Hash Aggregate? The > > > rows might come back in any random order. > > > > Yes, I was suggesting to add a new path with the pathkeys factored in, = which > > if chosen over the non-ordered path would result in an additional ORDER= BY > > clause to prevent a HashAggregate. But that doesn't seem a good idea af= ter > > all. > > > > > I kinda think that to fix this properly would need a new foreign > > > server option such as foreign_tuple_cost. I'd feel better about > > > something like that with some of the people with a vested interest in > > > the FDW code were watching more closely. So far we've not managed to > > > entice any of them with the bug report yet, but it's maybe early days > > > yet. > > > > We already have that in the form of fdw_tuple_cost as a server option i= f I'm > > not mistaken ? It works as expected when the number of tuples is notabl= y > > reduced by the foreign group by. > > > > The problem arise when the cardinality of the groups is equal to the in= put's > > cardinality. I think even in that case we should try to use a remote > > aggregate since it's a computation that will not happen on the local > > server. I also think we're more likely to have up to date statistics > > remotely than the ones collected locally on the foreign tables, and the > > estimated number of groups would be more accurate on the remote side th= an > > the local one. > > I took some time to toy with this again. > > At first I thought that charging a discount in foreign grouping paths for > Aggref targets (since they are computed remotely) would be a good idea, > similar to what is done for the grouping keys. > > But in the end, it's probably not something we would like to do. Yes, the > group planning will be more accurate on the remote side generally (better > statistics than locally for estimating the number of groups) but executin= g the > grouping locally when the number of groups is close to the input's cardin= ality > (ex: group by unique_key) gives us a form of parallelism which can actua= lly > perform better. > > For the other cases where there is fewer output than input tuples, that i= s, > when an actual grouping takes place, adjusting fdw_tuple_cost might be en= ough > to tune the behavior to what is desirable. > > > -- > Ronan Dunklau > > > > --=20 greg