Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value)
| От | Chao Li |
|---|---|
| Тема | Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value) |
| Дата | |
| Msg-id | 1FE6C83B-8CAB-4643-9D97-3F8C93C6B12F@gmail.com обсуждение |
| Ответ на | Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value) (Dean Rasheed <dean.a.rasheed@gmail.com>) |
| Список | pgsql-hackers |
> On Apr 13, 2026, at 19:03, Dean Rasheed <dean.a.rasheed@gmail.com> wrote: > > On Mon, 13 Apr 2026, 09:20 Richard Guo, <guofenglinux@gmail.com> wrote: > On Mon, Apr 13, 2026 at 4:21 PM Chao Li <li.evan.chao@gmail.com> wrote: > > I think the issue is that rewriteTargetListIU() removes generated columns from the target list, as described by thiscomment: > > > Later, when the rule action is rewritten, ReplaceVarsFromTargetList() cannot find a target list entry for NEW.gen. ForUPDATE rules, the missing NEW column is handled with REPLACEVARS_CHANGE_VARNO, so it falls back to referencing the originaltarget relation row, which gives the old value. > > I came to the same conclusion. > > > One possible fix is to build a new target list that adds generated columns back when there are rules to fire. I triedthe solution locally with some quick and dirty code and it seems to fix both stored and virtual generated columns forme. > > I think a simpler fix might be to expand generated column references > in the NEW relation to their generation expressions before > ReplaceVarsFromTargetList resolves NEW references, so that the base > column Vars within the expressions can be correctly resolved. > Something like attached. > > - Richard > > One thing about that approach is that it leads to 2 full rewrites of the rule action using ReplaceVarsFromTargetList().I think that could be avoided by using including generated column expressions in the targetlistpassed to ReplaceVarsFromTargetList() by rewriteRuleAction(). I haven't tried it, but I imagine it could reusesome code from expand_generated_columns_internal(). > > Regards, > Dean I think Dean’s comment aligns with my implementation. My implementation just adds generated columns back to target list whenneeded, and pass the new target list to ReplaceVarsFromTargetList. Please see the attached diff for my implementation: * The diff is based on Richard’s v1 patch. * As this diff is only to show my implementation rather than proposing a patch, so the diff is not well polished, and I didn’ttouch the change to expand_generated_columns_internal of v1 patch. * The v1 patch has a typo in generated_virtual.sql where “STORED” should be “VIRTUAL”, I fixed that as it impacts the tests. * I updated the test cases by adding one more column, so that the generated column is built upon two columns. I am really new to this area, so please don’t be surprised if I missed something or made something wrong. I am trying tolearn by resolving problems. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Вложения
В списке pgsql-hackers по дате отправления: