pgsql: Avoid quadratic slowdown in regexp match/split functions. - Mailing list pgsql-committers

From Andrew Gierth
Subject pgsql: Avoid quadratic slowdown in regexp match/split functions.
Date
Msg-id E1fuc3T-0007R7-AE@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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
------
REL9_3_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/861670369093b0bd5ddae336aed6e1e3c4ce8a13

Modified Files
--------------
src/backend/utils/adt/regexp.c | 185 +++++++++++++++++++++++++++++------------
1 file changed, 131 insertions(+), 54 deletions(-)


pgsql-committers by date:

Previous
From: Peter Eisentraut
Date:
Subject: pgsql: pg_verify_checksums: Message style improvements and NLS support
Next
From: Michael Paquier
Date:
Subject: pgsql: Rework option set of oid2name