Skip to content

Stream-parse the .backup metadata file - #125712

Merged
jkartseva merged 1 commit into
masterfrom
jkartseva/backup-metadata-stream-parse
Oct 14, 2026
Merged

jkartseva merged 1 commit into
masterfrom
jkartseva/backup-metadata-stream-parse

Conversation

@jkartseva

@jkartseva jkartseva commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Related: #109107
Related:https://github.com/ClickHouse/clickhouse-private/issues/62693

BackupImpl::readBackupMetadata read the whole .backup file into a String and ran the SAX parser over it. The file has a record per file of the backup, so for a backup of millions of files the string takes gigabytes. #109107 replaced the DOM tree with a SAX parser but kept this string.

The parser is now fed from the read buffer through StdIStreamFromReadBuffer, so the metadata is parsed in chunks while it is read, and the peak memory of opening a backup (RESTORE, the base of an incremental BACKUP) no longer includes the size of its metadata file. The parser sees the same chunks as before (Poco parses memory in 4 KiB chunks too), so the result is the same.

std::istream turns an exception from the buffer into badbit, which the parser takes for the end of the data. The stream rethrows it instead, so a read error (e.g. from S3) is reported as itself, not as a malformed manifest.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Reduced the peak memory usage of RESTORE and of incremental BACKUP: the metadata file of a backup is now parsed while it is read, instead of being read into memory whole first. For backups of millions of files this saves gigabytes.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125712]

Version info

  • Merged into: 26.10.1.2412-master (included in 26.10 and later)
  • Backported to: 26.9.20.13, 26.8.28.12

… one string

`BackupImpl::readBackupMetadata` read the whole `.backup` file into a `String` and then ran the SAX
parser over it. The file has a record per file of the backup, so for a backup of millions of files the
string takes gigabytes, on top of the maps built from it. Feed the parser from the read buffer through
`StdIStreamFromReadBuffer` instead, so the metadata is parsed in chunks while it is read.

`std::istream` turns an exception from the buffer into `badbit`, which the parser takes for the end of
the data, so the stream rethrows it instead: a read error isn't reported as a malformed manifest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6ee4932]

Summary: ✅


AI Review

Summary

PR #125712 streams .backup metadata into the SAX parser instead of first materializing the whole file, and uses the consumed byte count for size accounting. At commit 6ee493217b6c43b95c7de9dc44c27f6287bacb34, no concrete correctness defect was identified, but the central peak-memory claim needs direct evidence before approval.

Missing context / blind spots
  • ⚠️ S3 and archive backup validation has not completed in the available CI results. Successful applicable backup/restore jobs would close this gap; no local build or tests were run.
Findings

⚠️ Majors

  • src/Backups/BackupImpl.cpp:937: The promised reduction in peak memory has no before/after measurement or focused regression. The existing round-trip test verifies restored contents and the handler tests exercise parseMemoryNP; both pass with the previous whole-file buffering. Measurements in the related DOM-removal PR do not validate this allocation change. Provide peak-memory measurements for opening the same large backup before and after, including manifest size and file count, or a regression that fails when whole-file buffering returns. Exclude fixture creation from the measured phase.
Final Verdict

⚠️ Request changes — provide focused evidence for the peak-memory reduction. The existing full/incremental round-trip and XML escaping tests passed in Fast test, but do not establish this memory property.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-backup Backup/restore infrastructure and backup formats. labels Oct 14, 2026
@jkartseva
jkartseva requested a review from kolsha October 14, 2026 01:16
/// a record per file and can take gigabytes for a backup of many files.
ReadBuffer & metadata_in = *in;
/// The parser reads the stream from start to end and never seeks, so the stream doesn't need the size.
StdIStreamFromReadBuffer metadata_stream(std::move(in), /*size=*/ 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Major: verify the promised peak-memory reduction.

The main contract of this change is that opening a large .backup no longer needs the whole manifest in memory, but there is no measurement or regression test establishing that property. The existing 04494_backup_metadata_round_trip checks restored data, and the handler tests still call parseMemoryNP; both also pass with the previous whole-file buffering implementation. The measurements in the related PR concern DOM removal, which is a different allocation.

Please provide before/after peak-memory measurements for opening the same large backup, or a focused regression that fails if whole-file buffering returns. Include the manifest size and file count, and measure opening the backup separately from creating the fixture, so the result establishes the claimed saving on this production path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Measured the peak memory of opening a large backup, before and after this change.

Fixture: a MergeTree table with 101 columns, 2000 wide parts (PARTITION BY k, min_bytes_for_wide_part = 0), backed up with BACKUP TABLE t TO File(...). The backup has 824,003 files, and its .backup is 140,072,415 bytes.

Opening it, separately from creating the fixture: RESTORE TABLE t FROM File(...) SETTINGS structure_only = 1 in a fresh clickhouse local, which reads the whole manifest and restores only the definition. Peak memory of the query from --memory-usage (MemoryTrackerPeakUsage), three runs each:

Peak memory
Before (readStringUntilEOF + parseMemoryNP) 402,882,375 bytes, in all three runs
After (parsing from the read buffer) 114,051,151 - 114,051,167 bytes

The difference, 288.8 MB, is about twice the manifest: the string grows by doubling while it is read. What remains is the file_names and file_infos maps built from the manifest, which this change doesn't touch.

The parse result is unchanged: 04494_backup_metadata_round_trip restores a full and an incremental backup and compares the data, and it passes with this change, along with the integration tests that read manifests (test_backups_from_disk, the incremental and archive tests of test_backup_restore_new).

I didn't add a memory regression test: a fixture large enough for the difference to stand out of the rest of the query's memory takes about a minute to create, and the maps grow with the same number of files, so a threshold on a small fixture would be fragile.

@jkartseva
jkartseva added this pull request to the merge queue Oct 14, 2026
Merged via the queue into master with commit 3f605c4 Oct 14, 2026
176 checks passed
@jkartseva
jkartseva deleted the jkartseva/backup-metadata-stream-parse branch October 14, 2026 21:30
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 14, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125712 to 26.9: Stream-parse the `.backup` metadata file
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125712 to 26.8: Stream-parse the `.backup` metadata file
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-backup Backup/restore infrastructure and backup formats. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants