mirror of
				https://github.com/django/django.git
				synced 2025-10-26 07:06:08 +00:00 
			
		
		
		
	[1.11.x] Fixed #28062 -- Added a setting to disable server-side cursors on PostgreSQL.
When a connection pooler is set up in transaction pooling mode, queries
relying on server-side cursors fail. The DISABLE_SERVER_SIDE_CURSORS
setting in DATABASES disables server-side cursors for this use case.
Backport of 88336fdbb5 from master
			
			
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							f9a4593376
						
					
				
				
					commit
					6a2624286b
				
			| @@ -1,5 +1,7 @@ | ||||
| import operator | ||||
| import unittest | ||||
| from collections import namedtuple | ||||
| from contextlib import contextmanager | ||||
|  | ||||
| from django.db import connection | ||||
| from django.test import TestCase | ||||
| @@ -23,6 +25,18 @@ class ServerSideCursorsPostgres(TestCase): | ||||
|             cursors = cursor.fetchall() | ||||
|         return [self.PostgresCursor._make(c) for c in cursors] | ||||
|  | ||||
|     @contextmanager | ||||
|     def override_db_setting(self, **kwargs): | ||||
|         for setting, value in kwargs.items(): | ||||
|             original_value = connection.settings_dict.get(setting) | ||||
|             if setting in connection.settings_dict: | ||||
|                 self.addCleanup(operator.setitem, connection.settings_dict, setting, original_value) | ||||
|             else: | ||||
|                 self.addCleanup(operator.delitem, connection.settings_dict, setting) | ||||
|  | ||||
|             connection.settings_dict[setting] = kwargs[setting] | ||||
|             yield | ||||
|  | ||||
|     def test_server_side_cursor(self): | ||||
|         persons = Person.objects.iterator() | ||||
|         next(persons)  # Open a server-side cursor | ||||
| @@ -52,3 +66,17 @@ class ServerSideCursorsPostgres(TestCase): | ||||
|         del persons | ||||
|         cursors = self.inspect_cursors() | ||||
|         self.assertEqual(len(cursors), 0) | ||||
|  | ||||
|     def test_server_side_cursors_setting(self): | ||||
|         with self.override_db_setting(DISABLE_SERVER_SIDE_CURSORS=False): | ||||
|             persons = Person.objects.iterator() | ||||
|             next(persons)  # Open a server-side cursor | ||||
|             cursors = self.inspect_cursors() | ||||
|             self.assertEqual(len(cursors), 1) | ||||
|             del persons  # Close server-side cursor | ||||
|  | ||||
|         with self.override_db_setting(DISABLE_SERVER_SIDE_CURSORS=True): | ||||
|             persons = Person.objects.iterator() | ||||
|             next(persons)  # Should not open a server-side cursor | ||||
|             cursors = self.inspect_cursors() | ||||
|             self.assertEqual(len(cursors), 0) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user