SQLSTATE 2201BSeverity lowLab verified

Incident brief

Invalid regular expression

A regular expression pattern passed to a PostgreSQL function was not valid — usually unbalanced parentheses or brackets. PostgreSQL refused to compile it.

Reproduced on PostgreSQL 16.14Verified 2026-07-16 (Docker lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
Invalid regular expression
What triggers it
Call substring(... from pattern) with a pattern containing an unmatched opening parenthesis.
The fix
Balance every ( and [ in the pattern with its matching ) or ].
Proof
Reproduced on PostgreSQL 16.14 → Calling substring with an unbalanced-parenthesis pattern was rejected with SQLSTATE 2201B, naming the exact syntax problem. The session was unaffected afterward.

The fix

What to do right now

The immediate, application-level response to this error.

  • Balance every ( and [ in the pattern with its matching ) or ].
  • If you want to match a literal '(' or ')' character rather than group with it, escape it with a backslash.
  • Test regex patterns against a few real sample strings before shipping them, ideally with a fixed, non-fabricated example (see the premium test).
Fix — SQL
SELECT substring('(foo)' from '\(foo\)');

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-16 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed