Improve error messages for database object stats manipulation functions during recovery
Hi,
When database object stats manipulation functions like pg_set_relation_stats() are run,
they currently produce the following error and hint messages, which are "internal"
and make it hard for users to understand the issue:
ERROR: cannot acquire lock mode ShareUpdateExclusiveLock on database objects while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database objects during recovery.
So I'd like to propose updating these to clearer messages:
ERROR: recovery is in progress
HINT: Database object statistics manipulation functions cannot be executed during recovery.
Thought?
I've attached a patch implementing these changes. It also updates the documentation to
clearly state that these functions are not available during recovery.
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
Attachments:
v1-0001-Improve-error-message-for-database-object-stats-m.patchtext/plain; charset=UTF-8; name=v1-0001-Improve-error-message-for-database-object-stats-m.patchDownload
From d0245c92ee068c2715c16a34450799a670b5fc7c Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Sat, 26 Oct 2024 01:09:46 +0900
Subject: [PATCH v1] Improve error message for database object stats
manipulation functions.
Previously, database object statistics manipulation functions like
pg_set_relation_stats() reported unclear error and hint messages
when executed during recovery. These messages were "internal",
making it difficult for users to understand the issue:
ERROR: cannot acquire lock mode ShareUpdateExclusiveLock on database objects while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database objects during recovery.
This commit updates the error handling so that, if these functions
are called during recovery, they produce clearer messages:
ERROR: recovery is in progress
HINT: Database object statistics manipulation functions cannot be executed during recovery.
The related documentation has also been updated to explicitly
clarify that these functions are not available during recovery.
---
doc/src/sgml/func.sgml | 1 +
src/backend/statistics/attribute_stats.c | 12 ++++++++++++
src/backend/statistics/relation_stats.c | 6 ++++++
3 files changed, 19 insertions(+)
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 7be0324ac8..1245b56305 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -30182,6 +30182,7 @@ DETAIL: Make sure pg_wal_replay_wait() isn't called within a transaction with a
<para>
<xref linkend="functions-admin-statsmod"/> lists functions used to
manipulate statistics.
+ These functions cannot be executed during recovery.
<warning>
<para>
Changes made by these statistics manipulation functions are likely to be
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index af61fd79e4..c6163d1cd9 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -155,6 +155,12 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
stats_check_required_arg(fcinfo, attarginfo, ATTRELATION_ARG);
reloid = PG_GETARG_OID(ATTRELATION_ARG);
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is in progress"),
+ errhint("Database object statistics manipulation functions cannot be executed during recovery.")));
+
/* lock before looking up attribute */
stats_lock_check_privileges(reloid);
@@ -860,6 +866,12 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
stats_check_required_arg(fcinfo, attarginfo, ATTRELATION_ARG);
reloid = PG_GETARG_OID(ATTRELATION_ARG);
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is in progress"),
+ errhint("Database object statistics manipulation functions cannot be executed during recovery.")));
+
stats_lock_check_privileges(reloid);
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index 5a2aabc921..48d12f88e2 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -72,6 +72,12 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
stats_check_required_arg(fcinfo, relarginfo, RELATION_ARG);
reloid = PG_GETARG_OID(RELATION_ARG);
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is in progress"),
+ errhint("Database object statistics manipulation functions cannot be executed during recovery.")));
+
stats_lock_check_privileges(reloid);
/*
--
2.46.2
On 25/10/2024 20:07, Fujii Masao wrote:
Hi,
When database object stats manipulation functions like
pg_set_relation_stats() are run,
they currently produce the following error and hint messages, which are
"internal"
and make it hard for users to understand the issue:ERROR: cannot acquire lock mode ShareUpdateExclusiveLock on
database objects while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database
objects during recovery.So I'd like to propose updating these to clearer messages:
ERROR: recovery is in progress
HINT: Database object statistics manipulation functions cannot
be executed during recovery.Thought?
Makes sense.
"Database object statistics manipulation functions" is a bit of a
mouthful". Maybe something like "statistics cannot be modified during
recovery".
I've attached a patch implementing these changes. It also updates the
documentation to
clearly state that these functions are not available during recovery.
Looks good to me.
--
Heikki Linnakangas
Neon (https://neon.tech)
+1 for the patch.
Recently, one of our customer have tried to upgrade the replica, and,
obviously, failed. I don't remember the exact error message, but for him it
was not so clear that server is in recovery. Explicitly declaring error is
the way to go in my view.
So, I consider this patch useful. Overall, looks good to me.
--
Best regards,
Maxim Orlov.
On 2024/11/13 6:09, Heikki Linnakangas wrote:
On 25/10/2024 20:07, Fujii Masao wrote:
Hi,
When database object stats manipulation functions like pg_set_relation_stats() are run,
they currently produce the following error and hint messages, which are "internal"
and make it hard for users to understand the issue:ERROR: cannot acquire lock mode ShareUpdateExclusiveLock on database objects while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database objects during recovery.So I'd like to propose updating these to clearer messages:
ERROR: recovery is in progress
HINT: Database object statistics manipulation functions cannot be executed during recovery.Thought?
Makes sense.
"Database object statistics manipulation functions" is a bit of a mouthful". Maybe something like "statistics cannot be modified during recovery".
Sounds good! I've updated the hint messages as suggested and attached the revised patch.
Thanks for the review!
Unless there are any objections, I'll proceed with committing it.
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
Attachments:
v2-0001-Improve-error-message-for-database-object-stats-m.patchtext/plain; charset=UTF-8; name=v2-0001-Improve-error-message-for-database-object-stats-m.patchDownload
From 0d8711ff4a52a81c337c157177c423ce65bb3c20 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Sat, 26 Oct 2024 01:09:46 +0900
Subject: [PATCH v2] Improve error message for database object stats
manipulation functions.
Previously, database object statistics manipulation functions like
pg_set_relation_stats() reported unclear error and hint messages
when executed during recovery. These messages were "internal",
making it difficult for users to understand the issue:
ERROR: cannot acquire lock mode ShareUpdateExclusiveLock on database objects while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database objects during recovery.
This commit updates the error handling so that, if these functions
are called during recovery, they produce clearer messages:
ERROR: recovery is in progress
HINT: Statistics cannot be modified during recovery.
The related documentation has also been updated to explicitly
clarify that these functions are not available during recovery.
Author: Fujii Masao
Reviewed-by: Heikki Linnakangas, Maxim Orlov
Discussion: https://postgr.es/m/6d313829-5f56-4a28-ae4b-bd01bf1ae791@oss.nttdata.com
---
doc/src/sgml/func.sgml | 1 +
src/backend/statistics/attribute_stats.c | 12 ++++++++++++
src/backend/statistics/relation_stats.c | 6 ++++++
3 files changed, 19 insertions(+)
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 73979f20ff..1a0b85bb4d 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -30029,6 +30029,7 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
<para>
<xref linkend="functions-admin-statsmod"/> lists functions used to
manipulate statistics.
+ These functions cannot be executed during recovery.
<warning>
<para>
Changes made by these statistics manipulation functions are likely to be
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index 4ae0722b78..686f2e639c 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -155,6 +155,12 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
stats_check_required_arg(fcinfo, attarginfo, ATTRELATION_ARG);
reloid = PG_GETARG_OID(ATTRELATION_ARG);
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is in progress"),
+ errhint("Statistics cannot be modified during recovery.")));
+
/* lock before looking up attribute */
stats_lock_check_privileges(reloid);
@@ -865,6 +871,12 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
stats_check_required_arg(fcinfo, attarginfo, ATTRELATION_ARG);
reloid = PG_GETARG_OID(ATTRELATION_ARG);
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is in progress"),
+ errhint("Statistics cannot be modified during recovery.")));
+
stats_lock_check_privileges(reloid);
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index ed5dea2e05..e619d5cf5b 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -72,6 +72,12 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
stats_check_required_arg(fcinfo, relarginfo, RELATION_ARG);
reloid = PG_GETARG_OID(RELATION_ARG);
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is in progress"),
+ errhint("Statistics cannot be modified during recovery.")));
+
stats_lock_check_privileges(reloid);
/*
--
2.47.0
On 2024/11/15 20:59, Maxim Orlov wrote:
+1 for the patch.
Recently, one of our customer have tried to upgrade the replica, and, obviously, failed. I don't remember the exact error message, but for him it was not so clear that server is in recovery. Explicitly declaring error is the way to go in my view.
So, I consider this patch useful. Overall, looks good to me.
Thanks for the review!
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
On 2024/11/16 2:36, Fujii Masao wrote:
Sounds good! I've updated the hint messages as suggested and attached the revised patch.
Thanks for the review!Unless there are any objections, I'll proceed with committing it.
Pushed. Thanks!
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION