Good Code
The good version expresses the month as a half-open range, which keeps an index on submitted_at useful.
Lesson 06
Keep indexed columns searchable by avoiding unnecessary functions on the column side.
SELECT id, title, submitted_at
FROM code_reviews
WHERE submitted_at >= TIMESTAMPTZ '2026-01-01'
AND submitted_at < TIMESTAMPTZ '2026-02-01'
ORDER BY submitted_at DESC;SELECT id, title, submitted_at
FROM code_reviews
WHERE TO_CHAR(submitted_at, 'YYYY-MM') = '2026-01'
ORDER BY submitted_at DESC;The good version expresses the month as a half-open range, which keeps an index on submitted_at useful.
The bad version wraps the column in a formatting function for every row. It is readable, but it usually blocks normal index usage.