Re: Is there a better way to do this? - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: Is there a better way to do this?
Date
Msg-id 83132F43-384B-44EA-AE69-B872CF55EF70@seespotcode.net
Whole thread Raw
In response to Re: Is there a better way to do this?  ("D. Dante Lorenso" <dante@lorenso.com>)
Responses Re: Is there a better way to do this?  (Michael Glaesemann <grzm@seespotcode.net>)
Re: Is there a better way to do this?  ("D. Dante Lorenso" <dante@lorenso.com>)
Re: Is there a better way to do this?  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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



pgsql-general by date:

Previous
From: "Yonatan Ben-Nes"
Date:
Subject: Will Index improve the speed?
Next
From: Ron Johnson
Date:
Subject: Re: Install on 32 or 64 bit Linux?