DROP TABLE greylist;
DROP TABLE greylist_log;

CREATE TABLE greylist (
    id			SERIAL		PRIMARY KEY,
    relay_ip		CIDR		NOT NULL,
    bounce		BOOLEAN		NOT NULL,
    sender		VARCHAR(150)	NOT NULL,
    recipient		VARCHAR(150)	NOT NULL,
    block_expires	TIMESTAMP,
    record_expires	TIMESTAMP,
    create_time		TIMESTAMP	NOT NULL DEFAULT now(),
    manual		BOOLEAN		NOT NULL DEFAULT FALSE,
    passcount		BIGINT		NOT NULL DEFAULT 0,
    last_pass		TIMESTAMP,
    blockcount		BIGINT		NOT NULL DEFAULT 0,
    last_block		TIMESTAMP
);

CREATE INDEX greylist_relay_ip ON greylist(relay_ip);
CREATE INDEX greylist_sender ON greylist(sender);
CREATE INDEX greylist_recipient ON greylist(recipient);
CREATE INDEX greylist_record_expires ON greylist(record_expires);

CREATE TABLE greylist_log (
    relay_ip		INET		NOT NULL,
    sender		VARCHAR(150)	NOT NULL,
    recipient		VARCHAR(150)	NOT NULL,
    deferred		BOOLEAN		NOT NULL,
    date		TIMESTAMP	NOT NULL DEFAULT now(),
    id			INTEGER
);

CREATE INDEX greylist_log_date ON greylist_log(date);
