-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
conn_executor_prepare.go
656 lines (593 loc) · 21.6 KB
/
conn_executor_prepare.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
// 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"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgwirebase"
"github.com/cockroachdb/cockroach/pkg/sql/querycache"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/fsm"
"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/lib/pq/oid"
)
func (ex *connExecutor) execPrepare(
ctx context.Context, parseCmd PrepareStmt,
) (fsm.Event, fsm.EventPayload) {
retErr := func(err error) (fsm.Event, fsm.EventPayload) {
return ex.makeErrEvent(err, parseCmd.AST)
}
// Preparing needs a transaction because it needs to retrieve db/table
// descriptors for type checking. This implicit txn will be open until
// the Sync message is handled.
if _, isNoTxn := ex.machine.CurState().(stateNoTxn); isNoTxn {
return ex.beginImplicitTxn(ctx, parseCmd.AST)
} else if _, isAbortedTxn := ex.machine.CurState().(stateAborted); isAbortedTxn {
if !ex.isAllowedInAbortedTxn(parseCmd.AST) {
return retErr(sqlerrors.NewTransactionAbortedError("" /* customMsg */))
}
}
ctx, sp := tracing.EnsureChildSpan(ctx, ex.server.cfg.AmbientCtx.Tracer, "prepare stmt")
defer sp.Finish()
// The anonymous statement can be overwritten.
if parseCmd.Name != "" {
if _, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[parseCmd.Name]; ok {
err := pgerror.Newf(
pgcode.DuplicatePreparedStatement,
"prepared statement %q already exists", parseCmd.Name,
)
return retErr(err)
}
} else {
// Deallocate the unnamed statement, if it exists.
ex.deletePreparedStmt(ctx, "")
}
stmt := makeStatement(parseCmd.Statement, ex.generateID())
_, err := ex.addPreparedStmt(
ctx,
parseCmd.Name,
stmt,
parseCmd.TypeHints,
parseCmd.RawTypeHints,
PreparedStatementOriginWire,
)
if err != nil {
return retErr(err)
}
return nil, nil
}
// addPreparedStmt creates a new PreparedStatement with the provided name using
// the given query. The new prepared statement is added to the connExecutor and
// also returned. It is illegal to call this when a statement with that name
// already exists (even for anonymous prepared statements).
//
// placeholderHints are used to assist in inferring placeholder types. The
// rawTypeHints are optional and represent OIDs indicated for the placeholders
// coming from the client via the wire protocol.
func (ex *connExecutor) addPreparedStmt(
ctx context.Context,
name string,
stmt Statement,
placeholderHints tree.PlaceholderTypes,
rawTypeHints []oid.Oid,
origin PreparedStatementOrigin,
) (*PreparedStatement, error) {
if _, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[name]; ok {
return nil, pgerror.Newf(
pgcode.DuplicatePreparedStatement,
"prepared statement %q already exists", name,
)
}
// Prepare the query. This completes the typing of placeholders.
prepared, err := ex.prepare(ctx, stmt, placeholderHints, rawTypeHints, origin)
if err != nil {
return nil, err
}
if len(prepared.TypeHints) > pgwirebase.MaxPreparedStatementArgs {
prepared.memAcc.Close(ctx)
return nil, pgwirebase.NewProtocolViolationErrorf(
"more than %d arguments to prepared statement: %d",
pgwirebase.MaxPreparedStatementArgs, len(prepared.TypeHints))
}
if err := prepared.memAcc.Grow(ctx, int64(len(name))); err != nil {
prepared.memAcc.Close(ctx)
return nil, err
}
ex.extraTxnState.prepStmtsNamespace.prepStmts[name] = prepared
// Remember the inferred placeholder types so they can be reported on
// Describe. First, try to preserve the hints sent by the client.
prepared.InferredTypes = make([]oid.Oid, len(prepared.Types))
copy(prepared.InferredTypes, rawTypeHints)
for i, it := range prepared.InferredTypes {
// If the client did not provide an OID type hint, then infer the OID.
if it == 0 || it == oid.T_unknown {
if t, ok := prepared.ValueType(tree.PlaceholderIdx(i)); ok {
prepared.InferredTypes[i] = t.Oid()
}
}
}
return prepared, nil
}
// prepare prepares the given statement.
//
// placeholderHints may contain partial type information for placeholders.
// prepare will populate the missing types. It can be nil.
func (ex *connExecutor) prepare(
ctx context.Context,
stmt Statement,
placeholderHints tree.PlaceholderTypes,
rawTypeHints []oid.Oid,
origin PreparedStatementOrigin,
) (_ *PreparedStatement, retErr error) {
prepared := &PreparedStatement{
memAcc: ex.sessionMon.MakeBoundAccount(),
refCount: 1,
createdAt: timeutil.Now(),
origin: origin,
}
defer func() {
// Make sure to close the memory account if an error is returned.
if retErr != nil {
prepared.memAcc.Close(ctx)
}
}()
if stmt.AST == nil {
return prepared, nil
}
origNumPlaceholders := stmt.NumPlaceholders
switch stmt.AST.(type) {
case *tree.Prepare:
// Special case: we're preparing a SQL-level PREPARE using the
// wire protocol. There's an ambiguity from the perspective of this code:
// any placeholders that are inside of the statement that we're preparing
// shouldn't be treated as placeholders to the PREPARE statement. So, we
// edit the NumPlaceholders field to be 0 here.
stmt.NumPlaceholders = 0
}
var flags planFlags
prepare := func(ctx context.Context, txn *kv.Txn) (err error) {
p := &ex.planner
if origin == PreparedStatementOriginWire {
// If the PREPARE command was issued as a SQL statement or through
// deserialize_session, then we have already reset the planner at the very
// beginning of the execution (in execStmtInOpenState). We might have also
// instrumented the planner to collect execution statistics, and resetting
// the planner here would break the assumptions of the instrumentation.
ex.statsCollector.Reset(ex.applicationStats, ex.phaseTimes)
ex.resetPlanner(ctx, p, txn, ex.server.cfg.Clock.PhysicalTime())
}
if placeholderHints == nil {
placeholderHints = make(tree.PlaceholderTypes, stmt.NumPlaceholders)
} else if rawTypeHints != nil {
// If we were provided any type hints, attempt to resolve any user defined
// type OIDs into types.T's.
for i := range placeholderHints {
if placeholderHints[i] == nil {
if i >= len(rawTypeHints) {
break
}
if types.IsOIDUserDefinedType(rawTypeHints[i]) {
var err error
placeholderHints[i], err = ex.planner.ResolveTypeByOID(ctx, rawTypeHints[i])
if err != nil {
return err
}
}
}
}
}
prepared.PrepareMetadata = querycache.PrepareMetadata{
PlaceholderTypesInfo: tree.PlaceholderTypesInfo{
TypeHints: placeholderHints,
Types: placeholderHints,
},
}
prepared.Statement = stmt.Statement
// When we set our prepared statement, we need to make sure to propagate
// the original NumPlaceholders if we're preparing a PREPARE.
prepared.Statement.NumPlaceholders = origNumPlaceholders
prepared.StatementNoConstants = stmt.StmtNoConstants
prepared.StatementSummary = stmt.StmtSummary
// Point to the prepared state, which can be further populated during query
// preparation.
stmt.Prepared = prepared
if err := tree.ProcessPlaceholderAnnotations(&ex.planner.semaCtx, stmt.AST, placeholderHints); err != nil {
return err
}
p.stmt = stmt
p.semaCtx.Annotations = tree.MakeAnnotations(stmt.NumAnnotations)
flags, err = ex.populatePrepared(ctx, txn, placeholderHints, p, origin)
return err
}
// Use the existing transaction.
if err := prepare(ctx, ex.state.mu.txn); err != nil && origin != PreparedStatementOriginSessionMigration {
return nil, err
}
// Account for the memory used by this prepared statement.
if err := prepared.memAcc.Grow(ctx, prepared.MemoryEstimate()); err != nil {
return nil, err
}
ex.updateOptCounters(flags)
return prepared, nil
}
// populatePrepared analyzes and type-checks the query and populates
// stmt.Prepared.
func (ex *connExecutor) populatePrepared(
ctx context.Context,
txn *kv.Txn,
placeholderHints tree.PlaceholderTypes,
p *planner,
origin PreparedStatementOrigin,
) (planFlags, error) {
if before := ex.server.cfg.TestingKnobs.BeforePrepare; before != nil {
if err := before(ctx, ex.planner.stmt.String(), txn); err != nil {
return 0, err
}
}
stmt := &p.stmt
if err := p.semaCtx.Placeholders.Init(stmt.NumPlaceholders, placeholderHints); err != nil {
return 0, err
}
p.extendedEvalCtx.PrepareOnly = true
if err := ex.handleAOST(ctx, p.stmt.AST); err != nil {
return 0, err
}
// PREPARE has a limited subset of statements it can be run with. Postgres
// only allows SELECT, INSERT, UPDATE, DELETE and VALUES statements to be
// prepared.
// See: https://www.postgresql.org/docs/current/static/sql-prepare.html
// However, we must be able to handle every type of statement below because
// the Postgres extended protocol requires running statements via the prepare
// and execute paths.
flags, err := p.prepareUsingOptimizer(ctx)
if err != nil {
log.VEventf(ctx, 1, "optimizer prepare failed: %v", err)
return 0, err
}
log.VEvent(ctx, 2, "optimizer prepare succeeded")
// stmt.Prepared fields have been populated.
return flags, nil
}
func (ex *connExecutor) execBind(
ctx context.Context, bindCmd BindStmt,
) (fsm.Event, fsm.EventPayload) {
retErr := func(err error) (fsm.Event, fsm.EventPayload) {
return eventNonRetriableErr{IsCommit: fsm.False}, eventNonRetriableErrPayload{err: err}
}
ps, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[bindCmd.PreparedStatementName]
if !ok {
return retErr(pgerror.Newf(
pgcode.InvalidSQLStatementName,
"unknown prepared statement %q", bindCmd.PreparedStatementName))
}
// We need to make sure type resolution happens within a transaction.
// Otherwise, for user-defined types we won't take the correct leases and
// will get back out of date type information.
// This code path is only used by the wire-level Bind command. The
// SQL EXECUTE command (which also needs to bind and resolve types) is
// handled separately in conn_executor_exec.
if _, isNoTxn := ex.machine.CurState().(stateNoTxn); isNoTxn {
return ex.beginImplicitTxn(ctx, ps.AST)
} else if _, isAbortedTxn := ex.machine.CurState().(stateAborted); isAbortedTxn {
if !ex.isAllowedInAbortedTxn(ps.AST) {
return retErr(sqlerrors.NewTransactionAbortedError("" /* customMsg */))
}
}
portalName := bindCmd.PortalName
// The unnamed portal can be freely overwritten.
if portalName != "" {
if _, ok := ex.extraTxnState.prepStmtsNamespace.portals[portalName]; ok {
return retErr(pgerror.Newf(
pgcode.DuplicateCursor, "portal %q already exists", portalName))
}
if cursor := ex.getCursorAccessor().getCursor(tree.Name(portalName)); cursor != nil {
return retErr(pgerror.Newf(
pgcode.DuplicateCursor, "portal %q already exists as cursor", portalName))
}
} else {
// Deallocate the unnamed portal, if it exists.
ex.deletePortal(ctx, "")
}
numQArgs := uint16(len(ps.InferredTypes))
// Decode the arguments, except for internal queries for which we just verify
// that the arguments match what's expected.
qargs := make(tree.QueryArguments, numQArgs)
if bindCmd.internalArgs != nil {
if len(bindCmd.internalArgs) != int(numQArgs) {
return retErr(
pgwirebase.NewProtocolViolationErrorf(
"expected %d arguments, got %d", numQArgs, len(bindCmd.internalArgs)))
}
for i, datum := range bindCmd.internalArgs {
t := ps.InferredTypes[i]
if oid := datum.ResolvedType().Oid(); datum != tree.DNull && oid != t {
return retErr(
pgwirebase.NewProtocolViolationErrorf(
"for argument %d expected OID %d, got %d", i, t, oid))
}
qargs[i] = datum
}
} else {
qArgFormatCodes := bindCmd.ArgFormatCodes
// If there is only one format code, then that format code is used to decode all the
// arguments. But if the number of format codes provided does not match the number of
// arguments AND it's not a single format code then we cannot infer what format to use to
// decode all of the arguments.
if len(qArgFormatCodes) != 1 && len(qArgFormatCodes) != int(numQArgs) {
return retErr(pgwirebase.NewProtocolViolationErrorf(
"wrong number of format codes specified: %d for %d arguments",
len(qArgFormatCodes), numQArgs))
}
// If a single format code is provided and there is more than one argument to be decoded,
// then expand qArgFormatCodes to the number of arguments provided.
// If the number of format codes matches the number of arguments then nothing needs to be
// done.
if len(qArgFormatCodes) == 1 && numQArgs > 1 {
fmtCode := qArgFormatCodes[0]
qArgFormatCodes = make([]pgwirebase.FormatCode, numQArgs)
for i := range qArgFormatCodes {
qArgFormatCodes[i] = fmtCode
}
}
if len(bindCmd.Args) != int(numQArgs) {
return retErr(
pgwirebase.NewProtocolViolationErrorf(
"expected %d arguments, got %d", numQArgs, len(bindCmd.Args)))
}
resolve := func(ctx context.Context, txn *kv.Txn) (err error) {
ex.statsCollector.Reset(ex.applicationStats, ex.phaseTimes)
p := &ex.planner
ex.resetPlanner(ctx, p, txn, ex.server.cfg.Clock.PhysicalTime() /* stmtTS */)
if err := ex.handleAOST(ctx, ps.AST); err != nil {
return err
}
for i, arg := range bindCmd.Args {
k := tree.PlaceholderIdx(i)
t := ps.InferredTypes[i]
if arg == nil {
// nil indicates a NULL argument value.
qargs[k] = tree.DNull
} else {
typ, ok := types.OidToType[t]
if !ok {
if t == oid.T_json {
// This special case is here so we can support decoding parameters
// with oid=json without adding full support for the JSON type.
// TODO(sql-exp): Remove this if we support JSON.
typ = types.Json
} else {
var err error
typ, err = ex.planner.ResolveTypeByOID(ctx, t)
if err != nil {
return err
}
}
}
d, err := pgwirebase.DecodeDatum(
ctx,
ex.planner.EvalContext(),
typ,
qArgFormatCodes[i],
arg,
)
if err != nil {
return pgerror.Wrapf(err, pgcode.ProtocolViolation, "error in argument for %s", k)
}
qargs[k] = d
}
}
return nil
}
// Use the existing transaction.
if err := resolve(ctx, ex.state.mu.txn); err != nil {
return retErr(err)
}
}
numCols := len(ps.Columns)
if (len(bindCmd.OutFormats) > 1) && (len(bindCmd.OutFormats) != numCols) {
return retErr(pgwirebase.NewProtocolViolationErrorf(
"expected 1 or %d for number of format codes, got %d",
numCols, len(bindCmd.OutFormats)))
}
columnFormatCodes := bindCmd.OutFormats
if len(bindCmd.OutFormats) == 1 && numCols > 1 {
// Apply the format code to every column.
columnFormatCodes = make([]pgwirebase.FormatCode, numCols)
for i := 0; i < numCols; i++ {
columnFormatCodes[i] = bindCmd.OutFormats[0]
}
}
// This is a huge kludge to deal with the fact that we're resolving types
// using a planner with a committed transaction. This ends up being almost
// okay because the execution is going to re-acquire leases on these types.
// Regardless, holding this lease is worse than not holding it. Users might
// expect to get type mismatch errors if a rename of the type occurred.
if ex.getTransactionState() == NoTxnStateStr {
ex.planner.Descriptors().ReleaseAll(ctx)
}
// Create the new PreparedPortal.
if err := ex.addPortal(ctx, portalName, ps, qargs, columnFormatCodes); err != nil {
return retErr(err)
}
if log.V(2) {
log.Infof(ctx, "portal: %q for %q, args %q, formats %q",
portalName, ps.Statement, qargs, columnFormatCodes)
}
return nil, nil
}
// addPortal creates a new PreparedPortal on the connExecutor.
//
// It is illegal to call this when a portal with that name already exists (even
// for anonymous portals).
func (ex *connExecutor) addPortal(
ctx context.Context,
portalName string,
stmt *PreparedStatement,
qargs tree.QueryArguments,
outFormats []pgwirebase.FormatCode,
) error {
if _, ok := ex.extraTxnState.prepStmtsNamespace.portals[portalName]; ok {
panic(errors.AssertionFailedf("portal already exists: %q", portalName))
}
if cursor := ex.getCursorAccessor().getCursor(tree.Name(portalName)); cursor != nil {
panic(errors.AssertionFailedf("portal already exists as cursor: %q", portalName))
}
portal, err := ex.makePreparedPortal(ctx, portalName, stmt, qargs, outFormats)
if err != nil {
return err
}
ex.extraTxnState.prepStmtsNamespace.portals[portalName] = portal
return nil
}
// exhaustPortal marks a portal with the provided name as "exhausted" and
// panics if there is no portal with such name.
func (ex *connExecutor) exhaustPortal(portalName string) {
portal, ok := ex.extraTxnState.prepStmtsNamespace.portals[portalName]
if !ok {
panic(errors.AssertionFailedf("portal %s doesn't exist", portalName))
}
portal.exhausted = true
ex.extraTxnState.prepStmtsNamespace.portals[portalName] = portal
}
func (ex *connExecutor) deletePreparedStmt(ctx context.Context, name string) {
ps, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[name]
if !ok {
return
}
ps.decRef(ctx)
delete(ex.extraTxnState.prepStmtsNamespace.prepStmts, name)
}
func (ex *connExecutor) deletePortal(ctx context.Context, name string) {
portal, ok := ex.extraTxnState.prepStmtsNamespace.portals[name]
if !ok {
return
}
portal.close(ctx, &ex.extraTxnState.prepStmtsNamespaceMemAcc, name)
delete(ex.extraTxnState.prepStmtsNamespace.portals, name)
}
func (ex *connExecutor) execDelPrepStmt(
ctx context.Context, delCmd DeletePreparedStmt,
) (fsm.Event, fsm.EventPayload) {
switch delCmd.Type {
case pgwirebase.PrepareStatement:
_, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[delCmd.Name]
if !ok {
// The spec says "It is not an error to issue Close against a nonexistent
// statement or portal name". See
// https://www.postgresql.org/docs/current/static/protocol-flow.html.
break
}
ex.deletePreparedStmt(ctx, delCmd.Name)
case pgwirebase.PreparePortal:
_, ok := ex.extraTxnState.prepStmtsNamespace.portals[delCmd.Name]
if !ok {
break
}
ex.deletePortal(ctx, delCmd.Name)
default:
panic(errors.AssertionFailedf("unknown del type: %s", delCmd.Type))
}
return nil, nil
}
func (ex *connExecutor) execDescribe(
ctx context.Context, descCmd DescribeStmt, res DescribeResult,
) (fsm.Event, fsm.EventPayload) {
retErr := func(err error) (fsm.Event, fsm.EventPayload) {
return eventNonRetriableErr{IsCommit: fsm.False}, eventNonRetriableErrPayload{err: err}
}
_, isAbortedTxn := ex.machine.CurState().(stateAborted)
switch descCmd.Type {
case pgwirebase.PrepareStatement:
ps, ok := ex.extraTxnState.prepStmtsNamespace.prepStmts[string(descCmd.Name)]
if !ok {
return retErr(pgerror.Newf(
pgcode.InvalidSQLStatementName,
"unknown prepared statement %q", descCmd.Name))
}
ast := ps.AST
if execute, ok := ast.(*tree.Execute); ok {
// If we're describing an EXECUTE, we need to look up the statement type
// of the prepared statement that the EXECUTE refers to, or else we'll
// return the wrong information for describe.
innerPs, found := ex.extraTxnState.prepStmtsNamespace.prepStmts[string(execute.Name)]
if !found {
return retErr(pgerror.Newf(
pgcode.InvalidSQLStatementName,
"unknown prepared statement %q", descCmd.Name))
}
ast = innerPs.AST
}
if isAbortedTxn && !ex.isAllowedInAbortedTxn(ast) {
return retErr(sqlerrors.NewTransactionAbortedError("" /* customMsg */))
}
res.SetInferredTypes(ps.InferredTypes)
if stmtHasNoData(ast) {
res.SetNoDataRowDescription()
} else {
res.SetPrepStmtOutput(ctx, ps.Columns)
}
case pgwirebase.PreparePortal:
// TODO(rimadeodhar): prepStmtsNamespace should also be updated to use tree.Name instead of string
// for indexing internal maps.
portal, ok := ex.extraTxnState.prepStmtsNamespace.portals[string(descCmd.Name)]
if !ok {
// Check SQL-level cursors.
cursor := ex.getCursorAccessor().getCursor(descCmd.Name)
if cursor == nil {
return retErr(pgerror.Newf(
pgcode.InvalidCursorName, "unknown portal %q", descCmd.Name))
}
if isAbortedTxn {
return retErr(sqlerrors.NewTransactionAbortedError("" /* customMsg */))
}
// Sending a nil formatCodes is equivalent to sending all text format
// codes.
res.SetPortalOutput(ctx, cursor.InternalRows.Types(), nil /* formatCodes */)
return nil, nil
}
ast := portal.Stmt.AST
if isAbortedTxn && !ex.isAllowedInAbortedTxn(ast) {
return retErr(sqlerrors.NewTransactionAbortedError("" /* customMsg */))
}
if stmtHasNoData(ast) {
res.SetNoDataRowDescription()
} else {
res.SetPortalOutput(ctx, portal.Stmt.Columns, portal.OutFormats)
}
default:
return retErr(pgerror.Newf(
pgcode.ProtocolViolation,
"invalid DESCRIBE message subtype %d", errors.Safe(byte(descCmd.Type)),
))
}
return nil, nil
}
// isAllowedInAbortedTxn returns true if the statement is allowed to be
// prepared and executed inside of an aborted transaction.
func (ex *connExecutor) isAllowedInAbortedTxn(ast tree.Statement) bool {
switch s := ast.(type) {
case *tree.CommitTransaction, *tree.RollbackTransaction, *tree.RollbackToSavepoint:
return true
case *tree.Savepoint:
if ex.isCommitOnReleaseSavepoint(s.Name) {
return true
}
return false
default:
return false
}
}