Re: pg_trgm Memory Allocation logic

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Beena Emerson <memissemerson(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: pg_trgm Memory Allocation logic
Date: 2015-03-09 14:09:00
Message-ID: 54FDA97C.8020205@iki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 03/09/2015 03:33 PM, Tom Lane wrote:
> Beena Emerson <memissemerson(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jim Nasby 2015-03-09 14:15:20 Re: In-core regression tests for replication, cascading, archiving, PITR, etc.
Previous Message Heikki Linnakangas 2015-03-09 14:02:46 Re: pg_rewind in contrib