Incident Response

PostgreSQL ERROR: null value in column violates not-null constraint

A row reached the table with NULL in a NOT NULL column. Here is how to read the DETAIL line, the usual reasons defaults do not apply, and how to add NOT NULL to a large live table without locking it for an hour.

· 13 min read · AG Data Team

ERROR: null value in column "..." of relation "..." violates not-null constraint means an INSERT, UPDATE or COPY tried to store NULL in a column declared NOT NULL. PostgreSQL rejects the row and the whole statement. The error is SQLSTATE 23502, not_null_violation. The constraint did its job. The real question is where the NULL came from.

INSERT INTO orders (customer_id, total) VALUES (NULL, 49.00);

ERROR:  null value in column "customer_id" of relation "orders" violates not-null constraint
DETAIL:  Failing row contains (1042, null, 49.00, 2026-11-24 15:12:03.21+00, new).

Read the message and the DETAIL line

The message names the column and, since PostgreSQL 13, the table as well. Older versions print only the column, which is confusing when a trigger or a rule writes into another table. The DETAIL: Failing row contains (...) line shows the whole row in column order, which helps you find which code path built it. A few details from the executor source:

Then check how the table is defined in the database the application actually uses:

SELECT column_name, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'orders'
ORDER BY ordinal_position;

Why the default did not save you

The most common surprise is that a column has a DEFAULT and the insert still fails. The INSERT documentation says that a column not present in the column list is filled with its default value, or with NULL if there is none. An explicit NULL is not "missing". It is a value, and PostgreSQL stores it, or rejects it:

-- the default is used: the column is not mentioned
INSERT INTO orders (customer_id, total) VALUES (7, 49.00);

-- the default is used: the DEFAULT keyword
INSERT INTO orders (customer_id, total, status) VALUES (7, 49.00, DEFAULT);

-- the default is NOT used: an explicit NULL, and the insert fails
INSERT INTO orders (customer_id, total, status) VALUES (7, 49.00, NULL);

ORMs and generic data layers often send every column of a model, with NULL for fields the code never set. In our experience this is the number one cause of this error right after a column gets NOT NULL DEFAULT ...: the database is ready, but the application still sends NULL explicitly. Either stop sending the column, send DEFAULT, or set the value in the application.

Other usual causes

  1. A subquery or join that finds nothing. A scalar subquery that returns no rows gives NULL, not an error (value expressions). INSERT ... SELECT with a LEFT JOIN does the same for rows that have no match.
  2. UPDATE with a lookup. UPDATE orders SET customer_id = (SELECT id FROM customers WHERE email = ...) writes NULL wherever the lookup misses.
  3. COPY and CSV files. In CSV format, the default NULL string is an unquoted empty value (COPY documentation). An empty field in a NOT NULL text column fails. If empty should mean an empty string, use FORCE_NOT_NULL for those columns.
  4. A trigger. A BEFORE trigger that sets NEW.col to the result of a failed lookup produces the same error, even though the statement itself looked fine.
  5. A migration on a table that already has rows. Adding a column with NOT NULL and no default, or running SET NOT NULL while old rows are NULL, fails with the related message column "..." of relation "..." contains null values.
  6. Code and schema out of sync. A new column was added in one environment and the code was deployed to another. The same pattern causes the error in our article on column does not exist.

Do not "fix" it with DROP NOT NULL. Removing the constraint makes the error go away and moves the problem into your data: reports, joins and application code that assume the value exists. Find the code path that writes NULL and fix that, or give the column a real default.

Adding NOT NULL to a large table safely

On a small table, ALTER TABLE ... ALTER COLUMN ... SET NOT NULL finishes in milliseconds. On a table with hundreds of millions of rows, it is a different operation. The ALTER TABLE documentation explains that SET NOT NULL checks the whole table with a full scan, and like most forms of ALTER TABLE it holds an ACCESS EXCLUSIVE lock. Nothing can read or write the table during that scan. The same page describes the way around it: if a valid CHECK constraint already proves that no NULL can exist, the scan is skipped. That optimisation exists since PostgreSQL 12. VALIDATE CONSTRAINT needs only a SHARE UPDATE EXCLUSIVE lock, so normal reads and writes continue while it scans.

-- PostgreSQL 12 to 17: add NOT NULL to a big table without a long lock
SET lock_timeout = '3s';

-- 1. a CHECK constraint that only applies to new rows (short lock, no scan)
ALTER TABLE orders
    ADD CONSTRAINT orders_customer_id_nn CHECK (customer_id IS NOT NULL) NOT VALID;

-- 2. fix the old rows in small batches
UPDATE orders SET customer_id = 0
WHERE id IN (SELECT id FROM orders WHERE customer_id IS NULL LIMIT 10000);
-- ...repeat until no rows are left...

-- 3. scan the table under SHARE UPDATE EXCLUSIVE: reads and writes keep working
ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_id_nn;

-- 4. SET NOT NULL can now skip the full scan, then drop the helper constraint
ALTER TABLE orders ALTER COLUMN customer_id SET NOT NULL;
ALTER TABLE orders DROP CONSTRAINT orders_customer_id_nn;

PostgreSQL 18 stores NOT NULL constraints in pg_constraint and lets you add them as NOT VALID, so the helper CHECK constraint is no longer needed:

-- PostgreSQL 18: a NOT NULL constraint can itself be NOT VALID
ALTER TABLE orders
    ADD CONSTRAINT orders_customer_id_nn NOT NULL customer_id NOT VALID;
-- ...backfill old rows...
ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_id_nn;

For new columns, the cheap path is different. Since PostgreSQL 11, ADD COLUMN ... NOT NULL DEFAULT with a constant default does not rewrite the table. The value is stored in the catalog and returned for existing rows. A volatile default such as clock_timestamp() still rewrites the whole table.

What changes on large databases

Key takeaways

PostgreSQL Errors Migrations Constraints
Work with us

Schema change blocked by NULLs?

We plan and run PostgreSQL schema changes on large live tables: backfills, constraints and column changes with short locks and no surprise downtime.

Get in touch More from the blog