If I use:
SET pg_trgm.similarity_threshold = 0.9;
... where column % 'some string s';
PostgreSQL does an index scan on: gin (column gin_trgm_ops)
In contrast, which should be the same:
... where similarity(column, 'some string s' ) >= 0.9
That does a seqential scan instead of using the index.
From the documentation: https://www.postgresql.org/docs/11/pgtrgm.html
text % text
boolean
Returns true if its arguments have a similarity that is greater than the current similarity threshold set bypg_trgm.similarity_threshold
.
Why?
Fundamentally, a WHERE
condition must look like this to allow an index scan:
<indexed expression> <operator> <constant>
Here, <indexed expression>
is what you created the index on <operator>
is an operator supported by the operator family of the index's operator class, and <constant>
is an expression that is constant for the duration of the index scan (in particular, it can only contain STABLE
functions).
The one exception in PostgreSQL is if you use a function returning boolean
in the WHERE
condition, and that function has an optimizer support function that allows PostgreSQL to substitute an index scan. But similarity
is no such function.