Re: Small improvements to substring()

From: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Junwang Zhao <zhjwpku(at)gmail(dot)com>
Subject: Re: Small improvements to substring()
Date: 2026-02-16 21:41:52
Message-ID: CA+hUKG+YoAT-7iE4KkwM93rQhkedW3-cW-YBPCYe_4Q_JkXhnA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Feb 17, 2026 at 10:05 AM Zsolt Parragi
<zsolt(dot)parragi(at)percona(dot)com> wrote:
> 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("");
> -

Thanks Zsolt. Huh. Is it wasting time actually detoasting when it
doesn't need to? I had peeked perhaps too briefly at that and figured
it would skip the work for slicelength == 0 (see comment "/* Can
save a lot of work at this point! */")... Maybe we need that check
after all, but the duplication is annoying. Perhaps we should figure
out how to hoist all the length computations up to the top so they're
not duplicated in the eml == 1 and eml > 1 branches. Previously I'd
written this as a proposal for the bugfix and was deliberately
touching only the eml > 1 branch, but I guess we can aim a bit higher
now and try to improve that aspect of the function too...

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexandre Felipe 2026-02-16 21:43:09 Re: Incremental View Maintenance, take 2
Previous Message Andrew Dunstan 2026-02-16 21:38:02 Re: BackgroundPsql swallowing errors on windows