diff --git a/contrib/btree_gist/btree_cash.c b/contrib/btree_gist/btree_cash.c
index f22e1bff92982e4736a433c5f33a53dd735249be..8e8495ca0686fde77a830e6dfbe9c4d83cfc7982 100644
--- a/contrib/btree_gist/btree_cash.c
+++ b/contrib/btree_gist/btree_cash.c
@@ -35,34 +35,34 @@ Datum		gbt_cash_same(PG_FUNCTION_ARGS);
 static bool
 gbt_cashgt(const void *a, const void *b)
 {
-	return (*((Cash *) a) > *((Cash *) b));
+	return (*((const Cash *) a) > *((const Cash *) b));
 }
 static bool
 gbt_cashge(const void *a, const void *b)
 {
-	return (*((Cash *) a) >= *((Cash *) b));
+	return (*((const Cash *) a) >= *((const Cash *) b));
 }
 static bool
 gbt_casheq(const void *a, const void *b)
 {
-	return (*((Cash *) a) == *((Cash *) b));
+	return (*((const Cash *) a) == *((const Cash *) b));
 }
 static bool
 gbt_cashle(const void *a, const void *b)
 {
-	return (*((Cash *) a) <= *((Cash *) b));
+	return (*((const Cash *) a) <= *((const Cash *) b));
 }
 static bool
 gbt_cashlt(const void *a, const void *b)
 {
-	return (*((Cash *) a) < *((Cash *) b));
+	return (*((const Cash *) a) < *((const Cash *) b));
 }
 
 static int
 gbt_cashkey_cmp(const void *a, const void *b)
 {
-	cashKEY    *ia = (cashKEY *) (((Nsrt *) a)->t);
-	cashKEY    *ib = (cashKEY *) (((Nsrt *) b)->t);
+	cashKEY    *ia = (cashKEY *) (((const Nsrt *) a)->t);
+	cashKEY    *ib = (cashKEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_date.c b/contrib/btree_gist/btree_date.c
index d6b989a177b3821ae14cbeb39220e39b4a8beecf..1c0c3ec20c8a7f9d5a4acd8c1067936cc2ff47ef 100644
--- a/contrib/btree_gist/btree_date.c
+++ b/contrib/btree_gist/btree_date.c
@@ -36,7 +36,7 @@ static bool
 gbt_dategt(const void *a, const void *b)
 {
 	return DatumGetBool(
-						DirectFunctionCall2(date_gt, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
+						DirectFunctionCall2(date_gt, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
 		);
 }
 
@@ -44,7 +44,7 @@ static bool
 gbt_datege(const void *a, const void *b)
 {
 	return DatumGetBool(
-						DirectFunctionCall2(date_ge, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
+						DirectFunctionCall2(date_ge, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
 		);
 }
 
@@ -52,7 +52,7 @@ static bool
 gbt_dateeq(const void *a, const void *b)
 {
 	return DatumGetBool(
-						DirectFunctionCall2(date_eq, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
+						DirectFunctionCall2(date_eq, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
 		);
 }
 
@@ -60,7 +60,7 @@ static bool
 gbt_datele(const void *a, const void *b)
 {
 	return DatumGetBool(
-						DirectFunctionCall2(date_le, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
+						DirectFunctionCall2(date_le, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
 		);
 }
 
@@ -68,7 +68,7 @@ static bool
 gbt_datelt(const void *a, const void *b)
 {
 	return DatumGetBool(
-						DirectFunctionCall2(date_lt, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
+						DirectFunctionCall2(date_lt, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
 		);
 }
 
@@ -77,8 +77,8 @@ gbt_datelt(const void *a, const void *b)
 static int
 gbt_datekey_cmp(const void *a, const void *b)
 {
-	dateKEY    *ia = (dateKEY *) (((Nsrt *) a)->t);
-	dateKEY    *ib = (dateKEY *) (((Nsrt *) b)->t);
+	dateKEY    *ia = (dateKEY *) (((const Nsrt *) a)->t);
+	dateKEY    *ib = (dateKEY *) (((const Nsrt *) b)->t);
 	int			res;
 
 	res = DatumGetInt32(DirectFunctionCall2(date_cmp, DateADTGetDatum(ia->lower), DateADTGetDatum(ib->lower)));
diff --git a/contrib/btree_gist/btree_float4.c b/contrib/btree_gist/btree_float4.c
index e3f2155d5935225f495eacf70e2d5d01f7998020..cf1e45a381bcf31ffbfc1faefc0389a00e795803 100644
--- a/contrib/btree_gist/btree_float4.c
+++ b/contrib/btree_gist/btree_float4.c
@@ -34,34 +34,34 @@ Datum		gbt_float4_same(PG_FUNCTION_ARGS);
 static bool
 gbt_float4gt(const void *a, const void *b)
 {
-	return (*((float4 *) a) > *((float4 *) b));
+	return (*((const float4 *) a) > *((const float4 *) b));
 }
 static bool
 gbt_float4ge(const void *a, const void *b)
 {
-	return (*((float4 *) a) >= *((float4 *) b));
+	return (*((const float4 *) a) >= *((const float4 *) b));
 }
 static bool
 gbt_float4eq(const void *a, const void *b)
 {
-	return (*((float4 *) a) == *((float4 *) b));
+	return (*((const float4 *) a) == *((const float4 *) b));
 }
 static bool
 gbt_float4le(const void *a, const void *b)
 {
-	return (*((float4 *) a) <= *((float4 *) b));
+	return (*((const float4 *) a) <= *((const float4 *) b));
 }
 static bool
 gbt_float4lt(const void *a, const void *b)
 {
-	return (*((float4 *) a) < *((float4 *) b));
+	return (*((const float4 *) a) < *((const float4 *) b));
 }
 
 static int
 gbt_float4key_cmp(const void *a, const void *b)
 {
-	float4KEY  *ia = (float4KEY *) (((Nsrt *) a)->t);
-	float4KEY  *ib = (float4KEY *) (((Nsrt *) b)->t);
+	float4KEY  *ia = (float4KEY *) (((const Nsrt *) a)->t);
+	float4KEY  *ib = (float4KEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_float8.c b/contrib/btree_gist/btree_float8.c
index 0fbebeea077b0df72ab758054d27f8d5a10ebcf9..3ce87642cb9574dbb073209ce26b7d47374d6f8a 100644
--- a/contrib/btree_gist/btree_float8.c
+++ b/contrib/btree_gist/btree_float8.c
@@ -35,34 +35,34 @@ Datum		gbt_float8_same(PG_FUNCTION_ARGS);
 static bool
 gbt_float8gt(const void *a, const void *b)
 {
-	return (*((float8 *) a) > *((float8 *) b));
+	return (*((const float8 *) a) > *((const float8 *) b));
 }
 static bool
 gbt_float8ge(const void *a, const void *b)
 {
-	return (*((float8 *) a) >= *((float8 *) b));
+	return (*((const float8 *) a) >= *((const float8 *) b));
 }
 static bool
 gbt_float8eq(const void *a, const void *b)
 {
-	return (*((float8 *) a) == *((float8 *) b));
+	return (*((const float8 *) a) == *((const float8 *) b));
 }
 static bool
 gbt_float8le(const void *a, const void *b)
 {
-	return (*((float8 *) a) <= *((float8 *) b));
+	return (*((const float8 *) a) <= *((const float8 *) b));
 }
 static bool
 gbt_float8lt(const void *a, const void *b)
 {
-	return (*((float8 *) a) < *((float8 *) b));
+	return (*((const float8 *) a) < *((const float8 *) b));
 }
 
 static int
 gbt_float8key_cmp(const void *a, const void *b)
 {
-	float8KEY  *ia = (float8KEY *) (((Nsrt *) a)->t);
-	float8KEY  *ib = (float8KEY *) (((Nsrt *) b)->t);
+	float8KEY  *ia = (float8KEY *) (((const Nsrt *) a)->t);
+	float8KEY  *ib = (float8KEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_inet.c b/contrib/btree_gist/btree_inet.c
index 439e6c90a7c9906de62ed020b4935463879e5220..c136296ab536e2262628fb182eab9d3debe960d0 100644
--- a/contrib/btree_gist/btree_inet.c
+++ b/contrib/btree_gist/btree_inet.c
@@ -36,34 +36,34 @@ Datum		gbt_inet_same(PG_FUNCTION_ARGS);
 static bool
 gbt_inetgt(const void *a, const void *b)
 {
-	return (*((double *) a) > *((double *) b));
+	return (*((const double *) a) > *((const double *) b));
 }
 static bool
 gbt_inetge(const void *a, const void *b)
 {
-	return (*((double *) a) >= *((double *) b));
+	return (*((const double *) a) >= *((const double *) b));
 }
 static bool
 gbt_ineteq(const void *a, const void *b)
 {
-	return (*((double *) a) == *((double *) b));
+	return (*((const double *) a) == *((const double *) b));
 }
 static bool
 gbt_inetle(const void *a, const void *b)
 {
-	return (*((double *) a) <= *((double *) b));
+	return (*((const double *) a) <= *((const double *) b));
 }
 static bool
 gbt_inetlt(const void *a, const void *b)
 {
-	return (*((double *) a) < *((double *) b));
+	return (*((const double *) a) < *((const double *) b));
 }
 
 static int
 gbt_inetkey_cmp(const void *a, const void *b)
 {
-	inetKEY    *ia = (inetKEY *) (((Nsrt *) a)->t);
-	inetKEY    *ib = (inetKEY *) (((Nsrt *) b)->t);
+	inetKEY    *ia = (inetKEY *) (((const Nsrt *) a)->t);
+	inetKEY    *ib = (inetKEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_int2.c b/contrib/btree_gist/btree_int2.c
index 3eb76b8be0697170c629f5e852d77df28eb1e88a..a40912b13d65b97151ac3cb3eb819e0629dbb7ec 100644
--- a/contrib/btree_gist/btree_int2.c
+++ b/contrib/btree_gist/btree_int2.c
@@ -34,34 +34,34 @@ Datum		gbt_int2_same(PG_FUNCTION_ARGS);
 static bool
 gbt_int2gt(const void *a, const void *b)
 {
-	return (*((int16 *) a) > *((int16 *) b));
+	return (*((const int16 *) a) > *((const int16 *) b));
 }
 static bool
 gbt_int2ge(const void *a, const void *b)
 {
-	return (*((int16 *) a) >= *((int16 *) b));
+	return (*((const int16 *) a) >= *((const int16 *) b));
 }
 static bool
 gbt_int2eq(const void *a, const void *b)
 {
-	return (*((int16 *) a) == *((int16 *) b));
+	return (*((const int16 *) a) == *((const int16 *) b));
 }
 static bool
 gbt_int2le(const void *a, const void *b)
 {
-	return (*((int16 *) a) <= *((int16 *) b));
+	return (*((const int16 *) a) <= *((const int16 *) b));
 }
 static bool
 gbt_int2lt(const void *a, const void *b)
 {
-	return (*((int16 *) a) < *((int16 *) b));
+	return (*((const int16 *) a) < *((const int16 *) b));
 }
 
 static int
 gbt_int2key_cmp(const void *a, const void *b)
 {
-	int16KEY   *ia = (int16KEY *) (((Nsrt *) a)->t);
-	int16KEY   *ib = (int16KEY *) (((Nsrt *) b)->t);
+	int16KEY   *ia = (int16KEY *) (((const Nsrt *) a)->t);
+	int16KEY   *ib = (int16KEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_int4.c b/contrib/btree_gist/btree_int4.c
index 9e1362cc2f312590cf15b1a5c760474f27de2305..426f23f3fe09f039252c75fe9c038f694ec46338 100644
--- a/contrib/btree_gist/btree_int4.c
+++ b/contrib/btree_gist/btree_int4.c
@@ -35,34 +35,34 @@ Datum		gbt_int4_same(PG_FUNCTION_ARGS);
 static bool
 gbt_int4gt(const void *a, const void *b)
 {
-	return (*((int32 *) a) > *((int32 *) b));
+	return (*((const int32 *) a) > *((const int32 *) b));
 }
 static bool
 gbt_int4ge(const void *a, const void *b)
 {
-	return (*((int32 *) a) >= *((int32 *) b));
+	return (*((const int32 *) a) >= *((const int32 *) b));
 }
 static bool
 gbt_int4eq(const void *a, const void *b)
 {
-	return (*((int32 *) a) == *((int32 *) b));
+	return (*((const int32 *) a) == *((const int32 *) b));
 }
 static bool
 gbt_int4le(const void *a, const void *b)
 {
-	return (*((int32 *) a) <= *((int32 *) b));
+	return (*((const int32 *) a) <= *((const int32 *) b));
 }
 static bool
 gbt_int4lt(const void *a, const void *b)
 {
-	return (*((int32 *) a) < *((int32 *) b));
+	return (*((const int32 *) a) < *((const int32 *) b));
 }
 
 static int
 gbt_int4key_cmp(const void *a, const void *b)
 {
-	int32KEY   *ia = (int32KEY *) (((Nsrt *) a)->t);
-	int32KEY   *ib = (int32KEY *) (((Nsrt *) b)->t);
+	int32KEY   *ia = (int32KEY *) (((const Nsrt *) a)->t);
+	int32KEY   *ib = (int32KEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_int8.c b/contrib/btree_gist/btree_int8.c
index 45f5379a3e0526c616fd9694841ca661478c23ab..c05d8687fd3e8b79855327f844033b63c73cdd69 100644
--- a/contrib/btree_gist/btree_int8.c
+++ b/contrib/btree_gist/btree_int8.c
@@ -35,34 +35,34 @@ Datum		gbt_int8_same(PG_FUNCTION_ARGS);
 static bool
 gbt_int8gt(const void *a, const void *b)
 {
-	return (*((int64 *) a) > *((int64 *) b));
+	return (*((const int64 *) a) > *((const int64 *) b));
 }
 static bool
 gbt_int8ge(const void *a, const void *b)
 {
-	return (*((int64 *) a) >= *((int64 *) b));
+	return (*((const int64 *) a) >= *((const int64 *) b));
 }
 static bool
 gbt_int8eq(const void *a, const void *b)
 {
-	return (*((int64 *) a) == *((int64 *) b));
+	return (*((const int64 *) a) == *((const int64 *) b));
 }
 static bool
 gbt_int8le(const void *a, const void *b)
 {
-	return (*((int64 *) a) <= *((int64 *) b));
+	return (*((const int64 *) a) <= *((const int64 *) b));
 }
 static bool
 gbt_int8lt(const void *a, const void *b)
 {
-	return (*((int64 *) a) < *((int64 *) b));
+	return (*((const int64 *) a) < *((const int64 *) b));
 }
 
 static int
 gbt_int8key_cmp(const void *a, const void *b)
 {
-	int64KEY   *ia = (int64KEY *) (((Nsrt *) a)->t);
-	int64KEY   *ib = (int64KEY *) (((Nsrt *) b)->t);
+	int64KEY   *ia = (int64KEY *) (((const Nsrt *) a)->t);
+	int64KEY   *ib = (int64KEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_interval.c b/contrib/btree_gist/btree_interval.c
index b56494684f7348ba60f33c04c3db2e171ca260ea..bb779adf8e55a56952a742e2cebcd5ae784c4def 100644
--- a/contrib/btree_gist/btree_interval.c
+++ b/contrib/btree_gist/btree_interval.c
@@ -69,8 +69,8 @@ gbt_intvlt(const void *a, const void *b)
 static int
 gbt_intvkey_cmp(const void *a, const void *b)
 {
-	intvKEY    *ia = (intvKEY *) (((Nsrt *) a)->t);
-	intvKEY    *ib = (intvKEY *) (((Nsrt *) b)->t);
+	intvKEY    *ia = (intvKEY *) (((const Nsrt *) a)->t);
+	intvKEY    *ib = (intvKEY *) (((const Nsrt *) b)->t);
 	int			res;
 
 	res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
@@ -90,7 +90,7 @@ intr2num(const Interval *i)
 static float8
 gbt_intv_dist(const void *a, const void *b)
 {
-	return (float8) Abs(intr2num((Interval *) a) - intr2num((Interval *) b));
+	return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
 }
 
 /*
diff --git a/contrib/btree_gist/btree_macaddr.c b/contrib/btree_gist/btree_macaddr.c
index 2a223361b8d96ccb0f37371a05a915bf50e9cab3..31125beda61479dce6f389445b16d7aa28fc9e38 100644
--- a/contrib/btree_gist/btree_macaddr.c
+++ b/contrib/btree_gist/btree_macaddr.c
@@ -65,8 +65,8 @@ gbt_macadlt(const void *a, const void *b)
 static int
 gbt_macadkey_cmp(const void *a, const void *b)
 {
-	macKEY	   *ia = (macKEY *) (((Nsrt *) a)->t);
-	macKEY	   *ib = (macKEY *) (((Nsrt *) b)->t);
+	macKEY	   *ia = (macKEY *) (((const Nsrt *) a)->t);
+	macKEY	   *ib = (macKEY *) (((const Nsrt *) b)->t);
 	int			res;
 
 	res = DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->lower), MacaddrPGetDatum(&ib->lower)));
diff --git a/contrib/btree_gist/btree_oid.c b/contrib/btree_gist/btree_oid.c
index 7b4de5726dca595b799dbeda880a06afdad94087..e80a23c0b1a3996d134924e4f0dd098f5b3bada8 100644
--- a/contrib/btree_gist/btree_oid.c
+++ b/contrib/btree_gist/btree_oid.c
@@ -35,34 +35,34 @@ Datum		gbt_oid_same(PG_FUNCTION_ARGS);
 static bool
 gbt_oidgt(const void *a, const void *b)
 {
-	return (*((Oid *) a) > *((Oid *) b));
+	return (*((const Oid *) a) > *((const Oid *) b));
 }
 static bool
 gbt_oidge(const void *a, const void *b)
 {
-	return (*((Oid *) a) >= *((Oid *) b));
+	return (*((const Oid *) a) >= *((const Oid *) b));
 }
 static bool
 gbt_oideq(const void *a, const void *b)
 {
-	return (*((Oid *) a) == *((Oid *) b));
+	return (*((const Oid *) a) == *((const Oid *) b));
 }
 static bool
 gbt_oidle(const void *a, const void *b)
 {
-	return (*((Oid *) a) <= *((Oid *) b));
+	return (*((const Oid *) a) <= *((const Oid *) b));
 }
 static bool
 gbt_oidlt(const void *a, const void *b)
 {
-	return (*((Oid *) a) < *((Oid *) b));
+	return (*((const Oid *) a) < *((const Oid *) b));
 }
 
 static int
 gbt_oidkey_cmp(const void *a, const void *b)
 {
-	oidKEY	   *ia = (oidKEY *) (((Nsrt *) a)->t);
-	oidKEY	   *ib = (oidKEY *) (((Nsrt *) b)->t);
+	oidKEY	   *ia = (oidKEY *) (((const Nsrt *) a)->t);
+	oidKEY	   *ib = (oidKEY *) (((const Nsrt *) b)->t);
 
 	if (ia->lower == ib->lower)
 	{
diff --git a/contrib/btree_gist/btree_time.c b/contrib/btree_gist/btree_time.c
index c7f37d42302d4631b0e87f5860f2d92fcc0d20d2..a148e5e120bfb8402e99c13ec9bf199a61eaac36 100644
--- a/contrib/btree_gist/btree_time.c
+++ b/contrib/btree_gist/btree_time.c
@@ -105,8 +105,8 @@ gbt_timelt(const void *a, const void *b)
 static int
 gbt_timekey_cmp(const void *a, const void *b)
 {
-	timeKEY    *ia = (timeKEY *) (((Nsrt *) a)->t);
-	timeKEY    *ib = (timeKEY *) (((Nsrt *) b)->t);
+	timeKEY    *ia = (timeKEY *) (((const Nsrt *) a)->t);
+	timeKEY    *ib = (timeKEY *) (((const Nsrt *) b)->t);
 	int			res;
 
 	res = DatumGetInt32(DirectFunctionCall2(time_cmp, TimeADTGetDatumFast(ia->lower), TimeADTGetDatumFast(ib->lower)));
diff --git a/contrib/btree_gist/btree_ts.c b/contrib/btree_gist/btree_ts.c
index 4badb80bd2fd8e71bb5be0e7db28e0cca289e764..05609232d250322e6575a66c639e316a3850593e 100644
--- a/contrib/btree_gist/btree_ts.c
+++ b/contrib/btree_gist/btree_ts.c
@@ -106,8 +106,8 @@ gbt_tslt(const void *a, const void *b)
 static int
 gbt_tskey_cmp(const void *a, const void *b)
 {
-	tsKEY	   *ia = (tsKEY *) (((Nsrt *) a)->t);
-	tsKEY	   *ib = (tsKEY *) (((Nsrt *) b)->t);
+	tsKEY	   *ia = (tsKEY *) (((const Nsrt *) a)->t);
+	tsKEY	   *ib = (tsKEY *) (((const Nsrt *) b)->t);
 	int			res;
 
 	res = DatumGetInt32(DirectFunctionCall2(timestamp_cmp, TimestampGetDatumFast(ia->lower), TimestampGetDatumFast(ib->lower)));
diff --git a/contrib/dict_xsyn/dict_xsyn.c b/contrib/dict_xsyn/dict_xsyn.c
index a6b413bb554f359ae1cf6e00739e3d8a6abfa450..3c05a1ea908639d5a5b8b763a8af25cbe3aa9d53 100644
--- a/contrib/dict_xsyn/dict_xsyn.c
+++ b/contrib/dict_xsyn/dict_xsyn.c
@@ -69,7 +69,7 @@ find_word(char *in, char **end)
 static int
 compare_syn(const void *a, const void *b)
 {
-	return strcmp(((Syn *) a)->key, ((Syn *) b)->key);
+	return strcmp(((const Syn *) a)->key, ((const Syn *) b)->key);
 }
 
 static void
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index 800de48247eb69a6dace7312f6a5e5033cdae049..f5c4b71eaff2b8af252c9c5ac6986d55a2919e0c 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -322,7 +322,7 @@ typedef struct
 static int
 comparecost(const void *a, const void *b)
 {
-	return ((SPLITCOST *) a)->cost - ((SPLITCOST *) b)->cost;
+	return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
 }
 
 
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index fc39bebc58f7b2de4936c067412423523cbebe2d..0eb48cf580634feb6a18218709b2cd947e2b0e6d 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -276,24 +276,25 @@ parse_hstore(HSParser *state)
 static int
 comparePairs(const void *a, const void *b)
 {
-	if (((Pairs *) a)->keylen == ((Pairs *) b)->keylen)
+	const Pairs *pa = a;
+	const Pairs *pb = b;
+
+	if (pa->keylen == pb->keylen)
 	{
-		int			res = memcmp(((Pairs *) a)->key,
-								 ((Pairs *) b)->key,
-								 ((Pairs *) a)->keylen);
+		int			res = memcmp(pa->key, pb->key, pa->keylen);
 
 		if (res)
 			return res;
 
 		/* guarantee that needfree will be later */
-		if (((Pairs *) b)->needfree == ((Pairs *) a)->needfree)
+		if (pb->needfree == pa->needfree)
 			return 0;
-		else if (((Pairs *) a)->needfree)
+		else if (pa->needfree)
 			return 1;
 		else
 			return -1;
 	}
-	return (((Pairs *) a)->keylen > ((Pairs *) b)->keylen) ? 1 : -1;
+	return (pa->keylen > pb->keylen) ? 1 : -1;
 }
 
 /*
diff --git a/contrib/intarray/_int_gist.c b/contrib/intarray/_int_gist.c
index 0a173bfcb66cac57f63f1b866112c38141e8b541..0123906be8849aa554c8fa8799c63ecf939c45cb 100644
--- a/contrib/intarray/_int_gist.c
+++ b/contrib/intarray/_int_gist.c
@@ -357,10 +357,10 @@ typedef struct
 static int
 comparecost(const void *a, const void *b)
 {
-	if (((SPLITCOST *) a)->cost == ((SPLITCOST *) b)->cost)
+	if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
 		return 0;
 	else
-		return (((SPLITCOST *) a)->cost > ((SPLITCOST *) b)->cost) ? 1 : -1;
+		return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
 }
 
 /*
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index bfc55501dbcdd6f9ba361808b69a712207cb0031..79f018d333b1d6810aa7fd2996c158b41b0263fb 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -388,15 +388,15 @@ int_to_intset(int32 n)
 int
 compASC(const void *a, const void *b)
 {
-	if (*(int4 *) a == *(int4 *) b)
+	if (*(const int4 *) a == *(const int4 *) b)
 		return 0;
-	return (*(int4 *) a > *(int4 *) b) ? 1 : -1;
+	return (*(const int4 *) a > *(const int4 *) b) ? 1 : -1;
 }
 
 int
 compDESC(const void *a, const void *b)
 {
-	if (*(int4 *) a == *(int4 *) b)
+	if (*(const int4 *) a == *(const int4 *) b)
 		return 0;
-	return (*(int4 *) a < *(int4 *) b) ? 1 : -1;
+	return (*(const int4 *) a < *(const int4 *) b) ? 1 : -1;
 }
diff --git a/contrib/ltree/_ltree_gist.c b/contrib/ltree/_ltree_gist.c
index f03f6332906ff215641a461731e83228df1d067a..8afc2bd540787a4221a196aa3fff0509d73166f0 100644
--- a/contrib/ltree/_ltree_gist.c
+++ b/contrib/ltree/_ltree_gist.c
@@ -280,7 +280,7 @@ typedef struct
 static int
 comparecost(const void *a, const void *b)
 {
-	return ((SPLITCOST *) a)->cost - ((SPLITCOST *) b)->cost;
+	return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
 }
 
 Datum
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index eb4c494ae682aab01333b71716d3e89320460b62..8dc3054e372ac12d4bf4216aa86b65a188b44cbc 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -973,8 +973,8 @@ entry_alloc(pgssHashKey *key)
 static int
 entry_cmp(const void *lhs, const void *rhs)
 {
-	double		l_usage = (*(const pgssEntry **) lhs)->counters.usage;
-	double		r_usage = (*(const pgssEntry **) rhs)->counters.usage;
+	double		l_usage = (*(pgssEntry * const *) lhs)->counters.usage;
+	double		r_usage = (*(pgssEntry * const *) rhs)->counters.usage;
 
 	if (l_usage < r_usage)
 		return -1;
diff --git a/contrib/pg_trgm/trgm.h b/contrib/pg_trgm/trgm.h
index 61de5d89d161bf7e4d9a5965d0211b1447c0c2c3..067f29d4daf85b685bc46496a04cfa35a7182a1a 100644
--- a/contrib/pg_trgm/trgm.h
+++ b/contrib/pg_trgm/trgm.h
@@ -33,7 +33,7 @@
 typedef char trgm[3];
 
 #define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
-#define CMPPCHAR(a,b,i)  CMPCHAR( *(((char*)(a))+i), *(((char*)(b))+i) )
+#define CMPPCHAR(a,b,i)  CMPCHAR( *(((const char*)(a))+i), *(((const char*)(b))+i) )
 #define CMPTRGM(a,b) ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
 
 #define CPTRGM(a,b) do {				\
diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c
index aee060bab737e808e324370a3ae77ab863f78abb..5621e9097751a9d2a888c0817dc1df58fb406817 100644
--- a/contrib/pg_trgm/trgm_gist.c
+++ b/contrib/pg_trgm/trgm_gist.c
@@ -594,10 +594,10 @@ typedef struct
 static int
 comparecost(const void *a, const void *b)
 {
-	if (((SPLITCOST *) a)->cost == ((SPLITCOST *) b)->cost)
+	if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
 		return 0;
 	else
-		return (((SPLITCOST *) a)->cost > ((SPLITCOST *) b)->cost) ? 1 : -1;
+		return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
 }
 
 
diff --git a/contrib/pgcrypto/crypt-blowfish.c b/contrib/pgcrypto/crypt-blowfish.c
index 7cca92b6ab5e49ea3f4ecba0923af4a0a6891c9e..b49747d9265d2aa7179da1419081909387925d66 100644
--- a/contrib/pgcrypto/crypt-blowfish.c
+++ b/contrib/pgcrypto/crypt-blowfish.c
@@ -371,7 +371,7 @@ BF_decode(BF_word *dst, const char *src, int size)
 {
 	unsigned char *dptr = (unsigned char *) dst;
 	unsigned char *end = dptr + size;
-	unsigned char *sptr = (unsigned char *) src;
+	const unsigned char *sptr = (const unsigned char *) src;
 	unsigned int tmp,
 				c1,
 				c2,
@@ -401,8 +401,8 @@ BF_decode(BF_word *dst, const char *src, int size)
 static void
 BF_encode(char *dst, const BF_word *src, int size)
 {
-	unsigned char *sptr = (unsigned char *) src;
-	unsigned char *end = sptr + size;
+	const unsigned char *sptr = (const unsigned char *) src;
+	const unsigned char *end = sptr + size;
 	unsigned char *dptr = (unsigned char *) dst;
 	unsigned int c1,
 				c2;
diff --git a/contrib/pgcrypto/crypt-des.c b/contrib/pgcrypto/crypt-des.c
index e50c1f4a9254fab523f0f1b89c04b5b712ab327a..44475a1b446c8fc903c26ad20de2234982f3a8a7 100644
--- a/contrib/pgcrypto/crypt-des.c
+++ b/contrib/pgcrypto/crypt-des.c
@@ -407,8 +407,8 @@ des_setkey(const char *key)
 	if (!des_initialised)
 		des_init();
 
-	rawkey0 = ntohl(*(uint32 *) key);
-	rawkey1 = ntohl(*(uint32 *) (key + 4));
+	rawkey0 = ntohl(*(const uint32 *) key);
+	rawkey1 = ntohl(*(const uint32 *) (key + 4));
 
 	if ((rawkey0 | rawkey1)
 		&& rawkey0 == old_rawkey0
diff --git a/contrib/pgcrypto/crypt-gensalt.c b/contrib/pgcrypto/crypt-gensalt.c
index 062ab864167e80235ffa52179b0d483d7c81d574..ec2e0fa0250b15bad811ac9d4639c033c619d345 100644
--- a/contrib/pgcrypto/crypt-gensalt.c
+++ b/contrib/pgcrypto/crypt-gensalt.c
@@ -123,8 +123,8 @@ static unsigned char BF_itoa64[64 + 1] =
 static void
 BF_encode(char *dst, const BF_word *src, int size)
 {
-	unsigned char *sptr = (unsigned char *) src;
-	unsigned char *end = sptr + size;
+	const unsigned char *sptr = (const unsigned char *) src;
+	const unsigned char *end = sptr + size;
 	unsigned char *dptr = (unsigned char *) dst;
 	unsigned int c1,
 				c2;
@@ -180,7 +180,7 @@ _crypt_gensalt_blowfish_rn(unsigned long count,
 	output[5] = '0' + count % 10;
 	output[6] = '$';
 
-	BF_encode(&output[7], (BF_word *) input, 16);
+	BF_encode(&output[7], (const BF_word *) input, 16);
 	output[7 + 22] = '\0';
 
 	return output;
diff --git a/contrib/pgcrypto/crypt-md5.c b/contrib/pgcrypto/crypt-md5.c
index 30eb8bf5a2743cc997dea6377953f4e6a68efd30..2215f38d8bf09c36e19a652de1abe872d0d1e43b 100644
--- a/contrib/pgcrypto/crypt-md5.c
+++ b/contrib/pgcrypto/crypt-md5.c
@@ -72,18 +72,18 @@ px_crypt_md5(const char *pw, const char *salt, char *passwd, unsigned dstlen)
 	err = px_find_digest("md5", &ctx1);
 
 	/* The password first, since that is what is most unknown */
-	px_md_update(ctx, (uint8 *) pw, strlen(pw));
+	px_md_update(ctx, (const uint8 *) pw, strlen(pw));
 
 	/* Then our magic string */
 	px_md_update(ctx, (uint8 *) magic, strlen(magic));
 
 	/* Then the raw salt */
-	px_md_update(ctx, (uint8 *) sp, sl);
+	px_md_update(ctx, (const uint8 *) sp, sl);
 
 	/* Then just as many characters of the MD5(pw,salt,pw) */
-	px_md_update(ctx1, (uint8 *) pw, strlen(pw));
-	px_md_update(ctx1, (uint8 *) sp, sl);
-	px_md_update(ctx1, (uint8 *) pw, strlen(pw));
+	px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
+	px_md_update(ctx1, (const uint8 *) sp, sl);
+	px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
 	px_md_finish(ctx1, final);
 	for (pl = strlen(pw); pl > 0; pl -= MD5_SIZE)
 		px_md_update(ctx, final, pl > MD5_SIZE ? MD5_SIZE : pl);
@@ -96,7 +96,7 @@ px_crypt_md5(const char *pw, const char *salt, char *passwd, unsigned dstlen)
 		if (i & 1)
 			px_md_update(ctx, final, 1);
 		else
-			px_md_update(ctx, (uint8 *) pw, 1);
+			px_md_update(ctx, (const uint8 *) pw, 1);
 
 	/* Now make the output string */
 	strcpy(passwd, magic);
@@ -114,20 +114,20 @@ px_crypt_md5(const char *pw, const char *salt, char *passwd, unsigned dstlen)
 	{
 		px_md_reset(ctx1);
 		if (i & 1)
-			px_md_update(ctx1, (uint8 *) pw, strlen(pw));
+			px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
 		else
 			px_md_update(ctx1, final, MD5_SIZE);
 
 		if (i % 3)
-			px_md_update(ctx1, (uint8 *) sp, sl);
+			px_md_update(ctx1, (const uint8 *) sp, sl);
 
 		if (i % 7)
-			px_md_update(ctx1, (uint8 *) pw, strlen(pw));
+			px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
 
 		if (i & 1)
 			px_md_update(ctx1, final, MD5_SIZE);
 		else
-			px_md_update(ctx1, (uint8 *) pw, strlen(pw));
+			px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
 		px_md_finish(ctx1, final);
 	}
 
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 36cb40d4517f7255c4a442eaf73fa0fbffcf4780..18d44c572c755daea2f5ecaf7df060ab43e090ce 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -1362,8 +1362,8 @@ get_next_S(double t, int n, double *stateptr)
 static int
 compare_rows(const void *a, const void *b)
 {
-	HeapTuple	ha = *(HeapTuple *) a;
-	HeapTuple	hb = *(HeapTuple *) b;
+	HeapTuple	ha = *(const HeapTuple *) a;
+	HeapTuple	hb = *(const HeapTuple *) b;
 	BlockNumber ba = ItemPointerGetBlockNumber(&ha->t_self);
 	OffsetNumber oa = ItemPointerGetOffsetNumber(&ha->t_self);
 	BlockNumber bb = ItemPointerGetBlockNumber(&hb->t_self);
@@ -2700,10 +2700,10 @@ compute_scalar_stats(VacAttrStatsP stats,
 static int
 compare_scalars(const void *a, const void *b, void *arg)
 {
-	Datum		da = ((ScalarItem *) a)->value;
-	int			ta = ((ScalarItem *) a)->tupno;
-	Datum		db = ((ScalarItem *) b)->value;
-	int			tb = ((ScalarItem *) b)->tupno;
+	Datum		da = ((const ScalarItem *) a)->value;
+	int			ta = ((const ScalarItem *) a)->tupno;
+	Datum		db = ((const ScalarItem *) b)->value;
+	int			tb = ((const ScalarItem *) b)->tupno;
 	CompareScalarsContext *cxt = (CompareScalarsContext *) arg;
 	int32		compare;
 
@@ -2734,8 +2734,8 @@ compare_scalars(const void *a, const void *b, void *arg)
 static int
 compare_mcvs(const void *a, const void *b)
 {
-	int			da = ((ScalarMCVItem *) a)->first;
-	int			db = ((ScalarMCVItem *) b)->first;
+	int			da = ((const ScalarMCVItem *) a)->first;
+	int			db = ((const ScalarMCVItem *) b)->first;
 
 	return da - db;
 }
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 528a3a1f5397f43ae0cc75e9643c7253160bda93..a9c1980e5d53a6d5222282f68f110114a6c7a0ae 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -296,7 +296,7 @@ static char *limit_printout_length(const char *str);
 static void SendCopyBegin(CopyState cstate);
 static void ReceiveCopyBegin(CopyState cstate);
 static void SendCopyEnd(CopyState cstate);
-static void CopySendData(CopyState cstate, void *databuf, int datasize);
+static void CopySendData(CopyState cstate, const void *databuf, int datasize);
 static void CopySendString(CopyState cstate, const char *str);
 static void CopySendChar(CopyState cstate, char c);
 static void CopySendEndOfRow(CopyState cstate);
@@ -431,9 +431,9 @@ SendCopyEnd(CopyState cstate)
  *----------
  */
 static void
-CopySendData(CopyState cstate, void *databuf, int datasize)
+CopySendData(CopyState cstate, const void *databuf, int datasize)
 {
-	appendBinaryStringInfo(cstate->fe_msgbuf, (char *) databuf, datasize);
+	appendBinaryStringInfo(cstate->fe_msgbuf, databuf, datasize);
 }
 
 static void
@@ -1535,7 +1535,7 @@ CopyTo(CopyState cstate)
 		int32		tmp;
 
 		/* Signature */
-		CopySendData(cstate, (char *) BinarySignature, 11);
+		CopySendData(cstate, BinarySignature, 11);
 		/* Flags field */
 		tmp = 0;
 		if (cstate->oids)
diff --git a/src/backend/libpq/ip.c b/src/backend/libpq/ip.c
index cc6c2abac683780082b6e7b2d8345f45c05df16d..71fd9e76a29b889d0172e0113d673ec4aaabfa7c 100644
--- a/src/backend/libpq/ip.c
+++ b/src/backend/libpq/ip.c
@@ -283,14 +283,14 @@ pg_range_sockaddr(const struct sockaddr_storage * addr,
 				  const struct sockaddr_storage * netmask)
 {
 	if (addr->ss_family == AF_INET)
-		return range_sockaddr_AF_INET((struct sockaddr_in *) addr,
-									  (struct sockaddr_in *) netaddr,
-									  (struct sockaddr_in *) netmask);
+		return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
+									  (const struct sockaddr_in *) netaddr,
+									  (const struct sockaddr_in *) netmask);
 #ifdef HAVE_IPV6
 	else if (addr->ss_family == AF_INET6)
-		return range_sockaddr_AF_INET6((struct sockaddr_in6 *) addr,
-									   (struct sockaddr_in6 *) netaddr,
-									   (struct sockaddr_in6 *) netmask);
+		return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
+									   (const struct sockaddr_in6 *) netaddr,
+									   (const struct sockaddr_in6 *) netmask);
 #endif
 	else
 		return 0;
diff --git a/src/backend/libpq/md5.c b/src/backend/libpq/md5.c
index d40c37b6375b0d15f1b044eebba906c522fffe75..dd8c5ae50b3d01d3e6896a16f9d71c1e47085783 100644
--- a/src/backend/libpq/md5.c
+++ b/src/backend/libpq/md5.c
@@ -33,7 +33,7 @@
  *	when it is no longer needed.
  */
 static uint8 *
-createPaddedCopyWithLength(uint8 *b, uint32 *l)
+createPaddedCopyWithLength(const uint8 *b, uint32 *l)
 {
 	uint8	   *ret;
 	uint32		q;
@@ -182,7 +182,7 @@ doTheRounds(uint32 X[16], uint32 state[4])
 }
 
 static int
-calculateDigestFromBuffer(uint8 *b, uint32 len, uint8 sum[16])
+calculateDigestFromBuffer(const uint8 *b, uint32 len, uint8 sum[16])
 {
 	register uint32 i,
 				j,
@@ -291,7 +291,7 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum)
 {
 	uint8		sum[16];
 
-	if (!calculateDigestFromBuffer((uint8 *) buff, len, sum))
+	if (!calculateDigestFromBuffer(buff, len, sum))
 		return false;
 
 	bytesToHex(sum, hexsum);
@@ -301,7 +301,7 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum)
 bool
 pg_md5_binary(const void *buff, size_t len, void *outbuf)
 {
-	if (!calculateDigestFromBuffer((uint8 *) buff, len, outbuf))
+	if (!calculateDigestFromBuffer(buff, len, outbuf))
 		return false;
 	return true;
 }
diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c
index 4d2eccf81790561eb25938b663a5470e9a37db5c..9070cd2d739abecc60db22efa1cfb842ce20b417 100644
--- a/src/backend/nodes/makefuncs.c
+++ b/src/backend/nodes/makefuncs.c
@@ -45,7 +45,7 @@ makeA_Expr(A_Expr_Kind kind, List *name,
  *		As above, given a simple (unqualified) operator name
  */
 A_Expr *
-makeSimpleA_Expr(A_Expr_Kind kind, const char *name,
+makeSimpleA_Expr(A_Expr_Kind kind, char *name,
 				 Node *lexpr, Node *rexpr, int location)
 {
 	A_Expr	   *a = makeNode(A_Expr);
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 2363ec22a43fd4ef4da170a35ce648266a81ecfb..a2e9e21885b7e4271e704a65b87293eb98334031 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -1011,8 +1011,8 @@ tbm_lossify(TIDBitmap *tbm)
 static int
 tbm_comparator(const void *left, const void *right)
 {
-	BlockNumber l = (*((const PagetableEntry **) left))->blockno;
-	BlockNumber r = (*((const PagetableEntry **) right))->blockno;
+	BlockNumber l = (*((PagetableEntry * const *) left))->blockno;
+	BlockNumber r = (*((PagetableEntry * const *) right))->blockno;
 
 	if (l < r)
 		return -1;
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 4e211977b28b3aa6643f4b9d6060bf58cf640071..3b71232b882ae20753e24f91ebbd882d672e5b32 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -1028,10 +1028,10 @@ rebuild_database_list(Oid newdb)
 static int
 db_comparator(const void *a, const void *b)
 {
-	if (((avl_dbase *) a)->adl_score == ((avl_dbase *) b)->adl_score)
+	if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
 		return 0;
 	else
-		return (((avl_dbase *) a)->adl_score < ((avl_dbase *) b)->adl_score) ? 1 : -1;
+		return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
 }
 
 /*
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 3264a65985947e4a06fead6dd78f5ec3c5604211..a1ce5d8b8a236635aade72885b568bc6056e5241 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -937,7 +937,7 @@ SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
 	 * anytime.
 	 */
 	lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
-												(void *) locktag,
+												(const void *) locktag,
 												hashcode,
 												HASH_ENTER_NULL,
 												&found);
@@ -1673,7 +1673,7 @@ LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
 
 		Assert(FastPathTag(locktag) && FastPathWeakMode(lockmode));
 		lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
-													(void *) locktag,
+													(const void *) locktag,
 													locallock->hashcode,
 													HASH_FIND,
 													&found);
@@ -2505,7 +2505,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode)
 	LWLockAcquire(partitionLock, LW_SHARED);
 
 	lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
-												(void *) locktag,
+												(const void *) locktag,
 												hashcode,
 												HASH_FIND,
 												NULL);
diff --git a/src/backend/tsearch/dict_synonym.c b/src/backend/tsearch/dict_synonym.c
index 0788b3da8475a017477d49a3153136d5433a9025..afe4f5212e7b436f1284ac3a77b8f78e1245794f 100644
--- a/src/backend/tsearch/dict_synonym.c
+++ b/src/backend/tsearch/dict_synonym.c
@@ -83,7 +83,7 @@ findwrd(char *in, char **end, uint16 *flags)
 static int
 compareSyn(const void *a, const void *b)
 {
-	return strcmp(((Syn *) a)->in, ((Syn *) b)->in);
+	return strcmp(((const Syn *) a)->in, ((const Syn *) b)->in);
 }
 
 
diff --git a/src/backend/tsearch/dict_thesaurus.c b/src/backend/tsearch/dict_thesaurus.c
index 509420f70d0a68614f8ea270310eb107d402b127..e0d21c2ad61fbd075570c0238f59d5978d95dba0 100644
--- a/src/backend/tsearch/dict_thesaurus.c
+++ b/src/backend/tsearch/dict_thesaurus.c
@@ -348,7 +348,7 @@ cmpLexemeInfo(LexemeInfo *a, LexemeInfo *b)
 }
 
 static int
-cmpLexeme(TheLexeme *a, TheLexeme *b)
+cmpLexeme(const TheLexeme *a, const TheLexeme *b)
 {
 	if (a->lexeme == NULL)
 	{
@@ -366,14 +366,14 @@ cmpLexeme(TheLexeme *a, TheLexeme *b)
 static int
 cmpLexemeQ(const void *a, const void *b)
 {
-	return cmpLexeme((TheLexeme *) a, (TheLexeme *) b);
+	return cmpLexeme((const TheLexeme *) a, (const TheLexeme *) b);
 }
 
 static int
 cmpTheLexeme(const void *a, const void *b)
 {
-	TheLexeme  *la = (TheLexeme *) a;
-	TheLexeme  *lb = (TheLexeme *) b;
+	const TheLexeme  *la = (const TheLexeme *) a;
+	const TheLexeme  *lb = (const TheLexeme *) b;
 	int			res;
 
 	if ((res = cmpLexeme(la, lb)) != 0)
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index be1663cd8810e5ae26b07cf6ef3e7c592180513a..c9bc748b682b9affaa8b46726aed97ecec83d071 100644
--- a/src/backend/tsearch/spell.c
+++ b/src/backend/tsearch/spell.c
@@ -140,7 +140,7 @@ lowerstr_ctx(IspellDict *Conf, const char *src)
 #define MAXNORMLEN 256
 
 #define STRNCMP(s,p)	strncmp( (s), (p), strlen(p) )
-#define GETWCHAR(W,L,N,T) ( ((uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
+#define GETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
 #define GETCHAR(A,N,T)	  GETWCHAR( (A)->repl, (A)->replen, N, T )
 
 static char *VoidString = "";
@@ -148,12 +148,12 @@ static char *VoidString = "";
 static int
 cmpspell(const void *s1, const void *s2)
 {
-	return (strcmp((*(const SPELL **) s1)->word, (*(const SPELL **) s2)->word));
+	return (strcmp((*(SPELL * const *) s1)->word, (*(SPELL * const *) s2)->word));
 }
 static int
 cmpspellaffix(const void *s1, const void *s2)
 {
-	return (strncmp((*(const SPELL **) s1)->p.flag, (*(const SPELL **) s2)->p.flag, MAXFLAGLEN));
+	return (strncmp((*(SPELL * const *) s1)->p.flag, (*(SPELL * const *) s2)->p.flag, MAXFLAGLEN));
 }
 
 static char *
@@ -332,7 +332,7 @@ FindWord(IspellDict *Conf, const char *word, int affixflag, int flag)
 	SPNodeData *StopLow,
 			   *StopHigh,
 			   *StopMiddle;
-	uint8	   *ptr = (uint8 *) word;
+	const uint8 *ptr = (const uint8 *) word;
 
 	flag &= FF_DICTFLAGMASK;
 
diff --git a/src/backend/tsearch/to_tsany.c b/src/backend/tsearch/to_tsany.c
index d143aaebfacfc8491b23234d341a7a8e2cd54e22..3f194ec6bfd2b8311a2a75f6bab3cf29e6445cc9 100644
--- a/src/backend/tsearch/to_tsany.c
+++ b/src/backend/tsearch/to_tsany.c
@@ -33,16 +33,16 @@ compareWORD(const void *a, const void *b)
 	int			res;
 
 	res = tsCompareString(
-						  ((ParsedWord *) a)->word, ((ParsedWord *) a)->len,
-						  ((ParsedWord *) b)->word, ((ParsedWord *) b)->len,
+						  ((const ParsedWord *) a)->word, ((const ParsedWord *) a)->len,
+						  ((const ParsedWord *) b)->word, ((const ParsedWord *) b)->len,
 						  false);
 
 	if (res == 0)
 	{
-		if (((ParsedWord *) a)->pos.pos == ((ParsedWord *) b)->pos.pos)
+		if (((const ParsedWord *) a)->pos.pos == ((const ParsedWord *) b)->pos.pos)
 			return 0;
 
-		res = (((ParsedWord *) a)->pos.pos > ((ParsedWord *) b)->pos.pos) ? 1 : -1;
+		res = (((const ParsedWord *) a)->pos.pos > ((const ParsedWord *) b)->pos.pos) ? 1 : -1;
 	}
 
 	return res;
diff --git a/src/backend/tsearch/ts_utils.c b/src/backend/tsearch/ts_utils.c
index c728ee2d2acc078ae6da4f8b91d8de3ea7b42d8c..5c4ace2bcf1536b0a11ace3af4e6b64c2206f0f5 100644
--- a/src/backend/tsearch/ts_utils.c
+++ b/src/backend/tsearch/ts_utils.c
@@ -62,7 +62,7 @@ get_tsearch_config_filename(const char *basename,
 static int
 comparestr(const void *a, const void *b)
 {
-	return strcmp(*(char **) a, *(char **) b);
+	return strcmp(*(char * const *) a, *(char * const *) b);
 }
 
 /*
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index 1ac2b17237fb69477de19644a2a23964bb940cd4..72f4a784d27df504fabfd7739135733486d8f197 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -131,8 +131,8 @@ gtsvectorout(PG_FUNCTION_ARGS)
 static int
 compareint(const void *va, const void *vb)
 {
-	int4		a = *((int4 *) va);
-	int4		b = *((int4 *) vb);
+	int4		a = *((const int4 *) va);
+	int4		b = *((const int4 *) vb);
 
 	if (a == b)
 		return 0;
@@ -593,8 +593,8 @@ typedef struct
 static int
 comparecost(const void *va, const void *vb)
 {
-	SPLITCOST  *a = (SPLITCOST *) va;
-	SPLITCOST  *b = (SPLITCOST *) vb;
+	const SPLITCOST  *a = (const SPLITCOST *) va;
+	const SPLITCOST  *b = (const SPLITCOST *) vb;
 
 	if (a->cost == b->cost)
 		return 0;
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index bef86036dc13dc8e100c12934e52f984802458db..d4468d617a4821e4a78e9cb4972d2275c50ae340 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -156,10 +156,10 @@ typedef struct
 static int
 comparecost(const void *a, const void *b)
 {
-	if (((SPLITCOST *) a)->cost == ((SPLITCOST *) b)->cost)
+	if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
 		return 0;
 	else
-		return (((SPLITCOST *) a)->cost > ((SPLITCOST *) b)->cost) ? 1 : -1;
+		return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
 }
 
 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsrank.c b/src/backend/utils/adt/tsrank.c
index 564d855817a1d1f0a75686f713c264b3a1c588a7..5a50b771fba68703595bc4abf4c1032de46f2dc8 100644
--- a/src/backend/utils/adt/tsrank.c
+++ b/src/backend/utils/adt/tsrank.c
@@ -134,8 +134,8 @@ static int
 compareQueryOperand(const void *a, const void *b, void *arg)
 {
 	char	   *operand = (char *) arg;
-	QueryOperand *qa = (*(QueryOperand **) a);
-	QueryOperand *qb = (*(QueryOperand **) b);
+	QueryOperand *qa = (*(QueryOperand * const *) a);
+	QueryOperand *qb = (*(QueryOperand * const *) b);
 
 	return tsCompareString(operand + qa->distance, qa->length,
 						   operand + qb->distance, qb->length,
@@ -498,8 +498,8 @@ typedef struct
 static int
 compareDocR(const void *va, const void *vb)
 {
-	DocRepresentation *a = (DocRepresentation *) va;
-	DocRepresentation *b = (DocRepresentation *) vb;
+	const DocRepresentation *a = (const DocRepresentation *) va;
+	const DocRepresentation *b = (const DocRepresentation *) vb;
 
 	if (a->pos == b->pos)
 		return 0;
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 99b978c42c08089f3675f6e675f419b2c935a3ae..93ff366cd744ae01fafa294c2dbb6fa4d6153866 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -396,7 +396,7 @@ cstring_to_xmltype(const char *string)
 static xmltype *
 xmlBuffer_to_xmltype(xmlBufferPtr buf)
 {
-	return (xmltype *) cstring_to_text_with_len((char *) xmlBufferContent(buf),
+	return (xmltype *) cstring_to_text_with_len((const char *) xmlBufferContent(buf),
 												xmlBufferLength(buf));
 }
 #endif
@@ -1267,7 +1267,7 @@ static bool
 print_xml_decl(StringInfo buf, const xmlChar *version,
 			   pg_enc encoding, int standalone)
 {
-	if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
+	if ((version && strcmp((const char *) version, PG_XML_DEFAULT_VERSION) != 0)
 		|| (encoding && encoding != PG_UTF8)
 		|| standalone != -1)
 	{
diff --git a/src/backend/utils/mb/conv.c b/src/backend/utils/mb/conv.c
index 8af157b2d5eef546a2131ed967a71c2bb540b341..0d7f471a15b14f44af87ab8de2014090e2c5717b 100644
--- a/src/backend/utils/mb/conv.c
+++ b/src/backend/utils/mb/conv.c
@@ -247,8 +247,8 @@ compare1(const void *p1, const void *p2)
 	uint32		v1,
 				v2;
 
-	v1 = *(uint32 *) p1;
-	v2 = ((pg_utf_to_local *) p2)->utf;
+	v1 = *(const uint32 *) p1;
+	v2 = ((const pg_utf_to_local *) p2)->utf;
 	return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
 }
 
@@ -262,8 +262,8 @@ compare2(const void *p1, const void *p2)
 	uint32		v1,
 				v2;
 
-	v1 = *(uint32 *) p1;
-	v2 = ((pg_local_to_utf *) p2)->code;
+	v1 = *(const uint32 *) p1;
+	v2 = ((const pg_local_to_utf *) p2)->code;
 	return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
 }
 
@@ -279,10 +279,10 @@ compare3(const void *p1, const void *p2)
 				d1,
 				d2;
 
-	s1 = *(uint32 *) p1;
-	s2 = *((uint32 *) p1 + 1);
-	d1 = ((pg_utf_to_local_combined *) p2)->utf1;
-	d2 = ((pg_utf_to_local_combined *) p2)->utf2;
+	s1 = *(const uint32 *) p1;
+	s2 = *((const uint32 *) p1 + 1);
+	d1 = ((const pg_utf_to_local_combined *) p2)->utf1;
+	d2 = ((const pg_utf_to_local_combined *) p2)->utf2;
 	return (s1 > d1 || (s1 == d1 && s2 > d2)) ? 1 : ((s1 == d1 && s2 == d2) ? 0 : -1);
 }
 
@@ -296,8 +296,8 @@ compare4(const void *p1, const void *p2)
 	uint32		v1,
 				v2;
 
-	v1 = *(uint32 *) p1;
-	v2 = ((pg_local_to_utf_combined *) p2)->code;
+	v1 = *(const uint32 *) p1;
+	v2 = ((const pg_local_to_utf_combined *) p2)->code;
 	return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
 }
 
diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c
index a631f64c36f4768031b5beb3198a03921939f3c4..7eff05fcedfd0c419043c134266cefd2e0143871 100644
--- a/src/bin/pg_dump/common.c
+++ b/src/bin/pg_dump/common.c
@@ -650,8 +650,8 @@ buildIndexArray(void *objArray, int numObjs, Size objSize)
 static int
 DOCatalogIdCompare(const void *p1, const void *p2)
 {
-	DumpableObject *obj1 = *(DumpableObject **) p1;
-	DumpableObject *obj2 = *(DumpableObject **) p2;
+	const DumpableObject *obj1 = *(DumpableObject * const *) p1;
+	const DumpableObject *obj2 = *(DumpableObject * const *) p2;
 	int			cmpval;
 
 	/*
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 1611551638b76aac5a7d15fa70dc442b426e61bc..f47af264cba07952dea3507b6de8afa232339ae6 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -1393,7 +1393,7 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
 	}
 	else if (AH->gzOut)
 	{
-		res = GZWRITE((void *) ptr, size, nmemb, AH->OF);
+		res = GZWRITE(ptr, size, nmemb, AH->OF);
 		if (res != (nmemb * size))
 			die_horribly(AH, modulename, "could not write to output file: %s\n", strerror(errno));
 		return res;
@@ -1416,7 +1416,7 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
 			return ExecuteSqlCommandBuf(AH, (const char *) ptr, size * nmemb);
 		else
 		{
-			res = fwrite((void *) ptr, size, nmemb, AH->OF);
+			res = fwrite(ptr, size, nmemb, AH->OF);
 			if (res != nmemb)
 				die_horribly(AH, modulename, "could not write to output file: %s\n",
 							 strerror(errno));
diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c
index abc93b140308e55f1a2d33cf06fc06f70048648d..afd53bff8e26407faf501d7df8fd895d1250d3be 100644
--- a/src/bin/pg_dump/pg_backup_files.c
+++ b/src/bin/pg_dump/pg_backup_files.c
@@ -268,7 +268,7 @@ _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
 {
 	lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData;
 
-	GZWRITE((void *) data, 1, dLen, tctx->FH);
+	GZWRITE(data, 1, dLen, tctx->FH);
 
 	return dLen;
 }
diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c
index 45aab1e2364ec44d99c276089be7c2033a7e8e7b..4642132d7152188c98803f3cc02555b611e6ca35 100644
--- a/src/bin/pg_dump/pg_backup_tar.c
+++ b/src/bin/pg_dump/pg_backup_tar.c
@@ -581,7 +581,7 @@ tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
 	size_t		res;
 
 	if (th->zFH != NULL)
-		res = GZWRITE((void *) buf, 1, len, th->zFH);
+		res = GZWRITE(buf, 1, len, th->zFH);
 	else
 		res = fwrite(buf, 1, len, th->nFH);
 
@@ -598,7 +598,7 @@ _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
 {
 	lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData;
 
-	dLen = tarWrite((void *) data, dLen, tctx->TH);
+	dLen = tarWrite(data, dLen, tctx->TH);
 
 	return dLen;
 }
@@ -797,7 +797,7 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
 	lclContext *ctx = (lclContext *) AH->formatData;
 	size_t		res;
 
-	res = tarWrite((void *) buf, len, ctx->FH);
+	res = tarWrite(buf, len, ctx->FH);
 	ctx->filePos += res;
 	return res;
 }
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index 963f734dce2ff012f95b94b2023739d6a1327eb1..efde0d0026bd87c0ad099044f25b61461c202b14 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -138,8 +138,8 @@ sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs)
 static int
 DOTypeNameCompare(const void *p1, const void *p2)
 {
-	DumpableObject *obj1 = *(DumpableObject **) p1;
-	DumpableObject *obj2 = *(DumpableObject **) p2;
+	DumpableObject *obj1 = *(DumpableObject * const *) p1;
+	DumpableObject *obj2 = *(DumpableObject * const *) p2;
 	int			cmpval;
 
 	/* Sort by type */
@@ -170,8 +170,8 @@ DOTypeNameCompare(const void *p1, const void *p2)
 	/* To have a stable sort order, break ties for some object types */
 	if (obj1->objType == DO_FUNC || obj1->objType == DO_AGG)
 	{
-		FuncInfo   *fobj1 = *(FuncInfo **) p1;
-		FuncInfo   *fobj2 = *(FuncInfo **) p2;
+		FuncInfo   *fobj1 = *(FuncInfo * const *) p1;
+		FuncInfo   *fobj2 = *(FuncInfo * const *) p2;
 
 		cmpval = fobj1->nargs - fobj2->nargs;
 		if (cmpval != 0)
@@ -200,8 +200,8 @@ sortDumpableObjectsByTypeOid(DumpableObject **objs, int numObjs)
 static int
 DOTypeOidCompare(const void *p1, const void *p2)
 {
-	DumpableObject *obj1 = *(DumpableObject **) p1;
-	DumpableObject *obj2 = *(DumpableObject **) p2;
+	DumpableObject *obj1 = *(DumpableObject * const *) p1;
+	DumpableObject *obj2 = *(DumpableObject * const *) p2;
 	int			cmpval;
 
 	cmpval = oldObjectTypePriority[obj1->objType] -
diff --git a/src/bin/psql/mbprint.c b/src/bin/psql/mbprint.c
index ff9293a07a7f06832fafd36fbaa50215c58303fc..e5fcb387356b241396d75fa47deabbf600f3e09e 100644
--- a/src/bin/psql/mbprint.c
+++ b/src/bin/psql/mbprint.c
@@ -205,7 +205,7 @@ pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding)
  * This MUST be kept in sync with pg_wcsformat!
  */
 void
-pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
+pg_wcssize(const unsigned char *pwcs, size_t len, int encoding,
 		   int *result_width, int *result_height, int *result_format_size)
 {
 	int			w,
@@ -288,7 +288,7 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
  * This MUST be kept in sync with pg_wcssize!
  */
 void
-pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
+pg_wcsformat(const unsigned char *pwcs, size_t len, int encoding,
 			 struct lineptr * lines, int count)
 {
 	int			w,
diff --git a/src/bin/psql/mbprint.h b/src/bin/psql/mbprint.h
index f729ef045c7366b84debcfcdb020754a0fe3d0e5..83050ffcd7ee7d9a0fc25d625a66ac54746348d7 100644
--- a/src/bin/psql/mbprint.h
+++ b/src/bin/psql/mbprint.h
@@ -11,8 +11,8 @@ struct lineptr
 
 extern unsigned char *mbvalidate(unsigned char *pwcs, int encoding);
 extern int	pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding);
-extern void pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, struct lineptr * lines, int count);
-extern void pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
+extern void pg_wcsformat(const unsigned char *pwcs, size_t len, int encoding, struct lineptr * lines, int count);
+extern void pg_wcssize(const unsigned char *pwcs, size_t len, int encoding,
 		   int *width, int *height, int *format_size);
 
 #endif   /* MBPRINT_H */
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 28afcdda743ec28ac5fc3a0c95d0dc60597c4324..0d18665566f4f50c3a5aee2c3ba3bf4736de8a60 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -559,7 +559,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
 					nl_lines,
 					bytes_required;
 
-		pg_wcssize((unsigned char *) cont->headers[i], strlen(cont->headers[i]),
+		pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
 				   encoding, &width, &nl_lines, &bytes_required);
 		if (width > max_width[i])
 			max_width[i] = width;
@@ -583,7 +583,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
 					nl_lines,
 					bytes_required;
 
-		pg_wcssize((unsigned char *) *ptr, strlen(*ptr), encoding,
+		pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
 				   &width, &nl_lines, &bytes_required);
 
 		if (width > max_width[i % col_count])
@@ -731,7 +731,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
 						nl_lines,
 						bytes_required;
 
-			pg_wcssize((unsigned char *) *ptr, strlen(*ptr), encoding,
+			pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
 					   &width, &nl_lines, &bytes_required);
 
 			/*
@@ -768,7 +768,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
 			int			width,
 						height;
 
-			pg_wcssize((unsigned char *) cont->title, strlen(cont->title),
+			pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
 					   encoding, &width, &height, NULL);
 			if (width >= width_total)
 				/* Aligned */
@@ -790,7 +790,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
 									   PRINT_RULE_TOP, format, fout);
 
 			for (i = 0; i < col_count; i++)
-				pg_wcsformat((unsigned char *) cont->headers[i],
+				pg_wcsformat((const unsigned char *) cont->headers[i],
 							 strlen(cont->headers[i]), encoding,
 							 col_lineptrs[i], max_nl_lines[i]);
 
@@ -861,7 +861,7 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
 		 */
 		for (j = 0; j < col_count; j++)
 		{
-			pg_wcsformat((unsigned char *) ptr[j], strlen(ptr[j]), encoding,
+			pg_wcsformat((const unsigned char *) ptr[j], strlen(ptr[j]), encoding,
 						 col_lineptrs[j], max_nl_lines[j]);
 			curr_nl_line[j] = 0;
 		}
@@ -1146,7 +1146,7 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 					height,
 					fs;
 
-		pg_wcssize((unsigned char *) cont->headers[i], strlen(cont->headers[i]),
+		pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
 				   encoding, &width, &height, &fs);
 		if (width > hwidth)
 			hwidth = width;
@@ -1163,7 +1163,7 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 					height,
 					fs;
 
-		pg_wcssize((unsigned char *) *ptr, strlen(*ptr), encoding,
+		pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
 				   &width, &height, &fs);
 		if (width > dwidth)
 			dwidth = width;
@@ -1219,11 +1219,11 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 		}
 
 		/* Format the header */
-		pg_wcsformat((unsigned char *) cont->headers[i % cont->ncolumns],
+		pg_wcsformat((const unsigned char *) cont->headers[i % cont->ncolumns],
 					 strlen(cont->headers[i % cont->ncolumns]),
 					 encoding, hlineptr, hheight);
 		/* Format the data */
-		pg_wcsformat((unsigned char *) *ptr, strlen(*ptr), encoding,
+		pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
 					 dlineptr, dheight);
 
 		line_count = 0;
diff --git a/src/include/nodes/makefuncs.h b/src/include/nodes/makefuncs.h
index 61314608f53e02a99a1eaa72beeacb4be9fff64a..532681f8c015e5606d9de395e97d51745a10b5f7 100644
--- a/src/include/nodes/makefuncs.h
+++ b/src/include/nodes/makefuncs.h
@@ -20,7 +20,7 @@
 extern A_Expr *makeA_Expr(A_Expr_Kind kind, List *name,
 		   Node *lexpr, Node *rexpr, int location);
 
-extern A_Expr *makeSimpleA_Expr(A_Expr_Kind kind, const char *name,
+extern A_Expr *makeSimpleA_Expr(A_Expr_Kind kind, char *name,
 				 Node *lexpr, Node *rexpr, int location);
 
 extern Var *makeVar(Index varno,
diff --git a/src/interfaces/ecpg/ecpglib/descriptor.c b/src/interfaces/ecpg/ecpglib/descriptor.c
index fb5aa9a185dae89fd5f8a2fd26b1929c3d1a42f4..17a956ea5d4100b8afdb22794a91c9a39acdda5f 100644
--- a/src/interfaces/ecpg/ecpglib/descriptor.c
+++ b/src/interfaces/ecpg/ecpglib/descriptor.c
@@ -155,36 +155,36 @@ set_int_item(int lineno, int *target, const void *var, enum ECPGttype vartype)
 	switch (vartype)
 	{
 		case ECPGt_short:
-			*target = *(short *) var;
+			*target = *(const short *) var;
 			break;
 		case ECPGt_int:
-			*target = *(int *) var;
+			*target = *(const int *) var;
 			break;
 		case ECPGt_long:
-			*target = *(long *) var;
+			*target = *(const long *) var;
 			break;
 		case ECPGt_unsigned_short:
-			*target = *(unsigned short *) var;
+			*target = *(const unsigned short *) var;
 			break;
 		case ECPGt_unsigned_int:
-			*target = *(unsigned int *) var;
+			*target = *(const unsigned int *) var;
 			break;
 		case ECPGt_unsigned_long:
-			*target = *(unsigned long *) var;
+			*target = *(const unsigned long *) var;
 			break;
 #ifdef HAVE_LONG_LONG_INT
 		case ECPGt_long_long:
-			*target = *(long long int *) var;
+			*target = *(const long long int *) var;
 			break;
 		case ECPGt_unsigned_long_long:
-			*target = *(unsigned long long int *) var;
+			*target = *(const unsigned long long int *) var;
 			break;
 #endif   /* HAVE_LONG_LONG_INT */
 		case ECPGt_float:
-			*target = *(float *) var;
+			*target = *(const float *) var;
 			break;
 		case ECPGt_double:
-			*target = *(double *) var;
+			*target = *(const double *) var;
 			break;
 		default:
 			ecpg_raise(lineno, ECPG_VAR_NOT_NUMERIC, ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION, NULL);
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index a288b3ff1254821adc3ea30f846f19be9dc19520..b8e48a366c67765ce7c4a05a781338324759f6ab 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -1074,7 +1074,7 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 
 			default:
 				/* Not implemented yet */
-				ecpg_raise(lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (char *) ecpg_type_name(var->type));
+				ecpg_raise(lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ecpg_type_name(var->type));
 				return false;
 				break;
 		}
@@ -1940,7 +1940,7 @@ bool
 ECPGdo_descriptor(int line, const char *connection,
 				  const char *descriptor, const char *query)
 {
-	return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, (char *) query, ECPGt_EOIT,
+	return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
 				  ECPGt_descriptor, descriptor, 0L, 0L, 0L,
 				  ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
 }