From 82b5926b8f1d92169cdc8d8f20662d99da11590c Mon Sep 17 00:00:00 2001 From: Michael Erickson Date: Wed, 18 May 2022 14:42:29 -0700 Subject: [PATCH 1/3] sql/stats: store non-NULL histograms for empty tables We have been storing NULL histograms / using nil HistogramData for all the following cases: 1. regular stats, GenerateHistogram=false 2. regular stats, GenerateHistogram=true, empty table 3. regular stats, GenerateHistogram=true, all NULL values 4. inverted stats, no inverted index 5. inverted stats, yes inverted index, empty table 6. inverted stats, yes inverted index, all NULL values When predicting histograms for statistics forecasts, we need to distinguish between case 1 and cases 2 and 3. In case 1 we cannot predict histograms, but in cases 2 and 3 we can (and the emptiness of the histogram is important). So, for cases 2 and 3 we now store an empty histogram instead of NULL, and correspondingly use an initialized HistogramData with 0-length Buckets instead of nil HistogramData. This also helps with testing statistics forecasts. (The inability to distinguish cases 4-6 doesn't matter, because we cannot predict histograms for inverted indexes anyway. I tried to change cases 5 and 6 to be non-NULL for consistency but ran into some problems, so I'll leave them as they are.) Release note: None --- .../testdata/logic_test/distsql_stats | 194 ++++++++++++++++++ pkg/sql/rowexec/sample_aggregator.go | 2 +- pkg/sql/stats/histogram.go | 4 +- pkg/sql/stats/histogram_test.go | 6 + pkg/sql/stats/json.go | 2 +- 5 files changed, 205 insertions(+), 3 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/distsql_stats b/pkg/sql/logictest/testdata/logic_test/distsql_stats index db0f790d3039..3b622364ead1 100644 --- a/pkg/sql/logictest/testdata/logic_test/distsql_stats +++ b/pkg/sql/logictest/testdata/logic_test/distsql_stats @@ -1219,6 +1219,7 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE all_null] ], "distinct_count": 1, "histo_col_type": "INT8", + "histo_version": 1, "name": "s", "null_count": 1, "row_count": 1 @@ -1375,3 +1376,196 @@ ANALYZE system.jobs # Collecting stats on system.scheduled_jobs is disallowed. statement error pq: cannot create statistics on system.scheduled_jobs ANALYZE system.scheduled_jobs + +# Collecting stats on empty tables should result in empty (but not NULL) +# histograms. +statement ok +CREATE TABLE tabula (r INT, a INT, sa INT, PRIMARY KEY (r), INDEX (a, sa)) + +statement ok +CREATE STATISTICS aristotle FROM tabula + +query TTIB colnames +SELECT statistics_name, column_names, row_count, histogram_id IS NOT NULL AS has_histogram +FROM [SHOW STATISTICS FOR TABLE tabula] +ORDER BY statistics_name, column_names::STRING +---- +statistics_name column_names row_count has_histogram +aristotle {a,sa} 0 false +aristotle {a} 0 true +aristotle {r} 0 true +aristotle {sa} 0 true + +let $hist_id_1 +SELECT histogram_id FROM [SHOW STATISTICS FOR TABLE tabula] +WHERE statistics_name = 'aristotle' AND column_names = '{a}' + +# This histogram should be empty. +query TIRI colnames +SHOW HISTOGRAM $hist_id_1 +---- +upper_bound range_rows distinct_range_rows equal_rows + +query T +SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]')) + FROM (SELECT json_array_elements(statistics) - 'created_at' AS stat + FROM [SHOW STATISTICS USING JSON FOR TABLE tabula]) +---- +[ + { + "avg_size": 0, + "columns": [ + "r" + ], + "distinct_count": 0, + "histo_col_type": "INT8", + "histo_version": 1, + "name": "aristotle", + "null_count": 0, + "row_count": 0 + }, + { + "avg_size": 0, + "columns": [ + "a" + ], + "distinct_count": 0, + "histo_col_type": "INT8", + "histo_version": 1, + "name": "aristotle", + "null_count": 0, + "row_count": 0 + }, + { + "avg_size": 0, + "columns": [ + "sa" + ], + "distinct_count": 0, + "histo_col_type": "INT8", + "histo_version": 1, + "name": "aristotle", + "null_count": 0, + "row_count": 0 + }, + { + "avg_size": 0, + "columns": [ + "a", + "sa" + ], + "distinct_count": 0, + "histo_col_type": "", + "name": "aristotle", + "null_count": 0, + "row_count": 0 + } +] + +# Collecting stats on columns with all NULL values should also result in empty +# (but not NULL) histograms. +statement ok +INSERT INTO tabula VALUES (11, 12, NULL) + +statement ok +CREATE STATISTICS locke FROM tabula + +query TTIIB colnames +SELECT statistics_name, column_names, row_count, null_count, histogram_id IS NOT NULL AS has_histogram +FROM [SHOW STATISTICS FOR TABLE tabula] +ORDER BY statistics_name, column_names::STRING +---- +statistics_name column_names row_count null_count has_histogram +locke {a,sa} 1 0 false +locke {a} 1 0 true +locke {r} 1 0 true +locke {sa} 1 1 true + +let $hist_id_1 +SELECT histogram_id FROM [SHOW STATISTICS FOR TABLE tabula] +WHERE statistics_name = 'locke' AND column_names = '{a}' + +# This histogram should *not* be empty. +query TIRI colnames +SHOW HISTOGRAM $hist_id_1 +---- +upper_bound range_rows distinct_range_rows equal_rows +12 0 0 1 + +let $hist_id_1 +SELECT histogram_id FROM [SHOW STATISTICS FOR TABLE tabula] +WHERE statistics_name = 'locke' AND column_names = '{sa}' + +# This histogram *should* be empty. +query TIRI colnames +SHOW HISTOGRAM $hist_id_1 +---- +upper_bound range_rows distinct_range_rows equal_rows + +query T +SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]')) + FROM (SELECT json_array_elements(statistics) - 'created_at' - 'avg_size' AS stat + FROM [SHOW STATISTICS USING JSON FOR TABLE tabula]) +---- +[ + { + "columns": [ + "r" + ], + "distinct_count": 1, + "histo_buckets": [ + { + "distinct_range": 0, + "num_eq": 1, + "num_range": 0, + "upper_bound": "11" + } + ], + "histo_col_type": "INT8", + "histo_version": 1, + "name": "locke", + "null_count": 0, + "row_count": 1 + }, + { + "columns": [ + "a" + ], + "distinct_count": 1, + "histo_buckets": [ + { + "distinct_range": 0, + "num_eq": 1, + "num_range": 0, + "upper_bound": "12" + } + ], + "histo_col_type": "INT8", + "histo_version": 1, + "name": "locke", + "null_count": 0, + "row_count": 1 + }, + { + "columns": [ + "sa" + ], + "distinct_count": 1, + "histo_col_type": "INT8", + "histo_version": 1, + "name": "locke", + "null_count": 1, + "row_count": 1 + }, + { + "columns": [ + "a", + "sa" + ], + "distinct_count": 1, + "histo_col_type": "", + "name": "locke", + "null_count": 0, + "row_count": 1 + } +] diff --git a/pkg/sql/rowexec/sample_aggregator.go b/pkg/sql/rowexec/sample_aggregator.go index 2a69dd0cd530..035b4d03da16 100644 --- a/pkg/sql/rowexec/sample_aggregator.go +++ b/pkg/sql/rowexec/sample_aggregator.go @@ -436,7 +436,7 @@ func (s *sampleAggregator) writeResults(ctx context.Context) error { if err := s.FlowCtx.Cfg.DB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { for _, si := range s.sketches { var histogram *stats.HistogramData - if si.spec.GenerateHistogram && len(s.sr.Get()) != 0 { + if si.spec.GenerateHistogram { colIdx := int(si.spec.Columns[0]) typ := s.inTypes[colIdx] diff --git a/pkg/sql/stats/histogram.go b/pkg/sql/stats/histogram.go index 0da70ed8a36b..e5156c00f7ca 100644 --- a/pkg/sql/stats/histogram.go +++ b/pkg/sql/stats/histogram.go @@ -87,7 +87,9 @@ func EquiDepthHistogram( ) (HistogramData, []cat.HistogramBucket, error) { numSamples := len(samples) if numSamples == 0 { - return HistogramData{ColumnType: colType}, nil, nil + return HistogramData{ + ColumnType: colType, Buckets: make([]HistogramData_Bucket, 0), Version: histVersion, + }, nil, nil } if maxBuckets < 2 { return HistogramData{}, nil, errors.Errorf("histogram requires at least two buckets") diff --git a/pkg/sql/stats/histogram_test.go b/pkg/sql/stats/histogram_test.go index 186639846560..c40ec5f8551b 100644 --- a/pkg/sql/stats/histogram_test.go +++ b/pkg/sql/stats/histogram_test.go @@ -245,6 +245,12 @@ func TestEquiDepthHistogram(t *testing.T) { if err != nil { t.Fatal(err) } + if h.Version != histVersion { + t.Errorf("Invalid histogram version %d expected %d", h.Version, histVersion) + } + if (h.Buckets == nil) != (tc.buckets == nil) { + t.Fatalf("Invalid bucket == nil: %v, expected %v", h.Buckets == nil, tc.buckets == nil) + } if len(h.Buckets) != len(tc.buckets) { t.Fatalf("Invalid number of buckets %d, expected %d", len(h.Buckets), len(tc.buckets)) } diff --git a/pkg/sql/stats/json.go b/pkg/sql/stats/json.go index c1bd214802e7..4ae382b505ee 100644 --- a/pkg/sql/stats/json.go +++ b/pkg/sql/stats/json.go @@ -130,7 +130,7 @@ func (js *JSONStatistic) DecodeAndSetHistogram( func (js *JSONStatistic) GetHistogram( semaCtx *tree.SemaContext, evalCtx *eval.Context, ) (*HistogramData, error) { - if len(js.HistogramBuckets) == 0 { + if js.HistogramColumnType == "" { return nil, nil } h := &HistogramData{} From 41c642a60524fd3a8c713a4ed1048ad1e53df2ab Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Wed, 25 May 2022 09:20:25 -0400 Subject: [PATCH 2/3] vendor: bump Pebble to bb2c1501ac23 bb2c1501 tool/logs: print total flush / ingestion counts and durations d1cf3412 tool/logs: sort output by (start, node, store) 0a77780a tool/logs: fix total time calculation 0011c057 db: fix data race in SeekPrefixGE 99f35c80 internal/keyspan: gate invariant checks behind build tag 00302bfc internal/keyspan: use pointer receiver for most Span methods 3355a02e internal/rangekey: reduce range-key iteration allocations 4e33626f db: fix bug in interaction between Clone and indexed batches b14ad704 metamorphic: enable writer parallelism for randomzied metamorphic runs 9bd43143 db: reduce allocations in range key iteration e32e94d8 db: standardize behavior of indexed batch mutations during iteration a07efd96 internal/keyspan: lift visibility filtering out of Seek{GE,LE} Release note: none --- DEPS.bzl | 6 +++--- build/bazelutil/distdir_files.bzl | 2 +- go.mod | 2 +- go.sum | 4 ++-- vendor | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/DEPS.bzl b/DEPS.bzl index 4e7378300d53..a77f7281e63e 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -1347,10 +1347,10 @@ def go_deps(): patches = [ "@com_github_cockroachdb_cockroach//build/patches:com_github_cockroachdb_pebble.patch", ], - sha256 = "65c359674e777445a63c2268e62d8fc740992c1aa86f042a07344371ba01e46b", - strip_prefix = "github.com/cockroachdb/pebble@v0.0.0-20220517003944-e567fec84c6e", + sha256 = "d9f3208c0bb38558acffd03addd6a057891665dc58c47c07ef0a4c8c6bfbb130", + strip_prefix = "github.com/cockroachdb/pebble@v0.0.0-20220523221036-bb2c1501ac23", urls = [ - "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20220517003944-e567fec84c6e.zip", + "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20220523221036-bb2c1501ac23.zip", ], ) go_repository( diff --git a/build/bazelutil/distdir_files.bzl b/build/bazelutil/distdir_files.bzl index 9f6ac6bd41a6..5e38e3b9080f 100644 --- a/build/bazelutil/distdir_files.bzl +++ b/build/bazelutil/distdir_files.bzl @@ -179,7 +179,7 @@ DISTDIR_FILES = { "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/go-test-teamcity/com_github_cockroachdb_go_test_teamcity-v0.0.0-20191211140407-cff980ad0a55.zip": "bac30148e525b79d004da84d16453ddd2d5cd20528e9187f1d7dac708335674b", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/gostdlib/com_github_cockroachdb_gostdlib-v1.13.0.zip": "b3d43d8f95edf65f73a5348f29e1159823cac64b148f8d3bb48340bf55d70872", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/logtags/com_github_cockroachdb_logtags-v0.0.0-20211118104740-dabe8e521a4f.zip": "1972c3f171f118add3fd9e64bcea6cbb9959a3b7fa0ada308e8a7310813fea74", - "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20220517003944-e567fec84c6e.zip": "65c359674e777445a63c2268e62d8fc740992c1aa86f042a07344371ba01e46b", + "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20220523221036-bb2c1501ac23.zip": "d9f3208c0bb38558acffd03addd6a057891665dc58c47c07ef0a4c8c6bfbb130", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/redact/com_github_cockroachdb_redact-v1.1.3.zip": "7778b1e4485e4f17f35e5e592d87eb99c29e173ac9507801d000ad76dd0c261e", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/returncheck/com_github_cockroachdb_returncheck-v0.0.0-20200612231554-92cdbca611dd.zip": "ce92ba4352deec995b1f2eecf16eba7f5d51f5aa245a1c362dfe24c83d31f82b", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/sentry-go/com_github_cockroachdb_sentry_go-v0.6.1-cockroachdb.2.zip": "fbb2207d02aecfdd411b1357efe1192dbb827959e36b7cab7491731ac55935c9", diff --git a/go.mod b/go.mod index 0c871ec8c977..7243937f59ab 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( github.com/cockroachdb/go-test-teamcity v0.0.0-20191211140407-cff980ad0a55 github.com/cockroachdb/gostdlib v1.13.0 github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f - github.com/cockroachdb/pebble v0.0.0-20220517003944-e567fec84c6e + github.com/cockroachdb/pebble v0.0.0-20220523221036-bb2c1501ac23 github.com/cockroachdb/redact v1.1.3 github.com/cockroachdb/returncheck v0.0.0-20200612231554-92cdbca611dd github.com/cockroachdb/stress v0.0.0-20220310203902-58fb4627376e diff --git a/go.sum b/go.sum index 8be89d6d20ca..a78ca69f3b20 100644 --- a/go.sum +++ b/go.sum @@ -453,8 +453,8 @@ github.com/cockroachdb/gostdlib v1.13.0/go.mod h1:eXX95p9QDrYwJfJ6AgeN9QnRa/lqqi github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI= github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f h1:6jduT9Hfc0njg5jJ1DdKCFPdMBrp/mdZfCpa5h+WM74= github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= -github.com/cockroachdb/pebble v0.0.0-20220517003944-e567fec84c6e h1:PU73bIcAcMerOI+xzYa4f3Grrd4I5cxO2ffT9+OcRt0= -github.com/cockroachdb/pebble v0.0.0-20220517003944-e567fec84c6e/go.mod h1:buxOO9GBtOcq1DiXDpIPYrmxY020K2A8lOrwno5FetU= +github.com/cockroachdb/pebble v0.0.0-20220523221036-bb2c1501ac23 h1:/Pvbuwd61qRxNCIpSIWbx7Oqy1tinfErdetF91DU9gQ= +github.com/cockroachdb/pebble v0.0.0-20220523221036-bb2c1501ac23/go.mod h1:buxOO9GBtOcq1DiXDpIPYrmxY020K2A8lOrwno5FetU= github.com/cockroachdb/redact v1.0.8/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ= github.com/cockroachdb/redact v1.1.3/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= diff --git a/vendor b/vendor index 204a1b7af4da..00535a0caa1b 160000 --- a/vendor +++ b/vendor @@ -1 +1 @@ -Subproject commit 204a1b7af4daf37ede07ade3fe26832ce5ff98e9 +Subproject commit 00535a0caa1b2c2f694a2c2ccf4315e29f3ecb8e From 5d691f1d44b73475876ae4c6ee763c0b9a4ca5b9 Mon Sep 17 00:00:00 2001 From: richardjcai Date: Mon, 23 May 2022 11:35:05 -0400 Subject: [PATCH 3/3] roachtest: delete public schema upgrade roachtest The test is no longer needed as the migration is done on the current version of master (22.2). Release note: None --- pkg/cmd/roachtest/tests/BUILD.bazel | 1 - pkg/cmd/roachtest/tests/registry.go | 1 - .../tests/version_upgrade_public_schema.go | 213 ------------------ 3 files changed, 215 deletions(-) delete mode 100644 pkg/cmd/roachtest/tests/version_upgrade_public_schema.go diff --git a/pkg/cmd/roachtest/tests/BUILD.bazel b/pkg/cmd/roachtest/tests/BUILD.bazel index 7af0f6f26efc..caa8623fa0da 100644 --- a/pkg/cmd/roachtest/tests/BUILD.bazel +++ b/pkg/cmd/roachtest/tests/BUILD.bazel @@ -139,7 +139,6 @@ go_library( "util_load_group.go", "validate_system_schema_after_version_upgrade.go", "version.go", - "version_upgrade_public_schema.go", "versionupgrade.go", "ycsb.go", ], diff --git a/pkg/cmd/roachtest/tests/registry.go b/pkg/cmd/roachtest/tests/registry.go index cda41c2a3b7f..6d42554be0a5 100644 --- a/pkg/cmd/roachtest/tests/registry.go +++ b/pkg/cmd/roachtest/tests/registry.go @@ -126,7 +126,6 @@ func RegisterTests(r registry.Registry) { registerOverload(r) registerMultiTenantUpgrade(r) registerMultiTenantFairness(r) - registerVersionUpgradePublicSchema(r) registerValidateSystemSchemaAfterVersionUpgrade(r) registerIndexBackfill(r) } diff --git a/pkg/cmd/roachtest/tests/version_upgrade_public_schema.go b/pkg/cmd/roachtest/tests/version_upgrade_public_schema.go deleted file mode 100644 index 89e271e0c6d6..000000000000 --- a/pkg/cmd/roachtest/tests/version_upgrade_public_schema.go +++ /dev/null @@ -1,213 +0,0 @@ -// Copyright 2021 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 tests - -import ( - "context" - "fmt" - - "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster" - "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry" - "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/test" - "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" - "github.com/cockroachdb/cockroach/pkg/testutils" - "github.com/cockroachdb/cockroach/pkg/util/version" - "github.com/stretchr/testify/require" -) - -// this test ensures that privileges stay consistent after version upgrades. -func registerVersionUpgradePublicSchema(r registry.Registry) { - r.Add(registry.TestSpec{ - Name: "versionupgrade/publicschema", - Owner: registry.OwnerSQLExperience, - Cluster: r.MakeClusterSpec(3), - Run: func(ctx context.Context, t test.Test, c cluster.Cluster) { - runVersionUpgradePublicSchema(ctx, t, c, *t.BuildVersion()) - }, - }) -} - -const loadNode = 1 - -func runVersionUpgradePublicSchema( - ctx context.Context, t test.Test, c cluster.Cluster, buildVersion version.Version, -) { - predecessorVersion, err := PredecessorVersion(buildVersion) - if err != nil { - t.Fatal(err) - } - - const currentVersion = "" - - steps := []versionStep{ - resetStep(), - uploadAndStart(c.All(), predecessorVersion), - waitForUpgradeStep(c.All()), - - // NB: at this point, cluster and binary version equal predecessorVersion, - // and auto-upgrades are on. - preventAutoUpgradeStep(1), - } - - steps = append( - steps, - createDatabaseStep("test1"), - - tryReparentingDatabase(false, ""), - - // Roll nodes forward. - binaryUpgradeStep(c.Node(3), currentVersion), - binaryUpgradeStep(c.Node(1), currentVersion), - - tryReparentingDatabase(false, ""), - - binaryUpgradeStep(c.Node(2), currentVersion), - - createDatabaseStep("test2"), - - allowAutoUpgradeStep(1), - waitForUpgradeStep(c.All()), - - tryReparentingDatabase(true, "pq: cannot perform ALTER DATABASE CONVERT TO SCHEMA"), - - createDatabaseStep("test3"), - - createTableInDatabasePublicSchema("test1"), - createTableInDatabasePublicSchema("test2"), - createTableInDatabasePublicSchema("test3"), - - insertIntoTable("test1"), - insertIntoTable("test2"), - insertIntoTable("test3"), - - selectFromTable("test1"), - selectFromTable("test2"), - selectFromTable("test3"), - - dropTableInDatabase("test1"), - dropTableInDatabase("test2"), - dropTableInDatabase("test3"), - ) - - newVersionUpgradeTest(c, steps...).run(ctx, t) -} - -func createDatabaseStep(dbName string) versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - conn, err := u.c.ConnE(ctx, t.L(), loadNode) - defer func() { - _ = conn.Close() - }() - require.NoError(t, err) - _, err = conn.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName)) - require.NoError(t, err) - } -} - -func createTableInDatabasePublicSchema(dbName string) versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - conn, err := u.c.ConnE(ctx, t.L(), loadNode) - defer func() { - _ = conn.Close() - }() - require.NoError(t, err) - _, err = conn.Exec(fmt.Sprintf("CREATE TABLE %s.public.t(x INT)", dbName)) - require.NoError(t, err) - } -} - -func dropTableInDatabase(dbName string) versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - conn, err := u.c.ConnE(ctx, t.L(), loadNode) - defer func() { - _ = conn.Close() - }() - require.NoError(t, err) - _, err = conn.Exec(fmt.Sprintf("DROP TABLE %s.public.t", dbName)) - require.NoError(t, err) - } -} - -func insertIntoTable(dbName string) versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - conn, err := u.c.ConnE(ctx, t.L(), loadNode) - defer func() { - _ = conn.Close() - }() - require.NoError(t, err) - _, err = conn.Exec(fmt.Sprintf("INSERT INTO %s.public.t VALUES (0), (1), (2)", dbName)) - require.NoError(t, err) - } -} - -func selectFromTable(dbName string) versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - conn, err := u.c.ConnE(ctx, t.L(), loadNode) - defer func() { - _ = conn.Close() - }() - require.NoError(t, err) - rows, err := conn.Query(fmt.Sprintf("SELECT x FROM %s.public.t ORDER BY x", dbName)) - defer func() { - _ = rows.Close() - }() - require.NoError(t, err) - numRows := 3 - var x int - for i := 0; i < numRows; i++ { - rows.Next() - err := rows.Scan(&x) - require.NoError(t, err) - require.Equal(t, x, i) - } - } -} - -func tryReparentingDatabase(shouldError bool, errRe string) versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - conn, err := u.c.ConnE(ctx, t.L(), loadNode) - defer func() { - _ = conn.Close() - }() - require.NoError(t, err) - _, err = conn.Exec("CREATE DATABASE to_reparent;") - require.NoError(t, err) - _, err = conn.Exec("CREATE DATABASE new_parent") - require.NoError(t, err) - - _, err = conn.Exec("ALTER DATABASE to_reparent CONVERT TO SCHEMA WITH PARENT new_parent;") - - if !shouldError { - require.NoError(t, err) - } else { - if !testutils.IsError(err, errRe) { - t.Fatalf("expected error '%s', got: %s", errRe, pgerror.FullError(err)) - } - - _, err = conn.Exec("DROP DATABASE to_reparent") - require.NoError(t, err) - } - - _, err = conn.Exec("DROP DATABASE new_parent") - require.NoError(t, err) - } -} - -func resetStep() versionStep { - return func(ctx context.Context, t test.Test, u *versionUpgradeTest) { - err := u.c.WipeE(ctx, t.L()) - require.NoError(t, err) - err = u.c.RunE(ctx, u.c.All(), "rm -rf "+t.PerfArtifactsDir()) - require.NoError(t, err) - err = u.c.RunE(ctx, u.c.All(), "rm -rf {store-dir}") - require.NoError(t, err) - } -}