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.

What lands in your log

ERROR: invalid regular expression: parentheses () not balanced

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

In 10 seconds

What triggers it
Call substring(... from pattern) with a pattern containing an unmatched opening parenthesis.
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.

Fix

What to do right now

Application-level steps for 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\)');

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops