CREATE SCHEMA oschm;
CREATE TABLE oschm.res1 AS SELECT
  ST_AddBand(
    ST_MakeEmptyRaster(10, 10, x, y, 1, -1, 0, 0, 0)
    , 1, '8BUI', 0, 0
  ) r
FROM generate_series(-170, 160, 10) x,
     generate_series(80, -70, -10) y;
SELECT addrasterconstraints('oschm'::name,'res1'::name, 'r'::name);

SELECT ST_CreateOverview('oschm.res1', 'r', 2)::text = 'oschm.o_2_res1';

SELECT ST_CreateOverview('oschm.res1', 'r', 4)::text = 'oschm.o_4_res1';

SELECT ST_CreateOverview('oschm.res1', 'r', 8)::text = 'oschm.o_8_res1';

SELECT ST_CreateOverview('oschm.res1', 'r', 16)::text = 'oschm.o_16_res1';

SELECT r_table_schema, r_table_name tab, r_raster_column c, 
 srid s, scale_x sx, scale_y sy,
 blocksize_x w, blocksize_y h, same_alignment a,
 -- regular_blocking (why not regular?)
 --extent::box2d e,
 st_covers(extent::box2d, 'BOX(-170 -80,170 80)'::box2d) ec,
 st_xmin(extent::box2d) = -170 as eix,
 st_ymax(extent::box2d) = 80 as eiy,
 (st_xmax(extent::box2d) - 170) <= scale_x as eox,
 --(st_xmax(extent::box2d) - 170) eoxd,
 abs(st_ymin(extent::box2d) + 80) <= abs(scale_y) as eoy
 --,abs(st_ymin(extent::box2d) + 80) eoyd
 FROM raster_columns
WHERE r_table_name like '%res1'
AND r_table_schema = 'oschm'
ORDER BY scale_x, r_table_name;

SELECT o_table_schema, o_table_name, o_raster_column,
       r_table_schema, r_table_name, r_raster_column, overview_factor
FROM raster_overviews
WHERE r_table_name = 'res1'
AND   r_table_schema = 'oschm'
ORDER BY overview_factor;

SELECT 'count',
(SELECT count(*) r1 from oschm.res1),
(SELECT count(*) r2 from oschm.o_2_res1),
(SELECT count(*) r4 from oschm.o_4_res1),
(SELECT count(*) r8 from oschm.o_8_res1),
(SELECT count(*) r16 from oschm.o_16_res1)
;


DROP TABLE oschm.o_16_res1;
DROP TABLE oschm.o_8_res1;
DROP TABLE oschm.o_4_res1;
DROP TABLE oschm.o_2_res1;
DROP TABLE oschm.res1;
DROP SCHEMA oschm;
