Skip to content

Hold StoragePtr in StorageFromMergeTreeDataPart for clarity#95123

Closed
alexey-milovidov wants to merge 3 commits intomasterfrom
fix-storage-from-part-lifetime
Closed

Hold StoragePtr in StorageFromMergeTreeDataPart for clarity#95123
alexey-milovidov wants to merge 3 commits intomasterfrom
fix-storage-from-part-lifetime

Conversation

@alexey-milovidov
Copy link
Member

StorageFromMergeTreeDataPart holds parts that reference the original MergeTreeData storage. Previously it only held a raw reference to the storage, which could theoretically become dangling if the storage were destroyed while this wrapper still exists.

In practice, this is a no-op change because MutationsInterpreter (the only place where StorageFromMergeTreeDataPart is created) cannot outlive the storage. However, holding the StoragePtr explicitly makes the lifetime dependency clear and follows the same pattern as StorageFromMergeTreeProjection.

Also removed an unused constructor and its associated analysis_result_ptr member.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

`StorageFromMergeTreeDataPart` holds parts that reference the original
`MergeTreeData` storage. Previously it only held a raw reference to the
storage, which could theoretically become dangling if the storage were
destroyed while this wrapper still exists.

In practice, this is a no-op change because `MutationsInterpreter` (the
only place where `StorageFromMergeTreeDataPart` is created) cannot
outlive the storage. However, holding the `StoragePtr` explicitly makes
the lifetime dependency clear and follows the same pattern as
`StorageFromMergeTreeProjection`.

Also removed an unused constructor and its associated `analysis_result_ptr`
member.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@clickhouse-gh
Copy link
Contributor

clickhouse-gh bot commented Jan 26, 2026

Workflow [PR], commit [4d1ed99]

Summary:

job_name test_name status info comment
Fast test failure
03276_index_empty_part FAIL cidb
02377_modify_column_from_nested FAIL cidb
01504_compression_multiple_streams FAIL cidb
01172_transaction_counters FAIL cidb
00980_merge_alter_settings FAIL cidb
03774_wide_part_mutation_sparse_ratio_setting_bug FAIL cidb
00804_test_alter_compression_codecs FAIL cidb
02319_lightweight_delete_on_merge_tree FAIL cidb
03533_clickhouse_local_database_argument FAIL cidb
03128_merge_tree_index_lazy_load FAIL cidb
85 more test cases not shown
Build (amd_debug) dropped
Build (amd_asan) dropped
Build (amd_tsan) dropped
Build (amd_msan) dropped
Build (amd_ubsan) dropped
Build (amd_binary) dropped
Build (arm_asan) dropped
Build (arm_binary) dropped
Build (arm_tsan) dropped

@clickhouse-gh clickhouse-gh bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jan 26, 2026
alexey-milovidov and others added 2 commits January 26, 2026 03:40
In rare cases, the query pipeline can hold references to data parts
after the storage has been dropped. When the part destructor runs,
it tries to access `storage.decrefColumnsDescriptionForColumns` and
`storage.total_outdated_parts_count`, which causes a segmentation
fault if the storage has already been destroyed.

This change adds a `storage_weak` member that stores a weak pointer
to the storage. In the destructor, we lock this weak pointer to check
if the storage is still alive before accessing it.

The existing `const MergeTreeData & storage` reference is kept for
backward compatibility since ~30 files access `part->storage.`
directly. The weak pointer is only used in the destructor path
where we need to safely handle the case of expired storage.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
}

void IMergeTreeDataPart::decrementStateMetric(MergeTreeDataPartState state_) const
void IMergeTreeDataPart::decrementStateMetric(MergeTreeDataPartState state_, bool storage_alive) const
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WTF?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant