@@ -74,24 +74,43 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
7474 } else {
7575 crate :: postgres:: PageState :: new ( 1000 )
7676 } ;
77- let mut stream = sqlx:: query_as :: < _ , ( String , Vec < u8 > ) > (
78- "
77+ let mut stream = if let Some ( last_data_key) = & page_state. last_data_key {
78+ sqlx:: query_as :: < _ , ( String , Vec < u8 > ) > (
79+ "
7980 SELECT data_key, data_value
8081 FROM state_changes_data_compact
8182 WHERE account_id = $1
8283 AND block_height_from <= $2
8384 AND (block_height_to IS NULL OR block_height_to > $2)
84- AND ($3 IS NULL OR data_key > $3)
85+ AND data_key > $3
8586 ORDER BY
8687 data_key ASC
8788 LIMIT $4;
8889 " ,
89- )
90- . bind ( account_id. to_string ( ) )
91- . bind ( block_height as i64 ) // Convert to i64 for database compatibility
92- . bind ( page_state. last_data_key . clone ( ) )
93- . bind ( page_state. page_size )
94- . fetch ( shard_id_pool. pool ) ;
90+ )
91+ . bind ( account_id. to_string ( ) )
92+ . bind ( block_height as i64 ) // Convert to i64 for database compatibility
93+ . bind ( last_data_key. clone ( ) )
94+ . bind ( page_state. page_size )
95+ . fetch ( shard_id_pool. pool )
96+ } else {
97+ sqlx:: query_as :: < _ , ( String , Vec < u8 > ) > (
98+ "
99+ SELECT data_key, data_value
100+ FROM state_changes_data_compact
101+ WHERE account_id = $1
102+ AND block_height_from <= $2
103+ AND (block_height_to IS NULL OR block_height_to > $2)
104+ ORDER BY
105+ data_key ASC
106+ LIMIT $4;
107+ " ,
108+ )
109+ . bind ( account_id. to_string ( ) )
110+ . bind ( block_height as i64 ) // Convert to i64 for database compatibility
111+ . bind ( page_state. page_size )
112+ . fetch ( shard_id_pool. pool )
113+ } ;
95114 let mut items = std:: collections:: HashMap :: new ( ) ;
96115 let mut last_data_key = String :: new ( ) ;
97116 while let Some ( row) = stream. next ( ) . await {
0 commit comments