create table purchaseorder_line_status (id serial primary key, abbreviation text unique);
create table purchaseorder_line (salesorder_line_id int, status_id int references purchaseorder_line_status(id));

CREATE OR REPLACE FUNCTION pol_stat(varchar) RETURNS int LANGUAGE 'sql'
IMMUTABLE STRICT SECURITY INVOKER AS 'SELECT id FROM purchaseorder_line_status
WHERE abbreviation = $1';

create index purchaseorder_line_idx01 on purchaseorder_line (salesorder_line_id) where
	status_id <> pol_stat('POL_CANCELLED') AND
	status_id <> pol_stat('POL_HANDLED');

insert into pg_autovacuum values ('purchaseorder_line'::regclass,
't', 99,99, 5, 0, -1, -1);

insert into purchaseorder_line_status (abbreviation) values ('POL_HANDLED');
insert into purchaseorder_line_status (abbreviation) values ('POL_CANCELLED');
insert into purchaseorder_line_status (abbreviation) values ('POL_STARTED');
insert into purchaseorder_line_status (abbreviation) values ('POL_PAID');
insert into purchaseorder_line_status (abbreviation) values ('POL_MADE_UP');

insert into purchaseorder_line values (1, 1);
insert into purchaseorder_line values (2, 2);
insert into purchaseorder_line values (3, 3);
insert into purchaseorder_line values (4, 4);
insert into purchaseorder_line values (5, 5);
insert into purchaseorder_line values (6, 1);
insert into purchaseorder_line values (7, 2);
insert into purchaseorder_line values (8, 3);
insert into purchaseorder_line values (9, 4);
insert into purchaseorder_line values (10, 5);
