Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10142 -- Added docs and an exploding error message to highligh…

…t an error present in the implementation of STDDEV_POP and VAR_POP in PostgreSQL 8.2-8.2.4 that will give incorrect answers (the database function mistakenly returns sample, rather than population deviation/variance). Thanks to Vinay Sajip <> for the report, and Ian Kelly for pointing out the cause of the problem.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 65ad2132abd4c56f38c43b0ba9a23ed716733728 1 parent 2b8e768
Russell Keith-Magee freakboy3742 authored
9 django/db/backends/
@@ -398,6 +398,15 @@ def convert_values(self, value, field):
# Default to a float
return float(value)
+ def check_aggregate_support(self, aggregate_func):
+ """Check that the backend supports the provided aggregate
+ This is used on specific backends to rule out known aggregates
+ that are known to have faulty implementations. If the named
+ aggregate function has a known problem, the backend should
+ raise NotImplemented.
+ """
+ pass
class BaseDatabaseIntrospection(object):
11 django/db/backends/postgresql/
@@ -144,3 +144,14 @@ def savepoint_rollback_sql(self, sid):
def prep_for_iexact_query(self, x):
return x
+ def check_aggregate_support(self, aggregate):
+ """Check that the backend fully supports the provided aggregate.
+ The implementation of population statistics (STDDEV_POP and VAR_POP)
+ under Postgres 8.2 - 8.2.4 is known to be faulty. Raise
+ NotImplementedError if this is the database in use.
+ """
+ if aggregate.sql_function == 'STDDEV_POP' or aggregate.sql_function == 'VAR_POP':
+ if self.postgres_version[0] == 8 and self.postgres_version[1] == 2 and self.postgres_version[1] <= 4:
+ raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
8 django/db/models/
@@ -41,8 +41,12 @@ def add_to_query(self, query, alias, col, source, is_summary):
* is_summary is a boolean that is set True if the aggregate is a
summary value rather than an annotation.
- aggregate = getattr(query.aggregates_module,
- query.aggregate_select[alias] = aggregate(col, source=source, is_summary=is_summary, **self.extra)
+ klass = getattr(query.aggregates_module,
+ aggregate = klass(col, source=source, is_summary=is_summary, **self.extra)
+ # Validate that the backend has a fully supported, correct
+ # implementation of this aggregate
+ query.connection.ops.check_aggregate_support(aggregate)
+ query.aggregate_select[alias] = aggregate
class Avg(Aggregate):
name = 'Avg'
16 docs/ref/databases.txt
@@ -13,6 +13,22 @@ This file describes some of the features that might be relevant to Django
usage. Of course, it is not intended as a replacement for server-specific
documentation or reference manuals.
+PostgreSQL notes
+PostgreSQL 8.2 to 8.2.4
+The implementation of the population statistics aggregates ``STDDEV_POP`` and
+``VAR_POP`` that shipped with PostgreSQL 8.2 to 8.2.4 are `known to be
+faulty`_. Users of these releases of PostgreSQL are advised to upgrade to
+`Release 8.2.5`_ or later. Django will raise a ``NotImplementedError`` if you
+attempt to use the ``StdDev(sample=False)`` or ``Variance(sample=False)``
+aggregate with an database backend falls within the affected release range.
+.. _known to be faulty:
+.. _Release 8.2.5:
.. _mysql-notes:
MySQL notes
Please sign in to comment.
Something went wrong with that request. Please try again.