You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexes.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. from django.db.models import Index
  2. from django.db.utils import NotSupportedError
  3. from django.utils.functional import cached_property
  4. __all__ = [
  5. 'BrinIndex', 'BTreeIndex', 'GinIndex', 'GistIndex', 'HashIndex',
  6. 'SpGistIndex',
  7. ]
  8. class PostgresIndex(Index):
  9. @cached_property
  10. def max_name_length(self):
  11. # Allow an index name longer than 30 characters when the suffix is
  12. # longer than the usual 3 character limit. The 30 character limit for
  13. # cross-database compatibility isn't applicable to PostgreSQL-specific
  14. # indexes.
  15. return Index.max_name_length - len(Index.suffix) + len(self.suffix)
  16. def create_sql(self, model, schema_editor, using=''):
  17. self.check_supported(schema_editor)
  18. statement = super().create_sql(model, schema_editor, using=' USING %s' % self.suffix)
  19. with_params = self.get_with_params()
  20. if with_params:
  21. statement.parts['extra'] = 'WITH (%s) %s' % (
  22. ', '.join(with_params),
  23. statement.parts['extra'],
  24. )
  25. return statement
  26. def check_supported(self, schema_editor):
  27. pass
  28. def get_with_params(self):
  29. return []
  30. class BrinIndex(PostgresIndex):
  31. suffix = 'brin'
  32. def __init__(self, *, autosummarize=None, pages_per_range=None, **kwargs):
  33. if pages_per_range is not None and pages_per_range <= 0:
  34. raise ValueError('pages_per_range must be None or a positive integer')
  35. self.autosummarize = autosummarize
  36. self.pages_per_range = pages_per_range
  37. super().__init__(**kwargs)
  38. def deconstruct(self):
  39. path, args, kwargs = super().deconstruct()
  40. if self.autosummarize is not None:
  41. kwargs['autosummarize'] = self.autosummarize
  42. if self.pages_per_range is not None:
  43. kwargs['pages_per_range'] = self.pages_per_range
  44. return path, args, kwargs
  45. def check_supported(self, schema_editor):
  46. if not schema_editor.connection.features.has_brin_index_support:
  47. raise NotSupportedError('BRIN indexes require PostgreSQL 9.5+.')
  48. if self.autosummarize and not schema_editor.connection.features.has_brin_autosummarize:
  49. raise NotSupportedError('BRIN option autosummarize requires PostgreSQL 10+.')
  50. def get_with_params(self):
  51. with_params = []
  52. if self.autosummarize is not None:
  53. with_params.append('autosummarize = %s' % ('on' if self.autosummarize else 'off'))
  54. if self.pages_per_range is not None:
  55. with_params.append('pages_per_range = %d' % self.pages_per_range)
  56. return with_params
  57. class BTreeIndex(PostgresIndex):
  58. suffix = 'btree'
  59. def __init__(self, *, fillfactor=None, **kwargs):
  60. self.fillfactor = fillfactor
  61. super().__init__(**kwargs)
  62. def deconstruct(self):
  63. path, args, kwargs = super().deconstruct()
  64. if self.fillfactor is not None:
  65. kwargs['fillfactor'] = self.fillfactor
  66. return path, args, kwargs
  67. def get_with_params(self):
  68. with_params = []
  69. if self.fillfactor is not None:
  70. with_params.append('fillfactor = %d' % self.fillfactor)
  71. return with_params
  72. class GinIndex(PostgresIndex):
  73. suffix = 'gin'
  74. def __init__(self, *, fastupdate=None, gin_pending_list_limit=None, **kwargs):
  75. self.fastupdate = fastupdate
  76. self.gin_pending_list_limit = gin_pending_list_limit
  77. super().__init__(**kwargs)
  78. def deconstruct(self):
  79. path, args, kwargs = super().deconstruct()
  80. if self.fastupdate is not None:
  81. kwargs['fastupdate'] = self.fastupdate
  82. if self.gin_pending_list_limit is not None:
  83. kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
  84. return path, args, kwargs
  85. def check_supported(self, schema_editor):
  86. if self.gin_pending_list_limit and not schema_editor.connection.features.has_gin_pending_list_limit:
  87. raise NotSupportedError('GIN option gin_pending_list_limit requires PostgreSQL 9.5+.')
  88. def get_with_params(self):
  89. with_params = []
  90. if self.gin_pending_list_limit is not None:
  91. with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
  92. if self.fastupdate is not None:
  93. with_params.append('fastupdate = %s' % ('on' if self.fastupdate else 'off'))
  94. return with_params
  95. class GistIndex(PostgresIndex):
  96. suffix = 'gist'
  97. def __init__(self, *, buffering=None, fillfactor=None, **kwargs):
  98. self.buffering = buffering
  99. self.fillfactor = fillfactor
  100. super().__init__(**kwargs)
  101. def deconstruct(self):
  102. path, args, kwargs = super().deconstruct()
  103. if self.buffering is not None:
  104. kwargs['buffering'] = self.buffering
  105. if self.fillfactor is not None:
  106. kwargs['fillfactor'] = self.fillfactor
  107. return path, args, kwargs
  108. def get_with_params(self):
  109. with_params = []
  110. if self.buffering is not None:
  111. with_params.append('buffering = %s' % ('on' if self.buffering else 'off'))
  112. if self.fillfactor is not None:
  113. with_params.append('fillfactor = %d' % self.fillfactor)
  114. return with_params
  115. class HashIndex(PostgresIndex):
  116. suffix = 'hash'
  117. def __init__(self, *, fillfactor=None, **kwargs):
  118. self.fillfactor = fillfactor
  119. super().__init__(**kwargs)
  120. def deconstruct(self):
  121. path, args, kwargs = super().deconstruct()
  122. if self.fillfactor is not None:
  123. kwargs['fillfactor'] = self.fillfactor
  124. return path, args, kwargs
  125. def get_with_params(self):
  126. with_params = []
  127. if self.fillfactor is not None:
  128. with_params.append('fillfactor = %d' % self.fillfactor)
  129. return with_params
  130. class SpGistIndex(PostgresIndex):
  131. suffix = 'spgist'
  132. def __init__(self, *, fillfactor=None, **kwargs):
  133. self.fillfactor = fillfactor
  134. super().__init__(**kwargs)
  135. def deconstruct(self):
  136. path, args, kwargs = super().deconstruct()
  137. if self.fillfactor is not None:
  138. kwargs['fillfactor'] = self.fillfactor
  139. return path, args, kwargs
  140. def get_with_params(self):
  141. with_params = []
  142. if self.fillfactor is not None:
  143. with_params.append('fillfactor = %d' % self.fillfactor)
  144. return with_params