drop table if exists m;
create table m(pk bigserial primary key, status text);
insert into m(status) values ('A');
vacuum analyze m;

drop table if exists s;
create table s(pk bigserial primary key, status text, action_at date, m_fk bigint);

insert into s(status, action_at, m_fk)
select
  (
    CASE
    WHEN series.n % 100 < 80 THEN
      (ARRAY['E', 'C'])[(series.n % 2) + 1]
    ELSE
      (ARRAY['P', 'PD', 'A'])[((random() * 3)::integer % 3) + 1]
    END
  ),
  (
    CASE
    WHEN series.n % 100 < 80 THEN
      '2018-09-07'::date + ((series.n % 365 - 365)::text || ' day')::interval
    ELSE
      '2018-09-07'::date + (((random() * 30)::integer % 30 - 4)::text || ' day')::interval
    END
  ),
  (select m.pk from m limit 1)
from generate_series(1, 500000) series(n);

create index s_pk_action_at on s(pk, action_at) where status in ('P', 'PD', 'A');
create index s_action_at_pk on s(action_at, pk) where status in ('P', 'PD', 'A');

vacuum analyze s;
