Re: pg_trgm Memory Allocation logic - Mailing list pgsql-hackers

From Heikki Linnakangas
Subject Re: pg_trgm Memory Allocation logic
Date
Msg-id 54FDA007.1060303@iki.fi
Whole thread Raw
In response to Re: pg_trgm Memory Allocation logic  (Alvaro Herrera <alvherre@2ndquadrant.com>)
List pgsql-hackers
On 03/09/2015 02:54 PM, Alvaro Herrera wrote:
> Beena Emerson wrote:
>> 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.
>
> Maybe it's considering a worst-case for multibyte characteres?  I don't
> really know if trgm supports multibyte, but I assume it does.  If it
> does, then probably the trigrams consist of chars, not bytes.

Nope. Trigrams are always three bytes, even ones containing multibyte 
characters. If there are any multibyte characters in the trigram, we 
store a 3-byte checksum of the three characters instead. That loses some 
information, you can have a collision where one multibyte trigram 
incorrectly matches another one, but the trigram algorithms are 
generally not too concerned about exact results.

- Heikki




pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Object files generated by ecpg test suite not ignored on Windows
Next
From: Tom Lane
Date:
Subject: Re: pg_trgm Memory Allocation logic