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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: ranvis(at)gmail(dot)com
Subject: BUG #19406: substring(text) fails on valid UTF-8 toasted value in PostgreSQL 15.16
Date: 2026-02-13 07:46:22
Message-ID: 19406-9867fddddd724fca@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19406
Logged by: SATŌ Kentarō
Email address: ranvis(at)gmail(dot)com
PostgreSQL version: 15.16
Operating system: Linux 5.14.0-611.27.1.el9_7.x86_64
Description:

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.

The same SQL works on PostgreSQL 15.15.
The data is valid UTF-8. The problem occurs only when operating on a
TOAST-compressed value.

Steps to Reproduce:
$ psql
psql (15.16)
Type "help" for help.

user=> \x
Expanded display is on.
user=> \set VERBOSITY verbose
user=> create table toast_repro (data text);
CREATE TABLE
user=> insert into toast_repro select repeat('日向', 2000); -- e6 97 a5 e5 90
91
INSERT 0 1
user=> select pg_column_size(data), octet_length(data) from toast_repro;
-[ RECORD 1 ]--+------
pg_column_size | 153
octet_length | 12000
user=> select substring(data from 1 for 2) from toast_repro;
-[ RECORD 1 ]---
substring | 日向
user=> select substring(data from 1 for 1) from toast_repro;
ERROR: 22021: invalid byte sequence for encoding "UTF8": 0xe6 0x97
LOCATION: report_invalid_encoding_int, mbutils.c:1796
user=> select substring('日向' from 1 for 1);
-[ RECORD 1 ]-
substring | 日
user=> select substring(data::varchar(100) from 1 for 1) from toast_repro;
-[ RECORD 1 ]-
substring | 日
user=> select substring(data from 1 for 1) from ( select data || '' as data
from toast_repro) t;
-[ RECORD 1 ]-
substring | 日

Environment:
version | PostgreSQL 15.16 on x86_64-pc-linux-gnu, compiled by gcc (GCC)
11.5.0 20240719 (Red Hat 11.5.0-11), 64-bit
server_encoding | UTF8
client_encoding | UTF8
lc_collate | ja_JP.UTF-8
lc_ctype | ja_JP.UTF-8

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-02-13 09:02:37 BUG #19407: pg_dump : DROP RULE creates forward references
Previous Message PG Bug reporting form 2026-02-13 05:00:01 BUG #19405: Assertion in eval_windowaggregates() fails due to integer overflow