Repository navigation
Switch the default compression to ZSTD(3) for table data and network - #108786
Conversation
Change the built-in default compression codec from `LZ4` to `ZSTD(3)`, for both on-disk table data and client/server and server/server network communication. On-disk: `CompressionCodecFactory::getDefaultCodec` now returns `ZSTD(3)`. This is the codec applied to columns that do not specify a codec and when no matching `<compression>` config case is present. It flows through the `MergeTree` write path, the `Log` family, temporary data, and the HTTP `compress=1` output. Network: the defaults of `network_compression_method` (`LZ4` -> `ZSTD`) and `network_zstd_compression_level` (`1` -> `3`) are changed so that the native protocol uses `ZSTD(3)` by default. The changes are recorded in `SettingsChangesHistory.cpp` for the `compatibility` setting. Docs updated accordingly, including the `Native` format and protocol specifications and the server `compression` settings. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [8b9018b] Summary: ✅
AI ReviewSummaryThis PR switches the built-in default compression from Findings
Final Verdict
LLVM Coverage Report |
After switching the default compression codec from `LZ4` to `ZSTD(3)`, several stateless tests that implicitly relied on the `LZ4` default started producing different output. Pin `LZ4` explicitly where the test output depends on the codec, so the references stay stable regardless of the server default: - `04267_estimate_compression_ratio_window_accumulation` and `03363_estimate_compression_bug91415`: pass `'LZ4'` as the codec parameter of `estimateCompressionRatio`. - `03634_autopr_input_bytes_estimation_compact`: set the `MergeTree` setting `default_compression_codec='LZ4'` so `ReadCompressedBytes` matches the statistics-based estimate. - `02346_text_index_posting_list_compression_data_size_validation`: set `default_compression_codec='LZ4'` so the text index file compression (which uses the part default codec) is pinned. - `01943_log_column_sizes`: add `CODEC(LZ4)` to the `Log`/`TinyLog` columns (the `Log` family has no `MergeTree` settings). `02967_http_compressed` cannot pin the codec: the HTTP `compress=1` output uses `CompressionCodecFactory::getDefaultCodec` directly with no per-query override. Its reference is updated to the new `ZSTD(3)` output (verified to match the bytes produced by `clickhouse compressor` with `ZSTD(3)`, which are identical to the CI result). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Follow-up to the previous commit, after determining empirically how each code path selects its codec: - `03634_autopr_input_bytes_estimation_compact`: the compact-part input-bytes estimate serializes a sample with `getDefaultCodec` (now `ZSTD(3)`), so the actually-read compressed bytes must use the same codec to stay within the test's 2x tolerance. Pin `default_compression_codec='ZSTD(3)'` instead of `LZ4` (the previous commit pinned `LZ4`, which left the estimate and the reads mismatched). - `02346_text_index_posting_list_compression_data_size_validation`: the text index files are compressed with `getDefaultCodec` directly, which is not controlled by a column codec or the `default_compression_codec` setting. Drop the ineffective setting and update the reference to the `ZSTD(3)` sizes. - `03595_funcs_on_zero` (`no-fasttest`): pass `'LZ4'` to `estimateCompressionRatio`. - `00753_system_columns_and_system_tables_long` (`long`): add `CODEC(LZ4)` to the `TinyLog`/`Log` columns whose `total_bytes` depend on the default codec (`StripeLog` and `Memory` are codec-independent here; the `MergeTree` table is already pinned to `LZ4` by the test harness for `no-random-*` tests). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The msan flaky check confirmed the `TinyLog`/`Log` `CODEC(LZ4)` pins work (`total_bytes` = 27/43), but `StripeLog` reports `135` instead of `113`. Unlike `TinyLog`/`Log`, `StripeLog` compresses its combined data stream with `getDefaultCodec` (now `ZSTD(3)`) regardless of the column codec, so its size cannot be pinned via SQL — update the expected `total_bytes` to the `ZSTD(3)` value. The value is stable: it depends only on the default codec, not on the randomized query settings (`min`/`max_compress_block_size`, etc.). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
With `ZSTD(3)` as the default codec, the autopr input-bytes estimate (which serializes a few sampled blocks via `getDefaultCodec`) lands at up to ~2x the actual full-column compressed size for highly-compressible columns: `ZSTD(3)` compresses the full column better than the small sample predicts, and the sampled-block set varies with thread scheduling, so the previous 2x tolerance flaked right at the boundary (`query_2`: 808201 vs 1617909 = 2.002x). Widen the tolerance to 4x, which still catches gross misestimation. The table stays pinned to `ZSTD(3)` so the estimate and the reads use the same codec. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
estimateCompressionRatio('') uses the default codec; pass 'LZ4' explicitly so
the ratio does not depend on the server default. (no-fasttest test, skipped by
Fast test, surfaced in the full parallel stateless run.)
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`test_jbod_overflow` inserts random printable ASCII into a small (40MB) jbod disk and asserts the data overflows to the `external` disk. `randomPrintableASCII` data is entropy-light (95-symbol alphabet): `ZSTD(3)` (the new default codec) entropy-codes it ~18% smaller than `LZ4`, so the parts now fit in the jbod and no longer overflow, failing `assert ... == 'external'`. Pin the column to `CODEC(LZ4)` so the on-disk part size is independent of the server default codec. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`03634_autopr_output_bytes_estimation` compares the autopr output-bytes estimate against hardcoded `expected_bytes` with a 2.5x tolerance. The estimate serializes the output (aggregation state) with `getDefaultCodec`; under the new `ZSTD(3)` default, query_12's output compresses to ~3.6M instead of the ~11.2M seen under `LZ4`, exceeding the 2.5x bound. Update query_12's expected value to 3620000. Across three CI runs the estimate was stable (3609402/3622834/3629968), and it is the only query whose LZ4-calibrated expectation falls outside 2.5x of the ZSTD estimate; the others remain within tolerance. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
test_recompression_ttl asserts the pre-recompression part reports `default_compression_codec == 'LZ4'`. With the default codec switched to `ZSTD(3)` the initial part now reports `ZSTD(3)`, failing the assertion (same situation as the stateless `01465_ttl_recompression`). Pin `default_compression_codec = 'LZ4'` on all three tables so the initial parts stay `LZ4`; the `RECOMPRESS CODEC(ZSTD(...))` codecs are unaffected (recompression wins over the setting). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ault `test_backward_compatibility::test_aggregate_states` compares the serialized state of every aggregate function between an old and a new server. The old server defaults to `LZ4` and the new one to `ZSTD(3)`; `estimateCompressionRatio` is the only aggregate whose state encodes the argument compressed with the server's default codec, so its state (and final ratio) legitimately differs across the change and the cross-version comparison is meaningless. Skip it, matching how the test already skips functions it cannot meaningfully compare. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`04100_autopr_input_bytes_estimation_aggregation_in_order` checks the autopr input/output byte estimates. Both use `getDefaultCodec`, now `ZSTD(3)`: - Pin `default_compression_codec='ZSTD(3)'` so the read bytes match the input estimate (the `no-random-*` harness would otherwise inject `LZ4`). - The output estimate serializes the aggregation output with `getDefaultCodec`; under `ZSTD(3)` the two `max_threads=1` queries (`agg_in_order_single`, `agg_in_order_multi_agg`) compress ~5x smaller (4781834 / 6889765) than the original `LZ4`-era expectations (25519057 / 33649632), exceeding the 2x bound. Recalibrate those two expected values; the `max_threads=4` queries stay within tolerance. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The flaky check showed `04100`'s autopr output-bytes estimate swings several-fold between runs under `ZSTD(3)` (e.g. agg_in_order_single 4.78M..20.4M, multi_agg 6.89M..45.2M) because the estimate serializes the aggregation output with the default codec and is very sensitive to randomized block-sizing query settings - a sensitivity that did not surface under the previous `LZ4` default (04100 is green on master). Add `no-random-settings` (the test already used `no-random-merge-tree-settings` "to stabilize the test") so the query settings are fixed and the estimate is deterministic. The expected output values may need a follow-up recalibration to the deterministic `ZSTD(3)` values. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
With `no-random-settings` now fixing the query settings, `04100`'s autopr output estimates are deterministic under `ZSTD(3)`: agg_in_order_single=20406542 and agg_in_order_multi_agg=45288700 (identical across all flaky-check reruns). These are within 2x of the original `LZ4`-era expectations (25519057 / 33649632 — ratios 1.25x / 1.35x), so the original values hold. The earlier recalibration to 4781834 / 6889765 was based on an outlier randomized-settings run and is reverted. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The `Log`, `TinyLog`, and `StripeLog` engines write each insert with the current default codec and append it to the same compressed stream. Their columns store no per-stream codec on disk, and the codec is resolved fresh at write time from `CompressionCodecFactory::getDefaultCodec`. Switching that default from `LZ4` to `ZSTD(3)` therefore makes an upgraded table append `ZSTD(3)` blocks after the existing `LZ4` blocks in one `.bin`/`index` file. The readers constructed their `CompressedReadBuffer`/`CompressedReadBufferFromFile` with `allow_different_codecs = false`, and `CompressedReadBufferBase` throws `CANNOT_DECOMPRESS` when the method byte changes within a single stream. So a post-upgrade `SELECT` (or `StripeLog` index load / restore) over a range that spans the append boundary would fail to read existing data. Each compressed block is self-describing (its codec method byte is in the header), so a mixed-codec append-only stream is valid. Set `allow_different_codecs = true` on the Log-family read paths, exactly as the `MergeTree` reader already does. A new gtest `gtest_mixed_codec_append` writes an `LZ4` block followed by a `ZSTD(3)` block and asserts both buffer types the Log family uses read it back with the tolerance on, and still throw `CANNOT_DECOMPRESS` with it off. A SQL-level upgrade test is not possible in a single server: the Log family has no per-query or `ALTER MODIFY ... CODEC` override, so a mixed-codec file can only arise from a cross-version change of the global default codec. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`KeeperSnapshotManager` writes snapshots with a no-argument `CompressedWriteBuffer` when `compress_snapshots_with_zstd_format` is `false`. That setting is documented as "Write compressed snapshots in ZSTD format (instead of custom LZ4)", i.e. the `false` branch is the legacy custom-frame `LZ4` format. With the default codec now `ZSTD(3)`, that branch would silently start emitting `ZSTD(3)`-framed snapshots while still being presented as the legacy `LZ4` format. Old snapshots remain readable either way (the custom-frame reader auto-detects the codec from each frame's method byte), so this is not a correctness bug, but pinning `LZ4` keeps the documented legacy format literally true and independent of the server's default codec. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
After switching the default codec to `ZSTD(3)`, more tests whose output depends on the on-disk/network byte volume need the codec pinned so they do not depend on the server default: - `02047_log_family_data_file_dumps` and `02047_log_family_complex_structs_data_file_dumps`: these dump the exact bytes of the `Log`/`TinyLog`/`StripeLog` `.bin` files. Pin the columns to `CODEC(LZ4)` so the references keep validating the on-disk layout regardless of the default codec (explicit `CODEC(LZ4)` produces byte-identical output to the previous implicit `LZ4` default). - `test_replicated_fetches_bandwidth`: the throttling assertions depend on the size of the fetched parts. `randomPrintableASCII` produces random printable ASCII, which `ZSTD` entropy-codes noticeably better than `LZ4`, shrinking the parts enough to change fetch timing. Pin the `data` column to `CODEC(LZ4)` in all of these tests, matching the existing fix in `test_jbod_overflow`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The sample `<compression>` block recommended uncommenting it "for better compression", but its case used `<method>zstd</method>` with no level, i.e. `ZSTD(1)`, which is weaker than the new `ZSTD(3)` default. Enabling it would have reduced compression for matching (large) parts. Give the example an explicit `<level>` above the default so the case genuinely improves compression for large parts, and reword the surrounding comment. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI review (the Block verdict's required actions) and the two real CI failures. Pushed Log-family mixed-codec upgrade path (the blocker). Global-default consumers, settled.
Docs / user-facing. Narrowed the rollback guidance (PR body + changelog): CI reds. Left open for you, @alexey-milovidov: the performance-evidence thread on Verification: the three changed |
`Log`/`TinyLog` store each column with its own codec, so pinning the columns to `CODEC(LZ4)` keeps their on-disk byte dumps independent of the server default codec. `StripeLog`, however, writes a single shared `data.bin` (and its `index.mrk`) through `CompressionCodecFactory::getDefaultCodec`, ignoring the per-column codec, so its dumps reflect the default — which this PR changes to `ZSTD(3)`. The previous column-codec pin therefore could not stabilize the `StripeLog` sections, and `02047_log_family_data_file_dumps` / `02047_log_family_complex_structs_data_file_dumps` still differed from their references. Regenerate the `StripeLog` sections of both references under the `ZSTD(3)` default (the `Log`/`TinyLog` sections are byte-identical) and update the explanatory comment to describe the per-engine behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Three more stateless tests asserted sizes that depend on the default compression codec and broke under the `ZSTD(3)` default: - `02241_filesystem_cache_on_write_operations`: the cached on-disk segment byte totals shift (`ZSTD(3)` has slightly larger per-block overhead than `LZ4` on the tiny, highly-compressible blocks this test writes). Pin the columns to `CODEC(LZ4)` and update the three echoed `CREATE TABLE` lines in the reference. - `03546_add_distinct_to_in_clause`: compares compressed `NetworkReceiveBytes` between the with-`DISTINCT` and without-`DISTINCT` queries; under the `ZSTD(3)` network default the repetitive without-`DISTINCT` stream is entropy-coded almost as small as the tiny with-`DISTINCT` payload, so the strict comparison flaps. Pin `network_compression_method = 'LZ4'`. - `03783_autopr_dataflow_cache_reuse_qcc`: the existing `default_compression_codec = 'LZ4'` pin only covers wide parts; the compact-part byte estimation falls back to `getDefaultCodec` (now `ZSTD(3)`), and the merge-tree-settings randomizer can force compact parts. Add `min_bytes_for_wide_part = 0` so the estimate uses the on-disk `LZ4` ratio. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Switch the default compression in ClickHouse from
LZ4toZSTD(3), for both on-disk table data and network communication.Motivation
LZ4has been the default for a long time and optimizes for speed, butZSTD(3)gives substantially better compression ratios at a still-modest CPU cost, reducing both storage footprint and network traffic out of the box. ClickHouse Cloud already uses a stronger default; this aligns the self-managed defaults.What changes
CompressionCodecFactory::getDefaultCodecnow returnsZSTD(3)instead ofLZ4. ForMergeTreecolumn data the built-in default is size-aware: when a column specifies no codec and no<compression>server-config case matches,CompressionCodecSelector::chooseuses the fasterLZ4below 100 MB andZSTD(3)at or above it, so freshly inserted data starts asLZ4and the bigger parts produced by background merges switch toZSTD(3). The threshold is applied to the part size known when the codec has to be chosen, before the data is written -0for a freshly inserted part and the source parts' size for a merge or a mutation - which is the same estimate the<compression>min_part_sizeconditions have always been matched against, not the size of the resulting part. A rewrite that shrinks its input, such as aTTL DELETEor a collapsing merge, is therefore classified by the input size and can emit a part below the threshold that is stillZSTD(3). The directgetDefaultCodecusers — theLogfamily, part checksums, the HTTPcompress=1output, theStripeLogdata stream, and the persistentSet/Joinfiles — are not size-aware and switch uniformly fromLZ4toZSTD(3). External temporary (spill) files are unaffected: they keep their owntemporary_files_codecdefault (LZ4).network_compression_method(LZ4→ZSTD) andnetwork_zstd_compression_level(1→3) are changed, so the native client/server and Distributed (server/server) protocol usesZSTD(3)by default. The changes are recorded inSettingsChangesHistory.cppso thecompatibilitysetting restores the previous behavior for these paths. The distributed-query streaming exchange (StreamingExchangeSink) does not readnetwork_compression_methodand always uses the server default codec; this is safe because each frame is self-describing (the receiver auto-detects the codec) and the exchange is a transient, same-version channel —StreamingExchangeProtocolrejects peers on a different protocol version, so a stream is never read back by a node expecting a different codec.Log/TinyLog/StripeLogengines resolve the default codec at write time, so a table written before the upgrade (LZ4) and appended to after it (ZSTD(3)) ends up with mixed-codec blocks in one file. Their readers now passallow_different_codecs = true(as theMergeTreereader already does) so such files still read back correctly. The legacy custom-frame Keeper snapshot format (compress_snapshots_with_zstd_format = false) is pinned toLZ4so it stays the documented format.MultiplexedConnections,HedgedConnections,RemoteInserter) now demote the compatibility-derived network settings to unchanged, so the shard re-derives them from the serializedcompatibilitysetting instead of receiving them as explicit changes. Sending them explicitly is not an error -TCPHandlerclamps a secondary query's setting violations rather than throwing - but it subjects them to the shard's own settings constraints, which drop aCONSTpinned value and clamp a range-pinned one, so the shard would silently run under a codec other than the one the initiator'scompatibilityselects. The preamble the three senders share now lives inprepareSecondaryQuerySettingsand is pinned directly bygtest_secondary_query_settings. Fixing that surfaced a pre-existing bug in the same copies: the twoSELECTsenders set thedialect = 'clickhouse'override that lets a shard read the ClickHouse-SQL the initiator rewrote for it, but then cleared thechangedflag, soConnection::sendQuery- which serializes only changed settings - dropped it;RemoteInserterhad no such override at all. A shard whose own user or profile defaults to a foreigndialecttherefore re-parsed the rewritten query with the wrong parser. The override is now serialized on all three paths.Columns, tables, and connections that specify a codec or method explicitly are unaffected — with one exception: streams written through the built-in default codec directly, which ignore per-column codecs and the
<compression>config. TheStripeLogdata stream, the persistentSet/Joinbackup files (written bySetOrJoinSinkandStorageJoin::mutatethrough aCompressedWriteBufferwith no codec), andMergeTreeauxiliary streams such as part checksums (checksums.txt, viaMergeTreeDataPartChecksums::write) always follow the new default; see the changelog entry below for the per-path rollback.Validation
Built locally and verified against the new binary:
MergeTreetable created with no explicit codec follows the size-aware default: small parts reportLZ4and parts whose write-time size estimate reaches 100 MB reportZSTD(3)as theirdefault_compression_codecinsystem.parts. DirectgetDefaultCodecstreams (e.g.StripeLog) reportZSTD(3)regardless of size.system.settingsshowsnetwork_compression_method = ZSTDandnetwork_zstd_compression_level = 3.SELECT/INSERTover the native protocol with default (nowZSTD) network compression work; all ofLZ4/lz4hc/zstd/nonestill work and an invalid method still errors withBAD_ARGUMENTS.02995_new_settings_historyconsistency check passes (both setting changes are recorded).gtest_network_compression_settingsfor the client's own helper queries andgtest_secondary_query_settingsfor the interserver senders. The end-to-end shell test04758_distributed_compatibility_readonly_constrained_settingcannot prove the sender-side demotion on its own, because a shard clamps a secondary query's setting violations instead of rejecting the query; its comment now says so.02047_log_family_*_data_file_dumps) were regenerated for the new default:Log/TinyLogkeepLZ4-pinned columns, whileStripeLog's shareddata.bin/index.mrkreflect theZSTD(3)default (it usesgetDefaultCodecand ignores the per-column codec). Size-sensitive stateless and integration tests that assert compressed byte sizes were pinned back toLZ4(cache segment sizes, distributed-batch corruption offsets, full-disk thresholds, frozen-part checksums).The CI performance comparison does not measure this tradeoff: the harness pins the built-in default codec
to
LZ4(tests/performance/scripts/config/config.d/compression.xml) andnetwork_compression_methodtoLZ4(
tests/performance/scripts/config/users.d/perf-comparison-tweaks-users.xml) on both the reference and thetested server, deliberately, so the report measures query logic rather than drowning in expected codec
regressions. It therefore serves only as a regression filter for the non-codec parts of this change; the
storage/network-vs-CPU tradeoff of
LZ4→ZSTD(3)itself is a well-known property of the two codecs and isnot measured by this pull request's CI.
Documentation updated accordingly, including the
Nativeformat and protocol specifications and the<compression>config examples.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The default compression method is changed from
LZ4toZSTD(3). For client/server and server/server network communication it switches uniformly toZSTD(3). For on-disk table data,MergeTreecolumn data now uses a size-aware built-in default —LZ4below 100 MB andZSTD(3)at or above it, applied to the part size known at write time (the source parts' size for a merge or a mutation) rather than to the size of the resulting part — while the direct built-in-default streams (theStripeLogdata stream, the persistentSet/Joinfiles, the HTTPcompress=1framed output, and part checksums) switch uniformly fromLZ4toZSTD(3). This improves compression ratios and reduces storage and network usage out of the box, at a modest increase in CPU usage. None of the on-disk or HTTP paths are controlled bycompatibility; the previous behavior can be restored per path as follows. For client/server and Distributed network compression, use thecompatibilitysetting (or setnetwork_compression_method = 'LZ4'). ForMergeTreecolumn data, set a column/tableCODEC(LZ4)or configure the server<compression>default toLZ4; the same part default also covers theMergeTreeauxiliary files that follow it rather than the per-column codec, such as text-index files. For theLog/TinyLogengines, set a columnCODEC(LZ4)(their default does not consult the<compression>config). TheStripeLogdata stream, the persistentSet/Joinbackup files, the HTTPcompress=1framed output, andMergeTreeauxiliary streams such as part checksums (checksums.txt) use the built-in default codec directly — they ignore per-column codecs and the<compression>config — so they always use the newZSTD(3)default and have no runtime rollback; reading stays correct because every compressed frame is self-describing.Workflow [PR]
Sync PR [sync-upstream/pr/108786]
Version info
26.9.1.810(included in26.9and later)