Skip to content

Commit a0942f4

Browse files
committed
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
1 parent 4d14350 commit a0942f4

File tree

7 files changed

+61
-27
lines changed

7 files changed

+61
-27
lines changed

src/backend/access/common/heaptuple.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,16 +1452,20 @@ heap_freetuple(HeapTuple htup)
14521452
MinimalTuple
14531453
heap_form_minimal_tuple(TupleDesc tupleDescriptor,
14541454
const Datum *values,
1455-
const bool *isnull)
1455+
const bool *isnull,
1456+
Size extra)
14561457
{
14571458
MinimalTuple tuple; /* return tuple */
1459+
char *mem;
14581460
Size len,
14591461
data_len;
14601462
int hoff;
14611463
bool hasnull = false;
14621464
int numberOfAttributes = tupleDescriptor->natts;
14631465
int i;
14641466

1467+
Assert(extra == MAXALIGN(extra));
1468+
14651469
if (numberOfAttributes > MaxTupleAttributeNumber)
14661470
ereport(ERROR,
14671471
(errcode(ERRCODE_TOO_MANY_COLUMNS),
@@ -1497,7 +1501,9 @@ heap_form_minimal_tuple(TupleDesc tupleDescriptor,
14971501
/*
14981502
* Allocate and zero the space needed.
14991503
*/
1500-
tuple = (MinimalTuple) palloc0(len);
1504+
mem = palloc0(len + extra);
1505+
memset(mem, 0, extra);
1506+
tuple = (MinimalTuple) (mem + extra);
15011507

15021508
/*
15031509
* And fill in the information.
@@ -1533,11 +1539,15 @@ heap_free_minimal_tuple(MinimalTuple mtup)
15331539
* The result is allocated in the current memory context.
15341540
*/
15351541
MinimalTuple
1536-
heap_copy_minimal_tuple(MinimalTuple mtup)
1542+
heap_copy_minimal_tuple(MinimalTuple mtup, Size extra)
15371543
{
15381544
MinimalTuple result;
1545+
char *mem;
15391546

1540-
result = (MinimalTuple) palloc(mtup->t_len);
1547+
Assert(extra == MAXALIGN(extra));
1548+
mem = palloc(mtup->t_len + extra);
1549+
memset(mem, 0, extra);
1550+
result = (MinimalTuple) (mem + extra);
15411551
memcpy(result, mtup, mtup->t_len);
15421552
return result;
15431553
}
@@ -1574,15 +1584,20 @@ heap_tuple_from_minimal_tuple(MinimalTuple mtup)
15741584
* The result is allocated in the current memory context.
15751585
*/
15761586
MinimalTuple
1577-
minimal_tuple_from_heap_tuple(HeapTuple htup)
1587+
minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra)
15781588
{
15791589
MinimalTuple result;
1590+
char *mem;
15801591
uint32 len;
15811592

1593+
Assert(extra == MAXALIGN(extra));
15821594
Assert(htup->t_len > MINIMAL_TUPLE_OFFSET);
15831595
len = htup->t_len - MINIMAL_TUPLE_OFFSET;
1584-
result = (MinimalTuple) palloc(len);
1596+
mem = palloc(len + extra);
1597+
memset(mem, 0, extra);
1598+
result = (MinimalTuple) (mem + extra);
15851599
memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
1600+
15861601
result->t_len = len;
15871602
return result;
15881603
}

src/backend/executor/execTuples.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,14 @@ tts_virtual_copy_heap_tuple(TupleTableSlot *slot)
298298
}
299299

300300
static MinimalTuple
301-
tts_virtual_copy_minimal_tuple(TupleTableSlot *slot)
301+
tts_virtual_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
302302
{
303303
Assert(!TTS_EMPTY(slot));
304304

305305
return heap_form_minimal_tuple(slot->tts_tupleDescriptor,
306306
slot->tts_values,
307-
slot->tts_isnull);
307+
slot->tts_isnull,
308+
extra);
308309
}
309310

310311

@@ -472,14 +473,14 @@ tts_heap_copy_heap_tuple(TupleTableSlot *slot)
472473
}
473474

