pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Alexey Dokuchaev <danfe@nsu.ru>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Aggregate functions with FROM clause and ROW_COUNT diagnostics
Date: Mon, 21 May 2018 10:45:27 -0400
Message-ID: <30939.1526913927@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwaQ_jGJF5OZEvfz=J99BS5i6BeBZO+35RQnK-foBo1EPQ@mail.gmail.com>
References: <20180521125441.GA85087@regency.nsu.ru>
	<CAKFQuwaQ_jGJF5OZEvfz=J99BS5i6BeBZO+35RQnK-foBo1EPQ@mail.gmail.com>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
>> Is it possible to obtain the
>> first ROW_COUNT (after SELECT) without performing it twice?

> ​Not directly, no.  You should execute the inner query to a temporary table
> than perform your counting and json_agg from that.

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.

			regards, tom lane




view thread (9+ messages)  latest in thread

Message-ID: <30939.1526913927@sss.pgh.pa.us>
Permalink:  ../30939.1526913927@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/30939.1526913927@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, danfe@nsu.ru
  Subject: Re: Aggregate functions with FROM clause and ROW_COUNT diagnostics
  In-Reply-To: <30939.1526913927@sss.pgh.pa.us>
  References: <20180521125441.GA85087@regency.nsu.ru> <CAKFQuwaQ_jGJF5OZEvfz=J99BS5i6BeBZO+35RQnK-foBo1EPQ@mail.gmail.com> <30939.1526913927@sss.pgh.pa.us>

* 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