pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <rhodiumtoad@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid quadratic slowdown in regexp match/split functions.
Date: Tue, 28 Aug 2018 11:21:03 +0000
Message-ID: <E1fuc3T-0007Qy-8w@gemulon.postgresql.org> (raw)

Avoid quadratic slowdown in regexp match/split functions.

regexp_matches, regexp_split_to_table and regexp_split_to_array all
work by compiling a list of match positions as character offsets (NOT
byte positions) in the source string.

Formerly, they then used text_substr to extract the matched text; but
in a multi-byte encoding, that counts the characters in the string,
and the characters needed to reach the starting byte position, on
every call. Accordingly, the performance degraded as the product of
the input string length and the number of match positions, such that
splitting a string of a few hundred kbytes could take many minutes.

Repair by keeping the wide-character copy of the input string
available (only in the case where encoding_max_length is not 1) after
performing the match operation, and extracting substrings from that
instead. This reduces the complexity to being linear in the number of
result bytes, discounting the actual regexp match itself (which is not
affected by this patch).

In passing, remove cleanup using retail pfree() which was obsoleted by
commit ff428cded (Feb 2008) which made cleanup of SRF multi-call
contexts automatic. Also increase (to ~134 million) the maximum number
of matches and provide an error message when it is reached.

Backpatch all the way because this has been wrong forever.

Analysis and patch by me; review by Kaiting Chen.

Discussion: https://postgr.es/m/87pnyn55qh.fsf@news-spur.riddles.org.uk

see also https://postgr.es/m/87lg996g4r.fsf@news-spur.riddles.org.uk

Branch
------
REL_11_STABLE

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

Modified Files
--------------
src/backend/utils/adt/regexp.c | 182 +++++++++++++++++++++++++++++------------
1 file changed, 129 insertions(+), 53 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1fuc3T-0007Qy-8w@gemulon.postgresql.org>
Permalink:  ../E1fuc3T-0007Qy-8w@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1fuc3T-0007Qy-8w@gemulon.postgresql.org

 · 

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-committers@postgresql.org
  Cc: rhodiumtoad@postgresql.org
  Subject: Re: pgsql: Avoid quadratic slowdown in regexp match/split functions.
  In-Reply-To: <E1fuc3T-0007Qy-8w@gemulon.postgresql.org>
  References: <E1fuc3T-0007Qy-8w@gemulon.postgresql.org>

* 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