{"uid":"cap_3PzZ-Slfqxr_2J79tXjcm","slug":"postgresql-query-rewriter-4cbd6af5","name":"PostgreSQL Query Rewriter","description":"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.","url":"https://www.amnt.io/api/agent/rapid_panda/postgres-query-rewriter","method":"POST","headers":{},"bodySchema":{"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"}}}}}}},"responseSchema":null,"example":null,"exampleRequest":null,"tags":["x402"],"displayCostAmount":"0.02","displayCostAsset":"USDC","priceDynamic":false,"priceHint":null,"priceStatus":"priced","priceSource":"registry","requiresHandshake":false,"reviewCount":0,"rating":{"score":"0.00","successRate":"0.00","reviews":0,"stars":null,"state":"unrated"},"availabilityStatus":"unknown","priceObserved":null,"sessionDeposit":null,"pricing":{"kind":"static","summary":"$0.02/call","primary":{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.02","per":"call","confidence":"exact"},"accepted":[{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.02","per":"call","confidence":"exact"}]},"paymentMethods":[{"uid":"pm_IB_VHyFTPL1tG9-LYG8uG","protocol":"x402","methodType":"crypto","chain":"base","mode":"charge","costAmount":"0.02","costPer":"request","priority":0,"asset":null,"unit":"request","depositMicros":null,"planRef":null}],"brandName":null,"brandSlug":null,"brandBaseUrl":null,"brandDocsUrl":null,"whatItDoes":"Rewrites complex PostgreSQL JOINs and subqueries into more efficient, semantically equivalent SQL, with concise explanations and index suggestions","exampleAgentPrompt":"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';","exampleUseCases":[{"title":"Slow reporting query optimization","prompt":"This SQL report query is timing out on our production Postgres DB — it has three nested subqueries and two JOINs across large tables. Can you rewrite it to be faster and semantically equivalent, and suggest what indexes would help? Query: SELECT d.name, (SELECT SUM(s.amount) FROM sales s WHERE s.dept_id = d.id) FROM departments d LEFT JOIN employees e ON e.dept_id = d.id WHERE d.active = true;"},{"title":"Correlated subquery refactor","prompt":"I have a correlated subquery in my Postgres database that runs way too slowly for each row. Can you convert it into a JOIN or CTE that's more efficient and explain what you changed? Here it is: SELECT p.title, (SELECT AVG(r.score) FROM reviews r WHERE r.product_id = p.id AND r.verified = true) AS avg_score FROM products p;"},{"title":"Index recommendation for JOIN-heavy query","prompt":"My Postgres query with multiple JOINs is killing performance — can you rewrite it and tell me exactly which indexes to create to speed it up? SELECT o.id, c.email, p.name FROM orders o JOIN customers c ON c.id = o.customer_id JOIN products p ON p.id = o.product_id WHERE o.status = 'pending' AND o.created_at > NOW() - INTERVAL '7 days';"}],"resultDescription":"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.","failureModes":["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"],"whenToPreferThis":"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.","instructions":null,"reviewSummary":null,"reviewSummaryHighlights":null,"reviewSummaryConcerns":null,"reviewSummaryGeneratedAt":null,"activationCount":0,"lastUsedAt":null,"lastSuccessfullyRanAt":null,"lastHealthCheckAt":"2026-09-15T06:51:37.682Z","isFirstParty":false}