On Sun, 2024-10-27 at 14:00 +0300, Alexander Lakhin wrote:
> Please look at the following seemingly atypical behavior of the new
> functions:

...

> SELECT pg_restore_attribute_stats(
>    'relation', 'test'::regclass,
>    'attname', 'id'::name,
>    'inherited', false
> ) FROM generate_series(1, 2);
> ERROR:  XX000: tuple already updated by self

Thank you for the report!

Attached a patch to add calls to CommandCounterIncrement().

Regards,
        Jeff Davis

From ccff3df8b2d6f0c139a39e5aef8721b4480bdbd3 Mon Sep 17 00:00:00 2001
From: Jeff Davis <j...@j-davis.com>
Date: Mon, 28 Oct 2024 18:16:09 -0700
Subject: [PATCH] Add missing CommandCounterIncrement() in stats import
 functions.

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/98b2fcf0-f701-369e-d63d-6be9739ce...@gmail.com
---
 src/backend/statistics/attribute_stats.c | 11 ++++++++---
 src/backend/statistics/relation_stats.c  |  2 ++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index af61fd79e4..4ae0722b78 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -752,6 +752,8 @@ upsert_pg_statistic(Relation starel, HeapTuple oldtup,
 	}
 
 	heap_freetuple(newtup);
+
+	CommandCounterIncrement();
 }
 
 /*
@@ -762,6 +764,7 @@ delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
 {
 	Relation	sd = table_open(StatisticRelationId, RowExclusiveLock);
 	HeapTuple	oldtup;
+	bool		result = false;
 
 	/* Is there already a pg_statistic tuple for this attribute? */
 	oldtup = SearchSysCache3(STATRELATTINH,
@@ -773,12 +776,14 @@ delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
 	{
 		CatalogTupleDelete(sd, &oldtup->t_self);
 		ReleaseSysCache(oldtup);
-		table_close(sd, RowExclusiveLock);
-		return true;
+		result = true;
 	}
 
 	table_close(sd, RowExclusiveLock);
-	return false;
+
+	CommandCounterIncrement();
+
+	return result;
 }
 
 /*
diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index 5a2aabc921..ed5dea2e05 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -171,6 +171,8 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
 	/* release the lock, consistent with vac_update_relstats() */
 	table_close(crel, RowExclusiveLock);
 
+	CommandCounterIncrement();
+
 	return result;
 }
 
-- 
2.34.1

Reply via email to