Skip to content
← Guides

Guide · Rescue

Your AI-built app breaks in production: the symptoms, the causes, and where to start

Building with AI works: the prototype exists, it has users, and it solves something real. What the AI did not do was design it for load, concurrency, or data from several customers. This guide walks through what breaks most, why, and the order in which to fix it.

Published on 10 min read By Manuel Reyes Bravo

What happened, in one sentence

The AI optimized for "it works" in the demo. In the demo there is one user, a hundred rows, and nobody malicious. In production there are hundreds of users at once, hundreds of thousands of rows, and the database pays for every shortcut. What follows are the seven symptoms we see most, each with its typical cause and the first check you can do yourself.

1. It was fast and now every screen takes seconds

Typical causes: tables without indexes on the columns used to filter or join; the N+1 pattern (one query per row of a list); SELECT * on wide tables; unpaginated lists that pull everything into memory.

First check: enable pg_stat_statements and look at which queries accumulate the most time. Then EXPLAIN (ANALYZE, BUFFERS) on the worst one: a Seq Scan over a large table with a filter means a missing index.

sql
-- the queries that accumulate the most time
SELECT calls, round(total_exec_time) AS ms_total, round(mean_exec_time) AS ms_mean, left(query, 90) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

2. Duplicate, orphan or impossible data shows up

Typical cause: the database has no constraints. The AI put validation in the frontend or the code, and any path that does not go through it (a second tab, a repeated request, a script) leaves invalid data. Without UNIQUE there are duplicates; without foreign keys there are orphans; without NOT NULL or CHECK there are rows that mean nothing.

First check: for each important table, ask what should never be possible, and see whether the database prevents it. If the answer is "the app prevents it", nobody does.

3. It crashes when several users come in at once

Typical causes: every request opens its own database connection and nobody closes them, until PostgreSQL rejects new ones; transactions left open while waiting on an external call; serverless functions opening dozens of connections in a spike.

First check: count connections and see how many are idle inside a transaction. If many, there are transactions that never close. A connection pool (in the application or with PgBouncer) is usually the first fix.

sql
SELECT state, count(*)
FROM pg_stat_activity
WHERE datname = current_database()
GROUP BY state;

4. Errors that come and go

Typical cause: the code does not handle errors, or it catches and hides them. A failure during a write leaves half the work done because there was no transaction; a blind retry duplicates a payment; a log without context cannot tell which user, which data, what time.

First check: search the code for places where an error is caught and nothing is done with it. Each one is data that was lost in silence.

5. One user can see another user’s data

Typical cause: authorization lives in the frontend. The database accepts any id it is given, and changing a number in the URL is enough. In apps on Supabase it is common to find tables reachable with the public key and no row-level policies, which amounts to leaving the database open.

First check: take the URL or request for one of your own resources, swap the id for another user’s, and see what happens. If you can see it, anyone can. The real fix is RLS in the database, forced, with the user’s context set by authentication and not by the request.

6. There are passwords and keys in the repository

Typical cause: the AI needed it to work and put the connection string or the API key in the code. If the repository is public, they are already compromised; if the frontend ships them, too.

First check: search the full history of the repository, not only the current version. Whatever you find must be rotated (password or key changed), not just removed from the file.

shell
git log -p --all -S 'postgres://' -- . | head -50
git log -p --all -S 'sk-' -- . | head -50

7. Nobody can rebuild the database or restore a backup

Typical cause: the schema was changed by hand, from a dashboard or with loose statements, and no sequence of migrations reproduces it. The provider’s "automatic" backup was never restored anywhere.

First check: try to bring the database up from scratch on your machine, using only what is in the repository. Then restore the latest backup into a separate environment and time it. If either fails, that is your biggest risk today.

Where to start

The order matters because each step protects the next. If you do one thing this week, do the first.

  • A tested backup: actually restored somewhere else. Without this, every other fix is a bet.
  • Secrets out of the code and rotated.
  • Separation between customers in the database (RLS), if you store data for more than one.
  • What crashes: connections, transactions and error handling.
  • What is slow: indexes and the queries that accumulate the most time.
  • Constraints on the tables, so odd data stops getting in.
  • Versioned migrations and a structure the next person understands.

How to keep using AI without breaking the same things again

You do not have to stop using it; you have to change the foundation it works on. With migrations, tests on what is critical, and constraints in the database, the AI breaks far less, because the database tells it. Ask for small changes, one at a time, each with its migration and its test. And never give it superuser access to production: an agent that can bypass the rules is not behind any.

Frequently asked questions

Should I rewrite everything from scratch?

Almost never at first. An app with users already validated something worthwhile. First the diagnosis with evidence; a rewrite is decided with numbers (which part, what it costs, what it gains), not out of frustration.

Can the AI fix these problems on its own?

Some, if you ask for the exact change and can verify it. The structural ones (data model, separation between customers, transactions) need someone to decide what the database must guarantee; the AI executes better once that decision is made.

When should I ask for help?

When there is customer data and you do not know who can see it, when you cannot restore a backup, or when every fix breaks something else. Those three are signs that the problem is structural, not a bug.

Other guides

See all services →

Try it Talk