postgresql - Postegres: Queries with INTERVAL operation don work over constraints -
just in matter of simplification, have following query:
select * table (date_trunc('day',create_time ) > now() - interval '2 days');
running explain got this:
-> seq scan on table (cost=0.00..1.62 rows=10 width=232) filter: (date_trunc('day'::text, create_time) > (now() - '2 days'::**interval**))
as i've highlighted, operation (now() - interval '2 days') returns interval, need timestamp.
in case, how can convert interval timestamp or thing ?
thank answers, think not explained, here's detailed problem:
for performance purposes, have here table named 'transactions' , child tables each day, example 'transactions_2015_05_29'.
on each child table have following constraint:
constraint transactions_2015_05_29_create_time_check check (date_trunc('day'::text, create_time) = '2015-05-29 00:00:00'::timestamp without time zone)
when run following 'explain' following query this:
explain:
explain select * pp_transactions (date_trunc('day', create_row_time) < current_date + interval '1 day'); "result (cost=0.00..120.52 rows=731 width=232)" " -> append (cost=0.00..120.52 rows=731 width=232)" " -> seq scan on transactions (cost=0.00..1.70 rows=10 width=232)" " filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))" " -> seq scan on **transactions_2015_05_28** pp_transactions (cost=0.00..14.65 rows=103 width=232)" " filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)" " -> seq scan on **transactions_2015_05_29** transactions (cost=0.00..16.98 rows=103 width=232)" " filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))" " -> seq scan on **transactions_2015_05_30** transactions (cost=0.00..16.98 rows=103 width=232)" " filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))" " -> seq scan on **transactions_2015_05_31** transactions (cost=0.00..16.98 rows=103 width=232)" " filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))"
as can see, there tables should not there. if run following query correct result on explain:
explain:
explain select * pp_transactions (date_trunc('day', create_row_time) < '2015-05-30 00:00:00'); "result (cost=0.00..30.76 rows=216 width=232)" " -> append (cost=0.00..30.76 rows=216 width=232)" " -> seq scan on transactions (cost=0.00..1.46 rows=10 width=232)" " filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)" " -> seq scan on **transactions_2015_05_28** pp_transactions (cost=0.00..14.65 rows=103 width=232)" " filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)" " -> seq scan on **transactions_2015_05_29** pp_transactions (cost=0.00..14.65 rows=103 width=232)" " filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)"
so, first query must behave second one.
from postgres manual:
- you may add or subtract interval timestamp produce timestamp
timestamp '1999-12-11' + interval '19 days' = timestamp '1999-12-30'
and looks date me (2 days ago @ 19:08 cest), , according manual (once again) now() produces timestamp:
(now() - '2 days'::**interval**)
now() function , '2 days' interval.
Comments
Post a Comment