Skip to content

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

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

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 2 times, most recently from 4294387 to 61bcf45 Compare July 3, 2026 03:24
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 61bcf45 to 88b492c Compare July 6, 2026 01:29
@junbujianwpl junbujianwpl requested a review from t8y2 July 6, 2026 01:29
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.

2 participants