Re: copy_from and rowcount

From: David Blewett <david(at)dawninglight(dot)net>
To: Federico Di Gregorio <fog(at)dndg(dot)it>
Cc: psycopg(at)postgresql(dot)org, rpkelly22(at)gmail(dot)com
Subject: Re: copy_from and rowcount
Date: 2013-09-20 19:08:49
Message-ID: CAFzAthYGDo3irMgifswuk_F5=4vvdiRNxOjGcg4iL7+2E4WNfA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

​On Fri, Sep 20, 2013 at 10:04 AM, Federico Di Gregorio <fog(at)dndg(dot)it> wrote:

> On 12/09/2013 18:06, Ryan Kelly wrote:
> > I'm wondering if it's possible to get the number of rows copied when
> > using copy_from? I would've thought the .rowcount attribute of the cursor
> > would have this value, but that doesn't seem to be the case.
>
> Sorry for the late asnwer.
>
> If I remember correctly the libpq docs don't say if it is possible to
> retrieve the number of inserted columns and we're not sending data
> line-by-line but as chunks so counting the lines isn't possible. Maybe
> (a big maybe) the final PGresult contains that but I can't find this
> documented anywhere. We'll need to instrument psycopg to print PGresult
> fields and do a bit of guesswork.
>

​You can use the PQcmdTuples​ [1] function on the PGresult returned by the
copy operation:

"This function returns a string containing the number of rows affected by
the SQL statement that generated the PGresult. This function can only be
used following the execution of a SELECT, CREATE TABLE AS, INSERT, UPDATE,
DELETE, MOVE, FETCH, or COPY statement, or an EXECUTE of a prepared query
that contains an INSERT, UPDATE, or DELETE statement. If the command that
generated the PGresult was anything else, PQcmdTuples returns an empty
string. The caller should not free the return value directly. It will be
freed when the associated PGresult handle is passed to PQclear."

​1. www.postgresql.org/docs/current/static/libpq-exec.html#LIBPQ-PQCMDTUPLES

--
Thanks,

David Blewett

In response to

Browse psycopg by date

  From Date Subject
Next Message Audrius Kažukauskas 2013-09-30 17:56:54 psycopg2 doesn't use LDFLAGS from pg_config
Previous Message Federico Di Gregorio 2013-09-20 14:04:19 Re: copy_from and rowcount