diff --git a/pkg/build/version.txt b/pkg/build/version.txt index d33cd2a53b62..d669b56ae5d5 100644 --- a/pkg/build/version.txt +++ b/pkg/build/version.txt @@ -1 +1 @@ -v23.1.0-alpha.6 +v23.1.0-alpha.7 diff --git a/pkg/ccl/backupccl/testdata/backup-restore/metadata b/pkg/ccl/backupccl/testdata/backup-restore/metadata index ea883b877f57..298ca30b80cf 100644 --- a/pkg/ccl/backupccl/testdata/backup-restore/metadata +++ b/pkg/ccl/backupccl/testdata/backup-restore/metadata @@ -14,6 +14,10 @@ exec-sql SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false; ---- +exec-sql +SET enable_create_stats_using_extremes = true; +---- + exec-sql CREATE DATABASE db1; USE db1; diff --git a/pkg/ccl/multitenantccl/tenantcostclient/BUILD.bazel b/pkg/ccl/multitenantccl/tenantcostclient/BUILD.bazel index 4f27f01e9ff6..017cfd6cff59 100644 --- a/pkg/ccl/multitenantccl/tenantcostclient/BUILD.bazel +++ b/pkg/ccl/multitenantccl/tenantcostclient/BUILD.bazel @@ -71,6 +71,7 @@ go_test( "//pkg/server", "//pkg/settings/cluster", "//pkg/sql", + "//pkg/sql/catalog/systemschema", "//pkg/sql/distsql", "//pkg/sql/execinfra", "//pkg/sql/sqlliveness", diff --git a/pkg/ccl/multitenantccl/tenantcostclient/tenant_side_test.go b/pkg/ccl/multitenantccl/tenantcostclient/tenant_side_test.go index a64d2ff23836..42794ac355f6 100644 --- a/pkg/ccl/multitenantccl/tenantcostclient/tenant_side_test.go +++ b/pkg/ccl/multitenantccl/tenantcostclient/tenant_side_test.go @@ -43,6 +43,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/server" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/distsql" "github.com/cockroachdb/cockroach/pkg/sql/execinfra" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" @@ -963,7 +964,8 @@ func TestSQLLivenessExemption(t *testing.T) { _ = r codec := keys.MakeSQLCodec(tenantID) - key := codec.IndexPrefix(keys.SqllivenessID, 1) + indexID := uint32(systemschema.SqllivenessTable().GetPrimaryIndexID()) + key := codec.IndexPrefix(keys.SqllivenessID, indexID) // livenessValue returns the KV value for the one row in the // system.sqlliveness table. The value contains the session expiration time diff --git a/pkg/server/server_sql.go b/pkg/server/server_sql.go index 8584b76d6c5e..09afa6ed638b 100644 --- a/pkg/server/server_sql.go +++ b/pkg/server/server_sql.go @@ -554,7 +554,7 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { } cfg.sqlLivenessProvider = slprovider.New( cfg.AmbientCtx, - cfg.stopper, cfg.clock, cfg.db, codec, cfg.Settings, sqllivenessKnobs, sessionEventsConsumer, + cfg.stopper, cfg.clock, cfg.db, codec, cfg.Settings, settingsWatcher, sqllivenessKnobs, sessionEventsConsumer, ) cfg.sqlInstanceStorage = instancestorage.NewStorage( diff --git a/pkg/server/settingswatcher/version_guard.go b/pkg/server/settingswatcher/version_guard.go index e8c1460a69ec..8512e5d4de6e 100644 --- a/pkg/server/settingswatcher/version_guard.go +++ b/pkg/server/settingswatcher/version_guard.go @@ -87,3 +87,8 @@ func (s *SettingsWatcher) MakeVersionGuard( func (v *VersionGuard) IsActive(version clusterversion.Key) bool { return v.activeVersion.IsActive(version) } + +// TestMakeVersionGuard initializes a version guard at specific version. +func TestMakeVersionGuard(activeVersion clusterversion.ClusterVersion) VersionGuard { + return VersionGuard{activeVersion: activeVersion} +} diff --git a/pkg/sql/catalog/bootstrap/testdata/testdata b/pkg/sql/catalog/bootstrap/testdata/testdata index 1af08cb97881..a922da30681e 100644 --- a/pkg/sql/catalog/bootstrap/testdata/testdata +++ b/pkg/sql/catalog/bootstrap/testdata/testdata @@ -1,4 +1,4 @@ -system hash=e935d845b4027179deccb63f0590d74687a9f3ed1864c4f5c5e1e5a990f0357e +system hash=5814a2c0654c7aefaf6e732e3b12d13c7eae580ee5771f6e08dbc8f951cbf626 ---- [{"key":"04646573632d696467656e","value":"01c801"} ,{"key":"8b"} @@ -31,7 +31,7 @@ system hash=e935d845b4027179deccb63f0590d74687a9f3ed1864c4f5c5e1e5a990f0357e ,{"key":"8b89ab8a89","value":"030aa60a0a1e73746174656d656e745f646961676e6f73746963735f72657175657374731823200128013a0042370a02696410011a0c08011040180030005014600020002a0e756e697175655f726f7769642829300068007000780080010088010098010042350a09636f6d706c6574656410021a0c08001000180030005010600020002a0566616c73653000680070007800800100880100980100423a0a1573746174656d656e745f66696e6765727072696e7410031a0c08071000180030005019600020003000680070007800800100880100980100423d0a1873746174656d656e745f646961676e6f73746963735f696410041a0c0801104018003000501460002001300068007000780080010088010098010042320a0c7265717565737465645f617410051a0d080910001800300050a00960002000300068007000780080010088010098010042410a156d696e5f657865637574696f6e5f6c6174656e637910061a13080610001800300050a20960006a04080010002001300068007000780080010088010098010042300a0a657870697265735f617410071a0d080910001800300050a009600020013000680070007800800100880100980100423a0a1473616d706c696e675f70726f626162696c69747910081a0d080210401800300050bd05600020013000680070007800800100880100980100480952ea010a077072696d61727910011801220269642a09636f6d706c657465642a1573746174656d656e745f66696e6765727072696e742a1873746174656d656e745f646961676e6f73746963735f69642a0c7265717565737465645f61742a156d696e5f657865637574696f6e5f6c6174656e63792a0a657870697265735f61742a1473616d706c696e675f70726f626162696c697479300140004a10080010001a00200028003000380040005a0070027003700470057006700770087a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e001005ac6010a0d636f6d706c657465645f696478100218002209636f6d706c65746564220269642a1573746174656d656e745f66696e6765727072696e742a156d696e5f657865637574696f6e5f6c6174656e63792a0a657870697265735f61742a1473616d706c696e675f70726f626162696c69747930023001400040004a10080010001a00200028003000380040005a0070037006700770087a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100a201620a3a73616d706c696e675f70726f626162696c697479204245545745454e20302e303a3a3a464c4f41543820414e4420312e303a3a3a464c4f415438121a636865636b5f73616d706c696e675f70726f626162696c69747918002808300038004002b201a4010a077072696d61727910001a0269641a09636f6d706c657465641a1573746174656d656e745f66696e6765727072696e741a1873746174656d656e745f646961676e6f73746963735f69641a0c7265717565737465645f61741a156d696e5f657865637574696f6e5f6c6174656e63791a0a657870697265735f61741a1473616d706c696e675f70726f626162696c697479200120022003200420052006200720082800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89ac8a89","value":"030aa5060a1573746174656d656e745f646961676e6f73746963731824200128013a0042370a02696410011a0c08011040180030005014600020002a0e756e697175655f726f77696428293000680070007800800100880100980100423a0a1573746174656d656e745f66696e6765727072696e7410021a0c08071000180030005019600020003000680070007800800100880100980100422e0a0973746174656d656e7410031a0c0807100018003000501960002000300068007000780080010088010098010042320a0c636f6c6c65637465645f617410041a0d080910001800300050a009600020003000680070007800800100880100980100422b0a05747261636510051a0d081210001800300050da1d60002001300068007000780080010088010098010042430a0d62756e646c655f6368756e6b7310061a1d080f104018003000380150f8075a0c080110401800300050146000600020013000680070007800800100880100980100422a0a056572726f7210071a0c08071000180030005019600020013000680070007800800100880100980100480852b2010a077072696d61727910011801220269642a1573746174656d656e745f66696e6765727072696e742a0973746174656d656e742a0c636f6c6c65637465645f61742a0574726163652a0d62756e646c655f6368756e6b732a056572726f72300140004a10080010001a00200028003000380040005a007002700370047005700670077a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2016c0a077072696d61727910001a0269641a1573746174656d656e745f66696e6765727072696e741a0973746174656d656e741a0c636f6c6c65637465645f61741a0574726163651a0d62756e646c655f6368756e6b731a056572726f7220012002200320042005200620072800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89ad8a89","value":"030ab0090a0e7363686564756c65645f6a6f62731825200128013a0042400a0b7363686564756c655f696410011a0c08011040180030005014600020002a0e756e697175655f726f7769642829300068007000780080010088010098010042320a0d7363686564756c655f6e616d6510021a0c0807100018003000501960002000300068007000780080010088010098010042420a076372656174656410031a0d080910001800300050a009600020002a136e6f7728293a3a3a54494d455354414d50545a3000680070007800800100880100980100422a0a056f776e657210041a0c08071000180030005019600020003000680070007800800100880100980100422e0a086e6578745f72756e10051a0d080910001800300050a00960002001300068007000780080010088010098010042330a0e7363686564756c655f737461746510061a0c0808100018003000501160002001300068007000780080010088010098010042320a0d7363686564756c655f6578707210071a0c0807100018003000501960002001300068007000780080010088010098010042350a107363686564756c655f64657461696c7310081a0c0808100018003000501160002001300068007000780080010088010098010042320a0d6578656375746f725f7479706510091a0c0807100018003000501960002000300068007000780080010088010098010042330a0e657865637574696f6e5f61726773100a1a0c08081000180030005011600020003000680070007800800100880100980100480b52ed010a077072696d61727910011801220b7363686564756c655f69642a0d7363686564756c655f6e616d652a07637265617465642a056f776e65722a086e6578745f72756e2a0e7363686564756c655f73746174652a0d7363686564756c655f657870722a107363686564756c655f64657461696c732a0d6578656375746f725f747970652a0e657865637574696f6e5f61726773300140004a10080010001a00200028003000380040005a0070027003700470057006700770087009700a7a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e001005a660a0c6e6578745f72756e5f6964781002180022086e6578745f72756e3005380140004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201380a05736368656410001a0b7363686564756c655f69641a086e6578745f72756e1a0e7363686564756c655f73746174652001200520062800b201780a056f7468657210011a0d7363686564756c655f6e616d651a07637265617465641a056f776e65721a0d7363686564756c655f657870721a107363686564756c655f64657461696c731a0d6578656375746f725f747970651a0e657865637574696f6e5f61726773200220032004200720082009200a2800b80102c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} -,{"key":"8b89af8a89","value":"030a93030a0b73716c6c6976656e6573731827200128013a00422f0a0a73657373696f6e5f696410011a0c0808100018003000501160002000300068007000780080010088010098010042300a0a65787069726174696f6e10021a0d080310001800300050a40d6000200030006800700078008001008801009801004803526f0a077072696d61727910011801220a73657373696f6e5f69642a0a65787069726174696f6e300140004a10080010001a00200028003000380040005a0070027a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2013c0a1a66616d305f73657373696f6e5f69645f65787069726174696f6e10001a0a73657373696f6e5f69641a0a65787069726174696f6e200120022802b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} +,{"key":"8b89af8a89","value":"030ad3030a0b73716c6c6976656e6573731827200128013a00422f0a0a73657373696f6e5f696410011a0c0808100018003000501160002000300068007000780080010088010098010042300a0a65787069726174696f6e10021a0d080310001800300050a40d60002000300068007000780080010088010098010042300a0b637264625f726567696f6e10031a0c0808100018003000501160002000300068007000780080010088010098010048045280010a077072696d61727910021801220b637264625f726567696f6e220a73657373696f6e5f69642a0a65787069726174696f6e30033001400040004a10080010001a00200028003000380040005a0070027a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201380a077072696d61727910001a0b637264625f726567696f6e1a0a73657373696f6e5f69641a0a65787069726174696f6e2003200120022802b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b08a89","value":"030ac0040a0a6d6967726174696f6e731828200128013a00422a0a056d616a6f7210011a0c08011040180030005014600020003000680070007800800100880100980100422a0a056d696e6f7210021a0c08011040180030005014600020003000680070007800800100880100980100422a0a05706174636810031a0c08011040180030005014600020003000680070007800800100880100980100422d0a08696e7465726e616c10041a0c0801104018003000501460002000300068007000780080010088010098010042320a0c636f6d706c657465645f617410051a0d080910001800300050a00960002000300068007000780080010088010098010048065290010a077072696d6172791001180122056d616a6f7222056d696e6f72220570617463682208696e7465726e616c2a0c636f6d706c657465645f6174300130023003300440004000400040004a10080010001a00200028003000380040005a0070057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201440a077072696d61727910001a056d616a6f721a056d696e6f721a0570617463681a08696e7465726e616c1a0c636f6d706c657465645f6174200120022003200420052805b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b18a89","value":"030aaa030a0b6a6f696e5f746f6b656e731829200128013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422b0a0673656372657410021a0c0808100018003000501160002000300068007000780080010088010098010042300a0a65787069726174696f6e10031a0d080910001800300050a009600020003000680070007800800100880100980100480452710a077072696d61727910011801220269642a067365637265742a0a65787069726174696f6e300140004a10080010001a00200028003000380040005a00700270037a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2012b0a077072696d61727910001a0269641a067365637265741a0a65787069726174696f6e2001200220032800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b28a89","value":"030abe140a1473746174656d656e745f73746174697374696373182a200128013a0042330a0d616767726567617465645f747310011a0d080910001800300050a00960002000300068007000780080010088010098010042330a0e66696e6765727072696e745f696410021a0c08081000180030005011600020003000680070007800800100880100980100423f0a1a7472616e73616374696f6e5f66696e6765727072696e745f696410031a0c08081000180030005011600020003000680070007800800100880100980100422e0a09706c616e5f6861736810041a0c08081000180030005011600020003000680070007800800100880100980100422d0a086170705f6e616d6510051a0c08071000180030005019600020003000680070007800800100880100980100422c0a076e6f64655f696410061a0c0801104018003000501460002000300068007000780080010088010098010042380a0c6167675f696e74657276616c10071a13080610001800300050a20960006a040800100020003000680070007800800100880100980100422e0a086d6574616461746110081a0d081210001800300050da1d60002000300068007000780080010088010098010042300a0a7374617469737469637310091a0d081210001800300050da1d600020003000680070007800800100880100980100422a0a04706c616e100a1a0d081210001800300050da1d600020003000680070007800800100880100980100429c020a68637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f38100b1a0c080110201800300050176000200030015a8c016d6f6428666e76333228637264625f696e7465726e616c2e646174756d735f746f5f627974657328616767726567617465645f74732c206170705f6e616d652c2066696e6765727072696e745f69642c206e6f64655f69642c20706c616e5f686173682c207472616e73616374696f6e5f66696e6765727072696e745f696429292c20383a3a3a494e543829680070007800800100880100980100425f0a15696e6465785f7265636f6d6d656e646174696f6e73100c1a1d080f100018003000380750f1075a0c080710001800300050196000600020002a1241525241595b5d3a3a3a535452494e475b5d3000680070007800800100880100980100426c0a0d696e64657865735f7573616765100d1a0d081210001800300050da1d6000200130005a3728737461746973746963732d3e2773746174697374696373273a3a3a535452494e47292d3e27696e6465786573273a3a3a535452494e47680070007800800101880100980100480e52bd040a077072696d617279100118012268637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f38220d616767726567617465645f7473220e66696e6765727072696e745f6964221a7472616e73616374696f6e5f66696e6765727072696e745f69642209706c616e5f6861736822086170705f6e616d6522076e6f64655f69642a0c6167675f696e74657276616c2a086d657461646174612a0a737461746973746963732a04706c616e2a15696e6465785f7265636f6d6d656e646174696f6e73300b30013002300330043005300640004000400040004000400040004a10080010001a00200028003000380040005a00700770087009700a700c7a0408002000800100880100900104980101a201c70108011268637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381808220d616767726567617465645f747322086170705f6e616d65220e66696e6765727072696e745f696422076e6f64655f69642209706c616e5f68617368221a7472616e73616374696f6e5f66696e6765727072696e745f6964a80100b20100ba0100c00100c80100d00101e001005a9d010a1566696e6765727072696e745f73746174735f69647810021800220e66696e6765727072696e745f6964221a7472616e73616374696f6e5f66696e6765727072696e745f696430023003380b3801380438053806400040004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e001005a7f0a11696e64657865735f75736167655f69647810031800220d696e64657865735f7573616765300d380b38013802380338043805380640004a10080010001a00200028003000380040005a007a0408002000800101880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100d80100e0010060046a210a0b0a0561646d696e102018200a0a0a04726f6f741020182012046e6f64651802800101880103980100a201b9020abc01637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f3820494e2028303a3a3a494e54382c20313a3a3a494e54382c20323a3a3a494e54382c20333a3a3a494e54382c20343a3a3a494e54382c20353a3a3a494e54382c20363a3a3a494e54382c20373a3a3a494e543829126e636865636b5f637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381800280b300038014002b201a9020a077072696d61727910001a68637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381a0d616767726567617465645f74731a0e66696e6765727072696e745f69641a1a7472616e73616374696f6e5f66696e6765727072696e745f69641a09706c616e5f686173681a086170705f6e616d651a076e6f64655f69641a0c6167675f696e74657276616c1a086d657461646174611a0a737461746973746963731a04706c616e1a15696e6465785f7265636f6d6d656e646174696f6e73200b200120022003200420052006200720082009200a200c2800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} @@ -163,7 +163,7 @@ system hash=e935d845b4027179deccb63f0590d74687a9f3ed1864c4f5c5e1e5a990f0357e ,{"key":"c1"} ] -tenant hash=fa086a87fa2ecd8f485fc272ea7d85b08f30906235f83830dc446c098b895edc +tenant hash=a964826de7f4674524ebea61cd09bad65d8b6acbee18178e732dc4d46aa98be0 ---- [{"key":""} ,{"key":"8b89898a89","value":"0312390a0673797374656d10011a250a0d0a0561646d696e1080101880100a0c0a04726f6f7410801018801012046e6f646518022200280140004a00"} @@ -194,7 +194,7 @@ tenant hash=fa086a87fa2ecd8f485fc272ea7d85b08f30906235f83830dc446c098b895edc ,{"key":"8b89ab8a89","value":"030aa60a0a1e73746174656d656e745f646961676e6f73746963735f72657175657374731823200128013a0042370a02696410011a0c08011040180030005014600020002a0e756e697175655f726f7769642829300068007000780080010088010098010042350a09636f6d706c6574656410021a0c08001000180030005010600020002a0566616c73653000680070007800800100880100980100423a0a1573746174656d656e745f66696e6765727072696e7410031a0c08071000180030005019600020003000680070007800800100880100980100423d0a1873746174656d656e745f646961676e6f73746963735f696410041a0c0801104018003000501460002001300068007000780080010088010098010042320a0c7265717565737465645f617410051a0d080910001800300050a00960002000300068007000780080010088010098010042410a156d696e5f657865637574696f6e5f6c6174656e637910061a13080610001800300050a20960006a04080010002001300068007000780080010088010098010042300a0a657870697265735f617410071a0d080910001800300050a009600020013000680070007800800100880100980100423a0a1473616d706c696e675f70726f626162696c69747910081a0d080210401800300050bd05600020013000680070007800800100880100980100480952ea010a077072696d61727910011801220269642a09636f6d706c657465642a1573746174656d656e745f66696e6765727072696e742a1873746174656d656e745f646961676e6f73746963735f69642a0c7265717565737465645f61742a156d696e5f657865637574696f6e5f6c6174656e63792a0a657870697265735f61742a1473616d706c696e675f70726f626162696c697479300140004a10080010001a00200028003000380040005a0070027003700470057006700770087a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e001005ac6010a0d636f6d706c657465645f696478100218002209636f6d706c65746564220269642a1573746174656d656e745f66696e6765727072696e742a156d696e5f657865637574696f6e5f6c6174656e63792a0a657870697265735f61742a1473616d706c696e675f70726f626162696c69747930023001400040004a10080010001a00200028003000380040005a0070037006700770087a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100a201620a3a73616d706c696e675f70726f626162696c697479204245545745454e20302e303a3a3a464c4f41543820414e4420312e303a3a3a464c4f415438121a636865636b5f73616d706c696e675f70726f626162696c69747918002808300038004002b201a4010a077072696d61727910001a0269641a09636f6d706c657465641a1573746174656d656e745f66696e6765727072696e741a1873746174656d656e745f646961676e6f73746963735f69641a0c7265717565737465645f61741a156d696e5f657865637574696f6e5f6c6174656e63791a0a657870697265735f61741a1473616d706c696e675f70726f626162696c697479200120022003200420052006200720082800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89ac8a89","value":"030aa5060a1573746174656d656e745f646961676e6f73746963731824200128013a0042370a02696410011a0c08011040180030005014600020002a0e756e697175655f726f77696428293000680070007800800100880100980100423a0a1573746174656d656e745f66696e6765727072696e7410021a0c08071000180030005019600020003000680070007800800100880100980100422e0a0973746174656d656e7410031a0c0807100018003000501960002000300068007000780080010088010098010042320a0c636f6c6c65637465645f617410041a0d080910001800300050a009600020003000680070007800800100880100980100422b0a05747261636510051a0d081210001800300050da1d60002001300068007000780080010088010098010042430a0d62756e646c655f6368756e6b7310061a1d080f104018003000380150f8075a0c080110401800300050146000600020013000680070007800800100880100980100422a0a056572726f7210071a0c08071000180030005019600020013000680070007800800100880100980100480852b2010a077072696d61727910011801220269642a1573746174656d656e745f66696e6765727072696e742a0973746174656d656e742a0c636f6c6c65637465645f61742a0574726163652a0d62756e646c655f6368756e6b732a056572726f72300140004a10080010001a00200028003000380040005a007002700370047005700670077a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2016c0a077072696d61727910001a0269641a1573746174656d656e745f66696e6765727072696e741a0973746174656d656e741a0c636f6c6c65637465645f61741a0574726163651a0d62756e646c655f6368756e6b731a056572726f7220012002200320042005200620072800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89ad8a89","value":"030ab0090a0e7363686564756c65645f6a6f62731825200128013a0042400a0b7363686564756c655f696410011a0c08011040180030005014600020002a0e756e697175655f726f7769642829300068007000780080010088010098010042320a0d7363686564756c655f6e616d6510021a0c0807100018003000501960002000300068007000780080010088010098010042420a076372656174656410031a0d080910001800300050a009600020002a136e6f7728293a3a3a54494d455354414d50545a3000680070007800800100880100980100422a0a056f776e657210041a0c08071000180030005019600020003000680070007800800100880100980100422e0a086e6578745f72756e10051a0d080910001800300050a00960002001300068007000780080010088010098010042330a0e7363686564756c655f737461746510061a0c0808100018003000501160002001300068007000780080010088010098010042320a0d7363686564756c655f6578707210071a0c0807100018003000501960002001300068007000780080010088010098010042350a107363686564756c655f64657461696c7310081a0c0808100018003000501160002001300068007000780080010088010098010042320a0d6578656375746f725f7479706510091a0c0807100018003000501960002000300068007000780080010088010098010042330a0e657865637574696f6e5f61726773100a1a0c08081000180030005011600020003000680070007800800100880100980100480b52ed010a077072696d61727910011801220b7363686564756c655f69642a0d7363686564756c655f6e616d652a07637265617465642a056f776e65722a086e6578745f72756e2a0e7363686564756c655f73746174652a0d7363686564756c655f657870722a107363686564756c655f64657461696c732a0d6578656375746f725f747970652a0e657865637574696f6e5f61726773300140004a10080010001a00200028003000380040005a0070027003700470057006700770087009700a7a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e001005a660a0c6e6578745f72756e5f6964781002180022086e6578745f72756e3005380140004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201380a05736368656410001a0b7363686564756c655f69641a086e6578745f72756e1a0e7363686564756c655f73746174652001200520062800b201780a056f7468657210011a0d7363686564756c655f6e616d651a07637265617465641a056f776e65721a0d7363686564756c655f657870721a107363686564756c655f64657461696c731a0d6578656375746f725f747970651a0e657865637574696f6e5f61726773200220032004200720082009200a2800b80102c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} -,{"key":"8b89af8a89","value":"030a93030a0b73716c6c6976656e6573731827200128013a00422f0a0a73657373696f6e5f696410011a0c0808100018003000501160002000300068007000780080010088010098010042300a0a65787069726174696f6e10021a0d080310001800300050a40d6000200030006800700078008001008801009801004803526f0a077072696d61727910011801220a73657373696f6e5f69642a0a65787069726174696f6e300140004a10080010001a00200028003000380040005a0070027a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2013c0a1a66616d305f73657373696f6e5f69645f65787069726174696f6e10001a0a73657373696f6e5f69641a0a65787069726174696f6e200120022802b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} +,{"key":"8b89af8a89","value":"030ad3030a0b73716c6c6976656e6573731827200128013a00422f0a0a73657373696f6e5f696410011a0c0808100018003000501160002000300068007000780080010088010098010042300a0a65787069726174696f6e10021a0d080310001800300050a40d60002000300068007000780080010088010098010042300a0b637264625f726567696f6e10031a0c0808100018003000501160002000300068007000780080010088010098010048045280010a077072696d61727910021801220b637264625f726567696f6e220a73657373696f6e5f69642a0a65787069726174696f6e30033001400040004a10080010001a00200028003000380040005a0070027a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201380a077072696d61727910001a0b637264625f726567696f6e1a0a73657373696f6e5f69641a0a65787069726174696f6e2003200120022802b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b08a89","value":"030ac0040a0a6d6967726174696f6e731828200128013a00422a0a056d616a6f7210011a0c08011040180030005014600020003000680070007800800100880100980100422a0a056d696e6f7210021a0c08011040180030005014600020003000680070007800800100880100980100422a0a05706174636810031a0c08011040180030005014600020003000680070007800800100880100980100422d0a08696e7465726e616c10041a0c0801104018003000501460002000300068007000780080010088010098010042320a0c636f6d706c657465645f617410051a0d080910001800300050a00960002000300068007000780080010088010098010048065290010a077072696d6172791001180122056d616a6f7222056d696e6f72220570617463682208696e7465726e616c2a0c636f6d706c657465645f6174300130023003300440004000400040004a10080010001a00200028003000380040005a0070057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201440a077072696d61727910001a056d616a6f721a056d696e6f721a0570617463681a08696e7465726e616c1a0c636f6d706c657465645f6174200120022003200420052805b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b18a89","value":"030aaa030a0b6a6f696e5f746f6b656e731829200128013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422b0a0673656372657410021a0c0808100018003000501160002000300068007000780080010088010098010042300a0a65787069726174696f6e10031a0d080910001800300050a009600020003000680070007800800100880100980100480452710a077072696d61727910011801220269642a067365637265742a0a65787069726174696f6e300140004a10080010001a00200028003000380040005a00700270037a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2012b0a077072696d61727910001a0269641a067365637265741a0a65787069726174696f6e2001200220032800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b28a89","value":"030abe140a1473746174656d656e745f73746174697374696373182a200128013a0042330a0d616767726567617465645f747310011a0d080910001800300050a00960002000300068007000780080010088010098010042330a0e66696e6765727072696e745f696410021a0c08081000180030005011600020003000680070007800800100880100980100423f0a1a7472616e73616374696f6e5f66696e6765727072696e745f696410031a0c08081000180030005011600020003000680070007800800100880100980100422e0a09706c616e5f6861736810041a0c08081000180030005011600020003000680070007800800100880100980100422d0a086170705f6e616d6510051a0c08071000180030005019600020003000680070007800800100880100980100422c0a076e6f64655f696410061a0c0801104018003000501460002000300068007000780080010088010098010042380a0c6167675f696e74657276616c10071a13080610001800300050a20960006a040800100020003000680070007800800100880100980100422e0a086d6574616461746110081a0d081210001800300050da1d60002000300068007000780080010088010098010042300a0a7374617469737469637310091a0d081210001800300050da1d600020003000680070007800800100880100980100422a0a04706c616e100a1a0d081210001800300050da1d600020003000680070007800800100880100980100429c020a68637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f38100b1a0c080110201800300050176000200030015a8c016d6f6428666e76333228637264625f696e7465726e616c2e646174756d735f746f5f627974657328616767726567617465645f74732c206170705f6e616d652c2066696e6765727072696e745f69642c206e6f64655f69642c20706c616e5f686173682c207472616e73616374696f6e5f66696e6765727072696e745f696429292c20383a3a3a494e543829680070007800800100880100980100425f0a15696e6465785f7265636f6d6d656e646174696f6e73100c1a1d080f100018003000380750f1075a0c080710001800300050196000600020002a1241525241595b5d3a3a3a535452494e475b5d3000680070007800800100880100980100426c0a0d696e64657865735f7573616765100d1a0d081210001800300050da1d6000200130005a3728737461746973746963732d3e2773746174697374696373273a3a3a535452494e47292d3e27696e6465786573273a3a3a535452494e47680070007800800101880100980100480e52bd040a077072696d617279100118012268637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f38220d616767726567617465645f7473220e66696e6765727072696e745f6964221a7472616e73616374696f6e5f66696e6765727072696e745f69642209706c616e5f6861736822086170705f6e616d6522076e6f64655f69642a0c6167675f696e74657276616c2a086d657461646174612a0a737461746973746963732a04706c616e2a15696e6465785f7265636f6d6d656e646174696f6e73300b30013002300330043005300640004000400040004000400040004a10080010001a00200028003000380040005a00700770087009700a700c7a0408002000800100880100900104980101a201c70108011268637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381808220d616767726567617465645f747322086170705f6e616d65220e66696e6765727072696e745f696422076e6f64655f69642209706c616e5f68617368221a7472616e73616374696f6e5f66696e6765727072696e745f6964a80100b20100ba0100c00100c80100d00101e001005a9d010a1566696e6765727072696e745f73746174735f69647810021800220e66696e6765727072696e745f6964221a7472616e73616374696f6e5f66696e6765727072696e745f696430023003380b3801380438053806400040004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e001005a7f0a11696e64657865735f75736167655f69647810031800220d696e64657865735f7573616765300d380b38013802380338043805380640004a10080010001a00200028003000380040005a007a0408002000800101880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100d80100e0010060046a210a0b0a0561646d696e102018200a0a0a04726f6f741020182012046e6f64651802800101880103980100a201b9020abc01637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f3820494e2028303a3a3a494e54382c20313a3a3a494e54382c20323a3a3a494e54382c20333a3a3a494e54382c20343a3a3a494e54382c20353a3a3a494e54382c20363a3a3a494e54382c20373a3a3a494e543829126e636865636b5f637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381800280b300038014002b201a9020a077072696d61727910001a68637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381a0d616767726567617465645f74731a0e66696e6765727072696e745f69641a1a7472616e73616374696f6e5f66696e6765727072696e745f69641a09706c616e5f686173681a086170705f6e616d651a076e6f64655f69641a0c6167675f696e74657276616c1a086d657461646174611a0a737461746973746963731a04706c616e1a15696e6465785f7265636f6d6d656e646174696f6e73200b200120022003200420052006200720082009200a200c2800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} diff --git a/pkg/sql/catalog/systemschema/system.go b/pkg/sql/catalog/systemschema/system.go index a822e35a0da5..35de001871d0 100644 --- a/pkg/sql/catalog/systemschema/system.go +++ b/pkg/sql/catalog/systemschema/system.go @@ -483,14 +483,6 @@ CREATE TABLE system.scheduled_jobs ( )` SqllivenessTableSchema = ` -CREATE TABLE system.sqlliveness ( - session_id BYTES NOT NULL, - expiration DECIMAL NOT NULL, - CONSTRAINT "primary" PRIMARY KEY (session_id), - FAMILY fam0_session_id_expiration (session_id, expiration) -)` - - MrSqllivenessTableSchema = ` CREATE TABLE system.sqlliveness ( session_id BYTES NOT NULL, expiration DECIMAL NOT NULL, @@ -2306,36 +2298,6 @@ var ( // TODO(jeffswenson): remove the function wrapper around the // SqllivenessTable descriptor. See TestSupportMultiRegion for context. SqllivenessTable = func() SystemTable { - if TestSupportMultiRegion() { - return makeSystemTable( - MrSqllivenessTableSchema, - systemTable( - catconstants.SqllivenessTableName, - keys.SqllivenessID, - []descpb.ColumnDescriptor{ - {Name: "session_id", ID: 1, Type: types.Bytes, Nullable: false}, - {Name: "expiration", ID: 2, Type: types.Decimal, Nullable: false}, - {Name: "crdb_region", ID: 3, Type: types.Bytes, Nullable: false}, - }, - []descpb.ColumnFamilyDescriptor{ - { - Name: "primary", - ID: 0, - ColumnNames: []string{"session_id", "expiration", "crdb_region"}, - ColumnIDs: []descpb.ColumnID{1, 2, 3}, - DefaultColumnID: 2, - }, - }, - descpb.IndexDescriptor{ - Name: "primary", - ID: 2, - Unique: true, - KeyColumnNames: []string{"crdb_region", "session_id"}, - KeyColumnDirections: []catenumpb.IndexColumn_Direction{catenumpb.IndexColumn_ASC, catenumpb.IndexColumn_ASC}, - KeyColumnIDs: []descpb.ColumnID{3, 1}, - }, - )) - } return makeSystemTable( SqllivenessTableSchema, systemTable( @@ -2344,17 +2306,25 @@ var ( []descpb.ColumnDescriptor{ {Name: "session_id", ID: 1, Type: types.Bytes, Nullable: false}, {Name: "expiration", ID: 2, Type: types.Decimal, Nullable: false}, + {Name: "crdb_region", ID: 3, Type: types.Bytes, Nullable: false}, }, []descpb.ColumnFamilyDescriptor{ { - Name: "fam0_session_id_expiration", + Name: "primary", ID: 0, - ColumnNames: []string{"session_id", "expiration"}, - ColumnIDs: []descpb.ColumnID{1, 2}, + ColumnNames: []string{"crdb_region", "session_id", "expiration"}, + ColumnIDs: []descpb.ColumnID{3, 1, 2}, DefaultColumnID: 2, }, }, - pk("session_id"), + descpb.IndexDescriptor{ + Name: "primary", + ID: 2, + Unique: true, + KeyColumnNames: []string{"crdb_region", "session_id"}, + KeyColumnDirections: []catenumpb.IndexColumn_Direction{catenumpb.IndexColumn_ASC, catenumpb.IndexColumn_ASC}, + KeyColumnIDs: []descpb.ColumnID{3, 1}, + }, )) } diff --git a/pkg/sql/catalog/systemschema_test/testdata/bootstrap b/pkg/sql/catalog/systemschema_test/testdata/bootstrap index 3711f0475263..9d4ac3c3ad88 100644 --- a/pkg/sql/catalog/systemschema_test/testdata/bootstrap +++ b/pkg/sql/catalog/systemschema_test/testdata/bootstrap @@ -299,8 +299,8 @@ CREATE TABLE public.scheduled_jobs ( CREATE TABLE public.sqlliveness ( session_id BYTES NOT NULL, expiration DECIMAL NOT NULL, - CONSTRAINT "primary" PRIMARY KEY (session_id ASC), - FAMILY fam0_session_id_expiration (session_id, expiration) + crdb_region BYTES NOT NULL, + CONSTRAINT "primary" PRIMARY KEY (crdb_region ASC, session_id ASC) ); CREATE TABLE public.migrations ( major INT8 NOT NULL, @@ -486,7 +486,7 @@ schema_telemetry {"table":{"name":"span_stats_tenant_boundaries","id":57,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"tenant_id","id":1,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"boundaries","id":2,"type":{"family":"BytesFamily","oid":17}}],"nextColumnId":3,"families":[{"name":"primary","columnNames":["tenant_id","boundaries"],"columnIds":[1,2],"defaultColumnId":2}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["tenant_id"],"keyColumnDirections":["ASC"],"storeColumnNames":["boundaries"],"keyColumnIds":[1],"storeColumnIds":[2],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}} {"table":{"name":"span_stats_unique_keys","id":54,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"UuidFamily","oid":2950},"defaultExpr":"gen_random_uuid()"},{"name":"key_bytes","id":2,"type":{"family":"BytesFamily","oid":17},"nullable":true}],"nextColumnId":3,"families":[{"name":"primary","columnNames":["id","key_bytes"],"columnIds":[1,2],"defaultColumnId":2}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["key_bytes"],"keyColumnIds":[1],"storeColumnIds":[2],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":2},"indexes":[{"name":"unique_keys_key_bytes_idx","id":2,"unique":true,"version":3,"keyColumnNames":["key_bytes"],"keyColumnDirections":["ASC"],"keyColumnIds":[2],"keySuffixColumnIds":[1],"foreignKey":{},"interleave":{},"partitioning":{},"sharded":{},"geoConfig":{},"constraintId":1}],"nextIndexId":3,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":3}} {"table":{"name":"sql_instances","id":46,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"addr","id":2,"type":{"family":"StringFamily","oid":25},"nullable":true},{"name":"session_id","id":3,"type":{"family":"BytesFamily","oid":17},"nullable":true},{"name":"locality","id":4,"type":{"family":"JsonFamily","oid":3802},"nullable":true},{"name":"sql_addr","id":5,"type":{"family":"StringFamily","oid":25},"nullable":true}],"nextColumnId":6,"families":[{"name":"primary","columnNames":["id","addr","session_id","locality","sql_addr"],"columnIds":[1,2,3,4,5]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["addr","session_id","locality","sql_addr"],"keyColumnIds":[1],"storeColumnIds":[2,3,4,5],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}} -{"table":{"name":"sqlliveness","id":39,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"session_id","id":1,"type":{"family":"BytesFamily","oid":17}},{"name":"expiration","id":2,"type":{"family":"DecimalFamily","oid":1700}}],"nextColumnId":3,"families":[{"name":"fam0_session_id_expiration","columnNames":["session_id","expiration"],"columnIds":[1,2],"defaultColumnId":2}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["session_id"],"keyColumnDirections":["ASC"],"storeColumnNames":["expiration"],"keyColumnIds":[1],"storeColumnIds":[2],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}} +{"table":{"name":"sqlliveness","id":39,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"session_id","id":1,"type":{"family":"BytesFamily","oid":17}},{"name":"expiration","id":2,"type":{"family":"DecimalFamily","oid":1700}},{"name":"crdb_region","id":3,"type":{"family":"BytesFamily","oid":17}}],"nextColumnId":4,"families":[{"name":"primary","columnNames":["crdb_region","session_id","expiration"],"columnIds":[3,1,2],"defaultColumnId":2}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":2,"unique":true,"version":4,"keyColumnNames":["crdb_region","session_id"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["expiration"],"keyColumnIds":[3,1],"storeColumnIds":[2],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":3,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}} {"table":{"name":"statement_bundle_chunks","id":34,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"IntFamily","width":64,"oid":20},"defaultExpr":"unique_rowid()"},{"name":"description","id":2,"type":{"family":"StringFamily","oid":25},"nullable":true},{"name":"data","id":3,"type":{"family":"BytesFamily","oid":17}}],"nextColumnId":4,"families":[{"name":"primary","columnNames":["id","description","data"],"columnIds":[1,2,3]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["description","data"],"keyColumnIds":[1],"storeColumnIds":[2,3],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}} {"table":{"name":"statement_diagnostics","id":36,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"IntFamily","width":64,"oid":20},"defaultExpr":"unique_rowid()"},{"name":"statement_fingerprint","id":2,"type":{"family":"StringFamily","oid":25}},{"name":"statement","id":3,"type":{"family":"StringFamily","oid":25}},{"name":"collected_at","id":4,"type":{"family":"TimestampTZFamily","oid":1184}},{"name":"trace","id":5,"type":{"family":"JsonFamily","oid":3802},"nullable":true},{"name":"bundle_chunks","id":6,"type":{"family":"ArrayFamily","width":64,"arrayElemType":"IntFamily","oid":1016,"arrayContents":{"family":"IntFamily","width":64,"oid":20}},"nullable":true},{"name":"error","id":7,"type":{"family":"StringFamily","oid":25},"nullable":true}],"nextColumnId":8,"families":[{"name":"primary","columnNames":["id","statement_fingerprint","statement","collected_at","trace","bundle_chunks","error"],"columnIds":[1,2,3,4,5,6,7]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["statement_fingerprint","statement","collected_at","trace","bundle_chunks","error"],"keyColumnIds":[1],"storeColumnIds":[2,3,4,5,6,7],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}} {"table":{"name":"statement_diagnostics_requests","id":35,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"IntFamily","width":64,"oid":20},"defaultExpr":"unique_rowid()"},{"name":"completed","id":2,"type":{"oid":16},"defaultExpr":"false"},{"name":"statement_fingerprint","id":3,"type":{"family":"StringFamily","oid":25}},{"name":"statement_diagnostics_id","id":4,"type":{"family":"IntFamily","width":64,"oid":20},"nullable":true},{"name":"requested_at","id":5,"type":{"family":"TimestampTZFamily","oid":1184}},{"name":"min_execution_latency","id":6,"type":{"family":"IntervalFamily","oid":1186,"intervalDurationField":{}},"nullable":true},{"name":"expires_at","id":7,"type":{"family":"TimestampTZFamily","oid":1184},"nullable":true},{"name":"sampling_probability","id":8,"type":{"family":"FloatFamily","width":64,"oid":701},"nullable":true}],"nextColumnId":9,"families":[{"name":"primary","columnNames":["id","completed","statement_fingerprint","statement_diagnostics_id","requested_at","min_execution_latency","expires_at","sampling_probability"],"columnIds":[1,2,3,4,5,6,7,8]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["completed","statement_fingerprint","statement_diagnostics_id","requested_at","min_execution_latency","expires_at","sampling_probability"],"keyColumnIds":[1],"storeColumnIds":[2,3,4,5,6,7,8],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"indexes":[{"name":"completed_idx","id":2,"version":3,"keyColumnNames":["completed","id"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["statement_fingerprint","min_execution_latency","expires_at","sampling_probability"],"keyColumnIds":[2,1],"storeColumnIds":[3,6,7,8],"foreignKey":{},"interleave":{},"partitioning":{},"sharded":{},"geoConfig":{}}],"nextIndexId":3,"privileges":{"users":[{"userProto":"admin","privileges":"480","withGrantOption":"480"},{"userProto":"root","privileges":"480","withGrantOption":"480"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"checks":[{"expr":"sampling_probability BETWEEN _:::FLOAT8 AND _:::FLOAT8","name":"check_sampling_probability","columnIds":[8],"constraintId":2}],"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":3}} diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index 323c8e480ae9..55797f257b51 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -250,6 +250,18 @@ func (n *createStatsNode) makeJobRecord(ctx context.Context) (*jobs.Record, erro ) } + if n.Options.UsingExtremes && !n.p.SessionData().EnableCreateStatsUsingExtremes { + return nil, pgerror.New(pgcode.FeatureNotSupported, + "creating partial statistics at extremes is not yet supported", + ) + } + + if n.Options.Where != nil { + return nil, pgerror.New(pgcode.FeatureNotSupported, + "creating partial statistics with a WHERE clause is not yet supported", + ) + } + if err := n.p.CheckPrivilege(ctx, tableDesc, privilege.SELECT); err != nil { return nil, err } diff --git a/pkg/sql/exec_util.go b/pkg/sql/exec_util.go index ec5d3b332ee2..6336c770fa66 100644 --- a/pkg/sql/exec_util.go +++ b/pkg/sql/exec_util.go @@ -3470,6 +3470,10 @@ func (m *sessionDataMutator) SetOptimizerAlwaysUseHistograms(val bool) { m.data.OptimizerAlwaysUseHistograms = val } +func (m *sessionDataMutator) SetEnableCreateStatsUsingExtremes(val bool) { + m.data.EnableCreateStatsUsingExtremes = val +} + // Utility functions related to scrubbing sensitive information on SQL Stats. // quantizeCounts ensures that the Count field in the diff --git a/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog b/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog index 4418339681cd..2a9dc5586355 100644 --- a/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog +++ b/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog @@ -133,7 +133,7 @@ SELECT id, strip_volatile(descriptor) FROM crdb_internal.kv_catalog_descriptor 35 {"table": {"checks": [{"columnIds": [8], "constraintId": 2, "expr": "sampling_probability BETWEEN 0.0:::FLOAT8 AND 1.0:::FLOAT8", "name": "check_sampling_probability"}], "columns": [{"defaultExpr": "unique_rowid()", "id": 1, "name": "id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"defaultExpr": "false", "id": 2, "name": "completed", "type": {"oid": 16}}, {"id": 3, "name": "statement_fingerprint", "type": {"family": "StringFamily", "oid": 25}}, {"id": 4, "name": "statement_diagnostics_id", "nullable": true, "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 5, "name": "requested_at", "type": {"family": "TimestampTZFamily", "oid": 1184}}, {"id": 6, "name": "min_execution_latency", "nullable": true, "type": {"family": "IntervalFamily", "intervalDurationField": {}, "oid": 1186}}, {"id": 7, "name": "expires_at", "nullable": true, "type": {"family": "TimestampTZFamily", "oid": 1184}}, {"id": 8, "name": "sampling_probability", "nullable": true, "type": {"family": "FloatFamily", "oid": 701, "width": 64}}], "formatVersion": 3, "id": 35, "indexes": [{"foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [2, 1], "keyColumnNames": ["completed", "id"], "name": "completed_idx", "partitioning": {}, "sharded": {}, "storeColumnIds": [3, 6, 7, 8], "storeColumnNames": ["statement_fingerprint", "min_execution_latency", "expires_at", "sampling_probability"], "version": 3}], "name": "statement_diagnostics_requests", "nextColumnId": 9, "nextConstraintId": 3, "nextIndexId": 3, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3, 4, 5, 6, 7, 8], "storeColumnNames": ["completed", "statement_fingerprint", "statement_diagnostics_id", "requested_at", "min_execution_latency", "expires_at", "sampling_probability"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} 36 {"table": {"columns": [{"defaultExpr": "unique_rowid()", "id": 1, "name": "id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "statement_fingerprint", "type": {"family": "StringFamily", "oid": 25}}, {"id": 3, "name": "statement", "type": {"family": "StringFamily", "oid": 25}}, {"id": 4, "name": "collected_at", "type": {"family": "TimestampTZFamily", "oid": 1184}}, {"id": 5, "name": "trace", "nullable": true, "type": {"family": "JsonFamily", "oid": 3802}}, {"id": 6, "name": "bundle_chunks", "nullable": true, "type": {"arrayContents": {"family": "IntFamily", "oid": 20, "width": 64}, "arrayElemType": "IntFamily", "family": "ArrayFamily", "oid": 1016, "width": 64}}, {"id": 7, "name": "error", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 36, "name": "statement_diagnostics", "nextColumnId": 8, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3, 4, 5, 6, 7], "storeColumnNames": ["statement_fingerprint", "statement", "collected_at", "trace", "bundle_chunks", "error"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} 37 {"table": {"columns": [{"defaultExpr": "unique_rowid()", "id": 1, "name": "schedule_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "schedule_name", "type": {"family": "StringFamily", "oid": 25}}, {"defaultExpr": "now():::TIMESTAMPTZ", "id": 3, "name": "created", "type": {"family": "TimestampTZFamily", "oid": 1184}}, {"id": 4, "name": "owner", "type": {"family": "StringFamily", "oid": 25}}, {"id": 5, "name": "next_run", "nullable": true, "type": {"family": "TimestampTZFamily", "oid": 1184}}, {"id": 6, "name": "schedule_state", "nullable": true, "type": {"family": "BytesFamily", "oid": 17}}, {"id": 7, "name": "schedule_expr", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}, {"id": 8, "name": "schedule_details", "nullable": true, "type": {"family": "BytesFamily", "oid": 17}}, {"id": 9, "name": "executor_type", "type": {"family": "StringFamily", "oid": 25}}, {"id": 10, "name": "execution_args", "type": {"family": "BytesFamily", "oid": 17}}], "formatVersion": 3, "id": 37, "indexes": [{"foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [5], "keyColumnNames": ["next_run"], "keySuffixColumnIds": [1], "name": "next_run_idx", "partitioning": {}, "sharded": {}, "version": 3}], "name": "scheduled_jobs", "nextColumnId": 11, "nextConstraintId": 2, "nextIndexId": 3, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["schedule_id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3, 4, 5, 6, 7, 8, 9, 10], "storeColumnNames": ["schedule_name", "created", "owner", "next_run", "schedule_state", "schedule_expr", "schedule_details", "executor_type", "execution_args"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} -39 {"table": {"columns": [{"id": 1, "name": "session_id", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 2, "name": "expiration", "type": {"family": "DecimalFamily", "oid": 1700}}], "formatVersion": 3, "id": 39, "name": "sqlliveness", "nextColumnId": 3, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["session_id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2], "storeColumnNames": ["expiration"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} +39 {"table": {"columns": [{"id": 1, "name": "session_id", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 2, "name": "expiration", "type": {"family": "DecimalFamily", "oid": 1700}}, {"id": 3, "name": "crdb_region", "type": {"family": "BytesFamily", "oid": 17}}], "formatVersion": 3, "id": 39, "name": "sqlliveness", "nextColumnId": 4, "nextConstraintId": 2, "nextIndexId": 3, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [3, 1], "keyColumnNames": ["crdb_region", "session_id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2], "storeColumnNames": ["expiration"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} 40 {"table": {"columns": [{"id": 1, "name": "major", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "minor", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 3, "name": "patch", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 4, "name": "internal", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 5, "name": "completed_at", "type": {"family": "TimestampTZFamily", "oid": 1184}}], "formatVersion": 3, "id": 40, "name": "migrations", "nextColumnId": 6, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC", "ASC", "ASC", "ASC"], "keyColumnIds": [1, 2, 3, 4], "keyColumnNames": ["major", "minor", "patch", "internal"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [5], "storeColumnNames": ["completed_at"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} 41 {"table": {"columns": [{"id": 1, "name": "id", "type": {"family": "UuidFamily", "oid": 2950}}, {"id": 2, "name": "secret", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 3, "name": "expiration", "type": {"family": "TimestampTZFamily", "oid": 1184}}], "formatVersion": 3, "id": 41, "name": "join_tokens", "nextColumnId": 4, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3], "storeColumnNames": ["secret", "expiration"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "480", "userProto": "admin", "withGrantOption": "480"}, {"privileges": "480", "userProto": "root", "withGrantOption": "480"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} 42 {"table": {"checks": [{"columnIds": [11], "constraintId": 2, "expr": "crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8 IN (0:::INT8, 1:::INT8, 2:::INT8, 3:::INT8, 4:::INT8, 5:::INT8, 6:::INT8, 7:::INT8)", "fromHashShardedColumn": true, "name": "check_crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8"}], "columns": [{"id": 1, "name": "aggregated_ts", "type": {"family": "TimestampTZFamily", "oid": 1184}}, {"id": 2, "name": "fingerprint_id", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 3, "name": "transaction_fingerprint_id", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 4, "name": "plan_hash", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 5, "name": "app_name", "type": {"family": "StringFamily", "oid": 25}}, {"id": 6, "name": "node_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 7, "name": "agg_interval", "type": {"family": "IntervalFamily", "intervalDurationField": {}, "oid": 1186}}, {"id": 8, "name": "metadata", "type": {"family": "JsonFamily", "oid": 3802}}, {"id": 9, "name": "statistics", "type": {"family": "JsonFamily", "oid": 3802}}, {"id": 10, "name": "plan", "type": {"family": "JsonFamily", "oid": 3802}}, {"computeExpr": "mod(fnv32(crdb_internal.datums_to_bytes(aggregated_ts, app_name, fingerprint_id, node_id, plan_hash, transaction_fingerprint_id)), 8:::INT8)", "hidden": true, "id": 11, "name": "crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8", "type": {"family": "IntFamily", "oid": 23, "width": 32}}, {"defaultExpr": "ARRAY[]:::STRING[]", "id": 12, "name": "index_recommendations", "type": {"arrayContents": {"family": "StringFamily", "oid": 25}, "arrayElemType": "StringFamily", "family": "ArrayFamily", "oid": 1009}}, {"computeExpr": "(statistics->'statistics':::STRING)->'indexes':::STRING", "id": 13, "name": "indexes_usage", "nullable": true, "type": {"family": "JsonFamily", "oid": 3802}, "virtual": true}], "formatVersion": 3, "id": 42, "indexes": [{"foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [2, 3], "keyColumnNames": ["fingerprint_id", "transaction_fingerprint_id"], "keySuffixColumnIds": [11, 1, 4, 5, 6], "name": "fingerprint_stats_idx", "partitioning": {}, "sharded": {}, "version": 3}, {"foreignKey": {}, "geoConfig": {}, "id": 3, "interleave": {}, "invertedColumnKinds": ["DEFAULT"], "keyColumnDirections": ["ASC"], "keyColumnIds": [13], "keyColumnNames": ["indexes_usage"], "keySuffixColumnIds": [11, 1, 2, 3, 4, 5, 6], "name": "indexes_usage_idx", "partitioning": {}, "sharded": {}, "type": "INVERTED", "version": 3}], "name": "statement_statistics", "nextColumnId": 14, "nextConstraintId": 3, "nextIndexId": 4, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC", "ASC", "ASC", "ASC", "ASC", "ASC", "ASC"], "keyColumnIds": [11, 1, 2, 3, 4, 5, 6], "keyColumnNames": ["crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8", "aggregated_ts", "fingerprint_id", "transaction_fingerprint_id", "plan_hash", "app_name", "node_id"], "name": "primary", "partitioning": {}, "sharded": {"columnNames": ["aggregated_ts", "app_name", "fingerprint_id", "node_id", "plan_hash", "transaction_fingerprint_id"], "isSharded": true, "name": "crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8", "shardBuckets": 8}, "storeColumnIds": [7, 8, 9, 10, 12], "storeColumnNames": ["agg_interval", "metadata", "statistics", "plan", "index_recommendations"], "unique": true, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "admin", "withGrantOption": "32"}, {"privileges": "32", "userProto": "root", "withGrantOption": "32"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 29, "version": "1"}} diff --git a/pkg/sql/logictest/testdata/logic_test/distsql_stats b/pkg/sql/logictest/testdata/logic_test/distsql_stats index 5e7bfec78a01..271c985cd97c 100644 --- a/pkg/sql/logictest/testdata/logic_test/distsql_stats +++ b/pkg/sql/logictest/testdata/logic_test/distsql_stats @@ -1998,6 +1998,15 @@ INSERT INTO abcd VALUES statement ok INSERT INTO xy VALUES (-1, 9), (-2, 8), (5, 15), (6, 16) +statement error pgcode 0A000 creating partial statistics with a WHERE clause is not yet supported +CREATE STATISTICS abcd_a_partial ON a FROM abcd WHERE a > 1; + +statement error pgcode 0A000 creating partial statistics at extremes is not yet supported +CREATE STATISTICS abcd_a_partial ON a FROM abcd USING EXTREMES; + +statement ok +SET enable_create_stats_using_extremes = on + statement ok CREATE STATISTICS abcd_a_partial ON a FROM abcd USING EXTREMES; diff --git a/pkg/sql/logictest/testdata/logic_test/information_schema b/pkg/sql/logictest/testdata/logic_test/information_schema index a0dc1bd75c04..a111a7f42700 100644 --- a/pkg/sql/logictest/testdata/logic_test/information_schema +++ b/pkg/sql/logictest/testdata/logic_test/information_schema @@ -1676,6 +1676,7 @@ system public 29_46_1_not_null system public primary system public sql_instances PRIMARY KEY NO NO system public 29_39_1_not_null system public sqlliveness CHECK NO NO system public 29_39_2_not_null system public sqlliveness CHECK NO NO +system public 29_39_3_not_null system public sqlliveness CHECK NO NO system public primary system public sqlliveness PRIMARY KEY NO NO system public 29_34_1_not_null system public statement_bundle_chunks CHECK NO NO system public 29_34_3_not_null system public statement_bundle_chunks CHECK NO NO @@ -1876,6 +1877,7 @@ system public 29_37_4_not_null system public 29_37_9_not_null executor_type IS NOT NULL system public 29_39_1_not_null session_id IS NOT NULL system public 29_39_2_not_null expiration IS NOT NULL +system public 29_39_3_not_null crdb_region IS NOT NULL system public 29_3_1_not_null id IS NOT NULL system public 29_40_1_not_null major IS NOT NULL system public 29_40_2_not_null minor IS NOT NULL @@ -2034,6 +2036,7 @@ system public span_stats_tenant_boundaries tenant_id system public span_stats_unique_keys id system public primary system public span_stats_unique_keys key_bytes system public unique_keys_key_bytes_idx system public sql_instances id system public primary +system public sqlliveness crdb_region system public primary system public sqlliveness session_id system public primary system public statement_bundle_chunks id system public primary system public statement_diagnostics id system public primary @@ -2331,6 +2334,7 @@ system public sql_instances id system public sql_instances locality 4 system public sql_instances session_id 3 system public sql_instances sql_addr 5 +system public sqlliveness crdb_region 3 system public sqlliveness expiration 2 system public sqlliveness session_id 1 system public statement_bundle_chunks data 3 @@ -4972,6 +4976,7 @@ disable_partially_distributed_plans off disable_plan_gists off disallow_full_table_scans off enable_auto_rehoming off +enable_create_stats_using_extremes off enable_drop_enum_value on enable_experimental_alter_column_type_general off enable_implicit_select_for_update on diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index 46445aaf63a5..c83723e0494c 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -1113,7 +1113,7 @@ ORDER BY indexrelid indexrelid indrelid indnatts indisunique indnullsnotdistinct indisprimary indisexclusion indimmediate indisclustered indisvalid indcheckxmin indisready indislive indisreplident indkey indcollation indclass indoption indexprs indpred indnkeyatts 144368028 32 1 true false true false true false true false false true false 1 0 0 2 NULL NULL 1 190763692 48 1 false false true false false false true false false true false 1 0 0 2 NULL NULL 1 -404104299 39 1 true false true false true false true false false true false 1 0 0 2 NULL NULL 1 +404104296 39 2 true false true false true false true false false true false 3 1 0 0 0 0 2 2 NULL NULL 2 450499960 55 1 false false false false false false true false false true false 2 0 0 2 NULL NULL 1 450499963 55 1 true false true false true false true false false true false 1 0 0 2 NULL NULL 1 496895627 7 1 false false true false false false true false false true false 1 0 0 2 NULL NULL 1 @@ -1198,7 +1198,8 @@ ORDER BY indexrelid, operator_argument_position indexrelid operator_argument_type_oid operator_argument_position 144368028 0 1 190763692 0 1 -404104299 0 1 +404104296 0 1 +404104296 0 2 450499960 0 1 450499963 0 1 496895627 0 1 @@ -2595,6 +2596,7 @@ disable_plan_gists off NULL disallow_full_table_scans off NULL NULL NULL string distsql off NULL NULL NULL string enable_auto_rehoming off NULL NULL NULL string +enable_create_stats_using_extremes off NULL NULL NULL string enable_experimental_alter_column_type_general off NULL NULL NULL string enable_implicit_select_for_update on NULL NULL NULL string enable_implicit_transaction_for_batch_statements on NULL NULL NULL string @@ -2743,6 +2745,7 @@ disable_plan_gists off NULL disallow_full_table_scans off NULL user NULL off off distsql off NULL user NULL off off enable_auto_rehoming off NULL user NULL off off +enable_create_stats_using_extremes off NULL user NULL off off enable_experimental_alter_column_type_general off NULL user NULL off off enable_implicit_select_for_update on NULL user NULL on on enable_implicit_transaction_for_batch_statements on NULL user NULL on on @@ -2889,6 +2892,7 @@ disallow_full_table_scans NULL NULL NULL distsql NULL NULL NULL NULL NULL distsql_workmem NULL NULL NULL NULL NULL enable_auto_rehoming NULL NULL NULL NULL NULL +enable_create_stats_using_extremes NULL NULL NULL NULL NULL enable_experimental_alter_column_type_general NULL NULL NULL NULL NULL enable_implicit_select_for_update NULL NULL NULL NULL NULL enable_implicit_transaction_for_batch_statements NULL NULL NULL NULL NULL diff --git a/pkg/sql/logictest/testdata/logic_test/show_source b/pkg/sql/logictest/testdata/logic_test/show_source index b5b3861d5aa4..d600373f1097 100644 --- a/pkg/sql/logictest/testdata/logic_test/show_source +++ b/pkg/sql/logictest/testdata/logic_test/show_source @@ -57,6 +57,7 @@ disable_plan_gists off disallow_full_table_scans off distsql off enable_auto_rehoming off +enable_create_stats_using_extremes off enable_experimental_alter_column_type_general off enable_implicit_select_for_update on enable_implicit_transaction_for_batch_statements on diff --git a/pkg/sql/sessiondatapb/local_only_session_data.proto b/pkg/sql/sessiondatapb/local_only_session_data.proto index 400b4a9ce958..73482feb5e5d 100644 --- a/pkg/sql/sessiondatapb/local_only_session_data.proto +++ b/pkg/sql/sessiondatapb/local_only_session_data.proto @@ -346,6 +346,9 @@ message LocalOnlySessionData { // OptimizerAlwaysUseHistograms, when true, ensures that the optimizer // always uses histograms to calculate statistics if available. bool optimizer_always_use_histograms = 94; + // EnableCreateStatsUsingExtremes, when true, allows the use of CREATE + // STATISTICS .. USING EXTREMES. + bool enable_create_stats_using_extremes = 95; /////////////////////////////////////////////////////////////////////////// // WARNING: consider whether a session parameter you're adding needs to // diff --git a/pkg/sql/sqlliveness/slprovider/BUILD.bazel b/pkg/sql/sqlliveness/slprovider/BUILD.bazel index 4f733309cde9..13d759790c1e 100644 --- a/pkg/sql/sqlliveness/slprovider/BUILD.bazel +++ b/pkg/sql/sqlliveness/slprovider/BUILD.bazel @@ -9,6 +9,7 @@ go_library( deps = [ "//pkg/keys", "//pkg/kv", + "//pkg/server/settingswatcher", "//pkg/settings/cluster", "//pkg/sql/sqlliveness", "//pkg/sql/sqlliveness/slinstance", diff --git a/pkg/sql/sqlliveness/slprovider/slprovider.go b/pkg/sql/sqlliveness/slprovider/slprovider.go index 9e593bb780ec..33c9cb25b3c2 100644 --- a/pkg/sql/sqlliveness/slprovider/slprovider.go +++ b/pkg/sql/sqlliveness/slprovider/slprovider.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slinstance" @@ -37,10 +38,11 @@ func New( db *kv.DB, codec keys.SQLCodec, settings *cluster.Settings, + settingsWatcher *settingswatcher.SettingsWatcher, testingKnobs *sqlliveness.TestingKnobs, sessionEvents slinstance.SessionEventListener, ) sqlliveness.Provider { - storage := slstorage.NewStorage(ambientCtx, stopper, clock, db, codec, settings) + storage := slstorage.NewStorage(ambientCtx, stopper, clock, db, codec, settings, settingsWatcher) instance := slinstance.NewSQLInstance(stopper, clock, storage, settings, testingKnobs, sessionEvents) return &provider{ Storage: storage, diff --git a/pkg/sql/sqlliveness/slstorage/BUILD.bazel b/pkg/sql/sqlliveness/slstorage/BUILD.bazel index 6307f99bd18d..c78d1c6504e7 100644 --- a/pkg/sql/sqlliveness/slstorage/BUILD.bazel +++ b/pkg/sql/sqlliveness/slstorage/BUILD.bazel @@ -13,14 +13,17 @@ go_library( importpath = "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slstorage", visibility = ["//visibility:public"], deps = [ + "//pkg/clusterversion", "//pkg/keys", "//pkg/kv", "//pkg/multitenant", "//pkg/roachpb", + "//pkg/server/settingswatcher", "//pkg/settings", "//pkg/settings/cluster", + "//pkg/sql/catalog", "//pkg/sql/catalog/systemschema", - "//pkg/sql/sem/catid", + "//pkg/sql/enum", "//pkg/sql/sem/eval", "//pkg/sql/sqlliveness", "//pkg/util/cache", @@ -46,12 +49,14 @@ go_test( "key_encoder_test.go", "main_test.go", "sessionid_test.go", + "slstorage_internal_test.go", "slstorage_test.go", ], args = ["-test.timeout=55s"], embed = [":slstorage"], deps = [ "//pkg/base", + "//pkg/clusterversion", "//pkg/keys", "//pkg/kv/kvpb", "//pkg/kv/kvserver", @@ -59,8 +64,10 @@ go_test( "//pkg/security/securityassets", "//pkg/security/securitytest", "//pkg/server", + "//pkg/server/settingswatcher", "//pkg/settings/cluster", - "//pkg/sql/catalog/descpb", + "//pkg/sql/catalog", + "//pkg/sql/catalog/desctestutils", "//pkg/sql/catalog/systemschema", "//pkg/sql/enum", "//pkg/sql/sqlliveness", diff --git a/pkg/sql/sqlliveness/slstorage/key_encoder.go b/pkg/sql/sqlliveness/slstorage/key_encoder.go index 9df68c1b39e6..2111cfe6e281 100644 --- a/pkg/sql/sqlliveness/slstorage/key_encoder.go +++ b/pkg/sql/sqlliveness/slstorage/key_encoder.go @@ -15,8 +15,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/roachpb" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" - "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/errors" @@ -35,17 +33,6 @@ type keyCodec interface { indexPrefix() roachpb.Key } -// makeKeyCodec constructs a key codec. It consults the -// COCKROACH_MR_SYSTEM_DATABASE environment variable to determine if it should -// use the regional by table or regional by row index format. -func makeKeyCodec(codec keys.SQLCodec, tableID catid.DescID, rbrIndex catid.IndexID) keyCodec { - if systemschema.TestSupportMultiRegion() { - return &rbrEncoder{codec.IndexPrefix(uint32(tableID), uint32(rbrIndex))} - } - const rbtIndexID = 1 - return &rbtEncoder{codec.IndexPrefix(uint32(tableID), rbtIndexID)} -} - type rbrEncoder struct { rbrIndex roachpb.Key } diff --git a/pkg/sql/sqlliveness/slstorage/key_encoder_test.go b/pkg/sql/sqlliveness/slstorage/key_encoder_test.go index bf3d3b0cc3d8..2b10440165f0 100644 --- a/pkg/sql/sqlliveness/slstorage/key_encoder_test.go +++ b/pkg/sql/sqlliveness/slstorage/key_encoder_test.go @@ -30,20 +30,18 @@ func TestKeyEncoder(t *testing.T) { defer leaktest.AfterTest(t)() defer log.Scope(t).Close(t) + codec := keys.MakeSQLCodec(roachpb.MustMakeTenantID(1337)) t.Run("RegionalByRow", func(t *testing.T) { defer envutil.TestSetEnv(t, "COCKROACH_MR_SYSTEM_DATABASE", "1")() - testKeyEncoder(t) + testKeyEncoder(t, &rbrEncoder{codec.IndexPrefix(42, 2)}) }) t.Run("RegionalByTable", func(t *testing.T) { defer envutil.TestSetEnv(t, "COCKROACH_MR_SYSTEM_DATABASE", "0")() - testKeyEncoder(t) + testKeyEncoder(t, &rbtEncoder{codec.IndexPrefix(42, 1)}) }) } -func testKeyEncoder(t *testing.T) { - codec := keys.MakeSQLCodec(roachpb.MustMakeTenantID(1337)) - keyCodec := makeKeyCodec(codec, 42, 2) - +func testKeyEncoder(t *testing.T, keyCodec keyCodec) { t.Run("Prefix", func(t *testing.T) { prefix := keyCodec.indexPrefix() @@ -79,13 +77,9 @@ func testKeyEncoder(t *testing.T) { id := sqlliveness.SessionID(uuid.MakeV4().GetBytes()) key, err := keyCodec.encode(id) - if systemschema.TestSupportMultiRegion() { - require.Error(t, err) - } else { - require.NoError(t, err) - decodedID, err := keyCodec.decode(key) - require.NoError(t, err) - require.Equal(t, id, decodedID) - } + require.NoError(t, err) + decodedID, err := keyCodec.decode(key) + require.NoError(t, err) + require.Equal(t, id, decodedID) }) } diff --git a/pkg/sql/sqlliveness/slstorage/sessionid.go b/pkg/sql/sqlliveness/slstorage/sessionid.go index 0dedeac5453b..bdf5b01ddec0 100644 --- a/pkg/sql/sqlliveness/slstorage/sessionid.go +++ b/pkg/sql/sqlliveness/slstorage/sessionid.go @@ -11,6 +11,8 @@ package slstorage import ( + "github.com/cockroachdb/cockroach/pkg/clusterversion" + "github.com/cockroachdb/cockroach/pkg/sql/enum" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/util/uuid" "github.com/cockroachdb/errors" @@ -68,8 +70,15 @@ func MakeSessionID(region []byte, id uuid.UUID) (sqlliveness.SessionID, error) { func UnsafeDecodeSessionID(session sqlliveness.SessionID) (region, id []byte, err error) { b := session.UnsafeBytes() if len(b) == legacyLen { - // Legacy format of SessionID. - return nil, b, nil + // TODO(jeffswenson): once the V23_1_SystemRbrCleanup version gate is + // deleted, replace this branch with a validation error. + _ = clusterversion.V23_1_SystemRbrCleanup + + // Legacy format of SessionID. Treat the session as if it belongs to + // region enum.One. This may crop up briefly if a session was created + // with an old binary right before the server is upgraded and the + // upgrade is kicked off while the session is still 'live'. + return enum.One, b, nil } if len(b) < minimumNonLegacyLen { // The smallest valid v1 session id is a [version, 1, single_byte_region, uuid...], diff --git a/pkg/sql/sqlliveness/slstorage/sessionid_test.go b/pkg/sql/sqlliveness/slstorage/sessionid_test.go index 8a46cb008738..ba0097bde8a1 100644 --- a/pkg/sql/sqlliveness/slstorage/sessionid_test.go +++ b/pkg/sql/sqlliveness/slstorage/sessionid_test.go @@ -90,6 +90,7 @@ func TestSessionIDEncoding(t *testing.T) { { name: "legacy_session", session: sqlliveness.SessionID(id1.GetBytes()), + region: enum.One, id: id1, }, { diff --git a/pkg/sql/sqlliveness/slstorage/slstorage.go b/pkg/sql/sqlliveness/slstorage/slstorage.go index 3f55f6b46bd5..2d148c7cbfa1 100644 --- a/pkg/sql/sqlliveness/slstorage/slstorage.go +++ b/pkg/sql/sqlliveness/slstorage/slstorage.go @@ -15,13 +15,16 @@ import ( "math/rand" "time" + "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/multitenant" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" "github.com/cockroachdb/cockroach/pkg/settings" "github.com/cockroachdb/cockroach/pkg/settings/cluster" - "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/util/cache" @@ -78,15 +81,17 @@ var CacheSize = settings.RegisterIntSetting( // Storage deals with reading and writing session records. It implements the // sqlliveness.Reader interface, and the slinstace.Writer interface. type Storage struct { - settings *cluster.Settings - stopper *stop.Stopper - clock *hlc.Clock - db *kv.DB - codec keys.SQLCodec - metrics Metrics - gcInterval func() time.Duration - newTimer func() timeutil.TimerI - keyCodec keyCodec + settings *cluster.Settings + settingsWatcher *settingswatcher.SettingsWatcher + stopper *stop.Stopper + clock *hlc.Clock + db *kv.DB + codec keys.SQLCodec + metrics Metrics + gcInterval func() time.Duration + newTimer func() timeutil.TimerI + newKeyCodec keyCodec + oldKeyCodec keyCodec mu struct { syncutil.Mutex @@ -116,18 +121,21 @@ func NewTestingStorage( db *kv.DB, codec keys.SQLCodec, settings *cluster.Settings, - sqllivenessTableID catid.DescID, - rbrIndexID catid.IndexID, + settingsWatcher *settingswatcher.SettingsWatcher, + table catalog.TableDescriptor, newTimer func() timeutil.TimerI, ) *Storage { + const rbtIndexID = 1 s := &Storage{ - settings: settings, - stopper: stopper, - clock: clock, - db: db, - codec: codec, - keyCodec: makeKeyCodec(codec, sqllivenessTableID, rbrIndexID), - newTimer: newTimer, + settings: settings, + settingsWatcher: settingsWatcher, + stopper: stopper, + clock: clock, + db: db, + codec: codec, + newKeyCodec: &rbrEncoder{codec.IndexPrefix(uint32(table.GetID()), uint32(table.GetPrimaryIndexID()))}, + oldKeyCodec: &rbtEncoder{codec.IndexPrefix(uint32(table.GetID()), rbtIndexID)}, + newTimer: newTimer, gcInterval: func() time.Duration { baseInterval := GCInterval.Get(&settings.SV) jitter := GCJitter.Get(&settings.SV) @@ -156,9 +164,9 @@ func NewStorage( db *kv.DB, codec keys.SQLCodec, settings *cluster.Settings, + settingsWatcher *settingswatcher.SettingsWatcher, ) *Storage { - const rbrIndexID = 2 - return NewTestingStorage(ambientCtx, stopper, clock, db, codec, settings, keys.SqllivenessID, rbrIndexID, + return NewTestingStorage(ambientCtx, stopper, clock, db, codec, settings, settingsWatcher, systemschema.SqllivenessTable(), timeutil.DefaultTimeSource{}.NewTimer) } @@ -237,6 +245,32 @@ func (s *Storage) isAlive( return res.Val.(bool), nil } +func (s *Storage) getReadCodec(version *settingswatcher.VersionGuard) keyCodec { + if version.IsActive(clusterversion.V23_1_SystemRbrReadNew) { + return s.newKeyCodec + } + return s.oldKeyCodec +} + +func (s *Storage) getDualWriteCodec(version *settingswatcher.VersionGuard) keyCodec { + switch { + case version.IsActive(clusterversion.V23_1_SystemRbrSingleWrite): + return nil + case version.IsActive(clusterversion.V23_1_SystemRbrReadNew): + return s.oldKeyCodec + case version.IsActive(clusterversion.V23_1_SystemRbrDualWrite): + return s.newKeyCodec + default: + return nil + } +} + +func (s *Storage) versionGuard( + ctx context.Context, txn *kv.Txn, +) (settingswatcher.VersionGuard, error) { + return s.settingsWatcher.MakeVersionGuard(ctx, txn, clusterversion.V23_1_SystemRbrCleanup) +} + // This function will launch a singleflight goroutine for the session which // will populate its result into the caches underneath the mutex. The result // value will be a bool. The singleflight goroutine does not cancel its work @@ -291,7 +325,13 @@ func (s *Storage) deleteOrFetchSession( // Reset captured variable in case of retry. deleted, expiration, prevExpiration = false, hlc.Timestamp{}, hlc.Timestamp{} - k, err := s.keyCodec.encode(sid) + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + + readCodec := s.getReadCodec(&version) + k, err := readCodec.encode(sid) if err != nil { return err } @@ -318,6 +358,14 @@ func (s *Storage) deleteOrFetchSession( deleted, expiration = true, hlc.Timestamp{} ba := txn.NewBatch() ba.Del(k) + if dualCodec := s.getDualWriteCodec(&version); dualCodec != nil { + dualKey, err := dualCodec.encode(sid) + if err != nil { + return err + } + ba.Del(dualKey) + } + return txn.CommitInBatch(ctx, ba) }); err != nil { return false, hlc.Timestamp{}, errors.Wrapf(err, @@ -385,20 +433,21 @@ func (s *Storage) deleteExpiredSessions(ctx context.Context) { } func (s *Storage) fetchExpiredSessionIDs(ctx context.Context) ([]sqlliveness.SessionID, error) { - var toCheck []sqlliveness.SessionID - if err := s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - toCheck = nil // reset for restarts - start := s.keyCodec.indexPrefix() + findRows := func(ctx context.Context, txn *kv.Txn, keyCodec keyCodec) ([]sqlliveness.SessionID, error) { + start := keyCodec.indexPrefix() end := start.PrefixEnd() now := s.clock.Now() + + var toCheck []sqlliveness.SessionID + const maxRows = 1024 // arbitrary but plenty for { rows, err := txn.Scan(ctx, start, end, maxRows) if err != nil { - return err + return nil, err } if len(rows) == 0 { - return nil + return nil, nil } for i := range rows { exp, err := decodeValue(rows[i]) @@ -407,7 +456,7 @@ func (s *Storage) fetchExpiredSessionIDs(ctx context.Context) ([]sqlliveness.Ses continue } if exp.Less(now) { - id, err := s.keyCodec.decode(rows[i].Key) + id, err := keyCodec.decode(rows[i].Key) if err != nil { log.Warningf(ctx, "failed to decode row %s session: %v", rows[i].Key.String(), err) } @@ -415,14 +464,25 @@ func (s *Storage) fetchExpiredSessionIDs(ctx context.Context) ([]sqlliveness.Ses } } if len(rows) < maxRows { - return nil + return toCheck, nil } start = rows[len(rows)-1].Key.Next() } + } + + var result []sqlliveness.SessionID + if err := s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + result, err = findRows(ctx, txn, s.getReadCodec(&version)) + return err }); err != nil { return nil, err } - return toCheck, nil + + return result, nil } // Insert inserts the input Session in table `system.sqlliveness`. @@ -432,13 +492,33 @@ func (s *Storage) fetchExpiredSessionIDs(ctx context.Context) ([]sqlliveness.Ses func (s *Storage) Insert( ctx context.Context, sid sqlliveness.SessionID, expiration hlc.Timestamp, ) (err error) { - k, err := s.keyCodec.encode(sid) - if err != nil { - return err - } - v := encodeValue(expiration) ctx = multitenant.WithTenantCostControlExemption(ctx) - if err := s.db.InitPut(ctx, k, &v, true); err != nil { + if err := s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { + batch := txn.NewBatch() + + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + readCodec := s.getReadCodec(&version) + k, err := readCodec.encode(sid) + if err != nil { + return err + } + v := encodeValue(expiration) + batch.InitPut(k, &v, true) + + if dualCodec := s.getDualWriteCodec(&version); dualCodec != nil { + dualKey, err := dualCodec.encode(sid) + if err != nil { + return err + } + dualValue := encodeValue(expiration) + batch.InitPut(dualKey, &dualValue, true) + } + + return txn.CommitInBatch(ctx, batch) + }); err != nil { s.metrics.WriteFailures.Inc(1) return errors.Wrapf(err, "could not insert session %s", sid) } @@ -454,7 +534,14 @@ func (s *Storage) Update( ) (sessionExists bool, err error) { ctx = multitenant.WithTenantCostControlExemption(ctx) err = s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - k, err := s.keyCodec.encode(sid) + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + + readCodec := s.getReadCodec(&version) + + k, err := readCodec.encode(sid) if err != nil { return err } @@ -468,6 +555,14 @@ func (s *Storage) Update( v := encodeValue(expiration) ba := txn.NewBatch() ba.Put(k, &v) + if dualCodec := s.getDualWriteCodec(&version); dualCodec != nil { + dualKey, err := dualCodec.encode(sid) + if err != nil { + return err + } + dualValue := encodeValue(expiration) + ba.Put(dualKey, &dualValue) + } return txn.CommitInBatch(ctx, ba) }) if err != nil || !sessionExists { diff --git a/pkg/sql/sqlliveness/slstorage/slstorage_internal_test.go b/pkg/sql/sqlliveness/slstorage/slstorage_internal_test.go new file mode 100644 index 000000000000..21d3d51b4606 --- /dev/null +++ b/pkg/sql/sqlliveness/slstorage/slstorage_internal_test.go @@ -0,0 +1,108 @@ +// Copyright 2023 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 slstorage + +import ( + "testing" + + "github.com/cockroachdb/cockroach/pkg/clusterversion" + "github.com/cockroachdb/cockroach/pkg/keys" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" + "github.com/cockroachdb/cockroach/pkg/util/leaktest" + "github.com/cockroachdb/cockroach/pkg/util/log" + "github.com/stretchr/testify/require" +) + +func TestGetEncoder(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + + type codecType int + const ( + isNil codecType = iota + isRbr + isRbt + ) + + checkCodec := func(t *testing.T, typ codecType, codec keyCodec) { + t.Helper() + switch typ { + case isNil: + require.Nil(t, codec) + case isRbr: + require.NotNil(t, codec) + _, ok := codec.(*rbrEncoder) + require.True(t, ok, "expected %v to be an rbr encoder", codec) + case isRbt: + require.NotNil(t, codec) + _, ok := codec.(*rbtEncoder) + require.True(t, ok, "expected %v to be an rbt encoder", codec) + } + } + + type testCase struct { + name string + version clusterversion.Key + readCodec codecType + dualCodec codecType + } + tests := []testCase{ + { + name: "v23_1", + version: clusterversion.V23_1, + readCodec: isRbr, + dualCodec: isNil, + }, + { + name: "v22_2", + version: clusterversion.V22_2, + readCodec: isRbt, + dualCodec: isNil, + }, + { + name: "V23_1_SystemRbrDualWrite", + version: clusterversion.V23_1_SystemRbrDualWrite, + readCodec: isRbt, + dualCodec: isRbr, + }, + { + name: "V23_1_SystemRbrReadNew", + version: clusterversion.V23_1_SystemRbrReadNew, + readCodec: isRbr, + dualCodec: isRbt, + }, + { + name: "V23_1_SystemRbrSingleWrite", + version: clusterversion.V23_1_SystemRbrSingleWrite, + readCodec: isRbr, + dualCodec: isNil, + }, + { + name: "V23_1_SystemRbrCleanup", + version: clusterversion.V23_1_SystemRbrCleanup, + readCodec: isRbr, + dualCodec: isNil, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + storage := NewTestingStorage( + log.AmbientContext{}, nil, nil, nil, keys.SystemSQLCodec, nil, nil, systemschema.SqllivenessTable(), nil) + + version := clusterversion.ClusterVersion{Version: clusterversion.ByKey(tc.version)} + guard := settingswatcher.TestMakeVersionGuard(version) + + checkCodec(t, tc.readCodec, storage.getReadCodec(&guard)) + checkCodec(t, tc.dualCodec, storage.getDualWriteCodec(&guard)) + }) + } +} diff --git a/pkg/sql/sqlliveness/slstorage/slstorage_test.go b/pkg/sql/sqlliveness/slstorage/slstorage_test.go index c8ffdddeb5fe..2404316ce7f0 100644 --- a/pkg/sql/sqlliveness/slstorage/slstorage_test.go +++ b/pkg/sql/sqlliveness/slstorage/slstorage_test.go @@ -25,8 +25,10 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv/kvpb" "github.com/cockroachdb/cockroach/pkg/kv/kvserver" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" "github.com/cockroachdb/cockroach/pkg/settings/cluster" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/enum" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" @@ -72,17 +74,14 @@ func testStorage(t *testing.T) { setup := func(t *testing.T) ( *hlc.Clock, *timeutil.ManualTime, *cluster.Settings, *stop.Stopper, *slstorage.Storage, ) { - dbName := t.Name() - - tableID := newSqllivenessTable(t, tDB, dbName) - + table := newSqllivenessTable(t, tDB, s) timeSource := timeutil.NewManualTime(t0) clock := hlc.NewClockForTesting(timeSource) settings := cluster.MakeTestingClusterSettings() stopper := stop.NewStopper(stop.WithTracer(s.TracerI().(*tracing.Tracer))) var ambientCtx log.AmbientContext storage := slstorage.NewTestingStorage(ambientCtx, stopper, clock, kvDB, keys.SystemSQLCodec, settings, - tableID, rbrIndexID, timeSource.NewTimer) + s.SettingsWatcher().(*settingswatcher.SettingsWatcher), table, timeSource.NewTimer) return clock, timeSource, settings, stopper, storage } @@ -336,8 +335,8 @@ func testConcurrentAccessesAndEvictions(t *testing.T) { defer s.Stopper().Stop(ctx) tDB := sqlutils.MakeSQLRunner(sqlDB) t0 := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) - dbName := t.Name() - tableID := newSqllivenessTable(t, tDB, dbName) + + table := newSqllivenessTable(t, tDB, s) timeSource := timeutil.NewManualTime(t0) clock := hlc.NewClockForTesting(timeSource) @@ -347,7 +346,7 @@ func testConcurrentAccessesAndEvictions(t *testing.T) { slstorage.CacheSize.Override(ctx, &settings.SV, 10) var ambientCtx log.AmbientContext storage := slstorage.NewTestingStorage(ambientCtx, stopper, clock, kvDB, keys.SystemSQLCodec, settings, - tableID, rbrIndexID, timeSource.NewTimer) + s.SettingsWatcher().(*settingswatcher.SettingsWatcher), table, timeSource.NewTimer) storage.Start(ctx) const ( @@ -504,8 +503,7 @@ func testConcurrentAccessSynchronization(t *testing.T) { tDB := sqlutils.MakeSQLRunner(sqlDB) t0 := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) - dbName := t.Name() - tableID := newSqllivenessTable(t, tDB, dbName) + table := newSqllivenessTable(t, tDB, s) timeSource := timeutil.NewManualTime(t0) clock := hlc.NewClockForTesting(timeSource) @@ -515,12 +513,12 @@ func testConcurrentAccessSynchronization(t *testing.T) { slstorage.CacheSize.Override(ctx, &settings.SV, 10) var ambientCtx log.AmbientContext storage := slstorage.NewTestingStorage(ambientCtx, stopper, clock, kvDB, keys.SystemSQLCodec, settings, - tableID, rbrIndexID, timeSource.NewTimer) + s.SettingsWatcher().(*settingswatcher.SettingsWatcher), table, timeSource.NewTimer) storage.Start(ctx) // Synchronize reading from the store with the blocked channel by detecting // a Get to the table. - prefix := keys.SystemSQLCodec.TablePrefix(uint32(tableID)) + prefix := keys.SystemSQLCodec.TablePrefix(uint32(table.GetID())) var blockChannel atomic.Value var blocked int64 resetBlockedChannel := func() { blockChannel.Store(make(chan struct{})) } @@ -709,13 +707,12 @@ func testDeleteMidUpdateFails(t *testing.T) { tdb := sqlutils.MakeSQLRunner(sqlDB) // Set up a fake storage implementation using a separate table. - dbName := t.Name() - tableID := newSqllivenessTable(t, tdb, dbName) + table := newSqllivenessTable(t, tdb, s) storage := slstorage.NewTestingStorage( s.DB().AmbientContext, s.Stopper(), s.Clock(), kvDB, keys.SystemSQLCodec, s.ClusterSettings(), - tableID, rbrIndexID, timeutil.DefaultTimeSource{}.NewTimer, + s.SettingsWatcher().(*settingswatcher.SettingsWatcher), table, timeutil.DefaultTimeSource{}.NewTimer, ) // Insert a session. @@ -730,7 +727,7 @@ func testDeleteMidUpdateFails(t *testing.T) { ) *kvpb.Error { if get, ok := request.GetArg(kvpb.Get); !ok || !bytes.HasPrefix( get.(*kvpb.GetRequest).Key, - keys.SystemSQLCodec.TablePrefix(uint32(tableID)), + keys.SystemSQLCodec.TablePrefix(uint32(table.GetID())), ) { return nil } @@ -757,7 +754,7 @@ func testDeleteMidUpdateFails(t *testing.T) { unblock := <-getChan // Delete the session being updated. - tdb.Exec(t, `DELETE FROM "`+dbName+`".sqlliveness WHERE true`) + tdb.Exec(t, `DELETE FROM "`+t.Name()+`".sqlliveness WHERE true`) // Unblock the update and ensure that it saw that its session was deleted. close(unblock) @@ -766,32 +763,14 @@ func testDeleteMidUpdateFails(t *testing.T) { require.NoError(t, res.err) } -// rbrIndexID is the index id used to access the regional by row index in -// tests. In production it will be index 2, but the freshly created test table -// will have index 1. -const rbrIndexID = 1 - -func newSqllivenessTable(t *testing.T, db *sqlutils.SQLRunner, dbName string) (tableID descpb.ID) { - var schema string - if systemschema.TestSupportMultiRegion() { - schema = systemschema.MrSqllivenessTableSchema - } else { - schema = systemschema.SqllivenessTableSchema - } +func newSqllivenessTable( + t *testing.T, db *sqlutils.SQLRunner, s serverutils.TestServerInterface, +) (tableID catalog.TableDescriptor) { t.Helper() - db.Exec(t, fmt.Sprintf(`CREATE DATABASE IF NOT EXISTS "%s"`, dbName)) - tableName := "sqlliveness" - schema = strings.Replace(schema, - fmt.Sprintf("CREATE TABLE system.%s", tableName), - fmt.Sprintf(`CREATE TABLE "%s".%s`, dbName, tableName), - 1) - db.Exec(t, schema) - db.QueryRow(t, ` - select u.id - from system.namespace t - join system.namespace u - on t.id = u."parentID" - where t.name = $1 and u.name = $2`, - dbName, tableName).Scan(&tableID) - return tableID + db.Exec(t, fmt.Sprintf(`CREATE DATABASE IF NOT EXISTS "%s"`, t.Name())) + db.Exec(t, strings.Replace(systemschema.SqllivenessTableSchema, + "CREATE TABLE system.sqlliveness", + fmt.Sprintf(`CREATE TABLE "%s".sqlliveness`, t.Name()), + 1)) + return desctestutils.TestingGetTableDescriptor(s.DB(), s.Codec(), t.Name(), "public", "sqlliveness") } diff --git a/pkg/sql/tests/system_table_test.go b/pkg/sql/tests/system_table_test.go index 665c8f9117f6..2a8717512963 100644 --- a/pkg/sql/tests/system_table_test.go +++ b/pkg/sql/tests/system_table_test.go @@ -220,6 +220,12 @@ func TestSystemTableLiterals(t *testing.T) { } require.NoError(t, desctestutils.TestingValidateSelf(gen)) + switch gen.GetID() { + case keys.SqllivenessID: + gen.TableDescriptor.PrimaryIndex.ID = 2 + gen.TableDescriptor.NextIndexID = 3 + } + if desc.TableDesc().Equal(gen.TableDesc()) { return } diff --git a/pkg/sql/vars.go b/pkg/sql/vars.go index 7ca1bac29541..4cb8cc2939ff 100644 --- a/pkg/sql/vars.go +++ b/pkg/sql/vars.go @@ -2532,6 +2532,23 @@ var varGen = map[string]sessionVar{ }, GlobalDefault: globalTrue, }, + + // CockroachDB extension. + `enable_create_stats_using_extremes`: { + GetStringVal: makePostgresBoolGetStringValFn(`enable_create_stats_using_extremes`), + Set: func(_ context.Context, m sessionDataMutator, s string) error { + b, err := paramparse.ParseBoolVar("enable_create_stats_using_extremes", s) + if err != nil { + return err + } + m.SetEnableCreateStatsUsingExtremes(b) + return nil + }, + Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) { + return formatBoolAsPostgresSetting(evalCtx.SessionData().EnableCreateStatsUsingExtremes), nil + }, + GlobalDefault: globalFalse, + }, } // We want test coverage for this on and off so make it metamorphic. diff --git a/pkg/upgrade/upgrades/system_rbr_indexes.go b/pkg/upgrade/upgrades/system_rbr_indexes.go index 52a46223c836..c4b6f796baf9 100644 --- a/pkg/upgrade/upgrades/system_rbr_indexes.go +++ b/pkg/upgrade/upgrades/system_rbr_indexes.go @@ -21,6 +21,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/enum" "github.com/cockroachdb/cockroach/pkg/upgrade" "github.com/cockroachdb/cockroach/pkg/util/encoding" @@ -84,24 +85,21 @@ func cleanUpRegionalByTableIndex( } func migrations(codec keys.SQLCodec) (result []rbrMigration) { - // TODO(jeffswenson): enable the migrations along with the PRs that add - // version gates for the sub systems interacting with the table.. return []rbrMigration{ - // sqlLivenessMigration(codec), + sqlLivenessMigration(codec), // sqlInstanceMigration(codec), // leaseMigration(codec), } } -// TODO(jeffswenson): enable this migration -//func sqlLivenessMigration(codec keys.SQLCodec) rbrMigration { -// descriptor := systemschema.SqllivenessTable() -// return rbrMigration{ -// tableName: "sqlliveness", -// keyMapper: makeKeyMapper(codec, descriptor, 1), -// finalDescriptor: descriptor, -// } -//} +func sqlLivenessMigration(codec keys.SQLCodec) rbrMigration { + descriptor := systemschema.SqllivenessTable() + return rbrMigration{ + tableName: "sqlliveness", + keyMapper: makeKeyMapper(codec, descriptor, 1), + finalDescriptor: descriptor, + } +} // TODO(jeffswenson): enable this migration //func sqlInstanceMigration(codec keys.SQLCodec) rbrMigration {