Schema row counts

From CarlosCaPe/octorato · View interactive version

Schema row counts is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Get exact row counts per table in a given schema — PostgreSQL primary, with notes for other engines. Two-column result: table_name, row_count.

How to use this skill

Skill file

schema-row-counts

Returns a two-column table (table_name, row_count) for every BASE TABLE in a schema.

PostgreSQL — exact count (any schema)

SELECT
    table_name,
    (xpath('/row/cnt/text()',
           query_to_xml(format('SELECT COUNT(*) AS cnt FROM %I.%I', table_schema, table_name),
                        false, true, ''))
    )[1]::text::int AS row_count
FROM information_schema.tables
WHERE table_schema = '<schema>'        -- replace with target schema
  AND table_type   = 'BASE TABLE'
ORDER BY row_count DESC, table_name;

Replace <schema> with the target schema name (e.g., payer, eligibility, public).

PostgreSQL — fast approximate (uses pg_stat, no full scan)

Use when tables are large and exact counts are not required (e.g., dashboards, health checks):

SELECT
    relname          AS table_name,
    n_live_tup       AS row_count
FROM pg_stat_user_tables
WHERE schemaname = '<schema>'
ORDER BY n_live_tup DESC, relname;

Stats lag behind until ANALYZE runs. Good for ballpark; not for audits.

How to run (local Docker setup — newum_db pattern)

# One-liner: replace payer with target schema
docker compose exec postgres psql -U $env:NEWUM_LOCAL_USER -d newum -c `
  "SELECT table_name, (xpath('/row/cnt/text()', query_to_xml(format('SELECT COUNT(*) AS cnt FROM payer.%I', table_name), false, true, '')))[1]::text::int AS row_count FROM information_schema.tables WHERE table_schema = 'payer' AND table_type = 'BASE TABLE' ORDER BY row_count DESC, table_name;"

How to run (via QueryMaster)

qm -e postgresql -c <conn> "SELECT table_name, ... WHERE table_schema = '<schema>' ..." --execute

MS-SQL equivalent

SELECT
    t.name            AS table_name,
    p.rows            AS row_count
FROM sys.tables t
JOIN sys.partitions p ON p.object_id = t.object_id
                     AND p.index_id IN (0, 1)
WHERE SCHEMA_NAME(t.schema_id) = '<schema>'
ORDER BY p.rows DESC, t.name;

Databricks / Spark SQL equivalent

SHOW TABLES IN <schema>;
-- then per table:
SELECT COUNT(*) FROM <schema>.<table>;
-- or loop via notebook:
-- %python
-- for t in spark.catalog.listTables("<schema>"):
--     print(t.name, spark.table(f"<schema>.{t.name}").count())

Lessons Learned

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.