From 0cf8a90c37ec95bbbefb274bc3d48a077b4868b8 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Wed, 16 Oct 2024 19:13:01 -0400
Subject: [PATCH v3 1/2] Allow relation_statistics_update to continue after
 non-ERROR validation.

Some validation checks would, when elevel was set to WARNING or lower,
immediately return false after the failed check, rather than let the
update continue on to other attributes.

Currently the only caller is pg_set_relation_stats() which sets the
elevel to ERROR, so this incorrect behavior would never surface, but
later callers will user WARNING elevel.
---
 src/backend/statistics/relation_stats.c | 31 +++++++++----------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index 1a6d1640c3..126eab49d8 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -67,7 +67,6 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
 	bool		nulls[3] = {0};
 	int			ncols = 0;
 	TupleDesc	tupdesc;
-	HeapTuple	newtup;
 
 
 	stats_check_required_arg(fcinfo, relarginfo, RELATION_ARG);
@@ -110,10 +109,8 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 					 errmsg("relpages cannot be < -1")));
 			table_close(crel, RowExclusiveLock);
-			return false;
 		}
-
-		if (relpages != pgcform->relpages)
+		else if (relpages != pgcform->relpages)
 		{
 			replaces[ncols] = Anum_pg_class_relpages;
 			values[ncols] = Int32GetDatum(relpages);
@@ -131,10 +128,8 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 					 errmsg("reltuples cannot be < -1.0")));
 			table_close(crel, RowExclusiveLock);
-			return false;
 		}
-
-		if (reltuples != pgcform->reltuples)
+		else if (reltuples != pgcform->reltuples)
 		{
 			replaces[ncols] = Anum_pg_class_reltuples;
 			values[ncols] = Float4GetDatum(reltuples);
@@ -152,10 +147,8 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 					 errmsg("relallvisible cannot be < 0")));
 			table_close(crel, RowExclusiveLock);
-			return false;
 		}
-
-		if (relallvisible != pgcform->relallvisible)
+		else if (relallvisible != pgcform->relallvisible)
 		{
 			replaces[ncols] = Anum_pg_class_relallvisible;
 			values[ncols] = Int32GetDatum(relallvisible);
@@ -164,22 +157,20 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
 	}
 
 	/* only update pg_class if there is a meaningful change */
-	if (ncols == 0)
+	if (ncols > 0)
 	{
-		table_close(crel, RowExclusiveLock);
-		return false;
+		HeapTuple	newtup;
+
+		newtup = heap_modify_tuple_by_cols(ctup, tupdesc, ncols, replaces, values,
+										nulls);
+		CatalogTupleUpdate(crel, &newtup->t_self, newtup);
+		heap_freetuple(newtup);
 	}
 
-	newtup = heap_modify_tuple_by_cols(ctup, tupdesc, ncols, replaces, values,
-									   nulls);
-
-	CatalogTupleUpdate(crel, &newtup->t_self, newtup);
-	heap_freetuple(newtup);
-
 	/* release the lock, consistent with vac_update_relstats() */
 	table_close(crel, RowExclusiveLock);
 
-	return true;
+	return (ncols > 0);
 }
 
 /*
-- 
2.47.0

