11//! A collection of replicas and a primary.
22
33use parking_lot:: Mutex ;
4- use pgdog_config:: { PreparedStatements , QueryParserEngine , QueryParserLevel , Rewrite , RewriteMode } ;
4+ use pgdog_config:: {
5+ LoadSchema , PreparedStatements , QueryParserEngine , QueryParserLevel , Rewrite , RewriteMode ,
6+ } ;
57use std:: {
68 sync:: {
79 atomic:: { AtomicBool , AtomicUsize , Ordering } ,
@@ -75,6 +77,7 @@ pub struct Cluster {
7577 client_connection_recovery : ConnectionRecovery ,
7678 query_parser_engine : QueryParserEngine ,
7779 reload_schema_on_ddl : bool ,
80+ load_schema : LoadSchema ,
7881}
7982
8083/// Sharding configuration from the cluster.
@@ -149,6 +152,7 @@ pub struct ClusterConfig<'a> {
149152 pub client_connection_recovery : ConnectionRecovery ,
150153 pub lsn_check_interval : Duration ,
151154 pub reload_schema_on_ddl : bool ,
155+ pub load_schema : LoadSchema ,
152156}
153157
154158impl < ' a > ClusterConfig < ' a > {
@@ -198,6 +202,7 @@ impl<'a> ClusterConfig<'a> {
198202 client_connection_recovery : general. client_connection_recovery ,
199203 lsn_check_interval : Duration :: from_millis ( general. lsn_check_interval ) ,
200204 reload_schema_on_ddl : general. reload_schema_on_ddl ,
205+ load_schema : general. load_schema ,
201206 }
202207 }
203208}
@@ -233,6 +238,7 @@ impl Cluster {
233238 lsn_check_interval,
234239 query_parser_engine,
235240 reload_schema_on_ddl,
241+ load_schema,
236242 } = config;
237243
238244 let identifier = Arc :: new ( DatabaseUser {
@@ -280,6 +286,7 @@ impl Cluster {
280286 client_connection_recovery,
281287 query_parser_engine,
282288 reload_schema_on_ddl,
289+ load_schema,
283290 }
284291 }
285292
@@ -486,10 +493,11 @@ impl Cluster {
486493 }
487494
488495 fn load_schema ( & self ) -> bool {
489- self . shards . len ( ) > 1
490- && self . sharded_schemas . is_empty ( )
491- && !self . sharded_tables . tables ( ) . is_empty ( )
492- || self . multi_tenant ( ) . is_some ( )
496+ match self . load_schema {
497+ LoadSchema :: On => true ,
498+ LoadSchema :: Off => false ,
499+ LoadSchema :: Auto => self . shards . len ( ) > 1 || self . multi_tenant ( ) . is_some ( ) ,
500+ }
493501 }
494502
495503 /// Get currently loaded schema from shard 0.
@@ -549,12 +557,11 @@ impl Cluster {
549557
550558 info ! ( "loaded schema from {}/{} shards" , done + 1 , shards) ;
551559
560+ schema_changed_hook ( & shard. schema ( ) , & identifier, & shard) ;
561+
552562 // Loaded schema on all shards.
553563 if done >= shards - 1 {
554564 readiness. schemas_ready . notify_waiters ( ) ;
555- // We assume the schema is the same on all shards.
556- // TODO: check that this is the case and raise a stink if its not.
557- schema_changed_hook ( & shard. schema ( ) , & identifier) ;
558565 }
559566 } ) ;
560567 }
@@ -745,7 +752,8 @@ mod test {
745752 let config = ConfigAndUsers :: default ( ) ;
746753 let cluster = Cluster :: new_test ( & config) ;
747754
748- assert ! ( !cluster. load_schema( ) ) ;
755+ // In Auto mode with multiple shards, load_schema returns true
756+ assert ! ( cluster. load_schema( ) ) ;
749757 }
750758
751759 #[ test]
@@ -755,7 +763,9 @@ mod test {
755763 cluster. sharded_schemas = ShardedSchemas :: default ( ) ;
756764 cluster. sharded_tables = ShardedTables :: default ( ) ;
757765
758- assert ! ( !cluster. load_schema( ) ) ;
766+ // In Auto mode with multiple shards, load_schema returns true
767+ // (sharded_schemas and sharded_tables no longer affect this decision)
768+ assert ! ( cluster. load_schema( ) ) ;
759769 }
760770
761771 #[ test]
@@ -839,9 +849,9 @@ mod test {
839849 #[ tokio:: test]
840850 async fn test_wait_schema_loaded_returns_immediately_when_not_needed ( ) {
841851 let config = ConfigAndUsers :: default ( ) ;
842- let cluster = Cluster :: new_test ( & config) ;
852+ let cluster = Cluster :: new_test_single_shard ( & config) ;
843853
844- // load_schema() returns false because sharded_schemas is not empty
854+ // load_schema() returns false for single shard without multi_tenant
845855 assert ! ( !cluster. load_schema( ) ) ;
846856
847857 // Should return immediately without waiting
0 commit comments