···11+with new_info as (
22+ SELECT did, coalesce(following, 0) as following, coalesce(followers, 0) as followers
33+ FROM actors
44+ LEFT JOIN (SELECT did, count(*) AS following
55+ FROM follows
66+ GROUP BY 1) fi USING (did)
77+ LEFT JOIN (SELECT subject as did, count(*) AS followers
88+ FROM follows
99+ GROUP BY 1) fe USING (did)
1010+ where actors.did = any($1)
1111+ )
1212+ merge into follow_stats fs
1313+using new_info ni
1414+on ni.did = fs.did
1515+when matched then
1616+ update
1717+ set followers=ni.followers,
1818+ following=ni.following
1919+when not matched then
2020+ insert (did, followers, following)
2121+ values (ni.did, ni.followers, ni.following);
···11+create table blocks
22+(
33+ at_uri text primary key,
44+ did text not null references actors (did),
55+ subject text not null,
66+ created_at timestamptz not null
77+);
88+99+create index blocks_did_index on blocks using hash (did);
1010+create index blocks_subject_index on blocks using hash (subject);
1111+1212+create table follows
1313+(
1414+ at_uri text primary key,
1515+ did text not null references actors (did),
1616+ subject text not null,
1717+ created_at timestamptz not null
1818+);
1919+2020+create index follow_did_index on follows using hash (did);
2121+create index follow_subject_index on follows using hash (subject);
2222+2323+create table follow_stats
2424+(
2525+ did text primary key,
2626+2727+ followers integer not null default 0,
2828+ following integer not null default 0
2929+);