On 05.09.25 14:30, Peter Eisentraut wrote:
On 29.08.25 14:48, Álvaro Herrera wrote:
On 2025-Aug-29, jian he wrote:
On Fri, Aug 29, 2025 at 5:46 AM Tom Lane <[email protected]> wrote:
WFM, although I think you could shorten it to "tables, materialized
views, and foreign tables". We generally expect that partitioned
tables are included when saying "tables", no? I'm not dead set on
that either way, though.
https://www.postgresql.org/docs/current/sql-copy.html
use "COPY TO can be used only with plain tables, not views, and does
not copy rows from child tables or child partitions"
I'm inclined to think that we should only mention partitioned tables
specifically when they for some reason deviate from what we do for
regular tables, i.e., what Tom is saying. I don't think we've had an
explicit, consistent rule for that thus far, so there may be places
where we fail to follow it.
Anyway, I have pushed the error message change.
I think this message is still wrong. The check doesn't even look at the
relation kind, which is what the message is implying. (If the message
were about relkinds, then it should use
errdetail_relkind_not_supported().) It checks that the from list entry
is a table name instead of some other thing like VALUES or JOIN. So it
should be something like
CREATE STATISTICS only supports plain table names in the FROM clause
I propose the attached patch to fix this. I think this restores the
original meaning better.
From 7c55d765dc1ec2f8a28e8ae1fe3551d32ed5a964 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 12 Sep 2025 15:30:16 +0200
Subject: [PATCH v4] CREATE STATISTICS: improve misleading error message
The previous change (commit f225473cbae) was still not on target,
because it talked about relation kinds, which are not what is being
checked here. Provide a more accurate message.
Discussion:
https://postgr.es/m/CACJufxEZ48toGH0Em_6vdsT57Y3L8pLF=DZCQ_gCii6=c3m...@mail.gmail.com
---
src/backend/tcop/utility.c | 5 ++---
src/test/regress/expected/stats_ext.out | 21 +++++++--------------
2 files changed, 9 insertions(+), 17 deletions(-)
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 5f442bc3bd4..68605656b31 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1873,9 +1873,8 @@ ProcessUtilitySlow(ParseState *pstate,
if (!IsA(rel, RangeVar))
ereport(ERROR,
-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot
create statistics on the specified relation"),
-
errdetail("CREATE STATISTICS only supports tables, foreign tables and
materialized views.")));
+
(errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("CREATE
STATISTICS only supports relation names in the FROM clause")));
/*
* CREATE STATISTICS will influence
future execution plans
diff --git a/src/test/regress/expected/stats_ext.out
b/src/test/regress/expected/stats_ext.out
index a1f83b58b23..fdc0aa130bd 100644
--- a/src/test/regress/expected/stats_ext.out
+++ b/src/test/regress/expected/stats_ext.out
@@ -56,29 +56,22 @@ CREATE STATISTICS tst (unrecognized) ON x, y FROM
ext_stats_test;
ERROR: unrecognized statistics kind "unrecognized"
-- unsupported targets
CREATE STATISTICS tst ON a FROM (VALUES (x)) AS foo;
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
CREATE STATISTICS tst ON a FROM foo NATURAL JOIN bar;
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
CREATE STATISTICS tst ON a FROM (SELECT * FROM ext_stats_test) AS foo;
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
CREATE STATISTICS tst ON a FROM ext_stats_test s TABLESAMPLE system (x);
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
CREATE STATISTICS tst ON a FROM XMLTABLE('foo' PASSING 'bar' COLUMNS a text);
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
CREATE STATISTICS tst ON a FROM JSON_TABLE(jsonb '123', '$' COLUMNS (item
int));
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
CREATE FUNCTION tftest(int) returns table(a int, b int) as $$
SELECT $1, $1+i FROM generate_series(1,5) g(i);
$$ LANGUAGE sql IMMUTABLE STRICT;
CREATE STATISTICS alt_stat2 ON a FROM tftest(1);
-ERROR: cannot create statistics on the specified relation
-DETAIL: CREATE STATISTICS only supports tables, foreign tables and
materialized views.
+ERROR: CREATE STATISTICS only supports relation names in the FROM clause
DROP FUNCTION tftest;
-- incorrect expressions
CREATE STATISTICS tst ON (y) FROM ext_stats_test; -- single column reference
--
2.51.0