On Fri, 2023-12-22 at 10:59 +0300, Pavel Luzanov wrote:
> Please, consider small suggestion to replace last sentence.
> 
> - This is not considered a bug, and it is the responsibility of the user 
> to write triggers so that such problems are avoided.
> + It is the trigger programmer's responsibility to avoid such scenarios.
> 
> To be consistent with the sentence about recursive trigger calls: [1]
> "It is the trigger programmer's responsibility to avoid infinite 
> recursion in such scenarios."

Yes, that is better - shorter and avoids passive mode.  Changed.

> Also I don't really like "This is not considered a bug" part, since it 
> looks like an excuse.

In a way, it is an excuse, so why not be honest about it.

The example you provided in your other message (cascading triggers
fail if the table ovner has revoked the required permissions from
herself) is not really about breaking foreign keys.  You hit a
surprising error, but referential integrity will be maintained.

Patch v3 is attached.

Yours,
Laurenz Albe
From f47c149edd529dc7f1f39977b3d01ee501e19fab Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.a...@cybertec.at>
Date: Fri, 22 Dec 2023 12:33:40 +0100
Subject: [PATCH v3] Document foreign key internals

Warn the user that foreign keys are implemented as triggers, and
that user-defined triggers can interact with them and break
referential integrity.

Author: Laurenz Albe
Reviewed-by: David G. Johnston, Pavel Luzanov
Discussion: https://postgr.es/m/b81fe38fcc25a81be6e2e5b3fc1ff624130762fa.camel%40cybertec.at
---
 doc/src/sgml/ddl.sgml | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml
index d2951cd754..03c5619e9e 100644
--- a/doc/src/sgml/ddl.sgml
+++ b/doc/src/sgml/ddl.sgml
@@ -1224,6 +1224,18 @@ CREATE TABLE posts (
     syntax in the reference documentation for
     <xref linkend="sql-createtable"/>.
    </para>
+
+   <note>
+    <para>
+     Foreign key constraints are implemented as system triggers in
+     <productname>PostgreSQL</productname>.  As such, they are subject to the
+     trigger firing rules described in <xref linkend="trigger-definition"/>.
+     In particular, user-defined triggers on the referencing table can cancel
+     or modify the effects of cascading deletes or updates, thereby breaking
+     referential integrity.  This is not considered a bug, and it is the
+     trigger programmer's responsibility of avoid such problems.
+    </para>
+   </note>
   </sect2>
 
   <sect2 id="ddl-constraints-exclusion">
-- 
2.43.0

Reply via email to