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 1pgWmr-0007K7-T8 for pgsql-general@arkaria.postgresql.org; Sun, 26 Mar 2023 20:16:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pgWmp-000103-Cf for pgsql-general@arkaria.postgresql.org; Sun, 26 Mar 2023 20:16:19 +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 1pgWmo-0000zc-FC for pgsql-general@lists.postgresql.org; Sun, 26 Mar 2023 20:16:18 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pgWmg-0006e1-0B for pgsql-general@lists.postgresql.org; Sun, 26 Mar 2023 20:16:17 +0000 Received: by mail-pj1-x1032.google.com with SMTP id lr16-20020a17090b4b9000b0023f187954acso6624008pjb.2 for ; Sun, 26 Mar 2023 13:16:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; t=1679861768; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=FxgzHaPDzsOzX4O/CGzB/brZSt3c8VioEblhzdYehHM=; b=FQtdhF/tAerRASb/mH7Yq1ui1QfZWyt3afRjFEUg51+xma0FZ7k0Q3R8xA4Ha8Ok2g OLDi6EfOrv722CggK+kLJ+KB+G91A5oegeYmMV0ZSgsMFjyFjJMSh/JE3Q+sodOz/XID jG8E2B/cD7z63u9oyw0FeIB3ibg+HSsSW3/yQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679861768; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FxgzHaPDzsOzX4O/CGzB/brZSt3c8VioEblhzdYehHM=; b=y7+M98bTnPNfzy0Uc5Ub2PZmvkJY/HtlxAkp/9hTs0YrRq0o87GpOCyaj2Pg4Dpxm4 V/siEOXPLMEv3XuBK4kny4D7DnqhyFb64m9c6sM62Ld0UBCYlX/kDYGcBOI9klm9veTt 3UMRyqfCfLlItv3B0Tl1ryE5JgFpQbbCILtkbdqe0ysYgVjEINoR0LqlobzV8MPkJRQu 3MBkROUsjyiGk2vW6+dMaQrlSqRT6ayV3XSRXV4qIM9oDJBSZWusC75CtWdJmGHSn1Rw b8zAV2lgApS7AvN1eAfvUBGwgB7F4xoeagiN6l+FoQiJAIKWWmutasU/c6pwFsHJ0XM1 d1vQ== X-Gm-Message-State: AO0yUKUnHg6WeEJFC5thI/jTXNFWgjAY3ltmtWd3Ru//+lty/dfit7d9 cZvkx+8GA3z/HWvLqICxFUIdQA== X-Google-Smtp-Source: AK7set89RfC9wc5MIQkaVVOYvFo5KzW/wCtgNjrxjhInSGDuJc/YchSsT5WChBGS+F6XdDw4ZOsMqA== X-Received: by 2002:a05:6a20:3949:b0:da:e9fa:9c29 with SMTP id r9-20020a056a20394900b000dae9fa9c29mr15150743pzg.26.1679861768366; Sun, 26 Mar 2023 13:16:08 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:59b0:b1b1:2a6a:10c6]) by smtp.gmail.com with ESMTPSA id i13-20020a63584d000000b0050336b0b08csm16171064pgm.19.2023.03.26.13.16.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Mar 2023 13:16:07 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_C1AF5A3F-F17F-4BDC-B1EE-06E3A050F6C6" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.14\)) Subject: Re: Is the PL/pgSQL refcursor useful in a modern three-tier app? Date: Sun, 26 Mar 2023 13:16:06 -0700 In-Reply-To: <3661e718-944f-bac3-a436-f0e38ebf127b@aklaver.com> Cc: Laurenz Albe , pgsql-general list To: Adrian Klaver References: <6A73AAEA-7A9F-42C9-A842-88213FA41003@yugabyte.com> <1C63744C-F4B2-44AB-B9EF-3564D2B04049@yugabyte.com> <898378b8-f60a-8b27-31f6-83882040f5b6@aklaver.com> <58b2a298-9cad-47dc-1d07-0601588553c1@aklaver.com> <8659357C-1DC6-4EB7-9451-3ECB04E6E67D@yugabyte.com> <793675c6b0521c127044ac5b557d98154fcfc7c5.camel@cybertec.at> <3661e718-944f-bac3-a436-f0e38ebf127b@aklaver.com> X-Mailer: Apple Mail (2.3654.120.0.1.14) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_C1AF5A3F-F17F-4BDC-B1EE-06E3A050F6C6 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > adrian.klaver@aklaver.com wrote: >=20 >> bryn@yugabyte.com replied to laurenz.albe@cybertec.at: >>=20 >> Thanks for the link to your SQL file at the line where you get the = row count in the way that you describe... I noted that neither of these = functions has a refcursor formal argument and that, rather, you open = (i.e. create) each of the three cursors that you use within the two = functions that uses them. >=20 > CREATE FUNCTION materialize_foreign_table(...) >=20 > CREATE FUNCTION db_migrate_refresh(...)=20 >=20 > What is not formal about the above? I used the term of art "formal argument" ordinarily to denote what's = listed in parentheses at the start of a subprogram definition. The term = stands in contrast to "actual argument"=E2=80=94meaning the expression = that's used in a subprogram invocation to provide a value for the = corresponding formal argument. Go to this page: dotnettutorials.net/lesson/types-of-function-arguments-in-python/ and search in it for "Example: formal and actual function arguments in = python". The blurb that comes just before the example says what I just = said. It seems to be more common to talk about formal and actual = "parameters". But the PG doc prefers "argument". I didn't at all mean that "formal" is good and that Laurenz's code is = not formal and therefore is bad! So sorry if you took it to mean this.=20= > Though it does not matter as, back to the docs again: >=20 > www.postgresql.org/docs/current/plpgsql-cursors.html >=20 > "All access to cursors in PL/pgSQL goes through cursor variables, = which are always of the special data type refcursor. One way to create a = cursor variable is just to declare it as a variable of type = refcursor..." Though maybe you are trying to differentiate between bound = and unbound refcursor's... Ah... you missed my point entirely. Looks like my prose was opaque. I = was referring to this paradigm as described at the start of the page = that you referenced: =C2=AB A more interesting usage is to return a reference to a cursor that a = function has created, allowing the caller to read the rows. This = provides an efficient way to return large row sets from functions. =C2=BB I needed to know that the definition and use of each "refcursor" = variable, in Laurentz's code, are in the same subprogram because it's = only when this is the case that using an ordinary "select count(*)" and = an ordinary "for" loop, where the "select" is written in place within = the "for" clause, are viable alternatives. > Also order by is not relevant for getting a count. Yes, of course. I know that. (But it does matter in many cases of = results presentation.) I was simply noting that Laurenz=E2=80=99s = use-case didn=E2=80=99t need ordering and that the "select" statements = will be faster than if ordering were needed. Sorry if I implied = something that I didn=E2=80=99t mean too. (The example in Laurenz's = post, = www.cybertec-postgresql.com/en/pagination-problem-total-result-count/ = , does use "order by".) >> I did a little test an saw that this: >>=20 >> move last in cur; >> get diagnostics n =3D row_count; >>=20 >> reported just 1=E2=80=94in contrast to what you used: >>=20 >> move forward all in cur; >=20 > Because they are different actions. The first directly moves to the = last row in the cursor and counts just that row. The second moves = through all the rows left in the cursor and counts all the rows it = traversed. It is spelled out here: >=20 > https://www.postgresql.org/docs/current/sql-move.html >=20 > "The count is the number of rows that a FETCH command with the same = parameters would have returned (possibly zero)." Yes, I'm afraid that I expressed myself poorly again. I meant to say = that while I know nothing about the internals, I can't see how "move = last in cur" can know where the last row is unless it traverses the = entire result set from its current position until it finds no more rows. = And while its doing this, it may just as well count the rows it = traverses. It would seem, therefore, that a single operation that moves = to where you asked for and that tells you how many rows it traversed = would be enough=E2=80=94rather than two that both achieve the same = effect in the same time where one gives you the count of rows traversed = and the other doesn't. But never mind. The simple way to see it is that = the operations simply do what they do=E2=80=94and I won't worry about = how they might have been conceived differently. I did some timing tests. I=E2=80=99ve copied the results and the code at = the end=E2=80=94just for completeness. Here=E2=80=99s what I concluded. (1) (and this is unremarkable), if you can find a way to meet your = requirement with just a single SQL statement, then this is bound to be = best. I chose a simple requirement: read from a table and insert half = the total number of rows into one table and the remainder into another. = This single SQL statement meets that requirement: with chunk(n) as (select count(*)/2 from s.t0), i1 as (insert into s.t1(k, v) select k, v from s.t0 where k < (select = n from chunk) returning k) insert into s.t2(k, v) select k, v from s.t0 where k >=3D (select n from = chunk); Internet search shows that this CTE approach seems to be the popular PG = pattern to achieve what Oracle=E2=80=99s dedicated multi-table "insert" = achieves. On the assumption that the source table suffers constant = concurrent changes from other sessions, the PL/pgSQL loop that meets = this requirement _must_ use the cursor-based approach for getting the = count that we=E2=80=99ve been discussing. I=E2=80=99m glad to have = learned all this because some requirements (e.g. when the source table = traversal has to do DDLs) can=E2=80=99t be met with a single SQL = statement, and so a PL/pgSQL loop must be used. (2) Even though the declaration (in a txn that you commit) materializes = the entire result set, this manages to be a lot faster than doing this = yourself with a temp table like this: with c(n) as ( select count(*) from s.t), i1(n) as ( insert into pg_temp.t_count select n from c returning n) insert into pg_temp.t(k, v, r) select k, v, row_number() over(order by = k) from s.t; create unique index temp_t_k_unq on pg_temp.t(k asc); The index is meant to help "move". Maybe some use cases wouldn=E2=80=99t = need it. Same goes for recording the count in its own one-row, = one-column table. (3) The times to do this: move absolute 0 in cur; move forward all in cur; get diagnostics n =3D row_count; move absolute 0 in cur; and this: move absolute 0 in cur; move last in cur; move absolute 0 in cur; are the same (within the limits of my measurements). But, of course, = there=E2=80=99s no need to use the second alternative. (4) The time to get the count using "move forward all in cur" is about = the same as doing an ordinary "count(*)"=E2=80=94but "move forward" is a = little slower. I'm therefore puzzled by Laurentz's "the result set count = comes for free [with the cursor approach]" in his blog post (x-ref = above). Its appeal seems to me to be limited to its rock-solid = semantics. (5) It's somewhat quicker to move around in a holdable cursor than in a = non-holdable one. But, of course, you pay a price when you create the = former. Here are my timings=E2=80=94all in seconds and all with three digits of = precision. I used PG 15.2 in a Ubuntu VM on my Mac Book. I run psql in = the bare macOS and connect into the VM. On some runs, some times are = higher than usual. I expect that the way the VM handles files has = something to do with this. I chose a run where the timings were pretty = stable to copy here. Insert 10,000,000 rows time: 13.7 =20 Populate temp tables time: 10.6 =20 Native count(*) time: 0.633 0.637 0.677 0.781 Declare "Is-Holdable" time: 1.91=20 Is-Holdable move fwd count time: 0.703 0.700 0.700 0.700 Declare "Not-Holdable" time: 0.000 Not-Holdable move fwd count time: 0.877 0.866 0.864 0.872 Declare "Is-Holdable" time: 1.78=20 Is-Holdable move back & fore time: 0.691 0.695 0.709 0.708 Declare "Not-Holdable" time: 0.000 Not-Holdable move back & fore time: 0.872 0.871 0.874 0.888 Here's the code. If you want to run it, just copy it into a single file = and start it at the psql prompt. You need just to connect as a regular = user that has "create" on the database that you use. = --------------------------------------------------------------------------= ------ -- Create the objects. \c :db :u set search_path =3D pg_catalog, pg_temp; drop schema if exists s cascade; create schema s; create table s.t(k int primary key, v int not null); /* Format the elapsed time since t0 in seconds with three digits of precision.=20 */; create function s.elapsed_time(t0 in double precision) returns text set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare e constant double precision not null :=3D extract(epoch from = clock_timestamp()) - t0; t constant text not null :=3D case when e >=3D 100000.0 then '>=3D 100K' = ||' ' when e >=3D 10000.0 then to_char(round(e/100.0)*100.0, '99,999' = )||' ' when e >=3D 1000.0 then to_char(round(e/10.0)* 10.0, '9,999' = )||' ' when e >=3D 100.0 then to_char(e, '999' = )||' ' when e >=3D 10.0 then to_char(e, '99.9' = )||' ' when e >=3D 1.0 then to_char(e, = '9.99' )||' ' else to_char(e, '0.999') end; begin return lpad(t, 12); end; $body$; create procedure s.insert_table_time(no_of_rows int, t inout text) set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare n_text constant text not null :=3D ltrim(to_char(no_of_rows, = '999,999,999,999')); caption constant text not null :=3D rpad('Insert '||n_text||' = rows time:', 35); t0 double precision not null :=3D 0.0; begin truncate table s.t; t0 :=3D extract(epoch from clock_timestamp()); with g(v) as (select generate_series(1, no_of_rows)) insert into s.t(k, v) select g.v, (g.v)*2 from g; t :=3D caption||s.elapsed_time(t0); end; $body$; create procedure s.populate_temp_tables_time(t inout text) set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare caption constant text not null :=3D rpad('Populate temp tables = time:', 35); t0 double precision not null :=3D 0.0; begin create table pg_temp.t_count(n int); create table pg_temp.t(k int, v int, r int); t0 :=3D extract(epoch from clock_timestamp()); with c(n) as ( select count(*) from s.t), i1(n) as ( insert into pg_temp.t_count select n from c returning n) insert into pg_temp.t(k, v, r) select k, v, row_number() over(order by = k) from s.t; create unique index temp_t_k_unq on pg_temp.t(k asc); t :=3D caption||s.elapsed_time(t0); end; $body$; create procedure s.close_cursor(cur in refcursor) set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare begin close cur; end; $body$; create procedure s.declare_cursor_time(cur_name in text, holdable in = boolean, t inout text) -- set search_path =3D pg_catalog, pg_temp :: Incomparible with = "commit". language plpgsql as $body$ declare hld constant text not null :=3D case holdable when true then 'with hold' else 'without hold' end; slct constant text not null :=3D 'select k, v from s.t order by = k'; decl constant text not null :=3D format('declare %I scroll cursor = %s for '||slct, cur_name, hld); caption constant text not null :=3D rpad('Declare = "'||cur_name||'" time:', 35); t0 constant double precision not null :=3D extract(epoch from = clock_timestamp()); begin execute decl; if holdable then commit; end if; t :=3D caption||s.elapsed_time(t0); end; $body$; create function s.native_count_time(expected_n in int, show_caption in = boolean =3D false) returns text set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare n int not null :=3D 0; caption constant text not null :=3D case show_caption when true then = rpad('Native count(*) time:', 35) else rpad('', = 35) end; t0 constant double precision not null :=3D extract(epoch from = clock_timestamp()); begin n :=3D (select count(*) from s.t); assert n =3D expected_n; return caption||s.elapsed_time(t0); end; $body$; drop function if exists s.count_by_move_time(refcursor, int, boolean) = cascade; create function s.count_by_move_time(cur in refcursor, expected_n in = int, show_caption in boolean =3D false) returns text set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare n int not null :=3D 0; caption constant text not null :=3D case show_caption when true then = rpad(cur::text||' move fwd count time:', 35) else rpad('', = 35) end; t0 constant double precision not null :=3D extract(epoch from = clock_timestamp()); begin move absolute 0 in cur; move forward all in cur; get diagnostics n =3D row_count; move absolute 0 in cur; assert n =3D expected_n; return caption||s.elapsed_time(t0); end; $body$; create function s.move_back_and_fore_time(cur in refcursor, show_caption = in boolean =3D false) returns text set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare caption constant text not null :=3D case show_caption when true then = rpad(cur::text||' move back & fore time:', 35) else rpad('', = 35) end; t0 constant double precision not null :=3D extract(epoch from = clock_timestamp()); begin move absolute 0 in cur; move last in cur; move absolute 0 in cur; return caption||s.elapsed_time(t0); end; $body$; = --------------------------------------------------------------------------= ------ -- Do the timimg. -- Ten thousand rows. \set no_of_rows 10000000 \set Is_Holdable ''''Is-Holdable'''' \set Not_Holdable ''''Not-Holdable'''' \t on \o results.txt call s.insert_table_time(:no_of_rows, null::text); call s.populate_temp_tables_time(null::text); select s.native_count_time(:no_of_rows, true); select s.native_count_time(:no_of_rows); select s.native_count_time(:no_of_rows); select s.native_count_time(:no_of_rows); call s.declare_cursor_time(:Is_Holdable, true, null::text); select s.count_by_move_time(:Is_Holdable, :no_of_rows, true); select s.count_by_move_time(:Is_Holdable, :no_of_rows); select s.count_by_move_time(:Is_Holdable, :no_of_rows); select s.count_by_move_time(:Is_Holdable, :no_of_rows); start transaction; call s.declare_cursor_time(:Not_Holdable, false, null::text); select s.count_by_move_time(:Not_Holdable, :no_of_rows, true); select s.count_by_move_time(:Not_Holdable, :no_of_rows); select s.count_by_move_time(:Not_Holdable, :no_of_rows); select s.count_by_move_time(:Not_Holdable, :no_of_rows); rollback; call s.close_cursor(:Is_Holdable); call s.declare_cursor_time(:Is_Holdable, true, null::text); select s.move_back_and_fore_time(:Is_Holdable, true); select s.move_back_and_fore_time(:Is_Holdable); select s.move_back_and_fore_time(:Is_Holdable); select s.move_back_and_fore_time(:Is_Holdable); start transaction; call s.declare_cursor_time(:Not_Holdable, false, null::text); select s.move_back_and_fore_time(:Not_Holdable, true); select s.move_back_and_fore_time(:Not_Holdable); select s.move_back_and_fore_time(:Not_Holdable); select s.move_back_and_fore_time(:Not_Holdable); rollback; \o \t off --Apple-Mail=_C1AF5A3F-F17F-4BDC-B1EE-06E3A050F6C6 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Thanks for the link to your SQL = file at the line where you get the row count in the way that you = describe... I noted that neither of these functions has a refcursor = formal argument and that, rather, you open (i.e. create) each of the = three cursors that you use within the two functions that uses = them.

