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(redshift): use PostgreSQL 8.0 catalogs for DDL and procedure source

ordinary beingecb14099bfcf
diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rs
index fa4e3b3f50..f3a637466f 100644
--- a/crates/dbx-core/src/schema/mod.rs
+++ b/crates/dbx-core/src/schema/mod.rs
@@ -12138,6 +12138,21 @@ async fn get_object_source_once(
// only view
db::questdb::questdb_object_source(pool, name).await?
}
+ PoolKind::Postgres(pool)
+ if db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::Redshift)
+ && matches!(object_type, db::ObjectSourceKind::Procedure) =>
+ {
+ // Redshift has no pg_proc.prokind / pg_get_functiondef
+ // (PostgreSQL 8.0 catalog); read the full CREATE PROCEDURE
+ // text from prosrc and use pg_proc_info for the routine
+ // kind (issue #13293).
+ db::postgres::execute_query(
+ pool,
+ &db::postgres::redshift_procedure_source_sql(schema, name, signature),
+ )
+ .await
+ .and_then(first_string_cell)?
+ }
PoolKind::Postgres(pool) => {
let unwrap_opengauss_record = db_config.as_ref().is_some_and(is_opengauss_family_config);
let isolate_view_search_path = postgres_view_source_uses_isolated_search_path(
@@ -12760,19 +12775,33 @@ impl Default for PostgresCatalogCaps {
}
}
-/// Probe the catalog features once per object-source fetch. A failed probe
-/// (permissions, proxies) stays on the modern queries and lets the existing
-/// error-triggered fallbacks handle legacy servers.
-async fn postgres_object_source_catalog_caps(pool: &deadpool_postgres::Pool) -> PostgresCatalogCaps {
- const PROBE_SQL: &str = "SELECT EXISTS ( \
+/// Catalog-feature probe for object-source queries.
+///
+/// `to_regclass` is PostgreSQL 9.4+, so it errors on legacy forks such as
+/// Redshift (8.0.2) and the failed probe would leave the modern capabilities
+/// (including `pg_proc.prokind`, PostgreSQL 11+) enabled. Probe `pg_sequence`
+/// through the PostgreSQL 8.0 catalogs instead so those servers actually take
+/// the legacy paths (issue #13293).
+const POSTGRES_OBJECT_SOURCE_CATALOG_CAPS_SQL: &str = "SELECT EXISTS ( \
SELECT 1 \
FROM pg_catalog.pg_attribute \
WHERE attrelid = 'pg_catalog.pg_proc'::regclass \
AND attname = 'prokind' \
AND NOT attisdropped \
), \
- to_regclass('pg_catalog.pg_sequence') IS NOT NULL";
- match db::postgres::execute_query(pool, PROBE_SQL).await {
+ EXISTS ( \
+ SELECT 1 \
+ FROM pg_catalog.pg_class c \
+ JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
+ WHERE n.nspname = 'pg_catalog' \
+ AND c.relname = 'pg_sequence' \
+ )";
+
+/// Probe the catalog features once per object-source fetch. A failed probe
+/// (permissions, proxies) stays on the modern queries and lets the existing
+/// error-triggered fallbacks handle legacy servers.
+async fn postgres_object_source_catalog_caps(pool: &deadpool_postgres::Pool) -> PostgresCatalogCaps {
+ match db::postgres::execute_query(pool, POSTGRES_OBJECT_SOURCE_CATALOG_CAPS_SQL).await {
Ok(result) => {
let values = result.rows.first().cloned().unwrap_or_default();
PostgresCatalogCaps {
@@ -12860,7 +12889,7 @@ async fn postgres_object_source(
}
Err(primary_err)
if postgres_missing_prokind_error(&primary_err)
- && matches!(object_type, db::ObjectSourceKind::Function) =>
+ && matches!(object_type, db::ObjectSourceKind::Function | db::ObjectSourceKind::Procedure) =>
{
let fallback_sql = postgres_function_object_source_sql_without_prokind(schema, name, false);
db::postgres::execute_query(pool, &fallback_sql)
@@ -13218,6 +13247,19 @@ mod object_source_tests {
assert!(legacy.contains("owned by ' || quote_ident(owned.relname)"));
}
+ #[test]
+ fn object_source_capability_probe_avoids_post_pg8_catalog_helpers() {
+ // Redshift is based on PostgreSQL 8.0.2: to_regclass (9.4+) errors,
+ // and the failed probe would keep the modern prokind / pg_sequence
+ // queries enabled (#13293).
+ let sql = super::POSTGRES_OBJECT_SOURCE_CATALOG_CAPS_SQL;
+ assert!(sql.contains("attname = 'prokind'"));
+ assert!(sql.contains("FROM pg_catalog.pg_class c"));
+ assert!(sql.contains("JOIN pg_catalog.pg_namespace n"));
+ assert!(sql.contains("c.relname = 'pg_sequence'"));
+ assert!(!sql.contains("to_regclass"));
+ }
+
#[test]
fn missing_prokind_gate_matches_localized_undefined_column_errors() {
// The exact strings from #11161: PostgreSQL reports undefined-column
diff --git a/crates/dbx-driver-postgres/src/postgres.rs b/crates/dbx-driver-postgres/src/postgres.rs
index 2c268946aa..77976032ab 100644
--- a/crates/dbx-driver-postgres/src/postgres.rs
+++ b/crates/dbx-driver-postgres/src/postgres.rs
@@ -4470,12 +4470,11 @@ pub async fn get_table_partition_info(
}
let params: [&(dyn tokio_postgres::types::ToSql + Sync); 2] = [&schema, &table];
- let rows = query_with_compat_fallback(
- "get_table_partition_info",
- &[postgres_table_partition_info_sql(), postgres_table_partition_info_compat_sql()],
- |sql| postgres_query_cached(client, sql, &params),
- )
- .await?;
+ let rows =
+ query_with_compat_fallback("get_table_partition_info", &postgres_table_partition_info_query_tiers(), |sql| {
+ postgres_query_cached(client, sql, &params)
+ })
+ .await?;
let Some(row) = rows.first() else {
let inherits_parents = if has_inherits_parent {
get_table_inherits_parents_inner(client, schema, table).await?
@@ -5064,6 +5063,27 @@ fn postgres_partition_tree_compat_sql() -> &'static str {
WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r','p','f')"
}
+/// Redshift is based on PostgreSQL 8.0.2 and has neither `pg_foreign_table`
+/// nor `pg_foreign_server` (both PostgreSQL 9.1+), so the pre-10 compat query
+/// still fails there. Keep the same column shape with NULL foreign-table
+/// fields; Redshift has no declarative partitions, so the requested relation
+/// is the whole tree (issue #13293).
+fn postgres_partition_tree_legacy_sql() -> &'static str {
+ "SELECT c.oid::bigint AS oid, n.nspname::text AS schema, c.relname::text AS relname, \
+ NULL::bigint AS parent_oid, NULL::text AS parent_schema, NULL::text AS parent_relname, \
+ c.relkind::text AS relkind, \
+ NULL::text AS partition_bound, NULL::text AS partition_key, \
+ NULL::text AS foreign_server, \
+ NULL::text[] AS foreign_options \
+ FROM pg_catalog.pg_class c \
+ JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
+ WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r','p','f')"
+}
+
+fn postgres_partition_tree_query_tiers() -> [&'static str; 3] {
+ [postgres_partition_tree_sql(), postgres_partition_tree_compat_sql(), postgres_partition_tree_legacy_sql()]
+}
+
/// The whole partition tree rooted at (schema, table) — the root itself plus
/// every descendant partition at any depth — plus each node's partition info,
/// in a single round trip. Used by `pg_ddl_with_partitions` instead of
@@ -5078,12 +5098,11 @@ pub async fn fetch_postgres_partition_tree(
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let client = &EncodingClient::new(&client);
let params: [&(dyn tokio_postgres::types::ToSql + Sync); 2] = [&schema, &table];
- let rows = query_with_compat_fallback(
- "fetch_postgres_partition_tree",
- &[postgres_partition_tree_sql(), postgres_partition_tree_compat_sql()],
- |sql| postgres_query_cached(client, sql, &params),
- )
- .await?;
+ let rows =
+ query_with_compat_fallback("fetch_postgres_partition_tree", &postgres_partition_tree_query_tiers(), |sql| {
+ postgres_query_cached(client, sql, &params)
+ })
+ .await?;
Ok(rows
.into_iter()
.filter_map(|row| {
@@ -6245,6 +6264,28 @@ fn postgres_table_partition_info_compat_sql() -> &'static str {
LIMIT 1"
}
+/// Redshift (PostgreSQL 8.0.2) lacks `pg_foreign_table`/`pg_foreign_server`
+/// (PostgreSQL 9.1+), so the pre-10 compat query still fails there. Keep the
+/// same column shape with NULL foreign-table fields (issue #13293).
+fn postgres_table_partition_info_legacy_sql() -> &'static str {
+ "SELECT NULL::text AS parent_schema, NULL::text AS parent_table, NULL::text AS partition_bound, \
+ NULL::text AS partition_key, \
+ NULL::text AS foreign_server, \
+ NULL::text[] AS foreign_options \
+ FROM pg_catalog.pg_class c \
+ JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
+ WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r','p','f') \
+ LIMIT 1"
+}
+
+fn postgres_table_partition_info_query_tiers() -> [&'static str; 3] {
+ [
+ postgres_table_partition_info_sql(),
+ postgres_table_partition_info_compat_sql(),
+ postgres_table_partition_info_legacy_sql(),
+ ]
+}
+
fn postgres_table_inherits_parents_sql() -> &'static str {
// Traditional-inheritance parents only: a declarative partition child
// is also a `pg_inherits` row, but its parent has relkind 'p', which we
@@ -6578,6 +6619,38 @@ fn redshift_routine_objects_sql() -> &'static str {
ORDER BY sort_order, object_name"
}
+/// Source of a Redshift stored procedure.
+///
+/// Redshift's `pg_proc` is the PostgreSQL 8.0 catalog: it has no `prokind`
+/// (PostgreSQL 11) and no `pg_get_functiondef` (PostgreSQL 8.4). The routine
+/// kind is exposed by the Redshift-only `pg_proc_info` view, and a stored
+/// procedure's complete `CREATE [OR REPLACE] PROCEDURE` text is stored in
+/// `pg_proc.prosrc`, so it is read directly (issue #13293). The signature from
+/// `svv_redshift_functions` only orders same-named procedures; it is not a
+/// filter so a formatting difference can never hide the only match.
+pub fn redshift_procedure_source_sql(schema: &str, name: &str, signature: Option<&str>) -> String {
+ let signature_order = signature
+ .map(str::trim)
+ .filter(|value| !value.is_empty())
+ .map(|value| {
+ format!(
+ "CASE WHEN pg_catalog.oidvectortypes(p.proargtypes) = {} THEN 0 ELSE 1 END, ",
+ pg_quote_literal(value)
+ )
+ })
+ .unwrap_or_default();
+ format!(
+ "SELECT p.prosrc \
+ FROM pg_catalog.pg_proc p \
+ JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
+ JOIN pg_catalog.pg_proc_info pi ON pi.prooid = p.oid \
+ WHERE n.nspname = {} AND p.proname = {} AND pi.prokind = 'p' \
+ ORDER BY {signature_order}p.oid LIMIT 1",
+ pg_quote_literal(schema),
+ pg_quote_literal(name)
+ )
+}
+
/// SQL for listing user-defined types in one schema.
///
/// Only explicitly created types are returned: base types (b), standalone
@@ -16292,6 +16365,51 @@ mod tests {
assert!(!sql.contains("UNION"));
}
+ #[test]
+ fn redshift_procedure_source_uses_proc_info_and_prosrc() {
+ let sql = redshift_procedure_source_sql("tenant's", "load_orders", None);
+ assert!(sql.contains("SELECT p.prosrc"));
+ assert!(sql.contains("JOIN pg_catalog.pg_proc_info pi ON pi.prooid = p.oid"));
+ assert!(sql.contains("pi.prokind = 'p'"));
+ assert!(sql.contains("n.nspname = 'tenant''s'"));
+ assert!(sql.contains("p.proname = 'load_orders'"));
+ // Redshift's pg_proc is the 8.0 catalog; these would fail there.
+ assert!(!sql.contains("pg_get_functiondef"));
+ assert!(!sql.contains("p.prokind"));
+ }
+
+ #[test]
+ fn redshift_procedure_source_orders_by_signature_without_filtering() {
+ let sql = redshift_procedure_source_sql("public", "load_orders", Some("integer, character varying"));
+ assert!(sql.contains(
+ "CASE WHEN pg_catalog.oidvectortypes(p.proargtypes) = 'integer, character varying' THEN 0 ELSE 1 END, p.oid"
+ ));
+ assert!(!sql.contains("AND pg_catalog.oidvectortypes"));
+ let without_signature = redshift_procedure_source_sql("public", "load_orders", None);
+ assert!(without_signature.contains("ORDER BY p.oid LIMIT 1"));
+ assert!(!without_signature.contains("CASE WHEN"));
+ }
+
+ #[test]
+ fn partition_query_tiers_fall_back_without_foreign_catalogs() {
+ let tree = postgres_partition_tree_query_tiers();
+ assert!(tree[0].contains("pg_catalog.pg_foreign_table"));
+ assert!(tree[1].contains("pg_catalog.pg_foreign_table"));
+ assert!(!tree[2].contains("pg_foreign_table"));
+ assert!(!tree[2].contains("pg_foreign_server"));
+ assert!(!tree[2].contains("relispartition"));
+ assert!(!tree[2].contains("pg_get_partkeydef"));
+ assert!(tree[2].contains("NULL::text[] AS foreign_options"));
+
+ let info = postgres_table_partition_info_query_tiers();
+ assert!(info[1].contains("pg_catalog.pg_foreign_table"));
+ assert!(!info[2].contains("pg_foreign_table"));
+ assert!(!info[2].contains("pg_foreign_server"));
+ assert!(!info[2].contains("relispartition"));
+ assert!(!info[2].contains("pg_get_expr"));
+ assert!(info[2].contains("NULL::text[] AS foreign_options"));
+ }
+
#[test]
fn redshift_relation_objects_sql_avoids_unsupported_postgres_helpers() {
let sql = list_objects_sql(false, false, false, false, true, false, false, false);