1mod iceberg_query_storage_mode;
16mod non_zero64;
17mod opt;
18pub mod parallelism;
19mod query_mode;
20mod search_path;
21pub mod sink_decouple;
22mod statement_timeout;
23mod transaction_isolation_level;
24mod visibility_mode;
25
26use chrono_tz::Tz;
27pub use iceberg_query_storage_mode::IcebergQueryStorageMode;
28use itertools::Itertools;
29pub use opt::OptionConfig;
30pub use query_mode::QueryMode;
31use risingwave_common_proc_macro::{ConfigDoc, SessionConfig};
32pub use search_path::{SearchPath, USER_NAME_WILD_CARD};
33use serde::{Deserialize, Serialize};
34pub use statement_timeout::StatementTimeout;
35use thiserror::Error;
36
37use self::non_zero64::ConfigNonZeroU64;
38use crate::config::mutate::TomlTableMutateExt;
39use crate::config::streaming::{JoinEncodingType, OverWindowCachePolicy};
40use crate::config::{ConfigMergeError, StreamingConfig, merge_streaming_config_section};
41use crate::hash::VirtualNode;
42use crate::session_config::parallelism::{ConfigBackfillParallelism, ConfigParallelism};
43use crate::session_config::sink_decouple::SinkDecouple;
44use crate::session_config::transaction_isolation_level::IsolationLevel;
45pub use crate::session_config::visibility_mode::VisibilityMode;
46use crate::{PG_VERSION, SERVER_ENCODING, SERVER_VERSION_NUM, STANDARD_CONFORMING_STRINGS};
47
48pub const SESSION_CONFIG_LIST_SEP: &str = ", ";
49
50#[derive(Error, Debug)]
51pub enum SessionConfigError {
52 #[error("Invalid value `{value}` for `{entry}`")]
53 InvalidValue {
54 entry: &'static str,
55 value: String,
56 source: anyhow::Error,
57 },
58
59 #[error("Unrecognized config entry `{0}`")]
60 UnrecognizedEntry(String),
61}
62
63type SessionConfigResult<T> = std::result::Result<T, SessionConfigError>;
64
65const DISABLE_BACKFILL_RATE_LIMIT: i32 = -1;
68const DISABLE_SOURCE_RATE_LIMIT: i32 = -1;
69const DISABLE_DML_RATE_LIMIT: i32 = -1;
70const DISABLE_SINK_RATE_LIMIT: i32 = -1;
71
72const BYPASS_CLUSTER_LIMITS: bool = cfg!(debug_assertions);
74
75#[serde_with::apply(_ => #[serde_as(as = "serde_with::DisplayFromStr")] )]
87#[serde_with::serde_as]
88#[derive(Clone, Debug, Deserialize, Serialize, SessionConfig, ConfigDoc, PartialEq)]
89pub struct SessionConfig {
90 #[parameter(default = false, alias = "rw_implicit_flush")]
94 implicit_flush: bool,
95
96 #[parameter(default = false)]
99 dml_wait_persistence: bool,
100
101 #[parameter(default = false)]
104 create_compaction_group_for_mv: bool,
105
106 #[parameter(default = QueryMode::default())]
110 query_mode: QueryMode,
111
112 #[parameter(default = IcebergQueryStorageMode::default())]
115 iceberg_query_storage_mode: IcebergQueryStorageMode,
116
117 #[parameter(default = 1)]
120 extra_float_digits: i32,
121
122 #[parameter(default = "", flags = "REPORT")]
125 application_name: String,
126
127 #[parameter(default = "", rename = "datestyle")]
130 date_style: String,
131
132 #[parameter(default = true, alias = "rw_batch_enable_lookup_join")]
134 batch_enable_lookup_join: bool,
135
136 #[parameter(default = true, alias = "rw_batch_enable_sort_agg")]
139 batch_enable_sort_agg: bool,
140
141 #[parameter(default = false, rename = "batch_enable_distributed_dml")]
144 batch_enable_distributed_dml: bool,
145
146 #[parameter(default = true)]
150 batch_expr_strict_mode: bool,
151
152 #[parameter(default = 8)]
154 max_split_range_gap: i32,
155
156 #[parameter(default = SearchPath::default())]
160 search_path: SearchPath,
161
162 #[parameter(default = VisibilityMode::default())]
164 visibility_mode: VisibilityMode,
165
166 #[parameter(default = IsolationLevel::default())]
168 transaction_isolation: IsolationLevel,
169
170 #[parameter(default = ConfigNonZeroU64::default())]
173 query_epoch: ConfigNonZeroU64,
174
175 #[parameter(default = "UTC", check_hook = check_timezone)]
177 timezone: String,
178
179 #[parameter(default = ConfigParallelism::Default, flags = "SESSION_INIT")]
183 streaming_parallelism: ConfigParallelism,
184
185 #[parameter(
188 default = ConfigBackfillParallelism::Default,
189 check_hook = check_streaming_parallelism_for_backfill,
190 flags = "SESSION_INIT"
191 )]
192 streaming_parallelism_for_backfill: ConfigBackfillParallelism,
193
194 #[parameter(default = ConfigParallelism::Default, flags = "SESSION_INIT")]
198 streaming_parallelism_for_table: ConfigParallelism,
199
200 #[parameter(default = ConfigParallelism::Default, flags = "SESSION_INIT")]
202 streaming_parallelism_for_sink: ConfigParallelism,
203
204 #[parameter(default = ConfigParallelism::Default, flags = "SESSION_INIT")]
206 streaming_parallelism_for_index: ConfigParallelism,
207
208 #[parameter(default = ConfigParallelism::Default, flags = "SESSION_INIT")]
212 streaming_parallelism_for_source: ConfigParallelism,
213
214 #[parameter(default = ConfigParallelism::Default, flags = "SESSION_INIT")]
216 streaming_parallelism_for_materialized_view: ConfigParallelism,
217
218 #[parameter(default = false, alias = "rw_streaming_enable_delta_join")]
220 streaming_enable_delta_join: bool,
221
222 #[parameter(default = true, alias = "rw_streaming_enable_bushy_join")]
224 streaming_enable_bushy_join: bool,
225
226 #[parameter(default = false, alias = "rw_streaming_force_filter_inside_join")]
229 streaming_force_filter_inside_join: bool,
230
231 #[parameter(
234 default = true,
235 deprecated = "The session variable STREAMING_USE_ARRANGEMENT_BACKFILL has been deprecated and is ignored. Arrangement backfill is always used as the fallback backfill type for new streaming jobs."
236 )]
237 streaming_use_arrangement_backfill: bool,
238
239 #[parameter(default = true)]
240 streaming_use_snapshot_backfill: bool,
241
242 #[parameter(default = false)]
244 enable_serverless_backfill: bool,
245
246 #[parameter(default = false, alias = "rw_streaming_allow_jsonb_in_stream_key")]
248 streaming_allow_jsonb_in_stream_key: bool,
249
250 #[parameter(default = false)]
254 streaming_unsafe_allow_unmaterialized_impure_expr: bool,
255
256 #[parameter(default = false)]
262 streaming_unsafe_allow_upsert_sink_pk_mismatch: bool,
263
264 #[parameter(default = false)]
266 streaming_separate_consecutive_join: bool,
267
268 #[parameter(default = false)]
270 streaming_separate_sink: bool,
271
272 #[parameter(default = None)]
277 streaming_join_encoding: OptionConfig<JoinEncodingType>,
278
279 #[parameter(default = true, alias = "rw_enable_join_ordering")]
281 enable_join_ordering: bool,
282
283 #[parameter(default = true, flags = "SETTER", alias = "rw_enable_two_phase_agg")]
286 enable_two_phase_agg: bool,
287
288 #[parameter(default = false, flags = "SETTER", alias = "rw_force_two_phase_agg")]
292 force_two_phase_agg: bool,
293
294 #[parameter(default = true, alias = "rw_enable_share_plan")]
297 enable_share_plan: bool,
299
300 #[parameter(default = false, alias = "rw_force_split_distinct_agg")]
302 force_split_distinct_agg: bool,
303
304 #[parameter(default = "", rename = "intervalstyle")]
306 interval_style: String,
307
308 #[parameter(default = ConfigNonZeroU64::default())]
310 batch_parallelism: ConfigNonZeroU64,
311
312 #[parameter(default = PG_VERSION)]
314 server_version: String,
315
316 #[parameter(default = SERVER_VERSION_NUM)]
318 server_version_num: i32,
319
320 #[parameter(default = "notice")]
322 client_min_messages: String,
323
324 #[parameter(default = SERVER_ENCODING, check_hook = check_client_encoding)]
326 client_encoding: String,
327
328 #[parameter(default = SinkDecouple::default())]
330 sink_decouple: SinkDecouple,
331
332 #[parameter(default = false)]
335 synchronize_seqscans: bool,
336
337 #[parameter(default = StatementTimeout::default())]
342 statement_timeout: StatementTimeout,
343
344 #[parameter(default = 60000u32)]
346 idle_in_transaction_session_timeout: u32,
347
348 #[parameter(default = 0)]
351 lock_timeout: i32,
352
353 #[parameter(default = 60)]
355 cdc_source_wait_streaming_start_timeout: i32,
356
357 #[parameter(default = true)]
360 row_security: bool,
361
362 #[parameter(default = STANDARD_CONFORMING_STRINGS)]
364 standard_conforming_strings: String,
365
366 #[parameter(default = DISABLE_BACKFILL_RATE_LIMIT)]
370 backfill_rate_limit: i32,
371
372 #[parameter(default = DISABLE_SOURCE_RATE_LIMIT)]
376 source_rate_limit: i32,
377
378 #[parameter(default = DISABLE_DML_RATE_LIMIT)]
382 dml_rate_limit: i32,
383
384 #[parameter(default = DISABLE_SINK_RATE_LIMIT)]
388 sink_rate_limit: i32,
389
390 #[parameter(default = None, alias = "rw_streaming_over_window_cache_policy")]
396 streaming_over_window_cache_policy: OptionConfig<OverWindowCachePolicy>,
397
398 #[parameter(default = false)]
400 background_ddl: bool,
401
402 #[parameter(default = true)]
407 streaming_use_shared_source: bool,
408
409 #[parameter(default = true)]
416 streaming_asof_join_use_cache: bool,
417
418 #[parameter(default = SERVER_ENCODING)]
420 server_encoding: String,
421
422 #[parameter(default = "hex", check_hook = check_bytea_output)]
423 bytea_output: String,
424
425 #[parameter(default = BYPASS_CLUSTER_LIMITS)]
429 bypass_cluster_limits: bool,
430
431 #[parameter(default = VirtualNode::COUNT_FOR_COMPAT, check_hook = check_streaming_max_parallelism)]
441 streaming_max_parallelism: usize,
442
443 #[parameter(default = "", check_hook = check_iceberg_engine_connection)]
446 iceberg_engine_connection: String,
447
448 #[parameter(default = false)]
450 streaming_enable_unaligned_join: bool,
451
452 #[parameter(default = None)]
459 streaming_sync_log_store_pause_duration_ms: OptionConfig<usize>,
460
461 #[parameter(default = None)]
466 streaming_sync_log_store_buffer_size: OptionConfig<usize>,
467
468 #[parameter(default = false, flags = "NO_ALTER_SYS")]
472 disable_purify_definition: bool,
473
474 #[parameter(default = 40_usize)] batch_hnsw_ef_search: usize,
477
478 #[parameter(default = true)]
480 enable_index_selection: bool,
481
482 #[parameter(default = false)]
484 enable_mv_selection: bool,
485
486 #[parameter(default = false)]
488 enable_locality_backfill: bool,
489
490 #[parameter(default = 30u32)]
493 slow_ddl_notification_secs: u32,
494
495 #[parameter(default = false)]
499 unsafe_enable_storage_retention_for_non_append_only_tables: bool,
500
501 #[parameter(default = true)]
504 enable_datafusion_engine: bool,
505
506 #[parameter(default = true)]
510 datafusion_prefer_hash_join: bool,
511
512 #[parameter(default = false)]
519 upsert_dml: bool,
520}
521
522fn check_iceberg_engine_connection(val: &str) -> Result<(), String> {
523 if val.is_empty() {
524 return Ok(());
525 }
526
527 let parts: Vec<&str> = val.split('.').collect();
528 if parts.len() != 2 {
529 return Err("Invalid iceberg engine connection format, Should be set to this format: schema_name.connection_name.".to_owned());
530 }
531
532 Ok(())
533}
534
535fn check_timezone(val: &str) -> Result<(), String> {
536 Tz::from_str_insensitive(val).map_err(|_e| "Not a valid timezone")?;
538 Ok(())
539}
540
541fn check_client_encoding(val: &str) -> Result<(), String> {
542 let clean = val.replace(|c: char| !c.is_ascii_alphanumeric(), "");
544 if !clean.eq_ignore_ascii_case("UTF8") {
545 Err("Only support 'UTF8' for CLIENT_ENCODING".to_owned())
546 } else {
547 Ok(())
548 }
549}
550
551fn check_bytea_output(val: &str) -> Result<(), String> {
552 if val == "hex" {
553 Ok(())
554 } else {
555 Err("Only support 'hex' for BYTEA_OUTPUT".to_owned())
556 }
557}
558
559fn check_streaming_max_parallelism(val: &usize) -> Result<(), String> {
561 match val {
562 0 | 1 => Err("STREAMING_MAX_PARALLELISM must be greater than 1".to_owned()),
565 2..=VirtualNode::MAX_COUNT => Ok(()),
566 _ => Err(format!(
567 "STREAMING_MAX_PARALLELISM must be less than or equal to {}",
568 VirtualNode::MAX_COUNT
569 )),
570 }
571}
572
573fn check_streaming_parallelism_for_backfill(val: &ConfigBackfillParallelism) -> Result<(), String> {
574 match val {
575 ConfigBackfillParallelism::Default | ConfigBackfillParallelism::Fixed(_) => Ok(()),
576 ConfigBackfillParallelism::Adaptive
577 | ConfigBackfillParallelism::Bounded(_)
578 | ConfigBackfillParallelism::Ratio(_) => Err(
579 "Only `default` or fixed backfill parallelism is supported here; adaptive backfill strategy is deferred to a later change.".to_owned(),
580 ),
581 }
582}
583
584impl SessionConfig {
585 pub fn set_force_two_phase_agg(
586 &mut self,
587 val: bool,
588 reporter: &mut impl ConfigReporter,
589 ) -> SessionConfigResult<bool> {
590 let set_val = self.set_force_two_phase_agg_inner(val, reporter)?;
591 if self.force_two_phase_agg {
592 self.set_enable_two_phase_agg(true, reporter)
593 } else {
594 Ok(set_val)
595 }
596 }
597
598 pub fn set_enable_two_phase_agg(
599 &mut self,
600 val: bool,
601 reporter: &mut impl ConfigReporter,
602 ) -> SessionConfigResult<bool> {
603 let set_val = self.set_enable_two_phase_agg_inner(val, reporter)?;
604 if !self.force_two_phase_agg {
605 self.set_force_two_phase_agg(false, reporter)
606 } else {
607 Ok(set_val)
608 }
609 }
610}
611
612pub struct VariableInfo {
613 pub name: String,
614 pub setting: String,
615 pub description: String,
616}
617
618pub trait ConfigReporter {
620 fn report_status(&mut self, key: &str, new_val: String);
621}
622
623impl ConfigReporter for () {
625 fn report_status(&mut self, _key: &str, _new_val: String) {}
626}
627
628def_anyhow_newtype! {
629 pub SessionConfigToOverrideError,
630 toml::ser::Error => "failed to serialize session config",
631 ConfigMergeError => transparent,
632}
633
634impl SessionConfig {
635 pub fn to_initial_streaming_config_override(
637 &self,
638 ) -> Result<String, SessionConfigToOverrideError> {
639 let mut table = toml::Table::new();
640
641 if let Some(v) = self.streaming_join_encoding.as_ref() {
644 table
645 .upsert("streaming.developer.join_encoding_type", v)
646 .unwrap();
647 }
648 if let Some(v) = self.streaming_sync_log_store_pause_duration_ms.as_ref() {
649 table
650 .upsert("streaming.developer.sync_log_store_pause_duration_ms", v)
651 .unwrap();
652 }
653 if let Some(v) = self.streaming_sync_log_store_buffer_size.as_ref() {
654 table
655 .upsert("streaming.developer.sync_log_store_buffer_size", v)
656 .unwrap();
657 }
658 if let Some(v) = self.streaming_over_window_cache_policy.as_ref() {
659 table
660 .upsert("streaming.developer.over_window_cache_policy", v)
661 .unwrap();
662 }
663
664 let res = toml::to_string(&table)?;
665
666 if !res.is_empty() {
668 let merged =
669 merge_streaming_config_section(&StreamingConfig::default(), res.as_str())?.unwrap();
670
671 let unrecognized_keys = merged.unrecognized_keys().collect_vec();
672 if !unrecognized_keys.is_empty() {
673 bail!("unrecognized configs: {:?}", unrecognized_keys);
674 }
675 }
676
677 Ok(res)
678 }
679}
680
681#[cfg(test)]
682mod test {
683 use expect_test::expect;
684
685 use super::*;
686
687 #[derive(SessionConfig)]
688 struct TestConfig {
689 #[parameter(default = 1, flags = "NO_ALTER_SYS", alias = "test_param_alias" | "alias_param_test")]
690 test_param: i32,
691 #[parameter(default = false, deprecated = "deprecated test notice")]
692 deprecated_test_param: bool,
693 }
694
695 #[test]
696 fn test_session_config_alias() {
697 let mut config = TestConfig::default();
698 config.set("test_param", "2".to_owned(), &mut ()).unwrap();
699 assert_eq!(config.get("test_param_alias").unwrap(), "2");
700 config
701 .set("alias_param_test", "3".to_owned(), &mut ())
702 .unwrap();
703 assert_eq!(config.get("test_param_alias").unwrap(), "3");
704 assert!(TestConfig::check_no_alter_sys("test_param").unwrap());
705 assert_eq!(
706 TestConfig::deprecated_notice("deprecated_test_param").unwrap(),
707 Some("deprecated test notice")
708 );
709 assert_eq!(TestConfig::deprecated_notice("test_param").unwrap(), None);
710 }
711
712 #[test]
713 fn test_initial_streaming_config_override() {
714 let mut config = SessionConfig::default();
715 config
716 .set_streaming_join_encoding(Some(JoinEncodingType::Cpu).into(), &mut ())
717 .unwrap();
718 config
719 .set_streaming_over_window_cache_policy(
720 Some(OverWindowCachePolicy::RecentFirstN).into(),
721 &mut (),
722 )
723 .unwrap();
724
725 let override_str = config.to_initial_streaming_config_override().unwrap();
727 expect![[r#"
728 [streaming.developer]
729 join_encoding_type = "cpu_optimized"
730 over_window_cache_policy = "recent_first_n"
731 "#]]
732 .assert_eq(&override_str);
733
734 let merged = merge_streaming_config_section(&StreamingConfig::default(), &override_str)
736 .unwrap()
737 .unwrap();
738 assert_eq!(merged.developer.join_encoding_type, JoinEncodingType::Cpu);
739 assert_eq!(
740 merged.developer.over_window_cache_policy,
741 OverWindowCachePolicy::RecentFirstN
742 );
743 }
744
745 #[test]
746 fn test_streaming_parallelism_defaults() {
747 let config = SessionConfig::default();
748
749 assert_eq!(config.streaming_parallelism(), ConfigParallelism::Default);
750 assert_eq!(
751 config.streaming_parallelism_for_table(),
752 ConfigParallelism::Default
753 );
754 assert_eq!(
755 config.streaming_parallelism_for_source(),
756 ConfigParallelism::Default
757 );
758 assert_eq!(
759 config.streaming_parallelism_for_sink(),
760 ConfigParallelism::Default
761 );
762 assert_eq!(
763 config.streaming_parallelism_for_index(),
764 ConfigParallelism::Default
765 );
766 assert_eq!(
767 config.streaming_parallelism_for_materialized_view(),
768 ConfigParallelism::Default
769 );
770 assert!(!config.streaming_unsafe_allow_upsert_sink_pk_mismatch());
771 }
772
773 #[test]
774 fn test_streaming_parallelism_default_round_trip() {
775 let mut config = SessionConfig::default();
776
777 assert_eq!(config.get("streaming_parallelism").unwrap(), "default");
778 assert_eq!(
779 config.get("streaming_parallelism_for_table").unwrap(),
780 "default"
781 );
782 assert_eq!(
783 config.get("streaming_parallelism_for_source").unwrap(),
784 "default"
785 );
786
787 config
788 .set("streaming_parallelism", "default".to_owned(), &mut ())
789 .unwrap();
790 assert_eq!(config.get("streaming_parallelism").unwrap(), "default");
791
792 config
793 .set("streaming_parallelism", "bounded(16)".to_owned(), &mut ())
794 .unwrap();
795 config
796 .set(
797 "streaming_parallelism_for_table",
798 "bounded(8)".to_owned(),
799 &mut (),
800 )
801 .unwrap();
802 config
803 .set(
804 "streaming_parallelism_for_source",
805 "bounded(8)".to_owned(),
806 &mut (),
807 )
808 .unwrap();
809
810 assert_eq!(
811 config.reset("streaming_parallelism", &mut ()).unwrap(),
812 "default"
813 );
814 assert_eq!(
815 config
816 .reset("streaming_parallelism_for_table", &mut ())
817 .unwrap(),
818 "default"
819 );
820 assert_eq!(
821 config
822 .reset("streaming_parallelism_for_source", &mut ())
823 .unwrap(),
824 "default"
825 );
826 }
827 #[test]
828 fn test_streaming_parallelism_for_backfill_accepts_default_and_fixed() {
829 let mut config = SessionConfig::default();
830
831 config
832 .set(
833 "streaming_parallelism_for_backfill",
834 "default".to_owned(),
835 &mut (),
836 )
837 .unwrap();
838 assert_eq!(
839 config.get("streaming_parallelism_for_backfill").unwrap(),
840 "default"
841 );
842
843 config
844 .set(
845 "streaming_parallelism_for_backfill",
846 "2".to_owned(),
847 &mut (),
848 )
849 .unwrap();
850 assert_eq!(config.streaming_parallelism_for_backfill().to_string(), "2");
851 }
852
853 #[test]
854 fn test_streaming_parallelism_for_backfill_rejects_adaptive_modes() {
855 let mut config = SessionConfig::default();
856 let expected = "Only `default` or fixed backfill parallelism is supported here; adaptive backfill strategy is deferred to a later change.";
857
858 for value in ["adaptive", "bounded(2)", "ratio(0.5)"] {
859 let err = config
860 .set(
861 "streaming_parallelism_for_backfill",
862 value.to_owned(),
863 &mut (),
864 )
865 .unwrap_err();
866
867 match err {
868 SessionConfigError::InvalidValue {
869 entry,
870 value: actual_value,
871 source,
872 } => {
873 assert_eq!(entry, "streaming_parallelism_for_backfill");
874 assert_eq!(actual_value, value);
875 assert_eq!(source.to_string(), expected);
876 }
877 other => panic!("unexpected error: {other:?}"),
878 }
879 }
880 }
881}