-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdd.sql
More file actions
85 lines (77 loc) · 4.02 KB
/
dd.sql
File metadata and controls
85 lines (77 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/****************************************************************
*
* PostgreSQL database objects data definition for fmsgd
*
****************************************************************/
-- database with encoding UTF8 should already be created and connected
create table if not exists msg (
id bigserial primary key,
version int not null,
pid bigint references msg (id),
no_reply boolean not null default false,
is_important boolean not null default false,
is_deflate boolean not null default false,
time_sent double precision, -- time sending host recieved message for sending, message timestamp field, NULL means message not ready for sending i.e. draft
from_addr varchar(255) not null,
add_to_from varchar(255),
topic varchar(255) not null,
type varchar(255) not null,
sha256 bytea unique,
psha256 bytea,
size int not null, -- spec allows uint32 but we don't enforced by FMSG_MAX_MSG_SIZE
filepath text not null
);
create index on msg ((lower(from_addr)));
create table if not exists msg_to (
id bigserial primary key,
msg_id bigint not null references msg (id),
addr varchar(255) not null,
time_delivered double precision, -- if sending, time sending host recieved delivery confirmation, if receiving, time successfully received message
time_last_attempt double precision, -- only used when sending, time of last delivery attempt if failed; otherwise null
response_code smallint, -- only used when sending, response code of last delivery attempt if failed; otherwise null
attempt_count int not null default 0, -- number of failed delivery attempts; used for exponential back-off
unique (msg_id, addr)
);
create index on msg_to ((lower(addr)));
create table if not exists msg_add_to (
id bigserial primary key,
msg_id bigint not null references msg (id),
addr varchar(255) not null,
time_delivered double precision, -- if sending, time sending host recieved delivery confirmation, if receiving, time successfully received message
time_last_attempt double precision, -- only used when sending, time of last delivery attempt if failed; otherwise null
response_code smallint, -- only used when sending, response code of last delivery attempt if failed; otherwise null
attempt_count int not null default 0, -- number of failed delivery attempts; used for exponential back-off
unique (msg_id, addr)
);
create index on msg_add_to ((lower(addr)));
create table if not exists msg_attachment (
msg_id bigint references msg (id),
position smallint not null default 0,
flags smallint not null default 0,
type varchar(255) not null default 'application/octet-stream',
filename varchar(255) not null,
filesize int not null,
filepath text not null,
primary key (msg_id, filename)
);
-- notify when a new msg_to row is inserted with null time_delivered so the
-- sender can pick it up immediately instead of waiting for the next poll.
create or replace function notify_msg_to_insert() returns trigger as $$
begin
if NEW.time_delivered is null then
perform pg_notify('new_msg_to', NEW.msg_id::text || ',' || NEW.addr)
from msg where id = NEW.msg_id and time_sent is not null;
end if;
return NEW;
end;
$$ language plpgsql;
drop trigger if exists trg_msg_to_insert on msg_to;
create trigger trg_msg_to_insert
after insert on msg_to
for each row execute function notify_msg_to_insert();
-- notify when a new msg_add_to row is inserted with null time_delivered so the
-- sender can pick it up immediately instead of waiting for the next poll.
drop trigger if exists trg_msg_add_to_insert on msg_add_to;
create trigger trg_msg_add_to_insert
after insert on msg_add_to
for each row execute function notify_msg_to_insert();