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 1fKlfy-0007Tl-9b for pgsql-general@arkaria.postgresql.org; Mon, 21 May 2018 14:20:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fKlfx-0003PE-0j for pgsql-general@arkaria.postgresql.org; Mon, 21 May 2018 14:20:37 +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 1fKk2n-00022T-4W for pgsql-general@lists.postgresql.org; Mon, 21 May 2018 12:36:05 +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 1fKk2f-0005ng-Dn for pgsql-general@postgresql.org; Mon, 21 May 2018 12:36:04 +0000 Received: from [84.237.50.47] (helo=regency.nsu.ru) by mx.nsu.ru with esmtp (Exim 4.72) (envelope-from ) id 1fKk2c-0004vS-3b for pgsql-general@postgresql.org; Mon, 21 May 2018 19:35:54 +0700 Received: from regency.nsu.ru (localhost [127.0.0.1]) by regency.nsu.ru (8.14.2/8.14.2) with ESMTP id w4LCskfG086628 for ; Mon, 21 May 2018 18:54:46 +0600 (NOVT) (envelope-from danfe@regency.nsu.ru) Received: (from danfe@localhost) by regency.nsu.ru (8.14.2/8.14.2/Submit) id w4LCsfoK086581 for pgsql-general@postgresql.org; Mon, 21 May 2018 19:54:41 +0700 (+07) (envelope-from danfe) Date: Mon, 21 May 2018 19:54:41 +0700 From: Alexey Dokuchaev To: pgsql-general@postgresql.org Subject: Aggregate functions with FROM clause and ROW_COUNT diagnostics Message-ID: <20180521125441.GA85087@regency.nsu.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 Hi, I'm seeing somewhat confusing results here with 9.6.8, and cannot find the answer in the docs or google. I'm returning JSON array (or any array, it does not make a difference) from my plpgsql function like this: OUT retcode int, OUT result json) . . . result := json_agg(_) FROM ( SELECT foo, bar, baz ... FROM t1, t2, t3 WHERE ...) AS _; -- this works fine GET DIAGNOSTICS retcode = ROW_COUNT; -- always returns 1 I'd expected `retcode' to contain the number of SELECT'ed rows, but it is something else (always 1). Apparently, aggregation functions like json_agg()/array_agg() mangle the ROW_COUNT from the inner SELECT (the one I'm interested in). Is this expected and correct behavior? Is it possible to obtain the first ROW_COUNT (after SELECT) without performing it twice? Thanks, ./danfe