From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Can I get the number of results plus the results with a single query?
Date: Tue, 16 Aug 2022 22:42:38 +0200
Message-ID: <20220816204238.sfrtsl73z4yoc2d2@hjp.at> (raw)
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>
On 2022-08-15 12:20:44 -0700, Bryn Llewellyn wrote:
>
> 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].
That's not quite what I meant. I meant "I don't think there can be what
you want 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;
I may be missing something but I don't see how this solves the problem.
The OP wants some kind of progress indicator. To be useful, such an
indicator should be approximately linear in time. I.e. if your query
returns 10000 rows in 5 minutes (yes, that's slow, but you don't need a
progress bar for fast queries), it should display "0/10000" after 0
seconds, "33/10000" after 1 second, "2000/10000" after 1 minute, etc.
That ideal is of course unrealistic, it's quite ok if it displays
"0/unknown" fpr a few seconds and speeds up and slows down during
execution. But if it displays "0/unknown" for 4 minutes and 55 seconds
and then counts up to 10000 during the last 5 seconds, the progress
indicator is useless.
You are stuffing the whole result into an array and THEN counting the
number of elements. So when you get to the count all of the work (except
sending the result to the client) is already done, so there is little
point in displaying a progress indicator.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Attachments:
signature.asc (application/pgp-signature, 832B) download
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: hjp-pgsql@hjp.at
Subject: Re: Can I get the number of results plus the results with a single query?
In-Reply-To: <20220816204238.sfrtsl73z4yoc2d2@hjp.at>
References: <75BBE3C5-D495-47B8-A81C-6359391E3B99@easesoftware.com> <20220815181618.bkpg7jwf546qlvlb@hjp.at> <C6C274DF-62A3-4E54-AC83-F0FB3D7017EC@yugabyte.com> <20220816204238.sfrtsl73z4yoc2d2@hjp.at>
* 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