Improve error message for database object stats manipulation functions.
authorFujii Masao <[email protected]>
Tue, 19 Nov 2024 17:00:50 +0000 (02:00 +0900)
committerFujii Masao <[email protected]>
Tue, 19 Nov 2024 17:00:50 +0000 (02:00 +0900)
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
src/backend/statistics/attribute_stats.c
src/backend/statistics/relation_stats.c

index 73979f20fff041970fc03c427a86d186f25b9c5f..1a0b85bb4d7ee4ddf2832bbbdefa1b15330c96b9 100644 (file)
@@ -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
index 4ae0722b781365ae60160b23597120e0d39254af..686f2e639c66adb9d6bae69ac148134eec7d5adc 100644 (file)
@@ -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);
index ed5dea2e0588fd8ba1e29dc25d304f4f629273fd..e619d5cf5b19aa96e6ddd2cfdeef7d180fe8d217 100644 (file)
@@ -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);
 
    /*