-- Wrong result with constraint_exclusion = on (not the default) when a -- partition has its own VIRTUAL generation expression. The partition -- enforces NOT NULL / CHECK with its own expression, a read through the -- parent computes the parent's, and constraint exclusion trusts the -- parent's constraints. The correct answer is 1 for each count. -- Run with: psql -X -f repro-ce-on.sql.txt -- With the v1 patch, the two CREATE TABLE ... PARTITION OF fail. \pset null '(null)' select current_setting('server_version') as server_version; create table p (a int, v int generated always as (nullif(a, 0)) virtual not null) partition by list (a); create table p1 partition of p (v generated always as (a + 1) virtual) for values in (0); insert into p values (0); create table c (a int, v int generated always as (-a) virtual check (v > 0)) partition by list (a); create table c1 partition of c (v generated always as (a) virtual) for values in (5); insert into c values (5); select a, v from p; -- v reads NULL through p set constraint_exclusion = partition; -- the default select count(*) as p_null from p where v is null; -- 1 select count(*) as c_neg from c where v < 0; -- 1 set constraint_exclusion = on; select count(*) as p_null from p where v is null; -- 0, should be 1 select count(*) as c_neg from c where v < 0; -- 0, should be 1 explain (costs off) select count(*) from p where v is null; reset constraint_exclusion;