The "DB::Exception: Cannot compress data" error in ClickHouse indicates that a compression operation failed during data writing. The CANNOT_COMPRESS error can surface during inserts, merges, or any operation that needs to write compressed blocks to disk. While relatively uncommon, it points to a problem with the compression library, the data being compressed, or the resources available for the compression operation.
Impact
When this error occurs, the write operation that triggered it will fail. This means inserts may be rejected and background merges can stall. If merges cannot complete, part counts will grow, eventually degrading query performance and potentially triggering "too many parts" errors. Data that was being written at the time of the failure will not be persisted.
Common Causes
- Insufficient memory available for the compression buffer, especially with high compression levels
- A bug or incompatibility in the compression library linked with ClickHouse
- Extremely large block sizes that exceed the compressor's internal limits
- Corrupted or misconfigured codec settings on a column
- Disk space exhaustion causing write failures that manifest as compression errors
- Hardware issues such as faulty memory affecting compression computations
Troubleshooting and Resolution Steps
Check available memory and disk space:
free -h df -h /var/lib/clickhouseCompression requires working memory, and the result must be written to disk. Verify both are adequate.
Review the codec configuration for the affected table:
SELECT name, compression_codec FROM system.columns WHERE database = 'your_database' AND table = 'your_table';Try a simpler compression codec. If you are using a high compression level, reduce it:
ALTER TABLE your_table MODIFY COLUMN your_column CODEC(ZSTD(1));High compression levels (e.g., ZSTD(22)) require significantly more memory and CPU.
Check ClickHouse logs for more detailed error information:
grep -i "cannot compress" /var/log/clickhouse-server/clickhouse-server.logThe log often contains the underlying library error message which can pinpoint the exact cause.
Verify the ClickHouse build is not corrupted:
clickhouse-server --versionConsider reinstalling or upgrading if you suspect a faulty build.
Test compression independently by inserting a small amount of data into a test table with the same codec to isolate whether the issue is data-specific or systemic.
Best Practices
- Use moderate compression levels (ZSTD(1) through ZSTD(5)) for most workloads to balance compression ratio and resource usage.
- Monitor disk space proactively; do not let data volumes fill beyond 80%.
- Keep ClickHouse updated to benefit from bug fixes in compression libraries.
- Test codec changes on a staging environment before applying them to production tables.
Frequently Asked Questions
Q: Can high compression levels cause this error?
A: Yes. Very high compression levels like ZSTD(22) require substantially more memory per compression operation. If memory is constrained, the compression library may fail and ClickHouse will report the CANNOT_COMPRESS error.
Q: Is the data lost when this error occurs?
A: The specific write operation that failed will not be persisted, but existing data in the table remains intact. You can retry the insert after resolving the underlying issue.
Q: Could this be caused by a ClickHouse bug?
A: While rare, bugs in the compression library integration have occurred in past releases. Check the ClickHouse GitHub issues and changelog for known compression-related bugs in your version.