Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Guide the user through diagnosing and fixing application-side query patterns that cause excessive data transfer (egress) from their Postgres database. Most high egress bills come from the application fetching more data than it uses.
.claude/skills/neon-postgres-egress-optimizer/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-07 | ✗→✓ | ▲ Improved | 86% | 0% |
| case-17 | ✗→✓ | ▲ Improved | 65% | 0% |
| case-18 | ✗→✓ | ▲ Improved | 14% | 0% |
| case-19 | ✗→✓ | ▲ Improved | 120% | 0% |
| case-20 | ✗→✓ | ▲ Improved | 47% | 0% |
FIRST: Use the parent neon skill for a Neon overview, getting started with Neon, Neon development best practices, and more.
If the neon skill is not installed, fetch it from https://neon.com/docs/ai/skills/neon/SKILL.md or install it with:
bashnpx skills add neondatabase/agent-skills --skill neon
Guide the user through diagnosing and fixing application-side query patterns that cause excessive data transfer (egress) from their Postgres database. Most high egress bills come from the application fetching more data than it uses.
Work the four steps in order: diagnose which queries transfer the most data, analyze the codebase behind them, fix the anti-patterns, then verify nothing broke and the transfer actually dropped.
Identify which queries transfer the most data. The primary tool is the pg_stat_statements extension.
sqlSELECT 1 FROM pg_stat_statements LIMIT 1;
If this errors, the extension needs to be created:
sqlCREATE EXTENSION IF NOT EXISTS pg_stat_statements;
On Neon the extension is available by default, but it may still need this CREATE EXTENSION step.
Stats are cleared when a Neon compute scales to zero and restarts. If the stats are empty or the compute recently woke up:
SELECT pg_stat_statements_reset();If the user has stats from a production database, use those. If they have no access to production stats, proceed to Step 2 and analyze the codebase directly — code-level patterns are often sufficient to identify the worst offenders.
Run these to identify the top egress contributors. Focus on queries that return many rows, return wide rows (JSONB, TEXT, BYTEA columns), or are called very frequently.
Queries returning the most total rows:
sqlSELECT 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;
Queries returning the most rows per execution (poorly scoped SELECTs, missing pagination):
sqlSELECT query, calls, rows AS total_rows, rows / calls AS avg_rows_per_call FROM pg_stat_statements WHERE calls > 0 ORDER BY avg_rows_per_call DESC LIMIT 10;
Most frequently called queries (candidates for caching):
sqlSELECT query, calls, rows AS total_rows, rows / calls AS avg_rows_per_call FROM pg_stat_statements WHERE calls > 0 ORDER BY calls DESC LIMIT 10;
Longest running queries (not a direct egress measure, but helps identify problem queries during a spike):
sqlSELECT query, calls, rows AS total_rows, round(total_exec_time::numeric, 2) AS total_exec_time_ms FROM pg_stat_statements WHERE calls > 0 ORDER BY total_exec_time DESC LIMIT 10;
Rank findings by estimated egress impact:
For each query identified in Step 1, or for each database query in the codebase if no stats are available, check:
Apply the appropriate fix for each problem found. Below are the most common egress anti-patterns and how to fix them.
Problem: The query fetches all columns but the application only uses a few. Large columns (JSONB blobs, TEXT fields) get transferred over the wire and discarded.
Fix: Name only the columns the response needs.
Before:
sqlSELECT * FROM products;
After:
sqlSELECT id, name, price, image_urls FROM products;
Problem: A list endpoint returns all rows with no LIMIT. This is an unbounded egress risk — every new row in the table increases data transfer on every request. Flag this regardless of current table size.
This is easy to miss because the application may work fine with small datasets. But at scale, an unpaginated endpoint returning 10,000 rows with even moderate column widths can transfer hundreds of megabytes per day.
Fix: Bound the result set with ORDER BY plus LIMIT/OFFSET.
Before:
sqlSELECT id, name, price FROM products;
After:
sqlSELECT id, name, price FROM products ORDER BY id LIMIT 50 OFFSET 0;
When adding pagination, check whether the consuming client already supports paginated responses. If not, pick sensible defaults and document the pagination parameters in the API.
Problem: A query is called thousands of times per day but returns data that rarely changes. Every call transfers the same rows from the database. This pattern is only visible from pg_stat_statements — the code itself looks normal.
Look for queries with extremely high call counts relative to other queries. Common examples: configuration tables, category lists, feature flags, user role definitions.
Fix: Add a caching layer between the application and the database so it avoids hitting the database on every request.
Problem: The application fetches all rows from a table and then computes aggregates (averages, counts, sums, groupings) in application code. The full dataset transfers over the wire even though the result is a small summary.
Fix: Push the aggregation into SQL.
Before: The application fetches entire tables and aggregates in code with loops or .reduce().
After:
sqlSELECT 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;
Problem: A JOIN between a wide parent table and a child table duplicates all parent columns across every child row. If a product has 200 reviews and the product row includes a 50KB JSONB column, the join sends that 50KB × 200 = ~10MB for a single request.
This is distinct from the SELECT \ problem. Even if you select only needed columns, a JOIN still repeats the parent data for every child row. The fix is structural: avoid the join entirely.
Fix: Split the join into two queries, one per table.
Before:
sqlSELECT * FROM products LEFT JOIN reviews ON reviews.product_id = products.id WHERE products.id = 1;
After (two separate queries):
sqlSELECT id, name, price, description, image_urls FROM products WHERE id = 1; SELECT id, user_name, rating, body FROM reviews WHERE product_id = 1;
Two queries instead of one JOIN. The product data is fetched once. The reviews are fetched once. No duplication.
After applying fixes:
SELECT pg_stat_statements_reset();), let traffic run, then re-run the diagnostic queries to compare before and after.neon.ts)The fixes above cut egress (data transferred out of Postgres). The other big non-prod cost lever is compute, and you can codify it durably in neon.ts — Neon's infrastructure-as-code file (see the neon skill for the full reference) — so dev, preview, and CI branches stay cheap by default instead of relying on per-branch flags:
bashnpm i @neon/config
typescript// neon.ts import { defineConfig } from "@neon/config/v1"; export default defineConfig({ branch: (branch) => { if (branch.exists || branch.isDefault) return {}; // don't touch prod return { ttl: "7d", // ephemeral branches auto-expire instead of accruing storage postgres: { computeSettings: { autoscalingLimitMinCu: 0.25, // scale to zero when idle autoscalingLimitMaxCu: 1, // cap autoscaling on throwaway branches suspendTimeout: "5m", }, }, }; }, });
bashneon config apply # apply to the current branch (neon deploy is an alias)
This is complementary, not a substitute: query-pattern fixes are what actually reduce egress charges, while these settings keep non-production compute and storage from quietly inflating the same bill. Because neon checkout applies the policy when it creates a branch, new dev/preview branches inherit the cheap profile automatically.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 18,220 | 10,763 | -41% | 1 | 1 | 0% | 3,179 | 4,512 | +42% | 0 | 0 | — |
case-02 | fail→fail | 19,545 | 11,281 | -42% | 1 | 1 | 0% | 3,536 | 4,485 | +27% | 0 | 0 | — |
case-03 | fail→fail | 18,666 | 10,990 | -41% | 1 | 1 | 0% | 3,390 | 4,420 | +30% | 0 | 0 | — |
case-04 | pass→pass | 17,118 | 12,202 | -29% | 1 | 1 | 0% | 3,039 | 4,457 | +47% | 0 | 0 | — |
case-05 | pass→pass | 17,805 | 17,260 | -3% | 1 | 1 | 0% | 3,135 | 5,412 | +73% | 0 | 0 | — |
case-06 | pass→pass | 15,165 | 8,878 | -41% | 1 | 1 | 0% | 2,620 | 3,880 | +48% | 0 | 0 | — |
case-07 | fail→pass | 7,945 | 1,981 | -75% | 1 | 1 | 0% | 1,415 | 2,629 | +86% | 0 | 0 | — |
case-08 | fail→fail | 12,416 | 6,043 | -51% | 1 | 1 | 0% | 2,035 | 3,454 | +70% | 0 | 0 | — |
case-09 | pass→pass | 11,705 | 6,100 | -48% | 1 | 1 | 0% | 2,272 | 3,410 | +50% | 0 | 0 | — |
case-10 | pass→pass | 8,594 | 2,015 | -77% | 1 | 1 | 0% | 1,607 | 2,681 | +67% | 0 | 0 | — |
case-11 | pass→pass | 14,201 | 8,426 | -41% | 1 | 1 | 0% | 2,702 | 3,913 | +45% | 0 | 0 | — |
case-12 | pass→pass | 8,088 | 7,013 | -13% | 1 | 1 | 0% | 1,475 | 3,596 | +144% | 0 | 0 | — |
case-13 | pass→pass | 11,039 | 5,853 | -47% | 1 | 1 | 0% | 1,676 | 3,343 | +99% | 0 | 0 | — |
case-14 | pass→pass | 13,555 | 7,389 | -45% | 1 | 1 | 0% | 2,299 | 3,679 | +60% | 0 | 0 | — |
case-15 | pass→pass | 14,613 | 7,506 | -49% | 1 | 1 | 0% | 2,596 | 3,720 | +43% | 0 | 0 | — |
case-16 | pass→pass | 13,381 | 6,050 | -55% | 1 | 1 | 0% | 2,353 | 3,460 | +47% | 0 | 0 | — |
case-17 | fail→pass | 15,520 | 11,793 | -24% | 1 | 1 | 0% | 2,530 | 4,166 | +65% | 0 | 0 | — |
case-18 | fail→pass | 13,719 | 2,700 | -80% | 1 | 1 | 0% | 2,478 | 2,831 | +14% | 0 | 0 | — |
case-19 | fail→pass | 34,580 | 5,791 | -83% | 1 | 1 | 0% | 1,539 | 3,389 | +120% | 0 | 0 | — |
case-20 | fail→pass | 10,127 | 1,385 | -86% | 1 | 1 | 0% | 1,715 | 2,522 | +47% | 0 | 0 | — |
case-21 | fail→pass | 18,635 | 7,845 | -58% | 1 | 1 | 0% | 3,089 | 3,627 | +17% | 0 | 0 | — |
case-22 | pass→pass | 5,170 | 3,174 | -39% | 1 | 1 | 0% | 982 | 2,852 | +190% | 0 | 0 | — |
case-23 | pass→pass | 5,123 | 3,129 | -39% | 1 | 1 | 0% | 999 | 2,902 | +190% | 0 | 0 | — |
case-24 | fail→fail | 16,482 | 9,898 | -40% | 1 | 1 | 0% | 2,618 | 4,054 | +55% | 0 | 0 | — |
case-25 | fail→pass | 9,479 | 5,359 | -43% | 1 | 1 | 0% | 1,561 | 3,286 | +111% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 25 cases were attempted, and 24 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +28 percentage points is the difference between those two pass rates over the 24 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
| Model | Method | Date | Lift |
|---|---|---|---|
| gemini-3.6-flash | verified | 7/23/2026 | +41% |
Other measured skills in the registry, with their headline benchmark lift.