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 1m6Cmv-0007d5-Ec for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Jul 2021 14:01: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 1m6Cmu-00039u-Ev for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Jul 2021 14:01:28 +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 1m6Cmu-00036j-65 for pgsql-hackers@lists.postgresql.org; Wed, 21 Jul 2021 14:01:28 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m6Cmk-0001On-1A for pgsql-hackers@lists.postgresql.org; Wed, 21 Jul 2021 14:01:27 +0000 Received: by mail-wm1-x336.google.com with SMTP id l17-20020a05600c1d11b029021f84fcaf75so3500387wms.1 for ; Wed, 21 Jul 2021 07:01:17 -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=t2KtpdvG2OP08vsyDEGnRjgRD8OlGmvPyuMmb/EimsU=; b=eZBfOQ9kQ4ak3OOOoYoAOW6bFcykQjB7tbU9VK355IZiE/awAnIijDvrnOepsHN4vb mwwaqbtx9FuSDhTwwZsYsdEkz7kA9SbR8RYZgzo9ZXcgqarbO5VckCAaAEOruuSSOT6L IZudZK4ylWM4lpCnybw9VLjwPtEFO+bsKk+XI= 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=t2KtpdvG2OP08vsyDEGnRjgRD8OlGmvPyuMmb/EimsU=; b=A2YoMphj7oElnlwo3np8co/38W1H/pqQjgvwCam6WlmQll8pxnRxoAa+p4WMvWqA90 w8M4aC0IR+neSNCJlzdW0FA232RbnlqapfeTAr+DrmCM+usWcB/Jov5HzwbqwKljMmMw 6URo7ZlKuHdOjS8pM4ffBZ5L7JnyhXDEtws4fb/ROZ9IogIXg+iJdgZ0MEId0EwouGhE uKpaFKxAOiIJkmi/s1wRoQpowy6v2Eiqz+FALKoO+A2A0sY7n8nbpR+Aiij3qJbEHf8l ik6pcgITIyRZFm4qngoaeyGJOI50Q2lmM5gFUI3ZwtdcVOxflPDNF6WXCXRmJdL5KLoi vd5Q== X-Gm-Message-State: AOAM533fEfdQHoqBT3FxX2weQ6k7a0ky9u4NIF3sVD+XWfLqvWJF3bLy UEdi0SMXMYxOBmQYJnaJGXSJBg== X-Google-Smtp-Source: ABdhPJzFAmbd7MwZJkOQtkEFDe6Y8CZFyb5elBeNnOfpR8+PGR69gfbSEUl9DZdOzhAY4pfLP8O4SA== X-Received: by 2002:a1c:4d06:: with SMTP id o6mr37777114wmh.55.1626876075845; Wed, 21 Jul 2021 07:01:15 -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 j2sm5827723wrp.90.2021.07.21.07.01.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jul 2021 07:01:15 -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: Wed, 21 Jul 2021 16:01:03 +0200 Message-ID: <5137746.gn36MdYUqo@aivenronan> Organization: aiven In-Reply-To: References: <4285468.TQqHX9kgJM@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 mercredi 21 juillet 2021, 04:52:43 CEST David Rowley a =E9crit : > Thanks for finding this. I've made a few changes to make this case > work as you describe. Please see attached v6 patches. >=20 > Because I had to add additional code to take the GROUP BY pathkeys > into account when choosing the best ORDER BY agg pathkeys, the > function that does that became a little bigger. To try to reduce the > complexity of it, I got rid of all the processing from the initial > loop and instead it now uses the logic from the 2nd loop to find the > best pathkeys. The reason I'd not done that in the first place was > because I'd thought I could get away without building an additional > Bitmapset for simple cases, but that's probably fairly cheap compared > to building Pathkeys. With the additional complexity for the GROUP > BY pathkeys, the extra code seemed not worth it. >=20 > The 0001 patch is the ORDER BY aggregate code. 0002 is to fix up some > broken regression tests in postgres_fdw that 0001 caused. It appears > that 0001 uncovered a bug in the postgres_fdw code. I've reported > that in [1]. If that turns out to be a bug then it'll need to be fixed > before this can progress. I tested the 0001 patch against both HEAD and my proposed bugfix for=20 postgres_fdw. There is a problem that the ordered aggregate is not pushed down anymore. T= he=20 underlying Sort node is correctly pushed down though.=20 This comes from the fact that postgres_fdw grouping path never contains any= =20 pathkey. Since the cost is fuzzily the same between the pushed-down aggrega= te=20 and the locally performed one, the tie is broken against the pathkeys. Ideally we would add the group pathkeys to the grouping path, but this woul= d=20 add an additional ORDER BY expression matching the GROUP BY. Moreover, some= =20 triaging of the pathkeys would be necessary since we now mix the sort-in- aggref pathkeys with the group_pathkeys. =2D-=20 Ronan Dunklau