From f65cb5dfdf8208282ae3a924f33a9df7677ba8ca Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 18 Dec 2025 09:51:01 +0000
Subject: [PATCH v1 1/3] Don't cast away const where possible

Add const to read only local variables, preserving the const qualifiers from the
function signatures.

This does not change all such instances, but only those hand-picked by the author.

The ones that are not changed:

- are just thin wrappers
- would require public API changes
- rely on external functions (such as LZ4F_compressUpdate())
- would require complex subsystem changes
---
 src/backend/access/brin/brin_minmax_multi.c          | 4 ++--
 src/backend/access/heap/pruneheap.c                  | 4 ++--
 src/backend/access/spgist/spgkdtreeproc.c            | 8 ++++----
 src/backend/statistics/mcv.c                         | 4 ++--
 src/backend/tsearch/spell.c                          | 4 ++--
 src/test/modules/injection_points/injection_points.c | 8 ++++----
 6 files changed, 16 insertions(+), 16 deletions(-)
  10.8% src/backend/access/brin/
  12.8% src/backend/access/heap/
  19.8% src/backend/access/spgist/
   8.8% src/backend/statistics/
  11.2% src/backend/tsearch/
  36.2% src/test/modules/injection_points/

diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c
index 0298a9da8ba..a2d72aa1791 100644
--- a/src/backend/access/brin/brin_minmax_multi.c
+++ b/src/backend/access/brin/brin_minmax_multi.c
@@ -1304,8 +1304,8 @@ merge_overlapping_ranges(FmgrInfo *cmp, Oid colloid,
 static int
 compare_distances(const void *a, const void *b)
 {
-	DistanceValue *da = (DistanceValue *) a;
-	DistanceValue *db = (DistanceValue *) b;
+	const DistanceValue *da = (const DistanceValue *) a;
+	const DistanceValue *db = (const DistanceValue *) b;
 
 	if (da->value < db->value)
 		return 1;
diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index 07aa08cfe14..dbb417217f1 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -2021,8 +2021,8 @@ heap_log_freeze_eq(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
 static int
 heap_log_freeze_cmp(const void *arg1, const void *arg2)
 {
-	HeapTupleFreeze *frz1 = (HeapTupleFreeze *) arg1;
-	HeapTupleFreeze *frz2 = (HeapTupleFreeze *) arg2;
+	const HeapTupleFreeze *frz1 = (const HeapTupleFreeze *) arg1;
+	const HeapTupleFreeze *frz2 = (const HeapTupleFreeze *) arg2;
 
 	if (frz1->xmax < frz2->xmax)
 		return -1;
diff --git a/src/backend/access/spgist/spgkdtreeproc.c b/src/backend/access/spgist/spgkdtreeproc.c
index f0167d6ffa6..dc29d6898c5 100644
--- a/src/backend/access/spgist/spgkdtreeproc.c
+++ b/src/backend/access/spgist/spgkdtreeproc.c
@@ -84,8 +84,8 @@ typedef struct SortedPoint
 static int
 x_cmp(const void *a, const void *b)
 {
-	SortedPoint *pa = (SortedPoint *) a;
-	SortedPoint *pb = (SortedPoint *) b;
+	const SortedPoint *pa = (const SortedPoint *) a;
+	const SortedPoint *pb = (const SortedPoint *) b;
 
 	if (pa->p->x == pb->p->x)
 		return 0;
@@ -95,8 +95,8 @@ x_cmp(const void *a, const void *b)
 static int
 y_cmp(const void *a, const void *b)
 {
-	SortedPoint *pa = (SortedPoint *) a;
-	SortedPoint *pb = (SortedPoint *) b;
+	const SortedPoint *pa = (const SortedPoint *) a;
+	const SortedPoint *pb = (const SortedPoint *) b;
 
 	if (pa->p->y == pb->p->y)
 		return 0;
diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.c
index ec650ba029f..1d210cdba3c 100644
--- a/src/backend/statistics/mcv.c
+++ b/src/backend/statistics/mcv.c
@@ -402,8 +402,8 @@ count_distinct_groups(int numrows, SortItem *items, MultiSortSupport mss)
 static int
 compare_sort_item_count(const void *a, const void *b, void *arg)
 {
-	SortItem   *ia = (SortItem *) a;
-	SortItem   *ib = (SortItem *) b;
+	const SortItem *ia = (const SortItem *) a;
+	const SortItem *ib = (const SortItem *) b;
 
 	if (ia->count == ib->count)
 		return 0;
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index e5badb6b43f..1af6b212245 100644
--- a/src/backend/tsearch/spell.c
+++ b/src/backend/tsearch/spell.c
@@ -210,8 +210,8 @@ cmpspellaffix(const void *s1, const void *s2)
 static int
 cmpcmdflag(const void *f1, const void *f2)
 {
-	CompoundAffixFlag *fv1 = (CompoundAffixFlag *) f1,
-			   *fv2 = (CompoundAffixFlag *) f2;
+	const CompoundAffixFlag *fv1 = (const CompoundAffixFlag *) f1,
+			   *fv2 = (const CompoundAffixFlag *) f2;
 
 	Assert(fv1->flagMode == fv2->flagMode);
 
diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c
index 25340e8d81b..df8f4cf1717 100644
--- a/src/test/modules/injection_points/injection_points.c
+++ b/src/test/modules/injection_points/injection_points.c
@@ -189,7 +189,7 @@ injection_init_shmem(void)
  * otherwise.
  */
 static bool
-injection_point_allowed(InjectionPointCondition *condition)
+injection_point_allowed(const InjectionPointCondition *condition)
 {
 	bool		result = true;
 
@@ -232,7 +232,7 @@ injection_points_cleanup(int code, Datum arg)
 void
 injection_error(const char *name, const void *private_data, void *arg)
 {
-	InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
+	const InjectionPointCondition *condition = (const InjectionPointCondition *) private_data;
 	char	   *argstr = (char *) arg;
 
 	if (!injection_point_allowed(condition))
@@ -248,7 +248,7 @@ injection_error(const char *name, const void *private_data, void *arg)
 void
 injection_notice(const char *name, const void *private_data, void *arg)
 {
-	InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
+	const InjectionPointCondition *condition = (const InjectionPointCondition *) private_data;
 	char	   *argstr = (char *) arg;
 
 	if (!injection_point_allowed(condition))
@@ -268,7 +268,7 @@ injection_wait(const char *name, const void *private_data, void *arg)
 	uint32		old_wait_counts = 0;
 	int			index = -1;
 	uint32		injection_wait_event = 0;
-	InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
+	const InjectionPointCondition *condition = (const InjectionPointCondition *) private_data;
 
 	if (inj_state == NULL)
 		injection_init_shmem();
-- 
2.34.1