CREATE FUNCTION = materialize_foreign_table(...)

CREATE = FUNCTION db_migrate_refresh(...) 

What = is not formal about the above?

I used the term of art "formal = argument" ordinarily to denote what's listed in parentheses at the start = of a subprogram definition. The term stands in contrast to "actual = argument"=E2=80=94meaning the expression that's used in a subprogram = invocation to provide a value for the corresponding formal argument. Go = to this page:

dotnettutorials.net/lesson/types-of-function-arguments-in-pytho= n/

and = search in it for "Example: formal and actual function arguments in = python". The blurb that comes just before the example says what I just = said. It seems to be more common to talk about formal and actual = "parameters". But the PG doc prefers "argument".

I didn't at all mean that "formal" is = good and that Laurenz's code is not formal and therefore is bad! So = sorry if you took it to mean this. 

Though it does not = matter as, back to the docs again:

www.postgresql.org/docs/current/plpgsql-cursors.html

"All access to cursors in PL/pgSQL goes = through cursor variables, which are always of the special data type = refcursor. One way to create a cursor variable is just to declare = it as a variable of type refcursor..." Though maybe you are trying = to differentiate between bound and unbound = refcursor's...

Ah... you missed my point entirely. Looks like my prose was = opaque. I was referring to this paradigm as described at the start of = the page that you referenced:

