From 17e0281496ab480afea115d522e680bfe8b37101 Mon Sep 17 00:00:00 2001 From: Hannu Krosing Date: Sun, 23 Aug 2026 20:08:08 +0000 Subject: [PATCH v2 1/8] Refactor detoasting pipeline to unify full and slice fetches Historically, toast_fetch_datum() and toast_fetch_datum_slice() contained largely duplicated logic for pointer extraction, slice bounds calculation, result buffer allocation, compression header handling, and table lifecycle. Unify the implementation by making toast_fetch_datum() a static inline wrapper around toast_fetch_datum_slice(attr, 0, -1). --- src/backend/access/common/detoast.c | 55 ++++------------------------- 1 file changed, 6 insertions(+), 49 deletions(-) diff --git a/src/backend/access/common/detoast.c b/src/backend/access/common/detoast.c index a6c1f3a734b..f85bed1cf99 100644 --- a/src/backend/access/common/detoast.c +++ b/src/backend/access/common/detoast.c @@ -22,10 +22,15 @@ #include "utils/expandeddatum.h" #include "utils/rel.h" -static varlena *toast_fetch_datum(varlena *attr); static varlena *toast_fetch_datum_slice(varlena *attr, int32 sliceoffset, int32 slicelength); +static inline varlena * +toast_fetch_datum(varlena *attr) +{ + return toast_fetch_datum_slice(attr, 0, -1); +} + static varlena *toast_decompress_datum(varlena *attr); static varlena *toast_decompress_datum_slice(varlena *attr, int32 slicelength); @@ -332,54 +337,6 @@ detoast_attr_slice(varlena *attr, return result; } -/* ---------- - * toast_fetch_datum - - * - * Reconstruct an in memory Datum from the chunks saved - * in the toast relation - * ---------- - */ -static varlena * -toast_fetch_datum(varlena *attr) -{ - Relation toastrel; - varlena *result; - varatt_external toast_pointer; - int32 attrsize; - - if (!VARATT_IS_EXTERNAL_ONDISK(attr)) - elog(ERROR, "toast_fetch_datum shouldn't be called for non-ondisk datums"); - - /* Must copy to access aligned fields */ - VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr); - - attrsize = VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer); - - result = (varlena *) palloc(attrsize + VARHDRSZ); - - if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer)) - SET_VARSIZE_COMPRESSED(result, attrsize + VARHDRSZ); - else - SET_VARSIZE(result, attrsize + VARHDRSZ); - - if (attrsize == 0) - return result; /* Probably shouldn't happen, but just in - * case. */ - - /* - * Open the toast relation and its indexes - */ - toastrel = table_open(toast_pointer.va_toastrelid, AccessShareLock); - - /* Fetch all chunks */ - table_relation_fetch_toast_slice(toastrel, toast_pointer.va_valueid, - attrsize, 0, attrsize, result); - - /* Close toast table */ - table_close(toastrel, AccessShareLock); - - return result; -} /* ---------- * toast_fetch_datum_slice - -- 2.55.0.970.g62bdec98f9-goog