Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vnf77-009wt8-34 for pgsql-general@arkaria.postgresql.org; Wed, 04 Feb 2026 15:48:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnf76-00C9BP-37 for pgsql-general@arkaria.postgresql.org; Wed, 04 Feb 2026 15:48:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vnf76-00C9BD-1Y for pgsql-general@lists.postgresql.org; Wed, 04 Feb 2026 15:48:20 +0000 Received: from sonic310-24.consmr.mail.ne1.yahoo.com ([66.163.186.205]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vnf74-00000000Xo3-00JW for pgsql-general@lists.postgresql.org; Wed, 04 Feb 2026 15:48:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1770220095; bh=pDRW6tY8kaeB0jpoEju4MVRLBaYqQ90yzx+UoSfh9+8=; h=Date:From:To:In-Reply-To:References:Subject:From:Subject:Reply-To; b=CaHL5b2KRQJH66BCvcmLaMbgUxtOSFHydeYkTmsTfukM7Ek+UIOQJDQvWVTeOkzu8yNHOaFIidNr6igazlOdszUZ79P6ak813/+0RYop+CguJpagHw00oyoa2xjtoTlXbnPy9PylMVvdeAnLu4DW/lRxmC02Gcel+sjQKn0/pvyxIlf+3OllogX5l5yEeEcxHiMA/9RQdJeMsh0DFcKMs6nSi29AOujp7LykWF1Wtozmo9elr7VBeHJwlCVKnxnC9RYzdSxwgkYT/CMAS6N0UP5mCXi3YA9DcHTucco9bv4mNmXK/d71m5X3N3LmWH9mbWQWPceFMXJl0b5Sot/DYA== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1770220095; bh=21AdSQd1u3EUhZABExh9QMgJu4lM5uh3aDZBF9a70RS=; h=X-Sonic-MF:Date:From:To:Subject:From:Subject; b=JZr7IYze61lVKet4fR7WZgavYtHBmmdCRFdOqfIw5fbUyMYiagxvFS5nXhXfhE3sWZJASainVZSF8/amP19NuOQ7j/++eCawXDxItHV2c+e6kNcHthrmjrnHl+7xghLgSAMlwkHNWjylFD5ZshxYXtQ2kF3PywlJ/VrWAO2zDhwXqhdoB0SMjyApBkfca0klDska8I+vguq7SqngxyfH+Jl047NzNlB3i5A9PXOw6r34YZT8rGF+nZuTJ8qdiVqsCPo2DkgyrTwtYPDoiIsuqOlWY/ogX3OBOPjJenea937MT4jsTst/v9iE/nryqNSm5tF1Rd4DxKttRUSI1qM7Hw== X-YMail-OSG: k8wnOMsVM1lqQwXJRNP7T83ABZ2tKaEohZHjEAIJ3ox77I7Asqllko2BL38mjIo 9dbfH44g2wWAeInR5IEe_Nnb8iVZCjQZpypM4NtpjUR.PXsl8AmSWToQGDw5s0nYRIiy9fwkHh_A 2grVVS1NyvvIqq81N2q20i22lumShmSOJqF7j5crf4tStyxQkkDniEZmCu3Yhw5Xffm8fX595qQn 8pVri5VkQEaejkxK3iaHFMw_iRo9ImQ_sviNPUsMwU9Yyi7MOPjR2yryGO3RSe07Jagkn5jUE3Pq MTHtJrwSAqZjvWa74gkheMpAq7iscgpCMNSG2jtqSlTGhlgSYEi_Yg82.LwF0ShjEvpnkMGDdkP0 wYAYnqZeys4fXqGYgpJr0I9T3iaLvhGbe.l4laZr6b3JuU8du6g9ADzN1edSqvHEJD.gFwCodNJB u2MAKx1xeOse_cjIVsARgwikxVte4WDN9A5KuUaZKww6eyHf3WqEJMZHqsHTCJXo9HZ4i3qBHwFn 51sl1pIbxT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iRclwql26PBoLMJghLu2pDVEPtDMSLvxVhMUI.NZ.1ecmFKR_wA5VHMtE1iHlKns 1k8YxPNHWp_F1lDPKVvusa4TtUScbAeOMb5T9q5Lc1y8ivarHQ05FO2rbDXG7Fq0JCModL1ALMPf 0QdmWo7BCdvhcTkRgEMY80m4Nqb.Qd7yWQeeN_ks5jXASljocjPkZlKtPSeYc2zuyGatv7in2I2m K4YhhkN7IQXyffrVvrbfcKId8wdt4DIn8WdMln5TjO4oQwgew5Ap42pdpSUdUAReRS0uzMVmwQtP 46zfEMQfmQJi0rGQT7zQccvqYtLJgflTHbkkt77qm8ET246pmihK2qZuXjZ3KQcTnBBV_BpN.lQw UA.VQZJJjoDQgl04Nlb7jDE6ccJ2jSPe09nMZM1y9jgma8gKoxKxypTOyhh3zrpCvUPbRN3ibnYB X6TIi7yaznANeOV8ea6Q3DcqfPjN5ZN2RhgGeS.Sa1G2JWj0hXe13xE5VzkiEgEWxAkr.3mIV_ZB 75HXv_yP6CLJvmkqtaNvFwRQJEvjc5sgiDn0Pr.a3jpshj7tLSNM.Yo36gOhOreTpaKgAXekD.zC ZfhSTQdA1oi_nTGKktZ6yXRcBfJqWDsTMXkykzT4uTvLA9J5iFvUwl8PXcXXKglUI01SDw2ABVaA fkFiQ59uirO8eQQ-- X-Sonic-MF: X-Sonic-ID: 63030b9d-d4b0-4f61-ab33-87a98931bf3d Received: from sonic.gate.mail.ne1.yahoo.com by sonic310.consmr.mail.ne1.yahoo.com with HTTP; Wed, 4 Feb 2026 15:48:15 +0000 Date: Wed, 4 Feb 2026 15:48:13 +0000 (UTC) From: felix.quintgz@yahoo.com To: pgsql-general@lists.postgresql.org Message-ID: <323794933.277637.1770220093639@mail.yahoo.com> In-Reply-To: References: Subject: Re: Top -N Query performance issue and high CPU usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Mailer: WebService/1.1.25116 YMailNodin Content-Length: 2819 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Have you tried adding an index to txn_tbl.txn_type?=20 And a vacuum on all tables? It seems the visibility map is outdated. I'm using https://explain.dalibo.com to view the plan visually; it's more c= onvenient. You could use the option to periodically save the results of queries with c= ommon filters to another table, and then retrieve the results from that tab= le when a user performs a query with their own filters. You should also store the user's query results somewhere for a while to pre= vent excessive database access. I imagine this is some kind of dashboard that each user is taken to after a= uthenticating. It looks nice in presentations, but after a while in product= ion, it can make the system unusable. I had to remove similar charts from t= he homepage of a system because after a year of work, they were taking a mi= nute to load. On Saturday, January 31, 2026 at 08:30:33 AM GMT-5, yudhi s wrote: Hello Experts, =C2=A0We have a "Select" query which is using three to five main transactio= n tables (txn_tbl, txn_status, txn_decision, txn_sale, ath) holding ~2milli= on rows in each of them(which is going to increase to have ~50-100million i= n future) and others(6-7) tables out of which some are master and some othe= r small tables. When we are running this query , and it's taking ~2-3seconds , however when= we hit this query from 10-15 session at same time its causing CPU spike up= to ~50-60% for the DB instance and this is increasing and touching 90% whe= n we are increasing the hits further to 40-50 times concurrently. This query is going to be called in the first page of an UI screen and is s= upposed to show the latest 1000 rows based on a certain transaction date. T= his query is supposed to allow thousands of users to hit this same query at= the first landing page at the same time. Its postgres version 17.=C2=A0 The instance has 2-VCPU and 16GB RAM. I have the following=C2=A0questions. 1)Why is this query causing a high cpu spike ,if there is any way in postgr= es to understand what part/line of the query is contributing to the high cp= u time? 2)How can we tune this query to further reduce response time and mainly CPU= consumption ? Is any additional index or anything will make this plan bett= er further? 3) Is there any guidance or best practices exists , to create/design top N-= queries for such UI scenarios where performance is an important factor? 4)And based on the CPU core and memory , is there any calculation by using = which , we can say that this machine can support a maximum N number of conc= urrent queries of such type beyond which we need more cpu cores machines? Below is the query and its current plan:-https://gist.github.com/databasete= ch0073/6688701431dc4bf4eaab8d345c1dc65f RegardsYudhi