Skip to content

Commit 7abac2f

Browse files
committed
sql: DROP TABLE respects dynamic zone config GC TTL
fixes #21887 Release note (sql change): The GC of table data after a DROP TABLE respects changes to the GC TTL interval specified in the zone config
1 parent 23f9391 commit 7abac2f

9 files changed

Lines changed: 315 additions & 295 deletions

File tree

pkg/sql/crdb_internal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ CREATE TABLE crdb_internal.tables (
189189
state STRING NOT NULL,
190190
sc_lease_node_id INT,
191191
sc_lease_expiration_time TIMESTAMP,
192-
gc_deadline TIMESTAMP,
192+
drop_time TIMESTAMP,
193193
audit_mode STRING NOT NULL
194194
);
195195
`,
@@ -229,10 +229,10 @@ CREATE TABLE crdb_internal.tables (
229229
timeutil.Unix(0, table.Lease.ExpirationTime), time.Nanosecond,
230230
)
231231
}
232-
gcDeadlineDatum := tree.DNull
233-
if table.GCDeadline != 0 {
234-
gcDeadlineDatum = tree.MakeDTimestamp(
235-
timeutil.Unix(0, table.GCDeadline), time.Nanosecond,
232+
dropTimeDatum := tree.DNull
233+
if table.DropTime != 0 {
234+
dropTimeDatum = tree.MakeDTimestamp(
235+
timeutil.Unix(0, table.DropTime), time.Nanosecond,
236236
)
237237
}
238238
if err := addRow(
@@ -247,7 +247,7 @@ CREATE TABLE crdb_internal.tables (
247247
tree.NewDString(table.State.String()),
248248
leaseNodeDatum,
249249
leaseExpDatum,
250-
gcDeadlineDatum,
250+
dropTimeDatum,
251251
tree.NewDString(table.AuditMode.String()),
252252
); err != nil {
253253
return err

pkg/sql/drop_table.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package sql
1717
import (
1818
"context"
1919
"fmt"
20-
"time"
2120

2221
"github.com/pkg/errors"
2322

@@ -325,7 +324,7 @@ func (p *planner) initiateDropTable(
325324
}
326325

327326
// If the table is not interleaved and the ClearRange feature is
328-
// enabled in the cluster, use the GCDeadline mechanism to schedule
327+
// enabled in the cluster, use the delayed GC mechanism to schedule
329328
// usage of the more efficient ClearRange pathway. ClearRange will
330329
// only work if the entire hierarchy of interleaved tables are
331330
// dropped at once, as with ON DELETE CASCADE where the top-level
@@ -336,15 +335,15 @@ func (p *planner) initiateDropTable(
336335
if !tableDesc.IsInterleaved() &&
337336
p.ExecCfg().Settings.Version.IsActive(cluster.VersionClearRange) {
338337
// Get the zone config applying to this table in order to
339-
// set the GC deadline.
340-
_, zoneCfg, _, err := GetZoneConfigInTxn(
338+
// ensure there is a GC TTL.
339+
_, _, _, err := GetZoneConfigInTxn(
341340
ctx, p.txn, uint32(tableDesc.ID), &sqlbase.IndexDescriptor{}, "",
342341
)
343342
if err != nil {
344343
return err
345344
}
346-
tableDesc.GCDeadline = timeutil.Now().UnixNano() +
347-
int64(zoneCfg.GC.TTLSeconds)*time.Second.Nanoseconds()
345+
346+
tableDesc.DropTime = timeutil.Now().UnixNano()
348347
}
349348

350349
tableDesc.State = sqlbase.TableDescriptor_DROP

pkg/sql/drop_test.go

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,7 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
157157
}
158158

159159
tableSpan := tbDesc.TableSpan()
160-
if kvs, err := kvDB.Scan(ctx, tableSpan.Key, tableSpan.EndKey, 0); err != nil {
161-
t.Fatal(err)
162-
} else if l := 6; len(kvs) != l {
163-
t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
164-
}
160+
tests.CheckKeyCount(t, kvDB, tableSpan, 6)
165161

166162
if _, err := sqlDB.Exec(`DROP DATABASE t RESTRICT`); !testutils.IsError(err,
167163
`database "t" is not empty`) {
@@ -173,11 +169,7 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
173169
}
174170

175171
// Data is not deleted.
176-
if kvs, err := kvDB.Scan(ctx, tableSpan.Key, tableSpan.EndKey, 0); err != nil {
177-
t.Fatal(err)
178-
} else if l := 6; len(kvs) != l {
179-
t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
180-
}
172+
tests.CheckKeyCount(t, kvDB, tableSpan, 6)
181173

182174
if err := descExists(sqlDB, true, tbDesc.ID); err != nil {
183175
t.Fatal(err)
@@ -259,40 +251,32 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
259251
}
260252
tbDesc := desc.GetTable()
261253

262-
// Add a zone config for both the table and database.
263-
cfg := config.DefaultZoneConfig()
264-
cfg.GC.TTLSeconds = 0 // Set TTL so the data is deleted immediately.
265-
buf, err := protoutil.Marshal(&cfg)
266-
if err != nil {
267-
t.Fatal(err)
268-
}
269-
if _, err := sqlDB.Exec(`INSERT INTO system.zones VALUES ($1, $2)`, tbDesc.ID, buf); err != nil {
270-
t.Fatal(err)
271-
}
272-
if _, err := sqlDB.Exec(`INSERT INTO system.zones VALUES ($1, $2)`, dbDesc.ID, buf); err != nil {
273-
t.Fatal(err)
274-
}
254+
tableSpan := tbDesc.TableSpan()
255+
tests.CheckKeyCount(t, kvDB, tableSpan, 6)
275256

276-
if err := zoneExists(sqlDB, &cfg, tbDesc.ID); err != nil {
257+
if _, err := sqlDB.Exec(`DROP DATABASE t RESTRICT`); !testutils.IsError(err,
258+
`database "t" is not empty`) {
277259
t.Fatal(err)
278260
}
279-
if err := zoneExists(sqlDB, &cfg, dbDesc.ID); err != nil {
261+
262+
if _, err := sqlDB.Exec(`DROP DATABASE t CASCADE`); err != nil {
280263
t.Fatal(err)
281264
}
282265

283-
tableSpan := tbDesc.TableSpan()
284-
if kvs, err := kvDB.Scan(ctx, tableSpan.Key, tableSpan.EndKey, 0); err != nil {
266+
tests.CheckKeyCount(t, kvDB, tableSpan, 6)
267+
268+
// Push a new zone config for both the table and database with TTL=0
269+
// so the data is deleted immediately.
270+
cfg := config.DefaultZoneConfig()
271+
cfg.GC.TTLSeconds = 0
272+
buf, err := protoutil.Marshal(&cfg)
273+
if err != nil {
285274
t.Fatal(err)
286-
} else if l := 6; len(kvs) != l {
287-
t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
288275
}
289-
290-
if _, err := sqlDB.Exec(`DROP DATABASE t RESTRICT`); !testutils.IsError(err,
291-
`database "t" is not empty`) {
276+
if _, err := sqlDB.Exec(`INSERT INTO system.zones VALUES ($1, $2)`, tbDesc.ID, buf); err != nil {
292277
t.Fatal(err)
293278
}
294-
295-
if _, err := sqlDB.Exec(`DROP DATABASE t CASCADE`); err != nil {
279+
if _, err := sqlDB.Exec(`INSERT INTO system.zones VALUES ($1, $2)`, dbDesc.ID, buf); err != nil {
296280
t.Fatal(err)
297281
}
298282

@@ -305,11 +289,7 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
305289
})
306290

307291
// Data is deleted.
308-
if kvs, err := kvDB.Scan(ctx, tableSpan.Key, tableSpan.EndKey, 0); err != nil {
309-
t.Fatal(err)
310-
} else if l := 0; len(kvs) != l {
311-
t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
312-
}
292+
tests.CheckKeyCount(t, kvDB, tableSpan, 0)
313293
}
314294

315295
// Tests that SHOW TABLES works correctly when a database is recreated
@@ -618,24 +598,27 @@ func TestDropTableDeleteData(t *testing.T) {
618598
t.Fatalf("Name entry %q does not exist", nameKey)
619599
}
620600

621-
// Add a zone config for the table.
622-
cfg := config.DefaultZoneConfig()
623-
cfg.GC.TTLSeconds = 0 // Set TTL so the data is deleted immediately.
624-
buf, err := protoutil.Marshal(&cfg)
625-
if err != nil {
601+
tableSpan := tableDesc.TableSpan()
602+
tests.CheckKeyCount(t, kvDB, tableSpan, 3*numRows)
603+
if _, err := sqlDB.Exec(`DROP TABLE t.kv`); err != nil {
626604
t.Fatal(err)
627605
}
628-
if _, err := sqlDB.Exec(`INSERT INTO system.zones VALUES ($1, $2)`, tableDesc.ID, buf); err != nil {
606+
607+
// Data still exists.
608+
if err := descExists(sqlDB, true, tableDesc.ID); err != nil {
629609
t.Fatal(err)
630610
}
611+
tests.CheckKeyCount(t, kvDB, tableSpan, 3*numRows)
631612

632-
if err := zoneExists(sqlDB, &cfg, tableDesc.ID); err != nil {
613+
// Push a new zone config for the table with TTL=0 so the data
614+
// is deleted immediately.
615+
cfg := config.DefaultZoneConfig()
616+
cfg.GC.TTLSeconds = 0
617+
buf, err := protoutil.Marshal(&cfg)
618+
if err != nil {
633619
t.Fatal(err)
634620
}
635-
636-
tableSpan := tableDesc.TableSpan()
637-
tests.CheckKeyCount(t, kvDB, tableSpan, 3*numRows)
638-
if _, err := sqlDB.Exec(`DROP TABLE t.kv`); err != nil {
621+
if _, err := sqlDB.Exec(`INSERT INTO system.zones VALUES ($1, $2)`, tableDesc.ID, buf); err != nil {
639622
t.Fatal(err)
640623
}
641624

pkg/sql/logictest/testdata/logic_test/crdb_internal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ table_id parent_id name type target_id target_name state direction
3737
query IITTITRTTTTTT colnames
3838
SELECT * FROM crdb_internal.tables WHERE NAME = 'namespace'
3939
----
40-
table_id parent_id name database_name version mod_time mod_time_logical format_version state sc_lease_node_id sc_lease_expiration_time gc_deadline audit_mode
41-
2 1 namespace system 1 1970-01-01 00:00:00 +0000 +0000 0E-10 InterleavedFormatVersion PUBLIC NULL NULL NULL DISABLED
40+
table_id parent_id name database_name version mod_time mod_time_logical format_version state sc_lease_node_id sc_lease_expiration_time drop_time audit_mode
41+
2 1 namespace system 1 1970-01-01 00:00:00 +0000 +0000 0E-10 InterleavedFormatVersion PUBLIC NULL NULL NULL DISABLED
4242

4343
# Verify that table names are not double escaped.
4444

0 commit comments

Comments
 (0)