From 36b51d617c196d4882b784a381514fa28e6b30ee Mon Sep 17 00:00:00 2001 From: Naisila Puka <37271756+naisila@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:25:01 +0300 Subject: [PATCH] PG16 - Throw meaningful error for stats without a name on Citus tables (#7136) Relevant PG commit: https://github.com/postgres/postgres/commit/624aa2a13bd02dd584bb0995c883b5b93b2152df 624aa2a13bd02dd584bb0995c883b5b93b2152df --- src/backend/distributed/commands/statistics.c | 8 +++++++ src/test/regress/expected/pg16.out | 22 +++++++++++++++++++ src/test/regress/sql/pg16.sql | 15 +++++++++++++ 3 files changed, 45 insertions(+) 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;