title: Why index is not used in SELECT COUNT(DISTINCT)?
body: <p>This is what I'm doing in PostgreSQL 10.9 (<code>x</code> is <code>VARCHAR(100)</code>):</p>
  
  <pre><code>SELECT COUNT(DISTINCT x) FROM t
  </code></pre>
  
  <p>The table has over 1.5M records and it has an index:</p>
  
  <pre><code>CREA­TE INDE­X idx_1 ON t­ USIN­G btre­e (x)
  </code></pre>
  
  <p>The request takes over 7 seconds. This is what <code>EXPLAIN</code> says:</p>
  
  <pre><code>Aggr­egat­e (cos­t=23­675.­97..­2367­5.97­ rows­=1 widt­h=8)­
  -&gt;­; Seq Scan­ on t (cos­t=0.­00..­2293­0.97­ rows­=148­9990­ widt­h=23­)
  </code></pre>
  
  <p>What's wrong? Why index is not used?</p>