Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 869ee4f

Browse files
committed
Disallow modifying statistics on system columns.
Reported-by: Heikki Linnakangas Discussion: https://postgr.es/m/df3e1c41-4e6c-40ad-9636-98deefe488cd@iki.fi
1 parent efdc7d7 commit 869ee4f

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/backend/statistics/attribute_stats.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
167167
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
168168
attname = PG_GETARG_NAME(ATTNAME_ARG);
169169
attnum = get_attnum(reloid, NameStr(*attname));
170+
171+
if (attnum < 0)
172+
ereport(ERROR,
173+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
174+
errmsg("cannot modify statistics on system column \"%s\"",
175+
NameStr(*attname))));
176+
170177
if (attnum == InvalidAttrNumber)
171178
ereport(ERROR,
172179
(errcode(ERRCODE_UNDEFINED_COLUMN),
@@ -882,6 +889,13 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
882889
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
883890
attname = PG_GETARG_NAME(ATTNAME_ARG);
884891
attnum = get_attnum(reloid, NameStr(*attname));
892+
893+
if (attnum < 0)
894+
ereport(ERROR,
895+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
896+
errmsg("cannot clear statistics on system column \"%s\"",
897+
NameStr(*attname))));
898+
885899
if (attnum == InvalidAttrNumber)
886900
ereport(ERROR,
887901
(errcode(ERRCODE_UNDEFINED_COLUMN),

src/test/regress/expected/stats_import.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ SELECT pg_catalog.pg_set_attribute_stats(
195195
avg_width => 2::integer,
196196
n_distinct => 0.3::real);
197197
ERROR: "relation" cannot be NULL
198+
-- error: attribute is system column
199+
SELECT pg_catalog.pg_set_attribute_stats(
200+
relation => 'stats_import.test'::regclass,
201+
attname => 'xmin'::name,
202+
inherited => false::boolean,
203+
null_frac => 0.1::real,
204+
avg_width => 2::integer,
205+
n_distinct => 0.3::real);
206+
ERROR: cannot modify statistics on system column "xmin"
198207
-- error: attname doesn't exist
199208
SELECT pg_catalog.pg_set_attribute_stats(
200209
relation => 'stats_import.test'::regclass,
@@ -204,6 +213,12 @@ SELECT pg_catalog.pg_set_attribute_stats(
204213
avg_width => 2::integer,
205214
n_distinct => 0.3::real);
206215
ERROR: column "nope" of relation "test" does not exist
216+
-- error: attribute is system column
217+
SELECT pg_catalog.pg_clear_attribute_stats(
218+
relation => 'stats_import.test'::regclass,
219+
attname => 'ctid'::name,
220+
inherited => false::boolean);
221+
ERROR: cannot clear statistics on system column "ctid"
207222
-- error: attname doesn't exist
208223
SELECT pg_catalog.pg_clear_attribute_stats(
209224
relation => 'stats_import.test'::regclass,

src/test/regress/sql/stats_import.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ SELECT pg_catalog.pg_set_attribute_stats(
145145
avg_width => 2::integer,
146146
n_distinct => 0.3::real);
147147

148+
-- error: attribute is system column
149+
SELECT pg_catalog.pg_set_attribute_stats(
150+
relation => 'stats_import.test'::regclass,
151+
attname => 'xmin'::name,
152+
inherited => false::boolean,
153+
null_frac => 0.1::real,
154+
avg_width => 2::integer,
155+
n_distinct => 0.3::real);
156+
148157
-- error: attname doesn't exist
149158
SELECT pg_catalog.pg_set_attribute_stats(
150159
relation => 'stats_import.test'::regclass,
@@ -154,6 +163,12 @@ SELECT pg_catalog.pg_set_attribute_stats(
154163
avg_width => 2::integer,
155164
n_distinct => 0.3::real);
156165

166+
-- error: attribute is system column
167+
SELECT pg_catalog.pg_clear_attribute_stats(
168+
relation => 'stats_import.test'::regclass,
169+
attname => 'ctid'::name,
170+
inherited => false::boolean);
171+
157172
-- error: attname doesn't exist
158173
SELECT pg_catalog.pg_clear_attribute_stats(
159174
relation => 'stats_import.test'::regclass,

0 commit comments

Comments
 (0)