feat(pg,kingbase): partition table operations and detail queries
This commit is contained in:
1 parent
882bd130ab
commit
1000aa07b6
62 files changed
+6180
-133
No files matched your search
@@ -3948,6 +3948,93 @@ pub async fn execute_statements(
|
||||
result
|
||||
}
|
||||
|
||||
/// Execute a batch, optionally on a single transaction.
|
||||
///
|
||||
/// `use_transaction` is opt-in and only the structure editor sets it, for
|
||||
/// batches that change a partition hierarchy: a mid-batch failure there would
|
||||
/// otherwise leave a half-created hierarchy behind. The shared transaction
|
||||
/// kernel rejects backends whose DDL cannot roll back, and statements that
|
||||
/// cannot run inside a transaction block (`... CONCURRENTLY`) are refused up
|
||||
/// front rather than half-applied.
|
||||
pub async fn execute_statements_with_transaction_option(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
statements: &[String],
|
||||
schema: Option<&str>,
|
||||
use_transaction: bool,
|
||||
timeout_secs: Option<u64>,
|
||||
) -> Result<db::QueryResult, String> {
|
||||
if use_transaction && statements.len() > 1 {
|
||||
if batch_has_concurrently_statement(statements) {
|
||||
return Err(
|
||||
"use_transaction cannot wrap CONCURRENTLY statements: they cannot run inside a transaction block. Run the batch without use_transaction."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
let db_type = connection_database_type(state, connection_id).await;
|
||||
if batch_transaction_ddl_is_unrollbackable(db_type, statements) {
|
||||
return Err(
|
||||
"use_transaction cannot be used with a batch whose DDL cannot be rolled back: DDL statements implicitly commit and cannot be undone. Run the batch without use_transaction (auto-commit, one result per statement) or split the DDL and DML into separate calls."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
let result = execute_statements_in_transaction_typed(
|
||||
state,
|
||||
connection_id,
|
||||
database,
|
||||
statements,
|
||||
schema,
|
||||
None,
|
||||
timeout_secs,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| error.into_legacy_string())?;
|
||||
let invalidate = statements.iter().any(|sql| crate::object_cache::sql_may_change_object_metadata(sql, db_type));
|
||||
if invalidate {
|
||||
crate::object_cache::invalidate_connection_object_cache(&state.storage, connection_id).await;
|
||||
}
|
||||
return Ok(result);
|
||||
}
|
||||
execute_statements(state, connection_id, database, statements, schema, timeout_secs).await
|
||||
}
|
||||
|
||||
/// Whether a batch contains a statement PostgreSQL-family servers refuse to run
|
||||
/// inside a transaction block (`CREATE/DROP INDEX CONCURRENTLY`,
|
||||
/// `ALTER TABLE ... DETACH PARTITION CONCURRENTLY`, ...).
|
||||
fn batch_has_concurrently_statement(statements: &[String]) -> bool {
|
||||
statements.iter().any(|statement| {
|
||||
let upper = statement.to_ascii_uppercase();
|
||||
// Real CONCURRENTLY statements always start with one of these verbs.
|
||||
// Requiring the verb plus a standalone keyword keeps the word inside
|
||||
// string literals, comments or plain identifiers (e.g. a table named
|
||||
// `concurrently`) from silently demoting the batch to auto-commit; the
|
||||
// direction stays fail-safe because no CONCURRENTLY statement form
|
||||
// starts with another verb.
|
||||
let trimmed = upper.trim_start();
|
||||
let starts_with_ddl_verb =
|
||||
["CREATE ", "DROP ", "REINDEX", "REFRESH ", "ALTER "].iter().any(|verb| trimmed.starts_with(verb));
|
||||
starts_with_ddl_verb && contains_standalone_concurrently_keyword(&upper)
|
||||
})
|
||||
}
|
||||
|
||||
fn contains_standalone_concurrently_keyword(upper: &str) -> bool {
|
||||
let keyword = "CONCURRENTLY";
|
||||
let mut search_from = 0;
|
||||
while let Some(pos) = upper[search_from..].find(keyword) {
|
||||
let pos = search_from + pos;
|
||||
let boundary =
|
||||
|ch: Option<char>| ch.map(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '"').unwrap_or(false);
|
||||
let before = boundary(upper[..pos].chars().next_back());
|
||||
let after = boundary(upper[pos + keyword.len()..].chars().next());
|
||||
if !before && !after {
|
||||
return true;
|
||||
}
|
||||
search_from = pos + 1;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
async fn execute_statements_inner(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
@@ -8171,6 +8258,37 @@ for line in sys.stdin:
|
||||
assert!(!batch_transaction_ddl_is_unrollbackable(None, &ddl));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_has_concurrently_statement_detects_transaction_block_escapees() {
|
||||
assert!(batch_has_concurrently_statement(&["CREATE INDEX CONCURRENTLY idx ON t (id)".to_string(),]));
|
||||
assert!(batch_has_concurrently_statement(&[
|
||||
"ALTER TABLE parent DETACH PARTITION child CONCURRENTLY".to_string(),
|
||||
]));
|
||||
// Case-insensitive, and a single CONCURRENTLY anywhere in the batch is enough.
|
||||
assert!(batch_has_concurrently_statement(&[
|
||||
"ALTER TABLE t ADD COLUMN c int".to_string(),
|
||||
"drop index concurrently idx".to_string(),
|
||||
]));
|
||||
assert!(!batch_has_concurrently_statement(&[
|
||||
"CREATE TABLE child PARTITION OF parent FOR VALUES FROM (0) TO (1)".to_string(),
|
||||
]));
|
||||
// The word inside literals or plain identifiers must not demote a batch
|
||||
// that would otherwise run in one transaction.
|
||||
assert!(!batch_has_concurrently_statement(&[
|
||||
"INSERT INTO notes (body) VALUES ('run CREATE INDEX CONCURRENTLY later')".to_string(),
|
||||
]));
|
||||
assert!(!batch_has_concurrently_statement(&[
|
||||
"SELECT id FROM concurrently WHERE label = 'drop index concurrently idx'".to_string(),
|
||||
]));
|
||||
assert!(!batch_has_concurrently_statement(&[
|
||||
"-- refresh materialized view concurrently next".to_string(),
|
||||
"SELECT 1".to_string(),
|
||||
]));
|
||||
// REINDEX/REFRESH forms still detect.
|
||||
assert!(batch_has_concurrently_statement(&["REINDEX TABLE CONCURRENTLY t".to_string()]));
|
||||
assert!(batch_has_concurrently_statement(&["REFRESH MATERIALIZED VIEW CONCURRENTLY mv".to_string()]));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connection_pool_is_sqlserver_agent_detects_agent_and_native_pools() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-query-sqlserver-agent-flag-{}", uuid::Uuid::new_v4()));
|
||||
|
||||
@@ -5250,6 +5250,27 @@ for line in sys.stdin:
|
||||
assert!(!super::is_agent_completion_assistant_unsupported("Agent RPC error (-1): Connection failed"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_unsupported_agent_partition_method_errors() {
|
||||
assert!(super::is_agent_partition_method_unsupported(
|
||||
"Agent RPC error (-1): unknown method: get_table_partitioning",
|
||||
"get_table_partitioning",
|
||||
));
|
||||
assert!(super::is_agent_partition_method_unsupported(
|
||||
"Agent RPC error (-32601): Method not found: get_table_partition_status",
|
||||
"get_table_partition_status",
|
||||
));
|
||||
// A different method in the same error must not match.
|
||||
assert!(!super::is_agent_partition_method_unsupported(
|
||||
"Agent RPC error (-1): unknown method: get_table_partitioning",
|
||||
"get_table_partition_status",
|
||||
));
|
||||
assert!(!super::is_agent_partition_method_unsupported(
|
||||
"Agent RPC error (-1): Connection failed",
|
||||
"get_table_partitioning",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clickhouse_metadata_prefers_schema_qualifier() {
|
||||
assert_eq!(clickhouse_metadata_database("", "testdb"), "testdb");
|
||||
@@ -6141,6 +6162,15 @@ fn is_agent_completion_assistant_unsupported(error: &str) -> bool {
|
||||
|| error.contains("completion assistant search is not supported")
|
||||
}
|
||||
|
||||
/// True when an agent built against an older protocol does not implement a
|
||||
/// table-partition RPC. A mixed-version deployment (new core, old agent) must
|
||||
/// hide the Partitions tab rather than fail every probe, so callers degrade to
|
||||
/// the default value instead of surfacing the error.
|
||||
fn is_agent_partition_method_unsupported(error: &str, method: &str) -> bool {
|
||||
let error = error.to_ascii_lowercase();
|
||||
error.contains(method) && (error.contains("unknown method") || error.contains("method not found"))
|
||||
}
|
||||
|
||||
async fn completion_assistant_fallback_core(
|
||||
state: &AppState,
|
||||
request: &db::CompletionAssistantRequest,
|
||||
@@ -7809,10 +7839,31 @@ pub async fn table_partition_status_core(
|
||||
match pool_handle.as_ref() {
|
||||
Some(PoolKind::Postgres(pool)) => {
|
||||
let info = db::postgres::get_table_partition_info(pool, schema, table).await?;
|
||||
Ok(TablePartitionStatus {
|
||||
is_partitioned_parent: info.key.is_some() && !info.is_partition,
|
||||
is_partition: info.is_partition,
|
||||
})
|
||||
Ok(TablePartitionStatus { is_partitioned_parent: info.key.is_some(), is_partition: info.is_partition })
|
||||
}
|
||||
Some(PoolKind::Agent(client)) => {
|
||||
// Resolve the config once: it gates the arm and feeds the RPC
|
||||
// timeout.
|
||||
let db_config = connection_config(state, connection_id).await;
|
||||
if !db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::Kingbase) {
|
||||
return Ok(TablePartitionStatus::default());
|
||||
}
|
||||
let mut client = client.lock().await;
|
||||
match client
|
||||
.get_table_partition_status::<TablePartitionStatus>(
|
||||
database,
|
||||
schema,
|
||||
table,
|
||||
agent_metadata_timeout(db_config.as_ref()),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(status) => Ok(status),
|
||||
Err(error) if is_agent_partition_method_unsupported(&error, "get_table_partition_status") => {
|
||||
Ok(TablePartitionStatus::default())
|
||||
}
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
_ => Ok(TablePartitionStatus::default()),
|
||||
}
|
||||
@@ -7820,6 +7871,52 @@ pub async fn table_partition_status_core(
|
||||
.await
|
||||
}
|
||||
|
||||
/// Structured declarative partitioning view used by the table structure
|
||||
/// editor. Native PostgreSQL pools and compatible agents (such as Kingbase)
|
||||
/// provide the same response shape; unsupported pools return the default
|
||||
/// all-false/empty value.
|
||||
pub async fn get_table_partitioning_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
schema: &str,
|
||||
table: &str,
|
||||
) -> Result<db::PgTablePartitioning, String> {
|
||||
retry_metadata_connection(state, connection_id, Some(database), || async {
|
||||
let pool_key = state.get_or_create_metadata_pool_for_session(connection_id, Some(database), None).await?;
|
||||
let pool_handle = state.pool_handle(&pool_key).await;
|
||||
match pool_handle.as_ref() {
|
||||
Some(PoolKind::Postgres(pool)) => db::postgres::get_table_partitioning(pool, schema, table).await,
|
||||
Some(PoolKind::Agent(client)) => {
|
||||
// Resolve the config once: it gates the arm and feeds the RPC
|
||||
// timeout.
|
||||
let db_config = connection_config(state, connection_id).await;
|
||||
if !db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::Kingbase) {
|
||||
return Ok(db::PgTablePartitioning::default());
|
||||
}
|
||||
let mut client = client.lock().await;
|
||||
match client
|
||||
.get_table_partitioning::<db::PgTablePartitioning>(
|
||||
database,
|
||||
schema,
|
||||
table,
|
||||
agent_metadata_timeout(db_config.as_ref()),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(partitioning) => Ok(partitioning),
|
||||
Err(error) if is_agent_partition_method_unsupported(&error, "get_table_partitioning") => {
|
||||
Ok(db::PgTablePartitioning::default())
|
||||
}
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
_ => Ok(db::PgTablePartitioning::default()),
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Same-table index names whose `pg_index.indisvalid` is `false` (left behind
|
||||
/// by a cancelled `CREATE INDEX CONCURRENTLY`). Empty for non-PostgreSQL pools.
|
||||
pub async fn list_invalid_indexes_core(
|
||||
|
||||
Reference in new issue
Block a user