# The RAG client was stealing my connections

> Our vectors live in the same PostgreSQL as the application, which is the right call until three clients want the same twenty connections. Last weekend the retrieval pool alone was sized to the whole role limit and the vector store was opening more on top, so these are notes on budgeting connections instead of adding a database.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2026-06-13
- Tags: PostgreSQL, pgvector, Prisma, Node.js, AI, Heroku
- URL: https://deyanpeev.com/posts/the-rag-client-was-stealing-my-connections

We keep our embeddings in PostgreSQL, in the same instance, with no vector
database next to it. They are `pgvector` columns on the same tables the
application reads, so a retrieval query can join to organisation and
permissions without a second round trip, and there is one backup, one migration
story, one thing to be on call for. I still think that's right. But it has a
consequence that the "just use Postgres" argument tends to skip, and it caught
up with us last weekend.

# P2028

The symptom was `P2028: unable to start a transaction in the given time`,
arriving in bursts from the API under moderate load. There was no slow query
behind it. Prisma was failing to *acquire a connection* within its default
two-second wait, before it even began the transaction. The database was fine
and the pool was empty.

Our Postgres plan allows the role roughly twenty connections. I knew that, and
I thought I had sized for it. Then I counted the clients.

# Three clients, one role

The application had three separate pools pointed at the same `DATABASE_URL`,
written at three different times by people solving three different problems,
and each one was sized as if it were the only one:

- **The main Prisma client**, with a pool max of 15. It serves HTTP requests
  and, more to the point, the twenty-odd background jobs that run inside the
  same web process.
- **The analytics Prisma client**, a second instance against a reporting
  connection string that happens to share the role, with a pool max of 10.
- **The RAG service**, a plain `pg.Pool` for `pgvector` queries, with
  `max: 20`.

Fifteen plus ten plus twenty, against a limit of twenty. The retrieval pool
alone was sized to the entire role. It hadn't gone wrong before only because
pools grow lazily and the RAG path wasn't busy.

And one thing made this worse than the arithmetic. We moved to Prisma 7 this
month, which means the `pg` driver adapter in place of the old Rust query
engine. The old engine honoured `connection_limit` and `connect_timeout` as URL
parameters, and `pg` silently ignores them. So anyone who had tuned the pool
through the connection string had been tuning nothing since the upgrade.

# The fourth client I had not counted

The RAG service builds its vector store through LangChain's `PGVectorStore`,
and it was passing connection options, not a pool:

```ts
const store = await PGVectorStore.initialize(embeddings, {
  postgresConnectionOptions: { connectionString: process.env.DATABASE_URL },
  tableName: `${table}_embeddings`,
  // ...
})
```

Given options instead of a pool, `PGVectorStore` constructs its own `pg.Pool`.
And `initialize` was being called per lookup, per table. So on top of the
twenty-connection pool the service already owned (and wasn't using for this),
each retrieval created a *fresh* pool, with `pg`'s default max of ten, and
never closed it. The stated budget was wrong and the real one was unbounded.

The fix is the smallest diff in all of this:

```ts
const store = await PGVectorStore.initialize(embeddings, {
  pool: this.pool, // reuse the one we configured, do not let the store make its own
  tableName: `${table}_embeddings`,
  // ...
})
```

Now there is one pool with a size somebody chose, and the store borrows from
it.

# Writing the budget down

The rest was arithmetic, and then writing the arithmetic where the next person
will read it. The three pools now default to 9, 3 and 3, so fifteen in total,
which leaves around five for the release-phase migration, a `psql` session, and
whoever has Prisma Studio open. Each is an environment variable, and each has
a comment pointing at the same paragraph in `lib/prisma.ts` that lays out the
sum and names the constraint: *`pg` caps connections per pool; there is no
global limit; the role's limit is shared by every pool that points at this
URL.*

Two smaller things rode along. The RAG pool had no `ssl` setting, and `pg`
doesn't enable TLS by default where the old engine did implicitly. Managed
Postgres rejects the unencrypted attempt, so the pool now uses the same
`resolveSsl` helper the main client does. And the transaction `maxWait` went
from two seconds to eight, with a ten-second `timeout` on the transaction
itself (both comfortably inside the thirty-second router limit). Two seconds to
acquire a connection is fine when the pool is idle and hopeless when the jobs
and a CMS fan-out are competing for nine of them.

# What this did not fix

Nine connections for HTTP and twenty background jobs is only a budget. The main
pool is still the contended resource, and if P2028 comes back, raising the
number past the role limit won't help. That just moves the error from Prisma to
Postgres. The durable fixes are a connection pooler in front of the database,
or moving the jobs out of the web process into a worker of their own. Both are
on the list, and neither was a weekend job.

Putting vectors in the application database is still a good decision. It does
make retrieval one more client of a shared, finite resource, and that client
has to be given a number and held to it like the others. Until last weekend the
vector store had no number at all.
