@@ -20,6 +20,7 @@ import (
2020 "strconv"
2121 "strings"
2222 "sync"
23+ "sync/atomic"
2324
2425 "github.com/dolthub/vitess/go/sqltypes"
2526 querypb "github.com/dolthub/vitess/go/vt/proto/query"
@@ -136,7 +137,7 @@ type Engine struct {
136137 ProcessList sql.ProcessList
137138 MemoryManager * sql.MemoryManager
138139 BackgroundThreads * sql.BackgroundThreads
139- IsReadOnly bool
140+ ReadOnly atomic. Bool
140141 IsServerLocked bool
141142 PreparedDataCache * PreparedDataCache
142143 mu * sync.Mutex
@@ -168,17 +169,18 @@ func New(a *analyzer.Analyzer, cfg *Config) *Engine {
168169 })
169170 a .Catalog .RegisterFunction (emptyCtx , function .GetLockingFuncs (ls )... )
170171
171- return & Engine {
172+ ret := & Engine {
172173 Analyzer : a ,
173174 MemoryManager : sql .NewMemoryManager (sql .ProcessMemory ),
174175 ProcessList : NewProcessList (),
175176 LS : ls ,
176177 BackgroundThreads : sql .NewBackgroundThreads (),
177- IsReadOnly : cfg .IsReadOnly ,
178178 IsServerLocked : cfg .IsServerLocked ,
179179 PreparedDataCache : NewPreparedDataCache (),
180180 mu : & sync.Mutex {},
181181 }
182+ ret .ReadOnly .Store (cfg .IsReadOnly )
183+ return ret
182184}
183185
184186// NewDefault creates a new default Engine.
@@ -635,23 +637,19 @@ func (e *Engine) WithBackgroundThreads(b *sql.BackgroundThreads) *Engine {
635637 return e
636638}
637639
640+ func (e * Engine ) IsReadOnly () bool {
641+ return e .ReadOnly .Load ()
642+ }
643+
638644// readOnlyCheck checks to see if the query is valid with the modification setting of the engine.
639645func (e * Engine ) readOnlyCheck (node sql.Node ) error {
640- if plan .IsDDLNode (node ) {
641- if e .IsReadOnly {
642- return sql .ErrReadOnly .New ()
643- } else if e .IsServerLocked {
644- return sql .ErrDatabaseWriteLocked .New ()
645- }
646+ // Note: We only compute plan.IsReadOnly if the server is in one of
647+ // these two modes, since otherwise it is simply wasted work.
648+ if e .IsReadOnly () && ! plan .IsReadOnly (node ) {
649+ return sql .ErrReadOnly .New ()
646650 }
647- switch node .(type ) {
648- case
649- * plan.DeleteFrom , * plan.InsertInto , * plan.Update , * plan.LockTables , * plan.UnlockTables :
650- if e .IsReadOnly {
651- return sql .ErrReadOnly .New ()
652- } else if e .IsServerLocked {
653- return sql .ErrDatabaseWriteLocked .New ()
654- }
651+ if e .IsServerLocked && ! plan .IsReadOnly (node ) {
652+ return sql .ErrDatabaseWriteLocked .New ()
655653 }
656654 return nil
657655}
0 commit comments