Opened 11 days ago

Closed 10 days ago

Last modified 10 days ago

#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)

monkey_patch.py (1.6 KB ) - added by canardoFR 11 days ago.

Download all attachments as: .zip

Change History (8)

by canardoFR, 11 days ago

Attachment: monkey_patch.py added

comment:1 by canardoFR, 11 days ago

Has patch: set
Patch needs improvement: set

comment:2 by canardoFR, 11 days ago

Summary: [DJANGO] bulk INSERT is not using UNNEST when a field is a FK on PostgreSQLbulk INSERT is not using UNNEST when a field is a FK on PostgreSQL

comment:3 by Simon Charette, 11 days ago

Cc: Simon Charette added
Has patch: unset
Keywords: postgres bulk_create unnest foreignkey added
Owner: set to Simon Charette
Patch needs improvement: unset
Severity: NormalRelease blocker
Status: newassigned
Summary: bulk INSERT is not using UNNEST when a field is a FK on PostgreSQLbulk_create on Postgres doesn't use the unnest strategy when foreign keys are involved since 5.2.1
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

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.

comment:4 by Simon Charette, 11 days ago

Has patch: set

comment:5 by Mariusz Felisiak, 10 days ago

Triage Stage: AcceptedReady for checkin

comment:6 by GitHub <noreply@…>, 10 days ago

Resolution: fixed
Status: assignedclosed

In 0fe2188:

Fixed #36502 -- Restored UNNEST strategy for foreign key bulk inserts on PostgreSQL.

Regression in 764af7a3d6c0b543dcf659a2c327f214da768fe4.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 10 days ago

In 3df1ad57:

[5.2.x] Fixed #36502 -- Restored UNNEST strategy for foreign key bulk inserts on PostgreSQL.

Regression in 764af7a3d6c0b543dcf659a2c327f214da768fe4.

Backport of 0fe218842e0e396e3ab3982bd21227968a9e7fd8 from main.

Note: See TracTickets for help on using tickets.
Back to Top