Fix get_latest_posts_per_board query

- put back order by clause (need that!)
- use left joins which still return rows if there are no threads or posts on
  the board yet.
- move is_missing_attachments flag into join condition (thanks deepseek)
This commit is contained in:
towards-a-new-leftypol 2025-02-14 10:11:24 -05:00
parent f4652eecbb
commit 4dd29a72b6
2 changed files with 9 additions and 8 deletions

View File

@ -282,15 +282,15 @@ SELECT DISTINCT ON (b.board_id)
p.post_id,
p.board_post_id,
p.creation_time,
p.body,
t.thread_id,
t.board_thread_id
FROM boards b
LEFT JOIN threads t ON t.board_id = b.board_id
LEFT JOIN posts p ON p.thread_id = t.thread_id AND p.is_missing_attachments = false
ORDER BY b.board_id, p.creation_time DESC NULLS LAST;
ORDER BY b.board_id, p.creation_time DESC;
-- for Sync
CREATE OR REPLACE FUNCTION get_latest_posts_per_board()
RETURNS TABLE (
board_id int,
@ -312,9 +312,9 @@ RETURNS TABLE (
t.thread_id,
t.board_thread_id
FROM boards b
JOIN threads t ON t.board_id = b.board_id
JOIN posts p ON p.thread_id = t.thread_id
WHERE p.is_missing_attachments = false;
LEFT JOIN threads t ON t.board_id = b.board_id
LEFT JOIN posts p ON p.thread_id = t.thread_id AND p.is_missing_attachments = false
ORDER BY b.board_id, p.creation_time DESC;
$$ LANGUAGE sql STABLE;
SELECT * FROM get_latest_posts_per_board();

View File

@ -402,6 +402,7 @@ RETURNS SETOF catalog_grid_result AS $$
$$ LANGUAGE sql STABLE;
-- for Sync
CREATE OR REPLACE FUNCTION get_latest_posts_per_board()
RETURNS TABLE (
board_id int,
@ -423,9 +424,9 @@ RETURNS TABLE (
t.thread_id,
t.board_thread_id
FROM boards b
JOIN threads t ON t.board_id = b.board_id
JOIN posts p ON p.thread_id = t.thread_id
WHERE p.is_missing_attachments = false;
LEFT JOIN threads t ON t.board_id = b.board_id
LEFT JOIN posts p ON p.thread_id = t.thread_id AND p.is_missing_attachments = false
ORDER BY b.board_id, p.creation_time DESC;
$$ LANGUAGE sql STABLE;