Skip to content

Commit

Permalink
upgrades,systemschema: add covering index to system.privileges on path
Browse files Browse the repository at this point in the history
Queries to the system.privileges table are usually
filtered by path. With a primary key using (user,path)
all those queries were full scans.  This commit adds a
secondary covering index to the system.privileges table
and a migration to upgrade the table.

Release note: None
  • Loading branch information
e-mbrown committed Feb 22, 2023
1 parent e9e513d commit ba38bb9
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,4 @@ trace.jaeger.agent string the address of a Jaeger agent to receive traces using
trace.opentelemetry.collector string address of an OpenTelemetry trace collector to receive traces using the otel gRPC protocol, as <host>:<port>. If no port is specified, 4317 will be used.
trace.span_registry.enabled boolean true if set, ongoing traces can be seen at https://<ui>/#/debug/tracez
trace.zipkin.collector string the address of a Zipkin instance to receive traces, as <host>:<port>. If no port is specified, 9411 will be used.
version version 1000022.2-50 set the active cluster version in the format '<major>.<minor>'
version version 1000022.2-52 set the active cluster version in the format '<major>.<minor>'
2 changes: 1 addition & 1 deletion docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,6 @@
<tr><td><div id="setting-trace-opentelemetry-collector" class="anchored"><code>trace.opentelemetry.collector</code></div></td><td>string</td><td><code></code></td><td>address of an OpenTelemetry trace collector to receive traces using the otel gRPC protocol, as &lt;host&gt;:&lt;port&gt;. If no port is specified, 4317 will be used.</td></tr>
<tr><td><div id="setting-trace-span-registry-enabled" class="anchored"><code>trace.span_registry.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>if set, ongoing traces can be seen at https://&lt;ui&gt;/#/debug/tracez</td></tr>
<tr><td><div id="setting-trace-zipkin-collector" class="anchored"><code>trace.zipkin.collector</code></div></td><td>string</td><td><code></code></td><td>the address of a Zipkin instance to receive traces, as &lt;host&gt;:&lt;port&gt;. If no port is specified, 9411 will be used.</td></tr>
<tr><td><div id="setting-version" class="anchored"><code>version</code></div></td><td>version</td><td><code>1000022.2-50</code></td><td>set the active cluster version in the format &#39;&lt;major&gt;.&lt;minor&gt;&#39;</td></tr>
<tr><td><div id="setting-version" class="anchored"><code>version</code></div></td><td>version</td><td><code>1000022.2-52</code></td><td>set the active cluster version in the format &#39;&lt;major&gt;.&lt;minor&gt;&#39;</td></tr>
</tbody>
</table>
8 changes: 8 additions & 0 deletions pkg/clusterversion/cockroach_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ const (
// elements.
V23_1_SchemaChangerDeprecatedIndexPredicates

// V23_1AlterSystemPrivilegesAddIndexOnPathAndUsername adds a covering secondary index to
// system.privileges, on the path and username columns.
V23_1AlterSystemPrivilegesAddIndexOnPathAndUsername

// *************************************************
// Step (1): Add new versions here.
// Do not add new versions to a patch release.
Expand Down Expand Up @@ -744,6 +748,10 @@ var rawVersionsSingleton = keyedVersions{
Key: V23_1_SchemaChangerDeprecatedIndexPredicates,
Version: roachpb.Version{Major: 22, Minor: 2, Internal: 50},
},
{
Key: V23_1AlterSystemPrivilegesAddIndexOnPathAndUsername,
Version: roachpb.Version{Major: 22, Minor: 2, Internal: 52},
},

// *************************************************
// Step (2): Add new versions here.
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/catalog/bootstrap/testdata/testdata

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions pkg/sql/catalog/systemschema/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ CREATE TABLE system.privileges (
user_id OID,
CONSTRAINT "primary" PRIMARY KEY (username, path),
UNIQUE INDEX (path, user_id) STORING (privileges, grant_options),
UNIQUE INDEX (path, username) STORING (privileges, grant_options),
FAMILY "primary" (username, path, privileges, grant_options, user_id)
);`

Expand Down Expand Up @@ -2943,6 +2944,17 @@ var (
StoreColumnIDs: []descpb.ColumnID{3, 4},
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
},
descpb.IndexDescriptor{
Name: "privileges_path_username_key",
ID: 3,
Unique: true,
KeyColumnNames: []string{"path", "username"},
KeyColumnDirections: []catenumpb.IndexColumn_Direction{catenumpb.IndexColumn_ASC, catenumpb.IndexColumn_ASC},
KeyColumnIDs: []descpb.ColumnID{2, 1},
StoreColumnNames: []string{"privileges", "grant_options"},
StoreColumnIDs: []descpb.ColumnID{3, 4},
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
},
),
)

Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/catalog/systemschema_test/testdata/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ CREATE TABLE public.privileges (
grant_options STRING[] NOT NULL,
user_id OID NULL,
CONSTRAINT "primary" PRIMARY KEY (username ASC, path ASC),
UNIQUE INDEX privileges_path_user_id_key (path ASC, user_id ASC) STORING (privileges, grant_options)
UNIQUE INDEX privileges_path_user_id_key (path ASC, user_id ASC) STORING (privileges, grant_options),
UNIQUE INDEX privileges_path_username_key (path ASC, username ASC) STORING (privileges, grant_options)
);
CREATE TABLE public.external_connections (
connection_name STRING NOT NULL,
Expand Down Expand Up @@ -460,7 +461,7 @@ schema_telemetry
{"table":{"name":"locations","id":21,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"localityKey","id":1,"type":{"family":"StringFamily","oid":25}},{"name":"localityValue","id":2,"type":{"family":"StringFamily","oid":25}},{"name":"latitude","id":3,"type":{"family":"DecimalFamily","width":15,"precision":18,"oid":1700}},{"name":"longitude","id":4,"type":{"family":"DecimalFamily","width":15,"precision":18,"oid":1700}}],"nextColumnId":5,"families":[{"name":"fam_0_localityKey_localityValue_latitude_longitude","columnNames":["localityKey","localityValue","latitude","longitude"],"columnIds":[1,2,3,4]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["localityKey","localityValue"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["latitude","longitude"],"keyColumnIds":[1,2],"storeColumnIds":[3,4],"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":"migrations","id":40,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"major","id":1,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"minor","id":2,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"patch","id":3,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"internal","id":4,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"completed_at","id":5,"type":{"family":"TimestampTZFamily","oid":1184}}],"nextColumnId":6,"families":[{"name":"primary","columnNames":["major","minor","patch","internal","completed_at"],"columnIds":[1,2,3,4,5],"defaultColumnId":5}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["major","minor","patch","internal"],"keyColumnDirections":["ASC","ASC","ASC","ASC"],"storeColumnNames":["completed_at"],"keyColumnIds":[1,2,3,4],"storeColumnIds":[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":"namespace","id":30,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"parentID","id":1,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"parentSchemaID","id":2,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"name","id":3,"type":{"family":"StringFamily","oid":25}},{"name":"id","id":4,"type":{"family":"IntFamily","width":64,"oid":20},"nullable":true}],"nextColumnId":5,"families":[{"name":"primary","columnNames":["parentID","parentSchemaID","name"],"columnIds":[1,2,3]},{"name":"fam_4_id","id":4,"columnNames":["id"],"columnIds":[4],"defaultColumnId":4}],"nextFamilyId":5,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["parentID","parentSchemaID","name"],"keyColumnDirections":["ASC","ASC","ASC"],"storeColumnNames":["id"],"keyColumnIds":[1,2,3],"storeColumnIds":[4],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"32","withGrantOption":"32"},{"userProto":"root","privileges":"32","withGrantOption":"32"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}}
{"table":{"name":"privileges","id":51,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"username","id":1,"type":{"family":"StringFamily","oid":25}},{"name":"path","id":2,"type":{"family":"StringFamily","oid":25}},{"name":"privileges","id":3,"type":{"family":"ArrayFamily","arrayElemType":"StringFamily","oid":1009,"arrayContents":{"family":"StringFamily","oid":25}}},{"name":"grant_options","id":4,"type":{"family":"ArrayFamily","arrayElemType":"StringFamily","oid":1009,"arrayContents":{"family":"StringFamily","oid":25}}},{"name":"user_id","id":5,"type":{"family":"OidFamily","oid":26},"nullable":true}],"nextColumnId":6,"families":[{"name":"primary","columnNames":["username","path","privileges","grant_options","user_id"],"columnIds":[1,2,3,4,5]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["username","path"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["privileges","grant_options","user_id"],"keyColumnIds":[1,2],"storeColumnIds":[3,4,5],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":2},"indexes":[{"name":"privileges_path_user_id_key","id":2,"unique":true,"version":3,"keyColumnNames":["path","user_id"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["privileges","grant_options"],"keyColumnIds":[2,5],"keySuffixColumnIds":[1],"storeColumnIds":[3,4],"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":"privileges","id":51,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"username","id":1,"type":{"family":"StringFamily","oid":25}},{"name":"path","id":2,"type":{"family":"StringFamily","oid":25}},{"name":"privileges","id":3,"type":{"family":"ArrayFamily","arrayElemType":"StringFamily","oid":1009,"arrayContents":{"family":"StringFamily","oid":25}}},{"name":"grant_options","id":4,"type":{"family":"ArrayFamily","arrayElemType":"StringFamily","oid":1009,"arrayContents":{"family":"StringFamily","oid":25}}},{"name":"user_id","id":5,"type":{"family":"OidFamily","oid":26},"nullable":true}],"nextColumnId":6,"families":[{"name":"primary","columnNames":["username","path","privileges","grant_options","user_id"],"columnIds":[1,2,3,4,5]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["username","path"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["privileges","grant_options","user_id"],"keyColumnIds":[1,2],"storeColumnIds":[3,4,5],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":3},"indexes":[{"name":"privileges_path_user_id_key","id":2,"unique":true,"version":3,"keyColumnNames":["path","user_id"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["privileges","grant_options"],"keyColumnIds":[2,5],"keySuffixColumnIds":[1],"storeColumnIds":[3,4],"foreignKey":{},"interleave":{},"partitioning":{},"sharded":{},"geoConfig":{},"constraintId":1},{"name":"privileges_path_username_key","id":3,"unique":true,"version":3,"keyColumnNames":["path","username"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["privileges","grant_options"],"keyColumnIds":[2,1],"storeColumnIds":[3,4],"foreignKey":{},"interleave":{},"partitioning":{},"sharded":{},"geoConfig":{},"constraintId":2}],"nextIndexId":4,"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":4}}
{"table":{"name":"protected_ts_meta","id":31,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"singleton","id":1,"type":{"oid":16},"defaultExpr":"true"},{"name":"version","id":2,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"num_records","id":3,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"num_spans","id":4,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"total_bytes","id":5,"type":{"family":"IntFamily","width":64,"oid":20}}],"nextColumnId":6,"families":[{"name":"primary","columnNames":["singleton","version","num_records","num_spans","total_bytes"],"columnIds":[1,2,3,4,5]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["singleton"],"keyColumnDirections":["ASC"],"storeColumnNames":["version","num_records","num_spans","total_bytes"],"keyColumnIds":[1],"storeColumnIds":[2,3,4,5],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"32","withGrantOption":"32"},{"userProto":"root","privileges":"32","withGrantOption":"32"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"checks":[{"expr":"singleton","name":"check_singleton","columnIds":[1],"constraintId":2}],"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":3}}
{"table":{"name":"protected_ts_records","id":32,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"id","id":1,"type":{"family":"UuidFamily","oid":2950}},{"name":"ts","id":2,"type":{"family":"DecimalFamily","oid":1700}},{"name":"meta_type","id":3,"type":{"family":"StringFamily","oid":25}},{"name":"meta","id":4,"type":{"family":"BytesFamily","oid":17},"nullable":true},{"name":"num_spans","id":5,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"spans","id":6,"type":{"family":"BytesFamily","oid":17}},{"name":"verified","id":7,"type":{"oid":16},"defaultExpr":"false"},{"name":"target","id":8,"type":{"family":"BytesFamily","oid":17},"nullable":true}],"nextColumnId":9,"families":[{"name":"primary","columnNames":["id","ts","meta_type","meta","num_spans","spans","verified","target"],"columnIds":[1,2,3,4,5,6,7,8]}],"nextFamilyId":1,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["id"],"keyColumnDirections":["ASC"],"storeColumnNames":["ts","meta_type","meta","num_spans","spans","verified","target"],"keyColumnIds":[1],"storeColumnIds":[2,3,4,5,6,7,8],"foreignKey":{},"interleave":{},"partitioning":{},"encodingType":1,"sharded":{},"geoConfig":{},"constraintId":1},"nextIndexId":2,"privileges":{"users":[{"userProto":"admin","privileges":"32","withGrantOption":"32"},{"userProto":"root","privileges":"32","withGrantOption":"32"}],"ownerProto":"node","version":2},"nextMutationId":1,"formatVersion":3,"replacementOf":{"time":{}},"createAsOfTime":{"wallTime":"0"},"nextConstraintId":2}}
{"table":{"name":"rangelog","id":13,"version":"1","modificationTime":{"wallTime":"0"},"parentId":1,"unexposedParentSchemaId":29,"columns":[{"name":"timestamp","id":1,"type":{"family":"TimestampFamily","oid":1114}},{"name":"rangeID","id":2,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"storeID","id":3,"type":{"family":"IntFamily","width":64,"oid":20}},{"name":"eventType","id":4,"type":{"family":"StringFamily","oid":25}},{"name":"otherRangeID","id":5,"type":{"family":"IntFamily","width":64,"oid":20},"nullable":true},{"name":"info","id":6,"type":{"family":"StringFamily","oid":25},"nullable":true},{"name":"uniqueID","id":7,"type":{"family":"IntFamily","width":64,"oid":20},"defaultExpr":"unique_rowid()"}],"nextColumnId":8,"families":[{"name":"primary","columnNames":["timestamp","uniqueID"],"columnIds":[1,7]},{"name":"fam_2_rangeID","id":2,"columnNames":["rangeID"],"columnIds":[2],"defaultColumnId":2},{"name":"fam_3_storeID","id":3,"columnNames":["storeID"],"columnIds":[3],"defaultColumnId":3},{"name":"fam_4_eventType","id":4,"columnNames":["eventType"],"columnIds":[4],"defaultColumnId":4},{"name":"fam_5_otherRangeID","id":5,"columnNames":["otherRangeID"],"columnIds":[5],"defaultColumnId":5},{"name":"fam_6_info","id":6,"columnNames":["info"],"columnIds":[6],"defaultColumnId":6}],"nextFamilyId":7,"primaryIndex":{"name":"primary","id":1,"unique":true,"version":4,"keyColumnNames":["timestamp","uniqueID"],"keyColumnDirections":["ASC","ASC"],"storeColumnNames":["rangeID","storeID","eventType","otherRangeID","info"],"keyColumnIds":[1,7],"storeColumnIds":[2,3,4,5,6],"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}}
Expand Down
Loading

0 comments on commit ba38bb9

Please sign in to comment.