mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	CREATE STATISTICS: improve misleading error message
I think the error message for a different condition was inadvertently
copied.
This problem seems to have been introduced by commit a4d75c86bf.
Author: Álvaro Herrera <alvherre@kurilemu.de>
Reported-by: jian he <jian.universality@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Backpatch-through: 14
Discussion: https://postgr.es/m/CACJufxEZ48toGH0Em_6vdsT57Y3L8pLF=DZCQ_gCii6=C3MeXw@mail.gmail.com
			
			
This commit is contained in:
		@@ -1880,7 +1880,8 @@ ProcessUtilitySlow(ParseState *pstate,
 | 
				
			|||||||
					if (!IsA(rel, RangeVar))
 | 
										if (!IsA(rel, RangeVar))
 | 
				
			||||||
						ereport(ERROR,
 | 
											ereport(ERROR,
 | 
				
			||||||
								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 | 
													(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 | 
				
			||||||
								 errmsg("only a single relation is allowed in CREATE STATISTICS")));
 | 
													 errmsg("cannot create statistics on the specified relation"),
 | 
				
			||||||
 | 
													 errdetail("CREATE STATISTICS only supports tables, foreign tables and materialized views.")));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					/*
 | 
										/*
 | 
				
			||||||
					 * CREATE STATISTICS will influence future execution plans
 | 
										 * CREATE STATISTICS will influence future execution plans
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,6 +54,32 @@ CREATE STATISTICS tst ON (x || 'x'), (x || 'x'), y FROM ext_stats_test;
 | 
				
			|||||||
ERROR:  duplicate expression in statistics definition
 | 
					ERROR:  duplicate expression in statistics definition
 | 
				
			||||||
CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test;
 | 
					CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test;
 | 
				
			||||||
ERROR:  unrecognized statistics kind "unrecognized"
 | 
					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.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					DROP FUNCTION tftest;
 | 
				
			||||||
-- incorrect expressions
 | 
					-- incorrect expressions
 | 
				
			||||||
CREATE STATISTICS tst ON (y) FROM ext_stats_test; -- single column reference
 | 
					CREATE STATISTICS tst ON (y) FROM ext_stats_test; -- single column reference
 | 
				
			||||||
ERROR:  extended statistics require at least 2 columns
 | 
					ERROR:  extended statistics require at least 2 columns
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,6 +40,18 @@ CREATE STATISTICS tst ON x, x, y, x, x, (x || 'x'), (y + 1), (x || 'x'), (x || '
 | 
				
			|||||||
CREATE STATISTICS tst ON (x || 'x'), (x || 'x'), (y + 1), (x || 'x'), (x || 'x'), (y + 1), (x || 'x'), (x || 'x'), (y + 1) FROM ext_stats_test;
 | 
					CREATE STATISTICS tst ON (x || 'x'), (x || 'x'), (y + 1), (x || 'x'), (x || 'x'), (y + 1), (x || 'x'), (x || 'x'), (y + 1) FROM ext_stats_test;
 | 
				
			||||||
CREATE STATISTICS tst ON (x || 'x'), (x || 'x'), y FROM ext_stats_test;
 | 
					CREATE STATISTICS tst ON (x || 'x'), (x || 'x'), y FROM ext_stats_test;
 | 
				
			||||||
CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test;
 | 
					CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test;
 | 
				
			||||||
 | 
					-- unsupported targets
 | 
				
			||||||
 | 
					CREATE STATISTICS tst ON a FROM (VALUES (x)) AS foo;
 | 
				
			||||||
 | 
					CREATE STATISTICS tst ON a FROM foo NATURAL JOIN bar;
 | 
				
			||||||
 | 
					CREATE STATISTICS tst ON a FROM (SELECT * FROM ext_stats_test) AS foo;
 | 
				
			||||||
 | 
					CREATE STATISTICS tst ON a FROM ext_stats_test s TABLESAMPLE system (x);
 | 
				
			||||||
 | 
					CREATE STATISTICS tst ON a FROM XMLTABLE('foo' PASSING 'bar' COLUMNS a text);
 | 
				
			||||||
 | 
					CREATE STATISTICS tst ON a FROM JSON_TABLE(jsonb '123', '$' COLUMNS (item int));
 | 
				
			||||||
 | 
					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);
 | 
				
			||||||
 | 
					DROP FUNCTION tftest;
 | 
				
			||||||
-- incorrect expressions
 | 
					-- incorrect expressions
 | 
				
			||||||
CREATE STATISTICS tst ON (y) FROM ext_stats_test; -- single column reference
 | 
					CREATE STATISTICS tst ON (y) FROM ext_stats_test; -- single column reference
 | 
				
			||||||
CREATE STATISTICS tst ON y + z FROM ext_stats_test; -- missing parentheses
 | 
					CREATE STATISTICS tst ON y + z FROM ext_stats_test; -- missing parentheses
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user