From 99b023afc89108aa5a4b795014d92014a7689caf Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Wed, 5 Mar 2025 19:37:21 +0100
Subject: [PATCH v20250307 1/4] Remove size argument from GIN tuplesort infra

The length is already implied by GinTuple->tuplen, so there's
no need to pass it around separately.
---
 src/include/utils/tuplesort.h              |  2 +-
 src/backend/access/gin/gininsert.c         | 28 ++++++++--------------
 src/backend/utils/sort/tuplesortvariants.c |  3 ++-
 3 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/src/include/utils/tuplesort.h b/src/include/utils/tuplesort.h
index ef79f259f93..be1bd8a1862 100644
--- a/src/include/utils/tuplesort.h
+++ b/src/include/utils/tuplesort.h
@@ -461,7 +461,7 @@ extern void tuplesort_putindextuplevalues(Tuplesortstate *state,
 										  Relation rel, ItemPointer self,
 										  const Datum *values, const bool *isnull);
 extern void tuplesort_putbrintuple(Tuplesortstate *state, BrinTuple *tuple, Size size);
-extern void tuplesort_putgintuple(Tuplesortstate *state, GinTuple *tuple, Size size);
+extern void tuplesort_putgintuple(Tuplesortstate *state, GinTuple *tuple);
 extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
 							   bool isNull);
 
diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c
index b2f89cad880..f5782ea95df 100644
--- a/src/backend/access/gin/gininsert.c
+++ b/src/backend/access/gin/gininsert.c
@@ -195,8 +195,7 @@ static Datum _gin_parse_tuple_key(GinTuple *a);
 
 static GinTuple *_gin_build_tuple(OffsetNumber attrnum, unsigned char category,
 								  Datum key, int16 typlen, bool typbyval,
-								  ItemPointerData *items, uint32 nitems,
-								  Size *len);
+								  ItemPointerData *items, uint32 nitems);
 
 /*
  * Adds array of item pointers to tuple's posting list, or
@@ -499,16 +498,15 @@ ginFlushBuildState(GinBuildState *buildstate, Relation index)
 
 		/* GIN tuple and tuple length */
 		GinTuple   *tup;
-		Size		tuplen;
 
 		/* there could be many entries, so be willing to abort here */
 		CHECK_FOR_INTERRUPTS();
 
 		tup = _gin_build_tuple(attnum, category,
 							   key, attr->attlen, attr->attbyval,
-							   list, nlist, &tuplen);
+							   list, nlist);
 
-		tuplesort_putgintuple(buildstate->bs_worker_sort, tup, tuplen);
+		tuplesort_putgintuple(buildstate->bs_worker_sort, tup);
 
 		pfree(tup);
 	}
@@ -1852,7 +1850,6 @@ _gin_process_worker_data(GinBuildState *state, Tuplesortstate *worker_sort,
 		if (!GinBufferCanAddKey(buffer, tup))
 		{
 			GinTuple   *ntup;
-			Size		ntuplen;
 
 			/*
 			 * Buffer is not empty and it's storing a different key - flush
@@ -1863,9 +1860,9 @@ _gin_process_worker_data(GinBuildState *state, Tuplesortstate *worker_sort,
 
 			ntup = _gin_build_tuple(buffer->attnum, buffer->category,
 									buffer->key, buffer->typlen, buffer->typbyval,
-									buffer->items, buffer->nitems, &ntuplen);
+									buffer->items, buffer->nitems);
 
-			tuplesort_putgintuple(state->bs_sortstate, ntup, ntuplen);
+			tuplesort_putgintuple(state->bs_sortstate, ntup);
 			state->bs_numtuples++;
 
 			pfree(ntup);
@@ -1884,7 +1881,6 @@ _gin_process_worker_data(GinBuildState *state, Tuplesortstate *worker_sort,
 		if (GinBufferShouldTrim(buffer, tup))
 		{
 			GinTuple   *ntup;
-			Size		ntuplen;
 
 			Assert(buffer->nfrozen > 0);
 
@@ -1897,9 +1893,9 @@ _gin_process_worker_data(GinBuildState *state, Tuplesortstate *worker_sort,
 
 			ntup = _gin_build_tuple(buffer->attnum, buffer->category,
 									buffer->key, buffer->typlen, buffer->typbyval,
-									buffer->items, buffer->nfrozen, &ntuplen);
+									buffer->items, buffer->nfrozen);
 
-			tuplesort_putgintuple(state->bs_sortstate, ntup, ntuplen);
+			tuplesort_putgintuple(state->bs_sortstate, ntup);
 
 			pfree(ntup);
 
@@ -1918,15 +1914,14 @@ _gin_process_worker_data(GinBuildState *state, Tuplesortstate *worker_sort,
 	if (!GinBufferIsEmpty(buffer))
 	{
 		GinTuple   *ntup;
-		Size		ntuplen;
 
 		AssertCheckItemPointers(buffer);
 
 		ntup = _gin_build_tuple(buffer->attnum, buffer->category,
 								buffer->key, buffer->typlen, buffer->typbyval,
-								buffer->items, buffer->nitems, &ntuplen);
+								buffer->items, buffer->nitems);
 
-		tuplesort_putgintuple(state->bs_sortstate, ntup, ntuplen);
+		tuplesort_putgintuple(state->bs_sortstate, ntup);
 		state->bs_numtuples++;
 
 		pfree(ntup);
@@ -2187,8 +2182,7 @@ typedef struct
 static GinTuple *
 _gin_build_tuple(OffsetNumber attrnum, unsigned char category,
 				 Datum key, int16 typlen, bool typbyval,
-				 ItemPointerData *items, uint32 nitems,
-				 Size *len)
+				 ItemPointerData *items, uint32 nitems)
 {
 	GinTuple   *tuple;
 	char	   *ptr;
@@ -2256,8 +2250,6 @@ _gin_build_tuple(OffsetNumber attrnum, unsigned char category,
 	 */
 	tuplen = SHORTALIGN(offsetof(GinTuple, data) + keylen) + compresslen;
 
-	*len = tuplen;
-
 	/*
 	 * Allocate space for the whole GIN tuple.
 	 *
diff --git a/src/backend/utils/sort/tuplesortvariants.c b/src/backend/utils/sort/tuplesortvariants.c
index eb8601e2257..0b83b8b25b3 100644
--- a/src/backend/utils/sort/tuplesortvariants.c
+++ b/src/backend/utils/sort/tuplesortvariants.c
@@ -885,12 +885,13 @@ tuplesort_putbrintuple(Tuplesortstate *state, BrinTuple *tuple, Size size)
 }
 
 void
-tuplesort_putgintuple(Tuplesortstate *state, GinTuple *tuple, Size size)
+tuplesort_putgintuple(Tuplesortstate *state, GinTuple *tuple)
 {
 	SortTuple	stup;
 	GinTuple   *ctup;
 	TuplesortPublic *base = TuplesortstateGetPublic(state);
 	MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext);
+	Size		size = tuple->tuplen;
 	Size		tuplen;
 
 	/* copy the GinTuple into the right memory context */
-- 
2.45.2

