Thread: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

From
Md Arqum Farooqui
Date:

Hi,

I'm facing "Too many clients already" error frequently with my PostgreSQL (v9.6.5) container (i.e. being used along with CKAN application i.e. 
A open source data management system ), I have observed that PostgreSQL is not releasing IDLE connections from their end. I have set "max_connetions = 100" in postgresql.

Please provide your suggestions on below queries:

1. Why PostgreSQL is not releasing IDLE connection? (If we leave the setup then postgres keep IDLE  connection even for 1 month, after that I have to restart the Postgres)
2. Is there any parameter or any solution by which i can remove unused old IDLE connection regularly?


Thanks and Regards,
Arqum farooqui





The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. It shall not attach any liability on the originator or NEC Corporation India Private Limited or its affiliates. Any views or opinions presented in this email are solely those of the author and may not necessarily reflect the opinions of NEC Corporation India Private Limited or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately.

Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

From
Ron
Date:
On 12/8/21 12:45 AM, Md Arqum Farooqui wrote:
@font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;}p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-fareast-language:EN-US;}a:link, span.MsoHyperlink {mso-style-priority:99; color:#0563C1; text-decoration:underline;}a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:#954F72; text-decoration:underline;}span.EmailStyle17 {mso-style-type:personal-compose; font-family:"Calibri",sans-serif; color:windowtext;}.MsoChpDefault {mso-style-type:export-only; font-family:"Calibri",sans-serif; mso-fareast-language:EN-US;}div.WordSection1 {page:WordSection1;}

Hi,

I'm facing "Too many clients already" error frequently with my PostgreSQL (v9.6.5)


You really need to update to 9.6.24.

container (i.e. being used along with CKAN application i.e. A open source data management system ), I have observed that PostgreSQL is not releasing IDLE connections from their end. I have set "max_connetions = 100" in postgresql.

Please provide your suggestions on below queries:

1. Why PostgreSQL is not releasing IDLE connection?


It is the application's job to close connections when they are no longer of use.

(If we leave the setup then postgres keep IDLE  connection even for 1 month, after that I have to restart the Postgres)
2. Is there any parameter or any solution by which i can remove unused old IDLE connection regularly?


I set up a cron job that regularly kills old idle connections.

SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'databasename'
  AND pid <> pg_backend_pid()
  AND state = 'idle'
  and extract(epoch from (current_timestamp - query_start)) > 20*60 -- 20 minutes
;


--
Angular momentum makes the world go 'round.

Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

From
Paul Smith
Date:
On 08/12/2021 06:45, Md Arqum Farooqui wrote:
@font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;}p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-fareast-language:EN-US;}a:link, span.MsoHyperlink {mso-style-priority:99; color:#0563C1; text-decoration:underline;}a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:#954F72; text-decoration:underline;}span.EmailStyle17 {mso-style-type:personal-compose; font-family:"Calibri",sans-serif; color:windowtext;}.MsoChpDefault {mso-style-type:export-only; font-family:"Calibri",sans-serif; mso-fareast-language:EN-US;}div.WordSection1 {page:WordSection1;}

Hi,

I'm facing "Too many clients already" error frequently with my PostgreSQL (v9.6.5) container (i.e. being used along with CKAN application i.e. 
A open source data management system ), I have observed that PostgreSQL is not releasing IDLE connections from their end. I have set "max_connetions = 100" in postgresql.

It is the responsibility of the client software to release connections when they are no longer needed. The server won't just time-out idle connections unless it is told to do so

I guess CKAN is keeping a pool of available connections that it can reuse without having to start a new connection. You may be able to set parameters in CKAN so that it kills connections after they have been idle a while. If you're not sure, you really need to ask on a CKAN support forum

Or you could use a separate connection pooler (eg PgBouncer) and have CKAN connect to that instead of the database directly. The pooler will then manage reuse across all the client applications, and may be able to timeout idle connections for you.

-- 
Paul
Paul Smith Computer Services
support@pscs.co.uk - 01484 855800

--

Paul Smith Computer Services
Tel: 01484 855800
Vat No: GB 685 6987 53

Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

From
Thomas Kellerer
Date:
Paul Smith schrieb am 08.12.2021 um 10:18:
>> I'm facing "Too many clients already" error frequently with my
>> PostgreSQL (v9.6.5) container (i.e. being used along with CKAN
>> application i.e. A open source data management system
>> <https://ckan.org/> ), I have observed that PostgreSQL is not
>> releasing IDLE connections from their end. I have set
>> "max_connetions = 100" in postgresql.
>>
> It is the responsibility of the client software to release
> connections when they are no longer needed. The server won't just
> time-out idle connections unless it is told to do so

Automatically closing idle sessions is only available since Postgres 14



Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

From
hubert depesz lubaczewski
Date:
On Wed, Dec 08, 2021 at 01:23:32AM -0600, Ron wrote:
> I set up a cron job that regularly kills old idle connections.
> SELECT pg_terminate_backend(pid)
> FROM pg_stat_activity
> WHERE datname = 'databasename'
>   AND pid <> pg_backend_pid()
>   AND state = 'idle'
>   and extract(epoch from (current_timestamp - query_start)) > 20*60 -- 20
> minutes
> ;

Two notes:
1. it's better to use state_change and not query_start. For example, if
   I'd start 2 hours query, 3 hours ago, then "now" it would still have
   query_start at (now() - 3 hours), despite the fact that it would be
   idle only for 1 hour.
2. doing calculations in epoch is bad idea. it can become your muscle
   memory, and it will cause problems with indexing. instead:
       and state_change < current_timestamp - '20 minutes'::interval

depesz