Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix macOS HIG compliance: system colors, accessibility labels, theme tokens, localization
- Fix idle ping spin loop caused by exhausted AsyncStream iterator (#618)
- Skip exact row count for large tables — use database statistics estimate (#519)

## [0.28.0] - 2026-04-07

Expand Down
9 changes: 7 additions & 2 deletions TablePro/Models/Settings/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct DataGridSettings: Codable, Equatable {
var showRowNumbers: Bool
var autoShowInspector: Bool
var enableSmartValueDetection: Bool
var countRowsIfEstimateLessThan: Int

static let `default` = DataGridSettings(
rowHeight: .normal,
Expand All @@ -131,7 +132,8 @@ struct DataGridSettings: Codable, Equatable {
showAlternateRows: true,
showRowNumbers: true,
autoShowInspector: false,
enableSmartValueDetection: true
enableSmartValueDetection: true,
countRowsIfEstimateLessThan: 100_000
)

init(
Expand All @@ -142,7 +144,8 @@ struct DataGridSettings: Codable, Equatable {
showAlternateRows: Bool = true,
showRowNumbers: Bool = true,
autoShowInspector: Bool = false,
enableSmartValueDetection: Bool = true
enableSmartValueDetection: Bool = true,
countRowsIfEstimateLessThan: Int = 100_000
) {
self.rowHeight = rowHeight
self.dateFormat = dateFormat
Expand All @@ -152,6 +155,7 @@ struct DataGridSettings: Codable, Equatable {
self.showRowNumbers = showRowNumbers
self.autoShowInspector = autoShowInspector
self.enableSmartValueDetection = enableSmartValueDetection
self.countRowsIfEstimateLessThan = countRowsIfEstimateLessThan
}

init(from decoder: Decoder) throws {
Expand All @@ -165,6 +169,7 @@ struct DataGridSettings: Codable, Equatable {
showRowNumbers = try container.decodeIfPresent(Bool.self, forKey: .showRowNumbers) ?? true
autoShowInspector = try container.decodeIfPresent(Bool.self, forKey: .autoShowInspector) ?? false
enableSmartValueDetection = try container.decodeIfPresent(Bool.self, forKey: .enableSmartValueDetection) ?? true
countRowsIfEstimateLessThan = try container.decodeIfPresent(Int.self, forKey: .countRowsIfEstimateLessThan) ?? 100_000
}

// MARK: - Validated Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,23 @@ extension MainContentCoordinator {
guard let mainDriver = DatabaseManager.shared.driver(for: connectionId) else { return }

let count: Int?
let isApproximate: Bool
if isNonSQL {
count = try? await mainDriver.fetchApproximateRowCount(table: tableName)
isApproximate = true
} else {
// Skip exact COUNT(*) if the approximate count exceeds the threshold.
// PostgreSQL COUNT(*) requires a full sequential scan (MVCC) and can take
// 10-20+ seconds on multi-million-row tables. Industry standard (TablePlus,
// pgAdmin, DBeaver) is to use estimates for large tables.
let threshold = await AppSettingsManager.shared.dataGrid.countRowsIfEstimateLessThan
let approxCount = await MainActor.run {
self.tabManager.tabs.first { $0.id == tabId }?.pagination.totalRowCount
}
if let approx = approxCount, approx >= threshold {
return // Keep approximate count — skip expensive COUNT(*)
}

let quotedTable = mainDriver.quoteIdentifier(tableName)
let countResult = try? await mainDriver.execute(
query: "SELECT COUNT(*) FROM \(quotedTable)"
Expand All @@ -266,6 +280,7 @@ extension MainContentCoordinator {
} else {
count = nil
}
isApproximate = false
}

if let count {
Expand All @@ -274,7 +289,7 @@ extension MainContentCoordinator {
guard capturedGeneration == queryGeneration else { return }
if let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) {
tabManager.tabs[idx].pagination.totalRowCount = count
tabManager.tabs[idx].pagination.isApproximateRowCount = isNonSQL
tabManager.tabs[idx].pagination.isApproximateRowCount = isApproximate
}
}
}
Expand Down Expand Up @@ -346,9 +361,19 @@ extension MainContentCoordinator {
guard let mainDriver = DatabaseManager.shared.driver(for: connectionId) else { return }

let count: Int?
let isApproximate: Bool
if isNonSQL {
count = try? await mainDriver.fetchApproximateRowCount(table: tableName)
isApproximate = true
} else {
let threshold = await AppSettingsManager.shared.dataGrid.countRowsIfEstimateLessThan
let approxCount = await MainActor.run {
self.tabManager.tabs.first { $0.id == tabId }?.pagination.totalRowCount
}
if let approx = approxCount, approx >= threshold {
return
}

let quotedTable = mainDriver.quoteIdentifier(tableName)
let countResult = try? await mainDriver.execute(
query: "SELECT COUNT(*) FROM \(quotedTable)"
Expand All @@ -359,14 +384,15 @@ extension MainContentCoordinator {
} else {
count = nil
}
isApproximate = false
}

if let count {
await MainActor.run { [weak self] in
guard let self else { return }
if let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) {
tabManager.tabs[idx].pagination.totalRowCount = count
tabManager.tabs[idx].pagination.isApproximateRowCount = isNonSQL
tabManager.tabs[idx].pagination.isApproximateRowCount = isApproximate
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions TablePro/Views/Settings/DataGridSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ struct DataGridSettingsView: View {
Text("5,000 rows").tag(5_000)
Text("10,000 rows").tag(10_000)
}

Picker(String(localized: "Count rows if estimate less than:"), selection: $settings.countRowsIfEstimateLessThan) {
Text("1,000").tag(1_000)
Text("10,000").tag(10_000)
Text("100,000").tag(100_000)
Text("1,000,000").tag(1_000_000)
Text(String(localized: "Always count")).tag(Int.max)
}
.help(String(localized: "Tables with more estimated rows use approximate counts to avoid slow COUNT(*) queries"))
}
}
.formStyle(.grouped)
Expand Down
8 changes: 8 additions & 0 deletions docs/customization/settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ Options:
Larger page sizes use more memory but show more data at once.
</Note>

### Row Count Estimation

| Setting | Options | Default |
|---------|---------|---------|
| Count rows if estimate less than | 1,000 / 10,000 / 100,000 / 1,000,000 / Always count | 100,000 |

Tables above this threshold show an estimated count (`~3,000,000 rows`) from database statistics instead of running `COUNT(*)`. Useful for large PostgreSQL tables where `COUNT(*)` requires a full table scan.

<Note>
**Validation ranges:**
- **Page Size**: 10 -- 100,000 rows (values outside this range are clamped)
Expand Down
Loading