Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Cache NO ACTION foreign keys separately from RESTRICT foreign keys
authorPeter Eisentraut <peter@eisentraut.org>
Sun, 9 Feb 2025 12:43:56 +0000 (13:43 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Sun, 9 Feb 2025 12:43:56 +0000 (13:43 +0100)
Now that we generate different SQL for temporal NO ACTION vs RESTRICT
foreign keys, we should cache their query plans with different keys.
Since the key also includes the constraint oid, this shouldn't be
necessary, but we have been seeing build farm failures that suggest we
might be sometimes using a cached NO ACTION plan to implement a RESTRICT
constraint.

Author: Paul A. Jungwirth <pj@illuminatedcomputing.com>
Discussion: https://www.postgresql.org/message-id/flat/CA+renyUApHgSZF9-nd-a0+OPGharLQLO=mDHcY4_qQ0+noCUVg@mail.gmail.com

src/backend/utils/adt/ri_triggers.c

index 3d9985b17c2823c328713327511bc5b2a10ffaaa..8473448849cfce6d54117ce6cbf1f19537fe0040 100644 (file)
 /* these queries are executed against the FK (referencing) table: */
 #define RI_PLAN_CASCADE_ONDELETE       3
 #define RI_PLAN_CASCADE_ONUPDATE       4
+#define RI_PLAN_NO_ACTION              5
 /* For RESTRICT, the same plan can be used for both ON DELETE and ON UPDATE triggers. */
-#define RI_PLAN_RESTRICT               5
-#define RI_PLAN_SETNULL_ONDELETE       6
-#define RI_PLAN_SETNULL_ONUPDATE       7
-#define RI_PLAN_SETDEFAULT_ONDELETE        8
-#define RI_PLAN_SETDEFAULT_ONUPDATE        9
+#define RI_PLAN_RESTRICT               6
+#define RI_PLAN_SETNULL_ONDELETE       7
+#define RI_PLAN_SETNULL_ONUPDATE       8
+#define RI_PLAN_SETDEFAULT_ONDELETE        9
+#define RI_PLAN_SETDEFAULT_ONUPDATE        10
 
 #define MAX_QUOTED_NAME_LEN  (NAMEDATALEN*2+3)
 #define MAX_QUOTED_REL_NAME_LEN  (MAX_QUOTED_NAME_LEN*2)
@@ -752,7 +753,7 @@ ri_restrict(TriggerData *trigdata, bool is_no_action)
     * Fetch or prepare a saved plan for the restrict lookup (it's the same
     * query for delete and update cases)
     */
-   ri_BuildQueryKey(&qkey, riinfo, RI_PLAN_RESTRICT);
+   ri_BuildQueryKey(&qkey, riinfo, is_no_action ? RI_PLAN_NO_ACTION : RI_PLAN_RESTRICT);
 
    if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
    {