PostgreSQL raises ERROR: zero-length delimited identifier or a context-specific variant with SQLSTATE 2200F (zero_length_character_string) when an operation requires a non-empty string argument but receives a zero-length string (''). This belongs to error class 22 — data exceptions — and is typically encountered with specific SQL functions or operators that have an explicit restriction against empty string arguments.
What This Error Means
SQLSTATE class 22 covers data exceptions: situations where a value is structurally valid SQL but violates a semantic constraint imposed by the operation or the SQL standard. Code 2200F specifically targets the case where an operation mandates that a character string argument have at least one character, and the caller provides ''.
PostgreSQL raises this error synchronously at statement execution time. The statement is aborted and, if you are inside an explicit transaction block, the transaction is placed in an error state (idle in transaction (aborted)). You must issue ROLLBACK (or ROLLBACK TO SAVEPOINT) before executing any further statements in that transaction.
This is a relatively uncommon error in everyday application code. It surfaces most often in administrative or schema-manipulation contexts — for example, constructing identifier names dynamically, calling certain string-processing functions with a computed empty result, or using SIMILAR TO or LIKE patterns in contexts that disallow empty pattern strings.
Common Causes
Passing an empty string as a delimited identifier. When PostgreSQL parses a double-quoted identifier such as
"", it produces a zero-length identifier, which is forbidden by the SQL standard. Dynamically constructing DDL or queries usingformat()or string concatenation can accidentally produce"".Calling
pg_catalogor extension functions that require a non-empty string argument. Some built-in functions (e.g., certainto_tsvectorconfigurations, FDW option names, or procedural language handlers) explicitly reject empty strings and raise 2200F rather than returning a null or empty result.Using
SIMILAR TOwith an empty pattern in strict contexts. Although a bare''pattern inSIMILAR TOusually matches only the empty string without error in modern PostgreSQL, certain older versions or contrib extensions enforce the non-empty requirement and surface 2200F.Empty string passed to
XMLPARSEor XML-related functions. XML processing functions require a non-trivially empty content string; passing''triggers this error in some call paths.Dynamic SQL via
EXECUTEthat constructs an identifier or argument from a variable that is unexpectedly empty. A PL/pgSQL procedure that doesEXECUTE format('SELECT %I', v_name)will raise 2200F ifv_nameis''.
How to Fix zero_length_character_string
Validate string arguments before use. Add a guard clause in application code or PL/pgSQL to confirm that string arguments intended as identifiers or function inputs are non-empty before passing them to PostgreSQL.
DO $$ DECLARE v_schema TEXT := ''; -- would cause 2200F BEGIN IF v_schema = '' THEN RAISE EXCEPTION 'Schema name must not be empty'; END IF; EXECUTE format('CREATE TABLE %I.my_table (id INT)', v_schema); END; $$;Use
NULLIFto convert empty strings to NULL and handle NULL separately. If the empty string originates from user input or an outer query, convert it to NULL so that the calling logic can apply a default or skip the operation.-- Replace '' with NULL, then COALESCE to a safe default SELECT COALESCE(NULLIF(v_name, ''), 'default_schema') INTO v_schema;Audit dynamic DDL generation. Review any use of
format('%I', ...)or manual string concatenation building identifiers. Addassertstatements or precondition checks in PL/pgSQL:ASSERT v_table_name <> '', 'Table name must be non-empty';Check function documentation for empty-string restrictions. When calling less common PostgreSQL functions that raise 2200F, consult the documentation for the specific function to understand whether it requires a non-empty argument and what the correct substitute value is (e.g., a single space, a NULL, or a sentinel value).
Wrap the call in an exception handler when you expect the input may sometimes be empty and want to continue execution:
BEGIN PERFORM some_function(v_input); EXCEPTION WHEN zero_length_character_string THEN -- log or substitute a default RAISE NOTICE 'Skipping empty input for some_function'; END;
Additional Information
- SQLSTATE 2200F is part of class 22 (Data Exception). Related sibling codes include
22001(string data right truncation),22003(numeric value out of range),22007(invalid datetime format), and22P02(invalid text representation). - The condition name
zero_length_character_stringcan be used directly in PL/pgSQLEXCEPTION WHENclauses — no need to use the numeric code. - This error code has been present in PostgreSQL since at least PostgreSQL 8.x; its behavior has not changed significantly across versions.
- Most ORM layers (SQLAlchemy, ActiveRecord, Hibernate) will surface this as a generic
DatabaseErrororOperationalErrorwith the SQLSTATE code in the exception detail. Inspect thepgcodeattribute (Python) or equivalent to distinguish it from other class-22 errors. - Because this error immediately aborts the current statement, any
RETURNINGclause on the failed statement produces no rows.
Frequently Asked Questions
Why does PostgreSQL reject an empty string here when it accepts empty strings in most other places?
PostgreSQL allows empty strings in regular VARCHAR, TEXT, and CHAR columns without issue. SQLSTATE 2200F applies only to specific operations — primarily identifier construction and certain built-in functions — where the SQL standard or the function's contract explicitly prohibits a zero-length value. It is not a general restriction on empty strings in the database.
My transaction is now stuck in an aborted state after this error. What do I do?
Issue a ROLLBACK (or ROLLBACK TO SAVEPOINT <name> if you set a savepoint before the failing statement). You cannot execute any further SQL in the same transaction until you roll back. After rolling back you may retry the operation with corrected input.
Can I use WHEN zero_length_character_string THEN in a PL/pgSQL exception handler?
Yes. PostgreSQL maps the condition name zero_length_character_string directly to SQLSTATE 2200F, so EXCEPTION WHEN zero_length_character_string THEN ... works correctly without needing to reference the numeric code.
Is there a way to allow empty string identifiers in PostgreSQL? No. Zero-length delimited identifiers are forbidden by the SQL standard and there is no configuration parameter to permit them. The correct fix is always to ensure the identifier or argument is non-empty before it reaches PostgreSQL.