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 1vmCAK-008cdY-1C for pgsql-general@arkaria.postgresql.org; Sat, 31 Jan 2026 14:41:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmCAH-008Iw1-1C for pgsql-general@arkaria.postgresql.org; Sat, 31 Jan 2026 14:41:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vmCAG-008Ivn-2y for pgsql-general@lists.postgresql.org; Sat, 31 Jan 2026 14:41:33 +0000 Received: from mail-yw1-x112a.google.com ([2607:f8b0:4864:20::112a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vmCAF-00000000NDW-0TbK for pgsql-general@lists.postgresql.org; Sat, 31 Jan 2026 14:41:33 +0000 Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-794c2db2ee5so1096887b3.2 for ; Sat, 31 Jan 2026 06:41:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769870489; cv=none; d=google.com; s=arc-20240605; b=PWMFwxn2fhLAIg5DhCGZAG4hgVgvs5cbFD/lUm52KZFrD4d2MFmc35EHCPfIlV4k6S D37crBqRnPgVqNKAplOZ5xPH9zOAxJ84aA9jYiAG0ChcC3LbpL5dyPD8zJc6I/78cqXI AL665r9dba+UiNBIt180xSR1iFNpeiUBQdWV1OL1ej3eWVjpK6hTR6NmO/OLAKknjDkp n1cEodTYPZ3wxdS9QpOvPSyzgKWAZhtEzE0Mtpbp/y75r0+VNllCI+dNexlbuxdd222A 7exvo7JDseZK30Eic5ncUp1UPRkrB/Iy6XjnkNIVfffA61rKlJ2lMdBCzCaNBSQrsugw 08GA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=65w3GNeSuAttQdEfISVLMMTBI6U173ZEJgCmCkR+f8Q=; fh=96VaAOi4JBVy5mHg9iGH6j8s4dG9NsU8MuMPAn4D9lI=; b=Zd5hCYCYsO08gf/YlizKArIcDUMqYe1A61P+MOMjNiT78XZMs8ym/aODeBbXIgaSbL pkOPVrvIwFt+i7i46QxvJcy6/mHYApgKLtinZkE7vJgTxVmIstwFFTjYQpXPF9NhIO/G 1fnlnTzhvBfgyg1kX/E6rdJkpY2AJAppyk6jx3Pc8MYY/cGqawwa9hqoNbFcBUY0eF1r MAZNFhCyRN89UCJjvynoYevOMX4jKH3uu19iQyZ5r1c/bThzQ4z5HrQocbVVQlMD7WbG qyzkCAoQsvdG/DA0pRVzvnO7mlB4o1mPU+3MqzJtVXBm8Ofui+LyO54HbrPJGw9JuzGs hLjA==; 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=1769870489; x=1770475289; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=65w3GNeSuAttQdEfISVLMMTBI6U173ZEJgCmCkR+f8Q=; b=IMw7y+kUMPe8JoQj80eDrAu7swOpI+PQtm31NfMlVnQkK7StW4dGa7FuB4FArXPVGU KsmmrgPup9gbJofEeTiGZE1EOtq2YwZNhfFp8meVjjKD6jCW4nwK5D6hWNCOz0l2Dles Cjs4x87TJQ7bN56leAS+Vocxjd8NUwNBNc4FeF4Q598wj8wXRKgTVWjeIoH+IKCsp1Zo 0fUJ4FFC5/WMLHM1oUYgqhDeimx+gNt8M3fGWG90bW/PMFk9Cj1tQV9q26zWXaa1xemZ yiwN9ydRt19IPqn2ZQHeio8FpVu7fl8Wk+fSKQtMOHoZ/4LsggytdvIFM0bM7P1niVi4 zChQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769870489; x=1770475289; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=65w3GNeSuAttQdEfISVLMMTBI6U173ZEJgCmCkR+f8Q=; b=DjU+41i/HiFpTpDGu7/ei3VI1ZO9yws5UoQ4r6TFKSYTyu4yqNVfP3mQo3udC/HPX1 1l82BwQr5lCv1iSeSlawjh7jOWtRTI191xhr6X7QXjQkp2/S5vbJBoJY/Qd4apP1aArr eAHZ0CXKVkQ0g6aSN55WcjjkM3p5qRB11cycidSeiHHObKAARLuxHBkPThL5kxvAt6aK njYgbKqisRzf6uN/JFjyr1NfzScO4E6RX65FHySQanT3rkpj7l1ejB1Iczs7XxxN8noq C5c4OEicKpLDT7u3KukA4RRc1sHBirw3ZlVnZDAqVPqFKoyLVmlTDiqtisQ7h/SiJjgG soew== X-Gm-Message-State: AOJu0YyNPC0xNfYGAXmGDY+HsmlkuvQtskqNyEe71uNpT1K/U/Vdrz5N xiwbGT+cjT95rWUUrb493efd5A84hid8+fjKZJQS/txZ5GZDf6GjxQaLDTYGAiDd8LOkCoY7o1n ZBWKxHPbUximfbxN7c32efTInqIt+mRc= X-Gm-Gg: AZuq6aK9q2/mLIfOFxqB4WLrMGcuwYomX730UE6oj57M6A/F5rKIxqgQNQmhNV/fszT UUHyu9IbGHk3nG2ReCH8h1SLJg/tCpJcd/mWkxdLTcWYgnaULrSbeivPbKjZp8BT8xcJeuTkkdB QtNkeul/p6bXiSQKyj/ZCMm4A/qTmSzGOAKABx1fq38iHweaBY1q0lBZkVT8mai0Wvn3l05arsF H9BFY3YEy3/5nx2jyNpvFgLCinPzFMbFI94vbdD3Kd+gOKGXSnVoVL84aRMd5ue9guyAvVzK9en AihWaOs= X-Received: by 2002:a05:690c:a91:b0:794:897:9054 with SMTP id 00721157ae682-7949dfb5554mr50232267b3.42.1769870489182; Sat, 31 Jan 2026 06:41:29 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: David Mullineux Date: Sat, 31 Jan 2026 14:41:18 +0000 X-Gm-Features: AZwV_Qg0itYX3cg3tMw7eFSRjL05jsQOMoeXclDrB1peeRbxnoueEcdX-BejSQA Message-ID: Subject: Re: Top -N Query performance issue and high CPU usage To: yudhi s Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000ce67790649b01552" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ce67790649b01552 Content-Type: text/plain; charset="UTF-8" On Sat, 31 Jan 2026, 13:30 yudhi s, wrote: > 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 > Plan says it's using temp files for sorting....I would suggest you increase work_mem for this to avoid temp.fike creation...Although not the answer to all your problems, it would be a good start . --000000000000ce67790649b01552 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Sat, 31 Jan 2026, 13:30 yudhi= s, <learnerdatabase99@gm= ail.com> wrote:
Hello Experts,
=C2=A0We have a "Select" query which is us= ing three to five main transaction tables (txn_tbl, txn_status, txn_decisio= n, 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 wh= ich some are master and some other small tables.

When we are runnin= g this query , and it's taking ~2-3seconds , however when we hit this q= uery from 10-15 session at same time its causing CPU spike up to ~50-60% fo= r the DB instance and this is increasing and touching 90% when we are incre= asing the hits further to 40-50 times concurrently.

This query is g= oing 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 su= pposed to allow thousands of users to hit this same query at the first land= ing 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:-

Regard= s
Yudhi=C2=A0

Plan says it's using temp files for = sorting....I would suggest you increase work_mem for this to avoid temp.fik= e creation...Although not the answer to all your problems, it would be a go= od start .

--000000000000ce67790649b01552--