This project is archived and is in readonly mode.

#100 ✓resolved
Psycopg website

cursor rownumber fails

Reported by Psycopg website | February 22nd, 2012 @ 07:46 AM

Submitted by: Mariano

Hi
I'm a developer using psycopg since two years
I'm using gentoo and when I update to 2.4.4, rownumber atribute from cursor always give de rowcount value.
If I downgrade to 2.4.2 I got the problem solved.
I've tried in several machines and I get always the same error
Thank You

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo February 22nd, 2012 @ 01:52 PM

    Hi Mariano,

    Can't reproduce what you say: can you provide an example? I've tried with regular cursors as well with DictCursor and NamedTupleCursor:

    In [1]: import psycopg2
    
    In [2]: cnn = psycopg2.connect('')
    
    In [3]: cur = cnn.cursor()
    
    In [4]: cur.execute('select * from generate_series(1,10)')
    
    In [5]: cur.rownumber, cur.rowcount
    Out[5]: (0, 10)
    
    In [7]: cur.fetchone()
    Out[7]: (1,)
    
    In [8]: cur.rownumber, cur.rowcount
    Out[8]: (1, 10)
    
    In [9]: cur.fetchmany(3)
    Out[9]: [(2,), (3,), (4,)]
    
    In [10]: cur.rownumber, cur.rowcount
    Out[10]: (4, 10)
    
    In [11]: import psycopg2.extras
    
    In [12]: cur = cnn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
    
    In [13]: cur.rownumber, cur.rowcount
    Out[13]: (0, -1)
    
    In [14]: cur.execute('select * from generate_series(1,10)')
    
    In [15]: cur.rownumber, cur.rowcount
    Out[15]: (0, 10)
    
    In [16]: cur.fetchone()
    Out[16]: Record(generate_series=1)
    
    In [17]: cur.rownumber, cur.rowcount
    Out[17]: (1, 10)
    
    In [18]: cur.fetchmany(3)
    Out[18]: 
    [Record(generate_series=2),
     Record(generate_series=3),
     Record(generate_series=4)]
    
    In [19]: cur.rownumber, cur.rowcount
    Out[19]: (4, 10)
    
    In [20]: cur = cnn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    
    In [21]: cur.execute('select * from generate_series(1,10)')
    
    In [22]: cur.rownumber, cur.rowcount
    Out[22]: (0, 10)
    
    In [23]: cur.fetchone()
    Out[23]: [1]
    
    In [24]: cur.rownumber, cur.rowcount
    Out[24]: (1, 10)
    
    In [25]: cur.fetchmany(3)
    Out[25]: [[2], [3], [4]]
    
    In [26]: cur.rownumber, cur.rowcount
    Out[26]: (4, 10)
    
    In [27]: psycopg2.__version__
    Out[27]: '2.4.4 (dt dec pq3 ext)'
    

    Please provide an example of the problem, thanks.

  • Mariano

    Mariano February 23rd, 2012 @ 11:41 AM

    Hi again Daniele!

    This is the code I've tried.

    import psycopg2, psycopg2.extras
    cnn = psycopg2.extras.DictConnection("user='postgres'")
    cur = cnn.cursor()
    cur.execute('select * from generate_series(1,10)')
    for i in cur:

        print (cur.rownumber, cur.rowcount)
    

    If I execute it with 2.4.2, with python2 and python3, I get:
    (1, 10) (2, 10) (3, 10) (4, 10) (5, 10) (6, 10) (7, 10) (8, 10) (9, 10) (10, 10)

    If I execute it with 2.4.4, with python2 and python3, I get the error:
    (10, 10) (10, 10) (10, 10) (10, 10) (10, 10) (10, 10) (10, 10) (10, 10) (10, 10) (10, 10)

    Thank you

  • Daniele Varrazzo

    Daniele Varrazzo February 23rd, 2012 @ 12:01 PM

    • State changed from “new” to “open”

    I see: It's probably a regression introduced with this change, to make itersize work as expected.

    https://github.com/dvarrazzo/psycopg/commit/8473209d240bad964630b9b...

    Will see this evening how to fix it. The problem is shared by several cursor subclasses.

  • Daniele Varrazzo

    Daniele Varrazzo February 23rd, 2012 @ 11:18 PM

    • State changed from “open” to “resolved”

    Fixed in my dev branch.

    rownumber is still broken for named cursors though. This would be an entirely different correction, and they have been broken forever, so I'm not in hurry to fix them.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="https://github.com/psycopg/psycopg2/issues">the new tracker</a>.
<br/>
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins

Pages