Skip to content

Commit 4b096c6

Browse files
committed
Improve pg_set_attribute_stats() error message.
Previously, an invalid attribute name was caught, but the error message was unhelpful.
1 parent 7b8b8dd commit 4b096c6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/backend/statistics/attribute_stats.c

+10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
161161
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
162162
attname = PG_GETARG_NAME(ATTNAME_ARG);
163163
attnum = get_attnum(reloid, NameStr(*attname));
164+
if (attnum == InvalidAttrNumber)
165+
ereport(ERROR,
166+
(errcode(ERRCODE_UNDEFINED_COLUMN),
167+
errmsg("column \"%s\" of relation \"%s\" does not exist",
168+
NameStr(*attname), get_rel_name(reloid))));
164169

165170
stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
166171
inherited = PG_GETARG_BOOL(INHERITED_ARG);
@@ -860,6 +865,11 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
860865
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
861866
attname = PG_GETARG_NAME(ATTNAME_ARG);
862867
attnum = get_attnum(reloid, NameStr(*attname));
868+
if (attnum == InvalidAttrNumber)
869+
ereport(ERROR,
870+
(errcode(ERRCODE_UNDEFINED_COLUMN),
871+
errmsg("column \"%s\" of relation \"%s\" does not exist",
872+
NameStr(*attname), get_rel_name(reloid))));
863873

864874
stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
865875
inherited = PG_GETARG_BOOL(INHERITED_ARG);

src/test/regress/expected/stats_import.out

+22-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ SELECT pg_catalog.pg_set_attribute_stats(
180180
avg_width => 2::integer,
181181
n_distinct => 0.3::real);
182182
ERROR: could not open relation with OID 0
183+
-- error: object doesn't exist
184+
SELECT pg_catalog.pg_clear_attribute_stats(
185+
relation => '0'::oid,
186+
attname => 'id'::name,
187+
inherited => false::boolean);
188+
ERROR: could not open relation with OID 0
183189
-- error: relation null
184190
SELECT pg_catalog.pg_set_attribute_stats(
185191
relation => NULL::oid,
@@ -189,6 +195,21 @@ SELECT pg_catalog.pg_set_attribute_stats(
189195
avg_width => 2::integer,
190196
n_distinct => 0.3::real);
191197
ERROR: "relation" cannot be NULL
198+
-- error: attname doesn't exist
199+
SELECT pg_catalog.pg_set_attribute_stats(
200+
relation => 'stats_import.test'::regclass,
201+
attname => 'nope'::name,
202+
inherited => false::boolean,
203+
null_frac => 0.1::real,
204+
avg_width => 2::integer,
205+
n_distinct => 0.3::real);
206+
ERROR: column "nope" of relation "test" does not exist
207+
-- error: attname doesn't exist
208+
SELECT pg_catalog.pg_clear_attribute_stats(
209+
relation => 'stats_import.test'::regclass,
210+
attname => 'nope'::name,
211+
inherited => false::boolean);
212+
ERROR: column "nope" of relation "test" does not exist
192213
-- error: attname null
193214
SELECT pg_catalog.pg_set_attribute_stats(
194215
relation => 'stats_import.test'::regclass,
@@ -301,7 +322,7 @@ SELECT pg_catalog.pg_set_attribute_stats(
301322
most_common_freqs => '{0.2,0.1}'::real[]
302323
);
303324
ERROR: invalid input syntax for type integer: "2023-09-30"
304-
-- warning: mcv cast failure
325+
-- error: mcv cast failure
305326
SELECT pg_catalog.pg_set_attribute_stats(
306327
relation => 'stats_import.test'::regclass,
307328
attname => 'id'::name,

src/test/regress/sql/stats_import.sql

+22-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ SELECT pg_catalog.pg_set_attribute_stats(
130130
avg_width => 2::integer,
131131
n_distinct => 0.3::real);
132132

133+
-- error: object doesn't exist
134+
SELECT pg_catalog.pg_clear_attribute_stats(
135+
relation => '0'::oid,
136+
attname => 'id'::name,
137+
inherited => false::boolean);
138+
133139
-- error: relation null
134140
SELECT pg_catalog.pg_set_attribute_stats(
135141
relation => NULL::oid,
@@ -139,6 +145,21 @@ SELECT pg_catalog.pg_set_attribute_stats(
139145
avg_width => 2::integer,
140146
n_distinct => 0.3::real);
141147

148+
-- error: attname doesn't exist
149+
SELECT pg_catalog.pg_set_attribute_stats(
150+
relation => 'stats_import.test'::regclass,
151+
attname => 'nope'::name,
152+
inherited => false::boolean,
153+
null_frac => 0.1::real,
154+
avg_width => 2::integer,
155+
n_distinct => 0.3::real);
156+
157+
-- error: attname doesn't exist
158+
SELECT pg_catalog.pg_clear_attribute_stats(
159+
relation => 'stats_import.test'::regclass,
160+
attname => 'nope'::name,
161+
inherited => false::boolean);
162+
142163
-- error: attname null
143164
SELECT pg_catalog.pg_set_attribute_stats(
144165
relation => 'stats_import.test'::regclass,
@@ -231,7 +252,7 @@ SELECT pg_catalog.pg_set_attribute_stats(
231252
most_common_freqs => '{0.2,0.1}'::real[]
232253
);
233254

234-
-- warning: mcv cast failure
255+
-- error: mcv cast failure
235256
SELECT pg_catalog.pg_set_attribute_stats(
236257
relation => 'stats_import.test'::regclass,
237258
attname => 'id'::name,

0 commit comments

Comments
 (0)