pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steve Atkins <steve@blighty.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Fwd: sensible configuration of max_connections
Date: Fri, 7 Feb 2020 13:35:45 +0000
Message-ID: <a3c04d35-627d-ed80-d047-9bbe45119bd6@blighty.com> (raw)
In-Reply-To: <d555e64d-9fb8-1eb9-775a-61aaa698ad5a@withers.org>
References: <e1c73911-cfee-8b5d-d847-2487729f35f5@withers.org>
	<CAF0QPmjXSv+wdOF_CFn0ZEHMEQekqMe93w+nCkLjX=6k2hUF_w@mail.gmail.com>
	<CAF0QPmjW85wFZ=44C3rxBskNG4H9iqui7eqAdAatmNAmrcFCMA@mail.gmail.com>
	<d555e64d-9fb8-1eb9-775a-61aaa698ad5a@withers.org>


On 07/02/2020 13:18, Chris Withers wrote:
> On 07/02/2020 12:49, Chris Ellis wrote:
>> What's "too much" for max_connections? What happens when you set it to
>>
>>     high? What factors affect that number?
>>
>>
>> When sizing max_connections you need to trade off how many 
>> connections your application will use at peak vs how much RAM and CPU 
>> you have.
>>
>> Each connection is capable of allocating work_mem and has a stack etc.
>>
>> As such you don't want max_connections to be able to run your system 
>> out of RAM.
> Sure, but that's where I'm trying to find out what's sensible. The box 
> has 196GB memory, most of that in hugepages, 18 core Intel Skylake 
> with HT on giving 36 cores and tonnes of SSD for storage. How would I 
> turn that spec into a sensible number for max_connections? As that 
> number grows, what contention points in postgres will start creaking 
> (shared memory where the IPC happens?)

The max_connections setting  is an upper limit after which postgresql 
will reject connections. You don't really want to hit that limit, rather 
you want to keep the number of concurrent connections to a reasonable 
number (and have max_connections somewhere above that).

Each connection is a postgresql process, so active connections are 
competing for resources and even idle connections take up some RAM. 
Creating a new connection is launching a new process (and doing some 
setup) so it's relatively expensive.

Doing some sort of connection pooling is a good idea, especially for 
web-apps that connect, do a few short queries and disconnect. Django is 
probably doing a passable job at pooling already, so you might want to 
see how many connections it's using under normal load. Adding a 
dedicated pooler in between Django and PostgreSQL would give you more 
flexibility and might be a good idea, but if what Django is already 
doing is reasonable you may not need it.

What's a good number of active connections to aim for? It probably 
depends on whether they tend to be CPU-bound or IO-bound, but I've seen 
the rule of thumb of "around twice the number of CPU cores" tossed 
around, and it's probably a decent place to start, then run it under 
normal load and see how it behaves - cpu usage, RAM, IO, request latency 
and throughput.

Cheers,
   Steve

view thread (9+ messages)  latest in thread

Message-ID: <a3c04d35-627d-ed80-d047-9bbe45119bd6@blighty.com>
Permalink:  ../a3c04d35-627d-ed80-d047-9bbe45119bd6@blighty.com/
Also on:    postgresql.org/message-id/a3c04d35-627d-ed80-d047-9bbe45119bd6@blighty.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: steve@blighty.com
  Subject: Re: Fwd: sensible configuration of max_connections
  In-Reply-To: <a3c04d35-627d-ed80-d047-9bbe45119bd6@blighty.com>
  References: <e1c73911-cfee-8b5d-d847-2487729f35f5@withers.org> <CAF0QPmjXSv+wdOF_CFn0ZEHMEQekqMe93w+nCkLjX=6k2hUF_w@mail.gmail.com> <CAF0QPmjW85wFZ=44C3rxBskNG4H9iqui7eqAdAatmNAmrcFCMA@mail.gmail.com> <d555e64d-9fb8-1eb9-775a-61aaa698ad5a@withers.org> <a3c04d35-627d-ed80-d047-9bbe45119bd6@blighty.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox