Repository navigation
Commit e5ae260
Forbid to create a table as a MergeTree introspection table function
CREATE TABLE p AS mergeTreeIndex('db', 's') was accepted, and once p had
been read it made s undroppable: DROP TABLE s SYNC never returned, s stayed
in system.dropped_tables forever, and its data was never reclaimed from
disk. The same persisted form is wrong two further ways that were measured
alongside: reads of p keep describing the dropped table (count(p) stayed at
2 after the source was dropped and re-created with 3 rows), and p stops
seeing new parts of a live source, because the parts vector is captured in
the storage's constructor.
A persisted table function is wrapped in a StorageTableFunctionProxy, whose
getNestedImpl memoises the storage it built in nested and clears
get_nested, so the storage lives for the whole server lifetime after the
first read. Each of these five storages keeps the source table's StoragePtr
as a data member: StorageMergeTreeIndex::source_table,
StorageFromMergeTreeProjection::parent_storage,
StorageMergeTreeAnalyzeIndexes::source_table,
StorageMergeTreeTextIndex::source_table and
StorageMergeTreeCodecBlockCounts::source_table. DROP TABLE s SYNC detaches
s, pushes it onto tables_marked_dropped and waits in
DatabaseCatalog::waitTableFinallyDropped, but the background drop task only
takes an entry whose pointer is unique (DatabaseCatalog::getTablesToDrop,
bool in_use = it->table && !isSharedPtrUnique(it->table)). The memoised
member is a permanent second reference, so in_use stays true, the entry is
never selected, and the wait loop exits only on is_shutting_down. The gate
is an ordinary shared_ptr use-count test rather than an assertion, so
release builds hang identically.
Removing the member is not the fix. Four of the five storages also cache
MergeTreeData::DataPartsVector data_parts in their constructor, and
IMergeTreeDataPart references its storage by raw reference (const
MergeTreeData & storage), so the cached parts do not keep the source alive
and would dangle. The tree states this in two places:
MergeTreeData::SnapshotData carries a ConstStoragePtr storage to
compensate, and StorageMergeTreeCodecBlockCounts says "The parts reference
the source table's MergeTreeData without owning it" before calling
addStorageHolder. Resolving the source per read instead would be a redesign
rather than a mechanical change, above all for
StorageFromMergeTreeProjection, whose const MergeTreeData & member cannot
be rebound, whose projection is a raw interior pointer into the parent
metadata, and whose constructor derives the storage's own schema from the
source's projection.
So the defect is not that the storage holds a pointer, which it must, but
that this storage is allowed to outlive its query.
canBeUsedToCreateTable() is the mechanism that already expresses exactly
that, used by eval, viewIfPermitted and the cluster functions, so give the
five table functions the same override. Five class bodies cover six
registered names, because mergeTreeAnalyzeIndexes and
mergeTreeAnalyzeIndexesUUID are the same class registered twice. Nothing on
the drop path changes, no bound is relaxed, and the transient
SELECT ... FROM mergeTreeIndex(...) form, the only documented and only
in-tree-exercised usage, is untouched.
The veto inspects only the outermost function, so nested carriers were
measured rather than assumed: remote(self, view(SELECT * FROM
mergeTreeIndex(...))), merge() over a database holding the proxy, and
ENGINE = Remote(..., mergeTreeIndex(...)) each materialise the inner
storage and still release it, because the outer storage keeps the AST and
rebuilds the inner one per query. The direct persisted form is therefore
the only pinning carrier.
canBeUsedToCreateTable() is consulted at two sites and only one is reached
by metadata replay, since the inferred column list is itself persisted. A
server that already holds such a table therefore keeps loading it at
startup and on ATTACH DATABASE, which route through
DatabaseOnDisk::createTableFromAST and do not consult the veto, while an
explicit ATTACH TABLE, a RESTORE, a replica recovery and
CREATE TABLE x AS p are refused. That asymmetry is the same one the
existing vetoes already carry, and it applies to a form with no uses
anywhere in the tree.
Co-Authored-By: Claude Opus 5 <[email protected]>1 parent 55c2563 commit e5ae260
7 files changed
Lines changed: 42 additions & 0 deletions
File tree
- src/TableFunctions
- tests/queries/0_stateless
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
92 | 95 | | |
93 | 96 | | |
94 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
35 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
0 commit comments