Thread: materialized views and FDWs

materialized views and FDWs

From
Kevin Grittner
Date:
In final testing and documentation today, it occurred to me to test
a materialized view with foreign data wrapper.  I picked the
file_fdw for convenience, but I think this should work as well with
any other FDW.  The idea is to create an MV which mirrors an FDW so
that it can be indexed and quickly accessed.  Timings below are all
fully cached to minimize caching effects.

test=# create extension file_fdw;
CREATE EXTENSION
test=# create server local_file foreign data wrapper file_fdw ;
CREATE SERVER
test=# create foreign table words (word text not null) server local_file options (filename
'/etc/dictionaries-common/words');
CREATE FOREIGN TABLE
test=# create materialized view wrd as select * from words;
SELECT 99171
test=# create unique index wrd_word on wrd (word);
CREATE INDEX
test=# create extension pg_trgm ;
CREATE EXTENSION
test=# create index wrd_trgm on wrd using gist (word gist_trgm_ops);
CREATE INDEX
test=# vacuum analyze wrd;
VACUUM
test=# select word from wrd order by word <-> 'caterpiler' limit 10;
     word     
---------------
 cater
 caterpillar
 Caterpillar
 caterpillars
 caterpillar's
 Caterpillar's
 caterer
 caterer's
 caters
 catered
(10 rows)

test=# explain analyze select word from words order by word <-> 'caterpiler' limit 10;
                                                         QUERY
PLAN                                                         

-----------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=2195.70..2195.72 rows=10 width=32) (actual time=218.904..218.906 rows=10 loops=1)
   ->  Sort  (cost=2195.70..2237.61 rows=16765 width=32) (actual time=218.902..218.904 rows=10 loops=1)
         Sort Key: ((word <-> 'caterpiler'::text))
         Sort Method: top-N heapsort  Memory: 25kB
         ->  Foreign Scan on words  (cost=0.00..1833.41 rows=16765 width=32) (actual time=0.046..200.965 rows=99171
loops=1)
               Foreign File: /etc/dictionaries-common/words
               Foreign File Size: 938848
 Total runtime: 218.966 ms
(8 rows)

test=# set enable_indexscan = off;
test=# explain analyze select word from wrd order by word <-> 'caterpiler' limit 10;
                                                      QUERY PLAN                                                     
----------------------------------------------------------------------------------------------------------------------
 Limit  (cost=3883.69..3883.71 rows=10 width=9) (actual time=203.819..203.821 rows=10 loops=1)
   ->  Sort  (cost=3883.69..4131.61 rows=99171 width=9) (actual time=203.818..203.818 rows=10 loops=1)
         Sort Key: ((word <-> 'caterpiler'::text))
         Sort Method: top-N heapsort  Memory: 25kB
         ->  Seq Scan on wrd  (cost=0.00..1740.64 rows=99171 width=9) (actual time=0.029..186.749 rows=99171 loops=1)
 Total runtime: 203.851 ms
(6 rows)

test=# reset enable_indexscan;
test=# explain analyze select word from wrd order by word <-> 'caterpiler' limit 10;
                                                          QUERY
PLAN                                                         

------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.28..1.02 rows=10 width=9) (actual time=24.916..25.079 rows=10 loops=1)
   ->  Index Scan using wrd_trgm on wrd  (cost=0.28..7383.70 rows=99171 width=9) (actual time=24.914..25.076 rows=10
loops=1)
         Order By: (word <-> 'caterpiler'::text)
 Total runtime: 25.884 ms
(4 rows)

Does this deserve specific treatment in the docs?  Where?

--
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



Re: materialized views and FDWs

From
Kevin Grittner
Date:
Kevin Grittner <kgrittn@ymail.com> wrote:

> test=# explain analyze select word from words order by word <-> 'caterpiler' limit 10;
> Foreign Scan on words
>  Total runtime: 218.966 ms

> test=# explain analyze select word from wrd order by word <-> 'caterpiler' limit 10;
> Index Scan using wrd_trgm on wrd
>  Total runtime: 25.884 ms

I forgot to put the initial check for a valid word, where the
difference is much larger:

test=# explain analyze select count(*) from words where word = 'caterpiler';
                                                   QUERY PLAN                                                  
----------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=4125.19..4125.20 rows=1 width=0) (actual time=26.013..26.014 rows=1 loops=1)
   ->  Foreign Scan on words  (cost=0.00..4124.70 rows=196 width=0) (actual time=26.011..26.011 rows=0 loops=1)
         Filter: (word = 'caterpiler'::text)
         Rows Removed by Filter: 99171
         Foreign File: /etc/dictionaries-common/words
         Foreign File Size: 938848
 Total runtime: 26.081 ms
(7 rows)

test=# explain analyze select count(*) from wrd where word = 'caterpiler';
                                                       QUERY
PLAN                                                        

-------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=4.44..4.45 rows=1 width=0) (actual time=0.074..0.074 rows=1 loops=1)
   ->  Index Only Scan using wrd_word on wrd  (cost=0.42..4.44 rows=1 width=0) (actual time=0.071..0.071 rows=0
loops=1)
         Index Cond: (word = 'caterpiler'::text)
         Heap Fetches: 0
 Total runtime: 0.119 ms
(5 rows)


The question remains the same, though ... document this usage?

 
--
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company




Re: materialized views and FDWs

From
Heikki Linnakangas
Date:
On 03.03.2013 20:06, Kevin Grittner wrote:
> The question remains the same, though ... document this usage?

Seems like a good thing to put in an example somewhere.

- Heikki