pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: james+postgres@carbocation.com
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
Date: Wed, 12 Jul 2017 14:32:11 +0300
Message-ID: <dc65ba89-46d1-07f2-3f94-51ba00446931@iki.fi> (raw)
In-Reply-To: <20170514200602.1451.46797@wrigleys.postgresql.org>
References: <20170514200602.1451.46797@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 05/14/2017 11:06 PM, james+postgres@carbocation.com wrote:
> It seems that ANALYZE on a ts_vector column can consume 300 * (statistics
> target) * (size of data in field), which in my case ended up being well
> above 10 gigabytes. I wonder if this might be considered a bug (either in
> code, or of documentation), as this memory usage seems not to obey other
> limits, or at least wasn't documented in a way that might have helped me
> guess at the underlying problem.

Yes, I can see that happening here too. The problem seems to be that the 
analyze-function detoasts every row in the sample. Tsvectors can be very 
large, so it adds up.

That's pretty easy to fix, the analyze function needs to free the 
detoasted copies as it goes. But in order to do that, it needs to make 
copies of all the lexemes stored in the hash table, instead of pointing 
directly to the detoasted copies.

Patch attached. I think this counts as a bug, and we should backport this.

- Heikki

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

reduce-tsvector-analyze-memory-usage.patch (text/x-diff, 2.1K) download diff --git a/src/backend/tsearch/ts_typanalyze.c b/src/backend/tsearch/ts_typanalyze.c index ab224b76b8..89dbe50dfd 100644 --- a/src/backend/tsearch/ts_typanalyze.c +++ b/src/backend/tsearch/ts_typanalyze.c @@ -232,9 +232,7 @@ compute_tsvector_stats(VacAttrStats *stats, /* * We loop through the lexemes in the tsvector and add them to our - * tracking hashtable. Note: the hashtable entries will point into - * the (detoasted) tsvector value, therefore we cannot free that - * storage until we're done. + * tracking hashtable. */ lexemesptr = STRPTR(vector); curentryptr = ARRPTR(vector); @@ -242,7 +240,12 @@ compute_tsvector_stats(VacAttrStats *stats, { bool found; - /* Construct a hash key */ + /* + * Construct a hash key. The key points to into the (detoasted) + * tsvector value at this point, but if a new entry is created, we + * will make a copy of it, so that we can free the tsvector value + * afterwards. + */ hash_key.lexeme = lexemesptr + curentryptr->pos; hash_key.length = curentryptr->len; @@ -261,6 +264,9 @@ compute_tsvector_stats(VacAttrStats *stats, /* Initialize new tracking list element */ item->frequency = 1; item->delta = b_current - 1; + + item->key.lexeme = palloc(hash_key.length); + memcpy(item->key.lexeme, hash_key.lexeme, hash_key.length); } /* lexeme_no is the number of elements processed (ie N) */ @@ -276,6 +282,10 @@ compute_tsvector_stats(VacAttrStats *stats, /* Advance to the next WordEntry in the tsvector */ curentryptr++; } + + /* If the vector was toasted, free the detoasted copy. */ + if (TSVectorGetDatum(vector) != value) + pfree(vector); } /* We can only compute real stats if we found some non-null values. */ @@ -447,9 +457,12 @@ prune_lexemes_hashtable(HTAB *lexemes_tab, int b_current) { if (item->frequency + item->delta <= b_current) { + char *lexeme = item->key.lexeme; + if (hash_search(lexemes_tab, (const void *) &item->key, HASH_REMOVE, NULL) == NULL) elog(ERROR, "hash table corrupted"); + pfree(lexeme); } } }

view thread (4+ messages)  latest in thread

Message-ID: <dc65ba89-46d1-07f2-3f94-51ba00446931@iki.fi>
Permalink:  ../dc65ba89-46d1-07f2-3f94-51ba00446931@iki.fi/
Also on:    postgresql.org/message-id/dc65ba89-46d1-07f2-3f94-51ba00446931@iki.fi

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hlinnaka@iki.fi, james+postgres@carbocation.com
  Subject: Re: BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
  In-Reply-To: <dc65ba89-46d1-07f2-3f94-51ba00446931@iki.fi>
  References: <20170514200602.1451.46797@wrigleys.postgresql.org> <dc65ba89-46d1-07f2-3f94-51ba00446931@iki.fi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox