From d8639d58de527f9a14abf768ab945c9fe2e07b46 Mon Sep 17 00:00:00 2001 From: gindibay Date: Tue, 31 Oct 2023 10:58:44 +0300 Subject: [PATCH] Adds locale restrictions --- src/backend/distributed/commands/database.c | 17 +++++++ .../commands/distribute_object_ops.c | 2 +- .../deparser/deparse_database_stmts.c | 15 +++++++ src/include/distributed/commands.h | 2 + .../create_drop_database_propagation.out | 40 ++++++++++++----- .../create_drop_database_propagation_pg15.out | 38 +++------------- .../sql/create_drop_database_propagation.sql | 44 ++++++++++++++----- .../create_drop_database_propagation_pg15.sql | 27 +----------- 8 files changed, 106 insertions(+), 79 deletions(-) diff --git a/src/backend/distributed/commands/database.c b/src/backend/distributed/commands/database.c index 42ef2ed43..4867c55c2 100644 --- a/src/backend/distributed/commands/database.c +++ b/src/backend/distributed/commands/database.c @@ -263,6 +263,23 @@ PreprocessAlterDatabaseSetStmt(Node *node, const char *queryString, return NodeDDLTaskList(NON_COORDINATOR_NODES, commands); } +List * +PreprocessCreateDatabaseStmt(Node *node, const char *queryString, + ProcessUtilityContext processUtilityContext) +{ + if (!EnableCreateDatabasePropagation || !ShouldPropagate()) + { + return NIL; + } + + EnsureCoordinator(); + + //Validate the statement + DeparseTreeNode(node); + + return NIL; +} + /* * PostprocessCreatedbStmt is executed after the statement is applied to the local diff --git a/src/backend/distributed/commands/distribute_object_ops.c b/src/backend/distributed/commands/distribute_object_ops.c index 2888bef1d..37ad4c5a4 100644 --- a/src/backend/distributed/commands/distribute_object_ops.c +++ b/src/backend/distributed/commands/distribute_object_ops.c @@ -469,7 +469,7 @@ static DistributeObjectOps Database_Alter = { static DistributeObjectOps Database_Create = { .deparse = DeparseCreateDatabaseStmt, .qualify = NULL, - .preprocess = NULL, + .preprocess = PreprocessCreateDatabaseStmt, .postprocess = PostprocessCreateDatabaseStmt, .objectType = OBJECT_DATABASE, .operationType = DIST_OPS_CREATE, diff --git a/src/backend/distributed/deparser/deparse_database_stmts.c b/src/backend/distributed/deparser/deparse_database_stmts.c index bf98b9622..169ca40e8 100644 --- a/src/backend/distributed/deparser/deparse_database_stmts.c +++ b/src/backend/distributed/deparser/deparse_database_stmts.c @@ -237,6 +237,21 @@ AppendCreateDatabaseStmt(StringInfo buf, CreatedbStmt *stmt) foreach_ptr(option, stmt->options) { + //If option is template, lc_type, locale or lc_collate, propagation will not be supportted + // since template database is not stored in the catalog + if (strcmp(option->defname, "template") == 0 || + strcmp(option->defname, "strategy") == 0 || + strcmp(option->defname, "lc_ctype") == 0 || + strcmp(option->defname, "locale") == 0 || + strcmp(option->defname, "lc_collate") == 0 || + strcmp(option->defname, "icu_locale") == 0 || + strcmp(option->defname, "locale_provider") == 0 ) + { + ereport(ERROR, + errmsg("CREATE DATABASE option \"%s\" is not supported", + option->defname)); + } + optionToStatement(buf, option, create_database_option_formats, lengthof( create_database_option_formats)); } diff --git a/src/include/distributed/commands.h b/src/include/distributed/commands.h index a4f890f9e..28828075b 100644 --- a/src/include/distributed/commands.h +++ b/src/include/distributed/commands.h @@ -237,6 +237,8 @@ extern List * CreateDatabaseStmtObjectAddress(Node *node, bool missing_ok, bool extern List * PreprocessAlterDatabaseSetStmt(Node *node, const char *queryString, ProcessUtilityContext processUtilityContext); +extern List * PreprocessCreateDatabaseStmt(Node *node, const char *queryString, + ProcessUtilityContext processUtilityContext); extern List * PostprocessCreateDatabaseStmt(Node *node, const char *queryString); extern List * PreprocessDropDatabaseStmt(Node *node, const char *queryString, ProcessUtilityContext processUtilityContext); diff --git a/src/test/regress/expected/create_drop_database_propagation.out b/src/test/regress/expected/create_drop_database_propagation.out index fd7173ce2..9489664eb 100644 --- a/src/test/regress/expected/create_drop_database_propagation.out +++ b/src/test/regress/expected/create_drop_database_propagation.out @@ -14,11 +14,8 @@ create user create_drop_db_test_user; set citus.enable_create_database_propagation=on; CREATE DATABASE mydatabase WITH OWNER = create_drop_db_test_user - TEMPLATE = 'template0' ENCODING = 'UTF8' CONNECTION LIMIT = 10 - LC_COLLATE = 'C' - LC_CTYPE = 'C' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = true IS_TEMPLATE = false; @@ -74,12 +71,9 @@ select 1 from citus_remove_node('localhost', :worker_2_port); --test with is_template true and allow connections false CREATE DATABASE mydatabase - WITH TEMPLATE = 'template0' OWNER = create_drop_db_test_user CONNECTION LIMIT = 10 ENCODING = 'UTF8' - LC_COLLATE = 'C' - LC_CTYPE = 'C' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = false IS_TEMPLATE = false; @@ -161,11 +155,8 @@ SELECT result from run_command_on_all_nodes( -- create a template database with all options set and allow connections false CREATE DATABASE my_template_database - WITH TEMPLATE = 'template0' - OWNER = create_drop_db_test_user + WITH OWNER = create_drop_db_test_user ENCODING = 'UTF8' - LC_COLLATE = 'C' - LC_CTYPE = 'C' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = false IS_TEMPLATE = true; @@ -248,6 +239,35 @@ NOTICE: issuing DROP DATABASE IF EXISTS "mydatabase#1'2" DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx NOTICE: issuing DROP DATABASE IF EXISTS "mydatabase#1'2" DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx +--test for unsupported options +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LC_CTYPE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; +ERROR: CREATE DATABASE option "lc_ctype" is not supported +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LC_CTYPE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; +ERROR: CREATE DATABASE option "lc_ctype" is not supported +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LC_COLLATE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; +ERROR: CREATE DATABASE option "lc_collate" is not supported +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LOCALE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; +ERROR: CREATE DATABASE option "locale" is not supported --clean up resources created by this test drop tablespace create_drop_db_tablespace; \c - - - :worker_1_port diff --git a/src/test/regress/expected/create_drop_database_propagation_pg15.out b/src/test/regress/expected/create_drop_database_propagation_pg15.out index e1073f980..bc6374803 100644 --- a/src/test/regress/expected/create_drop_database_propagation_pg15.out +++ b/src/test/regress/expected/create_drop_database_propagation_pg15.out @@ -23,24 +23,17 @@ set citus.enable_create_database_propagation=on; SET citus.log_remote_commands = true; set citus.grep_remote_commands = '%CREATE DATABASE%'; CREATE DATABASE mydatabase - WITH TEMPLATE = 'template0' + WITH OWNER = create_drop_db_test_user CONNECTION LIMIT = 10 ENCODING = 'UTF8' - STRATEGY = 'wal_log' - LOCALE = '' - LC_COLLATE = 'POSIX' - LC_CTYPE = 'POSIX' - ICU_LOCALE = 'und' - LOCALE_PROVIDER = 'icu' - COLLATION_VERSION = '1.0' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = true IS_TEMPLATE = false OID = 966345; -NOTICE: issuing CREATE DATABASE mydatabase TEMPLATE template0 OWNER create_drop_db_test_user CONNECTION LIMIT 10 ENCODING 'UTF8' STRATEGY 'wal_log' LOCALE '' LC_COLLATE 'POSIX' LC_CTYPE 'POSIX' ICU_LOCALE 'und' LOCALE_PROVIDER 'icu' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS true IS_TEMPLATE false OID 966345 +NOTICE: issuing CREATE DATABASE mydatabase OWNER create_drop_db_test_user CONNECTION LIMIT 10 ENCODING 'UTF8' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS true IS_TEMPLATE false OID 966345 DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx -NOTICE: issuing CREATE DATABASE mydatabase TEMPLATE template0 OWNER create_drop_db_test_user CONNECTION LIMIT 10 ENCODING 'UTF8' STRATEGY 'wal_log' LOCALE '' LC_COLLATE 'POSIX' LC_CTYPE 'POSIX' ICU_LOCALE 'und' LOCALE_PROVIDER 'icu' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS true IS_TEMPLATE false OID 966345 +NOTICE: issuing CREATE DATABASE mydatabase OWNER create_drop_db_test_user CONNECTION LIMIT 10 ENCODING 'UTF8' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS true IS_TEMPLATE false OID 966345 DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx SET citus.log_remote_commands = false; SELECT result from run_command_on_all_nodes( @@ -104,16 +97,11 @@ set citus.grep_remote_commands = '%CREATE DATABASE%'; CREATE DATABASE mydatabase2 WITH OWNER = create_drop_db_test_user ENCODING = 'UTF8' - STRATEGY = 'wal_log' - LOCALE = 'en_US.utf8' - LC_COLLATE = 'POSIX' - LC_CTYPE = 'POSIX' - COLLATION_VERSION = '1.0' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = true IS_TEMPLATE = false OID = 966345; -NOTICE: issuing CREATE DATABASE mydatabase2 OWNER create_drop_db_test_user ENCODING 'UTF8' STRATEGY 'wal_log' LOCALE 'en_US.utf8' LC_COLLATE 'POSIX' LC_CTYPE 'POSIX' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS true IS_TEMPLATE false OID 966345 +NOTICE: issuing CREATE DATABASE mydatabase2 OWNER create_drop_db_test_user ENCODING 'UTF8' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS true IS_TEMPLATE false OID 966345 DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx SET citus.log_remote_commands = false; SELECT result from run_command_on_all_nodes( @@ -196,22 +184,15 @@ SET citus.log_remote_commands = true; set citus.grep_remote_commands = '%CREATE DATABASE%'; -- create a template database with all options set and allow connections false CREATE DATABASE my_template_database - WITH TEMPLATE = 'template0' - OWNER = create_drop_db_test_user + WITH OWNER = create_drop_db_test_user ENCODING = 'UTF8' - STRATEGY = 'wal_log' - LOCALE = 'en_US.utf8' - LC_COLLATE = 'POSIX' - LC_CTYPE = 'POSIX' - ICU_LOCALE = 'en-US' - LOCALE_PROVIDER = 'icu' COLLATION_VERSION = '1.0' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = false IS_TEMPLATE = true; -NOTICE: issuing CREATE DATABASE my_template_database TEMPLATE template0 OWNER create_drop_db_test_user ENCODING 'UTF8' STRATEGY 'wal_log' LOCALE 'en_US.utf8' LC_COLLATE 'POSIX' LC_CTYPE 'POSIX' ICU_LOCALE 'en-US' LOCALE_PROVIDER 'icu' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS false IS_TEMPLATE true +NOTICE: issuing CREATE DATABASE my_template_database OWNER create_drop_db_test_user ENCODING 'UTF8' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS false IS_TEMPLATE true DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx -NOTICE: issuing CREATE DATABASE my_template_database TEMPLATE template0 OWNER create_drop_db_test_user ENCODING 'UTF8' STRATEGY 'wal_log' LOCALE 'en_US.utf8' LC_COLLATE 'POSIX' LC_CTYPE 'POSIX' ICU_LOCALE 'en-US' LOCALE_PROVIDER 'icu' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS false IS_TEMPLATE true +NOTICE: issuing CREATE DATABASE my_template_database OWNER create_drop_db_test_user ENCODING 'UTF8' COLLATION_VERSION '1.0' TABLESPACE create_drop_db_tablespace ALLOW_CONNECTIONS false IS_TEMPLATE true DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx SET citus.log_remote_commands = false; SELECT result from run_command_on_all_nodes( @@ -235,10 +216,6 @@ SELECT result from run_command_on_all_nodes( [{"datacl": null, "datname": "my_template_database", "datctype": "C", "encoding": "UTF8", "datcollate": "C", "tablespace": "create_drop_db_tablespace", "datallowconn": false, "datconnlimit": -1, "datistemplate": true, "database_owner": "create_drop_db_test_user"}] (3 rows) -SET citus.log_remote_commands = true; -set citus.grep_remote_commands = '%DROP DATABASE%'; -drop database my_template_database; -ERROR: cannot drop a template database SET citus.log_remote_commands = false; SELECT result from run_command_on_all_nodes( $$ @@ -275,7 +252,6 @@ SELECT result from run_command_on_all_nodes( UPDATE 1 (3 rows) -; set citus.grep_remote_commands = '%DROP DATABASE%'; drop database my_template_database; NOTICE: issuing DROP DATABASE my_template_database diff --git a/src/test/regress/sql/create_drop_database_propagation.sql b/src/test/regress/sql/create_drop_database_propagation.sql index 540d6a9e6..ae90088d1 100644 --- a/src/test/regress/sql/create_drop_database_propagation.sql +++ b/src/test/regress/sql/create_drop_database_propagation.sql @@ -21,17 +21,12 @@ set citus.enable_create_database_propagation=on; CREATE DATABASE mydatabase WITH OWNER = create_drop_db_test_user - TEMPLATE = 'template0' ENCODING = 'UTF8' CONNECTION LIMIT = 10 - LC_COLLATE = 'C' - LC_CTYPE = 'C' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = true IS_TEMPLATE = false; - - SELECT result from run_command_on_all_nodes( $$ SELECT jsonb_agg(to_jsonb(q2.*)) FROM ( @@ -73,12 +68,9 @@ select 1 from citus_remove_node('localhost', :worker_2_port); --test with is_template true and allow connections false CREATE DATABASE mydatabase - WITH TEMPLATE = 'template0' OWNER = create_drop_db_test_user CONNECTION LIMIT = 10 ENCODING = 'UTF8' - LC_COLLATE = 'C' - LC_CTYPE = 'C' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = false IS_TEMPLATE = false; @@ -139,11 +131,8 @@ SELECT result from run_command_on_all_nodes( -- create a template database with all options set and allow connections false CREATE DATABASE my_template_database - WITH TEMPLATE = 'template0' - OWNER = create_drop_db_test_user + WITH OWNER = create_drop_db_test_user ENCODING = 'UTF8' - LC_COLLATE = 'C' - LC_CTYPE = 'C' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = false IS_TEMPLATE = true; @@ -203,6 +192,37 @@ create database "mydatabase#1'2"; set citus.grep_remote_commands = '%DROP DATABASE%'; drop database if exists "mydatabase#1'2"; +--test for unsupported options + +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LC_CTYPE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; + +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LC_CTYPE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; + +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LC_COLLATE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; + +CREATE DATABASE mydatabase + with CONNECTION LIMIT = 10 + ENCODING = 'UTF8' + LOCALE = 'C.UTF-8' + ALLOW_CONNECTIONS = false + IS_TEMPLATE = false; + + --clean up resources created by this test drop tablespace create_drop_db_tablespace; diff --git a/src/test/regress/sql/create_drop_database_propagation_pg15.sql b/src/test/regress/sql/create_drop_database_propagation_pg15.sql index 3a8e80ebf..ca3e3b202 100644 --- a/src/test/regress/sql/create_drop_database_propagation_pg15.sql +++ b/src/test/regress/sql/create_drop_database_propagation_pg15.sql @@ -29,17 +29,10 @@ set citus.enable_create_database_propagation=on; SET citus.log_remote_commands = true; set citus.grep_remote_commands = '%CREATE DATABASE%'; CREATE DATABASE mydatabase - WITH TEMPLATE = 'template0' + WITH OWNER = create_drop_db_test_user CONNECTION LIMIT = 10 ENCODING = 'UTF8' - STRATEGY = 'wal_log' - LOCALE = '' - LC_COLLATE = 'POSIX' - LC_CTYPE = 'POSIX' - ICU_LOCALE = 'und' - LOCALE_PROVIDER = 'icu' - COLLATION_VERSION = '1.0' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = true IS_TEMPLATE = false @@ -91,11 +84,6 @@ set citus.grep_remote_commands = '%CREATE DATABASE%'; CREATE DATABASE mydatabase2 WITH OWNER = create_drop_db_test_user ENCODING = 'UTF8' - STRATEGY = 'wal_log' - LOCALE = 'en_US.utf8' - LC_COLLATE = 'POSIX' - LC_CTYPE = 'POSIX' - COLLATION_VERSION = '1.0' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = true IS_TEMPLATE = false @@ -161,15 +149,8 @@ set citus.grep_remote_commands = '%CREATE DATABASE%'; -- create a template database with all options set and allow connections false CREATE DATABASE my_template_database - WITH TEMPLATE = 'template0' - OWNER = create_drop_db_test_user + WITH OWNER = create_drop_db_test_user ENCODING = 'UTF8' - STRATEGY = 'wal_log' - LOCALE = 'en_US.utf8' - LC_COLLATE = 'POSIX' - LC_CTYPE = 'POSIX' - ICU_LOCALE = 'en-US' - LOCALE_PROVIDER = 'icu' COLLATION_VERSION = '1.0' TABLESPACE = create_drop_db_tablespace ALLOW_CONNECTIONS = false @@ -192,9 +173,6 @@ SELECT result from run_command_on_all_nodes( $$ ) ORDER BY result; -SET citus.log_remote_commands = true; -set citus.grep_remote_commands = '%DROP DATABASE%'; -drop database my_template_database; SET citus.log_remote_commands = false; @@ -222,7 +200,6 @@ SELECT result from run_command_on_all_nodes( $$ ) ORDER BY result; -; set citus.grep_remote_commands = '%DROP DATABASE%'; drop database my_template_database;