Skip to main content

PostgreSQL: How to kill the idle connections


Establishing connections is the initial stage in making changes or reading data from a PostgreSQL.
Each link, on the other hand, generated overhead through the use of method and storage. That's why a device with few resources (memory, storage, and hardware) can handle a small number of connections. It should continue throwing errors or rejecting connections once the limited aggregate has gone far beyond a point.

PostgreSQL does a good job of limiting links within PostgreSQL.conf. We'll look at the many states that PostgreSQL links can have in this tutorial. We'll show you how to identify whether a link is active or has been dormant for an extended period of time, in which case it can be severed to free up resources and links. 

What are the different Connection States in PostgreSQL:

When a PostgreSQL connection is formed, it can conduct a variety of actions that cause state transitions. Depending on the state and the length of time it has been in each condition, a sensible conclusion should be made about whether the link is functional or has been left idle/unused. It's worth noting that the application will continue to run until the connection is intentionally closed, squandering resources long after the client has been disconnected.
A link can exist in one of four states: 

  • Active: This indicates that the link is operational.
  • Idle: This indicates that the link is inactive, and we must keep track of how long it has been idle.
  • Idle (in transaction): This indicates that the backend is working on a query, despite the fact that it is actually idle and waiting for input from the end user.
  • Idle in transaction (aborted): This is the same as idle in process. One of the declarations, however, resulted in an error. Depending on how long it has been idle, it can be tracked. 

How to see the active connections in PostgreSQL

The built-in view 'pg stat activity' in the PostgreSQL catalog tables can be used to check statistics on what a link does or how long it's been in this state. Open the query tool and run the following query to see all the statistics for each database and connection state: 

select * from pg_stat_activity

When you look at the data output side, you'll see a table with numerous columns. The values of the field' state' can be used to examine the states of connections. 


SELECT state, pid, usename, usesysid, datid, datname, application_name, backend_start, state_change, state FROM pg_stat_activity WHERE state = 'idle';

 

How to identify the Idle Connections in PostgreSQL

Within the above-mentioned results, the "state" appears to be the only value we're looking for.
We'll utilize this data to figure out which processes or queries are in which statuses, and then investigate further. By refining the query, we may narrow down the details we're looking for, allowing us to plan an intervention on that precise link. We may do this by utilizing the WHERE clause to select only the idle PIDs and the statuses for those PIDs. 

We should additionally keep track of how long the link has been dormant to ensure that we aren't wasting resources on inactive links. Use the command below to only show idle records:

SELECT pid, usename, usesysid, datid, datname, application_name, backend_start, state_change, state FROM pg_stat_activity WHERE state = ‘idle’;

How to Kill the idle connections in PostgreSQL

Now that you've identified any idle connections, it's time to terminate them. We could utilize the simple command to simply terminate the back-end mechanism without affecting the server's activity once we've whittled down the process either in a hold state or inactive for a long time.
In a terminate function, we must specify the Process ID within the query. 
  


SELECT pg_terminate_backend(7540); 

, where 7540 is the PID. 


For better configuration of PostgreSQL database, optimizing the configurations mentioned in the postgresql.conf file is mandatory. 

You can refer here for memory configurations. 

Tunning PostgreSQL for better performance


Popular posts from this blog

Tuning postgresql for better performance - ADempiere ERP

PostgreSQL is a highly customizable relational database management system (RDBMS) with a dazzling array of configuration options.   The postgresql.conf file is located in the \PostgreSQL\9.x\Data folder. You can tune the following sections of the postgresql.conf file to get the best performance.     CONNECTIONS AND AUTHENTICATION In this section change the following entries to the following: max_connections = 350 authentication_timeout = 7min RESOURCE USAGE (except WAL) In this section change the following entries to the following: shared_buffers = 256MB : PG Backends that need to access tables first look for needed blocks in this cache. If they are already there, they can continue processing right away. The change can be made with a   postmaster  command-line flag or by changing the value of   shared_buffers temp_buffers = 32MB max_prepared_transactions = 20 work_mem = 1024MB maintenance_work_mem = 1024MB QUERY TUNING effect...

Installation of Adempiere ERP on Windows

              This installation instruction is intended for initial installations where the database, application server and client all run on the same machine. For more complex installations, see   Installation Steps . An installation can take as little as 15 or 20 minutes if you start with the required downloads and do everything correctly. Other alternatives you might want to investigate are the   Windows Installer   or VMWare/VirtualBox AVA packages. However, the following method will give you much more control over the installation including upgrades with the latest patches and scripts. Before you begin, download each of the following packages: §   Java SE Development Kit   - Get the latest from   http://java.sun.com/javase/downloads/index.jsp . You only need the   JDK   without JavaFX, EE or NetBeans bundles. §   Postgre SQL   - Get the latest Windows install from   http://www.po...

Send Email Through Adempiere ERP

Send Email Through Adempiere E-Mail Configuration in Adempiere Mail Server This will specify the mail server to use Default: mailserver.(domain portion of %{serverFQDN}) Example: smtp.gmail.com Admin Email Use the default administrative mail address and it can be overwritten on client level Default: adempiere@(domain portion of %{ serverFQDN}) Example: teksalahadempiere@gmail.com Mail User Here we specify user of the default mail account Default: adempiere Example: teksalahadempiere Mail Password The password of the default mail account Default: adempiere Example: ********** 1    2.     Configuring Email in the client window The Client Definition Tab defines a unique client Step 1 .Login as admin Step  2 . Go to Menu -> System Admin -> Client Rules -> Client. In tab client fill fields’ mail host, request email request user and request user password. Finally press Test Em...