Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita0942f4

Browse files
committed
Add ExecCopySlotMinimalTupleExtra().
Allows an "extra" argument that allocates extra memory at the end ofthe MinimalTuple. This is important for callers that need to storeadditional 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 parent4d14350 commita0942f4

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(TupleDesctupleDescriptor,
14541454
constDatum*values,
1455-
constbool*isnull)
1455+
constbool*isnull,
1456+
Sizeextra)
14561457
{
14571458
MinimalTupletuple;/* return tuple */
1459+
char*mem;
14581460
Sizelen,
14591461
data_len;
14601462
inthoff;
14611463
boolhasnull= false;
14621464
intnumberOfAttributes=tupleDescriptor->natts;
14631465
inti;
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(MinimalTuplemtup)
1542+
heap_copy_minimal_tuple(MinimalTuplemtup,Sizeextra)
15371543
{
15381544
MinimalTupleresult;
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
returnresult;
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(HeapTuplehtup)
1587+
minimal_tuple_from_heap_tuple(HeapTuplehtup,Sizeextra)
15781588
{
15791589
MinimalTupleresult;
1590+
char*mem;
15801591
uint32len;
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
returnresult;
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
staticMinimalTuple
301-
tts_virtual_copy_minimal_tuple(TupleTableSlot*slot)
301+
tts_virtual_copy_minimal_tuple(TupleTableSlot*slot,Sizeextra)
302302
{
303303
Assert(!TTS_EMPTY(slot));
304304

305305
returnheap_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
staticMinimalTuple
475-
tts_heap_copy_minimal_tuple(TupleTableSlot*slot)
476+
tts_heap_copy_minimal_tuple(TupleTableSlot*slot,Sizeextra)
476477
{
477478
HeapTupleTableSlot*hslot= (HeapTupleTableSlot*)slot;
478479

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

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

485486
staticvoid
@@ -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
staticMinimalTuple
669-
tts_minimal_copy_minimal_tuple(TupleTableSlot*slot)
671+
tts_minimal_copy_minimal_tuple(TupleTableSlot*slot,Sizeextra)
670672
{
671673
MinimalTupleTableSlot*mslot= (MinimalTupleTableSlot*)slot;
672674

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

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

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

928930
staticMinimalTuple
929-
tts_buffer_heap_copy_minimal_tuple(TupleTableSlot*slot)
931+
tts_buffer_heap_copy_minimal_tuple(TupleTableSlot*slot,Sizeextra)
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-
returnminimal_tuple_from_heap_tuple(bslot->base.tuple);
940+
returnminimal_tuple_from_heap_tuple(bslot->base.tuple,extra);
939941
}
940942

941943
staticinlinevoid
@@ -1895,7 +1897,7 @@ ExecFetchSlotMinimalTuple(TupleTableSlot *slot,
18951897
{
18961898
if (shouldFree)
18971899
*shouldFree= true;
1898-
returnslot->tts_ops->copy_minimal_tuple(slot);
1900+
returnslot->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-
returntup ?heap_copy_minimal_tuple(tup) :NULL;
738+
returntup ?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
MinimalTupletuple;
788788
MemoryContextoldcxt=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
MinimalTupletuple;
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
returntuple;
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
externvoidheap_freetuple(HeapTuplehtup);
841841
externMinimalTupleheap_form_minimal_tuple(TupleDesctupleDescriptor,
842-
constDatum*values,constbool*isnull);
842+
constDatum*values,constbool*isnull,
843+
Sizeextra);
843844
externvoidheap_free_minimal_tuple(MinimalTuplemtup);
844-
externMinimalTupleheap_copy_minimal_tuple(MinimalTuplemtup);
845+
externMinimalTupleheap_copy_minimal_tuple(MinimalTuplemtup,Sizeextra);
845846
externHeapTupleheap_tuple_from_minimal_tuple(MinimalTuplemtup);
846-
externMinimalTupleminimal_tuple_from_heap_tuple(HeapTuplehtup);
847+
externMinimalTupleminimal_tuple_from_heap_tuple(HeapTuplehtup,Sizeextra);
847848
externsize_tvarsize_any(void*p);
848849
externHeapTupleheap_expand_tuple(HeapTuplesourceTuple,TupleDesctupleDesc);
849850
externMinimalTupleminimal_expand_tuple(HeapTuplesourceTuple,TupleDesctupleDesc);

‎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,Sizeextra);
223227
};
224228

225229
/*
@@ -491,7 +495,19 @@ ExecCopySlotHeapTuple(TupleTableSlot *slot)
491495
staticinlineMinimalTuple
492496
ExecCopySlotMinimalTuple(TupleTableSlot*slot)
493497
{
494-
returnslot->tts_ops->copy_minimal_tuple(slot);
498+
returnslot->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+
staticinlineMinimalTuple
508+
ExecCopySlotMinimalTupleExtra(TupleTableSlot*slot,Sizeextra)
509+
{
510+
returnslot->tts_ops->copy_minimal_tuple(slot,extra);
495511
}
496512

497513
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp