What is the difference between using a single cursor in psycopg2 to perform all your queries against using multiple cursors.
I.e, say I do this:
import psycopg2 as pg2
con = psycopg2.connect(...)
cur = con.cursor()
cur.execute(...)
....
....
cur.execute(...)
...
and every time I wish to execute a query thereafter, I use the same cursor cur
.
Alternatively I could do this every time I want to query my database:
with cur as con.cursor():
cur.execute(...)
In which case, my cursor cur
would be deleted after every use.
Which method is better? Does one have an advantage over another? Is one faster than the other? More generally, why are multiple cursors for one connection even needed?
The Cursor class of the psycopg library provide methods to execute the PostgreSQL commands in the database using python code. Using the methods of it you can execute SQL statements, fetch data from the result sets, call procedures. You can create Cursor object using the cursor() method of the Connection object/class.
Asynchronous notificationsPsycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY .
Also, We are using Psycopg2 to work with PostgreSQL because of the following reasons. It is used in most of the Python and Postgres frameworks. It is also actively maintained and supports Python's primary version, i.e., Python 3 and Python 2. It is thread-safe and designed for heavily multi-threaded applications.
Thread and process safetyThe Psycopg module and the connection objects are thread-safe: many threads can access the same database either using separate sessions and creating a connection per thread or using the same connection and creating separate cursors.
The two options are comparable; you can always benchmark both to see if there's a meaningful difference, but psycopg2 cursors are pretty lightweight (they don't represent an actual server-side, DECLARE
d cursor, unless you pass a name
argument) and I wouldn't expect any substantial slowdown from either route.
The reason psycopg2 has cursors at all is twofold. The first is to be able to represent server-side cursors for situations where the result set is larger than memory, and can't be retrieved from the DB all at once; in this case the cursor serves as the client-side interface for interacting with the server-side cursor.
The second is that psycopg2 cursors are not thread-safe; a connection object can be freely used by any thread, but each cursor should be used by at most one thread. Having a cursor-per-thread allows for multithreaded applications to access the DB from any thread, while sharing the same connection.
See the psycopg2 usage docs on server side cursors and thread and process safety for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With