Skip to content

Commit c99800e

Browse files
authored
Merge pull request #160404 from yuzefovich/blathers/backport-release-25.4-158416
release-25.4: sql: record vector index usage of index_usage_stats
2 parents a2905cd + 59ca289 commit c99800e

File tree

3 files changed

+42
-55
lines changed

3 files changed

+42
-55
lines changed

pkg/sql/distsql_spec_exec_factory.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func (e *distSQLSpecExecFactory) ConstructScan(
317317
Reverse: params.Reverse,
318318
TableDescriptorModificationTime: tabDesc.GetModificationTime(),
319319
}
320+
// TODO(yuzefovich): record the index usage when applicable.
320321
if err := rowenc.InitIndexFetchSpec(&trSpec.FetchSpec, e.planner.ExecCfg().Codec, tabDesc, idx, columnIDs); err != nil {
321322
return nil, err
322323
}
@@ -871,7 +872,7 @@ func (e *distSQLSpecExecFactory) ConstructIndexJoin(
871872
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
872873
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
873874

874-
// TODO(drewk): in an EXPLAIN context, record the index usage.
875+
// TODO(drewk): record the index usage when applicable.
875876
planInfo := &indexJoinPlanningInfo{
876877
fetch: fetch,
877878
keyCols: keyCols,
@@ -954,7 +955,7 @@ func (e *distSQLSpecExecFactory) ConstructLookupJoin(
954955
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
955956
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
956957

957-
// TODO(drewk): if in an EXPLAIN context, record the index usage.
958+
// TODO(drewk): record the index usage when applicable.
958959
planInfo := &lookupJoinPlanningInfo{
959960
fetch: fetch,
960961
joinType: joinType,
@@ -1031,7 +1032,7 @@ func (e *distSQLSpecExecFactory) ConstructInvertedJoin(
10311032
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
10321033
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
10331034

1034-
// TODO(drewk): if we're in an EXPLAIN context, record the index usage.
1035+
// TODO(drewk): record the index usage when applicable.
10351036
planInfo := &invertedJoinPlanningInfo{
10361037
fetch: fetch,
10371038
joinType: joinType,
@@ -1093,8 +1094,7 @@ func (e *distSQLSpecExecFactory) constructZigzagJoinSide(
10931094
return zigzagPlanningSide{}, err
10941095
}
10951096

1096-
// TODO (cucaroach): update indexUsageStats.
1097-
1097+
// TODO(yuzefovich): record the index usage when applicable.
10981098
return zigzagPlanningSide{
10991099
desc: desc,
11001100
index: index.(*optIndex).idx,
@@ -1524,6 +1524,7 @@ func (e *distSQLSpecExecFactory) ConstructVectorSearch(
15241524
if err != nil {
15251525
return nil, err
15261526
}
1527+
// TODO(yuzefovich): record the index usage when applicable.
15271528
planInfo := &vectorSearchPlanningInfo{
15281529
table: tabDesc,
15291530
index: indexDesc,
@@ -1562,6 +1563,7 @@ func (e *distSQLSpecExecFactory) ConstructVectorMutationSearch(
15621563
if isIndexPut {
15631564
cols = append(cols, colinfo.ResultColumn{Name: "quantized-vector", Typ: types.Bytes})
15641565
}
1566+
// TODO(yuzefovich): record the index usage when applicable.
15651567
planInfo := &vectorMutationSearchPlanningInfo{
15661568
table: table.(*optTable).desc,
15671569
index: index.(*optIndex).idx,

pkg/sql/opt/exec/execbuilder/relational.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ func (b *Builder) buildScan(scan *memo.ScanExpr) (_ execPlan, outputCols colOrdM
956956
}
957957
root, err := b.factory.ConstructScan(
958958
tab,
959-
tab.Index(scan.Index),
959+
idx,
960960
params,
961961
reqOrdering,
962962
)
@@ -1014,6 +1014,7 @@ func (b *Builder) buildPlaceholderScan(
10141014
md := b.mem.Metadata()
10151015
tab := md.Table(scan.Table)
10161016
idx := tab.Index(scan.Index)
1017+
b.IndexesUsed.add(tab.ID(), idx.ID())
10171018

10181019
// Build the index constraint.
10191020
spanColumns := make([]opt.OrderingColumn, len(scan.Span))
@@ -1051,7 +1052,7 @@ func (b *Builder) buildPlaceholderScan(
10511052
}
10521053
root, err := b.factory.ConstructScan(
10531054
tab,
1054-
tab.Index(scan.Index),
1055+
idx,
10551056
params,
10561057
reqOrdering,
10571058
)
@@ -4144,6 +4145,7 @@ func (b *Builder) buildVectorSearch(
41444145
return execPlan{}, colOrdMap{}, errors.AssertionFailedf(
41454146
"vector search is only supported on vector indexes")
41464147
}
4148+
b.IndexesUsed.add(table.ID(), index.ID())
41474149
primaryKeyCols := md.TableMeta(search.Table).IndexKeyColumns(cat.PrimaryIndex)
41484150
for col, ok := search.Cols.Next(0); ok; col, ok = search.Cols.Next(col + 1) {
41494151
if !primaryKeyCols.Contains(col) {
@@ -4190,6 +4192,7 @@ func (b *Builder) buildVectorMutationSearch(
41904192
return execPlan{}, colOrdMap{}, errors.AssertionFailedf(
41914193
"vector mutation search is only supported on vector indexes")
41924194
}
4195+
b.IndexesUsed.add(table.ID(), index.ID())
41934196

41944197
input, inputCols, err := b.buildRelational(search.Input)
41954198
if err != nil {

pkg/sql/opt_exec_factory.go

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ func (ef *execFactory) ConstructLiteralValues(
111111
}
112112
}
113113

114+
// recordIndexRead - if applicable - records the fact that the given index has
115+
// been used for reading.
116+
func (ef *execFactory) recordIndexRead(tabDesc catalog.TableDescriptor, idx catalog.Index) {
117+
if !ef.isExplain && !ef.planner.SessionData().Internal {
118+
idxUsageKey := roachpb.IndexUsageKey{
119+
TableID: roachpb.TableID(tabDesc.GetID()),
120+
IndexID: roachpb.IndexID(idx.GetID()),
121+
}
122+
ef.planner.extendedEvalCtx.indexUsageStats.RecordRead(idxUsageKey)
123+
}
124+
}
125+
114126
// ConstructScan is part of the exec.Factory interface.
115127
func (ef *execFactory) ConstructScan(
116128
table cat.Table, index cat.Index, params exec.ScanParams, reqOrdering exec.OutputOrdering,
@@ -158,13 +170,7 @@ func (ef *execFactory) ConstructScan(
158170
scan.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(params.Locking.WaitPolicy)
159171
scan.lockingDurability = descpb.ToScanLockingDurability(params.Locking.Durability)
160172
scan.localityOptimized = params.LocalityOptimized
161-
if !ef.isExplain && !ef.planner.SessionData().Internal {
162-
idxUsageKey := roachpb.IndexUsageKey{
163-
TableID: roachpb.TableID(tabDesc.GetID()),
164-
IndexID: roachpb.IndexID(idx.GetID()),
165-
}
166-
ef.planner.extendedEvalCtx.indexUsageStats.RecordRead(idxUsageKey)
167-
}
173+
ef.recordIndexRead(tabDesc, idx)
168174

169175
return scan, nil
170176
}
@@ -693,19 +699,12 @@ func (ef *execFactory) ConstructIndexJoin(
693699
}
694700

695701
idx := tabDesc.GetPrimaryIndex()
702+
ef.recordIndexRead(tabDesc, idx)
696703
fetch.index = idx
697704
fetch.lockingStrength = descpb.ToScanLockingStrength(locking.Strength)
698705
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
699706
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
700707

701-
if !ef.isExplain && !ef.planner.SessionData().Internal {
702-
idxUsageKey := roachpb.IndexUsageKey{
703-
TableID: roachpb.TableID(tabDesc.GetID()),
704-
IndexID: roachpb.IndexID(idx.GetID()),
705-
}
706-
ef.planner.extendedEvalCtx.indexUsageStats.RecordRead(idxUsageKey)
707-
}
708-
709708
n := &indexJoinNode{
710709
singleInputPlanNode: singleInputPlanNode{input.(planNode)},
711710
columns: fetch.columns,
@@ -756,19 +755,12 @@ func (ef *execFactory) ConstructLookupJoin(
756755
return nil, err
757756
}
758757

758+
ef.recordIndexRead(tabDesc, idx)
759759
fetch.index = idx
760760
fetch.lockingStrength = descpb.ToScanLockingStrength(locking.Strength)
761761
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
762762
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
763763

764-
if !ef.isExplain && !ef.planner.SessionData().Internal {
765-
idxUsageKey := roachpb.IndexUsageKey{
766-
TableID: roachpb.TableID(tabDesc.GetID()),
767-
IndexID: roachpb.IndexID(idx.GetID()),
768-
}
769-
ef.planner.extendedEvalCtx.indexUsageStats.RecordRead(idxUsageKey)
770-
}
771-
772764
n := &lookupJoinNode{
773765
singleInputPlanNode: singleInputPlanNode{input.(planNode)},
774766
lookupJoinPlanningInfo: lookupJoinPlanningInfo{
@@ -893,19 +885,12 @@ func (ef *execFactory) ConstructInvertedJoin(
893885
if err := fetch.initDescDefaults(tabDesc, colCfg); err != nil {
894886
return nil, err
895887
}
888+
ef.recordIndexRead(tabDesc, idx)
896889
fetch.index = idx
897890
fetch.lockingStrength = descpb.ToScanLockingStrength(locking.Strength)
898891
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
899892
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
900893

901-
if !ef.isExplain && !ef.planner.SessionData().Internal {
902-
idxUsageKey := roachpb.IndexUsageKey{
903-
TableID: roachpb.TableID(tabDesc.GetID()),
904-
IndexID: roachpb.IndexID(idx.GetID()),
905-
}
906-
ef.planner.extendedEvalCtx.indexUsageStats.RecordRead(idxUsageKey)
907-
}
908-
909894
n := &invertedJoinNode{
910895
singleInputPlanNode: singleInputPlanNode{input.(planNode)},
911896
invertedJoinPlanningInfo: invertedJoinPlanningInfo{
@@ -966,22 +951,15 @@ func (ef *execFactory) constructFetchForZigzag(
966951
return fetchPlanningInfo{}, nil, err
967952
}
968953

969-
tableDesc := table.(*optTable).desc
970-
idxDesc := index.(*optIndex).idx
954+
tabDesc := table.(*optTable).desc
955+
idx := index.(*optIndex).idx
971956
var fetch fetchPlanningInfo
972-
if err := fetch.initDescDefaults(tableDesc, colCfg); err != nil {
957+
if err := fetch.initDescDefaults(tabDesc, colCfg); err != nil {
973958
return fetchPlanningInfo{}, nil, err
974959
}
975960

976-
if !ef.isExplain && !ef.planner.SessionData().Internal {
977-
idxUsageKey := roachpb.IndexUsageKey{
978-
TableID: roachpb.TableID(tableDesc.GetID()),
979-
IndexID: roachpb.IndexID(idxDesc.GetID()),
980-
}
981-
ef.planner.extendedEvalCtx.indexUsageStats.RecordRead(idxUsageKey)
982-
}
983-
984-
fetch.index = idxDesc
961+
ef.recordIndexRead(tabDesc, idx)
962+
fetch.index = idx
985963
fetch.lockingStrength = descpb.ToScanLockingStrength(locking.Strength)
986964
fetch.lockingWaitPolicy = descpb.ToScanLockingWaitPolicy(locking.WaitPolicy)
987965
fetch.lockingDurability = descpb.ToScanLockingDurability(locking.Durability)
@@ -2068,23 +2046,24 @@ func (ef *execFactory) ConstructVectorSearch(
20682046
targetNeighborCount uint64,
20692047
) (exec.Node, error) {
20702048
tabDesc := table.(*optTable).desc
2071-
indexDesc := index.(*optIndex).idx
2049+
idx := index.(*optIndex).idx
20722050
cols := makeColList(table, outCols)
20732051
resultCols := colinfo.ResultColumnsFromColumns(tabDesc.GetID(), cols)
20742052

20752053
// Encode the prefix constraint as a list of roachpb.Keys.
20762054
var sb span.Builder
20772055
sb.InitAllowingExternalRowData(
2078-
ef.planner.EvalContext(), ef.planner.ExecCfg().Codec, tabDesc, indexDesc,
2056+
ef.planner.EvalContext(), ef.planner.ExecCfg().Codec, tabDesc, idx,
20792057
)
20802058
prefixKeys, err := sb.KeysFromVectorPrefixConstraint(ef.ctx, prefixConstraint)
20812059
if err != nil {
20822060
return nil, err
20832061
}
2062+
ef.recordIndexRead(tabDesc, idx)
20842063
return &vectorSearchNode{
20852064
vectorSearchPlanningInfo: vectorSearchPlanningInfo{
20862065
table: tabDesc,
2087-
index: indexDesc,
2066+
index: idx,
20882067
prefixKeys: prefixKeys,
20892068
queryVector: queryVector,
20902069
targetNeighborCount: targetNeighborCount,
@@ -2115,11 +2094,14 @@ func (ef *execFactory) ConstructVectorMutationSearch(
21152094
cols = append(cols, colinfo.ResultColumn{Name: "quantized-vector", Typ: types.Bytes})
21162095
}
21172096

2097+
tabDesc := table.(*optTable).desc
2098+
idx := index.(*optIndex).idx
2099+
ef.recordIndexRead(tabDesc, idx)
21182100
return &vectorMutationSearchNode{
21192101
singleInputPlanNode: singleInputPlanNode{input: inputPlan},
21202102
vectorMutationSearchPlanningInfo: vectorMutationSearchPlanningInfo{
2121-
table: table.(*optTable).desc,
2122-
index: index.(*optIndex).idx,
2103+
table: tabDesc,
2104+
index: idx,
21232105
prefixKeyCols: prefixKeyCols,
21242106
queryVectorCol: queryVectorCol,
21252107
suffixKeyCols: suffixKeyCols,

0 commit comments

Comments
 (0)