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

From Heikki Linnakangas
Subject Re: pg_trgm Memory Allocation logic
Date
Msg-id 54FDA97C.8020205@iki.fi
Whole thread Raw
In response to Re: pg_trgm Memory Allocation logic  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: pg_trgm Memory Allocation logic  (Beena Emerson <memissemerson@gmail.com>)
List pgsql-hackers
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




pgsql-hackers by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: pg_rewind in contrib
Next
From: Jim Nasby
Date:
Subject: Re: In-core regression tests for replication, cascading, archiving, PITR, etc.