neon-postgres-egress-optimizer: Diagnose and Reduce Neon Postgres Data Egress Costs

Introduction

In Serverless Postgres billing, beyond compute and storage, network data egress often becomes conspicuous as traffic scales up: the number of rows fetched from the database per call, column width, and call frequency directly compound into questions like “Why is my Neon bill so high?”. Manually digging through pg_stat_statements and cross-referencing ORM queries in the code usually takes up most of the day. Below, we introduce neon-postgres-egress-optimizer from Neon’s official Agent Skills repository: it turns “identifying high-egress queries -> checking code anti-patterns -> modifying queries -> verifying” into a fixed workflow for coding assistants to execute step-by-step.

What is this

neon-postgres-egress-optimizer is a specialized Skill under Neon agent-skills, maintained consistently with the Neon documentation and CLI skill ecosystem (also listed on officialskills.sh).

In one sentence: diagnose and fix query patterns in the codebase that cause Postgres to transfer excessive data to the application side. Official documentation points out that most high egress costs stem from “the application retrieving more data than actually used”—such as full table scans on wide columns, unpaginated lists, high-frequency reads of static configuration, performing aggregations in the app that should be done in SQL, and JOINs causing wide parent table columns to be transmitted repeatedly across child rows.

The Skill requires first using the parent neon Skill to understand Neon overview and development practices; if not installed, you can fetch it from the docs or run:

neon skills -s neon -y

Parent Skill documentation address: https://neon.com/docs/ai/skills/neon/SKILL.md.

Core Features

The workflow in the documentation follows four sequential steps: diagnose -> analyze (code) -> fix -> verify (check if egress drops and functionality remains intact). Core capabilities can be verified in the official SKILL text, mainly including:

  1. Use pg_stat_statements to find high-egress candidates
    Check if the extension is available and CREATE EXTENSION if necessary on Neon; handle cases where statistics are cleared after compute instances scale to zero (reset stats, run representative traffic for a sufficient time window before querying). Provides multiple diagnostic SQL queries: Top queries sorted by total rows returned, average rows per call, number of calls, and total execution time.

  2. Egress priority when interpreting results
    Prioritize high row counts + wide columns (JSONB, TEXT, BYTEA, etc.); extremely high-frequency small queries also accumulate; judge column width based on the schema.

  3. Code-side checklist
    Check if only needed columns are SELECTed, if there is LIMIT/pagination, if caching is appropriate, if aggregation is happening in the app, and if JOINs are repeating parent table data.

  4. Repair guidelines for common anti-patterns (including before/after SQL examples)
    - SELECT * -> explicit column names
    - Unpaginated lists -> ORDER BY + LIMIT/OFFSET (and remind to check if the API already supports pagination parameters)
    - High-frequency static data -> add caching between app and database
    - App-side aggregation -> push down GROUP BY etc. to SQL
    - JOIN wide parent table repeated transmission -> split into two queries to fetch parent and child tables separately

Trigger scenarios (from Skill description): High database bills, unexpected data transfer costs, egress spikes, optimizing SELECT *, reducing data volume from replicas to the app, or reviewing query cost-efficiency even when egress isn’t explicitly mentioned.

Installation and Activation

In assistant environments supporting Agent Skills, you can install a single Skill from Neon’s agent-skills repository. The command given by officialskills.sh is:

npx skills add https://github.com/neondatabase/agent-skills --skill neon-postgres-egress-optimizer

You can also provide the Skill directory link to the assistant for installation:

https://github.com/neondatabase/agent-skills/tree/main/skills/neon-postgres-egress-optimizer

The Neon CLI side installs the parent neon Skill (see above neon skills -s neon -y); the specialized egress Skill follows the GitHub / npx skills add path. If various IDEs (Cursor, Codex CLI, Claude Code) already support the same Skills specification, they typically share the installation methods above; if the material does not list differences separately, refer to your current tool’s Skills documentation.

Typical Usage

After activating the Skill, simply describe the billing or egress issue in natural language; the assistant should proceed according to the official four steps. Below is an excerpt of the reproducible diagnostic section from the material (the repair section follows the same logic, selected based on Step 3 anti-patterns).

1. Verify pg_stat_statements

SELECT 1 FROM pg_stat_statements LIMIT 1;

If an error occurs, create the extension:

CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

This extension is available by default in Neon, but the above CREATE EXTENSION may still be required.

2. When statistics are empty or the instance was just woken up

  1. SELECT pg_stat_statements_reset();
  2. Let the application run under representative traffic for at least about one hour.
  3. Then execute the diagnostic query.

If you do not have permission for production database statistics, you can skip to code analysis; official documentation states that code-level patterns are often sufficient to locate the most serious issues.

3. Find queries contributing the most to egress (Example: Top 10 by total rows returned)

SELECT query, calls, rows AS total_rows, rows / calls AS avg_rows_per_call
FROM pg_stat_statements
WHERE calls > 0
ORDER BY rows DESC
LIMIT 10;

Similar queries sorted by avg_rows_per_call, calls, and total_exec_time are also provided in the official SKILL.

4. Cross-reference code and apply repair templates

For example, changing an unpaginated list to a bounded result set:

SELECT id, name, price FROM products
ORDER BY id
LIMIT 50 OFFSET 0;

Or changing an app-side full table fetch and aggregation to SQL aggregation (material example):

SELECT p.category_id,
       AVG(r.rating) AS avg_rating,
       COUNT(r.id) AS review_count
FROM reviews r
INNER JOIN products p ON r.product_id = p.id
GROUP BY p.category_id;

When a JOIN causes wide parent table columns to be repeated, the material suggests splitting into two queries, fetching values by products.id and reviews.product_id separately, to avoid transmitting wide fields like parent table JSONB multiplied by the number of child rows in a single request.

Use Cases and Considerations

Who it’s for

  • Using Neon Serverless Postgres with anomalies related to data transfer or egress in bills or monitoring.
  • Needing to trace “Neon bill spikes” to specific unpaginated interfaces, SELECT *, high-frequency config queries, etc.
  • Wanting to review queries for cost-efficiency during the code review stage, without waiting for users to mention egress verbally.

Limitations and Considerations

  • Diagnosis relies on pg_stat_statements; Neon compute scale-to-zero restarts clear statistics, requiring a reset and a sufficient observation window according to official steps.
  • Changes like pagination and caching require confirming client and API contracts; the Skill text reminds checking if consumers support paginated responses.
  • Unpaginated lists should be flagged as risks even if the table is currently small—egress per request will deteriorate linearly as scale increases.
  • Always work with the parent neon Skill to avoid modifying SQL in isolation without understanding Neon operations and best practices.

Conclusion

neon-postgres-egress-optimizer converges the cross-cutting issue of “egress costs” in Serverless databases into repeatable diagnostic SQL, interpretation rules, and repair checklists, suitable for handing over to AI assistants for step-by-step implementation in real codebases. Official Skill directory:

https://github.com/neondatabase/agent-skills/tree/main/skills/neon-postgres-egress-optimizer

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye