On Thursday, July 9, 2026 10:47 AM Michael Paquier <michael@paquier.xyz> wrote:
>
> An updated patch is attached, with the "flags" business added, and something
> for the other issue reported by Imran for the case where
> reform_and_rewrite_tuple() was not handled correctly. If we have a sort,
> that means going back to a more aggressive detoasting to check if some
> chunks are missing if a tuple has been found as recently dead.
> I was wondering about doing a split into two patches for
> build_range_scan() and copy_for_cluster(), but refrained from that due to the
> detoast_external_attr_extended() required in both cases.
>
> Anyway, thoughts and reviews are welcome.
Thank you for updating the patch. I've reviewed it and have a few comments
below.
1.
I think there could be a memory leak in the early return path of
heap_toast_insert_or_update() when failing to retrieve a toast value.
In toast_tuple_init, each attribute is detoasted one by one. This means that if
there are multiple toasted columns, it's possible that some columns have already
been detoasted while the remaining ones cannot be accessed (e.g., they were
removed by vacuum). This can happen because we have a "HOT" mechanism that
avoids updating unchanged toast columns, so if a user only updates one toast
column value, the other toast columns in the table can still point to old chunks
that may no longer be available.
In such cases, the code continues rewriting without freeing the already
detoasted values, unlike what we do in toast_tuple_cleanup.
2.
This patch does not add TOAST_MISSING_OK to the
end_heap_rewrite->raw_heap_insert call path, but it does seem possible to
trigger the similar bug there.
In rewrite_heap_tuple(), the tuple being updated is added to the
rs_unresolved_tups hash. If that tuple has an unavailable toast value, then when
end_heap_rewrite() is reached, it will attempt to read that toast value and
report a "missing chunk" error.
Please see the attachment which contains a script that I had AI generate to
reproduce both issues.
Best Regards,
Zhijie Hou