From 6b09e2082eb8b2ff8ebc53ddfcdf1bd110660214 Mon Sep 17 00:00:00 2001 From: houzj Date: Mon, 1 Feb 2021 11:24:22 +0800 Subject: [PATCH 1/2] reloption parallel_dml_enabled src add new tableoption parallel_dml_enabled In addition to guc option, user may want to use parallel dml for some specific tables.So add new tableoption parallel_dml_enabled, let user decide whether to use parallel DML for specific table. The default is true. --- src/backend/access/common/reloptions.c | 25 +++++++++++-- src/backend/optimizer/plan/planner.c | 3 +- src/backend/optimizer/util/clauses.c | 68 ++++++++++++++++++++++++++++------ src/bin/psql/tab-complete.c | 1 + src/include/optimizer/clauses.h | 1 + src/include/utils/rel.h | 10 +++++ 6 files changed, 91 insertions(+), 17 deletions(-) diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c index c687d3e..3fb3ceb 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,19 @@ build_local_reloptions(local_relopts *relopts, Datum options, bool validate) bytea * partitioned_table_reloptions(Datum reloptions, bool validate) { + static const relopt_parse_elt tab[] = { + {"parallel_dml_enabled", RELOPT_TYPE_BOOL, + offsetof(StdRdOptions, parallel_dml_enabled)} + }; + /* - * There are no options for partitioned tables yet, but this is able to do - * some validation. + * The only option for partitioned tables works for heap too, + * so we temporarily store it in the same struct as heap. */ return (bytea *) build_reloptions(reloptions, validate, RELOPT_KIND_PARTITIONED, - 0, NULL, 0); + sizeof(StdRdOptions), + tab, lengthof(tab)); } /* diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index f6ac972..cac11a9 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -339,8 +339,7 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions, if ((cursorOptions & CURSOR_OPT_PARALLEL_OK) != 0 && IsUnderPostmaster && (parse->commandType == CMD_SELECT || - (enable_parallel_dml && - IsModifySupportedInParallelMode(parse->commandType))) && + is_parallel_possible_for_modify(parse)) && !parse->hasModifyingCTE && max_parallel_workers_per_gather > 0 && !IsParallelWorker()) diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 02a7e05..8a119a2 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1169,23 +1169,32 @@ rel_max_parallel_hazard_for_modify(Relation rel, } /* - * max_parallel_hazard_for_modify + * is_parallel_possible_for_modify * - * Determines the worst parallel-mode hazard level for the specified - * table-modification statement, based on the statement attributes and - * target table. An initial max parallel hazard level may optionally be - * supplied. The search returns the earliest in the following list: - * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE + * Check if current table-modification statement is able to use + * parallel mode. The following case are currently not able to + * use parallel mode : + * + * 1) enable_parallel_dml is off + * 2) UPDATE or DELETE command + * 3) INSERT...ON CONFLICT...DO UPDATE + * 4) INSERT without SELECT part + * 5) the reloption parallel_dml_enabled is not set for the target table + * + * (Note: we do not do parallel-safety check here, we do only the + * advance check to see whether it's necessary to do futher + * safety check) */ -char -max_parallel_hazard_for_modify(Query *parse, char initial_max_parallel_hazard) +bool is_parallel_possible_for_modify(Query *parse) { RangeTblEntry *rte; ListCell *lc; bool hasSubQueryOnRelation; - max_parallel_hazard_context context; Relation rel; + if (!enable_parallel_dml || + !IsModifySupportedInParallelMode(parse->commandType)) + return false; /* * UPDATE is not currently supported in parallel-mode, so prohibit @@ -1197,7 +1206,7 @@ max_parallel_hazard_for_modify(Query *parse, char initial_max_parallel_hazard) * combo-cid and it needs to be propagated to the workers. */ if (parse->onConflict != NULL && parse->onConflict->action == ONCONFLICT_UPDATE) - return PROPARALLEL_UNSAFE; + return false; /* * If there is no underlying query on a relation, a parallel @@ -1226,7 +1235,44 @@ max_parallel_hazard_for_modify(Query *parse, char initial_max_parallel_hazard) } } if (!hasSubQueryOnRelation) - return PROPARALLEL_UNSAFE; + return false; + + rte = rt_fetch(parse->resultRelation, parse->rtable); + rel = table_open(rte->relid, NoLock); + + /* + * Check if parallel_dml_enabled is enabled for the target table, + * if not, skip the safety checks and return PARALLEL_UNSAFE. + * + * (Note: if target table is partitioned, we just check the + * option of parent table and ignore the option values for its child) + */ + if (!RelationGetParallelDML(rel, true)) + { + table_close(rel, NoLock); + return false; + } + + table_close(rel, NoLock); + + return true; +} + +/* + * max_parallel_hazard_for_modify + * + * Determines the worst parallel-mode hazard level for the specified + * table-modification statement, based on the statement attributes and + * target table. An initial max parallel hazard level may optionally be + * supplied. The search returns the earliest in the following list: + * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE + */ +char +max_parallel_hazard_for_modify(Query *parse, char initial_max_parallel_hazard) +{ + RangeTblEntry *rte; + max_parallel_hazard_context context; + Relation rel; /* * Setup the context used in finding the max parallel-mode hazard. diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 17f7265..89b5fe5 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/optimizer/clauses.h b/src/include/optimizer/clauses.h index 3cdddbf..ccbc35a 100644 --- a/src/include/optimizer/clauses.h +++ b/src/include/optimizer/clauses.h @@ -53,5 +53,6 @@ extern void CommuteOpExpr(OpExpr *clause); extern Query *inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte); extern char max_parallel_hazard_for_modify(Query *parse, char initial_max_parallel_hazard); +extern bool is_parallel_possible_for_modify(Query *parse); #endif /* CLAUSES_H */ diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h index 2a41a00..fd1f409 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; /* enable parallel table-modification */ } StdRdOptions; #define HEAP_MIN_FILLFACTOR 10 @@ -362,6 +363,15 @@ typedef struct StdRdOptions ((relation)->rd_options ? \ ((StdRdOptions *) (relation)->rd_options)->parallel_workers : (defaultpw)) +/* + * RelationGetParallelDML + * Returns the relation's parallel_dml_enabled reloption setting. + * Note multiple eval of argument! + */ +#define RelationGetParallelDML(relation, defaultpd) \ + ((relation)->rd_options ? \ + ((StdRdOptions *) (relation)->rd_options)->parallel_dml_enabled : (defaultpd)) + /* ViewOptions->check_option values */ typedef enum ViewOptCheckOption { -- 2.7.2.windows.1