Fixed #36490 -- Avoided unnecessary transaction in bulk_create. by charettes · Pull Request #19614 · django/django · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Fixed #36490 -- Avoided unnecessary transaction in bulk_create. #19614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

charettes
Copy link
Member

@charettes charettes commented Jul 2, 2025

Trac ticket number

ticket-36490

Branch description

When bulk_create deals with an heterogeneous set of object with regards to primary key assignment that fits in a single batch there's no need to wrap the resulting single INSERT statement in a transaction.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

Copy link
Contributor

@shangxiao shangxiao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I glossed over finer details but generally seems good 🏆

@charettes charettes marked this pull request as ready for review July 2, 2025 06:26
@hermann-wenninger

This comment was marked as spam.

charettes added 3 commits July 2, 2025 13:38
When dealing with an heterogeneous set of object with regards to primary key
assignment that fits in a single batch there's no need to wrap the single
INSERT statement in a transaction.
…andling.

Whether or not returning_fields should be specified to _insert is not a
function of each batches so the conditional can be moved outside of the loop.
Asserting an upper bound for the number of executed queries can be achieved by
using CaptureQueriesContext instead of enabling the whole DEBUG machinery.
@charettes charettes changed the title Fixed #36490 -- Avoided unnecessary transaction creation in bulk_create. Fixed #36490 -- Avoided unnecessary transaction in bulk_create. Jul 2, 2025
@charettes
Copy link
Member Author

FWIW recent improvements in SQLCompiler for default eliding that landed in 4608d34 allow for heterogeneous set of objects (with and without pk) to be efficiently combined in a single INSERT as demonstrated here

@@ -800,7 +801,11 @@ def bulk_create(
fields = [f for f in opts.concrete_fields if not f.generated]
objs = list(objs)
objs_with_pk, objs_without_pk = self._prepare_for_bulk_create(objs)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that this whole primary key assignment partition logic can also be elided if we'd like to go further

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.