title: Postgres positive, integer, non-null estimate for the number of distinct non-null values
body: <p>I am asked to write a query that uses pg stats to return:</p>
  <ul>
  <li>the table name, as tablename;</li>
  <li>the attribute name, as attname;</li>
  <li>the number of distinct non-null values that Postgres estimates that this attribute has, as a distval attribute;</li>
  <li>and some histogram data.</li>
  </ul>
  <p>However, this number distval should be positive integer, while my query outputs negative numbers (ex. -0.86514354). What am I doing wrong here?</p>
  <p>The query is:</p>
  <pre><code>SELECT tablename,
         attname,
         n_distinct as distval,
         (histogram_bounds::text::varchar[])[1] as min,
         (histogram_bounds::text::varchar[])[array_upper(histogram_bounds::text::varchar[],1)] as max
  
  FROM information_schema.columns,pg_stats 
  WHERE tablename = table_name
    AND attname = column_name
    AND (NOT (information_schema.columns.table_schema LIKE 'pg_%'
              OR information_schema.columns.table_schema = 'information_schema'))
    AND (information_schema.columns.data_type ='character varying')
    AND (table_name NOT in (SELECT viewname FROM pg_views))
  LIMIT 5;
  </code></pre>