lemmy/migrations/2023-07-11-084714_receive_activity_table/up.sql
Nutomic e9e76549a8
Split activity table into sent and received parts (fixes #3103) (#3583)
* Split activity table into sent and received parts (fixes #3103)

The received activities are only stored in order to avoid processing
the same incoming activity multiple times. For this purpose it is
completely unnecessary to store the data. So we can split the
table into sent_activity and received_activity parts, where
only sent_activity table needs to store activity data. This should
reduce storage use significantly.

Also reduces activity storage duration to three months, we can reduce
this further if necessary.

Additionally the id columns of activity tables are removed because
they are completely unused and risk overflowing (fixes #3560).

* address review

* move insert_received_activity() methods to verify handlers

* remove unnecessary conflict line

* clippy

* use on conflict, add tests
2023-07-14 11:17:06 -04:00

36 lines
1.1 KiB
SQL

-- outgoing activities, need to be stored to be later server over http
-- we change data column from jsonb to json for decreased size
-- https://stackoverflow.com/a/22910602
create table sent_activity (
id bigserial primary key,
ap_id text unique not null,
data json not null,
sensitive boolean not null,
published timestamp not null default now()
);
-- incoming activities, we only need the id to avoid processing the same activity multiple times
create table received_activity (
id bigserial primary key,
ap_id text unique not null,
published timestamp not null default now()
);
-- copy sent activities to new table. only copy last 100k for faster migration
insert into sent_activity(ap_id, data, sensitive, published)
select ap_id, data, sensitive, published
from activity
where local = true
order by id desc
limit 100000;
-- copy received activities to new table. only last 1m for faster migration
insert into received_activity(ap_id, published)
select ap_id, published
from activity
where local = false
order by id desc
limit 1000000;
drop table activity;