Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Return number of rows for copy operations

#180

As asked by Ryan Kelly on the ML:

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.

Reported by Federico Di Gregorio · September 20th, 2013 @ 02:05 PM

State: resolved
Milestone: psycopg2 2.5.3
Assigned to: Federico Di Gregorio Federico Di Gregorio

Activity

  1. Federico Di Gregorio
    Federico Di Gregorio

    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.

    September 20th, 2013 @ 02:05 PM

  2. Rui Da Costa
    Rui Da Costa

    I'm no expert, but isn't it described here?:
    www.postgresql.org/docs/9.3/static/libpq-exec.html#LIBPQ-PQCMDTUPLES

    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, ...

    December 9th, 2013 @ 01:49 PM

  3. Nicholas Chammas
    Nicholas Chammas

    On a related note, I have been using psycopg2 with Amazon Redshift and recently asked a similar question about getting the rowcount for COPY statements executed via cursor.execute. I've attached a PDF of the linked AWS forum page in case you can't access it.

    Amazon claims that Redshift correctly reports rowcount to the calling interface and that psycopg2 doesn't seem to pick it up.

    How can I validate Amazon's claim?

    May 5th, 2014 @ 09:59 PM

  4. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from open to resolved
    • Milestone set to psycopg2 2.5.3

    As Rui pointed out the number of rows is returned by PQcmdTuples. We weren't calling this function on copy results but only after normal opreations.

    Fixed in 2.5.3. Nicholas, try checking out https://github.com/psycopg/psycopg2/tree/maint_2_5 and see if it works as expected, thank you.

    May 5th, 2014 @ 11:00 PM

  5. Bilal Aslam
    Bilal Aslam

    Daniele, thanks for checking in the fix. I tried this out on May 27 using the branch you suggested, but I am still getting a rowcount of -1.

    In [36]: cursor.rowcount
    Out[36]: -1

    Nicholas, did you have any better luck than me?

    May 27th, 2014 @ 06:02 PM

  6. Daniele Varrazzo
    Daniele Varrazzo

    Hello Bilal,

    yes, it works for me: check the test suite. The changeset is released in psycopg 2.5.3

    Note that old postgres versions (before 8.2) don't return the rowcount. So it depends on a message the server must send. If the server doesn't send it we cannot help.

    May 27th, 2014 @ 07:32 PM

  7. Nicholas Chammas
    Nicholas Chammas

    Hey Daniele,

    It appears that we are still seeing the -1 rowcount on COPYs in psycopg 2.5.3 when testing against Redshift 1.0.789. This may well be an issue on Redshift's side, and I think it's beyond psycopg2's mandate to work cleanly with forks of PostgreSQL. (I think Bilal is also testing against Redshift.)

    FYI: Redshift is a distant fork of PostgreSQL 8 with a column-oriented storage engine, hosted as a service by Amazon. Its primary use case is for massive analytics workloads. Amazon recommends using PostgreSQL drivers to interact with the database.

    Anyway, I am following up with Amazon support here to get to the bottom of this. They claim it should work.

    May 27th, 2014 @ 10:09 PM