pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[PATCH v1 1/3] Don't cast away const where possible
13+ messages / 3 participants
[nested] [flat]

* [PATCH v1 1/3] Don't cast away const where possible
@ 2025-12-18 09:51  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Bertrand Drouvot @ 2025-12-18 09:51 UTC (permalink / raw)

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


--jEYXENy/7KdvfqDm
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v1-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch"



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Don't cast away const where possible
@ 2025-12-18 13:55  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Bertrand Drouvot @ 2025-12-18 13:55 UTC (permalink / raw)
  To: pgsql-hackers@lists.postgresql.org

Hi hackers,

Some functions are casting away the const qualifiers from their signatures in
local variables.

These 3 patches add const to read only local variables, preserving the const
qualifiers from the function signatures.

0001: those are simple changes in 6 files (16 changes in total)

0002: Add const to read only TableInfo pointers in pg_dump

Functions that dump table data receive their parameters through const void *
but were casting away const. Add const qualifiers to functions that only read
the table information.

Also change getRootTableInfo to return const TableInfo *, since it only traverses
the parent chain without modifying any TableInfo structures. This allows the dump
functions to maintain const correctness when calling it.

0003: Separate read and write pointers in pg_saslprep

Use separate pointers for reading const input ('p') and writing
to mutable output ('outp'), avoiding the need to cast away const on the input
parameter.

It has been done with the help of [1], but not all the changes proposed by it have
been implemented. Indeed, I did some filtering and decided not to change the ones
that:

- are just thin wrappers
- would require public API changes
- rely on external functions (such as LZ4F_compressUpdate())
- would require changes beyond the scope of this cleanup

[1]: https://github.com/bdrouvot/coccinelle_on_pg/blob/main/misc/search_const_away.cocci

Thoughts?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

  [text/x-diff] v1-0001-Don-t-cast-away-const-where-possible.patch (6.1K, ../../aUQHy%2FMmWq7c97wK@ip-10-97-1-34.eu-west-3.compute.internal/2-v1-0001-Don-t-cast-away-const-where-possible.patch)
  download | inline diff:
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

  [text/x-diff] v1-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch (3.5K, ../../aUQHy%2FMmWq7c97wK@ip-10-97-1-34.eu-west-3.compute.internal/3-v1-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch)
  download | inline diff:
From a3d493f1f8430949a1bae1d5718baae9caea61cb Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 18 Dec 2025 12:47:07 +0000
Subject: [PATCH v1 2/3] Add const to read only TableInfo pointers in pg_dump

Functions that dump table data receive their parameters through const void *
but were casting away const. Add const qualifiers to functions that only read
the table information.

Also change getRootTableInfo to return const TableInfo *, since it only traverses
the parent chain without modifying any TableInfo structures. This allows the dump
functions to maintain const correctness when calling it.
---
 src/bin/pg_dump/pg_dump.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
 100.0% src/bin/pg_dump/

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 24ad201af2f..6aecc8b32fd 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -412,7 +412,7 @@ static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
 static char *get_synchronized_snapshot(Archive *fout);
 static void set_restrict_relation_kind(Archive *AH, const char *value);
 static void setupDumpWorker(Archive *AH);
-static TableInfo *getRootTableInfo(const TableInfo *tbinfo);
+static const TableInfo *getRootTableInfo(const TableInfo *tbinfo);
 static bool forcePartitionRootLoad(const TableInfo *tbinfo);
 static void read_dump_filters(const char *filename, DumpOptions *dopt);
 
