From 74f7f684d6ed1fd3c3aee4d868f2e6d1da0ec93b Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 28 Sep 2026 12:12:10 +0900 Subject: [PATCH] Refactor assembly of external on-disk TOAST pointers toast_save_datum() ended with two copies of the same three steps, one each external on-disk TOAST pointer: allocate a datum, set its tag, then copy the contents from a varatt_external_oid or a varatt_external_oid8 into it. These steps are moved into a new helper function, toast_pointer_build(), which takes the tag and a pointer to the struct and derives everything else from the tag. For now, only two varatt_external_* exist for external on-disk TOAST pointers. Some more are under discussion, where this refactoring shows some benefits in the shape of less duplication. There should be no behavior change. Author: Nikhil Kumar Veldanda Discussion: https://postgr.es/m/CAFAfj_FeJQZrKyVn7ur0aJx8iz8SR14NheVJNyc8JNoqtMX+og@mail.gmail.com --- src/backend/access/common/toast_internals.c | 28 ++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c index 5485d4e3fa9..2bdc0b7d3f2 100644 --- a/src/backend/access/common/toast_internals.c +++ b/src/backend/access/common/toast_internals.c @@ -30,6 +30,26 @@ static bool toastrel_valueid_exists(Relation toastrel, Oid8 valueid); static bool toastid_valueid_exists(Oid toastrelid, Oid8 valueid); +/* ---------- + * toast_pointer_build - + * + * Build an on-disk TOAST pointer datum based on the given tag from "ptr", + * itself a varatt_external_*. + * ---------- + */ +static inline 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_compress_datum - * @@ -391,9 +411,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,9 +422,7 @@ 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);