From 6a5c9cc2ae08f6d2a0ddb3f1675b28e67b9d1773 Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Veldanda Date: Thu, 24 Sep 2026 00:29:05 -0700 Subject: [PATCH v4 1/4] 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 a pointer to the struct and derives everything else from the tag. No behavior change. --- src/backend/access/common/toast_internals.c | 29 ++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c index 5485d4e3fa9..353fbd45f20 100644 --- a/src/backend/access/common/toast_internals.c +++ b/src/backend/access/common/toast_internals.c @@ -29,6 +29,7 @@ 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 *ptr); /* ---------- * toast_compress_datum - @@ -391,9 +392,7 @@ 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); } else { @@ -404,14 +403,32 @@ 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); } return PointerGetDatum(result); } +/* ---------- + * toast_pointer_build - + * + * Build an on-disk TOAST pointer datum of the given tag from the + * varatt_external_oid or varatt_external_oid8 at ptr. + * ---------- + */ +static varlena * +toast_pointer_build(vartag_external tag, const void *ptr) +{ + varlena *result; + + result = (varlena *) palloc(VARHDRSZ_EXTERNAL + VARTAG_SIZE(tag)); + SET_VARTAG_EXTERNAL(result, tag); + Assert(VARATT_IS_EXTERNAL_ONDISK(result)); + memcpy(VARDATA_EXTERNAL(result), ptr, VARTAG_SIZE(tag)); + + return result; +} + /* ---------- * toast_valueid_scankey_init - * base-commit: 16a104073d012ac2c70e9d233dc5e76f71455035 -- 2.54.0 (Apple Git-157)