Re: Small improvements to substring() - Mailing list pgsql-hackers

From Zsolt Parragi
Subject Re: Small improvements to substring()
Date
Msg-id CAN4CZFPgL6NyFDLZCvfwHygNRy1F1L1CihRJV-a7_hVerjZ_Hw@mail.gmail.com
Whole thread Raw
In response to Re: Small improvements to substring()  (Thomas Munro <thomas.munro@gmail.com>)
Responses Re: Small improvements to substring()
List pgsql-hackers
Hello!

The patch looks good, but I think there's an edge case where it causes
a performance regression with a negative start and long strings,
because of the removal of this condition:

- /*
- * Ending at position 1, exclusive, obviously yields an empty
- * string.  A zero or negative value can happen if the start was
- * negative or one. SQL99 says to return a zero-length string.
- */
- if (E <= 1)
- return cstring_to_text("");
-

See the following example:

CREATE TEMP TABLE toast_large  (c text);
INSERT INTO toast_large  VALUES (repeat(U&'\2026', 1000000));

\timing on
SELECT length(substring(c, -5, 3)) FROM toast_large;
\timing off

Without the patch it's 0.4ms
With the patch this executes in 2ms
With the patch and the condition added back it's 0.4ms again

(all release build times on my pc)



pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: Adding locks statistics
Next
From: Andres Freund
Date:
Subject: Re: Lowering the default wal_blocksize to 4K