From de1a8e02b051359fe59cd03b9870656b973bc643 Mon Sep 17 00:00:00 2001
From: Greg Nancarrow <gregn4422@gmail.com>
Date: Fri, 12 Mar 2021 17:25:21 +1100
Subject: [PATCH 1/1] Better implement FK trigger parallel-safety checking
 added by commit 05c8482.

Commit 05c8482 added special logic related to parallel-safety of FK triggers.
This is a bit of a hack and should have instead been done by simply setting
appropriate proparallel values on those trigger functions themselves.

Discussion: https://www.postgresql.org/message-id/flat/CA%2BHiwqHJ3sp9hAZF1vM3F55%3D8kuDnMkESrTACN5A4TMw6JznUg%40mail.gmail.com
---
 src/backend/optimizer/util/clauses.c | 13 -------------
 src/include/catalog/catversion.h     |  2 +-
 src/include/catalog/pg_proc.dat      |  8 ++++----
 3 files changed, 5 insertions(+), 18 deletions(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 12754fdb2b..74cc8ddda7 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -976,19 +976,6 @@ target_rel_trigger_max_parallel_hazard(Relation rel,
 
 		if (max_parallel_hazard_test(func_parallel(tgfoid), context))
 			return true;
-
-		/*
-		 * If the trigger type is RI_TRIGGER_FK, this indicates a FK exists in
-		 * the relation, and this would result in creation of new CommandIds
-		 * on insert/update and this isn't supported in a parallel worker (but
-		 * is safe in the parallel leader).
-		 */
-		trigtype = RI_FKey_trigger_type(tgfoid);
-		if (trigtype == RI_TRIGGER_FK)
-		{
-			if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
-				return true;
-		}
 	}
 
 	return false;
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index 33bb6f9672..6c4d9f0689 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -53,6 +53,6 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	202103093
+#define CATALOG_VERSION_NO	202103121
 
 #endif
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 61361a6bc9..93393fcfd4 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -3745,11 +3745,11 @@
 
 # Generic referential integrity constraint triggers
 { oid => '1644', descr => 'referential integrity FOREIGN KEY ... REFERENCES',
-  proname => 'RI_FKey_check_ins', provolatile => 'v', prorettype => 'trigger',
-  proargtypes => '', prosrc => 'RI_FKey_check_ins' },
+  proname => 'RI_FKey_check_ins', provolatile => 'v', proparallel => 'r',
+  prorettype => 'trigger', proargtypes => '', prosrc => 'RI_FKey_check_ins' },
 { oid => '1645', descr => 'referential integrity FOREIGN KEY ... REFERENCES',
-  proname => 'RI_FKey_check_upd', provolatile => 'v', prorettype => 'trigger',
-  proargtypes => '', prosrc => 'RI_FKey_check_upd' },
+  proname => 'RI_FKey_check_upd', provolatile => 'v', proparallel => 'r',
+  prorettype => 'trigger', proargtypes => '', prosrc => 'RI_FKey_check_upd' },
 { oid => '1646', descr => 'referential integrity ON DELETE CASCADE',
   proname => 'RI_FKey_cascade_del', provolatile => 'v', prorettype => 'trigger',
   proargtypes => '', prosrc => 'RI_FKey_cascade_del' },
-- 
2.27.0

