Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBLga-00AVhe-8v for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Dec 2023 21:13:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rBLfa-002dpX-Lo for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Dec 2023 21:12:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBLfa-002dpP-Bw for pgsql-hackers@lists.postgresql.org; Thu, 07 Dec 2023 21:12:30 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBLfT-00AcVO-Ch for pgsql-hackers@postgresql.org; Thu, 07 Dec 2023 21:12:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2023062407; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-ID:Content-Description; bh=eq1xQTRpShKpJAIqJH74wiYg5QGjHx1zv7jix+2w8r0=; b=1nNWg/q+rMQx4Rb7t6N7ZDGir7 J9ajbjA9F7GXHSQYFkd4Rs8wcVNqfEM21RLOptE60svA+AtKLmENDkl/8acfDW8BhMvn8WLd4JAbY PCzAO+bvcNtNdnQUqUoPEM7+suwAhZZq6p4C7dUBeCQZtZkRTm4T3UnUkDafuNGMYvv/Hg5rZQIiT Nkol3HhDdiDk3S2rdu1xdnpxgmoKrSW3cCWxil1NJCdCbTsecmpsYllxfbye+9sVUD0GUORndLeSC 1HYLrCPCquwSpAxhiuCWIJ5OSjkFx1NUuEkfSTpTgewDarlsfq1oYqYxKqeJgbMtucTAE1ZeNMfPh 0mOLGh9w==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1rBLfO-00AgBZ-07; Thu, 07 Dec 2023 16:12:18 -0500 Date: Thu, 7 Dec 2023 16:12:18 -0500 From: Bruce Momjian To: Robert Haas Cc: "Fujii.Yuki@df.MitsubishiElectric.co.jp" , PostgreSQL-development , Tom Lane , Alexander Pyhalov , Stephen Frost , "Finnerty, Jim" , Andres Freund , Tomas Vondra , Julien Rouhaud , Daniel Gustafsson Subject: Re: [CAUTION!! freemail] Re: Partial aggregates pushdown Message-ID: References: <4012625.1701120204@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Dec 7, 2023 at 09:56:08AM -0500, Robert Haas wrote: > On Wed, Dec 6, 2023 at 7:11 PM Fujii.Yuki@df.MitsubishiElectric.co.jp > wrote: > > I would be grateful if we can resolve this issue gradually. I would also like to continue the discussion if possible in the future. > > I think that would be good. Thanks for your work on this. It is a hard problem. Agreed. First, Robert is right that this feature is long overdue. It might not help many of our existing workloads, but it opens us up to handling new, larger workloads. Second, the patch already has a mechanism to check the remote server version to see if it is the same or newer. Here is the version check documentation patch: check_partial_aggregate_support (boolean) If this option is false, postgres_fdw always uses partial aggregate pushdown by assuming that each built-in aggregate function has a partial aggregate function defined on the remote server. If this option is true, local aggregates whose partial computation function references itself are assumed to exist on the remote server. If not, during query planning, postgres_fdw will connect to the remote server and retrieve the remote server version. If the remote version is the same or newer, partial aggregate functions will be assumed to exist. If older, postgres_fdw checks that the remote server has a matching partial aggregate function before performing partial aggregate pushdown. The default is false. There is also an extension list that specifies which extension-owned functions can be pushed down; from the doc patch: To reduce the risk of misexecution of queries, WHERE clauses and aggregate expressions are not sent to the remote server unless they only use data types, operators, and functions that are built-in or belong to an extension that is listed in the foreign server's extensions option. Third, we already have a way of creating records for tables: SELECT pg_language FROM pg_language; pg_language ------------------------------------------- (12,internal,10,f,f,0,0,2246,) (13,c,10,f,f,0,0,2247,) (14,sql,10,f,t,0,0,2248,) (13576,plpgsql,10,t,t,13573,13574,13575,) And we do have record input functionality: CREATE TABLE test (x int, language pg_language); INSERT INTO test SELECT 0, pg_language FROM pg_language; SELECT * FROM test; x | language ---+------------------------------------------- 0 | (12,internal,10,f,f,0,0,2246,) 0 | (13,c,10,f,f,0,0,2247,) 0 | (14,sql,10,f,t,0,0,2248,) 0 | (13576,plpgsql,10,t,t,13573,13574,13575,) (4 rows) However, functions don't have pre-created records, and internal functions don't see to have an SQL-defined structure, but as I remember the internal aggregate functions all take the same internal structure, so I guess we only need one fixed input and one output that would output/input such records. Performance might be an issue, but at this point let's just implement this and measure the overhead since there are few/any(?) other viable options. Fourth, going with #2 where we do the pushdown using an SQL keyword also allows extensions to automatically work, while requiring partial aggregate functions for every non-partial aggregate will require work for extensions, and potentially lead to more version mismatch issues. Finally, I am now concerned that this will not be able to be in PG 17, which I was hoping for. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you.