-- The number of tables
\set NumTables 8

-- The number of partitions
\set N 256

SELECT format('CREATE TABLE testtable_%s_%s(x int, y int) PARTITION BY RANGE (x);', table_no, :N)
FROM generate_series(1, :NumTables) as table_no \gexec

SELECT format('CREATE TABLE child_%s_of_testtable_%s_%s PARTITION OF testtable_%s_%s FOR VALUES FROM (%s) TO (%s)', i, table_no, :N, table_no, :N, (i - 1) * 100, i * 100)
FROM generate_series(1, :NumTables) as table_no, generate_series(1, :N) as i \gexec

SELECT format('INSERT INTO testtable_%s_%s SELECT id, id FROM generate_series(0, %s) AS id', table_no, :N, :N * 100 - 1)
FROM generate_series(1, :NumTables) as table_no \gexec

SELECT format('ALTER TABLE testtable_%s_%s ADD PRIMARY KEY (x);', table_no, :N)
FROM generate_series(1, :NumTables) as table_no \gexec

SELECT format('ANALYZE testtable_%s_%s', table_no, :N)
FROM generate_series(1, :NumTables) as table_no \gexec
