Skip to content

42P07 — Duplicate table or relation definition

PostgreSQL SQLSTATE 42P07: Duplicate table or relation definition (duplicate_table), source-backed diagnosis and recovery guidance.

42P07 — Duplicate table or relation definition

At a glance

42P07 (duplicate_table) covers relation-name collisions, conditional skips, and inheritance graph checks. Identify the operation before repairing it.

Field Value
SQLSTATE 42P07
Condition duplicate_table
Status active
Known present by 7.4
Locked snapshots 9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3
Macros ERRCODE_DUPLICATE_TABLE
Aliases

Meaning

Core 18.6 paths report relation "%s" already exists for table, sequence, index, and CREATE TABLE AS. These object kinds share the pg_class relation namespace, so a same-name object of another relation kind can be the collision. The index path with IF NOT EXISTS reports NOTICE relation "%s" already exists, skipping and returns without checking that the existing index has the requested definition. Inheritance reuses the code for repeated parents and for circular inheritance not allowed with DETAIL "%s" is already a child of "%s".

Diagnosis

Capture message, severity, DETAIL, statement, schema, and relation kind. For a name collision, inspect pg_class joined to pg_namespace and include sequences, indexes, tables, views, and materialized views in the search. For an IF NOT EXISTS NOTICE, inspect the existing object’s owner, persistence, columns, indexes, and options before treating the migration as satisfied; the source guard is name-based. For inheritance messages, inspect pg_inherits, resolve the complete parent list, and distinguish a duplicate parent from a cycle.

Response

Use idempotent DDL only when the existing definition is acceptable; otherwise choose a deliberate name or an object-specific migration. A relation-name collision may require adopting the existing object or changing the intended name, while a repeated-parent error requires removing the duplicate parent entry. A cycle requires breaking the parent graph and then verifying pg_inherits; renaming alone cannot repair it. Recover an explicit-transaction ERROR with a savepoint rollback or transaction rollback before catalog checks; a NOTICE path does not require that recovery.

Messages

Representative variants are:

Path Severity Primary DETAIL
relation name collision ERROR relation "%s" already exists none
index IF NOT EXISTS collision NOTICE relation "%s" already exists, skipping none
repeated inheritance parent ERROR relation "%s" would be inherited from more than once none
circular inheritance ERROR circular inheritance not allowed "%s" is already a child of "%s".

Versions

From 7.4 through 18.6 and 19beta3; that catalogue range is not a precise introduction claim. The selected 18.6 paths cover relation-name lookup, index IF NOT EXISTS, repeated inheritance parents, and circularity; runtime was not run.

Sources

  • src.errcodes.42P07.18.6src/backend/utils/errcodes.txt line 383, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba (source).

  • src.call.42P07.694fcfd426e95ce13263fb9asrc/backend/catalog/heap.c lines 1194-1196, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 e720ee58279590793edd0985b3c910970ef56e7361b8ef92e245370587e02a0e (source).

  • src.call.42P07.38d8697cf62c00c05657a6d4src/backend/catalog/index.c lines 898-901, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 98a4156a5c21ae856a3b583b633cdeb578a5b69910386c9f166d69fcb04ba2d0 (source).

  • src.call.42P07.30cb2a5359e1ec4bea60a466src/backend/commands/tablecmds.c lines 875-878, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9 (source).

  • src.call.42P07.19ee2522385544dc7ceb62bbsrc/backend/commands/tablecmds.c lines 17376-17381, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9 (source).

  • src.calls.REL_18_6.42P07 — resolved core call groups; SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf.

  • authored evidence — source claims, message roles, and runtime boundary.