I tried to analyze why a certain query takes quite a long time. It runs on a partitioned PostgreSQL (v9.1) table. It is partitioned monthly. The rule is based on the a column which holds an integer representation of the date (so in example 20130801).
If I write a query like this one:
EXPLAIN SELECT DISTINCT (user_id)
FROM users
WHERE
date_tk >= 20130801
Only the relevant partitions get selected. But, when I run something like this, it scans all the partitions:
EXPLAIN SELECT DISTINCT (user_id)
FROM users
WHERE
date_tk >= TO_CHAR(CURRENT_DATE - '30 days'::INTERVAL, 'yyyyMMdd')::INT
Now I casted the constraint date to int, so it seems to be ok I would assume, but unfortunately it isn't. Does somebody have an idea on how to improve this query so that only the relevant partitions get scanned?
Thanks, Diddy