From 558c7be93cc183d64dade8af8a2ccd02a408b404 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Tue, 12 Mar 2019 17:53:59 +0100 Subject: [PATCH] sql: add telemetry for SERIAL expansions This adds telemetry for the SERIAL expansion strategies. Counter format: `sql.schema.serial..` For example: `sql.schema.serial.rowid.SERIAL2` Release note (sql change): CockroachDB will now report how the SERIAL pseudo-type is expanded in table column definitions, when telemetry is enabled. --- pkg/server/updates_test.go | 13 ++++++++++++- pkg/sql/serial.go | 4 ++++ pkg/sql/sqltelemetry/schema.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 pkg/sql/sqltelemetry/schema.go diff --git a/pkg/server/updates_test.go b/pkg/server/updates_test.go index 9a741483f97d..875e677e3e88 100644 --- a/pkg/server/updates_test.go +++ b/pkg/server/updates_test.go @@ -275,6 +275,12 @@ func TestReportUsage(t *testing.T) { t.Fatal(err) } + if _, err := db.Exec( + fmt.Sprintf(`CREATE TABLE %[1]s.%[1]s_s (%[1]s SERIAL2)`, elemName), + ); err != nil { + t.Fatal(err) + } + // Run some queries so we have some query statistics collected. for i := 0; i < 10; i++ { // Run some sample queries. Each are passed a string and int by Exec. @@ -388,7 +394,7 @@ func TestReportUsage(t *testing.T) { if err != nil { t.Fatal(err) } - if actual := len(tables); actual != 1 { + if actual := len(tables); actual != 2 { t.Fatalf("unexpected table count %d", actual) } for _, table := range tables { @@ -543,6 +549,9 @@ func TestReportUsage(t *testing.T) { "test.b": 2, "test.c": 3, + // SERIAL normalization. + "sql.schema.serial.rowid.SERIAL2": 1, + // Although the query is executed 10 times, due to plan caching // keyed by the SQL text, the planning only occurs once. "sql.plan.ops.cast.string::inet": 1, @@ -693,6 +702,7 @@ func TestReportUsage(t *testing.T) { `[false,false,false] SET application_name = $1`, `[false,false,false] SET application_name = DEFAULT`, `[false,false,false] SET application_name = _`, + `[true,false,false] CREATE TABLE _ (_ INT8 NOT NULL DEFAULT unique_rowid())`, `[true,false,false] CREATE TABLE _ (_ INT8, CONSTRAINT _ CHECK (_ > _))`, `[true,false,false] INSERT INTO _ SELECT unnest(ARRAY[_, _, __more2__])`, `[true,false,false] INSERT INTO _ VALUES (_), (__more2__)`, @@ -743,6 +753,7 @@ func TestReportUsage(t *testing.T) { `ALTER TABLE _ CONFIGURE ZONE = _`, `CREATE DATABASE _`, `CREATE TABLE _ (_ INT8, CONSTRAINT _ CHECK (_ > _))`, + `CREATE TABLE _ (_ INT8 NOT NULL DEFAULT unique_rowid())`, `CREATE TABLE _ (_ INT8 PRIMARY KEY, _ INT8, INDEX (_) INTERLEAVE IN PARENT _ (_))`, `INSERT INTO _ VALUES (length($1::STRING)), (__more1__)`, `INSERT INTO _ VALUES (_), (__more2__)`, diff --git a/pkg/sql/serial.go b/pkg/sql/serial.go index e96696335334..25f33f7eea95 100644 --- a/pkg/sql/serial.go +++ b/pkg/sql/serial.go @@ -18,10 +18,12 @@ import ( "context" "fmt" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/coltypes" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/log" ) @@ -83,6 +85,8 @@ func (p *planner) processSerialInColumnDef( newSpec.Type = t.TInt } + telemetry.Inc(sqltelemetry.SerialColumnNormalizationCounter(t.String(), serialNormalizationMode.String())) + if serialNormalizationMode == sessiondata.SerialUsesRowID { // We're not constructing a sequence for this SERIAL column. // Use the "old school" CockroachDB default. diff --git a/pkg/sql/sqltelemetry/schema.go b/pkg/sql/sqltelemetry/schema.go new file mode 100644 index 000000000000..71872bd3343f --- /dev/null +++ b/pkg/sql/sqltelemetry/schema.go @@ -0,0 +1,29 @@ +// Copyright 2019 The Cockroach Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. See the License for the specific language governing +// permissions and limitations under the License. + +package sqltelemetry + +import ( + "fmt" + + "github.com/cockroachdb/cockroach/pkg/server/telemetry" +) + +// SerialColumnNormalizationCounter is to be incremented every time +// a SERIAL type is processed in a column definition. +// It includes the normalization type, so we can +// estimate usage of the various normalization strategies. +func SerialColumnNormalizationCounter(inputType, normType string) telemetry.Counter { + return telemetry.GetCounter(fmt.Sprintf("sql.schema.serial.%s.%s", normType, inputType)) +}