0002-Some-stylistic-improvements-in-toast_save_datum.patch

text/plain

Filename: 0002-Some-stylistic-improvements-in-toast_save_datum.patch
Type: text/plain
Part: 1
Message: Re: new warnings with clang-21 / how const is Datum

Patch

Format: format-patch
Series: patch 0002
Subject: Some stylistic improvements in toast_save_datum()
File+
src/backend/access/common/toast_internals.c 16 23
From a4e9c43d5fa9642adcd2efa0f7db8efa45328236 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 8 Sep 2025 13:24:29 +0200
Subject: [PATCH 2/2] Some stylistic improvements in toast_save_datum()

Move some variables to a smaller scope.  Initialize chunk_data before
storing a pointer to it; this avoids compiler warnings on clang21, or
respectively us having to work around it by initializing it to zero
before the variable is used.

Discussion: https://www.postgresql.org/message-id/flat/6604ad6e-5934-43ac-8590-15113d6ae4b1%40eisentraut.org
---
 src/backend/access/common/toast_internals.c | 39 +++++++++------------
 1 file changed, 16 insertions(+), 23 deletions(-)

diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c
index 75e908c2e80..81dbd67c725 100644
--- a/src/backend/access/common/toast_internals.c
+++ b/src/backend/access/common/toast_internals.c
@@ -121,22 +121,10 @@ toast_save_datum(Relation rel, Datum value,
 {
 	Relation	toastrel;
 	Relation   *toastidxs;
-	HeapTuple	toasttup;
 	TupleDesc	toasttupDesc;
-	Datum		t_values[3];
-	bool		t_isnull[3];
 	CommandId	mycid = GetCurrentCommandId(true);
 	struct varlena *result;
 	struct varatt_external toast_pointer;
-	union
-	{
-		struct varlena hdr;
-		/* this is to make the union big enough for a chunk: */
-		char		data[TOAST_MAX_CHUNK_SIZE + VARHDRSZ];
-		/* ensure union is aligned well enough: */
-		int32		align_it;
-	}			chunk_data = {0};	/* silence compiler warning */
-	int32		chunk_size;
 	int32		chunk_seq = 0;
 	char	   *data_p;
 	int32		data_todo;
@@ -289,21 +277,23 @@ toast_save_datum(Relation rel, Datum value,
 		}
 	}
 
-	/*
-	 * Initialize constant parts of the tuple data
-	 */
-	t_values[0] = ObjectIdGetDatum(toast_pointer.va_valueid);
-	t_values[2] = PointerGetDatum(&chunk_data);
-	t_isnull[0] = false;
-	t_isnull[1] = false;
-	t_isnull[2] = false;
-
 	/*
 	 * Split up the item into chunks
 	 */
 	while (data_todo > 0)
 	{
-		int			i;
+		HeapTuple	toasttup;
+		Datum		t_values[3];
+		bool		t_isnull[3] = {0};
+		union
+		{
+			struct varlena hdr;
+			/* this is to make the union big enough for a chunk: */
+			char		data[TOAST_MAX_CHUNK_SIZE + VARHDRSZ];
+			/* ensure union is aligned well enough: */
+			int32		align_it;
+		}			chunk_data;
+		int32		chunk_size;
 
 		CHECK_FOR_INTERRUPTS();
 
@@ -315,9 +305,12 @@ toast_save_datum(Relation rel, Datum value,
 		/*
 		 * Build a tuple and store it
 		 */
+		t_values[0] = ObjectIdGetDatum(toast_pointer.va_valueid);
 		t_values[1] = Int32GetDatum(chunk_seq++);
 		SET_VARSIZE(&chunk_data, chunk_size + VARHDRSZ);
 		memcpy(VARDATA(&chunk_data), data_p, chunk_size);
+		t_values[2] = PointerGetDatum(&chunk_data);
+
 		toasttup = heap_form_tuple(toasttupDesc, t_values, t_isnull);
 
 		heap_insert(toastrel, toasttup, mycid, options, NULL);
@@ -333,7 +326,7 @@ toast_save_datum(Relation rel, Datum value,
 		 * Note also that there had better not be any user-created index on
 		 * the TOAST table, since we don't bother to update anything else.
 		 */
-		for (i = 0; i < num_indexes; i++)
+		for (int i = 0; i < num_indexes; i++)
 		{
 			/* Only index relations marked as ready can be updated */
 			if (toastidxs[i]->rd_index->indisready)
-- 
2.51.0