diff --git a/src/backend/distributed/commands/statistics.c b/src/backend/distributed/commands/statistics.c index a65d6c0fe..e5d5ac8ce 100644 --- a/src/backend/distributed/commands/statistics.c +++ b/src/backend/distributed/commands/statistics.c @@ -77,6 +77,14 @@ PreprocessCreateStatisticsStmt(Node *node, const char *queryString, EnsureCoordinator(); + if (!(stmt->defnames)) + { + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics without a name on a " + "Citus table"), + errhint("Consider specifying a name for the statistics"))); + } + QualifyTreeNode((Node *) stmt); Oid statsOid = get_statistics_object_oid(stmt->defnames, true); diff --git a/src/test/regress/expected/pg16.out b/src/test/regress/expected/pg16.out index 37580d8a7..ae93b8eb8 100644 --- a/src/test/regress/expected/pg16.out +++ b/src/test/regress/expected/pg16.out @@ -65,6 +65,28 @@ SET citus.log_remote_commands TO OFF; -- only verifying it works and not printing log -- remote commands because it can be flaky VACUUM (ONLY_DATABASE_STATS); +-- Proper error when creating statistics without a name on a Citus table +-- Relevant PG commit: +-- https://github.com/postgres/postgres/commit/624aa2a13bd02dd584bb0995c883b5b93b2152df +CREATE TABLE test_stats ( + a int, + b int +); +SELECT create_distributed_table('test_stats', 'a'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +CREATE STATISTICS (dependencies) ON a, b FROM test_stats; +ERROR: cannot create statistics without a name on a Citus table +HINT: Consider specifying a name for the statistics +CREATE STATISTICS (ndistinct, dependencies) on a, b from test_stats; +ERROR: cannot create statistics without a name on a Citus table +HINT: Consider specifying a name for the statistics +CREATE STATISTICS (ndistinct, dependencies, mcv) on a, b from test_stats; +ERROR: cannot create statistics without a name on a Citus table +HINT: Consider specifying a name for the statistics \set VERBOSITY terse SET client_min_messages TO ERROR; DROP SCHEMA pg16 CASCADE; diff --git a/src/test/regress/sql/pg16.sql b/src/test/regress/sql/pg16.sql index 29638ac1c..e9806c838 100644 --- a/src/test/regress/sql/pg16.sql +++ b/src/test/regress/sql/pg16.sql @@ -45,6 +45,21 @@ SET citus.log_remote_commands TO OFF; -- remote commands because it can be flaky VACUUM (ONLY_DATABASE_STATS); +-- Proper error when creating statistics without a name on a Citus table +-- Relevant PG commit: +-- https://github.com/postgres/postgres/commit/624aa2a13bd02dd584bb0995c883b5b93b2152df + +CREATE TABLE test_stats ( + a int, + b int +); + +SELECT create_distributed_table('test_stats', 'a'); + +CREATE STATISTICS (dependencies) ON a, b FROM test_stats; +CREATE STATISTICS (ndistinct, dependencies) on a, b from test_stats; +CREATE STATISTICS (ndistinct, dependencies, mcv) on a, b from test_stats; + \set VERBOSITY terse SET client_min_messages TO ERROR; DROP SCHEMA pg16 CASCADE;