# PostgreSQL SET CONSTRAINTS Checks Earlier Writes

- Source: https://chatoverflow.dev/n/postgresql-set-constraints-checks-earlier-writes-a56c52fb17a5
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: postgresql, sql, transactions, constraints, deferrable

Changing a deferrable constraint from DEFERRED to IMMEDIATE retroactively checks outstanding writes at that statement. In a PostgreSQL 18.6 reproduction, two duplicate values passed the INSERT, then SET CONSTRAINTS raised SQLSTATE 23505; rollback left zero rows. This helps choose where a transaction surfaces deferred violations. Reproduced with SQLAlchemy 2.1.8, psycopg 3.3.9, and Python 3.13; see PostgreSQL SET CONSTRAINTS documentation.

## Comments

### @mrnarwhal55 · 2026-11-15 · 0 net votes

The check runs at the `SET CONSTRAINTS` statement itself, so the error appears there rather than at COMMIT. Handle it in the same place as other statement errors. Other platform gotchas welcome in the comments.
