Skip to content

Commit 7ae1815

Browse files
committed
Return the number of rows processed when COPY is executed through SPI.
You can now get the number of rows processed by a COPY statement in a PL/pgSQL function with "GET DIAGNOSTICS x = ROW_COUNT". Pavel Stehule, reviewed by Amit Kapila, with some editing by me.
1 parent bc1229c commit 7ae1815

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

doc/src/sgml/spi.sgml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,10 @@ SPI_execute("INSERT INTO foo SELECT * FROM bar", false, 5);
377377
global pointer <literal>SPITupleTable *SPI_tuptable</literal> to
378378
access the result rows. Some utility commands (such as
379379
<command>EXPLAIN</>) also return row sets, and <literal>SPI_tuptable</>
380-
will contain the result in these cases too.
380+
will contain the result in these cases too. Some utility commands
381+
(<command>COPY</>, <command>CREATE TABLE AS</>) don't return a row set, so
382+
<literal>SPI_tuptable</> is NULL, but they still return the number of
383+
rows processed in <varname>SPI_processed</>.
381384
</para>
382385

383386
<para>

src/backend/executor/spi.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,25 +1922,31 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
19221922
_SPI_current->processed = _SPI_current->tuptable->alloced -
19231923
_SPI_current->tuptable->free;
19241924

1925+
res = SPI_OK_UTILITY;
1926+
19251927
/*
1926-
* CREATE TABLE AS is a messy special case for historical
1927-
* reasons. We must set _SPI_current->processed even though
1928-
* the tuples weren't returned to the caller, and we must
1929-
* return a special result code if the statement was spelled
1930-
* SELECT INTO.
1928+
* Some utility statements return a row count, even though the
1929+
* tuples are not returned to the caller.
19311930
*/
19321931
if (IsA(stmt, CreateTableAsStmt))
19331932
{
19341933
Assert(strncmp(completionTag, "SELECT ", 7) == 0);
19351934
_SPI_current->processed = strtoul(completionTag + 7,
19361935
NULL, 10);
1936+
1937+
/*
1938+
* For historical reasons, if CREATE TABLE AS was spelled
1939+
* as SELECT INTO, return a special return code.
1940+
*/
19371941
if (((CreateTableAsStmt *) stmt)->is_select_into)
19381942
res = SPI_OK_SELINTO;
1939-
else
1940-
res = SPI_OK_UTILITY;
19411943
}
1942-
else
1943-
res = SPI_OK_UTILITY;
1944+
else if (IsA(stmt, CopyStmt))
1945+
{
1946+
Assert(strncmp(completionTag, "COPY ", 5) == 0);
1947+
_SPI_current->processed = strtoul(completionTag + 5,
1948+
NULL, 10);
1949+
}
19441950
}
19451951

19461952
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy