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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(postgres): keep opclass joins before where in gaussdb compat sql
弈秋45c0ccadcd7a
diff --git a/crates/dbx-driver-postgres/src/postgres.rs b/crates/dbx-driver-postgres/src/postgres.rsindex 9a6132c0d2..25a90c526a 100644--- a/crates/dbx-driver-postgres/src/postgres.rs+++ b/crates/dbx-driver-postgres/src/postgres.rs@@ -5437,9 +5437,9 @@ fn postgres_indexes_for_relations_compat_sql() -> &'static str {ON a.attrelid = t.oid \AND a.attnum = (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 \AND a.attnum > 0 \- WHERE (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 >= 0 \LEFT JOIN pg_opclass oc \ON oc.oid = (string_to_array(ix.indclass::text, ' '))[pos.n]::oid \+ WHERE (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 >= 0 \ORDER BY pos.n \) AS column_opclasses, \(ix.indisunique AND ix.indisvalid) AS is_unique, \@@ -9704,6 +9704,11 @@ const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \// Compat-tier sibling of `postgres_indexes_for_relations_compat_sql` (~line// 3312) — see the note on `POSTGRES_INDEXES_SQL` above.+//+// The opclass subquery has to join `pg_opclass` before its WHERE: a JOIN after+// WHERE is a syntax error, and this tier is the only one GaussDB / openGauss+// ever run (`list_indexes_compat_first`), so the misordered join broke every+// index listing there with `syntax error at or near "LEFT"` (#12650, #12675).const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \ARRAY( \SELECT COALESCE(a.attname::text, pg_get_indexdef(ix.indexrelid, pos.n, false)) \@@ -9724,9 +9729,9 @@ const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \ON a.attrelid = t.oid \AND a.attnum = (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 \AND a.attnum > 0 \- WHERE (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 >= 0 \LEFT JOIN pg_opclass oc \ON oc.oid = (string_to_array(ix.indclass::text, ' '))[pos.n]::oid \+ WHERE (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 >= 0 \ORDER BY pos.n \) AS column_opclasses, \(ix.indisunique AND ix.indisvalid) AS is_unique, \@@ -15491,6 +15496,50 @@ mod tests {}}+ #[test]+ fn compat_index_queries_keep_the_opclass_join_before_the_where_clause() {+ // Both compat queries used to build the opclass subquery as+ // `FROM generate_series(...) LEFT JOIN pg_attribute ... WHERE <key filter> LEFT JOIN pg_opclass ...`.+ // A JOIN after WHERE is a syntax error, and GaussDB / openGauss never run the modern tier+ // (`list_indexes_compat_first`), so every index listing — and every schema comparison that+ // loads one — died with `syntax error at or near "LEFT"` on the opclass join (#12650, #12675).+ for sql in [POSTGRES_INDEXES_COMPAT_SQL, postgres_indexes_for_relations_compat_sql()] {+ let compact = sql.split_whitespace().collect::<Vec<_>>().join(" ");+ assert_eq!(compact.matches("LEFT JOIN pg_opclass oc").count(), 1, "expected one opclass join in {sql}");+ assert!(!compact.contains(">= 0 LEFT JOIN"), "a JOIN after the key filter's WHERE is invalid: {sql}");+ }+ }++ #[tokio::test]+ #[ignore = "requires DBX_TEST_POSTGRES_URL pointing at a writable PostgreSQL database"]+ async fn compat_index_query_parses_and_reports_index_columns() {+ // The compat tier is the only tier GaussDB and openGauss ever run, so it has to be valid+ // SQL against a real server and not merely a plausible-looking string (#12650, #12675).+ let url = std::env::var("DBX_TEST_POSTGRES_URL").expect("DBX_TEST_POSTGRES_URL");+ let pool = connect(&url, Duration::from_secs(5)).await.expect("connect postgres");+ let timeout = crate::db::connection_timeout();+ let client = checkout_postgres_client(&pool, None, timeout).await.expect("checkout client");+ let table = format!("dbx_compat_index_{}", std::process::id());+ client+ .batch_execute(&format!(+ "DROP TABLE IF EXISTS public.{table}; \+ CREATE TABLE public.{table} (id int PRIMARY KEY, email text, tag text); \+ CREATE INDEX {table}_email_tag ON public.{table} (email, tag)"+ ))+ .await+ .expect("create index fixtures");+ let indexes = list_indexes_compat_first(&pool, "public", &table).await.expect("compat index listing");+ client.batch_execute(&format!("DROP TABLE IF EXISTS public.{table}")).await.expect("drop index fixtures");+ let primary_name = format!("{table}_pkey");+ let primary = indexes.iter().find(|index| index.name == primary_name).expect("primary key index");+ assert_eq!(primary.columns, vec!["id".to_string()]);+ assert!(primary.is_primary && primary.is_unique);+ let composite_name = format!("{table}_email_tag");+ let composite = indexes.iter().find(|index| index.name == composite_name).expect("composite index");+ assert_eq!(composite.columns, vec!["email".to_string(), "tag".to_string()]);+ assert!(!composite.is_primary && !composite.is_unique);+ }+#[test]fn postgres_index_metadata_schema_qualifies_opclass() {// The LATERAL (modern) index-introspection SQL returns each non-default