pgsql: Improve error message for duplicate labels when creating an enum

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Improve error message for duplicate labels when creating an enum
Date: 2025-09-02 17:51:11
Message-ID: E1utVA0-000Bo3-0K@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Improve error message for duplicate labels when creating an enum type.

Previously, duplicate labels in CREATE TYPE AS ENUM were caught by
the unique index on pg_enum, resulting in a generic error message.
While this was evidently intentional, it's not terribly user-friendly,
nor consistent with the ALTER TYPE cases which take more care with
such errors. This patch adds an explicit check to produce a more
user-friendly and descriptive error message.

A potential objection to this implementation is that it adds O(N^2)
work to the creation operation. However, quick testing finds that
that's pretty negligible below 1000 enum labels, and tolerable even
at 10000. So it doesn't really seem worth being smarter.

Author: Yugo Nagata <nagata(at)sraoss(dot)co(dot)jp>
Reviewed-by: Rahila Syed <rahilasyed90(at)gmail(dot)com>
Reviewed-by: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/20250704000402.37e605ab0c59c300965a17ee@sraoss.co.jp

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/1b1960c8c9e81171efabdd9e3393b4eec1ffc477

Modified Files
--------------
src/backend/catalog/pg_enum.c | 25 +++++++++++++++++++------
src/test/regress/expected/enum.out | 3 +++
src/test/regress/sql/enum.sql | 3 +++
3 files changed, 25 insertions(+), 6 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message noreply 2025-09-02 21:03:32 pgsql: Tag refs/tags/REL_18_RC1 was created
Previous Message Jacob Champion 2025-09-02 15:25:45 Re: pgsql: Fix .gitignore for src/interfaces/libpq-oauth.