Postgres 9.3
Debian 7
I'm working on a mobile game project (poker like) where an admin user can register a recurrent tournament. As it's recurrent, I modeled it crontab like as follows.
challenge_schedule
+-----------------------+---------+------------------------------------------------------------------------------------+---------+--------------+-------------+
| Column | Type | Modifiers | Storage | Stats target | Description |
+-----------------------+---------+------------------------------------------------------------------------------------+---------+--------------+-------------+
| challenge_schedule_id | integer | not null default nextval('challenge_schedule_challenge_schedule_id_seq'::regclass) | plain | | |
| challenge_type_id | integer | not null | plain | | |
| game_type_id | integer | not null | plain | | |
| quorum | integer | not null | plain | | |
| fee | integer | | plain | | |
| winnings | integer | | plain | | |
| frequency_minute | integer | | plain | | |
| frequency_hour | integer | | plain | | |
| frequency_dom | integer | | plain | | |
| frequency_month | integer | | plain | | |
| frequency_dow | integer | | plain | | |
| frequency_year | integer | | plain | | |
| description | varchar | |
+-----------------------+---------+------------------------------------------------------------------------------------+---------+--------------+-------------+
And I need to create a job (procedure) that will populate a queue of the next tournaments in the history table as follows.
challenge_history
+----------------------+-----------------------------+----------------------------------------------------------------------------------+----------+--------------+-------------+
| Column | Type | Modifiers | Storage | Stats target | Description |
+----------------------+-----------------------------+----------------------------------------------------------------------------------+----------+--------------+-------------+
| challenge_history_id | integer | not null default nextval('challenge_history_challenge_history_id_seq'::regclass) | plain | | |
| challenge_type_id | integer | | plain | | |
| game_type_id | integer | | plain | | |
| start_time | timestamp without time zone | | plain | | |
| end_time | timestamp without time zone | | plain | | |
| fee | integer | | plain | | |
| winnings | integer | | plain | | |
| special | boolean | | plain | | |
| canceled_time | timestamp without time zone | | plain | | |
| canceled_reason | character varying(255) | | extended | | |
+----------------------+-----------------------------+----------------------------------------------------------------------------------+----------+--------------+-------------+
In other words, if I have only a daily tournament on schedule and run it to process the next 7 days, it needs to create 7 rows with the correct start_time on each. Consider that in a real world it will have a lot of recurrent tournaments daily, weekly, monthly… And it needs to populate everything for the next X days (7 in the example).
I was wondering the best way to solve this and want thoughts/ideas about.
Best Answer
After some time i figured out how to do this. Lets put as sample data 2 recurrent tournaments:
One daily at 16:20 and another every sunday at 14:00
My solution was to make a full join between the schedule and every minute timestamp (as it's the smallest division) from now until the days limit and comparing each timestamp field with each schedule as follows (In the example I'm calling the query to process 5 days (1440 minutes * 5)):
In the comparisons each field needs to be equals to the generated timestamp field OR NULL that means every.
The resultset was:
Hope it helps somebody.