Documentation for generate_series says that argument can be int or bigint for generate_series(start, stop) and generate_series(start, stop, step) cases and timestamp or timestamp with time zone for generate_series(start, stop, step interval).
What is the reason that generate_series works also with date type as input and returns timestamp with timezone?
pg=# select generate_series('2014-01-01'::date,'2014-01-02'::date,'1 day');
generate_series
------------------------
2014-01-01 00:00:00+01
2014-01-02 00:00:00+01
(2 rows)
Thanks to function type resolution we can also pass
datevalues togenerate_series()because there is an implicit cast fromdatetotimestampas well as fromdatetotimestamptz. Would be ambiguous, buttimestamptzis "preferred" among "Date/time types". Detailed explanation:For a bare
datethe local time00:00is assumed in the cast. Be aware that the current time zone setting directly affects the result if you usedateas input since, obviously, '2014-01-10 00:00' represents a different point in time in Tokio than it does in New York.How does Postgres decide what types are acceptable?
Postgres basically distinguishes between three types of casts:
Explicit casts.. when usingCASTor::syntax.Assignment cast.. implicit cast when a value is assigned to a target column.Implicit cast.. implicit casts in all other expressions.There has to be an implicit cast registered in the system from the input type to the expected type to make a function silently accept (and convert) an input value.
To see which casts are defined to
timestamptz, you can query the catalog tablepg_cast:All of these casts are implicit. The manual on
castcontext:Bold emphasis mine.