From 7ca148b2567d5c7d6d281cef839cc3caa43e502b Mon Sep 17 00:00:00 2001 From: houzj Date: Thu, 4 Mar 2021 14:13:23 +0800 Subject: [PATCH] Avoid CCI in RI trigger when INSERT fk relation Currently, postgres increments command id in ri trigger every time when inserting into a referencing table(fk relation). It can be a block for supporting "parallel insert into a referencing table", because we do not allow increment command id in parallel mode. As far as I can see, itfs 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. * has modifyingcte which modifies the referenced table * has modifying function which modifies the referenced table. Since the above two cases are not supported in parallel mode(parallel unsafe). IMO, It seems itfs not necessary to increment command id in parallel mode, we can just skip commandCounterIncrement when in parallel mode. --- src/backend/utils/adt/ri_triggers.c | 43 ++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index 6e3a410..f257227 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -213,7 +213,7 @@ static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, - bool detectNewRows, int expect_OK); + bool detectNewRows, int expect_OK, bool modifypk); static void ri_ExtractValues(Relation rel, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk, Datum *vals, char *nulls); @@ -229,7 +229,7 @@ static void ri_ReportViolation(const RI_ConstraintInfo *riinfo, * Check foreign key existence (combined for INSERT and UPDATE). */ static Datum -RI_FKey_check(TriggerData *trigdata) +RI_FKey_check(TriggerData *trigdata, bool modifypk) { const RI_ConstraintInfo *riinfo; Relation fk_rel; @@ -393,7 +393,8 @@ RI_FKey_check(TriggerData *trigdata) fk_rel, pk_rel, NULL, newslot, false, - SPI_OK_SELECT); + SPI_OK_SELECT, + modifypk); if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); @@ -412,11 +413,24 @@ RI_FKey_check(TriggerData *trigdata) Datum RI_FKey_check_ins(PG_FUNCTION_ARGS) { + bool modifypk = 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); + /* + * In parallel mode, any other modifications other + * than the insert event itself are parallel unsafe. + * So, there is no chance to modify the pk relation. + * + * Once we support extra modifications in parallel mode, + * this dependency is no longer needed. + */ + if (IsInParallelMode()) + modifypk = false; + /* Share code with UPDATE case. */ - return RI_FKey_check((TriggerData *) fcinfo->context); + return RI_FKey_check((TriggerData *) fcinfo->context, modifypk); } @@ -432,7 +446,7 @@ RI_FKey_check_upd(PG_FUNCTION_ARGS) ri_CheckTrigger(fcinfo, "RI_FKey_check_upd", RI_TRIGTYPE_UPDATE); /* Share code with INSERT case. */ - return RI_FKey_check((TriggerData *) fcinfo->context); + return RI_FKey_check((TriggerData *) fcinfo->context, true); } @@ -520,7 +534,8 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, fk_rel, pk_rel, oldslot, NULL, true, /* treat like update */ - SPI_OK_SELECT); + SPI_OK_SELECT, + true); if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); @@ -712,7 +727,8 @@ ri_restrict(TriggerData *trigdata, bool is_no_action) fk_rel, pk_rel, oldslot, NULL, true, /* must detect new rows */ - SPI_OK_SELECT); + SPI_OK_SELECT, + true); if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); @@ -818,7 +834,8 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS) fk_rel, pk_rel, oldslot, NULL, true, /* must detect new rows */ - SPI_OK_DELETE); + SPI_OK_DELETE, + true); if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); @@ -939,7 +956,8 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS) fk_rel, pk_rel, oldslot, newslot, true, /* must detect new rows */ - SPI_OK_UPDATE); + SPI_OK_UPDATE, + true); if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); @@ -1118,7 +1136,8 @@ ri_set(TriggerData *trigdata, bool is_set_null) fk_rel, pk_rel, oldslot, NULL, true, /* must detect new rows */ - SPI_OK_UPDATE); + SPI_OK_UPDATE, + true); if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); @@ -2179,7 +2198,7 @@ ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, - bool detectNewRows, int expect_OK) + bool detectNewRows, int expect_OK, bool modifypk) { Relation query_rel, source_rel; @@ -2277,7 +2296,7 @@ ri_PerformCheck(const RI_ConstraintInfo *riinfo, spi_result = SPI_execute_snapshot(qplan, vals, nulls, test_snapshot, crosscheck_snapshot, - false, false, limit); + !modifypk, false, limit); /* Restore UID and security context */ SetUserIdAndSecContext(save_userid, save_sec_context); -- 2.7.2.windows.1