[Django] #36502: [DJANGO] bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL

18 views
Skip to first unread message

Django

unread,
Jul 9, 2025, 12:01:25 PM7/9/25
to django-...@googlegroups.com
#36502: [DJANGO] bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: canardoFR | Type:
| Uncategorized
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Dear Django team,

I'm very fresh & green to Django, but we had to insert millions of lines
into a PostgreSQLtable.

We noticed that the ORM was setting up a "INSERT with UNNEST" for our
bulk_create as long as there was no ForeignKey.

Is related to code here
https://github.com/django/django/blob/main/django/db/backends/postgresql/compiler.py,
line 42 to 45

I made a quick a dirty test by monkey patching the assemble_as_sql
function to created a new list of tested fields ignoring ForeignKey type.
I would have done better by testing the ForeignKey type, but as said
above, to yound and green on django (some days).

I do not know if code is intentional or if this is a bug, thus the ticket
submission.

the patch is attached as a file...

Thanks for reading and cheers,
Alain R.
--
Ticket URL: <https://code.djangoproject.com/ticket/36502>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 9, 2025, 12:01:39 PM7/9/25
to django-...@googlegroups.com
#36502: [DJANGO] bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: canardoFR | Owner: (none)
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by canardoFR):

* Attachment "monkey_patch.py" added.

Django

unread,
Jul 9, 2025, 12:05:12 PM7/9/25
to django-...@googlegroups.com
#36502: [DJANGO] bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: canardoFR | Owner: (none)
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by canardoFR):

* has_patch: 0 => 1
* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36502#comment:1>
Reply all
Reply to author
Forward
0 new messages