Subject: Fix accidentally cast away qualifiers

From: Peter Eisentraut <[email-masked]>
Date: Tue, 20 Jan 2026 08:54:18 +0100

This patch fixes cases where a qualifier (const, in all cases here) was 
dropped by a cast, but the cast was otherwise necessary or desirable, so 
the straightforward fix is to add the qualifier into the cast.

This was checked with gcc -Wcast-qual, but it doesn't fix all such 
warnings, only the trivially fixable ones.

From b8ce9fd7377e666bb9fbd774e48741a74d9a7091 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email-masked]>
Date: Sat, 13 Dec 2025 23:34:38 +0100
Subject: [PATCH] Fix accidentally cast away qualifiers

This fixes cases where a qualifier (const, in all cases here) was
dropped by a cast, but the cast was otherwise necessary or desirable,
so the straightforward fix is to add the qualifier into the cast.
---
 contrib/intarray/_int_selfuncs.c              |  2 +-
 contrib/pageinspect/heapfuncs.c               |  2 +-
 contrib/uuid-ossp/uuid-ossp.c                 |  4 +-
 src/backend/access/common/toast_compression.c |  8 ++--
 src/backend/access/nbtree/nbtinsert.c         |  4 +-
 src/backend/access/transam/xloginsert.c       |  4 +-
 src/backend/backup/backup_manifest.c          |  2 +-
 src/backend/backup/basebackup.c               |  2 +-
 src/backend/backup/basebackup_incremental.c   |  6 +--
 src/backend/executor/nodeIndexscan.c          |  4 +-
 src/backend/libpq/auth-scram.c                |  4 +-
 src/backend/libpq/crypt.c                     |  4 +-
 src/backend/nodes/nodeFuncs.c                 |  6 +--
 src/backend/nodes/outfuncs.c                  | 12 ++---
 src/backend/nodes/tidbitmap.c                 |  4 +-
 src/backend/statistics/extended_stats.c       |  4 +-
 src/backend/storage/ipc/shm_mq.c              |  2 +-
 src/backend/tsearch/spell.c                   |  4 +-
 src/backend/utils/adt/geo_spgist.c            |  4 +-
 src/backend/utils/adt/json.c                  |  2 +-
 src/backend/utils/adt/pg_locale_builtin.c     |  2 +-
 src/backend/utils/adt/rangetypes.c            |  8 ++--
 src/backend/utils/adt/rangetypes_gist.c       |  4 +-
 src/bin/pg_basebackup/walmethods.c            |  2 +-
 src/bin/pg_rewind/filemap.c                   |  4 +-
 src/bin/pg_verifybackup/astreamer_verify.c    |  2 +-
 src/bin/pg_walsummary/pg_walsummary.c         |  4 +-
 src/common/scram-common.c                     |  4 +-
 src/common/unicode_case.c                     |  4 +-
 src/include/access/tupmacs.h                  |  2 +-
 src/include/common/hashfn_unstable.h          |  2 +-
 src/include/utils/memutils.h                  | 12 ++---
 src/include/varatt.h                          | 48 +++++++++----------
 src/interfaces/libpq/fe-auth-scram.c          |  2 +-
 src/interfaces/libpq/fe-auth.c                |  4 +-
 src/port/pg_crc32c_armv8.c                    | 10 ++--
 src/port/pg_popcount_aarch64.c                |  4 +-
 src/test/isolation/isolationtester.c          |  8 ++--
 .../modules/libpq_pipeline/libpq_pipeline.c   |  2 +-
 .../modules/test_tidstore/test_tidstore.c     |  8 ++--
 40 files changed, 110 insertions(+), 110 deletions(-)

diff --git a/contrib/intarray/_int_selfuncs.c b/contrib/intarray/_int_selfuncs.c
index c3e19cdf27f..4a7053028c6 100644
--- a/contrib/intarray/_int_selfuncs.c
+++ b/contrib/intarray/_int_selfuncs.c
@@ -328,7 +328,7 @@ int_query_opr_selec(ITEM *item, Datum *mcelems, float4 *mcefreqs,
 static int
 compare_val_int4(const void *a, const void *b)
 {
-	int32		key = *(int32 *) a;
+	int32		key = *(const int32 *) a;
 	int32		value = DatumGetInt32(*(const Datum *) b);
 
 	if (key < value)
diff --git a/contrib/pageinspect/heapfuncs.c b/contrib/pageinspect/heapfuncs.c
index 1cf0b44e731..8277fa256c3 100644
--- a/contrib/pageinspect/heapfuncs.c
+++ b/contrib/pageinspect/heapfuncs.c
@@ -46,7 +46,7 @@ static inline Oid
 HeapTupleHeaderGetOidOld(const HeapTupleHeaderData *tup)
 {
 	if (tup->t_infomask & HEAP_HASOID_OLD)
-		return *((Oid *) ((char *) (tup) + (tup)->t_hoff - sizeof(Oid)));
+		return *((const Oid *) ((const char *) (tup) + (tup)->t_hoff - sizeof(Oid)));
 	else
 		return InvalidOid;
 }
diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c
index 70e698f4ab3..aa4d0becace 100644
--- a/contrib/uuid-ossp/uuid-ossp.c
+++ b/contrib/uuid-ossp/uuid-ossp.c
@@ -337,7 +337,7 @@ uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
 						elog(ERROR, "could not initialize %s context: %s", "MD5",
 							 pg_cryptohash_error(ctx));
 					if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
-						pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
+						pg_cryptohash_update(ctx, (const unsigned char *) ptr, len) < 0)
 						elog(ERROR, "could not update %s context: %s", "MD5",
 							 pg_cryptohash_error(ctx));
 					/* we assume sizeof MD5 result is 16, same as UUID size */
@@ -356,7 +356,7 @@ uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
 						elog(ERROR, "could not initialize %s context: %s", "SHA1",
 							 pg_cryptohash_error(ctx));
 					if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
-						pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
+						pg_cryptohash_update(ctx, (const unsigned char *) ptr, len) < 0)
 						elog(ERROR, "could not update %s context: %s", "SHA1",
 							 pg_cryptohash_error(ctx));
 					if (pg_cryptohash_final(ctx, sha1result, sizeof(sha1result)) < 0)
diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c
index 1336328cc0b..d449613b21f 100644
--- a/src/backend/access/common/toast_compression.c
+++ b/src/backend/access/common/toast_compression.c
@@ -88,7 +88,7 @@ pglz_decompress_datum(const struct varlena *value)
 	result = (struct varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
 
 	/* decompress the data */
-	rawsize = pglz_decompress((char *) value + VARHDRSZ_COMPRESSED,
+	rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
 							  VARSIZE(value) - VARHDRSZ_COMPRESSED,
 							  VARDATA(result),
 							  VARDATA_COMPRESSED_GET_EXTSIZE(value), true);
@@ -116,7 +116,7 @@ pglz_decompress_datum_slice(const struct varlena *value,
 	result = (struct varlena *) palloc(slicelength + VARHDRSZ);
 
 	/* decompress the data */
-	rawsize = pglz_decompress((char *) value + VARHDRSZ_COMPRESSED,
+	rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
 							  VARSIZE(value) - VARHDRSZ_COMPRESSED,
 							  VARDATA(result),
 							  slicelength, false);
@@ -192,7 +192,7 @@ lz4_decompress_datum(const struct varlena *value)
 	result = (struct varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
 
 	/* decompress the data */
-	rawsize = LZ4_decompress_safe((char *) value + VARHDRSZ_COMPRESSED,
+	rawsize = LZ4_decompress_safe((const char *) value + VARHDRSZ_COMPRESSED,
 								  VARDATA(result),
 								  VARSIZE(value) - VARHDRSZ_COMPRESSED,
 								  VARDATA_COMPRESSED_GET_EXTSIZE(value));
@@ -229,7 +229,7 @@ lz4_decompress_datum_slice(const struct varlena *value, int32 slicelength)
 	result = (struct varlena *) palloc(slicelength + VARHDRSZ);
 
 	/* decompress the data */
-	rawsize = LZ4_decompress_safe_partial((char *) value + VARHDRSZ_COMPRESSED,
+	rawsize = LZ4_decompress_safe_partial((const char *) value + VARHDRSZ_COMPRESSED,
 										  VARDATA(result),
 										  VARSIZE(value) - VARHDRSZ_COMPRESSED,
 										  slicelength,
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 63eda08f7a2..d17aaa5aa0f 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -3023,8 +3023,8 @@ _bt_deadblocks(Page page, OffsetNumber *deletable, int ndeletable,
 static inline int
 _bt_blk_cmp(const void *arg1, const void *arg2)
 {
-	BlockNumber b1 = *((BlockNumber *) arg1);
-	BlockNumber b2 = *((BlockNumber *) arg2);
+	BlockNumber b1 = *((const BlockNumber *) arg1);
+	BlockNumber b2 = *((const BlockNumber *) arg2);
 
 	return pg_cmp_u32(b1, b2);
 }
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index 92c48e768c3..f928bc7c0ef 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -677,8 +677,8 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
 			if (regbuf->flags & REGBUF_STANDARD)
 			{
 				/* Assume we can omit data between pd_lower and pd_upper */
-				uint16		lower = ((PageHeader) page)->pd_lower;
-				uint16		upper = ((PageHeader) page)->pd_upper;
+				uint16		lower = ((const PageHeaderData *) page)->pd_lower;
+				uint16		upper = ((const PageHeaderData *) page)->pd_upper;
 
 				if (lower >= SizeOfPageHeaderData &&
 					upper > lower &&
diff --git a/src/backend/backup/backup_manifest.c b/src/backend/backup/backup_manifest.c
index c697caef895..3760b003907 100644
--- a/src/backend/backup/backup_manifest.c
+++ b/src/backend/backup/backup_manifest.c
@@ -388,7 +388,7 @@ AppendStringToManifest(backup_manifest_info *manifest, const char *s)
 	Assert(manifest != NULL);
 	if (manifest->still_checksumming)
 	{
-		if (pg_cryptohash_update(manifest->manifest_ctx, (uint8 *) s, len) < 0)
+		if (pg_cryptohash_update(manifest->manifest_ctx, (const uint8 *) s, len) < 0)
 			elog(ERROR, "failed to update checksum of backup manifest: %s",
 				 pg_cryptohash_error(manifest->manifest_ctx));
 	}
diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c
index ba06a38c033..463c0756b5e 100644
--- a/src/backend/backup/basebackup.c
+++ b/src/backend/backup/basebackup.c
@@ -1104,7 +1104,7 @@ sendFileWithContent(bbsink *sink, const char *filename, const char *content,
 
 	_tarWriteHeader(sink, filename, NULL, &statbuf, false);
 
-	if (pg_checksum_update(&checksum_ctx, (uint8 *) content, len) < 0)
+	if (pg_checksum_update(&checksum_ctx, (const uint8 *) content, len) < 0)
 		elog(ERROR, "could not update checksum of file \"%s\"",
 			 filename);
 
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 77dce24ad38..f58ed9b198a 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -930,7 +930,7 @@ GetIncrementalFileSize(unsigned num_blocks_required)
 static uint32
 hash_string_pointer(const char *s)
 {
-	unsigned char *ss = (unsigned char *) s;
+	const unsigned char *ss = (const unsigned char *) s;
 
 	return hash_bytes(ss, strlen(s));
 }
@@ -1049,8 +1049,8 @@ manifest_report_error(JsonManifestParseContext *context, const char *fmt,...)
 static int
 compare_block_numbers(const void *a, const void *b)
 {
-	BlockNumber aa = *(BlockNumber *) a;
-	BlockNumber bb = *(BlockNumber *) b;
+	BlockNumber aa = *(const BlockNumber *) a;
+	BlockNumber bb = *(const BlockNumber *) b;
 
 	return pg_cmp_u32(aa, bb);
 }
diff --git a/src/backend/executor/nodeIndexscan.c b/src/backend/executor/nodeIndexscan.c
index 84823f0b615..a616abff04c 100644
--- a/src/backend/executor/nodeIndexscan.c
+++ b/src/backend/executor/nodeIndexscan.c
@@ -443,8 +443,8 @@ static int
 reorderqueue_cmp(const pairingheap_node *a, const pairingheap_node *b,
 				 void *arg)
 {
-	ReorderTuple *rta = (ReorderTuple *) a;
-	ReorderTuple *rtb = (ReorderTuple *) b;
+	const ReorderTuple *rta = (const ReorderTuple *) a;
+	const ReorderTuple *rtb = (const ReorderTuple *) b;
 	IndexScanState *node = (IndexScanState *) arg;
 
 	/* exchange argument order to invert the sort order */
diff --git a/src/backend/libpq/auth-scram.c b/src/backend/libpq/auth-scram.c
index 3c41145d926..0267edb29cd 100644
--- a/src/backend/libpq/auth-scram.c
+++ b/src/backend/libpq/auth-scram.c
@@ -1490,8 +1490,8 @@ scram_mock_salt(const char *username, pg_cryptohash_type hash_type,
 
 	ctx = pg_cryptohash_create(hash_type);
 	if (pg_cryptohash_init(ctx) < 0 ||
-		pg_cryptohash_update(ctx, (uint8 *) username, strlen(username)) < 0 ||
-		pg_cryptohash_update(ctx, (uint8 *) mock_auth_nonce, MOCK_AUTH_NONCE_LEN) < 0 ||
+		pg_cryptohash_update(ctx, (const uint8 *) username, strlen(username)) < 0 ||
+		pg_cryptohash_update(ctx, (const uint8 *) mock_auth_nonce, MOCK_AUTH_NONCE_LEN) < 0 ||
 		pg_cryptohash_final(ctx, sha_digest, key_length) < 0)
 	{
 		pg_cryptohash_free(ctx);
diff --git a/src/backend/libpq/crypt.c b/src/backend/libpq/crypt.c
index 4c1052b3d42..52722060451 100644
--- a/src/backend/libpq/crypt.c
+++ b/src/backend/libpq/crypt.c
@@ -136,7 +136,7 @@ encrypt_password(PasswordType target_type, const char *role,
 			case PASSWORD_TYPE_MD5:
 				encrypted_password = palloc(MD5_PASSWD_LEN + 1);
 
-				if (!pg_md5_encrypt(password, (uint8 *) role, strlen(role),
+				if (!pg_md5_encrypt(password, (const uint8 *) role, strlen(role),
 									encrypted_password, &errstr))
 					elog(ERROR, "password encryption failed: %s", errstr);
 				break;
@@ -284,7 +284,7 @@ plain_crypt_verify(const char *role, const char *shadow_pass,
 
 		case PASSWORD_TYPE_MD5:
 			if (!pg_md5_encrypt(client_pass,
-								(uint8 *) role,
+								(const uint8 *) role,
 								strlen(role),
 								crypt_client_pass,
 								&errstr))
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index d29664ca5d4..199ed27995f 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -1009,14 +1009,14 @@ exprCollation(const Node *expr)
 			break;
 		case T_JsonExpr:
 			{
-				const JsonExpr *jsexpr = (JsonExpr *) expr;
+				const JsonExpr *jsexpr = (const JsonExpr *) expr;
 
 				coll = jsexpr->collation;
 			}
 			break;
 		case T_JsonBehavior:
 			{
-				const JsonBehavior *behavior = (JsonBehavior *) expr;
+				const JsonBehavior *behavior = (const JsonBehavior *) expr;
 
 				if (behavior->expr)
 					coll = exprCollation(behavior->expr);
@@ -1593,7 +1593,7 @@ exprLocation(const Node *expr)
 			}
 			break;
 		case T_JsonBehavior:
-			loc = exprLocation(((JsonBehavior *) expr)->expr);
+			loc = exprLocation(((const JsonBehavior *) expr)->expr);
 			break;
 		case T_NullTest:
 			{
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index c8eef2c75d2..40990143927 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -736,17 +736,17 @@ outNode(StringInfo str, const void *obj)
 		_outList(str, obj);
 	/* nodeRead does not want to see { } around these! */
 	else if (IsA(obj, Integer))
-		_outInteger(str, (Integer *) obj);
+		_outInteger(str, (const Integer *) obj);
 	else if (IsA(obj, Float))
-		_outFloat(str, (Float *) obj);
+		_outFloat(str, (const Float *) obj);
 	else if (IsA(obj, Boolean))
-		_outBoolean(str, (Boolean *) obj);
+		_outBoolean(str, (const Boolean *) obj);
 	else if (IsA(obj, String))
-		_outString(str, (String *) obj);
+		_outString(str, (const String *) obj);
 	else if (IsA(obj, BitString))
-		_outBitString(str, (BitString *) obj);
+		_outBitString(str, (const BitString *) obj);
 	else if (IsA(obj, Bitmapset))
-		outBitmapset(str, (Bitmapset *) obj);
+		outBitmapset(str, (const Bitmapset *) obj);
 	else
 	{
 		appendStringInfoChar(str, '{');
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 805ff21c7f1..f1f925cb13b 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -1439,8 +1439,8 @@ static int
 tbm_shared_comparator(const void *left, const void *right, void *arg)
 {
 	PagetableEntry *base = (PagetableEntry *) arg;
-	PagetableEntry *lpage = &base[*(int *) left];
-	PagetableEntry *rpage = &base[*(int *) right];
+	PagetableEntry *lpage = &base[*(const int *) left];
+	PagetableEntry *rpage = &base[*(const int *) right];
 
 	if (lpage->blockno < rpage->blockno)
 		return -1;
diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index b9a8f257042..3895ed72ef7 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -915,8 +915,8 @@ multi_sort_compare_dims(int start, int end,
 int
 compare_scalars_simple(const void *a, const void *b, void *arg)
 {
-	return compare_datums_simple(*(Datum *) a,
-								 *(Datum *) b,
+	return compare_datums_simple(*(const Datum *) a,
+								 *(const Datum *) b,
 								 (SortSupport) arg);
 }
 
diff --git a/src/backend/storage/ipc/shm_mq.c b/src/backend/storage/ipc/shm_mq.c
index 4d6cc16c0bc..3ce6068ac54 100644
--- a/src/backend/storage/ipc/shm_mq.c
+++ b/src/backend/storage/ipc/shm_mq.c
@@ -1041,7 +1041,7 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, const void *data,
 			 */
 			pg_memory_barrier();
 			memcpy(&mq->mq_ring[mq->mq_ring_offset + offset],
-				   (char *) data + sent, sendnow);
+				   (const char *) data + sent, sendnow);
 			sent += sendnow;
 
 			/*
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index e3436dbddd2..ad0ceec37b0 100644
--- a/src/backend/tsearch/spell.c
+++ b/src/backend/tsearch/spell.c
@@ -2463,9 +2463,9 @@ SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *wor
 		while (StopLow < StopHigh)
 		{
 			StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
-			if (StopMiddle->val == ((uint8 *) (word))[level])
+			if (StopMiddle->val == ((const uint8 *) (word))[level])
 				break;
-			else if (StopMiddle->val < ((uint8 *) (word))[level])
+			else if (StopMiddle->val < ((const uint8 *) (word))[level])
 				StopLow = StopMiddle + 1;
 			else
 				StopHigh = StopMiddle;
diff --git a/src/backend/utils/adt/geo_spgist.c b/src/backend/utils/adt/geo_spgist.c
index 19bcae3848b..7a19ca3892b 100644
--- a/src/backend/utils/adt/geo_spgist.c
+++ b/src/backend/utils/adt/geo_spgist.c
@@ -92,8 +92,8 @@
 static int
 compareDoubles(const void *a, const void *b)
 {
-	float8		x = *(float8 *) a;
-	float8		y = *(float8 *) b;
+	float8		x = *(const float8 *) a;
+	float8		y = *(const float8 *) b;
 
 	if (x == y)
 		return 0;
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 78e84727fdc..0b161398465 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -901,7 +901,7 @@ json_agg_finalfn(PG_FUNCTION_ARGS)
 static uint32
 json_unique_hash(const void *key, Size keysize)
 {
-	const JsonUniqueHashEntry *entry = (JsonUniqueHashEntry *) key;
+	const JsonUniqueHashEntry *entry = (const JsonUniqueHashEntry *) key;
 	uint32		hash = hash_bytes_uint32(entry->object_id);
 
 	hash ^= hash_bytes((const unsigned char *) entry->key, entry->key_len);
diff --git a/src/backend/utils/adt/pg_locale_builtin.c b/src/backend/utils/adt/pg_locale_builtin.c
index 1f5fc1c97f3..b5aeb7a337a 100644
--- a/src/backend/utils/adt/pg_locale_builtin.c
+++ b/src/backend/utils/adt/pg_locale_builtin.c
@@ -63,7 +63,7 @@ initcap_wbnext(void *state)
 	while (wbstate->offset < wbstate->len &&
 		   wbstate->str[wbstate->offset] != '\0')
 	{
-		char32_t	u = utf8_to_unicode((unsigned char *) wbstate->str +
+		char32_t	u = utf8_to_unicode((const unsigned char *) wbstate->str +
 										wbstate->offset);
 		bool		curr_alnum = pg_u_isalnum(u, wbstate->posix);
 
diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c
index 701aacbbfb0..06cc3af4f4a 100644
--- a/src/backend/utils/adt/rangetypes.c
+++ b/src/backend/utils/adt/rangetypes.c
@@ -2108,7 +2108,7 @@ range_deserialize(TypeCacheEntry *typcache, const RangeType *range,
 	typalign = typcache->rngelemtype->typalign;
 
 	/* initialize data pointer just after the range OID */
-	ptr = (char *) (range + 1);
+	ptr = (const char *) (range + 1);
 
 	/* fetch lower bound, if any */
 	if (RANGE_HAS_LBOUND(flags))
@@ -2155,7 +2155,7 @@ char
 range_get_flags(const RangeType *range)
 {
 	/* fetch the flag byte from datum's last byte */
-	return *((char *) range + VARSIZE(range) - 1);
+	return *((const char *) range + VARSIZE(range) - 1);
 }
 
 /*
@@ -2360,8 +2360,8 @@ range_cmp_bound_values(TypeCacheEntry *typcache, const RangeBound *b1,
 int
 range_compare(const void *key1, const void *key2, void *arg)
 {
-	RangeType  *r1 = *(RangeType **) key1;
-	RangeType  *r2 = *(RangeType **) key2;
+	RangeType  *r1 = *(RangeType *const *) key1;
+	RangeType  *r2 = *(RangeType *const *) key2;
 	TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
 	RangeBound	lower1;
 	RangeBound	upper1;
diff --git a/src/backend/utils/adt/rangetypes_gist.c b/src/backend/utils/adt/rangetypes_gist.c
index cb9f769cdb2..1a01a8f4c3c 100644
--- a/src/backend/utils/adt/rangetypes_gist.c
+++ b/src/backend/utils/adt/rangetypes_gist.c
@@ -1768,8 +1768,8 @@ interval_cmp_upper(const void *a, const void *b, void *arg)
 static int
 common_entry_cmp(const void *i1, const void *i2)
 {
-	double		delta1 = ((CommonEntry *) i1)->delta;
-	double		delta2 = ((CommonEntry *) i2)->delta;
+	double		delta1 = ((const CommonEntry *) i1)->delta;
+	double		delta2 = ((const CommonEntry *) i2)->delta;
 
 	if (delta1 < delta2)
 		return -1;
diff --git a/src/bin/pg_basebackup/walmethods.c b/src/bin/pg_basebackup/walmethods.c
index f6e2371f477..17d22c79f68 100644
--- a/src/bin/pg_basebackup/walmethods.c
+++ b/src/bin/pg_basebackup/walmethods.c
@@ -359,7 +359,7 @@ dir_write(Walfile *f, const void *buf, size_t count)
 				return -1;
 			}
 
-			inbuf = ((char *) inbuf) + chunk;
+			inbuf = ((const char *) inbuf) + chunk;
 		}
 
 		/* Our caller keeps track of the uncompressed size. */
diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c
index 2610bae7e09..b79c47f9252 100644
--- a/src/bin/pg_rewind/filemap.c
+++ b/src/bin/pg_rewind/filemap.c
@@ -694,8 +694,8 @@ datasegpath(RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno)
 static int
 final_filemap_cmp(const void *a, const void *b)
 {
-	file_entry_t *fa = *((file_entry_t **) a);
-	file_entry_t *fb = *((file_entry_t **) b);
+	file_entry_t *fa = *((file_entry_t *const *) a);
+	file_entry_t *fb = *((file_entry_t *const *) b);
 
 	if (fa->action > fb->action)
 		return 1;
diff --git a/src/bin/pg_verifybackup/astreamer_verify.c b/src/bin/pg_verifybackup/astreamer_verify.c
index 440c96269d7..0edc8123b43 100644
--- a/src/bin/pg_verifybackup/astreamer_verify.c
+++ b/src/bin/pg_verifybackup/astreamer_verify.c
@@ -268,7 +268,7 @@ member_compute_checksum(astreamer *streamer, astreamer_member *member,
 	mystreamer->checksum_bytes += len;
 
 	/* Feed these bytes to the checksum calculation. */
-	if (pg_checksum_update(checksum_ctx, (uint8 *) data, len) < 0)
+	if (pg_checksum_update(checksum_ctx, (const uint8 *) data, len) < 0)
 	{
 		report_backup_error(mystreamer->context,
 							"could not update checksum of file \"%s\"",
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 74851f84f92..aa214b8616d 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -217,8 +217,8 @@ dump_one_relation(ws_options *opt, RelFileLocator *rlocator,
 static int
 compare_block_numbers(const void *a, const void *b)
 {
-	BlockNumber aa = *(BlockNumber *) a;
-	BlockNumber bb = *(BlockNumber *) b;
+	BlockNumber aa = *(const BlockNumber *) a;
+	BlockNumber bb = *(const BlockNumber *) b;
 
 	return pg_cmp_u32(aa, bb);
 }
diff --git a/src/common/scram-common.c b/src/common/scram-common.c
index dbf188ae872..259fa5554b6 100644
--- a/src/common/scram-common.c
+++ b/src/common/scram-common.c
@@ -61,7 +61,7 @@ scram_SaltedPassword(const char *password,
 	 */
 
 	/* First iteration */
-	if (pg_hmac_init(hmac_ctx, (uint8 *) password, password_len) < 0 ||
+	if (pg_hmac_init(hmac_ctx, (const uint8 *) password, password_len) < 0 ||
 		pg_hmac_update(hmac_ctx, salt, saltlen) < 0 ||
 		pg_hmac_update(hmac_ctx, (uint8 *) &one, sizeof(uint32)) < 0 ||
 		pg_hmac_final(hmac_ctx, Ui_prev, key_length) < 0)
@@ -84,7 +84,7 @@ scram_SaltedPassword(const char *password,
 		CHECK_FOR_INTERRUPTS();
 #endif
 
-		if (pg_hmac_init(hmac_ctx, (uint8 *) password, password_len) < 0 ||
+		if (pg_hmac_init(hmac_ctx, (const uint8 *) password, password_len) < 0 ||
 			pg_hmac_update(hmac_ctx, (uint8 *) Ui_prev, key_length) < 0 ||
 			pg_hmac_final(hmac_ctx, Ui, key_length) < 0)
 		{
diff --git a/src/common/unicode_case.c b/src/common/unicode_case.c
index 71acd38d6fe..0b8d3ffc0b4 100644
--- a/src/common/unicode_case.c
+++ b/src/common/unicode_case.c
@@ -231,7 +231,7 @@ convert_case(char *dst, size_t dstsize, const char *src, ssize_t srclen,
 
 	while ((srclen < 0 || srcoff < srclen) && src[srcoff] != '\0')
 	{
-		char32_t	u1 = utf8_to_unicode((unsigned char *) src + srcoff);
+		char32_t	u1 = utf8_to_unicode((const unsigned char *) src + srcoff);
 		int			u1len = unicode_utf8len(u1);
 		char32_t	simple = 0;
 		const char32_t *special = NULL;
@@ -373,7 +373,7 @@ check_special_conditions(int conditions, const char *str, size_t len,
 	if (conditions == 0)
 		return true;
 	else if (conditions == PG_U_FINAL_SIGMA)
-		return check_final_sigma((unsigned char *) str, len, offset);
+		return check_final_sigma((const unsigned char *) str, len, offset);
 
 	/* no other conditions supported */
 	Assert(false);
diff --git a/src/include/access/tupmacs.h b/src/include/access/tupmacs.h
index e6df8264750..3e5530658c9 100644
--- a/src/include/access/tupmacs.h
+++ b/src/include/access/tupmacs.h
@@ -190,7 +190,7 @@ fetch_att(const void *T, bool attbyval, int attlen)
 	: \
 	( \
 		AssertMacro((attlen) == -2), \
-		(cur_offset) + (strlen((char *) (attptr)) + 1) \
+		(cur_offset) + (strlen((const char *) (attptr)) + 1) \
 	)) \
 )
 
diff --git a/src/include/common/hashfn_unstable.h b/src/include/common/hashfn_unstable.h
index 428936b8b64..11225faafda 100644
--- a/src/include/common/hashfn_unstable.h
+++ b/src/include/common/hashfn_unstable.h
@@ -264,7 +264,7 @@ fasthash_accum_cstring_aligned(fasthash_state *hs, const char *str)
 	 */
 	for (;;)
 	{
-		uint64		chunk = *(uint64 *) str;
+		uint64		chunk = *(const uint64 *) str;
 
 		zero_byte_low = haszero64(chunk);
 		if (zero_byte_low)
diff --git a/src/include/utils/memutils.h b/src/include/utils/memutils.h
index 2bc13c3a054..11ab1717a16 100644
--- a/src/include/utils/memutils.h
+++ b/src/include/utils/memutils.h
@@ -253,7 +253,7 @@ pg_memory_is_all_zeros(const void *ptr, size_t len)
 		 */
 		for (; p < aligned_end; p += sizeof(size_t))
 		{
-			if (*(size_t *) p != 0)
+			if (*(const size_t *) p != 0)
 				return false;
 		}
 
@@ -290,10 +290,10 @@ pg_memory_is_all_zeros(const void *ptr, size_t len)
 	 */
 	for (; p < aligned_end - (sizeof(size_t) * 7); p += sizeof(size_t) * 8)
 	{
-		if ((((size_t *) p)[0] != 0) | (((size_t *) p)[1] != 0) |
-			(((size_t *) p)[2] != 0) | (((size_t *) p)[3] != 0) |
-			(((size_t *) p)[4] != 0) | (((size_t *) p)[5] != 0) |
-			(((size_t *) p)[6] != 0) | (((size_t *) p)[7] != 0))
+		if ((((const size_t *) p)[0] != 0) | (((const size_t *) p)[1] != 0) |
+			(((const size_t *) p)[2] != 0) | (((const size_t *) p)[3] != 0) |
+			(((const size_t *) p)[4] != 0) | (((const size_t *) p)[5] != 0) |
+			(((const size_t *) p)[6] != 0) | (((const size_t *) p)[7] != 0))
 			return false;
 	}
 
@@ -305,7 +305,7 @@ pg_memory_is_all_zeros(const void *ptr, size_t len)
 	 */
 	for (; p < aligned_end; p += sizeof(size_t))
 	{
-		if (*(size_t *) p != 0)
+		if (*(const size_t *) p != 0)
 			return false;
 	}
 
diff --git a/src/include/varatt.h b/src/include/varatt.h
index eccd3ca04d6..03e9d1869aa 100644
--- a/src/include/varatt.h
+++ b/src/include/varatt.h
@@ -193,25 +193,25 @@ typedef struct
 #ifdef WORDS_BIGENDIAN
 
 #define VARATT_IS_4B(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
+	((((const varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
 #define VARATT_IS_4B_U(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
+	((((const varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
 #define VARATT_IS_4B_C(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
+	((((const varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
 #define VARATT_IS_1B(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
+	((((const varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
 #define VARATT_IS_1B_E(PTR) \
-	((((varattrib_1b *) (PTR))->va_header) == 0x80)
+	((((const varattrib_1b *) (PTR))->va_header) == 0x80)
 #define VARATT_NOT_PAD_BYTE(PTR) \
-	(*((uint8 *) (PTR)) != 0)
+	(*((const uint8 *) (PTR)) != 0)
 
 /* VARSIZE_4B() should only be used on known-aligned data */
 #define VARSIZE_4B(PTR) \
-	(((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
+	(((const varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
 #define VARSIZE_1B(PTR) \
-	(((varattrib_1b *) (PTR))->va_header & 0x7F)
+	(((const varattrib_1b *) (PTR))->va_header & 0x7F)
 #define VARTAG_1B_E(PTR) \
-	((vartag_external) ((varattrib_1b_e *) (PTR))->va_tag)
+	((vartag_external) ((const varattrib_1b_e *) (PTR))->va_tag)
 
 #define SET_VARSIZE_4B(PTR,len) \
 	(((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
@@ -226,25 +226,25 @@ typedef struct
 #else							/* !WORDS_BIGENDIAN */
 
 #define VARATT_IS_4B(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
+	((((const varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
 #define VARATT_IS_4B_U(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
+	((((const varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
 #define VARATT_IS_4B_C(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
+	((((const varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
 #define VARATT_IS_1B(PTR) \
-	((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
+	((((const varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
 #define VARATT_IS_1B_E(PTR) \
-	((((varattrib_1b *) (PTR))->va_header) == 0x01)
+	((((const varattrib_1b *) (PTR))->va_header) == 0x01)
 #define VARATT_NOT_PAD_BYTE(PTR) \
-	(*((uint8 *) (PTR)) != 0)
+	(*((const uint8 *) (PTR)) != 0)
 
 /* VARSIZE_4B() should only be used on known-aligned data */
 #define VARSIZE_4B(PTR) \
-	((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
+	((((const varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
 #define VARSIZE_1B(PTR) \
-	((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
+	((((const varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
 #define VARTAG_1B_E(PTR) \
-	((vartag_external) ((varattrib_1b_e *) (PTR))->va_tag)
+	((vartag_external) ((const varattrib_1b_e *) (PTR))->va_tag)
 
 #define SET_VARSIZE_4B(PTR,len) \
 	(((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
@@ -258,10 +258,10 @@ typedef struct
 
 #endif							/* WORDS_BIGENDIAN */
 
-#define VARDATA_4B(PTR)		(((varattrib_4b *) (PTR))->va_4byte.va_data)
-#define VARDATA_4B_C(PTR)	(((varattrib_4b *) (PTR))->va_compressed.va_data)
-#define VARDATA_1B(PTR)		(((varattrib_1b *) (PTR))->va_data)
-#define VARDATA_1B_E(PTR)	(((varattrib_1b_e *) (PTR))->va_data)
+#define VARDATA_4B(PTR)		(((const varattrib_4b *) (PTR))->va_4byte.va_data)
+#define VARDATA_4B_C(PTR)	(((const varattrib_4b *) (PTR))->va_compressed.va_data)
+#define VARDATA_1B(PTR)		(((const varattrib_1b *) (PTR))->va_data)
+#define VARDATA_1B_E(PTR)	(((const varattrib_1b_e *) (PTR))->va_data)
 
 /*
  * Externally visible TOAST functions and macros begin here.  All of these
@@ -492,14 +492,14 @@ VARDATA_ANY(const void *PTR)
 static inline Size
 VARDATA_COMPRESSED_GET_EXTSIZE(const void *PTR)
 {
-	return ((varattrib_4b *) PTR)->va_compressed.va_tcinfo & VARLENA_EXTSIZE_MASK;
+	return ((const varattrib_4b *) PTR)->va_compressed.va_tcinfo & VARLENA_EXTSIZE_MASK;
 }
 
 /* Compression method of a compressed-in-line varlena datum */
 static inline uint32
 VARDATA_COMPRESSED_GET_COMPRESS_METHOD(const void *PTR)
 {
-	return ((varattrib_4b *) PTR)->va_compressed.va_tcinfo >> VARLENA_EXTSIZE_BITS;
+	return ((const varattrib_4b *) PTR)->va_compressed.va_tcinfo >> VARLENA_EXTSIZE_BITS;
 }
 
 /* Same for external Datums; but note argument is a struct varatt_external */
diff --git a/src/interfaces/libpq/fe-auth-scram.c b/src/interfaces/libpq/fe-auth-scram.c
index 05273c91f98..99103b7e2b6 100644
--- a/src/interfaces/libpq/fe-auth-scram.c
+++ b/src/interfaces/libpq/fe-auth-scram.c
@@ -819,7 +819,7 @@ calculate_client_proof(fe_scram_state *state,
 					   strlen(state->server_first_message)) < 0 ||
 		pg_hmac_update(ctx, (uint8 *) ",", 1) < 0 ||
 		pg_hmac_update(ctx,
-					   (uint8 *) client_final_message_without_proof,
+					   (const uint8 *) client_final_message_without_proof,
 					   strlen(client_final_message_without_proof)) < 0 ||
 		pg_hmac_final(ctx, ClientSignature, state->key_length) < 0)
 	{
diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c
index f08db30dbb7..f05aaea9651 100644
--- a/src/interfaces/libpq/fe-auth.c
+++ b/src/interfaces/libpq/fe-auth.c
@@ -1369,7 +1369,7 @@ PQencryptPassword(const char *passwd, const char *user)
 	if (!crypt_pwd)
 		return NULL;
 
-	if (!pg_md5_encrypt(passwd, (uint8 *) user, strlen(user), crypt_pwd, &errstr))
+	if (!pg_md5_encrypt(passwd, (const uint8 *) user, strlen(user), crypt_pwd, &errstr))
 	{
 		free(crypt_pwd);
 		return NULL;
@@ -1482,7 +1482,7 @@ PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user,
 		{
 			const char *errstr = NULL;
 
-			if (!pg_md5_encrypt(passwd, (uint8 *) user, strlen(user), crypt_pwd, &errstr))
+			if (!pg_md5_encrypt(passwd, (const uint8 *) user, strlen(user), crypt_pwd, &errstr))
 			{
 				libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
 				free(crypt_pwd);
diff --git a/src/port/pg_crc32c_armv8.c b/src/port/pg_crc32c_armv8.c
index 039986c7b33..9ca0f728d39 100644
--- a/src/port/pg_crc32c_armv8.c
+++ b/src/port/pg_crc32c_armv8.c
@@ -42,32 +42,32 @@ pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len)
 	if (!PointerIsAligned(p, uint32) &&
 		p + 2 <= pend)
 	{
-		crc = __crc32ch(crc, *(uint16 *) p);
+		crc = __crc32ch(crc, *(const uint16 *) p);
 		p += 2;
 	}
 	if (!PointerIsAligned(p, uint64) &&
 		p + 4 <= pend)
 	{
-		crc = __crc32cw(crc, *(uint32 *) p);
+		crc = __crc32cw(crc, *(const uint32 *) p);
 		p += 4;
 	}
 
 	/* Process eight bytes at a time, as far as we can. */
 	while (p + 8 <= pend)
 	{
-		crc = __crc32cd(crc, *(uint64 *) p);
+		crc = __crc32cd(crc, *(const uint64 *) p);
 		p += 8;
 	}
 
 	/* Process remaining 0-7 bytes. */
 	if (p + 4 <= pend)
 	{
-		crc = __crc32cw(crc, *(uint32 *) p);
+		crc = __crc32cw(crc, *(const uint32 *) p);
 		p += 4;
 	}
 	if (p + 2 <= pend)
 	{
-		crc = __crc32ch(crc, *(uint16 *) p);
+		crc = __crc32ch(crc, *(const uint16 *) p);
 		p += 2;
 	}
 	if (p < pend)
diff --git a/src/port/pg_popcount_aarch64.c b/src/port/pg_popcount_aarch64.c
index cda73cf6088..7c248ba7561 100644
--- a/src/port/pg_popcount_aarch64.c
+++ b/src/port/pg_popcount_aarch64.c
@@ -383,7 +383,7 @@ pg_popcount_neon(const char *buf, int bytes)
 	 */
 	for (; bytes >= sizeof(uint64); bytes -= sizeof(uint64))
 	{
-		popcnt += pg_popcount64(*((uint64 *) buf));
+		popcnt += pg_popcount64(*((const uint64 *) buf));
 		buf += sizeof(uint64);
 	}
 
@@ -465,7 +465,7 @@ pg_popcount_masked_neon(const char *buf, int bytes, bits8 mask)
 	 */
 	for (; bytes >= sizeof(uint64); bytes -= sizeof(uint64))
 	{
-		popcnt += pg_popcount64(*((uint64 *) buf) & mask64);
+		popcnt += pg_popcount64(*((const uint64 *) buf) & mask64);
 		buf += sizeof(uint64);
 	}
 
diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index e01c0c9de93..a0aec04d994 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -498,8 +498,8 @@ run_named_permutations(TestSpec *testspec)
 static int
 step_qsort_cmp(const void *a, const void *b)
 {
-	Step	   *stepa = *((Step **) a);
-	Step	   *stepb = *((Step **) b);
+	Step	   *stepa = *((Step *const *) a);
+	Step	   *stepb = *((Step *const *) b);
 
 	return strcmp(stepa->name, stepb->name);
 }
@@ -507,8 +507,8 @@ step_qsort_cmp(const void *a, const void *b)
 static int
 step_bsearch_cmp(const void *a, const void *b)
 {
-	char	   *stepname = (char *) a;
-	Step	   *step = *((Step **) b);
+	const char *stepname = (const char *) a;
+	Step	   *step = *((Step *const *) b);
 
 	return strcmp(stepname, step->name);
 }
diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c
index 03371721460..4a9bb65b9bb 100644
--- a/src/test/modules/libpq_pipeline/libpq_pipeline.c
+++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c
@@ -1576,7 +1576,7 @@ test_singlerowmode(PGconn *conn)
 							  "SELECT generate_series(42, $1)",
 							  1,
 							  NULL,
-							  (const char **) param,
+							  (const char *const *) param,
 							  NULL,
 							  NULL,
 							  0) != 1)
diff --git a/src/test/modules/test_tidstore/test_tidstore.c b/src/test/modules/test_tidstore/test_tidstore.c
index 9a3a209da0b..c9a035fa494 100644
--- a/src/test/modules/test_tidstore/test_tidstore.c
+++ b/src/test/modules/test_tidstore/test_tidstore.c
@@ -56,16 +56,16 @@ itemptr_cmp(const void *left, const void *right)
 	OffsetNumber loff,
 				roff;
 
-	lblk = ItemPointerGetBlockNumber((ItemPointer) left);
-	rblk = ItemPointerGetBlockNumber((ItemPointer) right);
+	lblk = ItemPointerGetBlockNumber((const ItemPointerData *) left);
+	rblk = ItemPointerGetBlockNumber((const ItemPointerData *) right);
 
 	if (lblk < rblk)
 		return -1;
 	if (lblk > rblk)
 		return 1;
 
-	loff = ItemPointerGetOffsetNumber((ItemPointer) left);
-	roff = ItemPointerGetOffsetNumber((ItemPointer) right);
+	loff = ItemPointerGetOffsetNumber((const ItemPointerData *) left);
+	roff = ItemPointerGetOffsetNumber((const ItemPointerData *) right);
 
 	if (loff < roff)
 		return -1;
-- 
2.52.0