Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzKMj-0005Ub-7h for pgsql-general@arkaria.postgresql.org; Mon, 10 Sep 2018 11:28:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fzKMg-0004UN-Qc for pgsql-general@arkaria.postgresql.org; Mon, 10 Sep 2018 11:28:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzKMg-0004U9-I8 for pgsql-general@lists.postgresql.org; Mon, 10 Sep 2018 11:28:22 +0000 Received: from tamriel.snowman.net ([96.255.250.162]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fzKMd-0004ve-D6 for pgsql-general@postgresql.org; Mon, 10 Sep 2018 11:28:21 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 09A565F79C; Mon, 10 Sep 2018 07:28:17 -0400 (EDT) Date: Mon, 10 Sep 2018 07:28:17 -0400 From: Stephen Frost To: Arup Rakshit Cc: pgsql-general@postgresql.org Subject: Re: Why my query not doing index only scan Message-ID: <20180910112817.GW4184@tamriel.snowman.net> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="nSY6dmU9u4MMxp1o" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --nSY6dmU9u4MMxp1o Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Greetings, * Arup Rakshit (ar@zeit.io) wrote: > I have an index defined "inspector_tool_idx4_1" UNIQUE, btree (company_id, item_code, deleted_at). Now I am using the *company_id* column in the where clause, and the selecting just the *item_code* field for all matching rows. I expected here the planner will do a index only scans. But it is doing bitmap index scan. Any idea what it is not doing what I expected it to do. One possibility is that the visibility map isn't current. Indexes don't include visibility information. The way an index-only scan works is that we track pages which are 'all visible' (meaning that every tuple on that page is visible to all running transactions) in a seperate file called the 'visibility map' (aka the VM). The VM is updated by the VACUUM process- but we only automatically run a VACUUM (with the autovacuum process) when thresholds have been reached for the number of UPDATE'd or DELETE'd tuples. What this means is that if you are playing around in development and just COPY a bunch of rows into a table, but don't ever UPDATE or DELETE the rows in that table, then you'll almost never get an index-only scan because the VM won't be current (and PG knows this). Make sure to do a VACUUM after loading data (and letting any ongoing transactions finish) and then re-test. That should make it sure that the VM is current and make it more likely that PG will do an index-only scan. Not a guarantee still, but that's the first thing I'd try, based on what you've shared here. Thanks! Stephen --nSY6dmU9u4MMxp1o Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJbllVRAAoJEO1sijiDR2RV+QgQALtxyYWnPDNtzIh7CSXy9z0a q/7mFgfAyGllnt0GWyM+uiLdHTYsyGoIgVqJwQXQbhdPQfqkpKId6msTbwV69NTe EGmBX7+Da/9zBL0nN8/zPrx6sw+PnLT1I8Tc/mYJIuHiEM6Cq2QNWsj0MVp7YSp7 6UUGSnMk9p0qrJqe25cNbbjEJfuDuUBqG0/Q779+qoiRS5l91IacQC8kOqQP4I9X emf/y2f/IxhZ78ccWe/bJWQq0UOdHhTcl8hsrNP9vzxiBiZezIxG/Ut+mVRdge3Q kuViN/yohZjMM6i1vHO8M2zYuS206fMcPbDcexeFDyIeGUHj1JaGgdb2jzr67gdv H2w6CLegDJ1NyyGwoA4bUB2B/BfDAOMzfUGx6QEgzLDuZYyNLq0Mp20wURoinfZY e/yVqHeGwYvJheNfYdckjNKLzzrcfrpmakpynG0pMRvLXMosbNdtJs3XfruNpt8e 6/+Q7T8MABpK0hU24Qcc8OfO8u3AXUJZ4dIUNvG01xsMOG0/Kjux2MKghE2Czo4H 7kd6enM+vpX6eh3ouTLA5AYVEpTZ9EkRVaewsZ/+OLQVxoF5gaoK2CuWa6wfm+Ni tcXKVgmlqcbe8zQBzSCTZ2H1ke3uThWqSaNEEEGiIOgkM6VwtbaAs3VkDUzUjD5u HG7olbx5WfwSSxHv0uZt =CGDL -----END PGP SIGNATURE----- --nSY6dmU9u4MMxp1o--