Skip to content

Commit

Permalink
Merge #86246 #86944
Browse files Browse the repository at this point in the history
86246: sql: Implement DISCARD TEMP r=rafiss a=e-mbrown

Refs #83061

Release note (sql change): We now support DISCARD TEMP/TEMPORARY, which drops all temporary tables created in the current session. The command does not drop temporary schemas.

Release note (bug fix): DISCARD ALL now deletes temporary tables

Release justification: Low risk, high benefit changes to existing functionality(DISCARD ALL)

86944: batcheval: fix incorrect StartTime in ExportResponse r=stevendanna a=adityamaru

Previously, ExportResponse for a revision history backup
was always setting the `StartTime` in the response to the
GCThreshold of the span being exported. This is correct for
a full backup where revision history stretches only as far back
as the GCThreshold, but is incorrect for incremental backups
where the StartTime should stretch as far back as the StartTime
of the ExportRequest.

Thankfully, this StartTime in ExportResponse is only used for
validating that the restore AOST is greater than the StartTime
of the full backup i.e. this field is inconsequential for incrementals.
Nonetheless, we should send back an accurate response.

Release note: None

Release justification: low risk bug fix

Co-authored-by: e-mbrown <[email protected]>
Co-authored-by: adityamaru <[email protected]>
  • Loading branch information
3 people committed Aug 31, 2022
3 parents da00c3a + 50639e4 + 8709b40 commit 0602871
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/bnf/discard_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
discard_stmt ::=
'DISCARD' 'ALL'
| 'DISCARD' 'SEQUENCES'
| 'DISCARD' 'TEMP'
| 'DISCARD' 'TEMPORARY'
2 changes: 2 additions & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ deallocate_stmt ::=
discard_stmt ::=
'DISCARD' 'ALL'
| 'DISCARD' 'SEQUENCES'
| 'DISCARD' 'TEMP'
| 'DISCARD' 'TEMPORARY'

grant_stmt ::=
'GRANT' privileges 'ON' grant_targets 'TO' role_spec_list opt_with_grant_option
Expand Down
5 changes: 4 additions & 1 deletion pkg/kv/kvserver/batcheval/cmd_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func evalExport(
// BACKUP to correctly note the supported time bounds for RESTORE AS OF SYSTEM
// TIME.
if args.MVCCFilter == roachpb.MVCCFilter_All {
reply.StartTime = cArgs.EvalCtx.GetGCThreshold()
reply.StartTime = args.StartTime
if args.StartTime.IsEmpty() {
reply.StartTime = cArgs.EvalCtx.GetGCThreshold()
}
}

var exportAllRevisions bool
Expand Down
32 changes: 32 additions & 0 deletions pkg/sql/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (n *discardNode) startExec(params runParams) error {

// RESET ALL
if err := params.p.resetAllSessionVars(params.ctx); err != nil {

return err
}

Expand All @@ -59,13 +60,44 @@ func (n *discardNode) startExec(params runParams) error {
m.data.SequenceState = sessiondata.NewSequenceState()
m.initSequenceCache()
})

// DISCARD TEMP
err := deleteTempTables(params.ctx, params.p)
if err != nil {
return err
}

case tree.DiscardModeSequences:
params.p.sessionDataMutatorIterator.applyOnEachMutator(func(m sessionDataMutator) {
m.data.SequenceState = sessiondata.NewSequenceState()
m.initSequenceCache()
})
case tree.DiscardModeTemp:
err := deleteTempTables(params.ctx, params.p)
if err != nil {
return err
}
default:
return errors.AssertionFailedf("unknown mode for DISCARD: %d", n.mode)
}
return nil
}

func deleteTempTables(ctx context.Context, p *planner) error {
codec := p.execCfg.Codec
descCol := p.Descriptors()
allDbDescs, err := descCol.GetAllDatabaseDescriptors(ctx, p.Txn())
if err != nil {
return err
}
ie := p.execCfg.InternalExecutor

for _, dbDesc := range allDbDescs {
schemaName := p.TemporarySchemaName()
err = cleanupSchemaObjects(ctx, p.execCfg.Settings, p.Txn(), descCol, codec, ie, dbDesc, schemaName)
if err != nil {
return err
}
}
return nil
}
41 changes: 41 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/discard
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,44 @@ query I
SELECT nextval('s2')
----
11

statement ok
SET experimental_enable_temp_tables=on

query I
SELECT count(*) FROM [SHOW SCHEMAS] WHERE schema_name LIKE 'pg_temp_%'
----
0

statement ok
DISCARD TEMP;

query I
SELECT count(*) FROM [SHOW SCHEMAS] WHERE schema_name LIKE 'pg_temp_%'
----
0

statement ok
CREATE TEMP TABLE test (a int);

statement ok
CREATE TEMP TABLE test2 (a uuid);

query T rowsort
SELECT table_name FROM [SHOW TABLES FROM pg_temp]
----
test
test2

statement ok
DISCARD TEMP;

query T rowsort
SELECT table_name FROM [SHOW TABLES FROM pg_temp]
----

#Ensure temp schema is not deleted
query I
SELECT count(*) FROM [SHOW SCHEMAS] WHERE schema_name LIKE 'pg_temp_%'
----
1
2 changes: 0 additions & 2 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,6 @@ func TestUnimplementedSyntax(t *testing.T) {
{`DROP TRIGGER a`, 28296, `drop`, ``},

{`DISCARD PLANS`, 0, `discard plans`, ``},
{`DISCARD TEMP`, 0, `discard temp`, ``},
{`DISCARD TEMPORARY`, 0, `discard temp`, ``},

{`SET CONSTRAINTS foo`, 0, `set constraints`, ``},
{`SET foo FROM CURRENT`, 0, `set from current`, ``},
Expand Down
10 changes: 8 additions & 2 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -4816,8 +4816,14 @@ discard_stmt:
{
$$.val = &tree.Discard{Mode: tree.DiscardModeSequences}
}
| DISCARD TEMP { return unimplemented(sqllex, "discard temp") }
| DISCARD TEMPORARY { return unimplemented(sqllex, "discard temp") }
| DISCARD TEMP
{
$$.val = &tree.Discard{Mode: tree.DiscardModeTemp}
}
| DISCARD TEMPORARY
{
$$.val = &tree.Discard{Mode: tree.DiscardModeTemp}
}
| DISCARD error // SHOW HELP: DISCARD

// %Help: DROP
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/sem/tree/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const (

// DiscardModeSequences represents a DISCARD SEQUENCES statement
DiscardModeSequences

// DiscardModeTemp represents a DISCARD TEMPORARY statement
DiscardModeTemp
)

// Format implements the NodeFormatter interface.
Expand All @@ -35,6 +38,8 @@ func (node *Discard) Format(ctx *FmtCtx) {
ctx.WriteString("DISCARD ALL")
case DiscardModeSequences:
ctx.WriteString("DISCARD SEQUENCES")
case DiscardModeTemp:
ctx.WriteString("DISCARD TEMPORARY")
}
}

Expand Down

0 comments on commit 0602871

Please sign in to comment.