Re: Improve error message for duplicate labels in enum types

From: Yugo Nagata <nagata(at)sraoss(dot)co(dot)jp>
To: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Improve error message for duplicate labels in enum types
Date: 2025-08-26 02:55:34
Message-ID: 20250826115534.be9da8378beed34876eaa8d2@sraoss.co.jp
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Jim,

On Mon, 25 Aug 2025 10:08:23 +0200
Jim Jones <jim(dot)jones(at)uni-muenster(dot)de> wrote:

> Hi Yugo
>
> On 03.07.25 17:04, Yugo Nagata wrote:
> > Currently, when creating an enum type, duplicate labels are caught by a unique
> > index on pg_enum, resulting in a generic error message.
> >
> > postgres=# create type t as enum ('a','b','a');
> > ERROR: duplicate key value violates unique constraint "pg_enum_typid_label_index"
> > DETAIL: Key (enumtypid, enumlabel)=(16418, a) already exists.
> >
> > I propose adding an explicit check for duplicate labels during enum creation,
> > so that a more user-friendly and descriptive error message can be produced,
> > similar to what is already done in ALTER TYPE ... ADD VALUE
> > or ALTER TYPE ... RENAME VALUE .. TO ....
> >
> > With the attached patch applied, the error message becomes:
> >
> > ERROR: label "a" used more than once
> >
>
> The error message for already existing enum labels starts with "enum",
> e.g.  ERROR:  enum label "bar" already exists. So, perhaps this new
> error message should follow the same pattern?

Thank you for taking a look. That makes sense, so I updated the message to:

ERROR: enum label "a" used more than once

> I also wonder if we need to add tests for it, so that we make sure the
> new error is triggered prior to the generic one, e.g. in create_type.sql
>
> -- check for duplicate enum entries
> CREATE TYPE den AS ENUM ('foo','bar','foo');
> CREATE TYPE en AS ENUM ('foo','bar');
> ALTER TYPE en ADD VALUE 'foo';
> ALTER TYPE en RENAME VALUE 'foo' TO 'bar';
> DROP TYPE en;

I also added a test for duplicate enum entries to enum.sql,
since tests for existing entries are already there.

Regards,
Yugo Nagata

--
Yugo Nagata <nagata(at)sraoss(dot)co(dot)jp>

Attachment Content-Type Size
v2-0001-Improve-error-message-for-duplicate-labels-in-enu.patch text/x-diff 2.3 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2025-08-26 03:21:34 Re: Non-reproducible AIO failure
Previous Message Peter Geoghegan 2025-08-26 01:08:47 Re: index prefetching