Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZLEB2-0000PG-UC for pgsql-bugs@arkaria.postgresql.org; Fri, 31 Jul 2015 17:33:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZLEB1-0001LV-VJ for pgsql-bugs@arkaria.postgresql.org; Fri, 31 Jul 2015 17:33:00 +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.84) (envelope-from ) id 1ZLEAO-0008ML-Dr for pgsql-bugs@postgresql.org; Fri, 31 Jul 2015 17:32:20 +0000 Received: from cl-1892.mbx-01.si.sixxs.net ([2001:15c0:65ff:763::2] helo=zeno.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZLEAL-0008HG-4V for pgsql-bugs@postgresql.org; Fri, 31 Jul 2015 17:32:19 +0000 Received: by zeno.hjp.at (Postfix, from userid 1000) id 3BBDC4011; Fri, 31 Jul 2015 19:32:14 +0200 (CEST) Date: Fri, 31 Jul 2015 19:32:14 +0200 From: "Peter J. Holzer" To: pgsql-bugs@postgresql.org Subject: 9.5alpha1: Partial index not used Message-ID: <20150731173213.GB2423@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="/WwmFnJnmDyWGHa4" Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --/WwmFnJnmDyWGHa4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Consider this table: wdsold=3D> \d concept Table "public.concept" Column | Type | Mod= ifiers =20 -------------------+-----------------------------+-------------------------= ----------------------------- id | integer | not null default nextval= ('concept_id_seq'::regclass) canonicalname | character varying |=20 description | character varying |=20 start | boolean |=20 hidden | boolean |=20 sortorder | integer |=20 valid_from | timestamp without time zone | not null default now() from_job_queue_id | integer |=20 Indexes: "concept_pkey" PRIMARY KEY, btree (id) "concept_canonicalname_idx" btree (canonicalname) "concept_start_idx" btree (start) WHERE start IS NOT NULL Referenced by: TABLE "facttablemetadata" CONSTRAINT "facttablemetadata_dimension_fkey"= FOREIGN KEY (dimension) REFERENCES concept(id) TABLE "facttablemetadata" CONSTRAINT "facttablemetadata_member_fkey" FO= REIGN KEY (member) REFERENCES concept(id) TABLE "relation" CONSTRAINT "relation_child_fkey" FOREIGN KEY (child) R= EFERENCES concept(id) DEFERRABLE TABLE "relation" CONSTRAINT "relation_parent_fkey" FOREIGN KEY (parent)= REFERENCES concept(id) DEFERRABLE TABLE "term" CONSTRAINT "term_concept_id_fkey" FOREIGN KEY (concept_id)= REFERENCES concept(id) DEFERRABLE wdsold=3D> select start, count(*) from concept group by start order by star= t; start | count =20 -------+--------- t | 3 | 3431866 (2 rows) and this query: select id as IdValue, canonicalname as Description, null as IsLeaf from pub= lic.concept where start; Clearly this should be able to use the partial index (if start is true it is also not null) and since there are only 3 out of 3 million rows in result it would also be beneficial (and PostgreSQL 9.1 did use the index). However, it PostgreSQL 9.5 doesn't use it: wdsold=3D> explain analyze select id as IdValue, canonicalname as Descripti= on, null as IsLeaf from public.concept where start; QUERY PLAN = =20 ---------------------------------------------------------------------------= ----------------------------- Seq Scan on concept (cost=3D0.00..81659.03 rows=3D1 width=3D27) (actual t= ime=3D0.026..955.889 rows=3D3 loops=3D1) Filter: start Rows Removed by Filter: 3431866 Planning time: 0.193 ms Execution time: 955.926 ms (5 rows) Even if I try to force it: wdsold=3D> set enable_seqscan to off; SET Time: 0.540 ms wdsold=3D> explain analyze select id as IdValue, canonicalname as Descripti= on, null as IsLeaf from public.concept where start; QUERY PLAN = =20 ---------------------------------------------------------------------------= --------------------------------------------- Seq Scan on concept (cost=3D10000000000.00..10000099078.69 rows=3D1 width= =3D27) (actual time=3D0.014..948.738 rows=3D3 loops=3D1) Filter: start Rows Removed by Filter: 3431866 Planning time: 0.060 ms Execution time: 948.777 ms (5 rows) So it obviously thinks that it can't use the index. However, if I create a full index: wdsold=3D> create index on concept(start); CREATE INDEX Time: 5899.635 ms wdsold=3D> explain analyze select id as IdValue, canonicalname as Descripti= on, null as IsLeaf from public.concept where start; QUERY PLAN = =20 ---------------------------------------------------------------------------= ------------------------------------------------- Index Scan using concept_start_idx on concept (cost=3D0.43..3.05 rows=3D1= width=3D27) (actual time=3D0.501..0.535 rows=3D3 loops=3D1) Index Cond: (start =3D true) Filter: start Planning time: 0.731 ms Execution time: 0.577 ms (5 rows) it is used, and also if I create a partial index just on true values: wdsold=3D> create index on concept(start) where start; CREATE INDEX Time: 937.267 ms wdsold=3D> explain analyze select id as IdValue, canonicalname as Descripti= on, null as IsLeaf from public.concept where start; QUERY PLAN = =20 ---------------------------------------------------------------------------= ------------------------------------------------- Index Scan using concept_start_idx on concept (cost=3D0.13..2.75 rows=3D1= width=3D27) (actual time=3D0.028..0.033 rows=3D3 loops=3D1) Index Cond: (start =3D true) Planning time: 0.499 ms Execution time: 0.073 ms (4 rows) it is also used.=20 So I think the problem is that PostgreSQL 9.5alpha1 doesn't consider true to be a subset of the non-null values for the purpose of index selection. hp --=20 _ | Peter J. Holzer | I want to forget all about both belts and |_|_) | | suspenders; instead, I want to buy pants=20 | | | hjp@hjp.at | that actually fit. __/ | http://www.hjp.at/ | -- http://noncombatant.org/ --/WwmFnJnmDyWGHa4 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIVAwUBVbuxHPIOSFES/ihdAQIbDQ/+MsXqpbmalsy3bJfLrIDKRhQ7nmbcDpdM Yh/Vdlkzv/e4PPPzs77LYb2UEQCh6NfQX3sIcSl7b29bobXVU8Y/2YdZJ5jfyQdF EtlM5MvZsgUeT8LzYqGIecWxKhCJfLeFB8Qetcs/Z9/uVL3rNdF9nNCP8NXD6EfF lkY1jC+Ur70EegdlP73Spk81RC2dLwyQfjFn/mMEcOmVHrWzNLiFQB0j/RSAO6u6 yUIX4sAs9afBbId0x/nVrzUfDHfdRFq/U+WOBp544I7+/v0CZx0sUWcRD/QOUVZz 1qI7JXTJmJA00ZwBfxWJoNcsCyUFJb0LnDKmjeo0cDPQQ/sRQNg2AGOb4Yr7S4xN DmcpLekb2ltwiFweUjf13CSlCODTjeG4X3B6fVXbaGg7v2tfyqBdWU27PMmWi3T2 AvrOzgPdB/Rt2hNqRMW0ZhaKlpQW3RKrY9JunhmNGfS/qpMw82tW1Z87nim/X05/ RzqmVkFOKWZds6MlRGk9ka3869CjMLvtKYhSkFTJh3gtBBGbU2gs/zfCB9wMe1OB j0z2WaI8ieARYI8fYA7exmfS8NT7/fKNOT5hWU79bohiWbFKo5BYA3+A9fHy8+3K muX8XLGhu+BrW/b02wJ0+BaSEvO7qe2cAfUMEW06vO3sTxeo76V4/caO3ucIzLQB bEN7lBgKhqI= =qMhd -----END PGP SIGNATURE----- --/WwmFnJnmDyWGHa4--