sraoss / pgsql-ivm

IVM (Incremental View Maintenance) development for PostgreSQL
Other
129 stars 12 forks source link

Views containing NULL value can not be updated correctly #31

Closed yugo-n closed 4 years ago

yugo-n commented 5 years ago
=# create table t (i int, v int);
CREATE TABLE
=# insert into t values (1,10),(2, NULL);
INSERT 0 2
=# create incremental materialized view mv as select * from t;
SELECT 2
=# select * from mv;
 i | v  
---+----
 2 |   
 1 | 10
(2 rows)

=# update t set v = 20 where i = 2;
UPDATE 1
=# select * from mv;
 i | v  
---+----
 2 |   
 1 | 10
 2 | 20
(3 rows)
tatsuo-ishii commented 4 years ago

Upcoming OUTER JOIN support will remove this limitation.

yugo-n commented 4 years ago

Yes. It is because OUTER JOIN can generate tuple including NULL and IVM have to handle this correctly.

I will also add this test case to the regression test when making the patch for outer-join.

yugo-n commented 4 years ago

I found remaining problem with aggregate views. I will fix this.

=# create table t (i int, v int);
CREATE TABLE
=# insert into t values (null, 1), (null, 2);
INSERT 0 2
=# create incremental materialized view mv as select i, sum(v) from t group by i;
SELECT 1
=# select * from mv;
 i | sum 
---+-----
   |   3
(1 row)

=# update t set v = v*10;
UPDATE 2
=# select * from mv;
 i | sum 
---+-----
   |   3
   |  30
(2 rows)
yugo-n commented 4 years ago

During fixing this, I found another issue in IMMVs without aggregate.

null=# \d x
                 Table "public.x"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 i      | integer |           |          | 
Indexes:
    "x_i_idx" btree (i)

null=# \d+ v
                                  Materialized view "public.v"
    Column     |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
---------------+---------+-----------+----------+---------+---------+--------------+-------------
 i             | integer |           |          |         | plain   |              | 
 __ivm_count__ | bigint  |           |          |         | plain   |              | 
Indexes:
    "v_i_idx" btree (i)
View definition:
 SELECT x.i
   FROM x;
Access method: heap
Incremental view maintenance: yes

null=# insert into x values (1),(null);
INSERT 0 2
null=# select * from v;
 i 
---
(0 rows)

This is due to NOT IN behaviour involving NULL value. I'll fix this, too.

yugo-n commented 4 years ago

This is fixed by the above commit.