September 25th, 2011 by depesz | Tags: , , , , , , | No comments »
Did it help? If yes - maybe you can help me?

On 22nd of September, Tom Lane committed patch:

Make EXPLAIN ANALYZE report the numbers of rows rejected by filter steps.
This provides information about the numbers of tuples that were visited
but not returned by table scans, as well as the numbers of join tuples
that were considered and discarded within a join plan node.
There is still some discussion going on about the best way to report counts
for outer-join situations, but I think most of what's in the patch would
not change if we revise that, so I'm going to go ahead and commit it as-is.
Documentation changes to follow (they weren't in the submitted patch
Marko Tiikkaja, reviewed by Marc Cousin, somewhat revised by Tom

How does it work?

Let's see. On 9.0, when I run a simple query, I get explain which looks like this:

$ explain analyze select * from pg_class where relname ~ 'a';
                                               QUERY PLAN
 Seq Scan on pg_class  (cost=0.00..10.16 rows=202 width=190) (actual time=0.013..0.169 rows=197 loops=1)
   Filter: (relname ~ 'a'::text)
 Total runtime: 0.200 ms
(3 rows)

Which is fine – it shows how many rows have been returned. But it doesn't show how many rows were actually scanned. Which is important, as returning 197 rows by seq scan from table of 200 rows is ok, but from table that has 1000000 rows – not so ok.

In 9.2 it looks like this:

$ explain analyze select * from pg_class where relname ~ 'a';
                                               QUERY PLAN
 Seq Scan on pg_class  (cost=0.00..11.60 rows=215 width=194) (actual time=0.027..0.388 rows=236 loops=1)
   Filter: (relname ~ 'a'::text)
   Rows Removed by Filter: 72
 Total runtime: 0.486 ms
(4 rows)

Which means that Seq Scan worked on 236 + 72 rows, and 72 were removed.

Of course in case of whole table scans, it's nearly irrelevant, since we can always check how large the table is. The thing is – this works also in a bit more complex situations:

$ explain analyze select * from test where i < 100 and i::text ~ '9';
                                                QUERY PLAN
 Index Scan using q on test  (cost=0.00..10.54 rows=91 width=4) (actual time=0.057..0.147 rows=19 loops=1)
   Index Cond: (i < 100)
   Filter: ((i)::text ~ '9'::text)
   Rows Removed by Filter: 80
 Total runtime: 0.171 ms
(5 rows)

The one thing I didn't understand was – what will be shown in case of multiple runs of given node. So:

$ explain analyze select * from generate_series(1,4) as q, test where i < 100 and i::text ~ q::text and q< 3;
                                                        QUERY PLAN
 Nested Loop  (cost=0.00..1829.17 rows=152 width=8) (actual time=0.031..0.441 rows=38 loops=1)
   Join Filter: ((test.i)::text ~ (q.q)::text)
   Rows Removed by Join Filter: 160
   ->  Index Scan using q on test  (cost=0.00..9.85 rows=91 width=4) (actual time=0.009..0.053 rows=99 loops=1)
         Index Cond: (i < 100)
   ->  Function Scan on generate_series q  (cost=0.00..12.50 rows=333 width=4) (actual time=0.000..0.001 rows=2 loops=99)
         Filter: (q < 3)
         Rows Removed by Filter: 2
 Total runtime: 0.478 ms
(9 rows)

As we see the generate_series was ran 99 times, and each time the filter removed 2 rows, so in total we had 198 rows removed from generate_series' outputs.

Sweet. Will definitely help diagnosing some explains.

Also – there was (short, but still) discussion whether the change will break – Well, clearly it doesn't :).

Leave a comment