trytond-patches/issue12480.diff

53 lines
2.0 KiB
Diff

diff --git a/tryton/trytond/trytond/backend/postgresql/table.py b/tryton/trytond/trytond/backend/postgresql/table.py
index 75c4c8b276..8a8fc7a3cb 100644
--- a/tryton/trytond/trytond/backend/postgresql/table.py
+++ b/tryton/trytond/trytond/backend/postgresql/table.py
@@ -571,7 +571,18 @@ class IndexMixin:
@classmethod
def _get_indexed_expressions(cls, index):
- return index.expressions
+ database = Transaction().database
+ has_btree_gin = database.has_extension('btree_gin')
+ has_trigram = database.has_extension('pg_trgm')
+
+ def filter(usage):
+ if usage.__class__.__name__ == 'Similarity':
+ return has_trigram
+ elif usage.__class__.__name__ in {'Range', 'Equality'}:
+ return has_btree_gin
+ else:
+ return False
+ return [(e, u) for e, u in index.expressions if filter(u)]
@classmethod
def _get_expression_variables(cls, expression, usage):
@@ -647,14 +658,24 @@ class TrigramTranslator(IndexMixin, IndexTranslatorInterface):
@classmethod
def score(cls, index):
- has_trigram = Transaction().database.has_extension('pg_trgm')
- if not has_trigram:
+ database = Transaction().database
+ has_btree_gin = database.has_extension('btree_gin')
+ has_trigram = database.has_extension('pg_trgm')
+ if not has_btree_gin and not has_trigram:
return 0
score = 0
for _, usage in index.expressions:
if usage.__class__.__name__ == 'Similarity':
- score += 100
+ if has_trigram:
+ score += 100
+ else:
+ score += 50
+ elif has_btree_gin:
+ if usage.__class__.__name__ == 'Range':
+ score += 90
+ elif usage.__class__.__name__ == 'Equality':
+ score += 40
else:
return 0
return score