Generating a table of calendar information in SQL can be done with little effort using two trivial tables -- no for-loops or iterators required. This talk describes the PostgreSQL interval and daterange types used for a calendar along with SQL to generate the full set of daterange rows to fill the calendar.
It's a good exercise in what Joe Celko calls "Thinking in Sets" and shows how just how little SQL it takes to manage the data.
This includes some useful general tricks for managing tables of constants and using sequence-generating functions.