#36502 closed Bug (fixed)
bulk_create on Postgres doesn't use the unnest strategy when foreign keys are involved since 5.2.1
| Reported by: | canardoFR | Owned by: | Simon Charette |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 5.2 |
| Severity: | Release blocker | Keywords: | postgres bulk_create unnest foreignkey |
| Cc: | canardoFR, Simon Charette | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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.
Attachments (1)
Change History (8)
by , 5 months ago
| Attachment: | monkey_patch.py added |
|---|
comment:1 by , 5 months ago
| Has patch: | set |
|---|---|
| Patch needs improvement: | set |
comment:2 by , 5 months ago
| Summary: | [DJANGO] bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL → bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL |
|---|
comment:3 by , 5 months ago
| Cc: | added |
|---|---|
| Has patch: | unset |
| Keywords: | postgres bulk_create unnest foreignkey added |
| Owner: | set to |
| Patch needs improvement: | unset |
| Severity: | Normal → Release blocker |
| Status: | new → assigned |
| Summary: | bulk INSERT is not using UNNEST when a field is a FK on PostgreSQL → bulk_create on Postgres doesn't use the unnest strategy when foreign keys are involved since 5.2.1 |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:4 by , 5 months ago
| Has patch: | set |
|---|
comment:5 by , 5 months ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
From looking at your attached monkey patching solution you seem to want to make sure the optimization introduced in Django 5.2 on Postgres to make use of
UNNEST(see #35936) is enabled when foreign keys are involved which appears to have been inadvertently disabled by 764af7a3d6c0b543dcf659a2c327f214da768fe4 (see #36289).Going to move a release blocker since this a regression in 5.2.1.