diff --git a/src/gausskernel/optimizer/commands/tablecmds.cpp b/src/gausskernel/optimizer/commands/tablecmds.cpp index d0a615335..a2f90f4f9 100755 --- a/src/gausskernel/optimizer/commands/tablecmds.cpp +++ b/src/gausskernel/optimizer/commands/tablecmds.cpp @@ -33380,8 +33380,11 @@ static int128 EvaluateAutoIncrement(Relation rel, TupleDesc desc, AttrNumber att autoinc = datum2autoinc(cons_autoinc, *value); modify_value = (autoinc == 0); } - /* When datum is NULL/0, auto increase */ - if (autoinc == 0) { + /* + * By default, when datum is NULL/0, auto increase; + * If NO_AUTO_VALUE_ON_ZERO is set, auto increase only when datum is NULL. + */ + if (*is_null || (autoinc == 0 && !CheckPluginNoAutoValueOnZero())) { if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP) { autoinc = tmptable_autoinc_nextval(rel->rd_rel->relfilenode, cons_autoinc->next); } else { diff --git a/src/gausskernel/runtime/executor/execUtils.cpp b/src/gausskernel/runtime/executor/execUtils.cpp index 71b4a51f4..f0df0ca51 100644 --- a/src/gausskernel/runtime/executor/execUtils.cpp +++ b/src/gausskernel/runtime/executor/execUtils.cpp @@ -1669,8 +1669,11 @@ Tuple ExecAutoIncrement(Relation rel, EState* estate, TupleTableSlot* slot, Tupl autoinc = datum2autoinc(cons_autoinc, datum); modify_tuple = (autoinc == 0); } - /* When datum is NULL/0, auto increase */ - if (autoinc == 0) { + + /* By default, when datum is NULL/0, auto increase; + * If NO_AUTO_VALUE_ON_ZERO is set, auto increase only when datum is NULL. + */ + if (is_null || (autoinc == 0 && !CheckPluginNoAutoValueOnZero())) { if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP) { autoinc = tmptable_autoinc_nextval(rel->rd_rel->relfilenode, cons_autoinc->next); } else { diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index bf15e8d6b..7882e1c85 100755 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -746,6 +746,14 @@ extern inline bool CheckPluginReplaceNull() ((replaceNullOrNotFunc)(u_sess->hook_cxt.replaceNullOrNotHook))() : false; } +typedef bool (*noAutoValueOnZeroFunc)(); + +extern inline bool CheckPluginNoAutoValueOnZero() +{ + return u_sess->hook_cxt.noAutoValueOnZeroHook != NULL ? + ((noAutoValueOnZeroFunc)(u_sess->hook_cxt.noAutoValueOnZeroHook))() : false; +} + // AutoMutexLock // Auto object for non-recursive pthread_mutex_t lock // diff --git a/src/include/knl/knl_session.h b/src/include/knl/knl_session.h index 663efea7e..712dc3f31 100644 --- a/src/include/knl/knl_session.h +++ b/src/include/knl/knl_session.h @@ -2959,6 +2959,7 @@ typedef struct knl_u_hook_context { void *pluginPlannerHook; void *groupingplannerHook; void *replaceNullOrNotHook; + void *noAutoValueOnZeroHook; void *nullsMinimalPolicyHook; void *getIgnoreKeywordTokenHook; void *modifyTypeForPartitionKeyHook;