Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(schema): list table sequences for kingbase and vastbase

zipga8006a1fddc0
diff --git a/apps/desktop/src/lib/__tests__/database/databaseObjectCapabilities.spec.ts b/apps/desktop/src/lib/__tests__/database/databaseObjectCapabilities.spec.ts
index d2d2187ca4..5532065916 100644
--- a/apps/desktop/src/lib/__tests__/database/databaseObjectCapabilities.spec.ts
+++ b/apps/desktop/src/lib/__tests__/database/databaseObjectCapabilities.spec.ts
@@ -111,6 +111,13 @@ describe("databaseObjectCapabilities", () => {
expect(sidebarObjectKindsForDatabase("vastbase")).not.toContain("TRIGGER");
});
+ it("exposes sequences for Kingbase and Vastbase via the agent PostgreSQL catalogs", () => {
+ for (const dbType of ["kingbase", "vastbase"] as const) {
+ expect(sidebarObjectKindsForDatabase(dbType), dbType).toContain("SEQUENCE");
+ expect(databaseObjectCapabilities(dbType).sourceReadable, dbType).toContain("SEQUENCE");
+ }
+ });
+
it("only Xugu TYPE nodes can open object source", () => {
expect(supportsTypeObjectSource("xugu")).toBe(true);
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase", undefined] as const) {
diff --git a/apps/desktop/src/lib/database/databaseObjectCapabilities.ts b/apps/desktop/src/lib/database/databaseObjectCapabilities.ts
index 9e31ffae2a..c510e70668 100644
--- a/apps/desktop/src/lib/database/databaseObjectCapabilities.ts
+++ b/apps/desktop/src/lib/database/databaseObjectCapabilities.ts
@@ -25,11 +25,12 @@ const OPENGAUSS_A_OBJECTS: SidebarObjectKind[] = [...POSTGRES_OBJECTS, "PACKAGE"
const POSTGRES_NO_TYPE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "SEQUENCE"];
// Kingbase and Vastbase agents support the same user-defined type listing via
-// their own metadata query, but do not expose sequences. Kingbase additionally
-// has a verified schema-trigger path; keep Vastbase separate until its Agent
+// their own metadata query, and their sequences are read from the PostgreSQL
+// catalogs over the agent connection (t8y2/dbx#9016). Kingbase additionally has
+// a verified schema-trigger path; keep Vastbase separate until its Agent
// exposes the same parent-table-aware metadata contract.
-const KINGBASE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "TYPE"];
-const VASTBASE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TYPE"];
+const KINGBASE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE", "TYPE"];
+const VASTBASE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "SEQUENCE", "TYPE"];
const POSTGRES_LIKE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION"];
const ORACLE_OBJECTS: SidebarObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "SEQUENCE", "SYNONYM", "PACKAGE", "PACKAGE_BODY"];
diff --git a/crates/dbx-core/src/schema/agent_pg_sequences.rs b/crates/dbx-core/src/schema/agent_pg_sequences.rs
new file mode 100644
index 0000000000..338568a380
--- /dev/null
+++ b/crates/dbx-core/src/schema/agent_pg_sequences.rs
@@ -0,0 +1,464 @@
+//! Sequence metadata for agent-backed PostgreSQL-family engines.
+//!
+//! KingbaseES and Vastbase run through Agent pools instead of the native
+//! PostgreSQL driver, so sequences need explicit agent-side queries. The
+//! `sys_catalog`/`pg_catalog` spellings are tried in turn because KingbaseES
+//! ships both while Vastbase only exposes the `pg_*` one, and older KingbaseES
+//! releases predate the `pg_sequence` catalog and are served from
+//! `information_schema.sequences` instead (t8y2/dbx#9016).
+
+use crate::db;
+use crate::query::{agent_execute_query_params, QueryExecutionOptions};
+use std::sync::Arc;
+use std::time::Duration;
+
+use super::{pg_ident, query_result_cell_string, sql_string};
+
+#[derive(Clone, Copy)]
+enum PgCatalog {
+ Sys,
+ Pg,
+}
+
+impl PgCatalog {
+ fn catalog_name(self) -> &'static str {
+ match self {
+ Self::Sys => "sys_catalog",
+ Self::Pg => "pg_catalog",
+ }
+ }
+
+ fn prefix(self) -> &'static str {
+ match self {
+ Self::Sys => "sys",
+ Self::Pg => "pg",
+ }
+ }
+
+ fn user_by_id(self) -> &'static str {
+ match self {
+ Self::Sys => "sys_get_userbyid",
+ Self::Pg => "pg_get_userbyid",
+ }
+ }
+}
+
+async fn query_result(
+ client: Arc<db::agent_driver::PooledAgentClient>,
+ database: &str,
+ sql: &str,
+ max_rows: usize,
+ timeout_duration: Option<Duration>,
+) -> Result<db::QueryResult, String> {
+ let params = agent_execute_query_params(
+ sql,
+ if database.is_empty() { None } else { Some(database) },
+ None,
+ QueryExecutionOptions { max_rows: Some(max_rows), ..Default::default() },
+ );
+ let mut client = client.lock().await;
+ client.execute_query_with_timeout(params, timeout_duration).await
+}
+
+async fn query_result_with_catalog_fallback(
+ client: Arc<db::agent_driver::PooledAgentClient>,
+ database: &str,
+ sys_sql: String,
+ pg_sql: String,
+ max_rows: usize,
+ timeout_duration: Option<Duration>,
+) -> Result<db::QueryResult, String> {
+ match query_result(client.clone(), database, &sys_sql, max_rows, timeout_duration).await {
+ Ok(result) => Ok(result),
+ Err(sys_error) => query_result(client, database, &pg_sql, max_rows, timeout_duration)
+ .await
+ .map_err(|pg_error| format!("{sys_error}; pg_catalog fallback failed: {pg_error}")),
+ }
+}
+
+fn list_sequences_sql(schema: &str, catalog: PgCatalog) -> String {
+ let catalog_name = catalog.catalog_name();
+ let prefix = catalog.prefix();
+ format!(
+ "SELECT c.relname, \
+ COALESCE(format_type(s.seqtypid, NULL), 'bigint') AS data_type, \
+ COALESCE(s.seqstart::text, '1') AS start_value, \
+ COALESCE(s.seqmin::text, '1') AS min_value, \
+ COALESCE(s.seqmax::text, '9223372036854775807') AS max_value, \
+ COALESCE(s.seqincrement::text, '1') AS increment, \
+ CASE WHEN s.seqcycle THEN 'YES' ELSE 'NO' END AS cycle \
+ FROM {catalog_name}.{prefix}_class c \
+ JOIN {catalog_name}.{prefix}_namespace n ON n.oid = c.relnamespace \
+ LEFT JOIN {catalog_name}.{prefix}_sequence s ON s.seqrelid = c.oid \
+ WHERE c.relkind = 'S' AND n.nspname = {} \
+ ORDER BY c.relname",
+ sql_string(schema),
+ )
+}
+
+/// Portable sibling of `list_sequences_sql` for engines without the
+/// `pg_sequence` catalog (mirrors the PostgreSQL driver's pre-PG10 tier).
+fn list_sequences_compat_sql(schema: &str) -> String {
+ format!(
+ "SELECT sequence_name, \
+ COALESCE(data_type::text, 'bigint') AS data_type, \
+ COALESCE(start_value::text, '1') AS start_value, \
+ COALESCE(minimum_value::text, '1') AS min_value, \
+ COALESCE(maximum_value::text, '9223372036854775807') AS max_value, \
+ COALESCE(increment::text, '1') AS increment, \
+ COALESCE(cycle_option::text, 'NO') AS cycle \
+ FROM information_schema.sequences \
+ WHERE sequence_schema = {} \
+ ORDER BY sequence_name",
+ sql_string(schema),
+ )
+}
+
+fn sequence_last_values_sql(schema: &str, catalog: PgCatalog) -> String {
+ let catalog_name = catalog.catalog_name();
+ let prefix = catalog.prefix();
+ format!(
+ "SELECT c.relname, {catalog_name}.{prefix}_sequence_last_value(c.oid)::text \
+ FROM {catalog_name}.{prefix}_class c \
+ JOIN {catalog_name}.{prefix}_namespace n ON n.oid = c.relnamespace \
+ WHERE c.relkind = 'S' AND n.nspname = {}",
+ sql_string(schema),
+ )
+}
+
+/// Sequence DDL for the object-source view. The property values come from the
+/// portable `information_schema.sequences` view, while owner and owned-by
+/// references keep the catalog spelling of the other queries.
+///
+/// The dependency join filters on `'pg_class'::regclass` even for the `sys`
+/// catalog: KingbaseES stores the `pg_class` oid in `sys_depend.classid`, so
+/// `'sys_class'::regclass` (the compatibility view's own oid) never matches and
+/// the `owned by` clause would be dropped.
+fn sequence_source_sql(schema: &str, name: &str, catalog: PgCatalog) -> String {
+ let catalog_name = catalog.catalog_name();
+ let prefix = catalog.prefix();
+ let user_by_id = catalog.user_by_id();
+ format!(
+ "SELECT concat_ws(E'\\n\\n', \
+ '-- auto-generated definition' || E'\\n' || \
+ 'create sequence ' || quote_ident(c.relname) || E'\\n' || \
+ ' increment by ' || COALESCE(s.increment::text, '1') || E'\\n' || \
+ ' minvalue ' || COALESCE(s.minimum_value::text, '1') || E'\\n' || \
+ ' maxvalue ' || COALESCE(s.maximum_value::text, '9223372036854775807') || E'\\n' || \
+ ' start with ' || COALESCE(s.start_value::text, '1') || E'\\n' || \
+ CASE WHEN upper(COALESCE(s.cycle_option::text, 'NO')) = 'YES' \
+ THEN ' cycle;' ELSE ' no cycle;' END, \
+ 'alter sequence ' || quote_ident(c.relname) || ' owner to ' || quote_ident({user_by_id}(c.relowner)) || ';', \
+ CASE WHEN owned.relname IS NOT NULL AND a.attname IS NOT NULL \
+ THEN 'alter sequence ' || quote_ident(c.relname) || ' owned by ' || quote_ident(owned.relname) || '.' || quote_ident(a.attname) || ';' \
+ END \
+ ) \
+ FROM {catalog_name}.{prefix}_class c \
+ JOIN {catalog_name}.{prefix}_namespace n ON n.oid = c.relnamespace \
+ JOIN information_schema.sequences s \
+ ON s.sequence_schema = n.nspname AND s.sequence_name = c.relname \
+ LEFT JOIN {catalog_name}.{prefix}_depend d \
+ ON d.classid = 'pg_class'::regclass AND d.objid = c.oid AND d.deptype = 'a' \
+ LEFT JOIN {catalog_name}.{prefix}_class owned ON owned.oid = d.refobjid \
+ LEFT JOIN {catalog_name}.{prefix}_attribute a \
+ ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid \
+ WHERE n.nspname = {} AND c.relname = {} AND c.relkind = 'S' \
+ ORDER BY c.oid LIMIT 1",
+ sql_string(schema),
+ sql_string(name),
+ )
+}
+
+fn sequence_infos_from_query_result(result: db::QueryResult) -> Vec<db::SequenceInfo> {
+ result
+ .rows
+ .into_iter()
+ .filter_map(|row| {
+ let name = query_result_cell_string(&row, 0)?;
+ Some(db::SequenceInfo {
+ name,
+ data_type: query_result_cell_string(&row, 1).unwrap_or_else(|| "bigint".to_string()),
+ start_value: query_result_cell_string(&row, 2).unwrap_or_else(|| "1".to_string()),
+ min_value: query_result_cell_string(&row, 3).unwrap_or_else(|| "1".to_string()),
+ max_value: query_result_cell_string(&row, 4).unwrap_or_else(|| "9223372036854775807".to_string()),
+ increment: query_result_cell_string(&row, 5).unwrap_or_else(|| "1".to_string()),
+ cycle: query_result_cell_string(&row, 6).is_some_and(|value| value.eq_ignore_ascii_case("YES")),
+ last_value: None,
+ })
+ })
+ .collect()
+}
+
+fn apply_sequence_last_values(sequences: &mut [db::SequenceInfo], result: db::QueryResult) {
+ for row in result.rows {
+ let Some(name) = query_result_cell_string(&row, 0) else {
+ continue;
+ };
+ let Some(last_value) = query_result_cell_string(&row, 1) else {
+ continue;
+ };
+ if let Some(sequence) = sequences.iter_mut().find(|sequence| sequence.name == name) {
+ sequence.last_value = Some(last_value);
+ }
+ }
+}
+
+/// Lists sequences over an agent connection, degrading from the `sys`/`pg`
+/// sequence catalogs to `information_schema.sequences` for engines that ship
+/// neither (t8y2/dbx#9016).
+pub(super) async fn list_sequences(
+ client: Arc<db::agent_driver::PooledAgentClient>,
+ database: &str,
+ schema: &str,
+ with_last_values: bool,
+ timeout_duration: Option<Duration>,
+) -> Result<Vec<db::SequenceInfo>, String> {
+ let (result, catalog) = match query_result(
+ client.clone(),
+ database,
+ &list_sequences_sql(schema, PgCatalog::Sys),
+ 10_000,
+ timeout_duration,
+ )
+ .await
+ {
+ Ok(result) => (result, Some(PgCatalog::Sys)),
+ Err(sys_error) => match query_result(
+ client.clone(),
+ database,
+ &list_sequences_sql(schema, PgCatalog::Pg),
+ 10_000,
+ timeout_duration,
+ )
+ .await
+ {
+ Ok(result) => (result, Some(PgCatalog::Pg)),
+ Err(pg_error) => {
+ let compat = query_result(
+ client.clone(),
+ database,
+ &list_sequences_compat_sql(schema),
+ 10_000,
+ timeout_duration,
+ )
+ .await
+ .map_err(|compat_error| {
+ format!(
+ "{sys_error}; pg_catalog fallback failed: {pg_error}; information_schema fallback failed: {compat_error}"
+ )
+ })?;
+ (compat, None)
+ }
+ },
+ };
+
+ let mut sequences = sequence_infos_from_query_result(result);
+ if !with_last_values || sequences.is_empty() {
+ return Ok(sequences);
+ }
+
+ match catalog {
+ Some(catalog) => {
+ let sql = sequence_last_values_sql(schema, catalog);
+ if let Ok(result) = query_result(client, database, &sql, 10_000, timeout_duration).await {
+ apply_sequence_last_values(&mut sequences, result);
+ }
+ }
+ None => {
+ // Pre-PG10 servers cannot read an arbitrary sequence value from the
+ // catalog, so read each sequence relation directly.
+ for sequence in sequences.iter_mut() {
+ let sql = format!("SELECT last_value::text FROM {}.{}", pg_ident(schema), pg_ident(&sequence.name));
+ if let Ok(result) = query_result(client.clone(), database, &sql, 1, timeout_duration).await {
+ sequence.last_value = result.rows.first().and_then(|row| query_result_cell_string(row, 0));
+ }
+ }
+ }
+ }
+
+ Ok(sequences)
+}
+
+/// `CREATE SEQUENCE`/`ALTER SEQUENCE` source for the object-source view.
+pub(super) async fn sequence_source(
+ client: Arc<db::agent_driver::PooledAgentClient>,
+ database: &str,
+ schema: &str,
+ name: &str,
+ timeout_duration: Option<Duration>,
+) -> Result<Option<String>, String> {
+ let result = query_result_with_catalog_fallback(
+ client,
+ database,
+ sequence_source_sql(schema, name, PgCatalog::Sys),
+ sequence_source_sql(schema, name, PgCatalog::Pg),
+ 1,
+ timeout_duration,
+ )
+ .await?;
+ Ok(result.rows.first().and_then(|row| query_result_cell_string(row, 0)).filter(|source| !source.trim().is_empty()))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn list_sequences_sql_uses_native_catalog_and_escapes_schema() {
+ let sql = list_sequences_sql("core's", PgCatalog::Sys);
+
+ assert!(sql.contains("sys_catalog.sys_class"));
+ assert!(sql.contains("sys_catalog.sys_namespace"));
+ assert!(sql.contains("sys_catalog.sys_sequence"));
+ assert!(sql.contains("c.relkind = 'S'"));
+ assert!(sql.contains("n.nspname = 'core''s'"));
+ assert!(!sql.contains("pg_catalog"));
+ }
+
+ #[test]
+ fn list_sequences_sql_pg_catalog_variant_uses_pg_prefix() {
+ let sql = list_sequences_sql("public", PgCatalog::Pg);
+
+ assert!(sql.contains("pg_catalog.pg_class"));
+ assert!(sql.contains("pg_catalog.pg_sequence"));
+ assert!(sql.contains("n.nspname = 'public'"));
+ }
+
+ #[test]
+ fn list_sequences_compat_sql_avoids_sequence_catalogs() {
+ let sql = list_sequences_compat_sql("core's");
+
+ assert!(sql.contains("information_schema.sequences"));
+ assert!(sql.contains("sequence_schema = 'core''s'"));
+ assert!(!sql.contains("_sequence s ON"));
+ assert!(!sql.contains("pg_class"));
+ }
+
+ #[test]
+ fn sequence_last_values_sql_targets_catalog_function() {
+ assert!(sequence_last_values_sql("public", PgCatalog::Sys).contains("sys_catalog.sys_sequence_last_value"));
+ assert!(sequence_last_values_sql("public", PgCatalog::Pg).contains("pg_catalog.pg_sequence_last_value"));
+ }
+
+ #[test]
+ fn sequence_source_sql_escapes_schema_and_name_per_catalog() {
+ let sql = sequence_source_sql("core's", "seq's", PgCatalog::Sys);
+
+ assert!(sql.contains("sys_catalog.sys_class"));
+ assert!(sql.contains("'pg_class'::regclass"));
+ assert!(!sql.contains("'sys_class'::regclass"));
+ assert!(sql.contains("sys_get_userbyid(c.relowner)"));
+ assert!(sql.contains("n.nspname = 'core''s'"));
+ assert!(sql.contains("c.relname = 'seq''s'"));
+ assert!(sql.contains("information_schema.sequences"));
+
+ let pg_sql = sequence_source_sql("public", "orders_seq", PgCatalog::Pg);
+ assert!(pg_sql.contains("pg_catalog.pg_class"));
+ assert!(pg_sql.contains("'pg_class'::regclass"));
+ assert!(pg_sql.contains("pg_get_userbyid(c.relowner)"));
+ }
+
+ fn sequence_query_result(rows: Vec<Vec<serde_json::Value>>) -> db::QueryResult {
+ db::QueryResult {
+ columns: vec![
+ "relname".to_string(),
+ "data_type".to_string(),
+ "start_value".to_string(),
+ "min_value".to_string(),
+ "max_value".to_string(),
+ "increment".to_string(),
+ "cycle".to_string(),
+ ],
+ column_types: Vec::new(),
+ column_sortables: Vec::new(),
+ spatial_columns: vec![],
+ spatial_values: vec![],
+ rows,
+ affected_rows: 0,
+ execution_time_ms: 0,
+ server_execute_time_us: None,
+ query_timings_ms: None,
+ truncated: false,
+ session_id: None,
+ has_more: false,
+ elasticsearch_raw_body: None,
+ messages: Vec::new(),
+ }
+ }
+
+ #[test]
+ fn sequence_infos_map_catalog_rows_and_cycle_flag() {
+ let result = sequence_query_result(vec![
+ vec![
+ serde_json::json!("dbx_seq"),
+ serde_json::json!("bigint"),
+ serde_json::json!("5"),
+ serde_json::json!("1"),
+ serde_json::json!("9223372036854775807"),
+ serde_json::json!("2"),
+ serde_json::json!("NO"),
+ ],
+ vec![
+ serde_json::json!("cyc_seq"),
+ serde_json::json!("integer"),
+ serde_json::Value::Null,
+ serde_json::Value::Null,
+ serde_json::Value::Null,
+ serde_json::Value::Null,
+ serde_json::json!("YES"),
+ ],
+ ]);
+
+ let sequences = sequence_infos_from_query_result(result);
+
+ assert_eq!(sequences.len(), 2);
+ assert_eq!(sequences[0].name, "dbx_seq");
+ assert_eq!(sequences[0].data_type, "bigint");
+ assert_eq!(sequences[0].start_value, "5");
+ assert_eq!(sequences[0].increment, "2");
+ assert!(!sequences[0].cycle);
+ assert!(sequences[0].last_value.is_none());
+ assert_eq!(sequences[1].data_type, "integer");
+ assert_eq!(sequences[1].start_value, "1");
+ assert_eq!(sequences[1].min_value, "1");
+ assert_eq!(sequences[1].increment, "1");
+ assert!(sequences[1].cycle);
+ }
+
+ #[test]
+ fn apply_sequence_last_values_matches_by_name_and_ignores_nulls() {
+ let mut sequences = sequence_infos_from_query_result(sequence_query_result(vec![
+ vec![
+ serde_json::json!("dbx_seq"),
+ serde_json::json!("bigint"),
+ serde_json::json!("5"),
+ serde_json::json!("1"),
+ serde_json::json!("9223372036854775807"),
+ serde_json::json!("2"),
+ serde_json::json!("NO"),
+ ],
+ vec![
+ serde_json::json!("other_seq"),
+ serde_json::json!("bigint"),
+ serde_json::json!("1"),
+ serde_json::json!("1"),
+ serde_json::json!("9223372036854775807"),
+ serde_json::json!("1"),
+ serde_json::json!("NO"),
+ ],
+ ]));
+
+ let last_values = db::QueryResult {
+ columns: vec!["relname".to_string(), "last_value".to_string()],
+ rows: vec![
+ vec![serde_json::json!("dbx_seq"), serde_json::json!("5")],
+ vec![serde_json::json!("other_seq"), serde_json::Value::Null],
+ ],
+ ..sequence_query_result(vec![])
+ };
+ apply_sequence_last_values(&mut sequences, last_values);
+
+ assert_eq!(sequences[0].last_value.as_deref(), Some("5"));
+ assert!(sequences[1].last_value.is_none());
+ }
+}
diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rs
index a08b23353f..38bcf787d9 100644
--- a/crates/dbx-core/src/schema/mod.rs
+++ b/crates/dbx-core/src/schema/mod.rs
@@ -17,6 +17,7 @@ use std::future::Future;
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
+mod agent_pg_sequences;
mod kingbase;
mod mongodb_columns;
pub mod plugin_metadata;
@@ -3155,7 +3156,8 @@ mod tests {
use super::{list_databases_core, list_tables_core};
use super::{
object_types_include_custom_types, object_types_include_relations, object_types_include_routines,
- object_types_only_custom_types, supports_custom_type_details, supports_pg_custom_type_objects,
+ object_types_include_sequences, object_types_only_custom_types, supports_custom_type_details,
+ supports_pg_custom_type_objects, with_agent_pg_sequence_objects,
};
use crate::connection::{AppState, PoolKind};
@@ -3479,6 +3481,32 @@ mod tests {
assert!(!object_types_include_custom_types(Some(&["FUNCTION".to_string()])));
}
+ #[test]
+ fn object_types_include_sequences_only_for_sequence_requests() {
+ assert!(object_types_include_sequences(None));
+ assert!(object_types_include_sequences(Some(&["SEQUENCE".to_string()])));
+ assert!(object_types_include_sequences(Some(&["sequence".to_string()])));
+ assert!(object_types_include_sequences(Some(&["TABLE".to_string(), "SEQUENCE".to_string()])));
+ assert!(!object_types_include_sequences(Some(&["TABLE".to_string()])));
+ assert!(!object_types_include_sequences(Some(&[])));
+ }
+
+ #[test]
+ fn with_agent_pg_sequence_objects_merges_without_duplicating() {
+ let objects = vec![test_object_info("dbx9016_serial", "TABLE"), test_object_info("dbx9016_seq", "SEQUENCE")];
+ let sequences =
+ vec![test_object_info("dbx9016_seq", "SEQUENCE"), test_object_info("dbx9016_serial_id_seq", "SEQUENCE")];
+
+ let merged = with_agent_pg_sequence_objects(objects, &sequences);
+
+ assert_eq!(
+ merged.iter().map(|object| object.name.as_str()).collect::<Vec<_>>(),
+ ["dbx9016_serial", "dbx9016_seq", "dbx9016_serial_id_seq"]
+ );
+ assert_eq!(merged[1].object_type, "SEQUENCE");
+ assert_eq!(merged[2].object_type, "SEQUENCE");
+ }
+
#[test]
fn object_types_select_independent_catalog_branches() {
assert!(object_types_include_relations(None));
@@ -7023,7 +7051,7 @@ async fn list_objects_once(
let mut client = lock_sqlserver_metadata_client(&client).await?;
return db::sqlserver::list_objects(&mut client, schema).await.map(unpaged_object_list);
}
- if let Some(client) = extract_pool!(pool_handle.as_ref(), Agent) {
+ if let Some(agent_client) = extract_pool!(pool_handle.as_ref(), Agent) {
let is_oracle = db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::Oracle);
let use_oracle_agent_paging = db_config.as_ref().is_some_and(is_default_oracle_agent_config);
let filter_locally_after_oracle_comments =
@@ -7031,11 +7059,21 @@ async fn list_objects_once(
let timeout_duration = agent_metadata_timeout(db_config.as_ref());
let fallback_config = db_config.clone();
if is_oracle && !use_oracle_agent_paging {
- return oracle_agent_list_objects(client, database, schema, timeout_duration)
+ return oracle_agent_list_objects(agent_client, database, schema, timeout_duration)
.await
.map(unpaged_object_list);
}
- let mut client = client.lock().await;
+ // KingbaseES/Vastbase agents list relations, routines and types but
+ // never sequences, so their pg_class-backed sequence list is merged
+ // into whatever the agent returns (t8y2/dbx#9016).
+ let sequence_objects = if db_config.as_ref().is_some_and(is_agent_pg_sequence_config)
+ && object_types_include_sequences(object_types)
+ {
+ agent_pg_sequence_objects(agent_client.clone(), database, schema, timeout_duration).await
+ } else {
+ Vec::new()
+ };
+ let mut client = agent_client.lock().await;
let agent_filter = if filter_locally_after_oracle_comments { None } else { filter };
let agent_limit = if filter_locally_after_oracle_comments || force_local_table_name_filter {
None
@@ -7074,7 +7112,7 @@ async fn list_objects_once(
)
.await?;
}
- return Ok(unpaged_object_list(objects));
+ return Ok(unpaged_object_list(with_agent_pg_sequence_objects(objects, &sequence_objects)));
}
Ok(objects) => {
if object_types_only_custom_types(object_types) {
@@ -7087,11 +7125,13 @@ async fn list_objects_once(
if let Some(config) = fallback_config.as_ref() {
match native_postgres_metadata_pool(state, connection_id, database, config).await {
Ok(Some(pool)) => {
- return list_native_postgres_objects(&pool, config, schema)
- .await
- .map(unpaged_object_list)
+ let objects = list_native_postgres_objects(&pool, config, schema).await?;
+ return Ok(unpaged_object_list(with_agent_pg_sequence_objects(
+ objects,
+ &sequence_objects,
+ )));
}
- Ok(None) => return Ok(unpaged_object_list(objects)),
+ Ok(None) => {}
Err(error) => {
log::warn!(
"[schema][agent:list_objects:fallback-failed] connection_id={} database={} schema={} error={}",
@@ -7103,7 +7143,7 @@ async fn list_objects_once(
}
}
}
- return Ok(unpaged_object_list(objects));
+ return Ok(unpaged_object_list(with_agent_pg_sequence_objects(objects, &sequence_objects)));
}
Err(agent_error) => {
if object_types_only_custom_types(object_types) {
@@ -8825,6 +8865,19 @@ pub async fn list_sequences_core(
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 db_config = connection_config(state, connection_id).await;
+ if db_config.as_ref().is_some_and(is_agent_pg_sequence_config) {
+ let pool_handle = state.pool_handle(&pool_key).await;
+ if let Some(client) = extract_pool!(pool_handle.as_ref(), Agent) {
+ return agent_pg_sequences::list_sequences(
+ client,
+ database,
+ schema,
+ with_last_values,
+ agent_metadata_timeout(db_config.as_ref()),
+ )
+ .await;
+ }
+ }
let pool = clone_metadata_pool(state, &pool_key).await.ok_or("Pool not found")?;
match &pool {
@@ -9392,6 +9445,70 @@ fn is_opengauss_constraint_config(config: &ConnectionConfig) -> bool {
config.db_type == DatabaseType::OpenGauss || config.driver_profile.as_deref() == Some("opengauss")
}
+/// KingbaseES and Vastbase run through Agent pools but expose PostgreSQL's
+/// sequence catalogs, so their sequence metadata is queried over the agent
+/// connection (t8y2/dbx#9016). HighGo/UXDB stay on their existing paths.
+fn is_agent_pg_sequence_config(config: &ConnectionConfig) -> bool {
+ matches!(config.db_type, DatabaseType::Kingbase | DatabaseType::Vastbase)
+}
+
+/// Sequences for agent-backed PostgreSQL-family engines, shaped like the
+/// relation listing the native PostgreSQL driver returns for `relkind = 'S'`.
+async fn agent_pg_sequence_objects(
+ client: Arc<db::agent_driver::PooledAgentClient>,
+ database: &str,
+ schema: &str,
+ timeout_duration: Option<Duration>,
+) -> Vec<db::ObjectInfo> {
+ match agent_pg_sequences::list_sequences(client, database, schema, false, timeout_duration).await {
+ Ok(sequences) => sequences
+ .into_iter()
+ .map(|sequence| db::ObjectInfo {
+ name: sequence.name,
+ object_type: "SEQUENCE".to_string(),
+ schema: if schema.is_empty() { None } else { Some(schema.to_string()) },
+ valid: None,
+ signature: None,
+ custom_type_kind: None,
+ has_members: None,
+ comment: None,
+ created_at: None,
+ updated_at: None,
+ parent_schema: None,
+ parent_name: None,
+ trigger: None,
+ xugu_type_members_expandable: None,
+ })
+ .collect(),
+ Err(error) => {
+ log::warn!(
+ "[schema][agent:list_objects:sequences-failed] database={} schema={} error={}",
+ database,
+ schema,
+ error
+ );
+ Vec::new()
+ }
+ }
+}
+
+fn with_agent_pg_sequence_objects(
+ mut objects: Vec<db::ObjectInfo>,
+ sequences: &[db::ObjectInfo],
+) -> Vec<db::ObjectInfo> {
+ for sequence in sequences {
+ let already_listed = objects.iter().any(|object| {
+ object.name == sequence.name
+ && normalize_object_info_object_type(&object.object_type)
+ == normalize_object_info_object_type(&sequence.object_type)
+ });
+ if !already_listed {
+ objects.push(sequence.clone());
+ }
+ }
+ objects
+}
+
fn is_opengauss_family_config(config: &ConnectionConfig) -> bool {
matches!(config.db_type, DatabaseType::OpenGauss | DatabaseType::Gaussdb)
|| matches!(config.driver_profile.as_deref(), Some("opengauss" | "gaussdb"))
@@ -9430,6 +9547,10 @@ fn object_types_include_relations(object_types: Option<&[String]>) -> bool {
})
}
+fn object_types_include_sequences(object_types: Option<&[String]>) -> bool {
+ object_types.is_none_or(|types| types.iter().any(|t| t.eq_ignore_ascii_case("SEQUENCE")))
+}
+
fn object_types_include_routines(object_types: Option<&[String]>) -> bool {
object_types
.is_none_or(|types| types.iter().any(|t| matches!(t.to_ascii_uppercase().as_str(), "PROCEDURE" | "FUNCTION")))
@@ -10626,6 +10747,29 @@ async fn get_object_source_once(
agent_metadata_timeout(db_config.as_ref()),
)
.await?
+ } else if matches!(object_type, db::ObjectSourceKind::Sequence)
+ && db_config.as_ref().is_some_and(is_agent_pg_sequence_config)
+ {
+ match agent_pg_sequences::sequence_source(
+ client.clone(),
+ database,
+ schema,
+ name,
+ agent_metadata_timeout(db_config.as_ref()),
+ )
+ .await?
+ {
+ Some(source) => {
+ return Ok(db::ObjectSource {
+ name: name.to_string(),
+ object_type,
+ schema: if schema.is_empty() { None } else { Some(schema.to_string()) },
+ source,
+ editable: None,
+ });
+ }
+ None => String::new(),
+ }
} else {
let mut client = client.lock().await;
let result: db::ObjectSource = client