v2-0003-Allow-pg_set_relation_stats-to-continue-after-non.patch

text/x-patch

Filename: v2-0003-Allow-pg_set_relation_stats-to-continue-after-non.patch
Type: text/x-patch
Part: 2
Message: Re: Statistics Import and Export

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v2-0003
Subject: Allow pg_set_relation_stats to continue after non-ERROR validation.
File+
src/backend/statistics/relation_stats.c 11 20
From cd66d8c4d3874cffe27f63ef7eaedbbce70142af 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 v2 3/3] Allow pg_set_relation_stats 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. That isn't the correct
behavior, and we should instead let the function continue in case there
were other valid values that could be set.
---
 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 b90f70b190..ffc8d4cebd 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);
@@ -109,10 +108,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);
@@ -130,10 +127,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);
@@ -151,10 +146,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);
@@ -163,22 +156,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.46.2