feat(sidebar): display database size in sidebar tree#2409
Conversation
t8y2
left a comment
There was a problem hiding this comment.
PR #2409: feat(sidebar): display database size in sidebar tree
+42/-17,15 个文件 | CI: 只有 notify 通过 | 可合并性: 冲突 / dirty
🔴 必须修复
apps/desktop/src/components/sidebar/TreeItem.vue和main有真实 merge conflict。
GitHub 当前报告 mergeable=CONFLICTING。冲突在 sidebar trailing metadata 区域:main 给 comment span 新增了 Windows 字体 class,这个 PR 在同一区域新增了 database size span。需要 rebase/merge main,并同时保留现有注释样式改动和新增 size 显示。
crates/dbx-core/src/db/postgres.rs不应在list_databases()主路径里无保护地批量调用pg_database_size()。
PostgreSQL 官方文档说明 pg_database_size() 需要目标数据库的 CONNECT 权限,或者 pg_read_all_stats 权限。当前查询对所有 datallowconn=true 数据库直接调用 pg_database_size(datname),低权限账号只要遇到一个无权限数据库,就可能让整个数据库列表查询失败,导致 sidebar 无法列库。
另外,这也把“计算数据库磁盘占用”放进了连接后基础元数据加载路径。数据库数量多、库很大、远程连接慢或磁盘较忙时,sidebar 初始加载会被 size 统计拖慢。
建议先保持 list_databases() 只返回基础列表,然后把 size 做成异步/可失败/可缓存的统计加载;如果这个 PR 必须同步返回 size,也至少用权限保护,例如 CASE WHEN has_database_privilege(datname, 'CONNECT') THEN pg_database_size(oid) ELSE NULL END,保证无权限数据库不会破坏列表加载。
DBeaver 的 PostgreSQL 实现也是把这两件事拆开的:数据库对象基础信息只读 pg_database;size 通过 DBPObjectStatistics / collectObjectStatistics() 这类独立 statistics 流程加载,并受统计开关、缓存和 server capability 控制,而不是放在基础 list databases 查询里。
🟡 改进建议
补一个小的前端回归测试,确认 buildDatabaseTreeNodes() 会把后端 size_bytes 传成 tree node 的 sizeBytes。如果保留 PostgreSQL size 查询,也建议补一个无权限数据库返回 NULL size、但列表仍能加载的查询层测试。
✅ 做得好的地方
新增字段是 optional,并且序列化时会跳过空值,非 PostgreSQL driver 基本能保持现有行为。前端没有 size 时也会隐藏显示,不会给其他数据库类型增加额外文案。
结论
Request changes。当前有 merge conflict 不能合并;同时 pg_database_size() 的权限和性能风险需要先处理,避免影响 PostgreSQL 低权限账号和 sidebar 初始加载。
61bcf45 to
88b492c
Compare
88b492c to
aad6c2d
Compare
|
这个很实用的, 我看datagrip上没有,但是dbeaver上是有的。 能直观看到表大小 |
|
这个晚些会review~需要斟酌一下,感谢pr |
6f75c3c to
3df8697
Compare
t8y2
left a comment
There was a problem hiding this comment.
request changes: PostgreSQL 的权限保护已经修好,冲突也已解决,但目前还有两个问题需要处理。
-
crates/dbx-core/src/db/mysql.rs:1470将缺失的统计值COALESCE为 0。MySQL 的INFORMATION_SCHEMA.TABLES只返回用户有对象权限的行,因此用户能看到数据库但没有表元数据权限时,会错误显示为0 B;只有部分表权限时也可能显示不完整的合计。这里至少应保留NULL,并避免把部分可见对象的合计当作完整数据库大小,同时补低权限测试。 -
大小统计仍同步放在基础
list_databases()路径中。PostgreSQL 会逐库调用pg_database_size(),MySQL 会聚合所有可见库的INFORMATION_SCHEMA.TABLES,侧边栏首次加载和刷新都必须等待统计完成。建议参考 DBeaver,将 size 拆成独立、可失败、可缓存的 statistics 加载:先返回数据库列表,再异步填充大小。
3df8697 to
5306636
Compare
t8y2
left a comment
There was a problem hiding this comment.
Request changes: database-size reporting still has incomplete/unsupported Agent paths.
-
JDBC/Agent MySQL executes the
information_schema.TABLESaggregate directly and accepts the result as complete. With partial table privileges, MySQL returns only the visible tables, so the UI displays a smaller value as the full database size. The native MySQL completeness/permission protection is not applied to this path. -
The UI advertises size support for HighGo, Vastbase, and Kingbase, but these Agent-backed types are not dispatched by the backend size implementation. The menu is shown even though the request cannot return a size.
The current head also conflicts with main in the sidebar files. Please make support gating match the backend implementation and apply equivalent completeness protection to Agent MySQL before resolving the conflicts.
Add size_bytes field to DatabaseInfo struct and query pg_database_size() for PostgreSQL connections. The sidebar now shows human-readable size (B/KB/MB/GB) next to each database name, helping users quickly assess storage usage without running manual queries.
5306636 to
11eb62c
Compare
t8y2
left a comment
There was a problem hiding this comment.
Thanks for the update. Several earlier issues are fixed, but the current head still has blocking regressions and unsupported behavior.
-
apps/desktop/src/stores/connectionStore.tsuseswindow.setTimeoutin the metadata refresh path. This breaks the existing Node/Vitest connection-store tests withReferenceError: window is not defined(three regression failures). Please use an environment-independent timer API. -
Generic JDBC connections inferred as MySQL are advertised as supporting database-size loading, but the backend creates them as
ExternalDriverand the database-size dispatch does not handle that driver. The automatic request and context-menu action therefore cannot return a size for JDBC MySQL. Please align the frontend capability gate with the backend implementation or add the missing backend support. -
The MySQL completeness check only inspects direct
USER_PRIVILEGESandSCHEMA_PRIVILEGESrows. MySQL 8 users receivingSELECTthrough an enabled role can see completeinformation_schema.TABLESdata, but this check reports no permission and hides the size. Please account for effective role privileges and add coverage for role-based grants. -
Automatic statistics loading still queries every database before the frontend applies
visible_databases. Connections configured to show only a small subset therefore scan hidden databases as well. Please pass or apply the visible database scope before running the statistics queries.

Add size_bytes field to DatabaseInfo struct and query pg_database_size() for PostgreSQL connections. The sidebar now shows human-readable size (B/KB/MB/GB) next to each database name, helping users quickly assess storage usage without running manual queries.
变更说明
在侧边栏为每个数据库节点显示其占用磁盘空间大小。
后端为
DatabaseInfo新增size_bytes: Option<i64>字段,PostgreSQL driver 查询pg_database_size(datname)填充;其余 driver 默认None。前端
TreeNode传入sizeBytes,TreeItem.vue将其格式化为 B/KB/MB/GB 显示在数据库名称右侧。变更类型
涉及前端
验证
pnpm typecheck通过pnpm tauri build编译成功(release exe 产出)