pgsql: Fix serious performance problems in json(b) to_tsvector().

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix serious performance problems in json(b) to_tsvector().
Date: 2017-07-18 16:46:02
Message-ID: E1dXVdK-0005Rc-Cw@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix serious performance problems in json(b) to_tsvector().

In an off-list followup to bug #14745, Bob Jones complained that
to_tsvector() on a 2MB jsonb value took an unreasonable amount of
time and space --- enough to draw the wrath of the OOM killer on
his machine. On my machine, his example proved to require upwards
of 18 seconds and 4GB, which seemed pretty bogus considering that
to_tsvector() on the same data treated as text took just a couple
hundred msec and 10 or so MB.

On investigation, the problem is that the implementation scans each
string element of the json(b) and converts it to tsvector separately,
then applies tsvector_concat() to join those separate tsvectors.
The unreasonable memory usage came from leaking every single one of
the transient tsvectors --- but even without that mistake, this is an
O(N^2) or worse algorithm, because tsvector_concat() has to repeatedly
process the words coming from earlier elements.

We can fix it by accumulating all the lexeme data and applying
make_tsvector() just once. As a side benefit, that also makes the
desired adjustment of lexeme positions far cheaper, because we can
just tweak the running "pos" counter between JSON elements.

In passing, try to make the explanation of that tweak more intelligible.
(I didn't think that a barely-readable comment far removed from the
actual code was helpful.) And do some minor other code beautification.

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b4c6d31c0be0f5c42a75d50afcf13bdd392db4a1

Modified Files
--------------
src/backend/tsearch/to_tsany.c | 120 ++++++++++++++++++++---------------------
src/include/tsearch/ts_type.h | 9 ----
2 files changed, 58 insertions(+), 71 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2017-07-18 17:13:54 pgsql: Improve make_tsvector() to handle empty input, and simplify its
Previous Message Tom Lane 2017-07-18 14:17:20 pgsql: Doc: fix thinko in v10 release notes.