pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryn Llewellyn <bryn@yugabyte.com>
To: pedz@easesoftware.com
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Cc: Peter J. Holzer <hjp-pgsql@hjp.at>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Subject: Re: Can I get the number of results plus the results with a single query?
Date: Mon, 15 Aug 2022 12:20:44 -0700
Message-ID: <C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.com> (raw)
In-Reply-To: <20220815181618.bkpg7jwf546qlvlb@hjp.at>
References: <75BBE3C5-D495-47B8-A81C-6359391E3B99@easesoftware.com>
	<20220815181618.bkpg7jwf546qlvlb@hjp.at>

>>> pedz@easesoftware.com <mailto:pedz@easesoftware.com> wrote:
>>> 
>>> Currently I’m doing this with two queries such as:
>>> 
>>>     SELECT COUNT(*) FROM table WHERE …. expression …
>>>     SELECT * FROM table WHERE …. expression …
>>> 
>>> But this requires two queries. Is there a way to do the same thing with just one quer?
>> 
>> david.g.johnston@gmail.com wrote:
>> 
>> Use count as a window function.
> 
> hjp-pgsql@hjp.at wrote:
> 
> I don't think there can be [a way to do the same thing with just one query].

How about this:

create table t(k serial primary key, v int not null);
insert into t(v) values (7), (19), (42), (57), (100), (200), (300);

create function f()
  returns table(z text)
  language plpgsql
  stable
as $body$
declare
  r int not null := 0;
  results constant int[] :=
    (
      select array_agg(v order by v) from t where v < 100
    );
begin
  z := 'Count(*): '||cardinality(results);    return next;

  foreach r in array results loop
    z := r::int;                              return next;
  end loop;
end;
$body$;

select f();

It suns without error and gets this result:

 Count(*): 4
 7
 19
 42
 57

Is this what you want, Perry? B.t.w., your second "count(*)" might give a misleading answer unless you use "repeatable read" or "serializable"—and it's generally a good plan to avoid those isolation levels unless your use case forces no other choice.

I've never heard anybody say that this "bulk collect" approach (using Oracle Database's vocabulary) harms performance. Does anybody think that it might?

view thread (8+ messages)  latest in thread

Message-ID: <C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.com>
Permalink:  ../C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.com/
Also on:    postgresql.org/message-id/C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.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: bryn@yugabyte.com, pedz@easesoftware.com, hjp-pgsql@hjp.at, david.g.johnston@gmail.com
  Subject: Re: Can I get the number of results plus the results with a single query?
  In-Reply-To: <C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.com>
  References: <75BBE3C5-D495-47B8-A81C-6359391E3B99@easesoftware.com> <20220815181618.bkpg7jwf546qlvlb@hjp.at> <C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.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