Add macro for maximum statistics target
authorPeter Eisentraut <[email protected]>
Mon, 3 Jul 2023 05:09:22 +0000 (07:09 +0200)
committerPeter Eisentraut <[email protected]>
Mon, 3 Jul 2023 05:18:57 +0000 (07:18 +0200)
The number of places where 10000 was hardcoded had grown a bit beyond
the comfort level.  Introduce a macro MAX_STATISTICS_TARGET instead.

Reviewed-by: Tomas Vondra <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/d6069765-5971-04d3-c10d-e4f7b2e9c459%40eisentraut.org

src/backend/commands/statscmds.c
src/backend/commands/tablecmds.c
src/backend/statistics/extended_stats.c
src/backend/utils/misc/guc_tables.c
src/include/catalog/pg_attribute.h
src/include/commands/vacuum.h
src/include/statistics/statistics.h

index c2dab20bdc496ba8dc71514976b8d47818e47a4d..36bc8c33baf3f3c262169c03848803476d59ba09 100644 (file)
@@ -619,9 +619,9 @@ AlterStatistics(AlterStatsStmt *stmt)
                 errmsg("statistics target %d is too low",
                        newtarget)));
    }
-   else if (newtarget > 10000)
+   else if (newtarget > MAX_STATISTICS_TARGET)
    {
-       newtarget = 10000;
+       newtarget = MAX_STATISTICS_TARGET;
        ereport(WARNING,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("lowering statistics target to %d",
index d985278ac6b4eea124ea0bfaa3d5f96866c261c1..fce5e6f22095aa4bd5e5993faad80898a8fcc7f4 100644 (file)
@@ -61,6 +61,7 @@
 #include "commands/trigger.h"
 #include "commands/typecmds.h"
 #include "commands/user.h"
+#include "commands/vacuum.h"
 #include "executor/executor.h"
 #include "foreign/fdwapi.h"
 #include "foreign/foreign.h"
@@ -8180,9 +8181,9 @@ ATExecSetStatistics(Relation rel, const char *colName, int16 colNum, Node *newVa
                 errmsg("statistics target %d is too low",
                        newtarget)));
    }
-   else if (newtarget > 10000)
+   else if (newtarget > MAX_STATISTICS_TARGET)
    {
-       newtarget = 10000;
+       newtarget = MAX_STATISTICS_TARGET;
        ereport(WARNING,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("lowering statistics target to %d",
index 28b52d8aa1f88f9087cbe3617e7a57f74bf30cb2..513ddf540abfc875d9eac138c2f237043bee0519 100644 (file)
@@ -379,7 +379,7 @@ statext_compute_stattarget(int stattarget, int nattrs, VacAttrStats **stats)
        stattarget = default_statistics_target;
 
    /* As this point we should have a valid statistics target. */
-   Assert((stattarget >= 0) && (stattarget <= 10000));
+   Assert((stattarget >= 0) && (stattarget <= MAX_STATISTICS_TARGET));
 
    return stattarget;
 }
index 71e27f8eb051c16e6e9b193eeceed6ed85b9e2c1..f8ef87d26dcdcbe6fa8eea2065742221a47194a2 100644 (file)
@@ -2035,7 +2035,7 @@ struct config_int ConfigureNamesInt[] =
                         "column-specific target set via ALTER TABLE SET STATISTICS.")
        },
        &default_statistics_target,
-       100, 1, 10000,
+       100, 1, MAX_STATISTICS_TARGET,
        NULL, NULL, NULL
    },
    {
index f8b4861b94ab569d368bcf31dd6a08e14674791a..f00df488ce7de8e1985dbca326ab8d0980156591 100644 (file)
@@ -165,7 +165,7 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75,
     * that no value has been explicitly set for this column, so ANALYZE
     * should use the default setting.
     *
-    * int16 is sufficient because the max value is currently 10000.
+    * int16 is sufficient for the current max value (MAX_STATISTICS_TARGET).
     */
    int16       attstattarget BKI_DEFAULT(-1);
 
index cb5b11ab31f7341d4bc9d925cf0a5074d0d68caa..bda7792770a68ab8ebbaa7f3ac860e44324a869a 100644 (file)
@@ -305,6 +305,13 @@ extern PGDLLIMPORT int vacuum_multixact_freeze_table_age;
 extern PGDLLIMPORT int vacuum_failsafe_age;
 extern PGDLLIMPORT int vacuum_multixact_failsafe_age;
 
+/*
+ * Maximum value for default_statistics_target and per-column statistics
+ * targets.  This is fairly arbitrary, mainly to prevent users from creating
+ * unreasonably large statistics that the system cannot handle well.
+ */
+#define MAX_STATISTICS_TARGET 10000
+
 /* Variables for cost-based parallel vacuum */
 extern PGDLLIMPORT pg_atomic_uint32 *VacuumSharedCostBalance;
 extern PGDLLIMPORT pg_atomic_uint32 *VacuumActiveNWorkers;
index 17e3e7f881dfaee607fe126c7df3d7da44847871..5e538fec32f13a1b0ed5b446ad080ae16680d410 100644 (file)
@@ -66,8 +66,8 @@ typedef struct MVDependencies
 #define STATS_MCV_MAGIC            0xE1A651C2  /* marks serialized bytea */
 #define STATS_MCV_TYPE_BASIC   1   /* basic MCV list type */
 
-/* max items in MCV list (should be equal to max default_statistics_target) */
-#define STATS_MCVLIST_MAX_ITEMS        10000
+/* max items in MCV list */
+#define STATS_MCVLIST_MAX_ITEMS        MAX_STATISTICS_TARGET
 
 /*
  * Multivariate MCV (most-common value) lists