Skip to content

Commit 36d442a

Browse files
committed
Clarify that cross-row constraints are unsupported
Maybe we'll implement them later, or maybe not, but let's make the statu quo clear for now. Author: Lætitia Avrot, Patrick Francelle Reviewers: too many to list Discussion: https://postgr.es/m/CAB_COdhUuzNFOJfc7SNNso5rOuVA3ui93KMVunEM8Yih+K5A6A@mail.gmail.com
1 parent 664f01b commit 36d442a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,33 @@ CREATE TABLE products (
403403
ensure that a column does not contain null values, the not-null
404404
constraint described in the next section can be used.
405405
</para>
406+
407+
<note>
408+
<para>
409+
<productname>PostgreSQL</productname> does not support
410+
<literal>CHECK</literal> constraints that reference table data other than
411+
the new or updated row being checked. While a <literal>CHECK</literal>
412+
constraint that violates this rule may appear to work in simple
413+
tests, it cannot guarantee that the database will not reach a state
414+
in which the constraint condition is false (due to subsequent changes
415+
of the other row(s) involved). This would cause a database dump and
416+
reload to fail. The reload could fail even when the complete
417+
database state is consistent with the constraint, due to rows not
418+
being loaded in an order that will satisfy the constraint. If
419+
possible, use <literal>UNIQUE</literal>, <literal>EXCLUDE</literal>,
420+
or <literal>FOREIGN KEY</literal> constraints to express
421+
cross-row and cross-table restrictions.
422+
</para>
423+
424+
<para>
425+
If what you desire is a one-time check against other rows at row
426+
insertion, rather than a continuously-maintained consistency
427+
guarantee, a custom <link linkend="triggers">trigger</link> can be used
428+
to implement that. (This approach avoids the dump/reload problem because
429+
<application>pg_dump</application> does not reinstall triggers until after
430+
reloading data, so that the check will not be enforced during a dump/reload.)
431+
</para>
432+
</note>
406433
</sect2>
407434

408435
<sect2>

doc/src/sgml/ref/create_table.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
754754
<para>
755755
Currently, <literal>CHECK</literal> expressions cannot contain
756756
subqueries nor refer to variables other than columns of the
757-
current row. The system column <literal>tableoid</literal>
757+
current row (see <xref linkend="ddl-constraints-check-constraints"/>).
758+
The system column <literal>tableoid</literal>
758759
may be referenced, but not any other system column.
759760
</para>
760761

0 commit comments

Comments
 (0)