Files
genai-toolbox/docs/en/resources/tools/postgres/postgres-list-table-stats.md
Yuan Teoh 293c1d6889 feat!: update configuration file v2 (#2369)
This PR introduces a significant update to the Toolbox configuration
file format, which is one of the primary **breaking changes** required
for the implementation of the Advanced Control Plane.

# Summary of Changes
The configuration schema has been updated to enforce resource isolation
and facilitate atomic, incremental updates.
* Resource Isolation: Resource definitions are now separated into
individual blocks, using a distinct structure for each resource type
(Source, Tool, Toolset, etc.). This improves readability, management,
and auditing of configuration files.
* Field Name Modification: Internal field names have been modified to
align with declarative methodologies. Specifically, the configuration
now separates kind (general resource type, e.g., Source) from type
(specific implementation, e.g., Postgres).

# User Impact
Existing tools.yaml configuration files are now in an outdated format.
Users must eventually update their files to the new YAML format.

# Mitigation & Compatibility
Backward compatibility is maintained during this transition to ensure no
immediate user action is required for existing files.
* Immediate Backward Compatibility: The source code includes a
pre-processing layer that automatically detects outdated configuration
files (v1 format) and converts them to the new v2 format under the hood.
* [COMING SOON] Migration Support: The new toolbox migrate subcommand
will be introduced to allow users to automatically convert their old
configuration files to the latest format.

# Example
Example for config file v2:
```
kind: sources
name: my-pg-instance
type: cloud-sql-postgres
project: my-project
region: my-region
instance: my-instance
database: my_db
user: my_user
password: my_pass
---
kind: authServices
name: my-google-auth
type: google
clientId: testing-id
---
kind: tools
name: example_tool
type: postgres-sql
source: my-pg-instance
description: some description
statement: SELECT * FROM SQL_STATEMENT;
parameters:
- name: country
  type: string
  description: some description
---
kind: tools
name: example_tool_2
type: postgres-sql
source: my-pg-instance
description: returning the number one
statement: SELECT 1;
---
kind: toolsets
name: example_toolset
tools:
- example_tool
```

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Averi Kitsch <akitsch@google.com>
2026-01-27 16:58:43 -08:00

6.8 KiB

title, type, weight, description, aliases
title type weight description aliases
postgres-list-table-stats docs 1 The "postgres-list-table-stats" tool reports table statistics including size, scan metrics, and bloat indicators for PostgreSQL tables.
/resources/tools/postgres-list-table-stats

About

The postgres-list-table-stats tool queries pg_stat_all_tables to provide comprehensive statistics about tables in the database. It calculates useful metrics like index scan ratio and dead row ratio to help identify performance issues and table bloat.

Compatible sources:

The tool returns a JSON array where each element represents statistics for a table, including scan metrics, row counts, and vacuum history. Results are sorted by sequential scans by default and limited to 50 rows.

Example

kind: tools
name: list_table_stats
type: postgres-list-table-stats
source: postgres-source
description: "Lists table statistics including size, scans, and bloat metrics."

Example Requests

List default tables in public schema:

{}

Filter by specific table name:

{
  "table_name": "users"
}

Filter by owner and sort by size:

{
  "owner": "app_user",
  "sort_by": "size",
  "limit": 10
}

Find tables with high dead row ratio:

{
  "sort_by": "dead_rows",
  "limit": 20
}

Example Response

[
  {
    "schema_name": "public",
    "table_name": "users",
    "owner": "postgres",
    "total_size_bytes": 8388608,
    "seq_scan": 150,
    "idx_scan": 450,
    "idx_scan_ratio_percent": 75.0,
    "live_rows": 50000,
    "dead_rows": 1200,
    "dead_row_ratio_percent": 2.34,
    "n_tup_ins": 52000,
    "n_tup_upd": 12500,
    "n_tup_del": 800,
    "last_vacuum": "2025-11-27T10:30:00Z",
    "last_autovacuum": "2025-11-27T09:15:00Z",
    "last_autoanalyze": "2025-11-27T09:16:00Z"
  },
  {
    "schema_name": "public",
    "table_name": "orders",
    "owner": "postgres",
    "total_size_bytes": 16777216,
    "seq_scan": 50,
    "idx_scan": 1200,
    "idx_scan_ratio_percent": 96.0,
    "live_rows": 100000,
    "dead_rows": 5000,
    "dead_row_ratio_percent": 4.76,
    "n_tup_ins": 120000,
    "n_tup_upd": 45000,
    "n_tup_del": 15000,
    "last_vacuum": "2025-11-26T14:22:00Z",
    "last_autovacuum": "2025-11-27T02:30:00Z",
    "last_autoanalyze": "2025-11-27T02:31:00Z"
  }
]

Parameters

parameter type required default description
schema_name string false "public" Optional: A specific schema name to filter by (supports partial matching)
table_name string false null Optional: A specific table name to filter by (supports partial matching)
owner string false null Optional: A specific owner to filter by (supports partial matching)
sort_by string false null Optional: The column to sort by. Valid values: size, dead_rows, seq_scan, idx_scan (defaults to seq_scan)
limit integer false 50 Optional: The maximum number of results to return

Output Fields Reference

field type description
schema_name string Name of the schema containing the table.
table_name string Name of the table.
owner string PostgreSQL user who owns the table.
total_size_bytes integer Total size of the table including all indexes in bytes.
seq_scan integer Number of sequential (full table) scans performed on this table.
idx_scan integer Number of index scans performed on this table.
idx_scan_ratio_percent decimal Percentage of total scans (seq_scan + idx_scan) that used an index. A low ratio may indicate missing or ineffective indexes.
live_rows integer Number of live (non-deleted) rows in the table.
dead_rows integer Number of dead (deleted but not yet vacuumed) rows in the table.
dead_row_ratio_percent decimal Percentage of dead rows relative to total rows. High values indicate potential table bloat.
n_tup_ins integer Total number of rows inserted into this table.
n_tup_upd integer Total number of rows updated in this table.
n_tup_del integer Total number of rows deleted from this table.
last_vacuum timestamp Timestamp of the last manual VACUUM operation on this table (null if never manually vacuumed).
last_autovacuum timestamp Timestamp of the last automatic vacuum operation on this table.
last_autoanalyze timestamp Timestamp of the last automatic analyze operation on this table.

Interpretation Guide

Index Scan Ratio (idx_scan_ratio_percent)

  • High ratio (> 80%): Table queries are efficiently using indexes. This is typically desirable.
  • Low ratio (< 20%): Many sequential scans indicate missing indexes or queries that cannot use existing indexes effectively. Consider adding indexes to frequently searched columns.
  • 0%: No index scans performed; all queries performed sequential scans. May warrant index investigation.

Dead Row Ratio (dead_row_ratio_percent)

  • < 2%: Healthy table with minimal bloat.
  • 2-5%: Moderate bloat; consider running VACUUM if not recent.
  • > 5%: High bloat; may benefit from manual VACUUM or VACUUM FULL.

Vacuum History

  • Null last_vacuum: Table has never been manually vacuumed; relies on autovacuum.
  • Recent last_autovacuum: Autovacuum is actively managing the table.
  • Stale timestamps: Consider running manual VACUUM and ANALYZE if maintenance windows exist.

Performance Considerations

  • Statistics are collected from pg_stat_all_tables, which resets on PostgreSQL restart.
  • Run ANALYZE on tables to update statistics for accurate query planning.
  • The tool defaults to limiting results to 50 rows; adjust the limit parameter for larger result sets.
  • Filtering by schema, table name, or owner uses LIKE pattern matching (supports partial matches).

Use Cases

  • Finding ineffective indexes: Identify tables with low idx_scan_ratio_percent to evaluate index strategy.
  • Detecting table bloat: Sort by dead_rows to find tables needing VACUUM.
  • Monitoring growth: Track total_size_bytes over time for capacity planning.
  • Audit maintenance: Check last_autovacuum and last_autoanalyze timestamps to ensure maintenance tasks are running.
  • Understanding workload: Examine seq_scan vs idx_scan ratios to understand query patterns.