diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index 1a68dbfb1..f405350e2 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -79,8 +79,7 @@ static void InsertIntoPgDistPartition(Oid relationId, char distributionMethod, char replicationModel); static void CreateHashDistributedTable(Oid relationId, char *distributionColumnName, char *colocateWithTableName, - int shardCount, int replicationFactor, - char replicationModel); + int shardCount, int replicationFactor); static Oid ColumnType(Oid relationId, char *columnName); @@ -172,8 +171,7 @@ create_distributed_table(PG_FUNCTION_ARGS) /* use configuration values for shard count and shard replication factor */ CreateHashDistributedTable(relationId, distributionColumnName, colocateWithTableName, ShardCount, - ShardReplicationFactor, - REPLICATION_MODEL_COORDINATOR); + ShardReplicationFactor); PG_RETURN_VOID(); } @@ -953,17 +951,28 @@ CreateTruncateTrigger(Oid relationId) static void CreateHashDistributedTable(Oid relationId, char *distributionColumnName, char *colocateWithTableName, int shardCount, - int replicationFactor, char replicationModel) + int replicationFactor) { Relation distributedRelation = NULL; Relation pgDistColocation = NULL; uint32 colocationId = INVALID_COLOCATION_ID; Oid sourceRelationId = InvalidOid; Oid distributionColumnType = InvalidOid; + char replicationModel = 0; /* get an access lock on the relation to prevent DROP TABLE and ALTER TABLE */ distributedRelation = relation_open(relationId, AccessShareLock); + /* all hash-distributed tables with repfactor=1 are treated as MX tables */ + if (replicationFactor == 1) + { + replicationModel = REPLICATION_MODEL_STREAMING; + } + else + { + replicationModel = REPLICATION_MODEL_COORDINATOR; + } + /* * Get an exclusive lock on the colocation system catalog. Therefore, we * can be sure that there will no modifications on the colocation table @@ -1004,7 +1013,7 @@ CreateHashDistributedTable(Oid relationId, char *distributionColumnName, /* create distributed table metadata */ ConvertToDistributedTable(relationId, distributionColumnName, DISTRIBUTE_BY_HASH, - colocationId, REPLICATION_MODEL_COORDINATOR); + colocationId, replicationModel); /* create shards */ if (sourceRelationId != InvalidOid) diff --git a/src/test/regress/expected/multi_colocation_utils.out b/src/test/regress/expected/multi_colocation_utils.out index cc0e44623..724f5fa04 100644 --- a/src/test/regress/expected/multi_colocation_utils.out +++ b/src/test/regress/expected/multi_colocation_utils.out @@ -371,6 +371,8 @@ SELECT create_distributed_table('table2_groupB', 'id'); (1 row) +UPDATE pg_dist_partition SET repmodel='c' WHERE logicalrelid='table1_groupB'::regclass; +UPDATE pg_dist_partition SET repmodel='c' WHERE logicalrelid='table2_groupB'::regclass; -- revert back to default shard replication factor SET citus.shard_replication_factor to DEFAULT; -- change partition column type diff --git a/src/test/regress/expected/multi_create_table.out b/src/test/regress/expected/multi_create_table.out index 4ce0aad0c..27caeeb87 100644 --- a/src/test/regress/expected/multi_create_table.out +++ b/src/test/regress/expected/multi_create_table.out @@ -137,3 +137,53 @@ SELECT master_create_distributed_table('supplier_single_shard', 's_suppkey', 'ap (1 row) +-- Show that when a hash distributed table with replication factor=1 is created, it +-- automatically marked as streaming replicated +SET citus.shard_replication_factor TO 1; +CREATE TABLE mx_table_test (col1 int, col2 text); +SELECT create_distributed_table('mx_table_test', 'col1'); + create_distributed_table +-------------------------- + +(1 row) + +SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; + repmodel +---------- + s +(1 row) + +DROP TABLE mx_table_test; +-- Show that it is not possible to create an mx table with the old +-- master_create_distributed_table function +CREATE TABLE mx_table_test (col1 int, col2 text); +SELECT master_create_distributed_table('mx_table_test', 'col1', 'hash'); + master_create_distributed_table +--------------------------------- + +(1 row) + +SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; + repmodel +---------- + c +(1 row) + +DROP TABLE mx_table_test; +-- Show that when replication factor > 1 the table is created as coordinator-replicated +SET citus.shard_replication_factor TO 2; +CREATE TABLE mx_table_test (col1 int, col2 text); +SELECT create_distributed_table('mx_table_test', 'col1'); + create_distributed_table +-------------------------- + +(1 row) + +SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; + repmodel +---------- + c +(1 row) + +DROP TABLE mx_table_test; +SET citus.shard_replication_factor TO default; diff --git a/src/test/regress/sql/multi_colocation_utils.sql b/src/test/regress/sql/multi_colocation_utils.sql index c1dfaeedb..336758f56 100644 --- a/src/test/regress/sql/multi_colocation_utils.sql +++ b/src/test/regress/sql/multi_colocation_utils.sql @@ -175,6 +175,9 @@ SELECT create_distributed_table('table1_groupB', 'id'); CREATE TABLE table2_groupB ( id int ); SELECT create_distributed_table('table2_groupB', 'id'); +UPDATE pg_dist_partition SET repmodel='c' WHERE logicalrelid='table1_groupB'::regclass; +UPDATE pg_dist_partition SET repmodel='c' WHERE logicalrelid='table2_groupB'::regclass; + -- revert back to default shard replication factor SET citus.shard_replication_factor to DEFAULT; diff --git a/src/test/regress/sql/multi_create_table.sql b/src/test/regress/sql/multi_create_table.sql index 17e12b7a2..1f0dfef22 100644 --- a/src/test/regress/sql/multi_create_table.sql +++ b/src/test/regress/sql/multi_create_table.sql @@ -111,3 +111,28 @@ CREATE TABLE supplier_single_shard s_comment varchar(101) not null ); SELECT master_create_distributed_table('supplier_single_shard', 's_suppkey', 'append'); + +-- Show that when a hash distributed table with replication factor=1 is created, it +-- automatically marked as streaming replicated +SET citus.shard_replication_factor TO 1; + +CREATE TABLE mx_table_test (col1 int, col2 text); +SELECT create_distributed_table('mx_table_test', 'col1'); +SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; +DROP TABLE mx_table_test; + +-- Show that it is not possible to create an mx table with the old +-- master_create_distributed_table function +CREATE TABLE mx_table_test (col1 int, col2 text); +SELECT master_create_distributed_table('mx_table_test', 'col1', 'hash'); +SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; +DROP TABLE mx_table_test; + +-- Show that when replication factor > 1 the table is created as coordinator-replicated +SET citus.shard_replication_factor TO 2; +CREATE TABLE mx_table_test (col1 int, col2 text); +SELECT create_distributed_table('mx_table_test', 'col1'); +SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; +DROP TABLE mx_table_test; + +SET citus.shard_replication_factor TO default;