On 03/09/2015 03:33 PM, Tom Lane wrote:
> Beena Emerson <memissemerson@gmail.com> writes:
>> In the pg_trgm module, within function generate_trgm, the memory for trigrams
>> is allocated as follows:
>
>> trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) *3);
>
>> I have been trying to understand why this is so because it seems to be
>> allocating more space than that is required.
>
> Consider input like 'X X X X X'. Each X produces 3 trigrams.
No it won't. Only two:
postgres=# select show_trgm('a b c'); show_trgm
--------------------------------------- {" a"," b"," c"," a "," b "," c "}
(1 row)
If you manually set RPADDING 2 in trgm.h, then it will, but the
allocation probably should use LPADDING/RPADDING to get it right, rather
than assume the max values.
- Heikki