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 1r5gnG-00EWJc-U5 for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Nov 2023 06:33:02 +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 1r5gnF-005JRn-Fc for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Nov 2023 06:33:01 +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 1r5gnF-005JRf-6Q for pgsql-hackers@lists.postgresql.org; Wed, 22 Nov 2023 06:33:01 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r5gnC-007jbV-U0 for pgsql-hackers@postgresql.org; Wed, 22 Nov 2023 06:33:00 +0000 Received: from mail.postgrespro.ru (webmail.mstn.postgrespro.ru [192.168.2.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: a.pyhalov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 155DCE20F0A; Wed, 22 Nov 2023 09:32:58 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1700634778; bh=axxxDLRzgedffNOMTNfHIXboETDj6qLE6i7MaQ1IWoc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:User-Agent: Message-ID:From; b=COrHZEdEXJYHUHvu9I5+qj732/jIqoOL7U3Qa3C35hOaI7OYxPYWZ/FANeXhQECxV mZp+5gr4YYLqnVlqc+q0LLLsd55XEKSgXFticqBZUBWcFJJ50SUnAlPDgdTkAA1K17 WjBqFelb8Pxkr2MKeUhOp4I7Bty17ckMahH7DQhQin7NSkf5C/008ciVP4r6FQI9IJ 5PLijtIBD7KYvaIw54kGMWG5YdOijLFiaE35zNhVzRuTkWsowr7ll/Y/JVU6mZkOeJ EP/FDJMTk0fvdkd2ZWlpXoAwEaELxg/ywC5rW0mTX5XBW0JfwPiaRQ9we4fNYCYI2P NUcaqZJXueO7Q== MIME-Version: 1.0 Date: Wed, 22 Nov 2023 09:32:58 +0300 From: Alexander Pyhalov To: Robert Haas Cc: Bruce Momjian , "Fujii.Yuki@df.MitsubishiElectric.co.jp" , PostgreSQL-development , "Finnerty, Jim" , Andres Freund , Tom Lane , Tomas Vondra , Julien Rouhaud , Daniel Gustafsson Subject: Re: Partial aggregates pushdown In-Reply-To: References: User-Agent: Roundcube Webmail/1.4.11 Message-ID: <8175ddeb6d417d8a1f91e667fef77abf@postgrespro.ru> X-Sender: a.pyhalov@postgrespro.ru Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Robert Haas писал 2023-11-21 20:16: >> > I don't think the patch does a good job explaining why HAVING, >> > DISTINCT, and ORDER BY are a problem. It seems to me that HAVING >> > shouldn't really be a problem, because HAVING is basically a WHERE >> > clause that occurs after aggregation is complete, and whether or not >> > the aggregation is safe shouldn't depend on what we're going to do >> > with the value afterward. The HAVING clause can't necessarily be >> > pushed to the remote side, but I don't see how or why it could make >> > the aggregate itself unsafe to push down. DISTINCT and ORDER BY are a >> > little trickier: if we pushed down DISTINCT, we'd still have to >> > re-DISTINCT-ify when combining locally, and if we pushed down ORDER >> > BY, we'd have to do a merge pass to combine the returned values unless >> > we could prove that the partitions were non-overlapping ranges that >> > would be visited in the correct order. Although that all sounds >> > doable, I think it's probably a good thing that the current patch >> > doesn't try to handle it -- this is complicated already. But it should >> > explain why it's not handling it and maybe even a bit about how it >> > could be handling in the future, rather than just saying "well, this >> > kind of thing is not safe." The trouble with that explanation is that >> > it does nothing to help the reader understand whether the thing in >> > question is *fundamentally* unsafe or whether we just don't have the >> > right code to make it work. >> >> Makes sense. > > Actually, I think I was wrong about this. We can't handle ORDER BY or > DISTINCT because we can't distinct-ify or order after we've already > partially aggregated. At least not in general, and not without > additional aggregate support functions. So what I said above was wrong > with respect to those. Or so I believe, anyway. But I still don't see > why HAVING should be a problem. Hi. HAVING is also a problem. Consider the following query SELECT count(a) FROM t HAVING count(a) > 10 - we can't push it down to foreign server as HAVING needs full aggregate result, but foreign server don't know it. -- Best regards, Alexander Pyhalov, Postgres Professional