Skip to content

feat(sidebar): display database size in sidebar tree#2409

Closed
junbujianwpl wants to merge 1 commit into
t8y2:mainfrom
junbujianwpl:feat/sidebar-database-size
Closed

feat(sidebar): display database size in sidebar tree#2409
junbujianwpl wants to merge 1 commit into
t8y2:mainfrom
junbujianwpl:feat/sidebar-database-size

Conversation

@junbujianwpl

Copy link
Copy Markdown

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 传入 sizeBytesTreeItem.vue 将其格式化为 B/KB/MB/GB 显示在数据库名称右侧。

变更类型

  • 新功能
  • Bug 修复
  • 性能优化
  • 代码重构
  • 文档更新
  • CI / 构建

涉及前端

  • 本 PR 涉及前端改动,已附截图/录屏(见下方)

验证

  • pnpm typecheck 通过
  • pnpm tauri build 编译成功(release exe 产出)
  • 连接 PostgreSQL 后侧边栏数据库名旁显示 KB/MB/GB 大小
  • 连接 MySQL/SQLite 等不查询 size 的 driver 时不显示额外内容
dbsize

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #2409: feat(sidebar): display database size in sidebar tree

+42/-17,15 个文件 | CI: 只有 notify 通过 | 可合并性: 冲突 / dirty

🔴 必须修复

  1. apps/desktop/src/components/sidebar/TreeItem.vuemain 有真实 merge conflict。

GitHub 当前报告 mergeable=CONFLICTING。冲突在 sidebar trailing metadata 区域:main 给 comment span 新增了 Windows 字体 class,这个 PR 在同一区域新增了 database size span。需要 rebase/merge main,并同时保留现有注释样式改动和新增 size 显示。

  1. 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 初始加载。

@junbujianwpl
junbujianwpl force-pushed the feat/sidebar-database-size branch 3 times, most recently from 61bcf45 to 88b492c Compare July 6, 2026 01:29
@junbujianwpl
junbujianwpl requested a review from t8y2 July 6, 2026 01:29
@junbujianwpl
junbujianwpl force-pushed the feat/sidebar-database-size branch from 88b492c to aad6c2d Compare July 6, 2026 05:37
@zhuhaoh

zhuhaoh commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

这个很实用的, 我看datagrip上没有,但是dbeaver上是有的。 能直观看到表大小

@t8y2

t8y2 commented Jul 6, 2026

Copy link
Copy Markdown
Owner

这个晚些会review~需要斟酌一下,感谢pr

@junbujianwpl
junbujianwpl force-pushed the feat/sidebar-database-size branch 2 times, most recently from 6f75c3c to 3df8697 Compare July 10, 2026 03:08

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request changes: PostgreSQL 的权限保护已经修好,冲突也已解决,但目前还有两个问题需要处理。

  1. crates/dbx-core/src/db/mysql.rs:1470 将缺失的统计值 COALESCE 为 0。MySQL 的 INFORMATION_SCHEMA.TABLES 只返回用户有对象权限的行,因此用户能看到数据库但没有表元数据权限时,会错误显示为 0 B;只有部分表权限时也可能显示不完整的合计。这里至少应保留 NULL,并避免把部分可见对象的合计当作完整数据库大小,同时补低权限测试。

  2. 大小统计仍同步放在基础 list_databases() 路径中。PostgreSQL 会逐库调用 pg_database_size(),MySQL 会聚合所有可见库的 INFORMATION_SCHEMA.TABLES,侧边栏首次加载和刷新都必须等待统计完成。建议参考 DBeaver,将 size 拆成独立、可失败、可缓存的 statistics 加载:先返回数据库列表,再异步填充大小。

@junbujianwpl
junbujianwpl force-pushed the feat/sidebar-database-size branch from 3df8697 to 5306636 Compare July 14, 2026 06:44
@junbujianwpl

Copy link
Copy Markdown
Author

新改动总结

侧栏数据库大小改为 自动 + 手动 两套:

  • 自动:连接后后台拉全库大小;超时/失败则留空,不挡列表
  • 手动:数据库节点右键「获取数据库大小」,只查当前库,超时 60s

改动

  • MySQL / Postgres 新增单库 database_size API(Tauri + Web)
  • 前端右键菜单 + fetchDatabaseSize;自动结果为 null 时不覆盖手动已取到的大小
  • 权限不足仍返回 None,不误报 0 B
  • 补 Rust / 前端单测;i18n 7 语种

旧库 / 慢库失败 case(预期行为,请确认)

Case 场景 行为
慢库超时 MySQL 5.7 / MariaDB,information_schema 很慢 自动常超时留空;右键单库多数能出,极大库仍可能 60s 超时
提示无效 服务器忽略 MAX_EXECUTION_TIME 靠客户端超时释放连接,不应拖死打开表
无统计权限 能看见库但无 schema 统计权限 / 仅部分表权限 不显示大小,不显示残缺或 0 B
PG 无 CONNECT 无目标库 CONNECT 权限 该库无大小,列表不整批失败
手动后自动刷新 手动取到大小后,自动又超时返回 null 不覆盖手动值
dbx

@junbujianwpl
junbujianwpl requested a review from t8y2 July 14, 2026 07:00

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes: database-size reporting still has incomplete/unsupported Agent paths.

  1. JDBC/Agent MySQL executes the information_schema.TABLES aggregate 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.

  2. 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.
@junbujianwpl
junbujianwpl force-pushed the feat/sidebar-database-size branch from 5306636 to 11eb62c Compare July 14, 2026 12:23
@junbujianwpl
junbujianwpl requested a review from t8y2 July 14, 2026 12:25

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Several earlier issues are fixed, but the current head still has blocking regressions and unsupported behavior.

  1. apps/desktop/src/stores/connectionStore.ts uses window.setTimeout in the metadata refresh path. This breaks the existing Node/Vitest connection-store tests with ReferenceError: window is not defined (three regression failures). Please use an environment-independent timer API.

  2. Generic JDBC connections inferred as MySQL are advertised as supporting database-size loading, but the backend creates them as ExternalDriver and 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.

  3. The MySQL completeness check only inspects direct USER_PRIVILEGES and SCHEMA_PRIVILEGES rows. MySQL 8 users receiving SELECT through an enabled role can see complete information_schema.TABLES data, but this check reports no permission and hides the size. Please account for effective role privileges and add coverage for role-based grants.

  4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants