Git Product home page Git Product logo

Comments (2)

kndndrj avatar kndndrj commented on June 19, 2024

Thanks for reporting. I got that once too but I can't replicate it. It seems to be related to non freed resources at a first thought.

If you (or anyone) finds a way to replicate this, please let me know.

from nvim-dbee.

slarse avatar slarse commented on June 19, 2024

Hello @kndndrj,

TL;DR: I can replicate and (potentially) fix this issue.

First of all I just wanted to thank you for this wonderful plugin. I especially love the call log functionality, it's just incredibly useful.

But to the topic at hand. I encountered this issue as well and came to the conclusion that Dbee more or less never closes connections to PostgreSQL. As max_connections is 100 by default in Postgres you have to have a pretty long session to run into it or work against a database that already is close to the maximum.

It's easy to tell that connections accumulate by for example executing the following query multiple times with BB:

SELECT pid, state, wait_event, query FROM pg_stat_activity;

The result will include one additional row with that query for each query. It will look something like this:

   │ pid │ state  │ wait_event          │ query                                                       
───┼─────┼────────┼─────────────────────┼─────────────────────────────────────────────────────────────
 163<nil>  │ AutoVacuumMain      │                                                             
 264<nil>  │ LogicalLauncherMain │                                                             
 392 │ idle   │ ClientRead          │ SELECT pid, state, wait_event, query FROM pg_stat_activity; 
 493 │ idle   │ ClientRead          │ SELECT pid, state, wait_event, query FROM pg_stat_activity; 
 595 │ idle   │ ClientRead          │ SELECT pid, state, wait_event, query FROM pg_stat_activity; 
 696 │ active │ <nil>SELECT pid, state, wait_event, query FROM pg_stat_activity; 
 760<nil>  │ BgWriterHibernate   │                                                             
 859<nil>  │ CheckpointerMain    │                                                             
 962<nil>  │ WalWriterMain       │

As we can see, the server is waiting for the client (Dbee) which has no intention of doing anything else with that query.

Replication

First start a PostgreSQL server with a low value for max_connections. Below is a Docker command to start with a value of 4 and a Dbee connection string to connect to it.

docker run --rm -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres:16 -N 4

export DBEE_CONNECTIONS='[
    {
        "name": "postgres",
        "url": "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable",
        "type": "postgres"
    }
]'

Note: Don't set -N to anything less than 4, the server will not be able to start due to reserved connections for the superuser

Then start Dbee and execute any 4 queries, filling the max connections. The 5th query will then produce the following error:

Call execution failed after 0.006 seconds
Reason:
    c.db.Conn: pq: sorry, too many clients already

The problem

When a result set returns something, we hit this code path on the core client:

if len(result.Header()) > 0 {
result.AddCallback(func() { _ = conn.Close() })
return result, nil
}

That callback is executed when ResultStream.Close() is called, but at least in the case of the PostgreSQL part of Dbee, I can't see that actually ever happening.

I'll open a PR in a moment where I do close ResultStreams in some places that I found by haphazardly tracing around in the Postgres-relevant parts of the code base.

from nvim-dbee.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.