Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17266 -- Skipped the "SET TIME ZONE" query for PostgreSQL when…

… it isn't necessary.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17194 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 219b41cc155a216fc912cd37f215fb44acee317b 1 parent d317f2a
Aymeric Augustin authored December 11, 2011
17  django/db/backends/postgresql_psycopg2/base.py
@@ -174,12 +174,21 @@ def _cursor(self):
174 174
                 conn_params['port'] = settings_dict['PORT']
175 175
             self.connection = Database.connect(**conn_params)
176 176
             self.connection.set_client_encoding('UTF8')
177  
-            # Set the time zone in autocommit mode (see #17062)
178 177
             tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')
179 178
             if tz:
180  
-                self.connection.set_isolation_level(
181  
-                        psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
182  
-                self.connection.cursor().execute("SET TIME ZONE %s", [tz])
  179
+                try:
  180
+                    get_parameter_status = self.connection.get_parameter_status
  181
+                except AttributeError:
  182
+                    # psycopg2 < 2.0.12 doesn't have get_parameter_status
  183
+                    conn_tz = None
  184
+                else:
  185
+                    conn_tz = get_parameter_status('TimeZone')
  186
+
  187
+                if conn_tz != tz:
  188
+                    # Set the time zone in autocommit mode (see #17062)
  189
+                    self.connection.set_isolation_level(
  190
+                            psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
  191
+                    self.connection.cursor().execute("SET TIME ZONE %s", [tz])
183 192
             self.connection.set_isolation_level(self.isolation_level)
184 193
             self._get_pg_version()
185 194
             connection_created.send(sender=self.__class__, connection=self)
25  docs/ref/databases.txt
@@ -33,6 +33,26 @@ aggregate with a database backend that falls within the affected release range.
33 33
 .. _known to be faulty: http://archives.postgresql.org/pgsql-bugs/2007-07/msg00046.php
34 34
 .. _Release 8.2.5: http://developer.postgresql.org/pgdocs/postgres/release-8-2-5.html
35 35
 
  36
+Optimizing PostgreSQL's configuration
  37
+-------------------------------------
  38
+
  39
+Django needs the following parameters for its database connections:
  40
+
  41
+- ``client_encoding``: ``'UTF8'``,
  42
+- ``default_transaction_isolation``: ``'read committed'``,
  43
+- ``timezone``: ``'UTC'`` when :setting:`USE_TZ` is ``True``, value of
  44
+  :setting:`TIME_ZONE` otherwise.
  45
+
  46
+If these parameters already have the correct values, Django won't set them for
  47
+every new connection, which improves performance slightly. You can configure
  48
+them directly in :file:`postgresql.conf` or more conveniently per database
  49
+user with `ALTER ROLE`_.
  50
+
  51
+Django will work just fine without this optimization, but each new connection
  52
+will do some additional queries to set these parameters.
  53
+
  54
+.. _ALTER ROLE: http://www.postgresql.org/docs/current/interactive/sql-alterrole.html
  55
+
36 56
 Transaction handling
37 57
 ---------------------
38 58
 
@@ -48,9 +68,8 @@ Autocommit mode
48 68
 
49 69
 If your application is particularly read-heavy and doesn't make many
50 70
 database writes, the overhead of a constantly open transaction can
51  
-sometimes be noticeable. For those situations, if you're using the
52  
-``postgresql_psycopg2`` backend, you can configure Django to use
53  
-*"autocommit"* behavior for the connection, meaning that each database
  71
+sometimes be noticeable. For those situations, you can configure Django
  72
+to use *"autocommit"* behavior for the connection, meaning that each database
54 73
 operation will normally be in its own transaction, rather than having
55 74
 the transaction extend over multiple operations. In this case, you can
56 75
 still manually start a transaction if you're doing something that

0 notes on commit 219b41c

Please sign in to comment.
Something went wrong with that request. Please try again.