You probably could define an index on reverse(column) for that, then the wildcard is at the end of the string again and a B-tree index works. That index won't be good for anything else, though.
A heavier option is a trigram index, that should be able to speed up this kind of query as well.
Usually for those full search capabilities, inverted indexes are the structure of choice. Sadly those systems essentially try to break text into words, so certain code / numbering convention type of situations get fucked.
So I took a look at this, and I'm not sure how GIN would help with string wildcard prefix matches. It did just occur to me that you could have a b-tree comparator that sorted strings backwards to achieve that but of course that would incur two b-tree writes per value.
DUDE I'm going to have to try this. We work with millions of records and while we implement full-text indexing for human-readable fields, for something such as partial part number searches, this would be great.
I'm wondering if maybe full-text indexing would fits your needs.. It transliterates words into their base word and indexes them. Then when testing against the index, your query is also transliterated into the base words so that matches may be found.
If you're looking for true "starts with" or "ends with" functionality, I'd have to do some digging but seems possible.
289
u/little_blue_teapot Sep 24 '20
The major highlights:
Space savings and performance gains from de-duplication of B-tree index entries
Improved performance for queries that use aggregates or partitioned tables
Better query planning when using extended statistics
Parallelized vacuuming of indexes
Incremental sorting