=C2=AB
A more = interesting usage is to return a reference to a cursor that a function = has created, allowing the caller to read the rows. This provides = an efficient way to return large row sets from functions.
=C2=BB

<aside> This seems to me to imply that the producer = function with a "refcursor" return is "security definer" and that the = consumer subprogram cannot access the tables of interest with explicit = "select=E2=80=9D. I thinks that's nice. </aside>

I needed to know that = the definition and use of each "refcursor" variable, in Laurentz's code, = are in the same subprogram because it's only when this is the case that = using an ordinary "select count(*)" and an ordinary "for" loop, where = the "select" is written in place within the "for" clause, are viable = alternatives.

Also order by is not relevant for getting a count.

Yes, of course. I know that. (But it does matter in many = cases of results presentation.) I was simply noting that Laurenz=E2=80=99s= use-case didn=E2=80=99t need ordering and that the "select" statements = will be faster than if ordering were needed. Sorry if I implied = something that I didn=E2=80=99t mean too. (The example in Laurenz's = post, www.cybertec-postgresql.com/en/pagination-problem-total-result-= count/, does use "order by".)

I did a little test an saw that this:

move last in cur;
get diagnostics n =3D row_count;

reported just 1=E2=80=94in contrast to what you used:

move forward all in = cur;

Because they are different actions. The first directly = moves to the last row in the cursor and counts just that row. The = second moves through all the rows left in the cursor and counts all the = rows it traversed. It is spelled out here:

https://www.postgresql.org/docs/current/sql-move.html

"The count is the number of rows that a FETCH = command with the same parameters would have returned (possibly = zero)."

Yes, I'm afraid that I expressed myself poorly again. I meant = to say that while I know nothing about the internals, I can't see how = "move last in cur" can know where the last row is unless it traverses = the entire result set from its current position until it finds no more = rows. And while its doing this, it may just as well count the rows it = traverses. It would seem, therefore, that a single operation that moves = to where you asked for and that tells you how many rows it traversed = would be enough=E2=80=94rather than two that both achieve the same = effect in the same time where one gives you the count of rows traversed = and the other doesn't. But never mind. The simple way to see it is that = the operations simply do what they do=E2=80=94and I won't worry about = how they might have been conceived differently.

I did some timing tests. I=E2=80=99ve = copied the results and the code at the end=E2=80=94just for = completeness. Here=E2=80=99s what I concluded.

(1) (and this is unremarkable), if you = can find a way to meet your requirement with just a single SQL = statement, then this is bound to be best. I chose a simple requirement: = read from a table and insert half the total number of rows into one = table and the remainder into another. This single SQL statement meets = that requirement:

with
  chunk(n) = as (select count(*)/2 from s.t0),
  i1 as (insert = into s.t1(k, v) select k, v from s.t0 where k < (select n from chunk) = returning k)
insert into s.t2(k, v) select k, v from s.t0 = where k >=3D (select n from chunk);

Internet search shows that this CTE = approach seems to be the popular PG pattern to achieve what Oracle=E2=80=99= s dedicated multi-table "insert" achieves. On the assumption that the = source table suffers constant concurrent changes from other sessions, = the PL/pgSQL loop that meets this requirement _must_ use the = cursor-based approach for getting the count that we=E2=80=99ve been = discussing. I=E2=80=99m glad to have learned all this because some = requirements (e.g. when the source table traversal has to do DDLs) = can=E2=80=99t be met with a single SQL statement, and so a PL/pgSQL loop = must be used.

(2) Even though the declaration (in a txn that you commit) = materializes the entire result set, this manages to be a lot faster than = doing this yourself with a temp table like this:

with
  c(n) as (
    select = count(*) from s.t),
  i1(n) as (
    insert into pg_temp.t_count select n from = c returning n)
insert into pg_temp.t(k, v, r) select k, v, = row_number() over(order by k) from s.t;
create unique = index temp_t_k_unq on pg_temp.t(k asc);

The index is meant to help "move". = Maybe some use cases wouldn=E2=80=99t need it. Same goes for recording = the count in its own one-row, one-column table.

(3) The times to do this:

move absolute 0 in cur;
move forward all in = cur;
get diagnostics n =3D row_count;
move = absolute 0 in cur;

and this:

move absolute 0 in cur;
move last in cur;
move absolute 0 in cur;

are the = same (within the limits of my measurements). But, of course, there=E2=80=99= s no need to use the second alternative.

(4) The time to get the count using = "move forward all in cur" is about the same as doing an ordinary = "count(*)"=E2=80=94but "move forward" is a little slower. I'm therefore = puzzled by Laurentz's "the result set count comes for free [with the = cursor approach]" in his blog post (x-ref above). Its appeal seems = to me to be limited to its rock-solid semantics.

(5) It's somewhat quicker to move = around in a holdable cursor than in a non-holdable one. But, of course, = you pay a price when you create the former.

Here are my timings=E2=80=94all in = seconds and all with three digits of precision. I used PG 15.2 in a = Ubuntu VM on my Mac Book. I run psql in the bare macOS and connect into = the VM. On some runs, some times are higher than usual. I expect that = the way the VM handles files has something to do with this. I chose a = run where the timings were pretty stable to copy here.

Insert 10,000,000 = rows time:             13.7  

Populate temp tables time:     =           10.6  

Native count(*) time:           =           0.633
    =                     =                   0.637
                =                     =       0.677
        =                     =               0.781

Declare "Is-Holdable" time:         =       1.91 

Is-Holdable = move fwd count time:          0.703
                =                     =       0.700
        =                     =               0.700
                =                     =       0.700

Declare = "Not-Holdable" time:             =  0.000

Not-Holdable move fwd count = time:         0.877
    =                     =                   0.866
                =                     =       0.864
        =                     =               0.872

Declare "Is-Holdable" time:         =       1.78 

Is-Holdable = move back & fore time:        0.691
                =                     =       0.695
        =                     =               0.709
                =                     =       0.708

Declare = "Not-Holdable" time:             =  0.000

Not-Holdable move back & = fore time:       0.872
      =                     =                 0.871
                =                     =       0.874
        =                     =               0.888


Here's the code. = If you want to run it, just copy it into a single file and start it at = the psql prompt. You need just to connect as a regular user that has = "create" on the database that you use.
---------------------------------------------------------------= -----------------

-- Create the objects.
\c :db :u
set search_path =3D pg_catalog, = pg_temp;
drop schema if exists s cascade;
create schema s;
create table s.t(k int primary = key, v int not null);

/*
  Format the elapsed time since t0 in seconds
  with three digits of precision. 
*/;
create function s.elapsed_time(t0 in double = precision)
  returns text
  set search_path =3D pg_catalog, pg_temp
  language plpgsql
as $body$
declare
  e = constant  double
        =         precision not null :=3D extract(epoch = from clock_timestamp()) - t0;

  t = constant  text        not null :=3D
    case
    =   when e >=3D 100000.0 then '>=3D 100K'    =                     =           ||'    '
      when e >=3D  10000.0 = then to_char(round(e/100.0)*100.0, '99,999'    )||'  =   '
      when e = >=3D   1000.0 then to_char(round(e/10.0)* = 10.0,   '9,999'    )||'  =   '
      when e >=3D =   100.0 then to_char(e,         =               '999'  =   )||'    '
    =   when e >=3D     10.0 then = to_char(e,                  =       '99.9'  )||'  '
      when e >=3D    =   1.0 then to_char(e,           =               '9.99' )||' '
      else       =             to_char(e,   =                     =   '0.999')
    end;
begin
  return lpad(t, 12);
end;
$body$;

create = procedure s.insert_table_time(no_of_rows int, t inout text)
  set search_path =3D pg_catalog, pg_temp
  language plpgsql
as $body$
declare
  n_text  constant  text  =       not null :=3D ltrim(to_char(no_of_rows, = '999,999,999,999'));
  caption = constant  text        not null :=3D = rpad('Insert '||n_text||' rows time:', 35);

  t0            =     double
        =               precision not null = :=3D 0.0;
begin
  truncate table = s.t;

  t0 :=3D extract(epoch from = clock_timestamp());
  with g(v) as (select = generate_series(1, no_of_rows))
  insert into = s.t(k, v) select g.v, (g.v)*2 from g;
  t :=3D = caption||s.elapsed_time(t0);
end;
$body$;

create procedure s.populate_temp_tables_time(t = inout text)
  set search_path =3D pg_catalog, = pg_temp
  language plpgsql
as = $body$
declare
  caption = constant  text        not null :=3D = rpad('Populate temp tables time:', 35);
  t0 =               double
                =       precision not null :=3D 0.0;
begin
  create table = pg_temp.t_count(n int);
  create table = pg_temp.t(k int, v int, r int);

  t0 :=3D extract(epoch from clock_timestamp());
  with
    c(n) as = (
      select count(*) from s.t),
    i1(n) as (
    =   insert into pg_temp.t_count select n from c returning n)
  insert into pg_temp.t(k, v, r) select k, v, = row_number() over(order by k) from s.t;
  create = unique index temp_t_k_unq on pg_temp.t(k asc);
  t= :=3D caption||s.elapsed_time(t0);
end;
$body$;

create procedure = s.close_cursor(cur in refcursor)
  set = search_path =3D pg_catalog, pg_temp
  language = plpgsql
as $body$
declare
begin
  close cur;
end;
$body$;

create = procedure s.declare_cursor_time(cur_name in text, holdable in boolean, t = inout text)
  -- set search_path =3D pg_catalog, = pg_temp :: Incomparible with "commit".
  language = plpgsql
as $body$
declare
  hld   =   constant  text not null :=3D case holdable
                =                     =    when true then 'with hold'
    =                     =                else   =         'without hold'
  =                     =                end;
  slct    constant  text = not null :=3D 'select k, v from s.t order by k';
  decl    constant  text = not null :=3D format('declare %I scroll cursor %s for '||slct, cur_name, = hld);
  caption constant  text  =       not null :=3D rpad('Declare "'||cur_name||'" = time:', 35);

  t0    =   constant  double
    =                 =   precision not null :=3D extract(epoch from = clock_timestamp());
begin
  execute = decl;
  if holdable then
  =   commit;
  end if;
  t :=3D caption||s.elapsed_time(t0);
end;
$body$;

create = function s.native_count_time(expected_n in int, show_caption in boolean = =3D false)
  returns text
  set search_path =3D pg_catalog, pg_temp
  language plpgsql
as $body$
declare
  n       =           int       =   not null :=3D 0;
  caption = constant  text        not null :=3D = case show_caption
          =                     =                 when true = then rpad('Native count(*) time:', 35)
    =                     =                     =   else           rpad('', = 35)
              =                     =           end;
  t0    =   constant  double
    =                 =   precision not null :=3D extract(epoch from = clock_timestamp());
begin
  n :=3D = (select count(*) from s.t);
  assert n =3D = expected_n;
  return = caption||s.elapsed_time(t0);
end;
$body$;

drop function if exists = s.count_by_move_time(refcursor, int, boolean) cascade;
create function s.count_by_move_time(cur in refcursor, = expected_n in int, show_caption in boolean =3D false)
  returns text
  set = search_path =3D pg_catalog, pg_temp
  language = plpgsql
as $body$
declare
  n             =     int         not null :=3D= 0;
  caption constant  text  =       not null :=3D case show_caption
                =                     =           when true then rpad(cur::text||' = move fwd count time:', 35)
        =                     =                 =   else           rpad('', = 35)
              =                     =           end;
  t0    =   constant  double
    =                 =   precision not null :=3D extract(epoch from = clock_timestamp());
begin
  move = absolute 0 in cur;
  move forward all in cur;
  get diagnostics n =3D row_count;
  move absolute 0 in cur;
  assert n =3D expected_n;
  return caption||s.elapsed_time(t0);
end;
$body$;

create = function s.move_back_and_fore_time(cur in refcursor, show_caption in = boolean =3D false)
  returns text
  set search_path =3D pg_catalog, pg_temp
  language plpgsql
as $body$
declare
  caption = constant  text        not null :=3D = case show_caption
          =                     =                 when true = then rpad(cur::text||' move back & fore time:', 35)
                =                     =           else       =     rpad('', 35)
      =                     =                 =   end;
  t0    =   constant  double
    =                 =   precision not null :=3D extract(epoch from = clock_timestamp());
begin
  move = absolute 0 in cur;
  move last in cur;
  move absolute 0 in cur;
  return caption||s.elapsed_time(t0);
end;
$body$;
---------------------------------------------------------------= -----------------

-- Do the timimg.
-- Ten thousand rows.
\set no_of_rows = 10000000
\set Is_Holdable ''''Is-Holdable''''
\set Not_Holdable ''''Not-Holdable''''
\t on
\o results.txt

call = s.insert_table_time(:no_of_rows, null::text);
call = s.populate_temp_tables_time(null::text);

select s.native_count_time(:no_of_rows, true);
select s.native_count_time(:no_of_rows);
select = s.native_count_time(:no_of_rows);
select = s.native_count_time(:no_of_rows);

call = s.declare_cursor_time(:Is_Holdable, true, null::text);

select s.count_by_move_time(:Is_Holdable, :no_of_rows, = true);
select s.count_by_move_time(:Is_Holdable, = :no_of_rows);
select s.count_by_move_time(:Is_Holdable, = :no_of_rows);
select s.count_by_move_time(:Is_Holdable, = :no_of_rows);

start transaction;
  call s.declare_cursor_time(:Not_Holdable, false, = null::text);

  select = s.count_by_move_time(:Not_Holdable, :no_of_rows, true);
  select s.count_by_move_time(:Not_Holdable, = :no_of_rows);
  select = s.count_by_move_time(:Not_Holdable, :no_of_rows);
  select s.count_by_move_time(:Not_Holdable, = :no_of_rows);
rollback;

call = s.close_cursor(:Is_Holdable);

call = s.declare_cursor_time(:Is_Holdable, true, null::text);

select s.move_back_and_fore_time(:Is_Holdable, true);
select s.move_back_and_fore_time(:Is_Holdable);
select s.move_back_and_fore_time(:Is_Holdable);
select s.move_back_and_fore_time(:Is_Holdable);

start transaction;
  call s.declare_cursor_time(:Not_Holdable, false, = null::text);

  select = s.move_back_and_fore_time(:Not_Holdable, true);
  select = s.move_back_and_fore_time(:Not_Holdable);
  select= s.move_back_and_fore_time(:Not_Holdable);
  select = s.move_back_and_fore_time(:Not_Holdable);
rollback;

\o
\t off

= --Apple-Mail=_C1AF5A3F-F17F-4BDC-B1EE-06E3A050F6C6--