Skip to content

Commit

Permalink
sql: change when txn is checked refresh ...
Browse files Browse the repository at this point in the history
materialized views

Due to the transaction being checked during planning
and not during execution, refresh materialized views
would fail from the client. Now the transaction is checked
during execution .

Release justification: Bug fix for refresh materialized views

Release note: None
  • Loading branch information
e-mbrown committed Sep 6, 2022
1 parent 977e294 commit fcf22cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 10 additions & 1 deletion pkg/sql/materialized_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ REFRESH MATERIALIZED VIEW t.v;
t.Fatal(err)
}

// Verify that refreshing with a prepared statement works.
preparedStmt, err := sqlDB.Prepare(`REFRESH MATERIALIZED VIEW t.v;`)
if err != nil {
t.Fatal(err)
}
if _, err := preparedStmt.Exec(); err != nil {
t.Fatal(err)
}

// Add a zone config to delete all table data.
_, err := sqltestutils.AddImmediateGCZoneConfig(sqlDB, descBeforeRefresh.GetID())
_, err = sqltestutils.AddImmediateGCZoneConfig(sqlDB, descBeforeRefresh.GetID())
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/refresh_materialized_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ type refreshMaterializedViewNode struct {
func (p *planner) RefreshMaterializedView(
ctx context.Context, n *tree.RefreshMaterializedView,
) (planNode, error) {
if !p.extendedEvalCtx.TxnIsSingleStmt {
return nil, pgerror.Newf(pgcode.InvalidTransactionState, "cannot refresh view in a multi-statement transaction")
}
_, desc, err := p.ResolveMutableTableDescriptorEx(ctx, n.Name, true /* required */, tree.ResolveRequireViewDesc)
if err != nil {
return nil, err
Expand Down Expand Up @@ -80,6 +77,10 @@ func (n *refreshMaterializedViewNode) startExec(params runParams) error {
// results of the view query into the new set of indexes, and then change the
// set of indexes over to the new set of indexes atomically.

if !params.p.extendedEvalCtx.TxnIsSingleStmt {
return pgerror.Newf(pgcode.InvalidTransactionState, "cannot refresh view in a multi-statement transaction")
}

telemetry.Inc(sqltelemetry.SchemaRefreshMaterializedView)

// Inform the user that CONCURRENTLY is not needed.
Expand Down

0 comments on commit fcf22cd

Please sign in to comment.