@@ -584,6 +584,184 @@ mod postgres_tests {
584584 . any( |child| child. name == format!( "{}_p2026_09" , table. table) ) ) ) ;
585585 }
586586
587+ #[ tokio:: test]
588+ #[ ignore = "requires Postgres" ]
589+ async fn cluster_global_parent_readers_follow_monthly_creation_and_catch_all_advancement ( ) {
590+ use crate :: store:: partition:: quote_identifier;
591+
592+ let ( pool, scratch) = scratch_pool ( ) . await ;
593+ seed_parents ( & pool) . await ;
594+ let reader = format ! ( "{}_r\" x" , scratch. name) ;
595+ let writer = format ! ( "{}_writer" , scratch. name) ;
596+ for role in [ & reader, & writer] {
597+ execute_ddl (
598+ & mut pool. acquire ( ) . await . unwrap ( ) ,
599+ format ! ( "CREATE ROLE {}" , quote_identifier( role) ) ,
600+ )
601+ . await
602+ . unwrap ( ) ;
603+ }
604+ for statement in [
605+ format ! (
606+ "GRANT USAGE ON SCHEMA {} TO {}, {}" ,
607+ scratch. name,
608+ quote_identifier( & reader) ,
609+ quote_identifier( & writer)
610+ ) ,
611+ format ! (
612+ "GRANT SELECT ON events TO {} WITH GRANT OPTION" ,
613+ quote_identifier( & reader)
614+ ) ,
615+ format ! (
616+ "GRANT INSERT, UPDATE ON events TO {}" ,
617+ quote_identifier( & reader)
618+ ) ,
619+ format ! (
620+ "GRANT INSERT, SELECT (id) ON events TO {}" ,
621+ quote_identifier( & writer)
622+ ) ,
623+ "GRANT SELECT ON delivery_log TO PUBLIC" . to_string ( ) ,
624+ "CREATE TABLE unrelated (id BIGINT)" . to_string ( ) ,
625+ ] {
626+ execute_ddl ( & mut pool. acquire ( ) . await . unwrap ( ) , statement)
627+ . await
628+ . unwrap ( ) ;
629+ }
630+ for table in PARTITIONED_TABLES {
631+ create_child (
632+ & pool,
633+ table,
634+ & format ! ( "{table}_p_past" ) ,
635+ "MINVALUE" ,
636+ "'2026-09-01'" ,
637+ )
638+ . await ;
639+ }
640+
641+ maintain_partitions_at ( & pool, 0 , CREATE_ONLY , fixed_now ( ) )
642+ . await
643+ . expect ( "create uncovered September" ) ;
644+ let can = |role : String , relation : String , privilege : & ' static str | {
645+ let pool = pool. clone ( ) ;
646+ async move {
647+ sqlx:: query_scalar :: < _ , bool > ( "SELECT has_table_privilege($1, $2, $3)" )
648+ . bind ( role)
649+ . bind ( relation)
650+ . bind ( privilege)
651+ . fetch_one ( & pool)
652+ . await
653+ . unwrap ( )
654+ }
655+ } ;
656+ let mut direct_read = pool. begin ( ) . await . unwrap ( ) ;
657+ execute_ddl (
658+ & mut direct_read,
659+ format ! ( "SET LOCAL ROLE {}" , quote_identifier( & reader) ) ,
660+ )
661+ . await
662+ . unwrap ( ) ;
663+ sqlx:: query ( "SELECT * FROM events_p2026_09 LIMIT 0" )
664+ . execute ( & mut * direct_read)
665+ . await
666+ . expect ( "parent reader can query the new child directly" ) ;
667+ direct_read. rollback ( ) . await . unwrap ( ) ;
668+ assert ! ( can( reader. clone( ) , "events_p2026_09" . into( ) , "SELECT" ) . await ) ;
669+ for privilege in [ "INSERT" , "UPDATE" , "SELECT WITH GRANT OPTION" ] {
670+ assert ! ( !can( reader. clone( ) , "events_p2026_09" . into( ) , privilege) . await ) ;
671+ }
672+ for relation in [
673+ "events_p2026_09" ,
674+ "events_p_past" ,
675+ "delivery_log_p_past" ,
676+ "unrelated" ,
677+ ] {
678+ assert ! ( !can( writer. clone( ) , relation. into( ) , "SELECT" ) . await ) ;
679+ }
680+ assert ! ( !can( reader. clone( ) , "events_p_past" . into( ) , "SELECT" ) . await ) ;
681+ assert ! ( !can( reader. clone( ) , "unrelated" . into( ) , "SELECT" ) . await ) ;
682+ assert ! ( can( writer. clone( ) , "delivery_log_p2026_09" . into( ) , "SELECT" ) . await ) ;
683+
684+ // The next rotation uses the current parent ACL, not a cached reader list.
685+ for statement in [
686+ format ! ( "REVOKE SELECT ON events FROM {}" , quote_identifier( & reader) ) ,
687+ format ! ( "GRANT SELECT ON events TO {}" , quote_identifier( & writer) ) ,
688+ ] {
689+ execute_ddl ( & mut pool. acquire ( ) . await . unwrap ( ) , statement)
690+ . await
691+ . unwrap ( ) ;
692+ }
693+ for table in PARTITIONED_TABLES {
694+ create_child (
695+ & pool,
696+ table,
697+ & format ! ( "{table}_p_future" ) ,
698+ "'2026-10-01'" ,
699+ "MAXVALUE" ,
700+ )
701+ . await ;
702+ }
703+ maintain_partitions_at ( & pool, 1 , ADVANCE , fixed_now ( ) )
704+ . await
705+ . expect ( "replace catch-all and grant new readers" ) ;
706+ for relation in [ "events_p2026_10" , "events_p_future" ] {
707+ assert ! ( can( writer. clone( ) , relation. into( ) , "SELECT" ) . await ) ;
708+ assert ! ( !can( writer. clone( ) , relation. into( ) , "INSERT" ) . await ) ;
709+ assert ! ( !can( reader. clone( ) , relation. into( ) , "SELECT" ) . await ) ;
710+ }
711+ for relation in [ "delivery_log_p2026_10" , "delivery_log_p_future" ] {
712+ assert ! ( can( reader. clone( ) , relation. into( ) , "SELECT" ) . await ) ;
713+ assert ! ( !can( reader. clone( ) , relation. into( ) , "INSERT" ) . await ) ;
714+ }
715+ let before = catalog_snapshot ( & pool) . await ;
716+ maintain_partitions_at ( & pool, 1 , ADVANCE , fixed_now ( ) )
717+ . await
718+ . unwrap ( ) ;
719+ assert_eq ! ( catalog_snapshot( & pool) . await , before) ;
720+ }
721+
722+ #[ tokio:: test]
723+ #[ ignore = "requires Postgres" ]
724+ async fn cluster_global_reader_grant_failure_rolls_back_partition_replacement ( ) {
725+ let ( pool, scratch) = scratch_pool ( ) . await ;
726+ seed_repaired_layout ( & pool, false ) . await ;
727+ sqlx:: query ( "GRANT SELECT ON events TO PUBLIC" )
728+ . execute ( & pool)
729+ . await
730+ . unwrap ( ) ;
731+ let before = catalog_snapshot ( & pool) . await ;
732+ sqlx:: query (
733+ "CREATE FUNCTION reject_partition_grant() RETURNS event_trigger \
734+ LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected grant failure'; END $$",
735+ )
736+ . execute ( & pool)
737+ . await
738+ . unwrap ( ) ;
739+ execute_ddl (
740+ & mut pool. acquire ( ) . await . unwrap ( ) ,
741+ format ! (
742+ "CREATE EVENT TRIGGER {}_grant_failure ON ddl_command_start \
743+ WHEN TAG IN ('GRANT') EXECUTE FUNCTION reject_partition_grant()",
744+ scratch. name
745+ ) ,
746+ )
747+ . await
748+ . unwrap ( ) ;
749+
750+ let result = maintain ( & pool) . await ;
751+ assert ! (
752+ matches!( result, Err ( DbError :: InvalidData ( ref message) )
753+ if message. contains( "events error" ) && message. contains( "injected grant failure" ) ) ,
754+ "{result:?}"
755+ ) ;
756+ let events = |snapshot : Vec < ( String , String , String , i64 ) > | {
757+ snapshot
758+ . into_iter ( )
759+ . filter ( |( name, ..) | name. starts_with ( "events_" ) )
760+ . collect :: < Vec < _ > > ( )
761+ } ;
762+ assert_eq ! ( events( catalog_snapshot( & pool) . await ) , events( before) ) ;
763+ }
764+
587765 #[ tokio:: test]
588766 #[ ignore = "requires Postgres" ]
589767 async fn populated_catch_all_is_refused_before_any_lock ( ) {
0 commit comments