Replies: 1 comment 1 reply
-
Hello, Federico, after it is closed, the cursor is unusable. What you are seeing in psycopg2 is likely an implementation accident, with the value copied to the C structure and not reset on I don't think it is worth mentioning it in the porting guide because, neither in psycopg 2 nor in 3, you should be using the cursor after closing. I don't want to commit to an interface the the behaviour of a closed cursor.
In your use case, what stops you from reading |
Beta Was this translation helpful? Give feedback.
-
Hi,
I've noticed a difference in how the rowcount of a closed cursor works between psycopg2 and psycopg: in psycopg2 a closed cursor kept the last query rowcount, with psyocpg resets it to -1.
The following pattern is basically what sqlalchemy does when running inserts (it closes the cursor before returning the result if it does not returns rows)
The above code prints
Is this by design?
If it may be worth mentioning it in https://www.psycopg.org/psycopg3/docs/basic/from_pg2.html ?
If not is it reasonable to restore the psycopg2 behaviour?
I kinda think it's by design but I though it's worth asking.
I've noticed this while updated a work project from psycopg2 to psycopg3 in sqlalchemy. The issue there is the following: sqlalchemy/sqlalchemy#10974
My use case in the project is getting the number of rows inserted in an
insert ... select ...
, where otherwise there is no easy way of getting how many rows the query would match. My current workaround is to addreturning 1
and then count the number of rows that it returned, but that's less than idealBeta Was this translation helpful? Give feedback.
All reactions