summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/backends/postgresql')
-rw-r--r--django/db/backends/postgresql/base.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
index b4008532ee..91dee6a722 100644
--- a/django/db/backends/postgresql/base.py
+++ b/django/db/backends/postgresql/base.py
@@ -4,6 +4,7 @@ PostgreSQL database backend for Django.
Requires psycopg 2: http://initd.org/projects/psycopg2
"""
+import threading
import warnings
from django.conf import settings
@@ -145,6 +146,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
introspection_class = DatabaseIntrospection
ops_class = DatabaseOperations
+ def __init__(self, *args, **kwargs):
+ super(DatabaseWrapper, self).__init__(*args, **kwargs)
+ self._named_cursor_idx = 0
+
def get_connection_params(self):
settings_dict = self.settings_dict
# None may be used to connect to the default 'postgres' db
@@ -206,11 +211,27 @@ class DatabaseWrapper(BaseDatabaseWrapper):
if not self.get_autocommit():
self.connection.commit()
- def create_cursor(self):
- cursor = self.connection.cursor()
+ def create_cursor(self, name=None):
+ if name:
+ # In autocommit mode, the cursor will be used outside of a
+ # transaction, hence use a holdable cursor.
+ cursor = self.connection.cursor(name, scrollable=False, withhold=self.connection.autocommit)
+ else:
+ cursor = self.connection.cursor()
cursor.tzinfo_factory = utc_tzinfo_factory if settings.USE_TZ else None
return cursor
+ def chunked_cursor(self):
+ self._named_cursor_idx += 1
+ db_cursor = self._cursor(
+ name='_django_curs_%d_%d' % (
+ # Avoid reusing name in other threads
+ threading.current_thread().ident,
+ self._named_cursor_idx,
+ )
+ )
+ return self._prepare_cursor(db_cursor)
+
def _set_autocommit(self, autocommit):
with self.wrap_database_errors:
self.connection.autocommit = autocommit