Skip to content

Instantly share code, notes, and snippets.

@silvioq
Created November 20, 2009 09:31
Show Gist options
  • Save silvioq/239380 to your computer and use it in GitHub Desktop.
Save silvioq/239380 to your computer and use it in GitHub Desktop.
-- Source: http://archives.postgresql.org/pgsql-sql/2005-08/msg00046.php
-- Thanks: http://archives.postgresql.org/pgsql-es-ayuda/2008-02/msg00293.php
CREATE FUNCTION count_estimate(query text) RETURNS integer AS $$
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;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment