From 425a02ba81f6c24742f5f959e81b44531b357071 Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Veldanda Date: Thu, 24 Sep 2026 00:29:05 -0700 Subject: [PATCH v3 2/5] Factor out the assembly of on-disk TOAST pointers toast_save_datum() ended with two copies of the same three steps, one per kind of TOAST pointer: allocate the datum, set its tag, copy the varatt_external_oid or varatt_external_oid8 into it. Move them into a helper, toast_pointer_build(), which takes the tag and the fixed part of the pointer and checks that the two agree in size. No behavior change. --- src/backend/access/common/toast_internals.c | 34 +++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c index c979e4a32a2..23a55ed4bf4 100644 --- a/src/backend/access/common/toast_internals.c +++ b/src/backend/access/common/toast_internals.c @@ -28,6 +28,8 @@ static bool toastrel_valueid_exists(Relation toastrel, Oid8 valueid); static bool toastid_valueid_exists(Oid toastrelid, Oid8 valueid); +static varlena *toast_pointer_build(vartag_external tag, const void *fixed, + Size fixedsize); /* ---------- * toast_compress_datum - @@ -390,9 +392,8 @@ toast_save_datum(Relation rel, Datum value, VARATT_EXTERNAL_OID8_SET_VALUEID(&toast_pointer, va_valueid); toast_pointer.va_toastrelid = va_toastrelid; - result = (varlena *) palloc(TOAST_OID8_POINTER_SIZE); - SET_VARTAG_EXTERNAL(result, VARTAG_ONDISK_OID8); - memcpy(VARDATA_EXTERNAL(result), &toast_pointer, sizeof(toast_pointer)); + result = toast_pointer_build(VARTAG_ONDISK_OID8, + &toast_pointer, sizeof(toast_pointer)); } else { @@ -403,14 +404,35 @@ toast_save_datum(Relation rel, Datum value, toast_pointer.va_valueid = (Oid) va_valueid; toast_pointer.va_toastrelid = va_toastrelid; - result = (varlena *) palloc(TOAST_OID_POINTER_SIZE); - SET_VARTAG_EXTERNAL(result, VARTAG_ONDISK_OID); - memcpy(VARDATA_EXTERNAL(result), &toast_pointer, sizeof(toast_pointer)); + result = toast_pointer_build(VARTAG_ONDISK_OID, + &toast_pointer, sizeof(toast_pointer)); } return PointerGetDatum(result); } +/* ---------- + * toast_pointer_build - + * + * Build an on-disk TOAST pointer datum of the given tag from its fixed + * part (a varatt_external_oid or varatt_external_oid8). + * ---------- + */ +static varlena * +toast_pointer_build(vartag_external tag, const void *fixed, Size fixedsize) +{ + varlena *result; + + Assert(tag == VARTAG_ONDISK_OID || tag == VARTAG_ONDISK_OID8); + Assert(VARTAG_SIZE(tag) == fixedsize); + + result = (varlena *) palloc(VARHDRSZ_EXTERNAL + VARTAG_SIZE(tag)); + SET_VARTAG_EXTERNAL(result, tag); + memcpy(VARDATA_EXTERNAL(result), fixed, fixedsize); + + return result; +} + /* ---------- * toast_valueid_scankey_init - * -- 2.54.0 (Apple Git-157)