/
prepared_statement.go
70 lines (55 loc) · 2.23 KB
/
prepared_statement.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
// Copyright 2019 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 sqlbase
import (
"time"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/lib/pq/oid"
)
// PrepareMetadata encapsulates information about a statement that is gathered
// during Prepare and is later used during Describe or Execute.
type PrepareMetadata struct {
// Note that AST may be nil if the prepared statement is empty.
parser.Statement
// AnonymizedStr is the anonymized statement string suitable for recording
// in statement statistics.
AnonymizedStr string
// Provides TypeHints and Types fields which contain placeholder typing
// information.
tree.PlaceholderTypesInfo
// Columns are the types and names of the query output columns.
Columns ResultColumns
// InferredTypes represents the inferred types for placeholder, using protocol
// identifiers. Used for reporting on Describe.
InferredTypes []oid.Oid
// CreatedAt is the timestamp this prepare statement was made.
// Used for reporting on `pg_prepared_statements`.
CreatedAt time.Time
}
// MemoryEstimate returns an estimation (in bytes) of how much memory is used by
// the prepare metadata.
func (pm *PrepareMetadata) MemoryEstimate() int64 {
res := int64(unsafe.Sizeof(*pm))
res += int64(len(pm.SQL))
// We don't have a good way of estimating the size of the AST. Just assume
// it's a small multiple of the string length.
res += 2 * int64(len(pm.SQL))
res += int64(len(pm.AnonymizedStr))
res += int64(len(pm.TypeHints)+len(pm.Types)) *
int64(unsafe.Sizeof(tree.PlaceholderIdx(0))+unsafe.Sizeof((*types.T)(nil)))
res += int64(len(pm.Columns)) * int64(unsafe.Sizeof(ResultColumn{}))
res += int64(len(pm.InferredTypes)) * int64(unsafe.Sizeof(oid.Oid(0)))
// This is missing pm.CreatedAt.Location.
// TODO: confirm whether we should add it in.
return res
}