···11+-- Add sort_date computed column to documents table
22+-- This column stores the older of publishedAt (from JSON data) or indexed_at
33+-- Used for sorting feeds chronologically by when content was actually published
44+55+ALTER TABLE documents
66+ADD COLUMN sort_date timestamptz GENERATED ALWAYS AS (
77+ LEAST(
88+ COALESCE((data->>'publishedAt')::timestamptz, indexed_at),
99+ indexed_at
1010+ )
1111+) STORED;
1212+1313+-- Create index on sort_date for efficient ordering
1414+CREATE INDEX documents_sort_date_idx ON documents (sort_date DESC, uri DESC);