parse_ident rejects a U& quoted identifier in strict mode
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
parse_ident() rejects Unicode-escaped quoted identifiers
Summary
In its default strict mode, parse_ident() rejects a valid PostgreSQL Unicode-escaped quoted identifier (U&"...") even though the SQL parser accepts it and stores the decoded identifier. It raises SQLSTATE 22023; the equivalent ordinary quoted identifier is parsed correctly.
PostgreSQL's lexical structure documentation (https://www.postgresql.org/docs/19/sql-syntax-lexical.html) defines U&"..." as a variant of quoted identifiers and gives U&"d\0061t\+000061" as the spelling of data. The parse_ident() documentation (https://www.postgresql.org/docs/19/functions-string.html) says the function splits a qualified identifier into components and removes their quoting.
Reproduction
Run the script in psql -X against a test database where the parse_ident_u_test schema does not already exist. Leave ON_ERROR_STOP disabled so execution continues after the expected error; the transaction rolls back the schema and table.
BEGIN;
\set VERBOSITY verbose
CREATE SCHEMA parse_ident_u_test;
CREATE TABLE parse_ident_u_test.U&"d\0061t\+000061" (id integer);
SELECT n.nspname, c.relname
FROM pg_class AS c
JOIN pg_namespace AS n ON n.oid = c.relnamespace
WHERE n.nspname = 'parse_ident_u_test';
SAVEPOINT strict_case;
SELECT parse_ident($id$parse_ident_u_test.U&"d\0061t\+000061"$id$);
ROLLBACK TO SAVEPOINT strict_case;
SELECT parse_ident($id$parse_ident_u_test."data"$id$);
ROLLBACK;
The SQL parser creates the table as parse_ident_u_test.data. On PostgreSQL 19.0, strict parse_ident() reports:
ERROR: 22023: string is not a valid identifier: "parse_ident_u_test.U&"d\0061t\+000061""
LOCATION: parse_ident, misc.c:956
After rolling back to the savepoint, the ordinary quoted control returns {parse_ident_u_test,data}. The same strict-mode rejection and control result were reproduced in two independent PostgreSQL 19.0 sessions and in PostgreSQL 18.7.
Expected behavior
Both spellings identify the same two components, so the Unicode-escaped form should return {parse_ident_u_test,data} in the default strict mode.
Source observation
PostgreSQL 19.0's source comment describes parse_ident() as parsing an SQL qualified identifier. Its implementation (https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/utils/adt/misc.c) handles ordinary quoted components beginning with " and unquoted identifier characters, but does not recognize the documented U& prefix before a quoted identifier.
Tested environment
- PostgreSQL 19.0 server and psql client (REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10); PostgreSQL 18.7 also reproduced the behavior.
- Ubuntu 24.04, Linux kernel 7.0.0-38-generic, x86-64, GCC 13.3.0. Build options: --without-readline --without-zlib --without-icu --without-llvm.
- A fresh cluster was initialized with initdb --no-locale -E UTF8 --auth-local=trust --auth-host=reject and listened on a local socket only. Startup settings were listen_addresses='', port=55491, max_connections=20, and shared_buffers=32MB; the socket directory was local to the test environment. No other configuration was changed.