Avoid CommandCounterIncrement in RI trigger when INSERT INTO referencing table

From: "houzj(dot)fnst(at)fujitsu(dot)com" <houzj(dot)fnst(at)fujitsu(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Avoid CommandCounterIncrement in RI trigger when INSERT INTO referencing table
Date: 2021-03-03 10:30:55
Message-ID: OS0PR01MB5716ECCB4B70C50D5E61A95F94989@OS0PR01MB5716.jpnprd01.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2021-03-03 10:31:22 Re: Disallow SSL compression?
Previous Message Julien Rouhaud 2021-03-03 10:26:19 Re: Add support for PROVE_FLAGS and PROVE_TESTS in MSVC scripts