# Django 4.2 vs 3.2 psycopg2 poor cursor performance

**URL:** <https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104>\
**Category:** ORM\
**Created:** [April 12, 2024, 3:02pm UTC](https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104 "2024-04-12T15:02:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtruty](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jtruty/32/20572_2.png) [@jtruty](https://forum.djangoproject.com/u/jtruty)\
**Post date:** [April 12, 2024, 3:02pm UTC](https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104/1 "2024-04-12T15:02:42Z")

</div>

I’m updating an app that is currently on Django 3.2.x to 4.2.x and have run into performance issues when iterating over the same queryset with the same data between versions (using psycopg2 with postgres). I’ve narrowed down the issue via profiling, here are the timings below (I see this consistently):

 ![Screenshot 2024-04-12 at 10.02.10](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/a/d/ad4ff4a84922df900d54d8431959ce95e54813b4.png)

This is on psycopg2 2.9.7. Has anyone seen anything similar or know of any settings that might have changed that might affect this? I’ve tried psycopg3 and see similarly poor performance as well, also have tried disabling server-side cursors, no luck yet.

---

<div class="post-metadata">

**Author:** ![jtruty](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jtruty/32/20572_2.png) [@jtruty](https://forum.djangoproject.com/u/jtruty)\
**Post date:** [April 12, 2024, 3:04pm UTC](https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104/2 "2024-04-12T15:04:47Z")

</div>

With psycopg3[c] a similar delay is seen in the connection wait method:

 ![Screenshot 2024-04-12 at 10.04.37](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/2/c/2c13a3d0abd10d8846f0734ddf6c01e16a5d1e46.png)

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [April 12, 2024, 3:08pm UTC](https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104/3 "2024-04-12T15:08:02Z")

</div>

Kind of hard to provide you any support here as the only thing the profiling data demonstrates is that for the same number of call to `psycopg2.extensions.cursor` it takes significantly more time to execute on 4.2.

I’d look into what particular `(sql, params)` are being fed into the cursor between the two Django versions to compare it and try to identify differences.

It might be that for the same ORM calls some subtly different SQL is generated on 3.2 and 4.2 which happens to be less efficient and make it look like `psycopg2` takes more time to run.

---

<div class="post-metadata">

**Author:** ![jtruty](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jtruty/32/20572_2.png) [@jtruty](https://forum.djangoproject.com/u/jtruty)\
**Post date:** [April 15, 2024, 7:33pm UTC](https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104/4 "2024-04-15T19:33:34Z")

</div>

@charettes you are correct - the data going into what generates the SQL (from a previous SQL query) is different. What was happening is that we generate a very large OR query with data from the results of a previous SQL query, and in that data there were many duplicates in Django 4 vs Django 3. This resulted in a query that returned the same results, but was ~5x as big. Eliminating the duplicates fixes the performance (and improves beyond django 3 as well).

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [April 15, 2024, 7:45pm UTC](https://forum.djangoproject.com/t/django-4-2-vs-3-2-psycopg2-poor-cursor-performance/30104/5 "2024-04-15T19:45:50Z")

</div>

Thanks for reporting on your findings @jtruty, glad you were able to resolve the issue you were facing.

Hopefully that’ll be useful to others who might run into something similar.
