Re: FOR PORTION OF does not recompute GENERATED STORED columns that depend on the range column - Mailing list pgsql-hackers

From jian he
Subject Re: FOR PORTION OF does not recompute GENERATED STORED columns that depend on the range column
Date
Msg-id CACJufxEkomKYmWgqXJmQr_qS+z=BZ3w801eh7Z7ekh-3oHXxHQ@mail.gmail.com
Whole thread
In response to Re: FOR PORTION OF does not recompute GENERATED STORED columns that depend on the range column  (Paul A Jungwirth <pj@illuminatedcomputing.com>)
Responses Re: FOR PORTION OF does not recompute GENERATED STORED columns that depend on the range column
List pgsql-hackers
On Mon, Apr 20, 2026 at 4:10 AM Paul A Jungwirth
<pj@illuminatedcomputing.com> wrote:
>
> v5 attached.
>
v5 will cause a segfault.

+ /*
+ * For UPDATE ... FOR PORTION OF, the range column is being modified
+ * (narrowed via intersection), but it is not included in updatedCols
+ * because the user does not need UPDATE permission on it. Now manualy
+ * add it to updatedCols. Since ri_forPortionOf->fp_rangeAttno is already
+ * mapped for the child partition, we have to add it after the mapping just
+ * above. Also that makes it unsafe to mutate perminfo. XXX: Always add the
+ * unmapped attno instead (before mapping), and mutate perminfo, to avoid
+ * repeated allocations?
+ */
+ if (relinfo->ri_forPortionOf)
+ {
+ AttrNumber rangeAttno = relinfo->ri_forPortionOf->fp_rangeAttno;
+
+ if (!bms_is_member(rangeAttno - FirstLowInvalidHeapAttributeNumber,
+   updatedCols))
+ {
+ MemoryContext oldContext;
+
+ oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
+
+ updatedCols =
+ bms_add_member(updatedCols,
+   rangeAttno - FirstLowInvalidHeapAttributeNumber);
+
+ MemoryContextSwitchTo(oldContext);
+ }
+ }
+
+ return updatedCols;

+ updatedCols =
+ bms_add_member(updatedCols,
+   rangeAttno - FirstLowInvalidHeapAttributeNumber);

Here, use "perminfo->updatedCols" not "updatedCols", otherwise segfault.
The attached diff based on v5, fixes this issue.

+ALTER TABLE temporal_partitioned_3 ADD COLUMN range_len int GENERATED
ALWAYS AS (upper(valid_at) - lower(valid_at)) STORED;
Slightly refactoring the tests will allow for easier comparison of
range_len values.



--
jian
https://www.enterprisedb.com/

Attachment

pgsql-hackers by date:

Previous
From: John Naylor
Date:
Subject: Re: Fix translation error in zh_CN.po for "parameter specified more than once"
Next
From: Xuneng Zhou
Date:
Subject: Re: Implement waiting for wal lsn replay: reloaded