Get the count of rows from a COPY command

偶尔善良 提交于 2019-12-19 03:39:31

问题


When copying data from a file, you get the count of rows in psql with the "command tag":

db=# COPY t FROM '/var/lib/postgres/test.sql';
COPY 10

I need the number of rows and would like to avoid a redundant count() on the table.

Is there a way to get this count from COPY directly in a PL/pgSQL function?
As far as I know there is none, but maybe I am missing something?

For PostgreSQL 9.2. But any option in any version would be of interest.


回答1:


Not in PG 9.2, but there is in PG 9.3 courtesy of Pavel (E 1.3.1.7):

Allow PL/pgSQL to access the number of rows processed by COPY (Pavel Stehule)

The command is GET DIAGNOSTICS x = ROW_COUNT.

http://www.postgresql.org/docs/devel/static/release-9-3.html



来源:https://stackoverflow.com/questions/16610449/get-the-count-of-rows-from-a-copy-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!