Obviously the only way to get an exact answer to this is to execute the query. But if an estimate is good enough, you can use PostgreSQL’s optimizer to get it for you.
The following simple function uses dynamic SQL and EXPLAIN
to get the execution plan for the query passed as argument and returns the row count estimate:
1
2
3
4
5
6
7
8
9
|
CREATE FUNCTION row_estimator(query text) RETURNS bigint <span style="color: #282f3a;"> </span> LANGUAGE plpgsql AS $$ DECLARE <span style="color: #282f3a;"> </span> plan jsonb; BEGIN <span style="color: #282f3a;"> </span> EXECUTE 'EXPLAIN (FORMAT JSON) ' || query INTO plan; <span style="color: #282f3a;"> </span> RETURN (plan->0-> 'Plan' ->> 'Plan Rows' ):: bigint ; END ;$$; |
Do not use this function to process untrusted SQL statements, since it is by nature vulnerable to SQL injection.