从COPY命令获取行数 [英] Get the count of rows from a COPY command
问题描述
从文件复制数据时,您可以通过命令标签获取psql中的行数:
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
我需要行数,在表上有多余的 count()
。
I need the number of rows and would like to avoid a redundant count()
on the table.
有办法从 COPY
直接在PL / pgSQL函数中?
据我所知,没有,但也许我错过了一些东西?
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?
对于PostgreSQL 9.2。
For PostgreSQL 9.2. But any option in any version would be of interest.
推荐答案
不在PG 9.2中,但在PG 9.3中有Pavel (E 1.3.1.7):
Not in PG 9.2, but there is in PG 9.3 courtesy of Pavel (E 1.3.1.7):
允许PL / pgSQL访问COPY处理的行数(Pavel Stehule)
Allow PL/pgSQL to access the number of rows processed by COPY (Pavel Stehule)
命令是GET DIAGNOSTICS x = ROW_COUNT。
The command is GET DIAGNOSTICS x = ROW_COUNT.
http://www.postgresql.org/docs/devel/static/release-9-3。 html
这篇关于从COPY命令获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!