# 2201B — invalid_regular_expression

> Source-backed reference for PostgreSQL SQLSTATE 2201B.
---

## At a glance {#at-a-glance}
SQL regular-expression compilation and execution have separate `2201B` paths, and HBA/ident configuration handling uses the same code with log-level reporting. The fixed messages and severities must be read by owner rather than merged into one generic regex failure.

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| Field | Value |
| --- | --- |
| SQLSTATE | `2201B` |
| Condition | `invalid_regular_expression` |
| Status | `active` |
| Known present by | `7.4` |
| Locked snapshots | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| Macros | `ERRCODE_INVALID_REGULAR_EXPRESSION` |
| Aliases | `—` |

<!-- source facts: data/errcodes/2201B.json -->
<!-- END SQLSTATE FACTS -->

## Meaning {#meaning}
In `regexp.c`, a pattern that cannot compile raises `ERROR` with `invalid regular expression: %s`; a compiled pattern whose execution returns a regex-engine error raises `ERROR` with `regular expression failed: %s`. `REG_NOMATCH` is an ordinary no-match result, not this SQLSTATE. In `hba.c`, configuration regex compilation uses `invalid regular expression "%s": %s` at the caller's level (the normal `load_hba` path passes `LOG`), and ident-map match/backreference failures are also logged with their own primary text. These configuration messages are not SQL expression `ERROR` paths.

## Diagnosis {#diagnosis}
Use the complete primary, severity, and context to identify the owner. For SQL regex functions, inspect the pattern and flags and distinguish compile errors from execution errors. For HBA or ident configuration, inspect the named file and line, the token's leading-slash marker and the regex text after it, and the reload/startup context; a logged configuration failure can leave the previous HBA configuration in place on reload. Do not treat a normal `REG_NOMATCH` as invalid syntax.

## Response {#response}
Repair the pattern or flags in the owning syntax and validate that same subsystem. A SQL regex `ERROR` aborts the current statement, so an explicit transaction must be rolled back or rolled back to an existing savepoint before retrying; autocommit can retry only the corrected statement. A configuration `LOG`/debug report requires fixing the HBA or ident file and reloading or restarting according to that subsystem, not transaction rollback.

## Messages {#messages}
- SQL regex compile, `ERROR`: `invalid regular expression: %s`.
- SQL regex execution error, `ERROR`: `regular expression failed: %s`.
- HBA regex compilation, caller-selected level (the normal `load_hba` path uses `LOG`): `invalid regular expression "%s": %s`; the source also adds configuration-file line context.
- HBA ident-map execution, `LOG`: `regular expression match for "%s" failed: %s`.
- HBA ident backreference without a captured subexpression, `LOG`: `regular expression "%s" has no subexpressions as requested by backreference in "%s"`.

## Versions {#versions}
The locked catalogue records this condition from 7.4; fixed source coverage is PostgreSQL 18.6. The cited SQL regex and HBA/ident paths are distinct owners; extension or other subsystem messages are outside this bounded review.

## Related {#related}
[`2200C`](../2200c/), [`2200D`](../2200d/)

## Sources {#sources}
- [`src/backend/utils/adt/regexp.c#L204-L224`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/regexp.c#L204-L224) contains the SQL regex compile `ERROR`; [`#L285-L305`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/regexp.c#L285-L305) and [`#L2050-L2070`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/regexp.c#L2050-L2070) contain execution-error variants.
- [`src/backend/libpq/hba.c#L298-L340`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/libpq/hba.c#L298-L340) contains HBA regex compilation with caller-selected `elevel`; [`#L2635-L2683`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/libpq/hba.c#L2635-L2683) shows `load_hba` passing `LOG`; [`#L2835-L2864`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/libpq/hba.c#L2835-L2864) and [`#L2880-L2887`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/libpq/hba.c#L2880-L2887) contain the separate ident-map `LOG` messages.

Runtime verification is `not_run`; no regex runtime observation is claimed. The structured [evidence record](../data/evidence/2201b.json) retains owner-specific messages, severities, and source limits.
