Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of using multiple cursors in psycopg2 for PostgreSQL queries?

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?

like image 408
Adnan Siddiquei Avatar asked Jun 14 '20 11:06

Adnan Siddiquei


People also ask

What is cursor in Psycopg2?

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.

Is Psycopg2 asynchronous?

Asynchronous notificationsPsycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY .

Why do we use Psycopg2?

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.

Is Psycopg2 connection thread-safe?

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.


1 Answers

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

like image 62
AdamKG Avatar answered Oct 03 '22 02:10

AdamKG