CYBERTEC PostgreSQL Logo

Reserve connections in PostgreSQL 16

02.2023 / Category: , / Tags: |

Reserve connections for the pg_use_reserved_connections group in PostgreSQL 16

Nathan Bossart implemented a brand-new patch that provides a way to reserve connection slots for non-superusers.

The patch was reviewed by Tushar Ahuja and Robert Haas. Committed by Robert Haas. The commit message is:

Let's try reserve connections in our work!

Let's edit the postgresql.conf file and set these values:

I set superuser_reserved_connections to zero so that it does not interfere with our testing.

Now, let's create a simple mortal:

Now we connect as a user "pasha":

postgres slot down
One slot down! One to go!

From another terminal we will repeat the above command:

WTF! In previous versions you are able to use all of the max_connections slots. But not anymore!

Now the time for the new role pg_use_reserved_connections:

And now you can establish the second session:

Reserve connections? Why not regular ones?

You may wonder how this new feature can be useful. After all, we already have superuser_reserved_connections, which reserves some connections for superusers. The idea of that parameter is that even if all connections are taken, a superuser can still connect and fix the problem, typically by terminating some connections. However, not all connections are equal: even if your application's connection pool is exhausted, you still would like your backup and monitoring tools to be able to connect to the database. And if you are security conscious, you won't want those tools to use a superuser to connect. The traditional way to handle this is a connection limit on the application user, but that requires you to readjust the limit whenever you change the size of the connection pool. pg_use_reserved_connections offers a convenient alternative solution to make some connections be “more equal” than others.

Finally...

Connection pooling is a related topic that may interest you. Find out about pgbouncer, which is a free tool that makes connection pooling easy, right here.


Please leave your comments below. In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Twitter, Facebook, or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2024
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram