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 1oNfdv-00076u-QG for pgsql-general@arkaria.postgresql.org; Mon, 15 Aug 2022 19:20:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oNfdt-0001SF-5a for pgsql-general@arkaria.postgresql.org; Mon, 15 Aug 2022 19:20:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oNfds-0001Re-Nz for pgsql-general@lists.postgresql.org; Mon, 15 Aug 2022 19:20:52 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oNfdp-0000En-TX for pgsql-general@lists.postgresql.org; Mon, 15 Aug 2022 19:20:51 +0000 Received: by mail-pg1-x533.google.com with SMTP id 24so7289657pgr.7 for ; Mon, 15 Aug 2022 12:20:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc; bh=SyBvNU6Bsc7PdijcefzQJgsvDEUAnIpGkQ3abLLK82w=; b=L5KZPRCcPbA8UB9dZppFu4c/NJBStNk8KaukDmL/cIXeWI882k8OMLGDYEEiqpu6Qa eyXSf5bxpD+AAUkYfR9+UzVQXIqPc6Z+W2R82sBbArpWStTpjCI2eMf0Y63B9C9WbVJd 4msxyThDWCGxej8tmkxs06tSnt5B0pvCSAIR8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc; bh=SyBvNU6Bsc7PdijcefzQJgsvDEUAnIpGkQ3abLLK82w=; b=xHJK4ixOviN3y/WPowq7S3Dvu1LB9TrhQqhFBSJYuVo05r4XO4v1k9u4rQuekhGXwx fPzQyVhtJY3n18W/gCF8Qws9cm6oBblFZkdbzFd3datOZRfOiJ+a2H5dwnNXLHyMEDsQ GQAaJpc1Mwd6HBEbsmnY0RXp2fgVTodi571/1RKgNGvgrg9IVCGKywrIBlqi+Xj9zn0Z f98tla0+4SrqNRrOQh8mbF5dSvrdhtZjDU+vw5Ifg/9iY7PFk+bzlURMF7ZvNzoCBVgb yOfpWm4m1ojQPu3l8Xigjl9HW+USe51QD+aoBaj7skj8VYLh5KfH73Kjhdpgx4Ny0+yG FfFg== X-Gm-Message-State: ACgBeo3e9m9O2mnVVabDXUFsYcqmY4J+Dd3xk8/qYUst9Y4YwGszo0zG WTG9Tz5nOGlwJlNLEQ8Dje9dFw== X-Google-Smtp-Source: AA6agR5tPvFMDYQO8R5ixg3eGi5rL0O78giaTtbrPGWN8uwzRtr/joX5Lzif23YvEZMKLXAKx/WZtw== X-Received: by 2002:a05:6a00:a05:b0:534:b1ad:cfac with SMTP id p5-20020a056a000a0500b00534b1adcfacmr9177400pfh.35.1660591246622; Mon, 15 Aug 2022 12:20:46 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:14b4:b48b:3c45:7f74]) by smtp.gmail.com with ESMTPSA id fs1-20020a17090af28100b001f246f2a423sm4967101pjb.17.2022.08.15.12.20.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Aug 2022 12:20:46 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_A42F11B8-5E3F-4578-82E2-DA9BE57026B6" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) 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 In-Reply-To: <20220815181618.bkpg7jwf546qlvlb@hjp.at> Cc: pgsql-general list , "Peter J. Holzer" , "David G. Johnston" To: pedz@easesoftware.com References: <75BBE3C5-D495-47B8-A81C-6359391E3B99@easesoftware.com> <20220815181618.bkpg7jwf546qlvlb@hjp.at> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_A42F11B8-5E3F-4578-82E2-DA9BE57026B6 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 >>> 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 >> david.g.johnston@gmail.com wrote: >>=20 >> Use count as a window function. >=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]. 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 :=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; foreach r in array results loop z :=3D 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"=E2=80=94and 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? --Apple-Mail=_A42F11B8-5E3F-4578-82E2-DA9BE57026B6 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
pedz@easesoftware.com wrote:

Currently I=E2=80=99m doing this with = two queries such as:

    SELECT COUNT(*) FROM = table WHERE =E2=80=A6. expression =E2=80=A6
    SELECT * = FROM table WHERE =E2=80=A6. expression =E2=80=A6

But this requires two = queries. Is there a way to do the same thing with just one = quer?


Use count as a window = function.

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 :=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;

  foreach r in array results loop
    z :=3D 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"=E2=80=94and 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?


= --Apple-Mail=_A42F11B8-5E3F-4578-82E2-DA9BE57026B6--