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 1vmB3P-008QPv-1B for pgsql-general@arkaria.postgresql.org; Sat, 31 Jan 2026 13:30:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmB3L-0088Zt-26 for pgsql-general@arkaria.postgresql.org; Sat, 31 Jan 2026 13:30: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 1vmB3L-0088Zj-0S for pgsql-general@lists.postgresql.org; Sat, 31 Jan 2026 13:30:20 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vmB3J-000KkO-2D for pgsql-general@lists.postgresql.org; Sat, 31 Jan 2026 13:30:19 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-65815ec51d3so5030010a12.2 for ; Sat, 31 Jan 2026 05:30:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769866216; cv=none; d=google.com; s=arc-20240605; b=MhbY8HIGk2X+DyAWZcQlf9nWll/vLfOyqZN9gG2J7gbyIvpcT0EWFT4JZwtiwtv/rl Tx5jybKXc7e3ipXxO064TC2QIgot1q9abYrLuC3d19LLKUAlWyZzNML7FmVHmK2HN9PL 4aJpeAFgN8u5a91blZyYHCLDuqUBoDet5p68cGGsLZU96C1At2XSRmez0hEfvEr8QtYr 4sHpWohOlzXOpZPBM3m2BoyDJCsEB9HUtNZZQvN3WRxg4GUkgs+vW3dnrWpXXH6U9I4A q37hzhYGleMxWoNVh2VrzfIZJWQUJsgrv0Tx8i+Hi4r9UFx5TRPA6Z8Su76Z3O/7Iy1l 5/Eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:mime-version:dkim-signature; bh=T3IIazB29TFNaB/EjP/ddKmRdhHh63/oGGQgrnFu6hg=; fh=U9Vkrj/8YXu+wNKP4ImG87p6jlJYRxuB/zicT9qNons=; b=CpfTGDniz+49Po2IZq0K/BV2aIj0zlsARihTeKckVmWh6SDW1d2t+mZhNTjc4Wc7/c TMKDE0lExtsls7VstMYwMxZsJY/9TyrMm+PlgMoE2N5i11+pY6iz/yyfLhQRrwPIgdfR gkXsSXUjhosJuwELdlScojuaxMx+ggoVAUNqMJV8yO0bGEG+qHDaLpmioFun8CWHhxdO G00fGjdluu5BsekxY2xOG51N6rMuilpiPnu22mAMEq3KsnnV6IoKqEgTVyd98gk6+Ydw qbdEzuiLc7LjIzOXbgH/hb4kUyLhlEj1pjDKFWQSTJnzox9IlM9Xb3RtZ+YZQb/B2GcQ sBmg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769866216; x=1770471016; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=T3IIazB29TFNaB/EjP/ddKmRdhHh63/oGGQgrnFu6hg=; b=TGobsGZgnAw26d5SqIvn+C+of7Dlu+RovRqGVoETAs5TZjLcjNbssfLgB7yd9EVO2S As/dhKxLdJTGmLdKN3Pe+yAp9yezCs0xzc2dSPsq6rlIbYvmTgeytK2mIAaX9pAE36xk kN+e10JFTq7aZ9uB5Lu3bB1R/rUSPdJkAUsrdMrkcRTXIuej4ybTHMaG331P2uiXxnUT zKx9ApnWDK5wDKbzyXfD945BuY0BKsq8HFiOJ+Uc3RZ0aLXDokfhXRrUp8zANgJmZ47a PdP7oKSun8i8qOg6F202jW4NVifbxdRLSXho08pJTKm/p4DP+dZBoB7XVWt0e5o3SvSK sbtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769866216; x=1770471016; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T3IIazB29TFNaB/EjP/ddKmRdhHh63/oGGQgrnFu6hg=; b=oktBosenngxy6NVhLcciphO1TdXhIDy9KwcGPAYc2wyqYI9539M1eF0EDge1XsZN3/ AkVBLD/Up4RMZIcfmHTMnNKyf1jx9p2SaxdisbEccKS6ewgo22YAPgmGvecMkRZ5U2Yn CvSx3CsTV8QpfJbmtRe/1hohFya90hQ7B7oZeK7+smOWW4fPgvP3NofzKs9/cKA2VbMY PyEEyXpVrMGnnpA78CS1hizIWBGB1ri3t20/LMmeMzp3JGue1QmkfoUSq1BJLs/a15FP zm7rT9ncnHETWU4me/EaYOxgZ6a6WZbjYS3qZXtGfKZHPj34vJHl1/uIU1d2d84SwuIr Uy0A== X-Gm-Message-State: AOJu0YwwQDYEQB7614zHynKAHMOGAAihtGJq499Oo9O8Nf9rq/iVSOff oKuLozObbkt8DymjAl7QFTjWA4eVBrDG0/obq/J3ILQ4zAn53g7cCiw2u8HhubJbZxhWXwntY1a LCgmDHp6AFRCW3+p4g0ISrluuOGycdFJHpAV6 X-Gm-Gg: AZuq6aI8DTfSx8I4WF5Oeu/dSEtfwXieT7JDxpVfHzBOhoJ08M6JSzNeaP1/Sh1R3Ee g9FHBQHRW4OUFmh3HVdCF1DP44o6rONeW+XL3yivfzjs1ijcU2GuX7aPtkt7woiE3StND/Fgvuy v+4wqcE7AWwaq8hXQBQBNmZjgpe+S4Vys2/YLGrhMX3WlkdmFWqWvvUVwOIZszgSpcw6SJlmUD5 z0YiQBAQAutle8o1schvQasNLenxvQR5ETRPHYvIKA4EmXM2ndHvTR9R8DzdB/Gz7zqKbnnx3O7 3/zqEubnOW4HQgVqNTYt6y+Y1A== X-Received: by 2002:a05:6402:1453:b0:658:776:5737 with SMTP id 4fb4d7f45d1cf-658de5ab8a4mr4110789a12.17.1769866216285; Sat, 31 Jan 2026 05:30:16 -0800 (PST) MIME-Version: 1.0 From: yudhi s Date: Sat, 31 Jan 2026 19:00:03 +0530 X-Gm-Features: AZwV_Qihg7ByP7ZvdqPaVKsCyVUxp3P0eL3X6LGTaO8CruAAtDQhrEfhshAC-RI Message-ID: Subject: Top -N Query performance issue and high CPU usage To: pgsql-general Content-Type: multipart/alternative; boundary="0000000000001f2c590649af1756" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001f2c590649af1756 Content-Type: text/plain; charset="UTF-8" Hello Experts, We have a "Select" query which is using three to five main transaction tables (txn_tbl, txn_status, txn_decision, txn_sale, ath) holding ~2million rows in each of them(which is going to increase to have ~50-100million in future) and others(6-7) tables out of which some are master and some other 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% when 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 supposed to show the latest 1000 rows based on a certain transaction date. This 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. The instance has 2-VCPU and 16GB RAM. I have the following questions. 1)Why is this query causing a high cpu spike ,if there is any way in postgres to understand what part/line of the query is contributing to the high cpu 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 better 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 concurrent queries of such type beyond which we need more cpu cores machines? Below is the query and its current plan:- https://gist.github.com/databasetech0073/6688701431dc4bf4eaab8d345c1dc65f Regards Yudhi --0000000000001f2c590649af1756 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello Experts,
=C2=A0We have a "Select" query= which is using three to five main transaction tables (txn_tbl, txn_status,= txn_decision, txn_sale, ath) holding ~2million rows in each of them(which = is going to increase to have ~50-100million in future) and others(6-7) tabl= es out of which some are master and some other small tables.

When w= e are running this query , and it's taking ~2-3seconds , however when w= e hit this query from 10-15 session at same time its causing CPU spike up t= o ~50-60% for the DB instance and this is increasing and touching 90% when = we are increasing the hits further to 40-50 times concurrently.

Thi= s query is going to be called in the first page of an UI screen and is supp= osed to show the latest 1000 rows based on a certain transaction date. This= query is supposed to allow thousands of users to hit this same query at th= e first landing page at the same time.=C2=A0 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 postgres to understand what part/lin= e of the query is contributing to the high cpu time?
2)How can we tune t= his query to further reduce response time and mainly CPU consumption ? Is a= ny additional index or anything will make this plan better further?
3) I= s there any guidance or best practices exists , to create/design top N-quer= ies for such UI scenarios where performance is an important factor?
4)An= d based on the CPU core and memory , is there any calculation by using whic= h , we can say that this machine can support a maximum N number of concurre= nt queries of such type beyond which we need more cpu cores machines?
<= br>
Below is the query and its current plan:-

Regards
Yudhi=C2=A0
--0000000000001f2c590649af1756--