474475
static MinimalTuple
475-
tts_heap_copy_minimal_tuple(TupleTableSlot *slot)
476+
tts_heap_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
476477
{
477478
HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
478479

479480
if (!hslot->tuple)
480481
tts_heap_materialize(slot);
481482

482-
return minimal_tuple_from_heap_tuple(hslot->tuple);
483+
return minimal_tuple_from_heap_tuple(hslot->tuple, extra);
483484
}
484485

485486
static void
@@ -607,7 +608,8 @@ tts_minimal_materialize(TupleTableSlot *slot)
607608
{
608609
mslot->mintuple = heap_form_minimal_tuple(slot->tts_tupleDescriptor,
609610
slot->tts_values,
610-
slot->tts_isnull);
611+
slot->tts_isnull,
612+
0);
611613
}
612614
else
613615
{
@@ -617,7 +619,7 @@ tts_minimal_materialize(TupleTableSlot *slot)
617619
* TTS_FLAG_SHOULDFREE set). Copy the minimal tuple into the given
618620
* slot's memory context.
619621
*/
620-
mslot->mintuple = heap_copy_minimal_tuple(mslot->mintuple);
622+
mslot->mintuple = heap_copy_minimal_tuple(mslot->mintuple, 0);
621623
}
622624

623625
slot->tts_flags |= TTS_FLAG_SHOULDFREE;
@@ -666,14 +668,14 @@ tts_minimal_copy_heap_tuple(TupleTableSlot *slot)
666668
}
667669

668670
static MinimalTuple
669-
tts_minimal_copy_minimal_tuple(TupleTableSlot *slot)
671+
tts_minimal_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
670672
{
671673
MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
672674

673675
if (!mslot->mintuple)
674676
tts_minimal_materialize(slot);
675677

676-
return heap_copy_minimal_tuple(mslot->mintuple);
678+
return heap_copy_minimal_tuple(mslot->mintuple, extra);
677679
}
678680

679681
static void
@@ -926,7 +928,7 @@ tts_buffer_heap_copy_heap_tuple(TupleTableSlot *slot)
926928
}
927929

928930
static MinimalTuple
929-
tts_buffer_heap_copy_minimal_tuple(TupleTableSlot *slot)
931+
tts_buffer_heap_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
930932
{
931933
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
932934

@@ -935,7 +937,7 @@ tts_buffer_heap_copy_minimal_tuple(TupleTableSlot *slot)
935937
if (!bslot->base.tuple)
936938
tts_buffer_heap_materialize(slot);
937939

938-
return minimal_tuple_from_heap_tuple(bslot->base.tuple);
940+
return minimal_tuple_from_heap_tuple(bslot->base.tuple, extra);
939941
}
940942

941943
static inline void
@@ -1895,7 +1897,7 @@ ExecFetchSlotMinimalTuple(TupleTableSlot *slot,
18951897
{
18961898
if (shouldFree)
18971899
*shouldFree = true;
1898-
return slot->tts_ops->copy_minimal_tuple(slot);
1900+
return slot->tts_ops->copy_minimal_tuple(slot, 0);
18991901
}
19001902
}
19011903

src/backend/executor/nodeGatherMerge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ gm_readnext_tuple(GatherMergeState *gm_state, int nreader, bool nowait,
735735
* Since we'll be buffering these across multiple calls, we need to make a
736736
* copy.
737737
*/
738-
return tup ? heap_copy_minimal_tuple(tup) : NULL;
738+
return tup ? heap_copy_minimal_tuple(tup, 0) : NULL;
739739
}
740740

