Skip to content

Commit

Permalink
sql: do not allow SET (ttl_expire_after) if automatic column defined
Browse files Browse the repository at this point in the history
Note we allow this for CREATE TABLE so SHOW CREATE TABLE round trips.
But we should never need this for the ALTER TABLE ... SET ...

Release note: None
  • Loading branch information
otan committed Feb 23, 2022
1 parent b7bd998 commit 56756f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,13 @@ func handleTTLStorageParamChange(
// Adding a TTL requires adding the automatic column and deferring the TTL
// addition to after the column is successfully added.
tableDesc.RowLevelTTL = nil
if _, err := tableDesc.FindColumnWithName(colinfo.TTLDefaultExpirationColumnName); err == nil {
return pgerror.Newf(
pgcode.InvalidTableDefinition,
"cannot add TTL to table with the %s column already defined",
colinfo.TTLDefaultExpirationColumnName,
)
}
col, err := rowLevelTTLAutomaticColumnDef(after)
if err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/row_level_ttl
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ CREATE TABLE public.tbl (
FAMILY fam_0_id_text_crdb_internal_expiration (id, text, crdb_internal_expiration)
) WITH (ttl = 'on', ttl_automatic_column = 'on', ttl_expire_after = '00:10:00':::INTERVAL)

# Test adding to TTL table with crdb_internal_expiration already defined.
statement ok
DROP TABLE tbl;
CREATE TABLE tbl (
id INT PRIMARY KEY,
text TEXT,
crdb_internal_expiration TIMESTAMPTZ,
FAMILY (id, text)
)

statement error cannot add TTL to table with the crdb_internal_expiration column already defined
ALTER TABLE tbl SET (ttl_expire_after = '10 minutes')

# Test we cannot add FKs to a TTL table.
statement ok
CREATE TABLE ref_table (id INT PRIMARY KEY, ref INT)
Expand Down

0 comments on commit 56756f8

Please sign in to comment.