PG
PRO
42P16ERRORTier 1 — Safe✅ HIGH confidence

invalid table definition

Category: Syntax Error or Access Rule ViolationVersions: All Postgres versions

What this means

A CREATE TABLE or ALTER TABLE statement produced a table definition that violates Postgres structural rules. This includes circular inheritance, duplicate column names, invalid partitioning specifications, or a PRIMARY KEY on a column that permits NULLs.

Why it happens

  1. 1Duplicate column name in the CREATE TABLE column list
  2. 2PRIMARY KEY specified on a column already declared NOT NULL with a separate UNIQUE constraint (not an error, but see below)
  3. 3Inheritance cycle: table A inherits from B which inherits from A
  4. 4Partitioned table missing the PARTITION BY clause
  5. 5Conflicting column type in a partitioned table that does not match the partition key type

How to reproduce

A CREATE TABLE has a duplicate column name.

trigger — this will ERROR
CREATE TABLE events (
  id   SERIAL PRIMARY KEY,
  name TEXT,
  name TEXT -- duplicate column name
);
ERROR: column "name" specified more than once

Fix 1: Remove the duplicate column

When the same column name appears more than once in the column list.

fix
CREATE TABLE events (
  id   SERIAL PRIMARY KEY,
  name TEXT NOT NULL
);

Why this works

Postgres validates the column list in DefineRelation() before any catalog entries are written. Finding a duplicate column name raises the error immediately. Removing the duplicate allows the table to be created.

Fix 2: Fix the partitioning specification

When the error relates to an invalid PARTITION BY clause.

fix
-- Correct partitioned table definition:
CREATE TABLE measurements (
  id         BIGSERIAL,
  recorded   DATE NOT NULL,
  value      NUMERIC
) PARTITION BY RANGE (recorded);

CREATE TABLE measurements_2024
  PARTITION OF measurements
  FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

Why this works

Partitioned table validation checks that the partition key columns exist, have compatible types, and that partition bounds do not overlap. Each partition must be a separate CREATE TABLE PARTITION OF statement after the parent is created.

What not to do

Use ALTER TABLE to add duplicate columns after creation

Why it's wrong: ALTER TABLE ADD COLUMN also raises an error for duplicate names; the fix is always to correct the schema definition.

Version notes

Postgres 10+Declarative table partitioning introduced. The 42P16 error covers partitioning definition errors in addition to classic structural errors.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

📚 Feature docs: https://www.postgresql.org/docs/current/sql-createtable.html

🔧 Source ref: src/backend/commands/tablecmds.c — DefineRelation()

📖 Further reading: CREATE TABLE

📖 Further reading: Table Partitioning

Confidence assessment

✅ HIGH confidence

Stable and well-documented for standard cases. Partitioning error messages were improved significantly in Postgres 10-13. Edge case: inheritance-related 42P16 errors are rare and the messages are less clear; check the DETAIL line in the server log.

See also

📄 Reference pages

CREATE TABLETable PartitioningTable Inheritance
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →