﻿DROP TABLE IF EXISTS cest_child1;
DROP TABLE IF EXISTS cest_child2;
DROP TABLE IF EXISTS cest;


create table cest (
  v integer, p integer
);
CREATE TABLE cest_child1 ( CHECK(p = 1) ) INHERITS (cest);

INSERT INTO cest_child1 
(
  SELECT round((random()*499)) AS v, 1 AS p FROM generate_series(1,10000)
);

CREATE TABLE cest_child2 ( CHECK(p = 2)) INHERITS (cest) ;
INSERT INTO cest_child2
(
  SELECT round((random()*499)+1000) AS v, 2 AS p FROM generate_series(1,10000)
);

ANALYZE cest_child1;
ANALYZE cest_child2;
ANALYZE cest;

-- Correctly guessing 500 different values for the child1 table
EXPLAIN ANALYZE SELECT DISTINCT v FROM cest_child1 WHERE p = 1;

-- Assuming 1000 different values even if the select effictvly only uses the single child1 table (and the empty child table)
EXPLAIN ANALYZE SELECT DISTINCT v FROM cest WHERE p = 1;
