title: Count Estimate on PostgreSQL
body: <p>I was having troubles with a count query, it was too slow. Then i found an article talking about the Count Estimate on PostgreSQL. It uses the function below to estimate the amount of rows:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION public.count_estimate(query text)
    RETURNS integer AS
  $BODY$
  DECLARE
      rec   record;
      ROWS  INTEGER;
  BEGIN
      FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
          ROWS := SUBSTRING(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
          EXIT WHEN ROWS IS NOT NULL;
      END LOOP;
  
      RETURN ROWS;
  END
  $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
  ALTER FUNCTION public.count_estimate(text)
  </code></pre>
  
  <p>And i use it like this:</p>
  
  <pre><code>SELECT count_estimate('SELECT * FROM table');
  </code></pre>
  
  <p>The query I pass to the function accepts conditions (=, >, &lt;, LIKE, SIMILAR TO ...), but when I try to use this specific conditional <code>~</code> it returns me a wrong count.</p>
  
  <p>Is there anyway to adapt this function to make it works with <code>~</code> in where clause?</p>
  
  <p>Thank you</p>