Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h6ofh-0006yb-Nj for pgsql-performance@arkaria.postgresql.org; Thu, 21 Mar 2019 03:47:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h6off-0001xS-BD for pgsql-performance@arkaria.postgresql.org; Thu, 21 Mar 2019 03:47:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h6ofe-0001wp-Nl for pgsql-performance@lists.postgresql.org; Thu, 21 Mar 2019 03:47:10 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h6ofZ-0001GE-2Q for pgsql-performance@postgresql.org; Thu, 21 Mar 2019 03:47:09 +0000 Received: from [192.168.127.1] (port=16869 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1h6ofU-0005L2-JE; Thu, 21 Mar 2019 03:47:00 +0000 Received: from [127.0.0.1] (port=41105 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1h6ofT-0003C8-Tm; Thu, 21 Mar 2019 03:47:00 +0000 From: Andrew Gierth To: Gunther Cc: "pgsql-performance." Subject: Re: Poor man's partitioned index .... not being used? In-Reply-To: (Gunther's message of "Wed, 20 Mar 2019 22:45:07 -0400") Message-ID: <87tvfwx5x0.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Thu, 21 Mar 2019 03:46:58 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Gunther" == Gunther writes: Gunther> foo=# CREATE UNIQUE INDEX Test_pk0 ON Test(id) WHERE mod(id,2) = 0; Gunther> CREATE INDEX Gunther> foo=# EXPLAIN SELECT * FROM Test WHERE id = '8934'; Gunther> QUERY PLAN Gunther> ------------------------------------------------------- Gunther> Seq Scan on test (cost=0.00..6569.61 rows=1 width=4) Gunther> Filter: (id = 8934) Gunther> (2 rows) Gunther> Why is that index never used? Because the expression mod(id,2) does not appear in the query, and there is no logic in the implication prover to prove that (mod(id,2) = 0) is implied by (id = 8934). If you did WHERE mod(id,2) = mod(8934,2) AND id = 8934 then the index would likely be used - because the prover can then treat mod(id,2) as an atom (call it X), constant-fold mod(8934,2) to 0 because mod() is immutable, and then observe that (X = 0) proves that (X = 0). Pretty much the only simple implications that the prover can currently deduce are: - identical immutable subexpressions are equivalent - strict operator expressions imply scalar IS NOT NULL - (A op1 B) implies (B op2 A) if op2 is op1's declared commutator - Btree semantics: if <, <=, =, >=, > are all members of a btree opfamily, and <> is the declared negator of =, then implications like (X < A) and (A <= B) implies (X < B) can be deduced. -- Andrew (irc:RhodiumToad)