risingwave_frontend/handler/
drop_subscription.rs1use pgwire::pg_response::{PgResponse, StatementType};
16use risingwave_sqlparser::ast::ObjectName;
17
18use super::util::{LongRunningNotificationAction, execute_with_long_running_notification};
19use super::{HandlerArgs, RwPgResponse};
20use crate::Binder;
21use crate::catalog::RelationKind;
22use crate::catalog::root_catalog::SchemaPath;
23use crate::error::Result;
24
25pub async fn handle_drop_subscription(
26 handler_args: HandlerArgs,
27 subscription_name: ObjectName,
28 if_exists: bool,
29 cascade: bool,
30) -> Result<RwPgResponse> {
31 let session = handler_args.session;
32 let db_name = &session.database();
33 let (schema_name, subscription_name) =
34 Binder::resolve_schema_qualified_name(db_name, &subscription_name)?;
35 let search_path = session.config().search_path();
36 let user_name = &session.user_name();
37 let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
38
39 let subscription = {
40 let catalog_reader = session.env().catalog_reader().read_guard();
41 let (subscription, schema_name) =
42 match catalog_reader.get_subscription_by_name(db_name, schema_path, &subscription_name)
43 {
44 Ok((subscription, schema)) => (subscription.clone(), schema),
45 Err(e) => {
46 return if if_exists {
47 Ok(RwPgResponse::builder(StatementType::DROP_SUBSCRIPTION)
48 .notice(format!(
49 "subscription \"{}\" does not exist, skipping",
50 subscription_name
51 ))
52 .into())
53 } else {
54 Err(catalog_reader
55 .wrong_kind_for_drop(
56 db_name,
57 schema_path,
58 &subscription_name,
59 RelationKind::Subscription,
60 )
61 .unwrap_or(e)
62 .into())
63 };
64 }
65 };
66
67 session.check_privilege_for_drop_alter(schema_name, &*subscription)?;
68
69 subscription
70 };
71
72 let subscription_id = subscription.id;
73
74 let catalog_writer = session.catalog_writer()?;
75 execute_with_long_running_notification(
76 catalog_writer.drop_subscription(subscription_id, cascade),
77 &session,
78 "DROP SUBSCRIPTION",
79 LongRunningNotificationAction::SuggestRecover,
80 )
81 .await?;
82
83 Ok(PgResponse::empty_result(StatementType::DROP_SUBSCRIPTION))
84}