select version();

CREATE TABLE validate_pg_repack
(
   effectivedate                                  timestamp,
   masterentityid                                 integer not null,
   primaryissueid                                 varchar(65535),
   longshortindicator                             varchar(65535),
   pg_repack_id                                   varchar(65535)
);
CREATE SEQUENCE validate_pg_repack_masterentityid_seq INCREMENT 1 START 1 OWNED BY validate_pg_repack.masterentityid;

CREATE unique INDEX idx_pg_repack_masterentityid ON validate_pg_repack USING btree (masterentityid);

CREATE INDEX idx_pg_repack_effectivedate ON validate_pg_repack USING btree (effectivedate);

INSERT INTO validate_pg_repack (effectivedate,masterentityid,primaryissueid,longshortindicator,pg_repack_id) SELECT 
now() + round(random() * 1000) * '1 second' :: interval, 
nextval('validate_pg_repack_masterentityid_seq'), 
'some-phone-' || round(random() * 65000),
'some-phone-' || round(random() * 1000),  
'some-phone-' || round(random() * 1000)
FROM
generate_series(1, 900000);

select pg_sleep(30);
select relname,n_tup_ins,n_tup_del,last_autoanalyze,autoanalyze_count from pg_stat_all_tables where relname ='validate_pg_repack';

select pg_size_pretty(pg_relation_size('validate_pg_repack'));
\di+ idx_pg_repack_*


delete from validate_pg_repack where masterentityid > 450000;
select pg_sleep(30);
select relname,n_tup_ins,n_tup_del,last_autoanalyze,autoanalyze_count from pg_stat_all_tables where relname ='validate_pg_repack';

select pg_size_pretty(pg_relation_size('validate_pg_repack'));
\di+ idx_pg_repack_*

INSERT INTO validate_pg_repack (effectivedate,masterentityid,primaryissueid,longshortindicator,pg_repack_id) SELECT 
now() + round(random() * 1000) * '1 second' :: interval, 
nextval('validate_pg_repack_masterentityid_seq'), 
'some-phone-' || round(random() * 65000),
'some-phone-' || round(random() * 1000),  
'some-phone-' || round(random() * 1000)
FROM
generate_series(450000, 900000);
select pg_sleep(120);
select relname,n_tup_ins,n_tup_del,last_autoanalyze,autoanalyze_count from pg_stat_all_tables where relname ='validate_pg_repack';

select pg_size_pretty(pg_relation_size('validate_pg_repack'));
\di+ idx_pg_repack_*

reindex table CONCURRENTLY validate_pg_repack;

select pg_size_pretty(pg_relation_size('validate_pg_repack'));
\di+ idx_pg_repack_*

drop table validate_pg_repack cascade;