SET_CONSTRAINTS(7)
โจ SET_CONSTRAINTS โจ (that's the SET CONSTRAINTS manual page for PostgreSQL, bestie ๐)
okay so listen. this is SET_CONSTRAINTS from the PostgreSQL 18.6 docs, and it's literally about controlling when your constraints get checked inside the current transaction. it's giving โจtime managementโจ but for databases.
๐ธ SYNOPSIS ๐ธ
SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
๐ DESCRIPTION ๐
so SET CONSTRAINTS sets the behavior of constraint checking within the current transaction. that's it, that's the vibe.
IMMEDIATEconstraints get checked at the end of each statement.DEFERREDconstraints? not checked until transaction commit, bestie. they're patient like that.
every single constraint has its own IMMEDIATE or DEFERRED mode, no cap.
when a constraint is created, it gets one of three characteristics:
DEFERRABLE INITIALLY DEFERREDDEFERRABLE INITIALLY IMMEDIATENOT DEFERRABLE
the third one (NOT DEFERRABLE) is always IMMEDIATE and SET CONSTRAINTS cannot touch it. periodt. the first two start every transaction in the mode they were given, but SET CONSTRAINTS can change their behavior inside a transaction.
okay so SET CONSTRAINTS with a list of constraint names changes the mode of just those constraints โ and they must all be deferrable, no exceptions. each constraint name can be schema-qualified. if you don't specify a schema name, the current schema search path is used to find the first matching name. ๐
SET CONSTRAINTS ALL changes the mode of all deferrable constraints. the whole squad.
the iconic retroactive plot twist ๐: when SET CONSTRAINTS flips a constraint from DEFERRED to IMMEDIATE, the new mode takes effect retroactively. any outstanding data modifications that would have been checked at the end of the transaction get checked right then, during the execution of the SET CONSTRAINTS command instead.
and if any of those constraints is violated, the SET CONSTRAINTS fails โ and it does not change the constraint mode. ๐ญ
so basically SET CONSTRAINTS lets you force constraint checking to happen at a very specific point in a transaction. main character energy for real. ๐
currently, only these constraint types are affected by this setting:
UNIQUEPRIMARY KEYREFERENCES(foreign key)EXCLUDE
meanwhile NOT NULL and CHECK constraints are always checked immediately when a row is inserted or modified (not at the end of the statement, bestie). and uniqueness + exclusion constraints that haven't been declared DEFERRABLE are also checked immediately. they said "no waiting for us." ๐
also!! the firing of triggers declared as "constraint triggers" is controlled by this setting too โ they fire at the same exact time the associated constraint should be checked. synchronized slay. ๐ฆ
๐ NOTES ๐
because PostgreSQL doesn't require constraint names to be unique within a schema (only per-table), it's totally possible to have more than one match for a constraint name you specify. in that case, SET CONSTRAINTS acts on all the matches. ๐
and for a non-schema-qualified name: once a match (or matches) is found in some schema in the search path, schemas appearing later in the path are not searched. no backtracking, bestie.
โ ๏ธ important: this command only alters constraint behavior within the current transaction. if you issue it outside of a transaction block, it emits a warning and otherwise has no effect. so likeโฆ don't do that. ๐
๐ COMPATIBILITY ๐
this command complies with the behavior defined in the SQL standard โ except for two things:
- the limitation that, in PostgreSQL, it does not apply to
NOT NULLandCHECKconstraints. - PostgreSQL checks non-deferrable uniqueness constraints immediately, not at end of statement like the standard would suggest.
๐ซถ a lil sign-off ๐ซถ
that's the whole page, bestie! this one shipped as part of PostgreSQL 18.6 Documentation โ the source is PostgreSQL 18.6, dated 2026. now go forth and defer responsibly ๐โจ๐
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms