diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 3bb0d24cd2..1903e09318 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -15,6 +15,7 @@ #include "postgres.h" #include "access/relscan.h" +#include "catalog/catalog.h" #include "catalog/dependency.h" #include "catalog/indexing.h" #include "catalog/namespace.h" @@ -124,6 +125,13 @@ CreateStatistics(CreateStatsStmt *stmt) if (!pg_class_ownercheck(RelationGetRelid(rel), stxowner)) aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(rel->rd_rel->relkind), RelationGetRelationName(rel)); + + /* Creating statistics on system catalogs is not allowed */ + if (!allowSystemTableMods && IsSystemRelation(rel)) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("permission denied: \"%s\" is a system catalog", + RelationGetRelationName(rel)))); } Assert(rel); diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.out index 5b7e1d4660..914e3f1473 100644 --- a/src/test/regress/expected/stats_ext.out +++ b/src/test/regress/expected/stats_ext.out @@ -6,6 +6,7 @@ SET max_parallel_workers = 0; SET max_parallel_workers_per_gather = 0; SET work_mem = '128kB'; -- Verify failures +CREATE TABLE ext_stats_test (x int, y int, z int); CREATE STATISTICS tst; ERROR: syntax error at or near ";" LINE 1: CREATE STATISTICS tst; @@ -20,16 +21,17 @@ LINE 1: CREATE STATISTICS tst FROM sometab; ^ CREATE STATISTICS tst ON a, b FROM nonexistant; ERROR: relation "nonexistant" does not exist -CREATE STATISTICS tst ON a, b FROM pg_class; +CREATE STATISTICS tst ON a, b FROM ext_stats_test; ERROR: column "a" does not exist -CREATE STATISTICS tst ON relname, relname, relnatts FROM pg_class; +CREATE STATISTICS tst ON x, x, y FROM ext_stats_test; ERROR: duplicate column name in statistics definition -CREATE STATISTICS tst ON relnatts + relpages FROM pg_class; +CREATE STATISTICS tst ON x + y FROM ext_stats_test; ERROR: only simple column references are allowed in CREATE STATISTICS -CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class; +CREATE STATISTICS tst ON (x, y) FROM ext_stats_test; ERROR: only simple column references are allowed in CREATE STATISTICS -CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class; +CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test; ERROR: unrecognized statistics kind "unrecognized" +DROP TABLE ext_stats_test; -- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER); CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1; diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sql index 2543dbfffa..c1dda8ca7d 100644 --- a/src/test/regress/sql/stats_ext.sql +++ b/src/test/regress/sql/stats_ext.sql @@ -8,15 +8,17 @@ SET max_parallel_workers_per_gather = 0; SET work_mem = '128kB'; -- Verify failures +CREATE TABLE ext_stats_test (x int, y int, z int); CREATE STATISTICS tst; CREATE STATISTICS tst ON a, b; CREATE STATISTICS tst FROM sometab; CREATE STATISTICS tst ON a, b FROM nonexistant; -CREATE STATISTICS tst ON a, b FROM pg_class; -CREATE STATISTICS tst ON relname, relname, relnatts FROM pg_class; -CREATE STATISTICS tst ON relnatts + relpages FROM pg_class; -CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class; -CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class; +CREATE STATISTICS tst ON a, b FROM ext_stats_test; +CREATE STATISTICS tst ON x, x, y FROM ext_stats_test; +CREATE STATISTICS tst ON x + y FROM ext_stats_test; +CREATE STATISTICS tst ON (x, y) FROM ext_stats_test; +CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test; +DROP TABLE ext_stats_test; -- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);