Skip to content

Commit 943a113

Browse files
committed
Disallow CREATE STATISTICS on system catalogs
Add a check that CREATE STATISTICS does not add extended statistics on system catalogs, similarly to indexes etc. It can be overriden using the allow_system_table_mods GUC. This bug exists since 7b504eb, adding the extended statistics, so backpatch all the way back to PostgreSQL 10. Author: Tomas Vondra Reported-by: Dean Rasheed Backpatch-through: 10 Discussion: https://postgr.es/m/CAEZATCXAPrrOKwEsyZKQ4uzzJQWBCt6QAvOcgqRGdWwT1zb%2BrQ%40mail.gmail.com
1 parent f5d044e commit 943a113

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/backend/commands/statscmds.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ CreateStatistics(CreateStatsStmt *stmt)
132132
if (!pg_class_ownercheck(RelationGetRelid(rel), stxowner))
133133
aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(rel->rd_rel->relkind),
134134
RelationGetRelationName(rel));
135+
136+
/* Creating statistics on system catalogs is not allowed */
137+
if (!allowSystemTableMods && IsSystemRelation(rel))
138+
ereport(ERROR,
139+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
140+
errmsg("permission denied: \"%s\" is a system catalog",
141+
RelationGetRelationName(rel))));
135142
}
136143

137144
Assert(rel);

src/test/regress/expected/stats_ext.out

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ begin
2323
end;
2424
$$;
2525
-- Verify failures
26+
CREATE TABLE ext_stats_test (x int, y int, z int);
2627
CREATE STATISTICS tst;
2728
ERROR: syntax error at or near ";"
2829
LINE 1: CREATE STATISTICS tst;
@@ -37,16 +38,17 @@ LINE 1: CREATE STATISTICS tst FROM sometab;
3738
^
3839
CREATE STATISTICS tst ON a, b FROM nonexistent;
3940
ERROR: relation "nonexistent" does not exist
40-
CREATE STATISTICS tst ON a, b FROM pg_class;
41+
CREATE STATISTICS tst ON a, b FROM ext_stats_test;
4142
ERROR: column "a" does not exist
42-
CREATE STATISTICS tst ON relname, relname, relnatts FROM pg_class;
43+
CREATE STATISTICS tst ON x, x, y FROM ext_stats_test;
4344
ERROR: duplicate column name in statistics definition
44-
CREATE STATISTICS tst ON relnatts + relpages FROM pg_class;
45+
CREATE STATISTICS tst ON x + y FROM ext_stats_test;
4546
ERROR: only simple column references are allowed in CREATE STATISTICS
46-
CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class;
47+
CREATE STATISTICS tst ON (x, y) FROM ext_stats_test;
4748
ERROR: only simple column references are allowed in CREATE STATISTICS
48-
CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class;
49+
CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test;
4950
ERROR: unrecognized statistics kind "unrecognized"
51+
DROP TABLE ext_stats_test;
5052
-- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it
5153
CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);
5254
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;

src/test/regress/sql/stats_ext.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ end;
2626
$$;
2727

2828
-- Verify failures
29+
CREATE TABLE ext_stats_test (x int, y int, z int);
2930
CREATE STATISTICS tst;
3031
CREATE STATISTICS tst ON a, b;
3132
CREATE STATISTICS tst FROM sometab;
3233
CREATE STATISTICS tst ON a, b FROM nonexistent;
33-
CREATE STATISTICS tst ON a, b FROM pg_class;
34-
CREATE STATISTICS tst ON relname, relname, relnatts FROM pg_class;
35-
CREATE STATISTICS tst ON relnatts + relpages FROM pg_class;
36-
CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class;
37-
CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class;
34+
CREATE STATISTICS tst ON a, b FROM ext_stats_test;
35+
CREATE STATISTICS tst ON x, x, y FROM ext_stats_test;
36+
CREATE STATISTICS tst ON x + y FROM ext_stats_test;
37+
CREATE STATISTICS tst ON (x, y) FROM ext_stats_test;
38+
CREATE STATISTICS tst (unrecognized) ON x, y FROM ext_stats_test;
39+
DROP TABLE ext_stats_test;
3840

3941
-- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it
4042
CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy