diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 792026530f..7bcda5b6f6 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -9,6 +9,7 @@ from django.db.backends.postgresql.client import DatabaseClient from django.db.backends.postgresql.creation import DatabaseCreation from django.db.backends.postgresql.introspection import DatabaseIntrospection from django.db.backends.postgresql.operations import DatabaseOperations +from django.db.backends.postgresql.version import get_version from django.utils.encoding import smart_str, smart_unicode try: @@ -115,6 +116,12 @@ class DatabaseWrapper(BaseDatabaseWrapper): cursor = self.connection.cursor() if set_tz: cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) + if not hasattr(self, '_version'): + version = get_version(cursor) + self.__class__._version = version + if version < (8, 0): + # No savepoint support for earlier version of PostgreSQL. + self.features.uses_savepoints = False cursor.execute("SET client_encoding to 'UNICODE'") cursor = UnicodeCursorWrapper(cursor, 'utf-8') return cursor diff --git a/django/db/backends/postgresql/version.py b/django/db/backends/postgresql/version.py new file mode 100644 index 0000000000..e14d791b07 --- /dev/null +++ b/django/db/backends/postgresql/version.py @@ -0,0 +1,18 @@ +""" +Extracts the version of the PostgreSQL server. +""" + +import re + +VERSION_RE = re.compile(r'PostgreSQL (\d+)\.(\d+)\.') + +def get_version(cursor): + """ + Returns a tuple representing the major and minor version number of the + server. For example, (7, 4) or (8, 3). + """ + cursor.execute("SELECT version()") + version = cursor.fetchone()[0] + major, minor = VERSION_RE.search(version).groups() + return int(major), int(minor) + diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 08014bd993..b50cb1d1c9 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -8,6 +8,7 @@ from django.db.backends import * from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations from django.db.backends.postgresql.client import DatabaseClient from django.db.backends.postgresql.creation import DatabaseCreation +from django.db.backends.postgresql.version import get_version from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection from django.utils.safestring import SafeUnicode @@ -86,4 +87,10 @@ class DatabaseWrapper(BaseDatabaseWrapper): cursor.tzinfo_factory = None if set_tz: cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) + if not hasattr(self, '_version'): + version = get_version(cursor) + self.__class__._version = version + if version < (8, 0): + # No savepoint support for earlier version of PostgreSQL. + self.features.uses_savepoints = False return cursor