From d526b07784d8caa208055c1f2bd1cedc88fb52dc Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Tue, 5 Dec 2017 09:43:56 +0000 Subject: [PATCH] Fixed #26974 -- Added HashIndex to django.contrib.postgres. Thanks Akshesh Doshi for the initial implementation. --- django/contrib/postgres/indexes.py | 22 ++++++++++++- docs/ref/contrib/postgres/indexes.txt | 19 +++++++++++ docs/releases/2.2.txt | 3 ++ tests/postgres_tests/test_indexes.py | 46 ++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/django/contrib/postgres/indexes.py b/django/contrib/postgres/indexes.py index 677c63b4a2..3b77b97f3e 100644 --- a/django/contrib/postgres/indexes.py +++ b/django/contrib/postgres/indexes.py @@ -1,7 +1,7 @@ from django.db.models import Index from django.utils.functional import cached_property -__all__ = ['BrinIndex', 'GinIndex', 'GistIndex'] +__all__ = ['BrinIndex', 'GinIndex', 'GistIndex', 'HashIndex'] class PostgresIndex(Index): @@ -98,3 +98,23 @@ class GistIndex(PostgresIndex): if self.fillfactor is not None: with_params.append('fillfactor = %d' % self.fillfactor) return with_params + + +class HashIndex(PostgresIndex): + suffix = 'hash' + + def __init__(self, *, fillfactor=None, **kwargs): + self.fillfactor = fillfactor + super().__init__(**kwargs) + + def deconstruct(self): + path, args, kwargs = super().deconstruct() + if self.fillfactor is not None: + kwargs['fillfactor'] = self.fillfactor + return path, args, kwargs + + def get_with_params(self): + with_params = [] + if self.fillfactor is not None: + with_params.append('fillfactor = %d' % self.fillfactor) + return with_params diff --git a/docs/ref/contrib/postgres/indexes.txt b/docs/ref/contrib/postgres/indexes.txt index e7c681e6ba..2d966553c3 100644 --- a/docs/ref/contrib/postgres/indexes.txt +++ b/docs/ref/contrib/postgres/indexes.txt @@ -72,3 +72,22 @@ available from the ``django.contrib.postgres.indexes`` module. .. _buffering build: https://www.postgresql.org/docs/current/static/gist-implementation.html#GIST-BUFFERING-BUILD .. _fillfactor: https://www.postgresql.org/docs/current/static/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS + +``HashIndex`` +============= + +.. class:: HashIndex(fillfactor=None, **options) + + .. versionadded:: 2.2 + + Creates a hash index. + + Provide an integer value from 10 to 100 to the fillfactor_ parameter to + tune how packed the index pages will be. PostgreSQL's default is 90. + + .. admonition:: Use this index only on PostgreSQL 10 and later + + Hash indexes have been available in PostgreSQL for a long time, but + they suffer from a number of data integrity issues in older versions. + + .. _fillfactor: https://www.postgresql.org/docs/current/static/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt index a581d8d317..cf3e9bff80 100644 --- a/docs/releases/2.2.txt +++ b/docs/releases/2.2.txt @@ -82,6 +82,9 @@ Minor features :class:`~django.contrib.postgres.aggregates.StringAgg` determines the ordering of the aggregated elements. +* The new :class:`~django.contrib.postgres.indexes.HashIndex` class + allows creating ``hash`` indexes in the database. + :mod:`django.contrib.redirects` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py index defab8530a..dcb285cc1c 100644 --- a/tests/postgres_tests/test_indexes.py +++ b/tests/postgres_tests/test_indexes.py @@ -1,4 +1,6 @@ -from django.contrib.postgres.indexes import BrinIndex, GinIndex, GistIndex +from django.contrib.postgres.indexes import ( + BrinIndex, GinIndex, GistIndex, HashIndex, +) from django.db import connection from django.test import skipUnlessDBFeature @@ -83,6 +85,20 @@ class GistIndexTests(IndexTestMixin, PostgreSQLTestCase): }) +class HashIndexTests(IndexTestMixin, PostgreSQLTestCase): + index_class = HashIndex + + def test_suffix(self): + self.assertEqual(HashIndex.suffix, 'hash') + + def test_deconstruction(self): + index = HashIndex(fields=['title'], name='test_title_hash', fillfactor=80) + path, args, kwargs = index.deconstruct() + self.assertEqual(path, 'django.contrib.postgres.indexes.HashIndex') + self.assertEqual(args, ()) + self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_hash', 'fillfactor': 80}) + + class SchemaTests(PostgreSQLTestCase): def get_constraints(self, table): @@ -173,3 +189,31 @@ class SchemaTests(PostgreSQLTestCase): with connection.schema_editor() as editor: editor.remove_index(CharFieldModel, index) self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table)) + + def test_hash_index(self): + # Ensure the table is there and doesn't have an index. + self.assertNotIn('field', self.get_constraints(CharFieldModel._meta.db_table)) + # Add the index. + index_name = 'char_field_model_field_hash' + index = HashIndex(fields=['field'], name=index_name) + with connection.schema_editor() as editor: + editor.add_index(CharFieldModel, index) + constraints = self.get_constraints(CharFieldModel._meta.db_table) + # The index was added. + self.assertEqual(constraints[index_name]['type'], HashIndex.suffix) + # Drop the index. + with connection.schema_editor() as editor: + editor.remove_index(CharFieldModel, index) + self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table)) + + def test_hash_parameters(self): + index_name = 'integer_array_hash_fillfactor' + index = HashIndex(fields=['field'], name=index_name, fillfactor=80) + with connection.schema_editor() as editor: + editor.add_index(CharFieldModel, index) + constraints = self.get_constraints(CharFieldModel._meta.db_table) + self.assertEqual(constraints[index_name]['type'], HashIndex.suffix) + self.assertEqual(constraints[index_name]['options'], ['fillfactor=80']) + with connection.schema_editor() as editor: + editor.remove_index(CharFieldModel, index) + self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))