Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dVFsc-0002IG-Pd for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Jul 2017 11:32:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dVFsc-0005nB-CV for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Jul 2017 11:32:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dVFsb-0005lK-Nm for pgsql-bugs@postgresql.org; Wed, 12 Jul 2017 11:32:29 +0000 Received: from mail-lf0-x22e.google.com ([2a00:1450:4010:c07::22e]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dVFsZ-0002jU-I7 for pgsql-bugs@postgresql.org; Wed, 12 Jul 2017 11:32:29 +0000 Received: by mail-lf0-x22e.google.com with SMTP id h22so13574114lfk.3 for ; Wed, 12 Jul 2017 04:32:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to; bh=V9AoH30ZPYFQO5CPRR04nJxH8lKsF51PmQe/3rMzlpU=; b=FyTUTdmh6/WLmf5qvH/m+vaikDLQQ1+N+6S3ZXnMv+hkNoUdYUXF08OywFStKU/q+F 6vI/wdUYaYLW/yotuAhuR7VciKULOeW0CqmlwhCVbYn2/uH+TZ/jkWGDBFYLqrEnIr0k +yF1nGoC7i6kLuqxm0cpILXur8rUyKGCtRaBpglKFwq099VfkEReaZhnGqZHqkb4OTCR xGglcWmTXA9csnEUm687xLiDpDW0LImLkAyGvfc3Cmy+m/tY0ydp11R69RQKjFi+tjKd d3H/sB5Hk7v8ojgpXMpv0ksPbq1e4SptMvEUqJ+JR0zmgbtBWa6Xd2vYbFTm4X5PD1NR ljRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:references:from:message-id :date:user-agent:mime-version:in-reply-to; bh=V9AoH30ZPYFQO5CPRR04nJxH8lKsF51PmQe/3rMzlpU=; b=GXSTFYQTNKeb3nsT4FDVYvv9TbYzjYcihPELUdmdr9jt20pgDMShRRRzEdxbOxxMi0 1RwG3x/ksVRnGwL5VJeT+hCTSdI1hml4vC0/R0tizcTFHVo5uxM+NNykdKxEVc3w1FUq /kxjqgQCvF2dvJbbgFhE8DbbarZ039xrfx/TRE7iePMN9wlDsj26wVEFSiIAQ35zUYqo IMm+g9M/Ar6/tkCQpwG2YOvtGDL3U0ZHOEnjEUjARSe9PIyvFSu6/wnc40a1hJUoaOSC x0MkLwbyk/6CLmDNs2xdVsItENgpKqUQp9vC1riJorxx8R/gY2RCCGnS5tU4ZAVMKy/C lS9g== X-Gm-Message-State: AIVw112nFClj8kb+x8YQn87VpQr25I/GH6CYXaEoW1+I/9LvHhV5z6Ek 0P4IzfHyqcu8vdD1 X-Received: by 10.46.5.10 with SMTP id 10mr1772935ljf.3.1499859145969; Wed, 12 Jul 2017 04:32:25 -0700 (PDT) Received: from [192.168.1.99] (dsl-hkibng32-54fb4e-103.dhcp.inet.fi. [84.251.78.103]) by smtp.gmail.com with ESMTPSA id n89sm441792lje.20.2017.07.12.04.32.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jul 2017 04:32:14 -0700 (PDT) Subject: Re: BUG #14654: With high statistics targets on ts_vector, unexpectedly high memory use & OOM are triggered To: james+postgres@carbocation.com, pgsql-bugs@postgresql.org References: <20170514200602.1451.46797@wrigleys.postgresql.org> From: Heikki Linnakangas Message-ID: Date: Wed, 12 Jul 2017 14:32:11 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20170514200602.1451.46797@wrigleys.postgresql.org> Content-Type: multipart/mixed; boundary="------------A20EB56B20EDF90F5B9B19BC" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------A20EB56B20EDF90F5B9B19BC Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------A20EB56B20EDF90F5B9B19BC Content-Type: text/x-diff; name="reduce-tsvector-analyze-memory-usage.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="reduce-tsvector-analyze-memory-usage.patch" 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); } } } --------------A20EB56B20EDF90F5B9B19BC Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------A20EB56B20EDF90F5B9B19BC--