从 COPY 命令获取行数 [英] Get the count of rows from a COPY command

查看:33
本文介绍了从 COPY 命令获取行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文件中复制数据时,您可以使用命令标签"获取 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.

有没有办法直接在 PL/pgSQL 函数中从 COPY 获取这个计数?
据我所知没有,但也许我遗漏了什么?

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 中没有,但 Pavel (E 1.3.1.7) 提供的 PG 9.3 中有:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