Skip to content

ReplicatedMergeTree: CHECK TABLE fails after fetching part with unknown projection (re-fetch loop) #100413

Open

Description

@zlareb1

Describe what's wrong

PR #99623 fixed parts with unknown projections being marked as lost, but the fix is incomplete on the receiver side (replica that fetches the part). After a successful fetch, a subsequent CHECK TABLE or the background part-check thread calls checkDataPart and fails with NO_FILE_IN_DATA_PART, marking the part as broken and triggering an infinite re-fetch loop.

Root cause:

downloadPartToDisk (fixed by #99623) patches data_checksums in memory so the immediate checkEqual passes — but checksums.txt is still written to disk verbatim from the sender, including the pp.proj entry that was never transferred (because the projection is unknown, getProjectionParts() returns 0 for it so send_projections loop skips it).

When checkDataPart later runs on replica 2:

  • projections_on_disk is built by iterating the part directory for .proj subdirectories — pp.proj does not exist on disk, so projections_on_disk stays empty for it.
  • getProjectionParts() does not include pp (unknown projection).
  • The new guard added by redo Part with unknown projections should not be marked as lost forever #99623 — if (!projections_on_disk.empty()) — does not fire.
  • checksums_txt (loaded from disk) still has pp.proj.
  • checksums_data does not have pp.proj (directory never existed on disk).
  • checksums_txt.checkEqual(checksums_data) throws NO_FILE_IN_DATA_PART.

The part is marked broken → re-fetched from replica 1 → same outcome → infinite loop.

The fix in checkDataPart.cpp only handles the local-attach case (.proj directory exists on disk but is unknown to the table, i.e. projections_on_disk is non-empty). It does not handle the fetch case (checksums.txt references a .proj entry whose directory was never transferred).

Does it reproduce on the most recent release?

Yes — introduced/unblocked by #99623 (merged 2026-03-21).

How to reproduce

Requires ReplicatedMergeTree with 2 replicas and a ZooKeeper/Keeper.

-- On both replicas:
CREATE TABLE t (x Int32, y Int32, PROJECTION p (SELECT x, y ORDER BY x))
ENGINE = ReplicatedMergeTree('/clickhouse/tables/test/t', '{replica}')
PARTITION BY intDiv(y, 100) ORDER BY y
SETTINGS max_parts_to_merge_at_once = 1;

-- On replica 1:
INSERT INTO t SELECT number, number FROM numbers(7);
ALTER TABLE t ADD PROJECTION pp (SELECT x, count() GROUP BY x);
ALTER TABLE t MATERIALIZE PROJECTION pp SETTINGS mutations_sync=2;
ALTER TABLE t DETACH PARTITION 0;

-- On replica 2: remove the detached copy so it is forced to re-fetch later
ALTER TABLE t DROP DETACHED PARTITION 0 SETTINGS allow_drop_detached=1;

-- On replica 1: drop projection while part is detached, then re-attach
ALTER TABLE t DROP PROJECTION pp;
ALTER TABLE t ATTACH PARTITION 0;

-- On replica 2: sync, then run CHECK TABLE
SYSTEM SYNC REPLICA t;
SELECT count() FROM t;   -- returns 7 (data is fine)
CHECK TABLE t;           -- fails: NO_FILE_IN_DATA_PART for pp.proj

The SELECT succeeds because data is intact. CHECK TABLE fails because checksums.txt references pp.proj but the directory was never transferred. The background ReplicatedMergeTreePartCheckThread will then mark the part as broken and re-fetch it from replica 1, creating a loop.

The same state can be reproduced without replication — manually remove a .proj directory from a detached part whose checksums.txt still references it, then attach and run CHECK TABLE:

# After the detach step above:
rm -rf /var/lib/clickhouse/data/default/t/detached/<part_name>/pp.proj
ALTER TABLE t ATTACH PARTITION 0;
SELECT count() FROM t;   -- 7, works
CHECK TABLE t;           -- Code: 226. NO_FILE_IN_DATA_PART: No file pp.proj in data part

Suggested fix

In checkDataPart.cpp, after the existing new block (which handles projections whose directories exist on disk), also remove from checksums_txt any .proj entries that are referenced in checksums_txt but absent from checksums_data (i.e., whose directory was never on disk):

/// Also handle the case where checksums_txt references a .proj entry whose
/// directory is absent from disk entirely (e.g. receiver after an inter-replica
/// fetch of a part with an unknown projection — checksums.txt was transferred
/// but the .proj directory was not).
for (auto it = checksums_txt.files.begin(); it != checksums_txt.files.end(); )
{
    if (it->first.ends_with(".proj") && !checksums_data.has(it->first))
    {
        is_broken_projection = true;
        it = checksums_txt.files.erase(it);
    }
    else
        ++it;
}

The test 03822_attach_with_unknown_projection.sh should also add CHECK TABLE t_unknown_proj_2 after SYSTEM SYNC REPLICA t_unknown_proj_2 to cover this path.

Related

Activity

  1. added
    potential bug
    on Mar 23, 2026
  2. added
    bug
    and removed
    potential bug
    on Mar 23, 2026
  3. CheSema commented on Mar 23, 2026

    @CheSema
    Member

    It looks like you are ready to bring PR with the test and the fix, is that right?

  4. zlareb1 commented on Mar 23, 2026

    @zlareb1
    MemberAuthor

    sure, I will make the fix.

  5. assigned and unassigned on Mar 23, 2026
  6. zlareb1 commented on May 5, 2026

    @zlareb1
    MemberAuthor

    @groeneai please fix this in a separate PR.

  7. groeneai commented on May 5, 2026

    @groeneai
    Collaborator

    @zlareb1 — fix PR up: #104098.

    Root cause: when a replica fetches a part with an unknown projection, the sender's getProjectionParts excludes it so the .proj/ subdirectory is not transferred, but checksums.txt is copied verbatim and still references it. The receiver's checkDataPart then throws NO_FILE_IN_DATA_PART from checkEqual, the part is marked broken, and the cycle repeats indefinitely.

    Fix: strip orphan .proj entries from checksums_txt before checkEqual when the corresponding directory is not on disk — mirrors the existing PR #99623 block that handles the symmetric local-attach variant.

    Includes a new regression test (04203_100413_check_table_unknown_projection_after_fetch.sh) that exercises the fetch path and verifies CHECK TABLE succeeds on the receiving replica. Verified 20/20 passes with random settings; pre-fix the test fails deterministically with Part 0_5_5_0 looks broken. Removing it and will try to fetch..

    Reviewers pinged: @CheSema (original #99623 author) and @KochetovNicolai (comp-projections).

  8. removed their assignment
    on May 12, 2026
  9. added
    comp-projections
    comp-replication
    on Aug 12, 2026
  10. zlareb1 commented on Aug 12, 2026

    @zlareb1
    MemberAuthor

    @groeneai should this still be open? or we can close this?

  11. groeneai commented on Aug 12, 2026

    @groeneai
    Collaborator

    Fixed on master, but not in any active release branch, so I would hold off closing it.

    The fix landed inside #108118 (merged 2026-07-22), commit 04235cb by @ Algunenano: checkDataPart now also drops leftover checksums.txt entries for .proj directories that are unknown to the metadata and absent on disk, before the base-part checkEqual. My #104098 is closed as superseded by it, and #108118's version is the better one since it keeps genuine base-file corruption fatal on fetch.

    I checked by running it rather than by reading the code, using the non-replicated variant from the issue (drop pp.proj from a detached part, drop the projection, re-attach, CHECK TABLE):

    • master: CHECK TABLE passes, data intact, OPTIMIZE FINAL succeeds.
    • 26.7.4.21 official build, same state: 0_5_5_0 0 Code: 226. DB::Exception: No file pp.proj in data part. (NO_FILE_IN_DATA_PART)

    The part is genuinely in the reported state in both runs: after ATTACH its checksums.txt still lists pp.proj with no such directory on disk, so checksums.txt is not being regenerated underneath the check.

    The new block is absent from checkDataPart.cpp on all five active release branches. 26.7, 26.6 and 26.5 carry only the earlier #99623 in-memory patch, which is the incomplete state this issue was filed about; 26.3 and 25.8 predate #99623 entirely.

    Two things worth knowing before you decide:

    1. 03822_attach_with_unknown_projection.sh does not cover this case, despite its comment claiming the fetch path. It runs CHECK TABLE only on the sending replica; on the fetching replica it asserts SELECT count() and the sums, and the point of this issue is that the SELECT succeeds while CHECK TABLE fails. CIDB agrees: over the last 30 days it is 98 OK and 0 FAIL on 26.7, 122 OK on 26.6, 113 OK on 26.5, green on every branch where the bug still reproduces.
    2. Nothing pins the fixed behaviour on master either, since my test went away with Fix CHECK TABLE re-fetch loop on parts with unknown-projection orphan checksum entries #104098.

    I can send a test-only PR adding a CHECK TABLE assertion on the receiving replica in 03822, which would both pin master and fail honestly on the release branches, if you or @ CheSema want that. The backport is a separate call: the useful hunk is about 20 lines of checkDataPart.cpp, so labelling #108118 as a whole is probably not what you want. Closing is your call, not mine.

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

Metadata

Metadata

Assignees

Labels

bugcomp-projectionscomp-replication

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions