pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
4+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2017-07-12 209970ded8 852902822e 11854dee01 40ba61b447 bbeec3c749 da11977de9  Reduce memory usage of tsvector type analyze function.
* BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
@ 2017-05-14 20:06  james+postgres@carbocation.com
  0 siblings, 1 reply; 4+ messages in thread

From: james+postgres@carbocation.com @ 2017-05-14 20:06 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14654
Logged by:          James C
Email address:      james+postgres@carbocation.com
PostgreSQL version: 9.5.5
Operating system:   Linux
Description:        

I am running a mirror of PubMed, which contains about 27 million articles.
One of my tables (article) includes a unique ID for each article, the
article title, a ts_vector of the abstract, some timestamps, and a numeric
column. There are (unsurprisingly) about 27 million rows in this table.
Because the abstracts contain scientific terms, the number of lexemes is
quite large.

With some prototyping on a local machine with 64G of RAM, I found queries to
work quite well with the the ts_vector column having default statistics of
10,000. The production box has about 12G of RAM, but initially I didn't
notice any issues.

Today, I attempted to execute 'ANALYZE article;' However, the OOM killer
kept taking down postgres. I reviewed numerous system settings and postgres
settings, all of which seemed to properly give about 10G+ of RAM to
postgres, and which should have constrained postgres to using less than that
limit.

RhodiumToad in Freenode#postgres helped me troubleshoot, and suggested I try
reducing the statistics on the ts_vector. Dropping from 10,000 to 1,000
permitted ANALYZE to run without consuming large amounts of RAM.

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.


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


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
@ 2017-07-12 11:32  Heikki Linnakangas <hlinnaka@iki.fi>
  parent: james+postgres@carbocation.com
  0 siblings, 1 reply; 4+ messages in thread

From: Heikki Linnakangas @ 2017-07-12 11:32 UTC (permalink / raw)
  To: james+postgres@carbocation.com; pgsql-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); } } }
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
@ 2017-07-12 15:30  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2017-07-12 15:30 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnaka@iki.fi>; +Cc: james+postgres@carbocation.com; pgsql-bugs

Heikki Linnakangas <hlinnaka@iki.fi> writes:
> 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.

+1.  I didn't test the patch, but it looks sane to the eyeball.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered
@ 2017-07-12 19:09  Heikki Linnakangas <hlinnaka@iki.fi>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Heikki Linnakangas @ 2017-07-12 19:09 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: james+postgres@carbocation.com; pgsql-bugs

On 07/12/2017 06:30 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnaka@iki.fi> writes:
>> 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.
>
> +1.  I didn't test the patch, but it looks sane to the eyeball.

Ok, committed.

In some quick testing on my laptop, and the extra palloc+pfree adds 
about 10% of overhead, in the worst case scenario that every tsvector in 
the sample consists of totally unique lexemes. That's a bit unfortunate, 
but it's a lot better than consuming gigabytes of memory.

- Heikki



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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2017-07-12 19:09 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-05-14 20:06 BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered james+postgres@carbocation.com
2017-07-12 11:32 ` Heikki Linnakangas <hlinnaka@iki.fi>
2017-07-12 15:30   ` Tom Lane <tgl@sss.pgh.pa.us>
2017-07-12 19:09     ` Heikki Linnakangas <hlinnaka@iki.fi>

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