From df42bbacfb9ee782a4a33827ab40af2ce659f0b5 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 22 Feb 2023 20:42:59 +0000 Subject: [PATCH] sql_instance: migrate to rbr compatible index Migration sql_instance to a regional by row compatible idnex. The version gates are intended to follow the protocol discussed in the comment at the top of upgrades/system_rbr_indexes.go The crdb_region column ID was changed from 5 to 6 in order to match the logical order in which the sql_addr and crdb_region columns were added. The exact ID doesn't really matter in this case since the sql_addr column was added in v23.1. Most of the rbr migration work is the same for sqlliveness, lease, and sql_instances. The main exception to that is the migration cache used by the sql instance reader. The cache is backed by a range feed and we need to switch implementations when the Version setting changes. Part of #94843 Relase note: None --- pkg/server/server_sql.go | 4 +- pkg/sql/catalog/bootstrap/testdata/testdata | 8 +- pkg/sql/catalog/systemschema/system.go | 60 ++---- .../systemschema_test/testdata/bootstrap | 5 +- .../testdata/logic_test/crdb_internal_catalog | 2 +- .../testdata/logic_test/gen_test_objects | 14 +- .../testdata/logic_test/information_schema | 4 + .../logictest/testdata/logic_test/pg_catalog | 5 +- .../sqlinstance/instancestorage/BUILD.bazel | 5 + .../instancestorage/helpers_test.go | 7 +- .../instancestorage/instancecache.go | 127 +++++++++++ .../instancestorage/instancecache_test.go | 152 ++++++++++++- .../instancestorage/instancereader.go | 5 +- .../instancestorage/instancereader_test.go | 3 +- .../instancestorage/instancestorage.go | 201 ++++++++++++++---- .../instancestorage_internal_test.go | 10 +- .../instancestorage/instancestorage_test.go | 9 +- .../sqlinstance/instancestorage/row_codec.go | 9 +- .../instancestorage/row_codec_test.go | 4 +- .../instancestorage/test_helpers.go | 16 +- pkg/sql/tests/system_table_test.go | 6 + pkg/upgrade/upgrades/system_rbr_indexes.go | 20 +- 22 files changed, 531 insertions(+), 145 deletions(-) diff --git a/pkg/server/server_sql.go b/pkg/server/server_sql.go index f69777561792..d2e7e0eab9a5 100644 --- a/pkg/server/server_sql.go +++ b/pkg/server/server_sql.go @@ -558,7 +558,9 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { ) cfg.sqlInstanceStorage = instancestorage.NewStorage( - cfg.db, codec, cfg.sqlLivenessProvider.CachedReader(), cfg.Settings, cfg.clock, cfg.rangeFeedFactory) + cfg.db, codec, cfg.sqlLivenessProvider.CachedReader(), cfg.Settings, + cfg.clock, cfg.rangeFeedFactory, settingsWatcher) + cfg.sqlInstanceReader = instancestorage.NewReader( cfg.sqlInstanceStorage, cfg.sqlLivenessProvider, diff --git a/pkg/sql/catalog/bootstrap/testdata/testdata b/pkg/sql/catalog/bootstrap/testdata/testdata index 1af08cb97881..4fda18679e6a 100644 --- a/pkg/sql/catalog/bootstrap/testdata/testdata +++ b/pkg/sql/catalog/bootstrap/testdata/testdata @@ -1,4 +1,4 @@ -system hash=e935d845b4027179deccb63f0590d74687a9f3ed1864c4f5c5e1e5a990f0357e +system hash=18874d6f76d19e14c1a9ce877372ffc6d549488ccbb7a17f238b1f6a7b89377b ---- [{"key":"04646573632d696467656e","value":"01c801"} ,{"key":"8b"} @@ -38,7 +38,7 @@ system hash=e935d845b4027179deccb63f0590d74687a9f3ed1864c4f5c5e1e5a990f0357e ,{"key":"8b89b38a89","value":"030ae60c0a167472616e73616374696f6e5f73746174697374696373182b200128013a0042330a0d616767726567617465645f747310011a0d080910001800300050a00960002000300068007000780080010088010098010042330a0e66696e6765727072696e745f696410021a0c08081000180030005011600020003000680070007800800100880100980100422d0a086170705f6e616d6510031a0c08071000180030005019600020003000680070007800800100880100980100422c0a076e6f64655f696410041a0c0801104018003000501460002000300068007000780080010088010098010042380a0c6167675f696e74657276616c10051a13080610001800300050a20960006a040800100020003000680070007800800100880100980100422e0a086d6574616461746110061a0d081210001800300050da1d60002000300068007000780080010088010098010042300a0a7374617469737469637310071a0d081210001800300050da1d60002000300068007000780080010088010098010042cf010a43637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f3810081a0c080110201800300050176000200030015a656d6f6428666e76333228637264625f696e7465726e616c2e646174756d735f746f5f627974657328616767726567617465645f74732c206170705f6e616d652c2066696e6765727072696e745f69642c206e6f64655f696429292c20383a3a3a494e543829680070007800800100880100980100480952fb020a077072696d617279100118012243637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f38220d616767726567617465645f7473220e66696e6765727072696e745f696422086170705f6e616d6522076e6f64655f69642a0c6167675f696e74657276616c2a086d657461646174612a0a7374617469737469637330083001300230033004400040004000400040004a10080010001a00200028003000380040005a007005700670077a0408002000800100880100900104980101a2017b08011243637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f381808220d616767726567617465645f747322086170705f6e616d65220e66696e6765727072696e745f696422076e6f64655f6964a80100b20100ba0100c00100c80100d00101e001005a7b0a1566696e6765727072696e745f73746174735f69647810021800220e66696e6765727072696e745f69643002380838013803380440004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060036a210a0b0a0561646d696e102018200a0a0a04726f6f741020182012046e6f64651802800101880103980100a201ef010a9701637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f3820494e2028303a3a3a494e54382c20313a3a3a494e54382c20323a3a3a494e54382c20333a3a3a494e54382c20343a3a3a494e54382c20353a3a3a494e54382c20363a3a3a494e54382c20373a3a3a494e5438291249636865636b5f637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f3818002808300038014002b201b8010a077072696d61727910001a43637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f381a0d616767726567617465645f74731a0e66696e6765727072696e745f69641a086170705f6e616d651a076e6f64655f69641a0c6167675f696e74657276616c1a086d657461646174611a0a73746174697374696373200820012002200320042005200620072800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89b48a89","value":"030ad0050a1664617461626173655f726f6c655f73657474696e6773182c200128013a0042300a0b64617461626173655f696410011a0c080c100018003000501a600020003000680070007800800100880100980100422e0a09726f6c655f6e616d6510021a0c08071000180030005019600020003000680070007800800100880100980100423e0a0873657474696e677310031a1d080f100018003000380750f1075a0c080710001800300050196000600020003000680070007800800100880100980100422c0a07726f6c655f696410041a0c080c100018003000501a60002001300068007000780080010088010098010048055288010a077072696d61727910011801220b64617461626173655f69642209726f6c655f6e616d652a0873657474696e67732a07726f6c655f696430013002400040004a10080010001a00200028003000380040005a00700370047a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00102e001005aa4010a2e64617461626173655f726f6c655f73657474696e67735f64617461626173655f69645f726f6c655f69645f6b657910021801220b64617461626173655f69642207726f6c655f69642a0873657474696e6773300130043802400040004a10080010001a00200028003000380040005a0070037a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201400a077072696d61727910001a0b64617461626173655f69641a09726f6c655f6e616d651a0873657474696e67731a07726f6c655f696420012002200320042800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89b58a89","value":"030a840a0a0c74656e616e745f7573616765182d200128013a00422e0a0974656e616e745f696410011a0c0801104018003000501460002000300068007000780080010088010098010042300a0b696e7374616e63655f696410021a0c0801104018003000501460002000300068007000780080010088010098010042350a106e6578745f696e7374616e63655f696410031a0c0801104018003000501460002000300068007000780080010088010098010042310a0b6c6173745f75706461746510041a0d080510001800300050da0860002000300068007000780080010088010098010042340a0e72755f62757273745f6c696d697410051a0d080210401800300050bd0560002001300068007000780080010088010098010042340a0e72755f726566696c6c5f7261746510061a0d080210401800300050bd0560002001300068007000780080010088010098010042300a0a72755f63757272656e7410071a0d080210401800300050bd0560002001300068007000780080010088010098010042370a1163757272656e745f73686172655f73756d10081a0d080210401800300050bd0560002001300068007000780080010088010098010042360a11746f74616c5f636f6e73756d7074696f6e10091a0c0808100018003000501160002001300068007000780080010088010098010042330a0e696e7374616e63655f6c65617365100a1a0c0808100018003000501160002001300068007000780080010088010098010042310a0c696e7374616e63655f736571100b1a0c0801104018003000501460002001300068007000780080010088010098010042350a0f696e7374616e63655f736861726573100c1a0d080210401800300050bd05600020013000680070007800800100880100980100480d52a5020a077072696d61727910011801220974656e616e745f6964220b696e7374616e63655f69642a106e6578745f696e7374616e63655f69642a0b6c6173745f7570646174652a0e72755f62757273745f6c696d69742a0e72755f726566696c6c5f726174652a0a72755f63757272656e742a1163757272656e745f73686172655f73756d2a11746f74616c5f636f6e73756d7074696f6e2a0e696e7374616e63655f6c656173652a0c696e7374616e63655f7365712a0f696e7374616e63655f73686172657330013002400040004a10080010001a00200028003000380040005a007003700470057006700770087009700a700b700c7a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201dd010a077072696d61727910001a0974656e616e745f69641a0b696e7374616e63655f69641a106e6578745f696e7374616e63655f69641a0b6c6173745f7570646174651a0e72755f62757273745f6c696d69741a0e72755f726566696c6c5f726174651a0a72755f63757272656e741a1163757272656e745f73686172655f73756d1a11746f74616c5f636f6e73756d7074696f6e1a0e696e7374616e63655f6c656173651a0c696e7374616e63655f7365711a0f696e7374616e63655f736861726573200120022003200420052006200720082009200a200b200c2800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} -,{"key":"8b89b68a89","value":"030ab4040a0d73716c5f696e7374616e636573182e200128013a0042270a02696410011a0c0801104018003000501460002000300068007000780080010088010098010042290a046164647210021a0c08071000180030005019600020013000680070007800800100880100980100422f0a0a73657373696f6e5f696410031a0c08081000180030005011600020013000680070007800800100880100980100422e0a086c6f63616c69747910041a0d081210001800300050da1d600020013000680070007800800100880100980100422d0a0873716c5f6164647210051a0c0807100018003000501960002001300068007000780080010088010098010048065287010a077072696d61727910011801220269642a04616464722a0a73657373696f6e5f69642a086c6f63616c6974792a0873716c5f61646472300140004a10080010001a00200028003000380040005a0070027003700470057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201410a077072696d61727910001a0269641a04616464721a0a73657373696f6e5f69641a086c6f63616c6974791a0873716c5f61646472200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} +,{"key":"8b89b68a89","value":"030a86050a0d73716c5f696e7374616e636573182e200128013a0042270a02696410011a0c0801104018003000501460002000300068007000780080010088010098010042290a046164647210021a0c08071000180030005019600020013000680070007800800100880100980100422f0a0a73657373696f6e5f696410031a0c08081000180030005011600020013000680070007800800100880100980100422e0a086c6f63616c69747910041a0d081210001800300050da1d600020013000680070007800800100880100980100422d0a0873716c5f6164647210051a0c0807100018003000501960002001300068007000780080010088010098010042300a0b637264625f726567696f6e10061a0c0808100018003000501160002000300068007000780080010088010098010048075298010a077072696d61727910021801220b637264625f726567696f6e220269642a04616464722a0a73657373696f6e5f69642a086c6f63616c6974792a0873716c5f6164647230063001400040004a10080010001a00200028003000380040005a0070027003700470057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201500a077072696d61727910001a0269641a04616464721a0a73657373696f6e5f69641a086c6f63616c6974791a0873716c5f616464721a0b637264625f726567696f6e2001200220032004200520062800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b78a89","value":"030aee030a137370616e5f636f6e66696775726174696f6e73182f200128013a00422e0a0973746172745f6b657910011a0c08081000180030005011600020003000680070007800800100880100980100422c0a07656e645f6b657910021a0c08081000180030005011600020003000680070007800800100880100980100422b0a06636f6e66696710031a0c08081000180030005011600020003000680070007800800100880100980100480452750a077072696d61727910011801220973746172745f6b65792a07656e645f6b65792a06636f6e666967300140004a10080010001a00200028003000380040005a00700270037a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100a2012f0a1373746172745f6b6579203c20656e645f6b6579120c636865636b5f626f756e6473180028012802300038004002b2012f0a077072696d61727910001a0973746172745f6b65791a07656e645f6b65791a06636f6e6669672001200220032800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89b88a89","value":"030abe020a0b726f6c655f69645f7365711830200128013a00422a0a0576616c756510011a0c080110401800300050146000200030006800700078008001008801009801004800525c0a077072696d61727910011800220576616c7565300140004a10080010001a00200028003000380040005a007a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060006a250a0d0a0561646d696e10a00618a0060a0c0a04726f6f7410a00618a00612046e6f64651802800100880103980100b201160a077072696d61727910001a0576616c756520012801b80100c20100e201180801106418ffffffff072064280032040800100038014200e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880300a80300b00300"} ,{"key":"8b89ba8a89","value":"030ad7050a0f74656e616e745f73657474696e67731832200128013a00422e0a0974656e616e745f696410011a0c0801104018003000501460002000300068007000780080010088010098010042290a046e616d6510021a0c08071000180030005019600020003000680070007800800100880100980100422a0a0576616c756510031a0c0807100018003000501960002000300068007000780080010088010098010042450a0c6c6173745f7570646174656410041a0d080510001800300050da08600020002a116e6f7728293a3a3a54494d455354414d503000680070007800800100880100980100422f0a0a76616c75655f7479706510051a0c08071000180030005019600020003000680070007800800100880100980100422b0a06726561736f6e10061a0c080710001800300050196000200130006800700078008001008801009801004807529b010a077072696d61727910011801220974656e616e745f696422046e616d652a0576616c75652a0c6c6173745f757064617465642a0a76616c75655f747970652a06726561736f6e30013002400040004a10080010001a00200028003000380040005a0070037004700570067a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b20185010a3966616d5f305f74656e616e745f69645f6e616d655f76616c75655f6c6173745f757064617465645f76616c75655f747970655f726561736f6e10001a0974656e616e745f69641a046e616d651a0576616c75651a0c6c6173745f757064617465641a0a76616c75655f747970651a06726561736f6e2001200220032004200520062800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} @@ -163,7 +163,7 @@ system hash=e935d845b4027179deccb63f0590d74687a9f3ed1864c4f5c5e1e5a990f0357e ,{"key":"c1"} ] -tenant hash=fa086a87fa2ecd8f485fc272ea7d85b08f30906235f83830dc446c098b895edc +tenant hash=1c06725c5438fb12f355ab31e1efa3b9d2a009a2ada035a3cef1eac011387da0 ---- [{"key":""} ,{"key":"8b89898a89","value":"0312390a0673797374656d10011a250a0d0a0561646d696e1080101880100a0c0a04726f6f7410801018801012046e6f646518022200280140004a00"} @@ -200,7 +200,7 @@ tenant hash=fa086a87fa2ecd8f485fc272ea7d85b08f30906235f83830dc446c098b895edc ,{"key":"8b89b28a89","value":"030abe140a1473746174656d656e745f73746174697374696373182a200128013a0042330a0d616767726567617465645f747310011a0d080910001800300050a00960002000300068007000780080010088010098010042330a0e66696e6765727072696e745f696410021a0c08081000180030005011600020003000680070007800800100880100980100423f0a1a7472616e73616374696f6e5f66696e6765727072696e745f696410031a0c08081000180030005011600020003000680070007800800100880100980100422e0a09706c616e5f6861736810041a0c08081000180030005011600020003000680070007800800100880100980100422d0a086170705f6e616d6510051a0c08071000180030005019600020003000680070007800800100880100980100422c0a076e6f64655f696410061a0c0801104018003000501460002000300068007000780080010088010098010042380a0c6167675f696e74657276616c10071a13080610001800300050a20960006a040800100020003000680070007800800100880100980100422e0a086d6574616461746110081a0d081210001800300050da1d60002000300068007000780080010088010098010042300a0a7374617469737469637310091a0d081210001800300050da1d600020003000680070007800800100880100980100422a0a04706c616e100a1a0d081210001800300050da1d600020003000680070007800800100880100980100429c020a68637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f38100b1a0c080110201800300050176000200030015a8c016d6f6428666e76333228637264625f696e7465726e616c2e646174756d735f746f5f627974657328616767726567617465645f74732c206170705f6e616d652c2066696e6765727072696e745f69642c206e6f64655f69642c20706c616e5f686173682c207472616e73616374696f6e5f66696e6765727072696e745f696429292c20383a3a3a494e543829680070007800800100880100980100425f0a15696e6465785f7265636f6d6d656e646174696f6e73100c1a1d080f100018003000380750f1075a0c080710001800300050196000600020002a1241525241595b5d3a3a3a535452494e475b5d3000680070007800800100880100980100426c0a0d696e64657865735f7573616765100d1a0d081210001800300050da1d6000200130005a3728737461746973746963732d3e2773746174697374696373273a3a3a535452494e47292d3e27696e6465786573273a3a3a535452494e47680070007800800101880100980100480e52bd040a077072696d617279100118012268637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f38220d616767726567617465645f7473220e66696e6765727072696e745f6964221a7472616e73616374696f6e5f66696e6765727072696e745f69642209706c616e5f6861736822086170705f6e616d6522076e6f64655f69642a0c6167675f696e74657276616c2a086d657461646174612a0a737461746973746963732a04706c616e2a15696e6465785f7265636f6d6d656e646174696f6e73300b30013002300330043005300640004000400040004000400040004a10080010001a00200028003000380040005a00700770087009700a700c7a0408002000800100880100900104980101a201c70108011268637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381808220d616767726567617465645f747322086170705f6e616d65220e66696e6765727072696e745f696422076e6f64655f69642209706c616e5f68617368221a7472616e73616374696f6e5f66696e6765727072696e745f6964a80100b20100ba0100c00100c80100d00101e001005a9d010a1566696e6765727072696e745f73746174735f69647810021800220e66696e6765727072696e745f6964221a7472616e73616374696f6e5f66696e6765727072696e745f696430023003380b3801380438053806400040004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e001005a7f0a11696e64657865735f75736167655f69647810031800220d696e64657865735f7573616765300d380b38013802380338043805380640004a10080010001a00200028003000380040005a007a0408002000800101880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100d80100e0010060046a210a0b0a0561646d696e102018200a0a0a04726f6f741020182012046e6f64651802800101880103980100a201b9020abc01637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f3820494e2028303a3a3a494e54382c20313a3a3a494e54382c20323a3a3a494e54382c20333a3a3a494e54382c20343a3a3a494e54382c20353a3a3a494e54382c20363a3a3a494e54382c20373a3a3a494e543829126e636865636b5f637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381800280b300038014002b201a9020a077072696d61727910001a68637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f706c616e5f686173685f7472616e73616374696f6e5f66696e6765727072696e745f69645f73686172645f381a0d616767726567617465645f74731a0e66696e6765727072696e745f69641a1a7472616e73616374696f6e5f66696e6765727072696e745f69641a09706c616e5f686173681a086170705f6e616d651a076e6f64655f69641a0c6167675f696e74657276616c1a086d657461646174611a0a737461746973746963731a04706c616e1a15696e6465785f7265636f6d6d656e646174696f6e73200b200120022003200420052006200720082009200a200c2800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89b38a89","value":"030ae60c0a167472616e73616374696f6e5f73746174697374696373182b200128013a0042330a0d616767726567617465645f747310011a0d080910001800300050a00960002000300068007000780080010088010098010042330a0e66696e6765727072696e745f696410021a0c08081000180030005011600020003000680070007800800100880100980100422d0a086170705f6e616d6510031a0c08071000180030005019600020003000680070007800800100880100980100422c0a076e6f64655f696410041a0c0801104018003000501460002000300068007000780080010088010098010042380a0c6167675f696e74657276616c10051a13080610001800300050a20960006a040800100020003000680070007800800100880100980100422e0a086d6574616461746110061a0d081210001800300050da1d60002000300068007000780080010088010098010042300a0a7374617469737469637310071a0d081210001800300050da1d60002000300068007000780080010088010098010042cf010a43637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f3810081a0c080110201800300050176000200030015a656d6f6428666e76333228637264625f696e7465726e616c2e646174756d735f746f5f627974657328616767726567617465645f74732c206170705f6e616d652c2066696e6765727072696e745f69642c206e6f64655f696429292c20383a3a3a494e543829680070007800800100880100980100480952fb020a077072696d617279100118012243637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f38220d616767726567617465645f7473220e66696e6765727072696e745f696422086170705f6e616d6522076e6f64655f69642a0c6167675f696e74657276616c2a086d657461646174612a0a7374617469737469637330083001300230033004400040004000400040004a10080010001a00200028003000380040005a007005700670077a0408002000800100880100900104980101a2017b08011243637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f381808220d616767726567617465645f747322086170705f6e616d65220e66696e6765727072696e745f696422076e6f64655f6964a80100b20100ba0100c00100c80100d00101e001005a7b0a1566696e6765727072696e745f73746174735f69647810021800220e66696e6765727072696e745f69643002380838013803380440004a10080010001a00200028003000380040005a007a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060036a210a0b0a0561646d696e102018200a0a0a04726f6f741020182012046e6f64651802800101880103980100a201ef010a9701637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f3820494e2028303a3a3a494e54382c20313a3a3a494e54382c20323a3a3a494e54382c20333a3a3a494e54382c20343a3a3a494e54382c20353a3a3a494e54382c20363a3a3a494e54382c20373a3a3a494e5438291249636865636b5f637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f3818002808300038014002b201b8010a077072696d61727910001a43637264625f696e7465726e616c5f616767726567617465645f74735f6170705f6e616d655f66696e6765727072696e745f69645f6e6f64655f69645f73686172645f381a0d616767726567617465645f74731a0e66696e6765727072696e745f69641a086170705f6e616d651a076e6f64655f69641a0c6167675f696e74657276616c1a086d657461646174611a0a73746174697374696373200820012002200320042005200620072800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89b48a89","value":"030ad0050a1664617461626173655f726f6c655f73657474696e6773182c200128013a0042300a0b64617461626173655f696410011a0c080c100018003000501a600020003000680070007800800100880100980100422e0a09726f6c655f6e616d6510021a0c08071000180030005019600020003000680070007800800100880100980100423e0a0873657474696e677310031a1d080f100018003000380750f1075a0c080710001800300050196000600020003000680070007800800100880100980100422c0a07726f6c655f696410041a0c080c100018003000501a60002001300068007000780080010088010098010048055288010a077072696d61727910011801220b64617461626173655f69642209726f6c655f6e616d652a0873657474696e67732a07726f6c655f696430013002400040004a10080010001a00200028003000380040005a00700370047a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00102e001005aa4010a2e64617461626173655f726f6c655f73657474696e67735f64617461626173655f69645f726f6c655f69645f6b657910021801220b64617461626173655f69642207726f6c655f69642a0873657474696e6773300130043802400040004a10080010001a00200028003000380040005a0070037a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201400a077072696d61727910001a0b64617461626173655f69641a09726f6c655f6e616d651a0873657474696e67731a07726f6c655f696420012002200320042800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} -,{"key":"8b89b68a89","value":"030ab4040a0d73716c5f696e7374616e636573182e200128013a0042270a02696410011a0c0801104018003000501460002000300068007000780080010088010098010042290a046164647210021a0c08071000180030005019600020013000680070007800800100880100980100422f0a0a73657373696f6e5f696410031a0c08081000180030005011600020013000680070007800800100880100980100422e0a086c6f63616c69747910041a0d081210001800300050da1d600020013000680070007800800100880100980100422d0a0873716c5f6164647210051a0c0807100018003000501960002001300068007000780080010088010098010048065287010a077072696d61727910011801220269642a04616464722a0a73657373696f6e5f69642a086c6f63616c6974792a0873716c5f61646472300140004a10080010001a00200028003000380040005a0070027003700470057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201410a077072696d61727910001a0269641a04616464721a0a73657373696f6e5f69641a086c6f63616c6974791a0873716c5f61646472200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} +,{"key":"8b89b68a89","value":"030a86050a0d73716c5f696e7374616e636573182e200128013a0042270a02696410011a0c0801104018003000501460002000300068007000780080010088010098010042290a046164647210021a0c08071000180030005019600020013000680070007800800100880100980100422f0a0a73657373696f6e5f696410031a0c08081000180030005011600020013000680070007800800100880100980100422e0a086c6f63616c69747910041a0d081210001800300050da1d600020013000680070007800800100880100980100422d0a0873716c5f6164647210051a0c0807100018003000501960002001300068007000780080010088010098010042300a0b637264625f726567696f6e10061a0c0808100018003000501160002000300068007000780080010088010098010048075298010a077072696d61727910021801220b637264625f726567696f6e220269642a04616464722a0a73657373696f6e5f69642a086c6f63616c6974792a0873716c5f6164647230063001400040004a10080010001a00200028003000380040005a0070027003700470057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060036a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b201500a077072696d61727910001a0269641a04616464721a0a73657373696f6e5f69641a086c6f63616c6974791a0873716c5f616464721a0b637264625f726567696f6e2001200220032004200520062800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880302a80300b00300"} ,{"key":"8b89b88a89","value":"030abe020a0b726f6c655f69645f7365711830200128013a00422a0a0576616c756510011a0c080110401800300050146000200030006800700078008001008801009801004800525c0a077072696d61727910011800220576616c7565300140004a10080010001a00200028003000380040005a007a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00100e0010060006a250a0d0a0561646d696e10a00618a0060a0c0a04726f6f7410a00618a00612046e6f64651802800100880103980100b201160a077072696d61727910001a0576616c756520012801b80100c20100e201180801106418ffffffff072064280032040800100038014200e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880300a80300b00300"} ,{"key":"8b89ba8a89","value":"030aa5030a0a7370616e5f636f756e741832200128013a0042340a0973696e676c65746f6e10011a0c08001000180030005010600020002a04747275653000680070007800800100880100980100422f0a0a7370616e5f636f756e7410021a0c080110401800300050146000200030006800700078008001008801009801004803526e0a077072696d61727910011801220973696e676c65746f6e2a0a7370616e5f636f756e74300140004a10080010001a00200028003000380040005a0070027a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00101e0010060026a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100a201210a0973696e676c65746f6e120a73696e676c655f726f7718002801300038004002b201280a077072696d61727910001a0973696e676c65746f6e1a0a7370616e5f636f756e74200120022802b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880303a80300b00300"} ,{"key":"8b89bb8a89","value":"030ab2070a0a70726976696c656765731833200128013a00422d0a08757365726e616d6510011a0c0807100018003000501960002000300068007000780080010088010098010042290a047061746810021a0c0807100018003000501960002000300068007000780080010088010098010042400a0a70726976696c6567657310031a1d080f100018003000380750f1075a0c08071000180030005019600060002000300068007000780080010088010098010042430a0d6772616e745f6f7074696f6e7310041a1d080f100018003000380750f1075a0c080710001800300050196000600020003000680070007800800100880100980100422c0a07757365725f696410051a0c080c100018003000501a60002001300068007000780080010088010098010048065293010a077072696d617279100118012208757365726e616d652204706174682a0a70726976696c656765732a0d6772616e745f6f7074696f6e732a07757365725f696430013002400040004a10080010001a00200028003000380040005a007003700470057a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c80100d00103e001005a9d010a1b70726976696c656765735f706174685f757365725f69645f6b6579100218012204706174682207757365725f69642a0a70726976696c656765732a0d6772616e745f6f7074696f6e73300230053801400040004a10080010001a00200028003000380040005a00700370047a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00101e001005a9d010a1c70726976696c656765735f706174685f757365726e616d655f6b6579100318012204706174682208757365726e616d652a0a70726976696c656765732a0d6772616e745f6f7074696f6e7330023001400040004a10080010001a00200028003000380040005a00700370047a0408002000800100880100900103980100a20106080012001800a80100b20100ba0100c00100c80100d00102e0010060046a250a0d0a0561646d696e10e00318e0030a0c0a04726f6f7410e00318e00312046e6f64651802800101880103980100b2014b0a077072696d61727910001a08757365726e616d651a04706174681a0a70726976696c656765731a0d6772616e745f6f7074696f6e731a07757365725f6964200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880304a80300b00300"} diff --git a/pkg/sql/catalog/systemschema/system.go b/pkg/sql/catalog/systemschema/system.go index a822e35a0da5..217b8f947525 100644 --- a/pkg/sql/catalog/systemschema/system.go +++ b/pkg/sql/catalog/systemschema/system.go @@ -685,20 +685,9 @@ CREATE TABLE system.sql_instances ( session_id BYTES, locality JSONB, sql_addr STRING, - CONSTRAINT "primary" PRIMARY KEY (id), - FAMILY "primary" (id, addr, session_id, locality, sql_addr) -)` - - MrSQLInstancesTableSchema = ` -CREATE TABLE system.sql_instances ( - id INT NOT NULL, - addr STRING, - session_id BYTES, - locality JSONB, crdb_region BYTES NOT NULL, - sql_addr STRING, CONSTRAINT "primary" PRIMARY KEY (crdb_region, id), - FAMILY "primary" (crdb_region, id, addr, session_id, locality, sql_addr) + FAMILY "primary" (id, addr, session_id, locality, sql_addr, crdb_region) )` SpanConfigurationsTableSchema = ` @@ -2760,39 +2749,6 @@ var ( // TODO(jeffswenson): remove the function wrapper around the // SQLInstanceTable descriptor. See TestSupportMultiRegion for context. SQLInstancesTable = func() SystemTable { - if TestSupportMultiRegion() { - return makeSystemTable( - MrSQLInstancesTableSchema, - systemTable( - catconstants.SQLInstancesTableName, - keys.SQLInstancesTableID, - []descpb.ColumnDescriptor{ - {Name: "id", ID: 1, Type: types.Int, Nullable: false}, - {Name: "addr", ID: 2, Type: types.String, Nullable: true}, - {Name: "session_id", ID: 3, Type: types.Bytes, Nullable: true}, - {Name: "locality", ID: 4, Type: types.Jsonb, Nullable: true}, - {Name: "crdb_region", ID: 5, Type: types.Bytes, Nullable: false}, - {Name: "sql_addr", ID: 6, Type: types.String, Nullable: true}, - }, - []descpb.ColumnFamilyDescriptor{ - { - Name: "primary", - ID: 0, - ColumnNames: []string{"id", "addr", "session_id", "locality", "crdb_region", "sql_addr"}, - ColumnIDs: []descpb.ColumnID{1, 2, 3, 4, 5, 6}, - DefaultColumnID: 0, - }, - }, - descpb.IndexDescriptor{ - Name: "primary", - ID: 2, - Unique: true, - KeyColumnNames: []string{"crdb_region", "id"}, - KeyColumnDirections: []catenumpb.IndexColumn_Direction{catenumpb.IndexColumn_ASC, catenumpb.IndexColumn_ASC}, - KeyColumnIDs: []descpb.ColumnID{5, 1}, - }, - )) - } return makeSystemTable( SQLInstancesTableSchema, systemTable( @@ -2804,17 +2760,25 @@ var ( {Name: "session_id", ID: 3, Type: types.Bytes, Nullable: true}, {Name: "locality", ID: 4, Type: types.Jsonb, Nullable: true}, {Name: "sql_addr", ID: 5, Type: types.String, Nullable: true}, + {Name: "crdb_region", ID: 6, Type: types.Bytes, Nullable: false}, }, []descpb.ColumnFamilyDescriptor{ { Name: "primary", ID: 0, - ColumnNames: []string{"id", "addr", "session_id", "locality", "sql_addr"}, - ColumnIDs: []descpb.ColumnID{1, 2, 3, 4, 5}, + ColumnNames: []string{"id", "addr", "session_id", "locality", "sql_addr", "crdb_region"}, + ColumnIDs: []descpb.ColumnID{1, 2, 3, 4, 5, 6}, DefaultColumnID: 0, }, }, - pk("id"), + descpb.IndexDescriptor{ + Name: "primary", + ID: 2, + Unique: true, + KeyColumnNames: []string{"crdb_region", "id"}, + KeyColumnDirections: []catenumpb.IndexColumn_Direction{catenumpb.IndexColumn_ASC, catenumpb.IndexColumn_ASC}, + KeyColumnIDs: []descpb.ColumnID{6, 1}, + }, )) } diff --git a/pkg/sql/catalog/systemschema_test/testdata/bootstrap b/pkg/sql/catalog/systemschema_test/testdata/bootstrap index 3711f0475263..e05b00a42c8a 100644 --- a/pkg/sql/catalog/systemschema_test/testdata/bootstrap +++ b/pkg/sql/catalog/systemschema_test/testdata/bootstrap @@ -375,7 +375,8 @@ CREATE TABLE public.sql_instances ( session_id BYTES NULL, locality JSONB NULL, sql_addr STRING NULL, - CONSTRAINT "primary" PRIMARY KEY (id ASC) + crdb_region BYTES NOT NULL, + CONSTRAINT "primary" PRIMARY KEY (crdb_region ASC, id ASC) ); CREATE TABLE public.span_configurations ( start_key BYTES NOT NULL, @@ -485,7 +486,7 @@ schema_telemetry {"table":{"name":"span_stats_samples","id":56,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"UuidFamily","oid":2950},"defaultExpr":"gen_random_uuid()"},{"name":"sample_time","id":2,"type":{"family":"TimestampFamily","oid":1114},"defaultExpr":"now():::TIMESTAMP"}],"nextColumnId":3,"families":[{"name":"primary","columnNames":["id","sample_time"],"columnIds":[1,2],"defaultColumnId":2}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["sample_time"],"keyColumnIds":[1],"storeColumnIds":[2],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":2},"indexes":[{"name":"samples_sample_time_idx","id":2,"unique":true,"version":3,"keyColumnNames":["sample_time"],"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":"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":"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},{"name":"crdb_region","id":6,"type":{"family":"BytesFamily","oid":17}}],"nextColumnId":7,"families":[{"name":"primary","columnNames":["id","addr","session_id","locality","sql_addr","crdb_region"],"columnIds":[1,2,3,4,5,6]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":2,"unique":true,"version":4,"keyColumnNames":["crdb_region","id"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["addr","session_id","locality","sql_addr"],"keyColumnIds":[6,1],"storeColumnIds":[2,3,4,5],"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":"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":"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}} diff --git a/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog b/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog index ff2f29d1480d..1eafdd5360cd 100644 --- a/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog +++ b/pkg/sql/logictest/testdata/logic_test/crdb_internal_catalog @@ -140,7 +140,7 @@ SELECT id, strip_volatile(descriptor) FROM crdb_internal.kv_catalog_descriptor 43 {"table": {"checks": [{"columnIds": [8], "constraintId": 2, "expr": "crdb_internal_aggregated_ts_app_name_fingerprint_id_node_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_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": "app_name", "type": {"family": "StringFamily", "oid": 25}}, {"id": 4, "name": "node_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 5, "name": "agg_interval", "type": {"family": "IntervalFamily", "intervalDurationField": {}, "oid": 1186}}, {"id": 6, "name": "metadata", "type": {"family": "JsonFamily", "oid": 3802}}, {"id": 7, "name": "statistics", "type": {"family": "JsonFamily", "oid": 3802}}, {"computeExpr": "mod(fnv32(crdb_internal.datums_to_bytes(aggregated_ts, app_name, fingerprint_id, node_id)), 8:::INT8)", "hidden": true, "id": 8, "name": "crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_shard_8", "type": {"family": "IntFamily", "oid": 23, "width": 32}}], "formatVersion": 3, "id": 43, "indexes": [{"foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [2], "keyColumnNames": ["fingerprint_id"], "keySuffixColumnIds": [8, 1, 3, 4], "name": "fingerprint_stats_idx", "partitioning": {}, "sharded": {}, "version": 3}], "name": "transaction_statistics", "nextColumnId": 9, "nextConstraintId": 3, "nextIndexId": 3, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC", "ASC", "ASC", "ASC", "ASC"], "keyColumnIds": [8, 1, 2, 3, 4], "keyColumnNames": ["crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_shard_8", "aggregated_ts", "fingerprint_id", "app_name", "node_id"], "name": "primary", "partitioning": {}, "sharded": {"columnNames": ["aggregated_ts", "app_name", "fingerprint_id", "node_id"], "isSharded": true, "name": "crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_shard_8", "shardBuckets": 8}, "storeColumnIds": [5, 6, 7], "storeColumnNames": ["agg_interval", "metadata", "statistics"], "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"}} 44 {"table": {"columns": [{"id": 1, "name": "database_id", "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "role_name", "type": {"family": "StringFamily", "oid": 25}}, {"id": 3, "name": "settings", "type": {"arrayContents": {"family": "StringFamily", "oid": 25}, "arrayElemType": "StringFamily", "family": "ArrayFamily", "oid": 1009}}, {"id": 4, "name": "role_id", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}], "formatVersion": 3, "id": 44, "indexes": [{"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [1, 4], "keyColumnNames": ["database_id", "role_id"], "keySuffixColumnIds": [2], "name": "database_role_settings_database_id_role_id_key", "partitioning": {}, "sharded": {}, "storeColumnIds": [3], "storeColumnNames": ["settings"], "unique": true, "version": 3}], "name": "database_role_settings", "nextColumnId": 5, "nextConstraintId": 3, "nextIndexId": 3, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 2, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [1, 2], "keyColumnNames": ["database_id", "role_name"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [3, 4], "storeColumnNames": ["settings", "role_id"], "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"}} 45 {"table": {"columns": [{"id": 1, "name": "tenant_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "instance_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 3, "name": "next_instance_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 4, "name": "last_update", "type": {"family": "TimestampFamily", "oid": 1114}}, {"id": 5, "name": "ru_burst_limit", "nullable": true, "type": {"family": "FloatFamily", "oid": 701, "width": 64}}, {"id": 6, "name": "ru_refill_rate", "nullable": true, "type": {"family": "FloatFamily", "oid": 701, "width": 64}}, {"id": 7, "name": "ru_current", "nullable": true, "type": {"family": "FloatFamily", "oid": 701, "width": 64}}, {"id": 8, "name": "current_share_sum", "nullable": true, "type": {"family": "FloatFamily", "oid": 701, "width": 64}}, {"id": 9, "name": "total_consumption", "nullable": true, "type": {"family": "BytesFamily", "oid": 17}}, {"id": 10, "name": "instance_lease", "nullable": true, "type": {"family": "BytesFamily", "oid": 17}}, {"id": 11, "name": "instance_seq", "nullable": true, "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 12, "name": "instance_shares", "nullable": true, "type": {"family": "FloatFamily", "oid": 701, "width": 64}}], "formatVersion": 3, "id": 45, "name": "tenant_usage", "nextColumnId": 13, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [1, 2], "keyColumnNames": ["tenant_id", "instance_id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "storeColumnNames": ["next_instance_id", "last_update", "ru_burst_limit", "ru_refill_rate", "ru_current", "current_share_sum", "total_consumption", "instance_lease", "instance_seq", "instance_shares"], "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"}} -46 {"table": {"columns": [{"id": 1, "name": "id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "addr", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}, {"id": 3, "name": "session_id", "nullable": true, "type": {"family": "BytesFamily", "oid": 17}}, {"id": 4, "name": "locality", "nullable": true, "type": {"family": "JsonFamily", "oid": 3802}}, {"id": 5, "name": "sql_addr", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 46, "name": "sql_instances", "nextColumnId": 6, "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], "storeColumnNames": ["addr", "session_id", "locality", "sql_addr"], "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"}} +46 {"table": {"columns": [{"id": 1, "name": "id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "addr", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}, {"id": 3, "name": "session_id", "nullable": true, "type": {"family": "BytesFamily", "oid": 17}}, {"id": 4, "name": "locality", "nullable": true, "type": {"family": "JsonFamily", "oid": 3802}}, {"id": 5, "name": "sql_addr", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}, {"id": 6, "name": "crdb_region", "type": {"family": "BytesFamily", "oid": 17}}], "formatVersion": 3, "id": 46, "name": "sql_instances", "nextColumnId": 7, "nextConstraintId": 2, "nextIndexId": 3, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [6, 1], "keyColumnNames": ["crdb_region", "id"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3, 4, 5], "storeColumnNames": ["addr", "session_id", "locality", "sql_addr"], "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"}} 47 {"table": {"checks": [{"columnIds": [1, 2], "constraintId": 2, "expr": "start_key < end_key", "name": "check_bounds"}], "columns": [{"id": 1, "name": "start_key", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 2, "name": "end_key", "type": {"family": "BytesFamily", "oid": 17}}, {"id": 3, "name": "config", "type": {"family": "BytesFamily", "oid": 17}}], "formatVersion": 3, "id": 47, "name": "span_configurations", "nextColumnId": 4, "nextConstraintId": 3, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["start_key"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3], "storeColumnNames": ["end_key", "config"], "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"}} 48 {"table": {"columns": [{"id": 1, "name": "value", "type": {"family": "IntFamily", "oid": 20, "width": 64}}], "formatVersion": 3, "id": 48, "name": "role_id_seq", "parentId": 1, "primaryIndex": {"encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["value"], "name": "primary", "partitioning": {}, "sharded": {}, "version": 4}, "privileges": {"ownerProto": "node", "users": [{"privileges": "800", "userProto": "admin", "withGrantOption": "800"}, {"privileges": "800", "userProto": "root", "withGrantOption": "800"}], "version": 2}, "replacementOf": {"time": {}}, "sequenceOpts": {"cacheSize": "1", "increment": "1", "maxValue": "2147483647", "minValue": "100", "sequenceOwner": {}, "start": "100"}, "unexposedParentSchemaId": 29, "version": "1"}} 50 {"table": {"columns": [{"id": 1, "name": "tenant_id", "type": {"family": "IntFamily", "oid": 20, "width": 64}}, {"id": 2, "name": "name", "type": {"family": "StringFamily", "oid": 25}}, {"id": 3, "name": "value", "type": {"family": "StringFamily", "oid": 25}}, {"defaultExpr": "now():::TIMESTAMP", "id": 4, "name": "last_updated", "type": {"family": "TimestampFamily", "oid": 1114}}, {"id": 5, "name": "value_type", "type": {"family": "StringFamily", "oid": 25}}, {"id": 6, "name": "reason", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 50, "name": "tenant_settings", "nextColumnId": 7, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "parentId": 1, "primaryIndex": {"constraintId": 1, "encodingType": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "keyColumnDirections": ["ASC", "ASC"], "keyColumnIds": [1, 2], "keyColumnNames": ["tenant_id", "name"], "name": "primary", "partitioning": {}, "sharded": {}, "storeColumnIds": [3, 4, 5, 6], "storeColumnNames": ["value", "last_updated", "value_type", "reason"], "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"}} diff --git a/pkg/sql/logictest/testdata/logic_test/gen_test_objects b/pkg/sql/logictest/testdata/logic_test/gen_test_objects index 5038b9d52580..4880a6df95a8 100644 --- a/pkg/sql/logictest/testdata/logic_test/gen_test_objects +++ b/pkg/sql/logictest/testdata/logic_test/gen_test_objects @@ -192,18 +192,18 @@ WHERE table_catalog = 'newdb2' AND table_schema = 'public' ORDER BY table_name, column_name LIMIT 20 ---- -database_role_settings "dataBase_ḯd" oid -database_role_settings "r""ole_i d" oid -database_role_settings role_name text +database_role_settings database_id oid +database_role_settings "role\\u6CFA_id" oid +database_role_settings "role_nam*e" text database_role_settings rowid bigint -database_role_settings settings ARRAY +database_role_settings "set tings" ARRAY +ev̅entlog """uniqueID" bytea ev̅entlog "eventType" text ev̅entlog info text -ev̅entlog "reportingID" bigint +ev̅entlog "reporti%55ngID" bigint ev̅entlog rowid bigint ev̅entlog "targetID" bigint -ev̅entlog "uni̹queID" bytea -ev̅entlog "🙃t%vimestamp" timestamp without time zone +ev̅entlog "timestamp" timestamp without time zone namespace "name " text namespace "parent%42ID" bigint namespace "parentSchemaID" bigint diff --git a/pkg/sql/logictest/testdata/logic_test/information_schema b/pkg/sql/logictest/testdata/logic_test/information_schema index 4889c1754a0c..1f49db8dc224 100644 --- a/pkg/sql/logictest/testdata/logic_test/information_schema +++ b/pkg/sql/logictest/testdata/logic_test/information_schema @@ -1666,6 +1666,7 @@ system public 29_54_1_not_null system public primary system public span_stats_unique_keys PRIMARY KEY NO NO system public unique_keys_key_bytes_idx system public span_stats_unique_keys UNIQUE NO NO system public 29_46_1_not_null system public sql_instances CHECK NO NO +system public 29_46_6_not_null system public sql_instances CHECK NO NO 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 @@ -1904,6 +1905,7 @@ system public 29_45_2_not_null system public 29_45_3_not_null next_instance_id IS NOT NULL system public 29_45_4_not_null last_update IS NOT NULL system public 29_46_1_not_null id IS NOT NULL +system public 29_46_6_not_null crdb_region IS NOT NULL system public 29_47_1_not_null start_key IS NOT NULL system public 29_47_2_not_null end_key IS NOT NULL system public 29_47_3_not_null config IS NOT NULL @@ -2026,6 +2028,7 @@ system public span_stats_samples sample_time system public span_stats_tenant_boundaries tenant_id system public primary 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 crdb_region system public primary system public sql_instances id system public primary system public sqlliveness session_id system public primary system public statement_bundle_chunks id system public primary @@ -2320,6 +2323,7 @@ system pg_extension spatial_ref_sys proj4text system pg_extension spatial_ref_sys srid 1 system pg_extension spatial_ref_sys srtext 4 system public sql_instances addr 2 +system public sql_instances crdb_region 6 system public sql_instances id 1 system public sql_instances locality 4 system public sql_instances session_id 3 diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index 014c7d272b75..ecd48f943766 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -1180,7 +1180,7 @@ indexrelid indrelid indnatts indisunique indnullsnotdistinct indisprimary 3613730855 43 4 true false true false true false true false false true false 1 2 3 4 0 0 3403232968 0 0 0 0 0 2 2 2 2 NULL NULL 4 3706522183 11 4 true false true false true false true false false true false 1 2 4 3 0 0 0 0 0 0 0 0 2 2 2 2 NULL NULL 4 3752917847 27 2 true false true false true false true false false true false 1 2 0 0 0 0 2 2 NULL NULL 2 -3873467122 46 1 true false true false true false true false false true false 1 0 0 2 NULL NULL 1 +3873467121 46 2 true false true false true false true false false true false 6 1 0 0 0 0 2 2 NULL NULL 2 3966258450 14 1 true false true false true false true false false true false 1 3403232968 0 2 NULL NULL 1 4012654114 30 3 true false true false true false true false false true false 1 2 3 0 0 3403232968 0 0 0 2 2 2 NULL NULL 3 4133203393 45 2 true false true false true false true false false true false 1 2 0 0 0 0 2 2 NULL NULL 2 @@ -1307,7 +1307,8 @@ indexrelid operator_argument_type_oid operator_argument_position 3706522183 0 4 3752917847 0 1 3752917847 0 2 -3873467122 0 1 +3873467121 0 1 +3873467121 0 2 3966258450 0 1 4012654114 0 1 4012654114 0 2 diff --git a/pkg/sql/sqlinstance/instancestorage/BUILD.bazel b/pkg/sql/sqlinstance/instancestorage/BUILD.bazel index 3d4d775a4fcc..0482388a37ec 100644 --- a/pkg/sql/sqlinstance/instancestorage/BUILD.bazel +++ b/pkg/sql/sqlinstance/instancestorage/BUILD.bazel @@ -14,6 +14,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/base", + "//pkg/clusterversion", "//pkg/keys", "//pkg/kv", "//pkg/kv/kvclient/rangefeed", @@ -21,6 +22,7 @@ go_library( "//pkg/kv/kvserver/concurrency/lock", "//pkg/multitenant", "//pkg/roachpb", + "//pkg/server/settingswatcher", "//pkg/settings", "//pkg/settings/cluster", "//pkg/sql", @@ -63,6 +65,7 @@ go_test( deps = [ "//pkg/base", "//pkg/ccl/kvccl/kvtenantccl", + "//pkg/clusterversion", "//pkg/keys", "//pkg/kv", "//pkg/kv/kvclient/rangefeed", @@ -70,6 +73,8 @@ go_test( "//pkg/security/securityassets", "//pkg/security/securitytest", "//pkg/server", + "//pkg/server/settingswatcher", + "//pkg/settings/cluster", "//pkg/sql/catalog/descs", "//pkg/sql/catalog/desctestutils", "//pkg/sql/catalog/systemschema", diff --git a/pkg/sql/sqlinstance/instancestorage/helpers_test.go b/pkg/sql/sqlinstance/instancestorage/helpers_test.go index 54feba3722c5..90dd9081bace 100644 --- a/pkg/sql/sqlinstance/instancestorage/helpers_test.go +++ b/pkg/sql/sqlinstance/instancestorage/helpers_test.go @@ -20,12 +20,7 @@ import ( // table schema for a given database taking into consideration whether we're // configured for the MR schema. func GetTableSQLForDatabase(dbName string) string { - schema := systemschema.SQLInstancesTableSchema - if systemschema.TestSupportMultiRegion() { - schema = systemschema.MrSQLInstancesTableSchema - } - schema = strings.Replace(schema, + return strings.Replace(systemschema.SQLInstancesTableSchema, `CREATE TABLE system.sql_instances`, `CREATE TABLE "`+dbName+`".sql_instances`, 1) - return schema } diff --git a/pkg/sql/sqlinstance/instancestorage/instancecache.go b/pkg/sql/sqlinstance/instancestorage/instancecache.go index 48b24e6a8384..257de1a55464 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancecache.go +++ b/pkg/sql/sqlinstance/instancestorage/instancecache.go @@ -15,12 +15,15 @@ import ( "strings" "github.com/cockroachdb/cockroach/pkg/base" + "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed" "github.com/cockroachdb/cockroach/pkg/kv/kvpb" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/util/grpcutil" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" + "github.com/cockroachdb/cockroach/pkg/util/stop" "github.com/cockroachdb/cockroach/pkg/util/syncutil" ) @@ -204,3 +207,127 @@ func (r *rangeFeedCache) updateInstanceMap(instance instancerow, deletionEvent b func (s *rangeFeedCache) Close() { s.feed.Close() } + +type migrationCache struct { + mu struct { + syncutil.Mutex + versionChanged bool + cache instanceCache + } +} + +// newMigrationCache uses the oldCache and newCache functions to construct +// instanceCaches. The cache registers a hook with the setting and switches +// from the old implementation to the new implementation when the version +// changes to V23_1_SystemRbrReadNew. +func newMigrationCache( + ctx context.Context, + stopper *stop.Stopper, + setting *cluster.Settings, + oldCache, newCache func(ctx context.Context) (instanceCache, error), +) (instanceCache, error) { + c := &migrationCache{} + oldReady := make(chan error, 1) + newReady := make(chan error, 1) + + onVersionChange := func(ctx context.Context, version clusterversion.ClusterVersion) { + if !version.IsActive(clusterversion.V23_1_SystemRbrReadNew) { + return + } + + c.mu.Lock() + defer c.mu.Unlock() + + if c.mu.versionChanged { + return + } + c.mu.versionChanged = true + + if c.mu.cache != nil { + log.Ops.Info(ctx, "closing old system.sql_instance cache") + c.mu.cache.Close() + } + + // Use the background context because we don't want RPC cancellation to + // prevent starting the new range feed. + err := stopper.RunAsyncTask(context.Background(), "start-new-cache-implementation", func(ctx context.Context) { + log.Ops.Info(ctx, "starting new system.sql_instance cache") + + cache, err := newCache(ctx) + if err != nil { + log.Ops.Errorf(ctx, "error starting the new system.sql_instance cache: %s", err) + newReady <- err + return + } + + c.mu.Lock() + defer c.mu.Unlock() + + log.Ops.Info(ctx, "new system.sql_instance cache is ready") + + if c.mu.cache != nil { + c.mu.cache.Close() + } + c.mu.cache = cache + newReady <- nil + }) + if err != nil { + log.Ops.Errorf(ctx, "unable to start new system.sql_instance cache: %s", err) + } + } + + // Register the hook, then run it once in case the version is already + // active. + setting.Version.SetOnChange(onVersionChange) + onVersionChange(ctx, setting.Version.ActiveVersion(ctx)) + + err := stopper.RunAsyncTask(ctx, "start-old-cache-implementation", func(ctx context.Context) { + cache, err := oldCache(ctx) + if err != nil { + oldReady <- err + } + + c.mu.Lock() + defer c.mu.Unlock() + + if c.mu.versionChanged { + cache.Close() + return + } + + c.mu.cache = cache + oldReady <- nil + }) + if err != nil { + oldReady <- err + } + + select { + case err = <-newReady: + case err = <-oldReady: + } + if err != nil { + return nil, err + } + return c, nil +} + +func (c *migrationCache) Close() { + c.mu.Lock() + defer c.mu.Unlock() + c.mu.cache.Close() +} + +func (c *migrationCache) getInstance(instanceID base.SQLInstanceID) (i instancerow, ok bool) { + c.mu.Lock() + defer c.mu.Unlock() + return c.mu.cache.getInstance(instanceID) +} + +func (c *migrationCache) listInstances() []instancerow { + c.mu.Lock() + defer c.mu.Unlock() + return c.mu.cache.listInstances() +} + +var _ instanceCache = &migrationCache{} diff --git a/pkg/sql/sqlinstance/instancestorage/instancecache_test.go b/pkg/sql/sqlinstance/instancestorage/instancecache_test.go index 14c244d13f93..687dee2c9c2a 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancecache_test.go +++ b/pkg/sql/sqlinstance/instancestorage/instancecache_test.go @@ -17,8 +17,11 @@ import ( "github.com/cockroachdb/cockroach/pkg/base" "github.com/cockroachdb/cockroach/pkg/ccl/kvccl/kvtenantccl" + "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" + "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/enum" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slstorage" @@ -27,6 +30,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/grpcutil" "github.com/cockroachdb/cockroach/pkg/util/leaktest" "github.com/cockroachdb/cockroach/pkg/util/log" + "github.com/cockroachdb/cockroach/pkg/util/stop" "github.com/stretchr/testify/require" ) @@ -82,7 +86,7 @@ func TestRangeFeed(t *testing.T) { tableDesc := desctestutils.TestingGetTableDescriptor(tenant.DB(), tenant.Codec(), t.Name(), "public", "sql_instances") slStorage := slstorage.NewFakeStorage() return NewTestingStorage(tenant.DB(), codec, tableDesc, slStorage, - tenant.ClusterSettings(), tenant.Clock(), tenant.RangeFeedFactory().(*rangefeed.Factory)) + tenant.ClusterSettings(), tenant.Clock(), tenant.RangeFeedFactory().(*rangefeed.Factory), tenant.SettingsWatcher().(*settingswatcher.SettingsWatcher)) } t.Run("success", func(t *testing.T) { @@ -90,7 +94,7 @@ func TestRangeFeed(t *testing.T) { require.NoError(t, storage.generateAvailableInstanceRows(ctx, [][]byte{enum.One}, tenant.Clock().Now().Add(int64(time.Minute), 0))) - feed, err := storage.newInstanceCache(ctx) + feed, err := storage.newInstanceCache(ctx, tenant.Stopper()) require.NoError(t, err) require.NotNil(t, feed) defer feed.Close() @@ -103,8 +107,8 @@ func TestRangeFeed(t *testing.T) { t.Run("auth_error", func(t *testing.T) { storage := newStorage(t, keys.SystemSQLCodec) - _, err := storage.newInstanceCache(ctx) - require.True(t, grpcutil.IsAuthError(err), "expected %v to be an auth error", err) + _, err := storage.newInstanceCache(ctx, tenant.Stopper()) + require.True(t, grpcutil.IsAuthError(err), "expected %+v to be an auth error", err) }) t.Run("context_cancelled", func(t *testing.T) { @@ -113,8 +117,146 @@ func TestRangeFeed(t *testing.T) { ctx, cancel := context.WithCancel(ctx) cancel() - _, err := storage.newInstanceCache(ctx) + _, err := storage.newInstanceCache(ctx, tenant.Stopper()) require.Error(t, err) require.ErrorIs(t, err, ctx.Err()) }) } + +func TestMigrationCache(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + + ctx := context.Background() + + makeVersion := func(key clusterversion.Key) clusterversion.ClusterVersion { + return clusterversion.ClusterVersion{ + Version: clusterversion.ByKey(key), + } + } + + makeCache := func(id int) instanceCache { + return &singletonInstanceFeed{instance: instancerow{ + instanceID: base.SQLInstanceID(id), + }} + } + + requireCache := func(t *testing.T, expectedID int, cache instanceCache) { + _, ok := cache.getInstance(base.SQLInstanceID(expectedID)) + require.True(t, ok, "expected %v to contain instance id %d", cache, expectedID) + } + + waitForIdle := func(t *testing.T, s *stop.Stopper) { + require.Eventually( + t, + func() bool { + return s.NumTasks() == 0 + }, + 30*time.Second, + 10*time.Millisecond, + "waiting for stopper to have no active tasks", + ) + } + + t.Run("setting_changed_before_initialization", func(t *testing.T) { + stopper := stop.NewStopper() + defer stopper.Stop(ctx) + + settings := cluster.MakeClusterSettings() + settings.Version.SetActiveVersion(ctx, makeVersion(clusterversion.V23_1_SystemRbrReadNew)) + + oldCache := 1 + newCache := 2 + + cache, err := newMigrationCache( + context.Background(), + stopper, + settings, + func(ctx context.Context) (instanceCache, error) { + return makeCache(oldCache), nil + }, + func(ctx context.Context) (instanceCache, error) { + return makeCache(newCache), nil + }) + + require.NoError(t, err) + requireCache(t, newCache, cache) + }) + + t.Run("setting_changed_after_initialization", func(t *testing.T) { + stopper := stop.NewStopper() + defer stopper.Stop(ctx) + + settings := cluster.MakeClusterSettings() + settings.Version.SetActiveVersion(ctx, makeVersion(clusterversion.V22_2)) + + oldCache := 1 + newCache := 2 + + cache, err := newMigrationCache( + context.Background(), + stopper, + settings, + func(ctx context.Context) (instanceCache, error) { + return makeCache(oldCache), nil + }, + func(ctx context.Context) (instanceCache, error) { + return makeCache(newCache), nil + }) + + require.NoError(t, err) + waitForIdle(t, stopper) + requireCache(t, oldCache, cache) + + // At this point we should still be using the old cache + settings.Version.SetActiveVersion(ctx, makeVersion(clusterversion.V23_1_SystemRbrDualWrite)) + waitForIdle(t, stopper) + requireCache(t, oldCache, cache) + + // At this point we should be using the new cache + settings.Version.SetActiveVersion(ctx, makeVersion(clusterversion.V23_1_SystemRbrReadNew)) + time.Sleep(time.Second) + waitForIdle(t, stopper) + requireCache(t, newCache, cache) + }) + + t.Run("setting_changed_during_initialization", func(t *testing.T) { + stopper := stop.NewStopper() + defer stopper.Stop(ctx) + + start := make(chan struct{}) + block := make(chan struct{}) + + settings := cluster.MakeClusterSettings() + settings.Version.SetActiveVersion(ctx, makeVersion(clusterversion.V22_2)) + + oldCache := 1 + newCache := 2 + + go func() { + <-start + settings.Version.SetActiveVersion(ctx, makeVersion(clusterversion.V23_1_SystemRbrReadNew)) + }() + + cache, err := newMigrationCache( + context.Background(), + stopper, + settings, + func(ctx context.Context) (instanceCache, error) { + close(start) + <-block + return makeCache(oldCache), nil + }, + func(ctx context.Context) (instanceCache, error) { + return makeCache(newCache), nil + }) + + require.NoError(t, err) + requireCache(t, newCache, cache) + + close(block) + waitForIdle(t, stopper) + + requireCache(t, newCache, cache) + }) +} diff --git a/pkg/sql/sqlinstance/instancestorage/instancereader.go b/pkg/sql/sqlinstance/instancestorage/instancereader.go index 6c1cc6c6ff97..df63ef2b57f1 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancereader.go +++ b/pkg/sql/sqlinstance/instancestorage/instancereader.go @@ -16,7 +16,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/base" "github.com/cockroachdb/cockroach/pkg/kv" - "github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock" "github.com/cockroachdb/cockroach/pkg/sql/sqlinstance" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -79,7 +78,7 @@ func (r *Reader) Start(ctx context.Context, self sqlinstance.InstanceInfo) { }, }) err := r.stopper.RunAsyncTask(ctx, "start-instance-reader", func(ctx context.Context) { - cache, err := r.storage.newInstanceCache(ctx) + cache, err := r.storage.newInstanceCache(ctx, r.stopper) if err != nil { r.setInitialScanDone(err) return @@ -144,7 +143,7 @@ func makeInstanceInfos(rows []instancerow) []sqlinstance.InstanceInfo { func (r *Reader) GetAllInstancesUsingTxn( ctx context.Context, txn *kv.Txn, ) ([]sqlinstance.InstanceInfo, error) { - decodedRows, err := r.storage.getInstanceRows(ctx, nil /*all regions*/, txn, lock.WaitPolicy_Block) + decodedRows, err := r.storage.getAllInstanceRows(ctx, txn) if err != nil { return nil, err } diff --git a/pkg/sql/sqlinstance/instancestorage/instancereader_test.go b/pkg/sql/sqlinstance/instancestorage/instancereader_test.go index add090ff4761..d1e59eecbb2f 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancereader_test.go +++ b/pkg/sql/sqlinstance/instancestorage/instancereader_test.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/enum" "github.com/cockroachdb/cockroach/pkg/sql/sqlinstance" @@ -57,7 +58,7 @@ func TestReader(t *testing.T) { tDB.Exec(t, schema) table := desctestutils.TestingGetPublicTableDescriptor(s.DB(), s.Codec(), dbName, "sql_instances") slStorage := slstorage.NewFakeStorage() - storage := instancestorage.NewTestingStorage(s.DB(), keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), s.Clock(), s.RangeFeedFactory().(*rangefeed.Factory)) + storage := instancestorage.NewTestingStorage(s.DB(), keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), s.Clock(), s.RangeFeedFactory().(*rangefeed.Factory), s.SettingsWatcher().(*settingswatcher.SettingsWatcher)) reader := instancestorage.NewTestingReader(storage, slStorage, s.Stopper()) return storage, slStorage, s.Clock(), reader } diff --git a/pkg/sql/sqlinstance/instancestorage/instancestorage.go b/pkg/sql/sqlinstance/instancestorage/instancestorage.go index 28e02fb030b2..a75db7f892ce 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancestorage.go +++ b/pkg/sql/sqlinstance/instancestorage/instancestorage.go @@ -19,12 +19,14 @@ import ( "time" "github.com/cockroachdb/cockroach/pkg/base" + "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock" "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" @@ -83,12 +85,14 @@ var errNoPreallocatedRows = errors.New("no preallocated rows") // session id, it is available for immediate use. It is also legal to reclaim // instances ids if the owning session has expired. type Storage struct { - db *kv.DB - slReader sqlliveness.Reader - rowcodec rowCodec - settings *cluster.Settings - clock *hlc.Clock - f *rangefeed.Factory + db *kv.DB + slReader sqlliveness.Reader + oldRowCodec rowCodec + newRowCodec rowCodec + settings *cluster.Settings + settingsWatch *settingswatcher.SettingsWatcher + clock *hlc.Clock + f *rangefeed.Factory // TestingKnobs refers to knobs used for testing. TestingKnobs struct { // JitteredIntervalFn corresponds to the function used to jitter the @@ -124,14 +128,17 @@ func NewTestingStorage( settings *cluster.Settings, clock *hlc.Clock, f *rangefeed.Factory, + settingsWatch *settingswatcher.SettingsWatcher, ) *Storage { s := &Storage{ - db: db, - rowcodec: makeRowCodec(codec, table), - slReader: slReader, - settings: settings, - clock: clock, - f: f, + db: db, + newRowCodec: makeRowCodec(codec, table, true), + oldRowCodec: makeRowCodec(codec, table, false), + slReader: slReader, + clock: clock, + f: f, + settings: settings, + settingsWatch: settingsWatch, } return s } @@ -144,8 +151,9 @@ func NewStorage( settings *cluster.Settings, clock *hlc.Clock, f *rangefeed.Factory, + settingsWatcher *settingswatcher.SettingsWatcher, ) *Storage { - return NewTestingStorage(db, codec, systemschema.SQLInstancesTable(), slReader, settings, clock, f) + return NewTestingStorage(db, codec, systemschema.SQLInstancesTable(), slReader, settings, clock, f, settingsWatcher) } // CreateNodeInstance claims a unique instance identifier for the SQL pod, and @@ -211,6 +219,11 @@ func (s *Storage) createInstanceRow( return err } + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + // Set the transaction deadline to the session expiration to ensure // transaction commits before the session expires. err = txn.UpdateDeadline(ctx, sessionExpiration) @@ -229,21 +242,29 @@ func (s *Storage) createInstanceRow( } else { // Try to retrieve an available instance ID. This blocks until one // is available. - availableID, err = s.getAvailableInstanceIDForRegion(ctx, region, txn) + availableID, err = s.getAvailableInstanceIDForRegion(ctx, region, txn, &version) if err != nil { return err } } - key := s.rowcodec.encodeKey(region, availableID) - value, err := s.rowcodec.encodeValue(rpcAddr, sqlAddr, sessionID, locality) + b := txn.NewBatch() + + rowCodec := s.getReadCodec(&version) + value, err := rowCodec.encodeValue(rpcAddr, sqlAddr, sessionID, locality) if err != nil { - log.Warningf(ctx, "failed to encode row for instance id %d: %v", availableID, err) return err } + b.Put(rowCodec.encodeKey(region, availableID), value) + + if dualCodec := s.getDualWriteCodec(&version); dualCodec != nil { + dualValue, err := dualCodec.encodeValue(rpcAddr, sqlAddr, sessionID, locality) + if err != nil { + return err + } + b.Put(dualCodec.encodeKey(region, availableID), dualValue) + } - b := txn.NewBatch() - b.Put(key, value) return txn.CommitInBatch(ctx, b) }); err != nil { return base.SQLInstanceID(0), err @@ -298,17 +319,28 @@ func (s *Storage) createInstanceRow( // newInstanceCache constructs an instanceCache backed by a range feed over the // sql_instances table. newInstanceCache blocks until the initial scan is // complete. -func (s *Storage) newInstanceCache(ctx context.Context) (instanceCache, error) { - return newRangeFeedCache(ctx, s.rowcodec, s.clock, s.f) +func (s *Storage) newInstanceCache( + ctx context.Context, stopper *stop.Stopper, +) (instanceCache, error) { + if !s.settings.Version.IsActive(ctx, clusterversion.V23_1_SystemRbrReadNew) { + oldCache := func(ctx context.Context) (instanceCache, error) { + return newRangeFeedCache(ctx, s.oldRowCodec, s.clock, s.f) + } + newCache := func(ctx context.Context) (instanceCache, error) { + return newRangeFeedCache(ctx, s.newRowCodec, s.clock, s.f) + } + return newMigrationCache(ctx, stopper, s.settings, oldCache, newCache) + } + return newRangeFeedCache(ctx, s.newRowCodec, s.clock, s.f) } // getAvailableInstanceIDForRegion retrieves an available instance ID for the // current region associated with Storage s, and returns errNoPreallocatedRows // if there are no available rows. func (s *Storage) getAvailableInstanceIDForRegion( - ctx context.Context, region []byte, txn *kv.Txn, + ctx context.Context, region []byte, txn *kv.Txn, version *settingswatcher.VersionGuard, ) (base.SQLInstanceID, error) { - rows, err := s.getInstanceRows(ctx, region, txn, lock.WaitPolicy_SkipLocked) + rows, err := s.getInstanceRows(ctx, region, version, txn, lock.WaitPolicy_SkipLocked) if err != nil { return base.SQLInstanceID(0), err } @@ -345,8 +377,11 @@ func (s *Storage) reclaimRegion(ctx context.Context, region []byte) error { // never become active again. var instances []instancerow if err := s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - var err error - instances, err = s.getInstanceRows(ctx, region, txn, lock.WaitPolicy_Block) + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + instances, err = s.getInstanceRows(ctx, region, &version, txn, lock.WaitPolicy_Block) return err }); err != nil { return err @@ -365,29 +400,58 @@ func (s *Storage) reclaimRegion(ctx context.Context, region []byte) error { // Reclaim and delete rows target := int(PreallocatedCount.Get(&s.settings.SV)) return s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - instances, err := s.getInstanceRows(ctx, region, txn, lock.WaitPolicy_Block) + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + + instances, err := s.getInstanceRows(ctx, region, &version, txn, lock.WaitPolicy_Block) if err != nil { return err } toReclaim, toDelete := idsToReclaim(target, instances, isExpired) + readCodec := s.getReadCodec(&version) + dualCodec := s.getDualWriteCodec(&version) + writeBatch := txn.NewBatch() for _, instance := range toReclaim { - availableValue, err := s.rowcodec.encodeAvailableValue() + availableValue, err := readCodec.encodeAvailableValue() if err != nil { return err } - writeBatch.Put(s.rowcodec.encodeKey(region, instance), availableValue) + writeBatch.Put(readCodec.encodeKey(region, instance), availableValue) + + if dualCodec != nil { + dualValue, err := dualCodec.encodeAvailableValue() + if err != nil { + return err + } + writeBatch.Put(dualCodec.encodeKey(region, instance), dualValue) + } } for _, instance := range toDelete { - writeBatch.Del(s.rowcodec.encodeKey(region, instance)) + writeBatch.Del(readCodec.encodeKey(region, instance)) + if dualCodec != nil { + writeBatch.Del(dualCodec.encodeKey(region, instance)) + } } return txn.CommitInBatch(ctx, writeBatch) }) } +// getAllInstanceRows returns all instance rows, including instance rows that +// are pre-allocated. +func (s *Storage) getAllInstanceRows(ctx context.Context, txn *kv.Txn) ([]instancerow, error) { + version, err := s.versionGuard(ctx, txn) + if err != nil { + return nil, err + } + return s.getInstanceRows(ctx, nil, &version, txn, lock.WaitPolicy_Block) +} + // getInstanceRows decodes and returns all instance rows associated // with a given region from the sql_instances table. This returns both used and // available instance rows. @@ -396,13 +460,19 @@ func (s *Storage) reclaimRegion(ctx context.Context, region []byte) error { // case where multiple instances attempt to initialize their instance IDs // simultaneously. func (s *Storage) getInstanceRows( - ctx context.Context, region []byte, txn *kv.Txn, waitPolicy lock.WaitPolicy, + ctx context.Context, + region []byte, + version *settingswatcher.VersionGuard, + txn *kv.Txn, + waitPolicy lock.WaitPolicy, ) ([]instancerow, error) { + rowCodec := s.getReadCodec(version) + var start roachpb.Key if region == nil { - start = s.rowcodec.makeIndexPrefix() + start = rowCodec.makeIndexPrefix() } else { - start = s.rowcodec.makeRegionPrefix(region) + start = rowCodec.makeRegionPrefix(region) } // Scan the entire range @@ -424,7 +494,7 @@ func (s *Storage) getInstanceRows( instances := make([]instancerow, len(rows)) for i := range rows { var err error - instances[i], err = s.rowcodec.decodeRow(rows[i].Key, rows[i].Value) + instances[i], err = rowCodec.decodeRow(rows[i].Key, rows[i].Value) if err != nil { return nil, err } @@ -440,9 +510,23 @@ func (s *Storage) ReleaseInstanceID( ) error { // TODO(andrei): Ensure that we do not delete an instance ID that we no longer // own, instead of deleting blindly. - key := s.rowcodec.encodeKey(region, id) ctx = multitenant.WithTenantCostControlExemption(ctx) - if _, err := s.db.Del(ctx, key); err != nil { + err := s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + + b := txn.NewBatch() + + readCodec := s.getReadCodec(&version) + b.Del(readCodec.encodeKey(region, id)) + if dualCodec := s.getDualWriteCodec(&version); dualCodec != nil { + b.Del(dualCodec.encodeKey(region, id)) + } + return txn.CommitInBatch(ctx, b) + }) + if err != nil { return errors.Wrapf(err, "could not delete instance %d", id) } return nil @@ -531,6 +615,32 @@ func (s *Storage) RunInstanceIDReclaimLoop( }) } +func (s *Storage) getReadCodec(version *settingswatcher.VersionGuard) *rowCodec { + if version.IsActive(clusterversion.V23_1_SystemRbrReadNew) { + return &s.newRowCodec + } + return &s.oldRowCodec +} + +func (s *Storage) getDualWriteCodec(version *settingswatcher.VersionGuard) *rowCodec { + switch { + case version.IsActive(clusterversion.V23_1_SystemRbrSingleWrite): + return nil + case version.IsActive(clusterversion.V23_1_SystemRbrReadNew): + return &s.oldRowCodec + case version.IsActive(clusterversion.V23_1_SystemRbrDualWrite): + return &s.newRowCodec + default: + return nil + } +} + +func (s *Storage) versionGuard( + ctx context.Context, txn *kv.Txn, +) (settingswatcher.VersionGuard, error) { + return s.settingsWatch.MakeVersionGuard(ctx, txn, clusterversion.V23_1_SystemRbrCleanup) +} + // generateAvailableInstanceRows allocates available instance IDs, and store // them in the sql_instances table. When instance IDs are pre-allocated, all // other fields in that row will be NULL. @@ -540,18 +650,33 @@ func (s *Storage) generateAvailableInstanceRows( ctx = multitenant.WithTenantCostControlExemption(ctx) target := int(PreallocatedCount.Get(&s.settings.SV)) return s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - instances, err := s.getInstanceRows(ctx, nil /*global*/, txn, lock.WaitPolicy_Block) + version, err := s.versionGuard(ctx, txn) if err != nil { return err } + instances, err := s.getInstanceRows(ctx, nil /*global*/, &version, txn, lock.WaitPolicy_Block) + if err != nil { + return err + } + + readCodec := s.getReadCodec(&version) + dualCodec := s.getDualWriteCodec(&version) + b := txn.NewBatch() for _, row := range idsToAllocate(target, regions, instances) { - value, err := s.rowcodec.encodeAvailableValue() + value, err := readCodec.encodeAvailableValue() if err != nil { return errors.Wrapf(err, "failed to encode row for instance id %d", row.instanceID) } - b.Put(s.rowcodec.encodeKey(row.region, row.instanceID), value) + b.Put(readCodec.encodeKey(row.region, row.instanceID), value) + if dualCodec != nil { + dualValue, err := dualCodec.encodeAvailableValue() + if err != nil { + return errors.Wrapf(err, "failed to encode dual write row for instance id %d", row.instanceID) + } + b.Put(dualCodec.encodeKey(row.region, row.instanceID), dualValue) + } } return txn.CommitInBatch(ctx, b) }) diff --git a/pkg/sql/sqlinstance/instancestorage/instancestorage_internal_test.go b/pkg/sql/sqlinstance/instancestorage/instancestorage_internal_test.go index ca44063b8dea..1b7fc613d29b 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancestorage_internal_test.go +++ b/pkg/sql/sqlinstance/instancestorage/instancestorage_internal_test.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/enum" "github.com/cockroachdb/cockroach/pkg/sql/sqlinstance" @@ -55,7 +56,12 @@ func TestGetAvailableInstanceIDForRegion(t *testing.T) { getAvailableInstanceID := func(storage *Storage, region []byte) (id base.SQLInstanceID, err error) { err = storage.db.Txn(context.Background(), func(ctx context.Context, txn *kv.Txn) error { - id, err = storage.getAvailableInstanceIDForRegion(ctx, region, txn) + version, err := storage.versionGuard(ctx, txn) + if err != nil { + return err + } + + id, err = storage.getAvailableInstanceIDForRegion(ctx, region, txn, &version) return err }) return @@ -451,7 +457,7 @@ func setup( stopper := stop.NewStopper() slStorage := slstorage.NewFakeStorage() f := s.RangeFeedFactory().(*rangefeed.Factory) - storage := NewTestingStorage(s.DB(), keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), clock, f) + storage := NewTestingStorage(s.DB(), keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), clock, f, s.SettingsWatcher().(*settingswatcher.SettingsWatcher)) return stopper, storage, slStorage, clock } diff --git a/pkg/sql/sqlinstance/instancestorage/instancestorage_test.go b/pkg/sql/sqlinstance/instancestorage/instancestorage_test.go index ae4ca4916fa7..18dc6b851aa8 100644 --- a/pkg/sql/sqlinstance/instancestorage/instancestorage_test.go +++ b/pkg/sql/sqlinstance/instancestorage/instancestorage_test.go @@ -24,6 +24,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/server/settingswatcher" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" @@ -79,7 +80,7 @@ func TestStorage(t *testing.T) { stopper := stop.NewStopper() slStorage := slstorage.NewFakeStorage() f := s.RangeFeedFactory().(*rangefeed.Factory) - storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), s.Clock(), f) + storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), s.Clock(), f, s.SettingsWatcher().(*settingswatcher.SettingsWatcher)) return stopper, storage, slStorage, clock } @@ -286,7 +287,7 @@ func TestSQLAccess(t *testing.T) { defer stopper.Stop(ctx) f := s.RangeFeedFactory().(*rangefeed.Factory) storage := instancestorage.NewTestingStorage( - kvDB, keys.SystemSQLCodec, table, slstorage.NewFakeStorage(), s.ClusterSettings(), s.Clock(), f) + kvDB, keys.SystemSQLCodec, table, slstorage.NewFakeStorage(), s.ClusterSettings(), s.Clock(), f, s.SettingsWatcher().(*settingswatcher.SettingsWatcher)) const ( tierStr = "region=test1,zone=test2" expiration = time.Minute @@ -412,7 +413,7 @@ func TestConcurrentCreateAndRelease(t *testing.T) { slStorage := slstorage.NewFakeStorage() defer stopper.Stop(ctx) f := s.RangeFeedFactory().(*rangefeed.Factory) - storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), s.Clock(), f) + storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, table, slStorage, s.ClusterSettings(), s.Clock(), f, s.SettingsWatcher().(*settingswatcher.SettingsWatcher)) instancestorage.PreallocatedCount.Override(ctx, &s.ClusterSettings().SV, 1) const ( @@ -558,7 +559,7 @@ func TestReclaimLoop(t *testing.T) { tableID := desctestutils.TestingGetPublicTableDescriptor(kvDB, s.Codec(), dbName, "sql_instances") slStorage := slstorage.NewFakeStorage() f := s.RangeFeedFactory().(*rangefeed.Factory) - storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, tableID, slStorage, s.ClusterSettings(), s.Clock(), f) + storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, tableID, slStorage, s.ClusterSettings(), s.Clock(), f, s.SettingsWatcher().(*settingswatcher.SettingsWatcher)) storage.TestingKnobs.JitteredIntervalFn = func(d time.Duration) time.Duration { // For deterministic tests. return d diff --git a/pkg/sql/sqlinstance/instancestorage/row_codec.go b/pkg/sql/sqlinstance/instancestorage/row_codec.go index c2c615f7a6a5..5be86db90cc0 100644 --- a/pkg/sql/sqlinstance/instancestorage/row_codec.go +++ b/pkg/sql/sqlinstance/instancestorage/row_codec.go @@ -150,16 +150,19 @@ func (c *rbtKeyCodec) decodeKey(key roachpb.Key) (region []byte, id base.SQLInst } // MakeRowCodec makes a new rowCodec for the sql_instances table. -func makeRowCodec(codec keys.SQLCodec, table catalog.TableDescriptor) rowCodec { +func makeRowCodec( + codec keys.SQLCodec, table catalog.TableDescriptor, useRegionalByRow bool, +) rowCodec { columns := table.PublicColumns() var key keyCodec - if catalog.FindColumnByName(table, "crdb_region") != nil { + if useRegionalByRow { key = &rbrKeyCodec{ indexPrefix: codec.IndexPrefix(uint32(table.GetID()), uint32(table.GetPrimaryIndexID())), } } else { + const rbtIndexID = 1 key = &rbtKeyCodec{ - indexPrefix: codec.IndexPrefix(uint32(table.GetID()), uint32(table.GetPrimaryIndexID())), + indexPrefix: codec.IndexPrefix(uint32(table.GetID()), uint32(rbtIndexID)), } } rc := rowCodec{ diff --git a/pkg/sql/sqlinstance/instancestorage/row_codec_test.go b/pkg/sql/sqlinstance/instancestorage/row_codec_test.go index b9c5208eacd7..ecc3e6b70750 100644 --- a/pkg/sql/sqlinstance/instancestorage/row_codec_test.go +++ b/pkg/sql/sqlinstance/instancestorage/row_codec_test.go @@ -40,11 +40,11 @@ func TestRowCodec(t *testing.T) { t.Run("RegionalByRow", func(t *testing.T) { defer envutil.TestSetEnv(t, "COCKROACH_MR_SYSTEM_DATABASE", "1")() - testEncoder(t, makeRowCodec(codec, systemschema.SQLInstancesTable()), tenantID) + testEncoder(t, makeRowCodec(codec, systemschema.SQLInstancesTable(), true), tenantID) }) t.Run("RegionalByTable", func(t *testing.T) { defer envutil.TestSetEnv(t, "COCKROACH_MR_SYSTEM_DATABASE", "0")() - testEncoder(t, makeRowCodec(codec, systemschema.SQLInstancesTable()), tenantID) + testEncoder(t, makeRowCodec(codec, systemschema.SQLInstancesTable(), false), tenantID) }) } diff --git a/pkg/sql/sqlinstance/instancestorage/test_helpers.go b/pkg/sql/sqlinstance/instancestorage/test_helpers.go index db673a8f1cec..b80e62bf0db3 100644 --- a/pkg/sql/sqlinstance/instancestorage/test_helpers.go +++ b/pkg/sql/sqlinstance/instancestorage/test_helpers.go @@ -95,8 +95,9 @@ func (s *Storage) CreateInstanceDataForTest( if err != nil { return err } - key := s.rowcodec.encodeKey(region, instanceID) - value, err := s.rowcodec.encodeValue(rpcAddr, sqlAddr, sessionID, locality) + + key := s.newRowCodec.encodeKey(region, instanceID) + value, err := s.newRowCodec.encodeValue(rpcAddr, sqlAddr, sessionID, locality) if err != nil { return err } @@ -111,7 +112,7 @@ func (s *Storage) CreateInstanceDataForTest( func (s *Storage) GetInstanceDataForTest( ctx context.Context, region []byte, instanceID base.SQLInstanceID, ) (sqlinstance.InstanceInfo, error) { - k := s.rowcodec.encodeKey(region, instanceID) + k := s.newRowCodec.encodeKey(region, instanceID) ctx = multitenant.WithTenantCostControlExemption(ctx) row, err := s.db.Get(ctx, k) if err != nil { @@ -120,7 +121,7 @@ func (s *Storage) GetInstanceDataForTest( if row.Value == nil { return sqlinstance.InstanceInfo{}, sqlinstance.NonExistentInstanceError } - rpcAddr, sqlAddr, sessionID, locality, _, err := s.rowcodec.decodeValue(*row.Value) + rpcAddr, sqlAddr, sessionID, locality, _, err := s.newRowCodec.decodeValue(*row.Value) if err != nil { return sqlinstance.InstanceInfo{}, errors.Wrapf(err, "could not decode data for instance %d", instanceID) } @@ -142,8 +143,11 @@ func (s *Storage) GetAllInstancesDataForTest( var rows []instancerow ctx = multitenant.WithTenantCostControlExemption(ctx) if err := s.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - var err error - rows, err = s.getInstanceRows(ctx, nil /*global*/, txn, lock.WaitPolicy_Block) + version, err := s.versionGuard(ctx, txn) + if err != nil { + return err + } + rows, err = s.getInstanceRows(ctx, nil /*global*/, &version, txn, lock.WaitPolicy_Block) return err }); err != nil { return nil, err diff --git a/pkg/sql/tests/system_table_test.go b/pkg/sql/tests/system_table_test.go index 665c8f9117f6..6078ca84b042 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.SQLInstancesTableID: + gen.TableDescriptor.PrimaryIndex.ID = 2 + gen.TableDescriptor.NextIndexID = 3 + } + if desc.TableDesc().Equal(gen.TableDesc()) { return } diff --git a/pkg/upgrade/upgrades/system_rbr_indexes.go b/pkg/upgrade/upgrades/system_rbr_indexes.go index 52a46223c836..c7f77d1654e2 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" @@ -88,7 +89,7 @@ func migrations(codec keys.SQLCodec) (result []rbrMigration) { // version gates for the sub systems interacting with the table.. return []rbrMigration{ // sqlLivenessMigration(codec), - // sqlInstanceMigration(codec), + sqlInstanceMigration(codec), // leaseMigration(codec), } } @@ -103,15 +104,14 @@ func migrations(codec keys.SQLCodec) (result []rbrMigration) { // } //} -// TODO(jeffswenson): enable this migration -//func sqlInstanceMigration(codec keys.SQLCodec) rbrMigration { -// descriptor := systemschema.SQLInstancesTable() -// return rbrMigration{ -// tableName: "sql_instances", -// keyMapper: makeKeyMapper(codec, descriptor, 1), -// finalDescriptor: descriptor, -// } -//} +func sqlInstanceMigration(codec keys.SQLCodec) rbrMigration { + descriptor := systemschema.SQLInstancesTable() + return rbrMigration{ + tableName: "sql_instances", + keyMapper: makeKeyMapper(codec, descriptor, 1), + finalDescriptor: descriptor, + } +} // TODO(jeffswenson): enable this migration //func leaseMigration(codec keys.SQLCodec) rbrMigration {