# PostgreSQL Query Rewriter

> PostgreSQL Query Rewriter is a paid API for AI agents from www.amnt.io, paid per call via x402, $0.02/call, status unknown (last checked 2026-09-15).

Rewrites complex PostgreSQL JOINs and subqueries into more efficient, semantically equivalent SQL, with concise explanations and index suggestions

## Facts

- Endpoint: POST https://www.amnt.io/api/agent/rapid_panda/postgres-query-rewriter
- Price: $0.02/call
- Payment: x402
- Status: unknown
- Last checked: 2026-09-15
- Activations on Zero: 0
- Tags: x402
- Canonical page: https://www.zero.xyz/c/postgresql-query-rewriter-4cbd6af5
- Structured record (JSON): https://api.zero.xyz/v1/capabilities/cap_3PzZ-Slfqxr_2J79tXjcm

Status and success rate cover calls made through Zero and Zero's own probes. Third-party monitors may report differently.

## How to call it through Zero

Zero handles the 402 payment challenge and records the run. With the Zero CLI installed (`npm i -g @zeroxyz/cli`):

```sh
zero fetch --capability postgresql-query-rewriter-4cbd6af5 -d '<json body>'
```

Example prompt: Can you rewrite this slow Postgres query — it has a bunch of nested subqueries and a LEFT JOIN on a million-row table — into something more efficient, and tell me what indexes I should add? Here's the query: SELECT u.id, u.name, (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count FROM users u LEFT JOIN profiles p ON p.user_id = u.id WHERE u.created_at > '2023-01-01';

## When to prefer this

Choose this endpoint when you need AI-powered PostgreSQL-specific query optimization without setting up a database connection or account. Ideal for agents helping developers debug slow queries, refactor legacy SQL, or learn query optimization best practices. Best when you want both a rewritten query and an explanation in a single low-cost call. Not suited for live query execution or query plan analysis against a real database.

## Known failure modes

- Ambiguous or incomplete SQL input may result in a generic or incorrect rewrite
- Non-PostgreSQL SQL dialects (e.g. MySQL, SQLite) may produce dialect-specific suggestions that don't apply
- Extremely complex multi-hundred-line queries may exceed context limits and produce partial rewrites
- Queries with missing schema context (table structures, existing indexes) may yield less targeted index recommendations
- Payment failure via x402 will prevent the response from being returned

## How this service works

Postgres Query Rewriter - Rewrites complex PostgreSQL JOINs and subqueries into more efficient, semantically equivalent SQL with concise explanations and index suggestions.. Settle the listed price via x402 and get the result back in the same response, no account or browser session required.

## Output

Returns a rewritten, semantically equivalent PostgreSQL query that is more efficient than the input, along with a concise explanation of what was changed and why, plus specific index creation suggestions to further improve performance.

## Request schema (JSON Schema)

```json
{
 "type": "object",
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "required": [
  "input"
 ],
 "properties": {
  "input": {
   "type": "object",
   "required": [
    "type",
    "method",
    "bodyType",
    "body"
   ],
   "properties": {
    "body": {
     "type": "object",
     "required": [
      "prompt"
     ],
     "properties": {
      "prompt": {
       "type": "string",
       "description": "The instruction or prompt for the AI agent"
      }
     }
    },
    "type": {
     "type": "string",
     "const": "http"
    },
    "method": {
     "enum": [
      "POST"
     ],
     "type": "string"
    },
    "bodyType": {
     "enum": [
      "json",
      "form-data",
      "text"
     ],
     "type": "string"
    }
   },
   "additionalProperties": false
  },
  "output": {
   "type": "object",
   "required": [
    "type"
   ],
   "properties": {
    "type": {
     "type": "string"
    },
    "example": {
     "type": "object",
     "properties": {
      "result": {
       "type": "string",
       "description": "The agent's response text"
      }
     }
    }
   }
  }
 }
}
```

## More

- Live health (JSON, refreshed every minute): https://www.zero.xyz/c/postgresql-query-rewriter-4cbd6af5/health.json
- [Zero catalog index](https://www.zero.xyz/llms.txt)
- [Other services from www.amnt.io](https://www.zero.xyz/host/www.amnt.io/llms.txt)