@@ -2379,8 +2379,8 @@ selectDumpableObject(DumpableObject *dobj, Archive *fout)
 static int
 dumpTableData_copy(Archive *fout, const void *dcontext)
 {
-	TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableDataInfo *tdinfo = (const TableDataInfo *) dcontext;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	const char *classname = tbinfo->dobj.name;
 	PQExpBuffer q = createPQExpBuffer();
 
@@ -2547,8 +2547,8 @@ dumpTableData_copy(Archive *fout, const void *dcontext)
 static int
 dumpTableData_insert(Archive *fout, const void *dcontext)
 {
-	TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableDataInfo *tdinfo = (const TableDataInfo *) dcontext;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	DumpOptions *dopt = fout->dopt;
 	PQExpBuffer q = createPQExpBuffer();
 	PQExpBuffer insertStmt = NULL;
@@ -2618,7 +2618,7 @@ dumpTableData_insert(Archive *fout, const void *dcontext)
 		 */
 		if (insertStmt == NULL)
 		{
-			TableInfo  *targettab;
+			const TableInfo *targettab;
 
 			insertStmt = createPQExpBuffer();
 
@@ -2813,10 +2813,10 @@ dumpTableData_insert(Archive *fout, const void *dcontext)
  * getRootTableInfo:
  *     get the root TableInfo for the given partition table.
  */
-static TableInfo *
+static const TableInfo *
 getRootTableInfo(const TableInfo *tbinfo)
 {
-	TableInfo  *parentTbinfo;
+	const TableInfo *parentTbinfo;
 
 	Assert(tbinfo->ispartition);
 	Assert(tbinfo->numParents == 1);
@@ -2870,7 +2870,7 @@ static void
 dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
 {
 	DumpOptions *dopt = fout->dopt;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	PQExpBuffer copyBuf = createPQExpBuffer();
 	PQExpBuffer clistBuf = createPQExpBuffer();
 	DataDumperPtr dumpFn;
@@ -2891,7 +2891,7 @@ dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
 		(dopt->load_via_partition_root ||
 		 forcePartitionRootLoad(tbinfo)))
 	{
-		TableInfo  *parentTbinfo;
+		const TableInfo *parentTbinfo;
 		char	   *sanitized;
 
 		parentTbinfo = getRootTableInfo(tbinfo);
-- 
2.34.1

  [text/x-diff] v1-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch (1.7K, ../../aUQHy%2FMmWq7c97wK@ip-10-97-1-34.eu-west-3.compute.internal/4-v1-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch)
  download | inline diff:
From f32dd7b645090b85e1297f312c73353d93c83383 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 18 Dec 2025 12:17:21 +0000
Subject: [PATCH v1 3/3] Separate read and write pointers in pg_saslprep

Use separate pointers for reading const input ('p') and writing
to mutable output ('outp'), avoiding the need to cast away const
on the input parameter.
---
 src/common/saslprep.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
 100.0% src/common/

diff --git a/src/common/saslprep.c b/src/common/saslprep.c
index 101e8d65a4d..b215ab0bf8e 100644
--- a/src/common/saslprep.c
+++ b/src/common/saslprep.c
@@ -1054,7 +1054,8 @@ pg_saslprep(const char *input, char **output)
 	int			count;
 	int			i;
 	bool		contains_RandALCat;
-	unsigned char *p;
+	const unsigned char *p;
+	unsigned char *outp;
 	char32_t   *wp;
 
 	/* Ensure we return *output as NULL on failure */
@@ -1087,7 +1088,7 @@ pg_saslprep(const char *input, char **output)
 	if (!input_chars)
 		goto oom;
 
-	p = (unsigned char *) input;
+	p = (const unsigned char *) input;
 	for (i = 0; i < input_size; i++)
 	{
 		input_chars[i] = utf8_to_unicode(p);
@@ -1217,14 +1218,14 @@ pg_saslprep(const char *input, char **output)
 	 * There are no error exits below here, so the error exit paths don't need
 	 * to worry about possibly freeing "result".
 	 */
-	p = (unsigned char *) result;
+	outp = (unsigned char *) result;
 	for (wp = output_chars; *wp; wp++)
 	{
-		unicode_to_utf8(*wp, p);
-		p += pg_utf_mblen(p);
+		unicode_to_utf8(*wp, outp);
+		outp += pg_utf_mblen(outp);
 	}
-	Assert((char *) p == result + result_size);
-	*p = '\0';
+	Assert((char *) outp == result + result_size);
+	*outp = '\0';
 
 	FREE(input_chars);
 	FREE(output_chars);
-- 
2.34.1

^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2025-12-22 11:53  Peter Eisentraut <peter@eisentraut.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Peter Eisentraut @ 2025-12-22 11:53 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; pgsql-hackers@lists.postgresql.org

On 18.12.25 14:55, Bertrand Drouvot wrote:
> Some functions are casting away the const qualifiers from their signatures in
> local variables.

@@ -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;

I wonder if the better fix here wouldn't be to get rid of the cast. 
It's not necessary, and without it the compiler would automatically warn 
about qualifier mismatches.  These comparison functions seem to be a 
common pattern.






^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2025-12-29 09:01  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 3 replies; 13+ messages in thread

From: Bertrand Drouvot @ 2025-12-29 09:01 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; +Cc: pgsql-hackers@lists.postgresql.org

Hi,

On Mon, Dec 22, 2025 at 12:53:03PM +0100, Peter Eisentraut wrote:
> On 18.12.25 14:55, Bertrand Drouvot wrote:
> > Some functions are casting away the const qualifiers from their signatures in
> > local variables.
> 
> @@ -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;
> 
> I wonder if the better fix here wouldn't be to get rid of the cast. It's not
> necessary, and without it the compiler would automatically warn about
> qualifier mismatches.

Yeah, that looks better as it provides an extra safety check should the function
signature change.

> These comparison functions seem to be a common
> pattern.

Right, in the attached I applied your proposal on all those places.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

  [text/x-diff] v2-0001-Don-t-cast-away-const-where-possible.patch (5.9K, ../../aVJDegDFOPfaG87M@ip-10-97-1-34.eu-west-3.compute.internal/2-v2-0001-Don-t-cast-away-const-where-possible.patch)
  download | inline diff:
From aa228ca767d9f1cfd6785ea5387ea37db2a560c0 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 v2 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

Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal
---
 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.2% src/backend/access/brin/
  12.5% src/backend/access/heap/
  18.7% src/backend/access/spgist/
   8.3% src/backend/statistics/
  11.6% src/backend/tsearch/
  38.4% 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..0e87ce759f1 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 = a;
+	const DistanceValue *db = 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..92aab1d6723 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 = arg1;
+	const HeapTupleFreeze *frz2 = 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..c02de835847 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 = a;
+	const SortedPoint *pb = 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 = a;
+	const SortedPoint *pb = 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..dc577b9a1e4 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 = a;
+	const SortItem *ib = b;
 
 	if (ia->count == ib->count)
 		return 0;
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index e5badb6b43f..3b0abfd9c35 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 = f1;
+	const CompoundAffixFlag *fv2 = 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..12ea81a030c 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 = 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 = 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 = private_data;
 
 	if (inj_state == NULL)
 		injection_init_shmem();
-- 
2.34.1

  [text/x-diff] v2-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch (3.6K, ../../aVJDegDFOPfaG87M@ip-10-97-1-34.eu-west-3.compute.internal/3-v2-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch)
  download | inline diff:
From 3c8b291ddd9cce04850b36002befcaf5d9daee57 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 18 Dec 2025 12:47:07 +0000
Subject: [PATCH v2 2/3] Add const to read only TableInfo pointers in pg_dump

Functions that dump table data receive their parameters through const void *
but were casting away const. Add const qualifiers to functions that only read
the table information.

Also change getRootTableInfo to return const TableInfo *, since it only traverses
the parent chain without modifying any TableInfo structures. This allows the dump
functions to maintain const correctness when calling it.

Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal
---
 src/bin/pg_dump/pg_dump.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
 100.0% src/bin/pg_dump/

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 27f6be3f0f8..3f5e917ad99 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -412,7 +412,7 @@ static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
 static char *get_synchronized_snapshot(Archive *fout);
 static void set_restrict_relation_kind(Archive *AH, const char *value);
 static void setupDumpWorker(Archive *AH);
-static TableInfo *getRootTableInfo(const TableInfo *tbinfo);
+static const TableInfo *getRootTableInfo(const TableInfo *tbinfo);
 static bool forcePartitionRootLoad(const TableInfo *tbinfo);
 static void read_dump_filters(const char *filename, DumpOptions *dopt);
 
@@ -2379,8 +2379,8 @@ selectDumpableObject(DumpableObject *dobj, Archive *fout)
 static int
 dumpTableData_copy(Archive *fout, const void *dcontext)
 {
-	TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableDataInfo *tdinfo = dcontext;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	const char *classname = tbinfo->dobj.name;
 	PQExpBuffer q = createPQExpBuffer();
 
@@ -2547,8 +2547,8 @@ dumpTableData_copy(Archive *fout, const void *dcontext)
 static int
 dumpTableData_insert(Archive *fout, const void *dcontext)
 {
-	TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableDataInfo *tdinfo = dcontext;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	DumpOptions *dopt = fout->dopt;
 	PQExpBuffer q = createPQExpBuffer();
 	PQExpBuffer insertStmt = NULL;
@@ -2618,7 +2618,7 @@ dumpTableData_insert(Archive *fout, const void *dcontext)
 		 */
 		if (insertStmt == NULL)
 		{
-			TableInfo  *targettab;
+			const TableInfo *targettab;
 
 			insertStmt = createPQExpBuffer();
 
@@ -2813,10 +2813,10 @@ dumpTableData_insert(Archive *fout, const void *dcontext)
  * getRootTableInfo:
  *     get the root TableInfo for the given partition table.
  */
-static TableInfo *
+static const TableInfo *
 getRootTableInfo(const TableInfo *tbinfo)
 {
-	TableInfo  *parentTbinfo;
+	const TableInfo *parentTbinfo;
 
 	Assert(tbinfo->ispartition);
 	Assert(tbinfo->numParents == 1);
@@ -2870,7 +2870,7 @@ static void
 dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
 {
 	DumpOptions *dopt = fout->dopt;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	PQExpBuffer copyBuf = createPQExpBuffer();
 	PQExpBuffer clistBuf = createPQExpBuffer();
 	DataDumperPtr dumpFn;
@@ -2891,7 +2891,7 @@ dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
 		(dopt->load_via_partition_root ||
 		 forcePartitionRootLoad(tbinfo)))
 	{
-		TableInfo  *parentTbinfo;
+		const TableInfo *parentTbinfo;
 		char	   *sanitized;
 
 		parentTbinfo = getRootTableInfo(tbinfo);
-- 
2.34.1

  [text/x-diff] v2-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch (1.8K, ../../aVJDegDFOPfaG87M@ip-10-97-1-34.eu-west-3.compute.internal/4-v2-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch)
  download | inline diff:
From 4fa3127b65fc6237e6f63e5d5ad0e6486ae40a38 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 18 Dec 2025 12:17:21 +0000
Subject: [PATCH v2 3/3] Separate read and write pointers in pg_saslprep

Use separate pointers for reading const input ('p') and writing
to mutable output ('outp'), avoiding the need to cast away const
on the input parameter.

Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal
---
 src/common/saslprep.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
 100.0% src/common/

diff --git a/src/common/saslprep.c b/src/common/saslprep.c
index 101e8d65a4d..b215ab0bf8e 100644
--- a/src/common/saslprep.c
+++ b/src/common/saslprep.c
@@ -1054,7 +1054,8 @@ pg_saslprep(const char *input, char **output)
 	int			count;
 	int			i;
 	bool		contains_RandALCat;
-	unsigned char *p;
+	const unsigned char *p;
+	unsigned char *outp;
 	char32_t   *wp;
 
 	/* Ensure we return *output as NULL on failure */
@@ -1087,7 +1088,7 @@ pg_saslprep(const char *input, char **output)
 	if (!input_chars)
 		goto oom;
 
-	p = (unsigned char *) input;
+	p = (const unsigned char *) input;
 	for (i = 0; i < input_size; i++)
 	{
 		input_chars[i] = utf8_to_unicode(p);
@@ -1217,14 +1218,14 @@ pg_saslprep(const char *input, char **output)
 	 * There are no error exits below here, so the error exit paths don't need
 	 * to worry about possibly freeing "result".
 	 */
-	p = (unsigned char *) result;
+	outp = (unsigned char *) result;
 	for (wp = output_chars; *wp; wp++)
 	{
-		unicode_to_utf8(*wp, p);
-		p += pg_utf_mblen(p);
+		unicode_to_utf8(*wp, outp);
+		outp += pg_utf_mblen(outp);
 	}
-	Assert((char *) p == result + result_size);
-	*p = '\0';
+	Assert((char *) outp == result + result_size);
+	*outp = '\0';
 
 	FREE(input_chars);
 	FREE(output_chars);
-- 
2.34.1

^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2025-12-29 09:28  Chao Li <li.evan.chao@gmail.com>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  2 siblings, 0 replies; 13+ messages in thread

From: Chao Li @ 2025-12-29 09:28 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers@lists.postgresql.org



> On Dec 29, 2025, at 17:01, Bertrand Drouvot <bertranddrouvot.pg@gmail.com> wrote:
> 
> Hi,
> 
> On Mon, Dec 22, 2025 at 12:53:03PM +0100, Peter Eisentraut wrote:
>> On 18.12.25 14:55, Bertrand Drouvot wrote:
>>> Some functions are casting away the const qualifiers from their signatures in
>>> local variables.
>> 
>> @@ -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;
>> 
>> I wonder if the better fix here wouldn't be to get rid of the cast. It's not
>> necessary, and without it the compiler would automatically warn about
>> qualifier mismatches.
> 
> Yeah, that looks better as it provides an extra safety check should the function
> signature change.
> 
>> These comparison functions seem to be a common
>> pattern.
> 
> Right, in the attached I applied your proposal on all those places.
> 
> Regards,
> 
> -- 
> Bertrand Drouvot
> PostgreSQL Contributors Team
> RDS Open Source Databases
> Amazon Web Services: https://aws.amazon.com
> <v2-0001-Don-t-cast-away-const-where-possible.patch><v2-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch><v2-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch>


I have similar patch at https://docs.qq.com/sheet/DR0JRQ3lPVGtCWW5q?tab=000001&_t=1761030496005&nlc=1 doing the exact same thing in pg_dump_sort.c.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/









^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2025-12-31 08:42  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  2 siblings, 0 replies; 13+ messages in thread

From: Bertrand Drouvot @ 2025-12-31 08:42 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; +Cc: pgsql-hackers@lists.postgresql.org

Hi,

On Mon, Dec 29, 2025 at 09:01:46AM +0000, Bertrand Drouvot wrote:
> Hi,
> 
> On Mon, Dec 22, 2025 at 12:53:03PM +0100, Peter Eisentraut wrote:
> > On 18.12.25 14:55, Bertrand Drouvot wrote:
> > > Some functions are casting away the const qualifiers from their signatures in
> > > local variables.
> > 
> > @@ -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;
> > 
> > I wonder if the better fix here wouldn't be to get rid of the cast. It's not
> > necessary, and without it the compiler would automatically warn about
> > qualifier mismatches.
> 
> Yeah, that looks better as it provides an extra safety check should the function
> signature change.

Out of curiosity, I searched for places where we could remove explicit casts when
assigning from void pointers (relying on implicit conversion instead), that would
lead to:

"
 157 files changed, 387 insertions(+), 388 deletions(-)
"

That's not a small patch and I think that doing this work is valuable though.

We could imagine, working on say 20 files at a time and say once per month.
That would ease the review(s) and also avoid too many rebases for patches waiting
in the commitfest.

Thoughts?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-01-05 13:35  Peter Eisentraut <peter@eisentraut.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  2 siblings, 1 reply; 13+ messages in thread

From: Peter Eisentraut @ 2026-01-05 13:35 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org

On 29.12.25 10:01, Bertrand Drouvot wrote:
> On Mon, Dec 22, 2025 at 12:53:03PM +0100, Peter Eisentraut wrote:
>> On 18.12.25 14:55, Bertrand Drouvot wrote:
>>> Some functions are casting away the const qualifiers from their signatures in
>>> local variables.
>>
>> @@ -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;
>>
>> I wonder if the better fix here wouldn't be to get rid of the cast. It's not
>> necessary, and without it the compiler would automatically warn about
>> qualifier mismatches.
> 
> Yeah, that looks better as it provides an extra safety check should the function
> signature change.
> 
>> These comparison functions seem to be a common
>> pattern.
> 
> Right, in the attached I applied your proposal on all those places.

I have committed patch 0003 (pg_saslprep).

For patch 0002, I don't understand the change for getRootTableInfo(). 
It returns tbinfo->parents[0] (possibly some levels deep), but the 
parents field is not const-qualfied, so I don't understand the claim 
that this fixes anything.

For patch 0001, this seems good, but I wonder why your patch catches 
some cases and not some other similar ones.  For example, in 
src/backend/access/brin/brin_minmax_multi.c, you change 
compare_distances(), but not the very similar compare_expanded_ranges() 
and compare_values() nearby.






^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-01-05 16:18  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 13+ messages in thread

From: Bertrand Drouvot @ 2026-01-05 16:18 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; +Cc: pgsql-hackers@lists.postgresql.org

Hi,

On Mon, Jan 05, 2026 at 02:35:43PM +0100, Peter Eisentraut wrote:
> On 29.12.25 10:01, Bertrand Drouvot wrote:
> > Right, in the attached I applied your proposal on all those places.
> 
> I have committed patch 0003 (pg_saslprep).

Thanks!

> For patch 0002, I don't understand the change for getRootTableInfo(). It
> returns tbinfo->parents[0] (possibly some levels deep), but the parents
> field is not const-qualfied, so I don't understand the claim that this fixes
> anything.

You're right, the function doesn't modify anything that its argument's pointer
members point to. If it did, that would be misleading to accept a const parameter
while modifying any of its non const pointer members data. getRootTableInfo()
is not one of those cases so PFA a new version without the getRootTableInfo()
related changes.

> 
> For patch 0001, this seems good, but I wonder why your patch catches some
> cases and not some other similar ones.  For example, in
> src/backend/access/brin/brin_minmax_multi.c, you change compare_distances(),
> but not the very similar compare_expanded_ranges() and compare_values()
> nearby.

The initial patch was filtering out more complex functions that would need more
study. The idea was to look at those later on.

Now, about compare_expanded_ranges() and compare_values(), that's right that
those functions have similar patterns and could be included and their "extra"
study is simple as realizing that minval and maxval are Datum (so uint64_t),
are pass by values to FunctionCall2Coll() so that it can not modify them.

So, better to be consistent within the same file, those 2 functions have been
added in the attached.

Also I've added the changes for sort_item_compare() even this is a thin wrapper
so that the changes are consistent accross the mcv.c file too.

Now all the remaining ones reported by [1] are in files not touched by the attached,
making it consistent on a per file basis.

Note that it does not take care at all of "nearby" places where we could remove
explicit casts when assigning from void pointers (for example the arg parameter
in compare_expanded_ranges() and compare_values()) as I think that could be
worth a dedicated project as stated in [2].

[1]: https://github.com/bdrouvot/coccinelle_on_pg/blob/main/misc/search_const_away.cocci
[2]: https://www.postgresql.org/message-id/aVTiCHBalaFCneYD%40ip-10-97-1-34.eu-west-3.compute.internal

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

  [text/x-diff] v3-0001-Don-t-cast-away-const-where-possible.patch (7.0K, ../../aVvkR7DJNBNbco+6@ip-10-97-1-34.eu-west-3.compute.internal/2-v3-0001-Don-t-cast-away-const-where-possible.patch)
  download | inline diff:
From 541be8414a50520cd370e5aa546a4532f81bc3f7 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 v3 1/2] 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 (except when there is other changes in the same file)
- would require public API changes
- rely on external functions (such as LZ4F_compressUpdate())
- would require complex subsystem changes

Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal
---
 src/backend/access/brin/brin_minmax_multi.c          | 12 ++++++------
 src/backend/access/heap/pruneheap.c                  |  4 ++--
 src/backend/access/spgist/spgkdtreeproc.c            |  8 ++++----
 src/backend/statistics/mcv.c                         |  8 ++++----
 src/backend/tsearch/spell.c                          |  4 ++--
 src/test/modules/injection_points/injection_points.c |  8 ++++----
 6 files changed, 22 insertions(+), 22 deletions(-)
  22.0% src/backend/access/brin/
   9.9% src/backend/access/heap/
  14.9% src/backend/access/spgist/
  13.3% src/backend/statistics/
   9.2% src/backend/tsearch/
  30.5% 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 6b86b1fd889..75dd8d5083b 100644
--- a/src/backend/access/brin/brin_minmax_multi.c
+++ b/src/backend/access/brin/brin_minmax_multi.c
@@ -857,8 +857,8 @@ brin_range_deserialize(int maxvalues, SerializedRanges *serialized)
 static int
 compare_expanded_ranges(const void *a, const void *b, void *arg)
 {
-	ExpandedRange *ra = (ExpandedRange *) a;
-	ExpandedRange *rb = (ExpandedRange *) b;
+	const ExpandedRange *ra = a;
+	const ExpandedRange *rb = b;
 	Datum		r;
 
 	compare_context *cxt = (compare_context *) arg;
@@ -895,8 +895,8 @@ compare_expanded_ranges(const void *a, const void *b, void *arg)
 static int
 compare_values(const void *a, const void *b, void *arg)
 {
-	Datum	   *da = (Datum *) a;
-	Datum	   *db = (Datum *) b;
+	const Datum *da = a;
+	const Datum *db = b;
 	Datum		r;
 
 	compare_context *cxt = (compare_context *) arg;
@@ -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 = a;
+	const DistanceValue *db = 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 af788b29714..632c2427952 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 = arg1;
+	const HeapTupleFreeze *frz2 = 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 6acda3fdcff..1ec0a4f59f3 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 = a;
+	const SortedPoint *pb = 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 = a;
+	const SortedPoint *pb = 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 390ad83497a..9749871b18e 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 = a;
+	const SortItem *ib = b;
 
 	if (ia->count == ib->count)
 		return 0;
@@ -465,8 +465,8 @@ static int
 sort_item_compare(const void *a, const void *b, void *arg)
 {
 	SortSupport ssup = (SortSupport) arg;
-	SortItem   *ia = (SortItem *) a;
-	SortItem   *ib = (SortItem *) b;
+	const SortItem *ia = a;
+	const SortItem *ib = b;
 
 	return ApplySortComparator(ia->values[0], ia->isnull[0],
 							   ib->values[0], ib->isnull[0],
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index 2e96c86b0b3..e3436dbddd2 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 = f1;
+	const CompoundAffixFlag *fv2 = 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 9c3806f5958..17d95caf164 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 = 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 = 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 = private_data;
 
 	if (inj_state == NULL)
 		injection_init_shmem();
-- 
2.34.1

  [text/x-diff] v3-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch (2.5K, ../../aVvkR7DJNBNbco+6@ip-10-97-1-34.eu-west-3.compute.internal/3-v3-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch)
  download | inline diff:
From ffde17e8b053209a17b6776f58e994b90483ba8e Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 18 Dec 2025 12:47:07 +0000
Subject: [PATCH v3 2/2] Add const to read only TableInfo pointers in pg_dump

Functions that dump table data receive their parameters through const void *
but were casting away const. Add const qualifiers to functions that only read
the table information.

Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal
---
 src/bin/pg_dump/pg_dump.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
 100.0% src/bin/pg_dump/

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7df56d8b1b0..e3b4035e57a 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2379,8 +2379,8 @@ selectDumpableObject(DumpableObject *dobj, Archive *fout)
 static int
 dumpTableData_copy(Archive *fout, const void *dcontext)
 {
-	TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableDataInfo *tdinfo = dcontext;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	const char *classname = tbinfo->dobj.name;
 	PQExpBuffer q = createPQExpBuffer();
 
@@ -2547,8 +2547,8 @@ dumpTableData_copy(Archive *fout, const void *dcontext)
 static int
 dumpTableData_insert(Archive *fout, const void *dcontext)
 {
-	TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableDataInfo *tdinfo = dcontext;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	DumpOptions *dopt = fout->dopt;
 	PQExpBuffer q = createPQExpBuffer();
 	PQExpBuffer insertStmt = NULL;
@@ -2618,7 +2618,7 @@ dumpTableData_insert(Archive *fout, const void *dcontext)
 		 */
 		if (insertStmt == NULL)
 		{
-			TableInfo  *targettab;
+			const TableInfo *targettab;
 
 			insertStmt = createPQExpBuffer();
 
@@ -2870,7 +2870,7 @@ static void
 dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
 {
 	DumpOptions *dopt = fout->dopt;
-	TableInfo  *tbinfo = tdinfo->tdtable;
+	const TableInfo *tbinfo = tdinfo->tdtable;
 	PQExpBuffer copyBuf = createPQExpBuffer();
 	PQExpBuffer clistBuf = createPQExpBuffer();
 	DataDumperPtr dumpFn;
@@ -2891,7 +2891,7 @@ dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
 		(dopt->load_via_partition_root ||
 		 forcePartitionRootLoad(tbinfo)))
 	{
-		TableInfo  *parentTbinfo;
+		const TableInfo *parentTbinfo;
 		char	   *sanitized;
 
 		parentTbinfo = getRootTableInfo(tbinfo);
-- 
2.34.1

^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-01-12 15:44  Peter Eisentraut <peter@eisentraut.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Peter Eisentraut @ 2026-01-12 15:44 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org

On 05.01.26 17:18, Bertrand Drouvot wrote:
>> For patch 0001, this seems good, but I wonder why your patch catches some
>> cases and not some other similar ones.  For example, in
>> src/backend/access/brin/brin_minmax_multi.c, you change compare_distances(),
>> but not the very similar compare_expanded_ranges() and compare_values()
>> nearby.
> The initial patch was filtering out more complex functions that would need more
> study. The idea was to look at those later on.
> 
> Now, about compare_expanded_ranges() and compare_values(), that's right that
> those functions have similar patterns and could be included and their "extra"
> study is simple as realizing that minval and maxval are Datum (so uint64_t),
> are pass by values to FunctionCall2Coll() so that it can not modify them.
> 
> So, better to be consistent within the same file, those 2 functions have been
> added in the attached.
> 
> Also I've added the changes for sort_item_compare() even this is a thin wrapper
> so that the changes are consistent accross the mcv.c file too.
> 
> Now all the remaining ones reported by [1] are in files not touched by the attached,
> making it consistent on a per file basis.
> 
> Note that it does not take care at all of "nearby" places where we could remove
> explicit casts when assigning from void pointers (for example the arg parameter
> in compare_expanded_ranges() and compare_values()) as I think that could be
> worth a dedicated project as stated in [2].

I have committed the remaining two patches.

I had in my own work parked a few changes that were similar to the ones 
in your patch 0001, so I threw them in there as well.





^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-02-23 07:24  Peter Eisentraut <peter@eisentraut.org>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 13+ messages in thread

From: Peter Eisentraut @ 2026-02-23 07:24 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org

I have another patch that removes some -Wcast-qual warnings, by 
adjusting some function APIs.

This is as far as I get without resorting to advanced tricks like 
unconstify() or _Generic.
From de207dfe45c9944de5006bf347011794ec94a90c Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 20 Jan 2026 08:45:50 +0100
Subject: [PATCH] Fix -Wcast-qual warnings, easy without compiler tricks

---
 src/backend/access/gin/ginbulk.c        |  2 +-
 src/backend/postmaster/launch_backend.c | 12 ++++++------
 src/backend/utils/error/elog.c          |  7 +++++--
 src/common/jsonapi.c                    |  6 +++---
 src/fe_utils/astreamer_lz4.c            | 12 ++++++------
 src/include/access/gin_private.h        |  2 +-
 src/include/port/pg_lfind.h             |  4 ++--
 src/include/postmaster/postmaster.h     |  2 +-
 src/interfaces/ecpg/preproc/parser.c    | 17 +++++++++++------
 9 files changed, 36 insertions(+), 28 deletions(-)

diff --git a/src/backend/access/gin/ginbulk.c b/src/backend/access/gin/ginbulk.c
index 839d7d31166..85865b39105 100644
--- a/src/backend/access/gin/ginbulk.c
+++ b/src/backend/access/gin/ginbulk.c
@@ -244,7 +244,7 @@ ginInsertBAEntries(BuildAccumulator *accum,
 static int
 qsortCompareItemPointers(const void *a, const void *b)
 {
-	int			res = ginCompareItemPointers((ItemPointer) a, (ItemPointer) b);
+	int			res = ginCompareItemPointers((const ItemPointerData *) a, (const ItemPointerData *) b);
 
 	/* Assert that there are no equal item pointers being sorted */
 	Assert(res != 0);
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index e9134b9751b..30357845729 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -154,7 +154,7 @@ static void read_backend_variables(char *id, void **startup_data, size_t *startu
 static void restore_backend_variables(BackendParameters *param);
 
 static bool save_backend_variables(BackendParameters *param, int child_slot,
-								   ClientSocket *client_sock,
+								   const ClientSocket *client_sock,
 #ifdef WIN32
 								   HANDLE childProcess, pid_t childPid,
 #endif
@@ -162,7 +162,7 @@ static bool save_backend_variables(BackendParameters *param, int child_slot,
 
 static pid_t internal_forkexec(BackendType child_kind, int child_slot,
 							   const void *startup_data, size_t startup_data_len,
-							   ClientSocket *client_sock);
+							   const ClientSocket *client_sock);
 
 #endif							/* EXEC_BACKEND */
 
@@ -204,7 +204,7 @@ PostmasterChildName(BackendType child_type)
 pid_t
 postmaster_child_launch(BackendType child_type, int child_slot,
 						void *startup_data, size_t startup_data_len,
-						ClientSocket *client_sock)
+						const ClientSocket *client_sock)
 {
 	pid_t		pid;
 
@@ -283,7 +283,7 @@ postmaster_child_launch(BackendType child_type, int child_slot,
  */
 static pid_t
 internal_forkexec(BackendType child_kind, int child_slot,
-				  const void *startup_data, size_t startup_data_len, ClientSocket *client_sock)
+				  const void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
 {
 	static unsigned long tmpBackendFileNum = 0;
 	pid_t		pid;
@@ -393,7 +393,7 @@ internal_forkexec(BackendType child_kind, int child_slot,
  */
 static pid_t
 internal_forkexec(BackendType child_kind, int child_slot,
-				  const void *startup_data, size_t startup_data_len, ClientSocket *client_sock)
+				  const void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
 {
 	int			retry_count = 0;
 	STARTUPINFO si;
@@ -700,7 +700,7 @@ static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
 /* Save critical backend variables into the BackendParameters struct */
 static bool
 save_backend_variables(BackendParameters *param,
-					   int child_slot, ClientSocket *client_sock,
+					   int child_slot, const ClientSocket *client_sock,
 #ifdef WIN32
 					   HANDLE childProcess, pid_t childPid,
 #endif
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index cb1c9d85ffe..6198d736e50 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2755,7 +2755,6 @@ GetACPEncoding(void)
 static void
 write_eventlog(int level, const char *line, int len)
 {
-	WCHAR	   *utf16;
 	int			eventlevel = EVENTLOG_ERROR_TYPE;
 	static HANDLE evtHandle = INVALID_HANDLE_VALUE;
 
@@ -2812,9 +2811,13 @@ write_eventlog(int level, const char *line, int len)
 		CurrentMemoryContext != NULL &&
 		GetMessageEncoding() != GetACPEncoding())
 	{
+		WCHAR	   *utf16;
+
 		utf16 = pgwin32_message_to_UTF16(line, len, NULL);
 		if (utf16)
 		{
+			const WCHAR *utf16_const = utf16;
+
 			ReportEventW(evtHandle,
 						 eventlevel,
 						 0,
@@ -2822,7 +2825,7 @@ write_eventlog(int level, const char *line, int len)
 						 NULL,
 						 1,
 						 0,
-						 (LPCWSTR *) &utf16,
+						 &utf16_const,
 						 NULL);
 			/* XXX Try ReportEventA() when ReportEventW() fails? */
 
diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c
index 265698e55bf..1145d93945f 100644
--- a/src/common/jsonapi.c
+++ b/src/common/jsonapi.c
@@ -2167,9 +2167,9 @@ json_lex_string(JsonLexContext *lex)
 			 * can batch calls to jsonapi_appendBinaryStringInfo.
 			 */
 			while (p < end - sizeof(Vector8) &&
-				   !pg_lfind8('\\', (uint8 *) p, sizeof(Vector8)) &&
-				   !pg_lfind8('"', (uint8 *) p, sizeof(Vector8)) &&
-				   !pg_lfind8_le(31, (uint8 *) p, sizeof(Vector8)))
+				   !pg_lfind8('\\', (const uint8 *) p, sizeof(Vector8)) &&
+				   !pg_lfind8('"', (const uint8 *) p, sizeof(Vector8)) &&
+				   !pg_lfind8_le(31, (const uint8 *) p, sizeof(Vector8)))
 				p += sizeof(Vector8);
 
 			for (; p < end; p++)
diff --git a/src/fe_utils/astreamer_lz4.c b/src/fe_utils/astreamer_lz4.c
index bcee7e088de..a5865c93598 100644
--- a/src/fe_utils/astreamer_lz4.c
+++ b/src/fe_utils/astreamer_lz4.c
@@ -121,14 +121,14 @@ astreamer_lz4_compressor_content(astreamer *streamer,
 								 astreamer_archive_context context)
 {
 	astreamer_lz4_frame *mystreamer;
-	uint8	   *next_in,
-			   *next_out;
+	const uint8 *next_in;
+	uint8	   *next_out;
 	size_t		out_bound,
 				compressed_size,
 				avail_out;
 
 	mystreamer = (astreamer_lz4_frame *) streamer;
-	next_in = (uint8 *) data;
+	next_in = (const uint8 *) data;
 
 	/* Write header before processing the first input chunk. */
 	if (!mystreamer->header_written)
@@ -315,13 +315,13 @@ astreamer_lz4_decompressor_content(astreamer *streamer,
 								   astreamer_archive_context context)
 {
 	astreamer_lz4_frame *mystreamer;
-	uint8	   *next_in,
-			   *next_out;
+	const uint8 *next_in;
+	uint8	   *next_out;
 	size_t		avail_in,
 				avail_out;
 
 	mystreamer = (astreamer_lz4_frame *) streamer;
-	next_in = (uint8 *) data;
+	next_in = (const uint8 *) data;
 	next_out = (uint8 *) mystreamer->base.bbs_buffer.data + mystreamer->bytes_written;
 	avail_in = len;
 	avail_out = mystreamer->base.bbs_buffer.maxlen - mystreamer->bytes_written;
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index e155045ce8a..7c3b4db94cd 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -488,7 +488,7 @@ extern ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na,
  * so we want this to be inlined.
  */
 static inline int
-ginCompareItemPointers(ItemPointer a, ItemPointer b)
+ginCompareItemPointers(const ItemPointerData *a, const ItemPointerData *b)
 {
 	uint64		ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
 	uint64		ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
diff --git a/src/include/port/pg_lfind.h b/src/include/port/pg_lfind.h
index 05ac3f61247..18856c7a8c9 100644
--- a/src/include/port/pg_lfind.h
+++ b/src/include/port/pg_lfind.h
@@ -23,7 +23,7 @@
  * return false.
  */
 static inline bool
-pg_lfind8(uint8 key, uint8 *base, uint32 nelem)
+pg_lfind8(uint8 key, const uint8 *base, uint32 nelem)
 {
 	uint32		i;
 
@@ -55,7 +55,7 @@ pg_lfind8(uint8 key, uint8 *base, uint32 nelem)
  * 'key', otherwise return false.
  */
 static inline bool
-pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
+pg_lfind8_le(uint8 key, const uint8 *base, uint32 nelem)
 {
 	uint32		i;
 
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index d6ab9ee2d96..716b4c912b3 100644
--- a/src/include/postmaster/postmaster.h
+++ b/src/include/postmaster/postmaster.h
@@ -110,7 +110,7 @@ extern pid_t postmaster_child_launch(BackendType child_type,
 									 int child_slot,
 									 void *startup_data,
 									 size_t startup_data_len,
-									 struct ClientSocket *client_sock);
+									 const struct ClientSocket *client_sock);
 const char *PostmasterChildName(BackendType child_type);
 #ifdef EXEC_BACKEND
 pg_noreturn extern void SubPostmasterMain(int argc, char *argv[]);
diff --git a/src/interfaces/ecpg/preproc/parser.c b/src/interfaces/ecpg/preproc/parser.c
index ea256dd3ac7..498f599aeed 100644
--- a/src/interfaces/ecpg/preproc/parser.c
+++ b/src/interfaces/ecpg/preproc/parser.c
@@ -260,14 +260,19 @@ base_yylex_location(void)
 			break;
 		default:
 			/* Else just use the input, i.e., yytext */
-			base_yylloc = loc_strdup(base_yytext);
-			/* Apply an ASCII-only downcasing */
-			for (unsigned char *ptr = (unsigned char *) base_yylloc; *ptr; ptr++)
 			{
-				if (*ptr >= 'A' && *ptr <= 'Z')
-					*ptr += 'a' - 'A';
+				char *tmp;
+
+				tmp = loc_strdup(base_yytext);
+				/* Apply an ASCII-only downcasing */
+				for (unsigned char *ptr = (unsigned char *) tmp; *ptr; ptr++)
+				{
+					if (*ptr >= 'A' && *ptr <= 'Z')
+						*ptr += 'a' - 'A';
+				}
+				base_yylloc = tmp;
+				break;
 			}
-			break;
 	}
 	return token;
 }
-- 
2.53.0



Attachments:

  [text/plain] 0001-Fix-Wcast-qual-warnings-easy-without-compiler-tricks.patch (9.4K, ../../990c9117-b013-4026-aaf5-261fe2832c3d@eisentraut.org/2-0001-Fix-Wcast-qual-warnings-easy-without-compiler-tricks.patch)
  download | inline diff:
From de207dfe45c9944de5006bf347011794ec94a90c Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 20 Jan 2026 08:45:50 +0100
Subject: [PATCH] Fix -Wcast-qual warnings, easy without compiler tricks

---
 src/backend/access/gin/ginbulk.c        |  2 +-
 src/backend/postmaster/launch_backend.c | 12 ++++++------
 src/backend/utils/error/elog.c          |  7 +++++--
 src/common/jsonapi.c                    |  6 +++---
 src/fe_utils/astreamer_lz4.c            | 12 ++++++------
 src/include/access/gin_private.h        |  2 +-
 src/include/port/pg_lfind.h             |  4 ++--
 src/include/postmaster/postmaster.h     |  2 +-
 src/interfaces/ecpg/preproc/parser.c    | 17 +++++++++++------
 9 files changed, 36 insertions(+), 28 deletions(-)

diff --git a/src/backend/access/gin/ginbulk.c b/src/backend/access/gin/ginbulk.c
index 839d7d31166..85865b39105 100644
--- a/src/backend/access/gin/ginbulk.c
+++ b/src/backend/access/gin/ginbulk.c
@@ -244,7 +244,7 @@ ginInsertBAEntries(BuildAccumulator *accum,
 static int
 qsortCompareItemPointers(const void *a, const void *b)
 {
-	int			res = ginCompareItemPointers((ItemPointer) a, (ItemPointer) b);
+	int			res = ginCompareItemPointers((const ItemPointerData *) a, (const ItemPointerData *) b);
 
 	/* Assert that there are no equal item pointers being sorted */
 	Assert(res != 0);
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index e9134b9751b..30357845729 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -154,7 +154,7 @@ static void read_backend_variables(char *id, void **startup_data, size_t *startu
 static void restore_backend_variables(BackendParameters *param);
 
 static bool save_backend_variables(BackendParameters *param, int child_slot,
-								   ClientSocket *client_sock,
+								   const ClientSocket *client_sock,
 #ifdef WIN32
 								   HANDLE childProcess, pid_t childPid,
 #endif
@@ -162,7 +162,7 @@ static bool save_backend_variables(BackendParameters *param, int child_slot,
 
 static pid_t internal_forkexec(BackendType child_kind, int child_slot,
 							   const void *startup_data, size_t startup_data_len,
-							   ClientSocket *client_sock);
+							   const ClientSocket *client_sock);
 
 #endif							/* EXEC_BACKEND */
 
@@ -204,7 +204,7 @@ PostmasterChildName(BackendType child_type)
 pid_t
 postmaster_child_launch(BackendType child_type, int child_slot,
 						void *startup_data, size_t startup_data_len,
-						ClientSocket *client_sock)
+						const ClientSocket *client_sock)
 {
 	pid_t		pid;
 
@@ -283,7 +283,7 @@ postmaster_child_launch(BackendType child_type, int child_slot,
  */
 static pid_t
 internal_forkexec(BackendType child_kind, int child_slot,
-				  const void *startup_data, size_t startup_data_len, ClientSocket *client_sock)
+				  const void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
 {
 	static unsigned long tmpBackendFileNum = 0;
 	pid_t		pid;
@@ -393,7 +393,7 @@ internal_forkexec(BackendType child_kind, int child_slot,
  */
 static pid_t
 internal_forkexec(BackendType child_kind, int child_slot,
-				  const void *startup_data, size_t startup_data_len, ClientSocket *client_sock)
+				  const void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
 {
 	int			retry_count = 0;
 	STARTUPINFO si;
@@ -700,7 +700,7 @@ static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
 /* Save critical backend variables into the BackendParameters struct */
 static bool
 save_backend_variables(BackendParameters *param,
-					   int child_slot, ClientSocket *client_sock,
+					   int child_slot, const ClientSocket *client_sock,
 #ifdef WIN32
 					   HANDLE childProcess, pid_t childPid,
 #endif
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index cb1c9d85ffe..6198d736e50 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2755,7 +2755,6 @@ GetACPEncoding(void)
 static void
 write_eventlog(int level, const char *line, int len)
 {
-	WCHAR	   *utf16;
 	int			eventlevel = EVENTLOG_ERROR_TYPE;
 	static HANDLE evtHandle = INVALID_HANDLE_VALUE;
 
@@ -2812,9 +2811,13 @@ write_eventlog(int level, const char *line, int len)
 		CurrentMemoryContext != NULL &&
 		GetMessageEncoding() != GetACPEncoding())
 	{
+		WCHAR	   *utf16;
+
 		utf16 = pgwin32_message_to_UTF16(line, len, NULL);
 		if (utf16)
 		{
+			const WCHAR *utf16_const = utf16;
+
 			ReportEventW(evtHandle,
 						 eventlevel,
 						 0,
@@ -2822,7 +2825,7 @@ write_eventlog(int level, const char *line, int len)
 						 NULL,
 						 1,
 						 0,
-						 (LPCWSTR *) &utf16,
+						 &utf16_const,
 						 NULL);
 			/* XXX Try ReportEventA() when ReportEventW() fails? */
 
diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c
index 265698e55bf..1145d93945f 100644
--- a/src/common/jsonapi.c
+++ b/src/common/jsonapi.c
@@ -2167,9 +2167,9 @@ json_lex_string(JsonLexContext *lex)
 			 * can batch calls to jsonapi_appendBinaryStringInfo.
 			 */
 			while (p < end - sizeof(Vector8) &&
-				   !pg_lfind8('\\', (uint8 *) p, sizeof(Vector8)) &&
-				   !pg_lfind8('"', (uint8 *) p, sizeof(Vector8)) &&
-				   !pg_lfind8_le(31, (uint8 *) p, sizeof(Vector8)))
+				   !pg_lfind8('\\', (const uint8 *) p, sizeof(Vector8)) &&
+				   !pg_lfind8('"', (const uint8 *) p, sizeof(Vector8)) &&
+				   !pg_lfind8_le(31, (const uint8 *) p, sizeof(Vector8)))
 				p += sizeof(Vector8);
 
 			for (; p < end; p++)
diff --git a/src/fe_utils/astreamer_lz4.c b/src/fe_utils/astreamer_lz4.c
index bcee7e088de..a5865c93598 100644
--- a/src/fe_utils/astreamer_lz4.c
+++ b/src/fe_utils/astreamer_lz4.c
@@ -121,14 +121,14 @@ astreamer_lz4_compressor_content(astreamer *streamer,
 								 astreamer_archive_context context)
 {
 	astreamer_lz4_frame *mystreamer;
-	uint8	   *next_in,
-			   *next_out;
+	const uint8 *next_in;
+	uint8	   *next_out;
 	size_t		out_bound,
 				compressed_size,
 				avail_out;
 
 	mystreamer = (astreamer_lz4_frame *) streamer;
-	next_in = (uint8 *) data;
+	next_in = (const uint8 *) data;
 
 	/* Write header before processing the first input chunk. */
 	if (!mystreamer->header_written)
@@ -315,13 +315,13 @@ astreamer_lz4_decompressor_content(astreamer *streamer,
 								   astreamer_archive_context context)
 {
 	astreamer_lz4_frame *mystreamer;
-	uint8	   *next_in,
-			   *next_out;
+	const uint8 *next_in;
+	uint8	   *next_out;
 	size_t		avail_in,
 				avail_out;
 
 	mystreamer = (astreamer_lz4_frame *) streamer;
-	next_in = (uint8 *) data;
+	next_in = (const uint8 *) data;
 	next_out = (uint8 *) mystreamer->base.bbs_buffer.data + mystreamer->bytes_written;
 	avail_in = len;
 	avail_out = mystreamer->base.bbs_buffer.maxlen - mystreamer->bytes_written;
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index e155045ce8a..7c3b4db94cd 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -488,7 +488,7 @@ extern ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na,
  * so we want this to be inlined.
  */
 static inline int
-ginCompareItemPointers(ItemPointer a, ItemPointer b)
+ginCompareItemPointers(const ItemPointerData *a, const ItemPointerData *b)
 {
 	uint64		ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
 	uint64		ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
diff --git a/src/include/port/pg_lfind.h b/src/include/port/pg_lfind.h
index 05ac3f61247..18856c7a8c9 100644
--- a/src/include/port/pg_lfind.h
+++ b/src/include/port/pg_lfind.h
@@ -23,7 +23,7 @@
  * return false.
  */
 static inline bool
-pg_lfind8(uint8 key, uint8 *base, uint32 nelem)
+pg_lfind8(uint8 key, const uint8 *base, uint32 nelem)
 {
 	uint32		i;
 
@@ -55,7 +55,7 @@ pg_lfind8(uint8 key, uint8 *base, uint32 nelem)
  * 'key', otherwise return false.
  */
 static inline bool
-pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
+pg_lfind8_le(uint8 key, const uint8 *base, uint32 nelem)
 {
 	uint32		i;
 
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index d6ab9ee2d96..716b4c912b3 100644
--- a/src/include/postmaster/postmaster.h
+++ b/src/include/postmaster/postmaster.h
@@ -110,7 +110,7 @@ extern pid_t postmaster_child_launch(BackendType child_type,
 									 int child_slot,
 									 void *startup_data,
 									 size_t startup_data_len,
-									 struct ClientSocket *client_sock);
+									 const struct ClientSocket *client_sock);
 const char *PostmasterChildName(BackendType child_type);
 #ifdef EXEC_BACKEND
 pg_noreturn extern void SubPostmasterMain(int argc, char *argv[]);
diff --git a/src/interfaces/ecpg/preproc/parser.c b/src/interfaces/ecpg/preproc/parser.c
index ea256dd3ac7..498f599aeed 100644
--- a/src/interfaces/ecpg/preproc/parser.c
+++ b/src/interfaces/ecpg/preproc/parser.c
@@ -260,14 +260,19 @@ base_yylex_location(void)
 			break;
 		default:
 			/* Else just use the input, i.e., yytext */
-			base_yylloc = loc_strdup(base_yytext);
-			/* Apply an ASCII-only downcasing */
-			for (unsigned char *ptr = (unsigned char *) base_yylloc; *ptr; ptr++)
 			{
-				if (*ptr >= 'A' && *ptr <= 'Z')
-					*ptr += 'a' - 'A';
+				char *tmp;
+
+				tmp = loc_strdup(base_yytext);
+				/* Apply an ASCII-only downcasing */
+				for (unsigned char *ptr = (unsigned char *) tmp; *ptr; ptr++)
+				{
+					if (*ptr >= 'A' && *ptr <= 'Z')
+						*ptr += 'a' - 'A';
+				}
+				base_yylloc = tmp;
+				break;
 			}
-			break;
 	}
 	return token;
 }
-- 
2.53.0



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-02-23 11:22  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 13+ messages in thread

From: Bertrand Drouvot @ 2026-02-23 11:22 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; +Cc: pgsql-hackers@lists.postgresql.org

Hi,

On Mon, Feb 23, 2026 at 08:24:22AM +0100, Peter Eisentraut wrote:
> I have another patch that removes some -Wcast-qual warnings, by adjusting
> some function APIs.
> 
> This is as far as I get without resorting to advanced tricks like
> unconstify() or _Generic.

Yeah, so the patch removes those:

jsonapi.c:2170:53: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2171:52: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2172:54: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2170:53: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2171:52: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2172:54: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2170:53: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2171:52: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
jsonapi.c:2172:54: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
ginbulk.c:247:62: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
ginbulk.c:247:79: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
parser.c:265:51: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]

and looks ok.

I can see a "volatile" one though:

vacuum.c:1885:46: warning: cast discards ‘volatile’ qualifier from pointer target type [-Wcast-qual]

worth to "fix" like in the attached while at it? (It's not really a fix as it
moves the warning from vacuum.c to c.h, but that's consistent with 481018f2804).

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
commit b2fe0f92698cad74cd072a254d3c943b6f51d43d
Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date:   Mon Feb 23 09:30:25 2026 +0000

    Make use of unvolatize() in vac_truncate_clog()
    
    481018f2804 introduced unvolatize() but c66a7d75e652 forgot to make use of
    it.
    
    Reported by -Wcast-qual. Note that that does not remove the warning (481018f2804
    also did not remove them), but move it from vacuum.c:1885 to c.h:1263.

diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 03932f45c8a..edef3396aae 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -1882,7 +1882,7 @@ vac_truncate_clog(TransactionId frozenXID,
 		 * anymore. Therefore we don't need to take it into account here.
 		 * Which is good, because it can't be processed by autovacuum either.
 		 */
-		if (database_is_invalid_form((Form_pg_database) dbform))
+		if (database_is_invalid_form(unvolatize(FormData_pg_database *, dbform)))
 		{
 			elog(DEBUG2,
 				 "skipping invalid database \"%s\" while computing relfrozenxid",

Attachments:

  [text/plain] unvolatize.txt (1.1K, ../../aZw4fcj1qBYgN41V@ip-10-97-1-34.eu-west-3.compute.internal/2-unvolatize.txt)
  download | inline diff:
commit b2fe0f92698cad74cd072a254d3c943b6f51d43d
Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date:   Mon Feb 23 09:30:25 2026 +0000

    Make use of unvolatize() in vac_truncate_clog()
    
    481018f2804 introduced unvolatize() but c66a7d75e652 forgot to make use of
    it.
    
    Reported by -Wcast-qual. Note that that does not remove the warning (481018f2804
    also did not remove them), but move it from vacuum.c:1885 to c.h:1263.

diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 03932f45c8a..edef3396aae 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -1882,7 +1882,7 @@ vac_truncate_clog(TransactionId frozenXID,
 		 * anymore. Therefore we don't need to take it into account here.
 		 * Which is good, because it can't be processed by autovacuum either.
 		 */
-		if (database_is_invalid_form((Form_pg_database) dbform))
+		if (database_is_invalid_form(unvolatize(FormData_pg_database *, dbform)))
 		{
 			elog(DEBUG2,
 				 "skipping invalid database \"%s\" while computing relfrozenxid",

^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-02-24 16:05  Peter Eisentraut <peter@eisentraut.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Peter Eisentraut @ 2026-02-24 16:05 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org

On 23.02.26 12:22, Bertrand Drouvot wrote:
> I can see a "volatile" one though:
> 
> vacuum.c:1885:46: warning: cast discards ‘volatile’ qualifier from pointer target type [-Wcast-qual]
> 
> worth to "fix" like in the attached while at it? (It's not really a fix as it
> moves the warning from vacuum.c to c.h, but that's consistent with 481018f2804).

That fix looks correct.  But my patch says it's making changes only 
without use unconstify and the like.  To fix all -Wcast-qual warnings, 
you'd need more changes like that, which I'm not currently proposing.






^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Don't cast away const where possible
@ 2026-02-24 16:33  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 0 replies; 13+ messages in thread

From: Bertrand Drouvot @ 2026-02-24 16:33 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; +Cc: pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Feb 24, 2026 at 05:05:34PM +0100, Peter Eisentraut wrote:
> On 23.02.26 12:22, Bertrand Drouvot wrote:
> > I can see a "volatile" one though:
> > 
> > vacuum.c:1885:46: warning: cast discards ‘volatile’ qualifier from pointer target type [-Wcast-qual]
> > 
> > worth to "fix" like in the attached while at it? (It's not really a fix as it
> > moves the warning from vacuum.c to c.h, but that's consistent with 481018f2804).
> 
> That fix looks correct.

Thanks for looking at it!

> But my patch says it's making changes only without
> use unconstify and the like.
> To fix all -Wcast-qual warnings, you'd need
> more changes like that, which I'm not currently proposing.

That's the only one "cast discards ‘volatile'" remaining outside c.h, that's
why I proposed a dedicated patch for it in passing. I'll open a dedicated thread
for the remaining volatile one.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 13+ messages in thread


end of thread, other threads:[~2026-02-24 16:33 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-12-18 09:51 [PATCH v1 1/3] Don't cast away const where possible Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-12-18 13:55 Don't cast away const where possible Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-12-22 11:53 ` Peter Eisentraut <peter@eisentraut.org>
2025-12-29 09:01   ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-12-29 09:28     ` Chao Li <li.evan.chao@gmail.com>
2025-12-31 08:42     ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-01-05 13:35     ` Peter Eisentraut <peter@eisentraut.org>
2026-01-05 16:18       ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-01-12 15:44         ` Peter Eisentraut <peter@eisentraut.org>
2026-02-23 07:24           ` Peter Eisentraut <peter@eisentraut.org>
2026-02-23 11:22             ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-02-24 16:05               ` Peter Eisentraut <peter@eisentraut.org>
2026-02-24 16:33                 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox