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): list tables from pg_catalog

cyenx89af6406fff1
diff --git a/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift b/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
index 99f1ee9..e1fa0b5 100644
--- a/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
+++ b/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
@@ -8,6 +8,7 @@ import PostgresNIO
import NIOCore
import NIOPosix
import NIOSSL
+import Logging
final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Sendable {
let databaseType: DatabaseType = .postgresql
@@ -15,6 +16,7 @@ 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()
@@ -270,19 +272,20 @@ final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Se
func listTables(schema: String?) async throws -> [TableInfo] {
let schemaFilter = schema ?? "public"
let result = try await executeParameterized(sql: """
- SELECT t.table_name,
- (SELECT reltuples::bigint FROM pg_class c
- JOIN pg_namespace n ON n.oid = c.relnamespace
- WHERE c.relname = t.table_name AND n.nspname = $1)
- FROM information_schema.tables t
- WHERE t.table_schema = $1 AND t.table_type = 'BASE TABLE'
- ORDER BY t.table_name
+ 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')
+ ORDER BY c.relname
""", params: [schemaFilter])
- return result.rows.compactMap { row -> TableInfo? in
+ let tables = 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 a83f99d..cb2e43d 100644
--- a/macos/Tests/GridexTests/PostgreSQLAdapterIntegrationTests.swift
+++ b/macos/Tests/GridexTests/PostgreSQLAdapterIntegrationTests.swift
@@ -205,4 +205,43 @@ final class PostgreSQLAdapterIntegrationTests: XCTestCase {
}
}
}
+
+ func test_listTables_usesPgCatalogOnHighGoSecure() async throws {
+ let env = ProcessInfo.processInfo.environment
+ guard let host = env["GRIDEX_HIGHGO_HOST"],
+ let port = Int(env["GRIDEX_HIGHGO_PORT"] ?? ""),
+ let database = env["GRIDEX_HIGHGO_DATABASE"],
+ let username = env["GRIDEX_HIGHGO_USER"],
+ let password = env["GRIDEX_HIGHGO_PASSWORD"] else {
+ throw XCTSkip("Set GRIDEX_HIGHGO_* to run the HighGo Secure catalog regression test")
+ }
+
+ let cfg = ConnectionConfig(
+ id: UUID(),
+ name: "highgo-catalog-regression",
+ databaseType: .postgresql,
+ host: host,
+ port: port,
+ database: database,
+ username: username,
+ sslEnabled: false,
+ sslMode: .disabled
+ )
+ let adapter = PostgreSQLAdapter()
+ try await adapter.connect(config: cfg, password: password)
+
+ let tables: [TableInfo]
+ do {
+ tables = try await adapter.listTables(schema: "public")
+ try await adapter.disconnect()
+ } catch {
+ try? await adapter.disconnect()
+ throw error
+ }
+
+ let names = tables.map(\.name)
+ XCTAssertEqual(names.count, Set(names).count, "HighGo information_schema.tables can duplicate rows; listTables must return unique physical tables")
+ XCTAssertEqual(names.filter { $0 == "app_view" }.count, 1)
+ XCTAssertTrue(names.contains("hg_t_audit_log"), "pg_class-backed listTables should include physical tables hidden by information_schema.tables")
+ }
}