Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ksF7q-0000Dv-Th for pgsql-general@arkaria.postgresql.org; Thu, 24 Dec 2020 01:09:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ksF7p-0003Tu-5m for pgsql-general@arkaria.postgresql.org; Thu, 24 Dec 2020 01:09:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ksF7o-0003Tn-IO for pgsql-general@lists.postgresql.org; Thu, 24 Dec 2020 01:09:04 +0000 Received: from sonic303-3.consmr.mail.bf2.yahoo.com ([74.6.131.42]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ksF7l-0005Nl-FF for pgsql-general@lists.postgresql.org; Thu, 24 Dec 2020 01:09:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1608772138; bh=ExzF4jy2SQWjEBua8cC5J7BOK+Dt2s/U3rSYn3Wbsp8=; h=Date:From:To:Subject:References:From:Subject; b=BlTHwQ4w/+YCrOTxtBtJ2l7/8MJtz3KAv4IWUK+rd7dLsJcm6ixRC6aur1jKgjmxG/HJFgb7z4p6VkqdpBVEBKRptWc06wBgqECA1K6A/wyhIyRhvzDYnIWIxGCJjTZJP8Zw7wb2TAT1B+uwnqaWwTAKfIwHIAH0e5J9mK7DtMMxPdhl8RYG+oqhSqoG9yP2+tJ38Y11RH3wfnj61obk3yh4devpevU5BPtmiwmdDJAUefnGoWh/euMbeqLtTzfl2X4AVOeXjfSkoopuDJxyxjdi4KEyAy+lTewLI+xgA9XyFp8PEHT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01:08:58 +0000 Date: Thu, 24 Dec 2020 01:08:54 +0000 (UTC) From: Shantanu Shekhar To: "pgsql-general@lists.postgresql.org" Message-ID: <1366951904.2544671.1608772134019@mail.yahoo.com> Subject: Range search on primary key does not use index scan MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2544670_490360937.1608772134017" References: <1366951904.2544671.1608772134019.ref@mail.yahoo.com> X-Mailer: WebService/1.1.17278 YMailNorrin Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Content-Length: 8803 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_2544670_490360937.1608772134017 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I am trying to understand how Postgres uses index and ran into a surprising= behavior if someone can help me with. I have a table like so: CREATE TABLE testschema.employees (=C2=A0 =C2=A0 =C2=A0 =C2=A0 employee_id = integer not null, first_name=C2=A0 varchar(1000) not null, last_name=C2=A0 = =C2=A0varchar(1000) not null, date_of_birth date not null, phone_number var= char(1000) not null, constraint employees_pk primary key (employee_id)=C2= =A0 =C2=A0 ); At the moment I have ZERO rows in the table. I verified that employees_pk c= reates an index of type 'btree(employee_id). After that I ran explain on tw= o queries: (1) Here I am searching for employee_id '123' and as expected the plan show= s it will do an b-tree traversal on employees_pk index.=C2=A0 testdb# explain select first_name, last_name from testschema.employees wher= e employee_id =3D 123;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0QU= ERY PLAN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=C2=A0Index Scan using employe= es_pk on employees=C2=A0 (cost=3D0.14..8.16 rows=3D1 width=3D1032)=C2=A0 = =C2=A0Index Cond: (employee_id =3D 123)(2 rows) (2) Here I am searching for employee_id < 123. I was expecting the plan wou= ld use the index on employees_pk to find all leaf nodes where employee_id <= 123 and then issue read of table blocks for each of the matching entries i= n the index leaf. But looks like the query plan has decided on using full t= able scan instead and not using the index. Any ideas why is this happening? testdb# explain select first_name, last_name from testschema.employees wher= e employee_id < 123;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 QUERY PLAN=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=C2=A0Seq Scan on employees=C2=A0 (cost=3D0.00..= 10.62 rows=3D17 width=3D1032)=C2=A0 =C2=A0Filter: (employee_id < 123)(2 row= s) Any help will be greatly appreciated.=C2=A0 Thanks, Shantanu ------=_Part_2544670_490360937.1608772134017 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I am trying to understand how Postgres uses = index and ran into a surprising behavior if someone can help me with. I hav= e a table like so:

CREATE TABLE testschema.empl= oyees (
        employee_id integer=09not null,
=09first_name  varchar(1000) not null,
=09last_name  &= nbsp;varchar(1000) not null,
=09date_of_birth date=09not null,
=09p= hone_number=09varchar(1000) n= ot null,
=09constra= int employees_pk primary key (employee_id)
    );
=

At the moment I have= ZERO rows in the table. I verified that employees_pk creates an index of t= ype 'btree(employee_id). After that I ran explain on two queries:

(1) Here I am searching for employee_id '123' and as expected th= e plan shows it will do an b-tree traversal on employees_pk index. 

testdb# explain select first_name, last_name from= testschema.employees where employee_id =3D 123;
    &n= bsp;                     =          QUERY PLAN        &nb= sp;                     &= nbsp;     
=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90
 Index Scan using employees_pk on employees&= nbsp; (cost=3D0.14..8.16 rows=3D1 width=3D1032)
   Inde= x Cond: (employee_id =3D 123)
(2 rows)

(2) Here I am searching for employee_id &l= t; 123. I was expecting the plan would use the index on employees_pk to fin= d all leaf nodes where employee_id < 123 and then issue read of table bl= ocks for each of the matching entries in the index leaf. But looks like the= query plan has decided on using full table scan instead and not using the = index. Any ideas why is this happening?

testdb# explain select first_name, last_name from= testschema.employees where employee_id < 123;
    &= nbsp;                    = QUERY PLAN                  &= nbsp;       
=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90
 Seq Scan on employees  (cost=3D0.00..10.62 rows=3D= 17 width=3D1032)
   Filter: (employee_id < 123)
(2 rows)

Any help will be greatly appreciat= ed. 

Thanks,

Shantanu
<= /body> ------=_Part_2544670_490360937.1608772134017--