Trace:
```
2022.04.18 07:16:59.019423 [ 1737 ] {576c4a07-1c17-4bc5-b992-d5bf44cbb774} <Debug> executeQuery: (from [::1]:43406) (comment: 01451_replicated_detach_drop_part_long.sql) ALTER TABLE replica2 ATTACH PART 'all_1_1_0';
2022.04.18 07:16:59.024434 [ 1737 ] {576c4a07-1c17-4bc5-b992-d5bf44cbb774} <Trace> ContextAccess (default): Access granted: INSERT ON test_3.replica2
2022.04.18 07:16:59.028259 [ 1737 ] {576c4a07-1c17-4bc5-b992-d5bf44cbb774} <Debug> test_3.replica2 (6787bb64-2236-4d15-9695-35a027d2ecce): Checking parts
2022.04.18 07:16:59.029644 [ 1737 ] {576c4a07-1c17-4bc5-b992-d5bf44cbb774} <Debug> test_3.replica2 (6787bb64-2236-4d15-9695-35a027d2ecce): Checking part attaching_all_1_1_0
2022.04.18 07:17:07.267287 [ 1737 ] {576c4a07-1c17-4bc5-b992-d5bf44cbb774} <Error> test_3.replica2 (6787bb64-2236-4d15-9695-35a027d2ecce): File store/678/6787bb64-2236-4d15-9695-35a027d2ecce/detached/attaching_all_1_1_0/txn_version.txt contains:
2022.04.18 07:17:07.267287 [ 1737 ] {576c4a07-1c17-4bc5-b992-d5bf44cbb774} <Error> test_3.replica2 (6787bb64-2236-4d15-9695-35a027d2ecce): File store/678/6787bb64-2236-4d15-9695-35a027d2ecce/detached/attaching_all_1_1_0/txn_version.txt contains:

expected:
version: 1
creation_tid: (0, 0, 00000000-0000-0000-0000-000000000000): std::exception. Code: 1001, type: std::__1::__fs::filesystem::filesystem_error, e.what() = filesystem error: in file_size: No such file or directory ["/var/lib/clickhouse/store/678/6787bb64-2236-4d15-9695-35a027d2ecce/detached/attaching_all_1_1_0/txn_version.txt"], Stack trace (when copying this message, always include the lines below):

0. /build/build_docker/../contrib/libcxx/include/exception:133: std::exception::capture() @ 0x17b4ffc2 in /usr/bin/clickhouse
1. /build/build_docker/../contrib/libcxx/include/exception:111: std::exception::exception() @ 0x17b4ff93 in /usr/bin/clickhouse
2. /build/build_docker/../contrib/libcxx/src/support/runtime/stdexcept_default.ipp:33: std::runtime_error::runtime_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x3004f1bd in /usr/bin/clickhouse
3. /build/build_docker/../contrib/libcxx/src/system_error.cpp:244: std::__1::system_error::system_error(std::__1::error_code, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x300601ba in /usr/bin/clickhouse
4. /build/build_docker/../contrib/libcxx/include/__filesystem/filesystem_error.h:37: std::__1::__fs::filesystem::filesystem_error::filesystem_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__fs::filesystem::path const&, std::__1::error_code) @ 0x2587ba21 in /usr/bin/clickhouse
5. /build/build_docker/../contrib/libcxx/include/__filesystem/filesystem_error.h:86: void std::__1::__fs::filesystem::__throw_filesystem_error<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::__fs::filesystem::path const&, std::__1::error_code const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::__fs::filesystem::path const&, std::__1::error_code const&) @ 0x2ffb1b28 in /usr/bin/clickhouse
6. /build/build_docker/../contrib/libcxx/src/filesystem/filesystem_common.h:177: std::__1::__fs::filesystem::detail::(anonymous namespace)::ErrorHandler<unsigned long>::report(std::__1::error_code const&) const @ 0x2ffbab3b in /usr/bin/clickhouse
7. /build/build_docker/../contrib/libcxx/src/filesystem/operations.cpp:1156: std::__1::__fs::filesystem::__file_size(std::__1::__fs::filesystem::path const&, std::__1::error_code*) @ 0x2ffba918 in /usr/bin/clickhouse
8. /build/build_docker/../contrib/libcxx/include/__filesystem/operations.h:104: std::__1::__fs::filesystem::file_size(std::__1::__fs::filesystem::path const&) @ 0x17c51259 in /usr/bin/clickhouse
9. /build/build_docker/../src/Disks/DiskLocal.cpp:294: DB::DiskLocal::getFileSize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x261982fa in /usr/bin/clickhouse
10. /build/build_docker/../src/Storages/MergeTree/IMergeTreeDataPart.cpp:71: DB::openForReading(std::__1::shared_ptr<DB::IDisk> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x27aa63ec in /usr/bin/clickhouse
11. /build/build_docker/../src/Storages/MergeTree/IMergeTreeDataPart.cpp:1427: DB::IMergeTreeDataPart::assertHasValidVersionMetadata() const @ 0x27a93347 in /usr/bin/clickhouse
12. /build/build_docker/../src/Storages/MergeTree/IMergeTreeDataPart.cpp:476: DB::IMergeTreeDataPart::removeIfNeeded() @ 0x27a92165 in /usr/bin/clickhouse
13. /build/build_docker/../src/Storages/MergeTree/MergeTreeDataPartCompact.cpp:192: DB::MergeTreeDataPartCompact::~MergeTreeDataPartCompact() @ 0x27bfe9e0 in /usr/bin/clickhouse
14. /build/build_docker/../contrib/libcxx/include/__memory/construct_at.h:57: void std::__1::__destroy_at<DB::MergeTreeDataPartCompact, 0>(DB::MergeTreeDataPartCompact*) @ 0x27bcda28 in /usr/bin/clickhouse
15. /build/build_docker/../contrib/libcxx/include/__memory/construct_at.h:82: void std::__1::destroy_at<DB::MergeTreeDataPartCompact, 0>(DB::MergeTreeDataPartCompact*) @ 0x27bcd9b5 in /usr/bin/clickhouse
16. /build/build_docker/../contrib/libcxx/include/__memory/allocator_traits.h:321: void std::__1::allocator_traits<std::__1::allocator<DB::MergeTreeDataPartCompact> >::destroy<DB::MergeTreeDataPartCompact, void, void>(std::__1::allocator<DB::MergeTreeDataPartCompact>&, DB::MergeTreeDataPartCompact*) @ 0x27bcd999 in /usr/bin/clickhouse
17. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:310: std::__1::__shared_ptr_emplace<DB::MergeTreeDataPartCompact, std::__1::allocator<DB::MergeTreeDataPartCompact> >::__on_zero_shared() @ 0x27bcd6ee in /usr/bin/clickhouse
18. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:175: std::__1::__shared_count::__release_shared() @ 0x17b1fa51 in /usr/bin/clickhouse
19. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:216: std::__1::__shared_weak_count::__release_shared() @ 0x17b1f9f9 in /usr/bin/clickhouse
20. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:704: std::__1::shared_ptr<DB::IMergeTreeDataPart>::~shared_ptr() @ 0x2700afec in /usr/bin/clickhouse
21. /build/build_docker/../contrib/libcxx/include/__memory/construct_at.h:57: void std::__1::__destroy_at<std::__1::shared_ptr<DB::IMergeTreeDataPart>, 0>(std::__1::shared_ptr<DB::IMergeTreeDataPart>*) @ 0x2700bd87 in /usr/bin/clickhouse
22. /build/build_docker/../contrib/libcxx/include/__memory/construct_at.h:82: void std::__1::destroy_at<std::__1::shared_ptr<DB::IMergeTreeDataPart>, 0>(std::__1::shared_ptr<DB::IMergeTreeDataPart>*) @ 0x2700bd15 in /usr/bin/clickhouse
23. /build/build_docker/../contrib/libcxx/include/__memory/allocator_traits.h:321: void std::__1::allocator_traits<std::__1::allocator<std::__1::shared_ptr<DB::IMergeTreeDataPart> > >::destroy<std::__1::shared_ptr<DB::IMergeTreeDataPart>, void, void>(std::__1::allocator<std::__1::shared_ptr<DB::IMergeTreeDataPart> >&, std::__1::shared_ptr<DB::IMergeTreeDataPart>*) @ 0x2700bcf9 in /usr/bin/clickhouse
24. /build/build_docker/../contrib/libcxx/include/vector:803: std::__1::vector<std::__1::shared_ptr<DB::IMergeTreeDataPart>, std::__1::allocator<std::__1::shared_ptr<DB::IMergeTreeDataPart> > >::__base_destruct_at_end(std::__1::shared_ptr<DB::IMergeTreeDataPart>*) @ 0x2700bcb8 in /usr/bin/clickhouse
25. /build/build_docker/../contrib/libcxx/include/vector:797: std::__1::vector<std::__1::shared_ptr<DB::IMergeTreeDataPart>, std::__1::allocator<std::__1::shared_ptr<DB::IMergeTreeDataPart> > >::__clear() @ 0x2700bbf8 in /usr/bin/clickhouse
26. /build/build_docker/../contrib/libcxx/include/vector:401: std::__1::vector<std::__1::shared_ptr<DB::IMergeTreeDataPart>, std::__1::allocator<std::__1::shared_ptr<DB::IMergeTreeDataPart> > >::~vector() @ 0x2700af70 in /usr/bin/clickhouse
27. /build/build_docker/../src/Storages/StorageReplicatedMergeTree.cpp:5125: DB::StorageReplicatedMergeTree::attachPartition(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, bool, std::__1::shared_ptr<DB::Context const>) @ 0x277b79ff in /usr/bin/clickhouse
28. /build/build_docker/../src/Storages/MergeTree/MergeTreeData.cpp:3765: DB::MergeTreeData::alterPartition(std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::__1::vector<DB::PartitionCommand, std::__1::allocator<DB::PartitionCommand> > const&, std::__1::shared_ptr<DB::Context const>) @ 0x27b79888 in /usr/bin/clickhouse
29. /build/build_docker/../src/Interpreters/InterpreterAlterQuery.cpp:150: DB::InterpreterAlterQuery::executeToTable(DB::ASTAlterQuery const&) @ 0x26b3d0e6 in /usr/bin/clickhouse
30. /build/build_docker/../src/Interpreters/InterpreterAlterQuery.cpp:58: DB::InterpreterAlterQuery::execute() @ 0x26b3afaa in /usr/bin/clickhouse
31. /build/build_docker/../src/Interpreters/executeQuery.cpp:703: DB::executeQueryImpl(char const*, char const*, std::__1::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum, DB::ReadBuffer*) @ 0x271658a2 in /usr/bin/clickhouse

Cannot print extra info for Poco::Exception (version 22.4.1.2066)

2022.04.18 07:29:58.284574 [ 17210 ] {} <Fatal> BaseDaemon: ########################################
2022.04.18 07:29:58.758557 [ 17210 ] {} <Fatal> BaseDaemon: (version 22.4.1.2066, build id: 17F13612BBEE1BC3) (from thread 1737) (query_id: 576c4a07-1c17-4bc5-b992-d5bf44cbb774) (query: ALTER TABLE replica2 ATTACH PART 'all_1_1_0';) Received signal Aborted (6)
2022.04.18 07:29:58.814579 [ 17210 ] {} <Fatal> BaseDaemon: 
2022.04.18 07:29:59.117024 [ 17210 ] {} <Fatal> BaseDaemon: Stack trace: 0x7ff5f623a03b 0x7ff5f6219859 0x7ff5f6219729 0x7ff5f622b006 0x27a9219f 0x27bfe9e0 0x27bcda28 0x27bcd9b5 0x27bcd999 0x27bcd6ee 0x17b1fa51 0x17b1f9f9 0x2700afec 0x2700bd87 0x2700bd15 0x2700bcf9 0x2700bcb8 0x2700bbf8 0x2700af70 0x277b79ff 0x27b79888 0x26b3d0e6 0x26b3afaa 0x271658a2 0x271628e4 0x280a09eb 0x280aefe5 0x2dfe9c19 0x2dfea428 0x2e23e634 0x2e23b13a 0x2e239f1c 0x7ff5f63f1609 0x7ff5f6316163
2022.04.18 07:29:59.177030 [ 17210 ] {} <Fatal> BaseDaemon: 4. raise @ 0x7ff5f623a03b in ?
2022.04.18 07:29:59.196693 [ 17210 ] {} <Fatal> BaseDaemon: 5. abort @ 0x7ff5f6219859 in ?
2022.04.18 07:29:59.203716 [ 17210 ] {} <Fatal> BaseDaemon: 6. ? @ 0x7ff5f6219729 in ?
2022.04.18 07:29:59.208949 [ 17210 ] {} <Fatal> BaseDaemon: 7. ? @ 0x7ff5f622b006 in ?
2022.04.18 07:30:00.137244 [ 17210 ] {} <Fatal> BaseDaemon: 8. /build/build_docker/../src/Storages/MergeTree/IMergeTreeDataPart.cpp:0: DB::IMergeTreeDataPart::removeIfNeeded() @ 0x27a9219f in /usr/bin/clickhouse
2022.04.18 07:30:00.668490 [ 17210 ] {} <Fatal> BaseDaemon: 9. /build/build_docker/../src/Storages/MergeTree/MergeTreeDataPartCompact.cpp:192: DB::MergeTreeDataPartCompact::~MergeTreeDataPartCompact() @ 0x27bfe9e0 in /usr/bin/clickhouse
2022.04.18 07:30:04.718399 [ 17210 ] {} <Fatal> BaseDaemon: 10. /build/build_docker/../contrib/libcxx/include/__memory/construct_at.h:57: void std::__1::__destroy_at<DB::MergeTreeDataPartCompact, 0>(DB::MergeTreeDataPartCompact*) @ 0x27bcda28 in /usr/bin/clickhouse
2022.04.18 07:30:08.023312 [ 17210 ] {} <Fatal> BaseDaemon: 11. /build/build_docker/../contrib/libcxx/include/__memory/construct_at.h:82: void std::__1::destroy_at<DB::MergeTreeDataPartCompact, 0>(DB::MergeTreeDataPartCompact*) @ 0x27bcd9b5 in /usr/bin/clickhouse
2022.04.18 07:30:11.954548 [ 17210 ] {} <Fatal> BaseDaemon: 12. /build/build_docker/../contrib/libcxx/include/__memory/allocator_traits.h:321: void std::__1::allocator_traits<std::__1::allocator<DB::MergeTreeDataPartCompact> >::destroy<DB::MergeTreeDataPartCompact, void, void>(std::__1::allocator<DB::MergeTreeDataPartCompact>&, DB::MergeTreeDataPartCompact*) @ 0x27bcd999 in /usr/bin/clickhouse
2022.04.18 07:30:17.500877 [ 17210 ] {} <Fatal> BaseDaemon: 13. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:310: std::__1::__shared_ptr_emplace<DB::MergeTreeDataPartCompact, std::__1::allocator<DB::MergeTreeDataPartCompact> >::__on_zero_shared() @ 0x27bcd6ee in /usr/bin/clickhouse
2022.04.18 07:30:17.568942 [ 17210 ] {} <Fatal> BaseDaemon: 14. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:175: std::__1::__shared_count::__release_shared() @ 0x17b1fa51 in /usr/bin/clickhouse
2022.04.18 07:30:17.655994 [ 17210 ] {} <Fatal> BaseDaemon: 15. /build/build_docker/../contrib/libcxx/include/__memory/shared_ptr.h:216: std::__1::__shared_weak_count::__release_shared() @ 0x17b1f9f9 in /usr/bin/clickhouse
2022.04.18 07:30:18.418624 [ 650 ] {} <Fatal> Application: Child process was terminated by signal 6.
```

{
    String res;
    /** If you need to detach a part, and directory into which we want to rename it already exists,
        *  we will rename to the directory with the name to which the suffix is added in the form of "_tryN".
        * This is done only in the case of `to_detached`, because it is assumed that in this case the exact name does not matter.
        * No more than 10 attempts are made so that there are not too many junk directories left.
        */
    auto full_relative_path = fs::path(storage.relative_data_path);
    if (detached)
        full_relative_path /= "detached";
    if (detached && parent_part)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot detach projection");
    else if (parent_part)
        full_relative_path /= parent_part->relative_path;
    for (int try_no = 0; try_no < 10; ++try_no)
    {
        res = (prefix.empty() ? "" : prefix + "_") + name + (try_no ? "_try" + DB::toString(try_no) : "");
        if (!volume->getDisk()->exists(full_relative_path / res))
            return res;
        LOG_WARNING(storage.log, "Directory {} (to detach to) already exists. Will detach to directory with '_tryN' suffix.", res);
    }
    return res;
}
String IMergeTreeDataPart::getRelativePathForDetachedPart(const String & prefix) const