@@ -7,16 +7,13 @@ use super::{
77 tx_state:: TxState ,
88 SharedMutexGuard , SharedWriteGuard ,
99} ;
10- use crate :: db:: {
11- datastore:: {
12- system_tables:: {
13- table_name_is_system, StColumnFields , StColumnRow , StConstraintFields , StConstraintRow , StFields as _,
14- StIndexFields , StIndexRow , StScheduledRow , StSequenceFields , StSequenceRow , StTableFields , StTableRow ,
15- SystemTable , ST_COLUMN_ID , ST_CONSTRAINT_ID , ST_INDEX_ID , ST_SCHEDULED_ID , ST_SEQUENCE_ID , ST_TABLE_ID ,
16- } ,
17- traits:: { RowTypeForTable , TxData } ,
10+ use crate :: db:: datastore:: {
11+ system_tables:: {
12+ table_name_is_system, StColumnFields , StColumnRow , StConstraintFields , StConstraintRow , StFields as _,
13+ StIndexFields , StIndexRow , StScheduledRow , StSequenceFields , StSequenceRow , StTableFields , StTableRow ,
14+ SystemTable , ST_COLUMN_ID , ST_CONSTRAINT_ID , ST_INDEX_ID , ST_SCHEDULED_ID , ST_SEQUENCE_ID , ST_TABLE_ID ,
1815 } ,
19- db_metrics :: table_num_rows ,
16+ traits :: { RowTypeForTable , TxData } ,
2017} ;
2118use crate :: {
2219 error:: { DBError , IndexError , SequenceError , TableError } ,
@@ -1082,6 +1079,18 @@ impl StateView for MutTxId {
10821079 . map ( |table| table. get_schema ( ) )
10831080 }
10841081
1082+ fn table_row_count ( & self , table_id : TableId ) -> Option < u64 > {
1083+ let commit_count = self . committed_state_write_lock . table_row_count ( table_id) ;
1084+ let ( tx_ins_count, tx_del_count) = self . tx_state . table_row_count ( table_id) ;
1085+ let commit_count = commit_count. map ( |cc| cc - tx_del_count) ;
1086+ // Keep track of whether `table_id` exists.
1087+ match ( commit_count, tx_ins_count) {
1088+ ( Some ( cc) , Some ( ic) ) => Some ( cc + ic) ,
1089+ ( Some ( c) , None ) | ( None , Some ( c) ) => Some ( c) ,
1090+ ( None , None ) => None ,
1091+ }
1092+ }
1093+
10851094 fn iter < ' a > ( & ' a self , ctx : & ' a ExecutionContext , table_id : TableId ) -> Result < Iter < ' a > > {
10861095 if let Some ( table_name) = self . table_name ( table_id) {
10871096 return Ok ( Iter :: new (
@@ -1137,29 +1146,31 @@ impl StateView for MutTxId {
11371146 ) ) ) ,
11381147 None => {
11391148 #[ cfg( feature = "unindexed_iter_by_col_range_warn" ) ]
1140- match self . schema_for_table ( ctx , table_id) {
1149+ match self . table_row_count ( table_id) {
11411150 // TODO(ux): log these warnings to the module logs rather than host logs.
1142- Err ( e ) => log:: error!(
1143- "iter_by_col_range on unindexed column, but got error from `schema_for_table` during diagnostics: {e:?} " ,
1151+ None => log:: error!(
1152+ "iter_by_col_range on unindexed column, but couldn't fetch table `{table_id}`s row count " ,
11441153 ) ,
1145- Ok ( schema ) => {
1154+ Some ( num_rows ) => {
11461155 const TOO_MANY_ROWS_FOR_SCAN : u64 = 1000 ;
1147-
1148- let table_name = & schema. table_name ;
1149- let num_rows = table_num_rows ( ctx. database ( ) , table_id, table_name) ;
1150-
11511156 if num_rows >= TOO_MANY_ROWS_FOR_SCAN {
1152- let col_names = cols. iter ( )
1153- . map ( |col_id| schema. columns ( )
1154- . get ( col_id. idx ( ) )
1155- . map ( |col| & col. col_name [ ..] )
1156- . unwrap_or ( "[unknown column]" ) )
1157+ let schema = self . schema_for_table ( ctx, table_id) . unwrap ( ) ;
1158+ let table_name = & schema. table_name ;
1159+ let col_names = cols
1160+ . iter ( )
1161+ . map ( |col_id| {
1162+ schema
1163+ . columns ( )
1164+ . get ( col_id. idx ( ) )
1165+ . map ( |col| & col. col_name [ ..] )
1166+ . unwrap_or ( "[unknown column]" )
1167+ } )
11571168 . collect :: < Vec < _ > > ( ) ;
11581169 log:: warn!(
11591170 "iter_by_col_range without index: table {table_name} has {num_rows} rows; scanning columns {col_names:?}" ,
11601171 ) ;
11611172 }
1162- } ,
1173+ }
11631174 }
11641175
11651176 Ok ( IterByColRange :: Scan ( ScanIterByColRange :: new (
0 commit comments