Re: BUG #19406: substring(text) fails on valid UTF-8 toasted value in PostgreSQL 15.16

From: Noah Misch <noah(at)leadboat(dot)com>
To: ranvis(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: thomas(dot)munro(at)gmail(dot)com
Subject: Re: BUG #19406: substring(text) fails on valid UTF-8 toasted value in PostgreSQL 15.16
Date: 2026-02-14 00:21:13
Message-ID: 20260214002113.1f.noahmisch@microsoft.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Feb 13, 2026 at 02:48:04PM -0800, Noah Misch wrote:
> On Fri, Feb 13, 2026 at 09:27:02AM -0800, Noah Misch wrote:
> > On Fri, Feb 13, 2026 at 07:46:22AM +0000, PG Bug reporting form wrote:
> > > After upgrading from PostgreSQL 15.15 to 15.16, substring(text) raises:
> > > >ERROR: invalid byte sequence for encoding "UTF8": 0xe6 0x97
> > > on valid UTF-8 text stored in a TOAST-compressed column.
> >
> > > user=> select substring(data from 1 for 1) from toast_repro;
> > > ERROR: 22021: invalid byte sequence for encoding "UTF8": 0xe6 0x97
> >
> > Thanks for the report. That is a bug and a regression; I regret missing it
> > during review. The substring operation works by taking a 4-byte slice from
> > the toasted value (4 bytes being the max length of a UTF8 char in PostgreSQL),
> > the finding the actual first character within those bytes. However, it
> > incorrectly requires those four bytes to be a valid UTF8 string. I'll start
> > on a fix.
>
> Attached. I may add some more tests, e.g. a toasted invalid string where the
> detoasted length is less than the slice we request.

Tests already covered that in particular, but I added some other tests. I
think this is now ready. Review welcome. I have a Valgrind test run ongoing.

Attachment Content-Type Size
toast-slice-mblen-v2.patch text/plain 8.6 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-02-14 01:11:01 Re: BUG #19407: pg_dump : DROP RULE creates forward references
Previous Message Rafia Sabih 2026-02-13 23:37:15 Re: Two issues with REFRESH MATERIALIZED VIEW CONCURRENTLY