risingwave_frontend/handler/
alter_system.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use pgwire::pg_response::StatementType;
16use risingwave_common::session_config::SessionConfig;
17use risingwave_common::system_param::reader::SystemParamsRead;
18use risingwave_common::system_param::{NOTICE_BARRIER_INTERVAL_MS, NOTICE_CHECKPOINT_FREQUENCY};
19use risingwave_sqlparser::ast::{Ident, SetVariableValue};
20
21use super::variable::set_var_to_param_str;
22use super::{HandlerArgs, RwPgResponse};
23use crate::error::{ErrorCode, Result};
24
25pub async fn handle_alter_system(
26    handler_args: HandlerArgs,
27    param: Ident,
28    value: SetVariableValue,
29) -> Result<RwPgResponse> {
30    let value = set_var_to_param_str(&value);
31    let param_name = param.to_string();
32    let meta_client = handler_args.session.env().meta_client();
33    let mut builder = RwPgResponse::builder(StatementType::ALTER_SYSTEM);
34
35    // Currently session params are separated from system params. If the param exist in session params, we set it. Otherwise
36    // we try to set it as a system param.
37    if SessionConfig::contains_param(&param_name) {
38        if SessionConfig::check_no_alter_sys(&param_name)? {
39            return Err(ErrorCode::InternalError(format!(
40                "session param {} cannot be altered system wide",
41                param_name
42            ))
43            .into());
44        }
45        meta_client
46            .set_session_param(param_name.clone(), value.clone())
47            .await?;
48
49        if let Some(value) = value {
50            builder = builder.notice(format!(
51                "The config {param_name} of the current session has already been set to {value}"
52            ));
53            handler_args
54                .session
55                .set_config(param_name.as_str(), value)?;
56        } else {
57            builder = builder.notice(format!(
58                "The config {param_name} of the current session has already been reset to default"
59            ));
60            handler_args.session.reset_config(param_name.as_str())?;
61        }
62    } else {
63        if !handler_args.session.is_super_user() {
64            return Err(ErrorCode::PermissionDenied(
65                "must be superuser to execute ALTER SYSTEM command".to_owned(),
66            )
67            .into());
68        }
69        let params = meta_client.set_system_param(param_name, value).await?;
70        if let Some(params) = params {
71            if params.barrier_interval_ms() >= NOTICE_BARRIER_INTERVAL_MS {
72                builder = builder.notice(
73                    format!("Barrier interval is set to {} ms >= {} ms. This can hurt freshness and potentially cause OOM.",
74                             params.barrier_interval_ms(), NOTICE_BARRIER_INTERVAL_MS));
75            }
76            if params.checkpoint_frequency() >= NOTICE_CHECKPOINT_FREQUENCY {
77                builder = builder.notice(
78                    format!("Checkpoint frequency is set to {} >= {}. This can hurt freshness and potentially cause OOM.",
79                             params.checkpoint_frequency(), NOTICE_CHECKPOINT_FREQUENCY));
80            }
81        }
82    }
83    Ok(builder.into())
84}