Ticket #17266: 17266.2.diff

File 17266.2.diff, 3.6 KB (added by Aymeric Augustin, 13 years ago)
  • docs/ref/databases.txt

     
    3333.. _known to be faulty: http://archives.postgresql.org/pgsql-bugs/2007-07/msg00046.php
    3434.. _Release 8.2.5: http://developer.postgresql.org/pgdocs/postgres/release-8-2-5.html
    3535
     36Optimizing PostgreSQL's configuration
     37-------------------------------------
     38
     39Django 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
     46If these parameters already have the correct values, Django won't set them for
     47every new connection, which improves performance slightly. You can configure
     48them directly in :file:`postgresql.conf` or more conveniently per database
     49user with `ALTER ROLE`_.
     50
     51Django will work just fine without this optimization, but each new connection
     52will do some additional queries to set these parameters.
     53
     54.. _ALTER ROLE: http://www.postgresql.org/docs/current/interactive/sql-alterrole.html
     55
    3656Transaction handling
    3757---------------------
    3858
     
    4868
    4969If your application is particularly read-heavy and doesn't make many
    5070database 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
     71sometimes be noticeable. For those situations, you can configure Django
     72to use *"autocommit"* behavior for the connection, meaning that each database
    5473operation will normally be in its own transaction, rather than having
    5574the transaction extend over multiple operations. In this case, you can
    5675still manually start a transaction if you're doing something that
  • django/db/backends/postgresql_psycopg2/base.py

     
    174174                conn_params['port'] = settings_dict['PORT']
    175175            self.connection = Database.connect(**conn_params)
    176176            self.connection.set_client_encoding('UTF8')
    177             # Set the time zone in autocommit mode (see #17062)
    178177            tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')
    179178            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])
    183192            self.connection.set_isolation_level(self.isolation_level)
    184193            self._get_pg_version()
    185194            connection_created.send(sender=self.__class__, connection=self)
Back to Top