Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fKnWe-0006jn-2v for pgsql-general@arkaria.postgresql.org; Mon, 21 May 2018 16:19:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fKnWc-0005Mx-HL for pgsql-general@arkaria.postgresql.org; Mon, 21 May 2018 16:19:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fKnNz-00083e-Ks for pgsql-general@lists.postgresql.org; Mon, 21 May 2018 16:10:11 +0000 Received: from mx.nsu.ru ([84.237.50.39]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fKnNs-0002Gp-HJ for pgsql-general@postgresql.org; Mon, 21 May 2018 16:10:10 +0000 Received: from Debian-exim by mx.nsu.ru with local (Exim 4.72) (envelope-from ) id 1fKnNn-0007hs-CR for pgsql-general@postgresql.org; Mon, 21 May 2018 23:09:59 +0700 Received: from [84.237.50.47] (helo=regency.nsu.ru) by mx.nsu.ru with esmtp (Exim 4.72) (envelope-from ) id 1fKnNX-0007Vu-Dx; Mon, 21 May 2018 23:09:44 +0700 Received: from regency.nsu.ru (localhost [127.0.0.1]) by regency.nsu.ru (8.14.2/8.14.2) with ESMTP id w4LGSaZ8000349; Mon, 21 May 2018 22:28:36 +0600 (NOVT) (envelope-from danfe@regency.nsu.ru) Received: (from danfe@localhost) by regency.nsu.ru (8.14.2/8.14.2/Submit) id w4LGSV90000306; Mon, 21 May 2018 23:28:31 +0700 (+07) (envelope-from danfe) Date: Mon, 21 May 2018 23:28:31 +0700 From: Alexey Dokuchaev To: Tom Lane Cc: "David G. Johnston" , pgsql-general Subject: Re: Aggregate functions with FROM clause and ROW_COUNT diagnostics Message-ID: <20180521162831.GA97361@regency.nsu.ru> References: <20180521125441.GA85087@regency.nsu.ru> <30939.1526913927@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <30939.1526913927@sss.pgh.pa.us> User-Agent: Mutt/1.4.2.1i X-KLMS-Rule-ID: 3 X-KLMS-Message-Action: skipped X-KLMS-AntiSpam-Status: not scanned, whitelist X-KLMS-AntiPhishing: not scanned, whitelist X-KLMS-AntiVirus: Kaspersky Security 8.0 for Linux Mail Server, version 8.0.1.705, not scanned, whitelist List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, May 21, 2018 at 10:45:27AM -0400, Tom Lane wrote: > OP could do something like > > declare r record; > ... > select json_agg(_) as j, count(*) as c INTO r FROM ( > SELECT foo, bar, baz ... > FROM t1, t2, t3 WHERE ...) AS _; > > This would be slightly more expensive than doing only the one aggregate, > but it should beat anything involving a temp table. Thanks, I've arrived at the same solution (using local RECORD) eventually. It works as intended, but I still need to assign OUT parameters by hand: retcode := tmp.c; result := tmp.j; I'd love to get rid of OUT parameters and return RECORD directly (it also would make the code more "functional" in style), but then I'd be getting this annoying ``a column definition list is required for functions returning "record"'' error which I don't know how to avoid. Using OUT's simplifies things for callers (they don't have to annotate the types by hand) while still preserving type safety. On Mon, May 21, 2018 at 07:30:44AM -0700, David G. Johnston wrote: > > ... > > Is this expected and correct behavior? > > Yes, the query returned only one row, with a single json column. You > wrote the equivalent of: > > SELECT json_agg(...) FROM ... INTO result; > > And you are getting the count of the top-most select (which is implied > in the syntax that you used). I see, thanks; I've neglected the fact that there's still another implicit SELECT being involved. ./danfe