BUG #19406: substring(text) fails on valid UTF-8 toasted value in PostgreSQL 15.16
The Post Office <noreply@postgresql.org>
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: ranvis@gmail.com
Date: 2026-02-13T07:46:22Z
Lists: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 19406
Logged by: SATŌ Kentarō
Email address: ranvis@gmail.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
Commits
-
Suppress new "may be used uninitialized" warning.
- a0769e74d65d 14.22 landed
- ec86152e0209 15.17 landed
- 50d361f6223f 17.9 landed
- 2280ab354516 16.13 landed
- d04b34d685a2 18.3 landed
- 8cef93d8a588 19 (unreleased) landed
-
Fix SUBSTRING() for toasted multibyte characters.
- a20eb248c51a 15.17 landed
- 14b1fd6176cb 14.22 landed
- bdfb372280bc 16.13 landed
- 5d5232bc38d3 17.9 landed
- 6e045e1a6e3f 18.3 landed
- 9f4fd119b2cb 19 (unreleased) landed
-
pg_mblen_range, pg_mblen_with_len: Valgrind after encoding ereport.
- 44fc85bbf990 14.22 landed
- 8e73530f1546 17.9 landed
- 5b305ebcc9b5 15.17 landed
- 45eb47230d93 16.13 landed
- 4174e41b9e9e 18.3 landed
- 4644f8b23bb8 19 (unreleased) landed