indexes.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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='', **kwargs):
  17. self.check_supported(schema_editor)
  18. statement = super().create_sql(model, schema_editor, using=' USING %s' % self.suffix, **kwargs)
  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 self.autosummarize and not schema_editor.connection.features.has_brin_autosummarize:
  47. raise NotSupportedError('BRIN option autosummarize requires PostgreSQL 10+.')
  48. def get_with_params(self):
  49. with_params = []
  50. if self.autosummarize is not None:
  51. with_params.append('autosummarize = %s' % ('on' if self.autosummarize else 'off'))
  52. if self.pages_per_range is not None:
  53. with_params.append('pages_per_range = %d' % self.pages_per_range)
  54. return with_params
  55. class BTreeIndex(PostgresIndex):
  56. suffix = 'btree'
  57. def __init__(self, *, fillfactor=None, **kwargs):
  58. self.fillfactor = fillfactor
  59. super().__init__(**kwargs)
  60. def deconstruct(self):
  61. path, args, kwargs = super().deconstruct()
  62. if self.fillfactor is not None:
  63. kwargs['fillfactor'] = self.fillfactor
  64. return path, args, kwargs
  65. def get_with_params(self):
  66. with_params = []
  67. if self.fillfactor is not None:
  68. with_params.append('fillfactor = %d' % self.fillfactor)
  69. return with_params
  70. class GinIndex(PostgresIndex):
  71. suffix = 'gin'
  72. def __init__(self, *, fastupdate=None, gin_pending_list_limit=None, **kwargs):
  73. self.fastupdate = fastupdate
  74. self.gin_pending_list_limit = gin_pending_list_limit
  75. super().__init__(**kwargs)
  76. def deconstruct(self):
  77. path, args, kwargs = super().deconstruct()
  78. if self.fastupdate is not None:
  79. kwargs['fastupdate'] = self.fastupdate
  80. if self.gin_pending_list_limit is not None:
  81. kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
  82. return path, args, kwargs
  83. def get_with_params(self):
  84. with_params = []
  85. if self.gin_pending_list_limit is not None:
  86. with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
  87. if self.fastupdate is not None:
  88. with_params.append('fastupdate = %s' % ('on' if self.fastupdate else 'off'))
  89. return with_params
  90. class GistIndex(PostgresIndex):
  91. suffix = 'gist'
  92. def __init__(self, *, buffering=None, fillfactor=None, **kwargs):
  93. self.buffering = buffering
  94. self.fillfactor = fillfactor
  95. super().__init__(**kwargs)
  96. def deconstruct(self):
  97. path, args, kwargs = super().deconstruct()
  98. if self.buffering is not None:
  99. kwargs['buffering'] = self.buffering
  100. if self.fillfactor is not None:
  101. kwargs['fillfactor'] = self.fillfactor
  102. return path, args, kwargs
  103. def get_with_params(self):
  104. with_params = []
  105. if self.buffering is not None:
  106. with_params.append('buffering = %s' % ('on' if self.buffering else 'off'))
  107. if self.fillfactor is not None:
  108. with_params.append('fillfactor = %d' % self.fillfactor)
  109. return with_params
  110. class HashIndex(PostgresIndex):
  111. suffix = 'hash'
  112. def __init__(self, *, fillfactor=None, **kwargs):
  113. self.fillfactor = fillfactor
  114. super().__init__(**kwargs)
  115. def deconstruct(self):
  116. path, args, kwargs = super().deconstruct()
  117. if self.fillfactor is not None:
  118. kwargs['fillfactor'] = self.fillfactor
  119. return path, args, kwargs
  120. def get_with_params(self):
  121. with_params = []
  122. if self.fillfactor is not None:
  123. with_params.append('fillfactor = %d' % self.fillfactor)
  124. return with_params
  125. class SpGistIndex(PostgresIndex):
  126. suffix = 'spgist'
  127. def __init__(self, *, fillfactor=None, **kwargs):
  128. self.fillfactor = fillfactor
  129. super().__init__(**kwargs)
  130. def deconstruct(self):
  131. path, args, kwargs = super().deconstruct()
  132. if self.fillfactor is not None:
  133. kwargs['fillfactor'] = self.fillfactor
  134. return path, args, kwargs
  135. def get_with_params(self):
  136. with_params = []
  137. if self.fillfactor is not None:
  138. with_params.append('fillfactor = %d' % self.fillfactor)
  139. return with_params