aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJeff Davis <jdavis@postgresql.org>2024-10-23 16:11:45 -0700
committerJeff Davis <jdavis@postgresql.org>2024-10-23 16:16:39 -0700
commit4b096c67e0eed81e287094b9692fff72b9ea3eef (patch)
tree70d4a30bb8c4269f0db1bd15482d6beba85dee04 /src
parent7b8b8dddd68aca184ae12e01d2f12d1d338e8f90 (diff)
downloadpostgresql-4b096c67e0eed81e287094b9692fff72b9ea3eef.tar.gz
postgresql-4b096c67e0eed81e287094b9692fff72b9ea3eef.zip
Improve pg_set_attribute_stats() error message.
Previously, an invalid attribute name was caught, but the error message was unhelpful.
Diffstat (limited to 'src')
-rw-r--r--src/backend/statistics/attribute_stats.c10
-rw-r--r--src/test/regress/expected/stats_import.out23
-rw-r--r--src/test/regress/sql/stats_import.sql23
3 files changed, 54 insertions, 2 deletions
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index 086dceaeafe..979fe41a000 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -161,6 +161,11 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
attname = PG_GETARG_NAME(ATTNAME_ARG);
attnum = get_attnum(reloid, NameStr(*attname));
+ if (attnum == InvalidAttrNumber)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("column \"%s\" of relation \"%s\" does not exist",
+ NameStr(*attname), get_rel_name(reloid))));
stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
inherited = PG_GETARG_BOOL(INHERITED_ARG);
@@ -860,6 +865,11 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
attname = PG_GETARG_NAME(ATTNAME_ARG);
attnum = get_attnum(reloid, NameStr(*attname));
+ if (attnum == InvalidAttrNumber)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("column \"%s\" of relation \"%s\" does not exist",
+ NameStr(*attname), get_rel_name(reloid))));
stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
inherited = PG_GETARG_BOOL(INHERITED_ARG);
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 2868cc47f1d..6ab21ca324c 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -180,6 +180,12 @@ SELECT pg_catalog.pg_set_attribute_stats(
avg_width => 2::integer,
n_distinct => 0.3::real);
ERROR: could not open relation with OID 0
+-- error: object doesn't exist
+SELECT pg_catalog.pg_clear_attribute_stats(
+ relation => '0'::oid,
+ attname => 'id'::name,
+ inherited => false::boolean);
+ERROR: could not open relation with OID 0
-- error: relation null
SELECT pg_catalog.pg_set_attribute_stats(
relation => NULL::oid,
@@ -189,6 +195,21 @@ SELECT pg_catalog.pg_set_attribute_stats(
avg_width => 2::integer,
n_distinct => 0.3::real);
ERROR: "relation" cannot be NULL
+-- error: attname doesn't exist
+SELECT pg_catalog.pg_set_attribute_stats(
+ relation => 'stats_import.test'::regclass,
+ attname => 'nope'::name,
+ inherited => false::boolean,
+ null_frac => 0.1::real,
+ avg_width => 2::integer,
+ n_distinct => 0.3::real);
+ERROR: column "nope" of relation "test" does not exist
+-- error: attname doesn't exist
+SELECT pg_catalog.pg_clear_attribute_stats(
+ relation => 'stats_import.test'::regclass,
+ attname => 'nope'::name,
+ inherited => false::boolean);
+ERROR: column "nope" of relation "test" does not exist
-- error: attname null
SELECT pg_catalog.pg_set_attribute_stats(
relation => 'stats_import.test'::regclass,
@@ -301,7 +322,7 @@ SELECT pg_catalog.pg_set_attribute_stats(
most_common_freqs => '{0.2,0.1}'::real[]
);
ERROR: invalid input syntax for type integer: "2023-09-30"
--- warning: mcv cast failure
+-- error: mcv cast failure
SELECT pg_catalog.pg_set_attribute_stats(
relation => 'stats_import.test'::regclass,
attname => 'id'::name,
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index 9b6c9094862..b5689ac8fb3 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -130,6 +130,12 @@ SELECT pg_catalog.pg_set_attribute_stats(
avg_width => 2::integer,
n_distinct => 0.3::real);
+-- error: object doesn't exist
+SELECT pg_catalog.pg_clear_attribute_stats(
+ relation => '0'::oid,
+ attname => 'id'::name,
+ inherited => false::boolean);
+
-- error: relation null
SELECT pg_catalog.pg_set_attribute_stats(
relation => NULL::oid,
@@ -139,6 +145,21 @@ SELECT pg_catalog.pg_set_attribute_stats(
avg_width => 2::integer,
n_distinct => 0.3::real);
+-- error: attname doesn't exist
+SELECT pg_catalog.pg_set_attribute_stats(
+ relation => 'stats_import.test'::regclass,
+ attname => 'nope'::name,
+ inherited => false::boolean,
+ null_frac => 0.1::real,
+ avg_width => 2::integer,
+ n_distinct => 0.3::real);
+
+-- error: attname doesn't exist
+SELECT pg_catalog.pg_clear_attribute_stats(
+ relation => 'stats_import.test'::regclass,
+ attname => 'nope'::name,
+ inherited => false::boolean);
+
-- error: attname null
SELECT pg_catalog.pg_set_attribute_stats(
relation => 'stats_import.test'::regclass,
@@ -231,7 +252,7 @@ SELECT pg_catalog.pg_set_attribute_stats(
most_common_freqs => '{0.2,0.1}'::real[]
);
--- warning: mcv cast failure
+-- error: mcv cast failure
SELECT pg_catalog.pg_set_attribute_stats(
relation => 'stats_import.test'::regclass,
attname => 'id'::name,