In progress changes and planning
================================

Simple test case of an in progress INSERT making a previously optimal
plan be seriously no optimal.

When the INSERT is complete and COMMITed and autovac has recalculated the
stats a *new* optimal plan is found, but for the intervening period the (old)
now no longer optimal plan is used.

               Table "public.plan"
 Column |            Type             | Modifiers 
--------+-----------------------------+-----------
 id     | integer                     | not null
 typ    | integer                     | not null
 dat    | timestamp without time zone | 
 val    | text                        | not null
Indexes:
    "plan_id" UNIQUE, btree (id)
    "plan_dat" btree (dat)
    "plan_typ" btree (typ)


[Session 1]
EXPLAIN ANALYZE
SELECT * FROM plan
WHERE typ = 3 AND dat IS NOT NULL;
                                                     QUERY PLAN                 
---------------------------------------------------------------------------------------------------------------------
 Index Scan using plan_dat on plan  (cost=0.00..265.47 rows=55 width=117) (actual time=0.130..4.409 rows=75 loops=1)
   Index Cond: (dat IS NOT NULL)
   Filter: (typ = 3)
   Rows Removed by Filter: 5960
 Total runtime: 4.440 ms
(5 rows)

[Session 2]

BEGIN;
INSERT INTO plan
SELECT id + 2000001,typ,current_date + id * '1 seconds'::interval ,val
FROM plan
;

[Session 1]
EXPLAIN ANALYZE
SELECT * FROM plan
WHERE typ = 3 AND dat IS NOT NULL;

                                                      QUERY PLAN                
-----------------------------------------------------------------------------------------------------------------------
 Index Scan using plan_dat on plan  (cost=0.00..551.35 rows=91 width=117) (actual time=0.131..202.699 rows=75 loops=1)
   Index Cond: (dat IS NOT NULL)
   Filter: (typ = 3)
   Rows Removed by Filter: 5960
 Total runtime: 202.729 ms
(5 rows)

[Session 2]
COMMIT;

[Session 1...wait for autoanalyze to finish then]

EXPLAIN ANALYZE
SELECT * FROM plan
WHERE typ = 3 AND dat IS NOT NULL;
                                                        QUERY PLAN             
---------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on plan  (cost=407.87..44991.95 rows=10116 width=117) (actual time=2.692..6.582 rows=75 loops=1)
   Recheck Cond: (typ = 3)
   Filter: (dat IS NOT NULL)
   Rows Removed by Filter: 19925
   ->  Bitmap Index Scan on plan_typ  (cost=0.00..405.34 rows=20346 width=0) (actual time=2.573..2.573 rows=20000 loops=1)
         Index Cond: (typ = 3)
 Total runtime: 6.615 ms

