Re: Integer input functions for date and timestamp

Lists: pgsql-hackers
From: Brendan Jurd <direvus(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Integer input functions for date and timestamp
Date: 2010-10-22 18:45:07
Message-ID: AANLkTi=W1wtcL7qR4PuQaQ=UoabmjSUSz6QGJTUCXF-P@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi folks,

In my own databases, I've been using a couple of C functions that
might be useful to the wider community.

They are very simple date/timestamp constructors that take integers as
their arguments. Nothing fancy, but very convenient and *much* faster
than using a SQL or PL/pgSQL workaround.

The offering is analogous to mktime() in C/PHP, the standard datetime
constructors in Python, and Perl's Time::Local. The function
signatures pretty much speak for themselves:

date(year int, month int, day int) returns date
datetime(year int, month int, day int, hour int, minute int, second
int) returns timestamp

Without these functions (or some variation), a user wishing to
construct a date from integers can only assemble the date into a
string and then put that string through postgres' datetime parser,
which is totally perverse.

Is there any interest in adding this to core, or failing that,
contrib? If so I'd be happy to provide a patch including the
functions themselves and some attendant documentation.

I'm not wedded to the function names or argument order, and I realise
a fully realised offering would need to include a variant for
'timestamp with time zone'.

Cheers,
BJ


From: Peter Geoghegan <peter(dot)geoghegan86(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 18:58:29
Message-ID: AANLkTimRjEQH+RpVqNy2c=wjjH8Gt_JyKaf-N39v2xkk@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 22 October 2010 19:45, Brendan Jurd <direvus(at)gmail(dot)com> wrote:
> Hi folks,
>
> In my own databases, I've been using a couple of C functions that
> might be useful to the wider community.
>
> They are very simple date/timestamp constructors that take integers as
> their arguments.  Nothing fancy, but very convenient and *much* faster
> than using a SQL or PL/pgSQL workaround.
>
> The offering is analogous to mktime() in C/PHP, the standard datetime
> constructors in Python, and Perl's Time::Local.  The function
> signatures pretty much speak for themselves:
>
> date(year int, month int, day int) returns date
> datetime(year int, month int, day int, hour int, minute int, second
> int) returns timestamp
>
> Without these functions (or some variation), a user wishing to
> construct a date from integers can only assemble the date into a
> string and then put that string through postgres' datetime parser,
> which is totally perverse.
>
> Is there any interest in adding this to core, or failing that,
> contrib?  If so I'd be happy to provide a patch including the
> functions themselves and some attendant documentation.
>
> I'm not wedded to the function names or argument order, and I realise
> a fully realised offering would need to include a variant for
> 'timestamp with time zone'.

What's wrong with to_timestamp() and to_date()? Sure, your functions
might be marginally faster, but I don't think that it's likely to be a
very performance sensitive area.

--
Regards,
Peter Geoghegan


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 19:12:54
Message-ID: AANLkTinrREOZ=ECnAuAT3RtqFoG3q+4quEDquYiCLQvb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Oct 22, 2010 at 2:45 PM, Brendan Jurd <direvus(at)gmail(dot)com> wrote:
> date(year int, month int, day int) returns date
> datetime(year int, month int, day int, hour int, minute int, second
> int) returns timestamp
>
> Without these functions (or some variation), a user wishing to
> construct a date from integers can only assemble the date into a
> string and then put that string through postgres' datetime parser,
> which is totally perverse.
>
> Is there any interest in adding this to core, or failing that,
> contrib?  If so I'd be happy to provide a patch including the
> functions themselves and some attendant documentation.

I think that would be useful.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Brendan Jurd <direvus(at)gmail(dot)com>
To: Peter Geoghegan <peter(dot)geoghegan86(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 19:15:20
Message-ID: AANLkTikQSy4jbQ3_X5y7SXB5VCHAzeaf2tOZTyV1_jQ_@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 23 October 2010 05:58, Peter Geoghegan <peter(dot)geoghegan86(at)gmail(dot)com> wrote:
> On 22 October 2010 19:45, Brendan Jurd <direvus(at)gmail(dot)com> wrote:
>> Without these functions (or some variation), a user wishing to
>> construct a date from integers can only assemble the date into a
>> string and then put that string through postgres' datetime parser,
>> which is totally perverse.
>
> What's wrong with to_timestamp() and to_date()? Sure, your functions
> might be marginally faster, but I don't think that it's likely to be a
> very performance sensitive area.
>

Hi Peter,

The answer to your question is in the paragraph I quoted from my OP
above. to_timestamp() and to_date() don't offer any non-retarded way
to get from integer values to datetime values. They are great if you
are coming from text, but if you already have integers they are lame.

Perhaps an example would be constructive. Would you rather do this:

datetime(2010, 10, 23, 6, 11, 0)

or this:

to_date(2010::text || '-' || 10::text || '-' || 23::text || ' ' ||
6::text || ':' || 11::text || ':' || 0::text, 'YYYY-MM-DD HH24:MI:SS')

The performance increase is nice, but as you say, this isn't likely to
be in a performance critical path. The main benefits are convenience,
simplicity and readability.

Cheers,
BJ


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 19:15:40
Message-ID: 4CC1E2DC.9040809@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Brendan,

> date(year int, month int, day int) returns date
> datetime(year int, month int, day int, hour int, minute int, second
> int) returns timestamp

a) you'd need to rename these.
b) we'd also want the inverse of these, which would be extremely useful.

> Without these functions (or some variation), a user wishing to
> construct a date from integers can only assemble the date into a
> string and then put that string through postgres' datetime parser,
> which is totally perverse.

"Parsers Gone Wild!" ;-)

