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
From d0245c92ee068c2715c16a34450799a670b5fc7c Mon Sep 17 00:00:00 2001
From: Fujii Masao <fu...@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

Reply via email to