1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

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 <veldanda.nikhilkumar17@gmail.com>
Discussion: https://postgr.es/m/CAFAfj_FeJQZrKyVn7ur0aJx8iz8SR14NheVJNyc8JNoqtMX+og@mail.gmail.com
This commit is contained in:
Michael Paquier
2026-09-28 12:12:10 +09:00
parent 5e59290402
commit 74f7f684d6

View File

@@ -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);