title: PostgreSQL constraint exclusion not working with subquery SELECT IN
body: <p>When using partitioning in PostgreSQL, where <em>master</em> is the master partition table </p>
  
  <pre><code>CREATE TABLE master
  (
    _id numeric,
    name character varying
  );
  </code></pre>
  
  <p>and having two sub tables  </p>
  
  <p>partition_1</p>
  
  <pre><code>CREATE TABLE partition_1
  (
    -- _id numeric,
    -- name character varying,
    CONSTRAINT partition_1_check CHECK (_id &lt; 1000)
  )    
  INHERITS (master);
  CREATE INDEX partition_1_id_idx
    ON partition_1
    USING btree
    (_id);
  </code></pre>
  
  <p>partition_2</p>
  
  <pre><code>CREATE TABLE partition_2
  (
    -- _id numeric,
    -- name character varying,
    CONSTRAINT partition_2_check CHECK (_id &gt;= 1000)
  )    
  INHERITS (master);
  CREATE INDEX partition_2_id_idx
    ON partition_2
    USING btree
    (_id);
  </code></pre>
  
  <p>and a table for some ids (1,3) in this example</p>
  
  <pre><code>CREATE TABLE _ids
  (
  _id numeric NOT NULL,
  CONSTRAINT ids_pkey PRIMARY KEY (_id)
  )
  </code></pre>
  
  <p>the statement </p>
  
  <pre><code>EXPLAIN SELECT * FROM master WHERE _id IN (SELECT * FROM _ids)
  </code></pre>
  
  <p>yields a seq scan of both partitions regardless whether _ids contains elements from partition_1/2 or not.</p>
  
  <pre><code>Hash Semi Join  (cost=39.48..141.14 rows=2621 width=14)
    Hash Cond: (master._id = _ids._id)
    -&gt;  Append  (cost=0.00..62.00 rows=4001 width=14)
          -&gt;  Seq Scan on master  (cost=0.00..0.00 rows=1 width=14)
          -&gt;  Seq Scan on partition_1  (cost=0.00..30.98 rows=1998 width=13)
          -&gt;  Seq Scan on partition_2  (cost=0.00..31.02 rows=2002 width=15)
    -&gt;  Hash  (cost=23.10..23.10 rows=1310 width=32)
          -&gt;  Seq Scan on _ids  (cost=0.00..23.10 rows=1310 width=32)
  </code></pre>
  
  <p>If I instead use something like</p>
  
  <pre><code>SELECT * FROM master WHERE _id IN (1,3) 
  </code></pre>
  
  <p>I get the desired result:</p>
  
  <pre><code>Append  (cost=0.00..17.40 rows=5 width=13)
    -&gt;  Seq Scan on master  (cost=0.00..0.00 rows=1 width=14)
          Filter: (_id = ANY ('{1,3}'::numeric[]))
    -&gt;  Bitmap Heap Scan on partition_1  (cost=8.59..17.40 rows=4 width=13)
          Recheck Cond: (_id = ANY ('{1,3}'::numeric[]))
          -&gt;  Bitmap Index Scan on partition_1_id_idx  (cost=0.00..8.58 rows=4 width=0)
                Index Cond: (_id = ANY ('{1,3}'::numeric[]))
  </code></pre>
  
  <p>How do I get PostgreSQL to use constraint exclusion correctly ?</p>
  
  <p><strong>Note: I have constraint_exclusion set to partition</strong> </p>