Thread

  1. Re: Improve error message for duplicate labels in enum types

    Rahila Syed <rahilasyed90@gmail.com> — 2025-07-04T02:12:58Z

    Hi Yugo,
    
    
    
    > 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
    >
    
     Thank you for sharing the patch.
    +1 to the idea of improving the error message.
    
    Please take the following points mentioned into consideration.
    
    1. I’m considering whether there might be a more efficient way to handle
    this.
    The current method adds an extra loop to check for duplicates, in addition
    to the existing duplicate index check,
    even when no duplicates are present. Would it be possible to achieve this
    by wrapping the following
    insert call in a PG_TRY() and PG_CATCH() block and logging more descriptive
    error in the PG_CATCH() block?
    
    CatalogTuplesMultiInsertWithInfo(pg_enum, slot, slotCount,
    
                 indstate);
    
    2. If we choose to include the check in the 0001 patch you provided, would
    it make more sense to place
    it earlier in the function, before assigning OIDs to the labels and running
    qsort? This way, we could
    catch duplicates sooner and prevent unnecessary processing.
    
    Thank you,
    Rahila Syed