Now GET DIAGNOSTIC will return the number of rows processed by COPY statement in a Pl/Pgsql function.
COPY statement in Pl/Pgsql Function:
--Raghav
COPY statement in Pl/Pgsql Function:
CREATE OR REPLACE FUNCTION public.copy_data(fname text) RETURNS integerPrevious release:
AS
$$
declare
copy_qry text;
cnt integer;
Begin
copy_qry := 'copy t from'||quote_literal(fname)||' with CSV HEADER;';
Execute copy_qry;
GET DIAGNOSTICS cnt = ROW_COUNT;
return cnt;
end;
$$ Language plpgsql;
-bash-4.1$ psqlIn PostgreSQL 9.3
psql.bin (9.2.3)
Type "help" for help.
postgres=# select copy_data('/usr/local/pg93beta/t_load.csv');
copy_data
-----------
0
(1 row)
-bash-4.1$ ./psql -p 5555Thanks to author, it looks simple but very effective when working with loading the data using scripts and want to know the number of rows processed by the COPY statement.
psql (9.3beta1)
Type "help" for help.
postgres=# select copy_data('/usr/local/pg93beta/t_load.csv');
copy_data
-----------
3
(1 row)
or
-bash-4.1$ ./psql -p 5555 -c "select copy_data('/usr/local/pg93beta/t_load.csv');"
copy_data
-----------
3
(1 row)
--Raghav