Skip to content

Interactive transaction maxWait is inconsistent vs. pool_timeout & suppresses/bypasses connection pool limit error #20015

Open

Description

@andyjy

Problem

When the Prisma (Postgres) connection pool connection_limit is reached, query methods wait for pool_timeout to retrieve a connection from the pool, which defaults to 10s.

Problem 1:

The behaviour differs for an interactive $transaction - if maxWait is shorter than pool_timeout - and it is by default - 2s - it throw after maxWait without waiting for pool_timeout.

This inconsistency in timeout given the same underlying cause of Prisma connection pool limit reached - with a special-case behaviour for interactive transactions - seems unexpected and undesirable.

Problem 2:

The error message indicating the root cause to be a timeout waiting for a connection from the pool after the limit has been reached is suppressed/bypassed for $transaction.

All other query methods throw the error Timed out fetching a new connection from the pool. Please consider reducing the number of requests or increasing the connection_limit parameter (https://www.prisma.io/docs/concepts/components/prisma-client/connection-management#connection-pool). Current limit: 10..

However - when an interactive transaction is delayed waiting on the connection pool and maxWait is hit, the error thrown is Transaction API error: Unable to start a transaction in the given time. This is less helpful without the reference to the connection pool / connection_limit.

To me, at least, I also found this misleading - for a while I interpreted Unable to start a transaction in the given time to indicate the root cause was originating from Postgres itself (e.g. due to locking).

Suggested solution

  1. Standardise the behaviour for interactive transactions to use the pool_timeout setting rather than maxWait specifically for the time spent waiting for a free connection from the Prisma connection pool, consistent with other query methods.

  2. Ensure the error message thrown by $transaction when the reason a transaction can't be started in time is due to timeout waiting for a free connection from the Prisma connection pool mentions the connection pool as the underlying cause of the timeout, consistent with when this happens via other query methods.

Potentially (1) isn't quite the right move - I ultimately don't know the exact set of circumstances maxWait encompasses. I find the documented explanation of maxWait as "the time the Prisma Client will wait to acquire a transaction from the database" unclear - I don't understand what this refers to in terms of the underlying SQL/network/Postgres steps - some clarification to the docs could be worthwhile.

(If it turns out the connection pool timeout is the only timeout affected by maxWait, then it would appear to be duplicate/conflicting functionality and I would suggest deprecating + eventually removing maxWait in favour of applying the pool_timeout consistently everywhere.)

Thanks!

Alternatives

Maybe it's just me that finds this unexpected and misleading..? 😁 Or there may be additional reasoning behind the shorter maxWait for interactive transactions even in the case of Prisma connection pool saturation that I'm unaware of.

Additional context

I ran into this issue under CI (GitHub actions) where a) several tests are executed in parallel, and b) the automatic CPU-count-based computation of the default connection_limit under GitHub's default runners is much lower (5) than on my local development machine (17). Hence the error only occurred under CI - a classic frustrating "works on my machine..." 🙈

It took quite a while before I realise the root cause was the Prisma connection pool - which I was then able to quickly resolve by overriding the default connection_limit under CI. I only realised this by chance after enabling info level Prisma logging and noticing the log output referring to the Prisma connection pool being created with 5 connections under my CI.

Activity

  1. added
    topic: $transaction
    on Jul 3, 2023
  2. why-el commented on Oct 21, 2024

    @why-el

    I would call this a bug, and not an inconsistency. maxWait being lower than pool_timeout by default is the bug. It should be either the same or higher. It means, categorically, that a full pool will never allow any transaction to start (since the transaction will never wait long enough for the pool timeout), which is strange because it will allow "non-interactive-transaction" queries to start.

    I think the error message is not big of an issue, because it is speaking in terms of Prisma's own implementation of a Transaction API, and as long as it behaves in a manner consistent with what happened in the system (i.e. we failed to start you a transaction because your pool is full and we waited enough), I think it is fine.

  3. dennemark commented on Nov 28, 2024

    @dennemark

    I think I am running into a similar issue when trying to wrap my queries into transactions for client extensions.
    Would like to see a solution to this, too.
    I haven't found a way yet to get the pool_timeout from prisma itself. Would be nice to know in an extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions