powa-team / pg_qualstats

A PostgreSQL extension for collecting statistics about predicates, helping find what indices are missing
Other
274 stars 26 forks source link

Segfault when qual on view using expressions #5

Closed rjuju closed 8 years ago

rjuju commented 8 years ago

Found with sqlsmith. Minimal test case to reproduce:

create table bug1 (id1 integer, val1 integer);
create table bug2 (id2 integer, val2 integer);
create view vbug as select id1, int4smaller(val1, val2) val12 from bug1 join bug2 on id1 = id2;
create table bug3 (id3 integer);
select * from vbug join bug3 on id3 = val12;

At least, pgqs_resolve_var() doesn't handle get_tle_by_resno() returning NULL.

rjuju commented 8 years ago

After fixing this problem, it looks like there's also an infinite recursion in https://github.com/dalibo/pg_qualstats/blob/master/pg_qualstats.c#L1734 which isn't triggered by this simplified test case.

The original sqlsmith query raising both issue (on regression database) is:

select                  
  ref_33.nslots as c0
from 
  (select  
          74 as c0
        from 
          pg_catalog.pg_ts_parser as sample_0 tablesample system (6.4) 
        where 96 = 78
        limit 174) as subq_0
    left join regress_rls_schema.rls_tbl_force as ref_31
            inner join public.credit_usage as sample_28 tablesample system (5.3) 
            on (ref_31.c1 = sample_28.cid )
          inner join public.shoe_ready as ref_32
            right join public.hub as ref_33
            on (ref_32.total_avail = ref_33.nslots )
          on (sample_28.usage = ref_32.sh_avail )
        left join public.check_tbl as ref_34
          inner join pg_catalog.pg_class as sample_29 tablesample system (8.5) 
          on (ref_34.x = sample_29.relpages )
        on (ref_32.total_avail = ref_34.x )
      left join (select  
              ref_35.stuff as c0
            from 
              public.thethings as ref_35
            where public.max_xacttest() is not NULL
            limit 72) as subq_15
        inner join pg_catalog.pg_ts_config_map as sample_30 tablesample bernoulli (7.3) 
        on (subq_15.c0 = sample_30.maptokentype )
      on (ref_32.sl_avail = sample_30.maptokentype )
    on (subq_0.c0 = ref_34.x )
where ref_31.c1 >= sample_28.usage
limit 8;
rdunklau commented 8 years ago

This should be fixed by https://github.com/dalibo/pg_qualstats/commit/e0ddf700f4e3f7e3da0320c62a7d89c2378798fb