检查数据库
本指南说明了如何使用 CLI、MCP 或 Explorer 查看实时数据库统计信息。
🌐 This guide explains how to read live database statistics using the CLI, MCP, or Explorer.
读取数据库统计信息自:
🌐 Read database statistics from:
- 工作室:Explorer 使用查询来源 Database
- MCP:
execute_sql - 命令行接口:
supabase inspect db
使用此页面来:
🌐 Use this page to:
要从这些检查中获取信号,请参见检测。至于其他来源,请参见可观测性。
🌐 To pick up a signal from these checks, see Detecting. For the other sources, see Observability.
使用命令行 #
🌐 Using the CLI
Supabase CLI 可以从 Postgres 内部机制 中读取实时统计数据。大多数命令可以在任何 Postgres 数据库上使用,而不仅仅是 Supabase 项目。
🌐 The Supabase CLI reads live statistics from Postgres internals. Most commands work on any Postgres database, not only a Supabase project.
inspect db#
🌐 The inspect db command
你可以通过 inspect db 命令访问你的 Postgres 数据库的检查工具。你可以运行 supabase inspect db help 来获取所有可用命令的完整列表。
🌐 The inspection tools for your Postgres database are under the inspect db command. You can get a full list of available commands by running supabase inspect db help.
$ supabase inspect db helpTools to inspect your Supabase databaseUsage: supabase inspect db [command]Available Commands: bloat Estimates space allocated to a relation that is full of dead tuples blocking Show queries that are holding locks and the queries that are waiting for them to be released cache-hit Show cache hit rates for tables and indices...连接到任何 Postgres 数据库 #
🌐 Connect to any Postgres database
大多数检查命令对 Postgres 是通用的。即使不是 Supabase 项目,你也可以通过 --db-url 提供连接字符串,在任何 Postgres 数据库上运行检查程序。
🌐 Most inspection commands are Postgres agnostic. You can run inspection routines on any Postgres database even if it is not a Supabase project by providing a connection string via --db-url.
例如,你可以连接到你本地的 Postgres 实例:
🌐 For example you can connect to your local Postgres instance:
supabase inspect db bloat --db-url postgresql://postgres:postgres@localhost:5432/postgres连接到 Supabase 实例 #
🌐 Connect to a Supabase instance
使用 Supabase 时,你可以将 Supabase CLI 与你的项目关联起来:
🌐 Working with Supabase, you can link the Supabase CLI with your project:
supabase link --project-ref <project-id>然后每当你在项目文件夹里时,CLI 会自动连接到你的 Supabase 项目,你就不需要再提供 --db-url 了。
🌐 Then the CLI will automatically connect to your Supabase project whenever you are in the project folder and you no longer need to provide --db-url.
检查命令 #
🌐 Inspection commands
下面是提供的 db 检查命令,按不同的使用场景分组。
🌐 Below are the db inspection commands provided, grouped by different use cases.
有些命令可能需要启用 pg_stat_statements 或使用特定版本的 Postgres。
🌐 Some commands might require pg_stat_statements to be enabled or a specific Postgres version to be used.
磁盘存储 #
🌐 Disk storage
如果你的磁盘空间快不够用了,这些命令很有用:
🌐 These commands are handy if you are running low on disk storage:
- bloat - 估计浪费空间的数量
- vacuum-stats - 提供垃圾收集的相关信息
- table-record-counts - 估计每个表的记录数
- table-sizes - 显示表格的大小
- index-sizes - 显示各个索引的大小
- table-index-sizes - 显示每个表的索引大小
查询性能 #
🌐 Query performance
下面的命令在你的 Postgres 数据库占用大量资源(如 CPU、内存或磁盘 IO)时很有用。你也可以用它们来调查慢查询。
🌐 The commands below are useful if your Postgres database consumes a lot of resources like CPU, RAM or Disk IO. You can also use them to investigate slow queries.
- 缓存命中 - 显示你的缓存使用效率整体如何
- unused-indexes - 显示低索引扫描的索引
- index-usage - 显示关于索引效率的信息
- seq-scans - 显示所有表记录的顺序扫描次数
- long-running-queries - 显示当前正在执行的长时间运行查询
- outliers - 显示执行时间长但调用次数少的查询,以及在同步 I/O 上花费执行时间比例高的查询
锁 #
🌐 Locks
连接 #
🌐 Connections
- role-connections - 显示所有数据库角色的活跃连接数(Supabase 专用命令)
- replication-slots - 显示数据库上复制槽的信息
使用 SQL #
🌐 Using SQL
打开 资源管理器,选择 运行 SQL,然后选择 数据库 作为查询来源。你也可以通过 MCP execute_sql 运行只读诊断。
🌐 Open Explorer, select Run SQL, and choose Database as the query source. You can also run read-only diagnostics through MCP execute_sql.
使用 性能检查 查看活跃会话、阻塞、耗资源的语句和缓存命中率。使用 容量检查 查看关系大小和连接数。
🌐 Use Performance checks for active sessions, blockers, expensive statements, and cache hit rates. Use Capacity checks for relation sizes and connection counts.
pg_stat_activity 是一个实时快照。pg_stat_statements 和缓存计数器自上次重置以来是累积的;它们并不描述任意的历史时间窗口。在测量变化时,请将保存的快照与相同的重置间隔进行比较。查看 pg_stat_statements 指南 以了解扩展需求。
当检查识别出一个语句时,查看它的查询计划。长时间运行的会话或累计查询时间过长只是需要调查的证据,而不是取消查询或重置统计信息的理由。
🌐 When a check identifies a statement, inspect its query plan. A long-running session or high cumulative query time is evidence to investigate, not a reason by itself to cancel a query or reset statistics.