Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(postgres): exclude partition children from listTables

Tobi3ba1d6e2fb29
diff --git a/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift b/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
index e1fa0b5..8634491 100644
--- a/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
+++ b/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
@@ -8,7 +8,6 @@ import PostgresNIO
import NIOCore
import NIOPosix
import NIOSSL
-import Logging
final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Sendable {
let databaseType: DatabaseType = .postgresql
@@ -16,7 +15,6 @@ final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Se
private var client: PostgresClient?
private var clientTask: Task<Void, Never>?
private let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
- private let logger = Logger(label: "com.gridex.postgresql")
deinit {
clientTask?.cancel()
@@ -271,21 +269,25 @@ final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Se
func listTables(schema: String?) async throws -> [TableInfo] {
let schemaFilter = schema ?? "public"
+ // relkind 'r' = ordinary table, 'p' = partitioned table parent.
+ // `NOT relispartition` excludes partition children — they show up as
+ // ordinary 'r' rows but the parent already represents them in the UI;
+ // including them would double-count rows (regression vs the original
+ // information_schema query, which only returned 'BASE TABLE' parents).
let result = try await executeParameterized(sql: """
SELECT c.relname, NULLIF(c.reltuples::bigint, -1)
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = $1
AND c.relkind IN ('r', 'p')
+ AND NOT c.relispartition
ORDER BY c.relname
""", params: [schemaFilter])
- let tables = result.rows.compactMap { row -> TableInfo? in
+ return result.rows.compactMap { row -> TableInfo? in
guard let name = row.first?.stringValue else { return nil }
let count = row.count > 1 ? row[1].intValue : nil
return TableInfo(name: name, schema: schemaFilter, type: .table, estimatedRowCount: count)
}
- logger.debug("Loaded tables", metadata: ["schema": "\(schemaFilter)", "count": "\(tables.count)"])
- return tables
}
func listViews(schema: String?) async throws -> [ViewInfo] {
diff --git a/macos/Tests/GridexTests/PostgreSQLAdapterIntegrationTests.swift b/macos/Tests/GridexTests/PostgreSQLAdapterIntegrationTests.swift
index fe8da7c..9284ce0 100644
--- a/macos/Tests/GridexTests/PostgreSQLAdapterIntegrationTests.swift
+++ b/macos/Tests/GridexTests/PostgreSQLAdapterIntegrationTests.swift
@@ -260,4 +260,50 @@ final class PostgreSQLAdapterIntegrationTests: XCTestCase {
"pg_class-backed listTables should include HighGo-managed physical tables (hg_*) that information_schema.tables can hide"
)
}
+
+ // MARK: - Vanilla PG regression — partition children must not surface as siblings
+
+ // Declarative partitioning (PG 11+): a partition child has relkind='r' and
+ // relispartition=true. The parent (relkind='p') already represents its
+ // children's data in the UI, so leaking children would double-count rows
+ // and clutter the sidebar. The original information_schema.tables query
+ // returned only 'BASE TABLE' parents; switching to pg_class introduced
+ // the leak unless we also filter `NOT relispartition`.
+ func test_listTables_excludesPartitionChildren_onVanillaPG() async throws {
+ try skipIfNoServer(port: 55434)
+
+ let cfg = makeBaseConfig(host: "127.0.0.1", port: 55434, sslMode: .disabled)
+ let adapter = PostgreSQLAdapter()
+ try await adapter.connect(config: cfg, password: nil)
+
+ // Clean previous fixtures, then create a partitioned table + one partition.
+ let prefix = "gridex_part_test_\(UUID().uuidString.prefix(8).lowercased())"
+ let plain = "\(prefix)_plain"
+ let parent = "\(prefix)_parent"
+ let child = "\(prefix)_p1"
+ do {
+ _ = try await adapter.executeRaw(sql: "CREATE TABLE \(plain) (id int)")
+ _ = try await adapter.executeRaw(sql: "CREATE TABLE \(parent) (id int) PARTITION BY RANGE (id)")
+ _ = try await adapter.executeRaw(sql: "CREATE TABLE \(child) PARTITION OF \(parent) FOR VALUES FROM (0) TO (100)")
+
+ let names = try await adapter.listTables(schema: "public").map(\.name)
+
+ XCTAssertTrue(names.contains(plain),
+ "ordinary table \(plain) must appear")
+ XCTAssertTrue(names.contains(parent),
+ "partitioned-table parent \(parent) must appear")
+ XCTAssertFalse(names.contains(child),
+ "partition child \(child) must NOT appear as a sibling — it would double-count the parent's data")
+ } catch {
+ // Best-effort cleanup, then rethrow.
+ _ = try? await adapter.executeRaw(sql: "DROP TABLE IF EXISTS \(parent) CASCADE")
+ _ = try? await adapter.executeRaw(sql: "DROP TABLE IF EXISTS \(plain)")
+ try? await adapter.disconnect()
+ throw error
+ }
+
+ _ = try? await adapter.executeRaw(sql: "DROP TABLE IF EXISTS \(parent) CASCADE")
+ _ = try? await adapter.executeRaw(sql: "DROP TABLE IF EXISTS \(plain)")
+ try await adapter.disconnect()
+ }
}