Re: Duplicate key value violates unique constraint "pg_type_typname_nsp_index" error

From: "Edward J(dot) Sabol" <edwardjsabol(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: Duplicate key value violates unique constraint "pg_type_typname_nsp_index" error
Date: 2026-02-19 13:02:33
Message-ID: ED9777D8-6F59-4B2D-A555-C4574A5B0942@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Feb 18, 2026, at 11:51 PM, Tom Lane wrote:
> Ed Sabol <edwardjsabol(at)gmail(dot)com> writes:
>> Using PostgreSQL 15.16, I have some SQL for creating a materialized view that's inside a larger transaction which randomly emits the following error:
>> ERROR: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
>> DETAIL: Key (typname, typnamespace)=(largetable, 20488) already exists.
>
> Huh. Does it always mention 20488?

Yes, it's always 20488.

>> I've tried the following queries to try to figure out what typnamespace = 20488 refers to:
>
>> SELECT nspname FROM pg_namespace WHERE oid = 20488;
>> SELECT count(*) FROM pg_catalog.pg_type WHERE typname = 'targetable' AND typnamespace = 20488;
>
>> Neither query provides any useful information.
>
> What do you mean by "any useful information", exactly? Do these show
> that there is no pg_namespace row with oid = 20488?

Well, the first query returns a schema that is completely unrelated to anything the SQL is doing. I wondered if the typnamespace = 20488 DETAIL was referring to something else.

> If that is the case, I'd bet on index corruption in the indexes of
> pg_type and/or pg_namespace. REINDEX might help.

I'll try that. But why would it only happen sometimes?

All of the mentions of this error message I could find on the Internet suggest this happens when you have multiple processes trying to execute the same DDL simultaneously, but I'm sure there's only one process executing this transaction.

Thanks,
Ed

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Fabrice Chapuis 2026-03-02 10:27:52 problem during postgres restore: max_connections = 100 is a lower setting than on the primary server, where its value was 350
Previous Message Tom Lane 2026-02-19 04:51:57 Re: Duplicate key value violates unique constraint "pg_type_typname_nsp_index" error