diff options
| author | Simon Charette <charette.s@gmail.com> | 2024-11-17 00:30:00 -0500 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-12-11 13:56:18 +0100 |
| commit | a16eedcf9c69d8a11d94cac1811018c5b996d491 (patch) | |
| tree | ff30bd7f076156079e6636f93b8a5bc05e7be9f9 /tests/backends/postgresql/test_compilation.py | |
| parent | 2638b75554d2624dca3062a8da113a47f855f2a2 (diff) | |
Fixed #35936 -- Used unnest for bulk inserts on Postgres when possible.
This should make bulk_create significantly faster on Postgres when provided
only literal values.
Thanks James Sewell for writing about this technique, Tom Forbes for
validating the performance benefits, David Sanders and Mariusz Felisiak
for the review.
Diffstat (limited to 'tests/backends/postgresql/test_compilation.py')
| -rw-r--r-- | tests/backends/postgresql/test_compilation.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/backends/postgresql/test_compilation.py b/tests/backends/postgresql/test_compilation.py new file mode 100644 index 0000000000..67fe893e35 --- /dev/null +++ b/tests/backends/postgresql/test_compilation.py @@ -0,0 +1,29 @@ +import unittest + +from django.db import connection +from django.db.models.expressions import RawSQL +from django.test import TestCase + +from ..models import Square + + +@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL tests") +class BulkCreateUnnestTests(TestCase): + def test_single_object(self): + with self.assertNumQueries(1) as ctx: + Square.objects.bulk_create([Square(root=2, square=4)]) + self.assertNotIn("UNNEST", ctx[0]["sql"]) + + def test_non_literal(self): + with self.assertNumQueries(1) as ctx: + Square.objects.bulk_create( + [Square(root=2, square=RawSQL("%s", (4,))), Square(root=3, square=9)] + ) + self.assertNotIn("UNNEST", ctx[0]["sql"]) + + def test_unnest_eligible(self): + with self.assertNumQueries(1) as ctx: + Square.objects.bulk_create( + [Square(root=2, square=4), Square(root=3, square=9)] + ) + self.assertIn("UNNEST", ctx[0]["sql"]) |
