···1+-- Add sort_date computed column to documents table
2+-- This column stores the older of publishedAt (from JSON data) or indexed_at
3+-- Used for sorting feeds chronologically by when content was actually published
4+5+ALTER TABLE documents
6+ADD COLUMN sort_date timestamptz GENERATED ALWAYS AS (
7+ LEAST(
8+ COALESCE((data->>'publishedAt')::timestamptz, indexed_at),
9+ indexed_at
10+ )
11+) STORED;
12+13+-- Create index on sort_date for efficient ordering
14+CREATE INDEX documents_sort_date_idx ON documents (sort_date DESC, uri DESC);