42P16ERRORTier 1 — Safe✅ HIGH confidenceinvalid table definition
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
- 1Duplicate column name in the CREATE TABLE column list
- 2PRIMARY KEY specified on a column already declared NOT NULL with a separate UNIQUE constraint (not an error, but see below)
- 3Inheritance cycle: table A inherits from B which inherits from A
- 4Partitioned table missing the PARTITION BY clause
- 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.
CREATE TABLE events (
id SERIAL PRIMARY KEY,
name TEXT,
name TEXT -- duplicate column name
);Fix 1: Remove the duplicate column
When the same column name appears more than once in the column list.
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.
-- 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
🔗 Related errors
📄 Reference pages