diff options
author | Jeff Davis <jdavis@postgresql.org> | 2025-03-24 22:05:53 -0700 |
---|---|---|
committer | Jeff Davis <jdavis@postgresql.org> | 2025-03-24 22:05:53 -0700 |
commit | a0942f441ed651f6345d969b7a8f4774eda1fceb (patch) | |
tree | c40b5889e11c5794963f90096e5f4ba5390059b0 /src/backend/access/common/heaptuple.c | |
parent | 4d143509cbfae0207c35abffae7b0e3b4d078349 (diff) | |
download | postgresql-a0942f441ed651f6345d969b7a8f4774eda1fceb.tar.gz postgresql-a0942f441ed651f6345d969b7a8f4774eda1fceb.zip |
Add ExecCopySlotMinimalTupleExtra().
Allows an "extra" argument that allocates extra memory at the end of
the MinimalTuple. This is important for callers that need to store
additional data, but do not want to perform an additional allocation.
Suggested-by: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CAApHDvppeqw2pNM-+ahBOJwq2QmC0hOAGsmCpC89QVmEoOvsdg@mail.gmail.com
Diffstat (limited to 'src/backend/access/common/heaptuple.c')
-rw-r--r-- | src/backend/access/common/heaptuple.c | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/backend/access/common/heaptuple.c b/src/backend/access/common/heaptuple.c index acd5da4ccf8..969d1028cae 100644 --- a/src/backend/access/common/heaptuple.c +++ b/src/backend/access/common/heaptuple.c @@ -1452,9 +1452,11 @@ heap_freetuple(HeapTuple htup) MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor, const Datum *values, - const bool *isnull) + const bool *isnull, + Size extra) { MinimalTuple tuple; /* return tuple */ + char *mem; Size len, data_len; int hoff; @@ -1462,6 +1464,8 @@ heap_form_minimal_tuple(TupleDesc tupleDescriptor, int numberOfAttributes = tupleDescriptor->natts; int i; + Assert(extra == MAXALIGN(extra)); + if (numberOfAttributes > MaxTupleAttributeNumber) ereport(ERROR, (errcode(ERRCODE_TOO_MANY_COLUMNS), @@ -1497,7 +1501,9 @@ heap_form_minimal_tuple(TupleDesc tupleDescriptor, /* * Allocate and zero the space needed. */ - tuple = (MinimalTuple) palloc0(len); + mem = palloc0(len + extra); + memset(mem, 0, extra); + tuple = (MinimalTuple) (mem + extra); /* * And fill in the information. @@ -1533,11 +1539,15 @@ heap_free_minimal_tuple(MinimalTuple mtup) * The result is allocated in the current memory context. */ MinimalTuple -heap_copy_minimal_tuple(MinimalTuple mtup) +heap_copy_minimal_tuple(MinimalTuple mtup, Size extra) { MinimalTuple result; + char *mem; - result = (MinimalTuple) palloc(mtup->t_len); + Assert(extra == MAXALIGN(extra)); + mem = palloc(mtup->t_len + extra); + memset(mem, 0, extra); + result = (MinimalTuple) (mem + extra); memcpy(result, mtup, mtup->t_len); return result; } @@ -1574,15 +1584,20 @@ heap_tuple_from_minimal_tuple(MinimalTuple mtup) * The result is allocated in the current memory context. */ MinimalTuple -minimal_tuple_from_heap_tuple(HeapTuple htup) +minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra) { MinimalTuple result; + char *mem; uint32 len; + Assert(extra == MAXALIGN(extra)); Assert(htup->t_len > MINIMAL_TUPLE_OFFSET); len = htup->t_len - MINIMAL_TUPLE_OFFSET; - result = (MinimalTuple) palloc(len); + mem = palloc(len + extra); + memset(mem, 0, extra); + result = (MinimalTuple) (mem + extra); memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len); + result->t_len = len; return result; } |