Repository navigation
Add settings to limit the number and the size of temporary tables - #122165
Conversation
There was no way to limit how many temporary tables a session creates or how large they grow. Add four user/profile/query settings, all `0` (unlimited) by default: - `max_temporary_tables`: the number of tables created with `CREATE TEMPORARY TABLE` in a session, checked when a new one is created (`TOO_MANY_TABLES`); - `max_temporary_table_memory_usage`: the memory of one temporary table with the `Memory` engine, checked in `MemorySink` while the data is received and before it is added (`TOO_MANY_BYTES`); - `max_temporary_table_size_bytes_compressed` and `max_temporary_table_size_bytes_uncompressed`: the size of one temporary table with a `MergeTree`-family engine, checked in `MergeTreeSink` before each part is committed (`TOO_MANY_BYTES`). The sizes are counted as `total_bytes` / `total_bytes_uncompressed` in `system.tables`. The internal temporary tables for external data, `GLOBAL IN` and CTEs are not affected: they have their own limits (`max_bytes_to_transfer` and others). The existing `max_bytes_to_keep` of the `Memory` engine is not similar: it evicts old data instead of rejecting new data. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [10a7fa6] Summary: ✅
AI ReviewSummaryThis PR adds Findings❌ Blockers
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 10a7fa6.
Changed lines: Changed C/C++ lines covered: 170/182 (93.41%) · Uncovered code |
Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`StorageMemory` now keeps the row and byte counters together with the blocks in `BlocksWithCounts`, so the temporary table memory check reads the size from `data` instead of the removed `total_size_bytes`. Also add the `database = ''` condition to the queries to `system.tables` in the test, as required by the style check. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122165&sha=a27d21cccc8519dda99acc90c2704ef981183270&name_0=PR&name_1=Fast%20test #122165 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…geTree` tables `checkTemporaryTableSize` ran before `commitPart` applied the non-replicated deduplication, so re-inserting an already seen block could throw `TOO_MANY_BYTES` although nothing would be added. Skip the check when the block is already in the deduplication log; the part rebuilt from the remaining rows is checked on the next try. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ns of temporary `Memory` tables `StorageMemory::mutate` rebuilt the data and committed it without the cap check, so `ALTER TABLE ... UPDATE` could grow a temporary `Memory` table past the limit. Check the size of the mutated data before `setData`, using the settings of the `ALTER` query. For the `MergeTree` family, mutations and merges run in the background and rewrite parts outside of the insert sink, so document that the `max_temporary_table_size_bytes_compressed` / `_uncompressed` limits apply only to `INSERT`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Commit See the job log for details. |
…attaching parts `max_temporary_table_size_bytes_compressed` and `max_temporary_table_size_bytes_uncompressed` were only checked in `MergeTreeSink`, so `ATTACH PART`, `ATTACH PARTITION ... FROM`, `REPLACE PARTITION ... FROM` and `CREATE TEMPORARY TABLE ... CLONE AS` (which goes through `replacePartitionFrom`) could grow a temporary table past the limit. Check the whole batch of parts under the parts lock in these paths (and for the destination of `MOVE PARTITION TO TABLE`), allowing operations that do not increase the size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ry table size limit as a whole `attachPartition` commits the parts loaded from `detached` one by one, so checking `max_temporary_table_size_bytes_compressed` / `max_temporary_table_size_bytes_uncompressed` per part could leave a partition half attached. Check the whole batch before the first commit (each part is still checked again in case the table grows concurrently), and cover this in the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lly with the commit The size check of `INSERT` into a temporary `MergeTree` table is moved into `MergeTreeSink::commitPart`, under the same parts lock as the deduplication log update and the commit, so a concurrent insert of the same block cannot make a deduplicated insert fail with `TOO_MANY_BYTES`, and concurrent inserts cannot exceed the limit. It reuses `throwIfTemporaryTableSizeLimitsExceededForReplacement`. `ATTACH PARTITION` no longer re-checks each part, which could leave the partition half attached under concurrent writes; the whole-batch check remains, and the setting documentation now says that concurrent writes may exceed the limit slightly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`RESTORE TEMPORARY TABLE` bypassed `max_temporary_table_memory_usage` for `Memory` tables (`StorageMemory::restoreDataImpl` called `setData` without a check) and `max_temporary_table_size_bytes_compressed` / `max_temporary_table_size_bytes_uncompressed` for `MergeTree` tables (`StorageMergeTree::attachRestoredParts` committed the parts without a check). Both are now checked with the settings of the `RESTORE` query; a `MergeTree` restore is checked for the whole batch before the first part is committed, so it is rejected as a whole. Addresses the review comments on ClickHouse#122165 Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was no way to limit how many temporary tables a session creates or how large they grow, so a single session could fill the server's memory or disk with
CREATE TEMPORARY TABLE. This PR adds four user/profile/query settings. All of them default to0, which means unlimited:max_temporary_tables: the number of tables created withCREATE TEMPORARY TABLEin one session. It is checked when a new table is created and throwsTOO_MANY_TABLES.CREATE OR REPLACEof an existing table andIF NOT EXISTSdo not count as new tables.max_temporary_table_memory_usage: the memory used by one temporary table with theMemoryengine, the default temporary engine.MemorySinkchecks it while data is received, so an oversizedINSERTfails early instead of being buffered first, and again before the data is added. It throwsTOO_MANY_BYTES.max_temporary_table_size_bytes_compressed/max_temporary_table_size_bytes_uncompressed: the size of one temporary table with aMergeTree-family engine.MergeTreeSinkchecks them before each part is committed and throwsTOO_MANY_BYTES.Sizes are counted the same way as
total_bytes/total_bytes_uncompressedinsystem.tables. The value is taken from the settings of theINSERT, soreadonlyor settings constraints in a profile can enforce it.Existing similar settings: the
Memoryengine's ownmax_bytes_to_keep/max_rows_to_keepevict old data rather than reject new data, andmax_temporary_data_on_disk_size_*only covers spilling. If bothmax_bytes_to_keepand the new limit are set, eviction is applied first.Scope: the settings only apply to tables created with
CREATE TEMPORARY TABLE. The internal temporary tables for external data,GLOBAL IN/GLOBAL JOINand CTEs already have their own limits (max_bytes_to_transferand others) and are not affected. In the code, onlyCREATE TEMPORARY TABLEbuilds aMemorytable throughStorageFactoryinside the temporary database; the internal tables constructStorageMemorydirectly. The storage is marked inregisterStorageMemory.Known limitations, all documented in the setting descriptions:
INSERTthat writes with several threads commits each thread's data separately, so some of the data may already be added when another thread throws. The table still never exceeds the limit.MergeTree, parts committed earlier by the sameINSERTstay in the table, as with any other error during a multi-blockINSERT.max_table_num_to_throwhas the same best-effort behavior.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add the settings
max_temporary_tables,max_temporary_table_memory_usage,max_temporary_table_size_bytes_compressed, andmax_temporary_table_size_bytes_uncompressedto limit the number of temporary tables in a session, the memory usage ofMemorytemporary tables, and the size ofMergeTreetemporary tables.Documentation entry for user-facing changes
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122165]
Version info
26.10.1.1798-master(included in26.10and later)