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
This commit is contained in:
parent
d317f2ab98
commit
219b41cc15
|
@ -174,12 +174,21 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
conn_params['port'] = settings_dict['PORT']
|
conn_params['port'] = settings_dict['PORT']
|
||||||
self.connection = Database.connect(**conn_params)
|
self.connection = Database.connect(**conn_params)
|
||||||
self.connection.set_client_encoding('UTF8')
|
self.connection.set_client_encoding('UTF8')
|
||||||
# Set the time zone in autocommit mode (see #17062)
|
|
||||||
tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')
|
tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')
|
||||||
if tz:
|
if tz:
|
||||||
self.connection.set_isolation_level(
|
try:
|
||||||
psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
get_parameter_status = self.connection.get_parameter_status
|
||||||
self.connection.cursor().execute("SET TIME ZONE %s", [tz])
|
except AttributeError:
|
||||||
|
# psycopg2 < 2.0.12 doesn't have get_parameter_status
|
||||||
|
conn_tz = None
|
||||||
|
else:
|
||||||
|
conn_tz = get_parameter_status('TimeZone')
|
||||||
|
|
||||||
|
if conn_tz != tz:
|
||||||
|
# Set the time zone in autocommit mode (see #17062)
|
||||||
|
self.connection.set_isolation_level(
|
||||||
|
psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||||
|
self.connection.cursor().execute("SET TIME ZONE %s", [tz])
|
||||||
self.connection.set_isolation_level(self.isolation_level)
|
self.connection.set_isolation_level(self.isolation_level)
|
||||||
self._get_pg_version()
|
self._get_pg_version()
|
||||||
connection_created.send(sender=self.__class__, connection=self)
|
connection_created.send(sender=self.__class__, connection=self)
|
||||||
|
|
|
@ -33,6 +33,26 @@ aggregate with a database backend that falls within the affected release range.
|
||||||
.. _known to be faulty: http://archives.postgresql.org/pgsql-bugs/2007-07/msg00046.php
|
.. _known to be faulty: http://archives.postgresql.org/pgsql-bugs/2007-07/msg00046.php
|
||||||
.. _Release 8.2.5: http://developer.postgresql.org/pgdocs/postgres/release-8-2-5.html
|
.. _Release 8.2.5: http://developer.postgresql.org/pgdocs/postgres/release-8-2-5.html
|
||||||
|
|
||||||
|
Optimizing PostgreSQL's configuration
|
||||||
|
-------------------------------------
|
||||||
|
|
||||||
|
Django needs the following parameters for its database connections:
|
||||||
|
|
||||||
|
- ``client_encoding``: ``'UTF8'``,
|
||||||
|
- ``default_transaction_isolation``: ``'read committed'``,
|
||||||
|
- ``timezone``: ``'UTC'`` when :setting:`USE_TZ` is ``True``, value of
|
||||||
|
:setting:`TIME_ZONE` otherwise.
|
||||||
|
|
||||||
|
If these parameters already have the correct values, Django won't set them for
|
||||||
|
every new connection, which improves performance slightly. You can configure
|
||||||
|
them directly in :file:`postgresql.conf` or more conveniently per database
|
||||||
|
user with `ALTER ROLE`_.
|
||||||
|
|
||||||
|
Django will work just fine without this optimization, but each new connection
|
||||||
|
will do some additional queries to set these parameters.
|
||||||
|
|
||||||
|
.. _ALTER ROLE: http://www.postgresql.org/docs/current/interactive/sql-alterrole.html
|
||||||
|
|
||||||
Transaction handling
|
Transaction handling
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
|
@ -48,9 +68,8 @@ Autocommit mode
|
||||||
|
|
||||||
If your application is particularly read-heavy and doesn't make many
|
If your application is particularly read-heavy and doesn't make many
|
||||||
database writes, the overhead of a constantly open transaction can
|
database writes, the overhead of a constantly open transaction can
|
||||||
sometimes be noticeable. For those situations, if you're using the
|
sometimes be noticeable. For those situations, you can configure Django
|
||||||
``postgresql_psycopg2`` backend, you can configure Django to use
|
to use *"autocommit"* behavior for the connection, meaning that each database
|
||||||
*"autocommit"* behavior for the connection, meaning that each database
|
|
||||||
operation will normally be in its own transaction, rather than having
|
operation will normally be in its own transaction, rather than having
|
||||||
the transaction extend over multiple operations. In this case, you can
|
the transaction extend over multiple operations. In this case, you can
|
||||||
still manually start a transaction if you're doing something that
|
still manually start a transaction if you're doing something that
|
||||||
|
|
Loading…
Reference in New Issue