diff --git a/dbms/src/Storages/StorageReplicatedMergeTree.cpp b/dbms/src/Storages/StorageReplicatedMergeTree.cpp index cc92dd4fac5..c73af3786c7 100644 --- a/dbms/src/Storages/StorageReplicatedMergeTree.cpp +++ b/dbms/src/Storages/StorageReplicatedMergeTree.cpp @@ -3014,7 +3014,7 @@ bool StorageReplicatedMergeTree::optimize(const ASTPtr & query, const ASTPtr & p return true; } - ReplicatedMergeTreeLogEntryData merge_entry; + std::vector merge_entries; { /// We must select parts for merge under merge_selecting_mutex because other threads /// (merge_selecting_thread or OPTIMIZE queries) could assign new merges. @@ -3045,9 +3045,12 @@ bool StorageReplicatedMergeTree::optimize(const ASTPtr & query, const ASTPtr & p FutureMergedMutatedPart future_merged_part; bool selected = merger_mutator.selectAllPartsToMergeWithinPartition( future_merged_part, disk_space, can_merge, partition_id, true, nullptr); + ReplicatedMergeTreeLogEntryData merge_entry; if (selected && !createLogEntryToMergeParts(zookeeper, future_merged_part.parts, future_merged_part.name, deduplicate, &merge_entry)) return handle_noop("Can't create merge queue node in ZooKeeper"); + if (merge_entry.type != ReplicatedMergeTreeLogEntryData::Type::EMPTY) + merge_entries.push_back(std::move(merge_entry)); } } else @@ -3074,15 +3077,20 @@ bool StorageReplicatedMergeTree::optimize(const ASTPtr & query, const ASTPtr & p return handle_noop(disable_reason); } + ReplicatedMergeTreeLogEntryData merge_entry; if (!createLogEntryToMergeParts(zookeeper, future_merged_part.parts, future_merged_part.name, deduplicate, &merge_entry)) return handle_noop("Can't create merge queue node in ZooKeeper"); + if (merge_entry.type != ReplicatedMergeTreeLogEntryData::Type::EMPTY) + merge_entries.push_back(std::move(merge_entry)); } } /// TODO: Bad setting name for such purpose - if (merge_entry.type != ReplicatedMergeTreeLogEntryData::Type::EMPTY - && query_context.getSettingsRef().replication_alter_partitions_sync != 0) - waitForAllReplicasToProcessLogEntry(merge_entry); + if (query_context.getSettingsRef().replication_alter_partitions_sync != 0) + { + for (auto & merge_entry : merge_entries) + waitForAllReplicasToProcessLogEntry(merge_entry); + } return true; } diff --git a/dbms/tests/queries/0_stateless/00925_empty_replicated_merge_tree_optimize_final.reference b/dbms/tests/queries/0_stateless/00925_empty_replicated_merge_tree_optimize_final.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/dbms/tests/queries/0_stateless/00925_empty_replicated_merge_tree_optimize_final.sql b/dbms/tests/queries/0_stateless/00925_empty_replicated_merge_tree_optimize_final.sql new file mode 100644 index 00000000000..6903209c5c4 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00925_empty_replicated_merge_tree_optimize_final.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS test.replicated_optimize1; +DROP TABLE IF EXISTS test.replicated_optimize2; +CREATE TABLE test.replicated_optimize1 (d Date, k UInt64, i32 Int32) ENGINE=ReplicatedMergeTree('/clickhouse/tables/test/optimize', 'r1', d, k, 8192); +CREATE TABLE test.replicated_optimize2 (d Date, k UInt64, i32 Int32) ENGINE=ReplicatedMergeTree('/clickhouse/tables/test/optimize', 'r2', d, k, 8192); + +OPTIMIZE TABLE test.replicated_optimize1 FINAL; + +DROP TABLE test.replicated_optimize1; +DROP TABLE test.replicated_optimize2;