Debian Codegraph @

django/contrib/postgres/indexes.py

raw
1 from django.db.models import Func, Index
2 from django.utils.functional import cached_property
3
4 __all__ = [
5 "BloomIndex",
6 "BrinIndex",
7 "BTreeIndex",
8 "GinIndex",
9 "GistIndex",
10 "HashIndex",
11 "SpGistIndex",
12 ]
13
14
15 class PostgresIndex(Index):
16 @cached_property
17 def max_name_length(self):
18 # Allow an index name longer than 30 characters when the suffix is
19 # longer than the usual 3 character limit. The 30 character limit for
20 # cross-database compatibility isn't applicable to PostgreSQL-specific
21 # indexes.
22 return Index.max_name_length - len(Index.suffix) + len(self.suffix)
23
24 def create_sql(self, model, schema_editor, using="", **kwargs):
25 self.check_supported(schema_editor)
26 statement = super().create_sql(
27 model, schema_editor, using=" USING %s" % (using or self.suffix), **kwargs
28 )
29 with_params = self.get_with_params()
30 if with_params:
31 statement.parts["extra"] = " WITH (%s)%s" % (
32 ", ".join(with_params),
33 statement.parts["extra"],
34 )
35 return statement
36
37 def check_supported(self, schema_editor):
38 pass
39
40 def get_with_params(self):
41 return []
42
43
44 class BloomIndex(PostgresIndex):
45 suffix = "bloom"
46
47 def __init__(self, *expressions, length=None, columns=(), **kwargs):
48 super().__init__(*expressions, **kwargs)
49 if len(self.fields) > 32:
50 raise ValueError("Bloom indexes support a maximum of 32 fields.")
51 if not isinstance(columns, (list, tuple)):
52 raise ValueError("BloomIndex.columns must be a list or tuple.")
53 if len(columns) > len(self.fields):
54 raise ValueError("BloomIndex.columns cannot have more values than fields.")
55 if not all(0 < col <= 4095 for col in columns):
56 raise ValueError(
57 "BloomIndex.columns must contain integers from 1 to 4095.",
58 )
59 if length is not None and not 0 < length <= 4096:
60 raise ValueError(
61 "BloomIndex.length must be None or an integer from 1 to 4096.",
62 )
63 self.length = length
64 self.columns = columns
65
66 def deconstruct(self):
67 path, args, kwargs = super().deconstruct()
68 if self.length is not None:
69 kwargs["length"] = self.length
70 if self.columns:
71 kwargs["columns"] = self.columns
72 return path, args, kwargs
73
74 def get_with_params(self):
75 with_params = []
76 if self.length is not None:
77 with_params.append("length = %d" % self.length)
78 if self.columns:
79 with_params.extend(
80 "col%d = %d" % (i, v) for i, v in enumerate(self.columns, start=1)
81 )
82 return with_params
83
84
85 class BrinIndex(PostgresIndex):
86 suffix = "brin"
87
88 def __init__(
89 self, *expressions, autosummarize=None, pages_per_range=None, **kwargs
90 ):
91 if pages_per_range is not None and pages_per_range <= 0:
92 raise ValueError("pages_per_range must be None or a positive integer")
93 self.autosummarize = autosummarize
94 self.pages_per_range = pages_per_range
95 super().__init__(*expressions, **kwargs)
96
97 def deconstruct(self):
98 path, args, kwargs = super().deconstruct()
99 if self.autosummarize is not None:
100 kwargs["autosummarize"] = self.autosummarize
101 if self.pages_per_range is not None:
102 kwargs["pages_per_range"] = self.pages_per_range
103 return path, args, kwargs
104
105 def get_with_params(self):
106 with_params = []
107 if self.autosummarize is not None:
108 with_params.append(
109 "autosummarize = %s" % ("on" if self.autosummarize else "off")
110 )
111 if self.pages_per_range is not None:
112 with_params.append("pages_per_range = %d" % self.pages_per_range)
113 return with_params
114
115
116 class BTreeIndex(PostgresIndex):
117 suffix = "btree"
118
119 def __init__(self, *expressions, fillfactor=None, deduplicate_items=None, **kwargs):
120 self.fillfactor = fillfactor
121 self.deduplicate_items = deduplicate_items
122 super().__init__(*expressions, **kwargs)
123
124 def deconstruct(self):
125 path, args, kwargs = super().deconstruct()
126 if self.fillfactor is not None:
127 kwargs["fillfactor"] = self.fillfactor
128 if self.deduplicate_items is not None:
129 kwargs["deduplicate_items"] = self.deduplicate_items
130 return path, args, kwargs
131
132 def get_with_params(self):
133 with_params = []
134 if self.fillfactor is not None:
135 with_params.append("fillfactor = %d" % self.fillfactor)
136 if self.deduplicate_items is not None:
137 with_params.append(
138 "deduplicate_items = %s" % ("on" if self.deduplicate_items else "off")
139 )
140 return with_params
141
142
143 class GinIndex(PostgresIndex):
144 suffix = "gin"
145
146 def __init__(
147 self, *expressions, fastupdate=None, gin_pending_list_limit=None, **kwargs
148 ):
149 self.fastupdate = fastupdate
150 self.gin_pending_list_limit = gin_pending_list_limit
151 super().__init__(*expressions, **kwargs)
152
153 def deconstruct(self):
154 path, args, kwargs = super().deconstruct()
155 if self.fastupdate is not None:
156 kwargs["fastupdate"] = self.fastupdate
157 if self.gin_pending_list_limit is not None:
158 kwargs["gin_pending_list_limit"] = self.gin_pending_list_limit
159 return path, args, kwargs
160
161 def get_with_params(self):
162 with_params = []
163 if self.gin_pending_list_limit is not None:
164 with_params.append(
165 "gin_pending_list_limit = %d" % self.gin_pending_list_limit
166 )
167 if self.fastupdate is not None:
168 with_params.append("fastupdate = %s" % ("on" if self.fastupdate else "off"))
169 return with_params
170
171
172 class GistIndex(PostgresIndex):
173 suffix = "gist"
174
175 def __init__(self, *expressions, buffering=None, fillfactor=None, **kwargs):
176 self.buffering = buffering
177 self.fillfactor = fillfactor
178 super().__init__(*expressions, **kwargs)
179
180 def deconstruct(self):
181 path, args, kwargs = super().deconstruct()
182 if self.buffering is not None:
183 kwargs["buffering"] = self.buffering
184 if self.fillfactor is not None:
185 kwargs["fillfactor"] = self.fillfactor
186 return path, args, kwargs
187
188 def get_with_params(self):
189 with_params = []
190 if self.buffering is not None:
191 with_params.append("buffering = %s" % ("on" if self.buffering else "off"))
192 if self.fillfactor is not None:
193 with_params.append("fillfactor = %d" % self.fillfactor)
194 return with_params
195
196
197 class HashIndex(PostgresIndex):
198 suffix = "hash"
199
200 def __init__(self, *expressions, fillfactor=None, **kwargs):
201 self.fillfactor = fillfactor
202 super().__init__(*expressions, **kwargs)
203
204 def deconstruct(self):
205 path, args, kwargs = super().deconstruct()
206 if self.fillfactor is not None:
207 kwargs["fillfactor"] = self.fillfactor
208 return path, args, kwargs
209
210 def get_with_params(self):
211 with_params = []
212 if self.fillfactor is not None:
213 with_params.append("fillfactor = %d" % self.fillfactor)
214 return with_params
215
216
217 class SpGistIndex(PostgresIndex):
218 suffix = "spgist"
219
220 def __init__(self, *expressions, fillfactor=None, **kwargs):
221 self.fillfactor = fillfactor
222 super().__init__(*expressions, **kwargs)
223
224 def deconstruct(self):
225 path, args, kwargs = super().deconstruct()
226 if self.fillfactor is not None:
227 kwargs["fillfactor"] = self.fillfactor
228 return path, args, kwargs
229
230 def get_with_params(self):
231 with_params = []
232 if self.fillfactor is not None:
233 with_params.append("fillfactor = %d" % self.fillfactor)
234 return with_params
235
236
237 class OpClass(Func):
238 template = "%(expressions)s %(name)s"
239 constraint_validation_compatible = False
240
241 def __init__(self, expression, name):
242 super().__init__(expression, name=name)