github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sqlbase/prepared_statement.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sqlbase
    12  
    13  import (
    14  	"unsafe"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    19  	"github.com/lib/pq/oid"
    20  )
    21  
    22  // PrepareMetadata encapsulates information about a statement that is gathered
    23  // during Prepare and is later used during Describe or Execute.
    24  type PrepareMetadata struct {
    25  	// Note that AST may be nil if the prepared statement is empty.
    26  	parser.Statement
    27  
    28  	// AnonymizedStr is the anonymized statement string suitable for recording
    29  	// in statement statistics.
    30  	AnonymizedStr string
    31  
    32  	// Provides TypeHints and Types fields which contain placeholder typing
    33  	// information.
    34  	tree.PlaceholderTypesInfo
    35  
    36  	// Columns are the types and names of the query output columns.
    37  	Columns ResultColumns
    38  
    39  	// InferredTypes represents the inferred types for placeholder, using protocol
    40  	// identifiers. Used for reporting on Describe.
    41  	InferredTypes []oid.Oid
    42  }
    43  
    44  // MemoryEstimate returns an estimation (in bytes) of how much memory is used by
    45  // the prepare metadata.
    46  func (pm *PrepareMetadata) MemoryEstimate() int64 {
    47  	res := int64(unsafe.Sizeof(*pm))
    48  	res += int64(len(pm.SQL))
    49  	// We don't have a good way of estimating the size of the AST. Just assume
    50  	// it's a small multiple of the string length.
    51  	res += 2 * int64(len(pm.SQL))
    52  
    53  	res += int64(len(pm.AnonymizedStr))
    54  
    55  	res += int64(len(pm.TypeHints)+len(pm.Types)) *
    56  		int64(unsafe.Sizeof(tree.PlaceholderIdx(0))+unsafe.Sizeof((*types.T)(nil)))
    57  
    58  	res += int64(len(pm.Columns)) * int64(unsafe.Sizeof(ResultColumn{}))
    59  	res += int64(len(pm.InferredTypes)) * int64(unsafe.Sizeof(oid.Oid(0)))
    60  
    61  	return res
    62  }