741741
/*

src/backend/utils/sort/tuplesortvariants.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy,
10021002
*abbrev = stup.datum1;
10031003

10041004
if (copy)
1005-
stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple);
1005+
stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple, 0);
10061006

10071007
ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, copy);
10081008
return true;

src/backend/utils/sort/tuplestore.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc,
787787
MinimalTuple tuple;
788788
MemoryContext oldcxt = MemoryContextSwitchTo(state->context);
789789

790-
tuple = heap_form_minimal_tuple(tdesc, values, isnull);
790+
tuple = heap_form_minimal_tuple(tdesc, values, isnull, 0);
791791
USEMEM(state, GetMemoryChunkSpace(tuple));
792792

793793
tuplestore_puttuple_common(state, tuple);
@@ -1139,7 +1139,7 @@ tuplestore_gettupleslot(Tuplestorestate *state, bool forward,
11391139
{
11401140
if (copy && !should_free)
11411141
{
1142-
tuple = heap_copy_minimal_tuple(tuple);
1142+
tuple = heap_copy_minimal_tuple(tuple, 0);
11431143
should_free = true;
11441144
}
11451145
ExecStoreMinimalTuple(tuple, slot, should_free);
@@ -1590,7 +1590,7 @@ copytup_heap(Tuplestorestate *state, void *tup)
15901590
{
15911591
MinimalTuple tuple;
15921592

1593-
tuple = minimal_tuple_from_heap_tuple((HeapTuple) tup);
1593+
tuple = minimal_tuple_from_heap_tuple((HeapTuple) tup, 0);
15941594
USEMEM(state, GetMemoryChunkSpace(tuple));
15951595
return tuple;
15961596
}

src/include/access/htup_details.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,12 @@ extern void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
839839
Datum *values, bool *isnull);
840840
extern void heap_freetuple(HeapTuple htup);
841841
extern MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor,
842-
const Datum *values, const bool *isnull);
842+
const Datum *values, const bool *isnull,
843+
Size extra);
843844
extern void heap_free_minimal_tuple(MinimalTuple mtup);
844-
extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup);
845+
extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup, Size extra);
845846
extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
846-
extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup);
847+
extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra);
847848
extern size_t varsize_any(void *p);
848849
extern HeapTuple heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
849850
extern MinimalTuple minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);

src/include/executor/tuptable.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,12 @@ struct TupleTableSlotOps
218218
* meaningful "system columns" in the copy. The copy is not be "owned" by
219219
* the slot i.e. the caller has to take responsibility to free memory
220220
* consumed by the slot.
221+
*
222+
* The copy has "extra" bytes (maxaligned and zeroed) available before the
223+
* tuple, which is useful so that some callers may store extra data along
224+
* with the minimal tuple without the need for an additional allocation.
221225
*/
222-
MinimalTuple (*copy_minimal_tuple) (TupleTableSlot *slot);
226+
MinimalTuple (*copy_minimal_tuple) (TupleTableSlot *slot, Size extra);
223227
};
224228

225229
/*
@@ -491,7 +495,19 @@ ExecCopySlotHeapTuple(TupleTableSlot *slot)
491495
static inline MinimalTuple
492496
ExecCopySlotMinimalTuple(TupleTableSlot *slot)
493497
{
494-
return slot->tts_ops->copy_minimal_tuple(slot);
498+
return slot->tts_ops->copy_minimal_tuple(slot, 0);
499+
}
500+
501+
/*
502+
* ExecCopySlotMinimalTupleExtra - return MinimalTuple allocated in caller's
503+
* context, with extra bytes (maxaligned and zeroed) before the tuple for data
504+
* the caller wishes to store along with the tuple (without requiring the
505+
* caller to make an additional allocation).
506+
*/
507+
static inline MinimalTuple
508+
ExecCopySlotMinimalTupleExtra(TupleTableSlot *slot, Size extra)
509+
{
510+
return slot->tts_ops->copy_minimal_tuple(slot, extra);
495511
}
496512

497513
/*

0 commit comments

Comments
 (0)