-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
conn_executor_exec.go
1472 lines (1346 loc) · 52.1 KB
/
conn_executor_exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"fmt"
"runtime/pprof"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/paramparse"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/cancelchecker"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/fsm"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
"github.com/opentracing/opentracing-go"
)
// execStmt executes one statement by dispatching according to the current
// state. Returns an Event to be passed to the state machine, or nil if no
// transition is needed. If nil is returned, then the cursor is supposed to
// advance to the next statement.
//
// If an error is returned, the session is supposed to be considered done. Query
// execution errors are not returned explicitly and they're also not
// communicated to the client. Instead they're incorporated in the returned
// event (the returned payload will implement payloadWithError). It is the
// caller's responsibility to deliver execution errors to the client.
//
// Args:
// stmt: The statement to execute.
// res: Used to produce query results.
// pinfo: The values to use for the statement's placeholders. If nil is passed,
// then the statement cannot have any placeholder.
func (ex *connExecutor) execStmt(
ctx context.Context, stmt Statement, res RestrictedCommandResult, pinfo *tree.PlaceholderInfo,
) (fsm.Event, fsm.EventPayload, error) {
if log.V(2) || logStatementsExecuteEnabled.Get(&ex.server.cfg.Settings.SV) ||
log.HasSpanOrEvent(ctx) {
log.VEventf(ctx, 2, "executing: %s in state: %s", stmt, ex.machine.CurState())
}
// Stop the session idle timeout when a new statement is executed.
ex.mu.IdleInSessionTimeout.Stop()
ex.mu.IdleInTransactionSessionTimeout.Stop()
// Run observer statements in a separate code path; their execution does not
// depend on the current transaction state.
if _, ok := stmt.AST.(tree.ObserverStatement); ok {
ex.statsCollector.reset(&ex.server.sqlStats, ex.appStats, &ex.phaseTimes)
err := ex.runObserverStatement(ctx, stmt, res)
// Note that regardless of res.Err(), these observer statements don't
// generate error events; transactions are always allowed to continue.
return nil, nil, err
}
queryID := ex.generateID()
stmt.queryID = queryID
// Dispatch the statement for execution based on the current state.
var ev fsm.Event
var payload fsm.EventPayload
var err error
switch ex.machine.CurState().(type) {
case stateNoTxn:
ev, payload = ex.execStmtInNoTxnState(ctx, stmt)
case stateOpen:
if ex.server.cfg.Settings.CPUProfileType() == cluster.CPUProfileWithLabels {
remoteAddr := "internal"
if rAddr := ex.sessionData.RemoteAddr; rAddr != nil {
remoteAddr = rAddr.String()
}
labels := pprof.Labels(
"appname", ex.sessionData.ApplicationName,
"addr", remoteAddr,
"stmt.tag", stmt.AST.StatementTag(),
"stmt.anonymized", stmt.AnonymizedStr,
)
pprof.Do(ctx, labels, func(ctx context.Context) {
ev, payload, err = ex.execStmtInOpenState(ctx, stmt, res, pinfo)
})
} else {
ev, payload, err = ex.execStmtInOpenState(ctx, stmt, res, pinfo)
}
switch ev.(type) {
case eventNonRetriableErr:
ex.recordFailure()
}
case stateAborted:
ev, payload = ex.execStmtInAbortedState(ctx, stmt, res)
case stateCommitWait:
ev, payload = ex.execStmtInCommitWaitState(stmt, res)
default:
panic(errors.AssertionFailedf("unexpected txn state: %#v", ex.machine.CurState()))
}
if ex.sessionData.IdleInSessionTimeout > 0 {
// Cancel the session if the idle time exceeds the idle in session timeout.
ex.mu.IdleInSessionTimeout = timeout{time.AfterFunc(
ex.sessionData.IdleInSessionTimeout,
ex.cancelSession,
)}
}
if ex.sessionData.IdleInTransactionSessionTimeout > 0 {
startIdleInTransactionSessionTimeout := func() {
switch stmt.AST.(type) {
case *tree.CommitTransaction, *tree.RollbackTransaction:
// Do nothing, the transaction is completed, we do not want to start
// an idle timer.
default:
ex.mu.IdleInTransactionSessionTimeout = timeout{time.AfterFunc(
ex.sessionData.IdleInTransactionSessionTimeout,
ex.cancelSession,
)}
}
}
switch ex.machine.CurState().(type) {
case stateAborted, stateCommitWait:
startIdleInTransactionSessionTimeout()
case stateOpen:
// Only start timeout if the statement is executed in an
// explicit transaction.
if !ex.implicitTxn() {
startIdleInTransactionSessionTimeout()
}
}
}
return ev, payload, err
}
func (ex *connExecutor) recordFailure() {
ex.metrics.EngineMetrics.FailureCount.Inc(1)
}
// execPortal executes a prepared statement. It is a "wrapper" around execStmt
// method that is performing additional work to track portal's state.
func (ex *connExecutor) execPortal(
ctx context.Context,
portal PreparedPortal,
portalName string,
stmtRes CommandResult,
pinfo *tree.PlaceholderInfo,
) (ev fsm.Event, payload fsm.EventPayload, err error) {
curStmt := Statement{
Statement: portal.Stmt.Statement,
Prepared: portal.Stmt,
ExpectedTypes: portal.Stmt.Columns,
AnonymizedStr: portal.Stmt.AnonymizedStr,
}
stmtCtx := withStatement(ctx, ex.curStmt)
switch ex.machine.CurState().(type) {
case stateOpen:
// We're about to execute the statement in an open state which
// could trigger the dispatch to the execution engine. However, it
// is possible that we're trying to execute an already exhausted
// portal - in such a scenario we should return no rows, but the
// execution engine is not aware of that and would run the
// statement as if it was running it for the first time. In order
// to prevent such behavior, we check whether the portal has been
// exhausted and execute the statement only if it hasn't. If it has
// been exhausted, then we do not dispatch the query for execution,
// but connExecutor will still perform necessary state transitions
// which will emit CommandComplete messages and alike (in a sense,
// by not calling execStmt we "execute" the portal in such a way
// that it returns 0 rows).
// Note that here we deviate from Postgres which returns an error
// when attempting to execute an exhausted portal which has a
// StatementType() different from "Rows".
if !portal.exhausted {
ev, payload, err = ex.execStmt(stmtCtx, curStmt, stmtRes, pinfo)
// Portal suspension is supported via a "side" state machine
// (see pgwire.limitedCommandResult for details), so when
// execStmt returns, we know for sure that the portal has been
// executed to completion, thus, it is exhausted.
// Note that the portal is considered exhausted regardless of
// the fact whether an error occurred or not - if it did, we
// still don't want to re-execute the portal from scratch.
// The current statement may have just closed and deleted the portal,
// so only exhaust it if it still exists.
if _, ok := ex.extraTxnState.prepStmtsNamespace.portals[portalName]; ok {
ex.exhaustPortal(portalName)
}
}
default:
ev, payload, err = ex.execStmt(stmtCtx, curStmt, stmtRes, pinfo)
}
return
}
// execStmtInOpenState executes one statement in the context of the session's
// current transaction.
// It handles statements that affect the transaction state (BEGIN, COMMIT)
// directly and delegates everything else to the execution engines.
// Results and query execution errors are written to res.
//
// This method also handles "auto commit" - committing of implicit transactions.
//
// If an error is returned, the connection is supposed to be consider done.
// Query execution errors are not returned explicitly; they're incorporated in
// the returned Event.
//
// The returned event can be nil if no state transition is required.
func (ex *connExecutor) execStmtInOpenState(
ctx context.Context, stmt Statement, res RestrictedCommandResult, pinfo *tree.PlaceholderInfo,
) (retEv fsm.Event, retPayload fsm.EventPayload, retErr error) {
ex.incrementStartedStmtCounter(stmt)
defer func() {
if retErr == nil && !payloadHasError(retPayload) {
ex.incrementExecutedStmtCounter(stmt)
}
}()
ex.state.mu.Lock()
ex.state.mu.stmtCount++
ex.state.mu.Unlock()
os := ex.machine.CurState().(stateOpen)
var timeoutTicker *time.Timer
queryTimedOut := false
doneAfterFunc := make(chan struct{}, 1)
// Canceling a query cancels its transaction's context so we take a reference
// to the cancelation function here.
unregisterFn := ex.addActiveQuery(stmt.queryID, stmt, ex.state.cancel)
// queryDone is a cleanup function dealing with unregistering a query.
// It also deals with overwriting res.Error to a more user-friendly message in
// case of query cancelation. res can be nil to opt out of this.
queryDone := func(ctx context.Context, res RestrictedCommandResult) {
if timeoutTicker != nil {
if !timeoutTicker.Stop() {
// Wait for the timer callback to complete to avoid a data race on
// queryTimedOut.
<-doneAfterFunc
}
}
unregisterFn()
// Detect context cancelation and overwrite whatever error might have been
// set on the result before. The idea is that once the query's context is
// canceled, all sorts of actors can detect the cancelation and set all
// sorts of errors on the result. Rather than trying to impose discipline
// in that jungle, we just overwrite them all here with an error that's
// nicer to look at for the client.
if res != nil && ctx.Err() != nil && res.Err() != nil {
// Even in the cases where the error is a retryable error, we want to
// intercept the event and payload returned here to ensure that the query
// is not retried.
retEv = eventNonRetriableErr{
IsCommit: fsm.FromBool(isCommit(stmt.AST)),
}
res.SetError(cancelchecker.QueryCanceledError)
retPayload = eventNonRetriableErrPayload{err: cancelchecker.QueryCanceledError}
}
// If the query timed out, we intercept the error, payload, and event here
// for the same reasons we intercept them for canceled queries above.
// Overriding queries with a QueryTimedOut error needs to happen after
// we've checked for canceled queries as some queries may be canceled
// because of a timeout, in which case the appropriate error to return to
// the client is one that indicates the timeout, rather than the more general
// query canceled error. It's important to note that a timed out query may
// not have been canceled (eg. We never even start executing a query
// because the timeout has already expired), and therefore this check needs
// to happen outside the canceled query check above.
if queryTimedOut {
// A timed out query should never produce retryable errors/events/payloads
// so we intercept and overwrite them all here.
retEv = eventNonRetriableErr{
IsCommit: fsm.FromBool(isCommit(stmt.AST)),
}
res.SetError(sqlerrors.QueryTimeoutError)
retPayload = eventNonRetriableErrPayload{err: sqlerrors.QueryTimeoutError}
}
}
// Generally we want to unregister after the auto-commit below. However, in
// case we'll execute the statement through the parallel execution queue,
// we'll pass the responsibility for unregistering to the queue.
defer func() {
if queryDone != nil {
queryDone(ctx, res)
}
}()
p := &ex.planner
stmtTS := ex.server.cfg.Clock.PhysicalTime()
ex.statsCollector.reset(&ex.server.sqlStats, ex.appStats, &ex.phaseTimes)
ex.resetPlanner(ctx, p, ex.state.mu.txn, stmtTS)
p.sessionDataMutator.paramStatusUpdater = res
p.noticeSender = res
var shouldCollectDiagnostics bool
var finishCollectionDiagnostics StmtDiagnosticsTraceFinishFunc
if explainBundle, ok := stmt.AST.(*tree.ExplainAnalyzeDebug); ok {
telemetry.Inc(sqltelemetry.ExplainAnalyzeDebugUseCounter)
// Always collect diagnostics for EXPLAIN ANALYZE (DEBUG).
shouldCollectDiagnostics = true
// Strip off the explain node to execute the inner statement.
stmt.AST = explainBundle.Statement
// TODO(radu): should we trim the "EXPLAIN ANALYZE (DEBUG)" part from
// stmt.SQL?
// Clear any ExpectedTypes we set if we prepared this statement (they
// reflect the column types of the EXPLAIN itself and not those of the inner
// statement).
stmt.ExpectedTypes = nil
// EXPLAIN ANALYZE (DEBUG) does not return the rows for the given query;
// instead it returns some text which includes a URL.
// TODO(radu): maybe capture some of the rows and include them in the
// bundle.
p.discardRows = true
} else {
shouldCollectDiagnostics, finishCollectionDiagnostics = ex.stmtDiagnosticsRecorder.ShouldCollectDiagnostics(ctx, stmt.AST)
if shouldCollectDiagnostics {
telemetry.Inc(sqltelemetry.StatementDiagnosticsCollectedCounter)
}
}
if shouldCollectDiagnostics {
p.collectBundle = true
tr := ex.server.cfg.AmbientCtx.Tracer
origCtx := ctx
var sp opentracing.Span
ctx, sp = tracing.StartSnowballTrace(ctx, tr, "traced statement")
// TODO(radu): consider removing this if/when #46164 is addressed.
p.extendedEvalCtx.Context = ctx
defer func() {
// Record the statement information that we've collected.
// Note that in case of implicit transactions, the trace contains the auto-commit too.
sp.Finish()
trace := tracing.GetRecording(sp)
ie := p.extendedEvalCtx.InternalExecutor.(*InternalExecutor)
placeholders := p.extendedEvalCtx.Placeholders
if finishCollectionDiagnostics != nil {
bundle, collectionErr := buildStatementBundle(
origCtx, ex.server.cfg.DB, ie, &p.curPlan, trace, placeholders,
)
finishCollectionDiagnostics(origCtx, bundle.trace, bundle.zip, collectionErr)
} else {
// Handle EXPLAIN ANALYZE (DEBUG).
// If there was a communication error, no point in setting any results.
if retErr == nil {
retErr = setExplainBundleResult(
origCtx, res, stmt.AST, trace, &p.curPlan, placeholders, ie, ex.server.cfg,
)
}
}
}()
}
if ex.server.cfg.TestingKnobs.WithStatementTrace != nil {
tr := ex.server.cfg.AmbientCtx.Tracer
var sp opentracing.Span
ctx, sp = tracing.StartSnowballTrace(ctx, tr, stmt.SQL)
// TODO(radu): consider removing this if/when #46164 is addressed.
p.extendedEvalCtx.Context = ctx
defer func() {
ex.server.cfg.TestingKnobs.WithStatementTrace(sp, stmt.SQL)
}()
}
makeErrEvent := func(err error) (fsm.Event, fsm.EventPayload, error) {
ev, payload := ex.makeErrEvent(err, stmt.AST)
return ev, payload, nil
}
// We exempt `SET` statements from the statement timeout, particularly so as
// not to block the `SET statement_timeout` command itself.
if ex.sessionData.StmtTimeout > 0 && stmt.AST.StatementTag() != "SET" {
timerDuration := ex.sessionData.StmtTimeout - timeutil.Since(ex.phaseTimes[sessionQueryReceived])
// There's no need to proceed with execution if the timer has already expired.
if timerDuration < 0 {
queryTimedOut = true
return makeErrEvent(sqlerrors.QueryTimeoutError)
}
timeoutTicker = time.AfterFunc(
timerDuration,
func() {
ex.cancelQuery(stmt.queryID)
queryTimedOut = true
doneAfterFunc <- struct{}{}
})
}
defer func() {
if filter := ex.server.cfg.TestingKnobs.StatementFilter; retErr == nil && filter != nil {
var execErr error
if perr, ok := retPayload.(payloadWithError); ok {
execErr = perr.errorCause()
}
filter(ctx, stmt.String(), execErr)
}
// Do the auto-commit, if necessary.
if retEv != nil || retErr != nil {
return
}
if os.ImplicitTxn.Get() {
retEv, retPayload = ex.handleAutoCommit(ctx, stmt.AST)
return
}
}()
switch s := stmt.AST.(type) {
case *tree.BeginTransaction:
// BEGIN is always an error when in the Open state. It's legitimate only in
// the NoTxn state.
return makeErrEvent(errTransactionInProgress)
case *tree.CommitTransaction:
// CommitTransaction is executed fully here; there's no plan for it.
ev, payload := ex.commitSQLTransaction(ctx, stmt.AST)
return ev, payload, nil
case *tree.RollbackTransaction:
// RollbackTransaction is executed fully here; there's no plan for it.
ev, payload := ex.rollbackSQLTransaction(ctx)
return ev, payload, nil
case *tree.Savepoint:
return ex.execSavepointInOpenState(ctx, s, res)
case *tree.ReleaseSavepoint:
ev, payload := ex.execRelease(ctx, s, res)
return ev, payload, nil
case *tree.RollbackToSavepoint:
ev, payload := ex.execRollbackToSavepointInOpenState(ctx, s, res)
return ev, payload, nil
case *tree.Prepare:
// This is handling the SQL statement "PREPARE". See execPrepare for
// handling of the protocol-level command for preparing statements.
name := s.Name.String()
if _, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[name]; ok {
err := pgerror.Newf(
pgcode.DuplicatePreparedStatement,
"prepared statement %q already exists", name,
)
return makeErrEvent(err)
}
var typeHints tree.PlaceholderTypes
if len(s.Types) > 0 {
if len(s.Types) > stmt.NumPlaceholders {
err := pgerror.Newf(pgcode.Syntax, "too many types provided")
return makeErrEvent(err)
}
typeHints = make(tree.PlaceholderTypes, stmt.NumPlaceholders)
for i, t := range s.Types {
resolved, err := tree.ResolveType(ctx, t, ex.planner.semaCtx.GetTypeResolver())
if err != nil {
return makeErrEvent(err)
}
typeHints[i] = resolved
}
}
if _, err := ex.addPreparedStmt(
ctx, name,
Statement{
Statement: parser.Statement{
// We need the SQL string just for the part that comes after
// "PREPARE ... AS",
// TODO(radu): it would be nice if the parser would figure out this
// string and store it in tree.Prepare.
SQL: tree.AsStringWithFlags(s.Statement, tree.FmtParsable),
AST: s.Statement,
NumPlaceholders: stmt.NumPlaceholders,
NumAnnotations: stmt.NumAnnotations,
},
},
typeHints,
PreparedStatementOriginSQL,
); err != nil {
return makeErrEvent(err)
}
return nil, nil, nil
case *tree.Execute:
// Replace the `EXECUTE foo` statement with the prepared statement, and
// continue execution below.
name := s.Name.String()
ps, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[name]
if !ok {
err := pgerror.Newf(
pgcode.InvalidSQLStatementName,
"prepared statement %q does not exist", name,
)
return makeErrEvent(err)
}
var err error
pinfo, err = fillInPlaceholders(ctx, ps, name, s.Params, ex.sessionData.SearchPath)
if err != nil {
return makeErrEvent(err)
}
stmt.Statement = ps.Statement
stmt.Prepared = ps
stmt.ExpectedTypes = ps.Columns
stmt.AnonymizedStr = ps.AnonymizedStr
res.ResetStmtType(ps.AST)
if s.DiscardRows {
p.discardRows = true
}
}
p.semaCtx.Annotations = tree.MakeAnnotations(stmt.NumAnnotations)
// For regular statements (the ones that get to this point), we
// don't return any event unless an error happens.
if os.ImplicitTxn.Get() {
asOfTs, err := p.isAsOf(ctx, stmt.AST)
if err != nil {
return makeErrEvent(err)
}
if asOfTs != nil {
p.semaCtx.AsOfTimestamp = asOfTs
p.extendedEvalCtx.SetTxnTimestamp(asOfTs.GoTime())
ex.state.setHistoricalTimestamp(ctx, *asOfTs)
}
} else {
// If we're in an explicit txn, we allow AOST but only if it matches with
// the transaction's timestamp. This is useful for running AOST statements
// using the InternalExecutor inside an external transaction; one might want
// to do that to force p.avoidCachedDescriptors to be set below.
ts, err := p.isAsOf(ctx, stmt.AST)
if err != nil {
return makeErrEvent(err)
}
if ts != nil {
if readTs := ex.state.getReadTimestamp(); *ts != readTs {
err = pgerror.Newf(pgcode.Syntax,
"inconsistent AS OF SYSTEM TIME timestamp; expected: %s", readTs)
err = errors.WithHint(err, "try SET TRANSACTION AS OF SYSTEM TIME")
return makeErrEvent(err)
}
p.semaCtx.AsOfTimestamp = ts
}
}
// The first order of business is to ensure proper sequencing
// semantics. As per PostgreSQL's dialect specs, the "read" part of
// statements always see the data as per a snapshot of the database
// taken the instant the statement begins to run. In particular a
// mutation does not see its own writes. If a query contains
// multiple mutations using CTEs (WITH) or a read part following a
// mutation, all still operate on the same read snapshot.
//
// (To communicate data between CTEs and a main query, the result
// set / RETURNING can be used instead. However this is not relevant
// here.)
// We first ensure stepping mode is enabled.
//
// This ought to be done just once when a txn gets initialized;
// unfortunately, there are too many places where the txn object
// is re-configured, re-set etc without using NewTxnWithSteppingEnabled().
//
// Manually hunting them down and calling ConfigureStepping() each
// time would be error prone (and increase the chance that a future
// change would forget to add the call).
//
// TODO(andrei): really the code should be rearchitected to ensure
// that all uses of SQL execution initialize the client.Txn using a
// single/common function. That would be where the stepping mode
// gets enabled once for all SQL statements executed "underneath".
prevSteppingMode := ex.state.mu.txn.ConfigureStepping(ctx, kv.SteppingEnabled)
defer func() { _ = ex.state.mu.txn.ConfigureStepping(ctx, prevSteppingMode) }()
// Then we create a sequencing point.
//
// This is not the only place where a sequencing point is
// placed. There are also sequencing point after every stage of
// constraint checks and cascading actions at the _end_ of a
// statement's execution.
//
// TODO(knz): At the time of this writing CockroachDB performs
// cascading actions and the corresponding FK existence checks
// interleaved with mutations. This is incorrect; the correct
// behavior, as described in issue
// https://github.com/cockroachdb/cockroach/issues/33475, is to
// execute cascading actions no earlier than after all the "main
// effects" of the current statement (including all its CTEs) have
// completed. There should be a sequence point between the end of
// the main execution and the start of the cascading actions, as
// well as in-between very stage of cascading actions.
// This TODO can be removed when the cascading code is reorganized
// accordingly and the missing call to Step() is introduced.
if err := ex.state.mu.txn.Step(ctx); err != nil {
return makeErrEvent(err)
}
if err := p.semaCtx.Placeholders.Assign(pinfo, stmt.NumPlaceholders); err != nil {
return makeErrEvent(err)
}
p.extendedEvalCtx.Placeholders = &p.semaCtx.Placeholders
p.extendedEvalCtx.Annotations = &p.semaCtx.Annotations
p.stmt = &stmt
p.cancelChecker = cancelchecker.NewCancelChecker(ctx)
p.autoCommit = os.ImplicitTxn.Get() && !ex.server.cfg.TestingKnobs.DisableAutoCommit
if err := ex.dispatchToExecutionEngine(ctx, p, res); err != nil {
return nil, nil, err
}
if err := res.Err(); err != nil {
return makeErrEvent(err)
}
txn := ex.state.mu.txn
if !os.ImplicitTxn.Get() && txn.IsSerializablePushAndRefreshNotPossible() {
rc, canAutoRetry := ex.getRewindTxnCapability()
if canAutoRetry {
ev := eventRetriableErr{
IsCommit: fsm.FromBool(isCommit(stmt.AST)),
CanAutoRetry: fsm.FromBool(canAutoRetry),
}
txn.ManualRestart(ctx, ex.server.cfg.Clock.Now())
payload := eventRetriableErrPayload{
err: roachpb.NewTransactionRetryWithProtoRefreshError(
"serializable transaction timestamp pushed (detected by connExecutor)",
txn.ID(),
// No updated transaction required; we've already manually updated our
// client.Txn.
roachpb.Transaction{},
),
rewCap: rc,
}
return ev, payload, nil
}
}
// No event was generated.
return nil, nil, nil
}
func (ex *connExecutor) checkDescriptorTwoVersionInvariant(ctx context.Context) error {
var inRetryBackoff func()
if knobs := ex.server.cfg.SchemaChangerTestingKnobs; knobs != nil {
inRetryBackoff = knobs.TwoVersionLeaseViolation
}
retryErr, err := descs.CheckTwoVersionInvariant(
ctx,
ex.server.cfg.Clock,
ex.server.cfg.InternalExecutor,
&ex.extraTxnState.descCollection,
ex.state.mu.txn,
inRetryBackoff,
)
if retryErr {
// Create a new transaction to retry with a higher timestamp than the
// timestamps used in the retry loop above.
userPriority := ex.state.mu.txn.UserPriority()
ex.state.mu.txn = kv.NewTxnWithSteppingEnabled(ctx, ex.transitionCtx.db, ex.transitionCtx.nodeIDOrZero)
if err := ex.state.mu.txn.SetUserPriority(userPriority); err != nil {
return err
}
}
return err
}
// commitSQLTransaction executes a commit after the execution of a
// stmt, which can be any statement when executing a statement with an
// implicit transaction, or a COMMIT statement when using an explicit
// transaction.
func (ex *connExecutor) commitSQLTransaction(
ctx context.Context, stmt tree.Statement,
) (fsm.Event, fsm.EventPayload) {
ex.phaseTimes[sessionStartTransactionCommit] = timeutil.Now()
err := ex.commitSQLTransactionInternal(ctx, stmt)
if err != nil {
return ex.makeErrEvent(err, stmt)
}
ex.phaseTimes[sessionEndTransactionCommit] = timeutil.Now()
return eventTxnFinish{}, eventTxnFinishPayload{commit: true}
}
func (ex *connExecutor) commitSQLTransactionInternal(
ctx context.Context, stmt tree.Statement,
) error {
if err := validatePrimaryKeys(&ex.extraTxnState.descCollection); err != nil {
return err
}
if err := ex.checkDescriptorTwoVersionInvariant(ctx); err != nil {
return err
}
if err := ex.state.mu.txn.Commit(ctx); err != nil {
return err
}
// Now that we've committed, if we modified any descriptor we need to make sure
// to release the leases for them so that the schema change can proceed and
// we don't block the client.
if descs := ex.extraTxnState.descCollection.GetDescriptorsWithNewVersion(); descs != nil {
ex.extraTxnState.descCollection.ReleaseLeases(ctx)
}
return nil
}
// validatePrimaryKeys verifies that all tables modified in the transaction have
// an enabled primary key after potentially undergoing DROP PRIMARY KEY, which
// is required to be followed by ADD PRIMARY KEY.
func validatePrimaryKeys(tc *descs.Collection) error {
tables := tc.GetUncommittedTables()
for _, table := range tables {
if !table.HasPrimaryKey() {
return unimplemented.NewWithIssuef(48026,
"primary key of table %s dropped without subsequent addition of new primary key",
table.Name,
)
}
}
return nil
}
// rollbackSQLTransaction executes a ROLLBACK statement: the KV transaction is
// rolled-back and an event is produced.
func (ex *connExecutor) rollbackSQLTransaction(ctx context.Context) (fsm.Event, fsm.EventPayload) {
if err := ex.state.mu.txn.Rollback(ctx); err != nil {
log.Warningf(ctx, "txn rollback failed: %s", err)
}
// We're done with this txn.
return eventTxnFinish{}, eventTxnFinishPayload{commit: false}
}
// dispatchToExecutionEngine executes the statement, writes the result to res
// and returns an event for the connection's state machine.
//
// If an error is returned, the connection needs to stop processing queries.
// Query execution errors are written to res; they are not returned; it is
// expected that the caller will inspect res and react to query errors by
// producing an appropriate state machine event.
func (ex *connExecutor) dispatchToExecutionEngine(
ctx context.Context, planner *planner, res RestrictedCommandResult,
) error {
stmt := planner.stmt
ex.sessionTracing.TracePlanStart(ctx, stmt.AST.StatementTag())
ex.statsCollector.phaseTimes[plannerStartLogicalPlan] = timeutil.Now()
// Prepare the plan. Note, the error is processed below. Everything
// between here and there needs to happen even if there's an error.
err := ex.makeExecPlan(ctx, planner)
// We'll be closing the plan manually below after execution; this
// defer is a catch-all in case some other return path is taken.
defer planner.curPlan.close(ctx)
if planner.autoCommit {
planner.curPlan.flags.Set(planFlagImplicitTxn)
}
// Certain statements want their results to go to the client
// directly. Configure this here.
if planner.curPlan.avoidBuffering {
res.DisableBuffering()
}
defer func() {
planner.maybeLogStatement(
ctx,
ex.executorType,
ex.extraTxnState.autoRetryCounter,
res.RowsAffected(),
res.Err(),
ex.statsCollector.phaseTimes[sessionQueryReceived],
)
}()
ex.statsCollector.phaseTimes[plannerEndLogicalPlan] = timeutil.Now()
ex.sessionTracing.TracePlanEnd(ctx, err)
// Finally, process the planning error from above.
if err != nil {
res.SetError(err)
return nil
}
var cols colinfo.ResultColumns
if stmt.AST.StatementType() == tree.Rows {
cols = planner.curPlan.main.planColumns()
}
if err := ex.initStatementResult(ctx, res, stmt, cols); err != nil {
res.SetError(err)
return nil
}
ex.sessionTracing.TracePlanCheckStart(ctx)
distributePlan := getPlanDistribution(
ctx, planner, planner.execCfg.NodeID, ex.sessionData.DistSQLMode, planner.curPlan.main,
)
ex.sessionTracing.TracePlanCheckEnd(ctx, nil, distributePlan.WillDistribute())
if ex.server.cfg.TestingKnobs.BeforeExecute != nil {
ex.server.cfg.TestingKnobs.BeforeExecute(ctx, stmt.String())
}
ex.statsCollector.phaseTimes[plannerStartExecStmt] = timeutil.Now()
ex.mu.Lock()
queryMeta, ok := ex.mu.ActiveQueries[stmt.queryID]
if !ok {
ex.mu.Unlock()
panic(errors.AssertionFailedf("query %d not in registry", stmt.queryID))
}
queryMeta.phase = executing
// TODO(yuzefovich): introduce ternary PlanDistribution into queryMeta.
queryMeta.isDistributed = distributePlan.WillDistribute()
progAtomic := &queryMeta.progressAtomic
ex.mu.Unlock()
// We need to set the "exec done" flag early because
// curPlan.close(), which will need to observe it, may be closed
// during execution (PlanAndRun).
//
// TODO(knz): This is a mis-design. Andrei says "it's OK if
// execution closes the plan" but it transfers responsibility to
// run any "finalizers" on the plan (including plan sampling for
// stats) to the execution engine. That's a lot of responsibility
// to transfer! It would be better if this responsibility remained
// around here.
planner.curPlan.flags.Set(planFlagExecDone)
if !planner.ExecCfg().Codec.ForSystemTenant() {
planner.curPlan.flags.Set(planFlagTenant)
}
switch distributePlan {
case physicalplan.FullyDistributedPlan:
planner.curPlan.flags.Set(planFlagFullyDistributed)
case physicalplan.PartiallyDistributedPlan:
planner.curPlan.flags.Set(planFlagPartiallyDistributed)
default:
planner.curPlan.flags.Set(planFlagNotDistributed)
}
ex.sessionTracing.TraceExecStart(ctx, "distributed")
stats, err := ex.execWithDistSQLEngine(
ctx, planner, stmt.AST.StatementType(), res, distributePlan.WillDistribute(), progAtomic,
)
ex.sessionTracing.TraceExecEnd(ctx, res.Err(), res.RowsAffected())
ex.statsCollector.phaseTimes[plannerEndExecStmt] = timeutil.Now()
// Record the statement summary. This also closes the plan if the
// plan has not been closed earlier.
ex.recordStatementSummary(
ctx, planner,
ex.extraTxnState.autoRetryCounter, res.RowsAffected(), res.Err(), stats,
)
if ex.server.cfg.TestingKnobs.AfterExecute != nil {
ex.server.cfg.TestingKnobs.AfterExecute(ctx, stmt.String(), res.Err())
}
return err
}
// makeExecPlan creates an execution plan and populates planner.curPlan using
// the cost-based optimizer.
func (ex *connExecutor) makeExecPlan(ctx context.Context, planner *planner) error {
savePlanString := planner.collectBundle
planner.curPlan.init(
planner.stmt,
ex.appStats,
savePlanString,
)
if err := planner.makeOptimizerPlan(ctx); err != nil {
log.VEventf(ctx, 1, "optimizer plan failed: %v", err)
return err
}
flags := planner.curPlan.flags
// We don't execute the statement if:
// - plan contains a full table or full index scan.
// - the session setting disallows full table/index scans.
// - the query is not an internal query.
if (flags.IsSet(planFlagContainsFullIndexScan) || flags.IsSet(planFlagContainsFullTableScan)) &&
planner.EvalContext().SessionData.DisallowFullTableScans && ex.executorType == executorTypeExec {
return errors.WithHint(
pgerror.Newf(pgcode.TooManyRows,
"query `%s` contains a full table/index scan which is explicitly disallowed",
planner.stmt.SQL),
"try overriding the `disallow_full_table_scans` cluster/session setting")
}
// TODO(knz): Remove this accounting if/when savepoint rollbacks
// support rolling back over DDL.
if flags.IsSet(planFlagIsDDL) {
ex.extraTxnState.numDDL++
}
return nil
}
// topLevelQueryStats returns some basic statistics about the run of the query.
type topLevelQueryStats struct {
// bytesRead is the number of bytes read from disk.
bytesRead int64
// rowsRead is the number of rows read from disk.
rowsRead int64
}
// execWithDistSQLEngine converts a plan to a distributed SQL physical plan and
// runs it.
// If an error is returned, the connection needs to stop processing queries.
// Query execution errors are written to res; they are not returned.
func (ex *connExecutor) execWithDistSQLEngine(
ctx context.Context,
planner *planner,
stmtType tree.StatementType,
res RestrictedCommandResult,
distribute bool,
progressAtomic *uint64,
) (topLevelQueryStats, error) {
recv := MakeDistSQLReceiver(
ctx, res, stmtType,
ex.server.cfg.RangeDescriptorCache,
planner.txn,
func(ts hlc.Timestamp) {
ex.server.cfg.Clock.Update(ts)
},
&ex.sessionTracing,
)
recv.progressAtomic = progressAtomic
defer recv.Release()
evalCtx := planner.ExtendedEvalContext()
planCtx := ex.server.cfg.DistSQLPlanner.NewPlanningCtx(ctx, evalCtx, planner, planner.txn, distribute)
planCtx.stmtType = recv.stmtType
if planner.collectBundle {
planCtx.saveDiagram = func(diagram execinfrapb.FlowDiagram) {
planner.curPlan.distSQLDiagrams = append(planner.curPlan.distSQLDiagrams, diagram)
}
}
var evalCtxFactory func() *extendedEvalContext
if len(planner.curPlan.subqueryPlans) != 0 ||
len(planner.curPlan.cascades) != 0 ||
len(planner.curPlan.checkPlans) != 0 {
// The factory reuses the same object because the contexts are not used
// concurrently.
var factoryEvalCtx extendedEvalContext
ex.initEvalCtx(ctx, &factoryEvalCtx, planner)
evalCtxFactory = func() *extendedEvalContext {
ex.resetEvalCtx(&factoryEvalCtx, planner.txn, planner.ExtendedEvalContext().StmtTimestamp)
factoryEvalCtx.Placeholders = &planner.semaCtx.Placeholders
factoryEvalCtx.Annotations = &planner.semaCtx.Annotations
// Query diagnostics can change the Context; make sure we are using the
// same one.
// TODO(radu): consider removing this if/when #46164 is addressed.
factoryEvalCtx.Context = evalCtx.Context
return &factoryEvalCtx
}
}
if len(planner.curPlan.subqueryPlans) != 0 {
if !ex.server.cfg.DistSQLPlanner.PlanAndRunSubqueries(
ctx, planner, evalCtxFactory, planner.curPlan.subqueryPlans, recv, distribute,
) {
return recv.stats, recv.commErr
}
}
recv.discardRows = planner.discardRows
// We pass in whether or not we wanted to distribute this plan, which tells
// the planner whether or not to plan remote table readers.