···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
45--- Note: We use ::timestamp AT TIME ZONE 'UTC' to make the expression immutable
6--- (direct ::timestamptz cast is not immutable as it depends on session timezone)
0000000000007ALTER TABLE documents
8ADD COLUMN sort_date timestamptz GENERATED ALWAYS AS (
9 LEAST(
10- COALESCE((data->>'publishedAt')::timestamp AT TIME ZONE 'UTC', indexed_at),
11 indexed_at
12 )
13) STORED;
···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
45+-- Create an immutable function to parse ISO 8601 timestamps from text
6+-- This is needed because direct ::timestamp cast is not immutable (accepts 'now', 'today', etc.)
7+-- The regex validates the format before casting to ensure immutability
8+CREATE OR REPLACE FUNCTION parse_iso_timestamp(text) RETURNS timestamptz
9+LANGUAGE sql IMMUTABLE STRICT AS $$
10+ SELECT CASE
11+ -- Match ISO 8601 format: YYYY-MM-DDTHH:MM:SS with optional fractional seconds and Z/timezone
12+ WHEN $1 ~ '^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})?$' THEN
13+ $1::timestamptz
14+ ELSE
15+ NULL
16+ END
17+$$;
18+19ALTER TABLE documents
20ADD COLUMN sort_date timestamptz GENERATED ALWAYS AS (
21 LEAST(
22+ COALESCE(parse_iso_timestamp(data->>'publishedAt'), indexed_at),
23 indexed_at
24 )
25) STORED;