Re: stale comment about locking in ExecInitModifyTable()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Amit Langote <amitlangote09(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: stale comment about locking in ExecInitModifyTable()
Date: 2026-12-01 05:17:01
Message-ID: 7B070B8B-6D04-497E-B76B-74CB2F854D36@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Dec 1, 2026, at 10:52, Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
>
> Subject: Remove stale comment about locking in ExecInitModifyTable()
>
> Hi,
>
> While looking at ExecInitModifyTable() for another patch, I noticed
> that a comment there still says that ExecDoInitialPruning() takes care
> of locking the first result relation:
>
> * In either case, it doesn't matter which result relation is kept, so we
> * just keep the first one, if all others have been pruned. See also,
> * ExecDoInitialPruning(), which ensures that this first result relation
> * has been locked.
>
> That sentence came in with my 28317de723, along with the code in
> ExecDoInitialPruning() that it refers to, which locked the first
> result relation of each ModifyTable node. 1722d5eb05 (mine too)
> removed that code but left the sentence behind.
>
> The attached patch removes it. I'm inclined to back-patch this to v18,
> where the sentence first appeared.
>
> --
> Thanks, Amit Langote
> <v1-0001-Remove-stale-comment-about-locking-in-ExecInitMod.patch>

I checked both 28317de723 and 1722d5eb05.

Looks like 1722d5eb05 partially reverted 28317de723. The changes in nodeModifyTable.c were retained. This patch removes a sentence from a comment in nodeModifyTable.c:
```
* See also,
* ExecDoInitialPruning(), which ensures that this first result relation
* has been locked.
```

If I understand correctly, it's related to the following code reverted in ExecDoInitialPruning(), which is defined in execPartition.c:
```
-
- /*
- * Lock the first result relation of each ModifyTable node, even if it was
- * pruned. This is required for ExecInitModifyTable(), which keeps its
- * first result relation if all other result relations have been pruned,
- * because some executor paths (e.g., in nodeModifyTable.c and
- * execPartition.c) rely on there being at least one result relation.
- *
- * There's room for improvement here --- we actually only need to do this
- * if all other result relations of the ModifyTable node were pruned, but
- * we don't have an easy way to tell that here.
- */
- if (stmt->resultRelations && ExecShouldLockRelations(estate))
- {
- foreach(lc, stmt->firstResultRels)
- {
- Index firstResultRel = lfirst_int(lc);
-
- if (!bms_is_member(firstResultRel, estate->es_unpruned_relids))
- {
- RangeTblEntry *rte = exec_rt_fetch(firstResultRel, estate);
-
- Assert(rte->rtekind == RTE_RELATION && rte->rellockmode != NoLock);
- LockRelationOid(rte->relid, rte->rellockmode);
- locked_relids = lappend_int(locked_relids, firstResultRel);
- }
- }
- }
```

So, v1 looks good to me.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-12-01 05:17:52 Re: Should we skip foreign key checks in binary upgrade mode?
Previous Message Kirill Reshke 2026-12-01 05:13:42 Re: Doc: Fix BrinOpcInfo members to match the code