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 1vrvH7-00D6B0-19 for pgsql-general@arkaria.postgresql.org; Mon, 16 Feb 2026 09:52:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vrvH5-000cho-2n for pgsql-general@arkaria.postgresql.org; Mon, 16 Feb 2026 09:52:16 +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 1vrvDX-000CCe-2f for pgsql-general@lists.postgresql.org; Mon, 16 Feb 2026 09:48:35 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vruSU-00000000zEp-2ehb for pgsql-general@lists.postgresql.org; Mon, 16 Feb 2026 09:00:00 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-48069a48629so27668375e9.0 for ; Mon, 16 Feb 2026 00:59:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1771232397; x=1771837197; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=CjGsohnUSxBA4CZv1Ljaf4qIWkxtKLErgHL7rr3diPg=; b=KjX58Wh6ntbrIYFY4XDx3mHVCpgSaO1I1dy0hPdA+19u2Qgm7mmbOoXm+c8rhzU6yD TcgQvrsal4Y4pHn6F6hToBh+VAqibDWELp5QIGKo3KXBxd7yCq4KEP4HJLSYCBH+OZEp Vl0Q9DdZZFU7+Gb4EFigJMIYA1L0rlXI0pfAFfLO8oDUuXeydD2EBQ9hlRF+N8ZiWTC+ jcz2Vr5AhWJnNHvp1XpG8P+b1W8P2P709sCc4ho4mwzieFdpvLXV5rrPnF6UT5rf7z5e Sdhnj9CXuhzxmMZQvZfdZ5lw/9Lp7Gf21Iw5KtFA7myn7Om2GgCrJGdAmY4QdmiKTthT JWlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771232397; x=1771837197; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CjGsohnUSxBA4CZv1Ljaf4qIWkxtKLErgHL7rr3diPg=; b=Mu520YHlHU3mykmYUgeUQO23Z+Gvu/Fr3mQ3fQEo6wVBfogBnB6kjy3/T7VJDG00K1 o1hnBecmeLiP433QatmvA4cwaoLfA45GNkdxh37NPZl9pKE5INsXNYt2fcBeDY6OM4u7 C70IG9KbbyyGRjR57wzXNIB/+Zsa0IrSI3ft2ptlH8xC127rtwp4Mvj4N9Vp+gVUHe5+ Jh6nGjJ96Z6qnlKreJ+wfdXmeTy2kZFXF6NMA2gwoKxtdRCyxtc+R76Yvd5PG8RP+8C8 SNkFXmmCgXCrL/FTLZngazXVd+irFcVJBoNMqqOMFpFxyZdgufdMdxM1oZrGs+5pDFgh uCXQ== X-Forwarded-Encrypted: i=1; AJvYcCXIxIE3gvWK80z4hN3X1aWm998BPaIzyV777+y9cJLr2sMnhdm2gy2kvZNRSWYIgvNs1rySCxKJmalvjcEJ@lists.postgresql.org X-Gm-Message-State: AOJu0YzkFhjHyRuBhgyB+xSk/NUH8TQghnSh9uGaFmrOUqU0whSy52tJ anrcY6Lx3iEyEvIAVizMjK6ilnIbOhBEt76+YC3KcXpSD6BKXd1SLYP5+xebRJ/FyIQ= X-Gm-Gg: AZuq6aJFPDIsrTVAcFkQasBbpl/B3LEq+0L553lOunMup+RGKcJej76EcceJ82/9VAH g9+7N2nztlAHyrcKc/fZ4YTTG4bVkoqkTPxwaDJS425PzBB4XSF0qU/6Z3THeciHQ3mRgjZ8xfX Pzy0gqzorcp85Ei7VEJB2gxz0gh8EcW1T8h64NYPLUyPUI23Y8aU0bNqnpe5xud0qsNXkl2RMWS 2x8kt1DsRBaAUPKviDyanbuYgswQO/Md6COA7FNxJ3d/qtt5zXkSfJ1KxrhK0usIIiDiWbMNBL5 Un4jVbM1QAclMhMAiGi1l4ucambvwoXuxAXaqEdtfcjjFnv0mPPyJkAGtRXW8KFQkWES/1XMK/L ltIoeOA1f/OVTlE6n6EsFMh/MIGJxlBk9uUt5HNAvCYYeE1ARa4wcariNjV0r/7RzqdNSMPeGoc vv82pNAzDbXJspoww+tbz2MXJhLNgrQVOAaqxov8MOMfRRExI5gR/O X-Received: by 2002:a05:600c:1f91:b0:47d:4047:f377 with SMTP id 5b1f17b1804b1-48373a69e47mr147716135e9.36.1771232396938; Mon, 16 Feb 2026 00:59:56 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:70:b35f:c18e:4ea7:5f6d:fb6b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48371a29c13sm80420965e9.16.2026.02.16.00.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Feb 2026 00:59:56 -0800 (PST) Message-ID: <818d0359d8b629a80b55b2e068dab958fc8e0a2a.camel@cybertec.at> Subject: Re: Question on execution plan and suitable index From: Laurenz Albe To: yudhi s , pgsql-general Date: Mon, 16 Feb 2026 09:59:55 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.3 (3.58.3-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2026-02-16 at 00:34 +0530, yudhi s wrote: > It's postgres version 17. We are having a critical UI query which runs fo= r ~7 seconds+. The requirement is to bring down the response time within ~1= sec. Now in this plan , If i read this correctly, the below section is con= suming a significant amount of resources and should be addressed. i.e. "Ful= l scan of table "orders" and Nested loop with event_audit_log table". >=20 > Below is the query and its complete plan:-=C2=A0 > https://gist.github.com/databasetech0073/f564ac23ee35d1f0413980fe4d00efa9 >=20 > I am a bit new to the indexing strategy in postgres. My question is, what= suitable index should we create to cater these above? >=20 > 1)For table event_audit_log:- Should we create composite Index on column = (request_id,created_at,event_comment_text) or should we create the covering= index i.e. just on two column (request_id,created_at) with "include" claus= e for "event_comment_text". How and when the covering index indexes should = be used here in postgres. Want to understand from experts?=C2=A0 > 2)Similarly for table orders:- Should we create a covering index on colum= n (entity_id,due_date,order_type) with include clause (firm_dspt_case_id). = Or just a composite index (entity_id,due_date,order_type). > 3)Whether the column used as range operator (here created_at or due_date)= should be used as leading column in the composite index or is it fine to k= eep it as non leading? >=20 > -> =C2=A0Nested Loop =C2=A0(cost=3D50.06..2791551.71 rows=3D3148 width=3D= 19) (actual time=3D280.735..7065.313 rows=3D57943 loops=3D3) > =C2=A0=C2=A0Buffers: shared hit=3D10014901 > =C2=A0=C2=A0-> =C2=A0Hash Join =C2=A0(cost=3D49.49..1033247.35 rows=3D367= 29 width=3D8) (actual time=3D196.407..3805.755 rows=3D278131 loops=3D3) > =C2=A0Hash Cond: ((ord.entity_id)::numeric =3D e.entity_id) > =C2=A0Buffers: shared hit=3D755352 > =C2=A0-> =C2=A0Parallel Seq Scan on orders ord =C2=A0(cost=3D0.00..102287= 2.54 rows=3D3672860 width=3D16) (actual time=3D139.883..3152.627 rows=3D294= 4671 loops=3D3) > =C2=A0=C2=A0Filter: ((due_date >=3D '2024-01-01'::date) AND (due_date <= =3D '2024-04-01'::date) AND (order_type =3D ANY ('{TYPE_A,TYPE_B}'::text[])= )) > =C2=A0=C2=A0Rows Removed by Filter: 6572678 > =C2=A0=C2=A0Buffers: shared hit=3D755208 You are selecting a lot of rows, so the query will never be really cheap. But I agree that an index scan should be a win. If the condition on "order_type" is always the same, a partial index is ide= al: CREATE INDEX ON orders (due_date) WHERE order_type IN ('TYPE_A', 'TYPE_B= '); Otherwise, I'd create two indexes: one on "order_type" and one on "due_date= ". Yours, Laurenz Albe