Skip to content

Commit e1852c0

Browse files
committed
MB-70998 Merge remote-tracking branch 'origin/trinity' into morpheus
Change-Id: I878396dbbb72df89fa7d17f1da49a363cffece49
2 parents bc5cea9 + 53b68a4 commit e1852c0

5 files changed

Lines changed: 45 additions & 5 deletions

File tree

algebra/algebra.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type Statement interface {
6868
/*
6969
Returns the parameter count, for AutoPrepare and other purposes
7070
*/
71-
Params() int
71+
ParamsCount() int
7272

7373
/*
7474
Returns the optimizer hints

algebra/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (this *statementBase) SetParamsCount(params int) {
7676
/*
7777
does it have parameters?
7878
*/
79-
func (this *statementBase) Params() int {
79+
func (this *statementBase) ParamsCount() int {
8080
return this.paramCount
8181
}
8282

algebra/statement_util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2026-Present Couchbase, Inc.
2+
//
3+
// Use of this software is governed by the Business Source License included
4+
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
5+
// in that file, in accordance with the Business Source License, use of this
6+
// software will be governed by the Apache License, Version 2.0, included in
7+
// the file licenses/APL2.txt.
8+
9+
package algebra
10+
11+
/*
12+
* With auto prepare, determine whether a statement can skip being prepared
13+
*/
14+
func CanSkipAutoPrepare(stmt Statement) bool {
15+
switch stmt.(type) {
16+
case *InferKeyspace, *InferExpression, *Explain, *ExplainFunction, *Prepare, *Execute,
17+
*UpdateStatistics,
18+
*CreateIndex, *DropIndex, *BuildIndexes, *AlterIndex, *CreatePrimaryIndex,
19+
*CreateScope, *DropScope,
20+
*CreateCollection, *DropCollection, *FlushCollection,
21+
*GrantRole, *RevokeRole,
22+
*CreateFunction, *DropFunction, *ExecuteFunction,
23+
*StartTransaction, *CommitTransaction, *RollbackTransaction, *Savepoint, *TransactionIsolation,
24+
*CreateSequence, *DropSequence, *AlterSequence:
25+
return true
26+
}
27+
return false
28+
}

prepareds/prepareds.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func AddAutoPreparePlan(stmt algebra.Statement, prepared *plan.Prepared) bool {
440440

441441
// we also don't cache anything that might depend on placeholders
442442
// (you should be using prepared statements for that anyway!)
443-
if stmt.Params() > 0 {
443+
if stmt.ParamsCount() > 0 {
444444
return false
445445
}
446446

@@ -645,7 +645,11 @@ func (prepareds *preparedCache) getPrepared(preparedName string, queryContext st
645645
// without blocking the whole prepared cacheline
646646
// locking will occur at adding time: both requests will insert,
647647
// the last wins
648-
if (!good || prepared.PreparedTime().IsZero()) && !metaCheck {
648+
if metaCheck {
649+
if !good {
650+
prepared = nil
651+
}
652+
} else if !good || prepared.PreparedTime().IsZero() {
649653
prepared, err = reprepare(prepared, nil, phaseTime, log)
650654
if err == nil {
651655
err = AddPrepared(prepared)

server/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ func (this *Server) getPrepared(request Request, context *execution.Context) (*p
18371837
positionalArgs := request.PositionalArgs()
18381838
dsContext := context
18391839
autoExecute := request.AutoExecute() == value.TRUE
1840-
if len(namedArgs) > 0 || len(positionalArgs) > 0 || autoExecute {
1840+
if autoExecute {
18411841
autoPrepare = false
18421842
}
18431843

@@ -1874,6 +1874,14 @@ func (this *Server) getPrepared(request Request, context *execution.Context) (*p
18741874
}
18751875
}
18761876

1877+
if autoPrepare {
1878+
if stmt.ParamsCount() > 0 {
1879+
autoPrepare = false
1880+
} else if algebra.CanSkipAutoPrepare(stmt) {
1881+
autoPrepare = false
1882+
}
1883+
}
1884+
18771885
isPrepare := false
18781886
if _, ok := stmt.(*algebra.Prepare); ok {
18791887
isPrepare = true

0 commit comments

Comments
 (0)