#20562·databend

bug: CTAS CREATE OR REPLACE leaves old table with NULL dropped_on in tables_with_history

Author: TCeasonCreated Sep 23, 2026Updated Sep 23, 2026
LabelsC-bug

Search before asking

  • I searched existing issues and found no report of this CTAS replace behavior.

Version

Reproduced on the currently running instance; the exact output of SELECT version() was not captured.

What's Wrong?

After CREATE OR REPLACE TABLE ... AS SELECT, the replaced table remains in system.tables_with_history with dropped_on = NULL, even though the visible table name now maps to a different table ID. This makes the old, no-longer-visible table appear active in the history view.

In the reproduction below, system.tables_with_history returns both table IDs 18232 and 18242 with dropped_on = NULL, whereas system.tables returns only 18242.

The CTAS replace path stages a dropped table, then commit_table_meta() switches the name mapping to the new table ID and appends it to the history list. It checks the previous table metadata but does not update its drop_on in the transaction (src/meta/api/src/api_impl/table_api.rs). This appears to differ from the non-CTAS replace path. Any implications for retention, undrop, or vacuum should also be checked.

How to Reproduce?

sql
CREATE TABLE t1 (id INT);
CREATE OR REPLACE TABLE t1 AS SELECT 1;

SELECT name, table_id, dropped_on
FROM system.tables_with_history
WHERE name = 't1';

SELECT name, table_id, dropped_on
FROM system.tables
WHERE name = 't1';

Observed system.tables_with_history result:

name table_id dropped_on
t1 18232 NULL
t1 18242 NULL

Observed system.tables result:

name table_id dropped_on
t1 18242 NULL

Expected: the old table ID should not retain a NULL drop time after it is replaced and no longer visible by name; the historical state should be consistent with the visible table mapping.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!