From 9d46fec85b84db27f840e2e0a06eefd0cfcfc2ed Mon Sep 17 00:00:00 2001
From: houzj <houzj.fnst@cn.fujitsu.com>
Date: Thu, 18 Feb 2021 10:35:11 +0800
Subject: [PATCH 3/4] reloption parallel_dml_enabled

Add new table option parallel_dml_enabled.

In addition to the GUC option, the user may want a mechanism for
specifying parallel dml with finer granularity, to specify the
use of parallel dml for specific tables.
The new table option parallel_dml_enabled allows this.

The default is true.
---
 src/backend/access/common/reloptions.c | 25 +++++++++++++++++++------
 src/backend/optimizer/util/clauses.c   | 18 +++++++++++++++++-
 src/bin/psql/tab-complete.c            |  1 +
 src/include/utils/rel.h                | 23 +++++++++++++++++++++++
 4 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c
index c687d3e..938131a 100644
--- a/src/backend/access/common/reloptions.c
+++ b/src/backend/access/common/reloptions.c
@@ -168,6 +168,15 @@ static relopt_bool boolRelOpts[] =
 		},
 		true
 	},
+	{
+		{
+			"parallel_dml_enabled",
+			"Enables \"parallel dml\" feature for this table",
+			RELOPT_KIND_HEAP | RELOPT_KIND_PARTITIONED,
+			ShareUpdateExclusiveLock
+		},
+		true
+	},
 	/* list terminator */
 	{{NULL}}
 };
@@ -1859,7 +1868,9 @@ default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
 		{"vacuum_index_cleanup", RELOPT_TYPE_BOOL,
 		offsetof(StdRdOptions, vacuum_index_cleanup)},
 		{"vacuum_truncate", RELOPT_TYPE_BOOL,
-		offsetof(StdRdOptions, vacuum_truncate)}
+		offsetof(StdRdOptions, vacuum_truncate)},
+		{"parallel_dml_enabled", RELOPT_TYPE_BOOL,
+		offsetof(StdRdOptions, parallel_dml_enabled)}
 	};
 
 	return (bytea *) build_reloptions(reloptions, validate, kind,
@@ -1961,13 +1972,15 @@ build_local_reloptions(local_relopts *relopts, Datum options, bool validate)
 bytea *
 partitioned_table_reloptions(Datum reloptions, bool validate)
 {
-	/*
-	 * There are no options for partitioned tables yet, but this is able to do
-	 * some validation.
-	 */
+	static const relopt_parse_elt tab[] = {
+		{"parallel_dml_enabled", RELOPT_TYPE_BOOL,
+		offsetof(PartitionedOptions, parallel_dml_enabled)}
+	};
+
 	return (bytea *) build_reloptions(reloptions, validate,
 									  RELOPT_KIND_PARTITIONED,
-									  0, NULL, 0);
+									  sizeof(PartitionedOptions),
+									  tab, lengthof(tab));
 }
 
 /*
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index eb6eee5..f134681 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1222,6 +1222,7 @@ target_rel_max_parallel_hazard_recurse(Relation rel,
  *  1) enable_parallel_dml is off
  *  2) INSERT...ON CONFLICT...DO UPDATE
  *  3) INSERT without SELECT
+ *  4) the reloption parallel_dml_enabled is set to off
  *
  * (Note: we don't do in-depth parallel-safety checks here, we do only the
  * cheaper tests that can quickly exclude obvious cases for which
@@ -1232,8 +1233,10 @@ bool
 is_parallel_possible_for_modify(Query *parse)
 {
 	bool			hasSubQuery;
+	bool			parallel_enabled;
 	RangeTblEntry  *rte;
 	ListCell	   *lc;
+	Relation		rel;
 
 	Assert(IsModifySupportedInParallelMode(parse->commandType));
 
@@ -1271,7 +1274,20 @@ is_parallel_possible_for_modify(Query *parse)
 	if (!hasSubQuery)
 		return false;
 
-	return true;
+	/*
+	 * Check if parallel_dml_enabled is enabled for the target table,
+	 * if not, skip the safety checks.
+	 *
+	 * (Note: if the target table is partitioned, the parallel_dml_enabled
+	 * option setting of the partitions are ignored).
+	 */
+	rte = rt_fetch(parse->resultRelation, parse->rtable);
+	rel = table_open(rte->relid, NoLock);
+
+	parallel_enabled = RelationGetParallelDML(rel, true);
+	table_close(rel, NoLock);
+
+	return parallel_enabled;
 }
 
 /*****************************************************************************
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index b64db82..ee7f48c 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1110,6 +1110,7 @@ static const char *const table_storage_parameters[] = {
 	"autovacuum_vacuum_threshold",
 	"fillfactor",
 	"log_autovacuum_min_duration",
+	"parallel_dml_enabled",
 	"parallel_workers",
 	"toast.autovacuum_enabled",
 	"toast.autovacuum_freeze_max_age",
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index 2a41a00..1197ae2 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -307,6 +307,7 @@ typedef struct StdRdOptions
 	int			parallel_workers;	/* max number of parallel workers */
 	bool		vacuum_index_cleanup;	/* enables index vacuuming and cleanup */
 	bool		vacuum_truncate;	/* enables vacuum to truncate a relation */
+	bool		parallel_dml_enabled;	/* enables planner's use of parallel DML */
 } StdRdOptions;
 
 #define HEAP_MIN_FILLFACTOR			10
@@ -425,6 +426,28 @@ typedef struct ViewOptions
 	  VIEW_OPTION_CHECK_OPTION_CASCADED)
 
 /*
+ * PartitionedOptions
+ *		Contents of rd_options for partitioned tables
+ */
+typedef struct PartitionedOptions
+{
+	int32		vl_len_;		/* varlena header (do not touch directly!) */
+	bool		parallel_dml_enabled;	/* enables planner's use of parallel DML */
+} PartitionedOptions;
+
+/*
+ * RelationGetParallelDML
+ *		Returns the relation's parallel_dml_enabled reloption setting.
+ *		Note multiple eval of argument!
+ */
+#define RelationGetParallelDML(relation, defaultpd) 						\
+	((relation)->rd_options ?												\
+	 (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?				\
+	 ((PartitionedOptions *) (relation)->rd_options)->parallel_dml_enabled :\
+	 ((StdRdOptions *) (relation)->rd_options)->parallel_dml_enabled) :		\
+	 (defaultpd))
+
+/*
  * RelationIsValid
  *		True iff relation descriptor is valid.
  */
-- 
2.7.2.windows.1

