2019-08-20 15:54:41 +08:00
|
|
|
from django.db import NotSupportedError
|
2017-01-16 03:09:38 +08:00
|
|
|
from django.db.models import Index
|
2017-12-05 17:09:09 +08:00
|
|
|
from django.utils.functional import cached_property
|
2016-08-08 19:50:25 +08:00
|
|
|
|
2018-07-31 06:28:11 +08:00
|
|
|
__all__ = [
|
2019-10-18 17:08:50 +08:00
|
|
|
'BloomIndex', 'BrinIndex', 'BTreeIndex', 'GinIndex', 'GistIndex',
|
|
|
|
'HashIndex', 'SpGistIndex',
|
2018-07-31 06:28:11 +08:00
|
|
|
]
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
|
2017-12-05 17:09:09 +08:00
|
|
|
class PostgresIndex(Index):
|
2016-10-13 20:39:44 +08:00
|
|
|
|
2017-12-05 17:09:09 +08:00
|
|
|
@cached_property
|
|
|
|
def max_name_length(self):
|
|
|
|
# Allow an index name longer than 30 characters when the suffix is
|
|
|
|
# longer than the usual 3 character limit. The 30 character limit for
|
|
|
|
# cross-database compatibility isn't applicable to PostgreSQL-specific
|
|
|
|
# indexes.
|
|
|
|
return Index.max_name_length - len(Index.suffix) + len(self.suffix)
|
2017-07-01 21:30:34 +08:00
|
|
|
|
2019-07-25 19:44:18 +08:00
|
|
|
def create_sql(self, model, schema_editor, using='', **kwargs):
|
2017-12-05 17:15:22 +08:00
|
|
|
self.check_supported(schema_editor)
|
2019-07-25 19:44:18 +08:00
|
|
|
statement = super().create_sql(model, schema_editor, using=' USING %s' % self.suffix, **kwargs)
|
2018-01-11 03:59:15 +08:00
|
|
|
with_params = self.get_with_params()
|
|
|
|
if with_params:
|
|
|
|
statement.parts['extra'] = 'WITH (%s) %s' % (
|
|
|
|
', '.join(with_params),
|
|
|
|
statement.parts['extra'],
|
|
|
|
)
|
|
|
|
return statement
|
|
|
|
|
2017-12-05 17:15:22 +08:00
|
|
|
def check_supported(self, schema_editor):
|
|
|
|
pass
|
|
|
|
|
2018-01-11 03:59:15 +08:00
|
|
|
def get_with_params(self):
|
|
|
|
return []
|
|
|
|
|
2017-12-05 17:09:09 +08:00
|
|
|
|
2019-10-18 17:08:50 +08:00
|
|
|
class BloomIndex(PostgresIndex):
|
|
|
|
suffix = 'bloom'
|
|
|
|
|
|
|
|
def __init__(self, *, length=None, columns=(), **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
if len(self.fields) > 32:
|
|
|
|
raise ValueError('Bloom indexes support a maximum of 32 fields.')
|
|
|
|
if not isinstance(columns, (list, tuple)):
|
|
|
|
raise ValueError('BloomIndex.columns must be a list or tuple.')
|
|
|
|
if len(columns) > len(self.fields):
|
|
|
|
raise ValueError(
|
|
|
|
'BloomIndex.columns cannot have more values than fields.'
|
|
|
|
)
|
|
|
|
if not all(0 < col <= 4095 for col in columns):
|
|
|
|
raise ValueError(
|
|
|
|
'BloomIndex.columns must contain integers from 1 to 4095.',
|
|
|
|
)
|
|
|
|
if length is not None and not 0 < length <= 4096:
|
|
|
|
raise ValueError(
|
|
|
|
'BloomIndex.length must be None or an integer from 1 to 4096.',
|
|
|
|
)
|
|
|
|
self.length = length
|
|
|
|
self.columns = columns
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
|
|
|
if self.length is not None:
|
|
|
|
kwargs['length'] = self.length
|
|
|
|
if self.columns:
|
|
|
|
kwargs['columns'] = self.columns
|
|
|
|
return path, args, kwargs
|
|
|
|
|
|
|
|
def get_with_params(self):
|
|
|
|
with_params = []
|
|
|
|
if self.length is not None:
|
|
|
|
with_params.append('length = %d' % self.length)
|
|
|
|
if self.columns:
|
|
|
|
with_params.extend(
|
|
|
|
'col%d = %d' % (i, v)
|
|
|
|
for i, v in enumerate(self.columns, start=1)
|
|
|
|
)
|
|
|
|
return with_params
|
|
|
|
|
|
|
|
|
2017-12-05 17:09:09 +08:00
|
|
|
class BrinIndex(PostgresIndex):
|
2017-07-01 21:30:34 +08:00
|
|
|
suffix = 'brin'
|
|
|
|
|
2018-01-05 19:53:08 +08:00
|
|
|
def __init__(self, *, autosummarize=None, pages_per_range=None, **kwargs):
|
2017-01-17 00:28:30 +08:00
|
|
|
if pages_per_range is not None and pages_per_range <= 0:
|
|
|
|
raise ValueError('pages_per_range must be None or a positive integer')
|
2018-01-05 19:53:08 +08:00
|
|
|
self.autosummarize = autosummarize
|
2016-10-13 20:39:44 +08:00
|
|
|
self.pages_per_range = pages_per_range
|
2017-06-28 01:39:37 +08:00
|
|
|
super().__init__(**kwargs)
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
path, args, kwargs = super().deconstruct()
|
2018-01-05 19:53:08 +08:00
|
|
|
if self.autosummarize is not None:
|
|
|
|
kwargs['autosummarize'] = self.autosummarize
|
2017-08-26 05:54:36 +08:00
|
|
|
if self.pages_per_range is not None:
|
|
|
|
kwargs['pages_per_range'] = self.pages_per_range
|
2016-10-13 20:39:44 +08:00
|
|
|
return path, args, kwargs
|
|
|
|
|
2017-12-05 17:15:22 +08:00
|
|
|
def check_supported(self, schema_editor):
|
|
|
|
if self.autosummarize and not schema_editor.connection.features.has_brin_autosummarize:
|
|
|
|
raise NotSupportedError('BRIN option autosummarize requires PostgreSQL 10+.')
|
|
|
|
|
2018-01-11 03:59:15 +08:00
|
|
|
def get_with_params(self):
|
|
|
|
with_params = []
|
2018-01-05 19:53:08 +08:00
|
|
|
if self.autosummarize is not None:
|
|
|
|
with_params.append('autosummarize = %s' % ('on' if self.autosummarize else 'off'))
|
2016-10-13 20:39:44 +08:00
|
|
|
if self.pages_per_range is not None:
|
2018-01-11 03:59:15 +08:00
|
|
|
with_params.append('pages_per_range = %d' % self.pages_per_range)
|
|
|
|
return with_params
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
|
2018-07-31 06:28:11 +08:00
|
|
|
class BTreeIndex(PostgresIndex):
|
|
|
|
suffix = 'btree'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-12-05 17:09:09 +08:00
|
|
|
class GinIndex(PostgresIndex):
|
2016-08-08 19:50:25 +08:00
|
|
|
suffix = 'gin'
|
|
|
|
|
2017-06-28 01:39:37 +08:00
|
|
|
def __init__(self, *, fastupdate=None, gin_pending_list_limit=None, **kwargs):
|
2017-05-31 05:17:32 +08:00
|
|
|
self.fastupdate = fastupdate
|
|
|
|
self.gin_pending_list_limit = gin_pending_list_limit
|
2017-06-28 01:39:37 +08:00
|
|
|
super().__init__(**kwargs)
|
2017-05-31 05:17:32 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
2017-08-26 22:32:03 +08:00
|
|
|
if self.fastupdate is not None:
|
|
|
|
kwargs['fastupdate'] = self.fastupdate
|
|
|
|
if self.gin_pending_list_limit is not None:
|
|
|
|
kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
|
2017-05-31 05:17:32 +08:00
|
|
|
return path, args, kwargs
|
|
|
|
|
2018-01-11 03:59:15 +08:00
|
|
|
def get_with_params(self):
|
2017-05-31 05:17:32 +08:00
|
|
|
with_params = []
|
|
|
|
if self.gin_pending_list_limit is not None:
|
|
|
|
with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
|
|
|
|
if self.fastupdate is not None:
|
2018-01-11 03:59:15 +08:00
|
|
|
with_params.append('fastupdate = %s' % ('on' if self.fastupdate else 'off'))
|
|
|
|
return with_params
|
2017-07-01 21:30:34 +08:00
|
|
|
|
|
|
|
|
2017-12-05 17:09:09 +08:00
|
|
|
class GistIndex(PostgresIndex):
|
2017-07-01 21:30:34 +08:00
|
|
|
suffix = 'gist'
|
|
|
|
|
|
|
|
def __init__(self, *, buffering=None, fillfactor=None, **kwargs):
|
|
|
|
self.buffering = buffering
|
|
|
|
self.fillfactor = fillfactor
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
|
|
|
if self.buffering is not None:
|
|
|
|
kwargs['buffering'] = self.buffering
|
|
|
|
if self.fillfactor is not None:
|
|
|
|
kwargs['fillfactor'] = self.fillfactor
|
|
|
|
return path, args, kwargs
|
|
|
|
|
2018-01-11 03:59:15 +08:00
|
|
|
def get_with_params(self):
|
2017-07-01 21:30:34 +08:00
|
|
|
with_params = []
|
|
|
|
if self.buffering is not None:
|
2018-01-11 03:59:15 +08:00
|
|
|
with_params.append('buffering = %s' % ('on' if self.buffering else 'off'))
|
2017-07-01 21:30:34 +08:00
|
|
|
if self.fillfactor is not None:
|
2017-12-05 17:52:21 +08:00
|
|
|
with_params.append('fillfactor = %d' % self.fillfactor)
|
2018-01-11 03:59:15 +08:00
|
|
|
return with_params
|
2017-12-05 17:43:56 +08:00
|
|
|
|
2019-10-31 20:33:53 +08:00
|
|
|
def check_supported(self, schema_editor):
|
|
|
|
if self.include and not schema_editor.connection.features.supports_covering_gist_indexes:
|
|
|
|
raise NotSupportedError('Covering GiST indexes requires PostgreSQL 12+.')
|
|
|
|
|
2017-12-05 17:43:56 +08:00
|
|
|
|
|
|
|
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
|
2017-12-05 17:48:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SpGistIndex(PostgresIndex):
|
|
|
|
suffix = 'spgist'
|
|
|
|
|
|
|
|
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
|