Generating time series between two dates in PostgreSQL
Asked Answered
A

4

150

I have a query like this that nicely generates a series of dates between 2 given dates:

select date '2004-03-07' + j - i as AllDate 
from generate_series(0, extract(doy from date '2004-03-07')::int - 1) as i,
     generate_series(0, extract(doy from date '2004-08-16')::int - 1) as j

It generates 162 dates between 2004-03-07 and 2004-08-16 and this what I want. The problem with this code is that it wouldn't give the right answer when the two dates are from different years, for example when I try 2007-02-01 and 2008-04-01.

Is there a better solution?

Afrika answered 1/1, 2013 at 19:23 Comment(1)
possible duplicate of Getting date list in a range in PostgreSQLThermolysis
A
267

Can be done without conversion to/from int (but to/from timestamp instead)

SELECT date_trunc('day', dd):: date
FROM generate_series
        ( '2007-02-01'::timestamp 
        , '2008-04-01'::timestamp
        , '1 day'::interval) dd
        ;
Adsorbate answered 1/1, 2013 at 19:40 Comment(4)
why is date_trunc needed?Aspidistra
It's just presentation. It eliminates the printing of the time part of the timestamp which is alwas zeros in this case.Enugu
date_trunc is not needed because you're already coercing it to a date type with ::date. It produces the same result with or without it.Hanoi
IIRC date_trunc() was needed in older versions (8.4 ?). Anyway, it won't harm, and you could always try to omit the casts.Adsorbate
L
133

To generate a series of dates this is the optimal way:

SELECT t.day::date 
FROM   generate_series(timestamp '2004-03-07'
                     , timestamp '2004-08-16'
                     , interval  '1 day') AS t(day);
  • Additional date_trunc() is not needed. The cast to date (day::date) does that implicitly.

  • But there is also no point in casting date literals to date as input parameter. Au contraire, timestamp is the best choice. The advantage in performance is small, but there is no reason not to take it. And you do not needlessly involve DST (daylight saving time) rules coupled with the conversion from date to timestamp with time zone and back. See below.

Equivalent, less explicit short syntax:

SELECT day::date 
FROM   generate_series(timestamp '2004-03-07', '2004-08-16', '1 day') day;

Or with the set-returning function in the SELECT list:

SELECT generate_series(timestamp '2004-03-07', '2004-08-16', '1 day')::date AS day;

The AS keyword is required in the last variant, Postgres would misinterpret the column alias day otherwise. And I would not advise that variant before Postgres 10 - at least not with more than one set-returning function in the same SELECT list:

(That aside, the last variant is typically fastest by a tiny margin.)

Why timestamp [without time zone]?

There are a number of overloaded variants of generate_series(). Currently (Postgres 11):

SELECT oid::regprocedure   AS function_signature
     , prorettype::regtype AS return_type
FROM   pg_proc
where  proname = 'generate_series';
function_signature                                                                | return_type                
:-------------------------------------------------------------------------------- | :--------------------------
generate_series(integer,integer,integer)                                          | integer                    
generate_series(integer,integer)                                                  | integer                    
generate_series(bigint,bigint,bigint)                                             | bigint                     
generate_series(bigint,bigint)                                                    | bigint                     
generate_series(numeric,numeric,numeric)                                          | numeric                    
generate_series(numeric,numeric)                                                  | numeric                    
generate_series(timestamp without time zone,timestamp without time zone,interval) | timestamp without time zone
generate_series(timestamp with time zone,timestamp with time zone,interval)       | timestamp with time zone

(numeric variants were added with Postgres 9.5.) The relevant ones are the last two in bold taking and returning timestamp / timestamptz.

There is no variant taking or returning date. An explicit cast is needed to return date. The call with timestamp arguments resolves to the best variant directly without descending into function type resolution rules and without additional cast for the input.

timestamp '2004-03-07' is perfectly valid, btw. The omitted time part defaults to 00:00 with ISO format.

Thanks to function type resolution we can still pass date. But that requires more work from Postgres. There is an implicit cast from date to timestamp as well as one from date to timestamptz. Would be ambiguous, but timestamptz is "preferred" among "date/time types". So the match is decided at step 4d.:

Run through all candidates and keep those that accept preferred types (of the input data type's type category) at the most positions where type conversion will be required. Keep all candidates if none accept preferred types. If only one candidate remains, use it; else continue to the next step.

In addition to the extra work in function type resolution this adds an extra cast to timestamptz - which not only adds more cost, it can also introduce problems with DST leading to unexpected results in rare cases. (DST is a moronic concept, btw, can't stress this enough.) Related:

I added demos to the fiddle showing the more expensive query plan:

db<>fiddle here

Related:

Lindholm answered 30/9, 2017 at 5:33 Comment(7)
Even more shorter version: SELECT generate_series(timestamp '2004-03-07', '2004-08-16', '1 day') :: DATE AS day;Cryptic
What does the t(day) syntax signify?Congregate
@rendang: AS t(day) in SELECT * FROM func() AS t(day) are table and column alias. The AS keyword is optional noise in this context. See: https://mcmap.net/q/28632/-query-to-order-by-the-number-of-rows-returned-from-another-selectLindholm
Are you sure about this? "timestamp '2004-03-07' is perfectly valid, btw. The omitted time part defaults to 00:00 with ISO format." <- it sounds like it would depend on postgres.conf or session timezone, no?Gamelan
@Seivan: Absolutely sure. ISO 8601 format is unambiguous, regardless of locale or datestyle setting. (Recommended for all date / time literals.) See: postgresql.org/docs/current/interactive/…Lindholm
I just missed the [without... part, my bad. Just to be clear, if you omit offset, Z or abrev/name it will actually convert it based on your session, but that's with timestamptz, not timestamp. My bad.Gamelan
betwen years! SELECT t.day::date from generate_series ( ((current_timestamp - interval '70 year')), ((current_timestamp - interval '18 year')), interval '1 day' ) as t(day) order by random() limit 1Micelle
P
41

You can generate series directly with dates. No need to use ints or timestamps:

select date::date 
from generate_series(
  '2004-03-07'::date,
  '2004-08-16'::date,
  '1 day'::interval
) date;
Purse answered 25/2, 2015 at 17:6 Comment(3)
Depending on your time zone, this may return an unexpected result. I had this problem. Use the timestamp instead. SET session TIME zone 'America/Sao_Paulo' SELECT d::date FROM generate_series('2019-11-01'::date, '2019-11-03'::date, '1 day') d SELECT d::date FROM generate_series('2019-11-01'::date, '2019-11-04'::date, '1 day') dHuck
What is the problem with this code? I ran it here (back to version 9.6) and it runs fine!Treadway
@Vérace see Erwin's answerPompadour
D
4

You can also use this.

select generate_series  ( '2012-12-31'::timestamp , '2018-10-31'::timestamp , '1 day'::interval) :: date 
Dolly answered 31/10, 2018 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.