pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Alexey Dokuchaev <danfe@nsu.ru>
To: pgsql-general@postgresql.org
Subject: Re: Aggregate functions with FROM clause and ROW_COUNT diagnostics
Date: Mon, 21 May 2018 07:35:06 -0700
Message-ID: <43a358da-a0a2-9965-c3c6-02a19e0a3561@aklaver.com> (raw)
In-Reply-To: <20180521125441.GA85087@regency.nsu.ru>
References: <20180521125441.GA85087@regency.nsu.ru>

On 05/21/2018 05:54 AM, Alexey Dokuchaev wrote:
> 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,

Off the top of my head:

  SELECT count(*) as ct, foo, bar, baz ...

retcode = result ->'ct'

> 
> ./danfe
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (9+ messages)  latest in thread

Message-ID: <43a358da-a0a2-9965-c3c6-02a19e0a3561@aklaver.com>
Permalink:  ../43a358da-a0a2-9965-c3c6-02a19e0a3561@aklaver.com/
Also on:    postgresql.org/message-id/43a358da-a0a2-9965-c3c6-02a19e0a3561@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, danfe@nsu.ru
  Subject: Re: Aggregate functions with FROM clause and ROW_COUNT diagnostics
  In-Reply-To: <43a358da-a0a2-9965-c3c6-02a19e0a3561@aklaver.com>
  References: <20180521125441.GA85087@regency.nsu.ru> <43a358da-a0a2-9965-c3c6-02a19e0a3561@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox