Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oO3Ok-00044j-I4 for pgsql-general@arkaria.postgresql.org; Tue, 16 Aug 2022 20:42:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oO3Oj-0007gT-DE for pgsql-general@arkaria.postgresql.org; Tue, 16 Aug 2022 20:42:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oO3Oj-0007gK-2M for pgsql-general@lists.postgresql.org; Tue, 16 Aug 2022 20:42:49 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oO3Oc-000509-EX for pgsql-general@lists.postgresql.org; Tue, 16 Aug 2022 20:42:47 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id E6592783F; Tue, 16 Aug 2022 22:42:38 +0200 (CEST) Date: Tue, 16 Aug 2022 22:42:38 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Can I get the number of results plus the results with a single query? Message-ID: <20220816204238.sfrtsl73z4yoc2d2@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: <75BBE3C5-D495-47B8-A81C-6359391E3B99@easesoftware.com> <20220815181618.bkpg7jwf546qlvlb@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="mqsfs2xa56m235cq" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --mqsfs2xa56m235cq Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2022-08-15 12:20:44 -0700, Bryn Llewellyn wrote: >=20 > pedz@easesoftware.com wrote: >=20 > Currently I=E2=80=99m doing this with two queries such as: >=20 > SELECT COUNT(*) FROM table WHERE =E2=80=A6. expression = =E2=80=A6 > SELECT * FROM table WHERE =E2=80=A6. expression =E2=80=A6 >=20 > But this requires two queries. Is there a way to do the same = thing > with just one quer? >=20 >=20 > david.g.johnston@gmail.com wrote: >=20 > Use count as a window function. >=20 >=20 > hjp-pgsql@hjp.at wrote: >=20 > 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: >=20 > create table t(k serial primary key, v int not null); > insert into t(v) values (7), (19), (42), (57), (100), (200), (300); >=20 > create function f() > returns table(z text) > language plpgsql > stable > as $body$ > declare > r int not null :=3D 0; > results constant int[] :=3D > ( > select array_agg(v order by v) from t where v < 100 > ); > begin > z :=3D '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.=20 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 --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --mqsfs2xa56m235cq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmL8ATkACgkQ8g5IURL+ KF1Sxw//UbXwGudOcAHL5caCvUUww7+RLny2jkaIM0itvMJR+cR5Enoj0ad54dWD l7r8ZkDJV4cyMu/CNtKUxk0X9qDjOaSrWyjJ4Rs+kL5ovdUNTq6wzYUqIRSbuCgm 4vjyiaO4Bo+fierhwKuu4F2gjiEGgjK82a4pK175DiRuBEC9HJ1eT7mb9gjQWhgN VB7U+NXKz+l21gQ0rDxBiLEXt6rhDg3ecMjHgDolqF+fbXcFpPeTg3zaEcDcGH6g 0+XtfuITem3B20VBPKKTfEnh0eKIZoY2A6wydALsqHEhLNercpUrmLyp7eEMamVV urTZS3OoIVvsGDXinX6marzPEOLGcda3sYRkP/1U/umEPvrZSM2JqJnM/KFuvj4x p1Ft49l0OZ1T9FirZpns3/cx5z5MK4flPCB8V3/qk0zb4DyFo+najN5YQPHOqbKT OzvoSR+bT+2QuKiX8FRv3Ehorx21V7h/l6xcgzqberV7zRY+skhf/RUTlU7y0jTG DaIp+bUaIOyKP9vbVFg/5gupOxU9XeXkTkbWGYPbG0zJLLt1+zqmXDcBpJR++oeM cdcgYd0LpB7ba8PPkmmvhWHM8U4q2zRTl/toSbzwPGAjbbgkMuWNhreBcPWgkrBw oM+Q61NheaoGBrwHiw12Kg8qE88Rm1CKHAMpRwd9HNB36eOZc4w= =oP+H -----END PGP SIGNATURE----- --mqsfs2xa56m235cq--