Hi hackers, Currently, postgres increments command id in ri trigger every time when inserting into a referencing table(fk relation). RI_FKey_check-> ri_PerformCheck->SPI_execute_snapshot-> _SPI_execute_plan-> CommandCounterIncrement
It can be a block for supporting "parallel insert into a referencing table", because we do not allow increment command id in parallel mode. So, I was wondering if we can avoid incrementing command id in some cases when executing INSERT. As far as I can see, it’s only necessary to increment command id when the INSERT command modified the referenced table. And INSERT command only have one target table, the modification on other tables can happen in the following cases. 1) has modifyingcte which modifies the referenced table 2) has modifying function which modifies the referenced table. (If I missed something please let me know) Since the above two cases are not supported in parallel mode(parallel unsafe). IMO, It seems it’s not necessary to increment command id in parallel mode, we can just skip commandCounterIncrement when in parallel mode. With this change, we can smoothly support "parallel insert into referencing table" which is desirable. Part of what I plan to change is as the following: ----------- RI_FKey_check_ins(PG_FUNCTION_ARGS) { + bool needCCI = true; + /* Check that this is a valid trigger call on the right time and event. */ ri_CheckTrigger(fcinfo, "RI_FKey_check_ins", RI_TRIGTYPE_INSERT); + /* + * We do not need to increment the command counter + * in parallel mode, because any other modifications + * other than the insert event itself are parallel unsafe. + * So, there is no chance to modify the pk relation. + */ + if (IsInParallelMode()) + needCCI = false; + /* Share code with UPDATE case. */ - return RI_FKey_check((TriggerData *) fcinfo->context); + return RI_FKey_check((TriggerData *) fcinfo->context, needCCI); ... ----------- Thoughts ? Best regards, houzj