From 902419a3b98d41d1cec51524b4d76f8ff3afb577 Mon Sep 17 00:00:00 2001 From: zipg Date: Wed, 24 Jun 2026 19:12:28 +0800 Subject: [PATCH] fix(postgres): compatibility with older PostgreSQL versions for table DDL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: 修复 PostgreSQL CTE DML 分页改写 * fix: 兼容 PostgreSQL 旧版本索引 DDL 查询 --------- Co-authored-by: staff --- crates/dbx-core/src/db/postgres.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/dbx-core/src/db/postgres.rs b/crates/dbx-core/src/db/postgres.rs index c115a73891..88a15348d9 100644 --- a/crates/dbx-core/src/db/postgres.rs +++ b/crates/dbx-core/src/db/postgres.rs @@ -1952,7 +1952,15 @@ const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \ ORDER BY i.relname"; const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \ - array_agg(COALESCE(a.attname, pg_get_indexdef(ix.indexrelid, k.n::int, true)) ORDER BY k.n) AS columns, \ + ARRAY( \ + SELECT COALESCE(a.attname, pg_get_indexdef(ix.indexrelid, pos.n, true)) \ + FROM generate_series(1, array_length(string_to_array(ix.indkey::text, ' '), 1)) AS pos(n) \ + LEFT JOIN pg_attribute a \ + ON a.attrelid = t.oid \ + AND a.attnum = (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 \ + AND a.attnum > 0 \ + ORDER BY pos.n \ + ) AS columns, \ ix.indisunique AS is_unique, \ ix.indisprimary AS is_primary, \ pg_get_expr(ix.indpred, ix.indrelid) AS filter_expr, \ @@ -1965,10 +1973,7 @@ const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \ JOIN pg_class i ON i.oid = ix.indexrelid \ JOIN pg_namespace n ON n.oid = t.relnamespace \ JOIN pg_am am ON am.oid = i.relam \ - JOIN LATERAL unnest(ix.indkey) WITH ORDINALITY AS k(attnum, n) ON true \ - LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum AND k.attnum > 0 \ WHERE n.nspname = $1 AND t.relname = $2 \ - GROUP BY i.relname, i.oid, ix.indisunique, ix.indisprimary, ix.indpred, ix.indrelid, am.amname, ix.indkey \ ORDER BY i.relname"; const POSTGRES_OWNERS_SQL: &str = @@ -2762,6 +2767,10 @@ mod tests { assert!(POSTGRES_INDEXES_SQL.contains("ix.indnkeyatts")); assert!(!POSTGRES_INDEXES_COMPAT_SQL.contains("ix.indnkeyatts")); assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("NULL::smallint AS nkeyatts")); + assert!(!POSTGRES_INDEXES_COMPAT_SQL.contains("LATERAL")); + assert!(!POSTGRES_INDEXES_COMPAT_SQL.contains("WITH ORDINALITY")); + assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("generate_series")); + assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("string_to_array(ix.indkey::text, ' ')")); } #[test]