From b61706faf90d2c8fa7ed8ea4c9ba383b63c8b199 Mon Sep 17 00:00:00 2001 From: Alexandra Wang Date: Mon, 3 Aug 2026 15:59:52 -0700 Subject: [PATCH v10 3/4] Improve auto-generated names for extended statistics When CREATE STATISTICS is used without an explicit name, the name is generated from the columns and expressions the object is defined on. Previously only a bare, unqualified column reference contributed its name; a column wrapped in parentheses, such as (a) or (t.a), was treated as an expression and contributed a fixed "expr". Resolve a simple column reference to its column name regardless of how it was written, so a column written "(a)" or "(t.a)" contributes "a" just as "a" does; a more complex expression still contributes "expr". For example, "CREATE STATISTICS ON (a), (b) FROM t" is now named t_a_b_stat instead of t_expr_expr_stat. Reported-by: Tomas Vondra Discussion: https://postgr.es/m/02dd92ff-b1ee-4e69-9f7c-abc60dfb4142@vondra.me --- src/backend/commands/statscmds.c | 18 +++++++++++++----- src/test/regress/expected/stats_ext.out | 24 ++++++++++++++++++++++++ src/test/regress/sql/stats_ext.sql | 22 ++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 03dbf68f2a0..ea758a70a76 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -44,7 +44,7 @@ static char *ChooseExtendedStatisticName(const char *name1, const char *name2, const char *label, Oid namespaceid); -static char *ChooseExtendedStatisticNameAddition(List *exprs); +static char *ChooseExtendedStatisticNameAddition(List *exprs, Oid relid); /* * CREATE STATISTICS @@ -169,7 +169,8 @@ CreateStatistics(List *relids, CreateStatsStmt *stmt, bool check_rights) { namespaceId = RelationGetNamespace(rel); namestr = ChooseExtendedStatisticName(RelationGetRelationName(rel), - ChooseExtendedStatisticNameAddition(stmt->exprs), + ChooseExtendedStatisticNameAddition(stmt->exprs, + relid), "stat", namespaceId); } @@ -878,7 +879,7 @@ ChooseExtendedStatisticName(const char *name1, const char *name2, * ChooseIndexNameAddition. */ static char * -ChooseExtendedStatisticNameAddition(List *exprs) +ChooseExtendedStatisticNameAddition(List *exprs, Oid relid) { char buf[NAMEDATALEN * 2]; int buflen = 0; @@ -896,8 +897,12 @@ ChooseExtendedStatisticNameAddition(List *exprs) name = selem->name; - if (buflen > 0) - buf[buflen++] = '_'; /* insert _ between names */ + /* + * A simple column reference resolves to a bare Var; use the column + * name. + */ + if (!name && selem->expr && IsA(selem->expr, Var)) + name = get_attname(relid, ((Var *) selem->expr)->varattno, true); /* * We use fixed 'expr' for expressions, which have empty column names. @@ -908,6 +913,9 @@ ChooseExtendedStatisticNameAddition(List *exprs) if (!name) name = "expr"; + if (buflen > 0) + buf[buflen++] = '_'; /* insert _ between names */ + /* * At this point we have buflen <= NAMEDATALEN. name should be less * than NAMEDATALEN already, but use strlcpy for paranoia. diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.out index a8daa480b19..0af4144b60e 100644 --- a/src/test/regress/expected/stats_ext.out +++ b/src/test/regress/expected/stats_ext.out @@ -3963,3 +3963,27 @@ SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM declared_order_grouping (1 row) DROP TABLE declared_order_grouping; +-- +-- Auto-generated statistics object names. +-- +-- A simple column reference contributes the column name, whether written +-- unqualified or wrapped in parens; a more complex expression contributes a +-- fixed "expr". +CREATE TABLE auto_t1 (a INTEGER, b INTEGER, c INTEGER); +-- Unqualified columns: bare column names. +CREATE STATISTICS ON b, c FROM auto_t1; +-- Table-qualified columns in parens: still the column names. +CREATE STATISTICS ON (auto_t1.a), (auto_t1.b) FROM auto_t1; +-- A parenthesized column and a more complex expression: the column resolves +-- to its name, the expression uses the fixed "expr". +CREATE STATISTICS ON (a), (b + c) FROM auto_t1; +\dX auto_t1* + List of extended statistics + Schema | Name | Definition | Ndistinct | Dependencies | MCV +--------+---------------------+-------------------------+-----------+--------------+--------- + public | auto_t1_a_b_stat | a, b FROM auto_t1 | defined | defined | defined + public | auto_t1_a_expr_stat | a, (b + c) FROM auto_t1 | defined | defined | defined + public | auto_t1_b_c_stat | b, c FROM auto_t1 | defined | defined | defined +(3 rows) + +DROP TABLE auto_t1; diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sql index bad72bcba43..02116ed3b7a 100644 --- a/src/test/regress/sql/stats_ext.sql +++ b/src/test/regress/sql/stats_ext.sql @@ -2051,3 +2051,25 @@ ANALYZE declared_order_grouping; -- estimate should track the 100 distinct groups, not ndistinct(a) * ndistinct(b) SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM declared_order_grouping GROUP BY a, b'); DROP TABLE declared_order_grouping; + +-- +-- Auto-generated statistics object names. +-- +-- A simple column reference contributes the column name, whether written +-- unqualified or wrapped in parens; a more complex expression contributes a +-- fixed "expr". +CREATE TABLE auto_t1 (a INTEGER, b INTEGER, c INTEGER); + +-- Unqualified columns: bare column names. +CREATE STATISTICS ON b, c FROM auto_t1; + +-- Table-qualified columns in parens: still the column names. +CREATE STATISTICS ON (auto_t1.a), (auto_t1.b) FROM auto_t1; + +-- A parenthesized column and a more complex expression: the column resolves +-- to its name, the expression uses the fixed "expr". +CREATE STATISTICS ON (a), (b + c) FROM auto_t1; + +\dX auto_t1* + +DROP TABLE auto_t1; -- 2.54.0 (Apple Git-157)