Skip to content

Instantly share code, notes, and snippets.

@sashka
Created January 18, 2019 12:05
Show Gist options
  • Save sashka/a1a770e3aab4c35a6f2f89de78594ba3 to your computer and use it in GitHub Desktop.
Save sashka/a1a770e3aab4c35a6f2f89de78594ba3 to your computer and use it in GitHub Desktop.
CREATE FUNCTION count_estimate(query text) RETURNS INTEGER AS
$func$
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
$func$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment