On Fri, Sep 20, 2013 at 10:04 AM, Federico Di Gregorio <fog@dndg.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."