--
-- Josh Berkus
PostgreSQL Experts Inc.
http://www.pgexperts.com


From: Brendan Jurd <direvus(at)gmail(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 19:20:31
Message-ID: AANLkTikPJ5Og06EhyVdhwcELO69b_HRTxUEGMJw7r7U_@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 23 October 2010 06:15, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
> a) you'd need to rename these.

I'm open to that. What names would you propose?

> b) we'd also want the inverse of these, which would be extremely useful.

Not a problem.

Cheers,
BJ


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 19:54:01
Message-ID: AANLkTi=6HfxHsTBr0fz0M-uh_xG0Jd1B2iY=mwNrw7eY@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Oct 22, 2010 at 3:20 PM, Brendan Jurd <direvus(at)gmail(dot)com> wrote:
> On 23 October 2010 06:15, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
>> a) you'd need to rename these.
>
> I'm open to that.  What names would you propose?
>
>> b) we'd also want the inverse of these, which would be extremely useful.
>
> Not a problem.

Ooh, I like it. A related personal pet peeve of mine: AFAIK the
easiest way to convert from an integer number of seconds to an
interval representing that many seconds is:

(the_int || ' s')::interval

I guess we don't want to get too carried away with this, but has
anyone else gotten annoyed by this?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Brendan Jurd <direvus(at)gmail(dot)com>, Josh Berkus <josh(at)agliodbs(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 20:13:31
Message-ID: 1287778337-sup-3755@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Robert Haas's message of vie oct 22 16:54:01 -0300 2010:

> Ooh, I like it. A related personal pet peeve of mine: AFAIK the
> easiest way to convert from an integer number of seconds to an
> interval representing that many seconds is:
>
> (the_int || ' s')::interval
>
> I guess we don't want to get too carried away with this, but has
> anyone else gotten annoyed by this?

Why do you go through text and concatenation? I advocate this approach:
the_int * interval '1 second'

I haven't measured it though.

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Brendan Jurd <direvus(at)gmail(dot)com>, Josh Berkus <josh(at)agliodbs(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 20:27:14
Message-ID: 1287778800-sup-4683@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Alvaro Herrera's message of vie oct 22 17:13:31 -0300 2010:
> Excerpts from Robert Haas's message of vie oct 22 16:54:01 -0300 2010:
>
> > Ooh, I like it. A related personal pet peeve of mine: AFAIK the
> > easiest way to convert from an integer number of seconds to an
> > interval representing that many seconds is:
> >
> > (the_int || ' s')::interval
>
> Why do you go through text and concatenation? I advocate this approach:
> the_int * interval '1 second'

A quick tests with pgbench custom scripts says that it's about 10%
faster.

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Brendan Jurd <direvus(at)gmail(dot)com>, Josh Berkus <josh(at)agliodbs(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 20:28:00
Message-ID: AANLkTimyvSkoZxN6d+hgDxQB7xZ6aJOyWk0mMxLfQt_0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Oct 22, 2010 at 4:27 PM, Alvaro Herrera
<alvherre(at)commandprompt(dot)com> wrote:
> Excerpts from Alvaro Herrera's message of vie oct 22 17:13:31 -0300 2010:
>> Excerpts from Robert Haas's message of vie oct 22 16:54:01 -0300 2010:
>>
>> > Ooh, I like it.  A related personal pet peeve of mine: AFAIK the
>> > easiest way to convert from an integer number of seconds to an
>> > interval representing that many seconds is:
>> >
>> > (the_int || ' s')::interval
>>
>> Why do you go through text and concatenation?  I advocate this approach:
>>     the_int * interval '1 second'
>
> A quick tests with pgbench custom scripts says that it's about 10%
> faster.

Hmm, never thought of that approach.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Brendan Jurd <direvus(at)gmail(dot)com>, Josh Berkus <josh(at)agliodbs(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Integer input functions for date and timestamp
Date: 2010-10-22 21:10:42
Message-ID: 29164.1287781842@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> Ooh, I like it. A related personal pet peeve of mine: AFAIK the
> easiest way to convert from an integer number of seconds to an
> interval representing that many seconds is:

> (the_int || ' s')::interval

No, the standard solution is

the_int * '1 second'::interval

which is fast and adapts readily to different units for the interval
number.

regards, tom lane