Large regex fails with XX000 instead of a regex error

From: hi(at)joel(dot)id
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Large regex fails with XX000 instead of a regex error
Date: 2026-12-04 10:08:32
Message-ID: 18ee05170bc0203e.42a1ede4aa3e3434.7b9922f2c86fdc59@neubau
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PostgreSQL 18.6 on aarch64 Linux; newdfa() is unchanged on master.

SELECT regexp_like('', string_agg(chr(19968 + i), ''))
FROM generate_series(0, 5999) i;

ERROR: XX000: invalid memory alloc request size 1153536480
LOCATION: MemoryContextSizeFailure, mcxt.c:1180

With 5,701 distinct characters it runs; with 5,800 it fails. The
pattern compiles; the failure is in pg_regexec's newdfa()
(rege_dfa.c), which allocates outsarea and incarea of nss * ncolors
entries (8 and 16 bytes each, nss = 2 * nstates). Its guard keeps those
arrays under INT_MAX entries, but the regex code allocates through
palloc_extended(..., MCXT_ALLOC_NO_OOM) (regcustom.h), which still
raises a plain ERROR for any request over MaxAllocSize: the flag covers
only malloc failing. So between 1 GB and the INT_MAX guard the caller
gets XX000, an internal error, where the code means REG_ETOOBIG or
REG_ESPACE (SQLSTATE 2201B, "regular expression is too complex" or "out
of memory"), which a caller can expect and handle.
Patterns with character classes reach it with far fewer characters,
e.g. '[[:alpha:]][[:digit:]][[:space:]][[:punct:]](' followed by 100
distinct CJK characters and '){255}'. Checking the arrays' byte sizes
against MaxAllocSize in that guard would answer REG_ETOOBIG instead.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2026-12-04 10:15:08 Re: BUG #19953: pg_stat_io exposes impossible rows for io worker backends
Previous Message hi 2026-12-04 09:24:06 EXCEPTION block in a PARALLEL SAFE function fails CREATE TABLE AS