From a473c258577f9f9b28435feef27e8cbc25721d5c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 27 Jun 2023 14:09:39 +0200 Subject: [PATCH 2/3] Add macro for maximum statistics target The number of places where 10000 was hardcoded had grown a bit beyond the comfort level. Introduce a macro MAX_STATISTICS_TARGET instead. --- src/backend/commands/statscmds.c | 4 ++-- src/backend/commands/tablecmds.c | 5 +++-- src/backend/statistics/extended_stats.c | 2 +- src/backend/utils/misc/guc_tables.c | 2 +- src/include/catalog/pg_attribute.h | 2 +- src/include/commands/vacuum.h | 7 +++++++ src/include/statistics/statistics.h | 4 ++-- 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index c2dab20bdc..36bc8c33ba 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -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", diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index d985278ac6..fce5e6f220 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -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", diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 28b52d8aa1..513ddf540a 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -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; } diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c index 71e27f8eb0..f8ef87d26d 100644 --- a/src/backend/utils/misc/guc_tables.c +++ b/src/backend/utils/misc/guc_tables.c @@ -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 }, { diff --git a/src/include/catalog/pg_attribute.h b/src/include/catalog/pg_attribute.h index f8b4861b94..f00df488ce 100644 --- a/src/include/catalog/pg_attribute.h +++ b/src/include/catalog/pg_attribute.h @@ -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); diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index cb5b11ab31..bda7792770 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -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; diff --git a/src/include/statistics/statistics.h b/src/include/statistics/statistics.h index 17e3e7f881..5e538fec32 100644 --- a/src/include/statistics/statistics.h +++ b/src/include/statistics/statistics.h @@ -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 -- 2.41.0