Re: Is there a better way to do this?

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: "D(dot) Dante Lorenso" <dante(at)lorenso(dot)com>
Cc: Wei Weng <wweng(at)kencast(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a better way to do this?
Date: 2007-08-28 22:46:25
Message-ID: 83132F43-384B-44EA-AE69-B872CF55EF70@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Aug 28, 2007, at 17:22 , D. Dante Lorenso wrote:

> Michael Glaesemann wrote:
>> On Aug 28, 2007, at 16:55 , D. Dante Lorenso wrote:
>>> RETURN time + (days || ' days')::INTERVAL;
>> It's bad practice to concatenate like this. Use time + days *
>> interval '1 day' and be done with it.
>
> Why? Is this functionality expected to break in the future or has
> unexpected side effects? Is it less clear or less efficient? Who
> declared it bad practice and where can I read that documentation?

It's generally bad practice to interpolate unnecessarily. You're
right, in this case you're probably safe from this particular case
ever changing. I personally find it less clear (though clarity is
often in the eye of the beholder). time + * interval '1 day' is to
me a clearer expression of what you're doing: add this multiple of
days to the time.

(days || ' days')::interval says "Make a string using this value (oh,
it's an int? we need to cast it to text) and this string: the result
just happens to match the proper input format for an interval, which
is fortunate because now we're casting the string to interval". Okay,
there's a little editorializing thrown in, but that's what the
concatenation says to me.

The concatenation is probably less efficient: you're casting an int
to text and then the text to interval with the concatenation you're
using. I don't know how that compares in terms of cycles to the int *
interval math, but efficiency isn't really the reason I would avoid it.

I'm sure others could provide more cogent explanations, but those are
my initial thoughts.

Michael Glaesemann
grzm seespotcode net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2007-08-28 23:01:59 Re: Install on 32 or 64 bit Linux?
Previous Message Alvaro Herrera 2007-08-28 22:40:01 Re: Out of Memory - 8.2.4