pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Avoid quadratic slowdown in regexp match/split functions.
7+ messages / 1 participants
[nested] [flat]

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_5_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/41cfae1f37187ecff15b4eba5b732ba350801228

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



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

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
master

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

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



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

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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(-)



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

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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(-)



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

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_6_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/450b247415125e08821dfbb68b0a15a4f0f7eb22

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



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

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_10_STABLE

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

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



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

* pgsql: Avoid quadratic slowdown in regexp match/split functions.
@ 2018-08-28 11:21  Andrew Gierth <rhodiumtoad@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Andrew Gierth @ 2018-08-28 11:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_4_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/2ba7c4e6c4624fdb6230b060625a494bc93e0b9b

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



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


end of thread, other threads:[~2018-08-28 11:21 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>
2018-08-28 11:21 pgsql: Avoid quadratic slowdown in regexp match/split functions. Andrew Gierth <rhodiumtoad@postgresql.org>

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