@@ -571,15 +571,25 @@ pub(crate) async fn query_events_on(
571571 // Recent-first (see `recent_pass`): the newest partitions answer a page
572572 // that fills there; older rows are read only for what is still missing.
573573 let persistent = !has_tag_values ( q) ;
574- let floor = recent_pass_floor ( q, Utc :: now ( ) ) ;
575574 let limit = page_limit ( q) ;
576- let mut rows = Vec :: new ( ) ;
577- let mut pass = Some ( ( crate :: store:: recent_pass:: first ( floor) , limit) ) ;
578- while let Some ( ( range, pass_limit) ) = pass {
579- let mut qb = build_query_events_sql ( q, range, pass_limit) ;
580- rows. extend ( fetch_with_read_deadline ( conn, & mut qb, persistent, deadline) . await ?) ;
581- pass = crate :: store:: recent_pass:: next ( range, rows. len ( ) , limit) ;
582- }
575+ let rows = match recent_pass_floor ( q, Utc :: now ( ) ) {
576+ None => {
577+ let mut qb = build_query_events_sql ( q, Range :: All , limit) ;
578+ fetch_with_read_deadline ( conn, & mut qb, persistent, deadline) . await ?
579+ }
580+ Some ( floor) => {
581+ let mut read = ReadTransaction :: begin ( conn, Some ( deadline) ) . await ?;
582+ let mut rows = Vec :: new ( ) ;
583+ let mut pass = Some ( ( Range :: Recent ( floor) , limit) ) ;
584+ while let Some ( ( range, pass_limit) ) = pass {
585+ let mut qb = build_query_events_sql ( q, range, pass_limit) ;
586+ rows. extend ( read. fetch ( qb. build ( ) . persistent ( persistent) ) . await ?) ;
587+ pass = crate :: store:: recent_pass:: next ( range, rows. len ( ) , limit) ;
588+ }
589+ read. finish ( ) . await ?;
590+ rows
591+ }
592+ } ;
583593
584594 let mut out = Vec :: with_capacity ( rows. len ( ) ) ;
585595 for row in rows {
@@ -899,11 +909,11 @@ fn push_mention_join(
899909 }
900910}
901911
902- /// [`READ_STATEMENT_TIMEOUT`] as a literal, so [`BEGIN_WITH_READ_DEADLINE_SQL`]
903- /// can splice it into a static statement.
904- macro_rules! read_statement_timeout {
912+ /// [`READ_STATEMENT_TIMEOUT`] in seconds, as a literal, so
913+ /// [`BEGIN_WITH_READ_DEADLINE_SQL`] can splice it into a static statement.
914+ macro_rules! read_statement_timeout_secs {
905915 ( ) => {
906- "20s"
916+ 20
907917 } ;
908918}
909919
@@ -913,8 +923,14 @@ macro_rules! read_statement_timeout {
913923/// once no client can still be waiting for it. It only ever tightens the
914924/// effective `statement_timeout`: a shorter operator or caller cap is kept,
915925/// and a disabled (`0`) or longer one is lowered to 20 s. The limit is per
916- /// statement, not per request.
917- pub const READ_STATEMENT_TIMEOUT : & str = read_statement_timeout ! ( ) ;
926+ /// read, not per request: the passes of a split read share one (see
927+ /// [`ReadTransaction`]).
928+ pub const READ_STATEMENT_TIMEOUT : & str = concat ! ( read_statement_timeout_secs!( ) , "s" ) ;
929+
930+ /// [`READ_STATEMENT_TIMEOUT`] as a duration, for the budget a split read's
931+ /// later passes run under.
932+ const READ_DEADLINE : std:: time:: Duration =
933+ std:: time:: Duration :: from_secs ( read_statement_timeout_secs ! ( ) ) ;
918934
919935/// Lower `statement_timeout` to `$1` unless it is already tighter; `$2`
920936/// selects transaction-local (`true`) or session (`false`) scope.
@@ -923,17 +939,22 @@ pub(crate) const TIGHTEN_STATEMENT_TIMEOUT_SQL: &str = "SELECT set_config('state
923939 OR current_setting('statement_timeout')::interval > $1::text::interval \
924940 THEN $1::text ELSE current_setting('statement_timeout') END, $2)";
925941
926- /// `BEGIN` plus the [`TIGHTEN_STATEMENT_TIMEOUT_SQL`] tighten to
942+ /// Opens a [`ReadTransaction`]: `REPEATABLE READ, READ ONLY`, so every
943+ /// statement in it reads one snapshot.
944+ const BEGIN_READ_SQL : & str = "BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY" ;
945+
946+ /// [`BEGIN_READ_SQL`] plus the [`TIGHTEN_STATEMENT_TIMEOUT_SQL`] tighten to
927947/// [`READ_STATEMENT_TIMEOUT`], transaction-local, as one simple-query message:
928948/// one round trip instead of two.
929949const BEGIN_WITH_READ_DEADLINE_SQL : & str = concat ! (
930- "BEGIN; SELECT set_config('statement_timeout', \
950+ "BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY; \
951+ SELECT set_config('statement_timeout', \
931952 CASE WHEN current_setting('statement_timeout')::interval = interval '0' \
932953 OR current_setting('statement_timeout')::interval > interval '",
933- read_statement_timeout !( ) ,
934- "' THEN '" ,
935- read_statement_timeout !( ) ,
936- "' ELSE current_setting('statement_timeout') END, true)"
954+ read_statement_timeout_secs !( ) ,
955+ "s ' THEN '" ,
956+ read_statement_timeout_secs !( ) ,
957+ "s ' ELSE current_setting('statement_timeout') END, true)"
937958) ;
938959
939960/// How an event read runs under [`READ_STATEMENT_TIMEOUT`].
@@ -963,44 +984,109 @@ pub async fn tighten_session_read_statement_timeout(
963984 . map ( |_| ( ) )
964985}
965986
966- /// Run an event read under [`READ_STATEMENT_TIMEOUT`] without changing the
967- /// deadline for anything else on `conn`. Inside an open transaction the read
968- /// takes a savepoint, so a failed or cancelled read leaves the transaction
969- /// usable and the read's locks are released when it ends. With
970- /// [`ReadDeadline::PerRead`] the savepoint tightens the deadline, and an
971- /// autocommit connection opens a transaction whose `BEGIN` carries the
972- /// transaction-local tighten. Rolling either back (the read has no writes to
973- /// keep) reverts the tighten.
987+ /// The statements of one event read, in one transaction that reads one
988+ /// snapshot under one deadline.
989+ ///
990+ /// A read split at the recent-pass floor (see `recent_pass`) runs its passes
991+ /// as separate statements, and they must see the same rows: under
992+ /// `READ COMMITTED` a replacement that commits between them (a replaceable
993+ /// event, an artifact head) can move a live row from below the floor to
994+ /// above it, out of both passes. Outside a transaction this opens
995+ /// `REPEATABLE READ, READ ONLY`, whose snapshot every statement reads. Inside
996+ /// one it takes a savepoint, so a failed or cancelled statement leaves the
997+ /// transaction usable and its locks are released when the read ends; the
998+ /// callers that read inside a transaction are routed replica reads, already
999+ /// `REPEATABLE READ`.
1000+ ///
1001+ /// With a [`ReadDeadline`] the read runs under [`READ_STATEMENT_TIMEOUT`]
1002+ /// without changing the deadline for anything else on the connection, and
1003+ /// its statements share it: each after the first runs under what the earlier
1004+ /// ones left. [`ReadDeadline::PerRead`] tightens the deadline in the
1005+ /// transaction or savepoint. Rolling it back (the read has no writes to keep)
1006+ /// reverts every tighten.
1007+ pub ( crate ) struct ReadTransaction < ' c > {
1008+ tx : sqlx:: Transaction < ' c , sqlx:: Postgres > ,
1009+ deadline_from : Option < std:: time:: Instant > ,
1010+ statements : usize ,
1011+ }
1012+
1013+ impl < ' c > ReadTransaction < ' c > {
1014+ /// Open the read on `conn`; `deadline` `None` leaves `statement_timeout`
1015+ /// as it is.
1016+ pub ( crate ) async fn begin (
1017+ conn : & ' c mut sqlx:: PgConnection ,
1018+ deadline : Option < ReadDeadline > ,
1019+ ) -> Result < Self > {
1020+ use sqlx:: Connection as _;
1021+ let per_read = deadline == Some ( ReadDeadline :: PerRead ) ;
1022+ let tx = if conn. is_in_transaction ( ) {
1023+ let mut tx = conn. begin ( ) . await ?;
1024+ if per_read {
1025+ tighten_read_deadline ( & mut tx, READ_STATEMENT_TIMEOUT ) . await ?;
1026+ }
1027+ tx
1028+ } else if per_read {
1029+ conn. begin_with ( BEGIN_WITH_READ_DEADLINE_SQL ) . await ?
1030+ } else {
1031+ conn. begin_with ( BEGIN_READ_SQL ) . await ?
1032+ } ;
1033+ Ok ( Self {
1034+ tx,
1035+ deadline_from : deadline. map ( |_| std:: time:: Instant :: now ( ) ) ,
1036+ statements : 0 ,
1037+ } )
1038+ }
1039+
1040+ /// Run one statement of the read.
1041+ pub ( crate ) async fn fetch (
1042+ & mut self ,
1043+ query : sqlx:: query:: Query < ' _ , sqlx:: Postgres , sqlx:: postgres:: PgArguments > ,
1044+ ) -> Result < Vec < sqlx:: postgres:: PgRow > > {
1045+ if let Some ( from) = self . deadline_from . filter ( |_| self . statements > 0 ) {
1046+ let left = READ_DEADLINE
1047+ . saturating_sub ( from. elapsed ( ) )
1048+ . max ( std:: time:: Duration :: from_millis ( 1 ) ) ;
1049+ tighten_read_deadline ( & mut self . tx , & format ! ( "{}ms" , left. as_millis( ) ) ) . await ?;
1050+ }
1051+ self . statements += 1 ;
1052+ Ok ( query. fetch_all ( & mut * self . tx ) . await ?)
1053+ }
1054+
1055+ /// End the read, rolling back everything it set.
1056+ pub ( crate ) async fn finish ( self ) -> Result < ( ) > {
1057+ Ok ( self . tx . rollback ( ) . await ?)
1058+ }
1059+ }
1060+
1061+ /// Lower the transaction-local `statement_timeout` to `timeout`.
1062+ async fn tighten_read_deadline (
1063+ tx : & mut sqlx:: Transaction < ' _ , sqlx:: Postgres > ,
1064+ timeout : & str ,
1065+ ) -> Result < ( ) > {
1066+ sqlx:: query ( TIGHTEN_STATEMENT_TIMEOUT_SQL )
1067+ . bind ( timeout)
1068+ . bind ( true )
1069+ . execute ( & mut * * tx)
1070+ . await ?;
1071+ Ok ( ( ) )
1072+ }
1073+
1074+ /// Run a single-statement event read as a [`ReadTransaction`], except on an
1075+ /// autocommit session already under the deadline, where the statement alone
1076+ /// is the read.
9741077async fn fetch_with_read_deadline (
9751078 conn : & mut sqlx:: PgConnection ,
9761079 qb : & mut QueryBuilder < sqlx:: Postgres > ,
9771080 persistent : bool ,
9781081 deadline : ReadDeadline ,
9791082) -> Result < Vec < sqlx:: postgres:: PgRow > > {
9801083 use sqlx:: Connection as _;
981- let mut tx = match ( conn. is_in_transaction ( ) , deadline) {
982- ( false , ReadDeadline :: Session ) => {
983- return Ok ( qb. build ( ) . persistent ( persistent) . fetch_all ( conn) . await ?) ;
984- }
985- ( false , ReadDeadline :: PerRead ) => conn. begin_with ( BEGIN_WITH_READ_DEADLINE_SQL ) . await ?,
986- ( true , _) => {
987- let mut tx = conn. begin ( ) . await ?;
988- if deadline == ReadDeadline :: PerRead {
989- sqlx:: query ( TIGHTEN_STATEMENT_TIMEOUT_SQL )
990- . bind ( READ_STATEMENT_TIMEOUT )
991- . bind ( true )
992- . execute ( & mut * tx)
993- . await ?;
994- }
995- tx
996- }
997- } ;
998- let rows = qb
999- . build ( )
1000- . persistent ( persistent)
1001- . fetch_all ( & mut * tx)
1002- . await ?;
1003- tx. rollback ( ) . await ?;
1084+ if !conn. is_in_transaction ( ) && deadline == ReadDeadline :: Session {
1085+ return Ok ( qb. build ( ) . persistent ( persistent) . fetch_all ( conn) . await ?) ;
1086+ }
1087+ let mut read = ReadTransaction :: begin ( conn, Some ( deadline) ) . await ?;
1088+ let rows = read. fetch ( qb. build ( ) . persistent ( persistent) ) . await ?;
1089+ read. finish ( ) . await ?;
10041090 Ok ( rows)
10051091}
10061092
@@ -3581,6 +3667,101 @@ mod postgres_tests {
35813667 . expect ( "reset lock_timeout" ) ;
35823668 }
35833669
3670+ /// The passes of a split read share one snapshot: a replacement that
3671+ /// commits between them neither drops the row it replaces nor shows the
3672+ /// row it adds. Both deadline modes open the read the same way.
3673+ #[ tokio:: test]
3674+ #[ ignore = "requires Postgres" ]
3675+ async fn split_read_passes_share_one_snapshot ( ) {
3676+ let pool = setup_pool ( ) . await ;
3677+ let community_uuid = make_test_community ( & pool) . await ;
3678+ let community = CommunityId :: from_uuid ( community_uuid) ;
3679+ for deadline in [ ReadDeadline :: PerRead , ReadDeadline :: Session ] {
3680+ let channel = make_test_channel ( & pool, community_uuid, None ) . await ;
3681+ let q = EventQuery {
3682+ kinds : Some ( vec ! [ 9 ] ) ,
3683+ channel_id : Some ( channel) ,
3684+ limit : Some ( 10 ) ,
3685+ ..EventQuery :: for_community ( community)
3686+ } ;
3687+ let ( page, expected) = crate :: test_support:: replace_between_passes (
3688+ & pool,
3689+ community,
3690+ channel,
3691+ move |mut conn| async move {
3692+ query_events_on ( & mut conn, & q, deadline)
3693+ . await
3694+ . expect ( "split read" )
3695+ . into_iter ( )
3696+ . map ( |stored| stored. event . id )
3697+ . collect :: < Vec < _ > > ( )
3698+ } ,
3699+ )
3700+ . await ;
3701+ assert_eq ! ( page, expected, "{deadline:?}" ) ;
3702+ }
3703+ }
3704+
3705+ /// The statements of one read share its deadline: each after the first
3706+ /// runs under what the earlier ones left, and ending the read restores
3707+ /// the connection's own.
3708+ #[ tokio:: test]
3709+ #[ ignore = "requires Postgres" ]
3710+ async fn read_statements_share_one_deadline ( ) {
3711+ use sqlx:: Connection as _;
3712+ let pool = setup_pool ( ) . await ;
3713+ let timeout_ms = || {
3714+ sqlx:: query (
3715+ "SELECT (EXTRACT(EPOCH FROM current_setting('statement_timeout')::interval) \
3716+ * 1000)::float8",
3717+ )
3718+ } ;
3719+ let full = READ_DEADLINE . as_millis ( ) as f64 ;
3720+ for case in [ "per read" , "session" , "inside a transaction" ] {
3721+ let mut conn = pool. acquire ( ) . await . expect ( "acquire" ) . detach ( ) ;
3722+ let deadline = match case {
3723+ "session" => {
3724+ tighten_session_read_statement_timeout ( & mut conn)
3725+ . await
3726+ . expect ( "session deadline" ) ;
3727+ ReadDeadline :: Session
3728+ }
3729+ _ => ReadDeadline :: PerRead ,
3730+ } ;
3731+ let mut outer = None ;
3732+ let conn: & mut sqlx:: PgConnection = if case == "inside a transaction" {
3733+ outer = Some ( conn. begin ( ) . await . expect ( "begin outer" ) ) ;
3734+ outer. as_mut ( ) . expect ( "outer" )
3735+ } else {
3736+ & mut conn
3737+ } ;
3738+ let before: String = sqlx:: query_scalar ( "SHOW statement_timeout" )
3739+ . fetch_one ( & mut * conn)
3740+ . await
3741+ . expect ( "show timeout" ) ;
3742+ let mut read = ReadTransaction :: begin ( & mut * conn, Some ( deadline) )
3743+ . await
3744+ . expect ( "begin read" ) ;
3745+ let first: f64 = read. fetch ( timeout_ms ( ) ) . await . expect ( "first" ) [ 0 ] . get ( 0 ) ;
3746+ let second: f64 = read. fetch ( timeout_ms ( ) ) . await . expect ( "second" ) [ 0 ] . get ( 0 ) ;
3747+ read. finish ( ) . await . expect ( "finish" ) ;
3748+ assert_eq ! (
3749+ first, full,
3750+ "{case}: the first statement gets the whole deadline"
3751+ ) ;
3752+ assert ! (
3753+ ( 1.0 ..full) . contains( & second) ,
3754+ "{case}: the second gets what is left, got {second} ms"
3755+ ) ;
3756+ let after: String = sqlx:: query_scalar ( "SHOW statement_timeout" )
3757+ . fetch_one ( & mut * conn)
3758+ . await
3759+ . expect ( "show timeout" ) ;
3760+ assert_eq ! ( after, before, "{case}: restored" ) ;
3761+ drop ( outer) ;
3762+ }
3763+ }
3764+
35843765 /// Effective `statement_timeout` observed *inside* the read deadline, and
35853766 /// the value restored on `conn` afterwards.
35863767 async fn deadline_inside_and_after ( conn : & mut sqlx:: PgConnection ) -> ( String , String ) {
0 commit comments