| 1 | -- The posts of one page of a feed, planned the way our servers plan it: make plan Q=page U=42 |
| 2 | set plan_cache_mode = force_generic_plan; |
| 3 | prepare page(bigint[], bigint) as |
| 4 | select p.id, u.handle, p.body, p.created_at, p.likes_count, |
| 5 | exists (select 1 from likes where post_id = p.id and user_id = $2) |
| 6 | from posts p |
| 7 | join users u on u.id = p.user_id |
| 8 | where p.id = any($1) |
| 9 | order by p.id desc; |
| 10 | -- the ids of the page: the newest 21 posts of the people the reader follows |
| 11 | select array_agg(id) as ids from ( |
| 12 | select p.id from follows f |
| 13 | cross join lateral (select id from posts where user_id = f.followee_id order by id desc limit 21) p |
| 14 | where f.follower_id = :user order by p.id desc limit 21) page \gset |
| 15 | explain (analyze, timing off) execute page(:'ids', :user); |